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
);
57 /* called with shost->host_lock held */
58 void scsi_eh_wakeup(struct Scsi_Host
*shost
)
60 if (shost
->host_busy
== shost
->host_failed
) {
61 trace_scsi_eh_wakeup(shost
);
62 wake_up_process(shost
->ehandler
);
63 SCSI_LOG_ERROR_RECOVERY(5,
64 printk("Waking error handler thread\n"));
69 * scsi_schedule_eh - schedule EH for SCSI host
70 * @shost: SCSI host to invoke error handling on.
72 * Schedule SCSI EH without scmd.
74 void scsi_schedule_eh(struct Scsi_Host
*shost
)
78 spin_lock_irqsave(shost
->host_lock
, flags
);
80 if (scsi_host_set_state(shost
, SHOST_RECOVERY
) == 0 ||
81 scsi_host_set_state(shost
, SHOST_CANCEL_RECOVERY
) == 0) {
82 shost
->host_eh_scheduled
++;
83 scsi_eh_wakeup(shost
);
86 spin_unlock_irqrestore(shost
->host_lock
, flags
);
88 EXPORT_SYMBOL_GPL(scsi_schedule_eh
);
90 static int scsi_host_eh_past_deadline(struct Scsi_Host
*shost
)
92 if (!shost
->last_reset
|| !shost
->eh_deadline
)
95 if (time_before(jiffies
,
96 shost
->last_reset
+ shost
->eh_deadline
))
103 * scsi_eh_scmd_add - add scsi cmd to error handling.
104 * @scmd: scmd to run eh on.
105 * @eh_flag: optional SCSI_EH flag.
110 int scsi_eh_scmd_add(struct scsi_cmnd
*scmd
, int eh_flag
)
112 struct Scsi_Host
*shost
= scmd
->device
->host
;
116 if (!shost
->ehandler
)
119 spin_lock_irqsave(shost
->host_lock
, flags
);
120 if (scsi_host_set_state(shost
, SHOST_RECOVERY
))
121 if (scsi_host_set_state(shost
, SHOST_CANCEL_RECOVERY
))
124 if (shost
->eh_deadline
&& !shost
->last_reset
)
125 shost
->last_reset
= jiffies
;
128 scmd
->eh_eflags
|= eh_flag
;
129 list_add_tail(&scmd
->eh_entry
, &shost
->eh_cmd_q
);
130 shost
->host_failed
++;
131 scsi_eh_wakeup(shost
);
133 spin_unlock_irqrestore(shost
->host_lock
, flags
);
138 * scsi_times_out - Timeout function for normal scsi commands.
139 * @req: request that is timing out.
142 * We do not need to lock this. There is the potential for a race
143 * only in that the normal completion handling might run, but if the
144 * normal completion function determines that the timer has already
145 * fired, then it mustn't do anything.
147 enum blk_eh_timer_return
scsi_times_out(struct request
*req
)
149 struct scsi_cmnd
*scmd
= req
->special
;
150 enum blk_eh_timer_return rtn
= BLK_EH_NOT_HANDLED
;
151 struct Scsi_Host
*host
= scmd
->device
->host
;
153 trace_scsi_dispatch_cmd_timeout(scmd
);
154 scsi_log_completion(scmd
, TIMEOUT_ERROR
);
156 if (host
->eh_deadline
&& !host
->last_reset
)
157 host
->last_reset
= jiffies
;
159 if (host
->transportt
->eh_timed_out
)
160 rtn
= host
->transportt
->eh_timed_out(scmd
);
161 else if (host
->hostt
->eh_timed_out
)
162 rtn
= host
->hostt
->eh_timed_out(scmd
);
164 scmd
->result
|= DID_TIME_OUT
<< 16;
166 if (unlikely(rtn
== BLK_EH_NOT_HANDLED
&&
167 !scsi_eh_scmd_add(scmd
, SCSI_EH_CANCEL_CMD
)))
168 rtn
= BLK_EH_HANDLED
;
174 * scsi_block_when_processing_errors - Prevent cmds from being queued.
175 * @sdev: Device on which we are performing recovery.
178 * We block until the host is out of error recovery, and then check to
179 * see whether the host or the device is offline.
182 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
184 int scsi_block_when_processing_errors(struct scsi_device
*sdev
)
188 wait_event(sdev
->host
->host_wait
, !scsi_host_in_recovery(sdev
->host
));
190 online
= scsi_device_online(sdev
);
192 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: rtn: %d\n", __func__
,
197 EXPORT_SYMBOL(scsi_block_when_processing_errors
);
199 #ifdef CONFIG_SCSI_LOGGING
201 * scsi_eh_prt_fail_stats - Log info on failures.
202 * @shost: scsi host being recovered.
203 * @work_q: Queue of scsi cmds to process.
205 static inline void scsi_eh_prt_fail_stats(struct Scsi_Host
*shost
,
206 struct list_head
*work_q
)
208 struct scsi_cmnd
*scmd
;
209 struct scsi_device
*sdev
;
210 int total_failures
= 0;
213 int devices_failed
= 0;
215 shost_for_each_device(sdev
, shost
) {
216 list_for_each_entry(scmd
, work_q
, eh_entry
) {
217 if (scmd
->device
== sdev
) {
219 if (scmd
->eh_eflags
& SCSI_EH_CANCEL_CMD
)
226 if (cmd_cancel
|| cmd_failed
) {
227 SCSI_LOG_ERROR_RECOVERY(3,
228 sdev_printk(KERN_INFO
, sdev
,
229 "%s: cmds failed: %d, cancel: %d\n",
230 __func__
, cmd_failed
,
238 SCSI_LOG_ERROR_RECOVERY(2, printk("Total of %d commands on %d"
239 " devices require eh work\n",
240 total_failures
, devices_failed
));
245 * scsi_report_lun_change - Set flag on all *other* devices on the same target
246 * to indicate that a UNIT ATTENTION is expected.
247 * @sdev: Device reporting the UNIT ATTENTION
249 static void scsi_report_lun_change(struct scsi_device
*sdev
)
251 sdev
->sdev_target
->expecting_lun_change
= 1;
255 * scsi_report_sense - Examine scsi sense information and log messages for
256 * certain conditions, also issue uevents for some of them.
257 * @sdev: Device reporting the sense code
258 * @sshdr: sshdr to be examined
260 static void scsi_report_sense(struct scsi_device
*sdev
,
261 struct scsi_sense_hdr
*sshdr
)
263 enum scsi_device_event evt_type
= SDEV_EVT_MAXBITS
; /* i.e. none */
265 if (sshdr
->sense_key
== UNIT_ATTENTION
) {
266 if (sshdr
->asc
== 0x3f && sshdr
->ascq
== 0x03) {
267 evt_type
= SDEV_EVT_INQUIRY_CHANGE_REPORTED
;
268 sdev_printk(KERN_WARNING
, sdev
,
269 "Inquiry data has changed");
270 } else if (sshdr
->asc
== 0x3f && sshdr
->ascq
== 0x0e) {
271 evt_type
= SDEV_EVT_LUN_CHANGE_REPORTED
;
272 scsi_report_lun_change(sdev
);
273 sdev_printk(KERN_WARNING
, sdev
,
274 "Warning! Received an indication that the "
275 "LUN assignments on this target have "
276 "changed. The Linux SCSI layer does not "
277 "automatically remap LUN assignments.\n");
278 } else if (sshdr
->asc
== 0x3f)
279 sdev_printk(KERN_WARNING
, sdev
,
280 "Warning! Received an indication that the "
281 "operating parameters on this target have "
282 "changed. The Linux SCSI layer does not "
283 "automatically adjust these parameters.\n");
285 if (sshdr
->asc
== 0x38 && sshdr
->ascq
== 0x07) {
286 evt_type
= SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED
;
287 sdev_printk(KERN_WARNING
, sdev
,
288 "Warning! Received an indication that the "
289 "LUN reached a thin provisioning soft "
293 if (sshdr
->asc
== 0x2a && sshdr
->ascq
== 0x01) {
294 evt_type
= SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED
;
295 sdev_printk(KERN_WARNING
, sdev
,
296 "Mode parameters changed");
297 } else if (sshdr
->asc
== 0x2a && sshdr
->ascq
== 0x09) {
298 evt_type
= SDEV_EVT_CAPACITY_CHANGE_REPORTED
;
299 sdev_printk(KERN_WARNING
, sdev
,
300 "Capacity data has changed");
301 } else if (sshdr
->asc
== 0x2a)
302 sdev_printk(KERN_WARNING
, sdev
,
303 "Parameters changed");
306 if (evt_type
!= SDEV_EVT_MAXBITS
) {
307 set_bit(evt_type
, sdev
->pending_events
);
308 schedule_work(&sdev
->event_work
);
313 * scsi_check_sense - Examine scsi cmd sense
314 * @scmd: Cmd to have sense checked.
317 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
320 * When a deferred error is detected the current command has
321 * not been executed and needs retrying.
323 static int scsi_check_sense(struct scsi_cmnd
*scmd
)
325 struct scsi_device
*sdev
= scmd
->device
;
326 struct scsi_sense_hdr sshdr
;
328 if (! scsi_command_normalize_sense(scmd
, &sshdr
))
329 return FAILED
; /* no valid sense data */
331 if (scmd
->cmnd
[0] == TEST_UNIT_READY
&& scmd
->scsi_done
!= scsi_eh_done
)
333 * nasty: for mid-layer issued TURs, we need to return the
334 * actual sense data without any recovery attempt. For eh
335 * issued ones, we need to try to recover and interpret
339 scsi_report_sense(sdev
, &sshdr
);
341 if (scsi_sense_is_deferred(&sshdr
))
344 if (sdev
->scsi_dh_data
&& sdev
->scsi_dh_data
->scsi_dh
&&
345 sdev
->scsi_dh_data
->scsi_dh
->check_sense
) {
348 rc
= sdev
->scsi_dh_data
->scsi_dh
->check_sense(sdev
, &sshdr
);
349 if (rc
!= SCSI_RETURN_NOT_HANDLED
)
351 /* handler does not care. Drop down to default handling */
355 * Previous logic looked for FILEMARK, EOM or ILI which are
356 * mainly associated with tapes and returned SUCCESS.
358 if (sshdr
.response_code
== 0x70) {
360 if (scmd
->sense_buffer
[2] & 0xe0)
364 * descriptor format: look for "stream commands sense data
365 * descriptor" (see SSC-3). Assume single sense data
366 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
368 if ((sshdr
.additional_length
> 3) &&
369 (scmd
->sense_buffer
[8] == 0x4) &&
370 (scmd
->sense_buffer
[11] & 0xe0))
374 switch (sshdr
.sense_key
) {
377 case RECOVERED_ERROR
:
378 return /* soft_error */ SUCCESS
;
380 case ABORTED_COMMAND
:
381 if (sshdr
.asc
== 0x10) /* DIF */
388 * if we are expecting a cc/ua because of a bus reset that we
389 * performed, treat this just as a retry. otherwise this is
390 * information that we should pass up to the upper-level driver
391 * so that we can deal with it there.
393 if (scmd
->device
->expecting_cc_ua
) {
395 * Because some device does not queue unit
396 * attentions correctly, we carefully check
397 * additional sense code and qualifier so as
398 * not to squash media change unit attention.
400 if (sshdr
.asc
!= 0x28 || sshdr
.ascq
!= 0x00) {
401 scmd
->device
->expecting_cc_ua
= 0;
406 * we might also expect a cc/ua if another LUN on the target
407 * reported a UA with an ASC/ASCQ of 3F 0E -
408 * REPORTED LUNS DATA HAS CHANGED.
410 if (scmd
->device
->sdev_target
->expecting_lun_change
&&
411 sshdr
.asc
== 0x3f && sshdr
.ascq
== 0x0e)
414 * if the device is in the process of becoming ready, we
417 if ((sshdr
.asc
== 0x04) && (sshdr
.ascq
== 0x01))
420 * if the device is not started, we need to wake
421 * the error handler to start the motor
423 if (scmd
->device
->allow_restart
&&
424 (sshdr
.asc
== 0x04) && (sshdr
.ascq
== 0x02))
427 * Pass the UA upwards for a determination in the completion
432 /* these are not supported */
434 if (sshdr
.asc
== 0x27 && sshdr
.ascq
== 0x07) {
435 /* Thin provisioning hard threshold reached */
436 set_host_byte(scmd
, DID_ALLOC_FAILURE
);
440 case VOLUME_OVERFLOW
:
443 set_host_byte(scmd
, DID_TARGET_FAILURE
);
447 if (sshdr
.asc
== 0x11 || /* UNRECOVERED READ ERR */
448 sshdr
.asc
== 0x13 || /* AMNF DATA FIELD */
449 sshdr
.asc
== 0x14) { /* RECORD NOT FOUND */
450 set_host_byte(scmd
, DID_MEDIUM_ERROR
);
456 if (scmd
->device
->retry_hwerror
)
457 return ADD_TO_MLQUEUE
;
459 set_host_byte(scmd
, DID_TARGET_FAILURE
);
461 case ILLEGAL_REQUEST
:
462 if (sshdr
.asc
== 0x20 || /* Invalid command operation code */
463 sshdr
.asc
== 0x21 || /* Logical block address out of range */
464 sshdr
.asc
== 0x24 || /* Invalid field in cdb */
465 sshdr
.asc
== 0x26) { /* Parameter value invalid */
466 set_host_byte(scmd
, DID_TARGET_FAILURE
);
475 static void scsi_handle_queue_ramp_up(struct scsi_device
*sdev
)
477 struct scsi_host_template
*sht
= sdev
->host
->hostt
;
478 struct scsi_device
*tmp_sdev
;
480 if (!sht
->change_queue_depth
||
481 sdev
->queue_depth
>= sdev
->max_queue_depth
)
484 if (time_before(jiffies
,
485 sdev
->last_queue_ramp_up
+ sdev
->queue_ramp_up_period
))
488 if (time_before(jiffies
,
489 sdev
->last_queue_full_time
+ sdev
->queue_ramp_up_period
))
493 * Walk all devices of a target and do
496 shost_for_each_device(tmp_sdev
, sdev
->host
) {
497 if (tmp_sdev
->channel
!= sdev
->channel
||
498 tmp_sdev
->id
!= sdev
->id
||
499 tmp_sdev
->queue_depth
== sdev
->max_queue_depth
)
502 * call back into LLD to increase queue_depth by one
503 * with ramp up reason code.
505 sht
->change_queue_depth(tmp_sdev
, tmp_sdev
->queue_depth
+ 1,
506 SCSI_QDEPTH_RAMP_UP
);
507 sdev
->last_queue_ramp_up
= jiffies
;
511 static void scsi_handle_queue_full(struct scsi_device
*sdev
)
513 struct scsi_host_template
*sht
= sdev
->host
->hostt
;
514 struct scsi_device
*tmp_sdev
;
516 if (!sht
->change_queue_depth
)
519 shost_for_each_device(tmp_sdev
, sdev
->host
) {
520 if (tmp_sdev
->channel
!= sdev
->channel
||
521 tmp_sdev
->id
!= sdev
->id
)
524 * We do not know the number of commands that were at
525 * the device when we got the queue full so we start
526 * from the highest possible value and work our way down.
528 sht
->change_queue_depth(tmp_sdev
, tmp_sdev
->queue_depth
- 1,
534 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
535 * @scmd: SCSI cmd to examine.
538 * This is *only* called when we are examining the status of commands
539 * queued during error recovery. the main difference here is that we
540 * don't allow for the possibility of retries here, and we are a lot
541 * more restrictive about what we consider acceptable.
543 static int scsi_eh_completed_normally(struct scsi_cmnd
*scmd
)
546 * first check the host byte, to see if there is anything in there
547 * that would indicate what we need to do.
549 if (host_byte(scmd
->result
) == DID_RESET
) {
551 * rats. we are already in the error handler, so we now
552 * get to try and figure out what to do next. if the sense
553 * is valid, we have a pretty good idea of what to do.
554 * if not, we mark it as FAILED.
556 return scsi_check_sense(scmd
);
558 if (host_byte(scmd
->result
) != DID_OK
)
562 * next, check the message byte.
564 if (msg_byte(scmd
->result
) != COMMAND_COMPLETE
)
568 * now, check the status byte to see if this indicates
571 switch (status_byte(scmd
->result
)) {
573 scsi_handle_queue_ramp_up(scmd
->device
);
574 case COMMAND_TERMINATED
:
576 case CHECK_CONDITION
:
577 return scsi_check_sense(scmd
);
579 case INTERMEDIATE_GOOD
:
580 case INTERMEDIATE_C_GOOD
:
582 * who knows? FIXME(eric)
585 case RESERVATION_CONFLICT
:
586 if (scmd
->cmnd
[0] == TEST_UNIT_READY
)
587 /* it is a success, we probed the device and
590 /* otherwise, we failed to send the command */
593 scsi_handle_queue_full(scmd
->device
);
604 * scsi_eh_done - Completion function for error handling.
605 * @scmd: Cmd that is done.
607 static void scsi_eh_done(struct scsi_cmnd
*scmd
)
609 struct completion
*eh_action
;
611 SCSI_LOG_ERROR_RECOVERY(3,
612 printk("%s scmd: %p result: %x\n",
613 __func__
, scmd
, scmd
->result
));
615 eh_action
= scmd
->device
->host
->eh_action
;
621 * scsi_try_host_reset - ask host adapter to reset itself
622 * @scmd: SCSI cmd to send host reset.
624 static int scsi_try_host_reset(struct scsi_cmnd
*scmd
)
628 struct Scsi_Host
*host
= scmd
->device
->host
;
629 struct scsi_host_template
*hostt
= host
->hostt
;
631 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Host RST\n",
634 if (!hostt
->eh_host_reset_handler
)
637 rtn
= hostt
->eh_host_reset_handler(scmd
);
639 if (rtn
== SUCCESS
) {
640 if (!hostt
->skip_settle_delay
)
641 ssleep(HOST_RESET_SETTLE_TIME
);
642 spin_lock_irqsave(host
->host_lock
, flags
);
643 scsi_report_bus_reset(host
, scmd_channel(scmd
));
644 spin_unlock_irqrestore(host
->host_lock
, flags
);
651 * scsi_try_bus_reset - ask host to perform a bus reset
652 * @scmd: SCSI cmd to send bus reset.
654 static int scsi_try_bus_reset(struct scsi_cmnd
*scmd
)
658 struct Scsi_Host
*host
= scmd
->device
->host
;
659 struct scsi_host_template
*hostt
= host
->hostt
;
661 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Bus RST\n",
664 if (!hostt
->eh_bus_reset_handler
)
667 rtn
= hostt
->eh_bus_reset_handler(scmd
);
669 if (rtn
== SUCCESS
) {
670 if (!hostt
->skip_settle_delay
)
671 ssleep(BUS_RESET_SETTLE_TIME
);
672 spin_lock_irqsave(host
->host_lock
, flags
);
673 scsi_report_bus_reset(host
, scmd_channel(scmd
));
674 spin_unlock_irqrestore(host
->host_lock
, flags
);
680 static void __scsi_report_device_reset(struct scsi_device
*sdev
, void *data
)
683 sdev
->expecting_cc_ua
= 1;
687 * scsi_try_target_reset - Ask host to perform a target reset
688 * @scmd: SCSI cmd used to send a target reset
691 * There is no timeout for this operation. if this operation is
692 * unreliable for a given host, then the host itself needs to put a
693 * timer on it, and set the host back to a consistent state prior to
696 static int scsi_try_target_reset(struct scsi_cmnd
*scmd
)
700 struct Scsi_Host
*host
= scmd
->device
->host
;
701 struct scsi_host_template
*hostt
= host
->hostt
;
703 if (!hostt
->eh_target_reset_handler
)
706 rtn
= hostt
->eh_target_reset_handler(scmd
);
707 if (rtn
== SUCCESS
) {
708 spin_lock_irqsave(host
->host_lock
, flags
);
709 __starget_for_each_device(scsi_target(scmd
->device
), NULL
,
710 __scsi_report_device_reset
);
711 spin_unlock_irqrestore(host
->host_lock
, flags
);
718 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
719 * @scmd: SCSI cmd used to send BDR
722 * There is no timeout for this operation. if this operation is
723 * unreliable for a given host, then the host itself needs to put a
724 * timer on it, and set the host back to a consistent state prior to
727 static int scsi_try_bus_device_reset(struct scsi_cmnd
*scmd
)
730 struct scsi_host_template
*hostt
= scmd
->device
->host
->hostt
;
732 if (!hostt
->eh_device_reset_handler
)
735 rtn
= hostt
->eh_device_reset_handler(scmd
);
737 __scsi_report_device_reset(scmd
->device
, NULL
);
741 static int scsi_try_to_abort_cmd(struct scsi_host_template
*hostt
, struct scsi_cmnd
*scmd
)
743 if (!hostt
->eh_abort_handler
)
746 return hostt
->eh_abort_handler(scmd
);
749 static void scsi_abort_eh_cmnd(struct scsi_cmnd
*scmd
)
751 if (scsi_try_to_abort_cmd(scmd
->device
->host
->hostt
, scmd
) != SUCCESS
)
752 if (scsi_try_bus_device_reset(scmd
) != SUCCESS
)
753 if (scsi_try_target_reset(scmd
) != SUCCESS
)
754 if (scsi_try_bus_reset(scmd
) != SUCCESS
)
755 scsi_try_host_reset(scmd
);
759 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
760 * @scmd: SCSI command structure to hijack
761 * @ses: structure to save restore information
762 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
763 * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
764 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
766 * This function is used to save a scsi command information before re-execution
767 * as part of the error recovery process. If @sense_bytes is 0 the command
768 * sent must be one that does not transfer any data. If @sense_bytes != 0
769 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
770 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
772 void scsi_eh_prep_cmnd(struct scsi_cmnd
*scmd
, struct scsi_eh_save
*ses
,
773 unsigned char *cmnd
, int cmnd_size
, unsigned sense_bytes
)
775 struct scsi_device
*sdev
= scmd
->device
;
778 * We need saved copies of a number of fields - this is because
779 * error handling may need to overwrite these with different values
780 * to run different commands, and once error handling is complete,
781 * we will need to restore these values prior to running the actual
784 ses
->cmd_len
= scmd
->cmd_len
;
785 ses
->cmnd
= scmd
->cmnd
;
786 ses
->data_direction
= scmd
->sc_data_direction
;
787 ses
->sdb
= scmd
->sdb
;
788 ses
->next_rq
= scmd
->request
->next_rq
;
789 ses
->result
= scmd
->result
;
790 ses
->underflow
= scmd
->underflow
;
791 ses
->prot_op
= scmd
->prot_op
;
793 scmd
->prot_op
= SCSI_PROT_NORMAL
;
794 scmd
->cmnd
= ses
->eh_cmnd
;
795 memset(scmd
->cmnd
, 0, BLK_MAX_CDB
);
796 memset(&scmd
->sdb
, 0, sizeof(scmd
->sdb
));
797 scmd
->request
->next_rq
= NULL
;
800 scmd
->sdb
.length
= min_t(unsigned, SCSI_SENSE_BUFFERSIZE
,
802 sg_init_one(&ses
->sense_sgl
, scmd
->sense_buffer
,
804 scmd
->sdb
.table
.sgl
= &ses
->sense_sgl
;
805 scmd
->sc_data_direction
= DMA_FROM_DEVICE
;
806 scmd
->sdb
.table
.nents
= 1;
807 scmd
->cmnd
[0] = REQUEST_SENSE
;
808 scmd
->cmnd
[4] = scmd
->sdb
.length
;
809 scmd
->cmd_len
= COMMAND_SIZE(scmd
->cmnd
[0]);
811 scmd
->sc_data_direction
= DMA_NONE
;
813 BUG_ON(cmnd_size
> BLK_MAX_CDB
);
814 memcpy(scmd
->cmnd
, cmnd
, cmnd_size
);
815 scmd
->cmd_len
= COMMAND_SIZE(scmd
->cmnd
[0]);
821 if (sdev
->scsi_level
<= SCSI_2
&& sdev
->scsi_level
!= SCSI_UNKNOWN
)
822 scmd
->cmnd
[1] = (scmd
->cmnd
[1] & 0x1f) |
823 (sdev
->lun
<< 5 & 0xe0);
826 * Zero the sense buffer. The scsi spec mandates that any
827 * untransferred sense data should be interpreted as being zero.
829 memset(scmd
->sense_buffer
, 0, SCSI_SENSE_BUFFERSIZE
);
831 EXPORT_SYMBOL(scsi_eh_prep_cmnd
);
834 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
835 * @scmd: SCSI command structure to restore
836 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
838 * Undo any damage done by above scsi_eh_prep_cmnd().
840 void scsi_eh_restore_cmnd(struct scsi_cmnd
* scmd
, struct scsi_eh_save
*ses
)
843 * Restore original data
845 scmd
->cmd_len
= ses
->cmd_len
;
846 scmd
->cmnd
= ses
->cmnd
;
847 scmd
->sc_data_direction
= ses
->data_direction
;
848 scmd
->sdb
= ses
->sdb
;
849 scmd
->request
->next_rq
= ses
->next_rq
;
850 scmd
->result
= ses
->result
;
851 scmd
->underflow
= ses
->underflow
;
852 scmd
->prot_op
= ses
->prot_op
;
854 EXPORT_SYMBOL(scsi_eh_restore_cmnd
);
857 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
858 * @scmd: SCSI command structure to hijack
860 * @cmnd_size: size in bytes of @cmnd
861 * @timeout: timeout for this request
862 * @sense_bytes: size of sense data to copy or 0
864 * This function is used to send a scsi command down to a target device
865 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
868 * SUCCESS or FAILED or NEEDS_RETRY
870 static int scsi_send_eh_cmnd(struct scsi_cmnd
*scmd
, unsigned char *cmnd
,
871 int cmnd_size
, int timeout
, unsigned sense_bytes
)
873 struct scsi_device
*sdev
= scmd
->device
;
874 struct Scsi_Host
*shost
= sdev
->host
;
875 DECLARE_COMPLETION_ONSTACK(done
);
876 unsigned long timeleft
= timeout
;
877 struct scsi_eh_save ses
;
878 const unsigned long stall_for
= msecs_to_jiffies(100);
882 scsi_eh_prep_cmnd(scmd
, &ses
, cmnd
, cmnd_size
, sense_bytes
);
883 shost
->eh_action
= &done
;
886 scmd
->scsi_done
= scsi_eh_done
;
887 rtn
= shost
->hostt
->queuecommand(shost
, scmd
);
889 if (timeleft
> stall_for
) {
890 scsi_eh_restore_cmnd(scmd
, &ses
);
891 timeleft
-= stall_for
;
892 msleep(jiffies_to_msecs(stall_for
));
895 /* signal not to enter either branch of the if () below */
899 timeleft
= wait_for_completion_timeout(&done
, timeout
);
902 shost
->eh_action
= NULL
;
904 scsi_log_completion(scmd
, rtn
);
906 SCSI_LOG_ERROR_RECOVERY(3,
907 printk("%s: scmd: %p, timeleft: %ld\n",
908 __func__
, scmd
, timeleft
));
911 * If there is time left scsi_eh_done got called, and we will examine
912 * the actual status codes to see whether the command actually did
913 * complete normally, else if we have a zero return and no time left,
914 * the command must still be pending, so abort it and return FAILED.
915 * If we never actually managed to issue the command, because
916 * ->queuecommand() kept returning non zero, use the rtn = FAILED
917 * value above (so don't execute either branch of the if)
920 rtn
= scsi_eh_completed_normally(scmd
);
921 SCSI_LOG_ERROR_RECOVERY(3,
922 printk("%s: scsi_eh_completed_normally %x\n",
938 scsi_abort_eh_cmnd(scmd
);
942 scsi_eh_restore_cmnd(scmd
, &ses
);
948 * scsi_request_sense - Request sense data from a particular target.
949 * @scmd: SCSI cmd for request sense.
952 * Some hosts automatically obtain this information, others require
953 * that we obtain it on our own. This function will *not* return until
954 * the command either times out, or it completes.
956 static int scsi_request_sense(struct scsi_cmnd
*scmd
)
958 return scsi_send_eh_cmnd(scmd
, NULL
, 0, scmd
->device
->eh_timeout
, ~0);
961 static int scsi_eh_action(struct scsi_cmnd
*scmd
, int rtn
)
963 if (scmd
->request
->cmd_type
!= REQ_TYPE_BLOCK_PC
) {
964 struct scsi_driver
*sdrv
= scsi_cmd_to_driver(scmd
);
966 rtn
= sdrv
->eh_action(scmd
, rtn
);
972 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
973 * @scmd: Original SCSI cmd that eh has finished.
974 * @done_q: Queue for processed commands.
977 * We don't want to use the normal command completion while we are are
978 * still handling errors - it may cause other commands to be queued,
979 * and that would disturb what we are doing. Thus we really want to
980 * keep a list of pending commands for final completion, and once we
981 * are ready to leave error handling we handle completion for real.
983 void scsi_eh_finish_cmd(struct scsi_cmnd
*scmd
, struct list_head
*done_q
)
985 scmd
->device
->host
->host_failed
--;
987 list_move_tail(&scmd
->eh_entry
, done_q
);
989 EXPORT_SYMBOL(scsi_eh_finish_cmd
);
992 * scsi_eh_get_sense - Get device sense data.
993 * @work_q: Queue of commands to process.
994 * @done_q: Queue of processed commands.
997 * See if we need to request sense information. if so, then get it
998 * now, so we have a better idea of what to do.
1001 * This has the unfortunate side effect that if a shost adapter does
1002 * not automatically request sense information, we end up shutting
1003 * it down before we request it.
1005 * All drivers should request sense information internally these days,
1006 * so for now all I have to say is tough noogies if you end up in here.
1008 * XXX: Long term this code should go away, but that needs an audit of
1011 int scsi_eh_get_sense(struct list_head
*work_q
,
1012 struct list_head
*done_q
)
1014 struct scsi_cmnd
*scmd
, *next
;
1015 struct Scsi_Host
*shost
;
1017 unsigned long flags
;
1019 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1020 if ((scmd
->eh_eflags
& SCSI_EH_CANCEL_CMD
) ||
1021 SCSI_SENSE_VALID(scmd
))
1024 shost
= scmd
->device
->host
;
1025 spin_lock_irqsave(shost
->host_lock
, flags
);
1026 if (scsi_host_eh_past_deadline(shost
)) {
1027 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1028 SCSI_LOG_ERROR_RECOVERY(3,
1029 shost_printk(KERN_INFO
, shost
,
1030 "skip %s, past eh deadline\n",
1034 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1035 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO
, scmd
,
1036 "%s: requesting sense\n",
1038 rtn
= scsi_request_sense(scmd
);
1042 SCSI_LOG_ERROR_RECOVERY(3, printk("sense requested for %p"
1043 " result %x\n", scmd
,
1045 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense("bh", scmd
));
1047 rtn
= scsi_decide_disposition(scmd
);
1050 * if the result was normal, then just pass it along to the
1054 /* we don't want this command reissued, just
1055 * finished with the sense data, so set
1056 * retries to the max allowed to ensure it
1057 * won't get reissued */
1058 scmd
->retries
= scmd
->allowed
;
1059 else if (rtn
!= NEEDS_RETRY
)
1062 scsi_eh_finish_cmd(scmd
, done_q
);
1065 return list_empty(work_q
);
1067 EXPORT_SYMBOL_GPL(scsi_eh_get_sense
);
1070 * scsi_eh_tur - Send TUR to device.
1071 * @scmd: &scsi_cmnd to send TUR
1074 * 0 - Device is ready. 1 - Device NOT ready.
1076 static int scsi_eh_tur(struct scsi_cmnd
*scmd
)
1078 static unsigned char tur_command
[6] = {TEST_UNIT_READY
, 0, 0, 0, 0, 0};
1079 int retry_cnt
= 1, rtn
;
1082 rtn
= scsi_send_eh_cmnd(scmd
, tur_command
, 6,
1083 scmd
->device
->eh_timeout
, 0);
1085 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd %p rtn %x\n",
1086 __func__
, scmd
, rtn
));
1101 * scsi_eh_test_devices - check if devices are responding from error recovery.
1102 * @cmd_list: scsi commands in error recovery.
1103 * @work_q: queue for commands which still need more error recovery
1104 * @done_q: queue for commands which are finished
1105 * @try_stu: boolean on if a STU command should be tried in addition to TUR.
1108 * Tests if devices are in a working state. Commands to devices now in
1109 * a working state are sent to the done_q while commands to devices which
1110 * are still failing to respond are returned to the work_q for more
1113 static int scsi_eh_test_devices(struct list_head
*cmd_list
,
1114 struct list_head
*work_q
,
1115 struct list_head
*done_q
, int try_stu
)
1117 struct scsi_cmnd
*scmd
, *next
;
1118 struct scsi_device
*sdev
;
1120 unsigned long flags
;
1122 while (!list_empty(cmd_list
)) {
1123 scmd
= list_entry(cmd_list
->next
, struct scsi_cmnd
, eh_entry
);
1124 sdev
= scmd
->device
;
1127 spin_lock_irqsave(sdev
->host
->host_lock
, flags
);
1128 if (scsi_host_eh_past_deadline(sdev
->host
)) {
1129 /* Push items back onto work_q */
1130 list_splice_init(cmd_list
, work_q
);
1131 spin_unlock_irqrestore(sdev
->host
->host_lock
,
1133 SCSI_LOG_ERROR_RECOVERY(3,
1134 shost_printk(KERN_INFO
, sdev
->host
,
1135 "skip %s, past eh deadline",
1139 spin_unlock_irqrestore(sdev
->host
->host_lock
, flags
);
1142 finish_cmds
= !scsi_device_online(scmd
->device
) ||
1143 (try_stu
&& !scsi_eh_try_stu(scmd
) &&
1144 !scsi_eh_tur(scmd
)) ||
1147 list_for_each_entry_safe(scmd
, next
, cmd_list
, eh_entry
)
1148 if (scmd
->device
== sdev
) {
1151 scsi_eh_action(scmd
, SUCCESS
) == SUCCESS
))
1152 scsi_eh_finish_cmd(scmd
, done_q
);
1154 list_move_tail(&scmd
->eh_entry
, work_q
);
1157 return list_empty(work_q
);
1162 * scsi_eh_abort_cmds - abort pending commands.
1163 * @work_q: &list_head for pending commands.
1164 * @done_q: &list_head for processed commands.
1167 * Try and see whether or not it makes sense to try and abort the
1168 * running command. This only works out to be the case if we have one
1169 * command that has timed out. If the command simply failed, it makes
1170 * no sense to try and abort the command, since as far as the shost
1171 * adapter is concerned, it isn't running.
1173 static int scsi_eh_abort_cmds(struct list_head
*work_q
,
1174 struct list_head
*done_q
)
1176 struct scsi_cmnd
*scmd
, *next
;
1177 LIST_HEAD(check_list
);
1179 struct Scsi_Host
*shost
;
1180 unsigned long flags
;
1182 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1183 if (!(scmd
->eh_eflags
& SCSI_EH_CANCEL_CMD
))
1185 shost
= scmd
->device
->host
;
1186 spin_lock_irqsave(shost
->host_lock
, flags
);
1187 if (scsi_host_eh_past_deadline(shost
)) {
1188 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1189 list_splice_init(&check_list
, work_q
);
1190 SCSI_LOG_ERROR_RECOVERY(3,
1191 shost_printk(KERN_INFO
, shost
,
1192 "skip %s, past eh deadline\n",
1194 return list_empty(work_q
);
1196 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1197 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting cmd:"
1198 "0x%p\n", current
->comm
,
1200 rtn
= scsi_try_to_abort_cmd(shost
->hostt
, scmd
);
1201 if (rtn
== FAILED
) {
1202 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting"
1207 list_splice_init(&check_list
, work_q
);
1208 return list_empty(work_q
);
1210 scmd
->eh_eflags
&= ~SCSI_EH_CANCEL_CMD
;
1211 if (rtn
== FAST_IO_FAIL
)
1212 scsi_eh_finish_cmd(scmd
, done_q
);
1214 list_move_tail(&scmd
->eh_entry
, &check_list
);
1217 return scsi_eh_test_devices(&check_list
, work_q
, done_q
, 0);
1221 * scsi_eh_try_stu - Send START_UNIT to device.
1222 * @scmd: &scsi_cmnd to send START_UNIT
1225 * 0 - Device is ready. 1 - Device NOT ready.
1227 static int scsi_eh_try_stu(struct scsi_cmnd
*scmd
)
1229 static unsigned char stu_command
[6] = {START_STOP
, 0, 0, 0, 1, 0};
1231 if (scmd
->device
->allow_restart
) {
1232 int i
, rtn
= NEEDS_RETRY
;
1234 for (i
= 0; rtn
== NEEDS_RETRY
&& i
< 2; i
++)
1235 rtn
= scsi_send_eh_cmnd(scmd
, stu_command
, 6, scmd
->device
->request_queue
->rq_timeout
, 0);
1245 * scsi_eh_stu - send START_UNIT if needed
1246 * @shost: &scsi host being recovered.
1247 * @work_q: &list_head for pending commands.
1248 * @done_q: &list_head for processed commands.
1251 * If commands are failing due to not ready, initializing command required,
1252 * try revalidating the device, which will end up sending a start unit.
1254 static int scsi_eh_stu(struct Scsi_Host
*shost
,
1255 struct list_head
*work_q
,
1256 struct list_head
*done_q
)
1258 struct scsi_cmnd
*scmd
, *stu_scmd
, *next
;
1259 struct scsi_device
*sdev
;
1260 unsigned long flags
;
1262 shost_for_each_device(sdev
, shost
) {
1263 spin_lock_irqsave(shost
->host_lock
, flags
);
1264 if (scsi_host_eh_past_deadline(shost
)) {
1265 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1266 SCSI_LOG_ERROR_RECOVERY(3,
1267 shost_printk(KERN_INFO
, shost
,
1268 "skip %s, past eh deadline\n",
1272 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1274 list_for_each_entry(scmd
, work_q
, eh_entry
)
1275 if (scmd
->device
== sdev
&& SCSI_SENSE_VALID(scmd
) &&
1276 scsi_check_sense(scmd
) == FAILED
) {
1284 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending START_UNIT to sdev:"
1285 " 0x%p\n", current
->comm
, sdev
));
1287 if (!scsi_eh_try_stu(stu_scmd
)) {
1288 if (!scsi_device_online(sdev
) ||
1289 !scsi_eh_tur(stu_scmd
)) {
1290 list_for_each_entry_safe(scmd
, next
,
1292 if (scmd
->device
== sdev
&&
1293 scsi_eh_action(scmd
, SUCCESS
) == SUCCESS
)
1294 scsi_eh_finish_cmd(scmd
, done_q
);
1298 SCSI_LOG_ERROR_RECOVERY(3,
1299 printk("%s: START_UNIT failed to sdev:"
1300 " 0x%p\n", current
->comm
, sdev
));
1304 return list_empty(work_q
);
1309 * scsi_eh_bus_device_reset - send bdr if needed
1310 * @shost: scsi host being recovered.
1311 * @work_q: &list_head for pending commands.
1312 * @done_q: &list_head for processed commands.
1315 * Try a bus device reset. Still, look to see whether we have multiple
1316 * devices that are jammed or not - if we have multiple devices, it
1317 * makes no sense to try bus_device_reset - we really would need to try
1318 * a bus_reset instead.
1320 static int scsi_eh_bus_device_reset(struct Scsi_Host
*shost
,
1321 struct list_head
*work_q
,
1322 struct list_head
*done_q
)
1324 struct scsi_cmnd
*scmd
, *bdr_scmd
, *next
;
1325 struct scsi_device
*sdev
;
1326 unsigned long flags
;
1329 shost_for_each_device(sdev
, shost
) {
1330 spin_lock_irqsave(shost
->host_lock
, flags
);
1331 if (scsi_host_eh_past_deadline(shost
)) {
1332 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1333 SCSI_LOG_ERROR_RECOVERY(3,
1334 shost_printk(KERN_INFO
, shost
,
1335 "skip %s, past eh deadline\n",
1339 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1341 list_for_each_entry(scmd
, work_q
, eh_entry
)
1342 if (scmd
->device
== sdev
) {
1350 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BDR sdev:"
1351 " 0x%p\n", current
->comm
,
1353 rtn
= scsi_try_bus_device_reset(bdr_scmd
);
1354 if (rtn
== SUCCESS
|| rtn
== FAST_IO_FAIL
) {
1355 if (!scsi_device_online(sdev
) ||
1356 rtn
== FAST_IO_FAIL
||
1357 !scsi_eh_tur(bdr_scmd
)) {
1358 list_for_each_entry_safe(scmd
, next
,
1360 if (scmd
->device
== sdev
&&
1361 scsi_eh_action(scmd
, rtn
) != FAILED
)
1362 scsi_eh_finish_cmd(scmd
,
1367 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BDR"
1375 return list_empty(work_q
);
1379 * scsi_eh_target_reset - send target reset if needed
1380 * @shost: scsi host being recovered.
1381 * @work_q: &list_head for pending commands.
1382 * @done_q: &list_head for processed commands.
1385 * Try a target reset.
1387 static int scsi_eh_target_reset(struct Scsi_Host
*shost
,
1388 struct list_head
*work_q
,
1389 struct list_head
*done_q
)
1391 LIST_HEAD(tmp_list
);
1392 LIST_HEAD(check_list
);
1394 list_splice_init(work_q
, &tmp_list
);
1396 while (!list_empty(&tmp_list
)) {
1397 struct scsi_cmnd
*next
, *scmd
;
1400 unsigned long flags
;
1402 spin_lock_irqsave(shost
->host_lock
, flags
);
1403 if (scsi_host_eh_past_deadline(shost
)) {
1404 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1405 /* push back on work queue for further processing */
1406 list_splice_init(&check_list
, work_q
);
1407 list_splice_init(&tmp_list
, work_q
);
1408 SCSI_LOG_ERROR_RECOVERY(3,
1409 shost_printk(KERN_INFO
, shost
,
1410 "skip %s, past eh deadline\n",
1412 return list_empty(work_q
);
1414 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1416 scmd
= list_entry(tmp_list
.next
, struct scsi_cmnd
, eh_entry
);
1419 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending target reset "
1421 current
->comm
, id
));
1422 rtn
= scsi_try_target_reset(scmd
);
1423 if (rtn
!= SUCCESS
&& rtn
!= FAST_IO_FAIL
)
1424 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Target reset"
1427 current
->comm
, id
));
1428 list_for_each_entry_safe(scmd
, next
, &tmp_list
, eh_entry
) {
1429 if (scmd_id(scmd
) != id
)
1433 list_move_tail(&scmd
->eh_entry
, &check_list
);
1434 else if (rtn
== FAST_IO_FAIL
)
1435 scsi_eh_finish_cmd(scmd
, done_q
);
1437 /* push back on work queue for further processing */
1438 list_move(&scmd
->eh_entry
, work_q
);
1442 return scsi_eh_test_devices(&check_list
, work_q
, done_q
, 0);
1446 * scsi_eh_bus_reset - send a bus reset
1447 * @shost: &scsi host being recovered.
1448 * @work_q: &list_head for pending commands.
1449 * @done_q: &list_head for processed commands.
1451 static int scsi_eh_bus_reset(struct Scsi_Host
*shost
,
1452 struct list_head
*work_q
,
1453 struct list_head
*done_q
)
1455 struct scsi_cmnd
*scmd
, *chan_scmd
, *next
;
1456 LIST_HEAD(check_list
);
1457 unsigned int channel
;
1459 unsigned long flags
;
1462 * we really want to loop over the various channels, and do this on
1463 * a channel by channel basis. we should also check to see if any
1464 * of the failed commands are on soft_reset devices, and if so, skip
1468 for (channel
= 0; channel
<= shost
->max_channel
; channel
++) {
1469 spin_lock_irqsave(shost
->host_lock
, flags
);
1470 if (scsi_host_eh_past_deadline(shost
)) {
1471 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1472 list_splice_init(&check_list
, work_q
);
1473 SCSI_LOG_ERROR_RECOVERY(3,
1474 shost_printk(KERN_INFO
, shost
,
1475 "skip %s, past eh deadline\n",
1477 return list_empty(work_q
);
1479 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1482 list_for_each_entry(scmd
, work_q
, eh_entry
) {
1483 if (channel
== scmd_channel(scmd
)) {
1487 * FIXME add back in some support for
1488 * soft_reset devices.
1495 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BRST chan:"
1496 " %d\n", current
->comm
,
1498 rtn
= scsi_try_bus_reset(chan_scmd
);
1499 if (rtn
== SUCCESS
|| rtn
== FAST_IO_FAIL
) {
1500 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1501 if (channel
== scmd_channel(scmd
)) {
1502 if (rtn
== FAST_IO_FAIL
)
1503 scsi_eh_finish_cmd(scmd
,
1506 list_move_tail(&scmd
->eh_entry
,
1511 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BRST"
1512 " failed chan: %d\n",
1517 return scsi_eh_test_devices(&check_list
, work_q
, done_q
, 0);
1521 * scsi_eh_host_reset - send a host reset
1522 * @work_q: list_head for processed commands.
1523 * @done_q: list_head for processed commands.
1525 static int scsi_eh_host_reset(struct list_head
*work_q
,
1526 struct list_head
*done_q
)
1528 struct scsi_cmnd
*scmd
, *next
;
1529 LIST_HEAD(check_list
);
1532 if (!list_empty(work_q
)) {
1533 scmd
= list_entry(work_q
->next
,
1534 struct scsi_cmnd
, eh_entry
);
1536 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending HRST\n"
1539 rtn
= scsi_try_host_reset(scmd
);
1540 if (rtn
== SUCCESS
) {
1541 list_splice_init(work_q
, &check_list
);
1542 } else if (rtn
== FAST_IO_FAIL
) {
1543 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1544 scsi_eh_finish_cmd(scmd
, done_q
);
1547 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: HRST"
1552 return scsi_eh_test_devices(&check_list
, work_q
, done_q
, 1);
1556 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
1557 * @work_q: list_head for processed commands.
1558 * @done_q: list_head for processed commands.
1560 static void scsi_eh_offline_sdevs(struct list_head
*work_q
,
1561 struct list_head
*done_q
)
1563 struct scsi_cmnd
*scmd
, *next
;
1565 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1566 sdev_printk(KERN_INFO
, scmd
->device
, "Device offlined - "
1567 "not ready after error recovery\n");
1568 scsi_device_set_state(scmd
->device
, SDEV_OFFLINE
);
1569 if (scmd
->eh_eflags
& SCSI_EH_CANCEL_CMD
) {
1571 * FIXME: Handle lost cmds.
1574 scsi_eh_finish_cmd(scmd
, done_q
);
1580 * scsi_noretry_cmd - determinte if command should be failed fast
1581 * @scmd: SCSI cmd to examine.
1583 int scsi_noretry_cmd(struct scsi_cmnd
*scmd
)
1585 switch (host_byte(scmd
->result
)) {
1589 return (scmd
->request
->cmd_flags
& REQ_FAILFAST_TRANSPORT
);
1591 return (scmd
->request
->cmd_flags
& REQ_FAILFAST_DEV
);
1593 if (msg_byte(scmd
->result
) == COMMAND_COMPLETE
&&
1594 status_byte(scmd
->result
) == RESERVATION_CONFLICT
)
1597 case DID_SOFT_ERROR
:
1598 return (scmd
->request
->cmd_flags
& REQ_FAILFAST_DRIVER
);
1601 switch (status_byte(scmd
->result
)) {
1602 case CHECK_CONDITION
:
1604 * assume caller has checked sense and determinted
1605 * the check condition was retryable.
1607 if (scmd
->request
->cmd_flags
& REQ_FAILFAST_DEV
||
1608 scmd
->request
->cmd_type
== REQ_TYPE_BLOCK_PC
)
1616 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1617 * @scmd: SCSI cmd to examine.
1620 * This is *only* called when we are examining the status after sending
1621 * out the actual data command. any commands that are queued for error
1622 * recovery (e.g. test_unit_ready) do *not* come through here.
1624 * When this routine returns failed, it means the error handler thread
1625 * is woken. In cases where the error code indicates an error that
1626 * doesn't require the error handler read (i.e. we don't need to
1627 * abort/reset), this function should return SUCCESS.
1629 int scsi_decide_disposition(struct scsi_cmnd
*scmd
)
1634 * if the device is offline, then we clearly just pass the result back
1635 * up to the top level.
1637 if (!scsi_device_online(scmd
->device
)) {
1638 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: device offline - report"
1645 * first check the host byte, to see if there is anything in there
1646 * that would indicate what we need to do.
1648 switch (host_byte(scmd
->result
)) {
1649 case DID_PASSTHROUGH
:
1651 * no matter what, pass this through to the upper layer.
1652 * nuke this special code so that it looks like we are saying
1655 scmd
->result
&= 0xff00ffff;
1659 * looks good. drop through, and check the next byte.
1662 case DID_NO_CONNECT
:
1663 case DID_BAD_TARGET
:
1666 * note - this means that we just report the status back
1667 * to the top level driver, not that we actually think
1668 * that it indicates SUCCESS.
1672 * when the low level driver returns did_soft_error,
1673 * it is responsible for keeping an internal retry counter
1674 * in order to avoid endless loops (db)
1676 * actually this is a bug in this function here. we should
1677 * be mindful of the maximum number of retries specified
1678 * and not get stuck in a loop.
1680 case DID_SOFT_ERROR
:
1686 return ADD_TO_MLQUEUE
;
1687 case DID_TRANSPORT_DISRUPTED
:
1689 * LLD/transport was disrupted during processing of the IO.
1690 * The transport class is now blocked/blocking,
1691 * and the transport will decide what to do with the IO
1692 * based on its timers and recovery capablilities if
1693 * there are enough retries.
1696 case DID_TRANSPORT_FAILFAST
:
1698 * The transport decided to failfast the IO (most likely
1699 * the fast io fail tmo fired), so send IO directly upwards.
1703 if (msg_byte(scmd
->result
) == COMMAND_COMPLETE
&&
1704 status_byte(scmd
->result
) == RESERVATION_CONFLICT
)
1706 * execute reservation conflict processing code
1716 * when we scan the bus, we get timeout messages for
1717 * these commands if there is no device available.
1718 * other hosts report did_no_connect for the same thing.
1720 if ((scmd
->cmnd
[0] == TEST_UNIT_READY
||
1721 scmd
->cmnd
[0] == INQUIRY
)) {
1733 * next, check the message byte.
1735 if (msg_byte(scmd
->result
) != COMMAND_COMPLETE
)
1739 * check the status byte to see if this indicates anything special.
1741 switch (status_byte(scmd
->result
)) {
1743 scsi_handle_queue_full(scmd
->device
);
1745 * the case of trying to send too many commands to a
1746 * tagged queueing device.
1750 * device can't talk to us at the moment. Should only
1751 * occur (SAM-3) when the task queue is empty, so will cause
1752 * the empty queue handling to trigger a stall in the
1755 return ADD_TO_MLQUEUE
;
1757 if (scmd
->cmnd
[0] == REPORT_LUNS
)
1758 scmd
->device
->sdev_target
->expecting_lun_change
= 0;
1759 scsi_handle_queue_ramp_up(scmd
->device
);
1760 case COMMAND_TERMINATED
:
1764 case CHECK_CONDITION
:
1765 rtn
= scsi_check_sense(scmd
);
1766 if (rtn
== NEEDS_RETRY
)
1768 /* if rtn == FAILED, we have no sense information;
1769 * returning FAILED will wake the error handler thread
1770 * to collect the sense and redo the decide
1773 case CONDITION_GOOD
:
1774 case INTERMEDIATE_GOOD
:
1775 case INTERMEDIATE_C_GOOD
:
1778 * who knows? FIXME(eric)
1782 case RESERVATION_CONFLICT
:
1783 sdev_printk(KERN_INFO
, scmd
->device
,
1784 "reservation conflict\n");
1785 set_host_byte(scmd
, DID_NEXUS_FAILURE
);
1786 return SUCCESS
; /* causes immediate i/o error */
1794 /* we requeue for retry because the error was retryable, and
1795 * the request was not marked fast fail. Note that above,
1796 * even if the request is marked fast fail, we still requeue
1797 * for queue congestion conditions (QUEUE_FULL or BUSY) */
1798 if ((++scmd
->retries
) <= scmd
->allowed
1799 && !scsi_noretry_cmd(scmd
)) {
1803 * no more retries - report this one back to upper level.
1809 static void eh_lock_door_done(struct request
*req
, int uptodate
)
1811 __blk_put_request(req
->q
, req
);
1815 * scsi_eh_lock_door - Prevent medium removal for the specified device
1816 * @sdev: SCSI device to prevent medium removal
1819 * We must be called from process context.
1822 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1823 * head of the devices request queue, and continue.
1825 static void scsi_eh_lock_door(struct scsi_device
*sdev
)
1827 struct request
*req
;
1830 * blk_get_request with GFP_KERNEL (__GFP_WAIT) sleeps until a
1831 * request becomes available
1833 req
= blk_get_request(sdev
->request_queue
, READ
, GFP_KERNEL
);
1835 req
->cmd
[0] = ALLOW_MEDIUM_REMOVAL
;
1839 req
->cmd
[4] = SCSI_REMOVAL_PREVENT
;
1842 req
->cmd_len
= COMMAND_SIZE(req
->cmd
[0]);
1844 req
->cmd_type
= REQ_TYPE_BLOCK_PC
;
1845 req
->cmd_flags
|= REQ_QUIET
;
1846 req
->timeout
= 10 * HZ
;
1849 blk_execute_rq_nowait(req
->q
, NULL
, req
, 1, eh_lock_door_done
);
1853 * scsi_restart_operations - restart io operations to the specified host.
1854 * @shost: Host we are restarting.
1857 * When we entered the error handler, we blocked all further i/o to
1858 * this device. we need to 'reverse' this process.
1860 static void scsi_restart_operations(struct Scsi_Host
*shost
)
1862 struct scsi_device
*sdev
;
1863 unsigned long flags
;
1866 * If the door was locked, we need to insert a door lock request
1867 * onto the head of the SCSI request queue for the device. There
1868 * is no point trying to lock the door of an off-line device.
1870 shost_for_each_device(sdev
, shost
) {
1871 if (scsi_device_online(sdev
) && sdev
->locked
)
1872 scsi_eh_lock_door(sdev
);
1876 * next free up anything directly waiting upon the host. this
1877 * will be requests for character device operations, and also for
1878 * ioctls to queued block devices.
1880 SCSI_LOG_ERROR_RECOVERY(3,
1881 printk("scsi_eh_%d waking up host to restart\n",
1884 spin_lock_irqsave(shost
->host_lock
, flags
);
1885 if (scsi_host_set_state(shost
, SHOST_RUNNING
))
1886 if (scsi_host_set_state(shost
, SHOST_CANCEL
))
1887 BUG_ON(scsi_host_set_state(shost
, SHOST_DEL
));
1888 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1890 wake_up(&shost
->host_wait
);
1893 * finally we need to re-initiate requests that may be pending. we will
1894 * have had everything blocked while error handling is taking place, and
1895 * now that error recovery is done, we will need to ensure that these
1896 * requests are started.
1898 scsi_run_host_queues(shost
);
1901 * if eh is active and host_eh_scheduled is pending we need to re-run
1902 * recovery. we do this check after scsi_run_host_queues() to allow
1903 * everything pent up since the last eh run a chance to make forward
1904 * progress before we sync again. Either we'll immediately re-run
1905 * recovery or scsi_device_unbusy() will wake us again when these
1906 * pending commands complete.
1908 spin_lock_irqsave(shost
->host_lock
, flags
);
1909 if (shost
->host_eh_scheduled
)
1910 if (scsi_host_set_state(shost
, SHOST_RECOVERY
))
1911 WARN_ON(scsi_host_set_state(shost
, SHOST_CANCEL_RECOVERY
));
1912 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1916 * scsi_eh_ready_devs - check device ready state and recover if not.
1917 * @shost: host to be recovered.
1918 * @work_q: &list_head for pending commands.
1919 * @done_q: &list_head for processed commands.
1921 void scsi_eh_ready_devs(struct Scsi_Host
*shost
,
1922 struct list_head
*work_q
,
1923 struct list_head
*done_q
)
1925 if (!scsi_eh_stu(shost
, work_q
, done_q
))
1926 if (!scsi_eh_bus_device_reset(shost
, work_q
, done_q
))
1927 if (!scsi_eh_target_reset(shost
, work_q
, done_q
))
1928 if (!scsi_eh_bus_reset(shost
, work_q
, done_q
))
1929 if (!scsi_eh_host_reset(work_q
, done_q
))
1930 scsi_eh_offline_sdevs(work_q
,
1933 EXPORT_SYMBOL_GPL(scsi_eh_ready_devs
);
1936 * scsi_eh_flush_done_q - finish processed commands or retry them.
1937 * @done_q: list_head of processed commands.
1939 void scsi_eh_flush_done_q(struct list_head
*done_q
)
1941 struct scsi_cmnd
*scmd
, *next
;
1943 list_for_each_entry_safe(scmd
, next
, done_q
, eh_entry
) {
1944 list_del_init(&scmd
->eh_entry
);
1945 if (scsi_device_online(scmd
->device
) &&
1946 !scsi_noretry_cmd(scmd
) &&
1947 (++scmd
->retries
<= scmd
->allowed
)) {
1948 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush"
1952 scsi_queue_insert(scmd
, SCSI_MLQUEUE_EH_RETRY
);
1955 * If just we got sense for the device (called
1956 * scsi_eh_get_sense), scmd->result is already
1957 * set, do not set DRIVER_TIMEOUT.
1960 scmd
->result
|= (DRIVER_TIMEOUT
<< 24);
1961 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush finish"
1963 current
->comm
, scmd
));
1964 scsi_finish_command(scmd
);
1968 EXPORT_SYMBOL(scsi_eh_flush_done_q
);
1971 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
1972 * @shost: Host to unjam.
1975 * When we come in here, we *know* that all commands on the bus have
1976 * either completed, failed or timed out. we also know that no further
1977 * commands are being sent to the host, so things are relatively quiet
1978 * and we have freedom to fiddle with things as we wish.
1980 * This is only the *default* implementation. it is possible for
1981 * individual drivers to supply their own version of this function, and
1982 * if the maintainer wishes to do this, it is strongly suggested that
1983 * this function be taken as a template and modified. this function
1984 * was designed to correctly handle problems for about 95% of the
1985 * different cases out there, and it should always provide at least a
1986 * reasonable amount of error recovery.
1988 * Any command marked 'failed' or 'timeout' must eventually have
1989 * scsi_finish_cmd() called for it. we do all of the retry stuff
1990 * here, so when we restart the host after we return it should have an
1993 static void scsi_unjam_host(struct Scsi_Host
*shost
)
1995 unsigned long flags
;
1996 LIST_HEAD(eh_work_q
);
1997 LIST_HEAD(eh_done_q
);
1999 spin_lock_irqsave(shost
->host_lock
, flags
);
2000 list_splice_init(&shost
->eh_cmd_q
, &eh_work_q
);
2001 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2003 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost
, &eh_work_q
));
2005 if (!scsi_eh_get_sense(&eh_work_q
, &eh_done_q
))
2006 if (!scsi_eh_abort_cmds(&eh_work_q
, &eh_done_q
))
2007 scsi_eh_ready_devs(shost
, &eh_work_q
, &eh_done_q
);
2009 spin_lock_irqsave(shost
->host_lock
, flags
);
2010 if (shost
->eh_deadline
)
2011 shost
->last_reset
= 0;
2012 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2013 scsi_eh_flush_done_q(&eh_done_q
);
2017 * scsi_error_handler - SCSI error handler thread
2018 * @data: Host for which we are running.
2021 * This is the main error handling loop. This is run as a kernel thread
2022 * for every SCSI host and handles all error handling activity.
2024 int scsi_error_handler(void *data
)
2026 struct Scsi_Host
*shost
= data
;
2029 * We use TASK_INTERRUPTIBLE so that the thread is not
2030 * counted against the load average as a running process.
2031 * We never actually get interrupted because kthread_run
2032 * disables signal delivery for the created thread.
2034 while (!kthread_should_stop()) {
2035 set_current_state(TASK_INTERRUPTIBLE
);
2036 if ((shost
->host_failed
== 0 && shost
->host_eh_scheduled
== 0) ||
2037 shost
->host_failed
!= shost
->host_busy
) {
2038 SCSI_LOG_ERROR_RECOVERY(1,
2039 printk("scsi_eh_%d: sleeping\n",
2045 __set_current_state(TASK_RUNNING
);
2046 SCSI_LOG_ERROR_RECOVERY(1,
2047 printk("scsi_eh_%d: waking up %d/%d/%d\n",
2048 shost
->host_no
, shost
->host_eh_scheduled
,
2049 shost
->host_failed
, shost
->host_busy
));
2052 * We have a host that is failing for some reason. Figure out
2053 * what we need to do to get it up and online again (if we can).
2054 * If we fail, we end up taking the thing offline.
2056 if (!shost
->eh_noresume
&& scsi_autopm_get_host(shost
) != 0) {
2057 SCSI_LOG_ERROR_RECOVERY(1,
2058 printk(KERN_ERR
"Error handler scsi_eh_%d "
2059 "unable to autoresume\n",
2064 if (shost
->transportt
->eh_strategy_handler
)
2065 shost
->transportt
->eh_strategy_handler(shost
);
2067 scsi_unjam_host(shost
);
2070 * Note - if the above fails completely, the action is to take
2071 * individual devices offline and flush the queue of any
2072 * outstanding requests that may have been pending. When we
2073 * restart, we restart any I/O to any other devices on the bus
2074 * which are still online.
2076 scsi_restart_operations(shost
);
2077 if (!shost
->eh_noresume
)
2078 scsi_autopm_put_host(shost
);
2080 __set_current_state(TASK_RUNNING
);
2082 SCSI_LOG_ERROR_RECOVERY(1,
2083 printk("Error handler scsi_eh_%d exiting\n", shost
->host_no
));
2084 shost
->ehandler
= NULL
;
2089 * Function: scsi_report_bus_reset()
2091 * Purpose: Utility function used by low-level drivers to report that
2092 * they have observed a bus reset on the bus being handled.
2094 * Arguments: shost - Host in question
2095 * channel - channel on which reset was observed.
2099 * Lock status: Host lock must be held.
2101 * Notes: This only needs to be called if the reset is one which
2102 * originates from an unknown location. Resets originated
2103 * by the mid-level itself don't need to call this, but there
2104 * should be no harm.
2106 * The main purpose of this is to make sure that a CHECK_CONDITION
2107 * is properly treated.
2109 void scsi_report_bus_reset(struct Scsi_Host
*shost
, int channel
)
2111 struct scsi_device
*sdev
;
2113 __shost_for_each_device(sdev
, shost
) {
2114 if (channel
== sdev_channel(sdev
))
2115 __scsi_report_device_reset(sdev
, NULL
);
2118 EXPORT_SYMBOL(scsi_report_bus_reset
);
2121 * Function: scsi_report_device_reset()
2123 * Purpose: Utility function used by low-level drivers to report that
2124 * they have observed a device reset on the device being handled.
2126 * Arguments: shost - Host in question
2127 * channel - channel on which reset was observed
2128 * target - target on which reset was observed
2132 * Lock status: Host lock must be held
2134 * Notes: This only needs to be called if the reset is one which
2135 * originates from an unknown location. Resets originated
2136 * by the mid-level itself don't need to call this, but there
2137 * should be no harm.
2139 * The main purpose of this is to make sure that a CHECK_CONDITION
2140 * is properly treated.
2142 void scsi_report_device_reset(struct Scsi_Host
*shost
, int channel
, int target
)
2144 struct scsi_device
*sdev
;
2146 __shost_for_each_device(sdev
, shost
) {
2147 if (channel
== sdev_channel(sdev
) &&
2148 target
== sdev_id(sdev
))
2149 __scsi_report_device_reset(sdev
, NULL
);
2152 EXPORT_SYMBOL(scsi_report_device_reset
);
2155 scsi_reset_provider_done_command(struct scsi_cmnd
*scmd
)
2160 * Function: scsi_reset_provider
2162 * Purpose: Send requested reset to a bus or device at any phase.
2164 * Arguments: device - device to send reset to
2165 * flag - reset type (see scsi.h)
2167 * Returns: SUCCESS/FAILURE.
2169 * Notes: This is used by the SCSI Generic driver to provide
2170 * Bus/Device reset capability.
2173 scsi_reset_provider(struct scsi_device
*dev
, int flag
)
2175 struct scsi_cmnd
*scmd
;
2176 struct Scsi_Host
*shost
= dev
->host
;
2178 unsigned long flags
;
2181 if (scsi_autopm_get_host(shost
) < 0)
2184 scmd
= scsi_get_command(dev
, GFP_KERNEL
);
2185 blk_rq_init(NULL
, &req
);
2186 scmd
->request
= &req
;
2188 scmd
->cmnd
= req
.cmd
;
2190 scmd
->scsi_done
= scsi_reset_provider_done_command
;
2191 memset(&scmd
->sdb
, 0, sizeof(scmd
->sdb
));
2195 scmd
->sc_data_direction
= DMA_BIDIRECTIONAL
;
2197 spin_lock_irqsave(shost
->host_lock
, flags
);
2198 shost
->tmf_in_progress
= 1;
2199 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2202 case SCSI_TRY_RESET_DEVICE
:
2203 rtn
= scsi_try_bus_device_reset(scmd
);
2207 case SCSI_TRY_RESET_TARGET
:
2208 rtn
= scsi_try_target_reset(scmd
);
2212 case SCSI_TRY_RESET_BUS
:
2213 rtn
= scsi_try_bus_reset(scmd
);
2217 case SCSI_TRY_RESET_HOST
:
2218 rtn
= scsi_try_host_reset(scmd
);
2224 spin_lock_irqsave(shost
->host_lock
, flags
);
2225 shost
->tmf_in_progress
= 0;
2226 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2229 * be sure to wake up anyone who was sleeping or had their queue
2230 * suspended while we performed the TMF.
2232 SCSI_LOG_ERROR_RECOVERY(3,
2233 printk("%s: waking up host to restart after TMF\n",
2236 wake_up(&shost
->host_wait
);
2238 scsi_run_host_queues(shost
);
2240 scsi_next_command(scmd
);
2241 scsi_autopm_put_host(shost
);
2244 EXPORT_SYMBOL(scsi_reset_provider
);
2247 * scsi_normalize_sense - normalize main elements from either fixed or
2248 * descriptor sense data format into a common format.
2250 * @sense_buffer: byte array containing sense data returned by device
2251 * @sb_len: number of valid bytes in sense_buffer
2252 * @sshdr: pointer to instance of structure that common
2253 * elements are written to.
2256 * The "main elements" from sense data are: response_code, sense_key,
2257 * asc, ascq and additional_length (only for descriptor format).
2259 * Typically this function can be called after a device has
2260 * responded to a SCSI command with the CHECK_CONDITION status.
2263 * 1 if valid sense data information found, else 0;
2265 int scsi_normalize_sense(const u8
*sense_buffer
, int sb_len
,
2266 struct scsi_sense_hdr
*sshdr
)
2268 if (!sense_buffer
|| !sb_len
)
2271 memset(sshdr
, 0, sizeof(struct scsi_sense_hdr
));
2273 sshdr
->response_code
= (sense_buffer
[0] & 0x7f);
2275 if (!scsi_sense_valid(sshdr
))
2278 if (sshdr
->response_code
>= 0x72) {
2283 sshdr
->sense_key
= (sense_buffer
[1] & 0xf);
2285 sshdr
->asc
= sense_buffer
[2];
2287 sshdr
->ascq
= sense_buffer
[3];
2289 sshdr
->additional_length
= sense_buffer
[7];
2295 sshdr
->sense_key
= (sense_buffer
[2] & 0xf);
2297 sb_len
= (sb_len
< (sense_buffer
[7] + 8)) ?
2298 sb_len
: (sense_buffer
[7] + 8);
2300 sshdr
->asc
= sense_buffer
[12];
2302 sshdr
->ascq
= sense_buffer
[13];
2308 EXPORT_SYMBOL(scsi_normalize_sense
);
2310 int scsi_command_normalize_sense(struct scsi_cmnd
*cmd
,
2311 struct scsi_sense_hdr
*sshdr
)
2313 return scsi_normalize_sense(cmd
->sense_buffer
,
2314 SCSI_SENSE_BUFFERSIZE
, sshdr
);
2316 EXPORT_SYMBOL(scsi_command_normalize_sense
);
2319 * scsi_sense_desc_find - search for a given descriptor type in descriptor sense data format.
2320 * @sense_buffer: byte array of descriptor format sense data
2321 * @sb_len: number of valid bytes in sense_buffer
2322 * @desc_type: value of descriptor type to find
2323 * (e.g. 0 -> information)
2326 * only valid when sense data is in descriptor format
2329 * pointer to start of (first) descriptor if found else NULL
2331 const u8
* scsi_sense_desc_find(const u8
* sense_buffer
, int sb_len
,
2334 int add_sen_len
, add_len
, desc_len
, k
;
2337 if ((sb_len
< 8) || (0 == (add_sen_len
= sense_buffer
[7])))
2339 if ((sense_buffer
[0] < 0x72) || (sense_buffer
[0] > 0x73))
2341 add_sen_len
= (add_sen_len
< (sb_len
- 8)) ?
2342 add_sen_len
: (sb_len
- 8);
2343 descp
= &sense_buffer
[8];
2344 for (desc_len
= 0, k
= 0; k
< add_sen_len
; k
+= desc_len
) {
2346 add_len
= (k
< (add_sen_len
- 1)) ? descp
[1]: -1;
2347 desc_len
= add_len
+ 2;
2348 if (descp
[0] == desc_type
)
2350 if (add_len
< 0) // short descriptor ??
2355 EXPORT_SYMBOL(scsi_sense_desc_find
);
2358 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
2359 * @sense_buffer: byte array of sense data
2360 * @sb_len: number of valid bytes in sense_buffer
2361 * @info_out: pointer to 64 integer where 8 or 4 byte information
2362 * field will be placed if found.
2365 * 1 if information field found, 0 if not found.
2367 int scsi_get_sense_info_fld(const u8
* sense_buffer
, int sb_len
,
2376 switch (sense_buffer
[0] & 0x7f) {
2379 if (sense_buffer
[0] & 0x80) {
2380 *info_out
= (sense_buffer
[3] << 24) +
2381 (sense_buffer
[4] << 16) +
2382 (sense_buffer
[5] << 8) + sense_buffer
[6];
2388 ucp
= scsi_sense_desc_find(sense_buffer
, sb_len
,
2390 if (ucp
&& (0xa == ucp
[1])) {
2392 for (j
= 0; j
< 8; ++j
) {
2405 EXPORT_SYMBOL(scsi_get_sense_info_fld
);
2408 * scsi_build_sense_buffer - build sense data in a buffer
2409 * @desc: Sense format (non zero == descriptor format,
2410 * 0 == fixed format)
2411 * @buf: Where to build sense data
2413 * @asc: Additional sense code
2414 * @ascq: Additional sense code qualifier
2417 void scsi_build_sense_buffer(int desc
, u8
*buf
, u8 key
, u8 asc
, u8 ascq
)
2420 buf
[0] = 0x72; /* descriptor, current */
2426 buf
[0] = 0x70; /* fixed, current */
2433 EXPORT_SYMBOL(scsi_build_sense_buffer
);