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 /* called with shost->host_lock held */
65 void scsi_eh_wakeup(struct Scsi_Host
*shost
)
67 if (atomic_read(&shost
->host_busy
) == shost
->host_failed
) {
68 trace_scsi_eh_wakeup(shost
);
69 wake_up_process(shost
->ehandler
);
70 SCSI_LOG_ERROR_RECOVERY(5, shost_printk(KERN_INFO
, shost
,
71 "Waking error handler thread\n"));
76 * scsi_schedule_eh - schedule EH for SCSI host
77 * @shost: SCSI host to invoke error handling on.
79 * Schedule SCSI EH without scmd.
81 void scsi_schedule_eh(struct Scsi_Host
*shost
)
85 spin_lock_irqsave(shost
->host_lock
, flags
);
87 if (scsi_host_set_state(shost
, SHOST_RECOVERY
) == 0 ||
88 scsi_host_set_state(shost
, SHOST_CANCEL_RECOVERY
) == 0) {
89 shost
->host_eh_scheduled
++;
90 scsi_eh_wakeup(shost
);
93 spin_unlock_irqrestore(shost
->host_lock
, flags
);
95 EXPORT_SYMBOL_GPL(scsi_schedule_eh
);
97 static int scsi_host_eh_past_deadline(struct Scsi_Host
*shost
)
99 if (!shost
->last_reset
|| shost
->eh_deadline
== -1)
103 * 32bit accesses are guaranteed to be atomic
104 * (on all supported architectures), so instead
105 * of using a spinlock we can as well double check
106 * if eh_deadline has been set to 'off' during the
109 if (time_before(jiffies
, shost
->last_reset
+ shost
->eh_deadline
) &&
110 shost
->eh_deadline
> -1)
117 * scmd_eh_abort_handler - Handle command aborts
118 * @work: command to be aborted.
121 scmd_eh_abort_handler(struct work_struct
*work
)
123 struct scsi_cmnd
*scmd
=
124 container_of(work
, struct scsi_cmnd
, abort_work
.work
);
125 struct scsi_device
*sdev
= scmd
->device
;
128 if (scsi_host_eh_past_deadline(sdev
->host
)) {
129 SCSI_LOG_ERROR_RECOVERY(3,
130 scmd_printk(KERN_INFO
, scmd
,
131 "eh timeout, not aborting\n"));
133 SCSI_LOG_ERROR_RECOVERY(3,
134 scmd_printk(KERN_INFO
, scmd
,
135 "aborting command\n"));
136 rtn
= scsi_try_to_abort_cmd(sdev
->host
->hostt
, scmd
);
137 if (rtn
== SUCCESS
) {
138 set_host_byte(scmd
, DID_TIME_OUT
);
139 if (scsi_host_eh_past_deadline(sdev
->host
)) {
140 SCSI_LOG_ERROR_RECOVERY(3,
141 scmd_printk(KERN_INFO
, scmd
,
142 "eh timeout, not retrying "
143 "aborted command\n"));
144 } else if (!scsi_noretry_cmd(scmd
) &&
145 (++scmd
->retries
<= scmd
->allowed
)) {
146 SCSI_LOG_ERROR_RECOVERY(3,
147 scmd_printk(KERN_WARNING
, scmd
,
148 "retry aborted command\n"));
149 scsi_queue_insert(scmd
, SCSI_MLQUEUE_EH_RETRY
);
152 SCSI_LOG_ERROR_RECOVERY(3,
153 scmd_printk(KERN_WARNING
, scmd
,
154 "finish aborted command\n"));
155 scsi_finish_command(scmd
);
159 SCSI_LOG_ERROR_RECOVERY(3,
160 scmd_printk(KERN_INFO
, scmd
,
162 (rtn
== FAST_IO_FAIL
) ?
163 "not send" : "failed"));
167 scsi_eh_scmd_add(scmd
);
171 * scsi_abort_command - schedule a command abort
172 * @scmd: scmd to abort.
174 * We only need to abort commands after a command timeout
177 scsi_abort_command(struct scsi_cmnd
*scmd
)
179 struct scsi_device
*sdev
= scmd
->device
;
180 struct Scsi_Host
*shost
= sdev
->host
;
183 if (scmd
->eh_eflags
& SCSI_EH_ABORT_SCHEDULED
) {
185 * Retry after abort failed, escalate to next level.
187 SCSI_LOG_ERROR_RECOVERY(3,
188 scmd_printk(KERN_INFO
, scmd
,
189 "previous abort failed\n"));
190 BUG_ON(delayed_work_pending(&scmd
->abort_work
));
194 spin_lock_irqsave(shost
->host_lock
, flags
);
195 if (shost
->eh_deadline
!= -1 && !shost
->last_reset
)
196 shost
->last_reset
= jiffies
;
197 spin_unlock_irqrestore(shost
->host_lock
, flags
);
199 scmd
->eh_eflags
|= SCSI_EH_ABORT_SCHEDULED
;
200 SCSI_LOG_ERROR_RECOVERY(3,
201 scmd_printk(KERN_INFO
, scmd
, "abort scheduled\n"));
202 queue_delayed_work(shost
->tmf_work_q
, &scmd
->abort_work
, HZ
/ 100);
207 * scsi_eh_reset - call into ->eh_action to reset internal counters
208 * @scmd: scmd to run eh on.
210 * The scsi driver might be carrying internal state about the
211 * devices, so we need to call into the driver to reset the
212 * internal state once the error handler is started.
214 static void scsi_eh_reset(struct scsi_cmnd
*scmd
)
216 if (!blk_rq_is_passthrough(scmd
->request
)) {
217 struct scsi_driver
*sdrv
= scsi_cmd_to_driver(scmd
);
219 sdrv
->eh_reset(scmd
);
224 * scsi_eh_scmd_add - add scsi cmd to error handling.
225 * @scmd: scmd to run eh on.
227 void scsi_eh_scmd_add(struct scsi_cmnd
*scmd
)
229 struct Scsi_Host
*shost
= scmd
->device
->host
;
233 WARN_ON_ONCE(!shost
->ehandler
);
235 spin_lock_irqsave(shost
->host_lock
, flags
);
236 if (scsi_host_set_state(shost
, SHOST_RECOVERY
)) {
237 ret
= scsi_host_set_state(shost
, SHOST_CANCEL_RECOVERY
);
240 if (shost
->eh_deadline
!= -1 && !shost
->last_reset
)
241 shost
->last_reset
= jiffies
;
244 list_add_tail(&scmd
->eh_entry
, &shost
->eh_cmd_q
);
245 shost
->host_failed
++;
246 scsi_eh_wakeup(shost
);
247 spin_unlock_irqrestore(shost
->host_lock
, flags
);
251 * scsi_times_out - Timeout function for normal scsi commands.
252 * @req: request that is timing out.
255 * We do not need to lock this. There is the potential for a race
256 * only in that the normal completion handling might run, but if the
257 * normal completion function determines that the timer has already
258 * fired, then it mustn't do anything.
260 enum blk_eh_timer_return
scsi_times_out(struct request
*req
)
262 struct scsi_cmnd
*scmd
= blk_mq_rq_to_pdu(req
);
263 enum blk_eh_timer_return rtn
= BLK_EH_NOT_HANDLED
;
264 struct Scsi_Host
*host
= scmd
->device
->host
;
266 trace_scsi_dispatch_cmd_timeout(scmd
);
267 scsi_log_completion(scmd
, TIMEOUT_ERROR
);
269 if (host
->eh_deadline
!= -1 && !host
->last_reset
)
270 host
->last_reset
= jiffies
;
272 if (host
->hostt
->eh_timed_out
)
273 rtn
= host
->hostt
->eh_timed_out(scmd
);
275 if (rtn
== BLK_EH_NOT_HANDLED
) {
276 if (scsi_abort_command(scmd
) != SUCCESS
) {
277 set_host_byte(scmd
, DID_TIME_OUT
);
278 scsi_eh_scmd_add(scmd
);
286 * scsi_block_when_processing_errors - Prevent cmds from being queued.
287 * @sdev: Device on which we are performing recovery.
290 * We block until the host is out of error recovery, and then check to
291 * see whether the host or the device is offline.
294 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
296 int scsi_block_when_processing_errors(struct scsi_device
*sdev
)
300 wait_event(sdev
->host
->host_wait
, !scsi_host_in_recovery(sdev
->host
));
302 online
= scsi_device_online(sdev
);
304 SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_INFO
, sdev
,
305 "%s: rtn: %d\n", __func__
, online
));
309 EXPORT_SYMBOL(scsi_block_when_processing_errors
);
311 #ifdef CONFIG_SCSI_LOGGING
313 * scsi_eh_prt_fail_stats - Log info on failures.
314 * @shost: scsi host being recovered.
315 * @work_q: Queue of scsi cmds to process.
317 static inline void scsi_eh_prt_fail_stats(struct Scsi_Host
*shost
,
318 struct list_head
*work_q
)
320 struct scsi_cmnd
*scmd
;
321 struct scsi_device
*sdev
;
322 int total_failures
= 0;
325 int devices_failed
= 0;
327 shost_for_each_device(sdev
, shost
) {
328 list_for_each_entry(scmd
, work_q
, eh_entry
) {
329 if (scmd
->device
== sdev
) {
331 if (scmd
->eh_eflags
& SCSI_EH_ABORT_SCHEDULED
)
338 if (cmd_cancel
|| cmd_failed
) {
339 SCSI_LOG_ERROR_RECOVERY(3,
340 shost_printk(KERN_INFO
, shost
,
341 "%s: cmds failed: %d, cancel: %d\n",
342 __func__
, cmd_failed
,
350 SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO
, shost
,
351 "Total of %d commands on %d"
352 " devices require eh work\n",
353 total_failures
, devices_failed
));
358 * scsi_report_lun_change - Set flag on all *other* devices on the same target
359 * to indicate that a UNIT ATTENTION is expected.
360 * @sdev: Device reporting the UNIT ATTENTION
362 static void scsi_report_lun_change(struct scsi_device
*sdev
)
364 sdev
->sdev_target
->expecting_lun_change
= 1;
368 * scsi_report_sense - Examine scsi sense information and log messages for
369 * certain conditions, also issue uevents for some of them.
370 * @sdev: Device reporting the sense code
371 * @sshdr: sshdr to be examined
373 static void scsi_report_sense(struct scsi_device
*sdev
,
374 struct scsi_sense_hdr
*sshdr
)
376 enum scsi_device_event evt_type
= SDEV_EVT_MAXBITS
; /* i.e. none */
378 if (sshdr
->sense_key
== UNIT_ATTENTION
) {
379 if (sshdr
->asc
== 0x3f && sshdr
->ascq
== 0x03) {
380 evt_type
= SDEV_EVT_INQUIRY_CHANGE_REPORTED
;
381 sdev_printk(KERN_WARNING
, sdev
,
382 "Inquiry data has changed");
383 } else if (sshdr
->asc
== 0x3f && sshdr
->ascq
== 0x0e) {
384 evt_type
= SDEV_EVT_LUN_CHANGE_REPORTED
;
385 scsi_report_lun_change(sdev
);
386 sdev_printk(KERN_WARNING
, sdev
,
387 "Warning! Received an indication that the "
388 "LUN assignments on this target have "
389 "changed. The Linux SCSI layer does not "
390 "automatically remap LUN assignments.\n");
391 } else if (sshdr
->asc
== 0x3f)
392 sdev_printk(KERN_WARNING
, sdev
,
393 "Warning! Received an indication that the "
394 "operating parameters on this target have "
395 "changed. The Linux SCSI layer does not "
396 "automatically adjust these parameters.\n");
398 if (sshdr
->asc
== 0x38 && sshdr
->ascq
== 0x07) {
399 evt_type
= SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED
;
400 sdev_printk(KERN_WARNING
, sdev
,
401 "Warning! Received an indication that the "
402 "LUN reached a thin provisioning soft "
406 if (sshdr
->asc
== 0x29) {
407 evt_type
= SDEV_EVT_POWER_ON_RESET_OCCURRED
;
408 sdev_printk(KERN_WARNING
, sdev
,
409 "Power-on or device reset occurred\n");
412 if (sshdr
->asc
== 0x2a && sshdr
->ascq
== 0x01) {
413 evt_type
= SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED
;
414 sdev_printk(KERN_WARNING
, sdev
,
415 "Mode parameters changed");
416 } else if (sshdr
->asc
== 0x2a && sshdr
->ascq
== 0x06) {
417 evt_type
= SDEV_EVT_ALUA_STATE_CHANGE_REPORTED
;
418 sdev_printk(KERN_WARNING
, sdev
,
419 "Asymmetric access state changed");
420 } else if (sshdr
->asc
== 0x2a && sshdr
->ascq
== 0x09) {
421 evt_type
= SDEV_EVT_CAPACITY_CHANGE_REPORTED
;
422 sdev_printk(KERN_WARNING
, sdev
,
423 "Capacity data has changed");
424 } else if (sshdr
->asc
== 0x2a)
425 sdev_printk(KERN_WARNING
, sdev
,
426 "Parameters changed");
429 if (evt_type
!= SDEV_EVT_MAXBITS
) {
430 set_bit(evt_type
, sdev
->pending_events
);
431 schedule_work(&sdev
->event_work
);
436 * scsi_check_sense - Examine scsi cmd sense
437 * @scmd: Cmd to have sense checked.
440 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
443 * When a deferred error is detected the current command has
444 * not been executed and needs retrying.
446 int scsi_check_sense(struct scsi_cmnd
*scmd
)
448 struct scsi_device
*sdev
= scmd
->device
;
449 struct scsi_sense_hdr sshdr
;
451 if (! scsi_command_normalize_sense(scmd
, &sshdr
))
452 return FAILED
; /* no valid sense data */
454 scsi_report_sense(sdev
, &sshdr
);
456 if (scsi_sense_is_deferred(&sshdr
))
459 if (sdev
->handler
&& sdev
->handler
->check_sense
) {
462 rc
= sdev
->handler
->check_sense(sdev
, &sshdr
);
463 if (rc
!= SCSI_RETURN_NOT_HANDLED
)
465 /* handler does not care. Drop down to default handling */
468 if (scmd
->cmnd
[0] == TEST_UNIT_READY
&& scmd
->scsi_done
!= scsi_eh_done
)
470 * nasty: for mid-layer issued TURs, we need to return the
471 * actual sense data without any recovery attempt. For eh
472 * issued ones, we need to try to recover and interpret
477 * Previous logic looked for FILEMARK, EOM or ILI which are
478 * mainly associated with tapes and returned SUCCESS.
480 if (sshdr
.response_code
== 0x70) {
482 if (scmd
->sense_buffer
[2] & 0xe0)
486 * descriptor format: look for "stream commands sense data
487 * descriptor" (see SSC-3). Assume single sense data
488 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
490 if ((sshdr
.additional_length
> 3) &&
491 (scmd
->sense_buffer
[8] == 0x4) &&
492 (scmd
->sense_buffer
[11] & 0xe0))
496 switch (sshdr
.sense_key
) {
499 case RECOVERED_ERROR
:
500 return /* soft_error */ SUCCESS
;
502 case ABORTED_COMMAND
:
503 if (sshdr
.asc
== 0x10) /* DIF */
510 * if we are expecting a cc/ua because of a bus reset that we
511 * performed, treat this just as a retry. otherwise this is
512 * information that we should pass up to the upper-level driver
513 * so that we can deal with it there.
515 if (scmd
->device
->expecting_cc_ua
) {
517 * Because some device does not queue unit
518 * attentions correctly, we carefully check
519 * additional sense code and qualifier so as
520 * not to squash media change unit attention.
522 if (sshdr
.asc
!= 0x28 || sshdr
.ascq
!= 0x00) {
523 scmd
->device
->expecting_cc_ua
= 0;
528 * we might also expect a cc/ua if another LUN on the target
529 * reported a UA with an ASC/ASCQ of 3F 0E -
530 * REPORTED LUNS DATA HAS CHANGED.
532 if (scmd
->device
->sdev_target
->expecting_lun_change
&&
533 sshdr
.asc
== 0x3f && sshdr
.ascq
== 0x0e)
536 * if the device is in the process of becoming ready, we
539 if ((sshdr
.asc
== 0x04) && (sshdr
.ascq
== 0x01))
542 * if the device is not started, we need to wake
543 * the error handler to start the motor
545 if (scmd
->device
->allow_restart
&&
546 (sshdr
.asc
== 0x04) && (sshdr
.ascq
== 0x02))
549 * Pass the UA upwards for a determination in the completion
554 /* these are not supported */
556 if (sshdr
.asc
== 0x27 && sshdr
.ascq
== 0x07) {
557 /* Thin provisioning hard threshold reached */
558 set_host_byte(scmd
, DID_ALLOC_FAILURE
);
563 case VOLUME_OVERFLOW
:
566 set_host_byte(scmd
, DID_TARGET_FAILURE
);
570 if (sshdr
.asc
== 0x11 || /* UNRECOVERED READ ERR */
571 sshdr
.asc
== 0x13 || /* AMNF DATA FIELD */
572 sshdr
.asc
== 0x14) { /* RECORD NOT FOUND */
573 set_host_byte(scmd
, DID_MEDIUM_ERROR
);
579 if (scmd
->device
->retry_hwerror
)
580 return ADD_TO_MLQUEUE
;
582 set_host_byte(scmd
, DID_TARGET_FAILURE
);
585 case ILLEGAL_REQUEST
:
586 if (sshdr
.asc
== 0x20 || /* Invalid command operation code */
587 sshdr
.asc
== 0x21 || /* Logical block address out of range */
588 sshdr
.asc
== 0x22 || /* Invalid function */
589 sshdr
.asc
== 0x24 || /* Invalid field in cdb */
590 sshdr
.asc
== 0x26 || /* Parameter value invalid */
591 sshdr
.asc
== 0x27) { /* Write protected */
592 set_host_byte(scmd
, DID_TARGET_FAILURE
);
600 EXPORT_SYMBOL_GPL(scsi_check_sense
);
602 static void scsi_handle_queue_ramp_up(struct scsi_device
*sdev
)
604 struct scsi_host_template
*sht
= sdev
->host
->hostt
;
605 struct scsi_device
*tmp_sdev
;
607 if (!sht
->track_queue_depth
||
608 sdev
->queue_depth
>= sdev
->max_queue_depth
)
611 if (time_before(jiffies
,
612 sdev
->last_queue_ramp_up
+ sdev
->queue_ramp_up_period
))
615 if (time_before(jiffies
,
616 sdev
->last_queue_full_time
+ sdev
->queue_ramp_up_period
))
620 * Walk all devices of a target and do
623 shost_for_each_device(tmp_sdev
, sdev
->host
) {
624 if (tmp_sdev
->channel
!= sdev
->channel
||
625 tmp_sdev
->id
!= sdev
->id
||
626 tmp_sdev
->queue_depth
== sdev
->max_queue_depth
)
629 scsi_change_queue_depth(tmp_sdev
, tmp_sdev
->queue_depth
+ 1);
630 sdev
->last_queue_ramp_up
= jiffies
;
634 static void scsi_handle_queue_full(struct scsi_device
*sdev
)
636 struct scsi_host_template
*sht
= sdev
->host
->hostt
;
637 struct scsi_device
*tmp_sdev
;
639 if (!sht
->track_queue_depth
)
642 shost_for_each_device(tmp_sdev
, sdev
->host
) {
643 if (tmp_sdev
->channel
!= sdev
->channel
||
644 tmp_sdev
->id
!= sdev
->id
)
647 * We do not know the number of commands that were at
648 * the device when we got the queue full so we start
649 * from the highest possible value and work our way down.
651 scsi_track_queue_full(tmp_sdev
, tmp_sdev
->queue_depth
- 1);
656 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
657 * @scmd: SCSI cmd to examine.
660 * This is *only* called when we are examining the status of commands
661 * queued during error recovery. the main difference here is that we
662 * don't allow for the possibility of retries here, and we are a lot
663 * more restrictive about what we consider acceptable.
665 static int scsi_eh_completed_normally(struct scsi_cmnd
*scmd
)
668 * first check the host byte, to see if there is anything in there
669 * that would indicate what we need to do.
671 if (host_byte(scmd
->result
) == DID_RESET
) {
673 * rats. we are already in the error handler, so we now
674 * get to try and figure out what to do next. if the sense
675 * is valid, we have a pretty good idea of what to do.
676 * if not, we mark it as FAILED.
678 return scsi_check_sense(scmd
);
680 if (host_byte(scmd
->result
) != DID_OK
)
684 * next, check the message byte.
686 if (msg_byte(scmd
->result
) != COMMAND_COMPLETE
)
690 * now, check the status byte to see if this indicates
693 switch (status_byte(scmd
->result
)) {
695 scsi_handle_queue_ramp_up(scmd
->device
);
697 case COMMAND_TERMINATED
:
699 case CHECK_CONDITION
:
700 return scsi_check_sense(scmd
);
702 case INTERMEDIATE_GOOD
:
703 case INTERMEDIATE_C_GOOD
:
705 * who knows? FIXME(eric)
708 case RESERVATION_CONFLICT
:
709 if (scmd
->cmnd
[0] == TEST_UNIT_READY
)
710 /* it is a success, we probed the device and
713 /* otherwise, we failed to send the command */
716 scsi_handle_queue_full(scmd
->device
);
727 * scsi_eh_done - Completion function for error handling.
728 * @scmd: Cmd that is done.
730 static void scsi_eh_done(struct scsi_cmnd
*scmd
)
732 struct completion
*eh_action
;
734 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO
, scmd
,
735 "%s result: %x\n", __func__
, scmd
->result
));
737 eh_action
= scmd
->device
->host
->eh_action
;
743 * scsi_try_host_reset - ask host adapter to reset itself
744 * @scmd: SCSI cmd to send host reset.
746 static int scsi_try_host_reset(struct scsi_cmnd
*scmd
)
750 struct Scsi_Host
*host
= scmd
->device
->host
;
751 struct scsi_host_template
*hostt
= host
->hostt
;
753 SCSI_LOG_ERROR_RECOVERY(3,
754 shost_printk(KERN_INFO
, host
, "Snd Host RST\n"));
756 if (!hostt
->eh_host_reset_handler
)
759 rtn
= hostt
->eh_host_reset_handler(scmd
);
761 if (rtn
== SUCCESS
) {
762 if (!hostt
->skip_settle_delay
)
763 ssleep(HOST_RESET_SETTLE_TIME
);
764 spin_lock_irqsave(host
->host_lock
, flags
);
765 scsi_report_bus_reset(host
, scmd_channel(scmd
));
766 spin_unlock_irqrestore(host
->host_lock
, flags
);
773 * scsi_try_bus_reset - ask host to perform a bus reset
774 * @scmd: SCSI cmd to send bus reset.
776 static int scsi_try_bus_reset(struct scsi_cmnd
*scmd
)
780 struct Scsi_Host
*host
= scmd
->device
->host
;
781 struct scsi_host_template
*hostt
= host
->hostt
;
783 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO
, scmd
,
784 "%s: Snd Bus RST\n", __func__
));
786 if (!hostt
->eh_bus_reset_handler
)
789 rtn
= hostt
->eh_bus_reset_handler(scmd
);
791 if (rtn
== SUCCESS
) {
792 if (!hostt
->skip_settle_delay
)
793 ssleep(BUS_RESET_SETTLE_TIME
);
794 spin_lock_irqsave(host
->host_lock
, flags
);
795 scsi_report_bus_reset(host
, scmd_channel(scmd
));
796 spin_unlock_irqrestore(host
->host_lock
, flags
);
802 static void __scsi_report_device_reset(struct scsi_device
*sdev
, void *data
)
805 sdev
->expecting_cc_ua
= 1;
809 * scsi_try_target_reset - Ask host to perform a target reset
810 * @scmd: SCSI cmd used to send a target reset
813 * There is no timeout for this operation. if this operation is
814 * unreliable for a given host, then the host itself needs to put a
815 * timer on it, and set the host back to a consistent state prior to
818 static int scsi_try_target_reset(struct scsi_cmnd
*scmd
)
822 struct Scsi_Host
*host
= scmd
->device
->host
;
823 struct scsi_host_template
*hostt
= host
->hostt
;
825 if (!hostt
->eh_target_reset_handler
)
828 rtn
= hostt
->eh_target_reset_handler(scmd
);
829 if (rtn
== SUCCESS
) {
830 spin_lock_irqsave(host
->host_lock
, flags
);
831 __starget_for_each_device(scsi_target(scmd
->device
), NULL
,
832 __scsi_report_device_reset
);
833 spin_unlock_irqrestore(host
->host_lock
, flags
);
840 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
841 * @scmd: SCSI cmd used to send BDR
844 * There is no timeout for this operation. if this operation is
845 * unreliable for a given host, then the host itself needs to put a
846 * timer on it, and set the host back to a consistent state prior to
849 static int scsi_try_bus_device_reset(struct scsi_cmnd
*scmd
)
852 struct scsi_host_template
*hostt
= scmd
->device
->host
->hostt
;
854 if (!hostt
->eh_device_reset_handler
)
857 rtn
= hostt
->eh_device_reset_handler(scmd
);
859 __scsi_report_device_reset(scmd
->device
, NULL
);
864 * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
865 * @hostt: SCSI driver host template
866 * @scmd: SCSI cmd used to send a target reset
869 * SUCCESS, FAILED, or FAST_IO_FAIL
872 * SUCCESS does not necessarily indicate that the command
873 * has been aborted; it only indicates that the LLDDs
874 * has cleared all references to that command.
875 * LLDDs should return FAILED only if an abort was required
876 * but could not be executed. LLDDs should return FAST_IO_FAIL
877 * if the device is temporarily unavailable (eg due to a
878 * link down on FibreChannel)
880 static int scsi_try_to_abort_cmd(struct scsi_host_template
*hostt
,
881 struct scsi_cmnd
*scmd
)
883 if (!hostt
->eh_abort_handler
)
886 return hostt
->eh_abort_handler(scmd
);
889 static void scsi_abort_eh_cmnd(struct scsi_cmnd
*scmd
)
891 if (scsi_try_to_abort_cmd(scmd
->device
->host
->hostt
, scmd
) != SUCCESS
)
892 if (scsi_try_bus_device_reset(scmd
) != SUCCESS
)
893 if (scsi_try_target_reset(scmd
) != SUCCESS
)
894 if (scsi_try_bus_reset(scmd
) != SUCCESS
)
895 scsi_try_host_reset(scmd
);
899 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
900 * @scmd: SCSI command structure to hijack
901 * @ses: structure to save restore information
902 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
903 * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
904 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
906 * This function is used to save a scsi command information before re-execution
907 * as part of the error recovery process. If @sense_bytes is 0 the command
908 * sent must be one that does not transfer any data. If @sense_bytes != 0
909 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
910 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
912 void scsi_eh_prep_cmnd(struct scsi_cmnd
*scmd
, struct scsi_eh_save
*ses
,
913 unsigned char *cmnd
, int cmnd_size
, unsigned sense_bytes
)
915 struct scsi_device
*sdev
= scmd
->device
;
918 * We need saved copies of a number of fields - this is because
919 * error handling may need to overwrite these with different values
920 * to run different commands, and once error handling is complete,
921 * we will need to restore these values prior to running the actual
924 ses
->cmd_len
= scmd
->cmd_len
;
925 ses
->cmnd
= scmd
->cmnd
;
926 ses
->data_direction
= scmd
->sc_data_direction
;
927 ses
->sdb
= scmd
->sdb
;
928 ses
->next_rq
= scmd
->request
->next_rq
;
929 ses
->result
= scmd
->result
;
930 ses
->underflow
= scmd
->underflow
;
931 ses
->prot_op
= scmd
->prot_op
;
932 ses
->eh_eflags
= scmd
->eh_eflags
;
934 scmd
->prot_op
= SCSI_PROT_NORMAL
;
936 scmd
->cmnd
= ses
->eh_cmnd
;
937 memset(scmd
->cmnd
, 0, BLK_MAX_CDB
);
938 memset(&scmd
->sdb
, 0, sizeof(scmd
->sdb
));
939 scmd
->request
->next_rq
= NULL
;
943 scmd
->sdb
.length
= min_t(unsigned, SCSI_SENSE_BUFFERSIZE
,
945 sg_init_one(&ses
->sense_sgl
, scmd
->sense_buffer
,
947 scmd
->sdb
.table
.sgl
= &ses
->sense_sgl
;
948 scmd
->sc_data_direction
= DMA_FROM_DEVICE
;
949 scmd
->sdb
.table
.nents
= scmd
->sdb
.table
.orig_nents
= 1;
950 scmd
->cmnd
[0] = REQUEST_SENSE
;
951 scmd
->cmnd
[4] = scmd
->sdb
.length
;
952 scmd
->cmd_len
= COMMAND_SIZE(scmd
->cmnd
[0]);
954 scmd
->sc_data_direction
= DMA_NONE
;
956 BUG_ON(cmnd_size
> BLK_MAX_CDB
);
957 memcpy(scmd
->cmnd
, cmnd
, cmnd_size
);
958 scmd
->cmd_len
= COMMAND_SIZE(scmd
->cmnd
[0]);
964 if (sdev
->scsi_level
<= SCSI_2
&& sdev
->scsi_level
!= SCSI_UNKNOWN
)
965 scmd
->cmnd
[1] = (scmd
->cmnd
[1] & 0x1f) |
966 (sdev
->lun
<< 5 & 0xe0);
969 * Zero the sense buffer. The scsi spec mandates that any
970 * untransferred sense data should be interpreted as being zero.
972 memset(scmd
->sense_buffer
, 0, SCSI_SENSE_BUFFERSIZE
);
974 EXPORT_SYMBOL(scsi_eh_prep_cmnd
);
977 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
978 * @scmd: SCSI command structure to restore
979 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
981 * Undo any damage done by above scsi_eh_prep_cmnd().
983 void scsi_eh_restore_cmnd(struct scsi_cmnd
* scmd
, struct scsi_eh_save
*ses
)
986 * Restore original data
988 scmd
->cmd_len
= ses
->cmd_len
;
989 scmd
->cmnd
= ses
->cmnd
;
990 scmd
->sc_data_direction
= ses
->data_direction
;
991 scmd
->sdb
= ses
->sdb
;
992 scmd
->request
->next_rq
= ses
->next_rq
;
993 scmd
->result
= ses
->result
;
994 scmd
->underflow
= ses
->underflow
;
995 scmd
->prot_op
= ses
->prot_op
;
996 scmd
->eh_eflags
= ses
->eh_eflags
;
998 EXPORT_SYMBOL(scsi_eh_restore_cmnd
);
1001 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
1002 * @scmd: SCSI command structure to hijack
1003 * @cmnd: CDB to send
1004 * @cmnd_size: size in bytes of @cmnd
1005 * @timeout: timeout for this request
1006 * @sense_bytes: size of sense data to copy or 0
1008 * This function is used to send a scsi command down to a target device
1009 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
1012 * SUCCESS or FAILED or NEEDS_RETRY
1014 static int scsi_send_eh_cmnd(struct scsi_cmnd
*scmd
, unsigned char *cmnd
,
1015 int cmnd_size
, int timeout
, unsigned sense_bytes
)
1017 struct scsi_device
*sdev
= scmd
->device
;
1018 struct Scsi_Host
*shost
= sdev
->host
;
1019 DECLARE_COMPLETION_ONSTACK(done
);
1020 unsigned long timeleft
= timeout
;
1021 struct scsi_eh_save ses
;
1022 const unsigned long stall_for
= msecs_to_jiffies(100);
1026 scsi_eh_prep_cmnd(scmd
, &ses
, cmnd
, cmnd_size
, sense_bytes
);
1027 shost
->eh_action
= &done
;
1029 scsi_log_send(scmd
);
1030 scmd
->scsi_done
= scsi_eh_done
;
1031 rtn
= shost
->hostt
->queuecommand(shost
, scmd
);
1033 if (timeleft
> stall_for
) {
1034 scsi_eh_restore_cmnd(scmd
, &ses
);
1035 timeleft
-= stall_for
;
1036 msleep(jiffies_to_msecs(stall_for
));
1039 /* signal not to enter either branch of the if () below */
1043 timeleft
= wait_for_completion_timeout(&done
, timeout
);
1047 shost
->eh_action
= NULL
;
1049 scsi_log_completion(scmd
, rtn
);
1051 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO
, scmd
,
1052 "%s timeleft: %ld\n",
1053 __func__
, timeleft
));
1056 * If there is time left scsi_eh_done got called, and we will examine
1057 * the actual status codes to see whether the command actually did
1058 * complete normally, else if we have a zero return and no time left,
1059 * the command must still be pending, so abort it and return FAILED.
1060 * If we never actually managed to issue the command, because
1061 * ->queuecommand() kept returning non zero, use the rtn = FAILED
1062 * value above (so don't execute either branch of the if)
1065 rtn
= scsi_eh_completed_normally(scmd
);
1066 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO
, scmd
,
1067 "%s: scsi_eh_completed_normally %x\n", __func__
, rtn
));
1074 case ADD_TO_MLQUEUE
:
1081 } else if (rtn
!= FAILED
) {
1082 scsi_abort_eh_cmnd(scmd
);
1086 scsi_eh_restore_cmnd(scmd
, &ses
);
1092 * scsi_request_sense - Request sense data from a particular target.
1093 * @scmd: SCSI cmd for request sense.
1096 * Some hosts automatically obtain this information, others require
1097 * that we obtain it on our own. This function will *not* return until
1098 * the command either times out, or it completes.
1100 static int scsi_request_sense(struct scsi_cmnd
*scmd
)
1102 return scsi_send_eh_cmnd(scmd
, NULL
, 0, scmd
->device
->eh_timeout
, ~0);
1105 static int scsi_eh_action(struct scsi_cmnd
*scmd
, int rtn
)
1107 if (!blk_rq_is_passthrough(scmd
->request
)) {
1108 struct scsi_driver
*sdrv
= scsi_cmd_to_driver(scmd
);
1109 if (sdrv
->eh_action
)
1110 rtn
= sdrv
->eh_action(scmd
, rtn
);
1116 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
1117 * @scmd: Original SCSI cmd that eh has finished.
1118 * @done_q: Queue for processed commands.
1121 * We don't want to use the normal command completion while we are are
1122 * still handling errors - it may cause other commands to be queued,
1123 * and that would disturb what we are doing. Thus we really want to
1124 * keep a list of pending commands for final completion, and once we
1125 * are ready to leave error handling we handle completion for real.
1127 void scsi_eh_finish_cmd(struct scsi_cmnd
*scmd
, struct list_head
*done_q
)
1129 list_move_tail(&scmd
->eh_entry
, done_q
);
1131 EXPORT_SYMBOL(scsi_eh_finish_cmd
);
1134 * scsi_eh_get_sense - Get device sense data.
1135 * @work_q: Queue of commands to process.
1136 * @done_q: Queue of processed commands.
1139 * See if we need to request sense information. if so, then get it
1140 * now, so we have a better idea of what to do.
1143 * This has the unfortunate side effect that if a shost adapter does
1144 * not automatically request sense information, we end up shutting
1145 * it down before we request it.
1147 * All drivers should request sense information internally these days,
1148 * so for now all I have to say is tough noogies if you end up in here.
1150 * XXX: Long term this code should go away, but that needs an audit of
1153 int scsi_eh_get_sense(struct list_head
*work_q
,
1154 struct list_head
*done_q
)
1156 struct scsi_cmnd
*scmd
, *next
;
1157 struct Scsi_Host
*shost
;
1161 * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
1162 * should not get sense.
1164 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1165 if ((scmd
->eh_eflags
& SCSI_EH_ABORT_SCHEDULED
) ||
1166 SCSI_SENSE_VALID(scmd
))
1169 shost
= scmd
->device
->host
;
1170 if (scsi_host_eh_past_deadline(shost
)) {
1171 SCSI_LOG_ERROR_RECOVERY(3,
1172 scmd_printk(KERN_INFO
, scmd
,
1173 "%s: skip request sense, past eh deadline\n",
1177 if (status_byte(scmd
->result
) != CHECK_CONDITION
)
1179 * don't request sense if there's no check condition
1180 * status because the error we're processing isn't one
1181 * that has a sense code (and some devices get
1182 * confused by sense requests out of the blue)
1186 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO
, scmd
,
1187 "%s: requesting sense\n",
1189 rtn
= scsi_request_sense(scmd
);
1193 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO
, scmd
,
1194 "sense requested, result %x\n", scmd
->result
));
1195 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd
));
1197 rtn
= scsi_decide_disposition(scmd
);
1200 * if the result was normal, then just pass it along to the
1204 /* we don't want this command reissued, just
1205 * finished with the sense data, so set
1206 * retries to the max allowed to ensure it
1207 * won't get reissued */
1208 scmd
->retries
= scmd
->allowed
;
1209 else if (rtn
!= NEEDS_RETRY
)
1212 scsi_eh_finish_cmd(scmd
, done_q
);
1215 return list_empty(work_q
);
1217 EXPORT_SYMBOL_GPL(scsi_eh_get_sense
);
1220 * scsi_eh_tur - Send TUR to device.
1221 * @scmd: &scsi_cmnd to send TUR
1224 * 0 - Device is ready. 1 - Device NOT ready.
1226 static int scsi_eh_tur(struct scsi_cmnd
*scmd
)
1228 static unsigned char tur_command
[6] = {TEST_UNIT_READY
, 0, 0, 0, 0, 0};
1229 int retry_cnt
= 1, rtn
;
1232 rtn
= scsi_send_eh_cmnd(scmd
, tur_command
, 6,
1233 scmd
->device
->eh_timeout
, 0);
1235 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO
, scmd
,
1236 "%s return: %x\n", __func__
, rtn
));
1251 * scsi_eh_test_devices - check if devices are responding from error recovery.
1252 * @cmd_list: scsi commands in error recovery.
1253 * @work_q: queue for commands which still need more error recovery
1254 * @done_q: queue for commands which are finished
1255 * @try_stu: boolean on if a STU command should be tried in addition to TUR.
1258 * Tests if devices are in a working state. Commands to devices now in
1259 * a working state are sent to the done_q while commands to devices which
1260 * are still failing to respond are returned to the work_q for more
1263 static int scsi_eh_test_devices(struct list_head
*cmd_list
,
1264 struct list_head
*work_q
,
1265 struct list_head
*done_q
, int try_stu
)
1267 struct scsi_cmnd
*scmd
, *next
;
1268 struct scsi_device
*sdev
;
1271 while (!list_empty(cmd_list
)) {
1272 scmd
= list_entry(cmd_list
->next
, struct scsi_cmnd
, eh_entry
);
1273 sdev
= scmd
->device
;
1276 if (scsi_host_eh_past_deadline(sdev
->host
)) {
1277 /* Push items back onto work_q */
1278 list_splice_init(cmd_list
, work_q
);
1279 SCSI_LOG_ERROR_RECOVERY(3,
1280 sdev_printk(KERN_INFO
, sdev
,
1281 "%s: skip test device, past eh deadline",
1287 finish_cmds
= !scsi_device_online(scmd
->device
) ||
1288 (try_stu
&& !scsi_eh_try_stu(scmd
) &&
1289 !scsi_eh_tur(scmd
)) ||
1292 list_for_each_entry_safe(scmd
, next
, cmd_list
, eh_entry
)
1293 if (scmd
->device
== sdev
) {
1296 scsi_eh_action(scmd
, SUCCESS
) == SUCCESS
))
1297 scsi_eh_finish_cmd(scmd
, done_q
);
1299 list_move_tail(&scmd
->eh_entry
, work_q
);
1302 return list_empty(work_q
);
1306 * scsi_eh_try_stu - Send START_UNIT to device.
1307 * @scmd: &scsi_cmnd to send START_UNIT
1310 * 0 - Device is ready. 1 - Device NOT ready.
1312 static int scsi_eh_try_stu(struct scsi_cmnd
*scmd
)
1314 static unsigned char stu_command
[6] = {START_STOP
, 0, 0, 0, 1, 0};
1316 if (scmd
->device
->allow_restart
) {
1317 int i
, rtn
= NEEDS_RETRY
;
1319 for (i
= 0; rtn
== NEEDS_RETRY
&& i
< 2; i
++)
1320 rtn
= scsi_send_eh_cmnd(scmd
, stu_command
, 6, scmd
->device
->request_queue
->rq_timeout
, 0);
1330 * scsi_eh_stu - send START_UNIT if needed
1331 * @shost: &scsi host being recovered.
1332 * @work_q: &list_head for pending commands.
1333 * @done_q: &list_head for processed commands.
1336 * If commands are failing due to not ready, initializing command required,
1337 * try revalidating the device, which will end up sending a start unit.
1339 static int scsi_eh_stu(struct Scsi_Host
*shost
,
1340 struct list_head
*work_q
,
1341 struct list_head
*done_q
)
1343 struct scsi_cmnd
*scmd
, *stu_scmd
, *next
;
1344 struct scsi_device
*sdev
;
1346 shost_for_each_device(sdev
, shost
) {
1347 if (scsi_host_eh_past_deadline(shost
)) {
1348 SCSI_LOG_ERROR_RECOVERY(3,
1349 sdev_printk(KERN_INFO
, sdev
,
1350 "%s: skip START_UNIT, past eh deadline\n",
1355 list_for_each_entry(scmd
, work_q
, eh_entry
)
1356 if (scmd
->device
== sdev
&& SCSI_SENSE_VALID(scmd
) &&
1357 scsi_check_sense(scmd
) == FAILED
) {
1365 SCSI_LOG_ERROR_RECOVERY(3,
1366 sdev_printk(KERN_INFO
, sdev
,
1367 "%s: Sending START_UNIT\n",
1370 if (!scsi_eh_try_stu(stu_scmd
)) {
1371 if (!scsi_device_online(sdev
) ||
1372 !scsi_eh_tur(stu_scmd
)) {
1373 list_for_each_entry_safe(scmd
, next
,
1375 if (scmd
->device
== sdev
&&
1376 scsi_eh_action(scmd
, SUCCESS
) == SUCCESS
)
1377 scsi_eh_finish_cmd(scmd
, done_q
);
1381 SCSI_LOG_ERROR_RECOVERY(3,
1382 sdev_printk(KERN_INFO
, sdev
,
1383 "%s: START_UNIT failed\n",
1388 return list_empty(work_q
);
1393 * scsi_eh_bus_device_reset - send bdr if needed
1394 * @shost: scsi host being recovered.
1395 * @work_q: &list_head for pending commands.
1396 * @done_q: &list_head for processed commands.
1399 * Try a bus device reset. Still, look to see whether we have multiple
1400 * devices that are jammed or not - if we have multiple devices, it
1401 * makes no sense to try bus_device_reset - we really would need to try
1402 * a bus_reset instead.
1404 static int scsi_eh_bus_device_reset(struct Scsi_Host
*shost
,
1405 struct list_head
*work_q
,
1406 struct list_head
*done_q
)
1408 struct scsi_cmnd
*scmd
, *bdr_scmd
, *next
;
1409 struct scsi_device
*sdev
;
1412 shost_for_each_device(sdev
, shost
) {
1413 if (scsi_host_eh_past_deadline(shost
)) {
1414 SCSI_LOG_ERROR_RECOVERY(3,
1415 sdev_printk(KERN_INFO
, sdev
,
1416 "%s: skip BDR, past eh deadline\n",
1421 list_for_each_entry(scmd
, work_q
, eh_entry
)
1422 if (scmd
->device
== sdev
) {
1430 SCSI_LOG_ERROR_RECOVERY(3,
1431 sdev_printk(KERN_INFO
, sdev
,
1432 "%s: Sending BDR\n", current
->comm
));
1433 rtn
= scsi_try_bus_device_reset(bdr_scmd
);
1434 if (rtn
== SUCCESS
|| rtn
== FAST_IO_FAIL
) {
1435 if (!scsi_device_online(sdev
) ||
1436 rtn
== FAST_IO_FAIL
||
1437 !scsi_eh_tur(bdr_scmd
)) {
1438 list_for_each_entry_safe(scmd
, next
,
1440 if (scmd
->device
== sdev
&&
1441 scsi_eh_action(scmd
, rtn
) != FAILED
)
1442 scsi_eh_finish_cmd(scmd
,
1447 SCSI_LOG_ERROR_RECOVERY(3,
1448 sdev_printk(KERN_INFO
, sdev
,
1449 "%s: BDR failed\n", current
->comm
));
1453 return list_empty(work_q
);
1457 * scsi_eh_target_reset - send target reset if needed
1458 * @shost: scsi host being recovered.
1459 * @work_q: &list_head for pending commands.
1460 * @done_q: &list_head for processed commands.
1463 * Try a target reset.
1465 static int scsi_eh_target_reset(struct Scsi_Host
*shost
,
1466 struct list_head
*work_q
,
1467 struct list_head
*done_q
)
1469 LIST_HEAD(tmp_list
);
1470 LIST_HEAD(check_list
);
1472 list_splice_init(work_q
, &tmp_list
);
1474 while (!list_empty(&tmp_list
)) {
1475 struct scsi_cmnd
*next
, *scmd
;
1479 if (scsi_host_eh_past_deadline(shost
)) {
1480 /* push back on work queue for further processing */
1481 list_splice_init(&check_list
, work_q
);
1482 list_splice_init(&tmp_list
, work_q
);
1483 SCSI_LOG_ERROR_RECOVERY(3,
1484 shost_printk(KERN_INFO
, shost
,
1485 "%s: Skip target reset, past eh deadline\n",
1487 return list_empty(work_q
);
1490 scmd
= list_entry(tmp_list
.next
, struct scsi_cmnd
, eh_entry
);
1493 SCSI_LOG_ERROR_RECOVERY(3,
1494 shost_printk(KERN_INFO
, shost
,
1495 "%s: Sending target reset to target %d\n",
1496 current
->comm
, id
));
1497 rtn
= scsi_try_target_reset(scmd
);
1498 if (rtn
!= SUCCESS
&& rtn
!= FAST_IO_FAIL
)
1499 SCSI_LOG_ERROR_RECOVERY(3,
1500 shost_printk(KERN_INFO
, shost
,
1501 "%s: Target reset failed"
1503 current
->comm
, id
));
1504 list_for_each_entry_safe(scmd
, next
, &tmp_list
, eh_entry
) {
1505 if (scmd_id(scmd
) != id
)
1509 list_move_tail(&scmd
->eh_entry
, &check_list
);
1510 else if (rtn
== FAST_IO_FAIL
)
1511 scsi_eh_finish_cmd(scmd
, done_q
);
1513 /* push back on work queue for further processing */
1514 list_move(&scmd
->eh_entry
, work_q
);
1518 return scsi_eh_test_devices(&check_list
, work_q
, done_q
, 0);
1522 * scsi_eh_bus_reset - send a bus reset
1523 * @shost: &scsi host being recovered.
1524 * @work_q: &list_head for pending commands.
1525 * @done_q: &list_head for processed commands.
1527 static int scsi_eh_bus_reset(struct Scsi_Host
*shost
,
1528 struct list_head
*work_q
,
1529 struct list_head
*done_q
)
1531 struct scsi_cmnd
*scmd
, *chan_scmd
, *next
;
1532 LIST_HEAD(check_list
);
1533 unsigned int channel
;
1537 * we really want to loop over the various channels, and do this on
1538 * a channel by channel basis. we should also check to see if any
1539 * of the failed commands are on soft_reset devices, and if so, skip
1543 for (channel
= 0; channel
<= shost
->max_channel
; channel
++) {
1544 if (scsi_host_eh_past_deadline(shost
)) {
1545 list_splice_init(&check_list
, work_q
);
1546 SCSI_LOG_ERROR_RECOVERY(3,
1547 shost_printk(KERN_INFO
, shost
,
1548 "%s: skip BRST, past eh deadline\n",
1550 return list_empty(work_q
);
1554 list_for_each_entry(scmd
, work_q
, eh_entry
) {
1555 if (channel
== scmd_channel(scmd
)) {
1559 * FIXME add back in some support for
1560 * soft_reset devices.
1567 SCSI_LOG_ERROR_RECOVERY(3,
1568 shost_printk(KERN_INFO
, shost
,
1569 "%s: Sending BRST chan: %d\n",
1570 current
->comm
, channel
));
1571 rtn
= scsi_try_bus_reset(chan_scmd
);
1572 if (rtn
== SUCCESS
|| rtn
== FAST_IO_FAIL
) {
1573 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1574 if (channel
== scmd_channel(scmd
)) {
1575 if (rtn
== FAST_IO_FAIL
)
1576 scsi_eh_finish_cmd(scmd
,
1579 list_move_tail(&scmd
->eh_entry
,
1584 SCSI_LOG_ERROR_RECOVERY(3,
1585 shost_printk(KERN_INFO
, shost
,
1586 "%s: BRST failed chan: %d\n",
1587 current
->comm
, channel
));
1590 return scsi_eh_test_devices(&check_list
, work_q
, done_q
, 0);
1594 * scsi_eh_host_reset - send a host reset
1595 * @shost: host to be reset.
1596 * @work_q: &list_head for pending commands.
1597 * @done_q: &list_head for processed commands.
1599 static int scsi_eh_host_reset(struct Scsi_Host
*shost
,
1600 struct list_head
*work_q
,
1601 struct list_head
*done_q
)
1603 struct scsi_cmnd
*scmd
, *next
;
1604 LIST_HEAD(check_list
);
1607 if (!list_empty(work_q
)) {
1608 scmd
= list_entry(work_q
->next
,
1609 struct scsi_cmnd
, eh_entry
);
1611 SCSI_LOG_ERROR_RECOVERY(3,
1612 shost_printk(KERN_INFO
, shost
,
1613 "%s: Sending HRST\n",
1616 rtn
= scsi_try_host_reset(scmd
);
1617 if (rtn
== SUCCESS
) {
1618 list_splice_init(work_q
, &check_list
);
1619 } else if (rtn
== FAST_IO_FAIL
) {
1620 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1621 scsi_eh_finish_cmd(scmd
, done_q
);
1624 SCSI_LOG_ERROR_RECOVERY(3,
1625 shost_printk(KERN_INFO
, shost
,
1626 "%s: HRST failed\n",
1630 return scsi_eh_test_devices(&check_list
, work_q
, done_q
, 1);
1634 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
1635 * @work_q: &list_head for pending commands.
1636 * @done_q: &list_head for processed commands.
1638 static void scsi_eh_offline_sdevs(struct list_head
*work_q
,
1639 struct list_head
*done_q
)
1641 struct scsi_cmnd
*scmd
, *next
;
1642 struct scsi_device
*sdev
;
1644 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1645 sdev_printk(KERN_INFO
, scmd
->device
, "Device offlined - "
1646 "not ready after error recovery\n");
1647 sdev
= scmd
->device
;
1649 mutex_lock(&sdev
->state_mutex
);
1650 scsi_device_set_state(sdev
, SDEV_OFFLINE
);
1651 mutex_unlock(&sdev
->state_mutex
);
1653 scsi_eh_finish_cmd(scmd
, done_q
);
1659 * scsi_noretry_cmd - determine if command should be failed fast
1660 * @scmd: SCSI cmd to examine.
1662 int scsi_noretry_cmd(struct scsi_cmnd
*scmd
)
1664 switch (host_byte(scmd
->result
)) {
1670 return (scmd
->request
->cmd_flags
& REQ_FAILFAST_TRANSPORT
);
1672 return (scmd
->request
->cmd_flags
& REQ_FAILFAST_DEV
);
1674 if (msg_byte(scmd
->result
) == COMMAND_COMPLETE
&&
1675 status_byte(scmd
->result
) == RESERVATION_CONFLICT
)
1678 case DID_SOFT_ERROR
:
1679 return (scmd
->request
->cmd_flags
& REQ_FAILFAST_DRIVER
);
1682 if (status_byte(scmd
->result
) != CHECK_CONDITION
)
1687 * assume caller has checked sense and determined
1688 * the check condition was retryable.
1690 if (scmd
->request
->cmd_flags
& REQ_FAILFAST_DEV
||
1691 blk_rq_is_passthrough(scmd
->request
))
1698 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1699 * @scmd: SCSI cmd to examine.
1702 * This is *only* called when we are examining the status after sending
1703 * out the actual data command. any commands that are queued for error
1704 * recovery (e.g. test_unit_ready) do *not* come through here.
1706 * When this routine returns failed, it means the error handler thread
1707 * is woken. In cases where the error code indicates an error that
1708 * doesn't require the error handler read (i.e. we don't need to
1709 * abort/reset), this function should return SUCCESS.
1711 int scsi_decide_disposition(struct scsi_cmnd
*scmd
)
1716 * if the device is offline, then we clearly just pass the result back
1717 * up to the top level.
1719 if (!scsi_device_online(scmd
->device
)) {
1720 SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO
, scmd
,
1721 "%s: device offline - report as SUCCESS\n", __func__
));
1726 * first check the host byte, to see if there is anything in there
1727 * that would indicate what we need to do.
1729 switch (host_byte(scmd
->result
)) {
1730 case DID_PASSTHROUGH
:
1732 * no matter what, pass this through to the upper layer.
1733 * nuke this special code so that it looks like we are saying
1736 scmd
->result
&= 0xff00ffff;
1740 * looks good. drop through, and check the next byte.
1744 if (scmd
->eh_eflags
& SCSI_EH_ABORT_SCHEDULED
) {
1745 set_host_byte(scmd
, DID_TIME_OUT
);
1749 case DID_NO_CONNECT
:
1750 case DID_BAD_TARGET
:
1752 * note - this means that we just report the status back
1753 * to the top level driver, not that we actually think
1754 * that it indicates SUCCESS.
1757 case DID_SOFT_ERROR
:
1759 * when the low level driver returns did_soft_error,
1760 * it is responsible for keeping an internal retry counter
1761 * in order to avoid endless loops (db)
1768 return ADD_TO_MLQUEUE
;
1769 case DID_TRANSPORT_DISRUPTED
:
1771 * LLD/transport was disrupted during processing of the IO.
1772 * The transport class is now blocked/blocking,
1773 * and the transport will decide what to do with the IO
1774 * based on its timers and recovery capablilities if
1775 * there are enough retries.
1778 case DID_TRANSPORT_FAILFAST
:
1780 * The transport decided to failfast the IO (most likely
1781 * the fast io fail tmo fired), so send IO directly upwards.
1785 if (msg_byte(scmd
->result
) == COMMAND_COMPLETE
&&
1786 status_byte(scmd
->result
) == RESERVATION_CONFLICT
)
1788 * execute reservation conflict processing code
1798 * when we scan the bus, we get timeout messages for
1799 * these commands if there is no device available.
1800 * other hosts report did_no_connect for the same thing.
1802 if ((scmd
->cmnd
[0] == TEST_UNIT_READY
||
1803 scmd
->cmnd
[0] == INQUIRY
)) {
1815 * next, check the message byte.
1817 if (msg_byte(scmd
->result
) != COMMAND_COMPLETE
)
1821 * check the status byte to see if this indicates anything special.
1823 switch (status_byte(scmd
->result
)) {
1825 scsi_handle_queue_full(scmd
->device
);
1827 * the case of trying to send too many commands to a
1828 * tagged queueing device.
1833 * device can't talk to us at the moment. Should only
1834 * occur (SAM-3) when the task queue is empty, so will cause
1835 * the empty queue handling to trigger a stall in the
1838 return ADD_TO_MLQUEUE
;
1840 if (scmd
->cmnd
[0] == REPORT_LUNS
)
1841 scmd
->device
->sdev_target
->expecting_lun_change
= 0;
1842 scsi_handle_queue_ramp_up(scmd
->device
);
1844 case COMMAND_TERMINATED
:
1848 case CHECK_CONDITION
:
1849 rtn
= scsi_check_sense(scmd
);
1850 if (rtn
== NEEDS_RETRY
)
1852 /* if rtn == FAILED, we have no sense information;
1853 * returning FAILED will wake the error handler thread
1854 * to collect the sense and redo the decide
1857 case CONDITION_GOOD
:
1858 case INTERMEDIATE_GOOD
:
1859 case INTERMEDIATE_C_GOOD
:
1862 * who knows? FIXME(eric)
1866 case RESERVATION_CONFLICT
:
1867 sdev_printk(KERN_INFO
, scmd
->device
,
1868 "reservation conflict\n");
1869 set_host_byte(scmd
, DID_NEXUS_FAILURE
);
1870 return SUCCESS
; /* causes immediate i/o error */
1878 /* we requeue for retry because the error was retryable, and
1879 * the request was not marked fast fail. Note that above,
1880 * even if the request is marked fast fail, we still requeue
1881 * for queue congestion conditions (QUEUE_FULL or BUSY) */
1882 if ((++scmd
->retries
) <= scmd
->allowed
1883 && !scsi_noretry_cmd(scmd
)) {
1887 * no more retries - report this one back to upper level.
1893 static void eh_lock_door_done(struct request
*req
, blk_status_t status
)
1895 __blk_put_request(req
->q
, req
);
1899 * scsi_eh_lock_door - Prevent medium removal for the specified device
1900 * @sdev: SCSI device to prevent medium removal
1903 * We must be called from process context.
1906 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1907 * head of the devices request queue, and continue.
1909 static void scsi_eh_lock_door(struct scsi_device
*sdev
)
1911 struct request
*req
;
1912 struct scsi_request
*rq
;
1915 * blk_get_request with GFP_KERNEL (__GFP_RECLAIM) sleeps until a
1916 * request becomes available
1918 req
= blk_get_request(sdev
->request_queue
, REQ_OP_SCSI_IN
, GFP_KERNEL
);
1923 rq
->cmd
[0] = ALLOW_MEDIUM_REMOVAL
;
1927 rq
->cmd
[4] = SCSI_REMOVAL_PREVENT
;
1929 rq
->cmd_len
= COMMAND_SIZE(rq
->cmd
[0]);
1931 req
->rq_flags
|= RQF_QUIET
;
1932 req
->timeout
= 10 * HZ
;
1935 blk_execute_rq_nowait(req
->q
, NULL
, req
, 1, eh_lock_door_done
);
1939 * scsi_restart_operations - restart io operations to the specified host.
1940 * @shost: Host we are restarting.
1943 * When we entered the error handler, we blocked all further i/o to
1944 * this device. we need to 'reverse' this process.
1946 static void scsi_restart_operations(struct Scsi_Host
*shost
)
1948 struct scsi_device
*sdev
;
1949 unsigned long flags
;
1952 * If the door was locked, we need to insert a door lock request
1953 * onto the head of the SCSI request queue for the device. There
1954 * is no point trying to lock the door of an off-line device.
1956 shost_for_each_device(sdev
, shost
) {
1957 if (scsi_device_online(sdev
) && sdev
->was_reset
&& sdev
->locked
) {
1958 scsi_eh_lock_door(sdev
);
1959 sdev
->was_reset
= 0;
1964 * next free up anything directly waiting upon the host. this
1965 * will be requests for character device operations, and also for
1966 * ioctls to queued block devices.
1968 SCSI_LOG_ERROR_RECOVERY(3,
1969 shost_printk(KERN_INFO
, shost
, "waking up host to restart\n"));
1971 spin_lock_irqsave(shost
->host_lock
, flags
);
1972 if (scsi_host_set_state(shost
, SHOST_RUNNING
))
1973 if (scsi_host_set_state(shost
, SHOST_CANCEL
))
1974 BUG_ON(scsi_host_set_state(shost
, SHOST_DEL
));
1975 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1977 wake_up(&shost
->host_wait
);
1980 * finally we need to re-initiate requests that may be pending. we will
1981 * have had everything blocked while error handling is taking place, and
1982 * now that error recovery is done, we will need to ensure that these
1983 * requests are started.
1985 scsi_run_host_queues(shost
);
1988 * if eh is active and host_eh_scheduled is pending we need to re-run
1989 * recovery. we do this check after scsi_run_host_queues() to allow
1990 * everything pent up since the last eh run a chance to make forward
1991 * progress before we sync again. Either we'll immediately re-run
1992 * recovery or scsi_device_unbusy() will wake us again when these
1993 * pending commands complete.
1995 spin_lock_irqsave(shost
->host_lock
, flags
);
1996 if (shost
->host_eh_scheduled
)
1997 if (scsi_host_set_state(shost
, SHOST_RECOVERY
))
1998 WARN_ON(scsi_host_set_state(shost
, SHOST_CANCEL_RECOVERY
));
1999 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2003 * scsi_eh_ready_devs - check device ready state and recover if not.
2004 * @shost: host to be recovered.
2005 * @work_q: &list_head for pending commands.
2006 * @done_q: &list_head for processed commands.
2008 void scsi_eh_ready_devs(struct Scsi_Host
*shost
,
2009 struct list_head
*work_q
,
2010 struct list_head
*done_q
)
2012 if (!scsi_eh_stu(shost
, work_q
, done_q
))
2013 if (!scsi_eh_bus_device_reset(shost
, work_q
, done_q
))
2014 if (!scsi_eh_target_reset(shost
, work_q
, done_q
))
2015 if (!scsi_eh_bus_reset(shost
, work_q
, done_q
))
2016 if (!scsi_eh_host_reset(shost
, work_q
, done_q
))
2017 scsi_eh_offline_sdevs(work_q
,
2020 EXPORT_SYMBOL_GPL(scsi_eh_ready_devs
);
2023 * scsi_eh_flush_done_q - finish processed commands or retry them.
2024 * @done_q: list_head of processed commands.
2026 void scsi_eh_flush_done_q(struct list_head
*done_q
)
2028 struct scsi_cmnd
*scmd
, *next
;
2030 list_for_each_entry_safe(scmd
, next
, done_q
, eh_entry
) {
2031 list_del_init(&scmd
->eh_entry
);
2032 if (scsi_device_online(scmd
->device
) &&
2033 !scsi_noretry_cmd(scmd
) &&
2034 (++scmd
->retries
<= scmd
->allowed
)) {
2035 SCSI_LOG_ERROR_RECOVERY(3,
2036 scmd_printk(KERN_INFO
, scmd
,
2037 "%s: flush retry cmd\n",
2039 scsi_queue_insert(scmd
, SCSI_MLQUEUE_EH_RETRY
);
2042 * If just we got sense for the device (called
2043 * scsi_eh_get_sense), scmd->result is already
2044 * set, do not set DRIVER_TIMEOUT.
2047 scmd
->result
|= (DRIVER_TIMEOUT
<< 24);
2048 SCSI_LOG_ERROR_RECOVERY(3,
2049 scmd_printk(KERN_INFO
, scmd
,
2050 "%s: flush finish cmd\n",
2052 scsi_finish_command(scmd
);
2056 EXPORT_SYMBOL(scsi_eh_flush_done_q
);
2059 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
2060 * @shost: Host to unjam.
2063 * When we come in here, we *know* that all commands on the bus have
2064 * either completed, failed or timed out. we also know that no further
2065 * commands are being sent to the host, so things are relatively quiet
2066 * and we have freedom to fiddle with things as we wish.
2068 * This is only the *default* implementation. it is possible for
2069 * individual drivers to supply their own version of this function, and
2070 * if the maintainer wishes to do this, it is strongly suggested that
2071 * this function be taken as a template and modified. this function
2072 * was designed to correctly handle problems for about 95% of the
2073 * different cases out there, and it should always provide at least a
2074 * reasonable amount of error recovery.
2076 * Any command marked 'failed' or 'timeout' must eventually have
2077 * scsi_finish_cmd() called for it. we do all of the retry stuff
2078 * here, so when we restart the host after we return it should have an
2081 static void scsi_unjam_host(struct Scsi_Host
*shost
)
2083 unsigned long flags
;
2084 LIST_HEAD(eh_work_q
);
2085 LIST_HEAD(eh_done_q
);
2087 spin_lock_irqsave(shost
->host_lock
, flags
);
2088 list_splice_init(&shost
->eh_cmd_q
, &eh_work_q
);
2089 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2091 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost
, &eh_work_q
));
2093 if (!scsi_eh_get_sense(&eh_work_q
, &eh_done_q
))
2094 scsi_eh_ready_devs(shost
, &eh_work_q
, &eh_done_q
);
2096 spin_lock_irqsave(shost
->host_lock
, flags
);
2097 if (shost
->eh_deadline
!= -1)
2098 shost
->last_reset
= 0;
2099 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2100 scsi_eh_flush_done_q(&eh_done_q
);
2104 * scsi_error_handler - SCSI error handler thread
2105 * @data: Host for which we are running.
2108 * This is the main error handling loop. This is run as a kernel thread
2109 * for every SCSI host and handles all error handling activity.
2111 int scsi_error_handler(void *data
)
2113 struct Scsi_Host
*shost
= data
;
2116 * We use TASK_INTERRUPTIBLE so that the thread is not
2117 * counted against the load average as a running process.
2118 * We never actually get interrupted because kthread_run
2119 * disables signal delivery for the created thread.
2123 * The sequence in kthread_stop() sets the stop flag first
2124 * then wakes the process. To avoid missed wakeups, the task
2125 * should always be in a non running state before the stop
2128 set_current_state(TASK_INTERRUPTIBLE
);
2129 if (kthread_should_stop())
2132 if ((shost
->host_failed
== 0 && shost
->host_eh_scheduled
== 0) ||
2133 shost
->host_failed
!= atomic_read(&shost
->host_busy
)) {
2134 SCSI_LOG_ERROR_RECOVERY(1,
2135 shost_printk(KERN_INFO
, shost
,
2136 "scsi_eh_%d: sleeping\n",
2142 __set_current_state(TASK_RUNNING
);
2143 SCSI_LOG_ERROR_RECOVERY(1,
2144 shost_printk(KERN_INFO
, shost
,
2145 "scsi_eh_%d: waking up %d/%d/%d\n",
2146 shost
->host_no
, shost
->host_eh_scheduled
,
2148 atomic_read(&shost
->host_busy
)));
2151 * We have a host that is failing for some reason. Figure out
2152 * what we need to do to get it up and online again (if we can).
2153 * If we fail, we end up taking the thing offline.
2155 if (!shost
->eh_noresume
&& scsi_autopm_get_host(shost
) != 0) {
2156 SCSI_LOG_ERROR_RECOVERY(1,
2157 shost_printk(KERN_ERR
, shost
,
2158 "scsi_eh_%d: unable to autoresume\n",
2163 if (shost
->transportt
->eh_strategy_handler
)
2164 shost
->transportt
->eh_strategy_handler(shost
);
2166 scsi_unjam_host(shost
);
2168 /* All scmds have been handled */
2169 shost
->host_failed
= 0;
2172 * Note - if the above fails completely, the action is to take
2173 * individual devices offline and flush the queue of any
2174 * outstanding requests that may have been pending. When we
2175 * restart, we restart any I/O to any other devices on the bus
2176 * which are still online.
2178 scsi_restart_operations(shost
);
2179 if (!shost
->eh_noresume
)
2180 scsi_autopm_put_host(shost
);
2182 __set_current_state(TASK_RUNNING
);
2184 SCSI_LOG_ERROR_RECOVERY(1,
2185 shost_printk(KERN_INFO
, shost
,
2186 "Error handler scsi_eh_%d exiting\n",
2188 shost
->ehandler
= NULL
;
2193 * Function: scsi_report_bus_reset()
2195 * Purpose: Utility function used by low-level drivers to report that
2196 * they have observed a bus reset on the bus being handled.
2198 * Arguments: shost - Host in question
2199 * channel - channel on which reset was observed.
2203 * Lock status: Host lock must be held.
2205 * Notes: This only needs to be called if the reset is one which
2206 * originates from an unknown location. Resets originated
2207 * by the mid-level itself don't need to call this, but there
2208 * should be no harm.
2210 * The main purpose of this is to make sure that a CHECK_CONDITION
2211 * is properly treated.
2213 void scsi_report_bus_reset(struct Scsi_Host
*shost
, int channel
)
2215 struct scsi_device
*sdev
;
2217 __shost_for_each_device(sdev
, shost
) {
2218 if (channel
== sdev_channel(sdev
))
2219 __scsi_report_device_reset(sdev
, NULL
);
2222 EXPORT_SYMBOL(scsi_report_bus_reset
);
2225 * Function: scsi_report_device_reset()
2227 * Purpose: Utility function used by low-level drivers to report that
2228 * they have observed a device reset on the device being handled.
2230 * Arguments: shost - Host in question
2231 * channel - channel on which reset was observed
2232 * target - target on which reset was observed
2236 * Lock status: Host lock must be held
2238 * Notes: This only needs to be called if the reset is one which
2239 * originates from an unknown location. Resets originated
2240 * by the mid-level itself don't need to call this, but there
2241 * should be no harm.
2243 * The main purpose of this is to make sure that a CHECK_CONDITION
2244 * is properly treated.
2246 void scsi_report_device_reset(struct Scsi_Host
*shost
, int channel
, int target
)
2248 struct scsi_device
*sdev
;
2250 __shost_for_each_device(sdev
, shost
) {
2251 if (channel
== sdev_channel(sdev
) &&
2252 target
== sdev_id(sdev
))
2253 __scsi_report_device_reset(sdev
, NULL
);
2256 EXPORT_SYMBOL(scsi_report_device_reset
);
2259 scsi_reset_provider_done_command(struct scsi_cmnd
*scmd
)
2264 * scsi_ioctl_reset: explicitly reset a host/bus/target/device
2265 * @dev: scsi_device to operate on
2266 * @arg: reset type (see sg.h)
2269 scsi_ioctl_reset(struct scsi_device
*dev
, int __user
*arg
)
2271 struct scsi_cmnd
*scmd
;
2272 struct Scsi_Host
*shost
= dev
->host
;
2274 unsigned long flags
;
2275 int error
= 0, rtn
, val
;
2277 if (!capable(CAP_SYS_ADMIN
) || !capable(CAP_SYS_RAWIO
))
2280 error
= get_user(val
, arg
);
2284 if (scsi_autopm_get_host(shost
) < 0)
2288 rq
= kzalloc(sizeof(struct request
) + sizeof(struct scsi_cmnd
) +
2289 shost
->hostt
->cmd_size
, GFP_KERNEL
);
2291 goto out_put_autopm_host
;
2292 blk_rq_init(NULL
, rq
);
2294 scmd
= (struct scsi_cmnd
*)(rq
+ 1);
2295 scsi_init_command(dev
, scmd
);
2297 scmd
->cmnd
= scsi_req(rq
)->cmd
;
2299 scmd
->scsi_done
= scsi_reset_provider_done_command
;
2300 memset(&scmd
->sdb
, 0, sizeof(scmd
->sdb
));
2304 scmd
->sc_data_direction
= DMA_BIDIRECTIONAL
;
2306 spin_lock_irqsave(shost
->host_lock
, flags
);
2307 shost
->tmf_in_progress
= 1;
2308 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2310 switch (val
& ~SG_SCSI_RESET_NO_ESCALATE
) {
2311 case SG_SCSI_RESET_NOTHING
:
2314 case SG_SCSI_RESET_DEVICE
:
2315 rtn
= scsi_try_bus_device_reset(scmd
);
2316 if (rtn
== SUCCESS
|| (val
& SG_SCSI_RESET_NO_ESCALATE
))
2319 case SG_SCSI_RESET_TARGET
:
2320 rtn
= scsi_try_target_reset(scmd
);
2321 if (rtn
== SUCCESS
|| (val
& SG_SCSI_RESET_NO_ESCALATE
))
2324 case SG_SCSI_RESET_BUS
:
2325 rtn
= scsi_try_bus_reset(scmd
);
2326 if (rtn
== SUCCESS
|| (val
& SG_SCSI_RESET_NO_ESCALATE
))
2329 case SG_SCSI_RESET_HOST
:
2330 rtn
= scsi_try_host_reset(scmd
);
2339 error
= (rtn
== SUCCESS
) ? 0 : -EIO
;
2341 spin_lock_irqsave(shost
->host_lock
, flags
);
2342 shost
->tmf_in_progress
= 0;
2343 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2346 * be sure to wake up anyone who was sleeping or had their queue
2347 * suspended while we performed the TMF.
2349 SCSI_LOG_ERROR_RECOVERY(3,
2350 shost_printk(KERN_INFO
, shost
,
2351 "waking up host to restart after TMF\n"));
2353 wake_up(&shost
->host_wait
);
2354 scsi_run_host_queues(shost
);
2356 scsi_put_command(scmd
);
2359 out_put_autopm_host
:
2360 scsi_autopm_put_host(shost
);
2363 EXPORT_SYMBOL(scsi_ioctl_reset
);
2365 bool scsi_command_normalize_sense(const struct scsi_cmnd
*cmd
,
2366 struct scsi_sense_hdr
*sshdr
)
2368 return scsi_normalize_sense(cmd
->sense_buffer
,
2369 SCSI_SENSE_BUFFERSIZE
, sshdr
);
2371 EXPORT_SYMBOL(scsi_command_normalize_sense
);
2374 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
2375 * @sense_buffer: byte array of sense data
2376 * @sb_len: number of valid bytes in sense_buffer
2377 * @info_out: pointer to 64 integer where 8 or 4 byte information
2378 * field will be placed if found.
2381 * true if information field found, false if not found.
2383 bool scsi_get_sense_info_fld(const u8
*sense_buffer
, int sb_len
,
2390 switch (sense_buffer
[0] & 0x7f) {
2393 if (sense_buffer
[0] & 0x80) {
2394 *info_out
= get_unaligned_be32(&sense_buffer
[3]);
2400 ucp
= scsi_sense_desc_find(sense_buffer
, sb_len
,
2402 if (ucp
&& (0xa == ucp
[1])) {
2403 *info_out
= get_unaligned_be64(&ucp
[4]);
2411 EXPORT_SYMBOL(scsi_get_sense_info_fld
);