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