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