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