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