Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6
[safe/jmp/linux-2.6] / drivers / scsi / scsi_error.c
index 1c7d993..eaf5a8a 100644 (file)
@@ -18,8 +18,8 @@
 #include <linux/sched.h>
 #include <linux/timer.h>
 #include <linux/string.h>
-#include <linux/slab.h>
 #include <linux/kernel.h>
+#include <linux/freezer.h>
 #include <linux/kthread.h>
 #include <linux/interrupt.h>
 #include <linux/blkdev.h>
@@ -36,9 +36,9 @@
 
 #include "scsi_priv.h"
 #include "scsi_logging.h"
+#include "scsi_transport_api.h"
 
 #define SENSE_TIMEOUT          (10*HZ)
-#define START_UNIT_TIMEOUT     (30*HZ)
 
 /*
  * These should *probably* be handled by the host itself.
@@ -58,13 +58,35 @@ void scsi_eh_wakeup(struct Scsi_Host *shost)
 }
 
 /**
+ * scsi_schedule_eh - schedule EH for SCSI host
+ * @shost:     SCSI host to invoke error handling on.
+ *
+ * Schedule SCSI EH without scmd.
+ */
+void scsi_schedule_eh(struct Scsi_Host *shost)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(shost->host_lock, flags);
+
+       if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
+           scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
+               shost->host_eh_scheduled++;
+               scsi_eh_wakeup(shost);
+       }
+
+       spin_unlock_irqrestore(shost->host_lock, flags);
+}
+EXPORT_SYMBOL_GPL(scsi_schedule_eh);
+
+/**
  * scsi_eh_scmd_add - add scsi cmd to error handling.
  * @scmd:      scmd to run eh on.
  * @eh_flag:   optional SCSI_EH flag.
  *
  * Return value:
  *     0 on failure.
- **/
+ */
 int scsi_eh_scmd_add(struct scsi_cmnd *scmd, int eh_flag)
 {
        struct Scsi_Host *shost = scmd->device->host;
@@ -99,7 +121,7 @@ int scsi_eh_scmd_add(struct scsi_cmnd *scmd, int eh_flag)
  *    This should be turned into an inline function.  Each scsi command
  *    has its own timer, and as it is added to the queue, we set up the
  *    timer.  When the command completes, we cancel the timer.
- **/
+ */
 void scsi_add_timer(struct scsi_cmnd *scmd, int timeout,
                    void (*complete)(struct scsi_cmnd *))
 {
@@ -133,7 +155,7 @@ void scsi_add_timer(struct scsi_cmnd *scmd, int timeout,
  * Return value:
  *     1 if we were able to detach the timer.  0 if we blew it, and the
  *     timer function has already started to run.
- **/
+ */
 int scsi_delete_timer(struct scsi_cmnd *scmd)
 {
        int rtn;
@@ -159,13 +181,22 @@ int scsi_delete_timer(struct scsi_cmnd *scmd)
  *     only in that the normal completion handling might run, but if the
  *     normal completion function determines that the timer has already
  *     fired, then it mustn't do anything.
- **/
+ */
 void scsi_times_out(struct scsi_cmnd *scmd)
 {
+       enum scsi_eh_timer_return (* eh_timed_out)(struct scsi_cmnd *);
+
        scsi_log_completion(scmd, TIMEOUT_ERROR);
 
        if (scmd->device->host->transportt->eh_timed_out)
-               switch (scmd->device->host->transportt->eh_timed_out(scmd)) {
+               eh_timed_out = scmd->device->host->transportt->eh_timed_out;
+       else if (scmd->device->host->hostt->eh_timed_out)
+               eh_timed_out = scmd->device->host->hostt->eh_timed_out;
+       else
+               eh_timed_out = NULL;
+
+       if (eh_timed_out)
+               switch (eh_timed_out(scmd)) {
                case EH_HANDLED:
                        __scsi_done(scmd);
                        return;
@@ -193,7 +224,7 @@ void scsi_times_out(struct scsi_cmnd *scmd)
  *
  * Return value:
  *     0 when dev was taken offline by error recovery. 1 OK to proceed.
- **/
+ */
 int scsi_block_when_processing_errors(struct scsi_device *sdev)
 {
        int online;
@@ -214,7 +245,7 @@ EXPORT_SYMBOL(scsi_block_when_processing_errors);
  * scsi_eh_prt_fail_stats - Log info on failures.
  * @shost:     scsi host being recovered.
  * @work_q:    Queue of scsi cmds to process.
- **/
+ */
 static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
                                          struct list_head *work_q)
 {
@@ -264,7 +295,7 @@ static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
  * Notes:
  *     When a deferred error is detected the current command has
  *     not been executed and needs retrying.
- **/
+ */
 static int scsi_check_sense(struct scsi_cmnd *scmd)
 {
        struct scsi_sense_hdr sshdr;
@@ -337,6 +368,11 @@ static int scsi_check_sense(struct scsi_cmnd *scmd)
                return SUCCESS;
 
        case MEDIUM_ERROR:
+               if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
+                   sshdr.asc == 0x13 || /* AMNF DATA FIELD */
+                   sshdr.asc == 0x14) { /* RECORD NOT FOUND */
+                       return SUCCESS;
+               }
                return NEEDS_RETRY;
 
        case HARDWARE_ERROR:
@@ -362,7 +398,7 @@ static int scsi_check_sense(struct scsi_cmnd *scmd)
  *    queued during error recovery.  the main difference here is that we
  *    don't allow for the possibility of retries here, and we are a lot
  *    more restrictive about what we consider acceptable.
- **/
+ */
 static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
 {
        /*
@@ -416,7 +452,7 @@ static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
 /**
  * scsi_eh_done - Completion function for error handling.
  * @scmd:      Cmd that is done.
- **/
+ */
 static void scsi_eh_done(struct scsi_cmnd *scmd)
 {
        struct completion     *eh_action;
@@ -431,26 +467,279 @@ static void scsi_eh_done(struct scsi_cmnd *scmd)
 }
 
 /**
- * scsi_send_eh_cmnd  - send a cmd to a device as part of error recovery.
- * @scmd:      SCSI Cmd to send.
- * @timeout:   Timeout for cmd.
+ * scsi_try_host_reset - ask host adapter to reset itself
+ * @scmd:      SCSI cmd to send hsot reset.
+ */
+static int scsi_try_host_reset(struct scsi_cmnd *scmd)
+{
+       unsigned long flags;
+       int rtn;
+
+       SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Host RST\n",
+                                         __FUNCTION__));
+
+       if (!scmd->device->host->hostt->eh_host_reset_handler)
+               return FAILED;
+
+       rtn = scmd->device->host->hostt->eh_host_reset_handler(scmd);
+
+       if (rtn == SUCCESS) {
+               if (!scmd->device->host->hostt->skip_settle_delay)
+                       ssleep(HOST_RESET_SETTLE_TIME);
+               spin_lock_irqsave(scmd->device->host->host_lock, flags);
+               scsi_report_bus_reset(scmd->device->host,
+                                     scmd_channel(scmd));
+               spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
+       }
+
+       return rtn;
+}
+
+/**
+ * scsi_try_bus_reset - ask host to perform a bus reset
+ * @scmd:      SCSI cmd to send bus reset.
+ */
+static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
+{
+       unsigned long flags;
+       int rtn;
+
+       SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Bus RST\n",
+                                         __FUNCTION__));
+
+       if (!scmd->device->host->hostt->eh_bus_reset_handler)
+               return FAILED;
+
+       rtn = scmd->device->host->hostt->eh_bus_reset_handler(scmd);
+
+       if (rtn == SUCCESS) {
+               if (!scmd->device->host->hostt->skip_settle_delay)
+                       ssleep(BUS_RESET_SETTLE_TIME);
+               spin_lock_irqsave(scmd->device->host->host_lock, flags);
+               scsi_report_bus_reset(scmd->device->host,
+                                     scmd_channel(scmd));
+               spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
+       }
+
+       return rtn;
+}
+
+static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
+{
+       sdev->was_reset = 1;
+       sdev->expecting_cc_ua = 1;
+}
+
+/**
+ * scsi_try_target_reset - Ask host to perform a target reset
+ * @scmd:      SCSI cmd used to send a target reset
+ *
+ * Notes:
+ *    There is no timeout for this operation.  if this operation is
+ *    unreliable for a given host, then the host itself needs to put a
+ *    timer on it, and set the host back to a consistent state prior to
+ *    returning.
+ */
+static int scsi_try_target_reset(struct scsi_cmnd *scmd)
+{
+       unsigned long flags;
+       int rtn;
+
+       if (!scmd->device->host->hostt->eh_target_reset_handler)
+               return FAILED;
+
+       rtn = scmd->device->host->hostt->eh_target_reset_handler(scmd);
+       if (rtn == SUCCESS) {
+               spin_lock_irqsave(scmd->device->host->host_lock, flags);
+               __starget_for_each_device(scsi_target(scmd->device), NULL,
+                                         __scsi_report_device_reset);
+               spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
+       }
+
+       return rtn;
+}
+
+/**
+ * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
+ * @scmd:      SCSI cmd used to send BDR
+ *
+ * Notes:
+ *    There is no timeout for this operation.  if this operation is
+ *    unreliable for a given host, then the host itself needs to put a
+ *    timer on it, and set the host back to a consistent state prior to
+ *    returning.
+ */
+static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
+{
+       int rtn;
+
+       if (!scmd->device->host->hostt->eh_device_reset_handler)
+               return FAILED;
+
+       rtn = scmd->device->host->hostt->eh_device_reset_handler(scmd);
+       if (rtn == SUCCESS)
+               __scsi_report_device_reset(scmd->device, NULL);
+       return rtn;
+}
+
+static int __scsi_try_to_abort_cmd(struct scsi_cmnd *scmd)
+{
+       if (!scmd->device->host->hostt->eh_abort_handler)
+               return FAILED;
+
+       return scmd->device->host->hostt->eh_abort_handler(scmd);
+}
+
+/**
+ * scsi_try_to_abort_cmd - Ask host to abort a running command.
+ * @scmd:      SCSI cmd to abort from Lower Level.
+ *
+ * Notes:
+ *    This function will not return until the user's completion function
+ *    has been called.  there is no timeout on this operation.  if the
+ *    author of the low-level driver wishes this operation to be timed,
+ *    they can provide this facility themselves.  helper functions in
+ *    scsi_error.c can be supplied to make this easier to do.
+ */
+static int scsi_try_to_abort_cmd(struct scsi_cmnd *scmd)
+{
+       /*
+        * scsi_done was called just after the command timed out and before
+        * we had a chance to process it. (db)
+        */
+       if (scmd->serial_number == 0)
+               return SUCCESS;
+       return __scsi_try_to_abort_cmd(scmd);
+}
+
+static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
+{
+       if (__scsi_try_to_abort_cmd(scmd) != SUCCESS)
+               if (scsi_try_bus_device_reset(scmd) != SUCCESS)
+                       if (scsi_try_target_reset(scmd) != SUCCESS)
+                               if (scsi_try_bus_reset(scmd) != SUCCESS)
+                                       scsi_try_host_reset(scmd);
+}
+
+/**
+ * scsi_eh_prep_cmnd  - Save a scsi command info as part of error recory
+ * @scmd:       SCSI command structure to hijack
+ * @ses:        structure to save restore information
+ * @cmnd:       CDB to send. Can be NULL if no new cmnd is needed
+ * @cmnd_size:  size in bytes of @cmnd (must be <= BLK_MAX_CDB)
+ * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
+ *
+ * This function is used to save a scsi command information before re-execution
+ * as part of the error recovery process.  If @sense_bytes is 0 the command
+ * sent must be one that does not transfer any data.  If @sense_bytes != 0
+ * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
+ * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
+ */
+void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
+                       unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
+{
+       struct scsi_device *sdev = scmd->device;
+
+       /*
+        * We need saved copies of a number of fields - this is because
+        * error handling may need to overwrite these with different values
+        * to run different commands, and once error handling is complete,
+        * we will need to restore these values prior to running the actual
+        * command.
+        */
+       ses->cmd_len = scmd->cmd_len;
+       ses->cmnd = scmd->cmnd;
+       ses->data_direction = scmd->sc_data_direction;
+       ses->sdb = scmd->sdb;
+       ses->next_rq = scmd->request->next_rq;
+       ses->result = scmd->result;
+
+       scmd->cmnd = ses->eh_cmnd;
+       memset(scmd->cmnd, 0, BLK_MAX_CDB);
+       memset(&scmd->sdb, 0, sizeof(scmd->sdb));
+       scmd->request->next_rq = NULL;
+
+       if (sense_bytes) {
+               scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
+                                        sense_bytes);
+               sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
+                           scmd->sdb.length);
+               scmd->sdb.table.sgl = &ses->sense_sgl;
+               scmd->sc_data_direction = DMA_FROM_DEVICE;
+               scmd->sdb.table.nents = 1;
+               scmd->cmnd[0] = REQUEST_SENSE;
+               scmd->cmnd[4] = scmd->sdb.length;
+               scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
+       } else {
+               scmd->sc_data_direction = DMA_NONE;
+               if (cmnd) {
+                       BUG_ON(cmnd_size > BLK_MAX_CDB);
+                       memcpy(scmd->cmnd, cmnd, cmnd_size);
+                       scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
+               }
+       }
+
+       scmd->underflow = 0;
+
+       if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
+               scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
+                       (sdev->lun << 5 & 0xe0);
+
+       /*
+        * Zero the sense buffer.  The scsi spec mandates that any
+        * untransferred sense data should be interpreted as being zero.
+        */
+       memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
+}
+EXPORT_SYMBOL(scsi_eh_prep_cmnd);
+
+/**
+ * scsi_eh_restore_cmnd  - Restore a scsi command info as part of error recory
+ * @scmd:       SCSI command structure to restore
+ * @ses:        saved information from a coresponding call to scsi_prep_eh_cmnd
+ *
+ * Undo any damage done by above scsi_prep_eh_cmnd().
+ */
+void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
+{
+       /*
+        * Restore original data
+        */
+       scmd->cmd_len = ses->cmd_len;
+       scmd->cmnd = ses->cmnd;
+       scmd->sc_data_direction = ses->data_direction;
+       scmd->sdb = ses->sdb;
+       scmd->request->next_rq = ses->next_rq;
+       scmd->result = ses->result;
+}
+EXPORT_SYMBOL(scsi_eh_restore_cmnd);
+
+/**
+ * scsi_send_eh_cmnd  - submit a scsi command as part of error recory
+ * @scmd:       SCSI command structure to hijack
+ * @cmnd:       CDB to send
+ * @cmnd_size:  size in bytes of @cmnd
+ * @timeout:    timeout for this request
+ * @sense_bytes: size of sense data to copy or 0
+ *
+ * This function is used to send a scsi command down to a target device
+ * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
  *
  * Return value:
  *    SUCCESS or FAILED or NEEDS_RETRY
- **/
-static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, int timeout)
+ */
+static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
+                            int cmnd_size, int timeout, unsigned sense_bytes)
 {
        struct scsi_device *sdev = scmd->device;
        struct Scsi_Host *shost = sdev->host;
-       DECLARE_COMPLETION(done);
+       DECLARE_COMPLETION_ONSTACK(done);
        unsigned long timeleft;
        unsigned long flags;
+       struct scsi_eh_save ses;
        int rtn;
 
-       if (sdev->scsi_level <= SCSI_2)
-               scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
-                       (sdev->lun << 5 & 0xe0);
-
+       scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
        shost->eh_action = &done;
 
        spin_lock_irqsave(shost->host_lock, flags);
@@ -490,16 +779,11 @@ static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, int timeout)
                        break;
                }
        } else {
-               /*
-                * FIXME(eric) - we are not tracking whether we could
-                * abort a timed out command or not.  not sure how
-                * we should treat them differently anyways.
-                */
-               if (shost->hostt->eh_abort_handler)
-                       shost->hostt->eh_abort_handler(scmd);
+               scsi_abort_eh_cmnd(scmd);
                rtn = FAILED;
        }
 
+       scsi_eh_restore_cmnd(scmd, &ses);
        return rtn;
 }
 
@@ -511,60 +795,10 @@ static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, int timeout)
  *    Some hosts automatically obtain this information, others require
  *    that we obtain it on our own. This function will *not* return until
  *    the command either times out, or it completes.
- **/
+ */
 static int scsi_request_sense(struct scsi_cmnd *scmd)
 {
-       static unsigned char generic_sense[6] =
-       {REQUEST_SENSE, 0, 0, 0, 252, 0};
-       unsigned char *scsi_result;
-       int saved_result;
-       int rtn;
-
-       memcpy(scmd->cmnd, generic_sense, sizeof(generic_sense));
-
-       scsi_result = kmalloc(252, GFP_ATOMIC | ((scmd->device->host->hostt->unchecked_isa_dma) ? __GFP_DMA : 0));
-
-
-       if (unlikely(!scsi_result)) {
-               printk(KERN_ERR "%s: cannot allocate scsi_result.\n",
-                      __FUNCTION__);
-               return FAILED;
-       }
-
-       /*
-        * zero the sense buffer.  some host adapters automatically always
-        * request sense, so it is not a good idea that
-        * scmd->request_buffer and scmd->sense_buffer point to the same
-        * address (db).  0 is not a valid sense code. 
-        */
-       memset(scmd->sense_buffer, 0, sizeof(scmd->sense_buffer));
-       memset(scsi_result, 0, 252);
-
-       saved_result = scmd->result;
-       scmd->request_buffer = scsi_result;
-       scmd->request_bufflen = 252;
-       scmd->use_sg = 0;
-       scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
-       scmd->sc_data_direction = DMA_FROM_DEVICE;
-       scmd->underflow = 0;
-
-       rtn = scsi_send_eh_cmnd(scmd, SENSE_TIMEOUT);
-
-       /* last chance to have valid sense data */
-       if(!SCSI_SENSE_VALID(scmd)) {
-               memcpy(scmd->sense_buffer, scmd->request_buffer,
-                      sizeof(scmd->sense_buffer));
-       }
-
-       kfree(scsi_result);
-
-       /*
-        * when we eventually call scsi_finish, we really wish to complete
-        * the original request, so let's restore the original data. (db)
-        */
-       scsi_setup_cmd_retry(scmd);
-       scmd->result = saved_result;
-       return rtn;
+       return scsi_send_eh_cmnd(scmd, NULL, 0, SENSE_TIMEOUT, ~0);
 }
 
 /**
@@ -575,20 +809,14 @@ static int scsi_request_sense(struct scsi_cmnd *scmd)
  * Notes:
  *    We don't want to use the normal command completion while we are are
  *    still handling errors - it may cause other commands to be queued,
- *    and that would disturb what we are doing.  thus we really want to
+ *    and that would disturb what we are doing.  Thus we really want to
  *    keep a list of pending commands for final completion, and once we
  *    are ready to leave error handling we handle completion for real.
- **/
+ */
 void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
 {
        scmd->device->host->host_failed--;
        scmd->eh_eflags = 0;
-
-       /*
-        * set this back so that the upper level can correctly free up
-        * things.
-        */
-       scsi_setup_cmd_retry(scmd);
        list_move_tail(&scmd->eh_entry, done_q);
 }
 EXPORT_SYMBOL(scsi_eh_finish_cmd);
@@ -596,7 +824,7 @@ EXPORT_SYMBOL(scsi_eh_finish_cmd);
 /**
  * scsi_eh_get_sense - Get device sense data.
  * @work_q:    Queue of commands to process.
- * @done_q:    Queue of proccessed commands..
+ * @done_q:    Queue of processed commands.
  *
  * Description:
  *    See if we need to request sense information.  if so, then get it
@@ -604,7 +832,7 @@ EXPORT_SYMBOL(scsi_eh_finish_cmd);
  *
  * Notes:
  *    This has the unfortunate side effect that if a shost adapter does
- *    not automatically request sense information, that we end up shutting
+ *    not automatically request sense information, we end up shutting
  *    it down before we request it.
  *
  *    All drivers should request sense information internally these days,
@@ -612,9 +840,9 @@ EXPORT_SYMBOL(scsi_eh_finish_cmd);
  *
  *    XXX: Long term this code should go away, but that needs an audit of
  *         all LLDDs first.
- **/
-static int scsi_eh_get_sense(struct list_head *work_q,
-                            struct list_head *done_q)
+ */
+int scsi_eh_get_sense(struct list_head *work_q,
+                     struct list_head *done_q)
 {
        struct scsi_cmnd *scmd, *next;
        int rtn;
@@ -656,98 +884,50 @@ static int scsi_eh_get_sense(struct list_head *work_q,
 
        return list_empty(work_q);
 }
-
-/**
- * scsi_try_to_abort_cmd - Ask host to abort a running command.
- * @scmd:      SCSI cmd to abort from Lower Level.
- *
- * Notes:
- *    This function will not return until the user's completion function
- *    has been called.  there is no timeout on this operation.  if the
- *    author of the low-level driver wishes this operation to be timed,
- *    they can provide this facility themselves.  helper functions in
- *    scsi_error.c can be supplied to make this easier to do.
- **/
-static int scsi_try_to_abort_cmd(struct scsi_cmnd *scmd)
-{
-       if (!scmd->device->host->hostt->eh_abort_handler)
-               return FAILED;
-
-       /*
-        * scsi_done was called just after the command timed out and before
-        * we had a chance to process it. (db)
-        */
-       if (scmd->serial_number == 0)
-               return SUCCESS;
-       return scmd->device->host->hostt->eh_abort_handler(scmd);
-}
+EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
 
 /**
  * scsi_eh_tur - Send TUR to device.
- * @scmd:      Scsi cmd to send TUR
+ * @scmd:      &scsi_cmnd to send TUR
  *
  * Return value:
  *    0 - Device is ready. 1 - Device NOT ready.
- **/
+ */
 static int scsi_eh_tur(struct scsi_cmnd *scmd)
 {
        static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
        int retry_cnt = 1, rtn;
-       int saved_result;
 
 retry_tur:
-       memcpy(scmd->cmnd, tur_command, sizeof(tur_command));
-
-       /*
-        * zero the sense buffer.  the scsi spec mandates that any
-        * untransferred sense data should be interpreted as being zero.
-        */
-       memset(scmd->sense_buffer, 0, sizeof(scmd->sense_buffer));
-
-       saved_result = scmd->result;
-       scmd->request_buffer = NULL;
-       scmd->request_bufflen = 0;
-       scmd->use_sg = 0;
-       scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
-       scmd->underflow = 0;
-       scmd->sc_data_direction = DMA_NONE;
-
-       rtn = scsi_send_eh_cmnd(scmd, SENSE_TIMEOUT);
+       rtn = scsi_send_eh_cmnd(scmd, tur_command, 6, SENSE_TIMEOUT, 0);
 
-       /*
-        * when we eventually call scsi_finish, we really wish to complete
-        * the original request, so let's restore the original data. (db)
-        */
-       scsi_setup_cmd_retry(scmd);
-       scmd->result = saved_result;
-
-       /*
-        * hey, we are done.  let's look to see what happened.
-        */
        SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd %p rtn %x\n",
                __FUNCTION__, scmd, rtn));
-       if (rtn == SUCCESS)
-               return 0;
-       else if (rtn == NEEDS_RETRY) {
+
+       switch (rtn) {
+       case NEEDS_RETRY:
                if (retry_cnt--)
                        goto retry_tur;
+               /*FALLTHRU*/
+       case SUCCESS:
                return 0;
+       default:
+               return 1;
        }
-       return 1;
 }
 
 /**
- * scsi_eh_abort_cmds - abort canceled commands.
- * @shost:     scsi host being recovered.
- * @eh_done_q: list_head for processed commands.
+ * scsi_eh_abort_cmds - abort pending commands.
+ * @work_q:    &list_head for pending commands.
+ * @done_q:    &list_head for processed commands.
  *
  * Decription:
  *    Try and see whether or not it makes sense to try and abort the
- *    running command.  this only works out to be the case if we have one
- *    command that has timed out.  if the command simply failed, it makes
+ *    running command.  This only works out to be the case if we have one
+ *    command that has timed out.  If the command simply failed, it makes
  *    no sense to try and abort the command, since as far as the shost
  *    adapter is concerned, it isn't running.
- **/
+ */
 static int scsi_eh_abort_cmds(struct list_head *work_q,
                              struct list_head *done_q)
 {
@@ -780,91 +960,40 @@ static int scsi_eh_abort_cmds(struct list_head *work_q,
 }
 
 /**
- * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
- * @scmd:      SCSI cmd used to send BDR       
- *
- * Notes:
- *    There is no timeout for this operation.  if this operation is
- *    unreliable for a given host, then the host itself needs to put a
- *    timer on it, and set the host back to a consistent state prior to
- *    returning.
- **/
-static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
-{
-       int rtn;
-
-       if (!scmd->device->host->hostt->eh_device_reset_handler)
-               return FAILED;
-
-       rtn = scmd->device->host->hostt->eh_device_reset_handler(scmd);
-       if (rtn == SUCCESS) {
-               scmd->device->was_reset = 1;
-               scmd->device->expecting_cc_ua = 1;
-       }
-
-       return rtn;
-}
-
-/**
  * scsi_eh_try_stu - Send START_UNIT to device.
- * @scmd:      Scsi cmd to send START_UNIT
+ * @scmd:      &scsi_cmnd to send START_UNIT
  *
  * Return value:
  *    0 - Device is ready. 1 - Device NOT ready.
- **/
+ */
 static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
 {
        static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
-       int rtn;
-       int saved_result;
-
-       if (!scmd->device->allow_restart)
-               return 1;
-
-       memcpy(scmd->cmnd, stu_command, sizeof(stu_command));
 
-       /*
-        * zero the sense buffer.  the scsi spec mandates that any
-        * untransferred sense data should be interpreted as being zero.
-        */
-       memset(scmd->sense_buffer, 0, sizeof(scmd->sense_buffer));
+       if (scmd->device->allow_restart) {
+               int i, rtn = NEEDS_RETRY;
 
-       saved_result = scmd->result;
-       scmd->request_buffer = NULL;
-       scmd->request_bufflen = 0;
-       scmd->use_sg = 0;
-       scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
-       scmd->underflow = 0;
-       scmd->sc_data_direction = DMA_NONE;
+               for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
+                       rtn = scsi_send_eh_cmnd(scmd, stu_command, 6,
+                                               scmd->device->timeout, 0);
 
-       rtn = scsi_send_eh_cmnd(scmd, START_UNIT_TIMEOUT);
-
-       /*
-        * when we eventually call scsi_finish, we really wish to complete
-        * the original request, so let's restore the original data. (db)
-        */
-       scsi_setup_cmd_retry(scmd);
-       scmd->result = saved_result;
+               if (rtn == SUCCESS)
+                       return 0;
+       }
 
-       /*
-        * hey, we are done.  let's look to see what happened.
-        */
-       SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd %p rtn %x\n",
-               __FUNCTION__, scmd, rtn));
-       if (rtn == SUCCESS)
-               return 0;
        return 1;
 }
 
  /**
  * scsi_eh_stu - send START_UNIT if needed
- * @shost:     scsi host being recovered.
- * @eh_done_q: list_head for processed commands.
+ * @shost:     &scsi host being recovered.
+ * @work_q:     &list_head for pending commands.
+ * @done_q:    &list_head for processed commands.
  *
  * Notes:
  *    If commands are failing due to not ready, initializing command required,
  *     try revalidating the device, which will end up sending a start unit. 
- **/
+ */
 static int scsi_eh_stu(struct Scsi_Host *shost,
                              struct list_head *work_q,
                              struct list_head *done_q)
@@ -910,14 +1039,15 @@ static int scsi_eh_stu(struct Scsi_Host *shost,
 /**
  * scsi_eh_bus_device_reset - send bdr if needed
  * @shost:     scsi host being recovered.
- * @eh_done_q: list_head for processed commands.
+ * @work_q:     &list_head for pending commands.
+ * @done_q:    &list_head for processed commands.
  *
  * Notes:
- *    Try a bus device reset.  still, look to see whether we have multiple
+ *    Try a bus device reset.  Still, look to see whether we have multiple
  *    devices that are jammed or not - if we have multiple devices, it
  *    makes no sense to try bus_device_reset - we really would need to try
  *    a bus_reset instead. 
- **/
+ */
 static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
                                    struct list_head *work_q,
                                    struct list_head *done_q)
@@ -964,68 +1094,61 @@ static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
 }
 
 /**
- * scsi_try_bus_reset - ask host to perform a bus reset
- * @scmd:      SCSI cmd to send bus reset.
- **/
-static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
-{
-       unsigned long flags;
-       int rtn;
-
-       SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Bus RST\n",
-                                         __FUNCTION__));
-
-       if (!scmd->device->host->hostt->eh_bus_reset_handler)
-               return FAILED;
-
-       rtn = scmd->device->host->hostt->eh_bus_reset_handler(scmd);
-
-       if (rtn == SUCCESS) {
-               if (!scmd->device->host->hostt->skip_settle_delay)
-                       ssleep(BUS_RESET_SETTLE_TIME);
-               spin_lock_irqsave(scmd->device->host->host_lock, flags);
-               scsi_report_bus_reset(scmd->device->host,
-                                     scmd_channel(scmd));
-               spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
-       }
-
-       return rtn;
-}
-
-/**
- * scsi_try_host_reset - ask host adapter to reset itself
- * @scmd:      SCSI cmd to send hsot reset.
- **/
-static int scsi_try_host_reset(struct scsi_cmnd *scmd)
+ * scsi_eh_target_reset - send target reset if needed
+ * @shost:     scsi host being recovered.
+ * @work_q:     &list_head for pending commands.
+ * @done_q:    &list_head for processed commands.
+ *
+ * Notes:
+ *    Try a target reset.
+ */
+static int scsi_eh_target_reset(struct Scsi_Host *shost,
+                               struct list_head *work_q,
+                               struct list_head *done_q)
 {
-       unsigned long flags;
+       struct scsi_cmnd *scmd, *tgtr_scmd, *next;
+       unsigned int id;
        int rtn;
 
-       SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Host RST\n",
-                                         __FUNCTION__));
-
-       if (!scmd->device->host->hostt->eh_host_reset_handler)
-               return FAILED;
-
-       rtn = scmd->device->host->hostt->eh_host_reset_handler(scmd);
+       for (id = 0; id <= shost->max_id; id++) {
+               tgtr_scmd = NULL;
+               list_for_each_entry(scmd, work_q, eh_entry) {
+                       if (id == scmd_id(scmd)) {
+                               tgtr_scmd = scmd;
+                               break;
+                       }
+               }
+               if (!tgtr_scmd)
+                       continue;
 
-       if (rtn == SUCCESS) {
-               if (!scmd->device->host->hostt->skip_settle_delay)
-                       ssleep(HOST_RESET_SETTLE_TIME);
-               spin_lock_irqsave(scmd->device->host->host_lock, flags);
-               scsi_report_bus_reset(scmd->device->host,
-                                     scmd_channel(scmd));
-               spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
+               SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending target reset "
+                                                 "to target %d\n",
+                                                 current->comm, id));
+               rtn = scsi_try_target_reset(tgtr_scmd);
+               if (rtn == SUCCESS) {
+                       list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
+                               if (id == scmd_id(scmd))
+                                       if (!scsi_device_online(scmd->device) ||
+                                           !scsi_eh_tur(tgtr_scmd))
+                                               scsi_eh_finish_cmd(scmd,
+                                                                  done_q);
+                       }
+               } else
+                       SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Target reset"
+                                                         " failed target: "
+                                                         "%d\n",
+                                                         current->comm, id));
        }
 
-       return rtn;
+       return list_empty(work_q);
 }
 
 /**
  * scsi_eh_bus_reset - send a bus reset 
- * @shost:     scsi host being recovered.
- * @eh_done_q: list_head for processed commands.
- **/
+ * @shost:     &scsi host being recovered.
+ * @work_q:     &list_head for pending commands.
+ * @done_q:    &list_head for processed commands.
+ */
 static int scsi_eh_bus_reset(struct Scsi_Host *shost,
                             struct list_head *work_q,
                             struct list_head *done_q)
@@ -1082,7 +1205,7 @@ static int scsi_eh_bus_reset(struct Scsi_Host *shost,
  * scsi_eh_host_reset - send a host reset 
  * @work_q:    list_head for processed commands.
  * @done_q:    list_head for processed commands.
- **/
+ */
 static int scsi_eh_host_reset(struct list_head *work_q,
                              struct list_head *done_q)
 {
@@ -1117,17 +1240,15 @@ static int scsi_eh_host_reset(struct list_head *work_q,
  * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
  * @work_q:    list_head for processed commands.
  * @done_q:    list_head for processed commands.
- *
- **/
+ */
 static void scsi_eh_offline_sdevs(struct list_head *work_q,
                                  struct list_head *done_q)
 {
        struct scsi_cmnd *scmd, *next;
 
        list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
-               sdev_printk(KERN_INFO, scmd->device,
-                           "scsi: Device offlined - not"
-                           " ready after error recovery\n");
+               sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
+                           "not ready after error recovery\n");
                scsi_device_set_state(scmd->device, SDEV_OFFLINE);
                if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD) {
                        /*
@@ -1152,7 +1273,7 @@ static void scsi_eh_offline_sdevs(struct list_head *work_q,
  *    is woken.  In cases where the error code indicates an error that
  *    doesn't require the error handler read (i.e. we don't need to
  *    abort/reset), this function should return SUCCESS.
- **/
+ */
 int scsi_decide_disposition(struct scsi_cmnd *scmd)
 {
        int rtn;
@@ -1333,7 +1454,7 @@ int scsi_decide_disposition(struct scsi_cmnd *scmd)
  *
  *     If scsi_allocate_request() fails for what ever reason, we
  *     completely forget to lock the door.
- **/
+ */
 static void scsi_eh_lock_door(struct scsi_device *sdev)
 {
        unsigned char cmnd[MAX_COMMAND_SIZE];
@@ -1357,7 +1478,7 @@ static void scsi_eh_lock_door(struct scsi_device *sdev)
  * Notes:
  *    When we entered the error handler, we blocked all further i/o to
  *    this device.  we need to 'reverse' this process.
- **/
+ */
 static void scsi_restart_operations(struct Scsi_Host *shost)
 {
        struct scsi_device *sdev;
@@ -1401,25 +1522,27 @@ static void scsi_restart_operations(struct Scsi_Host *shost)
 /**
  * scsi_eh_ready_devs - check device ready state and recover if not.
  * @shost:     host to be recovered.
- * @eh_done_q: list_head for processed commands.
- *
- **/
-static void scsi_eh_ready_devs(struct Scsi_Host *shost,
-                              struct list_head *work_q,
-                              struct list_head *done_q)
+ * @work_q:     &list_head for pending commands.
+ * @done_q:    &list_head for processed commands.
+ */
+void scsi_eh_ready_devs(struct Scsi_Host *shost,
+                       struct list_head *work_q,
+                       struct list_head *done_q)
 {
        if (!scsi_eh_stu(shost, work_q, done_q))
                if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
-                       if (!scsi_eh_bus_reset(shost, work_q, done_q))
-                               if (!scsi_eh_host_reset(work_q, done_q))
-                                       scsi_eh_offline_sdevs(work_q, done_q);
+                       if (!scsi_eh_target_reset(shost, work_q, done_q))
+                               if (!scsi_eh_bus_reset(shost, work_q, done_q))
+                                       if (!scsi_eh_host_reset(work_q, done_q))
+                                               scsi_eh_offline_sdevs(work_q,
+                                                                     done_q);
 }
+EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
 
 /**
  * scsi_eh_flush_done_q - finish processed commands or retry them.
  * @done_q:    list_head of processed commands.
- *
- **/
+ */
 void scsi_eh_flush_done_q(struct list_head *done_q)
 {
        struct scsi_cmnd *scmd, *next;
@@ -1473,7 +1596,7 @@ EXPORT_SYMBOL(scsi_eh_flush_done_q);
  *    scsi_finish_cmd() called for it.  we do all of the retry stuff
  *    here, so when we restart the host after we return it should have an
  *    empty queue.
- **/
+ */
 static void scsi_unjam_host(struct Scsi_Host *shost)
 {
        unsigned long flags;
@@ -1500,13 +1623,11 @@ static void scsi_unjam_host(struct Scsi_Host *shost)
  * Notes:
  *    This is the main error handling loop.  This is run as a kernel thread
  *    for every SCSI host and handles all error handling activity.
- **/
+ */
 int scsi_error_handler(void *data)
 {
        struct Scsi_Host *shost = data;
 
-       current->flags |= PF_NOFREEZE;
-
        /*
         * We use TASK_INTERRUPTIBLE so that the thread is not
         * counted against the load average as a running process.
@@ -1515,7 +1636,7 @@ int scsi_error_handler(void *data)
         */
        set_current_state(TASK_INTERRUPTIBLE);
        while (!kthread_should_stop()) {
-               if (shost->host_failed == 0 ||
+               if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
                    shost->host_failed != shost->host_busy) {
                        SCSI_LOG_ERROR_RECOVERY(1,
                                printk("Error handler scsi_eh_%d sleeping\n",
@@ -1584,10 +1705,8 @@ void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
        struct scsi_device *sdev;
 
        __shost_for_each_device(sdev, shost) {
-               if (channel == sdev_channel(sdev)) {
-                       sdev->was_reset = 1;
-                       sdev->expecting_cc_ua = 1;
-               }
+               if (channel == sdev_channel(sdev))
+                       __scsi_report_device_reset(sdev, NULL);
        }
 }
 EXPORT_SYMBOL(scsi_report_bus_reset);
@@ -1620,10 +1739,8 @@ void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
 
        __shost_for_each_device(sdev, shost) {
                if (channel == sdev_channel(sdev) &&
-                   target == sdev_id(sdev)) {
-                       sdev->was_reset = 1;
-                       sdev->expecting_cc_ua = 1;
-               }
+                   target == sdev_id(sdev))
+                       __scsi_report_device_reset(sdev, NULL);
        }
 }
 EXPORT_SYMBOL(scsi_report_device_reset);
@@ -1650,20 +1767,19 @@ int
 scsi_reset_provider(struct scsi_device *dev, int flag)
 {
        struct scsi_cmnd *scmd = scsi_get_command(dev, GFP_KERNEL);
+       struct Scsi_Host *shost = dev->host;
        struct request req;
+       unsigned long flags;
        int rtn;
 
+       blk_rq_init(NULL, &req);
        scmd->request = &req;
        memset(&scmd->eh_timeout, 0, sizeof(scmd->eh_timeout));
 
-       memset(&scmd->cmnd, '\0', sizeof(scmd->cmnd));
-    
+       scmd->cmnd = req.cmd;
+
        scmd->scsi_done         = scsi_reset_provider_done_command;
-       scmd->done                      = NULL;
-       scmd->buffer                    = NULL;
-       scmd->bufflen                   = 0;
-       scmd->request_buffer            = NULL;
-       scmd->request_bufflen           = 0;
+       memset(&scmd->sdb, 0, sizeof(scmd->sdb));
 
        scmd->cmd_len                   = 0;
 
@@ -1671,11 +1787,9 @@ scsi_reset_provider(struct scsi_device *dev, int flag)
 
        init_timer(&scmd->eh_timeout);
 
-       /*
-        * Sometimes the command can get back into the timer chain,
-        * so use the pid as an identifier.
-        */
-       scmd->pid                       = 0;
+       spin_lock_irqsave(shost->host_lock, flags);
+       shost->tmf_in_progress = 1;
+       spin_unlock_irqrestore(shost->host_lock, flags);
 
        switch (flag) {
        case SCSI_TRY_RESET_DEVICE:
@@ -1683,6 +1797,11 @@ scsi_reset_provider(struct scsi_device *dev, int flag)
                if (rtn == SUCCESS)
                        break;
                /* FALLTHROUGH */
+       case SCSI_TRY_RESET_TARGET:
+               rtn = scsi_try_target_reset(scmd);
+               if (rtn == SUCCESS)
+                       break;
+               /* FALLTHROUGH */
        case SCSI_TRY_RESET_BUS:
                rtn = scsi_try_bus_reset(scmd);
                if (rtn == SUCCESS)
@@ -1695,6 +1814,22 @@ scsi_reset_provider(struct scsi_device *dev, int flag)
                rtn = FAILED;
        }
 
+       spin_lock_irqsave(shost->host_lock, flags);
+       shost->tmf_in_progress = 0;
+       spin_unlock_irqrestore(shost->host_lock, flags);
+
+       /*
+        * be sure to wake up anyone who was sleeping or had their queue
+        * suspended while we performed the TMF.
+        */
+       SCSI_LOG_ERROR_RECOVERY(3,
+               printk("%s: waking up host to restart after TMF\n",
+               __FUNCTION__));
+
+       wake_up(&shost->host_wait);
+
+       scsi_run_host_queues(shost);
+
        scsi_next_command(scmd);
        return rtn;
 }
@@ -1718,7 +1853,7 @@ EXPORT_SYMBOL(scsi_reset_provider);
  *
  * Return value:
  *     1 if valid sense data information found, else 0;
- **/
+ */
 int scsi_normalize_sense(const u8 *sense_buffer, int sb_len,
                          struct scsi_sense_hdr *sshdr)
 {
@@ -1768,14 +1903,12 @@ int scsi_command_normalize_sense(struct scsi_cmnd *cmd,
                                 struct scsi_sense_hdr *sshdr)
 {
        return scsi_normalize_sense(cmd->sense_buffer,
-                       sizeof(cmd->sense_buffer), sshdr);
+                       SCSI_SENSE_BUFFERSIZE, sshdr);
 }
 EXPORT_SYMBOL(scsi_command_normalize_sense);
 
 /**
- * scsi_sense_desc_find - search for a given descriptor type in
- *                     descriptor sense data format.
- *
+ * scsi_sense_desc_find - search for a given descriptor type in        descriptor sense data format.
  * @sense_buffer:      byte array of descriptor format sense data
  * @sb_len:            number of valid bytes in sense_buffer
  * @desc_type:         value of descriptor type to find
@@ -1786,7 +1919,7 @@ EXPORT_SYMBOL(scsi_command_normalize_sense);
  *
  * Return value:
  *     pointer to start of (first) descriptor if found else NULL
- **/
+ */
 const u8 * scsi_sense_desc_find(const u8 * sense_buffer, int sb_len,
                                int desc_type)
 {
@@ -1814,9 +1947,7 @@ const u8 * scsi_sense_desc_find(const u8 * sense_buffer, int sb_len,
 EXPORT_SYMBOL(scsi_sense_desc_find);
 
 /**
- * scsi_get_sense_info_fld - attempts to get information field from
- *                     sense data (either fixed or descriptor format)
- *
+ * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
  * @sense_buffer:      byte array of sense data
  * @sb_len:            number of valid bytes in sense_buffer
  * @info_out:          pointer to 64 integer where 8 or 4 byte information
@@ -1824,7 +1955,7 @@ EXPORT_SYMBOL(scsi_sense_desc_find);
  *
  * Return value:
  *     1 if information field found, 0 if not found.
- **/
+ */
 int scsi_get_sense_info_fld(const u8 * sense_buffer, int sb_len,
                            u64 * info_out)
 {
@@ -1864,3 +1995,31 @@ int scsi_get_sense_info_fld(const u8 * sense_buffer, int sb_len,
        }
 }
 EXPORT_SYMBOL(scsi_get_sense_info_fld);
+
+/**
+ * scsi_build_sense_buffer - build sense data in a buffer
+ * @desc:      Sense format (non zero == descriptor format,
+ *             0 == fixed format)
+ * @buf:       Where to build sense data
+ * @key:       Sense key
+ * @asc:       Additional sense code
+ * @ascq:      Additional sense code qualifier
+ *
+ **/
+void scsi_build_sense_buffer(int desc, u8 *buf, u8 key, u8 asc, u8 ascq)
+{
+       if (desc) {
+               buf[0] = 0x72;  /* descriptor, current */
+               buf[1] = key;
+               buf[2] = asc;
+               buf[3] = ascq;
+               buf[7] = 0;
+       } else {
+               buf[0] = 0x70;  /* fixed, current */
+               buf[2] = key;
+               buf[7] = 0xa;
+               buf[12] = asc;
+               buf[13] = ascq;
+       }
+}
+EXPORT_SYMBOL(scsi_build_sense_buffer);