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