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