Import 2.3.18pre1
[davej-history.git] / drivers / scsi / scsi_obsolete.c
blobb431c3849197d1f5e5cb2e9e0a824e96a21b9258
1 /*
2 * scsi.c Copyright (C) 1992 Drew Eckhardt
3 * Copyright (C) 1993, 1994, 1995 Eric Youngdale
5 * generic mid-level SCSI driver
6 * Initial versions: Drew Eckhardt
7 * Subsequent revisions: Eric Youngdale
9 * <drew@colorado.edu>
11 * Bug correction thanks go to :
12 * Rik Faith <faith@cs.unc.edu>
13 * Tommy Thorn <tthorn>
14 * Thomas Wuensche <tw@fgb1.fgb.mw.tu-muenchen.de>
16 * Modified by Eric Youngdale eric@aib.com to
17 * add scatter-gather, multiple outstanding request, and other
18 * enhancements.
20 * Native multichannel, wide scsi, /proc/scsi and hot plugging
21 * support added by Michael Neuffer <mike@i-connect.net>
23 * Major improvements to the timeout, abort, and reset processing,
24 * as well as performance modifications for large queue depths by
25 * Leonard N. Zubkoff <lnz@dandelion.com>
27 * Improved compatibility with 2.0 behaviour by Manfred Spraul
28 * <masp0008@stud.uni-sb.de>
32 *#########################################################################
33 *#########################################################################
34 *#########################################################################
35 *#########################################################################
36 * NOTE - NOTE - NOTE - NOTE - NOTE - NOTE - NOTE
38 *#########################################################################
39 *#########################################################################
40 *#########################################################################
41 *#########################################################################
43 * This file contains the 'old' scsi error handling. It is only present
44 * while the new error handling code is being debugged, and while the low
45 * level drivers are being converted to use the new code. Once the last
46 * driver uses the new code this *ENTIRE* file will be nuked.
49 #define __NO_VERSION__
50 #include <linux/module.h>
52 #include <linux/sched.h>
53 #include <linux/timer.h>
54 #include <linux/string.h>
55 #include <linux/malloc.h>
56 #include <linux/ioport.h>
57 #include <linux/kernel.h>
58 #include <linux/stat.h>
59 #include <linux/blk.h>
60 #include <linux/interrupt.h>
61 #include <linux/delay.h>
63 #include <asm/system.h>
64 #include <asm/irq.h>
65 #include <asm/dma.h>
67 #include "scsi.h"
68 #include "hosts.h"
69 #include "constants.h"
71 #undef USE_STATIC_SCSI_MEMORY
74 static const char RCSid[] = "$Header: /mnt/ide/home/eric/CVSROOT/linux/drivers/scsi/scsi_obsolete.c,v 1.1 1997/05/18 23:27:21 eric Exp $";
78 #define INTERNAL_ERROR (panic ("Internal error in file %s, line %d.\n", __FILE__, __LINE__))
81 static int scsi_abort(Scsi_Cmnd *, int code);
82 static int scsi_reset(Scsi_Cmnd *, unsigned int);
84 extern void scsi_old_done(Scsi_Cmnd * SCpnt);
85 int update_timeout(Scsi_Cmnd *, int);
86 extern void scsi_old_times_out(Scsi_Cmnd * SCpnt);
87 extern void internal_cmnd(Scsi_Cmnd * SCpnt);
89 extern volatile struct Scsi_Host *host_active;
90 #define SCSI_BLOCK(HOST) ((HOST->block && host_active && HOST != host_active) \
91 || (HOST->can_queue && HOST->host_busy >= HOST->can_queue))
93 static unsigned char generic_sense[6] = {REQUEST_SENSE, 0, 0, 0, 255, 0};
96 * This is the number of clock ticks we should wait before we time out
97 * and abort the command. This is for where the scsi.c module generates
98 * the command, not where it originates from a higher level, in which
99 * case the timeout is specified there.
101 * ABORT_TIMEOUT and RESET_TIMEOUT are the timeouts for RESET and ABORT
102 * respectively.
105 #ifdef DEBUG_TIMEOUT
106 static void scsi_dump_status(void);
107 #endif
110 #ifdef DEBUG
111 #define SCSI_TIMEOUT (5*HZ)
112 #else
113 #define SCSI_TIMEOUT (2*HZ)
114 #endif
116 #ifdef DEBUG
117 #define SENSE_TIMEOUT SCSI_TIMEOUT
118 #define ABORT_TIMEOUT SCSI_TIMEOUT
119 #define RESET_TIMEOUT SCSI_TIMEOUT
120 #else
121 #define SENSE_TIMEOUT (5*HZ/10)
122 #define RESET_TIMEOUT (5*HZ/10)
123 #define ABORT_TIMEOUT (5*HZ/10)
124 #endif
127 /* Do not call reset on error if we just did a reset within 15 sec. */
128 #define MIN_RESET_PERIOD (15*HZ)
133 * Flag bits for the internal_timeout array
135 #define NORMAL_TIMEOUT 0
136 #define IN_ABORT 1
137 #define IN_RESET 2
138 #define IN_RESET2 4
139 #define IN_RESET3 8
142 * This is our time out function, called when the timer expires for a
143 * given host adapter. It will attempt to abort the currently executing
144 * command, that failing perform a kernel panic.
147 void scsi_old_times_out(Scsi_Cmnd * SCpnt)
149 unsigned long flags;
151 spin_lock_irqsave(&io_request_lock, flags);
153 /* Set the serial_number_at_timeout to the current serial_number */
154 SCpnt->serial_number_at_timeout = SCpnt->serial_number;
156 switch (SCpnt->internal_timeout & (IN_ABORT | IN_RESET | IN_RESET2 | IN_RESET3)) {
157 case NORMAL_TIMEOUT:
159 #ifdef DEBUG_TIMEOUT
160 scsi_dump_status();
161 #endif
164 if (!scsi_abort(SCpnt, DID_TIME_OUT))
165 break;
166 case IN_ABORT:
167 printk("SCSI host %d abort (pid %ld) timed out - resetting\n",
168 SCpnt->host->host_no, SCpnt->pid);
169 if (!scsi_reset(SCpnt, SCSI_RESET_ASYNCHRONOUS))
170 break;
171 case IN_RESET:
172 case (IN_ABORT | IN_RESET):
173 /* This might be controversial, but if there is a bus hang,
174 * you might conceivably want the machine up and running
175 * esp if you have an ide disk.
177 printk("SCSI host %d channel %d reset (pid %ld) timed out - "
178 "trying harder\n",
179 SCpnt->host->host_no, SCpnt->channel, SCpnt->pid);
180 SCpnt->internal_timeout &= ~IN_RESET;
181 SCpnt->internal_timeout |= IN_RESET2;
182 scsi_reset(SCpnt,
183 SCSI_RESET_ASYNCHRONOUS | SCSI_RESET_SUGGEST_BUS_RESET);
184 break;
185 case IN_RESET2:
186 case (IN_ABORT | IN_RESET2):
187 /* Obviously the bus reset didn't work.
188 * Let's try even harder and call for an HBA reset.
189 * Maybe the HBA itself crashed and this will shake it loose.
191 printk("SCSI host %d reset (pid %ld) timed out - trying to shake it loose\n",
192 SCpnt->host->host_no, SCpnt->pid);
193 SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2);
194 SCpnt->internal_timeout |= IN_RESET3;
195 scsi_reset(SCpnt,
196 SCSI_RESET_ASYNCHRONOUS | SCSI_RESET_SUGGEST_HOST_RESET);
197 break;
199 default:
200 printk("SCSI host %d reset (pid %ld) timed out again -\n",
201 SCpnt->host->host_no, SCpnt->pid);
202 printk("probably an unrecoverable SCSI bus or device hang.\n");
203 break;
206 spin_unlock_irqrestore(&io_request_lock, flags);
211 * From what I can find in scsi_obsolete.c, this function is only called
212 * by scsi_old_done and scsi_reset. Both of these functions run with the
213 * io_request_lock already held, so we need do nothing here about grabbing
214 * any locks.
216 static void scsi_request_sense(Scsi_Cmnd * SCpnt)
218 SCpnt->flags |= WAS_SENSE | ASKED_FOR_SENSE;
219 update_timeout(SCpnt, SENSE_TIMEOUT);
222 memcpy((void *) SCpnt->cmnd, (void *) generic_sense,
223 sizeof(generic_sense));
224 memset((void *) SCpnt->sense_buffer, 0,
225 sizeof(SCpnt->sense_buffer));
227 SCpnt->cmnd[1] = SCpnt->lun << 5;
228 SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);
230 SCpnt->request_buffer = &SCpnt->sense_buffer;
231 SCpnt->request_bufflen = sizeof(SCpnt->sense_buffer);
232 SCpnt->use_sg = 0;
233 SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
234 SCpnt->result = 0;
235 internal_cmnd(SCpnt);
241 static int check_sense(Scsi_Cmnd * SCpnt)
243 /* If there is no sense information, request it. If we have already
244 * requested it, there is no point in asking again - the firmware must
245 * be confused.
247 if (((SCpnt->sense_buffer[0] & 0x70) >> 4) != 7) {
248 if (!(SCpnt->flags & ASKED_FOR_SENSE))
249 return SUGGEST_SENSE;
250 else
251 return SUGGEST_RETRY;
253 SCpnt->flags &= ~ASKED_FOR_SENSE;
255 #ifdef DEBUG_INIT
256 printk("scsi%d, channel%d : ", SCpnt->host->host_no, SCpnt->channel);
257 print_sense("", SCpnt);
258 printk("\n");
259 #endif
260 if (SCpnt->sense_buffer[2] & 0xe0)
261 return SUGGEST_ABORT;
263 switch (SCpnt->sense_buffer[2] & 0xf) {
264 case NO_SENSE:
265 return 0;
266 case RECOVERED_ERROR:
267 return SUGGEST_IS_OK;
269 case ABORTED_COMMAND:
270 return SUGGEST_RETRY;
271 case NOT_READY:
272 case UNIT_ATTENTION:
274 * If we are expecting a CC/UA because of a bus reset that we
275 * performed, treat this just as a retry. Otherwise this is
276 * information that we should pass up to the upper-level driver
277 * so that we can deal with it there.
279 if (SCpnt->device->expecting_cc_ua) {
280 SCpnt->device->expecting_cc_ua = 0;
281 return SUGGEST_RETRY;
283 return SUGGEST_ABORT;
285 /* these three are not supported */
286 case COPY_ABORTED:
287 case VOLUME_OVERFLOW:
288 case MISCOMPARE:
290 case MEDIUM_ERROR:
291 return SUGGEST_REMAP;
292 case BLANK_CHECK:
293 case DATA_PROTECT:
294 case HARDWARE_ERROR:
295 case ILLEGAL_REQUEST:
296 default:
297 return SUGGEST_ABORT;
301 /* This function is the mid-level interrupt routine, which decides how
302 * to handle error conditions. Each invocation of this function must
303 * do one and *only* one of the following:
305 * (1) Call last_cmnd[host].done. This is done for fatal errors and
306 * normal completion, and indicates that the handling for this
307 * request is complete.
308 * (2) Call internal_cmnd to requeue the command. This will result in
309 * scsi_done being called again when the retry is complete.
310 * (3) Call scsi_request_sense. This asks the host adapter/drive for
311 * more information about the error condition. When the information
312 * is available, scsi_done will be called again.
313 * (4) Call reset(). This is sort of a last resort, and the idea is that
314 * this may kick things loose and get the drive working again. reset()
315 * automatically calls scsi_request_sense, and thus scsi_done will be
316 * called again once the reset is complete.
318 * If none of the above actions are taken, the drive in question
319 * will hang. If more than one of the above actions are taken by
320 * scsi_done, then unpredictable behavior will result.
322 void scsi_old_done(Scsi_Cmnd * SCpnt)
324 int status = 0;
325 int exit = 0;
326 int checked;
327 int oldto;
328 struct Scsi_Host *host = SCpnt->host;
329 int result = SCpnt->result;
330 SCpnt->serial_number = 0;
331 SCpnt->serial_number_at_timeout = 0;
332 oldto = update_timeout(SCpnt, 0);
334 #ifdef DEBUG_TIMEOUT
335 if (result)
336 printk("Non-zero result in scsi_done %x %d:%d\n",
337 result, SCpnt->target, SCpnt->lun);
338 #endif
340 /* If we requested an abort, (and we got it) then fix up the return
341 * status to say why
343 if (host_byte(result) == DID_ABORT && SCpnt->abort_reason)
344 SCpnt->result = result = (result & 0xff00ffff) |
345 (SCpnt->abort_reason << 16);
348 #define CMD_FINISHED 0
349 #define MAYREDO 1
350 #define REDO 3
351 #define PENDING 4
353 #ifdef DEBUG
354 printk("In scsi_done(host = %d, result = %06x)\n", host->host_no, result);
355 #endif
357 if (SCpnt->flags & SYNC_RESET) {
359 * The behaviou of scsi_reset(SYNC) was changed in 2.1.? .
360 * The scsi mid-layer does a REDO after every sync reset, the driver
361 * must not do that any more. In order to prevent old drivers from
362 * crashing, all scsi_done() calls during sync resets are ignored.
364 printk("scsi%d: device driver called scsi_done() "
365 "for a syncronous reset.\n", SCpnt->host->host_no);
366 return;
368 if (SCpnt->flags & WAS_SENSE) {
369 SCpnt->use_sg = SCpnt->old_use_sg;
370 SCpnt->cmd_len = SCpnt->old_cmd_len;
372 switch (host_byte(result)) {
373 case DID_OK:
374 if (status_byte(result) && (SCpnt->flags & WAS_SENSE))
375 /* Failed to obtain sense information */
377 SCpnt->flags &= ~WAS_SENSE;
378 #if 0 /* This cannot possibly be correct. */
379 SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
380 #endif
382 if (!(SCpnt->flags & WAS_RESET)) {
383 printk("scsi%d : channel %d target %d lun %d request sense"
384 " failed, performing reset.\n",
385 SCpnt->host->host_no, SCpnt->channel, SCpnt->target,
386 SCpnt->lun);
387 scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
388 status = REDO;
389 break;
390 } else {
391 exit = (DRIVER_HARD | SUGGEST_ABORT);
392 status = CMD_FINISHED;
394 } else
395 switch (msg_byte(result)) {
396 case COMMAND_COMPLETE:
397 switch (status_byte(result)) {
398 case GOOD:
399 if (SCpnt->flags & WAS_SENSE) {
400 #ifdef DEBUG
401 printk("In scsi_done, GOOD status, COMMAND COMPLETE, "
402 "parsing sense information.\n");
403 #endif
404 SCpnt->flags &= ~WAS_SENSE;
405 #if 0 /* This cannot possibly be correct. */
406 SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
407 #endif
409 switch (checked = check_sense(SCpnt)) {
410 case SUGGEST_SENSE:
411 case 0:
412 #ifdef DEBUG
413 printk("NO SENSE. status = REDO\n");
414 #endif
415 update_timeout(SCpnt, oldto);
416 status = REDO;
417 break;
418 case SUGGEST_IS_OK:
419 break;
420 case SUGGEST_REMAP:
421 #ifdef DEBUG
422 printk("SENSE SUGGEST REMAP - status = CMD_FINISHED\n");
423 #endif
424 status = CMD_FINISHED;
425 exit = DRIVER_SENSE | SUGGEST_ABORT;
426 break;
427 case SUGGEST_RETRY:
428 #ifdef DEBUG
429 printk("SENSE SUGGEST RETRY - status = MAYREDO\n");
430 #endif
431 status = MAYREDO;
432 exit = DRIVER_SENSE | SUGGEST_RETRY;
433 break;
434 case SUGGEST_ABORT:
435 #ifdef DEBUG
436 printk("SENSE SUGGEST ABORT - status = CMD_FINISHED");
437 #endif
438 status = CMD_FINISHED;
439 exit = DRIVER_SENSE | SUGGEST_ABORT;
440 break;
441 default:
442 printk("Internal error %s %d \n", __FILE__,
443 __LINE__);
446 /* end WAS_SENSE */
447 else {
448 #ifdef DEBUG
449 printk("COMMAND COMPLETE message returned, "
450 "status = CMD_FINISHED. \n");
451 #endif
452 exit = DRIVER_OK;
453 status = CMD_FINISHED;
455 break;
457 case CHECK_CONDITION:
458 case COMMAND_TERMINATED:
459 switch (check_sense(SCpnt)) {
460 case 0:
461 update_timeout(SCpnt, oldto);
462 status = REDO;
463 break;
464 case SUGGEST_REMAP:
465 status = CMD_FINISHED;
466 exit = DRIVER_SENSE | SUGGEST_ABORT;
467 break;
468 case SUGGEST_RETRY:
469 status = MAYREDO;
470 exit = DRIVER_SENSE | SUGGEST_RETRY;
471 break;
472 case SUGGEST_ABORT:
473 status = CMD_FINISHED;
474 exit = DRIVER_SENSE | SUGGEST_ABORT;
475 break;
476 case SUGGEST_SENSE:
477 scsi_request_sense(SCpnt);
478 status = PENDING;
479 break;
481 break;
483 case CONDITION_GOOD:
484 case INTERMEDIATE_GOOD:
485 case INTERMEDIATE_C_GOOD:
486 break;
488 case BUSY:
489 case QUEUE_FULL:
490 update_timeout(SCpnt, oldto);
491 status = REDO;
492 break;
494 case RESERVATION_CONFLICT:
495 printk("scsi%d, channel %d : RESERVATION CONFLICT performing"
496 " reset.\n", SCpnt->host->host_no, SCpnt->channel);
497 scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
498 status = REDO;
499 break;
500 default:
501 printk("Internal error %s %d \n"
502 "status byte = %d \n", __FILE__,
503 __LINE__, status_byte(result));
506 break;
507 default:
508 panic("scsi: unsupported message byte %d received\n",
509 msg_byte(result));
511 break;
512 case DID_TIME_OUT:
513 #ifdef DEBUG
514 printk("Host returned DID_TIME_OUT - ");
515 #endif
517 if (SCpnt->flags & WAS_TIMEDOUT) {
518 #ifdef DEBUG
519 printk("Aborting\n");
520 #endif
522 Allow TEST_UNIT_READY and INQUIRY commands to timeout early
523 without causing resets. All other commands should be retried.
525 if (SCpnt->cmnd[0] != TEST_UNIT_READY &&
526 SCpnt->cmnd[0] != INQUIRY)
527 status = MAYREDO;
528 exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
529 } else {
530 #ifdef DEBUG
531 printk("Retrying.\n");
532 #endif
533 SCpnt->flags |= WAS_TIMEDOUT;
534 SCpnt->internal_timeout &= ~IN_ABORT;
535 status = REDO;
537 break;
538 case DID_BUS_BUSY:
539 case DID_PARITY:
540 status = REDO;
541 break;
542 case DID_NO_CONNECT:
543 #ifdef DEBUG
544 printk("Couldn't connect.\n");
545 #endif
546 exit = (DRIVER_HARD | SUGGEST_ABORT);
547 break;
548 case DID_ERROR:
549 status = MAYREDO;
550 exit = (DRIVER_HARD | SUGGEST_ABORT);
551 break;
552 case DID_BAD_TARGET:
553 case DID_ABORT:
554 exit = (DRIVER_INVALID | SUGGEST_ABORT);
555 break;
556 case DID_RESET:
557 if (SCpnt->flags & IS_RESETTING) {
558 SCpnt->flags &= ~IS_RESETTING;
559 status = REDO;
560 break;
562 if (msg_byte(result) == GOOD &&
563 status_byte(result) == CHECK_CONDITION) {
564 switch (check_sense(SCpnt)) {
565 case 0:
566 update_timeout(SCpnt, oldto);
567 status = REDO;
568 break;
569 case SUGGEST_REMAP:
570 case SUGGEST_RETRY:
571 status = MAYREDO;
572 exit = DRIVER_SENSE | SUGGEST_RETRY;
573 break;
574 case SUGGEST_ABORT:
575 status = CMD_FINISHED;
576 exit = DRIVER_SENSE | SUGGEST_ABORT;
577 break;
578 case SUGGEST_SENSE:
579 scsi_request_sense(SCpnt);
580 status = PENDING;
581 break;
583 } else {
584 status = REDO;
585 exit = SUGGEST_RETRY;
587 break;
588 default:
589 exit = (DRIVER_ERROR | SUGGEST_DIE);
592 switch (status) {
593 case CMD_FINISHED:
594 case PENDING:
595 break;
596 case MAYREDO:
597 #ifdef DEBUG
598 printk("In MAYREDO, allowing %d retries, have %d\n",
599 SCpnt->allowed, SCpnt->retries);
600 #endif
601 if ((++SCpnt->retries) < SCpnt->allowed) {
602 if ((SCpnt->retries >= (SCpnt->allowed >> 1))
603 && !(SCpnt->host->resetting && time_before(jiffies, SCpnt->host->last_reset + MIN_RESET_PERIOD))
604 && !(SCpnt->flags & WAS_RESET)) {
605 printk("scsi%d channel %d : resetting for second half of retries.\n",
606 SCpnt->host->host_no, SCpnt->channel);
607 scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
608 /* fall through to REDO */
610 } else {
611 status = CMD_FINISHED;
612 break;
614 /* fall through to REDO */
616 case REDO:
618 if (SCpnt->flags & WAS_SENSE)
619 scsi_request_sense(SCpnt);
620 else {
621 memcpy((void *) SCpnt->cmnd,
622 (void *) SCpnt->data_cmnd,
623 sizeof(SCpnt->data_cmnd));
624 memset((void *) SCpnt->sense_buffer, 0,
625 sizeof(SCpnt->sense_buffer));
626 SCpnt->request_buffer = SCpnt->buffer;
627 SCpnt->request_bufflen = SCpnt->bufflen;
628 SCpnt->use_sg = SCpnt->old_use_sg;
629 SCpnt->cmd_len = SCpnt->old_cmd_len;
630 SCpnt->result = 0;
631 internal_cmnd(SCpnt);
633 break;
634 default:
635 INTERNAL_ERROR;
638 if (status == CMD_FINISHED) {
639 #ifdef DEBUG
640 printk("Calling done function - at address %p\n", SCpnt->done);
641 #endif
642 host->host_busy--; /* Indicate that we are free */
644 if (host->block && host->host_busy == 0) {
645 host_active = NULL;
647 /* For block devices "wake_up" is done in end_scsi_request */
648 if (!SCSI_BLK_MAJOR(MAJOR(SCpnt->request.rq_dev))) {
649 struct Scsi_Host *next;
651 for (next = host->block; next != host; next = next->block)
652 wake_up(&next->host_wait);
655 wake_up(&host->host_wait);
656 SCpnt->result = result | ((exit & 0xff) << 24);
657 SCpnt->use_sg = SCpnt->old_use_sg;
658 SCpnt->cmd_len = SCpnt->old_cmd_len;
659 SCpnt->done(SCpnt);
661 #undef CMD_FINISHED
662 #undef REDO
663 #undef MAYREDO
664 #undef PENDING
668 * The scsi_abort function interfaces with the abort() function of the host
669 * we are aborting, and causes the current command to not complete. The
670 * caller should deal with any error messages or status returned on the
671 * next call.
673 * This will not be called reentrantly for a given host.
677 * Since we're nice guys and specified that abort() and reset()
678 * can be non-reentrant. The internal_timeout flags are used for
679 * this.
683 static int scsi_abort(Scsi_Cmnd * SCpnt, int why)
685 int oldto;
686 struct Scsi_Host *host = SCpnt->host;
688 while (1) {
691 * Protect against races here. If the command is done, or we are
692 * on a different command forget it.
694 if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
695 return 0;
697 if (SCpnt->internal_timeout & IN_ABORT) {
698 spin_unlock_irq(&io_request_lock);
699 while (SCpnt->internal_timeout & IN_ABORT)
700 barrier();
701 spin_lock_irq(&io_request_lock);
702 } else {
703 SCpnt->internal_timeout |= IN_ABORT;
704 oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
706 if ((SCpnt->flags & IS_RESETTING) && SCpnt->device->soft_reset) {
707 /* OK, this command must have died when we did the
708 * reset. The device itself must have lied.
710 printk("Stale command on %d %d:%d appears to have died when"
711 " the bus was reset\n",
712 SCpnt->channel, SCpnt->target, SCpnt->lun);
714 if (!host->host_busy) {
715 SCpnt->internal_timeout &= ~IN_ABORT;
716 update_timeout(SCpnt, oldto);
717 return 0;
719 printk("scsi : aborting command due to timeout : pid %lu, scsi%d,"
720 " channel %d, id %d, lun %d ",
721 SCpnt->pid, SCpnt->host->host_no, (int) SCpnt->channel,
722 (int) SCpnt->target, (int) SCpnt->lun);
723 print_command(SCpnt->cmnd);
724 if (SCpnt->serial_number != SCpnt->serial_number_at_timeout)
725 return 0;
726 SCpnt->abort_reason = why;
727 switch (host->hostt->abort(SCpnt)) {
728 /* We do not know how to abort. Try waiting another
729 * time increment and see if this helps. Set the
730 * WAS_TIMEDOUT flag set so we do not try this twice
732 case SCSI_ABORT_BUSY: /* Tough call - returning 1 from
733 * this is too severe
735 case SCSI_ABORT_SNOOZE:
736 if (why == DID_TIME_OUT) {
737 SCpnt->internal_timeout &= ~IN_ABORT;
738 if (SCpnt->flags & WAS_TIMEDOUT) {
739 return 1; /* Indicate we cannot handle this.
740 * We drop down into the reset handler
741 * and try again
743 } else {
744 SCpnt->flags |= WAS_TIMEDOUT;
745 oldto = SCpnt->timeout_per_command;
746 update_timeout(SCpnt, oldto);
749 return 0;
750 case SCSI_ABORT_PENDING:
751 if (why != DID_TIME_OUT) {
752 update_timeout(SCpnt, oldto);
754 return 0;
755 case SCSI_ABORT_SUCCESS:
756 /* We should have already aborted this one. No
757 * need to adjust timeout
759 SCpnt->internal_timeout &= ~IN_ABORT;
760 return 0;
761 case SCSI_ABORT_NOT_RUNNING:
762 SCpnt->internal_timeout &= ~IN_ABORT;
763 update_timeout(SCpnt, 0);
764 return 0;
765 case SCSI_ABORT_ERROR:
766 default:
767 SCpnt->internal_timeout &= ~IN_ABORT;
768 return 1;
775 /* Mark a single SCSI Device as having been reset. */
777 static inline void scsi_mark_device_reset(Scsi_Device * Device)
779 Device->was_reset = 1;
780 Device->expecting_cc_ua = 1;
784 /* Mark all SCSI Devices on a specific Host as having been reset. */
786 void scsi_mark_host_reset(struct Scsi_Host *Host)
788 Scsi_Cmnd *SCpnt;
789 Scsi_Device *SDpnt;
791 for (SDpnt = Host->host_queue; SDpnt; SDpnt = SDpnt->next) {
792 for (SCpnt = SDpnt->device_queue; SCpnt; SCpnt = SCpnt->next)
793 scsi_mark_device_reset(SCpnt->device);
798 /* Mark all SCSI Devices on a specific Host Bus as having been reset. */
800 static void scsi_mark_bus_reset(struct Scsi_Host *Host, int channel)
802 Scsi_Cmnd *SCpnt;
803 Scsi_Device *SDpnt;
805 for (SDpnt = Host->host_queue; SDpnt; SDpnt = SDpnt->next) {
806 for (SCpnt = SDpnt->device_queue; SCpnt; SCpnt = SCpnt->next)
807 if (SCpnt->channel == channel)
808 scsi_mark_device_reset(SCpnt->device);
813 static int scsi_reset(Scsi_Cmnd * SCpnt, unsigned int reset_flags)
815 int temp;
816 Scsi_Cmnd *SCpnt1;
817 Scsi_Device *SDpnt;
818 struct Scsi_Host *host = SCpnt->host;
820 printk("SCSI bus is being reset for host %d channel %d.\n",
821 host->host_no, SCpnt->channel);
823 #if 0
825 * First of all, we need to make a recommendation to the low-level
826 * driver as to whether a BUS_DEVICE_RESET should be performed,
827 * or whether we should do a full BUS_RESET. There is no simple
828 * algorithm here - we basically use a series of heuristics
829 * to determine what we should do.
831 SCpnt->host->suggest_bus_reset = FALSE;
834 * First see if all of the active devices on the bus have
835 * been jammed up so that we are attempting resets. If so,
836 * then suggest a bus reset. Forcing a bus reset could
837 * result in some race conditions, but no more than
838 * you would usually get with timeouts. We will cross
839 * that bridge when we come to it.
841 * This is actually a pretty bad idea, since a sequence of
842 * commands will often timeout together and this will cause a
843 * Bus Device Reset followed immediately by a SCSI Bus Reset.
844 * If all of the active devices really are jammed up, the
845 * Bus Device Reset will quickly timeout and scsi_times_out
846 * will follow up with a SCSI Bus Reset anyway.
848 SCpnt1 = host->host_queue;
849 while (SCpnt1) {
850 if (SCpnt1->request.rq_status != RQ_INACTIVE
851 && (SCpnt1->flags & (WAS_RESET | IS_RESETTING)) == 0)
852 break;
853 SCpnt1 = SCpnt1->next;
855 if (SCpnt1 == NULL) {
856 reset_flags |= SCSI_RESET_SUGGEST_BUS_RESET;
859 * If the code that called us is suggesting a hard reset, then
860 * definitely request it. This usually occurs because a
861 * BUS_DEVICE_RESET times out.
863 * Passing reset_flags along takes care of this automatically.
865 if (reset_flags & SCSI_RESET_SUGGEST_BUS_RESET) {
866 SCpnt->host->suggest_bus_reset = TRUE;
868 #endif
870 while (1) {
873 * Protect against races here. If the command is done, or we are
874 * on a different command forget it.
876 if (reset_flags & SCSI_RESET_ASYNCHRONOUS)
877 if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
878 return 0;
880 if (SCpnt->internal_timeout & IN_RESET) {
881 spin_unlock_irq(&io_request_lock);
882 while (SCpnt->internal_timeout & IN_RESET)
883 barrier();
884 spin_lock_irq(&io_request_lock);
885 } else {
886 SCpnt->internal_timeout |= IN_RESET;
887 update_timeout(SCpnt, RESET_TIMEOUT);
889 if (reset_flags & SCSI_RESET_SYNCHRONOUS)
890 SCpnt->flags |= SYNC_RESET;
891 if (host->host_busy) {
892 for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
893 SCpnt1 = SDpnt->device_queue;
894 while (SCpnt1) {
895 if (SCpnt1->request.rq_status != RQ_INACTIVE) {
896 #if 0
897 if (!(SCpnt1->flags & IS_RESETTING) &&
898 !(SCpnt1->internal_timeout & IN_ABORT))
899 scsi_abort(SCpnt1, DID_RESET);
900 #endif
901 SCpnt1->flags |= (WAS_RESET | IS_RESETTING);
903 SCpnt1 = SCpnt1->next;
907 host->last_reset = jiffies;
908 host->resetting = 1;
910 * I suppose that the host reset callback will not play
911 * with the resetting field. We have just set the resetting
912 * flag here. -arca
914 temp = host->hostt->reset(SCpnt, reset_flags);
916 This test allows the driver to introduce an additional bus
917 settle time delay by setting last_reset up to 20 seconds in
918 the future. In the normal case where the driver does not
919 modify last_reset, it must be assumed that the actual bus
920 reset occurred immediately prior to the return to this code,
921 and so last_reset must be updated to the current time, so
922 that the delay in internal_cmnd will guarantee at least a
923 MIN_RESET_DELAY bus settle time.
925 if (host->last_reset - jiffies > 20UL * HZ)
926 host->last_reset = jiffies;
927 } else {
928 if (!host->block)
929 host->host_busy++;
930 host->last_reset = jiffies;
931 host->resetting = 1;
932 SCpnt->flags |= (WAS_RESET | IS_RESETTING);
934 * I suppose that the host reset callback will not play
935 * with the resetting field. We have just set the resetting
936 * flag here. -arca
938 temp = host->hostt->reset(SCpnt, reset_flags);
939 if (time_before(host->last_reset, jiffies) ||
940 (time_after(host->last_reset, jiffies + 20 * HZ)))
941 host->last_reset = jiffies;
942 if (!host->block)
943 host->host_busy--;
945 if (reset_flags & SCSI_RESET_SYNCHRONOUS)
946 SCpnt->flags &= ~SYNC_RESET;
948 #ifdef DEBUG
949 printk("scsi reset function returned %d\n", temp);
950 #endif
953 * Now figure out what we need to do, based upon
954 * what the low level driver said that it did.
955 * If the result is SCSI_RESET_SUCCESS, SCSI_RESET_PENDING,
956 * or SCSI_RESET_WAKEUP, then the low level driver did a
957 * bus device reset or bus reset, so we should go through
958 * and mark one or all of the devices on that bus
959 * as having been reset.
961 switch (temp & SCSI_RESET_ACTION) {
962 case SCSI_RESET_SUCCESS:
963 if (temp & SCSI_RESET_HOST_RESET)
964 scsi_mark_host_reset(host);
965 else if (temp & SCSI_RESET_BUS_RESET)
966 scsi_mark_bus_reset(host, SCpnt->channel);
967 else
968 scsi_mark_device_reset(SCpnt->device);
969 SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2 | IN_RESET3);
970 return 0;
971 case SCSI_RESET_PENDING:
972 if (temp & SCSI_RESET_HOST_RESET)
973 scsi_mark_host_reset(host);
974 else if (temp & SCSI_RESET_BUS_RESET)
975 scsi_mark_bus_reset(host, SCpnt->channel);
976 else
977 scsi_mark_device_reset(SCpnt->device);
978 case SCSI_RESET_NOT_RUNNING:
979 return 0;
980 case SCSI_RESET_PUNT:
981 SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2 | IN_RESET3);
982 scsi_request_sense(SCpnt);
983 return 0;
984 case SCSI_RESET_WAKEUP:
985 if (temp & SCSI_RESET_HOST_RESET)
986 scsi_mark_host_reset(host);
987 else if (temp & SCSI_RESET_BUS_RESET)
988 scsi_mark_bus_reset(host, SCpnt->channel);
989 else
990 scsi_mark_device_reset(SCpnt->device);
991 SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2 | IN_RESET3);
992 scsi_request_sense(SCpnt);
994 * If a bus reset was performed, we
995 * need to wake up each and every command
996 * that was active on the bus or if it was a HBA
997 * reset all active commands on all channels
999 if (temp & SCSI_RESET_HOST_RESET) {
1000 for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
1001 SCpnt1 = SDpnt->device_queue;
1002 while (SCpnt1) {
1003 if (SCpnt1->request.rq_status != RQ_INACTIVE
1004 && SCpnt1 != SCpnt)
1005 scsi_request_sense(SCpnt1);
1006 SCpnt1 = SCpnt1->next;
1009 } else if (temp & SCSI_RESET_BUS_RESET) {
1010 for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
1011 SCpnt1 = SDpnt->device_queue;
1012 while (SCpnt1) {
1013 if (SCpnt1->request.rq_status != RQ_INACTIVE
1014 && SCpnt1 != SCpnt
1015 && SCpnt1->channel == SCpnt->channel)
1016 scsi_request_sense(SCpnt);
1017 SCpnt1 = SCpnt1->next;
1021 return 0;
1022 case SCSI_RESET_SNOOZE:
1023 /* In this case, we set the timeout field to 0
1024 * so that this command does not time out any more,
1025 * and we return 1 so that we get a message on the
1026 * screen.
1028 SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2 | IN_RESET3);
1029 update_timeout(SCpnt, 0);
1030 /* If you snooze, you lose... */
1031 case SCSI_RESET_ERROR:
1032 default:
1033 return 1;
1036 return temp;
1042 * The strategy is to cause the timer code to call scsi_times_out()
1043 * when the soonest timeout is pending.
1044 * The arguments are used when we are queueing a new command, because
1045 * we do not want to subtract the time used from this time, but when we
1046 * set the timer, we want to take this value into account.
1049 int update_timeout(Scsi_Cmnd * SCset, int timeout)
1051 int rtn;
1054 * We are using the new error handling code to actually register/deregister
1055 * timers for timeout.
1058 if (!timer_pending(&SCset->eh_timeout)) {
1059 rtn = 0;
1060 } else {
1061 rtn = SCset->eh_timeout.expires - jiffies;
1064 if (timeout == 0) {
1065 scsi_delete_timer(SCset);
1066 } else {
1067 scsi_add_timer(SCset, timeout, scsi_old_times_out);
1070 return rtn;
1075 * Overrides for Emacs so that we follow Linus's tabbing style.
1076 * Emacs will notice this stuff at the end of the file and automatically
1077 * adjust the settings for this buffer only. This must remain at the end
1078 * of the file.
1079 * ---------------------------------------------------------------------------
1080 * Local variables:
1081 * c-indent-level: 4
1082 * c-brace-imaginary-offset: 0
1083 * c-brace-offset: -4
1084 * c-argdecl-indent: 4
1085 * c-label-offset: -4
1086 * c-continued-statement-offset: 4
1087 * c-continued-brace-offset: 0
1088 * indent-tabs-mode: nil
1089 * tab-width: 8
1090 * End: