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
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
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/slab.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>
69 #include "constants.h"
71 #undef USE_STATIC_SCSI_MEMORY
74 static const char RCSid[] = "$Header: /cvshome/samwise/ppclinux/drivers/scsi/scsi_obsolete.c,v 1.1.1.1 2005/04/11 02:50:36 jack 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
106 static void scsi_dump_status(void);
111 #define SCSI_TIMEOUT (5*HZ)
113 #define SCSI_TIMEOUT (2*HZ)
117 #define SENSE_TIMEOUT SCSI_TIMEOUT
118 #define ABORT_TIMEOUT SCSI_TIMEOUT
119 #define RESET_TIMEOUT SCSI_TIMEOUT
121 #define SENSE_TIMEOUT (5*HZ/10)
122 #define RESET_TIMEOUT (5*HZ/10)
123 #define ABORT_TIMEOUT (5*HZ/10)
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
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)
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)) {
163 if (!scsi_abort(SCpnt, DID_TIME_OUT))
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))
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 - "
178 SCpnt->host->host_no, SCpnt->channel, SCpnt->pid);
179 SCpnt->internal_timeout &= ~IN_RESET;
180 SCpnt->internal_timeout |= IN_RESET2;
182 SCSI_RESET_ASYNCHRONOUS | SCSI_RESET_SUGGEST_BUS_RESET);
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;
195 SCSI_RESET_ASYNCHRONOUS | SCSI_RESET_SUGGEST_HOST_RESET);
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");
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
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 if (SCpnt->device->scsi_level <= SCSI_2)
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);
233 SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
235 SCpnt->sc_data_direction = SCSI_DATA_READ;
238 * Ugly, ugly. The newer interfaces all assume that the lock
239 * isn't held. Mustn't disappoint, or we deadlock the system.
241 spin_unlock_irq(&io_request_lock);
242 scsi_dispatch_cmd(SCpnt);
243 spin_lock_irq(&io_request_lock);
249 static int check_sense(Scsi_Cmnd * SCpnt)
251 /* If there is no sense information, request it. If we have already
252 * requested it, there is no point in asking again - the firmware must
255 if (((SCpnt->sense_buffer[0] & 0x70) >> 4) != 7) {
256 if (!(SCpnt->flags & ASKED_FOR_SENSE))
257 return SUGGEST_SENSE;
259 return SUGGEST_RETRY;
261 SCpnt->flags &= ~ASKED_FOR_SENSE;
264 printk("scsi%d, channel%d : ", SCpnt->host->host_no, SCpnt->channel);
265 print_sense("", SCpnt);
268 if (SCpnt->sense_buffer[2] & 0xe0)
269 return SUGGEST_ABORT;
271 switch (SCpnt->sense_buffer[2] & 0xf) {
274 case RECOVERED_ERROR:
275 return SUGGEST_IS_OK;
277 case ABORTED_COMMAND:
278 return SUGGEST_RETRY;
282 * If we are expecting a CC/UA because of a bus reset that we
283 * performed, treat this just as a retry. Otherwise this is
284 * information that we should pass up to the upper-level driver
285 * so that we can deal with it there.
287 if (SCpnt->device->expecting_cc_ua) {
288 SCpnt->device->expecting_cc_ua = 0;
289 return SUGGEST_RETRY;
291 return SUGGEST_ABORT;
293 /* these three are not supported */
295 case VOLUME_OVERFLOW:
299 return SUGGEST_REMAP;
303 case ILLEGAL_REQUEST:
305 return SUGGEST_ABORT;
309 /* This function is the mid-level interrupt routine, which decides how
310 * to handle error conditions. Each invocation of this function must
311 * do one and *only* one of the following:
313 * (1) Call last_cmnd[host].done. This is done for fatal errors and
314 * normal completion, and indicates that the handling for this
315 * request is complete.
316 * (2) Call internal_cmnd to requeue the command. This will result in
317 * scsi_done being called again when the retry is complete.
318 * (3) Call scsi_request_sense. This asks the host adapter/drive for
319 * more information about the error condition. When the information
320 * is available, scsi_done will be called again.
321 * (4) Call reset(). This is sort of a last resort, and the idea is that
322 * this may kick things loose and get the drive working again. reset()
323 * automatically calls scsi_request_sense, and thus scsi_done will be
324 * called again once the reset is complete.
326 * If none of the above actions are taken, the drive in question
327 * will hang. If more than one of the above actions are taken by
328 * scsi_done, then unpredictable behavior will result.
330 void scsi_old_done(Scsi_Cmnd * SCpnt)
336 struct Scsi_Host *host = SCpnt->host;
337 Scsi_Device * device = SCpnt->device;
338 int result = SCpnt->result;
339 SCpnt->serial_number = 0;
340 SCpnt->serial_number_at_timeout = 0;
341 oldto = update_timeout(SCpnt, 0);
345 printk("Non-zero result in scsi_done %x %d:%d\n",
346 result, SCpnt->target, SCpnt->lun);
349 /* If we requested an abort, (and we got it) then fix up the return
352 if (host_byte(result) == DID_ABORT && SCpnt->abort_reason)
353 SCpnt->result = result = (result & 0xff00ffff) |
354 (SCpnt->abort_reason << 16);
357 #define CMD_FINISHED 0
363 printk("In scsi_done(host = %d, result = %06x)\n", host->host_no, result);
366 if (SCpnt->flags & SYNC_RESET) {
368 * The behaviou of scsi_reset(SYNC) was changed in 2.1.? .
369 * The scsi mid-layer does a REDO after every sync reset, the driver
370 * must not do that any more. In order to prevent old drivers from
371 * crashing, all scsi_done() calls during sync resets are ignored.
373 printk("scsi%d: device driver called scsi_done() "
374 "for a synchronous reset.\n", SCpnt->host->host_no);
377 if (SCpnt->flags & WAS_SENSE) {
378 SCpnt->use_sg = SCpnt->old_use_sg;
379 SCpnt->cmd_len = SCpnt->old_cmd_len;
380 SCpnt->sc_data_direction = SCpnt->sc_old_data_direction;
381 SCpnt->underflow = SCpnt->old_underflow;
383 switch (host_byte(result)) {
385 if (status_byte(result) && (SCpnt->flags & WAS_SENSE))
386 /* Failed to obtain sense information */
388 SCpnt->flags &= ~WAS_SENSE;
389 #if 0 /* This cannot possibly be correct. */
390 SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
393 if (!(SCpnt->flags & WAS_RESET)) {
394 printk("scsi%d : channel %d target %d lun %d request sense"
395 " failed, performing reset.\n",
396 SCpnt->host->host_no, SCpnt->channel, SCpnt->target,
398 scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
402 exit = (DRIVER_HARD | SUGGEST_ABORT);
403 status = CMD_FINISHED;
406 switch (msg_byte(result)) {
407 case COMMAND_COMPLETE:
408 switch (status_byte(result)) {
410 if (SCpnt->flags & WAS_SENSE) {
412 printk("In scsi_done, GOOD status, COMMAND COMPLETE, "
413 "parsing sense information.\n");
415 SCpnt->flags &= ~WAS_SENSE;
416 #if 0 /* This cannot possibly be correct. */
417 SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
420 switch (checked = check_sense(SCpnt)) {
424 printk("NO SENSE. status = REDO\n");
426 update_timeout(SCpnt, oldto);
433 printk("SENSE SUGGEST REMAP - status = CMD_FINISHED\n");
435 status = CMD_FINISHED;
436 exit = DRIVER_SENSE | SUGGEST_ABORT;
440 printk("SENSE SUGGEST RETRY - status = MAYREDO\n");
443 exit = DRIVER_SENSE | SUGGEST_RETRY;
447 printk("SENSE SUGGEST ABORT - status = CMD_FINISHED");
449 status = CMD_FINISHED;
450 exit = DRIVER_SENSE | SUGGEST_ABORT;
453 printk("Internal error %s %d \n", __FILE__,
460 printk("COMMAND COMPLETE message returned, "
461 "status = CMD_FINISHED. \n");
464 status = CMD_FINISHED;
468 case CHECK_CONDITION:
469 case COMMAND_TERMINATED:
470 switch (check_sense(SCpnt)) {
472 update_timeout(SCpnt, oldto);
476 status = CMD_FINISHED;
477 exit = DRIVER_SENSE | SUGGEST_ABORT;
481 exit = DRIVER_SENSE | SUGGEST_RETRY;
484 status = CMD_FINISHED;
485 exit = DRIVER_SENSE | SUGGEST_ABORT;
488 scsi_request_sense(SCpnt);
495 case INTERMEDIATE_GOOD:
496 case INTERMEDIATE_C_GOOD:
501 update_timeout(SCpnt, oldto);
505 case RESERVATION_CONFLICT:
507 * Most HAs will return an error for
508 * this, so usually reservation
509 * conflicts will be processed under
512 printk("scsi%d (%d,%d,%d) : RESERVATION CONFLICT\n",
513 SCpnt->host->host_no, SCpnt->channel,
514 SCpnt->device->id, SCpnt->device->lun);
515 status = CMD_FINISHED; /* returns I/O error */
519 printk("Internal error %s %d \n"
520 "status byte = %d \n", __FILE__,
521 __LINE__, status_byte(result));
526 panic("scsi: unsupported message byte %d received\n",
532 printk("Host returned DID_TIME_OUT - ");
535 if (SCpnt->flags & WAS_TIMEDOUT) {
537 printk("Aborting\n");
540 Allow TEST_UNIT_READY and INQUIRY commands to timeout early
541 without causing resets. All other commands should be retried.
543 if (SCpnt->cmnd[0] != TEST_UNIT_READY &&
544 SCpnt->cmnd[0] != INQUIRY)
546 exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
549 printk("Retrying.\n");
551 SCpnt->flags |= WAS_TIMEDOUT;
552 SCpnt->internal_timeout &= ~IN_ABORT;
562 printk("Couldn't connect.\n");
564 exit = (DRIVER_HARD | SUGGEST_ABORT);
567 if (msg_byte(result) == COMMAND_COMPLETE &&
568 status_byte(result) == RESERVATION_CONFLICT) {
569 printk("scsi%d (%d,%d,%d) : RESERVATION CONFLICT\n",
570 SCpnt->host->host_no, SCpnt->channel,
571 SCpnt->device->id, SCpnt->device->lun);
572 status = CMD_FINISHED; /* returns I/O error */
576 exit = (DRIVER_HARD | SUGGEST_ABORT);
580 exit = (DRIVER_INVALID | SUGGEST_ABORT);
583 if (SCpnt->flags & IS_RESETTING) {
584 SCpnt->flags &= ~IS_RESETTING;
588 if (msg_byte(result) == GOOD &&
589 status_byte(result) == CHECK_CONDITION) {
590 switch (check_sense(SCpnt)) {
592 update_timeout(SCpnt, oldto);
598 exit = DRIVER_SENSE | SUGGEST_RETRY;
601 status = CMD_FINISHED;
602 exit = DRIVER_SENSE | SUGGEST_ABORT;
605 scsi_request_sense(SCpnt);
611 exit = SUGGEST_RETRY;
615 exit = (DRIVER_ERROR | SUGGEST_DIE);
624 printk("In MAYREDO, allowing %d retries, have %d\n",
625 SCpnt->allowed, SCpnt->retries);
627 if ((++SCpnt->retries) < SCpnt->allowed) {
628 if ((SCpnt->retries >= (SCpnt->allowed >> 1))
629 && !(SCpnt->host->resetting && time_before(jiffies, SCpnt->host->last_reset + MIN_RESET_PERIOD))
630 && !(SCpnt->flags & WAS_RESET)) {
631 printk("scsi%d channel %d : resetting for second half of retries.\n",
632 SCpnt->host->host_no, SCpnt->channel);
633 scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
634 /* fall through to REDO */
637 status = CMD_FINISHED;
640 /* fall through to REDO */
644 if (SCpnt->flags & WAS_SENSE)
645 scsi_request_sense(SCpnt);
647 memcpy((void *) SCpnt->cmnd,
648 (void *) SCpnt->data_cmnd,
649 sizeof(SCpnt->data_cmnd));
650 memset((void *) SCpnt->sense_buffer, 0,
651 sizeof(SCpnt->sense_buffer));
652 SCpnt->request_buffer = SCpnt->buffer;
653 SCpnt->request_bufflen = SCpnt->bufflen;
654 SCpnt->use_sg = SCpnt->old_use_sg;
655 SCpnt->cmd_len = SCpnt->old_cmd_len;
656 SCpnt->sc_data_direction = SCpnt->sc_old_data_direction;
657 SCpnt->underflow = SCpnt->old_underflow;
660 * Ugly, ugly. The newer interfaces all
661 * assume that the lock isn't held. Mustn't
662 * disappoint, or we deadlock the system.
664 spin_unlock_irq(&io_request_lock);
665 scsi_dispatch_cmd(SCpnt);
666 spin_lock_irq(&io_request_lock);
673 if (status == CMD_FINISHED) {
676 printk("Calling done function - at address %p\n", SCpnt->done);
678 host->host_busy--; /* Indicate that we are free */
679 device->device_busy--; /* Decrement device usage counter. */
681 SCpnt->result = result | ((exit & 0xff) << 24);
682 SCpnt->use_sg = SCpnt->old_use_sg;
683 SCpnt->cmd_len = SCpnt->old_cmd_len;
684 SCpnt->sc_data_direction = SCpnt->sc_old_data_direction;
685 SCpnt->underflow = SCpnt->old_underflow;
687 * The upper layers assume the lock isn't held. We mustn't
688 * disappoint them. When the new error handling code is in
689 * use, the upper code is run from a bottom half handler, so
692 spin_unlock_irq(&io_request_lock);
693 SRpnt = SCpnt->sc_request;
694 if( SRpnt != NULL ) {
695 SRpnt->sr_result = SRpnt->sr_command->result;
696 if( SRpnt->sr_result != 0 ) {
697 memcpy(SRpnt->sr_sense_buffer,
698 SRpnt->sr_command->sense_buffer,
699 sizeof(SRpnt->sr_sense_buffer));
704 spin_lock_irq(&io_request_lock);
713 * The scsi_abort function interfaces with the abort() function of the host
714 * we are aborting, and causes the current command to not complete. The
715 * caller should deal with any error messages or status returned on the
718 * This will not be called reentrantly for a given host.
722 * Since we're nice guys and specified that abort() and reset()
723 * can be non-reentrant. The internal_timeout flags are used for
728 static int scsi_abort(Scsi_Cmnd * SCpnt, int why)
731 struct Scsi_Host *host = SCpnt->host;
736 * Protect against races here. If the command is done, or we are
737 * on a different command forget it.
739 if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
742 if (SCpnt->internal_timeout & IN_ABORT) {
743 spin_unlock_irq(&io_request_lock);
744 while (SCpnt->internal_timeout & IN_ABORT)
746 spin_lock_irq(&io_request_lock);
748 SCpnt->internal_timeout |= IN_ABORT;
749 oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
751 if ((SCpnt->flags & IS_RESETTING) && SCpnt->device->soft_reset) {
752 /* OK, this command must have died when we did the
753 * reset. The device itself must have lied.
755 printk("Stale command on %d %d:%d appears to have died when"
756 " the bus was reset\n",
757 SCpnt->channel, SCpnt->target, SCpnt->lun);
759 if (!host->host_busy) {
760 SCpnt->internal_timeout &= ~IN_ABORT;
761 update_timeout(SCpnt, oldto);
764 printk("scsi : aborting command due to timeout : pid %lu, scsi%d,"
765 " channel %d, id %d, lun %d ",
766 SCpnt->pid, SCpnt->host->host_no, (int) SCpnt->channel,
767 (int) SCpnt->target, (int) SCpnt->lun);
768 print_command(SCpnt->cmnd);
769 if (SCpnt->serial_number != SCpnt->serial_number_at_timeout)
771 SCpnt->abort_reason = why;
772 switch (host->hostt->abort(SCpnt)) {
773 /* We do not know how to abort. Try waiting another
774 * time increment and see if this helps. Set the
775 * WAS_TIMEDOUT flag set so we do not try this twice
777 case SCSI_ABORT_BUSY: /* Tough call - returning 1 from
780 case SCSI_ABORT_SNOOZE:
781 if (why == DID_TIME_OUT) {
782 SCpnt->internal_timeout &= ~IN_ABORT;
783 if (SCpnt->flags & WAS_TIMEDOUT) {
784 return 1; /* Indicate we cannot handle this.
785 * We drop down into the reset handler
789 SCpnt->flags |= WAS_TIMEDOUT;
790 oldto = SCpnt->timeout_per_command;
791 update_timeout(SCpnt, oldto);
795 case SCSI_ABORT_PENDING:
796 if (why != DID_TIME_OUT) {
797 update_timeout(SCpnt, oldto);
800 case SCSI_ABORT_SUCCESS:
801 /* We should have already aborted this one. No
802 * need to adjust timeout
804 SCpnt->internal_timeout &= ~IN_ABORT;
806 case SCSI_ABORT_NOT_RUNNING:
807 SCpnt->internal_timeout &= ~IN_ABORT;
808 update_timeout(SCpnt, 0);
810 case SCSI_ABORT_ERROR:
812 SCpnt->internal_timeout &= ~IN_ABORT;
820 /* Mark a single SCSI Device as having been reset. */
822 static inline void scsi_mark_device_reset(Scsi_Device * Device)
824 Device->was_reset = 1;
825 Device->expecting_cc_ua = 1;
829 /* Mark all SCSI Devices on a specific Host as having been reset. */
831 void scsi_mark_host_reset(struct Scsi_Host *Host)
836 for (SDpnt = Host->host_queue; SDpnt; SDpnt = SDpnt->next) {
837 for (SCpnt = SDpnt->device_queue; SCpnt; SCpnt = SCpnt->next)
838 scsi_mark_device_reset(SCpnt->device);
843 /* Mark all SCSI Devices on a specific Host Bus as having been reset. */
845 static void scsi_mark_bus_reset(struct Scsi_Host *Host, int channel)
850 for (SDpnt = Host->host_queue; SDpnt; SDpnt = SDpnt->next) {
851 for (SCpnt = SDpnt->device_queue; SCpnt; SCpnt = SCpnt->next)
852 if (SCpnt->channel == channel)
853 scsi_mark_device_reset(SCpnt->device);
858 static int scsi_reset(Scsi_Cmnd * SCpnt, unsigned int reset_flags)
863 struct Scsi_Host *host = SCpnt->host;
865 printk("SCSI bus is being reset for host %d channel %d.\n",
866 host->host_no, SCpnt->channel);
870 * First of all, we need to make a recommendation to the low-level
871 * driver as to whether a BUS_DEVICE_RESET should be performed,
872 * or whether we should do a full BUS_RESET. There is no simple
873 * algorithm here - we basically use a series of heuristics
874 * to determine what we should do.
876 SCpnt->host->suggest_bus_reset = FALSE;
879 * First see if all of the active devices on the bus have
880 * been jammed up so that we are attempting resets. If so,
881 * then suggest a bus reset. Forcing a bus reset could
882 * result in some race conditions, but no more than
883 * you would usually get with timeouts. We will cross
884 * that bridge when we come to it.
886 * This is actually a pretty bad idea, since a sequence of
887 * commands will often timeout together and this will cause a
888 * Bus Device Reset followed immediately by a SCSI Bus Reset.
889 * If all of the active devices really are jammed up, the
890 * Bus Device Reset will quickly timeout and scsi_times_out
891 * will follow up with a SCSI Bus Reset anyway.
893 SCpnt1 = host->host_queue;
895 if (SCpnt1->request.rq_status != RQ_INACTIVE
896 && (SCpnt1->flags & (WAS_RESET | IS_RESETTING)) == 0)
898 SCpnt1 = SCpnt1->next;
900 if (SCpnt1 == NULL) {
901 reset_flags |= SCSI_RESET_SUGGEST_BUS_RESET;
904 * If the code that called us is suggesting a hard reset, then
905 * definitely request it. This usually occurs because a
906 * BUS_DEVICE_RESET times out.
908 * Passing reset_flags along takes care of this automatically.
910 if (reset_flags & SCSI_RESET_SUGGEST_BUS_RESET) {
911 SCpnt->host->suggest_bus_reset = TRUE;
918 * Protect against races here. If the command is done, or we are
919 * on a different command forget it.
921 if (reset_flags & SCSI_RESET_ASYNCHRONOUS)
922 if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
925 if (SCpnt->internal_timeout & IN_RESET) {
926 spin_unlock_irq(&io_request_lock);
927 while (SCpnt->internal_timeout & IN_RESET)
929 spin_lock_irq(&io_request_lock);
931 SCpnt->internal_timeout |= IN_RESET;
932 update_timeout(SCpnt, RESET_TIMEOUT);
934 if (reset_flags & SCSI_RESET_SYNCHRONOUS)
935 SCpnt->flags |= SYNC_RESET;
936 if (host->host_busy) {
937 for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
938 SCpnt1 = SDpnt->device_queue;
940 if (SCpnt1->request.rq_status != RQ_INACTIVE) {
942 if (!(SCpnt1->flags & IS_RESETTING) &&
943 !(SCpnt1->internal_timeout & IN_ABORT))
944 scsi_abort(SCpnt1, DID_RESET);
946 SCpnt1->flags |= (WAS_RESET | IS_RESETTING);
948 SCpnt1 = SCpnt1->next;
952 host->last_reset = jiffies;
955 * I suppose that the host reset callback will not play
956 * with the resetting field. We have just set the resetting
959 temp = host->hostt->reset(SCpnt, reset_flags);
961 This test allows the driver to introduce an additional bus
962 settle time delay by setting last_reset up to 20 seconds in
963 the future. In the normal case where the driver does not
964 modify last_reset, it must be assumed that the actual bus
965 reset occurred immediately prior to the return to this code,
966 and so last_reset must be updated to the current time, so
967 that the delay in internal_cmnd will guarantee at least a
968 MIN_RESET_DELAY bus settle time.
970 if (host->last_reset - jiffies > 20UL * HZ)
971 host->last_reset = jiffies;
974 host->last_reset = jiffies;
976 SCpnt->flags |= (WAS_RESET | IS_RESETTING);
978 * I suppose that the host reset callback will not play
979 * with the resetting field. We have just set the resetting
982 temp = host->hostt->reset(SCpnt, reset_flags);
983 if (time_before(host->last_reset, jiffies) ||
984 (time_after(host->last_reset, jiffies + 20 * HZ)))
985 host->last_reset = jiffies;
988 if (reset_flags & SCSI_RESET_SYNCHRONOUS)
989 SCpnt->flags &= ~SYNC_RESET;
992 printk("scsi reset function returned %d\n", temp);
996 * Now figure out what we need to do, based upon
997 * what the low level driver said that it did.
998 * If the result is SCSI_RESET_SUCCESS, SCSI_RESET_PENDING,
999 * or SCSI_RESET_WAKEUP, then the low level driver did a
1000 * bus device reset or bus reset, so we should go through
1001 * and mark one or all of the devices on that bus
1002 * as having been reset.
1004 switch (temp & SCSI_RESET_ACTION) {
1005 case SCSI_RESET_SUCCESS:
1006 if (temp & SCSI_RESET_HOST_RESET)
1007 scsi_mark_host_reset(host);
1008 else if (temp & SCSI_RESET_BUS_RESET)
1009 scsi_mark_bus_reset(host, SCpnt->channel);
1011 scsi_mark_device_reset(SCpnt->device);
1012 SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2 | IN_RESET3);
1014 case SCSI_RESET_PENDING:
1015 if (temp & SCSI_RESET_HOST_RESET)
1016 scsi_mark_host_reset(host);
1017 else if (temp & SCSI_RESET_BUS_RESET)
1018 scsi_mark_bus_reset(host, SCpnt->channel);
1020 scsi_mark_device_reset(SCpnt->device);
1021 case SCSI_RESET_NOT_RUNNING:
1023 case SCSI_RESET_PUNT:
1024 SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2 | IN_RESET3);
1025 scsi_request_sense(SCpnt);
1027 case SCSI_RESET_WAKEUP:
1028 if (temp & SCSI_RESET_HOST_RESET)
1029 scsi_mark_host_reset(host);
1030 else if (temp & SCSI_RESET_BUS_RESET)
1031 scsi_mark_bus_reset(host, SCpnt->channel);
1033 scsi_mark_device_reset(SCpnt->device);
1034 SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2 | IN_RESET3);
1035 scsi_request_sense(SCpnt);
1037 * If a bus reset was performed, we
1038 * need to wake up each and every command
1039 * that was active on the bus or if it was a HBA
1040 * reset all active commands on all channels
1042 if (temp & SCSI_RESET_HOST_RESET) {
1043 for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
1044 SCpnt1 = SDpnt->device_queue;
1046 if (SCpnt1->request.rq_status != RQ_INACTIVE
1048 scsi_request_sense(SCpnt1);
1049 SCpnt1 = SCpnt1->next;
1052 } else if (temp & SCSI_RESET_BUS_RESET) {
1053 for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
1054 SCpnt1 = SDpnt->device_queue;
1056 if (SCpnt1->request.rq_status != RQ_INACTIVE
1058 && SCpnt1->channel == SCpnt->channel)
1059 scsi_request_sense(SCpnt);
1060 SCpnt1 = SCpnt1->next;
1065 case SCSI_RESET_SNOOZE:
1066 /* In this case, we set the timeout field to 0
1067 * so that this command does not time out any more,
1068 * and we return 1 so that we get a message on the
1071 SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2 | IN_RESET3);
1072 update_timeout(SCpnt, 0);
1073 /* If you snooze, you lose... */
1074 case SCSI_RESET_ERROR:
1085 * The strategy is to cause the timer code to call scsi_times_out()
1086 * when the soonest timeout is pending.
1087 * The arguments are used when we are queueing a new command, because
1088 * we do not want to subtract the time used from this time, but when we
1089 * set the timer, we want to take this value into account.
1092 int update_timeout(Scsi_Cmnd * SCset, int timeout)
1097 * We are using the new error handling code to actually register/deregister
1098 * timers for timeout.
1101 if (!timer_pending(&SCset->eh_timeout)) {
1104 rtn = SCset->eh_timeout.expires - jiffies;
1108 scsi_delete_timer(SCset);
1110 scsi_add_timer(SCset, timeout, scsi_old_times_out);
1118 * This function exports SCSI Bus, Device or Host reset capability
1119 * and is for use with the SCSI generic driver.
1122 scsi_old_reset(Scsi_Cmnd *SCpnt, unsigned int flag)
1124 unsigned int old_flags = SCSI_RESET_SYNCHRONOUS;
1127 case SCSI_TRY_RESET_DEVICE:
1128 /* no suggestion flags to add, device reset is default */
1130 case SCSI_TRY_RESET_BUS:
1131 old_flags |= SCSI_RESET_SUGGEST_BUS_RESET;
1133 case SCSI_TRY_RESET_HOST:
1134 old_flags |= SCSI_RESET_SUGGEST_HOST_RESET;
1140 if (scsi_reset(SCpnt, old_flags))
1146 * Overrides for Emacs so that we follow Linus's tabbing style.
1147 * Emacs will notice this stuff at the end of the file and automatically
1148 * adjust the settings for this buffer only. This must remain at the end
1150 * ---------------------------------------------------------------------------
1153 * c-brace-imaginary-offset: 0
1154 * c-brace-offset: -4
1155 * c-argdecl-indent: 4
1156 * c-label-offset: -4
1157 * c-continued-statement-offset: 4
1158 * c-continued-brace-offset: 0
1159 * indent-tabs-mode: nil