Merge to Fedora kernel-2.6.18-1.2239_FC5 patched with stable patch-2.6.18.2-vs2.0...
[linux-2.6.git] / drivers / ide / ide-io.c
index 248e3cc..fb67952 100644 (file)
@@ -24,7 +24,6 @@
  */
  
  
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/types.h>
 #include <linux/string.h>
@@ -55,8 +54,8 @@
 #include <asm/io.h>
 #include <asm/bitops.h>
 
-int __ide_end_request(ide_drive_t *drive, struct request *rq, int uptodate,
-                     int nr_sectors)
+static int __ide_end_request(ide_drive_t *drive, struct request *rq,
+                            int uptodate, int nr_sectors)
 {
        int ret = 1;
 
@@ -83,18 +82,14 @@ int __ide_end_request(ide_drive_t *drive, struct request *rq, int uptodate,
 
        if (!end_that_request_first(rq, uptodate, nr_sectors)) {
                add_disk_randomness(rq->rq_disk);
-
-               if (blk_rq_tagged(rq))
-                       blk_queue_end_tag(drive->queue, rq);
-
                blkdev_dequeue_request(rq);
                HWGROUP(drive)->rq = NULL;
-               end_that_request_last(rq);
+               end_that_request_last(rq, uptodate);
                ret = 0;
        }
+
        return ret;
 }
-EXPORT_SYMBOL(__ide_end_request);
 
 /**
  *     ide_end_request         -       complete an IDE I/O
@@ -113,16 +108,17 @@ int ide_end_request (ide_drive_t *drive, int uptodate, int nr_sectors)
        unsigned long flags;
        int ret = 1;
 
+       /*
+        * room for locking improvements here, the calls below don't
+        * need the queue lock held at all
+        */
        spin_lock_irqsave(&ide_lock, flags);
        rq = HWGROUP(drive)->rq;
 
        if (!nr_sectors)
                nr_sectors = rq->hard_cur_sectors;
 
-       if (blk_complete_barrier_rq_locked(drive->queue, rq, nr_sectors))
-               ret = rq->nr_sectors != 0;
-       else
-               ret = __ide_end_request(drive, rq, uptodate, nr_sectors);
+       ret = __ide_end_request(drive, rq, uptodate, nr_sectors);
 
        spin_unlock_irqrestore(&ide_lock, flags);
        return ret;
@@ -145,38 +141,41 @@ enum {
 
 static void ide_complete_power_step(ide_drive_t *drive, struct request *rq, u8 stat, u8 error)
 {
+       struct request_pm_state *pm = rq->end_io_data;
+
        if (drive->media != ide_disk)
                return;
 
-       switch (rq->pm->pm_step) {
+       switch (pm->pm_step) {
        case ide_pm_flush_cache:        /* Suspend step 1 (flush cache) complete */
-               if (rq->pm->pm_state == 4)
-                       rq->pm->pm_step = ide_pm_state_completed;
+               if (pm->pm_state == PM_EVENT_FREEZE)
+                       pm->pm_step = ide_pm_state_completed;
                else
-                       rq->pm->pm_step = idedisk_pm_standby;
+                       pm->pm_step = idedisk_pm_standby;
                break;
        case idedisk_pm_standby:        /* Suspend step 2 (standby) complete */
-               rq->pm->pm_step = ide_pm_state_completed;
+               pm->pm_step = ide_pm_state_completed;
                break;
        case idedisk_pm_idle:           /* Resume step 1 (idle) complete */
-               rq->pm->pm_step = ide_pm_restore_dma;
+               pm->pm_step = ide_pm_restore_dma;
                break;
        }
 }
 
 static ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
 {
+       struct request_pm_state *pm = rq->end_io_data;
        ide_task_t *args = rq->special;
 
        memset(args, 0, sizeof(*args));
 
        if (drive->media != ide_disk) {
                /* skip idedisk_pm_idle for ATAPI devices */
-               if (rq->pm->pm_step == idedisk_pm_idle)
-                       rq->pm->pm_step = ide_pm_restore_dma;
+               if (pm->pm_step == idedisk_pm_idle)
+                       pm->pm_step = ide_pm_restore_dma;
        }
 
-       switch (rq->pm->pm_step) {
+       switch (pm->pm_step) {
        case ide_pm_flush_cache:        /* Suspend step 1 (flush cache) */
                if (drive->media != ide_disk)
                        break;
@@ -218,10 +217,67 @@ static ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *
                drive->hwif->ide_dma_check(drive);
                break;
        }
-       rq->pm->pm_step = ide_pm_state_completed;
+       pm->pm_step = ide_pm_state_completed;
        return ide_stopped;
 }
 
+/**
+ *     ide_end_dequeued_request        -       complete an IDE I/O
+ *     @drive: IDE device for the I/O
+ *     @uptodate:
+ *     @nr_sectors: number of sectors completed
+ *
+ *     Complete an I/O that is no longer on the request queue. This
+ *     typically occurs when we pull the request and issue a REQUEST_SENSE.
+ *     We must still finish the old request but we must not tamper with the
+ *     queue in the meantime.
+ *
+ *     NOTE: This path does not handle barrier, but barrier is not supported
+ *     on ide-cd anyway.
+ */
+
+int ide_end_dequeued_request(ide_drive_t *drive, struct request *rq,
+                            int uptodate, int nr_sectors)
+{
+       unsigned long flags;
+       int ret = 1;
+
+       spin_lock_irqsave(&ide_lock, flags);
+
+       BUG_ON(!(rq->flags & REQ_STARTED));
+
+       /*
+        * if failfast is set on a request, override number of sectors and
+        * complete the whole request right now
+        */
+       if (blk_noretry_request(rq) && end_io_error(uptodate))
+               nr_sectors = rq->hard_nr_sectors;
+
+       if (!blk_fs_request(rq) && end_io_error(uptodate) && !rq->errors)
+               rq->errors = -EIO;
+
+       /*
+        * decide whether to reenable DMA -- 3 is a random magic for now,
+        * if we DMA timeout more than 3 times, just stay in PIO
+        */
+       if (drive->state == DMA_PIO_RETRY && drive->retry_pio <= 3) {
+               drive->state = 0;
+               HWGROUP(drive)->hwif->ide_dma_on(drive);
+       }
+
+       if (!end_that_request_first(rq, uptodate, nr_sectors)) {
+               add_disk_randomness(rq->rq_disk);
+               if (blk_rq_tagged(rq))
+                       blk_queue_end_tag(drive->queue, rq);
+               end_that_request_last(rq, uptodate);
+               ret = 0;
+       }
+       spin_unlock_irqrestore(&ide_lock, flags);
+       return ret;
+}
+EXPORT_SYMBOL_GPL(ide_end_dequeued_request);
+
+
 /**
  *     ide_complete_pm_request - end the current Power Management request
  *     @drive: target drive
@@ -247,7 +303,7 @@ static void ide_complete_pm_request (ide_drive_t *drive, struct request *rq)
        }
        blkdev_dequeue_request(rq);
        HWGROUP(drive)->rq = NULL;
-       end_that_request_last(rq);
+       end_that_request_last(rq, 1);
        spin_unlock_irqrestore(&ide_lock, flags);
 }
 
@@ -365,12 +421,13 @@ void ide_end_drive_cmd (ide_drive_t *drive, u8 stat, u8 err)
                        }
                }
        } else if (blk_pm_request(rq)) {
+               struct request_pm_state *pm = rq->end_io_data;
 #ifdef DEBUG_PM
                printk("%s: complete_power_step(step: %d, stat: %x, err: %x)\n",
                        drive->name, rq->pm->pm_step, stat, err);
 #endif
                ide_complete_power_step(drive, rq, stat, err);
-               if (rq->pm->pm_step == ide_pm_state_completed)
+               if (pm->pm_step == ide_pm_state_completed)
                        ide_complete_pm_request(drive, rq);
                return;
        }
@@ -379,7 +436,7 @@ void ide_end_drive_cmd (ide_drive_t *drive, u8 stat, u8 err)
        blkdev_dequeue_request(rq);
        HWGROUP(drive)->rq = NULL;
        rq->errors = err;
-       end_that_request_last(rq);
+       end_that_request_last(rq, !rq->errors);
        spin_unlock_irqrestore(&ide_lock, flags);
 }
 
@@ -447,7 +504,7 @@ static ide_startstop_t ide_ata_error(ide_drive_t *drive, struct request *rq, u8
                }
        }
 
-       if ((stat & DRQ_STAT) && rq_data_dir(rq) == READ)
+       if ((stat & DRQ_STAT) && rq_data_dir(rq) == READ && hwif->err_stops_fifo == 0)
                try_to_flush_leftover_data(drive);
 
        if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT))
@@ -560,7 +617,7 @@ ide_startstop_t __ide_abort(ide_drive_t *drive, struct request *rq)
 EXPORT_SYMBOL_GPL(__ide_abort);
 
 /**
- *     ide_abort       -       abort pending IDE operatins
+ *     ide_abort       -       abort pending IDE operations
  *     @drive: drive the error occurred on
  *     @msg: message to report
  *
@@ -623,7 +680,7 @@ static void ide_cmd (ide_drive_t *drive, u8 cmd, u8 nsect,
  *     @drive: drive the completion interrupt occurred on
  *
  *     drive_cmd_intr() is invoked on completion of a special DRIVE_CMD.
- *     We do any necessary daya reading and then wait for the drive to
+ *     We do any necessary data reading and then wait for the drive to
  *     go non busy. At that point we may read the error data and complete
  *     the request
  */
@@ -636,7 +693,7 @@ static ide_startstop_t drive_cmd_intr (ide_drive_t *drive)
        u8 stat = hwif->INB(IDE_STATUS_REG);
        int retries = 10;
 
-       local_irq_enable();
+       local_irq_enable_in_hardirq();
        if ((stat & DRQ_STAT) && args && args[3]) {
                u8 io_32bit = drive->io_32bit;
                drive->io_32bit = 0;
@@ -773,7 +830,7 @@ EXPORT_SYMBOL_GPL(ide_init_sg_cmd);
 
 /**
  *     execute_drive_command   -       issue special drive command
- *     @drive: the drive to issue th command on
+ *     @drive: the drive to issue the command on
  *     @rq: the request structure holding the command
  *
  *     execute_drive_cmd() issues a special drive command,  usually 
@@ -874,6 +931,39 @@ done:
        return ide_stopped;
 }
 
+static void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
+{
+       struct request_pm_state *pm = rq->end_io_data;
+
+       if (blk_pm_suspend_request(rq) &&
+           pm->pm_step == ide_pm_state_start_suspend)
+               /* Mark drive blocked when starting the suspend sequence. */
+               drive->blocked = 1;
+       else if (blk_pm_resume_request(rq) &&
+                pm->pm_step == ide_pm_state_start_resume) {
+               /* 
+                * The first thing we do on wakeup is to wait for BSY bit to
+                * go away (with a looong timeout) as a drive on this hwif may
+                * just be POSTing itself.
+                * We do that before even selecting as the "other" device on
+                * the bus may be broken enough to walk on our toes at this
+                * point.
+                */
+               int rc;
+#ifdef DEBUG_PM
+               printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
+#endif
+               rc = ide_wait_not_busy(HWIF(drive), 35000);
+               if (rc)
+                       printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
+               SELECT_DRIVE(drive);
+               HWIF(drive)->OUTB(8, HWIF(drive)->io_ports[IDE_CONTROL_OFFSET]);
+               rc = ide_wait_not_busy(HWIF(drive), 100000);
+               if (rc)
+                       printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
+       }
+}
+
 /**
  *     start_request   -       start of I/O and command issuing for IDE
  *
@@ -912,33 +1002,8 @@ static ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
        if (block == 0 && drive->remap_0_to_1 == 1)
                block = 1;  /* redirect MBR access to EZ-Drive partn table */
 
-       if (blk_pm_suspend_request(rq) &&
-           rq->pm->pm_step == ide_pm_state_start_suspend)
-               /* Mark drive blocked when starting the suspend sequence. */
-               drive->blocked = 1;
-       else if (blk_pm_resume_request(rq) &&
-                rq->pm->pm_step == ide_pm_state_start_resume) {
-               /* 
-                * The first thing we do on wakeup is to wait for BSY bit to
-                * go away (with a looong timeout) as a drive on this hwif may
-                * just be POSTing itself.
-                * We do that before even selecting as the "other" device on
-                * the bus may be broken enough to walk on our toes at this
-                * point.
-                */
-               int rc;
-#ifdef DEBUG_PM
-               printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
-#endif
-               rc = ide_wait_not_busy(HWIF(drive), 35000);
-               if (rc)
-                       printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
-               SELECT_DRIVE(drive);
-               HWIF(drive)->OUTB(8, HWIF(drive)->io_ports[IDE_CONTROL_OFFSET]);
-               rc = ide_wait_not_busy(HWIF(drive), 10000);
-               if (rc)
-                       printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
-       }
+       if (blk_pm_request(rq))
+               ide_check_pm_state(drive, rq);
 
        SELECT_DRIVE(drive);
        if (ide_wait_stat(&startstop, drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
@@ -953,13 +1018,14 @@ static ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
                else if (rq->flags & REQ_DRIVE_TASKFILE)
                        return execute_drive_cmd(drive, rq);
                else if (blk_pm_request(rq)) {
+                       struct request_pm_state *pm = rq->end_io_data;
 #ifdef DEBUG_PM
                        printk("%s: start_power_step(step: %d)\n",
                                drive->name, rq->pm->pm_step);
 #endif
                        startstop = ide_start_power_step(drive, rq);
                        if (startstop == ide_stopped &&
-                           rq->pm->pm_step == ide_pm_state_completed)
+                           pm->pm_step == ide_pm_state_completed)
                                ide_complete_pm_request(drive, rq);
                        return startstop;
                }
@@ -1101,6 +1167,7 @@ static void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq)
        ide_hwif_t      *hwif;
        struct request  *rq;
        ide_startstop_t startstop;
+       int             loops = 0;
 
        /* for atari only: POSSIBLY BROKEN HERE(?) */
        ide_get_lock(ide_intr, hwgroup);
@@ -1153,6 +1220,7 @@ static void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq)
                        /* no more work for this hwgroup (for now) */
                        return;
                }
+       again:
                hwif = HWIF(drive);
                if (hwgroup->hwif->sharing_irq &&
                    hwif != hwgroup->hwif &&
@@ -1192,8 +1260,14 @@ static void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq)
                 * though. I hope that doesn't happen too much, hopefully not
                 * unless the subdriver triggers such a thing in its own PM
                 * state machine.
+                *
+                * We count how many times we loop here to make sure we service
+                * all drives in the hwgroup without looping for ever
                 */
                if (drive->blocked && !blk_pm_request(rq) && !(rq->flags & REQ_PREEMPT)) {
+                       drive = drive->next ? drive->next : hwgroup->drive;
+                       if (loops++ < 4 && !blk_queue_plugged(drive->queue))
+                               goto again;
                        /* We clear busy, there should be no pending ATA command at this point. */
                        hwgroup->busy = 0;
                        break;
@@ -1212,7 +1286,7 @@ static void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq)
                if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
                        disable_irq_nosync(hwif->irq);
                spin_unlock(&ide_lock);
-               local_irq_enable();
+               local_irq_enable_in_hardirq();
                        /* allow other IRQs while we start this request */
                startstop = start_request(drive, rq);
                spin_lock_irq(&ide_lock);
@@ -1557,7 +1631,7 @@ irqreturn_t ide_intr (int irq, void *dev_id, struct pt_regs *regs)
        spin_unlock(&ide_lock);
 
        if (drive->unmask)
-               local_irq_enable();
+               local_irq_enable_in_hardirq();
        /* service this interrupt, may set handler for next interrupt */
        startstop = handler(drive);
        spin_lock_irq(&ide_lock);
@@ -1590,7 +1664,7 @@ irqreturn_t ide_intr (int irq, void *dev_id, struct pt_regs *regs)
  *     Initialize a request before we fill it in and send it down to
  *     ide_do_drive_cmd. Commands must be set up by this function. Right
  *     now it doesn't do a lot, but if that changes abusers will have a
- *     nasty suprise.
+ *     nasty surprise.
  */
 
 void ide_init_drive_cmd (struct request *rq)
@@ -1621,12 +1695,6 @@ EXPORT_SYMBOL(ide_init_drive_cmd);
  *     for the new rq to be completed.  This is VERY DANGEROUS, and is
  *     intended for careful use by the ATAPI tape/cdrom driver code.
  *
- *     If action is ide_next, then the rq is queued immediately after
- *     the currently-being-processed-request (if any), and the function
- *     returns without waiting for the new rq to be completed.  As above,
- *     This is VERY DANGEROUS, and is intended for careful use by the
- *     ATAPI tape/cdrom driver code.
- *
  *     If action is ide_end, then the rq is queued at the end of the
  *     request queue, and the function returns immediately without waiting
  *     for the new rq to be completed. This is again intended for careful
@@ -1637,7 +1705,7 @@ int ide_do_drive_cmd (ide_drive_t *drive, struct request *rq, ide_action_t actio
 {
        unsigned long flags;
        ide_hwgroup_t *hwgroup = HWGROUP(drive);
-       DECLARE_COMPLETION(wait);
+       DECLARE_COMPLETION_ONSTACK(wait);
        int where = ELEVATOR_INSERT_BACK, err;
        int must_wait = (action == ide_wait || action == ide_head_wait);