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>
54 * These should *probably* be handled by the host itself.
55 * Since it is allowed to sleep, it probably should.
57 #define BUS_RESET_SETTLE_TIME (10)
58 #define HOST_RESET_SETTLE_TIME (10)
60 static int scsi_eh_try_stu(struct scsi_cmnd *scmd);
61 static enum scsi_disposition scsi_try_to_abort_cmd(struct scsi_host_template *,
64 void scsi_eh_wakeup(struct Scsi_Host *shost)
66 lockdep_assert_held(shost->host_lock);
68 if (scsi_host_busy(shost) == shost->host_failed) {
69 trace_scsi_eh_wakeup(shost);
70 wake_up_process(shost->ehandler);
71 SCSI_LOG_ERROR_RECOVERY(5, shost_printk(KERN_INFO, shost,
72 "Waking error handler thread\n"));
77 * scsi_schedule_eh - schedule EH for SCSI host
78 * @shost: SCSI host to invoke error handling on.
80 * Schedule SCSI EH without scmd.
82 void scsi_schedule_eh(struct Scsi_Host *shost)
86 spin_lock_irqsave(shost->host_lock, flags);
88 if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
89 scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
90 shost->host_eh_scheduled++;
91 scsi_eh_wakeup(shost);
94 spin_unlock_irqrestore(shost->host_lock, flags);
96 EXPORT_SYMBOL_GPL(scsi_schedule_eh);
98 static int scsi_host_eh_past_deadline(struct Scsi_Host *shost)
100 if (!shost->last_reset || shost->eh_deadline == -1)
104 * 32bit accesses are guaranteed to be atomic
105 * (on all supported architectures), so instead
106 * of using a spinlock we can as well double check
107 * if eh_deadline has been set to 'off' during the
110 if (time_before(jiffies, shost->last_reset + shost->eh_deadline) &&
111 shost->eh_deadline > -1)
117 static bool scsi_cmd_retry_allowed(struct scsi_cmnd *cmd)
119 if (cmd->allowed == SCSI_CMD_RETRIES_NO_LIMIT)
122 return ++cmd->retries <= cmd->allowed;
125 static bool scsi_eh_should_retry_cmd(struct scsi_cmnd *cmd)
127 struct scsi_device *sdev = cmd->device;
128 struct Scsi_Host *host = sdev->host;
130 if (host->hostt->eh_should_retry_cmd)
131 return host->hostt->eh_should_retry_cmd(cmd);
137 * scmd_eh_abort_handler - Handle command aborts
138 * @work: command to be aborted.
140 * Note: this function must be called only for a command that has timed out.
141 * Because the block layer marks a request as complete before it calls
142 * scsi_timeout(), a .scsi_done() call from the LLD for a command that has
143 * timed out do not have any effect. Hence it is safe to call
144 * scsi_finish_command() from this function.
147 scmd_eh_abort_handler(struct work_struct *work)
149 struct scsi_cmnd *scmd =
150 container_of(work, struct scsi_cmnd, abort_work.work);
151 struct scsi_device *sdev = scmd->device;
152 struct Scsi_Host *shost = sdev->host;
153 enum scsi_disposition rtn;
156 if (scsi_host_eh_past_deadline(shost)) {
157 SCSI_LOG_ERROR_RECOVERY(3,
158 scmd_printk(KERN_INFO, scmd,
159 "eh timeout, not aborting\n"));
163 SCSI_LOG_ERROR_RECOVERY(3,
164 scmd_printk(KERN_INFO, scmd,
165 "aborting command\n"));
166 rtn = scsi_try_to_abort_cmd(shost->hostt, scmd);
167 if (rtn != SUCCESS) {
168 SCSI_LOG_ERROR_RECOVERY(3,
169 scmd_printk(KERN_INFO, scmd,
171 (rtn == FAST_IO_FAIL) ?
172 "not send" : "failed"));
175 set_host_byte(scmd, DID_TIME_OUT);
176 if (scsi_host_eh_past_deadline(shost)) {
177 SCSI_LOG_ERROR_RECOVERY(3,
178 scmd_printk(KERN_INFO, scmd,
179 "eh timeout, not retrying "
180 "aborted command\n"));
184 spin_lock_irqsave(shost->host_lock, flags);
185 list_del_init(&scmd->eh_entry);
188 * If the abort succeeds, and there is no further
189 * EH action, clear the ->last_reset time.
191 if (list_empty(&shost->eh_abort_list) &&
192 list_empty(&shost->eh_cmd_q))
193 if (shost->eh_deadline != -1)
194 shost->last_reset = 0;
196 spin_unlock_irqrestore(shost->host_lock, flags);
198 if (!scsi_noretry_cmd(scmd) &&
199 scsi_cmd_retry_allowed(scmd) &&
200 scsi_eh_should_retry_cmd(scmd)) {
201 SCSI_LOG_ERROR_RECOVERY(3,
202 scmd_printk(KERN_WARNING, scmd,
203 "retry aborted command\n"));
204 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
206 SCSI_LOG_ERROR_RECOVERY(3,
207 scmd_printk(KERN_WARNING, scmd,
208 "finish aborted command\n"));
209 scsi_finish_command(scmd);
214 spin_lock_irqsave(shost->host_lock, flags);
215 list_del_init(&scmd->eh_entry);
216 spin_unlock_irqrestore(shost->host_lock, flags);
218 scsi_eh_scmd_add(scmd);
222 * scsi_abort_command - schedule a command abort
223 * @scmd: scmd to abort.
225 * We only need to abort commands after a command timeout
228 scsi_abort_command(struct scsi_cmnd *scmd)
230 struct scsi_device *sdev = scmd->device;
231 struct Scsi_Host *shost = sdev->host;
234 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
236 * Retry after abort failed, escalate to next level.
238 SCSI_LOG_ERROR_RECOVERY(3,
239 scmd_printk(KERN_INFO, scmd,
240 "previous abort failed\n"));
241 BUG_ON(delayed_work_pending(&scmd->abort_work));
245 spin_lock_irqsave(shost->host_lock, flags);
246 if (shost->eh_deadline != -1 && !shost->last_reset)
247 shost->last_reset = jiffies;
248 BUG_ON(!list_empty(&scmd->eh_entry));
249 list_add_tail(&scmd->eh_entry, &shost->eh_abort_list);
250 spin_unlock_irqrestore(shost->host_lock, flags);
252 scmd->eh_eflags |= SCSI_EH_ABORT_SCHEDULED;
253 SCSI_LOG_ERROR_RECOVERY(3,
254 scmd_printk(KERN_INFO, scmd, "abort scheduled\n"));
255 queue_delayed_work(shost->tmf_work_q, &scmd->abort_work, HZ / 100);
260 * scsi_eh_reset - call into ->eh_action to reset internal counters
261 * @scmd: scmd to run eh on.
263 * The scsi driver might be carrying internal state about the
264 * devices, so we need to call into the driver to reset the
265 * internal state once the error handler is started.
267 static void scsi_eh_reset(struct scsi_cmnd *scmd)
269 if (!blk_rq_is_passthrough(scsi_cmd_to_rq(scmd))) {
270 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
272 sdrv->eh_reset(scmd);
276 static void scsi_eh_inc_host_failed(struct rcu_head *head)
278 struct scsi_cmnd *scmd = container_of(head, typeof(*scmd), rcu);
279 struct Scsi_Host *shost = scmd->device->host;
282 spin_lock_irqsave(shost->host_lock, flags);
283 shost->host_failed++;
284 scsi_eh_wakeup(shost);
285 spin_unlock_irqrestore(shost->host_lock, flags);
289 * scsi_eh_scmd_add - add scsi cmd to error handling.
290 * @scmd: scmd to run eh on.
292 void scsi_eh_scmd_add(struct scsi_cmnd *scmd)
294 struct Scsi_Host *shost = scmd->device->host;
298 WARN_ON_ONCE(!shost->ehandler);
300 spin_lock_irqsave(shost->host_lock, flags);
301 if (scsi_host_set_state(shost, SHOST_RECOVERY)) {
302 ret = scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY);
305 if (shost->eh_deadline != -1 && !shost->last_reset)
306 shost->last_reset = jiffies;
309 list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
310 spin_unlock_irqrestore(shost->host_lock, flags);
312 * Ensure that all tasks observe the host state change before the
313 * host_failed change.
315 call_rcu(&scmd->rcu, scsi_eh_inc_host_failed);
319 * scsi_timeout - Timeout function for normal scsi commands.
320 * @req: request that is timing out.
323 * We do not need to lock this. There is the potential for a race
324 * only in that the normal completion handling might run, but if the
325 * normal completion function determines that the timer has already
326 * fired, then it mustn't do anything.
328 enum blk_eh_timer_return scsi_timeout(struct request *req)
330 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
331 enum blk_eh_timer_return rtn = BLK_EH_DONE;
332 struct Scsi_Host *host = scmd->device->host;
334 trace_scsi_dispatch_cmd_timeout(scmd);
335 scsi_log_completion(scmd, TIMEOUT_ERROR);
337 atomic_inc(&scmd->device->iotmo_cnt);
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 * Set the command to complete first in order to prevent a real
347 * completion from releasing the command while error handling
348 * is using it. If the command was already completed, then the
349 * lower level driver beat the timeout handler, and it is safe
350 * to return without escalating error recovery.
352 * If timeout handling lost the race to a real completion, the
353 * block layer may ignore that due to a fake timeout injection,
354 * so return RESET_TIMER to allow error handling another shot
357 if (test_and_set_bit(SCMD_STATE_COMPLETE, &scmd->state))
358 return BLK_EH_RESET_TIMER;
359 if (scsi_abort_command(scmd) != SUCCESS) {
360 set_host_byte(scmd, DID_TIME_OUT);
361 scsi_eh_scmd_add(scmd);
369 * scsi_block_when_processing_errors - Prevent cmds from being queued.
370 * @sdev: Device on which we are performing recovery.
373 * We block until the host is out of error recovery, and then check to
374 * see whether the host or the device is offline.
377 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
379 int scsi_block_when_processing_errors(struct scsi_device *sdev)
383 wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
385 online = scsi_device_online(sdev);
389 EXPORT_SYMBOL(scsi_block_when_processing_errors);
391 #ifdef CONFIG_SCSI_LOGGING
393 * scsi_eh_prt_fail_stats - Log info on failures.
394 * @shost: scsi host being recovered.
395 * @work_q: Queue of scsi cmds to process.
397 static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
398 struct list_head *work_q)
400 struct scsi_cmnd *scmd;
401 struct scsi_device *sdev;
402 int total_failures = 0;
405 int devices_failed = 0;
407 shost_for_each_device(sdev, shost) {
408 list_for_each_entry(scmd, work_q, eh_entry) {
409 if (scmd->device == sdev) {
411 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED)
418 if (cmd_cancel || cmd_failed) {
419 SCSI_LOG_ERROR_RECOVERY(3,
420 shost_printk(KERN_INFO, shost,
421 "%s: cmds failed: %d, cancel: %d\n",
422 __func__, cmd_failed,
430 SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO, shost,
431 "Total of %d commands on %d"
432 " devices require eh work\n",
433 total_failures, devices_failed));
438 * scsi_report_lun_change - Set flag on all *other* devices on the same target
439 * to indicate that a UNIT ATTENTION is expected.
440 * @sdev: Device reporting the UNIT ATTENTION
442 static void scsi_report_lun_change(struct scsi_device *sdev)
444 sdev->sdev_target->expecting_lun_change = 1;
448 * scsi_report_sense - Examine scsi sense information and log messages for
449 * certain conditions, also issue uevents for some of them.
450 * @sdev: Device reporting the sense code
451 * @sshdr: sshdr to be examined
453 static void scsi_report_sense(struct scsi_device *sdev,
454 struct scsi_sense_hdr *sshdr)
456 enum scsi_device_event evt_type = SDEV_EVT_MAXBITS; /* i.e. none */
458 if (sshdr->sense_key == UNIT_ATTENTION) {
459 if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) {
460 evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED;
461 sdev_printk(KERN_WARNING, sdev,
462 "Inquiry data has changed");
463 } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) {
464 evt_type = SDEV_EVT_LUN_CHANGE_REPORTED;
465 scsi_report_lun_change(sdev);
466 sdev_printk(KERN_WARNING, sdev,
467 "LUN assignments on this target have "
468 "changed. The Linux SCSI layer does not "
469 "automatically remap LUN assignments.\n");
470 } else if (sshdr->asc == 0x3f)
471 sdev_printk(KERN_WARNING, sdev,
472 "Operating parameters on this target have "
473 "changed. The Linux SCSI layer does not "
474 "automatically adjust these parameters.\n");
476 if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) {
477 evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED;
478 sdev_printk(KERN_WARNING, sdev,
479 "Warning! Received an indication that the "
480 "LUN reached a thin provisioning soft "
484 if (sshdr->asc == 0x29) {
485 evt_type = SDEV_EVT_POWER_ON_RESET_OCCURRED;
487 * Do not print message if it is an expected side-effect
490 if (!sdev->silence_suspend)
491 sdev_printk(KERN_WARNING, sdev,
492 "Power-on or device reset occurred\n");
495 if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) {
496 evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED;
497 sdev_printk(KERN_WARNING, sdev,
498 "Mode parameters changed");
499 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x06) {
500 evt_type = SDEV_EVT_ALUA_STATE_CHANGE_REPORTED;
501 sdev_printk(KERN_WARNING, sdev,
502 "Asymmetric access state changed");
503 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) {
504 evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED;
505 sdev_printk(KERN_WARNING, sdev,
506 "Capacity data has changed");
507 } else if (sshdr->asc == 0x2a)
508 sdev_printk(KERN_WARNING, sdev,
509 "Parameters changed");
512 if (evt_type != SDEV_EVT_MAXBITS) {
513 set_bit(evt_type, sdev->pending_events);
514 schedule_work(&sdev->event_work);
518 static inline void set_scsi_ml_byte(struct scsi_cmnd *cmd, u8 status)
520 cmd->result = (cmd->result & 0xffff00ff) | (status << 8);
524 * scsi_check_sense - Examine scsi cmd sense
525 * @scmd: Cmd to have sense checked.
528 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
531 * When a deferred error is detected the current command has
532 * not been executed and needs retrying.
534 enum scsi_disposition scsi_check_sense(struct scsi_cmnd *scmd)
536 struct scsi_device *sdev = scmd->device;
537 struct scsi_sense_hdr sshdr;
539 if (! scsi_command_normalize_sense(scmd, &sshdr))
540 return FAILED; /* no valid sense data */
542 scsi_report_sense(sdev, &sshdr);
544 if (scsi_sense_is_deferred(&sshdr))
547 if (sdev->handler && sdev->handler->check_sense) {
548 enum scsi_disposition rc;
550 rc = sdev->handler->check_sense(sdev, &sshdr);
551 if (rc != SCSI_RETURN_NOT_HANDLED)
553 /* handler does not care. Drop down to default handling */
556 if (scmd->cmnd[0] == TEST_UNIT_READY &&
557 scmd->submitter != SUBMITTED_BY_SCSI_ERROR_HANDLER)
559 * nasty: for mid-layer issued TURs, we need to return the
560 * actual sense data without any recovery attempt. For eh
561 * issued ones, we need to try to recover and interpret
566 * Previous logic looked for FILEMARK, EOM or ILI which are
567 * mainly associated with tapes and returned SUCCESS.
569 if (sshdr.response_code == 0x70) {
571 if (scmd->sense_buffer[2] & 0xe0)
575 * descriptor format: look for "stream commands sense data
576 * descriptor" (see SSC-3). Assume single sense data
577 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
579 if ((sshdr.additional_length > 3) &&
580 (scmd->sense_buffer[8] == 0x4) &&
581 (scmd->sense_buffer[11] & 0xe0))
585 switch (sshdr.sense_key) {
588 case RECOVERED_ERROR:
589 return /* soft_error */ SUCCESS;
591 case ABORTED_COMMAND:
592 if (sshdr.asc == 0x10) /* DIF */
595 if (sshdr.asc == 0x44 && sdev->sdev_bflags & BLIST_RETRY_ITF)
596 return ADD_TO_MLQUEUE;
597 if (sshdr.asc == 0xc1 && sshdr.ascq == 0x01 &&
598 sdev->sdev_bflags & BLIST_RETRY_ASC_C1)
599 return ADD_TO_MLQUEUE;
605 * if we are expecting a cc/ua because of a bus reset that we
606 * performed, treat this just as a retry. otherwise this is
607 * information that we should pass up to the upper-level driver
608 * so that we can deal with it there.
610 if (scmd->device->expecting_cc_ua) {
612 * Because some device does not queue unit
613 * attentions correctly, we carefully check
614 * additional sense code and qualifier so as
615 * not to squash media change unit attention.
617 if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) {
618 scmd->device->expecting_cc_ua = 0;
623 * we might also expect a cc/ua if another LUN on the target
624 * reported a UA with an ASC/ASCQ of 3F 0E -
625 * REPORTED LUNS DATA HAS CHANGED.
627 if (scmd->device->sdev_target->expecting_lun_change &&
628 sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
631 * if the device is in the process of becoming ready, we
634 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
637 * if the device is not started, we need to wake
638 * the error handler to start the motor
640 if (scmd->device->allow_restart &&
641 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
644 * Pass the UA upwards for a determination in the completion
649 /* these are not supported */
651 if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
652 /* Thin provisioning hard threshold reached */
653 set_scsi_ml_byte(scmd, SCSIML_STAT_NOSPC);
658 case VOLUME_OVERFLOW:
661 set_scsi_ml_byte(scmd, SCSIML_STAT_TGT_FAILURE);
665 if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
666 sshdr.asc == 0x13 || /* AMNF DATA FIELD */
667 sshdr.asc == 0x14) { /* RECORD NOT FOUND */
668 set_scsi_ml_byte(scmd, SCSIML_STAT_MED_ERROR);
674 if (scmd->device->retry_hwerror)
675 return ADD_TO_MLQUEUE;
677 set_scsi_ml_byte(scmd, SCSIML_STAT_TGT_FAILURE);
680 case ILLEGAL_REQUEST:
681 if (sshdr.asc == 0x20 || /* Invalid command operation code */
682 sshdr.asc == 0x21 || /* Logical block address out of range */
683 sshdr.asc == 0x22 || /* Invalid function */
684 sshdr.asc == 0x24 || /* Invalid field in cdb */
685 sshdr.asc == 0x26 || /* Parameter value invalid */
686 sshdr.asc == 0x27) { /* Write protected */
687 set_scsi_ml_byte(scmd, SCSIML_STAT_TGT_FAILURE);
695 EXPORT_SYMBOL_GPL(scsi_check_sense);
697 static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
699 struct scsi_host_template *sht = sdev->host->hostt;
700 struct scsi_device *tmp_sdev;
702 if (!sht->track_queue_depth ||
703 sdev->queue_depth >= sdev->max_queue_depth)
706 if (time_before(jiffies,
707 sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
710 if (time_before(jiffies,
711 sdev->last_queue_full_time + sdev->queue_ramp_up_period))
715 * Walk all devices of a target and do
718 shost_for_each_device(tmp_sdev, sdev->host) {
719 if (tmp_sdev->channel != sdev->channel ||
720 tmp_sdev->id != sdev->id ||
721 tmp_sdev->queue_depth == sdev->max_queue_depth)
724 scsi_change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1);
725 sdev->last_queue_ramp_up = jiffies;
729 static void scsi_handle_queue_full(struct scsi_device *sdev)
731 struct scsi_host_template *sht = sdev->host->hostt;
732 struct scsi_device *tmp_sdev;
734 if (!sht->track_queue_depth)
737 shost_for_each_device(tmp_sdev, sdev->host) {
738 if (tmp_sdev->channel != sdev->channel ||
739 tmp_sdev->id != sdev->id)
742 * We do not know the number of commands that were at
743 * the device when we got the queue full so we start
744 * from the highest possible value and work our way down.
746 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
751 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
752 * @scmd: SCSI cmd to examine.
755 * This is *only* called when we are examining the status of commands
756 * queued during error recovery. the main difference here is that we
757 * don't allow for the possibility of retries here, and we are a lot
758 * more restrictive about what we consider acceptable.
760 static enum scsi_disposition scsi_eh_completed_normally(struct scsi_cmnd *scmd)
763 * first check the host byte, to see if there is anything in there
764 * that would indicate what we need to do.
766 if (host_byte(scmd->result) == DID_RESET) {
768 * rats. we are already in the error handler, so we now
769 * get to try and figure out what to do next. if the sense
770 * is valid, we have a pretty good idea of what to do.
771 * if not, we mark it as FAILED.
773 return scsi_check_sense(scmd);
775 if (host_byte(scmd->result) != DID_OK)
779 * now, check the status byte to see if this indicates
782 switch (get_status_byte(scmd)) {
784 scsi_handle_queue_ramp_up(scmd->device);
786 case SAM_STAT_COMMAND_TERMINATED:
788 case SAM_STAT_CHECK_CONDITION:
789 return scsi_check_sense(scmd);
790 case SAM_STAT_CONDITION_MET:
791 case SAM_STAT_INTERMEDIATE:
792 case SAM_STAT_INTERMEDIATE_CONDITION_MET:
794 * who knows? FIXME(eric)
797 case SAM_STAT_RESERVATION_CONFLICT:
798 if (scmd->cmnd[0] == TEST_UNIT_READY)
799 /* it is a success, we probed the device and
802 /* otherwise, we failed to send the command */
804 case SAM_STAT_TASK_SET_FULL:
805 scsi_handle_queue_full(scmd->device);
816 * scsi_eh_done - Completion function for error handling.
817 * @scmd: Cmd that is done.
819 void scsi_eh_done(struct scsi_cmnd *scmd)
821 struct completion *eh_action;
823 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
824 "%s result: %x\n", __func__, scmd->result));
826 eh_action = scmd->device->host->eh_action;
832 * scsi_try_host_reset - ask host adapter to reset itself
833 * @scmd: SCSI cmd to send host reset.
835 static enum scsi_disposition scsi_try_host_reset(struct scsi_cmnd *scmd)
838 enum scsi_disposition rtn;
839 struct Scsi_Host *host = scmd->device->host;
840 struct scsi_host_template *hostt = host->hostt;
842 SCSI_LOG_ERROR_RECOVERY(3,
843 shost_printk(KERN_INFO, host, "Snd Host RST\n"));
845 if (!hostt->eh_host_reset_handler)
848 rtn = hostt->eh_host_reset_handler(scmd);
850 if (rtn == SUCCESS) {
851 if (!hostt->skip_settle_delay)
852 ssleep(HOST_RESET_SETTLE_TIME);
853 spin_lock_irqsave(host->host_lock, flags);
854 scsi_report_bus_reset(host, scmd_channel(scmd));
855 spin_unlock_irqrestore(host->host_lock, flags);
862 * scsi_try_bus_reset - ask host to perform a bus reset
863 * @scmd: SCSI cmd to send bus reset.
865 static enum scsi_disposition scsi_try_bus_reset(struct scsi_cmnd *scmd)
868 enum scsi_disposition rtn;
869 struct Scsi_Host *host = scmd->device->host;
870 struct scsi_host_template *hostt = host->hostt;
872 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
873 "%s: Snd Bus RST\n", __func__));
875 if (!hostt->eh_bus_reset_handler)
878 rtn = hostt->eh_bus_reset_handler(scmd);
880 if (rtn == SUCCESS) {
881 if (!hostt->skip_settle_delay)
882 ssleep(BUS_RESET_SETTLE_TIME);
883 spin_lock_irqsave(host->host_lock, flags);
884 scsi_report_bus_reset(host, scmd_channel(scmd));
885 spin_unlock_irqrestore(host->host_lock, flags);
891 static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
894 sdev->expecting_cc_ua = 1;
898 * scsi_try_target_reset - Ask host to perform a target reset
899 * @scmd: SCSI cmd used to send a target reset
902 * There is no timeout for this operation. if this operation is
903 * unreliable for a given host, then the host itself needs to put a
904 * timer on it, and set the host back to a consistent state prior to
907 static enum scsi_disposition scsi_try_target_reset(struct scsi_cmnd *scmd)
910 enum scsi_disposition rtn;
911 struct Scsi_Host *host = scmd->device->host;
912 struct scsi_host_template *hostt = host->hostt;
914 if (!hostt->eh_target_reset_handler)
917 rtn = hostt->eh_target_reset_handler(scmd);
918 if (rtn == SUCCESS) {
919 spin_lock_irqsave(host->host_lock, flags);
920 __starget_for_each_device(scsi_target(scmd->device), NULL,
921 __scsi_report_device_reset);
922 spin_unlock_irqrestore(host->host_lock, flags);
929 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
930 * @scmd: SCSI cmd used to send BDR
933 * There is no timeout for this operation. if this operation is
934 * unreliable for a given host, then the host itself needs to put a
935 * timer on it, and set the host back to a consistent state prior to
938 static enum scsi_disposition scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
940 enum scsi_disposition rtn;
941 struct scsi_host_template *hostt = scmd->device->host->hostt;
943 if (!hostt->eh_device_reset_handler)
946 rtn = hostt->eh_device_reset_handler(scmd);
948 __scsi_report_device_reset(scmd->device, NULL);
953 * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
954 * @hostt: SCSI driver host template
955 * @scmd: SCSI cmd used to send a target reset
958 * SUCCESS, FAILED, or FAST_IO_FAIL
961 * SUCCESS does not necessarily indicate that the command
962 * has been aborted; it only indicates that the LLDDs
963 * has cleared all references to that command.
964 * LLDDs should return FAILED only if an abort was required
965 * but could not be executed. LLDDs should return FAST_IO_FAIL
966 * if the device is temporarily unavailable (eg due to a
967 * link down on FibreChannel)
969 static enum scsi_disposition
970 scsi_try_to_abort_cmd(struct scsi_host_template *hostt, struct scsi_cmnd *scmd)
972 if (!hostt->eh_abort_handler)
975 return hostt->eh_abort_handler(scmd);
978 static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
980 if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
981 if (scsi_try_bus_device_reset(scmd) != SUCCESS)
982 if (scsi_try_target_reset(scmd) != SUCCESS)
983 if (scsi_try_bus_reset(scmd) != SUCCESS)
984 scsi_try_host_reset(scmd);
988 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
989 * @scmd: SCSI command structure to hijack
990 * @ses: structure to save restore information
991 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
992 * @cmnd_size: size in bytes of @cmnd (must be <= MAX_COMMAND_SIZE)
993 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
995 * This function is used to save a scsi command information before re-execution
996 * as part of the error recovery process. If @sense_bytes is 0 the command
997 * sent must be one that does not transfer any data. If @sense_bytes != 0
998 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
999 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
1001 void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
1002 unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
1004 struct scsi_device *sdev = scmd->device;
1007 * We need saved copies of a number of fields - this is because
1008 * error handling may need to overwrite these with different values
1009 * to run different commands, and once error handling is complete,
1010 * we will need to restore these values prior to running the actual
1013 ses->cmd_len = scmd->cmd_len;
1014 ses->data_direction = scmd->sc_data_direction;
1015 ses->sdb = scmd->sdb;
1016 ses->result = scmd->result;
1017 ses->resid_len = scmd->resid_len;
1018 ses->underflow = scmd->underflow;
1019 ses->prot_op = scmd->prot_op;
1020 ses->eh_eflags = scmd->eh_eflags;
1022 scmd->prot_op = SCSI_PROT_NORMAL;
1023 scmd->eh_eflags = 0;
1024 memcpy(ses->cmnd, scmd->cmnd, sizeof(ses->cmnd));
1025 memset(scmd->cmnd, 0, sizeof(scmd->cmnd));
1026 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
1028 scmd->resid_len = 0;
1031 scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
1033 sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
1035 scmd->sdb.table.sgl = &ses->sense_sgl;
1036 scmd->sc_data_direction = DMA_FROM_DEVICE;
1037 scmd->sdb.table.nents = scmd->sdb.table.orig_nents = 1;
1038 scmd->cmnd[0] = REQUEST_SENSE;
1039 scmd->cmnd[4] = scmd->sdb.length;
1040 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
1042 scmd->sc_data_direction = DMA_NONE;
1044 BUG_ON(cmnd_size > sizeof(scmd->cmnd));
1045 memcpy(scmd->cmnd, cmnd, cmnd_size);
1046 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
1050 scmd->underflow = 0;
1052 if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
1053 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
1054 (sdev->lun << 5 & 0xe0);
1057 * Zero the sense buffer. The scsi spec mandates that any
1058 * untransferred sense data should be interpreted as being zero.
1060 memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1062 EXPORT_SYMBOL(scsi_eh_prep_cmnd);
1065 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
1066 * @scmd: SCSI command structure to restore
1067 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
1069 * Undo any damage done by above scsi_eh_prep_cmnd().
1071 void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
1074 * Restore original data
1076 scmd->cmd_len = ses->cmd_len;
1077 memcpy(scmd->cmnd, ses->cmnd, sizeof(ses->cmnd));
1078 scmd->sc_data_direction = ses->data_direction;
1079 scmd->sdb = ses->sdb;
1080 scmd->result = ses->result;
1081 scmd->resid_len = ses->resid_len;
1082 scmd->underflow = ses->underflow;
1083 scmd->prot_op = ses->prot_op;
1084 scmd->eh_eflags = ses->eh_eflags;
1086 EXPORT_SYMBOL(scsi_eh_restore_cmnd);
1089 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
1090 * @scmd: SCSI command structure to hijack
1091 * @cmnd: CDB to send
1092 * @cmnd_size: size in bytes of @cmnd
1093 * @timeout: timeout for this request
1094 * @sense_bytes: size of sense data to copy or 0
1096 * This function is used to send a scsi command down to a target device
1097 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
1100 * SUCCESS or FAILED or NEEDS_RETRY
1102 static enum scsi_disposition scsi_send_eh_cmnd(struct scsi_cmnd *scmd,
1103 unsigned char *cmnd, int cmnd_size, int timeout, unsigned sense_bytes)
1105 struct scsi_device *sdev = scmd->device;
1106 struct Scsi_Host *shost = sdev->host;
1107 DECLARE_COMPLETION_ONSTACK(done);
1108 unsigned long timeleft = timeout, delay;
1109 struct scsi_eh_save ses;
1110 const unsigned long stall_for = msecs_to_jiffies(100);
1114 scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
1115 shost->eh_action = &done;
1117 scsi_log_send(scmd);
1118 scmd->submitter = SUBMITTED_BY_SCSI_ERROR_HANDLER;
1121 * Lock sdev->state_mutex to avoid that scsi_device_quiesce() can
1122 * change the SCSI device state after we have examined it and before
1123 * .queuecommand() is called.
1125 mutex_lock(&sdev->state_mutex);
1126 while (sdev->sdev_state == SDEV_BLOCK && timeleft > 0) {
1127 mutex_unlock(&sdev->state_mutex);
1128 SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_DEBUG, sdev,
1129 "%s: state %d <> %d\n", __func__, sdev->sdev_state,
1131 delay = min(timeleft, stall_for);
1133 msleep(jiffies_to_msecs(delay));
1134 mutex_lock(&sdev->state_mutex);
1136 if (sdev->sdev_state != SDEV_BLOCK)
1137 rtn = shost->hostt->queuecommand(shost, scmd);
1140 mutex_unlock(&sdev->state_mutex);
1143 if (timeleft > stall_for) {
1144 scsi_eh_restore_cmnd(scmd, &ses);
1146 timeleft -= stall_for;
1147 msleep(jiffies_to_msecs(stall_for));
1150 /* signal not to enter either branch of the if () below */
1154 timeleft = wait_for_completion_timeout(&done, timeout);
1158 shost->eh_action = NULL;
1160 scsi_log_completion(scmd, rtn);
1162 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1163 "%s timeleft: %ld\n",
1164 __func__, timeleft));
1167 * If there is time left scsi_eh_done got called, and we will examine
1168 * the actual status codes to see whether the command actually did
1169 * complete normally, else if we have a zero return and no time left,
1170 * the command must still be pending, so abort it and return FAILED.
1171 * If we never actually managed to issue the command, because
1172 * ->queuecommand() kept returning non zero, use the rtn = FAILED
1173 * value above (so don't execute either branch of the if)
1176 rtn = scsi_eh_completed_normally(scmd);
1177 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1178 "%s: scsi_eh_completed_normally %x\n", __func__, rtn));
1185 case ADD_TO_MLQUEUE:
1192 } else if (rtn != FAILED) {
1193 scsi_abort_eh_cmnd(scmd);
1197 scsi_eh_restore_cmnd(scmd, &ses);
1203 * scsi_request_sense - Request sense data from a particular target.
1204 * @scmd: SCSI cmd for request sense.
1207 * Some hosts automatically obtain this information, others require
1208 * that we obtain it on our own. This function will *not* return until
1209 * the command either times out, or it completes.
1211 static enum scsi_disposition scsi_request_sense(struct scsi_cmnd *scmd)
1213 return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
1216 static enum scsi_disposition
1217 scsi_eh_action(struct scsi_cmnd *scmd, enum scsi_disposition rtn)
1219 if (!blk_rq_is_passthrough(scsi_cmd_to_rq(scmd))) {
1220 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
1221 if (sdrv->eh_action)
1222 rtn = sdrv->eh_action(scmd, rtn);
1228 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
1229 * @scmd: Original SCSI cmd that eh has finished.
1230 * @done_q: Queue for processed commands.
1233 * We don't want to use the normal command completion while we are are
1234 * still handling errors - it may cause other commands to be queued,
1235 * and that would disturb what we are doing. Thus we really want to
1236 * keep a list of pending commands for final completion, and once we
1237 * are ready to leave error handling we handle completion for real.
1239 void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
1241 list_move_tail(&scmd->eh_entry, done_q);
1243 EXPORT_SYMBOL(scsi_eh_finish_cmd);
1246 * scsi_eh_get_sense - Get device sense data.
1247 * @work_q: Queue of commands to process.
1248 * @done_q: Queue of processed commands.
1251 * See if we need to request sense information. if so, then get it
1252 * now, so we have a better idea of what to do.
1255 * This has the unfortunate side effect that if a shost adapter does
1256 * not automatically request sense information, we end up shutting
1257 * it down before we request it.
1259 * All drivers should request sense information internally these days,
1260 * so for now all I have to say is tough noogies if you end up in here.
1262 * XXX: Long term this code should go away, but that needs an audit of
1265 int scsi_eh_get_sense(struct list_head *work_q,
1266 struct list_head *done_q)
1268 struct scsi_cmnd *scmd, *next;
1269 struct Scsi_Host *shost;
1270 enum scsi_disposition rtn;
1273 * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
1274 * should not get sense.
1276 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1277 if ((scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) ||
1278 SCSI_SENSE_VALID(scmd))
1281 shost = scmd->device->host;
1282 if (scsi_host_eh_past_deadline(shost)) {
1283 SCSI_LOG_ERROR_RECOVERY(3,
1284 scmd_printk(KERN_INFO, scmd,
1285 "%s: skip request sense, past eh deadline\n",
1289 if (!scsi_status_is_check_condition(scmd->result))
1291 * don't request sense if there's no check condition
1292 * status because the error we're processing isn't one
1293 * that has a sense code (and some devices get
1294 * confused by sense requests out of the blue)
1298 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
1299 "%s: requesting sense\n",
1301 rtn = scsi_request_sense(scmd);
1305 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1306 "sense requested, result %x\n", scmd->result));
1307 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd));
1309 rtn = scsi_decide_disposition(scmd);
1312 * if the result was normal, then just pass it along to the
1317 * We don't want this command reissued, just finished
1318 * with the sense data, so set retries to the max
1319 * allowed to ensure it won't get reissued. If the user
1320 * has requested infinite retries, we also want to
1321 * finish this command, so force completion by setting
1322 * retries and allowed to the same value.
1324 if (scmd->allowed == SCSI_CMD_RETRIES_NO_LIMIT)
1325 scmd->retries = scmd->allowed = 1;
1327 scmd->retries = scmd->allowed;
1328 else if (rtn != NEEDS_RETRY)
1331 scsi_eh_finish_cmd(scmd, done_q);
1334 return list_empty(work_q);
1336 EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
1339 * scsi_eh_tur - Send TUR to device.
1340 * @scmd: &scsi_cmnd to send TUR
1343 * 0 - Device is ready. 1 - Device NOT ready.
1345 static int scsi_eh_tur(struct scsi_cmnd *scmd)
1347 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
1349 enum scsi_disposition rtn;
1352 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
1353 scmd->device->eh_timeout, 0);
1355 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1356 "%s return: %x\n", __func__, rtn));
1371 * scsi_eh_test_devices - check if devices are responding from error recovery.
1372 * @cmd_list: scsi commands in error recovery.
1373 * @work_q: queue for commands which still need more error recovery
1374 * @done_q: queue for commands which are finished
1375 * @try_stu: boolean on if a STU command should be tried in addition to TUR.
1378 * Tests if devices are in a working state. Commands to devices now in
1379 * a working state are sent to the done_q while commands to devices which
1380 * are still failing to respond are returned to the work_q for more
1383 static int scsi_eh_test_devices(struct list_head *cmd_list,
1384 struct list_head *work_q,
1385 struct list_head *done_q, int try_stu)
1387 struct scsi_cmnd *scmd, *next;
1388 struct scsi_device *sdev;
1391 while (!list_empty(cmd_list)) {
1392 scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
1393 sdev = scmd->device;
1396 if (scsi_host_eh_past_deadline(sdev->host)) {
1397 /* Push items back onto work_q */
1398 list_splice_init(cmd_list, work_q);
1399 SCSI_LOG_ERROR_RECOVERY(3,
1400 sdev_printk(KERN_INFO, sdev,
1401 "%s: skip test device, past eh deadline",
1407 finish_cmds = !scsi_device_online(scmd->device) ||
1408 (try_stu && !scsi_eh_try_stu(scmd) &&
1409 !scsi_eh_tur(scmd)) ||
1412 list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
1413 if (scmd->device == sdev) {
1416 scsi_eh_action(scmd, SUCCESS) == SUCCESS))
1417 scsi_eh_finish_cmd(scmd, done_q);
1419 list_move_tail(&scmd->eh_entry, work_q);
1422 return list_empty(work_q);
1426 * scsi_eh_try_stu - Send START_UNIT to device.
1427 * @scmd: &scsi_cmnd to send START_UNIT
1430 * 0 - Device is ready. 1 - Device NOT ready.
1432 static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
1434 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
1436 if (scmd->device->allow_restart) {
1438 enum scsi_disposition rtn = NEEDS_RETRY;
1440 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
1441 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6,
1442 scmd->device->eh_timeout, 0);
1452 * scsi_eh_stu - send START_UNIT if needed
1453 * @shost: &scsi host being recovered.
1454 * @work_q: &list_head for pending commands.
1455 * @done_q: &list_head for processed commands.
1458 * If commands are failing due to not ready, initializing command required,
1459 * try revalidating the device, which will end up sending a start unit.
1461 static int scsi_eh_stu(struct Scsi_Host *shost,
1462 struct list_head *work_q,
1463 struct list_head *done_q)
1465 struct scsi_cmnd *scmd, *stu_scmd, *next;
1466 struct scsi_device *sdev;
1468 shost_for_each_device(sdev, shost) {
1469 if (scsi_host_eh_past_deadline(shost)) {
1470 SCSI_LOG_ERROR_RECOVERY(3,
1471 sdev_printk(KERN_INFO, sdev,
1472 "%s: skip START_UNIT, past eh deadline\n",
1474 scsi_device_put(sdev);
1478 list_for_each_entry(scmd, work_q, eh_entry)
1479 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
1480 scsi_check_sense(scmd) == FAILED ) {
1488 SCSI_LOG_ERROR_RECOVERY(3,
1489 sdev_printk(KERN_INFO, sdev,
1490 "%s: Sending START_UNIT\n",
1493 if (!scsi_eh_try_stu(stu_scmd)) {
1494 if (!scsi_device_online(sdev) ||
1495 !scsi_eh_tur(stu_scmd)) {
1496 list_for_each_entry_safe(scmd, next,
1498 if (scmd->device == sdev &&
1499 scsi_eh_action(scmd, SUCCESS) == SUCCESS)
1500 scsi_eh_finish_cmd(scmd, done_q);
1504 SCSI_LOG_ERROR_RECOVERY(3,
1505 sdev_printk(KERN_INFO, sdev,
1506 "%s: START_UNIT failed\n",
1511 return list_empty(work_q);
1516 * scsi_eh_bus_device_reset - send bdr if needed
1517 * @shost: scsi host being recovered.
1518 * @work_q: &list_head for pending commands.
1519 * @done_q: &list_head for processed commands.
1522 * Try a bus device reset. Still, look to see whether we have multiple
1523 * devices that are jammed or not - if we have multiple devices, it
1524 * makes no sense to try bus_device_reset - we really would need to try
1525 * a bus_reset instead.
1527 static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1528 struct list_head *work_q,
1529 struct list_head *done_q)
1531 struct scsi_cmnd *scmd, *bdr_scmd, *next;
1532 struct scsi_device *sdev;
1533 enum scsi_disposition rtn;
1535 shost_for_each_device(sdev, shost) {
1536 if (scsi_host_eh_past_deadline(shost)) {
1537 SCSI_LOG_ERROR_RECOVERY(3,
1538 sdev_printk(KERN_INFO, sdev,
1539 "%s: skip BDR, past eh deadline\n",
1541 scsi_device_put(sdev);
1545 list_for_each_entry(scmd, work_q, eh_entry)
1546 if (scmd->device == sdev) {
1554 SCSI_LOG_ERROR_RECOVERY(3,
1555 sdev_printk(KERN_INFO, sdev,
1556 "%s: Sending BDR\n", current->comm));
1557 rtn = scsi_try_bus_device_reset(bdr_scmd);
1558 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
1559 if (!scsi_device_online(sdev) ||
1560 rtn == FAST_IO_FAIL ||
1561 !scsi_eh_tur(bdr_scmd)) {
1562 list_for_each_entry_safe(scmd, next,
1564 if (scmd->device == sdev &&
1565 scsi_eh_action(scmd, rtn) != FAILED)
1566 scsi_eh_finish_cmd(scmd,
1571 SCSI_LOG_ERROR_RECOVERY(3,
1572 sdev_printk(KERN_INFO, sdev,
1573 "%s: BDR failed\n", current->comm));
1577 return list_empty(work_q);
1581 * scsi_eh_target_reset - send target reset if needed
1582 * @shost: scsi host being recovered.
1583 * @work_q: &list_head for pending commands.
1584 * @done_q: &list_head for processed commands.
1587 * Try a target reset.
1589 static int scsi_eh_target_reset(struct Scsi_Host *shost,
1590 struct list_head *work_q,
1591 struct list_head *done_q)
1593 LIST_HEAD(tmp_list);
1594 LIST_HEAD(check_list);
1596 list_splice_init(work_q, &tmp_list);
1598 while (!list_empty(&tmp_list)) {
1599 struct scsi_cmnd *next, *scmd;
1600 enum scsi_disposition rtn;
1603 if (scsi_host_eh_past_deadline(shost)) {
1604 /* push back on work queue for further processing */
1605 list_splice_init(&check_list, work_q);
1606 list_splice_init(&tmp_list, work_q);
1607 SCSI_LOG_ERROR_RECOVERY(3,
1608 shost_printk(KERN_INFO, shost,
1609 "%s: Skip target reset, past eh deadline\n",
1611 return list_empty(work_q);
1614 scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
1617 SCSI_LOG_ERROR_RECOVERY(3,
1618 shost_printk(KERN_INFO, shost,
1619 "%s: Sending target reset to target %d\n",
1620 current->comm, id));
1621 rtn = scsi_try_target_reset(scmd);
1622 if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
1623 SCSI_LOG_ERROR_RECOVERY(3,
1624 shost_printk(KERN_INFO, shost,
1625 "%s: Target reset failed"
1627 current->comm, id));
1628 list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
1629 if (scmd_id(scmd) != id)
1633 list_move_tail(&scmd->eh_entry, &check_list);
1634 else if (rtn == FAST_IO_FAIL)
1635 scsi_eh_finish_cmd(scmd, done_q);
1637 /* push back on work queue for further processing */
1638 list_move(&scmd->eh_entry, work_q);
1642 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1646 * scsi_eh_bus_reset - send a bus reset
1647 * @shost: &scsi host being recovered.
1648 * @work_q: &list_head for pending commands.
1649 * @done_q: &list_head for processed commands.
1651 static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1652 struct list_head *work_q,
1653 struct list_head *done_q)
1655 struct scsi_cmnd *scmd, *chan_scmd, *next;
1656 LIST_HEAD(check_list);
1657 unsigned int channel;
1658 enum scsi_disposition rtn;
1661 * we really want to loop over the various channels, and do this on
1662 * a channel by channel basis. we should also check to see if any
1663 * of the failed commands are on soft_reset devices, and if so, skip
1667 for (channel = 0; channel <= shost->max_channel; channel++) {
1668 if (scsi_host_eh_past_deadline(shost)) {
1669 list_splice_init(&check_list, work_q);
1670 SCSI_LOG_ERROR_RECOVERY(3,
1671 shost_printk(KERN_INFO, shost,
1672 "%s: skip BRST, past eh deadline\n",
1674 return list_empty(work_q);
1678 list_for_each_entry(scmd, work_q, eh_entry) {
1679 if (channel == scmd_channel(scmd)) {
1683 * FIXME add back in some support for
1684 * soft_reset devices.
1691 SCSI_LOG_ERROR_RECOVERY(3,
1692 shost_printk(KERN_INFO, shost,
1693 "%s: Sending BRST chan: %d\n",
1694 current->comm, channel));
1695 rtn = scsi_try_bus_reset(chan_scmd);
1696 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
1697 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1698 if (channel == scmd_channel(scmd)) {
1699 if (rtn == FAST_IO_FAIL)
1700 scsi_eh_finish_cmd(scmd,
1703 list_move_tail(&scmd->eh_entry,
1708 SCSI_LOG_ERROR_RECOVERY(3,
1709 shost_printk(KERN_INFO, shost,
1710 "%s: BRST failed chan: %d\n",
1711 current->comm, channel));
1714 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1718 * scsi_eh_host_reset - send a host reset
1719 * @shost: host to be reset.
1720 * @work_q: &list_head for pending commands.
1721 * @done_q: &list_head for processed commands.
1723 static int scsi_eh_host_reset(struct Scsi_Host *shost,
1724 struct list_head *work_q,
1725 struct list_head *done_q)
1727 struct scsi_cmnd *scmd, *next;
1728 LIST_HEAD(check_list);
1729 enum scsi_disposition rtn;
1731 if (!list_empty(work_q)) {
1732 scmd = list_entry(work_q->next,
1733 struct scsi_cmnd, eh_entry);
1735 SCSI_LOG_ERROR_RECOVERY(3,
1736 shost_printk(KERN_INFO, shost,
1737 "%s: Sending HRST\n",
1740 rtn = scsi_try_host_reset(scmd);
1741 if (rtn == SUCCESS) {
1742 list_splice_init(work_q, &check_list);
1743 } else if (rtn == FAST_IO_FAIL) {
1744 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1745 scsi_eh_finish_cmd(scmd, done_q);
1748 SCSI_LOG_ERROR_RECOVERY(3,
1749 shost_printk(KERN_INFO, shost,
1750 "%s: HRST failed\n",
1754 return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
1758 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
1759 * @work_q: &list_head for pending commands.
1760 * @done_q: &list_head for processed commands.
1762 static void scsi_eh_offline_sdevs(struct list_head *work_q,
1763 struct list_head *done_q)
1765 struct scsi_cmnd *scmd, *next;
1766 struct scsi_device *sdev;
1768 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1769 sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
1770 "not ready after error recovery\n");
1771 sdev = scmd->device;
1773 mutex_lock(&sdev->state_mutex);
1774 scsi_device_set_state(sdev, SDEV_OFFLINE);
1775 mutex_unlock(&sdev->state_mutex);
1777 scsi_eh_finish_cmd(scmd, done_q);
1783 * scsi_noretry_cmd - determine if command should be failed fast
1784 * @scmd: SCSI cmd to examine.
1786 bool scsi_noretry_cmd(struct scsi_cmnd *scmd)
1788 struct request *req = scsi_cmd_to_rq(scmd);
1790 switch (host_byte(scmd->result)) {
1796 return !!(req->cmd_flags & REQ_FAILFAST_TRANSPORT);
1798 return !!(req->cmd_flags & REQ_FAILFAST_DEV);
1800 if (get_status_byte(scmd) == SAM_STAT_RESERVATION_CONFLICT)
1803 case DID_SOFT_ERROR:
1804 return !!(req->cmd_flags & REQ_FAILFAST_DRIVER);
1807 if (!scsi_status_is_check_condition(scmd->result))
1812 * assume caller has checked sense and determined
1813 * the check condition was retryable.
1815 if (req->cmd_flags & REQ_FAILFAST_DEV || blk_rq_is_passthrough(req))
1822 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1823 * @scmd: SCSI cmd to examine.
1826 * This is *only* called when we are examining the status after sending
1827 * out the actual data command. any commands that are queued for error
1828 * recovery (e.g. test_unit_ready) do *not* come through here.
1830 * When this routine returns failed, it means the error handler thread
1831 * is woken. In cases where the error code indicates an error that
1832 * doesn't require the error handler read (i.e. we don't need to
1833 * abort/reset), this function should return SUCCESS.
1835 enum scsi_disposition scsi_decide_disposition(struct scsi_cmnd *scmd)
1837 enum scsi_disposition rtn;
1840 * if the device is offline, then we clearly just pass the result back
1841 * up to the top level.
1843 if (!scsi_device_online(scmd->device)) {
1844 SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO, scmd,
1845 "%s: device offline - report as SUCCESS\n", __func__));
1850 * first check the host byte, to see if there is anything in there
1851 * that would indicate what we need to do.
1853 switch (host_byte(scmd->result)) {
1854 case DID_PASSTHROUGH:
1856 * no matter what, pass this through to the upper layer.
1857 * nuke this special code so that it looks like we are saying
1860 scmd->result &= 0xff00ffff;
1864 * looks good. drop through, and check the next byte.
1868 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
1869 set_host_byte(scmd, DID_TIME_OUT);
1873 case DID_NO_CONNECT:
1874 case DID_BAD_TARGET:
1876 * note - this means that we just report the status back
1877 * to the top level driver, not that we actually think
1878 * that it indicates SUCCESS.
1881 case DID_SOFT_ERROR:
1883 * when the low level driver returns did_soft_error,
1884 * it is responsible for keeping an internal retry counter
1885 * in order to avoid endless loops (db)
1892 return ADD_TO_MLQUEUE;
1893 case DID_TRANSPORT_DISRUPTED:
1895 * LLD/transport was disrupted during processing of the IO.
1896 * The transport class is now blocked/blocking,
1897 * and the transport will decide what to do with the IO
1898 * based on its timers and recovery capablilities if
1899 * there are enough retries.
1902 case DID_TRANSPORT_FAILFAST:
1904 * The transport decided to failfast the IO (most likely
1905 * the fast io fail tmo fired), so send IO directly upwards.
1908 case DID_TRANSPORT_MARGINAL:
1910 * caller has decided not to do retries on
1911 * abort success, so send IO directly upwards
1915 if (get_status_byte(scmd) == SAM_STAT_RESERVATION_CONFLICT)
1917 * execute reservation conflict processing code
1927 * when we scan the bus, we get timeout messages for
1928 * these commands if there is no device available.
1929 * other hosts report did_no_connect for the same thing.
1931 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1932 scmd->cmnd[0] == INQUIRY)) {
1944 * check the status byte to see if this indicates anything special.
1946 switch (get_status_byte(scmd)) {
1947 case SAM_STAT_TASK_SET_FULL:
1948 scsi_handle_queue_full(scmd->device);
1950 * the case of trying to send too many commands to a
1951 * tagged queueing device.
1956 * device can't talk to us at the moment. Should only
1957 * occur (SAM-3) when the task queue is empty, so will cause
1958 * the empty queue handling to trigger a stall in the
1961 return ADD_TO_MLQUEUE;
1963 if (scmd->cmnd[0] == REPORT_LUNS)
1964 scmd->device->sdev_target->expecting_lun_change = 0;
1965 scsi_handle_queue_ramp_up(scmd->device);
1967 case SAM_STAT_COMMAND_TERMINATED:
1969 case SAM_STAT_TASK_ABORTED:
1971 case SAM_STAT_CHECK_CONDITION:
1972 rtn = scsi_check_sense(scmd);
1973 if (rtn == NEEDS_RETRY)
1975 /* if rtn == FAILED, we have no sense information;
1976 * returning FAILED will wake the error handler thread
1977 * to collect the sense and redo the decide
1980 case SAM_STAT_CONDITION_MET:
1981 case SAM_STAT_INTERMEDIATE:
1982 case SAM_STAT_INTERMEDIATE_CONDITION_MET:
1983 case SAM_STAT_ACA_ACTIVE:
1985 * who knows? FIXME(eric)
1989 case SAM_STAT_RESERVATION_CONFLICT:
1990 sdev_printk(KERN_INFO, scmd->device,
1991 "reservation conflict\n");
1992 set_scsi_ml_byte(scmd, SCSIML_STAT_RESV_CONFLICT);
1993 return SUCCESS; /* causes immediate i/o error */
1999 /* we requeue for retry because the error was retryable, and
2000 * the request was not marked fast fail. Note that above,
2001 * even if the request is marked fast fail, we still requeue
2002 * for queue congestion conditions (QUEUE_FULL or BUSY) */
2003 if (scsi_cmd_retry_allowed(scmd) && !scsi_noretry_cmd(scmd)) {
2007 * no more retries - report this one back to upper level.
2013 static enum rq_end_io_ret eh_lock_door_done(struct request *req,
2014 blk_status_t status)
2016 blk_mq_free_request(req);
2017 return RQ_END_IO_NONE;
2021 * scsi_eh_lock_door - Prevent medium removal for the specified device
2022 * @sdev: SCSI device to prevent medium removal
2025 * We must be called from process context.
2028 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
2029 * head of the devices request queue, and continue.
2031 static void scsi_eh_lock_door(struct scsi_device *sdev)
2033 struct scsi_cmnd *scmd;
2034 struct request *req;
2036 req = scsi_alloc_request(sdev->request_queue, REQ_OP_DRV_IN, 0);
2039 scmd = blk_mq_rq_to_pdu(req);
2041 scmd->cmnd[0] = ALLOW_MEDIUM_REMOVAL;
2045 scmd->cmnd[4] = SCSI_REMOVAL_PREVENT;
2047 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
2050 req->rq_flags |= RQF_QUIET;
2051 req->timeout = 10 * HZ;
2052 req->end_io = eh_lock_door_done;
2054 blk_execute_rq_nowait(req, true);
2058 * scsi_restart_operations - restart io operations to the specified host.
2059 * @shost: Host we are restarting.
2062 * When we entered the error handler, we blocked all further i/o to
2063 * this device. we need to 'reverse' this process.
2065 static void scsi_restart_operations(struct Scsi_Host *shost)
2067 struct scsi_device *sdev;
2068 unsigned long flags;
2071 * If the door was locked, we need to insert a door lock request
2072 * onto the head of the SCSI request queue for the device. There
2073 * is no point trying to lock the door of an off-line device.
2075 shost_for_each_device(sdev, shost) {
2076 if (scsi_device_online(sdev) && sdev->was_reset && sdev->locked) {
2077 scsi_eh_lock_door(sdev);
2078 sdev->was_reset = 0;
2083 * next free up anything directly waiting upon the host. this
2084 * will be requests for character device operations, and also for
2085 * ioctls to queued block devices.
2087 SCSI_LOG_ERROR_RECOVERY(3,
2088 shost_printk(KERN_INFO, shost, "waking up host to restart\n"));
2090 spin_lock_irqsave(shost->host_lock, flags);
2091 if (scsi_host_set_state(shost, SHOST_RUNNING))
2092 if (scsi_host_set_state(shost, SHOST_CANCEL))
2093 BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
2094 spin_unlock_irqrestore(shost->host_lock, flags);
2096 wake_up(&shost->host_wait);
2099 * finally we need to re-initiate requests that may be pending. we will
2100 * have had everything blocked while error handling is taking place, and
2101 * now that error recovery is done, we will need to ensure that these
2102 * requests are started.
2104 scsi_run_host_queues(shost);
2107 * if eh is active and host_eh_scheduled is pending we need to re-run
2108 * recovery. we do this check after scsi_run_host_queues() to allow
2109 * everything pent up since the last eh run a chance to make forward
2110 * progress before we sync again. Either we'll immediately re-run
2111 * recovery or scsi_device_unbusy() will wake us again when these
2112 * pending commands complete.
2114 spin_lock_irqsave(shost->host_lock, flags);
2115 if (shost->host_eh_scheduled)
2116 if (scsi_host_set_state(shost, SHOST_RECOVERY))
2117 WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY));
2118 spin_unlock_irqrestore(shost->host_lock, flags);
2122 * scsi_eh_ready_devs - check device ready state and recover if not.
2123 * @shost: host to be recovered.
2124 * @work_q: &list_head for pending commands.
2125 * @done_q: &list_head for processed commands.
2127 void scsi_eh_ready_devs(struct Scsi_Host *shost,
2128 struct list_head *work_q,
2129 struct list_head *done_q)
2131 if (!scsi_eh_stu(shost, work_q, done_q))
2132 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
2133 if (!scsi_eh_target_reset(shost, work_q, done_q))
2134 if (!scsi_eh_bus_reset(shost, work_q, done_q))
2135 if (!scsi_eh_host_reset(shost, work_q, done_q))
2136 scsi_eh_offline_sdevs(work_q,
2139 EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
2142 * scsi_eh_flush_done_q - finish processed commands or retry them.
2143 * @done_q: list_head of processed commands.
2145 void scsi_eh_flush_done_q(struct list_head *done_q)
2147 struct scsi_cmnd *scmd, *next;
2149 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
2150 list_del_init(&scmd->eh_entry);
2151 if (scsi_device_online(scmd->device) &&
2152 !scsi_noretry_cmd(scmd) && scsi_cmd_retry_allowed(scmd) &&
2153 scsi_eh_should_retry_cmd(scmd)) {
2154 SCSI_LOG_ERROR_RECOVERY(3,
2155 scmd_printk(KERN_INFO, scmd,
2156 "%s: flush retry cmd\n",
2158 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
2161 * If just we got sense for the device (called
2162 * scsi_eh_get_sense), scmd->result is already
2163 * set, do not set DID_TIME_OUT.
2166 scmd->result |= (DID_TIME_OUT << 16);
2167 SCSI_LOG_ERROR_RECOVERY(3,
2168 scmd_printk(KERN_INFO, scmd,
2169 "%s: flush finish cmd\n",
2171 scsi_finish_command(scmd);
2175 EXPORT_SYMBOL(scsi_eh_flush_done_q);
2178 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
2179 * @shost: Host to unjam.
2182 * When we come in here, we *know* that all commands on the bus have
2183 * either completed, failed or timed out. we also know that no further
2184 * commands are being sent to the host, so things are relatively quiet
2185 * and we have freedom to fiddle with things as we wish.
2187 * This is only the *default* implementation. it is possible for
2188 * individual drivers to supply their own version of this function, and
2189 * if the maintainer wishes to do this, it is strongly suggested that
2190 * this function be taken as a template and modified. this function
2191 * was designed to correctly handle problems for about 95% of the
2192 * different cases out there, and it should always provide at least a
2193 * reasonable amount of error recovery.
2195 * Any command marked 'failed' or 'timeout' must eventually have
2196 * scsi_finish_cmd() called for it. we do all of the retry stuff
2197 * here, so when we restart the host after we return it should have an
2200 static void scsi_unjam_host(struct Scsi_Host *shost)
2202 unsigned long flags;
2203 LIST_HEAD(eh_work_q);
2204 LIST_HEAD(eh_done_q);
2206 spin_lock_irqsave(shost->host_lock, flags);
2207 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
2208 spin_unlock_irqrestore(shost->host_lock, flags);
2210 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
2212 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
2213 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
2215 spin_lock_irqsave(shost->host_lock, flags);
2216 if (shost->eh_deadline != -1)
2217 shost->last_reset = 0;
2218 spin_unlock_irqrestore(shost->host_lock, flags);
2219 scsi_eh_flush_done_q(&eh_done_q);
2223 * scsi_error_handler - SCSI error handler thread
2224 * @data: Host for which we are running.
2227 * This is the main error handling loop. This is run as a kernel thread
2228 * for every SCSI host and handles all error handling activity.
2230 int scsi_error_handler(void *data)
2232 struct Scsi_Host *shost = data;
2235 * We use TASK_INTERRUPTIBLE so that the thread is not
2236 * counted against the load average as a running process.
2237 * We never actually get interrupted because kthread_run
2238 * disables signal delivery for the created thread.
2242 * The sequence in kthread_stop() sets the stop flag first
2243 * then wakes the process. To avoid missed wakeups, the task
2244 * should always be in a non running state before the stop
2247 set_current_state(TASK_INTERRUPTIBLE);
2248 if (kthread_should_stop())
2251 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
2252 shost->host_failed != scsi_host_busy(shost)) {
2253 SCSI_LOG_ERROR_RECOVERY(1,
2254 shost_printk(KERN_INFO, shost,
2255 "scsi_eh_%d: sleeping\n",
2261 __set_current_state(TASK_RUNNING);
2262 SCSI_LOG_ERROR_RECOVERY(1,
2263 shost_printk(KERN_INFO, shost,
2264 "scsi_eh_%d: waking up %d/%d/%d\n",
2265 shost->host_no, shost->host_eh_scheduled,
2267 scsi_host_busy(shost)));
2270 * We have a host that is failing for some reason. Figure out
2271 * what we need to do to get it up and online again (if we can).
2272 * If we fail, we end up taking the thing offline.
2274 if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
2275 SCSI_LOG_ERROR_RECOVERY(1,
2276 shost_printk(KERN_ERR, shost,
2277 "scsi_eh_%d: unable to autoresume\n",
2282 if (shost->transportt->eh_strategy_handler)
2283 shost->transportt->eh_strategy_handler(shost);
2285 scsi_unjam_host(shost);
2287 /* All scmds have been handled */
2288 shost->host_failed = 0;
2291 * Note - if the above fails completely, the action is to take
2292 * individual devices offline and flush the queue of any
2293 * outstanding requests that may have been pending. When we
2294 * restart, we restart any I/O to any other devices on the bus
2295 * which are still online.
2297 scsi_restart_operations(shost);
2298 if (!shost->eh_noresume)
2299 scsi_autopm_put_host(shost);
2301 __set_current_state(TASK_RUNNING);
2303 SCSI_LOG_ERROR_RECOVERY(1,
2304 shost_printk(KERN_INFO, shost,
2305 "Error handler scsi_eh_%d exiting\n",
2307 shost->ehandler = NULL;
2312 * Function: scsi_report_bus_reset()
2314 * Purpose: Utility function used by low-level drivers to report that
2315 * they have observed a bus reset on the bus being handled.
2317 * Arguments: shost - Host in question
2318 * channel - channel on which reset was observed.
2322 * Lock status: Host lock must be held.
2324 * Notes: This only needs to be called if the reset is one which
2325 * originates from an unknown location. Resets originated
2326 * by the mid-level itself don't need to call this, but there
2327 * should be no harm.
2329 * The main purpose of this is to make sure that a CHECK_CONDITION
2330 * is properly treated.
2332 void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
2334 struct scsi_device *sdev;
2336 __shost_for_each_device(sdev, shost) {
2337 if (channel == sdev_channel(sdev))
2338 __scsi_report_device_reset(sdev, NULL);
2341 EXPORT_SYMBOL(scsi_report_bus_reset);
2344 * Function: scsi_report_device_reset()
2346 * Purpose: Utility function used by low-level drivers to report that
2347 * they have observed a device reset on the device being handled.
2349 * Arguments: shost - Host in question
2350 * channel - channel on which reset was observed
2351 * target - target on which reset was observed
2355 * Lock status: Host lock must be held
2357 * Notes: This only needs to be called if the reset is one which
2358 * originates from an unknown location. Resets originated
2359 * by the mid-level itself don't need to call this, but there
2360 * should be no harm.
2362 * The main purpose of this is to make sure that a CHECK_CONDITION
2363 * is properly treated.
2365 void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
2367 struct scsi_device *sdev;
2369 __shost_for_each_device(sdev, shost) {
2370 if (channel == sdev_channel(sdev) &&
2371 target == sdev_id(sdev))
2372 __scsi_report_device_reset(sdev, NULL);
2375 EXPORT_SYMBOL(scsi_report_device_reset);
2378 * scsi_ioctl_reset: explicitly reset a host/bus/target/device
2379 * @dev: scsi_device to operate on
2380 * @arg: reset type (see sg.h)
2383 scsi_ioctl_reset(struct scsi_device *dev, int __user *arg)
2385 struct scsi_cmnd *scmd;
2386 struct Scsi_Host *shost = dev->host;
2388 unsigned long flags;
2390 enum scsi_disposition rtn;
2392 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2395 error = get_user(val, arg);
2399 if (scsi_autopm_get_host(shost) < 0)
2403 rq = kzalloc(sizeof(struct request) + sizeof(struct scsi_cmnd) +
2404 shost->hostt->cmd_size, GFP_KERNEL);
2406 goto out_put_autopm_host;
2407 blk_rq_init(NULL, rq);
2409 scmd = (struct scsi_cmnd *)(rq + 1);
2410 scsi_init_command(dev, scmd);
2412 scmd->submitter = SUBMITTED_BY_SCSI_RESET_IOCTL;
2413 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
2417 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
2419 spin_lock_irqsave(shost->host_lock, flags);
2420 shost->tmf_in_progress = 1;
2421 spin_unlock_irqrestore(shost->host_lock, flags);
2423 switch (val & ~SG_SCSI_RESET_NO_ESCALATE) {
2424 case SG_SCSI_RESET_NOTHING:
2427 case SG_SCSI_RESET_DEVICE:
2428 rtn = scsi_try_bus_device_reset(scmd);
2429 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
2432 case SG_SCSI_RESET_TARGET:
2433 rtn = scsi_try_target_reset(scmd);
2434 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
2437 case SG_SCSI_RESET_BUS:
2438 rtn = scsi_try_bus_reset(scmd);
2439 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
2442 case SG_SCSI_RESET_HOST:
2443 rtn = scsi_try_host_reset(scmd);
2452 error = (rtn == SUCCESS) ? 0 : -EIO;
2454 spin_lock_irqsave(shost->host_lock, flags);
2455 shost->tmf_in_progress = 0;
2456 spin_unlock_irqrestore(shost->host_lock, flags);
2459 * be sure to wake up anyone who was sleeping or had their queue
2460 * suspended while we performed the TMF.
2462 SCSI_LOG_ERROR_RECOVERY(3,
2463 shost_printk(KERN_INFO, shost,
2464 "waking up host to restart after TMF\n"));
2466 wake_up(&shost->host_wait);
2467 scsi_run_host_queues(shost);
2471 out_put_autopm_host:
2472 scsi_autopm_put_host(shost);
2476 bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd,
2477 struct scsi_sense_hdr *sshdr)
2479 return scsi_normalize_sense(cmd->sense_buffer,
2480 SCSI_SENSE_BUFFERSIZE, sshdr);
2482 EXPORT_SYMBOL(scsi_command_normalize_sense);
2485 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
2486 * @sense_buffer: byte array of sense data
2487 * @sb_len: number of valid bytes in sense_buffer
2488 * @info_out: pointer to 64 integer where 8 or 4 byte information
2489 * field will be placed if found.
2492 * true if information field found, false if not found.
2494 bool scsi_get_sense_info_fld(const u8 *sense_buffer, int sb_len,
2501 switch (sense_buffer[0] & 0x7f) {
2504 if (sense_buffer[0] & 0x80) {
2505 *info_out = get_unaligned_be32(&sense_buffer[3]);
2511 ucp = scsi_sense_desc_find(sense_buffer, sb_len,
2513 if (ucp && (0xa == ucp[1])) {
2514 *info_out = get_unaligned_be64(&ucp[4]);
2522 EXPORT_SYMBOL(scsi_get_sense_info_fld);