0df8615732c1e6cd8dd75a1f1de2661aa81aea16
[deliverable/linux.git] / drivers / scsi / scsi_error.c
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
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>
23 #include <linux/interrupt.h>
24 #include <linux/blkdev.h>
25 #include <linux/delay.h>
26
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_dbg.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_eh.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_ioctl.h>
33 #include <scsi/scsi_request.h>
34
35 #include "scsi_priv.h"
36 #include "scsi_logging.h"
37
38 #define SENSE_TIMEOUT (10*HZ)
39 #define START_UNIT_TIMEOUT (30*HZ)
40
41 /*
42 * These should *probably* be handled by the host itself.
43 * Since it is allowed to sleep, it probably should.
44 */
45 #define BUS_RESET_SETTLE_TIME (10)
46 #define HOST_RESET_SETTLE_TIME (10)
47
48 /* called with shost->host_lock held */
49 void scsi_eh_wakeup(struct Scsi_Host *shost)
50 {
51 if (shost->host_busy == shost->host_failed) {
52 up(shost->eh_wait);
53 SCSI_LOG_ERROR_RECOVERY(5,
54 printk("Waking error handler thread\n"));
55 }
56 }
57
58 /**
59 * scsi_eh_scmd_add - add scsi cmd to error handling.
60 * @scmd: scmd to run eh on.
61 * @eh_flag: optional SCSI_EH flag.
62 *
63 * Return value:
64 * 0 on failure.
65 **/
66 int scsi_eh_scmd_add(struct scsi_cmnd *scmd, int eh_flag)
67 {
68 struct Scsi_Host *shost = scmd->device->host;
69 unsigned long flags;
70
71 if (shost->eh_wait == NULL)
72 return 0;
73
74 spin_lock_irqsave(shost->host_lock, flags);
75
76 scsi_eh_eflags_set(scmd, eh_flag);
77 /*
78 * FIXME: Can we stop setting owner and state.
79 */
80 scmd->state = SCSI_STATE_FAILED;
81 list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
82 set_bit(SHOST_RECOVERY, &shost->shost_state);
83 shost->host_failed++;
84 scsi_eh_wakeup(shost);
85 spin_unlock_irqrestore(shost->host_lock, flags);
86 return 1;
87 }
88
89 /**
90 * scsi_add_timer - Start timeout timer for a single scsi command.
91 * @scmd: scsi command that is about to start running.
92 * @timeout: amount of time to allow this command to run.
93 * @complete: timeout function to call if timer isn't canceled.
94 *
95 * Notes:
96 * This should be turned into an inline function. Each scsi command
97 * has its own timer, and as it is added to the queue, we set up the
98 * timer. When the command completes, we cancel the timer.
99 **/
100 void scsi_add_timer(struct scsi_cmnd *scmd, int timeout,
101 void (*complete)(struct scsi_cmnd *))
102 {
103
104 /*
105 * If the clock was already running for this command, then
106 * first delete the timer. The timer handling code gets rather
107 * confused if we don't do this.
108 */
109 if (scmd->eh_timeout.function)
110 del_timer(&scmd->eh_timeout);
111
112 scmd->eh_timeout.data = (unsigned long)scmd;
113 scmd->eh_timeout.expires = jiffies + timeout;
114 scmd->eh_timeout.function = (void (*)(unsigned long)) complete;
115
116 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: scmd: %p, time:"
117 " %d, (%p)\n", __FUNCTION__,
118 scmd, timeout, complete));
119
120 add_timer(&scmd->eh_timeout);
121 }
122 EXPORT_SYMBOL(scsi_add_timer);
123
124 /**
125 * scsi_delete_timer - Delete/cancel timer for a given function.
126 * @scmd: Cmd that we are canceling timer for
127 *
128 * Notes:
129 * This should be turned into an inline function.
130 *
131 * Return value:
132 * 1 if we were able to detach the timer. 0 if we blew it, and the
133 * timer function has already started to run.
134 **/
135 int scsi_delete_timer(struct scsi_cmnd *scmd)
136 {
137 int rtn;
138
139 rtn = del_timer(&scmd->eh_timeout);
140
141 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: scmd: %p,"
142 " rtn: %d\n", __FUNCTION__,
143 scmd, rtn));
144
145 scmd->eh_timeout.data = (unsigned long)NULL;
146 scmd->eh_timeout.function = NULL;
147
148 return rtn;
149 }
150 EXPORT_SYMBOL(scsi_delete_timer);
151
152 /**
153 * scsi_times_out - Timeout function for normal scsi commands.
154 * @scmd: Cmd that is timing out.
155 *
156 * Notes:
157 * We do not need to lock this. There is the potential for a race
158 * only in that the normal completion handling might run, but if the
159 * normal completion function determines that the timer has already
160 * fired, then it mustn't do anything.
161 **/
162 void scsi_times_out(struct scsi_cmnd *scmd)
163 {
164 scsi_log_completion(scmd, TIMEOUT_ERROR);
165
166 if (scmd->device->host->hostt->eh_timed_out)
167 switch (scmd->device->host->hostt->eh_timed_out(scmd)) {
168 case EH_HANDLED:
169 __scsi_done(scmd);
170 return;
171 case EH_RESET_TIMER:
172 /* This allows a single retry even of a command
173 * with allowed == 0 */
174 if (scmd->retries++ > scmd->allowed)
175 break;
176 scsi_add_timer(scmd, scmd->timeout_per_command,
177 scsi_times_out);
178 return;
179 case EH_NOT_HANDLED:
180 break;
181 }
182
183 if (unlikely(!scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD))) {
184 panic("Error handler thread not present at %p %p %s %d",
185 scmd, scmd->device->host, __FILE__, __LINE__);
186 }
187 }
188
189 /**
190 * scsi_block_when_processing_errors - Prevent cmds from being queued.
191 * @sdev: Device on which we are performing recovery.
192 *
193 * Description:
194 * We block until the host is out of error recovery, and then check to
195 * see whether the host or the device is offline.
196 *
197 * Return value:
198 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
199 **/
200 int scsi_block_when_processing_errors(struct scsi_device *sdev)
201 {
202 int online;
203
204 wait_event(sdev->host->host_wait, (!test_bit(SHOST_RECOVERY, &sdev->host->shost_state)));
205
206 online = scsi_device_online(sdev);
207
208 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: rtn: %d\n", __FUNCTION__,
209 online));
210
211 return online;
212 }
213 EXPORT_SYMBOL(scsi_block_when_processing_errors);
214
215 #ifdef CONFIG_SCSI_LOGGING
216 /**
217 * scsi_eh_prt_fail_stats - Log info on failures.
218 * @shost: scsi host being recovered.
219 * @work_q: Queue of scsi cmds to process.
220 **/
221 static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
222 struct list_head *work_q)
223 {
224 struct scsi_cmnd *scmd;
225 struct scsi_device *sdev;
226 int total_failures = 0;
227 int cmd_failed = 0;
228 int cmd_cancel = 0;
229 int devices_failed = 0;
230
231 shost_for_each_device(sdev, shost) {
232 list_for_each_entry(scmd, work_q, eh_entry) {
233 if (scmd->device == sdev) {
234 ++total_failures;
235 if (scsi_eh_eflags_chk(scmd,
236 SCSI_EH_CANCEL_CMD))
237 ++cmd_cancel;
238 else
239 ++cmd_failed;
240 }
241 }
242
243 if (cmd_cancel || cmd_failed) {
244 SCSI_LOG_ERROR_RECOVERY(3,
245 printk("%s: %d:%d:%d:%d cmds failed: %d,"
246 " cancel: %d\n",
247 __FUNCTION__, shost->host_no,
248 sdev->channel, sdev->id, sdev->lun,
249 cmd_failed, cmd_cancel));
250 cmd_cancel = 0;
251 cmd_failed = 0;
252 ++devices_failed;
253 }
254 }
255
256 SCSI_LOG_ERROR_RECOVERY(2, printk("Total of %d commands on %d"
257 " devices require eh work\n",
258 total_failures, devices_failed));
259 }
260 #endif
261
262 /**
263 * scsi_check_sense - Examine scsi cmd sense
264 * @scmd: Cmd to have sense checked.
265 *
266 * Return value:
267 * SUCCESS or FAILED or NEEDS_RETRY
268 *
269 * Notes:
270 * When a deferred error is detected the current command has
271 * not been executed and needs retrying.
272 **/
273 static int scsi_check_sense(struct scsi_cmnd *scmd)
274 {
275 struct scsi_sense_hdr sshdr;
276
277 if (! scsi_command_normalize_sense(scmd, &sshdr))
278 return FAILED; /* no valid sense data */
279
280 if (scsi_sense_is_deferred(&sshdr))
281 return NEEDS_RETRY;
282
283 /*
284 * Previous logic looked for FILEMARK, EOM or ILI which are
285 * mainly associated with tapes and returned SUCCESS.
286 */
287 if (sshdr.response_code == 0x70) {
288 /* fixed format */
289 if (scmd->sense_buffer[2] & 0xe0)
290 return SUCCESS;
291 } else {
292 /*
293 * descriptor format: look for "stream commands sense data
294 * descriptor" (see SSC-3). Assume single sense data
295 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
296 */
297 if ((sshdr.additional_length > 3) &&
298 (scmd->sense_buffer[8] == 0x4) &&
299 (scmd->sense_buffer[11] & 0xe0))
300 return SUCCESS;
301 }
302
303 switch (sshdr.sense_key) {
304 case NO_SENSE:
305 return SUCCESS;
306 case RECOVERED_ERROR:
307 return /* soft_error */ SUCCESS;
308
309 case ABORTED_COMMAND:
310 return NEEDS_RETRY;
311 case NOT_READY:
312 case UNIT_ATTENTION:
313 /*
314 * if we are expecting a cc/ua because of a bus reset that we
315 * performed, treat this just as a retry. otherwise this is
316 * information that we should pass up to the upper-level driver
317 * so that we can deal with it there.
318 */
319 if (scmd->device->expecting_cc_ua) {
320 scmd->device->expecting_cc_ua = 0;
321 return NEEDS_RETRY;
322 }
323 /*
324 * if the device is in the process of becoming ready, we
325 * should retry.
326 */
327 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
328 return NEEDS_RETRY;
329 /*
330 * if the device is not started, we need to wake
331 * the error handler to start the motor
332 */
333 if (scmd->device->allow_restart &&
334 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
335 return FAILED;
336 return SUCCESS;
337
338 /* these three are not supported */
339 case COPY_ABORTED:
340 case VOLUME_OVERFLOW:
341 case MISCOMPARE:
342 return SUCCESS;
343
344 case MEDIUM_ERROR:
345 return NEEDS_RETRY;
346
347 case HARDWARE_ERROR:
348 if (scmd->device->retry_hwerror)
349 return NEEDS_RETRY;
350 else
351 return SUCCESS;
352
353 case ILLEGAL_REQUEST:
354 case BLANK_CHECK:
355 case DATA_PROTECT:
356 default:
357 return SUCCESS;
358 }
359 }
360
361 /**
362 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
363 * @scmd: SCSI cmd to examine.
364 *
365 * Notes:
366 * This is *only* called when we are examining the status of commands
367 * queued during error recovery. the main difference here is that we
368 * don't allow for the possibility of retries here, and we are a lot
369 * more restrictive about what we consider acceptable.
370 **/
371 static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
372 {
373 /*
374 * first check the host byte, to see if there is anything in there
375 * that would indicate what we need to do.
376 */
377 if (host_byte(scmd->result) == DID_RESET) {
378 /*
379 * rats. we are already in the error handler, so we now
380 * get to try and figure out what to do next. if the sense
381 * is valid, we have a pretty good idea of what to do.
382 * if not, we mark it as FAILED.
383 */
384 return scsi_check_sense(scmd);
385 }
386 if (host_byte(scmd->result) != DID_OK)
387 return FAILED;
388
389 /*
390 * next, check the message byte.
391 */
392 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
393 return FAILED;
394
395 /*
396 * now, check the status byte to see if this indicates
397 * anything special.
398 */
399 switch (status_byte(scmd->result)) {
400 case GOOD:
401 case COMMAND_TERMINATED:
402 return SUCCESS;
403 case CHECK_CONDITION:
404 return scsi_check_sense(scmd);
405 case CONDITION_GOOD:
406 case INTERMEDIATE_GOOD:
407 case INTERMEDIATE_C_GOOD:
408 /*
409 * who knows? FIXME(eric)
410 */
411 return SUCCESS;
412 case BUSY:
413 case QUEUE_FULL:
414 case RESERVATION_CONFLICT:
415 default:
416 return FAILED;
417 }
418 return FAILED;
419 }
420
421 /**
422 * scsi_eh_times_out - timeout function for error handling.
423 * @scmd: Cmd that is timing out.
424 *
425 * Notes:
426 * During error handling, the kernel thread will be sleeping waiting
427 * for some action to complete on the device. our only job is to
428 * record that it timed out, and to wake up the thread.
429 **/
430 static void scsi_eh_times_out(struct scsi_cmnd *scmd)
431 {
432 scsi_eh_eflags_set(scmd, SCSI_EH_REC_TIMEOUT);
433 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd:%p\n", __FUNCTION__,
434 scmd));
435
436 up(scmd->device->host->eh_action);
437 }
438
439 /**
440 * scsi_eh_done - Completion function for error handling.
441 * @scmd: Cmd that is done.
442 **/
443 static void scsi_eh_done(struct scsi_cmnd *scmd)
444 {
445 /*
446 * if the timeout handler is already running, then just set the
447 * flag which says we finished late, and return. we have no
448 * way of stopping the timeout handler from running, so we must
449 * always defer to it.
450 */
451 if (del_timer(&scmd->eh_timeout)) {
452 scmd->request->rq_status = RQ_SCSI_DONE;
453
454 SCSI_LOG_ERROR_RECOVERY(3, printk("%s scmd: %p result: %x\n",
455 __FUNCTION__, scmd, scmd->result));
456
457 up(scmd->device->host->eh_action);
458 }
459 }
460
461 /**
462 * scsi_send_eh_cmnd - send a cmd to a device as part of error recovery.
463 * @scmd: SCSI Cmd to send.
464 * @timeout: Timeout for cmd.
465 *
466 * Notes:
467 * The initialization of the structures is quite a bit different in
468 * this case, and furthermore, there is a different completion handler
469 * vs scsi_dispatch_cmd.
470 * Return value:
471 * SUCCESS or FAILED or NEEDS_RETRY
472 **/
473 static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, int timeout)
474 {
475 struct scsi_device *sdev = scmd->device;
476 struct Scsi_Host *shost = sdev->host;
477 DECLARE_MUTEX_LOCKED(sem);
478 unsigned long flags;
479 int rtn = SUCCESS;
480
481 /*
482 * we will use a queued command if possible, otherwise we will
483 * emulate the queuing and calling of completion function ourselves.
484 */
485 if (sdev->scsi_level <= SCSI_2)
486 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
487 (sdev->lun << 5 & 0xe0);
488
489 scsi_add_timer(scmd, timeout, scsi_eh_times_out);
490
491 /*
492 * set up the semaphore so we wait for the command to complete.
493 */
494 shost->eh_action = &sem;
495 scmd->request->rq_status = RQ_SCSI_BUSY;
496
497 spin_lock_irqsave(shost->host_lock, flags);
498 scsi_log_send(scmd);
499 shost->hostt->queuecommand(scmd, scsi_eh_done);
500 spin_unlock_irqrestore(shost->host_lock, flags);
501
502 down(&sem);
503 scsi_log_completion(scmd, SUCCESS);
504
505 shost->eh_action = NULL;
506
507 /*
508 * see if timeout. if so, tell the host to forget about it.
509 * in other words, we don't want a callback any more.
510 */
511 if (scsi_eh_eflags_chk(scmd, SCSI_EH_REC_TIMEOUT)) {
512 scsi_eh_eflags_clr(scmd, SCSI_EH_REC_TIMEOUT);
513
514 /*
515 * as far as the low level driver is
516 * concerned, this command is still active, so
517 * we must give the low level driver a chance
518 * to abort it. (db)
519 *
520 * FIXME(eric) - we are not tracking whether we could
521 * abort a timed out command or not. not sure how
522 * we should treat them differently anyways.
523 */
524 if (shost->hostt->eh_abort_handler)
525 shost->hostt->eh_abort_handler(scmd);
526
527 scmd->request->rq_status = RQ_SCSI_DONE;
528 rtn = FAILED;
529 }
530
531 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd: %p, rtn:%x\n",
532 __FUNCTION__, scmd, rtn));
533
534 /*
535 * now examine the actual status codes to see whether the command
536 * actually did complete normally.
537 */
538 if (rtn == SUCCESS) {
539 rtn = scsi_eh_completed_normally(scmd);
540 SCSI_LOG_ERROR_RECOVERY(3,
541 printk("%s: scsi_eh_completed_normally %x\n",
542 __FUNCTION__, rtn));
543 switch (rtn) {
544 case SUCCESS:
545 case NEEDS_RETRY:
546 case FAILED:
547 break;
548 default:
549 rtn = FAILED;
550 break;
551 }
552 }
553
554 return rtn;
555 }
556
557 /**
558 * scsi_request_sense - Request sense data from a particular target.
559 * @scmd: SCSI cmd for request sense.
560 *
561 * Notes:
562 * Some hosts automatically obtain this information, others require
563 * that we obtain it on our own. This function will *not* return until
564 * the command either times out, or it completes.
565 **/
566 static int scsi_request_sense(struct scsi_cmnd *scmd)
567 {
568 static unsigned char generic_sense[6] =
569 {REQUEST_SENSE, 0, 0, 0, 252, 0};
570 unsigned char *scsi_result;
571 int saved_result;
572 int rtn;
573
574 memcpy(scmd->cmnd, generic_sense, sizeof(generic_sense));
575
576 scsi_result = kmalloc(252, GFP_ATOMIC | ((scmd->device->host->hostt->unchecked_isa_dma) ? __GFP_DMA : 0));
577
578
579 if (unlikely(!scsi_result)) {
580 printk(KERN_ERR "%s: cannot allocate scsi_result.\n",
581 __FUNCTION__);
582 return FAILED;
583 }
584
585 /*
586 * zero the sense buffer. some host adapters automatically always
587 * request sense, so it is not a good idea that
588 * scmd->request_buffer and scmd->sense_buffer point to the same
589 * address (db). 0 is not a valid sense code.
590 */
591 memset(scmd->sense_buffer, 0, sizeof(scmd->sense_buffer));
592 memset(scsi_result, 0, 252);
593
594 saved_result = scmd->result;
595 scmd->request_buffer = scsi_result;
596 scmd->request_bufflen = 252;
597 scmd->use_sg = 0;
598 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
599 scmd->sc_data_direction = DMA_FROM_DEVICE;
600 scmd->underflow = 0;
601
602 rtn = scsi_send_eh_cmnd(scmd, SENSE_TIMEOUT);
603
604 /* last chance to have valid sense data */
605 if(!SCSI_SENSE_VALID(scmd)) {
606 memcpy(scmd->sense_buffer, scmd->request_buffer,
607 sizeof(scmd->sense_buffer));
608 }
609
610 kfree(scsi_result);
611
612 /*
613 * when we eventually call scsi_finish, we really wish to complete
614 * the original request, so let's restore the original data. (db)
615 */
616 scsi_setup_cmd_retry(scmd);
617 scmd->result = saved_result;
618 return rtn;
619 }
620
621 /**
622 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
623 * @scmd: Original SCSI cmd that eh has finished.
624 * @done_q: Queue for processed commands.
625 *
626 * Notes:
627 * We don't want to use the normal command completion while we are are
628 * still handling errors - it may cause other commands to be queued,
629 * and that would disturb what we are doing. thus we really want to
630 * keep a list of pending commands for final completion, and once we
631 * are ready to leave error handling we handle completion for real.
632 **/
633 static void scsi_eh_finish_cmd(struct scsi_cmnd *scmd,
634 struct list_head *done_q)
635 {
636 scmd->device->host->host_failed--;
637 scmd->state = SCSI_STATE_BHQUEUE;
638
639 scsi_eh_eflags_clr_all(scmd);
640
641 /*
642 * set this back so that the upper level can correctly free up
643 * things.
644 */
645 scsi_setup_cmd_retry(scmd);
646 list_move_tail(&scmd->eh_entry, done_q);
647 }
648
649 /**
650 * scsi_eh_get_sense - Get device sense data.
651 * @work_q: Queue of commands to process.
652 * @done_q: Queue of proccessed commands..
653 *
654 * Description:
655 * See if we need to request sense information. if so, then get it
656 * now, so we have a better idea of what to do.
657 *
658 * Notes:
659 * This has the unfortunate side effect that if a shost adapter does
660 * not automatically request sense information, that we end up shutting
661 * it down before we request it.
662 *
663 * All drivers should request sense information internally these days,
664 * so for now all I have to say is tough noogies if you end up in here.
665 *
666 * XXX: Long term this code should go away, but that needs an audit of
667 * all LLDDs first.
668 **/
669 static int scsi_eh_get_sense(struct list_head *work_q,
670 struct list_head *done_q)
671 {
672 struct list_head *lh, *lh_sf;
673 struct scsi_cmnd *scmd;
674 int rtn;
675
676 list_for_each_safe(lh, lh_sf, work_q) {
677 scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
678 if (scsi_eh_eflags_chk(scmd, SCSI_EH_CANCEL_CMD) ||
679 SCSI_SENSE_VALID(scmd))
680 continue;
681
682 SCSI_LOG_ERROR_RECOVERY(2, printk("%s: requesting sense"
683 " for id: %d\n",
684 current->comm,
685 scmd->device->id));
686 rtn = scsi_request_sense(scmd);
687 if (rtn != SUCCESS)
688 continue;
689
690 SCSI_LOG_ERROR_RECOVERY(3, printk("sense requested for %p"
691 " result %x\n", scmd,
692 scmd->result));
693 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense("bh", scmd));
694
695 rtn = scsi_decide_disposition(scmd);
696
697 /*
698 * if the result was normal, then just pass it along to the
699 * upper level.
700 */
701 if (rtn == SUCCESS)
702 /* we don't want this command reissued, just
703 * finished with the sense data, so set
704 * retries to the max allowed to ensure it
705 * won't get reissued */
706 scmd->retries = scmd->allowed;
707 else if (rtn != NEEDS_RETRY)
708 continue;
709
710 scsi_eh_finish_cmd(scmd, done_q);
711 }
712
713 return list_empty(work_q);
714 }
715
716 /**
717 * scsi_try_to_abort_cmd - Ask host to abort a running command.
718 * @scmd: SCSI cmd to abort from Lower Level.
719 *
720 * Notes:
721 * This function will not return until the user's completion function
722 * has been called. there is no timeout on this operation. if the
723 * author of the low-level driver wishes this operation to be timed,
724 * they can provide this facility themselves. helper functions in
725 * scsi_error.c can be supplied to make this easier to do.
726 **/
727 static int scsi_try_to_abort_cmd(struct scsi_cmnd *scmd)
728 {
729 if (!scmd->device->host->hostt->eh_abort_handler)
730 return FAILED;
731
732 /*
733 * scsi_done was called just after the command timed out and before
734 * we had a chance to process it. (db)
735 */
736 if (scmd->serial_number == 0)
737 return SUCCESS;
738 return scmd->device->host->hostt->eh_abort_handler(scmd);
739 }
740
741 /**
742 * scsi_eh_tur - Send TUR to device.
743 * @scmd: Scsi cmd to send TUR
744 *
745 * Return value:
746 * 0 - Device is ready. 1 - Device NOT ready.
747 **/
748 static int scsi_eh_tur(struct scsi_cmnd *scmd)
749 {
750 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
751 int retry_cnt = 1, rtn;
752 int saved_result;
753
754 retry_tur:
755 memcpy(scmd->cmnd, tur_command, sizeof(tur_command));
756
757 /*
758 * zero the sense buffer. the scsi spec mandates that any
759 * untransferred sense data should be interpreted as being zero.
760 */
761 memset(scmd->sense_buffer, 0, sizeof(scmd->sense_buffer));
762
763 saved_result = scmd->result;
764 scmd->request_buffer = NULL;
765 scmd->request_bufflen = 0;
766 scmd->use_sg = 0;
767 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
768 scmd->underflow = 0;
769 scmd->sc_data_direction = DMA_NONE;
770
771 rtn = scsi_send_eh_cmnd(scmd, SENSE_TIMEOUT);
772
773 /*
774 * when we eventually call scsi_finish, we really wish to complete
775 * the original request, so let's restore the original data. (db)
776 */
777 scsi_setup_cmd_retry(scmd);
778 scmd->result = saved_result;
779
780 /*
781 * hey, we are done. let's look to see what happened.
782 */
783 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd %p rtn %x\n",
784 __FUNCTION__, scmd, rtn));
785 if (rtn == SUCCESS)
786 return 0;
787 else if (rtn == NEEDS_RETRY)
788 if (retry_cnt--)
789 goto retry_tur;
790 return 1;
791 }
792
793 /**
794 * scsi_eh_abort_cmds - abort canceled commands.
795 * @shost: scsi host being recovered.
796 * @eh_done_q: list_head for processed commands.
797 *
798 * Decription:
799 * Try and see whether or not it makes sense to try and abort the
800 * running command. this only works out to be the case if we have one
801 * command that has timed out. if the command simply failed, it makes
802 * no sense to try and abort the command, since as far as the shost
803 * adapter is concerned, it isn't running.
804 **/
805 static int scsi_eh_abort_cmds(struct list_head *work_q,
806 struct list_head *done_q)
807 {
808 struct list_head *lh, *lh_sf;
809 struct scsi_cmnd *scmd;
810 int rtn;
811
812 list_for_each_safe(lh, lh_sf, work_q) {
813 scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
814 if (!scsi_eh_eflags_chk(scmd, SCSI_EH_CANCEL_CMD))
815 continue;
816 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting cmd:"
817 "0x%p\n", current->comm,
818 scmd));
819 rtn = scsi_try_to_abort_cmd(scmd);
820 if (rtn == SUCCESS) {
821 scsi_eh_eflags_clr(scmd, SCSI_EH_CANCEL_CMD);
822 if (!scsi_device_online(scmd->device) ||
823 !scsi_eh_tur(scmd)) {
824 scsi_eh_finish_cmd(scmd, done_q);
825 }
826
827 } else
828 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting"
829 " cmd failed:"
830 "0x%p\n",
831 current->comm,
832 scmd));
833 }
834
835 return list_empty(work_q);
836 }
837
838 /**
839 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
840 * @scmd: SCSI cmd used to send BDR
841 *
842 * Notes:
843 * There is no timeout for this operation. if this operation is
844 * unreliable for a given host, then the host itself needs to put a
845 * timer on it, and set the host back to a consistent state prior to
846 * returning.
847 **/
848 static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
849 {
850 int rtn;
851
852 if (!scmd->device->host->hostt->eh_device_reset_handler)
853 return FAILED;
854
855 rtn = scmd->device->host->hostt->eh_device_reset_handler(scmd);
856 if (rtn == SUCCESS) {
857 scmd->device->was_reset = 1;
858 scmd->device->expecting_cc_ua = 1;
859 }
860
861 return rtn;
862 }
863
864 /**
865 * scsi_eh_try_stu - Send START_UNIT to device.
866 * @scmd: Scsi cmd to send START_UNIT
867 *
868 * Return value:
869 * 0 - Device is ready. 1 - Device NOT ready.
870 **/
871 static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
872 {
873 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
874 int rtn;
875 int saved_result;
876
877 if (!scmd->device->allow_restart)
878 return 1;
879
880 memcpy(scmd->cmnd, stu_command, sizeof(stu_command));
881
882 /*
883 * zero the sense buffer. the scsi spec mandates that any
884 * untransferred sense data should be interpreted as being zero.
885 */
886 memset(scmd->sense_buffer, 0, sizeof(scmd->sense_buffer));
887
888 saved_result = scmd->result;
889 scmd->request_buffer = NULL;
890 scmd->request_bufflen = 0;
891 scmd->use_sg = 0;
892 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
893 scmd->underflow = 0;
894 scmd->sc_data_direction = DMA_NONE;
895
896 rtn = scsi_send_eh_cmnd(scmd, START_UNIT_TIMEOUT);
897
898 /*
899 * when we eventually call scsi_finish, we really wish to complete
900 * the original request, so let's restore the original data. (db)
901 */
902 scsi_setup_cmd_retry(scmd);
903 scmd->result = saved_result;
904
905 /*
906 * hey, we are done. let's look to see what happened.
907 */
908 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd %p rtn %x\n",
909 __FUNCTION__, scmd, rtn));
910 if (rtn == SUCCESS)
911 return 0;
912 return 1;
913 }
914
915 /**
916 * scsi_eh_stu - send START_UNIT if needed
917 * @shost: scsi host being recovered.
918 * @eh_done_q: list_head for processed commands.
919 *
920 * Notes:
921 * If commands are failing due to not ready, initializing command required,
922 * try revalidating the device, which will end up sending a start unit.
923 **/
924 static int scsi_eh_stu(struct Scsi_Host *shost,
925 struct list_head *work_q,
926 struct list_head *done_q)
927 {
928 struct list_head *lh, *lh_sf;
929 struct scsi_cmnd *scmd, *stu_scmd;
930 struct scsi_device *sdev;
931
932 shost_for_each_device(sdev, shost) {
933 stu_scmd = NULL;
934 list_for_each_entry(scmd, work_q, eh_entry)
935 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
936 scsi_check_sense(scmd) == FAILED ) {
937 stu_scmd = scmd;
938 break;
939 }
940
941 if (!stu_scmd)
942 continue;
943
944 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending START_UNIT to sdev:"
945 " 0x%p\n", current->comm, sdev));
946
947 if (!scsi_eh_try_stu(stu_scmd)) {
948 if (!scsi_device_online(sdev) ||
949 !scsi_eh_tur(stu_scmd)) {
950 list_for_each_safe(lh, lh_sf, work_q) {
951 scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
952 if (scmd->device == sdev)
953 scsi_eh_finish_cmd(scmd, done_q);
954 }
955 }
956 } else {
957 SCSI_LOG_ERROR_RECOVERY(3,
958 printk("%s: START_UNIT failed to sdev:"
959 " 0x%p\n", current->comm, sdev));
960 }
961 }
962
963 return list_empty(work_q);
964 }
965
966
967 /**
968 * scsi_eh_bus_device_reset - send bdr if needed
969 * @shost: scsi host being recovered.
970 * @eh_done_q: list_head for processed commands.
971 *
972 * Notes:
973 * Try a bus device reset. still, look to see whether we have multiple
974 * devices that are jammed or not - if we have multiple devices, it
975 * makes no sense to try bus_device_reset - we really would need to try
976 * a bus_reset instead.
977 **/
978 static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
979 struct list_head *work_q,
980 struct list_head *done_q)
981 {
982 struct list_head *lh, *lh_sf;
983 struct scsi_cmnd *scmd, *bdr_scmd;
984 struct scsi_device *sdev;
985 int rtn;
986
987 shost_for_each_device(sdev, shost) {
988 bdr_scmd = NULL;
989 list_for_each_entry(scmd, work_q, eh_entry)
990 if (scmd->device == sdev) {
991 bdr_scmd = scmd;
992 break;
993 }
994
995 if (!bdr_scmd)
996 continue;
997
998 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BDR sdev:"
999 " 0x%p\n", current->comm,
1000 sdev));
1001 rtn = scsi_try_bus_device_reset(bdr_scmd);
1002 if (rtn == SUCCESS) {
1003 if (!scsi_device_online(sdev) ||
1004 !scsi_eh_tur(bdr_scmd)) {
1005 list_for_each_safe(lh, lh_sf,
1006 work_q) {
1007 scmd = list_entry(lh, struct
1008 scsi_cmnd,
1009 eh_entry);
1010 if (scmd->device == sdev)
1011 scsi_eh_finish_cmd(scmd,
1012 done_q);
1013 }
1014 }
1015 } else {
1016 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BDR"
1017 " failed sdev:"
1018 "0x%p\n",
1019 current->comm,
1020 sdev));
1021 }
1022 }
1023
1024 return list_empty(work_q);
1025 }
1026
1027 /**
1028 * scsi_try_bus_reset - ask host to perform a bus reset
1029 * @scmd: SCSI cmd to send bus reset.
1030 **/
1031 static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
1032 {
1033 unsigned long flags;
1034 int rtn;
1035
1036 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Bus RST\n",
1037 __FUNCTION__));
1038
1039 if (!scmd->device->host->hostt->eh_bus_reset_handler)
1040 return FAILED;
1041
1042 rtn = scmd->device->host->hostt->eh_bus_reset_handler(scmd);
1043
1044 if (rtn == SUCCESS) {
1045 if (!scmd->device->host->hostt->skip_settle_delay)
1046 ssleep(BUS_RESET_SETTLE_TIME);
1047 spin_lock_irqsave(scmd->device->host->host_lock, flags);
1048 scsi_report_bus_reset(scmd->device->host, scmd->device->channel);
1049 spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
1050 }
1051
1052 return rtn;
1053 }
1054
1055 /**
1056 * scsi_try_host_reset - ask host adapter to reset itself
1057 * @scmd: SCSI cmd to send hsot reset.
1058 **/
1059 static int scsi_try_host_reset(struct scsi_cmnd *scmd)
1060 {
1061 unsigned long flags;
1062 int rtn;
1063
1064 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Host RST\n",
1065 __FUNCTION__));
1066
1067 if (!scmd->device->host->hostt->eh_host_reset_handler)
1068 return FAILED;
1069
1070 rtn = scmd->device->host->hostt->eh_host_reset_handler(scmd);
1071
1072 if (rtn == SUCCESS) {
1073 if (!scmd->device->host->hostt->skip_settle_delay)
1074 ssleep(HOST_RESET_SETTLE_TIME);
1075 spin_lock_irqsave(scmd->device->host->host_lock, flags);
1076 scsi_report_bus_reset(scmd->device->host, scmd->device->channel);
1077 spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
1078 }
1079
1080 return rtn;
1081 }
1082
1083 /**
1084 * scsi_eh_bus_reset - send a bus reset
1085 * @shost: scsi host being recovered.
1086 * @eh_done_q: list_head for processed commands.
1087 **/
1088 static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1089 struct list_head *work_q,
1090 struct list_head *done_q)
1091 {
1092 struct list_head *lh, *lh_sf;
1093 struct scsi_cmnd *scmd;
1094 struct scsi_cmnd *chan_scmd;
1095 unsigned int channel;
1096 int rtn;
1097
1098 /*
1099 * we really want to loop over the various channels, and do this on
1100 * a channel by channel basis. we should also check to see if any
1101 * of the failed commands are on soft_reset devices, and if so, skip
1102 * the reset.
1103 */
1104
1105 for (channel = 0; channel <= shost->max_channel; channel++) {
1106 chan_scmd = NULL;
1107 list_for_each_entry(scmd, work_q, eh_entry) {
1108 if (channel == scmd->device->channel) {
1109 chan_scmd = scmd;
1110 break;
1111 /*
1112 * FIXME add back in some support for
1113 * soft_reset devices.
1114 */
1115 }
1116 }
1117
1118 if (!chan_scmd)
1119 continue;
1120 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BRST chan:"
1121 " %d\n", current->comm,
1122 channel));
1123 rtn = scsi_try_bus_reset(chan_scmd);
1124 if (rtn == SUCCESS) {
1125 list_for_each_safe(lh, lh_sf, work_q) {
1126 scmd = list_entry(lh, struct scsi_cmnd,
1127 eh_entry);
1128 if (channel == scmd->device->channel)
1129 if (!scsi_device_online(scmd->device) ||
1130 !scsi_eh_tur(scmd))
1131 scsi_eh_finish_cmd(scmd,
1132 done_q);
1133 }
1134 } else {
1135 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BRST"
1136 " failed chan: %d\n",
1137 current->comm,
1138 channel));
1139 }
1140 }
1141 return list_empty(work_q);
1142 }
1143
1144 /**
1145 * scsi_eh_host_reset - send a host reset
1146 * @work_q: list_head for processed commands.
1147 * @done_q: list_head for processed commands.
1148 **/
1149 static int scsi_eh_host_reset(struct list_head *work_q,
1150 struct list_head *done_q)
1151 {
1152 int rtn;
1153 struct list_head *lh, *lh_sf;
1154 struct scsi_cmnd *scmd;
1155
1156 if (!list_empty(work_q)) {
1157 scmd = list_entry(work_q->next,
1158 struct scsi_cmnd, eh_entry);
1159
1160 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending HRST\n"
1161 , current->comm));
1162
1163 rtn = scsi_try_host_reset(scmd);
1164 if (rtn == SUCCESS) {
1165 list_for_each_safe(lh, lh_sf, work_q) {
1166 scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
1167 if (!scsi_device_online(scmd->device) ||
1168 (!scsi_eh_try_stu(scmd) && !scsi_eh_tur(scmd)) ||
1169 !scsi_eh_tur(scmd))
1170 scsi_eh_finish_cmd(scmd, done_q);
1171 }
1172 } else {
1173 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: HRST"
1174 " failed\n",
1175 current->comm));
1176 }
1177 }
1178 return list_empty(work_q);
1179 }
1180
1181 /**
1182 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
1183 * @work_q: list_head for processed commands.
1184 * @done_q: list_head for processed commands.
1185 *
1186 **/
1187 static void scsi_eh_offline_sdevs(struct list_head *work_q,
1188 struct list_head *done_q)
1189 {
1190 struct list_head *lh, *lh_sf;
1191 struct scsi_cmnd *scmd;
1192
1193 list_for_each_safe(lh, lh_sf, work_q) {
1194 scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
1195 printk(KERN_INFO "scsi: Device offlined - not"
1196 " ready after error recovery: host"
1197 " %d channel %d id %d lun %d\n",
1198 scmd->device->host->host_no,
1199 scmd->device->channel,
1200 scmd->device->id,
1201 scmd->device->lun);
1202 scsi_device_set_state(scmd->device, SDEV_OFFLINE);
1203 if (scsi_eh_eflags_chk(scmd, SCSI_EH_CANCEL_CMD)) {
1204 /*
1205 * FIXME: Handle lost cmds.
1206 */
1207 }
1208 scsi_eh_finish_cmd(scmd, done_q);
1209 }
1210 return;
1211 }
1212
1213 /**
1214 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1215 * @scmd: SCSI cmd to examine.
1216 *
1217 * Notes:
1218 * This is *only* called when we are examining the status after sending
1219 * out the actual data command. any commands that are queued for error
1220 * recovery (e.g. test_unit_ready) do *not* come through here.
1221 *
1222 * When this routine returns failed, it means the error handler thread
1223 * is woken. In cases where the error code indicates an error that
1224 * doesn't require the error handler read (i.e. we don't need to
1225 * abort/reset), this function should return SUCCESS.
1226 **/
1227 int scsi_decide_disposition(struct scsi_cmnd *scmd)
1228 {
1229 int rtn;
1230
1231 /*
1232 * if the device is offline, then we clearly just pass the result back
1233 * up to the top level.
1234 */
1235 if (!scsi_device_online(scmd->device)) {
1236 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: device offline - report"
1237 " as SUCCESS\n",
1238 __FUNCTION__));
1239 return SUCCESS;
1240 }
1241
1242 /*
1243 * first check the host byte, to see if there is anything in there
1244 * that would indicate what we need to do.
1245 */
1246 switch (host_byte(scmd->result)) {
1247 case DID_PASSTHROUGH:
1248 /*
1249 * no matter what, pass this through to the upper layer.
1250 * nuke this special code so that it looks like we are saying
1251 * did_ok.
1252 */
1253 scmd->result &= 0xff00ffff;
1254 return SUCCESS;
1255 case DID_OK:
1256 /*
1257 * looks good. drop through, and check the next byte.
1258 */
1259 break;
1260 case DID_NO_CONNECT:
1261 case DID_BAD_TARGET:
1262 case DID_ABORT:
1263 /*
1264 * note - this means that we just report the status back
1265 * to the top level driver, not that we actually think
1266 * that it indicates SUCCESS.
1267 */
1268 return SUCCESS;
1269 /*
1270 * when the low level driver returns did_soft_error,
1271 * it is responsible for keeping an internal retry counter
1272 * in order to avoid endless loops (db)
1273 *
1274 * actually this is a bug in this function here. we should
1275 * be mindful of the maximum number of retries specified
1276 * and not get stuck in a loop.
1277 */
1278 case DID_SOFT_ERROR:
1279 goto maybe_retry;
1280 case DID_IMM_RETRY:
1281 return NEEDS_RETRY;
1282
1283 case DID_REQUEUE:
1284 return ADD_TO_MLQUEUE;
1285
1286 case DID_ERROR:
1287 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1288 status_byte(scmd->result) == RESERVATION_CONFLICT)
1289 /*
1290 * execute reservation conflict processing code
1291 * lower down
1292 */
1293 break;
1294 /* fallthrough */
1295
1296 case DID_BUS_BUSY:
1297 case DID_PARITY:
1298 goto maybe_retry;
1299 case DID_TIME_OUT:
1300 /*
1301 * when we scan the bus, we get timeout messages for
1302 * these commands if there is no device available.
1303 * other hosts report did_no_connect for the same thing.
1304 */
1305 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1306 scmd->cmnd[0] == INQUIRY)) {
1307 return SUCCESS;
1308 } else {
1309 return FAILED;
1310 }
1311 case DID_RESET:
1312 return SUCCESS;
1313 default:
1314 return FAILED;
1315 }
1316
1317 /*
1318 * next, check the message byte.
1319 */
1320 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
1321 return FAILED;
1322
1323 /*
1324 * check the status byte to see if this indicates anything special.
1325 */
1326 switch (status_byte(scmd->result)) {
1327 case QUEUE_FULL:
1328 /*
1329 * the case of trying to send too many commands to a
1330 * tagged queueing device.
1331 */
1332 case BUSY:
1333 /*
1334 * device can't talk to us at the moment. Should only
1335 * occur (SAM-3) when the task queue is empty, so will cause
1336 * the empty queue handling to trigger a stall in the
1337 * device.
1338 */
1339 return ADD_TO_MLQUEUE;
1340 case GOOD:
1341 case COMMAND_TERMINATED:
1342 case TASK_ABORTED:
1343 return SUCCESS;
1344 case CHECK_CONDITION:
1345 rtn = scsi_check_sense(scmd);
1346 if (rtn == NEEDS_RETRY)
1347 goto maybe_retry;
1348 /* if rtn == FAILED, we have no sense information;
1349 * returning FAILED will wake the error handler thread
1350 * to collect the sense and redo the decide
1351 * disposition */
1352 return rtn;
1353 case CONDITION_GOOD:
1354 case INTERMEDIATE_GOOD:
1355 case INTERMEDIATE_C_GOOD:
1356 case ACA_ACTIVE:
1357 /*
1358 * who knows? FIXME(eric)
1359 */
1360 return SUCCESS;
1361
1362 case RESERVATION_CONFLICT:
1363 printk(KERN_INFO "scsi: reservation conflict: host"
1364 " %d channel %d id %d lun %d\n",
1365 scmd->device->host->host_no, scmd->device->channel,
1366 scmd->device->id, scmd->device->lun);
1367 return SUCCESS; /* causes immediate i/o error */
1368 default:
1369 return FAILED;
1370 }
1371 return FAILED;
1372
1373 maybe_retry:
1374
1375 /* we requeue for retry because the error was retryable, and
1376 * the request was not marked fast fail. Note that above,
1377 * even if the request is marked fast fail, we still requeue
1378 * for queue congestion conditions (QUEUE_FULL or BUSY) */
1379 if ((++scmd->retries) < scmd->allowed
1380 && !blk_noretry_request(scmd->request)) {
1381 return NEEDS_RETRY;
1382 } else {
1383 /*
1384 * no more retries - report this one back to upper level.
1385 */
1386 return SUCCESS;
1387 }
1388 }
1389
1390 /**
1391 * scsi_eh_lock_done - done function for eh door lock request
1392 * @scmd: SCSI command block for the door lock request
1393 *
1394 * Notes:
1395 * We completed the asynchronous door lock request, and it has either
1396 * locked the door or failed. We must free the command structures
1397 * associated with this request.
1398 **/
1399 static void scsi_eh_lock_done(struct scsi_cmnd *scmd)
1400 {
1401 struct scsi_request *sreq = scmd->sc_request;
1402
1403 scsi_release_request(sreq);
1404 }
1405
1406
1407 /**
1408 * scsi_eh_lock_door - Prevent medium removal for the specified device
1409 * @sdev: SCSI device to prevent medium removal
1410 *
1411 * Locking:
1412 * We must be called from process context; scsi_allocate_request()
1413 * may sleep.
1414 *
1415 * Notes:
1416 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1417 * head of the devices request queue, and continue.
1418 *
1419 * Bugs:
1420 * scsi_allocate_request() may sleep waiting for existing requests to
1421 * be processed. However, since we haven't kicked off any request
1422 * processing for this host, this may deadlock.
1423 *
1424 * If scsi_allocate_request() fails for what ever reason, we
1425 * completely forget to lock the door.
1426 **/
1427 static void scsi_eh_lock_door(struct scsi_device *sdev)
1428 {
1429 struct scsi_request *sreq = scsi_allocate_request(sdev, GFP_KERNEL);
1430
1431 if (unlikely(!sreq)) {
1432 printk(KERN_ERR "%s: request allocate failed,"
1433 "prevent media removal cmd not sent\n", __FUNCTION__);
1434 return;
1435 }
1436
1437 sreq->sr_cmnd[0] = ALLOW_MEDIUM_REMOVAL;
1438 sreq->sr_cmnd[1] = 0;
1439 sreq->sr_cmnd[2] = 0;
1440 sreq->sr_cmnd[3] = 0;
1441 sreq->sr_cmnd[4] = SCSI_REMOVAL_PREVENT;
1442 sreq->sr_cmnd[5] = 0;
1443 sreq->sr_data_direction = DMA_NONE;
1444 sreq->sr_bufflen = 0;
1445 sreq->sr_buffer = NULL;
1446 sreq->sr_allowed = 5;
1447 sreq->sr_done = scsi_eh_lock_done;
1448 sreq->sr_timeout_per_command = 10 * HZ;
1449 sreq->sr_cmd_len = COMMAND_SIZE(sreq->sr_cmnd[0]);
1450
1451 scsi_insert_special_req(sreq, 1);
1452 }
1453
1454
1455 /**
1456 * scsi_restart_operations - restart io operations to the specified host.
1457 * @shost: Host we are restarting.
1458 *
1459 * Notes:
1460 * When we entered the error handler, we blocked all further i/o to
1461 * this device. we need to 'reverse' this process.
1462 **/
1463 static void scsi_restart_operations(struct Scsi_Host *shost)
1464 {
1465 struct scsi_device *sdev;
1466
1467 /*
1468 * If the door was locked, we need to insert a door lock request
1469 * onto the head of the SCSI request queue for the device. There
1470 * is no point trying to lock the door of an off-line device.
1471 */
1472 shost_for_each_device(sdev, shost) {
1473 if (scsi_device_online(sdev) && sdev->locked)
1474 scsi_eh_lock_door(sdev);
1475 }
1476
1477 /*
1478 * next free up anything directly waiting upon the host. this
1479 * will be requests for character device operations, and also for
1480 * ioctls to queued block devices.
1481 */
1482 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: waking up host to restart\n",
1483 __FUNCTION__));
1484
1485 clear_bit(SHOST_RECOVERY, &shost->shost_state);
1486
1487 wake_up(&shost->host_wait);
1488
1489 /*
1490 * finally we need to re-initiate requests that may be pending. we will
1491 * have had everything blocked while error handling is taking place, and
1492 * now that error recovery is done, we will need to ensure that these
1493 * requests are started.
1494 */
1495 scsi_run_host_queues(shost);
1496 }
1497
1498 /**
1499 * scsi_eh_ready_devs - check device ready state and recover if not.
1500 * @shost: host to be recovered.
1501 * @eh_done_q: list_head for processed commands.
1502 *
1503 **/
1504 static void scsi_eh_ready_devs(struct Scsi_Host *shost,
1505 struct list_head *work_q,
1506 struct list_head *done_q)
1507 {
1508 if (!scsi_eh_stu(shost, work_q, done_q))
1509 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
1510 if (!scsi_eh_bus_reset(shost, work_q, done_q))
1511 if (!scsi_eh_host_reset(work_q, done_q))
1512 scsi_eh_offline_sdevs(work_q, done_q);
1513 }
1514
1515 /**
1516 * scsi_eh_flush_done_q - finish processed commands or retry them.
1517 * @done_q: list_head of processed commands.
1518 *
1519 **/
1520 static void scsi_eh_flush_done_q(struct list_head *done_q)
1521 {
1522 struct list_head *lh, *lh_sf;
1523 struct scsi_cmnd *scmd;
1524
1525 list_for_each_safe(lh, lh_sf, done_q) {
1526 scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
1527 list_del_init(lh);
1528 if (scsi_device_online(scmd->device) &&
1529 !blk_noretry_request(scmd->request) &&
1530 (++scmd->retries < scmd->allowed)) {
1531 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush"
1532 " retry cmd: %p\n",
1533 current->comm,
1534 scmd));
1535 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
1536 } else {
1537 /*
1538 * If just we got sense for the device (called
1539 * scsi_eh_get_sense), scmd->result is already
1540 * set, do not set DRIVER_TIMEOUT.
1541 */
1542 if (!scmd->result)
1543 scmd->result |= (DRIVER_TIMEOUT << 24);
1544 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush finish"
1545 " cmd: %p\n",
1546 current->comm, scmd));
1547 scsi_finish_command(scmd);
1548 }
1549 }
1550 }
1551
1552 /**
1553 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
1554 * @shost: Host to unjam.
1555 *
1556 * Notes:
1557 * When we come in here, we *know* that all commands on the bus have
1558 * either completed, failed or timed out. we also know that no further
1559 * commands are being sent to the host, so things are relatively quiet
1560 * and we have freedom to fiddle with things as we wish.
1561 *
1562 * This is only the *default* implementation. it is possible for
1563 * individual drivers to supply their own version of this function, and
1564 * if the maintainer wishes to do this, it is strongly suggested that
1565 * this function be taken as a template and modified. this function
1566 * was designed to correctly handle problems for about 95% of the
1567 * different cases out there, and it should always provide at least a
1568 * reasonable amount of error recovery.
1569 *
1570 * Any command marked 'failed' or 'timeout' must eventually have
1571 * scsi_finish_cmd() called for it. we do all of the retry stuff
1572 * here, so when we restart the host after we return it should have an
1573 * empty queue.
1574 **/
1575 static void scsi_unjam_host(struct Scsi_Host *shost)
1576 {
1577 unsigned long flags;
1578 LIST_HEAD(eh_work_q);
1579 LIST_HEAD(eh_done_q);
1580
1581 spin_lock_irqsave(shost->host_lock, flags);
1582 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
1583 spin_unlock_irqrestore(shost->host_lock, flags);
1584
1585 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
1586
1587 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
1588 if (!scsi_eh_abort_cmds(&eh_work_q, &eh_done_q))
1589 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
1590
1591 scsi_eh_flush_done_q(&eh_done_q);
1592 }
1593
1594 /**
1595 * scsi_error_handler - Handle errors/timeouts of SCSI cmds.
1596 * @data: Host for which we are running.
1597 *
1598 * Notes:
1599 * This is always run in the context of a kernel thread. The idea is
1600 * that we start this thing up when the kernel starts up (one per host
1601 * that we detect), and it immediately goes to sleep and waits for some
1602 * event (i.e. failure). When this takes place, we have the job of
1603 * trying to unjam the bus and restarting things.
1604 **/
1605 int scsi_error_handler(void *data)
1606 {
1607 struct Scsi_Host *shost = (struct Scsi_Host *) data;
1608 int rtn;
1609 DECLARE_MUTEX_LOCKED(sem);
1610
1611 /*
1612 * Flush resources
1613 */
1614
1615 daemonize("scsi_eh_%d", shost->host_no);
1616
1617 current->flags |= PF_NOFREEZE;
1618
1619 shost->eh_wait = &sem;
1620 shost->ehandler = current;
1621
1622 /*
1623 * Wake up the thread that created us.
1624 */
1625 SCSI_LOG_ERROR_RECOVERY(3, printk("Wake up parent of"
1626 " scsi_eh_%d\n",shost->host_no));
1627
1628 complete(shost->eh_notify);
1629
1630 while (1) {
1631 /*
1632 * If we get a signal, it means we are supposed to go
1633 * away and die. This typically happens if the user is
1634 * trying to unload a module.
1635 */
1636 SCSI_LOG_ERROR_RECOVERY(1, printk("Error handler"
1637 " scsi_eh_%d"
1638 " sleeping\n",shost->host_no));
1639
1640 /*
1641 * Note - we always use down_interruptible with the semaphore
1642 * even if the module was loaded as part of the kernel. The
1643 * reason is that down() will cause this thread to be counted
1644 * in the load average as a running process, and down
1645 * interruptible doesn't. Given that we need to allow this
1646 * thread to die if the driver was loaded as a module, using
1647 * semaphores isn't unreasonable.
1648 */
1649 down_interruptible(&sem);
1650 if (shost->eh_kill)
1651 break;
1652
1653 SCSI_LOG_ERROR_RECOVERY(1, printk("Error handler"
1654 " scsi_eh_%d waking"
1655 " up\n",shost->host_no));
1656
1657 shost->eh_active = 1;
1658
1659 /*
1660 * We have a host that is failing for some reason. Figure out
1661 * what we need to do to get it up and online again (if we can).
1662 * If we fail, we end up taking the thing offline.
1663 */
1664 if (shost->hostt->eh_strategy_handler)
1665 rtn = shost->hostt->eh_strategy_handler(shost);
1666 else
1667 scsi_unjam_host(shost);
1668
1669 shost->eh_active = 0;
1670
1671 /*
1672 * Note - if the above fails completely, the action is to take
1673 * individual devices offline and flush the queue of any
1674 * outstanding requests that may have been pending. When we
1675 * restart, we restart any I/O to any other devices on the bus
1676 * which are still online.
1677 */
1678 scsi_restart_operations(shost);
1679
1680 }
1681
1682 SCSI_LOG_ERROR_RECOVERY(1, printk("Error handler scsi_eh_%d"
1683 " exiting\n",shost->host_no));
1684
1685 /*
1686 * Make sure that nobody tries to wake us up again.
1687 */
1688 shost->eh_wait = NULL;
1689
1690 /*
1691 * Knock this down too. From this point on, the host is flying
1692 * without a pilot. If this is because the module is being unloaded,
1693 * that's fine. If the user sent a signal to this thing, we are
1694 * potentially in real danger.
1695 */
1696 shost->eh_active = 0;
1697 shost->ehandler = NULL;
1698
1699 /*
1700 * If anyone is waiting for us to exit (i.e. someone trying to unload
1701 * a driver), then wake up that process to let them know we are on
1702 * the way out the door.
1703 */
1704 complete_and_exit(shost->eh_notify, 0);
1705 return 0;
1706 }
1707
1708 /*
1709 * Function: scsi_report_bus_reset()
1710 *
1711 * Purpose: Utility function used by low-level drivers to report that
1712 * they have observed a bus reset on the bus being handled.
1713 *
1714 * Arguments: shost - Host in question
1715 * channel - channel on which reset was observed.
1716 *
1717 * Returns: Nothing
1718 *
1719 * Lock status: Host lock must be held.
1720 *
1721 * Notes: This only needs to be called if the reset is one which
1722 * originates from an unknown location. Resets originated
1723 * by the mid-level itself don't need to call this, but there
1724 * should be no harm.
1725 *
1726 * The main purpose of this is to make sure that a CHECK_CONDITION
1727 * is properly treated.
1728 */
1729 void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
1730 {
1731 struct scsi_device *sdev;
1732
1733 __shost_for_each_device(sdev, shost) {
1734 if (channel == sdev->channel) {
1735 sdev->was_reset = 1;
1736 sdev->expecting_cc_ua = 1;
1737 }
1738 }
1739 }
1740 EXPORT_SYMBOL(scsi_report_bus_reset);
1741
1742 /*
1743 * Function: scsi_report_device_reset()
1744 *
1745 * Purpose: Utility function used by low-level drivers to report that
1746 * they have observed a device reset on the device being handled.
1747 *
1748 * Arguments: shost - Host in question
1749 * channel - channel on which reset was observed
1750 * target - target on which reset was observed
1751 *
1752 * Returns: Nothing
1753 *
1754 * Lock status: Host lock must be held
1755 *
1756 * Notes: This only needs to be called if the reset is one which
1757 * originates from an unknown location. Resets originated
1758 * by the mid-level itself don't need to call this, but there
1759 * should be no harm.
1760 *
1761 * The main purpose of this is to make sure that a CHECK_CONDITION
1762 * is properly treated.
1763 */
1764 void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
1765 {
1766 struct scsi_device *sdev;
1767
1768 __shost_for_each_device(sdev, shost) {
1769 if (channel == sdev->channel &&
1770 target == sdev->id) {
1771 sdev->was_reset = 1;
1772 sdev->expecting_cc_ua = 1;
1773 }
1774 }
1775 }
1776 EXPORT_SYMBOL(scsi_report_device_reset);
1777
1778 static void
1779 scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
1780 {
1781 }
1782
1783 /*
1784 * Function: scsi_reset_provider
1785 *
1786 * Purpose: Send requested reset to a bus or device at any phase.
1787 *
1788 * Arguments: device - device to send reset to
1789 * flag - reset type (see scsi.h)
1790 *
1791 * Returns: SUCCESS/FAILURE.
1792 *
1793 * Notes: This is used by the SCSI Generic driver to provide
1794 * Bus/Device reset capability.
1795 */
1796 int
1797 scsi_reset_provider(struct scsi_device *dev, int flag)
1798 {
1799 struct scsi_cmnd *scmd = scsi_get_command(dev, GFP_KERNEL);
1800 struct request req;
1801 int rtn;
1802
1803 scmd->request = &req;
1804 memset(&scmd->eh_timeout, 0, sizeof(scmd->eh_timeout));
1805 scmd->request->rq_status = RQ_SCSI_BUSY;
1806 scmd->state = SCSI_STATE_INITIALIZING;
1807
1808 memset(&scmd->cmnd, '\0', sizeof(scmd->cmnd));
1809
1810 scmd->scsi_done = scsi_reset_provider_done_command;
1811 scmd->done = NULL;
1812 scmd->buffer = NULL;
1813 scmd->bufflen = 0;
1814 scmd->request_buffer = NULL;
1815 scmd->request_bufflen = 0;
1816
1817 scmd->cmd_len = 0;
1818
1819 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
1820 scmd->sc_request = NULL;
1821 scmd->sc_magic = SCSI_CMND_MAGIC;
1822
1823 init_timer(&scmd->eh_timeout);
1824
1825 /*
1826 * Sometimes the command can get back into the timer chain,
1827 * so use the pid as an identifier.
1828 */
1829 scmd->pid = 0;
1830
1831 switch (flag) {
1832 case SCSI_TRY_RESET_DEVICE:
1833 rtn = scsi_try_bus_device_reset(scmd);
1834 if (rtn == SUCCESS)
1835 break;
1836 /* FALLTHROUGH */
1837 case SCSI_TRY_RESET_BUS:
1838 rtn = scsi_try_bus_reset(scmd);
1839 if (rtn == SUCCESS)
1840 break;
1841 /* FALLTHROUGH */
1842 case SCSI_TRY_RESET_HOST:
1843 rtn = scsi_try_host_reset(scmd);
1844 break;
1845 default:
1846 rtn = FAILED;
1847 }
1848
1849 scsi_next_command(scmd);
1850 return rtn;
1851 }
1852 EXPORT_SYMBOL(scsi_reset_provider);
1853
1854 /**
1855 * scsi_normalize_sense - normalize main elements from either fixed or
1856 * descriptor sense data format into a common format.
1857 *
1858 * @sense_buffer: byte array containing sense data returned by device
1859 * @sb_len: number of valid bytes in sense_buffer
1860 * @sshdr: pointer to instance of structure that common
1861 * elements are written to.
1862 *
1863 * Notes:
1864 * The "main elements" from sense data are: response_code, sense_key,
1865 * asc, ascq and additional_length (only for descriptor format).
1866 *
1867 * Typically this function can be called after a device has
1868 * responded to a SCSI command with the CHECK_CONDITION status.
1869 *
1870 * Return value:
1871 * 1 if valid sense data information found, else 0;
1872 **/
1873 int scsi_normalize_sense(const u8 *sense_buffer, int sb_len,
1874 struct scsi_sense_hdr *sshdr)
1875 {
1876 if (!sense_buffer || !sb_len || (sense_buffer[0] & 0x70) != 0x70)
1877 return 0;
1878
1879 memset(sshdr, 0, sizeof(struct scsi_sense_hdr));
1880
1881 sshdr->response_code = (sense_buffer[0] & 0x7f);
1882 if (sshdr->response_code >= 0x72) {
1883 /*
1884 * descriptor format
1885 */
1886 if (sb_len > 1)
1887 sshdr->sense_key = (sense_buffer[1] & 0xf);
1888 if (sb_len > 2)
1889 sshdr->asc = sense_buffer[2];
1890 if (sb_len > 3)
1891 sshdr->ascq = sense_buffer[3];
1892 if (sb_len > 7)
1893 sshdr->additional_length = sense_buffer[7];
1894 } else {
1895 /*
1896 * fixed format
1897 */
1898 if (sb_len > 2)
1899 sshdr->sense_key = (sense_buffer[2] & 0xf);
1900 if (sb_len > 7) {
1901 sb_len = (sb_len < (sense_buffer[7] + 8)) ?
1902 sb_len : (sense_buffer[7] + 8);
1903 if (sb_len > 12)
1904 sshdr->asc = sense_buffer[12];
1905 if (sb_len > 13)
1906 sshdr->ascq = sense_buffer[13];
1907 }
1908 }
1909
1910 return 1;
1911 }
1912 EXPORT_SYMBOL(scsi_normalize_sense);
1913
1914 int scsi_request_normalize_sense(struct scsi_request *sreq,
1915 struct scsi_sense_hdr *sshdr)
1916 {
1917 return scsi_normalize_sense(sreq->sr_sense_buffer,
1918 sizeof(sreq->sr_sense_buffer), sshdr);
1919 }
1920 EXPORT_SYMBOL(scsi_request_normalize_sense);
1921
1922 int scsi_command_normalize_sense(struct scsi_cmnd *cmd,
1923 struct scsi_sense_hdr *sshdr)
1924 {
1925 return scsi_normalize_sense(cmd->sense_buffer,
1926 sizeof(cmd->sense_buffer), sshdr);
1927 }
1928 EXPORT_SYMBOL(scsi_command_normalize_sense);
1929
1930 /**
1931 * scsi_sense_desc_find - search for a given descriptor type in
1932 * descriptor sense data format.
1933 *
1934 * @sense_buffer: byte array of descriptor format sense data
1935 * @sb_len: number of valid bytes in sense_buffer
1936 * @desc_type: value of descriptor type to find
1937 * (e.g. 0 -> information)
1938 *
1939 * Notes:
1940 * only valid when sense data is in descriptor format
1941 *
1942 * Return value:
1943 * pointer to start of (first) descriptor if found else NULL
1944 **/
1945 const u8 * scsi_sense_desc_find(const u8 * sense_buffer, int sb_len,
1946 int desc_type)
1947 {
1948 int add_sen_len, add_len, desc_len, k;
1949 const u8 * descp;
1950
1951 if ((sb_len < 8) || (0 == (add_sen_len = sense_buffer[7])))
1952 return NULL;
1953 if ((sense_buffer[0] < 0x72) || (sense_buffer[0] > 0x73))
1954 return NULL;
1955 add_sen_len = (add_sen_len < (sb_len - 8)) ?
1956 add_sen_len : (sb_len - 8);
1957 descp = &sense_buffer[8];
1958 for (desc_len = 0, k = 0; k < add_sen_len; k += desc_len) {
1959 descp += desc_len;
1960 add_len = (k < (add_sen_len - 1)) ? descp[1]: -1;
1961 desc_len = add_len + 2;
1962 if (descp[0] == desc_type)
1963 return descp;
1964 if (add_len < 0) // short descriptor ??
1965 break;
1966 }
1967 return NULL;
1968 }
1969 EXPORT_SYMBOL(scsi_sense_desc_find);
1970
1971 /**
1972 * scsi_get_sense_info_fld - attempts to get information field from
1973 * sense data (either fixed or descriptor format)
1974 *
1975 * @sense_buffer: byte array of sense data
1976 * @sb_len: number of valid bytes in sense_buffer
1977 * @info_out: pointer to 64 integer where 8 or 4 byte information
1978 * field will be placed if found.
1979 *
1980 * Return value:
1981 * 1 if information field found, 0 if not found.
1982 **/
1983 int scsi_get_sense_info_fld(const u8 * sense_buffer, int sb_len,
1984 u64 * info_out)
1985 {
1986 int j;
1987 const u8 * ucp;
1988 u64 ull;
1989
1990 if (sb_len < 7)
1991 return 0;
1992 switch (sense_buffer[0] & 0x7f) {
1993 case 0x70:
1994 case 0x71:
1995 if (sense_buffer[0] & 0x80) {
1996 *info_out = (sense_buffer[3] << 24) +
1997 (sense_buffer[4] << 16) +
1998 (sense_buffer[5] << 8) + sense_buffer[6];
1999 return 1;
2000 } else
2001 return 0;
2002 case 0x72:
2003 case 0x73:
2004 ucp = scsi_sense_desc_find(sense_buffer, sb_len,
2005 0 /* info desc */);
2006 if (ucp && (0xa == ucp[1])) {
2007 ull = 0;
2008 for (j = 0; j < 8; ++j) {
2009 if (j > 0)
2010 ull <<= 8;
2011 ull |= ucp[4 + j];
2012 }
2013 *info_out = ull;
2014 return 1;
2015 } else
2016 return 0;
2017 default:
2018 return 0;
2019 }
2020 }
2021 EXPORT_SYMBOL(scsi_get_sense_info_fld);
This page took 0.071778 seconds and 4 git commands to generate.