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