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 
scsi_old_times_out(Scsi_Cmnd * SCpnt)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  */
scsi_request_sense(Scsi_Cmnd * SCpnt)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 
check_sense(Scsi_Cmnd * SCpnt)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  */
scsi_old_done(Scsi_Cmnd * SCpnt)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 					/*
507 					 * Most HAs will return an error for
508 					 * this, so usually reservation
509 					 * conflicts will  be processed under
510 					 * DID_ERROR code
511 					 */
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 */
516 					break;
517 
518 				default:
519 					printk("Internal error %s %d \n"
520 					 "status byte = %d \n", __FILE__,
521 					  __LINE__, status_byte(result));
522 
523 				}
524 				break;
525 			default:
526 				panic("scsi: unsupported message byte %d received\n",
527 				      msg_byte(result));
528 			}
529 		break;
530 	case DID_TIME_OUT:
531 #ifdef DEBUG
532 		printk("Host returned DID_TIME_OUT - ");
533 #endif
534 
535 		if (SCpnt->flags & WAS_TIMEDOUT) {
536 #ifdef DEBUG
537 			printk("Aborting\n");
538 #endif
539 			/*
540 			   Allow TEST_UNIT_READY and INQUIRY commands to timeout early
541 			   without causing resets.  All other commands should be retried.
542 			 */
543 			if (SCpnt->cmnd[0] != TEST_UNIT_READY &&
544 			    SCpnt->cmnd[0] != INQUIRY)
545 				status = MAYREDO;
546 			exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
547 		} else {
548 #ifdef DEBUG
549 			printk("Retrying.\n");
550 #endif
551 			SCpnt->flags |= WAS_TIMEDOUT;
552 			SCpnt->internal_timeout &= ~IN_ABORT;
553 			status = REDO;
554 		}
555 		break;
556 	case DID_BUS_BUSY:
557 	case DID_PARITY:
558 		status = REDO;
559 		break;
560 	case DID_NO_CONNECT:
561 #ifdef DEBUG
562 		printk("Couldn't connect.\n");
563 #endif
564 		exit = (DRIVER_HARD | SUGGEST_ABORT);
565 		break;
566 	case DID_ERROR:
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 */
573 			break;
574 		}
575 		status = MAYREDO;
576 		exit = (DRIVER_HARD | SUGGEST_ABORT);
577 		break;
578 	case DID_BAD_TARGET:
579 	case DID_ABORT:
580 		exit = (DRIVER_INVALID | SUGGEST_ABORT);
581 		break;
582 	case DID_RESET:
583 		if (SCpnt->flags & IS_RESETTING) {
584 			SCpnt->flags &= ~IS_RESETTING;
585 			status = REDO;
586 			break;
587 		}
588 		if (msg_byte(result) == GOOD &&
589 		    status_byte(result) == CHECK_CONDITION) {
590 			switch (check_sense(SCpnt)) {
591 			case 0:
592 				update_timeout(SCpnt, oldto);
593 				status = REDO;
594 				break;
595 			case SUGGEST_REMAP:
596 			case SUGGEST_RETRY:
597 				status = MAYREDO;
598 				exit = DRIVER_SENSE | SUGGEST_RETRY;
599 				break;
600 			case SUGGEST_ABORT:
601 				status = CMD_FINISHED;
602 				exit = DRIVER_SENSE | SUGGEST_ABORT;
603 				break;
604 			case SUGGEST_SENSE:
605 				scsi_request_sense(SCpnt);
606 				status = PENDING;
607 				break;
608 			}
609 		} else {
610 			status = REDO;
611 			exit = SUGGEST_RETRY;
612 		}
613 		break;
614 	default:
615 		exit = (DRIVER_ERROR | SUGGEST_DIE);
616 	}
617 
618 	switch (status) {
619 	case CMD_FINISHED:
620 	case PENDING:
621 		break;
622 	case MAYREDO:
623 #ifdef DEBUG
624 		printk("In MAYREDO, allowing %d retries, have %d\n",
625 		       SCpnt->allowed, SCpnt->retries);
626 #endif
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 */
635 			}
636 		} else {
637 			status = CMD_FINISHED;
638 			break;
639 		}
640 		/* fall through to REDO */
641 
642 	case REDO:
643 
644 		if (SCpnt->flags & WAS_SENSE)
645 			scsi_request_sense(SCpnt);
646 		else {
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;
658 			SCpnt->result = 0;
659                         /*
660                          * Ugly, ugly.  The newer interfaces all
661                          * assume that the lock isn't held.  Mustn't
662                          * disappoint, or we deadlock the system.
663                          */
664                         spin_unlock_irq(&io_request_lock);
665 			scsi_dispatch_cmd(SCpnt);
666                         spin_lock_irq(&io_request_lock);
667 		}
668 		break;
669 	default:
670 		INTERNAL_ERROR;
671 	}
672 
673 	if (status == CMD_FINISHED) {
674 		Scsi_Request *SRpnt;
675 #ifdef DEBUG
676 		printk("Calling done function - at address %p\n", SCpnt->done);
677 #endif
678 		host->host_busy--;	/* Indicate that we are free */
679                 device->device_busy--;	/* Decrement device usage counter. */
680 
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;
686                 /*
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
690                  * it isn't an issue.
691                  */
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));
700 			}
701 		}
702 
703 		SCpnt->done(SCpnt);
704                 spin_lock_irq(&io_request_lock);
705 	}
706 #undef CMD_FINISHED
707 #undef REDO
708 #undef MAYREDO
709 #undef PENDING
710 }
711 
712 /*
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
716  * next call.
717  *
718  * This will not be called reentrantly for a given host.
719  */
720 
721 /*
722  * Since we're nice guys and specified that abort() and reset()
723  * can be non-reentrant.  The internal_timeout flags are used for
724  * this.
725  */
726 
727 
scsi_abort(Scsi_Cmnd * SCpnt,int why)728 static int scsi_abort(Scsi_Cmnd * SCpnt, int why)
729 {
730 	int oldto;
731 	struct Scsi_Host *host = SCpnt->host;
732 
733 	while (1) {
734 
735 		/*
736 		 * Protect against races here.  If the command is done, or we are
737 		 * on a different command forget it.
738 		 */
739 		if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
740 			return 0;
741 		}
742 		if (SCpnt->internal_timeout & IN_ABORT) {
743 			spin_unlock_irq(&io_request_lock);
744 			while (SCpnt->internal_timeout & IN_ABORT)
745 				barrier();
746 			spin_lock_irq(&io_request_lock);
747 		} else {
748 			SCpnt->internal_timeout |= IN_ABORT;
749 			oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
750 
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.
754 				 */
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);
758 			}
759 			if (!host->host_busy) {
760 				SCpnt->internal_timeout &= ~IN_ABORT;
761 				update_timeout(SCpnt, oldto);
762 				return 0;
763 			}
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)
770 				return 0;
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
776 				 */
777 			case SCSI_ABORT_BUSY:	/* Tough call - returning 1 from
778 						 * this is too severe
779 						 */
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
786 								 * and try again
787 								 */
788 					} else {
789 						SCpnt->flags |= WAS_TIMEDOUT;
790 						oldto = SCpnt->timeout_per_command;
791 						update_timeout(SCpnt, oldto);
792 					}
793 				}
794 				return 0;
795 			case SCSI_ABORT_PENDING:
796 				if (why != DID_TIME_OUT) {
797 					update_timeout(SCpnt, oldto);
798 				}
799 				return 0;
800 			case SCSI_ABORT_SUCCESS:
801 				/* We should have already aborted this one.  No
802 				 * need to adjust timeout
803 				 */
804 				SCpnt->internal_timeout &= ~IN_ABORT;
805 				return 0;
806 			case SCSI_ABORT_NOT_RUNNING:
807 				SCpnt->internal_timeout &= ~IN_ABORT;
808 				update_timeout(SCpnt, 0);
809 				return 0;
810 			case SCSI_ABORT_ERROR:
811 			default:
812 				SCpnt->internal_timeout &= ~IN_ABORT;
813 				return 1;
814 			}
815 		}
816 	}
817 }
818 
819 
820 /* Mark a single SCSI Device as having been reset. */
821 
scsi_mark_device_reset(Scsi_Device * Device)822 static inline void scsi_mark_device_reset(Scsi_Device * Device)
823 {
824 	Device->was_reset = 1;
825 	Device->expecting_cc_ua = 1;
826 }
827 
828 
829 /* Mark all SCSI Devices on a specific Host as having been reset. */
830 
scsi_mark_host_reset(struct Scsi_Host * Host)831 void scsi_mark_host_reset(struct Scsi_Host *Host)
832 {
833 	Scsi_Cmnd *SCpnt;
834 	Scsi_Device *SDpnt;
835 
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);
839 	}
840 }
841 
842 
843 /* Mark all SCSI Devices on a specific Host Bus as having been reset. */
844 
scsi_mark_bus_reset(struct Scsi_Host * Host,int channel)845 static void scsi_mark_bus_reset(struct Scsi_Host *Host, int channel)
846 {
847 	Scsi_Cmnd *SCpnt;
848 	Scsi_Device *SDpnt;
849 
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);
854 	}
855 }
856 
857 
scsi_reset(Scsi_Cmnd * SCpnt,unsigned int reset_flags)858 static int scsi_reset(Scsi_Cmnd * SCpnt, unsigned int reset_flags)
859 {
860 	int temp;
861 	Scsi_Cmnd *SCpnt1;
862 	Scsi_Device *SDpnt;
863 	struct Scsi_Host *host = SCpnt->host;
864 
865 	printk("SCSI bus is being reset for host %d channel %d.\n",
866 	       host->host_no, SCpnt->channel);
867 
868 #if 0
869 	/*
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.
875 	 */
876 	SCpnt->host->suggest_bus_reset = FALSE;
877 
878 	/*
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.
885 	 *
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.
892 	 */
893 	SCpnt1 = host->host_queue;
894 	while (SCpnt1) {
895 		if (SCpnt1->request.rq_status != RQ_INACTIVE
896 		    && (SCpnt1->flags & (WAS_RESET | IS_RESETTING)) == 0)
897 			break;
898 		SCpnt1 = SCpnt1->next;
899 	}
900 	if (SCpnt1 == NULL) {
901 		reset_flags |= SCSI_RESET_SUGGEST_BUS_RESET;
902 	}
903 	/*
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.
907 	 *
908 	 * Passing reset_flags along takes care of this automatically.
909 	 */
910 	if (reset_flags & SCSI_RESET_SUGGEST_BUS_RESET) {
911 		SCpnt->host->suggest_bus_reset = TRUE;
912 	}
913 #endif
914 
915 	while (1) {
916 
917 		/*
918 		 * Protect against races here.  If the command is done, or we are
919 		 * on a different command forget it.
920 		 */
921 		if (reset_flags & SCSI_RESET_ASYNCHRONOUS)
922 			if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
923 				return 0;
924 			}
925 		if (SCpnt->internal_timeout & IN_RESET) {
926 			spin_unlock_irq(&io_request_lock);
927 			while (SCpnt->internal_timeout & IN_RESET)
928 				barrier();
929 			spin_lock_irq(&io_request_lock);
930 		} else {
931 			SCpnt->internal_timeout |= IN_RESET;
932 			update_timeout(SCpnt, RESET_TIMEOUT);
933 
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;
939 					while (SCpnt1) {
940 						if (SCpnt1->request.rq_status != RQ_INACTIVE) {
941 #if 0
942 							if (!(SCpnt1->flags & IS_RESETTING) &&
943 							    !(SCpnt1->internal_timeout & IN_ABORT))
944 								scsi_abort(SCpnt1, DID_RESET);
945 #endif
946 							SCpnt1->flags |= (WAS_RESET | IS_RESETTING);
947 						}
948 						SCpnt1 = SCpnt1->next;
949 					}
950 				}
951 
952 				host->last_reset = jiffies;
953 				host->resetting = 1;
954 				/*
955 				 * I suppose that the host reset callback will not play
956 				 * with the resetting field. We have just set the resetting
957 				 * flag here. -arca
958 				 */
959 				temp = host->hostt->reset(SCpnt, reset_flags);
960 				/*
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.
969 				 */
970 				if (host->last_reset - jiffies > 20UL * HZ)
971 					host->last_reset = jiffies;
972 			} else {
973 				host->host_busy++;
974 				host->last_reset = jiffies;
975 				host->resetting = 1;
976 				SCpnt->flags |= (WAS_RESET | IS_RESETTING);
977 				/*
978 				 * I suppose that the host reset callback will not play
979 				 * with the resetting field. We have just set the resetting
980 				 * flag here. -arca
981 				 */
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;
986 				host->host_busy--;
987 			}
988 			if (reset_flags & SCSI_RESET_SYNCHRONOUS)
989 				SCpnt->flags &= ~SYNC_RESET;
990 
991 #ifdef DEBUG
992 			printk("scsi reset function returned %d\n", temp);
993 #endif
994 
995 			/*
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.
1003 			 */
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);
1010 				else
1011 					scsi_mark_device_reset(SCpnt->device);
1012 				SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2 | IN_RESET3);
1013 				return 0;
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);
1019 				else
1020 					scsi_mark_device_reset(SCpnt->device);
1021 			case SCSI_RESET_NOT_RUNNING:
1022 				return 0;
1023 			case SCSI_RESET_PUNT:
1024 				SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2 | IN_RESET3);
1025 				scsi_request_sense(SCpnt);
1026 				return 0;
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);
1032 				else
1033 					scsi_mark_device_reset(SCpnt->device);
1034 				SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2 | IN_RESET3);
1035 				scsi_request_sense(SCpnt);
1036 				/*
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
1041 				 */
1042 				if (temp & SCSI_RESET_HOST_RESET) {
1043 					for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
1044 						SCpnt1 = SDpnt->device_queue;
1045 						while (SCpnt1) {
1046 							if (SCpnt1->request.rq_status != RQ_INACTIVE
1047 							    && SCpnt1 != SCpnt)
1048 								scsi_request_sense(SCpnt1);
1049 							SCpnt1 = SCpnt1->next;
1050 						}
1051 					}
1052 				} else if (temp & SCSI_RESET_BUS_RESET) {
1053 					for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
1054 						SCpnt1 = SDpnt->device_queue;
1055 						while (SCpnt1) {
1056 							if (SCpnt1->request.rq_status != RQ_INACTIVE
1057 							&& SCpnt1 != SCpnt
1058 							    && SCpnt1->channel == SCpnt->channel)
1059 								scsi_request_sense(SCpnt);
1060 							SCpnt1 = SCpnt1->next;
1061 						}
1062 					}
1063 				}
1064 				return 0;
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
1069 				 * screen.
1070 				 */
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:
1075 			default:
1076 				return 1;
1077 			}
1078 
1079 			return temp;
1080 		}
1081 	}
1082 }
1083 
1084 /*
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.
1090  */
1091 
update_timeout(Scsi_Cmnd * SCset,int timeout)1092 int update_timeout(Scsi_Cmnd * SCset, int timeout)
1093 {
1094 	int rtn;
1095 
1096 	/*
1097 	 * We are using the new error handling code to actually register/deregister
1098 	 * timers for timeout.
1099 	 */
1100 
1101 	if (!timer_pending(&SCset->eh_timeout)) {
1102 		rtn = 0;
1103 	} else {
1104 		rtn = SCset->eh_timeout.expires - jiffies;
1105 	}
1106 
1107 	if (timeout == 0) {
1108 		scsi_delete_timer(SCset);
1109 	} else {
1110 		scsi_add_timer(SCset, timeout, scsi_old_times_out);
1111 	}
1112 
1113 	return rtn;
1114 }
1115 
1116 
1117 /*
1118  * This function exports SCSI Bus, Device or Host reset capability
1119  * and is for use with the SCSI generic driver.
1120  */
1121 int
scsi_old_reset(Scsi_Cmnd * SCpnt,unsigned int flag)1122 scsi_old_reset(Scsi_Cmnd *SCpnt, unsigned int flag)
1123 {
1124 	unsigned int old_flags = SCSI_RESET_SYNCHRONOUS;
1125 
1126 	switch(flag) {
1127 	case SCSI_TRY_RESET_DEVICE:
1128 		/* no suggestion flags to add, device reset is default */
1129 		break;
1130 	case SCSI_TRY_RESET_BUS:
1131 		old_flags |= SCSI_RESET_SUGGEST_BUS_RESET;
1132 		break;
1133 	case SCSI_TRY_RESET_HOST:
1134 		old_flags |= SCSI_RESET_SUGGEST_HOST_RESET;
1135 		break;
1136 	default:
1137 		return FAILED;
1138 	}
1139 
1140 	if (scsi_reset(SCpnt, old_flags))
1141 		return FAILED;
1142 	return SUCCESS;
1143 }
1144 
1145 /*
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
1149  * of the file.
1150  * ---------------------------------------------------------------------------
1151  * Local variables:
1152  * c-indent-level: 4
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
1160  * tab-width: 8
1161  * End:
1162  */
1163