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