Merge tag 'pm-6.16-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[linux-2.6-block.git] / drivers / scsi / scsi_error.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
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
0bf8c869 7 * Leonard Zubkoff and David Miller at Linux Expo,
1da177e4
LT
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
0bf8c869 14 * minor cleanups.
1da177e4
LT
15 * September 30, 2002 Mike Anderson (andmike@us.ibm.com)
16 */
17
18#include <linux/module.h>
19#include <linux/sched.h>
5a0e3ad6 20#include <linux/gfp.h>
1da177e4
LT
21#include <linux/timer.h>
22#include <linux/string.h>
1da177e4 23#include <linux/kernel.h>
83144186 24#include <linux/freezer.h>
c5478def 25#include <linux/kthread.h>
1da177e4
LT
26#include <linux/interrupt.h>
27#include <linux/blkdev.h>
28#include <linux/delay.h>
fc73648a 29#include <linux/jiffies.h>
1da177e4
LT
30
31#include <scsi/scsi.h>
beb40487 32#include <scsi/scsi_cmnd.h>
1da177e4
LT
33#include <scsi/scsi_dbg.h>
34#include <scsi/scsi_device.h>
18a4d0a2 35#include <scsi/scsi_driver.h>
1da177e4 36#include <scsi/scsi_eh.h>
7708c165 37#include <scsi/scsi_common.h>
c829c394 38#include <scsi/scsi_transport.h>
1da177e4
LT
39#include <scsi/scsi_host.h>
40#include <scsi/scsi_ioctl.h>
ee14c674 41#include <scsi/scsi_dh.h>
29cfc2ab 42#include <scsi/scsi_devinfo.h>
176aa9d6 43#include <scsi/sg.h>
1da177e4
LT
44
45#include "scsi_priv.h"
46#include "scsi_logging.h"
79ee8304 47#include "scsi_transport_api.h"
1da177e4 48
bf816235
KT
49#include <trace/events/scsi.h>
50
5f60d5f6 51#include <linux/unaligned.h>
2908769c 52
1da177e4
LT
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
3eef6257 60static int scsi_eh_try_stu(struct scsi_cmnd *scmd);
31435de9 61static enum scsi_disposition scsi_try_to_abort_cmd(const struct scsi_host_template *,
b8e162f9 62 struct scsi_cmnd *);
3eef6257 63
4373534a 64void scsi_eh_wakeup(struct Scsi_Host *shost, unsigned int busy)
1da177e4 65{
f0317e88
BVA
66 lockdep_assert_held(shost->host_lock);
67
4373534a 68 if (busy == shost->host_failed) {
bf816235 69 trace_scsi_eh_wakeup(shost);
3ed7a470 70 wake_up_process(shost->ehandler);
91921e01
HR
71 SCSI_LOG_ERROR_RECOVERY(5, shost_printk(KERN_INFO, shost,
72 "Waking error handler thread\n"));
1da177e4
LT
73 }
74}
f8bbfc24
TH
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.
dc8875e1 81 */
f8bbfc24
TH
82void 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++;
4373534a 91 scsi_eh_wakeup(shost, scsi_host_busy(shost));
f8bbfc24
TH
92 }
93
94 spin_unlock_irqrestore(shost->host_lock, flags);
95}
96EXPORT_SYMBOL_GPL(scsi_schedule_eh);
1da177e4 97
b4562022
HR
98static int scsi_host_eh_past_deadline(struct Scsi_Host *shost)
99{
bb3b621a 100 if (!shost->last_reset || shost->eh_deadline == -1)
b4562022
HR
101 return 0;
102
76ad3e59
HR
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
bb3b621a 107 * if eh_deadline has been set to 'off' during the
76ad3e59
HR
108 * time_before call.
109 */
110 if (time_before(jiffies, shost->last_reset + shost->eh_deadline) &&
bb3b621a 111 shost->eh_deadline > -1)
b4562022
HR
112 return 0;
113
114 return 1;
115}
116
2a242d59
MC
117static 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
60bee27b
MK
125static 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
e494f6a7
HR
136/**
137 * scmd_eh_abort_handler - Handle command aborts
138 * @work: command to be aborted.
923f46f9
BVA
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
deef1be1 142 * scsi_timeout(), a .scsi_done() call from the LLD for a command that has
923f46f9
BVA
143 * timed out do not have any effect. Hence it is safe to call
144 * scsi_finish_command() from this function.
e494f6a7
HR
145 */
146void
147scmd_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;
54d816d3 152 struct Scsi_Host *shost = sdev->host;
b8e162f9 153 enum scsi_disposition rtn;
5ae17501 154 unsigned long flags;
e494f6a7 155
54d816d3 156 if (scsi_host_eh_past_deadline(shost)) {
e494f6a7
HR
157 SCSI_LOG_ERROR_RECOVERY(3,
158 scmd_printk(KERN_INFO, scmd,
470613b4 159 "eh timeout, not aborting\n"));
54d816d3
EM
160 goto out;
161 }
162
163 SCSI_LOG_ERROR_RECOVERY(3,
e494f6a7 164 scmd_printk(KERN_INFO, scmd,
470613b4 165 "aborting command\n"));
54d816d3
EM
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;
e494f6a7
HR
182 }
183
54d816d3 184 spin_lock_irqsave(shost->host_lock, flags);
5ae17501 185 list_del_init(&scmd->eh_entry);
54d816d3
EM
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
213out:
214 spin_lock_irqsave(shost->host_lock, flags);
215 list_del_init(&scmd->eh_entry);
216 spin_unlock_irqrestore(shost->host_lock, flags);
217
a0658632 218 scsi_eh_scmd_add(scmd);
e494f6a7
HR
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 */
227static int
228scsi_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
d0b90255
HR
234 if (!shost->hostt->eh_abort_handler) {
235 /* No abort handler, fail command directly */
236 return FAILED;
237 }
238
e494f6a7
HR
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,
470613b4 245 "previous abort failed\n"));
fcc95a76 246 BUG_ON(delayed_work_pending(&scmd->abort_work));
e494f6a7
HR
247 return FAILED;
248 }
249
e494f6a7 250 spin_lock_irqsave(shost->host_lock, flags);
bb3b621a 251 if (shost->eh_deadline != -1 && !shost->last_reset)
e494f6a7 252 shost->last_reset = jiffies;
5ae17501
EM
253 BUG_ON(!list_empty(&scmd->eh_entry));
254 list_add_tail(&scmd->eh_entry, &shost->eh_abort_list);
e494f6a7
HR
255 spin_unlock_irqrestore(shost->host_lock, flags);
256
257 scmd->eh_eflags |= SCSI_EH_ABORT_SCHEDULED;
258 SCSI_LOG_ERROR_RECOVERY(3,
470613b4 259 scmd_printk(KERN_INFO, scmd, "abort scheduled\n"));
e494f6a7
HR
260 queue_delayed_work(shost->tmf_work_q, &scmd->abort_work, HZ / 100);
261 return SUCCESS;
262}
263
1da177e4 264/**
7a38dc0b 265 * scsi_eh_reset - call into ->eh_action to reset internal counters
1da177e4 266 * @scmd: scmd to run eh on.
1da177e4 267 *
7a38dc0b
HR
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.
dc8875e1 271 */
7a38dc0b
HR
272static void scsi_eh_reset(struct scsi_cmnd *scmd)
273{
aa8e25e5 274 if (!blk_rq_is_passthrough(scsi_cmd_to_rq(scmd))) {
7a38dc0b
HR
275 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
276 if (sdrv->eh_reset)
277 sdrv->eh_reset(scmd);
278 }
279}
280
3bd6f43f
BVA
281static void scsi_eh_inc_host_failed(struct rcu_head *head)
282{
3be8828f
BVA
283 struct scsi_cmnd *scmd = container_of(head, typeof(*scmd), rcu);
284 struct Scsi_Host *shost = scmd->device->host;
4e6c9011 285 unsigned int busy = scsi_host_busy(shost);
3bd6f43f
BVA
286 unsigned long flags;
287
288 spin_lock_irqsave(shost->host_lock, flags);
289 shost->host_failed++;
4e6c9011 290 scsi_eh_wakeup(shost, busy);
3bd6f43f
BVA
291 spin_unlock_irqrestore(shost->host_lock, flags);
292}
293
1da177e4
LT
294/**
295 * scsi_eh_scmd_add - add scsi cmd to error handling.
296 * @scmd: scmd to run eh on.
dc8875e1 297 */
a0658632 298void scsi_eh_scmd_add(struct scsi_cmnd *scmd)
1da177e4
LT
299{
300 struct Scsi_Host *shost = scmd->device->host;
301 unsigned long flags;
2171b6d0 302 int ret;
1da177e4 303
2171b6d0 304 WARN_ON_ONCE(!shost->ehandler);
10b53db2 305 WARN_ON_ONCE(!test_bit(SCMD_STATE_INFLIGHT, &scmd->state));
1da177e4
LT
306
307 spin_lock_irqsave(shost->host_lock, flags);
2171b6d0
HR
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 }
bb3b621a 312 if (shost->eh_deadline != -1 && !shost->last_reset)
b4562022
HR
313 shost->last_reset = jiffies;
314
7a38dc0b 315 scsi_eh_reset(scmd);
1da177e4 316 list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
1da177e4 317 spin_unlock_irqrestore(shost->host_lock, flags);
3bd6f43f
BVA
318 /*
319 * Ensure that all tasks observe the host state change before the
320 * host_failed change.
321 */
54d87b0a 322 call_rcu_hurry(&scmd->rcu, scsi_eh_inc_host_failed);
1da177e4
LT
323}
324
1da177e4 325/**
deef1be1 326 * scsi_timeout - Timeout function for normal scsi commands.
242f9dcb 327 * @req: request that is timing out.
1da177e4
LT
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.
dc8875e1 334 */
9bdb4833 335enum blk_eh_timer_return scsi_timeout(struct request *req)
1da177e4 336{
bed2213d 337 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
0bf8c869 338 struct Scsi_Host *host = scmd->device->host;
6c5f8ce1 339
bf816235 340 trace_scsi_dispatch_cmd_timeout(scmd);
1da177e4
LT
341 scsi_log_completion(scmd, TIMEOUT_ERROR);
342
48517eef 343 atomic_inc(&scmd->device->iotmo_cnt);
bb3b621a 344 if (host->eh_deadline != -1 && !host->last_reset)
b4562022
HR
345 host->last_reset = jiffies;
346
dee7121e
BVA
347 if (host->hostt->eh_timed_out) {
348 switch (host->hostt->eh_timed_out(scmd)) {
349 case SCSI_EH_DONE:
978b7922 350 return BLK_EH_DONE;
dee7121e 351 case SCSI_EH_RESET_TIMER:
f1342709 352 return BLK_EH_RESET_TIMER;
dee7121e
BVA
353 case SCSI_EH_NOT_HANDLED:
354 break;
2171b6d0 355 }
a33c070b 356 }
242f9dcb 357
dee7121e
BVA
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;
ec9780e4 364 atomic_inc(&scmd->device->iodone_cnt);
dee7121e
BVA
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;
1da177e4
LT
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.
dc8875e1 383 */
1da177e4
LT
384int scsi_block_when_processing_errors(struct scsi_device *sdev)
385{
386 int online;
387
939647ee 388 wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
1da177e4
LT
389
390 online = scsi_device_online(sdev);
391
1da177e4
LT
392 return online;
393}
394EXPORT_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.
dc8875e1 401 */
1da177e4
LT
402static 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;
a0658632 416 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED)
1da177e4 417 ++cmd_cancel;
0bf8c869 418 else
1da177e4
LT
419 ++cmd_failed;
420 }
421 }
422
423 if (cmd_cancel || cmd_failed) {
424 SCSI_LOG_ERROR_RECOVERY(3,
a3a790dc 425 shost_printk(KERN_INFO, shost,
9ccfc756 426 "%s: cmds failed: %d, cancel: %d\n",
cadbd4a5 427 __func__, cmd_failed,
9ccfc756 428 cmd_cancel));
1da177e4
LT
429 cmd_cancel = 0;
430 cmd_failed = 0;
431 ++devices_failed;
432 }
433 }
434
91921e01
HR
435 SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO, shost,
436 "Total of %d commands on %d"
437 " devices require eh work\n",
0bf8c869 438 total_failures, devices_failed));
1da177e4
LT
439}
440#endif
441
279afdfe
EM
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 */
447static 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 */
458static 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,
279afdfe
EM
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,
a2417db3 477 "Operating parameters on this target have "
279afdfe
EM
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
cf3431bb
HR
489 if (sshdr->asc == 0x29) {
490 evt_type = SDEV_EVT_POWER_ON_RESET_OCCURRED;
af4edb1d
AH
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");
cf3431bb
HR
498 }
499
279afdfe
EM
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");
14c3e677
HR
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");
279afdfe
EM
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
36ebf1e2
MC
523static inline void set_scsi_ml_byte(struct scsi_cmnd *cmd, u8 status)
524{
525 cmd->result = (cmd->result & 0xffff00ff) | (status << 8);
526}
527
1da177e4
LT
528/**
529 * scsi_check_sense - Examine scsi cmd sense
530 * @scmd: Cmd to have sense checked.
531 *
532 * Return value:
87f14e65 533 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
1da177e4
LT
534 *
535 * Notes:
536 * When a deferred error is detected the current command has
537 * not been executed and needs retrying.
dc8875e1 538 */
b8e162f9 539enum scsi_disposition scsi_check_sense(struct scsi_cmnd *scmd)
1da177e4 540{
390e2d1a 541 struct request *req = scsi_cmd_to_rq(scmd);
a6a8d9f8 542 struct scsi_device *sdev = scmd->device;
1da177e4
LT
543 struct scsi_sense_hdr sshdr;
544
545 if (! scsi_command_normalize_sense(scmd, &sshdr))
546 return FAILED; /* no valid sense data */
547
279afdfe
EM
548 scsi_report_sense(sdev, &sshdr);
549
a5d518cd
KM
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
1da177e4
LT
562 if (scsi_sense_is_deferred(&sshdr))
563 return NEEDS_RETRY;
564
ee14c674 565 if (sdev->handler && sdev->handler->check_sense) {
b8e162f9 566 enum scsi_disposition rc;
a6a8d9f8 567
ee14c674 568 rc = sdev->handler->check_sense(sdev, &sshdr);
a6a8d9f8
CS
569 if (rc != SCSI_RETURN_NOT_HANDLED)
570 return rc;
571 /* handler does not care. Drop down to default handling */
572 }
573
bf23e619
BVA
574 if (scmd->cmnd[0] == TEST_UNIT_READY &&
575 scmd->submitter != SUBMITTED_BY_SCSI_ERROR_HANDLER)
e925cc43
CH
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
1da177e4
LT
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:
511e44f4
MP
610 if (sshdr.asc == 0x10) /* DIF */
611 return SUCCESS;
612
390e2d1a
NC
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
29cfc2ab
MW
629 if (sshdr.asc == 0x44 && sdev->sdev_bflags & BLIST_RETRY_ITF)
630 return ADD_TO_MLQUEUE;
c3606520
MW
631 if (sshdr.asc == 0xc1 && sshdr.ascq == 0x01 &&
632 sdev->sdev_bflags & BLIST_RETRY_ASC_C1)
633 return ADD_TO_MLQUEUE;
29cfc2ab 634
1da177e4
LT
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) {
dfcf7775
TH
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 }
1da177e4 655 }
279afdfe
EM
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;
1da177e4 664 /*
0bf8c869 665 * if the device is in the process of becoming ready, we
1da177e4
LT
666 * should retry.
667 */
5c3ba819
R
668 if ((sshdr.asc == 0x04) &&
669 (sshdr.ascq == 0x01 || sshdr.ascq == 0x0a))
1da177e4
LT
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;
02e031cb
CH
678 /*
679 * Pass the UA upwards for a determination in the completion
680 * functions.
681 */
682 return SUCCESS;
1da177e4 683
63583cca 684 /* these are not supported */
a9d6ceb8
HR
685 case DATA_PROTECT:
686 if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
687 /* Thin provisioning hard threshold reached */
7dfaae6a 688 set_scsi_ml_byte(scmd, SCSIML_STAT_NOSPC);
a9d6ceb8
HR
689 return SUCCESS;
690 }
df561f66 691 fallthrough;
1da177e4
LT
692 case COPY_ABORTED:
693 case VOLUME_OVERFLOW:
694 case MISCOMPARE:
63583cca 695 case BLANK_CHECK:
7dfaae6a 696 set_scsi_ml_byte(scmd, SCSIML_STAT_TGT_FAILURE);
87f14e65 697 return SUCCESS;
1da177e4
LT
698
699 case MEDIUM_ERROR:
fd1b494d
LT
700 if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
701 sshdr.asc == 0x13 || /* AMNF DATA FIELD */
702 sshdr.asc == 0x14) { /* RECORD NOT FOUND */
7dfaae6a 703 set_scsi_ml_byte(scmd, SCSIML_STAT_MED_ERROR);
87f14e65 704 return SUCCESS;
fd1b494d 705 }
1da177e4
LT
706 return NEEDS_RETRY;
707
708 case HARDWARE_ERROR:
709 if (scmd->device->retry_hwerror)
bb0003c1 710 return ADD_TO_MLQUEUE;
1da177e4 711 else
7dfaae6a 712 set_scsi_ml_byte(scmd, SCSIML_STAT_TGT_FAILURE);
df561f66 713 fallthrough;
1da177e4
LT
714
715 case ILLEGAL_REQUEST:
47ac56db
MS
716 if (sshdr.asc == 0x20 || /* Invalid command operation code */
717 sshdr.asc == 0x21 || /* Logical block address out of range */
a8bbb2ab 718 sshdr.asc == 0x22 || /* Invalid function */
47ac56db 719 sshdr.asc == 0x24 || /* Invalid field in cdb */
d0b7a909
MW
720 sshdr.asc == 0x26 || /* Parameter value invalid */
721 sshdr.asc == 0x27) { /* Write protected */
7dfaae6a 722 set_scsi_ml_byte(scmd, SCSIML_STAT_TGT_FAILURE);
47ac56db
MS
723 }
724 return SUCCESS;
725
390e2d1a 726 case COMPLETED:
e6a81567
DLM
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 */
390e2d1a
NC
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
1da177e4
LT
741 default:
742 return SUCCESS;
743 }
744}
3852e373 745EXPORT_SYMBOL_GPL(scsi_check_sense);
1da177e4 746
4a84067d
VD
747static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
748{
31435de9 749 const struct scsi_host_template *sht = sdev->host->hostt;
4a84067d
VD
750 struct scsi_device *tmp_sdev;
751
c40ecc12 752 if (!sht->track_queue_depth ||
4a84067d
VD
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;
c40ecc12 773
db5ed4df 774 scsi_change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1);
4a84067d
VD
775 sdev->last_queue_ramp_up = jiffies;
776 }
777}
778
42a6a918
MC
779static void scsi_handle_queue_full(struct scsi_device *sdev)
780{
31435de9 781 const struct scsi_host_template *sht = sdev->host->hostt;
42a6a918
MC
782 struct scsi_device *tmp_sdev;
783
c40ecc12 784 if (!sht->track_queue_depth)
42a6a918
MC
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 */
c40ecc12 796 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
42a6a918
MC
797 }
798}
799
1da177e4
LT
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.
dc8875e1 809 */
b8e162f9 810static enum scsi_disposition scsi_eh_completed_normally(struct scsi_cmnd *scmd)
1da177e4
LT
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
1da177e4
LT
828 /*
829 * now, check the status byte to see if this indicates
830 * anything special.
831 */
3d45cefc
HR
832 switch (get_status_byte(scmd)) {
833 case SAM_STAT_GOOD:
4a84067d 834 scsi_handle_queue_ramp_up(scmd->device);
390e2d1a
NC
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);
df561f66 843 fallthrough;
3d45cefc 844 case SAM_STAT_COMMAND_TERMINATED:
1da177e4 845 return SUCCESS;
3d45cefc 846 case SAM_STAT_CHECK_CONDITION:
1da177e4 847 return scsi_check_sense(scmd);
3d45cefc
HR
848 case SAM_STAT_CONDITION_MET:
849 case SAM_STAT_INTERMEDIATE:
850 case SAM_STAT_INTERMEDIATE_CONDITION_MET:
1da177e4
LT
851 /*
852 * who knows? FIXME(eric)
853 */
854 return SUCCESS;
3d45cefc 855 case SAM_STAT_RESERVATION_CONFLICT:
67110dfd
JB
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;
3d45cefc 862 case SAM_STAT_TASK_SET_FULL:
42a6a918 863 scsi_handle_queue_full(scmd->device);
df561f66 864 fallthrough;
3d45cefc 865 case SAM_STAT_BUSY:
3eb3a928 866 return NEEDS_RETRY;
1da177e4
LT
867 default:
868 return FAILED;
869 }
870 return FAILED;
871}
872
1da177e4
LT
873/**
874 * scsi_eh_done - Completion function for error handling.
875 * @scmd: Cmd that is done.
dc8875e1 876 */
bf23e619 877void scsi_eh_done(struct scsi_cmnd *scmd)
1da177e4 878{
0bf8c869 879 struct completion *eh_action;
85631672 880
91921e01 881 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
470613b4 882 "%s result: %x\n", __func__, scmd->result));
85631672
MR
883
884 eh_action = scmd->device->host->eh_action;
885 if (eh_action)
886 complete(eh_action);
1da177e4
LT
887}
888
292148f8
BK
889/**
890 * scsi_try_host_reset - ask host adapter to reset itself
c2b3ebd0 891 * @scmd: SCSI cmd to send host reset.
dc8875e1 892 */
b8e162f9 893static enum scsi_disposition scsi_try_host_reset(struct scsi_cmnd *scmd)
292148f8
BK
894{
895 unsigned long flags;
b8e162f9 896 enum scsi_disposition rtn;
0bf8c869 897 struct Scsi_Host *host = scmd->device->host;
31435de9 898 const struct scsi_host_template *hostt = host->hostt;
292148f8 899
91921e01
HR
900 SCSI_LOG_ERROR_RECOVERY(3,
901 shost_printk(KERN_INFO, host, "Snd Host RST\n"));
292148f8 902
0bf8c869 903 if (!hostt->eh_host_reset_handler)
292148f8
BK
904 return FAILED;
905
0bf8c869 906 rtn = hostt->eh_host_reset_handler(scmd);
292148f8
BK
907
908 if (rtn == SUCCESS) {
0bf8c869 909 if (!hostt->skip_settle_delay)
292148f8 910 ssleep(HOST_RESET_SETTLE_TIME);
0bf8c869
JJ
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);
292148f8
BK
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.
dc8875e1 922 */
b8e162f9 923static enum scsi_disposition scsi_try_bus_reset(struct scsi_cmnd *scmd)
292148f8
BK
924{
925 unsigned long flags;
b8e162f9 926 enum scsi_disposition rtn;
0bf8c869 927 struct Scsi_Host *host = scmd->device->host;
31435de9 928 const struct scsi_host_template *hostt = host->hostt;
292148f8 929
91921e01
HR
930 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
931 "%s: Snd Bus RST\n", __func__));
292148f8 932
0bf8c869 933 if (!hostt->eh_bus_reset_handler)
292148f8
BK
934 return FAILED;
935
0bf8c869 936 rtn = hostt->eh_bus_reset_handler(scmd);
292148f8
BK
937
938 if (rtn == SUCCESS) {
0bf8c869 939 if (!hostt->skip_settle_delay)
292148f8 940 ssleep(BUS_RESET_SETTLE_TIME);
0bf8c869
JJ
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);
292148f8
BK
944 }
945
946 return rtn;
947}
948
30bd7df8
MC
949static 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 */
b8e162f9 965static enum scsi_disposition scsi_try_target_reset(struct scsi_cmnd *scmd)
30bd7df8
MC
966{
967 unsigned long flags;
b8e162f9 968 enum scsi_disposition rtn;
0bf8c869 969 struct Scsi_Host *host = scmd->device->host;
31435de9 970 const struct scsi_host_template *hostt = host->hostt;
30bd7df8 971
0bf8c869 972 if (!hostt->eh_target_reset_handler)
30bd7df8
MC
973 return FAILED;
974
0bf8c869 975 rtn = hostt->eh_target_reset_handler(scmd);
30bd7df8 976 if (rtn == SUCCESS) {
0bf8c869 977 spin_lock_irqsave(host->host_lock, flags);
30bd7df8
MC
978 __starget_for_each_device(scsi_target(scmd->device), NULL,
979 __scsi_report_device_reset);
0bf8c869 980 spin_unlock_irqrestore(host->host_lock, flags);
30bd7df8
MC
981 }
982
983 return rtn;
984}
985
292148f8
BK
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.
dc8875e1 995 */
b8e162f9 996static enum scsi_disposition scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
292148f8 997{
b8e162f9 998 enum scsi_disposition rtn;
31435de9 999 const struct scsi_host_template *hostt = scmd->device->host->hostt;
292148f8 1000
0bf8c869 1001 if (!hostt->eh_device_reset_handler)
292148f8
BK
1002 return FAILED;
1003
0bf8c869 1004 rtn = hostt->eh_device_reset_handler(scmd);
30bd7df8
MC
1005 if (rtn == SUCCESS)
1006 __scsi_report_device_reset(scmd->device, NULL);
292148f8
BK
1007 return rtn;
1008}
1009
883a030f
HR
1010/**
1011 * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
6583f6fb 1012 * @hostt: SCSI driver host template
883a030f
HR
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 */
b8e162f9 1027static enum scsi_disposition
31435de9 1028scsi_try_to_abort_cmd(const struct scsi_host_template *hostt, struct scsi_cmnd *scmd)
292148f8 1029{
0bf8c869 1030 if (!hostt->eh_abort_handler)
292148f8
BK
1031 return FAILED;
1032
0bf8c869 1033 return hostt->eh_abort_handler(scmd);
292148f8
BK
1034}
1035
292148f8
BK
1036static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
1037{
0bf8c869 1038 if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
292148f8 1039 if (scsi_try_bus_device_reset(scmd) != SUCCESS)
30bd7df8
MC
1040 if (scsi_try_target_reset(scmd) != SUCCESS)
1041 if (scsi_try_bus_reset(scmd) != SUCCESS)
1042 scsi_try_host_reset(scmd);
292148f8
BK
1043}
1044
1da177e4 1045/**
3b729f76 1046 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
2dc611de 1047 * @scmd: SCSI command structure to hijack
e1c23468 1048 * @ses: structure to save restore information
55db6c1b 1049 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
ce70fd9a 1050 * @cmnd_size: size in bytes of @cmnd (must be <= MAX_COMMAND_SIZE)
55db6c1b 1051 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
2dc611de 1052 *
e1c23468 1053 * This function is used to save a scsi command information before re-execution
55db6c1b
BH
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.
dc8875e1 1058 */
e1c23468
BH
1059void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
1060 unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
1da177e4 1061{
f59114b7 1062 struct scsi_device *sdev = scmd->device;
1da177e4 1063
631c228c
CH
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 */
e1c23468 1071 ses->cmd_len = scmd->cmd_len;
e1c23468 1072 ses->data_direction = scmd->sc_data_direction;
30b0c37b 1073 ses->sdb = scmd->sdb;
e1c23468 1074 ses->result = scmd->result;
a9a4ea11 1075 ses->resid_len = scmd->resid_len;
12265709 1076 ses->underflow = scmd->underflow;
db007fc5 1077 ses->prot_op = scmd->prot_op;
8e8c9d01 1078 ses->eh_eflags = scmd->eh_eflags;
631c228c 1079
db007fc5 1080 scmd->prot_op = SCSI_PROT_NORMAL;
c69e6f81 1081 scmd->eh_eflags = 0;
ce70fd9a
CH
1082 memcpy(ses->cmnd, scmd->cmnd, sizeof(ses->cmnd));
1083 memset(scmd->cmnd, 0, sizeof(scmd->cmnd));
30b0c37b 1084 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
644373a4 1085 scmd->result = 0;
a9a4ea11 1086 scmd->resid_len = 0;
30b0c37b 1087
55db6c1b 1088 if (sense_bytes) {
30b0c37b
BH
1089 scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
1090 sense_bytes);
e1c23468 1091 sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
30b0c37b
BH
1092 scmd->sdb.length);
1093 scmd->sdb.table.sgl = &ses->sense_sgl;
55db6c1b 1094 scmd->sc_data_direction = DMA_FROM_DEVICE;
0c958ecc 1095 scmd->sdb.table.nents = scmd->sdb.table.orig_nents = 1;
55db6c1b 1096 scmd->cmnd[0] = REQUEST_SENSE;
30b0c37b 1097 scmd->cmnd[4] = scmd->sdb.length;
55db6c1b 1098 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
631c228c 1099 } else {
631c228c 1100 scmd->sc_data_direction = DMA_NONE;
55db6c1b 1101 if (cmnd) {
ce70fd9a 1102 BUG_ON(cmnd_size > sizeof(scmd->cmnd));
55db6c1b
BH
1103 memcpy(scmd->cmnd, cmnd, cmnd_size);
1104 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
1105 }
631c228c
CH
1106 }
1107
1108 scmd->underflow = 0;
631c228c 1109
55db6c1b 1110 if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
1da177e4 1111 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
f59114b7 1112 (sdev->lun << 5 & 0xe0);
1da177e4 1113
631c228c
CH
1114 /*
1115 * Zero the sense buffer. The scsi spec mandates that any
1116 * untransferred sense data should be interpreted as being zero.
1117 */
b80ca4f7 1118 memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
e1c23468
BH
1119}
1120EXPORT_SYMBOL(scsi_eh_prep_cmnd);
1121
1122/**
3b729f76 1123 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
e1c23468 1124 * @scmd: SCSI command structure to restore
477e608c 1125 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
e1c23468 1126 *
477e608c 1127 * Undo any damage done by above scsi_eh_prep_cmnd().
dc8875e1 1128 */
e1c23468
BH
1129void 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;
ce70fd9a 1135 memcpy(scmd->cmnd, ses->cmnd, sizeof(ses->cmnd));
e1c23468 1136 scmd->sc_data_direction = ses->data_direction;
30b0c37b 1137 scmd->sdb = ses->sdb;
e1c23468 1138 scmd->result = ses->result;
a9a4ea11 1139 scmd->resid_len = ses->resid_len;
12265709 1140 scmd->underflow = ses->underflow;
db007fc5 1141 scmd->prot_op = ses->prot_op;
8e8c9d01 1142 scmd->eh_eflags = ses->eh_eflags;
e1c23468
BH
1143}
1144EXPORT_SYMBOL(scsi_eh_restore_cmnd);
631c228c 1145
e1c23468 1146/**
3b729f76 1147 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
e1c23468
BH
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
dc8875e1 1159 */
b8e162f9
BVA
1160static enum scsi_disposition scsi_send_eh_cmnd(struct scsi_cmnd *scmd,
1161 unsigned char *cmnd, int cmnd_size, int timeout, unsigned sense_bytes)
e1c23468
BH
1162{
1163 struct scsi_device *sdev = scmd->device;
1164 struct Scsi_Host *shost = sdev->host;
1165 DECLARE_COMPLETION_ONSTACK(done);
bbe9fb0d 1166 unsigned long timeleft = timeout, delay;
e1c23468 1167 struct scsi_eh_save ses;
fc73648a 1168 const unsigned long stall_for = msecs_to_jiffies(100);
e1c23468
BH
1169 int rtn;
1170
fc73648a 1171retry:
e1c23468 1172 scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
7dfdc9a5 1173 shost->eh_action = &done;
1da177e4 1174
1da177e4 1175 scsi_log_send(scmd);
bf23e619 1176 scmd->submitter = SUBMITTED_BY_SCSI_ERROR_HANDLER;
066c5b46 1177 scmd->flags |= SCMD_LAST;
bbe9fb0d
BVA
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
280e91b0 1198 rtn = FAILED;
bbe9fb0d
BVA
1199 mutex_unlock(&sdev->state_mutex);
1200
fc73648a
HR
1201 if (rtn) {
1202 if (timeleft > stall_for) {
1203 scsi_eh_restore_cmnd(scmd, &ses);
bf23e619 1204
fc73648a
HR
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;
511833ac 1211 rtn = FAILED;
fc73648a
HR
1212 } else {
1213 timeleft = wait_for_completion_timeout(&done, timeout);
ac61d195 1214 rtn = SUCCESS;
fc73648a 1215 }
1da177e4 1216
f59114b7 1217 shost->eh_action = NULL;
1da177e4 1218
fc73648a 1219 scsi_log_completion(scmd, rtn);
1da177e4 1220
91921e01 1221 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
470613b4
HR
1222 "%s timeleft: %ld\n",
1223 __func__, timeleft));
1da177e4
LT
1224
1225 /*
fc73648a
HR
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)
1da177e4 1233 */
7dfdc9a5 1234 if (timeleft) {
1da177e4 1235 rtn = scsi_eh_completed_normally(scmd);
91921e01
HR
1236 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1237 "%s: scsi_eh_completed_normally %x\n", __func__, rtn));
7dfdc9a5 1238
1da177e4
LT
1239 switch (rtn) {
1240 case SUCCESS:
1241 case NEEDS_RETRY:
1242 case FAILED:
1243 break;
6e883b0e
HR
1244 case ADD_TO_MLQUEUE:
1245 rtn = NEEDS_RETRY;
1246 break;
1da177e4
LT
1247 default:
1248 rtn = FAILED;
1249 break;
1250 }
511833ac 1251 } else if (rtn != FAILED) {
292148f8 1252 scsi_abort_eh_cmnd(scmd);
7dfdc9a5 1253 rtn = FAILED;
1da177e4
LT
1254 }
1255
e1c23468 1256 scsi_eh_restore_cmnd(scmd, &ses);
18a4d0a2 1257
1da177e4
LT
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.
dc8875e1 1269 */
b8e162f9 1270static enum scsi_disposition scsi_request_sense(struct scsi_cmnd *scmd)
1da177e4 1271{
0816c925 1272 return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
1da177e4
LT
1273}
1274
b8e162f9
BVA
1275static enum scsi_disposition
1276scsi_eh_action(struct scsi_cmnd *scmd, enum scsi_disposition rtn)
2451079b 1277{
aa8e25e5 1278 if (!blk_rq_is_passthrough(scsi_cmd_to_rq(scmd))) {
2451079b
JB
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
1da177e4
LT
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,
eb44820c 1294 * and that would disturb what we are doing. Thus we really want to
1da177e4
LT
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.
dc8875e1 1297 */
041c5fc3 1298void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
1da177e4 1299{
1da177e4
LT
1300 list_move_tail(&scmd->eh_entry, done_q);
1301}
041c5fc3 1302EXPORT_SYMBOL(scsi_eh_finish_cmd);
1da177e4
LT
1303
1304/**
1305 * scsi_eh_get_sense - Get device sense data.
1306 * @work_q: Queue of commands to process.
eb44820c 1307 * @done_q: Queue of processed commands.
1da177e4
LT
1308 *
1309 * Description:
1310 * See if we need to request sense information. if so, then get it
0bf8c869 1311 * now, so we have a better idea of what to do.
1da177e4
LT
1312 *
1313 * Notes:
1314 * This has the unfortunate side effect that if a shost adapter does
eb44820c 1315 * not automatically request sense information, we end up shutting
1da177e4
LT
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.
dc8875e1 1323 */
dca84e46
DW
1324int scsi_eh_get_sense(struct list_head *work_q,
1325 struct list_head *done_q)
1da177e4 1326{
937abeaa 1327 struct scsi_cmnd *scmd, *next;
b4562022 1328 struct Scsi_Host *shost;
b8e162f9 1329 enum scsi_disposition rtn;
1da177e4 1330
709c75b5 1331 /*
1332 * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
1333 * should not get sense.
1334 */
937abeaa 1335 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
a0658632 1336 if ((scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) ||
1da177e4
LT
1337 SCSI_SENSE_VALID(scmd))
1338 continue;
1339
b4562022 1340 shost = scmd->device->host;
b4562022 1341 if (scsi_host_eh_past_deadline(shost)) {
b4562022 1342 SCSI_LOG_ERROR_RECOVERY(3,
a222b1e2
HR
1343 scmd_printk(KERN_INFO, scmd,
1344 "%s: skip request sense, past eh deadline\n",
1345 current->comm));
b4562022
HR
1346 break;
1347 }
d0672a03 1348 if (!scsi_status_is_check_condition(scmd->result))
d555a2ab
JB
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
3bf743e7
JG
1357 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
1358 "%s: requesting sense\n",
1359 current->comm));
1da177e4
LT
1360 rtn = scsi_request_sense(scmd);
1361 if (rtn != SUCCESS)
1362 continue;
1363
91921e01 1364 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
470613b4 1365 "sense requested, result %x\n", scmd->result));
d811b848 1366 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd));
1da177e4
LT
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)
2a242d59
MC
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;
1da177e4
LT
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}
dca84e46 1395EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
1da177e4 1396
1da177e4
LT
1397/**
1398 * scsi_eh_tur - Send TUR to device.
eb44820c 1399 * @scmd: &scsi_cmnd to send TUR
1da177e4
LT
1400 *
1401 * Return value:
1402 * 0 - Device is ready. 1 - Device NOT ready.
dc8875e1 1403 */
1da177e4
LT
1404static int scsi_eh_tur(struct scsi_cmnd *scmd)
1405{
1406 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
b8e162f9
BVA
1407 int retry_cnt = 1;
1408 enum scsi_disposition rtn;
1da177e4
LT
1409
1410retry_tur:
0816c925
MP
1411 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
1412 scmd->device->eh_timeout, 0);
1da177e4 1413
91921e01 1414 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
470613b4 1415 "%s return: %x\n", __func__, rtn));
631c228c
CH
1416
1417 switch (rtn) {
1418 case NEEDS_RETRY:
1da177e4
LT
1419 if (retry_cnt--)
1420 goto retry_tur;
df561f66 1421 fallthrough;
631c228c 1422 case SUCCESS:
e47373ec 1423 return 0;
631c228c
CH
1424 default:
1425 return 1;
e47373ec 1426 }
1da177e4
LT
1427}
1428
3eef6257
DJ
1429/**
1430 * scsi_eh_test_devices - check if devices are responding from error recovery.
1431 * @cmd_list: scsi commands in error recovery.
74cf298f
RD
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.
3eef6257
DJ
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 **/
1442static 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
b4562022 1454 if (!try_stu) {
b4562022
HR
1455 if (scsi_host_eh_past_deadline(sdev->host)) {
1456 /* Push items back onto work_q */
1457 list_splice_init(cmd_list, work_q);
b4562022 1458 SCSI_LOG_ERROR_RECOVERY(3,
a222b1e2
HR
1459 sdev_printk(KERN_INFO, sdev,
1460 "%s: skip test device, past eh deadline",
1461 current->comm));
b4562022
HR
1462 break;
1463 }
b4562022
HR
1464 }
1465
3eef6257
DJ
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) {
2451079b
JB
1473 if (finish_cmds &&
1474 (try_stu ||
1475 scsi_eh_action(scmd, SUCCESS) == SUCCESS))
3eef6257
DJ
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
1da177e4
LT
1484/**
1485 * scsi_eh_try_stu - Send START_UNIT to device.
eb44820c 1486 * @scmd: &scsi_cmnd to send START_UNIT
1da177e4
LT
1487 *
1488 * Return value:
1489 * 0 - Device is ready. 1 - Device NOT ready.
dc8875e1 1490 */
1da177e4
LT
1491static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
1492{
1493 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
1da177e4 1494
631c228c 1495 if (scmd->device->allow_restart) {
b8e162f9
BVA
1496 int i;
1497 enum scsi_disposition rtn = NEEDS_RETRY;
ed773e66
BK
1498
1499 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
adcc796b
CX
1500 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6,
1501 scmd->device->eh_timeout, 0);
1da177e4 1502
631c228c
CH
1503 if (rtn == SUCCESS)
1504 return 0;
1505 }
1da177e4 1506
1da177e4
LT
1507 return 1;
1508}
1509
1510 /**
1511 * scsi_eh_stu - send START_UNIT if needed
eb44820c 1512 * @shost: &scsi host being recovered.
74cf298f 1513 * @work_q: &list_head for pending commands.
eb44820c 1514 * @done_q: &list_head for processed commands.
1da177e4
LT
1515 *
1516 * Notes:
1517 * If commands are failing due to not ready, initializing command required,
0bf8c869 1518 * try revalidating the device, which will end up sending a start unit.
dc8875e1 1519 */
1da177e4
LT
1520static int scsi_eh_stu(struct Scsi_Host *shost,
1521 struct list_head *work_q,
1522 struct list_head *done_q)
1523{
937abeaa 1524 struct scsi_cmnd *scmd, *stu_scmd, *next;
1da177e4
LT
1525 struct scsi_device *sdev;
1526
1527 shost_for_each_device(sdev, shost) {
b4562022 1528 if (scsi_host_eh_past_deadline(shost)) {
b4562022 1529 SCSI_LOG_ERROR_RECOVERY(3,
a222b1e2
HR
1530 sdev_printk(KERN_INFO, sdev,
1531 "%s: skip START_UNIT, past eh deadline\n",
1532 current->comm));
4dea170f 1533 scsi_device_put(sdev);
b4562022
HR
1534 break;
1535 }
1da177e4
LT
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
91921e01 1547 SCSI_LOG_ERROR_RECOVERY(3,
a222b1e2
HR
1548 sdev_printk(KERN_INFO, sdev,
1549 "%s: Sending START_UNIT\n",
1550 current->comm));
1da177e4
LT
1551
1552 if (!scsi_eh_try_stu(stu_scmd)) {
1553 if (!scsi_device_online(sdev) ||
1554 !scsi_eh_tur(stu_scmd)) {
937abeaa
CH
1555 list_for_each_entry_safe(scmd, next,
1556 work_q, eh_entry) {
2451079b
JB
1557 if (scmd->device == sdev &&
1558 scsi_eh_action(scmd, SUCCESS) == SUCCESS)
1da177e4
LT
1559 scsi_eh_finish_cmd(scmd, done_q);
1560 }
1561 }
1562 } else {
1563 SCSI_LOG_ERROR_RECOVERY(3,
a222b1e2
HR
1564 sdev_printk(KERN_INFO, sdev,
1565 "%s: START_UNIT failed\n",
1566 current->comm));
1da177e4
LT
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.
74cf298f 1577 * @work_q: &list_head for pending commands.
eb44820c 1578 * @done_q: &list_head for processed commands.
1da177e4
LT
1579 *
1580 * Notes:
eb44820c 1581 * Try a bus device reset. Still, look to see whether we have multiple
1da177e4
LT
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
0bf8c869 1584 * a bus_reset instead.
dc8875e1 1585 */
1da177e4
LT
1586static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1587 struct list_head *work_q,
1588 struct list_head *done_q)
1589{
937abeaa 1590 struct scsi_cmnd *scmd, *bdr_scmd, *next;
1da177e4 1591 struct scsi_device *sdev;
b8e162f9 1592 enum scsi_disposition rtn;
1da177e4
LT
1593
1594 shost_for_each_device(sdev, shost) {
b4562022 1595 if (scsi_host_eh_past_deadline(shost)) {
b4562022 1596 SCSI_LOG_ERROR_RECOVERY(3,
a222b1e2
HR
1597 sdev_printk(KERN_INFO, sdev,
1598 "%s: skip BDR, past eh deadline\n",
1599 current->comm));
4dea170f 1600 scsi_device_put(sdev);
b4562022
HR
1601 break;
1602 }
1da177e4
LT
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
91921e01 1613 SCSI_LOG_ERROR_RECOVERY(3,
a222b1e2
HR
1614 sdev_printk(KERN_INFO, sdev,
1615 "%s: Sending BDR\n", current->comm));
1da177e4 1616 rtn = scsi_try_bus_device_reset(bdr_scmd);
2f2eb587 1617 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
1da177e4 1618 if (!scsi_device_online(sdev) ||
2f2eb587 1619 rtn == FAST_IO_FAIL ||
1da177e4 1620 !scsi_eh_tur(bdr_scmd)) {
937abeaa
CH
1621 list_for_each_entry_safe(scmd, next,
1622 work_q, eh_entry) {
2451079b
JB
1623 if (scmd->device == sdev &&
1624 scsi_eh_action(scmd, rtn) != FAILED)
1da177e4
LT
1625 scsi_eh_finish_cmd(scmd,
1626 done_q);
1627 }
1628 }
1629 } else {
91921e01 1630 SCSI_LOG_ERROR_RECOVERY(3,
a222b1e2
HR
1631 sdev_printk(KERN_INFO, sdev,
1632 "%s: BDR failed\n", current->comm));
1da177e4
LT
1633 }
1634 }
1635
1636 return list_empty(work_q);
1637}
1638
30bd7df8
MC
1639/**
1640 * scsi_eh_target_reset - send target reset if needed
1641 * @shost: scsi host being recovered.
74cf298f 1642 * @work_q: &list_head for pending commands.
30bd7df8
MC
1643 * @done_q: &list_head for processed commands.
1644 *
1645 * Notes:
1646 * Try a target reset.
1647 */
1648static int scsi_eh_target_reset(struct Scsi_Host *shost,
1649 struct list_head *work_q,
1650 struct list_head *done_q)
1651{
98db5195 1652 LIST_HEAD(tmp_list);
3eef6257 1653 LIST_HEAD(check_list);
30bd7df8 1654
98db5195
JB
1655 list_splice_init(work_q, &tmp_list);
1656
1657 while (!list_empty(&tmp_list)) {
1658 struct scsi_cmnd *next, *scmd;
b8e162f9 1659 enum scsi_disposition rtn;
98db5195 1660 unsigned int id;
b4562022 1661
b4562022 1662 if (scsi_host_eh_past_deadline(shost)) {
b4562022
HR
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,
a222b1e2
HR
1668 "%s: Skip target reset, past eh deadline\n",
1669 current->comm));
b4562022
HR
1670 return list_empty(work_q);
1671 }
98db5195
JB
1672
1673 scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
1674 id = scmd_id(scmd);
30bd7df8 1675
91921e01
HR
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));
98db5195
JB
1680 rtn = scsi_try_target_reset(scmd);
1681 if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
91921e01
HR
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));
98db5195
JB
1687 list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
1688 if (scmd_id(scmd) != id)
1689 continue;
1690
3eef6257
DJ
1691 if (rtn == SUCCESS)
1692 list_move_tail(&scmd->eh_entry, &check_list);
1693 else if (rtn == FAST_IO_FAIL)
98db5195
JB
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 }
30bd7df8 1700
3eef6257 1701 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
30bd7df8
MC
1702}
1703
1da177e4 1704/**
0bf8c869 1705 * scsi_eh_bus_reset - send a bus reset
eb44820c 1706 * @shost: &scsi host being recovered.
74cf298f 1707 * @work_q: &list_head for pending commands.
eb44820c 1708 * @done_q: &list_head for processed commands.
dc8875e1 1709 */
1da177e4
LT
1710static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1711 struct list_head *work_q,
1712 struct list_head *done_q)
1713{
937abeaa 1714 struct scsi_cmnd *scmd, *chan_scmd, *next;
3eef6257 1715 LIST_HEAD(check_list);
1da177e4 1716 unsigned int channel;
b8e162f9 1717 enum scsi_disposition rtn;
1da177e4
LT
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
0bf8c869 1723 * the reset.
1da177e4
LT
1724 */
1725
1726 for (channel = 0; channel <= shost->max_channel; channel++) {
b4562022 1727 if (scsi_host_eh_past_deadline(shost)) {
b4562022
HR
1728 list_splice_init(&check_list, work_q);
1729 SCSI_LOG_ERROR_RECOVERY(3,
1730 shost_printk(KERN_INFO, shost,
a222b1e2
HR
1731 "%s: skip BRST, past eh deadline\n",
1732 current->comm));
b4562022
HR
1733 return list_empty(work_q);
1734 }
b4562022 1735
1da177e4
LT
1736 chan_scmd = NULL;
1737 list_for_each_entry(scmd, work_q, eh_entry) {
422c0d61 1738 if (channel == scmd_channel(scmd)) {
1da177e4
LT
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;
91921e01
HR
1750 SCSI_LOG_ERROR_RECOVERY(3,
1751 shost_printk(KERN_INFO, shost,
1752 "%s: Sending BRST chan: %d\n",
1753 current->comm, channel));
1da177e4 1754 rtn = scsi_try_bus_reset(chan_scmd);
2f2eb587 1755 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
937abeaa 1756 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
3eef6257
DJ
1757 if (channel == scmd_channel(scmd)) {
1758 if (rtn == FAST_IO_FAIL)
1da177e4
LT
1759 scsi_eh_finish_cmd(scmd,
1760 done_q);
3eef6257
DJ
1761 else
1762 list_move_tail(&scmd->eh_entry,
1763 &check_list);
1764 }
1da177e4
LT
1765 }
1766 } else {
91921e01
HR
1767 SCSI_LOG_ERROR_RECOVERY(3,
1768 shost_printk(KERN_INFO, shost,
1769 "%s: BRST failed chan: %d\n",
1770 current->comm, channel));
1da177e4
LT
1771 }
1772 }
3eef6257 1773 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1da177e4
LT
1774}
1775
1776/**
0bf8c869 1777 * scsi_eh_host_reset - send a host reset
74cf298f
RD
1778 * @shost: host to be reset.
1779 * @work_q: &list_head for pending commands.
1780 * @done_q: &list_head for processed commands.
dc8875e1 1781 */
91921e01
HR
1782static int scsi_eh_host_reset(struct Scsi_Host *shost,
1783 struct list_head *work_q,
1da177e4
LT
1784 struct list_head *done_q)
1785{
937abeaa 1786 struct scsi_cmnd *scmd, *next;
3eef6257 1787 LIST_HEAD(check_list);
b8e162f9 1788 enum scsi_disposition rtn;
1da177e4
LT
1789
1790 if (!list_empty(work_q)) {
1791 scmd = list_entry(work_q->next,
1792 struct scsi_cmnd, eh_entry);
1793
91921e01
HR
1794 SCSI_LOG_ERROR_RECOVERY(3,
1795 shost_printk(KERN_INFO, shost,
1796 "%s: Sending HRST\n",
1797 current->comm));
1da177e4
LT
1798
1799 rtn = scsi_try_host_reset(scmd);
3eef6257
DJ
1800 if (rtn == SUCCESS) {
1801 list_splice_init(work_q, &check_list);
1802 } else if (rtn == FAST_IO_FAIL) {
937abeaa 1803 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1da177e4
LT
1804 scsi_eh_finish_cmd(scmd, done_q);
1805 }
1806 } else {
91921e01
HR
1807 SCSI_LOG_ERROR_RECOVERY(3,
1808 shost_printk(KERN_INFO, shost,
1809 "%s: HRST failed\n",
1810 current->comm));
1da177e4
LT
1811 }
1812 }
3eef6257 1813 return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
1da177e4
LT
1814}
1815
1816/**
1817 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
74cf298f
RD
1818 * @work_q: &list_head for pending commands.
1819 * @done_q: &list_head for processed commands.
dc8875e1 1820 */
1da177e4
LT
1821static void scsi_eh_offline_sdevs(struct list_head *work_q,
1822 struct list_head *done_q)
1823{
937abeaa 1824 struct scsi_cmnd *scmd, *next;
0db6ca8a 1825 struct scsi_device *sdev;
1da177e4 1826
937abeaa 1827 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
31765d7d
MW
1828 sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
1829 "not ready after error recovery\n");
0db6ca8a
BVA
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
1da177e4
LT
1836 scsi_eh_finish_cmd(scmd, done_q);
1837 }
1838 return;
1839}
1840
4a27446f 1841/**
e494f6a7 1842 * scsi_noretry_cmd - determine if command should be failed fast
4a27446f
MC
1843 * @scmd: SCSI cmd to examine.
1844 */
88b32c3c 1845bool scsi_noretry_cmd(struct scsi_cmnd *scmd)
4a27446f 1846{
aa8e25e5
BVA
1847 struct request *req = scsi_cmd_to_rq(scmd);
1848
4a27446f
MC
1849 switch (host_byte(scmd->result)) {
1850 case DID_OK:
1851 break;
e494f6a7
HR
1852 case DID_TIME_OUT:
1853 goto check_type;
4a27446f 1854 case DID_BUS_BUSY:
88b32c3c 1855 return !!(req->cmd_flags & REQ_FAILFAST_TRANSPORT);
4a27446f 1856 case DID_PARITY:
88b32c3c 1857 return !!(req->cmd_flags & REQ_FAILFAST_DEV);
4a27446f 1858 case DID_ERROR:
3d45cefc 1859 if (get_status_byte(scmd) == SAM_STAT_RESERVATION_CONFLICT)
88b32c3c 1860 return false;
df561f66 1861 fallthrough;
4a27446f 1862 case DID_SOFT_ERROR:
88b32c3c 1863 return !!(req->cmd_flags & REQ_FAILFAST_DRIVER);
4a27446f
MC
1864 }
1865
390e2d1a
NC
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
d0672a03 1870 if (!scsi_status_is_check_condition(scmd->result))
88b32c3c 1871 return false;
4a27446f 1872
e494f6a7
HR
1873check_type:
1874 /*
1875 * assume caller has checked sense and determined
1876 * the check condition was retryable.
1877 */
aa8e25e5 1878 if (req->cmd_flags & REQ_FAILFAST_DEV || blk_rq_is_passthrough(req))
88b32c3c 1879 return true;
342c81ee 1880
88b32c3c 1881 return false;
4a27446f
MC
1882}
1883
1da177e4
LT
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.
dc8875e1 1897 */
b8e162f9 1898enum scsi_disposition scsi_decide_disposition(struct scsi_cmnd *scmd)
1da177e4 1899{
b8e162f9 1900 enum scsi_disposition rtn;
1da177e4
LT
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)) {
91921e01
HR
1907 SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO, scmd,
1908 "%s: device offline - report as SUCCESS\n", __func__));
1da177e4
LT
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;
e494f6a7
HR
1930 case DID_ABORT:
1931 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
8922a908 1932 set_host_byte(scmd, DID_TIME_OUT);
e494f6a7
HR
1933 return SUCCESS;
1934 }
df561f66 1935 fallthrough;
1da177e4
LT
1936 case DID_NO_CONNECT:
1937 case DID_BAD_TARGET:
1da177e4
LT
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;
ad95028a 1944 case DID_SOFT_ERROR:
1da177e4
LT
1945 /*
1946 * when the low level driver returns did_soft_error,
0bf8c869 1947 * it is responsible for keeping an internal retry counter
1da177e4 1948 * in order to avoid endless loops (db)
1da177e4 1949 */
1da177e4
LT
1950 goto maybe_retry;
1951 case DID_IMM_RETRY:
1952 return NEEDS_RETRY;
1953
bf341919 1954 case DID_REQUEUE:
1955 return ADD_TO_MLQUEUE;
a4dfaa6f
MC
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
939c2288
MC
1961 * based on its timers and recovery capablilities if
1962 * there are enough retries.
a4dfaa6f 1963 */
939c2288 1964 goto maybe_retry;
a4dfaa6f
MC
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;
962c8dcd
MK
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;
1da177e4 1977 case DID_ERROR:
3d45cefc 1978 if (get_status_byte(scmd) == SAM_STAT_RESERVATION_CONFLICT)
1da177e4
LT
1979 /*
1980 * execute reservation conflict processing code
1981 * lower down
1982 */
1983 break;
df561f66 1984 fallthrough;
1da177e4
LT
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
1da177e4
LT
2006 /*
2007 * check the status byte to see if this indicates anything special.
2008 */
3d45cefc
HR
2009 switch (get_status_byte(scmd)) {
2010 case SAM_STAT_TASK_SET_FULL:
42a6a918 2011 scsi_handle_queue_full(scmd->device);
1da177e4
LT
2012 /*
2013 * the case of trying to send too many commands to a
2014 * tagged queueing device.
2015 */
df561f66 2016 fallthrough;
3d45cefc 2017 case SAM_STAT_BUSY:
1da177e4
LT
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;
3d45cefc 2025 case SAM_STAT_GOOD:
279afdfe
EM
2026 if (scmd->cmnd[0] == REPORT_LUNS)
2027 scmd->device->sdev_target->expecting_lun_change = 0;
4a84067d 2028 scsi_handle_queue_ramp_up(scmd->device);
390e2d1a
NC
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);
df561f66 2037 fallthrough;
3d45cefc 2038 case SAM_STAT_COMMAND_TERMINATED:
1da177e4 2039 return SUCCESS;
3d45cefc 2040 case SAM_STAT_TASK_ABORTED:
a9b589d9 2041 goto maybe_retry;
3d45cefc 2042 case SAM_STAT_CHECK_CONDITION:
1da177e4
LT
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;
3d45cefc
HR
2051 case SAM_STAT_CONDITION_MET:
2052 case SAM_STAT_INTERMEDIATE:
2053 case SAM_STAT_INTERMEDIATE_CONDITION_MET:
2054 case SAM_STAT_ACA_ACTIVE:
1da177e4
LT
2055 /*
2056 * who knows? FIXME(eric)
2057 */
2058 return SUCCESS;
2059
3d45cefc 2060 case SAM_STAT_RESERVATION_CONFLICT:
9ccfc756
JB
2061 sdev_printk(KERN_INFO, scmd->device,
2062 "reservation conflict\n");
7dfaae6a 2063 set_scsi_ml_byte(scmd, SCSIML_STAT_RESV_CONFLICT);
1da177e4 2064 return SUCCESS; /* causes immediate i/o error */
1da177e4
LT
2065 }
2066 return FAILED;
2067
8ef7fe4b 2068maybe_retry:
1da177e4
LT
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) */
2a242d59 2074 if (scsi_cmd_retry_allowed(scmd) && !scsi_noretry_cmd(scmd)) {
1da177e4
LT
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
de671d61
JA
2084static enum rq_end_io_ret eh_lock_door_done(struct request *req,
2085 blk_status_t status)
f078727b 2086{
0bf6d96c 2087 blk_mq_free_request(req);
de671d61 2088 return RQ_END_IO_NONE;
f078727b
FT
2089}
2090
1da177e4
LT
2091/**
2092 * scsi_eh_lock_door - Prevent medium removal for the specified device
2093 * @sdev: SCSI device to prevent medium removal
2094 *
2095 * Locking:
91bc31fb 2096 * We must be called from process context.
1da177e4
LT
2097 *
2098 * Notes:
2099 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
2100 * head of the devices request queue, and continue.
dc8875e1 2101 */
1da177e4
LT
2102static void scsi_eh_lock_door(struct scsi_device *sdev)
2103{
ce70fd9a 2104 struct scsi_cmnd *scmd;
f078727b 2105 struct request *req;
1da177e4 2106
68ec3b81 2107 req = scsi_alloc_request(sdev->request_queue, REQ_OP_DRV_IN, 0);
a492f075 2108 if (IS_ERR(req))
eb571eea 2109 return;
ce70fd9a 2110 scmd = blk_mq_rq_to_pdu(req);
1da177e4 2111
ce70fd9a
CH
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]);
e2e53086 2119 scmd->allowed = 5;
f078727b 2120
e8064021 2121 req->rq_flags |= RQF_QUIET;
f078727b 2122 req->timeout = 10 * HZ;
e2e53086 2123 req->end_io = eh_lock_door_done;
f078727b 2124
e2e53086 2125 blk_execute_rq_nowait(req, true);
f078727b 2126}
1da177e4
LT
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.
dc8875e1 2135 */
1da177e4
LT
2136static void scsi_restart_operations(struct Scsi_Host *shost)
2137{
2138 struct scsi_device *sdev;
939647ee 2139 unsigned long flags;
1da177e4
LT
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) {
48379270 2147 if (scsi_device_online(sdev) && sdev->was_reset && sdev->locked) {
1da177e4 2148 scsi_eh_lock_door(sdev);
48379270
CH
2149 sdev->was_reset = 0;
2150 }
1da177e4
LT
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 */
b4562022 2158 SCSI_LOG_ERROR_RECOVERY(3,
91921e01 2159 shost_printk(KERN_INFO, shost, "waking up host to restart\n"));
1da177e4 2160
939647ee
JB
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);
1da177e4
LT
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);
57fc2e33
DW
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);
1da177e4
LT
2190}
2191
2192/**
2193 * scsi_eh_ready_devs - check device ready state and recover if not.
74cf298f
RD
2194 * @shost: host to be recovered.
2195 * @work_q: &list_head for pending commands.
eb44820c 2196 * @done_q: &list_head for processed commands.
dc8875e1 2197 */
dca84e46
DW
2198void scsi_eh_ready_devs(struct Scsi_Host *shost,
2199 struct list_head *work_q,
2200 struct list_head *done_q)
1da177e4
LT
2201{
2202 if (!scsi_eh_stu(shost, work_q, done_q))
2203 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
30bd7df8
MC
2204 if (!scsi_eh_target_reset(shost, work_q, done_q))
2205 if (!scsi_eh_bus_reset(shost, work_q, done_q))
91921e01 2206 if (!scsi_eh_host_reset(shost, work_q, done_q))
30bd7df8
MC
2207 scsi_eh_offline_sdevs(work_q,
2208 done_q);
1da177e4 2209}
dca84e46 2210EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
1da177e4
LT
2211
2212/**
2213 * scsi_eh_flush_done_q - finish processed commands or retry them.
2214 * @done_q: list_head of processed commands.
dc8875e1 2215 */
041c5fc3 2216void scsi_eh_flush_done_q(struct list_head *done_q)
1da177e4 2217{
937abeaa 2218 struct scsi_cmnd *scmd, *next;
1da177e4 2219
937abeaa 2220 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
6df0e077
NC
2221 struct scsi_device *sdev = scmd->device;
2222
937abeaa 2223 list_del_init(&scmd->eh_entry);
6df0e077
NC
2224 if (scsi_device_online(sdev) && !scsi_noretry_cmd(scmd) &&
2225 scsi_cmd_retry_allowed(scmd) &&
2226 scsi_eh_should_retry_cmd(scmd)) {
91921e01
HR
2227 SCSI_LOG_ERROR_RECOVERY(3,
2228 scmd_printk(KERN_INFO, scmd,
470613b4
HR
2229 "%s: flush retry cmd\n",
2230 current->comm));
1da177e4 2231 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
6df0e077 2232 blk_mq_kick_requeue_list(sdev->request_queue);
1da177e4 2233 } else {
793698ce
PM
2234 /*
2235 * If just we got sense for the device (called
2236 * scsi_eh_get_sense), scmd->result is already
4bd51e54 2237 * set, do not set DID_TIME_OUT.
793698ce 2238 */
3d848ca1
NC
2239 if (!scmd->result &&
2240 !(scmd->flags & SCMD_FORCE_EH_SUCCESS))
4bd51e54 2241 scmd->result |= (DID_TIME_OUT << 16);
91921e01
HR
2242 SCSI_LOG_ERROR_RECOVERY(3,
2243 scmd_printk(KERN_INFO, scmd,
470613b4
HR
2244 "%s: flush finish cmd\n",
2245 current->comm));
1da177e4
LT
2246 scsi_finish_command(scmd);
2247 }
2248 }
2249}
041c5fc3 2250EXPORT_SYMBOL(scsi_eh_flush_done_q);
1da177e4
LT
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.
dc8875e1 2274 */
1da177e4
LT
2275static 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))
a0658632 2288 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
1da177e4 2289
b4562022 2290 spin_lock_irqsave(shost->host_lock, flags);
bb3b621a 2291 if (shost->eh_deadline != -1)
b4562022
HR
2292 shost->last_reset = 0;
2293 spin_unlock_irqrestore(shost->host_lock, flags);
1da177e4
LT
2294 scsi_eh_flush_done_q(&eh_done_q);
2295}
2296
2297/**
ad42eb1b 2298 * scsi_error_handler - SCSI error handler thread
1da177e4
LT
2299 * @data: Host for which we are running.
2300 *
2301 * Notes:
ad42eb1b
CH
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.
dc8875e1 2304 */
1da177e4
LT
2305int scsi_error_handler(void *data)
2306{
ad42eb1b 2307 struct Scsi_Host *shost = data;
1da177e4 2308
1da177e4 2309 /*
ad42eb1b
CH
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
c03264a7 2313 * disables signal delivery for the created thread.
1da177e4 2314 */
537b604c
MH
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 */
b9d5c6b7 2322 set_current_state(TASK_INTERRUPTIBLE);
537b604c
MH
2323 if (kthread_should_stop())
2324 break;
2325
ee7863bc 2326 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
c84b023a 2327 shost->host_failed != scsi_host_busy(shost)) {
ad42eb1b 2328 SCSI_LOG_ERROR_RECOVERY(1,
91921e01
HR
2329 shost_printk(KERN_INFO, shost,
2330 "scsi_eh_%d: sleeping\n",
2331 shost->host_no));
3ed7a470 2332 schedule();
3ed7a470
JB
2333 continue;
2334 }
1da177e4 2335
3ed7a470 2336 __set_current_state(TASK_RUNNING);
ad42eb1b 2337 SCSI_LOG_ERROR_RECOVERY(1,
91921e01
HR
2338 shost_printk(KERN_INFO, shost,
2339 "scsi_eh_%d: waking up %d/%d/%d\n",
2340 shost->host_no, shost->host_eh_scheduled,
74665016 2341 shost->host_failed,
c84b023a 2342 scsi_host_busy(shost)));
1da177e4 2343
1da177e4
LT
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 */
ae0751ff 2349 if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
bc4f2401 2350 SCSI_LOG_ERROR_RECOVERY(1,
a222b1e2
HR
2351 shost_printk(KERN_ERR, shost,
2352 "scsi_eh_%d: unable to autoresume\n",
2353 shost->host_no));
bc4f2401
AS
2354 continue;
2355 }
2356
9227c33d
CH
2357 if (shost->transportt->eh_strategy_handler)
2358 shost->transportt->eh_strategy_handler(shost);
1da177e4
LT
2359 else
2360 scsi_unjam_host(shost);
2361
72d8c36e
WF
2362 /* All scmds have been handled */
2363 shost->host_failed = 0;
2364
1da177e4
LT
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);
ae0751ff
LM
2373 if (!shost->eh_noresume)
2374 scsi_autopm_put_host(shost);
1da177e4 2375 }
461a0ffb
SR
2376 __set_current_state(TASK_RUNNING);
2377
ad42eb1b 2378 SCSI_LOG_ERROR_RECOVERY(1,
91921e01
HR
2379 shost_printk(KERN_INFO, shost,
2380 "Error handler scsi_eh_%d exiting\n",
2381 shost->host_no));
3ed7a470 2382 shost->ehandler = NULL;
1da177e4
LT
2383 return 0;
2384}
2385
20b98768
RD
2386/**
2387 * scsi_report_bus_reset() - report bus reset observed
1da177e4 2388 *
20b98768
RD
2389 * Utility function used by low-level drivers to report that
2390 * they have observed a bus reset on the bus being handled.
1da177e4 2391 *
20b98768
RD
2392 * @shost: Host in question
2393 * @channel: channel on which reset was observed.
1da177e4
LT
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 */
2407void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
2408{
2409 struct scsi_device *sdev;
2410
2411 __shost_for_each_device(sdev, shost) {
30bd7df8
MC
2412 if (channel == sdev_channel(sdev))
2413 __scsi_report_device_reset(sdev, NULL);
1da177e4
LT
2414 }
2415}
2416EXPORT_SYMBOL(scsi_report_bus_reset);
2417
20b98768
RD
2418/**
2419 * scsi_report_device_reset() - report device reset observed
1da177e4 2420 *
20b98768
RD
2421 * Utility function used by low-level drivers to report that
2422 * they have observed a device reset on the device being handled.
1da177e4 2423 *
20b98768
RD
2424 * @shost: Host in question
2425 * @channel: channel on which reset was observed
2426 * @target: target on which reset was observed
1da177e4
LT
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 */
2440void 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) {
422c0d61 2445 if (channel == sdev_channel(sdev) &&
30bd7df8
MC
2446 target == sdev_id(sdev))
2447 __scsi_report_device_reset(sdev, NULL);
1da177e4
LT
2448 }
2449}
2450EXPORT_SYMBOL(scsi_report_device_reset);
2451
176aa9d6
CH
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)
1da177e4
LT
2456 */
2457int
176aa9d6 2458scsi_ioctl_reset(struct scsi_device *dev, int __user *arg)
1da177e4 2459{
bc4f2401 2460 struct scsi_cmnd *scmd;
d7a1bb0a 2461 struct Scsi_Host *shost = dev->host;
e9c787e6 2462 struct request *rq;
d7a1bb0a 2463 unsigned long flags;
b8e162f9
BVA
2464 int error = 0, val;
2465 enum scsi_disposition rtn;
176aa9d6
CH
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;
1da177e4 2473
bc4f2401 2474 if (scsi_autopm_get_host(shost) < 0)
176aa9d6 2475 return -EIO;
bc4f2401 2476
176aa9d6 2477 error = -EIO;
e9c787e6
CH
2478 rq = kzalloc(sizeof(struct request) + sizeof(struct scsi_cmnd) +
2479 shost->hostt->cmd_size, GFP_KERNEL);
2480 if (!rq)
95eeb5f5 2481 goto out_put_autopm_host;
e9c787e6 2482 blk_rq_init(NULL, rq);
95eeb5f5 2483
e9c787e6
CH
2484 scmd = (struct scsi_cmnd *)(rq + 1);
2485 scsi_init_command(dev, scmd);
64a87b24 2486
bf23e619 2487 scmd->submitter = SUBMITTED_BY_SCSI_RESET_IOCTL;
066c5b46 2488 scmd->flags |= SCMD_LAST;
30b0c37b 2489 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
1da177e4
LT
2490
2491 scmd->cmd_len = 0;
2492
2493 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
1da177e4 2494
d7a1bb0a
JS
2495 spin_lock_irqsave(shost->host_lock, flags);
2496 shost->tmf_in_progress = 1;
2497 spin_unlock_irqrestore(shost->host_lock, flags);
2498
176aa9d6
CH
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:
1da177e4 2504 rtn = scsi_try_bus_device_reset(scmd);
176aa9d6 2505 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
1da177e4 2506 break;
df561f66 2507 fallthrough;
176aa9d6 2508 case SG_SCSI_RESET_TARGET:
30bd7df8 2509 rtn = scsi_try_target_reset(scmd);
176aa9d6 2510 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
30bd7df8 2511 break;
df561f66 2512 fallthrough;
176aa9d6 2513 case SG_SCSI_RESET_BUS:
1da177e4 2514 rtn = scsi_try_bus_reset(scmd);
176aa9d6 2515 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
1da177e4 2516 break;
df561f66 2517 fallthrough;
176aa9d6 2518 case SG_SCSI_RESET_HOST:
1da177e4 2519 rtn = scsi_try_host_reset(scmd);
176aa9d6
CH
2520 if (rtn == SUCCESS)
2521 break;
df561f66 2522 fallthrough;
3bf2ff67 2523 default:
1da177e4 2524 rtn = FAILED;
176aa9d6 2525 break;
1da177e4
LT
2526 }
2527
176aa9d6
CH
2528 error = (rtn == SUCCESS) ? 0 : -EIO;
2529
d7a1bb0a
JS
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,
91921e01
HR
2539 shost_printk(KERN_INFO, shost,
2540 "waking up host to restart after TMF\n"));
d7a1bb0a
JS
2541
2542 wake_up(&shost->host_wait);
d7a1bb0a
JS
2543 scsi_run_host_queues(shost);
2544
e9c787e6 2545 kfree(rq);
0f121dd8 2546
04796336 2547out_put_autopm_host:
bc4f2401 2548 scsi_autopm_put_host(shost);
176aa9d6 2549 return error;
1da177e4 2550}
1da177e4 2551
4753cbc0
HR
2552bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd,
2553 struct scsi_sense_hdr *sshdr)
1da177e4
LT
2554{
2555 return scsi_normalize_sense(cmd->sense_buffer,
b80ca4f7 2556 SCSI_SENSE_BUFFERSIZE, sshdr);
1da177e4
LT
2557}
2558EXPORT_SYMBOL(scsi_command_normalize_sense);
2559
1da177e4 2560/**
eb44820c 2561 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
1da177e4
LT
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:
2908769c 2568 * true if information field found, false if not found.
dc8875e1 2569 */
2908769c
DLM
2570bool scsi_get_sense_info_fld(const u8 *sense_buffer, int sb_len,
2571 u64 *info_out)
1da177e4 2572{
1da177e4 2573 const u8 * ucp;
1da177e4
LT
2574
2575 if (sb_len < 7)
2908769c 2576 return false;
1da177e4
LT
2577 switch (sense_buffer[0] & 0x7f) {
2578 case 0x70:
2579 case 0x71:
2580 if (sense_buffer[0] & 0x80) {
2908769c
DLM
2581 *info_out = get_unaligned_be32(&sense_buffer[3]);
2582 return true;
2583 }
2584 return false;
1da177e4
LT
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])) {
2908769c
DLM
2590 *info_out = get_unaligned_be64(&ucp[4]);
2591 return true;
2592 }
2593 return false;
1da177e4 2594 default:
2908769c 2595 return false;
1da177e4
LT
2596 }
2597}
2598EXPORT_SYMBOL(scsi_get_sense_info_fld);