]> err.no Git - linux-2.6/blobdiff - drivers/ide/ide-iops.c
ide: add ide_read_bcount_and_ireason() helper
[linux-2.6] / drivers / ide / ide-iops.c
index 57d9a9a79a6f845842a9b1baeb7d20254754dda8..cb11c7861a509ef050e2dddda8755616c38587e7 100644 (file)
@@ -42,7 +42,7 @@ static void ide_outb (u8 val, unsigned long port)
        outb(val, port);
 }
 
-static void ide_outbsync (ide_drive_t *drive, u8 addr, unsigned long port)
+static void ide_outbsync(ide_hwif_t *hwif, u8 addr, unsigned long port)
 {
        outb(addr, port);
 }
@@ -68,7 +68,7 @@ static void ide_mm_outb (u8 value, unsigned long port)
        writeb(value, (void __iomem *) port);
 }
 
-static void ide_mm_outbsync (ide_drive_t *drive, u8 value, unsigned long port)
+static void ide_mm_outbsync(ide_hwif_t *hwif, u8 value, unsigned long port)
 {
        writeb(value, (void __iomem *) port);
 }
@@ -88,14 +88,18 @@ void SELECT_DRIVE (ide_drive_t *drive)
 {
        ide_hwif_t *hwif = drive->hwif;
        const struct ide_port_ops *port_ops = hwif->port_ops;
+       ide_task_t task;
 
        if (port_ops && port_ops->selectproc)
                port_ops->selectproc(drive);
 
-       hwif->OUTB(drive->select.all, hwif->io_ports.device_addr);
+       memset(&task, 0, sizeof(task));
+       task.tf_flags = IDE_TFLAG_OUT_DEVICE;
+
+       drive->hwif->tf_load(drive, &task);
 }
 
-void SELECT_MASK (ide_drive_t *drive, int mask)
+void SELECT_MASK(ide_drive_t *drive, int mask)
 {
        const struct ide_port_ops *port_ops = drive->hwif->port_ops;
 
@@ -103,6 +107,55 @@ void SELECT_MASK (ide_drive_t *drive, int mask)
                port_ops->maskproc(drive, mask);
 }
 
+static void ide_exec_command(ide_hwif_t *hwif, u8 cmd)
+{
+       if (hwif->host_flags & IDE_HFLAG_MMIO)
+               writeb(cmd, (void __iomem *)hwif->io_ports.command_addr);
+       else
+               outb(cmd, hwif->io_ports.command_addr);
+}
+
+static u8 ide_read_status(ide_hwif_t *hwif)
+{
+       if (hwif->host_flags & IDE_HFLAG_MMIO)
+               return readb((void __iomem *)hwif->io_ports.status_addr);
+       else
+               return inb(hwif->io_ports.status_addr);
+}
+
+static u8 ide_read_altstatus(ide_hwif_t *hwif)
+{
+       if (hwif->host_flags & IDE_HFLAG_MMIO)
+               return readb((void __iomem *)hwif->io_ports.ctl_addr);
+       else
+               return inb(hwif->io_ports.ctl_addr);
+}
+
+static u8 ide_read_sff_dma_status(ide_hwif_t *hwif)
+{
+       if (hwif->host_flags & IDE_HFLAG_MMIO)
+               return readb((void __iomem *)(hwif->dma_base + ATA_DMA_STATUS));
+       else
+               return inb(hwif->dma_base + ATA_DMA_STATUS);
+}
+
+static void ide_set_irq(ide_hwif_t *hwif, int on)
+{
+       u8 ctl = ATA_DEVCTL_OBS;
+
+       if (on == 4) { /* hack for SRST */
+               ctl |= 4;
+               on &= ~4;
+       }
+
+       ctl |= on ? 0 : 2;
+
+       if (hwif->host_flags & IDE_HFLAG_MMIO)
+               writeb(ctl, (void __iomem *)hwif->io_ports.ctl_addr);
+       else
+               outb(ctl, hwif->io_ports.ctl_addr);
+}
+
 static void ide_tf_load(ide_drive_t *drive, ide_task_t *task)
 {
        ide_hwif_t *hwif = drive->hwif;
@@ -120,11 +173,6 @@ static void ide_tf_load(ide_drive_t *drive, ide_task_t *task)
        if (task->tf_flags & IDE_TFLAG_FLAGGED)
                HIHI = 0xFF;
 
-       ide_set_irq(drive, 1);
-
-       if ((task->tf_flags & IDE_TFLAG_NO_SELECT_MASK) == 0)
-               SELECT_MASK(drive, 0);
-
        if (task->tf_flags & IDE_TFLAG_OUT_DATA) {
                u16 data = (tf->hob_data << 8) | tf->data;
 
@@ -191,8 +239,10 @@ static void ide_tf_read(ide_drive_t *drive, ide_task_t *task)
        }
 
        /* be sure we're looking at the low order bits */
-       tf_outb(drive->ctl & ~0x80, io_ports->ctl_addr);
+       tf_outb(ATA_DEVCTL_OBS & ~0x80, io_ports->ctl_addr);
 
+       if (task->tf_flags & IDE_TFLAG_IN_FEATURE)
+               tf->feature = tf_inb(io_ports->feature_addr);
        if (task->tf_flags & IDE_TFLAG_IN_NSECT)
                tf->nsect  = tf_inb(io_ports->nsect_addr);
        if (task->tf_flags & IDE_TFLAG_IN_LBAL)
@@ -205,7 +255,7 @@ static void ide_tf_read(ide_drive_t *drive, ide_task_t *task)
                tf->device = tf_inb(io_ports->device_addr);
 
        if (task->tf_flags & IDE_TFLAG_LBA48) {
-               tf_outb(drive->ctl | 0x80, io_ports->ctl_addr);
+               tf_outb(ATA_DEVCTL_OBS | 0x80, io_ports->ctl_addr);
 
                if (task->tf_flags & IDE_TFLAG_IN_HOB_FEATURE)
                        tf->hob_feature = tf_inb(io_ports->feature_addr);
@@ -328,6 +378,13 @@ static void ata_output_data(ide_drive_t *drive, struct request *rq,
 
 void default_hwif_transport(ide_hwif_t *hwif)
 {
+       hwif->exec_command        = ide_exec_command;
+       hwif->read_status         = ide_read_status;
+       hwif->read_altstatus      = ide_read_altstatus;
+       hwif->read_sff_dma_status = ide_read_sff_dma_status;
+
+       hwif->set_irq     = ide_set_irq;
+
        hwif->tf_load     = ide_tf_load;
        hwif->tf_read     = ide_tf_read;
 
@@ -335,6 +392,34 @@ void default_hwif_transport(ide_hwif_t *hwif)
        hwif->output_data = ata_output_data;
 }
 
+u8 ide_read_error(ide_drive_t *drive)
+{
+       ide_task_t task;
+
+       memset(&task, 0, sizeof(task));
+       task.tf_flags = IDE_TFLAG_IN_FEATURE;
+
+       drive->hwif->tf_read(drive, &task);
+
+       return task.tf.error;
+}
+EXPORT_SYMBOL_GPL(ide_read_error);
+
+void ide_read_bcount_and_ireason(ide_drive_t *drive, u16 *bcount, u8 *ireason)
+{
+       ide_task_t task;
+
+       memset(&task, 0, sizeof(task));
+       task.tf_flags = IDE_TFLAG_IN_LBAH | IDE_TFLAG_IN_LBAM |
+                       IDE_TFLAG_IN_NSECT;
+
+       drive->hwif->tf_read(drive, &task);
+
+       *bcount = (task.tf.lbah << 8) | task.tf.lbam;
+       *ireason = task.tf.nsect & 3;
+}
+EXPORT_SYMBOL_GPL(ide_read_bcount_and_ireason);
+
 void ide_fix_driveid (struct hd_driveid *id)
 {
 #ifndef __LITTLE_ENDIAN
@@ -488,10 +573,10 @@ int drive_is_ready (ide_drive_t *drive)
         * about possible isa-pnp and pci-pnp issues yet.
         */
        if (hwif->io_ports.ctl_addr)
-               stat = ide_read_altstatus(drive);
+               stat = hwif->read_altstatus(hwif);
        else
                /* Note: this may clear a pending IRQ!! */
-               stat = ide_read_status(drive);
+               stat = hwif->read_status(hwif);
 
        if (stat & BUSY_STAT)
                /* drive busy:  definitely not interrupting */
@@ -516,24 +601,25 @@ EXPORT_SYMBOL(drive_is_ready);
  */
 static int __ide_wait_stat(ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout, u8 *rstat)
 {
+       ide_hwif_t *hwif = drive->hwif;
        unsigned long flags;
        int i;
        u8 stat;
 
        udelay(1);      /* spec allows drive 400ns to assert "BUSY" */
-       stat = ide_read_status(drive);
+       stat = hwif->read_status(hwif);
 
        if (stat & BUSY_STAT) {
                local_irq_set(flags);
                timeout += jiffies;
-               while ((stat = ide_read_status(drive)) & BUSY_STAT) {
+               while ((stat = hwif->read_status(hwif)) & BUSY_STAT) {
                        if (time_after(jiffies, timeout)) {
                                /*
                                 * One last read after the timeout in case
                                 * heavy interrupt load made us not make any
                                 * progress during the timeout..
                                 */
-                               stat = ide_read_status(drive);
+                               stat = hwif->read_status(hwif);
                                if (!(stat & BUSY_STAT))
                                        break;
 
@@ -553,7 +639,7 @@ static int __ide_wait_stat(ide_drive_t *drive, u8 good, u8 bad, unsigned long ti
         */
        for (i = 0; i < 10; i++) {
                udelay(1);
-               stat = ide_read_status(drive);
+               stat = hwif->read_status(hwif);
 
                if (OK_STAT(stat, good, bad)) {
                        *rstat = stat;
@@ -689,9 +775,9 @@ int ide_driveid_update(ide_drive_t *drive)
         */
 
        SELECT_MASK(drive, 1);
-       ide_set_irq(drive, 1);
+       hwif->set_irq(hwif, 0);
        msleep(50);
-       hwif->OUTBSYNC(drive, WIN_IDENTIFY, hwif->io_ports.command_addr);
+       hwif->exec_command(hwif, WIN_IDENTIFY);
        timeout = jiffies + WAIT_WORSTCASE;
        do {
                if (time_after(jiffies, timeout)) {
@@ -700,11 +786,11 @@ int ide_driveid_update(ide_drive_t *drive)
                }
 
                msleep(50);     /* give drive a breather */
-               stat = ide_read_altstatus(drive);
+               stat = hwif->read_altstatus(hwif);
        } while (stat & BUSY_STAT);
 
        msleep(50);     /* wait for IRQ and DRQ_STAT */
-       stat = ide_read_status(drive);
+       stat = hwif->read_status(hwif);
 
        if (!OK_STAT(stat, DRQ_STAT, BAD_R_STAT)) {
                SELECT_MASK(drive, 0);
@@ -719,7 +805,7 @@ int ide_driveid_update(ide_drive_t *drive)
                return 0;
        }
        hwif->input_data(drive, NULL, id, SECTOR_SIZE);
-       (void)ide_read_status(drive);   /* clear drive IRQ */
+       (void)hwif->read_status(hwif);  /* clear drive IRQ */
        local_irq_enable();
        local_irq_restore(flags);
        ide_fix_driveid(id);
@@ -740,12 +826,9 @@ int ide_driveid_update(ide_drive_t *drive)
 int ide_config_drive_speed(ide_drive_t *drive, u8 speed)
 {
        ide_hwif_t *hwif = drive->hwif;
-       struct ide_io_ports *io_ports = &hwif->io_ports;
        int error = 0;
        u8 stat;
-
-//     while (HWGROUP(drive)->busy)
-//             msleep(50);
+       ide_task_t task;
 
 #ifdef CONFIG_BLK_DEV_IDEDMA
        if (hwif->dma_ops)      /* check if host supports DMA */
@@ -778,12 +861,19 @@ int ide_config_drive_speed(ide_drive_t *drive, u8 speed)
        SELECT_DRIVE(drive);
        SELECT_MASK(drive, 0);
        udelay(1);
-       ide_set_irq(drive, 0);
-       hwif->OUTB(speed, io_ports->nsect_addr);
-       hwif->OUTB(SETFEATURES_XFER, io_ports->feature_addr);
-       hwif->OUTBSYNC(drive, WIN_SETFEATURES, io_ports->command_addr);
+       hwif->set_irq(hwif, 0);
+
+       memset(&task, 0, sizeof(task));
+       task.tf_flags = IDE_TFLAG_OUT_FEATURE | IDE_TFLAG_OUT_NSECT;
+       task.tf.feature = SETFEATURES_XFER;
+       task.tf.nsect   = speed;
+
+       hwif->tf_load(drive, &task);
+
+       hwif->exec_command(hwif, WIN_SETFEATURES);
+
        if (drive->quirk_list == 2)
-               ide_set_irq(drive, 1);
+               hwif->set_irq(hwif, 1);
 
        error = __ide_wait_stat(drive, drive->ready_stat,
                                BUSY_STAT|DRQ_STAT|ERR_STAT,
@@ -889,7 +979,7 @@ void ide_execute_command(ide_drive_t *drive, u8 cmd, ide_handler_t *handler,
 
        spin_lock_irqsave(&ide_lock, flags);
        __ide_set_handler(drive, handler, timeout, expiry);
-       hwif->OUTBSYNC(drive, cmd, hwif->io_ports.command_addr);
+       hwif->exec_command(hwif, cmd);
        /*
         * Drive takes 400nS to respond, we must avoid the IRQ being
         * serviced before that.
@@ -907,12 +997,20 @@ void ide_execute_pkt_cmd(ide_drive_t *drive)
        unsigned long flags;
 
        spin_lock_irqsave(&ide_lock, flags);
-       hwif->OUTBSYNC(drive, WIN_PACKETCMD, hwif->io_ports.command_addr);
+       hwif->exec_command(hwif, WIN_PACKETCMD);
        ndelay(400);
        spin_unlock_irqrestore(&ide_lock, flags);
 }
 EXPORT_SYMBOL_GPL(ide_execute_pkt_cmd);
 
+static inline void ide_complete_drive_reset(ide_drive_t *drive, int err)
+{
+       struct request *rq = drive->hwif->hwgroup->rq;
+
+       if (rq && blk_special_request(rq) && rq->cmd[0] == REQ_DRIVE_RESET)
+               ide_end_request(drive, err ? err : 1, 0);
+}
+
 /* needed below */
 static ide_startstop_t do_reset1 (ide_drive_t *, int);
 
@@ -924,12 +1022,13 @@ static ide_startstop_t do_reset1 (ide_drive_t *, int);
  */
 static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive)
 {
-       ide_hwgroup_t *hwgroup  = HWGROUP(drive);
+       ide_hwif_t *hwif = drive->hwif;
+       ide_hwgroup_t *hwgroup = hwif->hwgroup;
        u8 stat;
 
        SELECT_DRIVE(drive);
        udelay (10);
-       stat = ide_read_status(drive);
+       stat = hwif->read_status(hwif);
 
        if (OK_STAT(stat, 0, BUSY_STAT))
                printk("%s: ATAPI reset complete\n", drive->name);
@@ -948,7 +1047,7 @@ static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive)
        }
        /* done polling */
        hwgroup->polling = 0;
-       hwgroup->resetting = 0;
+       ide_complete_drive_reset(drive, 0);
        return ide_stopped;
 }
 
@@ -964,16 +1063,18 @@ static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
        ide_hwif_t *hwif        = HWIF(drive);
        const struct ide_port_ops *port_ops = hwif->port_ops;
        u8 tmp;
+       int err = 0;
 
        if (port_ops && port_ops->reset_poll) {
-               if (port_ops->reset_poll(drive)) {
+               err = port_ops->reset_poll(drive);
+               if (err) {
                        printk(KERN_ERR "%s: host reset_poll failure for %s.\n",
                                hwif->name, drive->name);
-                       return ide_stopped;
+                       goto out;
                }
        }
 
-       tmp = ide_read_status(drive);
+       tmp = hwif->read_status(hwif);
 
        if (!OK_STAT(tmp, 0, BUSY_STAT)) {
                if (time_before(jiffies, hwgroup->poll_timeout)) {
@@ -983,6 +1084,7 @@ static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
                }
                printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
                drive->failures++;
+               err = -EIO;
        } else  {
                printk("%s: reset: ", hwif->name);
                tmp = ide_read_error(drive);
@@ -1009,10 +1111,12 @@ static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
                        if (tmp & 0x80)
                                printk("; slave: failed");
                        printk("\n");
+                       err = -EIO;
                }
        }
+out:
        hwgroup->polling = 0;   /* done polling */
-       hwgroup->resetting = 0; /* done reset attempt */
+       ide_complete_drive_reset(drive, err);
        return ide_stopped;
 }
 
@@ -1085,7 +1189,6 @@ static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
        ide_hwgroup_t *hwgroup;
        struct ide_io_ports *io_ports;
        const struct ide_port_ops *port_ops;
-       u8 ctl;
 
        spin_lock_irqsave(&ide_lock, flags);
        hwif = HWIF(drive);
@@ -1098,11 +1201,10 @@ static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
 
        /* For an ATAPI device, first try an ATAPI SRST. */
        if (drive->media != ide_disk && !do_not_try_atapi) {
-               hwgroup->resetting = 1;
                pre_reset(drive);
                SELECT_DRIVE(drive);
                udelay (20);
-               hwif->OUTBSYNC(drive, WIN_SRST, io_ports->command_addr);
+               hwif->exec_command(hwif, WIN_SRST);
                ndelay(400);
                hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
                hwgroup->polling = 1;
@@ -1120,10 +1222,10 @@ static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
 
        if (io_ports->ctl_addr == 0) {
                spin_unlock_irqrestore(&ide_lock, flags);
+               ide_complete_drive_reset(drive, -ENXIO);
                return ide_stopped;
        }
 
-       hwgroup->resetting = 1;
        /*
         * Note that we also set nIEN while resetting the device,
         * to mask unwanted interrupts from the interface during the reset.
@@ -1131,16 +1233,15 @@ static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
         * immediate interrupt due to the edge transition it produces.
         * This single interrupt gives us a "fast poll" for drives that
         * recover from reset very quickly, saving us the first 50ms wait time.
+        *
+        * TODO: add ->softreset method and stop abusing ->set_irq
         */
        /* set SRST and nIEN */
-       hwif->OUTBSYNC(drive, drive->ctl|6, io_ports->ctl_addr);
+       hwif->set_irq(hwif, 4);
        /* more than enough time */
        udelay(10);
-       if (drive->quirk_list == 2)
-               ctl = drive->ctl;       /* clear SRST and nIEN */
-       else
-               ctl = drive->ctl | 2;   /* clear SRST, leave nIEN */
-       hwif->OUTBSYNC(drive, ctl, io_ports->ctl_addr);
+       /* clear SRST, leave nIEN (unless device is on the quirk list) */
+       hwif->set_irq(hwif, drive->quirk_list == 2);
        /* more than enough time */
        udelay(10);
        hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
@@ -1185,7 +1286,7 @@ int ide_wait_not_busy(ide_hwif_t *hwif, unsigned long timeout)
                 * about locking issues (2.5 work ?).
                 */
                mdelay(1);
-               stat = hwif->INB(hwif->io_ports.status_addr);
+               stat = hwif->read_status(hwif);
                if ((stat & BUSY_STAT) == 0)
                        return 0;
                /*