ide: remove ->dma_{status,command} fields from ide_hwif_t
[safe/jmp/linux-2.6] / drivers / ide / ide-iops.c
index bd513f5..17cad6c 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * linux/drivers/ide/ide-iops.c        Version 0.37    Mar 05, 2003
- *
  *  Copyright (C) 2000-2002    Andre Hedrick <andre@linux-ide.org>
  *  Copyright (C) 2003         Red Hat <alan@redhat.com>
  *
@@ -39,57 +37,21 @@ static u8 ide_inb (unsigned long port)
        return (u8) inb(port);
 }
 
-static u16 ide_inw (unsigned long port)
-{
-       return (u16) inw(port);
-}
-
-static void ide_insw (unsigned long port, void *addr, u32 count)
-{
-       insw(port, addr, count);
-}
-
-static void ide_insl (unsigned long port, void *addr, u32 count)
-{
-       insl(port, addr, count);
-}
-
 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);
 }
 
-static void ide_outw (u16 val, unsigned long port)
-{
-       outw(val, port);
-}
-
-static void ide_outsw (unsigned long port, void *addr, u32 count)
-{
-       outsw(port, addr, count);
-}
-
-static void ide_outsl (unsigned long port, void *addr, u32 count)
-{
-       outsl(port, addr, count);
-}
-
 void default_hwif_iops (ide_hwif_t *hwif)
 {
        hwif->OUTB      = ide_outb;
        hwif->OUTBSYNC  = ide_outbsync;
-       hwif->OUTW      = ide_outw;
-       hwif->OUTSW     = ide_outsw;
-       hwif->OUTSL     = ide_outsl;
        hwif->INB       = ide_inb;
-       hwif->INW       = ide_inw;
-       hwif->INSW      = ide_insw;
-       hwif->INSL      = ide_insl;
 }
 
 /*
@@ -101,98 +63,164 @@ static u8 ide_mm_inb (unsigned long port)
        return (u8) readb((void __iomem *) port);
 }
 
-static u16 ide_mm_inw (unsigned long port)
-{
-       return (u16) readw((void __iomem *) port);
-}
-
-static void ide_mm_insw (unsigned long port, void *addr, u32 count)
-{
-       __ide_mm_insw((void __iomem *) port, addr, count);
-}
-
-static void ide_mm_insl (unsigned long port, void *addr, u32 count)
-{
-       __ide_mm_insl((void __iomem *) port, addr, count);
-}
-
 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);
 }
 
-static void ide_mm_outw (u16 value, unsigned long port)
-{
-       writew(value, (void __iomem *) port);
-}
-
-static void ide_mm_outsw (unsigned long port, void *addr, u32 count)
-{
-       __ide_mm_outsw((void __iomem *) port, addr, count);
-}
-
-static void ide_mm_outsl (unsigned long port, void *addr, u32 count)
-{
-       __ide_mm_outsl((void __iomem *) port, addr, count);
-}
-
 void default_hwif_mmiops (ide_hwif_t *hwif)
 {
        hwif->OUTB      = ide_mm_outb;
        /* Most systems will need to override OUTBSYNC, alas however
           this one is controller specific! */
        hwif->OUTBSYNC  = ide_mm_outbsync;
-       hwif->OUTW      = ide_mm_outw;
-       hwif->OUTSW     = ide_mm_outsw;
-       hwif->OUTSL     = ide_mm_outsl;
        hwif->INB       = ide_mm_inb;
-       hwif->INW       = ide_mm_inw;
-       hwif->INSW      = ide_mm_insw;
-       hwif->INSL      = ide_mm_insl;
 }
 
 EXPORT_SYMBOL(default_hwif_mmiops);
 
-u32 ide_read_24 (ide_drive_t *drive)
+void SELECT_DRIVE (ide_drive_t *drive)
 {
-       u8 hcyl = HWIF(drive)->INB(IDE_HCYL_REG);
-       u8 lcyl = HWIF(drive)->INB(IDE_LCYL_REG);
-       u8 sect = HWIF(drive)->INB(IDE_SECTOR_REG);
-       return (hcyl<<16)|(lcyl<<8)|sect;
+       ide_hwif_t *hwif = drive->hwif;
+       const struct ide_port_ops *port_ops = hwif->port_ops;
+
+       if (port_ops && port_ops->selectproc)
+               port_ops->selectproc(drive);
+
+       hwif->OUTB(drive->select.all, hwif->io_ports.device_addr);
 }
 
-void SELECT_DRIVE (ide_drive_t *drive)
+void SELECT_MASK(ide_drive_t *drive, int mask)
 {
-       if (HWIF(drive)->selectproc)
-               HWIF(drive)->selectproc(drive);
-       HWIF(drive)->OUTB(drive->select.all, IDE_SELECT_REG);
-}
+       const struct ide_port_ops *port_ops = drive->hwif->port_ops;
 
-EXPORT_SYMBOL(SELECT_DRIVE);
+       if (port_ops && port_ops->maskproc)
+               port_ops->maskproc(drive, mask);
+}
 
-void SELECT_INTERRUPT (ide_drive_t *drive)
+static u8 ide_read_sff_dma_status(ide_hwif_t *hwif)
 {
-       if (HWIF(drive)->intrproc)
-               HWIF(drive)->intrproc(drive);
+       if (hwif->host_flags & IDE_HFLAG_MMIO)
+               return readb((void __iomem *)(hwif->dma_base + ATA_DMA_STATUS));
        else
-               HWIF(drive)->OUTB(drive->ctl|2, IDE_CONTROL_REG);
+               return inb(hwif->dma_base + ATA_DMA_STATUS);
 }
 
-void SELECT_MASK (ide_drive_t *drive, int mask)
+static void ide_tf_load(ide_drive_t *drive, ide_task_t *task)
 {
-       if (HWIF(drive)->maskproc)
-               HWIF(drive)->maskproc(drive, mask);
+       ide_hwif_t *hwif = drive->hwif;
+       struct ide_io_ports *io_ports = &hwif->io_ports;
+       struct ide_taskfile *tf = &task->tf;
+       void (*tf_outb)(u8 addr, unsigned long port);
+       u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
+       u8 HIHI = (task->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
+
+       if (mmio)
+               tf_outb = ide_mm_outb;
+       else
+               tf_outb = ide_outb;
+
+       if (task->tf_flags & IDE_TFLAG_FLAGGED)
+               HIHI = 0xFF;
+
+       if (task->tf_flags & IDE_TFLAG_OUT_DATA) {
+               u16 data = (tf->hob_data << 8) | tf->data;
+
+               if (mmio)
+                       writew(data, (void __iomem *)io_ports->data_addr);
+               else
+                       outw(data, io_ports->data_addr);
+       }
+
+       if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
+               tf_outb(tf->hob_feature, io_ports->feature_addr);
+       if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
+               tf_outb(tf->hob_nsect, io_ports->nsect_addr);
+       if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
+               tf_outb(tf->hob_lbal, io_ports->lbal_addr);
+       if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
+               tf_outb(tf->hob_lbam, io_ports->lbam_addr);
+       if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
+               tf_outb(tf->hob_lbah, io_ports->lbah_addr);
+
+       if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
+               tf_outb(tf->feature, io_ports->feature_addr);
+       if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
+               tf_outb(tf->nsect, io_ports->nsect_addr);
+       if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
+               tf_outb(tf->lbal, io_ports->lbal_addr);
+       if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
+               tf_outb(tf->lbam, io_ports->lbam_addr);
+       if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
+               tf_outb(tf->lbah, io_ports->lbah_addr);
+
+       if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
+               tf_outb((tf->device & HIHI) | drive->select.all,
+                        io_ports->device_addr);
 }
 
-void QUIRK_LIST (ide_drive_t *drive)
+static void ide_tf_read(ide_drive_t *drive, ide_task_t *task)
 {
-       if (HWIF(drive)->quirkproc)
-               drive->quirk_list = HWIF(drive)->quirkproc(drive);
+       ide_hwif_t *hwif = drive->hwif;
+       struct ide_io_ports *io_ports = &hwif->io_ports;
+       struct ide_taskfile *tf = &task->tf;
+       void (*tf_outb)(u8 addr, unsigned long port);
+       u8 (*tf_inb)(unsigned long port);
+       u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
+
+       if (mmio) {
+               tf_outb = ide_mm_outb;
+               tf_inb  = ide_mm_inb;
+       } else {
+               tf_outb = ide_outb;
+               tf_inb  = ide_inb;
+       }
+
+       if (task->tf_flags & IDE_TFLAG_IN_DATA) {
+               u16 data;
+
+               if (mmio)
+                       data = readw((void __iomem *)io_ports->data_addr);
+               else
+                       data = inw(io_ports->data_addr);
+
+               tf->data = data & 0xff;
+               tf->hob_data = (data >> 8) & 0xff;
+       }
+
+       /* be sure we're looking at the low order bits */
+       tf_outb(ATA_DEVCTL_OBS & ~0x80, io_ports->ctl_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)
+               tf->lbal   = tf_inb(io_ports->lbal_addr);
+       if (task->tf_flags & IDE_TFLAG_IN_LBAM)
+               tf->lbam   = tf_inb(io_ports->lbam_addr);
+       if (task->tf_flags & IDE_TFLAG_IN_LBAH)
+               tf->lbah   = tf_inb(io_ports->lbah_addr);
+       if (task->tf_flags & IDE_TFLAG_IN_DEVICE)
+               tf->device = tf_inb(io_ports->device_addr);
+
+       if (task->tf_flags & IDE_TFLAG_LBA48) {
+               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);
+               if (task->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
+                       tf->hob_nsect   = tf_inb(io_ports->nsect_addr);
+               if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
+                       tf->hob_lbal    = tf_inb(io_ports->lbal_addr);
+               if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
+                       tf->hob_lbam    = tf_inb(io_ports->lbam_addr);
+               if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
+                       tf->hob_lbah    = tf_inb(io_ports->lbah_addr);
+       }
 }
 
 /*
@@ -202,110 +230,116 @@ void QUIRK_LIST (ide_drive_t *drive)
  * of the sector count register location, with interrupts disabled
  * to ensure that the reads all happen together.
  */
-static void ata_vlb_sync(ide_drive_t *drive, unsigned long port)
+static void ata_vlb_sync(unsigned long port)
 {
-       (void) HWIF(drive)->INB(port);
-       (void) HWIF(drive)->INB(port);
-       (void) HWIF(drive)->INB(port);
+       (void)inb(port);
+       (void)inb(port);
+       (void)inb(port);
 }
 
 /*
  * This is used for most PIO data transfers *from* the IDE interface
+ *
+ * These routines will round up any request for an odd number of bytes,
+ * so if an odd len is specified, be sure that there's at least one
+ * extra byte allocated for the buffer.
  */
-static void ata_input_data(ide_drive_t *drive, void *buffer, u32 wcount)
+static void ata_input_data(ide_drive_t *drive, struct request *rq,
+                          void *buf, unsigned int len)
 {
-       ide_hwif_t *hwif        = HWIF(drive);
-       u8 io_32bit             = drive->io_32bit;
+       ide_hwif_t *hwif = drive->hwif;
+       struct ide_io_ports *io_ports = &hwif->io_ports;
+       unsigned long data_addr = io_ports->data_addr;
+       u8 io_32bit = drive->io_32bit;
+       u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
+
+       len++;
 
        if (io_32bit) {
-               if (io_32bit & 2) {
-                       unsigned long flags;
+               unsigned long uninitialized_var(flags);
+
+               if ((io_32bit & 2) && !mmio) {
                        local_irq_save(flags);
-                       ata_vlb_sync(drive, IDE_NSECTOR_REG);
-                       hwif->INSL(IDE_DATA_REG, buffer, wcount);
+                       ata_vlb_sync(io_ports->nsect_addr);
+               }
+
+               if (mmio)
+                       __ide_mm_insl((void __iomem *)data_addr, buf, len / 4);
+               else
+                       insl(data_addr, buf, len / 4);
+
+               if ((io_32bit & 2) && !mmio)
                        local_irq_restore(flags);
-               } else
-                       hwif->INSL(IDE_DATA_REG, buffer, wcount);
+
+               if ((len & 3) >= 2) {
+                       if (mmio)
+                               __ide_mm_insw((void __iomem *)data_addr,
+                                               (u8 *)buf + (len & ~3), 1);
+                       else
+                               insw(data_addr, (u8 *)buf + (len & ~3), 1);
+               }
        } else {
-               hwif->INSW(IDE_DATA_REG, buffer, wcount<<1);
+               if (mmio)
+                       __ide_mm_insw((void __iomem *)data_addr, buf, len / 2);
+               else
+                       insw(data_addr, buf, len / 2);
        }
 }
 
 /*
  * This is used for most PIO data transfers *to* the IDE interface
  */
-static void ata_output_data(ide_drive_t *drive, void *buffer, u32 wcount)
+static void ata_output_data(ide_drive_t *drive, struct request *rq,
+                           void *buf, unsigned int len)
 {
-       ide_hwif_t *hwif        = HWIF(drive);
-       u8 io_32bit             = drive->io_32bit;
+       ide_hwif_t *hwif = drive->hwif;
+       struct ide_io_ports *io_ports = &hwif->io_ports;
+       unsigned long data_addr = io_ports->data_addr;
+       u8 io_32bit = drive->io_32bit;
+       u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
 
        if (io_32bit) {
-               if (io_32bit & 2) {
-                       unsigned long flags;
+               unsigned long uninitialized_var(flags);
+
+               if ((io_32bit & 2) && !mmio) {
                        local_irq_save(flags);
-                       ata_vlb_sync(drive, IDE_NSECTOR_REG);
-                       hwif->OUTSL(IDE_DATA_REG, buffer, wcount);
-                       local_irq_restore(flags);
-               } else
-                       hwif->OUTSL(IDE_DATA_REG, buffer, wcount);
-       } else {
-               hwif->OUTSW(IDE_DATA_REG, buffer, wcount<<1);
-       }
-}
+                       ata_vlb_sync(io_ports->nsect_addr);
+               }
 
-/*
- * The following routines are mainly used by the ATAPI drivers.
- *
- * These routines will round up any request for an odd number of bytes,
- * so if an odd bytecount is specified, be sure that there's at least one
- * extra byte allocated for the buffer.
- */
+               if (mmio)
+                       __ide_mm_outsl((void __iomem *)data_addr, buf, len / 4);
+               else
+                       outsl(data_addr, buf, len / 4);
 
-static void atapi_input_bytes(ide_drive_t *drive, void *buffer, u32 bytecount)
-{
-       ide_hwif_t *hwif = HWIF(drive);
+               if ((io_32bit & 2) && !mmio)
+                       local_irq_restore(flags);
 
-       ++bytecount;
-#if defined(CONFIG_ATARI) || defined(CONFIG_Q40)
-       if (MACH_IS_ATARI || MACH_IS_Q40) {
-               /* Atari has a byte-swapped IDE interface */
-               insw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
-               return;
+               if ((len & 3) >= 2) {
+                       if (mmio)
+                               __ide_mm_outsw((void __iomem *)data_addr,
+                                                (u8 *)buf + (len & ~3), 1);
+                       else
+                               outsw(data_addr, (u8 *)buf + (len & ~3), 1);
+               }
+       } else {
+               if (mmio)
+                       __ide_mm_outsw((void __iomem *)data_addr, buf, len / 2);
+               else
+                       outsw(data_addr, buf, len / 2);
        }
-#endif /* CONFIG_ATARI || CONFIG_Q40 */
-       hwif->ata_input_data(drive, buffer, bytecount / 4);
-       if ((bytecount & 0x03) >= 2)
-               hwif->INSW(IDE_DATA_REG, ((u8 *)buffer)+(bytecount & ~0x03), 1);
 }
 
-static void atapi_output_bytes(ide_drive_t *drive, void *buffer, u32 bytecount)
+void default_hwif_transport(ide_hwif_t *hwif)
 {
-       ide_hwif_t *hwif = HWIF(drive);
+       hwif->read_sff_dma_status = ide_read_sff_dma_status;
 
-       ++bytecount;
-#if defined(CONFIG_ATARI) || defined(CONFIG_Q40)
-       if (MACH_IS_ATARI || MACH_IS_Q40) {
-               /* Atari has a byte-swapped IDE interface */
-               outsw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
-               return;
-       }
-#endif /* CONFIG_ATARI || CONFIG_Q40 */
-       hwif->ata_output_data(drive, buffer, bytecount / 4);
-       if ((bytecount & 0x03) >= 2)
-               hwif->OUTSW(IDE_DATA_REG, ((u8*)buffer)+(bytecount & ~0x03), 1);
-}
+       hwif->tf_load     = ide_tf_load;
+       hwif->tf_read     = ide_tf_read;
 
-void default_hwif_transport(ide_hwif_t *hwif)
-{
-       hwif->ata_input_data            = ata_input_data;
-       hwif->ata_output_data           = ata_output_data;
-       hwif->atapi_input_bytes         = atapi_input_bytes;
-       hwif->atapi_output_bytes        = atapi_output_bytes;
+       hwif->input_data  = ata_input_data;
+       hwif->output_data = ata_output_data;
 }
 
-/*
- * Beginning of Taskfile OPCODE Library and feature sets.
- */
 void ide_fix_driveid (struct hd_driveid *id)
 {
 #ifndef __LITTLE_ENDIAN
@@ -403,8 +437,12 @@ void ide_fix_driveid (struct hd_driveid *id)
 #endif
 }
 
-/* FIXME: exported for use by the USB storage (isd200.c) code only */
-EXPORT_SYMBOL(ide_fix_driveid);
+/*
+ * ide_fixstring() cleans up and (optionally) byte-swaps a text string,
+ * removing leading/trailing blanks and compressing internal blanks.
+ * It is primarily used to tidy up the model name/number fields as
+ * returned by the WIN_[P]IDENTIFY commands.
+ */
 
 void ide_fixstring (u8 *s, const int bytecount, const int byteswap)
 {
@@ -441,26 +479,24 @@ int drive_is_ready (ide_drive_t *drive)
        u8 stat                 = 0;
 
        if (drive->waiting_for_dma)
-               return hwif->ide_dma_test_irq(drive);
+               return hwif->dma_ops->dma_test_irq(drive);
 
 #if 0
        /* need to guarantee 400ns since last command was issued */
        udelay(1);
 #endif
 
-#ifdef CONFIG_IDEPCI_SHARE_IRQ
        /*
         * We do a passive status test under shared PCI interrupts on
         * cards that truly share the ATA side interrupt, but may also share
         * an interrupt with another pci card/device.  We make no assumptions
         * about possible isa-pnp and pci-pnp issues yet.
         */
-       if (IDE_CONTROL_REG)
-               stat = hwif->INB(IDE_ALTSTATUS_REG);
+       if (hwif->io_ports.ctl_addr)
+               stat = ide_read_altstatus(drive);
        else
-#endif /* CONFIG_IDEPCI_SHARE_IRQ */
                /* Note: this may clear a pending IRQ!! */
-               stat = hwif->INB(IDE_STATUS_REG);
+               stat = ide_read_status(drive);
 
        if (stat & BUSY_STAT)
                /* drive busy:  definitely not interrupting */
@@ -473,76 +509,42 @@ int drive_is_ready (ide_drive_t *drive)
 EXPORT_SYMBOL(drive_is_ready);
 
 /*
- * Global for All, and taken from ide-pmac.c. Can be called
- * with spinlock held & IRQs disabled, so don't schedule !
- */
-int wait_for_ready (ide_drive_t *drive, int timeout)
-{
-       ide_hwif_t *hwif        = HWIF(drive);
-       u8 stat                 = 0;
-
-       while(--timeout) {
-               stat = hwif->INB(IDE_STATUS_REG);
-               if (!(stat & BUSY_STAT)) {
-                       if (drive->ready_stat == 0)
-                               break;
-                       else if ((stat & drive->ready_stat)||(stat & ERR_STAT))
-                               break;
-               }
-               mdelay(1);
-       }
-       if ((stat & ERR_STAT) || timeout <= 0) {
-               if (stat & ERR_STAT) {
-                       printk(KERN_ERR "%s: wait_for_ready, "
-                               "error status: %x\n", drive->name, stat);
-               }
-               return 1;
-       }
-       return 0;
-}
-
-/*
  * This routine busy-waits for the drive status to be not "busy".
  * It then checks the status for all of the "good" bits and none
  * of the "bad" bits, and if all is okay it returns 0.  All other
- * cases return 1 after invoking ide_error() -- caller should just return.
+ * cases return error -- caller may then invoke ide_error().
  *
  * This routine should get fixed to not hog the cpu during extra long waits..
  * That could be done by busy-waiting for the first jiffy or two, and then
  * setting a timer to wake up at half second intervals thereafter,
  * until timeout is achieved, before timing out.
  */
-int ide_wait_stat (ide_startstop_t *startstop, ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout)
+static int __ide_wait_stat(ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout, u8 *rstat)
 {
-       ide_hwif_t *hwif = HWIF(drive);
-       u8 stat;
-       int i;
        unsigned long flags;
-       /* bail early if we've exceeded max_failures */
-       if (drive->max_failures && (drive->failures > drive->max_failures)) {
-               *startstop = ide_stopped;
-               return 1;
-       }
+       int i;
+       u8 stat;
 
        udelay(1);      /* spec allows drive 400ns to assert "BUSY" */
-       if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
+       stat = ide_read_status(drive);
+
+       if (stat & BUSY_STAT) {
                local_irq_set(flags);
                timeout += jiffies;
-               while ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
+               while ((stat = ide_read_status(drive)) & 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 = hwif->INB(IDE_STATUS_REG);
+                               stat = ide_read_status(drive);
                                if (!(stat & BUSY_STAT))
                                        break;
 
                                local_irq_restore(flags);
-                               *startstop = ide_error(drive, "status timeout", stat);
-                               return 1;
+                               *rstat = stat;
+                               return -EBUSY;
                        }
                }
                local_irq_restore(flags);
@@ -556,155 +558,160 @@ int ide_wait_stat (ide_startstop_t *startstop, ide_drive_t *drive, u8 good, u8 b
         */
        for (i = 0; i < 10; i++) {
                udelay(1);
-               if (OK_STAT((stat = hwif->INB(IDE_STATUS_REG)), good, bad))
+               stat = ide_read_status(drive);
+
+               if (OK_STAT(stat, good, bad)) {
+                       *rstat = stat;
                        return 0;
+               }
        }
-       *startstop = ide_error(drive, "status error", stat);
-       return 1;
+       *rstat = stat;
+       return -EFAULT;
 }
 
-EXPORT_SYMBOL(ide_wait_stat);
-
 /*
- *  All hosts that use the 80c ribbon must use!
- *  The name is derived from upper byte of word 93 and the 80c ribbon.
+ * In case of error returns error value after doing "*startstop = ide_error()".
+ * The caller should return the updated value of "startstop" in this case,
+ * "startstop" is unchanged when the function returns 0.
  */
-u8 eighty_ninty_three (ide_drive_t *drive)
+int ide_wait_stat(ide_startstop_t *startstop, ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout)
 {
-       if(HWIF(drive)->udma_four == 0)
-               return 0;
+       int err;
+       u8 stat;
 
-       /* Check for SATA but only if we are ATA5 or higher */
-       if (drive->id->hw_config == 0 && (drive->id->major_rev_num & 0x7FE0))
+       /* bail early if we've exceeded max_failures */
+       if (drive->max_failures && (drive->failures > drive->max_failures)) {
+               *startstop = ide_stopped;
                return 1;
-       if (!(drive->id->hw_config & 0x6000))
-               return 0;
-#ifndef CONFIG_IDEDMA_IVB
-       if(!(drive->id->hw_config & 0x4000))
-               return 0;
-#endif /* CONFIG_IDEDMA_IVB */
-       if (!(drive->id->hw_config & 0x2000))
-               return 0;
-       return 1;
+       }
+
+       err = __ide_wait_stat(drive, good, bad, timeout, &stat);
+
+       if (err) {
+               char *s = (err == -EBUSY) ? "status timeout" : "status error";
+               *startstop = ide_error(drive, s, stat);
+       }
+
+       return err;
 }
 
-EXPORT_SYMBOL(eighty_ninty_three);
+EXPORT_SYMBOL(ide_wait_stat);
+
+/**
+ *     ide_in_drive_list       -       look for drive in black/white list
+ *     @id: drive identifier
+ *     @drive_table: list to inspect
+ *
+ *     Look for a drive in the blacklist and the whitelist tables
+ *     Returns 1 if the drive is found in the table.
+ */
 
-int ide_ata66_check (ide_drive_t *drive, ide_task_t *args)
+int ide_in_drive_list(struct hd_driveid *id, const struct drive_list_entry *drive_table)
 {
-       if ((args->tfRegister[IDE_COMMAND_OFFSET] == WIN_SETFEATURES) &&
-           (args->tfRegister[IDE_SECTOR_OFFSET] > XFER_UDMA_2) &&
-           (args->tfRegister[IDE_FEATURE_OFFSET] == SETFEATURES_XFER)) {
-#ifndef CONFIG_IDEDMA_IVB
-               if ((drive->id->hw_config & 0x6000) == 0) {
-#else /* !CONFIG_IDEDMA_IVB */
-               if (((drive->id->hw_config & 0x2000) == 0) ||
-                   ((drive->id->hw_config & 0x4000) == 0)) {
-#endif /* CONFIG_IDEDMA_IVB */
-                       printk("%s: Speed warnings UDMA 3/4/5 is not "
-                               "functional.\n", drive->name);
-                       return 1;
-               }
-               if (!HWIF(drive)->udma_four) {
-                       printk("%s: Speed warnings UDMA 3/4/5 is not "
-                               "functional.\n",
-                               HWIF(drive)->name);
+       for ( ; drive_table->id_model; drive_table++)
+               if ((!strcmp(drive_table->id_model, id->model)) &&
+                   (!drive_table->id_firmware ||
+                    strstr(id->fw_rev, drive_table->id_firmware)))
                        return 1;
-               }
-       }
        return 0;
 }
 
+EXPORT_SYMBOL_GPL(ide_in_drive_list);
+
 /*
- * Backside of HDIO_DRIVE_CMD call of SETFEATURES_XFER.
- * 1 : Safe to update drive->id DMA registers.
- * 0 : OOPs not allowed.
+ * Early UDMA66 devices don't set bit14 to 1, only bit13 is valid.
+ * We list them here and depend on the device side cable detection for them.
+ *
+ * Some optical devices with the buggy firmwares have the same problem.
  */
-int set_transfer (ide_drive_t *drive, ide_task_t *args)
+static const struct drive_list_entry ivb_list[] = {
+       { "QUANTUM FIREBALLlct10 05"    , "A03.0900"    },
+       { "TSSTcorp CDDVDW SH-S202J"    , "SB00"        },
+       { "TSSTcorp CDDVDW SH-S202J"    , "SB01"        },
+       { "TSSTcorp CDDVDW SH-S202N"    , "SB00"        },
+       { "TSSTcorp CDDVDW SH-S202N"    , "SB01"        },
+       { "TSSTcorp CDDVDW SH-S202H"    , "SB00"        },
+       { "TSSTcorp CDDVDW SH-S202H"    , "SB01"        },
+       { NULL                          , NULL          }
+};
+
+/*
+ *  All hosts that use the 80c ribbon must use!
+ *  The name is derived from upper byte of word 93 and the 80c ribbon.
+ */
+u8 eighty_ninty_three (ide_drive_t *drive)
 {
-       if ((args->tfRegister[IDE_COMMAND_OFFSET] == WIN_SETFEATURES) &&
-           (args->tfRegister[IDE_SECTOR_OFFSET] >= XFER_SW_DMA_0) &&
-           (args->tfRegister[IDE_FEATURE_OFFSET] == SETFEATURES_XFER) &&
-           (drive->id->dma_ultra ||
-            drive->id->dma_mword ||
-            drive->id->dma_1word))
+       ide_hwif_t *hwif = drive->hwif;
+       struct hd_driveid *id = drive->id;
+       int ivb = ide_in_drive_list(id, ivb_list);
+
+       if (hwif->cbl == ATA_CBL_PATA40_SHORT)
                return 1;
 
-       return 0;
-}
+       if (ivb)
+               printk(KERN_DEBUG "%s: skipping word 93 validity check\n",
+                                 drive->name);
 
-#ifdef CONFIG_BLK_DEV_IDEDMA
-static u8 ide_auto_reduce_xfer (ide_drive_t *drive)
-{
-       if (!drive->crc_count)
-               return drive->current_speed;
-       drive->crc_count = 0;
-
-       switch(drive->current_speed) {
-               case XFER_UDMA_7:       return XFER_UDMA_6;
-               case XFER_UDMA_6:       return XFER_UDMA_5;
-               case XFER_UDMA_5:       return XFER_UDMA_4;
-               case XFER_UDMA_4:       return XFER_UDMA_3;
-               case XFER_UDMA_3:       return XFER_UDMA_2;
-               case XFER_UDMA_2:       return XFER_UDMA_1;
-               case XFER_UDMA_1:       return XFER_UDMA_0;
-                       /*
-                        * OOPS we do not goto non Ultra DMA modes
-                        * without iCRC's available we force
-                        * the system to PIO and make the user
-                        * invoke the ATA-1 ATA-2 DMA modes.
-                        */
-               case XFER_UDMA_0:
-               default:                return XFER_PIO_4;
-       }
+       if (ide_dev_is_sata(id) && !ivb)
+               return 1;
+
+       if (hwif->cbl != ATA_CBL_PATA80 && !ivb)
+               goto no_80w;
+
+       /*
+        * FIXME:
+        * - change master/slave IDENTIFY order
+        * - force bit13 (80c cable present) check also for !ivb devices
+        *   (unless the slave device is pre-ATA3)
+        */
+       if ((id->hw_config & 0x4000) || (ivb && (id->hw_config & 0x2000)))
+               return 1;
+
+no_80w:
+       if (drive->udma33_warned == 1)
+               return 0;
+
+       printk(KERN_WARNING "%s: %s side 80-wire cable detection failed, "
+                           "limiting max speed to UDMA33\n",
+                           drive->name,
+                           hwif->cbl == ATA_CBL_PATA80 ? "drive" : "host");
+
+       drive->udma33_warned = 1;
+
+       return 0;
 }
-#endif /* CONFIG_BLK_DEV_IDEDMA */
 
-/*
- * Update the 
- */
-int ide_driveid_update (ide_drive_t *drive)
+int ide_driveid_update(ide_drive_t *drive)
 {
-       ide_hwif_t *hwif        = HWIF(drive);
+       ide_hwif_t *hwif = drive->hwif;
        struct hd_driveid *id;
-#if 0
-       id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
-       if (!id)
-               return 0;
-
-       taskfile_lib_get_identify(drive, (char *)&id);
+       unsigned long timeout, flags;
+       u8 stat;
 
-       ide_fix_driveid(id);
-       if (id) {
-               drive->id->dma_ultra = id->dma_ultra;
-               drive->id->dma_mword = id->dma_mword;
-               drive->id->dma_1word = id->dma_1word;
-               /* anything more ? */
-               kfree(id);
-       }
-       return 1;
-#else
        /*
         * Re-read drive->id for possible DMA mode
         * change (copied from ide-probe.c)
         */
-       unsigned long timeout, flags;
 
        SELECT_MASK(drive, 1);
-       if (IDE_CONTROL_REG)
-               hwif->OUTB(drive->ctl,IDE_CONTROL_REG);
+       ide_set_irq(drive, 0);
        msleep(50);
-       hwif->OUTB(WIN_IDENTIFY, IDE_COMMAND_REG);
+       hwif->OUTBSYNC(hwif, WIN_IDENTIFY, hwif->io_ports.command_addr);
        timeout = jiffies + WAIT_WORSTCASE;
        do {
                if (time_after(jiffies, timeout)) {
                        SELECT_MASK(drive, 0);
                        return 0;       /* drive timed-out */
                }
+
                msleep(50);     /* give drive a breather */
-       } while (hwif->INB(IDE_ALTSTATUS_REG) & BUSY_STAT);
+               stat = ide_read_altstatus(drive);
+       } while (stat & BUSY_STAT);
+
        msleep(50);     /* wait for IRQ and DRQ_STAT */
-       if (!OK_STAT(hwif->INB(IDE_STATUS_REG),DRQ_STAT,BAD_R_STAT)) {
+       stat = ide_read_status(drive);
+
+       if (!OK_STAT(stat, DRQ_STAT, BAD_R_STAT)) {
                SELECT_MASK(drive, 0);
                printk("%s: CHECK for good STATUS\n", drive->name);
                return 0;
@@ -716,8 +723,8 @@ int ide_driveid_update (ide_drive_t *drive)
                local_irq_restore(flags);
                return 0;
        }
-       ata_input_data(drive, id, SECTOR_WORDS);
-       (void) hwif->INB(IDE_STATUS_REG);       /* clear drive IRQ */
+       hwif->input_data(drive, NULL, id, SECTOR_SIZE);
+       (void)ide_read_status(drive);   /* clear drive IRQ */
        local_irq_enable();
        local_irq_restore(flags);
        ide_fix_driveid(id);
@@ -727,37 +734,30 @@ int ide_driveid_update (ide_drive_t *drive)
                drive->id->dma_1word = id->dma_1word;
                /* anything more ? */
                kfree(id);
+
+               if (drive->using_dma && ide_id_dma_bug(drive))
+                       ide_dma_off(drive);
        }
 
        return 1;
-#endif
 }
 
-/*
- * Similar to ide_wait_stat(), except it never calls ide_error internally.
- * This is a kludge to handle the new ide_config_drive_speed() function,
- * and should not otherwise be used anywhere.  Eventually, the tuneproc's
- * should be updated to return ide_startstop_t, in which case we can get
- * rid of this abomination again.  :)   -ml
- *
- * It is gone..........
- *
- * const char *msg == consider adding for verbose errors.
- */
-int ide_config_drive_speed (ide_drive_t *drive, u8 speed)
+int ide_config_drive_speed(ide_drive_t *drive, u8 speed)
 {
-       ide_hwif_t *hwif        = HWIF(drive);
-       int     i, error        = 1;
+       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);
-
 #ifdef CONFIG_BLK_DEV_IDEDMA
-       if (hwif->ide_dma_check)         /* check if host supports DMA */
-               hwif->dma_host_off(drive);
+       if (hwif->dma_ops)      /* check if host supports DMA */
+               hwif->dma_ops->dma_host_set(drive, 0);
 #endif
 
+       /* Skip setting PIO flow-control modes on pre-EIDE drives */
+       if ((speed & 0xf8) == XFER_PIO_0 && !(drive->id->capability & 0x08))
+               goto skip;
+
        /*
         * Don't use ide_wait_cmd here - it will
         * attempt to set_geometry and recalibrate,
@@ -780,42 +780,16 @@ int ide_config_drive_speed (ide_drive_t *drive, u8 speed)
        SELECT_DRIVE(drive);
        SELECT_MASK(drive, 0);
        udelay(1);
-       if (IDE_CONTROL_REG)
-               hwif->OUTB(drive->ctl | 2, IDE_CONTROL_REG);
-       hwif->OUTB(speed, IDE_NSECTOR_REG);
-       hwif->OUTB(SETFEATURES_XFER, IDE_FEATURE_REG);
-       hwif->OUTB(WIN_SETFEATURES, IDE_COMMAND_REG);
-       if ((IDE_CONTROL_REG) && (drive->quirk_list == 2))
-               hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
-       udelay(1);
-       /*
-        * Wait for drive to become non-BUSY
-        */
-       if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
-               unsigned long flags, timeout;
-               local_irq_set(flags);
-               timeout = jiffies + WAIT_CMD;
-               while ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) {
-                       if (time_after(jiffies, timeout))
-                               break;
-               }
-               local_irq_restore(flags);
-       }
+       ide_set_irq(drive, 0);
+       hwif->OUTB(speed, io_ports->nsect_addr);
+       hwif->OUTB(SETFEATURES_XFER, io_ports->feature_addr);
+       hwif->OUTBSYNC(hwif, WIN_SETFEATURES, io_ports->command_addr);
+       if (drive->quirk_list == 2)
+               ide_set_irq(drive, 1);
 
-       /*
-        * Allow status to settle, then read it again.
-        * A few rare drives vastly violate the 400ns spec here,
-        * so we'll wait up to 10usec for a "good" status
-        * rather than expensively fail things immediately.
-        * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
-        */
-       for (i = 0; i < 10; i++) {
-               udelay(1);
-               if (OK_STAT((stat = hwif->INB(IDE_STATUS_REG)), DRIVE_READY, BUSY_STAT|DRQ_STAT|ERR_STAT)) {
-                       error = 0;
-                       break;
-               }
-       }
+       error = __ide_wait_stat(drive, drive->ready_stat,
+                               BUSY_STAT|DRQ_STAT|ERR_STAT,
+                               WAIT_CMD, &stat);
 
        SELECT_MASK(drive, 0);
 
@@ -830,11 +804,13 @@ int ide_config_drive_speed (ide_drive_t *drive, u8 speed)
        drive->id->dma_mword &= ~0x0F00;
        drive->id->dma_1word &= ~0x0F00;
 
+ skip:
 #ifdef CONFIG_BLK_DEV_IDEDMA
-       if (speed >= XFER_SW_DMA_0)
-               hwif->dma_host_on(drive);
-       else if (hwif->ide_dma_check)   /* check if host supports DMA */
-               hwif->dma_off_quietly(drive);
+       if ((speed >= XFER_SW_DMA_0 || (hwif->host_flags & IDE_HFLAG_VDMA)) &&
+           drive->using_dma)
+               hwif->dma_ops->dma_host_set(drive, 1);
+       else if (hwif->dma_ops) /* check if host supports DMA */
+               ide_dma_off_quietly(drive);
 #endif
 
        switch(speed) {
@@ -860,9 +836,6 @@ int ide_config_drive_speed (ide_drive_t *drive, u8 speed)
        return error;
 }
 
-EXPORT_SYMBOL(ide_config_drive_speed);
-
-
 /*
  * This should get invoked any time we exit the driver to
  * wait for an interrupt response from a drive.  handler() points
@@ -877,14 +850,11 @@ static void __ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
 {
        ide_hwgroup_t *hwgroup = HWGROUP(drive);
 
-       if (hwgroup->handler != NULL) {
-               printk(KERN_CRIT "%s: ide_set_handler: handler not null; "
-                       "old=%p, new=%p\n",
-                       drive->name, hwgroup->handler, handler);
-       }
+       BUG_ON(hwgroup->handler);
        hwgroup->handler        = handler;
        hwgroup->expiry         = expiry;
        hwgroup->timer.expires  = jiffies + timeout;
+       hwgroup->req_gen_timer  = hwgroup->req_gen;
        add_timer(&hwgroup->timer);
 }
 
@@ -912,33 +882,46 @@ EXPORT_SYMBOL(ide_set_handler);
  *     handler and IRQ setup do not race. All IDE command kick off
  *     should go via this function or do equivalent locking.
  */
-void ide_execute_command(ide_drive_t *drive, task_ioreg_t cmd, ide_handler_t *handler, unsigned timeout, ide_expiry_t *expiry)
+
+void ide_execute_command(ide_drive_t *drive, u8 cmd, ide_handler_t *handler,
+                        unsigned timeout, ide_expiry_t *expiry)
 {
        unsigned long flags;
-       ide_hwgroup_t *hwgroup = HWGROUP(drive);
        ide_hwif_t *hwif = HWIF(drive);
-       
+
        spin_lock_irqsave(&ide_lock, flags);
-       
-       BUG_ON(hwgroup->handler);
-       hwgroup->handler        = handler;
-       hwgroup->expiry         = expiry;
-       hwgroup->timer.expires  = jiffies + timeout;
-       add_timer(&hwgroup->timer);
-       hwif->OUTBSYNC(drive, cmd, IDE_COMMAND_REG);
-       /* Drive takes 400nS to respond, we must avoid the IRQ being
-          serviced before that. 
-          
-          FIXME: we could skip this delay with care on non shared
-          devices 
-       */
+       __ide_set_handler(drive, handler, timeout, expiry);
+       hwif->OUTBSYNC(hwif, cmd, hwif->io_ports.command_addr);
+       /*
+        * Drive takes 400nS to respond, we must avoid the IRQ being
+        * serviced before that.
+        *
+        * FIXME: we could skip this delay with care on non shared devices
+        */
        ndelay(400);
        spin_unlock_irqrestore(&ide_lock, flags);
 }
-
 EXPORT_SYMBOL(ide_execute_command);
 
+void ide_execute_pkt_cmd(ide_drive_t *drive)
+{
+       ide_hwif_t *hwif = drive->hwif;
+       unsigned long flags;
+
+       spin_lock_irqsave(&ide_lock, flags);
+       hwif->OUTBSYNC(hwif, WIN_PACKETCMD, hwif->io_ports.command_addr);
+       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);
@@ -952,17 +935,16 @@ 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        = HWIF(drive);
        u8 stat;
 
        SELECT_DRIVE(drive);
        udelay (10);
+       stat = ide_read_status(drive);
 
-       if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG), 0, BUSY_STAT)) {
+       if (OK_STAT(stat, 0, BUSY_STAT))
                printk("%s: ATAPI reset complete\n", drive->name);
-       else {
+       else {
                if (time_before(jiffies, hwgroup->poll_timeout)) {
-                       BUG_ON(HWGROUP(drive)->handler != NULL);
                        ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
                        /* continue polling */
                        return ide_started;
@@ -976,7 +958,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;
 }
 
@@ -990,28 +972,35 @@ static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
 {
        ide_hwgroup_t *hwgroup  = HWGROUP(drive);
        ide_hwif_t *hwif        = HWIF(drive);
+       const struct ide_port_ops *port_ops = hwif->port_ops;
        u8 tmp;
+       int err = 0;
 
-       if (hwif->reset_poll != NULL) {
-               if (hwif->reset_poll(drive)) {
+       if (port_ops && port_ops->reset_poll) {
+               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;
                }
        }
 
-       if (!OK_STAT(tmp = hwif->INB(IDE_STATUS_REG), 0, BUSY_STAT)) {
+       tmp = ide_read_status(drive);
+
+       if (!OK_STAT(tmp, 0, BUSY_STAT)) {
                if (time_before(jiffies, hwgroup->poll_timeout)) {
-                       BUG_ON(HWGROUP(drive)->handler != NULL);
                        ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
                        /* continue polling */
                        return ide_started;
                }
                printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
                drive->failures++;
+               err = -EIO;
        } else  {
                printk("%s: reset: ", hwif->name);
-               if ((tmp = hwif->INB(IDE_ERROR_REG)) == 1) {
+               tmp = ide_read_error(drive);
+
+               if (tmp == 1) {
                        printk("success\n");
                        drive->failures = 0;
                } else {
@@ -1033,26 +1022,15 @@ 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;
 }
 
-static void check_dma_crc(ide_drive_t *drive)
-{
-#ifdef CONFIG_BLK_DEV_IDEDMA
-       if (drive->crc_count) {
-               drive->hwif->dma_off_quietly(drive);
-               ide_set_xfer_rate(drive, ide_auto_reduce_xfer(drive));
-               if (drive->current_speed >= XFER_SW_DMA_0)
-                       (void) HWIF(drive)->ide_dma_on(drive);
-       } else
-               ide_dma_off(drive);
-#endif
-}
-
 static void ide_disk_pre_reset(ide_drive_t *drive)
 {
        int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
@@ -1060,8 +1038,7 @@ static void ide_disk_pre_reset(ide_drive_t *drive)
        drive->special.all = 0;
        drive->special.b.set_geometry = legacy;
        drive->special.b.recalibrate  = legacy;
-       if (OK_TO_RESET_CONTROLLER)
-               drive->mult_count = 0;
+       drive->mult_count = 0;
        if (!drive->keep_settings && !drive->using_dma)
                drive->mult_req = 0;
        if (drive->mult_req != drive->mult_count)
@@ -1070,26 +1047,34 @@ static void ide_disk_pre_reset(ide_drive_t *drive)
 
 static void pre_reset(ide_drive_t *drive)
 {
+       const struct ide_port_ops *port_ops = drive->hwif->port_ops;
+
        if (drive->media == ide_disk)
                ide_disk_pre_reset(drive);
        else
                drive->post_reset = 1;
 
+       if (drive->using_dma) {
+               if (drive->crc_count)
+                       ide_check_dma_crc(drive);
+               else
+                       ide_dma_off(drive);
+       }
+
        if (!drive->keep_settings) {
-               if (drive->using_dma) {
-                       check_dma_crc(drive);
-               } else {
+               if (!drive->using_dma) {
                        drive->unmask = 0;
                        drive->io_32bit = 0;
                }
                return;
        }
-       if (drive->using_dma)
-               check_dma_crc(drive);
 
-       if (HWIF(drive)->pre_reset != NULL)
-               HWIF(drive)->pre_reset(drive);
+       if (port_ops && port_ops->pre_reset)
+               port_ops->pre_reset(drive);
 
+       if (drive->current_speed != 0xff)
+               drive->desired_speed = drive->current_speed;
+       drive->current_speed = 0xff;
 }
 
 /*
@@ -1113,21 +1098,25 @@ static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
        unsigned long flags;
        ide_hwif_t *hwif;
        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);
        hwgroup = HWGROUP(drive);
 
+       io_ports = &hwif->io_ports;
+
        /* We must not reset with running handlers */
        BUG_ON(hwgroup->handler != NULL);
 
        /* 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, IDE_COMMAND_REG);
+               hwif->OUTBSYNC(hwif, WIN_SRST, io_ports->command_addr);
                ndelay(400);
                hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
                hwgroup->polling = 1;
@@ -1143,13 +1132,12 @@ static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
        for (unit = 0; unit < MAX_DRIVES; ++unit)
                pre_reset(&hwif->drives[unit]);
 
-#if OK_TO_RESET_CONTROLLER
-       if (!IDE_CONTROL_REG) {
+       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.
@@ -1159,16 +1147,14 @@ static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
         * recover from reset very quickly, saving us the first 50ms wait time.
         */
        /* set SRST and nIEN */
-       hwif->OUTBSYNC(drive, drive->ctl|6,IDE_CONTROL_REG);
+       hwif->OUTBSYNC(hwif, ATA_DEVCTL_OBS | 6, io_ports->ctl_addr);
        /* more than enough time */
        udelay(10);
-       if (drive->quirk_list == 2) {
-               /* clear SRST and nIEN */
-               hwif->OUTBSYNC(drive, drive->ctl, IDE_CONTROL_REG);
-       } else {
-               /* clear SRST, leave nIEN */
-               hwif->OUTBSYNC(drive, drive->ctl|2, IDE_CONTROL_REG);
-       }
+       if (drive->quirk_list == 2)
+               ctl = ATA_DEVCTL_OBS;           /* clear SRST and nIEN */
+       else
+               ctl = ATA_DEVCTL_OBS | 2;       /* clear SRST, leave nIEN */
+       hwif->OUTBSYNC(hwif, ctl, io_ports->ctl_addr);
        /* more than enough time */
        udelay(10);
        hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
@@ -1180,11 +1166,9 @@ static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
         * state when the disks are reset this way. At least, the Winbond
         * 553 documentation says that
         */
-       if (hwif->resetproc != NULL) {
-               hwif->resetproc(drive);
-       }
-       
-#endif /* OK_TO_RESET_CONTROLLER */
+       port_ops = hwif->port_ops;
+       if (port_ops && port_ops->resetproc)
+               port_ops->resetproc(drive);
 
        spin_unlock_irqrestore(&ide_lock, flags);
        return ide_started;
@@ -1203,7 +1187,7 @@ EXPORT_SYMBOL(ide_do_reset);
 
 /*
  * ide_wait_not_busy() waits for the currently selected device on the hwif
- * to report a non-busy status, see comments in probe_hwif().
+ * to report a non-busy status, see comments in ide_probe_port().
  */
 int ide_wait_not_busy(ide_hwif_t *hwif, unsigned long timeout)
 {
@@ -1215,7 +1199,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[IDE_STATUS_OFFSET]);
+               stat = hwif->INB(hwif->io_ports.status_addr);
                if ((stat & BUSY_STAT) == 0)
                        return 0;
                /*