- pre4:
[davej-history.git] / drivers / scsi / scsi_obsolete.c
blob0b25bd4e2e5e3580ab5b4cf65ae6d6bd2f66a8d4
1 /*
2 * scsi_obsolete.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@andante.org 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);
88 extern int scsi_dispatch_cmd(Scsi_Cmnd * SCpnt);
90 #define SCSI_BLOCK(HOST) (HOST->can_queue && HOST->host_busy >= HOST->can_queue)
92 static unsigned char generic_sense[6] =
93 {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 IN_ABORT 1
136 #define IN_RESET 2
137 #define IN_RESET2 4
138 #define IN_RESET3 8
141 * This is our time out function, called when the timer expires for a
142 * given host adapter. It will attempt to abort the currently executing
143 * command, that failing perform a kernel panic.
146 void scsi_old_times_out(Scsi_Cmnd * SCpnt)
148 unsigned long flags;
150 spin_lock_irqsave(&io_request_lock, flags);
152 /* Set the serial_number_at_timeout to the current serial_number */
153 SCpnt->serial_number_at_timeout = SCpnt->serial_number;
155 switch (SCpnt->internal_timeout & (IN_ABORT | IN_RESET | IN_RESET2 | IN_RESET3)) {
156 case NORMAL_TIMEOUT:
158 #ifdef DEBUG_TIMEOUT
159 scsi_dump_status();
160 #endif
163 if (!scsi_abort(SCpnt, DID_TIME_OUT))
164 break;
165 case IN_ABORT:
166 printk("SCSI host %d abort (pid %ld) timed out - resetting\n",
167 SCpnt->host->host_no, SCpnt->pid);
168 if (!scsi_reset(SCpnt, SCSI_RESET_ASYNCHRONOUS))
169 break;
170 case IN_RESET:
171 case (IN_ABORT | IN_RESET):
172 /* This might be controversial, but if there is a bus hang,
173 * you might conceivably want the machine up and running
174 * esp if you have an ide disk.
176 printk("SCSI host %d channel %d reset (pid %ld) timed out - "
177 "trying harder\n",
178 SCpnt->host->host_no, SCpnt->channel, SCpnt->pid);
179 SCpnt->internal_timeout &= ~IN_RESET;
180 SCpnt->internal_timeout |= IN_RESET2;
181 scsi_reset(SCpnt,
182 SCSI_RESET_ASYNCHRONOUS | SCSI_RESET_SUGGEST_BUS_RESET);
183 break;
184 case IN_RESET2:
185 case (IN_ABORT | IN_RESET2):
186 /* Obviously the bus reset didn't work.
187 * Let's try even harder and call for an HBA reset.
188 * Maybe the HBA itself crashed and this will shake it loose.
190 printk("SCSI host %d reset (pid %ld) timed out - trying to shake it loose\n",
191 SCpnt->host->host_no, SCpnt->pid);
192 SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2);
193 SCpnt->internal_timeout |= IN_RESET3;
194 scsi_reset(SCpnt,
195 SCSI_RESET_ASYNCHRONOUS | SCSI_RESET_SUGGEST_HOST_RESET);
196 break;
198 default:
199 printk("SCSI host %d reset (pid %ld) timed out again -\n",
200 SCpnt->host->host_no, SCpnt->pid);
201 printk("probably an unrecoverable SCSI bus or device hang.\n");
202 break;
205 spin_unlock_irqrestore(&io_request_lock, flags);
210 * From what I can find in scsi_obsolete.c, this function is only called
211 * by scsi_old_done and scsi_reset. Both of these functions run with the
212 * io_request_lock already held, so we need do nothing here about grabbing
213 * any locks.
215 static void scsi_request_sense(Scsi_Cmnd * SCpnt)
217 SCpnt->flags |= WAS_SENSE | ASKED_FOR_SENSE;
218 update_timeout(SCpnt, SENSE_TIMEOUT);
221 memcpy((void *) SCpnt->cmnd, (void *) generic_sense,
222 sizeof(generic_sense));
223 memset((void *) SCpnt->sense_buffer, 0,
224 sizeof(SCpnt->sense_buffer));
226 SCpnt->cmnd[1] = SCpnt->lun << 5;
227 SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);
229 SCpnt->request_buffer = &SCpnt->sense_buffer;
230 SCpnt->request_bufflen = sizeof(SCpnt->sense_buffer);
231 SCpnt->use_sg = 0;
232 SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
233 SCpnt->result = 0;
234 SCpnt->sc_data_direction = SCSI_DATA_READ;
237 * Ugly, ugly. The newer interfaces all assume that the lock
238 * isn't held. Mustn't disappoint, or we deadlock the system.
240 spin_unlock_irq(&io_request_lock);
241 scsi_dispatch_cmd(SCpnt);
242 spin_lock_irq(&io_request_lock);
248 static int check_sense(Scsi_Cmnd * SCpnt)
250 /* If there is no sense information, request it. If we have already
251 * requested it, there is no point in asking again - the firmware must
252 * be confused.
254 if (((SCpnt->sense_buffer[0] & 0x70) >> 4) != 7) {
255 if (!(SCpnt->flags & ASKED_FOR_SENSE))
256 return SUGGEST_SENSE;
257 else
258 return SUGGEST_RETRY;
260 SCpnt->flags &= ~ASKED_FOR_SENSE;
262 #ifdef DEBUG_INIT
263 printk("scsi%d, channel%d : ", SCpnt->host->host_no, SCpnt->channel);
264 print_sense("", SCpnt);
265 printk("\n");
266 #endif
267 if (SCpnt->sense_buffer[2] & 0xe0)
268 return SUGGEST_ABORT;
270 switch (SCpnt->sense_buffer[2] & 0xf) {
271 case NO_SENSE:
272 return 0;
273 case RECOVERED_ERROR:
274 return SUGGEST_IS_OK;
276 case ABORTED_COMMAND:
277 return SUGGEST_RETRY;
278 case NOT_READY:
279 case UNIT_ATTENTION:
281 * If we are expecting a CC/UA because of a bus reset that we
282 * performed, treat this just as a retry. Otherwise this is
283 * information that we should pass up to the upper-level driver
284 * so that we can deal with it there.
286 if (SCpnt->device->expecting_cc_ua) {
287 SCpnt->device->expecting_cc_ua = 0;
288 return SUGGEST_RETRY;
290 return SUGGEST_ABORT;
292 /* these three are not supported */
293 case COPY_ABORTED:
294 case VOLUME_OVERFLOW:
295 case MISCOMPARE:
297 case MEDIUM_ERROR:
298 return SUGGEST_REMAP;
299 case BLANK_CHECK:
300 case DATA_PROTECT:
301 case HARDWARE_ERROR:
302 case ILLEGAL_REQUEST:
303 default:
304 return SUGGEST_ABORT;
308 /* This function is the mid-level interrupt routine, which decides how
309 * to handle error conditions. Each invocation of this function must
310 * do one and *only* one of the following:
312 * (1) Call last_cmnd[host].done. This is done for fatal errors and
313 * normal completion, and indicates that the handling for this
314 * request is complete.
315 * (2) Call internal_cmnd to requeue the command. This will result in
316 * scsi_done being called again when the retry is complete.
317 * (3) Call scsi_request_sense. This asks the host adapter/drive for
318 * more information about the error condition. When the information
319 * is available, scsi_done will be called again.
320 * (4) Call reset(). This is sort of a last resort, and the idea is that
321 * this may kick things loose and get the drive working again. reset()
322 * automatically calls scsi_request_sense, and thus scsi_done will be
323 * called again once the reset is complete.
325 * If none of the above actions are taken, the drive in question
326 * will hang. If more than one of the above actions are taken by
327 * scsi_done, then unpredictable behavior will result.
329 void scsi_old_done(Scsi_Cmnd * SCpnt)
331 int status = 0;
332 int exit = 0;
333 int checked;
334 int oldto;
335 struct Scsi_Host *host = SCpnt->host;
336 Scsi_Device * device = SCpnt->device;
337 int result = SCpnt->result;
338 SCpnt->serial_number = 0;
339 SCpnt->serial_number_at_timeout = 0;
340 oldto = update_timeout(SCpnt, 0);
342 #ifdef DEBUG_TIMEOUT
343 if (result)
344 printk("Non-zero result in scsi_done %x %d:%d\n",
345 result, SCpnt->target, SCpnt->lun);
346 #endif
348 /* If we requested an abort, (and we got it) then fix up the return
349 * status to say why
351 if (host_byte(result) == DID_ABORT && SCpnt->abort_reason)
352 SCpnt->result = result = (result & 0xff00ffff) |
353 (SCpnt->abort_reason << 16);
356 #define CMD_FINISHED 0
357 #define MAYREDO 1
358 #define REDO 3
359 #define PENDING 4
361 #ifdef DEBUG
362 printk("In scsi_done(host = %d, result = %06x)\n", host->host_no, result);
363 #endif
365 if (SCpnt->flags & SYNC_RESET) {
367 * The behaviou of scsi_reset(SYNC) was changed in 2.1.? .
368 * The scsi mid-layer does a REDO after every sync reset, the driver
369 * must not do that any more. In order to prevent old drivers from
370 * crashing, all scsi_done() calls during sync resets are ignored.
372 printk("scsi%d: device driver called scsi_done() "
373 "for a syncronous reset.\n", SCpnt->host->host_no);
374 return;
376 if (SCpnt->flags & WAS_SENSE) {
377 SCpnt->use_sg = SCpnt->old_use_sg;
378 SCpnt->cmd_len = SCpnt->old_cmd_len;
379 SCpnt->sc_data_direction = SCpnt->sc_old_data_direction;
380 SCpnt->underflow = SCpnt->old_underflow;
382 switch (host_byte(result)) {
383 case DID_OK:
384 if (status_byte(result) && (SCpnt->flags & WAS_SENSE))
385 /* Failed to obtain sense information */
387 SCpnt->flags &= ~WAS_SENSE;
388 #if 0 /* This cannot possibly be correct. */
389 SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
390 #endif
392 if (!(SCpnt->flags & WAS_RESET)) {
393 printk("scsi%d : channel %d target %d lun %d request sense"
394 " failed, performing reset.\n",
395 SCpnt->host->host_no, SCpnt->channel, SCpnt->target,
396 SCpnt->lun);
397 scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
398 status = REDO;
399 break;
400 } else {
401 exit = (DRIVER_HARD | SUGGEST_ABORT);
402 status = CMD_FINISHED;
404 } else
405 switch (msg_byte(result)) {
406 case COMMAND_COMPLETE:
407 switch (status_byte(result)) {
408 case GOOD:
409 if (SCpnt->flags & WAS_SENSE) {
410 #ifdef DEBUG
411 printk("In scsi_done, GOOD status, COMMAND COMPLETE, "
412 "parsing sense information.\n");
413 #endif
414 SCpnt->flags &= ~WAS_SENSE;
415 #if 0 /* This cannot possibly be correct. */
416 SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
417 #endif
419 switch (checked = check_sense(SCpnt)) {
420 case SUGGEST_SENSE:
421 case 0:
422 #ifdef DEBUG
423 printk("NO SENSE. status = REDO\n");
424 #endif
425 update_timeout(SCpnt, oldto);
426 status = REDO;
427 break;
428 case SUGGEST_IS_OK:
429 break;
430 case SUGGEST_REMAP:
431 #ifdef DEBUG
432 printk("SENSE SUGGEST REMAP - status = CMD_FINISHED\n");
433 #endif
434 status = CMD_FINISHED;
435 exit = DRIVER_SENSE | SUGGEST_ABORT;
436 break;
437 case SUGGEST_RETRY:
438 #ifdef DEBUG
439 printk("SENSE SUGGEST RETRY - status = MAYREDO\n");
440 #endif
441 status = MAYREDO;
442 exit = DRIVER_SENSE | SUGGEST_RETRY;
443 break;
444 case SUGGEST_ABORT:
445 #ifdef DEBUG
446 printk("SENSE SUGGEST ABORT - status = CMD_FINISHED");
447 #endif
448 status = CMD_FINISHED;
449 exit = DRIVER_SENSE | SUGGEST_ABORT;
450 break;
451 default:
452 printk("Internal error %s %d \n", __FILE__,
453 __LINE__);
456 /* end WAS_SENSE */
457 else {
458 #ifdef DEBUG
459 printk("COMMAND COMPLETE message returned, "
460 "status = CMD_FINISHED. \n");
461 #endif
462 exit = DRIVER_OK;
463 status = CMD_FINISHED;
465 break;
467 case CHECK_CONDITION:
468 case COMMAND_TERMINATED:
469 switch (check_sense(SCpnt)) {
470 case 0:
471 update_timeout(SCpnt, oldto);
472 status = REDO;
473 break;
474 case SUGGEST_REMAP:
475 status = CMD_FINISHED;
476 exit = DRIVER_SENSE | SUGGEST_ABORT;
477 break;
478 case SUGGEST_RETRY:
479 status = MAYREDO;
480 exit = DRIVER_SENSE | SUGGEST_RETRY;
481 break;
482 case SUGGEST_ABORT:
483 status = CMD_FINISHED;
484 exit = DRIVER_SENSE | SUGGEST_ABORT;
485 break;
486 case SUGGEST_SENSE:
487 scsi_request_sense(SCpnt);
488 status = PENDING;
489 break;
491 break;
493 case CONDITION_GOOD:
494 case INTERMEDIATE_GOOD:
495 case INTERMEDIATE_C_GOOD:
496 break;
498 case BUSY:
499 case QUEUE_FULL:
500 update_timeout(SCpnt, oldto);
501 status = REDO;
502 break;
504 case RESERVATION_CONFLICT:
505 printk("scsi%d, channel %d : RESERVATION CONFLICT performing"
506 " reset.\n", SCpnt->host->host_no, SCpnt->channel);
507 scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
508 status = REDO;
509 break;
510 default:
511 printk("Internal error %s %d \n"
512 "status byte = %d \n", __FILE__,
513 __LINE__, status_byte(result));
516 break;
517 default:
518 panic("scsi: unsupported message byte %d received\n",
519 msg_byte(result));
521 break;
522 case DID_TIME_OUT:
523 #ifdef DEBUG
524 printk("Host returned DID_TIME_OUT - ");
525 #endif
527 if (SCpnt->flags & WAS_TIMEDOUT) {
528 #ifdef DEBUG
529 printk("Aborting\n");
530 #endif
532 Allow TEST_UNIT_READY and INQUIRY commands to timeout early
533 without causing resets. All other commands should be retried.
535 if (SCpnt->cmnd[0] != TEST_UNIT_READY &&
536 SCpnt->cmnd[0] != INQUIRY)
537 status = MAYREDO;
538 exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
539 } else {
540 #ifdef DEBUG
541 printk("Retrying.\n");
542 #endif
543 SCpnt->flags |= WAS_TIMEDOUT;
544 SCpnt->internal_timeout &= ~IN_ABORT;
545 status = REDO;
547 break;
548 case DID_BUS_BUSY:
549 case DID_PARITY:
550 status = REDO;
551 break;
552 case DID_NO_CONNECT:
553 #ifdef DEBUG
554 printk("Couldn't connect.\n");
555 #endif
556 exit = (DRIVER_HARD | SUGGEST_ABORT);
557 break;
558 case DID_ERROR:
559 status = MAYREDO;
560 exit = (DRIVER_HARD | SUGGEST_ABORT);
561 break;
562 case DID_BAD_TARGET:
563 case DID_ABORT:
564 exit = (DRIVER_INVALID | SUGGEST_ABORT);
565 break;
566 case DID_RESET:
567 if (SCpnt->flags & IS_RESETTING) {
568 SCpnt->flags &= ~IS_RESETTING;
569 status = REDO;
570 break;
572 if (msg_byte(result) == GOOD &&
573 status_byte(result) == CHECK_CONDITION) {
574 switch (check_sense(SCpnt)) {
575 case 0:
576 update_timeout(SCpnt, oldto);
577 status = REDO;
578 break;
579 case SUGGEST_REMAP:
580 case SUGGEST_RETRY:
581 status = MAYREDO;
582 exit = DRIVER_SENSE | SUGGEST_RETRY;
583 break;
584 case SUGGEST_ABORT:
585 status = CMD_FINISHED;
586 exit = DRIVER_SENSE | SUGGEST_ABORT;
587 break;
588 case SUGGEST_SENSE:
589 scsi_request_sense(SCpnt);
590 status = PENDING;
591 break;
593 } else {
594 status = REDO;
595 exit = SUGGEST_RETRY;
597 break;
598 default:
599 exit = (DRIVER_ERROR | SUGGEST_DIE);
602 switch (status) {
603 case CMD_FINISHED:
604 case PENDING:
605 break;
606 case MAYREDO:
607 #ifdef DEBUG
608 printk("In MAYREDO, allowing %d retries, have %d\n",
609 SCpnt->allowed, SCpnt->retries);
610 #endif
611 if ((++SCpnt->retries) < SCpnt->allowed) {
612 if ((SCpnt->retries >= (SCpnt->allowed >> 1))
613 && !(SCpnt->host->resetting && time_before(jiffies, SCpnt->host->last_reset + MIN_RESET_PERIOD))
614 && !(SCpnt->flags & WAS_RESET)) {
615 printk("scsi%d channel %d : resetting for second half of retries.\n",
616 SCpnt->host->host_no, SCpnt->channel);
617 scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
618 /* fall through to REDO */
620 } else {
621 status = CMD_FINISHED;
622 break;
624 /* fall through to REDO */
626 case REDO:
628 if (SCpnt->flags & WAS_SENSE)
629 scsi_request_sense(SCpnt);
630 else {
631 memcpy((void *) SCpnt->cmnd,
632 (void *) SCpnt->data_cmnd,
633 sizeof(SCpnt->data_cmnd));
634 memset((void *) SCpnt->sense_buffer, 0,
635 sizeof(SCpnt->sense_buffer));
636 SCpnt->request_buffer = SCpnt->buffer;
637 SCpnt->request_bufflen = SCpnt->bufflen;
638 SCpnt->use_sg = SCpnt->old_use_sg;
639 SCpnt->cmd_len = SCpnt->old_cmd_len;
640 SCpnt->sc_data_direction = SCpnt->sc_old_data_direction;
641 SCpnt->underflow = SCpnt->old_underflow;
642 SCpnt->result = 0;
644 * Ugly, ugly. The newer interfaces all
645 * assume that the lock isn't held. Mustn't
646 * disappoint, or we deadlock the system.
648 spin_unlock_irq(&io_request_lock);
649 scsi_dispatch_cmd(SCpnt);
650 spin_lock_irq(&io_request_lock);
652 break;
653 default:
654 INTERNAL_ERROR;
657 if (status == CMD_FINISHED) {
658 Scsi_Request *SRpnt;
659 #ifdef DEBUG
660 printk("Calling done function - at address %p\n", SCpnt->done);
661 #endif
662 host->host_busy--; /* Indicate that we are free */
663 device->device_busy--; /* Decrement device usage counter. */
665 SCpnt->result = result | ((exit & 0xff) << 24);
666 SCpnt->use_sg = SCpnt->old_use_sg;
667 SCpnt->cmd_len = SCpnt->old_cmd_len;
668 SCpnt->sc_data_direction = SCpnt->sc_old_data_direction;
669 SCpnt->underflow = SCpnt->old_underflow;
671 * The upper layers assume the lock isn't held. We mustn't
672 * disappoint them. When the new error handling code is in
673 * use, the upper code is run from a bottom half handler, so
674 * it isn't an issue.
676 spin_unlock_irq(&io_request_lock);
677 SRpnt = SCpnt->sc_request;
678 if( SRpnt != NULL ) {
679 SRpnt->sr_result = SRpnt->sr_command->result;
680 if( SRpnt->sr_result != 0 ) {
681 memcpy(SRpnt->sr_sense_buffer,
682 SRpnt->sr_command->sense_buffer,
683 sizeof(SRpnt->sr_sense_buffer));
687 SCpnt->done(SCpnt);
688 spin_lock_irq(&io_request_lock);
690 #undef CMD_FINISHED
691 #undef REDO
692 #undef MAYREDO
693 #undef PENDING
697 * The scsi_abort function interfaces with the abort() function of the host
698 * we are aborting, and causes the current command to not complete. The
699 * caller should deal with any error messages or status returned on the
700 * next call.
702 * This will not be called reentrantly for a given host.
706 * Since we're nice guys and specified that abort() and reset()
707 * can be non-reentrant. The internal_timeout flags are used for
708 * this.
712 static int scsi_abort(Scsi_Cmnd * SCpnt, int why)
714 int oldto;
715 struct Scsi_Host *host = SCpnt->host;
717 while (1) {
720 * Protect against races here. If the command is done, or we are
721 * on a different command forget it.
723 if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
724 return 0;
726 if (SCpnt->internal_timeout & IN_ABORT) {
727 spin_unlock_irq(&io_request_lock);
728 while (SCpnt->internal_timeout & IN_ABORT)
729 barrier();
730 spin_lock_irq(&io_request_lock);
731 } else {
732 SCpnt->internal_timeout |= IN_ABORT;
733 oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
735 if ((SCpnt->flags & IS_RESETTING) && SCpnt->device->soft_reset) {
736 /* OK, this command must have died when we did the
737 * reset. The device itself must have lied.
739 printk("Stale command on %d %d:%d appears to have died when"
740 " the bus was reset\n",
741 SCpnt->channel, SCpnt->target, SCpnt->lun);
743 if (!host->host_busy) {
744 SCpnt->internal_timeout &= ~IN_ABORT;
745 update_timeout(SCpnt, oldto);
746 return 0;
748 printk("scsi : aborting command due to timeout : pid %lu, scsi%d,"
749 " channel %d, id %d, lun %d ",
750 SCpnt->pid, SCpnt->host->host_no, (int) SCpnt->channel,
751 (int) SCpnt->target, (int) SCpnt->lun);
752 print_command(SCpnt->cmnd);
753 if (SCpnt->serial_number != SCpnt->serial_number_at_timeout)
754 return 0;
755 SCpnt->abort_reason = why;
756 switch (host->hostt->abort(SCpnt)) {
757 /* We do not know how to abort. Try waiting another
758 * time increment and see if this helps. Set the
759 * WAS_TIMEDOUT flag set so we do not try this twice
761 case SCSI_ABORT_BUSY: /* Tough call - returning 1 from
762 * this is too severe
764 case SCSI_ABORT_SNOOZE:
765 if (why == DID_TIME_OUT) {
766 SCpnt->internal_timeout &= ~IN_ABORT;
767 if (SCpnt->flags & WAS_TIMEDOUT) {
768 return 1; /* Indicate we cannot handle this.
769 * We drop down into the reset handler
770 * and try again
772 } else {
773 SCpnt->flags |= WAS_TIMEDOUT;
774 oldto = SCpnt->timeout_per_command;
775 update_timeout(SCpnt, oldto);
778 return 0;
779 case SCSI_ABORT_PENDING:
780 if (why != DID_TIME_OUT) {
781 update_timeout(SCpnt, oldto);
783 return 0;
784 case SCSI_ABORT_SUCCESS:
785 /* We should have already aborted this one. No
786 * need to adjust timeout
788 SCpnt->internal_timeout &= ~IN_ABORT;
789 return 0;
790 case SCSI_ABORT_NOT_RUNNING:
791 SCpnt->internal_timeout &= ~IN_ABORT;
792 update_timeout(SCpnt, 0);
793 return 0;
794 case SCSI_ABORT_ERROR:
795 default:
796 SCpnt->internal_timeout &= ~IN_ABORT;
797 return 1;
804 /* Mark a single SCSI Device as having been reset. */
806 static inline void scsi_mark_device_reset(Scsi_Device * Device)
808 Device->was_reset = 1;
809 Device->expecting_cc_ua = 1;
813 /* Mark all SCSI Devices on a specific Host as having been reset. */
815 void scsi_mark_host_reset(struct Scsi_Host *Host)
817 Scsi_Cmnd *SCpnt;
818 Scsi_Device *SDpnt;
820 for (SDpnt = Host->host_queue; SDpnt; SDpnt = SDpnt->next) {
821 for (SCpnt = SDpnt->device_queue; SCpnt; SCpnt = SCpnt->next)
822 scsi_mark_device_reset(SCpnt->device);
827 /* Mark all SCSI Devices on a specific Host Bus as having been reset. */
829 static void scsi_mark_bus_reset(struct Scsi_Host *Host, int channel)
831 Scsi_Cmnd *SCpnt;
832 Scsi_Device *SDpnt;
834 for (SDpnt = Host->host_queue; SDpnt; SDpnt = SDpnt->next) {
835 for (SCpnt = SDpnt->device_queue; SCpnt; SCpnt = SCpnt->next)
836 if (SCpnt->channel == channel)
837 scsi_mark_device_reset(SCpnt->device);
842 static int scsi_reset(Scsi_Cmnd * SCpnt, unsigned int reset_flags)
844 int temp;
845 Scsi_Cmnd *SCpnt1;
846 Scsi_Device *SDpnt;
847 struct Scsi_Host *host = SCpnt->host;
849 printk("SCSI bus is being reset for host %d channel %d.\n",
850 host->host_no, SCpnt->channel);
852 #if 0
854 * First of all, we need to make a recommendation to the low-level
855 * driver as to whether a BUS_DEVICE_RESET should be performed,
856 * or whether we should do a full BUS_RESET. There is no simple
857 * algorithm here - we basically use a series of heuristics
858 * to determine what we should do.
860 SCpnt->host->suggest_bus_reset = FALSE;
863 * First see if all of the active devices on the bus have
864 * been jammed up so that we are attempting resets. If so,
865 * then suggest a bus reset. Forcing a bus reset could
866 * result in some race conditions, but no more than
867 * you would usually get with timeouts. We will cross
868 * that bridge when we come to it.
870 * This is actually a pretty bad idea, since a sequence of
871 * commands will often timeout together and this will cause a
872 * Bus Device Reset followed immediately by a SCSI Bus Reset.
873 * If all of the active devices really are jammed up, the
874 * Bus Device Reset will quickly timeout and scsi_times_out
875 * will follow up with a SCSI Bus Reset anyway.
877 SCpnt1 = host->host_queue;
878 while (SCpnt1) {
879 if (SCpnt1->request.rq_status != RQ_INACTIVE
880 && (SCpnt1->flags & (WAS_RESET | IS_RESETTING)) == 0)
881 break;
882 SCpnt1 = SCpnt1->next;
884 if (SCpnt1 == NULL) {
885 reset_flags |= SCSI_RESET_SUGGEST_BUS_RESET;
888 * If the code that called us is suggesting a hard reset, then
889 * definitely request it. This usually occurs because a
890 * BUS_DEVICE_RESET times out.
892 * Passing reset_flags along takes care of this automatically.
894 if (reset_flags & SCSI_RESET_SUGGEST_BUS_RESET) {
895 SCpnt->host->suggest_bus_reset = TRUE;
897 #endif
899 while (1) {
902 * Protect against races here. If the command is done, or we are
903 * on a different command forget it.
905 if (reset_flags & SCSI_RESET_ASYNCHRONOUS)
906 if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
907 return 0;
909 if (SCpnt->internal_timeout & IN_RESET) {
910 spin_unlock_irq(&io_request_lock);
911 while (SCpnt->internal_timeout & IN_RESET)
912 barrier();
913 spin_lock_irq(&io_request_lock);
914 } else {
915 SCpnt->internal_timeout |= IN_RESET;
916 update_timeout(SCpnt, RESET_TIMEOUT);
918 if (reset_flags & SCSI_RESET_SYNCHRONOUS)
919 SCpnt->flags |= SYNC_RESET;
920 if (host->host_busy) {
921 for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
922 SCpnt1 = SDpnt->device_queue;
923 while (SCpnt1) {
924 if (SCpnt1->request.rq_status != RQ_INACTIVE) {
925 #if 0
926 if (!(SCpnt1->flags & IS_RESETTING) &&
927 !(SCpnt1->internal_timeout & IN_ABORT))
928 scsi_abort(SCpnt1, DID_RESET);
929 #endif
930 SCpnt1->flags |= (WAS_RESET | IS_RESETTING);
932 SCpnt1 = SCpnt1->next;
936 host->last_reset = jiffies;
937 host->resetting = 1;
939 * I suppose that the host reset callback will not play
940 * with the resetting field. We have just set the resetting
941 * flag here. -arca
943 temp = host->hostt->reset(SCpnt, reset_flags);
945 This test allows the driver to introduce an additional bus
946 settle time delay by setting last_reset up to 20 seconds in
947 the future. In the normal case where the driver does not
948 modify last_reset, it must be assumed that the actual bus
949 reset occurred immediately prior to the return to this code,
950 and so last_reset must be updated to the current time, so
951 that the delay in internal_cmnd will guarantee at least a
952 MIN_RESET_DELAY bus settle time.
954 if (host->last_reset - jiffies > 20UL * HZ)
955 host->last_reset = jiffies;
956 } else {
957 host->host_busy++;
958 host->last_reset = jiffies;
959 host->resetting = 1;
960 SCpnt->flags |= (WAS_RESET | IS_RESETTING);
962 * I suppose that the host reset callback will not play
963 * with the resetting field. We have just set the resetting
964 * flag here. -arca
966 temp = host->hostt->reset(SCpnt, reset_flags);
967 if (time_before(host->last_reset, jiffies) ||
968 (time_after(host->last_reset, jiffies + 20 * HZ)))
969 host->last_reset = jiffies;
970 host->host_busy--;
972 if (reset_flags & SCSI_RESET_SYNCHRONOUS)
973 SCpnt->flags &= ~SYNC_RESET;
975 #ifdef DEBUG
976 printk("scsi reset function returned %d\n", temp);
977 #endif
980 * Now figure out what we need to do, based upon
981 * what the low level driver said that it did.
982 * If the result is SCSI_RESET_SUCCESS, SCSI_RESET_PENDING,
983 * or SCSI_RESET_WAKEUP, then the low level driver did a
984 * bus device reset or bus reset, so we should go through
985 * and mark one or all of the devices on that bus
986 * as having been reset.
988 switch (temp & SCSI_RESET_ACTION) {
989 case SCSI_RESET_SUCCESS:
990 if (temp & SCSI_RESET_HOST_RESET)
991 scsi_mark_host_reset(host);
992 else if (temp & SCSI_RESET_BUS_RESET)
993 scsi_mark_bus_reset(host, SCpnt->channel);
994 else
995 scsi_mark_device_reset(SCpnt->device);
996 SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2 | IN_RESET3);
997 return 0;
998 case SCSI_RESET_PENDING:
999 if (temp & SCSI_RESET_HOST_RESET)
1000 scsi_mark_host_reset(host);
1001 else if (temp & SCSI_RESET_BUS_RESET)
1002 scsi_mark_bus_reset(host, SCpnt->channel);
1003 else
1004 scsi_mark_device_reset(SCpnt->device);
1005 case SCSI_RESET_NOT_RUNNING:
1006 return 0;
1007 case SCSI_RESET_PUNT:
1008 SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2 | IN_RESET3);
1009 scsi_request_sense(SCpnt);
1010 return 0;
1011 case SCSI_RESET_WAKEUP:
1012 if (temp & SCSI_RESET_HOST_RESET)
1013 scsi_mark_host_reset(host);
1014 else if (temp & SCSI_RESET_BUS_RESET)
1015 scsi_mark_bus_reset(host, SCpnt->channel);
1016 else
1017 scsi_mark_device_reset(SCpnt->device);
1018 SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2 | IN_RESET3);
1019 scsi_request_sense(SCpnt);
1021 * If a bus reset was performed, we
1022 * need to wake up each and every command
1023 * that was active on the bus or if it was a HBA
1024 * reset all active commands on all channels
1026 if (temp & SCSI_RESET_HOST_RESET) {
1027 for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
1028 SCpnt1 = SDpnt->device_queue;
1029 while (SCpnt1) {
1030 if (SCpnt1->request.rq_status != RQ_INACTIVE
1031 && SCpnt1 != SCpnt)
1032 scsi_request_sense(SCpnt1);
1033 SCpnt1 = SCpnt1->next;
1036 } else if (temp & SCSI_RESET_BUS_RESET) {
1037 for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
1038 SCpnt1 = SDpnt->device_queue;
1039 while (SCpnt1) {
1040 if (SCpnt1->request.rq_status != RQ_INACTIVE
1041 && SCpnt1 != SCpnt
1042 && SCpnt1->channel == SCpnt->channel)
1043 scsi_request_sense(SCpnt);
1044 SCpnt1 = SCpnt1->next;
1048 return 0;
1049 case SCSI_RESET_SNOOZE:
1050 /* In this case, we set the timeout field to 0
1051 * so that this command does not time out any more,
1052 * and we return 1 so that we get a message on the
1053 * screen.
1055 SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2 | IN_RESET3);
1056 update_timeout(SCpnt, 0);
1057 /* If you snooze, you lose... */
1058 case SCSI_RESET_ERROR:
1059 default:
1060 return 1;
1063 return temp;
1069 * The strategy is to cause the timer code to call scsi_times_out()
1070 * when the soonest timeout is pending.
1071 * The arguments are used when we are queueing a new command, because
1072 * we do not want to subtract the time used from this time, but when we
1073 * set the timer, we want to take this value into account.
1076 int update_timeout(Scsi_Cmnd * SCset, int timeout)
1078 int rtn;
1081 * We are using the new error handling code to actually register/deregister
1082 * timers for timeout.
1085 if (!timer_pending(&SCset->eh_timeout)) {
1086 rtn = 0;
1087 } else {
1088 rtn = SCset->eh_timeout.expires - jiffies;
1091 if (timeout == 0) {
1092 scsi_delete_timer(SCset);
1093 } else {
1094 scsi_add_timer(SCset, timeout, scsi_old_times_out);
1097 return rtn;
1102 * Overrides for Emacs so that we follow Linus's tabbing style.
1103 * Emacs will notice this stuff at the end of the file and automatically
1104 * adjust the settings for this buffer only. This must remain at the end
1105 * of the file.
1106 * ---------------------------------------------------------------------------
1107 * Local variables:
1108 * c-indent-level: 4
1109 * c-brace-imaginary-offset: 0
1110 * c-brace-offset: -4
1111 * c-argdecl-indent: 4
1112 * c-label-offset: -4
1113 * c-continued-statement-offset: 4
1114 * c-continued-brace-offset: 0
1115 * indent-tabs-mode: nil
1116 * tab-width: 8
1117 * End: