[SCSI] Fix command result state propagation
[deliverable/linux.git] / drivers / scsi / scsi_error.c
CommitLineData
1da177e4
LT
1/*
2 * scsi_error.c Copyright (C) 1997 Eric Youngdale
3 *
4 * SCSI error/timeout handling
5 * Initial versions: Eric Youngdale. Based upon conversations with
0bf8c869 6 * Leonard Zubkoff and David Miller at Linux Expo,
1da177e4
LT
7 * ideas originating from all over the place.
8 *
9 * Restructured scsi_unjam_host and associated functions.
10 * September 04, 2002 Mike Anderson (andmike@us.ibm.com)
11 *
12 * Forward port of Russell King's (rmk@arm.linux.org.uk) changes and
0bf8c869 13 * minor cleanups.
1da177e4
LT
14 * September 30, 2002 Mike Anderson (andmike@us.ibm.com)
15 */
16
17#include <linux/module.h>
18#include <linux/sched.h>
5a0e3ad6 19#include <linux/gfp.h>
1da177e4
LT
20#include <linux/timer.h>
21#include <linux/string.h>
1da177e4 22#include <linux/kernel.h>
83144186 23#include <linux/freezer.h>
c5478def 24#include <linux/kthread.h>
1da177e4
LT
25#include <linux/interrupt.h>
26#include <linux/blkdev.h>
27#include <linux/delay.h>
fc73648a 28#include <linux/jiffies.h>
1da177e4
LT
29
30#include <scsi/scsi.h>
beb40487 31#include <scsi/scsi_cmnd.h>
1da177e4
LT
32#include <scsi/scsi_dbg.h>
33#include <scsi/scsi_device.h>
18a4d0a2 34#include <scsi/scsi_driver.h>
1da177e4 35#include <scsi/scsi_eh.h>
c829c394 36#include <scsi/scsi_transport.h>
1da177e4
LT
37#include <scsi/scsi_host.h>
38#include <scsi/scsi_ioctl.h>
1da177e4
LT
39
40#include "scsi_priv.h"
41#include "scsi_logging.h"
79ee8304 42#include "scsi_transport_api.h"
1da177e4 43
bf816235
KT
44#include <trace/events/scsi.h>
45
14216561
JB
46static void scsi_eh_done(struct scsi_cmnd *scmd);
47
1da177e4
LT
48/*
49 * These should *probably* be handled by the host itself.
50 * Since it is allowed to sleep, it probably should.
51 */
52#define BUS_RESET_SETTLE_TIME (10)
53#define HOST_RESET_SETTLE_TIME (10)
54
3eef6257 55static int scsi_eh_try_stu(struct scsi_cmnd *scmd);
e494f6a7
HR
56static int scsi_try_to_abort_cmd(struct scsi_host_template *,
57 struct scsi_cmnd *);
3eef6257 58
1da177e4
LT
59/* called with shost->host_lock held */
60void scsi_eh_wakeup(struct Scsi_Host *shost)
61{
62 if (shost->host_busy == shost->host_failed) {
bf816235 63 trace_scsi_eh_wakeup(shost);
3ed7a470 64 wake_up_process(shost->ehandler);
1da177e4
LT
65 SCSI_LOG_ERROR_RECOVERY(5,
66 printk("Waking error handler thread\n"));
67 }
68}
f8bbfc24
TH
69
70/**
71 * scsi_schedule_eh - schedule EH for SCSI host
72 * @shost: SCSI host to invoke error handling on.
73 *
74 * Schedule SCSI EH without scmd.
dc8875e1 75 */
f8bbfc24
TH
76void scsi_schedule_eh(struct Scsi_Host *shost)
77{
78 unsigned long flags;
79
80 spin_lock_irqsave(shost->host_lock, flags);
81
82 if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
83 scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
84 shost->host_eh_scheduled++;
85 scsi_eh_wakeup(shost);
86 }
87
88 spin_unlock_irqrestore(shost->host_lock, flags);
89}
90EXPORT_SYMBOL_GPL(scsi_schedule_eh);
1da177e4 91
b4562022
HR
92static int scsi_host_eh_past_deadline(struct Scsi_Host *shost)
93{
bb3b621a 94 if (!shost->last_reset || shost->eh_deadline == -1)
b4562022
HR
95 return 0;
96
76ad3e59
HR
97 /*
98 * 32bit accesses are guaranteed to be atomic
99 * (on all supported architectures), so instead
100 * of using a spinlock we can as well double check
bb3b621a 101 * if eh_deadline has been set to 'off' during the
76ad3e59
HR
102 * time_before call.
103 */
104 if (time_before(jiffies, shost->last_reset + shost->eh_deadline) &&
bb3b621a 105 shost->eh_deadline > -1)
b4562022
HR
106 return 0;
107
108 return 1;
109}
110
e494f6a7
HR
111/**
112 * scmd_eh_abort_handler - Handle command aborts
113 * @work: command to be aborted.
114 */
115void
116scmd_eh_abort_handler(struct work_struct *work)
117{
118 struct scsi_cmnd *scmd =
119 container_of(work, struct scsi_cmnd, abort_work.work);
120 struct scsi_device *sdev = scmd->device;
e494f6a7
HR
121 int rtn;
122
e494f6a7 123 if (scsi_host_eh_past_deadline(sdev->host)) {
e494f6a7
HR
124 SCSI_LOG_ERROR_RECOVERY(3,
125 scmd_printk(KERN_INFO, scmd,
126 "scmd %p eh timeout, not aborting\n",
127 scmd));
128 } else {
e494f6a7
HR
129 SCSI_LOG_ERROR_RECOVERY(3,
130 scmd_printk(KERN_INFO, scmd,
131 "aborting command %p\n", scmd));
132 rtn = scsi_try_to_abort_cmd(sdev->host->hostt, scmd);
133 if (rtn == SUCCESS) {
134 scmd->result |= DID_TIME_OUT << 16;
bb3b621a
RM
135 if (scsi_host_eh_past_deadline(sdev->host)) {
136 SCSI_LOG_ERROR_RECOVERY(3,
137 scmd_printk(KERN_INFO, scmd,
138 "scmd %p eh timeout, "
139 "not retrying aborted "
140 "command\n", scmd));
141 } else if (!scsi_noretry_cmd(scmd) &&
e494f6a7
HR
142 (++scmd->retries <= scmd->allowed)) {
143 SCSI_LOG_ERROR_RECOVERY(3,
144 scmd_printk(KERN_WARNING, scmd,
145 "scmd %p retry "
146 "aborted command\n", scmd));
147 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
bb3b621a 148 return;
e494f6a7
HR
149 } else {
150 SCSI_LOG_ERROR_RECOVERY(3,
151 scmd_printk(KERN_WARNING, scmd,
152 "scmd %p finish "
153 "aborted command\n", scmd));
154 scsi_finish_command(scmd);
bb3b621a 155 return;
e494f6a7 156 }
bb3b621a
RM
157 } else {
158 SCSI_LOG_ERROR_RECOVERY(3,
159 scmd_printk(KERN_INFO, scmd,
160 "scmd %p abort failed, rtn %d\n",
161 scmd, rtn));
e494f6a7 162 }
e494f6a7
HR
163 }
164
165 if (!scsi_eh_scmd_add(scmd, 0)) {
166 SCSI_LOG_ERROR_RECOVERY(3,
167 scmd_printk(KERN_WARNING, scmd,
168 "scmd %p terminate "
169 "aborted command\n", scmd));
170 scmd->result |= DID_TIME_OUT << 16;
171 scsi_finish_command(scmd);
172 }
173}
174
175/**
176 * scsi_abort_command - schedule a command abort
177 * @scmd: scmd to abort.
178 *
179 * We only need to abort commands after a command timeout
180 */
181static int
182scsi_abort_command(struct scsi_cmnd *scmd)
183{
184 struct scsi_device *sdev = scmd->device;
185 struct Scsi_Host *shost = sdev->host;
186 unsigned long flags;
187
188 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
189 /*
190 * Retry after abort failed, escalate to next level.
191 */
192 SCSI_LOG_ERROR_RECOVERY(3,
193 scmd_printk(KERN_INFO, scmd,
194 "scmd %p previous abort failed\n", scmd));
195 cancel_delayed_work(&scmd->abort_work);
196 return FAILED;
197 }
198
199 /*
200 * Do not try a command abort if
201 * SCSI EH has already started.
202 */
203 spin_lock_irqsave(shost->host_lock, flags);
204 if (scsi_host_in_recovery(shost)) {
205 spin_unlock_irqrestore(shost->host_lock, flags);
206 SCSI_LOG_ERROR_RECOVERY(3,
207 scmd_printk(KERN_INFO, scmd,
208 "scmd %p not aborting, host in recovery\n",
209 scmd));
210 return FAILED;
211 }
212
bb3b621a 213 if (shost->eh_deadline != -1 && !shost->last_reset)
e494f6a7
HR
214 shost->last_reset = jiffies;
215 spin_unlock_irqrestore(shost->host_lock, flags);
216
217 scmd->eh_eflags |= SCSI_EH_ABORT_SCHEDULED;
218 SCSI_LOG_ERROR_RECOVERY(3,
219 scmd_printk(KERN_INFO, scmd,
220 "scmd %p abort scheduled\n", scmd));
221 queue_delayed_work(shost->tmf_work_q, &scmd->abort_work, HZ / 100);
222 return SUCCESS;
223}
224
1da177e4
LT
225/**
226 * scsi_eh_scmd_add - add scsi cmd to error handling.
227 * @scmd: scmd to run eh on.
228 * @eh_flag: optional SCSI_EH flag.
229 *
230 * Return value:
231 * 0 on failure.
dc8875e1 232 */
1da177e4
LT
233int scsi_eh_scmd_add(struct scsi_cmnd *scmd, int eh_flag)
234{
235 struct Scsi_Host *shost = scmd->device->host;
236 unsigned long flags;
939647ee 237 int ret = 0;
1da177e4 238
3ed7a470 239 if (!shost->ehandler)
1da177e4
LT
240 return 0;
241
242 spin_lock_irqsave(shost->host_lock, flags);
939647ee
JB
243 if (scsi_host_set_state(shost, SHOST_RECOVERY))
244 if (scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY))
245 goto out_unlock;
1da177e4 246
bb3b621a 247 if (shost->eh_deadline != -1 && !shost->last_reset)
b4562022
HR
248 shost->last_reset = jiffies;
249
939647ee 250 ret = 1;
e494f6a7
HR
251 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED)
252 eh_flag &= ~SCSI_EH_CANCEL_CMD;
3111b0d1 253 scmd->eh_eflags |= eh_flag;
1da177e4 254 list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
1da177e4
LT
255 shost->host_failed++;
256 scsi_eh_wakeup(shost);
939647ee 257 out_unlock:
1da177e4 258 spin_unlock_irqrestore(shost->host_lock, flags);
939647ee 259 return ret;
1da177e4
LT
260}
261
1da177e4
LT
262/**
263 * scsi_times_out - Timeout function for normal scsi commands.
242f9dcb 264 * @req: request that is timing out.
1da177e4
LT
265 *
266 * Notes:
267 * We do not need to lock this. There is the potential for a race
268 * only in that the normal completion handling might run, but if the
269 * normal completion function determines that the timer has already
270 * fired, then it mustn't do anything.
dc8875e1 271 */
242f9dcb 272enum blk_eh_timer_return scsi_times_out(struct request *req)
1da177e4 273{
242f9dcb 274 struct scsi_cmnd *scmd = req->special;
242f9dcb 275 enum blk_eh_timer_return rtn = BLK_EH_NOT_HANDLED;
0bf8c869 276 struct Scsi_Host *host = scmd->device->host;
6c5f8ce1 277
bf816235 278 trace_scsi_dispatch_cmd_timeout(scmd);
1da177e4
LT
279 scsi_log_completion(scmd, TIMEOUT_ERROR);
280
bb3b621a 281 if (host->eh_deadline != -1 && !host->last_reset)
b4562022
HR
282 host->last_reset = jiffies;
283
0bf8c869
JJ
284 if (host->transportt->eh_timed_out)
285 rtn = host->transportt->eh_timed_out(scmd);
286 else if (host->hostt->eh_timed_out)
287 rtn = host->hostt->eh_timed_out(scmd);
1da177e4 288
e494f6a7
HR
289 if (rtn == BLK_EH_NOT_HANDLED && !host->hostt->no_async_abort)
290 if (scsi_abort_command(scmd) == SUCCESS)
291 return BLK_EH_NOT_HANDLED;
292
18a4d0a2
MP
293 scmd->result |= DID_TIME_OUT << 16;
294
fa990781 295 if (unlikely(rtn == BLK_EH_NOT_HANDLED &&
18a4d0a2 296 !scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD)))
fa990781 297 rtn = BLK_EH_HANDLED;
242f9dcb 298
fa990781 299 return rtn;
1da177e4
LT
300}
301
302/**
303 * scsi_block_when_processing_errors - Prevent cmds from being queued.
304 * @sdev: Device on which we are performing recovery.
305 *
306 * Description:
307 * We block until the host is out of error recovery, and then check to
308 * see whether the host or the device is offline.
309 *
310 * Return value:
311 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
dc8875e1 312 */
1da177e4
LT
313int scsi_block_when_processing_errors(struct scsi_device *sdev)
314{
315 int online;
316
939647ee 317 wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
1da177e4
LT
318
319 online = scsi_device_online(sdev);
320
cadbd4a5 321 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: rtn: %d\n", __func__,
1da177e4
LT
322 online));
323
324 return online;
325}
326EXPORT_SYMBOL(scsi_block_when_processing_errors);
327
328#ifdef CONFIG_SCSI_LOGGING
329/**
330 * scsi_eh_prt_fail_stats - Log info on failures.
331 * @shost: scsi host being recovered.
332 * @work_q: Queue of scsi cmds to process.
dc8875e1 333 */
1da177e4
LT
334static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
335 struct list_head *work_q)
336{
337 struct scsi_cmnd *scmd;
338 struct scsi_device *sdev;
339 int total_failures = 0;
340 int cmd_failed = 0;
341 int cmd_cancel = 0;
342 int devices_failed = 0;
343
344 shost_for_each_device(sdev, shost) {
345 list_for_each_entry(scmd, work_q, eh_entry) {
346 if (scmd->device == sdev) {
347 ++total_failures;
3111b0d1 348 if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD)
1da177e4 349 ++cmd_cancel;
0bf8c869 350 else
1da177e4
LT
351 ++cmd_failed;
352 }
353 }
354
355 if (cmd_cancel || cmd_failed) {
356 SCSI_LOG_ERROR_RECOVERY(3,
9ccfc756
JB
357 sdev_printk(KERN_INFO, sdev,
358 "%s: cmds failed: %d, cancel: %d\n",
cadbd4a5 359 __func__, cmd_failed,
9ccfc756 360 cmd_cancel));
1da177e4
LT
361 cmd_cancel = 0;
362 cmd_failed = 0;
363 ++devices_failed;
364 }
365 }
366
367 SCSI_LOG_ERROR_RECOVERY(2, printk("Total of %d commands on %d"
368 " devices require eh work\n",
0bf8c869 369 total_failures, devices_failed));
1da177e4
LT
370}
371#endif
372
279afdfe
EM
373 /**
374 * scsi_report_lun_change - Set flag on all *other* devices on the same target
375 * to indicate that a UNIT ATTENTION is expected.
376 * @sdev: Device reporting the UNIT ATTENTION
377 */
378static void scsi_report_lun_change(struct scsi_device *sdev)
379{
380 sdev->sdev_target->expecting_lun_change = 1;
381}
382
383/**
384 * scsi_report_sense - Examine scsi sense information and log messages for
385 * certain conditions, also issue uevents for some of them.
386 * @sdev: Device reporting the sense code
387 * @sshdr: sshdr to be examined
388 */
389static void scsi_report_sense(struct scsi_device *sdev,
390 struct scsi_sense_hdr *sshdr)
391{
392 enum scsi_device_event evt_type = SDEV_EVT_MAXBITS; /* i.e. none */
393
394 if (sshdr->sense_key == UNIT_ATTENTION) {
395 if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) {
396 evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED;
397 sdev_printk(KERN_WARNING, sdev,
398 "Inquiry data has changed");
399 } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) {
400 evt_type = SDEV_EVT_LUN_CHANGE_REPORTED;
401 scsi_report_lun_change(sdev);
402 sdev_printk(KERN_WARNING, sdev,
403 "Warning! Received an indication that the "
404 "LUN assignments on this target have "
405 "changed. The Linux SCSI layer does not "
406 "automatically remap LUN assignments.\n");
407 } else if (sshdr->asc == 0x3f)
408 sdev_printk(KERN_WARNING, sdev,
409 "Warning! Received an indication that the "
410 "operating parameters on this target have "
411 "changed. The Linux SCSI layer does not "
412 "automatically adjust these parameters.\n");
413
414 if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) {
415 evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED;
416 sdev_printk(KERN_WARNING, sdev,
417 "Warning! Received an indication that the "
418 "LUN reached a thin provisioning soft "
419 "threshold.\n");
420 }
421
422 if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) {
423 evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED;
424 sdev_printk(KERN_WARNING, sdev,
425 "Mode parameters changed");
426 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) {
427 evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED;
428 sdev_printk(KERN_WARNING, sdev,
429 "Capacity data has changed");
430 } else if (sshdr->asc == 0x2a)
431 sdev_printk(KERN_WARNING, sdev,
432 "Parameters changed");
433 }
434
435 if (evt_type != SDEV_EVT_MAXBITS) {
436 set_bit(evt_type, sdev->pending_events);
437 schedule_work(&sdev->event_work);
438 }
439}
440
1da177e4
LT
441/**
442 * scsi_check_sense - Examine scsi cmd sense
443 * @scmd: Cmd to have sense checked.
444 *
445 * Return value:
87f14e65 446 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
1da177e4
LT
447 *
448 * Notes:
449 * When a deferred error is detected the current command has
450 * not been executed and needs retrying.
dc8875e1 451 */
1da177e4
LT
452static int scsi_check_sense(struct scsi_cmnd *scmd)
453{
a6a8d9f8 454 struct scsi_device *sdev = scmd->device;
1da177e4
LT
455 struct scsi_sense_hdr sshdr;
456
457 if (! scsi_command_normalize_sense(scmd, &sshdr))
458 return FAILED; /* no valid sense data */
459
14216561
JB
460 if (scmd->cmnd[0] == TEST_UNIT_READY && scmd->scsi_done != scsi_eh_done)
461 /*
462 * nasty: for mid-layer issued TURs, we need to return the
463 * actual sense data without any recovery attempt. For eh
464 * issued ones, we need to try to recover and interpret
465 */
466 return SUCCESS;
467
279afdfe
EM
468 scsi_report_sense(sdev, &sshdr);
469
1da177e4
LT
470 if (scsi_sense_is_deferred(&sshdr))
471 return NEEDS_RETRY;
472
a6a8d9f8
CS
473 if (sdev->scsi_dh_data && sdev->scsi_dh_data->scsi_dh &&
474 sdev->scsi_dh_data->scsi_dh->check_sense) {
475 int rc;
476
477 rc = sdev->scsi_dh_data->scsi_dh->check_sense(sdev, &sshdr);
478 if (rc != SCSI_RETURN_NOT_HANDLED)
479 return rc;
480 /* handler does not care. Drop down to default handling */
481 }
482
1da177e4
LT
483 /*
484 * Previous logic looked for FILEMARK, EOM or ILI which are
485 * mainly associated with tapes and returned SUCCESS.
486 */
487 if (sshdr.response_code == 0x70) {
488 /* fixed format */
489 if (scmd->sense_buffer[2] & 0xe0)
490 return SUCCESS;
491 } else {
492 /*
493 * descriptor format: look for "stream commands sense data
494 * descriptor" (see SSC-3). Assume single sense data
495 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
496 */
497 if ((sshdr.additional_length > 3) &&
498 (scmd->sense_buffer[8] == 0x4) &&
499 (scmd->sense_buffer[11] & 0xe0))
500 return SUCCESS;
501 }
502
503 switch (sshdr.sense_key) {
504 case NO_SENSE:
505 return SUCCESS;
506 case RECOVERED_ERROR:
507 return /* soft_error */ SUCCESS;
508
509 case ABORTED_COMMAND:
511e44f4
MP
510 if (sshdr.asc == 0x10) /* DIF */
511 return SUCCESS;
512
1da177e4
LT
513 return NEEDS_RETRY;
514 case NOT_READY:
515 case UNIT_ATTENTION:
516 /*
517 * if we are expecting a cc/ua because of a bus reset that we
518 * performed, treat this just as a retry. otherwise this is
519 * information that we should pass up to the upper-level driver
520 * so that we can deal with it there.
521 */
522 if (scmd->device->expecting_cc_ua) {
dfcf7775
TH
523 /*
524 * Because some device does not queue unit
525 * attentions correctly, we carefully check
526 * additional sense code and qualifier so as
527 * not to squash media change unit attention.
528 */
529 if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) {
530 scmd->device->expecting_cc_ua = 0;
531 return NEEDS_RETRY;
532 }
1da177e4 533 }
279afdfe
EM
534 /*
535 * we might also expect a cc/ua if another LUN on the target
536 * reported a UA with an ASC/ASCQ of 3F 0E -
537 * REPORTED LUNS DATA HAS CHANGED.
538 */
539 if (scmd->device->sdev_target->expecting_lun_change &&
540 sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
541 return NEEDS_RETRY;
1da177e4 542 /*
0bf8c869 543 * if the device is in the process of becoming ready, we
1da177e4
LT
544 * should retry.
545 */
546 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
547 return NEEDS_RETRY;
548 /*
549 * if the device is not started, we need to wake
550 * the error handler to start the motor
551 */
552 if (scmd->device->allow_restart &&
553 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
554 return FAILED;
02e031cb
CH
555 /*
556 * Pass the UA upwards for a determination in the completion
557 * functions.
558 */
559 return SUCCESS;
1da177e4 560
63583cca 561 /* these are not supported */
a9d6ceb8
HR
562 case DATA_PROTECT:
563 if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
564 /* Thin provisioning hard threshold reached */
565 set_host_byte(scmd, DID_ALLOC_FAILURE);
566 return SUCCESS;
567 }
1da177e4
LT
568 case COPY_ABORTED:
569 case VOLUME_OVERFLOW:
570 case MISCOMPARE:
63583cca 571 case BLANK_CHECK:
87f14e65
HR
572 set_host_byte(scmd, DID_TARGET_FAILURE);
573 return SUCCESS;
1da177e4
LT
574
575 case MEDIUM_ERROR:
fd1b494d
LT
576 if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
577 sshdr.asc == 0x13 || /* AMNF DATA FIELD */
578 sshdr.asc == 0x14) { /* RECORD NOT FOUND */
7e782af5 579 set_host_byte(scmd, DID_MEDIUM_ERROR);
87f14e65 580 return SUCCESS;
fd1b494d 581 }
1da177e4
LT
582 return NEEDS_RETRY;
583
584 case HARDWARE_ERROR:
585 if (scmd->device->retry_hwerror)
bb0003c1 586 return ADD_TO_MLQUEUE;
1da177e4 587 else
87f14e65 588 set_host_byte(scmd, DID_TARGET_FAILURE);
1da177e4
LT
589
590 case ILLEGAL_REQUEST:
47ac56db
MS
591 if (sshdr.asc == 0x20 || /* Invalid command operation code */
592 sshdr.asc == 0x21 || /* Logical block address out of range */
593 sshdr.asc == 0x24 || /* Invalid field in cdb */
594 sshdr.asc == 0x26) { /* Parameter value invalid */
87f14e65 595 set_host_byte(scmd, DID_TARGET_FAILURE);
47ac56db
MS
596 }
597 return SUCCESS;
598
1da177e4
LT
599 default:
600 return SUCCESS;
601 }
602}
603
4a84067d
VD
604static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
605{
606 struct scsi_host_template *sht = sdev->host->hostt;
607 struct scsi_device *tmp_sdev;
608
609 if (!sht->change_queue_depth ||
610 sdev->queue_depth >= sdev->max_queue_depth)
611 return;
612
613 if (time_before(jiffies,
614 sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
615 return;
616
617 if (time_before(jiffies,
618 sdev->last_queue_full_time + sdev->queue_ramp_up_period))
619 return;
620
621 /*
622 * Walk all devices of a target and do
623 * ramp up on them.
624 */
625 shost_for_each_device(tmp_sdev, sdev->host) {
626 if (tmp_sdev->channel != sdev->channel ||
627 tmp_sdev->id != sdev->id ||
628 tmp_sdev->queue_depth == sdev->max_queue_depth)
629 continue;
630 /*
631 * call back into LLD to increase queue_depth by one
632 * with ramp up reason code.
633 */
634 sht->change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1,
635 SCSI_QDEPTH_RAMP_UP);
636 sdev->last_queue_ramp_up = jiffies;
637 }
638}
639
42a6a918
MC
640static void scsi_handle_queue_full(struct scsi_device *sdev)
641{
642 struct scsi_host_template *sht = sdev->host->hostt;
643 struct scsi_device *tmp_sdev;
644
645 if (!sht->change_queue_depth)
646 return;
647
648 shost_for_each_device(tmp_sdev, sdev->host) {
649 if (tmp_sdev->channel != sdev->channel ||
650 tmp_sdev->id != sdev->id)
651 continue;
652 /*
653 * We do not know the number of commands that were at
654 * the device when we got the queue full so we start
655 * from the highest possible value and work our way down.
656 */
657 sht->change_queue_depth(tmp_sdev, tmp_sdev->queue_depth - 1,
658 SCSI_QDEPTH_QFULL);
659 }
660}
661
1da177e4
LT
662/**
663 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
664 * @scmd: SCSI cmd to examine.
665 *
666 * Notes:
667 * This is *only* called when we are examining the status of commands
668 * queued during error recovery. the main difference here is that we
669 * don't allow for the possibility of retries here, and we are a lot
670 * more restrictive about what we consider acceptable.
dc8875e1 671 */
1da177e4
LT
672static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
673{
674 /*
675 * first check the host byte, to see if there is anything in there
676 * that would indicate what we need to do.
677 */
678 if (host_byte(scmd->result) == DID_RESET) {
679 /*
680 * rats. we are already in the error handler, so we now
681 * get to try and figure out what to do next. if the sense
682 * is valid, we have a pretty good idea of what to do.
683 * if not, we mark it as FAILED.
684 */
685 return scsi_check_sense(scmd);
686 }
687 if (host_byte(scmd->result) != DID_OK)
688 return FAILED;
689
690 /*
691 * next, check the message byte.
692 */
693 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
694 return FAILED;
695
696 /*
697 * now, check the status byte to see if this indicates
698 * anything special.
699 */
700 switch (status_byte(scmd->result)) {
701 case GOOD:
4a84067d 702 scsi_handle_queue_ramp_up(scmd->device);
1da177e4
LT
703 case COMMAND_TERMINATED:
704 return SUCCESS;
705 case CHECK_CONDITION:
706 return scsi_check_sense(scmd);
707 case CONDITION_GOOD:
708 case INTERMEDIATE_GOOD:
709 case INTERMEDIATE_C_GOOD:
710 /*
711 * who knows? FIXME(eric)
712 */
713 return SUCCESS;
5f91bb05 714 case RESERVATION_CONFLICT:
67110dfd
JB
715 if (scmd->cmnd[0] == TEST_UNIT_READY)
716 /* it is a success, we probed the device and
717 * found it */
718 return SUCCESS;
719 /* otherwise, we failed to send the command */
720 return FAILED;
1da177e4 721 case QUEUE_FULL:
42a6a918
MC
722 scsi_handle_queue_full(scmd->device);
723 /* fall through */
724 case BUSY:
3eb3a928 725 return NEEDS_RETRY;
1da177e4
LT
726 default:
727 return FAILED;
728 }
729 return FAILED;
730}
731
1da177e4
LT
732/**
733 * scsi_eh_done - Completion function for error handling.
734 * @scmd: Cmd that is done.
dc8875e1 735 */
1da177e4
LT
736static void scsi_eh_done(struct scsi_cmnd *scmd)
737{
0bf8c869 738 struct completion *eh_action;
85631672 739
7dfdc9a5
CH
740 SCSI_LOG_ERROR_RECOVERY(3,
741 printk("%s scmd: %p result: %x\n",
cadbd4a5 742 __func__, scmd, scmd->result));
85631672
MR
743
744 eh_action = scmd->device->host->eh_action;
745 if (eh_action)
746 complete(eh_action);
1da177e4
LT
747}
748
292148f8
BK
749/**
750 * scsi_try_host_reset - ask host adapter to reset itself
c2b3ebd0 751 * @scmd: SCSI cmd to send host reset.
dc8875e1 752 */
292148f8
BK
753static int scsi_try_host_reset(struct scsi_cmnd *scmd)
754{
755 unsigned long flags;
756 int rtn;
0bf8c869
JJ
757 struct Scsi_Host *host = scmd->device->host;
758 struct scsi_host_template *hostt = host->hostt;
292148f8
BK
759
760 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Host RST\n",
cadbd4a5 761 __func__));
292148f8 762
0bf8c869 763 if (!hostt->eh_host_reset_handler)
292148f8
BK
764 return FAILED;
765
0bf8c869 766 rtn = hostt->eh_host_reset_handler(scmd);
292148f8
BK
767
768 if (rtn == SUCCESS) {
0bf8c869 769 if (!hostt->skip_settle_delay)
292148f8 770 ssleep(HOST_RESET_SETTLE_TIME);
0bf8c869
JJ
771 spin_lock_irqsave(host->host_lock, flags);
772 scsi_report_bus_reset(host, scmd_channel(scmd));
773 spin_unlock_irqrestore(host->host_lock, flags);
292148f8
BK
774 }
775
776 return rtn;
777}
778
779/**
780 * scsi_try_bus_reset - ask host to perform a bus reset
781 * @scmd: SCSI cmd to send bus reset.
dc8875e1 782 */
292148f8
BK
783static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
784{
785 unsigned long flags;
786 int rtn;
0bf8c869
JJ
787 struct Scsi_Host *host = scmd->device->host;
788 struct scsi_host_template *hostt = host->hostt;
292148f8
BK
789
790 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Bus RST\n",
cadbd4a5 791 __func__));
292148f8 792
0bf8c869 793 if (!hostt->eh_bus_reset_handler)
292148f8
BK
794 return FAILED;
795
0bf8c869 796 rtn = hostt->eh_bus_reset_handler(scmd);
292148f8
BK
797
798 if (rtn == SUCCESS) {
0bf8c869 799 if (!hostt->skip_settle_delay)
292148f8 800 ssleep(BUS_RESET_SETTLE_TIME);
0bf8c869
JJ
801 spin_lock_irqsave(host->host_lock, flags);
802 scsi_report_bus_reset(host, scmd_channel(scmd));
803 spin_unlock_irqrestore(host->host_lock, flags);
292148f8
BK
804 }
805
806 return rtn;
807}
808
30bd7df8
MC
809static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
810{
811 sdev->was_reset = 1;
812 sdev->expecting_cc_ua = 1;
813}
814
815/**
816 * scsi_try_target_reset - Ask host to perform a target reset
817 * @scmd: SCSI cmd used to send a target reset
818 *
819 * Notes:
820 * There is no timeout for this operation. if this operation is
821 * unreliable for a given host, then the host itself needs to put a
822 * timer on it, and set the host back to a consistent state prior to
823 * returning.
824 */
825static int scsi_try_target_reset(struct scsi_cmnd *scmd)
826{
827 unsigned long flags;
828 int rtn;
0bf8c869
JJ
829 struct Scsi_Host *host = scmd->device->host;
830 struct scsi_host_template *hostt = host->hostt;
30bd7df8 831
0bf8c869 832 if (!hostt->eh_target_reset_handler)
30bd7df8
MC
833 return FAILED;
834
0bf8c869 835 rtn = hostt->eh_target_reset_handler(scmd);
30bd7df8 836 if (rtn == SUCCESS) {
0bf8c869 837 spin_lock_irqsave(host->host_lock, flags);
30bd7df8
MC
838 __starget_for_each_device(scsi_target(scmd->device), NULL,
839 __scsi_report_device_reset);
0bf8c869 840 spin_unlock_irqrestore(host->host_lock, flags);
30bd7df8
MC
841 }
842
843 return rtn;
844}
845
292148f8
BK
846/**
847 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
848 * @scmd: SCSI cmd used to send BDR
849 *
850 * Notes:
851 * There is no timeout for this operation. if this operation is
852 * unreliable for a given host, then the host itself needs to put a
853 * timer on it, and set the host back to a consistent state prior to
854 * returning.
dc8875e1 855 */
292148f8
BK
856static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
857{
858 int rtn;
0bf8c869 859 struct scsi_host_template *hostt = scmd->device->host->hostt;
292148f8 860
0bf8c869 861 if (!hostt->eh_device_reset_handler)
292148f8
BK
862 return FAILED;
863
0bf8c869 864 rtn = hostt->eh_device_reset_handler(scmd);
30bd7df8
MC
865 if (rtn == SUCCESS)
866 __scsi_report_device_reset(scmd->device, NULL);
292148f8
BK
867 return rtn;
868}
869
0bf8c869 870static int scsi_try_to_abort_cmd(struct scsi_host_template *hostt, struct scsi_cmnd *scmd)
292148f8 871{
0bf8c869 872 if (!hostt->eh_abort_handler)
292148f8
BK
873 return FAILED;
874
0bf8c869 875 return hostt->eh_abort_handler(scmd);
292148f8
BK
876}
877
292148f8
BK
878static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
879{
0bf8c869 880 if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
292148f8 881 if (scsi_try_bus_device_reset(scmd) != SUCCESS)
30bd7df8
MC
882 if (scsi_try_target_reset(scmd) != SUCCESS)
883 if (scsi_try_bus_reset(scmd) != SUCCESS)
884 scsi_try_host_reset(scmd);
292148f8
BK
885}
886
1da177e4 887/**
3b729f76 888 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
2dc611de 889 * @scmd: SCSI command structure to hijack
e1c23468 890 * @ses: structure to save restore information
55db6c1b 891 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
64a87b24 892 * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
55db6c1b 893 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
2dc611de 894 *
e1c23468 895 * This function is used to save a scsi command information before re-execution
55db6c1b
BH
896 * as part of the error recovery process. If @sense_bytes is 0 the command
897 * sent must be one that does not transfer any data. If @sense_bytes != 0
898 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
899 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
dc8875e1 900 */
e1c23468
BH
901void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
902 unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
1da177e4 903{
f59114b7 904 struct scsi_device *sdev = scmd->device;
1da177e4 905
631c228c
CH
906 /*
907 * We need saved copies of a number of fields - this is because
908 * error handling may need to overwrite these with different values
909 * to run different commands, and once error handling is complete,
910 * we will need to restore these values prior to running the actual
911 * command.
912 */
e1c23468 913 ses->cmd_len = scmd->cmd_len;
64a87b24 914 ses->cmnd = scmd->cmnd;
e1c23468 915 ses->data_direction = scmd->sc_data_direction;
30b0c37b 916 ses->sdb = scmd->sdb;
6f9a35e2 917 ses->next_rq = scmd->request->next_rq;
e1c23468 918 ses->result = scmd->result;
12265709 919 ses->underflow = scmd->underflow;
db007fc5 920 ses->prot_op = scmd->prot_op;
631c228c 921
db007fc5 922 scmd->prot_op = SCSI_PROT_NORMAL;
64a87b24
BH
923 scmd->cmnd = ses->eh_cmnd;
924 memset(scmd->cmnd, 0, BLK_MAX_CDB);
30b0c37b 925 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
6f9a35e2 926 scmd->request->next_rq = NULL;
644373a4 927 scmd->result = 0;
30b0c37b 928
55db6c1b 929 if (sense_bytes) {
30b0c37b
BH
930 scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
931 sense_bytes);
e1c23468 932 sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
30b0c37b
BH
933 scmd->sdb.length);
934 scmd->sdb.table.sgl = &ses->sense_sgl;
55db6c1b 935 scmd->sc_data_direction = DMA_FROM_DEVICE;
30b0c37b 936 scmd->sdb.table.nents = 1;
55db6c1b 937 scmd->cmnd[0] = REQUEST_SENSE;
30b0c37b 938 scmd->cmnd[4] = scmd->sdb.length;
55db6c1b 939 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
631c228c 940 } else {
631c228c 941 scmd->sc_data_direction = DMA_NONE;
55db6c1b 942 if (cmnd) {
64a87b24 943 BUG_ON(cmnd_size > BLK_MAX_CDB);
55db6c1b
BH
944 memcpy(scmd->cmnd, cmnd, cmnd_size);
945 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
946 }
631c228c
CH
947 }
948
949 scmd->underflow = 0;
631c228c 950
55db6c1b 951 if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
1da177e4 952 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
f59114b7 953 (sdev->lun << 5 & 0xe0);
1da177e4 954
631c228c
CH
955 /*
956 * Zero the sense buffer. The scsi spec mandates that any
957 * untransferred sense data should be interpreted as being zero.
958 */
b80ca4f7 959 memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
e1c23468
BH
960}
961EXPORT_SYMBOL(scsi_eh_prep_cmnd);
962
963/**
3b729f76 964 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
e1c23468 965 * @scmd: SCSI command structure to restore
477e608c 966 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
e1c23468 967 *
477e608c 968 * Undo any damage done by above scsi_eh_prep_cmnd().
dc8875e1 969 */
e1c23468
BH
970void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
971{
972 /*
973 * Restore original data
974 */
975 scmd->cmd_len = ses->cmd_len;
64a87b24 976 scmd->cmnd = ses->cmnd;
e1c23468 977 scmd->sc_data_direction = ses->data_direction;
30b0c37b 978 scmd->sdb = ses->sdb;
6f9a35e2 979 scmd->request->next_rq = ses->next_rq;
e1c23468 980 scmd->result = ses->result;
12265709 981 scmd->underflow = ses->underflow;
db007fc5 982 scmd->prot_op = ses->prot_op;
e1c23468
BH
983}
984EXPORT_SYMBOL(scsi_eh_restore_cmnd);
631c228c 985
e1c23468 986/**
3b729f76 987 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
e1c23468
BH
988 * @scmd: SCSI command structure to hijack
989 * @cmnd: CDB to send
990 * @cmnd_size: size in bytes of @cmnd
991 * @timeout: timeout for this request
992 * @sense_bytes: size of sense data to copy or 0
993 *
994 * This function is used to send a scsi command down to a target device
995 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
996 *
997 * Return value:
998 * SUCCESS or FAILED or NEEDS_RETRY
dc8875e1 999 */
e1c23468
BH
1000static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
1001 int cmnd_size, int timeout, unsigned sense_bytes)
1002{
1003 struct scsi_device *sdev = scmd->device;
1004 struct Scsi_Host *shost = sdev->host;
1005 DECLARE_COMPLETION_ONSTACK(done);
fc73648a 1006 unsigned long timeleft = timeout;
e1c23468 1007 struct scsi_eh_save ses;
fc73648a 1008 const unsigned long stall_for = msecs_to_jiffies(100);
e1c23468
BH
1009 int rtn;
1010
fc73648a 1011retry:
e1c23468 1012 scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
7dfdc9a5 1013 shost->eh_action = &done;
1da177e4 1014
1da177e4 1015 scsi_log_send(scmd);
f281233d 1016 scmd->scsi_done = scsi_eh_done;
fc73648a
HR
1017 rtn = shost->hostt->queuecommand(shost, scmd);
1018 if (rtn) {
1019 if (timeleft > stall_for) {
1020 scsi_eh_restore_cmnd(scmd, &ses);
1021 timeleft -= stall_for;
1022 msleep(jiffies_to_msecs(stall_for));
1023 goto retry;
1024 }
1025 /* signal not to enter either branch of the if () below */
1026 timeleft = 0;
1027 rtn = NEEDS_RETRY;
1028 } else {
1029 timeleft = wait_for_completion_timeout(&done, timeout);
1030 }
1da177e4 1031
f59114b7 1032 shost->eh_action = NULL;
1da177e4 1033
fc73648a 1034 scsi_log_completion(scmd, rtn);
1da177e4 1035
7dfdc9a5
CH
1036 SCSI_LOG_ERROR_RECOVERY(3,
1037 printk("%s: scmd: %p, timeleft: %ld\n",
cadbd4a5 1038 __func__, scmd, timeleft));
1da177e4
LT
1039
1040 /*
fc73648a
HR
1041 * If there is time left scsi_eh_done got called, and we will examine
1042 * the actual status codes to see whether the command actually did
1043 * complete normally, else if we have a zero return and no time left,
1044 * the command must still be pending, so abort it and return FAILED.
1045 * If we never actually managed to issue the command, because
1046 * ->queuecommand() kept returning non zero, use the rtn = FAILED
1047 * value above (so don't execute either branch of the if)
1da177e4 1048 */
7dfdc9a5 1049 if (timeleft) {
1da177e4
LT
1050 rtn = scsi_eh_completed_normally(scmd);
1051 SCSI_LOG_ERROR_RECOVERY(3,
1052 printk("%s: scsi_eh_completed_normally %x\n",
cadbd4a5 1053 __func__, rtn));
7dfdc9a5 1054
1da177e4
LT
1055 switch (rtn) {
1056 case SUCCESS:
1057 case NEEDS_RETRY:
1058 case FAILED:
1059 break;
6e883b0e
HR
1060 case ADD_TO_MLQUEUE:
1061 rtn = NEEDS_RETRY;
1062 break;
1da177e4
LT
1063 default:
1064 rtn = FAILED;
1065 break;
1066 }
fc73648a 1067 } else if (!rtn) {
292148f8 1068 scsi_abort_eh_cmnd(scmd);
7dfdc9a5 1069 rtn = FAILED;
1da177e4
LT
1070 }
1071
e1c23468 1072 scsi_eh_restore_cmnd(scmd, &ses);
18a4d0a2 1073
1da177e4
LT
1074 return rtn;
1075}
1076
1077/**
1078 * scsi_request_sense - Request sense data from a particular target.
1079 * @scmd: SCSI cmd for request sense.
1080 *
1081 * Notes:
1082 * Some hosts automatically obtain this information, others require
1083 * that we obtain it on our own. This function will *not* return until
1084 * the command either times out, or it completes.
dc8875e1 1085 */
1da177e4
LT
1086static int scsi_request_sense(struct scsi_cmnd *scmd)
1087{
0816c925 1088 return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
1da177e4
LT
1089}
1090
2451079b
JB
1091static int scsi_eh_action(struct scsi_cmnd *scmd, int rtn)
1092{
1093 if (scmd->request->cmd_type != REQ_TYPE_BLOCK_PC) {
1094 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
1095 if (sdrv->eh_action)
1096 rtn = sdrv->eh_action(scmd, rtn);
1097 }
1098 return rtn;
1099}
1100
1da177e4
LT
1101/**
1102 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
1103 * @scmd: Original SCSI cmd that eh has finished.
1104 * @done_q: Queue for processed commands.
1105 *
1106 * Notes:
1107 * We don't want to use the normal command completion while we are are
1108 * still handling errors - it may cause other commands to be queued,
eb44820c 1109 * and that would disturb what we are doing. Thus we really want to
1da177e4
LT
1110 * keep a list of pending commands for final completion, and once we
1111 * are ready to leave error handling we handle completion for real.
dc8875e1 1112 */
041c5fc3 1113void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
1da177e4
LT
1114{
1115 scmd->device->host->host_failed--;
3111b0d1 1116 scmd->eh_eflags = 0;
1da177e4
LT
1117 list_move_tail(&scmd->eh_entry, done_q);
1118}
041c5fc3 1119EXPORT_SYMBOL(scsi_eh_finish_cmd);
1da177e4
LT
1120
1121/**
1122 * scsi_eh_get_sense - Get device sense data.
1123 * @work_q: Queue of commands to process.
eb44820c 1124 * @done_q: Queue of processed commands.
1da177e4
LT
1125 *
1126 * Description:
1127 * See if we need to request sense information. if so, then get it
0bf8c869 1128 * now, so we have a better idea of what to do.
1da177e4
LT
1129 *
1130 * Notes:
1131 * This has the unfortunate side effect that if a shost adapter does
eb44820c 1132 * not automatically request sense information, we end up shutting
1da177e4
LT
1133 * it down before we request it.
1134 *
1135 * All drivers should request sense information internally these days,
1136 * so for now all I have to say is tough noogies if you end up in here.
1137 *
1138 * XXX: Long term this code should go away, but that needs an audit of
1139 * all LLDDs first.
dc8875e1 1140 */
dca84e46
DW
1141int scsi_eh_get_sense(struct list_head *work_q,
1142 struct list_head *done_q)
1da177e4 1143{
937abeaa 1144 struct scsi_cmnd *scmd, *next;
b4562022 1145 struct Scsi_Host *shost;
1da177e4
LT
1146 int rtn;
1147
937abeaa 1148 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
3111b0d1 1149 if ((scmd->eh_eflags & SCSI_EH_CANCEL_CMD) ||
1da177e4
LT
1150 SCSI_SENSE_VALID(scmd))
1151 continue;
1152
b4562022 1153 shost = scmd->device->host;
b4562022 1154 if (scsi_host_eh_past_deadline(shost)) {
b4562022
HR
1155 SCSI_LOG_ERROR_RECOVERY(3,
1156 shost_printk(KERN_INFO, shost,
1157 "skip %s, past eh deadline\n",
1158 __func__));
1159 break;
1160 }
d555a2ab
JB
1161 if (status_byte(scmd->result) != CHECK_CONDITION)
1162 /*
1163 * don't request sense if there's no check condition
1164 * status because the error we're processing isn't one
1165 * that has a sense code (and some devices get
1166 * confused by sense requests out of the blue)
1167 */
1168 continue;
1169
3bf743e7
JG
1170 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
1171 "%s: requesting sense\n",
1172 current->comm));
1da177e4
LT
1173 rtn = scsi_request_sense(scmd);
1174 if (rtn != SUCCESS)
1175 continue;
1176
1177 SCSI_LOG_ERROR_RECOVERY(3, printk("sense requested for %p"
1178 " result %x\n", scmd,
1179 scmd->result));
1180 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense("bh", scmd));
1181
1182 rtn = scsi_decide_disposition(scmd);
1183
1184 /*
1185 * if the result was normal, then just pass it along to the
1186 * upper level.
1187 */
1188 if (rtn == SUCCESS)
1189 /* we don't want this command reissued, just
1190 * finished with the sense data, so set
1191 * retries to the max allowed to ensure it
1192 * won't get reissued */
1193 scmd->retries = scmd->allowed;
1194 else if (rtn != NEEDS_RETRY)
1195 continue;
1196
1197 scsi_eh_finish_cmd(scmd, done_q);
1198 }
1199
1200 return list_empty(work_q);
1201}
dca84e46 1202EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
1da177e4 1203
1da177e4
LT
1204/**
1205 * scsi_eh_tur - Send TUR to device.
eb44820c 1206 * @scmd: &scsi_cmnd to send TUR
1da177e4
LT
1207 *
1208 * Return value:
1209 * 0 - Device is ready. 1 - Device NOT ready.
dc8875e1 1210 */
1da177e4
LT
1211static int scsi_eh_tur(struct scsi_cmnd *scmd)
1212{
1213 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
1214 int retry_cnt = 1, rtn;
1215
1216retry_tur:
0816c925
MP
1217 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
1218 scmd->device->eh_timeout, 0);
1da177e4 1219
1da177e4 1220 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd %p rtn %x\n",
cadbd4a5 1221 __func__, scmd, rtn));
631c228c
CH
1222
1223 switch (rtn) {
1224 case NEEDS_RETRY:
1da177e4
LT
1225 if (retry_cnt--)
1226 goto retry_tur;
631c228c
CH
1227 /*FALLTHRU*/
1228 case SUCCESS:
e47373ec 1229 return 0;
631c228c
CH
1230 default:
1231 return 1;
e47373ec 1232 }
1da177e4
LT
1233}
1234
3eef6257
DJ
1235/**
1236 * scsi_eh_test_devices - check if devices are responding from error recovery.
1237 * @cmd_list: scsi commands in error recovery.
1238 * @work_q: queue for commands which still need more error recovery
1239 * @done_q: queue for commands which are finished
1240 * @try_stu: boolean on if a STU command should be tried in addition to TUR.
1241 *
1242 * Decription:
1243 * Tests if devices are in a working state. Commands to devices now in
1244 * a working state are sent to the done_q while commands to devices which
1245 * are still failing to respond are returned to the work_q for more
1246 * processing.
1247 **/
1248static int scsi_eh_test_devices(struct list_head *cmd_list,
1249 struct list_head *work_q,
1250 struct list_head *done_q, int try_stu)
1251{
1252 struct scsi_cmnd *scmd, *next;
1253 struct scsi_device *sdev;
1254 int finish_cmds;
1255
1256 while (!list_empty(cmd_list)) {
1257 scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
1258 sdev = scmd->device;
1259
b4562022 1260 if (!try_stu) {
b4562022
HR
1261 if (scsi_host_eh_past_deadline(sdev->host)) {
1262 /* Push items back onto work_q */
1263 list_splice_init(cmd_list, work_q);
b4562022
HR
1264 SCSI_LOG_ERROR_RECOVERY(3,
1265 shost_printk(KERN_INFO, sdev->host,
1266 "skip %s, past eh deadline",
1267 __func__));
1268 break;
1269 }
b4562022
HR
1270 }
1271
3eef6257
DJ
1272 finish_cmds = !scsi_device_online(scmd->device) ||
1273 (try_stu && !scsi_eh_try_stu(scmd) &&
1274 !scsi_eh_tur(scmd)) ||
1275 !scsi_eh_tur(scmd);
1276
1277 list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
1278 if (scmd->device == sdev) {
2451079b
JB
1279 if (finish_cmds &&
1280 (try_stu ||
1281 scsi_eh_action(scmd, SUCCESS) == SUCCESS))
3eef6257
DJ
1282 scsi_eh_finish_cmd(scmd, done_q);
1283 else
1284 list_move_tail(&scmd->eh_entry, work_q);
1285 }
1286 }
1287 return list_empty(work_q);
1288}
1289
1290
1da177e4 1291/**
eb44820c
RL
1292 * scsi_eh_abort_cmds - abort pending commands.
1293 * @work_q: &list_head for pending commands.
1294 * @done_q: &list_head for processed commands.
1da177e4
LT
1295 *
1296 * Decription:
1297 * Try and see whether or not it makes sense to try and abort the
eb44820c
RL
1298 * running command. This only works out to be the case if we have one
1299 * command that has timed out. If the command simply failed, it makes
1da177e4
LT
1300 * no sense to try and abort the command, since as far as the shost
1301 * adapter is concerned, it isn't running.
dc8875e1 1302 */
1da177e4
LT
1303static int scsi_eh_abort_cmds(struct list_head *work_q,
1304 struct list_head *done_q)
1305{
937abeaa 1306 struct scsi_cmnd *scmd, *next;
3eef6257 1307 LIST_HEAD(check_list);
1da177e4 1308 int rtn;
b4562022 1309 struct Scsi_Host *shost;
1da177e4 1310
937abeaa 1311 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
3111b0d1 1312 if (!(scmd->eh_eflags & SCSI_EH_CANCEL_CMD))
1da177e4 1313 continue;
b4562022 1314 shost = scmd->device->host;
b4562022 1315 if (scsi_host_eh_past_deadline(shost)) {
b4562022
HR
1316 list_splice_init(&check_list, work_q);
1317 SCSI_LOG_ERROR_RECOVERY(3,
1318 shost_printk(KERN_INFO, shost,
1319 "skip %s, past eh deadline\n",
1320 __func__));
1321 return list_empty(work_q);
1322 }
1da177e4
LT
1323 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting cmd:"
1324 "0x%p\n", current->comm,
1325 scmd));
b4562022 1326 rtn = scsi_try_to_abort_cmd(shost->hostt, scmd);
6fd046f9 1327 if (rtn == FAILED) {
1da177e4
LT
1328 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting"
1329 " cmd failed:"
1330 "0x%p\n",
1331 current->comm,
1332 scmd));
6fd046f9
HR
1333 list_splice_init(&check_list, work_q);
1334 return list_empty(work_q);
1335 }
1336 scmd->eh_eflags &= ~SCSI_EH_CANCEL_CMD;
1337 if (rtn == FAST_IO_FAIL)
1338 scsi_eh_finish_cmd(scmd, done_q);
1339 else
1340 list_move_tail(&scmd->eh_entry, &check_list);
1da177e4
LT
1341 }
1342
3eef6257 1343 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1da177e4
LT
1344}
1345
1da177e4
LT
1346/**
1347 * scsi_eh_try_stu - Send START_UNIT to device.
eb44820c 1348 * @scmd: &scsi_cmnd to send START_UNIT
1da177e4
LT
1349 *
1350 * Return value:
1351 * 0 - Device is ready. 1 - Device NOT ready.
dc8875e1 1352 */
1da177e4
LT
1353static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
1354{
1355 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
1da177e4 1356
631c228c 1357 if (scmd->device->allow_restart) {
ed773e66
BK
1358 int i, rtn = NEEDS_RETRY;
1359
1360 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
9728c081 1361 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->device->request_queue->rq_timeout, 0);
1da177e4 1362
631c228c
CH
1363 if (rtn == SUCCESS)
1364 return 0;
1365 }
1da177e4 1366
1da177e4
LT
1367 return 1;
1368}
1369
1370 /**
1371 * scsi_eh_stu - send START_UNIT if needed
eb44820c
RL
1372 * @shost: &scsi host being recovered.
1373 * @work_q: &list_head for pending commands.
1374 * @done_q: &list_head for processed commands.
1da177e4
LT
1375 *
1376 * Notes:
1377 * If commands are failing due to not ready, initializing command required,
0bf8c869 1378 * try revalidating the device, which will end up sending a start unit.
dc8875e1 1379 */
1da177e4
LT
1380static int scsi_eh_stu(struct Scsi_Host *shost,
1381 struct list_head *work_q,
1382 struct list_head *done_q)
1383{
937abeaa 1384 struct scsi_cmnd *scmd, *stu_scmd, *next;
1da177e4
LT
1385 struct scsi_device *sdev;
1386
1387 shost_for_each_device(sdev, shost) {
b4562022 1388 if (scsi_host_eh_past_deadline(shost)) {
b4562022
HR
1389 SCSI_LOG_ERROR_RECOVERY(3,
1390 shost_printk(KERN_INFO, shost,
1391 "skip %s, past eh deadline\n",
1392 __func__));
1393 break;
1394 }
1da177e4
LT
1395 stu_scmd = NULL;
1396 list_for_each_entry(scmd, work_q, eh_entry)
1397 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
1398 scsi_check_sense(scmd) == FAILED ) {
1399 stu_scmd = scmd;
1400 break;
1401 }
1402
1403 if (!stu_scmd)
1404 continue;
1405
1406 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending START_UNIT to sdev:"
1407 " 0x%p\n", current->comm, sdev));
1408
1409 if (!scsi_eh_try_stu(stu_scmd)) {
1410 if (!scsi_device_online(sdev) ||
1411 !scsi_eh_tur(stu_scmd)) {
937abeaa
CH
1412 list_for_each_entry_safe(scmd, next,
1413 work_q, eh_entry) {
2451079b
JB
1414 if (scmd->device == sdev &&
1415 scsi_eh_action(scmd, SUCCESS) == SUCCESS)
1da177e4
LT
1416 scsi_eh_finish_cmd(scmd, done_q);
1417 }
1418 }
1419 } else {
1420 SCSI_LOG_ERROR_RECOVERY(3,
1421 printk("%s: START_UNIT failed to sdev:"
1422 " 0x%p\n", current->comm, sdev));
1423 }
1424 }
1425
1426 return list_empty(work_q);
1427}
1428
1429
1430/**
1431 * scsi_eh_bus_device_reset - send bdr if needed
1432 * @shost: scsi host being recovered.
eb44820c
RL
1433 * @work_q: &list_head for pending commands.
1434 * @done_q: &list_head for processed commands.
1da177e4
LT
1435 *
1436 * Notes:
eb44820c 1437 * Try a bus device reset. Still, look to see whether we have multiple
1da177e4
LT
1438 * devices that are jammed or not - if we have multiple devices, it
1439 * makes no sense to try bus_device_reset - we really would need to try
0bf8c869 1440 * a bus_reset instead.
dc8875e1 1441 */
1da177e4
LT
1442static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1443 struct list_head *work_q,
1444 struct list_head *done_q)
1445{
937abeaa 1446 struct scsi_cmnd *scmd, *bdr_scmd, *next;
1da177e4
LT
1447 struct scsi_device *sdev;
1448 int rtn;
1449
1450 shost_for_each_device(sdev, shost) {
b4562022 1451 if (scsi_host_eh_past_deadline(shost)) {
b4562022
HR
1452 SCSI_LOG_ERROR_RECOVERY(3,
1453 shost_printk(KERN_INFO, shost,
1454 "skip %s, past eh deadline\n",
1455 __func__));
1456 break;
1457 }
1da177e4
LT
1458 bdr_scmd = NULL;
1459 list_for_each_entry(scmd, work_q, eh_entry)
1460 if (scmd->device == sdev) {
1461 bdr_scmd = scmd;
1462 break;
1463 }
1464
1465 if (!bdr_scmd)
1466 continue;
1467
1468 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BDR sdev:"
1469 " 0x%p\n", current->comm,
1470 sdev));
1471 rtn = scsi_try_bus_device_reset(bdr_scmd);
2f2eb587 1472 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
1da177e4 1473 if (!scsi_device_online(sdev) ||
2f2eb587 1474 rtn == FAST_IO_FAIL ||
1da177e4 1475 !scsi_eh_tur(bdr_scmd)) {
937abeaa
CH
1476 list_for_each_entry_safe(scmd, next,
1477 work_q, eh_entry) {
2451079b
JB
1478 if (scmd->device == sdev &&
1479 scsi_eh_action(scmd, rtn) != FAILED)
1da177e4
LT
1480 scsi_eh_finish_cmd(scmd,
1481 done_q);
1482 }
1483 }
1484 } else {
1485 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BDR"
1486 " failed sdev:"
1487 "0x%p\n",
1488 current->comm,
1489 sdev));
1490 }
1491 }
1492
1493 return list_empty(work_q);
1494}
1495
30bd7df8
MC
1496/**
1497 * scsi_eh_target_reset - send target reset if needed
1498 * @shost: scsi host being recovered.
1499 * @work_q: &list_head for pending commands.
1500 * @done_q: &list_head for processed commands.
1501 *
1502 * Notes:
1503 * Try a target reset.
1504 */
1505static int scsi_eh_target_reset(struct Scsi_Host *shost,
1506 struct list_head *work_q,
1507 struct list_head *done_q)
1508{
98db5195 1509 LIST_HEAD(tmp_list);
3eef6257 1510 LIST_HEAD(check_list);
30bd7df8 1511
98db5195
JB
1512 list_splice_init(work_q, &tmp_list);
1513
1514 while (!list_empty(&tmp_list)) {
1515 struct scsi_cmnd *next, *scmd;
1516 int rtn;
1517 unsigned int id;
b4562022 1518
b4562022 1519 if (scsi_host_eh_past_deadline(shost)) {
b4562022
HR
1520 /* push back on work queue for further processing */
1521 list_splice_init(&check_list, work_q);
1522 list_splice_init(&tmp_list, work_q);
1523 SCSI_LOG_ERROR_RECOVERY(3,
1524 shost_printk(KERN_INFO, shost,
1525 "skip %s, past eh deadline\n",
1526 __func__));
1527 return list_empty(work_q);
1528 }
98db5195
JB
1529
1530 scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
1531 id = scmd_id(scmd);
30bd7df8
MC
1532
1533 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending target reset "
1534 "to target %d\n",
1535 current->comm, id));
98db5195
JB
1536 rtn = scsi_try_target_reset(scmd);
1537 if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
30bd7df8
MC
1538 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Target reset"
1539 " failed target: "
1540 "%d\n",
1541 current->comm, id));
98db5195
JB
1542 list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
1543 if (scmd_id(scmd) != id)
1544 continue;
1545
3eef6257
DJ
1546 if (rtn == SUCCESS)
1547 list_move_tail(&scmd->eh_entry, &check_list);
1548 else if (rtn == FAST_IO_FAIL)
98db5195
JB
1549 scsi_eh_finish_cmd(scmd, done_q);
1550 else
1551 /* push back on work queue for further processing */
1552 list_move(&scmd->eh_entry, work_q);
1553 }
1554 }
30bd7df8 1555
3eef6257 1556 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
30bd7df8
MC
1557}
1558
1da177e4 1559/**
0bf8c869 1560 * scsi_eh_bus_reset - send a bus reset
eb44820c
RL
1561 * @shost: &scsi host being recovered.
1562 * @work_q: &list_head for pending commands.
1563 * @done_q: &list_head for processed commands.
dc8875e1 1564 */
1da177e4
LT
1565static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1566 struct list_head *work_q,
1567 struct list_head *done_q)
1568{
937abeaa 1569 struct scsi_cmnd *scmd, *chan_scmd, *next;
3eef6257 1570 LIST_HEAD(check_list);
1da177e4
LT
1571 unsigned int channel;
1572 int rtn;
1573
1574 /*
1575 * we really want to loop over the various channels, and do this on
1576 * a channel by channel basis. we should also check to see if any
1577 * of the failed commands are on soft_reset devices, and if so, skip
0bf8c869 1578 * the reset.
1da177e4
LT
1579 */
1580
1581 for (channel = 0; channel <= shost->max_channel; channel++) {
b4562022 1582 if (scsi_host_eh_past_deadline(shost)) {
b4562022
HR
1583 list_splice_init(&check_list, work_q);
1584 SCSI_LOG_ERROR_RECOVERY(3,
1585 shost_printk(KERN_INFO, shost,
1586 "skip %s, past eh deadline\n",
1587 __func__));
1588 return list_empty(work_q);
1589 }
b4562022 1590
1da177e4
LT
1591 chan_scmd = NULL;
1592 list_for_each_entry(scmd, work_q, eh_entry) {
422c0d61 1593 if (channel == scmd_channel(scmd)) {
1da177e4
LT
1594 chan_scmd = scmd;
1595 break;
1596 /*
1597 * FIXME add back in some support for
1598 * soft_reset devices.
1599 */
1600 }
1601 }
1602
1603 if (!chan_scmd)
1604 continue;
1605 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BRST chan:"
1606 " %d\n", current->comm,
1607 channel));
1608 rtn = scsi_try_bus_reset(chan_scmd);
2f2eb587 1609 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
937abeaa 1610 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
3eef6257
DJ
1611 if (channel == scmd_channel(scmd)) {
1612 if (rtn == FAST_IO_FAIL)
1da177e4
LT
1613 scsi_eh_finish_cmd(scmd,
1614 done_q);
3eef6257
DJ
1615 else
1616 list_move_tail(&scmd->eh_entry,
1617 &check_list);
1618 }
1da177e4
LT
1619 }
1620 } else {
1621 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BRST"
1622 " failed chan: %d\n",
1623 current->comm,
1624 channel));
1625 }
1626 }
3eef6257 1627 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1da177e4
LT
1628}
1629
1630/**
0bf8c869 1631 * scsi_eh_host_reset - send a host reset
1da177e4
LT
1632 * @work_q: list_head for processed commands.
1633 * @done_q: list_head for processed commands.
dc8875e1 1634 */
1da177e4
LT
1635static int scsi_eh_host_reset(struct list_head *work_q,
1636 struct list_head *done_q)
1637{
937abeaa 1638 struct scsi_cmnd *scmd, *next;
3eef6257 1639 LIST_HEAD(check_list);
1da177e4 1640 int rtn;
1da177e4
LT
1641
1642 if (!list_empty(work_q)) {
1643 scmd = list_entry(work_q->next,
1644 struct scsi_cmnd, eh_entry);
1645
1646 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending HRST\n"
1647 , current->comm));
1648
1649 rtn = scsi_try_host_reset(scmd);
3eef6257
DJ
1650 if (rtn == SUCCESS) {
1651 list_splice_init(work_q, &check_list);
1652 } else if (rtn == FAST_IO_FAIL) {
937abeaa 1653 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1da177e4
LT
1654 scsi_eh_finish_cmd(scmd, done_q);
1655 }
1656 } else {
1657 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: HRST"
1658 " failed\n",
1659 current->comm));
1660 }
1661 }
3eef6257 1662 return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
1da177e4
LT
1663}
1664
1665/**
1666 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
1667 * @work_q: list_head for processed commands.
1668 * @done_q: list_head for processed commands.
dc8875e1 1669 */
1da177e4
LT
1670static void scsi_eh_offline_sdevs(struct list_head *work_q,
1671 struct list_head *done_q)
1672{
937abeaa 1673 struct scsi_cmnd *scmd, *next;
1da177e4 1674
937abeaa 1675 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
31765d7d
MW
1676 sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
1677 "not ready after error recovery\n");
1da177e4 1678 scsi_device_set_state(scmd->device, SDEV_OFFLINE);
3111b0d1 1679 if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD) {
1da177e4
LT
1680 /*
1681 * FIXME: Handle lost cmds.
1682 */
1683 }
1684 scsi_eh_finish_cmd(scmd, done_q);
1685 }
1686 return;
1687}
1688
4a27446f 1689/**
e494f6a7 1690 * scsi_noretry_cmd - determine if command should be failed fast
4a27446f
MC
1691 * @scmd: SCSI cmd to examine.
1692 */
1693int scsi_noretry_cmd(struct scsi_cmnd *scmd)
1694{
1695 switch (host_byte(scmd->result)) {
1696 case DID_OK:
1697 break;
e494f6a7
HR
1698 case DID_TIME_OUT:
1699 goto check_type;
4a27446f 1700 case DID_BUS_BUSY:
33659ebb 1701 return (scmd->request->cmd_flags & REQ_FAILFAST_TRANSPORT);
4a27446f 1702 case DID_PARITY:
33659ebb 1703 return (scmd->request->cmd_flags & REQ_FAILFAST_DEV);
4a27446f
MC
1704 case DID_ERROR:
1705 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1706 status_byte(scmd->result) == RESERVATION_CONFLICT)
1707 return 0;
1708 /* fall through */
1709 case DID_SOFT_ERROR:
33659ebb 1710 return (scmd->request->cmd_flags & REQ_FAILFAST_DRIVER);
4a27446f
MC
1711 }
1712
e494f6a7
HR
1713 if (status_byte(scmd->result) != CHECK_CONDITION)
1714 return 0;
4a27446f 1715
e494f6a7
HR
1716check_type:
1717 /*
1718 * assume caller has checked sense and determined
1719 * the check condition was retryable.
1720 */
1721 if (scmd->request->cmd_flags & REQ_FAILFAST_DEV ||
1722 scmd->request->cmd_type == REQ_TYPE_BLOCK_PC)
1723 return 1;
1724 else
1725 return 0;
4a27446f
MC
1726}
1727
1da177e4
LT
1728/**
1729 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1730 * @scmd: SCSI cmd to examine.
1731 *
1732 * Notes:
1733 * This is *only* called when we are examining the status after sending
1734 * out the actual data command. any commands that are queued for error
1735 * recovery (e.g. test_unit_ready) do *not* come through here.
1736 *
1737 * When this routine returns failed, it means the error handler thread
1738 * is woken. In cases where the error code indicates an error that
1739 * doesn't require the error handler read (i.e. we don't need to
1740 * abort/reset), this function should return SUCCESS.
dc8875e1 1741 */
1da177e4
LT
1742int scsi_decide_disposition(struct scsi_cmnd *scmd)
1743{
1744 int rtn;
1745
1746 /*
1747 * if the device is offline, then we clearly just pass the result back
1748 * up to the top level.
1749 */
1750 if (!scsi_device_online(scmd->device)) {
1751 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: device offline - report"
1752 " as SUCCESS\n",
cadbd4a5 1753 __func__));
1da177e4
LT
1754 return SUCCESS;
1755 }
1756
1757 /*
1758 * first check the host byte, to see if there is anything in there
1759 * that would indicate what we need to do.
1760 */
1761 switch (host_byte(scmd->result)) {
1762 case DID_PASSTHROUGH:
1763 /*
1764 * no matter what, pass this through to the upper layer.
1765 * nuke this special code so that it looks like we are saying
1766 * did_ok.
1767 */
1768 scmd->result &= 0xff00ffff;
1769 return SUCCESS;
1770 case DID_OK:
1771 /*
1772 * looks good. drop through, and check the next byte.
1773 */
1774 break;
e494f6a7
HR
1775 case DID_ABORT:
1776 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
1777 scmd->result |= DID_TIME_OUT << 16;
1778 return SUCCESS;
1779 }
1da177e4
LT
1780 case DID_NO_CONNECT:
1781 case DID_BAD_TARGET:
1da177e4
LT
1782 /*
1783 * note - this means that we just report the status back
1784 * to the top level driver, not that we actually think
1785 * that it indicates SUCCESS.
1786 */
1787 return SUCCESS;
1788 /*
1789 * when the low level driver returns did_soft_error,
0bf8c869 1790 * it is responsible for keeping an internal retry counter
1da177e4
LT
1791 * in order to avoid endless loops (db)
1792 *
1793 * actually this is a bug in this function here. we should
1794 * be mindful of the maximum number of retries specified
1795 * and not get stuck in a loop.
1796 */
1797 case DID_SOFT_ERROR:
1798 goto maybe_retry;
1799 case DID_IMM_RETRY:
1800 return NEEDS_RETRY;
1801
bf341919 1802 case DID_REQUEUE:
1803 return ADD_TO_MLQUEUE;
a4dfaa6f
MC
1804 case DID_TRANSPORT_DISRUPTED:
1805 /*
1806 * LLD/transport was disrupted during processing of the IO.
1807 * The transport class is now blocked/blocking,
1808 * and the transport will decide what to do with the IO
939c2288
MC
1809 * based on its timers and recovery capablilities if
1810 * there are enough retries.
a4dfaa6f 1811 */
939c2288 1812 goto maybe_retry;
a4dfaa6f
MC
1813 case DID_TRANSPORT_FAILFAST:
1814 /*
1815 * The transport decided to failfast the IO (most likely
1816 * the fast io fail tmo fired), so send IO directly upwards.
1817 */
1818 return SUCCESS;
1da177e4
LT
1819 case DID_ERROR:
1820 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1821 status_byte(scmd->result) == RESERVATION_CONFLICT)
1822 /*
1823 * execute reservation conflict processing code
1824 * lower down
1825 */
1826 break;
1827 /* fallthrough */
1da177e4
LT
1828 case DID_BUS_BUSY:
1829 case DID_PARITY:
1830 goto maybe_retry;
1831 case DID_TIME_OUT:
1832 /*
1833 * when we scan the bus, we get timeout messages for
1834 * these commands if there is no device available.
1835 * other hosts report did_no_connect for the same thing.
1836 */
1837 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1838 scmd->cmnd[0] == INQUIRY)) {
1839 return SUCCESS;
1840 } else {
1841 return FAILED;
1842 }
1843 case DID_RESET:
1844 return SUCCESS;
1845 default:
1846 return FAILED;
1847 }
1848
1849 /*
1850 * next, check the message byte.
1851 */
1852 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
1853 return FAILED;
1854
1855 /*
1856 * check the status byte to see if this indicates anything special.
1857 */
1858 switch (status_byte(scmd->result)) {
1859 case QUEUE_FULL:
42a6a918 1860 scsi_handle_queue_full(scmd->device);
1da177e4
LT
1861 /*
1862 * the case of trying to send too many commands to a
1863 * tagged queueing device.
1864 */
1865 case BUSY:
1866 /*
1867 * device can't talk to us at the moment. Should only
1868 * occur (SAM-3) when the task queue is empty, so will cause
1869 * the empty queue handling to trigger a stall in the
1870 * device.
1871 */
1872 return ADD_TO_MLQUEUE;
1873 case GOOD:
279afdfe
EM
1874 if (scmd->cmnd[0] == REPORT_LUNS)
1875 scmd->device->sdev_target->expecting_lun_change = 0;
4a84067d 1876 scsi_handle_queue_ramp_up(scmd->device);
1da177e4 1877 case COMMAND_TERMINATED:
1da177e4 1878 return SUCCESS;
a9b589d9
VB
1879 case TASK_ABORTED:
1880 goto maybe_retry;
1da177e4
LT
1881 case CHECK_CONDITION:
1882 rtn = scsi_check_sense(scmd);
1883 if (rtn == NEEDS_RETRY)
1884 goto maybe_retry;
1885 /* if rtn == FAILED, we have no sense information;
1886 * returning FAILED will wake the error handler thread
1887 * to collect the sense and redo the decide
1888 * disposition */
1889 return rtn;
1890 case CONDITION_GOOD:
1891 case INTERMEDIATE_GOOD:
1892 case INTERMEDIATE_C_GOOD:
1893 case ACA_ACTIVE:
1894 /*
1895 * who knows? FIXME(eric)
1896 */
1897 return SUCCESS;
1898
1899 case RESERVATION_CONFLICT:
9ccfc756
JB
1900 sdev_printk(KERN_INFO, scmd->device,
1901 "reservation conflict\n");
2082ebc4 1902 set_host_byte(scmd, DID_NEXUS_FAILURE);
1da177e4
LT
1903 return SUCCESS; /* causes immediate i/o error */
1904 default:
1905 return FAILED;
1906 }
1907 return FAILED;
1908
1909 maybe_retry:
1910
1911 /* we requeue for retry because the error was retryable, and
1912 * the request was not marked fast fail. Note that above,
1913 * even if the request is marked fast fail, we still requeue
1914 * for queue congestion conditions (QUEUE_FULL or BUSY) */
8884efab 1915 if ((++scmd->retries) <= scmd->allowed
4a27446f 1916 && !scsi_noretry_cmd(scmd)) {
1da177e4
LT
1917 return NEEDS_RETRY;
1918 } else {
1919 /*
1920 * no more retries - report this one back to upper level.
1921 */
1922 return SUCCESS;
1923 }
1924}
1925
f078727b
FT
1926static void eh_lock_door_done(struct request *req, int uptodate)
1927{
1928 __blk_put_request(req->q, req);
1929}
1930
1da177e4
LT
1931/**
1932 * scsi_eh_lock_door - Prevent medium removal for the specified device
1933 * @sdev: SCSI device to prevent medium removal
1934 *
1935 * Locking:
91bc31fb 1936 * We must be called from process context.
1da177e4
LT
1937 *
1938 * Notes:
1939 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1940 * head of the devices request queue, and continue.
dc8875e1 1941 */
1da177e4
LT
1942static void scsi_eh_lock_door(struct scsi_device *sdev)
1943{
f078727b 1944 struct request *req;
1da177e4 1945
91bc31fb
JB
1946 /*
1947 * blk_get_request with GFP_KERNEL (__GFP_WAIT) sleeps until a
1948 * request becomes available
1949 */
f078727b 1950 req = blk_get_request(sdev->request_queue, READ, GFP_KERNEL);
1da177e4 1951
f078727b
FT
1952 req->cmd[0] = ALLOW_MEDIUM_REMOVAL;
1953 req->cmd[1] = 0;
1954 req->cmd[2] = 0;
1955 req->cmd[3] = 0;
1956 req->cmd[4] = SCSI_REMOVAL_PREVENT;
1957 req->cmd[5] = 0;
1da177e4 1958
f078727b
FT
1959 req->cmd_len = COMMAND_SIZE(req->cmd[0]);
1960
1961 req->cmd_type = REQ_TYPE_BLOCK_PC;
1962 req->cmd_flags |= REQ_QUIET;
1963 req->timeout = 10 * HZ;
1964 req->retries = 5;
1965
1966 blk_execute_rq_nowait(req->q, NULL, req, 1, eh_lock_door_done);
1967}
1da177e4
LT
1968
1969/**
1970 * scsi_restart_operations - restart io operations to the specified host.
1971 * @shost: Host we are restarting.
1972 *
1973 * Notes:
1974 * When we entered the error handler, we blocked all further i/o to
1975 * this device. we need to 'reverse' this process.
dc8875e1 1976 */
1da177e4
LT
1977static void scsi_restart_operations(struct Scsi_Host *shost)
1978{
1979 struct scsi_device *sdev;
939647ee 1980 unsigned long flags;
1da177e4
LT
1981
1982 /*
1983 * If the door was locked, we need to insert a door lock request
1984 * onto the head of the SCSI request queue for the device. There
1985 * is no point trying to lock the door of an off-line device.
1986 */
1987 shost_for_each_device(sdev, shost) {
1988 if (scsi_device_online(sdev) && sdev->locked)
1989 scsi_eh_lock_door(sdev);
1990 }
1991
1992 /*
1993 * next free up anything directly waiting upon the host. this
1994 * will be requests for character device operations, and also for
1995 * ioctls to queued block devices.
1996 */
b4562022
HR
1997 SCSI_LOG_ERROR_RECOVERY(3,
1998 printk("scsi_eh_%d waking up host to restart\n",
1999 shost->host_no));
1da177e4 2000
939647ee
JB
2001 spin_lock_irqsave(shost->host_lock, flags);
2002 if (scsi_host_set_state(shost, SHOST_RUNNING))
2003 if (scsi_host_set_state(shost, SHOST_CANCEL))
2004 BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
2005 spin_unlock_irqrestore(shost->host_lock, flags);
1da177e4
LT
2006
2007 wake_up(&shost->host_wait);
2008
2009 /*
2010 * finally we need to re-initiate requests that may be pending. we will
2011 * have had everything blocked while error handling is taking place, and
2012 * now that error recovery is done, we will need to ensure that these
2013 * requests are started.
2014 */
2015 scsi_run_host_queues(shost);
57fc2e33
DW
2016
2017 /*
2018 * if eh is active and host_eh_scheduled is pending we need to re-run
2019 * recovery. we do this check after scsi_run_host_queues() to allow
2020 * everything pent up since the last eh run a chance to make forward
2021 * progress before we sync again. Either we'll immediately re-run
2022 * recovery or scsi_device_unbusy() will wake us again when these
2023 * pending commands complete.
2024 */
2025 spin_lock_irqsave(shost->host_lock, flags);
2026 if (shost->host_eh_scheduled)
2027 if (scsi_host_set_state(shost, SHOST_RECOVERY))
2028 WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY));
2029 spin_unlock_irqrestore(shost->host_lock, flags);
1da177e4
LT
2030}
2031
2032/**
2033 * scsi_eh_ready_devs - check device ready state and recover if not.
2034 * @shost: host to be recovered.
eb44820c
RL
2035 * @work_q: &list_head for pending commands.
2036 * @done_q: &list_head for processed commands.
dc8875e1 2037 */
dca84e46
DW
2038void scsi_eh_ready_devs(struct Scsi_Host *shost,
2039 struct list_head *work_q,
2040 struct list_head *done_q)
1da177e4
LT
2041{
2042 if (!scsi_eh_stu(shost, work_q, done_q))
2043 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
30bd7df8
MC
2044 if (!scsi_eh_target_reset(shost, work_q, done_q))
2045 if (!scsi_eh_bus_reset(shost, work_q, done_q))
2046 if (!scsi_eh_host_reset(work_q, done_q))
2047 scsi_eh_offline_sdevs(work_q,
2048 done_q);
1da177e4 2049}
dca84e46 2050EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
1da177e4
LT
2051
2052/**
2053 * scsi_eh_flush_done_q - finish processed commands or retry them.
2054 * @done_q: list_head of processed commands.
dc8875e1 2055 */
041c5fc3 2056void scsi_eh_flush_done_q(struct list_head *done_q)
1da177e4 2057{
937abeaa 2058 struct scsi_cmnd *scmd, *next;
1da177e4 2059
937abeaa
CH
2060 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
2061 list_del_init(&scmd->eh_entry);
1da177e4 2062 if (scsi_device_online(scmd->device) &&
4a27446f 2063 !scsi_noretry_cmd(scmd) &&
8884efab 2064 (++scmd->retries <= scmd->allowed)) {
1da177e4
LT
2065 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush"
2066 " retry cmd: %p\n",
2067 current->comm,
2068 scmd));
2069 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
2070 } else {
793698ce
PM
2071 /*
2072 * If just we got sense for the device (called
2073 * scsi_eh_get_sense), scmd->result is already
2074 * set, do not set DRIVER_TIMEOUT.
2075 */
1da177e4
LT
2076 if (!scmd->result)
2077 scmd->result |= (DRIVER_TIMEOUT << 24);
2078 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush finish"
2079 " cmd: %p\n",
2080 current->comm, scmd));
2081 scsi_finish_command(scmd);
2082 }
2083 }
2084}
041c5fc3 2085EXPORT_SYMBOL(scsi_eh_flush_done_q);
1da177e4
LT
2086
2087/**
2088 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
2089 * @shost: Host to unjam.
2090 *
2091 * Notes:
2092 * When we come in here, we *know* that all commands on the bus have
2093 * either completed, failed or timed out. we also know that no further
2094 * commands are being sent to the host, so things are relatively quiet
2095 * and we have freedom to fiddle with things as we wish.
2096 *
2097 * This is only the *default* implementation. it is possible for
2098 * individual drivers to supply their own version of this function, and
2099 * if the maintainer wishes to do this, it is strongly suggested that
2100 * this function be taken as a template and modified. this function
2101 * was designed to correctly handle problems for about 95% of the
2102 * different cases out there, and it should always provide at least a
2103 * reasonable amount of error recovery.
2104 *
2105 * Any command marked 'failed' or 'timeout' must eventually have
2106 * scsi_finish_cmd() called for it. we do all of the retry stuff
2107 * here, so when we restart the host after we return it should have an
2108 * empty queue.
dc8875e1 2109 */
1da177e4
LT
2110static void scsi_unjam_host(struct Scsi_Host *shost)
2111{
2112 unsigned long flags;
2113 LIST_HEAD(eh_work_q);
2114 LIST_HEAD(eh_done_q);
2115
2116 spin_lock_irqsave(shost->host_lock, flags);
2117 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
2118 spin_unlock_irqrestore(shost->host_lock, flags);
2119
2120 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
2121
2122 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
2123 if (!scsi_eh_abort_cmds(&eh_work_q, &eh_done_q))
2124 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
2125
b4562022 2126 spin_lock_irqsave(shost->host_lock, flags);
bb3b621a 2127 if (shost->eh_deadline != -1)
b4562022
HR
2128 shost->last_reset = 0;
2129 spin_unlock_irqrestore(shost->host_lock, flags);
1da177e4
LT
2130 scsi_eh_flush_done_q(&eh_done_q);
2131}
2132
2133/**
ad42eb1b 2134 * scsi_error_handler - SCSI error handler thread
1da177e4
LT
2135 * @data: Host for which we are running.
2136 *
2137 * Notes:
ad42eb1b
CH
2138 * This is the main error handling loop. This is run as a kernel thread
2139 * for every SCSI host and handles all error handling activity.
dc8875e1 2140 */
1da177e4
LT
2141int scsi_error_handler(void *data)
2142{
ad42eb1b 2143 struct Scsi_Host *shost = data;
1da177e4 2144
1da177e4 2145 /*
ad42eb1b
CH
2146 * We use TASK_INTERRUPTIBLE so that the thread is not
2147 * counted against the load average as a running process.
2148 * We never actually get interrupted because kthread_run
c03264a7 2149 * disables signal delivery for the created thread.
1da177e4 2150 */
3ed7a470 2151 while (!kthread_should_stop()) {
b9d5c6b7 2152 set_current_state(TASK_INTERRUPTIBLE);
ee7863bc 2153 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
3ed7a470 2154 shost->host_failed != shost->host_busy) {
ad42eb1b 2155 SCSI_LOG_ERROR_RECOVERY(1,
b4562022 2156 printk("scsi_eh_%d: sleeping\n",
ad42eb1b 2157 shost->host_no));
3ed7a470 2158 schedule();
3ed7a470
JB
2159 continue;
2160 }
1da177e4 2161
3ed7a470 2162 __set_current_state(TASK_RUNNING);
ad42eb1b 2163 SCSI_LOG_ERROR_RECOVERY(1,
b4562022
HR
2164 printk("scsi_eh_%d: waking up %d/%d/%d\n",
2165 shost->host_no, shost->host_eh_scheduled,
2166 shost->host_failed, shost->host_busy));
1da177e4 2167
1da177e4
LT
2168 /*
2169 * We have a host that is failing for some reason. Figure out
2170 * what we need to do to get it up and online again (if we can).
2171 * If we fail, we end up taking the thing offline.
2172 */
ae0751ff 2173 if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
bc4f2401
AS
2174 SCSI_LOG_ERROR_RECOVERY(1,
2175 printk(KERN_ERR "Error handler scsi_eh_%d "
2176 "unable to autoresume\n",
2177 shost->host_no));
2178 continue;
2179 }
2180
9227c33d
CH
2181 if (shost->transportt->eh_strategy_handler)
2182 shost->transportt->eh_strategy_handler(shost);
1da177e4
LT
2183 else
2184 scsi_unjam_host(shost);
2185
1da177e4
LT
2186 /*
2187 * Note - if the above fails completely, the action is to take
2188 * individual devices offline and flush the queue of any
2189 * outstanding requests that may have been pending. When we
2190 * restart, we restart any I/O to any other devices on the bus
2191 * which are still online.
2192 */
2193 scsi_restart_operations(shost);
ae0751ff
LM
2194 if (!shost->eh_noresume)
2195 scsi_autopm_put_host(shost);
1da177e4 2196 }
461a0ffb
SR
2197 __set_current_state(TASK_RUNNING);
2198
ad42eb1b
CH
2199 SCSI_LOG_ERROR_RECOVERY(1,
2200 printk("Error handler scsi_eh_%d exiting\n", shost->host_no));
3ed7a470 2201 shost->ehandler = NULL;
1da177e4
LT
2202 return 0;
2203}
2204
2205/*
2206 * Function: scsi_report_bus_reset()
2207 *
2208 * Purpose: Utility function used by low-level drivers to report that
2209 * they have observed a bus reset on the bus being handled.
2210 *
2211 * Arguments: shost - Host in question
2212 * channel - channel on which reset was observed.
2213 *
2214 * Returns: Nothing
2215 *
2216 * Lock status: Host lock must be held.
2217 *
2218 * Notes: This only needs to be called if the reset is one which
2219 * originates from an unknown location. Resets originated
2220 * by the mid-level itself don't need to call this, but there
2221 * should be no harm.
2222 *
2223 * The main purpose of this is to make sure that a CHECK_CONDITION
2224 * is properly treated.
2225 */
2226void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
2227{
2228 struct scsi_device *sdev;
2229
2230 __shost_for_each_device(sdev, shost) {
30bd7df8
MC
2231 if (channel == sdev_channel(sdev))
2232 __scsi_report_device_reset(sdev, NULL);
1da177e4
LT
2233 }
2234}
2235EXPORT_SYMBOL(scsi_report_bus_reset);
2236
2237/*
2238 * Function: scsi_report_device_reset()
2239 *
2240 * Purpose: Utility function used by low-level drivers to report that
2241 * they have observed a device reset on the device being handled.
2242 *
2243 * Arguments: shost - Host in question
2244 * channel - channel on which reset was observed
2245 * target - target on which reset was observed
2246 *
2247 * Returns: Nothing
2248 *
2249 * Lock status: Host lock must be held
2250 *
2251 * Notes: This only needs to be called if the reset is one which
2252 * originates from an unknown location. Resets originated
2253 * by the mid-level itself don't need to call this, but there
2254 * should be no harm.
2255 *
2256 * The main purpose of this is to make sure that a CHECK_CONDITION
2257 * is properly treated.
2258 */
2259void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
2260{
2261 struct scsi_device *sdev;
2262
2263 __shost_for_each_device(sdev, shost) {
422c0d61 2264 if (channel == sdev_channel(sdev) &&
30bd7df8
MC
2265 target == sdev_id(sdev))
2266 __scsi_report_device_reset(sdev, NULL);
1da177e4
LT
2267 }
2268}
2269EXPORT_SYMBOL(scsi_report_device_reset);
2270
2271static void
2272scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
2273{
2274}
2275
2276/*
2277 * Function: scsi_reset_provider
2278 *
2279 * Purpose: Send requested reset to a bus or device at any phase.
2280 *
2281 * Arguments: device - device to send reset to
2282 * flag - reset type (see scsi.h)
2283 *
2284 * Returns: SUCCESS/FAILURE.
2285 *
2286 * Notes: This is used by the SCSI Generic driver to provide
2287 * Bus/Device reset capability.
2288 */
2289int
2290scsi_reset_provider(struct scsi_device *dev, int flag)
2291{
bc4f2401 2292 struct scsi_cmnd *scmd;
d7a1bb0a 2293 struct Scsi_Host *shost = dev->host;
1da177e4 2294 struct request req;
d7a1bb0a 2295 unsigned long flags;
1da177e4
LT
2296 int rtn;
2297
bc4f2401
AS
2298 if (scsi_autopm_get_host(shost) < 0)
2299 return FAILED;
2300
04796336
CH
2301 if (!get_device(&dev->sdev_gendev)) {
2302 rtn = FAILED;
2303 goto out_put_autopm_host;
2304 }
2305
bc4f2401 2306 scmd = scsi_get_command(dev, GFP_KERNEL);
4f54eec8 2307 blk_rq_init(NULL, &req);
1da177e4 2308 scmd->request = &req;
b4edcbca 2309
64a87b24
BH
2310 scmd->cmnd = req.cmd;
2311
1da177e4 2312 scmd->scsi_done = scsi_reset_provider_done_command;
30b0c37b 2313 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
1da177e4
LT
2314
2315 scmd->cmd_len = 0;
2316
2317 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
1da177e4 2318
d7a1bb0a
JS
2319 spin_lock_irqsave(shost->host_lock, flags);
2320 shost->tmf_in_progress = 1;
2321 spin_unlock_irqrestore(shost->host_lock, flags);
2322
1da177e4
LT
2323 switch (flag) {
2324 case SCSI_TRY_RESET_DEVICE:
2325 rtn = scsi_try_bus_device_reset(scmd);
2326 if (rtn == SUCCESS)
2327 break;
2328 /* FALLTHROUGH */
30bd7df8
MC
2329 case SCSI_TRY_RESET_TARGET:
2330 rtn = scsi_try_target_reset(scmd);
2331 if (rtn == SUCCESS)
2332 break;
2333 /* FALLTHROUGH */
1da177e4
LT
2334 case SCSI_TRY_RESET_BUS:
2335 rtn = scsi_try_bus_reset(scmd);
2336 if (rtn == SUCCESS)
2337 break;
2338 /* FALLTHROUGH */
2339 case SCSI_TRY_RESET_HOST:
2340 rtn = scsi_try_host_reset(scmd);
2341 break;
2342 default:
2343 rtn = FAILED;
2344 }
2345
d7a1bb0a
JS
2346 spin_lock_irqsave(shost->host_lock, flags);
2347 shost->tmf_in_progress = 0;
2348 spin_unlock_irqrestore(shost->host_lock, flags);
2349
2350 /*
2351 * be sure to wake up anyone who was sleeping or had their queue
2352 * suspended while we performed the TMF.
2353 */
2354 SCSI_LOG_ERROR_RECOVERY(3,
2355 printk("%s: waking up host to restart after TMF\n",
cadbd4a5 2356 __func__));
d7a1bb0a
JS
2357
2358 wake_up(&shost->host_wait);
2359
2360 scsi_run_host_queues(shost);
2361
1da177e4 2362 scsi_next_command(scmd);
04796336 2363out_put_autopm_host:
bc4f2401 2364 scsi_autopm_put_host(shost);
1da177e4
LT
2365 return rtn;
2366}
2367EXPORT_SYMBOL(scsi_reset_provider);
2368
2369/**
2370 * scsi_normalize_sense - normalize main elements from either fixed or
2371 * descriptor sense data format into a common format.
2372 *
2373 * @sense_buffer: byte array containing sense data returned by device
2374 * @sb_len: number of valid bytes in sense_buffer
2375 * @sshdr: pointer to instance of structure that common
2376 * elements are written to.
2377 *
2378 * Notes:
2379 * The "main elements" from sense data are: response_code, sense_key,
2380 * asc, ascq and additional_length (only for descriptor format).
2381 *
2382 * Typically this function can be called after a device has
2383 * responded to a SCSI command with the CHECK_CONDITION status.
2384 *
2385 * Return value:
2386 * 1 if valid sense data information found, else 0;
dc8875e1 2387 */
1da177e4
LT
2388int scsi_normalize_sense(const u8 *sense_buffer, int sb_len,
2389 struct scsi_sense_hdr *sshdr)
2390{
33aa687d 2391 if (!sense_buffer || !sb_len)
1da177e4
LT
2392 return 0;
2393
2394 memset(sshdr, 0, sizeof(struct scsi_sense_hdr));
2395
2396 sshdr->response_code = (sense_buffer[0] & 0x7f);
33aa687d
JB
2397
2398 if (!scsi_sense_valid(sshdr))
2399 return 0;
2400
1da177e4
LT
2401 if (sshdr->response_code >= 0x72) {
2402 /*
2403 * descriptor format
2404 */
2405 if (sb_len > 1)
2406 sshdr->sense_key = (sense_buffer[1] & 0xf);
2407 if (sb_len > 2)
2408 sshdr->asc = sense_buffer[2];
2409 if (sb_len > 3)
2410 sshdr->ascq = sense_buffer[3];
2411 if (sb_len > 7)
2412 sshdr->additional_length = sense_buffer[7];
2413 } else {
0bf8c869 2414 /*
1da177e4
LT
2415 * fixed format
2416 */
2417 if (sb_len > 2)
2418 sshdr->sense_key = (sense_buffer[2] & 0xf);
2419 if (sb_len > 7) {
2420 sb_len = (sb_len < (sense_buffer[7] + 8)) ?
2421 sb_len : (sense_buffer[7] + 8);
2422 if (sb_len > 12)
2423 sshdr->asc = sense_buffer[12];
2424 if (sb_len > 13)
2425 sshdr->ascq = sense_buffer[13];
2426 }
2427 }
2428
2429 return 1;
2430}
2431EXPORT_SYMBOL(scsi_normalize_sense);
2432
1da177e4
LT
2433int scsi_command_normalize_sense(struct scsi_cmnd *cmd,
2434 struct scsi_sense_hdr *sshdr)
2435{
2436 return scsi_normalize_sense(cmd->sense_buffer,
b80ca4f7 2437 SCSI_SENSE_BUFFERSIZE, sshdr);
1da177e4
LT
2438}
2439EXPORT_SYMBOL(scsi_command_normalize_sense);
2440
2441/**
eb44820c 2442 * scsi_sense_desc_find - search for a given descriptor type in descriptor sense data format.
1da177e4
LT
2443 * @sense_buffer: byte array of descriptor format sense data
2444 * @sb_len: number of valid bytes in sense_buffer
2445 * @desc_type: value of descriptor type to find
2446 * (e.g. 0 -> information)
2447 *
2448 * Notes:
2449 * only valid when sense data is in descriptor format
2450 *
2451 * Return value:
2452 * pointer to start of (first) descriptor if found else NULL
dc8875e1 2453 */
1da177e4
LT
2454const u8 * scsi_sense_desc_find(const u8 * sense_buffer, int sb_len,
2455 int desc_type)
2456{
2457 int add_sen_len, add_len, desc_len, k;
2458 const u8 * descp;
2459
2460 if ((sb_len < 8) || (0 == (add_sen_len = sense_buffer[7])))
2461 return NULL;
2462 if ((sense_buffer[0] < 0x72) || (sense_buffer[0] > 0x73))
2463 return NULL;
2464 add_sen_len = (add_sen_len < (sb_len - 8)) ?
2465 add_sen_len : (sb_len - 8);
2466 descp = &sense_buffer[8];
2467 for (desc_len = 0, k = 0; k < add_sen_len; k += desc_len) {
2468 descp += desc_len;
2469 add_len = (k < (add_sen_len - 1)) ? descp[1]: -1;
2470 desc_len = add_len + 2;
2471 if (descp[0] == desc_type)
2472 return descp;
2473 if (add_len < 0) // short descriptor ??
2474 break;
2475 }
2476 return NULL;
2477}
2478EXPORT_SYMBOL(scsi_sense_desc_find);
2479
2480/**
eb44820c 2481 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
1da177e4
LT
2482 * @sense_buffer: byte array of sense data
2483 * @sb_len: number of valid bytes in sense_buffer
2484 * @info_out: pointer to 64 integer where 8 or 4 byte information
2485 * field will be placed if found.
2486 *
2487 * Return value:
2488 * 1 if information field found, 0 if not found.
dc8875e1 2489 */
1da177e4
LT
2490int scsi_get_sense_info_fld(const u8 * sense_buffer, int sb_len,
2491 u64 * info_out)
2492{
2493 int j;
2494 const u8 * ucp;
2495 u64 ull;
2496
2497 if (sb_len < 7)
2498 return 0;
2499 switch (sense_buffer[0] & 0x7f) {
2500 case 0x70:
2501 case 0x71:
2502 if (sense_buffer[0] & 0x80) {
2503 *info_out = (sense_buffer[3] << 24) +
2504 (sense_buffer[4] << 16) +
2505 (sense_buffer[5] << 8) + sense_buffer[6];
2506 return 1;
2507 } else
2508 return 0;
2509 case 0x72:
2510 case 0x73:
2511 ucp = scsi_sense_desc_find(sense_buffer, sb_len,
2512 0 /* info desc */);
2513 if (ucp && (0xa == ucp[1])) {
2514 ull = 0;
2515 for (j = 0; j < 8; ++j) {
2516 if (j > 0)
2517 ull <<= 8;
2518 ull |= ucp[4 + j];
2519 }
2520 *info_out = ull;
2521 return 1;
2522 } else
2523 return 0;
2524 default:
2525 return 0;
2526 }
2527}
2528EXPORT_SYMBOL(scsi_get_sense_info_fld);
3bc6a261
FT
2529
2530/**
2531 * scsi_build_sense_buffer - build sense data in a buffer
2532 * @desc: Sense format (non zero == descriptor format,
2533 * 0 == fixed format)
2534 * @buf: Where to build sense data
2535 * @key: Sense key
2536 * @asc: Additional sense code
2537 * @ascq: Additional sense code qualifier
2538 *
2539 **/
2540void scsi_build_sense_buffer(int desc, u8 *buf, u8 key, u8 asc, u8 ascq)
2541{
2542 if (desc) {
2543 buf[0] = 0x72; /* descriptor, current */
2544 buf[1] = key;
2545 buf[2] = asc;
2546 buf[3] = ascq;
2547 buf[7] = 0;
2548 } else {
2549 buf[0] = 0x70; /* fixed, current */
2550 buf[2] = key;
2551 buf[7] = 0xa;
2552 buf[12] = asc;
2553 buf[13] = ascq;
2554 }
2555}
2556EXPORT_SYMBOL(scsi_build_sense_buffer);
This page took 0.890112 seconds and 5 git commands to generate.