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