Linux 5.1.15
[linux/fpc-iii.git] / drivers / scsi / scsi_error.c
blob1b8378f361399cff8e196943430d9f10702caefd
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/scsi_devinfo.h>
42 #include <scsi/sg.h>
44 #include "scsi_priv.h"
45 #include "scsi_logging.h"
46 #include "scsi_transport_api.h"
48 #include <trace/events/scsi.h>
50 #include <asm/unaligned.h>
52 static void scsi_eh_done(struct scsi_cmnd *scmd);
55 * These should *probably* be handled by the host itself.
56 * Since it is allowed to sleep, it probably should.
58 #define BUS_RESET_SETTLE_TIME (10)
59 #define HOST_RESET_SETTLE_TIME (10)
61 static int scsi_eh_try_stu(struct scsi_cmnd *scmd);
62 static int scsi_try_to_abort_cmd(struct scsi_host_template *,
63 struct scsi_cmnd *);
65 void scsi_eh_wakeup(struct Scsi_Host *shost)
67 lockdep_assert_held(shost->host_lock);
69 if (scsi_host_busy(shost) == shost->host_failed) {
70 trace_scsi_eh_wakeup(shost);
71 wake_up_process(shost->ehandler);
72 SCSI_LOG_ERROR_RECOVERY(5, shost_printk(KERN_INFO, shost,
73 "Waking error handler thread\n"));
77 /**
78 * scsi_schedule_eh - schedule EH for SCSI host
79 * @shost: SCSI host to invoke error handling on.
81 * Schedule SCSI EH without scmd.
83 void scsi_schedule_eh(struct Scsi_Host *shost)
85 unsigned long flags;
87 spin_lock_irqsave(shost->host_lock, flags);
89 if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
90 scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
91 shost->host_eh_scheduled++;
92 scsi_eh_wakeup(shost);
95 spin_unlock_irqrestore(shost->host_lock, flags);
97 EXPORT_SYMBOL_GPL(scsi_schedule_eh);
99 static int scsi_host_eh_past_deadline(struct Scsi_Host *shost)
101 if (!shost->last_reset || shost->eh_deadline == -1)
102 return 0;
105 * 32bit accesses are guaranteed to be atomic
106 * (on all supported architectures), so instead
107 * of using a spinlock we can as well double check
108 * if eh_deadline has been set to 'off' during the
109 * time_before call.
111 if (time_before(jiffies, shost->last_reset + shost->eh_deadline) &&
112 shost->eh_deadline > -1)
113 return 0;
115 return 1;
119 * scmd_eh_abort_handler - Handle command aborts
120 * @work: command to be aborted.
122 * Note: this function must be called only for a command that has timed out.
123 * Because the block layer marks a request as complete before it calls
124 * scsi_times_out(), a .scsi_done() call from the LLD for a command that has
125 * timed out do not have any effect. Hence it is safe to call
126 * scsi_finish_command() from this function.
128 void
129 scmd_eh_abort_handler(struct work_struct *work)
131 struct scsi_cmnd *scmd =
132 container_of(work, struct scsi_cmnd, abort_work.work);
133 struct scsi_device *sdev = scmd->device;
134 int rtn;
136 if (scsi_host_eh_past_deadline(sdev->host)) {
137 SCSI_LOG_ERROR_RECOVERY(3,
138 scmd_printk(KERN_INFO, scmd,
139 "eh timeout, not aborting\n"));
140 } else {
141 SCSI_LOG_ERROR_RECOVERY(3,
142 scmd_printk(KERN_INFO, scmd,
143 "aborting command\n"));
144 rtn = scsi_try_to_abort_cmd(sdev->host->hostt, scmd);
145 if (rtn == SUCCESS) {
146 set_host_byte(scmd, DID_TIME_OUT);
147 if (scsi_host_eh_past_deadline(sdev->host)) {
148 SCSI_LOG_ERROR_RECOVERY(3,
149 scmd_printk(KERN_INFO, scmd,
150 "eh timeout, not retrying "
151 "aborted command\n"));
152 } else if (!scsi_noretry_cmd(scmd) &&
153 (++scmd->retries <= scmd->allowed)) {
154 SCSI_LOG_ERROR_RECOVERY(3,
155 scmd_printk(KERN_WARNING, scmd,
156 "retry aborted command\n"));
157 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
158 return;
159 } else {
160 SCSI_LOG_ERROR_RECOVERY(3,
161 scmd_printk(KERN_WARNING, scmd,
162 "finish aborted command\n"));
163 scsi_finish_command(scmd);
164 return;
166 } else {
167 SCSI_LOG_ERROR_RECOVERY(3,
168 scmd_printk(KERN_INFO, scmd,
169 "cmd abort %s\n",
170 (rtn == FAST_IO_FAIL) ?
171 "not send" : "failed"));
175 scsi_eh_scmd_add(scmd);
179 * scsi_abort_command - schedule a command abort
180 * @scmd: scmd to abort.
182 * We only need to abort commands after a command timeout
184 static int
185 scsi_abort_command(struct scsi_cmnd *scmd)
187 struct scsi_device *sdev = scmd->device;
188 struct Scsi_Host *shost = sdev->host;
189 unsigned long flags;
191 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
193 * Retry after abort failed, escalate to next level.
195 SCSI_LOG_ERROR_RECOVERY(3,
196 scmd_printk(KERN_INFO, scmd,
197 "previous abort failed\n"));
198 BUG_ON(delayed_work_pending(&scmd->abort_work));
199 return FAILED;
202 spin_lock_irqsave(shost->host_lock, flags);
203 if (shost->eh_deadline != -1 && !shost->last_reset)
204 shost->last_reset = jiffies;
205 spin_unlock_irqrestore(shost->host_lock, flags);
207 scmd->eh_eflags |= SCSI_EH_ABORT_SCHEDULED;
208 SCSI_LOG_ERROR_RECOVERY(3,
209 scmd_printk(KERN_INFO, scmd, "abort scheduled\n"));
210 queue_delayed_work(shost->tmf_work_q, &scmd->abort_work, HZ / 100);
211 return SUCCESS;
215 * scsi_eh_reset - call into ->eh_action to reset internal counters
216 * @scmd: scmd to run eh on.
218 * The scsi driver might be carrying internal state about the
219 * devices, so we need to call into the driver to reset the
220 * internal state once the error handler is started.
222 static void scsi_eh_reset(struct scsi_cmnd *scmd)
224 if (!blk_rq_is_passthrough(scmd->request)) {
225 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
226 if (sdrv->eh_reset)
227 sdrv->eh_reset(scmd);
231 static void scsi_eh_inc_host_failed(struct rcu_head *head)
233 struct scsi_cmnd *scmd = container_of(head, typeof(*scmd), rcu);
234 struct Scsi_Host *shost = scmd->device->host;
235 unsigned long flags;
237 spin_lock_irqsave(shost->host_lock, flags);
238 shost->host_failed++;
239 scsi_eh_wakeup(shost);
240 spin_unlock_irqrestore(shost->host_lock, flags);
244 * scsi_eh_scmd_add - add scsi cmd to error handling.
245 * @scmd: scmd to run eh on.
247 void scsi_eh_scmd_add(struct scsi_cmnd *scmd)
249 struct Scsi_Host *shost = scmd->device->host;
250 unsigned long flags;
251 int ret;
253 WARN_ON_ONCE(!shost->ehandler);
255 spin_lock_irqsave(shost->host_lock, flags);
256 if (scsi_host_set_state(shost, SHOST_RECOVERY)) {
257 ret = scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY);
258 WARN_ON_ONCE(ret);
260 if (shost->eh_deadline != -1 && !shost->last_reset)
261 shost->last_reset = jiffies;
263 scsi_eh_reset(scmd);
264 list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
265 spin_unlock_irqrestore(shost->host_lock, flags);
267 * Ensure that all tasks observe the host state change before the
268 * host_failed change.
270 call_rcu(&scmd->rcu, scsi_eh_inc_host_failed);
274 * scsi_times_out - Timeout function for normal scsi commands.
275 * @req: request that is timing out.
277 * Notes:
278 * We do not need to lock this. There is the potential for a race
279 * only in that the normal completion handling might run, but if the
280 * normal completion function determines that the timer has already
281 * fired, then it mustn't do anything.
283 enum blk_eh_timer_return scsi_times_out(struct request *req)
285 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
286 enum blk_eh_timer_return rtn = BLK_EH_DONE;
287 struct Scsi_Host *host = scmd->device->host;
289 trace_scsi_dispatch_cmd_timeout(scmd);
290 scsi_log_completion(scmd, TIMEOUT_ERROR);
292 if (host->eh_deadline != -1 && !host->last_reset)
293 host->last_reset = jiffies;
295 if (host->hostt->eh_timed_out)
296 rtn = host->hostt->eh_timed_out(scmd);
298 if (rtn == BLK_EH_DONE) {
300 * Set the command to complete first in order to prevent a real
301 * completion from releasing the command while error handling
302 * is using it. If the command was already completed, then the
303 * lower level driver beat the timeout handler, and it is safe
304 * to return without escalating error recovery.
306 * If timeout handling lost the race to a real completion, the
307 * block layer may ignore that due to a fake timeout injection,
308 * so return RESET_TIMER to allow error handling another shot
309 * at this command.
311 if (test_and_set_bit(SCMD_STATE_COMPLETE, &scmd->state))
312 return BLK_EH_RESET_TIMER;
313 if (scsi_abort_command(scmd) != SUCCESS) {
314 set_host_byte(scmd, DID_TIME_OUT);
315 scsi_eh_scmd_add(scmd);
319 return rtn;
323 * scsi_block_when_processing_errors - Prevent cmds from being queued.
324 * @sdev: Device on which we are performing recovery.
326 * Description:
327 * We block until the host is out of error recovery, and then check to
328 * see whether the host or the device is offline.
330 * Return value:
331 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
333 int scsi_block_when_processing_errors(struct scsi_device *sdev)
335 int online;
337 wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
339 online = scsi_device_online(sdev);
341 return online;
343 EXPORT_SYMBOL(scsi_block_when_processing_errors);
345 #ifdef CONFIG_SCSI_LOGGING
347 * scsi_eh_prt_fail_stats - Log info on failures.
348 * @shost: scsi host being recovered.
349 * @work_q: Queue of scsi cmds to process.
351 static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
352 struct list_head *work_q)
354 struct scsi_cmnd *scmd;
355 struct scsi_device *sdev;
356 int total_failures = 0;
357 int cmd_failed = 0;
358 int cmd_cancel = 0;
359 int devices_failed = 0;
361 shost_for_each_device(sdev, shost) {
362 list_for_each_entry(scmd, work_q, eh_entry) {
363 if (scmd->device == sdev) {
364 ++total_failures;
365 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED)
366 ++cmd_cancel;
367 else
368 ++cmd_failed;
372 if (cmd_cancel || cmd_failed) {
373 SCSI_LOG_ERROR_RECOVERY(3,
374 shost_printk(KERN_INFO, shost,
375 "%s: cmds failed: %d, cancel: %d\n",
376 __func__, cmd_failed,
377 cmd_cancel));
378 cmd_cancel = 0;
379 cmd_failed = 0;
380 ++devices_failed;
384 SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO, shost,
385 "Total of %d commands on %d"
386 " devices require eh work\n",
387 total_failures, devices_failed));
389 #endif
392 * scsi_report_lun_change - Set flag on all *other* devices on the same target
393 * to indicate that a UNIT ATTENTION is expected.
394 * @sdev: Device reporting the UNIT ATTENTION
396 static void scsi_report_lun_change(struct scsi_device *sdev)
398 sdev->sdev_target->expecting_lun_change = 1;
402 * scsi_report_sense - Examine scsi sense information and log messages for
403 * certain conditions, also issue uevents for some of them.
404 * @sdev: Device reporting the sense code
405 * @sshdr: sshdr to be examined
407 static void scsi_report_sense(struct scsi_device *sdev,
408 struct scsi_sense_hdr *sshdr)
410 enum scsi_device_event evt_type = SDEV_EVT_MAXBITS; /* i.e. none */
412 if (sshdr->sense_key == UNIT_ATTENTION) {
413 if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) {
414 evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED;
415 sdev_printk(KERN_WARNING, sdev,
416 "Inquiry data has changed");
417 } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) {
418 evt_type = SDEV_EVT_LUN_CHANGE_REPORTED;
419 scsi_report_lun_change(sdev);
420 sdev_printk(KERN_WARNING, sdev,
421 "Warning! Received an indication that the "
422 "LUN assignments on this target have "
423 "changed. The Linux SCSI layer does not "
424 "automatically remap LUN assignments.\n");
425 } else if (sshdr->asc == 0x3f)
426 sdev_printk(KERN_WARNING, sdev,
427 "Warning! Received an indication that the "
428 "operating parameters on this target have "
429 "changed. The Linux SCSI layer does not "
430 "automatically adjust these parameters.\n");
432 if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) {
433 evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED;
434 sdev_printk(KERN_WARNING, sdev,
435 "Warning! Received an indication that the "
436 "LUN reached a thin provisioning soft "
437 "threshold.\n");
440 if (sshdr->asc == 0x29) {
441 evt_type = SDEV_EVT_POWER_ON_RESET_OCCURRED;
442 sdev_printk(KERN_WARNING, sdev,
443 "Power-on or device reset occurred\n");
446 if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) {
447 evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED;
448 sdev_printk(KERN_WARNING, sdev,
449 "Mode parameters changed");
450 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x06) {
451 evt_type = SDEV_EVT_ALUA_STATE_CHANGE_REPORTED;
452 sdev_printk(KERN_WARNING, sdev,
453 "Asymmetric access state changed");
454 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) {
455 evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED;
456 sdev_printk(KERN_WARNING, sdev,
457 "Capacity data has changed");
458 } else if (sshdr->asc == 0x2a)
459 sdev_printk(KERN_WARNING, sdev,
460 "Parameters changed");
463 if (evt_type != SDEV_EVT_MAXBITS) {
464 set_bit(evt_type, sdev->pending_events);
465 schedule_work(&sdev->event_work);
470 * scsi_check_sense - Examine scsi cmd sense
471 * @scmd: Cmd to have sense checked.
473 * Return value:
474 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
476 * Notes:
477 * When a deferred error is detected the current command has
478 * not been executed and needs retrying.
480 int scsi_check_sense(struct scsi_cmnd *scmd)
482 struct scsi_device *sdev = scmd->device;
483 struct scsi_sense_hdr sshdr;
485 if (! scsi_command_normalize_sense(scmd, &sshdr))
486 return FAILED; /* no valid sense data */
488 scsi_report_sense(sdev, &sshdr);
490 if (scsi_sense_is_deferred(&sshdr))
491 return NEEDS_RETRY;
493 if (sdev->handler && sdev->handler->check_sense) {
494 int rc;
496 rc = sdev->handler->check_sense(sdev, &sshdr);
497 if (rc != SCSI_RETURN_NOT_HANDLED)
498 return rc;
499 /* handler does not care. Drop down to default handling */
502 if (scmd->cmnd[0] == TEST_UNIT_READY && scmd->scsi_done != scsi_eh_done)
504 * nasty: for mid-layer issued TURs, we need to return the
505 * actual sense data without any recovery attempt. For eh
506 * issued ones, we need to try to recover and interpret
508 return SUCCESS;
511 * Previous logic looked for FILEMARK, EOM or ILI which are
512 * mainly associated with tapes and returned SUCCESS.
514 if (sshdr.response_code == 0x70) {
515 /* fixed format */
516 if (scmd->sense_buffer[2] & 0xe0)
517 return SUCCESS;
518 } else {
520 * descriptor format: look for "stream commands sense data
521 * descriptor" (see SSC-3). Assume single sense data
522 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
524 if ((sshdr.additional_length > 3) &&
525 (scmd->sense_buffer[8] == 0x4) &&
526 (scmd->sense_buffer[11] & 0xe0))
527 return SUCCESS;
530 switch (sshdr.sense_key) {
531 case NO_SENSE:
532 return SUCCESS;
533 case RECOVERED_ERROR:
534 return /* soft_error */ SUCCESS;
536 case ABORTED_COMMAND:
537 if (sshdr.asc == 0x10) /* DIF */
538 return SUCCESS;
540 if (sshdr.asc == 0x44 && sdev->sdev_bflags & BLIST_RETRY_ITF)
541 return ADD_TO_MLQUEUE;
542 if (sshdr.asc == 0xc1 && sshdr.ascq == 0x01 &&
543 sdev->sdev_bflags & BLIST_RETRY_ASC_C1)
544 return ADD_TO_MLQUEUE;
546 return NEEDS_RETRY;
547 case NOT_READY:
548 case UNIT_ATTENTION:
550 * if we are expecting a cc/ua because of a bus reset that we
551 * performed, treat this just as a retry. otherwise this is
552 * information that we should pass up to the upper-level driver
553 * so that we can deal with it there.
555 if (scmd->device->expecting_cc_ua) {
557 * Because some device does not queue unit
558 * attentions correctly, we carefully check
559 * additional sense code and qualifier so as
560 * not to squash media change unit attention.
562 if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) {
563 scmd->device->expecting_cc_ua = 0;
564 return NEEDS_RETRY;
568 * we might also expect a cc/ua if another LUN on the target
569 * reported a UA with an ASC/ASCQ of 3F 0E -
570 * REPORTED LUNS DATA HAS CHANGED.
572 if (scmd->device->sdev_target->expecting_lun_change &&
573 sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
574 return NEEDS_RETRY;
576 * if the device is in the process of becoming ready, we
577 * should retry.
579 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
580 return NEEDS_RETRY;
582 * if the device is not started, we need to wake
583 * the error handler to start the motor
585 if (scmd->device->allow_restart &&
586 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
587 return FAILED;
589 * Pass the UA upwards for a determination in the completion
590 * functions.
592 return SUCCESS;
594 /* these are not supported */
595 case DATA_PROTECT:
596 if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
597 /* Thin provisioning hard threshold reached */
598 set_host_byte(scmd, DID_ALLOC_FAILURE);
599 return SUCCESS;
601 /* FALLTHROUGH */
602 case COPY_ABORTED:
603 case VOLUME_OVERFLOW:
604 case MISCOMPARE:
605 case BLANK_CHECK:
606 set_host_byte(scmd, DID_TARGET_FAILURE);
607 return SUCCESS;
609 case MEDIUM_ERROR:
610 if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
611 sshdr.asc == 0x13 || /* AMNF DATA FIELD */
612 sshdr.asc == 0x14) { /* RECORD NOT FOUND */
613 set_host_byte(scmd, DID_MEDIUM_ERROR);
614 return SUCCESS;
616 return NEEDS_RETRY;
618 case HARDWARE_ERROR:
619 if (scmd->device->retry_hwerror)
620 return ADD_TO_MLQUEUE;
621 else
622 set_host_byte(scmd, DID_TARGET_FAILURE);
623 /* FALLTHROUGH */
625 case ILLEGAL_REQUEST:
626 if (sshdr.asc == 0x20 || /* Invalid command operation code */
627 sshdr.asc == 0x21 || /* Logical block address out of range */
628 sshdr.asc == 0x22 || /* Invalid function */
629 sshdr.asc == 0x24 || /* Invalid field in cdb */
630 sshdr.asc == 0x26 || /* Parameter value invalid */
631 sshdr.asc == 0x27) { /* Write protected */
632 set_host_byte(scmd, DID_TARGET_FAILURE);
634 return SUCCESS;
636 default:
637 return SUCCESS;
640 EXPORT_SYMBOL_GPL(scsi_check_sense);
642 static void scsi_handle_queue_ramp_up(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 sdev->queue_depth >= sdev->max_queue_depth)
649 return;
651 if (time_before(jiffies,
652 sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
653 return;
655 if (time_before(jiffies,
656 sdev->last_queue_full_time + sdev->queue_ramp_up_period))
657 return;
660 * Walk all devices of a target and do
661 * ramp up on them.
663 shost_for_each_device(tmp_sdev, sdev->host) {
664 if (tmp_sdev->channel != sdev->channel ||
665 tmp_sdev->id != sdev->id ||
666 tmp_sdev->queue_depth == sdev->max_queue_depth)
667 continue;
669 scsi_change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1);
670 sdev->last_queue_ramp_up = jiffies;
674 static void scsi_handle_queue_full(struct scsi_device *sdev)
676 struct scsi_host_template *sht = sdev->host->hostt;
677 struct scsi_device *tmp_sdev;
679 if (!sht->track_queue_depth)
680 return;
682 shost_for_each_device(tmp_sdev, sdev->host) {
683 if (tmp_sdev->channel != sdev->channel ||
684 tmp_sdev->id != sdev->id)
685 continue;
687 * We do not know the number of commands that were at
688 * the device when we got the queue full so we start
689 * from the highest possible value and work our way down.
691 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
696 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
697 * @scmd: SCSI cmd to examine.
699 * Notes:
700 * This is *only* called when we are examining the status of commands
701 * queued during error recovery. the main difference here is that we
702 * don't allow for the possibility of retries here, and we are a lot
703 * more restrictive about what we consider acceptable.
705 static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
708 * first check the host byte, to see if there is anything in there
709 * that would indicate what we need to do.
711 if (host_byte(scmd->result) == DID_RESET) {
713 * rats. we are already in the error handler, so we now
714 * get to try and figure out what to do next. if the sense
715 * is valid, we have a pretty good idea of what to do.
716 * if not, we mark it as FAILED.
718 return scsi_check_sense(scmd);
720 if (host_byte(scmd->result) != DID_OK)
721 return FAILED;
724 * next, check the message byte.
726 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
727 return FAILED;
730 * now, check the status byte to see if this indicates
731 * anything special.
733 switch (status_byte(scmd->result)) {
734 case GOOD:
735 scsi_handle_queue_ramp_up(scmd->device);
736 /* FALLTHROUGH */
737 case COMMAND_TERMINATED:
738 return SUCCESS;
739 case CHECK_CONDITION:
740 return scsi_check_sense(scmd);
741 case CONDITION_GOOD:
742 case INTERMEDIATE_GOOD:
743 case INTERMEDIATE_C_GOOD:
745 * who knows? FIXME(eric)
747 return SUCCESS;
748 case RESERVATION_CONFLICT:
749 if (scmd->cmnd[0] == TEST_UNIT_READY)
750 /* it is a success, we probed the device and
751 * found it */
752 return SUCCESS;
753 /* otherwise, we failed to send the command */
754 return FAILED;
755 case QUEUE_FULL:
756 scsi_handle_queue_full(scmd->device);
757 /* fall through */
758 case BUSY:
759 return NEEDS_RETRY;
760 default:
761 return FAILED;
763 return FAILED;
767 * scsi_eh_done - Completion function for error handling.
768 * @scmd: Cmd that is done.
770 static void scsi_eh_done(struct scsi_cmnd *scmd)
772 struct completion *eh_action;
774 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
775 "%s result: %x\n", __func__, scmd->result));
777 eh_action = scmd->device->host->eh_action;
778 if (eh_action)
779 complete(eh_action);
783 * scsi_try_host_reset - ask host adapter to reset itself
784 * @scmd: SCSI cmd to send host reset.
786 static int scsi_try_host_reset(struct scsi_cmnd *scmd)
788 unsigned long flags;
789 int rtn;
790 struct Scsi_Host *host = scmd->device->host;
791 struct scsi_host_template *hostt = host->hostt;
793 SCSI_LOG_ERROR_RECOVERY(3,
794 shost_printk(KERN_INFO, host, "Snd Host RST\n"));
796 if (!hostt->eh_host_reset_handler)
797 return FAILED;
799 rtn = hostt->eh_host_reset_handler(scmd);
801 if (rtn == SUCCESS) {
802 if (!hostt->skip_settle_delay)
803 ssleep(HOST_RESET_SETTLE_TIME);
804 spin_lock_irqsave(host->host_lock, flags);
805 scsi_report_bus_reset(host, scmd_channel(scmd));
806 spin_unlock_irqrestore(host->host_lock, flags);
809 return rtn;
813 * scsi_try_bus_reset - ask host to perform a bus reset
814 * @scmd: SCSI cmd to send bus reset.
816 static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
818 unsigned long flags;
819 int rtn;
820 struct Scsi_Host *host = scmd->device->host;
821 struct scsi_host_template *hostt = host->hostt;
823 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
824 "%s: Snd Bus RST\n", __func__));
826 if (!hostt->eh_bus_reset_handler)
827 return FAILED;
829 rtn = hostt->eh_bus_reset_handler(scmd);
831 if (rtn == SUCCESS) {
832 if (!hostt->skip_settle_delay)
833 ssleep(BUS_RESET_SETTLE_TIME);
834 spin_lock_irqsave(host->host_lock, flags);
835 scsi_report_bus_reset(host, scmd_channel(scmd));
836 spin_unlock_irqrestore(host->host_lock, flags);
839 return rtn;
842 static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
844 sdev->was_reset = 1;
845 sdev->expecting_cc_ua = 1;
849 * scsi_try_target_reset - Ask host to perform a target reset
850 * @scmd: SCSI cmd used to send a target reset
852 * Notes:
853 * There is no timeout for this operation. if this operation is
854 * unreliable for a given host, then the host itself needs to put a
855 * timer on it, and set the host back to a consistent state prior to
856 * returning.
858 static int scsi_try_target_reset(struct scsi_cmnd *scmd)
860 unsigned long flags;
861 int rtn;
862 struct Scsi_Host *host = scmd->device->host;
863 struct scsi_host_template *hostt = host->hostt;
865 if (!hostt->eh_target_reset_handler)
866 return FAILED;
868 rtn = hostt->eh_target_reset_handler(scmd);
869 if (rtn == SUCCESS) {
870 spin_lock_irqsave(host->host_lock, flags);
871 __starget_for_each_device(scsi_target(scmd->device), NULL,
872 __scsi_report_device_reset);
873 spin_unlock_irqrestore(host->host_lock, flags);
876 return rtn;
880 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
881 * @scmd: SCSI cmd used to send BDR
883 * Notes:
884 * There is no timeout for this operation. if this operation is
885 * unreliable for a given host, then the host itself needs to put a
886 * timer on it, and set the host back to a consistent state prior to
887 * returning.
889 static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
891 int rtn;
892 struct scsi_host_template *hostt = scmd->device->host->hostt;
894 if (!hostt->eh_device_reset_handler)
895 return FAILED;
897 rtn = hostt->eh_device_reset_handler(scmd);
898 if (rtn == SUCCESS)
899 __scsi_report_device_reset(scmd->device, NULL);
900 return rtn;
904 * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
905 * @hostt: SCSI driver host template
906 * @scmd: SCSI cmd used to send a target reset
908 * Return value:
909 * SUCCESS, FAILED, or FAST_IO_FAIL
911 * Notes:
912 * SUCCESS does not necessarily indicate that the command
913 * has been aborted; it only indicates that the LLDDs
914 * has cleared all references to that command.
915 * LLDDs should return FAILED only if an abort was required
916 * but could not be executed. LLDDs should return FAST_IO_FAIL
917 * if the device is temporarily unavailable (eg due to a
918 * link down on FibreChannel)
920 static int scsi_try_to_abort_cmd(struct scsi_host_template *hostt,
921 struct scsi_cmnd *scmd)
923 if (!hostt->eh_abort_handler)
924 return FAILED;
926 return hostt->eh_abort_handler(scmd);
929 static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
931 if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
932 if (scsi_try_bus_device_reset(scmd) != SUCCESS)
933 if (scsi_try_target_reset(scmd) != SUCCESS)
934 if (scsi_try_bus_reset(scmd) != SUCCESS)
935 scsi_try_host_reset(scmd);
939 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
940 * @scmd: SCSI command structure to hijack
941 * @ses: structure to save restore information
942 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
943 * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
944 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
946 * This function is used to save a scsi command information before re-execution
947 * as part of the error recovery process. If @sense_bytes is 0 the command
948 * sent must be one that does not transfer any data. If @sense_bytes != 0
949 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
950 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
952 void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
953 unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
955 struct scsi_device *sdev = scmd->device;
958 * We need saved copies of a number of fields - this is because
959 * error handling may need to overwrite these with different values
960 * to run different commands, and once error handling is complete,
961 * we will need to restore these values prior to running the actual
962 * command.
964 ses->cmd_len = scmd->cmd_len;
965 ses->cmnd = scmd->cmnd;
966 ses->data_direction = scmd->sc_data_direction;
967 ses->sdb = scmd->sdb;
968 ses->result = scmd->result;
969 ses->underflow = scmd->underflow;
970 ses->prot_op = scmd->prot_op;
971 ses->eh_eflags = scmd->eh_eflags;
973 scmd->prot_op = SCSI_PROT_NORMAL;
974 scmd->eh_eflags = 0;
975 scmd->cmnd = ses->eh_cmnd;
976 memset(scmd->cmnd, 0, BLK_MAX_CDB);
977 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
978 scmd->result = 0;
980 if (sense_bytes) {
981 scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
982 sense_bytes);
983 sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
984 scmd->sdb.length);
985 scmd->sdb.table.sgl = &ses->sense_sgl;
986 scmd->sc_data_direction = DMA_FROM_DEVICE;
987 scmd->sdb.table.nents = scmd->sdb.table.orig_nents = 1;
988 scmd->cmnd[0] = REQUEST_SENSE;
989 scmd->cmnd[4] = scmd->sdb.length;
990 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
991 } else {
992 scmd->sc_data_direction = DMA_NONE;
993 if (cmnd) {
994 BUG_ON(cmnd_size > BLK_MAX_CDB);
995 memcpy(scmd->cmnd, cmnd, cmnd_size);
996 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
1000 scmd->underflow = 0;
1002 if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
1003 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
1004 (sdev->lun << 5 & 0xe0);
1007 * Zero the sense buffer. The scsi spec mandates that any
1008 * untransferred sense data should be interpreted as being zero.
1010 memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1012 EXPORT_SYMBOL(scsi_eh_prep_cmnd);
1015 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
1016 * @scmd: SCSI command structure to restore
1017 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
1019 * Undo any damage done by above scsi_eh_prep_cmnd().
1021 void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
1024 * Restore original data
1026 scmd->cmd_len = ses->cmd_len;
1027 scmd->cmnd = ses->cmnd;
1028 scmd->sc_data_direction = ses->data_direction;
1029 scmd->sdb = ses->sdb;
1030 scmd->result = ses->result;
1031 scmd->underflow = ses->underflow;
1032 scmd->prot_op = ses->prot_op;
1033 scmd->eh_eflags = ses->eh_eflags;
1035 EXPORT_SYMBOL(scsi_eh_restore_cmnd);
1038 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
1039 * @scmd: SCSI command structure to hijack
1040 * @cmnd: CDB to send
1041 * @cmnd_size: size in bytes of @cmnd
1042 * @timeout: timeout for this request
1043 * @sense_bytes: size of sense data to copy or 0
1045 * This function is used to send a scsi command down to a target device
1046 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
1048 * Return value:
1049 * SUCCESS or FAILED or NEEDS_RETRY
1051 static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
1052 int cmnd_size, int timeout, unsigned sense_bytes)
1054 struct scsi_device *sdev = scmd->device;
1055 struct Scsi_Host *shost = sdev->host;
1056 DECLARE_COMPLETION_ONSTACK(done);
1057 unsigned long timeleft = timeout;
1058 struct scsi_eh_save ses;
1059 const unsigned long stall_for = msecs_to_jiffies(100);
1060 int rtn;
1062 retry:
1063 scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
1064 shost->eh_action = &done;
1066 scsi_log_send(scmd);
1067 scmd->scsi_done = scsi_eh_done;
1068 rtn = shost->hostt->queuecommand(shost, scmd);
1069 if (rtn) {
1070 if (timeleft > stall_for) {
1071 scsi_eh_restore_cmnd(scmd, &ses);
1072 timeleft -= stall_for;
1073 msleep(jiffies_to_msecs(stall_for));
1074 goto retry;
1076 /* signal not to enter either branch of the if () below */
1077 timeleft = 0;
1078 rtn = FAILED;
1079 } else {
1080 timeleft = wait_for_completion_timeout(&done, timeout);
1081 rtn = SUCCESS;
1084 shost->eh_action = NULL;
1086 scsi_log_completion(scmd, rtn);
1088 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1089 "%s timeleft: %ld\n",
1090 __func__, timeleft));
1093 * If there is time left scsi_eh_done got called, and we will examine
1094 * the actual status codes to see whether the command actually did
1095 * complete normally, else if we have a zero return and no time left,
1096 * the command must still be pending, so abort it and return FAILED.
1097 * If we never actually managed to issue the command, because
1098 * ->queuecommand() kept returning non zero, use the rtn = FAILED
1099 * value above (so don't execute either branch of the if)
1101 if (timeleft) {
1102 rtn = scsi_eh_completed_normally(scmd);
1103 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1104 "%s: scsi_eh_completed_normally %x\n", __func__, rtn));
1106 switch (rtn) {
1107 case SUCCESS:
1108 case NEEDS_RETRY:
1109 case FAILED:
1110 break;
1111 case ADD_TO_MLQUEUE:
1112 rtn = NEEDS_RETRY;
1113 break;
1114 default:
1115 rtn = FAILED;
1116 break;
1118 } else if (rtn != FAILED) {
1119 scsi_abort_eh_cmnd(scmd);
1120 rtn = FAILED;
1123 scsi_eh_restore_cmnd(scmd, &ses);
1125 return rtn;
1129 * scsi_request_sense - Request sense data from a particular target.
1130 * @scmd: SCSI cmd for request sense.
1132 * Notes:
1133 * Some hosts automatically obtain this information, others require
1134 * that we obtain it on our own. This function will *not* return until
1135 * the command either times out, or it completes.
1137 static int scsi_request_sense(struct scsi_cmnd *scmd)
1139 return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
1142 static int scsi_eh_action(struct scsi_cmnd *scmd, int rtn)
1144 if (!blk_rq_is_passthrough(scmd->request)) {
1145 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
1146 if (sdrv->eh_action)
1147 rtn = sdrv->eh_action(scmd, rtn);
1149 return rtn;
1153 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
1154 * @scmd: Original SCSI cmd that eh has finished.
1155 * @done_q: Queue for processed commands.
1157 * Notes:
1158 * We don't want to use the normal command completion while we are are
1159 * still handling errors - it may cause other commands to be queued,
1160 * and that would disturb what we are doing. Thus we really want to
1161 * keep a list of pending commands for final completion, and once we
1162 * are ready to leave error handling we handle completion for real.
1164 void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
1166 list_move_tail(&scmd->eh_entry, done_q);
1168 EXPORT_SYMBOL(scsi_eh_finish_cmd);
1171 * scsi_eh_get_sense - Get device sense data.
1172 * @work_q: Queue of commands to process.
1173 * @done_q: Queue of processed commands.
1175 * Description:
1176 * See if we need to request sense information. if so, then get it
1177 * now, so we have a better idea of what to do.
1179 * Notes:
1180 * This has the unfortunate side effect that if a shost adapter does
1181 * not automatically request sense information, we end up shutting
1182 * it down before we request it.
1184 * All drivers should request sense information internally these days,
1185 * so for now all I have to say is tough noogies if you end up in here.
1187 * XXX: Long term this code should go away, but that needs an audit of
1188 * all LLDDs first.
1190 int scsi_eh_get_sense(struct list_head *work_q,
1191 struct list_head *done_q)
1193 struct scsi_cmnd *scmd, *next;
1194 struct Scsi_Host *shost;
1195 int rtn;
1198 * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
1199 * should not get sense.
1201 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1202 if ((scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) ||
1203 SCSI_SENSE_VALID(scmd))
1204 continue;
1206 shost = scmd->device->host;
1207 if (scsi_host_eh_past_deadline(shost)) {
1208 SCSI_LOG_ERROR_RECOVERY(3,
1209 scmd_printk(KERN_INFO, scmd,
1210 "%s: skip request sense, past eh deadline\n",
1211 current->comm));
1212 break;
1214 if (status_byte(scmd->result) != CHECK_CONDITION)
1216 * don't request sense if there's no check condition
1217 * status because the error we're processing isn't one
1218 * that has a sense code (and some devices get
1219 * confused by sense requests out of the blue)
1221 continue;
1223 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
1224 "%s: requesting sense\n",
1225 current->comm));
1226 rtn = scsi_request_sense(scmd);
1227 if (rtn != SUCCESS)
1228 continue;
1230 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1231 "sense requested, result %x\n", scmd->result));
1232 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd));
1234 rtn = scsi_decide_disposition(scmd);
1237 * if the result was normal, then just pass it along to the
1238 * upper level.
1240 if (rtn == SUCCESS)
1241 /* we don't want this command reissued, just
1242 * finished with the sense data, so set
1243 * retries to the max allowed to ensure it
1244 * won't get reissued */
1245 scmd->retries = scmd->allowed;
1246 else if (rtn != NEEDS_RETRY)
1247 continue;
1249 scsi_eh_finish_cmd(scmd, done_q);
1252 return list_empty(work_q);
1254 EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
1257 * scsi_eh_tur - Send TUR to device.
1258 * @scmd: &scsi_cmnd to send TUR
1260 * Return value:
1261 * 0 - Device is ready. 1 - Device NOT ready.
1263 static int scsi_eh_tur(struct scsi_cmnd *scmd)
1265 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
1266 int retry_cnt = 1, rtn;
1268 retry_tur:
1269 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
1270 scmd->device->eh_timeout, 0);
1272 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1273 "%s return: %x\n", __func__, rtn));
1275 switch (rtn) {
1276 case NEEDS_RETRY:
1277 if (retry_cnt--)
1278 goto retry_tur;
1279 /*FALLTHRU*/
1280 case SUCCESS:
1281 return 0;
1282 default:
1283 return 1;
1288 * scsi_eh_test_devices - check if devices are responding from error recovery.
1289 * @cmd_list: scsi commands in error recovery.
1290 * @work_q: queue for commands which still need more error recovery
1291 * @done_q: queue for commands which are finished
1292 * @try_stu: boolean on if a STU command should be tried in addition to TUR.
1294 * Decription:
1295 * Tests if devices are in a working state. Commands to devices now in
1296 * a working state are sent to the done_q while commands to devices which
1297 * are still failing to respond are returned to the work_q for more
1298 * processing.
1300 static int scsi_eh_test_devices(struct list_head *cmd_list,
1301 struct list_head *work_q,
1302 struct list_head *done_q, int try_stu)
1304 struct scsi_cmnd *scmd, *next;
1305 struct scsi_device *sdev;
1306 int finish_cmds;
1308 while (!list_empty(cmd_list)) {
1309 scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
1310 sdev = scmd->device;
1312 if (!try_stu) {
1313 if (scsi_host_eh_past_deadline(sdev->host)) {
1314 /* Push items back onto work_q */
1315 list_splice_init(cmd_list, work_q);
1316 SCSI_LOG_ERROR_RECOVERY(3,
1317 sdev_printk(KERN_INFO, sdev,
1318 "%s: skip test device, past eh deadline",
1319 current->comm));
1320 break;
1324 finish_cmds = !scsi_device_online(scmd->device) ||
1325 (try_stu && !scsi_eh_try_stu(scmd) &&
1326 !scsi_eh_tur(scmd)) ||
1327 !scsi_eh_tur(scmd);
1329 list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
1330 if (scmd->device == sdev) {
1331 if (finish_cmds &&
1332 (try_stu ||
1333 scsi_eh_action(scmd, SUCCESS) == SUCCESS))
1334 scsi_eh_finish_cmd(scmd, done_q);
1335 else
1336 list_move_tail(&scmd->eh_entry, work_q);
1339 return list_empty(work_q);
1343 * scsi_eh_try_stu - Send START_UNIT to device.
1344 * @scmd: &scsi_cmnd to send START_UNIT
1346 * Return value:
1347 * 0 - Device is ready. 1 - Device NOT ready.
1349 static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
1351 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
1353 if (scmd->device->allow_restart) {
1354 int i, rtn = NEEDS_RETRY;
1356 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
1357 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->device->request_queue->rq_timeout, 0);
1359 if (rtn == SUCCESS)
1360 return 0;
1363 return 1;
1367 * scsi_eh_stu - send START_UNIT if needed
1368 * @shost: &scsi host being recovered.
1369 * @work_q: &list_head for pending commands.
1370 * @done_q: &list_head for processed commands.
1372 * Notes:
1373 * If commands are failing due to not ready, initializing command required,
1374 * try revalidating the device, which will end up sending a start unit.
1376 static int scsi_eh_stu(struct Scsi_Host *shost,
1377 struct list_head *work_q,
1378 struct list_head *done_q)
1380 struct scsi_cmnd *scmd, *stu_scmd, *next;
1381 struct scsi_device *sdev;
1383 shost_for_each_device(sdev, shost) {
1384 if (scsi_host_eh_past_deadline(shost)) {
1385 SCSI_LOG_ERROR_RECOVERY(3,
1386 sdev_printk(KERN_INFO, sdev,
1387 "%s: skip START_UNIT, past eh deadline\n",
1388 current->comm));
1389 break;
1391 stu_scmd = NULL;
1392 list_for_each_entry(scmd, work_q, eh_entry)
1393 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
1394 scsi_check_sense(scmd) == FAILED ) {
1395 stu_scmd = scmd;
1396 break;
1399 if (!stu_scmd)
1400 continue;
1402 SCSI_LOG_ERROR_RECOVERY(3,
1403 sdev_printk(KERN_INFO, sdev,
1404 "%s: Sending START_UNIT\n",
1405 current->comm));
1407 if (!scsi_eh_try_stu(stu_scmd)) {
1408 if (!scsi_device_online(sdev) ||
1409 !scsi_eh_tur(stu_scmd)) {
1410 list_for_each_entry_safe(scmd, next,
1411 work_q, eh_entry) {
1412 if (scmd->device == sdev &&
1413 scsi_eh_action(scmd, SUCCESS) == SUCCESS)
1414 scsi_eh_finish_cmd(scmd, done_q);
1417 } else {
1418 SCSI_LOG_ERROR_RECOVERY(3,
1419 sdev_printk(KERN_INFO, sdev,
1420 "%s: START_UNIT failed\n",
1421 current->comm));
1425 return list_empty(work_q);
1430 * scsi_eh_bus_device_reset - send bdr if needed
1431 * @shost: scsi host being recovered.
1432 * @work_q: &list_head for pending commands.
1433 * @done_q: &list_head for processed commands.
1435 * Notes:
1436 * Try a bus device reset. Still, look to see whether we have multiple
1437 * devices that are jammed or not - if we have multiple devices, it
1438 * makes no sense to try bus_device_reset - we really would need to try
1439 * a bus_reset instead.
1441 static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1442 struct list_head *work_q,
1443 struct list_head *done_q)
1445 struct scsi_cmnd *scmd, *bdr_scmd, *next;
1446 struct scsi_device *sdev;
1447 int rtn;
1449 shost_for_each_device(sdev, shost) {
1450 if (scsi_host_eh_past_deadline(shost)) {
1451 SCSI_LOG_ERROR_RECOVERY(3,
1452 sdev_printk(KERN_INFO, sdev,
1453 "%s: skip BDR, past eh deadline\n",
1454 current->comm));
1455 break;
1457 bdr_scmd = NULL;
1458 list_for_each_entry(scmd, work_q, eh_entry)
1459 if (scmd->device == sdev) {
1460 bdr_scmd = scmd;
1461 break;
1464 if (!bdr_scmd)
1465 continue;
1467 SCSI_LOG_ERROR_RECOVERY(3,
1468 sdev_printk(KERN_INFO, sdev,
1469 "%s: Sending BDR\n", current->comm));
1470 rtn = scsi_try_bus_device_reset(bdr_scmd);
1471 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
1472 if (!scsi_device_online(sdev) ||
1473 rtn == FAST_IO_FAIL ||
1474 !scsi_eh_tur(bdr_scmd)) {
1475 list_for_each_entry_safe(scmd, next,
1476 work_q, eh_entry) {
1477 if (scmd->device == sdev &&
1478 scsi_eh_action(scmd, rtn) != FAILED)
1479 scsi_eh_finish_cmd(scmd,
1480 done_q);
1483 } else {
1484 SCSI_LOG_ERROR_RECOVERY(3,
1485 sdev_printk(KERN_INFO, sdev,
1486 "%s: BDR failed\n", current->comm));
1490 return list_empty(work_q);
1494 * scsi_eh_target_reset - send target reset if needed
1495 * @shost: scsi host being recovered.
1496 * @work_q: &list_head for pending commands.
1497 * @done_q: &list_head for processed commands.
1499 * Notes:
1500 * Try a target reset.
1502 static int scsi_eh_target_reset(struct Scsi_Host *shost,
1503 struct list_head *work_q,
1504 struct list_head *done_q)
1506 LIST_HEAD(tmp_list);
1507 LIST_HEAD(check_list);
1509 list_splice_init(work_q, &tmp_list);
1511 while (!list_empty(&tmp_list)) {
1512 struct scsi_cmnd *next, *scmd;
1513 int rtn;
1514 unsigned int id;
1516 if (scsi_host_eh_past_deadline(shost)) {
1517 /* push back on work queue for further processing */
1518 list_splice_init(&check_list, work_q);
1519 list_splice_init(&tmp_list, work_q);
1520 SCSI_LOG_ERROR_RECOVERY(3,
1521 shost_printk(KERN_INFO, shost,
1522 "%s: Skip target reset, past eh deadline\n",
1523 current->comm));
1524 return list_empty(work_q);
1527 scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
1528 id = scmd_id(scmd);
1530 SCSI_LOG_ERROR_RECOVERY(3,
1531 shost_printk(KERN_INFO, shost,
1532 "%s: Sending target reset to target %d\n",
1533 current->comm, id));
1534 rtn = scsi_try_target_reset(scmd);
1535 if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
1536 SCSI_LOG_ERROR_RECOVERY(3,
1537 shost_printk(KERN_INFO, shost,
1538 "%s: Target reset failed"
1539 " target: %d\n",
1540 current->comm, id));
1541 list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
1542 if (scmd_id(scmd) != id)
1543 continue;
1545 if (rtn == SUCCESS)
1546 list_move_tail(&scmd->eh_entry, &check_list);
1547 else if (rtn == FAST_IO_FAIL)
1548 scsi_eh_finish_cmd(scmd, done_q);
1549 else
1550 /* push back on work queue for further processing */
1551 list_move(&scmd->eh_entry, work_q);
1555 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1559 * scsi_eh_bus_reset - send a bus reset
1560 * @shost: &scsi host being recovered.
1561 * @work_q: &list_head for pending commands.
1562 * @done_q: &list_head for processed commands.
1564 static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1565 struct list_head *work_q,
1566 struct list_head *done_q)
1568 struct scsi_cmnd *scmd, *chan_scmd, *next;
1569 LIST_HEAD(check_list);
1570 unsigned int channel;
1571 int rtn;
1574 * we really want to loop over the various channels, and do this on
1575 * a channel by channel basis. we should also check to see if any
1576 * of the failed commands are on soft_reset devices, and if so, skip
1577 * the reset.
1580 for (channel = 0; channel <= shost->max_channel; channel++) {
1581 if (scsi_host_eh_past_deadline(shost)) {
1582 list_splice_init(&check_list, work_q);
1583 SCSI_LOG_ERROR_RECOVERY(3,
1584 shost_printk(KERN_INFO, shost,
1585 "%s: skip BRST, past eh deadline\n",
1586 current->comm));
1587 return list_empty(work_q);
1590 chan_scmd = NULL;
1591 list_for_each_entry(scmd, work_q, eh_entry) {
1592 if (channel == scmd_channel(scmd)) {
1593 chan_scmd = scmd;
1594 break;
1596 * FIXME add back in some support for
1597 * soft_reset devices.
1602 if (!chan_scmd)
1603 continue;
1604 SCSI_LOG_ERROR_RECOVERY(3,
1605 shost_printk(KERN_INFO, shost,
1606 "%s: Sending BRST chan: %d\n",
1607 current->comm, channel));
1608 rtn = scsi_try_bus_reset(chan_scmd);
1609 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
1610 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1611 if (channel == scmd_channel(scmd)) {
1612 if (rtn == FAST_IO_FAIL)
1613 scsi_eh_finish_cmd(scmd,
1614 done_q);
1615 else
1616 list_move_tail(&scmd->eh_entry,
1617 &check_list);
1620 } else {
1621 SCSI_LOG_ERROR_RECOVERY(3,
1622 shost_printk(KERN_INFO, shost,
1623 "%s: BRST failed chan: %d\n",
1624 current->comm, channel));
1627 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1631 * scsi_eh_host_reset - send a host reset
1632 * @shost: host to be reset.
1633 * @work_q: &list_head for pending commands.
1634 * @done_q: &list_head for processed commands.
1636 static int scsi_eh_host_reset(struct Scsi_Host *shost,
1637 struct list_head *work_q,
1638 struct list_head *done_q)
1640 struct scsi_cmnd *scmd, *next;
1641 LIST_HEAD(check_list);
1642 int rtn;
1644 if (!list_empty(work_q)) {
1645 scmd = list_entry(work_q->next,
1646 struct scsi_cmnd, eh_entry);
1648 SCSI_LOG_ERROR_RECOVERY(3,
1649 shost_printk(KERN_INFO, shost,
1650 "%s: Sending HRST\n",
1651 current->comm));
1653 rtn = scsi_try_host_reset(scmd);
1654 if (rtn == SUCCESS) {
1655 list_splice_init(work_q, &check_list);
1656 } else if (rtn == FAST_IO_FAIL) {
1657 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1658 scsi_eh_finish_cmd(scmd, done_q);
1660 } else {
1661 SCSI_LOG_ERROR_RECOVERY(3,
1662 shost_printk(KERN_INFO, shost,
1663 "%s: HRST failed\n",
1664 current->comm));
1667 return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
1671 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
1672 * @work_q: &list_head for pending commands.
1673 * @done_q: &list_head for processed commands.
1675 static void scsi_eh_offline_sdevs(struct list_head *work_q,
1676 struct list_head *done_q)
1678 struct scsi_cmnd *scmd, *next;
1679 struct scsi_device *sdev;
1681 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1682 sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
1683 "not ready after error recovery\n");
1684 sdev = scmd->device;
1686 mutex_lock(&sdev->state_mutex);
1687 scsi_device_set_state(sdev, SDEV_OFFLINE);
1688 mutex_unlock(&sdev->state_mutex);
1690 scsi_eh_finish_cmd(scmd, done_q);
1692 return;
1696 * scsi_noretry_cmd - determine if command should be failed fast
1697 * @scmd: SCSI cmd to examine.
1699 int scsi_noretry_cmd(struct scsi_cmnd *scmd)
1701 switch (host_byte(scmd->result)) {
1702 case DID_OK:
1703 break;
1704 case DID_TIME_OUT:
1705 goto check_type;
1706 case DID_BUS_BUSY:
1707 return (scmd->request->cmd_flags & REQ_FAILFAST_TRANSPORT);
1708 case DID_PARITY:
1709 return (scmd->request->cmd_flags & REQ_FAILFAST_DEV);
1710 case DID_ERROR:
1711 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1712 status_byte(scmd->result) == RESERVATION_CONFLICT)
1713 return 0;
1714 /* fall through */
1715 case DID_SOFT_ERROR:
1716 return (scmd->request->cmd_flags & REQ_FAILFAST_DRIVER);
1719 if (status_byte(scmd->result) != CHECK_CONDITION)
1720 return 0;
1722 check_type:
1724 * assume caller has checked sense and determined
1725 * the check condition was retryable.
1727 if (scmd->request->cmd_flags & REQ_FAILFAST_DEV ||
1728 blk_rq_is_passthrough(scmd->request))
1729 return 1;
1730 else
1731 return 0;
1735 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1736 * @scmd: SCSI cmd to examine.
1738 * Notes:
1739 * This is *only* called when we are examining the status after sending
1740 * out the actual data command. any commands that are queued for error
1741 * recovery (e.g. test_unit_ready) do *not* come through here.
1743 * When this routine returns failed, it means the error handler thread
1744 * is woken. In cases where the error code indicates an error that
1745 * doesn't require the error handler read (i.e. we don't need to
1746 * abort/reset), this function should return SUCCESS.
1748 int scsi_decide_disposition(struct scsi_cmnd *scmd)
1750 int rtn;
1753 * if the device is offline, then we clearly just pass the result back
1754 * up to the top level.
1756 if (!scsi_device_online(scmd->device)) {
1757 SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO, scmd,
1758 "%s: device offline - report as SUCCESS\n", __func__));
1759 return SUCCESS;
1763 * first check the host byte, to see if there is anything in there
1764 * that would indicate what we need to do.
1766 switch (host_byte(scmd->result)) {
1767 case DID_PASSTHROUGH:
1769 * no matter what, pass this through to the upper layer.
1770 * nuke this special code so that it looks like we are saying
1771 * did_ok.
1773 scmd->result &= 0xff00ffff;
1774 return SUCCESS;
1775 case DID_OK:
1777 * looks good. drop through, and check the next byte.
1779 break;
1780 case DID_ABORT:
1781 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
1782 set_host_byte(scmd, DID_TIME_OUT);
1783 return SUCCESS;
1785 /* FALLTHROUGH */
1786 case DID_NO_CONNECT:
1787 case DID_BAD_TARGET:
1789 * note - this means that we just report the status back
1790 * to the top level driver, not that we actually think
1791 * that it indicates SUCCESS.
1793 return SUCCESS;
1794 case DID_SOFT_ERROR:
1796 * when the low level driver returns did_soft_error,
1797 * it is responsible for keeping an internal retry counter
1798 * in order to avoid endless loops (db)
1800 goto maybe_retry;
1801 case DID_IMM_RETRY:
1802 return NEEDS_RETRY;
1804 case DID_REQUEUE:
1805 return ADD_TO_MLQUEUE;
1806 case DID_TRANSPORT_DISRUPTED:
1808 * LLD/transport was disrupted during processing of the IO.
1809 * The transport class is now blocked/blocking,
1810 * and the transport will decide what to do with the IO
1811 * based on its timers and recovery capablilities if
1812 * there are enough retries.
1814 goto maybe_retry;
1815 case DID_TRANSPORT_FAILFAST:
1817 * The transport decided to failfast the IO (most likely
1818 * the fast io fail tmo fired), so send IO directly upwards.
1820 return SUCCESS;
1821 case DID_ERROR:
1822 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1823 status_byte(scmd->result) == RESERVATION_CONFLICT)
1825 * execute reservation conflict processing code
1826 * lower down
1828 break;
1829 /* fallthrough */
1830 case DID_BUS_BUSY:
1831 case DID_PARITY:
1832 goto maybe_retry;
1833 case DID_TIME_OUT:
1835 * when we scan the bus, we get timeout messages for
1836 * these commands if there is no device available.
1837 * other hosts report did_no_connect for the same thing.
1839 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1840 scmd->cmnd[0] == INQUIRY)) {
1841 return SUCCESS;
1842 } else {
1843 return FAILED;
1845 case DID_RESET:
1846 return SUCCESS;
1847 default:
1848 return FAILED;
1852 * next, check the message byte.
1854 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
1855 return FAILED;
1858 * check the status byte to see if this indicates anything special.
1860 switch (status_byte(scmd->result)) {
1861 case QUEUE_FULL:
1862 scsi_handle_queue_full(scmd->device);
1864 * the case of trying to send too many commands to a
1865 * tagged queueing device.
1867 /* FALLTHROUGH */
1868 case BUSY:
1870 * device can't talk to us at the moment. Should only
1871 * occur (SAM-3) when the task queue is empty, so will cause
1872 * the empty queue handling to trigger a stall in the
1873 * device.
1875 return ADD_TO_MLQUEUE;
1876 case GOOD:
1877 if (scmd->cmnd[0] == REPORT_LUNS)
1878 scmd->device->sdev_target->expecting_lun_change = 0;
1879 scsi_handle_queue_ramp_up(scmd->device);
1880 /* FALLTHROUGH */
1881 case COMMAND_TERMINATED:
1882 return SUCCESS;
1883 case TASK_ABORTED:
1884 goto maybe_retry;
1885 case CHECK_CONDITION:
1886 rtn = scsi_check_sense(scmd);
1887 if (rtn == NEEDS_RETRY)
1888 goto maybe_retry;
1889 /* if rtn == FAILED, we have no sense information;
1890 * returning FAILED will wake the error handler thread
1891 * to collect the sense and redo the decide
1892 * disposition */
1893 return rtn;
1894 case CONDITION_GOOD:
1895 case INTERMEDIATE_GOOD:
1896 case INTERMEDIATE_C_GOOD:
1897 case ACA_ACTIVE:
1899 * who knows? FIXME(eric)
1901 return SUCCESS;
1903 case RESERVATION_CONFLICT:
1904 sdev_printk(KERN_INFO, scmd->device,
1905 "reservation conflict\n");
1906 set_host_byte(scmd, DID_NEXUS_FAILURE);
1907 return SUCCESS; /* causes immediate i/o error */
1908 default:
1909 return FAILED;
1911 return FAILED;
1913 maybe_retry:
1915 /* we requeue for retry because the error was retryable, and
1916 * the request was not marked fast fail. Note that above,
1917 * even if the request is marked fast fail, we still requeue
1918 * for queue congestion conditions (QUEUE_FULL or BUSY) */
1919 if ((++scmd->retries) <= scmd->allowed
1920 && !scsi_noretry_cmd(scmd)) {
1921 return NEEDS_RETRY;
1922 } else {
1924 * no more retries - report this one back to upper level.
1926 return SUCCESS;
1930 static void eh_lock_door_done(struct request *req, blk_status_t status)
1932 blk_put_request(req);
1936 * scsi_eh_lock_door - Prevent medium removal for the specified device
1937 * @sdev: SCSI device to prevent medium removal
1939 * Locking:
1940 * We must be called from process context.
1942 * Notes:
1943 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1944 * head of the devices request queue, and continue.
1946 static void scsi_eh_lock_door(struct scsi_device *sdev)
1948 struct request *req;
1949 struct scsi_request *rq;
1951 req = blk_get_request(sdev->request_queue, REQ_OP_SCSI_IN, 0);
1952 if (IS_ERR(req))
1953 return;
1954 rq = scsi_req(req);
1956 rq->cmd[0] = ALLOW_MEDIUM_REMOVAL;
1957 rq->cmd[1] = 0;
1958 rq->cmd[2] = 0;
1959 rq->cmd[3] = 0;
1960 rq->cmd[4] = SCSI_REMOVAL_PREVENT;
1961 rq->cmd[5] = 0;
1962 rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
1964 req->rq_flags |= RQF_QUIET;
1965 req->timeout = 10 * HZ;
1966 rq->retries = 5;
1968 blk_execute_rq_nowait(req->q, NULL, req, 1, eh_lock_door_done);
1972 * scsi_restart_operations - restart io operations to the specified host.
1973 * @shost: Host we are restarting.
1975 * Notes:
1976 * When we entered the error handler, we blocked all further i/o to
1977 * this device. we need to 'reverse' this process.
1979 static void scsi_restart_operations(struct Scsi_Host *shost)
1981 struct scsi_device *sdev;
1982 unsigned long flags;
1985 * If the door was locked, we need to insert a door lock request
1986 * onto the head of the SCSI request queue for the device. There
1987 * is no point trying to lock the door of an off-line device.
1989 shost_for_each_device(sdev, shost) {
1990 if (scsi_device_online(sdev) && sdev->was_reset && sdev->locked) {
1991 scsi_eh_lock_door(sdev);
1992 sdev->was_reset = 0;
1997 * next free up anything directly waiting upon the host. this
1998 * will be requests for character device operations, and also for
1999 * ioctls to queued block devices.
2001 SCSI_LOG_ERROR_RECOVERY(3,
2002 shost_printk(KERN_INFO, shost, "waking up host to restart\n"));
2004 spin_lock_irqsave(shost->host_lock, flags);
2005 if (scsi_host_set_state(shost, SHOST_RUNNING))
2006 if (scsi_host_set_state(shost, SHOST_CANCEL))
2007 BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
2008 spin_unlock_irqrestore(shost->host_lock, flags);
2010 wake_up(&shost->host_wait);
2013 * finally we need to re-initiate requests that may be pending. we will
2014 * have had everything blocked while error handling is taking place, and
2015 * now that error recovery is done, we will need to ensure that these
2016 * requests are started.
2018 scsi_run_host_queues(shost);
2021 * if eh is active and host_eh_scheduled is pending we need to re-run
2022 * recovery. we do this check after scsi_run_host_queues() to allow
2023 * everything pent up since the last eh run a chance to make forward
2024 * progress before we sync again. Either we'll immediately re-run
2025 * recovery or scsi_device_unbusy() will wake us again when these
2026 * pending commands complete.
2028 spin_lock_irqsave(shost->host_lock, flags);
2029 if (shost->host_eh_scheduled)
2030 if (scsi_host_set_state(shost, SHOST_RECOVERY))
2031 WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY));
2032 spin_unlock_irqrestore(shost->host_lock, flags);
2036 * scsi_eh_ready_devs - check device ready state and recover if not.
2037 * @shost: host to be recovered.
2038 * @work_q: &list_head for pending commands.
2039 * @done_q: &list_head for processed commands.
2041 void scsi_eh_ready_devs(struct Scsi_Host *shost,
2042 struct list_head *work_q,
2043 struct list_head *done_q)
2045 if (!scsi_eh_stu(shost, work_q, done_q))
2046 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
2047 if (!scsi_eh_target_reset(shost, work_q, done_q))
2048 if (!scsi_eh_bus_reset(shost, work_q, done_q))
2049 if (!scsi_eh_host_reset(shost, work_q, done_q))
2050 scsi_eh_offline_sdevs(work_q,
2051 done_q);
2053 EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
2056 * scsi_eh_flush_done_q - finish processed commands or retry them.
2057 * @done_q: list_head of processed commands.
2059 void scsi_eh_flush_done_q(struct list_head *done_q)
2061 struct scsi_cmnd *scmd, *next;
2063 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
2064 list_del_init(&scmd->eh_entry);
2065 if (scsi_device_online(scmd->device) &&
2066 !scsi_noretry_cmd(scmd) &&
2067 (++scmd->retries <= scmd->allowed)) {
2068 SCSI_LOG_ERROR_RECOVERY(3,
2069 scmd_printk(KERN_INFO, scmd,
2070 "%s: flush retry cmd\n",
2071 current->comm));
2072 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
2073 } else {
2075 * If just we got sense for the device (called
2076 * scsi_eh_get_sense), scmd->result is already
2077 * set, do not set DRIVER_TIMEOUT.
2079 if (!scmd->result)
2080 scmd->result |= (DRIVER_TIMEOUT << 24);
2081 SCSI_LOG_ERROR_RECOVERY(3,
2082 scmd_printk(KERN_INFO, scmd,
2083 "%s: flush finish cmd\n",
2084 current->comm));
2085 scsi_finish_command(scmd);
2089 EXPORT_SYMBOL(scsi_eh_flush_done_q);
2092 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
2093 * @shost: Host to unjam.
2095 * Notes:
2096 * When we come in here, we *know* that all commands on the bus have
2097 * either completed, failed or timed out. we also know that no further
2098 * commands are being sent to the host, so things are relatively quiet
2099 * and we have freedom to fiddle with things as we wish.
2101 * This is only the *default* implementation. it is possible for
2102 * individual drivers to supply their own version of this function, and
2103 * if the maintainer wishes to do this, it is strongly suggested that
2104 * this function be taken as a template and modified. this function
2105 * was designed to correctly handle problems for about 95% of the
2106 * different cases out there, and it should always provide at least a
2107 * reasonable amount of error recovery.
2109 * Any command marked 'failed' or 'timeout' must eventually have
2110 * scsi_finish_cmd() called for it. we do all of the retry stuff
2111 * here, so when we restart the host after we return it should have an
2112 * empty queue.
2114 static void scsi_unjam_host(struct Scsi_Host *shost)
2116 unsigned long flags;
2117 LIST_HEAD(eh_work_q);
2118 LIST_HEAD(eh_done_q);
2120 spin_lock_irqsave(shost->host_lock, flags);
2121 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
2122 spin_unlock_irqrestore(shost->host_lock, flags);
2124 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
2126 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
2127 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
2129 spin_lock_irqsave(shost->host_lock, flags);
2130 if (shost->eh_deadline != -1)
2131 shost->last_reset = 0;
2132 spin_unlock_irqrestore(shost->host_lock, flags);
2133 scsi_eh_flush_done_q(&eh_done_q);
2137 * scsi_error_handler - SCSI error handler thread
2138 * @data: Host for which we are running.
2140 * Notes:
2141 * This is the main error handling loop. This is run as a kernel thread
2142 * for every SCSI host and handles all error handling activity.
2144 int scsi_error_handler(void *data)
2146 struct Scsi_Host *shost = data;
2149 * We use TASK_INTERRUPTIBLE so that the thread is not
2150 * counted against the load average as a running process.
2151 * We never actually get interrupted because kthread_run
2152 * disables signal delivery for the created thread.
2154 while (true) {
2156 * The sequence in kthread_stop() sets the stop flag first
2157 * then wakes the process. To avoid missed wakeups, the task
2158 * should always be in a non running state before the stop
2159 * flag is checked
2161 set_current_state(TASK_INTERRUPTIBLE);
2162 if (kthread_should_stop())
2163 break;
2165 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
2166 shost->host_failed != scsi_host_busy(shost)) {
2167 SCSI_LOG_ERROR_RECOVERY(1,
2168 shost_printk(KERN_INFO, shost,
2169 "scsi_eh_%d: sleeping\n",
2170 shost->host_no));
2171 schedule();
2172 continue;
2175 __set_current_state(TASK_RUNNING);
2176 SCSI_LOG_ERROR_RECOVERY(1,
2177 shost_printk(KERN_INFO, shost,
2178 "scsi_eh_%d: waking up %d/%d/%d\n",
2179 shost->host_no, shost->host_eh_scheduled,
2180 shost->host_failed,
2181 scsi_host_busy(shost)));
2184 * We have a host that is failing for some reason. Figure out
2185 * what we need to do to get it up and online again (if we can).
2186 * If we fail, we end up taking the thing offline.
2188 if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
2189 SCSI_LOG_ERROR_RECOVERY(1,
2190 shost_printk(KERN_ERR, shost,
2191 "scsi_eh_%d: unable to autoresume\n",
2192 shost->host_no));
2193 continue;
2196 if (shost->transportt->eh_strategy_handler)
2197 shost->transportt->eh_strategy_handler(shost);
2198 else
2199 scsi_unjam_host(shost);
2201 /* All scmds have been handled */
2202 shost->host_failed = 0;
2205 * Note - if the above fails completely, the action is to take
2206 * individual devices offline and flush the queue of any
2207 * outstanding requests that may have been pending. When we
2208 * restart, we restart any I/O to any other devices on the bus
2209 * which are still online.
2211 scsi_restart_operations(shost);
2212 if (!shost->eh_noresume)
2213 scsi_autopm_put_host(shost);
2215 __set_current_state(TASK_RUNNING);
2217 SCSI_LOG_ERROR_RECOVERY(1,
2218 shost_printk(KERN_INFO, shost,
2219 "Error handler scsi_eh_%d exiting\n",
2220 shost->host_no));
2221 shost->ehandler = NULL;
2222 return 0;
2226 * Function: scsi_report_bus_reset()
2228 * Purpose: Utility function used by low-level drivers to report that
2229 * they have observed a bus reset on the bus being handled.
2231 * Arguments: shost - Host in question
2232 * channel - channel on which reset was observed.
2234 * Returns: Nothing
2236 * Lock status: Host lock must be held.
2238 * Notes: This only needs to be called if the reset is one which
2239 * originates from an unknown location. Resets originated
2240 * by the mid-level itself don't need to call this, but there
2241 * should be no harm.
2243 * The main purpose of this is to make sure that a CHECK_CONDITION
2244 * is properly treated.
2246 void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
2248 struct scsi_device *sdev;
2250 __shost_for_each_device(sdev, shost) {
2251 if (channel == sdev_channel(sdev))
2252 __scsi_report_device_reset(sdev, NULL);
2255 EXPORT_SYMBOL(scsi_report_bus_reset);
2258 * Function: scsi_report_device_reset()
2260 * Purpose: Utility function used by low-level drivers to report that
2261 * they have observed a device reset on the device being handled.
2263 * Arguments: shost - Host in question
2264 * channel - channel on which reset was observed
2265 * target - target on which reset was observed
2267 * Returns: Nothing
2269 * Lock status: Host lock must be held
2271 * Notes: This only needs to be called if the reset is one which
2272 * originates from an unknown location. Resets originated
2273 * by the mid-level itself don't need to call this, but there
2274 * should be no harm.
2276 * The main purpose of this is to make sure that a CHECK_CONDITION
2277 * is properly treated.
2279 void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
2281 struct scsi_device *sdev;
2283 __shost_for_each_device(sdev, shost) {
2284 if (channel == sdev_channel(sdev) &&
2285 target == sdev_id(sdev))
2286 __scsi_report_device_reset(sdev, NULL);
2289 EXPORT_SYMBOL(scsi_report_device_reset);
2291 static void
2292 scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
2297 * scsi_ioctl_reset: explicitly reset a host/bus/target/device
2298 * @dev: scsi_device to operate on
2299 * @arg: reset type (see sg.h)
2302 scsi_ioctl_reset(struct scsi_device *dev, int __user *arg)
2304 struct scsi_cmnd *scmd;
2305 struct Scsi_Host *shost = dev->host;
2306 struct request *rq;
2307 unsigned long flags;
2308 int error = 0, rtn, val;
2310 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2311 return -EACCES;
2313 error = get_user(val, arg);
2314 if (error)
2315 return error;
2317 if (scsi_autopm_get_host(shost) < 0)
2318 return -EIO;
2320 error = -EIO;
2321 rq = kzalloc(sizeof(struct request) + sizeof(struct scsi_cmnd) +
2322 shost->hostt->cmd_size, GFP_KERNEL);
2323 if (!rq)
2324 goto out_put_autopm_host;
2325 blk_rq_init(NULL, rq);
2327 scmd = (struct scsi_cmnd *)(rq + 1);
2328 scsi_init_command(dev, scmd);
2329 scmd->request = rq;
2330 scmd->cmnd = scsi_req(rq)->cmd;
2332 scmd->scsi_done = scsi_reset_provider_done_command;
2333 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
2335 scmd->cmd_len = 0;
2337 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
2339 spin_lock_irqsave(shost->host_lock, flags);
2340 shost->tmf_in_progress = 1;
2341 spin_unlock_irqrestore(shost->host_lock, flags);
2343 switch (val & ~SG_SCSI_RESET_NO_ESCALATE) {
2344 case SG_SCSI_RESET_NOTHING:
2345 rtn = SUCCESS;
2346 break;
2347 case SG_SCSI_RESET_DEVICE:
2348 rtn = scsi_try_bus_device_reset(scmd);
2349 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
2350 break;
2351 /* FALLTHROUGH */
2352 case SG_SCSI_RESET_TARGET:
2353 rtn = scsi_try_target_reset(scmd);
2354 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
2355 break;
2356 /* FALLTHROUGH */
2357 case SG_SCSI_RESET_BUS:
2358 rtn = scsi_try_bus_reset(scmd);
2359 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
2360 break;
2361 /* FALLTHROUGH */
2362 case SG_SCSI_RESET_HOST:
2363 rtn = scsi_try_host_reset(scmd);
2364 if (rtn == SUCCESS)
2365 break;
2366 /* FALLTHROUGH */
2367 default:
2368 rtn = FAILED;
2369 break;
2372 error = (rtn == SUCCESS) ? 0 : -EIO;
2374 spin_lock_irqsave(shost->host_lock, flags);
2375 shost->tmf_in_progress = 0;
2376 spin_unlock_irqrestore(shost->host_lock, flags);
2379 * be sure to wake up anyone who was sleeping or had their queue
2380 * suspended while we performed the TMF.
2382 SCSI_LOG_ERROR_RECOVERY(3,
2383 shost_printk(KERN_INFO, shost,
2384 "waking up host to restart after TMF\n"));
2386 wake_up(&shost->host_wait);
2387 scsi_run_host_queues(shost);
2389 scsi_put_command(scmd);
2390 kfree(rq);
2392 out_put_autopm_host:
2393 scsi_autopm_put_host(shost);
2394 return error;
2396 EXPORT_SYMBOL(scsi_ioctl_reset);
2398 bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd,
2399 struct scsi_sense_hdr *sshdr)
2401 return scsi_normalize_sense(cmd->sense_buffer,
2402 SCSI_SENSE_BUFFERSIZE, sshdr);
2404 EXPORT_SYMBOL(scsi_command_normalize_sense);
2407 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
2408 * @sense_buffer: byte array of sense data
2409 * @sb_len: number of valid bytes in sense_buffer
2410 * @info_out: pointer to 64 integer where 8 or 4 byte information
2411 * field will be placed if found.
2413 * Return value:
2414 * true if information field found, false if not found.
2416 bool scsi_get_sense_info_fld(const u8 *sense_buffer, int sb_len,
2417 u64 *info_out)
2419 const u8 * ucp;
2421 if (sb_len < 7)
2422 return false;
2423 switch (sense_buffer[0] & 0x7f) {
2424 case 0x70:
2425 case 0x71:
2426 if (sense_buffer[0] & 0x80) {
2427 *info_out = get_unaligned_be32(&sense_buffer[3]);
2428 return true;
2430 return false;
2431 case 0x72:
2432 case 0x73:
2433 ucp = scsi_sense_desc_find(sense_buffer, sb_len,
2434 0 /* info desc */);
2435 if (ucp && (0xa == ucp[1])) {
2436 *info_out = get_unaligned_be64(&ucp[4]);
2437 return true;
2439 return false;
2440 default:
2441 return false;
2444 EXPORT_SYMBOL(scsi_get_sense_info_fld);