V4L/DVB: saa7127: support new vbi ops to set/get VBI format
[safe/jmp/linux-2.6] / drivers / ata / libata-eh.c
index aafe82b..228740f 100644 (file)
@@ -40,6 +40,7 @@
 #include <scsi/scsi_eh.h>
 #include <scsi/scsi_device.h>
 #include <scsi/scsi_cmnd.h>
+#include <scsi/scsi_dbg.h>
 #include "../scsi/scsi_transport_api.h"
 
 #include <linux/libata.h>
@@ -82,6 +83,10 @@ enum {
        ATA_EH_FASTDRAIN_INTERVAL       =  3000,
 
        ATA_EH_UA_TRIES                 = 5,
+
+       /* probe speed down parameters, see ata_eh_schedule_probe() */
+       ATA_EH_PROBE_TRIAL_INTERVAL     = 60000,        /* 1 min */
+       ATA_EH_PROBE_TRIALS             = 2,
 };
 
 /* The following table determines how we sequence resets.  Each entry
@@ -105,6 +110,13 @@ static const unsigned long ata_eh_identify_timeouts[] = {
        ULONG_MAX,
 };
 
+static const unsigned long ata_eh_flush_timeouts[] = {
+       15000,  /* be generous with flush */
+       15000,  /* ditto */
+       30000,  /* and even more generous */
+       ULONG_MAX,
+};
+
 static const unsigned long ata_eh_other_timeouts[] = {
         5000,  /* same rationale as identify timeout */
        10000,  /* ditto */
@@ -142,6 +154,8 @@ ata_eh_cmd_timeout_table[ATA_EH_CMD_TIMEOUT_TABLE_SIZE] = {
          .timeouts = ata_eh_other_timeouts, },
        { .commands = CMDS(ATA_CMD_INIT_DEV_PARAMS),
          .timeouts = ata_eh_other_timeouts, },
+       { .commands = CMDS(ATA_CMD_FLUSH, ATA_CMD_FLUSH_EXT),
+         .timeouts = ata_eh_flush_timeouts },
 };
 #undef CMDS
 
@@ -543,7 +557,7 @@ void ata_scsi_error(struct Scsi_Host *host)
 
        /* For new EH, all qcs are finished in one of three ways -
         * normal completion, error completion, and SCSI timeout.
-        * Both cmpletions can race against SCSI timeout.  When normal
+        * Both completions can race against SCSI timeout.  When normal
         * completion wins, the qc never reaches EH.  When error
         * completion wins, the qc has ATA_QCFLAG_FAILED set.
         *
@@ -558,7 +572,19 @@ void ata_scsi_error(struct Scsi_Host *host)
                int nr_timedout = 0;
 
                spin_lock_irqsave(ap->lock, flags);
-
+               
+               /* This must occur under the ap->lock as we don't want
+                  a polled recovery to race the real interrupt handler
+                  
+                  The lost_interrupt handler checks for any completed but
+                  non-notified command and completes much like an IRQ handler.
+                  
+                  We then fall into the error recovery code which will treat
+                  this as if normal completion won the race */
+
+               if (ap->ops->lost_interrupt)
+                       ap->ops->lost_interrupt(ap);
+                       
                list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
                        struct ata_queued_cmd *qc;
 
@@ -602,6 +628,9 @@ void ata_scsi_error(struct Scsi_Host *host)
                ap->eh_tries = ATA_EH_MAX_TRIES;
        } else
                spin_unlock_wait(ap->lock);
+               
+       /* If we timed raced normal completion and there is nothing to
+          recover nr_timedout == 0 why exactly are we doing error recovery ? */
 
  repeat:
        /* invoke error handler */
@@ -850,6 +879,8 @@ static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
 void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
 {
        struct ata_port *ap = qc->ap;
+       struct request_queue *q = qc->scsicmd->device->request_queue;
+       unsigned long flags;
 
        WARN_ON(!ap->ops->error_handler);
 
@@ -861,7 +892,9 @@ void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
         * Note that ATA_QCFLAG_FAILED is unconditionally set after
         * this function completes.
         */
+       spin_lock_irqsave(q->queue_lock, flags);
        blk_abort_request(qc->scsicmd->request);
+       spin_unlock_irqrestore(q->queue_lock, flags);
 }
 
 /**
@@ -980,7 +1013,9 @@ static void __ata_port_freeze(struct ata_port *ap)
  *     ata_port_freeze - abort & freeze port
  *     @ap: ATA port to freeze
  *
- *     Abort and freeze @ap.
+ *     Abort and freeze @ap.  The freeze operation must be called
+ *     first, because some hardware requires special operations
+ *     before the taskfile registers are accessible.
  *
  *     LOCKING:
  *     spin_lock_irqsave(host lock)
@@ -994,8 +1029,8 @@ int ata_port_freeze(struct ata_port *ap)
 
        WARN_ON(!ap->ops->error_handler);
 
-       nr_aborted = ata_port_abort(ap);
        __ata_port_freeze(ap);
+       nr_aborted = ata_port_abort(ap);
 
        return nr_aborted;
 }
@@ -1194,6 +1229,11 @@ void ata_dev_disable(struct ata_device *dev)
        ata_acpi_on_disable(dev);
        ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 | ATA_DNXFER_QUIET);
        dev->class++;
+
+       /* From now till the next successful probe, ering is used to
+        * track probe failures.  Clear accumulated device error info.
+        */
+       ata_ering_clear(&dev->ering);
 }
 
 /**
@@ -1588,6 +1628,7 @@ void ata_eh_analyze_ncq_error(struct ata_link *link)
        }
 
        /* okay, this error is ours */
+       memset(&tf, 0, sizeof(tf));
        rc = ata_eh_read_log_10h(dev, &tag, &tf);
        if (rc) {
                ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
@@ -1870,7 +1911,7 @@ static unsigned int ata_eh_speed_down(struct ata_device *dev,
        /* speed down? */
        if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
                /* speed down SATA link speed if possible */
-               if (sata_down_spd_limit(link) == 0) {
+               if (sata_down_spd_limit(link, 0) == 0) {
                        action |= ATA_EH_RESET;
                        goto done;
                }
@@ -1992,8 +2033,9 @@ static void ata_eh_link_autopsy(struct ata_link *link)
                        qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
 
                /* determine whether the command is worth retrying */
-               if (!(qc->err_mask & AC_ERR_INVALID) &&
-                   ((qc->flags & ATA_QCFLAG_IO) || qc->err_mask != AC_ERR_DEV))
+               if (qc->flags & ATA_QCFLAG_IO ||
+                   (!(qc->err_mask & AC_ERR_INVALID) &&
+                    qc->err_mask != AC_ERR_DEV))
                        qc->flags |= ATA_QCFLAG_RETRY;
 
                /* accumulate error info */
@@ -2086,6 +2128,116 @@ void ata_eh_autopsy(struct ata_port *ap)
 }
 
 /**
+ *     ata_get_cmd_descript - get description for ATA command
+ *     @command: ATA command code to get description for
+ *
+ *     Return a textual description of the given command, or NULL if the
+ *     command is not known.
+ *
+ *     LOCKING:
+ *     None
+ */
+const char *ata_get_cmd_descript(u8 command)
+{
+#ifdef CONFIG_ATA_VERBOSE_ERROR
+       static const struct
+       {
+               u8 command;
+               const char *text;
+       } cmd_descr[] = {
+               { ATA_CMD_DEV_RESET,            "DEVICE RESET" },
+               { ATA_CMD_CHK_POWER,            "CHECK POWER MODE" },
+               { ATA_CMD_STANDBY,              "STANDBY" },
+               { ATA_CMD_IDLE,                 "IDLE" },
+               { ATA_CMD_EDD,                  "EXECUTE DEVICE DIAGNOSTIC" },
+               { ATA_CMD_DOWNLOAD_MICRO,       "DOWNLOAD MICROCODE" },
+               { ATA_CMD_NOP,                  "NOP" },
+               { ATA_CMD_FLUSH,                "FLUSH CACHE" },
+               { ATA_CMD_FLUSH_EXT,            "FLUSH CACHE EXT" },
+               { ATA_CMD_ID_ATA,               "IDENTIFY DEVICE" },
+               { ATA_CMD_ID_ATAPI,             "IDENTIFY PACKET DEVICE" },
+               { ATA_CMD_SERVICE,              "SERVICE" },
+               { ATA_CMD_READ,                 "READ DMA" },
+               { ATA_CMD_READ_EXT,             "READ DMA EXT" },
+               { ATA_CMD_READ_QUEUED,          "READ DMA QUEUED" },
+               { ATA_CMD_READ_STREAM_EXT,      "READ STREAM EXT" },
+               { ATA_CMD_READ_STREAM_DMA_EXT,  "READ STREAM DMA EXT" },
+               { ATA_CMD_WRITE,                "WRITE DMA" },
+               { ATA_CMD_WRITE_EXT,            "WRITE DMA EXT" },
+               { ATA_CMD_WRITE_QUEUED,         "WRITE DMA QUEUED EXT" },
+               { ATA_CMD_WRITE_STREAM_EXT,     "WRITE STREAM EXT" },
+               { ATA_CMD_WRITE_STREAM_DMA_EXT, "WRITE STREAM DMA EXT" },
+               { ATA_CMD_WRITE_FUA_EXT,        "WRITE DMA FUA EXT" },
+               { ATA_CMD_WRITE_QUEUED_FUA_EXT, "WRITE DMA QUEUED FUA EXT" },
+               { ATA_CMD_FPDMA_READ,           "READ FPDMA QUEUED" },
+               { ATA_CMD_FPDMA_WRITE,          "WRITE FPDMA QUEUED" },
+               { ATA_CMD_PIO_READ,             "READ SECTOR(S)" },
+               { ATA_CMD_PIO_READ_EXT,         "READ SECTOR(S) EXT" },
+               { ATA_CMD_PIO_WRITE,            "WRITE SECTOR(S)" },
+               { ATA_CMD_PIO_WRITE_EXT,        "WRITE SECTOR(S) EXT" },
+               { ATA_CMD_READ_MULTI,           "READ MULTIPLE" },
+               { ATA_CMD_READ_MULTI_EXT,       "READ MULTIPLE EXT" },
+               { ATA_CMD_WRITE_MULTI,          "WRITE MULTIPLE" },
+               { ATA_CMD_WRITE_MULTI_EXT,      "WRITE MULTIPLE EXT" },
+               { ATA_CMD_WRITE_MULTI_FUA_EXT,  "WRITE MULTIPLE FUA EXT" },
+               { ATA_CMD_SET_FEATURES,         "SET FEATURES" },
+               { ATA_CMD_SET_MULTI,            "SET MULTIPLE MODE" },
+               { ATA_CMD_VERIFY,               "READ VERIFY SECTOR(S)" },
+               { ATA_CMD_VERIFY_EXT,           "READ VERIFY SECTOR(S) EXT" },
+               { ATA_CMD_WRITE_UNCORR_EXT,     "WRITE UNCORRECTABLE EXT" },
+               { ATA_CMD_STANDBYNOW1,          "STANDBY IMMEDIATE" },
+               { ATA_CMD_IDLEIMMEDIATE,        "IDLE IMMEDIATE" },
+               { ATA_CMD_SLEEP,                "SLEEP" },
+               { ATA_CMD_INIT_DEV_PARAMS,      "INITIALIZE DEVICE PARAMETERS" },
+               { ATA_CMD_READ_NATIVE_MAX,      "READ NATIVE MAX ADDRESS" },
+               { ATA_CMD_READ_NATIVE_MAX_EXT,  "READ NATIVE MAX ADDRESS EXT" },
+               { ATA_CMD_SET_MAX,              "SET MAX ADDRESS" },
+               { ATA_CMD_SET_MAX_EXT,          "SET MAX ADDRESS EXT" },
+               { ATA_CMD_READ_LOG_EXT,         "READ LOG EXT" },
+               { ATA_CMD_WRITE_LOG_EXT,        "WRITE LOG EXT" },
+               { ATA_CMD_READ_LOG_DMA_EXT,     "READ LOG DMA EXT" },
+               { ATA_CMD_WRITE_LOG_DMA_EXT,    "WRITE LOG DMA EXT" },
+               { ATA_CMD_TRUSTED_RCV,          "TRUSTED RECEIVE" },
+               { ATA_CMD_TRUSTED_RCV_DMA,      "TRUSTED RECEIVE DMA" },
+               { ATA_CMD_TRUSTED_SND,          "TRUSTED SEND" },
+               { ATA_CMD_TRUSTED_SND_DMA,      "TRUSTED SEND DMA" },
+               { ATA_CMD_PMP_READ,             "READ BUFFER" },
+               { ATA_CMD_PMP_WRITE,            "WRITE BUFFER" },
+               { ATA_CMD_CONF_OVERLAY,         "DEVICE CONFIGURATION OVERLAY" },
+               { ATA_CMD_SEC_SET_PASS,         "SECURITY SET PASSWORD" },
+               { ATA_CMD_SEC_UNLOCK,           "SECURITY UNLOCK" },
+               { ATA_CMD_SEC_ERASE_PREP,       "SECURITY ERASE PREPARE" },
+               { ATA_CMD_SEC_ERASE_UNIT,       "SECURITY ERASE UNIT" },
+               { ATA_CMD_SEC_FREEZE_LOCK,      "SECURITY FREEZE LOCK" },
+               { ATA_CMD_SEC_DISABLE_PASS,     "SECURITY DISABLE PASSWORD" },
+               { ATA_CMD_CONFIG_STREAM,        "CONFIGURE STREAM" },
+               { ATA_CMD_SMART,                "SMART" },
+               { ATA_CMD_MEDIA_LOCK,           "DOOR LOCK" },
+               { ATA_CMD_MEDIA_UNLOCK,         "DOOR UNLOCK" },
+               { ATA_CMD_CHK_MED_CRD_TYP,      "CHECK MEDIA CARD TYPE" },
+               { ATA_CMD_CFA_REQ_EXT_ERR,      "CFA REQUEST EXTENDED ERROR" },
+               { ATA_CMD_CFA_WRITE_NE,         "CFA WRITE SECTORS WITHOUT ERASE" },
+               { ATA_CMD_CFA_TRANS_SECT,       "CFA TRANSLATE SECTOR" },
+               { ATA_CMD_CFA_ERASE,            "CFA ERASE SECTORS" },
+               { ATA_CMD_CFA_WRITE_MULT_NE,    "CFA WRITE MULTIPLE WITHOUT ERASE" },
+               { ATA_CMD_READ_LONG,            "READ LONG (with retries)" },
+               { ATA_CMD_READ_LONG_ONCE,       "READ LONG (without retries)" },
+               { ATA_CMD_WRITE_LONG,           "WRITE LONG (with retries)" },
+               { ATA_CMD_WRITE_LONG_ONCE,      "WRITE LONG (without retries)" },
+               { ATA_CMD_RESTORE,              "RECALIBRATE" },
+               { 0,                            NULL } /* terminate list */
+       };
+
+       unsigned int i;
+       for (i = 0; cmd_descr[i].text; i++)
+               if (cmd_descr[i].command == command)
+                       return cmd_descr[i].text;
+#endif
+
+       return NULL;
+}
+
+/**
  *     ata_eh_link_report - report error handling to user
  *     @link: ATA link EH is going on
  *
@@ -2151,6 +2303,7 @@ static void ata_eh_link_report(struct ata_link *link)
                        ata_link_printk(link, KERN_ERR, "%s\n", desc);
        }
 
+#ifdef CONFIG_ATA_VERBOSE_ERROR
        if (ehc->i.serror)
                ata_link_printk(link, KERN_ERR,
                  "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
@@ -2171,6 +2324,7 @@ static void ata_eh_link_report(struct ata_link *link)
                  ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
                  ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
                  ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
+#endif
 
        for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
                struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
@@ -2202,14 +2356,23 @@ static void ata_eh_link_report(struct ata_link *link)
                                 dma_str[qc->dma_dir]);
                }
 
-               if (ata_is_atapi(qc->tf.protocol))
-                       snprintf(cdb_buf, sizeof(cdb_buf),
+               if (ata_is_atapi(qc->tf.protocol)) {
+                       if (qc->scsicmd)
+                               scsi_print_command(qc->scsicmd);
+                       else
+                               snprintf(cdb_buf, sizeof(cdb_buf),
                                 "cdb %02x %02x %02x %02x %02x %02x %02x %02x  "
                                 "%02x %02x %02x %02x %02x %02x %02x %02x\n         ",
                                 cdb[0], cdb[1], cdb[2], cdb[3],
                                 cdb[4], cdb[5], cdb[6], cdb[7],
                                 cdb[8], cdb[9], cdb[10], cdb[11],
                                 cdb[12], cdb[13], cdb[14], cdb[15]);
+               } else {
+                       const char *descr = ata_get_cmd_descript(cmd->command);
+                       if (descr)
+                               ata_dev_printk(qc->dev, KERN_ERR,
+                                       "failed command: %s\n", descr);
+               }
 
                ata_dev_printk(qc->dev, KERN_ERR,
                        "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
@@ -2228,6 +2391,7 @@ static void ata_eh_link_report(struct ata_link *link)
                        res->device, qc->err_mask, ata_err_string(qc->err_mask),
                        qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
 
+#ifdef CONFIG_ATA_VERBOSE_ERROR
                if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
                                    ATA_ERR)) {
                        if (res->command & ATA_BUSY)
@@ -2251,6 +2415,7 @@ static void ata_eh_link_report(struct ata_link *link)
                          res->feature & ATA_UNC ? "UNC " : "",
                          res->feature & ATA_IDNF ? "IDNF " : "",
                          res->feature & ATA_ABORTED ? "ABRT " : "");
+#endif
        }
 }
 
@@ -2303,7 +2468,7 @@ int ata_eh_reset(struct ata_link *link, int classify,
        struct ata_port *ap = link->ap;
        struct ata_link *slave = ap->slave_link;
        struct ata_eh_context *ehc = &link->eh_context;
-       struct ata_eh_context *sehc = &slave->eh_context;
+       struct ata_eh_context *sehc = slave ? &slave->eh_context : NULL;
        unsigned int *classes = ehc->classes;
        unsigned int lflags = link->flags;
        int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
@@ -2414,11 +2579,14 @@ int ata_eh_reset(struct ata_link *link, int classify,
                }
 
                /* prereset() might have cleared ATA_EH_RESET.  If so,
-                * bang classes and return.
+                * bang classes, thaw and return.
                 */
                if (reset && !(ehc->i.action & ATA_EH_RESET)) {
                        ata_for_each_dev(dev, link, ALL)
                                classes[dev->devno] = ATA_DEV_NONE;
+                       if ((ap->pflags & ATA_PFLAG_FROZEN) &&
+                           ata_is_host_link(link))
+                               ata_eh_thaw_port(ap);
                        rc = 0;
                        goto out;
                }
@@ -2490,6 +2658,10 @@ int ata_eh_reset(struct ata_link *link, int classify,
 
                        ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
                        rc = ata_do_reset(link, reset, classes, deadline, true);
+                       if (rc) {
+                               failed_link = link;
+                               goto fail;
+                       }
                }
        } else {
                if (verbose)
@@ -2510,14 +2682,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))) {
-                       /* 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;
+               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;
        }
 
        /* record current link speed */
@@ -2543,41 +2715,61 @@ int ata_eh_reset(struct ata_link *link, int classify,
                        postreset(slave, classes);
        }
 
-       /* clear cached SError */
+       /*
+        * Some controllers can't be frozen very well and may set
+        * spuruious error conditions during reset.  Clear accumulated
+        * error information.  As reset is the final recovery action,
+        * nothing is lost by doing this.
+        */
        spin_lock_irqsave(link->ap->lock, flags);
-       link->eh_info.serror = 0;
+       memset(&link->eh_info, 0, sizeof(link->eh_info));
        if (slave)
-               slave->eh_info.serror = 0;
+               memset(&slave->eh_info, 0, sizeof(link->eh_info));
+       ap->pflags &= ~ATA_PFLAG_EH_PENDING;
        spin_unlock_irqrestore(link->ap->lock, flags);
 
-       /* Make sure onlineness and classification result correspond.
+       /*
+        * Make sure onlineness and classification result correspond.
         * Hotplug could have happened during reset and some
         * controllers fail to wait while a drive is spinning up after
         * being hotplugged causing misdetection.  By cross checking
-        * link onlineness and classification result, those conditions
-        * can be reliably detected and retried.
+        * link on/offlineness and classification result, those
+        * conditions can be reliably detected and retried.
         */
        nr_unknown = 0;
        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;
-                       if (ata_phys_link_online(ata_dev_phys_link(dev)))
+               if (ata_phys_link_online(ata_dev_phys_link(dev))) {
+                       if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
+                               ata_dev_printk(dev, KERN_DEBUG, "link online "
+                                              "but device misclassifed\n");
+                               classes[dev->devno] = ATA_DEV_NONE;
                                nr_unknown++;
+                       }
+               } else if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
+                       if (ata_class_enabled(classes[dev->devno]))
+                               ata_dev_printk(dev, KERN_DEBUG, "link offline, "
+                                              "clearing class %d to NONE\n",
+                                              classes[dev->devno]);
+                       classes[dev->devno] = ATA_DEV_NONE;
+               } else if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
+                       ata_dev_printk(dev, KERN_DEBUG, "link status unknown, "
+                                      "clearing UNKNOWN to NONE\n");
+                       classes[dev->devno] = ATA_DEV_NONE;
                }
        }
 
        if (classify && nr_unknown) {
                if (try < max_tries) {
                        ata_link_printk(link, KERN_WARNING, "link online but "
-                                      "device misclassified, retrying\n");
+                                       "%d devices misclassified, retrying\n",
+                                       nr_unknown);
                        failed_link = link;
                        rc = -EAGAIN;
                        goto fail;
                }
                ata_link_printk(link, KERN_WARNING,
-                              "link online but device misclassified, "
-                              "device detection might fail\n");
+                               "link online but %d devices misclassified, "
+                               "device detection might fail\n", nr_unknown);
        }
 
        /* reset successful, schedule revalidation */
@@ -2622,11 +2814,11 @@ int ata_eh_reset(struct ata_link *link, int classify,
        }
 
        if (try == max_tries - 1) {
-               sata_down_spd_limit(link);
+               sata_down_spd_limit(link, 0);
                if (slave)
-                       sata_down_spd_limit(slave);
+                       sata_down_spd_limit(slave, 0);
        } else if (rc == -EPIPE)
-               sata_down_spd_limit(failed_link);
+               sata_down_spd_limit(failed_link, 0);
 
        if (hardreset)
                reset = hardreset;
@@ -2756,6 +2948,12 @@ static int ata_eh_revalidate_and_attach(struct ata_link *link,
                } else if (dev->class == ATA_DEV_UNKNOWN &&
                           ehc->tries[dev->devno] &&
                           ata_class_enabled(ehc->classes[dev->devno])) {
+                       /* Temporarily set dev->class, it will be
+                        * permanently set once all configurations are
+                        * complete.  This is necessary because new
+                        * device configuration is done in two
+                        * separate loops.
+                        */
                        dev->class = ehc->classes[dev->devno];
 
                        if (dev->class == ATA_DEV_PMP)
@@ -2763,20 +2961,25 @@ static int ata_eh_revalidate_and_attach(struct ata_link *link,
                        else
                                rc = ata_dev_read_id(dev, &dev->class,
                                                     readid_flags, dev->id);
+
+                       /* read_id might have changed class, store and reset */
+                       ehc->classes[dev->devno] = dev->class;
+                       dev->class = ATA_DEV_UNKNOWN;
+
                        switch (rc) {
                        case 0:
+                               /* clear error info accumulated during probe */
+                               ata_ering_clear(&dev->ering);
                                new_mask |= 1 << dev->devno;
                                break;
                        case -ENOENT:
                                /* IDENTIFY was issued to non-existent
                                 * device.  No need to reset.  Just
-                                * thaw and kill the device.
+                                * thaw and ignore the device.
                                 */
                                ata_eh_thaw_port(ap);
-                               dev->class = ATA_DEV_UNKNOWN;
                                break;
                        default:
-                               dev->class = ATA_DEV_UNKNOWN;
                                goto err;
                        }
                }
@@ -2793,15 +2996,21 @@ static int ata_eh_revalidate_and_attach(struct ata_link *link,
         * device detection messages backwards.
         */
        ata_for_each_dev(dev, link, ALL) {
-               if (!(new_mask & (1 << dev->devno)) ||
-                   dev->class == ATA_DEV_PMP)
+               if (!(new_mask & (1 << dev->devno)))
+                       continue;
+
+               dev->class = ehc->classes[dev->devno];
+
+               if (dev->class == ATA_DEV_PMP)
                        continue;
 
                ehc->i.flags |= ATA_EHI_PRINTINFO;
                rc = ata_dev_configure(dev);
                ehc->i.flags &= ~ATA_EHI_PRINTINFO;
-               if (rc)
+               if (rc) {
+                       dev->class = ATA_DEV_UNKNOWN;
                        goto err;
+               }
 
                spin_lock_irqsave(ap->lock, flags);
                ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
@@ -2822,7 +3031,7 @@ static int ata_eh_revalidate_and_attach(struct ata_link *link,
 /**
  *     ata_set_mode - Program timings and issue SET FEATURES - XFER
  *     @link: link on which timings will be programmed
- *     @r_failed_dev: out paramter for failed device
+ *     @r_failed_dev: out parameter for failed device
  *
  *     Set ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
  *     ata_set_mode() fails, pointer to the failing device is
@@ -2890,7 +3099,7 @@ static int atapi_eh_clear_ua(struct ata_device *dev)
        int i;
 
        for (i = 0; i < ATA_EH_UA_TRIES; i++) {
-               u8 sense_buffer[SCSI_SENSE_BUFFERSIZE];
+               u8 *sense_buffer = dev->link->ap->sector_buf;
                u8 sense_key = 0;
                unsigned int err_mask;
 
@@ -2918,6 +3127,82 @@ static int atapi_eh_clear_ua(struct ata_device *dev)
        return 0;
 }
 
+/**
+ *     ata_eh_maybe_retry_flush - Retry FLUSH if necessary
+ *     @dev: ATA device which may need FLUSH retry
+ *
+ *     If @dev failed FLUSH, it needs to be reported upper layer
+ *     immediately as it means that @dev failed to remap and already
+ *     lost at least a sector and further FLUSH retrials won't make
+ *     any difference to the lost sector.  However, if FLUSH failed
+ *     for other reasons, for example transmission error, FLUSH needs
+ *     to be retried.
+ *
+ *     This function determines whether FLUSH failure retry is
+ *     necessary and performs it if so.
+ *
+ *     RETURNS:
+ *     0 if EH can continue, -errno if EH needs to be repeated.
+ */
+static int ata_eh_maybe_retry_flush(struct ata_device *dev)
+{
+       struct ata_link *link = dev->link;
+       struct ata_port *ap = link->ap;
+       struct ata_queued_cmd *qc;
+       struct ata_taskfile tf;
+       unsigned int err_mask;
+       int rc = 0;
+
+       /* did flush fail for this device? */
+       if (!ata_tag_valid(link->active_tag))
+               return 0;
+
+       qc = __ata_qc_from_tag(ap, link->active_tag);
+       if (qc->dev != dev || (qc->tf.command != ATA_CMD_FLUSH_EXT &&
+                              qc->tf.command != ATA_CMD_FLUSH))
+               return 0;
+
+       /* if the device failed it, it should be reported to upper layers */
+       if (qc->err_mask & AC_ERR_DEV)
+               return 0;
+
+       /* flush failed for some other reason, give it another shot */
+       ata_tf_init(dev, &tf);
+
+       tf.command = qc->tf.command;
+       tf.flags |= ATA_TFLAG_DEVICE;
+       tf.protocol = ATA_PROT_NODATA;
+
+       ata_dev_printk(dev, KERN_WARNING, "retrying FLUSH 0x%x Emask 0x%x\n",
+                      tf.command, qc->err_mask);
+
+       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
+       if (!err_mask) {
+               /*
+                * FLUSH is complete but there's no way to
+                * successfully complete a failed command from EH.
+                * Making sure retry is allowed at least once and
+                * retrying it should do the trick - whatever was in
+                * the cache is already on the platter and this won't
+                * cause infinite loop.
+                */
+               qc->scsicmd->allowed = max(qc->scsicmd->allowed, 1);
+       } else {
+               ata_dev_printk(dev, KERN_WARNING, "FLUSH failed Emask 0x%x\n",
+                              err_mask);
+               rc = -EIO;
+
+               /* if device failed it, report it to upper layers */
+               if (err_mask & AC_ERR_DEV) {
+                       qc->err_mask |= AC_ERR_DEV;
+                       qc->result_tf = tf;
+                       if (!(ap->pflags & ATA_PFLAG_FROZEN))
+                               rc = 0;
+               }
+       }
+       return rc;
+}
+
 static int ata_link_nr_enabled(struct ata_link *link)
 {
        struct ata_device *dev;
@@ -2968,9 +3253,24 @@ static int ata_eh_skip_recovery(struct ata_link *link)
        return 1;
 }
 
+static int ata_count_probe_trials_cb(struct ata_ering_entry *ent, void *void_arg)
+{
+       u64 interval = msecs_to_jiffies(ATA_EH_PROBE_TRIAL_INTERVAL);
+       u64 now = get_jiffies_64();
+       int *trials = void_arg;
+
+       if (ent->timestamp < now - min(now, interval))
+               return -1;
+
+       (*trials)++;
+       return 0;
+}
+
 static int ata_eh_schedule_probe(struct ata_device *dev)
 {
        struct ata_eh_context *ehc = &dev->link->eh_context;
+       struct ata_link *link = ata_dev_phys_link(dev);
+       int trials = 0;
 
        if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
            (ehc->did_probe_mask & (1 << dev->devno)))
@@ -2983,6 +3283,25 @@ static int ata_eh_schedule_probe(struct ata_device *dev)
        ehc->saved_xfer_mode[dev->devno] = 0;
        ehc->saved_ncq_enabled &= ~(1 << dev->devno);
 
+       /* Record and count probe trials on the ering.  The specific
+        * error mask used is irrelevant.  Because a successful device
+        * detection clears the ering, this count accumulates only if
+        * there are consecutive failed probes.
+        *
+        * If the count is equal to or higher than ATA_EH_PROBE_TRIALS
+        * in the last ATA_EH_PROBE_TRIAL_INTERVAL, link speed is
+        * forced to 1.5Gbps.
+        *
+        * This is to work around cases where failed link speed
+        * negotiation results in device misdetection leading to
+        * infinite DEVXCHG or PHRDY CHG events.
+        */
+       ata_ering_record(&dev->ering, 0, AC_ERR_OTHER);
+       ata_ering_map(&dev->ering, ata_count_probe_trials_cb, &trials);
+
+       if (trials > ATA_EH_PROBE_TRIALS)
+               sata_down_spd_limit(link, 1);
+
        return 1;
 }
 
@@ -2990,7 +3309,11 @@ static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
 {
        struct ata_eh_context *ehc = &dev->link->eh_context;
 
-       ehc->tries[dev->devno]--;
+       /* -EAGAIN from EH routine indicates retry without prejudice.
+        * The requester is responsible for ensuring forward progress.
+        */
+       if (err != -EAGAIN)
+               ehc->tries[dev->devno]--;
 
        switch (err) {
        case -ENODEV:
@@ -3004,7 +3327,7 @@ static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
                        /* This is the last chance, better to slow
                         * down than lose it.
                         */
-                       sata_down_spd_limit(ata_dev_phys_link(dev));
+                       sata_down_spd_limit(ata_dev_phys_link(dev), 0);
                        if (dev->pio_mode > XFER_PIO_0)
                                ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
                }
@@ -3223,6 +3546,15 @@ int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
                        }
                }
 
+               /* retry flush if necessary */
+               ata_for_each_dev(dev, link, ALL) {
+                       if (dev->class != ATA_DEV_ATA)
+                               continue;
+                       rc = ata_eh_maybe_retry_flush(dev);
+                       if (rc)
+                               goto dev_fail;
+               }
+
                /* configure link power saving */
                if (ehc->i.action & ATA_EH_LPM)
                        ata_for_each_dev(dev, link, ALL)
@@ -3427,6 +3759,8 @@ static void ata_eh_handle_port_suspend(struct ata_port *ap)
  */
 static void ata_eh_handle_port_resume(struct ata_port *ap)
 {
+       struct ata_link *link;
+       struct ata_device *dev;
        unsigned long flags;
        int rc = 0;
 
@@ -3441,6 +3775,17 @@ static void ata_eh_handle_port_resume(struct ata_port *ap)
 
        WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
 
+       /*
+        * Error timestamps are in jiffies which doesn't run while
+        * suspended and PHY events during resume isn't too uncommon.
+        * When the two are combined, it can lead to unnecessary speed
+        * downs if the machine is suspended and resumed repeatedly.
+        * Clear error history.
+        */
+       ata_for_each_link(link, ap, HOST_FIRST)
+               ata_for_each_dev(dev, link, ALL)
+                       ata_ering_clear(&dev->ering);
+
        ata_acpi_set_state(ap, PMSG_ON);
 
        if (ap->ops->port_resume)