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