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