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/timer.h>
20 #include <linux/string.h>
21 #include <linux/slab.h>
22 #include <linux/kernel.h>
23 #include <linux/kthread.h>
24 #include <linux/interrupt.h>
25 #include <linux/blkdev.h>
26 #include <linux/delay.h>
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_dbg.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_eh.h>
33 #include <scsi/scsi_transport.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi_ioctl.h>
37 #include "scsi_priv.h"
38 #include "scsi_logging.h"
40 #define SENSE_TIMEOUT (10*HZ)
43 * These should *probably* be handled by the host itself.
44 * Since it is allowed to sleep, it probably should.
46 #define BUS_RESET_SETTLE_TIME (10)
47 #define HOST_RESET_SETTLE_TIME (10)
49 /* called with shost->host_lock held */
50 void scsi_eh_wakeup(struct Scsi_Host *shost)
52 if (shost->host_busy == shost->host_failed) {
53 wake_up_process(shost->ehandler);
54 SCSI_LOG_ERROR_RECOVERY(5,
55 printk("Waking error handler thread\n"));
60 * scsi_schedule_eh - schedule EH for SCSI host
61 * @shost: SCSI host to invoke error handling on.
63 * Schedule SCSI EH without scmd.
65 void scsi_schedule_eh(struct Scsi_Host *shost)
69 spin_lock_irqsave(shost->host_lock, flags);
71 if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
72 scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
73 shost->host_eh_scheduled++;
74 scsi_eh_wakeup(shost);
77 spin_unlock_irqrestore(shost->host_lock, flags);
79 EXPORT_SYMBOL_GPL(scsi_schedule_eh);
82 * scsi_eh_scmd_add - add scsi cmd to error handling.
83 * @scmd: scmd to run eh on.
84 * @eh_flag: optional SCSI_EH flag.
89 int scsi_eh_scmd_add(struct scsi_cmnd *scmd, int eh_flag)
91 struct Scsi_Host *shost = scmd->device->host;
98 spin_lock_irqsave(shost->host_lock, flags);
99 if (scsi_host_set_state(shost, SHOST_RECOVERY))
100 if (scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY))
104 scmd->eh_eflags |= eh_flag;
105 list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
106 shost->host_failed++;
107 scsi_eh_wakeup(shost);
109 spin_unlock_irqrestore(shost->host_lock, flags);
114 * scsi_add_timer - Start timeout timer for a single scsi command.
115 * @scmd: scsi command that is about to start running.
116 * @timeout: amount of time to allow this command to run.
117 * @complete: timeout function to call if timer isn't canceled.
120 * This should be turned into an inline function. Each scsi command
121 * has its own timer, and as it is added to the queue, we set up the
122 * timer. When the command completes, we cancel the timer.
124 void scsi_add_timer(struct scsi_cmnd *scmd, int timeout,
125 void (*complete)(struct scsi_cmnd *))
129 * If the clock was already running for this command, then
130 * first delete the timer. The timer handling code gets rather
131 * confused if we don't do this.
133 if (scmd->eh_timeout.function)
134 del_timer(&scmd->eh_timeout);
136 scmd->eh_timeout.data = (unsigned long)scmd;
137 scmd->eh_timeout.expires = jiffies + timeout;
138 scmd->eh_timeout.function = (void (*)(unsigned long)) complete;
140 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: scmd: %p, time:"
141 " %d, (%p)\n", __FUNCTION__,
142 scmd, timeout, complete));
144 add_timer(&scmd->eh_timeout);
148 * scsi_delete_timer - Delete/cancel timer for a given function.
149 * @scmd: Cmd that we are canceling timer for
152 * This should be turned into an inline function.
155 * 1 if we were able to detach the timer. 0 if we blew it, and the
156 * timer function has already started to run.
158 int scsi_delete_timer(struct scsi_cmnd *scmd)
162 rtn = del_timer(&scmd->eh_timeout);
164 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: scmd: %p,"
165 " rtn: %d\n", __FUNCTION__,
168 scmd->eh_timeout.data = (unsigned long)NULL;
169 scmd->eh_timeout.function = NULL;
175 * scsi_times_out - Timeout function for normal scsi commands.
176 * @scmd: Cmd that is timing out.
179 * We do not need to lock this. There is the potential for a race
180 * only in that the normal completion handling might run, but if the
181 * normal completion function determines that the timer has already
182 * fired, then it mustn't do anything.
184 void scsi_times_out(struct scsi_cmnd *scmd)
186 enum scsi_eh_timer_return (* eh_timed_out)(struct scsi_cmnd *);
188 scsi_log_completion(scmd, TIMEOUT_ERROR);
190 if (scmd->device->host->transportt->eh_timed_out)
191 eh_timed_out = scmd->device->host->transportt->eh_timed_out;
192 else if (scmd->device->host->hostt->eh_timed_out)
193 eh_timed_out = scmd->device->host->hostt->eh_timed_out;
198 switch (eh_timed_out(scmd)) {
203 scsi_add_timer(scmd, scmd->timeout_per_command,
210 if (unlikely(!scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD))) {
211 scmd->result |= DID_TIME_OUT << 16;
217 * scsi_block_when_processing_errors - Prevent cmds from being queued.
218 * @sdev: Device on which we are performing recovery.
221 * We block until the host is out of error recovery, and then check to
222 * see whether the host or the device is offline.
225 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
227 int scsi_block_when_processing_errors(struct scsi_device *sdev)
231 wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
233 online = scsi_device_online(sdev);
235 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: rtn: %d\n", __FUNCTION__,
240 EXPORT_SYMBOL(scsi_block_when_processing_errors);
242 #ifdef CONFIG_SCSI_LOGGING
244 * scsi_eh_prt_fail_stats - Log info on failures.
245 * @shost: scsi host being recovered.
246 * @work_q: Queue of scsi cmds to process.
248 static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
249 struct list_head *work_q)
251 struct scsi_cmnd *scmd;
252 struct scsi_device *sdev;
253 int total_failures = 0;
256 int devices_failed = 0;
258 shost_for_each_device(sdev, shost) {
259 list_for_each_entry(scmd, work_q, eh_entry) {
260 if (scmd->device == sdev) {
262 if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD)
269 if (cmd_cancel || cmd_failed) {
270 SCSI_LOG_ERROR_RECOVERY(3,
271 sdev_printk(KERN_INFO, sdev,
272 "%s: cmds failed: %d, cancel: %d\n",
273 __FUNCTION__, cmd_failed,
281 SCSI_LOG_ERROR_RECOVERY(2, printk("Total of %d commands on %d"
282 " devices require eh work\n",
283 total_failures, devices_failed));
288 * scsi_check_sense - Examine scsi cmd sense
289 * @scmd: Cmd to have sense checked.
292 * SUCCESS or FAILED or NEEDS_RETRY
295 * When a deferred error is detected the current command has
296 * not been executed and needs retrying.
298 static int scsi_check_sense(struct scsi_cmnd *scmd)
300 struct scsi_sense_hdr sshdr;
302 if (! scsi_command_normalize_sense(scmd, &sshdr))
303 return FAILED; /* no valid sense data */
305 if (scsi_sense_is_deferred(&sshdr))
309 * Previous logic looked for FILEMARK, EOM or ILI which are
310 * mainly associated with tapes and returned SUCCESS.
312 if (sshdr.response_code == 0x70) {
314 if (scmd->sense_buffer[2] & 0xe0)
318 * descriptor format: look for "stream commands sense data
319 * descriptor" (see SSC-3). Assume single sense data
320 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
322 if ((sshdr.additional_length > 3) &&
323 (scmd->sense_buffer[8] == 0x4) &&
324 (scmd->sense_buffer[11] & 0xe0))
328 switch (sshdr.sense_key) {
331 case RECOVERED_ERROR:
332 return /* soft_error */ SUCCESS;
334 case ABORTED_COMMAND:
339 * if we are expecting a cc/ua because of a bus reset that we
340 * performed, treat this just as a retry. otherwise this is
341 * information that we should pass up to the upper-level driver
342 * so that we can deal with it there.
344 if (scmd->device->expecting_cc_ua) {
345 scmd->device->expecting_cc_ua = 0;
349 * if the device is in the process of becoming ready, we
352 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
355 * if the device is not started, we need to wake
356 * the error handler to start the motor
358 if (scmd->device->allow_restart &&
359 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
363 /* these three are not supported */
365 case VOLUME_OVERFLOW:
370 if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
371 sshdr.asc == 0x13 || /* AMNF DATA FIELD */
372 sshdr.asc == 0x14) { /* RECORD NOT FOUND */
378 if (scmd->device->retry_hwerror)
383 case ILLEGAL_REQUEST:
392 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
393 * @scmd: SCSI cmd to examine.
396 * This is *only* called when we are examining the status of commands
397 * queued during error recovery. the main difference here is that we
398 * don't allow for the possibility of retries here, and we are a lot
399 * more restrictive about what we consider acceptable.
401 static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
404 * first check the host byte, to see if there is anything in there
405 * that would indicate what we need to do.
407 if (host_byte(scmd->result) == DID_RESET) {
409 * rats. we are already in the error handler, so we now
410 * get to try and figure out what to do next. if the sense
411 * is valid, we have a pretty good idea of what to do.
412 * if not, we mark it as FAILED.
414 return scsi_check_sense(scmd);
416 if (host_byte(scmd->result) != DID_OK)
420 * next, check the message byte.
422 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
426 * now, check the status byte to see if this indicates
429 switch (status_byte(scmd->result)) {
431 case COMMAND_TERMINATED:
433 case CHECK_CONDITION:
434 return scsi_check_sense(scmd);
436 case INTERMEDIATE_GOOD:
437 case INTERMEDIATE_C_GOOD:
439 * who knows? FIXME(eric)
444 case RESERVATION_CONFLICT:
452 * scsi_eh_done - Completion function for error handling.
453 * @scmd: Cmd that is done.
455 static void scsi_eh_done(struct scsi_cmnd *scmd)
457 struct completion *eh_action;
459 SCSI_LOG_ERROR_RECOVERY(3,
460 printk("%s scmd: %p result: %x\n",
461 __FUNCTION__, scmd, scmd->result));
463 eh_action = scmd->device->host->eh_action;
469 * scsi_try_host_reset - ask host adapter to reset itself
470 * @scmd: SCSI cmd to send hsot reset.
472 static int scsi_try_host_reset(struct scsi_cmnd *scmd)
477 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Host RST\n",
480 if (!scmd->device->host->hostt->eh_host_reset_handler)
483 rtn = scmd->device->host->hostt->eh_host_reset_handler(scmd);
485 if (rtn == SUCCESS) {
486 if (!scmd->device->host->hostt->skip_settle_delay)
487 ssleep(HOST_RESET_SETTLE_TIME);
488 spin_lock_irqsave(scmd->device->host->host_lock, flags);
489 scsi_report_bus_reset(scmd->device->host,
491 spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
498 * scsi_try_bus_reset - ask host to perform a bus reset
499 * @scmd: SCSI cmd to send bus reset.
501 static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
506 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Bus RST\n",
509 if (!scmd->device->host->hostt->eh_bus_reset_handler)
512 rtn = scmd->device->host->hostt->eh_bus_reset_handler(scmd);
514 if (rtn == SUCCESS) {
515 if (!scmd->device->host->hostt->skip_settle_delay)
516 ssleep(BUS_RESET_SETTLE_TIME);
517 spin_lock_irqsave(scmd->device->host->host_lock, flags);
518 scsi_report_bus_reset(scmd->device->host,
520 spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
527 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
528 * @scmd: SCSI cmd used to send BDR
531 * There is no timeout for this operation. if this operation is
532 * unreliable for a given host, then the host itself needs to put a
533 * timer on it, and set the host back to a consistent state prior to
536 static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
540 if (!scmd->device->host->hostt->eh_device_reset_handler)
543 rtn = scmd->device->host->hostt->eh_device_reset_handler(scmd);
544 if (rtn == SUCCESS) {
545 scmd->device->was_reset = 1;
546 scmd->device->expecting_cc_ua = 1;
552 static int __scsi_try_to_abort_cmd(struct scsi_cmnd *scmd)
554 if (!scmd->device->host->hostt->eh_abort_handler)
557 return scmd->device->host->hostt->eh_abort_handler(scmd);
561 * scsi_try_to_abort_cmd - Ask host to abort a running command.
562 * @scmd: SCSI cmd to abort from Lower Level.
565 * This function will not return until the user's completion function
566 * has been called. there is no timeout on this operation. if the
567 * author of the low-level driver wishes this operation to be timed,
568 * they can provide this facility themselves. helper functions in
569 * scsi_error.c can be supplied to make this easier to do.
571 static int scsi_try_to_abort_cmd(struct scsi_cmnd *scmd)
574 * scsi_done was called just after the command timed out and before
575 * we had a chance to process it. (db)
577 if (scmd->serial_number == 0)
579 return __scsi_try_to_abort_cmd(scmd);
582 static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
584 if (__scsi_try_to_abort_cmd(scmd) != SUCCESS)
585 if (scsi_try_bus_device_reset(scmd) != SUCCESS)
586 if (scsi_try_bus_reset(scmd) != SUCCESS)
587 scsi_try_host_reset(scmd);
591 * scsi_send_eh_cmnd - submit a scsi command as part of error recory
592 * @scmd: SCSI command structure to hijack
594 * @cmnd_size: size in bytes of @cmnd
595 * @timeout: timeout for this request
596 * @copy_sense: request sense data if set to 1
598 * This function is used to send a scsi command down to a target device
599 * as part of the error recovery process. If @copy_sense is 0 the command
600 * sent must be one that does not transfer any data. If @copy_sense is 1
601 * the command must be REQUEST_SENSE and this functions copies out the
602 * sense buffer it got into @scmd->sense_buffer.
605 * SUCCESS or FAILED or NEEDS_RETRY
607 static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
608 int cmnd_size, int timeout, int copy_sense)
610 struct scsi_device *sdev = scmd->device;
611 struct Scsi_Host *shost = sdev->host;
612 int old_result = scmd->result;
613 DECLARE_COMPLETION_ONSTACK(done);
614 unsigned long timeleft;
616 struct scatterlist sgl;
617 unsigned char old_cmnd[MAX_COMMAND_SIZE];
618 enum dma_data_direction old_data_direction;
619 unsigned short old_use_sg;
620 unsigned char old_cmd_len;
621 unsigned old_bufflen;
626 * We need saved copies of a number of fields - this is because
627 * error handling may need to overwrite these with different values
628 * to run different commands, and once error handling is complete,
629 * we will need to restore these values prior to running the actual
632 old_buffer = scmd->request_buffer;
633 old_bufflen = scmd->request_bufflen;
634 memcpy(old_cmnd, scmd->cmnd, sizeof(scmd->cmnd));
635 old_data_direction = scmd->sc_data_direction;
636 old_cmd_len = scmd->cmd_len;
637 old_use_sg = scmd->use_sg;
639 memset(scmd->cmnd, 0, sizeof(scmd->cmnd));
640 memcpy(scmd->cmnd, cmnd, cmnd_size);
643 gfp_t gfp_mask = GFP_ATOMIC;
645 if (shost->hostt->unchecked_isa_dma)
646 gfp_mask |= __GFP_DMA;
648 sgl.page = alloc_page(gfp_mask);
654 scmd->sc_data_direction = DMA_FROM_DEVICE;
655 scmd->request_bufflen = sgl.length;
656 scmd->request_buffer = &sgl;
659 scmd->request_buffer = NULL;
660 scmd->request_bufflen = 0;
661 scmd->sc_data_direction = DMA_NONE;
666 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
668 if (sdev->scsi_level <= SCSI_2)
669 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
670 (sdev->lun << 5 & 0xe0);
673 * Zero the sense buffer. The scsi spec mandates that any
674 * untransferred sense data should be interpreted as being zero.
676 memset(scmd->sense_buffer, 0, sizeof(scmd->sense_buffer));
678 shost->eh_action = &done;
680 spin_lock_irqsave(shost->host_lock, flags);
682 shost->hostt->queuecommand(scmd, scsi_eh_done);
683 spin_unlock_irqrestore(shost->host_lock, flags);
685 timeleft = wait_for_completion_timeout(&done, timeout);
687 shost->eh_action = NULL;
689 scsi_log_completion(scmd, SUCCESS);
691 SCSI_LOG_ERROR_RECOVERY(3,
692 printk("%s: scmd: %p, timeleft: %ld\n",
693 __FUNCTION__, scmd, timeleft));
696 * If there is time left scsi_eh_done got called, and we will
697 * examine the actual status codes to see whether the command
698 * actually did complete normally, else tell the host to forget
699 * about this command.
702 rtn = scsi_eh_completed_normally(scmd);
703 SCSI_LOG_ERROR_RECOVERY(3,
704 printk("%s: scsi_eh_completed_normally %x\n",
717 scsi_abort_eh_cmnd(scmd);
723 * Last chance to have valid sense data.
726 if (!SCSI_SENSE_VALID(scmd)) {
727 memcpy(scmd->sense_buffer, page_address(sgl.page),
728 sizeof(scmd->sense_buffer));
730 __free_page(sgl.page);
735 * Restore original data
737 scmd->request_buffer = old_buffer;
738 scmd->request_bufflen = old_bufflen;
739 memcpy(scmd->cmnd, old_cmnd, sizeof(scmd->cmnd));
740 scmd->sc_data_direction = old_data_direction;
741 scmd->cmd_len = old_cmd_len;
742 scmd->use_sg = old_use_sg;
743 scmd->result = old_result;
748 * scsi_request_sense - Request sense data from a particular target.
749 * @scmd: SCSI cmd for request sense.
752 * Some hosts automatically obtain this information, others require
753 * that we obtain it on our own. This function will *not* return until
754 * the command either times out, or it completes.
756 static int scsi_request_sense(struct scsi_cmnd *scmd)
758 static unsigned char generic_sense[6] =
759 {REQUEST_SENSE, 0, 0, 0, 252, 0};
761 return scsi_send_eh_cmnd(scmd, generic_sense, 6, SENSE_TIMEOUT, 1);
765 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
766 * @scmd: Original SCSI cmd that eh has finished.
767 * @done_q: Queue for processed commands.
770 * We don't want to use the normal command completion while we are are
771 * still handling errors - it may cause other commands to be queued,
772 * and that would disturb what we are doing. thus we really want to
773 * keep a list of pending commands for final completion, and once we
774 * are ready to leave error handling we handle completion for real.
776 void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
778 scmd->device->host->host_failed--;
780 list_move_tail(&scmd->eh_entry, done_q);
782 EXPORT_SYMBOL(scsi_eh_finish_cmd);
785 * scsi_eh_get_sense - Get device sense data.
786 * @work_q: Queue of commands to process.
787 * @done_q: Queue of proccessed commands..
790 * See if we need to request sense information. if so, then get it
791 * now, so we have a better idea of what to do.
794 * This has the unfortunate side effect that if a shost adapter does
795 * not automatically request sense information, that we end up shutting
796 * it down before we request it.
798 * All drivers should request sense information internally these days,
799 * so for now all I have to say is tough noogies if you end up in here.
801 * XXX: Long term this code should go away, but that needs an audit of
804 int scsi_eh_get_sense(struct list_head *work_q,
805 struct list_head *done_q)
807 struct scsi_cmnd *scmd, *next;
810 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
811 if ((scmd->eh_eflags & SCSI_EH_CANCEL_CMD) ||
812 SCSI_SENSE_VALID(scmd))
815 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
816 "%s: requesting sense\n",
818 rtn = scsi_request_sense(scmd);
822 SCSI_LOG_ERROR_RECOVERY(3, printk("sense requested for %p"
823 " result %x\n", scmd,
825 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense("bh", scmd));
827 rtn = scsi_decide_disposition(scmd);
830 * if the result was normal, then just pass it along to the
834 /* we don't want this command reissued, just
835 * finished with the sense data, so set
836 * retries to the max allowed to ensure it
837 * won't get reissued */
838 scmd->retries = scmd->allowed;
839 else if (rtn != NEEDS_RETRY)
842 scsi_eh_finish_cmd(scmd, done_q);
845 return list_empty(work_q);
847 EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
850 * scsi_eh_tur - Send TUR to device.
851 * @scmd: Scsi cmd to send TUR
854 * 0 - Device is ready. 1 - Device NOT ready.
856 static int scsi_eh_tur(struct scsi_cmnd *scmd)
858 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
859 int retry_cnt = 1, rtn;
862 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6, SENSE_TIMEOUT, 0);
864 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd %p rtn %x\n",
865 __FUNCTION__, scmd, rtn));
880 * scsi_eh_abort_cmds - abort canceled commands.
881 * @shost: scsi host being recovered.
882 * @eh_done_q: list_head for processed commands.
885 * Try and see whether or not it makes sense to try and abort the
886 * running command. this only works out to be the case if we have one
887 * command that has timed out. if the command simply failed, it makes
888 * no sense to try and abort the command, since as far as the shost
889 * adapter is concerned, it isn't running.
891 static int scsi_eh_abort_cmds(struct list_head *work_q,
892 struct list_head *done_q)
894 struct scsi_cmnd *scmd, *next;
897 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
898 if (!(scmd->eh_eflags & SCSI_EH_CANCEL_CMD))
900 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting cmd:"
901 "0x%p\n", current->comm,
903 rtn = scsi_try_to_abort_cmd(scmd);
904 if (rtn == SUCCESS) {
905 scmd->eh_eflags &= ~SCSI_EH_CANCEL_CMD;
906 if (!scsi_device_online(scmd->device) ||
907 !scsi_eh_tur(scmd)) {
908 scsi_eh_finish_cmd(scmd, done_q);
912 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting"
919 return list_empty(work_q);
923 * scsi_eh_try_stu - Send START_UNIT to device.
924 * @scmd: Scsi cmd to send START_UNIT
927 * 0 - Device is ready. 1 - Device NOT ready.
929 static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
931 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
933 if (scmd->device->allow_restart) {
934 int i, rtn = NEEDS_RETRY;
936 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
937 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6,
938 scmd->device->timeout, 0);
948 * scsi_eh_stu - send START_UNIT if needed
949 * @shost: scsi host being recovered.
950 * @eh_done_q: list_head for processed commands.
953 * If commands are failing due to not ready, initializing command required,
954 * try revalidating the device, which will end up sending a start unit.
956 static int scsi_eh_stu(struct Scsi_Host *shost,
957 struct list_head *work_q,
958 struct list_head *done_q)
960 struct scsi_cmnd *scmd, *stu_scmd, *next;
961 struct scsi_device *sdev;
963 shost_for_each_device(sdev, shost) {
965 list_for_each_entry(scmd, work_q, eh_entry)
966 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
967 scsi_check_sense(scmd) == FAILED ) {
975 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending START_UNIT to sdev:"
976 " 0x%p\n", current->comm, sdev));
978 if (!scsi_eh_try_stu(stu_scmd)) {
979 if (!scsi_device_online(sdev) ||
980 !scsi_eh_tur(stu_scmd)) {
981 list_for_each_entry_safe(scmd, next,
983 if (scmd->device == sdev)
984 scsi_eh_finish_cmd(scmd, done_q);
988 SCSI_LOG_ERROR_RECOVERY(3,
989 printk("%s: START_UNIT failed to sdev:"
990 " 0x%p\n", current->comm, sdev));
994 return list_empty(work_q);
999 * scsi_eh_bus_device_reset - send bdr if needed
1000 * @shost: scsi host being recovered.
1001 * @eh_done_q: list_head for processed commands.
1004 * Try a bus device reset. still, look to see whether we have multiple
1005 * devices that are jammed or not - if we have multiple devices, it
1006 * makes no sense to try bus_device_reset - we really would need to try
1007 * a bus_reset instead.
1009 static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1010 struct list_head *work_q,
1011 struct list_head *done_q)
1013 struct scsi_cmnd *scmd, *bdr_scmd, *next;
1014 struct scsi_device *sdev;
1017 shost_for_each_device(sdev, shost) {
1019 list_for_each_entry(scmd, work_q, eh_entry)
1020 if (scmd->device == sdev) {
1028 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BDR sdev:"
1029 " 0x%p\n", current->comm,
1031 rtn = scsi_try_bus_device_reset(bdr_scmd);
1032 if (rtn == SUCCESS) {
1033 if (!scsi_device_online(sdev) ||
1034 !scsi_eh_tur(bdr_scmd)) {
1035 list_for_each_entry_safe(scmd, next,
1037 if (scmd->device == sdev)
1038 scsi_eh_finish_cmd(scmd,
1043 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BDR"
1051 return list_empty(work_q);
1055 * scsi_eh_bus_reset - send a bus reset
1056 * @shost: scsi host being recovered.
1057 * @eh_done_q: list_head for processed commands.
1059 static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1060 struct list_head *work_q,
1061 struct list_head *done_q)
1063 struct scsi_cmnd *scmd, *chan_scmd, *next;
1064 unsigned int channel;
1068 * we really want to loop over the various channels, and do this on
1069 * a channel by channel basis. we should also check to see if any
1070 * of the failed commands are on soft_reset devices, and if so, skip
1074 for (channel = 0; channel <= shost->max_channel; channel++) {
1076 list_for_each_entry(scmd, work_q, eh_entry) {
1077 if (channel == scmd_channel(scmd)) {
1081 * FIXME add back in some support for
1082 * soft_reset devices.
1089 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BRST chan:"
1090 " %d\n", current->comm,
1092 rtn = scsi_try_bus_reset(chan_scmd);
1093 if (rtn == SUCCESS) {
1094 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1095 if (channel == scmd_channel(scmd))
1096 if (!scsi_device_online(scmd->device) ||
1098 scsi_eh_finish_cmd(scmd,
1102 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BRST"
1103 " failed chan: %d\n",
1108 return list_empty(work_q);
1112 * scsi_eh_host_reset - send a host reset
1113 * @work_q: list_head for processed commands.
1114 * @done_q: list_head for processed commands.
1116 static int scsi_eh_host_reset(struct list_head *work_q,
1117 struct list_head *done_q)
1119 struct scsi_cmnd *scmd, *next;
1122 if (!list_empty(work_q)) {
1123 scmd = list_entry(work_q->next,
1124 struct scsi_cmnd, eh_entry);
1126 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending HRST\n"
1129 rtn = scsi_try_host_reset(scmd);
1130 if (rtn == SUCCESS) {
1131 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1132 if (!scsi_device_online(scmd->device) ||
1133 (!scsi_eh_try_stu(scmd) && !scsi_eh_tur(scmd)) ||
1135 scsi_eh_finish_cmd(scmd, done_q);
1138 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: HRST"
1143 return list_empty(work_q);
1147 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
1148 * @work_q: list_head for processed commands.
1149 * @done_q: list_head for processed commands.
1152 static void scsi_eh_offline_sdevs(struct list_head *work_q,
1153 struct list_head *done_q)
1155 struct scsi_cmnd *scmd, *next;
1157 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1158 sdev_printk(KERN_INFO, scmd->device,
1159 "scsi: Device offlined - not"
1160 " ready after error recovery\n");
1161 scsi_device_set_state(scmd->device, SDEV_OFFLINE);
1162 if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD) {
1164 * FIXME: Handle lost cmds.
1167 scsi_eh_finish_cmd(scmd, done_q);
1173 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1174 * @scmd: SCSI cmd to examine.
1177 * This is *only* called when we are examining the status after sending
1178 * out the actual data command. any commands that are queued for error
1179 * recovery (e.g. test_unit_ready) do *not* come through here.
1181 * When this routine returns failed, it means the error handler thread
1182 * is woken. In cases where the error code indicates an error that
1183 * doesn't require the error handler read (i.e. we don't need to
1184 * abort/reset), this function should return SUCCESS.
1186 int scsi_decide_disposition(struct scsi_cmnd *scmd)
1191 * if the device is offline, then we clearly just pass the result back
1192 * up to the top level.
1194 if (!scsi_device_online(scmd->device)) {
1195 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: device offline - report"
1202 * first check the host byte, to see if there is anything in there
1203 * that would indicate what we need to do.
1205 switch (host_byte(scmd->result)) {
1206 case DID_PASSTHROUGH:
1208 * no matter what, pass this through to the upper layer.
1209 * nuke this special code so that it looks like we are saying
1212 scmd->result &= 0xff00ffff;
1216 * looks good. drop through, and check the next byte.
1219 case DID_NO_CONNECT:
1220 case DID_BAD_TARGET:
1223 * note - this means that we just report the status back
1224 * to the top level driver, not that we actually think
1225 * that it indicates SUCCESS.
1229 * when the low level driver returns did_soft_error,
1230 * it is responsible for keeping an internal retry counter
1231 * in order to avoid endless loops (db)
1233 * actually this is a bug in this function here. we should
1234 * be mindful of the maximum number of retries specified
1235 * and not get stuck in a loop.
1237 case DID_SOFT_ERROR:
1243 return ADD_TO_MLQUEUE;
1246 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1247 status_byte(scmd->result) == RESERVATION_CONFLICT)
1249 * execute reservation conflict processing code
1260 * when we scan the bus, we get timeout messages for
1261 * these commands if there is no device available.
1262 * other hosts report did_no_connect for the same thing.
1264 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1265 scmd->cmnd[0] == INQUIRY)) {
1277 * next, check the message byte.
1279 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
1283 * check the status byte to see if this indicates anything special.
1285 switch (status_byte(scmd->result)) {
1288 * the case of trying to send too many commands to a
1289 * tagged queueing device.
1293 * device can't talk to us at the moment. Should only
1294 * occur (SAM-3) when the task queue is empty, so will cause
1295 * the empty queue handling to trigger a stall in the
1298 return ADD_TO_MLQUEUE;
1300 case COMMAND_TERMINATED:
1303 case CHECK_CONDITION:
1304 rtn = scsi_check_sense(scmd);
1305 if (rtn == NEEDS_RETRY)
1307 /* if rtn == FAILED, we have no sense information;
1308 * returning FAILED will wake the error handler thread
1309 * to collect the sense and redo the decide
1312 case CONDITION_GOOD:
1313 case INTERMEDIATE_GOOD:
1314 case INTERMEDIATE_C_GOOD:
1317 * who knows? FIXME(eric)
1321 case RESERVATION_CONFLICT:
1322 sdev_printk(KERN_INFO, scmd->device,
1323 "reservation conflict\n");
1324 return SUCCESS; /* causes immediate i/o error */
1332 /* we requeue for retry because the error was retryable, and
1333 * the request was not marked fast fail. Note that above,
1334 * even if the request is marked fast fail, we still requeue
1335 * for queue congestion conditions (QUEUE_FULL or BUSY) */
1336 if ((++scmd->retries) <= scmd->allowed
1337 && !blk_noretry_request(scmd->request)) {
1341 * no more retries - report this one back to upper level.
1348 * scsi_eh_lock_door - Prevent medium removal for the specified device
1349 * @sdev: SCSI device to prevent medium removal
1352 * We must be called from process context; scsi_allocate_request()
1356 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1357 * head of the devices request queue, and continue.
1360 * scsi_allocate_request() may sleep waiting for existing requests to
1361 * be processed. However, since we haven't kicked off any request
1362 * processing for this host, this may deadlock.
1364 * If scsi_allocate_request() fails for what ever reason, we
1365 * completely forget to lock the door.
1367 static void scsi_eh_lock_door(struct scsi_device *sdev)
1369 unsigned char cmnd[MAX_COMMAND_SIZE];
1371 cmnd[0] = ALLOW_MEDIUM_REMOVAL;
1375 cmnd[4] = SCSI_REMOVAL_PREVENT;
1378 scsi_execute_async(sdev, cmnd, 6, DMA_NONE, NULL, 0, 0, 10 * HZ,
1379 5, NULL, NULL, GFP_KERNEL);
1384 * scsi_restart_operations - restart io operations to the specified host.
1385 * @shost: Host we are restarting.
1388 * When we entered the error handler, we blocked all further i/o to
1389 * this device. we need to 'reverse' this process.
1391 static void scsi_restart_operations(struct Scsi_Host *shost)
1393 struct scsi_device *sdev;
1394 unsigned long flags;
1397 * If the door was locked, we need to insert a door lock request
1398 * onto the head of the SCSI request queue for the device. There
1399 * is no point trying to lock the door of an off-line device.
1401 shost_for_each_device(sdev, shost) {
1402 if (scsi_device_online(sdev) && sdev->locked)
1403 scsi_eh_lock_door(sdev);
1407 * next free up anything directly waiting upon the host. this
1408 * will be requests for character device operations, and also for
1409 * ioctls to queued block devices.
1411 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: waking up host to restart\n",
1414 spin_lock_irqsave(shost->host_lock, flags);
1415 if (scsi_host_set_state(shost, SHOST_RUNNING))
1416 if (scsi_host_set_state(shost, SHOST_CANCEL))
1417 BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
1418 spin_unlock_irqrestore(shost->host_lock, flags);
1420 wake_up(&shost->host_wait);
1423 * finally we need to re-initiate requests that may be pending. we will
1424 * have had everything blocked while error handling is taking place, and
1425 * now that error recovery is done, we will need to ensure that these
1426 * requests are started.
1428 scsi_run_host_queues(shost);
1432 * scsi_eh_ready_devs - check device ready state and recover if not.
1433 * @shost: host to be recovered.
1434 * @eh_done_q: list_head for processed commands.
1437 void scsi_eh_ready_devs(struct Scsi_Host *shost,
1438 struct list_head *work_q,
1439 struct list_head *done_q)
1441 if (!scsi_eh_stu(shost, work_q, done_q))
1442 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
1443 if (!scsi_eh_bus_reset(shost, work_q, done_q))
1444 if (!scsi_eh_host_reset(work_q, done_q))
1445 scsi_eh_offline_sdevs(work_q, done_q);
1447 EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
1450 * scsi_eh_flush_done_q - finish processed commands or retry them.
1451 * @done_q: list_head of processed commands.
1454 void scsi_eh_flush_done_q(struct list_head *done_q)
1456 struct scsi_cmnd *scmd, *next;
1458 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
1459 list_del_init(&scmd->eh_entry);
1460 if (scsi_device_online(scmd->device) &&
1461 !blk_noretry_request(scmd->request) &&
1462 (++scmd->retries <= scmd->allowed)) {
1463 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush"
1467 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
1470 * If just we got sense for the device (called
1471 * scsi_eh_get_sense), scmd->result is already
1472 * set, do not set DRIVER_TIMEOUT.
1475 scmd->result |= (DRIVER_TIMEOUT << 24);
1476 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush finish"
1478 current->comm, scmd));
1479 scsi_finish_command(scmd);
1483 EXPORT_SYMBOL(scsi_eh_flush_done_q);
1486 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
1487 * @shost: Host to unjam.
1490 * When we come in here, we *know* that all commands on the bus have
1491 * either completed, failed or timed out. we also know that no further
1492 * commands are being sent to the host, so things are relatively quiet
1493 * and we have freedom to fiddle with things as we wish.
1495 * This is only the *default* implementation. it is possible for
1496 * individual drivers to supply their own version of this function, and
1497 * if the maintainer wishes to do this, it is strongly suggested that
1498 * this function be taken as a template and modified. this function
1499 * was designed to correctly handle problems for about 95% of the
1500 * different cases out there, and it should always provide at least a
1501 * reasonable amount of error recovery.
1503 * Any command marked 'failed' or 'timeout' must eventually have
1504 * scsi_finish_cmd() called for it. we do all of the retry stuff
1505 * here, so when we restart the host after we return it should have an
1508 static void scsi_unjam_host(struct Scsi_Host *shost)
1510 unsigned long flags;
1511 LIST_HEAD(eh_work_q);
1512 LIST_HEAD(eh_done_q);
1514 spin_lock_irqsave(shost->host_lock, flags);
1515 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
1516 spin_unlock_irqrestore(shost->host_lock, flags);
1518 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
1520 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
1521 if (!scsi_eh_abort_cmds(&eh_work_q, &eh_done_q))
1522 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
1524 scsi_eh_flush_done_q(&eh_done_q);
1528 * scsi_error_handler - SCSI error handler thread
1529 * @data: Host for which we are running.
1532 * This is the main error handling loop. This is run as a kernel thread
1533 * for every SCSI host and handles all error handling activity.
1535 int scsi_error_handler(void *data)
1537 struct Scsi_Host *shost = data;
1539 current->flags |= PF_NOFREEZE;
1542 * We use TASK_INTERRUPTIBLE so that the thread is not
1543 * counted against the load average as a running process.
1544 * We never actually get interrupted because kthread_run
1545 * disables singal delivery for the created thread.
1547 set_current_state(TASK_INTERRUPTIBLE);
1548 while (!kthread_should_stop()) {
1549 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
1550 shost->host_failed != shost->host_busy) {
1551 SCSI_LOG_ERROR_RECOVERY(1,
1552 printk("Error handler scsi_eh_%d sleeping\n",
1555 set_current_state(TASK_INTERRUPTIBLE);
1559 __set_current_state(TASK_RUNNING);
1560 SCSI_LOG_ERROR_RECOVERY(1,
1561 printk("Error handler scsi_eh_%d waking up\n",
1565 * We have a host that is failing for some reason. Figure out
1566 * what we need to do to get it up and online again (if we can).
1567 * If we fail, we end up taking the thing offline.
1569 if (shost->transportt->eh_strategy_handler)
1570 shost->transportt->eh_strategy_handler(shost);
1572 scsi_unjam_host(shost);
1575 * Note - if the above fails completely, the action is to take
1576 * individual devices offline and flush the queue of any
1577 * outstanding requests that may have been pending. When we
1578 * restart, we restart any I/O to any other devices on the bus
1579 * which are still online.
1581 scsi_restart_operations(shost);
1582 set_current_state(TASK_INTERRUPTIBLE);
1584 __set_current_state(TASK_RUNNING);
1586 SCSI_LOG_ERROR_RECOVERY(1,
1587 printk("Error handler scsi_eh_%d exiting\n", shost->host_no));
1588 shost->ehandler = NULL;
1593 * Function: scsi_report_bus_reset()
1595 * Purpose: Utility function used by low-level drivers to report that
1596 * they have observed a bus reset on the bus being handled.
1598 * Arguments: shost - Host in question
1599 * channel - channel on which reset was observed.
1603 * Lock status: Host lock must be held.
1605 * Notes: This only needs to be called if the reset is one which
1606 * originates from an unknown location. Resets originated
1607 * by the mid-level itself don't need to call this, but there
1608 * should be no harm.
1610 * The main purpose of this is to make sure that a CHECK_CONDITION
1611 * is properly treated.
1613 void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
1615 struct scsi_device *sdev;
1617 __shost_for_each_device(sdev, shost) {
1618 if (channel == sdev_channel(sdev)) {
1619 sdev->was_reset = 1;
1620 sdev->expecting_cc_ua = 1;
1624 EXPORT_SYMBOL(scsi_report_bus_reset);
1627 * Function: scsi_report_device_reset()
1629 * Purpose: Utility function used by low-level drivers to report that
1630 * they have observed a device reset on the device being handled.
1632 * Arguments: shost - Host in question
1633 * channel - channel on which reset was observed
1634 * target - target on which reset was observed
1638 * Lock status: Host lock must be held
1640 * Notes: This only needs to be called if the reset is one which
1641 * originates from an unknown location. Resets originated
1642 * by the mid-level itself don't need to call this, but there
1643 * should be no harm.
1645 * The main purpose of this is to make sure that a CHECK_CONDITION
1646 * is properly treated.
1648 void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
1650 struct scsi_device *sdev;
1652 __shost_for_each_device(sdev, shost) {
1653 if (channel == sdev_channel(sdev) &&
1654 target == sdev_id(sdev)) {
1655 sdev->was_reset = 1;
1656 sdev->expecting_cc_ua = 1;
1660 EXPORT_SYMBOL(scsi_report_device_reset);
1663 scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
1668 * Function: scsi_reset_provider
1670 * Purpose: Send requested reset to a bus or device at any phase.
1672 * Arguments: device - device to send reset to
1673 * flag - reset type (see scsi.h)
1675 * Returns: SUCCESS/FAILURE.
1677 * Notes: This is used by the SCSI Generic driver to provide
1678 * Bus/Device reset capability.
1681 scsi_reset_provider(struct scsi_device *dev, int flag)
1683 struct scsi_cmnd *scmd = scsi_get_command(dev, GFP_KERNEL);
1684 struct Scsi_Host *shost = dev->host;
1686 unsigned long flags;
1689 scmd->request = &req;
1690 memset(&scmd->eh_timeout, 0, sizeof(scmd->eh_timeout));
1692 memset(&scmd->cmnd, '\0', sizeof(scmd->cmnd));
1694 scmd->scsi_done = scsi_reset_provider_done_command;
1696 scmd->request_buffer = NULL;
1697 scmd->request_bufflen = 0;
1701 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
1703 init_timer(&scmd->eh_timeout);
1706 * Sometimes the command can get back into the timer chain,
1707 * so use the pid as an identifier.
1711 spin_lock_irqsave(shost->host_lock, flags);
1712 shost->tmf_in_progress = 1;
1713 spin_unlock_irqrestore(shost->host_lock, flags);
1716 case SCSI_TRY_RESET_DEVICE:
1717 rtn = scsi_try_bus_device_reset(scmd);
1721 case SCSI_TRY_RESET_BUS:
1722 rtn = scsi_try_bus_reset(scmd);
1726 case SCSI_TRY_RESET_HOST:
1727 rtn = scsi_try_host_reset(scmd);
1733 spin_lock_irqsave(shost->host_lock, flags);
1734 shost->tmf_in_progress = 0;
1735 spin_unlock_irqrestore(shost->host_lock, flags);
1738 * be sure to wake up anyone who was sleeping or had their queue
1739 * suspended while we performed the TMF.
1741 SCSI_LOG_ERROR_RECOVERY(3,
1742 printk("%s: waking up host to restart after TMF\n",
1745 wake_up(&shost->host_wait);
1747 scsi_run_host_queues(shost);
1749 scsi_next_command(scmd);
1752 EXPORT_SYMBOL(scsi_reset_provider);
1755 * scsi_normalize_sense - normalize main elements from either fixed or
1756 * descriptor sense data format into a common format.
1758 * @sense_buffer: byte array containing sense data returned by device
1759 * @sb_len: number of valid bytes in sense_buffer
1760 * @sshdr: pointer to instance of structure that common
1761 * elements are written to.
1764 * The "main elements" from sense data are: response_code, sense_key,
1765 * asc, ascq and additional_length (only for descriptor format).
1767 * Typically this function can be called after a device has
1768 * responded to a SCSI command with the CHECK_CONDITION status.
1771 * 1 if valid sense data information found, else 0;
1773 int scsi_normalize_sense(const u8 *sense_buffer, int sb_len,
1774 struct scsi_sense_hdr *sshdr)
1776 if (!sense_buffer || !sb_len)
1779 memset(sshdr, 0, sizeof(struct scsi_sense_hdr));
1781 sshdr->response_code = (sense_buffer[0] & 0x7f);
1783 if (!scsi_sense_valid(sshdr))
1786 if (sshdr->response_code >= 0x72) {
1791 sshdr->sense_key = (sense_buffer[1] & 0xf);
1793 sshdr->asc = sense_buffer[2];
1795 sshdr->ascq = sense_buffer[3];
1797 sshdr->additional_length = sense_buffer[7];
1803 sshdr->sense_key = (sense_buffer[2] & 0xf);
1805 sb_len = (sb_len < (sense_buffer[7] + 8)) ?
1806 sb_len : (sense_buffer[7] + 8);
1808 sshdr->asc = sense_buffer[12];
1810 sshdr->ascq = sense_buffer[13];
1816 EXPORT_SYMBOL(scsi_normalize_sense);
1818 int scsi_command_normalize_sense(struct scsi_cmnd *cmd,
1819 struct scsi_sense_hdr *sshdr)
1821 return scsi_normalize_sense(cmd->sense_buffer,
1822 sizeof(cmd->sense_buffer), sshdr);
1824 EXPORT_SYMBOL(scsi_command_normalize_sense);
1827 * scsi_sense_desc_find - search for a given descriptor type in
1828 * descriptor sense data format.
1830 * @sense_buffer: byte array of descriptor format sense data
1831 * @sb_len: number of valid bytes in sense_buffer
1832 * @desc_type: value of descriptor type to find
1833 * (e.g. 0 -> information)
1836 * only valid when sense data is in descriptor format
1839 * pointer to start of (first) descriptor if found else NULL
1841 const u8 * scsi_sense_desc_find(const u8 * sense_buffer, int sb_len,
1844 int add_sen_len, add_len, desc_len, k;
1847 if ((sb_len < 8) || (0 == (add_sen_len = sense_buffer[7])))
1849 if ((sense_buffer[0] < 0x72) || (sense_buffer[0] > 0x73))
1851 add_sen_len = (add_sen_len < (sb_len - 8)) ?
1852 add_sen_len : (sb_len - 8);
1853 descp = &sense_buffer[8];
1854 for (desc_len = 0, k = 0; k < add_sen_len; k += desc_len) {
1856 add_len = (k < (add_sen_len - 1)) ? descp[1]: -1;
1857 desc_len = add_len + 2;
1858 if (descp[0] == desc_type)
1860 if (add_len < 0) // short descriptor ??
1865 EXPORT_SYMBOL(scsi_sense_desc_find);
1868 * scsi_get_sense_info_fld - attempts to get information field from
1869 * sense data (either fixed or descriptor format)
1871 * @sense_buffer: byte array of sense data
1872 * @sb_len: number of valid bytes in sense_buffer
1873 * @info_out: pointer to 64 integer where 8 or 4 byte information
1874 * field will be placed if found.
1877 * 1 if information field found, 0 if not found.
1879 int scsi_get_sense_info_fld(const u8 * sense_buffer, int sb_len,
1888 switch (sense_buffer[0] & 0x7f) {
1891 if (sense_buffer[0] & 0x80) {
1892 *info_out = (sense_buffer[3] << 24) +
1893 (sense_buffer[4] << 16) +
1894 (sense_buffer[5] << 8) + sense_buffer[6];
1900 ucp = scsi_sense_desc_find(sense_buffer, sb_len,
1902 if (ucp && (0xa == ucp[1])) {
1904 for (j = 0; j < 8; ++j) {
1917 EXPORT_SYMBOL(scsi_get_sense_info_fld);