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