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