]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/ata/libata-core.c
libata: Spot bridge chips
[linux-2.6-omap-h63xx.git] / drivers / ata / libata-core.c
index 772be09b468966277c7d1a41960d52b758cd7480..b81265d1e46936170be39687cddc780933aaafe9 100644 (file)
@@ -70,6 +70,7 @@ const unsigned long sata_deb_timing_long[]            = { 100, 2000, 5000 };
 static unsigned int ata_dev_init_params(struct ata_device *dev,
                                        u16 heads, u16 sectors);
 static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
+static unsigned int ata_dev_set_AN(struct ata_device *dev, u8 enable);
 static void ata_dev_xfermask(struct ata_device *dev);
 static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
 
@@ -86,6 +87,10 @@ int atapi_dmadir = 0;
 module_param(atapi_dmadir, int, 0444);
 MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off, 1=on)");
 
+int atapi_passthru16 = 1;
+module_param(atapi_passthru16, int, 0444);
+MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices; on by default (0=off, 1=on)");
+
 int libata_fua = 0;
 module_param_named(fua, libata_fua, int, 0444);
 MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
@@ -235,7 +240,7 @@ static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
        if (dev->flags & ATA_DFLAG_PIO) {
                tf->protocol = ATA_PROT_PIO;
                index = dev->multi_count ? 0 : 8;
-       } else if (lba48 && (dev->ap->flags & ATA_FLAG_PIO_LBA48)) {
+       } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
                /* Unable to use DMA due to host limitation */
                tf->protocol = ATA_PROT_PIO;
                index = dev->multi_count ? 0 : 8;
@@ -604,7 +609,7 @@ static const char *sata_spd_string(unsigned int spd)
 void ata_dev_disable(struct ata_device *dev)
 {
        if (ata_dev_enabled(dev)) {
-               if (ata_msg_drv(dev->ap))
+               if (ata_msg_drv(dev->link->ap))
                        ata_dev_printk(dev, KERN_WARNING, "disabled\n");
                ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 |
                                             ATA_DNXFER_QUIET);
@@ -735,7 +740,7 @@ ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
        /* see if device passed diags: if master then continue and warn later */
        if (err == 0 && device == 0)
                /* diagnostic fail : do nothing _YET_ */
-               ap->device[device].horkage |= ATA_HORKAGE_DIAGNOSTIC;
+               ap->link.device[device].horkage |= ATA_HORKAGE_DIAGNOSTIC;
        else if (err == 1)
                /* do nothing */ ;
        else if ((device == 0) && (err == 0x81))
@@ -1150,7 +1155,7 @@ void ata_dev_select(struct ata_port *ap, unsigned int device,
        ap->ops->dev_select(ap, device);
 
        if (wait) {
-               if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
+               if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI)
                        msleep(150);
                ata_wait_idle(ap);
        }
@@ -1346,7 +1351,8 @@ unsigned ata_exec_internal_sg(struct ata_device *dev,
                              int dma_dir, struct scatterlist *sg,
                              unsigned int n_elem)
 {
-       struct ata_port *ap = dev->ap;
+       struct ata_link *link = dev->link;
+       struct ata_port *ap = link->ap;
        u8 command = tf->command;
        struct ata_queued_cmd *qc;
        unsigned int tag, preempted_tag;
@@ -1386,11 +1392,11 @@ unsigned ata_exec_internal_sg(struct ata_device *dev,
        qc->dev = dev;
        ata_qc_reinit(qc);
 
-       preempted_tag = ap->active_tag;
-       preempted_sactive = ap->sactive;
+       preempted_tag = link->active_tag;
+       preempted_sactive = link->sactive;
        preempted_qc_active = ap->qc_active;
-       ap->active_tag = ATA_TAG_POISON;
-       ap->sactive = 0;
+       link->active_tag = ATA_TAG_POISON;
+       link->sactive = 0;
        ap->qc_active = 0;
 
        /* prepare & issue qc */
@@ -1467,8 +1473,8 @@ unsigned ata_exec_internal_sg(struct ata_device *dev,
        err_mask = qc->err_mask;
 
        ata_qc_free(qc);
-       ap->active_tag = preempted_tag;
-       ap->sactive = preempted_sactive;
+       link->active_tag = preempted_tag;
+       link->sactive = preempted_sactive;
        ap->qc_active = preempted_qc_active;
 
        /* XXX - Some LLDDs (sata_mv) disable port on command failure.
@@ -1566,7 +1572,7 @@ unsigned int ata_pio_need_iordy(const struct ata_device *adev)
 {
        /* Controller doesn't support  IORDY. Probably a pointless check
           as the caller should know this */
-       if (adev->ap->flags & ATA_FLAG_NO_IORDY)
+       if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
                return 0;
        /* PIO3 and higher it is mandatory */
        if (adev->pio_mode > XFER_PIO_2)
@@ -1613,6 +1619,9 @@ static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
  *     devices.  This function also issues ATA_CMD_INIT_DEV_PARAMS
  *     for pre-ATA4 drives.
  *
+ *     FIXME: ATA_CMD_ID_ATA is optional for early drives and right
+ *     now we abort if we hit that case. 
+ *
  *     LOCKING:
  *     Kernel thread context (may sleep)
  *
@@ -1622,7 +1631,7 @@ static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
 int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
                    unsigned int flags, u16 *id)
 {
-       struct ata_port *ap = dev->ap;
+       struct ata_port *ap = dev->link->ap;
        unsigned int class = *p_class;
        struct ata_taskfile tf;
        unsigned int err_mask = 0;
@@ -1740,10 +1749,13 @@ int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
                /*
                 * The exact sequence expected by certain pre-ATA4 drives is:
                 * SRST RESET
-                * IDENTIFY
-                * INITIALIZE DEVICE PARAMETERS
+                * IDENTIFY (optional in early ATA)
+                * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
                 * anything else..
                 * Some drives were very specific about that exact sequence.
+                *
+                * Note that ATA4 says lba is mandatory so the second check
+                * shoud never trigger.
                 */
                if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
                        err_mask = ata_dev_init_params(dev, id[3], id[6]);
@@ -1774,13 +1786,14 @@ int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
 
 static inline u8 ata_dev_knobble(struct ata_device *dev)
 {
-       return ((dev->ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
+       struct ata_port *ap = dev->link->ap;
+       return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
 }
 
 static void ata_dev_config_ncq(struct ata_device *dev,
                               char *desc, size_t desc_sz)
 {
-       struct ata_port *ap = dev->ap;
+       struct ata_port *ap = dev->link->ap;
        int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
 
        if (!ata_id_has_ncq(dev->id)) {
@@ -1817,8 +1830,8 @@ static void ata_dev_config_ncq(struct ata_device *dev,
  */
 int ata_dev_configure(struct ata_device *dev)
 {
-       struct ata_port *ap = dev->ap;
-       struct ata_eh_context *ehc = &ap->eh_context;
+       struct ata_port *ap = dev->link->ap;
+       struct ata_eh_context *ehc = &dev->link->eh_context;
        int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
        const u16 *id = dev->id;
        unsigned int xfer_mask;
@@ -1975,6 +1988,22 @@ int ata_dev_configure(struct ata_device *dev)
                }
                dev->cdb_len = (unsigned int) rc;
 
+               /*
+                * check to see if this ATAPI device supports
+                * Asynchronous Notification
+                */
+               if ((ap->flags & ATA_FLAG_AN) && ata_id_has_AN(id)) {
+                       int err;
+                       /* issue SET feature command to turn this on */
+                       err = ata_dev_set_AN(dev, SETFEATURES_SATA_ENABLE);
+                       if (err)
+                               ata_dev_printk(dev, KERN_ERR,
+                                               "unable to set AN, err %x\n",
+                                               err);
+                       else
+                               dev->flags |= ATA_DFLAG_AN;
+               }
+
                if (ata_id_cdb_intr(dev->id)) {
                        dev->flags |= ATA_DFLAG_CDB_INTR;
                        cdb_intr_string = ", CDB intr";
@@ -2103,21 +2132,19 @@ int ata_bus_probe(struct ata_port *ap)
 {
        unsigned int classes[ATA_MAX_DEVICES];
        int tries[ATA_MAX_DEVICES];
-       int i, rc;
+       int rc;
        struct ata_device *dev;
 
        ata_port_probe(ap);
 
-       for (i = 0; i < ATA_MAX_DEVICES; i++)
-               tries[i] = ATA_PROBE_MAX_TRIES;
+       ata_link_for_each_dev(dev, &ap->link)
+               tries[dev->devno] = ATA_PROBE_MAX_TRIES;
 
  retry:
        /* reset and determine device classes */
        ap->ops->phy_reset(ap);
 
-       for (i = 0; i < ATA_MAX_DEVICES; i++) {
-               dev = &ap->device[i];
-
+       ata_link_for_each_dev(dev, &ap->link) {
                if (!(ap->flags & ATA_FLAG_DISABLED) &&
                    dev->class != ATA_DEV_UNKNOWN)
                        classes[dev->devno] = dev->class;
@@ -2132,18 +2159,16 @@ int ata_bus_probe(struct ata_port *ap)
        /* after the reset the device state is PIO 0 and the controller
           state is undefined. Record the mode */
 
-       for (i = 0; i < ATA_MAX_DEVICES; i++)
-               ap->device[i].pio_mode = XFER_PIO_0;
+       ata_link_for_each_dev(dev, &ap->link)
+               dev->pio_mode = XFER_PIO_0;
 
        /* read IDENTIFY page and configure devices. We have to do the identify
           specific sequence bass-ackwards so that PDIAG- is released by
           the slave device */
 
-       for (i = ATA_MAX_DEVICES - 1; i >=  0; i--) {
-               dev = &ap->device[i];
-
-               if (tries[i])
-                       dev->class = classes[i];
+       ata_link_for_each_dev(dev, &ap->link) {
+               if (tries[dev->devno])
+                       dev->class = classes[dev->devno];
 
                if (!ata_dev_enabled(dev))
                        continue;
@@ -2158,33 +2183,42 @@ int ata_bus_probe(struct ata_port *ap)
        if (ap->ops->cable_detect)
                ap->cbl = ap->ops->cable_detect(ap);
 
+       /* We may have SATA bridge glue hiding here irrespective of the
+          reported cable types and sensed types */
+       ata_link_for_each_dev(dev, &ap->link) {
+               if (!ata_dev_enabled(dev))
+                       continue;
+               /* SATA drives indicate we have a bridge. We don't know which
+                  end of the link the bridge is which is a problem */
+               if (ata_id_is_sata(dev->id))
+                       ap->cbl = ATA_CBL_SATA;
+       }
+
        /* After the identify sequence we can now set up the devices. We do
           this in the normal order so that the user doesn't get confused */
 
-       for(i = 0; i < ATA_MAX_DEVICES; i++) {
-               dev = &ap->device[i];
+       ata_link_for_each_dev(dev, &ap->link) {
                if (!ata_dev_enabled(dev))
                        continue;
 
-               ap->eh_context.i.flags |= ATA_EHI_PRINTINFO;
+               ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
                rc = ata_dev_configure(dev);
-               ap->eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
+               ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
                if (rc)
                        goto fail;
        }
 
        /* configure transfer mode */
-       rc = ata_set_mode(ap, &dev);
+       rc = ata_set_mode(&ap->link, &dev);
        if (rc)
                goto fail;
 
-       for (i = 0; i < ATA_MAX_DEVICES; i++)
-               if (ata_dev_enabled(&ap->device[i]))
+       ata_link_for_each_dev(dev, &ap->link)
+               if (ata_dev_enabled(dev))
                        return 0;
 
        /* no device present, disable port */
        ata_port_disable(ap);
-       ap->ops->port_disable(ap);
        return -ENODEV;
 
  fail:
@@ -2204,7 +2238,7 @@ int ata_bus_probe(struct ata_port *ap)
                        /* This is the last chance, better to slow
                         * down than lose it.
                         */
-                       sata_down_spd_limit(ap);
+                       sata_down_spd_limit(&ap->link);
                        ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
                }
        }
@@ -2233,28 +2267,28 @@ void ata_port_probe(struct ata_port *ap)
 
 /**
  *     sata_print_link_status - Print SATA link status
- *     @ap: SATA port to printk link status about
+ *     @link: SATA link to printk link status about
  *
  *     This function prints link speed and status of a SATA link.
  *
  *     LOCKING:
  *     None.
  */
-void sata_print_link_status(struct ata_port *ap)
+void sata_print_link_status(struct ata_link *link)
 {
        u32 sstatus, scontrol, tmp;
 
-       if (sata_scr_read(ap, SCR_STATUS, &sstatus))
+       if (sata_scr_read(link, SCR_STATUS, &sstatus))
                return;
-       sata_scr_read(ap, SCR_CONTROL, &scontrol);
+       sata_scr_read(link, SCR_CONTROL, &scontrol);
 
-       if (ata_port_online(ap)) {
+       if (ata_link_online(link)) {
                tmp = (sstatus >> 4) & 0xf;
-               ata_port_printk(ap, KERN_INFO,
+               ata_link_printk(link, KERN_INFO,
                                "SATA link up %s (SStatus %X SControl %X)\n",
                                sata_spd_string(tmp), sstatus, scontrol);
        } else {
-               ata_port_printk(ap, KERN_INFO,
+               ata_link_printk(link, KERN_INFO,
                                "SATA link down (SStatus %X SControl %X)\n",
                                sstatus, scontrol);
        }
@@ -2274,32 +2308,33 @@ void sata_print_link_status(struct ata_port *ap)
  */
 void __sata_phy_reset(struct ata_port *ap)
 {
-       u32 sstatus;
+       struct ata_link *link = &ap->link;
        unsigned long timeout = jiffies + (HZ * 5);
+       u32 sstatus;
 
        if (ap->flags & ATA_FLAG_SATA_RESET) {
                /* issue phy wake/reset */
-               sata_scr_write_flush(ap, SCR_CONTROL, 0x301);
+               sata_scr_write_flush(link, SCR_CONTROL, 0x301);
                /* Couldn't find anything in SATA I/II specs, but
                 * AHCI-1.1 10.4.2 says at least 1 ms. */
                mdelay(1);
        }
        /* phy wake/clear reset */
-       sata_scr_write_flush(ap, SCR_CONTROL, 0x300);
+       sata_scr_write_flush(link, SCR_CONTROL, 0x300);
 
        /* wait for phy to become ready, if necessary */
        do {
                msleep(200);
-               sata_scr_read(ap, SCR_STATUS, &sstatus);
+               sata_scr_read(link, SCR_STATUS, &sstatus);
                if ((sstatus & 0xf) != 1)
                        break;
        } while (time_before(jiffies, timeout));
 
        /* print link status */
-       sata_print_link_status(ap);
+       sata_print_link_status(link);
 
        /* TODO: phy layer with polling, timeouts, etc. */
-       if (!ata_port_offline(ap))
+       if (!ata_link_offline(link))
                ata_port_probe(ap);
        else
                ata_port_disable(ap);
@@ -2344,8 +2379,8 @@ void sata_phy_reset(struct ata_port *ap)
 
 struct ata_device *ata_dev_pair(struct ata_device *adev)
 {
-       struct ata_port *ap = adev->ap;
-       struct ata_device *pair = &ap->device[1 - adev->devno];
+       struct ata_link *link = adev->link;
+       struct ata_device *pair = &link->device[1 - adev->devno];
        if (!ata_dev_enabled(pair))
                return NULL;
        return pair;
@@ -2366,16 +2401,16 @@ struct ata_device *ata_dev_pair(struct ata_device *adev)
 
 void ata_port_disable(struct ata_port *ap)
 {
-       ap->device[0].class = ATA_DEV_NONE;
-       ap->device[1].class = ATA_DEV_NONE;
+       ap->link.device[0].class = ATA_DEV_NONE;
+       ap->link.device[1].class = ATA_DEV_NONE;
        ap->flags |= ATA_FLAG_DISABLED;
 }
 
 /**
  *     sata_down_spd_limit - adjust SATA spd limit downward
- *     @ap: Port to adjust SATA spd limit for
+ *     @link: Link to adjust SATA spd limit for
  *
- *     Adjust SATA spd limit of @ap downward.  Note that this
+ *     Adjust SATA spd limit of @link downward.  Note that this
  *     function only adjusts the limit.  The change must be applied
  *     using sata_set_spd().
  *
@@ -2385,24 +2420,24 @@ void ata_port_disable(struct ata_port *ap)
  *     RETURNS:
  *     0 on success, negative errno on failure
  */
-int sata_down_spd_limit(struct ata_port *ap)
+int sata_down_spd_limit(struct ata_link *link)
 {
        u32 sstatus, spd, mask;
        int rc, highbit;
 
-       if (!sata_scr_valid(ap))
+       if (!sata_scr_valid(link))
                return -EOPNOTSUPP;
 
        /* If SCR can be read, use it to determine the current SPD.
-        * If not, use cached value in ap->sata_spd.
+        * If not, use cached value in link->sata_spd.
         */
-       rc = sata_scr_read(ap, SCR_STATUS, &sstatus);
+       rc = sata_scr_read(link, SCR_STATUS, &sstatus);
        if (rc == 0)
                spd = (sstatus >> 4) & 0xf;
        else
-               spd = ap->sata_spd;
+               spd = link->sata_spd;
 
-       mask = ap->sata_spd_limit;
+       mask = link->sata_spd_limit;
        if (mask <= 1)
                return -EINVAL;
 
@@ -2422,22 +2457,22 @@ int sata_down_spd_limit(struct ata_port *ap)
        if (!mask)
                return -EINVAL;
 
-       ap->sata_spd_limit = mask;
+       link->sata_spd_limit = mask;
 
-       ata_port_printk(ap, KERN_WARNING, "limiting SATA link speed to %s\n",
+       ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
                        sata_spd_string(fls(mask)));
 
        return 0;
 }
 
-static int __sata_set_spd_needed(struct ata_port *ap, u32 *scontrol)
+static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
 {
        u32 spd, limit;
 
-       if (ap->sata_spd_limit == UINT_MAX)
+       if (link->sata_spd_limit == UINT_MAX)
                limit = 0;
        else
-               limit = fls(ap->sata_spd_limit);
+               limit = fls(link->sata_spd_limit);
 
        spd = (*scontrol >> 4) & 0xf;
        *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
@@ -2447,10 +2482,10 @@ static int __sata_set_spd_needed(struct ata_port *ap, u32 *scontrol)
 
 /**
  *     sata_set_spd_needed - is SATA spd configuration needed
- *     @ap: Port in question
+ *     @link: Link in question
  *
  *     Test whether the spd limit in SControl matches
- *     @ap->sata_spd_limit.  This function is used to determine
+ *     @link->sata_spd_limit.  This function is used to determine
  *     whether hardreset is necessary to apply SATA spd
  *     configuration.
  *
@@ -2460,21 +2495,21 @@ static int __sata_set_spd_needed(struct ata_port *ap, u32 *scontrol)
  *     RETURNS:
  *     1 if SATA spd configuration is needed, 0 otherwise.
  */
-int sata_set_spd_needed(struct ata_port *ap)
+int sata_set_spd_needed(struct ata_link *link)
 {
        u32 scontrol;
 
-       if (sata_scr_read(ap, SCR_CONTROL, &scontrol))
+       if (sata_scr_read(link, SCR_CONTROL, &scontrol))
                return 0;
 
-       return __sata_set_spd_needed(ap, &scontrol);
+       return __sata_set_spd_needed(link, &scontrol);
 }
 
 /**
  *     sata_set_spd - set SATA spd according to spd limit
- *     @ap: Port to set SATA spd for
+ *     @link: Link to set SATA spd for
  *
- *     Set SATA spd of @ap according to sata_spd_limit.
+ *     Set SATA spd of @link according to sata_spd_limit.
  *
  *     LOCKING:
  *     Inherited from caller.
@@ -2483,18 +2518,18 @@ int sata_set_spd_needed(struct ata_port *ap)
  *     0 if spd doesn't need to be changed, 1 if spd has been
  *     changed.  Negative errno if SCR registers are inaccessible.
  */
-int sata_set_spd(struct ata_port *ap)
+int sata_set_spd(struct ata_link *link)
 {
        u32 scontrol;
        int rc;
 
-       if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
+       if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
                return rc;
 
-       if (!__sata_set_spd_needed(ap, &scontrol))
+       if (!__sata_set_spd_needed(link, &scontrol))
                return 0;
 
-       if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
+       if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
                return rc;
 
        return 1;
@@ -2749,7 +2784,7 @@ int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
 
 static int ata_dev_set_mode(struct ata_device *dev)
 {
-       struct ata_eh_context *ehc = &dev->ap->eh_context;
+       struct ata_eh_context *ehc = &dev->link->eh_context;
        unsigned int err_mask;
        int rc;
 
@@ -2761,7 +2796,11 @@ static int ata_dev_set_mode(struct ata_device *dev)
        /* Old CFA may refuse this command, which is just fine */
        if (dev->xfer_shift == ATA_SHIFT_PIO && ata_id_is_cfa(dev->id))
                err_mask &= ~AC_ERR_DEV;
-
+       /* Some very old devices and some bad newer ones fail any kind of
+          SET_XFERMODE request but support PIO0-2 timings and no IORDY */
+       if (dev->xfer_shift == ATA_SHIFT_PIO && !ata_id_has_iordy(dev->id) &&
+                       dev->pio_mode <= XFER_PIO_2)
+               err_mask &= ~AC_ERR_DEV;
        if (err_mask) {
                ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
                               "(err_mask=0x%x)\n", err_mask);
@@ -2784,7 +2823,7 @@ static int ata_dev_set_mode(struct ata_device *dev)
 
 /**
  *     ata_do_set_mode - Program timings and issue SET FEATURES - XFER
- *     @ap: port on which timings will be programmed
+ *     @link: link on which timings will be programmed
  *     @r_failed_dev: out paramter for failed device
  *
  *     Standard implementation of the function used to tune and set
@@ -2799,18 +2838,16 @@ static int ata_dev_set_mode(struct ata_device *dev)
  *     0 on success, negative errno otherwise
  */
 
-int ata_do_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
+int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
 {
+       struct ata_port *ap = link->ap;
        struct ata_device *dev;
-       int i, rc = 0, used_dma = 0, found = 0;
-
+       int rc = 0, used_dma = 0, found = 0;
 
        /* step 1: calculate xfer_mask */
-       for (i = 0; i < ATA_MAX_DEVICES; i++) {
+       ata_link_for_each_dev(dev, link) {
                unsigned int pio_mask, dma_mask;
 
-               dev = &ap->device[i];
-
                if (!ata_dev_enabled(dev))
                        continue;
 
@@ -2829,8 +2866,7 @@ int ata_do_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
                goto out;
 
        /* step 2: always set host PIO timings */
-       for (i = 0; i < ATA_MAX_DEVICES; i++) {
-               dev = &ap->device[i];
+       ata_link_for_each_dev(dev, link) {
                if (!ata_dev_enabled(dev))
                        continue;
 
@@ -2847,9 +2883,7 @@ int ata_do_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
        }
 
        /* step 3: set host DMA timings */
-       for (i = 0; i < ATA_MAX_DEVICES; i++) {
-               dev = &ap->device[i];
-
+       ata_link_for_each_dev(dev, link) {
                if (!ata_dev_enabled(dev) || !dev->dma_mode)
                        continue;
 
@@ -2860,9 +2894,7 @@ int ata_do_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
        }
 
        /* step 4: update devices' xfer mode */
-       for (i = 0; i < ATA_MAX_DEVICES; i++) {
-               dev = &ap->device[i];
-
+       ata_link_for_each_dev(dev, link) {
                /* don't update suspended devices' xfer mode */
                if (!ata_dev_enabled(dev))
                        continue;
@@ -2886,7 +2918,7 @@ int ata_do_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
 
 /**
  *     ata_set_mode - Program timings and issue SET FEATURES - XFER
- *     @ap: port on which timings will be programmed
+ *     @link: link on which timings will be programmed
  *     @r_failed_dev: out paramter for failed device
  *
  *     Set ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
@@ -2899,12 +2931,14 @@ int ata_do_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
  *     RETURNS:
  *     0 on success, negative errno otherwise
  */
-int ata_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
+int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
 {
+       struct ata_port *ap = link->ap;
+
        /* has private set_mode? */
        if (ap->ops->set_mode)
-               return ap->ops->set_mode(ap, r_failed_dev);
-       return ata_do_set_mode(ap, r_failed_dev);
+               return ap->ops->set_mode(link, r_failed_dev);
+       return ata_do_set_mode(link, r_failed_dev);
 }
 
 /**
@@ -3007,7 +3041,7 @@ int ata_wait_ready(struct ata_port *ap, unsigned long deadline)
 
                if (!(status & ATA_BUSY))
                        return 0;
-               if (!ata_port_online(ap) && status == 0xff)
+               if (!ata_link_online(&ap->link) && status == 0xff)
                        return -ENODEV;
                if (time_after(now, deadline))
                        return -EBUSY;
@@ -3142,6 +3176,7 @@ static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
 
 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;
@@ -3177,19 +3212,19 @@ void ata_bus_reset(struct ata_port *ap)
        /*
         * determine by signature whether we have ATA or ATAPI devices
         */
-       ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
+       device[0].class = ata_dev_try_classify(ap, 0, &err);
        if ((slave_possible) && (err != 0x81))
-               ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
+               device[1].class = ata_dev_try_classify(ap, 1, &err);
 
        /* is double-select really necessary? */
-       if (ap->device[1].class != ATA_DEV_NONE)
+       if (device[1].class != ATA_DEV_NONE)
                ap->ops->dev_select(ap, 1);
-       if (ap->device[0].class != ATA_DEV_NONE)
+       if (device[0].class != ATA_DEV_NONE)
                ap->ops->dev_select(ap, 0);
 
        /* if no devices were detected, disable this port */
-       if ((ap->device[0].class == ATA_DEV_NONE) &&
-           (ap->device[1].class == ATA_DEV_NONE))
+       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)) {
@@ -3202,18 +3237,18 @@ void ata_bus_reset(struct ata_port *ap)
 
 err_out:
        ata_port_printk(ap, KERN_ERR, "disabling port\n");
-       ap->ops->port_disable(ap);
+       ata_port_disable(ap);
 
        DPRINTK("EXIT\n");
 }
 
 /**
- *     sata_phy_debounce - debounce SATA phy status
- *     @ap: ATA port to debounce SATA phy status for
+ *     sata_link_debounce - debounce SATA phy status
+ *     @link: ATA link to debounce SATA phy status for
  *     @params: timing parameters { interval, duratinon, timeout } in msec
  *     @deadline: deadline jiffies for the operation
  *
- *     Make sure SStatus of @ap reaches stable state, determined by
+*      Make sure SStatus of @link reaches stable state, determined by
  *     holding the same value where DET is not 1 for @duration polled
  *     every @interval, before @timeout.  Timeout constraints the
  *     beginning of the stable state.  Because DET gets stuck at 1 on
@@ -3229,8 +3264,8 @@ err_out:
  *     RETURNS:
  *     0 on success, -errno on failure.
  */
-int sata_phy_debounce(struct ata_port *ap, const unsigned long *params,
-                     unsigned long deadline)
+int sata_link_debounce(struct ata_link *link, const unsigned long *params,
+                      unsigned long deadline)
 {
        unsigned long interval_msec = params[0];
        unsigned long duration = msecs_to_jiffies(params[1]);
@@ -3242,7 +3277,7 @@ int sata_phy_debounce(struct ata_port *ap, const unsigned long *params,
        if (time_before(t, deadline))
                deadline = t;
 
-       if ((rc = sata_scr_read(ap, SCR_STATUS, &cur)))
+       if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
                return rc;
        cur &= 0xf;
 
@@ -3251,7 +3286,7 @@ int sata_phy_debounce(struct ata_port *ap, const unsigned long *params,
 
        while (1) {
                msleep(interval_msec);
-               if ((rc = sata_scr_read(ap, SCR_STATUS, &cur)))
+               if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
                        return rc;
                cur &= 0xf;
 
@@ -3277,12 +3312,12 @@ int sata_phy_debounce(struct ata_port *ap, const unsigned long *params,
 }
 
 /**
- *     sata_phy_resume - resume SATA phy
- *     @ap: ATA port to resume SATA phy for
+ *     sata_link_resume - resume SATA link
+ *     @link: ATA link to resume SATA
  *     @params: timing parameters { interval, duratinon, timeout } in msec
  *     @deadline: deadline jiffies for the operation
  *
- *     Resume SATA phy of @ap and debounce it.
+ *     Resume SATA phy @link and debounce it.
  *
  *     LOCKING:
  *     Kernel thread context (may sleep)
@@ -3290,18 +3325,18 @@ int sata_phy_debounce(struct ata_port *ap, const unsigned long *params,
  *     RETURNS:
  *     0 on success, -errno on failure.
  */
-int sata_phy_resume(struct ata_port *ap, const unsigned long *params,
-                   unsigned long deadline)
+int sata_link_resume(struct ata_link *link, const unsigned long *params,
+                    unsigned long deadline)
 {
        u32 scontrol;
        int rc;
 
-       if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
+       if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
                return rc;
 
        scontrol = (scontrol & 0x0f0) | 0x300;
 
-       if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
+       if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
                return rc;
 
        /* Some PHYs react badly if SStatus is pounded immediately
@@ -3309,15 +3344,15 @@ int sata_phy_resume(struct ata_port *ap, const unsigned long *params,
         */
        msleep(200);
 
-       return sata_phy_debounce(ap, params, deadline);
+       return sata_link_debounce(link, params, deadline);
 }
 
 /**
  *     ata_std_prereset - prepare for reset
- *     @ap: ATA port to be reset
+ *     @link: ATA link to be reset
  *     @deadline: deadline jiffies for the operation
  *
- *     @ap is about to be reset.  Initialize it.  Failure from
+ *     @link is about to be reset.  Initialize it.  Failure from
  *     prereset makes libata abort whole reset sequence and give up
  *     that port, so prereset should be best-effort.  It does its
  *     best to prepare for reset sequence but if things go wrong, it
@@ -3329,37 +3364,38 @@ int sata_phy_resume(struct ata_port *ap, const unsigned long *params,
  *     RETURNS:
  *     0 on success, -errno otherwise.
  */
-int ata_std_prereset(struct ata_port *ap, unsigned long deadline)
+int ata_std_prereset(struct ata_link *link, unsigned long deadline)
 {
-       struct ata_eh_context *ehc = &ap->eh_context;
+       struct ata_port *ap = link->ap;
+       struct ata_eh_context *ehc = &link->eh_context;
        const unsigned long *timing = sata_ehc_deb_timing(ehc);
        int rc;
 
        /* handle link resume */
        if ((ehc->i.flags & ATA_EHI_RESUME_LINK) &&
-           (ap->flags & ATA_FLAG_HRST_TO_RESUME))
+           (link->flags & ATA_LFLAG_HRST_TO_RESUME))
                ehc->i.action |= ATA_EH_HARDRESET;
 
        /* if we're about to do hardreset, nothing more to do */
        if (ehc->i.action & ATA_EH_HARDRESET)
                return 0;
 
-       /* if SATA, resume phy */
+       /* if SATA, resume link */
        if (ap->flags & ATA_FLAG_SATA) {
-               rc = sata_phy_resume(ap, timing, deadline);
+               rc = sata_link_resume(link, timing, deadline);
                /* whine about phy resume failure but proceed */
                if (rc && rc != -EOPNOTSUPP)
-                       ata_port_printk(ap, KERN_WARNING, "failed to resume "
+                       ata_link_printk(link, KERN_WARNING, "failed to resume "
                                        "link for reset (errno=%d)\n", rc);
        }
 
        /* Wait for !BSY if the controller can wait for the first D2H
         * Reg FIS and we don't know that no device is attached.
         */
-       if (!(ap->flags & ATA_FLAG_SKIP_D2H_BSY) && !ata_port_offline(ap)) {
+       if (!(link->flags & ATA_LFLAG_SKIP_D2H_BSY) && !ata_link_offline(link)) {
                rc = ata_wait_ready(ap, deadline);
                if (rc && rc != -ENODEV) {
-                       ata_port_printk(ap, KERN_WARNING, "device not ready "
+                       ata_link_printk(link, KERN_WARNING, "device not ready "
                                        "(errno=%d), forcing hardreset\n", rc);
                        ehc->i.action |= ATA_EH_HARDRESET;
                }
@@ -3370,7 +3406,7 @@ int ata_std_prereset(struct ata_port *ap, unsigned long deadline)
 
 /**
  *     ata_std_softreset - reset host port via ATA SRST
- *     @ap: port to reset
+ *     @link: ATA link to reset
  *     @classes: resulting classes of attached devices
  *     @deadline: deadline jiffies for the operation
  *
@@ -3382,9 +3418,10 @@ int ata_std_prereset(struct ata_port *ap, unsigned long deadline)
  *     RETURNS:
  *     0 on success, -errno otherwise.
  */
-int ata_std_softreset(struct ata_port *ap, unsigned int *classes,
+int ata_std_softreset(struct ata_link *link, unsigned int *classes,
                      unsigned long deadline)
 {
+       struct ata_port *ap = link->ap;
        unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
        unsigned int devmask = 0;
        int rc;
@@ -3392,7 +3429,7 @@ int ata_std_softreset(struct ata_port *ap, unsigned int *classes,
 
        DPRINTK("ENTER\n");
 
-       if (ata_port_offline(ap)) {
+       if (ata_link_offline(link)) {
                classes[0] = ATA_DEV_NONE;
                goto out;
        }
@@ -3410,8 +3447,8 @@ int ata_std_softreset(struct ata_port *ap, unsigned int *classes,
        DPRINTK("about to softreset, devmask=%x\n", devmask);
        rc = ata_bus_softreset(ap, devmask, deadline);
        /* if link is occupied, -ENODEV too is an error */
-       if (rc && (rc != -ENODEV || sata_scr_valid(ap))) {
-               ata_port_printk(ap, KERN_ERR, "SRST failed (errno=%d)\n", rc);
+       if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
+               ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
                return rc;
        }
 
@@ -3426,12 +3463,12 @@ int ata_std_softreset(struct ata_port *ap, unsigned int *classes,
 }
 
 /**
- *     sata_port_hardreset - reset port via SATA phy reset
- *     @ap: port to reset
+ *     sata_link_hardreset - reset link via SATA phy reset
+ *     @link: link to reset
  *     @timing: timing parameters { interval, duratinon, timeout } in msec
  *     @deadline: deadline jiffies for the operation
  *
- *     SATA phy-reset host port using DET bits of SControl register.
+ *     SATA phy-reset @link using DET bits of SControl register.
  *
  *     LOCKING:
  *     Kernel thread context (may sleep)
@@ -3439,7 +3476,7 @@ int ata_std_softreset(struct ata_port *ap, unsigned int *classes,
  *     RETURNS:
  *     0 on success, -errno otherwise.
  */
-int sata_port_hardreset(struct ata_port *ap, const unsigned long *timing,
+int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
                        unsigned long deadline)
 {
        u32 scontrol;
@@ -3447,30 +3484,30 @@ int sata_port_hardreset(struct ata_port *ap, const unsigned long *timing,
 
        DPRINTK("ENTER\n");
 
-       if (sata_set_spd_needed(ap)) {
+       if (sata_set_spd_needed(link)) {
                /* SATA spec says nothing about how to reconfigure
                 * spd.  To be on the safe side, turn off phy during
                 * reconfiguration.  This works for at least ICH7 AHCI
                 * and Sil3124.
                 */
-               if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
+               if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
                        goto out;
 
                scontrol = (scontrol & 0x0f0) | 0x304;
 
-               if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
+               if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
                        goto out;
 
-               sata_set_spd(ap);
+               sata_set_spd(link);
        }
 
        /* issue phy wake/reset */
-       if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
+       if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
                goto out;
 
        scontrol = (scontrol & 0x0f0) | 0x301;
 
-       if ((rc = sata_scr_write_flush(ap, SCR_CONTROL, scontrol)))
+       if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
                goto out;
 
        /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
@@ -3478,8 +3515,8 @@ int sata_port_hardreset(struct ata_port *ap, const unsigned long *timing,
         */
        msleep(1);
 
-       /* bring phy back */
-       rc = sata_phy_resume(ap, timing, deadline);
+       /* bring link back */
+       rc = sata_link_resume(link, timing, deadline);
  out:
        DPRINTK("EXIT, rc=%d\n", rc);
        return rc;
@@ -3487,7 +3524,7 @@ int sata_port_hardreset(struct ata_port *ap, const unsigned long *timing,
 
 /**
  *     sata_std_hardreset - reset host port via SATA phy reset
- *     @ap: port to reset
+ *     @link: link to reset
  *     @class: resulting class of attached device
  *     @deadline: deadline jiffies for the operation
  *
@@ -3500,24 +3537,25 @@ int sata_port_hardreset(struct ata_port *ap, const unsigned long *timing,
  *     RETURNS:
  *     0 on success, -errno otherwise.
  */
-int sata_std_hardreset(struct ata_port *ap, unsigned int *class,
+int sata_std_hardreset(struct ata_link *link, unsigned int *class,
                       unsigned long deadline)
 {
-       const unsigned long *timing = sata_ehc_deb_timing(&ap->eh_context);
+       struct ata_port *ap = link->ap;
+       const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
        int rc;
 
        DPRINTK("ENTER\n");
 
        /* do hardreset */
-       rc = sata_port_hardreset(ap, timing, deadline);
+       rc = sata_link_hardreset(link, timing, deadline);
        if (rc) {
-               ata_port_printk(ap, KERN_ERR,
+               ata_link_printk(link, KERN_ERR,
                                "COMRESET failed (errno=%d)\n", rc);
                return rc;
        }
 
        /* TODO: phy layer with polling, timeouts, etc. */
-       if (ata_port_offline(ap)) {
+       if (ata_link_offline(link)) {
                *class = ATA_DEV_NONE;
                DPRINTK("EXIT, link offline\n");
                return 0;
@@ -3529,7 +3567,7 @@ int sata_std_hardreset(struct ata_port *ap, unsigned int *class,
        rc = ata_wait_ready(ap, deadline);
        /* link occupied, -ENODEV too is an error */
        if (rc) {
-               ata_port_printk(ap, KERN_ERR,
+               ata_link_printk(link, KERN_ERR,
                                "COMRESET failed (errno=%d)\n", rc);
                return rc;
        }
@@ -3544,7 +3582,7 @@ int sata_std_hardreset(struct ata_port *ap, unsigned int *class,
 
 /**
  *     ata_std_postreset - standard postreset callback
- *     @ap: the target ata_port
+ *     @link: the target ata_link
  *     @classes: classes of attached devices
  *
  *     This function is invoked after a successful reset.  Note that
@@ -3554,18 +3592,19 @@ int sata_std_hardreset(struct ata_port *ap, unsigned int *class,
  *     LOCKING:
  *     Kernel thread context (may sleep)
  */
-void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
+void ata_std_postreset(struct ata_link *link, unsigned int *classes)
 {
+       struct ata_port *ap = link->ap;
        u32 serror;
 
        DPRINTK("ENTER\n");
 
        /* print link status */
-       sata_print_link_status(ap);
+       sata_print_link_status(link);
 
        /* clear SError */
-       if (sata_scr_read(ap, SCR_ERROR, &serror) == 0)
-               sata_scr_write(ap, SCR_ERROR, serror);
+       if (sata_scr_read(link, SCR_ERROR, &serror) == 0)
+               sata_scr_write(link, SCR_ERROR, serror);
 
        /* is double-select really necessary? */
        if (classes[0] != ATA_DEV_NONE)
@@ -3652,7 +3691,7 @@ static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
 int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
 {
        unsigned int class = dev->class;
-       u16 *id = (void *)dev->ap->sector_buf;
+       u16 *id = (void *)dev->link->ap->sector_buf;
        int rc;
 
        /* read ID data */
@@ -3837,7 +3876,7 @@ static int ata_dma_blacklisted(const struct ata_device *dev)
         * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
         * if the LLDD handles only interrupts in the HSM_ST_LAST state.
         */
-       if ((dev->ap->flags & ATA_FLAG_PIO_POLLING) &&
+       if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
            (dev->flags & ATA_DFLAG_CDB_INTR))
                return 1;
        return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
@@ -3857,7 +3896,8 @@ static int ata_dma_blacklisted(const struct ata_device *dev)
  */
 static void ata_dev_xfermask(struct ata_device *dev)
 {
-       struct ata_port *ap = dev->ap;
+       struct ata_link *link = dev->link;
+       struct ata_port *ap = link->ap;
        struct ata_host *host = ap->host;
        unsigned long xfer_mask;
 
@@ -3961,6 +4001,42 @@ static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
        return err_mask;
 }
 
+/**
+ *     ata_dev_set_AN - Issue SET FEATURES - SATA FEATURES
+ *     @dev: Device to which command will be sent
+ *     @enable: Whether to enable or disable the feature
+ *
+ *     Issue SET FEATURES - SATA FEATURES command to device @dev
+ *     on port @ap with sector count set to indicate Asynchronous
+ *     Notification feature
+ *
+ *     LOCKING:
+ *     PCI/etc. bus probe sem.
+ *
+ *     RETURNS:
+ *     0 on success, AC_ERR_* mask otherwise.
+ */
+static unsigned int ata_dev_set_AN(struct ata_device *dev, u8 enable)
+{
+       struct ata_taskfile tf;
+       unsigned int err_mask;
+
+       /* set up set-features taskfile */
+       DPRINTK("set features - SATA features\n");
+
+       ata_tf_init(dev, &tf);
+       tf.command = ATA_CMD_SET_FEATURES;
+       tf.feature = enable;
+       tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
+       tf.protocol = ATA_PROT_NODATA;
+       tf.nsect = SATA_AN;
+
+       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+
+       DPRINTK("EXIT, err_mask=%x\n", err_mask);
+       return err_mask;
+}
+
 /**
  *     ata_dev_init_params - Issue INIT DEV PARAMS command
  *     @dev: Device to which command will be sent
@@ -4482,7 +4558,7 @@ void swap_buf_le16(u16 *buf, unsigned int buf_words)
 void ata_data_xfer(struct ata_device *adev, unsigned char *buf,
                   unsigned int buflen, int write_data)
 {
-       struct ata_port *ap = adev->ap;
+       struct ata_port *ap = adev->link->ap;
        unsigned int words = buflen >> 1;
 
        /* Transfer multiple of 2 bytes */
@@ -4611,6 +4687,8 @@ static void ata_pio_sectors(struct ata_queued_cmd *qc)
                        ata_pio_sector(qc);
        } else
                ata_pio_sector(qc);
+
+       ata_altstatus(qc->ap); /* flush */
 }
 
 /**
@@ -4785,6 +4863,7 @@ static void atapi_pio_bytes(struct ata_queued_cmd *qc)
        VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
 
        __atapi_pio_bytes(qc, bytes);
+       ata_altstatus(ap); /* flush */
 
        return;
 
@@ -4956,7 +5035,6 @@ fsm_start:
                         */
                        ap->hsm_task_state = HSM_ST;
                        ata_pio_sectors(qc);
-                       ata_altstatus(ap); /* flush */
                } else
                        /* send CDB */
                        atapi_send_cdb(ap, qc);
@@ -5037,7 +5115,6 @@ fsm_start:
 
                                if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
                                        ata_pio_sectors(qc);
-                                       ata_altstatus(ap);
                                        status = ata_wait_idle(ap);
                                }
 
@@ -5057,13 +5134,11 @@ fsm_start:
                        if (ap->hsm_task_state == HSM_ST_LAST &&
                            (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
                                /* all data read */
-                               ata_altstatus(ap);
                                status = ata_wait_idle(ap);
                                goto fsm_start;
                        }
                }
 
-               ata_altstatus(ap); /* flush */
                poll_next = 1;
                break;
 
@@ -5188,7 +5263,7 @@ static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
 
 struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
 {
-       struct ata_port *ap = dev->ap;
+       struct ata_port *ap = dev->link->ap;
        struct ata_queued_cmd *qc;
 
        qc = ata_qc_new(ap);
@@ -5231,6 +5306,7 @@ void ata_qc_free(struct ata_queued_cmd *qc)
 void __ata_qc_complete(struct ata_queued_cmd *qc)
 {
        struct ata_port *ap = qc->ap;
+       struct ata_link *link = qc->dev->link;
 
        WARN_ON(qc == NULL);    /* ata_qc_from_tag _might_ return NULL */
        WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
@@ -5240,9 +5316,9 @@ void __ata_qc_complete(struct ata_queued_cmd *qc)
 
        /* command should be marked inactive atomically with qc completion */
        if (qc->tf.protocol == ATA_PROT_NCQ)
-               ap->sactive &= ~(1 << qc->tag);
+               link->sactive &= ~(1 << qc->tag);
        else
-               ap->active_tag = ATA_TAG_POISON;
+               link->active_tag = ATA_TAG_POISON;
 
        /* atapi: mark qc as inactive to prevent the interrupt handler
         * from completing the command twice later, before the error handler
@@ -5411,19 +5487,20 @@ static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
 void ata_qc_issue(struct ata_queued_cmd *qc)
 {
        struct ata_port *ap = qc->ap;
+       struct ata_link *link = qc->dev->link;
 
        /* Make sure only one non-NCQ command is outstanding.  The
         * check is skipped for old EH because it reuses active qc to
         * request ATAPI sense.
         */
-       WARN_ON(ap->ops->error_handler && ata_tag_valid(ap->active_tag));
+       WARN_ON(ap->ops->error_handler && ata_tag_valid(link->active_tag));
 
        if (qc->tf.protocol == ATA_PROT_NCQ) {
-               WARN_ON(ap->sactive & (1 << qc->tag));
-               ap->sactive |= 1 << qc->tag;
+               WARN_ON(link->sactive & (1 << qc->tag));
+               link->sactive |= 1 << qc->tag;
        } else {
-               WARN_ON(ap->sactive);
-               ap->active_tag = qc->tag;
+               WARN_ON(link->sactive);
+               link->active_tag = qc->tag;
        }
 
        qc->flags |= ATA_QCFLAG_ACTIVE;
@@ -5606,7 +5683,7 @@ unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
 inline unsigned int ata_host_intr (struct ata_port *ap,
                                   struct ata_queued_cmd *qc)
 {
-       struct ata_eh_info *ehi = &ap->eh_info;
+       struct ata_eh_info *ehi = &ap->link.eh_info;
        u8 status, host_stat = 0;
 
        VPRINTK("ata%u: protocol %d task_state %d\n",
@@ -5680,7 +5757,8 @@ idle_irq:
 
 #ifdef ATA_IRQ_TRAP
        if ((ap->stats.idle_irq % 1000) == 0) {
-               ap->ops->irq_ack(ap, 0); /* debug trap */
+               ata_chk_status(ap);
+               ap->ops->irq_clear(ap);
                ata_port_printk(ap, KERN_WARNING, "irq trap\n");
                return 1;
        }
@@ -5721,7 +5799,7 @@ irqreturn_t ata_interrupt (int irq, void *dev_instance)
                    !(ap->flags & ATA_FLAG_DISABLED)) {
                        struct ata_queued_cmd *qc;
 
-                       qc = ata_qc_from_tag(ap, ap->active_tag);
+                       qc = ata_qc_from_tag(ap, ap->link.active_tag);
                        if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
                            (qc->flags & ATA_QCFLAG_ACTIVE))
                                handled |= ata_host_intr(ap, qc);
@@ -5735,9 +5813,9 @@ irqreturn_t ata_interrupt (int irq, void *dev_instance)
 
 /**
  *     sata_scr_valid - test whether SCRs are accessible
- *     @ap: ATA port to test SCR accessibility for
+ *     @link: ATA link to test SCR accessibility for
  *
- *     Test whether SCRs are accessible for @ap.
+ *     Test whether SCRs are accessible for @link.
  *
  *     LOCKING:
  *     None.
@@ -5745,18 +5823,20 @@ irqreturn_t ata_interrupt (int irq, void *dev_instance)
  *     RETURNS:
  *     1 if SCRs are accessible, 0 otherwise.
  */
-int sata_scr_valid(struct ata_port *ap)
+int sata_scr_valid(struct ata_link *link)
 {
+       struct ata_port *ap = link->ap;
+
        return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
 }
 
 /**
  *     sata_scr_read - read SCR register of the specified port
- *     @ap: ATA port to read SCR for
+ *     @link: ATA link to read SCR for
  *     @reg: SCR to read
  *     @val: Place to store read value
  *
- *     Read SCR register @reg of @ap into *@val.  This function is
+ *     Read SCR register @reg of @link into *@val.  This function is
  *     guaranteed to succeed if the cable type of the port is SATA
  *     and the port implements ->scr_read.
  *
@@ -5766,20 +5846,22 @@ int sata_scr_valid(struct ata_port *ap)
  *     RETURNS:
  *     0 on success, negative errno on failure.
  */
-int sata_scr_read(struct ata_port *ap, int reg, u32 *val)
+int sata_scr_read(struct ata_link *link, int reg, u32 *val)
 {
-       if (sata_scr_valid(ap))
+       struct ata_port *ap = link->ap;
+
+       if (sata_scr_valid(link))
                return ap->ops->scr_read(ap, reg, val);
        return -EOPNOTSUPP;
 }
 
 /**
  *     sata_scr_write - write SCR register of the specified port
- *     @ap: ATA port to write SCR for
+ *     @link: ATA link to write SCR for
  *     @reg: SCR to write
  *     @val: value to write
  *
- *     Write @val to SCR register @reg of @ap.  This function is
+ *     Write @val to SCR register @reg of @link.  This function is
  *     guaranteed to succeed if the cable type of the port is SATA
  *     and the port implements ->scr_read.
  *
@@ -5789,16 +5871,18 @@ int sata_scr_read(struct ata_port *ap, int reg, u32 *val)
  *     RETURNS:
  *     0 on success, negative errno on failure.
  */
-int sata_scr_write(struct ata_port *ap, int reg, u32 val)
+int sata_scr_write(struct ata_link *link, int reg, u32 val)
 {
-       if (sata_scr_valid(ap))
+       struct ata_port *ap = link->ap;
+
+       if (sata_scr_valid(link))
                return ap->ops->scr_write(ap, reg, val);
        return -EOPNOTSUPP;
 }
 
 /**
  *     sata_scr_write_flush - write SCR register of the specified port and flush
- *     @ap: ATA port to write SCR for
+ *     @link: ATA link to write SCR for
  *     @reg: SCR to write
  *     @val: value to write
  *
@@ -5811,11 +5895,12 @@ int sata_scr_write(struct ata_port *ap, int reg, u32 val)
  *     RETURNS:
  *     0 on success, negative errno on failure.
  */
-int sata_scr_write_flush(struct ata_port *ap, int reg, u32 val)
+int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
 {
+       struct ata_port *ap = link->ap;
        int rc;
 
-       if (sata_scr_valid(ap)) {
+       if (sata_scr_valid(link)) {
                rc = ap->ops->scr_write(ap, reg, val);
                if (rc == 0)
                        rc = ap->ops->scr_read(ap, reg, &val);
@@ -5825,12 +5910,12 @@ int sata_scr_write_flush(struct ata_port *ap, int reg, u32 val)
 }
 
 /**
- *     ata_port_online - test whether the given port is online
- *     @ap: ATA port to test
+ *     ata_link_online - test whether the given link is online
+ *     @link: ATA link to test
  *
- *     Test whether @ap is online.  Note that this function returns 0
- *     if online status of @ap cannot be obtained, so
- *     ata_port_online(ap) != !ata_port_offline(ap).
+ *     Test whether @link is online.  Note that this function returns
+ *     0 if online status of @link cannot be obtained, so
+ *     ata_link_online(link) != !ata_link_offline(link).
  *
  *     LOCKING:
  *     None.
@@ -5838,22 +5923,23 @@ int sata_scr_write_flush(struct ata_port *ap, int reg, u32 val)
  *     RETURNS:
  *     1 if the port online status is available and online.
  */
-int ata_port_online(struct ata_port *ap)
+int ata_link_online(struct ata_link *link)
 {
        u32 sstatus;
 
-       if (!sata_scr_read(ap, SCR_STATUS, &sstatus) && (sstatus & 0xf) == 0x3)
+       if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
+           (sstatus & 0xf) == 0x3)
                return 1;
        return 0;
 }
 
 /**
- *     ata_port_offline - test whether the given port is offline
- *     @ap: ATA port to test
+ *     ata_link_offline - test whether the given link is offline
+ *     @link: ATA link to test
  *
- *     Test whether @ap is offline.  Note that this function returns
- *     0 if offline status of @ap cannot be obtained, so
- *     ata_port_online(ap) != !ata_port_offline(ap).
+ *     Test whether @link is offline.  Note that this function
+ *     returns 0 if offline status of @link cannot be obtained, so
+ *     ata_link_online(link) != !ata_link_offline(link).
  *
  *     LOCKING:
  *     None.
@@ -5861,11 +5947,12 @@ int ata_port_online(struct ata_port *ap)
  *     RETURNS:
  *     1 if the port offline status is available and offline.
  */
-int ata_port_offline(struct ata_port *ap)
+int ata_link_offline(struct ata_link *link)
 {
        u32 sstatus;
 
-       if (!sata_scr_read(ap, SCR_STATUS, &sstatus) && (sstatus & 0xf) != 0x3)
+       if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
+           (sstatus & 0xf) != 0x3)
                return 1;
        return 0;
 }
@@ -5883,6 +5970,10 @@ int ata_flush_cache(struct ata_device *dev)
        else
                cmd = ATA_CMD_FLUSH;
 
+       /* This is wrong. On a failed flush we get back the LBA of the lost
+          sector and we should (assuming it wasn't aborted as unknown) issue
+          a further flush command to continue the writeback until it 
+          does not error */
        err_mask = ata_do_simple_cmd(dev, cmd);
        if (err_mask) {
                ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
@@ -5902,6 +5993,7 @@ static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
 
        for (i = 0; i < host->n_ports; i++) {
                struct ata_port *ap = host->ports[i];
+               struct ata_link *link;
 
                /* Previous resume operation might still be in
                 * progress.  Wait for PM_PENDING to clear.
@@ -5921,8 +6013,10 @@ static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
                }
 
                ap->pflags |= ATA_PFLAG_PM_PENDING;
-               ap->eh_info.action |= action;
-               ap->eh_info.flags |= ehi_flags;
+               __ata_port_for_each_link(link, ap) {
+                       link->eh_info.action |= action;
+                       link->eh_info.flags |= ehi_flags;
+               }
 
                ata_port_schedule_eh(ap);
 
@@ -6026,12 +6120,13 @@ int ata_port_start(struct ata_port *ap)
  */
 void ata_dev_init(struct ata_device *dev)
 {
-       struct ata_port *ap = dev->ap;
+       struct ata_link *link = dev->link;
+       struct ata_port *ap = link->ap;
        unsigned long flags;
 
        /* SATA spd limit is bound to the first device */
-       ap->sata_spd_limit = ap->hw_sata_spd_limit;
-       ap->sata_spd = 0;
+       link->sata_spd_limit = link->hw_sata_spd_limit;
+       link->sata_spd = 0;
 
        /* High bits of dev->flags are used to record warm plug
         * requests which occur asynchronously.  Synchronize using
@@ -6049,6 +6144,70 @@ void ata_dev_init(struct ata_device *dev)
        dev->udma_mask = UINT_MAX;
 }
 
+/**
+ *     ata_link_init - Initialize an ata_link structure
+ *     @ap: ATA port link is attached to
+ *     @link: Link structure to initialize
+ *     @pmp: Port multiplier port number
+ *
+ *     Initialize @link.
+ *
+ *     LOCKING:
+ *     Kernel thread context (may sleep)
+ */
+static void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
+{
+       int i;
+
+       /* clear everything except for devices */
+       memset(link, 0, offsetof(struct ata_link, device[0]));
+
+       link->ap = ap;
+       link->pmp = pmp;
+       link->active_tag = ATA_TAG_POISON;
+       link->hw_sata_spd_limit = UINT_MAX;
+
+       /* can't use iterator, ap isn't initialized yet */
+       for (i = 0; i < ATA_MAX_DEVICES; i++) {
+               struct ata_device *dev = &link->device[i];
+
+               dev->link = link;
+               dev->devno = dev - link->device;
+               ata_dev_init(dev);
+       }
+}
+
+/**
+ *     sata_link_init_spd - Initialize link->sata_spd_limit
+ *     @link: Link to configure sata_spd_limit for
+ *
+ *     Initialize @link->[hw_]sata_spd_limit to the currently
+ *     configured value.
+ *
+ *     LOCKING:
+ *     Kernel thread context (may sleep).
+ *
+ *     RETURNS:
+ *     0 on success, -errno on failure.
+ */
+static int sata_link_init_spd(struct ata_link *link)
+{
+       u32 scontrol, spd;
+       int rc;
+
+       rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
+       if (rc)
+               return rc;
+
+       spd = (scontrol >> 4) & 0xf;
+       if (spd)
+               link->hw_sata_spd_limit &= (1 << spd) - 1;
+
+       link->sata_spd_limit = link->hw_sata_spd_limit;
+
+       return 0;
+}
+
 /**
  *     ata_port_alloc - allocate and initialize basic ATA port resources
  *     @host: ATA host this allocated port belongs to
@@ -6064,7 +6223,6 @@ void ata_dev_init(struct ata_device *dev)
 struct ata_port *ata_port_alloc(struct ata_host *host)
 {
        struct ata_port *ap;
-       unsigned int i;
 
        DPRINTK("ENTER\n");
 
@@ -6079,9 +6237,6 @@ struct ata_port *ata_port_alloc(struct ata_host *host)
        ap->ctl = ATA_DEVCTL_OBS;
        ap->host = host;
        ap->dev = host->dev;
-
-       ap->hw_sata_spd_limit = UINT_MAX;
-       ap->active_tag = ATA_TAG_POISON;
        ap->last_ctl = 0xFF;
 
 #if defined(ATA_VERBOSE_DEBUG)
@@ -6104,12 +6259,7 @@ struct ata_port *ata_port_alloc(struct ata_host *host)
 
        ap->cbl = ATA_CBL_NONE;
 
-       for (i = 0; i < ATA_MAX_DEVICES; i++) {
-               struct ata_device *dev = &ap->device[i];
-               dev->ap = ap;
-               dev->devno = i;
-               ata_dev_init(dev);
-       }
+       ata_link_init(ap, &ap->link, 0);
 
 #ifdef ATA_IRQ_TRAP
        ap->stats.unhandled_irq = 1;
@@ -6255,6 +6405,7 @@ struct ata_host *ata_host_alloc_pinfo(struct device *dev,
                ap->mwdma_mask = pi->mwdma_mask;
                ap->udma_mask = pi->udma_mask;
                ap->flags |= pi->flags;
+               ap->link.flags |= pi->link_flags;
                ap->ops = pi->port_ops;
 
                if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
@@ -6391,7 +6542,6 @@ int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
        for (i = 0; i < host->n_ports; i++) {
                struct ata_port *ap = host->ports[i];
                int irq_line;
-               u32 scontrol;
                unsigned long xfer_mask;
 
                /* set SATA cable type if still unset */
@@ -6399,12 +6549,7 @@ int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
                        ap->cbl = ATA_CBL_SATA;
 
                /* init sata_spd_limit to the current value */
-               if (sata_scr_read(ap, SCR_CONTROL, &scontrol) == 0) {
-                       int spd = (scontrol >> 4) & 0xf;
-                       if (spd)
-                               ap->hw_sata_spd_limit &= (1 << spd) - 1;
-               }
-               ap->sata_spd_limit = ap->hw_sata_spd_limit;
+               sata_link_init_spd(&ap->link);
 
                /* report the secondary IRQ for second channel legacy */
                irq_line = host->irq;
@@ -6436,7 +6581,7 @@ int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
 
                /* probe */
                if (ap->ops->error_handler) {
-                       struct ata_eh_info *ehi = &ap->eh_info;
+                       struct ata_eh_info *ehi = &ap->link.eh_info;
                        unsigned long flags;
 
                        ata_port_probe(ap);
@@ -6444,7 +6589,8 @@ int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
                        /* kick EH for boot probing */
                        spin_lock_irqsave(ap->lock, flags);
 
-                       ehi->probe_mask = (1 << ATA_MAX_DEVICES) - 1;
+                       ehi->probe_mask =
+                               (1 << ata_link_max_devices(&ap->link)) - 1;
                        ehi->action |= ATA_EH_SOFTRESET;
                        ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
 
@@ -6542,7 +6688,8 @@ int ata_host_activate(struct ata_host *host, int irq,
 void ata_port_detach(struct ata_port *ap)
 {
        unsigned long flags;
-       int i;
+       struct ata_link *link;
+       struct ata_device *dev;
 
        if (!ap->ops->error_handler)
                goto skip_eh;
@@ -6559,8 +6706,10 @@ void ata_port_detach(struct ata_port *ap)
         */
        spin_lock_irqsave(ap->lock, flags);
 
-       for (i = 0; i < ATA_MAX_DEVICES; i++)
-               ata_dev_disable(&ap->device[i]);
+       ata_port_for_each_link(link, ap) {
+               ata_link_for_each_dev(dev, link)
+                       ata_dev_disable(dev);
+       }
 
        spin_unlock_irqrestore(ap->lock, flags);
 
@@ -6847,7 +6996,6 @@ static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
 }
 
 const struct ata_port_operations ata_dummy_port_ops = {
-       .port_disable           = ata_port_disable,
        .check_status           = ata_dummy_check_status,
        .check_altstatus        = ata_dummy_check_status,
        .dev_select             = ata_noop_dev_select,
@@ -6925,14 +7073,14 @@ EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
 EXPORT_SYMBOL_GPL(ata_port_probe);
 EXPORT_SYMBOL_GPL(ata_dev_disable);
 EXPORT_SYMBOL_GPL(sata_set_spd);
-EXPORT_SYMBOL_GPL(sata_phy_debounce);
-EXPORT_SYMBOL_GPL(sata_phy_resume);
+EXPORT_SYMBOL_GPL(sata_link_debounce);
+EXPORT_SYMBOL_GPL(sata_link_resume);
 EXPORT_SYMBOL_GPL(sata_phy_reset);
 EXPORT_SYMBOL_GPL(__sata_phy_reset);
 EXPORT_SYMBOL_GPL(ata_bus_reset);
 EXPORT_SYMBOL_GPL(ata_std_prereset);
 EXPORT_SYMBOL_GPL(ata_std_softreset);
-EXPORT_SYMBOL_GPL(sata_port_hardreset);
+EXPORT_SYMBOL_GPL(sata_link_hardreset);
 EXPORT_SYMBOL_GPL(sata_std_hardreset);
 EXPORT_SYMBOL_GPL(ata_std_postreset);
 EXPORT_SYMBOL_GPL(ata_dev_classify);
@@ -6953,8 +7101,8 @@ EXPORT_SYMBOL_GPL(sata_scr_valid);
 EXPORT_SYMBOL_GPL(sata_scr_read);
 EXPORT_SYMBOL_GPL(sata_scr_write);
 EXPORT_SYMBOL_GPL(sata_scr_write_flush);
-EXPORT_SYMBOL_GPL(ata_port_online);
-EXPORT_SYMBOL_GPL(ata_port_offline);
+EXPORT_SYMBOL_GPL(ata_link_online);
+EXPORT_SYMBOL_GPL(ata_link_offline);
 #ifdef CONFIG_PM
 EXPORT_SYMBOL_GPL(ata_host_suspend);
 EXPORT_SYMBOL_GPL(ata_host_resume);
@@ -6990,6 +7138,7 @@ EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
 EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
 EXPORT_SYMBOL_GPL(ata_eng_timeout);
 EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
+EXPORT_SYMBOL_GPL(ata_link_abort);
 EXPORT_SYMBOL_GPL(ata_port_abort);
 EXPORT_SYMBOL_GPL(ata_port_freeze);
 EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
@@ -6998,9 +7147,6 @@ EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
 EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
 EXPORT_SYMBOL_GPL(ata_do_eh);
 EXPORT_SYMBOL_GPL(ata_irq_on);
-EXPORT_SYMBOL_GPL(ata_dummy_irq_on);
-EXPORT_SYMBOL_GPL(ata_irq_ack);
-EXPORT_SYMBOL_GPL(ata_dummy_irq_ack);
 EXPORT_SYMBOL_GPL(ata_dev_try_classify);
 
 EXPORT_SYMBOL_GPL(ata_cable_40wire);