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_transport.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_ioctl.h>
40 #include "scsi_priv.h"
41 #include "scsi_logging.h"
42 #include "scsi_transport_api.h"
44 #include <trace/events/scsi.h>
46 static void scsi_eh_done(struct scsi_cmnd
*scmd
);
49 * These should *probably* be handled by the host itself.
50 * Since it is allowed to sleep, it probably should.
52 #define BUS_RESET_SETTLE_TIME (10)
53 #define HOST_RESET_SETTLE_TIME (10)
55 static int scsi_eh_try_stu(struct scsi_cmnd
*scmd
);
56 static int scsi_try_to_abort_cmd(struct scsi_host_template
*,
59 /* called with shost->host_lock held */
60 void scsi_eh_wakeup(struct Scsi_Host
*shost
)
62 if (shost
->host_busy
== shost
->host_failed
) {
63 trace_scsi_eh_wakeup(shost
);
64 wake_up_process(shost
->ehandler
);
65 SCSI_LOG_ERROR_RECOVERY(5,
66 printk("Waking error handler thread\n"));
71 * scsi_schedule_eh - schedule EH for SCSI host
72 * @shost: SCSI host to invoke error handling on.
74 * Schedule SCSI EH without scmd.
76 void scsi_schedule_eh(struct Scsi_Host
*shost
)
80 spin_lock_irqsave(shost
->host_lock
, flags
);
82 if (scsi_host_set_state(shost
, SHOST_RECOVERY
) == 0 ||
83 scsi_host_set_state(shost
, SHOST_CANCEL_RECOVERY
) == 0) {
84 shost
->host_eh_scheduled
++;
85 scsi_eh_wakeup(shost
);
88 spin_unlock_irqrestore(shost
->host_lock
, flags
);
90 EXPORT_SYMBOL_GPL(scsi_schedule_eh
);
92 static int scsi_host_eh_past_deadline(struct Scsi_Host
*shost
)
94 if (!shost
->last_reset
|| !shost
->eh_deadline
)
97 if (time_before(jiffies
,
98 shost
->last_reset
+ shost
->eh_deadline
))
105 * scmd_eh_abort_handler - Handle command aborts
106 * @work: command to be aborted.
109 scmd_eh_abort_handler(struct work_struct
*work
)
111 struct scsi_cmnd
*scmd
=
112 container_of(work
, struct scsi_cmnd
, abort_work
.work
);
113 struct scsi_device
*sdev
= scmd
->device
;
117 spin_lock_irqsave(sdev
->host
->host_lock
, flags
);
118 if (scsi_host_eh_past_deadline(sdev
->host
)) {
119 spin_unlock_irqrestore(sdev
->host
->host_lock
, flags
);
120 SCSI_LOG_ERROR_RECOVERY(3,
121 scmd_printk(KERN_INFO
, scmd
,
122 "scmd %p eh timeout, not aborting\n",
125 spin_unlock_irqrestore(sdev
->host
->host_lock
, flags
);
126 SCSI_LOG_ERROR_RECOVERY(3,
127 scmd_printk(KERN_INFO
, scmd
,
128 "aborting command %p\n", scmd
));
129 rtn
= scsi_try_to_abort_cmd(sdev
->host
->hostt
, scmd
);
130 if (rtn
== SUCCESS
) {
131 scmd
->result
|= DID_TIME_OUT
<< 16;
132 if (!scsi_noretry_cmd(scmd
) &&
133 (++scmd
->retries
<= scmd
->allowed
)) {
134 SCSI_LOG_ERROR_RECOVERY(3,
135 scmd_printk(KERN_WARNING
, scmd
,
137 "aborted command\n", scmd
));
138 scsi_queue_insert(scmd
, SCSI_MLQUEUE_EH_RETRY
);
140 SCSI_LOG_ERROR_RECOVERY(3,
141 scmd_printk(KERN_WARNING
, scmd
,
143 "aborted command\n", scmd
));
144 scsi_finish_command(scmd
);
148 SCSI_LOG_ERROR_RECOVERY(3,
149 scmd_printk(KERN_INFO
, scmd
,
150 "scmd %p abort failed, rtn %d\n",
154 if (!scsi_eh_scmd_add(scmd
, 0)) {
155 SCSI_LOG_ERROR_RECOVERY(3,
156 scmd_printk(KERN_WARNING
, scmd
,
158 "aborted command\n", scmd
));
159 scmd
->result
|= DID_TIME_OUT
<< 16;
160 scsi_finish_command(scmd
);
165 * scsi_abort_command - schedule a command abort
166 * @scmd: scmd to abort.
168 * We only need to abort commands after a command timeout
171 scsi_abort_command(struct scsi_cmnd
*scmd
)
173 struct scsi_device
*sdev
= scmd
->device
;
174 struct Scsi_Host
*shost
= sdev
->host
;
177 if (scmd
->eh_eflags
& SCSI_EH_ABORT_SCHEDULED
) {
179 * Retry after abort failed, escalate to next level.
181 SCSI_LOG_ERROR_RECOVERY(3,
182 scmd_printk(KERN_INFO
, scmd
,
183 "scmd %p previous abort failed\n", scmd
));
184 cancel_delayed_work(&scmd
->abort_work
);
189 * Do not try a command abort if
190 * SCSI EH has already started.
192 spin_lock_irqsave(shost
->host_lock
, flags
);
193 if (scsi_host_in_recovery(shost
)) {
194 spin_unlock_irqrestore(shost
->host_lock
, flags
);
195 SCSI_LOG_ERROR_RECOVERY(3,
196 scmd_printk(KERN_INFO
, scmd
,
197 "scmd %p not aborting, host in recovery\n",
202 if (shost
->eh_deadline
&& !shost
->last_reset
)
203 shost
->last_reset
= jiffies
;
204 spin_unlock_irqrestore(shost
->host_lock
, flags
);
206 scmd
->eh_eflags
|= SCSI_EH_ABORT_SCHEDULED
;
207 SCSI_LOG_ERROR_RECOVERY(3,
208 scmd_printk(KERN_INFO
, scmd
,
209 "scmd %p abort scheduled\n", scmd
));
210 queue_delayed_work(shost
->tmf_work_q
, &scmd
->abort_work
, HZ
/ 100);
215 * scsi_eh_scmd_add - add scsi cmd to error handling.
216 * @scmd: scmd to run eh on.
217 * @eh_flag: optional SCSI_EH flag.
222 int scsi_eh_scmd_add(struct scsi_cmnd
*scmd
, int eh_flag
)
224 struct Scsi_Host
*shost
= scmd
->device
->host
;
228 if (!shost
->ehandler
)
231 spin_lock_irqsave(shost
->host_lock
, flags
);
232 if (scsi_host_set_state(shost
, SHOST_RECOVERY
))
233 if (scsi_host_set_state(shost
, SHOST_CANCEL_RECOVERY
))
236 if (shost
->eh_deadline
&& !shost
->last_reset
)
237 shost
->last_reset
= jiffies
;
240 if (scmd
->eh_eflags
& SCSI_EH_ABORT_SCHEDULED
)
241 eh_flag
&= ~SCSI_EH_CANCEL_CMD
;
242 scmd
->eh_eflags
|= eh_flag
;
243 list_add_tail(&scmd
->eh_entry
, &shost
->eh_cmd_q
);
244 shost
->host_failed
++;
245 scsi_eh_wakeup(shost
);
247 spin_unlock_irqrestore(shost
->host_lock
, flags
);
252 * scsi_times_out - Timeout function for normal scsi commands.
253 * @req: request that is timing out.
256 * We do not need to lock this. There is the potential for a race
257 * only in that the normal completion handling might run, but if the
258 * normal completion function determines that the timer has already
259 * fired, then it mustn't do anything.
261 enum blk_eh_timer_return
scsi_times_out(struct request
*req
)
263 struct scsi_cmnd
*scmd
= req
->special
;
264 enum blk_eh_timer_return rtn
= BLK_EH_NOT_HANDLED
;
265 struct Scsi_Host
*host
= scmd
->device
->host
;
267 trace_scsi_dispatch_cmd_timeout(scmd
);
268 scsi_log_completion(scmd
, TIMEOUT_ERROR
);
270 if (host
->eh_deadline
&& !host
->last_reset
)
271 host
->last_reset
= jiffies
;
273 if (host
->transportt
->eh_timed_out
)
274 rtn
= host
->transportt
->eh_timed_out(scmd
);
275 else if (host
->hostt
->eh_timed_out
)
276 rtn
= host
->hostt
->eh_timed_out(scmd
);
278 if (rtn
== BLK_EH_NOT_HANDLED
&& !host
->hostt
->no_async_abort
)
279 if (scsi_abort_command(scmd
) == SUCCESS
)
280 return BLK_EH_NOT_HANDLED
;
282 scmd
->result
|= DID_TIME_OUT
<< 16;
284 if (unlikely(rtn
== BLK_EH_NOT_HANDLED
&&
285 !scsi_eh_scmd_add(scmd
, SCSI_EH_CANCEL_CMD
)))
286 rtn
= BLK_EH_HANDLED
;
292 * scsi_block_when_processing_errors - Prevent cmds from being queued.
293 * @sdev: Device on which we are performing recovery.
296 * We block until the host is out of error recovery, and then check to
297 * see whether the host or the device is offline.
300 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
302 int scsi_block_when_processing_errors(struct scsi_device
*sdev
)
306 wait_event(sdev
->host
->host_wait
, !scsi_host_in_recovery(sdev
->host
));
308 online
= scsi_device_online(sdev
);
310 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: rtn: %d\n", __func__
,
315 EXPORT_SYMBOL(scsi_block_when_processing_errors
);
317 #ifdef CONFIG_SCSI_LOGGING
319 * scsi_eh_prt_fail_stats - Log info on failures.
320 * @shost: scsi host being recovered.
321 * @work_q: Queue of scsi cmds to process.
323 static inline void scsi_eh_prt_fail_stats(struct Scsi_Host
*shost
,
324 struct list_head
*work_q
)
326 struct scsi_cmnd
*scmd
;
327 struct scsi_device
*sdev
;
328 int total_failures
= 0;
331 int devices_failed
= 0;
333 shost_for_each_device(sdev
, shost
) {
334 list_for_each_entry(scmd
, work_q
, eh_entry
) {
335 if (scmd
->device
== sdev
) {
337 if (scmd
->eh_eflags
& SCSI_EH_CANCEL_CMD
)
344 if (cmd_cancel
|| cmd_failed
) {
345 SCSI_LOG_ERROR_RECOVERY(3,
346 sdev_printk(KERN_INFO
, sdev
,
347 "%s: cmds failed: %d, cancel: %d\n",
348 __func__
, cmd_failed
,
356 SCSI_LOG_ERROR_RECOVERY(2, printk("Total of %d commands on %d"
357 " devices require eh work\n",
358 total_failures
, devices_failed
));
363 * scsi_report_lun_change - Set flag on all *other* devices on the same target
364 * to indicate that a UNIT ATTENTION is expected.
365 * @sdev: Device reporting the UNIT ATTENTION
367 static void scsi_report_lun_change(struct scsi_device
*sdev
)
369 sdev
->sdev_target
->expecting_lun_change
= 1;
373 * scsi_report_sense - Examine scsi sense information and log messages for
374 * certain conditions, also issue uevents for some of them.
375 * @sdev: Device reporting the sense code
376 * @sshdr: sshdr to be examined
378 static void scsi_report_sense(struct scsi_device
*sdev
,
379 struct scsi_sense_hdr
*sshdr
)
381 enum scsi_device_event evt_type
= SDEV_EVT_MAXBITS
; /* i.e. none */
383 if (sshdr
->sense_key
== UNIT_ATTENTION
) {
384 if (sshdr
->asc
== 0x3f && sshdr
->ascq
== 0x03) {
385 evt_type
= SDEV_EVT_INQUIRY_CHANGE_REPORTED
;
386 sdev_printk(KERN_WARNING
, sdev
,
387 "Inquiry data has changed");
388 } else if (sshdr
->asc
== 0x3f && sshdr
->ascq
== 0x0e) {
389 evt_type
= SDEV_EVT_LUN_CHANGE_REPORTED
;
390 scsi_report_lun_change(sdev
);
391 sdev_printk(KERN_WARNING
, sdev
,
392 "Warning! Received an indication that the "
393 "LUN assignments on this target have "
394 "changed. The Linux SCSI layer does not "
395 "automatically remap LUN assignments.\n");
396 } else if (sshdr
->asc
== 0x3f)
397 sdev_printk(KERN_WARNING
, sdev
,
398 "Warning! Received an indication that the "
399 "operating parameters on this target have "
400 "changed. The Linux SCSI layer does not "
401 "automatically adjust these parameters.\n");
403 if (sshdr
->asc
== 0x38 && sshdr
->ascq
== 0x07) {
404 evt_type
= SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED
;
405 sdev_printk(KERN_WARNING
, sdev
,
406 "Warning! Received an indication that the "
407 "LUN reached a thin provisioning soft "
411 if (sshdr
->asc
== 0x2a && sshdr
->ascq
== 0x01) {
412 evt_type
= SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED
;
413 sdev_printk(KERN_WARNING
, sdev
,
414 "Mode parameters changed");
415 } else if (sshdr
->asc
== 0x2a && sshdr
->ascq
== 0x09) {
416 evt_type
= SDEV_EVT_CAPACITY_CHANGE_REPORTED
;
417 sdev_printk(KERN_WARNING
, sdev
,
418 "Capacity data has changed");
419 } else if (sshdr
->asc
== 0x2a)
420 sdev_printk(KERN_WARNING
, sdev
,
421 "Parameters changed");
424 if (evt_type
!= SDEV_EVT_MAXBITS
) {
425 set_bit(evt_type
, sdev
->pending_events
);
426 schedule_work(&sdev
->event_work
);
431 * scsi_check_sense - Examine scsi cmd sense
432 * @scmd: Cmd to have sense checked.
435 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
438 * When a deferred error is detected the current command has
439 * not been executed and needs retrying.
441 static int scsi_check_sense(struct scsi_cmnd
*scmd
)
443 struct scsi_device
*sdev
= scmd
->device
;
444 struct scsi_sense_hdr sshdr
;
446 if (! scsi_command_normalize_sense(scmd
, &sshdr
))
447 return FAILED
; /* no valid sense data */
449 if (scmd
->cmnd
[0] == TEST_UNIT_READY
&& scmd
->scsi_done
!= scsi_eh_done
)
451 * nasty: for mid-layer issued TURs, we need to return the
452 * actual sense data without any recovery attempt. For eh
453 * issued ones, we need to try to recover and interpret
457 scsi_report_sense(sdev
, &sshdr
);
459 if (scsi_sense_is_deferred(&sshdr
))
462 if (sdev
->scsi_dh_data
&& sdev
->scsi_dh_data
->scsi_dh
&&
463 sdev
->scsi_dh_data
->scsi_dh
->check_sense
) {
466 rc
= sdev
->scsi_dh_data
->scsi_dh
->check_sense(sdev
, &sshdr
);
467 if (rc
!= SCSI_RETURN_NOT_HANDLED
)
469 /* handler does not care. Drop down to default handling */
473 * Previous logic looked for FILEMARK, EOM or ILI which are
474 * mainly associated with tapes and returned SUCCESS.
476 if (sshdr
.response_code
== 0x70) {
478 if (scmd
->sense_buffer
[2] & 0xe0)
482 * descriptor format: look for "stream commands sense data
483 * descriptor" (see SSC-3). Assume single sense data
484 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
486 if ((sshdr
.additional_length
> 3) &&
487 (scmd
->sense_buffer
[8] == 0x4) &&
488 (scmd
->sense_buffer
[11] & 0xe0))
492 switch (sshdr
.sense_key
) {
495 case RECOVERED_ERROR
:
496 return /* soft_error */ SUCCESS
;
498 case ABORTED_COMMAND
:
499 if (sshdr
.asc
== 0x10) /* DIF */
506 * if we are expecting a cc/ua because of a bus reset that we
507 * performed, treat this just as a retry. otherwise this is
508 * information that we should pass up to the upper-level driver
509 * so that we can deal with it there.
511 if (scmd
->device
->expecting_cc_ua
) {
513 * Because some device does not queue unit
514 * attentions correctly, we carefully check
515 * additional sense code and qualifier so as
516 * not to squash media change unit attention.
518 if (sshdr
.asc
!= 0x28 || sshdr
.ascq
!= 0x00) {
519 scmd
->device
->expecting_cc_ua
= 0;
524 * we might also expect a cc/ua if another LUN on the target
525 * reported a UA with an ASC/ASCQ of 3F 0E -
526 * REPORTED LUNS DATA HAS CHANGED.
528 if (scmd
->device
->sdev_target
->expecting_lun_change
&&
529 sshdr
.asc
== 0x3f && sshdr
.ascq
== 0x0e)
532 * if the device is in the process of becoming ready, we
535 if ((sshdr
.asc
== 0x04) && (sshdr
.ascq
== 0x01))
538 * if the device is not started, we need to wake
539 * the error handler to start the motor
541 if (scmd
->device
->allow_restart
&&
542 (sshdr
.asc
== 0x04) && (sshdr
.ascq
== 0x02))
545 * Pass the UA upwards for a determination in the completion
550 /* these are not supported */
552 if (sshdr
.asc
== 0x27 && sshdr
.ascq
== 0x07) {
553 /* Thin provisioning hard threshold reached */
554 set_host_byte(scmd
, DID_ALLOC_FAILURE
);
558 case VOLUME_OVERFLOW
:
561 set_host_byte(scmd
, DID_TARGET_FAILURE
);
565 if (sshdr
.asc
== 0x11 || /* UNRECOVERED READ ERR */
566 sshdr
.asc
== 0x13 || /* AMNF DATA FIELD */
567 sshdr
.asc
== 0x14) { /* RECORD NOT FOUND */
568 set_host_byte(scmd
, DID_MEDIUM_ERROR
);
574 if (scmd
->device
->retry_hwerror
)
575 return ADD_TO_MLQUEUE
;
577 set_host_byte(scmd
, DID_TARGET_FAILURE
);
579 case ILLEGAL_REQUEST
:
580 if (sshdr
.asc
== 0x20 || /* Invalid command operation code */
581 sshdr
.asc
== 0x21 || /* Logical block address out of range */
582 sshdr
.asc
== 0x24 || /* Invalid field in cdb */
583 sshdr
.asc
== 0x26) { /* Parameter value invalid */
584 set_host_byte(scmd
, DID_TARGET_FAILURE
);
593 static void scsi_handle_queue_ramp_up(struct scsi_device
*sdev
)
595 struct scsi_host_template
*sht
= sdev
->host
->hostt
;
596 struct scsi_device
*tmp_sdev
;
598 if (!sht
->change_queue_depth
||
599 sdev
->queue_depth
>= sdev
->max_queue_depth
)
602 if (time_before(jiffies
,
603 sdev
->last_queue_ramp_up
+ sdev
->queue_ramp_up_period
))
606 if (time_before(jiffies
,
607 sdev
->last_queue_full_time
+ sdev
->queue_ramp_up_period
))
611 * Walk all devices of a target and do
614 shost_for_each_device(tmp_sdev
, sdev
->host
) {
615 if (tmp_sdev
->channel
!= sdev
->channel
||
616 tmp_sdev
->id
!= sdev
->id
||
617 tmp_sdev
->queue_depth
== sdev
->max_queue_depth
)
620 * call back into LLD to increase queue_depth by one
621 * with ramp up reason code.
623 sht
->change_queue_depth(tmp_sdev
, tmp_sdev
->queue_depth
+ 1,
624 SCSI_QDEPTH_RAMP_UP
);
625 sdev
->last_queue_ramp_up
= jiffies
;
629 static void scsi_handle_queue_full(struct scsi_device
*sdev
)
631 struct scsi_host_template
*sht
= sdev
->host
->hostt
;
632 struct scsi_device
*tmp_sdev
;
634 if (!sht
->change_queue_depth
)
637 shost_for_each_device(tmp_sdev
, sdev
->host
) {
638 if (tmp_sdev
->channel
!= sdev
->channel
||
639 tmp_sdev
->id
!= sdev
->id
)
642 * We do not know the number of commands that were at
643 * the device when we got the queue full so we start
644 * from the highest possible value and work our way down.
646 sht
->change_queue_depth(tmp_sdev
, tmp_sdev
->queue_depth
- 1,
652 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
653 * @scmd: SCSI cmd to examine.
656 * This is *only* called when we are examining the status of commands
657 * queued during error recovery. the main difference here is that we
658 * don't allow for the possibility of retries here, and we are a lot
659 * more restrictive about what we consider acceptable.
661 static int scsi_eh_completed_normally(struct scsi_cmnd
*scmd
)
664 * first check the host byte, to see if there is anything in there
665 * that would indicate what we need to do.
667 if (host_byte(scmd
->result
) == DID_RESET
) {
669 * rats. we are already in the error handler, so we now
670 * get to try and figure out what to do next. if the sense
671 * is valid, we have a pretty good idea of what to do.
672 * if not, we mark it as FAILED.
674 return scsi_check_sense(scmd
);
676 if (host_byte(scmd
->result
) != DID_OK
)
680 * next, check the message byte.
682 if (msg_byte(scmd
->result
) != COMMAND_COMPLETE
)
686 * now, check the status byte to see if this indicates
689 switch (status_byte(scmd
->result
)) {
691 scsi_handle_queue_ramp_up(scmd
->device
);
692 case COMMAND_TERMINATED
:
694 case CHECK_CONDITION
:
695 return scsi_check_sense(scmd
);
697 case INTERMEDIATE_GOOD
:
698 case INTERMEDIATE_C_GOOD
:
700 * who knows? FIXME(eric)
703 case RESERVATION_CONFLICT
:
704 if (scmd
->cmnd
[0] == TEST_UNIT_READY
)
705 /* it is a success, we probed the device and
708 /* otherwise, we failed to send the command */
711 scsi_handle_queue_full(scmd
->device
);
722 * scsi_eh_done - Completion function for error handling.
723 * @scmd: Cmd that is done.
725 static void scsi_eh_done(struct scsi_cmnd
*scmd
)
727 struct completion
*eh_action
;
729 SCSI_LOG_ERROR_RECOVERY(3,
730 printk("%s scmd: %p result: %x\n",
731 __func__
, scmd
, scmd
->result
));
733 eh_action
= scmd
->device
->host
->eh_action
;
739 * scsi_try_host_reset - ask host adapter to reset itself
740 * @scmd: SCSI cmd to send host reset.
742 static int scsi_try_host_reset(struct scsi_cmnd
*scmd
)
746 struct Scsi_Host
*host
= scmd
->device
->host
;
747 struct scsi_host_template
*hostt
= host
->hostt
;
749 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Host RST\n",
752 if (!hostt
->eh_host_reset_handler
)
755 rtn
= hostt
->eh_host_reset_handler(scmd
);
757 if (rtn
== SUCCESS
) {
758 if (!hostt
->skip_settle_delay
)
759 ssleep(HOST_RESET_SETTLE_TIME
);
760 spin_lock_irqsave(host
->host_lock
, flags
);
761 scsi_report_bus_reset(host
, scmd_channel(scmd
));
762 spin_unlock_irqrestore(host
->host_lock
, flags
);
769 * scsi_try_bus_reset - ask host to perform a bus reset
770 * @scmd: SCSI cmd to send bus reset.
772 static int scsi_try_bus_reset(struct scsi_cmnd
*scmd
)
776 struct Scsi_Host
*host
= scmd
->device
->host
;
777 struct scsi_host_template
*hostt
= host
->hostt
;
779 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Bus RST\n",
782 if (!hostt
->eh_bus_reset_handler
)
785 rtn
= hostt
->eh_bus_reset_handler(scmd
);
787 if (rtn
== SUCCESS
) {
788 if (!hostt
->skip_settle_delay
)
789 ssleep(BUS_RESET_SETTLE_TIME
);
790 spin_lock_irqsave(host
->host_lock
, flags
);
791 scsi_report_bus_reset(host
, scmd_channel(scmd
));
792 spin_unlock_irqrestore(host
->host_lock
, flags
);
798 static void __scsi_report_device_reset(struct scsi_device
*sdev
, void *data
)
801 sdev
->expecting_cc_ua
= 1;
805 * scsi_try_target_reset - Ask host to perform a target reset
806 * @scmd: SCSI cmd used to send a target reset
809 * There is no timeout for this operation. if this operation is
810 * unreliable for a given host, then the host itself needs to put a
811 * timer on it, and set the host back to a consistent state prior to
814 static int scsi_try_target_reset(struct scsi_cmnd
*scmd
)
818 struct Scsi_Host
*host
= scmd
->device
->host
;
819 struct scsi_host_template
*hostt
= host
->hostt
;
821 if (!hostt
->eh_target_reset_handler
)
824 rtn
= hostt
->eh_target_reset_handler(scmd
);
825 if (rtn
== SUCCESS
) {
826 spin_lock_irqsave(host
->host_lock
, flags
);
827 __starget_for_each_device(scsi_target(scmd
->device
), NULL
,
828 __scsi_report_device_reset
);
829 spin_unlock_irqrestore(host
->host_lock
, flags
);
836 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
837 * @scmd: SCSI cmd used to send BDR
840 * There is no timeout for this operation. if this operation is
841 * unreliable for a given host, then the host itself needs to put a
842 * timer on it, and set the host back to a consistent state prior to
845 static int scsi_try_bus_device_reset(struct scsi_cmnd
*scmd
)
848 struct scsi_host_template
*hostt
= scmd
->device
->host
->hostt
;
850 if (!hostt
->eh_device_reset_handler
)
853 rtn
= hostt
->eh_device_reset_handler(scmd
);
855 __scsi_report_device_reset(scmd
->device
, NULL
);
859 static int scsi_try_to_abort_cmd(struct scsi_host_template
*hostt
, struct scsi_cmnd
*scmd
)
861 if (!hostt
->eh_abort_handler
)
864 return hostt
->eh_abort_handler(scmd
);
867 static void scsi_abort_eh_cmnd(struct scsi_cmnd
*scmd
)
869 if (scsi_try_to_abort_cmd(scmd
->device
->host
->hostt
, scmd
) != SUCCESS
)
870 if (scsi_try_bus_device_reset(scmd
) != SUCCESS
)
871 if (scsi_try_target_reset(scmd
) != SUCCESS
)
872 if (scsi_try_bus_reset(scmd
) != SUCCESS
)
873 scsi_try_host_reset(scmd
);
877 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
878 * @scmd: SCSI command structure to hijack
879 * @ses: structure to save restore information
880 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
881 * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
882 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
884 * This function is used to save a scsi command information before re-execution
885 * as part of the error recovery process. If @sense_bytes is 0 the command
886 * sent must be one that does not transfer any data. If @sense_bytes != 0
887 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
888 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
890 void scsi_eh_prep_cmnd(struct scsi_cmnd
*scmd
, struct scsi_eh_save
*ses
,
891 unsigned char *cmnd
, int cmnd_size
, unsigned sense_bytes
)
893 struct scsi_device
*sdev
= scmd
->device
;
896 * We need saved copies of a number of fields - this is because
897 * error handling may need to overwrite these with different values
898 * to run different commands, and once error handling is complete,
899 * we will need to restore these values prior to running the actual
902 ses
->cmd_len
= scmd
->cmd_len
;
903 ses
->cmnd
= scmd
->cmnd
;
904 ses
->data_direction
= scmd
->sc_data_direction
;
905 ses
->sdb
= scmd
->sdb
;
906 ses
->next_rq
= scmd
->request
->next_rq
;
907 ses
->result
= scmd
->result
;
908 ses
->underflow
= scmd
->underflow
;
909 ses
->prot_op
= scmd
->prot_op
;
911 scmd
->prot_op
= SCSI_PROT_NORMAL
;
912 scmd
->cmnd
= ses
->eh_cmnd
;
913 memset(scmd
->cmnd
, 0, BLK_MAX_CDB
);
914 memset(&scmd
->sdb
, 0, sizeof(scmd
->sdb
));
915 scmd
->request
->next_rq
= NULL
;
918 scmd
->sdb
.length
= min_t(unsigned, SCSI_SENSE_BUFFERSIZE
,
920 sg_init_one(&ses
->sense_sgl
, scmd
->sense_buffer
,
922 scmd
->sdb
.table
.sgl
= &ses
->sense_sgl
;
923 scmd
->sc_data_direction
= DMA_FROM_DEVICE
;
924 scmd
->sdb
.table
.nents
= 1;
925 scmd
->cmnd
[0] = REQUEST_SENSE
;
926 scmd
->cmnd
[4] = scmd
->sdb
.length
;
927 scmd
->cmd_len
= COMMAND_SIZE(scmd
->cmnd
[0]);
929 scmd
->sc_data_direction
= DMA_NONE
;
931 BUG_ON(cmnd_size
> BLK_MAX_CDB
);
932 memcpy(scmd
->cmnd
, cmnd
, cmnd_size
);
933 scmd
->cmd_len
= COMMAND_SIZE(scmd
->cmnd
[0]);
939 if (sdev
->scsi_level
<= SCSI_2
&& sdev
->scsi_level
!= SCSI_UNKNOWN
)
940 scmd
->cmnd
[1] = (scmd
->cmnd
[1] & 0x1f) |
941 (sdev
->lun
<< 5 & 0xe0);
944 * Zero the sense buffer. The scsi spec mandates that any
945 * untransferred sense data should be interpreted as being zero.
947 memset(scmd
->sense_buffer
, 0, SCSI_SENSE_BUFFERSIZE
);
949 EXPORT_SYMBOL(scsi_eh_prep_cmnd
);
952 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
953 * @scmd: SCSI command structure to restore
954 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
956 * Undo any damage done by above scsi_eh_prep_cmnd().
958 void scsi_eh_restore_cmnd(struct scsi_cmnd
* scmd
, struct scsi_eh_save
*ses
)
961 * Restore original data
963 scmd
->cmd_len
= ses
->cmd_len
;
964 scmd
->cmnd
= ses
->cmnd
;
965 scmd
->sc_data_direction
= ses
->data_direction
;
966 scmd
->sdb
= ses
->sdb
;
967 scmd
->request
->next_rq
= ses
->next_rq
;
968 scmd
->result
= ses
->result
;
969 scmd
->underflow
= ses
->underflow
;
970 scmd
->prot_op
= ses
->prot_op
;
972 EXPORT_SYMBOL(scsi_eh_restore_cmnd
);
975 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
976 * @scmd: SCSI command structure to hijack
978 * @cmnd_size: size in bytes of @cmnd
979 * @timeout: timeout for this request
980 * @sense_bytes: size of sense data to copy or 0
982 * This function is used to send a scsi command down to a target device
983 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
986 * SUCCESS or FAILED or NEEDS_RETRY
988 static int scsi_send_eh_cmnd(struct scsi_cmnd
*scmd
, unsigned char *cmnd
,
989 int cmnd_size
, int timeout
, unsigned sense_bytes
)
991 struct scsi_device
*sdev
= scmd
->device
;
992 struct Scsi_Host
*shost
= sdev
->host
;
993 DECLARE_COMPLETION_ONSTACK(done
);
994 unsigned long timeleft
= timeout
;
995 struct scsi_eh_save ses
;
996 const unsigned long stall_for
= msecs_to_jiffies(100);
1000 scsi_eh_prep_cmnd(scmd
, &ses
, cmnd
, cmnd_size
, sense_bytes
);
1001 shost
->eh_action
= &done
;
1003 scsi_log_send(scmd
);
1004 scmd
->scsi_done
= scsi_eh_done
;
1005 rtn
= shost
->hostt
->queuecommand(shost
, scmd
);
1007 if (timeleft
> stall_for
) {
1008 scsi_eh_restore_cmnd(scmd
, &ses
);
1009 timeleft
-= stall_for
;
1010 msleep(jiffies_to_msecs(stall_for
));
1013 /* signal not to enter either branch of the if () below */
1017 timeleft
= wait_for_completion_timeout(&done
, timeout
);
1020 shost
->eh_action
= NULL
;
1022 scsi_log_completion(scmd
, rtn
);
1024 SCSI_LOG_ERROR_RECOVERY(3,
1025 printk("%s: scmd: %p, timeleft: %ld\n",
1026 __func__
, scmd
, timeleft
));
1029 * If there is time left scsi_eh_done got called, and we will examine
1030 * the actual status codes to see whether the command actually did
1031 * complete normally, else if we have a zero return and no time left,
1032 * the command must still be pending, so abort it and return FAILED.
1033 * If we never actually managed to issue the command, because
1034 * ->queuecommand() kept returning non zero, use the rtn = FAILED
1035 * value above (so don't execute either branch of the if)
1038 rtn
= scsi_eh_completed_normally(scmd
);
1039 SCSI_LOG_ERROR_RECOVERY(3,
1040 printk("%s: scsi_eh_completed_normally %x\n",
1048 case ADD_TO_MLQUEUE
:
1056 scsi_abort_eh_cmnd(scmd
);
1060 scsi_eh_restore_cmnd(scmd
, &ses
);
1066 * scsi_request_sense - Request sense data from a particular target.
1067 * @scmd: SCSI cmd for request sense.
1070 * Some hosts automatically obtain this information, others require
1071 * that we obtain it on our own. This function will *not* return until
1072 * the command either times out, or it completes.
1074 static int scsi_request_sense(struct scsi_cmnd
*scmd
)
1076 return scsi_send_eh_cmnd(scmd
, NULL
, 0, scmd
->device
->eh_timeout
, ~0);
1079 static int scsi_eh_action(struct scsi_cmnd
*scmd
, int rtn
)
1081 if (scmd
->request
->cmd_type
!= REQ_TYPE_BLOCK_PC
) {
1082 struct scsi_driver
*sdrv
= scsi_cmd_to_driver(scmd
);
1083 if (sdrv
->eh_action
)
1084 rtn
= sdrv
->eh_action(scmd
, rtn
);
1090 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
1091 * @scmd: Original SCSI cmd that eh has finished.
1092 * @done_q: Queue for processed commands.
1095 * We don't want to use the normal command completion while we are are
1096 * still handling errors - it may cause other commands to be queued,
1097 * and that would disturb what we are doing. Thus we really want to
1098 * keep a list of pending commands for final completion, and once we
1099 * are ready to leave error handling we handle completion for real.
1101 void scsi_eh_finish_cmd(struct scsi_cmnd
*scmd
, struct list_head
*done_q
)
1103 scmd
->device
->host
->host_failed
--;
1104 scmd
->eh_eflags
= 0;
1105 list_move_tail(&scmd
->eh_entry
, done_q
);
1107 EXPORT_SYMBOL(scsi_eh_finish_cmd
);
1110 * scsi_eh_get_sense - Get device sense data.
1111 * @work_q: Queue of commands to process.
1112 * @done_q: Queue of processed commands.
1115 * See if we need to request sense information. if so, then get it
1116 * now, so we have a better idea of what to do.
1119 * This has the unfortunate side effect that if a shost adapter does
1120 * not automatically request sense information, we end up shutting
1121 * it down before we request it.
1123 * All drivers should request sense information internally these days,
1124 * so for now all I have to say is tough noogies if you end up in here.
1126 * XXX: Long term this code should go away, but that needs an audit of
1129 int scsi_eh_get_sense(struct list_head
*work_q
,
1130 struct list_head
*done_q
)
1132 struct scsi_cmnd
*scmd
, *next
;
1133 struct Scsi_Host
*shost
;
1135 unsigned long flags
;
1137 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1138 if ((scmd
->eh_eflags
& SCSI_EH_CANCEL_CMD
) ||
1139 SCSI_SENSE_VALID(scmd
))
1142 shost
= scmd
->device
->host
;
1143 spin_lock_irqsave(shost
->host_lock
, flags
);
1144 if (scsi_host_eh_past_deadline(shost
)) {
1145 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1146 SCSI_LOG_ERROR_RECOVERY(3,
1147 shost_printk(KERN_INFO
, shost
,
1148 "skip %s, past eh deadline\n",
1152 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1153 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO
, scmd
,
1154 "%s: requesting sense\n",
1156 rtn
= scsi_request_sense(scmd
);
1160 SCSI_LOG_ERROR_RECOVERY(3, printk("sense requested for %p"
1161 " result %x\n", scmd
,
1163 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense("bh", scmd
));
1165 rtn
= scsi_decide_disposition(scmd
);
1168 * if the result was normal, then just pass it along to the
1172 /* we don't want this command reissued, just
1173 * finished with the sense data, so set
1174 * retries to the max allowed to ensure it
1175 * won't get reissued */
1176 scmd
->retries
= scmd
->allowed
;
1177 else if (rtn
!= NEEDS_RETRY
)
1180 scsi_eh_finish_cmd(scmd
, done_q
);
1183 return list_empty(work_q
);
1185 EXPORT_SYMBOL_GPL(scsi_eh_get_sense
);
1188 * scsi_eh_tur - Send TUR to device.
1189 * @scmd: &scsi_cmnd to send TUR
1192 * 0 - Device is ready. 1 - Device NOT ready.
1194 static int scsi_eh_tur(struct scsi_cmnd
*scmd
)
1196 static unsigned char tur_command
[6] = {TEST_UNIT_READY
, 0, 0, 0, 0, 0};
1197 int retry_cnt
= 1, rtn
;
1200 rtn
= scsi_send_eh_cmnd(scmd
, tur_command
, 6,
1201 scmd
->device
->eh_timeout
, 0);
1203 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd %p rtn %x\n",
1204 __func__
, scmd
, rtn
));
1219 * scsi_eh_test_devices - check if devices are responding from error recovery.
1220 * @cmd_list: scsi commands in error recovery.
1221 * @work_q: queue for commands which still need more error recovery
1222 * @done_q: queue for commands which are finished
1223 * @try_stu: boolean on if a STU command should be tried in addition to TUR.
1226 * Tests if devices are in a working state. Commands to devices now in
1227 * a working state are sent to the done_q while commands to devices which
1228 * are still failing to respond are returned to the work_q for more
1231 static int scsi_eh_test_devices(struct list_head
*cmd_list
,
1232 struct list_head
*work_q
,
1233 struct list_head
*done_q
, int try_stu
)
1235 struct scsi_cmnd
*scmd
, *next
;
1236 struct scsi_device
*sdev
;
1238 unsigned long flags
;
1240 while (!list_empty(cmd_list
)) {
1241 scmd
= list_entry(cmd_list
->next
, struct scsi_cmnd
, eh_entry
);
1242 sdev
= scmd
->device
;
1245 spin_lock_irqsave(sdev
->host
->host_lock
, flags
);
1246 if (scsi_host_eh_past_deadline(sdev
->host
)) {
1247 /* Push items back onto work_q */
1248 list_splice_init(cmd_list
, work_q
);
1249 spin_unlock_irqrestore(sdev
->host
->host_lock
,
1251 SCSI_LOG_ERROR_RECOVERY(3,
1252 shost_printk(KERN_INFO
, sdev
->host
,
1253 "skip %s, past eh deadline",
1257 spin_unlock_irqrestore(sdev
->host
->host_lock
, flags
);
1260 finish_cmds
= !scsi_device_online(scmd
->device
) ||
1261 (try_stu
&& !scsi_eh_try_stu(scmd
) &&
1262 !scsi_eh_tur(scmd
)) ||
1265 list_for_each_entry_safe(scmd
, next
, cmd_list
, eh_entry
)
1266 if (scmd
->device
== sdev
) {
1269 scsi_eh_action(scmd
, SUCCESS
) == SUCCESS
))
1270 scsi_eh_finish_cmd(scmd
, done_q
);
1272 list_move_tail(&scmd
->eh_entry
, work_q
);
1275 return list_empty(work_q
);
1280 * scsi_eh_abort_cmds - abort pending commands.
1281 * @work_q: &list_head for pending commands.
1282 * @done_q: &list_head for processed commands.
1285 * Try and see whether or not it makes sense to try and abort the
1286 * running command. This only works out to be the case if we have one
1287 * command that has timed out. If the command simply failed, it makes
1288 * no sense to try and abort the command, since as far as the shost
1289 * adapter is concerned, it isn't running.
1291 static int scsi_eh_abort_cmds(struct list_head
*work_q
,
1292 struct list_head
*done_q
)
1294 struct scsi_cmnd
*scmd
, *next
;
1295 LIST_HEAD(check_list
);
1297 struct Scsi_Host
*shost
;
1298 unsigned long flags
;
1300 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1301 if (!(scmd
->eh_eflags
& SCSI_EH_CANCEL_CMD
))
1303 shost
= scmd
->device
->host
;
1304 spin_lock_irqsave(shost
->host_lock
, flags
);
1305 if (scsi_host_eh_past_deadline(shost
)) {
1306 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1307 list_splice_init(&check_list
, work_q
);
1308 SCSI_LOG_ERROR_RECOVERY(3,
1309 shost_printk(KERN_INFO
, shost
,
1310 "skip %s, past eh deadline\n",
1312 return list_empty(work_q
);
1314 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1315 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting cmd:"
1316 "0x%p\n", current
->comm
,
1318 rtn
= scsi_try_to_abort_cmd(shost
->hostt
, scmd
);
1319 if (rtn
== FAILED
) {
1320 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting"
1325 list_splice_init(&check_list
, work_q
);
1326 return list_empty(work_q
);
1328 scmd
->eh_eflags
&= ~SCSI_EH_CANCEL_CMD
;
1329 if (rtn
== FAST_IO_FAIL
)
1330 scsi_eh_finish_cmd(scmd
, done_q
);
1332 list_move_tail(&scmd
->eh_entry
, &check_list
);
1335 return scsi_eh_test_devices(&check_list
, work_q
, done_q
, 0);
1339 * scsi_eh_try_stu - Send START_UNIT to device.
1340 * @scmd: &scsi_cmnd to send START_UNIT
1343 * 0 - Device is ready. 1 - Device NOT ready.
1345 static int scsi_eh_try_stu(struct scsi_cmnd
*scmd
)
1347 static unsigned char stu_command
[6] = {START_STOP
, 0, 0, 0, 1, 0};
1349 if (scmd
->device
->allow_restart
) {
1350 int i
, rtn
= NEEDS_RETRY
;
1352 for (i
= 0; rtn
== NEEDS_RETRY
&& i
< 2; i
++)
1353 rtn
= scsi_send_eh_cmnd(scmd
, stu_command
, 6, scmd
->device
->request_queue
->rq_timeout
, 0);
1363 * scsi_eh_stu - send START_UNIT if needed
1364 * @shost: &scsi host being recovered.
1365 * @work_q: &list_head for pending commands.
1366 * @done_q: &list_head for processed commands.
1369 * If commands are failing due to not ready, initializing command required,
1370 * try revalidating the device, which will end up sending a start unit.
1372 static int scsi_eh_stu(struct Scsi_Host
*shost
,
1373 struct list_head
*work_q
,
1374 struct list_head
*done_q
)
1376 struct scsi_cmnd
*scmd
, *stu_scmd
, *next
;
1377 struct scsi_device
*sdev
;
1378 unsigned long flags
;
1380 shost_for_each_device(sdev
, shost
) {
1381 spin_lock_irqsave(shost
->host_lock
, flags
);
1382 if (scsi_host_eh_past_deadline(shost
)) {
1383 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1384 SCSI_LOG_ERROR_RECOVERY(3,
1385 shost_printk(KERN_INFO
, shost
,
1386 "skip %s, past eh deadline\n",
1390 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1392 list_for_each_entry(scmd
, work_q
, eh_entry
)
1393 if (scmd
->device
== sdev
&& SCSI_SENSE_VALID(scmd
) &&
1394 scsi_check_sense(scmd
) == FAILED
) {
1402 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending START_UNIT to sdev:"
1403 " 0x%p\n", current
->comm
, sdev
));
1405 if (!scsi_eh_try_stu(stu_scmd
)) {
1406 if (!scsi_device_online(sdev
) ||
1407 !scsi_eh_tur(stu_scmd
)) {
1408 list_for_each_entry_safe(scmd
, next
,
1410 if (scmd
->device
== sdev
&&
1411 scsi_eh_action(scmd
, SUCCESS
) == SUCCESS
)
1412 scsi_eh_finish_cmd(scmd
, done_q
);
1416 SCSI_LOG_ERROR_RECOVERY(3,
1417 printk("%s: START_UNIT failed to sdev:"
1418 " 0x%p\n", current
->comm
, sdev
));
1422 return list_empty(work_q
);
1427 * scsi_eh_bus_device_reset - send bdr if needed
1428 * @shost: scsi host being recovered.
1429 * @work_q: &list_head for pending commands.
1430 * @done_q: &list_head for processed commands.
1433 * Try a bus device reset. Still, look to see whether we have multiple
1434 * devices that are jammed or not - if we have multiple devices, it
1435 * makes no sense to try bus_device_reset - we really would need to try
1436 * a bus_reset instead.
1438 static int scsi_eh_bus_device_reset(struct Scsi_Host
*shost
,
1439 struct list_head
*work_q
,
1440 struct list_head
*done_q
)
1442 struct scsi_cmnd
*scmd
, *bdr_scmd
, *next
;
1443 struct scsi_device
*sdev
;
1444 unsigned long flags
;
1447 shost_for_each_device(sdev
, shost
) {
1448 spin_lock_irqsave(shost
->host_lock
, flags
);
1449 if (scsi_host_eh_past_deadline(shost
)) {
1450 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1451 SCSI_LOG_ERROR_RECOVERY(3,
1452 shost_printk(KERN_INFO
, shost
,
1453 "skip %s, past eh deadline\n",
1457 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1459 list_for_each_entry(scmd
, work_q
, eh_entry
)
1460 if (scmd
->device
== sdev
) {
1468 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BDR sdev:"
1469 " 0x%p\n", current
->comm
,
1471 rtn
= scsi_try_bus_device_reset(bdr_scmd
);
1472 if (rtn
== SUCCESS
|| rtn
== FAST_IO_FAIL
) {
1473 if (!scsi_device_online(sdev
) ||
1474 rtn
== FAST_IO_FAIL
||
1475 !scsi_eh_tur(bdr_scmd
)) {
1476 list_for_each_entry_safe(scmd
, next
,
1478 if (scmd
->device
== sdev
&&
1479 scsi_eh_action(scmd
, rtn
) != FAILED
)
1480 scsi_eh_finish_cmd(scmd
,
1485 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BDR"
1493 return list_empty(work_q
);
1497 * scsi_eh_target_reset - send target reset if needed
1498 * @shost: scsi host being recovered.
1499 * @work_q: &list_head for pending commands.
1500 * @done_q: &list_head for processed commands.
1503 * Try a target reset.
1505 static int scsi_eh_target_reset(struct Scsi_Host
*shost
,
1506 struct list_head
*work_q
,
1507 struct list_head
*done_q
)
1509 LIST_HEAD(tmp_list
);
1510 LIST_HEAD(check_list
);
1512 list_splice_init(work_q
, &tmp_list
);
1514 while (!list_empty(&tmp_list
)) {
1515 struct scsi_cmnd
*next
, *scmd
;
1518 unsigned long flags
;
1520 spin_lock_irqsave(shost
->host_lock
, flags
);
1521 if (scsi_host_eh_past_deadline(shost
)) {
1522 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1523 /* push back on work queue for further processing */
1524 list_splice_init(&check_list
, work_q
);
1525 list_splice_init(&tmp_list
, work_q
);
1526 SCSI_LOG_ERROR_RECOVERY(3,
1527 shost_printk(KERN_INFO
, shost
,
1528 "skip %s, past eh deadline\n",
1530 return list_empty(work_q
);
1532 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1534 scmd
= list_entry(tmp_list
.next
, struct scsi_cmnd
, eh_entry
);
1537 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending target reset "
1539 current
->comm
, id
));
1540 rtn
= scsi_try_target_reset(scmd
);
1541 if (rtn
!= SUCCESS
&& rtn
!= FAST_IO_FAIL
)
1542 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Target reset"
1545 current
->comm
, id
));
1546 list_for_each_entry_safe(scmd
, next
, &tmp_list
, eh_entry
) {
1547 if (scmd_id(scmd
) != id
)
1551 list_move_tail(&scmd
->eh_entry
, &check_list
);
1552 else if (rtn
== FAST_IO_FAIL
)
1553 scsi_eh_finish_cmd(scmd
, done_q
);
1555 /* push back on work queue for further processing */
1556 list_move(&scmd
->eh_entry
, work_q
);
1560 return scsi_eh_test_devices(&check_list
, work_q
, done_q
, 0);
1564 * scsi_eh_bus_reset - send a bus reset
1565 * @shost: &scsi host being recovered.
1566 * @work_q: &list_head for pending commands.
1567 * @done_q: &list_head for processed commands.
1569 static int scsi_eh_bus_reset(struct Scsi_Host
*shost
,
1570 struct list_head
*work_q
,
1571 struct list_head
*done_q
)
1573 struct scsi_cmnd
*scmd
, *chan_scmd
, *next
;
1574 LIST_HEAD(check_list
);
1575 unsigned int channel
;
1577 unsigned long flags
;
1580 * we really want to loop over the various channels, and do this on
1581 * a channel by channel basis. we should also check to see if any
1582 * of the failed commands are on soft_reset devices, and if so, skip
1586 for (channel
= 0; channel
<= shost
->max_channel
; channel
++) {
1587 spin_lock_irqsave(shost
->host_lock
, flags
);
1588 if (scsi_host_eh_past_deadline(shost
)) {
1589 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1590 list_splice_init(&check_list
, work_q
);
1591 SCSI_LOG_ERROR_RECOVERY(3,
1592 shost_printk(KERN_INFO
, shost
,
1593 "skip %s, past eh deadline\n",
1595 return list_empty(work_q
);
1597 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1600 list_for_each_entry(scmd
, work_q
, eh_entry
) {
1601 if (channel
== scmd_channel(scmd
)) {
1605 * FIXME add back in some support for
1606 * soft_reset devices.
1613 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BRST chan:"
1614 " %d\n", current
->comm
,
1616 rtn
= scsi_try_bus_reset(chan_scmd
);
1617 if (rtn
== SUCCESS
|| rtn
== FAST_IO_FAIL
) {
1618 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1619 if (channel
== scmd_channel(scmd
)) {
1620 if (rtn
== FAST_IO_FAIL
)
1621 scsi_eh_finish_cmd(scmd
,
1624 list_move_tail(&scmd
->eh_entry
,
1629 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BRST"
1630 " failed chan: %d\n",
1635 return scsi_eh_test_devices(&check_list
, work_q
, done_q
, 0);
1639 * scsi_eh_host_reset - send a host reset
1640 * @work_q: list_head for processed commands.
1641 * @done_q: list_head for processed commands.
1643 static int scsi_eh_host_reset(struct list_head
*work_q
,
1644 struct list_head
*done_q
)
1646 struct scsi_cmnd
*scmd
, *next
;
1647 LIST_HEAD(check_list
);
1650 if (!list_empty(work_q
)) {
1651 scmd
= list_entry(work_q
->next
,
1652 struct scsi_cmnd
, eh_entry
);
1654 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending HRST\n"
1657 rtn
= scsi_try_host_reset(scmd
);
1658 if (rtn
== SUCCESS
) {
1659 list_splice_init(work_q
, &check_list
);
1660 } else if (rtn
== FAST_IO_FAIL
) {
1661 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1662 scsi_eh_finish_cmd(scmd
, done_q
);
1665 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: HRST"
1670 return scsi_eh_test_devices(&check_list
, work_q
, done_q
, 1);
1674 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
1675 * @work_q: list_head for processed commands.
1676 * @done_q: list_head for processed commands.
1678 static void scsi_eh_offline_sdevs(struct list_head
*work_q
,
1679 struct list_head
*done_q
)
1681 struct scsi_cmnd
*scmd
, *next
;
1683 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1684 sdev_printk(KERN_INFO
, scmd
->device
, "Device offlined - "
1685 "not ready after error recovery\n");
1686 scsi_device_set_state(scmd
->device
, SDEV_OFFLINE
);
1687 if (scmd
->eh_eflags
& SCSI_EH_CANCEL_CMD
) {
1689 * FIXME: Handle lost cmds.
1692 scsi_eh_finish_cmd(scmd
, done_q
);
1698 * scsi_noretry_cmd - determine if command should be failed fast
1699 * @scmd: SCSI cmd to examine.
1701 int scsi_noretry_cmd(struct scsi_cmnd
*scmd
)
1703 switch (host_byte(scmd
->result
)) {
1709 return (scmd
->request
->cmd_flags
& REQ_FAILFAST_TRANSPORT
);
1711 return (scmd
->request
->cmd_flags
& REQ_FAILFAST_DEV
);
1713 if (msg_byte(scmd
->result
) == COMMAND_COMPLETE
&&
1714 status_byte(scmd
->result
) == RESERVATION_CONFLICT
)
1717 case DID_SOFT_ERROR
:
1718 return (scmd
->request
->cmd_flags
& REQ_FAILFAST_DRIVER
);
1721 if (status_byte(scmd
->result
) != CHECK_CONDITION
)
1726 * assume caller has checked sense and determined
1727 * the check condition was retryable.
1729 if (scmd
->request
->cmd_flags
& REQ_FAILFAST_DEV
||
1730 scmd
->request
->cmd_type
== REQ_TYPE_BLOCK_PC
)
1737 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1738 * @scmd: SCSI cmd to examine.
1741 * This is *only* called when we are examining the status after sending
1742 * out the actual data command. any commands that are queued for error
1743 * recovery (e.g. test_unit_ready) do *not* come through here.
1745 * When this routine returns failed, it means the error handler thread
1746 * is woken. In cases where the error code indicates an error that
1747 * doesn't require the error handler read (i.e. we don't need to
1748 * abort/reset), this function should return SUCCESS.
1750 int scsi_decide_disposition(struct scsi_cmnd
*scmd
)
1755 * if the device is offline, then we clearly just pass the result back
1756 * up to the top level.
1758 if (!scsi_device_online(scmd
->device
)) {
1759 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: device offline - report"
1766 * first check the host byte, to see if there is anything in there
1767 * that would indicate what we need to do.
1769 switch (host_byte(scmd
->result
)) {
1770 case DID_PASSTHROUGH
:
1772 * no matter what, pass this through to the upper layer.
1773 * nuke this special code so that it looks like we are saying
1776 scmd
->result
&= 0xff00ffff;
1780 * looks good. drop through, and check the next byte.
1784 if (scmd
->eh_eflags
& SCSI_EH_ABORT_SCHEDULED
) {
1785 scmd
->result
|= DID_TIME_OUT
<< 16;
1788 case DID_NO_CONNECT
:
1789 case DID_BAD_TARGET
:
1791 * note - this means that we just report the status back
1792 * to the top level driver, not that we actually think
1793 * that it indicates SUCCESS.
1797 * when the low level driver returns did_soft_error,
1798 * it is responsible for keeping an internal retry counter
1799 * in order to avoid endless loops (db)
1801 * actually this is a bug in this function here. we should
1802 * be mindful of the maximum number of retries specified
1803 * and not get stuck in a loop.
1805 case DID_SOFT_ERROR
:
1811 return ADD_TO_MLQUEUE
;
1812 case DID_TRANSPORT_DISRUPTED
:
1814 * LLD/transport was disrupted during processing of the IO.
1815 * The transport class is now blocked/blocking,
1816 * and the transport will decide what to do with the IO
1817 * based on its timers and recovery capablilities if
1818 * there are enough retries.
1821 case DID_TRANSPORT_FAILFAST
:
1823 * The transport decided to failfast the IO (most likely
1824 * the fast io fail tmo fired), so send IO directly upwards.
1828 if (msg_byte(scmd
->result
) == COMMAND_COMPLETE
&&
1829 status_byte(scmd
->result
) == RESERVATION_CONFLICT
)
1831 * execute reservation conflict processing code
1841 * when we scan the bus, we get timeout messages for
1842 * these commands if there is no device available.
1843 * other hosts report did_no_connect for the same thing.
1845 if ((scmd
->cmnd
[0] == TEST_UNIT_READY
||
1846 scmd
->cmnd
[0] == INQUIRY
)) {
1858 * next, check the message byte.
1860 if (msg_byte(scmd
->result
) != COMMAND_COMPLETE
)
1864 * check the status byte to see if this indicates anything special.
1866 switch (status_byte(scmd
->result
)) {
1868 scsi_handle_queue_full(scmd
->device
);
1870 * the case of trying to send too many commands to a
1871 * tagged queueing device.
1875 * device can't talk to us at the moment. Should only
1876 * occur (SAM-3) when the task queue is empty, so will cause
1877 * the empty queue handling to trigger a stall in the
1880 return ADD_TO_MLQUEUE
;
1882 if (scmd
->cmnd
[0] == REPORT_LUNS
)
1883 scmd
->device
->sdev_target
->expecting_lun_change
= 0;
1884 scsi_handle_queue_ramp_up(scmd
->device
);
1885 case COMMAND_TERMINATED
:
1889 case CHECK_CONDITION
:
1890 rtn
= scsi_check_sense(scmd
);
1891 if (rtn
== NEEDS_RETRY
)
1893 /* if rtn == FAILED, we have no sense information;
1894 * returning FAILED will wake the error handler thread
1895 * to collect the sense and redo the decide
1898 case CONDITION_GOOD
:
1899 case INTERMEDIATE_GOOD
:
1900 case INTERMEDIATE_C_GOOD
:
1903 * who knows? FIXME(eric)
1907 case RESERVATION_CONFLICT
:
1908 sdev_printk(KERN_INFO
, scmd
->device
,
1909 "reservation conflict\n");
1910 set_host_byte(scmd
, DID_NEXUS_FAILURE
);
1911 return SUCCESS
; /* causes immediate i/o error */
1919 /* we requeue for retry because the error was retryable, and
1920 * the request was not marked fast fail. Note that above,
1921 * even if the request is marked fast fail, we still requeue
1922 * for queue congestion conditions (QUEUE_FULL or BUSY) */
1923 if ((++scmd
->retries
) <= scmd
->allowed
1924 && !scsi_noretry_cmd(scmd
)) {
1928 * no more retries - report this one back to upper level.
1934 static void eh_lock_door_done(struct request
*req
, int uptodate
)
1936 __blk_put_request(req
->q
, req
);
1940 * scsi_eh_lock_door - Prevent medium removal for the specified device
1941 * @sdev: SCSI device to prevent medium removal
1944 * We must be called from process context.
1947 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1948 * head of the devices request queue, and continue.
1950 static void scsi_eh_lock_door(struct scsi_device
*sdev
)
1952 struct request
*req
;
1955 * blk_get_request with GFP_KERNEL (__GFP_WAIT) sleeps until a
1956 * request becomes available
1958 req
= blk_get_request(sdev
->request_queue
, READ
, GFP_KERNEL
);
1960 req
->cmd
[0] = ALLOW_MEDIUM_REMOVAL
;
1964 req
->cmd
[4] = SCSI_REMOVAL_PREVENT
;
1967 req
->cmd_len
= COMMAND_SIZE(req
->cmd
[0]);
1969 req
->cmd_type
= REQ_TYPE_BLOCK_PC
;
1970 req
->cmd_flags
|= REQ_QUIET
;
1971 req
->timeout
= 10 * HZ
;
1974 blk_execute_rq_nowait(req
->q
, NULL
, req
, 1, eh_lock_door_done
);
1978 * scsi_restart_operations - restart io operations to the specified host.
1979 * @shost: Host we are restarting.
1982 * When we entered the error handler, we blocked all further i/o to
1983 * this device. we need to 'reverse' this process.
1985 static void scsi_restart_operations(struct Scsi_Host
*shost
)
1987 struct scsi_device
*sdev
;
1988 unsigned long flags
;
1991 * If the door was locked, we need to insert a door lock request
1992 * onto the head of the SCSI request queue for the device. There
1993 * is no point trying to lock the door of an off-line device.
1995 shost_for_each_device(sdev
, shost
) {
1996 if (scsi_device_online(sdev
) && sdev
->locked
)
1997 scsi_eh_lock_door(sdev
);
2001 * next free up anything directly waiting upon the host. this
2002 * will be requests for character device operations, and also for
2003 * ioctls to queued block devices.
2005 SCSI_LOG_ERROR_RECOVERY(3,
2006 printk("scsi_eh_%d waking up host to restart\n",
2009 spin_lock_irqsave(shost
->host_lock
, flags
);
2010 if (scsi_host_set_state(shost
, SHOST_RUNNING
))
2011 if (scsi_host_set_state(shost
, SHOST_CANCEL
))
2012 BUG_ON(scsi_host_set_state(shost
, SHOST_DEL
));
2013 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2015 wake_up(&shost
->host_wait
);
2018 * finally we need to re-initiate requests that may be pending. we will
2019 * have had everything blocked while error handling is taking place, and
2020 * now that error recovery is done, we will need to ensure that these
2021 * requests are started.
2023 scsi_run_host_queues(shost
);
2026 * if eh is active and host_eh_scheduled is pending we need to re-run
2027 * recovery. we do this check after scsi_run_host_queues() to allow
2028 * everything pent up since the last eh run a chance to make forward
2029 * progress before we sync again. Either we'll immediately re-run
2030 * recovery or scsi_device_unbusy() will wake us again when these
2031 * pending commands complete.
2033 spin_lock_irqsave(shost
->host_lock
, flags
);
2034 if (shost
->host_eh_scheduled
)
2035 if (scsi_host_set_state(shost
, SHOST_RECOVERY
))
2036 WARN_ON(scsi_host_set_state(shost
, SHOST_CANCEL_RECOVERY
));
2037 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2041 * scsi_eh_ready_devs - check device ready state and recover if not.
2042 * @shost: host to be recovered.
2043 * @work_q: &list_head for pending commands.
2044 * @done_q: &list_head for processed commands.
2046 void scsi_eh_ready_devs(struct Scsi_Host
*shost
,
2047 struct list_head
*work_q
,
2048 struct list_head
*done_q
)
2050 if (!scsi_eh_stu(shost
, work_q
, done_q
))
2051 if (!scsi_eh_bus_device_reset(shost
, work_q
, done_q
))
2052 if (!scsi_eh_target_reset(shost
, work_q
, done_q
))
2053 if (!scsi_eh_bus_reset(shost
, work_q
, done_q
))
2054 if (!scsi_eh_host_reset(work_q
, done_q
))
2055 scsi_eh_offline_sdevs(work_q
,
2058 EXPORT_SYMBOL_GPL(scsi_eh_ready_devs
);
2061 * scsi_eh_flush_done_q - finish processed commands or retry them.
2062 * @done_q: list_head of processed commands.
2064 void scsi_eh_flush_done_q(struct list_head
*done_q
)
2066 struct scsi_cmnd
*scmd
, *next
;
2068 list_for_each_entry_safe(scmd
, next
, done_q
, eh_entry
) {
2069 list_del_init(&scmd
->eh_entry
);
2070 if (scsi_device_online(scmd
->device
) &&
2071 !scsi_noretry_cmd(scmd
) &&
2072 (++scmd
->retries
<= scmd
->allowed
)) {
2073 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush"
2077 scsi_queue_insert(scmd
, SCSI_MLQUEUE_EH_RETRY
);
2080 * If just we got sense for the device (called
2081 * scsi_eh_get_sense), scmd->result is already
2082 * set, do not set DRIVER_TIMEOUT.
2085 scmd
->result
|= (DRIVER_TIMEOUT
<< 24);
2086 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush finish"
2088 current
->comm
, scmd
));
2089 scsi_finish_command(scmd
);
2093 EXPORT_SYMBOL(scsi_eh_flush_done_q
);
2096 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
2097 * @shost: Host to unjam.
2100 * When we come in here, we *know* that all commands on the bus have
2101 * either completed, failed or timed out. we also know that no further
2102 * commands are being sent to the host, so things are relatively quiet
2103 * and we have freedom to fiddle with things as we wish.
2105 * This is only the *default* implementation. it is possible for
2106 * individual drivers to supply their own version of this function, and
2107 * if the maintainer wishes to do this, it is strongly suggested that
2108 * this function be taken as a template and modified. this function
2109 * was designed to correctly handle problems for about 95% of the
2110 * different cases out there, and it should always provide at least a
2111 * reasonable amount of error recovery.
2113 * Any command marked 'failed' or 'timeout' must eventually have
2114 * scsi_finish_cmd() called for it. we do all of the retry stuff
2115 * here, so when we restart the host after we return it should have an
2118 static void scsi_unjam_host(struct Scsi_Host
*shost
)
2120 unsigned long flags
;
2121 LIST_HEAD(eh_work_q
);
2122 LIST_HEAD(eh_done_q
);
2124 spin_lock_irqsave(shost
->host_lock
, flags
);
2125 list_splice_init(&shost
->eh_cmd_q
, &eh_work_q
);
2126 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2128 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost
, &eh_work_q
));
2130 if (!scsi_eh_get_sense(&eh_work_q
, &eh_done_q
))
2131 if (!scsi_eh_abort_cmds(&eh_work_q
, &eh_done_q
))
2132 scsi_eh_ready_devs(shost
, &eh_work_q
, &eh_done_q
);
2134 spin_lock_irqsave(shost
->host_lock
, flags
);
2135 if (shost
->eh_deadline
)
2136 shost
->last_reset
= 0;
2137 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2138 scsi_eh_flush_done_q(&eh_done_q
);
2142 * scsi_error_handler - SCSI error handler thread
2143 * @data: Host for which we are running.
2146 * This is the main error handling loop. This is run as a kernel thread
2147 * for every SCSI host and handles all error handling activity.
2149 int scsi_error_handler(void *data
)
2151 struct Scsi_Host
*shost
= data
;
2154 * We use TASK_INTERRUPTIBLE so that the thread is not
2155 * counted against the load average as a running process.
2156 * We never actually get interrupted because kthread_run
2157 * disables signal delivery for the created thread.
2159 while (!kthread_should_stop()) {
2160 set_current_state(TASK_INTERRUPTIBLE
);
2161 if ((shost
->host_failed
== 0 && shost
->host_eh_scheduled
== 0) ||
2162 shost
->host_failed
!= shost
->host_busy
) {
2163 SCSI_LOG_ERROR_RECOVERY(1,
2164 printk("scsi_eh_%d: sleeping\n",
2170 __set_current_state(TASK_RUNNING
);
2171 SCSI_LOG_ERROR_RECOVERY(1,
2172 printk("scsi_eh_%d: waking up %d/%d/%d\n",
2173 shost
->host_no
, shost
->host_eh_scheduled
,
2174 shost
->host_failed
, shost
->host_busy
));
2177 * We have a host that is failing for some reason. Figure out
2178 * what we need to do to get it up and online again (if we can).
2179 * If we fail, we end up taking the thing offline.
2181 if (!shost
->eh_noresume
&& scsi_autopm_get_host(shost
) != 0) {
2182 SCSI_LOG_ERROR_RECOVERY(1,
2183 printk(KERN_ERR
"Error handler scsi_eh_%d "
2184 "unable to autoresume\n",
2189 if (shost
->transportt
->eh_strategy_handler
)
2190 shost
->transportt
->eh_strategy_handler(shost
);
2192 scsi_unjam_host(shost
);
2195 * Note - if the above fails completely, the action is to take
2196 * individual devices offline and flush the queue of any
2197 * outstanding requests that may have been pending. When we
2198 * restart, we restart any I/O to any other devices on the bus
2199 * which are still online.
2201 scsi_restart_operations(shost
);
2202 if (!shost
->eh_noresume
)
2203 scsi_autopm_put_host(shost
);
2205 __set_current_state(TASK_RUNNING
);
2207 SCSI_LOG_ERROR_RECOVERY(1,
2208 printk("Error handler scsi_eh_%d exiting\n", shost
->host_no
));
2209 shost
->ehandler
= NULL
;
2214 * Function: scsi_report_bus_reset()
2216 * Purpose: Utility function used by low-level drivers to report that
2217 * they have observed a bus reset on the bus being handled.
2219 * Arguments: shost - Host in question
2220 * channel - channel on which reset was observed.
2224 * Lock status: Host lock must be held.
2226 * Notes: This only needs to be called if the reset is one which
2227 * originates from an unknown location. Resets originated
2228 * by the mid-level itself don't need to call this, but there
2229 * should be no harm.
2231 * The main purpose of this is to make sure that a CHECK_CONDITION
2232 * is properly treated.
2234 void scsi_report_bus_reset(struct Scsi_Host
*shost
, int channel
)
2236 struct scsi_device
*sdev
;
2238 __shost_for_each_device(sdev
, shost
) {
2239 if (channel
== sdev_channel(sdev
))
2240 __scsi_report_device_reset(sdev
, NULL
);
2243 EXPORT_SYMBOL(scsi_report_bus_reset
);
2246 * Function: scsi_report_device_reset()
2248 * Purpose: Utility function used by low-level drivers to report that
2249 * they have observed a device reset on the device being handled.
2251 * Arguments: shost - Host in question
2252 * channel - channel on which reset was observed
2253 * target - target on which reset was observed
2257 * Lock status: Host lock must be held
2259 * Notes: This only needs to be called if the reset is one which
2260 * originates from an unknown location. Resets originated
2261 * by the mid-level itself don't need to call this, but there
2262 * should be no harm.
2264 * The main purpose of this is to make sure that a CHECK_CONDITION
2265 * is properly treated.
2267 void scsi_report_device_reset(struct Scsi_Host
*shost
, int channel
, int target
)
2269 struct scsi_device
*sdev
;
2271 __shost_for_each_device(sdev
, shost
) {
2272 if (channel
== sdev_channel(sdev
) &&
2273 target
== sdev_id(sdev
))
2274 __scsi_report_device_reset(sdev
, NULL
);
2277 EXPORT_SYMBOL(scsi_report_device_reset
);
2280 scsi_reset_provider_done_command(struct scsi_cmnd
*scmd
)
2285 * Function: scsi_reset_provider
2287 * Purpose: Send requested reset to a bus or device at any phase.
2289 * Arguments: device - device to send reset to
2290 * flag - reset type (see scsi.h)
2292 * Returns: SUCCESS/FAILURE.
2294 * Notes: This is used by the SCSI Generic driver to provide
2295 * Bus/Device reset capability.
2298 scsi_reset_provider(struct scsi_device
*dev
, int flag
)
2300 struct scsi_cmnd
*scmd
;
2301 struct Scsi_Host
*shost
= dev
->host
;
2303 unsigned long flags
;
2306 if (scsi_autopm_get_host(shost
) < 0)
2309 scmd
= scsi_get_command(dev
, GFP_KERNEL
);
2310 blk_rq_init(NULL
, &req
);
2311 scmd
->request
= &req
;
2313 scmd
->cmnd
= req
.cmd
;
2315 scmd
->scsi_done
= scsi_reset_provider_done_command
;
2316 memset(&scmd
->sdb
, 0, sizeof(scmd
->sdb
));
2320 scmd
->sc_data_direction
= DMA_BIDIRECTIONAL
;
2322 spin_lock_irqsave(shost
->host_lock
, flags
);
2323 shost
->tmf_in_progress
= 1;
2324 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2327 case SCSI_TRY_RESET_DEVICE
:
2328 rtn
= scsi_try_bus_device_reset(scmd
);
2332 case SCSI_TRY_RESET_TARGET
:
2333 rtn
= scsi_try_target_reset(scmd
);
2337 case SCSI_TRY_RESET_BUS
:
2338 rtn
= scsi_try_bus_reset(scmd
);
2342 case SCSI_TRY_RESET_HOST
:
2343 rtn
= scsi_try_host_reset(scmd
);
2349 spin_lock_irqsave(shost
->host_lock
, flags
);
2350 shost
->tmf_in_progress
= 0;
2351 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2354 * be sure to wake up anyone who was sleeping or had their queue
2355 * suspended while we performed the TMF.
2357 SCSI_LOG_ERROR_RECOVERY(3,
2358 printk("%s: waking up host to restart after TMF\n",
2361 wake_up(&shost
->host_wait
);
2363 scsi_run_host_queues(shost
);
2365 scsi_next_command(scmd
);
2366 scsi_autopm_put_host(shost
);
2369 EXPORT_SYMBOL(scsi_reset_provider
);
2372 * scsi_normalize_sense - normalize main elements from either fixed or
2373 * descriptor sense data format into a common format.
2375 * @sense_buffer: byte array containing sense data returned by device
2376 * @sb_len: number of valid bytes in sense_buffer
2377 * @sshdr: pointer to instance of structure that common
2378 * elements are written to.
2381 * The "main elements" from sense data are: response_code, sense_key,
2382 * asc, ascq and additional_length (only for descriptor format).
2384 * Typically this function can be called after a device has
2385 * responded to a SCSI command with the CHECK_CONDITION status.
2388 * 1 if valid sense data information found, else 0;
2390 int scsi_normalize_sense(const u8
*sense_buffer
, int sb_len
,
2391 struct scsi_sense_hdr
*sshdr
)
2393 if (!sense_buffer
|| !sb_len
)
2396 memset(sshdr
, 0, sizeof(struct scsi_sense_hdr
));
2398 sshdr
->response_code
= (sense_buffer
[0] & 0x7f);
2400 if (!scsi_sense_valid(sshdr
))
2403 if (sshdr
->response_code
>= 0x72) {
2408 sshdr
->sense_key
= (sense_buffer
[1] & 0xf);
2410 sshdr
->asc
= sense_buffer
[2];
2412 sshdr
->ascq
= sense_buffer
[3];
2414 sshdr
->additional_length
= sense_buffer
[7];
2420 sshdr
->sense_key
= (sense_buffer
[2] & 0xf);
2422 sb_len
= (sb_len
< (sense_buffer
[7] + 8)) ?
2423 sb_len
: (sense_buffer
[7] + 8);
2425 sshdr
->asc
= sense_buffer
[12];
2427 sshdr
->ascq
= sense_buffer
[13];
2433 EXPORT_SYMBOL(scsi_normalize_sense
);
2435 int scsi_command_normalize_sense(struct scsi_cmnd
*cmd
,
2436 struct scsi_sense_hdr
*sshdr
)
2438 return scsi_normalize_sense(cmd
->sense_buffer
,
2439 SCSI_SENSE_BUFFERSIZE
, sshdr
);
2441 EXPORT_SYMBOL(scsi_command_normalize_sense
);
2444 * scsi_sense_desc_find - search for a given descriptor type in descriptor sense data format.
2445 * @sense_buffer: byte array of descriptor format sense data
2446 * @sb_len: number of valid bytes in sense_buffer
2447 * @desc_type: value of descriptor type to find
2448 * (e.g. 0 -> information)
2451 * only valid when sense data is in descriptor format
2454 * pointer to start of (first) descriptor if found else NULL
2456 const u8
* scsi_sense_desc_find(const u8
* sense_buffer
, int sb_len
,
2459 int add_sen_len
, add_len
, desc_len
, k
;
2462 if ((sb_len
< 8) || (0 == (add_sen_len
= sense_buffer
[7])))
2464 if ((sense_buffer
[0] < 0x72) || (sense_buffer
[0] > 0x73))
2466 add_sen_len
= (add_sen_len
< (sb_len
- 8)) ?
2467 add_sen_len
: (sb_len
- 8);
2468 descp
= &sense_buffer
[8];
2469 for (desc_len
= 0, k
= 0; k
< add_sen_len
; k
+= desc_len
) {
2471 add_len
= (k
< (add_sen_len
- 1)) ? descp
[1]: -1;
2472 desc_len
= add_len
+ 2;
2473 if (descp
[0] == desc_type
)
2475 if (add_len
< 0) // short descriptor ??
2480 EXPORT_SYMBOL(scsi_sense_desc_find
);
2483 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
2484 * @sense_buffer: byte array of sense data
2485 * @sb_len: number of valid bytes in sense_buffer
2486 * @info_out: pointer to 64 integer where 8 or 4 byte information
2487 * field will be placed if found.
2490 * 1 if information field found, 0 if not found.
2492 int scsi_get_sense_info_fld(const u8
* sense_buffer
, int sb_len
,
2501 switch (sense_buffer
[0] & 0x7f) {
2504 if (sense_buffer
[0] & 0x80) {
2505 *info_out
= (sense_buffer
[3] << 24) +
2506 (sense_buffer
[4] << 16) +
2507 (sense_buffer
[5] << 8) + sense_buffer
[6];
2513 ucp
= scsi_sense_desc_find(sense_buffer
, sb_len
,
2515 if (ucp
&& (0xa == ucp
[1])) {
2517 for (j
= 0; j
< 8; ++j
) {
2530 EXPORT_SYMBOL(scsi_get_sense_info_fld
);
2533 * scsi_build_sense_buffer - build sense data in a buffer
2534 * @desc: Sense format (non zero == descriptor format,
2535 * 0 == fixed format)
2536 * @buf: Where to build sense data
2538 * @asc: Additional sense code
2539 * @ascq: Additional sense code qualifier
2542 void scsi_build_sense_buffer(int desc
, u8
*buf
, u8 key
, u8 asc
, u8 ascq
)
2545 buf
[0] = 0x72; /* descriptor, current */
2551 buf
[0] = 0x70; /* fixed, current */
2558 EXPORT_SYMBOL(scsi_build_sense_buffer
);