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