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