[PATCH] synclink.c: add clear stats
[linux-2.6/mini2440.git] / drivers / scsi / scsi_error.c
blob895c9452be4ca65cc3aebf78ee8bd9f319618c15
1 /*
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.
9 * Restructured scsi_unjam_host and associated functions.
10 * September 04, 2002 Mike Anderson (andmike@us.ibm.com)
12 * Forward port of Russell King's (rmk@arm.linux.org.uk) changes and
13 * minor cleanups.
14 * September 30, 2002 Mike Anderson (andmike@us.ibm.com)
17 #include <linux/module.h>
18 #include <linux/sched.h>
19 #include <linux/timer.h>
20 #include <linux/string.h>
21 #include <linux/slab.h>
22 #include <linux/kernel.h>
23 #include <linux/kthread.h>
24 #include <linux/interrupt.h>
25 #include <linux/blkdev.h>
26 #include <linux/delay.h>
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_dbg.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_eh.h>
32 #include <scsi/scsi_host.h>
33 #include <scsi/scsi_ioctl.h>
34 #include <scsi/scsi_request.h>
36 #include "scsi_priv.h"
37 #include "scsi_logging.h"
39 #define SENSE_TIMEOUT (10*HZ)
40 #define START_UNIT_TIMEOUT (30*HZ)
43 * These should *probably* be handled by the host itself.
44 * Since it is allowed to sleep, it probably should.
46 #define BUS_RESET_SETTLE_TIME (10)
47 #define HOST_RESET_SETTLE_TIME (10)
49 /* called with shost->host_lock held */
50 void scsi_eh_wakeup(struct Scsi_Host *shost)
52 if (shost->host_busy == shost->host_failed) {
53 up(shost->eh_wait);
54 SCSI_LOG_ERROR_RECOVERY(5,
55 printk("Waking error handler thread\n"));
59 /**
60 * scsi_eh_scmd_add - add scsi cmd to error handling.
61 * @scmd: scmd to run eh on.
62 * @eh_flag: optional SCSI_EH flag.
64 * Return value:
65 * 0 on failure.
66 **/
67 int scsi_eh_scmd_add(struct scsi_cmnd *scmd, int eh_flag)
69 struct Scsi_Host *shost = scmd->device->host;
70 unsigned long flags;
72 if (shost->eh_wait == NULL)
73 return 0;
75 spin_lock_irqsave(shost->host_lock, flags);
77 scmd->eh_eflags |= eh_flag;
78 list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
79 scsi_host_set_state(shost, SHOST_RECOVERY);
80 shost->host_failed++;
81 scsi_eh_wakeup(shost);
82 spin_unlock_irqrestore(shost->host_lock, flags);
83 return 1;
86 /**
87 * scsi_add_timer - Start timeout timer for a single scsi command.
88 * @scmd: scsi command that is about to start running.
89 * @timeout: amount of time to allow this command to run.
90 * @complete: timeout function to call if timer isn't canceled.
92 * Notes:
93 * This should be turned into an inline function. Each scsi command
94 * has its own timer, and as it is added to the queue, we set up the
95 * timer. When the command completes, we cancel the timer.
96 **/
97 void scsi_add_timer(struct scsi_cmnd *scmd, int timeout,
98 void (*complete)(struct scsi_cmnd *))
102 * If the clock was already running for this command, then
103 * first delete the timer. The timer handling code gets rather
104 * confused if we don't do this.
106 if (scmd->eh_timeout.function)
107 del_timer(&scmd->eh_timeout);
109 scmd->eh_timeout.data = (unsigned long)scmd;
110 scmd->eh_timeout.expires = jiffies + timeout;
111 scmd->eh_timeout.function = (void (*)(unsigned long)) complete;
113 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: scmd: %p, time:"
114 " %d, (%p)\n", __FUNCTION__,
115 scmd, timeout, complete));
117 add_timer(&scmd->eh_timeout);
121 * scsi_delete_timer - Delete/cancel timer for a given function.
122 * @scmd: Cmd that we are canceling timer for
124 * Notes:
125 * This should be turned into an inline function.
127 * Return value:
128 * 1 if we were able to detach the timer. 0 if we blew it, and the
129 * timer function has already started to run.
131 int scsi_delete_timer(struct scsi_cmnd *scmd)
133 int rtn;
135 rtn = del_timer(&scmd->eh_timeout);
137 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: scmd: %p,"
138 " rtn: %d\n", __FUNCTION__,
139 scmd, rtn));
141 scmd->eh_timeout.data = (unsigned long)NULL;
142 scmd->eh_timeout.function = NULL;
144 return rtn;
148 * scsi_times_out - Timeout function for normal scsi commands.
149 * @scmd: Cmd that is timing out.
151 * Notes:
152 * We do not need to lock this. There is the potential for a race
153 * only in that the normal completion handling might run, but if the
154 * normal completion function determines that the timer has already
155 * fired, then it mustn't do anything.
157 void scsi_times_out(struct scsi_cmnd *scmd)
159 scsi_log_completion(scmd, TIMEOUT_ERROR);
161 if (scmd->device->host->hostt->eh_timed_out)
162 switch (scmd->device->host->hostt->eh_timed_out(scmd)) {
163 case EH_HANDLED:
164 __scsi_done(scmd);
165 return;
166 case EH_RESET_TIMER:
167 /* This allows a single retry even of a command
168 * with allowed == 0 */
169 if (scmd->retries++ > scmd->allowed)
170 break;
171 scsi_add_timer(scmd, scmd->timeout_per_command,
172 scsi_times_out);
173 return;
174 case EH_NOT_HANDLED:
175 break;
178 if (unlikely(!scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD))) {
179 panic("Error handler thread not present at %p %p %s %d",
180 scmd, scmd->device->host, __FILE__, __LINE__);
185 * scsi_block_when_processing_errors - Prevent cmds from being queued.
186 * @sdev: Device on which we are performing recovery.
188 * Description:
189 * We block until the host is out of error recovery, and then check to
190 * see whether the host or the device is offline.
192 * Return value:
193 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
195 int scsi_block_when_processing_errors(struct scsi_device *sdev)
197 int online;
199 wait_event(sdev->host->host_wait, (sdev->host->shost_state !=
200 SHOST_RECOVERY));
202 online = scsi_device_online(sdev);
204 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: rtn: %d\n", __FUNCTION__,
205 online));
207 return online;
209 EXPORT_SYMBOL(scsi_block_when_processing_errors);
211 #ifdef CONFIG_SCSI_LOGGING
213 * scsi_eh_prt_fail_stats - Log info on failures.
214 * @shost: scsi host being recovered.
215 * @work_q: Queue of scsi cmds to process.
217 static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
218 struct list_head *work_q)
220 struct scsi_cmnd *scmd;
221 struct scsi_device *sdev;
222 int total_failures = 0;
223 int cmd_failed = 0;
224 int cmd_cancel = 0;
225 int devices_failed = 0;
227 shost_for_each_device(sdev, shost) {
228 list_for_each_entry(scmd, work_q, eh_entry) {
229 if (scmd->device == sdev) {
230 ++total_failures;
231 if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD)
232 ++cmd_cancel;
233 else
234 ++cmd_failed;
238 if (cmd_cancel || cmd_failed) {
239 SCSI_LOG_ERROR_RECOVERY(3,
240 printk("%s: %d:%d:%d:%d cmds failed: %d,"
241 " cancel: %d\n",
242 __FUNCTION__, shost->host_no,
243 sdev->channel, sdev->id, sdev->lun,
244 cmd_failed, cmd_cancel));
245 cmd_cancel = 0;
246 cmd_failed = 0;
247 ++devices_failed;
251 SCSI_LOG_ERROR_RECOVERY(2, printk("Total of %d commands on %d"
252 " devices require eh work\n",
253 total_failures, devices_failed));
255 #endif
258 * scsi_check_sense - Examine scsi cmd sense
259 * @scmd: Cmd to have sense checked.
261 * Return value:
262 * SUCCESS or FAILED or NEEDS_RETRY
264 * Notes:
265 * When a deferred error is detected the current command has
266 * not been executed and needs retrying.
268 static int scsi_check_sense(struct scsi_cmnd *scmd)
270 struct scsi_sense_hdr sshdr;
272 if (! scsi_command_normalize_sense(scmd, &sshdr))
273 return FAILED; /* no valid sense data */
275 if (scsi_sense_is_deferred(&sshdr))
276 return NEEDS_RETRY;
279 * Previous logic looked for FILEMARK, EOM or ILI which are
280 * mainly associated with tapes and returned SUCCESS.
282 if (sshdr.response_code == 0x70) {
283 /* fixed format */
284 if (scmd->sense_buffer[2] & 0xe0)
285 return SUCCESS;
286 } else {
288 * descriptor format: look for "stream commands sense data
289 * descriptor" (see SSC-3). Assume single sense data
290 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
292 if ((sshdr.additional_length > 3) &&
293 (scmd->sense_buffer[8] == 0x4) &&
294 (scmd->sense_buffer[11] & 0xe0))
295 return SUCCESS;
298 switch (sshdr.sense_key) {
299 case NO_SENSE:
300 return SUCCESS;
301 case RECOVERED_ERROR:
302 return /* soft_error */ SUCCESS;
304 case ABORTED_COMMAND:
305 return NEEDS_RETRY;
306 case NOT_READY:
307 case UNIT_ATTENTION:
309 * if we are expecting a cc/ua because of a bus reset that we
310 * performed, treat this just as a retry. otherwise this is
311 * information that we should pass up to the upper-level driver
312 * so that we can deal with it there.
314 if (scmd->device->expecting_cc_ua) {
315 scmd->device->expecting_cc_ua = 0;
316 return NEEDS_RETRY;
319 * if the device is in the process of becoming ready, we
320 * should retry.
322 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
323 return NEEDS_RETRY;
325 * if the device is not started, we need to wake
326 * the error handler to start the motor
328 if (scmd->device->allow_restart &&
329 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
330 return FAILED;
331 return SUCCESS;
333 /* these three are not supported */
334 case COPY_ABORTED:
335 case VOLUME_OVERFLOW:
336 case MISCOMPARE:
337 return SUCCESS;
339 case MEDIUM_ERROR:
340 return NEEDS_RETRY;
342 case HARDWARE_ERROR:
343 if (scmd->device->retry_hwerror)
344 return NEEDS_RETRY;
345 else
346 return SUCCESS;
348 case ILLEGAL_REQUEST:
349 case BLANK_CHECK:
350 case DATA_PROTECT:
351 default:
352 return SUCCESS;
357 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
358 * @scmd: SCSI cmd to examine.
360 * Notes:
361 * This is *only* called when we are examining the status of commands
362 * queued during error recovery. the main difference here is that we
363 * don't allow for the possibility of retries here, and we are a lot
364 * more restrictive about what we consider acceptable.
366 static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
369 * first check the host byte, to see if there is anything in there
370 * that would indicate what we need to do.
372 if (host_byte(scmd->result) == DID_RESET) {
374 * rats. we are already in the error handler, so we now
375 * get to try and figure out what to do next. if the sense
376 * is valid, we have a pretty good idea of what to do.
377 * if not, we mark it as FAILED.
379 return scsi_check_sense(scmd);
381 if (host_byte(scmd->result) != DID_OK)
382 return FAILED;
385 * next, check the message byte.
387 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
388 return FAILED;
391 * now, check the status byte to see if this indicates
392 * anything special.
394 switch (status_byte(scmd->result)) {
395 case GOOD:
396 case COMMAND_TERMINATED:
397 return SUCCESS;
398 case CHECK_CONDITION:
399 return scsi_check_sense(scmd);
400 case CONDITION_GOOD:
401 case INTERMEDIATE_GOOD:
402 case INTERMEDIATE_C_GOOD:
404 * who knows? FIXME(eric)
406 return SUCCESS;
407 case BUSY:
408 case QUEUE_FULL:
409 case RESERVATION_CONFLICT:
410 default:
411 return FAILED;
413 return FAILED;
417 * scsi_eh_times_out - timeout function for error handling.
418 * @scmd: Cmd that is timing out.
420 * Notes:
421 * During error handling, the kernel thread will be sleeping waiting
422 * for some action to complete on the device. our only job is to
423 * record that it timed out, and to wake up the thread.
425 static void scsi_eh_times_out(struct scsi_cmnd *scmd)
427 scmd->eh_eflags |= SCSI_EH_REC_TIMEOUT;
428 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd:%p\n", __FUNCTION__,
429 scmd));
431 up(scmd->device->host->eh_action);
435 * scsi_eh_done - Completion function for error handling.
436 * @scmd: Cmd that is done.
438 static void scsi_eh_done(struct scsi_cmnd *scmd)
441 * if the timeout handler is already running, then just set the
442 * flag which says we finished late, and return. we have no
443 * way of stopping the timeout handler from running, so we must
444 * always defer to it.
446 if (del_timer(&scmd->eh_timeout)) {
447 scmd->request->rq_status = RQ_SCSI_DONE;
449 SCSI_LOG_ERROR_RECOVERY(3, printk("%s scmd: %p result: %x\n",
450 __FUNCTION__, scmd, scmd->result));
452 up(scmd->device->host->eh_action);
457 * scsi_send_eh_cmnd - send a cmd to a device as part of error recovery.
458 * @scmd: SCSI Cmd to send.
459 * @timeout: Timeout for cmd.
461 * Notes:
462 * The initialization of the structures is quite a bit different in
463 * this case, and furthermore, there is a different completion handler
464 * vs scsi_dispatch_cmd.
465 * Return value:
466 * SUCCESS or FAILED or NEEDS_RETRY
468 static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, int timeout)
470 struct scsi_device *sdev = scmd->device;
471 struct Scsi_Host *shost = sdev->host;
472 DECLARE_MUTEX_LOCKED(sem);
473 unsigned long flags;
474 int rtn = SUCCESS;
477 * we will use a queued command if possible, otherwise we will
478 * emulate the queuing and calling of completion function ourselves.
480 if (sdev->scsi_level <= SCSI_2)
481 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
482 (sdev->lun << 5 & 0xe0);
484 scsi_add_timer(scmd, timeout, scsi_eh_times_out);
487 * set up the semaphore so we wait for the command to complete.
489 shost->eh_action = &sem;
490 scmd->request->rq_status = RQ_SCSI_BUSY;
492 spin_lock_irqsave(shost->host_lock, flags);
493 scsi_log_send(scmd);
494 shost->hostt->queuecommand(scmd, scsi_eh_done);
495 spin_unlock_irqrestore(shost->host_lock, flags);
497 down(&sem);
498 scsi_log_completion(scmd, SUCCESS);
500 shost->eh_action = NULL;
503 * see if timeout. if so, tell the host to forget about it.
504 * in other words, we don't want a callback any more.
506 if (scmd->eh_eflags & SCSI_EH_REC_TIMEOUT) {
507 scmd->eh_eflags &= ~SCSI_EH_REC_TIMEOUT;
510 * as far as the low level driver is
511 * concerned, this command is still active, so
512 * we must give the low level driver a chance
513 * to abort it. (db)
515 * FIXME(eric) - we are not tracking whether we could
516 * abort a timed out command or not. not sure how
517 * we should treat them differently anyways.
519 if (shost->hostt->eh_abort_handler)
520 shost->hostt->eh_abort_handler(scmd);
522 scmd->request->rq_status = RQ_SCSI_DONE;
523 rtn = FAILED;
526 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd: %p, rtn:%x\n",
527 __FUNCTION__, scmd, rtn));
530 * now examine the actual status codes to see whether the command
531 * actually did complete normally.
533 if (rtn == SUCCESS) {
534 rtn = scsi_eh_completed_normally(scmd);
535 SCSI_LOG_ERROR_RECOVERY(3,
536 printk("%s: scsi_eh_completed_normally %x\n",
537 __FUNCTION__, rtn));
538 switch (rtn) {
539 case SUCCESS:
540 case NEEDS_RETRY:
541 case FAILED:
542 break;
543 default:
544 rtn = FAILED;
545 break;
549 return rtn;
553 * scsi_request_sense - Request sense data from a particular target.
554 * @scmd: SCSI cmd for request sense.
556 * Notes:
557 * Some hosts automatically obtain this information, others require
558 * that we obtain it on our own. This function will *not* return until
559 * the command either times out, or it completes.
561 static int scsi_request_sense(struct scsi_cmnd *scmd)
563 static unsigned char generic_sense[6] =
564 {REQUEST_SENSE, 0, 0, 0, 252, 0};
565 unsigned char *scsi_result;
566 int saved_result;
567 int rtn;
569 memcpy(scmd->cmnd, generic_sense, sizeof(generic_sense));
571 scsi_result = kmalloc(252, GFP_ATOMIC | ((scmd->device->host->hostt->unchecked_isa_dma) ? __GFP_DMA : 0));
574 if (unlikely(!scsi_result)) {
575 printk(KERN_ERR "%s: cannot allocate scsi_result.\n",
576 __FUNCTION__);
577 return FAILED;
581 * zero the sense buffer. some host adapters automatically always
582 * request sense, so it is not a good idea that
583 * scmd->request_buffer and scmd->sense_buffer point to the same
584 * address (db). 0 is not a valid sense code.
586 memset(scmd->sense_buffer, 0, sizeof(scmd->sense_buffer));
587 memset(scsi_result, 0, 252);
589 saved_result = scmd->result;
590 scmd->request_buffer = scsi_result;
591 scmd->request_bufflen = 252;
592 scmd->use_sg = 0;
593 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
594 scmd->sc_data_direction = DMA_FROM_DEVICE;
595 scmd->underflow = 0;
597 rtn = scsi_send_eh_cmnd(scmd, SENSE_TIMEOUT);
599 /* last chance to have valid sense data */
600 if(!SCSI_SENSE_VALID(scmd)) {
601 memcpy(scmd->sense_buffer, scmd->request_buffer,
602 sizeof(scmd->sense_buffer));
605 kfree(scsi_result);
608 * when we eventually call scsi_finish, we really wish to complete
609 * the original request, so let's restore the original data. (db)
611 scsi_setup_cmd_retry(scmd);
612 scmd->result = saved_result;
613 return rtn;
617 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
618 * @scmd: Original SCSI cmd that eh has finished.
619 * @done_q: Queue for processed commands.
621 * Notes:
622 * We don't want to use the normal command completion while we are are
623 * still handling errors - it may cause other commands to be queued,
624 * and that would disturb what we are doing. thus we really want to
625 * keep a list of pending commands for final completion, and once we
626 * are ready to leave error handling we handle completion for real.
628 static void scsi_eh_finish_cmd(struct scsi_cmnd *scmd,
629 struct list_head *done_q)
631 scmd->device->host->host_failed--;
632 scmd->eh_eflags = 0;
635 * set this back so that the upper level can correctly free up
636 * things.
638 scsi_setup_cmd_retry(scmd);
639 list_move_tail(&scmd->eh_entry, done_q);
643 * scsi_eh_get_sense - Get device sense data.
644 * @work_q: Queue of commands to process.
645 * @done_q: Queue of proccessed commands..
647 * Description:
648 * See if we need to request sense information. if so, then get it
649 * now, so we have a better idea of what to do.
651 * Notes:
652 * This has the unfortunate side effect that if a shost adapter does
653 * not automatically request sense information, that we end up shutting
654 * it down before we request it.
656 * All drivers should request sense information internally these days,
657 * so for now all I have to say is tough noogies if you end up in here.
659 * XXX: Long term this code should go away, but that needs an audit of
660 * all LLDDs first.
662 static int scsi_eh_get_sense(struct list_head *work_q,
663 struct list_head *done_q)
665 struct scsi_cmnd *scmd, *next;
666 int rtn;
668 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
669 if ((scmd->eh_eflags & SCSI_EH_CANCEL_CMD) ||
670 SCSI_SENSE_VALID(scmd))
671 continue;
673 SCSI_LOG_ERROR_RECOVERY(2, printk("%s: requesting sense"
674 " for id: %d\n",
675 current->comm,
676 scmd->device->id));
677 rtn = scsi_request_sense(scmd);
678 if (rtn != SUCCESS)
679 continue;
681 SCSI_LOG_ERROR_RECOVERY(3, printk("sense requested for %p"
682 " result %x\n", scmd,
683 scmd->result));
684 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense("bh", scmd));
686 rtn = scsi_decide_disposition(scmd);
689 * if the result was normal, then just pass it along to the
690 * upper level.
692 if (rtn == SUCCESS)
693 /* we don't want this command reissued, just
694 * finished with the sense data, so set
695 * retries to the max allowed to ensure it
696 * won't get reissued */
697 scmd->retries = scmd->allowed;
698 else if (rtn != NEEDS_RETRY)
699 continue;
701 scsi_eh_finish_cmd(scmd, done_q);
704 return list_empty(work_q);
708 * scsi_try_to_abort_cmd - Ask host to abort a running command.
709 * @scmd: SCSI cmd to abort from Lower Level.
711 * Notes:
712 * This function will not return until the user's completion function
713 * has been called. there is no timeout on this operation. if the
714 * author of the low-level driver wishes this operation to be timed,
715 * they can provide this facility themselves. helper functions in
716 * scsi_error.c can be supplied to make this easier to do.
718 static int scsi_try_to_abort_cmd(struct scsi_cmnd *scmd)
720 if (!scmd->device->host->hostt->eh_abort_handler)
721 return FAILED;
724 * scsi_done was called just after the command timed out and before
725 * we had a chance to process it. (db)
727 if (scmd->serial_number == 0)
728 return SUCCESS;
729 return scmd->device->host->hostt->eh_abort_handler(scmd);
733 * scsi_eh_tur - Send TUR to device.
734 * @scmd: Scsi cmd to send TUR
736 * Return value:
737 * 0 - Device is ready. 1 - Device NOT ready.
739 static int scsi_eh_tur(struct scsi_cmnd *scmd)
741 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
742 int retry_cnt = 1, rtn;
743 int saved_result;
745 retry_tur:
746 memcpy(scmd->cmnd, tur_command, sizeof(tur_command));
749 * zero the sense buffer. the scsi spec mandates that any
750 * untransferred sense data should be interpreted as being zero.
752 memset(scmd->sense_buffer, 0, sizeof(scmd->sense_buffer));
754 saved_result = scmd->result;
755 scmd->request_buffer = NULL;
756 scmd->request_bufflen = 0;
757 scmd->use_sg = 0;
758 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
759 scmd->underflow = 0;
760 scmd->sc_data_direction = DMA_NONE;
762 rtn = scsi_send_eh_cmnd(scmd, SENSE_TIMEOUT);
765 * when we eventually call scsi_finish, we really wish to complete
766 * the original request, so let's restore the original data. (db)
768 scsi_setup_cmd_retry(scmd);
769 scmd->result = saved_result;
772 * hey, we are done. let's look to see what happened.
774 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd %p rtn %x\n",
775 __FUNCTION__, scmd, rtn));
776 if (rtn == SUCCESS)
777 return 0;
778 else if (rtn == NEEDS_RETRY) {
779 if (retry_cnt--)
780 goto retry_tur;
781 return 0;
783 return 1;
787 * scsi_eh_abort_cmds - abort canceled commands.
788 * @shost: scsi host being recovered.
789 * @eh_done_q: list_head for processed commands.
791 * Decription:
792 * Try and see whether or not it makes sense to try and abort the
793 * running command. this only works out to be the case if we have one
794 * command that has timed out. if the command simply failed, it makes
795 * no sense to try and abort the command, since as far as the shost
796 * adapter is concerned, it isn't running.
798 static int scsi_eh_abort_cmds(struct list_head *work_q,
799 struct list_head *done_q)
801 struct scsi_cmnd *scmd, *next;
802 int rtn;
804 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
805 if (!(scmd->eh_eflags & SCSI_EH_CANCEL_CMD))
806 continue;
807 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting cmd:"
808 "0x%p\n", current->comm,
809 scmd));
810 rtn = scsi_try_to_abort_cmd(scmd);
811 if (rtn == SUCCESS) {
812 scmd->eh_eflags &= ~SCSI_EH_CANCEL_CMD;
813 if (!scsi_device_online(scmd->device) ||
814 !scsi_eh_tur(scmd)) {
815 scsi_eh_finish_cmd(scmd, done_q);
818 } else
819 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting"
820 " cmd failed:"
821 "0x%p\n",
822 current->comm,
823 scmd));
826 return list_empty(work_q);
830 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
831 * @scmd: SCSI cmd used to send BDR
833 * Notes:
834 * There is no timeout for this operation. if this operation is
835 * unreliable for a given host, then the host itself needs to put a
836 * timer on it, and set the host back to a consistent state prior to
837 * returning.
839 static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
841 int rtn;
843 if (!scmd->device->host->hostt->eh_device_reset_handler)
844 return FAILED;
846 rtn = scmd->device->host->hostt->eh_device_reset_handler(scmd);
847 if (rtn == SUCCESS) {
848 scmd->device->was_reset = 1;
849 scmd->device->expecting_cc_ua = 1;
852 return rtn;
856 * scsi_eh_try_stu - Send START_UNIT to device.
857 * @scmd: Scsi cmd to send START_UNIT
859 * Return value:
860 * 0 - Device is ready. 1 - Device NOT ready.
862 static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
864 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
865 int rtn;
866 int saved_result;
868 if (!scmd->device->allow_restart)
869 return 1;
871 memcpy(scmd->cmnd, stu_command, sizeof(stu_command));
874 * zero the sense buffer. the scsi spec mandates that any
875 * untransferred sense data should be interpreted as being zero.
877 memset(scmd->sense_buffer, 0, sizeof(scmd->sense_buffer));
879 saved_result = scmd->result;
880 scmd->request_buffer = NULL;
881 scmd->request_bufflen = 0;
882 scmd->use_sg = 0;
883 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
884 scmd->underflow = 0;
885 scmd->sc_data_direction = DMA_NONE;
887 rtn = scsi_send_eh_cmnd(scmd, START_UNIT_TIMEOUT);
890 * when we eventually call scsi_finish, we really wish to complete
891 * the original request, so let's restore the original data. (db)
893 scsi_setup_cmd_retry(scmd);
894 scmd->result = saved_result;
897 * hey, we are done. let's look to see what happened.
899 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd %p rtn %x\n",
900 __FUNCTION__, scmd, rtn));
901 if (rtn == SUCCESS)
902 return 0;
903 return 1;
907 * scsi_eh_stu - send START_UNIT if needed
908 * @shost: scsi host being recovered.
909 * @eh_done_q: list_head for processed commands.
911 * Notes:
912 * If commands are failing due to not ready, initializing command required,
913 * try revalidating the device, which will end up sending a start unit.
915 static int scsi_eh_stu(struct Scsi_Host *shost,
916 struct list_head *work_q,
917 struct list_head *done_q)
919 struct scsi_cmnd *scmd, *stu_scmd, *next;
920 struct scsi_device *sdev;
922 shost_for_each_device(sdev, shost) {
923 stu_scmd = NULL;
924 list_for_each_entry(scmd, work_q, eh_entry)
925 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
926 scsi_check_sense(scmd) == FAILED ) {
927 stu_scmd = scmd;
928 break;
931 if (!stu_scmd)
932 continue;
934 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending START_UNIT to sdev:"
935 " 0x%p\n", current->comm, sdev));
937 if (!scsi_eh_try_stu(stu_scmd)) {
938 if (!scsi_device_online(sdev) ||
939 !scsi_eh_tur(stu_scmd)) {
940 list_for_each_entry_safe(scmd, next,
941 work_q, eh_entry) {
942 if (scmd->device == sdev)
943 scsi_eh_finish_cmd(scmd, done_q);
946 } else {
947 SCSI_LOG_ERROR_RECOVERY(3,
948 printk("%s: START_UNIT failed to sdev:"
949 " 0x%p\n", current->comm, sdev));
953 return list_empty(work_q);
958 * scsi_eh_bus_device_reset - send bdr if needed
959 * @shost: scsi host being recovered.
960 * @eh_done_q: list_head for processed commands.
962 * Notes:
963 * Try a bus device reset. still, look to see whether we have multiple
964 * devices that are jammed or not - if we have multiple devices, it
965 * makes no sense to try bus_device_reset - we really would need to try
966 * a bus_reset instead.
968 static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
969 struct list_head *work_q,
970 struct list_head *done_q)
972 struct scsi_cmnd *scmd, *bdr_scmd, *next;
973 struct scsi_device *sdev;
974 int rtn;
976 shost_for_each_device(sdev, shost) {
977 bdr_scmd = NULL;
978 list_for_each_entry(scmd, work_q, eh_entry)
979 if (scmd->device == sdev) {
980 bdr_scmd = scmd;
981 break;
984 if (!bdr_scmd)
985 continue;
987 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BDR sdev:"
988 " 0x%p\n", current->comm,
989 sdev));
990 rtn = scsi_try_bus_device_reset(bdr_scmd);
991 if (rtn == SUCCESS) {
992 if (!scsi_device_online(sdev) ||
993 !scsi_eh_tur(bdr_scmd)) {
994 list_for_each_entry_safe(scmd, next,
995 work_q, eh_entry) {
996 if (scmd->device == sdev)
997 scsi_eh_finish_cmd(scmd,
998 done_q);
1001 } else {
1002 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BDR"
1003 " failed sdev:"
1004 "0x%p\n",
1005 current->comm,
1006 sdev));
1010 return list_empty(work_q);
1014 * scsi_try_bus_reset - ask host to perform a bus reset
1015 * @scmd: SCSI cmd to send bus reset.
1017 static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
1019 unsigned long flags;
1020 int rtn;
1022 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Bus RST\n",
1023 __FUNCTION__));
1025 if (!scmd->device->host->hostt->eh_bus_reset_handler)
1026 return FAILED;
1028 rtn = scmd->device->host->hostt->eh_bus_reset_handler(scmd);
1030 if (rtn == SUCCESS) {
1031 if (!scmd->device->host->hostt->skip_settle_delay)
1032 ssleep(BUS_RESET_SETTLE_TIME);
1033 spin_lock_irqsave(scmd->device->host->host_lock, flags);
1034 scsi_report_bus_reset(scmd->device->host, scmd->device->channel);
1035 spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
1038 return rtn;
1042 * scsi_try_host_reset - ask host adapter to reset itself
1043 * @scmd: SCSI cmd to send hsot reset.
1045 static int scsi_try_host_reset(struct scsi_cmnd *scmd)
1047 unsigned long flags;
1048 int rtn;
1050 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Host RST\n",
1051 __FUNCTION__));
1053 if (!scmd->device->host->hostt->eh_host_reset_handler)
1054 return FAILED;
1056 rtn = scmd->device->host->hostt->eh_host_reset_handler(scmd);
1058 if (rtn == SUCCESS) {
1059 if (!scmd->device->host->hostt->skip_settle_delay)
1060 ssleep(HOST_RESET_SETTLE_TIME);
1061 spin_lock_irqsave(scmd->device->host->host_lock, flags);
1062 scsi_report_bus_reset(scmd->device->host, scmd->device->channel);
1063 spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
1066 return rtn;
1070 * scsi_eh_bus_reset - send a bus reset
1071 * @shost: scsi host being recovered.
1072 * @eh_done_q: list_head for processed commands.
1074 static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1075 struct list_head *work_q,
1076 struct list_head *done_q)
1078 struct scsi_cmnd *scmd, *chan_scmd, *next;
1079 unsigned int channel;
1080 int rtn;
1083 * we really want to loop over the various channels, and do this on
1084 * a channel by channel basis. we should also check to see if any
1085 * of the failed commands are on soft_reset devices, and if so, skip
1086 * the reset.
1089 for (channel = 0; channel <= shost->max_channel; channel++) {
1090 chan_scmd = NULL;
1091 list_for_each_entry(scmd, work_q, eh_entry) {
1092 if (channel == scmd->device->channel) {
1093 chan_scmd = scmd;
1094 break;
1096 * FIXME add back in some support for
1097 * soft_reset devices.
1102 if (!chan_scmd)
1103 continue;
1104 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BRST chan:"
1105 " %d\n", current->comm,
1106 channel));
1107 rtn = scsi_try_bus_reset(chan_scmd);
1108 if (rtn == SUCCESS) {
1109 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1110 if (channel == scmd->device->channel)
1111 if (!scsi_device_online(scmd->device) ||
1112 !scsi_eh_tur(scmd))
1113 scsi_eh_finish_cmd(scmd,
1114 done_q);
1116 } else {
1117 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BRST"
1118 " failed chan: %d\n",
1119 current->comm,
1120 channel));
1123 return list_empty(work_q);
1127 * scsi_eh_host_reset - send a host reset
1128 * @work_q: list_head for processed commands.
1129 * @done_q: list_head for processed commands.
1131 static int scsi_eh_host_reset(struct list_head *work_q,
1132 struct list_head *done_q)
1134 struct scsi_cmnd *scmd, *next;
1135 int rtn;
1137 if (!list_empty(work_q)) {
1138 scmd = list_entry(work_q->next,
1139 struct scsi_cmnd, eh_entry);
1141 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending HRST\n"
1142 , current->comm));
1144 rtn = scsi_try_host_reset(scmd);
1145 if (rtn == SUCCESS) {
1146 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1147 if (!scsi_device_online(scmd->device) ||
1148 (!scsi_eh_try_stu(scmd) && !scsi_eh_tur(scmd)) ||
1149 !scsi_eh_tur(scmd))
1150 scsi_eh_finish_cmd(scmd, done_q);
1152 } else {
1153 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: HRST"
1154 " failed\n",
1155 current->comm));
1158 return list_empty(work_q);
1162 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
1163 * @work_q: list_head for processed commands.
1164 * @done_q: list_head for processed commands.
1167 static void scsi_eh_offline_sdevs(struct list_head *work_q,
1168 struct list_head *done_q)
1170 struct scsi_cmnd *scmd, *next;
1172 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1173 printk(KERN_INFO "scsi: Device offlined - not"
1174 " ready after error recovery: host"
1175 " %d channel %d id %d lun %d\n",
1176 scmd->device->host->host_no,
1177 scmd->device->channel,
1178 scmd->device->id,
1179 scmd->device->lun);
1180 scsi_device_set_state(scmd->device, SDEV_OFFLINE);
1181 if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD) {
1183 * FIXME: Handle lost cmds.
1186 scsi_eh_finish_cmd(scmd, done_q);
1188 return;
1192 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1193 * @scmd: SCSI cmd to examine.
1195 * Notes:
1196 * This is *only* called when we are examining the status after sending
1197 * out the actual data command. any commands that are queued for error
1198 * recovery (e.g. test_unit_ready) do *not* come through here.
1200 * When this routine returns failed, it means the error handler thread
1201 * is woken. In cases where the error code indicates an error that
1202 * doesn't require the error handler read (i.e. we don't need to
1203 * abort/reset), this function should return SUCCESS.
1205 int scsi_decide_disposition(struct scsi_cmnd *scmd)
1207 int rtn;
1210 * if the device is offline, then we clearly just pass the result back
1211 * up to the top level.
1213 if (!scsi_device_online(scmd->device)) {
1214 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: device offline - report"
1215 " as SUCCESS\n",
1216 __FUNCTION__));
1217 return SUCCESS;
1221 * first check the host byte, to see if there is anything in there
1222 * that would indicate what we need to do.
1224 switch (host_byte(scmd->result)) {
1225 case DID_PASSTHROUGH:
1227 * no matter what, pass this through to the upper layer.
1228 * nuke this special code so that it looks like we are saying
1229 * did_ok.
1231 scmd->result &= 0xff00ffff;
1232 return SUCCESS;
1233 case DID_OK:
1235 * looks good. drop through, and check the next byte.
1237 break;
1238 case DID_NO_CONNECT:
1239 case DID_BAD_TARGET:
1240 case DID_ABORT:
1242 * note - this means that we just report the status back
1243 * to the top level driver, not that we actually think
1244 * that it indicates SUCCESS.
1246 return SUCCESS;
1248 * when the low level driver returns did_soft_error,
1249 * it is responsible for keeping an internal retry counter
1250 * in order to avoid endless loops (db)
1252 * actually this is a bug in this function here. we should
1253 * be mindful of the maximum number of retries specified
1254 * and not get stuck in a loop.
1256 case DID_SOFT_ERROR:
1257 goto maybe_retry;
1258 case DID_IMM_RETRY:
1259 return NEEDS_RETRY;
1261 case DID_REQUEUE:
1262 return ADD_TO_MLQUEUE;
1264 case DID_ERROR:
1265 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1266 status_byte(scmd->result) == RESERVATION_CONFLICT)
1268 * execute reservation conflict processing code
1269 * lower down
1271 break;
1272 /* fallthrough */
1274 case DID_BUS_BUSY:
1275 case DID_PARITY:
1276 goto maybe_retry;
1277 case DID_TIME_OUT:
1279 * when we scan the bus, we get timeout messages for
1280 * these commands if there is no device available.
1281 * other hosts report did_no_connect for the same thing.
1283 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1284 scmd->cmnd[0] == INQUIRY)) {
1285 return SUCCESS;
1286 } else {
1287 return FAILED;
1289 case DID_RESET:
1290 return SUCCESS;
1291 default:
1292 return FAILED;
1296 * next, check the message byte.
1298 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
1299 return FAILED;
1302 * check the status byte to see if this indicates anything special.
1304 switch (status_byte(scmd->result)) {
1305 case QUEUE_FULL:
1307 * the case of trying to send too many commands to a
1308 * tagged queueing device.
1310 case BUSY:
1312 * device can't talk to us at the moment. Should only
1313 * occur (SAM-3) when the task queue is empty, so will cause
1314 * the empty queue handling to trigger a stall in the
1315 * device.
1317 return ADD_TO_MLQUEUE;
1318 case GOOD:
1319 case COMMAND_TERMINATED:
1320 case TASK_ABORTED:
1321 return SUCCESS;
1322 case CHECK_CONDITION:
1323 rtn = scsi_check_sense(scmd);
1324 if (rtn == NEEDS_RETRY)
1325 goto maybe_retry;
1326 /* if rtn == FAILED, we have no sense information;
1327 * returning FAILED will wake the error handler thread
1328 * to collect the sense and redo the decide
1329 * disposition */
1330 return rtn;
1331 case CONDITION_GOOD:
1332 case INTERMEDIATE_GOOD:
1333 case INTERMEDIATE_C_GOOD:
1334 case ACA_ACTIVE:
1336 * who knows? FIXME(eric)
1338 return SUCCESS;
1340 case RESERVATION_CONFLICT:
1341 printk(KERN_INFO "scsi: reservation conflict: host"
1342 " %d channel %d id %d lun %d\n",
1343 scmd->device->host->host_no, scmd->device->channel,
1344 scmd->device->id, scmd->device->lun);
1345 return SUCCESS; /* causes immediate i/o error */
1346 default:
1347 return FAILED;
1349 return FAILED;
1351 maybe_retry:
1353 /* we requeue for retry because the error was retryable, and
1354 * the request was not marked fast fail. Note that above,
1355 * even if the request is marked fast fail, we still requeue
1356 * for queue congestion conditions (QUEUE_FULL or BUSY) */
1357 if ((++scmd->retries) < scmd->allowed
1358 && !blk_noretry_request(scmd->request)) {
1359 return NEEDS_RETRY;
1360 } else {
1362 * no more retries - report this one back to upper level.
1364 return SUCCESS;
1369 * scsi_eh_lock_done - done function for eh door lock request
1370 * @scmd: SCSI command block for the door lock request
1372 * Notes:
1373 * We completed the asynchronous door lock request, and it has either
1374 * locked the door or failed. We must free the command structures
1375 * associated with this request.
1377 static void scsi_eh_lock_done(struct scsi_cmnd *scmd)
1379 struct scsi_request *sreq = scmd->sc_request;
1381 scsi_release_request(sreq);
1386 * scsi_eh_lock_door - Prevent medium removal for the specified device
1387 * @sdev: SCSI device to prevent medium removal
1389 * Locking:
1390 * We must be called from process context; scsi_allocate_request()
1391 * may sleep.
1393 * Notes:
1394 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1395 * head of the devices request queue, and continue.
1397 * Bugs:
1398 * scsi_allocate_request() may sleep waiting for existing requests to
1399 * be processed. However, since we haven't kicked off any request
1400 * processing for this host, this may deadlock.
1402 * If scsi_allocate_request() fails for what ever reason, we
1403 * completely forget to lock the door.
1405 static void scsi_eh_lock_door(struct scsi_device *sdev)
1407 struct scsi_request *sreq = scsi_allocate_request(sdev, GFP_KERNEL);
1409 if (unlikely(!sreq)) {
1410 printk(KERN_ERR "%s: request allocate failed,"
1411 "prevent media removal cmd not sent\n", __FUNCTION__);
1412 return;
1415 sreq->sr_cmnd[0] = ALLOW_MEDIUM_REMOVAL;
1416 sreq->sr_cmnd[1] = 0;
1417 sreq->sr_cmnd[2] = 0;
1418 sreq->sr_cmnd[3] = 0;
1419 sreq->sr_cmnd[4] = SCSI_REMOVAL_PREVENT;
1420 sreq->sr_cmnd[5] = 0;
1421 sreq->sr_data_direction = DMA_NONE;
1422 sreq->sr_bufflen = 0;
1423 sreq->sr_buffer = NULL;
1424 sreq->sr_allowed = 5;
1425 sreq->sr_done = scsi_eh_lock_done;
1426 sreq->sr_timeout_per_command = 10 * HZ;
1427 sreq->sr_cmd_len = COMMAND_SIZE(sreq->sr_cmnd[0]);
1429 scsi_insert_special_req(sreq, 1);
1434 * scsi_restart_operations - restart io operations to the specified host.
1435 * @shost: Host we are restarting.
1437 * Notes:
1438 * When we entered the error handler, we blocked all further i/o to
1439 * this device. we need to 'reverse' this process.
1441 static void scsi_restart_operations(struct Scsi_Host *shost)
1443 struct scsi_device *sdev;
1446 * If the door was locked, we need to insert a door lock request
1447 * onto the head of the SCSI request queue for the device. There
1448 * is no point trying to lock the door of an off-line device.
1450 shost_for_each_device(sdev, shost) {
1451 if (scsi_device_online(sdev) && sdev->locked)
1452 scsi_eh_lock_door(sdev);
1456 * next free up anything directly waiting upon the host. this
1457 * will be requests for character device operations, and also for
1458 * ioctls to queued block devices.
1460 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: waking up host to restart\n",
1461 __FUNCTION__));
1463 scsi_host_set_state(shost, SHOST_RUNNING);
1465 wake_up(&shost->host_wait);
1468 * finally we need to re-initiate requests that may be pending. we will
1469 * have had everything blocked while error handling is taking place, and
1470 * now that error recovery is done, we will need to ensure that these
1471 * requests are started.
1473 scsi_run_host_queues(shost);
1477 * scsi_eh_ready_devs - check device ready state and recover if not.
1478 * @shost: host to be recovered.
1479 * @eh_done_q: list_head for processed commands.
1482 static void scsi_eh_ready_devs(struct Scsi_Host *shost,
1483 struct list_head *work_q,
1484 struct list_head *done_q)
1486 if (!scsi_eh_stu(shost, work_q, done_q))
1487 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
1488 if (!scsi_eh_bus_reset(shost, work_q, done_q))
1489 if (!scsi_eh_host_reset(work_q, done_q))
1490 scsi_eh_offline_sdevs(work_q, done_q);
1494 * scsi_eh_flush_done_q - finish processed commands or retry them.
1495 * @done_q: list_head of processed commands.
1498 static void scsi_eh_flush_done_q(struct list_head *done_q)
1500 struct scsi_cmnd *scmd, *next;
1502 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
1503 list_del_init(&scmd->eh_entry);
1504 if (scsi_device_online(scmd->device) &&
1505 !blk_noretry_request(scmd->request) &&
1506 (++scmd->retries < scmd->allowed)) {
1507 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush"
1508 " retry cmd: %p\n",
1509 current->comm,
1510 scmd));
1511 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
1512 } else {
1514 * If just we got sense for the device (called
1515 * scsi_eh_get_sense), scmd->result is already
1516 * set, do not set DRIVER_TIMEOUT.
1518 if (!scmd->result)
1519 scmd->result |= (DRIVER_TIMEOUT << 24);
1520 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush finish"
1521 " cmd: %p\n",
1522 current->comm, scmd));
1523 scsi_finish_command(scmd);
1529 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
1530 * @shost: Host to unjam.
1532 * Notes:
1533 * When we come in here, we *know* that all commands on the bus have
1534 * either completed, failed or timed out. we also know that no further
1535 * commands are being sent to the host, so things are relatively quiet
1536 * and we have freedom to fiddle with things as we wish.
1538 * This is only the *default* implementation. it is possible for
1539 * individual drivers to supply their own version of this function, and
1540 * if the maintainer wishes to do this, it is strongly suggested that
1541 * this function be taken as a template and modified. this function
1542 * was designed to correctly handle problems for about 95% of the
1543 * different cases out there, and it should always provide at least a
1544 * reasonable amount of error recovery.
1546 * Any command marked 'failed' or 'timeout' must eventually have
1547 * scsi_finish_cmd() called for it. we do all of the retry stuff
1548 * here, so when we restart the host after we return it should have an
1549 * empty queue.
1551 static void scsi_unjam_host(struct Scsi_Host *shost)
1553 unsigned long flags;
1554 LIST_HEAD(eh_work_q);
1555 LIST_HEAD(eh_done_q);
1557 spin_lock_irqsave(shost->host_lock, flags);
1558 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
1559 spin_unlock_irqrestore(shost->host_lock, flags);
1561 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
1563 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
1564 if (!scsi_eh_abort_cmds(&eh_work_q, &eh_done_q))
1565 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
1567 scsi_eh_flush_done_q(&eh_done_q);
1571 * scsi_error_handler - Handle errors/timeouts of SCSI cmds.
1572 * @data: Host for which we are running.
1574 * Notes:
1575 * This is always run in the context of a kernel thread. The idea is
1576 * that we start this thing up when the kernel starts up (one per host
1577 * that we detect), and it immediately goes to sleep and waits for some
1578 * event (i.e. failure). When this takes place, we have the job of
1579 * trying to unjam the bus and restarting things.
1581 int scsi_error_handler(void *data)
1583 struct Scsi_Host *shost = (struct Scsi_Host *) data;
1584 int rtn;
1585 DECLARE_MUTEX_LOCKED(sem);
1587 current->flags |= PF_NOFREEZE;
1588 shost->eh_wait = &sem;
1591 * Wake up the thread that created us.
1593 SCSI_LOG_ERROR_RECOVERY(3, printk("Wake up parent of"
1594 " scsi_eh_%d\n",shost->host_no));
1596 while (1) {
1598 * If we get a signal, it means we are supposed to go
1599 * away and die. This typically happens if the user is
1600 * trying to unload a module.
1602 SCSI_LOG_ERROR_RECOVERY(1, printk("Error handler"
1603 " scsi_eh_%d"
1604 " sleeping\n",shost->host_no));
1607 * Note - we always use down_interruptible with the semaphore
1608 * even if the module was loaded as part of the kernel. The
1609 * reason is that down() will cause this thread to be counted
1610 * in the load average as a running process, and down
1611 * interruptible doesn't. Given that we need to allow this
1612 * thread to die if the driver was loaded as a module, using
1613 * semaphores isn't unreasonable.
1615 down_interruptible(&sem);
1616 if (kthread_should_stop())
1617 break;
1619 SCSI_LOG_ERROR_RECOVERY(1, printk("Error handler"
1620 " scsi_eh_%d waking"
1621 " up\n",shost->host_no));
1623 shost->eh_active = 1;
1626 * We have a host that is failing for some reason. Figure out
1627 * what we need to do to get it up and online again (if we can).
1628 * If we fail, we end up taking the thing offline.
1630 if (shost->hostt->eh_strategy_handler)
1631 rtn = shost->hostt->eh_strategy_handler(shost);
1632 else
1633 scsi_unjam_host(shost);
1635 shost->eh_active = 0;
1638 * Note - if the above fails completely, the action is to take
1639 * individual devices offline and flush the queue of any
1640 * outstanding requests that may have been pending. When we
1641 * restart, we restart any I/O to any other devices on the bus
1642 * which are still online.
1644 scsi_restart_operations(shost);
1648 SCSI_LOG_ERROR_RECOVERY(1, printk("Error handler scsi_eh_%d"
1649 " exiting\n",shost->host_no));
1652 * Make sure that nobody tries to wake us up again.
1654 shost->eh_wait = NULL;
1655 return 0;
1659 * Function: scsi_report_bus_reset()
1661 * Purpose: Utility function used by low-level drivers to report that
1662 * they have observed a bus reset on the bus being handled.
1664 * Arguments: shost - Host in question
1665 * channel - channel on which reset was observed.
1667 * Returns: Nothing
1669 * Lock status: Host lock must be held.
1671 * Notes: This only needs to be called if the reset is one which
1672 * originates from an unknown location. Resets originated
1673 * by the mid-level itself don't need to call this, but there
1674 * should be no harm.
1676 * The main purpose of this is to make sure that a CHECK_CONDITION
1677 * is properly treated.
1679 void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
1681 struct scsi_device *sdev;
1683 __shost_for_each_device(sdev, shost) {
1684 if (channel == sdev->channel) {
1685 sdev->was_reset = 1;
1686 sdev->expecting_cc_ua = 1;
1690 EXPORT_SYMBOL(scsi_report_bus_reset);
1693 * Function: scsi_report_device_reset()
1695 * Purpose: Utility function used by low-level drivers to report that
1696 * they have observed a device reset on the device being handled.
1698 * Arguments: shost - Host in question
1699 * channel - channel on which reset was observed
1700 * target - target on which reset was observed
1702 * Returns: Nothing
1704 * Lock status: Host lock must be held
1706 * Notes: This only needs to be called if the reset is one which
1707 * originates from an unknown location. Resets originated
1708 * by the mid-level itself don't need to call this, but there
1709 * should be no harm.
1711 * The main purpose of this is to make sure that a CHECK_CONDITION
1712 * is properly treated.
1714 void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
1716 struct scsi_device *sdev;
1718 __shost_for_each_device(sdev, shost) {
1719 if (channel == sdev->channel &&
1720 target == sdev->id) {
1721 sdev->was_reset = 1;
1722 sdev->expecting_cc_ua = 1;
1726 EXPORT_SYMBOL(scsi_report_device_reset);
1728 static void
1729 scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
1734 * Function: scsi_reset_provider
1736 * Purpose: Send requested reset to a bus or device at any phase.
1738 * Arguments: device - device to send reset to
1739 * flag - reset type (see scsi.h)
1741 * Returns: SUCCESS/FAILURE.
1743 * Notes: This is used by the SCSI Generic driver to provide
1744 * Bus/Device reset capability.
1747 scsi_reset_provider(struct scsi_device *dev, int flag)
1749 struct scsi_cmnd *scmd = scsi_get_command(dev, GFP_KERNEL);
1750 struct request req;
1751 int rtn;
1753 scmd->request = &req;
1754 memset(&scmd->eh_timeout, 0, sizeof(scmd->eh_timeout));
1755 scmd->request->rq_status = RQ_SCSI_BUSY;
1757 memset(&scmd->cmnd, '\0', sizeof(scmd->cmnd));
1759 scmd->scsi_done = scsi_reset_provider_done_command;
1760 scmd->done = NULL;
1761 scmd->buffer = NULL;
1762 scmd->bufflen = 0;
1763 scmd->request_buffer = NULL;
1764 scmd->request_bufflen = 0;
1766 scmd->cmd_len = 0;
1768 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
1769 scmd->sc_request = NULL;
1770 scmd->sc_magic = SCSI_CMND_MAGIC;
1772 init_timer(&scmd->eh_timeout);
1775 * Sometimes the command can get back into the timer chain,
1776 * so use the pid as an identifier.
1778 scmd->pid = 0;
1780 switch (flag) {
1781 case SCSI_TRY_RESET_DEVICE:
1782 rtn = scsi_try_bus_device_reset(scmd);
1783 if (rtn == SUCCESS)
1784 break;
1785 /* FALLTHROUGH */
1786 case SCSI_TRY_RESET_BUS:
1787 rtn = scsi_try_bus_reset(scmd);
1788 if (rtn == SUCCESS)
1789 break;
1790 /* FALLTHROUGH */
1791 case SCSI_TRY_RESET_HOST:
1792 rtn = scsi_try_host_reset(scmd);
1793 break;
1794 default:
1795 rtn = FAILED;
1798 scsi_next_command(scmd);
1799 return rtn;
1801 EXPORT_SYMBOL(scsi_reset_provider);
1804 * scsi_normalize_sense - normalize main elements from either fixed or
1805 * descriptor sense data format into a common format.
1807 * @sense_buffer: byte array containing sense data returned by device
1808 * @sb_len: number of valid bytes in sense_buffer
1809 * @sshdr: pointer to instance of structure that common
1810 * elements are written to.
1812 * Notes:
1813 * The "main elements" from sense data are: response_code, sense_key,
1814 * asc, ascq and additional_length (only for descriptor format).
1816 * Typically this function can be called after a device has
1817 * responded to a SCSI command with the CHECK_CONDITION status.
1819 * Return value:
1820 * 1 if valid sense data information found, else 0;
1822 int scsi_normalize_sense(const u8 *sense_buffer, int sb_len,
1823 struct scsi_sense_hdr *sshdr)
1825 if (!sense_buffer || !sb_len)
1826 return 0;
1828 memset(sshdr, 0, sizeof(struct scsi_sense_hdr));
1830 sshdr->response_code = (sense_buffer[0] & 0x7f);
1832 if (!scsi_sense_valid(sshdr))
1833 return 0;
1835 if (sshdr->response_code >= 0x72) {
1837 * descriptor format
1839 if (sb_len > 1)
1840 sshdr->sense_key = (sense_buffer[1] & 0xf);
1841 if (sb_len > 2)
1842 sshdr->asc = sense_buffer[2];
1843 if (sb_len > 3)
1844 sshdr->ascq = sense_buffer[3];
1845 if (sb_len > 7)
1846 sshdr->additional_length = sense_buffer[7];
1847 } else {
1849 * fixed format
1851 if (sb_len > 2)
1852 sshdr->sense_key = (sense_buffer[2] & 0xf);
1853 if (sb_len > 7) {
1854 sb_len = (sb_len < (sense_buffer[7] + 8)) ?
1855 sb_len : (sense_buffer[7] + 8);
1856 if (sb_len > 12)
1857 sshdr->asc = sense_buffer[12];
1858 if (sb_len > 13)
1859 sshdr->ascq = sense_buffer[13];
1863 return 1;
1865 EXPORT_SYMBOL(scsi_normalize_sense);
1867 int scsi_request_normalize_sense(struct scsi_request *sreq,
1868 struct scsi_sense_hdr *sshdr)
1870 return scsi_normalize_sense(sreq->sr_sense_buffer,
1871 sizeof(sreq->sr_sense_buffer), sshdr);
1873 EXPORT_SYMBOL(scsi_request_normalize_sense);
1875 int scsi_command_normalize_sense(struct scsi_cmnd *cmd,
1876 struct scsi_sense_hdr *sshdr)
1878 return scsi_normalize_sense(cmd->sense_buffer,
1879 sizeof(cmd->sense_buffer), sshdr);
1881 EXPORT_SYMBOL(scsi_command_normalize_sense);
1884 * scsi_sense_desc_find - search for a given descriptor type in
1885 * descriptor sense data format.
1887 * @sense_buffer: byte array of descriptor format sense data
1888 * @sb_len: number of valid bytes in sense_buffer
1889 * @desc_type: value of descriptor type to find
1890 * (e.g. 0 -> information)
1892 * Notes:
1893 * only valid when sense data is in descriptor format
1895 * Return value:
1896 * pointer to start of (first) descriptor if found else NULL
1898 const u8 * scsi_sense_desc_find(const u8 * sense_buffer, int sb_len,
1899 int desc_type)
1901 int add_sen_len, add_len, desc_len, k;
1902 const u8 * descp;
1904 if ((sb_len < 8) || (0 == (add_sen_len = sense_buffer[7])))
1905 return NULL;
1906 if ((sense_buffer[0] < 0x72) || (sense_buffer[0] > 0x73))
1907 return NULL;
1908 add_sen_len = (add_sen_len < (sb_len - 8)) ?
1909 add_sen_len : (sb_len - 8);
1910 descp = &sense_buffer[8];
1911 for (desc_len = 0, k = 0; k < add_sen_len; k += desc_len) {
1912 descp += desc_len;
1913 add_len = (k < (add_sen_len - 1)) ? descp[1]: -1;
1914 desc_len = add_len + 2;
1915 if (descp[0] == desc_type)
1916 return descp;
1917 if (add_len < 0) // short descriptor ??
1918 break;
1920 return NULL;
1922 EXPORT_SYMBOL(scsi_sense_desc_find);
1925 * scsi_get_sense_info_fld - attempts to get information field from
1926 * sense data (either fixed or descriptor format)
1928 * @sense_buffer: byte array of sense data
1929 * @sb_len: number of valid bytes in sense_buffer
1930 * @info_out: pointer to 64 integer where 8 or 4 byte information
1931 * field will be placed if found.
1933 * Return value:
1934 * 1 if information field found, 0 if not found.
1936 int scsi_get_sense_info_fld(const u8 * sense_buffer, int sb_len,
1937 u64 * info_out)
1939 int j;
1940 const u8 * ucp;
1941 u64 ull;
1943 if (sb_len < 7)
1944 return 0;
1945 switch (sense_buffer[0] & 0x7f) {
1946 case 0x70:
1947 case 0x71:
1948 if (sense_buffer[0] & 0x80) {
1949 *info_out = (sense_buffer[3] << 24) +
1950 (sense_buffer[4] << 16) +
1951 (sense_buffer[5] << 8) + sense_buffer[6];
1952 return 1;
1953 } else
1954 return 0;
1955 case 0x72:
1956 case 0x73:
1957 ucp = scsi_sense_desc_find(sense_buffer, sb_len,
1958 0 /* info desc */);
1959 if (ucp && (0xa == ucp[1])) {
1960 ull = 0;
1961 for (j = 0; j < 8; ++j) {
1962 if (j > 0)
1963 ull <<= 8;
1964 ull |= ucp[4 + j];
1966 *info_out = ull;
1967 return 1;
1968 } else
1969 return 0;
1970 default:
1971 return 0;
1974 EXPORT_SYMBOL(scsi_get_sense_info_fld);