1 // SPDX-License-Identifier: GPL-2.0-only
3 * scsi_error.c Copyright (C) 1997 Eric Youngdale
5 * SCSI error/timeout handling
6 * Initial versions: Eric Youngdale. Based upon conversations with
7 * Leonard Zubkoff and David Miller at Linux Expo,
8 * ideas originating from all over the place.
10 * Restructured scsi_unjam_host and associated functions.
11 * September 04, 2002 Mike Anderson (andmike@us.ibm.com)
13 * Forward port of Russell King's (rmk@arm.linux.org.uk) changes and
15 * September 30, 2002 Mike Anderson (andmike@us.ibm.com)
18 #include <linux/module.h>
19 #include <linux/sched.h>
20 #include <linux/gfp.h>
21 #include <linux/timer.h>
22 #include <linux/string.h>
23 #include <linux/kernel.h>
24 #include <linux/freezer.h>
25 #include <linux/kthread.h>
26 #include <linux/interrupt.h>
27 #include <linux/blkdev.h>
28 #include <linux/delay.h>
29 #include <linux/jiffies.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <scsi/scsi_dbg.h>
34 #include <scsi/scsi_device.h>
35 #include <scsi/scsi_driver.h>
36 #include <scsi/scsi_eh.h>
37 #include <scsi/scsi_common.h>
38 #include <scsi/scsi_transport.h>
39 #include <scsi/scsi_host.h>
40 #include <scsi/scsi_ioctl.h>
41 #include <scsi/scsi_dh.h>
42 #include <scsi/scsi_devinfo.h>
45 #include "scsi_priv.h"
46 #include "scsi_logging.h"
47 #include "scsi_transport_api.h"
49 #include <trace/events/scsi.h>
51 #include <asm/unaligned.h>
53 static void scsi_eh_done(struct scsi_cmnd *scmd);
56 * These should *probably* be handled by the host itself.
57 * Since it is allowed to sleep, it probably should.
59 #define BUS_RESET_SETTLE_TIME (10)
60 #define HOST_RESET_SETTLE_TIME (10)
62 static int scsi_eh_try_stu(struct scsi_cmnd *scmd);
63 static enum scsi_disposition scsi_try_to_abort_cmd(struct scsi_host_template *,
66 void scsi_eh_wakeup(struct Scsi_Host *shost, unsigned int busy)
68 lockdep_assert_held(shost->host_lock);
70 if (busy == shost->host_failed) {
71 trace_scsi_eh_wakeup(shost);
72 wake_up_process(shost->ehandler);
73 SCSI_LOG_ERROR_RECOVERY(5, shost_printk(KERN_INFO, shost,
74 "Waking error handler thread\n"));
79 * scsi_schedule_eh - schedule EH for SCSI host
80 * @shost: SCSI host to invoke error handling on.
82 * Schedule SCSI EH without scmd.
84 void scsi_schedule_eh(struct Scsi_Host *shost)
88 spin_lock_irqsave(shost->host_lock, flags);
90 if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
91 scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
92 shost->host_eh_scheduled++;
93 scsi_eh_wakeup(shost, scsi_host_busy(shost));
96 spin_unlock_irqrestore(shost->host_lock, flags);
98 EXPORT_SYMBOL_GPL(scsi_schedule_eh);
100 static int scsi_host_eh_past_deadline(struct Scsi_Host *shost)
102 if (!shost->last_reset || shost->eh_deadline == -1)
106 * 32bit accesses are guaranteed to be atomic
107 * (on all supported architectures), so instead
108 * of using a spinlock we can as well double check
109 * if eh_deadline has been set to 'off' during the
112 if (time_before(jiffies, shost->last_reset + shost->eh_deadline) &&
113 shost->eh_deadline > -1)
119 static bool scsi_cmd_retry_allowed(struct scsi_cmnd *cmd)
121 if (cmd->allowed == SCSI_CMD_RETRIES_NO_LIMIT)
124 return ++cmd->retries <= cmd->allowed;
128 * scmd_eh_abort_handler - Handle command aborts
129 * @work: command to be aborted.
131 * Note: this function must be called only for a command that has timed out.
132 * Because the block layer marks a request as complete before it calls
133 * scsi_times_out(), a .scsi_done() call from the LLD for a command that has
134 * timed out do not have any effect. Hence it is safe to call
135 * scsi_finish_command() from this function.
138 scmd_eh_abort_handler(struct work_struct *work)
140 struct scsi_cmnd *scmd =
141 container_of(work, struct scsi_cmnd, abort_work.work);
142 struct scsi_device *sdev = scmd->device;
143 enum scsi_disposition rtn;
145 if (scsi_host_eh_past_deadline(sdev->host)) {
146 SCSI_LOG_ERROR_RECOVERY(3,
147 scmd_printk(KERN_INFO, scmd,
148 "eh timeout, not aborting\n"));
150 SCSI_LOG_ERROR_RECOVERY(3,
151 scmd_printk(KERN_INFO, scmd,
152 "aborting command\n"));
153 rtn = scsi_try_to_abort_cmd(sdev->host->hostt, scmd);
154 if (rtn == SUCCESS) {
155 set_host_byte(scmd, DID_TIME_OUT);
156 if (scsi_host_eh_past_deadline(sdev->host)) {
157 SCSI_LOG_ERROR_RECOVERY(3,
158 scmd_printk(KERN_INFO, scmd,
159 "eh timeout, not retrying "
160 "aborted command\n"));
161 } else if (!scsi_noretry_cmd(scmd) &&
162 scsi_cmd_retry_allowed(scmd)) {
163 SCSI_LOG_ERROR_RECOVERY(3,
164 scmd_printk(KERN_WARNING, scmd,
165 "retry aborted command\n"));
166 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
169 SCSI_LOG_ERROR_RECOVERY(3,
170 scmd_printk(KERN_WARNING, scmd,
171 "finish aborted command\n"));
172 scsi_finish_command(scmd);
176 SCSI_LOG_ERROR_RECOVERY(3,
177 scmd_printk(KERN_INFO, scmd,
179 (rtn == FAST_IO_FAIL) ?
180 "not send" : "failed"));
184 scsi_eh_scmd_add(scmd);
188 * scsi_abort_command - schedule a command abort
189 * @scmd: scmd to abort.
191 * We only need to abort commands after a command timeout
194 scsi_abort_command(struct scsi_cmnd *scmd)
196 struct scsi_device *sdev = scmd->device;
197 struct Scsi_Host *shost = sdev->host;
200 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
202 * Retry after abort failed, escalate to next level.
204 SCSI_LOG_ERROR_RECOVERY(3,
205 scmd_printk(KERN_INFO, scmd,
206 "previous abort failed\n"));
207 BUG_ON(delayed_work_pending(&scmd->abort_work));
211 spin_lock_irqsave(shost->host_lock, flags);
212 if (shost->eh_deadline != -1 && !shost->last_reset)
213 shost->last_reset = jiffies;
214 spin_unlock_irqrestore(shost->host_lock, flags);
216 scmd->eh_eflags |= SCSI_EH_ABORT_SCHEDULED;
217 SCSI_LOG_ERROR_RECOVERY(3,
218 scmd_printk(KERN_INFO, scmd, "abort scheduled\n"));
219 queue_delayed_work(shost->tmf_work_q, &scmd->abort_work, HZ / 100);
224 * scsi_eh_reset - call into ->eh_action to reset internal counters
225 * @scmd: scmd to run eh on.
227 * The scsi driver might be carrying internal state about the
228 * devices, so we need to call into the driver to reset the
229 * internal state once the error handler is started.
231 static void scsi_eh_reset(struct scsi_cmnd *scmd)
233 if (!blk_rq_is_passthrough(scmd->request)) {
234 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
236 sdrv->eh_reset(scmd);
240 static void scsi_eh_inc_host_failed(struct rcu_head *head)
242 struct scsi_cmnd *scmd = container_of(head, typeof(*scmd), rcu);
243 struct Scsi_Host *shost = scmd->device->host;
244 unsigned int busy = scsi_host_busy(shost);
247 spin_lock_irqsave(shost->host_lock, flags);
248 shost->host_failed++;
249 scsi_eh_wakeup(shost, busy);
250 spin_unlock_irqrestore(shost->host_lock, flags);
254 * scsi_eh_scmd_add - add scsi cmd to error handling.
255 * @scmd: scmd to run eh on.
257 void scsi_eh_scmd_add(struct scsi_cmnd *scmd)
259 struct Scsi_Host *shost = scmd->device->host;
263 WARN_ON_ONCE(!shost->ehandler);
265 spin_lock_irqsave(shost->host_lock, flags);
266 if (scsi_host_set_state(shost, SHOST_RECOVERY)) {
267 ret = scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY);
270 if (shost->eh_deadline != -1 && !shost->last_reset)
271 shost->last_reset = jiffies;
274 list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
275 spin_unlock_irqrestore(shost->host_lock, flags);
277 * Ensure that all tasks observe the host state change before the
278 * host_failed change.
280 call_rcu(&scmd->rcu, scsi_eh_inc_host_failed);
284 * scsi_times_out - Timeout function for normal scsi commands.
285 * @req: request that is timing out.
288 * We do not need to lock this. There is the potential for a race
289 * only in that the normal completion handling might run, but if the
290 * normal completion function determines that the timer has already
291 * fired, then it mustn't do anything.
293 enum blk_eh_timer_return scsi_times_out(struct request *req)
295 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
296 enum blk_eh_timer_return rtn = BLK_EH_DONE;
297 struct Scsi_Host *host = scmd->device->host;
299 trace_scsi_dispatch_cmd_timeout(scmd);
300 scsi_log_completion(scmd, TIMEOUT_ERROR);
302 if (host->eh_deadline != -1 && !host->last_reset)
303 host->last_reset = jiffies;
305 if (host->hostt->eh_timed_out)
306 rtn = host->hostt->eh_timed_out(scmd);
308 if (rtn == BLK_EH_DONE) {
310 * If scsi_done() has already set SCMD_STATE_COMPLETE, do not
313 if (test_and_set_bit(SCMD_STATE_COMPLETE, &scmd->state))
315 if (scsi_abort_command(scmd) != SUCCESS) {
316 set_host_byte(scmd, DID_TIME_OUT);
317 scsi_eh_scmd_add(scmd);
325 * scsi_block_when_processing_errors - Prevent cmds from being queued.
326 * @sdev: Device on which we are performing recovery.
329 * We block until the host is out of error recovery, and then check to
330 * see whether the host or the device is offline.
333 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
335 int scsi_block_when_processing_errors(struct scsi_device *sdev)
339 wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
341 online = scsi_device_online(sdev);
345 EXPORT_SYMBOL(scsi_block_when_processing_errors);
347 #ifdef CONFIG_SCSI_LOGGING
349 * scsi_eh_prt_fail_stats - Log info on failures.
350 * @shost: scsi host being recovered.
351 * @work_q: Queue of scsi cmds to process.
353 static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
354 struct list_head *work_q)
356 struct scsi_cmnd *scmd;
357 struct scsi_device *sdev;
358 int total_failures = 0;
361 int devices_failed = 0;
363 shost_for_each_device(sdev, shost) {
364 list_for_each_entry(scmd, work_q, eh_entry) {
365 if (scmd->device == sdev) {
367 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED)
374 if (cmd_cancel || cmd_failed) {
375 SCSI_LOG_ERROR_RECOVERY(3,
376 shost_printk(KERN_INFO, shost,
377 "%s: cmds failed: %d, cancel: %d\n",
378 __func__, cmd_failed,
386 SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO, shost,
387 "Total of %d commands on %d"
388 " devices require eh work\n",
389 total_failures, devices_failed));
394 * scsi_report_lun_change - Set flag on all *other* devices on the same target
395 * to indicate that a UNIT ATTENTION is expected.
396 * @sdev: Device reporting the UNIT ATTENTION
398 static void scsi_report_lun_change(struct scsi_device *sdev)
400 sdev->sdev_target->expecting_lun_change = 1;
404 * scsi_report_sense - Examine scsi sense information and log messages for
405 * certain conditions, also issue uevents for some of them.
406 * @sdev: Device reporting the sense code
407 * @sshdr: sshdr to be examined
409 static void scsi_report_sense(struct scsi_device *sdev,
410 struct scsi_sense_hdr *sshdr)
412 enum scsi_device_event evt_type = SDEV_EVT_MAXBITS; /* i.e. none */
414 if (sshdr->sense_key == UNIT_ATTENTION) {
415 if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) {
416 evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED;
417 sdev_printk(KERN_WARNING, sdev,
418 "Inquiry data has changed");
419 } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) {
420 evt_type = SDEV_EVT_LUN_CHANGE_REPORTED;
421 scsi_report_lun_change(sdev);
422 sdev_printk(KERN_WARNING, sdev,
423 "Warning! Received an indication that the "
424 "LUN assignments on this target have "
425 "changed. The Linux SCSI layer does not "
426 "automatically remap LUN assignments.\n");
427 } else if (sshdr->asc == 0x3f)
428 sdev_printk(KERN_WARNING, sdev,
429 "Warning! Received an indication that the "
430 "operating parameters on this target have "
431 "changed. The Linux SCSI layer does not "
432 "automatically adjust these parameters.\n");
434 if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) {
435 evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED;
436 sdev_printk(KERN_WARNING, sdev,
437 "Warning! Received an indication that the "
438 "LUN reached a thin provisioning soft "
442 if (sshdr->asc == 0x29) {
443 evt_type = SDEV_EVT_POWER_ON_RESET_OCCURRED;
444 sdev_printk(KERN_WARNING, sdev,
445 "Power-on or device reset occurred\n");
448 if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) {
449 evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED;
450 sdev_printk(KERN_WARNING, sdev,
451 "Mode parameters changed");
452 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x06) {
453 evt_type = SDEV_EVT_ALUA_STATE_CHANGE_REPORTED;
454 sdev_printk(KERN_WARNING, sdev,
455 "Asymmetric access state changed");
456 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) {
457 evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED;
458 sdev_printk(KERN_WARNING, sdev,
459 "Capacity data has changed");
460 } else if (sshdr->asc == 0x2a)
461 sdev_printk(KERN_WARNING, sdev,
462 "Parameters changed");
465 if (evt_type != SDEV_EVT_MAXBITS) {
466 set_bit(evt_type, sdev->pending_events);
467 schedule_work(&sdev->event_work);
472 * scsi_check_sense - Examine scsi cmd sense
473 * @scmd: Cmd to have sense checked.
476 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
479 * When a deferred error is detected the current command has
480 * not been executed and needs retrying.
482 enum scsi_disposition scsi_check_sense(struct scsi_cmnd *scmd)
484 struct scsi_device *sdev = scmd->device;
485 struct scsi_sense_hdr sshdr;
487 if (! scsi_command_normalize_sense(scmd, &sshdr))
488 return FAILED; /* no valid sense data */
490 scsi_report_sense(sdev, &sshdr);
492 if (scsi_sense_is_deferred(&sshdr))
495 if (sdev->handler && sdev->handler->check_sense) {
496 enum scsi_disposition rc;
498 rc = sdev->handler->check_sense(sdev, &sshdr);
499 if (rc != SCSI_RETURN_NOT_HANDLED)
501 /* handler does not care. Drop down to default handling */
504 if (scmd->cmnd[0] == TEST_UNIT_READY && scmd->scsi_done != scsi_eh_done)
506 * nasty: for mid-layer issued TURs, we need to return the
507 * actual sense data without any recovery attempt. For eh
508 * issued ones, we need to try to recover and interpret
513 * Previous logic looked for FILEMARK, EOM or ILI which are
514 * mainly associated with tapes and returned SUCCESS.
516 if (sshdr.response_code == 0x70) {
518 if (scmd->sense_buffer[2] & 0xe0)
522 * descriptor format: look for "stream commands sense data
523 * descriptor" (see SSC-3). Assume single sense data
524 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
526 if ((sshdr.additional_length > 3) &&
527 (scmd->sense_buffer[8] == 0x4) &&
528 (scmd->sense_buffer[11] & 0xe0))
532 switch (sshdr.sense_key) {
535 case RECOVERED_ERROR:
536 return /* soft_error */ SUCCESS;
538 case ABORTED_COMMAND:
539 if (sshdr.asc == 0x10) /* DIF */
542 if (sshdr.asc == 0x44 && sdev->sdev_bflags & BLIST_RETRY_ITF)
543 return ADD_TO_MLQUEUE;
544 if (sshdr.asc == 0xc1 && sshdr.ascq == 0x01 &&
545 sdev->sdev_bflags & BLIST_RETRY_ASC_C1)
546 return ADD_TO_MLQUEUE;
552 * if we are expecting a cc/ua because of a bus reset that we
553 * performed, treat this just as a retry. otherwise this is
554 * information that we should pass up to the upper-level driver
555 * so that we can deal with it there.
557 if (scmd->device->expecting_cc_ua) {
559 * Because some device does not queue unit
560 * attentions correctly, we carefully check
561 * additional sense code and qualifier so as
562 * not to squash media change unit attention.
564 if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) {
565 scmd->device->expecting_cc_ua = 0;
570 * we might also expect a cc/ua if another LUN on the target
571 * reported a UA with an ASC/ASCQ of 3F 0E -
572 * REPORTED LUNS DATA HAS CHANGED.
574 if (scmd->device->sdev_target->expecting_lun_change &&
575 sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
578 * if the device is in the process of becoming ready, we
581 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
584 * if the device is not started, we need to wake
585 * the error handler to start the motor
587 if (scmd->device->allow_restart &&
588 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
591 * Pass the UA upwards for a determination in the completion
596 /* these are not supported */
598 if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
599 /* Thin provisioning hard threshold reached */
600 set_host_byte(scmd, DID_ALLOC_FAILURE);
605 case VOLUME_OVERFLOW:
608 set_host_byte(scmd, DID_TARGET_FAILURE);
612 if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
613 sshdr.asc == 0x13 || /* AMNF DATA FIELD */
614 sshdr.asc == 0x14) { /* RECORD NOT FOUND */
615 set_host_byte(scmd, DID_MEDIUM_ERROR);
621 if (scmd->device->retry_hwerror)
622 return ADD_TO_MLQUEUE;
624 set_host_byte(scmd, DID_TARGET_FAILURE);
627 case ILLEGAL_REQUEST:
628 if (sshdr.asc == 0x20 || /* Invalid command operation code */
629 sshdr.asc == 0x21 || /* Logical block address out of range */
630 sshdr.asc == 0x22 || /* Invalid function */
631 sshdr.asc == 0x24 || /* Invalid field in cdb */
632 sshdr.asc == 0x26 || /* Parameter value invalid */
633 sshdr.asc == 0x27) { /* Write protected */
634 set_host_byte(scmd, DID_TARGET_FAILURE);
642 EXPORT_SYMBOL_GPL(scsi_check_sense);
644 static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
646 struct scsi_host_template *sht = sdev->host->hostt;
647 struct scsi_device *tmp_sdev;
649 if (!sht->track_queue_depth ||
650 sdev->queue_depth >= sdev->max_queue_depth)
653 if (time_before(jiffies,
654 sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
657 if (time_before(jiffies,
658 sdev->last_queue_full_time + sdev->queue_ramp_up_period))
662 * Walk all devices of a target and do
665 shost_for_each_device(tmp_sdev, sdev->host) {
666 if (tmp_sdev->channel != sdev->channel ||
667 tmp_sdev->id != sdev->id ||
668 tmp_sdev->queue_depth == sdev->max_queue_depth)
671 scsi_change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1);
672 sdev->last_queue_ramp_up = jiffies;
676 static void scsi_handle_queue_full(struct scsi_device *sdev)
678 struct scsi_host_template *sht = sdev->host->hostt;
679 struct scsi_device *tmp_sdev;
681 if (!sht->track_queue_depth)
684 shost_for_each_device(tmp_sdev, sdev->host) {
685 if (tmp_sdev->channel != sdev->channel ||
686 tmp_sdev->id != sdev->id)
689 * We do not know the number of commands that were at
690 * the device when we got the queue full so we start
691 * from the highest possible value and work our way down.
693 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
698 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
699 * @scmd: SCSI cmd to examine.
702 * This is *only* called when we are examining the status of commands
703 * queued during error recovery. the main difference here is that we
704 * don't allow for the possibility of retries here, and we are a lot
705 * more restrictive about what we consider acceptable.
707 static enum scsi_disposition scsi_eh_completed_normally(struct scsi_cmnd *scmd)
710 * first check the host byte, to see if there is anything in there
711 * that would indicate what we need to do.
713 if (host_byte(scmd->result) == DID_RESET) {
715 * rats. we are already in the error handler, so we now
716 * get to try and figure out what to do next. if the sense
717 * is valid, we have a pretty good idea of what to do.
718 * if not, we mark it as FAILED.
720 return scsi_check_sense(scmd);
722 if (host_byte(scmd->result) != DID_OK)
726 * next, check the message byte.
728 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
732 * now, check the status byte to see if this indicates
735 switch (status_byte(scmd->result)) {
737 scsi_handle_queue_ramp_up(scmd->device);
739 case COMMAND_TERMINATED:
741 case CHECK_CONDITION:
742 return scsi_check_sense(scmd);
744 case INTERMEDIATE_GOOD:
745 case INTERMEDIATE_C_GOOD:
747 * who knows? FIXME(eric)
750 case RESERVATION_CONFLICT:
751 if (scmd->cmnd[0] == TEST_UNIT_READY)
752 /* it is a success, we probed the device and
755 /* otherwise, we failed to send the command */
758 scsi_handle_queue_full(scmd->device);
769 * scsi_eh_done - Completion function for error handling.
770 * @scmd: Cmd that is done.
772 static void scsi_eh_done(struct scsi_cmnd *scmd)
774 struct completion *eh_action;
776 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
777 "%s result: %x\n", __func__, scmd->result));
779 eh_action = scmd->device->host->eh_action;
785 * scsi_try_host_reset - ask host adapter to reset itself
786 * @scmd: SCSI cmd to send host reset.
788 static enum scsi_disposition scsi_try_host_reset(struct scsi_cmnd *scmd)
791 enum scsi_disposition rtn;
792 struct Scsi_Host *host = scmd->device->host;
793 struct scsi_host_template *hostt = host->hostt;
795 SCSI_LOG_ERROR_RECOVERY(3,
796 shost_printk(KERN_INFO, host, "Snd Host RST\n"));
798 if (!hostt->eh_host_reset_handler)
801 rtn = hostt->eh_host_reset_handler(scmd);
803 if (rtn == SUCCESS) {
804 if (!hostt->skip_settle_delay)
805 ssleep(HOST_RESET_SETTLE_TIME);
806 spin_lock_irqsave(host->host_lock, flags);
807 scsi_report_bus_reset(host, scmd_channel(scmd));
808 spin_unlock_irqrestore(host->host_lock, flags);
815 * scsi_try_bus_reset - ask host to perform a bus reset
816 * @scmd: SCSI cmd to send bus reset.
818 static enum scsi_disposition scsi_try_bus_reset(struct scsi_cmnd *scmd)
821 enum scsi_disposition rtn;
822 struct Scsi_Host *host = scmd->device->host;
823 struct scsi_host_template *hostt = host->hostt;
825 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
826 "%s: Snd Bus RST\n", __func__));
828 if (!hostt->eh_bus_reset_handler)
831 rtn = hostt->eh_bus_reset_handler(scmd);
833 if (rtn == SUCCESS) {
834 if (!hostt->skip_settle_delay)
835 ssleep(BUS_RESET_SETTLE_TIME);
836 spin_lock_irqsave(host->host_lock, flags);
837 scsi_report_bus_reset(host, scmd_channel(scmd));
838 spin_unlock_irqrestore(host->host_lock, flags);
844 static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
847 sdev->expecting_cc_ua = 1;
851 * scsi_try_target_reset - Ask host to perform a target reset
852 * @scmd: SCSI cmd used to send a target reset
855 * There is no timeout for this operation. if this operation is
856 * unreliable for a given host, then the host itself needs to put a
857 * timer on it, and set the host back to a consistent state prior to
860 static enum scsi_disposition scsi_try_target_reset(struct scsi_cmnd *scmd)
863 enum scsi_disposition rtn;
864 struct Scsi_Host *host = scmd->device->host;
865 struct scsi_host_template *hostt = host->hostt;
867 if (!hostt->eh_target_reset_handler)
870 rtn = hostt->eh_target_reset_handler(scmd);
871 if (rtn == SUCCESS) {
872 spin_lock_irqsave(host->host_lock, flags);
873 __starget_for_each_device(scsi_target(scmd->device), NULL,
874 __scsi_report_device_reset);
875 spin_unlock_irqrestore(host->host_lock, flags);
882 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
883 * @scmd: SCSI cmd used to send BDR
886 * There is no timeout for this operation. if this operation is
887 * unreliable for a given host, then the host itself needs to put a
888 * timer on it, and set the host back to a consistent state prior to
891 static enum scsi_disposition scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
893 enum scsi_disposition rtn;
894 struct scsi_host_template *hostt = scmd->device->host->hostt;
896 if (!hostt->eh_device_reset_handler)
899 rtn = hostt->eh_device_reset_handler(scmd);
901 __scsi_report_device_reset(scmd->device, NULL);
906 * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
907 * @hostt: SCSI driver host template
908 * @scmd: SCSI cmd used to send a target reset
911 * SUCCESS, FAILED, or FAST_IO_FAIL
914 * SUCCESS does not necessarily indicate that the command
915 * has been aborted; it only indicates that the LLDDs
916 * has cleared all references to that command.
917 * LLDDs should return FAILED only if an abort was required
918 * but could not be executed. LLDDs should return FAST_IO_FAIL
919 * if the device is temporarily unavailable (eg due to a
920 * link down on FibreChannel)
922 static enum scsi_disposition
923 scsi_try_to_abort_cmd(struct scsi_host_template *hostt, struct scsi_cmnd *scmd)
925 if (!hostt->eh_abort_handler)
928 return hostt->eh_abort_handler(scmd);
931 static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
933 if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
934 if (scsi_try_bus_device_reset(scmd) != SUCCESS)
935 if (scsi_try_target_reset(scmd) != SUCCESS)
936 if (scsi_try_bus_reset(scmd) != SUCCESS)
937 scsi_try_host_reset(scmd);
941 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
942 * @scmd: SCSI command structure to hijack
943 * @ses: structure to save restore information
944 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
945 * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
946 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
948 * This function is used to save a scsi command information before re-execution
949 * as part of the error recovery process. If @sense_bytes is 0 the command
950 * sent must be one that does not transfer any data. If @sense_bytes != 0
951 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
952 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
954 void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
955 unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
957 struct scsi_device *sdev = scmd->device;
960 * We need saved copies of a number of fields - this is because
961 * error handling may need to overwrite these with different values
962 * to run different commands, and once error handling is complete,
963 * we will need to restore these values prior to running the actual
966 ses->cmd_len = scmd->cmd_len;
967 ses->cmnd = scmd->cmnd;
968 ses->data_direction = scmd->sc_data_direction;
969 ses->sdb = scmd->sdb;
970 ses->result = scmd->result;
971 ses->resid_len = scmd->req.resid_len;
972 ses->underflow = scmd->underflow;
973 ses->prot_op = scmd->prot_op;
974 ses->eh_eflags = scmd->eh_eflags;
976 scmd->prot_op = SCSI_PROT_NORMAL;
978 scmd->cmnd = ses->eh_cmnd;
979 memset(scmd->cmnd, 0, BLK_MAX_CDB);
980 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
982 scmd->req.resid_len = 0;
985 scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
987 sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
989 scmd->sdb.table.sgl = &ses->sense_sgl;
990 scmd->sc_data_direction = DMA_FROM_DEVICE;
991 scmd->sdb.table.nents = scmd->sdb.table.orig_nents = 1;
992 scmd->cmnd[0] = REQUEST_SENSE;
993 scmd->cmnd[4] = scmd->sdb.length;
994 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
996 scmd->sc_data_direction = DMA_NONE;
998 BUG_ON(cmnd_size > BLK_MAX_CDB);
999 memcpy(scmd->cmnd, cmnd, cmnd_size);
1000 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
1004 scmd->underflow = 0;
1006 if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
1007 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
1008 (sdev->lun << 5 & 0xe0);
1011 * Zero the sense buffer. The scsi spec mandates that any
1012 * untransferred sense data should be interpreted as being zero.
1014 memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1016 EXPORT_SYMBOL(scsi_eh_prep_cmnd);
1019 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
1020 * @scmd: SCSI command structure to restore
1021 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
1023 * Undo any damage done by above scsi_eh_prep_cmnd().
1025 void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
1028 * Restore original data
1030 scmd->cmd_len = ses->cmd_len;
1031 scmd->cmnd = ses->cmnd;
1032 scmd->sc_data_direction = ses->data_direction;
1033 scmd->sdb = ses->sdb;
1034 scmd->result = ses->result;
1035 scmd->req.resid_len = ses->resid_len;
1036 scmd->underflow = ses->underflow;
1037 scmd->prot_op = ses->prot_op;
1038 scmd->eh_eflags = ses->eh_eflags;
1040 EXPORT_SYMBOL(scsi_eh_restore_cmnd);
1043 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
1044 * @scmd: SCSI command structure to hijack
1045 * @cmnd: CDB to send
1046 * @cmnd_size: size in bytes of @cmnd
1047 * @timeout: timeout for this request
1048 * @sense_bytes: size of sense data to copy or 0
1050 * This function is used to send a scsi command down to a target device
1051 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
1054 * SUCCESS or FAILED or NEEDS_RETRY
1056 static enum scsi_disposition scsi_send_eh_cmnd(struct scsi_cmnd *scmd,
1057 unsigned char *cmnd, int cmnd_size, int timeout, unsigned sense_bytes)
1059 struct scsi_device *sdev = scmd->device;
1060 struct Scsi_Host *shost = sdev->host;
1061 DECLARE_COMPLETION_ONSTACK(done);
1062 unsigned long timeleft = timeout, delay;
1063 struct scsi_eh_save ses;
1064 const unsigned long stall_for = msecs_to_jiffies(100);
1068 scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
1069 shost->eh_action = &done;
1071 scsi_log_send(scmd);
1072 scmd->scsi_done = scsi_eh_done;
1073 scmd->flags |= SCMD_LAST;
1076 * Lock sdev->state_mutex to avoid that scsi_device_quiesce() can
1077 * change the SCSI device state after we have examined it and before
1078 * .queuecommand() is called.
1080 mutex_lock(&sdev->state_mutex);
1081 while (sdev->sdev_state == SDEV_BLOCK && timeleft > 0) {
1082 mutex_unlock(&sdev->state_mutex);
1083 SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_DEBUG, sdev,
1084 "%s: state %d <> %d\n", __func__, sdev->sdev_state,
1086 delay = min(timeleft, stall_for);
1088 msleep(jiffies_to_msecs(delay));
1089 mutex_lock(&sdev->state_mutex);
1091 if (sdev->sdev_state != SDEV_BLOCK)
1092 rtn = shost->hostt->queuecommand(shost, scmd);
1094 rtn = SCSI_MLQUEUE_DEVICE_BUSY;
1095 mutex_unlock(&sdev->state_mutex);
1098 if (timeleft > stall_for) {
1099 scsi_eh_restore_cmnd(scmd, &ses);
1100 timeleft -= stall_for;
1101 msleep(jiffies_to_msecs(stall_for));
1104 /* signal not to enter either branch of the if () below */
1108 timeleft = wait_for_completion_timeout(&done, timeout);
1112 shost->eh_action = NULL;
1114 scsi_log_completion(scmd, rtn);
1116 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1117 "%s timeleft: %ld\n",
1118 __func__, timeleft));
1121 * If there is time left scsi_eh_done got called, and we will examine
1122 * the actual status codes to see whether the command actually did
1123 * complete normally, else if we have a zero return and no time left,
1124 * the command must still be pending, so abort it and return FAILED.
1125 * If we never actually managed to issue the command, because
1126 * ->queuecommand() kept returning non zero, use the rtn = FAILED
1127 * value above (so don't execute either branch of the if)
1130 rtn = scsi_eh_completed_normally(scmd);
1131 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1132 "%s: scsi_eh_completed_normally %x\n", __func__, rtn));
1139 case ADD_TO_MLQUEUE:
1146 } else if (rtn != FAILED) {
1147 scsi_abort_eh_cmnd(scmd);
1151 scsi_eh_restore_cmnd(scmd, &ses);
1157 * scsi_request_sense - Request sense data from a particular target.
1158 * @scmd: SCSI cmd for request sense.
1161 * Some hosts automatically obtain this information, others require
1162 * that we obtain it on our own. This function will *not* return until
1163 * the command either times out, or it completes.
1165 static enum scsi_disposition scsi_request_sense(struct scsi_cmnd *scmd)
1167 return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
1170 static enum scsi_disposition
1171 scsi_eh_action(struct scsi_cmnd *scmd, enum scsi_disposition rtn)
1173 if (!blk_rq_is_passthrough(scmd->request)) {
1174 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
1175 if (sdrv->eh_action)
1176 rtn = sdrv->eh_action(scmd, rtn);
1182 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
1183 * @scmd: Original SCSI cmd that eh has finished.
1184 * @done_q: Queue for processed commands.
1187 * We don't want to use the normal command completion while we are are
1188 * still handling errors - it may cause other commands to be queued,
1189 * and that would disturb what we are doing. Thus we really want to
1190 * keep a list of pending commands for final completion, and once we
1191 * are ready to leave error handling we handle completion for real.
1193 void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
1195 list_move_tail(&scmd->eh_entry, done_q);
1197 EXPORT_SYMBOL(scsi_eh_finish_cmd);
1200 * scsi_eh_get_sense - Get device sense data.
1201 * @work_q: Queue of commands to process.
1202 * @done_q: Queue of processed commands.
1205 * See if we need to request sense information. if so, then get it
1206 * now, so we have a better idea of what to do.
1209 * This has the unfortunate side effect that if a shost adapter does
1210 * not automatically request sense information, we end up shutting
1211 * it down before we request it.
1213 * All drivers should request sense information internally these days,
1214 * so for now all I have to say is tough noogies if you end up in here.
1216 * XXX: Long term this code should go away, but that needs an audit of
1219 int scsi_eh_get_sense(struct list_head *work_q,
1220 struct list_head *done_q)
1222 struct scsi_cmnd *scmd, *next;
1223 struct Scsi_Host *shost;
1224 enum scsi_disposition rtn;
1227 * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
1228 * should not get sense.
1230 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1231 if ((scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) ||
1232 SCSI_SENSE_VALID(scmd))
1235 shost = scmd->device->host;
1236 if (scsi_host_eh_past_deadline(shost)) {
1237 SCSI_LOG_ERROR_RECOVERY(3,
1238 scmd_printk(KERN_INFO, scmd,
1239 "%s: skip request sense, past eh deadline\n",
1243 if (status_byte(scmd->result) != CHECK_CONDITION)
1245 * don't request sense if there's no check condition
1246 * status because the error we're processing isn't one
1247 * that has a sense code (and some devices get
1248 * confused by sense requests out of the blue)
1252 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
1253 "%s: requesting sense\n",
1255 rtn = scsi_request_sense(scmd);
1259 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1260 "sense requested, result %x\n", scmd->result));
1261 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd));
1263 rtn = scsi_decide_disposition(scmd);
1266 * if the result was normal, then just pass it along to the
1271 * We don't want this command reissued, just finished
1272 * with the sense data, so set retries to the max
1273 * allowed to ensure it won't get reissued. If the user
1274 * has requested infinite retries, we also want to
1275 * finish this command, so force completion by setting
1276 * retries and allowed to the same value.
1278 if (scmd->allowed == SCSI_CMD_RETRIES_NO_LIMIT)
1279 scmd->retries = scmd->allowed = 1;
1281 scmd->retries = scmd->allowed;
1282 else if (rtn != NEEDS_RETRY)
1285 scsi_eh_finish_cmd(scmd, done_q);
1288 return list_empty(work_q);
1290 EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
1293 * scsi_eh_tur - Send TUR to device.
1294 * @scmd: &scsi_cmnd to send TUR
1297 * 0 - Device is ready. 1 - Device NOT ready.
1299 static int scsi_eh_tur(struct scsi_cmnd *scmd)
1301 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
1303 enum scsi_disposition rtn;
1306 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
1307 scmd->device->eh_timeout, 0);
1309 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1310 "%s return: %x\n", __func__, rtn));
1325 * scsi_eh_test_devices - check if devices are responding from error recovery.
1326 * @cmd_list: scsi commands in error recovery.
1327 * @work_q: queue for commands which still need more error recovery
1328 * @done_q: queue for commands which are finished
1329 * @try_stu: boolean on if a STU command should be tried in addition to TUR.
1332 * Tests if devices are in a working state. Commands to devices now in
1333 * a working state are sent to the done_q while commands to devices which
1334 * are still failing to respond are returned to the work_q for more
1337 static int scsi_eh_test_devices(struct list_head *cmd_list,
1338 struct list_head *work_q,
1339 struct list_head *done_q, int try_stu)
1341 struct scsi_cmnd *scmd, *next;
1342 struct scsi_device *sdev;
1345 while (!list_empty(cmd_list)) {
1346 scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
1347 sdev = scmd->device;
1350 if (scsi_host_eh_past_deadline(sdev->host)) {
1351 /* Push items back onto work_q */
1352 list_splice_init(cmd_list, work_q);
1353 SCSI_LOG_ERROR_RECOVERY(3,
1354 sdev_printk(KERN_INFO, sdev,
1355 "%s: skip test device, past eh deadline",
1361 finish_cmds = !scsi_device_online(scmd->device) ||
1362 (try_stu && !scsi_eh_try_stu(scmd) &&
1363 !scsi_eh_tur(scmd)) ||
1366 list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
1367 if (scmd->device == sdev) {
1370 scsi_eh_action(scmd, SUCCESS) == SUCCESS))
1371 scsi_eh_finish_cmd(scmd, done_q);
1373 list_move_tail(&scmd->eh_entry, work_q);
1376 return list_empty(work_q);
1380 * scsi_eh_try_stu - Send START_UNIT to device.
1381 * @scmd: &scsi_cmnd to send START_UNIT
1384 * 0 - Device is ready. 1 - Device NOT ready.
1386 static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
1388 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
1390 if (scmd->device->allow_restart) {
1392 enum scsi_disposition rtn = NEEDS_RETRY;
1394 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
1395 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->device->request_queue->rq_timeout, 0);
1405 * scsi_eh_stu - send START_UNIT if needed
1406 * @shost: &scsi host being recovered.
1407 * @work_q: &list_head for pending commands.
1408 * @done_q: &list_head for processed commands.
1411 * If commands are failing due to not ready, initializing command required,
1412 * try revalidating the device, which will end up sending a start unit.
1414 static int scsi_eh_stu(struct Scsi_Host *shost,
1415 struct list_head *work_q,
1416 struct list_head *done_q)
1418 struct scsi_cmnd *scmd, *stu_scmd, *next;
1419 struct scsi_device *sdev;
1421 shost_for_each_device(sdev, shost) {
1422 if (scsi_host_eh_past_deadline(shost)) {
1423 SCSI_LOG_ERROR_RECOVERY(3,
1424 sdev_printk(KERN_INFO, sdev,
1425 "%s: skip START_UNIT, past eh deadline\n",
1427 scsi_device_put(sdev);
1431 list_for_each_entry(scmd, work_q, eh_entry)
1432 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
1433 scsi_check_sense(scmd) == FAILED ) {
1441 SCSI_LOG_ERROR_RECOVERY(3,
1442 sdev_printk(KERN_INFO, sdev,
1443 "%s: Sending START_UNIT\n",
1446 if (!scsi_eh_try_stu(stu_scmd)) {
1447 if (!scsi_device_online(sdev) ||
1448 !scsi_eh_tur(stu_scmd)) {
1449 list_for_each_entry_safe(scmd, next,
1451 if (scmd->device == sdev &&
1452 scsi_eh_action(scmd, SUCCESS) == SUCCESS)
1453 scsi_eh_finish_cmd(scmd, done_q);
1457 SCSI_LOG_ERROR_RECOVERY(3,
1458 sdev_printk(KERN_INFO, sdev,
1459 "%s: START_UNIT failed\n",
1464 return list_empty(work_q);
1469 * scsi_eh_bus_device_reset - send bdr if needed
1470 * @shost: scsi host being recovered.
1471 * @work_q: &list_head for pending commands.
1472 * @done_q: &list_head for processed commands.
1475 * Try a bus device reset. Still, look to see whether we have multiple
1476 * devices that are jammed or not - if we have multiple devices, it
1477 * makes no sense to try bus_device_reset - we really would need to try
1478 * a bus_reset instead.
1480 static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1481 struct list_head *work_q,
1482 struct list_head *done_q)
1484 struct scsi_cmnd *scmd, *bdr_scmd, *next;
1485 struct scsi_device *sdev;
1486 enum scsi_disposition rtn;
1488 shost_for_each_device(sdev, shost) {
1489 if (scsi_host_eh_past_deadline(shost)) {
1490 SCSI_LOG_ERROR_RECOVERY(3,
1491 sdev_printk(KERN_INFO, sdev,
1492 "%s: skip BDR, past eh deadline\n",
1494 scsi_device_put(sdev);
1498 list_for_each_entry(scmd, work_q, eh_entry)
1499 if (scmd->device == sdev) {
1507 SCSI_LOG_ERROR_RECOVERY(3,
1508 sdev_printk(KERN_INFO, sdev,
1509 "%s: Sending BDR\n", current->comm));
1510 rtn = scsi_try_bus_device_reset(bdr_scmd);
1511 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
1512 if (!scsi_device_online(sdev) ||
1513 rtn == FAST_IO_FAIL ||
1514 !scsi_eh_tur(bdr_scmd)) {
1515 list_for_each_entry_safe(scmd, next,
1517 if (scmd->device == sdev &&
1518 scsi_eh_action(scmd, rtn) != FAILED)
1519 scsi_eh_finish_cmd(scmd,
1524 SCSI_LOG_ERROR_RECOVERY(3,
1525 sdev_printk(KERN_INFO, sdev,
1526 "%s: BDR failed\n", current->comm));
1530 return list_empty(work_q);
1534 * scsi_eh_target_reset - send target reset if needed
1535 * @shost: scsi host being recovered.
1536 * @work_q: &list_head for pending commands.
1537 * @done_q: &list_head for processed commands.
1540 * Try a target reset.
1542 static int scsi_eh_target_reset(struct Scsi_Host *shost,
1543 struct list_head *work_q,
1544 struct list_head *done_q)
1546 LIST_HEAD(tmp_list);
1547 LIST_HEAD(check_list);
1549 list_splice_init(work_q, &tmp_list);
1551 while (!list_empty(&tmp_list)) {
1552 struct scsi_cmnd *next, *scmd;
1553 enum scsi_disposition rtn;
1556 if (scsi_host_eh_past_deadline(shost)) {
1557 /* push back on work queue for further processing */
1558 list_splice_init(&check_list, work_q);
1559 list_splice_init(&tmp_list, work_q);
1560 SCSI_LOG_ERROR_RECOVERY(3,
1561 shost_printk(KERN_INFO, shost,
1562 "%s: Skip target reset, past eh deadline\n",
1564 return list_empty(work_q);
1567 scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
1570 SCSI_LOG_ERROR_RECOVERY(3,
1571 shost_printk(KERN_INFO, shost,
1572 "%s: Sending target reset to target %d\n",
1573 current->comm, id));
1574 rtn = scsi_try_target_reset(scmd);
1575 if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
1576 SCSI_LOG_ERROR_RECOVERY(3,
1577 shost_printk(KERN_INFO, shost,
1578 "%s: Target reset failed"
1580 current->comm, id));
1581 list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
1582 if (scmd_id(scmd) != id)
1586 list_move_tail(&scmd->eh_entry, &check_list);
1587 else if (rtn == FAST_IO_FAIL)
1588 scsi_eh_finish_cmd(scmd, done_q);
1590 /* push back on work queue for further processing */
1591 list_move(&scmd->eh_entry, work_q);
1595 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1599 * scsi_eh_bus_reset - send a bus reset
1600 * @shost: &scsi host being recovered.
1601 * @work_q: &list_head for pending commands.
1602 * @done_q: &list_head for processed commands.
1604 static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1605 struct list_head *work_q,
1606 struct list_head *done_q)
1608 struct scsi_cmnd *scmd, *chan_scmd, *next;
1609 LIST_HEAD(check_list);
1610 unsigned int channel;
1611 enum scsi_disposition rtn;
1614 * we really want to loop over the various channels, and do this on
1615 * a channel by channel basis. we should also check to see if any
1616 * of the failed commands are on soft_reset devices, and if so, skip
1620 for (channel = 0; channel <= shost->max_channel; channel++) {
1621 if (scsi_host_eh_past_deadline(shost)) {
1622 list_splice_init(&check_list, work_q);
1623 SCSI_LOG_ERROR_RECOVERY(3,
1624 shost_printk(KERN_INFO, shost,
1625 "%s: skip BRST, past eh deadline\n",
1627 return list_empty(work_q);
1631 list_for_each_entry(scmd, work_q, eh_entry) {
1632 if (channel == scmd_channel(scmd)) {
1636 * FIXME add back in some support for
1637 * soft_reset devices.
1644 SCSI_LOG_ERROR_RECOVERY(3,
1645 shost_printk(KERN_INFO, shost,
1646 "%s: Sending BRST chan: %d\n",
1647 current->comm, channel));
1648 rtn = scsi_try_bus_reset(chan_scmd);
1649 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
1650 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1651 if (channel == scmd_channel(scmd)) {
1652 if (rtn == FAST_IO_FAIL)
1653 scsi_eh_finish_cmd(scmd,
1656 list_move_tail(&scmd->eh_entry,
1661 SCSI_LOG_ERROR_RECOVERY(3,
1662 shost_printk(KERN_INFO, shost,
1663 "%s: BRST failed chan: %d\n",
1664 current->comm, channel));
1667 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1671 * scsi_eh_host_reset - send a host reset
1672 * @shost: host to be reset.
1673 * @work_q: &list_head for pending commands.
1674 * @done_q: &list_head for processed commands.
1676 static int scsi_eh_host_reset(struct Scsi_Host *shost,
1677 struct list_head *work_q,
1678 struct list_head *done_q)
1680 struct scsi_cmnd *scmd, *next;
1681 LIST_HEAD(check_list);
1682 enum scsi_disposition rtn;
1684 if (!list_empty(work_q)) {
1685 scmd = list_entry(work_q->next,
1686 struct scsi_cmnd, eh_entry);
1688 SCSI_LOG_ERROR_RECOVERY(3,
1689 shost_printk(KERN_INFO, shost,
1690 "%s: Sending HRST\n",
1693 rtn = scsi_try_host_reset(scmd);
1694 if (rtn == SUCCESS) {
1695 list_splice_init(work_q, &check_list);
1696 } else if (rtn == FAST_IO_FAIL) {
1697 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1698 scsi_eh_finish_cmd(scmd, done_q);
1701 SCSI_LOG_ERROR_RECOVERY(3,
1702 shost_printk(KERN_INFO, shost,
1703 "%s: HRST failed\n",
1707 return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
1711 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
1712 * @work_q: &list_head for pending commands.
1713 * @done_q: &list_head for processed commands.
1715 static void scsi_eh_offline_sdevs(struct list_head *work_q,
1716 struct list_head *done_q)
1718 struct scsi_cmnd *scmd, *next;
1719 struct scsi_device *sdev;
1721 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1722 sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
1723 "not ready after error recovery\n");
1724 sdev = scmd->device;
1726 mutex_lock(&sdev->state_mutex);
1727 scsi_device_set_state(sdev, SDEV_OFFLINE);
1728 mutex_unlock(&sdev->state_mutex);
1730 scsi_eh_finish_cmd(scmd, done_q);
1736 * scsi_noretry_cmd - determine if command should be failed fast
1737 * @scmd: SCSI cmd to examine.
1739 int scsi_noretry_cmd(struct scsi_cmnd *scmd)
1741 switch (host_byte(scmd->result)) {
1747 return (scmd->request->cmd_flags & REQ_FAILFAST_TRANSPORT);
1749 return (scmd->request->cmd_flags & REQ_FAILFAST_DEV);
1751 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1752 status_byte(scmd->result) == RESERVATION_CONFLICT)
1755 case DID_SOFT_ERROR:
1756 return (scmd->request->cmd_flags & REQ_FAILFAST_DRIVER);
1759 if (status_byte(scmd->result) != CHECK_CONDITION)
1764 * assume caller has checked sense and determined
1765 * the check condition was retryable.
1767 if (scmd->request->cmd_flags & REQ_FAILFAST_DEV ||
1768 blk_rq_is_passthrough(scmd->request))
1775 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1776 * @scmd: SCSI cmd to examine.
1779 * This is *only* called when we are examining the status after sending
1780 * out the actual data command. any commands that are queued for error
1781 * recovery (e.g. test_unit_ready) do *not* come through here.
1783 * When this routine returns failed, it means the error handler thread
1784 * is woken. In cases where the error code indicates an error that
1785 * doesn't require the error handler read (i.e. we don't need to
1786 * abort/reset), this function should return SUCCESS.
1788 enum scsi_disposition scsi_decide_disposition(struct scsi_cmnd *scmd)
1790 enum scsi_disposition rtn;
1793 * if the device is offline, then we clearly just pass the result back
1794 * up to the top level.
1796 if (!scsi_device_online(scmd->device)) {
1797 SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO, scmd,
1798 "%s: device offline - report as SUCCESS\n", __func__));
1803 * first check the host byte, to see if there is anything in there
1804 * that would indicate what we need to do.
1806 switch (host_byte(scmd->result)) {
1807 case DID_PASSTHROUGH:
1809 * no matter what, pass this through to the upper layer.
1810 * nuke this special code so that it looks like we are saying
1813 scmd->result &= 0xff00ffff;
1817 * looks good. drop through, and check the next byte.
1821 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
1822 set_host_byte(scmd, DID_TIME_OUT);
1826 case DID_NO_CONNECT:
1827 case DID_BAD_TARGET:
1829 * note - this means that we just report the status back
1830 * to the top level driver, not that we actually think
1831 * that it indicates SUCCESS.
1834 case DID_SOFT_ERROR:
1836 * when the low level driver returns did_soft_error,
1837 * it is responsible for keeping an internal retry counter
1838 * in order to avoid endless loops (db)
1845 return ADD_TO_MLQUEUE;
1846 case DID_TRANSPORT_DISRUPTED:
1848 * LLD/transport was disrupted during processing of the IO.
1849 * The transport class is now blocked/blocking,
1850 * and the transport will decide what to do with the IO
1851 * based on its timers and recovery capablilities if
1852 * there are enough retries.
1855 case DID_TRANSPORT_FAILFAST:
1857 * The transport decided to failfast the IO (most likely
1858 * the fast io fail tmo fired), so send IO directly upwards.
1862 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1863 status_byte(scmd->result) == RESERVATION_CONFLICT)
1865 * execute reservation conflict processing code
1875 * when we scan the bus, we get timeout messages for
1876 * these commands if there is no device available.
1877 * other hosts report did_no_connect for the same thing.
1879 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1880 scmd->cmnd[0] == INQUIRY)) {
1892 * next, check the message byte.
1894 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
1898 * check the status byte to see if this indicates anything special.
1900 switch (status_byte(scmd->result)) {
1902 scsi_handle_queue_full(scmd->device);
1904 * the case of trying to send too many commands to a
1905 * tagged queueing device.
1910 * device can't talk to us at the moment. Should only
1911 * occur (SAM-3) when the task queue is empty, so will cause
1912 * the empty queue handling to trigger a stall in the
1915 return ADD_TO_MLQUEUE;
1917 if (scmd->cmnd[0] == REPORT_LUNS)
1918 scmd->device->sdev_target->expecting_lun_change = 0;
1919 scsi_handle_queue_ramp_up(scmd->device);
1921 case COMMAND_TERMINATED:
1925 case CHECK_CONDITION:
1926 rtn = scsi_check_sense(scmd);
1927 if (rtn == NEEDS_RETRY)
1929 /* if rtn == FAILED, we have no sense information;
1930 * returning FAILED will wake the error handler thread
1931 * to collect the sense and redo the decide
1934 case CONDITION_GOOD:
1935 case INTERMEDIATE_GOOD:
1936 case INTERMEDIATE_C_GOOD:
1939 * who knows? FIXME(eric)
1943 case RESERVATION_CONFLICT:
1944 sdev_printk(KERN_INFO, scmd->device,
1945 "reservation conflict\n");
1946 set_host_byte(scmd, DID_NEXUS_FAILURE);
1947 return SUCCESS; /* causes immediate i/o error */
1955 /* we requeue for retry because the error was retryable, and
1956 * the request was not marked fast fail. Note that above,
1957 * even if the request is marked fast fail, we still requeue
1958 * for queue congestion conditions (QUEUE_FULL or BUSY) */
1959 if (scsi_cmd_retry_allowed(scmd) && !scsi_noretry_cmd(scmd)) {
1963 * no more retries - report this one back to upper level.
1969 static void eh_lock_door_done(struct request *req, blk_status_t status)
1971 blk_put_request(req);
1975 * scsi_eh_lock_door - Prevent medium removal for the specified device
1976 * @sdev: SCSI device to prevent medium removal
1979 * We must be called from process context.
1982 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1983 * head of the devices request queue, and continue.
1985 static void scsi_eh_lock_door(struct scsi_device *sdev)
1987 struct request *req;
1988 struct scsi_request *rq;
1990 req = blk_get_request(sdev->request_queue, REQ_OP_SCSI_IN, 0);
1995 rq->cmd[0] = ALLOW_MEDIUM_REMOVAL;
1999 rq->cmd[4] = SCSI_REMOVAL_PREVENT;
2001 rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
2003 req->rq_flags |= RQF_QUIET;
2004 req->timeout = 10 * HZ;
2007 blk_execute_rq_nowait(req->q, NULL, req, 1, eh_lock_door_done);
2011 * scsi_restart_operations - restart io operations to the specified host.
2012 * @shost: Host we are restarting.
2015 * When we entered the error handler, we blocked all further i/o to
2016 * this device. we need to 'reverse' this process.
2018 static void scsi_restart_operations(struct Scsi_Host *shost)
2020 struct scsi_device *sdev;
2021 unsigned long flags;
2024 * If the door was locked, we need to insert a door lock request
2025 * onto the head of the SCSI request queue for the device. There
2026 * is no point trying to lock the door of an off-line device.
2028 shost_for_each_device(sdev, shost) {
2029 if (scsi_device_online(sdev) && sdev->was_reset && sdev->locked) {
2030 scsi_eh_lock_door(sdev);
2031 sdev->was_reset = 0;
2036 * next free up anything directly waiting upon the host. this
2037 * will be requests for character device operations, and also for
2038 * ioctls to queued block devices.
2040 SCSI_LOG_ERROR_RECOVERY(3,
2041 shost_printk(KERN_INFO, shost, "waking up host to restart\n"));
2043 spin_lock_irqsave(shost->host_lock, flags);
2044 if (scsi_host_set_state(shost, SHOST_RUNNING))
2045 if (scsi_host_set_state(shost, SHOST_CANCEL))
2046 BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
2047 spin_unlock_irqrestore(shost->host_lock, flags);
2049 wake_up(&shost->host_wait);
2052 * finally we need to re-initiate requests that may be pending. we will
2053 * have had everything blocked while error handling is taking place, and
2054 * now that error recovery is done, we will need to ensure that these
2055 * requests are started.
2057 scsi_run_host_queues(shost);
2060 * if eh is active and host_eh_scheduled is pending we need to re-run
2061 * recovery. we do this check after scsi_run_host_queues() to allow
2062 * everything pent up since the last eh run a chance to make forward
2063 * progress before we sync again. Either we'll immediately re-run
2064 * recovery or scsi_device_unbusy() will wake us again when these
2065 * pending commands complete.
2067 spin_lock_irqsave(shost->host_lock, flags);
2068 if (shost->host_eh_scheduled)
2069 if (scsi_host_set_state(shost, SHOST_RECOVERY))
2070 WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY));
2071 spin_unlock_irqrestore(shost->host_lock, flags);
2075 * scsi_eh_ready_devs - check device ready state and recover if not.
2076 * @shost: host to be recovered.
2077 * @work_q: &list_head for pending commands.
2078 * @done_q: &list_head for processed commands.
2080 void scsi_eh_ready_devs(struct Scsi_Host *shost,
2081 struct list_head *work_q,
2082 struct list_head *done_q)
2084 if (!scsi_eh_stu(shost, work_q, done_q))
2085 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
2086 if (!scsi_eh_target_reset(shost, work_q, done_q))
2087 if (!scsi_eh_bus_reset(shost, work_q, done_q))
2088 if (!scsi_eh_host_reset(shost, work_q, done_q))
2089 scsi_eh_offline_sdevs(work_q,
2092 EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
2095 * scsi_eh_flush_done_q - finish processed commands or retry them.
2096 * @done_q: list_head of processed commands.
2098 void scsi_eh_flush_done_q(struct list_head *done_q)
2100 struct scsi_cmnd *scmd, *next;
2102 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
2103 list_del_init(&scmd->eh_entry);
2104 if (scsi_device_online(scmd->device) &&
2105 !scsi_noretry_cmd(scmd) && scsi_cmd_retry_allowed(scmd)) {
2106 SCSI_LOG_ERROR_RECOVERY(3,
2107 scmd_printk(KERN_INFO, scmd,
2108 "%s: flush retry cmd\n",
2110 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
2113 * If just we got sense for the device (called
2114 * scsi_eh_get_sense), scmd->result is already
2115 * set, do not set DRIVER_TIMEOUT.
2118 scmd->result |= (DRIVER_TIMEOUT << 24);
2119 SCSI_LOG_ERROR_RECOVERY(3,
2120 scmd_printk(KERN_INFO, scmd,
2121 "%s: flush finish cmd\n",
2123 scsi_finish_command(scmd);
2127 EXPORT_SYMBOL(scsi_eh_flush_done_q);
2130 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
2131 * @shost: Host to unjam.
2134 * When we come in here, we *know* that all commands on the bus have
2135 * either completed, failed or timed out. we also know that no further
2136 * commands are being sent to the host, so things are relatively quiet
2137 * and we have freedom to fiddle with things as we wish.
2139 * This is only the *default* implementation. it is possible for
2140 * individual drivers to supply their own version of this function, and
2141 * if the maintainer wishes to do this, it is strongly suggested that
2142 * this function be taken as a template and modified. this function
2143 * was designed to correctly handle problems for about 95% of the
2144 * different cases out there, and it should always provide at least a
2145 * reasonable amount of error recovery.
2147 * Any command marked 'failed' or 'timeout' must eventually have
2148 * scsi_finish_cmd() called for it. we do all of the retry stuff
2149 * here, so when we restart the host after we return it should have an
2152 static void scsi_unjam_host(struct Scsi_Host *shost)
2154 unsigned long flags;
2155 LIST_HEAD(eh_work_q);
2156 LIST_HEAD(eh_done_q);
2158 spin_lock_irqsave(shost->host_lock, flags);
2159 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
2160 spin_unlock_irqrestore(shost->host_lock, flags);
2162 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
2164 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
2165 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
2167 spin_lock_irqsave(shost->host_lock, flags);
2168 if (shost->eh_deadline != -1)
2169 shost->last_reset = 0;
2170 spin_unlock_irqrestore(shost->host_lock, flags);
2171 scsi_eh_flush_done_q(&eh_done_q);
2175 * scsi_error_handler - SCSI error handler thread
2176 * @data: Host for which we are running.
2179 * This is the main error handling loop. This is run as a kernel thread
2180 * for every SCSI host and handles all error handling activity.
2182 int scsi_error_handler(void *data)
2184 struct Scsi_Host *shost = data;
2187 * We use TASK_INTERRUPTIBLE so that the thread is not
2188 * counted against the load average as a running process.
2189 * We never actually get interrupted because kthread_run
2190 * disables signal delivery for the created thread.
2194 * The sequence in kthread_stop() sets the stop flag first
2195 * then wakes the process. To avoid missed wakeups, the task
2196 * should always be in a non running state before the stop
2199 set_current_state(TASK_INTERRUPTIBLE);
2200 if (kthread_should_stop())
2203 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
2204 shost->host_failed != scsi_host_busy(shost)) {
2205 SCSI_LOG_ERROR_RECOVERY(1,
2206 shost_printk(KERN_INFO, shost,
2207 "scsi_eh_%d: sleeping\n",
2213 __set_current_state(TASK_RUNNING);
2214 SCSI_LOG_ERROR_RECOVERY(1,
2215 shost_printk(KERN_INFO, shost,
2216 "scsi_eh_%d: waking up %d/%d/%d\n",
2217 shost->host_no, shost->host_eh_scheduled,
2219 scsi_host_busy(shost)));
2222 * We have a host that is failing for some reason. Figure out
2223 * what we need to do to get it up and online again (if we can).
2224 * If we fail, we end up taking the thing offline.
2226 if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
2227 SCSI_LOG_ERROR_RECOVERY(1,
2228 shost_printk(KERN_ERR, shost,
2229 "scsi_eh_%d: unable to autoresume\n",
2234 if (shost->transportt->eh_strategy_handler)
2235 shost->transportt->eh_strategy_handler(shost);
2237 scsi_unjam_host(shost);
2239 /* All scmds have been handled */
2240 shost->host_failed = 0;
2243 * Note - if the above fails completely, the action is to take
2244 * individual devices offline and flush the queue of any
2245 * outstanding requests that may have been pending. When we
2246 * restart, we restart any I/O to any other devices on the bus
2247 * which are still online.
2249 scsi_restart_operations(shost);
2250 if (!shost->eh_noresume)
2251 scsi_autopm_put_host(shost);
2253 __set_current_state(TASK_RUNNING);
2255 SCSI_LOG_ERROR_RECOVERY(1,
2256 shost_printk(KERN_INFO, shost,
2257 "Error handler scsi_eh_%d exiting\n",
2259 shost->ehandler = NULL;
2264 * Function: scsi_report_bus_reset()
2266 * Purpose: Utility function used by low-level drivers to report that
2267 * they have observed a bus reset on the bus being handled.
2269 * Arguments: shost - Host in question
2270 * channel - channel on which reset was observed.
2274 * Lock status: Host lock must be held.
2276 * Notes: This only needs to be called if the reset is one which
2277 * originates from an unknown location. Resets originated
2278 * by the mid-level itself don't need to call this, but there
2279 * should be no harm.
2281 * The main purpose of this is to make sure that a CHECK_CONDITION
2282 * is properly treated.
2284 void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
2286 struct scsi_device *sdev;
2288 __shost_for_each_device(sdev, shost) {
2289 if (channel == sdev_channel(sdev))
2290 __scsi_report_device_reset(sdev, NULL);
2293 EXPORT_SYMBOL(scsi_report_bus_reset);
2296 * Function: scsi_report_device_reset()
2298 * Purpose: Utility function used by low-level drivers to report that
2299 * they have observed a device reset on the device being handled.
2301 * Arguments: shost - Host in question
2302 * channel - channel on which reset was observed
2303 * target - target on which reset was observed
2307 * Lock status: Host lock must be held
2309 * Notes: This only needs to be called if the reset is one which
2310 * originates from an unknown location. Resets originated
2311 * by the mid-level itself don't need to call this, but there
2312 * should be no harm.
2314 * The main purpose of this is to make sure that a CHECK_CONDITION
2315 * is properly treated.
2317 void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
2319 struct scsi_device *sdev;
2321 __shost_for_each_device(sdev, shost) {
2322 if (channel == sdev_channel(sdev) &&
2323 target == sdev_id(sdev))
2324 __scsi_report_device_reset(sdev, NULL);
2327 EXPORT_SYMBOL(scsi_report_device_reset);
2330 scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
2335 * scsi_ioctl_reset: explicitly reset a host/bus/target/device
2336 * @dev: scsi_device to operate on
2337 * @arg: reset type (see sg.h)
2340 scsi_ioctl_reset(struct scsi_device *dev, int __user *arg)
2342 struct scsi_cmnd *scmd;
2343 struct Scsi_Host *shost = dev->host;
2345 unsigned long flags;
2347 enum scsi_disposition rtn;
2349 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2352 error = get_user(val, arg);
2356 if (scsi_autopm_get_host(shost) < 0)
2360 rq = kzalloc(sizeof(struct request) + sizeof(struct scsi_cmnd) +
2361 shost->hostt->cmd_size, GFP_KERNEL);
2363 goto out_put_autopm_host;
2364 blk_rq_init(NULL, rq);
2366 scmd = (struct scsi_cmnd *)(rq + 1);
2367 scsi_init_command(dev, scmd);
2369 scmd->cmnd = scsi_req(rq)->cmd;
2370 scmd->flags |= SCMD_LAST;
2372 scmd->scsi_done = scsi_reset_provider_done_command;
2373 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
2377 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
2379 spin_lock_irqsave(shost->host_lock, flags);
2380 shost->tmf_in_progress = 1;
2381 spin_unlock_irqrestore(shost->host_lock, flags);
2383 switch (val & ~SG_SCSI_RESET_NO_ESCALATE) {
2384 case SG_SCSI_RESET_NOTHING:
2387 case SG_SCSI_RESET_DEVICE:
2388 rtn = scsi_try_bus_device_reset(scmd);
2389 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
2392 case SG_SCSI_RESET_TARGET:
2393 rtn = scsi_try_target_reset(scmd);
2394 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
2397 case SG_SCSI_RESET_BUS:
2398 rtn = scsi_try_bus_reset(scmd);
2399 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
2402 case SG_SCSI_RESET_HOST:
2403 rtn = scsi_try_host_reset(scmd);
2412 error = (rtn == SUCCESS) ? 0 : -EIO;
2414 spin_lock_irqsave(shost->host_lock, flags);
2415 shost->tmf_in_progress = 0;
2416 spin_unlock_irqrestore(shost->host_lock, flags);
2419 * be sure to wake up anyone who was sleeping or had their queue
2420 * suspended while we performed the TMF.
2422 SCSI_LOG_ERROR_RECOVERY(3,
2423 shost_printk(KERN_INFO, shost,
2424 "waking up host to restart after TMF\n"));
2426 wake_up(&shost->host_wait);
2427 scsi_run_host_queues(shost);
2431 out_put_autopm_host:
2432 scsi_autopm_put_host(shost);
2436 bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd,
2437 struct scsi_sense_hdr *sshdr)
2439 return scsi_normalize_sense(cmd->sense_buffer,
2440 SCSI_SENSE_BUFFERSIZE, sshdr);
2442 EXPORT_SYMBOL(scsi_command_normalize_sense);
2445 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
2446 * @sense_buffer: byte array of sense data
2447 * @sb_len: number of valid bytes in sense_buffer
2448 * @info_out: pointer to 64 integer where 8 or 4 byte information
2449 * field will be placed if found.
2452 * true if information field found, false if not found.
2454 bool scsi_get_sense_info_fld(const u8 *sense_buffer, int sb_len,
2461 switch (sense_buffer[0] & 0x7f) {
2464 if (sense_buffer[0] & 0x80) {
2465 *info_out = get_unaligned_be32(&sense_buffer[3]);
2471 ucp = scsi_sense_desc_find(sense_buffer, sb_len,
2473 if (ucp && (0xa == ucp[1])) {
2474 *info_out = get_unaligned_be64(&ucp[4]);
2482 EXPORT_SYMBOL(scsi_get_sense_info_fld);