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 == -1)
98 * 32bit accesses are guaranteed to be atomic
99 * (on all supported architectures), so instead
100 * of using a spinlock we can as well double check
101 * if eh_deadline has been set to 'off' during the
104 if (time_before(jiffies, shost->last_reset + shost->eh_deadline) &&
105 shost->eh_deadline > -1)
112 * scmd_eh_abort_handler - Handle command aborts
113 * @work: command to be aborted.
116 scmd_eh_abort_handler(struct work_struct *work)
118 struct scsi_cmnd *scmd =
119 container_of(work, struct scsi_cmnd, abort_work.work);
120 struct scsi_device *sdev = scmd->device;
123 if (scsi_host_eh_past_deadline(sdev->host)) {
124 SCSI_LOG_ERROR_RECOVERY(3,
125 scmd_printk(KERN_INFO, scmd,
126 "scmd %p eh timeout, not aborting\n",
129 SCSI_LOG_ERROR_RECOVERY(3,
130 scmd_printk(KERN_INFO, scmd,
131 "aborting command %p\n", scmd));
132 rtn = scsi_try_to_abort_cmd(sdev->host->hostt, scmd);
133 if (rtn == SUCCESS) {
134 scmd->result |= DID_TIME_OUT << 16;
135 if (scsi_host_eh_past_deadline(sdev->host)) {
136 SCSI_LOG_ERROR_RECOVERY(3,
137 scmd_printk(KERN_INFO, scmd,
138 "scmd %p eh timeout, "
139 "not retrying aborted "
141 } else if (!scsi_noretry_cmd(scmd) &&
142 (++scmd->retries <= scmd->allowed)) {
143 SCSI_LOG_ERROR_RECOVERY(3,
144 scmd_printk(KERN_WARNING, scmd,
146 "aborted command\n", scmd));
147 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
150 SCSI_LOG_ERROR_RECOVERY(3,
151 scmd_printk(KERN_WARNING, scmd,
153 "aborted command\n", scmd));
154 scsi_finish_command(scmd);
158 SCSI_LOG_ERROR_RECOVERY(3,
159 scmd_printk(KERN_INFO, scmd,
160 "scmd %p abort failed, rtn %d\n",
165 if (!scsi_eh_scmd_add(scmd, 0)) {
166 SCSI_LOG_ERROR_RECOVERY(3,
167 scmd_printk(KERN_WARNING, scmd,
169 "aborted command\n", scmd));
170 scmd->result |= DID_TIME_OUT << 16;
171 scsi_finish_command(scmd);
176 * scsi_abort_command - schedule a command abort
177 * @scmd: scmd to abort.
179 * We only need to abort commands after a command timeout
182 scsi_abort_command(struct scsi_cmnd *scmd)
184 struct scsi_device *sdev = scmd->device;
185 struct Scsi_Host *shost = sdev->host;
188 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
190 * Retry after abort failed, escalate to next level.
192 SCSI_LOG_ERROR_RECOVERY(3,
193 scmd_printk(KERN_INFO, scmd,
194 "scmd %p previous abort failed\n", scmd));
195 cancel_delayed_work(&scmd->abort_work);
200 * Do not try a command abort if
201 * SCSI EH has already started.
203 spin_lock_irqsave(shost->host_lock, flags);
204 if (scsi_host_in_recovery(shost)) {
205 spin_unlock_irqrestore(shost->host_lock, flags);
206 SCSI_LOG_ERROR_RECOVERY(3,
207 scmd_printk(KERN_INFO, scmd,
208 "scmd %p not aborting, host in recovery\n",
213 if (shost->eh_deadline != -1 && !shost->last_reset)
214 shost->last_reset = jiffies;
215 spin_unlock_irqrestore(shost->host_lock, flags);
217 scmd->eh_eflags |= SCSI_EH_ABORT_SCHEDULED;
218 SCSI_LOG_ERROR_RECOVERY(3,
219 scmd_printk(KERN_INFO, scmd,
220 "scmd %p abort scheduled\n", scmd));
221 queue_delayed_work(shost->tmf_work_q, &scmd->abort_work, HZ / 100);
226 * scsi_eh_scmd_add - add scsi cmd to error handling.
227 * @scmd: scmd to run eh on.
228 * @eh_flag: optional SCSI_EH flag.
233 int scsi_eh_scmd_add(struct scsi_cmnd *scmd, int eh_flag)
235 struct Scsi_Host *shost = scmd->device->host;
239 if (!shost->ehandler)
242 spin_lock_irqsave(shost->host_lock, flags);
243 if (scsi_host_set_state(shost, SHOST_RECOVERY))
244 if (scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY))
247 if (shost->eh_deadline != -1 && !shost->last_reset)
248 shost->last_reset = jiffies;
251 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED)
252 eh_flag &= ~SCSI_EH_CANCEL_CMD;
253 scmd->eh_eflags |= eh_flag;
254 list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
255 shost->host_failed++;
256 scsi_eh_wakeup(shost);
258 spin_unlock_irqrestore(shost->host_lock, flags);
263 * scsi_times_out - Timeout function for normal scsi commands.
264 * @req: request that is timing out.
267 * We do not need to lock this. There is the potential for a race
268 * only in that the normal completion handling might run, but if the
269 * normal completion function determines that the timer has already
270 * fired, then it mustn't do anything.
272 enum blk_eh_timer_return scsi_times_out(struct request *req)
274 struct scsi_cmnd *scmd = req->special;
275 enum blk_eh_timer_return rtn = BLK_EH_NOT_HANDLED;
276 struct Scsi_Host *host = scmd->device->host;
278 trace_scsi_dispatch_cmd_timeout(scmd);
279 scsi_log_completion(scmd, TIMEOUT_ERROR);
281 if (host->eh_deadline != -1 && !host->last_reset)
282 host->last_reset = jiffies;
284 if (host->transportt->eh_timed_out)
285 rtn = host->transportt->eh_timed_out(scmd);
286 else if (host->hostt->eh_timed_out)
287 rtn = host->hostt->eh_timed_out(scmd);
289 if (rtn == BLK_EH_NOT_HANDLED && !host->hostt->no_async_abort)
290 if (scsi_abort_command(scmd) == SUCCESS)
291 return BLK_EH_NOT_HANDLED;
293 scmd->result |= DID_TIME_OUT << 16;
295 if (unlikely(rtn == BLK_EH_NOT_HANDLED &&
296 !scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD)))
297 rtn = BLK_EH_HANDLED;
303 * scsi_block_when_processing_errors - Prevent cmds from being queued.
304 * @sdev: Device on which we are performing recovery.
307 * We block until the host is out of error recovery, and then check to
308 * see whether the host or the device is offline.
311 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
313 int scsi_block_when_processing_errors(struct scsi_device *sdev)
317 wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
319 online = scsi_device_online(sdev);
321 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: rtn: %d\n", __func__,
326 EXPORT_SYMBOL(scsi_block_when_processing_errors);
328 #ifdef CONFIG_SCSI_LOGGING
330 * scsi_eh_prt_fail_stats - Log info on failures.
331 * @shost: scsi host being recovered.
332 * @work_q: Queue of scsi cmds to process.
334 static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
335 struct list_head *work_q)
337 struct scsi_cmnd *scmd;
338 struct scsi_device *sdev;
339 int total_failures = 0;
342 int devices_failed = 0;
344 shost_for_each_device(sdev, shost) {
345 list_for_each_entry(scmd, work_q, eh_entry) {
346 if (scmd->device == sdev) {
348 if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD)
355 if (cmd_cancel || cmd_failed) {
356 SCSI_LOG_ERROR_RECOVERY(3,
357 sdev_printk(KERN_INFO, sdev,
358 "%s: cmds failed: %d, cancel: %d\n",
359 __func__, cmd_failed,
367 SCSI_LOG_ERROR_RECOVERY(2, printk("Total of %d commands on %d"
368 " devices require eh work\n",
369 total_failures, devices_failed));
374 * scsi_report_lun_change - Set flag on all *other* devices on the same target
375 * to indicate that a UNIT ATTENTION is expected.
376 * @sdev: Device reporting the UNIT ATTENTION
378 static void scsi_report_lun_change(struct scsi_device *sdev)
380 sdev->sdev_target->expecting_lun_change = 1;
384 * scsi_report_sense - Examine scsi sense information and log messages for
385 * certain conditions, also issue uevents for some of them.
386 * @sdev: Device reporting the sense code
387 * @sshdr: sshdr to be examined
389 static void scsi_report_sense(struct scsi_device *sdev,
390 struct scsi_sense_hdr *sshdr)
392 enum scsi_device_event evt_type = SDEV_EVT_MAXBITS; /* i.e. none */
394 if (sshdr->sense_key == UNIT_ATTENTION) {
395 if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) {
396 evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED;
397 sdev_printk(KERN_WARNING, sdev,
398 "Inquiry data has changed");
399 } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) {
400 evt_type = SDEV_EVT_LUN_CHANGE_REPORTED;
401 scsi_report_lun_change(sdev);
402 sdev_printk(KERN_WARNING, sdev,
403 "Warning! Received an indication that the "
404 "LUN assignments on this target have "
405 "changed. The Linux SCSI layer does not "
406 "automatically remap LUN assignments.\n");
407 } else if (sshdr->asc == 0x3f)
408 sdev_printk(KERN_WARNING, sdev,
409 "Warning! Received an indication that the "
410 "operating parameters on this target have "
411 "changed. The Linux SCSI layer does not "
412 "automatically adjust these parameters.\n");
414 if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) {
415 evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED;
416 sdev_printk(KERN_WARNING, sdev,
417 "Warning! Received an indication that the "
418 "LUN reached a thin provisioning soft "
422 if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) {
423 evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED;
424 sdev_printk(KERN_WARNING, sdev,
425 "Mode parameters changed");
426 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) {
427 evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED;
428 sdev_printk(KERN_WARNING, sdev,
429 "Capacity data has changed");
430 } else if (sshdr->asc == 0x2a)
431 sdev_printk(KERN_WARNING, sdev,
432 "Parameters changed");
435 if (evt_type != SDEV_EVT_MAXBITS) {
436 set_bit(evt_type, sdev->pending_events);
437 schedule_work(&sdev->event_work);
442 * scsi_check_sense - Examine scsi cmd sense
443 * @scmd: Cmd to have sense checked.
446 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
449 * When a deferred error is detected the current command has
450 * not been executed and needs retrying.
452 static int scsi_check_sense(struct scsi_cmnd *scmd)
454 struct scsi_device *sdev = scmd->device;
455 struct scsi_sense_hdr sshdr;
457 if (! scsi_command_normalize_sense(scmd, &sshdr))
458 return FAILED; /* no valid sense data */
460 if (scmd->cmnd[0] == TEST_UNIT_READY && scmd->scsi_done != scsi_eh_done)
462 * nasty: for mid-layer issued TURs, we need to return the
463 * actual sense data without any recovery attempt. For eh
464 * issued ones, we need to try to recover and interpret
468 scsi_report_sense(sdev, &sshdr);
470 if (scsi_sense_is_deferred(&sshdr))
473 if (sdev->scsi_dh_data && sdev->scsi_dh_data->scsi_dh &&
474 sdev->scsi_dh_data->scsi_dh->check_sense) {
477 rc = sdev->scsi_dh_data->scsi_dh->check_sense(sdev, &sshdr);
478 if (rc != SCSI_RETURN_NOT_HANDLED)
480 /* handler does not care. Drop down to default handling */
484 * Previous logic looked for FILEMARK, EOM or ILI which are
485 * mainly associated with tapes and returned SUCCESS.
487 if (sshdr.response_code == 0x70) {
489 if (scmd->sense_buffer[2] & 0xe0)
493 * descriptor format: look for "stream commands sense data
494 * descriptor" (see SSC-3). Assume single sense data
495 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
497 if ((sshdr.additional_length > 3) &&
498 (scmd->sense_buffer[8] == 0x4) &&
499 (scmd->sense_buffer[11] & 0xe0))
503 switch (sshdr.sense_key) {
506 case RECOVERED_ERROR:
507 return /* soft_error */ SUCCESS;
509 case ABORTED_COMMAND:
510 if (sshdr.asc == 0x10) /* DIF */
517 * if we are expecting a cc/ua because of a bus reset that we
518 * performed, treat this just as a retry. otherwise this is
519 * information that we should pass up to the upper-level driver
520 * so that we can deal with it there.
522 if (scmd->device->expecting_cc_ua) {
524 * Because some device does not queue unit
525 * attentions correctly, we carefully check
526 * additional sense code and qualifier so as
527 * not to squash media change unit attention.
529 if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) {
530 scmd->device->expecting_cc_ua = 0;
535 * we might also expect a cc/ua if another LUN on the target
536 * reported a UA with an ASC/ASCQ of 3F 0E -
537 * REPORTED LUNS DATA HAS CHANGED.
539 if (scmd->device->sdev_target->expecting_lun_change &&
540 sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
543 * if the device is in the process of becoming ready, we
546 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
549 * if the device is not started, we need to wake
550 * the error handler to start the motor
552 if (scmd->device->allow_restart &&
553 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
556 * Pass the UA upwards for a determination in the completion
561 /* these are not supported */
563 if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
564 /* Thin provisioning hard threshold reached */
565 set_host_byte(scmd, DID_ALLOC_FAILURE);
569 case VOLUME_OVERFLOW:
572 set_host_byte(scmd, DID_TARGET_FAILURE);
576 if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
577 sshdr.asc == 0x13 || /* AMNF DATA FIELD */
578 sshdr.asc == 0x14) { /* RECORD NOT FOUND */
579 set_host_byte(scmd, DID_MEDIUM_ERROR);
585 if (scmd->device->retry_hwerror)
586 return ADD_TO_MLQUEUE;
588 set_host_byte(scmd, DID_TARGET_FAILURE);
590 case ILLEGAL_REQUEST:
591 if (sshdr.asc == 0x20 || /* Invalid command operation code */
592 sshdr.asc == 0x21 || /* Logical block address out of range */
593 sshdr.asc == 0x24 || /* Invalid field in cdb */
594 sshdr.asc == 0x26) { /* Parameter value invalid */
595 set_host_byte(scmd, DID_TARGET_FAILURE);
604 static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
606 struct scsi_host_template *sht = sdev->host->hostt;
607 struct scsi_device *tmp_sdev;
609 if (!sht->change_queue_depth ||
610 sdev->queue_depth >= sdev->max_queue_depth)
613 if (time_before(jiffies,
614 sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
617 if (time_before(jiffies,
618 sdev->last_queue_full_time + sdev->queue_ramp_up_period))
622 * Walk all devices of a target and do
625 shost_for_each_device(tmp_sdev, sdev->host) {
626 if (tmp_sdev->channel != sdev->channel ||
627 tmp_sdev->id != sdev->id ||
628 tmp_sdev->queue_depth == sdev->max_queue_depth)
631 * call back into LLD to increase queue_depth by one
632 * with ramp up reason code.
634 sht->change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1,
635 SCSI_QDEPTH_RAMP_UP);
636 sdev->last_queue_ramp_up = jiffies;
640 static void scsi_handle_queue_full(struct scsi_device *sdev)
642 struct scsi_host_template *sht = sdev->host->hostt;
643 struct scsi_device *tmp_sdev;
645 if (!sht->change_queue_depth)
648 shost_for_each_device(tmp_sdev, sdev->host) {
649 if (tmp_sdev->channel != sdev->channel ||
650 tmp_sdev->id != sdev->id)
653 * We do not know the number of commands that were at
654 * the device when we got the queue full so we start
655 * from the highest possible value and work our way down.
657 sht->change_queue_depth(tmp_sdev, tmp_sdev->queue_depth - 1,
663 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
664 * @scmd: SCSI cmd to examine.
667 * This is *only* called when we are examining the status of commands
668 * queued during error recovery. the main difference here is that we
669 * don't allow for the possibility of retries here, and we are a lot
670 * more restrictive about what we consider acceptable.
672 static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
675 * first check the host byte, to see if there is anything in there
676 * that would indicate what we need to do.
678 if (host_byte(scmd->result) == DID_RESET) {
680 * rats. we are already in the error handler, so we now
681 * get to try and figure out what to do next. if the sense
682 * is valid, we have a pretty good idea of what to do.
683 * if not, we mark it as FAILED.
685 return scsi_check_sense(scmd);
687 if (host_byte(scmd->result) != DID_OK)
691 * next, check the message byte.
693 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
697 * now, check the status byte to see if this indicates
700 switch (status_byte(scmd->result)) {
702 scsi_handle_queue_ramp_up(scmd->device);
703 case COMMAND_TERMINATED:
705 case CHECK_CONDITION:
706 return scsi_check_sense(scmd);
708 case INTERMEDIATE_GOOD:
709 case INTERMEDIATE_C_GOOD:
711 * who knows? FIXME(eric)
714 case RESERVATION_CONFLICT:
715 if (scmd->cmnd[0] == TEST_UNIT_READY)
716 /* it is a success, we probed the device and
719 /* otherwise, we failed to send the command */
722 scsi_handle_queue_full(scmd->device);
733 * scsi_eh_done - Completion function for error handling.
734 * @scmd: Cmd that is done.
736 static void scsi_eh_done(struct scsi_cmnd *scmd)
738 struct completion *eh_action;
740 SCSI_LOG_ERROR_RECOVERY(3,
741 printk("%s scmd: %p result: %x\n",
742 __func__, scmd, scmd->result));
744 eh_action = scmd->device->host->eh_action;
750 * scsi_try_host_reset - ask host adapter to reset itself
751 * @scmd: SCSI cmd to send host reset.
753 static int scsi_try_host_reset(struct scsi_cmnd *scmd)
757 struct Scsi_Host *host = scmd->device->host;
758 struct scsi_host_template *hostt = host->hostt;
760 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Host RST\n",
763 if (!hostt->eh_host_reset_handler)
766 rtn = hostt->eh_host_reset_handler(scmd);
768 if (rtn == SUCCESS) {
769 if (!hostt->skip_settle_delay)
770 ssleep(HOST_RESET_SETTLE_TIME);
771 spin_lock_irqsave(host->host_lock, flags);
772 scsi_report_bus_reset(host, scmd_channel(scmd));
773 spin_unlock_irqrestore(host->host_lock, flags);
780 * scsi_try_bus_reset - ask host to perform a bus reset
781 * @scmd: SCSI cmd to send bus reset.
783 static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
787 struct Scsi_Host *host = scmd->device->host;
788 struct scsi_host_template *hostt = host->hostt;
790 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Bus RST\n",
793 if (!hostt->eh_bus_reset_handler)
796 rtn = hostt->eh_bus_reset_handler(scmd);
798 if (rtn == SUCCESS) {
799 if (!hostt->skip_settle_delay)
800 ssleep(BUS_RESET_SETTLE_TIME);
801 spin_lock_irqsave(host->host_lock, flags);
802 scsi_report_bus_reset(host, scmd_channel(scmd));
803 spin_unlock_irqrestore(host->host_lock, flags);
809 static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
812 sdev->expecting_cc_ua = 1;
816 * scsi_try_target_reset - Ask host to perform a target reset
817 * @scmd: SCSI cmd used to send a target reset
820 * There is no timeout for this operation. if this operation is
821 * unreliable for a given host, then the host itself needs to put a
822 * timer on it, and set the host back to a consistent state prior to
825 static int scsi_try_target_reset(struct scsi_cmnd *scmd)
829 struct Scsi_Host *host = scmd->device->host;
830 struct scsi_host_template *hostt = host->hostt;
832 if (!hostt->eh_target_reset_handler)
835 rtn = hostt->eh_target_reset_handler(scmd);
836 if (rtn == SUCCESS) {
837 spin_lock_irqsave(host->host_lock, flags);
838 __starget_for_each_device(scsi_target(scmd->device), NULL,
839 __scsi_report_device_reset);
840 spin_unlock_irqrestore(host->host_lock, flags);
847 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
848 * @scmd: SCSI cmd used to send BDR
851 * There is no timeout for this operation. if this operation is
852 * unreliable for a given host, then the host itself needs to put a
853 * timer on it, and set the host back to a consistent state prior to
856 static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
859 struct scsi_host_template *hostt = scmd->device->host->hostt;
861 if (!hostt->eh_device_reset_handler)
864 rtn = hostt->eh_device_reset_handler(scmd);
866 __scsi_report_device_reset(scmd->device, NULL);
870 static int scsi_try_to_abort_cmd(struct scsi_host_template *hostt, struct scsi_cmnd *scmd)
872 if (!hostt->eh_abort_handler)
875 return hostt->eh_abort_handler(scmd);
878 static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
880 if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
881 if (scsi_try_bus_device_reset(scmd) != SUCCESS)
882 if (scsi_try_target_reset(scmd) != SUCCESS)
883 if (scsi_try_bus_reset(scmd) != SUCCESS)
884 scsi_try_host_reset(scmd);
888 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
889 * @scmd: SCSI command structure to hijack
890 * @ses: structure to save restore information
891 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
892 * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
893 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
895 * This function is used to save a scsi command information before re-execution
896 * as part of the error recovery process. If @sense_bytes is 0 the command
897 * sent must be one that does not transfer any data. If @sense_bytes != 0
898 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
899 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
901 void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
902 unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
904 struct scsi_device *sdev = scmd->device;
907 * We need saved copies of a number of fields - this is because
908 * error handling may need to overwrite these with different values
909 * to run different commands, and once error handling is complete,
910 * we will need to restore these values prior to running the actual
913 ses->cmd_len = scmd->cmd_len;
914 ses->cmnd = scmd->cmnd;
915 ses->data_direction = scmd->sc_data_direction;
916 ses->sdb = scmd->sdb;
917 ses->next_rq = scmd->request->next_rq;
918 ses->result = scmd->result;
919 ses->underflow = scmd->underflow;
920 ses->prot_op = scmd->prot_op;
922 scmd->prot_op = SCSI_PROT_NORMAL;
923 scmd->cmnd = ses->eh_cmnd;
924 memset(scmd->cmnd, 0, BLK_MAX_CDB);
925 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
926 scmd->request->next_rq = NULL;
929 scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
931 sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
933 scmd->sdb.table.sgl = &ses->sense_sgl;
934 scmd->sc_data_direction = DMA_FROM_DEVICE;
935 scmd->sdb.table.nents = 1;
936 scmd->cmnd[0] = REQUEST_SENSE;
937 scmd->cmnd[4] = scmd->sdb.length;
938 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
940 scmd->sc_data_direction = DMA_NONE;
942 BUG_ON(cmnd_size > BLK_MAX_CDB);
943 memcpy(scmd->cmnd, cmnd, cmnd_size);
944 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
950 if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
951 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
952 (sdev->lun << 5 & 0xe0);
955 * Zero the sense buffer. The scsi spec mandates that any
956 * untransferred sense data should be interpreted as being zero.
958 memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
960 EXPORT_SYMBOL(scsi_eh_prep_cmnd);
963 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
964 * @scmd: SCSI command structure to restore
965 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
967 * Undo any damage done by above scsi_eh_prep_cmnd().
969 void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
972 * Restore original data
974 scmd->cmd_len = ses->cmd_len;
975 scmd->cmnd = ses->cmnd;
976 scmd->sc_data_direction = ses->data_direction;
977 scmd->sdb = ses->sdb;
978 scmd->request->next_rq = ses->next_rq;
979 scmd->result = ses->result;
980 scmd->underflow = ses->underflow;
981 scmd->prot_op = ses->prot_op;
983 EXPORT_SYMBOL(scsi_eh_restore_cmnd);
986 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
987 * @scmd: SCSI command structure to hijack
989 * @cmnd_size: size in bytes of @cmnd
990 * @timeout: timeout for this request
991 * @sense_bytes: size of sense data to copy or 0
993 * This function is used to send a scsi command down to a target device
994 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
997 * SUCCESS or FAILED or NEEDS_RETRY
999 static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
1000 int cmnd_size, int timeout, unsigned sense_bytes)
1002 struct scsi_device *sdev = scmd->device;
1003 struct Scsi_Host *shost = sdev->host;
1004 DECLARE_COMPLETION_ONSTACK(done);
1005 unsigned long timeleft = timeout;
1006 struct scsi_eh_save ses;
1007 const unsigned long stall_for = msecs_to_jiffies(100);
1011 scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
1012 shost->eh_action = &done;
1014 scsi_log_send(scmd);
1015 scmd->scsi_done = scsi_eh_done;
1016 rtn = shost->hostt->queuecommand(shost, scmd);
1018 if (timeleft > stall_for) {
1019 scsi_eh_restore_cmnd(scmd, &ses);
1020 timeleft -= stall_for;
1021 msleep(jiffies_to_msecs(stall_for));
1024 /* signal not to enter either branch of the if () below */
1028 timeleft = wait_for_completion_timeout(&done, timeout);
1031 shost->eh_action = NULL;
1033 scsi_log_completion(scmd, rtn);
1035 SCSI_LOG_ERROR_RECOVERY(3,
1036 printk("%s: scmd: %p, timeleft: %ld\n",
1037 __func__, scmd, timeleft));
1040 * If there is time left scsi_eh_done got called, and we will examine
1041 * the actual status codes to see whether the command actually did
1042 * complete normally, else if we have a zero return and no time left,
1043 * the command must still be pending, so abort it and return FAILED.
1044 * If we never actually managed to issue the command, because
1045 * ->queuecommand() kept returning non zero, use the rtn = FAILED
1046 * value above (so don't execute either branch of the if)
1049 rtn = scsi_eh_completed_normally(scmd);
1050 SCSI_LOG_ERROR_RECOVERY(3,
1051 printk("%s: scsi_eh_completed_normally %x\n",
1059 case ADD_TO_MLQUEUE:
1067 scsi_abort_eh_cmnd(scmd);
1071 scsi_eh_restore_cmnd(scmd, &ses);
1077 * scsi_request_sense - Request sense data from a particular target.
1078 * @scmd: SCSI cmd for request sense.
1081 * Some hosts automatically obtain this information, others require
1082 * that we obtain it on our own. This function will *not* return until
1083 * the command either times out, or it completes.
1085 static int scsi_request_sense(struct scsi_cmnd *scmd)
1087 return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
1090 static int scsi_eh_action(struct scsi_cmnd *scmd, int rtn)
1092 if (scmd->request->cmd_type != REQ_TYPE_BLOCK_PC) {
1093 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
1094 if (sdrv->eh_action)
1095 rtn = sdrv->eh_action(scmd, rtn);
1101 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
1102 * @scmd: Original SCSI cmd that eh has finished.
1103 * @done_q: Queue for processed commands.
1106 * We don't want to use the normal command completion while we are are
1107 * still handling errors - it may cause other commands to be queued,
1108 * and that would disturb what we are doing. Thus we really want to
1109 * keep a list of pending commands for final completion, and once we
1110 * are ready to leave error handling we handle completion for real.
1112 void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
1114 scmd->device->host->host_failed--;
1115 scmd->eh_eflags = 0;
1116 list_move_tail(&scmd->eh_entry, done_q);
1118 EXPORT_SYMBOL(scsi_eh_finish_cmd);
1121 * scsi_eh_get_sense - Get device sense data.
1122 * @work_q: Queue of commands to process.
1123 * @done_q: Queue of processed commands.
1126 * See if we need to request sense information. if so, then get it
1127 * now, so we have a better idea of what to do.
1130 * This has the unfortunate side effect that if a shost adapter does
1131 * not automatically request sense information, we end up shutting
1132 * it down before we request it.
1134 * All drivers should request sense information internally these days,
1135 * so for now all I have to say is tough noogies if you end up in here.
1137 * XXX: Long term this code should go away, but that needs an audit of
1140 int scsi_eh_get_sense(struct list_head *work_q,
1141 struct list_head *done_q)
1143 struct scsi_cmnd *scmd, *next;
1144 struct Scsi_Host *shost;
1147 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1148 if ((scmd->eh_eflags & SCSI_EH_CANCEL_CMD) ||
1149 SCSI_SENSE_VALID(scmd))
1152 shost = scmd->device->host;
1153 if (scsi_host_eh_past_deadline(shost)) {
1154 SCSI_LOG_ERROR_RECOVERY(3,
1155 shost_printk(KERN_INFO, shost,
1156 "skip %s, past eh deadline\n",
1160 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
1161 "%s: requesting sense\n",
1163 rtn = scsi_request_sense(scmd);
1167 SCSI_LOG_ERROR_RECOVERY(3, printk("sense requested for %p"
1168 " result %x\n", scmd,
1170 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense("bh", scmd));
1172 rtn = scsi_decide_disposition(scmd);
1175 * if the result was normal, then just pass it along to the
1179 /* we don't want this command reissued, just
1180 * finished with the sense data, so set
1181 * retries to the max allowed to ensure it
1182 * won't get reissued */
1183 scmd->retries = scmd->allowed;
1184 else if (rtn != NEEDS_RETRY)
1187 scsi_eh_finish_cmd(scmd, done_q);
1190 return list_empty(work_q);
1192 EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
1195 * scsi_eh_tur - Send TUR to device.
1196 * @scmd: &scsi_cmnd to send TUR
1199 * 0 - Device is ready. 1 - Device NOT ready.
1201 static int scsi_eh_tur(struct scsi_cmnd *scmd)
1203 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
1204 int retry_cnt = 1, rtn;
1207 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
1208 scmd->device->eh_timeout, 0);
1210 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd %p rtn %x\n",
1211 __func__, scmd, rtn));
1226 * scsi_eh_test_devices - check if devices are responding from error recovery.
1227 * @cmd_list: scsi commands in error recovery.
1228 * @work_q: queue for commands which still need more error recovery
1229 * @done_q: queue for commands which are finished
1230 * @try_stu: boolean on if a STU command should be tried in addition to TUR.
1233 * Tests if devices are in a working state. Commands to devices now in
1234 * a working state are sent to the done_q while commands to devices which
1235 * are still failing to respond are returned to the work_q for more
1238 static int scsi_eh_test_devices(struct list_head *cmd_list,
1239 struct list_head *work_q,
1240 struct list_head *done_q, int try_stu)
1242 struct scsi_cmnd *scmd, *next;
1243 struct scsi_device *sdev;
1246 while (!list_empty(cmd_list)) {
1247 scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
1248 sdev = scmd->device;
1251 if (scsi_host_eh_past_deadline(sdev->host)) {
1252 /* Push items back onto work_q */
1253 list_splice_init(cmd_list, work_q);
1254 SCSI_LOG_ERROR_RECOVERY(3,
1255 shost_printk(KERN_INFO, sdev->host,
1256 "skip %s, past eh deadline",
1262 finish_cmds = !scsi_device_online(scmd->device) ||
1263 (try_stu && !scsi_eh_try_stu(scmd) &&
1264 !scsi_eh_tur(scmd)) ||
1267 list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
1268 if (scmd->device == sdev) {
1271 scsi_eh_action(scmd, SUCCESS) == SUCCESS))
1272 scsi_eh_finish_cmd(scmd, done_q);
1274 list_move_tail(&scmd->eh_entry, work_q);
1277 return list_empty(work_q);
1282 * scsi_eh_abort_cmds - abort pending commands.
1283 * @work_q: &list_head for pending commands.
1284 * @done_q: &list_head for processed commands.
1287 * Try and see whether or not it makes sense to try and abort the
1288 * running command. This only works out to be the case if we have one
1289 * command that has timed out. If the command simply failed, it makes
1290 * no sense to try and abort the command, since as far as the shost
1291 * adapter is concerned, it isn't running.
1293 static int scsi_eh_abort_cmds(struct list_head *work_q,
1294 struct list_head *done_q)
1296 struct scsi_cmnd *scmd, *next;
1297 LIST_HEAD(check_list);
1299 struct Scsi_Host *shost;
1301 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1302 if (!(scmd->eh_eflags & SCSI_EH_CANCEL_CMD))
1304 shost = scmd->device->host;
1305 if (scsi_host_eh_past_deadline(shost)) {
1306 list_splice_init(&check_list, work_q);
1307 SCSI_LOG_ERROR_RECOVERY(3,
1308 shost_printk(KERN_INFO, shost,
1309 "skip %s, past eh deadline\n",
1311 return list_empty(work_q);
1313 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting cmd:"
1314 "0x%p\n", current->comm,
1316 rtn = scsi_try_to_abort_cmd(shost->hostt, scmd);
1317 if (rtn == FAILED) {
1318 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting"
1323 list_splice_init(&check_list, work_q);
1324 return list_empty(work_q);
1326 scmd->eh_eflags &= ~SCSI_EH_CANCEL_CMD;
1327 if (rtn == FAST_IO_FAIL)
1328 scsi_eh_finish_cmd(scmd, done_q);
1330 list_move_tail(&scmd->eh_entry, &check_list);
1333 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1337 * scsi_eh_try_stu - Send START_UNIT to device.
1338 * @scmd: &scsi_cmnd to send START_UNIT
1341 * 0 - Device is ready. 1 - Device NOT ready.
1343 static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
1345 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
1347 if (scmd->device->allow_restart) {
1348 int i, rtn = NEEDS_RETRY;
1350 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
1351 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->device->request_queue->rq_timeout, 0);
1361 * scsi_eh_stu - send START_UNIT if needed
1362 * @shost: &scsi host being recovered.
1363 * @work_q: &list_head for pending commands.
1364 * @done_q: &list_head for processed commands.
1367 * If commands are failing due to not ready, initializing command required,
1368 * try revalidating the device, which will end up sending a start unit.
1370 static int scsi_eh_stu(struct Scsi_Host *shost,
1371 struct list_head *work_q,
1372 struct list_head *done_q)
1374 struct scsi_cmnd *scmd, *stu_scmd, *next;
1375 struct scsi_device *sdev;
1377 shost_for_each_device(sdev, shost) {
1378 if (scsi_host_eh_past_deadline(shost)) {
1379 SCSI_LOG_ERROR_RECOVERY(3,
1380 shost_printk(KERN_INFO, shost,
1381 "skip %s, past eh deadline\n",
1386 list_for_each_entry(scmd, work_q, eh_entry)
1387 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
1388 scsi_check_sense(scmd) == FAILED ) {
1396 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending START_UNIT to sdev:"
1397 " 0x%p\n", current->comm, sdev));
1399 if (!scsi_eh_try_stu(stu_scmd)) {
1400 if (!scsi_device_online(sdev) ||
1401 !scsi_eh_tur(stu_scmd)) {
1402 list_for_each_entry_safe(scmd, next,
1404 if (scmd->device == sdev &&
1405 scsi_eh_action(scmd, SUCCESS) == SUCCESS)
1406 scsi_eh_finish_cmd(scmd, done_q);
1410 SCSI_LOG_ERROR_RECOVERY(3,
1411 printk("%s: START_UNIT failed to sdev:"
1412 " 0x%p\n", current->comm, sdev));
1416 return list_empty(work_q);
1421 * scsi_eh_bus_device_reset - send bdr if needed
1422 * @shost: scsi host being recovered.
1423 * @work_q: &list_head for pending commands.
1424 * @done_q: &list_head for processed commands.
1427 * Try a bus device reset. Still, look to see whether we have multiple
1428 * devices that are jammed or not - if we have multiple devices, it
1429 * makes no sense to try bus_device_reset - we really would need to try
1430 * a bus_reset instead.
1432 static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1433 struct list_head *work_q,
1434 struct list_head *done_q)
1436 struct scsi_cmnd *scmd, *bdr_scmd, *next;
1437 struct scsi_device *sdev;
1440 shost_for_each_device(sdev, shost) {
1441 if (scsi_host_eh_past_deadline(shost)) {
1442 SCSI_LOG_ERROR_RECOVERY(3,
1443 shost_printk(KERN_INFO, shost,
1444 "skip %s, past eh deadline\n",
1449 list_for_each_entry(scmd, work_q, eh_entry)
1450 if (scmd->device == sdev) {
1458 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BDR sdev:"
1459 " 0x%p\n", current->comm,
1461 rtn = scsi_try_bus_device_reset(bdr_scmd);
1462 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
1463 if (!scsi_device_online(sdev) ||
1464 rtn == FAST_IO_FAIL ||
1465 !scsi_eh_tur(bdr_scmd)) {
1466 list_for_each_entry_safe(scmd, next,
1468 if (scmd->device == sdev &&
1469 scsi_eh_action(scmd, rtn) != FAILED)
1470 scsi_eh_finish_cmd(scmd,
1475 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BDR"
1483 return list_empty(work_q);
1487 * scsi_eh_target_reset - send target reset if needed
1488 * @shost: scsi host being recovered.
1489 * @work_q: &list_head for pending commands.
1490 * @done_q: &list_head for processed commands.
1493 * Try a target reset.
1495 static int scsi_eh_target_reset(struct Scsi_Host *shost,
1496 struct list_head *work_q,
1497 struct list_head *done_q)
1499 LIST_HEAD(tmp_list);
1500 LIST_HEAD(check_list);
1502 list_splice_init(work_q, &tmp_list);
1504 while (!list_empty(&tmp_list)) {
1505 struct scsi_cmnd *next, *scmd;
1509 if (scsi_host_eh_past_deadline(shost)) {
1510 /* push back on work queue for further processing */
1511 list_splice_init(&check_list, work_q);
1512 list_splice_init(&tmp_list, work_q);
1513 SCSI_LOG_ERROR_RECOVERY(3,
1514 shost_printk(KERN_INFO, shost,
1515 "skip %s, past eh deadline\n",
1517 return list_empty(work_q);
1520 scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
1523 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending target reset "
1525 current->comm, id));
1526 rtn = scsi_try_target_reset(scmd);
1527 if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
1528 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Target reset"
1531 current->comm, id));
1532 list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
1533 if (scmd_id(scmd) != id)
1537 list_move_tail(&scmd->eh_entry, &check_list);
1538 else if (rtn == FAST_IO_FAIL)
1539 scsi_eh_finish_cmd(scmd, done_q);
1541 /* push back on work queue for further processing */
1542 list_move(&scmd->eh_entry, work_q);
1546 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1550 * scsi_eh_bus_reset - send a bus reset
1551 * @shost: &scsi host being recovered.
1552 * @work_q: &list_head for pending commands.
1553 * @done_q: &list_head for processed commands.
1555 static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1556 struct list_head *work_q,
1557 struct list_head *done_q)
1559 struct scsi_cmnd *scmd, *chan_scmd, *next;
1560 LIST_HEAD(check_list);
1561 unsigned int channel;
1565 * we really want to loop over the various channels, and do this on
1566 * a channel by channel basis. we should also check to see if any
1567 * of the failed commands are on soft_reset devices, and if so, skip
1571 for (channel = 0; channel <= shost->max_channel; channel++) {
1572 if (scsi_host_eh_past_deadline(shost)) {
1573 list_splice_init(&check_list, work_q);
1574 SCSI_LOG_ERROR_RECOVERY(3,
1575 shost_printk(KERN_INFO, shost,
1576 "skip %s, past eh deadline\n",
1578 return list_empty(work_q);
1582 list_for_each_entry(scmd, work_q, eh_entry) {
1583 if (channel == scmd_channel(scmd)) {
1587 * FIXME add back in some support for
1588 * soft_reset devices.
1595 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BRST chan:"
1596 " %d\n", current->comm,
1598 rtn = scsi_try_bus_reset(chan_scmd);
1599 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
1600 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1601 if (channel == scmd_channel(scmd)) {
1602 if (rtn == FAST_IO_FAIL)
1603 scsi_eh_finish_cmd(scmd,
1606 list_move_tail(&scmd->eh_entry,
1611 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BRST"
1612 " failed chan: %d\n",
1617 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1621 * scsi_eh_host_reset - send a host reset
1622 * @work_q: list_head for processed commands.
1623 * @done_q: list_head for processed commands.
1625 static int scsi_eh_host_reset(struct list_head *work_q,
1626 struct list_head *done_q)
1628 struct scsi_cmnd *scmd, *next;
1629 LIST_HEAD(check_list);
1632 if (!list_empty(work_q)) {
1633 scmd = list_entry(work_q->next,
1634 struct scsi_cmnd, eh_entry);
1636 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending HRST\n"
1639 rtn = scsi_try_host_reset(scmd);
1640 if (rtn == SUCCESS) {
1641 list_splice_init(work_q, &check_list);
1642 } else if (rtn == FAST_IO_FAIL) {
1643 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1644 scsi_eh_finish_cmd(scmd, done_q);
1647 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: HRST"
1652 return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
1656 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
1657 * @work_q: list_head for processed commands.
1658 * @done_q: list_head for processed commands.
1660 static void scsi_eh_offline_sdevs(struct list_head *work_q,
1661 struct list_head *done_q)
1663 struct scsi_cmnd *scmd, *next;
1665 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1666 sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
1667 "not ready after error recovery\n");
1668 scsi_device_set_state(scmd->device, SDEV_OFFLINE);
1669 if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD) {
1671 * FIXME: Handle lost cmds.
1674 scsi_eh_finish_cmd(scmd, done_q);
1680 * scsi_noretry_cmd - determine if command should be failed fast
1681 * @scmd: SCSI cmd to examine.
1683 int scsi_noretry_cmd(struct scsi_cmnd *scmd)
1685 switch (host_byte(scmd->result)) {
1691 return (scmd->request->cmd_flags & REQ_FAILFAST_TRANSPORT);
1693 return (scmd->request->cmd_flags & REQ_FAILFAST_DEV);
1695 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1696 status_byte(scmd->result) == RESERVATION_CONFLICT)
1699 case DID_SOFT_ERROR:
1700 return (scmd->request->cmd_flags & REQ_FAILFAST_DRIVER);
1703 if (status_byte(scmd->result) != CHECK_CONDITION)
1708 * assume caller has checked sense and determined
1709 * the check condition was retryable.
1711 if (scmd->request->cmd_flags & REQ_FAILFAST_DEV ||
1712 scmd->request->cmd_type == REQ_TYPE_BLOCK_PC)
1719 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1720 * @scmd: SCSI cmd to examine.
1723 * This is *only* called when we are examining the status after sending
1724 * out the actual data command. any commands that are queued for error
1725 * recovery (e.g. test_unit_ready) do *not* come through here.
1727 * When this routine returns failed, it means the error handler thread
1728 * is woken. In cases where the error code indicates an error that
1729 * doesn't require the error handler read (i.e. we don't need to
1730 * abort/reset), this function should return SUCCESS.
1732 int scsi_decide_disposition(struct scsi_cmnd *scmd)
1737 * if the device is offline, then we clearly just pass the result back
1738 * up to the top level.
1740 if (!scsi_device_online(scmd->device)) {
1741 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: device offline - report"
1748 * first check the host byte, to see if there is anything in there
1749 * that would indicate what we need to do.
1751 switch (host_byte(scmd->result)) {
1752 case DID_PASSTHROUGH:
1754 * no matter what, pass this through to the upper layer.
1755 * nuke this special code so that it looks like we are saying
1758 scmd->result &= 0xff00ffff;
1762 * looks good. drop through, and check the next byte.
1766 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
1767 scmd->result |= DID_TIME_OUT << 16;
1770 case DID_NO_CONNECT:
1771 case DID_BAD_TARGET:
1773 * note - this means that we just report the status back
1774 * to the top level driver, not that we actually think
1775 * that it indicates SUCCESS.
1779 * when the low level driver returns did_soft_error,
1780 * it is responsible for keeping an internal retry counter
1781 * in order to avoid endless loops (db)
1783 * actually this is a bug in this function here. we should
1784 * be mindful of the maximum number of retries specified
1785 * and not get stuck in a loop.
1787 case DID_SOFT_ERROR:
1793 return ADD_TO_MLQUEUE;
1794 case DID_TRANSPORT_DISRUPTED:
1796 * LLD/transport was disrupted during processing of the IO.
1797 * The transport class is now blocked/blocking,
1798 * and the transport will decide what to do with the IO
1799 * based on its timers and recovery capablilities if
1800 * there are enough retries.
1803 case DID_TRANSPORT_FAILFAST:
1805 * The transport decided to failfast the IO (most likely
1806 * the fast io fail tmo fired), so send IO directly upwards.
1810 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1811 status_byte(scmd->result) == RESERVATION_CONFLICT)
1813 * execute reservation conflict processing code
1823 * when we scan the bus, we get timeout messages for
1824 * these commands if there is no device available.
1825 * other hosts report did_no_connect for the same thing.
1827 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1828 scmd->cmnd[0] == INQUIRY)) {
1840 * next, check the message byte.
1842 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
1846 * check the status byte to see if this indicates anything special.
1848 switch (status_byte(scmd->result)) {
1850 scsi_handle_queue_full(scmd->device);
1852 * the case of trying to send too many commands to a
1853 * tagged queueing device.
1857 * device can't talk to us at the moment. Should only
1858 * occur (SAM-3) when the task queue is empty, so will cause
1859 * the empty queue handling to trigger a stall in the
1862 return ADD_TO_MLQUEUE;
1864 if (scmd->cmnd[0] == REPORT_LUNS)
1865 scmd->device->sdev_target->expecting_lun_change = 0;
1866 scsi_handle_queue_ramp_up(scmd->device);
1867 case COMMAND_TERMINATED:
1871 case CHECK_CONDITION:
1872 rtn = scsi_check_sense(scmd);
1873 if (rtn == NEEDS_RETRY)
1875 /* if rtn == FAILED, we have no sense information;
1876 * returning FAILED will wake the error handler thread
1877 * to collect the sense and redo the decide
1880 case CONDITION_GOOD:
1881 case INTERMEDIATE_GOOD:
1882 case INTERMEDIATE_C_GOOD:
1885 * who knows? FIXME(eric)
1889 case RESERVATION_CONFLICT:
1890 sdev_printk(KERN_INFO, scmd->device,
1891 "reservation conflict\n");
1892 set_host_byte(scmd, DID_NEXUS_FAILURE);
1893 return SUCCESS; /* causes immediate i/o error */
1901 /* we requeue for retry because the error was retryable, and
1902 * the request was not marked fast fail. Note that above,
1903 * even if the request is marked fast fail, we still requeue
1904 * for queue congestion conditions (QUEUE_FULL or BUSY) */
1905 if ((++scmd->retries) <= scmd->allowed
1906 && !scsi_noretry_cmd(scmd)) {
1910 * no more retries - report this one back to upper level.
1916 static void eh_lock_door_done(struct request *req, int uptodate)
1918 __blk_put_request(req->q, req);
1922 * scsi_eh_lock_door - Prevent medium removal for the specified device
1923 * @sdev: SCSI device to prevent medium removal
1926 * We must be called from process context.
1929 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1930 * head of the devices request queue, and continue.
1932 static void scsi_eh_lock_door(struct scsi_device *sdev)
1934 struct request *req;
1937 * blk_get_request with GFP_KERNEL (__GFP_WAIT) sleeps until a
1938 * request becomes available
1940 req = blk_get_request(sdev->request_queue, READ, GFP_KERNEL);
1942 req->cmd[0] = ALLOW_MEDIUM_REMOVAL;
1946 req->cmd[4] = SCSI_REMOVAL_PREVENT;
1949 req->cmd_len = COMMAND_SIZE(req->cmd[0]);
1951 req->cmd_type = REQ_TYPE_BLOCK_PC;
1952 req->cmd_flags |= REQ_QUIET;
1953 req->timeout = 10 * HZ;
1956 blk_execute_rq_nowait(req->q, NULL, req, 1, eh_lock_door_done);
1960 * scsi_restart_operations - restart io operations to the specified host.
1961 * @shost: Host we are restarting.
1964 * When we entered the error handler, we blocked all further i/o to
1965 * this device. we need to 'reverse' this process.
1967 static void scsi_restart_operations(struct Scsi_Host *shost)
1969 struct scsi_device *sdev;
1970 unsigned long flags;
1973 * If the door was locked, we need to insert a door lock request
1974 * onto the head of the SCSI request queue for the device. There
1975 * is no point trying to lock the door of an off-line device.
1977 shost_for_each_device(sdev, shost) {
1978 if (scsi_device_online(sdev) && sdev->locked)
1979 scsi_eh_lock_door(sdev);
1983 * next free up anything directly waiting upon the host. this
1984 * will be requests for character device operations, and also for
1985 * ioctls to queued block devices.
1987 SCSI_LOG_ERROR_RECOVERY(3,
1988 printk("scsi_eh_%d waking up host to restart\n",
1991 spin_lock_irqsave(shost->host_lock, flags);
1992 if (scsi_host_set_state(shost, SHOST_RUNNING))
1993 if (scsi_host_set_state(shost, SHOST_CANCEL))
1994 BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
1995 spin_unlock_irqrestore(shost->host_lock, flags);
1997 wake_up(&shost->host_wait);
2000 * finally we need to re-initiate requests that may be pending. we will
2001 * have had everything blocked while error handling is taking place, and
2002 * now that error recovery is done, we will need to ensure that these
2003 * requests are started.
2005 scsi_run_host_queues(shost);
2008 * if eh is active and host_eh_scheduled is pending we need to re-run
2009 * recovery. we do this check after scsi_run_host_queues() to allow
2010 * everything pent up since the last eh run a chance to make forward
2011 * progress before we sync again. Either we'll immediately re-run
2012 * recovery or scsi_device_unbusy() will wake us again when these
2013 * pending commands complete.
2015 spin_lock_irqsave(shost->host_lock, flags);
2016 if (shost->host_eh_scheduled)
2017 if (scsi_host_set_state(shost, SHOST_RECOVERY))
2018 WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY));
2019 spin_unlock_irqrestore(shost->host_lock, flags);
2023 * scsi_eh_ready_devs - check device ready state and recover if not.
2024 * @shost: host to be recovered.
2025 * @work_q: &list_head for pending commands.
2026 * @done_q: &list_head for processed commands.
2028 void scsi_eh_ready_devs(struct Scsi_Host *shost,
2029 struct list_head *work_q,
2030 struct list_head *done_q)
2032 if (!scsi_eh_stu(shost, work_q, done_q))
2033 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
2034 if (!scsi_eh_target_reset(shost, work_q, done_q))
2035 if (!scsi_eh_bus_reset(shost, work_q, done_q))
2036 if (!scsi_eh_host_reset(work_q, done_q))
2037 scsi_eh_offline_sdevs(work_q,
2040 EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
2043 * scsi_eh_flush_done_q - finish processed commands or retry them.
2044 * @done_q: list_head of processed commands.
2046 void scsi_eh_flush_done_q(struct list_head *done_q)
2048 struct scsi_cmnd *scmd, *next;
2050 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
2051 list_del_init(&scmd->eh_entry);
2052 if (scsi_device_online(scmd->device) &&
2053 !scsi_noretry_cmd(scmd) &&
2054 (++scmd->retries <= scmd->allowed)) {
2055 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush"
2059 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
2062 * If just we got sense for the device (called
2063 * scsi_eh_get_sense), scmd->result is already
2064 * set, do not set DRIVER_TIMEOUT.
2067 scmd->result |= (DRIVER_TIMEOUT << 24);
2068 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush finish"
2070 current->comm, scmd));
2071 scsi_finish_command(scmd);
2075 EXPORT_SYMBOL(scsi_eh_flush_done_q);
2078 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
2079 * @shost: Host to unjam.
2082 * When we come in here, we *know* that all commands on the bus have
2083 * either completed, failed or timed out. we also know that no further
2084 * commands are being sent to the host, so things are relatively quiet
2085 * and we have freedom to fiddle with things as we wish.
2087 * This is only the *default* implementation. it is possible for
2088 * individual drivers to supply their own version of this function, and
2089 * if the maintainer wishes to do this, it is strongly suggested that
2090 * this function be taken as a template and modified. this function
2091 * was designed to correctly handle problems for about 95% of the
2092 * different cases out there, and it should always provide at least a
2093 * reasonable amount of error recovery.
2095 * Any command marked 'failed' or 'timeout' must eventually have
2096 * scsi_finish_cmd() called for it. we do all of the retry stuff
2097 * here, so when we restart the host after we return it should have an
2100 static void scsi_unjam_host(struct Scsi_Host *shost)
2102 unsigned long flags;
2103 LIST_HEAD(eh_work_q);
2104 LIST_HEAD(eh_done_q);
2106 spin_lock_irqsave(shost->host_lock, flags);
2107 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
2108 spin_unlock_irqrestore(shost->host_lock, flags);
2110 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
2112 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
2113 if (!scsi_eh_abort_cmds(&eh_work_q, &eh_done_q))
2114 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
2116 spin_lock_irqsave(shost->host_lock, flags);
2117 if (shost->eh_deadline != -1)
2118 shost->last_reset = 0;
2119 spin_unlock_irqrestore(shost->host_lock, flags);
2120 scsi_eh_flush_done_q(&eh_done_q);
2124 * scsi_error_handler - SCSI error handler thread
2125 * @data: Host for which we are running.
2128 * This is the main error handling loop. This is run as a kernel thread
2129 * for every SCSI host and handles all error handling activity.
2131 int scsi_error_handler(void *data)
2133 struct Scsi_Host *shost = data;
2136 * We use TASK_INTERRUPTIBLE so that the thread is not
2137 * counted against the load average as a running process.
2138 * We never actually get interrupted because kthread_run
2139 * disables signal delivery for the created thread.
2141 while (!kthread_should_stop()) {
2142 set_current_state(TASK_INTERRUPTIBLE);
2143 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
2144 shost->host_failed != shost->host_busy) {
2145 SCSI_LOG_ERROR_RECOVERY(1,
2146 printk("scsi_eh_%d: sleeping\n",
2152 __set_current_state(TASK_RUNNING);
2153 SCSI_LOG_ERROR_RECOVERY(1,
2154 printk("scsi_eh_%d: waking up %d/%d/%d\n",
2155 shost->host_no, shost->host_eh_scheduled,
2156 shost->host_failed, shost->host_busy));
2159 * We have a host that is failing for some reason. Figure out
2160 * what we need to do to get it up and online again (if we can).
2161 * If we fail, we end up taking the thing offline.
2163 if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
2164 SCSI_LOG_ERROR_RECOVERY(1,
2165 printk(KERN_ERR "Error handler scsi_eh_%d "
2166 "unable to autoresume\n",
2171 if (shost->transportt->eh_strategy_handler)
2172 shost->transportt->eh_strategy_handler(shost);
2174 scsi_unjam_host(shost);
2177 * Note - if the above fails completely, the action is to take
2178 * individual devices offline and flush the queue of any
2179 * outstanding requests that may have been pending. When we
2180 * restart, we restart any I/O to any other devices on the bus
2181 * which are still online.
2183 scsi_restart_operations(shost);
2184 if (!shost->eh_noresume)
2185 scsi_autopm_put_host(shost);
2187 __set_current_state(TASK_RUNNING);
2189 SCSI_LOG_ERROR_RECOVERY(1,
2190 printk("Error handler scsi_eh_%d exiting\n", shost->host_no));
2191 shost->ehandler = NULL;
2196 * Function: scsi_report_bus_reset()
2198 * Purpose: Utility function used by low-level drivers to report that
2199 * they have observed a bus reset on the bus being handled.
2201 * Arguments: shost - Host in question
2202 * channel - channel on which reset was observed.
2206 * Lock status: Host lock must be held.
2208 * Notes: This only needs to be called if the reset is one which
2209 * originates from an unknown location. Resets originated
2210 * by the mid-level itself don't need to call this, but there
2211 * should be no harm.
2213 * The main purpose of this is to make sure that a CHECK_CONDITION
2214 * is properly treated.
2216 void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
2218 struct scsi_device *sdev;
2220 __shost_for_each_device(sdev, shost) {
2221 if (channel == sdev_channel(sdev))
2222 __scsi_report_device_reset(sdev, NULL);
2225 EXPORT_SYMBOL(scsi_report_bus_reset);
2228 * Function: scsi_report_device_reset()
2230 * Purpose: Utility function used by low-level drivers to report that
2231 * they have observed a device reset on the device being handled.
2233 * Arguments: shost - Host in question
2234 * channel - channel on which reset was observed
2235 * target - target on which reset was observed
2239 * Lock status: Host lock must be held
2241 * Notes: This only needs to be called if the reset is one which
2242 * originates from an unknown location. Resets originated
2243 * by the mid-level itself don't need to call this, but there
2244 * should be no harm.
2246 * The main purpose of this is to make sure that a CHECK_CONDITION
2247 * is properly treated.
2249 void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
2251 struct scsi_device *sdev;
2253 __shost_for_each_device(sdev, shost) {
2254 if (channel == sdev_channel(sdev) &&
2255 target == sdev_id(sdev))
2256 __scsi_report_device_reset(sdev, NULL);
2259 EXPORT_SYMBOL(scsi_report_device_reset);
2262 scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
2267 * Function: scsi_reset_provider
2269 * Purpose: Send requested reset to a bus or device at any phase.
2271 * Arguments: device - device to send reset to
2272 * flag - reset type (see scsi.h)
2274 * Returns: SUCCESS/FAILURE.
2276 * Notes: This is used by the SCSI Generic driver to provide
2277 * Bus/Device reset capability.
2280 scsi_reset_provider(struct scsi_device *dev, int flag)
2282 struct scsi_cmnd *scmd;
2283 struct Scsi_Host *shost = dev->host;
2285 unsigned long flags;
2288 if (scsi_autopm_get_host(shost) < 0)
2291 scmd = scsi_get_command(dev, GFP_KERNEL);
2292 blk_rq_init(NULL, &req);
2293 scmd->request = &req;
2295 scmd->cmnd = req.cmd;
2297 scmd->scsi_done = scsi_reset_provider_done_command;
2298 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
2302 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
2304 spin_lock_irqsave(shost->host_lock, flags);
2305 shost->tmf_in_progress = 1;
2306 spin_unlock_irqrestore(shost->host_lock, flags);
2309 case SCSI_TRY_RESET_DEVICE:
2310 rtn = scsi_try_bus_device_reset(scmd);
2314 case SCSI_TRY_RESET_TARGET:
2315 rtn = scsi_try_target_reset(scmd);
2319 case SCSI_TRY_RESET_BUS:
2320 rtn = scsi_try_bus_reset(scmd);
2324 case SCSI_TRY_RESET_HOST:
2325 rtn = scsi_try_host_reset(scmd);
2331 spin_lock_irqsave(shost->host_lock, flags);
2332 shost->tmf_in_progress = 0;
2333 spin_unlock_irqrestore(shost->host_lock, flags);
2336 * be sure to wake up anyone who was sleeping or had their queue
2337 * suspended while we performed the TMF.
2339 SCSI_LOG_ERROR_RECOVERY(3,
2340 printk("%s: waking up host to restart after TMF\n",
2343 wake_up(&shost->host_wait);
2345 scsi_run_host_queues(shost);
2347 scsi_next_command(scmd);
2348 scsi_autopm_put_host(shost);
2351 EXPORT_SYMBOL(scsi_reset_provider);
2354 * scsi_normalize_sense - normalize main elements from either fixed or
2355 * descriptor sense data format into a common format.
2357 * @sense_buffer: byte array containing sense data returned by device
2358 * @sb_len: number of valid bytes in sense_buffer
2359 * @sshdr: pointer to instance of structure that common
2360 * elements are written to.
2363 * The "main elements" from sense data are: response_code, sense_key,
2364 * asc, ascq and additional_length (only for descriptor format).
2366 * Typically this function can be called after a device has
2367 * responded to a SCSI command with the CHECK_CONDITION status.
2370 * 1 if valid sense data information found, else 0;
2372 int scsi_normalize_sense(const u8 *sense_buffer, int sb_len,
2373 struct scsi_sense_hdr *sshdr)
2375 if (!sense_buffer || !sb_len)
2378 memset(sshdr, 0, sizeof(struct scsi_sense_hdr));
2380 sshdr->response_code = (sense_buffer[0] & 0x7f);
2382 if (!scsi_sense_valid(sshdr))
2385 if (sshdr->response_code >= 0x72) {
2390 sshdr->sense_key = (sense_buffer[1] & 0xf);
2392 sshdr->asc = sense_buffer[2];
2394 sshdr->ascq = sense_buffer[3];
2396 sshdr->additional_length = sense_buffer[7];
2402 sshdr->sense_key = (sense_buffer[2] & 0xf);
2404 sb_len = (sb_len < (sense_buffer[7] + 8)) ?
2405 sb_len : (sense_buffer[7] + 8);
2407 sshdr->asc = sense_buffer[12];
2409 sshdr->ascq = sense_buffer[13];
2415 EXPORT_SYMBOL(scsi_normalize_sense);
2417 int scsi_command_normalize_sense(struct scsi_cmnd *cmd,
2418 struct scsi_sense_hdr *sshdr)
2420 return scsi_normalize_sense(cmd->sense_buffer,
2421 SCSI_SENSE_BUFFERSIZE, sshdr);
2423 EXPORT_SYMBOL(scsi_command_normalize_sense);
2426 * scsi_sense_desc_find - search for a given descriptor type in descriptor sense data format.
2427 * @sense_buffer: byte array of descriptor format sense data
2428 * @sb_len: number of valid bytes in sense_buffer
2429 * @desc_type: value of descriptor type to find
2430 * (e.g. 0 -> information)
2433 * only valid when sense data is in descriptor format
2436 * pointer to start of (first) descriptor if found else NULL
2438 const u8 * scsi_sense_desc_find(const u8 * sense_buffer, int sb_len,
2441 int add_sen_len, add_len, desc_len, k;
2444 if ((sb_len < 8) || (0 == (add_sen_len = sense_buffer[7])))
2446 if ((sense_buffer[0] < 0x72) || (sense_buffer[0] > 0x73))
2448 add_sen_len = (add_sen_len < (sb_len - 8)) ?
2449 add_sen_len : (sb_len - 8);
2450 descp = &sense_buffer[8];
2451 for (desc_len = 0, k = 0; k < add_sen_len; k += desc_len) {
2453 add_len = (k < (add_sen_len - 1)) ? descp[1]: -1;
2454 desc_len = add_len + 2;
2455 if (descp[0] == desc_type)
2457 if (add_len < 0) // short descriptor ??
2462 EXPORT_SYMBOL(scsi_sense_desc_find);
2465 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
2466 * @sense_buffer: byte array of sense data
2467 * @sb_len: number of valid bytes in sense_buffer
2468 * @info_out: pointer to 64 integer where 8 or 4 byte information
2469 * field will be placed if found.
2472 * 1 if information field found, 0 if not found.
2474 int scsi_get_sense_info_fld(const u8 * sense_buffer, int sb_len,
2483 switch (sense_buffer[0] & 0x7f) {
2486 if (sense_buffer[0] & 0x80) {
2487 *info_out = (sense_buffer[3] << 24) +
2488 (sense_buffer[4] << 16) +
2489 (sense_buffer[5] << 8) + sense_buffer[6];
2495 ucp = scsi_sense_desc_find(sense_buffer, sb_len,
2497 if (ucp && (0xa == ucp[1])) {
2499 for (j = 0; j < 8; ++j) {
2512 EXPORT_SYMBOL(scsi_get_sense_info_fld);
2515 * scsi_build_sense_buffer - build sense data in a buffer
2516 * @desc: Sense format (non zero == descriptor format,
2517 * 0 == fixed format)
2518 * @buf: Where to build sense data
2520 * @asc: Additional sense code
2521 * @ascq: Additional sense code qualifier
2524 void scsi_build_sense_buffer(int desc, u8 *buf, u8 key, u8 asc, u8 ascq)
2527 buf[0] = 0x72; /* descriptor, current */
2533 buf[0] = 0x70; /* fixed, current */
2540 EXPORT_SYMBOL(scsi_build_sense_buffer);