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/kernel.h>
22 #include <linux/freezer.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"
39 #include "scsi_transport_api.h"
41 #define SENSE_TIMEOUT (10*HZ)
44 * These should *probably* be handled by the host itself.
45 * Since it is allowed to sleep, it probably should.
47 #define BUS_RESET_SETTLE_TIME (10)
48 #define HOST_RESET_SETTLE_TIME (10)
50 /* called with shost->host_lock held */
51 void scsi_eh_wakeup(struct Scsi_Host
*shost
)
53 if (shost
->host_busy
== shost
->host_failed
) {
54 wake_up_process(shost
->ehandler
);
55 SCSI_LOG_ERROR_RECOVERY(5,
56 printk("Waking error handler thread\n"));
61 * scsi_schedule_eh - schedule EH for SCSI host
62 * @shost: SCSI host to invoke error handling on.
64 * Schedule SCSI EH without scmd.
66 void scsi_schedule_eh(struct Scsi_Host
*shost
)
70 spin_lock_irqsave(shost
->host_lock
, flags
);
72 if (scsi_host_set_state(shost
, SHOST_RECOVERY
) == 0 ||
73 scsi_host_set_state(shost
, SHOST_CANCEL_RECOVERY
) == 0) {
74 shost
->host_eh_scheduled
++;
75 scsi_eh_wakeup(shost
);
78 spin_unlock_irqrestore(shost
->host_lock
, flags
);
80 EXPORT_SYMBOL_GPL(scsi_schedule_eh
);
83 * scsi_eh_scmd_add - add scsi cmd to error handling.
84 * @scmd: scmd to run eh on.
85 * @eh_flag: optional SCSI_EH flag.
90 int scsi_eh_scmd_add(struct scsi_cmnd
*scmd
, int eh_flag
)
92 struct Scsi_Host
*shost
= scmd
->device
->host
;
99 spin_lock_irqsave(shost
->host_lock
, flags
);
100 if (scsi_host_set_state(shost
, SHOST_RECOVERY
))
101 if (scsi_host_set_state(shost
, SHOST_CANCEL_RECOVERY
))
105 scmd
->eh_eflags
|= eh_flag
;
106 list_add_tail(&scmd
->eh_entry
, &shost
->eh_cmd_q
);
107 shost
->host_failed
++;
108 scsi_eh_wakeup(shost
);
110 spin_unlock_irqrestore(shost
->host_lock
, flags
);
115 * scsi_times_out - Timeout function for normal scsi commands.
116 * @req: request that is timing out.
119 * We do not need to lock this. There is the potential for a race
120 * only in that the normal completion handling might run, but if the
121 * normal completion function determines that the timer has already
122 * fired, then it mustn't do anything.
124 enum blk_eh_timer_return
scsi_times_out(struct request
*req
)
126 struct scsi_cmnd
*scmd
= req
->special
;
127 enum blk_eh_timer_return rtn
= BLK_EH_NOT_HANDLED
;
129 scsi_log_completion(scmd
, TIMEOUT_ERROR
);
131 if (scmd
->device
->host
->transportt
->eh_timed_out
)
132 rtn
= scmd
->device
->host
->transportt
->eh_timed_out(scmd
);
133 else if (scmd
->device
->host
->hostt
->eh_timed_out
)
134 rtn
= scmd
->device
->host
->hostt
->eh_timed_out(scmd
);
136 if (unlikely(rtn
== BLK_EH_NOT_HANDLED
&&
137 !scsi_eh_scmd_add(scmd
, SCSI_EH_CANCEL_CMD
))) {
138 scmd
->result
|= DID_TIME_OUT
<< 16;
139 rtn
= BLK_EH_HANDLED
;
146 * scsi_block_when_processing_errors - Prevent cmds from being queued.
147 * @sdev: Device on which we are performing recovery.
150 * We block until the host is out of error recovery, and then check to
151 * see whether the host or the device is offline.
154 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
156 int scsi_block_when_processing_errors(struct scsi_device
*sdev
)
160 wait_event(sdev
->host
->host_wait
, !scsi_host_in_recovery(sdev
->host
));
162 online
= scsi_device_online(sdev
);
164 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: rtn: %d\n", __func__
,
169 EXPORT_SYMBOL(scsi_block_when_processing_errors
);
171 #ifdef CONFIG_SCSI_LOGGING
173 * scsi_eh_prt_fail_stats - Log info on failures.
174 * @shost: scsi host being recovered.
175 * @work_q: Queue of scsi cmds to process.
177 static inline void scsi_eh_prt_fail_stats(struct Scsi_Host
*shost
,
178 struct list_head
*work_q
)
180 struct scsi_cmnd
*scmd
;
181 struct scsi_device
*sdev
;
182 int total_failures
= 0;
185 int devices_failed
= 0;
187 shost_for_each_device(sdev
, shost
) {
188 list_for_each_entry(scmd
, work_q
, eh_entry
) {
189 if (scmd
->device
== sdev
) {
191 if (scmd
->eh_eflags
& SCSI_EH_CANCEL_CMD
)
198 if (cmd_cancel
|| cmd_failed
) {
199 SCSI_LOG_ERROR_RECOVERY(3,
200 sdev_printk(KERN_INFO
, sdev
,
201 "%s: cmds failed: %d, cancel: %d\n",
202 __func__
, cmd_failed
,
210 SCSI_LOG_ERROR_RECOVERY(2, printk("Total of %d commands on %d"
211 " devices require eh work\n",
212 total_failures
, devices_failed
));
217 * scsi_check_sense - Examine scsi cmd sense
218 * @scmd: Cmd to have sense checked.
221 * SUCCESS or FAILED or NEEDS_RETRY
224 * When a deferred error is detected the current command has
225 * not been executed and needs retrying.
227 static int scsi_check_sense(struct scsi_cmnd
*scmd
)
229 struct scsi_device
*sdev
= scmd
->device
;
230 struct scsi_sense_hdr sshdr
;
232 if (! scsi_command_normalize_sense(scmd
, &sshdr
))
233 return FAILED
; /* no valid sense data */
235 if (scsi_sense_is_deferred(&sshdr
))
238 if (sdev
->scsi_dh_data
&& sdev
->scsi_dh_data
->scsi_dh
&&
239 sdev
->scsi_dh_data
->scsi_dh
->check_sense
) {
242 rc
= sdev
->scsi_dh_data
->scsi_dh
->check_sense(sdev
, &sshdr
);
243 if (rc
!= SCSI_RETURN_NOT_HANDLED
)
245 /* handler does not care. Drop down to default handling */
249 * Previous logic looked for FILEMARK, EOM or ILI which are
250 * mainly associated with tapes and returned SUCCESS.
252 if (sshdr
.response_code
== 0x70) {
254 if (scmd
->sense_buffer
[2] & 0xe0)
258 * descriptor format: look for "stream commands sense data
259 * descriptor" (see SSC-3). Assume single sense data
260 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
262 if ((sshdr
.additional_length
> 3) &&
263 (scmd
->sense_buffer
[8] == 0x4) &&
264 (scmd
->sense_buffer
[11] & 0xe0))
268 switch (sshdr
.sense_key
) {
271 case RECOVERED_ERROR
:
272 return /* soft_error */ SUCCESS
;
274 case ABORTED_COMMAND
:
275 if (sshdr
.asc
== 0x10) /* DIF */
282 * if we are expecting a cc/ua because of a bus reset that we
283 * performed, treat this just as a retry. otherwise this is
284 * information that we should pass up to the upper-level driver
285 * so that we can deal with it there.
287 if (scmd
->device
->expecting_cc_ua
) {
288 scmd
->device
->expecting_cc_ua
= 0;
292 * if the device is in the process of becoming ready, we
295 if ((sshdr
.asc
== 0x04) && (sshdr
.ascq
== 0x01))
298 * if the device is not started, we need to wake
299 * the error handler to start the motor
301 if (scmd
->device
->allow_restart
&&
302 (sshdr
.asc
== 0x04) && (sshdr
.ascq
== 0x02))
305 if (blk_barrier_rq(scmd
->request
))
307 * barrier requests should always retry on UA
308 * otherwise block will get a spurious error
313 * for normal (non barrier) commands, pass the
314 * UA upwards for a determination in the
315 * completion functions
319 /* these three are not supported */
321 case VOLUME_OVERFLOW
:
326 if (sshdr
.asc
== 0x11 || /* UNRECOVERED READ ERR */
327 sshdr
.asc
== 0x13 || /* AMNF DATA FIELD */
328 sshdr
.asc
== 0x14) { /* RECORD NOT FOUND */
334 if (scmd
->device
->retry_hwerror
)
335 return ADD_TO_MLQUEUE
;
339 case ILLEGAL_REQUEST
:
348 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
349 * @scmd: SCSI cmd to examine.
352 * This is *only* called when we are examining the status of commands
353 * queued during error recovery. the main difference here is that we
354 * don't allow for the possibility of retries here, and we are a lot
355 * more restrictive about what we consider acceptable.
357 static int scsi_eh_completed_normally(struct scsi_cmnd
*scmd
)
360 * first check the host byte, to see if there is anything in there
361 * that would indicate what we need to do.
363 if (host_byte(scmd
->result
) == DID_RESET
) {
365 * rats. we are already in the error handler, so we now
366 * get to try and figure out what to do next. if the sense
367 * is valid, we have a pretty good idea of what to do.
368 * if not, we mark it as FAILED.
370 return scsi_check_sense(scmd
);
372 if (host_byte(scmd
->result
) != DID_OK
)
376 * next, check the message byte.
378 if (msg_byte(scmd
->result
) != COMMAND_COMPLETE
)
382 * now, check the status byte to see if this indicates
385 switch (status_byte(scmd
->result
)) {
387 case COMMAND_TERMINATED
:
389 case CHECK_CONDITION
:
390 return scsi_check_sense(scmd
);
392 case INTERMEDIATE_GOOD
:
393 case INTERMEDIATE_C_GOOD
:
395 * who knows? FIXME(eric)
398 case RESERVATION_CONFLICT
:
400 * let issuer deal with this, it could be just fine
412 * scsi_eh_done - Completion function for error handling.
413 * @scmd: Cmd that is done.
415 static void scsi_eh_done(struct scsi_cmnd
*scmd
)
417 struct completion
*eh_action
;
419 SCSI_LOG_ERROR_RECOVERY(3,
420 printk("%s scmd: %p result: %x\n",
421 __func__
, scmd
, scmd
->result
));
423 eh_action
= scmd
->device
->host
->eh_action
;
429 * scsi_try_host_reset - ask host adapter to reset itself
430 * @scmd: SCSI cmd to send hsot reset.
432 static int scsi_try_host_reset(struct scsi_cmnd
*scmd
)
437 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Host RST\n",
440 if (!scmd
->device
->host
->hostt
->eh_host_reset_handler
)
443 rtn
= scmd
->device
->host
->hostt
->eh_host_reset_handler(scmd
);
445 if (rtn
== SUCCESS
) {
446 if (!scmd
->device
->host
->hostt
->skip_settle_delay
)
447 ssleep(HOST_RESET_SETTLE_TIME
);
448 spin_lock_irqsave(scmd
->device
->host
->host_lock
, flags
);
449 scsi_report_bus_reset(scmd
->device
->host
,
451 spin_unlock_irqrestore(scmd
->device
->host
->host_lock
, flags
);
458 * scsi_try_bus_reset - ask host to perform a bus reset
459 * @scmd: SCSI cmd to send bus reset.
461 static int scsi_try_bus_reset(struct scsi_cmnd
*scmd
)
466 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Bus RST\n",
469 if (!scmd
->device
->host
->hostt
->eh_bus_reset_handler
)
472 rtn
= scmd
->device
->host
->hostt
->eh_bus_reset_handler(scmd
);
474 if (rtn
== SUCCESS
) {
475 if (!scmd
->device
->host
->hostt
->skip_settle_delay
)
476 ssleep(BUS_RESET_SETTLE_TIME
);
477 spin_lock_irqsave(scmd
->device
->host
->host_lock
, flags
);
478 scsi_report_bus_reset(scmd
->device
->host
,
480 spin_unlock_irqrestore(scmd
->device
->host
->host_lock
, flags
);
486 static void __scsi_report_device_reset(struct scsi_device
*sdev
, void *data
)
489 sdev
->expecting_cc_ua
= 1;
493 * scsi_try_target_reset - Ask host to perform a target reset
494 * @scmd: SCSI cmd used to send a target reset
497 * There is no timeout for this operation. if this operation is
498 * unreliable for a given host, then the host itself needs to put a
499 * timer on it, and set the host back to a consistent state prior to
502 static int scsi_try_target_reset(struct scsi_cmnd
*scmd
)
507 if (!scmd
->device
->host
->hostt
->eh_target_reset_handler
)
510 rtn
= scmd
->device
->host
->hostt
->eh_target_reset_handler(scmd
);
511 if (rtn
== SUCCESS
) {
512 spin_lock_irqsave(scmd
->device
->host
->host_lock
, flags
);
513 __starget_for_each_device(scsi_target(scmd
->device
), NULL
,
514 __scsi_report_device_reset
);
515 spin_unlock_irqrestore(scmd
->device
->host
->host_lock
, flags
);
522 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
523 * @scmd: SCSI cmd used to send BDR
526 * There is no timeout for this operation. if this operation is
527 * unreliable for a given host, then the host itself needs to put a
528 * timer on it, and set the host back to a consistent state prior to
531 static int scsi_try_bus_device_reset(struct scsi_cmnd
*scmd
)
535 if (!scmd
->device
->host
->hostt
->eh_device_reset_handler
)
538 rtn
= scmd
->device
->host
->hostt
->eh_device_reset_handler(scmd
);
540 __scsi_report_device_reset(scmd
->device
, NULL
);
544 static int __scsi_try_to_abort_cmd(struct scsi_cmnd
*scmd
)
546 if (!scmd
->device
->host
->hostt
->eh_abort_handler
)
549 return scmd
->device
->host
->hostt
->eh_abort_handler(scmd
);
553 * scsi_try_to_abort_cmd - Ask host to abort a running command.
554 * @scmd: SCSI cmd to abort from Lower Level.
557 * This function will not return until the user's completion function
558 * has been called. there is no timeout on this operation. if the
559 * author of the low-level driver wishes this operation to be timed,
560 * they can provide this facility themselves. helper functions in
561 * scsi_error.c can be supplied to make this easier to do.
563 static int scsi_try_to_abort_cmd(struct scsi_cmnd
*scmd
)
566 * scsi_done was called just after the command timed out and before
567 * we had a chance to process it. (db)
569 if (scmd
->serial_number
== 0)
571 return __scsi_try_to_abort_cmd(scmd
);
574 static void scsi_abort_eh_cmnd(struct scsi_cmnd
*scmd
)
576 if (__scsi_try_to_abort_cmd(scmd
) != SUCCESS
)
577 if (scsi_try_bus_device_reset(scmd
) != SUCCESS
)
578 if (scsi_try_target_reset(scmd
) != SUCCESS
)
579 if (scsi_try_bus_reset(scmd
) != SUCCESS
)
580 scsi_try_host_reset(scmd
);
584 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recory
585 * @scmd: SCSI command structure to hijack
586 * @ses: structure to save restore information
587 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
588 * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
589 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
591 * This function is used to save a scsi command information before re-execution
592 * as part of the error recovery process. If @sense_bytes is 0 the command
593 * sent must be one that does not transfer any data. If @sense_bytes != 0
594 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
595 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
597 void scsi_eh_prep_cmnd(struct scsi_cmnd
*scmd
, struct scsi_eh_save
*ses
,
598 unsigned char *cmnd
, int cmnd_size
, unsigned sense_bytes
)
600 struct scsi_device
*sdev
= scmd
->device
;
603 * We need saved copies of a number of fields - this is because
604 * error handling may need to overwrite these with different values
605 * to run different commands, and once error handling is complete,
606 * we will need to restore these values prior to running the actual
609 ses
->cmd_len
= scmd
->cmd_len
;
610 ses
->cmnd
= scmd
->cmnd
;
611 ses
->data_direction
= scmd
->sc_data_direction
;
612 ses
->sdb
= scmd
->sdb
;
613 ses
->next_rq
= scmd
->request
->next_rq
;
614 ses
->result
= scmd
->result
;
615 ses
->underflow
= scmd
->underflow
;
616 ses
->prot_op
= scmd
->prot_op
;
618 scmd
->prot_op
= SCSI_PROT_NORMAL
;
619 scmd
->cmnd
= ses
->eh_cmnd
;
620 memset(scmd
->cmnd
, 0, BLK_MAX_CDB
);
621 memset(&scmd
->sdb
, 0, sizeof(scmd
->sdb
));
622 scmd
->request
->next_rq
= NULL
;
625 scmd
->sdb
.length
= min_t(unsigned, SCSI_SENSE_BUFFERSIZE
,
627 sg_init_one(&ses
->sense_sgl
, scmd
->sense_buffer
,
629 scmd
->sdb
.table
.sgl
= &ses
->sense_sgl
;
630 scmd
->sc_data_direction
= DMA_FROM_DEVICE
;
631 scmd
->sdb
.table
.nents
= 1;
632 scmd
->cmnd
[0] = REQUEST_SENSE
;
633 scmd
->cmnd
[4] = scmd
->sdb
.length
;
634 scmd
->cmd_len
= COMMAND_SIZE(scmd
->cmnd
[0]);
636 scmd
->sc_data_direction
= DMA_NONE
;
638 BUG_ON(cmnd_size
> BLK_MAX_CDB
);
639 memcpy(scmd
->cmnd
, cmnd
, cmnd_size
);
640 scmd
->cmd_len
= COMMAND_SIZE(scmd
->cmnd
[0]);
646 if (sdev
->scsi_level
<= SCSI_2
&& sdev
->scsi_level
!= SCSI_UNKNOWN
)
647 scmd
->cmnd
[1] = (scmd
->cmnd
[1] & 0x1f) |
648 (sdev
->lun
<< 5 & 0xe0);
651 * Zero the sense buffer. The scsi spec mandates that any
652 * untransferred sense data should be interpreted as being zero.
654 memset(scmd
->sense_buffer
, 0, SCSI_SENSE_BUFFERSIZE
);
656 EXPORT_SYMBOL(scsi_eh_prep_cmnd
);
659 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recory
660 * @scmd: SCSI command structure to restore
661 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
663 * Undo any damage done by above scsi_eh_prep_cmnd().
665 void scsi_eh_restore_cmnd(struct scsi_cmnd
* scmd
, struct scsi_eh_save
*ses
)
668 * Restore original data
670 scmd
->cmd_len
= ses
->cmd_len
;
671 scmd
->cmnd
= ses
->cmnd
;
672 scmd
->sc_data_direction
= ses
->data_direction
;
673 scmd
->sdb
= ses
->sdb
;
674 scmd
->request
->next_rq
= ses
->next_rq
;
675 scmd
->result
= ses
->result
;
676 scmd
->underflow
= ses
->underflow
;
677 scmd
->prot_op
= ses
->prot_op
;
679 EXPORT_SYMBOL(scsi_eh_restore_cmnd
);
682 * scsi_send_eh_cmnd - submit a scsi command as part of error recory
683 * @scmd: SCSI command structure to hijack
685 * @cmnd_size: size in bytes of @cmnd
686 * @timeout: timeout for this request
687 * @sense_bytes: size of sense data to copy or 0
689 * This function is used to send a scsi command down to a target device
690 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
693 * SUCCESS or FAILED or NEEDS_RETRY
695 static int scsi_send_eh_cmnd(struct scsi_cmnd
*scmd
, unsigned char *cmnd
,
696 int cmnd_size
, int timeout
, unsigned sense_bytes
)
698 struct scsi_device
*sdev
= scmd
->device
;
699 struct Scsi_Host
*shost
= sdev
->host
;
700 DECLARE_COMPLETION_ONSTACK(done
);
701 unsigned long timeleft
;
703 struct scsi_eh_save ses
;
706 scsi_eh_prep_cmnd(scmd
, &ses
, cmnd
, cmnd_size
, sense_bytes
);
707 shost
->eh_action
= &done
;
709 spin_lock_irqsave(shost
->host_lock
, flags
);
711 shost
->hostt
->queuecommand(scmd
, scsi_eh_done
);
712 spin_unlock_irqrestore(shost
->host_lock
, flags
);
714 timeleft
= wait_for_completion_timeout(&done
, timeout
);
716 shost
->eh_action
= NULL
;
718 scsi_log_completion(scmd
, SUCCESS
);
720 SCSI_LOG_ERROR_RECOVERY(3,
721 printk("%s: scmd: %p, timeleft: %ld\n",
722 __func__
, scmd
, timeleft
));
725 * If there is time left scsi_eh_done got called, and we will
726 * examine the actual status codes to see whether the command
727 * actually did complete normally, else tell the host to forget
728 * about this command.
731 rtn
= scsi_eh_completed_normally(scmd
);
732 SCSI_LOG_ERROR_RECOVERY(3,
733 printk("%s: scsi_eh_completed_normally %x\n",
749 scsi_abort_eh_cmnd(scmd
);
753 scsi_eh_restore_cmnd(scmd
, &ses
);
758 * scsi_request_sense - Request sense data from a particular target.
759 * @scmd: SCSI cmd for request sense.
762 * Some hosts automatically obtain this information, others require
763 * that we obtain it on our own. This function will *not* return until
764 * the command either times out, or it completes.
766 static int scsi_request_sense(struct scsi_cmnd
*scmd
)
768 return scsi_send_eh_cmnd(scmd
, NULL
, 0, SENSE_TIMEOUT
, ~0);
772 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
773 * @scmd: Original SCSI cmd that eh has finished.
774 * @done_q: Queue for processed commands.
777 * We don't want to use the normal command completion while we are are
778 * still handling errors - it may cause other commands to be queued,
779 * and that would disturb what we are doing. Thus we really want to
780 * keep a list of pending commands for final completion, and once we
781 * are ready to leave error handling we handle completion for real.
783 void scsi_eh_finish_cmd(struct scsi_cmnd
*scmd
, struct list_head
*done_q
)
785 scmd
->device
->host
->host_failed
--;
787 list_move_tail(&scmd
->eh_entry
, done_q
);
789 EXPORT_SYMBOL(scsi_eh_finish_cmd
);
792 * scsi_eh_get_sense - Get device sense data.
793 * @work_q: Queue of commands to process.
794 * @done_q: Queue of processed commands.
797 * See if we need to request sense information. if so, then get it
798 * now, so we have a better idea of what to do.
801 * This has the unfortunate side effect that if a shost adapter does
802 * not automatically request sense information, we end up shutting
803 * it down before we request it.
805 * All drivers should request sense information internally these days,
806 * so for now all I have to say is tough noogies if you end up in here.
808 * XXX: Long term this code should go away, but that needs an audit of
811 int scsi_eh_get_sense(struct list_head
*work_q
,
812 struct list_head
*done_q
)
814 struct scsi_cmnd
*scmd
, *next
;
817 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
818 if ((scmd
->eh_eflags
& SCSI_EH_CANCEL_CMD
) ||
819 SCSI_SENSE_VALID(scmd
))
822 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO
, scmd
,
823 "%s: requesting sense\n",
825 rtn
= scsi_request_sense(scmd
);
829 SCSI_LOG_ERROR_RECOVERY(3, printk("sense requested for %p"
830 " result %x\n", scmd
,
832 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense("bh", scmd
));
834 rtn
= scsi_decide_disposition(scmd
);
837 * if the result was normal, then just pass it along to the
841 /* we don't want this command reissued, just
842 * finished with the sense data, so set
843 * retries to the max allowed to ensure it
844 * won't get reissued */
845 scmd
->retries
= scmd
->allowed
;
846 else if (rtn
!= NEEDS_RETRY
)
849 scsi_eh_finish_cmd(scmd
, done_q
);
852 return list_empty(work_q
);
854 EXPORT_SYMBOL_GPL(scsi_eh_get_sense
);
857 * scsi_eh_tur - Send TUR to device.
858 * @scmd: &scsi_cmnd to send TUR
861 * 0 - Device is ready. 1 - Device NOT ready.
863 static int scsi_eh_tur(struct scsi_cmnd
*scmd
)
865 static unsigned char tur_command
[6] = {TEST_UNIT_READY
, 0, 0, 0, 0, 0};
866 int retry_cnt
= 1, rtn
;
869 rtn
= scsi_send_eh_cmnd(scmd
, tur_command
, 6, SENSE_TIMEOUT
, 0);
871 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd %p rtn %x\n",
872 __func__
, scmd
, rtn
));
887 * scsi_eh_abort_cmds - abort pending commands.
888 * @work_q: &list_head for pending commands.
889 * @done_q: &list_head for processed commands.
892 * Try and see whether or not it makes sense to try and abort the
893 * running command. This only works out to be the case if we have one
894 * command that has timed out. If the command simply failed, it makes
895 * no sense to try and abort the command, since as far as the shost
896 * adapter is concerned, it isn't running.
898 static int scsi_eh_abort_cmds(struct list_head
*work_q
,
899 struct list_head
*done_q
)
901 struct scsi_cmnd
*scmd
, *next
;
904 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
905 if (!(scmd
->eh_eflags
& SCSI_EH_CANCEL_CMD
))
907 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting cmd:"
908 "0x%p\n", current
->comm
,
910 rtn
= scsi_try_to_abort_cmd(scmd
);
911 if (rtn
== SUCCESS
) {
912 scmd
->eh_eflags
&= ~SCSI_EH_CANCEL_CMD
;
913 if (!scsi_device_online(scmd
->device
) ||
914 !scsi_eh_tur(scmd
)) {
915 scsi_eh_finish_cmd(scmd
, done_q
);
919 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting"
926 return list_empty(work_q
);
930 * scsi_eh_try_stu - Send START_UNIT to device.
931 * @scmd: &scsi_cmnd to send START_UNIT
934 * 0 - Device is ready. 1 - Device NOT ready.
936 static int scsi_eh_try_stu(struct scsi_cmnd
*scmd
)
938 static unsigned char stu_command
[6] = {START_STOP
, 0, 0, 0, 1, 0};
940 if (scmd
->device
->allow_restart
) {
941 int i
, rtn
= NEEDS_RETRY
;
943 for (i
= 0; rtn
== NEEDS_RETRY
&& i
< 2; i
++)
944 rtn
= scsi_send_eh_cmnd(scmd
, stu_command
, 6, scmd
->device
->request_queue
->rq_timeout
, 0);
954 * scsi_eh_stu - send START_UNIT if needed
955 * @shost: &scsi host being recovered.
956 * @work_q: &list_head for pending commands.
957 * @done_q: &list_head for processed commands.
960 * If commands are failing due to not ready, initializing command required,
961 * try revalidating the device, which will end up sending a start unit.
963 static int scsi_eh_stu(struct Scsi_Host
*shost
,
964 struct list_head
*work_q
,
965 struct list_head
*done_q
)
967 struct scsi_cmnd
*scmd
, *stu_scmd
, *next
;
968 struct scsi_device
*sdev
;
970 shost_for_each_device(sdev
, shost
) {
972 list_for_each_entry(scmd
, work_q
, eh_entry
)
973 if (scmd
->device
== sdev
&& SCSI_SENSE_VALID(scmd
) &&
974 scsi_check_sense(scmd
) == FAILED
) {
982 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending START_UNIT to sdev:"
983 " 0x%p\n", current
->comm
, sdev
));
985 if (!scsi_eh_try_stu(stu_scmd
)) {
986 if (!scsi_device_online(sdev
) ||
987 !scsi_eh_tur(stu_scmd
)) {
988 list_for_each_entry_safe(scmd
, next
,
990 if (scmd
->device
== sdev
)
991 scsi_eh_finish_cmd(scmd
, done_q
);
995 SCSI_LOG_ERROR_RECOVERY(3,
996 printk("%s: START_UNIT failed to sdev:"
997 " 0x%p\n", current
->comm
, sdev
));
1001 return list_empty(work_q
);
1006 * scsi_eh_bus_device_reset - send bdr if needed
1007 * @shost: scsi host being recovered.
1008 * @work_q: &list_head for pending commands.
1009 * @done_q: &list_head for processed commands.
1012 * Try a bus device reset. Still, look to see whether we have multiple
1013 * devices that are jammed or not - if we have multiple devices, it
1014 * makes no sense to try bus_device_reset - we really would need to try
1015 * a bus_reset instead.
1017 static int scsi_eh_bus_device_reset(struct Scsi_Host
*shost
,
1018 struct list_head
*work_q
,
1019 struct list_head
*done_q
)
1021 struct scsi_cmnd
*scmd
, *bdr_scmd
, *next
;
1022 struct scsi_device
*sdev
;
1025 shost_for_each_device(sdev
, shost
) {
1027 list_for_each_entry(scmd
, work_q
, eh_entry
)
1028 if (scmd
->device
== sdev
) {
1036 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BDR sdev:"
1037 " 0x%p\n", current
->comm
,
1039 rtn
= scsi_try_bus_device_reset(bdr_scmd
);
1040 if (rtn
== SUCCESS
) {
1041 if (!scsi_device_online(sdev
) ||
1042 !scsi_eh_tur(bdr_scmd
)) {
1043 list_for_each_entry_safe(scmd
, next
,
1045 if (scmd
->device
== sdev
)
1046 scsi_eh_finish_cmd(scmd
,
1051 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BDR"
1059 return list_empty(work_q
);
1063 * scsi_eh_target_reset - send target reset if needed
1064 * @shost: scsi host being recovered.
1065 * @work_q: &list_head for pending commands.
1066 * @done_q: &list_head for processed commands.
1069 * Try a target reset.
1071 static int scsi_eh_target_reset(struct Scsi_Host
*shost
,
1072 struct list_head
*work_q
,
1073 struct list_head
*done_q
)
1075 struct scsi_cmnd
*scmd
, *tgtr_scmd
, *next
;
1076 unsigned int id
= 0;
1081 list_for_each_entry(scmd
, work_q
, eh_entry
) {
1082 if (id
== scmd_id(scmd
)) {
1088 /* not one exactly equal; find the next highest */
1089 list_for_each_entry(scmd
, work_q
, eh_entry
) {
1090 if (scmd_id(scmd
) > id
&&
1092 scmd_id(tgtr_scmd
) > scmd_id(scmd
)))
1097 /* no more commands, that's it */
1100 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending target reset "
1102 current
->comm
, id
));
1103 rtn
= scsi_try_target_reset(tgtr_scmd
);
1104 if (rtn
== SUCCESS
) {
1105 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1106 if (id
== scmd_id(scmd
))
1107 if (!scsi_device_online(scmd
->device
) ||
1108 !scsi_eh_tur(tgtr_scmd
))
1109 scsi_eh_finish_cmd(scmd
,
1113 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Target reset"
1116 current
->comm
, id
));
1120 return list_empty(work_q
);
1124 * scsi_eh_bus_reset - send a bus reset
1125 * @shost: &scsi host being recovered.
1126 * @work_q: &list_head for pending commands.
1127 * @done_q: &list_head for processed commands.
1129 static int scsi_eh_bus_reset(struct Scsi_Host
*shost
,
1130 struct list_head
*work_q
,
1131 struct list_head
*done_q
)
1133 struct scsi_cmnd
*scmd
, *chan_scmd
, *next
;
1134 unsigned int channel
;
1138 * we really want to loop over the various channels, and do this on
1139 * a channel by channel basis. we should also check to see if any
1140 * of the failed commands are on soft_reset devices, and if so, skip
1144 for (channel
= 0; channel
<= shost
->max_channel
; channel
++) {
1146 list_for_each_entry(scmd
, work_q
, eh_entry
) {
1147 if (channel
== scmd_channel(scmd
)) {
1151 * FIXME add back in some support for
1152 * soft_reset devices.
1159 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BRST chan:"
1160 " %d\n", current
->comm
,
1162 rtn
= scsi_try_bus_reset(chan_scmd
);
1163 if (rtn
== SUCCESS
) {
1164 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1165 if (channel
== scmd_channel(scmd
))
1166 if (!scsi_device_online(scmd
->device
) ||
1168 scsi_eh_finish_cmd(scmd
,
1172 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BRST"
1173 " failed chan: %d\n",
1178 return list_empty(work_q
);
1182 * scsi_eh_host_reset - send a host reset
1183 * @work_q: list_head for processed commands.
1184 * @done_q: list_head for processed commands.
1186 static int scsi_eh_host_reset(struct list_head
*work_q
,
1187 struct list_head
*done_q
)
1189 struct scsi_cmnd
*scmd
, *next
;
1192 if (!list_empty(work_q
)) {
1193 scmd
= list_entry(work_q
->next
,
1194 struct scsi_cmnd
, eh_entry
);
1196 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending HRST\n"
1199 rtn
= scsi_try_host_reset(scmd
);
1200 if (rtn
== SUCCESS
) {
1201 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1202 if (!scsi_device_online(scmd
->device
) ||
1203 (!scsi_eh_try_stu(scmd
) && !scsi_eh_tur(scmd
)) ||
1205 scsi_eh_finish_cmd(scmd
, done_q
);
1208 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: HRST"
1213 return list_empty(work_q
);
1217 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
1218 * @work_q: list_head for processed commands.
1219 * @done_q: list_head for processed commands.
1221 static void scsi_eh_offline_sdevs(struct list_head
*work_q
,
1222 struct list_head
*done_q
)
1224 struct scsi_cmnd
*scmd
, *next
;
1226 list_for_each_entry_safe(scmd
, next
, work_q
, eh_entry
) {
1227 sdev_printk(KERN_INFO
, scmd
->device
, "Device offlined - "
1228 "not ready after error recovery\n");
1229 scsi_device_set_state(scmd
->device
, SDEV_OFFLINE
);
1230 if (scmd
->eh_eflags
& SCSI_EH_CANCEL_CMD
) {
1232 * FIXME: Handle lost cmds.
1235 scsi_eh_finish_cmd(scmd
, done_q
);
1241 * scsi_noretry_cmd - determinte if command should be failed fast
1242 * @scmd: SCSI cmd to examine.
1244 int scsi_noretry_cmd(struct scsi_cmnd
*scmd
)
1246 switch (host_byte(scmd
->result
)) {
1250 return blk_failfast_transport(scmd
->request
);
1252 return blk_failfast_dev(scmd
->request
);
1254 if (msg_byte(scmd
->result
) == COMMAND_COMPLETE
&&
1255 status_byte(scmd
->result
) == RESERVATION_CONFLICT
)
1258 case DID_SOFT_ERROR
:
1259 return blk_failfast_driver(scmd
->request
);
1262 switch (status_byte(scmd
->result
)) {
1263 case CHECK_CONDITION
:
1265 * assume caller has checked sense and determinted
1266 * the check condition was retryable.
1268 return blk_failfast_dev(scmd
->request
);
1275 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1276 * @scmd: SCSI cmd to examine.
1279 * This is *only* called when we are examining the status after sending
1280 * out the actual data command. any commands that are queued for error
1281 * recovery (e.g. test_unit_ready) do *not* come through here.
1283 * When this routine returns failed, it means the error handler thread
1284 * is woken. In cases where the error code indicates an error that
1285 * doesn't require the error handler read (i.e. we don't need to
1286 * abort/reset), this function should return SUCCESS.
1288 int scsi_decide_disposition(struct scsi_cmnd
*scmd
)
1293 * if the device is offline, then we clearly just pass the result back
1294 * up to the top level.
1296 if (!scsi_device_online(scmd
->device
)) {
1297 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: device offline - report"
1304 * first check the host byte, to see if there is anything in there
1305 * that would indicate what we need to do.
1307 switch (host_byte(scmd
->result
)) {
1308 case DID_PASSTHROUGH
:
1310 * no matter what, pass this through to the upper layer.
1311 * nuke this special code so that it looks like we are saying
1314 scmd
->result
&= 0xff00ffff;
1318 * looks good. drop through, and check the next byte.
1321 case DID_NO_CONNECT
:
1322 case DID_BAD_TARGET
:
1325 * note - this means that we just report the status back
1326 * to the top level driver, not that we actually think
1327 * that it indicates SUCCESS.
1331 * when the low level driver returns did_soft_error,
1332 * it is responsible for keeping an internal retry counter
1333 * in order to avoid endless loops (db)
1335 * actually this is a bug in this function here. we should
1336 * be mindful of the maximum number of retries specified
1337 * and not get stuck in a loop.
1339 case DID_SOFT_ERROR
:
1345 return ADD_TO_MLQUEUE
;
1346 case DID_TRANSPORT_DISRUPTED
:
1348 * LLD/transport was disrupted during processing of the IO.
1349 * The transport class is now blocked/blocking,
1350 * and the transport will decide what to do with the IO
1351 * based on its timers and recovery capablilities if
1352 * there are enough retries.
1355 case DID_TRANSPORT_FAILFAST
:
1357 * The transport decided to failfast the IO (most likely
1358 * the fast io fail tmo fired), so send IO directly upwards.
1362 if (msg_byte(scmd
->result
) == COMMAND_COMPLETE
&&
1363 status_byte(scmd
->result
) == RESERVATION_CONFLICT
)
1365 * execute reservation conflict processing code
1376 * when we scan the bus, we get timeout messages for
1377 * these commands if there is no device available.
1378 * other hosts report did_no_connect for the same thing.
1380 if ((scmd
->cmnd
[0] == TEST_UNIT_READY
||
1381 scmd
->cmnd
[0] == INQUIRY
)) {
1393 * next, check the message byte.
1395 if (msg_byte(scmd
->result
) != COMMAND_COMPLETE
)
1399 * check the status byte to see if this indicates anything special.
1401 switch (status_byte(scmd
->result
)) {
1404 * the case of trying to send too many commands to a
1405 * tagged queueing device.
1409 * device can't talk to us at the moment. Should only
1410 * occur (SAM-3) when the task queue is empty, so will cause
1411 * the empty queue handling to trigger a stall in the
1414 return ADD_TO_MLQUEUE
;
1416 case COMMAND_TERMINATED
:
1420 case CHECK_CONDITION
:
1421 rtn
= scsi_check_sense(scmd
);
1422 if (rtn
== NEEDS_RETRY
)
1424 /* if rtn == FAILED, we have no sense information;
1425 * returning FAILED will wake the error handler thread
1426 * to collect the sense and redo the decide
1429 case CONDITION_GOOD
:
1430 case INTERMEDIATE_GOOD
:
1431 case INTERMEDIATE_C_GOOD
:
1434 * who knows? FIXME(eric)
1438 case RESERVATION_CONFLICT
:
1439 sdev_printk(KERN_INFO
, scmd
->device
,
1440 "reservation conflict\n");
1441 return SUCCESS
; /* causes immediate i/o error */
1449 /* we requeue for retry because the error was retryable, and
1450 * the request was not marked fast fail. Note that above,
1451 * even if the request is marked fast fail, we still requeue
1452 * for queue congestion conditions (QUEUE_FULL or BUSY) */
1453 if ((++scmd
->retries
) <= scmd
->allowed
1454 && !scsi_noretry_cmd(scmd
)) {
1458 * no more retries - report this one back to upper level.
1464 static void eh_lock_door_done(struct request
*req
, int uptodate
)
1466 __blk_put_request(req
->q
, req
);
1470 * scsi_eh_lock_door - Prevent medium removal for the specified device
1471 * @sdev: SCSI device to prevent medium removal
1474 * We must be called from process context.
1477 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1478 * head of the devices request queue, and continue.
1480 static void scsi_eh_lock_door(struct scsi_device
*sdev
)
1482 struct request
*req
;
1485 * blk_get_request with GFP_KERNEL (__GFP_WAIT) sleeps until a
1486 * request becomes available
1488 req
= blk_get_request(sdev
->request_queue
, READ
, GFP_KERNEL
);
1490 req
->cmd
[0] = ALLOW_MEDIUM_REMOVAL
;
1494 req
->cmd
[4] = SCSI_REMOVAL_PREVENT
;
1497 req
->cmd_len
= COMMAND_SIZE(req
->cmd
[0]);
1499 req
->cmd_type
= REQ_TYPE_BLOCK_PC
;
1500 req
->cmd_flags
|= REQ_QUIET
;
1501 req
->timeout
= 10 * HZ
;
1504 blk_execute_rq_nowait(req
->q
, NULL
, req
, 1, eh_lock_door_done
);
1508 * scsi_restart_operations - restart io operations to the specified host.
1509 * @shost: Host we are restarting.
1512 * When we entered the error handler, we blocked all further i/o to
1513 * this device. we need to 'reverse' this process.
1515 static void scsi_restart_operations(struct Scsi_Host
*shost
)
1517 struct scsi_device
*sdev
;
1518 unsigned long flags
;
1521 * If the door was locked, we need to insert a door lock request
1522 * onto the head of the SCSI request queue for the device. There
1523 * is no point trying to lock the door of an off-line device.
1525 shost_for_each_device(sdev
, shost
) {
1526 if (scsi_device_online(sdev
) && sdev
->locked
)
1527 scsi_eh_lock_door(sdev
);
1531 * next free up anything directly waiting upon the host. this
1532 * will be requests for character device operations, and also for
1533 * ioctls to queued block devices.
1535 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: waking up host to restart\n",
1538 spin_lock_irqsave(shost
->host_lock
, flags
);
1539 if (scsi_host_set_state(shost
, SHOST_RUNNING
))
1540 if (scsi_host_set_state(shost
, SHOST_CANCEL
))
1541 BUG_ON(scsi_host_set_state(shost
, SHOST_DEL
));
1542 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1544 wake_up(&shost
->host_wait
);
1547 * finally we need to re-initiate requests that may be pending. we will
1548 * have had everything blocked while error handling is taking place, and
1549 * now that error recovery is done, we will need to ensure that these
1550 * requests are started.
1552 scsi_run_host_queues(shost
);
1555 * if eh is active and host_eh_scheduled is pending we need to re-run
1556 * recovery. we do this check after scsi_run_host_queues() to allow
1557 * everything pent up since the last eh run a chance to make forward
1558 * progress before we sync again. Either we'll immediately re-run
1559 * recovery or scsi_device_unbusy() will wake us again when these
1560 * pending commands complete.
1562 spin_lock_irqsave(shost
->host_lock
, flags
);
1563 if (shost
->host_eh_scheduled
)
1564 if (scsi_host_set_state(shost
, SHOST_RECOVERY
))
1565 WARN_ON(scsi_host_set_state(shost
, SHOST_CANCEL_RECOVERY
));
1566 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1570 * scsi_eh_ready_devs - check device ready state and recover if not.
1571 * @shost: host to be recovered.
1572 * @work_q: &list_head for pending commands.
1573 * @done_q: &list_head for processed commands.
1575 void scsi_eh_ready_devs(struct Scsi_Host
*shost
,
1576 struct list_head
*work_q
,
1577 struct list_head
*done_q
)
1579 if (!scsi_eh_stu(shost
, work_q
, done_q
))
1580 if (!scsi_eh_bus_device_reset(shost
, work_q
, done_q
))
1581 if (!scsi_eh_target_reset(shost
, work_q
, done_q
))
1582 if (!scsi_eh_bus_reset(shost
, work_q
, done_q
))
1583 if (!scsi_eh_host_reset(work_q
, done_q
))
1584 scsi_eh_offline_sdevs(work_q
,
1587 EXPORT_SYMBOL_GPL(scsi_eh_ready_devs
);
1590 * scsi_eh_flush_done_q - finish processed commands or retry them.
1591 * @done_q: list_head of processed commands.
1593 void scsi_eh_flush_done_q(struct list_head
*done_q
)
1595 struct scsi_cmnd
*scmd
, *next
;
1597 list_for_each_entry_safe(scmd
, next
, done_q
, eh_entry
) {
1598 list_del_init(&scmd
->eh_entry
);
1599 if (scsi_device_online(scmd
->device
) &&
1600 !scsi_noretry_cmd(scmd
) &&
1601 (++scmd
->retries
<= scmd
->allowed
)) {
1602 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush"
1606 scsi_queue_insert(scmd
, SCSI_MLQUEUE_EH_RETRY
);
1609 * If just we got sense for the device (called
1610 * scsi_eh_get_sense), scmd->result is already
1611 * set, do not set DRIVER_TIMEOUT.
1614 scmd
->result
|= (DRIVER_TIMEOUT
<< 24);
1615 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush finish"
1617 current
->comm
, scmd
));
1618 scsi_finish_command(scmd
);
1622 EXPORT_SYMBOL(scsi_eh_flush_done_q
);
1625 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
1626 * @shost: Host to unjam.
1629 * When we come in here, we *know* that all commands on the bus have
1630 * either completed, failed or timed out. we also know that no further
1631 * commands are being sent to the host, so things are relatively quiet
1632 * and we have freedom to fiddle with things as we wish.
1634 * This is only the *default* implementation. it is possible for
1635 * individual drivers to supply their own version of this function, and
1636 * if the maintainer wishes to do this, it is strongly suggested that
1637 * this function be taken as a template and modified. this function
1638 * was designed to correctly handle problems for about 95% of the
1639 * different cases out there, and it should always provide at least a
1640 * reasonable amount of error recovery.
1642 * Any command marked 'failed' or 'timeout' must eventually have
1643 * scsi_finish_cmd() called for it. we do all of the retry stuff
1644 * here, so when we restart the host after we return it should have an
1647 static void scsi_unjam_host(struct Scsi_Host
*shost
)
1649 unsigned long flags
;
1650 LIST_HEAD(eh_work_q
);
1651 LIST_HEAD(eh_done_q
);
1653 spin_lock_irqsave(shost
->host_lock
, flags
);
1654 list_splice_init(&shost
->eh_cmd_q
, &eh_work_q
);
1655 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1657 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost
, &eh_work_q
));
1659 if (!scsi_eh_get_sense(&eh_work_q
, &eh_done_q
))
1660 if (!scsi_eh_abort_cmds(&eh_work_q
, &eh_done_q
))
1661 scsi_eh_ready_devs(shost
, &eh_work_q
, &eh_done_q
);
1663 scsi_eh_flush_done_q(&eh_done_q
);
1667 * scsi_error_handler - SCSI error handler thread
1668 * @data: Host for which we are running.
1671 * This is the main error handling loop. This is run as a kernel thread
1672 * for every SCSI host and handles all error handling activity.
1674 int scsi_error_handler(void *data
)
1676 struct Scsi_Host
*shost
= data
;
1679 * We use TASK_INTERRUPTIBLE so that the thread is not
1680 * counted against the load average as a running process.
1681 * We never actually get interrupted because kthread_run
1682 * disables signal delivery for the created thread.
1684 set_current_state(TASK_INTERRUPTIBLE
);
1685 while (!kthread_should_stop()) {
1686 if ((shost
->host_failed
== 0 && shost
->host_eh_scheduled
== 0) ||
1687 shost
->host_failed
!= shost
->host_busy
) {
1688 SCSI_LOG_ERROR_RECOVERY(1,
1689 printk("Error handler scsi_eh_%d sleeping\n",
1692 set_current_state(TASK_INTERRUPTIBLE
);
1696 __set_current_state(TASK_RUNNING
);
1697 SCSI_LOG_ERROR_RECOVERY(1,
1698 printk("Error handler scsi_eh_%d waking up\n",
1702 * We have a host that is failing for some reason. Figure out
1703 * what we need to do to get it up and online again (if we can).
1704 * If we fail, we end up taking the thing offline.
1706 if (shost
->transportt
->eh_strategy_handler
)
1707 shost
->transportt
->eh_strategy_handler(shost
);
1709 scsi_unjam_host(shost
);
1712 * Note - if the above fails completely, the action is to take
1713 * individual devices offline and flush the queue of any
1714 * outstanding requests that may have been pending. When we
1715 * restart, we restart any I/O to any other devices on the bus
1716 * which are still online.
1718 scsi_restart_operations(shost
);
1719 set_current_state(TASK_INTERRUPTIBLE
);
1721 __set_current_state(TASK_RUNNING
);
1723 SCSI_LOG_ERROR_RECOVERY(1,
1724 printk("Error handler scsi_eh_%d exiting\n", shost
->host_no
));
1725 shost
->ehandler
= NULL
;
1730 * Function: scsi_report_bus_reset()
1732 * Purpose: Utility function used by low-level drivers to report that
1733 * they have observed a bus reset on the bus being handled.
1735 * Arguments: shost - Host in question
1736 * channel - channel on which reset was observed.
1740 * Lock status: Host lock must be held.
1742 * Notes: This only needs to be called if the reset is one which
1743 * originates from an unknown location. Resets originated
1744 * by the mid-level itself don't need to call this, but there
1745 * should be no harm.
1747 * The main purpose of this is to make sure that a CHECK_CONDITION
1748 * is properly treated.
1750 void scsi_report_bus_reset(struct Scsi_Host
*shost
, int channel
)
1752 struct scsi_device
*sdev
;
1754 __shost_for_each_device(sdev
, shost
) {
1755 if (channel
== sdev_channel(sdev
))
1756 __scsi_report_device_reset(sdev
, NULL
);
1759 EXPORT_SYMBOL(scsi_report_bus_reset
);
1762 * Function: scsi_report_device_reset()
1764 * Purpose: Utility function used by low-level drivers to report that
1765 * they have observed a device reset on the device being handled.
1767 * Arguments: shost - Host in question
1768 * channel - channel on which reset was observed
1769 * target - target on which reset was observed
1773 * Lock status: Host lock must be held
1775 * Notes: This only needs to be called if the reset is one which
1776 * originates from an unknown location. Resets originated
1777 * by the mid-level itself don't need to call this, but there
1778 * should be no harm.
1780 * The main purpose of this is to make sure that a CHECK_CONDITION
1781 * is properly treated.
1783 void scsi_report_device_reset(struct Scsi_Host
*shost
, int channel
, int target
)
1785 struct scsi_device
*sdev
;
1787 __shost_for_each_device(sdev
, shost
) {
1788 if (channel
== sdev_channel(sdev
) &&
1789 target
== sdev_id(sdev
))
1790 __scsi_report_device_reset(sdev
, NULL
);
1793 EXPORT_SYMBOL(scsi_report_device_reset
);
1796 scsi_reset_provider_done_command(struct scsi_cmnd
*scmd
)
1801 * Function: scsi_reset_provider
1803 * Purpose: Send requested reset to a bus or device at any phase.
1805 * Arguments: device - device to send reset to
1806 * flag - reset type (see scsi.h)
1808 * Returns: SUCCESS/FAILURE.
1810 * Notes: This is used by the SCSI Generic driver to provide
1811 * Bus/Device reset capability.
1814 scsi_reset_provider(struct scsi_device
*dev
, int flag
)
1816 struct scsi_cmnd
*scmd
= scsi_get_command(dev
, GFP_KERNEL
);
1817 struct Scsi_Host
*shost
= dev
->host
;
1819 unsigned long flags
;
1822 blk_rq_init(NULL
, &req
);
1823 scmd
->request
= &req
;
1825 scmd
->cmnd
= req
.cmd
;
1827 scmd
->scsi_done
= scsi_reset_provider_done_command
;
1828 memset(&scmd
->sdb
, 0, sizeof(scmd
->sdb
));
1832 scmd
->sc_data_direction
= DMA_BIDIRECTIONAL
;
1834 spin_lock_irqsave(shost
->host_lock
, flags
);
1835 shost
->tmf_in_progress
= 1;
1836 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1839 case SCSI_TRY_RESET_DEVICE
:
1840 rtn
= scsi_try_bus_device_reset(scmd
);
1844 case SCSI_TRY_RESET_TARGET
:
1845 rtn
= scsi_try_target_reset(scmd
);
1849 case SCSI_TRY_RESET_BUS
:
1850 rtn
= scsi_try_bus_reset(scmd
);
1854 case SCSI_TRY_RESET_HOST
:
1855 rtn
= scsi_try_host_reset(scmd
);
1861 spin_lock_irqsave(shost
->host_lock
, flags
);
1862 shost
->tmf_in_progress
= 0;
1863 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1866 * be sure to wake up anyone who was sleeping or had their queue
1867 * suspended while we performed the TMF.
1869 SCSI_LOG_ERROR_RECOVERY(3,
1870 printk("%s: waking up host to restart after TMF\n",
1873 wake_up(&shost
->host_wait
);
1875 scsi_run_host_queues(shost
);
1877 scsi_next_command(scmd
);
1880 EXPORT_SYMBOL(scsi_reset_provider
);
1883 * scsi_normalize_sense - normalize main elements from either fixed or
1884 * descriptor sense data format into a common format.
1886 * @sense_buffer: byte array containing sense data returned by device
1887 * @sb_len: number of valid bytes in sense_buffer
1888 * @sshdr: pointer to instance of structure that common
1889 * elements are written to.
1892 * The "main elements" from sense data are: response_code, sense_key,
1893 * asc, ascq and additional_length (only for descriptor format).
1895 * Typically this function can be called after a device has
1896 * responded to a SCSI command with the CHECK_CONDITION status.
1899 * 1 if valid sense data information found, else 0;
1901 int scsi_normalize_sense(const u8
*sense_buffer
, int sb_len
,
1902 struct scsi_sense_hdr
*sshdr
)
1904 if (!sense_buffer
|| !sb_len
)
1907 memset(sshdr
, 0, sizeof(struct scsi_sense_hdr
));
1909 sshdr
->response_code
= (sense_buffer
[0] & 0x7f);
1911 if (!scsi_sense_valid(sshdr
))
1914 if (sshdr
->response_code
>= 0x72) {
1919 sshdr
->sense_key
= (sense_buffer
[1] & 0xf);
1921 sshdr
->asc
= sense_buffer
[2];
1923 sshdr
->ascq
= sense_buffer
[3];
1925 sshdr
->additional_length
= sense_buffer
[7];
1931 sshdr
->sense_key
= (sense_buffer
[2] & 0xf);
1933 sb_len
= (sb_len
< (sense_buffer
[7] + 8)) ?
1934 sb_len
: (sense_buffer
[7] + 8);
1936 sshdr
->asc
= sense_buffer
[12];
1938 sshdr
->ascq
= sense_buffer
[13];
1944 EXPORT_SYMBOL(scsi_normalize_sense
);
1946 int scsi_command_normalize_sense(struct scsi_cmnd
*cmd
,
1947 struct scsi_sense_hdr
*sshdr
)
1949 return scsi_normalize_sense(cmd
->sense_buffer
,
1950 SCSI_SENSE_BUFFERSIZE
, sshdr
);
1952 EXPORT_SYMBOL(scsi_command_normalize_sense
);
1955 * scsi_sense_desc_find - search for a given descriptor type in descriptor sense data format.
1956 * @sense_buffer: byte array of descriptor format sense data
1957 * @sb_len: number of valid bytes in sense_buffer
1958 * @desc_type: value of descriptor type to find
1959 * (e.g. 0 -> information)
1962 * only valid when sense data is in descriptor format
1965 * pointer to start of (first) descriptor if found else NULL
1967 const u8
* scsi_sense_desc_find(const u8
* sense_buffer
, int sb_len
,
1970 int add_sen_len
, add_len
, desc_len
, k
;
1973 if ((sb_len
< 8) || (0 == (add_sen_len
= sense_buffer
[7])))
1975 if ((sense_buffer
[0] < 0x72) || (sense_buffer
[0] > 0x73))
1977 add_sen_len
= (add_sen_len
< (sb_len
- 8)) ?
1978 add_sen_len
: (sb_len
- 8);
1979 descp
= &sense_buffer
[8];
1980 for (desc_len
= 0, k
= 0; k
< add_sen_len
; k
+= desc_len
) {
1982 add_len
= (k
< (add_sen_len
- 1)) ? descp
[1]: -1;
1983 desc_len
= add_len
+ 2;
1984 if (descp
[0] == desc_type
)
1986 if (add_len
< 0) // short descriptor ??
1991 EXPORT_SYMBOL(scsi_sense_desc_find
);
1994 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
1995 * @sense_buffer: byte array of sense data
1996 * @sb_len: number of valid bytes in sense_buffer
1997 * @info_out: pointer to 64 integer where 8 or 4 byte information
1998 * field will be placed if found.
2001 * 1 if information field found, 0 if not found.
2003 int scsi_get_sense_info_fld(const u8
* sense_buffer
, int sb_len
,
2012 switch (sense_buffer
[0] & 0x7f) {
2015 if (sense_buffer
[0] & 0x80) {
2016 *info_out
= (sense_buffer
[3] << 24) +
2017 (sense_buffer
[4] << 16) +
2018 (sense_buffer
[5] << 8) + sense_buffer
[6];
2024 ucp
= scsi_sense_desc_find(sense_buffer
, sb_len
,
2026 if (ucp
&& (0xa == ucp
[1])) {
2028 for (j
= 0; j
< 8; ++j
) {
2041 EXPORT_SYMBOL(scsi_get_sense_info_fld
);
2044 * scsi_build_sense_buffer - build sense data in a buffer
2045 * @desc: Sense format (non zero == descriptor format,
2046 * 0 == fixed format)
2047 * @buf: Where to build sense data
2049 * @asc: Additional sense code
2050 * @ascq: Additional sense code qualifier
2053 void scsi_build_sense_buffer(int desc
, u8
*buf
, u8 key
, u8 asc
, u8 ascq
)
2056 buf
[0] = 0x72; /* descriptor, current */
2062 buf
[0] = 0x70; /* fixed, current */
2069 EXPORT_SYMBOL(scsi_build_sense_buffer
);