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) &&
669 (sshdr.ascq == 0x01 || sshdr.ascq == 0x0a))
670 return NEEDS_RETRY;
671 /*
672 * if the device is not started, we need to wake
673 * the error handler to start the motor
674 */
675 if (scmd->device->allow_restart &&
676 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
677 return FAILED;
678 /*
679 * Pass the UA upwards for a determination in the completion
680 * functions.
681 */
682 return SUCCESS;
683
684 /* these are not supported */
685 case DATA_PROTECT:
686 if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
687 /* Thin provisioning hard threshold reached */
688 set_scsi_ml_byte(scmd, SCSIML_STAT_NOSPC);
689 return SUCCESS;
690 }
691 fallthrough;
692 case COPY_ABORTED:
693 case VOLUME_OVERFLOW:
694 case MISCOMPARE:
695 case BLANK_CHECK:
696 set_scsi_ml_byte(scmd, SCSIML_STAT_TGT_FAILURE);
697 return SUCCESS;
698
699 case MEDIUM_ERROR:
700 if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
701 sshdr.asc == 0x13 || /* AMNF DATA FIELD */
702 sshdr.asc == 0x14) { /* RECORD NOT FOUND */
703 set_scsi_ml_byte(scmd, SCSIML_STAT_MED_ERROR);
704 return SUCCESS;
705 }
706 return NEEDS_RETRY;
707
708 case HARDWARE_ERROR:
709 if (scmd->device->retry_hwerror)
710 return ADD_TO_MLQUEUE;
711 else
712 set_scsi_ml_byte(scmd, SCSIML_STAT_TGT_FAILURE);
713 fallthrough;
714
715 case ILLEGAL_REQUEST:
716 if (sshdr.asc == 0x20 || /* Invalid command operation code */
717 sshdr.asc == 0x21 || /* Logical block address out of range */
718 sshdr.asc == 0x22 || /* Invalid function */
719 sshdr.asc == 0x24 || /* Invalid field in cdb */
720 sshdr.asc == 0x26 || /* Parameter value invalid */
721 sshdr.asc == 0x27) { /* Write protected */
722 set_scsi_ml_byte(scmd, SCSIML_STAT_TGT_FAILURE);
723 }
724 return SUCCESS;
725
726 case COMPLETED:
727 /*
728 * A command using command duration limits (CDL) with a
729 * descriptor set with policy 0xD may be completed with success
730 * and the sense data DATA CURRENTLY UNAVAILABLE, indicating
731 * that the command was in fact aborted because it exceeded its
732 * duration limit. Never retry these commands.
733 */
734 if (sshdr.asc == 0x55 && sshdr.ascq == 0x0a) {
735 set_scsi_ml_byte(scmd, SCSIML_STAT_DL_TIMEOUT);
736 req->cmd_flags |= REQ_FAILFAST_DEV;
737 req->rq_flags |= RQF_QUIET;
738 }
739 return SUCCESS;
740
741 default:
742 return SUCCESS;
743 }
744 }
745 EXPORT_SYMBOL_GPL(scsi_check_sense);
746
scsi_handle_queue_ramp_up(struct scsi_device * sdev)747 static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
748 {
749 const struct scsi_host_template *sht = sdev->host->hostt;
750 struct scsi_device *tmp_sdev;
751
752 if (!sht->track_queue_depth ||
753 sdev->queue_depth >= sdev->max_queue_depth)
754 return;
755
756 if (time_before(jiffies,
757 sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
758 return;
759
760 if (time_before(jiffies,
761 sdev->last_queue_full_time + sdev->queue_ramp_up_period))
762 return;
763
764 /*
765 * Walk all devices of a target and do
766 * ramp up on them.
767 */
768 shost_for_each_device(tmp_sdev, sdev->host) {
769 if (tmp_sdev->channel != sdev->channel ||
770 tmp_sdev->id != sdev->id ||
771 tmp_sdev->queue_depth == sdev->max_queue_depth)
772 continue;
773
774 scsi_change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1);
775 sdev->last_queue_ramp_up = jiffies;
776 }
777 }
778
scsi_handle_queue_full(struct scsi_device * sdev)779 static void scsi_handle_queue_full(struct scsi_device *sdev)
780 {
781 const struct scsi_host_template *sht = sdev->host->hostt;
782 struct scsi_device *tmp_sdev;
783
784 if (!sht->track_queue_depth)
785 return;
786
787 shost_for_each_device(tmp_sdev, sdev->host) {
788 if (tmp_sdev->channel != sdev->channel ||
789 tmp_sdev->id != sdev->id)
790 continue;
791 /*
792 * We do not know the number of commands that were at
793 * the device when we got the queue full so we start
794 * from the highest possible value and work our way down.
795 */
796 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
797 }
798 }
799
800 /**
801 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
802 * @scmd: SCSI cmd to examine.
803 *
804 * Notes:
805 * This is *only* called when we are examining the status of commands
806 * queued during error recovery. the main difference here is that we
807 * don't allow for the possibility of retries here, and we are a lot
808 * more restrictive about what we consider acceptable.
809 */
scsi_eh_completed_normally(struct scsi_cmnd * scmd)810 static enum scsi_disposition scsi_eh_completed_normally(struct scsi_cmnd *scmd)
811 {
812 /*
813 * first check the host byte, to see if there is anything in there
814 * that would indicate what we need to do.
815 */
816 if (host_byte(scmd->result) == DID_RESET) {
817 /*
818 * rats. we are already in the error handler, so we now
819 * get to try and figure out what to do next. if the sense
820 * is valid, we have a pretty good idea of what to do.
821 * if not, we mark it as FAILED.
822 */
823 return scsi_check_sense(scmd);
824 }
825 if (host_byte(scmd->result) != DID_OK)
826 return FAILED;
827
828 /*
829 * now, check the status byte to see if this indicates
830 * anything special.
831 */
832 switch (get_status_byte(scmd)) {
833 case SAM_STAT_GOOD:
834 scsi_handle_queue_ramp_up(scmd->device);
835 if (scmd->sense_buffer && SCSI_SENSE_VALID(scmd))
836 /*
837 * If we have sense data, call scsi_check_sense() in
838 * order to set the correct SCSI ML byte (if any).
839 * No point in checking the return value, since the
840 * command has already completed successfully.
841 */
842 scsi_check_sense(scmd);
843 fallthrough;
844 case SAM_STAT_COMMAND_TERMINATED:
845 return SUCCESS;
846 case SAM_STAT_CHECK_CONDITION:
847 return scsi_check_sense(scmd);
848 case SAM_STAT_CONDITION_MET:
849 case SAM_STAT_INTERMEDIATE:
850 case SAM_STAT_INTERMEDIATE_CONDITION_MET:
851 /*
852 * who knows? FIXME(eric)
853 */
854 return SUCCESS;
855 case SAM_STAT_RESERVATION_CONFLICT:
856 if (scmd->cmnd[0] == TEST_UNIT_READY)
857 /* it is a success, we probed the device and
858 * found it */
859 return SUCCESS;
860 /* otherwise, we failed to send the command */
861 return FAILED;
862 case SAM_STAT_TASK_SET_FULL:
863 scsi_handle_queue_full(scmd->device);
864 fallthrough;
865 case SAM_STAT_BUSY:
866 return NEEDS_RETRY;
867 default:
868 return FAILED;
869 }
870 return FAILED;
871 }
872
873 /**
874 * scsi_eh_done - Completion function for error handling.
875 * @scmd: Cmd that is done.
876 */
scsi_eh_done(struct scsi_cmnd * scmd)877 void scsi_eh_done(struct scsi_cmnd *scmd)
878 {
879 struct completion *eh_action;
880
881 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
882 "%s result: %x\n", __func__, scmd->result));
883
884 eh_action = scmd->device->host->eh_action;
885 if (eh_action)
886 complete(eh_action);
887 }
888
889 /**
890 * scsi_try_host_reset - ask host adapter to reset itself
891 * @scmd: SCSI cmd to send host reset.
892 */
scsi_try_host_reset(struct scsi_cmnd * scmd)893 static enum scsi_disposition scsi_try_host_reset(struct scsi_cmnd *scmd)
894 {
895 unsigned long flags;
896 enum scsi_disposition rtn;
897 struct Scsi_Host *host = scmd->device->host;
898 const struct scsi_host_template *hostt = host->hostt;
899
900 SCSI_LOG_ERROR_RECOVERY(3,
901 shost_printk(KERN_INFO, host, "Snd Host RST\n"));
902
903 if (!hostt->eh_host_reset_handler)
904 return FAILED;
905
906 rtn = hostt->eh_host_reset_handler(scmd);
907
908 if (rtn == SUCCESS) {
909 if (!hostt->skip_settle_delay)
910 ssleep(HOST_RESET_SETTLE_TIME);
911 spin_lock_irqsave(host->host_lock, flags);
912 scsi_report_bus_reset(host, scmd_channel(scmd));
913 spin_unlock_irqrestore(host->host_lock, flags);
914 }
915
916 return rtn;
917 }
918
919 /**
920 * scsi_try_bus_reset - ask host to perform a bus reset
921 * @scmd: SCSI cmd to send bus reset.
922 */
scsi_try_bus_reset(struct scsi_cmnd * scmd)923 static enum scsi_disposition scsi_try_bus_reset(struct scsi_cmnd *scmd)
924 {
925 unsigned long flags;
926 enum scsi_disposition rtn;
927 struct Scsi_Host *host = scmd->device->host;
928 const struct scsi_host_template *hostt = host->hostt;
929
930 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
931 "%s: Snd Bus RST\n", __func__));
932
933 if (!hostt->eh_bus_reset_handler)
934 return FAILED;
935
936 rtn = hostt->eh_bus_reset_handler(scmd);
937
938 if (rtn == SUCCESS) {
939 if (!hostt->skip_settle_delay)
940 ssleep(BUS_RESET_SETTLE_TIME);
941 spin_lock_irqsave(host->host_lock, flags);
942 scsi_report_bus_reset(host, scmd_channel(scmd));
943 spin_unlock_irqrestore(host->host_lock, flags);
944 }
945
946 return rtn;
947 }
948
__scsi_report_device_reset(struct scsi_device * sdev,void * data)949 static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
950 {
951 sdev->was_reset = 1;
952 sdev->expecting_cc_ua = 1;
953 }
954
955 /**
956 * scsi_try_target_reset - Ask host to perform a target reset
957 * @scmd: SCSI cmd used to send a target reset
958 *
959 * Notes:
960 * There is no timeout for this operation. if this operation is
961 * unreliable for a given host, then the host itself needs to put a
962 * timer on it, and set the host back to a consistent state prior to
963 * returning.
964 */
scsi_try_target_reset(struct scsi_cmnd * scmd)965 static enum scsi_disposition scsi_try_target_reset(struct scsi_cmnd *scmd)
966 {
967 unsigned long flags;
968 enum scsi_disposition rtn;
969 struct Scsi_Host *host = scmd->device->host;
970 const struct scsi_host_template *hostt = host->hostt;
971
972 if (!hostt->eh_target_reset_handler)
973 return FAILED;
974
975 rtn = hostt->eh_target_reset_handler(scmd);
976 if (rtn == SUCCESS) {
977 spin_lock_irqsave(host->host_lock, flags);
978 __starget_for_each_device(scsi_target(scmd->device), NULL,
979 __scsi_report_device_reset);
980 spin_unlock_irqrestore(host->host_lock, flags);
981 }
982
983 return rtn;
984 }
985
986 /**
987 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
988 * @scmd: SCSI cmd used to send BDR
989 *
990 * Notes:
991 * There is no timeout for this operation. if this operation is
992 * unreliable for a given host, then the host itself needs to put a
993 * timer on it, and set the host back to a consistent state prior to
994 * returning.
995 */
scsi_try_bus_device_reset(struct scsi_cmnd * scmd)996 static enum scsi_disposition scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
997 {
998 enum scsi_disposition rtn;
999 const struct scsi_host_template *hostt = scmd->device->host->hostt;
1000
1001 if (!hostt->eh_device_reset_handler)
1002 return FAILED;
1003
1004 rtn = hostt->eh_device_reset_handler(scmd);
1005 if (rtn == SUCCESS)
1006 __scsi_report_device_reset(scmd->device, NULL);
1007 return rtn;
1008 }
1009
1010 /**
1011 * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
1012 * @hostt: SCSI driver host template
1013 * @scmd: SCSI cmd used to send a target reset
1014 *
1015 * Return value:
1016 * SUCCESS, FAILED, or FAST_IO_FAIL
1017 *
1018 * Notes:
1019 * SUCCESS does not necessarily indicate that the command
1020 * has been aborted; it only indicates that the LLDDs
1021 * has cleared all references to that command.
1022 * LLDDs should return FAILED only if an abort was required
1023 * but could not be executed. LLDDs should return FAST_IO_FAIL
1024 * if the device is temporarily unavailable (eg due to a
1025 * link down on FibreChannel)
1026 */
1027 static enum scsi_disposition
scsi_try_to_abort_cmd(const struct scsi_host_template * hostt,struct scsi_cmnd * scmd)1028 scsi_try_to_abort_cmd(const struct scsi_host_template *hostt, struct scsi_cmnd *scmd)
1029 {
1030 if (!hostt->eh_abort_handler)
1031 return FAILED;
1032
1033 return hostt->eh_abort_handler(scmd);
1034 }
1035
scsi_abort_eh_cmnd(struct scsi_cmnd * scmd)1036 static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
1037 {
1038 if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
1039 if (scsi_try_bus_device_reset(scmd) != SUCCESS)
1040 if (scsi_try_target_reset(scmd) != SUCCESS)
1041 if (scsi_try_bus_reset(scmd) != SUCCESS)
1042 scsi_try_host_reset(scmd);
1043 }
1044
1045 /**
1046 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
1047 * @scmd: SCSI command structure to hijack
1048 * @ses: structure to save restore information
1049 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
1050 * @cmnd_size: size in bytes of @cmnd (must be <= MAX_COMMAND_SIZE)
1051 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
1052 *
1053 * This function is used to save a scsi command information before re-execution
1054 * as part of the error recovery process. If @sense_bytes is 0 the command
1055 * sent must be one that does not transfer any data. If @sense_bytes != 0
1056 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
1057 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
1058 */
scsi_eh_prep_cmnd(struct scsi_cmnd * scmd,struct scsi_eh_save * ses,unsigned char * cmnd,int cmnd_size,unsigned sense_bytes)1059 void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
1060 unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
1061 {
1062 struct scsi_device *sdev = scmd->device;
1063
1064 /*
1065 * We need saved copies of a number of fields - this is because
1066 * error handling may need to overwrite these with different values
1067 * to run different commands, and once error handling is complete,
1068 * we will need to restore these values prior to running the actual
1069 * command.
1070 */
1071 ses->cmd_len = scmd->cmd_len;
1072 ses->data_direction = scmd->sc_data_direction;
1073 ses->sdb = scmd->sdb;
1074 ses->result = scmd->result;
1075 ses->resid_len = scmd->resid_len;
1076 ses->underflow = scmd->underflow;
1077 ses->prot_op = scmd->prot_op;
1078 ses->eh_eflags = scmd->eh_eflags;
1079
1080 scmd->prot_op = SCSI_PROT_NORMAL;
1081 scmd->eh_eflags = 0;
1082 memcpy(ses->cmnd, scmd->cmnd, sizeof(ses->cmnd));
1083 memset(scmd->cmnd, 0, sizeof(scmd->cmnd));
1084 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
1085 scmd->result = 0;
1086 scmd->resid_len = 0;
1087
1088 if (sense_bytes) {
1089 scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
1090 sense_bytes);
1091 sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
1092 scmd->sdb.length);
1093 scmd->sdb.table.sgl = &ses->sense_sgl;
1094 scmd->sc_data_direction = DMA_FROM_DEVICE;
1095 scmd->sdb.table.nents = scmd->sdb.table.orig_nents = 1;
1096 scmd->cmnd[0] = REQUEST_SENSE;
1097 scmd->cmnd[4] = scmd->sdb.length;
1098 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
1099 } else {
1100 scmd->sc_data_direction = DMA_NONE;
1101 if (cmnd) {
1102 BUG_ON(cmnd_size > sizeof(scmd->cmnd));
1103 memcpy(scmd->cmnd, cmnd, cmnd_size);
1104 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
1105 }
1106 }
1107
1108 scmd->underflow = 0;
1109
1110 if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
1111 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
1112 (sdev->lun << 5 & 0xe0);
1113
1114 /*
1115 * Zero the sense buffer. The scsi spec mandates that any
1116 * untransferred sense data should be interpreted as being zero.
1117 */
1118 memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1119 }
1120 EXPORT_SYMBOL(scsi_eh_prep_cmnd);
1121
1122 /**
1123 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
1124 * @scmd: SCSI command structure to restore
1125 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
1126 *
1127 * Undo any damage done by above scsi_eh_prep_cmnd().
1128 */
scsi_eh_restore_cmnd(struct scsi_cmnd * scmd,struct scsi_eh_save * ses)1129 void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
1130 {
1131 /*
1132 * Restore original data
1133 */
1134 scmd->cmd_len = ses->cmd_len;
1135 memcpy(scmd->cmnd, ses->cmnd, sizeof(ses->cmnd));
1136 scmd->sc_data_direction = ses->data_direction;
1137 scmd->sdb = ses->sdb;
1138 scmd->result = ses->result;
1139 scmd->resid_len = ses->resid_len;
1140 scmd->underflow = ses->underflow;
1141 scmd->prot_op = ses->prot_op;
1142 scmd->eh_eflags = ses->eh_eflags;
1143 }
1144 EXPORT_SYMBOL(scsi_eh_restore_cmnd);
1145
1146 /**
1147 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
1148 * @scmd: SCSI command structure to hijack
1149 * @cmnd: CDB to send
1150 * @cmnd_size: size in bytes of @cmnd
1151 * @timeout: timeout for this request
1152 * @sense_bytes: size of sense data to copy or 0
1153 *
1154 * This function is used to send a scsi command down to a target device
1155 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
1156 *
1157 * Return value:
1158 * SUCCESS or FAILED or NEEDS_RETRY
1159 */
scsi_send_eh_cmnd(struct scsi_cmnd * scmd,unsigned char * cmnd,int cmnd_size,int timeout,unsigned sense_bytes)1160 static enum scsi_disposition scsi_send_eh_cmnd(struct scsi_cmnd *scmd,
1161 unsigned char *cmnd, int cmnd_size, int timeout, unsigned sense_bytes)
1162 {
1163 struct scsi_device *sdev = scmd->device;
1164 struct Scsi_Host *shost = sdev->host;
1165 DECLARE_COMPLETION_ONSTACK(done);
1166 unsigned long timeleft = timeout, delay;
1167 struct scsi_eh_save ses;
1168 const unsigned long stall_for = msecs_to_jiffies(100);
1169 int rtn;
1170
1171 retry:
1172 scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
1173 shost->eh_action = &done;
1174
1175 scsi_log_send(scmd);
1176 scmd->submitter = SUBMITTED_BY_SCSI_ERROR_HANDLER;
1177 scmd->flags |= SCMD_LAST;
1178
1179 /*
1180 * Lock sdev->state_mutex to avoid that scsi_device_quiesce() can
1181 * change the SCSI device state after we have examined it and before
1182 * .queuecommand() is called.
1183 */
1184 mutex_lock(&sdev->state_mutex);
1185 while (sdev->sdev_state == SDEV_BLOCK && timeleft > 0) {
1186 mutex_unlock(&sdev->state_mutex);
1187 SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_DEBUG, sdev,
1188 "%s: state %d <> %d\n", __func__, sdev->sdev_state,
1189 SDEV_BLOCK));
1190 delay = min(timeleft, stall_for);
1191 timeleft -= delay;
1192 msleep(jiffies_to_msecs(delay));
1193 mutex_lock(&sdev->state_mutex);
1194 }
1195 if (sdev->sdev_state != SDEV_BLOCK)
1196 rtn = shost->hostt->queuecommand(shost, scmd);
1197 else
1198 rtn = FAILED;
1199 mutex_unlock(&sdev->state_mutex);
1200
1201 if (rtn) {
1202 if (timeleft > stall_for) {
1203 scsi_eh_restore_cmnd(scmd, &ses);
1204
1205 timeleft -= stall_for;
1206 msleep(jiffies_to_msecs(stall_for));
1207 goto retry;
1208 }
1209 /* signal not to enter either branch of the if () below */
1210 timeleft = 0;
1211 rtn = FAILED;
1212 } else {
1213 timeleft = wait_for_completion_timeout(&done, timeout);
1214 rtn = SUCCESS;
1215 }
1216
1217 shost->eh_action = NULL;
1218
1219 scsi_log_completion(scmd, rtn);
1220
1221 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1222 "%s timeleft: %ld\n",
1223 __func__, timeleft));
1224
1225 /*
1226 * If there is time left scsi_eh_done got called, and we will examine
1227 * the actual status codes to see whether the command actually did
1228 * complete normally, else if we have a zero return and no time left,
1229 * the command must still be pending, so abort it and return FAILED.
1230 * If we never actually managed to issue the command, because
1231 * ->queuecommand() kept returning non zero, use the rtn = FAILED
1232 * value above (so don't execute either branch of the if)
1233 */
1234 if (timeleft) {
1235 rtn = scsi_eh_completed_normally(scmd);
1236 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1237 "%s: scsi_eh_completed_normally %x\n", __func__, rtn));
1238
1239 switch (rtn) {
1240 case SUCCESS:
1241 case NEEDS_RETRY:
1242 case FAILED:
1243 break;
1244 case ADD_TO_MLQUEUE:
1245 rtn = NEEDS_RETRY;
1246 break;
1247 default:
1248 rtn = FAILED;
1249 break;
1250 }
1251 } else if (rtn != FAILED) {
1252 scsi_abort_eh_cmnd(scmd);
1253 rtn = FAILED;
1254 }
1255
1256 scsi_eh_restore_cmnd(scmd, &ses);
1257
1258 return rtn;
1259 }
1260
1261 /**
1262 * scsi_request_sense - Request sense data from a particular target.
1263 * @scmd: SCSI cmd for request sense.
1264 *
1265 * Notes:
1266 * Some hosts automatically obtain this information, others require
1267 * that we obtain it on our own. This function will *not* return until
1268 * the command either times out, or it completes.
1269 */
scsi_request_sense(struct scsi_cmnd * scmd)1270 static enum scsi_disposition scsi_request_sense(struct scsi_cmnd *scmd)
1271 {
1272 return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
1273 }
1274
1275 static enum scsi_disposition
scsi_eh_action(struct scsi_cmnd * scmd,enum scsi_disposition rtn)1276 scsi_eh_action(struct scsi_cmnd *scmd, enum scsi_disposition rtn)
1277 {
1278 if (!blk_rq_is_passthrough(scsi_cmd_to_rq(scmd))) {
1279 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
1280 if (sdrv->eh_action)
1281 rtn = sdrv->eh_action(scmd, rtn);
1282 }
1283 return rtn;
1284 }
1285
1286 /**
1287 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
1288 * @scmd: Original SCSI cmd that eh has finished.
1289 * @done_q: Queue for processed commands.
1290 *
1291 * Notes:
1292 * We don't want to use the normal command completion while we are are
1293 * still handling errors - it may cause other commands to be queued,
1294 * and that would disturb what we are doing. Thus we really want to
1295 * keep a list of pending commands for final completion, and once we
1296 * are ready to leave error handling we handle completion for real.
1297 */
scsi_eh_finish_cmd(struct scsi_cmnd * scmd,struct list_head * done_q)1298 void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
1299 {
1300 list_move_tail(&scmd->eh_entry, done_q);
1301 }
1302 EXPORT_SYMBOL(scsi_eh_finish_cmd);
1303
1304 /**
1305 * scsi_eh_get_sense - Get device sense data.
1306 * @work_q: Queue of commands to process.
1307 * @done_q: Queue of processed commands.
1308 *
1309 * Description:
1310 * See if we need to request sense information. if so, then get it
1311 * now, so we have a better idea of what to do.
1312 *
1313 * Notes:
1314 * This has the unfortunate side effect that if a shost adapter does
1315 * not automatically request sense information, we end up shutting
1316 * it down before we request it.
1317 *
1318 * All drivers should request sense information internally these days,
1319 * so for now all I have to say is tough noogies if you end up in here.
1320 *
1321 * XXX: Long term this code should go away, but that needs an audit of
1322 * all LLDDs first.
1323 */
scsi_eh_get_sense(struct list_head * work_q,struct list_head * done_q)1324 int scsi_eh_get_sense(struct list_head *work_q,
1325 struct list_head *done_q)
1326 {
1327 struct scsi_cmnd *scmd, *next;
1328 struct Scsi_Host *shost;
1329 enum scsi_disposition rtn;
1330
1331 /*
1332 * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
1333 * should not get sense.
1334 */
1335 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1336 if ((scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) ||
1337 SCSI_SENSE_VALID(scmd))
1338 continue;
1339
1340 shost = scmd->device->host;
1341 if (scsi_host_eh_past_deadline(shost)) {
1342 SCSI_LOG_ERROR_RECOVERY(3,
1343 scmd_printk(KERN_INFO, scmd,
1344 "%s: skip request sense, past eh deadline\n",
1345 current->comm));
1346 break;
1347 }
1348 if (!scsi_status_is_check_condition(scmd->result))
1349 /*
1350 * don't request sense if there's no check condition
1351 * status because the error we're processing isn't one
1352 * that has a sense code (and some devices get
1353 * confused by sense requests out of the blue)
1354 */
1355 continue;
1356
1357 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
1358 "%s: requesting sense\n",
1359 current->comm));
1360 rtn = scsi_request_sense(scmd);
1361 if (rtn != SUCCESS)
1362 continue;
1363
1364 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1365 "sense requested, result %x\n", scmd->result));
1366 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd));
1367
1368 rtn = scsi_decide_disposition(scmd);
1369
1370 /*
1371 * if the result was normal, then just pass it along to the
1372 * upper level.
1373 */
1374 if (rtn == SUCCESS)
1375 /*
1376 * We don't want this command reissued, just finished
1377 * with the sense data, so set retries to the max
1378 * allowed to ensure it won't get reissued. If the user
1379 * has requested infinite retries, we also want to
1380 * finish this command, so force completion by setting
1381 * retries and allowed to the same value.
1382 */
1383 if (scmd->allowed == SCSI_CMD_RETRIES_NO_LIMIT)
1384 scmd->retries = scmd->allowed = 1;
1385 else
1386 scmd->retries = scmd->allowed;
1387 else if (rtn != NEEDS_RETRY)
1388 continue;
1389
1390 scsi_eh_finish_cmd(scmd, done_q);
1391 }
1392
1393 return list_empty(work_q);
1394 }
1395 EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
1396
1397 /**
1398 * scsi_eh_tur - Send TUR to device.
1399 * @scmd: &scsi_cmnd to send TUR
1400 *
1401 * Return value:
1402 * 0 - Device is ready. 1 - Device NOT ready.
1403 */
scsi_eh_tur(struct scsi_cmnd * scmd)1404 static int scsi_eh_tur(struct scsi_cmnd *scmd)
1405 {
1406 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
1407 int retry_cnt = 1;
1408 enum scsi_disposition rtn;
1409
1410 retry_tur:
1411 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
1412 scmd->device->eh_timeout, 0);
1413
1414 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1415 "%s return: %x\n", __func__, rtn));
1416
1417 switch (rtn) {
1418 case NEEDS_RETRY:
1419 if (retry_cnt--)
1420 goto retry_tur;
1421 fallthrough;
1422 case SUCCESS:
1423 return 0;
1424 default:
1425 return 1;
1426 }
1427 }
1428
1429 /**
1430 * scsi_eh_test_devices - check if devices are responding from error recovery.
1431 * @cmd_list: scsi commands in error recovery.
1432 * @work_q: queue for commands which still need more error recovery
1433 * @done_q: queue for commands which are finished
1434 * @try_stu: boolean on if a STU command should be tried in addition to TUR.
1435 *
1436 * Decription:
1437 * Tests if devices are in a working state. Commands to devices now in
1438 * a working state are sent to the done_q while commands to devices which
1439 * are still failing to respond are returned to the work_q for more
1440 * processing.
1441 **/
scsi_eh_test_devices(struct list_head * cmd_list,struct list_head * work_q,struct list_head * done_q,int try_stu)1442 static int scsi_eh_test_devices(struct list_head *cmd_list,
1443 struct list_head *work_q,
1444 struct list_head *done_q, int try_stu)
1445 {
1446 struct scsi_cmnd *scmd, *next;
1447 struct scsi_device *sdev;
1448 int finish_cmds;
1449
1450 while (!list_empty(cmd_list)) {
1451 scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
1452 sdev = scmd->device;
1453
1454 if (!try_stu) {
1455 if (scsi_host_eh_past_deadline(sdev->host)) {
1456 /* Push items back onto work_q */
1457 list_splice_init(cmd_list, work_q);
1458 SCSI_LOG_ERROR_RECOVERY(3,
1459 sdev_printk(KERN_INFO, sdev,
1460 "%s: skip test device, past eh deadline",
1461 current->comm));
1462 break;
1463 }
1464 }
1465
1466 finish_cmds = !scsi_device_online(scmd->device) ||
1467 (try_stu && !scsi_eh_try_stu(scmd) &&
1468 !scsi_eh_tur(scmd)) ||
1469 !scsi_eh_tur(scmd);
1470
1471 list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
1472 if (scmd->device == sdev) {
1473 if (finish_cmds &&
1474 (try_stu ||
1475 scsi_eh_action(scmd, SUCCESS) == SUCCESS))
1476 scsi_eh_finish_cmd(scmd, done_q);
1477 else
1478 list_move_tail(&scmd->eh_entry, work_q);
1479 }
1480 }
1481 return list_empty(work_q);
1482 }
1483
1484 /**
1485 * scsi_eh_try_stu - Send START_UNIT to device.
1486 * @scmd: &scsi_cmnd to send START_UNIT
1487 *
1488 * Return value:
1489 * 0 - Device is ready. 1 - Device NOT ready.
1490 */
scsi_eh_try_stu(struct scsi_cmnd * scmd)1491 static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
1492 {
1493 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
1494
1495 if (scmd->device->allow_restart) {
1496 int i;
1497 enum scsi_disposition rtn = NEEDS_RETRY;
1498
1499 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
1500 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6,
1501 scmd->device->eh_timeout, 0);
1502
1503 if (rtn == SUCCESS)
1504 return 0;
1505 }
1506
1507 return 1;
1508 }
1509
1510 /**
1511 * scsi_eh_stu - send START_UNIT if needed
1512 * @shost: &scsi host being recovered.
1513 * @work_q: &list_head for pending commands.
1514 * @done_q: &list_head for processed commands.
1515 *
1516 * Notes:
1517 * If commands are failing due to not ready, initializing command required,
1518 * try revalidating the device, which will end up sending a start unit.
1519 */
scsi_eh_stu(struct Scsi_Host * shost,struct list_head * work_q,struct list_head * done_q)1520 static int scsi_eh_stu(struct Scsi_Host *shost,
1521 struct list_head *work_q,
1522 struct list_head *done_q)
1523 {
1524 struct scsi_cmnd *scmd, *stu_scmd, *next;
1525 struct scsi_device *sdev;
1526
1527 shost_for_each_device(sdev, shost) {
1528 if (scsi_host_eh_past_deadline(shost)) {
1529 SCSI_LOG_ERROR_RECOVERY(3,
1530 sdev_printk(KERN_INFO, sdev,
1531 "%s: skip START_UNIT, past eh deadline\n",
1532 current->comm));
1533 scsi_device_put(sdev);
1534 break;
1535 }
1536 stu_scmd = NULL;
1537 list_for_each_entry(scmd, work_q, eh_entry)
1538 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
1539 scsi_check_sense(scmd) == FAILED ) {
1540 stu_scmd = scmd;
1541 break;
1542 }
1543
1544 if (!stu_scmd)
1545 continue;
1546
1547 SCSI_LOG_ERROR_RECOVERY(3,
1548 sdev_printk(KERN_INFO, sdev,
1549 "%s: Sending START_UNIT\n",
1550 current->comm));
1551
1552 if (!scsi_eh_try_stu(stu_scmd)) {
1553 if (!scsi_device_online(sdev) ||
1554 !scsi_eh_tur(stu_scmd)) {
1555 list_for_each_entry_safe(scmd, next,
1556 work_q, eh_entry) {
1557 if (scmd->device == sdev &&
1558 scsi_eh_action(scmd, SUCCESS) == SUCCESS)
1559 scsi_eh_finish_cmd(scmd, done_q);
1560 }
1561 }
1562 } else {
1563 SCSI_LOG_ERROR_RECOVERY(3,
1564 sdev_printk(KERN_INFO, sdev,
1565 "%s: START_UNIT failed\n",
1566 current->comm));
1567 }
1568 }
1569
1570 return list_empty(work_q);
1571 }
1572
1573
1574 /**
1575 * scsi_eh_bus_device_reset - send bdr if needed
1576 * @shost: scsi host being recovered.
1577 * @work_q: &list_head for pending commands.
1578 * @done_q: &list_head for processed commands.
1579 *
1580 * Notes:
1581 * Try a bus device reset. Still, look to see whether we have multiple
1582 * devices that are jammed or not - if we have multiple devices, it
1583 * makes no sense to try bus_device_reset - we really would need to try
1584 * a bus_reset instead.
1585 */
scsi_eh_bus_device_reset(struct Scsi_Host * shost,struct list_head * work_q,struct list_head * done_q)1586 static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1587 struct list_head *work_q,
1588 struct list_head *done_q)
1589 {
1590 struct scsi_cmnd *scmd, *bdr_scmd, *next;
1591 struct scsi_device *sdev;
1592 enum scsi_disposition rtn;
1593
1594 shost_for_each_device(sdev, shost) {
1595 if (scsi_host_eh_past_deadline(shost)) {
1596 SCSI_LOG_ERROR_RECOVERY(3,
1597 sdev_printk(KERN_INFO, sdev,
1598 "%s: skip BDR, past eh deadline\n",
1599 current->comm));
1600 scsi_device_put(sdev);
1601 break;
1602 }
1603 bdr_scmd = NULL;
1604 list_for_each_entry(scmd, work_q, eh_entry)
1605 if (scmd->device == sdev) {
1606 bdr_scmd = scmd;
1607 break;
1608 }
1609
1610 if (!bdr_scmd)
1611 continue;
1612
1613 SCSI_LOG_ERROR_RECOVERY(3,
1614 sdev_printk(KERN_INFO, sdev,
1615 "%s: Sending BDR\n", current->comm));
1616 rtn = scsi_try_bus_device_reset(bdr_scmd);
1617 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
1618 if (!scsi_device_online(sdev) ||
1619 rtn == FAST_IO_FAIL ||
1620 !scsi_eh_tur(bdr_scmd)) {
1621 list_for_each_entry_safe(scmd, next,
1622 work_q, eh_entry) {
1623 if (scmd->device == sdev &&
1624 scsi_eh_action(scmd, rtn) != FAILED)
1625 scsi_eh_finish_cmd(scmd,
1626 done_q);
1627 }
1628 }
1629 } else {
1630 SCSI_LOG_ERROR_RECOVERY(3,
1631 sdev_printk(KERN_INFO, sdev,
1632 "%s: BDR failed\n", current->comm));
1633 }
1634 }
1635
1636 return list_empty(work_q);
1637 }
1638
1639 /**
1640 * scsi_eh_target_reset - send target reset if needed
1641 * @shost: scsi host being recovered.
1642 * @work_q: &list_head for pending commands.
1643 * @done_q: &list_head for processed commands.
1644 *
1645 * Notes:
1646 * Try a target reset.
1647 */
scsi_eh_target_reset(struct Scsi_Host * shost,struct list_head * work_q,struct list_head * done_q)1648 static int scsi_eh_target_reset(struct Scsi_Host *shost,
1649 struct list_head *work_q,
1650 struct list_head *done_q)
1651 {
1652 LIST_HEAD(tmp_list);
1653 LIST_HEAD(check_list);
1654
1655 list_splice_init(work_q, &tmp_list);
1656
1657 while (!list_empty(&tmp_list)) {
1658 struct scsi_cmnd *next, *scmd;
1659 enum scsi_disposition rtn;
1660 unsigned int id;
1661
1662 if (scsi_host_eh_past_deadline(shost)) {
1663 /* push back on work queue for further processing */
1664 list_splice_init(&check_list, work_q);
1665 list_splice_init(&tmp_list, work_q);
1666 SCSI_LOG_ERROR_RECOVERY(3,
1667 shost_printk(KERN_INFO, shost,
1668 "%s: Skip target reset, past eh deadline\n",
1669 current->comm));
1670 return list_empty(work_q);
1671 }
1672
1673 scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
1674 id = scmd_id(scmd);
1675
1676 SCSI_LOG_ERROR_RECOVERY(3,
1677 shost_printk(KERN_INFO, shost,
1678 "%s: Sending target reset to target %d\n",
1679 current->comm, id));
1680 rtn = scsi_try_target_reset(scmd);
1681 if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
1682 SCSI_LOG_ERROR_RECOVERY(3,
1683 shost_printk(KERN_INFO, shost,
1684 "%s: Target reset failed"
1685 " target: %d\n",
1686 current->comm, id));
1687 list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
1688 if (scmd_id(scmd) != id)
1689 continue;
1690
1691 if (rtn == SUCCESS)
1692 list_move_tail(&scmd->eh_entry, &check_list);
1693 else if (rtn == FAST_IO_FAIL)
1694 scsi_eh_finish_cmd(scmd, done_q);
1695 else
1696 /* push back on work queue for further processing */
1697 list_move(&scmd->eh_entry, work_q);
1698 }
1699 }
1700
1701 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1702 }
1703
1704 /**
1705 * scsi_eh_bus_reset - send a bus reset
1706 * @shost: &scsi host being recovered.
1707 * @work_q: &list_head for pending commands.
1708 * @done_q: &list_head for processed commands.
1709 */
scsi_eh_bus_reset(struct Scsi_Host * shost,struct list_head * work_q,struct list_head * done_q)1710 static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1711 struct list_head *work_q,
1712 struct list_head *done_q)
1713 {
1714 struct scsi_cmnd *scmd, *chan_scmd, *next;
1715 LIST_HEAD(check_list);
1716 unsigned int channel;
1717 enum scsi_disposition rtn;
1718
1719 /*
1720 * we really want to loop over the various channels, and do this on
1721 * a channel by channel basis. we should also check to see if any
1722 * of the failed commands are on soft_reset devices, and if so, skip
1723 * the reset.
1724 */
1725
1726 for (channel = 0; channel <= shost->max_channel; channel++) {
1727 if (scsi_host_eh_past_deadline(shost)) {
1728 list_splice_init(&check_list, work_q);
1729 SCSI_LOG_ERROR_RECOVERY(3,
1730 shost_printk(KERN_INFO, shost,
1731 "%s: skip BRST, past eh deadline\n",
1732 current->comm));
1733 return list_empty(work_q);
1734 }
1735
1736 chan_scmd = NULL;
1737 list_for_each_entry(scmd, work_q, eh_entry) {
1738 if (channel == scmd_channel(scmd)) {
1739 chan_scmd = scmd;
1740 break;
1741 /*
1742 * FIXME add back in some support for
1743 * soft_reset devices.
1744 */
1745 }
1746 }
1747
1748 if (!chan_scmd)
1749 continue;
1750 SCSI_LOG_ERROR_RECOVERY(3,
1751 shost_printk(KERN_INFO, shost,
1752 "%s: Sending BRST chan: %d\n",
1753 current->comm, channel));
1754 rtn = scsi_try_bus_reset(chan_scmd);
1755 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
1756 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1757 if (channel == scmd_channel(scmd)) {
1758 if (rtn == FAST_IO_FAIL)
1759 scsi_eh_finish_cmd(scmd,
1760 done_q);
1761 else
1762 list_move_tail(&scmd->eh_entry,
1763 &check_list);
1764 }
1765 }
1766 } else {
1767 SCSI_LOG_ERROR_RECOVERY(3,
1768 shost_printk(KERN_INFO, shost,
1769 "%s: BRST failed chan: %d\n",
1770 current->comm, channel));
1771 }
1772 }
1773 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1774 }
1775
1776 /**
1777 * scsi_eh_host_reset - send a host reset
1778 * @shost: host to be reset.
1779 * @work_q: &list_head for pending commands.
1780 * @done_q: &list_head for processed commands.
1781 */
scsi_eh_host_reset(struct Scsi_Host * shost,struct list_head * work_q,struct list_head * done_q)1782 static int scsi_eh_host_reset(struct Scsi_Host *shost,
1783 struct list_head *work_q,
1784 struct list_head *done_q)
1785 {
1786 struct scsi_cmnd *scmd, *next;
1787 LIST_HEAD(check_list);
1788 enum scsi_disposition rtn;
1789
1790 if (!list_empty(work_q)) {
1791 scmd = list_entry(work_q->next,
1792 struct scsi_cmnd, eh_entry);
1793
1794 SCSI_LOG_ERROR_RECOVERY(3,
1795 shost_printk(KERN_INFO, shost,
1796 "%s: Sending HRST\n",
1797 current->comm));
1798
1799 rtn = scsi_try_host_reset(scmd);
1800 if (rtn == SUCCESS) {
1801 list_splice_init(work_q, &check_list);
1802 } else if (rtn == FAST_IO_FAIL) {
1803 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1804 scsi_eh_finish_cmd(scmd, done_q);
1805 }
1806 } else {
1807 SCSI_LOG_ERROR_RECOVERY(3,
1808 shost_printk(KERN_INFO, shost,
1809 "%s: HRST failed\n",
1810 current->comm));
1811 }
1812 }
1813 return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
1814 }
1815
1816 /**
1817 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
1818 * @work_q: &list_head for pending commands.
1819 * @done_q: &list_head for processed commands.
1820 */
scsi_eh_offline_sdevs(struct list_head * work_q,struct list_head * done_q)1821 static void scsi_eh_offline_sdevs(struct list_head *work_q,
1822 struct list_head *done_q)
1823 {
1824 struct scsi_cmnd *scmd, *next;
1825 struct scsi_device *sdev;
1826
1827 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1828 sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
1829 "not ready after error recovery\n");
1830 sdev = scmd->device;
1831
1832 mutex_lock(&sdev->state_mutex);
1833 scsi_device_set_state(sdev, SDEV_OFFLINE);
1834 mutex_unlock(&sdev->state_mutex);
1835
1836 scsi_eh_finish_cmd(scmd, done_q);
1837 }
1838 return;
1839 }
1840
1841 /**
1842 * scsi_noretry_cmd - determine if command should be failed fast
1843 * @scmd: SCSI cmd to examine.
1844 */
scsi_noretry_cmd(struct scsi_cmnd * scmd)1845 bool scsi_noretry_cmd(struct scsi_cmnd *scmd)
1846 {
1847 struct request *req = scsi_cmd_to_rq(scmd);
1848
1849 switch (host_byte(scmd->result)) {
1850 case DID_OK:
1851 break;
1852 case DID_TIME_OUT:
1853 goto check_type;
1854 case DID_BUS_BUSY:
1855 return !!(req->cmd_flags & REQ_FAILFAST_TRANSPORT);
1856 case DID_PARITY:
1857 return !!(req->cmd_flags & REQ_FAILFAST_DEV);
1858 case DID_ERROR:
1859 if (get_status_byte(scmd) == SAM_STAT_RESERVATION_CONFLICT)
1860 return false;
1861 fallthrough;
1862 case DID_SOFT_ERROR:
1863 return !!(req->cmd_flags & REQ_FAILFAST_DRIVER);
1864 }
1865
1866 /* Never retry commands aborted due to a duration limit timeout */
1867 if (scsi_ml_byte(scmd->result) == SCSIML_STAT_DL_TIMEOUT)
1868 return true;
1869
1870 if (!scsi_status_is_check_condition(scmd->result))
1871 return false;
1872
1873 check_type:
1874 /*
1875 * assume caller has checked sense and determined
1876 * the check condition was retryable.
1877 */
1878 if (req->cmd_flags & REQ_FAILFAST_DEV || blk_rq_is_passthrough(req))
1879 return true;
1880
1881 return false;
1882 }
1883
1884 /**
1885 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1886 * @scmd: SCSI cmd to examine.
1887 *
1888 * Notes:
1889 * This is *only* called when we are examining the status after sending
1890 * out the actual data command. any commands that are queued for error
1891 * recovery (e.g. test_unit_ready) do *not* come through here.
1892 *
1893 * When this routine returns failed, it means the error handler thread
1894 * is woken. In cases where the error code indicates an error that
1895 * doesn't require the error handler read (i.e. we don't need to
1896 * abort/reset), this function should return SUCCESS.
1897 */
scsi_decide_disposition(struct scsi_cmnd * scmd)1898 enum scsi_disposition scsi_decide_disposition(struct scsi_cmnd *scmd)
1899 {
1900 enum scsi_disposition rtn;
1901
1902 /*
1903 * if the device is offline, then we clearly just pass the result back
1904 * up to the top level.
1905 */
1906 if (!scsi_device_online(scmd->device)) {
1907 SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO, scmd,
1908 "%s: device offline - report as SUCCESS\n", __func__));
1909 return SUCCESS;
1910 }
1911
1912 /*
1913 * first check the host byte, to see if there is anything in there
1914 * that would indicate what we need to do.
1915 */
1916 switch (host_byte(scmd->result)) {
1917 case DID_PASSTHROUGH:
1918 /*
1919 * no matter what, pass this through to the upper layer.
1920 * nuke this special code so that it looks like we are saying
1921 * did_ok.
1922 */
1923 scmd->result &= 0xff00ffff;
1924 return SUCCESS;
1925 case DID_OK:
1926 /*
1927 * looks good. drop through, and check the next byte.
1928 */
1929 break;
1930 case DID_ABORT:
1931 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
1932 set_host_byte(scmd, DID_TIME_OUT);
1933 return SUCCESS;
1934 }
1935 fallthrough;
1936 case DID_NO_CONNECT:
1937 case DID_BAD_TARGET:
1938 /*
1939 * note - this means that we just report the status back
1940 * to the top level driver, not that we actually think
1941 * that it indicates SUCCESS.
1942 */
1943 return SUCCESS;
1944 case DID_SOFT_ERROR:
1945 /*
1946 * when the low level driver returns did_soft_error,
1947 * it is responsible for keeping an internal retry counter
1948 * in order to avoid endless loops (db)
1949 */
1950 goto maybe_retry;
1951 case DID_IMM_RETRY:
1952 return NEEDS_RETRY;
1953
1954 case DID_REQUEUE:
1955 return ADD_TO_MLQUEUE;
1956 case DID_TRANSPORT_DISRUPTED:
1957 /*
1958 * LLD/transport was disrupted during processing of the IO.
1959 * The transport class is now blocked/blocking,
1960 * and the transport will decide what to do with the IO
1961 * based on its timers and recovery capablilities if
1962 * there are enough retries.
1963 */
1964 goto maybe_retry;
1965 case DID_TRANSPORT_FAILFAST:
1966 /*
1967 * The transport decided to failfast the IO (most likely
1968 * the fast io fail tmo fired), so send IO directly upwards.
1969 */
1970 return SUCCESS;
1971 case DID_TRANSPORT_MARGINAL:
1972 /*
1973 * caller has decided not to do retries on
1974 * abort success, so send IO directly upwards
1975 */
1976 return SUCCESS;
1977 case DID_ERROR:
1978 if (get_status_byte(scmd) == SAM_STAT_RESERVATION_CONFLICT)
1979 /*
1980 * execute reservation conflict processing code
1981 * lower down
1982 */
1983 break;
1984 fallthrough;
1985 case DID_BUS_BUSY:
1986 case DID_PARITY:
1987 goto maybe_retry;
1988 case DID_TIME_OUT:
1989 /*
1990 * when we scan the bus, we get timeout messages for
1991 * these commands if there is no device available.
1992 * other hosts report did_no_connect for the same thing.
1993 */
1994 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1995 scmd->cmnd[0] == INQUIRY)) {
1996 return SUCCESS;
1997 } else {
1998 return FAILED;
1999 }
2000 case DID_RESET:
2001 return SUCCESS;
2002 default:
2003 return FAILED;
2004 }
2005
2006 /*
2007 * check the status byte to see if this indicates anything special.
2008 */
2009 switch (get_status_byte(scmd)) {
2010 case SAM_STAT_TASK_SET_FULL:
2011 scsi_handle_queue_full(scmd->device);
2012 /*
2013 * the case of trying to send too many commands to a
2014 * tagged queueing device.
2015 */
2016 fallthrough;
2017 case SAM_STAT_BUSY:
2018 /*
2019 * device can't talk to us at the moment. Should only
2020 * occur (SAM-3) when the task queue is empty, so will cause
2021 * the empty queue handling to trigger a stall in the
2022 * device.
2023 */
2024 return ADD_TO_MLQUEUE;
2025 case SAM_STAT_GOOD:
2026 if (scmd->cmnd[0] == REPORT_LUNS)
2027 scmd->device->sdev_target->expecting_lun_change = 0;
2028 scsi_handle_queue_ramp_up(scmd->device);
2029 if (scmd->sense_buffer && SCSI_SENSE_VALID(scmd))
2030 /*
2031 * If we have sense data, call scsi_check_sense() in
2032 * order to set the correct SCSI ML byte (if any).
2033 * No point in checking the return value, since the
2034 * command has already completed successfully.
2035 */
2036 scsi_check_sense(scmd);
2037 fallthrough;
2038 case SAM_STAT_COMMAND_TERMINATED:
2039 return SUCCESS;
2040 case SAM_STAT_TASK_ABORTED:
2041 goto maybe_retry;
2042 case SAM_STAT_CHECK_CONDITION:
2043 rtn = scsi_check_sense(scmd);
2044 if (rtn == NEEDS_RETRY)
2045 goto maybe_retry;
2046 /* if rtn == FAILED, we have no sense information;
2047 * returning FAILED will wake the error handler thread
2048 * to collect the sense and redo the decide
2049 * disposition */
2050 return rtn;
2051 case SAM_STAT_CONDITION_MET:
2052 case SAM_STAT_INTERMEDIATE:
2053 case SAM_STAT_INTERMEDIATE_CONDITION_MET:
2054 case SAM_STAT_ACA_ACTIVE:
2055 /*
2056 * who knows? FIXME(eric)
2057 */
2058 return SUCCESS;
2059
2060 case SAM_STAT_RESERVATION_CONFLICT:
2061 sdev_printk(KERN_INFO, scmd->device,
2062 "reservation conflict\n");
2063 set_scsi_ml_byte(scmd, SCSIML_STAT_RESV_CONFLICT);
2064 return SUCCESS; /* causes immediate i/o error */
2065 }
2066 return FAILED;
2067
2068 maybe_retry:
2069
2070 /* we requeue for retry because the error was retryable, and
2071 * the request was not marked fast fail. Note that above,
2072 * even if the request is marked fast fail, we still requeue
2073 * for queue congestion conditions (QUEUE_FULL or BUSY) */
2074 if (scsi_cmd_retry_allowed(scmd) && !scsi_noretry_cmd(scmd)) {
2075 return NEEDS_RETRY;
2076 } else {
2077 /*
2078 * no more retries - report this one back to upper level.
2079 */
2080 return SUCCESS;
2081 }
2082 }
2083
eh_lock_door_done(struct request * req,blk_status_t status)2084 static enum rq_end_io_ret eh_lock_door_done(struct request *req,
2085 blk_status_t status)
2086 {
2087 blk_mq_free_request(req);
2088 return RQ_END_IO_NONE;
2089 }
2090
2091 /**
2092 * scsi_eh_lock_door - Prevent medium removal for the specified device
2093 * @sdev: SCSI device to prevent medium removal
2094 *
2095 * Locking:
2096 * We must be called from process context.
2097 *
2098 * Notes:
2099 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
2100 * head of the devices request queue, and continue.
2101 */
scsi_eh_lock_door(struct scsi_device * sdev)2102 static void scsi_eh_lock_door(struct scsi_device *sdev)
2103 {
2104 struct scsi_cmnd *scmd;
2105 struct request *req;
2106
2107 req = scsi_alloc_request(sdev->request_queue, REQ_OP_DRV_IN, 0);
2108 if (IS_ERR(req))
2109 return;
2110 scmd = blk_mq_rq_to_pdu(req);
2111
2112 scmd->cmnd[0] = ALLOW_MEDIUM_REMOVAL;
2113 scmd->cmnd[1] = 0;
2114 scmd->cmnd[2] = 0;
2115 scmd->cmnd[3] = 0;
2116 scmd->cmnd[4] = SCSI_REMOVAL_PREVENT;
2117 scmd->cmnd[5] = 0;
2118 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
2119 scmd->allowed = 5;
2120
2121 req->rq_flags |= RQF_QUIET;
2122 req->timeout = 10 * HZ;
2123 req->end_io = eh_lock_door_done;
2124
2125 blk_execute_rq_nowait(req, true);
2126 }
2127
2128 /**
2129 * scsi_restart_operations - restart io operations to the specified host.
2130 * @shost: Host we are restarting.
2131 *
2132 * Notes:
2133 * When we entered the error handler, we blocked all further i/o to
2134 * this device. we need to 'reverse' this process.
2135 */
scsi_restart_operations(struct Scsi_Host * shost)2136 static void scsi_restart_operations(struct Scsi_Host *shost)
2137 {
2138 struct scsi_device *sdev;
2139 unsigned long flags;
2140
2141 /*
2142 * If the door was locked, we need to insert a door lock request
2143 * onto the head of the SCSI request queue for the device. There
2144 * is no point trying to lock the door of an off-line device.
2145 */
2146 shost_for_each_device(sdev, shost) {
2147 if (scsi_device_online(sdev) && sdev->was_reset && sdev->locked) {
2148 scsi_eh_lock_door(sdev);
2149 sdev->was_reset = 0;
2150 }
2151 }
2152
2153 /*
2154 * next free up anything directly waiting upon the host. this
2155 * will be requests for character device operations, and also for
2156 * ioctls to queued block devices.
2157 */
2158 SCSI_LOG_ERROR_RECOVERY(3,
2159 shost_printk(KERN_INFO, shost, "waking up host to restart\n"));
2160
2161 spin_lock_irqsave(shost->host_lock, flags);
2162 if (scsi_host_set_state(shost, SHOST_RUNNING))
2163 if (scsi_host_set_state(shost, SHOST_CANCEL))
2164 BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
2165 spin_unlock_irqrestore(shost->host_lock, flags);
2166
2167 wake_up(&shost->host_wait);
2168
2169 /*
2170 * finally we need to re-initiate requests that may be pending. we will
2171 * have had everything blocked while error handling is taking place, and
2172 * now that error recovery is done, we will need to ensure that these
2173 * requests are started.
2174 */
2175 scsi_run_host_queues(shost);
2176
2177 /*
2178 * if eh is active and host_eh_scheduled is pending we need to re-run
2179 * recovery. we do this check after scsi_run_host_queues() to allow
2180 * everything pent up since the last eh run a chance to make forward
2181 * progress before we sync again. Either we'll immediately re-run
2182 * recovery or scsi_device_unbusy() will wake us again when these
2183 * pending commands complete.
2184 */
2185 spin_lock_irqsave(shost->host_lock, flags);
2186 if (shost->host_eh_scheduled)
2187 if (scsi_host_set_state(shost, SHOST_RECOVERY))
2188 WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY));
2189 spin_unlock_irqrestore(shost->host_lock, flags);
2190 }
2191
2192 /**
2193 * scsi_eh_ready_devs - check device ready state and recover if not.
2194 * @shost: host to be recovered.
2195 * @work_q: &list_head for pending commands.
2196 * @done_q: &list_head for processed commands.
2197 */
scsi_eh_ready_devs(struct Scsi_Host * shost,struct list_head * work_q,struct list_head * done_q)2198 void scsi_eh_ready_devs(struct Scsi_Host *shost,
2199 struct list_head *work_q,
2200 struct list_head *done_q)
2201 {
2202 if (!scsi_eh_stu(shost, work_q, done_q))
2203 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
2204 if (!scsi_eh_target_reset(shost, work_q, done_q))
2205 if (!scsi_eh_bus_reset(shost, work_q, done_q))
2206 if (!scsi_eh_host_reset(shost, work_q, done_q))
2207 scsi_eh_offline_sdevs(work_q,
2208 done_q);
2209 }
2210 EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
2211
2212 /**
2213 * scsi_eh_flush_done_q - finish processed commands or retry them.
2214 * @done_q: list_head of processed commands.
2215 */
scsi_eh_flush_done_q(struct list_head * done_q)2216 void scsi_eh_flush_done_q(struct list_head *done_q)
2217 {
2218 struct scsi_cmnd *scmd, *next;
2219
2220 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
2221 struct scsi_device *sdev = scmd->device;
2222
2223 list_del_init(&scmd->eh_entry);
2224 if (scsi_device_online(sdev) && !scsi_noretry_cmd(scmd) &&
2225 scsi_cmd_retry_allowed(scmd) &&
2226 scsi_eh_should_retry_cmd(scmd)) {
2227 SCSI_LOG_ERROR_RECOVERY(3,
2228 scmd_printk(KERN_INFO, scmd,
2229 "%s: flush retry cmd\n",
2230 current->comm));
2231 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
2232 blk_mq_kick_requeue_list(sdev->request_queue);
2233 } else {
2234 /*
2235 * If just we got sense for the device (called
2236 * scsi_eh_get_sense), scmd->result is already
2237 * set, do not set DID_TIME_OUT.
2238 */
2239 if (!scmd->result &&
2240 !(scmd->flags & SCMD_FORCE_EH_SUCCESS))
2241 scmd->result |= (DID_TIME_OUT << 16);
2242 SCSI_LOG_ERROR_RECOVERY(3,
2243 scmd_printk(KERN_INFO, scmd,
2244 "%s: flush finish cmd\n",
2245 current->comm));
2246 scsi_finish_command(scmd);
2247 }
2248 }
2249 }
2250 EXPORT_SYMBOL(scsi_eh_flush_done_q);
2251
2252 /**
2253 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
2254 * @shost: Host to unjam.
2255 *
2256 * Notes:
2257 * When we come in here, we *know* that all commands on the bus have
2258 * either completed, failed or timed out. we also know that no further
2259 * commands are being sent to the host, so things are relatively quiet
2260 * and we have freedom to fiddle with things as we wish.
2261 *
2262 * This is only the *default* implementation. it is possible for
2263 * individual drivers to supply their own version of this function, and
2264 * if the maintainer wishes to do this, it is strongly suggested that
2265 * this function be taken as a template and modified. this function
2266 * was designed to correctly handle problems for about 95% of the
2267 * different cases out there, and it should always provide at least a
2268 * reasonable amount of error recovery.
2269 *
2270 * Any command marked 'failed' or 'timeout' must eventually have
2271 * scsi_finish_cmd() called for it. we do all of the retry stuff
2272 * here, so when we restart the host after we return it should have an
2273 * empty queue.
2274 */
scsi_unjam_host(struct Scsi_Host * shost)2275 static void scsi_unjam_host(struct Scsi_Host *shost)
2276 {
2277 unsigned long flags;
2278 LIST_HEAD(eh_work_q);
2279 LIST_HEAD(eh_done_q);
2280
2281 spin_lock_irqsave(shost->host_lock, flags);
2282 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
2283 spin_unlock_irqrestore(shost->host_lock, flags);
2284
2285 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
2286
2287 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
2288 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
2289
2290 spin_lock_irqsave(shost->host_lock, flags);
2291 if (shost->eh_deadline != -1)
2292 shost->last_reset = 0;
2293 spin_unlock_irqrestore(shost->host_lock, flags);
2294 scsi_eh_flush_done_q(&eh_done_q);
2295 }
2296
2297 /**
2298 * scsi_error_handler - SCSI error handler thread
2299 * @data: Host for which we are running.
2300 *
2301 * Notes:
2302 * This is the main error handling loop. This is run as a kernel thread
2303 * for every SCSI host and handles all error handling activity.
2304 */
scsi_error_handler(void * data)2305 int scsi_error_handler(void *data)
2306 {
2307 struct Scsi_Host *shost = data;
2308
2309 /*
2310 * We use TASK_INTERRUPTIBLE so that the thread is not
2311 * counted against the load average as a running process.
2312 * We never actually get interrupted because kthread_run
2313 * disables signal delivery for the created thread.
2314 */
2315 while (true) {
2316 /*
2317 * The sequence in kthread_stop() sets the stop flag first
2318 * then wakes the process. To avoid missed wakeups, the task
2319 * should always be in a non running state before the stop
2320 * flag is checked
2321 */
2322 set_current_state(TASK_INTERRUPTIBLE);
2323 if (kthread_should_stop())
2324 break;
2325
2326 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
2327 shost->host_failed != scsi_host_busy(shost)) {
2328 SCSI_LOG_ERROR_RECOVERY(1,
2329 shost_printk(KERN_INFO, shost,
2330 "scsi_eh_%d: sleeping\n",
2331 shost->host_no));
2332 schedule();
2333 continue;
2334 }
2335
2336 __set_current_state(TASK_RUNNING);
2337 SCSI_LOG_ERROR_RECOVERY(1,
2338 shost_printk(KERN_INFO, shost,
2339 "scsi_eh_%d: waking up %d/%d/%d\n",
2340 shost->host_no, shost->host_eh_scheduled,
2341 shost->host_failed,
2342 scsi_host_busy(shost)));
2343
2344 /*
2345 * We have a host that is failing for some reason. Figure out
2346 * what we need to do to get it up and online again (if we can).
2347 * If we fail, we end up taking the thing offline.
2348 */
2349 if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
2350 SCSI_LOG_ERROR_RECOVERY(1,
2351 shost_printk(KERN_ERR, shost,
2352 "scsi_eh_%d: unable to autoresume\n",
2353 shost->host_no));
2354 continue;
2355 }
2356
2357 if (shost->transportt->eh_strategy_handler)
2358 shost->transportt->eh_strategy_handler(shost);
2359 else
2360 scsi_unjam_host(shost);
2361
2362 /* All scmds have been handled */
2363 shost->host_failed = 0;
2364
2365 /*
2366 * Note - if the above fails completely, the action is to take
2367 * individual devices offline and flush the queue of any
2368 * outstanding requests that may have been pending. When we
2369 * restart, we restart any I/O to any other devices on the bus
2370 * which are still online.
2371 */
2372 scsi_restart_operations(shost);
2373 if (!shost->eh_noresume)
2374 scsi_autopm_put_host(shost);
2375 }
2376 __set_current_state(TASK_RUNNING);
2377
2378 SCSI_LOG_ERROR_RECOVERY(1,
2379 shost_printk(KERN_INFO, shost,
2380 "Error handler scsi_eh_%d exiting\n",
2381 shost->host_no));
2382 shost->ehandler = NULL;
2383 return 0;
2384 }
2385
2386 /**
2387 * scsi_report_bus_reset() - report bus reset observed
2388 *
2389 * Utility function used by low-level drivers to report that
2390 * they have observed a bus reset on the bus being handled.
2391 *
2392 * @shost: Host in question
2393 * @channel: channel on which reset was observed.
2394 *
2395 * Returns: Nothing
2396 *
2397 * Lock status: Host lock must be held.
2398 *
2399 * Notes: This only needs to be called if the reset is one which
2400 * originates from an unknown location. Resets originated
2401 * by the mid-level itself don't need to call this, but there
2402 * should be no harm.
2403 *
2404 * The main purpose of this is to make sure that a CHECK_CONDITION
2405 * is properly treated.
2406 */
scsi_report_bus_reset(struct Scsi_Host * shost,int channel)2407 void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
2408 {
2409 struct scsi_device *sdev;
2410
2411 __shost_for_each_device(sdev, shost) {
2412 if (channel == sdev_channel(sdev))
2413 __scsi_report_device_reset(sdev, NULL);
2414 }
2415 }
2416 EXPORT_SYMBOL(scsi_report_bus_reset);
2417
2418 /**
2419 * scsi_report_device_reset() - report device reset observed
2420 *
2421 * Utility function used by low-level drivers to report that
2422 * they have observed a device reset on the device being handled.
2423 *
2424 * @shost: Host in question
2425 * @channel: channel on which reset was observed
2426 * @target: target on which reset was observed
2427 *
2428 * Returns: Nothing
2429 *
2430 * Lock status: Host lock must be held
2431 *
2432 * Notes: This only needs to be called if the reset is one which
2433 * originates from an unknown location. Resets originated
2434 * by the mid-level itself don't need to call this, but there
2435 * should be no harm.
2436 *
2437 * The main purpose of this is to make sure that a CHECK_CONDITION
2438 * is properly treated.
2439 */
scsi_report_device_reset(struct Scsi_Host * shost,int channel,int target)2440 void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
2441 {
2442 struct scsi_device *sdev;
2443
2444 __shost_for_each_device(sdev, shost) {
2445 if (channel == sdev_channel(sdev) &&
2446 target == sdev_id(sdev))
2447 __scsi_report_device_reset(sdev, NULL);
2448 }
2449 }
2450 EXPORT_SYMBOL(scsi_report_device_reset);
2451
2452 /**
2453 * scsi_ioctl_reset: explicitly reset a host/bus/target/device
2454 * @dev: scsi_device to operate on
2455 * @arg: reset type (see sg.h)
2456 */
2457 int
scsi_ioctl_reset(struct scsi_device * dev,int __user * arg)2458 scsi_ioctl_reset(struct scsi_device *dev, int __user *arg)
2459 {
2460 struct scsi_cmnd *scmd;
2461 struct Scsi_Host *shost = dev->host;
2462 struct request *rq;
2463 unsigned long flags;
2464 int error = 0, val;
2465 enum scsi_disposition rtn;
2466
2467 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2468 return -EACCES;
2469
2470 error = get_user(val, arg);
2471 if (error)
2472 return error;
2473
2474 if (scsi_autopm_get_host(shost) < 0)
2475 return -EIO;
2476
2477 error = -EIO;
2478 rq = kzalloc(sizeof(struct request) + sizeof(struct scsi_cmnd) +
2479 shost->hostt->cmd_size, GFP_KERNEL);
2480 if (!rq)
2481 goto out_put_autopm_host;
2482 blk_rq_init(NULL, rq);
2483
2484 scmd = (struct scsi_cmnd *)(rq + 1);
2485 scsi_init_command(dev, scmd);
2486
2487 scmd->submitter = SUBMITTED_BY_SCSI_RESET_IOCTL;
2488 scmd->flags |= SCMD_LAST;
2489 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
2490
2491 scmd->cmd_len = 0;
2492
2493 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
2494
2495 spin_lock_irqsave(shost->host_lock, flags);
2496 shost->tmf_in_progress = 1;
2497 spin_unlock_irqrestore(shost->host_lock, flags);
2498
2499 switch (val & ~SG_SCSI_RESET_NO_ESCALATE) {
2500 case SG_SCSI_RESET_NOTHING:
2501 rtn = SUCCESS;
2502 break;
2503 case SG_SCSI_RESET_DEVICE:
2504 rtn = scsi_try_bus_device_reset(scmd);
2505 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
2506 break;
2507 fallthrough;
2508 case SG_SCSI_RESET_TARGET:
2509 rtn = scsi_try_target_reset(scmd);
2510 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
2511 break;
2512 fallthrough;
2513 case SG_SCSI_RESET_BUS:
2514 rtn = scsi_try_bus_reset(scmd);
2515 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
2516 break;
2517 fallthrough;
2518 case SG_SCSI_RESET_HOST:
2519 rtn = scsi_try_host_reset(scmd);
2520 if (rtn == SUCCESS)
2521 break;
2522 fallthrough;
2523 default:
2524 rtn = FAILED;
2525 break;
2526 }
2527
2528 error = (rtn == SUCCESS) ? 0 : -EIO;
2529
2530 spin_lock_irqsave(shost->host_lock, flags);
2531 shost->tmf_in_progress = 0;
2532 spin_unlock_irqrestore(shost->host_lock, flags);
2533
2534 /*
2535 * be sure to wake up anyone who was sleeping or had their queue
2536 * suspended while we performed the TMF.
2537 */
2538 SCSI_LOG_ERROR_RECOVERY(3,
2539 shost_printk(KERN_INFO, shost,
2540 "waking up host to restart after TMF\n"));
2541
2542 wake_up(&shost->host_wait);
2543 scsi_run_host_queues(shost);
2544
2545 kfree(rq);
2546
2547 out_put_autopm_host:
2548 scsi_autopm_put_host(shost);
2549 return error;
2550 }
2551
scsi_command_normalize_sense(const struct scsi_cmnd * cmd,struct scsi_sense_hdr * sshdr)2552 bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd,
2553 struct scsi_sense_hdr *sshdr)
2554 {
2555 return scsi_normalize_sense(cmd->sense_buffer,
2556 SCSI_SENSE_BUFFERSIZE, sshdr);
2557 }
2558 EXPORT_SYMBOL(scsi_command_normalize_sense);
2559
2560 /**
2561 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
2562 * @sense_buffer: byte array of sense data
2563 * @sb_len: number of valid bytes in sense_buffer
2564 * @info_out: pointer to 64 integer where 8 or 4 byte information
2565 * field will be placed if found.
2566 *
2567 * Return value:
2568 * true if information field found, false if not found.
2569 */
scsi_get_sense_info_fld(const u8 * sense_buffer,int sb_len,u64 * info_out)2570 bool scsi_get_sense_info_fld(const u8 *sense_buffer, int sb_len,
2571 u64 *info_out)
2572 {
2573 const u8 * ucp;
2574
2575 if (sb_len < 7)
2576 return false;
2577 switch (sense_buffer[0] & 0x7f) {
2578 case 0x70:
2579 case 0x71:
2580 if (sense_buffer[0] & 0x80) {
2581 *info_out = get_unaligned_be32(&sense_buffer[3]);
2582 return true;
2583 }
2584 return false;
2585 case 0x72:
2586 case 0x73:
2587 ucp = scsi_sense_desc_find(sense_buffer, sb_len,
2588 0 /* info desc */);
2589 if (ucp && (0xa == ucp[1])) {
2590 *info_out = get_unaligned_be64(&ucp[4]);
2591 return true;
2592 }
2593 return false;
2594 default:
2595 return false;
2596 }
2597 }
2598 EXPORT_SYMBOL(scsi_get_sense_info_fld);
2599