2 * scsi_error.c Copyright (C) 1997 Eric Youngdale
4 * SCSI error/timeout handling
5 * Initial versions: Eric Youngdale. Based upon conversations with
6 * Leonard Zubkoff and David Miller at Linux Expo,
7 * ideas originating from all over the place.
9 * Restructured scsi_unjam_host and associated functions.
10 * September 04, 2002 Mike Anderson (andmike@us.ibm.com)
12 * Forward port of Russell King's (rmk@arm.linux.org.uk) changes and
14 * September 30, 2002 Mike Anderson (andmike@us.ibm.com)
17 #include <linux/module.h>
18 #include <linux/sched.h>
19 #include <linux/gfp.h>
20 #include <linux/timer.h>
21 #include <linux/string.h>
22 #include <linux/kernel.h>
23 #include <linux/freezer.h>
24 #include <linux/kthread.h>
25 #include <linux/interrupt.h>
26 #include <linux/blkdev.h>
27 #include <linux/delay.h>
28 #include <linux/jiffies.h>
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_cmnd.h>
32 #include <scsi/scsi_dbg.h>
33 #include <scsi/scsi_device.h>
34 #include <scsi/scsi_driver.h>
35 #include <scsi/scsi_eh.h>
36 #include <scsi/scsi_common.h>
37 #include <scsi/scsi_transport.h>
38 #include <scsi/scsi_host.h>
39 #include <scsi/scsi_ioctl.h>
40 #include <scsi/scsi_dh.h>
43 #include "scsi_priv.h"
44 #include "scsi_logging.h"
45 #include "scsi_transport_api.h"
47 #include <trace/events/scsi.h>
49 #include <asm/unaligned.h>
51 static void scsi_eh_done(struct scsi_cmnd
*scmd
);
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 int 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 (atomic_read(&shost
->host_busy
) == 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)
118 * scmd_eh_abort_handler - Handle command aborts
119 * @work: command to be aborted.
122 scmd_eh_abort_handler(struct work_struct
*work
)
124 struct scsi_cmnd
*scmd
=
125 container_of(work
, struct scsi_cmnd
, abort_work
.work
);
126 struct scsi_device
*sdev
= scmd
->device
;
129 if (scsi_host_eh_past_deadline(sdev
->host
)) {
130 SCSI_LOG_ERROR_RECOVERY(3,
131 scmd_printk(KERN_INFO
, scmd
,
132 "eh timeout, not aborting\n"));
134 SCSI_LOG_ERROR_RECOVERY(3,
135 scmd_printk(KERN_INFO
, scmd
,
136 "aborting command\n"));
137 rtn
= scsi_try_to_abort_cmd(sdev
->host
->hostt
, scmd
);
138 if (rtn
== SUCCESS
) {
139 set_host_byte(scmd
, DID_TIME_OUT
);
140 if (scsi_host_eh_past_deadline(sdev
->host
)) {
141 SCSI_LOG_ERROR_RECOVERY(3,
142 scmd_printk(KERN_INFO
, scmd
,
143 "eh timeout, not retrying "
144 "aborted command\n"));
145 } else if (!scsi_noretry_cmd(scmd
) &&
146 (++scmd
->retries
<= scmd
->allowed
)) {
147 SCSI_LOG_ERROR_RECOVERY(3,
148 scmd_printk(KERN_WARNING
, scmd
,
149 "retry aborted command\n"));
150 scsi_queue_insert(scmd
, SCSI_MLQUEUE_EH_RETRY
);
153 SCSI_LOG_ERROR_RECOVERY(3,
154 scmd_printk(KERN_WARNING
, scmd
,
155 "finish aborted command\n"));
156 scsi_finish_command(scmd
);
160 SCSI_LOG_ERROR_RECOVERY(3,
161 scmd_printk(KERN_INFO
, scmd
,
163 (rtn
== FAST_IO_FAIL
) ?
164 "not send" : "failed"));
168 scsi_eh_scmd_add(scmd
);
172 * scsi_abort_command - schedule a command abort
173 * @scmd: scmd to abort.
175 * We only need to abort commands after a command timeout
178 scsi_abort_command(struct scsi_cmnd
*scmd
)
180 struct scsi_device
*sdev
= scmd
->device
;
181 struct Scsi_Host
*shost
= sdev
->host
;
184 if (scmd
->eh_eflags
& SCSI_EH_ABORT_SCHEDULED
) {
186 * Retry after abort failed, escalate to next level.
188 SCSI_LOG_ERROR_RECOVERY(3,
189 scmd_printk(KERN_INFO
, scmd
,
190 "previous abort failed\n"));
191 BUG_ON(delayed_work_pending(&scmd
->abort_work
));
195 spin_lock_irqsave(shost
->host_lock
, flags
);
196 if (shost
->eh_deadline
!= -1 && !shost
->last_reset
)
197 shost
->last_reset
= jiffies
;
198 spin_unlock_irqrestore(shost
->host_lock
, flags
);
200 scmd
->eh_eflags
|= SCSI_EH_ABORT_SCHEDULED
;
201 SCSI_LOG_ERROR_RECOVERY(3,
202 scmd_printk(KERN_INFO
, scmd
, "abort scheduled\n"));
203 queue_delayed_work(shost
->tmf_work_q
, &scmd
->abort_work
, HZ
/ 100);
208 * scsi_eh_reset - call into ->eh_action to reset internal counters
209 * @scmd: scmd to run eh on.
211 * The scsi driver might be carrying internal state about the
212 * devices, so we need to call into the driver to reset the
213 * internal state once the error handler is started.
215 static void scsi_eh_reset(struct scsi_cmnd
*scmd
)
217 if (!blk_rq_is_passthrough(scmd
->request
)) {
218 struct scsi_driver
*sdrv
= scsi_cmd_to_driver(scmd
);
220 sdrv
->eh_reset(scmd
);
224 static void scsi_eh_inc_host_failed(struct rcu_head
*head
)
226 struct scsi_cmnd
*scmd
= container_of(head
, typeof(*scmd
), rcu
);
227 struct Scsi_Host
*shost
= scmd
->device
->host
;
230 spin_lock_irqsave(shost
->host_lock
, flags
);
231 shost
->host_failed
++;
232 scsi_eh_wakeup(shost
);
233 spin_unlock_irqrestore(shost
->host_lock
, flags
);
237 * scsi_eh_scmd_add - add scsi cmd to error handling.
238 * @scmd: scmd to run eh on.
240 void scsi_eh_scmd_add(struct scsi_cmnd
*scmd
)
242 struct Scsi_Host
*shost
= scmd
->device
->host
;
246 WARN_ON_ONCE(!shost
->ehandler
);
248 spin_lock_irqsave(shost
->host_lock
, flags
);
249 if (scsi_host_set_state(shost
, SHOST_RECOVERY
)) {
250 ret
= scsi_host_set_state(shost
, SHOST_CANCEL_RECOVERY
);
253 if (shost
->eh_deadline
!= -1 && !shost
->last_reset
)
254 shost
->last_reset
= jiffies
;
257 list_add_tail(&scmd
->eh_entry
, &shost
->eh_cmd_q
);
258 spin_unlock_irqrestore(shost
->host_lock
, flags
);
260 * Ensure that all tasks observe the host state change before the
261 * host_failed change.
263 call_rcu(&scmd
->rcu
, scsi_eh_inc_host_failed
);
267 * scsi_times_out - Timeout function for normal scsi commands.
268 * @req: request that is timing out.
271 * We do not need to lock this. There is the potential for a race
272 * only in that the normal completion handling might run, but if the
273 * normal completion function determines that the timer has already
274 * fired, then it mustn't do anything.
276 enum blk_eh_timer_return
scsi_times_out(struct request
*req
)
278 struct scsi_cmnd
*scmd
= blk_mq_rq_to_pdu(req
);
279 enum blk_eh_timer_return rtn
= BLK_EH_NOT_HANDLED
;
280 struct Scsi_Host
*host
= scmd
->device
->host
;
282 trace_scsi_dispatch_cmd_timeout(scmd
);
283 scsi_log_completion(scmd
, TIMEOUT_ERROR
);
285 if (host
->eh_deadline
!= -1 && !host
->last_reset
)
286 host
->last_reset
= jiffies
;
288 if (host
->hostt
->eh_timed_out
)
289 rtn
= host
->hostt
->eh_timed_out(scmd
);
291 if (rtn
== BLK_EH_NOT_HANDLED
) {
292 if (scsi_abort_command(scmd
) != SUCCESS
) {
293 set_host_byte(scmd
, DID_TIME_OUT
);
294 scsi_eh_scmd_add(scmd
);
302 * scsi_block_when_processing_errors - Prevent cmds from being queued.
303 * @sdev: Device on which we are performing recovery.
306 * We block until the host is out of error recovery, and then check to
307 * see whether the host or the device is offline.
310 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
312 int scsi_block_when_processing_errors(struct scsi_device
*sdev
)
316 wait_event(sdev
->host
->host_wait
, !scsi_host_in_recovery(sdev
->host
));
318 online
= scsi_device_online(sdev
);
320 SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_INFO
, sdev
,
321 "%s: rtn: %d\n", __func__
, online
));
325 EXPORT_SYMBOL(scsi_block_when_processing_errors
);
327 #ifdef CONFIG_SCSI_LOGGING
329 * scsi_eh_prt_fail_stats - Log info on failures.
330 * @shost: scsi host being recovered.
331 * @work_q: Queue of scsi cmds to process.
333 static inline void scsi_eh_prt_fail_stats(struct Scsi_Host
*shost
,
334 struct list_head
*work_q
)
336 struct scsi_cmnd
*scmd
;
337 struct scsi_device
*sdev
;
338 int total_failures
= 0;
341 int devices_failed
= 0;
343 shost_for_each_device(sdev
, shost
) {
344 list_for_each_entry(scmd
, work_q
, eh_entry
) {
345 if (scmd
->device
== sdev
) {
347 if (scmd
->eh_eflags
& SCSI_EH_ABORT_SCHEDULED
)
354 if (cmd_cancel
|| cmd_failed
) {
355 SCSI_LOG_ERROR_RECOVERY(3,
356 shost_printk(KERN_INFO
, shost
,
357 "%s: cmds failed: %d, cancel: %d\n",
358 __func__
, cmd_failed
,
366 SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO
, shost
,
367 "Total of %d commands on %d"
368 " devices require eh work\n",
369 total_failures
, devices_failed
));
374 * scsi_report_lun_change - Set flag on all *other* devices on the same target
375 * to indicate that a UNIT ATTENTION is expected.
376 * @sdev: Device reporting the UNIT ATTENTION
378 static void scsi_report_lun_change(struct scsi_device
*sdev
)
380 sdev
->sdev_target
->expecting_lun_change
= 1;
384 * scsi_report_sense - Examine scsi sense information and log messages for
385 * certain conditions, also issue uevents for some of them.
386 * @sdev: Device reporting the sense code
387 * @sshdr: sshdr to be examined
389 static void scsi_report_sense(struct scsi_device
*sdev
,
390 struct scsi_sense_hdr
*sshdr
)
392 enum scsi_device_event evt_type
= SDEV_EVT_MAXBITS
; /* i.e. none */
394 if (sshdr
->sense_key
== UNIT_ATTENTION
) {
395 if (sshdr
->asc
== 0x3f && sshdr
->ascq
== 0x03) {
396 evt_type
= SDEV_EVT_INQUIRY_CHANGE_REPORTED
;
397 sdev_printk(KERN_WARNING
, sdev
,
398 "Inquiry data has changed");
399 } else if (sshdr
->asc
== 0x3f && sshdr
->ascq
== 0x0e) {
400 evt_type
= SDEV_EVT_LUN_CHANGE_REPORTED
;
401 scsi_report_lun_change(sdev
);
402 sdev_printk(KERN_WARNING
, sdev
,
403 "Warning! Received an indication that the "
404 "LUN assignments on this target have "
405 "changed. The Linux SCSI layer does not "
406 "automatically remap LUN assignments.\n");
407 } else if (sshdr
->asc
== 0x3f)
408 sdev_printk(KERN_WARNING
, sdev
,
409 "Warning! Received an indication that the "
410 "operating parameters on this target have "
411 "changed. The Linux SCSI layer does not "
412 "automatically adjust these parameters.\n");
414 if (sshdr
->asc
== 0x38 && sshdr
->ascq
== 0x07) {
415 evt_type
= SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED
;
416 sdev_printk(KERN_WARNING
, sdev
,
417 "Warning! Received an indication that the "
418 "LUN reached a thin provisioning soft "
422 if (sshdr
->asc
== 0x29) {
423 evt_type
= SDEV_EVT_POWER_ON_RESET_OCCURRED
;
424 sdev_printk(KERN_WARNING
, sdev
,
425 "Power-on or device reset occurred\n");
428 if (sshdr
->asc
== 0x2a && sshdr
->ascq
== 0x01) {
429 evt_type
= SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED
;
430 sdev_printk(KERN_WARNING
, sdev
,
431 "Mode parameters changed");
432 } else if (sshdr
->asc
== 0x2a && sshdr
->ascq
== 0x06) {
433 evt_type
= SDEV_EVT_ALUA_STATE_CHANGE_REPORTED
;
434 sdev_printk(KERN_WARNING
, sdev
,
435 "Asymmetric access state changed");
436 } else if (sshdr
->asc
== 0x2a && sshdr
->ascq
== 0x09) {
437 evt_type
= SDEV_EVT_CAPACITY_CHANGE_REPORTED
;
438 sdev_printk(KERN_WARNING
, sdev
,
439 "Capacity data has changed");
440 } else if (sshdr
->asc
== 0x2a)
441 sdev_printk(KERN_WARNING
, sdev
,
442 "Parameters changed");
445 if (evt_type
!= SDEV_EVT_MAXBITS
) {
446 set_bit(evt_type
, sdev
->pending_events
);
447 schedule_work(&sdev
->event_work
);
452 * scsi_check_sense - Examine scsi cmd sense
453 * @scmd: Cmd to have sense checked.
456 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
459 * When a deferred error is detected the current command has
460 * not been executed and needs retrying.
462 int scsi_check_sense(struct scsi_cmnd
*scmd
)
464 struct scsi_device
*sdev
= scmd
->device
;
465 struct scsi_sense_hdr sshdr
;
467 if (! scsi_command_normalize_sense(scmd
, &sshdr
))
468 return FAILED
; /* no valid sense data */
470 scsi_report_sense(sdev
, &sshdr
);
472 if (scsi_sense_is_deferred(&sshdr
))
475 if (sdev
->handler
&& sdev
->handler
->check_sense
) {
478 rc
= sdev
->handler
->check_sense(sdev
, &sshdr
);
479 if (rc
!= SCSI_RETURN_NOT_HANDLED
)
481 /* handler does not care. Drop down to default handling */
484 if (scmd
->cmnd
[0] == TEST_UNIT_READY
&& scmd
->scsi_done
!= scsi_eh_done
)
486 * nasty: for mid-layer issued TURs, we need to return the
487 * actual sense data without any recovery attempt. For eh
488 * issued ones, we need to try to recover and interpret
493 * Previous logic looked for FILEMARK, EOM or ILI which are
494 * mainly associated with tapes and returned SUCCESS.
496 if (sshdr
.response_code
== 0x70) {
498 if (scmd
->sense_buffer
[2] & 0xe0)
502 * descriptor format: look for "stream commands sense data
503 * descriptor" (see SSC-3). Assume single sense data
504 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
506 if ((sshdr
.additional_length
> 3) &&
507 (scmd
->sense_buffer
[8] == 0x4) &&
508 (scmd
->sense_buffer
[11] & 0xe0))
512 switch (sshdr
.sense_key
) {
515 case RECOVERED_ERROR
:
516 return /* soft_error */ SUCCESS
;
518 case ABORTED_COMMAND
:
519 if (sshdr
.asc
== 0x10) /* DIF */
526 * if we are expecting a cc/ua because of a bus reset that we
527 * performed, treat this just as a retry. otherwise this is
528 * information that we should pass up to the upper-level driver
529 * so that we can deal with it there.
531 if (scmd
->device
->expecting_cc_ua
) {
533 * Because some device does not queue unit
534 * attentions correctly, we carefully check
535 * additional sense code and qualifier so as
536 * not to squash media change unit attention.
538 if (sshdr
.asc
!= 0x28 || sshdr
.ascq
!= 0x00) {
539 scmd
->device
->expecting_cc_ua
= 0;
544 * we might also expect a cc/ua if another LUN on the target
545 * reported a UA with an ASC/ASCQ of 3F 0E -
546 * REPORTED LUNS DATA HAS CHANGED.
548 if (scmd
->device
->sdev_target
->expecting_lun_change
&&
549 sshdr
.asc
== 0x3f && sshdr
.ascq
== 0x0e)
552 * if the device is in the process of becoming ready, we
555 if ((sshdr
.asc
== 0x04) && (sshdr
.ascq
== 0x01))
558 * if the device is not started, we need to wake
559 * the error handler to start the motor
561 if (scmd
->device
->allow_restart
&&
562 (sshdr
.asc
== 0x04) && (sshdr
.ascq
== 0x02))
565 * Pass the UA upwards for a determination in the completion
570 /* these are not supported */
572 if (sshdr
.asc
== 0x27 && sshdr
.ascq
== 0x07) {
573 /* Thin provisioning hard threshold reached */
574 set_host_byte(scmd
, DID_ALLOC_FAILURE
);
579 case VOLUME_OVERFLOW
:
582 set_host_byte(scmd
, DID_TARGET_FAILURE
);
586 if (sshdr
.asc
== 0x11 || /* UNRECOVERED READ ERR */
587 sshdr
.asc
== 0x13 || /* AMNF DATA FIELD */
588 sshdr
.asc
== 0x14) { /* RECORD NOT FOUND */
589 set_host_byte(scmd
, DID_MEDIUM_ERROR
);
595 if (scmd
->device
->retry_hwerror
)
596 return ADD_TO_MLQUEUE
;
598 set_host_byte(scmd
, DID_TARGET_FAILURE
);
601 case ILLEGAL_REQUEST
:
602 if (sshdr
.asc
== 0x20 || /* Invalid command operation code */
603 sshdr
.asc
== 0x21 || /* Logical block address out of range */
604 sshdr
.asc
== 0x22 || /* Invalid function */
605 sshdr
.asc
== 0x24 || /* Invalid field in cdb */
606 sshdr
.asc
== 0x26 || /* Parameter value invalid */
607 sshdr
.asc
== 0x27) { /* Write protected */
608 set_host_byte(scmd
, DID_TARGET_FAILURE
);
616 EXPORT_SYMBOL_GPL(scsi_check_sense
);
618 static void scsi_handle_queue_ramp_up(struct scsi_device
*sdev
)
620 struct scsi_host_template
*sht
= sdev
->host
->hostt
;
621 struct scsi_device
*tmp_sdev
;
623 if (!sht
->track_queue_depth
||
624 sdev
->queue_depth
>= sdev
->max_queue_depth
)
627 if (time_before(jiffies
,
628 sdev
->last_queue_ramp_up
+ sdev
->queue_ramp_up_period
))
631 if (time_before(jiffies
,
632 sdev
->last_queue_full_time
+ sdev
->queue_ramp_up_period
))
636 * Walk all devices of a target and do
639 shost_for_each_device(tmp_sdev
, sdev
->host
) {
640 if (tmp_sdev
->channel
!= sdev
->channel
||
641 tmp_sdev
->id
!= sdev
->id
||
642 tmp_sdev
->queue_depth
== sdev
->max_queue_depth
)
645 scsi_change_queue_depth(tmp_sdev
, tmp_sdev
->queue_depth
+ 1);
646 sdev
->last_queue_ramp_up
= jiffies
;
650 static void scsi_handle_queue_full(struct scsi_device
*sdev
)
652 struct scsi_host_template
*sht
= sdev
->host
->hostt
;
653 struct scsi_device
*tmp_sdev
;
655 if (!sht
->track_queue_depth
)
658 shost_for_each_device(tmp_sdev
, sdev
->host
) {
659 if (tmp_sdev
->channel
!= sdev
->channel
||
660 tmp_sdev
->id
!= sdev
->id
)
663 * We do not know the number of commands that were at
664 * the device when we got the queue full so we start
665 * from the highest possible value and work our way down.
667 scsi_track_queue_full(tmp_sdev
, tmp_sdev
->queue_depth
- 1);
672 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
673 * @scmd: SCSI cmd to examine.
676 * This is *only* called when we are examining the status of commands
677 * queued during error recovery. the main difference here is that we
678 * don't allow for the possibility of retries here, and we are a lot
679 * more restrictive about what we consider acceptable.
681 static int scsi_eh_completed_normally(struct scsi_cmnd
*scmd
)
684 * first check the host byte, to see if there is anything in there
685 * that would indicate what we need to do.
687 if (host_byte(scmd
->result
) == DID_RESET
) {
689 * rats. we are already in the error handler, so we now
690 * get to try and figure out what to do next. if the sense
691 * is valid, we have a pretty good idea of what to do.
692 * if not, we mark it as FAILED.
694 return scsi_check_sense(scmd
);
696 if (host_byte(scmd
->result
) != DID_OK
)
700 * next, check the message byte.
702 if (msg_byte(scmd
->result
) != COMMAND_COMPLETE
)
706 * now, check the status byte to see if this indicates
709 switch (status_byte(scmd
->result
)) {
711 scsi_handle_queue_ramp_up(scmd
->device
);
713 case COMMAND_TERMINATED
:
715 case CHECK_CONDITION
:
716 return scsi_check_sense(scmd
);
718 case INTERMEDIATE_GOOD
:
719 case INTERMEDIATE_C_GOOD
:
721 * who knows? FIXME(eric)
724 case RESERVATION_CONFLICT
:
725 if (scmd
->cmnd
[0] == TEST_UNIT_READY
)
726 /* it is a success, we probed the device and
729 /* otherwise, we failed to send the command */
732 scsi_handle_queue_full(scmd
->device
);
743 * scsi_eh_done - Completion function for error handling.
744 * @scmd: Cmd that is done.
746 static void scsi_eh_done(struct scsi_cmnd
*scmd
)
748 struct completion
*eh_action
;
750 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO
, scmd
,
751 "%s result: %x\n", __func__
, scmd
->result
));
753 eh_action
= scmd
->device
->host
->eh_action
;
759 * scsi_try_host_reset - ask host adapter to reset itself
760 * @scmd: SCSI cmd to send host reset.
762 static int scsi_try_host_reset(struct scsi_cmnd
*scmd
)
766 struct Scsi_Host
*host
= scmd
->device
->host
;
767 struct scsi_host_template
*hostt
= host
->hostt
;
769 SCSI_LOG_ERROR_RECOVERY(3,
770 shost_printk(KERN_INFO
, host
, "Snd Host RST\n"));
772 if (!hostt
->eh_host_reset_handler
)
775 rtn
= hostt
->eh_host_reset_handler(scmd
);
777 if (rtn
== SUCCESS
) {
778 if (!hostt
->skip_settle_delay
)
779 ssleep(HOST_RESET_SETTLE_TIME
);
780 spin_lock_irqsave(host
->host_lock
, flags
);
781 scsi_report_bus_reset(host
, scmd_channel(scmd
));
782 spin_unlock_irqrestore(host
->host_lock
, flags
);
789 * scsi_try_bus_reset - ask host to perform a bus reset
790 * @scmd: SCSI cmd to send bus reset.
792 static int scsi_try_bus_reset(struct scsi_cmnd
*scmd
)
796 struct Scsi_Host
*host
= scmd
->device
->host
;
797 struct scsi_host_template
*hostt
= host
->hostt
;
799 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO
, scmd
,
800 "%s: Snd Bus RST\n", __func__
));
802 if (!hostt
->eh_bus_reset_handler
)
805 rtn
= hostt
->eh_bus_reset_handler(scmd
);
807 if (rtn
== SUCCESS
) {
808 if (!hostt
->skip_settle_delay
)
809 ssleep(BUS_RESET_SETTLE_TIME
);
810 spin_lock_irqsave(host
->host_lock
, flags
);
811 scsi_report_bus_reset(host
, scmd_channel(scmd
));
812 spin_unlock_irqrestore(host
->host_lock
, flags
);
818 static void __scsi_report_device_reset(struct scsi_device
*sdev
, void *data
)
821 sdev
->expecting_cc_ua
= 1;
825 * scsi_try_target_reset - Ask host to perform a target reset
826 * @scmd: SCSI cmd used to send a target reset
829 * There is no timeout for this operation. if this operation is
830 * unreliable for a given host, then the host itself needs to put a
831 * timer on it, and set the host back to a consistent state prior to
834 static int scsi_try_target_reset(struct scsi_cmnd
*scmd
)
838 struct Scsi_Host
*host
= scmd
->device
->host
;
839 struct scsi_host_template
*hostt
= host
->hostt
;
841 if (!hostt
->eh_target_reset_handler
)
844 rtn
= hostt
->eh_target_reset_handler(scmd
);
845 if (rtn
== SUCCESS
) {
846 spin_lock_irqsave(host
->host_lock
, flags
);
847 __starget_for_each_device(scsi_target(scmd
->device
), NULL
,
848 __scsi_report_device_reset
);
849 spin_unlock_irqrestore(host
->host_lock
, flags
);
856 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
857 * @scmd: SCSI cmd used to send BDR
860 * There is no timeout for this operation. if this operation is
861 * unreliable for a given host, then the host itself needs to put a
862 * timer on it, and set the host back to a consistent state prior to
865 static int scsi_try_bus_device_reset(struct scsi_cmnd
*scmd
)
868 struct scsi_host_template
*hostt
= scmd
->device
->host
->hostt
;
870 if (!hostt
->eh_device_reset_handler
)
873 rtn
= hostt
->eh_device_reset_handler(scmd
);
875 __scsi_report_device_reset(scmd
->device
, NULL
);
880 * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
881 * @hostt: SCSI driver host template
882 * @scmd: SCSI cmd used to send a target reset
885 * SUCCESS, FAILED, or FAST_IO_FAIL
888 * SUCCESS does not necessarily indicate that the command
889 * has been aborted; it only indicates that the LLDDs
890 * has cleared all references to that command.
891 * LLDDs should return FAILED only if an abort was required
892 * but could not be executed. LLDDs should return FAST_IO_FAIL
893 * if the device is temporarily unavailable (eg due to a
894 * link down on FibreChannel)
896 static int scsi_try_to_abort_cmd(struct scsi_host_template
*hostt
,
897 struct scsi_cmnd
*scmd
)
899 if (!hostt
->eh_abort_handler
)
902 return hostt
->eh_abort_handler(scmd
);
905 static void scsi_abort_eh_cmnd(struct scsi_cmnd
*scmd
)
907 if (scsi_try_to_abort_cmd(scmd
->device
->host
->hostt
, scmd
) != SUCCESS
)
908 if (scsi_try_bus_device_reset(scmd
) != SUCCESS
)
909 if (scsi_try_target_reset(scmd
) != SUCCESS
)
910 if (scsi_try_bus_reset(scmd
) != SUCCESS
)
911 scsi_try_host_reset(scmd
);
915 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
916 * @scmd: SCSI command structure to hijack
917 * @ses: structure to save restore information
918 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
919 * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
920 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
922 * This function is used to save a scsi command information before re-execution
923 * as part of the error recovery process. If @sense_bytes is 0 the command
924 * sent must be one that does not transfer any data. If @sense_bytes != 0
925 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
926 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
928 void scsi_eh_prep_cmnd(struct scsi_cmnd
*scmd
, struct scsi_eh_save
*ses
,
929 unsigned char *cmnd
, int cmnd_size
, unsigned sense_bytes
)
931 struct scsi_device
*sdev
= scmd
->device
;
934 * We need saved copies of a number of fields - this is because
935 * error handling may need to overwrite these with different values
936 * to run different commands, and once error handling is complete,
937 * we will need to restore these values prior to running the actual
940 ses
->cmd_len
= scmd
->cmd_len
;
941 ses
->cmnd
= scmd
->cmnd
;
942 ses
->data_direction
= scmd
->sc_data_direction
;
943 ses
->sdb
= scmd
->sdb
;
944 ses
->next_rq
= scmd
->request
->next_rq
;
945 ses
->result
= scmd
->result
;
946 ses
->underflow
= scmd
->underflow
;
947 ses
->prot_op
= scmd
->prot_op
;
948 ses
->eh_eflags
= scmd
->eh_eflags
;
950 scmd
->prot_op
= SCSI_PROT_NORMAL
;
952 scmd
->cmnd
= ses
->eh_cmnd
;
953 memset(scmd
->cmnd
, 0, BLK_MAX_CDB
);
954 memset(&scmd
->sdb
, 0, sizeof(scmd
->sdb
));
955 scmd
->request
->next_rq
= NULL
;
959 scmd
->sdb
.length
= min_t(unsigned, SCSI_SENSE_BUFFERSIZE
,
961 sg_init_one(&ses
->sense_sgl
, scmd
->sense_buffer
,
963 scmd
->sdb
.table
.sgl
= &ses
->sense_sgl
;
964 scmd
->sc_data_direction
= DMA_FROM_DEVICE
;
965 scmd
->sdb
.table
.nents
= scmd
->sdb
.table
.orig_nents
= 1;
966 scmd
->cmnd
[0] = REQUEST_SENSE
;
967 scmd
->cmnd
[4] = scmd
->sdb
.length
;
968 scmd
->cmd_len
= COMMAND_SIZE(scmd
->cmnd
[0]);
970 scmd
->sc_data_direction
= DMA_NONE
;
972 BUG_ON(cmnd_size
> BLK_MAX_CDB
);
973 memcpy(scmd
->cmnd
, cmnd
, cmnd_size
);
974 scmd
->cmd_len
= COMMAND_SIZE(scmd
->cmnd
[0]);
980 if (sdev
->scsi_level
<= SCSI_2
&& sdev
->scsi_level
!= SCSI_UNKNOWN
)
981 scmd
->cmnd
[1] = (scmd
->cmnd
[1] & 0x1f) |
982 (sdev
->lun
<< 5 & 0xe0);
985 * Zero the sense buffer. The scsi spec mandates that any
986 * untransferred sense data should be interpreted as being zero.
988 memset(scmd
->sense_buffer
, 0, SCSI_SENSE_BUFFERSIZE
);
990 EXPORT_SYMBOL(scsi_eh_prep_cmnd
);
993 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
994 * @scmd: SCSI command structure to restore
995 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
997 * Undo any damage done by above scsi_eh_prep_cmnd().
999 void scsi_eh_restore_cmnd(struct scsi_cmnd
* scmd
, struct scsi_eh_save
*ses
)
1002 * Restore original data
1004 scmd
->cmd_len
= ses
->cmd_len
;
1005 scmd
->cmnd
= ses
->cmnd
;
1006 scmd
->sc_data_direction
= ses
->data_direction
;
1007 scmd
->sdb
= ses
->sdb
;
1008 scmd
->request
->next_rq
= ses
->next_rq
;
1009 scmd
->result
= ses
->result
;
1010 scmd
->underflow
= ses
->underflow
;
1011 scmd
->prot_op
= ses
->prot_op
;
1012 scmd
->eh_eflags
= ses
->eh_eflags
;
1014 EXPORT_SYMBOL(scsi_eh_restore_cmnd
);
1017 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
1018 * @scmd: SCSI command structure to hijack
1019 * @cmnd: CDB to send
1020 * @cmnd_size: size in bytes of @cmnd
1021 * @timeout: timeout for this request
1022 * @sense_bytes: size of sense data to copy or 0
1024 * This function is used to send a scsi command down to a target device
1025 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
1028 * SUCCESS or FAILED or NEEDS_RETRY
1030 static int scsi_send_eh_cmnd(struct scsi_cmnd
*scmd
, unsigned char *cmnd
,
1031 int cmnd_size
, int timeout
, unsigned sense_bytes
)
1033 struct scsi_device
*sdev
= scmd
->device
;
1034 struct Scsi_Host
*shost
= sdev
->host
;
1035 DECLARE_COMPLETION_ONSTACK(done
);
1036 unsigned long timeleft
= timeout
;
1037 struct scsi_eh_save ses
;
1038 const unsigned long stall_for
= msecs_to_jiffies(100);
1042 scsi_eh_prep_cmnd(scmd
, &ses
, cmnd
, cmnd_size
, sense_bytes
);
1043 shost
->eh_action
= &done
;
1045 scsi_log_send(scmd
);
1046 scmd
->scsi_done
= scsi_eh_done
;
1047 rtn
= shost
->hostt
->queuecommand(shost
, scmd
);
1049 if (timeleft
> stall_for
) {
1050 scsi_eh_restore_cmnd(scmd
, &ses
);
1051 timeleft
-= stall_for
;
1052 msleep(jiffies_to_msecs(stall_for
));
1055 /* signal not to enter either branch of the if () below */
1059 timeleft
= wait_for_completion_timeout(&done
, timeout
);
1063 shost
->eh_action
= NULL
;
1065 scsi_log_completion(scmd
, rtn
);
1067 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO
, scmd
,
1068 "%s timeleft: %ld\n",
1069 __func__
, timeleft
));
1072 * If there is time left scsi_eh_done got called, and we will examine
1073 * the actual status codes to see whether the command actually did
1074 * complete normally, else if we have a zero return and no time left,
1075 * the command must still be pending, so abort it and return FAILED.
1076 * If we never actually managed to issue the command, because
1077 * ->queuecommand() kept returning non zero, use the rtn = FAILED
1078 * value above (so don't execute either branch of the if)
1081 rtn
= scsi_eh_completed_normally(scmd
);
1082 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO
, scmd
,
1083 "%s: scsi_eh_completed_normally %x\n", __func__
, rtn
));
1090 case ADD_TO_MLQUEUE
:
1097 } else if (rtn
!= FAILED
) {
1098 scsi_abort_eh_cmnd(scmd
);
1102 scsi_eh_restore_cmnd(scmd
, &ses
);
1108 * scsi_request_sense - Request sense data from a particular target.
1109 * @scmd: SCSI cmd for request sense.
1112 * Some hosts automatically obtain this information, others require
1113 * that we obtain it on our own. This function will *not* return until
1114 * the command either times out, or it completes.
1116 static int scsi_request_sense(struct scsi_cmnd
*scmd
)
1118 return scsi_send_eh_cmnd(scmd
, NULL
, 0, scmd
->device
->eh_timeout
, ~0);
1121 static int scsi_eh_action(struct scsi_cmnd
*scmd
, int rtn
)
1123 if (!blk_rq_is_passthrough(scmd
->request
)) {
1124 struct scsi_driver
*sdrv
= scsi_cmd_to_driver(scmd
);
1125 if (sdrv
->eh_action
)
1126 rtn
= sdrv
->eh_action(scmd
, rtn
);
1132 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
1133 * @scmd: Original SCSI cmd that eh has finished.
1134 * @done_q: Queue for processed commands.
1137 * We don't want to use the normal command completion while we are are
1138 * still handling errors - it may cause other commands to be queued,
1139 * and that would disturb what we are doing. Thus we really want to
1140 * keep a list of pending commands for final completion, and once we
1141 * are ready to leave error handling we handle completion for real.
1143 void scsi_eh_finish_cmd(struct scsi_cmnd
*scmd
, struct list_head
*done_q
)
1145 list_move_tail(&scmd
->eh_entry
, done_q
);
1147 EXPORT_SYMBOL(scsi_eh_finish_cmd
);
1150 * scsi_eh_get_sense - Get device sense data.
1151 * @work_q: Queue of commands to process.
1152 * @done_q: Queue of processed commands.
1155 * See if we need to request sense information. if so, then get it
1156 * now, so we have a better idea of what to do.
1159 * This has the unfortunate side effect that if a shost adapter does
1160 * not automatically request sense information, we end up shutting
1161 * it down before we request it.
1163 * All drivers should request sense information internally these days,
1164 * so for now all I have to say is tough noogies if you end up in here.
1166 * XXX: Long term this code should go away, but that needs an audit of
1169 int scsi_eh_get_sense(struct list_head
*work_q
,
1170 struct list_head
*done_q
)
1172 struct scsi_cmnd
*scmd
, *next
;
1173 struct Scsi_Host
*shost
;
1177 * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
1178 * should not get sense.
1180 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1181 if ((scmd
->eh_eflags
& SCSI_EH_ABORT_SCHEDULED
) ||
1182 SCSI_SENSE_VALID(scmd
))
1185 shost
= scmd
->device
->host
;
1186 if (scsi_host_eh_past_deadline(shost
)) {
1187 SCSI_LOG_ERROR_RECOVERY(3,
1188 scmd_printk(KERN_INFO
, scmd
,
1189 "%s: skip request sense, past eh deadline\n",
1193 if (status_byte(scmd
->result
) != CHECK_CONDITION
)
1195 * don't request sense if there's no check condition
1196 * status because the error we're processing isn't one
1197 * that has a sense code (and some devices get
1198 * confused by sense requests out of the blue)
1202 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO
, scmd
,
1203 "%s: requesting sense\n",
1205 rtn
= scsi_request_sense(scmd
);
1209 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO
, scmd
,
1210 "sense requested, result %x\n", scmd
->result
));
1211 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd
));
1213 rtn
= scsi_decide_disposition(scmd
);
1216 * if the result was normal, then just pass it along to the
1220 /* we don't want this command reissued, just
1221 * finished with the sense data, so set
1222 * retries to the max allowed to ensure it
1223 * won't get reissued */
1224 scmd
->retries
= scmd
->allowed
;
1225 else if (rtn
!= NEEDS_RETRY
)
1228 scsi_eh_finish_cmd(scmd
, done_q
);
1231 return list_empty(work_q
);
1233 EXPORT_SYMBOL_GPL(scsi_eh_get_sense
);
1236 * scsi_eh_tur - Send TUR to device.
1237 * @scmd: &scsi_cmnd to send TUR
1240 * 0 - Device is ready. 1 - Device NOT ready.
1242 static int scsi_eh_tur(struct scsi_cmnd
*scmd
)
1244 static unsigned char tur_command
[6] = {TEST_UNIT_READY
, 0, 0, 0, 0, 0};
1245 int retry_cnt
= 1, rtn
;
1248 rtn
= scsi_send_eh_cmnd(scmd
, tur_command
, 6,
1249 scmd
->device
->eh_timeout
, 0);
1251 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO
, scmd
,
1252 "%s return: %x\n", __func__
, rtn
));
1267 * scsi_eh_test_devices - check if devices are responding from error recovery.
1268 * @cmd_list: scsi commands in error recovery.
1269 * @work_q: queue for commands which still need more error recovery
1270 * @done_q: queue for commands which are finished
1271 * @try_stu: boolean on if a STU command should be tried in addition to TUR.
1274 * Tests if devices are in a working state. Commands to devices now in
1275 * a working state are sent to the done_q while commands to devices which
1276 * are still failing to respond are returned to the work_q for more
1279 static int scsi_eh_test_devices(struct list_head
*cmd_list
,
1280 struct list_head
*work_q
,
1281 struct list_head
*done_q
, int try_stu
)
1283 struct scsi_cmnd
*scmd
, *next
;
1284 struct scsi_device
*sdev
;
1287 while (!list_empty(cmd_list
)) {
1288 scmd
= list_entry(cmd_list
->next
, struct scsi_cmnd
, eh_entry
);
1289 sdev
= scmd
->device
;
1292 if (scsi_host_eh_past_deadline(sdev
->host
)) {
1293 /* Push items back onto work_q */
1294 list_splice_init(cmd_list
, work_q
);
1295 SCSI_LOG_ERROR_RECOVERY(3,
1296 sdev_printk(KERN_INFO
, sdev
,
1297 "%s: skip test device, past eh deadline",
1303 finish_cmds
= !scsi_device_online(scmd
->device
) ||
1304 (try_stu
&& !scsi_eh_try_stu(scmd
) &&
1305 !scsi_eh_tur(scmd
)) ||
1308 list_for_each_entry_safe(scmd
, next
, cmd_list
, eh_entry
)
1309 if (scmd
->device
== sdev
) {
1312 scsi_eh_action(scmd
, SUCCESS
) == SUCCESS
))
1313 scsi_eh_finish_cmd(scmd
, done_q
);
1315 list_move_tail(&scmd
->eh_entry
, work_q
);
1318 return list_empty(work_q
);
1322 * scsi_eh_try_stu - Send START_UNIT to device.
1323 * @scmd: &scsi_cmnd to send START_UNIT
1326 * 0 - Device is ready. 1 - Device NOT ready.
1328 static int scsi_eh_try_stu(struct scsi_cmnd
*scmd
)
1330 static unsigned char stu_command
[6] = {START_STOP
, 0, 0, 0, 1, 0};
1332 if (scmd
->device
->allow_restart
) {
1333 int i
, rtn
= NEEDS_RETRY
;
1335 for (i
= 0; rtn
== NEEDS_RETRY
&& i
< 2; i
++)
1336 rtn
= scsi_send_eh_cmnd(scmd
, stu_command
, 6, scmd
->device
->request_queue
->rq_timeout
, 0);
1346 * scsi_eh_stu - send START_UNIT if needed
1347 * @shost: &scsi host being recovered.
1348 * @work_q: &list_head for pending commands.
1349 * @done_q: &list_head for processed commands.
1352 * If commands are failing due to not ready, initializing command required,
1353 * try revalidating the device, which will end up sending a start unit.
1355 static int scsi_eh_stu(struct Scsi_Host
*shost
,
1356 struct list_head
*work_q
,
1357 struct list_head
*done_q
)
1359 struct scsi_cmnd
*scmd
, *stu_scmd
, *next
;
1360 struct scsi_device
*sdev
;
1362 shost_for_each_device(sdev
, shost
) {
1363 if (scsi_host_eh_past_deadline(shost
)) {
1364 SCSI_LOG_ERROR_RECOVERY(3,
1365 sdev_printk(KERN_INFO
, sdev
,
1366 "%s: skip START_UNIT, past eh deadline\n",
1371 list_for_each_entry(scmd
, work_q
, eh_entry
)
1372 if (scmd
->device
== sdev
&& SCSI_SENSE_VALID(scmd
) &&
1373 scsi_check_sense(scmd
) == FAILED
) {
1381 SCSI_LOG_ERROR_RECOVERY(3,
1382 sdev_printk(KERN_INFO
, sdev
,
1383 "%s: Sending START_UNIT\n",
1386 if (!scsi_eh_try_stu(stu_scmd
)) {
1387 if (!scsi_device_online(sdev
) ||
1388 !scsi_eh_tur(stu_scmd
)) {
1389 list_for_each_entry_safe(scmd
, next
,
1391 if (scmd
->device
== sdev
&&
1392 scsi_eh_action(scmd
, SUCCESS
) == SUCCESS
)
1393 scsi_eh_finish_cmd(scmd
, done_q
);
1397 SCSI_LOG_ERROR_RECOVERY(3,
1398 sdev_printk(KERN_INFO
, sdev
,
1399 "%s: START_UNIT failed\n",
1404 return list_empty(work_q
);
1409 * scsi_eh_bus_device_reset - send bdr if needed
1410 * @shost: scsi host being recovered.
1411 * @work_q: &list_head for pending commands.
1412 * @done_q: &list_head for processed commands.
1415 * Try a bus device reset. Still, look to see whether we have multiple
1416 * devices that are jammed or not - if we have multiple devices, it
1417 * makes no sense to try bus_device_reset - we really would need to try
1418 * a bus_reset instead.
1420 static int scsi_eh_bus_device_reset(struct Scsi_Host
*shost
,
1421 struct list_head
*work_q
,
1422 struct list_head
*done_q
)
1424 struct scsi_cmnd
*scmd
, *bdr_scmd
, *next
;
1425 struct scsi_device
*sdev
;
1428 shost_for_each_device(sdev
, shost
) {
1429 if (scsi_host_eh_past_deadline(shost
)) {
1430 SCSI_LOG_ERROR_RECOVERY(3,
1431 sdev_printk(KERN_INFO
, sdev
,
1432 "%s: skip BDR, past eh deadline\n",
1437 list_for_each_entry(scmd
, work_q
, eh_entry
)
1438 if (scmd
->device
== sdev
) {
1446 SCSI_LOG_ERROR_RECOVERY(3,
1447 sdev_printk(KERN_INFO
, sdev
,
1448 "%s: Sending BDR\n", current
->comm
));
1449 rtn
= scsi_try_bus_device_reset(bdr_scmd
);
1450 if (rtn
== SUCCESS
|| rtn
== FAST_IO_FAIL
) {
1451 if (!scsi_device_online(sdev
) ||
1452 rtn
== FAST_IO_FAIL
||
1453 !scsi_eh_tur(bdr_scmd
)) {
1454 list_for_each_entry_safe(scmd
, next
,
1456 if (scmd
->device
== sdev
&&
1457 scsi_eh_action(scmd
, rtn
) != FAILED
)
1458 scsi_eh_finish_cmd(scmd
,
1463 SCSI_LOG_ERROR_RECOVERY(3,
1464 sdev_printk(KERN_INFO
, sdev
,
1465 "%s: BDR failed\n", current
->comm
));
1469 return list_empty(work_q
);
1473 * scsi_eh_target_reset - send target reset if needed
1474 * @shost: scsi host being recovered.
1475 * @work_q: &list_head for pending commands.
1476 * @done_q: &list_head for processed commands.
1479 * Try a target reset.
1481 static int scsi_eh_target_reset(struct Scsi_Host
*shost
,
1482 struct list_head
*work_q
,
1483 struct list_head
*done_q
)
1485 LIST_HEAD(tmp_list
);
1486 LIST_HEAD(check_list
);
1488 list_splice_init(work_q
, &tmp_list
);
1490 while (!list_empty(&tmp_list
)) {
1491 struct scsi_cmnd
*next
, *scmd
;
1495 if (scsi_host_eh_past_deadline(shost
)) {
1496 /* push back on work queue for further processing */
1497 list_splice_init(&check_list
, work_q
);
1498 list_splice_init(&tmp_list
, work_q
);
1499 SCSI_LOG_ERROR_RECOVERY(3,
1500 shost_printk(KERN_INFO
, shost
,
1501 "%s: Skip target reset, past eh deadline\n",
1503 return list_empty(work_q
);
1506 scmd
= list_entry(tmp_list
.next
, struct scsi_cmnd
, eh_entry
);
1509 SCSI_LOG_ERROR_RECOVERY(3,
1510 shost_printk(KERN_INFO
, shost
,
1511 "%s: Sending target reset to target %d\n",
1512 current
->comm
, id
));
1513 rtn
= scsi_try_target_reset(scmd
);
1514 if (rtn
!= SUCCESS
&& rtn
!= FAST_IO_FAIL
)
1515 SCSI_LOG_ERROR_RECOVERY(3,
1516 shost_printk(KERN_INFO
, shost
,
1517 "%s: Target reset failed"
1519 current
->comm
, id
));
1520 list_for_each_entry_safe(scmd
, next
, &tmp_list
, eh_entry
) {
1521 if (scmd_id(scmd
) != id
)
1525 list_move_tail(&scmd
->eh_entry
, &check_list
);
1526 else if (rtn
== FAST_IO_FAIL
)
1527 scsi_eh_finish_cmd(scmd
, done_q
);
1529 /* push back on work queue for further processing */
1530 list_move(&scmd
->eh_entry
, work_q
);
1534 return scsi_eh_test_devices(&check_list
, work_q
, done_q
, 0);
1538 * scsi_eh_bus_reset - send a bus reset
1539 * @shost: &scsi host being recovered.
1540 * @work_q: &list_head for pending commands.
1541 * @done_q: &list_head for processed commands.
1543 static int scsi_eh_bus_reset(struct Scsi_Host
*shost
,
1544 struct list_head
*work_q
,
1545 struct list_head
*done_q
)
1547 struct scsi_cmnd
*scmd
, *chan_scmd
, *next
;
1548 LIST_HEAD(check_list
);
1549 unsigned int channel
;
1553 * we really want to loop over the various channels, and do this on
1554 * a channel by channel basis. we should also check to see if any
1555 * of the failed commands are on soft_reset devices, and if so, skip
1559 for (channel
= 0; channel
<= shost
->max_channel
; channel
++) {
1560 if (scsi_host_eh_past_deadline(shost
)) {
1561 list_splice_init(&check_list
, work_q
);
1562 SCSI_LOG_ERROR_RECOVERY(3,
1563 shost_printk(KERN_INFO
, shost
,
1564 "%s: skip BRST, past eh deadline\n",
1566 return list_empty(work_q
);
1570 list_for_each_entry(scmd
, work_q
, eh_entry
) {
1571 if (channel
== scmd_channel(scmd
)) {
1575 * FIXME add back in some support for
1576 * soft_reset devices.
1583 SCSI_LOG_ERROR_RECOVERY(3,
1584 shost_printk(KERN_INFO
, shost
,
1585 "%s: Sending BRST chan: %d\n",
1586 current
->comm
, channel
));
1587 rtn
= scsi_try_bus_reset(chan_scmd
);
1588 if (rtn
== SUCCESS
|| rtn
== FAST_IO_FAIL
) {
1589 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1590 if (channel
== scmd_channel(scmd
)) {
1591 if (rtn
== FAST_IO_FAIL
)
1592 scsi_eh_finish_cmd(scmd
,
1595 list_move_tail(&scmd
->eh_entry
,
1600 SCSI_LOG_ERROR_RECOVERY(3,
1601 shost_printk(KERN_INFO
, shost
,
1602 "%s: BRST failed chan: %d\n",
1603 current
->comm
, channel
));
1606 return scsi_eh_test_devices(&check_list
, work_q
, done_q
, 0);
1610 * scsi_eh_host_reset - send a host reset
1611 * @shost: host to be reset.
1612 * @work_q: &list_head for pending commands.
1613 * @done_q: &list_head for processed commands.
1615 static int scsi_eh_host_reset(struct Scsi_Host
*shost
,
1616 struct list_head
*work_q
,
1617 struct list_head
*done_q
)
1619 struct scsi_cmnd
*scmd
, *next
;
1620 LIST_HEAD(check_list
);
1623 if (!list_empty(work_q
)) {
1624 scmd
= list_entry(work_q
->next
,
1625 struct scsi_cmnd
, eh_entry
);
1627 SCSI_LOG_ERROR_RECOVERY(3,
1628 shost_printk(KERN_INFO
, shost
,
1629 "%s: Sending HRST\n",
1632 rtn
= scsi_try_host_reset(scmd
);
1633 if (rtn
== SUCCESS
) {
1634 list_splice_init(work_q
, &check_list
);
1635 } else if (rtn
== FAST_IO_FAIL
) {
1636 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1637 scsi_eh_finish_cmd(scmd
, done_q
);
1640 SCSI_LOG_ERROR_RECOVERY(3,
1641 shost_printk(KERN_INFO
, shost
,
1642 "%s: HRST failed\n",
1646 return scsi_eh_test_devices(&check_list
, work_q
, done_q
, 1);
1650 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
1651 * @work_q: &list_head for pending commands.
1652 * @done_q: &list_head for processed commands.
1654 static void scsi_eh_offline_sdevs(struct list_head
*work_q
,
1655 struct list_head
*done_q
)
1657 struct scsi_cmnd
*scmd
, *next
;
1658 struct scsi_device
*sdev
;
1660 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1661 sdev_printk(KERN_INFO
, scmd
->device
, "Device offlined - "
1662 "not ready after error recovery\n");
1663 sdev
= scmd
->device
;
1665 mutex_lock(&sdev
->state_mutex
);
1666 scsi_device_set_state(sdev
, SDEV_OFFLINE
);
1667 mutex_unlock(&sdev
->state_mutex
);
1669 scsi_eh_finish_cmd(scmd
, done_q
);
1675 * scsi_noretry_cmd - determine if command should be failed fast
1676 * @scmd: SCSI cmd to examine.
1678 int scsi_noretry_cmd(struct scsi_cmnd
*scmd
)
1680 switch (host_byte(scmd
->result
)) {
1686 return (scmd
->request
->cmd_flags
& REQ_FAILFAST_TRANSPORT
);
1688 return (scmd
->request
->cmd_flags
& REQ_FAILFAST_DEV
);
1690 if (msg_byte(scmd
->result
) == COMMAND_COMPLETE
&&
1691 status_byte(scmd
->result
) == RESERVATION_CONFLICT
)
1694 case DID_SOFT_ERROR
:
1695 return (scmd
->request
->cmd_flags
& REQ_FAILFAST_DRIVER
);
1698 if (status_byte(scmd
->result
) != CHECK_CONDITION
)
1703 * assume caller has checked sense and determined
1704 * the check condition was retryable.
1706 if (scmd
->request
->cmd_flags
& REQ_FAILFAST_DEV
||
1707 blk_rq_is_passthrough(scmd
->request
))
1714 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1715 * @scmd: SCSI cmd to examine.
1718 * This is *only* called when we are examining the status after sending
1719 * out the actual data command. any commands that are queued for error
1720 * recovery (e.g. test_unit_ready) do *not* come through here.
1722 * When this routine returns failed, it means the error handler thread
1723 * is woken. In cases where the error code indicates an error that
1724 * doesn't require the error handler read (i.e. we don't need to
1725 * abort/reset), this function should return SUCCESS.
1727 int scsi_decide_disposition(struct scsi_cmnd
*scmd
)
1732 * if the device is offline, then we clearly just pass the result back
1733 * up to the top level.
1735 if (!scsi_device_online(scmd
->device
)) {
1736 SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO
, scmd
,
1737 "%s: device offline - report as SUCCESS\n", __func__
));
1742 * first check the host byte, to see if there is anything in there
1743 * that would indicate what we need to do.
1745 switch (host_byte(scmd
->result
)) {
1746 case DID_PASSTHROUGH
:
1748 * no matter what, pass this through to the upper layer.
1749 * nuke this special code so that it looks like we are saying
1752 scmd
->result
&= 0xff00ffff;
1756 * looks good. drop through, and check the next byte.
1760 if (scmd
->eh_eflags
& SCSI_EH_ABORT_SCHEDULED
) {
1761 set_host_byte(scmd
, DID_TIME_OUT
);
1765 case DID_NO_CONNECT
:
1766 case DID_BAD_TARGET
:
1768 * note - this means that we just report the status back
1769 * to the top level driver, not that we actually think
1770 * that it indicates SUCCESS.
1773 case DID_SOFT_ERROR
:
1775 * when the low level driver returns did_soft_error,
1776 * it is responsible for keeping an internal retry counter
1777 * in order to avoid endless loops (db)
1784 return ADD_TO_MLQUEUE
;
1785 case DID_TRANSPORT_DISRUPTED
:
1787 * LLD/transport was disrupted during processing of the IO.
1788 * The transport class is now blocked/blocking,
1789 * and the transport will decide what to do with the IO
1790 * based on its timers and recovery capablilities if
1791 * there are enough retries.
1794 case DID_TRANSPORT_FAILFAST
:
1796 * The transport decided to failfast the IO (most likely
1797 * the fast io fail tmo fired), so send IO directly upwards.
1801 if (msg_byte(scmd
->result
) == COMMAND_COMPLETE
&&
1802 status_byte(scmd
->result
) == RESERVATION_CONFLICT
)
1804 * execute reservation conflict processing code
1814 * when we scan the bus, we get timeout messages for
1815 * these commands if there is no device available.
1816 * other hosts report did_no_connect for the same thing.
1818 if ((scmd
->cmnd
[0] == TEST_UNIT_READY
||
1819 scmd
->cmnd
[0] == INQUIRY
)) {
1831 * next, check the message byte.
1833 if (msg_byte(scmd
->result
) != COMMAND_COMPLETE
)
1837 * check the status byte to see if this indicates anything special.
1839 switch (status_byte(scmd
->result
)) {
1841 scsi_handle_queue_full(scmd
->device
);
1843 * the case of trying to send too many commands to a
1844 * tagged queueing device.
1849 * device can't talk to us at the moment. Should only
1850 * occur (SAM-3) when the task queue is empty, so will cause
1851 * the empty queue handling to trigger a stall in the
1854 return ADD_TO_MLQUEUE
;
1856 if (scmd
->cmnd
[0] == REPORT_LUNS
)
1857 scmd
->device
->sdev_target
->expecting_lun_change
= 0;
1858 scsi_handle_queue_ramp_up(scmd
->device
);
1860 case COMMAND_TERMINATED
:
1864 case CHECK_CONDITION
:
1865 rtn
= scsi_check_sense(scmd
);
1866 if (rtn
== NEEDS_RETRY
)
1868 /* if rtn == FAILED, we have no sense information;
1869 * returning FAILED will wake the error handler thread
1870 * to collect the sense and redo the decide
1873 case CONDITION_GOOD
:
1874 case INTERMEDIATE_GOOD
:
1875 case INTERMEDIATE_C_GOOD
:
1878 * who knows? FIXME(eric)
1882 case RESERVATION_CONFLICT
:
1883 sdev_printk(KERN_INFO
, scmd
->device
,
1884 "reservation conflict\n");
1885 set_host_byte(scmd
, DID_NEXUS_FAILURE
);
1886 return SUCCESS
; /* causes immediate i/o error */
1894 /* we requeue for retry because the error was retryable, and
1895 * the request was not marked fast fail. Note that above,
1896 * even if the request is marked fast fail, we still requeue
1897 * for queue congestion conditions (QUEUE_FULL or BUSY) */
1898 if ((++scmd
->retries
) <= scmd
->allowed
1899 && !scsi_noretry_cmd(scmd
)) {
1903 * no more retries - report this one back to upper level.
1909 static void eh_lock_door_done(struct request
*req
, blk_status_t status
)
1911 __blk_put_request(req
->q
, req
);
1915 * scsi_eh_lock_door - Prevent medium removal for the specified device
1916 * @sdev: SCSI device to prevent medium removal
1919 * We must be called from process context.
1922 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1923 * head of the devices request queue, and continue.
1925 static void scsi_eh_lock_door(struct scsi_device
*sdev
)
1927 struct request
*req
;
1928 struct scsi_request
*rq
;
1931 * blk_get_request with GFP_KERNEL (__GFP_RECLAIM) sleeps until a
1932 * request becomes available
1934 req
= blk_get_request(sdev
->request_queue
, REQ_OP_SCSI_IN
, GFP_KERNEL
);
1939 rq
->cmd
[0] = ALLOW_MEDIUM_REMOVAL
;
1943 rq
->cmd
[4] = SCSI_REMOVAL_PREVENT
;
1945 rq
->cmd_len
= COMMAND_SIZE(rq
->cmd
[0]);
1947 req
->rq_flags
|= RQF_QUIET
;
1948 req
->timeout
= 10 * HZ
;
1951 blk_execute_rq_nowait(req
->q
, NULL
, req
, 1, eh_lock_door_done
);
1955 * scsi_restart_operations - restart io operations to the specified host.
1956 * @shost: Host we are restarting.
1959 * When we entered the error handler, we blocked all further i/o to
1960 * this device. we need to 'reverse' this process.
1962 static void scsi_restart_operations(struct Scsi_Host
*shost
)
1964 struct scsi_device
*sdev
;
1965 unsigned long flags
;
1968 * If the door was locked, we need to insert a door lock request
1969 * onto the head of the SCSI request queue for the device. There
1970 * is no point trying to lock the door of an off-line device.
1972 shost_for_each_device(sdev
, shost
) {
1973 if (scsi_device_online(sdev
) && sdev
->was_reset
&& sdev
->locked
) {
1974 scsi_eh_lock_door(sdev
);
1975 sdev
->was_reset
= 0;
1980 * next free up anything directly waiting upon the host. this
1981 * will be requests for character device operations, and also for
1982 * ioctls to queued block devices.
1984 SCSI_LOG_ERROR_RECOVERY(3,
1985 shost_printk(KERN_INFO
, shost
, "waking up host to restart\n"));
1987 spin_lock_irqsave(shost
->host_lock
, flags
);
1988 if (scsi_host_set_state(shost
, SHOST_RUNNING
))
1989 if (scsi_host_set_state(shost
, SHOST_CANCEL
))
1990 BUG_ON(scsi_host_set_state(shost
, SHOST_DEL
));
1991 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1993 wake_up(&shost
->host_wait
);
1996 * finally we need to re-initiate requests that may be pending. we will
1997 * have had everything blocked while error handling is taking place, and
1998 * now that error recovery is done, we will need to ensure that these
1999 * requests are started.
2001 scsi_run_host_queues(shost
);
2004 * if eh is active and host_eh_scheduled is pending we need to re-run
2005 * recovery. we do this check after scsi_run_host_queues() to allow
2006 * everything pent up since the last eh run a chance to make forward
2007 * progress before we sync again. Either we'll immediately re-run
2008 * recovery or scsi_device_unbusy() will wake us again when these
2009 * pending commands complete.
2011 spin_lock_irqsave(shost
->host_lock
, flags
);
2012 if (shost
->host_eh_scheduled
)
2013 if (scsi_host_set_state(shost
, SHOST_RECOVERY
))
2014 WARN_ON(scsi_host_set_state(shost
, SHOST_CANCEL_RECOVERY
));
2015 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2019 * scsi_eh_ready_devs - check device ready state and recover if not.
2020 * @shost: host to be recovered.
2021 * @work_q: &list_head for pending commands.
2022 * @done_q: &list_head for processed commands.
2024 void scsi_eh_ready_devs(struct Scsi_Host
*shost
,
2025 struct list_head
*work_q
,
2026 struct list_head
*done_q
)
2028 if (!scsi_eh_stu(shost
, work_q
, done_q
))
2029 if (!scsi_eh_bus_device_reset(shost
, work_q
, done_q
))
2030 if (!scsi_eh_target_reset(shost
, work_q
, done_q
))
2031 if (!scsi_eh_bus_reset(shost
, work_q
, done_q
))
2032 if (!scsi_eh_host_reset(shost
, work_q
, done_q
))
2033 scsi_eh_offline_sdevs(work_q
,
2036 EXPORT_SYMBOL_GPL(scsi_eh_ready_devs
);
2039 * scsi_eh_flush_done_q - finish processed commands or retry them.
2040 * @done_q: list_head of processed commands.
2042 void scsi_eh_flush_done_q(struct list_head
*done_q
)
2044 struct scsi_cmnd
*scmd
, *next
;
2046 list_for_each_entry_safe(scmd
, next
, done_q
, eh_entry
) {
2047 list_del_init(&scmd
->eh_entry
);
2048 if (scsi_device_online(scmd
->device
) &&
2049 !scsi_noretry_cmd(scmd
) &&
2050 (++scmd
->retries
<= scmd
->allowed
)) {
2051 SCSI_LOG_ERROR_RECOVERY(3,
2052 scmd_printk(KERN_INFO
, scmd
,
2053 "%s: flush retry cmd\n",
2055 scsi_queue_insert(scmd
, SCSI_MLQUEUE_EH_RETRY
);
2058 * If just we got sense for the device (called
2059 * scsi_eh_get_sense), scmd->result is already
2060 * set, do not set DRIVER_TIMEOUT.
2063 scmd
->result
|= (DRIVER_TIMEOUT
<< 24);
2064 SCSI_LOG_ERROR_RECOVERY(3,
2065 scmd_printk(KERN_INFO
, scmd
,
2066 "%s: flush finish cmd\n",
2068 scsi_finish_command(scmd
);
2072 EXPORT_SYMBOL(scsi_eh_flush_done_q
);
2075 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
2076 * @shost: Host to unjam.
2079 * When we come in here, we *know* that all commands on the bus have
2080 * either completed, failed or timed out. we also know that no further
2081 * commands are being sent to the host, so things are relatively quiet
2082 * and we have freedom to fiddle with things as we wish.
2084 * This is only the *default* implementation. it is possible for
2085 * individual drivers to supply their own version of this function, and
2086 * if the maintainer wishes to do this, it is strongly suggested that
2087 * this function be taken as a template and modified. this function
2088 * was designed to correctly handle problems for about 95% of the
2089 * different cases out there, and it should always provide at least a
2090 * reasonable amount of error recovery.
2092 * Any command marked 'failed' or 'timeout' must eventually have
2093 * scsi_finish_cmd() called for it. we do all of the retry stuff
2094 * here, so when we restart the host after we return it should have an
2097 static void scsi_unjam_host(struct Scsi_Host
*shost
)
2099 unsigned long flags
;
2100 LIST_HEAD(eh_work_q
);
2101 LIST_HEAD(eh_done_q
);
2103 spin_lock_irqsave(shost
->host_lock
, flags
);
2104 list_splice_init(&shost
->eh_cmd_q
, &eh_work_q
);
2105 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2107 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost
, &eh_work_q
));
2109 if (!scsi_eh_get_sense(&eh_work_q
, &eh_done_q
))
2110 scsi_eh_ready_devs(shost
, &eh_work_q
, &eh_done_q
);
2112 spin_lock_irqsave(shost
->host_lock
, flags
);
2113 if (shost
->eh_deadline
!= -1)
2114 shost
->last_reset
= 0;
2115 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2116 scsi_eh_flush_done_q(&eh_done_q
);
2120 * scsi_error_handler - SCSI error handler thread
2121 * @data: Host for which we are running.
2124 * This is the main error handling loop. This is run as a kernel thread
2125 * for every SCSI host and handles all error handling activity.
2127 int scsi_error_handler(void *data
)
2129 struct Scsi_Host
*shost
= data
;
2132 * We use TASK_INTERRUPTIBLE so that the thread is not
2133 * counted against the load average as a running process.
2134 * We never actually get interrupted because kthread_run
2135 * disables signal delivery for the created thread.
2139 * The sequence in kthread_stop() sets the stop flag first
2140 * then wakes the process. To avoid missed wakeups, the task
2141 * should always be in a non running state before the stop
2144 set_current_state(TASK_INTERRUPTIBLE
);
2145 if (kthread_should_stop())
2148 if ((shost
->host_failed
== 0 && shost
->host_eh_scheduled
== 0) ||
2149 shost
->host_failed
!= atomic_read(&shost
->host_busy
)) {
2150 SCSI_LOG_ERROR_RECOVERY(1,
2151 shost_printk(KERN_INFO
, shost
,
2152 "scsi_eh_%d: sleeping\n",
2158 __set_current_state(TASK_RUNNING
);
2159 SCSI_LOG_ERROR_RECOVERY(1,
2160 shost_printk(KERN_INFO
, shost
,
2161 "scsi_eh_%d: waking up %d/%d/%d\n",
2162 shost
->host_no
, shost
->host_eh_scheduled
,
2164 atomic_read(&shost
->host_busy
)));
2167 * We have a host that is failing for some reason. Figure out
2168 * what we need to do to get it up and online again (if we can).
2169 * If we fail, we end up taking the thing offline.
2171 if (!shost
->eh_noresume
&& scsi_autopm_get_host(shost
) != 0) {
2172 SCSI_LOG_ERROR_RECOVERY(1,
2173 shost_printk(KERN_ERR
, shost
,
2174 "scsi_eh_%d: unable to autoresume\n",
2179 if (shost
->transportt
->eh_strategy_handler
)
2180 shost
->transportt
->eh_strategy_handler(shost
);
2182 scsi_unjam_host(shost
);
2184 /* All scmds have been handled */
2185 shost
->host_failed
= 0;
2188 * Note - if the above fails completely, the action is to take
2189 * individual devices offline and flush the queue of any
2190 * outstanding requests that may have been pending. When we
2191 * restart, we restart any I/O to any other devices on the bus
2192 * which are still online.
2194 scsi_restart_operations(shost
);
2195 if (!shost
->eh_noresume
)
2196 scsi_autopm_put_host(shost
);
2198 __set_current_state(TASK_RUNNING
);
2200 SCSI_LOG_ERROR_RECOVERY(1,
2201 shost_printk(KERN_INFO
, shost
,
2202 "Error handler scsi_eh_%d exiting\n",
2204 shost
->ehandler
= NULL
;
2209 * Function: scsi_report_bus_reset()
2211 * Purpose: Utility function used by low-level drivers to report that
2212 * they have observed a bus reset on the bus being handled.
2214 * Arguments: shost - Host in question
2215 * channel - channel on which reset was observed.
2219 * Lock status: Host lock must be held.
2221 * Notes: This only needs to be called if the reset is one which
2222 * originates from an unknown location. Resets originated
2223 * by the mid-level itself don't need to call this, but there
2224 * should be no harm.
2226 * The main purpose of this is to make sure that a CHECK_CONDITION
2227 * is properly treated.
2229 void scsi_report_bus_reset(struct Scsi_Host
*shost
, int channel
)
2231 struct scsi_device
*sdev
;
2233 __shost_for_each_device(sdev
, shost
) {
2234 if (channel
== sdev_channel(sdev
))
2235 __scsi_report_device_reset(sdev
, NULL
);
2238 EXPORT_SYMBOL(scsi_report_bus_reset
);
2241 * Function: scsi_report_device_reset()
2243 * Purpose: Utility function used by low-level drivers to report that
2244 * they have observed a device reset on the device being handled.
2246 * Arguments: shost - Host in question
2247 * channel - channel on which reset was observed
2248 * target - target on which reset was observed
2252 * Lock status: Host lock must be held
2254 * Notes: This only needs to be called if the reset is one which
2255 * originates from an unknown location. Resets originated
2256 * by the mid-level itself don't need to call this, but there
2257 * should be no harm.
2259 * The main purpose of this is to make sure that a CHECK_CONDITION
2260 * is properly treated.
2262 void scsi_report_device_reset(struct Scsi_Host
*shost
, int channel
, int target
)
2264 struct scsi_device
*sdev
;
2266 __shost_for_each_device(sdev
, shost
) {
2267 if (channel
== sdev_channel(sdev
) &&
2268 target
== sdev_id(sdev
))
2269 __scsi_report_device_reset(sdev
, NULL
);
2272 EXPORT_SYMBOL(scsi_report_device_reset
);
2275 scsi_reset_provider_done_command(struct scsi_cmnd
*scmd
)
2280 * scsi_ioctl_reset: explicitly reset a host/bus/target/device
2281 * @dev: scsi_device to operate on
2282 * @arg: reset type (see sg.h)
2285 scsi_ioctl_reset(struct scsi_device
*dev
, int __user
*arg
)
2287 struct scsi_cmnd
*scmd
;
2288 struct Scsi_Host
*shost
= dev
->host
;
2290 unsigned long flags
;
2291 int error
= 0, rtn
, val
;
2293 if (!capable(CAP_SYS_ADMIN
) || !capable(CAP_SYS_RAWIO
))
2296 error
= get_user(val
, arg
);
2300 if (scsi_autopm_get_host(shost
) < 0)
2304 rq
= kzalloc(sizeof(struct request
) + sizeof(struct scsi_cmnd
) +
2305 shost
->hostt
->cmd_size
, GFP_KERNEL
);
2307 goto out_put_autopm_host
;
2308 blk_rq_init(NULL
, rq
);
2310 scmd
= (struct scsi_cmnd
*)(rq
+ 1);
2311 scsi_init_command(dev
, scmd
);
2313 scmd
->cmnd
= scsi_req(rq
)->cmd
;
2315 scmd
->scsi_done
= scsi_reset_provider_done_command
;
2316 memset(&scmd
->sdb
, 0, sizeof(scmd
->sdb
));
2320 scmd
->sc_data_direction
= DMA_BIDIRECTIONAL
;
2322 spin_lock_irqsave(shost
->host_lock
, flags
);
2323 shost
->tmf_in_progress
= 1;
2324 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2326 switch (val
& ~SG_SCSI_RESET_NO_ESCALATE
) {
2327 case SG_SCSI_RESET_NOTHING
:
2330 case SG_SCSI_RESET_DEVICE
:
2331 rtn
= scsi_try_bus_device_reset(scmd
);
2332 if (rtn
== SUCCESS
|| (val
& SG_SCSI_RESET_NO_ESCALATE
))
2335 case SG_SCSI_RESET_TARGET
:
2336 rtn
= scsi_try_target_reset(scmd
);
2337 if (rtn
== SUCCESS
|| (val
& SG_SCSI_RESET_NO_ESCALATE
))
2340 case SG_SCSI_RESET_BUS
:
2341 rtn
= scsi_try_bus_reset(scmd
);
2342 if (rtn
== SUCCESS
|| (val
& SG_SCSI_RESET_NO_ESCALATE
))
2345 case SG_SCSI_RESET_HOST
:
2346 rtn
= scsi_try_host_reset(scmd
);
2355 error
= (rtn
== SUCCESS
) ? 0 : -EIO
;
2357 spin_lock_irqsave(shost
->host_lock
, flags
);
2358 shost
->tmf_in_progress
= 0;
2359 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2362 * be sure to wake up anyone who was sleeping or had their queue
2363 * suspended while we performed the TMF.
2365 SCSI_LOG_ERROR_RECOVERY(3,
2366 shost_printk(KERN_INFO
, shost
,
2367 "waking up host to restart after TMF\n"));
2369 wake_up(&shost
->host_wait
);
2370 scsi_run_host_queues(shost
);
2372 scsi_put_command(scmd
);
2375 out_put_autopm_host
:
2376 scsi_autopm_put_host(shost
);
2379 EXPORT_SYMBOL(scsi_ioctl_reset
);
2381 bool scsi_command_normalize_sense(const struct scsi_cmnd
*cmd
,
2382 struct scsi_sense_hdr
*sshdr
)
2384 return scsi_normalize_sense(cmd
->sense_buffer
,
2385 SCSI_SENSE_BUFFERSIZE
, sshdr
);
2387 EXPORT_SYMBOL(scsi_command_normalize_sense
);
2390 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
2391 * @sense_buffer: byte array of sense data
2392 * @sb_len: number of valid bytes in sense_buffer
2393 * @info_out: pointer to 64 integer where 8 or 4 byte information
2394 * field will be placed if found.
2397 * true if information field found, false if not found.
2399 bool scsi_get_sense_info_fld(const u8
*sense_buffer
, int sb_len
,
2406 switch (sense_buffer
[0] & 0x7f) {
2409 if (sense_buffer
[0] & 0x80) {
2410 *info_out
= get_unaligned_be32(&sense_buffer
[3]);
2416 ucp
= scsi_sense_desc_find(sense_buffer
, sb_len
,
2418 if (ucp
&& (0xa == ucp
[1])) {
2419 *info_out
= get_unaligned_be64(&ucp
[4]);
2427 EXPORT_SYMBOL(scsi_get_sense_info_fld
);