File: /usr/src/linux/drivers/scsi/scsi_obsolete.c

1     /*
2      *  scsi_obsolete.c Copyright (C) 1992 Drew Eckhardt
3      *         Copyright (C) 1993, 1994, 1995 Eric Youngdale
4      *
5      *  generic mid-level SCSI driver
6      *      Initial versions: Drew Eckhardt
7      *      Subsequent revisions: Eric Youngdale
8      *
9      *  <drew@colorado.edu>
10      *
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>
15      *
16      *  Modified by Eric Youngdale eric@andante.org to
17      *  add scatter-gather, multiple outstanding request, and other
18      *  enhancements.
19      *
20      *  Native multichannel, wide scsi, /proc/scsi and hot plugging
21      *  support added by Michael Neuffer <mike@i-connect.net>
22      *
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>
26      *
27      *  Improved compatibility with 2.0 behaviour by Manfred Spraul
28      *  <masp0008@stud.uni-sb.de>
29      */
30     
31     /*
32      *#########################################################################
33      *#########################################################################
34      *#########################################################################
35      *#########################################################################
36      *              NOTE - NOTE - NOTE - NOTE - NOTE - NOTE - NOTE
37      *
38      *#########################################################################
39      *#########################################################################
40      *#########################################################################
41      *#########################################################################
42      *
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.
47      */
48     
49     #define __NO_VERSION__
50     #include <linux/module.h>
51     
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>
62     
63     #include <asm/system.h>
64     #include <asm/irq.h>
65     #include <asm/dma.h>
66     
67     #include "scsi.h"
68     #include "hosts.h"
69     #include "constants.h"
70     
71     #undef USE_STATIC_SCSI_MEMORY
72     
73     /*
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 $";
75      */
76     
77     
78     #define INTERNAL_ERROR (panic ("Internal error in file %s, line %d.\n", __FILE__, __LINE__))
79     
80     
81     static int scsi_abort(Scsi_Cmnd *, int code);
82     static int scsi_reset(Scsi_Cmnd *, unsigned int);
83     
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     
88     extern int scsi_dispatch_cmd(Scsi_Cmnd * SCpnt);
89     
90     #define SCSI_BLOCK(HOST) (HOST->can_queue && HOST->host_busy >= HOST->can_queue)
91     
92     static unsigned char generic_sense[6] =
93     {REQUEST_SENSE, 0, 0, 0, 255, 0};
94     
95     /*
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.
100      *
101      *  ABORT_TIMEOUT and RESET_TIMEOUT are the timeouts for RESET and ABORT
102      *  respectively.
103      */
104     
105     #ifdef DEBUG_TIMEOUT
106     static void scsi_dump_status(void);
107     #endif
108     
109     
110     #ifdef DEBUG
111     #define SCSI_TIMEOUT (5*HZ)
112     #else
113     #define SCSI_TIMEOUT (2*HZ)
114     #endif
115     
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
125     
126     
127     /* Do not call reset on error if we just did a reset within 15 sec. */
128     #define MIN_RESET_PERIOD (15*HZ)
129     
130     
131     
132     /*
133      *  Flag bits for the internal_timeout array
134      */
135     #define IN_ABORT  1
136     #define IN_RESET  2
137     #define IN_RESET2 4
138     #define IN_RESET3 8
139     
140     /*
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.
144      */
145     
146     void scsi_old_times_out(Scsi_Cmnd * SCpnt)
147     {
148     	unsigned long flags;
149     
150     	spin_lock_irqsave(&io_request_lock, flags);
151     
152     	/* Set the serial_number_at_timeout to the current serial_number */
153     	SCpnt->serial_number_at_timeout = SCpnt->serial_number;
154     
155     	switch (SCpnt->internal_timeout & (IN_ABORT | IN_RESET | IN_RESET2 | IN_RESET3)) {
156     	case NORMAL_TIMEOUT:
157     		{
158     #ifdef DEBUG_TIMEOUT
159     			scsi_dump_status();
160     #endif
161     		}
162     
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.
175     		 */
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.
189     		 */
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;
197     
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;
203     
204     	}
205     	spin_unlock_irqrestore(&io_request_lock, flags);
206     
207     }
208     
209     /*
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.
214      */
215     static void scsi_request_sense(Scsi_Cmnd * SCpnt)
216     {
217     	SCpnt->flags |= WAS_SENSE | ASKED_FOR_SENSE;
218     	update_timeout(SCpnt, SENSE_TIMEOUT);
219     
220     
221     	memcpy((void *) SCpnt->cmnd, (void *) generic_sense,
222     	       sizeof(generic_sense));
223     	memset((void *) SCpnt->sense_buffer, 0,
224     	       sizeof(SCpnt->sense_buffer));
225     
226     	if (SCpnt->device->scsi_level <= SCSI_2)
227     		SCpnt->cmnd[1] = SCpnt->lun << 5;
228     	SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);
229     
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     	SCpnt->sc_data_direction = SCSI_DATA_READ;
236     
237             /*
238              * Ugly, ugly.  The newer interfaces all assume that the lock
239              * isn't held.  Mustn't disappoint, or we deadlock the system.
240              */
241             spin_unlock_irq(&io_request_lock);
242     	scsi_dispatch_cmd(SCpnt);
243             spin_lock_irq(&io_request_lock);
244     }
245     
246     
247     
248     
249     static int check_sense(Scsi_Cmnd * SCpnt)
250     {
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
253     	 * be confused.
254     	 */
255     	if (((SCpnt->sense_buffer[0] & 0x70) >> 4) != 7) {
256     		if (!(SCpnt->flags & ASKED_FOR_SENSE))
257     			return SUGGEST_SENSE;
258     		else
259     			return SUGGEST_RETRY;
260     	}
261     	SCpnt->flags &= ~ASKED_FOR_SENSE;
262     
263     #ifdef DEBUG_INIT
264     	printk("scsi%d, channel%d : ", SCpnt->host->host_no, SCpnt->channel);
265     	print_sense("", SCpnt);
266     	printk("\n");
267     #endif
268     	if (SCpnt->sense_buffer[2] & 0xe0)
269     		return SUGGEST_ABORT;
270     
271     	switch (SCpnt->sense_buffer[2] & 0xf) {
272     	case NO_SENSE:
273     		return 0;
274     	case RECOVERED_ERROR:
275     		return SUGGEST_IS_OK;
276     
277     	case ABORTED_COMMAND:
278     		return SUGGEST_RETRY;
279     	case NOT_READY:
280     	case UNIT_ATTENTION:
281     		/*
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.
286     		 */
287     		if (SCpnt->device->expecting_cc_ua) {
288     			SCpnt->device->expecting_cc_ua = 0;
289     			return SUGGEST_RETRY;
290     		}
291     		return SUGGEST_ABORT;
292     
293     		/* these three are not supported */
294     	case COPY_ABORTED:
295     	case VOLUME_OVERFLOW:
296     	case MISCOMPARE:
297     
298     	case MEDIUM_ERROR:
299     		return SUGGEST_REMAP;
300     	case BLANK_CHECK:
301     	case DATA_PROTECT:
302     	case HARDWARE_ERROR:
303     	case ILLEGAL_REQUEST:
304     	default:
305     		return SUGGEST_ABORT;
306     	}
307     }
308     
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:
312      *
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.
325      *
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.
329      */
330     void scsi_old_done(Scsi_Cmnd * SCpnt)
331     {
332     	int status = 0;
333     	int exit = 0;
334     	int checked;
335     	int oldto;
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);
342     
343     #ifdef DEBUG_TIMEOUT
344     	if (result)
345     		printk("Non-zero result in scsi_done %x %d:%d\n",
346     		       result, SCpnt->target, SCpnt->lun);
347     #endif
348     
349     	/* If we requested an abort, (and we got it) then fix up the return
350     	 *  status to say why
351     	 */
352     	if (host_byte(result) == DID_ABORT && SCpnt->abort_reason)
353     		SCpnt->result = result = (result & 0xff00ffff) |
354     		    (SCpnt->abort_reason << 16);
355     
356     
357     #define CMD_FINISHED 0
358     #define MAYREDO  1
359     #define REDO     3
360     #define PENDING  4
361     
362     #ifdef DEBUG
363     	printk("In scsi_done(host = %d, result = %06x)\n", host->host_no, result);
364     #endif
365     
366     	if (SCpnt->flags & SYNC_RESET) {
367     		/*
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.
372     		 */
373     		printk("scsi%d: device driver called scsi_done() "
374     		       "for a synchronous reset.\n", SCpnt->host->host_no);
375     		return;
376     	}
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;
382     	}
383     	switch (host_byte(result)) {
384     	case DID_OK:
385     		if (status_byte(result) && (SCpnt->flags & WAS_SENSE))
386     			/* Failed to obtain sense information */
387     		{
388     			SCpnt->flags &= ~WAS_SENSE;
389     #if 0				/* This cannot possibly be correct. */
390     			SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
391     #endif
392     
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,
397     				       SCpnt->lun);
398     				scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
399     				status = REDO;
400     				break;
401     			} else {
402     				exit = (DRIVER_HARD | SUGGEST_ABORT);
403     				status = CMD_FINISHED;
404     			}
405     		} else
406     			switch (msg_byte(result)) {
407     			case COMMAND_COMPLETE:
408     				switch (status_byte(result)) {
409     				case GOOD:
410     					if (SCpnt->flags & WAS_SENSE) {
411     #ifdef DEBUG
412     						printk("In scsi_done, GOOD status, COMMAND COMPLETE, "
413     						       "parsing sense information.\n");
414     #endif
415     						SCpnt->flags &= ~WAS_SENSE;
416     #if 0				/* This cannot possibly be correct. */
417     						SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
418     #endif
419     
420     						switch (checked = check_sense(SCpnt)) {
421     						case SUGGEST_SENSE:
422     						case 0:
423     #ifdef DEBUG
424     							printk("NO SENSE.  status = REDO\n");
425     #endif
426     							update_timeout(SCpnt, oldto);
427     							status = REDO;
428     							break;
429     						case SUGGEST_IS_OK:
430     							break;
431     						case SUGGEST_REMAP:
432     #ifdef DEBUG
433     							printk("SENSE SUGGEST REMAP - status = CMD_FINISHED\n");
434     #endif
435     							status = CMD_FINISHED;
436     							exit = DRIVER_SENSE | SUGGEST_ABORT;
437     							break;
438     						case SUGGEST_RETRY:
439     #ifdef DEBUG
440     							printk("SENSE SUGGEST RETRY - status = MAYREDO\n");
441     #endif
442     							status = MAYREDO;
443     							exit = DRIVER_SENSE | SUGGEST_RETRY;
444     							break;
445     						case SUGGEST_ABORT:
446     #ifdef DEBUG
447     							printk("SENSE SUGGEST ABORT - status = CMD_FINISHED");
448     #endif
449     							status = CMD_FINISHED;
450     							exit = DRIVER_SENSE | SUGGEST_ABORT;
451     							break;
452     						default:
453     							printk("Internal error %s %d \n", __FILE__,
454     							       __LINE__);
455     						}
456     					}
457     					/* end WAS_SENSE */
458     					else {
459     #ifdef DEBUG
460     						printk("COMMAND COMPLETE message returned, "
461     						       "status = CMD_FINISHED. \n");
462     #endif
463     						exit = DRIVER_OK;
464     						status = CMD_FINISHED;
465     					}
466     					break;
467     
468     				case CHECK_CONDITION:
469     				case COMMAND_TERMINATED:
470     					switch (check_sense(SCpnt)) {
471     					case 0:
472     						update_timeout(SCpnt, oldto);
473     						status = REDO;
474     						break;
475     					case SUGGEST_REMAP:
476     						status = CMD_FINISHED;
477     						exit = DRIVER_SENSE | SUGGEST_ABORT;
478     						break;
479     					case SUGGEST_RETRY:
480     						status = MAYREDO;
481     						exit = DRIVER_SENSE | SUGGEST_RETRY;
482     						break;
483     					case SUGGEST_ABORT:
484     						status = CMD_FINISHED;
485     						exit = DRIVER_SENSE | SUGGEST_ABORT;
486     						break;
487     					case SUGGEST_SENSE:
488     						scsi_request_sense(SCpnt);
489     						status = PENDING;
490     						break;
491     					}
492     					break;
493     
494     				case CONDITION_GOOD:
495     				case INTERMEDIATE_GOOD:
496     				case INTERMEDIATE_C_GOOD:
497     					break;
498     
499     				case BUSY:
500     				case QUEUE_FULL:
501     					update_timeout(SCpnt, oldto);
502     					status = REDO;
503     					break;
504     
505     				case RESERVATION_CONFLICT:
506     					printk("scsi%d, channel %d : RESERVATION CONFLICT performing"
507     					       " reset.\n", SCpnt->host->host_no, SCpnt->channel);
508     					scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
509     					status = REDO;
510     					break;
511     				default:
512     					printk("Internal error %s %d \n"
513     					 "status byte = %d \n", __FILE__,
514     					  __LINE__, status_byte(result));
515     
516     				}
517     				break;
518     			default:
519     				panic("scsi: unsupported message byte %d received\n",
520     				      msg_byte(result));
521     			}
522     		break;
523     	case DID_TIME_OUT:
524     #ifdef DEBUG
525     		printk("Host returned DID_TIME_OUT - ");
526     #endif
527     
528     		if (SCpnt->flags & WAS_TIMEDOUT) {
529     #ifdef DEBUG
530     			printk("Aborting\n");
531     #endif
532     			/*
533     			   Allow TEST_UNIT_READY and INQUIRY commands to timeout early
534     			   without causing resets.  All other commands should be retried.
535     			 */
536     			if (SCpnt->cmnd[0] != TEST_UNIT_READY &&
537     			    SCpnt->cmnd[0] != INQUIRY)
538     				status = MAYREDO;
539     			exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
540     		} else {
541     #ifdef DEBUG
542     			printk("Retrying.\n");
543     #endif
544     			SCpnt->flags |= WAS_TIMEDOUT;
545     			SCpnt->internal_timeout &= ~IN_ABORT;
546     			status = REDO;
547     		}
548     		break;
549     	case DID_BUS_BUSY:
550     	case DID_PARITY:
551     		status = REDO;
552     		break;
553     	case DID_NO_CONNECT:
554     #ifdef DEBUG
555     		printk("Couldn't connect.\n");
556     #endif
557     		exit = (DRIVER_HARD | SUGGEST_ABORT);
558     		break;
559     	case DID_ERROR:
560     		status = MAYREDO;
561     		exit = (DRIVER_HARD | SUGGEST_ABORT);
562     		break;
563     	case DID_BAD_TARGET:
564     	case DID_ABORT:
565     		exit = (DRIVER_INVALID | SUGGEST_ABORT);
566     		break;
567     	case DID_RESET:
568     		if (SCpnt->flags & IS_RESETTING) {
569     			SCpnt->flags &= ~IS_RESETTING;
570     			status = REDO;
571     			break;
572     		}
573     		if (msg_byte(result) == GOOD &&
574     		    status_byte(result) == CHECK_CONDITION) {
575     			switch (check_sense(SCpnt)) {
576     			case 0:
577     				update_timeout(SCpnt, oldto);
578     				status = REDO;
579     				break;
580     			case SUGGEST_REMAP:
581     			case SUGGEST_RETRY:
582     				status = MAYREDO;
583     				exit = DRIVER_SENSE | SUGGEST_RETRY;
584     				break;
585     			case SUGGEST_ABORT:
586     				status = CMD_FINISHED;
587     				exit = DRIVER_SENSE | SUGGEST_ABORT;
588     				break;
589     			case SUGGEST_SENSE:
590     				scsi_request_sense(SCpnt);
591     				status = PENDING;
592     				break;
593     			}
594     		} else {
595     			status = REDO;
596     			exit = SUGGEST_RETRY;
597     		}
598     		break;
599     	default:
600     		exit = (DRIVER_ERROR | SUGGEST_DIE);
601     	}
602     
603     	switch (status) {
604     	case CMD_FINISHED:
605     	case PENDING:
606     		break;
607     	case MAYREDO:
608     #ifdef DEBUG
609     		printk("In MAYREDO, allowing %d retries, have %d\n",
610     		       SCpnt->allowed, SCpnt->retries);
611     #endif
612     		if ((++SCpnt->retries) < SCpnt->allowed) {
613     			if ((SCpnt->retries >= (SCpnt->allowed >> 1))
614     			    && !(SCpnt->host->resetting && time_before(jiffies, SCpnt->host->last_reset + MIN_RESET_PERIOD))
615     			    && !(SCpnt->flags & WAS_RESET)) {
616     				printk("scsi%d channel %d : resetting for second half of retries.\n",
617     				   SCpnt->host->host_no, SCpnt->channel);
618     				scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
619     				/* fall through to REDO */
620     			}
621     		} else {
622     			status = CMD_FINISHED;
623     			break;
624     		}
625     		/* fall through to REDO */
626     
627     	case REDO:
628     
629     		if (SCpnt->flags & WAS_SENSE)
630     			scsi_request_sense(SCpnt);
631     		else {
632     			memcpy((void *) SCpnt->cmnd,
633     			       (void *) SCpnt->data_cmnd,
634     			       sizeof(SCpnt->data_cmnd));
635     			memset((void *) SCpnt->sense_buffer, 0,
636     			       sizeof(SCpnt->sense_buffer));
637     			SCpnt->request_buffer = SCpnt->buffer;
638     			SCpnt->request_bufflen = SCpnt->bufflen;
639     			SCpnt->use_sg = SCpnt->old_use_sg;
640     			SCpnt->cmd_len = SCpnt->old_cmd_len;
641     			SCpnt->sc_data_direction = SCpnt->sc_old_data_direction;
642     			SCpnt->underflow = SCpnt->old_underflow;
643     			SCpnt->result = 0;
644                             /*
645                              * Ugly, ugly.  The newer interfaces all
646                              * assume that the lock isn't held.  Mustn't
647                              * disappoint, or we deadlock the system.  
648                              */
649                             spin_unlock_irq(&io_request_lock);
650     			scsi_dispatch_cmd(SCpnt);
651                             spin_lock_irq(&io_request_lock);
652     		}
653     		break;
654     	default:
655     		INTERNAL_ERROR;
656     	}
657     
658     	if (status == CMD_FINISHED) {
659     		Scsi_Request *SRpnt;
660     #ifdef DEBUG
661     		printk("Calling done function - at address %p\n", SCpnt->done);
662     #endif
663     		host->host_busy--;	/* Indicate that we are free */
664                     device->device_busy--;	/* Decrement device usage counter. */
665     
666     		SCpnt->result = result | ((exit & 0xff) << 24);
667     		SCpnt->use_sg = SCpnt->old_use_sg;
668     		SCpnt->cmd_len = SCpnt->old_cmd_len;
669     		SCpnt->sc_data_direction = SCpnt->sc_old_data_direction;
670     		SCpnt->underflow = SCpnt->old_underflow;
671                     /*
672                      * The upper layers assume the lock isn't held.  We mustn't
673                      * disappoint them.  When the new error handling code is in
674                      * use, the upper code is run from a bottom half handler, so
675                      * it isn't an issue.
676                      */
677                     spin_unlock_irq(&io_request_lock);
678     		SRpnt = SCpnt->sc_request;
679     		if( SRpnt != NULL ) {
680     			SRpnt->sr_result = SRpnt->sr_command->result;
681     			if( SRpnt->sr_result != 0 ) {
682     				memcpy(SRpnt->sr_sense_buffer,
683     				       SRpnt->sr_command->sense_buffer,
684     				       sizeof(SRpnt->sr_sense_buffer));
685     			}
686     		}
687     
688     		SCpnt->done(SCpnt);
689                     spin_lock_irq(&io_request_lock);
690     	}
691     #undef CMD_FINISHED
692     #undef REDO
693     #undef MAYREDO
694     #undef PENDING
695     }
696     
697     /*
698      * The scsi_abort function interfaces with the abort() function of the host
699      * we are aborting, and causes the current command to not complete.  The
700      * caller should deal with any error messages or status returned on the
701      * next call.
702      *
703      * This will not be called reentrantly for a given host.
704      */
705     
706     /*
707      * Since we're nice guys and specified that abort() and reset()
708      * can be non-reentrant.  The internal_timeout flags are used for
709      * this.
710      */
711     
712     
713     static int scsi_abort(Scsi_Cmnd * SCpnt, int why)
714     {
715     	int oldto;
716     	struct Scsi_Host *host = SCpnt->host;
717     
718     	while (1) {
719     
720     		/*
721     		 * Protect against races here.  If the command is done, or we are
722     		 * on a different command forget it.
723     		 */
724     		if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
725     			return 0;
726     		}
727     		if (SCpnt->internal_timeout & IN_ABORT) {
728     			spin_unlock_irq(&io_request_lock);
729     			while (SCpnt->internal_timeout & IN_ABORT)
730     				barrier();
731     			spin_lock_irq(&io_request_lock);
732     		} else {
733     			SCpnt->internal_timeout |= IN_ABORT;
734     			oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
735     
736     			if ((SCpnt->flags & IS_RESETTING) && SCpnt->device->soft_reset) {
737     				/* OK, this command must have died when we did the
738     				 *  reset.  The device itself must have lied.
739     				 */
740     				printk("Stale command on %d %d:%d appears to have died when"
741     				       " the bus was reset\n",
742     				       SCpnt->channel, SCpnt->target, SCpnt->lun);
743     			}
744     			if (!host->host_busy) {
745     				SCpnt->internal_timeout &= ~IN_ABORT;
746     				update_timeout(SCpnt, oldto);
747     				return 0;
748     			}
749     			printk("scsi : aborting command due to timeout : pid %lu, scsi%d,"
750     			       " channel %d, id %d, lun %d ",
751     			       SCpnt->pid, SCpnt->host->host_no, (int) SCpnt->channel,
752     			       (int) SCpnt->target, (int) SCpnt->lun);
753     			print_command(SCpnt->cmnd);
754     			if (SCpnt->serial_number != SCpnt->serial_number_at_timeout)
755     				return 0;
756     			SCpnt->abort_reason = why;
757     			switch (host->hostt->abort(SCpnt)) {
758     				/* We do not know how to abort.  Try waiting another
759     				 * time increment and see if this helps. Set the
760     				 * WAS_TIMEDOUT flag set so we do not try this twice
761     				 */
762     			case SCSI_ABORT_BUSY:	/* Tough call - returning 1 from
763     						 * this is too severe
764     						 */
765     			case SCSI_ABORT_SNOOZE:
766     				if (why == DID_TIME_OUT) {
767     					SCpnt->internal_timeout &= ~IN_ABORT;
768     					if (SCpnt->flags & WAS_TIMEDOUT) {
769     						return 1;	/* Indicate we cannot handle this.
770     								 * We drop down into the reset handler
771     								 * and try again
772     								 */
773     					} else {
774     						SCpnt->flags |= WAS_TIMEDOUT;
775     						oldto = SCpnt->timeout_per_command;
776     						update_timeout(SCpnt, oldto);
777     					}
778     				}
779     				return 0;
780     			case SCSI_ABORT_PENDING:
781     				if (why != DID_TIME_OUT) {
782     					update_timeout(SCpnt, oldto);
783     				}
784     				return 0;
785     			case SCSI_ABORT_SUCCESS:
786     				/* We should have already aborted this one.  No
787     				 * need to adjust timeout
788     				 */
789     				SCpnt->internal_timeout &= ~IN_ABORT;
790     				return 0;
791     			case SCSI_ABORT_NOT_RUNNING:
792     				SCpnt->internal_timeout &= ~IN_ABORT;
793     				update_timeout(SCpnt, 0);
794     				return 0;
795     			case SCSI_ABORT_ERROR:
796     			default:
797     				SCpnt->internal_timeout &= ~IN_ABORT;
798     				return 1;
799     			}
800     		}
801     	}
802     }
803     
804     
805     /* Mark a single SCSI Device as having been reset. */
806     
807     static inline void scsi_mark_device_reset(Scsi_Device * Device)
808     {
809     	Device->was_reset = 1;
810     	Device->expecting_cc_ua = 1;
811     }
812     
813     
814     /* Mark all SCSI Devices on a specific Host as having been reset. */
815     
816     void scsi_mark_host_reset(struct Scsi_Host *Host)
817     {
818     	Scsi_Cmnd *SCpnt;
819     	Scsi_Device *SDpnt;
820     
821     	for (SDpnt = Host->host_queue; SDpnt; SDpnt = SDpnt->next) {
822     		for (SCpnt = SDpnt->device_queue; SCpnt; SCpnt = SCpnt->next)
823     			scsi_mark_device_reset(SCpnt->device);
824     	}
825     }
826     
827     
828     /* Mark all SCSI Devices on a specific Host Bus as having been reset. */
829     
830     static void scsi_mark_bus_reset(struct Scsi_Host *Host, int channel)
831     {
832     	Scsi_Cmnd *SCpnt;
833     	Scsi_Device *SDpnt;
834     
835     	for (SDpnt = Host->host_queue; SDpnt; SDpnt = SDpnt->next) {
836     		for (SCpnt = SDpnt->device_queue; SCpnt; SCpnt = SCpnt->next)
837     			if (SCpnt->channel == channel)
838     				scsi_mark_device_reset(SCpnt->device);
839     	}
840     }
841     
842     
843     static int scsi_reset(Scsi_Cmnd * SCpnt, unsigned int reset_flags)
844     {
845     	int temp;
846     	Scsi_Cmnd *SCpnt1;
847     	Scsi_Device *SDpnt;
848     	struct Scsi_Host *host = SCpnt->host;
849     
850     	printk("SCSI bus is being reset for host %d channel %d.\n",
851     	       host->host_no, SCpnt->channel);
852     
853     #if 0
854     	/*
855     	 * First of all, we need to make a recommendation to the low-level
856     	 * driver as to whether a BUS_DEVICE_RESET should be performed,
857     	 * or whether we should do a full BUS_RESET.  There is no simple
858     	 * algorithm here - we basically use a series of heuristics
859     	 * to determine what we should do.
860     	 */
861     	SCpnt->host->suggest_bus_reset = FALSE;
862     
863     	/*
864     	 * First see if all of the active devices on the bus have
865     	 * been jammed up so that we are attempting resets.  If so,
866     	 * then suggest a bus reset.  Forcing a bus reset could
867     	 * result in some race conditions, but no more than
868     	 * you would usually get with timeouts.  We will cross
869     	 * that bridge when we come to it.
870     	 *
871     	 * This is actually a pretty bad idea, since a sequence of
872     	 * commands will often timeout together and this will cause a
873     	 * Bus Device Reset followed immediately by a SCSI Bus Reset.
874     	 * If all of the active devices really are jammed up, the
875     	 * Bus Device Reset will quickly timeout and scsi_times_out
876     	 * will follow up with a SCSI Bus Reset anyway.
877     	 */
878     	SCpnt1 = host->host_queue;
879     	while (SCpnt1) {
880     		if (SCpnt1->request.rq_status != RQ_INACTIVE
881     		    && (SCpnt1->flags & (WAS_RESET | IS_RESETTING)) == 0)
882     			break;
883     		SCpnt1 = SCpnt1->next;
884     	}
885     	if (SCpnt1 == NULL) {
886     		reset_flags |= SCSI_RESET_SUGGEST_BUS_RESET;
887     	}
888     	/*
889     	 * If the code that called us is suggesting a hard reset, then
890     	 * definitely request it.  This usually occurs because a
891     	 * BUS_DEVICE_RESET times out.
892     	 *
893     	 * Passing reset_flags along takes care of this automatically.
894     	 */
895     	if (reset_flags & SCSI_RESET_SUGGEST_BUS_RESET) {
896     		SCpnt->host->suggest_bus_reset = TRUE;
897     	}
898     #endif
899     
900     	while (1) {
901     
902     		/*
903     		 * Protect against races here.  If the command is done, or we are
904     		 * on a different command forget it.
905     		 */
906     		if (reset_flags & SCSI_RESET_ASYNCHRONOUS)
907     			if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
908     				return 0;
909     			}
910     		if (SCpnt->internal_timeout & IN_RESET) {
911     			spin_unlock_irq(&io_request_lock);
912     			while (SCpnt->internal_timeout & IN_RESET)
913     				barrier();
914     			spin_lock_irq(&io_request_lock);
915     		} else {
916     			SCpnt->internal_timeout |= IN_RESET;
917     			update_timeout(SCpnt, RESET_TIMEOUT);
918     
919     			if (reset_flags & SCSI_RESET_SYNCHRONOUS)
920     				SCpnt->flags |= SYNC_RESET;
921     			if (host->host_busy) {
922     				for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
923     					SCpnt1 = SDpnt->device_queue;
924     					while (SCpnt1) {
925     						if (SCpnt1->request.rq_status != RQ_INACTIVE) {
926     #if 0
927     							if (!(SCpnt1->flags & IS_RESETTING) &&
928     							    !(SCpnt1->internal_timeout & IN_ABORT))
929     								scsi_abort(SCpnt1, DID_RESET);
930     #endif
931     							SCpnt1->flags |= (WAS_RESET | IS_RESETTING);
932     						}
933     						SCpnt1 = SCpnt1->next;
934     					}
935     				}
936     
937     				host->last_reset = jiffies;
938     				host->resetting = 1;
939     				/*
940     				 * I suppose that the host reset callback will not play
941     				 * with the resetting field. We have just set the resetting
942     				 * flag here. -arca
943     				 */
944     				temp = host->hostt->reset(SCpnt, reset_flags);
945     				/*
946     				   This test allows the driver to introduce an additional bus
947     				   settle time delay by setting last_reset up to 20 seconds in
948     				   the future.  In the normal case where the driver does not
949     				   modify last_reset, it must be assumed that the actual bus
950     				   reset occurred immediately prior to the return to this code,
951     				   and so last_reset must be updated to the current time, so
952     				   that the delay in internal_cmnd will guarantee at least a
953     				   MIN_RESET_DELAY bus settle time.
954     				 */
955     				if (host->last_reset - jiffies > 20UL * HZ)
956     					host->last_reset = jiffies;
957     			} else {
958     				host->host_busy++;
959     				host->last_reset = jiffies;
960     				host->resetting = 1;
961     				SCpnt->flags |= (WAS_RESET | IS_RESETTING);
962     				/*
963     				 * I suppose that the host reset callback will not play
964     				 * with the resetting field. We have just set the resetting
965     				 * flag here. -arca
966     				 */
967     				temp = host->hostt->reset(SCpnt, reset_flags);
968     				if (time_before(host->last_reset, jiffies) ||
969     				    (time_after(host->last_reset, jiffies + 20 * HZ)))
970     					host->last_reset = jiffies;
971     				host->host_busy--;
972     			}
973     			if (reset_flags & SCSI_RESET_SYNCHRONOUS)
974     				SCpnt->flags &= ~SYNC_RESET;
975     
976     #ifdef DEBUG
977     			printk("scsi reset function returned %d\n", temp);
978     #endif
979     
980     			/*
981     			 * Now figure out what we need to do, based upon
982     			 * what the low level driver said that it did.
983     			 * If the result is SCSI_RESET_SUCCESS, SCSI_RESET_PENDING,
984     			 * or SCSI_RESET_WAKEUP, then the low level driver did a
985     			 * bus device reset or bus reset, so we should go through
986     			 * and mark one or all of the devices on that bus
987     			 * as having been reset.
988     			 */
989     			switch (temp & SCSI_RESET_ACTION) {
990     			case SCSI_RESET_SUCCESS:
991     				if (temp & SCSI_RESET_HOST_RESET)
992     					scsi_mark_host_reset(host);
993     				else if (temp & SCSI_RESET_BUS_RESET)
994     					scsi_mark_bus_reset(host, SCpnt->channel);
995     				else
996     					scsi_mark_device_reset(SCpnt->device);
997     				SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2 | IN_RESET3);
998     				return 0;
999     			case SCSI_RESET_PENDING:
1000     				if (temp & SCSI_RESET_HOST_RESET)
1001     					scsi_mark_host_reset(host);
1002     				else if (temp & SCSI_RESET_BUS_RESET)
1003     					scsi_mark_bus_reset(host, SCpnt->channel);
1004     				else
1005     					scsi_mark_device_reset(SCpnt->device);
1006     			case SCSI_RESET_NOT_RUNNING:
1007     				return 0;
1008     			case SCSI_RESET_PUNT:
1009     				SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2 | IN_RESET3);
1010     				scsi_request_sense(SCpnt);
1011     				return 0;
1012     			case SCSI_RESET_WAKEUP:
1013     				if (temp & SCSI_RESET_HOST_RESET)
1014     					scsi_mark_host_reset(host);
1015     				else if (temp & SCSI_RESET_BUS_RESET)
1016     					scsi_mark_bus_reset(host, SCpnt->channel);
1017     				else
1018     					scsi_mark_device_reset(SCpnt->device);
1019     				SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2 | IN_RESET3);
1020     				scsi_request_sense(SCpnt);
1021     				/*
1022     				 * If a bus reset was performed, we
1023     				 * need to wake up each and every command
1024     				 * that was active on the bus or if it was a HBA
1025     				 * reset all active commands on all channels
1026     				 */
1027     				if (temp & SCSI_RESET_HOST_RESET) {
1028     					for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
1029     						SCpnt1 = SDpnt->device_queue;
1030     						while (SCpnt1) {
1031     							if (SCpnt1->request.rq_status != RQ_INACTIVE
1032     							    && SCpnt1 != SCpnt)
1033     								scsi_request_sense(SCpnt1);
1034     							SCpnt1 = SCpnt1->next;
1035     						}
1036     					}
1037     				} else if (temp & SCSI_RESET_BUS_RESET) {
1038     					for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
1039     						SCpnt1 = SDpnt->device_queue;
1040     						while (SCpnt1) {
1041     							if (SCpnt1->request.rq_status != RQ_INACTIVE
1042     							&& SCpnt1 != SCpnt
1043     							    && SCpnt1->channel == SCpnt->channel)
1044     								scsi_request_sense(SCpnt);
1045     							SCpnt1 = SCpnt1->next;
1046     						}
1047     					}
1048     				}
1049     				return 0;
1050     			case SCSI_RESET_SNOOZE:
1051     				/* In this case, we set the timeout field to 0
1052     				 * so that this command does not time out any more,
1053     				 * and we return 1 so that we get a message on the
1054     				 * screen.
1055     				 */
1056     				SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2 | IN_RESET3);
1057     				update_timeout(SCpnt, 0);
1058     				/* If you snooze, you lose... */
1059     			case SCSI_RESET_ERROR:
1060     			default:
1061     				return 1;
1062     			}
1063     
1064     			return temp;
1065     		}
1066     	}
1067     }
1068     
1069     /*
1070      * The strategy is to cause the timer code to call scsi_times_out()
1071      * when the soonest timeout is pending.
1072      * The arguments are used when we are queueing a new command, because
1073      * we do not want to subtract the time used from this time, but when we
1074      * set the timer, we want to take this value into account.
1075      */
1076     
1077     int update_timeout(Scsi_Cmnd * SCset, int timeout)
1078     {
1079     	int rtn;
1080     
1081     	/*
1082     	 * We are using the new error handling code to actually register/deregister
1083     	 * timers for timeout.
1084     	 */
1085     
1086     	if (!timer_pending(&SCset->eh_timeout)) {
1087     		rtn = 0;
1088     	} else {
1089     		rtn = SCset->eh_timeout.expires - jiffies;
1090     	}
1091     
1092     	if (timeout == 0) {
1093     		scsi_delete_timer(SCset);
1094     	} else {
1095     		scsi_add_timer(SCset, timeout, scsi_old_times_out);
1096     	}
1097     
1098     	return rtn;
1099     }
1100     
1101     
1102     /*
1103      * Overrides for Emacs so that we follow Linus's tabbing style.
1104      * Emacs will notice this stuff at the end of the file and automatically
1105      * adjust the settings for this buffer only.  This must remain at the end
1106      * of the file.
1107      * ---------------------------------------------------------------------------
1108      * Local variables:
1109      * c-indent-level: 4
1110      * c-brace-imaginary-offset: 0
1111      * c-brace-offset: -4
1112      * c-argdecl-indent: 4
1113      * c-label-offset: -4
1114      * c-continued-statement-offset: 4
1115      * c-continued-brace-offset: 0
1116      * indent-tabs-mode: nil
1117      * tab-width: 8
1118      * End:
1119      */
1120