libata-sff: separate out BMDMA EH
[cascardo/linux.git] / drivers / ata / libata-sff.c
index e3877b6..aa378c0 100644 (file)
@@ -40,6 +40,8 @@
 
 #include "libata.h"
 
+static struct workqueue_struct *ata_sff_wq;
+
 const struct ata_port_operations ata_sff_port_ops = {
        .inherits               = &ata_base_port_ops,
 
@@ -53,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,
@@ -63,35 +63,13 @@ 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
@@ -445,6 +423,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
@@ -491,7 +490,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))
@@ -517,24 +516,29 @@ 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);
+       if (ap->ops->sff_set_devctl || ioaddr->ctl_addr)
+               ata_sff_set_devctl(ap, ap->ctl);
+       ata_wait_idle(ap);
 
        ap->ops->sff_irq_clear(ap);
-
-       return tmp;
 }
 EXPORT_SYMBOL_GPL(ata_sff_irq_on);
 
@@ -579,7 +583,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)) {
@@ -615,8 +618,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);
 
@@ -894,7 +895,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;
@@ -1165,7 +1166,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);
@@ -1181,7 +1182,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
@@ -1293,7 +1294,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;
@@ -1458,14 +1459,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);
 
@@ -1481,7 +1506,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;
                }
        }
@@ -1551,7 +1576,7 @@ 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);
+                       ata_sff_queue_pio_task(ap, 0);
 
                break;
 
@@ -1573,20 +1598,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.
                         */
                }
 
@@ -1604,7 +1630,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);
+                       ata_sff_queue_pio_task(ap, 0);
                break;
 
        case ATAPI_PROT_DMA:
@@ -1616,7 +1642,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))
-                       ata_pio_queue_task(ap, qc, 0);
+                       ata_sff_queue_pio_task(ap, 0);
                break;
 
        default:
@@ -1728,7 +1754,7 @@ unsigned int ata_sff_host_intr(struct ata_port *ap,
                        goto idle_irq;
        }
 
-       /* ack bmdma irq events */
+       /* clear irq events */
        ap->ops->sff_irq_clear(ap);
 
        ata_sff_hsm_move(ap, qc, status, 0);
@@ -1785,9 +1811,6 @@ retry:
                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) {
                        if (!(qc->tf.flags & ATA_TFLAG_POLLING))
@@ -1862,11 +1885,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 */
@@ -1888,20 +1908,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
@@ -1927,7 +1945,7 @@ 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);
+       ata_sff_irq_on(ap);
 }
 EXPORT_SYMBOL_GPL(ata_sff_thaw);
 
@@ -2301,8 +2319,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;
        }
 }
@@ -2342,7 +2360,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
@@ -2359,62 +2377,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,
@@ -2422,73 +2410,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
@@ -2515,56 +2436,463 @@ 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 native PCI 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
+ *     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.
  */
-void ata_bmdma_start(struct ata_queued_cmd *qc)
-{
+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;
+
+       /* init DMA related stuff */
+       ata_pci_bmdma_init(host);
+
+       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);
+
+/**
+ *     ata_pci_sff_init_one - Initialize/register PCI IDE host 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.
+ *
+ *     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.
+ *
+ *     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 = NULL;
+       struct ata_host *host = NULL;
+       int i, 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;
+               }
+       }
+
+       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;
+
+       pci_set_master(pdev);
+       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 */
+
+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,
+
+       .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_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);
+                       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_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->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)
+{
        struct ata_port *ap = qc->ap;
        u8 dmactl;
 
@@ -2631,507 +2959,193 @@ 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.
- *
- *     LOCKING:
- *     PCI/etc. bus probe sem.
- *     Obtains host lock.
- *
- *     SIDE EFFECTS:
- *     Sets ATA_FLAG_DISABLED if bus reset fails.
- *
- *     DEPRECATED:
- *     This function is only for drivers which still use old EH and
- *     will be removed soon.
- */
-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;
-       }
-
-       DPRINTK("EXIT\n");
-       return;
-
-err_out:
-       ata_port_printk(ap, KERN_ERR, "disabling port\n");
-       ata_port_disable(ap);
-
-       DPRINTK("EXIT\n");
-}
-EXPORT_SYMBOL_GPL(ata_bus_reset);
-
-#ifdef CONFIG_PCI
-
-/**
- *     ata_pci_bmdma_clear_simplex -   attempt to kick device out of simplex
- *     @pdev: PCI device
- *
- *     Some PCI ATA devices report simplex mode but in fact can be told to
- *     enter non simplex mode. This implements the necessary logic to
- *     perform the task on such devices. Calling it on other devices will
- *     have -undefined- behaviour.
- */
-int ata_pci_bmdma_clear_simplex(struct pci_dev *pdev)
-{
-       unsigned long bmdma = pci_resource_start(pdev, 4);
-       u8 simplex;
-
-       if (bmdma == 0)
-               return -ENOENT;
-
-       simplex = inb(bmdma + 0x02);
-       outb(simplex & 0x60, bmdma + 0x02);
-       simplex = inb(bmdma + 0x02);
-       if (simplex & 0x80)
-               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);
-
-static int ata_resources_present(struct pci_dev *pdev, int port)
-{
-       int i;
-
-       /* 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;
-}
-
-/**
- *     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:
- *     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)
-{
-       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 */
-       for (i = 0; i < 2; i++) {
-               struct ata_port *ap = host->ports[i];
-               int base = i * 2;
-               void __iomem * const *iomap;
-
-               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;
-       }
-
-       if (!mask) {
-               dev_printk(KERN_ERR, gdev, "no available native port\n");
-               return -ENODEV;
-       }
-
-       return 0;
-}
-EXPORT_SYMBOL_GPL(ata_pci_sff_init_host);
 
 /**
- *     ata_pci_sff_prepare_host - helper to prepare native PCI 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
+ *     ata_bmdma_port_start - Set port up for bmdma.
+ *     @ap: Port to initialize
  *
- *     Helper to allocate ATA host for @pdev, acquire all native PCI
- *     resources and initialize it accordingly in one go.
+ *     Called just after data structures for each port are
+ *     initialized.  Allocates space for PRD table.
  *
- *     LOCKING:
- *     Inherited from calling layer (may sleep).
+ *     May be used as the port_start() entry in ata_port_operations.
  *
- *     RETURNS:
- *     0 on success, -errno otherwise.
+ *     LOCKING:
+ *     Inherited from caller.
  */
-int ata_pci_sff_prepare_host(struct pci_dev *pdev,
-                            const struct ata_port_info * const *ppi,
-                            struct ata_host **r_host)
+int ata_bmdma_port_start(struct ata_port *ap)
 {
-       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;
+       if (ap->mwdma_mask || ap->udma_mask) {
+               ap->prd = dmam_alloc_coherent(ap->host->dev, ATA_PRD_TBL_SZ,
+                                             &ap->prd_dma, GFP_KERNEL);
+               if (!ap->prd)
+                       return -ENOMEM;
        }
 
-       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);
+EXPORT_SYMBOL_GPL(ata_bmdma_port_start);
 
 /**
- *     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
+ *     ata_bmdma_port_start32 - Set port up for dma.
+ *     @ap: Port to initialize
  *
- *     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.
+ *     Called just after data structures for each port are
+ *     initialized.  Enables 32bit PIO and allocates space for PRD
+ *     table.
  *
- *     LOCKING:
- *     Inherited from calling layer (may sleep).
+ *     May be used as the port_start() entry in ata_port_operations for
+ *     devices that are capable of 32bit PIO.
  *
- *     RETURNS:
- *     0 on success, -errno otherwise.
+ *     LOCKING:
+ *     Inherited from caller.
  */
-int ata_pci_sff_activate_host(struct ata_host *host,
-                             irq_handler_t irq_handler,
-                             struct scsi_host_template *sht)
+int ata_bmdma_port_start32(struct ata_port *ap)
 {
-       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;
+       ap->pflags |= ATA_PFLAG_PIO32 | ATA_PFLAG_PIO32CHANGE;
+       return ata_bmdma_port_start(ap);
+}
+EXPORT_SYMBOL_GPL(ata_bmdma_port_start32);
 
-               /* 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
-       }
+#ifdef CONFIG_PCI
 
-       if (!devres_open_group(dev, NULL, GFP_KERNEL))
-               return -ENOMEM;
+/**
+ *     ata_pci_bmdma_clear_simplex -   attempt to kick device out of simplex
+ *     @pdev: PCI device
+ *
+ *     Some PCI ATA devices report simplex mode but in fact can be told to
+ *     enter non simplex mode. This implements the necessary logic to
+ *     perform the task on such devices. Calling it on other devices will
+ *     have -undefined- behaviour.
+ */
+int ata_pci_bmdma_clear_simplex(struct pci_dev *pdev)
+{
+       unsigned long bmdma = pci_resource_start(pdev, 4);
+       u8 simplex;
 
-       if (!legacy_mode && pdev->irq) {
-               rc = devm_request_irq(dev, pdev->irq, irq_handler,
-                                     IRQF_SHARED, drv_name, host);
-               if (rc)
-                       goto out;
+       if (bmdma == 0)
+               return -ENOENT;
 
-               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;
+       simplex = inb(bmdma + 0x02);
+       outb(simplex & 0x60, bmdma + 0x02);
+       simplex = inb(bmdma + 0x02);
+       if (simplex & 0x80)
+               return -EOPNOTSUPP;
+       return 0;
+}
+EXPORT_SYMBOL_GPL(ata_pci_bmdma_clear_simplex);
 
-                       ata_port_desc(host->ports[0], "irq %d",
-                                     ATA_PRIMARY_IRQ(pdev));
-               }
+static void ata_bmdma_nodma(struct ata_host *host, const char *reason)
+{
+       int i;
 
-               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;
+       dev_printk(KERN_ERR, host->dev, "BMDMA: %s, falling back to PIO\n",
+                  reason);
 
-                       ata_port_desc(host->ports[1], "irq %d",
-                                     ATA_SECONDARY_IRQ(pdev));
-               }
+       for (i = 0; i < 2; i++) {
+               host->ports[i]->mwdma_mask = 0;
+               host->ports[i]->udma_mask = 0;
        }
-
-       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);
 
 /**
- *     ata_pci_sff_init_one - Initialize/register PCI IDE host 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.
- *
- *     This function calls pci_enable_device(), reserves its register
- *     regions, sets the dma mask, enables bus master mode, and calls
- *     ata_device_add()
+ *     ata_pci_bmdma_init - acquire PCI BMDMA resources and init ATA host
+ *     @host: target ATA host
  *
- *     ASSUMPTION:
- *     Nobody makes a single channel controller that appears solely as
- *     the secondary legacy port on PCI.
+ *     Acquire PCI BMDMA resources and initialize @host accordingly.
  *
  *     LOCKING:
- *     Inherited from PCI layer (may sleep).
- *
- *     RETURNS:
- *     Zero on success, negative on errno-based value on error.
+ *     Inherited from calling layer (may sleep).
  */
-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)
+void ata_pci_bmdma_init(struct ata_host *host)
 {
-       struct device *dev = &pdev->dev;
-       const struct ata_port_info *pi = NULL;
-       struct ata_host *host = NULL;
+       struct device *gdev = host->dev;
+       struct pci_dev *pdev = to_pci_dev(gdev);
        int i, 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;
-               }
+       /* No BAR4 allocation: No DMA */
+       if (pci_resource_start(pdev, 4) == 0) {
+               ata_bmdma_nodma(host, "BAR4 is zero");
+               return;
        }
 
-       if (!pi) {
-               dev_printk(KERN_ERR, &pdev->dev,
-                          "no valid port_info specified\n");
-               return -EINVAL;
+       /*
+        * 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");
        }
 
-       if (!devres_open_group(dev, NULL, GFP_KERNEL))
-               return -ENOMEM;
+       /* 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);
 
-       rc = pcim_enable_device(pdev);
-       if (rc)
-               goto out;
+       for (i = 0; i < 2; i++) {
+               struct ata_port *ap = host->ports[i];
+               void __iomem *bmdma = host->iomap[4] + 8 * i;
 
-       /* 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;
+               if (ata_port_is_dummy(ap))
+                       continue;
 
-       pci_set_master(pdev);
-       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);
+               ap->ioaddr.bmdma_addr = bmdma;
+               if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
+                   (ioread8(bmdma + 2) & 0x80))
+                       host->flags |= ATA_HOST_SIMPLEX;
 
-       return rc;
+               ata_port_desc(ap, "bmdma 0x%llx",
+                   (unsigned long long)pci_resource_start(pdev, 4) + 8 * i);
+       }
 }
-EXPORT_SYMBOL_GPL(ata_pci_sff_init_one);
+EXPORT_SYMBOL_GPL(ata_pci_bmdma_init);
 
 #endif /* CONFIG_PCI */
+
+/**
+ *     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);
+}