libata: factor out ata_pci_activate_sff_host() from ata_pci_one()
[safe/jmp/linux-2.6] / drivers / ata / libata-sff.c
index 12c88c5..60cd4b1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  libata-bmdma.c - helper library for PCI IDE BMDMA
+ *  libata-sff.c - helper library for PCI IDE BMDMA
  *
  *  Maintained by:  Jeff Garzik <jgarzik@pobox.com>
  *                 Please ALWAYS copy linux-ide@vger.kernel.org
@@ -56,10 +56,7 @@ u8 ata_irq_on(struct ata_port *ap)
        ap->ctl &= ~ATA_NIEN;
        ap->last_ctl = ap->ctl;
 
-       if (ap->flags & ATA_FLAG_MMIO)
-               writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
-       else
-               outb(ap->ctl, ioaddr->ctl_addr);
+       iowrite8(ap->ctl, ioaddr->ctl_addr);
        tmp = ata_wait_idle(ap);
 
        ap->ops->irq_clear(ap);
@@ -68,7 +65,7 @@ u8 ata_irq_on(struct ata_port *ap)
 }
 
 /**
- *     ata_tf_load_pio - send taskfile registers to host controller
+ *     ata_tf_load - send taskfile registers to host controller
  *     @ap: Port to which output is sent
  *     @tf: ATA taskfile register set
  *
@@ -78,81 +75,23 @@ u8 ata_irq_on(struct ata_port *ap)
  *     Inherited from caller.
  */
 
-static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
-{
-       struct ata_ioports *ioaddr = &ap->ioaddr;
-       unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
-
-       if (tf->ctl != ap->last_ctl) {
-               outb(tf->ctl, ioaddr->ctl_addr);
-               ap->last_ctl = tf->ctl;
-               ata_wait_idle(ap);
-       }
-
-       if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
-               outb(tf->hob_feature, ioaddr->feature_addr);
-               outb(tf->hob_nsect, ioaddr->nsect_addr);
-               outb(tf->hob_lbal, ioaddr->lbal_addr);
-               outb(tf->hob_lbam, ioaddr->lbam_addr);
-               outb(tf->hob_lbah, ioaddr->lbah_addr);
-               VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
-                       tf->hob_feature,
-                       tf->hob_nsect,
-                       tf->hob_lbal,
-                       tf->hob_lbam,
-                       tf->hob_lbah);
-       }
-
-       if (is_addr) {
-               outb(tf->feature, ioaddr->feature_addr);
-               outb(tf->nsect, ioaddr->nsect_addr);
-               outb(tf->lbal, ioaddr->lbal_addr);
-               outb(tf->lbam, ioaddr->lbam_addr);
-               outb(tf->lbah, ioaddr->lbah_addr);
-               VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
-                       tf->feature,
-                       tf->nsect,
-                       tf->lbal,
-                       tf->lbam,
-                       tf->lbah);
-       }
-
-       if (tf->flags & ATA_TFLAG_DEVICE) {
-               outb(tf->device, ioaddr->device_addr);
-               VPRINTK("device 0x%X\n", tf->device);
-       }
-
-       ata_wait_idle(ap);
-}
-
-/**
- *     ata_tf_load_mmio - send taskfile registers to host controller
- *     @ap: Port to which output is sent
- *     @tf: ATA taskfile register set
- *
- *     Outputs ATA taskfile to standard ATA host controller using MMIO.
- *
- *     LOCKING:
- *     Inherited from caller.
- */
-
-static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
+void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
 {
        struct ata_ioports *ioaddr = &ap->ioaddr;
        unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
 
        if (tf->ctl != ap->last_ctl) {
-               writeb(tf->ctl, (void __iomem *) ap->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)) {
-               writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
-               writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
-               writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
-               writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
-               writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
+               iowrite8(tf->hob_feature, ioaddr->feature_addr);
+               iowrite8(tf->hob_nsect, ioaddr->nsect_addr);
+               iowrite8(tf->hob_lbal, ioaddr->lbal_addr);
+               iowrite8(tf->hob_lbam, ioaddr->lbam_addr);
+               iowrite8(tf->hob_lbah, ioaddr->lbah_addr);
                VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
                        tf->hob_feature,
                        tf->hob_nsect,
@@ -162,11 +101,11 @@ static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
        }
 
        if (is_addr) {
-               writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
-               writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
-               writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
-               writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
-               writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
+               iowrite8(tf->feature, ioaddr->feature_addr);
+               iowrite8(tf->nsect, ioaddr->nsect_addr);
+               iowrite8(tf->lbal, ioaddr->lbal_addr);
+               iowrite8(tf->lbam, ioaddr->lbam_addr);
+               iowrite8(tf->lbah, ioaddr->lbah_addr);
                VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
                        tf->feature,
                        tf->nsect,
@@ -176,232 +115,69 @@ static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
        }
 
        if (tf->flags & ATA_TFLAG_DEVICE) {
-               writeb(tf->device, (void __iomem *) ioaddr->device_addr);
+               iowrite8(tf->device, ioaddr->device_addr);
                VPRINTK("device 0x%X\n", tf->device);
        }
 
        ata_wait_idle(ap);
 }
 
-
-/**
- *     ata_tf_load - send taskfile registers to host controller
- *     @ap: Port to which output is sent
- *     @tf: ATA taskfile register set
- *
- *     Outputs ATA taskfile to standard ATA host controller using MMIO
- *     or PIO as indicated by the ATA_FLAG_MMIO flag.
- *     Writes the control, feature, nsect, lbal, lbam, and lbah registers.
- *     Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
- *     hob_lbal, hob_lbam, and hob_lbah.
- *
- *     This function waits for idle (!BUSY and !DRQ) after writing
- *     registers.  If the control register has a new value, this
- *     function also waits for idle after writing control and before
- *     writing the remaining registers.
- *
- *     May be used as the tf_load() entry in ata_port_operations.
- *
- *     LOCKING:
- *     Inherited from caller.
- */
-void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
-{
-       if (ap->flags & ATA_FLAG_MMIO)
-               ata_tf_load_mmio(ap, tf);
-       else
-               ata_tf_load_pio(ap, tf);
-}
-
-/**
- *     ata_exec_command_pio - issue ATA command to host controller
- *     @ap: port to which command is being issued
- *     @tf: ATA taskfile register set
- *
- *     Issues PIO write to ATA command register, with proper
- *     synchronization with interrupt handler / other threads.
- *
- *     LOCKING:
- *     spin_lock_irqsave(host lock)
- */
-
-static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
-{
-       DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
-
-               outb(tf->command, ap->ioaddr.command_addr);
-       ata_pause(ap);
-}
-
-
-/**
- *     ata_exec_command_mmio - issue ATA command to host controller
- *     @ap: port to which command is being issued
- *     @tf: ATA taskfile register set
- *
- *     Issues MMIO write to ATA command register, with proper
- *     synchronization with interrupt handler / other threads.
- *
- *     FIXME: missing write posting for 400nS delay enforcement
- *
- *     LOCKING:
- *     spin_lock_irqsave(host lock)
- */
-
-static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
-{
-       DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
-
-               writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
-       ata_pause(ap);
-}
-
-
 /**
  *     ata_exec_command - issue ATA command to host controller
  *     @ap: port to which command is being issued
  *     @tf: ATA taskfile register set
  *
- *     Issues PIO/MMIO write to ATA command register, with proper
- *     synchronization with interrupt handler / other threads.
+ *     Issues ATA command, with proper synchronization with interrupt
+ *     handler / other threads.
  *
  *     LOCKING:
  *     spin_lock_irqsave(host lock)
  */
 void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
 {
-       if (ap->flags & ATA_FLAG_MMIO)
-               ata_exec_command_mmio(ap, tf);
-       else
-               ata_exec_command_pio(ap, tf);
-}
+       DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
 
-/**
- *     ata_tf_read_pio - input device's ATA taskfile shadow registers
- *     @ap: Port from which input is read
- *     @tf: ATA taskfile register set for storing input
- *
- *     Reads ATA taskfile registers for currently-selected device
- *     into @tf.
- *
- *     LOCKING:
- *     Inherited from caller.
- */
-
-static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
-{
-       struct ata_ioports *ioaddr = &ap->ioaddr;
-
-       tf->command = ata_check_status(ap);
-       tf->feature = inb(ioaddr->error_addr);
-       tf->nsect = inb(ioaddr->nsect_addr);
-       tf->lbal = inb(ioaddr->lbal_addr);
-       tf->lbam = inb(ioaddr->lbam_addr);
-       tf->lbah = inb(ioaddr->lbah_addr);
-       tf->device = inb(ioaddr->device_addr);
-
-       if (tf->flags & ATA_TFLAG_LBA48) {
-               outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
-               tf->hob_feature = inb(ioaddr->error_addr);
-               tf->hob_nsect = inb(ioaddr->nsect_addr);
-               tf->hob_lbal = inb(ioaddr->lbal_addr);
-               tf->hob_lbam = inb(ioaddr->lbam_addr);
-               tf->hob_lbah = inb(ioaddr->lbah_addr);
-       }
+       iowrite8(tf->command, ap->ioaddr.command_addr);
+       ata_pause(ap);
 }
 
 /**
- *     ata_tf_read_mmio - input device's ATA taskfile shadow registers
+ *     ata_tf_read - input device's ATA taskfile shadow registers
  *     @ap: Port from which input is read
  *     @tf: ATA taskfile register set for storing input
  *
  *     Reads ATA taskfile registers for currently-selected device
- *     into @tf via MMIO.
+ *     into @tf. Assumes the device has a fully SFF compliant task file
+ *     layout and behaviour. If you device does not (eg has a different
+ *     status method) then you will need to provide a replacement tf_read
  *
  *     LOCKING:
  *     Inherited from caller.
  */
-
-static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
+void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
 {
        struct ata_ioports *ioaddr = &ap->ioaddr;
 
        tf->command = ata_check_status(ap);
-       tf->feature = readb((void __iomem *)ioaddr->error_addr);
-       tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
-       tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
-       tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
-       tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
-       tf->device = readb((void __iomem *)ioaddr->device_addr);
+       tf->feature = ioread8(ioaddr->error_addr);
+       tf->nsect = ioread8(ioaddr->nsect_addr);
+       tf->lbal = ioread8(ioaddr->lbal_addr);
+       tf->lbam = ioread8(ioaddr->lbam_addr);
+       tf->lbah = ioread8(ioaddr->lbah_addr);
+       tf->device = ioread8(ioaddr->device_addr);
 
        if (tf->flags & ATA_TFLAG_LBA48) {
-               writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
-               tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
-               tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
-               tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
-               tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
-               tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
+               iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
+               tf->hob_feature = ioread8(ioaddr->error_addr);
+               tf->hob_nsect = ioread8(ioaddr->nsect_addr);
+               tf->hob_lbal = ioread8(ioaddr->lbal_addr);
+               tf->hob_lbam = ioread8(ioaddr->lbam_addr);
+               tf->hob_lbah = ioread8(ioaddr->lbah_addr);
+               iowrite8(tf->ctl, ioaddr->ctl_addr);
+               ap->last_ctl = tf->ctl;
        }
 }
 
-
-/**
- *     ata_tf_read - input device's ATA taskfile shadow registers
- *     @ap: Port from which input is read
- *     @tf: ATA taskfile register set for storing input
- *
- *     Reads ATA taskfile registers for currently-selected device
- *     into @tf.
- *
- *     Reads nsect, lbal, lbam, lbah, and device.  If ATA_TFLAG_LBA48
- *     is set, also reads the hob registers.
- *
- *     May be used as the tf_read() entry in ata_port_operations.
- *
- *     LOCKING:
- *     Inherited from caller.
- */
-void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
-{
-       if (ap->flags & ATA_FLAG_MMIO)
-               ata_tf_read_mmio(ap, tf);
-       else
-               ata_tf_read_pio(ap, tf);
-}
-
-/**
- *     ata_check_status_pio - Read device status reg & clear interrupt
- *     @ap: port where the device is
- *
- *     Reads ATA taskfile status register for currently-selected device
- *     and return its value. This also clears pending interrupts
- *      from this device
- *
- *     LOCKING:
- *     Inherited from caller.
- */
-static u8 ata_check_status_pio(struct ata_port *ap)
-{
-       return inb(ap->ioaddr.status_addr);
-}
-
-/**
- *     ata_check_status_mmio - Read device status reg & clear interrupt
- *     @ap: port where the device is
- *
- *     Reads ATA taskfile status register for currently-selected device
- *     via MMIO and return its value. This also clears pending interrupts
- *      from this device
- *
- *     LOCKING:
- *     Inherited from caller.
- */
-static u8 ata_check_status_mmio(struct ata_port *ap)
-{
-               return readb((void __iomem *) ap->ioaddr.status_addr);
-}
-
-
 /**
  *     ata_check_status - Read device status reg & clear interrupt
  *     @ap: port where the device is
@@ -410,19 +186,14 @@ static u8 ata_check_status_mmio(struct ata_port *ap)
  *     and return its value. This also clears pending interrupts
  *      from this device
  *
- *     May be used as the check_status() entry in ata_port_operations.
- *
  *     LOCKING:
  *     Inherited from caller.
  */
 u8 ata_check_status(struct ata_port *ap)
 {
-       if (ap->flags & ATA_FLAG_MMIO)
-               return ata_check_status_mmio(ap);
-       return ata_check_status_pio(ap);
+       return ioread8(ap->ioaddr.status_addr);
 }
 
-
 /**
  *     ata_altstatus - Read device alternate status reg
  *     @ap: port where the device is
@@ -441,60 +212,54 @@ u8 ata_altstatus(struct ata_port *ap)
        if (ap->ops->check_altstatus)
                return ap->ops->check_altstatus(ap);
 
-       if (ap->flags & ATA_FLAG_MMIO)
-               return readb((void __iomem *)ap->ioaddr.altstatus_addr);
-       return inb(ap->ioaddr.altstatus_addr);
+       return ioread8(ap->ioaddr.altstatus_addr);
 }
 
 /**
- *     ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
+ *     ata_bmdma_setup - Set up PCI IDE BMDMA transaction
  *     @qc: Info associated with this ATA transaction.
  *
  *     LOCKING:
  *     spin_lock_irqsave(host lock)
  */
-
-static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
+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;
-       void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
 
        /* load PRD table addr. */
        mb();   /* make sure PRD table writes are visible to controller */
-       writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
+       iowrite32(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
 
        /* specify data direction, triple-check start bit is clear */
-       dmactl = readb(mmio + ATA_DMA_CMD);
+       dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
        dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
        if (!rw)
                dmactl |= ATA_DMA_WR;
-       writeb(dmactl, mmio + ATA_DMA_CMD);
+       iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
 
        /* issue r/w command */
        ap->ops->exec_command(ap, &qc->tf);
 }
 
 /**
- *     ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
+ *     ata_bmdma_start - Start a PCI IDE BMDMA transaction
  *     @qc: Info associated with this ATA transaction.
  *
  *     LOCKING:
  *     spin_lock_irqsave(host lock)
  */
-
-static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
+void ata_bmdma_start(struct ata_queued_cmd *qc)
 {
        struct ata_port *ap = qc->ap;
-       void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
        u8 dmactl;
 
        /* start host DMA transaction */
-       dmactl = readb(mmio + ATA_DMA_CMD);
-       writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
+       dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
+       iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
 
-       /* Strictly, one may wish to issue a readb() here, to
+       /* Strictly, one may wish to issue an ioread8() here, to
         * flush the mmio write.  However, control also passes
         * to the hardware at this point, and it will interrupt
         * us when we are to resume control.  So, in effect,
@@ -504,100 +269,13 @@ static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
         * is expected, so I think it is best to not add a readb()
         * without first all the MMIO ATA cards/mobos.
         * Or maybe I'm just being paranoid.
+        *
+        * FIXME: The posting of this write means I/O starts are
+        * unneccessarily delayed for MMIO
         */
 }
 
 /**
- *     ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
- *     @qc: Info associated with this ATA transaction.
- *
- *     LOCKING:
- *     spin_lock_irqsave(host lock)
- */
-
-static void ata_bmdma_setup_pio (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. */
-       outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
-
-       /* specify data direction, triple-check start bit is clear */
-       dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
-       dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
-       if (!rw)
-               dmactl |= ATA_DMA_WR;
-       outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
-
-       /* issue r/w command */
-       ap->ops->exec_command(ap, &qc->tf);
-}
-
-/**
- *     ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
- *     @qc: Info associated with this ATA transaction.
- *
- *     LOCKING:
- *     spin_lock_irqsave(host lock)
- */
-
-static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
-{
-       struct ata_port *ap = qc->ap;
-       u8 dmactl;
-
-       /* start host DMA transaction */
-       dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
-       outb(dmactl | ATA_DMA_START,
-            ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
-}
-
-
-/**
- *     ata_bmdma_start - Start a PCI IDE BMDMA transaction
- *     @qc: Info associated with this ATA transaction.
- *
- *     Writes the ATA_DMA_START flag to the DMA command register.
- *
- *     May be used as the bmdma_start() entry in ata_port_operations.
- *
- *     LOCKING:
- *     spin_lock_irqsave(host lock)
- */
-void ata_bmdma_start(struct ata_queued_cmd *qc)
-{
-       if (qc->ap->flags & ATA_FLAG_MMIO)
-               ata_bmdma_start_mmio(qc);
-       else
-               ata_bmdma_start_pio(qc);
-}
-
-
-/**
- *     ata_bmdma_setup - Set up PCI IDE BMDMA transaction
- *     @qc: Info associated with this ATA transaction.
- *
- *     Writes address of PRD table to device's PRD Table Address
- *     register, sets the DMA control register, and calls
- *     ops->exec_command() to start the transfer.
- *
- *     May be used as the bmdma_setup() entry in ata_port_operations.
- *
- *     LOCKING:
- *     spin_lock_irqsave(host lock)
- */
-void ata_bmdma_setup(struct ata_queued_cmd *qc)
-{
-       if (qc->ap->flags & ATA_FLAG_MMIO)
-               ata_bmdma_setup_mmio(qc);
-       else
-               ata_bmdma_setup_pio(qc);
-}
-
-
-/**
  *     ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
  *     @ap: Port associated with this ATA transaction.
  *
@@ -608,23 +286,16 @@ void ata_bmdma_setup(struct ata_queued_cmd *qc)
  *     LOCKING:
  *     spin_lock_irqsave(host lock)
  */
-
 void ata_bmdma_irq_clear(struct ata_port *ap)
 {
-       if (!ap->ioaddr.bmdma_addr)
+       void __iomem *mmio = ap->ioaddr.bmdma_addr;
+
+       if (!mmio)
                return;
 
-       if (ap->flags & ATA_FLAG_MMIO) {
-               void __iomem *mmio =
-                     ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
-               writeb(readb(mmio), mmio);
-       } else {
-               unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
-               outb(inb(addr), addr);
-       }
+       iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS);
 }
 
-
 /**
  *     ata_bmdma_status - Read PCI IDE BMDMA status
  *     @ap: Port associated with this ATA transaction.
@@ -636,19 +307,11 @@ void ata_bmdma_irq_clear(struct ata_port *ap)
  *     LOCKING:
  *     spin_lock_irqsave(host lock)
  */
-
 u8 ata_bmdma_status(struct ata_port *ap)
 {
-       u8 host_stat;
-       if (ap->flags & ATA_FLAG_MMIO) {
-               void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
-               host_stat = readb(mmio + ATA_DMA_STATUS);
-       } else
-               host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
-       return host_stat;
+       return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
 }
 
-
 /**
  *     ata_bmdma_stop - Stop PCI IDE BMDMA transfer
  *     @qc: Command we are ending DMA for
@@ -660,21 +323,14 @@ u8 ata_bmdma_status(struct ata_port *ap)
  *     LOCKING:
  *     spin_lock_irqsave(host lock)
  */
-
 void ata_bmdma_stop(struct ata_queued_cmd *qc)
 {
        struct ata_port *ap = qc->ap;
-       if (ap->flags & ATA_FLAG_MMIO) {
-               void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
-
-               /* clear start/stop bit */
-               writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
-                       mmio + ATA_DMA_CMD);
-       } else {
-               /* clear start/stop bit */
-               outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
-                       ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
-       }
+       void __iomem *mmio = ap->ioaddr.bmdma_addr;
+
+       /* clear start/stop bit */
+       iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
+                mmio + ATA_DMA_CMD);
 
        /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
        ata_altstatus(ap);        /* dummy read */
@@ -696,10 +352,7 @@ void ata_bmdma_freeze(struct ata_port *ap)
        ap->ctl |= ATA_NIEN;
        ap->last_ctl = ap->ctl;
 
-       if (ap->flags & ATA_FLAG_MMIO)
-               writeb(ap->ctl, (void __iomem *)ioaddr->ctl_addr);
-       else
-               outb(ap->ctl, ioaddr->ctl_addr);
+       iowrite8(ap->ctl, ioaddr->ctl_addr);
 
        /* Under certain circumstances, some controllers raise IRQ on
         * ATA_NIEN manipulation.  Also, many controllers fail to mask
@@ -724,8 +377,7 @@ void ata_bmdma_thaw(struct ata_port *ap)
        /* clear & re-enable interrupts */
        ata_chk_status(ap);
        ap->ops->irq_clear(ap);
-       if (ap->ioaddr.ctl_addr)        /* FIXME: hack. create a hook instead */
-               ata_irq_on(ap);
+       ap->ops->irq_on(ap);
 }
 
 /**
@@ -755,7 +407,7 @@ void ata_bmdma_drive_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
        unsigned long flags;
        int thaw = 0;
 
-       qc = __ata_qc_from_tag(ap, ap->active_tag);
+       qc = __ata_qc_from_tag(ap, ap->link.active_tag);
        if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
                qc = NULL;
 
@@ -765,7 +417,7 @@ void ata_bmdma_drive_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
        ap->hsm_task_state = HSM_ST_IDLE;
 
        if (qc && (qc->tf.protocol == ATA_PROT_DMA ||
-                  qc->tf.protocol == ATA_PROT_ATAPI_DMA)) {
+                  qc->tf.protocol == ATAPI_PROT_DMA)) {
                u8 host_stat;
 
                host_stat = ap->ops->bmdma_status(ap);
@@ -775,7 +427,7 @@ void ata_bmdma_drive_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
                 * really a timeout event, adjust error mask and
                 * cancel frozen state.
                 */
-               if (qc->err_mask == AC_ERR_TIMEOUT && host_stat & ATA_DMA_ERR) {
+               if (qc->err_mask == AC_ERR_TIMEOUT && (host_stat & ATA_DMA_ERR)) {
                        qc->err_mask = AC_ERR_HOST_BUS;
                        thaw = 1;
                }
@@ -810,7 +462,7 @@ void ata_bmdma_error_handler(struct ata_port *ap)
        ata_reset_fn_t hardreset;
 
        hardreset = NULL;
-       if (sata_scr_valid(ap))
+       if (sata_scr_valid(&ap->link))
                hardreset = sata_std_hardreset;
 
        ata_bmdma_drive_eh(ap, ata_std_prereset, ata_std_softreset, hardreset,
@@ -831,196 +483,273 @@ void ata_bmdma_post_internal_cmd(struct ata_queued_cmd *qc)
                ata_bmdma_stop(qc);
 }
 
-#ifdef CONFIG_PCI
 /**
- *     ata_pci_init_native_mode - Initialize native-mode driver
- *     @pdev:  pci device to be initialized
- *     @port:  array[2] of pointers to port info structures.
- *     @ports: bitmap of ports present
- *
- *     Utility function which allocates and initializes an
- *     ata_probe_ent structure for a standard dual-port
- *     PIO-based IDE controller.  The returned ata_probe_ent
- *     structure can be passed to ata_device_add().  The returned
- *     ata_probe_ent structure should then be freed with kfree().
- *
- *     The caller need only pass the address of the primary port, the
- *     secondary will be deduced automatically. If the device has non
- *     standard secondary port mappings this function can be called twice,
- *     once for each interface.
+ *     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.
  */
 
-struct ata_probe_ent *
-ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
+int ata_sff_port_start(struct ata_port *ap)
 {
-       struct ata_probe_ent *probe_ent =
-               ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
-       int p = 0;
-       unsigned long bmdma;
-
-       if (!probe_ent)
-               return NULL;
-
-       probe_ent->irq = pdev->irq;
-       probe_ent->irq_flags = IRQF_SHARED;
-
-       if (ports & ATA_PORT_PRIMARY) {
-               probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
-               probe_ent->port[p].altstatus_addr =
-               probe_ent->port[p].ctl_addr =
-                       pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
-               bmdma = pci_resource_start(pdev, 4);
-               if (bmdma) {
-                       if ((!(port[p]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
-                           (inb(bmdma + 2) & 0x80))
-                               probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
-                       probe_ent->port[p].bmdma_addr = bmdma;
-               }
-               ata_std_ports(&probe_ent->port[p]);
-               p++;
+       if (ap->ioaddr.bmdma_addr)
+               return ata_port_start(ap);
+       return 0;
+}
+
+#ifdef CONFIG_PCI
+
+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;
+}
 
-       if (ports & ATA_PORT_SECONDARY) {
-               probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
-               probe_ent->port[p].altstatus_addr =
-               probe_ent->port[p].ctl_addr =
-                       pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
-               bmdma = pci_resource_start(pdev, 4);
-               if (bmdma) {
-                       bmdma += 8;
-                       if ((!(port[p]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
-                           (inb(bmdma + 2) & 0x80))
-                               probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
-                       probe_ent->port[p].bmdma_addr = bmdma;
-               }
-               ata_std_ports(&probe_ent->port[p]);
-               probe_ent->pinfo2 = port[1];
-               p++;
+/**
+ *     ata_pci_init_bmdma - 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_init_bmdma(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);
 
-       probe_ent->n_ports = p;
-       return probe_ent;
-}
+       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;
 
-static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev,
-                               struct ata_port_info **port, int port_mask)
+               ata_port_desc(ap, "bmdma 0x%llx",
+                       (unsigned long long)pci_resource_start(pdev, 4) + 8 * i);
+       }
+
+       return 0;
+}
+
+/**
+ *     ata_pci_init_sff_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_init_sff_host(struct ata_host *host)
 {
-       struct ata_probe_ent *probe_ent;
-       unsigned long bmdma = pci_resource_start(pdev, 4);
+       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;
 
-       probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
-       if (!probe_ent)
-               return NULL;
-
-       probe_ent->n_ports = 2;
-       probe_ent->irq_flags = IRQF_SHARED;
-
-       if (port_mask & ATA_PORT_PRIMARY) {
-               probe_ent->irq = ATA_PRIMARY_IRQ(pdev);
-               probe_ent->port[0].cmd_addr = ATA_PRIMARY_CMD;
-               probe_ent->port[0].altstatus_addr =
-               probe_ent->port[0].ctl_addr = ATA_PRIMARY_CTL;
-               if (bmdma) {
-                       probe_ent->port[0].bmdma_addr = bmdma;
-                       if ((!(port[0]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
-                           (inb(bmdma + 2) & 0x80))
-                               probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
+               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;
                }
-               ata_std_ports(&probe_ent->port[0]);
-       } else
-               probe_ent->dummy_port_mask |= ATA_PORT_PRIMARY;
-
-       if (port_mask & ATA_PORT_SECONDARY) {
-               if (probe_ent->irq)
-                       probe_ent->irq2 = ATA_SECONDARY_IRQ(pdev);
-               else
-                       probe_ent->irq = ATA_SECONDARY_IRQ(pdev);
-               probe_ent->port[1].cmd_addr = ATA_SECONDARY_CMD;
-               probe_ent->port[1].altstatus_addr =
-               probe_ent->port[1].ctl_addr = ATA_SECONDARY_CTL;
-               if (bmdma) {
-                       probe_ent->port[1].bmdma_addr = bmdma + 8;
-                       if ((!(port[1]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
-                           (inb(bmdma + 10) & 0x80))
-                               probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
+
+               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;
                }
-               ata_std_ports(&probe_ent->port[1]);
+               host->iomap = iomap = pcim_iomap_table(pdev);
 
-               /* FIXME: could be pointing to stack area; must copy */
-               probe_ent->pinfo2 = port[1];
-       } else
-               probe_ent->dummy_port_mask |= ATA_PORT_SECONDARY;
+               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_std_ports(&ap->ioaddr);
 
-       return probe_ent;
-}
+               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;
+}
 
 /**
- *     ata_pci_init_one - Initialize/register PCI IDE host controller
- *     @pdev: Controller to be initialized
- *     @port_info: Information from low-level host driver
- *     @n_ports: Number of ports attached to host controller
+ *     ata_pci_prepare_sff_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
  *
- *     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.
+ *     Helper to allocate ATA host for @pdev, acquire all native PCI
+ *     resources and initialize it accordingly in one go.
  *
  *     LOCKING:
- *     Inherited from PCI layer (may sleep).
+ *     Inherited from calling layer (may sleep).
  *
  *     RETURNS:
- *     Zero on success, negative on errno-based value on error.
+ *     0 on success, -errno otherwise.
  */
-
-int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
-                     unsigned int n_ports)
+int ata_pci_prepare_sff_host(struct pci_dev *pdev,
+                            const struct ata_port_info * const * ppi,
+                            struct ata_host **r_host)
 {
-       struct ata_probe_ent *probe_ent = NULL;
-       struct ata_port_info *port[2];
-       u8 mask;
-       unsigned int legacy_mode = 0;
-       int disable_dev_on_err = 1;
+       struct ata_host *host;
        int rc;
 
-       DPRINTK("ENTER\n");
+       if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
+               return -ENOMEM;
 
-       BUG_ON(n_ports < 1 || n_ports > 2);
+       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;
+       }
 
-       port[0] = port_info[0];
-       if (n_ports > 1)
-               port[1] = port_info[1];
-       else
-               port[1] = port[0];
+       rc = ata_pci_init_sff_host(host);
+       if (rc)
+               goto err_out;
 
-       /* FIXME: Really for ATA it isn't safe because the device may be
-          multi-purpose and we want to leave it alone if it was already
-          enabled. Secondly for shared use as Arjan says we want refcounting
+       /* init DMA related stuff */
+       rc = ata_pci_init_bmdma(host);
+       if (rc)
+               goto err_bmdma;
+
+       devres_remove_group(&pdev->dev, NULL);
+       *r_host = host;
+       return 0;
 
-          Checking dev->is_enabled is insufficient as this is not set at
-          boot for the primary video which is BIOS enabled
-         */
+ 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;
+}
 
-       rc = pci_enable_device(pdev);
+/**
+ *     ata_pci_activate_sff_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_activate_sff_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;
+               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 << 3);
+                       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
@@ -1032,119 +761,119 @@ int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
 #endif
        }
 
-       if (!legacy_mode) {
-               rc = pci_request_regions(pdev, DRV_NAME);
-               if (rc) {
-                       disable_dev_on_err = 0;
-                       goto err_out;
+       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));
                }
-       } else {
-               /* Deal with combined mode hack. This side of the logic all
-                  goes away once the combined mode hack is killed in 2.6.21 */
-               if (!request_region(ATA_PRIMARY_CMD, 8, "libata")) {
-                       struct resource *conflict, res;
-                       res.start = ATA_PRIMARY_CMD;
-                       res.end = ATA_PRIMARY_CMD + 8 - 1;
-                       conflict = ____request_resource(&ioport_resource, &res);
-                       while (conflict->child)
-                               conflict = ____request_resource(conflict, &res);
-                       if (!strcmp(conflict->name, "libata"))
-                               legacy_mode |= ATA_PORT_PRIMARY;
-                       else {
-                               disable_dev_on_err = 0;
-                               printk(KERN_WARNING "ata: 0x%0X IDE port busy\n" \
-                                                   "ata: conflict with %s\n",
-                                                   ATA_PRIMARY_CMD,
-                                                   conflict->name);
-                       }
-               } else
-                       legacy_mode |= ATA_PORT_PRIMARY;
-
-               if (!request_region(ATA_SECONDARY_CMD, 8, "libata")) {
-                       struct resource *conflict, res;
-                       res.start = ATA_SECONDARY_CMD;
-                       res.end = ATA_SECONDARY_CMD + 8 - 1;
-                       conflict = ____request_resource(&ioport_resource, &res);
-                       while (conflict->child)
-                               conflict = ____request_resource(conflict, &res);
-                       if (!strcmp(conflict->name, "libata"))
-                               legacy_mode |= ATA_PORT_SECONDARY;
-                       else {
-                               disable_dev_on_err = 0;
-                               printk(KERN_WARNING "ata: 0x%X IDE port busy\n" \
-                                                   "ata: conflict with %s\n",
-                                                   ATA_SECONDARY_CMD,
-                                                   conflict->name);
-                       }
-               } else
-                       legacy_mode |= ATA_PORT_SECONDARY;
-
-               if (legacy_mode & ATA_PORT_PRIMARY)
-                       pci_request_region(pdev, 1, DRV_NAME);
-               if (legacy_mode & ATA_PORT_SECONDARY)
-                       pci_request_region(pdev, 3, DRV_NAME);
-               /* If there is a DMA resource, allocate it */
-               pci_request_region(pdev, 4, DRV_NAME);
-       }
 
-       /* we have legacy mode, but all ports are unavailable */
-       if (legacy_mode == (1 << 3)) {
-               rc = -EBUSY;
-               goto err_out_regions;
+               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));
+               }
        }
 
-       /* TODO: If we get no DMA mask we should fall back to PIO */
-       rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
-       if (rc)
-               goto err_out_regions;
-       rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
-       if (rc)
-               goto err_out_regions;
-
-       if (legacy_mode) {
-               probe_ent = ata_pci_init_legacy_port(pdev, port, legacy_mode);
-       } else {
-               if (n_ports == 2)
-                       probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
-               else
-                       probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
+       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_init_one - Initialize/register PCI IDE host controller
+ *     @pdev: Controller to be initialized
+ *     @ppi: array of port_info, must be enough for two ports
+ *
+ *     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_init_one(struct pci_dev *pdev,
+                    const struct ata_port_info * const * ppi)
+{
+       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 (!probe_ent) {
-               rc = -ENOMEM;
-               goto err_out_regions;
+
+       if (!pi) {
+               dev_printk(KERN_ERR, &pdev->dev,
+                          "no valid port_info specified\n");
+               return -EINVAL;
        }
 
-       pci_set_master(pdev);
+       if (!devres_open_group(dev, NULL, GFP_KERNEL))
+               return -ENOMEM;
 
-       if (!ata_device_add(probe_ent)) {
-               rc = -ENODEV;
-               goto err_out_ent;
-       }
+       rc = pcim_enable_device(pdev);
+       if (rc)
+               goto out;
 
-       kfree(probe_ent);
+       /* prepare and activate SFF host */
+       rc = ata_pci_prepare_sff_host(pdev, ppi, &host);
+       if (rc)
+               goto out;
 
-       return 0;
+       pci_set_master(pdev);
+       rc = ata_pci_activate_sff_host(host, pi->port_ops->irq_handler,
+                                      pi->sht);
+ out:
+       if (rc == 0)
+               devres_remove_group(&pdev->dev, NULL);
+       else
+               devres_release_group(&pdev->dev, NULL);
 
-err_out_ent:
-       kfree(probe_ent);
-err_out_regions:
-       /* All this conditional stuff is needed for the combined mode hack
-          until 2.6.21 when it can go */
-       if (legacy_mode) {
-               pci_release_region(pdev, 4);
-               if (legacy_mode & ATA_PORT_PRIMARY) {
-                       release_region(ATA_PRIMARY_CMD, 8);
-                       pci_release_region(pdev, 1);
-               }
-               if (legacy_mode & ATA_PORT_SECONDARY) {
-                       release_region(ATA_SECONDARY_CMD, 8);
-                       pci_release_region(pdev, 3);
-               }
-       } else
-               pci_release_regions(pdev);
-err_out:
-       if (disable_dev_on_err)
-               pci_disable_device(pdev);
        return rc;
 }
 
@@ -1153,7 +882,7 @@ err_out:
  *     @pdev: PCI device
  *
  *     Some PCI ATA devices report simplex mode but in fact can be told to
- *     enter non simplex mode. This implements the neccessary logic 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.
  */
@@ -1174,12 +903,12 @@ int ata_pci_clear_simplex(struct pci_dev *pdev)
        return 0;
 }
 
-unsigned long ata_pci_default_filter(const struct ata_port *ap, struct ata_device *adev, unsigned long xfer_mask)
+unsigned long ata_pci_default_filter(struct ata_device *adev, unsigned long xfer_mask)
 {
        /* Filter out DMA modes if the device has been configured by
           the BIOS as PIO only */
 
-       if (ap->ioaddr.bmdma_addr == 0)
+       if (adev->link->ap->ioaddr.bmdma_addr == NULL)
                xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
        return xfer_mask;
 }