libata: reimplement link iterator
[safe/jmp/linux-2.6] / drivers / ata / libata-core.c
index a12a27e..d156616 100644 (file)
@@ -54,7 +54,6 @@
 #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>
@@ -105,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 {
@@ -121,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)");
 
@@ -145,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)");
 
@@ -163,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
@@ -197,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))
@@ -229,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);
+               }
        }
 }
 
@@ -1133,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;
@@ -1166,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);
@@ -1533,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
@@ -1552,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));
 }
 
 /**
@@ -1612,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;
@@ -1684,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));
 
@@ -1761,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;
 }
 
@@ -1877,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)
@@ -1911,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) {
@@ -1939,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,
@@ -2126,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)
@@ -3231,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)
@@ -3256,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;
@@ -3312,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))
@@ -3380,7 +3466,7 @@ int ata_wait_ready(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);
 }
@@ -3410,13 +3496,13 @@ 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;
 
@@ -3428,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;
@@ -3437,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;
                }
@@ -3629,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);
@@ -3893,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 },
@@ -4291,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
@@ -4304,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;
 
@@ -4810,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;
        }
 
@@ -4839,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;
        }
 
@@ -4867,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;
@@ -5141,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;
 
@@ -5397,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;
@@ -5736,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);
        }
@@ -5944,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;
@@ -6067,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)
@@ -6121,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
@@ -6140,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);
@@ -6152,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);
        }
 
@@ -6198,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);
@@ -6255,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);