2 * scsi_error.c Copyright (C) 1997 Eric Youngdale
4 * SCSI error/timeout handling
5 * Initial versions: Eric Youngdale. Based upon conversations with
6 * Leonard Zubkoff and David Miller at Linux Expo,
7 * ideas originating from all over the place.
11 #define __NO_VERSION__
12 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/timer.h>
16 #include <linux/string.h>
17 #include <linux/slab.h>
18 #include <linux/ioport.h>
19 #include <linux/kernel.h>
20 #include <linux/stat.h>
21 #include <linux/blk.h>
22 #include <linux/interrupt.h>
23 #include <linux/delay.h>
24 #include <linux/smp_lock.h>
26 #define __KERNEL_SYSCALLS__
28 #include <linux/unistd.h>
30 #include <asm/system.h>
36 #include "constants.h"
39 * We must always allow SHUTDOWN_SIGS. Even if we are not a module,
40 * the host drivers that we are using may be loaded as modules, and
41 * when we unload these, we need to ensure that the error handler thread
44 * Note - when we unload a module, we send a SIGHUP. We mustn't
45 * enable SIGTERM, as this is how the init shuts things down when you
46 * go to single-user mode. For that matter, init also sends SIGKILL,
47 * so we mustn't enable that one either. We use SIGHUP instead. Other
48 * options would be SIGPWR, I suppose.
50 #define SHUTDOWN_SIGS (sigmask(SIGHUP))
53 #define SENSE_TIMEOUT SCSI_TIMEOUT
54 #define ABORT_TIMEOUT SCSI_TIMEOUT
55 #define RESET_TIMEOUT SCSI_TIMEOUT
57 #define SENSE_TIMEOUT (10*HZ)
58 #define RESET_TIMEOUT (2*HZ)
59 #define ABORT_TIMEOUT (15*HZ)
65 * These should *probably* be handled by the host itself.
66 * Since it is allowed to sleep, it probably should.
68 #define BUS_RESET_SETTLE_TIME 5*HZ
69 #define HOST_RESET_SETTLE_TIME 10*HZ
72 static const char RCSid[] = "$Header: /cvshome/samwise/ppclinux/drivers/scsi/scsi_error.c,v 1.1.1.1 2005/04/11 02:50:36 jack Exp $";
74 STATIC int scsi_check_sense(Scsi_Cmnd * SCpnt);
75 STATIC int scsi_request_sense(Scsi_Cmnd *);
76 STATIC void scsi_send_eh_cmnd(Scsi_Cmnd * SCpnt, int timeout);
77 STATIC int scsi_try_to_abort_command(Scsi_Cmnd *, int);
78 STATIC int scsi_test_unit_ready(Scsi_Cmnd *);
79 STATIC int scsi_try_bus_device_reset(Scsi_Cmnd *, int timeout);
80 STATIC int scsi_try_bus_reset(Scsi_Cmnd *);
81 STATIC int scsi_try_host_reset(Scsi_Cmnd *);
82 STATIC int scsi_unit_is_ready(Scsi_Cmnd *);
83 STATIC void scsi_eh_action_done(Scsi_Cmnd *, int);
84 STATIC int scsi_eh_retry_command(Scsi_Cmnd *);
85 STATIC int scsi_eh_completed_normally(Scsi_Cmnd * SCpnt);
86 STATIC void scsi_restart_operations(struct Scsi_Host *);
87 STATIC void scsi_eh_finish_command(Scsi_Cmnd ** SClist, Scsi_Cmnd * SCpnt);
91 * Function: scsi_add_timer()
93 * Purpose: Start timeout timer for a single scsi command.
95 * Arguments: SCset - command that is about to start running.
96 * timeout - amount of time to allow this command to run.
97 * complete - timeout function to call if timer isn't
102 * Notes: This should be turned into an inline function.
104 * More Notes: Each scsi command has it's own timer, and as it is added to
105 * the queue, we set up the timer. When the command completes,
106 * we cancel the timer. Pretty simple, really, especially
107 * compared to the old way of handling this crap.
109 void scsi_add_timer(Scsi_Cmnd * SCset,
111 void (*complete) (Scsi_Cmnd *))
115 * If the clock was already running for this command, then
116 * first delete the timer. The timer handling code gets rather
117 * confused if we don't do this.
119 if (SCset->eh_timeout.function != NULL) {
120 del_timer(&SCset->eh_timeout);
122 SCset->eh_timeout.data = (unsigned long) SCset;
123 SCset->eh_timeout.expires = jiffies + timeout;
124 SCset->eh_timeout.function = (void (*)(unsigned long)) complete;
126 SCset->done_late = 0;
128 SCSI_LOG_ERROR_RECOVERY(5, printk("Adding timer for command %p at %d (%p)\n", SCset, timeout, complete));
130 add_timer(&SCset->eh_timeout);
135 * Function: scsi_delete_timer()
137 * Purpose: Delete/cancel timer for a given function.
139 * Arguments: SCset - command that we are canceling timer for.
141 * Returns: 1 if we were able to detach the timer. 0 if we
142 * blew it, and the timer function has already started
145 * Notes: This should be turned into an inline function.
147 int scsi_delete_timer(Scsi_Cmnd * SCset)
151 rtn = del_timer(&SCset->eh_timeout);
153 SCSI_LOG_ERROR_RECOVERY(5, printk("Clearing timer for command %p %d\n", SCset, rtn));
155 SCset->eh_timeout.data = (unsigned long) NULL;
156 SCset->eh_timeout.function = NULL;
162 * Function: scsi_times_out()
164 * Purpose: Timeout function for normal scsi commands..
166 * Arguments: SCpnt - command that is timing out.
170 * Notes: We do not need to lock this. There is the potential for
171 * a race only in that the normal completion handling might
172 * run, but if the normal completion function determines
173 * that the timer has already fired, then it mustn't do
176 void scsi_times_out(Scsi_Cmnd * SCpnt)
179 * Notify the low-level code that this operation failed and we are
180 * reposessing the command.
185 * Allow the host adapter to push a queue ordering tag
186 * out to the bus to force the command in question to complete.
187 * If the host wants to do this, then we just restart the timer
188 * for the command. Before we really do this, some real thought
189 * as to the optimum way to handle this should be done. We *do*
190 * need to force ordering every so often to ensure that all requests
191 * do eventually complete, but I am not sure if this is the best way
192 * to actually go about it.
194 * Better yet, force a sync here, but don't block since we are in an
197 if (SCpnt->host->hostt->eh_ordered_queue_tag) {
198 if ((*SCpnt->host->hostt->eh_ordered_queue_tag) (SCpnt)) {
199 scsi_add_timer(SCpnt, SCpnt->internal_timeout,
205 * FIXME(eric) - add a second special interface to handle this
206 * case. Ideally that interface can also be used to request
209 if (SCpnt->host->can_queue) {
210 SCpnt->host->hostt->queuecommand(SCpnt, NULL);
214 /* Set the serial_number_at_timeout to the current serial_number */
215 SCpnt->serial_number_at_timeout = SCpnt->serial_number;
217 SCpnt->eh_state = FAILED;
218 SCpnt->state = SCSI_STATE_TIMEOUT;
219 SCpnt->owner = SCSI_OWNER_ERROR_HANDLER;
221 SCpnt->host->in_recovery = 1;
222 SCpnt->host->host_failed++;
224 SCSI_LOG_TIMEOUT(3, printk("Command timed out active=%d busy=%d failed=%d\n",
225 atomic_read(&SCpnt->host->host_active),
226 SCpnt->host->host_busy,
227 SCpnt->host->host_failed));
230 * If the host is having troubles, then look to see if this was the last
231 * command that might have failed. If so, wake up the error handler.
233 if( SCpnt->host->eh_wait == NULL ) {
234 panic("Error handler thread not present at %p %p %s %d",
235 SCpnt, SCpnt->host, __FILE__, __LINE__);
237 if (SCpnt->host->host_busy == SCpnt->host->host_failed) {
238 up(SCpnt->host->eh_wait);
243 * Function scsi_block_when_processing_errors
245 * Purpose: Prevent more commands from being queued while error recovery
248 * Arguments: SDpnt - device on which we are performing recovery.
250 * Returns: FALSE The device was taken offline by error recovery.
251 * TRUE OK to proceed.
253 * Notes: We block until the host is out of error recovery, and then
254 * check to see whether the host or the device is offline.
256 int scsi_block_when_processing_errors(Scsi_Device * SDpnt)
259 SCSI_SLEEP(&SDpnt->host->host_wait, SDpnt->host->in_recovery);
261 SCSI_LOG_ERROR_RECOVERY(5, printk("Open returning %d\n", SDpnt->online));
263 return SDpnt->online;
267 * Function: scsi_eh_times_out()
269 * Purpose: Timeout function for error handling.
271 * Arguments: SCpnt - command that is timing out.
275 * Notes: During error handling, the kernel thread will be sleeping
276 * waiting for some action to complete on the device. Our only
277 * job is to record that it timed out, and to wake up the
281 void scsi_eh_times_out(Scsi_Cmnd * SCpnt)
283 SCpnt->eh_state = SCSI_STATE_TIMEOUT;
284 SCSI_LOG_ERROR_RECOVERY(5, printk("In scsi_eh_times_out %p\n", SCpnt));
286 if (SCpnt->host->eh_action != NULL)
287 up(SCpnt->host->eh_action);
289 printk("Missing scsi error handler thread\n");
294 * Function: scsi_eh_done()
296 * Purpose: Completion function for error handling.
298 * Arguments: SCpnt - command that is timing out.
302 * Notes: During error handling, the kernel thread will be sleeping
303 * waiting for some action to complete on the device. Our only
304 * job is to record that the action completed, and to wake up the
308 void scsi_eh_done(Scsi_Cmnd * SCpnt)
313 * If the timeout handler is already running, then just set the
314 * flag which says we finished late, and return. We have no
315 * way of stopping the timeout handler from running, so we must
316 * always defer to it.
318 rtn = del_timer(&SCpnt->eh_timeout);
320 SCpnt->done_late = 1;
324 SCpnt->request.rq_status = RQ_SCSI_DONE;
326 SCpnt->owner = SCSI_OWNER_ERROR_HANDLER;
327 SCpnt->eh_state = SUCCESS;
329 SCSI_LOG_ERROR_RECOVERY(5, printk("In eh_done %p result:%x\n", SCpnt,
332 if (SCpnt->host->eh_action != NULL)
333 up(SCpnt->host->eh_action);
337 * Function: scsi_eh_action_done()
339 * Purpose: Completion function for error handling.
341 * Arguments: SCpnt - command that is timing out.
342 * answer - boolean that indicates whether operation succeeded.
346 * Notes: This callback is only used for abort and reset operations.
349 void scsi_eh_action_done(Scsi_Cmnd * SCpnt, int answer)
351 SCpnt->request.rq_status = RQ_SCSI_DONE;
353 SCpnt->owner = SCSI_OWNER_ERROR_HANDLER;
354 SCpnt->eh_state = (answer ? SUCCESS : FAILED);
356 if (SCpnt->host->eh_action != NULL)
357 up(SCpnt->host->eh_action);
361 * Function: scsi_sense_valid()
363 * Purpose: Determine whether a host has automatically obtained sense
364 * information or not. If we have it, then give a recommendation
365 * as to what we should do next.
367 int scsi_sense_valid(Scsi_Cmnd * SCpnt)
369 if (((SCpnt->sense_buffer[0] & 0x70) >> 4) != 7) {
376 * Function: scsi_eh_retry_command()
378 * Purpose: Retry the original command
380 * Returns: SUCCESS - we were able to get the sense data.
381 * FAILED - we were not able to get the sense data.
383 * Notes: This function will *NOT* return until the command either
384 * times out, or it completes.
386 STATIC int scsi_eh_retry_command(Scsi_Cmnd * SCpnt)
388 memcpy((void *) SCpnt->cmnd, (void *) SCpnt->data_cmnd,
389 sizeof(SCpnt->data_cmnd));
390 SCpnt->request_buffer = SCpnt->buffer;
391 SCpnt->request_bufflen = SCpnt->bufflen;
392 SCpnt->use_sg = SCpnt->old_use_sg;
393 SCpnt->cmd_len = SCpnt->old_cmd_len;
394 SCpnt->sc_data_direction = SCpnt->sc_old_data_direction;
395 SCpnt->underflow = SCpnt->old_underflow;
397 scsi_send_eh_cmnd(SCpnt, SCpnt->timeout_per_command);
400 * Hey, we are done. Let's look to see what happened.
402 return SCpnt->eh_state;
406 * Function: scsi_request_sense()
408 * Purpose: Request sense data from a particular target.
410 * Returns: SUCCESS - we were able to get the sense data.
411 * FAILED - we were not able to get the sense data.
413 * Notes: Some hosts automatically obtain this information, others
414 * require that we obtain it on our own.
416 * This function will *NOT* return until the command either
417 * times out, or it completes.
419 STATIC int scsi_request_sense(Scsi_Cmnd * SCpnt)
421 static unsigned char generic_sense[6] =
422 {REQUEST_SENSE, 0, 0, 0, 255, 0};
423 unsigned char scsi_result0[256], *scsi_result = NULL;
426 ASSERT_LOCK(&io_request_lock, 0);
428 memcpy((void *) SCpnt->cmnd, (void *) generic_sense,
429 sizeof(generic_sense));
431 if (SCpnt->device->scsi_level <= SCSI_2)
432 SCpnt->cmnd[1] = SCpnt->lun << 5;
434 scsi_result = (!SCpnt->host->hostt->unchecked_isa_dma)
435 ? &scsi_result0[0] : kmalloc(512, GFP_ATOMIC | GFP_DMA);
437 if (scsi_result == NULL) {
438 printk("cannot allocate scsi_result in scsi_request_sense.\n");
442 * Zero the sense buffer. Some host adapters automatically always request
443 * sense, so it is not a good idea that SCpnt->request_buffer and
444 * SCpnt->sense_buffer point to the same address (DB).
445 * 0 is not a valid sense code.
447 memset((void *) SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
448 memset((void *) scsi_result, 0, 256);
450 saved_result = SCpnt->result;
451 SCpnt->request_buffer = scsi_result;
452 SCpnt->request_bufflen = 256;
454 SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
455 SCpnt->sc_data_direction = SCSI_DATA_READ;
456 SCpnt->underflow = 0;
458 scsi_send_eh_cmnd(SCpnt, SENSE_TIMEOUT);
460 /* Last chance to have valid sense data */
461 if (!scsi_sense_valid(SCpnt))
462 memcpy((void *) SCpnt->sense_buffer,
463 SCpnt->request_buffer,
464 sizeof(SCpnt->sense_buffer));
466 if (scsi_result != &scsi_result0[0] && scsi_result != NULL)
470 * When we eventually call scsi_finish, we really wish to complete
471 * the original request, so let's restore the original data. (DB)
473 memcpy((void *) SCpnt->cmnd, (void *) SCpnt->data_cmnd,
474 sizeof(SCpnt->data_cmnd));
475 SCpnt->result = saved_result;
476 SCpnt->request_buffer = SCpnt->buffer;
477 SCpnt->request_bufflen = SCpnt->bufflen;
478 SCpnt->use_sg = SCpnt->old_use_sg;
479 SCpnt->cmd_len = SCpnt->old_cmd_len;
480 SCpnt->sc_data_direction = SCpnt->sc_old_data_direction;
481 SCpnt->underflow = SCpnt->old_underflow;
484 * Hey, we are done. Let's look to see what happened.
486 return SCpnt->eh_state;
490 * Function: scsi_test_unit_ready()
492 * Purpose: Run test unit ready command to see if the device is talking to us or not.
495 STATIC int scsi_test_unit_ready(Scsi_Cmnd * SCpnt)
497 static unsigned char tur_command[6] =
498 {TEST_UNIT_READY, 0, 0, 0, 0, 0};
500 memcpy((void *) SCpnt->cmnd, (void *) tur_command,
501 sizeof(tur_command));
503 if (SCpnt->device->scsi_level <= SCSI_2)
504 SCpnt->cmnd[1] = SCpnt->lun << 5;
507 * Zero the sense buffer. The SCSI spec mandates that any
508 * untransferred sense data should be interpreted as being zero.
510 memset((void *) SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
512 SCpnt->request_buffer = NULL;
513 SCpnt->request_bufflen = 0;
515 SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
516 SCpnt->underflow = 0;
517 SCpnt->sc_data_direction = SCSI_DATA_NONE;
519 scsi_send_eh_cmnd(SCpnt, SENSE_TIMEOUT);
522 * When we eventually call scsi_finish, we really wish to complete
523 * the original request, so let's restore the original data. (DB)
525 memcpy((void *) SCpnt->cmnd, (void *) SCpnt->data_cmnd,
526 sizeof(SCpnt->data_cmnd));
527 SCpnt->request_buffer = SCpnt->buffer;
528 SCpnt->request_bufflen = SCpnt->bufflen;
529 SCpnt->use_sg = SCpnt->old_use_sg;
530 SCpnt->cmd_len = SCpnt->old_cmd_len;
531 SCpnt->sc_data_direction = SCpnt->sc_old_data_direction;
532 SCpnt->underflow = SCpnt->old_underflow;
535 * Hey, we are done. Let's look to see what happened.
537 SCSI_LOG_ERROR_RECOVERY(3,
538 printk("scsi_test_unit_ready: SCpnt %p eh_state %x\n",
539 SCpnt, SCpnt->eh_state));
540 return SCpnt->eh_state;
544 * This would normally need to get the IO request lock,
545 * but as it doesn't actually touch anything that needs
546 * to be locked we can avoid the lock here..
549 void scsi_sleep_done(struct semaphore *sem)
556 void scsi_sleep(int timeout)
558 DECLARE_MUTEX_LOCKED(sem);
559 struct timer_list timer;
562 timer.data = (unsigned long) &sem;
563 timer.expires = jiffies + timeout;
564 timer.function = (void (*)(unsigned long)) scsi_sleep_done;
566 SCSI_LOG_ERROR_RECOVERY(5, printk("Sleeping for timer tics %d\n", timeout));
575 * Function: scsi_send_eh_cmnd
577 * Purpose: Send a command out to a device as part of error recovery.
579 * Notes: The initialization of the structures is quite a bit different
580 * in this case, and furthermore, there is a different completion
583 STATIC void scsi_send_eh_cmnd(Scsi_Cmnd * SCpnt, int timeout)
586 struct Scsi_Host *host;
588 ASSERT_LOCK(&io_request_lock, 0);
594 * We will use a queued command if possible, otherwise we will emulate the
595 * queuing and calling of completion function ourselves.
597 SCpnt->owner = SCSI_OWNER_LOWLEVEL;
599 if (host->can_queue) {
600 DECLARE_MUTEX_LOCKED(sem);
602 SCpnt->eh_state = SCSI_STATE_QUEUED;
604 scsi_add_timer(SCpnt, timeout, scsi_eh_times_out);
607 * Set up the semaphore so we wait for the command to complete.
609 SCpnt->host->eh_action = &sem;
610 SCpnt->request.rq_status = RQ_SCSI_BUSY;
612 spin_lock_irqsave(&io_request_lock, flags);
613 host->hostt->queuecommand(SCpnt, scsi_eh_done);
614 spin_unlock_irqrestore(&io_request_lock, flags);
618 SCpnt->host->eh_action = NULL;
621 * See if timeout. If so, tell the host to forget about it.
622 * In other words, we don't want a callback any more.
624 if (SCpnt->eh_state == SCSI_STATE_TIMEOUT) {
625 SCpnt->owner = SCSI_OWNER_LOWLEVEL;
628 * As far as the low level driver is
629 * concerned, this command is still active, so
630 * we must give the low level driver a chance
633 * FIXME(eric) - we are not tracking whether we could
634 * abort a timed out command or not. Not sure how
635 * we should treat them differently anyways.
637 spin_lock_irqsave(&io_request_lock, flags);
638 if (SCpnt->host->hostt->eh_abort_handler)
639 SCpnt->host->hostt->eh_abort_handler(SCpnt);
640 spin_unlock_irqrestore(&io_request_lock, flags);
642 SCpnt->request.rq_status = RQ_SCSI_DONE;
643 SCpnt->owner = SCSI_OWNER_ERROR_HANDLER;
645 SCpnt->eh_state = FAILED;
647 SCSI_LOG_ERROR_RECOVERY(5, printk("send_eh_cmnd: %p eh_state:%x\n",
648 SCpnt, SCpnt->eh_state));
653 * We damn well had better never use this code. There is no timeout
654 * protection here, since we would end up waiting in the actual low
655 * level driver, we don't know how to wake it up.
657 spin_lock_irqsave(&io_request_lock, flags);
658 temp = host->hostt->command(SCpnt);
659 spin_unlock_irqrestore(&io_request_lock, flags);
661 SCpnt->result = temp;
662 /* Fall through to code below to examine status. */
663 SCpnt->eh_state = SUCCESS;
667 * Now examine the actual status codes to see whether the command actually
668 * did complete normally.
670 if (SCpnt->eh_state == SUCCESS) {
671 int ret = scsi_eh_completed_normally(SCpnt);
672 SCSI_LOG_ERROR_RECOVERY(3,
673 printk("scsi_send_eh_cmnd: scsi_eh_completed_normally %x\n", ret));
676 SCpnt->eh_state = SUCCESS;
682 SCpnt->eh_state = FAILED;
686 SCpnt->eh_state = FAILED;
691 * Function: scsi_unit_is_ready()
693 * Purpose: Called after TEST_UNIT_READY is run, to test to see if
694 * the unit responded in a way that indicates it is ready.
696 STATIC int scsi_unit_is_ready(Scsi_Cmnd * SCpnt)
699 if (((driver_byte(SCpnt->result) & DRIVER_SENSE) ||
700 (status_byte(SCpnt->result) & CHECK_CONDITION)) &&
701 ((SCpnt->sense_buffer[0] & 0x70) >> 4) == 7) {
702 if (((SCpnt->sense_buffer[2] & 0xf) != NOT_READY) &&
703 ((SCpnt->sense_buffer[2] & 0xf) != UNIT_ATTENTION) &&
704 ((SCpnt->sense_buffer[2] & 0xf) != ILLEGAL_REQUEST)) {
713 * Function: scsi_eh_finish_command
715 * Purpose: Handle a command that we are finished with WRT error handling.
717 * Arguments: SClist - pointer to list into which we are putting completed commands.
718 * SCpnt - command that is completing
720 * Notes: We don't want to use the normal command completion while we are
721 * are still handling errors - it may cause other commands to be queued,
722 * and that would disturb what we are doing. Thus we really want to keep
723 * a list of pending commands for final completion, and once we
724 * are ready to leave error handling we handle completion for real.
726 STATIC void scsi_eh_finish_command(Scsi_Cmnd ** SClist, Scsi_Cmnd * SCpnt)
728 SCpnt->state = SCSI_STATE_BHQUEUE;
729 SCpnt->bh_next = *SClist;
731 * Set this back so that the upper level can correctly free up
734 SCpnt->use_sg = SCpnt->old_use_sg;
735 SCpnt->sc_data_direction = SCpnt->sc_old_data_direction;
736 SCpnt->underflow = SCpnt->old_underflow;
741 * Function: scsi_try_to_abort_command
743 * Purpose: Ask host adapter to abort a running command.
745 * Returns: FAILED Operation failed or not supported.
748 * Notes: This function will not return until the user's completion
749 * function has been called. There is no timeout on this
750 * operation. If the author of the low-level driver wishes
751 * this operation to be timed, they can provide this facility
752 * themselves. Helper functions in scsi_error.c can be supplied
753 * to make this easier to do.
755 * Notes: It may be possible to combine this with all of the reset
756 * handling to eliminate a lot of code duplication. I don't
757 * know what makes more sense at the moment - this is just a
760 STATIC int scsi_try_to_abort_command(Scsi_Cmnd * SCpnt, int timeout)
765 SCpnt->eh_state = FAILED; /* Until we come up with something better */
767 if (SCpnt->host->hostt->eh_abort_handler == NULL) {
771 * scsi_done was called just after the command timed out and before
772 * we had a chance to process it. (DB)
774 if (SCpnt->serial_number == 0)
777 SCpnt->owner = SCSI_OWNER_LOWLEVEL;
779 spin_lock_irqsave(&io_request_lock, flags);
780 rtn = SCpnt->host->hostt->eh_abort_handler(SCpnt);
781 spin_unlock_irqrestore(&io_request_lock, flags);
786 * Function: scsi_try_bus_device_reset
788 * Purpose: Ask host adapter to perform a bus device reset for a given
791 * Returns: FAILED Operation failed or not supported.
794 * Notes: There is no timeout for this operation. If this operation is
795 * unreliable for a given host, then the host itself needs to put a
796 * timer on it, and set the host back to a consistent state prior
799 STATIC int scsi_try_bus_device_reset(Scsi_Cmnd * SCpnt, int timeout)
804 SCpnt->eh_state = FAILED; /* Until we come up with something better */
806 if (SCpnt->host->hostt->eh_device_reset_handler == NULL) {
809 SCpnt->owner = SCSI_OWNER_LOWLEVEL;
811 spin_lock_irqsave(&io_request_lock, flags);
812 rtn = SCpnt->host->hostt->eh_device_reset_handler(SCpnt);
813 spin_unlock_irqrestore(&io_request_lock, flags);
816 SCpnt->eh_state = SUCCESS;
818 return SCpnt->eh_state;
822 * Function: scsi_try_bus_reset
824 * Purpose: Ask host adapter to perform a bus reset for a host.
826 * Returns: FAILED Operation failed or not supported.
831 STATIC int scsi_try_bus_reset(Scsi_Cmnd * SCpnt)
836 SCpnt->eh_state = FAILED; /* Until we come up with something better */
837 SCpnt->owner = SCSI_OWNER_LOWLEVEL;
838 SCpnt->serial_number_at_timeout = SCpnt->serial_number;
840 if (SCpnt->host->hostt->eh_bus_reset_handler == NULL) {
844 spin_lock_irqsave(&io_request_lock, flags);
845 rtn = SCpnt->host->hostt->eh_bus_reset_handler(SCpnt);
846 spin_unlock_irqrestore(&io_request_lock, flags);
849 SCpnt->eh_state = SUCCESS;
852 * If we had a successful bus reset, mark the command blocks to expect
853 * a condition code of unit attention.
855 scsi_sleep(BUS_RESET_SETTLE_TIME);
856 if (SCpnt->eh_state == SUCCESS) {
858 for (SDloop = SCpnt->host->host_queue; SDloop; SDloop = SDloop->next) {
859 if (SCpnt->channel == SDloop->channel) {
860 SDloop->was_reset = 1;
861 SDloop->expecting_cc_ua = 1;
865 return SCpnt->eh_state;
869 * Function: scsi_try_host_reset
871 * Purpose: Ask host adapter to reset itself, and the bus.
873 * Returns: FAILED Operation failed or not supported.
878 STATIC int scsi_try_host_reset(Scsi_Cmnd * SCpnt)
883 SCpnt->eh_state = FAILED; /* Until we come up with something better */
884 SCpnt->owner = SCSI_OWNER_LOWLEVEL;
885 SCpnt->serial_number_at_timeout = SCpnt->serial_number;
887 if (SCpnt->host->hostt->eh_host_reset_handler == NULL) {
890 spin_lock_irqsave(&io_request_lock, flags);
891 rtn = SCpnt->host->hostt->eh_host_reset_handler(SCpnt);
892 spin_unlock_irqrestore(&io_request_lock, flags);
895 SCpnt->eh_state = SUCCESS;
898 * If we had a successful host reset, mark the command blocks to expect
899 * a condition code of unit attention.
901 scsi_sleep(HOST_RESET_SETTLE_TIME);
902 if (SCpnt->eh_state == SUCCESS) {
904 for (SDloop = SCpnt->host->host_queue; SDloop; SDloop = SDloop->next) {
905 SDloop->was_reset = 1;
906 SDloop->expecting_cc_ua = 1;
909 return SCpnt->eh_state;
913 * Function: scsi_decide_disposition
915 * Purpose: Examine a command block that has come back from the low-level
916 * and figure out what to do next.
918 * Returns: SUCCESS - pass on to upper level.
919 * FAILED - pass on to error handler thread.
920 * RETRY - command should be retried.
921 * SOFTERR - command succeeded, but we need to log
924 * Notes: This is *ONLY* called when we are examining the status
925 * after sending out the actual data command. Any commands
926 * that are queued for error recovery (i.e. TEST_UNIT_READY)
927 * do *NOT* come through here.
929 * NOTE - When this routine returns FAILED, it means the error
930 * handler thread is woken. In cases where the error code
931 * indicates an error that doesn't require the error handler
932 * thread (i.e. we don't need to abort/reset), then this function
933 * should return SUCCESS.
935 int scsi_decide_disposition(Scsi_Cmnd * SCpnt)
940 * If the device is offline, then we clearly just pass the result back
941 * up to the top level.
943 if (SCpnt->device->online == FALSE) {
944 SCSI_LOG_ERROR_RECOVERY(5, printk("scsi_error.c: device offline - report as SUCCESS\n"));
948 * First check the host byte, to see if there is anything in there
949 * that would indicate what we need to do.
952 switch (host_byte(SCpnt->result)) {
953 case DID_PASSTHROUGH:
955 * No matter what, pass this through to the upper layer.
956 * Nuke this special code so that it looks like we are saying
959 SCpnt->result &= 0xff00ffff;
963 * Looks good. Drop through, and check the next byte.
970 * Note - this means that we just report the status back to the
971 * top level driver, not that we actually think that it indicates
976 * When the low level driver returns DID_SOFT_ERROR,
977 * it is responsible for keeping an internal retry counter
978 * in order to avoid endless loops (DB)
980 * Actually this is a bug in this function here. We should
981 * be mindful of the maximum number of retries specified
982 * and not get stuck in a loop.
988 if (msg_byte(SCpnt->result) == COMMAND_COMPLETE &&
989 status_byte(SCpnt->result) == RESERVATION_CONFLICT)
991 * execute reservation conflict processing code
1002 * When we scan the bus, we get timeout messages for
1003 * these commands if there is no device available.
1004 * Other hosts report DID_NO_CONNECT for the same thing.
1006 if ((SCpnt->cmnd[0] == TEST_UNIT_READY ||
1007 SCpnt->cmnd[0] == INQUIRY)) {
1014 * In the normal case where we haven't initiated a reset, this is
1017 if (SCpnt->flags & IS_RESETTING) {
1018 SCpnt->flags &= ~IS_RESETTING;
1027 * Next, check the message byte.
1029 if (msg_byte(SCpnt->result) != COMMAND_COMPLETE) {
1033 * Now, check the status byte to see if this indicates anything special.
1035 switch (status_byte(SCpnt->result)) {
1038 * The case of trying to send too many commands to a tagged queueing
1041 return ADD_TO_MLQUEUE;
1043 case COMMAND_TERMINATED:
1045 case CHECK_CONDITION:
1046 rtn = scsi_check_sense(SCpnt);
1047 if (rtn == NEEDS_RETRY) {
1051 case CONDITION_GOOD:
1052 case INTERMEDIATE_GOOD:
1053 case INTERMEDIATE_C_GOOD:
1055 * Who knows? FIXME(eric)
1061 case RESERVATION_CONFLICT:
1062 printk("scsi%d (%d,%d,%d) : RESERVATION CONFLICT\n",
1063 SCpnt->host->host_no, SCpnt->channel,
1064 SCpnt->device->id, SCpnt->device->lun);
1065 return SUCCESS; /* causes immediate I/O error */
1073 if ((++SCpnt->retries) < SCpnt->allowed) {
1077 * No more retries - report this one back to upper level.
1084 * Function: scsi_eh_completed_normally
1086 * Purpose: Examine a command block that has come back from the low-level
1087 * and figure out what to do next.
1089 * Returns: SUCCESS - pass on to upper level.
1090 * FAILED - pass on to error handler thread.
1091 * RETRY - command should be retried.
1092 * SOFTERR - command succeeded, but we need to log
1095 * Notes: This is *ONLY* called when we are examining the status
1096 * of commands queued during error recovery. The main
1097 * difference here is that we don't allow for the possibility
1098 * of retries here, and we are a lot more restrictive about what
1099 * we consider acceptable.
1101 STATIC int scsi_eh_completed_normally(Scsi_Cmnd * SCpnt)
1104 * First check the host byte, to see if there is anything in there
1105 * that would indicate what we need to do.
1107 if (host_byte(SCpnt->result) == DID_RESET) {
1108 if (SCpnt->flags & IS_RESETTING) {
1110 * OK, this is normal. We don't know whether in fact the
1111 * command in question really needs to be rerun or not -
1112 * if this was the original data command then the answer is yes,
1113 * otherwise we just flag it as success.
1115 SCpnt->flags &= ~IS_RESETTING;
1119 * Rats. We are already in the error handler, so we now get to try
1120 * and figure out what to do next. If the sense is valid, we have
1121 * a pretty good idea of what to do. If not, we mark it as failed.
1123 return scsi_check_sense(SCpnt);
1125 if (host_byte(SCpnt->result) != DID_OK) {
1129 * Next, check the message byte.
1131 if (msg_byte(SCpnt->result) != COMMAND_COMPLETE) {
1135 * Now, check the status byte to see if this indicates anything special.
1137 switch (status_byte(SCpnt->result)) {
1139 case COMMAND_TERMINATED:
1141 case CHECK_CONDITION:
1142 return scsi_check_sense(SCpnt);
1143 case CONDITION_GOOD:
1144 case INTERMEDIATE_GOOD:
1145 case INTERMEDIATE_C_GOOD:
1147 * Who knows? FIXME(eric)
1152 case RESERVATION_CONFLICT:
1160 * Function: scsi_check_sense
1162 * Purpose: Examine sense information - give suggestion as to what
1163 * we should do with it.
1165 STATIC int scsi_check_sense(Scsi_Cmnd * SCpnt)
1167 if (!scsi_sense_valid(SCpnt)) {
1170 if (SCpnt->sense_buffer[2] & 0xe0)
1173 switch (SCpnt->sense_buffer[2] & 0xf) {
1176 case RECOVERED_ERROR:
1177 return /* SOFT_ERROR */ SUCCESS;
1179 case ABORTED_COMMAND:
1182 case UNIT_ATTENTION:
1184 * If we are expecting a CC/UA because of a bus reset that we
1185 * performed, treat this just as a retry. Otherwise this is
1186 * information that we should pass up to the upper-level driver
1187 * so that we can deal with it there.
1189 if (SCpnt->device->expecting_cc_ua) {
1190 SCpnt->device->expecting_cc_ua = 0;
1194 * If the device is in the process of becoming ready, we
1197 if ((SCpnt->sense_buffer[12] == 0x04) &&
1198 (SCpnt->sense_buffer[13] == 0x01)) {
1203 /* these three are not supported */
1205 case VOLUME_OVERFLOW:
1212 case ILLEGAL_REQUEST:
1215 case HARDWARE_ERROR:
1223 * Function: scsi_restart_operations
1225 * Purpose: Restart IO operations to the specified host.
1227 * Arguments: host - host that we are restarting
1229 * Lock status: Assumed that locks are not held upon entry.
1233 * Notes: When we entered the error handler, we blocked all further
1234 * I/O to this device. We need to 'reverse' this process.
1236 STATIC void scsi_restart_operations(struct Scsi_Host *host)
1239 unsigned long flags;
1241 ASSERT_LOCK(&io_request_lock, 0);
1244 * Next free up anything directly waiting upon the host. This will be
1245 * requests for character device operations, and also for ioctls to queued
1248 SCSI_LOG_ERROR_RECOVERY(5, printk("scsi_error.c: Waking up host to restart\n"));
1250 wake_up(&host->host_wait);
1253 * Finally we need to re-initiate requests that may be pending. We will
1254 * have had everything blocked while error handling is taking place, and
1255 * now that error recovery is done, we will need to ensure that these
1256 * requests are started.
1258 spin_lock_irqsave(&io_request_lock, flags);
1259 for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
1261 if ((host->can_queue > 0 && (host->host_busy >= host->can_queue))
1262 || (host->host_blocked)
1263 || (host->host_self_blocked)
1264 || (SDpnt->device_blocked)) {
1267 q = &SDpnt->request_queue;
1270 spin_unlock_irqrestore(&io_request_lock, flags);
1274 * Function: scsi_unjam_host
1276 * Purpose: Attempt to fix a host which has a command that failed for
1279 * Arguments: host - host that needs unjamming.
1283 * Notes: When we come in here, we *know* that all commands on the
1284 * bus have either completed, failed or timed out. We also
1285 * know that no further commands are being sent to the host,
1286 * so things are relatively quiet and we have freedom to
1287 * fiddle with things as we wish.
1289 * Additional note: This is only the *default* implementation. It is possible
1290 * for individual drivers to supply their own version of this
1291 * function, and if the maintainer wishes to do this, it is
1292 * strongly suggested that this function be taken as a template
1293 * and modified. This function was designed to correctly handle
1294 * problems for about 95% of the different cases out there, and
1295 * it should always provide at least a reasonable amount of error
1298 * Note3: Any command marked 'FAILED' or 'TIMEOUT' must eventually
1299 * have scsi_finish_command() called for it. We do all of
1300 * the retry stuff here, so when we restart the host after we
1301 * return it should have an empty queue.
1303 STATIC int scsi_unjam_host(struct Scsi_Host *host)
1313 Scsi_Device *SDloop;
1317 ASSERT_LOCK(&io_request_lock, 0);
1322 * First, protect against any sort of race condition. If any of the outstanding
1323 * commands are in states that indicate that we are not yet blocked (i.e. we are
1324 * not in a quiet state) then we got woken up in error. If we ever end up here,
1325 * we need to re-examine some of the assumptions.
1327 for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
1328 for (SCpnt = SDpnt->device_queue; SCpnt; SCpnt = SCpnt->next) {
1329 if (SCpnt->state == SCSI_STATE_FAILED
1330 || SCpnt->state == SCSI_STATE_TIMEOUT
1331 || SCpnt->state == SCSI_STATE_INITIALIZING
1332 || SCpnt->state == SCSI_STATE_UNUSED) {
1336 * Rats. Something is still floating around out there. This could
1337 * be the result of the fact that the upper level drivers are still frobbing
1338 * commands that might have succeeded. There are two outcomes. One is that
1339 * the command block will eventually be freed, and the other one is that
1340 * the command will be queued and will be finished along the way.
1342 SCSI_LOG_ERROR_RECOVERY(1, printk("Error handler prematurely woken - commands still active (%p %x %d)\n", SCpnt, SCpnt->state, SCpnt->target));
1345 * panic("SCSI Error handler woken too early\n");
1347 * This is no longer a problem, since now the code cares only about
1348 * SCSI_STATE_TIMEOUT and SCSI_STATE_FAILED.
1349 * Other states are useful only to release active commands when devices are
1350 * set offline. If (host->host_active == host->host_busy) we can safely assume
1351 * that there are no commands in state other then TIMEOUT od FAILED. (DB)
1354 * It is not easy to release correctly commands according to their state when
1355 * devices are set offline, when the state is neither TIMEOUT nor FAILED.
1356 * When a device is set offline, we can have some command with
1357 * rq_status=RQ_SCSY_BUSY, owner=SCSI_STATE_HIGHLEVEL,
1358 * state=SCSI_STATE_INITIALIZING and the driver module cannot be released.
1365 * Next, see if we need to request sense information. if so,
1366 * then get it now, so we have a better idea of what to do.
1367 * FIXME(eric) this has the unfortunate side effect that if a host
1368 * adapter does not automatically request sense information, that we end
1369 * up shutting it down before we request it. All hosts should be doing this
1370 * anyways, so for now all I have to say is tough noogies if you end up in here.
1371 * On second thought, this is probably a good idea. We *really* want to give
1372 * authors an incentive to automatically request this.
1374 SCSI_LOG_ERROR_RECOVERY(3, printk("scsi_unjam_host: Checking to see if we need to request sense\n"));
1376 for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
1377 for (SCpnt = SDpnt->device_queue; SCpnt; SCpnt = SCpnt->next) {
1378 if (SCpnt->state != SCSI_STATE_FAILED || scsi_sense_valid(SCpnt)) {
1381 SCSI_LOG_ERROR_RECOVERY(2, printk("scsi_unjam_host: Requesting sense for %d\n",
1383 rtn = scsi_request_sense(SCpnt);
1384 if (rtn != SUCCESS) {
1387 SCSI_LOG_ERROR_RECOVERY(3, printk("Sense requested for %p - result %x\n",
1388 SCpnt, SCpnt->result));
1389 SCSI_LOG_ERROR_RECOVERY(3, print_sense("bh", SCpnt));
1391 result = scsi_decide_disposition(SCpnt);
1394 * If the result was normal, then just pass it along to the
1397 if (result == SUCCESS) {
1398 SCpnt->host->host_failed--;
1399 scsi_eh_finish_command(&SCdone, SCpnt);
1401 if (result != NEEDS_RETRY) {
1405 * We only come in here if we want to retry a
1406 * command. The test to see whether the command
1407 * should be retried should be keeping track of the
1408 * number of tries, so we don't end up looping, of
1411 SCpnt->state = NEEDS_RETRY;
1412 rtn = scsi_eh_retry_command(SCpnt);
1413 if (rtn != SUCCESS) {
1417 * We eventually hand this one back to the top level.
1419 SCpnt->host->host_failed--;
1420 scsi_eh_finish_command(&SCdone, SCpnt);
1425 * Go through the list of commands and figure out where we stand and how bad things
1431 for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
1432 unsigned int device_error = 0;
1434 for (SCpnt = SDpnt->device_queue; SCpnt; SCpnt = SCpnt->next) {
1435 if (SCpnt->state == SCSI_STATE_FAILED) {
1436 SCSI_LOG_ERROR_RECOVERY(5, printk("Command to ID %d failed\n",
1441 if (SCpnt->state == SCSI_STATE_TIMEOUT) {
1442 SCSI_LOG_ERROR_RECOVERY(5, printk("Command to ID %d timedout\n",
1448 if (device_error > 0) {
1453 SCSI_LOG_ERROR_RECOVERY(2, printk("Total of %d+%d commands on %d devices require eh work\n",
1454 numfailed, timed_out, devices_failed));
1456 if (host->host_failed == 0) {
1461 * Next, try and see whether or not it makes sense to try and abort
1462 * the running command. This only works out to be the case if we have
1463 * one command that has timed out. If the command simply failed, it
1464 * makes no sense to try and abort the command, since as far as the
1465 * host adapter is concerned, it isn't running.
1468 SCSI_LOG_ERROR_RECOVERY(3, printk("scsi_unjam_host: Checking to see if we want to try abort\n"));
1470 for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
1471 for (SCloop = SDpnt->device_queue; SCloop; SCloop = SCloop->next) {
1472 if (SCloop->state != SCSI_STATE_TIMEOUT) {
1475 rtn = scsi_try_to_abort_command(SCloop, ABORT_TIMEOUT);
1476 if (rtn == SUCCESS) {
1477 rtn = scsi_test_unit_ready(SCloop);
1479 if (rtn == SUCCESS && scsi_unit_is_ready(SCloop)) {
1480 rtn = scsi_eh_retry_command(SCloop);
1482 if (rtn == SUCCESS) {
1483 SCloop->host->host_failed--;
1484 scsi_eh_finish_command(&SCdone, SCloop);
1492 * If we have corrected all of the problems, then we are done.
1494 if (host->host_failed == 0) {
1499 * Either the abort wasn't appropriate, or it didn't succeed.
1500 * Now try a bus device reset. Still, look to see whether we have
1501 * multiple devices that are jammed or not - if we have multiple devices,
1502 * it makes no sense to try BUS_DEVICE_RESET - we really would need
1503 * to try a BUS_RESET instead.
1505 * Does this make sense - should we try BDR on each device individually?
1508 SCSI_LOG_ERROR_RECOVERY(3, printk("scsi_unjam_host: Checking to see if we want to try BDR\n"));
1510 for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
1511 for (SCloop = SDpnt->device_queue; SCloop; SCloop = SCloop->next) {
1512 if (SCloop->state == SCSI_STATE_FAILED
1513 || SCloop->state == SCSI_STATE_TIMEOUT) {
1518 if (SCloop == NULL) {
1522 * OK, we have a device that is having problems. Try and send
1523 * a bus device reset to it.
1525 * FIXME(eric) - make sure we handle the case where multiple
1526 * commands to the same device have failed. They all must
1527 * get properly restarted.
1529 rtn = scsi_try_bus_device_reset(SCloop, RESET_TIMEOUT);
1531 if (rtn == SUCCESS) {
1532 rtn = scsi_test_unit_ready(SCloop);
1534 if (rtn == SUCCESS && scsi_unit_is_ready(SCloop)) {
1535 rtn = scsi_eh_retry_command(SCloop);
1537 if (rtn == SUCCESS) {
1538 SCloop->host->host_failed--;
1539 scsi_eh_finish_command(&SCdone, SCloop);
1545 if (host->host_failed == 0) {
1550 * If we ended up here, we have serious problems. The only thing left
1551 * to try is a full bus reset. If someone has grabbed the bus and isn't
1552 * letting go, then perhaps this will help.
1554 SCSI_LOG_ERROR_RECOVERY(3, printk("scsi_unjam_host: Try hard bus reset\n"));
1557 * We really want to loop over the various channels, and do this on
1558 * a channel by channel basis. We should also check to see if any
1559 * of the failed commands are on soft_reset devices, and if so, skip
1562 for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
1564 for (SCpnt = SDpnt->device_queue; SCpnt; SCpnt = SCpnt->next) {
1565 if (SCpnt->state != SCSI_STATE_FAILED
1566 && SCpnt->state != SCSI_STATE_TIMEOUT) {
1570 * We have a failed command. Make sure there are no other failed
1571 * commands on the same channel that are timed out and implement a
1574 for (SDloop = host->host_queue; SDloop; SDloop = SDloop->next) {
1575 for (SCloop = SDloop->device_queue; SCloop; SCloop = SCloop->next) {
1576 if (SCloop->channel != SCpnt->channel) {
1579 if (SCloop->state != SCSI_STATE_FAILED
1580 && SCloop->state != SCSI_STATE_TIMEOUT) {
1583 if (SDloop->soft_reset && SCloop->state == SCSI_STATE_TIMEOUT) {
1585 * If this device uses the soft reset option, and this
1586 * is one of the devices acting up, then our only
1587 * option is to wait a bit, since the command is
1588 * supposedly still running.
1590 * FIXME(eric) - right now we will just end up falling
1591 * through to the 'take device offline' case.
1593 * FIXME(eric) - It is possible that the command completed
1594 * *after* the error recovery procedure started, and if this
1595 * is the case, we are worrying about nothing here.
1605 * We now know that we are able to perform a reset for the
1606 * bus that SCpnt points to. There are no soft-reset devices
1607 * with outstanding timed out commands.
1609 rtn = scsi_try_bus_reset(SCpnt);
1610 if (rtn == SUCCESS) {
1611 for (SDloop = host->host_queue; SDloop; SDloop = SDloop->next) {
1612 for (SCloop = SDloop->device_queue; SCloop; SCloop = SCloop->next) {
1613 if (SCloop->channel != SCpnt->channel) {
1616 if (SCloop->state != SCSI_STATE_FAILED
1617 && SCloop->state != SCSI_STATE_TIMEOUT) {
1620 rtn = scsi_test_unit_ready(SCloop);
1622 if (rtn == SUCCESS && scsi_unit_is_ready(SCloop)) {
1623 rtn = scsi_eh_retry_command(SCloop);
1625 if (rtn == SUCCESS) {
1626 SCpnt->host->host_failed--;
1627 scsi_eh_finish_command(&SCdone, SCloop);
1631 * If the bus reset worked, but we are still unable to
1632 * talk to the device, take it offline.
1633 * FIXME(eric) - is this really the correct thing to do?
1635 if (rtn != SUCCESS) {
1636 printk(KERN_INFO "scsi: device set offline - not ready or command retry failed after bus reset: host %d channel %d id %d lun %d\n", SDloop->host->host_no, SDloop->channel, SDloop->id, SDloop->lun);
1638 SDloop->online = FALSE;
1639 SDloop->host->host_failed--;
1640 scsi_eh_finish_command(&SCdone, SCloop);
1648 if (host->host_failed == 0) {
1653 * If we ended up here, we have serious problems. The only thing left
1654 * to try is a full host reset - perhaps the firmware on the device
1655 * crashed, or something like that.
1657 * It is assumed that a succesful host reset will cause *all* information
1658 * about the command to be flushed from both the host adapter *and* the
1661 * FIXME(eric) - it isn't clear that devices that implement the soft reset
1662 * option can ever be cleared except via cycling the power. The problem is
1663 * that sending the host reset command will cause the host to forget
1664 * about the pending command, but the device won't forget. For now, we
1665 * skip the host reset option if any of the failed devices are configured
1666 * to use the soft reset option.
1668 for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
1670 for (SCpnt = SDpnt->device_queue; SCpnt; SCpnt = SCpnt->next) {
1671 if (SCpnt->state != SCSI_STATE_FAILED
1672 && SCpnt->state != SCSI_STATE_TIMEOUT) {
1675 if (SDpnt->soft_reset && SCpnt->state == SCSI_STATE_TIMEOUT) {
1677 * If this device uses the soft reset option, and this
1678 * is one of the devices acting up, then our only
1679 * option is to wait a bit, since the command is
1680 * supposedly still running.
1682 * FIXME(eric) - right now we will just end up falling
1683 * through to the 'take device offline' case.
1685 SCSI_LOG_ERROR_RECOVERY(3,
1686 printk("scsi_unjam_host: Unable to try hard host reset\n"));
1689 * Due to the spinlock, we will never get out of this
1690 * loop without a proper wait. (DB)
1696 SCSI_LOG_ERROR_RECOVERY(3, printk("scsi_unjam_host: Try hard host reset\n"));
1699 * FIXME(eric) - we need to obtain a valid SCpnt to perform this call.
1701 rtn = scsi_try_host_reset(SCpnt);
1702 if (rtn == SUCCESS) {
1704 * FIXME(eric) we assume that all commands are flushed from the
1705 * controller. We should get a DID_RESET for all of the commands
1706 * that were pending. We should ignore these so that we can
1707 * guarantee that we are in a consistent state.
1709 * I believe this to be the case right now, but this needs to be
1712 for (SDloop = host->host_queue; SDloop; SDloop = SDloop->next) {
1713 for (SCloop = SDloop->device_queue; SCloop; SCloop = SCloop->next) {
1714 if (SCloop->state != SCSI_STATE_FAILED
1715 && SCloop->state != SCSI_STATE_TIMEOUT) {
1718 rtn = scsi_test_unit_ready(SCloop);
1720 if (rtn == SUCCESS && scsi_unit_is_ready(SCloop)) {
1721 rtn = scsi_eh_retry_command(SCloop);
1723 if (rtn == SUCCESS) {
1724 SCpnt->host->host_failed--;
1725 scsi_eh_finish_command(&SCdone, SCloop);
1728 if (rtn != SUCCESS) {
1729 printk(KERN_INFO "scsi: device set offline - not ready or command retry failed after host reset: host %d channel %d id %d lun %d\n", SDloop->host->host_no, SDloop->channel, SDloop->id, SDloop->lun);
1730 SDloop->online = FALSE;
1731 SDloop->host->host_failed--;
1732 scsi_eh_finish_command(&SCdone, SCloop);
1741 * If we solved all of the problems, then let's rev up the engines again.
1743 if (host->host_failed == 0) {
1748 * If the HOST RESET failed, then for now we assume that the entire host
1749 * adapter is too hosed to be of any use. For our purposes, however, it is
1750 * easier to simply take the devices offline that correspond to commands
1753 SCSI_LOG_ERROR_RECOVERY(1, printk("scsi_unjam_host: Take device offline\n"));
1755 for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
1756 for (SCloop = SDpnt->device_queue; SCloop; SCloop = SCloop->next) {
1757 if (SCloop->state == SCSI_STATE_FAILED || SCloop->state == SCSI_STATE_TIMEOUT) {
1758 SDloop = SCloop->device;
1759 if (SDloop->online == TRUE) {
1760 printk(KERN_INFO "scsi: device set offline - command error recover failed: host %d channel %d id %d lun %d\n", SDloop->host->host_no, SDloop->channel, SDloop->id, SDloop->lun);
1761 SDloop->online = FALSE;
1765 * This should pass the failure up to the top level driver, and
1766 * it will have to try and do something intelligent with it.
1768 SCloop->host->host_failed--;
1770 if (SCloop->state == SCSI_STATE_TIMEOUT) {
1771 SCloop->result |= (DRIVER_TIMEOUT << 24);
1773 SCSI_LOG_ERROR_RECOVERY(3, printk("Finishing command for device %d %x\n",
1774 SDloop->id, SCloop->result));
1776 scsi_eh_finish_command(&SCdone, SCloop);
1781 if (host->host_failed != 0) {
1782 panic("scsi_unjam_host: Miscount of number of failed commands.\n");
1784 SCSI_LOG_ERROR_RECOVERY(3, printk("scsi_unjam_host: Returning\n"));
1791 * We should have a list of commands that we 'finished' during the course of
1792 * error recovery. This should be the same as the list of commands that timed out
1793 * or failed. We are currently holding these things in a linked list - we didn't
1794 * put them in the bottom half queue because we wanted to keep things quiet while
1795 * we were working on recovery, and passing them up to the top level could easily
1796 * cause the top level to try and queue something else again.
1798 * Start by marking that the host is no longer in error recovery.
1800 host->in_recovery = 0;
1803 * Take the list of commands, and stick them in the bottom half queue.
1804 * The current implementation of scsi_done will do this for us - if need
1805 * be we can create a special version of this function to do the
1808 for (SCpnt = SCdone; SCpnt != NULL; SCpnt = SCdone) {
1809 SCdone = SCpnt->bh_next;
1810 SCpnt->bh_next = NULL;
1812 * Oh, this is a vile hack. scsi_done() expects a timer
1813 * to be running on the command. If there isn't, it assumes
1814 * that the command has actually timed out, and a timer
1815 * handler is running. That may well be how we got into
1816 * this fix, but right now things are stable. We add
1817 * a timer back again so that we can report completion.
1818 * scsi_done() will immediately remove said timer from
1819 * the command, and then process it.
1821 scsi_add_timer(SCpnt, 100, scsi_eh_times_out);
1830 * Function: scsi_error_handler
1832 * Purpose: Handle errors/timeouts of scsi commands, try and clean up
1833 * and unjam the bus, and restart things.
1835 * Arguments: host - host for which we are running.
1837 * Returns: Never returns.
1839 * Notes: This is always run in the context of a kernel thread. The
1840 * idea is that we start this thing up when the kernel starts
1841 * up (one per host that we detect), and it immediately goes to
1842 * sleep and waits for some event (i.e. failure). When this
1843 * takes place, we have the job of trying to unjam the bus
1844 * and restarting things.
1847 void scsi_error_handler(void *data)
1849 struct Scsi_Host *host = (struct Scsi_Host *) data;
1851 DECLARE_MUTEX_LOCKED(sem);
1854 * We only listen to signals if the HA was loaded as a module.
1855 * If the HA was compiled into the kernel, then we don't listen
1858 if( host->loaded_as_module ) {
1859 siginitsetinv(¤t->blocked, SHUTDOWN_SIGS);
1861 siginitsetinv(¤t->blocked, 0);
1874 * Set the name of this process.
1877 sprintf(current->comm, "scsi_eh_%d", host->host_no);
1879 host->eh_wait = &sem;
1880 host->ehandler = current;
1885 * Wake up the thread that created us.
1887 SCSI_LOG_ERROR_RECOVERY(3, printk("Wake up parent %d\n", sem_getcount(host->eh_notify)));
1889 up(host->eh_notify);
1893 * If we get a signal, it means we are supposed to go
1894 * away and die. This typically happens if the user is
1895 * trying to unload a module.
1897 SCSI_LOG_ERROR_RECOVERY(1, printk("Error handler sleeping\n"));
1900 * Note - we always use down_interruptible with the semaphore
1901 * even if the module was loaded as part of the kernel. The
1902 * reason is that down() will cause this thread to be counted
1903 * in the load average as a running process, and down
1904 * interruptible doesn't. Given that we need to allow this
1905 * thread to die if the driver was loaded as a module, using
1906 * semaphores isn't unreasonable.
1908 down_interruptible(&sem);
1909 if( host->loaded_as_module ) {
1910 if (signal_pending(current))
1914 SCSI_LOG_ERROR_RECOVERY(1, printk("Error handler waking up\n"));
1916 host->eh_active = 1;
1919 * We have a host that is failing for some reason. Figure out
1920 * what we need to do to get it up and online again (if we can).
1921 * If we fail, we end up taking the thing offline.
1923 if (host->hostt->eh_strategy_handler != NULL) {
1924 rtn = host->hostt->eh_strategy_handler(host);
1926 rtn = scsi_unjam_host(host);
1929 host->eh_active = 0;
1932 * Note - if the above fails completely, the action is to take
1933 * individual devices offline and flush the queue of any
1934 * outstanding requests that may have been pending. When we
1935 * restart, we restart any I/O to any other devices on the bus
1936 * which are still online.
1938 scsi_restart_operations(host);
1942 SCSI_LOG_ERROR_RECOVERY(1, printk("Error handler exiting\n"));
1945 * Make sure that nobody tries to wake us up again.
1947 host->eh_wait = NULL;
1950 * Knock this down too. From this point on, the host is flying
1951 * without a pilot. If this is because the module is being unloaded,
1952 * that's fine. If the user sent a signal to this thing, we are
1953 * potentially in real danger.
1955 host->in_recovery = 0;
1956 host->eh_active = 0;
1957 host->ehandler = NULL;
1960 * If anyone is waiting for us to exit (i.e. someone trying to unload
1961 * a driver), then wake up that process to let them know we are on
1962 * the way out the door. This may be overkill - I *think* that we
1963 * could probably just unload the driver and send the signal, and when
1964 * the error handling thread wakes up that it would just exit without
1965 * needing to touch any memory associated with the driver itself.
1967 if (host->eh_notify != NULL)
1968 up(host->eh_notify);
1972 * Function: scsi_new_reset
1974 * Purpose: Send requested reset to a bus or device at any phase.
1976 * Arguments: SCpnt - command ptr to send reset with (usually a dummy)
1977 * flag - reset type (see scsi.h)
1979 * Returns: SUCCESS/FAILURE.
1981 * Notes: This is used by the SCSI Generic driver to provide
1982 * Bus/Device reset capability.
1985 scsi_new_reset(Scsi_Cmnd *SCpnt, int flag)
1990 case SCSI_TRY_RESET_DEVICE:
1991 rtn = scsi_try_bus_device_reset(SCpnt, 0);
1995 case SCSI_TRY_RESET_BUS:
1996 rtn = scsi_try_bus_reset(SCpnt);
2000 case SCSI_TRY_RESET_HOST:
2001 rtn = scsi_try_host_reset(SCpnt);
2011 * Overrides for Emacs so that we follow Linus's tabbing style.
2012 * Emacs will notice this stuff at the end of the file and automatically
2013 * adjust the settings for this buffer only. This must remain at the end
2015 * ---------------------------------------------------------------------------
2018 * c-brace-imaginary-offset: 0
2019 * c-brace-offset: -4
2020 * c-argdecl-indent: 4
2021 * c-label-offset: -4
2022 * c-continued-statement-offset: 4
2023 * c-continued-brace-offset: 0
2024 * indent-tabs-mode: nil