libata: reimplement link iterator
[safe/jmp/linux-2.6] / drivers / ata / libata-core.c
index b2d5d63..d156616 100644 (file)
 #include <linux/completion.h>
 #include <linux/suspend.h>
 #include <linux/workqueue.h>
-#include <linux/jiffies.h>
 #include <linux/scatterlist.h>
 #include <linux/io.h>
 #include <scsi/scsi.h>
 #include <scsi/scsi_cmnd.h>
 #include <scsi/scsi_host.h>
 #include <linux/libata.h>
-#include <asm/semaphore.h>
 #include <asm/byteorder.h>
 #include <linux/cdrom.h>
 
@@ -106,6 +104,7 @@ struct ata_force_param {
        unsigned long   xfer_mask;
        unsigned int    horkage_on;
        unsigned int    horkage_off;
+       unsigned int    lflags;
 };
 
 struct ata_force_ent {
@@ -122,7 +121,7 @@ static char ata_force_param_buf[PAGE_SIZE] __initdata;
 module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
 MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/kernel-parameters.txt for details)");
 
-int atapi_enabled = 1;
+static int atapi_enabled = 1;
 module_param(atapi_enabled, int, 0444);
 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
 
@@ -146,7 +145,7 @@ static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CF
 module_param_named(dma, libata_dma_mask, int, 0444);
 MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
 
-static int ata_probe_timeout = ATA_TMOUT_INTERNAL / HZ;
+static int ata_probe_timeout;
 module_param(ata_probe_timeout, int, 0444);
 MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
 
@@ -164,6 +163,35 @@ MODULE_LICENSE("GPL");
 MODULE_VERSION(DRV_VERSION);
 
 
+/*
+ * Iterator helpers.  Don't use directly.
+ *
+ * LOCKING:
+ * Host lock or EH context.
+ */
+struct ata_link *__ata_port_next_link(struct ata_port *ap,
+                                     struct ata_link *link, bool dev_only)
+{
+       /* NULL link indicates start of iteration */
+       if (!link) {
+               if (dev_only && sata_pmp_attached(ap))
+                       return ap->pmp_link;
+               return &ap->link;
+       }
+
+       /* we just iterated over the host link, what's next? */
+       if (ata_is_host_link(link)) {
+               if (!sata_pmp_attached(ap))
+                       return NULL;
+               return ap->pmp_link;
+       }
+
+       /* iterate to the next PMP link */
+       if (++link < ap->pmp_link + ap->nr_pmp_links)
+               return link;
+       return NULL;
+}
+
 /**
  *     ata_force_cbl - force cable type according to libata.force
  *     @ap: ATA port of interest
@@ -198,22 +226,23 @@ void ata_force_cbl(struct ata_port *ap)
 }
 
 /**
- *     ata_force_spd_limit - force SATA spd limit according to libata.force
+ *     ata_force_link_limits - force link limits according to libata.force
  *     @link: ATA link of interest
  *
- *     Force SATA spd limit according to libata.force and whine about
- *     it.  When only the port part is specified (e.g. 1:), the limit
- *     applies to all links connected to both the host link and all
- *     fan-out ports connected via PMP.  If the device part is
- *     specified as 0 (e.g. 1.00:), it specifies the first fan-out
- *     link not the host link.  Device number 15 always points to the
- *     host link whether PMP is attached or not.
+ *     Force link flags and SATA spd limit according to libata.force
+ *     and whine about it.  When only the port part is specified
+ *     (e.g. 1:), the limit applies to all links connected to both
+ *     the host link and all fan-out ports connected via PMP.  If the
+ *     device part is specified as 0 (e.g. 1.00:), it specifies the
+ *     first fan-out link not the host link.  Device number 15 always
+ *     points to the host link whether PMP is attached or not.
  *
  *     LOCKING:
  *     EH context.
  */
-static void ata_force_spd_limit(struct ata_link *link)
+static void ata_force_link_limits(struct ata_link *link)
 {
+       bool did_spd = false;
        int linkno, i;
 
        if (ata_is_host_link(link))
@@ -230,13 +259,22 @@ static void ata_force_spd_limit(struct ata_link *link)
                if (fe->device != -1 && fe->device != linkno)
                        continue;
 
-               if (!fe->param.spd_limit)
-                       continue;
+               /* only honor the first spd limit */
+               if (!did_spd && fe->param.spd_limit) {
+                       link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
+                       ata_link_printk(link, KERN_NOTICE,
+                                       "FORCE: PHY spd limit set to %s\n",
+                                       fe->param.name);
+                       did_spd = true;
+               }
 
-               link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
-               ata_link_printk(link, KERN_NOTICE,
-                       "FORCE: PHY spd limit set to %s\n", fe->param.name);
-               return;
+               /* let lflags stack */
+               if (fe->param.lflags) {
+                       link->flags |= fe->param.lflags;
+                       ata_link_printk(link, KERN_NOTICE,
+                                       "FORCE: link flag 0x%x forced -> 0x%x\n",
+                                       fe->param.lflags, link->flags);
+               }
        }
 }
 
@@ -1134,6 +1172,8 @@ void ata_id_string(const u16 *id, unsigned char *s,
 {
        unsigned int c;
 
+       BUG_ON(len & 1);
+
        while (len > 0) {
                c = id[ofs] >> 8;
                *s = c;
@@ -1167,8 +1207,6 @@ void ata_id_c_string(const u16 *id, unsigned char *s,
 {
        unsigned char *p;
 
-       WARN_ON(!(len & 1));
-
        ata_id_string(id, s, ofs, len - 1);
 
        p = s + strnlen(s, len - 1);
@@ -1534,7 +1572,7 @@ unsigned long ata_id_xfermask(const u16 *id)
  *     @ap: The ata_port to queue port_task for
  *     @fn: workqueue function to be scheduled
  *     @data: data for @fn to use
- *     @delay: delay time for workqueue function
+ *     @delay: delay time in msecs for workqueue function
  *
  *     Schedule @fn(@data) for execution after @delay jiffies using
  *     port_task.  There is one port_task per port and it's the
@@ -1553,7 +1591,7 @@ void ata_pio_queue_task(struct ata_port *ap, void *data, unsigned long delay)
        ap->port_task_data = data;
 
        /* may fail if ata_port_flush_task() in progress */
-       queue_delayed_work(ata_wq, &ap->port_task, delay);
+       queue_delayed_work(ata_wq, &ap->port_task, msecs_to_jiffies(delay));
 }
 
 /**
@@ -1613,6 +1651,7 @@ unsigned ata_exec_internal_sg(struct ata_device *dev,
        struct ata_link *link = dev->link;
        struct ata_port *ap = link->ap;
        u8 command = tf->command;
+       int auto_timeout = 0;
        struct ata_queued_cmd *qc;
        unsigned int tag, preempted_tag;
        u32 preempted_sactive, preempted_qc_active;
@@ -1685,8 +1724,14 @@ unsigned ata_exec_internal_sg(struct ata_device *dev,
 
        spin_unlock_irqrestore(ap->lock, flags);
 
-       if (!timeout)
-               timeout = ata_probe_timeout * 1000 / HZ;
+       if (!timeout) {
+               if (ata_probe_timeout)
+                       timeout = ata_probe_timeout * 1000;
+               else {
+                       timeout = ata_internal_cmd_timeout(dev, command);
+                       auto_timeout = 1;
+               }
+       }
 
        rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
 
@@ -1762,6 +1807,9 @@ unsigned ata_exec_internal_sg(struct ata_device *dev,
 
        spin_unlock_irqrestore(ap->lock, flags);
 
+       if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
+               ata_internal_cmd_timed_out(dev, command);
+
        return err_mask;
 }
 
@@ -1878,6 +1926,23 @@ static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
 }
 
 /**
+ *     ata_do_dev_read_id              -       default ID read method
+ *     @dev: device
+ *     @tf: proposed taskfile
+ *     @id: data buffer
+ *
+ *     Issue the identify taskfile and hand back the buffer containing
+ *     identify data. For some RAID controllers and for pre ATA devices
+ *     this function is wrapped or replaced by the driver
+ */
+unsigned int ata_do_dev_read_id(struct ata_device *dev,
+                                       struct ata_taskfile *tf, u16 *id)
+{
+       return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
+                                    id, sizeof(id[0]) * ATA_ID_WORDS, 0);
+}
+
+/**
  *     ata_dev_read_id - Read ID data from the specified device
  *     @dev: target device
  *     @p_class: pointer to class of the target device (may be changed)
@@ -1912,7 +1977,7 @@ int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
        if (ata_msg_ctl(ap))
                ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
 
- retry:
+retry:
        ata_tf_init(dev, &tf);
 
        switch (class) {
@@ -1940,8 +2005,11 @@ int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
         */
        tf.flags |= ATA_TFLAG_POLLING;
 
-       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
-                                    id, sizeof(id[0]) * ATA_ID_WORDS, 0);
+       if (ap->ops->read_id)
+               err_mask = ap->ops->read_id(dev, &tf, id);
+       else
+               err_mask = ata_do_dev_read_id(dev, &tf, id);
+
        if (err_mask) {
                if (err_mask & AC_ERR_NODEV_HINT) {
                        ata_dev_printk(dev, KERN_DEBUG,
@@ -2127,6 +2195,23 @@ int ata_dev_configure(struct ata_device *dev)
        dev->horkage |= ata_dev_blacklisted(dev);
        ata_force_horkage(dev);
 
+       if (dev->horkage & ATA_HORKAGE_DISABLE) {
+               ata_dev_printk(dev, KERN_INFO,
+                              "unsupported device, disabling\n");
+               ata_dev_disable(dev);
+               return 0;
+       }
+
+       if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
+           dev->class == ATA_DEV_ATAPI) {
+               ata_dev_printk(dev, KERN_WARNING,
+                       "WARNING: ATAPI is %s, device ignored.\n",
+                       atapi_enabled ? "not supported with this driver"
+                                     : "disabled");
+               ata_dev_disable(dev);
+               return 0;
+       }
+
        /* let ACPI work its magic */
        rc = ata_acpi_on_devcfg(dev);
        if (rc)
@@ -2278,7 +2363,7 @@ int ata_dev_configure(struct ata_device *dev)
                 * changed notifications and ATAPI ANs.
                 */
                if ((ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
-                   (!ap->nr_pmp_links ||
+                   (!sata_pmp_attached(ap) ||
                     sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
                        unsigned int err_mask;
 
@@ -2617,7 +2702,7 @@ void ata_port_probe(struct ata_port *ap)
  *     LOCKING:
  *     None.
  */
-void sata_print_link_status(struct ata_link *link)
+static void sata_print_link_status(struct ata_link *link)
 {
        u32 sstatus, scontrol, tmp;
 
@@ -2773,7 +2858,7 @@ static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
  *     RETURNS:
  *     1 if SATA spd configuration is needed, 0 otherwise.
  */
-int sata_set_spd_needed(struct ata_link *link)
+static int sata_set_spd_needed(struct ata_link *link)
 {
        u32 scontrol;
 
@@ -3132,16 +3217,21 @@ static int ata_dev_set_mode(struct ata_device *dev)
        if (rc)
                return rc;
 
-       /* Old CFA may refuse this command, which is just fine */
-       if (dev->xfer_shift == ATA_SHIFT_PIO && ata_id_is_cfa(dev->id))
-               ign_dev_err = 1;
-
-       /* 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)
-               ign_dev_err = 1;
-
+       if (dev->xfer_shift == ATA_SHIFT_PIO) {
+               /* Old CFA may refuse this command, which is just fine */
+               if (ata_id_is_cfa(dev->id))
+                       ign_dev_err = 1;
+               /* Catch several broken garbage emulations plus some pre
+                  ATA devices */
+               if (ata_id_major_version(dev->id) == 0 &&
+                                       dev->pio_mode <= XFER_PIO_2)
+                       ign_dev_err = 1;
+               /* 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 (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
+                       ign_dev_err = 1;
+       }
        /* Early MWDMA devices do DMA but don't allow DMA mode setting.
           Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
        if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
@@ -3227,7 +3317,7 @@ int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
                dev->dma_mode = ata_xfer_mask2mode(dma_mask);
 
                found = 1;
-               if (dev->dma_mode != 0xff)
+               if (ata_dma_enabled(dev))
                        used_dma = 1;
        }
        if (!found)
@@ -3252,7 +3342,7 @@ int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
 
        /* step 3: set host DMA timings */
        ata_link_for_each_dev(dev, link) {
-               if (!ata_dev_enabled(dev) || dev->dma_mode == 0xff)
+               if (!ata_dev_enabled(dev) || !ata_dma_enabled(dev))
                        continue;
 
                dev->xfer_mode = dev->dma_mode;
@@ -3308,7 +3398,7 @@ int ata_wait_ready(struct ata_link *link, unsigned long deadline,
                   int (*check_ready)(struct ata_link *link))
 {
        unsigned long start = jiffies;
-       unsigned long nodev_deadline = start + ATA_TMOUT_FF_WAIT;
+       unsigned long nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
        int warned = 0;
 
        if (time_after(nodev_deadline, deadline))
@@ -3373,10 +3463,10 @@ int ata_wait_ready(struct ata_link *link, unsigned long deadline,
  *     RETURNS:
  *     0 if @linke is ready before @deadline; otherwise, -errno.
  */
-extern int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
+int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
                                int (*check_ready)(struct ata_link *link))
 {
-       msleep(ATA_WAIT_AFTER_RESET_MSECS);
+       msleep(ATA_WAIT_AFTER_RESET);
 
        return ata_wait_ready(link, deadline, check_ready);
 }
@@ -3406,13 +3496,13 @@ extern int ata_wait_after_reset(struct ata_link *link, 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]);
+       unsigned long interval = params[0];
+       unsigned long duration = params[1];
        unsigned long last_jiffies, t;
        u32 last, cur;
        int rc;
 
-       t = jiffies + msecs_to_jiffies(params[2]);
+       t = ata_deadline(jiffies, params[2]);
        if (time_before(t, deadline))
                deadline = t;
 
@@ -3424,7 +3514,7 @@ int sata_link_debounce(struct ata_link *link, const unsigned long *params,
        last_jiffies = jiffies;
 
        while (1) {
-               msleep(interval_msec);
+               msleep(interval);
                if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
                        return rc;
                cur &= 0xf;
@@ -3433,7 +3523,8 @@ int sata_link_debounce(struct ata_link *link, const unsigned long *params,
                if (cur == last) {
                        if (cur == 1 && time_before(jiffies, deadline))
                                continue;
-                       if (time_after(jiffies, last_jiffies + duration))
+                       if (time_after(jiffies,
+                                      ata_deadline(last_jiffies, duration)))
                                return 0;
                        continue;
                }
@@ -3486,22 +3577,11 @@ int sata_link_resume(struct ata_link *link, const unsigned long *params,
        if ((rc = sata_link_debounce(link, params, deadline)))
                return rc;
 
-       /* Clear SError.  PMP and some host PHYs require this to
-        * operate and clearing should be done before checking PHY
-        * online status to avoid race condition (hotplugging between
-        * link resume and status check).
-        */
+       /* clear SError, some PHYs require this even for SRST to work */
        if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
                rc = sata_scr_write(link, SCR_ERROR, serror);
-       if (rc == 0 || rc == -EINVAL) {
-               unsigned long flags;
 
-               spin_lock_irqsave(link->ap->lock, flags);
-               link->eh_info.serror = 0;
-               spin_unlock_irqrestore(link->ap->lock, flags);
-               rc = 0;
-       }
-       return rc;
+       return rc != -EINVAL ? rc : 0;
 }
 
 /**
@@ -3541,6 +3621,10 @@ int ata_std_prereset(struct ata_link *link, unsigned long deadline)
                                        "link for reset (errno=%d)\n", rc);
        }
 
+       /* no point in trying softreset on offline link */
+       if (ata_link_offline(link))
+               ehc->i.action &= ~ATA_EH_SOFTRESET;
+
        return 0;
 }
 
@@ -3623,7 +3707,7 @@ int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
        if (online)
                *online = true;
 
-       if ((link->ap->flags & ATA_FLAG_PMP) && ata_is_host_link(link)) {
+       if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
                /* If PMP is supported, we have to do follow-up SRST.
                 * Some PMPs don't send D2H Reg FIS after hardreset if
                 * the first port is empty.  Wait only for
@@ -3632,7 +3716,8 @@ int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
                if (check_ready) {
                        unsigned long pmp_deadline;
 
-                       pmp_deadline = jiffies + ATA_TMOUT_PMP_SRST_WAIT;
+                       pmp_deadline = ata_deadline(jiffies,
+                                                   ATA_TMOUT_PMP_SRST_WAIT);
                        if (time_after(pmp_deadline, deadline))
                                pmp_deadline = deadline;
                        ata_wait_ready(link, pmp_deadline, check_ready);
@@ -3645,9 +3730,13 @@ int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
        if (check_ready)
                rc = ata_wait_ready(link, deadline, check_ready);
  out:
-       if (rc && rc != -EAGAIN)
+       if (rc && rc != -EAGAIN) {
+               /* online is set iff link is online && reset succeeded */
+               if (online)
+                       *online = false;
                ata_link_printk(link, KERN_ERR,
                                "COMRESET failed (errno=%d)\n", rc);
+       }
        DPRINTK("EXIT, rc=%d\n", rc);
        return rc;
 }
@@ -3692,8 +3781,14 @@ int sata_std_hardreset(struct ata_link *link, unsigned int *class,
  */
 void ata_std_postreset(struct ata_link *link, unsigned int *classes)
 {
+       u32 serror;
+
        DPRINTK("ENTER\n");
 
+       /* reset complete, clear SError */
+       if (!sata_scr_read(link, SCR_ERROR, &serror))
+               sata_scr_write(link, SCR_ERROR, serror);
+
        /* print link status */
        sata_print_link_status(link);
 
@@ -3886,8 +3981,7 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
        { "SAMSUNG CD-ROM SN-124", "N001",      ATA_HORKAGE_NODMA },
        { "Seagate STT20000A", NULL,            ATA_HORKAGE_NODMA },
        /* Odd clown on sil3726/4726 PMPs */
-       { "Config  Disk",       NULL,           ATA_HORKAGE_NODMA |
-                                               ATA_HORKAGE_SKIP_PM },
+       { "Config  Disk",       NULL,           ATA_HORKAGE_DISABLE },
 
        /* Weird ATAPI devices */
        { "TORiSAN DVD-ROM DRD-N216", NULL,     ATA_HORKAGE_MAX_SEC_128 },
@@ -3925,6 +4019,9 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
 
        /* Devices which get the IVB wrong */
        { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
+       /* Maybe we should just blacklist TSSTcorp... */
+       { "TSSTcorp CDDVDW SH-S202H", "SB00",     ATA_HORKAGE_IVB, },
+       { "TSSTcorp CDDVDW SH-S202H", "SB01",     ATA_HORKAGE_IVB, },
        { "TSSTcorp CDDVDW SH-S202J", "SB00",     ATA_HORKAGE_IVB, },
        { "TSSTcorp CDDVDW SH-S202J", "SB01",     ATA_HORKAGE_IVB, },
        { "TSSTcorp CDDVDW SH-S202N", "SB00",     ATA_HORKAGE_IVB, },
@@ -4029,6 +4126,10 @@ static int cable_is_40wire(struct ata_port *ap)
        /* If the controller thinks we are 80 wire, we are */
        if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
                return 0;
+       /* If the system is known to be 40 wire short cable (eg laptop),
+          then we allow 80 wire modes even if the drive isn't sure */
+       if (ap->cbl == ATA_CBL_PATA40_SHORT)
+               return 0;
        /* If the controller doesn't know we scan
 
           - Note: We look for all 40 wire detects at this point.
@@ -4277,7 +4378,7 @@ void ata_sg_clean(struct ata_queued_cmd *qc)
 }
 
 /**
- *     ata_check_atapi_dma - Check whether ATAPI DMA can be supported
+ *     atapi_check_dma - Check whether ATAPI DMA can be supported
  *     @qc: Metadata associated with taskfile to check
  *
  *     Allow low-level driver to filter ATA PACKET commands, returning
@@ -4290,7 +4391,7 @@ void ata_sg_clean(struct ata_queued_cmd *qc)
  *     RETURNS: 0 when ATAPI DMA can be used
  *               nonzero otherwise
  */
-int ata_check_atapi_dma(struct ata_queued_cmd *qc)
+int atapi_check_dma(struct ata_queued_cmd *qc)
 {
        struct ata_port *ap = qc->ap;
 
@@ -4796,10 +4897,8 @@ int sata_scr_valid(struct ata_link *link)
 int sata_scr_read(struct ata_link *link, int reg, u32 *val)
 {
        if (ata_is_host_link(link)) {
-               struct ata_port *ap = link->ap;
-
                if (sata_scr_valid(link))
-                       return ap->ops->scr_read(ap, reg, val);
+                       return link->ap->ops->scr_read(link, reg, val);
                return -EOPNOTSUPP;
        }
 
@@ -4825,10 +4924,8 @@ int sata_scr_read(struct ata_link *link, int reg, u32 *val)
 int sata_scr_write(struct ata_link *link, int reg, u32 val)
 {
        if (ata_is_host_link(link)) {
-               struct ata_port *ap = link->ap;
-
                if (sata_scr_valid(link))
-                       return ap->ops->scr_write(ap, reg, val);
+                       return link->ap->ops->scr_write(link, reg, val);
                return -EOPNOTSUPP;
        }
 
@@ -4853,13 +4950,12 @@ int sata_scr_write(struct ata_link *link, int reg, u32 val)
 int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
 {
        if (ata_is_host_link(link)) {
-               struct ata_port *ap = link->ap;
                int rc;
 
                if (sata_scr_valid(link)) {
-                       rc = ap->ops->scr_write(ap, reg, val);
+                       rc = link->ap->ops->scr_write(link, reg, val);
                        if (rc == 0)
-                               rc = ap->ops->scr_read(ap, reg, &val);
+                               rc = link->ap->ops->scr_read(link, reg, &val);
                        return rc;
                }
                return -EOPNOTSUPP;
@@ -5127,19 +5223,18 @@ void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
  */
 int sata_link_init_spd(struct ata_link *link)
 {
-       u32 scontrol;
        u8 spd;
        int rc;
 
-       rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
+       rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
        if (rc)
                return rc;
 
-       spd = (scontrol >> 4) & 0xf;
+       spd = (link->saved_scontrol >> 4) & 0xf;
        if (spd)
                link->hw_sata_spd_limit &= (1 << spd) - 1;
 
-       ata_force_spd_limit(link);
+       ata_force_link_limits(link);
 
        link->sata_spd_limit = link->hw_sata_spd_limit;
 
@@ -5383,7 +5478,7 @@ static void ata_host_stop(struct device *gendev, void *res)
  */
 static void ata_finalize_port_ops(struct ata_port_operations *ops)
 {
-       static spinlock_t lock = SPIN_LOCK_UNLOCKED;
+       static DEFINE_SPINLOCK(lock);
        const struct ata_port_operations *cur;
        void **begin = (void **)ops;
        void **end = (void **)&ops->inherits;
@@ -5601,7 +5696,7 @@ int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
                        spin_lock_irqsave(ap->lock, flags);
 
                        ehi->probe_mask |= ATA_ALL_DEVICES;
-                       ehi->action |= ATA_EH_RESET;
+                       ehi->action |= ATA_EH_RESET | ATA_EH_LPM;
                        ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
 
                        ap->pflags &= ~ATA_PFLAG_INITIALIZING;
@@ -5634,7 +5729,6 @@ int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
                struct ata_port *ap = host->ports[i];
 
                ata_scsi_scan_host(ap, 1);
-               ata_lpm_schedule(ap, ap->pm_policy);
        }
 
        return 0;
@@ -5723,9 +5817,10 @@ static void ata_port_detach(struct ata_port *ap)
        ata_port_wait_eh(ap);
 
        /* EH is now guaranteed to see UNLOADING - EH context belongs
-        * to us.  Disable all existing devices.
+        * to us.  Restore SControl and disable all existing devices.
         */
-       ata_port_for_each_link(link, ap) {
+       __ata_port_for_each_link(link, ap) {
+               sata_scr_write(link, SCR_CONTROL, link->saved_scontrol);
                ata_link_for_each_dev(dev, link)
                        ata_dev_disable(dev);
        }
@@ -5931,6 +6026,9 @@ static int __init ata_parse_force_one(char **cur,
                { "udma133",    .xfer_mask      = 1 << (ATA_SHIFT_UDMA + 6) },
                { "udma/133",   .xfer_mask      = 1 << (ATA_SHIFT_UDMA + 6) },
                { "udma7",      .xfer_mask      = 1 << (ATA_SHIFT_UDMA + 7) },
+               { "nohrst",     .lflags         = ATA_LFLAG_NO_HRST },
+               { "nosrst",     .lflags         = ATA_LFLAG_NO_SRST },
+               { "norst",      .lflags         = ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
        };
        char *start = *cur, *p = *cur;
        char *id, *val, *endp;
@@ -6054,22 +6152,24 @@ static void __init ata_parse_force_param(void)
 
 static int __init ata_init(void)
 {
-       ata_probe_timeout *= HZ;
-
        ata_parse_force_param();
 
        ata_wq = create_workqueue("ata");
        if (!ata_wq)
-               return -ENOMEM;
+               goto free_force_tbl;
 
        ata_aux_wq = create_singlethread_workqueue("ata_aux");
-       if (!ata_aux_wq) {
-               destroy_workqueue(ata_wq);
-               return -ENOMEM;
-       }
+       if (!ata_aux_wq)
+               goto free_wq;
 
        printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
        return 0;
+
+free_wq:
+       destroy_workqueue(ata_wq);
+free_force_tbl:
+       kfree(ata_force_tbl);
+       return -ENOMEM;
 }
 
 static void __exit ata_exit(void)
@@ -6108,8 +6208,8 @@ int ata_ratelimit(void)
  *     @reg: IO-mapped register
  *     @mask: Mask to apply to read register value
  *     @val: Wait condition
- *     @interval_msec: polling interval in milliseconds
- *     @timeout_msec: timeout in milliseconds
+ *     @interval: polling interval in milliseconds
+ *     @timeout: timeout in milliseconds
  *
  *     Waiting for some bits of register to change is a common
  *     operation for ATA controllers.  This function reads 32bit LE
@@ -6127,10 +6227,9 @@ int ata_ratelimit(void)
  *     The final register value.
  */
 u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
-                     unsigned long interval_msec,
-                     unsigned long timeout_msec)
+                     unsigned long interval, unsigned long timeout)
 {
-       unsigned long timeout;
+       unsigned long deadline;
        u32 tmp;
 
        tmp = ioread32(reg);
@@ -6139,10 +6238,10 @@ u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
         * preceding writes reach the controller before starting to
         * eat away the timeout.
         */
-       timeout = jiffies + (timeout_msec * HZ) / 1000;
+       deadline = ata_deadline(jiffies, timeout);
 
-       while ((tmp & mask) == val && time_before(jiffies, timeout)) {
-               msleep(interval_msec);
+       while ((tmp & mask) == val && time_before(jiffies, deadline)) {
+               msleep(interval);
                tmp = ioread32(reg);
        }
 
@@ -6185,6 +6284,7 @@ EXPORT_SYMBOL_GPL(ata_base_port_ops);
 EXPORT_SYMBOL_GPL(sata_port_ops);
 EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
 EXPORT_SYMBOL_GPL(ata_dummy_port_info);
+EXPORT_SYMBOL_GPL(__ata_port_next_link);
 EXPORT_SYMBOL_GPL(ata_std_bios_param);
 EXPORT_SYMBOL_GPL(ata_host_init);
 EXPORT_SYMBOL_GPL(ata_host_alloc);
@@ -6196,7 +6296,6 @@ EXPORT_SYMBOL_GPL(ata_host_detach);
 EXPORT_SYMBOL_GPL(ata_sg_init);
 EXPORT_SYMBOL_GPL(ata_qc_complete);
 EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
-EXPORT_SYMBOL_GPL(sata_print_link_status);
 EXPORT_SYMBOL_GPL(atapi_cmd_type);
 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
@@ -6243,6 +6342,7 @@ EXPORT_SYMBOL_GPL(ata_host_resume);
 #endif /* CONFIG_PM */
 EXPORT_SYMBOL_GPL(ata_id_string);
 EXPORT_SYMBOL_GPL(ata_id_c_string);
+EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
 
 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
@@ -6278,6 +6378,7 @@ EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
 EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
 EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
 EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
+EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
 EXPORT_SYMBOL_GPL(ata_do_eh);
 EXPORT_SYMBOL_GPL(ata_std_error_handler);