x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / drivers / scsi / scsi_error.c
blobcf70f0bb83754bab05dd101e245f4c0a50b7921c
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/gfp.h>
20 #include <linux/timer.h>
21 #include <linux/string.h>
22 #include <linux/kernel.h>
23 #include <linux/freezer.h>
24 #include <linux/kthread.h>
25 #include <linux/interrupt.h>
26 #include <linux/blkdev.h>
27 #include <linux/delay.h>
28 #include <linux/jiffies.h>
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_cmnd.h>
32 #include <scsi/scsi_dbg.h>
33 #include <scsi/scsi_device.h>
34 #include <scsi/scsi_driver.h>
35 #include <scsi/scsi_eh.h>
36 #include <scsi/scsi_common.h>
37 #include <scsi/scsi_transport.h>
38 #include <scsi/scsi_host.h>
39 #include <scsi/scsi_ioctl.h>
40 #include <scsi/scsi_dh.h>
41 #include <scsi/sg.h>
43 #include "scsi_priv.h"
44 #include "scsi_logging.h"
45 #include "scsi_transport_api.h"
47 #include <trace/events/scsi.h>
49 #include <asm/unaligned.h>
51 static void scsi_eh_done(struct scsi_cmnd *scmd);
54 * These should *probably* be handled by the host itself.
55 * Since it is allowed to sleep, it probably should.
57 #define BUS_RESET_SETTLE_TIME (10)
58 #define HOST_RESET_SETTLE_TIME (10)
60 static int scsi_eh_try_stu(struct scsi_cmnd *scmd);
61 static int scsi_try_to_abort_cmd(struct scsi_host_template *,
62 struct scsi_cmnd *);
64 /* called with shost->host_lock held */
65 void scsi_eh_wakeup(struct Scsi_Host *shost)
67 if (atomic_read(&shost->host_busy) == shost->host_failed) {
68 trace_scsi_eh_wakeup(shost);
69 wake_up_process(shost->ehandler);
70 SCSI_LOG_ERROR_RECOVERY(5, shost_printk(KERN_INFO, shost,
71 "Waking error handler thread\n"));
75 /**
76 * scsi_schedule_eh - schedule EH for SCSI host
77 * @shost: SCSI host to invoke error handling on.
79 * Schedule SCSI EH without scmd.
81 void scsi_schedule_eh(struct Scsi_Host *shost)
83 unsigned long flags;
85 spin_lock_irqsave(shost->host_lock, flags);
87 if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
88 scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
89 shost->host_eh_scheduled++;
90 scsi_eh_wakeup(shost);
93 spin_unlock_irqrestore(shost->host_lock, flags);
95 EXPORT_SYMBOL_GPL(scsi_schedule_eh);
97 static int scsi_host_eh_past_deadline(struct Scsi_Host *shost)
99 if (!shost->last_reset || shost->eh_deadline == -1)
100 return 0;
103 * 32bit accesses are guaranteed to be atomic
104 * (on all supported architectures), so instead
105 * of using a spinlock we can as well double check
106 * if eh_deadline has been set to 'off' during the
107 * time_before call.
109 if (time_before(jiffies, shost->last_reset + shost->eh_deadline) &&
110 shost->eh_deadline > -1)
111 return 0;
113 return 1;
117 * scmd_eh_abort_handler - Handle command aborts
118 * @work: command to be aborted.
120 void
121 scmd_eh_abort_handler(struct work_struct *work)
123 struct scsi_cmnd *scmd =
124 container_of(work, struct scsi_cmnd, abort_work.work);
125 struct scsi_device *sdev = scmd->device;
126 int rtn;
128 if (scsi_host_eh_past_deadline(sdev->host)) {
129 SCSI_LOG_ERROR_RECOVERY(3,
130 scmd_printk(KERN_INFO, scmd,
131 "eh timeout, not aborting\n"));
132 } else {
133 SCSI_LOG_ERROR_RECOVERY(3,
134 scmd_printk(KERN_INFO, scmd,
135 "aborting command\n"));
136 rtn = scsi_try_to_abort_cmd(sdev->host->hostt, scmd);
137 if (rtn == SUCCESS) {
138 set_host_byte(scmd, DID_TIME_OUT);
139 if (scsi_host_eh_past_deadline(sdev->host)) {
140 SCSI_LOG_ERROR_RECOVERY(3,
141 scmd_printk(KERN_INFO, scmd,
142 "eh timeout, not retrying "
143 "aborted command\n"));
144 } else if (!scsi_noretry_cmd(scmd) &&
145 (++scmd->retries <= scmd->allowed)) {
146 SCSI_LOG_ERROR_RECOVERY(3,
147 scmd_printk(KERN_WARNING, scmd,
148 "retry aborted command\n"));
149 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
150 return;
151 } else {
152 SCSI_LOG_ERROR_RECOVERY(3,
153 scmd_printk(KERN_WARNING, scmd,
154 "finish aborted command\n"));
155 scsi_finish_command(scmd);
156 return;
158 } else {
159 SCSI_LOG_ERROR_RECOVERY(3,
160 scmd_printk(KERN_INFO, scmd,
161 "cmd abort %s\n",
162 (rtn == FAST_IO_FAIL) ?
163 "not send" : "failed"));
167 scsi_eh_scmd_add(scmd);
171 * scsi_abort_command - schedule a command abort
172 * @scmd: scmd to abort.
174 * We only need to abort commands after a command timeout
176 static int
177 scsi_abort_command(struct scsi_cmnd *scmd)
179 struct scsi_device *sdev = scmd->device;
180 struct Scsi_Host *shost = sdev->host;
181 unsigned long flags;
183 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
185 * Retry after abort failed, escalate to next level.
187 SCSI_LOG_ERROR_RECOVERY(3,
188 scmd_printk(KERN_INFO, scmd,
189 "previous abort failed\n"));
190 BUG_ON(delayed_work_pending(&scmd->abort_work));
191 return FAILED;
194 spin_lock_irqsave(shost->host_lock, flags);
195 if (shost->eh_deadline != -1 && !shost->last_reset)
196 shost->last_reset = jiffies;
197 spin_unlock_irqrestore(shost->host_lock, flags);
199 scmd->eh_eflags |= SCSI_EH_ABORT_SCHEDULED;
200 SCSI_LOG_ERROR_RECOVERY(3,
201 scmd_printk(KERN_INFO, scmd, "abort scheduled\n"));
202 queue_delayed_work(shost->tmf_work_q, &scmd->abort_work, HZ / 100);
203 return SUCCESS;
207 * scsi_eh_reset - call into ->eh_action to reset internal counters
208 * @scmd: scmd to run eh on.
210 * The scsi driver might be carrying internal state about the
211 * devices, so we need to call into the driver to reset the
212 * internal state once the error handler is started.
214 static void scsi_eh_reset(struct scsi_cmnd *scmd)
216 if (!blk_rq_is_passthrough(scmd->request)) {
217 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
218 if (sdrv->eh_reset)
219 sdrv->eh_reset(scmd);
223 static void scsi_eh_inc_host_failed(struct rcu_head *head)
225 struct scsi_cmnd *scmd = container_of(head, typeof(*scmd), rcu);
226 struct Scsi_Host *shost = scmd->device->host;
227 unsigned long flags;
229 spin_lock_irqsave(shost->host_lock, flags);
230 shost->host_failed++;
231 scsi_eh_wakeup(shost);
232 spin_unlock_irqrestore(shost->host_lock, flags);
236 * scsi_eh_scmd_add - add scsi cmd to error handling.
237 * @scmd: scmd to run eh on.
239 void scsi_eh_scmd_add(struct scsi_cmnd *scmd)
241 struct Scsi_Host *shost = scmd->device->host;
242 unsigned long flags;
243 int ret;
245 WARN_ON_ONCE(!shost->ehandler);
247 spin_lock_irqsave(shost->host_lock, flags);
248 if (scsi_host_set_state(shost, SHOST_RECOVERY)) {
249 ret = scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY);
250 WARN_ON_ONCE(ret);
252 if (shost->eh_deadline != -1 && !shost->last_reset)
253 shost->last_reset = jiffies;
255 scsi_eh_reset(scmd);
256 list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
257 spin_unlock_irqrestore(shost->host_lock, flags);
259 * Ensure that all tasks observe the host state change before the
260 * host_failed change.
262 call_rcu(&scmd->rcu, scsi_eh_inc_host_failed);
266 * scsi_times_out - Timeout function for normal scsi commands.
267 * @req: request that is timing out.
269 * Notes:
270 * We do not need to lock this. There is the potential for a race
271 * only in that the normal completion handling might run, but if the
272 * normal completion function determines that the timer has already
273 * fired, then it mustn't do anything.
275 enum blk_eh_timer_return scsi_times_out(struct request *req)
277 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
278 enum blk_eh_timer_return rtn = BLK_EH_NOT_HANDLED;
279 struct Scsi_Host *host = scmd->device->host;
281 trace_scsi_dispatch_cmd_timeout(scmd);
282 scsi_log_completion(scmd, TIMEOUT_ERROR);
284 if (host->eh_deadline != -1 && !host->last_reset)
285 host->last_reset = jiffies;
287 if (host->hostt->eh_timed_out)
288 rtn = host->hostt->eh_timed_out(scmd);
290 if (rtn == BLK_EH_NOT_HANDLED) {
291 if (scsi_abort_command(scmd) != SUCCESS) {
292 set_host_byte(scmd, DID_TIME_OUT);
293 scsi_eh_scmd_add(scmd);
297 return rtn;
301 * scsi_block_when_processing_errors - Prevent cmds from being queued.
302 * @sdev: Device on which we are performing recovery.
304 * Description:
305 * We block until the host is out of error recovery, and then check to
306 * see whether the host or the device is offline.
308 * Return value:
309 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
311 int scsi_block_when_processing_errors(struct scsi_device *sdev)
313 int online;
315 wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
317 online = scsi_device_online(sdev);
319 SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_INFO, sdev,
320 "%s: rtn: %d\n", __func__, online));
322 return online;
324 EXPORT_SYMBOL(scsi_block_when_processing_errors);
326 #ifdef CONFIG_SCSI_LOGGING
328 * scsi_eh_prt_fail_stats - Log info on failures.
329 * @shost: scsi host being recovered.
330 * @work_q: Queue of scsi cmds to process.
332 static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
333 struct list_head *work_q)
335 struct scsi_cmnd *scmd;
336 struct scsi_device *sdev;
337 int total_failures = 0;
338 int cmd_failed = 0;
339 int cmd_cancel = 0;
340 int devices_failed = 0;
342 shost_for_each_device(sdev, shost) {
343 list_for_each_entry(scmd, work_q, eh_entry) {
344 if (scmd->device == sdev) {
345 ++total_failures;
346 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED)
347 ++cmd_cancel;
348 else
349 ++cmd_failed;
353 if (cmd_cancel || cmd_failed) {
354 SCSI_LOG_ERROR_RECOVERY(3,
355 shost_printk(KERN_INFO, shost,
356 "%s: cmds failed: %d, cancel: %d\n",
357 __func__, cmd_failed,
358 cmd_cancel));
359 cmd_cancel = 0;
360 cmd_failed = 0;
361 ++devices_failed;
365 SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO, shost,
366 "Total of %d commands on %d"
367 " devices require eh work\n",
368 total_failures, devices_failed));
370 #endif
373 * scsi_report_lun_change - Set flag on all *other* devices on the same target
374 * to indicate that a UNIT ATTENTION is expected.
375 * @sdev: Device reporting the UNIT ATTENTION
377 static void scsi_report_lun_change(struct scsi_device *sdev)
379 sdev->sdev_target->expecting_lun_change = 1;
383 * scsi_report_sense - Examine scsi sense information and log messages for
384 * certain conditions, also issue uevents for some of them.
385 * @sdev: Device reporting the sense code
386 * @sshdr: sshdr to be examined
388 static void scsi_report_sense(struct scsi_device *sdev,
389 struct scsi_sense_hdr *sshdr)
391 enum scsi_device_event evt_type = SDEV_EVT_MAXBITS; /* i.e. none */
393 if (sshdr->sense_key == UNIT_ATTENTION) {
394 if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) {
395 evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED;
396 sdev_printk(KERN_WARNING, sdev,
397 "Inquiry data has changed");
398 } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) {
399 evt_type = SDEV_EVT_LUN_CHANGE_REPORTED;
400 scsi_report_lun_change(sdev);
401 sdev_printk(KERN_WARNING, sdev,
402 "Warning! Received an indication that the "
403 "LUN assignments on this target have "
404 "changed. The Linux SCSI layer does not "
405 "automatically remap LUN assignments.\n");
406 } else if (sshdr->asc == 0x3f)
407 sdev_printk(KERN_WARNING, sdev,
408 "Warning! Received an indication that the "
409 "operating parameters on this target have "
410 "changed. The Linux SCSI layer does not "
411 "automatically adjust these parameters.\n");
413 if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) {
414 evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED;
415 sdev_printk(KERN_WARNING, sdev,
416 "Warning! Received an indication that the "
417 "LUN reached a thin provisioning soft "
418 "threshold.\n");
421 if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) {
422 evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED;
423 sdev_printk(KERN_WARNING, sdev,
424 "Mode parameters changed");
425 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x06) {
426 evt_type = SDEV_EVT_ALUA_STATE_CHANGE_REPORTED;
427 sdev_printk(KERN_WARNING, sdev,
428 "Asymmetric access state changed");
429 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) {
430 evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED;
431 sdev_printk(KERN_WARNING, sdev,
432 "Capacity data has changed");
433 } else if (sshdr->asc == 0x2a)
434 sdev_printk(KERN_WARNING, sdev,
435 "Parameters changed");
438 if (evt_type != SDEV_EVT_MAXBITS) {
439 set_bit(evt_type, sdev->pending_events);
440 schedule_work(&sdev->event_work);
445 * scsi_check_sense - Examine scsi cmd sense
446 * @scmd: Cmd to have sense checked.
448 * Return value:
449 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
451 * Notes:
452 * When a deferred error is detected the current command has
453 * not been executed and needs retrying.
455 int scsi_check_sense(struct scsi_cmnd *scmd)
457 struct scsi_device *sdev = scmd->device;
458 struct scsi_sense_hdr sshdr;
460 if (! scsi_command_normalize_sense(scmd, &sshdr))
461 return FAILED; /* no valid sense data */
463 scsi_report_sense(sdev, &sshdr);
465 if (scsi_sense_is_deferred(&sshdr))
466 return NEEDS_RETRY;
468 if (sdev->handler && sdev->handler->check_sense) {
469 int rc;
471 rc = sdev->handler->check_sense(sdev, &sshdr);
472 if (rc != SCSI_RETURN_NOT_HANDLED)
473 return rc;
474 /* handler does not care. Drop down to default handling */
477 if (scmd->cmnd[0] == TEST_UNIT_READY && scmd->scsi_done != scsi_eh_done)
479 * nasty: for mid-layer issued TURs, we need to return the
480 * actual sense data without any recovery attempt. For eh
481 * issued ones, we need to try to recover and interpret
483 return SUCCESS;
486 * Previous logic looked for FILEMARK, EOM or ILI which are
487 * mainly associated with tapes and returned SUCCESS.
489 if (sshdr.response_code == 0x70) {
490 /* fixed format */
491 if (scmd->sense_buffer[2] & 0xe0)
492 return SUCCESS;
493 } else {
495 * descriptor format: look for "stream commands sense data
496 * descriptor" (see SSC-3). Assume single sense data
497 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
499 if ((sshdr.additional_length > 3) &&
500 (scmd->sense_buffer[8] == 0x4) &&
501 (scmd->sense_buffer[11] & 0xe0))
502 return SUCCESS;
505 switch (sshdr.sense_key) {
506 case NO_SENSE:
507 return SUCCESS;
508 case RECOVERED_ERROR:
509 return /* soft_error */ SUCCESS;
511 case ABORTED_COMMAND:
512 if (sshdr.asc == 0x10) /* DIF */
513 return SUCCESS;
515 return NEEDS_RETRY;
516 case NOT_READY:
517 case UNIT_ATTENTION:
519 * if we are expecting a cc/ua because of a bus reset that we
520 * performed, treat this just as a retry. otherwise this is
521 * information that we should pass up to the upper-level driver
522 * so that we can deal with it there.
524 if (scmd->device->expecting_cc_ua) {
526 * Because some device does not queue unit
527 * attentions correctly, we carefully check
528 * additional sense code and qualifier so as
529 * not to squash media change unit attention.
531 if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) {
532 scmd->device->expecting_cc_ua = 0;
533 return NEEDS_RETRY;
537 * we might also expect a cc/ua if another LUN on the target
538 * reported a UA with an ASC/ASCQ of 3F 0E -
539 * REPORTED LUNS DATA HAS CHANGED.
541 if (scmd->device->sdev_target->expecting_lun_change &&
542 sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
543 return NEEDS_RETRY;
545 * if the device is in the process of becoming ready, we
546 * should retry.
548 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
549 return NEEDS_RETRY;
551 * if the device is not started, we need to wake
552 * the error handler to start the motor
554 if (scmd->device->allow_restart &&
555 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
556 return FAILED;
558 * Pass the UA upwards for a determination in the completion
559 * functions.
561 return SUCCESS;
563 /* these are not supported */
564 case DATA_PROTECT:
565 if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
566 /* Thin provisioning hard threshold reached */
567 set_host_byte(scmd, DID_ALLOC_FAILURE);
568 return SUCCESS;
570 /* FALLTHROUGH */
571 case COPY_ABORTED:
572 case VOLUME_OVERFLOW:
573 case MISCOMPARE:
574 case BLANK_CHECK:
575 set_host_byte(scmd, DID_TARGET_FAILURE);
576 return SUCCESS;
578 case MEDIUM_ERROR:
579 if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
580 sshdr.asc == 0x13 || /* AMNF DATA FIELD */
581 sshdr.asc == 0x14) { /* RECORD NOT FOUND */
582 set_host_byte(scmd, DID_MEDIUM_ERROR);
583 return SUCCESS;
585 return NEEDS_RETRY;
587 case HARDWARE_ERROR:
588 if (scmd->device->retry_hwerror)
589 return ADD_TO_MLQUEUE;
590 else
591 set_host_byte(scmd, DID_TARGET_FAILURE);
592 /* FALLTHROUGH */
594 case ILLEGAL_REQUEST:
595 if (sshdr.asc == 0x20 || /* Invalid command operation code */
596 sshdr.asc == 0x21 || /* Logical block address out of range */
597 sshdr.asc == 0x24 || /* Invalid field in cdb */
598 sshdr.asc == 0x26 || /* Parameter value invalid */
599 sshdr.asc == 0x27) { /* Write protected */
600 set_host_byte(scmd, DID_TARGET_FAILURE);
602 return SUCCESS;
604 default:
605 return SUCCESS;
608 EXPORT_SYMBOL_GPL(scsi_check_sense);
610 static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
612 struct scsi_host_template *sht = sdev->host->hostt;
613 struct scsi_device *tmp_sdev;
615 if (!sht->track_queue_depth ||
616 sdev->queue_depth >= sdev->max_queue_depth)
617 return;
619 if (time_before(jiffies,
620 sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
621 return;
623 if (time_before(jiffies,
624 sdev->last_queue_full_time + sdev->queue_ramp_up_period))
625 return;
628 * Walk all devices of a target and do
629 * ramp up on them.
631 shost_for_each_device(tmp_sdev, sdev->host) {
632 if (tmp_sdev->channel != sdev->channel ||
633 tmp_sdev->id != sdev->id ||
634 tmp_sdev->queue_depth == sdev->max_queue_depth)
635 continue;
637 scsi_change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1);
638 sdev->last_queue_ramp_up = jiffies;
642 static void scsi_handle_queue_full(struct scsi_device *sdev)
644 struct scsi_host_template *sht = sdev->host->hostt;
645 struct scsi_device *tmp_sdev;
647 if (!sht->track_queue_depth)
648 return;
650 shost_for_each_device(tmp_sdev, sdev->host) {
651 if (tmp_sdev->channel != sdev->channel ||
652 tmp_sdev->id != sdev->id)
653 continue;
655 * We do not know the number of commands that were at
656 * the device when we got the queue full so we start
657 * from the highest possible value and work our way down.
659 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
664 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
665 * @scmd: SCSI cmd to examine.
667 * Notes:
668 * This is *only* called when we are examining the status of commands
669 * queued during error recovery. the main difference here is that we
670 * don't allow for the possibility of retries here, and we are a lot
671 * more restrictive about what we consider acceptable.
673 static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
676 * first check the host byte, to see if there is anything in there
677 * that would indicate what we need to do.
679 if (host_byte(scmd->result) == DID_RESET) {
681 * rats. we are already in the error handler, so we now
682 * get to try and figure out what to do next. if the sense
683 * is valid, we have a pretty good idea of what to do.
684 * if not, we mark it as FAILED.
686 return scsi_check_sense(scmd);
688 if (host_byte(scmd->result) != DID_OK)
689 return FAILED;
692 * next, check the message byte.
694 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
695 return FAILED;
698 * now, check the status byte to see if this indicates
699 * anything special.
701 switch (status_byte(scmd->result)) {
702 case GOOD:
703 scsi_handle_queue_ramp_up(scmd->device);
704 /* FALLTHROUGH */
705 case COMMAND_TERMINATED:
706 return SUCCESS;
707 case CHECK_CONDITION:
708 return scsi_check_sense(scmd);
709 case CONDITION_GOOD:
710 case INTERMEDIATE_GOOD:
711 case INTERMEDIATE_C_GOOD:
713 * who knows? FIXME(eric)
715 return SUCCESS;
716 case RESERVATION_CONFLICT:
717 if (scmd->cmnd[0] == TEST_UNIT_READY)
718 /* it is a success, we probed the device and
719 * found it */
720 return SUCCESS;
721 /* otherwise, we failed to send the command */
722 return FAILED;
723 case QUEUE_FULL:
724 scsi_handle_queue_full(scmd->device);
725 /* fall through */
726 case BUSY:
727 return NEEDS_RETRY;
728 default:
729 return FAILED;
731 return FAILED;
735 * scsi_eh_done - Completion function for error handling.
736 * @scmd: Cmd that is done.
738 static void scsi_eh_done(struct scsi_cmnd *scmd)
740 struct completion *eh_action;
742 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
743 "%s result: %x\n", __func__, scmd->result));
745 eh_action = scmd->device->host->eh_action;
746 if (eh_action)
747 complete(eh_action);
751 * scsi_try_host_reset - ask host adapter to reset itself
752 * @scmd: SCSI cmd to send host reset.
754 static int scsi_try_host_reset(struct scsi_cmnd *scmd)
756 unsigned long flags;
757 int rtn;
758 struct Scsi_Host *host = scmd->device->host;
759 struct scsi_host_template *hostt = host->hostt;
761 SCSI_LOG_ERROR_RECOVERY(3,
762 shost_printk(KERN_INFO, host, "Snd Host RST\n"));
764 if (!hostt->eh_host_reset_handler)
765 return FAILED;
767 rtn = hostt->eh_host_reset_handler(scmd);
769 if (rtn == SUCCESS) {
770 if (!hostt->skip_settle_delay)
771 ssleep(HOST_RESET_SETTLE_TIME);
772 spin_lock_irqsave(host->host_lock, flags);
773 scsi_report_bus_reset(host, scmd_channel(scmd));
774 spin_unlock_irqrestore(host->host_lock, flags);
777 return rtn;
781 * scsi_try_bus_reset - ask host to perform a bus reset
782 * @scmd: SCSI cmd to send bus reset.
784 static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
786 unsigned long flags;
787 int rtn;
788 struct Scsi_Host *host = scmd->device->host;
789 struct scsi_host_template *hostt = host->hostt;
791 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
792 "%s: Snd Bus RST\n", __func__));
794 if (!hostt->eh_bus_reset_handler)
795 return FAILED;
797 rtn = hostt->eh_bus_reset_handler(scmd);
799 if (rtn == SUCCESS) {
800 if (!hostt->skip_settle_delay)
801 ssleep(BUS_RESET_SETTLE_TIME);
802 spin_lock_irqsave(host->host_lock, flags);
803 scsi_report_bus_reset(host, scmd_channel(scmd));
804 spin_unlock_irqrestore(host->host_lock, flags);
807 return rtn;
810 static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
812 sdev->was_reset = 1;
813 sdev->expecting_cc_ua = 1;
817 * scsi_try_target_reset - Ask host to perform a target reset
818 * @scmd: SCSI cmd used to send a target reset
820 * Notes:
821 * There is no timeout for this operation. if this operation is
822 * unreliable for a given host, then the host itself needs to put a
823 * timer on it, and set the host back to a consistent state prior to
824 * returning.
826 static int scsi_try_target_reset(struct scsi_cmnd *scmd)
828 unsigned long flags;
829 int rtn;
830 struct Scsi_Host *host = scmd->device->host;
831 struct scsi_host_template *hostt = host->hostt;
833 if (!hostt->eh_target_reset_handler)
834 return FAILED;
836 rtn = hostt->eh_target_reset_handler(scmd);
837 if (rtn == SUCCESS) {
838 spin_lock_irqsave(host->host_lock, flags);
839 __starget_for_each_device(scsi_target(scmd->device), NULL,
840 __scsi_report_device_reset);
841 spin_unlock_irqrestore(host->host_lock, flags);
844 return rtn;
848 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
849 * @scmd: SCSI cmd used to send BDR
851 * Notes:
852 * There is no timeout for this operation. if this operation is
853 * unreliable for a given host, then the host itself needs to put a
854 * timer on it, and set the host back to a consistent state prior to
855 * returning.
857 static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
859 int rtn;
860 struct scsi_host_template *hostt = scmd->device->host->hostt;
862 if (!hostt->eh_device_reset_handler)
863 return FAILED;
865 rtn = hostt->eh_device_reset_handler(scmd);
866 if (rtn == SUCCESS)
867 __scsi_report_device_reset(scmd->device, NULL);
868 return rtn;
872 * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
873 * @hostt: SCSI driver host template
874 * @scmd: SCSI cmd used to send a target reset
876 * Return value:
877 * SUCCESS, FAILED, or FAST_IO_FAIL
879 * Notes:
880 * SUCCESS does not necessarily indicate that the command
881 * has been aborted; it only indicates that the LLDDs
882 * has cleared all references to that command.
883 * LLDDs should return FAILED only if an abort was required
884 * but could not be executed. LLDDs should return FAST_IO_FAIL
885 * if the device is temporarily unavailable (eg due to a
886 * link down on FibreChannel)
888 static int scsi_try_to_abort_cmd(struct scsi_host_template *hostt,
889 struct scsi_cmnd *scmd)
891 if (!hostt->eh_abort_handler)
892 return FAILED;
894 return hostt->eh_abort_handler(scmd);
897 static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
899 if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
900 if (scsi_try_bus_device_reset(scmd) != SUCCESS)
901 if (scsi_try_target_reset(scmd) != SUCCESS)
902 if (scsi_try_bus_reset(scmd) != SUCCESS)
903 scsi_try_host_reset(scmd);
907 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
908 * @scmd: SCSI command structure to hijack
909 * @ses: structure to save restore information
910 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
911 * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
912 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
914 * This function is used to save a scsi command information before re-execution
915 * as part of the error recovery process. If @sense_bytes is 0 the command
916 * sent must be one that does not transfer any data. If @sense_bytes != 0
917 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
918 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
920 void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
921 unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
923 struct scsi_device *sdev = scmd->device;
926 * We need saved copies of a number of fields - this is because
927 * error handling may need to overwrite these with different values
928 * to run different commands, and once error handling is complete,
929 * we will need to restore these values prior to running the actual
930 * command.
932 ses->cmd_len = scmd->cmd_len;
933 ses->cmnd = scmd->cmnd;
934 ses->data_direction = scmd->sc_data_direction;
935 ses->sdb = scmd->sdb;
936 ses->next_rq = scmd->request->next_rq;
937 ses->result = scmd->result;
938 ses->underflow = scmd->underflow;
939 ses->prot_op = scmd->prot_op;
940 ses->eh_eflags = scmd->eh_eflags;
942 scmd->prot_op = SCSI_PROT_NORMAL;
943 scmd->eh_eflags = 0;
944 scmd->cmnd = ses->eh_cmnd;
945 memset(scmd->cmnd, 0, BLK_MAX_CDB);
946 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
947 scmd->request->next_rq = NULL;
948 scmd->result = 0;
950 if (sense_bytes) {
951 scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
952 sense_bytes);
953 sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
954 scmd->sdb.length);
955 scmd->sdb.table.sgl = &ses->sense_sgl;
956 scmd->sc_data_direction = DMA_FROM_DEVICE;
957 scmd->sdb.table.nents = scmd->sdb.table.orig_nents = 1;
958 scmd->cmnd[0] = REQUEST_SENSE;
959 scmd->cmnd[4] = scmd->sdb.length;
960 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
961 } else {
962 scmd->sc_data_direction = DMA_NONE;
963 if (cmnd) {
964 BUG_ON(cmnd_size > BLK_MAX_CDB);
965 memcpy(scmd->cmnd, cmnd, cmnd_size);
966 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
970 scmd->underflow = 0;
972 if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
973 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
974 (sdev->lun << 5 & 0xe0);
977 * Zero the sense buffer. The scsi spec mandates that any
978 * untransferred sense data should be interpreted as being zero.
980 memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
982 EXPORT_SYMBOL(scsi_eh_prep_cmnd);
985 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
986 * @scmd: SCSI command structure to restore
987 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
989 * Undo any damage done by above scsi_eh_prep_cmnd().
991 void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
994 * Restore original data
996 scmd->cmd_len = ses->cmd_len;
997 scmd->cmnd = ses->cmnd;
998 scmd->sc_data_direction = ses->data_direction;
999 scmd->sdb = ses->sdb;
1000 scmd->request->next_rq = ses->next_rq;
1001 scmd->result = ses->result;
1002 scmd->underflow = ses->underflow;
1003 scmd->prot_op = ses->prot_op;
1004 scmd->eh_eflags = ses->eh_eflags;
1006 EXPORT_SYMBOL(scsi_eh_restore_cmnd);
1009 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
1010 * @scmd: SCSI command structure to hijack
1011 * @cmnd: CDB to send
1012 * @cmnd_size: size in bytes of @cmnd
1013 * @timeout: timeout for this request
1014 * @sense_bytes: size of sense data to copy or 0
1016 * This function is used to send a scsi command down to a target device
1017 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
1019 * Return value:
1020 * SUCCESS or FAILED or NEEDS_RETRY
1022 static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
1023 int cmnd_size, int timeout, unsigned sense_bytes)
1025 struct scsi_device *sdev = scmd->device;
1026 struct Scsi_Host *shost = sdev->host;
1027 DECLARE_COMPLETION_ONSTACK(done);
1028 unsigned long timeleft = timeout;
1029 struct scsi_eh_save ses;
1030 const unsigned long stall_for = msecs_to_jiffies(100);
1031 int rtn;
1033 retry:
1034 scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
1035 shost->eh_action = &done;
1037 scsi_log_send(scmd);
1038 scmd->scsi_done = scsi_eh_done;
1039 rtn = shost->hostt->queuecommand(shost, scmd);
1040 if (rtn) {
1041 if (timeleft > stall_for) {
1042 scsi_eh_restore_cmnd(scmd, &ses);
1043 timeleft -= stall_for;
1044 msleep(jiffies_to_msecs(stall_for));
1045 goto retry;
1047 /* signal not to enter either branch of the if () below */
1048 timeleft = 0;
1049 rtn = FAILED;
1050 } else {
1051 timeleft = wait_for_completion_timeout(&done, timeout);
1052 rtn = SUCCESS;
1055 shost->eh_action = NULL;
1057 scsi_log_completion(scmd, rtn);
1059 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1060 "%s timeleft: %ld\n",
1061 __func__, timeleft));
1064 * If there is time left scsi_eh_done got called, and we will examine
1065 * the actual status codes to see whether the command actually did
1066 * complete normally, else if we have a zero return and no time left,
1067 * the command must still be pending, so abort it and return FAILED.
1068 * If we never actually managed to issue the command, because
1069 * ->queuecommand() kept returning non zero, use the rtn = FAILED
1070 * value above (so don't execute either branch of the if)
1072 if (timeleft) {
1073 rtn = scsi_eh_completed_normally(scmd);
1074 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1075 "%s: scsi_eh_completed_normally %x\n", __func__, rtn));
1077 switch (rtn) {
1078 case SUCCESS:
1079 case NEEDS_RETRY:
1080 case FAILED:
1081 break;
1082 case ADD_TO_MLQUEUE:
1083 rtn = NEEDS_RETRY;
1084 break;
1085 default:
1086 rtn = FAILED;
1087 break;
1089 } else if (rtn != FAILED) {
1090 scsi_abort_eh_cmnd(scmd);
1091 rtn = FAILED;
1094 scsi_eh_restore_cmnd(scmd, &ses);
1096 return rtn;
1100 * scsi_request_sense - Request sense data from a particular target.
1101 * @scmd: SCSI cmd for request sense.
1103 * Notes:
1104 * Some hosts automatically obtain this information, others require
1105 * that we obtain it on our own. This function will *not* return until
1106 * the command either times out, or it completes.
1108 static int scsi_request_sense(struct scsi_cmnd *scmd)
1110 return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
1113 static int scsi_eh_action(struct scsi_cmnd *scmd, int rtn)
1115 if (!blk_rq_is_passthrough(scmd->request)) {
1116 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
1117 if (sdrv->eh_action)
1118 rtn = sdrv->eh_action(scmd, rtn);
1120 return rtn;
1124 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
1125 * @scmd: Original SCSI cmd that eh has finished.
1126 * @done_q: Queue for processed commands.
1128 * Notes:
1129 * We don't want to use the normal command completion while we are are
1130 * still handling errors - it may cause other commands to be queued,
1131 * and that would disturb what we are doing. Thus we really want to
1132 * keep a list of pending commands for final completion, and once we
1133 * are ready to leave error handling we handle completion for real.
1135 void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
1137 list_move_tail(&scmd->eh_entry, done_q);
1139 EXPORT_SYMBOL(scsi_eh_finish_cmd);
1142 * scsi_eh_get_sense - Get device sense data.
1143 * @work_q: Queue of commands to process.
1144 * @done_q: Queue of processed commands.
1146 * Description:
1147 * See if we need to request sense information. if so, then get it
1148 * now, so we have a better idea of what to do.
1150 * Notes:
1151 * This has the unfortunate side effect that if a shost adapter does
1152 * not automatically request sense information, we end up shutting
1153 * it down before we request it.
1155 * All drivers should request sense information internally these days,
1156 * so for now all I have to say is tough noogies if you end up in here.
1158 * XXX: Long term this code should go away, but that needs an audit of
1159 * all LLDDs first.
1161 int scsi_eh_get_sense(struct list_head *work_q,
1162 struct list_head *done_q)
1164 struct scsi_cmnd *scmd, *next;
1165 struct Scsi_Host *shost;
1166 int rtn;
1169 * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
1170 * should not get sense.
1172 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1173 if ((scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) ||
1174 SCSI_SENSE_VALID(scmd))
1175 continue;
1177 shost = scmd->device->host;
1178 if (scsi_host_eh_past_deadline(shost)) {
1179 SCSI_LOG_ERROR_RECOVERY(3,
1180 scmd_printk(KERN_INFO, scmd,
1181 "%s: skip request sense, past eh deadline\n",
1182 current->comm));
1183 break;
1185 if (status_byte(scmd->result) != CHECK_CONDITION)
1187 * don't request sense if there's no check condition
1188 * status because the error we're processing isn't one
1189 * that has a sense code (and some devices get
1190 * confused by sense requests out of the blue)
1192 continue;
1194 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
1195 "%s: requesting sense\n",
1196 current->comm));
1197 rtn = scsi_request_sense(scmd);
1198 if (rtn != SUCCESS)
1199 continue;
1201 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1202 "sense requested, result %x\n", scmd->result));
1203 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd));
1205 rtn = scsi_decide_disposition(scmd);
1208 * if the result was normal, then just pass it along to the
1209 * upper level.
1211 if (rtn == SUCCESS)
1212 /* we don't want this command reissued, just
1213 * finished with the sense data, so set
1214 * retries to the max allowed to ensure it
1215 * won't get reissued */
1216 scmd->retries = scmd->allowed;
1217 else if (rtn != NEEDS_RETRY)
1218 continue;
1220 scsi_eh_finish_cmd(scmd, done_q);
1223 return list_empty(work_q);
1225 EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
1228 * scsi_eh_tur - Send TUR to device.
1229 * @scmd: &scsi_cmnd to send TUR
1231 * Return value:
1232 * 0 - Device is ready. 1 - Device NOT ready.
1234 static int scsi_eh_tur(struct scsi_cmnd *scmd)
1236 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
1237 int retry_cnt = 1, rtn;
1239 retry_tur:
1240 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
1241 scmd->device->eh_timeout, 0);
1243 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1244 "%s return: %x\n", __func__, rtn));
1246 switch (rtn) {
1247 case NEEDS_RETRY:
1248 if (retry_cnt--)
1249 goto retry_tur;
1250 /*FALLTHRU*/
1251 case SUCCESS:
1252 return 0;
1253 default:
1254 return 1;
1259 * scsi_eh_test_devices - check if devices are responding from error recovery.
1260 * @cmd_list: scsi commands in error recovery.
1261 * @work_q: queue for commands which still need more error recovery
1262 * @done_q: queue for commands which are finished
1263 * @try_stu: boolean on if a STU command should be tried in addition to TUR.
1265 * Decription:
1266 * Tests if devices are in a working state. Commands to devices now in
1267 * a working state are sent to the done_q while commands to devices which
1268 * are still failing to respond are returned to the work_q for more
1269 * processing.
1271 static int scsi_eh_test_devices(struct list_head *cmd_list,
1272 struct list_head *work_q,
1273 struct list_head *done_q, int try_stu)
1275 struct scsi_cmnd *scmd, *next;
1276 struct scsi_device *sdev;
1277 int finish_cmds;
1279 while (!list_empty(cmd_list)) {
1280 scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
1281 sdev = scmd->device;
1283 if (!try_stu) {
1284 if (scsi_host_eh_past_deadline(sdev->host)) {
1285 /* Push items back onto work_q */
1286 list_splice_init(cmd_list, work_q);
1287 SCSI_LOG_ERROR_RECOVERY(3,
1288 sdev_printk(KERN_INFO, sdev,
1289 "%s: skip test device, past eh deadline",
1290 current->comm));
1291 break;
1295 finish_cmds = !scsi_device_online(scmd->device) ||
1296 (try_stu && !scsi_eh_try_stu(scmd) &&
1297 !scsi_eh_tur(scmd)) ||
1298 !scsi_eh_tur(scmd);
1300 list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
1301 if (scmd->device == sdev) {
1302 if (finish_cmds &&
1303 (try_stu ||
1304 scsi_eh_action(scmd, SUCCESS) == SUCCESS))
1305 scsi_eh_finish_cmd(scmd, done_q);
1306 else
1307 list_move_tail(&scmd->eh_entry, work_q);
1310 return list_empty(work_q);
1314 * scsi_eh_try_stu - Send START_UNIT to device.
1315 * @scmd: &scsi_cmnd to send START_UNIT
1317 * Return value:
1318 * 0 - Device is ready. 1 - Device NOT ready.
1320 static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
1322 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
1324 if (scmd->device->allow_restart) {
1325 int i, rtn = NEEDS_RETRY;
1327 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
1328 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->device->request_queue->rq_timeout, 0);
1330 if (rtn == SUCCESS)
1331 return 0;
1334 return 1;
1338 * scsi_eh_stu - send START_UNIT if needed
1339 * @shost: &scsi host being recovered.
1340 * @work_q: &list_head for pending commands.
1341 * @done_q: &list_head for processed commands.
1343 * Notes:
1344 * If commands are failing due to not ready, initializing command required,
1345 * try revalidating the device, which will end up sending a start unit.
1347 static int scsi_eh_stu(struct Scsi_Host *shost,
1348 struct list_head *work_q,
1349 struct list_head *done_q)
1351 struct scsi_cmnd *scmd, *stu_scmd, *next;
1352 struct scsi_device *sdev;
1354 shost_for_each_device(sdev, shost) {
1355 if (scsi_host_eh_past_deadline(shost)) {
1356 SCSI_LOG_ERROR_RECOVERY(3,
1357 sdev_printk(KERN_INFO, sdev,
1358 "%s: skip START_UNIT, past eh deadline\n",
1359 current->comm));
1360 break;
1362 stu_scmd = NULL;
1363 list_for_each_entry(scmd, work_q, eh_entry)
1364 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
1365 scsi_check_sense(scmd) == FAILED ) {
1366 stu_scmd = scmd;
1367 break;
1370 if (!stu_scmd)
1371 continue;
1373 SCSI_LOG_ERROR_RECOVERY(3,
1374 sdev_printk(KERN_INFO, sdev,
1375 "%s: Sending START_UNIT\n",
1376 current->comm));
1378 if (!scsi_eh_try_stu(stu_scmd)) {
1379 if (!scsi_device_online(sdev) ||
1380 !scsi_eh_tur(stu_scmd)) {
1381 list_for_each_entry_safe(scmd, next,
1382 work_q, eh_entry) {
1383 if (scmd->device == sdev &&
1384 scsi_eh_action(scmd, SUCCESS) == SUCCESS)
1385 scsi_eh_finish_cmd(scmd, done_q);
1388 } else {
1389 SCSI_LOG_ERROR_RECOVERY(3,
1390 sdev_printk(KERN_INFO, sdev,
1391 "%s: START_UNIT failed\n",
1392 current->comm));
1396 return list_empty(work_q);
1401 * scsi_eh_bus_device_reset - send bdr if needed
1402 * @shost: scsi host being recovered.
1403 * @work_q: &list_head for pending commands.
1404 * @done_q: &list_head for processed commands.
1406 * Notes:
1407 * Try a bus device reset. Still, look to see whether we have multiple
1408 * devices that are jammed or not - if we have multiple devices, it
1409 * makes no sense to try bus_device_reset - we really would need to try
1410 * a bus_reset instead.
1412 static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1413 struct list_head *work_q,
1414 struct list_head *done_q)
1416 struct scsi_cmnd *scmd, *bdr_scmd, *next;
1417 struct scsi_device *sdev;
1418 int rtn;
1420 shost_for_each_device(sdev, shost) {
1421 if (scsi_host_eh_past_deadline(shost)) {
1422 SCSI_LOG_ERROR_RECOVERY(3,
1423 sdev_printk(KERN_INFO, sdev,
1424 "%s: skip BDR, past eh deadline\n",
1425 current->comm));
1426 break;
1428 bdr_scmd = NULL;
1429 list_for_each_entry(scmd, work_q, eh_entry)
1430 if (scmd->device == sdev) {
1431 bdr_scmd = scmd;
1432 break;
1435 if (!bdr_scmd)
1436 continue;
1438 SCSI_LOG_ERROR_RECOVERY(3,
1439 sdev_printk(KERN_INFO, sdev,
1440 "%s: Sending BDR\n", current->comm));
1441 rtn = scsi_try_bus_device_reset(bdr_scmd);
1442 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
1443 if (!scsi_device_online(sdev) ||
1444 rtn == FAST_IO_FAIL ||
1445 !scsi_eh_tur(bdr_scmd)) {
1446 list_for_each_entry_safe(scmd, next,
1447 work_q, eh_entry) {
1448 if (scmd->device == sdev &&
1449 scsi_eh_action(scmd, rtn) != FAILED)
1450 scsi_eh_finish_cmd(scmd,
1451 done_q);
1454 } else {
1455 SCSI_LOG_ERROR_RECOVERY(3,
1456 sdev_printk(KERN_INFO, sdev,
1457 "%s: BDR failed\n", current->comm));
1461 return list_empty(work_q);
1465 * scsi_eh_target_reset - send target reset if needed
1466 * @shost: scsi host being recovered.
1467 * @work_q: &list_head for pending commands.
1468 * @done_q: &list_head for processed commands.
1470 * Notes:
1471 * Try a target reset.
1473 static int scsi_eh_target_reset(struct Scsi_Host *shost,
1474 struct list_head *work_q,
1475 struct list_head *done_q)
1477 LIST_HEAD(tmp_list);
1478 LIST_HEAD(check_list);
1480 list_splice_init(work_q, &tmp_list);
1482 while (!list_empty(&tmp_list)) {
1483 struct scsi_cmnd *next, *scmd;
1484 int rtn;
1485 unsigned int id;
1487 if (scsi_host_eh_past_deadline(shost)) {
1488 /* push back on work queue for further processing */
1489 list_splice_init(&check_list, work_q);
1490 list_splice_init(&tmp_list, work_q);
1491 SCSI_LOG_ERROR_RECOVERY(3,
1492 shost_printk(KERN_INFO, shost,
1493 "%s: Skip target reset, past eh deadline\n",
1494 current->comm));
1495 return list_empty(work_q);
1498 scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
1499 id = scmd_id(scmd);
1501 SCSI_LOG_ERROR_RECOVERY(3,
1502 shost_printk(KERN_INFO, shost,
1503 "%s: Sending target reset to target %d\n",
1504 current->comm, id));
1505 rtn = scsi_try_target_reset(scmd);
1506 if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
1507 SCSI_LOG_ERROR_RECOVERY(3,
1508 shost_printk(KERN_INFO, shost,
1509 "%s: Target reset failed"
1510 " target: %d\n",
1511 current->comm, id));
1512 list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
1513 if (scmd_id(scmd) != id)
1514 continue;
1516 if (rtn == SUCCESS)
1517 list_move_tail(&scmd->eh_entry, &check_list);
1518 else if (rtn == FAST_IO_FAIL)
1519 scsi_eh_finish_cmd(scmd, done_q);
1520 else
1521 /* push back on work queue for further processing */
1522 list_move(&scmd->eh_entry, work_q);
1526 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1530 * scsi_eh_bus_reset - send a bus reset
1531 * @shost: &scsi host being recovered.
1532 * @work_q: &list_head for pending commands.
1533 * @done_q: &list_head for processed commands.
1535 static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1536 struct list_head *work_q,
1537 struct list_head *done_q)
1539 struct scsi_cmnd *scmd, *chan_scmd, *next;
1540 LIST_HEAD(check_list);
1541 unsigned int channel;
1542 int rtn;
1545 * we really want to loop over the various channels, and do this on
1546 * a channel by channel basis. we should also check to see if any
1547 * of the failed commands are on soft_reset devices, and if so, skip
1548 * the reset.
1551 for (channel = 0; channel <= shost->max_channel; channel++) {
1552 if (scsi_host_eh_past_deadline(shost)) {
1553 list_splice_init(&check_list, work_q);
1554 SCSI_LOG_ERROR_RECOVERY(3,
1555 shost_printk(KERN_INFO, shost,
1556 "%s: skip BRST, past eh deadline\n",
1557 current->comm));
1558 return list_empty(work_q);
1561 chan_scmd = NULL;
1562 list_for_each_entry(scmd, work_q, eh_entry) {
1563 if (channel == scmd_channel(scmd)) {
1564 chan_scmd = scmd;
1565 break;
1567 * FIXME add back in some support for
1568 * soft_reset devices.
1573 if (!chan_scmd)
1574 continue;
1575 SCSI_LOG_ERROR_RECOVERY(3,
1576 shost_printk(KERN_INFO, shost,
1577 "%s: Sending BRST chan: %d\n",
1578 current->comm, channel));
1579 rtn = scsi_try_bus_reset(chan_scmd);
1580 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
1581 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1582 if (channel == scmd_channel(scmd)) {
1583 if (rtn == FAST_IO_FAIL)
1584 scsi_eh_finish_cmd(scmd,
1585 done_q);
1586 else
1587 list_move_tail(&scmd->eh_entry,
1588 &check_list);
1591 } else {
1592 SCSI_LOG_ERROR_RECOVERY(3,
1593 shost_printk(KERN_INFO, shost,
1594 "%s: BRST failed chan: %d\n",
1595 current->comm, channel));
1598 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1602 * scsi_eh_host_reset - send a host reset
1603 * @shost: host to be reset.
1604 * @work_q: &list_head for pending commands.
1605 * @done_q: &list_head for processed commands.
1607 static int scsi_eh_host_reset(struct Scsi_Host *shost,
1608 struct list_head *work_q,
1609 struct list_head *done_q)
1611 struct scsi_cmnd *scmd, *next;
1612 LIST_HEAD(check_list);
1613 int rtn;
1615 if (!list_empty(work_q)) {
1616 scmd = list_entry(work_q->next,
1617 struct scsi_cmnd, eh_entry);
1619 SCSI_LOG_ERROR_RECOVERY(3,
1620 shost_printk(KERN_INFO, shost,
1621 "%s: Sending HRST\n",
1622 current->comm));
1624 rtn = scsi_try_host_reset(scmd);
1625 if (rtn == SUCCESS) {
1626 list_splice_init(work_q, &check_list);
1627 } else if (rtn == FAST_IO_FAIL) {
1628 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1629 scsi_eh_finish_cmd(scmd, done_q);
1631 } else {
1632 SCSI_LOG_ERROR_RECOVERY(3,
1633 shost_printk(KERN_INFO, shost,
1634 "%s: HRST failed\n",
1635 current->comm));
1638 return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
1642 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
1643 * @work_q: &list_head for pending commands.
1644 * @done_q: &list_head for processed commands.
1646 static void scsi_eh_offline_sdevs(struct list_head *work_q,
1647 struct list_head *done_q)
1649 struct scsi_cmnd *scmd, *next;
1650 struct scsi_device *sdev;
1652 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1653 sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
1654 "not ready after error recovery\n");
1655 sdev = scmd->device;
1657 mutex_lock(&sdev->state_mutex);
1658 scsi_device_set_state(sdev, SDEV_OFFLINE);
1659 mutex_unlock(&sdev->state_mutex);
1661 scsi_eh_finish_cmd(scmd, done_q);
1663 return;
1667 * scsi_noretry_cmd - determine if command should be failed fast
1668 * @scmd: SCSI cmd to examine.
1670 int scsi_noretry_cmd(struct scsi_cmnd *scmd)
1672 switch (host_byte(scmd->result)) {
1673 case DID_OK:
1674 break;
1675 case DID_TIME_OUT:
1676 goto check_type;
1677 case DID_BUS_BUSY:
1678 return (scmd->request->cmd_flags & REQ_FAILFAST_TRANSPORT);
1679 case DID_PARITY:
1680 return (scmd->request->cmd_flags & REQ_FAILFAST_DEV);
1681 case DID_ERROR:
1682 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1683 status_byte(scmd->result) == RESERVATION_CONFLICT)
1684 return 0;
1685 /* fall through */
1686 case DID_SOFT_ERROR:
1687 return (scmd->request->cmd_flags & REQ_FAILFAST_DRIVER);
1690 if (status_byte(scmd->result) != CHECK_CONDITION)
1691 return 0;
1693 check_type:
1695 * assume caller has checked sense and determined
1696 * the check condition was retryable.
1698 if (scmd->request->cmd_flags & REQ_FAILFAST_DEV ||
1699 blk_rq_is_passthrough(scmd->request))
1700 return 1;
1701 else
1702 return 0;
1706 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1707 * @scmd: SCSI cmd to examine.
1709 * Notes:
1710 * This is *only* called when we are examining the status after sending
1711 * out the actual data command. any commands that are queued for error
1712 * recovery (e.g. test_unit_ready) do *not* come through here.
1714 * When this routine returns failed, it means the error handler thread
1715 * is woken. In cases where the error code indicates an error that
1716 * doesn't require the error handler read (i.e. we don't need to
1717 * abort/reset), this function should return SUCCESS.
1719 int scsi_decide_disposition(struct scsi_cmnd *scmd)
1721 int rtn;
1724 * if the device is offline, then we clearly just pass the result back
1725 * up to the top level.
1727 if (!scsi_device_online(scmd->device)) {
1728 SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO, scmd,
1729 "%s: device offline - report as SUCCESS\n", __func__));
1730 return SUCCESS;
1734 * first check the host byte, to see if there is anything in there
1735 * that would indicate what we need to do.
1737 switch (host_byte(scmd->result)) {
1738 case DID_PASSTHROUGH:
1740 * no matter what, pass this through to the upper layer.
1741 * nuke this special code so that it looks like we are saying
1742 * did_ok.
1744 scmd->result &= 0xff00ffff;
1745 return SUCCESS;
1746 case DID_OK:
1748 * looks good. drop through, and check the next byte.
1750 break;
1751 case DID_ABORT:
1752 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
1753 set_host_byte(scmd, DID_TIME_OUT);
1754 return SUCCESS;
1756 /* FALLTHROUGH */
1757 case DID_NO_CONNECT:
1758 case DID_BAD_TARGET:
1760 * note - this means that we just report the status back
1761 * to the top level driver, not that we actually think
1762 * that it indicates SUCCESS.
1764 return SUCCESS;
1766 * when the low level driver returns did_soft_error,
1767 * it is responsible for keeping an internal retry counter
1768 * in order to avoid endless loops (db)
1770 * actually this is a bug in this function here. we should
1771 * be mindful of the maximum number of retries specified
1772 * and not get stuck in a loop.
1774 case DID_SOFT_ERROR:
1775 goto maybe_retry;
1776 case DID_IMM_RETRY:
1777 return NEEDS_RETRY;
1779 case DID_REQUEUE:
1780 return ADD_TO_MLQUEUE;
1781 case DID_TRANSPORT_DISRUPTED:
1783 * LLD/transport was disrupted during processing of the IO.
1784 * The transport class is now blocked/blocking,
1785 * and the transport will decide what to do with the IO
1786 * based on its timers and recovery capablilities if
1787 * there are enough retries.
1789 goto maybe_retry;
1790 case DID_TRANSPORT_FAILFAST:
1792 * The transport decided to failfast the IO (most likely
1793 * the fast io fail tmo fired), so send IO directly upwards.
1795 return SUCCESS;
1796 case DID_ERROR:
1797 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1798 status_byte(scmd->result) == RESERVATION_CONFLICT)
1800 * execute reservation conflict processing code
1801 * lower down
1803 break;
1804 /* fallthrough */
1805 case DID_BUS_BUSY:
1806 case DID_PARITY:
1807 goto maybe_retry;
1808 case DID_TIME_OUT:
1810 * when we scan the bus, we get timeout messages for
1811 * these commands if there is no device available.
1812 * other hosts report did_no_connect for the same thing.
1814 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1815 scmd->cmnd[0] == INQUIRY)) {
1816 return SUCCESS;
1817 } else {
1818 return FAILED;
1820 case DID_RESET:
1821 return SUCCESS;
1822 default:
1823 return FAILED;
1827 * next, check the message byte.
1829 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
1830 return FAILED;
1833 * check the status byte to see if this indicates anything special.
1835 switch (status_byte(scmd->result)) {
1836 case QUEUE_FULL:
1837 scsi_handle_queue_full(scmd->device);
1839 * the case of trying to send too many commands to a
1840 * tagged queueing device.
1842 /* FALLTHROUGH */
1843 case BUSY:
1845 * device can't talk to us at the moment. Should only
1846 * occur (SAM-3) when the task queue is empty, so will cause
1847 * the empty queue handling to trigger a stall in the
1848 * device.
1850 return ADD_TO_MLQUEUE;
1851 case GOOD:
1852 if (scmd->cmnd[0] == REPORT_LUNS)
1853 scmd->device->sdev_target->expecting_lun_change = 0;
1854 scsi_handle_queue_ramp_up(scmd->device);
1855 /* FALLTHROUGH */
1856 case COMMAND_TERMINATED:
1857 return SUCCESS;
1858 case TASK_ABORTED:
1859 goto maybe_retry;
1860 case CHECK_CONDITION:
1861 rtn = scsi_check_sense(scmd);
1862 if (rtn == NEEDS_RETRY)
1863 goto maybe_retry;
1864 /* if rtn == FAILED, we have no sense information;
1865 * returning FAILED will wake the error handler thread
1866 * to collect the sense and redo the decide
1867 * disposition */
1868 return rtn;
1869 case CONDITION_GOOD:
1870 case INTERMEDIATE_GOOD:
1871 case INTERMEDIATE_C_GOOD:
1872 case ACA_ACTIVE:
1874 * who knows? FIXME(eric)
1876 return SUCCESS;
1878 case RESERVATION_CONFLICT:
1879 sdev_printk(KERN_INFO, scmd->device,
1880 "reservation conflict\n");
1881 set_host_byte(scmd, DID_NEXUS_FAILURE);
1882 return SUCCESS; /* causes immediate i/o error */
1883 default:
1884 return FAILED;
1886 return FAILED;
1888 maybe_retry:
1890 /* we requeue for retry because the error was retryable, and
1891 * the request was not marked fast fail. Note that above,
1892 * even if the request is marked fast fail, we still requeue
1893 * for queue congestion conditions (QUEUE_FULL or BUSY) */
1894 if ((++scmd->retries) <= scmd->allowed
1895 && !scsi_noretry_cmd(scmd)) {
1896 return NEEDS_RETRY;
1897 } else {
1899 * no more retries - report this one back to upper level.
1901 return SUCCESS;
1905 static void eh_lock_door_done(struct request *req, blk_status_t status)
1907 __blk_put_request(req->q, req);
1911 * scsi_eh_lock_door - Prevent medium removal for the specified device
1912 * @sdev: SCSI device to prevent medium removal
1914 * Locking:
1915 * We must be called from process context.
1917 * Notes:
1918 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1919 * head of the devices request queue, and continue.
1921 static void scsi_eh_lock_door(struct scsi_device *sdev)
1923 struct request *req;
1924 struct scsi_request *rq;
1927 * blk_get_request with GFP_KERNEL (__GFP_RECLAIM) sleeps until a
1928 * request becomes available
1930 req = blk_get_request(sdev->request_queue, REQ_OP_SCSI_IN, GFP_KERNEL);
1931 if (IS_ERR(req))
1932 return;
1933 rq = scsi_req(req);
1935 rq->cmd[0] = ALLOW_MEDIUM_REMOVAL;
1936 rq->cmd[1] = 0;
1937 rq->cmd[2] = 0;
1938 rq->cmd[3] = 0;
1939 rq->cmd[4] = SCSI_REMOVAL_PREVENT;
1940 rq->cmd[5] = 0;
1941 rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
1943 req->rq_flags |= RQF_QUIET;
1944 req->timeout = 10 * HZ;
1945 rq->retries = 5;
1947 blk_execute_rq_nowait(req->q, NULL, req, 1, eh_lock_door_done);
1951 * scsi_restart_operations - restart io operations to the specified host.
1952 * @shost: Host we are restarting.
1954 * Notes:
1955 * When we entered the error handler, we blocked all further i/o to
1956 * this device. we need to 'reverse' this process.
1958 static void scsi_restart_operations(struct Scsi_Host *shost)
1960 struct scsi_device *sdev;
1961 unsigned long flags;
1964 * If the door was locked, we need to insert a door lock request
1965 * onto the head of the SCSI request queue for the device. There
1966 * is no point trying to lock the door of an off-line device.
1968 shost_for_each_device(sdev, shost) {
1969 if (scsi_device_online(sdev) && sdev->was_reset && sdev->locked) {
1970 scsi_eh_lock_door(sdev);
1971 sdev->was_reset = 0;
1976 * next free up anything directly waiting upon the host. this
1977 * will be requests for character device operations, and also for
1978 * ioctls to queued block devices.
1980 SCSI_LOG_ERROR_RECOVERY(3,
1981 shost_printk(KERN_INFO, shost, "waking up host to restart\n"));
1983 spin_lock_irqsave(shost->host_lock, flags);
1984 if (scsi_host_set_state(shost, SHOST_RUNNING))
1985 if (scsi_host_set_state(shost, SHOST_CANCEL))
1986 BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
1987 spin_unlock_irqrestore(shost->host_lock, flags);
1989 wake_up(&shost->host_wait);
1992 * finally we need to re-initiate requests that may be pending. we will
1993 * have had everything blocked while error handling is taking place, and
1994 * now that error recovery is done, we will need to ensure that these
1995 * requests are started.
1997 scsi_run_host_queues(shost);
2000 * if eh is active and host_eh_scheduled is pending we need to re-run
2001 * recovery. we do this check after scsi_run_host_queues() to allow
2002 * everything pent up since the last eh run a chance to make forward
2003 * progress before we sync again. Either we'll immediately re-run
2004 * recovery or scsi_device_unbusy() will wake us again when these
2005 * pending commands complete.
2007 spin_lock_irqsave(shost->host_lock, flags);
2008 if (shost->host_eh_scheduled)
2009 if (scsi_host_set_state(shost, SHOST_RECOVERY))
2010 WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY));
2011 spin_unlock_irqrestore(shost->host_lock, flags);
2015 * scsi_eh_ready_devs - check device ready state and recover if not.
2016 * @shost: host to be recovered.
2017 * @work_q: &list_head for pending commands.
2018 * @done_q: &list_head for processed commands.
2020 void scsi_eh_ready_devs(struct Scsi_Host *shost,
2021 struct list_head *work_q,
2022 struct list_head *done_q)
2024 if (!scsi_eh_stu(shost, work_q, done_q))
2025 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
2026 if (!scsi_eh_target_reset(shost, work_q, done_q))
2027 if (!scsi_eh_bus_reset(shost, work_q, done_q))
2028 if (!scsi_eh_host_reset(shost, work_q, done_q))
2029 scsi_eh_offline_sdevs(work_q,
2030 done_q);
2032 EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
2035 * scsi_eh_flush_done_q - finish processed commands or retry them.
2036 * @done_q: list_head of processed commands.
2038 void scsi_eh_flush_done_q(struct list_head *done_q)
2040 struct scsi_cmnd *scmd, *next;
2042 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
2043 list_del_init(&scmd->eh_entry);
2044 if (scsi_device_online(scmd->device) &&
2045 !scsi_noretry_cmd(scmd) &&
2046 (++scmd->retries <= scmd->allowed)) {
2047 SCSI_LOG_ERROR_RECOVERY(3,
2048 scmd_printk(KERN_INFO, scmd,
2049 "%s: flush retry cmd\n",
2050 current->comm));
2051 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
2052 } else {
2054 * If just we got sense for the device (called
2055 * scsi_eh_get_sense), scmd->result is already
2056 * set, do not set DRIVER_TIMEOUT.
2058 if (!scmd->result)
2059 scmd->result |= (DRIVER_TIMEOUT << 24);
2060 SCSI_LOG_ERROR_RECOVERY(3,
2061 scmd_printk(KERN_INFO, scmd,
2062 "%s: flush finish cmd\n",
2063 current->comm));
2064 scsi_finish_command(scmd);
2068 EXPORT_SYMBOL(scsi_eh_flush_done_q);
2071 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
2072 * @shost: Host to unjam.
2074 * Notes:
2075 * When we come in here, we *know* that all commands on the bus have
2076 * either completed, failed or timed out. we also know that no further
2077 * commands are being sent to the host, so things are relatively quiet
2078 * and we have freedom to fiddle with things as we wish.
2080 * This is only the *default* implementation. it is possible for
2081 * individual drivers to supply their own version of this function, and
2082 * if the maintainer wishes to do this, it is strongly suggested that
2083 * this function be taken as a template and modified. this function
2084 * was designed to correctly handle problems for about 95% of the
2085 * different cases out there, and it should always provide at least a
2086 * reasonable amount of error recovery.
2088 * Any command marked 'failed' or 'timeout' must eventually have
2089 * scsi_finish_cmd() called for it. we do all of the retry stuff
2090 * here, so when we restart the host after we return it should have an
2091 * empty queue.
2093 static void scsi_unjam_host(struct Scsi_Host *shost)
2095 unsigned long flags;
2096 LIST_HEAD(eh_work_q);
2097 LIST_HEAD(eh_done_q);
2099 spin_lock_irqsave(shost->host_lock, flags);
2100 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
2101 spin_unlock_irqrestore(shost->host_lock, flags);
2103 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
2105 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
2106 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
2108 spin_lock_irqsave(shost->host_lock, flags);
2109 if (shost->eh_deadline != -1)
2110 shost->last_reset = 0;
2111 spin_unlock_irqrestore(shost->host_lock, flags);
2112 scsi_eh_flush_done_q(&eh_done_q);
2116 * scsi_error_handler - SCSI error handler thread
2117 * @data: Host for which we are running.
2119 * Notes:
2120 * This is the main error handling loop. This is run as a kernel thread
2121 * for every SCSI host and handles all error handling activity.
2123 int scsi_error_handler(void *data)
2125 struct Scsi_Host *shost = data;
2128 * We use TASK_INTERRUPTIBLE so that the thread is not
2129 * counted against the load average as a running process.
2130 * We never actually get interrupted because kthread_run
2131 * disables signal delivery for the created thread.
2133 while (true) {
2135 * The sequence in kthread_stop() sets the stop flag first
2136 * then wakes the process. To avoid missed wakeups, the task
2137 * should always be in a non running state before the stop
2138 * flag is checked
2140 set_current_state(TASK_INTERRUPTIBLE);
2141 if (kthread_should_stop())
2142 break;
2144 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
2145 shost->host_failed != atomic_read(&shost->host_busy)) {
2146 SCSI_LOG_ERROR_RECOVERY(1,
2147 shost_printk(KERN_INFO, shost,
2148 "scsi_eh_%d: sleeping\n",
2149 shost->host_no));
2150 schedule();
2151 continue;
2154 __set_current_state(TASK_RUNNING);
2155 SCSI_LOG_ERROR_RECOVERY(1,
2156 shost_printk(KERN_INFO, shost,
2157 "scsi_eh_%d: waking up %d/%d/%d\n",
2158 shost->host_no, shost->host_eh_scheduled,
2159 shost->host_failed,
2160 atomic_read(&shost->host_busy)));
2163 * We have a host that is failing for some reason. Figure out
2164 * what we need to do to get it up and online again (if we can).
2165 * If we fail, we end up taking the thing offline.
2167 if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
2168 SCSI_LOG_ERROR_RECOVERY(1,
2169 shost_printk(KERN_ERR, shost,
2170 "scsi_eh_%d: unable to autoresume\n",
2171 shost->host_no));
2172 continue;
2175 if (shost->transportt->eh_strategy_handler)
2176 shost->transportt->eh_strategy_handler(shost);
2177 else
2178 scsi_unjam_host(shost);
2180 /* All scmds have been handled */
2181 shost->host_failed = 0;
2184 * Note - if the above fails completely, the action is to take
2185 * individual devices offline and flush the queue of any
2186 * outstanding requests that may have been pending. When we
2187 * restart, we restart any I/O to any other devices on the bus
2188 * which are still online.
2190 scsi_restart_operations(shost);
2191 if (!shost->eh_noresume)
2192 scsi_autopm_put_host(shost);
2194 __set_current_state(TASK_RUNNING);
2196 SCSI_LOG_ERROR_RECOVERY(1,
2197 shost_printk(KERN_INFO, shost,
2198 "Error handler scsi_eh_%d exiting\n",
2199 shost->host_no));
2200 shost->ehandler = NULL;
2201 return 0;
2205 * Function: scsi_report_bus_reset()
2207 * Purpose: Utility function used by low-level drivers to report that
2208 * they have observed a bus reset on the bus being handled.
2210 * Arguments: shost - Host in question
2211 * channel - channel on which reset was observed.
2213 * Returns: Nothing
2215 * Lock status: Host lock must be held.
2217 * Notes: This only needs to be called if the reset is one which
2218 * originates from an unknown location. Resets originated
2219 * by the mid-level itself don't need to call this, but there
2220 * should be no harm.
2222 * The main purpose of this is to make sure that a CHECK_CONDITION
2223 * is properly treated.
2225 void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
2227 struct scsi_device *sdev;
2229 __shost_for_each_device(sdev, shost) {
2230 if (channel == sdev_channel(sdev))
2231 __scsi_report_device_reset(sdev, NULL);
2234 EXPORT_SYMBOL(scsi_report_bus_reset);
2237 * Function: scsi_report_device_reset()
2239 * Purpose: Utility function used by low-level drivers to report that
2240 * they have observed a device reset on the device being handled.
2242 * Arguments: shost - Host in question
2243 * channel - channel on which reset was observed
2244 * target - target on which reset was observed
2246 * Returns: Nothing
2248 * Lock status: Host lock must be held
2250 * Notes: This only needs to be called if the reset is one which
2251 * originates from an unknown location. Resets originated
2252 * by the mid-level itself don't need to call this, but there
2253 * should be no harm.
2255 * The main purpose of this is to make sure that a CHECK_CONDITION
2256 * is properly treated.
2258 void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
2260 struct scsi_device *sdev;
2262 __shost_for_each_device(sdev, shost) {
2263 if (channel == sdev_channel(sdev) &&
2264 target == sdev_id(sdev))
2265 __scsi_report_device_reset(sdev, NULL);
2268 EXPORT_SYMBOL(scsi_report_device_reset);
2270 static void
2271 scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
2276 * scsi_ioctl_reset: explicitly reset a host/bus/target/device
2277 * @dev: scsi_device to operate on
2278 * @arg: reset type (see sg.h)
2281 scsi_ioctl_reset(struct scsi_device *dev, int __user *arg)
2283 struct scsi_cmnd *scmd;
2284 struct Scsi_Host *shost = dev->host;
2285 struct request *rq;
2286 unsigned long flags;
2287 int error = 0, rtn, val;
2289 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2290 return -EACCES;
2292 error = get_user(val, arg);
2293 if (error)
2294 return error;
2296 if (scsi_autopm_get_host(shost) < 0)
2297 return -EIO;
2299 error = -EIO;
2300 rq = kzalloc(sizeof(struct request) + sizeof(struct scsi_cmnd) +
2301 shost->hostt->cmd_size, GFP_KERNEL);
2302 if (!rq)
2303 goto out_put_autopm_host;
2304 blk_rq_init(NULL, rq);
2306 scmd = (struct scsi_cmnd *)(rq + 1);
2307 scsi_init_command(dev, scmd);
2308 scmd->request = rq;
2309 scmd->cmnd = scsi_req(rq)->cmd;
2311 scmd->scsi_done = scsi_reset_provider_done_command;
2312 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
2314 scmd->cmd_len = 0;
2316 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
2318 spin_lock_irqsave(shost->host_lock, flags);
2319 shost->tmf_in_progress = 1;
2320 spin_unlock_irqrestore(shost->host_lock, flags);
2322 switch (val & ~SG_SCSI_RESET_NO_ESCALATE) {
2323 case SG_SCSI_RESET_NOTHING:
2324 rtn = SUCCESS;
2325 break;
2326 case SG_SCSI_RESET_DEVICE:
2327 rtn = scsi_try_bus_device_reset(scmd);
2328 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
2329 break;
2330 /* FALLTHROUGH */
2331 case SG_SCSI_RESET_TARGET:
2332 rtn = scsi_try_target_reset(scmd);
2333 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
2334 break;
2335 /* FALLTHROUGH */
2336 case SG_SCSI_RESET_BUS:
2337 rtn = scsi_try_bus_reset(scmd);
2338 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
2339 break;
2340 /* FALLTHROUGH */
2341 case SG_SCSI_RESET_HOST:
2342 rtn = scsi_try_host_reset(scmd);
2343 if (rtn == SUCCESS)
2344 break;
2345 /* FALLTHROUGH */
2346 default:
2347 rtn = FAILED;
2348 break;
2351 error = (rtn == SUCCESS) ? 0 : -EIO;
2353 spin_lock_irqsave(shost->host_lock, flags);
2354 shost->tmf_in_progress = 0;
2355 spin_unlock_irqrestore(shost->host_lock, flags);
2358 * be sure to wake up anyone who was sleeping or had their queue
2359 * suspended while we performed the TMF.
2361 SCSI_LOG_ERROR_RECOVERY(3,
2362 shost_printk(KERN_INFO, shost,
2363 "waking up host to restart after TMF\n"));
2365 wake_up(&shost->host_wait);
2366 scsi_run_host_queues(shost);
2368 scsi_put_command(scmd);
2369 kfree(rq);
2371 out_put_autopm_host:
2372 scsi_autopm_put_host(shost);
2373 return error;
2375 EXPORT_SYMBOL(scsi_ioctl_reset);
2377 bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd,
2378 struct scsi_sense_hdr *sshdr)
2380 return scsi_normalize_sense(cmd->sense_buffer,
2381 SCSI_SENSE_BUFFERSIZE, sshdr);
2383 EXPORT_SYMBOL(scsi_command_normalize_sense);
2386 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
2387 * @sense_buffer: byte array of sense data
2388 * @sb_len: number of valid bytes in sense_buffer
2389 * @info_out: pointer to 64 integer where 8 or 4 byte information
2390 * field will be placed if found.
2392 * Return value:
2393 * true if information field found, false if not found.
2395 bool scsi_get_sense_info_fld(const u8 *sense_buffer, int sb_len,
2396 u64 *info_out)
2398 const u8 * ucp;
2400 if (sb_len < 7)
2401 return false;
2402 switch (sense_buffer[0] & 0x7f) {
2403 case 0x70:
2404 case 0x71:
2405 if (sense_buffer[0] & 0x80) {
2406 *info_out = get_unaligned_be32(&sense_buffer[3]);
2407 return true;
2409 return false;
2410 case 0x72:
2411 case 0x73:
2412 ucp = scsi_sense_desc_find(sense_buffer, sb_len,
2413 0 /* info desc */);
2414 if (ucp && (0xa == ucp[1])) {
2415 *info_out = get_unaligned_be64(&ucp[4]);
2416 return true;
2418 return false;
2419 default:
2420 return false;
2423 EXPORT_SYMBOL(scsi_get_sense_info_fld);