Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs-2.6
[safe/jmp/linux-2.6] / drivers / ata / libata-sff.c
index c62ed13..efa4a18 100644 (file)
  */
 
 #include <linux/kernel.h>
+#include <linux/gfp.h>
 #include <linux/pci.h>
 #include <linux/libata.h>
 #include <linux/highmem.h>
 
 #include "libata.h"
 
+static struct workqueue_struct *ata_sff_wq;
+
 const struct ata_port_operations ata_sff_port_ops = {
        .inherits               = &ata_base_port_ops,
 
-       .qc_prep                = ata_sff_qc_prep,
+       .qc_prep                = ata_noop_qc_prep,
        .qc_issue               = ata_sff_qc_issue,
        .qc_fill_rtf            = ata_sff_qc_fill_rtf,
 
@@ -52,9 +55,7 @@ const struct ata_port_operations ata_sff_port_ops = {
        .softreset              = ata_sff_softreset,
        .hardreset              = sata_sff_hardreset,
        .postreset              = ata_sff_postreset,
-       .drain_fifo             = ata_sff_drain_fifo,
        .error_handler          = ata_sff_error_handler,
-       .post_internal_cmd      = ata_sff_post_internal_cmd,
 
        .sff_dev_select         = ata_sff_dev_select,
        .sff_check_status       = ata_sff_check_status,
@@ -62,178 +63,12 @@ const struct ata_port_operations ata_sff_port_ops = {
        .sff_tf_read            = ata_sff_tf_read,
        .sff_exec_command       = ata_sff_exec_command,
        .sff_data_xfer          = ata_sff_data_xfer,
-       .sff_irq_on             = ata_sff_irq_on,
-       .sff_irq_clear          = ata_sff_irq_clear,
+       .sff_drain_fifo         = ata_sff_drain_fifo,
 
        .lost_interrupt         = ata_sff_lost_interrupt,
-
-       .port_start             = ata_sff_port_start,
 };
 EXPORT_SYMBOL_GPL(ata_sff_port_ops);
 
-const struct ata_port_operations ata_bmdma_port_ops = {
-       .inherits               = &ata_sff_port_ops,
-
-       .mode_filter            = ata_bmdma_mode_filter,
-
-       .bmdma_setup            = ata_bmdma_setup,
-       .bmdma_start            = ata_bmdma_start,
-       .bmdma_stop             = ata_bmdma_stop,
-       .bmdma_status           = ata_bmdma_status,
-};
-EXPORT_SYMBOL_GPL(ata_bmdma_port_ops);
-
-const struct ata_port_operations ata_bmdma32_port_ops = {
-       .inherits               = &ata_bmdma_port_ops,
-
-       .sff_data_xfer          = ata_sff_data_xfer32,
-       .port_start             = ata_sff_port_start32,
-};
-EXPORT_SYMBOL_GPL(ata_bmdma32_port_ops);
-
-/**
- *     ata_fill_sg - Fill PCI IDE PRD table
- *     @qc: Metadata associated with taskfile to be transferred
- *
- *     Fill PCI IDE PRD (scatter-gather) table with segments
- *     associated with the current disk command.
- *
- *     LOCKING:
- *     spin_lock_irqsave(host lock)
- *
- */
-static void ata_fill_sg(struct ata_queued_cmd *qc)
-{
-       struct ata_port *ap = qc->ap;
-       struct scatterlist *sg;
-       unsigned int si, pi;
-
-       pi = 0;
-       for_each_sg(qc->sg, sg, qc->n_elem, si) {
-               u32 addr, offset;
-               u32 sg_len, len;
-
-               /* determine if physical DMA addr spans 64K boundary.
-                * Note h/w doesn't support 64-bit, so we unconditionally
-                * truncate dma_addr_t to u32.
-                */
-               addr = (u32) sg_dma_address(sg);
-               sg_len = sg_dma_len(sg);
-
-               while (sg_len) {
-                       offset = addr & 0xffff;
-                       len = sg_len;
-                       if ((offset + sg_len) > 0x10000)
-                               len = 0x10000 - offset;
-
-                       ap->prd[pi].addr = cpu_to_le32(addr);
-                       ap->prd[pi].flags_len = cpu_to_le32(len & 0xffff);
-                       VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
-
-                       pi++;
-                       sg_len -= len;
-                       addr += len;
-               }
-       }
-
-       ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
-}
-
-/**
- *     ata_fill_sg_dumb - Fill PCI IDE PRD table
- *     @qc: Metadata associated with taskfile to be transferred
- *
- *     Fill PCI IDE PRD (scatter-gather) table with segments
- *     associated with the current disk command. Perform the fill
- *     so that we avoid writing any length 64K records for
- *     controllers that don't follow the spec.
- *
- *     LOCKING:
- *     spin_lock_irqsave(host lock)
- *
- */
-static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
-{
-       struct ata_port *ap = qc->ap;
-       struct scatterlist *sg;
-       unsigned int si, pi;
-
-       pi = 0;
-       for_each_sg(qc->sg, sg, qc->n_elem, si) {
-               u32 addr, offset;
-               u32 sg_len, len, blen;
-
-               /* determine if physical DMA addr spans 64K boundary.
-                * Note h/w doesn't support 64-bit, so we unconditionally
-                * truncate dma_addr_t to u32.
-                */
-               addr = (u32) sg_dma_address(sg);
-               sg_len = sg_dma_len(sg);
-
-               while (sg_len) {
-                       offset = addr & 0xffff;
-                       len = sg_len;
-                       if ((offset + sg_len) > 0x10000)
-                               len = 0x10000 - offset;
-
-                       blen = len & 0xffff;
-                       ap->prd[pi].addr = cpu_to_le32(addr);
-                       if (blen == 0) {
-                               /* Some PATA chipsets like the CS5530 can't
-                                  cope with 0x0000 meaning 64K as the spec
-                                  says */
-                               ap->prd[pi].flags_len = cpu_to_le32(0x8000);
-                               blen = 0x8000;
-                               ap->prd[++pi].addr = cpu_to_le32(addr + 0x8000);
-                       }
-                       ap->prd[pi].flags_len = cpu_to_le32(blen);
-                       VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
-
-                       pi++;
-                       sg_len -= len;
-                       addr += len;
-               }
-       }
-
-       ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
-}
-
-/**
- *     ata_sff_qc_prep - Prepare taskfile for submission
- *     @qc: Metadata associated with taskfile to be prepared
- *
- *     Prepare ATA taskfile for submission.
- *
- *     LOCKING:
- *     spin_lock_irqsave(host lock)
- */
-void ata_sff_qc_prep(struct ata_queued_cmd *qc)
-{
-       if (!(qc->flags & ATA_QCFLAG_DMAMAP))
-               return;
-
-       ata_fill_sg(qc);
-}
-EXPORT_SYMBOL_GPL(ata_sff_qc_prep);
-
-/**
- *     ata_sff_dumb_qc_prep - Prepare taskfile for submission
- *     @qc: Metadata associated with taskfile to be prepared
- *
- *     Prepare ATA taskfile for submission.
- *
- *     LOCKING:
- *     spin_lock_irqsave(host lock)
- */
-void ata_sff_dumb_qc_prep(struct ata_queued_cmd *qc)
-{
-       if (!(qc->flags & ATA_QCFLAG_DMAMAP))
-               return;
-
-       ata_fill_sg_dumb(qc);
-}
-EXPORT_SYMBOL_GPL(ata_sff_dumb_qc_prep);
-
 /**
  *     ata_sff_check_status - Read device status reg & clear interrupt
  *     @ap: port where the device is
@@ -445,6 +280,27 @@ int ata_sff_wait_ready(struct ata_link *link, unsigned long deadline)
 EXPORT_SYMBOL_GPL(ata_sff_wait_ready);
 
 /**
+ *     ata_sff_set_devctl - Write device control reg
+ *     @ap: port where the device is
+ *     @ctl: value to write
+ *
+ *     Writes ATA taskfile device control register.
+ *
+ *     Note: may NOT be used as the sff_set_devctl() entry in
+ *     ata_port_operations.
+ *
+ *     LOCKING:
+ *     Inherited from caller.
+ */
+static void ata_sff_set_devctl(struct ata_port *ap, u8 ctl)
+{
+       if (ap->ops->sff_set_devctl)
+               ap->ops->sff_set_devctl(ap, ctl);
+       else
+               iowrite8(ctl, ap->ioaddr.ctl_addr);
+}
+
+/**
  *     ata_sff_dev_select - Select device 0/1 on ATA bus
  *     @ap: ATA channel to manipulate
  *     @device: ATA device (numbered from zero) to select
@@ -490,7 +346,7 @@ EXPORT_SYMBOL_GPL(ata_sff_dev_select);
  *     LOCKING:
  *     caller.
  */
-void ata_dev_select(struct ata_port *ap, unsigned int device,
+static void ata_dev_select(struct ata_port *ap, unsigned int device,
                           unsigned int wait, unsigned int can_sleep)
 {
        if (ata_msg_probe(ap))
@@ -516,50 +372,34 @@ void ata_dev_select(struct ata_port *ap, unsigned int device,
  *     Enable interrupts on a legacy IDE device using MMIO or PIO,
  *     wait for idle, clear any pending interrupts.
  *
+ *     Note: may NOT be used as the sff_irq_on() entry in
+ *     ata_port_operations.
+ *
  *     LOCKING:
  *     Inherited from caller.
  */
-u8 ata_sff_irq_on(struct ata_port *ap)
+void ata_sff_irq_on(struct ata_port *ap)
 {
        struct ata_ioports *ioaddr = &ap->ioaddr;
-       u8 tmp;
+
+       if (ap->ops->sff_irq_on) {
+               ap->ops->sff_irq_on(ap);
+               return;
+       }
 
        ap->ctl &= ~ATA_NIEN;
        ap->last_ctl = ap->ctl;
 
-       if (ioaddr->ctl_addr)
-               iowrite8(ap->ctl, ioaddr->ctl_addr);
-       tmp = ata_wait_idle(ap);
-
-       ap->ops->sff_irq_clear(ap);
+       if (ap->ops->sff_set_devctl || ioaddr->ctl_addr)
+               ata_sff_set_devctl(ap, ap->ctl);
+       ata_wait_idle(ap);
 
-       return tmp;
+       if (ap->ops->sff_irq_clear)
+               ap->ops->sff_irq_clear(ap);
 }
 EXPORT_SYMBOL_GPL(ata_sff_irq_on);
 
 /**
- *     ata_sff_irq_clear - Clear PCI IDE BMDMA interrupt.
- *     @ap: Port associated with this ATA transaction.
- *
- *     Clear interrupt and error flags in DMA status register.
- *
- *     May be used as the irq_clear() entry in ata_port_operations.
- *
- *     LOCKING:
- *     spin_lock_irqsave(host lock)
- */
-void ata_sff_irq_clear(struct ata_port *ap)
-{
-       void __iomem *mmio = ap->ioaddr.bmdma_addr;
-
-       if (!mmio)
-               return;
-
-       iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS);
-}
-EXPORT_SYMBOL_GPL(ata_sff_irq_clear);
-
-/**
  *     ata_sff_tf_load - send taskfile registers to host controller
  *     @ap: Port to which output is sent
  *     @tf: ATA taskfile register set
@@ -578,7 +418,6 @@ void ata_sff_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
                if (ioaddr->ctl_addr)
                        iowrite8(tf->ctl, ioaddr->ctl_addr);
                ap->last_ctl = tf->ctl;
-               ata_wait_idle(ap);
        }
 
        if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
@@ -614,8 +453,6 @@ void ata_sff_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
                iowrite8(tf->device, ioaddr->device_addr);
                VPRINTK("device 0x%X\n", tf->device);
        }
-
-       ata_wait_idle(ap);
 }
 EXPORT_SYMBOL_GPL(ata_sff_tf_load);
 
@@ -893,7 +730,7 @@ static void ata_pio_sector(struct ata_queued_cmd *qc)
                                       do_write);
        }
 
-       if (!do_write)
+       if (!do_write && !PageSlab(page))
                flush_dcache_page(page);
 
        qc->curbytes += qc->sect_size;
@@ -961,11 +798,15 @@ static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
        case ATAPI_PROT_NODATA:
                ap->hsm_task_state = HSM_ST_LAST;
                break;
+#ifdef CONFIG_ATA_BMDMA
        case ATAPI_PROT_DMA:
                ap->hsm_task_state = HSM_ST_LAST;
                /* initiate bmdma */
                ap->ops->bmdma_start(qc);
                break;
+#endif /* CONFIG_ATA_BMDMA */
+       default:
+               BUG();
        }
 }
 
@@ -1164,7 +1005,7 @@ static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
                        qc = ata_qc_from_tag(ap, qc->tag);
                        if (qc) {
                                if (likely(!(qc->err_mask & AC_ERR_HSM))) {
-                                       ap->ops->sff_irq_on(ap);
+                                       ata_sff_irq_on(ap);
                                        ata_qc_complete(qc);
                                } else
                                        ata_port_freeze(ap);
@@ -1180,7 +1021,7 @@ static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
        } else {
                if (in_wq) {
                        spin_lock_irqsave(ap->lock, flags);
-                       ap->ops->sff_irq_on(ap);
+                       ata_sff_irq_on(ap);
                        ata_qc_complete(qc);
                        spin_unlock_irqrestore(ap->lock, flags);
                } else
@@ -1292,7 +1133,7 @@ fsm_start:
                if (in_wq)
                        spin_unlock_irqrestore(ap->lock, flags);
 
-               /* if polling, ata_pio_task() handles the rest.
+               /* if polling, ata_sff_pio_task() handles the rest.
                 * otherwise, interrupt handler takes over from here.
                 */
                break;
@@ -1457,14 +1298,38 @@ fsm_start:
 }
 EXPORT_SYMBOL_GPL(ata_sff_hsm_move);
 
-void ata_pio_task(struct work_struct *work)
+void ata_sff_queue_pio_task(struct ata_port *ap, unsigned long delay)
+{
+       /* may fail if ata_sff_flush_pio_task() in progress */
+       queue_delayed_work(ata_sff_wq, &ap->sff_pio_task,
+                          msecs_to_jiffies(delay));
+}
+EXPORT_SYMBOL_GPL(ata_sff_queue_pio_task);
+
+void ata_sff_flush_pio_task(struct ata_port *ap)
+{
+       DPRINTK("ENTER\n");
+
+       cancel_rearming_delayed_work(&ap->sff_pio_task);
+       ap->hsm_task_state = HSM_ST_IDLE;
+
+       if (ata_msg_ctl(ap))
+               ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __func__);
+}
+
+static void ata_sff_pio_task(struct work_struct *work)
 {
        struct ata_port *ap =
-               container_of(work, struct ata_port, port_task.work);
-       struct ata_queued_cmd *qc = ap->port_task_data;
+               container_of(work, struct ata_port, sff_pio_task.work);
+       struct ata_queued_cmd *qc;
        u8 status;
        int poll_next;
 
+       /* qc can be NULL if timeout occurred */
+       qc = ata_qc_from_tag(ap, ap->link.active_tag);
+       if (!qc)
+               return;
+
 fsm_start:
        WARN_ON_ONCE(ap->hsm_task_state == HSM_ST_IDLE);
 
@@ -1480,7 +1345,7 @@ fsm_start:
                msleep(2);
                status = ata_sff_busy_wait(ap, ATA_BUSY, 10);
                if (status & ATA_BUSY) {
-                       ata_pio_queue_task(ap, qc, ATA_SHORT_PAUSE);
+                       ata_sff_queue_pio_task(ap, ATA_SHORT_PAUSE);
                        return;
                }
        }
@@ -1496,15 +1361,11 @@ fsm_start:
 }
 
 /**
- *     ata_sff_qc_issue - issue taskfile to device in proto-dependent manner
+ *     ata_sff_qc_issue - issue taskfile to a SFF controller
  *     @qc: command to issue to device
  *
- *     Using various libata functions and hooks, this function
- *     starts an ATA command.  ATA commands are grouped into
- *     classes called "protocols", and issuing each type of protocol
- *     is slightly different.
- *
- *     May be used as the qc_issue() entry in ata_port_operations.
+ *     This function issues a PIO or NODATA command to a SFF
+ *     controller.
  *
  *     LOCKING:
  *     spin_lock_irqsave(host lock)
@@ -1519,23 +1380,8 @@ unsigned int ata_sff_qc_issue(struct ata_queued_cmd *qc)
        /* Use polling pio if the LLD doesn't handle
         * interrupt driven pio and atapi CDB interrupt.
         */
-       if (ap->flags & ATA_FLAG_PIO_POLLING) {
-               switch (qc->tf.protocol) {
-               case ATA_PROT_PIO:
-               case ATA_PROT_NODATA:
-               case ATAPI_PROT_PIO:
-               case ATAPI_PROT_NODATA:
-                       qc->tf.flags |= ATA_TFLAG_POLLING;
-                       break;
-               case ATAPI_PROT_DMA:
-                       if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
-                               /* see ata_dma_blacklisted() */
-                               BUG();
-                       break;
-               default:
-                       break;
-               }
-       }
+       if (ap->flags & ATA_FLAG_PIO_POLLING)
+               qc->tf.flags |= ATA_TFLAG_POLLING;
 
        /* select the device */
        ata_dev_select(ap, qc->dev->devno, 1, 0);
@@ -1550,17 +1396,8 @@ unsigned int ata_sff_qc_issue(struct ata_queued_cmd *qc)
                ap->hsm_task_state = HSM_ST_LAST;
 
                if (qc->tf.flags & ATA_TFLAG_POLLING)
-                       ata_pio_queue_task(ap, qc, 0);
-
-               break;
+                       ata_sff_queue_pio_task(ap, 0);
 
-       case ATA_PROT_DMA:
-               WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING);
-
-               ap->ops->sff_tf_load(ap, &qc->tf);  /* load tf registers */
-               ap->ops->bmdma_setup(qc);           /* set up bmdma */
-               ap->ops->bmdma_start(qc);           /* initiate bmdma */
-               ap->hsm_task_state = HSM_ST_LAST;
                break;
 
        case ATA_PROT_PIO:
@@ -1572,20 +1409,21 @@ unsigned int ata_sff_qc_issue(struct ata_queued_cmd *qc)
                if (qc->tf.flags & ATA_TFLAG_WRITE) {
                        /* PIO data out protocol */
                        ap->hsm_task_state = HSM_ST_FIRST;
-                       ata_pio_queue_task(ap, qc, 0);
+                       ata_sff_queue_pio_task(ap, 0);
 
-                       /* always send first data block using
-                        * the ata_pio_task() codepath.
+                       /* always send first data block using the
+                        * ata_sff_pio_task() codepath.
                         */
                } else {
                        /* PIO data in protocol */
                        ap->hsm_task_state = HSM_ST;
 
                        if (qc->tf.flags & ATA_TFLAG_POLLING)
-                               ata_pio_queue_task(ap, qc, 0);
+                               ata_sff_queue_pio_task(ap, 0);
 
-                       /* if polling, ata_pio_task() handles the rest.
-                        * otherwise, interrupt handler takes over from here.
+                       /* if polling, ata_sff_pio_task() handles the
+                        * rest.  otherwise, interrupt handler takes
+                        * over from here.
                         */
                }
 
@@ -1603,19 +1441,7 @@ unsigned int ata_sff_qc_issue(struct ata_queued_cmd *qc)
                /* send cdb by polling if no cdb interrupt */
                if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
                    (qc->tf.flags & ATA_TFLAG_POLLING))
-                       ata_pio_queue_task(ap, qc, 0);
-               break;
-
-       case ATAPI_PROT_DMA:
-               WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING);
-
-               ap->ops->sff_tf_load(ap, &qc->tf);  /* load tf registers */
-               ap->ops->bmdma_setup(qc);           /* set up bmdma */
-               ap->hsm_task_state = HSM_ST_FIRST;
-
-               /* send cdb by polling if no cdb interrupt */
-               if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
-                       ata_pio_queue_task(ap, qc, 0);
+                       ata_sff_queue_pio_task(ap, 0);
                break;
 
        default:
@@ -1647,26 +1473,27 @@ bool ata_sff_qc_fill_rtf(struct ata_queued_cmd *qc)
 }
 EXPORT_SYMBOL_GPL(ata_sff_qc_fill_rtf);
 
-/**
- *     ata_sff_host_intr - Handle host interrupt for given (port, task)
- *     @ap: Port on which interrupt arrived (possibly...)
- *     @qc: Taskfile currently active in engine
- *
- *     Handle host interrupt for given queued command.  Currently,
- *     only DMA interrupts are handled.  All other commands are
- *     handled via polling with interrupts disabled (nIEN bit).
- *
- *     LOCKING:
- *     spin_lock_irqsave(host lock)
- *
- *     RETURNS:
- *     One if interrupt was handled, zero if not (shared irq).
- */
-unsigned int ata_sff_host_intr(struct ata_port *ap,
-                                     struct ata_queued_cmd *qc)
+static unsigned int ata_sff_idle_irq(struct ata_port *ap)
 {
-       struct ata_eh_info *ehi = &ap->link.eh_info;
-       u8 status, host_stat = 0;
+       ap->stats.idle_irq++;
+
+#ifdef ATA_IRQ_TRAP
+       if ((ap->stats.idle_irq % 1000) == 0) {
+               ap->ops->sff_check_status(ap);
+               if (ap->ops->sff_irq_clear)
+                       ap->ops->sff_irq_clear(ap);
+               ata_port_printk(ap, KERN_WARNING, "irq trap\n");
+               return 1;
+       }
+#endif
+       return 0;       /* irq not handled */
+}
+
+static unsigned int __ata_sff_port_intr(struct ata_port *ap,
+                                       struct ata_queued_cmd *qc,
+                                       bool hsmv_on_idle)
+{
+       u8 status;
 
        VPRINTK("ata%u: protocol %d task_state %d\n",
                ap->print_id, qc->tf.protocol, ap->hsm_task_state);
@@ -1683,108 +1510,144 @@ unsigned int ata_sff_host_intr(struct ata_port *ap,
                 * need to check ata_is_atapi(qc->tf.protocol) again.
                 */
                if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
-                       goto idle_irq;
+                       return ata_sff_idle_irq(ap);
                break;
+       case HSM_ST:
        case HSM_ST_LAST:
-               if (qc->tf.protocol == ATA_PROT_DMA ||
-                   qc->tf.protocol == ATAPI_PROT_DMA) {
-                       /* check status of DMA engine */
-                       host_stat = ap->ops->bmdma_status(ap);
-                       VPRINTK("ata%u: host_stat 0x%X\n",
-                               ap->print_id, host_stat);
-
-                       /* if it's not our irq... */
-                       if (!(host_stat & ATA_DMA_INTR))
-                               goto idle_irq;
-
-                       /* before we do anything else, clear DMA-Start bit */
-                       ap->ops->bmdma_stop(qc);
-
-                       if (unlikely(host_stat & ATA_DMA_ERR)) {
-                               /* error when transfering data to/from memory */
-                               qc->err_mask |= AC_ERR_HOST_BUS;
-                               ap->hsm_task_state = HSM_ST_ERR;
-                       }
-               }
-               break;
-       case HSM_ST:
                break;
        default:
-               goto idle_irq;
+               return ata_sff_idle_irq(ap);
        }
 
-
        /* check main status, clearing INTRQ if needed */
        status = ata_sff_irq_status(ap);
-       if (status & ATA_BUSY)
-               goto idle_irq;
+       if (status & ATA_BUSY) {
+               if (hsmv_on_idle) {
+                       /* BMDMA engine is already stopped, we're screwed */
+                       qc->err_mask |= AC_ERR_HSM;
+                       ap->hsm_task_state = HSM_ST_ERR;
+               } else
+                       return ata_sff_idle_irq(ap);
+       }
 
-       /* ack bmdma irq events */
-       ap->ops->sff_irq_clear(ap);
+       /* clear irq events */
+       if (ap->ops->sff_irq_clear)
+               ap->ops->sff_irq_clear(ap);
 
        ata_sff_hsm_move(ap, qc, status, 0);
 
-       if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
-                                      qc->tf.protocol == ATAPI_PROT_DMA))
-               ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
-
        return 1;       /* irq handled */
-
-idle_irq:
-       ap->stats.idle_irq++;
-
-#ifdef ATA_IRQ_TRAP
-       if ((ap->stats.idle_irq % 1000) == 0) {
-               ap->ops->sff_check_status(ap);
-               ap->ops->sff_irq_clear(ap);
-               ata_port_printk(ap, KERN_WARNING, "irq trap\n");
-               return 1;
-       }
-#endif
-       return 0;       /* irq not handled */
 }
-EXPORT_SYMBOL_GPL(ata_sff_host_intr);
 
 /**
- *     ata_sff_interrupt - Default ATA host interrupt handler
- *     @irq: irq line (unused)
- *     @dev_instance: pointer to our ata_host information structure
+ *     ata_sff_port_intr - Handle SFF port interrupt
+ *     @ap: Port on which interrupt arrived (possibly...)
+ *     @qc: Taskfile currently active in engine
  *
- *     Default interrupt handler for PCI IDE devices.  Calls
- *     ata_sff_host_intr() for each port that is not disabled.
+ *     Handle port interrupt for given queued command.
  *
  *     LOCKING:
- *     Obtains host lock during operation.
+ *     spin_lock_irqsave(host lock)
  *
  *     RETURNS:
- *     IRQ_NONE or IRQ_HANDLED.
+ *     One if interrupt was handled, zero if not (shared irq).
  */
-irqreturn_t ata_sff_interrupt(int irq, void *dev_instance)
+unsigned int ata_sff_port_intr(struct ata_port *ap, struct ata_queued_cmd *qc)
+{
+       return __ata_sff_port_intr(ap, qc, false);
+}
+EXPORT_SYMBOL_GPL(ata_sff_port_intr);
+
+static inline irqreturn_t __ata_sff_interrupt(int irq, void *dev_instance,
+       unsigned int (*port_intr)(struct ata_port *, struct ata_queued_cmd *))
 {
        struct ata_host *host = dev_instance;
+       bool retried = false;
        unsigned int i;
-       unsigned int handled = 0;
+       unsigned int handled, idle, polling;
        unsigned long flags;
 
        /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
        spin_lock_irqsave(&host->lock, flags);
 
+retry:
+       handled = idle = polling = 0;
        for (i = 0; i < host->n_ports; i++) {
                struct ata_port *ap = host->ports[i];
                struct ata_queued_cmd *qc;
 
-               if (unlikely(ap->flags & ATA_FLAG_DISABLED))
-                       continue;
-
                qc = ata_qc_from_tag(ap, ap->link.active_tag);
-               if (qc && !(qc->tf.flags & ATA_TFLAG_POLLING))
-                       handled |= ata_sff_host_intr(ap, qc);
+               if (qc) {
+                       if (!(qc->tf.flags & ATA_TFLAG_POLLING))
+                               handled |= port_intr(ap, qc);
+                       else
+                               polling |= 1 << i;
+               } else
+                       idle |= 1 << i;
+       }
+
+       /*
+        * If no port was expecting IRQ but the controller is actually
+        * asserting IRQ line, nobody cared will ensue.  Check IRQ
+        * pending status if available and clear spurious IRQ.
+        */
+       if (!handled && !retried) {
+               bool retry = false;
+
+               for (i = 0; i < host->n_ports; i++) {
+                       struct ata_port *ap = host->ports[i];
+
+                       if (polling & (1 << i))
+                               continue;
+
+                       if (!ap->ops->sff_irq_check ||
+                           !ap->ops->sff_irq_check(ap))
+                               continue;
+
+                       if (idle & (1 << i)) {
+                               ap->ops->sff_check_status(ap);
+                               if (ap->ops->sff_irq_clear)
+                                       ap->ops->sff_irq_clear(ap);
+                       } else {
+                               /* clear INTRQ and check if BUSY cleared */
+                               if (!(ap->ops->sff_check_status(ap) & ATA_BUSY))
+                                       retry |= true;
+                               /*
+                                * With command in flight, we can't do
+                                * sff_irq_clear() w/o racing with completion.
+                                */
+                       }
+               }
+
+               if (retry) {
+                       retried = true;
+                       goto retry;
+               }
        }
 
        spin_unlock_irqrestore(&host->lock, flags);
 
        return IRQ_RETVAL(handled);
 }
+
+/**
+ *     ata_sff_interrupt - Default SFF ATA host interrupt handler
+ *     @irq: irq line (unused)
+ *     @dev_instance: pointer to our ata_host information structure
+ *
+ *     Default interrupt handler for PCI IDE devices.  Calls
+ *     ata_sff_port_intr() for each port that is not disabled.
+ *
+ *     LOCKING:
+ *     Obtains host lock during operation.
+ *
+ *     RETURNS:
+ *     IRQ_NONE or IRQ_HANDLED.
+ */
+irqreturn_t ata_sff_interrupt(int irq, void *dev_instance)
+{
+       return __ata_sff_interrupt(irq, dev_instance, ata_sff_port_intr);
+}
 EXPORT_SYMBOL_GPL(ata_sff_interrupt);
 
 /**
@@ -1807,11 +1670,8 @@ void ata_sff_lost_interrupt(struct ata_port *ap)
 
        /* Only one outstanding command per SFF channel */
        qc = ata_qc_from_tag(ap, ap->link.active_tag);
-       /* Check we have a live one.. */
-       if (qc == NULL ||  !(qc->flags & ATA_QCFLAG_ACTIVE))
-               return;
-       /* We cannot lose an interrupt on a polled command */
-       if (qc->tf.flags & ATA_TFLAG_POLLING)
+       /* We cannot lose an interrupt on a non-existent or polled command */
+       if (!qc || qc->tf.flags & ATA_TFLAG_POLLING)
                return;
        /* See if the controller thinks it is still busy - if so the command
           isn't a lost IRQ but is still in progress */
@@ -1825,7 +1685,7 @@ void ata_sff_lost_interrupt(struct ata_port *ap)
                                                                status);
        /* Run the host interrupt logic as if the interrupt had not been
           lost */
-       ata_sff_host_intr(ap, qc);
+       ata_sff_port_intr(ap, qc);
 }
 EXPORT_SYMBOL_GPL(ata_sff_lost_interrupt);
 
@@ -1833,20 +1693,18 @@ EXPORT_SYMBOL_GPL(ata_sff_lost_interrupt);
  *     ata_sff_freeze - Freeze SFF controller port
  *     @ap: port to freeze
  *
- *     Freeze BMDMA controller port.
+ *     Freeze SFF controller port.
  *
  *     LOCKING:
  *     Inherited from caller.
  */
 void ata_sff_freeze(struct ata_port *ap)
 {
-       struct ata_ioports *ioaddr = &ap->ioaddr;
-
        ap->ctl |= ATA_NIEN;
        ap->last_ctl = ap->ctl;
 
-       if (ioaddr->ctl_addr)
-               iowrite8(ap->ctl, ioaddr->ctl_addr);
+       if (ap->ops->sff_set_devctl || ap->ioaddr.ctl_addr)
+               ata_sff_set_devctl(ap, ap->ctl);
 
        /* Under certain circumstances, some controllers raise IRQ on
         * ATA_NIEN manipulation.  Also, many controllers fail to mask
@@ -1854,7 +1712,8 @@ void ata_sff_freeze(struct ata_port *ap)
         */
        ap->ops->sff_check_status(ap);
 
-       ap->ops->sff_irq_clear(ap);
+       if (ap->ops->sff_irq_clear)
+               ap->ops->sff_irq_clear(ap);
 }
 EXPORT_SYMBOL_GPL(ata_sff_freeze);
 
@@ -1871,8 +1730,9 @@ void ata_sff_thaw(struct ata_port *ap)
 {
        /* clear & re-enable interrupts */
        ap->ops->sff_check_status(ap);
-       ap->ops->sff_irq_clear(ap);
-       ap->ops->sff_irq_on(ap);
+       if (ap->ops->sff_irq_clear)
+               ap->ops->sff_irq_clear(ap);
+       ata_sff_irq_on(ap);
 }
 EXPORT_SYMBOL_GPL(ata_sff_thaw);
 
@@ -2246,8 +2106,8 @@ void ata_sff_postreset(struct ata_link *link, unsigned int *classes)
        }
 
        /* set up device control */
-       if (ap->ioaddr.ctl_addr) {
-               iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
+       if (ap->ops->sff_set_devctl || ap->ioaddr.ctl_addr) {
+               ata_sff_set_devctl(ap, ap->ctl);
                ap->last_ctl = ap->ctl;
        }
 }
@@ -2258,7 +2118,7 @@ EXPORT_SYMBOL_GPL(ata_sff_postreset);
  *     @qc: command
  *
  *     Drain the FIFO and device of any stuck data following a command
- *     failing to complete. In some cases this is neccessary before a
+ *     failing to complete. In some cases this is necessary before a
  *     reset will recover the device.
  *
  */
@@ -2287,7 +2147,7 @@ void ata_sff_drain_fifo(struct ata_queued_cmd *qc)
 EXPORT_SYMBOL_GPL(ata_sff_drain_fifo);
 
 /**
- *     ata_sff_error_handler - Stock error handler for BMDMA controller
+ *     ata_sff_error_handler - Stock error handler for SFF controller
  *     @ap: port to handle error for
  *
  *     Stock error handler for SFF controller.  It can handle both
@@ -2304,62 +2164,32 @@ void ata_sff_error_handler(struct ata_port *ap)
        ata_reset_fn_t hardreset = ap->ops->hardreset;
        struct ata_queued_cmd *qc;
        unsigned long flags;
-       int thaw = 0;
 
        qc = __ata_qc_from_tag(ap, ap->link.active_tag);
        if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
                qc = NULL;
 
-       /* reset PIO HSM and stop DMA engine */
        spin_lock_irqsave(ap->lock, flags);
 
-       ap->hsm_task_state = HSM_ST_IDLE;
-
-       if (ap->ioaddr.bmdma_addr &&
-           qc && (qc->tf.protocol == ATA_PROT_DMA ||
-                  qc->tf.protocol == ATAPI_PROT_DMA)) {
-               u8 host_stat;
-
-               host_stat = ap->ops->bmdma_status(ap);
-
-               /* BMDMA controllers indicate host bus error by
-                * setting DMA_ERR bit and timing out.  As it wasn't
-                * really a timeout event, adjust error mask and
-                * cancel frozen state.
-                */
-               if (qc->err_mask == AC_ERR_TIMEOUT
-                                               && (host_stat & ATA_DMA_ERR)) {
-                       qc->err_mask = AC_ERR_HOST_BUS;
-                       thaw = 1;
-               }
-
-               ap->ops->bmdma_stop(qc);
-       }
-
-       ata_sff_sync(ap);               /* FIXME: We don't need this */
-       ap->ops->sff_check_status(ap);
-       ap->ops->sff_irq_clear(ap);
-       /* We *MUST* do FIFO draining before we issue a reset as several
-        * devices helpfully clear their internal state and will lock solid
-        * if we touch the data port post reset. Pass qc in case anyone wants
-        *  to do different PIO/DMA recovery or has per command fixups
+       /*
+        * We *MUST* do FIFO draining before we issue a reset as
+        * several devices helpfully clear their internal state and
+        * will lock solid if we touch the data port post reset. Pass
+        * qc in case anyone wants to do different PIO/DMA recovery or
+        * has per command fixups
         */
-       if (ap->ops->drain_fifo)
-               ap->ops->drain_fifo(qc);
+       if (ap->ops->sff_drain_fifo)
+               ap->ops->sff_drain_fifo(qc);
 
        spin_unlock_irqrestore(ap->lock, flags);
 
-       if (thaw)
-               ata_eh_thaw_port(ap);
-
-       /* PIO and DMA engines have been stopped, perform recovery */
-
-       /* Ignore ata_sff_softreset if ctl isn't accessible and
-        * built-in hardresets if SCR access isn't available.
-        */
+       /* ignore ata_sff_softreset if ctl isn't accessible */
        if (softreset == ata_sff_softreset && !ap->ioaddr.ctl_addr)
                softreset = NULL;
-       if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link))
+
+       /* ignore built-in hardresets if SCR access is not available */
+       if ((hardreset == sata_std_hardreset ||
+            hardreset == sata_sff_hardreset) && !sata_scr_valid(&ap->link))
                hardreset = NULL;
 
        ata_do_eh(ap, ap->ops->prereset, softreset, hardreset,
@@ -2368,73 +2198,6 @@ void ata_sff_error_handler(struct ata_port *ap)
 EXPORT_SYMBOL_GPL(ata_sff_error_handler);
 
 /**
- *     ata_sff_post_internal_cmd - Stock post_internal_cmd for SFF controller
- *     @qc: internal command to clean up
- *
- *     LOCKING:
- *     Kernel thread context (may sleep)
- */
-void ata_sff_post_internal_cmd(struct ata_queued_cmd *qc)
-{
-       struct ata_port *ap = qc->ap;
-       unsigned long flags;
-
-       spin_lock_irqsave(ap->lock, flags);
-
-       ap->hsm_task_state = HSM_ST_IDLE;
-
-       if (ap->ioaddr.bmdma_addr)
-               ap->ops->bmdma_stop(qc);
-
-       spin_unlock_irqrestore(ap->lock, flags);
-}
-EXPORT_SYMBOL_GPL(ata_sff_post_internal_cmd);
-
-/**
- *     ata_sff_port_start - Set port up for dma.
- *     @ap: Port to initialize
- *
- *     Called just after data structures for each port are
- *     initialized.  Allocates space for PRD table if the device
- *     is DMA capable SFF.
- *
- *     May be used as the port_start() entry in ata_port_operations.
- *
- *     LOCKING:
- *     Inherited from caller.
- */
-int ata_sff_port_start(struct ata_port *ap)
-{
-       if (ap->ioaddr.bmdma_addr)
-               return ata_port_start(ap);
-       return 0;
-}
-EXPORT_SYMBOL_GPL(ata_sff_port_start);
-
-/**
- *     ata_sff_port_start32 - Set port up for dma.
- *     @ap: Port to initialize
- *
- *     Called just after data structures for each port are
- *     initialized.  Allocates space for PRD table if the device
- *     is DMA capable SFF.
- *
- *     May be used as the port_start() entry in ata_port_operations for
- *     devices that are capable of 32bit PIO.
- *
- *     LOCKING:
- *     Inherited from caller.
- */
-int ata_sff_port_start32(struct ata_port *ap)
-{
-       ap->pflags |= ATA_PFLAG_PIO32 | ATA_PFLAG_PIO32CHANGE;
-       if (ap->ioaddr.bmdma_addr)
-               return ata_port_start(ap);
-       return 0;
-}
-EXPORT_SYMBOL_GPL(ata_sff_port_start32);
-
-/**
  *     ata_sff_std_ports - initialize ioaddr with standard port offsets.
  *     @ioaddr: IO address structure to be initialized
  *
@@ -2460,53 +2223,766 @@ void ata_sff_std_ports(struct ata_ioports *ioaddr)
 }
 EXPORT_SYMBOL_GPL(ata_sff_std_ports);
 
-unsigned long ata_bmdma_mode_filter(struct ata_device *adev,
-                                   unsigned long xfer_mask)
+#ifdef CONFIG_PCI
+
+static int ata_resources_present(struct pci_dev *pdev, int port)
 {
-       /* Filter out DMA modes if the device has been configured by
-          the BIOS as PIO only */
+       int i;
 
-       if (adev->link->ap->ioaddr.bmdma_addr == NULL)
-               xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
-       return xfer_mask;
+       /* Check the PCI resources for this channel are enabled */
+       port = port * 2;
+       for (i = 0; i < 2; i++) {
+               if (pci_resource_start(pdev, port + i) == 0 ||
+                   pci_resource_len(pdev, port + i) == 0)
+                       return 0;
+       }
+       return 1;
 }
-EXPORT_SYMBOL_GPL(ata_bmdma_mode_filter);
 
 /**
- *     ata_bmdma_setup - Set up PCI IDE BMDMA transaction
- *     @qc: Info associated with this ATA transaction.
+ *     ata_pci_sff_init_host - acquire native PCI ATA resources and init host
+ *     @host: target ATA host
+ *
+ *     Acquire native PCI ATA resources for @host and initialize the
+ *     first two ports of @host accordingly.  Ports marked dummy are
+ *     skipped and allocation failure makes the port dummy.
+ *
+ *     Note that native PCI resources are valid even for legacy hosts
+ *     as we fix up pdev resources array early in boot, so this
+ *     function can be used for both native and legacy SFF hosts.
  *
  *     LOCKING:
- *     spin_lock_irqsave(host lock)
+ *     Inherited from calling layer (may sleep).
+ *
+ *     RETURNS:
+ *     0 if at least one port is initialized, -ENODEV if no port is
+ *     available.
  */
-void ata_bmdma_setup(struct ata_queued_cmd *qc)
+int ata_pci_sff_init_host(struct ata_host *host)
 {
-       struct ata_port *ap = qc->ap;
-       unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
-       u8 dmactl;
+       struct device *gdev = host->dev;
+       struct pci_dev *pdev = to_pci_dev(gdev);
+       unsigned int mask = 0;
+       int i, rc;
 
-       /* load PRD table addr. */
-       mb();   /* make sure PRD table writes are visible to controller */
-       iowrite32(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
+       /* request, iomap BARs and init port addresses accordingly */
+       for (i = 0; i < 2; i++) {
+               struct ata_port *ap = host->ports[i];
+               int base = i * 2;
+               void __iomem * const *iomap;
 
-       /* specify data direction, triple-check start bit is clear */
-       dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
-       dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
-       if (!rw)
-               dmactl |= ATA_DMA_WR;
-       iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
+               if (ata_port_is_dummy(ap))
+                       continue;
 
-       /* issue r/w command */
-       ap->ops->sff_exec_command(ap, &qc->tf);
+               /* Discard disabled ports.  Some controllers show
+                * their unused channels this way.  Disabled ports are
+                * made dummy.
+                */
+               if (!ata_resources_present(pdev, i)) {
+                       ap->ops = &ata_dummy_port_ops;
+                       continue;
+               }
+
+               rc = pcim_iomap_regions(pdev, 0x3 << base,
+                                       dev_driver_string(gdev));
+               if (rc) {
+                       dev_printk(KERN_WARNING, gdev,
+                                  "failed to request/iomap BARs for port %d "
+                                  "(errno=%d)\n", i, rc);
+                       if (rc == -EBUSY)
+                               pcim_pin_device(pdev);
+                       ap->ops = &ata_dummy_port_ops;
+                       continue;
+               }
+               host->iomap = iomap = pcim_iomap_table(pdev);
+
+               ap->ioaddr.cmd_addr = iomap[base];
+               ap->ioaddr.altstatus_addr =
+               ap->ioaddr.ctl_addr = (void __iomem *)
+                       ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS);
+               ata_sff_std_ports(&ap->ioaddr);
+
+               ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
+                       (unsigned long long)pci_resource_start(pdev, base),
+                       (unsigned long long)pci_resource_start(pdev, base + 1));
+
+               mask |= 1 << i;
+       }
+
+       if (!mask) {
+               dev_printk(KERN_ERR, gdev, "no available native port\n");
+               return -ENODEV;
+       }
+
+       return 0;
 }
-EXPORT_SYMBOL_GPL(ata_bmdma_setup);
+EXPORT_SYMBOL_GPL(ata_pci_sff_init_host);
 
 /**
- *     ata_bmdma_start - Start a PCI IDE BMDMA transaction
- *     @qc: Info associated with this ATA transaction.
+ *     ata_pci_sff_prepare_host - helper to prepare PCI PIO-only SFF ATA host
+ *     @pdev: target PCI device
+ *     @ppi: array of port_info, must be enough for two ports
+ *     @r_host: out argument for the initialized ATA host
+ *
+ *     Helper to allocate PIO-only SFF ATA host for @pdev, acquire
+ *     all PCI resources and initialize it accordingly in one go.
  *
  *     LOCKING:
- *     spin_lock_irqsave(host lock)
+ *     Inherited from calling layer (may sleep).
+ *
+ *     RETURNS:
+ *     0 on success, -errno otherwise.
+ */
+int ata_pci_sff_prepare_host(struct pci_dev *pdev,
+                            const struct ata_port_info * const *ppi,
+                            struct ata_host **r_host)
+{
+       struct ata_host *host;
+       int rc;
+
+       if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
+               return -ENOMEM;
+
+       host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
+       if (!host) {
+               dev_printk(KERN_ERR, &pdev->dev,
+                          "failed to allocate ATA host\n");
+               rc = -ENOMEM;
+               goto err_out;
+       }
+
+       rc = ata_pci_sff_init_host(host);
+       if (rc)
+               goto err_out;
+
+       devres_remove_group(&pdev->dev, NULL);
+       *r_host = host;
+       return 0;
+
+err_out:
+       devres_release_group(&pdev->dev, NULL);
+       return rc;
+}
+EXPORT_SYMBOL_GPL(ata_pci_sff_prepare_host);
+
+/**
+ *     ata_pci_sff_activate_host - start SFF host, request IRQ and register it
+ *     @host: target SFF ATA host
+ *     @irq_handler: irq_handler used when requesting IRQ(s)
+ *     @sht: scsi_host_template to use when registering the host
+ *
+ *     This is the counterpart of ata_host_activate() for SFF ATA
+ *     hosts.  This separate helper is necessary because SFF hosts
+ *     use two separate interrupts in legacy mode.
+ *
+ *     LOCKING:
+ *     Inherited from calling layer (may sleep).
+ *
+ *     RETURNS:
+ *     0 on success, -errno otherwise.
+ */
+int ata_pci_sff_activate_host(struct ata_host *host,
+                             irq_handler_t irq_handler,
+                             struct scsi_host_template *sht)
+{
+       struct device *dev = host->dev;
+       struct pci_dev *pdev = to_pci_dev(dev);
+       const char *drv_name = dev_driver_string(host->dev);
+       int legacy_mode = 0, rc;
+
+       rc = ata_host_start(host);
+       if (rc)
+               return rc;
+
+       if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
+               u8 tmp8, mask;
+
+               /* TODO: What if one channel is in native mode ... */
+               pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
+               mask = (1 << 2) | (1 << 0);
+               if ((tmp8 & mask) != mask)
+                       legacy_mode = 1;
+#if defined(CONFIG_NO_ATA_LEGACY)
+               /* Some platforms with PCI limits cannot address compat
+                  port space. In that case we punt if their firmware has
+                  left a device in compatibility mode */
+               if (legacy_mode) {
+                       printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
+                       return -EOPNOTSUPP;
+               }
+#endif
+       }
+
+       if (!devres_open_group(dev, NULL, GFP_KERNEL))
+               return -ENOMEM;
+
+       if (!legacy_mode && pdev->irq) {
+               rc = devm_request_irq(dev, pdev->irq, irq_handler,
+                                     IRQF_SHARED, drv_name, host);
+               if (rc)
+                       goto out;
+
+               ata_port_desc(host->ports[0], "irq %d", pdev->irq);
+               ata_port_desc(host->ports[1], "irq %d", pdev->irq);
+       } else if (legacy_mode) {
+               if (!ata_port_is_dummy(host->ports[0])) {
+                       rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev),
+                                             irq_handler, IRQF_SHARED,
+                                             drv_name, host);
+                       if (rc)
+                               goto out;
+
+                       ata_port_desc(host->ports[0], "irq %d",
+                                     ATA_PRIMARY_IRQ(pdev));
+               }
+
+               if (!ata_port_is_dummy(host->ports[1])) {
+                       rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev),
+                                             irq_handler, IRQF_SHARED,
+                                             drv_name, host);
+                       if (rc)
+                               goto out;
+
+                       ata_port_desc(host->ports[1], "irq %d",
+                                     ATA_SECONDARY_IRQ(pdev));
+               }
+       }
+
+       rc = ata_host_register(host, sht);
+out:
+       if (rc == 0)
+               devres_remove_group(dev, NULL);
+       else
+               devres_release_group(dev, NULL);
+
+       return rc;
+}
+EXPORT_SYMBOL_GPL(ata_pci_sff_activate_host);
+
+static const struct ata_port_info *ata_sff_find_valid_pi(
+                                       const struct ata_port_info * const *ppi)
+{
+       int i;
+
+       /* look up the first valid port_info */
+       for (i = 0; i < 2 && ppi[i]; i++)
+               if (ppi[i]->port_ops != &ata_dummy_port_ops)
+                       return ppi[i];
+
+       return NULL;
+}
+
+/**
+ *     ata_pci_sff_init_one - Initialize/register PIO-only PCI IDE controller
+ *     @pdev: Controller to be initialized
+ *     @ppi: array of port_info, must be enough for two ports
+ *     @sht: scsi_host_template to use when registering the host
+ *     @host_priv: host private_data
+ *     @hflag: host flags
+ *
+ *     This is a helper function which can be called from a driver's
+ *     xxx_init_one() probe function if the hardware uses traditional
+ *     IDE taskfile registers and is PIO only.
+ *
+ *     ASSUMPTION:
+ *     Nobody makes a single channel controller that appears solely as
+ *     the secondary legacy port on PCI.
+ *
+ *     LOCKING:
+ *     Inherited from PCI layer (may sleep).
+ *
+ *     RETURNS:
+ *     Zero on success, negative on errno-based value on error.
+ */
+int ata_pci_sff_init_one(struct pci_dev *pdev,
+                const struct ata_port_info * const *ppi,
+                struct scsi_host_template *sht, void *host_priv, int hflag)
+{
+       struct device *dev = &pdev->dev;
+       const struct ata_port_info *pi;
+       struct ata_host *host = NULL;
+       int rc;
+
+       DPRINTK("ENTER\n");
+
+       pi = ata_sff_find_valid_pi(ppi);
+       if (!pi) {
+               dev_printk(KERN_ERR, &pdev->dev,
+                          "no valid port_info specified\n");
+               return -EINVAL;
+       }
+
+       if (!devres_open_group(dev, NULL, GFP_KERNEL))
+               return -ENOMEM;
+
+       rc = pcim_enable_device(pdev);
+       if (rc)
+               goto out;
+
+       /* prepare and activate SFF host */
+       rc = ata_pci_sff_prepare_host(pdev, ppi, &host);
+       if (rc)
+               goto out;
+       host->private_data = host_priv;
+       host->flags |= hflag;
+
+       rc = ata_pci_sff_activate_host(host, ata_sff_interrupt, sht);
+out:
+       if (rc == 0)
+               devres_remove_group(&pdev->dev, NULL);
+       else
+               devres_release_group(&pdev->dev, NULL);
+
+       return rc;
+}
+EXPORT_SYMBOL_GPL(ata_pci_sff_init_one);
+
+#endif /* CONFIG_PCI */
+
+/*
+ *     BMDMA support
+ */
+
+#ifdef CONFIG_ATA_BMDMA
+
+const struct ata_port_operations ata_bmdma_port_ops = {
+       .inherits               = &ata_sff_port_ops,
+
+       .error_handler          = ata_bmdma_error_handler,
+       .post_internal_cmd      = ata_bmdma_post_internal_cmd,
+
+       .qc_prep                = ata_bmdma_qc_prep,
+       .qc_issue               = ata_bmdma_qc_issue,
+
+       .sff_irq_clear          = ata_bmdma_irq_clear,
+       .bmdma_setup            = ata_bmdma_setup,
+       .bmdma_start            = ata_bmdma_start,
+       .bmdma_stop             = ata_bmdma_stop,
+       .bmdma_status           = ata_bmdma_status,
+
+       .port_start             = ata_bmdma_port_start,
+};
+EXPORT_SYMBOL_GPL(ata_bmdma_port_ops);
+
+const struct ata_port_operations ata_bmdma32_port_ops = {
+       .inherits               = &ata_bmdma_port_ops,
+
+       .sff_data_xfer          = ata_sff_data_xfer32,
+       .port_start             = ata_bmdma_port_start32,
+};
+EXPORT_SYMBOL_GPL(ata_bmdma32_port_ops);
+
+/**
+ *     ata_bmdma_fill_sg - Fill PCI IDE PRD table
+ *     @qc: Metadata associated with taskfile to be transferred
+ *
+ *     Fill PCI IDE PRD (scatter-gather) table with segments
+ *     associated with the current disk command.
+ *
+ *     LOCKING:
+ *     spin_lock_irqsave(host lock)
+ *
+ */
+static void ata_bmdma_fill_sg(struct ata_queued_cmd *qc)
+{
+       struct ata_port *ap = qc->ap;
+       struct ata_bmdma_prd *prd = ap->bmdma_prd;
+       struct scatterlist *sg;
+       unsigned int si, pi;
+
+       pi = 0;
+       for_each_sg(qc->sg, sg, qc->n_elem, si) {
+               u32 addr, offset;
+               u32 sg_len, len;
+
+               /* determine if physical DMA addr spans 64K boundary.
+                * Note h/w doesn't support 64-bit, so we unconditionally
+                * truncate dma_addr_t to u32.
+                */
+               addr = (u32) sg_dma_address(sg);
+               sg_len = sg_dma_len(sg);
+
+               while (sg_len) {
+                       offset = addr & 0xffff;
+                       len = sg_len;
+                       if ((offset + sg_len) > 0x10000)
+                               len = 0x10000 - offset;
+
+                       prd[pi].addr = cpu_to_le32(addr);
+                       prd[pi].flags_len = cpu_to_le32(len & 0xffff);
+                       VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
+
+                       pi++;
+                       sg_len -= len;
+                       addr += len;
+               }
+       }
+
+       prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
+}
+
+/**
+ *     ata_bmdma_fill_sg_dumb - Fill PCI IDE PRD table
+ *     @qc: Metadata associated with taskfile to be transferred
+ *
+ *     Fill PCI IDE PRD (scatter-gather) table with segments
+ *     associated with the current disk command. Perform the fill
+ *     so that we avoid writing any length 64K records for
+ *     controllers that don't follow the spec.
+ *
+ *     LOCKING:
+ *     spin_lock_irqsave(host lock)
+ *
+ */
+static void ata_bmdma_fill_sg_dumb(struct ata_queued_cmd *qc)
+{
+       struct ata_port *ap = qc->ap;
+       struct ata_bmdma_prd *prd = ap->bmdma_prd;
+       struct scatterlist *sg;
+       unsigned int si, pi;
+
+       pi = 0;
+       for_each_sg(qc->sg, sg, qc->n_elem, si) {
+               u32 addr, offset;
+               u32 sg_len, len, blen;
+
+               /* determine if physical DMA addr spans 64K boundary.
+                * Note h/w doesn't support 64-bit, so we unconditionally
+                * truncate dma_addr_t to u32.
+                */
+               addr = (u32) sg_dma_address(sg);
+               sg_len = sg_dma_len(sg);
+
+               while (sg_len) {
+                       offset = addr & 0xffff;
+                       len = sg_len;
+                       if ((offset + sg_len) > 0x10000)
+                               len = 0x10000 - offset;
+
+                       blen = len & 0xffff;
+                       prd[pi].addr = cpu_to_le32(addr);
+                       if (blen == 0) {
+                               /* Some PATA chipsets like the CS5530 can't
+                                  cope with 0x0000 meaning 64K as the spec
+                                  says */
+                               prd[pi].flags_len = cpu_to_le32(0x8000);
+                               blen = 0x8000;
+                               prd[++pi].addr = cpu_to_le32(addr + 0x8000);
+                       }
+                       prd[pi].flags_len = cpu_to_le32(blen);
+                       VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
+
+                       pi++;
+                       sg_len -= len;
+                       addr += len;
+               }
+       }
+
+       prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
+}
+
+/**
+ *     ata_bmdma_qc_prep - Prepare taskfile for submission
+ *     @qc: Metadata associated with taskfile to be prepared
+ *
+ *     Prepare ATA taskfile for submission.
+ *
+ *     LOCKING:
+ *     spin_lock_irqsave(host lock)
+ */
+void ata_bmdma_qc_prep(struct ata_queued_cmd *qc)
+{
+       if (!(qc->flags & ATA_QCFLAG_DMAMAP))
+               return;
+
+       ata_bmdma_fill_sg(qc);
+}
+EXPORT_SYMBOL_GPL(ata_bmdma_qc_prep);
+
+/**
+ *     ata_bmdma_dumb_qc_prep - Prepare taskfile for submission
+ *     @qc: Metadata associated with taskfile to be prepared
+ *
+ *     Prepare ATA taskfile for submission.
+ *
+ *     LOCKING:
+ *     spin_lock_irqsave(host lock)
+ */
+void ata_bmdma_dumb_qc_prep(struct ata_queued_cmd *qc)
+{
+       if (!(qc->flags & ATA_QCFLAG_DMAMAP))
+               return;
+
+       ata_bmdma_fill_sg_dumb(qc);
+}
+EXPORT_SYMBOL_GPL(ata_bmdma_dumb_qc_prep);
+
+/**
+ *     ata_bmdma_qc_issue - issue taskfile to a BMDMA controller
+ *     @qc: command to issue to device
+ *
+ *     This function issues a PIO, NODATA or DMA command to a
+ *     SFF/BMDMA controller.  PIO and NODATA are handled by
+ *     ata_sff_qc_issue().
+ *
+ *     LOCKING:
+ *     spin_lock_irqsave(host lock)
+ *
+ *     RETURNS:
+ *     Zero on success, AC_ERR_* mask on failure
+ */
+unsigned int ata_bmdma_qc_issue(struct ata_queued_cmd *qc)
+{
+       struct ata_port *ap = qc->ap;
+
+       /* see ata_dma_blacklisted() */
+       BUG_ON((ap->flags & ATA_FLAG_PIO_POLLING) &&
+              qc->tf.protocol == ATAPI_PROT_DMA);
+
+       /* defer PIO handling to sff_qc_issue */
+       if (!ata_is_dma(qc->tf.protocol))
+               return ata_sff_qc_issue(qc);
+
+       /* select the device */
+       ata_dev_select(ap, qc->dev->devno, 1, 0);
+
+       /* start the command */
+       switch (qc->tf.protocol) {
+       case ATA_PROT_DMA:
+               WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING);
+
+               ap->ops->sff_tf_load(ap, &qc->tf);  /* load tf registers */
+               ap->ops->bmdma_setup(qc);           /* set up bmdma */
+               ap->ops->bmdma_start(qc);           /* initiate bmdma */
+               ap->hsm_task_state = HSM_ST_LAST;
+               break;
+
+       case ATAPI_PROT_DMA:
+               WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING);
+
+               ap->ops->sff_tf_load(ap, &qc->tf);  /* load tf registers */
+               ap->ops->bmdma_setup(qc);           /* set up bmdma */
+               ap->hsm_task_state = HSM_ST_FIRST;
+
+               /* send cdb by polling if no cdb interrupt */
+               if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
+                       ata_sff_queue_pio_task(ap, 0);
+               break;
+
+       default:
+               WARN_ON(1);
+               return AC_ERR_SYSTEM;
+       }
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(ata_bmdma_qc_issue);
+
+/**
+ *     ata_bmdma_port_intr - Handle BMDMA port interrupt
+ *     @ap: Port on which interrupt arrived (possibly...)
+ *     @qc: Taskfile currently active in engine
+ *
+ *     Handle port interrupt for given queued command.
+ *
+ *     LOCKING:
+ *     spin_lock_irqsave(host lock)
+ *
+ *     RETURNS:
+ *     One if interrupt was handled, zero if not (shared irq).
+ */
+unsigned int ata_bmdma_port_intr(struct ata_port *ap, struct ata_queued_cmd *qc)
+{
+       struct ata_eh_info *ehi = &ap->link.eh_info;
+       u8 host_stat = 0;
+       bool bmdma_stopped = false;
+       unsigned int handled;
+
+       if (ap->hsm_task_state == HSM_ST_LAST && ata_is_dma(qc->tf.protocol)) {
+               /* check status of DMA engine */
+               host_stat = ap->ops->bmdma_status(ap);
+               VPRINTK("ata%u: host_stat 0x%X\n", ap->print_id, host_stat);
+
+               /* if it's not our irq... */
+               if (!(host_stat & ATA_DMA_INTR))
+                       return ata_sff_idle_irq(ap);
+
+               /* before we do anything else, clear DMA-Start bit */
+               ap->ops->bmdma_stop(qc);
+               bmdma_stopped = true;
+
+               if (unlikely(host_stat & ATA_DMA_ERR)) {
+                       /* error when transfering data to/from memory */
+                       qc->err_mask |= AC_ERR_HOST_BUS;
+                       ap->hsm_task_state = HSM_ST_ERR;
+               }
+       }
+
+       handled = __ata_sff_port_intr(ap, qc, bmdma_stopped);
+
+       if (unlikely(qc->err_mask) && ata_is_dma(qc->tf.protocol))
+               ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
+
+       return handled;
+}
+EXPORT_SYMBOL_GPL(ata_bmdma_port_intr);
+
+/**
+ *     ata_bmdma_interrupt - Default BMDMA ATA host interrupt handler
+ *     @irq: irq line (unused)
+ *     @dev_instance: pointer to our ata_host information structure
+ *
+ *     Default interrupt handler for PCI IDE devices.  Calls
+ *     ata_bmdma_port_intr() for each port that is not disabled.
+ *
+ *     LOCKING:
+ *     Obtains host lock during operation.
+ *
+ *     RETURNS:
+ *     IRQ_NONE or IRQ_HANDLED.
+ */
+irqreturn_t ata_bmdma_interrupt(int irq, void *dev_instance)
+{
+       return __ata_sff_interrupt(irq, dev_instance, ata_bmdma_port_intr);
+}
+EXPORT_SYMBOL_GPL(ata_bmdma_interrupt);
+
+/**
+ *     ata_bmdma_error_handler - Stock error handler for BMDMA controller
+ *     @ap: port to handle error for
+ *
+ *     Stock error handler for BMDMA controller.  It can handle both
+ *     PATA and SATA controllers.  Most BMDMA controllers should be
+ *     able to use this EH as-is or with some added handling before
+ *     and after.
+ *
+ *     LOCKING:
+ *     Kernel thread context (may sleep)
+ */
+void ata_bmdma_error_handler(struct ata_port *ap)
+{
+       struct ata_queued_cmd *qc;
+       unsigned long flags;
+       bool thaw = false;
+
+       qc = __ata_qc_from_tag(ap, ap->link.active_tag);
+       if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
+               qc = NULL;
+
+       /* reset PIO HSM and stop DMA engine */
+       spin_lock_irqsave(ap->lock, flags);
+
+       if (qc && ata_is_dma(qc->tf.protocol)) {
+               u8 host_stat;
+
+               host_stat = ap->ops->bmdma_status(ap);
+
+               /* BMDMA controllers indicate host bus error by
+                * setting DMA_ERR bit and timing out.  As it wasn't
+                * really a timeout event, adjust error mask and
+                * cancel frozen state.
+                */
+               if (qc->err_mask == AC_ERR_TIMEOUT && (host_stat & ATA_DMA_ERR)) {
+                       qc->err_mask = AC_ERR_HOST_BUS;
+                       thaw = true;
+               }
+
+               ap->ops->bmdma_stop(qc);
+
+               /* if we're gonna thaw, make sure IRQ is clear */
+               if (thaw) {
+                       ap->ops->sff_check_status(ap);
+                       if (ap->ops->sff_irq_clear)
+                               ap->ops->sff_irq_clear(ap);
+               }
+       }
+
+       spin_unlock_irqrestore(ap->lock, flags);
+
+       if (thaw)
+               ata_eh_thaw_port(ap);
+
+       ata_sff_error_handler(ap);
+}
+EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
+
+/**
+ *     ata_bmdma_post_internal_cmd - Stock post_internal_cmd for BMDMA
+ *     @qc: internal command to clean up
+ *
+ *     LOCKING:
+ *     Kernel thread context (may sleep)
+ */
+void ata_bmdma_post_internal_cmd(struct ata_queued_cmd *qc)
+{
+       struct ata_port *ap = qc->ap;
+       unsigned long flags;
+
+       if (ata_is_dma(qc->tf.protocol)) {
+               spin_lock_irqsave(ap->lock, flags);
+               ap->ops->bmdma_stop(qc);
+               spin_unlock_irqrestore(ap->lock, flags);
+       }
+}
+EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
+
+/**
+ *     ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
+ *     @ap: Port associated with this ATA transaction.
+ *
+ *     Clear interrupt and error flags in DMA status register.
+ *
+ *     May be used as the irq_clear() entry in ata_port_operations.
+ *
+ *     LOCKING:
+ *     spin_lock_irqsave(host lock)
+ */
+void ata_bmdma_irq_clear(struct ata_port *ap)
+{
+       void __iomem *mmio = ap->ioaddr.bmdma_addr;
+
+       if (!mmio)
+               return;
+
+       iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS);
+}
+EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
+
+/**
+ *     ata_bmdma_setup - Set up PCI IDE BMDMA transaction
+ *     @qc: Info associated with this ATA transaction.
+ *
+ *     LOCKING:
+ *     spin_lock_irqsave(host lock)
+ */
+void ata_bmdma_setup(struct ata_queued_cmd *qc)
+{
+       struct ata_port *ap = qc->ap;
+       unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
+       u8 dmactl;
+
+       /* load PRD table addr. */
+       mb();   /* make sure PRD table writes are visible to controller */
+       iowrite32(ap->bmdma_prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
+
+       /* specify data direction, triple-check start bit is clear */
+       dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
+       dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
+       if (!rw)
+               dmactl |= ATA_DMA_WR;
+       iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
+
+       /* issue r/w command */
+       ap->ops->sff_exec_command(ap, &qc->tf);
+}
+EXPORT_SYMBOL_GPL(ata_bmdma_setup);
+
+/**
+ *     ata_bmdma_start - Start a PCI IDE BMDMA transaction
+ *     @qc: Info associated with this ATA transaction.
+ *
+ *     LOCKING:
+ *     spin_lock_irqsave(host lock)
  */
 void ata_bmdma_start(struct ata_queued_cmd *qc)
 {
@@ -2576,99 +3052,53 @@ u8 ata_bmdma_status(struct ata_port *ap)
 }
 EXPORT_SYMBOL_GPL(ata_bmdma_status);
 
+
 /**
- *     ata_bus_reset - reset host port and associated ATA channel
- *     @ap: port to reset
- *
- *     This is typically the first time we actually start issuing
- *     commands to the ATA channel.  We wait for BSY to clear, then
- *     issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
- *     result.  Determine what devices, if any, are on the channel
- *     by looking at the device 0/1 error register.  Look at the signature
- *     stored in each device's taskfile registers, to determine if
- *     the device is ATA or ATAPI.
+ *     ata_bmdma_port_start - Set port up for bmdma.
+ *     @ap: Port to initialize
  *
- *     LOCKING:
- *     PCI/etc. bus probe sem.
- *     Obtains host lock.
+ *     Called just after data structures for each port are
+ *     initialized.  Allocates space for PRD table.
  *
- *     SIDE EFFECTS:
- *     Sets ATA_FLAG_DISABLED if bus reset fails.
+ *     May be used as the port_start() entry in ata_port_operations.
  *
- *     DEPRECATED:
- *     This function is only for drivers which still use old EH and
- *     will be removed soon.
+ *     LOCKING:
+ *     Inherited from caller.
  */
-void ata_bus_reset(struct ata_port *ap)
-{
-       struct ata_device *device = ap->link.device;
-       struct ata_ioports *ioaddr = &ap->ioaddr;
-       unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
-       u8 err;
-       unsigned int dev0, dev1 = 0, devmask = 0;
-       int rc;
-
-       DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
-
-       /* determine if device 0/1 are present */
-       if (ap->flags & ATA_FLAG_SATA_RESET)
-               dev0 = 1;
-       else {
-               dev0 = ata_devchk(ap, 0);
-               if (slave_possible)
-                       dev1 = ata_devchk(ap, 1);
-       }
-
-       if (dev0)
-               devmask |= (1 << 0);
-       if (dev1)
-               devmask |= (1 << 1);
-
-       /* select device 0 again */
-       ap->ops->sff_dev_select(ap, 0);
-
-       /* issue bus reset */
-       if (ap->flags & ATA_FLAG_SRST) {
-               rc = ata_bus_softreset(ap, devmask,
-                                      ata_deadline(jiffies, 40000));
-               if (rc && rc != -ENODEV)
-                       goto err_out;
-       }
-
-       /*
-        * determine by signature whether we have ATA or ATAPI devices
-        */
-       device[0].class = ata_sff_dev_classify(&device[0], dev0, &err);
-       if ((slave_possible) && (err != 0x81))
-               device[1].class = ata_sff_dev_classify(&device[1], dev1, &err);
-
-       /* is double-select really necessary? */
-       if (device[1].class != ATA_DEV_NONE)
-               ap->ops->sff_dev_select(ap, 1);
-       if (device[0].class != ATA_DEV_NONE)
-               ap->ops->sff_dev_select(ap, 0);
-
-       /* if no devices were detected, disable this port */
-       if ((device[0].class == ATA_DEV_NONE) &&
-           (device[1].class == ATA_DEV_NONE))
-               goto err_out;
-
-       if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
-               /* set up device control for ATA_FLAG_SATA_RESET */
-               iowrite8(ap->ctl, ioaddr->ctl_addr);
-               ap->last_ctl = ap->ctl;
+int ata_bmdma_port_start(struct ata_port *ap)
+{
+       if (ap->mwdma_mask || ap->udma_mask) {
+               ap->bmdma_prd =
+                       dmam_alloc_coherent(ap->host->dev, ATA_PRD_TBL_SZ,
+                                           &ap->bmdma_prd_dma, GFP_KERNEL);
+               if (!ap->bmdma_prd)
+                       return -ENOMEM;
        }
 
-       DPRINTK("EXIT\n");
-       return;
-
-err_out:
-       ata_port_printk(ap, KERN_ERR, "disabling port\n");
-       ata_port_disable(ap);
+       return 0;
+}
+EXPORT_SYMBOL_GPL(ata_bmdma_port_start);
 
-       DPRINTK("EXIT\n");
+/**
+ *     ata_bmdma_port_start32 - Set port up for dma.
+ *     @ap: Port to initialize
+ *
+ *     Called just after data structures for each port are
+ *     initialized.  Enables 32bit PIO and allocates space for PRD
+ *     table.
+ *
+ *     May be used as the port_start() entry in ata_port_operations for
+ *     devices that are capable of 32bit PIO.
+ *
+ *     LOCKING:
+ *     Inherited from caller.
+ */
+int ata_bmdma_port_start32(struct ata_port *ap)
+{
+       ap->pflags |= ATA_PFLAG_PIO32 | ATA_PFLAG_PIO32CHANGE;
+       return ata_bmdma_port_start(ap);
 }
-EXPORT_SYMBOL_GPL(ata_bus_reset);
+EXPORT_SYMBOL_GPL(ata_bmdma_port_start32);
 
 #ifdef CONFIG_PCI
 
@@ -2696,166 +3126,91 @@ int ata_pci_bmdma_clear_simplex(struct pci_dev *pdev)
                return -EOPNOTSUPP;
        return 0;
 }
-EXPORT_SYMBOL_GPL(ata_pci_bmdma_clear_simplex);
-
-/**
- *     ata_pci_bmdma_init - acquire PCI BMDMA resources and init ATA host
- *     @host: target ATA host
- *
- *     Acquire PCI BMDMA resources and initialize @host accordingly.
- *
- *     LOCKING:
- *     Inherited from calling layer (may sleep).
- *
- *     RETURNS:
- *     0 on success, -errno otherwise.
- */
-int ata_pci_bmdma_init(struct ata_host *host)
-{
-       struct device *gdev = host->dev;
-       struct pci_dev *pdev = to_pci_dev(gdev);
-       int i, rc;
-
-       /* No BAR4 allocation: No DMA */
-       if (pci_resource_start(pdev, 4) == 0)
-               return 0;
-
-       /* TODO: If we get no DMA mask we should fall back to PIO */
-       rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
-       if (rc)
-               return rc;
-       rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
-       if (rc)
-               return rc;
-
-       /* request and iomap DMA region */
-       rc = pcim_iomap_regions(pdev, 1 << 4, dev_driver_string(gdev));
-       if (rc) {
-               dev_printk(KERN_ERR, gdev, "failed to request/iomap BAR4\n");
-               return -ENOMEM;
-       }
-       host->iomap = pcim_iomap_table(pdev);
-
-       for (i = 0; i < 2; i++) {
-               struct ata_port *ap = host->ports[i];
-               void __iomem *bmdma = host->iomap[4] + 8 * i;
-
-               if (ata_port_is_dummy(ap))
-                       continue;
-
-               ap->ioaddr.bmdma_addr = bmdma;
-               if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
-                   (ioread8(bmdma + 2) & 0x80))
-                       host->flags |= ATA_HOST_SIMPLEX;
-
-               ata_port_desc(ap, "bmdma 0x%llx",
-                   (unsigned long long)pci_resource_start(pdev, 4) + 8 * i);
-       }
-
-       return 0;
-}
-EXPORT_SYMBOL_GPL(ata_pci_bmdma_init);
+EXPORT_SYMBOL_GPL(ata_pci_bmdma_clear_simplex);
 
-static int ata_resources_present(struct pci_dev *pdev, int port)
+static void ata_bmdma_nodma(struct ata_host *host, const char *reason)
 {
        int i;
 
-       /* Check the PCI resources for this channel are enabled */
-       port = port * 2;
+       dev_printk(KERN_ERR, host->dev, "BMDMA: %s, falling back to PIO\n",
+                  reason);
+
        for (i = 0; i < 2; i++) {
-               if (pci_resource_start(pdev, port + i) == 0 ||
-                   pci_resource_len(pdev, port + i) == 0)
-                       return 0;
+               host->ports[i]->mwdma_mask = 0;
+               host->ports[i]->udma_mask = 0;
        }
-       return 1;
 }
 
 /**
- *     ata_pci_sff_init_host - acquire native PCI ATA resources and init host
+ *     ata_pci_bmdma_init - acquire PCI BMDMA resources and init ATA host
  *     @host: target ATA host
  *
- *     Acquire native PCI ATA resources for @host and initialize the
- *     first two ports of @host accordingly.  Ports marked dummy are
- *     skipped and allocation failure makes the port dummy.
- *
- *     Note that native PCI resources are valid even for legacy hosts
- *     as we fix up pdev resources array early in boot, so this
- *     function can be used for both native and legacy SFF hosts.
+ *     Acquire PCI BMDMA resources and initialize @host accordingly.
  *
  *     LOCKING:
  *     Inherited from calling layer (may sleep).
- *
- *     RETURNS:
- *     0 if at least one port is initialized, -ENODEV if no port is
- *     available.
  */
-int ata_pci_sff_init_host(struct ata_host *host)
+void ata_pci_bmdma_init(struct ata_host *host)
 {
        struct device *gdev = host->dev;
        struct pci_dev *pdev = to_pci_dev(gdev);
-       unsigned int mask = 0;
        int i, rc;
 
-       /* request, iomap BARs and init port addresses accordingly */
+       /* No BAR4 allocation: No DMA */
+       if (pci_resource_start(pdev, 4) == 0) {
+               ata_bmdma_nodma(host, "BAR4 is zero");
+               return;
+       }
+
+       /*
+        * Some controllers require BMDMA region to be initialized
+        * even if DMA is not in use to clear IRQ status via
+        * ->sff_irq_clear method.  Try to initialize bmdma_addr
+        * regardless of dma masks.
+        */
+       rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
+       if (rc)
+               ata_bmdma_nodma(host, "failed to set dma mask");
+       if (!rc) {
+               rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
+               if (rc)
+                       ata_bmdma_nodma(host,
+                                       "failed to set consistent dma mask");
+       }
+
+       /* request and iomap DMA region */
+       rc = pcim_iomap_regions(pdev, 1 << 4, dev_driver_string(gdev));
+       if (rc) {
+               ata_bmdma_nodma(host, "failed to request/iomap BAR4");
+               return;
+       }
+       host->iomap = pcim_iomap_table(pdev);
+
        for (i = 0; i < 2; i++) {
                struct ata_port *ap = host->ports[i];
-               int base = i * 2;
-               void __iomem * const *iomap;
+               void __iomem *bmdma = host->iomap[4] + 8 * i;
 
                if (ata_port_is_dummy(ap))
                        continue;
 
-               /* Discard disabled ports.  Some controllers show
-                * their unused channels this way.  Disabled ports are
-                * made dummy.
-                */
-               if (!ata_resources_present(pdev, i)) {
-                       ap->ops = &ata_dummy_port_ops;
-                       continue;
-               }
-
-               rc = pcim_iomap_regions(pdev, 0x3 << base,
-                                       dev_driver_string(gdev));
-               if (rc) {
-                       dev_printk(KERN_WARNING, gdev,
-                                  "failed to request/iomap BARs for port %d "
-                                  "(errno=%d)\n", i, rc);
-                       if (rc == -EBUSY)
-                               pcim_pin_device(pdev);
-                       ap->ops = &ata_dummy_port_ops;
-                       continue;
-               }
-               host->iomap = iomap = pcim_iomap_table(pdev);
-
-               ap->ioaddr.cmd_addr = iomap[base];
-               ap->ioaddr.altstatus_addr =
-               ap->ioaddr.ctl_addr = (void __iomem *)
-                       ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS);
-               ata_sff_std_ports(&ap->ioaddr);
-
-               ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
-                       (unsigned long long)pci_resource_start(pdev, base),
-                       (unsigned long long)pci_resource_start(pdev, base + 1));
-
-               mask |= 1 << i;
-       }
+               ap->ioaddr.bmdma_addr = bmdma;
+               if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
+                   (ioread8(bmdma + 2) & 0x80))
+                       host->flags |= ATA_HOST_SIMPLEX;
 
-       if (!mask) {
-               dev_printk(KERN_ERR, gdev, "no available native port\n");
-               return -ENODEV;
+               ata_port_desc(ap, "bmdma 0x%llx",
+                   (unsigned long long)pci_resource_start(pdev, 4) + 8 * i);
        }
-
-       return 0;
 }
-EXPORT_SYMBOL_GPL(ata_pci_sff_init_host);
+EXPORT_SYMBOL_GPL(ata_pci_bmdma_init);
 
 /**
- *     ata_pci_sff_prepare_host - helper to prepare native PCI ATA host
+ *     ata_pci_bmdma_prepare_host - helper to prepare PCI BMDMA ATA host
  *     @pdev: target PCI device
  *     @ppi: array of port_info, must be enough for two ports
  *     @r_host: out argument for the initialized ATA host
  *
- *     Helper to allocate ATA host for @pdev, acquire all native PCI
+ *     Helper to allocate BMDMA ATA host for @pdev, acquire all PCI
  *     resources and initialize it accordingly in one go.
  *
  *     LOCKING:
@@ -2864,162 +3219,31 @@ EXPORT_SYMBOL_GPL(ata_pci_sff_init_host);
  *     RETURNS:
  *     0 on success, -errno otherwise.
  */
-int ata_pci_sff_prepare_host(struct pci_dev *pdev,
-                            const struct ata_port_info * const *ppi,
-                            struct ata_host **r_host)
+int ata_pci_bmdma_prepare_host(struct pci_dev *pdev,
+                              const struct ata_port_info * const * ppi,
+                              struct ata_host **r_host)
 {
-       struct ata_host *host;
        int rc;
 
-       if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
-               return -ENOMEM;
-
-       host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
-       if (!host) {
-               dev_printk(KERN_ERR, &pdev->dev,
-                          "failed to allocate ATA host\n");
-               rc = -ENOMEM;
-               goto err_out;
-       }
-
-       rc = ata_pci_sff_init_host(host);
-       if (rc)
-               goto err_out;
-
-       /* init DMA related stuff */
-       rc = ata_pci_bmdma_init(host);
-       if (rc)
-               goto err_bmdma;
-
-       devres_remove_group(&pdev->dev, NULL);
-       *r_host = host;
-       return 0;
-
-err_bmdma:
-       /* This is necessary because PCI and iomap resources are
-        * merged and releasing the top group won't release the
-        * acquired resources if some of those have been acquired
-        * before entering this function.
-        */
-       pcim_iounmap_regions(pdev, 0xf);
-err_out:
-       devres_release_group(&pdev->dev, NULL);
-       return rc;
-}
-EXPORT_SYMBOL_GPL(ata_pci_sff_prepare_host);
-
-/**
- *     ata_pci_sff_activate_host - start SFF host, request IRQ and register it
- *     @host: target SFF ATA host
- *     @irq_handler: irq_handler used when requesting IRQ(s)
- *     @sht: scsi_host_template to use when registering the host
- *
- *     This is the counterpart of ata_host_activate() for SFF ATA
- *     hosts.  This separate helper is necessary because SFF hosts
- *     use two separate interrupts in legacy mode.
- *
- *     LOCKING:
- *     Inherited from calling layer (may sleep).
- *
- *     RETURNS:
- *     0 on success, -errno otherwise.
- */
-int ata_pci_sff_activate_host(struct ata_host *host,
-                             irq_handler_t irq_handler,
-                             struct scsi_host_template *sht)
-{
-       struct device *dev = host->dev;
-       struct pci_dev *pdev = to_pci_dev(dev);
-       const char *drv_name = dev_driver_string(host->dev);
-       int legacy_mode = 0, rc;
-
-       rc = ata_host_start(host);
+       rc = ata_pci_sff_prepare_host(pdev, ppi, r_host);
        if (rc)
                return rc;
 
-       if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
-               u8 tmp8, mask;
-
-               /* TODO: What if one channel is in native mode ... */
-               pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
-               mask = (1 << 2) | (1 << 0);
-               if ((tmp8 & mask) != mask)
-                       legacy_mode = 1;
-#if defined(CONFIG_NO_ATA_LEGACY)
-               /* Some platforms with PCI limits cannot address compat
-                  port space. In that case we punt if their firmware has
-                  left a device in compatibility mode */
-               if (legacy_mode) {
-                       printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
-                       return -EOPNOTSUPP;
-               }
-#endif
-       }
-
-       if (!devres_open_group(dev, NULL, GFP_KERNEL))
-               return -ENOMEM;
-
-       if (!legacy_mode && pdev->irq) {
-               rc = devm_request_irq(dev, pdev->irq, irq_handler,
-                                     IRQF_SHARED, drv_name, host);
-               if (rc)
-                       goto out;
-
-               ata_port_desc(host->ports[0], "irq %d", pdev->irq);
-               ata_port_desc(host->ports[1], "irq %d", pdev->irq);
-       } else if (legacy_mode) {
-               if (!ata_port_is_dummy(host->ports[0])) {
-                       rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev),
-                                             irq_handler, IRQF_SHARED,
-                                             drv_name, host);
-                       if (rc)
-                               goto out;
-
-                       ata_port_desc(host->ports[0], "irq %d",
-                                     ATA_PRIMARY_IRQ(pdev));
-               }
-
-               if (!ata_port_is_dummy(host->ports[1])) {
-                       rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev),
-                                             irq_handler, IRQF_SHARED,
-                                             drv_name, host);
-                       if (rc)
-                               goto out;
-
-                       ata_port_desc(host->ports[1], "irq %d",
-                                     ATA_SECONDARY_IRQ(pdev));
-               }
-       }
-
-       rc = ata_host_register(host, sht);
-out:
-       if (rc == 0)
-               devres_remove_group(dev, NULL);
-       else
-               devres_release_group(dev, NULL);
-
-       return rc;
+       ata_pci_bmdma_init(*r_host);
+       return 0;
 }
-EXPORT_SYMBOL_GPL(ata_pci_sff_activate_host);
+EXPORT_SYMBOL_GPL(ata_pci_bmdma_prepare_host);
 
 /**
- *     ata_pci_sff_init_one - Initialize/register PCI IDE host controller
+ *     ata_pci_bmdma_init_one - Initialize/register BMDMA PCI IDE controller
  *     @pdev: Controller to be initialized
  *     @ppi: array of port_info, must be enough for two ports
  *     @sht: scsi_host_template to use when registering the host
  *     @host_priv: host private_data
+ *     @hflags: host flags
  *
- *     This is a helper function which can be called from a driver's
- *     xxx_init_one() probe function if the hardware uses traditional
- *     IDE taskfile registers.
- *
- *     This function calls pci_enable_device(), reserves its register
- *     regions, sets the dma mask, enables bus master mode, and calls
- *     ata_device_add()
- *
- *     ASSUMPTION:
- *     Nobody makes a single channel controller that appears solely as
- *     the secondary legacy port on PCI.
+ *     This function is similar to ata_pci_sff_init_one() but also
+ *     takes care of BMDMA initialization.
  *
  *     LOCKING:
  *     Inherited from PCI layer (may sleep).
@@ -3027,25 +3251,19 @@ EXPORT_SYMBOL_GPL(ata_pci_sff_activate_host);
  *     RETURNS:
  *     Zero on success, negative on errno-based value on error.
  */
-int ata_pci_sff_init_one(struct pci_dev *pdev,
-                        const struct ata_port_info * const *ppi,
-                        struct scsi_host_template *sht, void *host_priv)
+int ata_pci_bmdma_init_one(struct pci_dev *pdev,
+                          const struct ata_port_info * const * ppi,
+                          struct scsi_host_template *sht, void *host_priv,
+                          int hflags)
 {
        struct device *dev = &pdev->dev;
-       const struct ata_port_info *pi = NULL;
+       const struct ata_port_info *pi;
        struct ata_host *host = NULL;
-       int i, rc;
+       int rc;
 
        DPRINTK("ENTER\n");
 
-       /* look up the first valid port_info */
-       for (i = 0; i < 2 && ppi[i]; i++) {
-               if (ppi[i]->port_ops != &ata_dummy_port_ops) {
-                       pi = ppi[i];
-                       break;
-               }
-       }
-
+       pi = ata_sff_find_valid_pi(ppi);
        if (!pi) {
                dev_printk(KERN_ERR, &pdev->dev,
                           "no valid port_info specified\n");
@@ -3059,15 +3277,16 @@ int ata_pci_sff_init_one(struct pci_dev *pdev,
        if (rc)
                goto out;
 
-       /* prepare and activate SFF host */
-       rc = ata_pci_sff_prepare_host(pdev, ppi, &host);
+       /* prepare and activate BMDMA host */
+       rc = ata_pci_bmdma_prepare_host(pdev, ppi, &host);
        if (rc)
                goto out;
        host->private_data = host_priv;
+       host->flags |= hflags;
 
        pci_set_master(pdev);
-       rc = ata_pci_sff_activate_host(host, ata_sff_interrupt, sht);
-out:
+       rc = ata_pci_sff_activate_host(host, ata_bmdma_interrupt, sht);
+ out:
        if (rc == 0)
                devres_remove_group(&pdev->dev, NULL);
        else
@@ -3075,6 +3294,45 @@ out:
 
        return rc;
 }
-EXPORT_SYMBOL_GPL(ata_pci_sff_init_one);
+EXPORT_SYMBOL_GPL(ata_pci_bmdma_init_one);
 
 #endif /* CONFIG_PCI */
+#endif /* CONFIG_ATA_BMDMA */
+
+/**
+ *     ata_sff_port_init - Initialize SFF/BMDMA ATA port
+ *     @ap: Port to initialize
+ *
+ *     Called on port allocation to initialize SFF/BMDMA specific
+ *     fields.
+ *
+ *     LOCKING:
+ *     None.
+ */
+void ata_sff_port_init(struct ata_port *ap)
+{
+       INIT_DELAYED_WORK(&ap->sff_pio_task, ata_sff_pio_task);
+       ap->ctl = ATA_DEVCTL_OBS;
+       ap->last_ctl = 0xFF;
+}
+
+int __init ata_sff_init(void)
+{
+       /*
+        * FIXME: In UP case, there is only one workqueue thread and if you
+        * have more than one PIO device, latency is bloody awful, with
+        * occasional multi-second "hiccups" as one PIO device waits for
+        * another.  It's an ugly wart that users DO occasionally complain
+        * about; luckily most users have at most one PIO polled device.
+        */
+       ata_sff_wq = create_workqueue("ata_sff");
+       if (!ata_sff_wq)
+               return -ENOMEM;
+
+       return 0;
+}
+
+void __exit ata_sff_exit(void)
+{
+       destroy_workqueue(ata_sff_wq);
+}