aboutsummaryrefslogtreecommitdiff
path: root/drivers/scsi/scsi_error.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/scsi/scsi_error.c')
-rw-r--r--drivers/scsi/scsi_error.c2050
1 files changed, 2050 insertions, 0 deletions
diff --git a/drivers/scsi/scsi_error.c b/drivers/scsi/scsi_error.c
new file mode 100644
index 00000000000..9bc597bd13b
--- /dev/null
+++ b/drivers/scsi/scsi_error.c
@@ -0,0 +1,2050 @@
+/*
+ * scsi_error.c Copyright (C) 1997 Eric Youngdale
+ *
+ * SCSI error/timeout handling
+ * Initial versions: Eric Youngdale. Based upon conversations with
+ * Leonard Zubkoff and David Miller at Linux Expo,
+ * ideas originating from all over the place.
+ *
+ * Restructured scsi_unjam_host and associated functions.
+ * September 04, 2002 Mike Anderson (andmike@us.ibm.com)
+ *
+ * Forward port of Russell King's (rmk@arm.linux.org.uk) changes and
+ * minor cleanups.
+ * September 30, 2002 Mike Anderson (andmike@us.ibm.com)
+ */
+
+#include <linux/module.h>
+#include <linux/sched.h>
+#include <linux/timer.h>
+#include <linux/string.h>
+#include <linux/slab.h>
+#include <linux/kernel.h>
+#include <linux/interrupt.h>
+#include <linux/blkdev.h>
+#include <linux/delay.h>
+
+#include <scsi/scsi.h>
+#include <scsi/scsi_dbg.h>
+#include <scsi/scsi_device.h>
+#include <scsi/scsi_eh.h>
+#include <scsi/scsi_host.h>
+#include <scsi/scsi_ioctl.h>
+#include <scsi/scsi_request.h>
+
+#include "scsi_priv.h"
+#include "scsi_logging.h"
+
+#define SENSE_TIMEOUT (10*HZ)
+#define START_UNIT_TIMEOUT (30*HZ)
+
+/*
+ * These should *probably* be handled by the host itself.
+ * Since it is allowed to sleep, it probably should.
+ */
+#define BUS_RESET_SETTLE_TIME (10)
+#define HOST_RESET_SETTLE_TIME (10)
+
+/* called with shost->host_lock held */
+void scsi_eh_wakeup(struct Scsi_Host *shost)
+{
+ if (shost->host_busy == shost->host_failed) {
+ up(shost->eh_wait);
+ SCSI_LOG_ERROR_RECOVERY(5,
+ printk("Waking error handler thread\n"));
+ }
+}
+
+/**
+ * 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;
+ unsigned long flags;
+
+ if (shost->eh_wait == NULL)
+ return 0;
+
+ spin_lock_irqsave(shost->host_lock, flags);
+
+ scsi_eh_eflags_set(scmd, eh_flag);
+ /*
+ * FIXME: Can we stop setting owner and state.
+ */
+ scmd->owner = SCSI_OWNER_ERROR_HANDLER;
+ scmd->state = SCSI_STATE_FAILED;
+ /*
+ * Set the serial_number_at_timeout to the current
+ * serial_number
+ */
+ scmd->serial_number_at_timeout = scmd->serial_number;
+ list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
+ set_bit(SHOST_RECOVERY, &shost->shost_state);
+ shost->host_failed++;
+ scsi_eh_wakeup(shost);
+ spin_unlock_irqrestore(shost->host_lock, flags);
+ return 1;
+}
+
+/**
+ * scsi_add_timer - Start timeout timer for a single scsi command.
+ * @scmd: scsi command that is about to start running.
+ * @timeout: amount of time to allow this command to run.
+ * @complete: timeout function to call if timer isn't canceled.
+ *
+ * Notes:
+ * 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 *))
+{
+
+ /*
+ * If the clock was already running for this command, then
+ * first delete the timer. The timer handling code gets rather
+ * confused if we don't do this.
+ */
+ if (scmd->eh_timeout.function)
+ del_timer(&scmd->eh_timeout);
+
+ scmd->eh_timeout.data = (unsigned long)scmd;
+ scmd->eh_timeout.expires = jiffies + timeout;
+ scmd->eh_timeout.function = (void (*)(unsigned long)) complete;
+
+ SCSI_LOG_ERROR_RECOVERY(5, printk("%s: scmd: %p, time:"
+ " %d, (%p)\n", __FUNCTION__,
+ scmd, timeout, complete));
+
+ add_timer(&scmd->eh_timeout);
+}
+EXPORT_SYMBOL(scsi_add_timer);
+
+/**
+ * scsi_delete_timer - Delete/cancel timer for a given function.
+ * @scmd: Cmd that we are canceling timer for
+ *
+ * Notes:
+ * This should be turned into an inline function.
+ *
+ * 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;
+
+ rtn = del_timer(&scmd->eh_timeout);
+
+ SCSI_LOG_ERROR_RECOVERY(5, printk("%s: scmd: %p,"
+ " rtn: %d\n", __FUNCTION__,
+ scmd, rtn));
+
+ scmd->eh_timeout.data = (unsigned long)NULL;
+ scmd->eh_timeout.function = NULL;
+
+ return rtn;
+}
+EXPORT_SYMBOL(scsi_delete_timer);
+
+/**
+ * scsi_times_out - Timeout function for normal scsi commands.
+ * @scmd: Cmd that is timing out.
+ *
+ * Notes:
+ * We do not need to lock this. There is the potential for a race
+ * 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)
+{
+ scsi_log_completion(scmd, TIMEOUT_ERROR);
+
+ if (scmd->device->host->hostt->eh_timed_out)
+ switch (scmd->device->host->hostt->eh_timed_out(scmd)) {
+ case EH_HANDLED:
+ __scsi_done(scmd);
+ return;
+ case EH_RESET_TIMER:
+ /* This allows a single retry even of a command
+ * with allowed == 0 */
+ if (scmd->retries++ > scmd->allowed)
+ break;
+ scsi_add_timer(scmd, scmd->timeout_per_command,
+ scsi_times_out);
+ return;
+ case EH_NOT_HANDLED:
+ break;
+ }
+
+ if (unlikely(!scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD))) {
+ panic("Error handler thread not present at %p %p %s %d",
+ scmd, scmd->device->host, __FILE__, __LINE__);
+ }
+}
+
+/**
+ * scsi_block_when_processing_errors - Prevent cmds from being queued.
+ * @sdev: Device on which we are performing recovery.
+ *
+ * Description:
+ * We block until the host is out of error recovery, and then check to
+ * see whether the host or the device is offline.
+ *
+ * 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;
+
+ wait_event(sdev->host->host_wait, (!test_bit(SHOST_RECOVERY, &sdev->host->shost_state)));
+
+ online = scsi_device_online(sdev);
+
+ SCSI_LOG_ERROR_RECOVERY(5, printk("%s: rtn: %d\n", __FUNCTION__,
+ online));
+
+ return online;
+}
+EXPORT_SYMBOL(scsi_block_when_processing_errors);
+
+#ifdef CONFIG_SCSI_LOGGING
+/**
+ * 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)
+{
+ struct scsi_cmnd *scmd;
+ struct scsi_device *sdev;
+ int total_failures = 0;
+ int cmd_failed = 0;
+ int cmd_cancel = 0;
+ int devices_failed = 0;
+
+ shost_for_each_device(sdev, shost) {
+ list_for_each_entry(scmd, work_q, eh_entry) {
+ if (scmd->device == sdev) {
+ ++total_failures;
+ if (scsi_eh_eflags_chk(scmd,
+ SCSI_EH_CANCEL_CMD))
+ ++cmd_cancel;
+ else
+ ++cmd_failed;
+ }
+ }
+
+ if (cmd_cancel || cmd_failed) {
+ SCSI_LOG_ERROR_RECOVERY(3,
+ printk("%s: %d:%d:%d:%d cmds failed: %d,"
+ " cancel: %d\n",
+ __FUNCTION__, shost->host_no,
+ sdev->channel, sdev->id, sdev->lun,
+ cmd_failed, cmd_cancel));
+ cmd_cancel = 0;
+ cmd_failed = 0;
+ ++devices_failed;
+ }
+ }
+
+ SCSI_LOG_ERROR_RECOVERY(2, printk("Total of %d commands on %d"
+ " devices require eh work\n",
+ total_failures, devices_failed));
+}
+#endif
+
+/**
+ * scsi_check_sense - Examine scsi cmd sense
+ * @scmd: Cmd to have sense checked.
+ *
+ * Return value:
+ * SUCCESS or FAILED or NEEDS_RETRY
+ *
+ * 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;
+
+ if (! scsi_command_normalize_sense(scmd, &sshdr))
+ return FAILED; /* no valid sense data */
+
+ if (scsi_sense_is_deferred(&sshdr))
+ return NEEDS_RETRY;
+
+ /*
+ * Previous logic looked for FILEMARK, EOM or ILI which are
+ * mainly associated with tapes and returned SUCCESS.
+ */
+ if (sshdr.response_code == 0x70) {
+ /* fixed format */
+ if (scmd->sense_buffer[2] & 0xe0)
+ return SUCCESS;
+ } else {
+ /*
+ * descriptor format: look for "stream commands sense data
+ * descriptor" (see SSC-3). Assume single sense data
+ * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
+ */
+ if ((sshdr.additional_length > 3) &&
+ (scmd->sense_buffer[8] == 0x4) &&
+ (scmd->sense_buffer[11] & 0xe0))
+ return SUCCESS;
+ }
+
+ switch (sshdr.sense_key) {
+ case NO_SENSE:
+ return SUCCESS;
+ case RECOVERED_ERROR:
+ return /* soft_error */ SUCCESS;
+
+ case ABORTED_COMMAND:
+ return NEEDS_RETRY;
+ case NOT_READY:
+ case UNIT_ATTENTION:
+ /*
+ * if we are expecting a cc/ua because of a bus reset that we
+ * performed, treat this just as a retry. otherwise this is
+ * information that we should pass up to the upper-level driver
+ * so that we can deal with it there.
+ */
+ if (scmd->device->expecting_cc_ua) {
+ scmd->device->expecting_cc_ua = 0;
+ return NEEDS_RETRY;
+ }
+ /*
+ * if the device is in the process of becoming ready, we
+ * should retry.
+ */
+ if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
+ return NEEDS_RETRY;
+ /*
+ * if the device is not started, we need to wake
+ * the error handler to start the motor
+ */
+ if (scmd->device->allow_restart &&
+ (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
+ return FAILED;
+ return SUCCESS;
+
+ /* these three are not supported */
+ case COPY_ABORTED:
+ case VOLUME_OVERFLOW:
+ case MISCOMPARE:
+ return SUCCESS;
+
+ case MEDIUM_ERROR:
+ return NEEDS_RETRY;
+
+ case HARDWARE_ERROR:
+ if (scmd->device->retry_hwerror)
+ return NEEDS_RETRY;
+ else
+ return SUCCESS;
+
+ case ILLEGAL_REQUEST:
+ case BLANK_CHECK:
+ case DATA_PROTECT:
+ default:
+ return SUCCESS;
+ }
+}
+
+/**
+ * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
+ * @scmd: SCSI cmd to examine.
+ *
+ * Notes:
+ * This is *only* called when we are examining the status of commands
+ * 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)
+{
+ /*
+ * first check the host byte, to see if there is anything in there
+ * that would indicate what we need to do.
+ */
+ if (host_byte(scmd->result) == DID_RESET) {
+ /*
+ * rats. we are already in the error handler, so we now
+ * get to try and figure out what to do next. if the sense
+ * is valid, we have a pretty good idea of what to do.
+ * if not, we mark it as FAILED.
+ */
+ return scsi_check_sense(scmd);
+ }
+ if (host_byte(scmd->result) != DID_OK)
+ return FAILED;
+
+ /*
+ * next, check the message byte.
+ */
+ if (msg_byte(scmd->result) != COMMAND_COMPLETE)
+ return FAILED;
+
+ /*
+ * now, check the status byte to see if this indicates
+ * anything special.
+ */
+ switch (status_byte(scmd->result)) {
+ case GOOD:
+ case COMMAND_TERMINATED:
+ return SUCCESS;
+ case CHECK_CONDITION:
+ return scsi_check_sense(scmd);
+ case CONDITION_GOOD:
+ case INTERMEDIATE_GOOD:
+ case INTERMEDIATE_C_GOOD:
+ /*
+ * who knows? FIXME(eric)
+ */
+ return SUCCESS;
+ case BUSY:
+ case QUEUE_FULL:
+ case RESERVATION_CONFLICT:
+ default:
+ return FAILED;
+ }
+ return FAILED;
+}
+
+/**
+ * scsi_eh_times_out - timeout function for error handling.
+ * @scmd: Cmd that is timing out.
+ *
+ * Notes:
+ * During error handling, the kernel thread will be sleeping waiting
+ * for some action to complete on the device. our only job is to
+ * record that it timed out, and to wake up the thread.
+ **/
+static void scsi_eh_times_out(struct scsi_cmnd *scmd)
+{
+ scsi_eh_eflags_set(scmd, SCSI_EH_REC_TIMEOUT);
+ SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd:%p\n", __FUNCTION__,
+ scmd));
+
+ if (scmd->device->host->eh_action)
+ up(scmd->device->host->eh_action);
+}
+
+/**
+ * scsi_eh_done - Completion function for error handling.
+ * @scmd: Cmd that is done.
+ **/
+static void scsi_eh_done(struct scsi_cmnd *scmd)
+{
+ /*
+ * if the timeout handler is already running, then just set the
+ * flag which says we finished late, and return. we have no
+ * way of stopping the timeout handler from running, so we must
+ * always defer to it.
+ */
+ if (del_timer(&scmd->eh_timeout)) {
+ scmd->request->rq_status = RQ_SCSI_DONE;
+ scmd->owner = SCSI_OWNER_ERROR_HANDLER;
+
+ SCSI_LOG_ERROR_RECOVERY(3, printk("%s scmd: %p result: %x\n",
+ __FUNCTION__, scmd, scmd->result));
+
+ if (scmd->device->host->eh_action)
+ up(scmd->device->host->eh_action);
+ }
+}
+
+/**
+ * scsi_send_eh_cmnd - send a cmd to a device as part of error recovery.
+ * @scmd: SCSI Cmd to send.
+ * @timeout: Timeout for cmd.
+ *
+ * Notes:
+ * The initialization of the structures is quite a bit different in
+ * this case, and furthermore, there is a different completion handler
+ * vs scsi_dispatch_cmd.
+ * Return value:
+ * SUCCESS or FAILED or NEEDS_RETRY
+ **/
+static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, int timeout)
+{
+ struct Scsi_Host *host = scmd->device->host;
+ DECLARE_MUTEX_LOCKED(sem);
+ unsigned long flags;
+ int rtn = SUCCESS;
+
+ /*
+ * we will use a queued command if possible, otherwise we will
+ * emulate the queuing and calling of completion function ourselves.
+ */
+ scmd->owner = SCSI_OWNER_LOWLEVEL;
+
+ if (scmd->device->scsi_level <= SCSI_2)
+ scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
+ (scmd->device->lun << 5 & 0xe0);
+
+ scsi_add_timer(scmd, timeout, scsi_eh_times_out);
+
+ /*
+ * set up the semaphore so we wait for the command to complete.
+ */
+ scmd->device->host->eh_action = &sem;
+ scmd->request->rq_status = RQ_SCSI_BUSY;
+
+ spin_lock_irqsave(scmd->device->host->host_lock, flags);
+ scsi_log_send(scmd);
+ host->hostt->queuecommand(scmd, scsi_eh_done);
+ spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
+
+ down(&sem);
+ scsi_log_completion(scmd, SUCCESS);
+
+ scmd->device->host->eh_action = NULL;
+
+ /*
+ * see if timeout. if so, tell the host to forget about it.
+ * in other words, we don't want a callback any more.
+ */
+ if (scsi_eh_eflags_chk(scmd, SCSI_EH_REC_TIMEOUT)) {
+ scsi_eh_eflags_clr(scmd, SCSI_EH_REC_TIMEOUT);
+ scmd->owner = SCSI_OWNER_LOWLEVEL;
+
+ /*
+ * as far as the low level driver is
+ * concerned, this command is still active, so
+ * we must give the low level driver a chance
+ * to abort it. (db)
+ *
+ * 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.
+ */
+ spin_lock_irqsave(scmd->device->host->host_lock, flags);
+ if (scmd->device->host->hostt->eh_abort_handler)
+ scmd->device->host->hostt->eh_abort_handler(scmd);
+ spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
+
+ scmd->request->rq_status = RQ_SCSI_DONE;
+ scmd->owner = SCSI_OWNER_ERROR_HANDLER;
+
+ rtn = FAILED;
+ }
+
+ SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd: %p, rtn:%x\n",
+ __FUNCTION__, scmd, rtn));
+
+ /*
+ * now examine the actual status codes to see whether the command
+ * actually did complete normally.
+ */
+ if (rtn == SUCCESS) {
+ rtn = scsi_eh_completed_normally(scmd);
+ SCSI_LOG_ERROR_RECOVERY(3,
+ printk("%s: scsi_eh_completed_normally %x\n",
+ __FUNCTION__, rtn));
+ switch (rtn) {
+ case SUCCESS:
+ case NEEDS_RETRY:
+ case FAILED:
+ break;
+ default:
+ rtn = FAILED;
+ break;
+ }
+ }
+
+ return rtn;
+}
+
+/**
+ * scsi_request_sense - Request sense data from a particular target.
+ * @scmd: SCSI cmd for request sense.
+ *
+ * Notes:
+ * 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;
+}
+
+/**
+ * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
+ * @scmd: Original SCSI cmd that eh has finished.
+ * @done_q: Queue for processed commands.
+ *
+ * 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
+ * keep a list of pending commands for final completion, and once we
+ * are ready to leave error handling we handle completion for real.
+ **/
+static void scsi_eh_finish_cmd(struct scsi_cmnd *scmd,
+ struct list_head *done_q)
+{
+ scmd->device->host->host_failed--;
+ scmd->state = SCSI_STATE_BHQUEUE;
+
+ scsi_eh_eflags_clr_all(scmd);
+
+ /*
+ * 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);
+}
+
+/**
+ * scsi_eh_get_sense - Get device sense data.
+ * @work_q: Queue of commands to process.
+ * @done_q: Queue of proccessed commands..
+ *
+ * Description:
+ * See if we need to request sense information. if so, then get it
+ * now, so we have a better idea of what to do.
+ *
+ * Notes:
+ * This has the unfortunate side effect that if a shost adapter does
+ * not automatically request sense information, that we end up shutting
+ * it down before we request it.
+ *
+ * All drivers should request sense information internally these days,
+ * so for now all I have to say is tough noogies if you end up in here.
+ *
+ * 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)
+{
+ struct list_head *lh, *lh_sf;
+ struct scsi_cmnd *scmd;
+ int rtn;
+
+ list_for_each_safe(lh, lh_sf, work_q) {
+ scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
+ if (scsi_eh_eflags_chk(scmd, SCSI_EH_CANCEL_CMD) ||
+ SCSI_SENSE_VALID(scmd))
+ continue;
+
+ SCSI_LOG_ERROR_RECOVERY(2, printk("%s: requesting sense"
+ " for id: %d\n",
+ current->comm,
+ scmd->device->id));
+ rtn = scsi_request_sense(scmd);
+ if (rtn != SUCCESS)
+ continue;
+
+ SCSI_LOG_ERROR_RECOVERY(3, printk("sense requested for %p"
+ " result %x\n", scmd,
+ scmd->result));
+ SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense("bh", scmd));
+
+ rtn = scsi_decide_disposition(scmd);
+
+ /*
+ * if the result was normal, then just pass it along to the
+ * upper level.
+ */
+ if (rtn == SUCCESS)
+ /* we don't want this command reissued, just
+ * finished with the sense data, so set
+ * retries to the max allowed to ensure it
+ * won't get reissued */
+ scmd->retries = scmd->allowed;
+ else if (rtn != NEEDS_RETRY)
+ continue;
+
+ scsi_eh_finish_cmd(scmd, done_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)
+{
+ unsigned long flags;
+ int rtn = FAILED;
+
+ if (!scmd->device->host->hostt->eh_abort_handler)
+ return rtn;
+
+ /*
+ * 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;
+
+ scmd->owner = SCSI_OWNER_LOWLEVEL;
+
+ spin_lock_irqsave(scmd->device->host->host_lock, flags);
+ rtn = scmd->device->host->hostt->eh_abort_handler(scmd);
+ spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
+
+ return rtn;
+}
+
+/**
+ * scsi_eh_tur - Send TUR to device.
+ * @scmd: Scsi cmd 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;
+
+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));
+
+ 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);
+
+ /*
+ * 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);
+
+ /*
+ * 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)
+ if (retry_cnt--)
+ goto retry_tur;
+ return 1;
+}
+
+/**
+ * scsi_eh_abort_cmds - abort canceled commands.
+ * @shost: scsi host being recovered.
+ * @eh_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
+ * 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)
+{
+ struct list_head *lh, *lh_sf;
+ struct scsi_cmnd *scmd;
+ int rtn;
+
+ list_for_each_safe(lh, lh_sf, work_q) {
+ scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
+ if (!scsi_eh_eflags_chk(scmd, SCSI_EH_CANCEL_CMD))
+ continue;
+ SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting cmd:"
+ "0x%p\n", current->comm,
+ scmd));
+ rtn = scsi_try_to_abort_cmd(scmd);
+ if (rtn == SUCCESS) {
+ scsi_eh_eflags_clr(scmd, SCSI_EH_CANCEL_CMD);
+ if (!scsi_device_online(scmd->device) ||
+ !scsi_eh_tur(scmd)) {
+ scsi_eh_finish_cmd(scmd, done_q);
+ }
+
+ } else
+ SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting"
+ " cmd failed:"
+ "0x%p\n",
+ current->comm,
+ scmd));
+ }
+
+ return list_empty(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)
+{
+ unsigned long flags;
+ int rtn = FAILED;
+
+ if (!scmd->device->host->hostt->eh_device_reset_handler)
+ return rtn;
+
+ scmd->owner = SCSI_OWNER_LOWLEVEL;
+
+ spin_lock_irqsave(scmd->device->host->host_lock, flags);
+ rtn = scmd->device->host->hostt->eh_device_reset_handler(scmd);
+ spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
+
+ 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
+ *
+ * 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;
+
+ 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));
+
+ 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, 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);
+
+ /*
+ * 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.
+ *
+ * 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)
+{
+ struct list_head *lh, *lh_sf;
+ struct scsi_cmnd *scmd, *stu_scmd;
+ struct scsi_device *sdev;
+
+ shost_for_each_device(sdev, shost) {
+ stu_scmd = NULL;
+ list_for_each_entry(scmd, work_q, eh_entry)
+ if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
+ scsi_check_sense(scmd) == FAILED ) {
+ stu_scmd = scmd;
+ break;
+ }
+
+ if (!stu_scmd)
+ continue;
+
+ SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending START_UNIT to sdev:"
+ " 0x%p\n", current->comm, sdev));
+
+ if (!scsi_eh_try_stu(stu_scmd)) {
+ if (!scsi_device_online(sdev) ||
+ !scsi_eh_tur(stu_scmd)) {
+ list_for_each_safe(lh, lh_sf, work_q) {
+ scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
+ if (scmd->device == sdev)
+ scsi_eh_finish_cmd(scmd, done_q);
+ }
+ }
+ } else {
+ SCSI_LOG_ERROR_RECOVERY(3,
+ printk("%s: START_UNIT failed to sdev:"
+ " 0x%p\n", current->comm, sdev));
+ }
+ }
+
+ return list_empty(work_q);
+}
+
+
+/**
+ * scsi_eh_bus_device_reset - send bdr if needed
+ * @shost: scsi host being recovered.
+ * @eh_done_q: list_head for processed commands.
+ *
+ * Notes:
+ * 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)
+{
+ struct list_head *lh, *lh_sf;
+ struct scsi_cmnd *scmd, *bdr_scmd;
+ struct scsi_device *sdev;
+ int rtn;
+
+ shost_for_each_device(sdev, shost) {
+ bdr_scmd = NULL;
+ list_for_each_entry(scmd, work_q, eh_entry)
+ if (scmd->device == sdev) {
+ bdr_scmd = scmd;
+ break;
+ }
+
+ if (!bdr_scmd)
+ continue;
+
+ SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BDR sdev:"
+ " 0x%p\n", current->comm,
+ sdev));
+ rtn = scsi_try_bus_device_reset(bdr_scmd);
+ if (rtn == SUCCESS) {
+ if (!scsi_device_online(sdev) ||
+ !scsi_eh_tur(bdr_scmd)) {
+ list_for_each_safe(lh, lh_sf,
+ work_q) {
+ scmd = list_entry(lh, struct
+ scsi_cmnd,
+ eh_entry);
+ if (scmd->device == sdev)
+ scsi_eh_finish_cmd(scmd,
+ done_q);
+ }
+ }
+ } else {
+ SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BDR"
+ " failed sdev:"
+ "0x%p\n",
+ current->comm,
+ sdev));
+ }
+ }
+
+ return list_empty(work_q);
+}
+
+/**
+ * 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__));
+ scmd->owner = SCSI_OWNER_LOWLEVEL;
+ scmd->serial_number_at_timeout = scmd->serial_number;
+
+ if (!scmd->device->host->hostt->eh_bus_reset_handler)
+ return FAILED;
+
+ spin_lock_irqsave(scmd->device->host->host_lock, flags);
+ rtn = scmd->device->host->hostt->eh_bus_reset_handler(scmd);
+ spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
+
+ 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->device->channel);
+ 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)
+{
+ unsigned long flags;
+ int rtn;
+
+ SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Host RST\n",
+ __FUNCTION__));
+ scmd->owner = SCSI_OWNER_LOWLEVEL;
+ scmd->serial_number_at_timeout = scmd->serial_number;
+
+ if (!scmd->device->host->hostt->eh_host_reset_handler)
+ return FAILED;
+
+ spin_lock_irqsave(scmd->device->host->host_lock, flags);
+ rtn = scmd->device->host->hostt->eh_host_reset_handler(scmd);
+ spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
+
+ 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->device->channel);
+ spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
+ }
+
+ return rtn;
+}
+
+/**
+ * scsi_eh_bus_reset - send a bus reset
+ * @shost: scsi host being recovered.
+ * @eh_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)
+{
+ struct list_head *lh, *lh_sf;
+ struct scsi_cmnd *scmd;
+ struct scsi_cmnd *chan_scmd;
+ unsigned int channel;
+ int rtn;
+
+ /*
+ * we really want to loop over the various channels, and do this on
+ * a channel by channel basis. we should also check to see if any
+ * of the failed commands are on soft_reset devices, and if so, skip
+ * the reset.
+ */
+
+ for (channel = 0; channel <= shost->max_channel; channel++) {
+ chan_scmd = NULL;
+ list_for_each_entry(scmd, work_q, eh_entry) {
+ if (channel == scmd->device->channel) {
+ chan_scmd = scmd;
+ break;
+ /*
+ * FIXME add back in some support for
+ * soft_reset devices.
+ */
+ }
+ }
+
+ if (!chan_scmd)
+ continue;
+ SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BRST chan:"
+ " %d\n", current->comm,
+ channel));
+ rtn = scsi_try_bus_reset(chan_scmd);
+ if (rtn == SUCCESS) {
+ list_for_each_safe(lh, lh_sf, work_q) {
+ scmd = list_entry(lh, struct scsi_cmnd,
+ eh_entry);
+ if (channel == scmd->device->channel)
+ if (!scsi_device_online(scmd->device) ||
+ !scsi_eh_tur(scmd))
+ scsi_eh_finish_cmd(scmd,
+ done_q);
+ }
+ } else {
+ SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BRST"
+ " failed chan: %d\n",
+ current->comm,
+