blob: e8350c562d24c0a4598d7a4c0e4066c1b4911d35 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scsi_error.c Copyright (C) 1997 Eric Youngdale
3 *
4 * SCSI error/timeout handling
5 * Initial versions: Eric Youngdale. Based upon conversations with
6 * Leonard Zubkoff and David Miller at Linux Expo,
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
13 * minor cleanups.
14 * September 30, 2002 Mike Anderson (andmike@us.ibm.com)
15 */
16
17#include <linux/module.h>
18#include <linux/sched.h>
19#include <linux/timer.h>
20#include <linux/string.h>
21#include <linux/slab.h>
22#include <linux/kernel.h>
Christoph Hellwigc5478de2005-09-06 14:04:26 +020023#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/interrupt.h>
25#include <linux/blkdev.h>
26#include <linux/delay.h>
27
28#include <scsi/scsi.h>
Christoph Hellwigbeb40482006-06-10 18:01:03 +020029#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <scsi/scsi_dbg.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_eh.h>
James Smartc829c392006-03-13 08:28:57 -050033#include <scsi/scsi_transport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <scsi/scsi_host.h>
35#include <scsi/scsi_ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include "scsi_priv.h"
38#include "scsi_logging.h"
39
40#define SENSE_TIMEOUT (10*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/*
43 * These should *probably* be handled by the host itself.
44 * Since it is allowed to sleep, it probably should.
45 */
46#define BUS_RESET_SETTLE_TIME (10)
47#define HOST_RESET_SETTLE_TIME (10)
48
49/* called with shost->host_lock held */
50void scsi_eh_wakeup(struct Scsi_Host *shost)
51{
52 if (shost->host_busy == shost->host_failed) {
James Bottomley3ed7a472005-09-19 09:50:04 -050053 wake_up_process(shost->ehandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 SCSI_LOG_ERROR_RECOVERY(5,
55 printk("Waking error handler thread\n"));
56 }
57}
Tejun Heof8bbfc22006-05-19 21:07:05 +090058
59/**
60 * scsi_schedule_eh - schedule EH for SCSI host
61 * @shost: SCSI host to invoke error handling on.
62 *
63 * Schedule SCSI EH without scmd.
64 **/
65void scsi_schedule_eh(struct Scsi_Host *shost)
66{
67 unsigned long flags;
68
69 spin_lock_irqsave(shost->host_lock, flags);
70
71 if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
72 scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
73 shost->host_eh_scheduled++;
74 scsi_eh_wakeup(shost);
75 }
76
77 spin_unlock_irqrestore(shost->host_lock, flags);
78}
79EXPORT_SYMBOL_GPL(scsi_schedule_eh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/**
82 * scsi_eh_scmd_add - add scsi cmd to error handling.
83 * @scmd: scmd to run eh on.
84 * @eh_flag: optional SCSI_EH flag.
85 *
86 * Return value:
87 * 0 on failure.
88 **/
89int scsi_eh_scmd_add(struct scsi_cmnd *scmd, int eh_flag)
90{
91 struct Scsi_Host *shost = scmd->device->host;
92 unsigned long flags;
James Bottomley939647e2005-09-18 15:05:20 -050093 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
James Bottomley3ed7a472005-09-19 09:50:04 -050095 if (!shost->ehandler)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return 0;
97
98 spin_lock_irqsave(shost->host_lock, flags);
James Bottomley939647e2005-09-18 15:05:20 -050099 if (scsi_host_set_state(shost, SHOST_RECOVERY))
100 if (scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY))
101 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
James Bottomley939647e2005-09-18 15:05:20 -0500103 ret = 1;
Christoph Hellwig3111b0d2005-06-19 13:43:26 +0200104 scmd->eh_eflags |= eh_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 shost->host_failed++;
107 scsi_eh_wakeup(shost);
James Bottomley939647e2005-09-18 15:05:20 -0500108 out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 spin_unlock_irqrestore(shost->host_lock, flags);
James Bottomley939647e2005-09-18 15:05:20 -0500110 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113/**
114 * scsi_add_timer - Start timeout timer for a single scsi command.
115 * @scmd: scsi command that is about to start running.
116 * @timeout: amount of time to allow this command to run.
117 * @complete: timeout function to call if timer isn't canceled.
118 *
119 * Notes:
120 * This should be turned into an inline function. Each scsi command
121 * has its own timer, and as it is added to the queue, we set up the
122 * timer. When the command completes, we cancel the timer.
123 **/
124void scsi_add_timer(struct scsi_cmnd *scmd, int timeout,
125 void (*complete)(struct scsi_cmnd *))
126{
127
128 /*
129 * If the clock was already running for this command, then
130 * first delete the timer. The timer handling code gets rather
131 * confused if we don't do this.
132 */
133 if (scmd->eh_timeout.function)
134 del_timer(&scmd->eh_timeout);
135
136 scmd->eh_timeout.data = (unsigned long)scmd;
137 scmd->eh_timeout.expires = jiffies + timeout;
138 scmd->eh_timeout.function = (void (*)(unsigned long)) complete;
139
140 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: scmd: %p, time:"
141 " %d, (%p)\n", __FUNCTION__,
142 scmd, timeout, complete));
143
144 add_timer(&scmd->eh_timeout);
145}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147/**
148 * scsi_delete_timer - Delete/cancel timer for a given function.
149 * @scmd: Cmd that we are canceling timer for
150 *
151 * Notes:
152 * This should be turned into an inline function.
153 *
154 * Return value:
155 * 1 if we were able to detach the timer. 0 if we blew it, and the
156 * timer function has already started to run.
157 **/
158int scsi_delete_timer(struct scsi_cmnd *scmd)
159{
160 int rtn;
161
162 rtn = del_timer(&scmd->eh_timeout);
163
164 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: scmd: %p,"
165 " rtn: %d\n", __FUNCTION__,
166 scmd, rtn));
167
168 scmd->eh_timeout.data = (unsigned long)NULL;
169 scmd->eh_timeout.function = NULL;
170
171 return rtn;
172}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174/**
175 * scsi_times_out - Timeout function for normal scsi commands.
176 * @scmd: Cmd that is timing out.
177 *
178 * Notes:
179 * We do not need to lock this. There is the potential for a race
180 * only in that the normal completion handling might run, but if the
181 * normal completion function determines that the timer has already
182 * fired, then it mustn't do anything.
183 **/
184void scsi_times_out(struct scsi_cmnd *scmd)
185{
James Bottomley6c5f8ce2007-03-16 17:44:41 -0500186 enum scsi_eh_timer_return (* eh_timed_out)(struct scsi_cmnd *);
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 scsi_log_completion(scmd, TIMEOUT_ERROR);
189
James Smartc829c392006-03-13 08:28:57 -0500190 if (scmd->device->host->transportt->eh_timed_out)
James Bottomley6c5f8ce2007-03-16 17:44:41 -0500191 eh_timed_out = scmd->device->host->transportt->eh_timed_out;
192 else if (scmd->device->host->hostt->eh_timed_out)
193 eh_timed_out = scmd->device->host->hostt->eh_timed_out;
194 else
195 eh_timed_out = NULL;
196
197 if (eh_timed_out)
198 switch (eh_timed_out(scmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 case EH_HANDLED:
200 __scsi_done(scmd);
201 return;
202 case EH_RESET_TIMER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 scsi_add_timer(scmd, scmd->timeout_per_command,
204 scsi_times_out);
205 return;
206 case EH_NOT_HANDLED:
207 break;
208 }
209
210 if (unlikely(!scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD))) {
James Bottomley939647e2005-09-18 15:05:20 -0500211 scmd->result |= DID_TIME_OUT << 16;
212 __scsi_done(scmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214}
215
216/**
217 * scsi_block_when_processing_errors - Prevent cmds from being queued.
218 * @sdev: Device on which we are performing recovery.
219 *
220 * Description:
221 * We block until the host is out of error recovery, and then check to
222 * see whether the host or the device is offline.
223 *
224 * Return value:
225 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
226 **/
227int scsi_block_when_processing_errors(struct scsi_device *sdev)
228{
229 int online;
230
James Bottomley939647e2005-09-18 15:05:20 -0500231 wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 online = scsi_device_online(sdev);
234
235 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: rtn: %d\n", __FUNCTION__,
236 online));
237
238 return online;
239}
240EXPORT_SYMBOL(scsi_block_when_processing_errors);
241
242#ifdef CONFIG_SCSI_LOGGING
243/**
244 * scsi_eh_prt_fail_stats - Log info on failures.
245 * @shost: scsi host being recovered.
246 * @work_q: Queue of scsi cmds to process.
247 **/
248static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
249 struct list_head *work_q)
250{
251 struct scsi_cmnd *scmd;
252 struct scsi_device *sdev;
253 int total_failures = 0;
254 int cmd_failed = 0;
255 int cmd_cancel = 0;
256 int devices_failed = 0;
257
258 shost_for_each_device(sdev, shost) {
259 list_for_each_entry(scmd, work_q, eh_entry) {
260 if (scmd->device == sdev) {
261 ++total_failures;
Christoph Hellwig3111b0d2005-06-19 13:43:26 +0200262 if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 ++cmd_cancel;
264 else
265 ++cmd_failed;
266 }
267 }
268
269 if (cmd_cancel || cmd_failed) {
270 SCSI_LOG_ERROR_RECOVERY(3,
James Bottomley9ccfc752005-10-02 11:45:08 -0500271 sdev_printk(KERN_INFO, sdev,
272 "%s: cmds failed: %d, cancel: %d\n",
273 __FUNCTION__, cmd_failed,
274 cmd_cancel));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 cmd_cancel = 0;
276 cmd_failed = 0;
277 ++devices_failed;
278 }
279 }
280
281 SCSI_LOG_ERROR_RECOVERY(2, printk("Total of %d commands on %d"
282 " devices require eh work\n",
283 total_failures, devices_failed));
284}
285#endif
286
287/**
288 * scsi_check_sense - Examine scsi cmd sense
289 * @scmd: Cmd to have sense checked.
290 *
291 * Return value:
292 * SUCCESS or FAILED or NEEDS_RETRY
293 *
294 * Notes:
295 * When a deferred error is detected the current command has
296 * not been executed and needs retrying.
297 **/
298static int scsi_check_sense(struct scsi_cmnd *scmd)
299{
300 struct scsi_sense_hdr sshdr;
301
302 if (! scsi_command_normalize_sense(scmd, &sshdr))
303 return FAILED; /* no valid sense data */
304
305 if (scsi_sense_is_deferred(&sshdr))
306 return NEEDS_RETRY;
307
308 /*
309 * Previous logic looked for FILEMARK, EOM or ILI which are
310 * mainly associated with tapes and returned SUCCESS.
311 */
312 if (sshdr.response_code == 0x70) {
313 /* fixed format */
314 if (scmd->sense_buffer[2] & 0xe0)
315 return SUCCESS;
316 } else {
317 /*
318 * descriptor format: look for "stream commands sense data
319 * descriptor" (see SSC-3). Assume single sense data
320 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
321 */
322 if ((sshdr.additional_length > 3) &&
323 (scmd->sense_buffer[8] == 0x4) &&
324 (scmd->sense_buffer[11] & 0xe0))
325 return SUCCESS;
326 }
327
328 switch (sshdr.sense_key) {
329 case NO_SENSE:
330 return SUCCESS;
331 case RECOVERED_ERROR:
332 return /* soft_error */ SUCCESS;
333
334 case ABORTED_COMMAND:
335 return NEEDS_RETRY;
336 case NOT_READY:
337 case UNIT_ATTENTION:
338 /*
339 * if we are expecting a cc/ua because of a bus reset that we
340 * performed, treat this just as a retry. otherwise this is
341 * information that we should pass up to the upper-level driver
342 * so that we can deal with it there.
343 */
344 if (scmd->device->expecting_cc_ua) {
345 scmd->device->expecting_cc_ua = 0;
346 return NEEDS_RETRY;
347 }
348 /*
349 * if the device is in the process of becoming ready, we
350 * should retry.
351 */
352 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
353 return NEEDS_RETRY;
354 /*
355 * if the device is not started, we need to wake
356 * the error handler to start the motor
357 */
358 if (scmd->device->allow_restart &&
359 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
360 return FAILED;
361 return SUCCESS;
362
363 /* these three are not supported */
364 case COPY_ABORTED:
365 case VOLUME_OVERFLOW:
366 case MISCOMPARE:
367 return SUCCESS;
368
369 case MEDIUM_ERROR:
Luben Tuikovfd1b4942006-11-29 19:45:23 -0800370 if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
371 sshdr.asc == 0x13 || /* AMNF DATA FIELD */
372 sshdr.asc == 0x14) { /* RECORD NOT FOUND */
373 return SUCCESS;
374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return NEEDS_RETRY;
376
377 case HARDWARE_ERROR:
378 if (scmd->device->retry_hwerror)
379 return NEEDS_RETRY;
380 else
381 return SUCCESS;
382
383 case ILLEGAL_REQUEST:
384 case BLANK_CHECK:
385 case DATA_PROTECT:
386 default:
387 return SUCCESS;
388 }
389}
390
391/**
392 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
393 * @scmd: SCSI cmd to examine.
394 *
395 * Notes:
396 * This is *only* called when we are examining the status of commands
397 * queued during error recovery. the main difference here is that we
398 * don't allow for the possibility of retries here, and we are a lot
399 * more restrictive about what we consider acceptable.
400 **/
401static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
402{
403 /*
404 * first check the host byte, to see if there is anything in there
405 * that would indicate what we need to do.
406 */
407 if (host_byte(scmd->result) == DID_RESET) {
408 /*
409 * rats. we are already in the error handler, so we now
410 * get to try and figure out what to do next. if the sense
411 * is valid, we have a pretty good idea of what to do.
412 * if not, we mark it as FAILED.
413 */
414 return scsi_check_sense(scmd);
415 }
416 if (host_byte(scmd->result) != DID_OK)
417 return FAILED;
418
419 /*
420 * next, check the message byte.
421 */
422 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
423 return FAILED;
424
425 /*
426 * now, check the status byte to see if this indicates
427 * anything special.
428 */
429 switch (status_byte(scmd->result)) {
430 case GOOD:
431 case COMMAND_TERMINATED:
432 return SUCCESS;
433 case CHECK_CONDITION:
434 return scsi_check_sense(scmd);
435 case CONDITION_GOOD:
436 case INTERMEDIATE_GOOD:
437 case INTERMEDIATE_C_GOOD:
438 /*
439 * who knows? FIXME(eric)
440 */
441 return SUCCESS;
442 case BUSY:
443 case QUEUE_FULL:
444 case RESERVATION_CONFLICT:
445 default:
446 return FAILED;
447 }
448 return FAILED;
449}
450
451/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * scsi_eh_done - Completion function for error handling.
453 * @scmd: Cmd that is done.
454 **/
455static void scsi_eh_done(struct scsi_cmnd *scmd)
456{
Michael Reed85631672005-12-07 21:46:27 -0600457 struct completion *eh_action;
458
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +0100459 SCSI_LOG_ERROR_RECOVERY(3,
460 printk("%s scmd: %p result: %x\n",
461 __FUNCTION__, scmd, scmd->result));
Michael Reed85631672005-12-07 21:46:27 -0600462
463 eh_action = scmd->device->host->eh_action;
464 if (eh_action)
465 complete(eh_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
468/**
Brian King292148f2007-01-30 17:51:17 -0600469 * scsi_try_host_reset - ask host adapter to reset itself
470 * @scmd: SCSI cmd to send hsot reset.
471 **/
472static int scsi_try_host_reset(struct scsi_cmnd *scmd)
473{
474 unsigned long flags;
475 int rtn;
476
477 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Host RST\n",
478 __FUNCTION__));
479
480 if (!scmd->device->host->hostt->eh_host_reset_handler)
481 return FAILED;
482
483 rtn = scmd->device->host->hostt->eh_host_reset_handler(scmd);
484
485 if (rtn == SUCCESS) {
486 if (!scmd->device->host->hostt->skip_settle_delay)
487 ssleep(HOST_RESET_SETTLE_TIME);
488 spin_lock_irqsave(scmd->device->host->host_lock, flags);
489 scsi_report_bus_reset(scmd->device->host,
490 scmd_channel(scmd));
491 spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
492 }
493
494 return rtn;
495}
496
497/**
498 * scsi_try_bus_reset - ask host to perform a bus reset
499 * @scmd: SCSI cmd to send bus reset.
500 **/
501static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
502{
503 unsigned long flags;
504 int rtn;
505
506 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Bus RST\n",
507 __FUNCTION__));
508
509 if (!scmd->device->host->hostt->eh_bus_reset_handler)
510 return FAILED;
511
512 rtn = scmd->device->host->hostt->eh_bus_reset_handler(scmd);
513
514 if (rtn == SUCCESS) {
515 if (!scmd->device->host->hostt->skip_settle_delay)
516 ssleep(BUS_RESET_SETTLE_TIME);
517 spin_lock_irqsave(scmd->device->host->host_lock, flags);
518 scsi_report_bus_reset(scmd->device->host,
519 scmd_channel(scmd));
520 spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
521 }
522
523 return rtn;
524}
525
526/**
527 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
528 * @scmd: SCSI cmd used to send BDR
529 *
530 * Notes:
531 * There is no timeout for this operation. if this operation is
532 * unreliable for a given host, then the host itself needs to put a
533 * timer on it, and set the host back to a consistent state prior to
534 * returning.
535 **/
536static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
537{
538 int rtn;
539
540 if (!scmd->device->host->hostt->eh_device_reset_handler)
541 return FAILED;
542
543 rtn = scmd->device->host->hostt->eh_device_reset_handler(scmd);
544 if (rtn == SUCCESS) {
545 scmd->device->was_reset = 1;
546 scmd->device->expecting_cc_ua = 1;
547 }
548
549 return rtn;
550}
551
552static int __scsi_try_to_abort_cmd(struct scsi_cmnd *scmd)
553{
554 if (!scmd->device->host->hostt->eh_abort_handler)
555 return FAILED;
556
557 return scmd->device->host->hostt->eh_abort_handler(scmd);
558}
559
560/**
561 * scsi_try_to_abort_cmd - Ask host to abort a running command.
562 * @scmd: SCSI cmd to abort from Lower Level.
563 *
564 * Notes:
565 * This function will not return until the user's completion function
566 * has been called. there is no timeout on this operation. if the
567 * author of the low-level driver wishes this operation to be timed,
568 * they can provide this facility themselves. helper functions in
569 * scsi_error.c can be supplied to make this easier to do.
570 **/
571static int scsi_try_to_abort_cmd(struct scsi_cmnd *scmd)
572{
573 /*
574 * scsi_done was called just after the command timed out and before
575 * we had a chance to process it. (db)
576 */
577 if (scmd->serial_number == 0)
578 return SUCCESS;
579 return __scsi_try_to_abort_cmd(scmd);
580}
581
582static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
583{
584 if (__scsi_try_to_abort_cmd(scmd) != SUCCESS)
585 if (scsi_try_bus_device_reset(scmd) != SUCCESS)
586 if (scsi_try_bus_reset(scmd) != SUCCESS)
587 scsi_try_host_reset(scmd);
588}
589
590/**
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100591 * scsi_send_eh_cmnd - submit a scsi command as part of error recory
592 * @scmd: SCSI command structure to hijack
593 * @cmnd: CDB to send
594 * @cmnd_size: size in bytes of @cmnd
595 * @timeout: timeout for this request
596 * @copy_sense: request sense data if set to 1
597 *
598 * This function is used to send a scsi command down to a target device
599 * as part of the error recovery process. If @copy_sense is 0 the command
600 * sent must be one that does not transfer any data. If @copy_sense is 1
601 * the command must be REQUEST_SENSE and this functions copies out the
602 * sense buffer it got into @scmd->sense_buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 * Return value:
605 * SUCCESS or FAILED or NEEDS_RETRY
606 **/
Mike Christie0db99e32006-08-26 03:00:22 -0400607static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
608 int cmnd_size, int timeout, int copy_sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
f59114b2005-04-17 15:00:23 -0500610 struct scsi_device *sdev = scmd->device;
611 struct Scsi_Host *shost = sdev->host;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200612 int old_result = scmd->result;
Stefan Richter7fbb3642006-09-12 20:35:54 -0700613 DECLARE_COMPLETION_ONSTACK(done);
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +0100614 unsigned long timeleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 unsigned long flags;
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100616 struct scatterlist sgl;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200617 unsigned char old_cmnd[MAX_COMMAND_SIZE];
618 enum dma_data_direction old_data_direction;
619 unsigned short old_use_sg;
620 unsigned char old_cmd_len;
621 unsigned old_bufflen;
622 void *old_buffer;
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +0100623 int rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Christoph Hellwig631c2282006-07-08 20:42:15 +0200625 /*
626 * We need saved copies of a number of fields - this is because
627 * error handling may need to overwrite these with different values
628 * to run different commands, and once error handling is complete,
629 * we will need to restore these values prior to running the actual
630 * command.
631 */
632 old_buffer = scmd->request_buffer;
633 old_bufflen = scmd->request_bufflen;
634 memcpy(old_cmnd, scmd->cmnd, sizeof(scmd->cmnd));
635 old_data_direction = scmd->sc_data_direction;
636 old_cmd_len = scmd->cmd_len;
637 old_use_sg = scmd->use_sg;
638
Mike Christie0db99e32006-08-26 03:00:22 -0400639 memset(scmd->cmnd, 0, sizeof(scmd->cmnd));
640 memcpy(scmd->cmnd, cmnd, cmnd_size);
641
Christoph Hellwig631c2282006-07-08 20:42:15 +0200642 if (copy_sense) {
Al Virofa1f5ea2006-10-10 22:47:47 +0100643 gfp_t gfp_mask = GFP_ATOMIC;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200644
645 if (shost->hostt->unchecked_isa_dma)
646 gfp_mask |= __GFP_DMA;
647
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100648 sgl.page = alloc_page(gfp_mask);
649 if (!sgl.page)
Christoph Hellwig631c2282006-07-08 20:42:15 +0200650 return FAILED;
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100651 sgl.offset = 0;
652 sgl.length = 252;
653
654 scmd->sc_data_direction = DMA_FROM_DEVICE;
655 scmd->request_bufflen = sgl.length;
656 scmd->request_buffer = &sgl;
657 scmd->use_sg = 1;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200658 } else {
659 scmd->request_buffer = NULL;
660 scmd->request_bufflen = 0;
661 scmd->sc_data_direction = DMA_NONE;
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100662 scmd->use_sg = 0;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200663 }
664
665 scmd->underflow = 0;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200666 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
667
f59114b2005-04-17 15:00:23 -0500668 if (sdev->scsi_level <= SCSI_2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
f59114b2005-04-17 15:00:23 -0500670 (sdev->lun << 5 & 0xe0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Christoph Hellwig631c2282006-07-08 20:42:15 +0200672 /*
673 * Zero the sense buffer. The scsi spec mandates that any
674 * untransferred sense data should be interpreted as being zero.
675 */
676 memset(scmd->sense_buffer, 0, sizeof(scmd->sense_buffer));
677
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +0100678 shost->eh_action = &done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
f59114b2005-04-17 15:00:23 -0500680 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 scsi_log_send(scmd);
f59114b2005-04-17 15:00:23 -0500682 shost->hostt->queuecommand(scmd, scsi_eh_done);
683 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +0100685 timeleft = wait_for_completion_timeout(&done, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
f59114b2005-04-17 15:00:23 -0500687 shost->eh_action = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +0100689 scsi_log_completion(scmd, SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +0100691 SCSI_LOG_ERROR_RECOVERY(3,
692 printk("%s: scmd: %p, timeleft: %ld\n",
693 __FUNCTION__, scmd, timeleft));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 /*
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +0100696 * If there is time left scsi_eh_done got called, and we will
697 * examine the actual status codes to see whether the command
698 * actually did complete normally, else tell the host to forget
699 * about this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 */
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +0100701 if (timeleft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 rtn = scsi_eh_completed_normally(scmd);
703 SCSI_LOG_ERROR_RECOVERY(3,
704 printk("%s: scsi_eh_completed_normally %x\n",
705 __FUNCTION__, rtn));
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +0100706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 switch (rtn) {
708 case SUCCESS:
709 case NEEDS_RETRY:
710 case FAILED:
711 break;
712 default:
713 rtn = FAILED;
714 break;
715 }
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +0100716 } else {
Brian King292148f2007-01-30 17:51:17 -0600717 scsi_abort_eh_cmnd(scmd);
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +0100718 rtn = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720
Christoph Hellwig631c2282006-07-08 20:42:15 +0200721
722 /*
723 * Last chance to have valid sense data.
724 */
725 if (copy_sense) {
726 if (!SCSI_SENSE_VALID(scmd)) {
David S. Miller8cc574a2007-04-02 14:21:55 -0700727 memcpy(scmd->sense_buffer, page_address(sgl.page),
Christoph Hellwig631c2282006-07-08 20:42:15 +0200728 sizeof(scmd->sense_buffer));
729 }
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100730 __free_page(sgl.page);
Christoph Hellwig631c2282006-07-08 20:42:15 +0200731 }
732
733
734 /*
735 * Restore original data
736 */
737 scmd->request_buffer = old_buffer;
738 scmd->request_bufflen = old_bufflen;
739 memcpy(scmd->cmnd, old_cmnd, sizeof(scmd->cmnd));
740 scmd->sc_data_direction = old_data_direction;
741 scmd->cmd_len = old_cmd_len;
742 scmd->use_sg = old_use_sg;
743 scmd->result = old_result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return rtn;
745}
746
747/**
748 * scsi_request_sense - Request sense data from a particular target.
749 * @scmd: SCSI cmd for request sense.
750 *
751 * Notes:
752 * Some hosts automatically obtain this information, others require
753 * that we obtain it on our own. This function will *not* return until
754 * the command either times out, or it completes.
755 **/
756static int scsi_request_sense(struct scsi_cmnd *scmd)
757{
758 static unsigned char generic_sense[6] =
Christoph Hellwig631c2282006-07-08 20:42:15 +0200759 {REQUEST_SENSE, 0, 0, 0, 252, 0};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Mike Christie0db99e32006-08-26 03:00:22 -0400761 return scsi_send_eh_cmnd(scmd, generic_sense, 6, SENSE_TIMEOUT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764/**
765 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
766 * @scmd: Original SCSI cmd that eh has finished.
767 * @done_q: Queue for processed commands.
768 *
769 * Notes:
770 * We don't want to use the normal command completion while we are are
771 * still handling errors - it may cause other commands to be queued,
772 * and that would disturb what we are doing. thus we really want to
773 * keep a list of pending commands for final completion, and once we
774 * are ready to leave error handling we handle completion for real.
775 **/
Tejun Heo041c5fc2006-01-23 13:09:36 +0900776void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 scmd->device->host->host_failed--;
Christoph Hellwig3111b0d2005-06-19 13:43:26 +0200779 scmd->eh_eflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 list_move_tail(&scmd->eh_entry, done_q);
781}
Tejun Heo041c5fc2006-01-23 13:09:36 +0900782EXPORT_SYMBOL(scsi_eh_finish_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784/**
785 * scsi_eh_get_sense - Get device sense data.
786 * @work_q: Queue of commands to process.
787 * @done_q: Queue of proccessed commands..
788 *
789 * Description:
790 * See if we need to request sense information. if so, then get it
791 * now, so we have a better idea of what to do.
792 *
793 * Notes:
794 * This has the unfortunate side effect that if a shost adapter does
795 * not automatically request sense information, that we end up shutting
796 * it down before we request it.
797 *
798 * All drivers should request sense information internally these days,
799 * so for now all I have to say is tough noogies if you end up in here.
800 *
801 * XXX: Long term this code should go away, but that needs an audit of
802 * all LLDDs first.
803 **/
Darrick J. Wongdca84e42007-01-26 14:08:49 -0800804int scsi_eh_get_sense(struct list_head *work_q,
805 struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +0200807 struct scsi_cmnd *scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 int rtn;
809
Christoph Hellwig937abeaa2005-06-19 13:43:56 +0200810 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Christoph Hellwig3111b0d2005-06-19 13:43:26 +0200811 if ((scmd->eh_eflags & SCSI_EH_CANCEL_CMD) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 SCSI_SENSE_VALID(scmd))
813 continue;
814
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400815 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
816 "%s: requesting sense\n",
817 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 rtn = scsi_request_sense(scmd);
819 if (rtn != SUCCESS)
820 continue;
821
822 SCSI_LOG_ERROR_RECOVERY(3, printk("sense requested for %p"
823 " result %x\n", scmd,
824 scmd->result));
825 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense("bh", scmd));
826
827 rtn = scsi_decide_disposition(scmd);
828
829 /*
830 * if the result was normal, then just pass it along to the
831 * upper level.
832 */
833 if (rtn == SUCCESS)
834 /* we don't want this command reissued, just
835 * finished with the sense data, so set
836 * retries to the max allowed to ensure it
837 * won't get reissued */
838 scmd->retries = scmd->allowed;
839 else if (rtn != NEEDS_RETRY)
840 continue;
841
842 scsi_eh_finish_cmd(scmd, done_q);
843 }
844
845 return list_empty(work_q);
846}
Darrick J. Wongdca84e42007-01-26 14:08:49 -0800847EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 * scsi_eh_tur - Send TUR to device.
851 * @scmd: Scsi cmd to send TUR
852 *
853 * Return value:
854 * 0 - Device is ready. 1 - Device NOT ready.
855 **/
856static int scsi_eh_tur(struct scsi_cmnd *scmd)
857{
858 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
859 int retry_cnt = 1, rtn;
860
861retry_tur:
Mike Christie0db99e32006-08-26 03:00:22 -0400862 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6, SENSE_TIMEOUT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd %p rtn %x\n",
865 __FUNCTION__, scmd, rtn));
Christoph Hellwig631c2282006-07-08 20:42:15 +0200866
867 switch (rtn) {
868 case NEEDS_RETRY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (retry_cnt--)
870 goto retry_tur;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200871 /*FALLTHRU*/
872 case SUCCESS:
Alan Sterne47373e2005-03-30 15:05:45 -0500873 return 0;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200874 default:
875 return 1;
Alan Sterne47373e2005-03-30 15:05:45 -0500876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
879/**
880 * scsi_eh_abort_cmds - abort canceled commands.
881 * @shost: scsi host being recovered.
882 * @eh_done_q: list_head for processed commands.
883 *
884 * Decription:
885 * Try and see whether or not it makes sense to try and abort the
886 * running command. this only works out to be the case if we have one
887 * command that has timed out. if the command simply failed, it makes
888 * no sense to try and abort the command, since as far as the shost
889 * adapter is concerned, it isn't running.
890 **/
891static int scsi_eh_abort_cmds(struct list_head *work_q,
892 struct list_head *done_q)
893{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +0200894 struct scsi_cmnd *scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 int rtn;
896
Christoph Hellwig937abeaa2005-06-19 13:43:56 +0200897 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Christoph Hellwig3111b0d2005-06-19 13:43:26 +0200898 if (!(scmd->eh_eflags & SCSI_EH_CANCEL_CMD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 continue;
900 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting cmd:"
901 "0x%p\n", current->comm,
902 scmd));
903 rtn = scsi_try_to_abort_cmd(scmd);
904 if (rtn == SUCCESS) {
Christoph Hellwig3111b0d2005-06-19 13:43:26 +0200905 scmd->eh_eflags &= ~SCSI_EH_CANCEL_CMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (!scsi_device_online(scmd->device) ||
907 !scsi_eh_tur(scmd)) {
908 scsi_eh_finish_cmd(scmd, done_q);
909 }
910
911 } else
912 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting"
913 " cmd failed:"
914 "0x%p\n",
915 current->comm,
916 scmd));
917 }
918
919 return list_empty(work_q);
920}
921
922/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 * scsi_eh_try_stu - Send START_UNIT to device.
924 * @scmd: Scsi cmd to send START_UNIT
925 *
926 * Return value:
927 * 0 - Device is ready. 1 - Device NOT ready.
928 **/
929static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
930{
931 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Christoph Hellwig631c2282006-07-08 20:42:15 +0200933 if (scmd->device->allow_restart) {
Brian Kinged773e62007-03-29 15:25:52 -0500934 int i, rtn = NEEDS_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Brian Kinged773e62007-03-29 15:25:52 -0500936 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
937 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6,
Brian Kinge555db92007-04-19 13:59:59 -0500938 scmd->device->timeout, 0);
Brian Kinged773e62007-03-29 15:25:52 -0500939
Christoph Hellwig631c2282006-07-08 20:42:15 +0200940 if (rtn == SUCCESS)
941 return 0;
942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return 1;
945}
946
947 /**
948 * scsi_eh_stu - send START_UNIT if needed
949 * @shost: scsi host being recovered.
950 * @eh_done_q: list_head for processed commands.
951 *
952 * Notes:
953 * If commands are failing due to not ready, initializing command required,
954 * try revalidating the device, which will end up sending a start unit.
955 **/
956static int scsi_eh_stu(struct Scsi_Host *shost,
957 struct list_head *work_q,
958 struct list_head *done_q)
959{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +0200960 struct scsi_cmnd *scmd, *stu_scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 struct scsi_device *sdev;
962
963 shost_for_each_device(sdev, shost) {
964 stu_scmd = NULL;
965 list_for_each_entry(scmd, work_q, eh_entry)
966 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
967 scsi_check_sense(scmd) == FAILED ) {
968 stu_scmd = scmd;
969 break;
970 }
971
972 if (!stu_scmd)
973 continue;
974
975 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending START_UNIT to sdev:"
976 " 0x%p\n", current->comm, sdev));
977
978 if (!scsi_eh_try_stu(stu_scmd)) {
979 if (!scsi_device_online(sdev) ||
980 !scsi_eh_tur(stu_scmd)) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +0200981 list_for_each_entry_safe(scmd, next,
982 work_q, eh_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (scmd->device == sdev)
984 scsi_eh_finish_cmd(scmd, done_q);
985 }
986 }
987 } else {
988 SCSI_LOG_ERROR_RECOVERY(3,
989 printk("%s: START_UNIT failed to sdev:"
990 " 0x%p\n", current->comm, sdev));
991 }
992 }
993
994 return list_empty(work_q);
995}
996
997
998/**
999 * scsi_eh_bus_device_reset - send bdr if needed
1000 * @shost: scsi host being recovered.
1001 * @eh_done_q: list_head for processed commands.
1002 *
1003 * Notes:
1004 * Try a bus device reset. still, look to see whether we have multiple
1005 * devices that are jammed or not - if we have multiple devices, it
1006 * makes no sense to try bus_device_reset - we really would need to try
1007 * a bus_reset instead.
1008 **/
1009static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1010 struct list_head *work_q,
1011 struct list_head *done_q)
1012{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001013 struct scsi_cmnd *scmd, *bdr_scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 struct scsi_device *sdev;
1015 int rtn;
1016
1017 shost_for_each_device(sdev, shost) {
1018 bdr_scmd = NULL;
1019 list_for_each_entry(scmd, work_q, eh_entry)
1020 if (scmd->device == sdev) {
1021 bdr_scmd = scmd;
1022 break;
1023 }
1024
1025 if (!bdr_scmd)
1026 continue;
1027
1028 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BDR sdev:"
1029 " 0x%p\n", current->comm,
1030 sdev));
1031 rtn = scsi_try_bus_device_reset(bdr_scmd);
1032 if (rtn == SUCCESS) {
1033 if (!scsi_device_online(sdev) ||
1034 !scsi_eh_tur(bdr_scmd)) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001035 list_for_each_entry_safe(scmd, next,
1036 work_q, eh_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (scmd->device == sdev)
1038 scsi_eh_finish_cmd(scmd,
1039 done_q);
1040 }
1041 }
1042 } else {
1043 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BDR"
1044 " failed sdev:"
1045 "0x%p\n",
1046 current->comm,
1047 sdev));
1048 }
1049 }
1050
1051 return list_empty(work_q);
1052}
1053
1054/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 * scsi_eh_bus_reset - send a bus reset
1056 * @shost: scsi host being recovered.
1057 * @eh_done_q: list_head for processed commands.
1058 **/
1059static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1060 struct list_head *work_q,
1061 struct list_head *done_q)
1062{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001063 struct scsi_cmnd *scmd, *chan_scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 unsigned int channel;
1065 int rtn;
1066
1067 /*
1068 * we really want to loop over the various channels, and do this on
1069 * a channel by channel basis. we should also check to see if any
1070 * of the failed commands are on soft_reset devices, and if so, skip
1071 * the reset.
1072 */
1073
1074 for (channel = 0; channel <= shost->max_channel; channel++) {
1075 chan_scmd = NULL;
1076 list_for_each_entry(scmd, work_q, eh_entry) {
Jeff Garzik422c0d62005-10-24 18:05:09 -04001077 if (channel == scmd_channel(scmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 chan_scmd = scmd;
1079 break;
1080 /*
1081 * FIXME add back in some support for
1082 * soft_reset devices.
1083 */
1084 }
1085 }
1086
1087 if (!chan_scmd)
1088 continue;
1089 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BRST chan:"
1090 " %d\n", current->comm,
1091 channel));
1092 rtn = scsi_try_bus_reset(chan_scmd);
1093 if (rtn == SUCCESS) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001094 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Jeff Garzik422c0d62005-10-24 18:05:09 -04001095 if (channel == scmd_channel(scmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (!scsi_device_online(scmd->device) ||
1097 !scsi_eh_tur(scmd))
1098 scsi_eh_finish_cmd(scmd,
1099 done_q);
1100 }
1101 } else {
1102 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BRST"
1103 " failed chan: %d\n",
1104 current->comm,
1105 channel));
1106 }
1107 }
1108 return list_empty(work_q);
1109}
1110
1111/**
1112 * scsi_eh_host_reset - send a host reset
1113 * @work_q: list_head for processed commands.
1114 * @done_q: list_head for processed commands.
1115 **/
1116static int scsi_eh_host_reset(struct list_head *work_q,
1117 struct list_head *done_q)
1118{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001119 struct scsi_cmnd *scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 int rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 if (!list_empty(work_q)) {
1123 scmd = list_entry(work_q->next,
1124 struct scsi_cmnd, eh_entry);
1125
1126 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending HRST\n"
1127 , current->comm));
1128
1129 rtn = scsi_try_host_reset(scmd);
1130 if (rtn == SUCCESS) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001131 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (!scsi_device_online(scmd->device) ||
1133 (!scsi_eh_try_stu(scmd) && !scsi_eh_tur(scmd)) ||
1134 !scsi_eh_tur(scmd))
1135 scsi_eh_finish_cmd(scmd, done_q);
1136 }
1137 } else {
1138 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: HRST"
1139 " failed\n",
1140 current->comm));
1141 }
1142 }
1143 return list_empty(work_q);
1144}
1145
1146/**
1147 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
1148 * @work_q: list_head for processed commands.
1149 * @done_q: list_head for processed commands.
1150 *
1151 **/
1152static void scsi_eh_offline_sdevs(struct list_head *work_q,
1153 struct list_head *done_q)
1154{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001155 struct scsi_cmnd *scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001157 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
James Bottomley9ccfc752005-10-02 11:45:08 -05001158 sdev_printk(KERN_INFO, scmd->device,
1159 "scsi: Device offlined - not"
1160 " ready after error recovery\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 scsi_device_set_state(scmd->device, SDEV_OFFLINE);
Christoph Hellwig3111b0d2005-06-19 13:43:26 +02001162 if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 /*
1164 * FIXME: Handle lost cmds.
1165 */
1166 }
1167 scsi_eh_finish_cmd(scmd, done_q);
1168 }
1169 return;
1170}
1171
1172/**
1173 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1174 * @scmd: SCSI cmd to examine.
1175 *
1176 * Notes:
1177 * This is *only* called when we are examining the status after sending
1178 * out the actual data command. any commands that are queued for error
1179 * recovery (e.g. test_unit_ready) do *not* come through here.
1180 *
1181 * When this routine returns failed, it means the error handler thread
1182 * is woken. In cases where the error code indicates an error that
1183 * doesn't require the error handler read (i.e. we don't need to
1184 * abort/reset), this function should return SUCCESS.
1185 **/
1186int scsi_decide_disposition(struct scsi_cmnd *scmd)
1187{
1188 int rtn;
1189
1190 /*
1191 * if the device is offline, then we clearly just pass the result back
1192 * up to the top level.
1193 */
1194 if (!scsi_device_online(scmd->device)) {
1195 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: device offline - report"
1196 " as SUCCESS\n",
1197 __FUNCTION__));
1198 return SUCCESS;
1199 }
1200
1201 /*
1202 * first check the host byte, to see if there is anything in there
1203 * that would indicate what we need to do.
1204 */
1205 switch (host_byte(scmd->result)) {
1206 case DID_PASSTHROUGH:
1207 /*
1208 * no matter what, pass this through to the upper layer.
1209 * nuke this special code so that it looks like we are saying
1210 * did_ok.
1211 */
1212 scmd->result &= 0xff00ffff;
1213 return SUCCESS;
1214 case DID_OK:
1215 /*
1216 * looks good. drop through, and check the next byte.
1217 */
1218 break;
1219 case DID_NO_CONNECT:
1220 case DID_BAD_TARGET:
1221 case DID_ABORT:
1222 /*
1223 * note - this means that we just report the status back
1224 * to the top level driver, not that we actually think
1225 * that it indicates SUCCESS.
1226 */
1227 return SUCCESS;
1228 /*
1229 * when the low level driver returns did_soft_error,
1230 * it is responsible for keeping an internal retry counter
1231 * in order to avoid endless loops (db)
1232 *
1233 * actually this is a bug in this function here. we should
1234 * be mindful of the maximum number of retries specified
1235 * and not get stuck in a loop.
1236 */
1237 case DID_SOFT_ERROR:
1238 goto maybe_retry;
1239 case DID_IMM_RETRY:
1240 return NEEDS_RETRY;
1241
bf341912005-04-12 17:49:09 -05001242 case DID_REQUEUE:
1243 return ADD_TO_MLQUEUE;
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 case DID_ERROR:
1246 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1247 status_byte(scmd->result) == RESERVATION_CONFLICT)
1248 /*
1249 * execute reservation conflict processing code
1250 * lower down
1251 */
1252 break;
1253 /* fallthrough */
1254
1255 case DID_BUS_BUSY:
1256 case DID_PARITY:
1257 goto maybe_retry;
1258 case DID_TIME_OUT:
1259 /*
1260 * when we scan the bus, we get timeout messages for
1261 * these commands if there is no device available.
1262 * other hosts report did_no_connect for the same thing.
1263 */
1264 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1265 scmd->cmnd[0] == INQUIRY)) {
1266 return SUCCESS;
1267 } else {
1268 return FAILED;
1269 }
1270 case DID_RESET:
1271 return SUCCESS;
1272 default:
1273 return FAILED;
1274 }
1275
1276 /*
1277 * next, check the message byte.
1278 */
1279 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
1280 return FAILED;
1281
1282 /*
1283 * check the status byte to see if this indicates anything special.
1284 */
1285 switch (status_byte(scmd->result)) {
1286 case QUEUE_FULL:
1287 /*
1288 * the case of trying to send too many commands to a
1289 * tagged queueing device.
1290 */
1291 case BUSY:
1292 /*
1293 * device can't talk to us at the moment. Should only
1294 * occur (SAM-3) when the task queue is empty, so will cause
1295 * the empty queue handling to trigger a stall in the
1296 * device.
1297 */
1298 return ADD_TO_MLQUEUE;
1299 case GOOD:
1300 case COMMAND_TERMINATED:
1301 case TASK_ABORTED:
1302 return SUCCESS;
1303 case CHECK_CONDITION:
1304 rtn = scsi_check_sense(scmd);
1305 if (rtn == NEEDS_RETRY)
1306 goto maybe_retry;
1307 /* if rtn == FAILED, we have no sense information;
1308 * returning FAILED will wake the error handler thread
1309 * to collect the sense and redo the decide
1310 * disposition */
1311 return rtn;
1312 case CONDITION_GOOD:
1313 case INTERMEDIATE_GOOD:
1314 case INTERMEDIATE_C_GOOD:
1315 case ACA_ACTIVE:
1316 /*
1317 * who knows? FIXME(eric)
1318 */
1319 return SUCCESS;
1320
1321 case RESERVATION_CONFLICT:
James Bottomley9ccfc752005-10-02 11:45:08 -05001322 sdev_printk(KERN_INFO, scmd->device,
1323 "reservation conflict\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return SUCCESS; /* causes immediate i/o error */
1325 default:
1326 return FAILED;
1327 }
1328 return FAILED;
1329
1330 maybe_retry:
1331
1332 /* we requeue for retry because the error was retryable, and
1333 * the request was not marked fast fail. Note that above,
1334 * even if the request is marked fast fail, we still requeue
1335 * for queue congestion conditions (QUEUE_FULL or BUSY) */
Brian King8884efa2006-02-24 17:10:04 -06001336 if ((++scmd->retries) <= scmd->allowed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 && !blk_noretry_request(scmd->request)) {
1338 return NEEDS_RETRY;
1339 } else {
1340 /*
1341 * no more retries - report this one back to upper level.
1342 */
1343 return SUCCESS;
1344 }
1345}
1346
1347/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 * scsi_eh_lock_door - Prevent medium removal for the specified device
1349 * @sdev: SCSI device to prevent medium removal
1350 *
1351 * Locking:
1352 * We must be called from process context; scsi_allocate_request()
1353 * may sleep.
1354 *
1355 * Notes:
1356 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1357 * head of the devices request queue, and continue.
1358 *
1359 * Bugs:
1360 * scsi_allocate_request() may sleep waiting for existing requests to
1361 * be processed. However, since we haven't kicked off any request
1362 * processing for this host, this may deadlock.
1363 *
1364 * If scsi_allocate_request() fails for what ever reason, we
1365 * completely forget to lock the door.
1366 **/
1367static void scsi_eh_lock_door(struct scsi_device *sdev)
1368{
Mike Christie6e68af62005-11-11 05:30:27 -06001369 unsigned char cmnd[MAX_COMMAND_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Mike Christie6e68af62005-11-11 05:30:27 -06001371 cmnd[0] = ALLOW_MEDIUM_REMOVAL;
1372 cmnd[1] = 0;
1373 cmnd[2] = 0;
1374 cmnd[3] = 0;
1375 cmnd[4] = SCSI_REMOVAL_PREVENT;
1376 cmnd[5] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
brking@us.ibm.combb1d1072006-01-23 15:03:22 -06001378 scsi_execute_async(sdev, cmnd, 6, DMA_NONE, NULL, 0, 0, 10 * HZ,
Mike Christie6e68af62005-11-11 05:30:27 -06001379 5, NULL, NULL, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
1381
1382
1383/**
1384 * scsi_restart_operations - restart io operations to the specified host.
1385 * @shost: Host we are restarting.
1386 *
1387 * Notes:
1388 * When we entered the error handler, we blocked all further i/o to
1389 * this device. we need to 'reverse' this process.
1390 **/
1391static void scsi_restart_operations(struct Scsi_Host *shost)
1392{
1393 struct scsi_device *sdev;
James Bottomley939647e2005-09-18 15:05:20 -05001394 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 /*
1397 * If the door was locked, we need to insert a door lock request
1398 * onto the head of the SCSI request queue for the device. There
1399 * is no point trying to lock the door of an off-line device.
1400 */
1401 shost_for_each_device(sdev, shost) {
1402 if (scsi_device_online(sdev) && sdev->locked)
1403 scsi_eh_lock_door(sdev);
1404 }
1405
1406 /*
1407 * next free up anything directly waiting upon the host. this
1408 * will be requests for character device operations, and also for
1409 * ioctls to queued block devices.
1410 */
1411 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: waking up host to restart\n",
1412 __FUNCTION__));
1413
James Bottomley939647e2005-09-18 15:05:20 -05001414 spin_lock_irqsave(shost->host_lock, flags);
1415 if (scsi_host_set_state(shost, SHOST_RUNNING))
1416 if (scsi_host_set_state(shost, SHOST_CANCEL))
1417 BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
1418 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 wake_up(&shost->host_wait);
1421
1422 /*
1423 * finally we need to re-initiate requests that may be pending. we will
1424 * have had everything blocked while error handling is taking place, and
1425 * now that error recovery is done, we will need to ensure that these
1426 * requests are started.
1427 */
1428 scsi_run_host_queues(shost);
1429}
1430
1431/**
1432 * scsi_eh_ready_devs - check device ready state and recover if not.
1433 * @shost: host to be recovered.
1434 * @eh_done_q: list_head for processed commands.
1435 *
1436 **/
Darrick J. Wongdca84e42007-01-26 14:08:49 -08001437void scsi_eh_ready_devs(struct Scsi_Host *shost,
1438 struct list_head *work_q,
1439 struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
1441 if (!scsi_eh_stu(shost, work_q, done_q))
1442 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
1443 if (!scsi_eh_bus_reset(shost, work_q, done_q))
1444 if (!scsi_eh_host_reset(work_q, done_q))
1445 scsi_eh_offline_sdevs(work_q, done_q);
1446}
Darrick J. Wongdca84e42007-01-26 14:08:49 -08001447EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449/**
1450 * scsi_eh_flush_done_q - finish processed commands or retry them.
1451 * @done_q: list_head of processed commands.
1452 *
1453 **/
Tejun Heo041c5fc2006-01-23 13:09:36 +09001454void scsi_eh_flush_done_q(struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001456 struct scsi_cmnd *scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001458 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
1459 list_del_init(&scmd->eh_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (scsi_device_online(scmd->device) &&
1461 !blk_noretry_request(scmd->request) &&
Brian King8884efa2006-02-24 17:10:04 -06001462 (++scmd->retries <= scmd->allowed)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush"
1464 " retry cmd: %p\n",
1465 current->comm,
1466 scmd));
1467 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
1468 } else {
Patrick Mansfield 793698c2005-05-16 17:42:15 -07001469 /*
1470 * If just we got sense for the device (called
1471 * scsi_eh_get_sense), scmd->result is already
1472 * set, do not set DRIVER_TIMEOUT.
1473 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (!scmd->result)
1475 scmd->result |= (DRIVER_TIMEOUT << 24);
1476 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush finish"
1477 " cmd: %p\n",
1478 current->comm, scmd));
1479 scsi_finish_command(scmd);
1480 }
1481 }
1482}
Tejun Heo041c5fc2006-01-23 13:09:36 +09001483EXPORT_SYMBOL(scsi_eh_flush_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485/**
1486 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
1487 * @shost: Host to unjam.
1488 *
1489 * Notes:
1490 * When we come in here, we *know* that all commands on the bus have
1491 * either completed, failed or timed out. we also know that no further
1492 * commands are being sent to the host, so things are relatively quiet
1493 * and we have freedom to fiddle with things as we wish.
1494 *
1495 * This is only the *default* implementation. it is possible for
1496 * individual drivers to supply their own version of this function, and
1497 * if the maintainer wishes to do this, it is strongly suggested that
1498 * this function be taken as a template and modified. this function
1499 * was designed to correctly handle problems for about 95% of the
1500 * different cases out there, and it should always provide at least a
1501 * reasonable amount of error recovery.
1502 *
1503 * Any command marked 'failed' or 'timeout' must eventually have
1504 * scsi_finish_cmd() called for it. we do all of the retry stuff
1505 * here, so when we restart the host after we return it should have an
1506 * empty queue.
1507 **/
1508static void scsi_unjam_host(struct Scsi_Host *shost)
1509{
1510 unsigned long flags;
1511 LIST_HEAD(eh_work_q);
1512 LIST_HEAD(eh_done_q);
1513
1514 spin_lock_irqsave(shost->host_lock, flags);
1515 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
1516 spin_unlock_irqrestore(shost->host_lock, flags);
1517
1518 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
1519
1520 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
1521 if (!scsi_eh_abort_cmds(&eh_work_q, &eh_done_q))
1522 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
1523
1524 scsi_eh_flush_done_q(&eh_done_q);
1525}
1526
1527/**
Christoph Hellwigad42eb12005-10-29 01:01:55 +02001528 * scsi_error_handler - SCSI error handler thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 * @data: Host for which we are running.
1530 *
1531 * Notes:
Christoph Hellwigad42eb12005-10-29 01:01:55 +02001532 * This is the main error handling loop. This is run as a kernel thread
1533 * for every SCSI host and handles all error handling activity.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 **/
1535int scsi_error_handler(void *data)
1536{
Christoph Hellwigad42eb12005-10-29 01:01:55 +02001537 struct Scsi_Host *shost = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 current->flags |= PF_NOFREEZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 /*
Christoph Hellwigad42eb12005-10-29 01:01:55 +02001542 * We use TASK_INTERRUPTIBLE so that the thread is not
1543 * counted against the load average as a running process.
1544 * We never actually get interrupted because kthread_run
1545 * disables singal delivery for the created thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 */
James Bottomley3ed7a472005-09-19 09:50:04 -05001547 set_current_state(TASK_INTERRUPTIBLE);
1548 while (!kthread_should_stop()) {
Tejun Heoee7863b2006-05-15 20:57:20 +09001549 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
James Bottomley3ed7a472005-09-19 09:50:04 -05001550 shost->host_failed != shost->host_busy) {
Christoph Hellwigad42eb12005-10-29 01:01:55 +02001551 SCSI_LOG_ERROR_RECOVERY(1,
1552 printk("Error handler scsi_eh_%d sleeping\n",
1553 shost->host_no));
James Bottomley3ed7a472005-09-19 09:50:04 -05001554 schedule();
1555 set_current_state(TASK_INTERRUPTIBLE);
1556 continue;
1557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
James Bottomley3ed7a472005-09-19 09:50:04 -05001559 __set_current_state(TASK_RUNNING);
Christoph Hellwigad42eb12005-10-29 01:01:55 +02001560 SCSI_LOG_ERROR_RECOVERY(1,
1561 printk("Error handler scsi_eh_%d waking up\n",
1562 shost->host_no));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 /*
1565 * We have a host that is failing for some reason. Figure out
1566 * what we need to do to get it up and online again (if we can).
1567 * If we fail, we end up taking the thing offline.
1568 */
Christoph Hellwig9227c332006-04-01 19:21:04 +02001569 if (shost->transportt->eh_strategy_handler)
1570 shost->transportt->eh_strategy_handler(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 else
1572 scsi_unjam_host(shost);
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 /*
1575 * Note - if the above fails completely, the action is to take
1576 * individual devices offline and flush the queue of any
1577 * outstanding requests that may have been pending. When we
1578 * restart, we restart any I/O to any other devices on the bus
1579 * which are still online.
1580 */
1581 scsi_restart_operations(shost);
James Bottomley3ed7a472005-09-19 09:50:04 -05001582 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 }
Steven Rostedt461a0ff2005-10-19 08:22:13 -04001584 __set_current_state(TASK_RUNNING);
1585
Christoph Hellwigad42eb12005-10-29 01:01:55 +02001586 SCSI_LOG_ERROR_RECOVERY(1,
1587 printk("Error handler scsi_eh_%d exiting\n", shost->host_no));
James Bottomley3ed7a472005-09-19 09:50:04 -05001588 shost->ehandler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 return 0;
1590}
1591
1592/*
1593 * Function: scsi_report_bus_reset()
1594 *
1595 * Purpose: Utility function used by low-level drivers to report that
1596 * they have observed a bus reset on the bus being handled.
1597 *
1598 * Arguments: shost - Host in question
1599 * channel - channel on which reset was observed.
1600 *
1601 * Returns: Nothing
1602 *
1603 * Lock status: Host lock must be held.
1604 *
1605 * Notes: This only needs to be called if the reset is one which
1606 * originates from an unknown location. Resets originated
1607 * by the mid-level itself don't need to call this, but there
1608 * should be no harm.
1609 *
1610 * The main purpose of this is to make sure that a CHECK_CONDITION
1611 * is properly treated.
1612 */
1613void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
1614{
1615 struct scsi_device *sdev;
1616
1617 __shost_for_each_device(sdev, shost) {
Jeff Garzik422c0d62005-10-24 18:05:09 -04001618 if (channel == sdev_channel(sdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 sdev->was_reset = 1;
1620 sdev->expecting_cc_ua = 1;
1621 }
1622 }
1623}
1624EXPORT_SYMBOL(scsi_report_bus_reset);
1625
1626/*
1627 * Function: scsi_report_device_reset()
1628 *
1629 * Purpose: Utility function used by low-level drivers to report that
1630 * they have observed a device reset on the device being handled.
1631 *
1632 * Arguments: shost - Host in question
1633 * channel - channel on which reset was observed
1634 * target - target on which reset was observed
1635 *
1636 * Returns: Nothing
1637 *
1638 * Lock status: Host lock must be held
1639 *
1640 * Notes: This only needs to be called if the reset is one which
1641 * originates from an unknown location. Resets originated
1642 * by the mid-level itself don't need to call this, but there
1643 * should be no harm.
1644 *
1645 * The main purpose of this is to make sure that a CHECK_CONDITION
1646 * is properly treated.
1647 */
1648void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
1649{
1650 struct scsi_device *sdev;
1651
1652 __shost_for_each_device(sdev, shost) {
Jeff Garzik422c0d62005-10-24 18:05:09 -04001653 if (channel == sdev_channel(sdev) &&
1654 target == sdev_id(sdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 sdev->was_reset = 1;
1656 sdev->expecting_cc_ua = 1;
1657 }
1658 }
1659}
1660EXPORT_SYMBOL(scsi_report_device_reset);
1661
1662static void
1663scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
1664{
1665}
1666
1667/*
1668 * Function: scsi_reset_provider
1669 *
1670 * Purpose: Send requested reset to a bus or device at any phase.
1671 *
1672 * Arguments: device - device to send reset to
1673 * flag - reset type (see scsi.h)
1674 *
1675 * Returns: SUCCESS/FAILURE.
1676 *
1677 * Notes: This is used by the SCSI Generic driver to provide
1678 * Bus/Device reset capability.
1679 */
1680int
1681scsi_reset_provider(struct scsi_device *dev, int flag)
1682{
1683 struct scsi_cmnd *scmd = scsi_get_command(dev, GFP_KERNEL);
James Smartd7a1bb02006-03-08 14:50:12 -05001684 struct Scsi_Host *shost = dev->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 struct request req;
James Smartd7a1bb02006-03-08 14:50:12 -05001686 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 int rtn;
1688
1689 scmd->request = &req;
1690 memset(&scmd->eh_timeout, 0, sizeof(scmd->eh_timeout));
Christoph Hellwigb4edcbc2005-06-19 13:40:52 +02001691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 memset(&scmd->cmnd, '\0', sizeof(scmd->cmnd));
1693
1694 scmd->scsi_done = scsi_reset_provider_done_command;
1695 scmd->done = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 scmd->request_buffer = NULL;
1697 scmd->request_bufflen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699 scmd->cmd_len = 0;
1700
1701 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 init_timer(&scmd->eh_timeout);
1704
1705 /*
1706 * Sometimes the command can get back into the timer chain,
1707 * so use the pid as an identifier.
1708 */
1709 scmd->pid = 0;
1710
James Smartd7a1bb02006-03-08 14:50:12 -05001711 spin_lock_irqsave(shost->host_lock, flags);
1712 shost->tmf_in_progress = 1;
1713 spin_unlock_irqrestore(shost->host_lock, flags);
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 switch (flag) {
1716 case SCSI_TRY_RESET_DEVICE:
1717 rtn = scsi_try_bus_device_reset(scmd);
1718 if (rtn == SUCCESS)
1719 break;
1720 /* FALLTHROUGH */
1721 case SCSI_TRY_RESET_BUS:
1722 rtn = scsi_try_bus_reset(scmd);
1723 if (rtn == SUCCESS)
1724 break;
1725 /* FALLTHROUGH */
1726 case SCSI_TRY_RESET_HOST:
1727 rtn = scsi_try_host_reset(scmd);
1728 break;
1729 default:
1730 rtn = FAILED;
1731 }
1732
James Smartd7a1bb02006-03-08 14:50:12 -05001733 spin_lock_irqsave(shost->host_lock, flags);
1734 shost->tmf_in_progress = 0;
1735 spin_unlock_irqrestore(shost->host_lock, flags);
1736
1737 /*
1738 * be sure to wake up anyone who was sleeping or had their queue
1739 * suspended while we performed the TMF.
1740 */
1741 SCSI_LOG_ERROR_RECOVERY(3,
1742 printk("%s: waking up host to restart after TMF\n",
1743 __FUNCTION__));
1744
1745 wake_up(&shost->host_wait);
1746
1747 scsi_run_host_queues(shost);
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 scsi_next_command(scmd);
1750 return rtn;
1751}
1752EXPORT_SYMBOL(scsi_reset_provider);
1753
1754/**
1755 * scsi_normalize_sense - normalize main elements from either fixed or
1756 * descriptor sense data format into a common format.
1757 *
1758 * @sense_buffer: byte array containing sense data returned by device
1759 * @sb_len: number of valid bytes in sense_buffer
1760 * @sshdr: pointer to instance of structure that common
1761 * elements are written to.
1762 *
1763 * Notes:
1764 * The "main elements" from sense data are: response_code, sense_key,
1765 * asc, ascq and additional_length (only for descriptor format).
1766 *
1767 * Typically this function can be called after a device has
1768 * responded to a SCSI command with the CHECK_CONDITION status.
1769 *
1770 * Return value:
1771 * 1 if valid sense data information found, else 0;
1772 **/
1773int scsi_normalize_sense(const u8 *sense_buffer, int sb_len,
1774 struct scsi_sense_hdr *sshdr)
1775{
James Bottomley33aa6872005-08-28 11:31:14 -05001776 if (!sense_buffer || !sb_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 return 0;
1778
1779 memset(sshdr, 0, sizeof(struct scsi_sense_hdr));
1780
1781 sshdr->response_code = (sense_buffer[0] & 0x7f);
James Bottomley33aa6872005-08-28 11:31:14 -05001782
1783 if (!scsi_sense_valid(sshdr))
1784 return 0;
1785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 if (sshdr->response_code >= 0x72) {
1787 /*
1788 * descriptor format
1789 */
1790 if (sb_len > 1)
1791 sshdr->sense_key = (sense_buffer[1] & 0xf);
1792 if (sb_len > 2)
1793 sshdr->asc = sense_buffer[2];
1794 if (sb_len > 3)
1795 sshdr->ascq = sense_buffer[3];
1796 if (sb_len > 7)
1797 sshdr->additional_length = sense_buffer[7];
1798 } else {
1799 /*
1800 * fixed format
1801 */
1802 if (sb_len > 2)
1803 sshdr->sense_key = (sense_buffer[2] & 0xf);
1804 if (sb_len > 7) {
1805 sb_len = (sb_len < (sense_buffer[7] + 8)) ?
1806 sb_len : (sense_buffer[7] + 8);
1807 if (sb_len > 12)
1808 sshdr->asc = sense_buffer[12];
1809 if (sb_len > 13)
1810 sshdr->ascq = sense_buffer[13];
1811 }
1812 }
1813
1814 return 1;
1815}
1816EXPORT_SYMBOL(scsi_normalize_sense);
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818int scsi_command_normalize_sense(struct scsi_cmnd *cmd,
1819 struct scsi_sense_hdr *sshdr)
1820{
1821 return scsi_normalize_sense(cmd->sense_buffer,
1822 sizeof(cmd->sense_buffer), sshdr);
1823}
1824EXPORT_SYMBOL(scsi_command_normalize_sense);
1825
1826/**
1827 * scsi_sense_desc_find - search for a given descriptor type in
1828 * descriptor sense data format.
1829 *
1830 * @sense_buffer: byte array of descriptor format sense data
1831 * @sb_len: number of valid bytes in sense_buffer
1832 * @desc_type: value of descriptor type to find
1833 * (e.g. 0 -> information)
1834 *
1835 * Notes:
1836 * only valid when sense data is in descriptor format
1837 *
1838 * Return value:
1839 * pointer to start of (first) descriptor if found else NULL
1840 **/
1841const u8 * scsi_sense_desc_find(const u8 * sense_buffer, int sb_len,
1842 int desc_type)
1843{
1844 int add_sen_len, add_len, desc_len, k;
1845 const u8 * descp;
1846
1847 if ((sb_len < 8) || (0 == (add_sen_len = sense_buffer[7])))
1848 return NULL;
1849 if ((sense_buffer[0] < 0x72) || (sense_buffer[0] > 0x73))
1850 return NULL;
1851 add_sen_len = (add_sen_len < (sb_len - 8)) ?
1852 add_sen_len : (sb_len - 8);
1853 descp = &sense_buffer[8];
1854 for (desc_len = 0, k = 0; k < add_sen_len; k += desc_len) {
1855 descp += desc_len;
1856 add_len = (k < (add_sen_len - 1)) ? descp[1]: -1;
1857 desc_len = add_len + 2;
1858 if (descp[0] == desc_type)
1859 return descp;
1860 if (add_len < 0) // short descriptor ??
1861 break;
1862 }
1863 return NULL;
1864}
1865EXPORT_SYMBOL(scsi_sense_desc_find);
1866
1867/**
1868 * scsi_get_sense_info_fld - attempts to get information field from
1869 * sense data (either fixed or descriptor format)
1870 *
1871 * @sense_buffer: byte array of sense data
1872 * @sb_len: number of valid bytes in sense_buffer
1873 * @info_out: pointer to 64 integer where 8 or 4 byte information
1874 * field will be placed if found.
1875 *
1876 * Return value:
1877 * 1 if information field found, 0 if not found.
1878 **/
1879int scsi_get_sense_info_fld(const u8 * sense_buffer, int sb_len,
1880 u64 * info_out)
1881{
1882 int j;
1883 const u8 * ucp;
1884 u64 ull;
1885
1886 if (sb_len < 7)
1887 return 0;
1888 switch (sense_buffer[0] & 0x7f) {
1889 case 0x70:
1890 case 0x71:
1891 if (sense_buffer[0] & 0x80) {
1892 *info_out = (sense_buffer[3] << 24) +
1893 (sense_buffer[4] << 16) +
1894 (sense_buffer[5] << 8) + sense_buffer[6];
1895 return 1;
1896 } else
1897 return 0;
1898 case 0x72:
1899 case 0x73:
1900 ucp = scsi_sense_desc_find(sense_buffer, sb_len,
1901 0 /* info desc */);
1902 if (ucp && (0xa == ucp[1])) {
1903 ull = 0;
1904 for (j = 0; j < 8; ++j) {
1905 if (j > 0)
1906 ull <<= 8;
1907 ull |= ucp[4 + j];
1908 }
1909 *info_out = ull;
1910 return 1;
1911 } else
1912 return 0;
1913 default:
1914 return 0;
1915 }
1916}
1917EXPORT_SYMBOL(scsi_get_sense_info_fld);