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