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 if (host->eh_deadline != -1 && !host->last_reset)
338 host->last_reset = jiffies;
340 if (host->hostt->eh_timed_out)
341 rtn = host->hostt->eh_timed_out(scmd);
343 if (rtn == BLK_EH_DONE) {
345 * Set the command to complete first in order to prevent a real
346 * completion from releasing the command while error handling
347 * is using it. If the command was already completed, then the
348 * lower level driver beat the timeout handler, and it is safe
349 * to return without escalating error recovery.
351 * If timeout handling lost the race to a real completion, the
352 * block layer may ignore that due to a fake timeout injection,
353 * so return RESET_TIMER to allow error handling another shot
356 if (test_and_set_bit(SCMD_STATE_COMPLETE, &scmd->state))
357 return BLK_EH_RESET_TIMER;
358 if (scsi_abort_command(scmd) != SUCCESS) {
359 set_host_byte(scmd, DID_TIME_OUT);
360 scsi_eh_scmd_add(scmd);
368 * scsi_block_when_processing_errors - Prevent cmds from being queued.
369 * @sdev: Device on which we are performing recovery.
372 * We block until the host is out of error recovery, and then check to
373 * see whether the host or the device is offline.
376 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
378 int scsi_block_when_processing_errors(struct scsi_device *sdev)
382 wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
384 online = scsi_device_online(sdev);
388 EXPORT_SYMBOL(scsi_block_when_processing_errors);
390 #ifdef CONFIG_SCSI_LOGGING
392 * scsi_eh_prt_fail_stats - Log info on failures.
393 * @shost: scsi host being recovered.
394 * @work_q: Queue of scsi cmds to process.
396 static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
397 struct list_head *work_q)
399 struct scsi_cmnd *scmd;
400 struct scsi_device *sdev;
401 int total_failures = 0;
404 int devices_failed = 0;
406 shost_for_each_device(sdev, shost) {
407 list_for_each_entry(scmd, work_q, eh_entry) {
408 if (scmd->device == sdev) {
410 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED)
417 if (cmd_cancel || cmd_failed) {
418 SCSI_LOG_ERROR_RECOVERY(3,
419 shost_printk(KERN_INFO, shost,
420 "%s: cmds failed: %d, cancel: %d\n",
421 __func__, cmd_failed,
429 SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO, shost,
430 "Total of %d commands on %d"
431 " devices require eh work\n",
432 total_failures, devices_failed));
437 * scsi_report_lun_change - Set flag on all *other* devices on the same target
438 * to indicate that a UNIT ATTENTION is expected.
439 * @sdev: Device reporting the UNIT ATTENTION
441 static void scsi_report_lun_change(struct scsi_device *sdev)
443 sdev->sdev_target->expecting_lun_change = 1;
447 * scsi_report_sense - Examine scsi sense information and log messages for
448 * certain conditions, also issue uevents for some of them.
449 * @sdev: Device reporting the sense code
450 * @sshdr: sshdr to be examined
452 static void scsi_report_sense(struct scsi_device *sdev,
453 struct scsi_sense_hdr *sshdr)
455 enum scsi_device_event evt_type = SDEV_EVT_MAXBITS; /* i.e. none */
457 if (sshdr->sense_key == UNIT_ATTENTION) {
458 if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) {
459 evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED;
460 sdev_printk(KERN_WARNING, sdev,
461 "Inquiry data has changed");
462 } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) {
463 evt_type = SDEV_EVT_LUN_CHANGE_REPORTED;
464 scsi_report_lun_change(sdev);
465 sdev_printk(KERN_WARNING, sdev,
466 "LUN assignments on this target have "
467 "changed. The Linux SCSI layer does not "
468 "automatically remap LUN assignments.\n");
469 } else if (sshdr->asc == 0x3f)
470 sdev_printk(KERN_WARNING, sdev,
471 "Operating parameters on this target have "
472 "changed. The Linux SCSI layer does not "
473 "automatically adjust these parameters.\n");
475 if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) {
476 evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED;
477 sdev_printk(KERN_WARNING, sdev,
478 "Warning! Received an indication that the "
479 "LUN reached a thin provisioning soft "
483 if (sshdr->asc == 0x29) {
484 evt_type = SDEV_EVT_POWER_ON_RESET_OCCURRED;
486 * Do not print message if it is an expected side-effect
489 if (!sdev->silence_suspend)
490 sdev_printk(KERN_WARNING, sdev,
491 "Power-on or device reset occurred\n");
494 if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) {
495 evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED;
496 sdev_printk(KERN_WARNING, sdev,
497 "Mode parameters changed");
498 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x06) {
499 evt_type = SDEV_EVT_ALUA_STATE_CHANGE_REPORTED;
500 sdev_printk(KERN_WARNING, sdev,
501 "Asymmetric access state changed");
502 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) {
503 evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED;
504 sdev_printk(KERN_WARNING, sdev,
505 "Capacity data has changed");
506 } else if (sshdr->asc == 0x2a)
507 sdev_printk(KERN_WARNING, sdev,
508 "Parameters changed");
511 if (evt_type != SDEV_EVT_MAXBITS) {
512 set_bit(evt_type, sdev->pending_events);
513 schedule_work(&sdev->event_work);
518 * scsi_check_sense - Examine scsi cmd sense
519 * @scmd: Cmd to have sense checked.
522 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
525 * When a deferred error is detected the current command has
526 * not been executed and needs retrying.
528 enum scsi_disposition scsi_check_sense(struct scsi_cmnd *scmd)
530 struct scsi_device *sdev = scmd->device;
531 struct scsi_sense_hdr sshdr;
533 if (! scsi_command_normalize_sense(scmd, &sshdr))
534 return FAILED; /* no valid sense data */
536 scsi_report_sense(sdev, &sshdr);
538 if (scsi_sense_is_deferred(&sshdr))
541 if (sdev->handler && sdev->handler->check_sense) {
542 enum scsi_disposition rc;
544 rc = sdev->handler->check_sense(sdev, &sshdr);
545 if (rc != SCSI_RETURN_NOT_HANDLED)
547 /* handler does not care. Drop down to default handling */
550 if (scmd->cmnd[0] == TEST_UNIT_READY &&
551 scmd->submitter != SUBMITTED_BY_SCSI_ERROR_HANDLER)
553 * nasty: for mid-layer issued TURs, we need to return the
554 * actual sense data without any recovery attempt. For eh
555 * issued ones, we need to try to recover and interpret
560 * Previous logic looked for FILEMARK, EOM or ILI which are
561 * mainly associated with tapes and returned SUCCESS.
563 if (sshdr.response_code == 0x70) {
565 if (scmd->sense_buffer[2] & 0xe0)
569 * descriptor format: look for "stream commands sense data
570 * descriptor" (see SSC-3). Assume single sense data
571 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
573 if ((sshdr.additional_length > 3) &&
574 (scmd->sense_buffer[8] == 0x4) &&
575 (scmd->sense_buffer[11] & 0xe0))
579 switch (sshdr.sense_key) {
582 case RECOVERED_ERROR:
583 return /* soft_error */ SUCCESS;
585 case ABORTED_COMMAND:
586 if (sshdr.asc == 0x10) /* DIF */
589 if (sshdr.asc == 0x44 && sdev->sdev_bflags & BLIST_RETRY_ITF)
590 return ADD_TO_MLQUEUE;
591 if (sshdr.asc == 0xc1 && sshdr.ascq == 0x01 &&
592 sdev->sdev_bflags & BLIST_RETRY_ASC_C1)
593 return ADD_TO_MLQUEUE;
599 * if we are expecting a cc/ua because of a bus reset that we
600 * performed, treat this just as a retry. otherwise this is
601 * information that we should pass up to the upper-level driver
602 * so that we can deal with it there.
604 if (scmd->device->expecting_cc_ua) {
606 * Because some device does not queue unit
607 * attentions correctly, we carefully check
608 * additional sense code and qualifier so as
609 * not to squash media change unit attention.
611 if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) {
612 scmd->device->expecting_cc_ua = 0;
617 * we might also expect a cc/ua if another LUN on the target
618 * reported a UA with an ASC/ASCQ of 3F 0E -
619 * REPORTED LUNS DATA HAS CHANGED.
621 if (scmd->device->sdev_target->expecting_lun_change &&
622 sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
625 * if the device is in the process of becoming ready, we
628 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
631 * if the device is not started, we need to wake
632 * the error handler to start the motor
634 if (scmd->device->allow_restart &&
635 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
638 * Pass the UA upwards for a determination in the completion
643 /* these are not supported */
645 if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
646 /* Thin provisioning hard threshold reached */
647 set_host_byte(scmd, DID_ALLOC_FAILURE);
652 case VOLUME_OVERFLOW:
655 set_host_byte(scmd, DID_TARGET_FAILURE);
659 if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
660 sshdr.asc == 0x13 || /* AMNF DATA FIELD */
661 sshdr.asc == 0x14) { /* RECORD NOT FOUND */
662 set_host_byte(scmd, DID_MEDIUM_ERROR);
668 if (scmd->device->retry_hwerror)
669 return ADD_TO_MLQUEUE;
671 set_host_byte(scmd, DID_TARGET_FAILURE);
674 case ILLEGAL_REQUEST:
675 if (sshdr.asc == 0x20 || /* Invalid command operation code */
676 sshdr.asc == 0x21 || /* Logical block address out of range */
677 sshdr.asc == 0x22 || /* Invalid function */
678 sshdr.asc == 0x24 || /* Invalid field in cdb */
679 sshdr.asc == 0x26 || /* Parameter value invalid */
680 sshdr.asc == 0x27) { /* Write protected */
681 set_host_byte(scmd, DID_TARGET_FAILURE);
689 EXPORT_SYMBOL_GPL(scsi_check_sense);
691 static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
693 struct scsi_host_template *sht = sdev->host->hostt;
694 struct scsi_device *tmp_sdev;
696 if (!sht->track_queue_depth ||
697 sdev->queue_depth >= sdev->max_queue_depth)
700 if (time_before(jiffies,
701 sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
704 if (time_before(jiffies,
705 sdev->last_queue_full_time + sdev->queue_ramp_up_period))
709 * Walk all devices of a target and do
712 shost_for_each_device(tmp_sdev, sdev->host) {
713 if (tmp_sdev->channel != sdev->channel ||
714 tmp_sdev->id != sdev->id ||
715 tmp_sdev->queue_depth == sdev->max_queue_depth)
718 scsi_change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1);
719 sdev->last_queue_ramp_up = jiffies;
723 static void scsi_handle_queue_full(struct scsi_device *sdev)
725 struct scsi_host_template *sht = sdev->host->hostt;
726 struct scsi_device *tmp_sdev;
728 if (!sht->track_queue_depth)
731 shost_for_each_device(tmp_sdev, sdev->host) {
732 if (tmp_sdev->channel != sdev->channel ||
733 tmp_sdev->id != sdev->id)
736 * We do not know the number of commands that were at
737 * the device when we got the queue full so we start
738 * from the highest possible value and work our way down.
740 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
745 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
746 * @scmd: SCSI cmd to examine.
749 * This is *only* called when we are examining the status of commands
750 * queued during error recovery. the main difference here is that we
751 * don't allow for the possibility of retries here, and we are a lot
752 * more restrictive about what we consider acceptable.
754 static enum scsi_disposition scsi_eh_completed_normally(struct scsi_cmnd *scmd)
757 * first check the host byte, to see if there is anything in there
758 * that would indicate what we need to do.
760 if (host_byte(scmd->result) == DID_RESET) {
762 * rats. we are already in the error handler, so we now
763 * get to try and figure out what to do next. if the sense
764 * is valid, we have a pretty good idea of what to do.
765 * if not, we mark it as FAILED.
767 return scsi_check_sense(scmd);
769 if (host_byte(scmd->result) != DID_OK)
773 * now, check the status byte to see if this indicates
776 switch (get_status_byte(scmd)) {
778 scsi_handle_queue_ramp_up(scmd->device);
780 case SAM_STAT_COMMAND_TERMINATED:
782 case SAM_STAT_CHECK_CONDITION:
783 return scsi_check_sense(scmd);
784 case SAM_STAT_CONDITION_MET:
785 case SAM_STAT_INTERMEDIATE:
786 case SAM_STAT_INTERMEDIATE_CONDITION_MET:
788 * who knows? FIXME(eric)
791 case SAM_STAT_RESERVATION_CONFLICT:
792 if (scmd->cmnd[0] == TEST_UNIT_READY)
793 /* it is a success, we probed the device and
796 /* otherwise, we failed to send the command */
798 case SAM_STAT_TASK_SET_FULL:
799 scsi_handle_queue_full(scmd->device);
810 * scsi_eh_done - Completion function for error handling.
811 * @scmd: Cmd that is done.
813 void scsi_eh_done(struct scsi_cmnd *scmd)
815 struct completion *eh_action;
817 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
818 "%s result: %x\n", __func__, scmd->result));
820 eh_action = scmd->device->host->eh_action;
826 * scsi_try_host_reset - ask host adapter to reset itself
827 * @scmd: SCSI cmd to send host reset.
829 static enum scsi_disposition scsi_try_host_reset(struct scsi_cmnd *scmd)
832 enum scsi_disposition rtn;
833 struct Scsi_Host *host = scmd->device->host;
834 struct scsi_host_template *hostt = host->hostt;
836 SCSI_LOG_ERROR_RECOVERY(3,
837 shost_printk(KERN_INFO, host, "Snd Host RST\n"));
839 if (!hostt->eh_host_reset_handler)
842 rtn = hostt->eh_host_reset_handler(scmd);
844 if (rtn == SUCCESS) {
845 if (!hostt->skip_settle_delay)
846 ssleep(HOST_RESET_SETTLE_TIME);
847 spin_lock_irqsave(host->host_lock, flags);
848 scsi_report_bus_reset(host, scmd_channel(scmd));
849 spin_unlock_irqrestore(host->host_lock, flags);
856 * scsi_try_bus_reset - ask host to perform a bus reset
857 * @scmd: SCSI cmd to send bus reset.
859 static enum scsi_disposition scsi_try_bus_reset(struct scsi_cmnd *scmd)
862 enum scsi_disposition rtn;
863 struct Scsi_Host *host = scmd->device->host;
864 struct scsi_host_template *hostt = host->hostt;
866 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
867 "%s: Snd Bus RST\n", __func__));
869 if (!hostt->eh_bus_reset_handler)
872 rtn = hostt->eh_bus_reset_handler(scmd);
874 if (rtn == SUCCESS) {
875 if (!hostt->skip_settle_delay)
876 ssleep(BUS_RESET_SETTLE_TIME);
877 spin_lock_irqsave(host->host_lock, flags);
878 scsi_report_bus_reset(host, scmd_channel(scmd));
879 spin_unlock_irqrestore(host->host_lock, flags);
885 static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
888 sdev->expecting_cc_ua = 1;
892 * scsi_try_target_reset - Ask host to perform a target reset
893 * @scmd: SCSI cmd used to send a target reset
896 * There is no timeout for this operation. if this operation is
897 * unreliable for a given host, then the host itself needs to put a
898 * timer on it, and set the host back to a consistent state prior to
901 static enum scsi_disposition scsi_try_target_reset(struct scsi_cmnd *scmd)
904 enum scsi_disposition rtn;
905 struct Scsi_Host *host = scmd->device->host;
906 struct scsi_host_template *hostt = host->hostt;
908 if (!hostt->eh_target_reset_handler)
911 rtn = hostt->eh_target_reset_handler(scmd);
912 if (rtn == SUCCESS) {
913 spin_lock_irqsave(host->host_lock, flags);
914 __starget_for_each_device(scsi_target(scmd->device), NULL,
915 __scsi_report_device_reset);
916 spin_unlock_irqrestore(host->host_lock, flags);
923 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
924 * @scmd: SCSI cmd used to send BDR
927 * There is no timeout for this operation. if this operation is
928 * unreliable for a given host, then the host itself needs to put a
929 * timer on it, and set the host back to a consistent state prior to
932 static enum scsi_disposition scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
934 enum scsi_disposition rtn;
935 struct scsi_host_template *hostt = scmd->device->host->hostt;
937 if (!hostt->eh_device_reset_handler)
940 rtn = hostt->eh_device_reset_handler(scmd);
942 __scsi_report_device_reset(scmd->device, NULL);
947 * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
948 * @hostt: SCSI driver host template
949 * @scmd: SCSI cmd used to send a target reset
952 * SUCCESS, FAILED, or FAST_IO_FAIL
955 * SUCCESS does not necessarily indicate that the command
956 * has been aborted; it only indicates that the LLDDs
957 * has cleared all references to that command.
958 * LLDDs should return FAILED only if an abort was required
959 * but could not be executed. LLDDs should return FAST_IO_FAIL
960 * if the device is temporarily unavailable (eg due to a
961 * link down on FibreChannel)
963 static enum scsi_disposition
964 scsi_try_to_abort_cmd(struct scsi_host_template *hostt, struct scsi_cmnd *scmd)
966 if (!hostt->eh_abort_handler)
969 return hostt->eh_abort_handler(scmd);
972 static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
974 if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
975 if (scsi_try_bus_device_reset(scmd) != SUCCESS)
976 if (scsi_try_target_reset(scmd) != SUCCESS)
977 if (scsi_try_bus_reset(scmd) != SUCCESS)
978 scsi_try_host_reset(scmd);
982 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
983 * @scmd: SCSI command structure to hijack
984 * @ses: structure to save restore information
985 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
986 * @cmnd_size: size in bytes of @cmnd (must be <= MAX_COMMAND_SIZE)
987 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
989 * This function is used to save a scsi command information before re-execution
990 * as part of the error recovery process. If @sense_bytes is 0 the command
991 * sent must be one that does not transfer any data. If @sense_bytes != 0
992 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
993 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
995 void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
996 unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
998 struct scsi_device *sdev = scmd->device;
1001 * We need saved copies of a number of fields - this is because
1002 * error handling may need to overwrite these with different values
1003 * to run different commands, and once error handling is complete,
1004 * we will need to restore these values prior to running the actual
1007 ses->cmd_len = scmd->cmd_len;
1008 ses->data_direction = scmd->sc_data_direction;
1009 ses->sdb = scmd->sdb;
1010 ses->result = scmd->result;
1011 ses->resid_len = scmd->resid_len;
1012 ses->underflow = scmd->underflow;
1013 ses->prot_op = scmd->prot_op;
1014 ses->eh_eflags = scmd->eh_eflags;
1016 scmd->prot_op = SCSI_PROT_NORMAL;
1017 scmd->eh_eflags = 0;
1018 memcpy(ses->cmnd, scmd->cmnd, sizeof(ses->cmnd));
1019 memset(scmd->cmnd, 0, sizeof(scmd->cmnd));
1020 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
1022 scmd->resid_len = 0;
1025 scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
1027 sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
1029 scmd->sdb.table.sgl = &ses->sense_sgl;
1030 scmd->sc_data_direction = DMA_FROM_DEVICE;
1031 scmd->sdb.table.nents = scmd->sdb.table.orig_nents = 1;
1032 scmd->cmnd[0] = REQUEST_SENSE;
1033 scmd->cmnd[4] = scmd->sdb.length;
1034 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
1036 scmd->sc_data_direction = DMA_NONE;
1038 BUG_ON(cmnd_size > sizeof(scmd->cmnd));
1039 memcpy(scmd->cmnd, cmnd, cmnd_size);
1040 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
1044 scmd->underflow = 0;
1046 if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
1047 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
1048 (sdev->lun << 5 & 0xe0);
1051 * Zero the sense buffer. The scsi spec mandates that any
1052 * untransferred sense data should be interpreted as being zero.
1054 memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1056 EXPORT_SYMBOL(scsi_eh_prep_cmnd);
1059 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
1060 * @scmd: SCSI command structure to restore
1061 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
1063 * Undo any damage done by above scsi_eh_prep_cmnd().
1065 void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
1068 * Restore original data
1070 scmd->cmd_len = ses->cmd_len;
1071 memcpy(scmd->cmnd, ses->cmnd, sizeof(ses->cmnd));
1072 scmd->sc_data_direction = ses->data_direction;
1073 scmd->sdb = ses->sdb;
1074 scmd->result = ses->result;
1075 scmd->resid_len = ses->resid_len;
1076 scmd->underflow = ses->underflow;
1077 scmd->prot_op = ses->prot_op;
1078 scmd->eh_eflags = ses->eh_eflags;
1080 EXPORT_SYMBOL(scsi_eh_restore_cmnd);
1083 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
1084 * @scmd: SCSI command structure to hijack
1085 * @cmnd: CDB to send
1086 * @cmnd_size: size in bytes of @cmnd
1087 * @timeout: timeout for this request
1088 * @sense_bytes: size of sense data to copy or 0
1090 * This function is used to send a scsi command down to a target device
1091 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
1094 * SUCCESS or FAILED or NEEDS_RETRY
1096 static enum scsi_disposition scsi_send_eh_cmnd(struct scsi_cmnd *scmd,
1097 unsigned char *cmnd, int cmnd_size, int timeout, unsigned sense_bytes)
1099 struct scsi_device *sdev = scmd->device;
1100 struct Scsi_Host *shost = sdev->host;
1101 DECLARE_COMPLETION_ONSTACK(done);
1102 unsigned long timeleft = timeout, delay;
1103 struct scsi_eh_save ses;
1104 const unsigned long stall_for = msecs_to_jiffies(100);
1108 scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
1109 shost->eh_action = &done;
1111 scsi_log_send(scmd);
1112 scmd->submitter = SUBMITTED_BY_SCSI_ERROR_HANDLER;
1115 * Lock sdev->state_mutex to avoid that scsi_device_quiesce() can
1116 * change the SCSI device state after we have examined it and before
1117 * .queuecommand() is called.
1119 mutex_lock(&sdev->state_mutex);
1120 while (sdev->sdev_state == SDEV_BLOCK && timeleft > 0) {
1121 mutex_unlock(&sdev->state_mutex);
1122 SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_DEBUG, sdev,
1123 "%s: state %d <> %d\n", __func__, sdev->sdev_state,
1125 delay = min(timeleft, stall_for);
1127 msleep(jiffies_to_msecs(delay));
1128 mutex_lock(&sdev->state_mutex);
1130 if (sdev->sdev_state != SDEV_BLOCK)
1131 rtn = shost->hostt->queuecommand(shost, scmd);
1134 mutex_unlock(&sdev->state_mutex);
1137 if (timeleft > stall_for) {
1138 scsi_eh_restore_cmnd(scmd, &ses);
1140 timeleft -= stall_for;
1141 msleep(jiffies_to_msecs(stall_for));
1144 /* signal not to enter either branch of the if () below */
1148 timeleft = wait_for_completion_timeout(&done, timeout);
1152 shost->eh_action = NULL;
1154 scsi_log_completion(scmd, rtn);
1156 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1157 "%s timeleft: %ld\n",
1158 __func__, timeleft));
1161 * If there is time left scsi_eh_done got called, and we will examine
1162 * the actual status codes to see whether the command actually did
1163 * complete normally, else if we have a zero return and no time left,
1164 * the command must still be pending, so abort it and return FAILED.
1165 * If we never actually managed to issue the command, because
1166 * ->queuecommand() kept returning non zero, use the rtn = FAILED
1167 * value above (so don't execute either branch of the if)
1170 rtn = scsi_eh_completed_normally(scmd);
1171 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1172 "%s: scsi_eh_completed_normally %x\n", __func__, rtn));
1179 case ADD_TO_MLQUEUE:
1186 } else if (rtn != FAILED) {
1187 scsi_abort_eh_cmnd(scmd);
1191 scsi_eh_restore_cmnd(scmd, &ses);
1197 * scsi_request_sense - Request sense data from a particular target.
1198 * @scmd: SCSI cmd for request sense.
1201 * Some hosts automatically obtain this information, others require
1202 * that we obtain it on our own. This function will *not* return until
1203 * the command either times out, or it completes.
1205 static enum scsi_disposition scsi_request_sense(struct scsi_cmnd *scmd)
1207 return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
1210 static enum scsi_disposition
1211 scsi_eh_action(struct scsi_cmnd *scmd, enum scsi_disposition rtn)
1213 if (!blk_rq_is_passthrough(scsi_cmd_to_rq(scmd))) {
1214 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
1215 if (sdrv->eh_action)
1216 rtn = sdrv->eh_action(scmd, rtn);
1222 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
1223 * @scmd: Original SCSI cmd that eh has finished.
1224 * @done_q: Queue for processed commands.
1227 * We don't want to use the normal command completion while we are are
1228 * still handling errors - it may cause other commands to be queued,
1229 * and that would disturb what we are doing. Thus we really want to
1230 * keep a list of pending commands for final completion, and once we
1231 * are ready to leave error handling we handle completion for real.
1233 void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
1235 list_move_tail(&scmd->eh_entry, done_q);
1237 EXPORT_SYMBOL(scsi_eh_finish_cmd);
1240 * scsi_eh_get_sense - Get device sense data.
1241 * @work_q: Queue of commands to process.
1242 * @done_q: Queue of processed commands.
1245 * See if we need to request sense information. if so, then get it
1246 * now, so we have a better idea of what to do.
1249 * This has the unfortunate side effect that if a shost adapter does
1250 * not automatically request sense information, we end up shutting
1251 * it down before we request it.
1253 * All drivers should request sense information internally these days,
1254 * so for now all I have to say is tough noogies if you end up in here.
1256 * XXX: Long term this code should go away, but that needs an audit of
1259 int scsi_eh_get_sense(struct list_head *work_q,
1260 struct list_head *done_q)
1262 struct scsi_cmnd *scmd, *next;
1263 struct Scsi_Host *shost;
1264 enum scsi_disposition rtn;
1267 * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
1268 * should not get sense.
1270 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1271 if ((scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) ||
1272 SCSI_SENSE_VALID(scmd))
1275 shost = scmd->device->host;
1276 if (scsi_host_eh_past_deadline(shost)) {
1277 SCSI_LOG_ERROR_RECOVERY(3,
1278 scmd_printk(KERN_INFO, scmd,
1279 "%s: skip request sense, past eh deadline\n",
1283 if (!scsi_status_is_check_condition(scmd->result))
1285 * don't request sense if there's no check condition
1286 * status because the error we're processing isn't one
1287 * that has a sense code (and some devices get
1288 * confused by sense requests out of the blue)
1292 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
1293 "%s: requesting sense\n",
1295 rtn = scsi_request_sense(scmd);
1299 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1300 "sense requested, result %x\n", scmd->result));
1301 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd));
1303 rtn = scsi_decide_disposition(scmd);
1306 * if the result was normal, then just pass it along to the
1311 * We don't want this command reissued, just finished
1312 * with the sense data, so set retries to the max
1313 * allowed to ensure it won't get reissued. If the user
1314 * has requested infinite retries, we also want to
1315 * finish this command, so force completion by setting
1316 * retries and allowed to the same value.
1318 if (scmd->allowed == SCSI_CMD_RETRIES_NO_LIMIT)
1319 scmd->retries = scmd->allowed = 1;
1321 scmd->retries = scmd->allowed;
1322 else if (rtn != NEEDS_RETRY)
1325 scsi_eh_finish_cmd(scmd, done_q);
1328 return list_empty(work_q);
1330 EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
1333 * scsi_eh_tur - Send TUR to device.
1334 * @scmd: &scsi_cmnd to send TUR
1337 * 0 - Device is ready. 1 - Device NOT ready.
1339 static int scsi_eh_tur(struct scsi_cmnd *scmd)
1341 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
1343 enum scsi_disposition rtn;
1346 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
1347 scmd->device->eh_timeout, 0);
1349 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1350 "%s return: %x\n", __func__, rtn));
1365 * scsi_eh_test_devices - check if devices are responding from error recovery.
1366 * @cmd_list: scsi commands in error recovery.
1367 * @work_q: queue for commands which still need more error recovery
1368 * @done_q: queue for commands which are finished
1369 * @try_stu: boolean on if a STU command should be tried in addition to TUR.
1372 * Tests if devices are in a working state. Commands to devices now in
1373 * a working state are sent to the done_q while commands to devices which
1374 * are still failing to respond are returned to the work_q for more
1377 static int scsi_eh_test_devices(struct list_head *cmd_list,
1378 struct list_head *work_q,
1379 struct list_head *done_q, int try_stu)
1381 struct scsi_cmnd *scmd, *next;
1382 struct scsi_device *sdev;
1385 while (!list_empty(cmd_list)) {
1386 scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
1387 sdev = scmd->device;
1390 if (scsi_host_eh_past_deadline(sdev->host)) {
1391 /* Push items back onto work_q */
1392 list_splice_init(cmd_list, work_q);
1393 SCSI_LOG_ERROR_RECOVERY(3,
1394 sdev_printk(KERN_INFO, sdev,
1395 "%s: skip test device, past eh deadline",
1401 finish_cmds = !scsi_device_online(scmd->device) ||
1402 (try_stu && !scsi_eh_try_stu(scmd) &&
1403 !scsi_eh_tur(scmd)) ||
1406 list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
1407 if (scmd->device == sdev) {
1410 scsi_eh_action(scmd, SUCCESS) == SUCCESS))
1411 scsi_eh_finish_cmd(scmd, done_q);
1413 list_move_tail(&scmd->eh_entry, work_q);
1416 return list_empty(work_q);
1420 * scsi_eh_try_stu - Send START_UNIT to device.
1421 * @scmd: &scsi_cmnd to send START_UNIT
1424 * 0 - Device is ready. 1 - Device NOT ready.
1426 static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
1428 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
1430 if (scmd->device->allow_restart) {
1432 enum scsi_disposition rtn = NEEDS_RETRY;
1434 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
1435 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6,
1436 scmd->device->eh_timeout, 0);
1446 * scsi_eh_stu - send START_UNIT if needed
1447 * @shost: &scsi host being recovered.
1448 * @work_q: &list_head for pending commands.
1449 * @done_q: &list_head for processed commands.
1452 * If commands are failing due to not ready, initializing command required,
1453 * try revalidating the device, which will end up sending a start unit.
1455 static int scsi_eh_stu(struct Scsi_Host *shost,
1456 struct list_head *work_q,
1457 struct list_head *done_q)
1459 struct scsi_cmnd *scmd, *stu_scmd, *next;
1460 struct scsi_device *sdev;
1462 shost_for_each_device(sdev, shost) {
1463 if (scsi_host_eh_past_deadline(shost)) {
1464 SCSI_LOG_ERROR_RECOVERY(3,
1465 sdev_printk(KERN_INFO, sdev,
1466 "%s: skip START_UNIT, past eh deadline\n",
1468 scsi_device_put(sdev);
1472 list_for_each_entry(scmd, work_q, eh_entry)
1473 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
1474 scsi_check_sense(scmd) == FAILED ) {
1482 SCSI_LOG_ERROR_RECOVERY(3,
1483 sdev_printk(KERN_INFO, sdev,
1484 "%s: Sending START_UNIT\n",
1487 if (!scsi_eh_try_stu(stu_scmd)) {
1488 if (!scsi_device_online(sdev) ||
1489 !scsi_eh_tur(stu_scmd)) {
1490 list_for_each_entry_safe(scmd, next,
1492 if (scmd->device == sdev &&
1493 scsi_eh_action(scmd, SUCCESS) == SUCCESS)
1494 scsi_eh_finish_cmd(scmd, done_q);
1498 SCSI_LOG_ERROR_RECOVERY(3,
1499 sdev_printk(KERN_INFO, sdev,
1500 "%s: START_UNIT failed\n",
1505 return list_empty(work_q);
1510 * scsi_eh_bus_device_reset - send bdr if needed
1511 * @shost: scsi host being recovered.
1512 * @work_q: &list_head for pending commands.
1513 * @done_q: &list_head for processed commands.
1516 * Try a bus device reset. Still, look to see whether we have multiple
1517 * devices that are jammed or not - if we have multiple devices, it
1518 * makes no sense to try bus_device_reset - we really would need to try
1519 * a bus_reset instead.
1521 static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1522 struct list_head *work_q,
1523 struct list_head *done_q)
1525 struct scsi_cmnd *scmd, *bdr_scmd, *next;
1526 struct scsi_device *sdev;
1527 enum scsi_disposition rtn;
1529 shost_for_each_device(sdev, shost) {
1530 if (scsi_host_eh_past_deadline(shost)) {
1531 SCSI_LOG_ERROR_RECOVERY(3,
1532 sdev_printk(KERN_INFO, sdev,
1533 "%s: skip BDR, past eh deadline\n",
1535 scsi_device_put(sdev);
1539 list_for_each_entry(scmd, work_q, eh_entry)
1540 if (scmd->device == sdev) {
1548 SCSI_LOG_ERROR_RECOVERY(3,
1549 sdev_printk(KERN_INFO, sdev,
1550 "%s: Sending BDR\n", current->comm));
1551 rtn = scsi_try_bus_device_reset(bdr_scmd);
1552 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
1553 if (!scsi_device_online(sdev) ||
1554 rtn == FAST_IO_FAIL ||
1555 !scsi_eh_tur(bdr_scmd)) {
1556 list_for_each_entry_safe(scmd, next,
1558 if (scmd->device == sdev &&
1559 scsi_eh_action(scmd, rtn) != FAILED)
1560 scsi_eh_finish_cmd(scmd,
1565 SCSI_LOG_ERROR_RECOVERY(3,
1566 sdev_printk(KERN_INFO, sdev,
1567 "%s: BDR failed\n", current->comm));
1571 return list_empty(work_q);
1575 * scsi_eh_target_reset - send target reset if needed
1576 * @shost: scsi host being recovered.
1577 * @work_q: &list_head for pending commands.
1578 * @done_q: &list_head for processed commands.
1581 * Try a target reset.
1583 static int scsi_eh_target_reset(struct Scsi_Host *shost,
1584 struct list_head *work_q,
1585 struct list_head *done_q)
1587 LIST_HEAD(tmp_list);
1588 LIST_HEAD(check_list);
1590 list_splice_init(work_q, &tmp_list);
1592 while (!list_empty(&tmp_list)) {
1593 struct scsi_cmnd *next, *scmd;
1594 enum scsi_disposition rtn;
1597 if (scsi_host_eh_past_deadline(shost)) {
1598 /* push back on work queue for further processing */
1599 list_splice_init(&check_list, work_q);
1600 list_splice_init(&tmp_list, work_q);
1601 SCSI_LOG_ERROR_RECOVERY(3,
1602 shost_printk(KERN_INFO, shost,
1603 "%s: Skip target reset, past eh deadline\n",
1605 return list_empty(work_q);
1608 scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
1611 SCSI_LOG_ERROR_RECOVERY(3,
1612 shost_printk(KERN_INFO, shost,
1613 "%s: Sending target reset to target %d\n",
1614 current->comm, id));
1615 rtn = scsi_try_target_reset(scmd);
1616 if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
1617 SCSI_LOG_ERROR_RECOVERY(3,
1618 shost_printk(KERN_INFO, shost,
1619 "%s: Target reset failed"
1621 current->comm, id));
1622 list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
1623 if (scmd_id(scmd) != id)
1627 list_move_tail(&scmd->eh_entry, &check_list);
1628 else if (rtn == FAST_IO_FAIL)
1629 scsi_eh_finish_cmd(scmd, done_q);
1631 /* push back on work queue for further processing */
1632 list_move(&scmd->eh_entry, work_q);
1636 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1640 * scsi_eh_bus_reset - send a bus reset
1641 * @shost: &scsi host being recovered.
1642 * @work_q: &list_head for pending commands.
1643 * @done_q: &list_head for processed commands.
1645 static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1646 struct list_head *work_q,
1647 struct list_head *done_q)
1649 struct scsi_cmnd *scmd, *chan_scmd, *next;
1650 LIST_HEAD(check_list);
1651 unsigned int channel;
1652 enum scsi_disposition rtn;
1655 * we really want to loop over the various channels, and do this on
1656 * a channel by channel basis. we should also check to see if any
1657 * of the failed commands are on soft_reset devices, and if so, skip
1661 for (channel = 0; channel <= shost->max_channel; channel++) {
1662 if (scsi_host_eh_past_deadline(shost)) {
1663 list_splice_init(&check_list, work_q);
1664 SCSI_LOG_ERROR_RECOVERY(3,
1665 shost_printk(KERN_INFO, shost,
1666 "%s: skip BRST, past eh deadline\n",
1668 return list_empty(work_q);
1672 list_for_each_entry(scmd, work_q, eh_entry) {
1673 if (channel == scmd_channel(scmd)) {
1677 * FIXME add back in some support for
1678 * soft_reset devices.
1685 SCSI_LOG_ERROR_RECOVERY(3,
1686 shost_printk(KERN_INFO, shost,
1687 "%s: Sending BRST chan: %d\n",
1688 current->comm, channel));
1689 rtn = scsi_try_bus_reset(chan_scmd);
1690 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
1691 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1692 if (channel == scmd_channel(scmd)) {
1693 if (rtn == FAST_IO_FAIL)
1694 scsi_eh_finish_cmd(scmd,
1697 list_move_tail(&scmd->eh_entry,
1702 SCSI_LOG_ERROR_RECOVERY(3,
1703 shost_printk(KERN_INFO, shost,
1704 "%s: BRST failed chan: %d\n",
1705 current->comm, channel));
1708 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1712 * scsi_eh_host_reset - send a host reset
1713 * @shost: host to be reset.
1714 * @work_q: &list_head for pending commands.
1715 * @done_q: &list_head for processed commands.
1717 static int scsi_eh_host_reset(struct Scsi_Host *shost,
1718 struct list_head *work_q,
1719 struct list_head *done_q)
1721 struct scsi_cmnd *scmd, *next;
1722 LIST_HEAD(check_list);
1723 enum scsi_disposition rtn;
1725 if (!list_empty(work_q)) {
1726 scmd = list_entry(work_q->next,
1727 struct scsi_cmnd, eh_entry);
1729 SCSI_LOG_ERROR_RECOVERY(3,
1730 shost_printk(KERN_INFO, shost,
1731 "%s: Sending HRST\n",
1734 rtn = scsi_try_host_reset(scmd);
1735 if (rtn == SUCCESS) {
1736 list_splice_init(work_q, &check_list);
1737 } else if (rtn == FAST_IO_FAIL) {
1738 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1739 scsi_eh_finish_cmd(scmd, done_q);
1742 SCSI_LOG_ERROR_RECOVERY(3,
1743 shost_printk(KERN_INFO, shost,
1744 "%s: HRST failed\n",
1748 return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
1752 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
1753 * @work_q: &list_head for pending commands.
1754 * @done_q: &list_head for processed commands.
1756 static void scsi_eh_offline_sdevs(struct list_head *work_q,
1757 struct list_head *done_q)
1759 struct scsi_cmnd *scmd, *next;
1760 struct scsi_device *sdev;
1762 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1763 sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
1764 "not ready after error recovery\n");
1765 sdev = scmd->device;
1767 mutex_lock(&sdev->state_mutex);
1768 scsi_device_set_state(sdev, SDEV_OFFLINE);
1769 mutex_unlock(&sdev->state_mutex);
1771 scsi_eh_finish_cmd(scmd, done_q);
1777 * scsi_noretry_cmd - determine if command should be failed fast
1778 * @scmd: SCSI cmd to examine.
1780 bool scsi_noretry_cmd(struct scsi_cmnd *scmd)
1782 struct request *req = scsi_cmd_to_rq(scmd);
1784 switch (host_byte(scmd->result)) {
1790 return !!(req->cmd_flags & REQ_FAILFAST_TRANSPORT);
1792 return !!(req->cmd_flags & REQ_FAILFAST_DEV);
1794 if (get_status_byte(scmd) == SAM_STAT_RESERVATION_CONFLICT)
1797 case DID_SOFT_ERROR:
1798 return !!(req->cmd_flags & REQ_FAILFAST_DRIVER);
1801 if (!scsi_status_is_check_condition(scmd->result))
1806 * assume caller has checked sense and determined
1807 * the check condition was retryable.
1809 if (req->cmd_flags & REQ_FAILFAST_DEV || blk_rq_is_passthrough(req))
1816 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1817 * @scmd: SCSI cmd to examine.
1820 * This is *only* called when we are examining the status after sending
1821 * out the actual data command. any commands that are queued for error
1822 * recovery (e.g. test_unit_ready) do *not* come through here.
1824 * When this routine returns failed, it means the error handler thread
1825 * is woken. In cases where the error code indicates an error that
1826 * doesn't require the error handler read (i.e. we don't need to
1827 * abort/reset), this function should return SUCCESS.
1829 enum scsi_disposition scsi_decide_disposition(struct scsi_cmnd *scmd)
1831 enum scsi_disposition rtn;
1834 * if the device is offline, then we clearly just pass the result back
1835 * up to the top level.
1837 if (!scsi_device_online(scmd->device)) {
1838 SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO, scmd,
1839 "%s: device offline - report as SUCCESS\n", __func__));
1844 * first check the host byte, to see if there is anything in there
1845 * that would indicate what we need to do.
1847 switch (host_byte(scmd->result)) {
1848 case DID_PASSTHROUGH:
1850 * no matter what, pass this through to the upper layer.
1851 * nuke this special code so that it looks like we are saying
1854 scmd->result &= 0xff00ffff;
1858 * looks good. drop through, and check the next byte.
1862 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
1863 set_host_byte(scmd, DID_TIME_OUT);
1867 case DID_NO_CONNECT:
1868 case DID_BAD_TARGET:
1870 * note - this means that we just report the status back
1871 * to the top level driver, not that we actually think
1872 * that it indicates SUCCESS.
1875 case DID_SOFT_ERROR:
1877 * when the low level driver returns did_soft_error,
1878 * it is responsible for keeping an internal retry counter
1879 * in order to avoid endless loops (db)
1886 return ADD_TO_MLQUEUE;
1887 case DID_TRANSPORT_DISRUPTED:
1889 * LLD/transport was disrupted during processing of the IO.
1890 * The transport class is now blocked/blocking,
1891 * and the transport will decide what to do with the IO
1892 * based on its timers and recovery capablilities if
1893 * there are enough retries.
1896 case DID_TRANSPORT_FAILFAST:
1898 * The transport decided to failfast the IO (most likely
1899 * the fast io fail tmo fired), so send IO directly upwards.
1902 case DID_TRANSPORT_MARGINAL:
1904 * caller has decided not to do retries on
1905 * abort success, so send IO directly upwards
1909 if (get_status_byte(scmd) == SAM_STAT_RESERVATION_CONFLICT)
1911 * execute reservation conflict processing code
1921 * when we scan the bus, we get timeout messages for
1922 * these commands if there is no device available.
1923 * other hosts report did_no_connect for the same thing.
1925 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1926 scmd->cmnd[0] == INQUIRY)) {
1938 * check the status byte to see if this indicates anything special.
1940 switch (get_status_byte(scmd)) {
1941 case SAM_STAT_TASK_SET_FULL:
1942 scsi_handle_queue_full(scmd->device);
1944 * the case of trying to send too many commands to a
1945 * tagged queueing device.
1950 * device can't talk to us at the moment. Should only
1951 * occur (SAM-3) when the task queue is empty, so will cause
1952 * the empty queue handling to trigger a stall in the
1955 return ADD_TO_MLQUEUE;
1957 if (scmd->cmnd[0] == REPORT_LUNS)
1958 scmd->device->sdev_target->expecting_lun_change = 0;
1959 scsi_handle_queue_ramp_up(scmd->device);
1961 case SAM_STAT_COMMAND_TERMINATED:
1963 case SAM_STAT_TASK_ABORTED:
1965 case SAM_STAT_CHECK_CONDITION:
1966 rtn = scsi_check_sense(scmd);
1967 if (rtn == NEEDS_RETRY)
1969 /* if rtn == FAILED, we have no sense information;
1970 * returning FAILED will wake the error handler thread
1971 * to collect the sense and redo the decide
1974 case SAM_STAT_CONDITION_MET:
1975 case SAM_STAT_INTERMEDIATE:
1976 case SAM_STAT_INTERMEDIATE_CONDITION_MET:
1977 case SAM_STAT_ACA_ACTIVE:
1979 * who knows? FIXME(eric)
1983 case SAM_STAT_RESERVATION_CONFLICT:
1984 sdev_printk(KERN_INFO, scmd->device,
1985 "reservation conflict\n");
1986 set_host_byte(scmd, DID_NEXUS_FAILURE);
1987 return SUCCESS; /* causes immediate i/o error */
1993 /* we requeue for retry because the error was retryable, and
1994 * the request was not marked fast fail. Note that above,
1995 * even if the request is marked fast fail, we still requeue
1996 * for queue congestion conditions (QUEUE_FULL or BUSY) */
1997 if (scsi_cmd_retry_allowed(scmd) && !scsi_noretry_cmd(scmd)) {
2001 * no more retries - report this one back to upper level.
2007 static void eh_lock_door_done(struct request *req, blk_status_t status)
2009 blk_mq_free_request(req);
2013 * scsi_eh_lock_door - Prevent medium removal for the specified device
2014 * @sdev: SCSI device to prevent medium removal
2017 * We must be called from process context.
2020 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
2021 * head of the devices request queue, and continue.
2023 static void scsi_eh_lock_door(struct scsi_device *sdev)
2025 struct scsi_cmnd *scmd;
2026 struct request *req;
2028 req = scsi_alloc_request(sdev->request_queue, REQ_OP_DRV_IN, 0);
2031 scmd = blk_mq_rq_to_pdu(req);
2033 scmd->cmnd[0] = ALLOW_MEDIUM_REMOVAL;
2037 scmd->cmnd[4] = SCSI_REMOVAL_PREVENT;
2039 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
2042 req->rq_flags |= RQF_QUIET;
2043 req->timeout = 10 * HZ;
2044 req->end_io = eh_lock_door_done;
2046 blk_execute_rq_nowait(req, true);
2050 * scsi_restart_operations - restart io operations to the specified host.
2051 * @shost: Host we are restarting.
2054 * When we entered the error handler, we blocked all further i/o to
2055 * this device. we need to 'reverse' this process.
2057 static void scsi_restart_operations(struct Scsi_Host *shost)
2059 struct scsi_device *sdev;
2060 unsigned long flags;
2063 * If the door was locked, we need to insert a door lock request
2064 * onto the head of the SCSI request queue for the device. There
2065 * is no point trying to lock the door of an off-line device.
2067 shost_for_each_device(sdev, shost) {
2068 if (scsi_device_online(sdev) && sdev->was_reset && sdev->locked) {
2069 scsi_eh_lock_door(sdev);
2070 sdev->was_reset = 0;
2075 * next free up anything directly waiting upon the host. this
2076 * will be requests for character device operations, and also for
2077 * ioctls to queued block devices.
2079 SCSI_LOG_ERROR_RECOVERY(3,
2080 shost_printk(KERN_INFO, shost, "waking up host to restart\n"));
2082 spin_lock_irqsave(shost->host_lock, flags);
2083 if (scsi_host_set_state(shost, SHOST_RUNNING))
2084 if (scsi_host_set_state(shost, SHOST_CANCEL))
2085 BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
2086 spin_unlock_irqrestore(shost->host_lock, flags);
2088 wake_up(&shost->host_wait);
2091 * finally we need to re-initiate requests that may be pending. we will
2092 * have had everything blocked while error handling is taking place, and
2093 * now that error recovery is done, we will need to ensure that these
2094 * requests are started.
2096 scsi_run_host_queues(shost);
2099 * if eh is active and host_eh_scheduled is pending we need to re-run
2100 * recovery. we do this check after scsi_run_host_queues() to allow
2101 * everything pent up since the last eh run a chance to make forward
2102 * progress before we sync again. Either we'll immediately re-run
2103 * recovery or scsi_device_unbusy() will wake us again when these
2104 * pending commands complete.
2106 spin_lock_irqsave(shost->host_lock, flags);
2107 if (shost->host_eh_scheduled)
2108 if (scsi_host_set_state(shost, SHOST_RECOVERY))
2109 WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY));
2110 spin_unlock_irqrestore(shost->host_lock, flags);
2114 * scsi_eh_ready_devs - check device ready state and recover if not.
2115 * @shost: host to be recovered.
2116 * @work_q: &list_head for pending commands.
2117 * @done_q: &list_head for processed commands.
2119 void scsi_eh_ready_devs(struct Scsi_Host *shost,
2120 struct list_head *work_q,
2121 struct list_head *done_q)
2123 if (!scsi_eh_stu(shost, work_q, done_q))
2124 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
2125 if (!scsi_eh_target_reset(shost, work_q, done_q))
2126 if (!scsi_eh_bus_reset(shost, work_q, done_q))
2127 if (!scsi_eh_host_reset(shost, work_q, done_q))
2128 scsi_eh_offline_sdevs(work_q,
2131 EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
2134 * scsi_eh_flush_done_q - finish processed commands or retry them.
2135 * @done_q: list_head of processed commands.
2137 void scsi_eh_flush_done_q(struct list_head *done_q)
2139 struct scsi_cmnd *scmd, *next;
2141 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
2142 list_del_init(&scmd->eh_entry);
2143 if (scsi_device_online(scmd->device) &&
2144 !scsi_noretry_cmd(scmd) && scsi_cmd_retry_allowed(scmd) &&
2145 scsi_eh_should_retry_cmd(scmd)) {
2146 SCSI_LOG_ERROR_RECOVERY(3,
2147 scmd_printk(KERN_INFO, scmd,
2148 "%s: flush retry cmd\n",
2150 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
2153 * If just we got sense for the device (called
2154 * scsi_eh_get_sense), scmd->result is already
2155 * set, do not set DID_TIME_OUT.
2158 scmd->result |= (DID_TIME_OUT << 16);
2159 SCSI_LOG_ERROR_RECOVERY(3,
2160 scmd_printk(KERN_INFO, scmd,
2161 "%s: flush finish cmd\n",
2163 scsi_finish_command(scmd);
2167 EXPORT_SYMBOL(scsi_eh_flush_done_q);
2170 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
2171 * @shost: Host to unjam.
2174 * When we come in here, we *know* that all commands on the bus have
2175 * either completed, failed or timed out. we also know that no further
2176 * commands are being sent to the host, so things are relatively quiet
2177 * and we have freedom to fiddle with things as we wish.
2179 * This is only the *default* implementation. it is possible for
2180 * individual drivers to supply their own version of this function, and
2181 * if the maintainer wishes to do this, it is strongly suggested that
2182 * this function be taken as a template and modified. this function
2183 * was designed to correctly handle problems for about 95% of the
2184 * different cases out there, and it should always provide at least a
2185 * reasonable amount of error recovery.
2187 * Any command marked 'failed' or 'timeout' must eventually have
2188 * scsi_finish_cmd() called for it. we do all of the retry stuff
2189 * here, so when we restart the host after we return it should have an
2192 static void scsi_unjam_host(struct Scsi_Host *shost)
2194 unsigned long flags;
2195 LIST_HEAD(eh_work_q);
2196 LIST_HEAD(eh_done_q);
2198 spin_lock_irqsave(shost->host_lock, flags);
2199 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
2200 spin_unlock_irqrestore(shost->host_lock, flags);
2202 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
2204 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
2205 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
2207 spin_lock_irqsave(shost->host_lock, flags);
2208 if (shost->eh_deadline != -1)
2209 shost->last_reset = 0;
2210 spin_unlock_irqrestore(shost->host_lock, flags);
2211 scsi_eh_flush_done_q(&eh_done_q);
2215 * scsi_error_handler - SCSI error handler thread
2216 * @data: Host for which we are running.
2219 * This is the main error handling loop. This is run as a kernel thread
2220 * for every SCSI host and handles all error handling activity.
2222 int scsi_error_handler(void *data)
2224 struct Scsi_Host *shost = data;
2227 * We use TASK_INTERRUPTIBLE so that the thread is not
2228 * counted against the load average as a running process.
2229 * We never actually get interrupted because kthread_run
2230 * disables signal delivery for the created thread.
2234 * The sequence in kthread_stop() sets the stop flag first
2235 * then wakes the process. To avoid missed wakeups, the task
2236 * should always be in a non running state before the stop
2239 set_current_state(TASK_INTERRUPTIBLE);
2240 if (kthread_should_stop())
2243 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
2244 shost->host_failed != scsi_host_busy(shost)) {
2245 SCSI_LOG_ERROR_RECOVERY(1,
2246 shost_printk(KERN_INFO, shost,
2247 "scsi_eh_%d: sleeping\n",
2253 __set_current_state(TASK_RUNNING);
2254 SCSI_LOG_ERROR_RECOVERY(1,
2255 shost_printk(KERN_INFO, shost,
2256 "scsi_eh_%d: waking up %d/%d/%d\n",
2257 shost->host_no, shost->host_eh_scheduled,
2259 scsi_host_busy(shost)));
2262 * We have a host that is failing for some reason. Figure out
2263 * what we need to do to get it up and online again (if we can).
2264 * If we fail, we end up taking the thing offline.
2266 if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
2267 SCSI_LOG_ERROR_RECOVERY(1,
2268 shost_printk(KERN_ERR, shost,
2269 "scsi_eh_%d: unable to autoresume\n",
2274 if (shost->transportt->eh_strategy_handler)
2275 shost->transportt->eh_strategy_handler(shost);
2277 scsi_unjam_host(shost);
2279 /* All scmds have been handled */
2280 shost->host_failed = 0;
2283 * Note - if the above fails completely, the action is to take
2284 * individual devices offline and flush the queue of any
2285 * outstanding requests that may have been pending. When we
2286 * restart, we restart any I/O to any other devices on the bus
2287 * which are still online.
2289 scsi_restart_operations(shost);
2290 if (!shost->eh_noresume)
2291 scsi_autopm_put_host(shost);
2293 __set_current_state(TASK_RUNNING);
2295 SCSI_LOG_ERROR_RECOVERY(1,
2296 shost_printk(KERN_INFO, shost,
2297 "Error handler scsi_eh_%d exiting\n",
2299 shost->ehandler = NULL;
2304 * Function: scsi_report_bus_reset()
2306 * Purpose: Utility function used by low-level drivers to report that
2307 * they have observed a bus reset on the bus being handled.
2309 * Arguments: shost - Host in question
2310 * channel - channel on which reset was observed.
2314 * Lock status: Host lock must be held.
2316 * Notes: This only needs to be called if the reset is one which
2317 * originates from an unknown location. Resets originated
2318 * by the mid-level itself don't need to call this, but there
2319 * should be no harm.
2321 * The main purpose of this is to make sure that a CHECK_CONDITION
2322 * is properly treated.
2324 void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
2326 struct scsi_device *sdev;
2328 __shost_for_each_device(sdev, shost) {
2329 if (channel == sdev_channel(sdev))
2330 __scsi_report_device_reset(sdev, NULL);
2333 EXPORT_SYMBOL(scsi_report_bus_reset);
2336 * Function: scsi_report_device_reset()
2338 * Purpose: Utility function used by low-level drivers to report that
2339 * they have observed a device reset on the device being handled.
2341 * Arguments: shost - Host in question
2342 * channel - channel on which reset was observed
2343 * target - target on which reset was observed
2347 * Lock status: Host lock must be held
2349 * Notes: This only needs to be called if the reset is one which
2350 * originates from an unknown location. Resets originated
2351 * by the mid-level itself don't need to call this, but there
2352 * should be no harm.
2354 * The main purpose of this is to make sure that a CHECK_CONDITION
2355 * is properly treated.
2357 void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
2359 struct scsi_device *sdev;
2361 __shost_for_each_device(sdev, shost) {
2362 if (channel == sdev_channel(sdev) &&
2363 target == sdev_id(sdev))
2364 __scsi_report_device_reset(sdev, NULL);
2367 EXPORT_SYMBOL(scsi_report_device_reset);
2370 * scsi_ioctl_reset: explicitly reset a host/bus/target/device
2371 * @dev: scsi_device to operate on
2372 * @arg: reset type (see sg.h)
2375 scsi_ioctl_reset(struct scsi_device *dev, int __user *arg)
2377 struct scsi_cmnd *scmd;
2378 struct Scsi_Host *shost = dev->host;
2380 unsigned long flags;
2382 enum scsi_disposition rtn;
2384 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2387 error = get_user(val, arg);
2391 if (scsi_autopm_get_host(shost) < 0)
2395 rq = kzalloc(sizeof(struct request) + sizeof(struct scsi_cmnd) +
2396 shost->hostt->cmd_size, GFP_KERNEL);
2398 goto out_put_autopm_host;
2399 blk_rq_init(NULL, rq);
2401 scmd = (struct scsi_cmnd *)(rq + 1);
2402 scsi_init_command(dev, scmd);
2404 scmd->submitter = SUBMITTED_BY_SCSI_RESET_IOCTL;
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);