libata: move ata_dev_disable() to libata-eh.c
[safe/jmp/linux-2.6] / drivers / ata / libata-eh.c
index d2409a8..aafe82b 100644 (file)
@@ -422,7 +422,7 @@ static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
 
        if (!dev) {
                ehi->action &= ~action;
-               ata_link_for_each_dev(tdev, link)
+               ata_for_each_dev(tdev, link, ALL)
                        ehi->dev_action[tdev->devno] &= ~action;
        } else {
                /* doesn't make sense for port-wide EH actions */
@@ -430,7 +430,7 @@ static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
 
                /* break ehi->action into ehi->dev_action */
                if (ehi->action & action) {
-                       ata_link_for_each_dev(tdev, link)
+                       ata_for_each_dev(tdev, link, ALL)
                                ehi->dev_action[tdev->devno] |=
                                        ehi->action & action;
                        ehi->action &= ~action;
@@ -491,6 +491,31 @@ enum blk_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
        return ret;
 }
 
+static void ata_eh_unload(struct ata_port *ap)
+{
+       struct ata_link *link;
+       struct ata_device *dev;
+       unsigned long flags;
+
+       /* Restore SControl IPM and SPD for the next driver and
+        * disable attached devices.
+        */
+       ata_for_each_link(link, ap, PMP_FIRST) {
+               sata_scr_write(link, SCR_CONTROL, link->saved_scontrol & 0xff0);
+               ata_for_each_dev(dev, link, ALL)
+                       ata_dev_disable(dev);
+       }
+
+       /* freeze and set UNLOADED */
+       spin_lock_irqsave(ap->lock, flags);
+
+       ata_port_freeze(ap);                    /* won't be thawed */
+       ap->pflags &= ~ATA_PFLAG_EH_PENDING;    /* clear pending from freeze */
+       ap->pflags |= ATA_PFLAG_UNLOADED;
+
+       spin_unlock_irqrestore(ap->lock, flags);
+}
+
 /**
  *     ata_scsi_error - SCSI layer error handler callback
  *     @host: SCSI host on which error occurred
@@ -592,7 +617,7 @@ void ata_scsi_error(struct Scsi_Host *host)
                /* fetch & clear EH info */
                spin_lock_irqsave(ap->lock, flags);
 
-               __ata_port_for_each_link(link, ap) {
+               ata_for_each_link(link, ap, HOST_FIRST) {
                        struct ata_eh_context *ehc = &link->eh_context;
                        struct ata_device *dev;
 
@@ -600,16 +625,13 @@ void ata_scsi_error(struct Scsi_Host *host)
                        link->eh_context.i = link->eh_info;
                        memset(&link->eh_info, 0, sizeof(link->eh_info));
 
-                       ata_link_for_each_dev(dev, link) {
+                       ata_for_each_dev(dev, link, ENABLED) {
                                int devno = dev->devno;
 
                                ehc->saved_xfer_mode[devno] = dev->xfer_mode;
                                if (ata_ncq_enabled(dev))
                                        ehc->saved_ncq_enabled |= 1 << devno;
                        }
-
-                       /* set last reset timestamp to some time in the past */
-                       ehc->last_reset = jiffies - 60 * HZ;
                }
 
                ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
@@ -621,8 +643,13 @@ void ata_scsi_error(struct Scsi_Host *host)
                /* invoke EH, skip if unloading or suspended */
                if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
                        ap->ops->error_handler(ap);
-               else
+               else {
+                       /* if unloading, commence suicide */
+                       if ((ap->pflags & ATA_PFLAG_UNLOADING) &&
+                           !(ap->pflags & ATA_PFLAG_UNLOADED))
+                               ata_eh_unload(ap);
                        ata_eh_finish(ap);
+               }
 
                /* process port suspend request */
                ata_eh_handle_port_suspend(ap);
@@ -644,7 +671,7 @@ void ata_scsi_error(struct Scsi_Host *host)
                }
 
                /* this run is complete, make sure EH info is clear */
-               __ata_port_for_each_link(link, ap)
+               ata_for_each_link(link, ap, HOST_FIRST)
                        memset(&link->eh_info, 0, sizeof(link->eh_info));
 
                /* Clear host_eh_scheduled while holding ap->lock such
@@ -1025,7 +1052,7 @@ int sata_async_notification(struct ata_port *ap)
                struct ata_link *link;
 
                /* check and notify ATAPI AN */
-               ata_port_for_each_link(link, ap) {
+               ata_for_each_link(link, ap, EDGE) {
                        if (!(sntf & (1 << link->pmp)))
                                continue;
 
@@ -1149,6 +1176,27 @@ void ata_eh_qc_retry(struct ata_queued_cmd *qc)
 }
 
 /**
+ *     ata_dev_disable - disable ATA device
+ *     @dev: ATA device to disable
+ *
+ *     Disable @dev.
+ *
+ *     Locking:
+ *     EH context.
+ */
+void ata_dev_disable(struct ata_device *dev)
+{
+       if (!ata_dev_enabled(dev))
+               return;
+
+       if (ata_msg_drv(dev->link->ap))
+               ata_dev_printk(dev, KERN_WARNING, "disabled\n");
+       ata_acpi_on_disable(dev);
+       ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 | ATA_DNXFER_QUIET);
+       dev->class++;
+}
+
+/**
  *     ata_eh_detach_dev - detach ATA device
  *     @dev: ATA device to detach
  *
@@ -1161,6 +1209,7 @@ void ata_eh_detach_dev(struct ata_device *dev)
 {
        struct ata_link *link = dev->link;
        struct ata_port *ap = link->ap;
+       struct ata_eh_context *ehc = &link->eh_context;
        unsigned long flags;
 
        ata_dev_disable(dev);
@@ -1174,9 +1223,11 @@ void ata_eh_detach_dev(struct ata_device *dev)
                ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
        }
 
-       /* clear per-dev EH actions */
+       /* clear per-dev EH info */
        ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
        ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
+       ehc->saved_xfer_mode[dev->devno] = 0;
+       ehc->saved_ncq_enabled &= ~(1 << dev->devno);
 
        spin_unlock_irqrestore(ap->lock, flags);
 }
@@ -1206,7 +1257,10 @@ void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
 
        ata_eh_clear_action(link, dev, ehi, action);
 
-       if (!(ehc->i.flags & ATA_EHI_QUIET))
+       /* About to take EH action, set RECOVERED.  Ignore actions on
+        * slave links as master will do them again.
+        */
+       if (!(ehc->i.flags & ATA_EHI_QUIET) && link != ap->slave_link)
                ap->pflags |= ATA_PFLAG_RECOVERED;
 
        spin_unlock_irqrestore(ap->lock, flags);
@@ -1999,7 +2053,7 @@ void ata_eh_autopsy(struct ata_port *ap)
 {
        struct ata_link *link;
 
-       ata_port_for_each_link(link, ap)
+       ata_for_each_link(link, ap, EDGE)
                ata_eh_link_autopsy(link);
 
        /* Handle the frigging slave link.  Autopsy is done similarly
@@ -2213,7 +2267,7 @@ void ata_eh_report(struct ata_port *ap)
 {
        struct ata_link *link;
 
-       __ata_port_for_each_link(link, ap)
+       ata_for_each_link(link, ap, HOST_FIRST)
                ata_eh_link_report(link);
 }
 
@@ -2224,7 +2278,7 @@ static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
        struct ata_device *dev;
 
        if (clear_classes)
-               ata_link_for_each_dev(dev, link)
+               ata_for_each_dev(dev, link, ALL)
                        classes[dev->devno] = ATA_DEV_UNKNOWN;
 
        return reset(link, classes, deadline);
@@ -2272,19 +2326,23 @@ int ata_eh_reset(struct ata_link *link, int classify,
        if (link->flags & ATA_LFLAG_NO_SRST)
                softreset = NULL;
 
-       now = jiffies;
-       deadline = ata_deadline(ehc->last_reset, ATA_EH_RESET_COOL_DOWN);
-       if (time_before(now, deadline))
-               schedule_timeout_uninterruptible(deadline - now);
+       /* make sure each reset attemp is at least COOL_DOWN apart */
+       if (ehc->i.flags & ATA_EHI_DID_RESET) {
+               now = jiffies;
+               WARN_ON(time_after(ehc->last_reset, now));
+               deadline = ata_deadline(ehc->last_reset,
+                                       ATA_EH_RESET_COOL_DOWN);
+               if (time_before(now, deadline))
+                       schedule_timeout_uninterruptible(deadline - now);
+       }
 
        spin_lock_irqsave(ap->lock, flags);
        ap->pflags |= ATA_PFLAG_RESETTING;
        spin_unlock_irqrestore(ap->lock, flags);
 
        ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
-       ehc->last_reset = jiffies;
 
-       ata_link_for_each_dev(dev, link) {
+       ata_for_each_dev(dev, link, ALL) {
                /* If we issue an SRST then an ATA drive (not ATAPI)
                 * may change configuration and be in PIO0 timing. If
                 * we do a hard reset (or are coming from power on)
@@ -2345,7 +2403,7 @@ int ata_eh_reset(struct ata_link *link, int classify,
                                                "port disabled. ignoring.\n");
                                ehc->i.action &= ~ATA_EH_RESET;
 
-                               ata_link_for_each_dev(dev, link)
+                               ata_for_each_dev(dev, link, ALL)
                                        classes[dev->devno] = ATA_DEV_NONE;
 
                                rc = 0;
@@ -2359,7 +2417,7 @@ int ata_eh_reset(struct ata_link *link, int classify,
                 * bang classes and return.
                 */
                if (reset && !(ehc->i.action & ATA_EH_RESET)) {
-                       ata_link_for_each_dev(dev, link)
+                       ata_for_each_dev(dev, link, ALL)
                                classes[dev->devno] = ATA_DEV_NONE;
                        rc = 0;
                        goto out;
@@ -2370,7 +2428,6 @@ int ata_eh_reset(struct ata_link *link, int classify,
        /*
         * Perform reset
         */
-       ehc->last_reset = jiffies;
        if (ata_is_host_link(link))
                ata_eh_freeze_port(ap);
 
@@ -2382,6 +2439,7 @@ int ata_eh_reset(struct ata_link *link, int classify,
                                        reset == softreset ? "soft" : "hard");
 
                /* mark that this EH session started with reset */
+               ehc->last_reset = jiffies;
                if (reset == hardreset)
                        ehc->i.flags |= ATA_EHI_DID_HARDRESET;
                else
@@ -2444,7 +2502,7 @@ int ata_eh_reset(struct ata_link *link, int classify,
        /*
         * Post-reset processing
         */
-       ata_link_for_each_dev(dev, link) {
+       ata_for_each_dev(dev, link, ALL) {
                /* After the reset, the device state is PIO 0 and the
                 * controller state is undefined.  Reset also wakes up
                 * drives from sleeping mode.
@@ -2452,14 +2510,14 @@ int ata_eh_reset(struct ata_link *link, int classify,
                dev->pio_mode = XFER_PIO_0;
                dev->flags &= ~ATA_DFLAG_SLEEPING;
 
-               if (ata_phys_link_offline(ata_dev_phys_link(dev)))
-                       continue;
-
-               /* apply class override */
-               if (lflags & ATA_LFLAG_ASSUME_ATA)
-                       classes[dev->devno] = ATA_DEV_ATA;
-               else if (lflags & ATA_LFLAG_ASSUME_SEMB)
-                       classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
+               if (!ata_phys_link_offline(ata_dev_phys_link(dev))) {
+                       /* apply class override */
+                       if (lflags & ATA_LFLAG_ASSUME_ATA)
+                               classes[dev->devno] = ATA_DEV_ATA;
+                       else if (lflags & ATA_LFLAG_ASSUME_SEMB)
+                               classes[dev->devno] = ATA_DEV_SEMB_UNSUP;
+               } else
+                       classes[dev->devno] = ATA_DEV_NONE;
        }
 
        /* record current link speed */
@@ -2500,7 +2558,7 @@ int ata_eh_reset(struct ata_link *link, int classify,
         * can be reliably detected and retried.
         */
        nr_unknown = 0;
-       ata_link_for_each_dev(dev, link) {
+       ata_for_each_dev(dev, link, ALL) {
                /* convert all ATA_DEV_UNKNOWN to ATA_DEV_NONE */
                if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
                        classes[dev->devno] = ATA_DEV_NONE;
@@ -2526,7 +2584,7 @@ int ata_eh_reset(struct ata_link *link, int classify,
        ata_eh_done(link, NULL, ATA_EH_RESET);
        if (slave)
                ata_eh_done(slave, NULL, ATA_EH_RESET);
-       ehc->last_reset = jiffies;
+       ehc->last_reset = jiffies;      /* update to completion time */
        ehc->i.action |= ATA_EH_REVALIDATE;
 
        rc = 0;
@@ -2609,8 +2667,8 @@ static inline void ata_eh_pull_park_action(struct ata_port *ap)
 
        spin_lock_irqsave(ap->lock, flags);
        INIT_COMPLETION(ap->park_req_pending);
-       ata_port_for_each_link(link, ap) {
-               ata_link_for_each_dev(dev, link) {
+       ata_for_each_link(link, ap, EDGE) {
+               ata_for_each_dev(dev, link, ALL) {
                        struct ata_eh_info *ehi = &link->eh_info;
 
                        link->eh_context.i.dev_action[dev->devno] |=
@@ -2665,7 +2723,7 @@ static int ata_eh_revalidate_and_attach(struct ata_link *link,
         * be done backwards such that PDIAG- is released by the slave
         * device before the master device is identified.
         */
-       ata_link_for_each_dev_reverse(dev, link) {
+       ata_for_each_dev(dev, link, ALL_REVERSE) {
                unsigned int action = ata_eh_dev_action(dev);
                unsigned int readid_flags = 0;
 
@@ -2734,7 +2792,7 @@ static int ata_eh_revalidate_and_attach(struct ata_link *link,
        /* Configure new devices forward such that user doesn't see
         * device detection messages backwards.
         */
-       ata_link_for_each_dev(dev, link) {
+       ata_for_each_dev(dev, link, ALL) {
                if (!(new_mask & (1 << dev->devno)) ||
                    dev->class == ATA_DEV_PMP)
                        continue;
@@ -2783,7 +2841,7 @@ int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
        int rc;
 
        /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
-       ata_link_for_each_dev(dev, link) {
+       ata_for_each_dev(dev, link, ENABLED) {
                if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
                        struct ata_ering_entry *ent;
 
@@ -2800,7 +2858,7 @@ int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
                rc = ata_do_set_mode(link, r_failed_dev);
 
        /* if transfer mode has changed, set DUBIOUS_XFER on device */
-       ata_link_for_each_dev(dev, link) {
+       ata_for_each_dev(dev, link, ENABLED) {
                struct ata_eh_context *ehc = &link->eh_context;
                u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
                u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
@@ -2865,9 +2923,8 @@ static int ata_link_nr_enabled(struct ata_link *link)
        struct ata_device *dev;
        int cnt = 0;
 
-       ata_link_for_each_dev(dev, link)
-               if (ata_dev_enabled(dev))
-                       cnt++;
+       ata_for_each_dev(dev, link, ENABLED)
+               cnt++;
        return cnt;
 }
 
@@ -2876,7 +2933,7 @@ static int ata_link_nr_vacant(struct ata_link *link)
        struct ata_device *dev;
        int cnt = 0;
 
-       ata_link_for_each_dev(dev, link)
+       ata_for_each_dev(dev, link, ALL)
                if (dev->class == ATA_DEV_UNKNOWN)
                        cnt++;
        return cnt;
@@ -2902,7 +2959,7 @@ static int ata_eh_skip_recovery(struct ata_link *link)
                return 0;
 
        /* skip if class codes for all vacant slots are ATA_DEV_NONE */
-       ata_link_for_each_dev(dev, link) {
+       ata_for_each_dev(dev, link, ALL) {
                if (dev->class == ATA_DEV_UNKNOWN &&
                    ehc->classes[dev->devno] != ATA_DEV_NONE)
                        return 0;
@@ -2943,12 +3000,13 @@ static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
                /* give it just one more chance */
                ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
        case -EIO:
-               if (ehc->tries[dev->devno] == 1 && dev->pio_mode > XFER_PIO_0) {
+               if (ehc->tries[dev->devno] == 1) {
                        /* This is the last chance, better to slow
                         * down than lose it.
                         */
                        sata_down_spd_limit(ata_dev_phys_link(dev));
-                       ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
+                       if (dev->pio_mode > XFER_PIO_0)
+                               ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
                }
        }
 
@@ -3010,7 +3068,7 @@ int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
        DPRINTK("ENTER\n");
 
        /* prep for recovery */
-       ata_port_for_each_link(link, ap) {
+       ata_for_each_link(link, ap, EDGE) {
                struct ata_eh_context *ehc = &link->eh_context;
 
                /* re-enable link? */
@@ -3022,7 +3080,7 @@ int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
                        ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
                }
 
-               ata_link_for_each_dev(dev, link) {
+               ata_for_each_dev(dev, link, ALL) {
                        if (link->flags & ATA_LFLAG_NO_RETRY)
                                ehc->tries[dev->devno] = 1;
                        else
@@ -3052,19 +3110,19 @@ int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
                goto out;
 
        /* prep for EH */
-       ata_port_for_each_link(link, ap) {
+       ata_for_each_link(link, ap, EDGE) {
                struct ata_eh_context *ehc = &link->eh_context;
 
                /* skip EH if possible. */
                if (ata_eh_skip_recovery(link))
                        ehc->i.action = 0;
 
-               ata_link_for_each_dev(dev, link)
+               ata_for_each_dev(dev, link, ALL)
                        ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
        }
 
        /* reset */
-       ata_port_for_each_link(link, ap) {
+       ata_for_each_link(link, ap, EDGE) {
                struct ata_eh_context *ehc = &link->eh_context;
 
                if (!(ehc->i.action & ATA_EH_RESET))
@@ -3089,8 +3147,8 @@ int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
                ata_eh_pull_park_action(ap);
 
                deadline = jiffies;
-               ata_port_for_each_link(link, ap) {
-                       ata_link_for_each_dev(dev, link) {
+               ata_for_each_link(link, ap, EDGE) {
+                       ata_for_each_dev(dev, link, ALL) {
                                struct ata_eh_context *ehc = &link->eh_context;
                                unsigned long tmp;
 
@@ -3118,8 +3176,8 @@ int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
                deadline = wait_for_completion_timeout(&ap->park_req_pending,
                                                       deadline - now);
        } while (deadline);
-       ata_port_for_each_link(link, ap) {
-               ata_link_for_each_dev(dev, link) {
+       ata_for_each_link(link, ap, EDGE) {
+               ata_for_each_dev(dev, link, ALL) {
                        if (!(link->eh_context.unloaded_mask &
                              (1 << dev->devno)))
                                continue;
@@ -3130,7 +3188,7 @@ int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
        }
 
        /* the rest */
-       ata_port_for_each_link(link, ap) {
+       ata_for_each_link(link, ap, EDGE) {
                struct ata_eh_context *ehc = &link->eh_context;
 
                /* revalidate existing devices and attach new ones */
@@ -3156,7 +3214,7 @@ int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
                 * disrupting the current users of the device.
                 */
                if (ehc->i.flags & ATA_EHI_DID_RESET) {
-                       ata_link_for_each_dev(dev, link) {
+                       ata_for_each_dev(dev, link, ALL) {
                                if (dev->class != ATA_DEV_ATAPI)
                                        continue;
                                rc = atapi_eh_clear_ua(dev);
@@ -3167,7 +3225,7 @@ int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
 
                /* configure link power saving */
                if (ehc->i.action & ATA_EH_LPM)
-                       ata_link_for_each_dev(dev, link)
+                       ata_for_each_dev(dev, link, ALL)
                                ata_dev_enable_pm(dev, ap->pm_policy);
 
                /* this link is okay now */
@@ -3272,7 +3330,7 @@ void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
        rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
                            NULL);
        if (rc) {
-               ata_link_for_each_dev(dev, &ap->link)
+               ata_for_each_dev(dev, &ap->link, ALL)
                        ata_dev_disable(dev);
        }