1 // SPDX-License-Identifier: GPL-2.0-only
3 * scsi_error.c Copyright (C) 1997 Eric Youngdale
5 * SCSI error/timeout handling
6 * Initial versions: Eric Youngdale. Based upon conversations with
7 * Leonard Zubkoff and David Miller at Linux Expo,
8 * ideas originating from all over the place.
10 * Restructured scsi_unjam_host and associated functions.
11 * September 04, 2002 Mike Anderson (andmike@us.ibm.com)
13 * Forward port of Russell King's (rmk@arm.linux.org.uk) changes and
15 * September 30, 2002 Mike Anderson (andmike@us.ibm.com)
18 #include <linux/module.h>
19 #include <linux/sched.h>
20 #include <linux/gfp.h>
21 #include <linux/timer.h>
22 #include <linux/string.h>
23 #include <linux/kernel.h>
24 #include <linux/freezer.h>
25 #include <linux/kthread.h>
26 #include <linux/interrupt.h>
27 #include <linux/blkdev.h>
28 #include <linux/delay.h>
29 #include <linux/jiffies.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <scsi/scsi_dbg.h>
34 #include <scsi/scsi_device.h>
35 #include <scsi/scsi_driver.h>
36 #include <scsi/scsi_eh.h>
37 #include <scsi/scsi_common.h>
38 #include <scsi/scsi_transport.h>
39 #include <scsi/scsi_host.h>
40 #include <scsi/scsi_ioctl.h>
41 #include <scsi/scsi_dh.h>
42 #include <scsi/scsi_devinfo.h>
45 #include "scsi_priv.h"
46 #include "scsi_logging.h"
47 #include "scsi_transport_api.h"
49 #include <trace/events/scsi.h>
51 #include <asm/unaligned.h>
53 static void scsi_eh_done(struct scsi_cmnd *scmd);
56 * These should *probably* be handled by the host itself.
57 * Since it is allowed to sleep, it probably should.
59 #define BUS_RESET_SETTLE_TIME (10)
60 #define HOST_RESET_SETTLE_TIME (10)
62 static int scsi_eh_try_stu(struct scsi_cmnd *scmd);
63 static enum scsi_disposition scsi_try_to_abort_cmd(struct scsi_host_template *,
66 void scsi_eh_wakeup(struct Scsi_Host *shost)
68 lockdep_assert_held(shost->host_lock);
70 if (scsi_host_busy(shost) == shost->host_failed) {
71 trace_scsi_eh_wakeup(shost);
72 wake_up_process(shost->ehandler);
73 SCSI_LOG_ERROR_RECOVERY(5, shost_printk(KERN_INFO, shost,
74 "Waking error handler thread\n"));
79 * scsi_schedule_eh - schedule EH for SCSI host
80 * @shost: SCSI host to invoke error handling on.
82 * Schedule SCSI EH without scmd.
84 void scsi_schedule_eh(struct Scsi_Host *shost)
88 spin_lock_irqsave(shost->host_lock, flags);
90 if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
91 scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
92 shost->host_eh_scheduled++;
93 scsi_eh_wakeup(shost);
96 spin_unlock_irqrestore(shost->host_lock, flags);
98 EXPORT_SYMBOL_GPL(scsi_schedule_eh);
100 static int scsi_host_eh_past_deadline(struct Scsi_Host *shost)
102 if (!shost->last_reset || shost->eh_deadline == -1)
106 * 32bit accesses are guaranteed to be atomic
107 * (on all supported architectures), so instead
108 * of using a spinlock we can as well double check
109 * if eh_deadline has been set to 'off' during the
112 if (time_before(jiffies, shost->last_reset + shost->eh_deadline) &&
113 shost->eh_deadline > -1)
119 static bool scsi_cmd_retry_allowed(struct scsi_cmnd *cmd)
121 if (cmd->allowed == SCSI_CMD_RETRIES_NO_LIMIT)
124 return ++cmd->retries <= cmd->allowed;
127 static bool scsi_eh_should_retry_cmd(struct scsi_cmnd *cmd)
129 struct scsi_device *sdev = cmd->device;
130 struct Scsi_Host *host = sdev->host;
132 if (host->hostt->eh_should_retry_cmd)
133 return host->hostt->eh_should_retry_cmd(cmd);
138 static void scsi_eh_complete_abort(struct scsi_cmnd *scmd, struct Scsi_Host *shost)
142 spin_lock_irqsave(shost->host_lock, flags);
143 list_del_init(&scmd->eh_entry);
145 * If the abort succeeds, and there is no further
146 * EH action, clear the ->last_reset time.
148 if (list_empty(&shost->eh_abort_list) &&
149 list_empty(&shost->eh_cmd_q))
150 if (shost->eh_deadline != -1)
151 shost->last_reset = 0;
152 spin_unlock_irqrestore(shost->host_lock, flags);
156 * scmd_eh_abort_handler - Handle command aborts
157 * @work: command to be aborted.
159 * Note: this function must be called only for a command that has timed out.
160 * Because the block layer marks a request as complete before it calls
161 * scsi_times_out(), a .scsi_done() call from the LLD for a command that has
162 * timed out do not have any effect. Hence it is safe to call
163 * scsi_finish_command() from this function.
166 scmd_eh_abort_handler(struct work_struct *work)
168 struct scsi_cmnd *scmd =
169 container_of(work, struct scsi_cmnd, abort_work.work);
170 struct scsi_device *sdev = scmd->device;
171 enum scsi_disposition rtn;
174 if (scsi_host_eh_past_deadline(sdev->host)) {
175 SCSI_LOG_ERROR_RECOVERY(3,
176 scmd_printk(KERN_INFO, scmd,
177 "eh timeout, not aborting\n"));
179 SCSI_LOG_ERROR_RECOVERY(3,
180 scmd_printk(KERN_INFO, scmd,
181 "aborting command\n"));
182 rtn = scsi_try_to_abort_cmd(sdev->host->hostt, scmd);
183 if (rtn == SUCCESS) {
184 set_host_byte(scmd, DID_TIME_OUT);
185 if (scsi_host_eh_past_deadline(sdev->host)) {
186 SCSI_LOG_ERROR_RECOVERY(3,
187 scmd_printk(KERN_INFO, scmd,
188 "eh timeout, not retrying "
189 "aborted command\n"));
190 } else if (!scsi_noretry_cmd(scmd) &&
191 scsi_cmd_retry_allowed(scmd) &&
192 scsi_eh_should_retry_cmd(scmd)) {
193 SCSI_LOG_ERROR_RECOVERY(3,
194 scmd_printk(KERN_WARNING, scmd,
195 "retry aborted command\n"));
196 scsi_eh_complete_abort(scmd, sdev->host);
197 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
200 SCSI_LOG_ERROR_RECOVERY(3,
201 scmd_printk(KERN_WARNING, scmd,
202 "finish aborted command\n"));
203 scsi_eh_complete_abort(scmd, sdev->host);
204 scsi_finish_command(scmd);
208 SCSI_LOG_ERROR_RECOVERY(3,
209 scmd_printk(KERN_INFO, scmd,
211 (rtn == FAST_IO_FAIL) ?
212 "not send" : "failed"));
216 spin_lock_irqsave(sdev->host->host_lock, flags);
217 list_del_init(&scmd->eh_entry);
218 spin_unlock_irqrestore(sdev->host->host_lock, flags);
219 scsi_eh_scmd_add(scmd);
223 * scsi_abort_command - schedule a command abort
224 * @scmd: scmd to abort.
226 * We only need to abort commands after a command timeout
229 scsi_abort_command(struct scsi_cmnd *scmd)
231 struct scsi_device *sdev = scmd->device;
232 struct Scsi_Host *shost = sdev->host;
235 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
237 * Retry after abort failed, escalate to next level.
239 SCSI_LOG_ERROR_RECOVERY(3,
240 scmd_printk(KERN_INFO, scmd,
241 "previous abort failed\n"));
242 BUG_ON(delayed_work_pending(&scmd->abort_work));
246 spin_lock_irqsave(shost->host_lock, flags);
247 if (shost->eh_deadline != -1 && !shost->last_reset)
248 shost->last_reset = jiffies;
249 BUG_ON(!list_empty(&scmd->eh_entry));
250 list_add_tail(&scmd->eh_entry, &shost->eh_abort_list);
251 spin_unlock_irqrestore(shost->host_lock, flags);
253 scmd->eh_eflags |= SCSI_EH_ABORT_SCHEDULED;
254 SCSI_LOG_ERROR_RECOVERY(3,
255 scmd_printk(KERN_INFO, scmd, "abort scheduled\n"));
256 queue_delayed_work(shost->tmf_work_q, &scmd->abort_work, HZ / 100);
261 * scsi_eh_reset - call into ->eh_action to reset internal counters
262 * @scmd: scmd to run eh on.
264 * The scsi driver might be carrying internal state about the
265 * devices, so we need to call into the driver to reset the
266 * internal state once the error handler is started.
268 static void scsi_eh_reset(struct scsi_cmnd *scmd)
270 if (!blk_rq_is_passthrough(scsi_cmd_to_rq(scmd))) {
271 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
273 sdrv->eh_reset(scmd);
277 static void scsi_eh_inc_host_failed(struct rcu_head *head)
279 struct scsi_cmnd *scmd = container_of(head, typeof(*scmd), rcu);
280 struct Scsi_Host *shost = scmd->device->host;
283 spin_lock_irqsave(shost->host_lock, flags);
284 shost->host_failed++;
285 scsi_eh_wakeup(shost);
286 spin_unlock_irqrestore(shost->host_lock, flags);
290 * scsi_eh_scmd_add - add scsi cmd to error handling.
291 * @scmd: scmd to run eh on.
293 void scsi_eh_scmd_add(struct scsi_cmnd *scmd)
295 struct Scsi_Host *shost = scmd->device->host;
299 WARN_ON_ONCE(!shost->ehandler);
301 spin_lock_irqsave(shost->host_lock, flags);
302 if (scsi_host_set_state(shost, SHOST_RECOVERY)) {
303 ret = scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY);
306 if (shost->eh_deadline != -1 && !shost->last_reset)
307 shost->last_reset = jiffies;
310 list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
311 spin_unlock_irqrestore(shost->host_lock, flags);
313 * Ensure that all tasks observe the host state change before the
314 * host_failed change.
316 call_rcu(&scmd->rcu, scsi_eh_inc_host_failed);
320 * scsi_times_out - Timeout function for normal scsi commands.
321 * @req: request that is timing out.
324 * We do not need to lock this. There is the potential for a race
325 * only in that the normal completion handling might run, but if the
326 * normal completion function determines that the timer has already
327 * fired, then it mustn't do anything.
329 enum blk_eh_timer_return scsi_times_out(struct request *req)
331 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
332 enum blk_eh_timer_return rtn = BLK_EH_DONE;
333 struct Scsi_Host *host = scmd->device->host;
335 trace_scsi_dispatch_cmd_timeout(scmd);
336 scsi_log_completion(scmd, TIMEOUT_ERROR);
338 if (host->eh_deadline != -1 && !host->last_reset)
339 host->last_reset = jiffies;
341 if (host->hostt->eh_timed_out)
342 rtn = host->hostt->eh_timed_out(scmd);
344 if (rtn == BLK_EH_DONE) {
346 * If scsi_done() has already set SCMD_STATE_COMPLETE, do not
349 if (test_and_set_bit(SCMD_STATE_COMPLETE, &scmd->state))
351 if (scsi_abort_command(scmd) != SUCCESS) {
352 set_host_byte(scmd, DID_TIME_OUT);
353 scsi_eh_scmd_add(scmd);
361 * scsi_block_when_processing_errors - Prevent cmds from being queued.
362 * @sdev: Device on which we are performing recovery.
365 * We block until the host is out of error recovery, and then check to
366 * see whether the host or the device is offline.
369 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
371 int scsi_block_when_processing_errors(struct scsi_device *sdev)
375 wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
377 online = scsi_device_online(sdev);
381 EXPORT_SYMBOL(scsi_block_when_processing_errors);
383 #ifdef CONFIG_SCSI_LOGGING
385 * scsi_eh_prt_fail_stats - Log info on failures.
386 * @shost: scsi host being recovered.
387 * @work_q: Queue of scsi cmds to process.
389 static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
390 struct list_head *work_q)
392 struct scsi_cmnd *scmd;
393 struct scsi_device *sdev;
394 int total_failures = 0;
397 int devices_failed = 0;
399 shost_for_each_device(sdev, shost) {
400 list_for_each_entry(scmd, work_q, eh_entry) {
401 if (scmd->device == sdev) {
403 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED)
410 if (cmd_cancel || cmd_failed) {
411 SCSI_LOG_ERROR_RECOVERY(3,
412 shost_printk(KERN_INFO, shost,
413 "%s: cmds failed: %d, cancel: %d\n",
414 __func__, cmd_failed,
422 SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO, shost,
423 "Total of %d commands on %d"
424 " devices require eh work\n",
425 total_failures, devices_failed));
430 * scsi_report_lun_change - Set flag on all *other* devices on the same target
431 * to indicate that a UNIT ATTENTION is expected.
432 * @sdev: Device reporting the UNIT ATTENTION
434 static void scsi_report_lun_change(struct scsi_device *sdev)
436 sdev->sdev_target->expecting_lun_change = 1;
440 * scsi_report_sense - Examine scsi sense information and log messages for
441 * certain conditions, also issue uevents for some of them.
442 * @sdev: Device reporting the sense code
443 * @sshdr: sshdr to be examined
445 static void scsi_report_sense(struct scsi_device *sdev,
446 struct scsi_sense_hdr *sshdr)
448 enum scsi_device_event evt_type = SDEV_EVT_MAXBITS; /* i.e. none */
450 if (sshdr->sense_key == UNIT_ATTENTION) {
451 if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) {
452 evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED;
453 sdev_printk(KERN_WARNING, sdev,
454 "Inquiry data has changed");
455 } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) {
456 evt_type = SDEV_EVT_LUN_CHANGE_REPORTED;
457 scsi_report_lun_change(sdev);
458 sdev_printk(KERN_WARNING, sdev,
459 "Warning! Received an indication that the "
460 "LUN assignments on this target have "
461 "changed. The Linux SCSI layer does not "
462 "automatically remap LUN assignments.\n");
463 } else if (sshdr->asc == 0x3f)
464 sdev_printk(KERN_WARNING, sdev,
465 "Warning! Received an indication that the "
466 "operating parameters on this target have "
467 "changed. The Linux SCSI layer does not "
468 "automatically adjust these parameters.\n");
470 if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) {
471 evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED;
472 sdev_printk(KERN_WARNING, sdev,
473 "Warning! Received an indication that the "
474 "LUN reached a thin provisioning soft "
478 if (sshdr->asc == 0x29) {
479 evt_type = SDEV_EVT_POWER_ON_RESET_OCCURRED;
481 * Do not print message if it is an expected side-effect
484 if (!sdev->silence_suspend)
485 sdev_printk(KERN_WARNING, sdev,
486 "Power-on or device reset occurred\n");
489 if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) {
490 evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED;
491 sdev_printk(KERN_WARNING, sdev,
492 "Mode parameters changed");
493 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x06) {
494 evt_type = SDEV_EVT_ALUA_STATE_CHANGE_REPORTED;
495 sdev_printk(KERN_WARNING, sdev,
496 "Asymmetric access state changed");
497 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) {
498 evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED;
499 sdev_printk(KERN_WARNING, sdev,
500 "Capacity data has changed");
501 } else if (sshdr->asc == 0x2a)
502 sdev_printk(KERN_WARNING, sdev,
503 "Parameters changed");
506 if (evt_type != SDEV_EVT_MAXBITS) {
507 set_bit(evt_type, sdev->pending_events);
508 schedule_work(&sdev->event_work);
513 * scsi_check_sense - Examine scsi cmd sense
514 * @scmd: Cmd to have sense checked.
517 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
520 * When a deferred error is detected the current command has
521 * not been executed and needs retrying.
523 enum scsi_disposition scsi_check_sense(struct scsi_cmnd *scmd)
525 struct scsi_device *sdev = scmd->device;
526 struct scsi_sense_hdr sshdr;
528 if (! scsi_command_normalize_sense(scmd, &sshdr))
529 return FAILED; /* no valid sense data */
531 scsi_report_sense(sdev, &sshdr);
533 if (scsi_sense_is_deferred(&sshdr))
536 if (sdev->handler && sdev->handler->check_sense) {
537 enum scsi_disposition rc;
539 rc = sdev->handler->check_sense(sdev, &sshdr);
540 if (rc != SCSI_RETURN_NOT_HANDLED)
542 /* handler does not care. Drop down to default handling */
545 if (scmd->cmnd[0] == TEST_UNIT_READY && scmd->scsi_done != scsi_eh_done)
547 * nasty: for mid-layer issued TURs, we need to return the
548 * actual sense data without any recovery attempt. For eh
549 * issued ones, we need to try to recover and interpret
554 * Previous logic looked for FILEMARK, EOM or ILI which are
555 * mainly associated with tapes and returned SUCCESS.
557 if (sshdr.response_code == 0x70) {
559 if (scmd->sense_buffer[2] & 0xe0)
563 * descriptor format: look for "stream commands sense data
564 * descriptor" (see SSC-3). Assume single sense data
565 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
567 if ((sshdr.additional_length > 3) &&
568 (scmd->sense_buffer[8] == 0x4) &&
569 (scmd->sense_buffer[11] & 0xe0))
573 switch (sshdr.sense_key) {
576 case RECOVERED_ERROR:
577 return /* soft_error */ SUCCESS;
579 case ABORTED_COMMAND:
580 if (sshdr.asc == 0x10) /* DIF */
583 if (sshdr.asc == 0x44 && sdev->sdev_bflags & BLIST_RETRY_ITF)
584 return ADD_TO_MLQUEUE;
585 if (sshdr.asc == 0xc1 && sshdr.ascq == 0x01 &&
586 sdev->sdev_bflags & BLIST_RETRY_ASC_C1)
587 return ADD_TO_MLQUEUE;
593 * if we are expecting a cc/ua because of a bus reset that we
594 * performed, treat this just as a retry. otherwise this is
595 * information that we should pass up to the upper-level driver
596 * so that we can deal with it there.
598 if (scmd->device->expecting_cc_ua) {
600 * Because some device does not queue unit
601 * attentions correctly, we carefully check
602 * additional sense code and qualifier so as
603 * not to squash media change unit attention.
605 if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) {
606 scmd->device->expecting_cc_ua = 0;
611 * we might also expect a cc/ua if another LUN on the target
612 * reported a UA with an ASC/ASCQ of 3F 0E -
613 * REPORTED LUNS DATA HAS CHANGED.
615 if (scmd->device->sdev_target->expecting_lun_change &&
616 sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
619 * if the device is in the process of becoming ready, we
622 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
625 * if the device is not started, we need to wake
626 * the error handler to start the motor
628 if (scmd->device->allow_restart &&
629 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
632 * Pass the UA upwards for a determination in the completion
637 /* these are not supported */
639 if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
640 /* Thin provisioning hard threshold reached */
641 set_host_byte(scmd, DID_ALLOC_FAILURE);
646 case VOLUME_OVERFLOW:
649 set_host_byte(scmd, DID_TARGET_FAILURE);
653 if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
654 sshdr.asc == 0x13 || /* AMNF DATA FIELD */
655 sshdr.asc == 0x14) { /* RECORD NOT FOUND */
656 set_host_byte(scmd, DID_MEDIUM_ERROR);
662 if (scmd->device->retry_hwerror)
663 return ADD_TO_MLQUEUE;
665 set_host_byte(scmd, DID_TARGET_FAILURE);
668 case ILLEGAL_REQUEST:
669 if (sshdr.asc == 0x20 || /* Invalid command operation code */
670 sshdr.asc == 0x21 || /* Logical block address out of range */
671 sshdr.asc == 0x22 || /* Invalid function */
672 sshdr.asc == 0x24 || /* Invalid field in cdb */
673 sshdr.asc == 0x26 || /* Parameter value invalid */
674 sshdr.asc == 0x27) { /* Write protected */
675 set_host_byte(scmd, DID_TARGET_FAILURE);
683 EXPORT_SYMBOL_GPL(scsi_check_sense);
685 static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
687 struct scsi_host_template *sht = sdev->host->hostt;
688 struct scsi_device *tmp_sdev;
690 if (!sht->track_queue_depth ||
691 sdev->queue_depth >= sdev->max_queue_depth)
694 if (time_before(jiffies,
695 sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
698 if (time_before(jiffies,
699 sdev->last_queue_full_time + sdev->queue_ramp_up_period))
703 * Walk all devices of a target and do
706 shost_for_each_device(tmp_sdev, sdev->host) {
707 if (tmp_sdev->channel != sdev->channel ||
708 tmp_sdev->id != sdev->id ||
709 tmp_sdev->queue_depth == sdev->max_queue_depth)
712 scsi_change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1);
713 sdev->last_queue_ramp_up = jiffies;
717 static void scsi_handle_queue_full(struct scsi_device *sdev)
719 struct scsi_host_template *sht = sdev->host->hostt;
720 struct scsi_device *tmp_sdev;
722 if (!sht->track_queue_depth)
725 shost_for_each_device(tmp_sdev, sdev->host) {
726 if (tmp_sdev->channel != sdev->channel ||
727 tmp_sdev->id != sdev->id)
730 * We do not know the number of commands that were at
731 * the device when we got the queue full so we start
732 * from the highest possible value and work our way down.
734 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
739 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
740 * @scmd: SCSI cmd to examine.
743 * This is *only* called when we are examining the status of commands
744 * queued during error recovery. the main difference here is that we
745 * don't allow for the possibility of retries here, and we are a lot
746 * more restrictive about what we consider acceptable.
748 static enum scsi_disposition scsi_eh_completed_normally(struct scsi_cmnd *scmd)
751 * first check the host byte, to see if there is anything in there
752 * that would indicate what we need to do.
754 if (host_byte(scmd->result) == DID_RESET) {
756 * rats. we are already in the error handler, so we now
757 * get to try and figure out what to do next. if the sense
758 * is valid, we have a pretty good idea of what to do.
759 * if not, we mark it as FAILED.
761 return scsi_check_sense(scmd);
763 if (host_byte(scmd->result) != DID_OK)
767 * now, check the status byte to see if this indicates
770 switch (get_status_byte(scmd)) {
772 scsi_handle_queue_ramp_up(scmd->device);
774 case SAM_STAT_COMMAND_TERMINATED:
776 case SAM_STAT_CHECK_CONDITION:
777 return scsi_check_sense(scmd);
778 case SAM_STAT_CONDITION_MET:
779 case SAM_STAT_INTERMEDIATE:
780 case SAM_STAT_INTERMEDIATE_CONDITION_MET:
782 * who knows? FIXME(eric)
785 case SAM_STAT_RESERVATION_CONFLICT:
786 if (scmd->cmnd[0] == TEST_UNIT_READY)
787 /* it is a success, we probed the device and
790 /* otherwise, we failed to send the command */
792 case SAM_STAT_TASK_SET_FULL:
793 scsi_handle_queue_full(scmd->device);
804 * scsi_eh_done - Completion function for error handling.
805 * @scmd: Cmd that is done.
807 static void scsi_eh_done(struct scsi_cmnd *scmd)
809 struct completion *eh_action;
811 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
812 "%s result: %x\n", __func__, scmd->result));
814 eh_action = scmd->device->host->eh_action;
820 * scsi_try_host_reset - ask host adapter to reset itself
821 * @scmd: SCSI cmd to send host reset.
823 static enum scsi_disposition scsi_try_host_reset(struct scsi_cmnd *scmd)
826 enum scsi_disposition rtn;
827 struct Scsi_Host *host = scmd->device->host;
828 struct scsi_host_template *hostt = host->hostt;
830 SCSI_LOG_ERROR_RECOVERY(3,
831 shost_printk(KERN_INFO, host, "Snd Host RST\n"));
833 if (!hostt->eh_host_reset_handler)
836 rtn = hostt->eh_host_reset_handler(scmd);
838 if (rtn == SUCCESS) {
839 if (!hostt->skip_settle_delay)
840 ssleep(HOST_RESET_SETTLE_TIME);
841 spin_lock_irqsave(host->host_lock, flags);
842 scsi_report_bus_reset(host, scmd_channel(scmd));
843 spin_unlock_irqrestore(host->host_lock, flags);
850 * scsi_try_bus_reset - ask host to perform a bus reset
851 * @scmd: SCSI cmd to send bus reset.
853 static enum scsi_disposition scsi_try_bus_reset(struct scsi_cmnd *scmd)
856 enum scsi_disposition rtn;
857 struct Scsi_Host *host = scmd->device->host;
858 struct scsi_host_template *hostt = host->hostt;
860 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
861 "%s: Snd Bus RST\n", __func__));
863 if (!hostt->eh_bus_reset_handler)
866 rtn = hostt->eh_bus_reset_handler(scmd);
868 if (rtn == SUCCESS) {
869 if (!hostt->skip_settle_delay)
870 ssleep(BUS_RESET_SETTLE_TIME);
871 spin_lock_irqsave(host->host_lock, flags);
872 scsi_report_bus_reset(host, scmd_channel(scmd));
873 spin_unlock_irqrestore(host->host_lock, flags);
879 static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
882 sdev->expecting_cc_ua = 1;
886 * scsi_try_target_reset - Ask host to perform a target reset
887 * @scmd: SCSI cmd used to send a target reset
890 * There is no timeout for this operation. if this operation is
891 * unreliable for a given host, then the host itself needs to put a
892 * timer on it, and set the host back to a consistent state prior to
895 static enum scsi_disposition scsi_try_target_reset(struct scsi_cmnd *scmd)
898 enum scsi_disposition rtn;
899 struct Scsi_Host *host = scmd->device->host;
900 struct scsi_host_template *hostt = host->hostt;
902 if (!hostt->eh_target_reset_handler)
905 rtn = hostt->eh_target_reset_handler(scmd);
906 if (rtn == SUCCESS) {
907 spin_lock_irqsave(host->host_lock, flags);
908 __starget_for_each_device(scsi_target(scmd->device), NULL,
909 __scsi_report_device_reset);
910 spin_unlock_irqrestore(host->host_lock, flags);
917 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
918 * @scmd: SCSI cmd used to send BDR
921 * There is no timeout for this operation. if this operation is
922 * unreliable for a given host, then the host itself needs to put a
923 * timer on it, and set the host back to a consistent state prior to
926 static enum scsi_disposition scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
928 enum scsi_disposition rtn;
929 struct scsi_host_template *hostt = scmd->device->host->hostt;
931 if (!hostt->eh_device_reset_handler)
934 rtn = hostt->eh_device_reset_handler(scmd);
936 __scsi_report_device_reset(scmd->device, NULL);
941 * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
942 * @hostt: SCSI driver host template
943 * @scmd: SCSI cmd used to send a target reset
946 * SUCCESS, FAILED, or FAST_IO_FAIL
949 * SUCCESS does not necessarily indicate that the command
950 * has been aborted; it only indicates that the LLDDs
951 * has cleared all references to that command.
952 * LLDDs should return FAILED only if an abort was required
953 * but could not be executed. LLDDs should return FAST_IO_FAIL
954 * if the device is temporarily unavailable (eg due to a
955 * link down on FibreChannel)
957 static enum scsi_disposition
958 scsi_try_to_abort_cmd(struct scsi_host_template *hostt, struct scsi_cmnd *scmd)
960 if (!hostt->eh_abort_handler)
963 return hostt->eh_abort_handler(scmd);
966 static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
968 if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
969 if (scsi_try_bus_device_reset(scmd) != SUCCESS)
970 if (scsi_try_target_reset(scmd) != SUCCESS)
971 if (scsi_try_bus_reset(scmd) != SUCCESS)
972 scsi_try_host_reset(scmd);
976 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
977 * @scmd: SCSI command structure to hijack
978 * @ses: structure to save restore information
979 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
980 * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
981 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
983 * This function is used to save a scsi command information before re-execution
984 * as part of the error recovery process. If @sense_bytes is 0 the command
985 * sent must be one that does not transfer any data. If @sense_bytes != 0
986 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
987 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
989 void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
990 unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
992 struct scsi_device *sdev = scmd->device;
995 * We need saved copies of a number of fields - this is because
996 * error handling may need to overwrite these with different values
997 * to run different commands, and once error handling is complete,
998 * we will need to restore these values prior to running the actual
1001 ses->cmd_len = scmd->cmd_len;
1002 ses->cmnd = scmd->cmnd;
1003 ses->data_direction = scmd->sc_data_direction;
1004 ses->sdb = scmd->sdb;
1005 ses->result = scmd->result;
1006 ses->resid_len = scmd->req.resid_len;
1007 ses->underflow = scmd->underflow;
1008 ses->prot_op = scmd->prot_op;
1009 ses->eh_eflags = scmd->eh_eflags;
1011 scmd->prot_op = SCSI_PROT_NORMAL;
1012 scmd->eh_eflags = 0;
1013 scmd->cmnd = ses->eh_cmnd;
1014 memset(scmd->cmnd, 0, BLK_MAX_CDB);
1015 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
1017 scmd->req.resid_len = 0;
1020 scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
1022 sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
1024 scmd->sdb.table.sgl = &ses->sense_sgl;
1025 scmd->sc_data_direction = DMA_FROM_DEVICE;
1026 scmd->sdb.table.nents = scmd->sdb.table.orig_nents = 1;
1027 scmd->cmnd[0] = REQUEST_SENSE;
1028 scmd->cmnd[4] = scmd->sdb.length;
1029 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
1031 scmd->sc_data_direction = DMA_NONE;
1033 BUG_ON(cmnd_size > BLK_MAX_CDB);
1034 memcpy(scmd->cmnd, cmnd, cmnd_size);
1035 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
1039 scmd->underflow = 0;
1041 if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
1042 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
1043 (sdev->lun << 5 & 0xe0);
1046 * Zero the sense buffer. The scsi spec mandates that any
1047 * untransferred sense data should be interpreted as being zero.
1049 memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1051 EXPORT_SYMBOL(scsi_eh_prep_cmnd);
1054 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
1055 * @scmd: SCSI command structure to restore
1056 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
1058 * Undo any damage done by above scsi_eh_prep_cmnd().
1060 void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
1063 * Restore original data
1065 scmd->cmd_len = ses->cmd_len;
1066 scmd->cmnd = ses->cmnd;
1067 scmd->sc_data_direction = ses->data_direction;
1068 scmd->sdb = ses->sdb;
1069 scmd->result = ses->result;
1070 scmd->req.resid_len = ses->resid_len;
1071 scmd->underflow = ses->underflow;
1072 scmd->prot_op = ses->prot_op;
1073 scmd->eh_eflags = ses->eh_eflags;
1075 EXPORT_SYMBOL(scsi_eh_restore_cmnd);
1078 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
1079 * @scmd: SCSI command structure to hijack
1080 * @cmnd: CDB to send
1081 * @cmnd_size: size in bytes of @cmnd
1082 * @timeout: timeout for this request
1083 * @sense_bytes: size of sense data to copy or 0
1085 * This function is used to send a scsi command down to a target device
1086 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
1089 * SUCCESS or FAILED or NEEDS_RETRY
1091 static enum scsi_disposition scsi_send_eh_cmnd(struct scsi_cmnd *scmd,
1092 unsigned char *cmnd, int cmnd_size, int timeout, unsigned sense_bytes)
1094 struct scsi_device *sdev = scmd->device;
1095 struct Scsi_Host *shost = sdev->host;
1096 DECLARE_COMPLETION_ONSTACK(done);
1097 unsigned long timeleft = timeout, delay;
1098 struct scsi_eh_save ses;
1099 const unsigned long stall_for = msecs_to_jiffies(100);
1103 scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
1104 shost->eh_action = &done;
1106 scsi_log_send(scmd);
1107 scmd->scsi_done = scsi_eh_done;
1110 * Lock sdev->state_mutex to avoid that scsi_device_quiesce() can
1111 * change the SCSI device state after we have examined it and before
1112 * .queuecommand() is called.
1114 mutex_lock(&sdev->state_mutex);
1115 while (sdev->sdev_state == SDEV_BLOCK && timeleft > 0) {
1116 mutex_unlock(&sdev->state_mutex);
1117 SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_DEBUG, sdev,
1118 "%s: state %d <> %d\n", __func__, sdev->sdev_state,
1120 delay = min(timeleft, stall_for);
1122 msleep(jiffies_to_msecs(delay));
1123 mutex_lock(&sdev->state_mutex);
1125 if (sdev->sdev_state != SDEV_BLOCK)
1126 rtn = shost->hostt->queuecommand(shost, scmd);
1129 mutex_unlock(&sdev->state_mutex);
1132 if (timeleft > stall_for) {
1133 scsi_eh_restore_cmnd(scmd, &ses);
1134 timeleft -= stall_for;
1135 msleep(jiffies_to_msecs(stall_for));
1138 /* signal not to enter either branch of the if () below */
1142 timeleft = wait_for_completion_timeout(&done, timeout);
1146 shost->eh_action = NULL;
1148 scsi_log_completion(scmd, rtn);
1150 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1151 "%s timeleft: %ld\n",
1152 __func__, timeleft));
1155 * If there is time left scsi_eh_done got called, and we will examine
1156 * the actual status codes to see whether the command actually did
1157 * complete normally, else if we have a zero return and no time left,
1158 * the command must still be pending, so abort it and return FAILED.
1159 * If we never actually managed to issue the command, because
1160 * ->queuecommand() kept returning non zero, use the rtn = FAILED
1161 * value above (so don't execute either branch of the if)
1164 rtn = scsi_eh_completed_normally(scmd);
1165 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1166 "%s: scsi_eh_completed_normally %x\n", __func__, rtn));
1173 case ADD_TO_MLQUEUE:
1180 } else if (rtn != FAILED) {
1181 scsi_abort_eh_cmnd(scmd);
1185 scsi_eh_restore_cmnd(scmd, &ses);
1191 * scsi_request_sense - Request sense data from a particular target.
1192 * @scmd: SCSI cmd for request sense.
1195 * Some hosts automatically obtain this information, others require
1196 * that we obtain it on our own. This function will *not* return until
1197 * the command either times out, or it completes.
1199 static enum scsi_disposition scsi_request_sense(struct scsi_cmnd *scmd)
1201 return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
1204 static enum scsi_disposition
1205 scsi_eh_action(struct scsi_cmnd *scmd, enum scsi_disposition rtn)
1207 if (!blk_rq_is_passthrough(scsi_cmd_to_rq(scmd))) {
1208 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
1209 if (sdrv->eh_action)
1210 rtn = sdrv->eh_action(scmd, rtn);
1216 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
1217 * @scmd: Original SCSI cmd that eh has finished.
1218 * @done_q: Queue for processed commands.
1221 * We don't want to use the normal command completion while we are are
1222 * still handling errors - it may cause other commands to be queued,
1223 * and that would disturb what we are doing. Thus we really want to
1224 * keep a list of pending commands for final completion, and once we
1225 * are ready to leave error handling we handle completion for real.
1227 void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
1229 list_move_tail(&scmd->eh_entry, done_q);
1231 EXPORT_SYMBOL(scsi_eh_finish_cmd);
1234 * scsi_eh_get_sense - Get device sense data.
1235 * @work_q: Queue of commands to process.
1236 * @done_q: Queue of processed commands.
1239 * See if we need to request sense information. if so, then get it
1240 * now, so we have a better idea of what to do.
1243 * This has the unfortunate side effect that if a shost adapter does
1244 * not automatically request sense information, we end up shutting
1245 * it down before we request it.
1247 * All drivers should request sense information internally these days,
1248 * so for now all I have to say is tough noogies if you end up in here.
1250 * XXX: Long term this code should go away, but that needs an audit of
1253 int scsi_eh_get_sense(struct list_head *work_q,
1254 struct list_head *done_q)
1256 struct scsi_cmnd *scmd, *next;
1257 struct Scsi_Host *shost;
1258 enum scsi_disposition rtn;
1261 * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
1262 * should not get sense.
1264 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1265 if ((scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) ||
1266 SCSI_SENSE_VALID(scmd))
1269 shost = scmd->device->host;
1270 if (scsi_host_eh_past_deadline(shost)) {
1271 SCSI_LOG_ERROR_RECOVERY(3,
1272 scmd_printk(KERN_INFO, scmd,
1273 "%s: skip request sense, past eh deadline\n",
1277 if (!scsi_status_is_check_condition(scmd->result))
1279 * don't request sense if there's no check condition
1280 * status because the error we're processing isn't one
1281 * that has a sense code (and some devices get
1282 * confused by sense requests out of the blue)
1286 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
1287 "%s: requesting sense\n",
1289 rtn = scsi_request_sense(scmd);
1293 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1294 "sense requested, result %x\n", scmd->result));
1295 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd));
1297 rtn = scsi_decide_disposition(scmd);
1300 * if the result was normal, then just pass it along to the
1305 * We don't want this command reissued, just finished
1306 * with the sense data, so set retries to the max
1307 * allowed to ensure it won't get reissued. If the user
1308 * has requested infinite retries, we also want to
1309 * finish this command, so force completion by setting
1310 * retries and allowed to the same value.
1312 if (scmd->allowed == SCSI_CMD_RETRIES_NO_LIMIT)
1313 scmd->retries = scmd->allowed = 1;
1315 scmd->retries = scmd->allowed;
1316 else if (rtn != NEEDS_RETRY)
1319 scsi_eh_finish_cmd(scmd, done_q);
1322 return list_empty(work_q);
1324 EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
1327 * scsi_eh_tur - Send TUR to device.
1328 * @scmd: &scsi_cmnd to send TUR
1331 * 0 - Device is ready. 1 - Device NOT ready.
1333 static int scsi_eh_tur(struct scsi_cmnd *scmd)
1335 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
1337 enum scsi_disposition rtn;
1340 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
1341 scmd->device->eh_timeout, 0);
1343 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1344 "%s return: %x\n", __func__, rtn));
1359 * scsi_eh_test_devices - check if devices are responding from error recovery.
1360 * @cmd_list: scsi commands in error recovery.
1361 * @work_q: queue for commands which still need more error recovery
1362 * @done_q: queue for commands which are finished
1363 * @try_stu: boolean on if a STU command should be tried in addition to TUR.
1366 * Tests if devices are in a working state. Commands to devices now in
1367 * a working state are sent to the done_q while commands to devices which
1368 * are still failing to respond are returned to the work_q for more
1371 static int scsi_eh_test_devices(struct list_head *cmd_list,
1372 struct list_head *work_q,
1373 struct list_head *done_q, int try_stu)
1375 struct scsi_cmnd *scmd, *next;
1376 struct scsi_device *sdev;
1379 while (!list_empty(cmd_list)) {
1380 scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
1381 sdev = scmd->device;
1384 if (scsi_host_eh_past_deadline(sdev->host)) {
1385 /* Push items back onto work_q */
1386 list_splice_init(cmd_list, work_q);
1387 SCSI_LOG_ERROR_RECOVERY(3,
1388 sdev_printk(KERN_INFO, sdev,
1389 "%s: skip test device, past eh deadline",
1395 finish_cmds = !scsi_device_online(scmd->device) ||
1396 (try_stu && !scsi_eh_try_stu(scmd) &&
1397 !scsi_eh_tur(scmd)) ||
1400 list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
1401 if (scmd->device == sdev) {
1404 scsi_eh_action(scmd, SUCCESS) == SUCCESS))
1405 scsi_eh_finish_cmd(scmd, done_q);
1407 list_move_tail(&scmd->eh_entry, work_q);
1410 return list_empty(work_q);
1414 * scsi_eh_try_stu - Send START_UNIT to device.
1415 * @scmd: &scsi_cmnd to send START_UNIT
1418 * 0 - Device is ready. 1 - Device NOT ready.
1420 static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
1422 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
1424 if (scmd->device->allow_restart) {
1426 enum scsi_disposition rtn = NEEDS_RETRY;
1428 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
1429 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->device->request_queue->rq_timeout, 0);
1439 * scsi_eh_stu - send START_UNIT if needed
1440 * @shost: &scsi host being recovered.
1441 * @work_q: &list_head for pending commands.
1442 * @done_q: &list_head for processed commands.
1445 * If commands are failing due to not ready, initializing command required,
1446 * try revalidating the device, which will end up sending a start unit.
1448 static int scsi_eh_stu(struct Scsi_Host *shost,
1449 struct list_head *work_q,
1450 struct list_head *done_q)
1452 struct scsi_cmnd *scmd, *stu_scmd, *next;
1453 struct scsi_device *sdev;
1455 shost_for_each_device(sdev, shost) {
1456 if (scsi_host_eh_past_deadline(shost)) {
1457 SCSI_LOG_ERROR_RECOVERY(3,
1458 sdev_printk(KERN_INFO, sdev,
1459 "%s: skip START_UNIT, past eh deadline\n",
1461 scsi_device_put(sdev);
1465 list_for_each_entry(scmd, work_q, eh_entry)
1466 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
1467 scsi_check_sense(scmd) == FAILED ) {
1475 SCSI_LOG_ERROR_RECOVERY(3,
1476 sdev_printk(KERN_INFO, sdev,
1477 "%s: Sending START_UNIT\n",
1480 if (!scsi_eh_try_stu(stu_scmd)) {
1481 if (!scsi_device_online(sdev) ||
1482 !scsi_eh_tur(stu_scmd)) {
1483 list_for_each_entry_safe(scmd, next,
1485 if (scmd->device == sdev &&
1486 scsi_eh_action(scmd, SUCCESS) == SUCCESS)
1487 scsi_eh_finish_cmd(scmd, done_q);
1491 SCSI_LOG_ERROR_RECOVERY(3,
1492 sdev_printk(KERN_INFO, sdev,
1493 "%s: START_UNIT failed\n",
1498 return list_empty(work_q);
1503 * scsi_eh_bus_device_reset - send bdr if needed
1504 * @shost: scsi host being recovered.
1505 * @work_q: &list_head for pending commands.
1506 * @done_q: &list_head for processed commands.
1509 * Try a bus device reset. Still, look to see whether we have multiple
1510 * devices that are jammed or not - if we have multiple devices, it
1511 * makes no sense to try bus_device_reset - we really would need to try
1512 * a bus_reset instead.
1514 static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1515 struct list_head *work_q,
1516 struct list_head *done_q)
1518 struct scsi_cmnd *scmd, *bdr_scmd, *next;
1519 struct scsi_device *sdev;
1520 enum scsi_disposition rtn;
1522 shost_for_each_device(sdev, shost) {
1523 if (scsi_host_eh_past_deadline(shost)) {
1524 SCSI_LOG_ERROR_RECOVERY(3,
1525 sdev_printk(KERN_INFO, sdev,
1526 "%s: skip BDR, past eh deadline\n",
1528 scsi_device_put(sdev);
1532 list_for_each_entry(scmd, work_q, eh_entry)
1533 if (scmd->device == sdev) {
1541 SCSI_LOG_ERROR_RECOVERY(3,
1542 sdev_printk(KERN_INFO, sdev,
1543 "%s: Sending BDR\n", current->comm));
1544 rtn = scsi_try_bus_device_reset(bdr_scmd);
1545 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
1546 if (!scsi_device_online(sdev) ||
1547 rtn == FAST_IO_FAIL ||
1548 !scsi_eh_tur(bdr_scmd)) {
1549 list_for_each_entry_safe(scmd, next,
1551 if (scmd->device == sdev &&
1552 scsi_eh_action(scmd, rtn) != FAILED)
1553 scsi_eh_finish_cmd(scmd,
1558 SCSI_LOG_ERROR_RECOVERY(3,
1559 sdev_printk(KERN_INFO, sdev,
1560 "%s: BDR failed\n", current->comm));
1564 return list_empty(work_q);
1568 * scsi_eh_target_reset - send target reset if needed
1569 * @shost: scsi host being recovered.
1570 * @work_q: &list_head for pending commands.
1571 * @done_q: &list_head for processed commands.
1574 * Try a target reset.
1576 static int scsi_eh_target_reset(struct Scsi_Host *shost,
1577 struct list_head *work_q,
1578 struct list_head *done_q)
1580 LIST_HEAD(tmp_list);
1581 LIST_HEAD(check_list);
1583 list_splice_init(work_q, &tmp_list);
1585 while (!list_empty(&tmp_list)) {
1586 struct scsi_cmnd *next, *scmd;
1587 enum scsi_disposition rtn;
1590 if (scsi_host_eh_past_deadline(shost)) {
1591 /* push back on work queue for further processing */
1592 list_splice_init(&check_list, work_q);
1593 list_splice_init(&tmp_list, work_q);
1594 SCSI_LOG_ERROR_RECOVERY(3,
1595 shost_printk(KERN_INFO, shost,
1596 "%s: Skip target reset, past eh deadline\n",
1598 return list_empty(work_q);
1601 scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
1604 SCSI_LOG_ERROR_RECOVERY(3,
1605 shost_printk(KERN_INFO, shost,
1606 "%s: Sending target reset to target %d\n",
1607 current->comm, id));
1608 rtn = scsi_try_target_reset(scmd);
1609 if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
1610 SCSI_LOG_ERROR_RECOVERY(3,
1611 shost_printk(KERN_INFO, shost,
1612 "%s: Target reset failed"
1614 current->comm, id));
1615 list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
1616 if (scmd_id(scmd) != id)
1620 list_move_tail(&scmd->eh_entry, &check_list);
1621 else if (rtn == FAST_IO_FAIL)
1622 scsi_eh_finish_cmd(scmd, done_q);
1624 /* push back on work queue for further processing */
1625 list_move(&scmd->eh_entry, work_q);
1629 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1633 * scsi_eh_bus_reset - send a bus reset
1634 * @shost: &scsi host being recovered.
1635 * @work_q: &list_head for pending commands.
1636 * @done_q: &list_head for processed commands.
1638 static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1639 struct list_head *work_q,
1640 struct list_head *done_q)
1642 struct scsi_cmnd *scmd, *chan_scmd, *next;
1643 LIST_HEAD(check_list);
1644 unsigned int channel;
1645 enum scsi_disposition rtn;
1648 * we really want to loop over the various channels, and do this on
1649 * a channel by channel basis. we should also check to see if any
1650 * of the failed commands are on soft_reset devices, and if so, skip
1654 for (channel = 0; channel <= shost->max_channel; channel++) {
1655 if (scsi_host_eh_past_deadline(shost)) {
1656 list_splice_init(&check_list, work_q);
1657 SCSI_LOG_ERROR_RECOVERY(3,
1658 shost_printk(KERN_INFO, shost,
1659 "%s: skip BRST, past eh deadline\n",
1661 return list_empty(work_q);
1665 list_for_each_entry(scmd, work_q, eh_entry) {
1666 if (channel == scmd_channel(scmd)) {
1670 * FIXME add back in some support for
1671 * soft_reset devices.
1678 SCSI_LOG_ERROR_RECOVERY(3,
1679 shost_printk(KERN_INFO, shost,
1680 "%s: Sending BRST chan: %d\n",
1681 current->comm, channel));
1682 rtn = scsi_try_bus_reset(chan_scmd);
1683 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
1684 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1685 if (channel == scmd_channel(scmd)) {
1686 if (rtn == FAST_IO_FAIL)
1687 scsi_eh_finish_cmd(scmd,
1690 list_move_tail(&scmd->eh_entry,
1695 SCSI_LOG_ERROR_RECOVERY(3,
1696 shost_printk(KERN_INFO, shost,
1697 "%s: BRST failed chan: %d\n",
1698 current->comm, channel));
1701 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1705 * scsi_eh_host_reset - send a host reset
1706 * @shost: host to be reset.
1707 * @work_q: &list_head for pending commands.
1708 * @done_q: &list_head for processed commands.
1710 static int scsi_eh_host_reset(struct Scsi_Host *shost,
1711 struct list_head *work_q,
1712 struct list_head *done_q)
1714 struct scsi_cmnd *scmd, *next;
1715 LIST_HEAD(check_list);
1716 enum scsi_disposition rtn;
1718 if (!list_empty(work_q)) {
1719 scmd = list_entry(work_q->next,
1720 struct scsi_cmnd, eh_entry);
1722 SCSI_LOG_ERROR_RECOVERY(3,
1723 shost_printk(KERN_INFO, shost,
1724 "%s: Sending HRST\n",
1727 rtn = scsi_try_host_reset(scmd);
1728 if (rtn == SUCCESS) {
1729 list_splice_init(work_q, &check_list);
1730 } else if (rtn == FAST_IO_FAIL) {
1731 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1732 scsi_eh_finish_cmd(scmd, done_q);
1735 SCSI_LOG_ERROR_RECOVERY(3,
1736 shost_printk(KERN_INFO, shost,
1737 "%s: HRST failed\n",
1741 return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
1745 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
1746 * @work_q: &list_head for pending commands.
1747 * @done_q: &list_head for processed commands.
1749 static void scsi_eh_offline_sdevs(struct list_head *work_q,
1750 struct list_head *done_q)
1752 struct scsi_cmnd *scmd, *next;
1753 struct scsi_device *sdev;
1755 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1756 sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
1757 "not ready after error recovery\n");
1758 sdev = scmd->device;
1760 mutex_lock(&sdev->state_mutex);
1761 scsi_device_set_state(sdev, SDEV_OFFLINE);
1762 mutex_unlock(&sdev->state_mutex);
1764 scsi_eh_finish_cmd(scmd, done_q);
1770 * scsi_noretry_cmd - determine if command should be failed fast
1771 * @scmd: SCSI cmd to examine.
1773 int scsi_noretry_cmd(struct scsi_cmnd *scmd)
1775 struct request *req = scsi_cmd_to_rq(scmd);
1777 switch (host_byte(scmd->result)) {
1783 return req->cmd_flags & REQ_FAILFAST_TRANSPORT;
1785 return req->cmd_flags & REQ_FAILFAST_DEV;
1787 if (get_status_byte(scmd) == SAM_STAT_RESERVATION_CONFLICT)
1790 case DID_SOFT_ERROR:
1791 return req->cmd_flags & REQ_FAILFAST_DRIVER;
1794 if (!scsi_status_is_check_condition(scmd->result))
1799 * assume caller has checked sense and determined
1800 * the check condition was retryable.
1802 if (req->cmd_flags & REQ_FAILFAST_DEV || blk_rq_is_passthrough(req))
1809 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1810 * @scmd: SCSI cmd to examine.
1813 * This is *only* called when we are examining the status after sending
1814 * out the actual data command. any commands that are queued for error
1815 * recovery (e.g. test_unit_ready) do *not* come through here.
1817 * When this routine returns failed, it means the error handler thread
1818 * is woken. In cases where the error code indicates an error that
1819 * doesn't require the error handler read (i.e. we don't need to
1820 * abort/reset), this function should return SUCCESS.
1822 enum scsi_disposition scsi_decide_disposition(struct scsi_cmnd *scmd)
1824 enum scsi_disposition rtn;
1827 * if the device is offline, then we clearly just pass the result back
1828 * up to the top level.
1830 if (!scsi_device_online(scmd->device)) {
1831 SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO, scmd,
1832 "%s: device offline - report as SUCCESS\n", __func__));
1837 * first check the host byte, to see if there is anything in there
1838 * that would indicate what we need to do.
1840 switch (host_byte(scmd->result)) {
1841 case DID_PASSTHROUGH:
1843 * no matter what, pass this through to the upper layer.
1844 * nuke this special code so that it looks like we are saying
1847 scmd->result &= 0xff00ffff;
1851 * looks good. drop through, and check the next byte.
1855 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
1856 set_host_byte(scmd, DID_TIME_OUT);
1860 case DID_NO_CONNECT:
1861 case DID_BAD_TARGET:
1863 * note - this means that we just report the status back
1864 * to the top level driver, not that we actually think
1865 * that it indicates SUCCESS.
1868 case DID_SOFT_ERROR:
1870 * when the low level driver returns did_soft_error,
1871 * it is responsible for keeping an internal retry counter
1872 * in order to avoid endless loops (db)
1879 return ADD_TO_MLQUEUE;
1880 case DID_TRANSPORT_DISRUPTED:
1882 * LLD/transport was disrupted during processing of the IO.
1883 * The transport class is now blocked/blocking,
1884 * and the transport will decide what to do with the IO
1885 * based on its timers and recovery capablilities if
1886 * there are enough retries.
1889 case DID_TRANSPORT_FAILFAST:
1891 * The transport decided to failfast the IO (most likely
1892 * the fast io fail tmo fired), so send IO directly upwards.
1895 case DID_TRANSPORT_MARGINAL:
1897 * caller has decided not to do retries on
1898 * abort success, so send IO directly upwards
1902 if (get_status_byte(scmd) == SAM_STAT_RESERVATION_CONFLICT)
1904 * execute reservation conflict processing code
1914 * when we scan the bus, we get timeout messages for
1915 * these commands if there is no device available.
1916 * other hosts report did_no_connect for the same thing.
1918 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1919 scmd->cmnd[0] == INQUIRY)) {
1931 * check the status byte to see if this indicates anything special.
1933 switch (get_status_byte(scmd)) {
1934 case SAM_STAT_TASK_SET_FULL:
1935 scsi_handle_queue_full(scmd->device);
1937 * the case of trying to send too many commands to a
1938 * tagged queueing device.
1943 * device can't talk to us at the moment. Should only
1944 * occur (SAM-3) when the task queue is empty, so will cause
1945 * the empty queue handling to trigger a stall in the
1948 return ADD_TO_MLQUEUE;
1950 if (scmd->cmnd[0] == REPORT_LUNS)
1951 scmd->device->sdev_target->expecting_lun_change = 0;
1952 scsi_handle_queue_ramp_up(scmd->device);
1954 case SAM_STAT_COMMAND_TERMINATED:
1956 case SAM_STAT_TASK_ABORTED:
1958 case SAM_STAT_CHECK_CONDITION:
1959 rtn = scsi_check_sense(scmd);
1960 if (rtn == NEEDS_RETRY)
1962 /* if rtn == FAILED, we have no sense information;
1963 * returning FAILED will wake the error handler thread
1964 * to collect the sense and redo the decide
1967 case SAM_STAT_CONDITION_MET:
1968 case SAM_STAT_INTERMEDIATE:
1969 case SAM_STAT_INTERMEDIATE_CONDITION_MET:
1970 case SAM_STAT_ACA_ACTIVE:
1972 * who knows? FIXME(eric)
1976 case SAM_STAT_RESERVATION_CONFLICT:
1977 sdev_printk(KERN_INFO, scmd->device,
1978 "reservation conflict\n");
1979 set_host_byte(scmd, DID_NEXUS_FAILURE);
1980 return SUCCESS; /* causes immediate i/o error */
1988 /* we requeue for retry because the error was retryable, and
1989 * the request was not marked fast fail. Note that above,
1990 * even if the request is marked fast fail, we still requeue
1991 * for queue congestion conditions (QUEUE_FULL or BUSY) */
1992 if (scsi_cmd_retry_allowed(scmd) && !scsi_noretry_cmd(scmd)) {
1996 * no more retries - report this one back to upper level.
2002 static void eh_lock_door_done(struct request *req, blk_status_t status)
2004 blk_put_request(req);
2008 * scsi_eh_lock_door - Prevent medium removal for the specified device
2009 * @sdev: SCSI device to prevent medium removal
2012 * We must be called from process context.
2015 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
2016 * head of the devices request queue, and continue.
2018 static void scsi_eh_lock_door(struct scsi_device *sdev)
2020 struct request *req;
2021 struct scsi_request *rq;
2023 req = blk_get_request(sdev->request_queue, REQ_OP_DRV_IN, 0);
2028 rq->cmd[0] = ALLOW_MEDIUM_REMOVAL;
2032 rq->cmd[4] = SCSI_REMOVAL_PREVENT;
2034 rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
2036 req->rq_flags |= RQF_QUIET;
2037 req->timeout = 10 * HZ;
2040 blk_execute_rq_nowait(NULL, req, 1, eh_lock_door_done);
2044 * scsi_restart_operations - restart io operations to the specified host.
2045 * @shost: Host we are restarting.
2048 * When we entered the error handler, we blocked all further i/o to
2049 * this device. we need to 'reverse' this process.
2051 static void scsi_restart_operations(struct Scsi_Host *shost)
2053 struct scsi_device *sdev;
2054 unsigned long flags;
2057 * If the door was locked, we need to insert a door lock request
2058 * onto the head of the SCSI request queue for the device. There
2059 * is no point trying to lock the door of an off-line device.
2061 shost_for_each_device(sdev, shost) {
2062 if (scsi_device_online(sdev) && sdev->was_reset && sdev->locked) {
2063 scsi_eh_lock_door(sdev);
2064 sdev->was_reset = 0;
2069 * next free up anything directly waiting upon the host. this
2070 * will be requests for character device operations, and also for
2071 * ioctls to queued block devices.
2073 SCSI_LOG_ERROR_RECOVERY(3,
2074 shost_printk(KERN_INFO, shost, "waking up host to restart\n"));
2076 spin_lock_irqsave(shost->host_lock, flags);
2077 if (scsi_host_set_state(shost, SHOST_RUNNING))
2078 if (scsi_host_set_state(shost, SHOST_CANCEL))
2079 BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
2080 spin_unlock_irqrestore(shost->host_lock, flags);
2082 wake_up(&shost->host_wait);
2085 * finally we need to re-initiate requests that may be pending. we will
2086 * have had everything blocked while error handling is taking place, and
2087 * now that error recovery is done, we will need to ensure that these
2088 * requests are started.
2090 scsi_run_host_queues(shost);
2093 * if eh is active and host_eh_scheduled is pending we need to re-run
2094 * recovery. we do this check after scsi_run_host_queues() to allow
2095 * everything pent up since the last eh run a chance to make forward
2096 * progress before we sync again. Either we'll immediately re-run
2097 * recovery or scsi_device_unbusy() will wake us again when these
2098 * pending commands complete.
2100 spin_lock_irqsave(shost->host_lock, flags);
2101 if (shost->host_eh_scheduled)
2102 if (scsi_host_set_state(shost, SHOST_RECOVERY))
2103 WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY));
2104 spin_unlock_irqrestore(shost->host_lock, flags);
2108 * scsi_eh_ready_devs - check device ready state and recover if not.
2109 * @shost: host to be recovered.
2110 * @work_q: &list_head for pending commands.
2111 * @done_q: &list_head for processed commands.
2113 void scsi_eh_ready_devs(struct Scsi_Host *shost,
2114 struct list_head *work_q,
2115 struct list_head *done_q)
2117 if (!scsi_eh_stu(shost, work_q, done_q))
2118 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
2119 if (!scsi_eh_target_reset(shost, work_q, done_q))
2120 if (!scsi_eh_bus_reset(shost, work_q, done_q))
2121 if (!scsi_eh_host_reset(shost, work_q, done_q))
2122 scsi_eh_offline_sdevs(work_q,
2125 EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
2128 * scsi_eh_flush_done_q - finish processed commands or retry them.
2129 * @done_q: list_head of processed commands.
2131 void scsi_eh_flush_done_q(struct list_head *done_q)
2133 struct scsi_cmnd *scmd, *next;
2135 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
2136 list_del_init(&scmd->eh_entry);
2137 if (scsi_device_online(scmd->device) &&
2138 !scsi_noretry_cmd(scmd) && scsi_cmd_retry_allowed(scmd) &&
2139 scsi_eh_should_retry_cmd(scmd)) {
2140 SCSI_LOG_ERROR_RECOVERY(3,
2141 scmd_printk(KERN_INFO, scmd,
2142 "%s: flush retry cmd\n",
2144 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
2147 * If just we got sense for the device (called
2148 * scsi_eh_get_sense), scmd->result is already
2149 * set, do not set DID_TIME_OUT.
2152 scmd->result |= (DID_TIME_OUT << 16);
2153 SCSI_LOG_ERROR_RECOVERY(3,
2154 scmd_printk(KERN_INFO, scmd,
2155 "%s: flush finish cmd\n",
2157 scsi_finish_command(scmd);
2161 EXPORT_SYMBOL(scsi_eh_flush_done_q);
2164 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
2165 * @shost: Host to unjam.
2168 * When we come in here, we *know* that all commands on the bus have
2169 * either completed, failed or timed out. we also know that no further
2170 * commands are being sent to the host, so things are relatively quiet
2171 * and we have freedom to fiddle with things as we wish.
2173 * This is only the *default* implementation. it is possible for
2174 * individual drivers to supply their own version of this function, and
2175 * if the maintainer wishes to do this, it is strongly suggested that
2176 * this function be taken as a template and modified. this function
2177 * was designed to correctly handle problems for about 95% of the
2178 * different cases out there, and it should always provide at least a
2179 * reasonable amount of error recovery.
2181 * Any command marked 'failed' or 'timeout' must eventually have
2182 * scsi_finish_cmd() called for it. we do all of the retry stuff
2183 * here, so when we restart the host after we return it should have an
2186 static void scsi_unjam_host(struct Scsi_Host *shost)
2188 unsigned long flags;
2189 LIST_HEAD(eh_work_q);
2190 LIST_HEAD(eh_done_q);
2192 spin_lock_irqsave(shost->host_lock, flags);
2193 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
2194 spin_unlock_irqrestore(shost->host_lock, flags);
2196 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
2198 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
2199 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
2201 spin_lock_irqsave(shost->host_lock, flags);
2202 if (shost->eh_deadline != -1)
2203 shost->last_reset = 0;
2204 spin_unlock_irqrestore(shost->host_lock, flags);
2205 scsi_eh_flush_done_q(&eh_done_q);
2209 * scsi_error_handler - SCSI error handler thread
2210 * @data: Host for which we are running.
2213 * This is the main error handling loop. This is run as a kernel thread
2214 * for every SCSI host and handles all error handling activity.
2216 int scsi_error_handler(void *data)
2218 struct Scsi_Host *shost = data;
2221 * We use TASK_INTERRUPTIBLE so that the thread is not
2222 * counted against the load average as a running process.
2223 * We never actually get interrupted because kthread_run
2224 * disables signal delivery for the created thread.
2228 * The sequence in kthread_stop() sets the stop flag first
2229 * then wakes the process. To avoid missed wakeups, the task
2230 * should always be in a non running state before the stop
2233 set_current_state(TASK_INTERRUPTIBLE);
2234 if (kthread_should_stop())
2237 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
2238 shost->host_failed != scsi_host_busy(shost)) {
2239 SCSI_LOG_ERROR_RECOVERY(1,
2240 shost_printk(KERN_INFO, shost,
2241 "scsi_eh_%d: sleeping\n",
2247 __set_current_state(TASK_RUNNING);
2248 SCSI_LOG_ERROR_RECOVERY(1,
2249 shost_printk(KERN_INFO, shost,
2250 "scsi_eh_%d: waking up %d/%d/%d\n",
2251 shost->host_no, shost->host_eh_scheduled,
2253 scsi_host_busy(shost)));
2256 * We have a host that is failing for some reason. Figure out
2257 * what we need to do to get it up and online again (if we can).
2258 * If we fail, we end up taking the thing offline.
2260 if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
2261 SCSI_LOG_ERROR_RECOVERY(1,
2262 shost_printk(KERN_ERR, shost,
2263 "scsi_eh_%d: unable to autoresume\n",
2268 if (shost->transportt->eh_strategy_handler)
2269 shost->transportt->eh_strategy_handler(shost);
2271 scsi_unjam_host(shost);
2273 /* All scmds have been handled */
2274 shost->host_failed = 0;
2277 * Note - if the above fails completely, the action is to take
2278 * individual devices offline and flush the queue of any
2279 * outstanding requests that may have been pending. When we
2280 * restart, we restart any I/O to any other devices on the bus
2281 * which are still online.
2283 scsi_restart_operations(shost);
2284 if (!shost->eh_noresume)
2285 scsi_autopm_put_host(shost);
2287 __set_current_state(TASK_RUNNING);
2289 SCSI_LOG_ERROR_RECOVERY(1,
2290 shost_printk(KERN_INFO, shost,
2291 "Error handler scsi_eh_%d exiting\n",
2293 shost->ehandler = NULL;
2298 * Function: scsi_report_bus_reset()
2300 * Purpose: Utility function used by low-level drivers to report that
2301 * they have observed a bus reset on the bus being handled.
2303 * Arguments: shost - Host in question
2304 * channel - channel on which reset was observed.
2308 * Lock status: Host lock must be held.
2310 * Notes: This only needs to be called if the reset is one which
2311 * originates from an unknown location. Resets originated
2312 * by the mid-level itself don't need to call this, but there
2313 * should be no harm.
2315 * The main purpose of this is to make sure that a CHECK_CONDITION
2316 * is properly treated.
2318 void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
2320 struct scsi_device *sdev;
2322 __shost_for_each_device(sdev, shost) {
2323 if (channel == sdev_channel(sdev))
2324 __scsi_report_device_reset(sdev, NULL);
2327 EXPORT_SYMBOL(scsi_report_bus_reset);
2330 * Function: scsi_report_device_reset()
2332 * Purpose: Utility function used by low-level drivers to report that
2333 * they have observed a device reset on the device being handled.
2335 * Arguments: shost - Host in question
2336 * channel - channel on which reset was observed
2337 * target - target on which reset was observed
2341 * Lock status: Host lock must be held
2343 * Notes: This only needs to be called if the reset is one which
2344 * originates from an unknown location. Resets originated
2345 * by the mid-level itself don't need to call this, but there
2346 * should be no harm.
2348 * The main purpose of this is to make sure that a CHECK_CONDITION
2349 * is properly treated.
2351 void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
2353 struct scsi_device *sdev;
2355 __shost_for_each_device(sdev, shost) {
2356 if (channel == sdev_channel(sdev) &&
2357 target == sdev_id(sdev))
2358 __scsi_report_device_reset(sdev, NULL);
2361 EXPORT_SYMBOL(scsi_report_device_reset);
2364 scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
2369 * scsi_ioctl_reset: explicitly reset a host/bus/target/device
2370 * @dev: scsi_device to operate on
2371 * @arg: reset type (see sg.h)
2374 scsi_ioctl_reset(struct scsi_device *dev, int __user *arg)
2376 struct scsi_cmnd *scmd;
2377 struct Scsi_Host *shost = dev->host;
2379 unsigned long flags;
2381 enum scsi_disposition rtn;
2383 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2386 error = get_user(val, arg);
2390 if (scsi_autopm_get_host(shost) < 0)
2394 rq = kzalloc(sizeof(struct request) + sizeof(struct scsi_cmnd) +
2395 shost->hostt->cmd_size, GFP_KERNEL);
2397 goto out_put_autopm_host;
2398 blk_rq_init(NULL, rq);
2400 scmd = (struct scsi_cmnd *)(rq + 1);
2401 scsi_init_command(dev, scmd);
2402 scmd->cmnd = scsi_req(rq)->cmd;
2404 scmd->scsi_done = scsi_reset_provider_done_command;
2405 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
2409 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
2411 spin_lock_irqsave(shost->host_lock, flags);
2412 shost->tmf_in_progress = 1;
2413 spin_unlock_irqrestore(shost->host_lock, flags);
2415 switch (val & ~SG_SCSI_RESET_NO_ESCALATE) {
2416 case SG_SCSI_RESET_NOTHING:
2419 case SG_SCSI_RESET_DEVICE:
2420 rtn = scsi_try_bus_device_reset(scmd);
2421 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
2424 case SG_SCSI_RESET_TARGET:
2425 rtn = scsi_try_target_reset(scmd);
2426 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
2429 case SG_SCSI_RESET_BUS:
2430 rtn = scsi_try_bus_reset(scmd);
2431 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
2434 case SG_SCSI_RESET_HOST:
2435 rtn = scsi_try_host_reset(scmd);
2444 error = (rtn == SUCCESS) ? 0 : -EIO;
2446 spin_lock_irqsave(shost->host_lock, flags);
2447 shost->tmf_in_progress = 0;
2448 spin_unlock_irqrestore(shost->host_lock, flags);
2451 * be sure to wake up anyone who was sleeping or had their queue
2452 * suspended while we performed the TMF.
2454 SCSI_LOG_ERROR_RECOVERY(3,
2455 shost_printk(KERN_INFO, shost,
2456 "waking up host to restart after TMF\n"));
2458 wake_up(&shost->host_wait);
2459 scsi_run_host_queues(shost);
2463 out_put_autopm_host:
2464 scsi_autopm_put_host(shost);
2468 bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd,
2469 struct scsi_sense_hdr *sshdr)
2471 return scsi_normalize_sense(cmd->sense_buffer,
2472 SCSI_SENSE_BUFFERSIZE, sshdr);
2474 EXPORT_SYMBOL(scsi_command_normalize_sense);
2477 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
2478 * @sense_buffer: byte array of sense data
2479 * @sb_len: number of valid bytes in sense_buffer
2480 * @info_out: pointer to 64 integer where 8 or 4 byte information
2481 * field will be placed if found.
2484 * true if information field found, false if not found.
2486 bool scsi_get_sense_info_fld(const u8 *sense_buffer, int sb_len,
2493 switch (sense_buffer[0] & 0x7f) {
2496 if (sense_buffer[0] & 0x80) {
2497 *info_out = get_unaligned_be32(&sense_buffer[3]);
2503 ucp = scsi_sense_desc_find(sense_buffer, sb_len,
2505 if (ucp && (0xa == ucp[1])) {
2506 *info_out = get_unaligned_be64(&ucp[4]);
2514 EXPORT_SYMBOL(scsi_get_sense_info_fld);