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_Host
*shost
= container_of(head
, typeof(*shost
), rcu
);
229 spin_lock_irqsave(shost
->host_lock
, flags
);
230 shost
->host_failed
++;
231 scsi_eh_wakeup(shost
);
232 spin_unlock_irqrestore(shost
->host_lock
, flags
);
236 * scsi_eh_scmd_add - add scsi cmd to error handling.
237 * @scmd: scmd to run eh on.
239 void scsi_eh_scmd_add(struct scsi_cmnd
*scmd
)
241 struct Scsi_Host
*shost
= scmd
->device
->host
;
245 WARN_ON_ONCE(!shost
->ehandler
);
247 spin_lock_irqsave(shost
->host_lock
, flags
);
248 if (scsi_host_set_state(shost
, SHOST_RECOVERY
)) {
249 ret
= scsi_host_set_state(shost
, SHOST_CANCEL_RECOVERY
);
252 if (shost
->eh_deadline
!= -1 && !shost
->last_reset
)
253 shost
->last_reset
= jiffies
;
256 list_add_tail(&scmd
->eh_entry
, &shost
->eh_cmd_q
);
257 spin_unlock_irqrestore(shost
->host_lock
, flags
);
259 * Ensure that all tasks observe the host state change before the
260 * host_failed change.
262 call_rcu(&shost
->rcu
, scsi_eh_inc_host_failed
);
266 * scsi_times_out - Timeout function for normal scsi commands.
267 * @req: request that is timing out.
270 * We do not need to lock this. There is the potential for a race
271 * only in that the normal completion handling might run, but if the
272 * normal completion function determines that the timer has already
273 * fired, then it mustn't do anything.
275 enum blk_eh_timer_return
scsi_times_out(struct request
*req
)
277 struct scsi_cmnd
*scmd
= blk_mq_rq_to_pdu(req
);
278 enum blk_eh_timer_return rtn
= BLK_EH_NOT_HANDLED
;
279 struct Scsi_Host
*host
= scmd
->device
->host
;
281 trace_scsi_dispatch_cmd_timeout(scmd
);
282 scsi_log_completion(scmd
, TIMEOUT_ERROR
);
284 if (host
->eh_deadline
!= -1 && !host
->last_reset
)
285 host
->last_reset
= jiffies
;
287 if (host
->hostt
->eh_timed_out
)
288 rtn
= host
->hostt
->eh_timed_out(scmd
);
290 if (rtn
== BLK_EH_NOT_HANDLED
) {
291 if (scsi_abort_command(scmd
) != SUCCESS
) {
292 set_host_byte(scmd
, DID_TIME_OUT
);
293 scsi_eh_scmd_add(scmd
);
301 * scsi_block_when_processing_errors - Prevent cmds from being queued.
302 * @sdev: Device on which we are performing recovery.
305 * We block until the host is out of error recovery, and then check to
306 * see whether the host or the device is offline.
309 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
311 int scsi_block_when_processing_errors(struct scsi_device
*sdev
)
315 wait_event(sdev
->host
->host_wait
, !scsi_host_in_recovery(sdev
->host
));
317 online
= scsi_device_online(sdev
);
319 SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_INFO
, sdev
,
320 "%s: rtn: %d\n", __func__
, online
));
324 EXPORT_SYMBOL(scsi_block_when_processing_errors
);
326 #ifdef CONFIG_SCSI_LOGGING
328 * scsi_eh_prt_fail_stats - Log info on failures.
329 * @shost: scsi host being recovered.
330 * @work_q: Queue of scsi cmds to process.
332 static inline void scsi_eh_prt_fail_stats(struct Scsi_Host
*shost
,
333 struct list_head
*work_q
)
335 struct scsi_cmnd
*scmd
;
336 struct scsi_device
*sdev
;
337 int total_failures
= 0;
340 int devices_failed
= 0;
342 shost_for_each_device(sdev
, shost
) {
343 list_for_each_entry(scmd
, work_q
, eh_entry
) {
344 if (scmd
->device
== sdev
) {
346 if (scmd
->eh_eflags
& SCSI_EH_ABORT_SCHEDULED
)
353 if (cmd_cancel
|| cmd_failed
) {
354 SCSI_LOG_ERROR_RECOVERY(3,
355 shost_printk(KERN_INFO
, shost
,
356 "%s: cmds failed: %d, cancel: %d\n",
357 __func__
, cmd_failed
,
365 SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO
, shost
,
366 "Total of %d commands on %d"
367 " devices require eh work\n",
368 total_failures
, devices_failed
));
373 * scsi_report_lun_change - Set flag on all *other* devices on the same target
374 * to indicate that a UNIT ATTENTION is expected.
375 * @sdev: Device reporting the UNIT ATTENTION
377 static void scsi_report_lun_change(struct scsi_device
*sdev
)
379 sdev
->sdev_target
->expecting_lun_change
= 1;
383 * scsi_report_sense - Examine scsi sense information and log messages for
384 * certain conditions, also issue uevents for some of them.
385 * @sdev: Device reporting the sense code
386 * @sshdr: sshdr to be examined
388 static void scsi_report_sense(struct scsi_device
*sdev
,
389 struct scsi_sense_hdr
*sshdr
)
391 enum scsi_device_event evt_type
= SDEV_EVT_MAXBITS
; /* i.e. none */
393 if (sshdr
->sense_key
== UNIT_ATTENTION
) {
394 if (sshdr
->asc
== 0x3f && sshdr
->ascq
== 0x03) {
395 evt_type
= SDEV_EVT_INQUIRY_CHANGE_REPORTED
;
396 sdev_printk(KERN_WARNING
, sdev
,
397 "Inquiry data has changed");
398 } else if (sshdr
->asc
== 0x3f && sshdr
->ascq
== 0x0e) {
399 evt_type
= SDEV_EVT_LUN_CHANGE_REPORTED
;
400 scsi_report_lun_change(sdev
);
401 sdev_printk(KERN_WARNING
, sdev
,
402 "Warning! Received an indication that the "
403 "LUN assignments on this target have "
404 "changed. The Linux SCSI layer does not "
405 "automatically remap LUN assignments.\n");
406 } else if (sshdr
->asc
== 0x3f)
407 sdev_printk(KERN_WARNING
, sdev
,
408 "Warning! Received an indication that the "
409 "operating parameters on this target have "
410 "changed. The Linux SCSI layer does not "
411 "automatically adjust these parameters.\n");
413 if (sshdr
->asc
== 0x38 && sshdr
->ascq
== 0x07) {
414 evt_type
= SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED
;
415 sdev_printk(KERN_WARNING
, sdev
,
416 "Warning! Received an indication that the "
417 "LUN reached a thin provisioning soft "
421 if (sshdr
->asc
== 0x29) {
422 evt_type
= SDEV_EVT_POWER_ON_RESET_OCCURRED
;
423 sdev_printk(KERN_WARNING
, sdev
,
424 "Power-on or device reset occurred\n");
427 if (sshdr
->asc
== 0x2a && sshdr
->ascq
== 0x01) {
428 evt_type
= SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED
;
429 sdev_printk(KERN_WARNING
, sdev
,
430 "Mode parameters changed");
431 } else if (sshdr
->asc
== 0x2a && sshdr
->ascq
== 0x06) {
432 evt_type
= SDEV_EVT_ALUA_STATE_CHANGE_REPORTED
;
433 sdev_printk(KERN_WARNING
, sdev
,
434 "Asymmetric access state changed");
435 } else if (sshdr
->asc
== 0x2a && sshdr
->ascq
== 0x09) {
436 evt_type
= SDEV_EVT_CAPACITY_CHANGE_REPORTED
;
437 sdev_printk(KERN_WARNING
, sdev
,
438 "Capacity data has changed");
439 } else if (sshdr
->asc
== 0x2a)
440 sdev_printk(KERN_WARNING
, sdev
,
441 "Parameters changed");
444 if (evt_type
!= SDEV_EVT_MAXBITS
) {
445 set_bit(evt_type
, sdev
->pending_events
);
446 schedule_work(&sdev
->event_work
);
451 * scsi_check_sense - Examine scsi cmd sense
452 * @scmd: Cmd to have sense checked.
455 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
458 * When a deferred error is detected the current command has
459 * not been executed and needs retrying.
461 int scsi_check_sense(struct scsi_cmnd
*scmd
)
463 struct scsi_device
*sdev
= scmd
->device
;
464 struct scsi_sense_hdr sshdr
;
466 if (! scsi_command_normalize_sense(scmd
, &sshdr
))
467 return FAILED
; /* no valid sense data */
469 scsi_report_sense(sdev
, &sshdr
);
471 if (scsi_sense_is_deferred(&sshdr
))
474 if (sdev
->handler
&& sdev
->handler
->check_sense
) {
477 rc
= sdev
->handler
->check_sense(sdev
, &sshdr
);
478 if (rc
!= SCSI_RETURN_NOT_HANDLED
)
480 /* handler does not care. Drop down to default handling */
483 if (scmd
->cmnd
[0] == TEST_UNIT_READY
&& scmd
->scsi_done
!= scsi_eh_done
)
485 * nasty: for mid-layer issued TURs, we need to return the
486 * actual sense data without any recovery attempt. For eh
487 * issued ones, we need to try to recover and interpret
492 * Previous logic looked for FILEMARK, EOM or ILI which are
493 * mainly associated with tapes and returned SUCCESS.
495 if (sshdr
.response_code
== 0x70) {
497 if (scmd
->sense_buffer
[2] & 0xe0)
501 * descriptor format: look for "stream commands sense data
502 * descriptor" (see SSC-3). Assume single sense data
503 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
505 if ((sshdr
.additional_length
> 3) &&
506 (scmd
->sense_buffer
[8] == 0x4) &&
507 (scmd
->sense_buffer
[11] & 0xe0))
511 switch (sshdr
.sense_key
) {
514 case RECOVERED_ERROR
:
515 return /* soft_error */ SUCCESS
;
517 case ABORTED_COMMAND
:
518 if (sshdr
.asc
== 0x10) /* DIF */
525 * if we are expecting a cc/ua because of a bus reset that we
526 * performed, treat this just as a retry. otherwise this is
527 * information that we should pass up to the upper-level driver
528 * so that we can deal with it there.
530 if (scmd
->device
->expecting_cc_ua
) {
532 * Because some device does not queue unit
533 * attentions correctly, we carefully check
534 * additional sense code and qualifier so as
535 * not to squash media change unit attention.
537 if (sshdr
.asc
!= 0x28 || sshdr
.ascq
!= 0x00) {
538 scmd
->device
->expecting_cc_ua
= 0;
543 * we might also expect a cc/ua if another LUN on the target
544 * reported a UA with an ASC/ASCQ of 3F 0E -
545 * REPORTED LUNS DATA HAS CHANGED.
547 if (scmd
->device
->sdev_target
->expecting_lun_change
&&
548 sshdr
.asc
== 0x3f && sshdr
.ascq
== 0x0e)
551 * if the device is in the process of becoming ready, we
554 if ((sshdr
.asc
== 0x04) && (sshdr
.ascq
== 0x01))
557 * if the device is not started, we need to wake
558 * the error handler to start the motor
560 if (scmd
->device
->allow_restart
&&
561 (sshdr
.asc
== 0x04) && (sshdr
.ascq
== 0x02))
564 * Pass the UA upwards for a determination in the completion
569 /* these are not supported */
571 if (sshdr
.asc
== 0x27 && sshdr
.ascq
== 0x07) {
572 /* Thin provisioning hard threshold reached */
573 set_host_byte(scmd
, DID_ALLOC_FAILURE
);
578 case VOLUME_OVERFLOW
:
581 set_host_byte(scmd
, DID_TARGET_FAILURE
);
585 if (sshdr
.asc
== 0x11 || /* UNRECOVERED READ ERR */
586 sshdr
.asc
== 0x13 || /* AMNF DATA FIELD */
587 sshdr
.asc
== 0x14) { /* RECORD NOT FOUND */
588 set_host_byte(scmd
, DID_MEDIUM_ERROR
);
594 if (scmd
->device
->retry_hwerror
)
595 return ADD_TO_MLQUEUE
;
597 set_host_byte(scmd
, DID_TARGET_FAILURE
);
600 case ILLEGAL_REQUEST
:
601 if (sshdr
.asc
== 0x20 || /* Invalid command operation code */
602 sshdr
.asc
== 0x21 || /* Logical block address out of range */
603 sshdr
.asc
== 0x22 || /* Invalid function */
604 sshdr
.asc
== 0x24 || /* Invalid field in cdb */
605 sshdr
.asc
== 0x26 || /* Parameter value invalid */
606 sshdr
.asc
== 0x27) { /* Write protected */
607 set_host_byte(scmd
, DID_TARGET_FAILURE
);
615 EXPORT_SYMBOL_GPL(scsi_check_sense
);
617 static void scsi_handle_queue_ramp_up(struct scsi_device
*sdev
)
619 struct scsi_host_template
*sht
= sdev
->host
->hostt
;
620 struct scsi_device
*tmp_sdev
;
622 if (!sht
->track_queue_depth
||
623 sdev
->queue_depth
>= sdev
->max_queue_depth
)
626 if (time_before(jiffies
,
627 sdev
->last_queue_ramp_up
+ sdev
->queue_ramp_up_period
))
630 if (time_before(jiffies
,
631 sdev
->last_queue_full_time
+ sdev
->queue_ramp_up_period
))
635 * Walk all devices of a target and do
638 shost_for_each_device(tmp_sdev
, sdev
->host
) {
639 if (tmp_sdev
->channel
!= sdev
->channel
||
640 tmp_sdev
->id
!= sdev
->id
||
641 tmp_sdev
->queue_depth
== sdev
->max_queue_depth
)
644 scsi_change_queue_depth(tmp_sdev
, tmp_sdev
->queue_depth
+ 1);
645 sdev
->last_queue_ramp_up
= jiffies
;
649 static void scsi_handle_queue_full(struct scsi_device
*sdev
)
651 struct scsi_host_template
*sht
= sdev
->host
->hostt
;
652 struct scsi_device
*tmp_sdev
;
654 if (!sht
->track_queue_depth
)
657 shost_for_each_device(tmp_sdev
, sdev
->host
) {
658 if (tmp_sdev
->channel
!= sdev
->channel
||
659 tmp_sdev
->id
!= sdev
->id
)
662 * We do not know the number of commands that were at
663 * the device when we got the queue full so we start
664 * from the highest possible value and work our way down.
666 scsi_track_queue_full(tmp_sdev
, tmp_sdev
->queue_depth
- 1);
671 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
672 * @scmd: SCSI cmd to examine.
675 * This is *only* called when we are examining the status of commands
676 * queued during error recovery. the main difference here is that we
677 * don't allow for the possibility of retries here, and we are a lot
678 * more restrictive about what we consider acceptable.
680 static int scsi_eh_completed_normally(struct scsi_cmnd
*scmd
)
683 * first check the host byte, to see if there is anything in there
684 * that would indicate what we need to do.
686 if (host_byte(scmd
->result
) == DID_RESET
) {
688 * rats. we are already in the error handler, so we now
689 * get to try and figure out what to do next. if the sense
690 * is valid, we have a pretty good idea of what to do.
691 * if not, we mark it as FAILED.
693 return scsi_check_sense(scmd
);
695 if (host_byte(scmd
->result
) != DID_OK
)
699 * next, check the message byte.
701 if (msg_byte(scmd
->result
) != COMMAND_COMPLETE
)
705 * now, check the status byte to see if this indicates
708 switch (status_byte(scmd
->result
)) {
710 scsi_handle_queue_ramp_up(scmd
->device
);
712 case COMMAND_TERMINATED
:
714 case CHECK_CONDITION
:
715 return scsi_check_sense(scmd
);
717 case INTERMEDIATE_GOOD
:
718 case INTERMEDIATE_C_GOOD
:
720 * who knows? FIXME(eric)
723 case RESERVATION_CONFLICT
:
724 if (scmd
->cmnd
[0] == TEST_UNIT_READY
)
725 /* it is a success, we probed the device and
728 /* otherwise, we failed to send the command */
731 scsi_handle_queue_full(scmd
->device
);
742 * scsi_eh_done - Completion function for error handling.
743 * @scmd: Cmd that is done.
745 static void scsi_eh_done(struct scsi_cmnd
*scmd
)
747 struct completion
*eh_action
;
749 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO
, scmd
,
750 "%s result: %x\n", __func__
, scmd
->result
));
752 eh_action
= scmd
->device
->host
->eh_action
;
758 * scsi_try_host_reset - ask host adapter to reset itself
759 * @scmd: SCSI cmd to send host reset.
761 static int scsi_try_host_reset(struct scsi_cmnd
*scmd
)
765 struct Scsi_Host
*host
= scmd
->device
->host
;
766 struct scsi_host_template
*hostt
= host
->hostt
;
768 SCSI_LOG_ERROR_RECOVERY(3,
769 shost_printk(KERN_INFO
, host
, "Snd Host RST\n"));
771 if (!hostt
->eh_host_reset_handler
)
774 rtn
= hostt
->eh_host_reset_handler(scmd
);
776 if (rtn
== SUCCESS
) {
777 if (!hostt
->skip_settle_delay
)
778 ssleep(HOST_RESET_SETTLE_TIME
);
779 spin_lock_irqsave(host
->host_lock
, flags
);
780 scsi_report_bus_reset(host
, scmd_channel(scmd
));
781 spin_unlock_irqrestore(host
->host_lock
, flags
);
788 * scsi_try_bus_reset - ask host to perform a bus reset
789 * @scmd: SCSI cmd to send bus reset.
791 static int scsi_try_bus_reset(struct scsi_cmnd
*scmd
)
795 struct Scsi_Host
*host
= scmd
->device
->host
;
796 struct scsi_host_template
*hostt
= host
->hostt
;
798 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO
, scmd
,
799 "%s: Snd Bus RST\n", __func__
));
801 if (!hostt
->eh_bus_reset_handler
)
804 rtn
= hostt
->eh_bus_reset_handler(scmd
);
806 if (rtn
== SUCCESS
) {
807 if (!hostt
->skip_settle_delay
)
808 ssleep(BUS_RESET_SETTLE_TIME
);
809 spin_lock_irqsave(host
->host_lock
, flags
);
810 scsi_report_bus_reset(host
, scmd_channel(scmd
));
811 spin_unlock_irqrestore(host
->host_lock
, flags
);
817 static void __scsi_report_device_reset(struct scsi_device
*sdev
, void *data
)
820 sdev
->expecting_cc_ua
= 1;
824 * scsi_try_target_reset - Ask host to perform a target reset
825 * @scmd: SCSI cmd used to send a target reset
828 * There is no timeout for this operation. if this operation is
829 * unreliable for a given host, then the host itself needs to put a
830 * timer on it, and set the host back to a consistent state prior to
833 static int scsi_try_target_reset(struct scsi_cmnd
*scmd
)
837 struct Scsi_Host
*host
= scmd
->device
->host
;
838 struct scsi_host_template
*hostt
= host
->hostt
;
840 if (!hostt
->eh_target_reset_handler
)
843 rtn
= hostt
->eh_target_reset_handler(scmd
);
844 if (rtn
== SUCCESS
) {
845 spin_lock_irqsave(host
->host_lock
, flags
);
846 __starget_for_each_device(scsi_target(scmd
->device
), NULL
,
847 __scsi_report_device_reset
);
848 spin_unlock_irqrestore(host
->host_lock
, flags
);
855 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
856 * @scmd: SCSI cmd used to send BDR
859 * There is no timeout for this operation. if this operation is
860 * unreliable for a given host, then the host itself needs to put a
861 * timer on it, and set the host back to a consistent state prior to
864 static int scsi_try_bus_device_reset(struct scsi_cmnd
*scmd
)
867 struct scsi_host_template
*hostt
= scmd
->device
->host
->hostt
;
869 if (!hostt
->eh_device_reset_handler
)
872 rtn
= hostt
->eh_device_reset_handler(scmd
);
874 __scsi_report_device_reset(scmd
->device
, NULL
);
879 * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
880 * @hostt: SCSI driver host template
881 * @scmd: SCSI cmd used to send a target reset
884 * SUCCESS, FAILED, or FAST_IO_FAIL
887 * SUCCESS does not necessarily indicate that the command
888 * has been aborted; it only indicates that the LLDDs
889 * has cleared all references to that command.
890 * LLDDs should return FAILED only if an abort was required
891 * but could not be executed. LLDDs should return FAST_IO_FAIL
892 * if the device is temporarily unavailable (eg due to a
893 * link down on FibreChannel)
895 static int scsi_try_to_abort_cmd(struct scsi_host_template
*hostt
,
896 struct scsi_cmnd
*scmd
)
898 if (!hostt
->eh_abort_handler
)
901 return hostt
->eh_abort_handler(scmd
);
904 static void scsi_abort_eh_cmnd(struct scsi_cmnd
*scmd
)
906 if (scsi_try_to_abort_cmd(scmd
->device
->host
->hostt
, scmd
) != SUCCESS
)
907 if (scsi_try_bus_device_reset(scmd
) != SUCCESS
)
908 if (scsi_try_target_reset(scmd
) != SUCCESS
)
909 if (scsi_try_bus_reset(scmd
) != SUCCESS
)
910 scsi_try_host_reset(scmd
);
914 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
915 * @scmd: SCSI command structure to hijack
916 * @ses: structure to save restore information
917 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
918 * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
919 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
921 * This function is used to save a scsi command information before re-execution
922 * as part of the error recovery process. If @sense_bytes is 0 the command
923 * sent must be one that does not transfer any data. If @sense_bytes != 0
924 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
925 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
927 void scsi_eh_prep_cmnd(struct scsi_cmnd
*scmd
, struct scsi_eh_save
*ses
,
928 unsigned char *cmnd
, int cmnd_size
, unsigned sense_bytes
)
930 struct scsi_device
*sdev
= scmd
->device
;
933 * We need saved copies of a number of fields - this is because
934 * error handling may need to overwrite these with different values
935 * to run different commands, and once error handling is complete,
936 * we will need to restore these values prior to running the actual
939 ses
->cmd_len
= scmd
->cmd_len
;
940 ses
->cmnd
= scmd
->cmnd
;
941 ses
->data_direction
= scmd
->sc_data_direction
;
942 ses
->sdb
= scmd
->sdb
;
943 ses
->next_rq
= scmd
->request
->next_rq
;
944 ses
->result
= scmd
->result
;
945 ses
->underflow
= scmd
->underflow
;
946 ses
->prot_op
= scmd
->prot_op
;
947 ses
->eh_eflags
= scmd
->eh_eflags
;
949 scmd
->prot_op
= SCSI_PROT_NORMAL
;
951 scmd
->cmnd
= ses
->eh_cmnd
;
952 memset(scmd
->cmnd
, 0, BLK_MAX_CDB
);
953 memset(&scmd
->sdb
, 0, sizeof(scmd
->sdb
));
954 scmd
->request
->next_rq
= NULL
;
958 scmd
->sdb
.length
= min_t(unsigned, SCSI_SENSE_BUFFERSIZE
,
960 sg_init_one(&ses
->sense_sgl
, scmd
->sense_buffer
,
962 scmd
->sdb
.table
.sgl
= &ses
->sense_sgl
;
963 scmd
->sc_data_direction
= DMA_FROM_DEVICE
;
964 scmd
->sdb
.table
.nents
= scmd
->sdb
.table
.orig_nents
= 1;
965 scmd
->cmnd
[0] = REQUEST_SENSE
;
966 scmd
->cmnd
[4] = scmd
->sdb
.length
;
967 scmd
->cmd_len
= COMMAND_SIZE(scmd
->cmnd
[0]);
969 scmd
->sc_data_direction
= DMA_NONE
;
971 BUG_ON(cmnd_size
> BLK_MAX_CDB
);
972 memcpy(scmd
->cmnd
, cmnd
, cmnd_size
);
973 scmd
->cmd_len
= COMMAND_SIZE(scmd
->cmnd
[0]);
979 if (sdev
->scsi_level
<= SCSI_2
&& sdev
->scsi_level
!= SCSI_UNKNOWN
)
980 scmd
->cmnd
[1] = (scmd
->cmnd
[1] & 0x1f) |
981 (sdev
->lun
<< 5 & 0xe0);
984 * Zero the sense buffer. The scsi spec mandates that any
985 * untransferred sense data should be interpreted as being zero.
987 memset(scmd
->sense_buffer
, 0, SCSI_SENSE_BUFFERSIZE
);
989 EXPORT_SYMBOL(scsi_eh_prep_cmnd
);
992 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
993 * @scmd: SCSI command structure to restore
994 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
996 * Undo any damage done by above scsi_eh_prep_cmnd().
998 void scsi_eh_restore_cmnd(struct scsi_cmnd
* scmd
, struct scsi_eh_save
*ses
)
1001 * Restore original data
1003 scmd
->cmd_len
= ses
->cmd_len
;
1004 scmd
->cmnd
= ses
->cmnd
;
1005 scmd
->sc_data_direction
= ses
->data_direction
;
1006 scmd
->sdb
= ses
->sdb
;
1007 scmd
->request
->next_rq
= ses
->next_rq
;
1008 scmd
->result
= ses
->result
;
1009 scmd
->underflow
= ses
->underflow
;
1010 scmd
->prot_op
= ses
->prot_op
;
1011 scmd
->eh_eflags
= ses
->eh_eflags
;
1013 EXPORT_SYMBOL(scsi_eh_restore_cmnd
);
1016 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
1017 * @scmd: SCSI command structure to hijack
1018 * @cmnd: CDB to send
1019 * @cmnd_size: size in bytes of @cmnd
1020 * @timeout: timeout for this request
1021 * @sense_bytes: size of sense data to copy or 0
1023 * This function is used to send a scsi command down to a target device
1024 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
1027 * SUCCESS or FAILED or NEEDS_RETRY
1029 static int scsi_send_eh_cmnd(struct scsi_cmnd
*scmd
, unsigned char *cmnd
,
1030 int cmnd_size
, int timeout
, unsigned sense_bytes
)
1032 struct scsi_device
*sdev
= scmd
->device
;
1033 struct Scsi_Host
*shost
= sdev
->host
;
1034 DECLARE_COMPLETION_ONSTACK(done
);
1035 unsigned long timeleft
= timeout
;
1036 struct scsi_eh_save ses
;
1037 const unsigned long stall_for
= msecs_to_jiffies(100);
1041 scsi_eh_prep_cmnd(scmd
, &ses
, cmnd
, cmnd_size
, sense_bytes
);
1042 shost
->eh_action
= &done
;
1044 scsi_log_send(scmd
);
1045 scmd
->scsi_done
= scsi_eh_done
;
1046 rtn
= shost
->hostt
->queuecommand(shost
, scmd
);
1048 if (timeleft
> stall_for
) {
1049 scsi_eh_restore_cmnd(scmd
, &ses
);
1050 timeleft
-= stall_for
;
1051 msleep(jiffies_to_msecs(stall_for
));
1054 /* signal not to enter either branch of the if () below */
1058 timeleft
= wait_for_completion_timeout(&done
, timeout
);
1062 shost
->eh_action
= NULL
;
1064 scsi_log_completion(scmd
, rtn
);
1066 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO
, scmd
,
1067 "%s timeleft: %ld\n",
1068 __func__
, timeleft
));
1071 * If there is time left scsi_eh_done got called, and we will examine
1072 * the actual status codes to see whether the command actually did
1073 * complete normally, else if we have a zero return and no time left,
1074 * the command must still be pending, so abort it and return FAILED.
1075 * If we never actually managed to issue the command, because
1076 * ->queuecommand() kept returning non zero, use the rtn = FAILED
1077 * value above (so don't execute either branch of the if)
1080 rtn
= scsi_eh_completed_normally(scmd
);
1081 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO
, scmd
,
1082 "%s: scsi_eh_completed_normally %x\n", __func__
, rtn
));
1089 case ADD_TO_MLQUEUE
:
1096 } else if (rtn
!= FAILED
) {
1097 scsi_abort_eh_cmnd(scmd
);
1101 scsi_eh_restore_cmnd(scmd
, &ses
);
1107 * scsi_request_sense - Request sense data from a particular target.
1108 * @scmd: SCSI cmd for request sense.
1111 * Some hosts automatically obtain this information, others require
1112 * that we obtain it on our own. This function will *not* return until
1113 * the command either times out, or it completes.
1115 static int scsi_request_sense(struct scsi_cmnd
*scmd
)
1117 return scsi_send_eh_cmnd(scmd
, NULL
, 0, scmd
->device
->eh_timeout
, ~0);
1120 static int scsi_eh_action(struct scsi_cmnd
*scmd
, int rtn
)
1122 if (!blk_rq_is_passthrough(scmd
->request
)) {
1123 struct scsi_driver
*sdrv
= scsi_cmd_to_driver(scmd
);
1124 if (sdrv
->eh_action
)
1125 rtn
= sdrv
->eh_action(scmd
, rtn
);
1131 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
1132 * @scmd: Original SCSI cmd that eh has finished.
1133 * @done_q: Queue for processed commands.
1136 * We don't want to use the normal command completion while we are are
1137 * still handling errors - it may cause other commands to be queued,
1138 * and that would disturb what we are doing. Thus we really want to
1139 * keep a list of pending commands for final completion, and once we
1140 * are ready to leave error handling we handle completion for real.
1142 void scsi_eh_finish_cmd(struct scsi_cmnd
*scmd
, struct list_head
*done_q
)
1144 list_move_tail(&scmd
->eh_entry
, done_q
);
1146 EXPORT_SYMBOL(scsi_eh_finish_cmd
);
1149 * scsi_eh_get_sense - Get device sense data.
1150 * @work_q: Queue of commands to process.
1151 * @done_q: Queue of processed commands.
1154 * See if we need to request sense information. if so, then get it
1155 * now, so we have a better idea of what to do.
1158 * This has the unfortunate side effect that if a shost adapter does
1159 * not automatically request sense information, we end up shutting
1160 * it down before we request it.
1162 * All drivers should request sense information internally these days,
1163 * so for now all I have to say is tough noogies if you end up in here.
1165 * XXX: Long term this code should go away, but that needs an audit of
1168 int scsi_eh_get_sense(struct list_head
*work_q
,
1169 struct list_head
*done_q
)
1171 struct scsi_cmnd
*scmd
, *next
;
1172 struct Scsi_Host
*shost
;
1176 * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
1177 * should not get sense.
1179 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1180 if ((scmd
->eh_eflags
& SCSI_EH_ABORT_SCHEDULED
) ||
1181 SCSI_SENSE_VALID(scmd
))
1184 shost
= scmd
->device
->host
;
1185 if (scsi_host_eh_past_deadline(shost
)) {
1186 SCSI_LOG_ERROR_RECOVERY(3,
1187 scmd_printk(KERN_INFO
, scmd
,
1188 "%s: skip request sense, past eh deadline\n",
1192 if (status_byte(scmd
->result
) != CHECK_CONDITION
)
1194 * don't request sense if there's no check condition
1195 * status because the error we're processing isn't one
1196 * that has a sense code (and some devices get
1197 * confused by sense requests out of the blue)
1201 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO
, scmd
,
1202 "%s: requesting sense\n",
1204 rtn
= scsi_request_sense(scmd
);
1208 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO
, scmd
,
1209 "sense requested, result %x\n", scmd
->result
));
1210 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd
));
1212 rtn
= scsi_decide_disposition(scmd
);
1215 * if the result was normal, then just pass it along to the
1219 /* we don't want this command reissued, just
1220 * finished with the sense data, so set
1221 * retries to the max allowed to ensure it
1222 * won't get reissued */
1223 scmd
->retries
= scmd
->allowed
;
1224 else if (rtn
!= NEEDS_RETRY
)
1227 scsi_eh_finish_cmd(scmd
, done_q
);
1230 return list_empty(work_q
);
1232 EXPORT_SYMBOL_GPL(scsi_eh_get_sense
);
1235 * scsi_eh_tur - Send TUR to device.
1236 * @scmd: &scsi_cmnd to send TUR
1239 * 0 - Device is ready. 1 - Device NOT ready.
1241 static int scsi_eh_tur(struct scsi_cmnd
*scmd
)
1243 static unsigned char tur_command
[6] = {TEST_UNIT_READY
, 0, 0, 0, 0, 0};
1244 int retry_cnt
= 1, rtn
;
1247 rtn
= scsi_send_eh_cmnd(scmd
, tur_command
, 6,
1248 scmd
->device
->eh_timeout
, 0);
1250 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO
, scmd
,
1251 "%s return: %x\n", __func__
, rtn
));
1266 * scsi_eh_test_devices - check if devices are responding from error recovery.
1267 * @cmd_list: scsi commands in error recovery.
1268 * @work_q: queue for commands which still need more error recovery
1269 * @done_q: queue for commands which are finished
1270 * @try_stu: boolean on if a STU command should be tried in addition to TUR.
1273 * Tests if devices are in a working state. Commands to devices now in
1274 * a working state are sent to the done_q while commands to devices which
1275 * are still failing to respond are returned to the work_q for more
1278 static int scsi_eh_test_devices(struct list_head
*cmd_list
,
1279 struct list_head
*work_q
,
1280 struct list_head
*done_q
, int try_stu
)
1282 struct scsi_cmnd
*scmd
, *next
;
1283 struct scsi_device
*sdev
;
1286 while (!list_empty(cmd_list
)) {
1287 scmd
= list_entry(cmd_list
->next
, struct scsi_cmnd
, eh_entry
);
1288 sdev
= scmd
->device
;
1291 if (scsi_host_eh_past_deadline(sdev
->host
)) {
1292 /* Push items back onto work_q */
1293 list_splice_init(cmd_list
, work_q
);
1294 SCSI_LOG_ERROR_RECOVERY(3,
1295 sdev_printk(KERN_INFO
, sdev
,
1296 "%s: skip test device, past eh deadline",
1302 finish_cmds
= !scsi_device_online(scmd
->device
) ||
1303 (try_stu
&& !scsi_eh_try_stu(scmd
) &&
1304 !scsi_eh_tur(scmd
)) ||
1307 list_for_each_entry_safe(scmd
, next
, cmd_list
, eh_entry
)
1308 if (scmd
->device
== sdev
) {
1311 scsi_eh_action(scmd
, SUCCESS
) == SUCCESS
))
1312 scsi_eh_finish_cmd(scmd
, done_q
);
1314 list_move_tail(&scmd
->eh_entry
, work_q
);
1317 return list_empty(work_q
);
1321 * scsi_eh_try_stu - Send START_UNIT to device.
1322 * @scmd: &scsi_cmnd to send START_UNIT
1325 * 0 - Device is ready. 1 - Device NOT ready.
1327 static int scsi_eh_try_stu(struct scsi_cmnd
*scmd
)
1329 static unsigned char stu_command
[6] = {START_STOP
, 0, 0, 0, 1, 0};
1331 if (scmd
->device
->allow_restart
) {
1332 int i
, rtn
= NEEDS_RETRY
;
1334 for (i
= 0; rtn
== NEEDS_RETRY
&& i
< 2; i
++)
1335 rtn
= scsi_send_eh_cmnd(scmd
, stu_command
, 6, scmd
->device
->request_queue
->rq_timeout
, 0);
1345 * scsi_eh_stu - send START_UNIT if needed
1346 * @shost: &scsi host being recovered.
1347 * @work_q: &list_head for pending commands.
1348 * @done_q: &list_head for processed commands.
1351 * If commands are failing due to not ready, initializing command required,
1352 * try revalidating the device, which will end up sending a start unit.
1354 static int scsi_eh_stu(struct Scsi_Host
*shost
,
1355 struct list_head
*work_q
,
1356 struct list_head
*done_q
)
1358 struct scsi_cmnd
*scmd
, *stu_scmd
, *next
;
1359 struct scsi_device
*sdev
;
1361 shost_for_each_device(sdev
, shost
) {
1362 if (scsi_host_eh_past_deadline(shost
)) {
1363 SCSI_LOG_ERROR_RECOVERY(3,
1364 sdev_printk(KERN_INFO
, sdev
,
1365 "%s: skip START_UNIT, past eh deadline\n",
1370 list_for_each_entry(scmd
, work_q
, eh_entry
)
1371 if (scmd
->device
== sdev
&& SCSI_SENSE_VALID(scmd
) &&
1372 scsi_check_sense(scmd
) == FAILED
) {
1380 SCSI_LOG_ERROR_RECOVERY(3,
1381 sdev_printk(KERN_INFO
, sdev
,
1382 "%s: Sending START_UNIT\n",
1385 if (!scsi_eh_try_stu(stu_scmd
)) {
1386 if (!scsi_device_online(sdev
) ||
1387 !scsi_eh_tur(stu_scmd
)) {
1388 list_for_each_entry_safe(scmd
, next
,
1390 if (scmd
->device
== sdev
&&
1391 scsi_eh_action(scmd
, SUCCESS
) == SUCCESS
)
1392 scsi_eh_finish_cmd(scmd
, done_q
);
1396 SCSI_LOG_ERROR_RECOVERY(3,
1397 sdev_printk(KERN_INFO
, sdev
,
1398 "%s: START_UNIT failed\n",
1403 return list_empty(work_q
);
1408 * scsi_eh_bus_device_reset - send bdr if needed
1409 * @shost: scsi host being recovered.
1410 * @work_q: &list_head for pending commands.
1411 * @done_q: &list_head for processed commands.
1414 * Try a bus device reset. Still, look to see whether we have multiple
1415 * devices that are jammed or not - if we have multiple devices, it
1416 * makes no sense to try bus_device_reset - we really would need to try
1417 * a bus_reset instead.
1419 static int scsi_eh_bus_device_reset(struct Scsi_Host
*shost
,
1420 struct list_head
*work_q
,
1421 struct list_head
*done_q
)
1423 struct scsi_cmnd
*scmd
, *bdr_scmd
, *next
;
1424 struct scsi_device
*sdev
;
1427 shost_for_each_device(sdev
, shost
) {
1428 if (scsi_host_eh_past_deadline(shost
)) {
1429 SCSI_LOG_ERROR_RECOVERY(3,
1430 sdev_printk(KERN_INFO
, sdev
,
1431 "%s: skip BDR, past eh deadline\n",
1436 list_for_each_entry(scmd
, work_q
, eh_entry
)
1437 if (scmd
->device
== sdev
) {
1445 SCSI_LOG_ERROR_RECOVERY(3,
1446 sdev_printk(KERN_INFO
, sdev
,
1447 "%s: Sending BDR\n", current
->comm
));
1448 rtn
= scsi_try_bus_device_reset(bdr_scmd
);
1449 if (rtn
== SUCCESS
|| rtn
== FAST_IO_FAIL
) {
1450 if (!scsi_device_online(sdev
) ||
1451 rtn
== FAST_IO_FAIL
||
1452 !scsi_eh_tur(bdr_scmd
)) {
1453 list_for_each_entry_safe(scmd
, next
,
1455 if (scmd
->device
== sdev
&&
1456 scsi_eh_action(scmd
, rtn
) != FAILED
)
1457 scsi_eh_finish_cmd(scmd
,
1462 SCSI_LOG_ERROR_RECOVERY(3,
1463 sdev_printk(KERN_INFO
, sdev
,
1464 "%s: BDR failed\n", current
->comm
));
1468 return list_empty(work_q
);
1472 * scsi_eh_target_reset - send target reset if needed
1473 * @shost: scsi host being recovered.
1474 * @work_q: &list_head for pending commands.
1475 * @done_q: &list_head for processed commands.
1478 * Try a target reset.
1480 static int scsi_eh_target_reset(struct Scsi_Host
*shost
,
1481 struct list_head
*work_q
,
1482 struct list_head
*done_q
)
1484 LIST_HEAD(tmp_list
);
1485 LIST_HEAD(check_list
);
1487 list_splice_init(work_q
, &tmp_list
);
1489 while (!list_empty(&tmp_list
)) {
1490 struct scsi_cmnd
*next
, *scmd
;
1494 if (scsi_host_eh_past_deadline(shost
)) {
1495 /* push back on work queue for further processing */
1496 list_splice_init(&check_list
, work_q
);
1497 list_splice_init(&tmp_list
, work_q
);
1498 SCSI_LOG_ERROR_RECOVERY(3,
1499 shost_printk(KERN_INFO
, shost
,
1500 "%s: Skip target reset, past eh deadline\n",
1502 return list_empty(work_q
);
1505 scmd
= list_entry(tmp_list
.next
, struct scsi_cmnd
, eh_entry
);
1508 SCSI_LOG_ERROR_RECOVERY(3,
1509 shost_printk(KERN_INFO
, shost
,
1510 "%s: Sending target reset to target %d\n",
1511 current
->comm
, id
));
1512 rtn
= scsi_try_target_reset(scmd
);
1513 if (rtn
!= SUCCESS
&& rtn
!= FAST_IO_FAIL
)
1514 SCSI_LOG_ERROR_RECOVERY(3,
1515 shost_printk(KERN_INFO
, shost
,
1516 "%s: Target reset failed"
1518 current
->comm
, id
));
1519 list_for_each_entry_safe(scmd
, next
, &tmp_list
, eh_entry
) {
1520 if (scmd_id(scmd
) != id
)
1524 list_move_tail(&scmd
->eh_entry
, &check_list
);
1525 else if (rtn
== FAST_IO_FAIL
)
1526 scsi_eh_finish_cmd(scmd
, done_q
);
1528 /* push back on work queue for further processing */
1529 list_move(&scmd
->eh_entry
, work_q
);
1533 return scsi_eh_test_devices(&check_list
, work_q
, done_q
, 0);
1537 * scsi_eh_bus_reset - send a bus reset
1538 * @shost: &scsi host being recovered.
1539 * @work_q: &list_head for pending commands.
1540 * @done_q: &list_head for processed commands.
1542 static int scsi_eh_bus_reset(struct Scsi_Host
*shost
,
1543 struct list_head
*work_q
,
1544 struct list_head
*done_q
)
1546 struct scsi_cmnd
*scmd
, *chan_scmd
, *next
;
1547 LIST_HEAD(check_list
);
1548 unsigned int channel
;
1552 * we really want to loop over the various channels, and do this on
1553 * a channel by channel basis. we should also check to see if any
1554 * of the failed commands are on soft_reset devices, and if so, skip
1558 for (channel
= 0; channel
<= shost
->max_channel
; channel
++) {
1559 if (scsi_host_eh_past_deadline(shost
)) {
1560 list_splice_init(&check_list
, work_q
);
1561 SCSI_LOG_ERROR_RECOVERY(3,
1562 shost_printk(KERN_INFO
, shost
,
1563 "%s: skip BRST, past eh deadline\n",
1565 return list_empty(work_q
);
1569 list_for_each_entry(scmd
, work_q
, eh_entry
) {
1570 if (channel
== scmd_channel(scmd
)) {
1574 * FIXME add back in some support for
1575 * soft_reset devices.
1582 SCSI_LOG_ERROR_RECOVERY(3,
1583 shost_printk(KERN_INFO
, shost
,
1584 "%s: Sending BRST chan: %d\n",
1585 current
->comm
, channel
));
1586 rtn
= scsi_try_bus_reset(chan_scmd
);
1587 if (rtn
== SUCCESS
|| rtn
== FAST_IO_FAIL
) {
1588 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1589 if (channel
== scmd_channel(scmd
)) {
1590 if (rtn
== FAST_IO_FAIL
)
1591 scsi_eh_finish_cmd(scmd
,
1594 list_move_tail(&scmd
->eh_entry
,
1599 SCSI_LOG_ERROR_RECOVERY(3,
1600 shost_printk(KERN_INFO
, shost
,
1601 "%s: BRST failed chan: %d\n",
1602 current
->comm
, channel
));
1605 return scsi_eh_test_devices(&check_list
, work_q
, done_q
, 0);
1609 * scsi_eh_host_reset - send a host reset
1610 * @shost: host to be reset.
1611 * @work_q: &list_head for pending commands.
1612 * @done_q: &list_head for processed commands.
1614 static int scsi_eh_host_reset(struct Scsi_Host
*shost
,
1615 struct list_head
*work_q
,
1616 struct list_head
*done_q
)
1618 struct scsi_cmnd
*scmd
, *next
;
1619 LIST_HEAD(check_list
);
1622 if (!list_empty(work_q
)) {
1623 scmd
= list_entry(work_q
->next
,
1624 struct scsi_cmnd
, eh_entry
);
1626 SCSI_LOG_ERROR_RECOVERY(3,
1627 shost_printk(KERN_INFO
, shost
,
1628 "%s: Sending HRST\n",
1631 rtn
= scsi_try_host_reset(scmd
);
1632 if (rtn
== SUCCESS
) {
1633 list_splice_init(work_q
, &check_list
);
1634 } else if (rtn
== FAST_IO_FAIL
) {
1635 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1636 scsi_eh_finish_cmd(scmd
, done_q
);
1639 SCSI_LOG_ERROR_RECOVERY(3,
1640 shost_printk(KERN_INFO
, shost
,
1641 "%s: HRST failed\n",
1645 return scsi_eh_test_devices(&check_list
, work_q
, done_q
, 1);
1649 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
1650 * @work_q: &list_head for pending commands.
1651 * @done_q: &list_head for processed commands.
1653 static void scsi_eh_offline_sdevs(struct list_head
*work_q
,
1654 struct list_head
*done_q
)
1656 struct scsi_cmnd
*scmd
, *next
;
1657 struct scsi_device
*sdev
;
1659 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1660 sdev_printk(KERN_INFO
, scmd
->device
, "Device offlined - "
1661 "not ready after error recovery\n");
1662 sdev
= scmd
->device
;
1664 mutex_lock(&sdev
->state_mutex
);
1665 scsi_device_set_state(sdev
, SDEV_OFFLINE
);
1666 mutex_unlock(&sdev
->state_mutex
);
1668 scsi_eh_finish_cmd(scmd
, done_q
);
1674 * scsi_noretry_cmd - determine if command should be failed fast
1675 * @scmd: SCSI cmd to examine.
1677 int scsi_noretry_cmd(struct scsi_cmnd
*scmd
)
1679 switch (host_byte(scmd
->result
)) {
1685 return (scmd
->request
->cmd_flags
& REQ_FAILFAST_TRANSPORT
);
1687 return (scmd
->request
->cmd_flags
& REQ_FAILFAST_DEV
);
1689 if (msg_byte(scmd
->result
) == COMMAND_COMPLETE
&&
1690 status_byte(scmd
->result
) == RESERVATION_CONFLICT
)
1693 case DID_SOFT_ERROR
:
1694 return (scmd
->request
->cmd_flags
& REQ_FAILFAST_DRIVER
);
1697 if (status_byte(scmd
->result
) != CHECK_CONDITION
)
1702 * assume caller has checked sense and determined
1703 * the check condition was retryable.
1705 if (scmd
->request
->cmd_flags
& REQ_FAILFAST_DEV
||
1706 blk_rq_is_passthrough(scmd
->request
))
1713 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1714 * @scmd: SCSI cmd to examine.
1717 * This is *only* called when we are examining the status after sending
1718 * out the actual data command. any commands that are queued for error
1719 * recovery (e.g. test_unit_ready) do *not* come through here.
1721 * When this routine returns failed, it means the error handler thread
1722 * is woken. In cases where the error code indicates an error that
1723 * doesn't require the error handler read (i.e. we don't need to
1724 * abort/reset), this function should return SUCCESS.
1726 int scsi_decide_disposition(struct scsi_cmnd
*scmd
)
1731 * if the device is offline, then we clearly just pass the result back
1732 * up to the top level.
1734 if (!scsi_device_online(scmd
->device
)) {
1735 SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO
, scmd
,
1736 "%s: device offline - report as SUCCESS\n", __func__
));
1741 * first check the host byte, to see if there is anything in there
1742 * that would indicate what we need to do.
1744 switch (host_byte(scmd
->result
)) {
1745 case DID_PASSTHROUGH
:
1747 * no matter what, pass this through to the upper layer.
1748 * nuke this special code so that it looks like we are saying
1751 scmd
->result
&= 0xff00ffff;
1755 * looks good. drop through, and check the next byte.
1759 if (scmd
->eh_eflags
& SCSI_EH_ABORT_SCHEDULED
) {
1760 set_host_byte(scmd
, DID_TIME_OUT
);
1764 case DID_NO_CONNECT
:
1765 case DID_BAD_TARGET
:
1767 * note - this means that we just report the status back
1768 * to the top level driver, not that we actually think
1769 * that it indicates SUCCESS.
1772 case DID_SOFT_ERROR
:
1774 * when the low level driver returns did_soft_error,
1775 * it is responsible for keeping an internal retry counter
1776 * in order to avoid endless loops (db)
1783 return ADD_TO_MLQUEUE
;
1784 case DID_TRANSPORT_DISRUPTED
:
1786 * LLD/transport was disrupted during processing of the IO.
1787 * The transport class is now blocked/blocking,
1788 * and the transport will decide what to do with the IO
1789 * based on its timers and recovery capablilities if
1790 * there are enough retries.
1793 case DID_TRANSPORT_FAILFAST
:
1795 * The transport decided to failfast the IO (most likely
1796 * the fast io fail tmo fired), so send IO directly upwards.
1800 if (msg_byte(scmd
->result
) == COMMAND_COMPLETE
&&
1801 status_byte(scmd
->result
) == RESERVATION_CONFLICT
)
1803 * execute reservation conflict processing code
1813 * when we scan the bus, we get timeout messages for
1814 * these commands if there is no device available.
1815 * other hosts report did_no_connect for the same thing.
1817 if ((scmd
->cmnd
[0] == TEST_UNIT_READY
||
1818 scmd
->cmnd
[0] == INQUIRY
)) {
1830 * next, check the message byte.
1832 if (msg_byte(scmd
->result
) != COMMAND_COMPLETE
)
1836 * check the status byte to see if this indicates anything special.
1838 switch (status_byte(scmd
->result
)) {
1840 scsi_handle_queue_full(scmd
->device
);
1842 * the case of trying to send too many commands to a
1843 * tagged queueing device.
1848 * device can't talk to us at the moment. Should only
1849 * occur (SAM-3) when the task queue is empty, so will cause
1850 * the empty queue handling to trigger a stall in the
1853 return ADD_TO_MLQUEUE
;
1855 if (scmd
->cmnd
[0] == REPORT_LUNS
)
1856 scmd
->device
->sdev_target
->expecting_lun_change
= 0;
1857 scsi_handle_queue_ramp_up(scmd
->device
);
1859 case COMMAND_TERMINATED
:
1863 case CHECK_CONDITION
:
1864 rtn
= scsi_check_sense(scmd
);
1865 if (rtn
== NEEDS_RETRY
)
1867 /* if rtn == FAILED, we have no sense information;
1868 * returning FAILED will wake the error handler thread
1869 * to collect the sense and redo the decide
1872 case CONDITION_GOOD
:
1873 case INTERMEDIATE_GOOD
:
1874 case INTERMEDIATE_C_GOOD
:
1877 * who knows? FIXME(eric)
1881 case RESERVATION_CONFLICT
:
1882 sdev_printk(KERN_INFO
, scmd
->device
,
1883 "reservation conflict\n");
1884 set_host_byte(scmd
, DID_NEXUS_FAILURE
);
1885 return SUCCESS
; /* causes immediate i/o error */
1893 /* we requeue for retry because the error was retryable, and
1894 * the request was not marked fast fail. Note that above,
1895 * even if the request is marked fast fail, we still requeue
1896 * for queue congestion conditions (QUEUE_FULL or BUSY) */
1897 if ((++scmd
->retries
) <= scmd
->allowed
1898 && !scsi_noretry_cmd(scmd
)) {
1902 * no more retries - report this one back to upper level.
1908 static void eh_lock_door_done(struct request
*req
, blk_status_t status
)
1910 __blk_put_request(req
->q
, req
);
1914 * scsi_eh_lock_door - Prevent medium removal for the specified device
1915 * @sdev: SCSI device to prevent medium removal
1918 * We must be called from process context.
1921 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1922 * head of the devices request queue, and continue.
1924 static void scsi_eh_lock_door(struct scsi_device
*sdev
)
1926 struct request
*req
;
1927 struct scsi_request
*rq
;
1930 * blk_get_request with GFP_KERNEL (__GFP_RECLAIM) sleeps until a
1931 * request becomes available
1933 req
= blk_get_request(sdev
->request_queue
, REQ_OP_SCSI_IN
, GFP_KERNEL
);
1938 rq
->cmd
[0] = ALLOW_MEDIUM_REMOVAL
;
1942 rq
->cmd
[4] = SCSI_REMOVAL_PREVENT
;
1944 rq
->cmd_len
= COMMAND_SIZE(rq
->cmd
[0]);
1946 req
->rq_flags
|= RQF_QUIET
;
1947 req
->timeout
= 10 * HZ
;
1950 blk_execute_rq_nowait(req
->q
, NULL
, req
, 1, eh_lock_door_done
);
1954 * scsi_restart_operations - restart io operations to the specified host.
1955 * @shost: Host we are restarting.
1958 * When we entered the error handler, we blocked all further i/o to
1959 * this device. we need to 'reverse' this process.
1961 static void scsi_restart_operations(struct Scsi_Host
*shost
)
1963 struct scsi_device
*sdev
;
1964 unsigned long flags
;
1967 * If the door was locked, we need to insert a door lock request
1968 * onto the head of the SCSI request queue for the device. There
1969 * is no point trying to lock the door of an off-line device.
1971 shost_for_each_device(sdev
, shost
) {
1972 if (scsi_device_online(sdev
) && sdev
->was_reset
&& sdev
->locked
) {
1973 scsi_eh_lock_door(sdev
);
1974 sdev
->was_reset
= 0;
1979 * next free up anything directly waiting upon the host. this
1980 * will be requests for character device operations, and also for
1981 * ioctls to queued block devices.
1983 SCSI_LOG_ERROR_RECOVERY(3,
1984 shost_printk(KERN_INFO
, shost
, "waking up host to restart\n"));
1986 spin_lock_irqsave(shost
->host_lock
, flags
);
1987 if (scsi_host_set_state(shost
, SHOST_RUNNING
))
1988 if (scsi_host_set_state(shost
, SHOST_CANCEL
))
1989 BUG_ON(scsi_host_set_state(shost
, SHOST_DEL
));
1990 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1992 wake_up(&shost
->host_wait
);
1995 * finally we need to re-initiate requests that may be pending. we will
1996 * have had everything blocked while error handling is taking place, and
1997 * now that error recovery is done, we will need to ensure that these
1998 * requests are started.
2000 scsi_run_host_queues(shost
);
2003 * if eh is active and host_eh_scheduled is pending we need to re-run
2004 * recovery. we do this check after scsi_run_host_queues() to allow
2005 * everything pent up since the last eh run a chance to make forward
2006 * progress before we sync again. Either we'll immediately re-run
2007 * recovery or scsi_device_unbusy() will wake us again when these
2008 * pending commands complete.
2010 spin_lock_irqsave(shost
->host_lock
, flags
);
2011 if (shost
->host_eh_scheduled
)
2012 if (scsi_host_set_state(shost
, SHOST_RECOVERY
))
2013 WARN_ON(scsi_host_set_state(shost
, SHOST_CANCEL_RECOVERY
));
2014 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2018 * scsi_eh_ready_devs - check device ready state and recover if not.
2019 * @shost: host to be recovered.
2020 * @work_q: &list_head for pending commands.
2021 * @done_q: &list_head for processed commands.
2023 void scsi_eh_ready_devs(struct Scsi_Host
*shost
,
2024 struct list_head
*work_q
,
2025 struct list_head
*done_q
)
2027 if (!scsi_eh_stu(shost
, work_q
, done_q
))
2028 if (!scsi_eh_bus_device_reset(shost
, work_q
, done_q
))
2029 if (!scsi_eh_target_reset(shost
, work_q
, done_q
))
2030 if (!scsi_eh_bus_reset(shost
, work_q
, done_q
))
2031 if (!scsi_eh_host_reset(shost
, work_q
, done_q
))
2032 scsi_eh_offline_sdevs(work_q
,
2035 EXPORT_SYMBOL_GPL(scsi_eh_ready_devs
);
2038 * scsi_eh_flush_done_q - finish processed commands or retry them.
2039 * @done_q: list_head of processed commands.
2041 void scsi_eh_flush_done_q(struct list_head
*done_q
)
2043 struct scsi_cmnd
*scmd
, *next
;
2045 list_for_each_entry_safe(scmd
, next
, done_q
, eh_entry
) {
2046 list_del_init(&scmd
->eh_entry
);
2047 if (scsi_device_online(scmd
->device
) &&
2048 !scsi_noretry_cmd(scmd
) &&
2049 (++scmd
->retries
<= scmd
->allowed
)) {
2050 SCSI_LOG_ERROR_RECOVERY(3,
2051 scmd_printk(KERN_INFO
, scmd
,
2052 "%s: flush retry cmd\n",
2054 scsi_queue_insert(scmd
, SCSI_MLQUEUE_EH_RETRY
);
2057 * If just we got sense for the device (called
2058 * scsi_eh_get_sense), scmd->result is already
2059 * set, do not set DRIVER_TIMEOUT.
2062 scmd
->result
|= (DRIVER_TIMEOUT
<< 24);
2063 SCSI_LOG_ERROR_RECOVERY(3,
2064 scmd_printk(KERN_INFO
, scmd
,
2065 "%s: flush finish cmd\n",
2067 scsi_finish_command(scmd
);
2071 EXPORT_SYMBOL(scsi_eh_flush_done_q
);
2074 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
2075 * @shost: Host to unjam.
2078 * When we come in here, we *know* that all commands on the bus have
2079 * either completed, failed or timed out. we also know that no further
2080 * commands are being sent to the host, so things are relatively quiet
2081 * and we have freedom to fiddle with things as we wish.
2083 * This is only the *default* implementation. it is possible for
2084 * individual drivers to supply their own version of this function, and
2085 * if the maintainer wishes to do this, it is strongly suggested that
2086 * this function be taken as a template and modified. this function
2087 * was designed to correctly handle problems for about 95% of the
2088 * different cases out there, and it should always provide at least a
2089 * reasonable amount of error recovery.
2091 * Any command marked 'failed' or 'timeout' must eventually have
2092 * scsi_finish_cmd() called for it. we do all of the retry stuff
2093 * here, so when we restart the host after we return it should have an
2096 static void scsi_unjam_host(struct Scsi_Host
*shost
)
2098 unsigned long flags
;
2099 LIST_HEAD(eh_work_q
);
2100 LIST_HEAD(eh_done_q
);
2102 spin_lock_irqsave(shost
->host_lock
, flags
);
2103 list_splice_init(&shost
->eh_cmd_q
, &eh_work_q
);
2104 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2106 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost
, &eh_work_q
));
2108 if (!scsi_eh_get_sense(&eh_work_q
, &eh_done_q
))
2109 scsi_eh_ready_devs(shost
, &eh_work_q
, &eh_done_q
);
2111 spin_lock_irqsave(shost
->host_lock
, flags
);
2112 if (shost
->eh_deadline
!= -1)
2113 shost
->last_reset
= 0;
2114 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2115 scsi_eh_flush_done_q(&eh_done_q
);
2119 * scsi_error_handler - SCSI error handler thread
2120 * @data: Host for which we are running.
2123 * This is the main error handling loop. This is run as a kernel thread
2124 * for every SCSI host and handles all error handling activity.
2126 int scsi_error_handler(void *data
)
2128 struct Scsi_Host
*shost
= data
;
2131 * We use TASK_INTERRUPTIBLE so that the thread is not
2132 * counted against the load average as a running process.
2133 * We never actually get interrupted because kthread_run
2134 * disables signal delivery for the created thread.
2138 * The sequence in kthread_stop() sets the stop flag first
2139 * then wakes the process. To avoid missed wakeups, the task
2140 * should always be in a non running state before the stop
2143 set_current_state(TASK_INTERRUPTIBLE
);
2144 if (kthread_should_stop())
2147 if ((shost
->host_failed
== 0 && shost
->host_eh_scheduled
== 0) ||
2148 shost
->host_failed
!= atomic_read(&shost
->host_busy
)) {
2149 SCSI_LOG_ERROR_RECOVERY(1,
2150 shost_printk(KERN_INFO
, shost
,
2151 "scsi_eh_%d: sleeping\n",
2157 __set_current_state(TASK_RUNNING
);
2158 SCSI_LOG_ERROR_RECOVERY(1,
2159 shost_printk(KERN_INFO
, shost
,
2160 "scsi_eh_%d: waking up %d/%d/%d\n",
2161 shost
->host_no
, shost
->host_eh_scheduled
,
2163 atomic_read(&shost
->host_busy
)));
2166 * We have a host that is failing for some reason. Figure out
2167 * what we need to do to get it up and online again (if we can).
2168 * If we fail, we end up taking the thing offline.
2170 if (!shost
->eh_noresume
&& scsi_autopm_get_host(shost
) != 0) {
2171 SCSI_LOG_ERROR_RECOVERY(1,
2172 shost_printk(KERN_ERR
, shost
,
2173 "scsi_eh_%d: unable to autoresume\n",
2178 if (shost
->transportt
->eh_strategy_handler
)
2179 shost
->transportt
->eh_strategy_handler(shost
);
2181 scsi_unjam_host(shost
);
2183 /* All scmds have been handled */
2184 shost
->host_failed
= 0;
2187 * Note - if the above fails completely, the action is to take
2188 * individual devices offline and flush the queue of any
2189 * outstanding requests that may have been pending. When we
2190 * restart, we restart any I/O to any other devices on the bus
2191 * which are still online.
2193 scsi_restart_operations(shost
);
2194 if (!shost
->eh_noresume
)
2195 scsi_autopm_put_host(shost
);
2197 __set_current_state(TASK_RUNNING
);
2199 SCSI_LOG_ERROR_RECOVERY(1,
2200 shost_printk(KERN_INFO
, shost
,
2201 "Error handler scsi_eh_%d exiting\n",
2203 shost
->ehandler
= NULL
;
2208 * Function: scsi_report_bus_reset()
2210 * Purpose: Utility function used by low-level drivers to report that
2211 * they have observed a bus reset on the bus being handled.
2213 * Arguments: shost - Host in question
2214 * channel - channel on which reset was observed.
2218 * Lock status: Host lock must be held.
2220 * Notes: This only needs to be called if the reset is one which
2221 * originates from an unknown location. Resets originated
2222 * by the mid-level itself don't need to call this, but there
2223 * should be no harm.
2225 * The main purpose of this is to make sure that a CHECK_CONDITION
2226 * is properly treated.
2228 void scsi_report_bus_reset(struct Scsi_Host
*shost
, int channel
)
2230 struct scsi_device
*sdev
;
2232 __shost_for_each_device(sdev
, shost
) {
2233 if (channel
== sdev_channel(sdev
))
2234 __scsi_report_device_reset(sdev
, NULL
);
2237 EXPORT_SYMBOL(scsi_report_bus_reset
);
2240 * Function: scsi_report_device_reset()
2242 * Purpose: Utility function used by low-level drivers to report that
2243 * they have observed a device reset on the device being handled.
2245 * Arguments: shost - Host in question
2246 * channel - channel on which reset was observed
2247 * target - target on which reset was observed
2251 * Lock status: Host lock must be held
2253 * Notes: This only needs to be called if the reset is one which
2254 * originates from an unknown location. Resets originated
2255 * by the mid-level itself don't need to call this, but there
2256 * should be no harm.
2258 * The main purpose of this is to make sure that a CHECK_CONDITION
2259 * is properly treated.
2261 void scsi_report_device_reset(struct Scsi_Host
*shost
, int channel
, int target
)
2263 struct scsi_device
*sdev
;
2265 __shost_for_each_device(sdev
, shost
) {
2266 if (channel
== sdev_channel(sdev
) &&
2267 target
== sdev_id(sdev
))
2268 __scsi_report_device_reset(sdev
, NULL
);
2271 EXPORT_SYMBOL(scsi_report_device_reset
);
2274 scsi_reset_provider_done_command(struct scsi_cmnd
*scmd
)
2279 * scsi_ioctl_reset: explicitly reset a host/bus/target/device
2280 * @dev: scsi_device to operate on
2281 * @arg: reset type (see sg.h)
2284 scsi_ioctl_reset(struct scsi_device
*dev
, int __user
*arg
)
2286 struct scsi_cmnd
*scmd
;
2287 struct Scsi_Host
*shost
= dev
->host
;
2289 unsigned long flags
;
2290 int error
= 0, rtn
, val
;
2292 if (!capable(CAP_SYS_ADMIN
) || !capable(CAP_SYS_RAWIO
))
2295 error
= get_user(val
, arg
);
2299 if (scsi_autopm_get_host(shost
) < 0)
2303 rq
= kzalloc(sizeof(struct request
) + sizeof(struct scsi_cmnd
) +
2304 shost
->hostt
->cmd_size
, GFP_KERNEL
);
2306 goto out_put_autopm_host
;
2307 blk_rq_init(NULL
, rq
);
2309 scmd
= (struct scsi_cmnd
*)(rq
+ 1);
2310 scsi_init_command(dev
, scmd
);
2312 scmd
->cmnd
= scsi_req(rq
)->cmd
;
2314 scmd
->scsi_done
= scsi_reset_provider_done_command
;
2315 memset(&scmd
->sdb
, 0, sizeof(scmd
->sdb
));
2319 scmd
->sc_data_direction
= DMA_BIDIRECTIONAL
;
2321 spin_lock_irqsave(shost
->host_lock
, flags
);
2322 shost
->tmf_in_progress
= 1;
2323 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2325 switch (val
& ~SG_SCSI_RESET_NO_ESCALATE
) {
2326 case SG_SCSI_RESET_NOTHING
:
2329 case SG_SCSI_RESET_DEVICE
:
2330 rtn
= scsi_try_bus_device_reset(scmd
);
2331 if (rtn
== SUCCESS
|| (val
& SG_SCSI_RESET_NO_ESCALATE
))
2334 case SG_SCSI_RESET_TARGET
:
2335 rtn
= scsi_try_target_reset(scmd
);
2336 if (rtn
== SUCCESS
|| (val
& SG_SCSI_RESET_NO_ESCALATE
))
2339 case SG_SCSI_RESET_BUS
:
2340 rtn
= scsi_try_bus_reset(scmd
);
2341 if (rtn
== SUCCESS
|| (val
& SG_SCSI_RESET_NO_ESCALATE
))
2344 case SG_SCSI_RESET_HOST
:
2345 rtn
= scsi_try_host_reset(scmd
);
2354 error
= (rtn
== SUCCESS
) ? 0 : -EIO
;
2356 spin_lock_irqsave(shost
->host_lock
, flags
);
2357 shost
->tmf_in_progress
= 0;
2358 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2361 * be sure to wake up anyone who was sleeping or had their queue
2362 * suspended while we performed the TMF.
2364 SCSI_LOG_ERROR_RECOVERY(3,
2365 shost_printk(KERN_INFO
, shost
,
2366 "waking up host to restart after TMF\n"));
2368 wake_up(&shost
->host_wait
);
2369 scsi_run_host_queues(shost
);
2371 scsi_put_command(scmd
);
2374 out_put_autopm_host
:
2375 scsi_autopm_put_host(shost
);
2378 EXPORT_SYMBOL(scsi_ioctl_reset
);
2380 bool scsi_command_normalize_sense(const struct scsi_cmnd
*cmd
,
2381 struct scsi_sense_hdr
*sshdr
)
2383 return scsi_normalize_sense(cmd
->sense_buffer
,
2384 SCSI_SENSE_BUFFERSIZE
, sshdr
);
2386 EXPORT_SYMBOL(scsi_command_normalize_sense
);
2389 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
2390 * @sense_buffer: byte array of sense data
2391 * @sb_len: number of valid bytes in sense_buffer
2392 * @info_out: pointer to 64 integer where 8 or 4 byte information
2393 * field will be placed if found.
2396 * true if information field found, false if not found.
2398 bool scsi_get_sense_info_fld(const u8
*sense_buffer
, int sb_len
,
2405 switch (sense_buffer
[0] & 0x7f) {
2408 if (sense_buffer
[0] & 0x80) {
2409 *info_out
= get_unaligned_be32(&sense_buffer
[3]);
2415 ucp
= scsi_sense_desc_find(sense_buffer
, sb_len
,
2417 if (ucp
&& (0xa == ucp
[1])) {
2418 *info_out
= get_unaligned_be64(&ucp
[4]);
2426 EXPORT_SYMBOL(scsi_get_sense_info_fld
);