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