1 /*
2 * scsi.c Copyright (C) 1992 Drew Eckhardt
3 * generic mid-level SCSI driver by
4 * Drew Eckhardt
5 *
6 * <drew@colorado.edu>
7 *
8 * Bug correction thanks go to :
9 * Rik Faith <faith@cs.unc.edu>
10 * Tommy Thorn <tthorn>
11 * Thomas Wuensche <tw@fgb1.fgb.mw.tu-meunchen.de>
12 */
13
14 #include <linux/config.h>
15
16 #ifdef CONFIG_SCSI
17 #include <asm/system.h>
18 #include <linux/sched.h>
19 #include <linux/timer.h>
20 #include <linux/string.h>
21
22 #include "scsi.h"
23 #include "hosts.h"
24
25 #ifdef CONFIG_BLK_DEV_SD
26 #include "sd.h"
27 #endif
28
29 #ifdef CONFIG_BLK_DEV_ST
30 #include "st.h"
31 #endif
32
33 /*
34 static const char RCSid[] = "$Header: /usr/src/linux/kernel/blk_drv/scsi/RCS/scsi.c,v 1.1 1992/07/24 06:27:38 root Exp root $";
35 */
36
37 #define INTERNAL_ERROR (printk ("Internal error in file %s, line %s.\n", __FILE__, __LINE__), panic(""))
38
39 static void scsi_done (int host, int result);
40 static void update_timeout (void);
41
42 static int time_start;
43 static int time_elapsed;
44
45 /*
46 global variables :
47 NR_SCSI_DEVICES is the number of SCSI devices we have detected,
48 scsi_devices an array of these specifing the address for each
49 (host, id, LUN)
50 */
51
52 int NR_SCSI_DEVICES=0;
53 Scsi_Device scsi_devices[MAX_SCSI_DEVICE];
54
55 #define SENSE_LENGTH 255
56 /*
57 * As the scsi do command functions are inteligent, and may need to
58 * redo a command, we need to keep track of the last command
59 * executed on each one.
60 */
61
62 #define WAS_RESET 0x01
63 #define WAS_TIMEDOUT 0x02
64 #define WAS_SENSE 0x04
65 #define IS_RESETTING 0x08
66
67 static Scsi_Cmnd last_cmnd[MAX_SCSI_HOSTS];
68 static int last_reset[MAX_SCSI_HOSTS];
69
70 /*
71 * This is the number of clock ticks we should wait before we time out
72 * and abort the command. This is for where the scsi.c module generates
73 * the command, not where it originates from a higher level, in which
74 * case the timeout is specified there.
75 *
76 * ABORT_TIMEOUT and RESET_TIMEOUT are the timeouts for RESET and ABORT
77 * respectively.
78 */
79
80 #ifdef DEBUG
81 #define SCSI_TIMEOUT 500
82 #else
83 #define SCSI_TIMEOUT 100
84 #endif
85
86 #ifdef DEBUG
87 #define SENSE_TIMEOUT SCSI_TIMEOUT
88 #define ABORT_TIMEOUT SCSI_TIMEOUT
89 #define RESET_TIMEOUT SCSI_TIMEOUT
90 #else
91 #define SENSE_TIMEOUT 50
92 #define RESET_TIMEOUT 50
93 #define ABORT_TIMEOUT 50
94 #define MIN_RESET_DELAY 25
95 #endif
96
97 /*
98 * As the actual SCSI command runs in the background, we must set up a
99 * flag that tells scan_scsis() when the result it has is valid.
100 * scan_scsis can set the_result to -1, and watch for it to become the
101 * actual return code for that call. the scan_scsis_done function() is
102 * our user specified completion function that is passed on to the
103 * scsi_do_cmd() function.
104 */
105
106 volatile static int in_scan = 0;
107 static int the_result;
108 static unsigned char sense_buffer[SENSE_LENGTH];
109 static void scan_scsis_done (int host, int result)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
110 {
111
112 #ifdef DEBUG
113 printk ("scan_scsis_done(%d, %06x)\n\r", host, result);
114 #endif
115 the_result = result;
116 }
117 /*
118 * Detecting SCSI devices :
119 * We scan all present host adapter's busses, from ID 0 to ID 6.
120 * We use the INQUIRY command, determine device type, and pass the ID /
121 * lun address of all sequential devices to the tape driver, all random
122 * devices to the disk driver.
123 */
124
125 static void scan_scsis (void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
126 {
127 int host_nr , dev, lun, type, maxed, slave;
128 static unsigned char scsi_cmd [12];
129 static unsigned char scsi_result [256];
130
131 ++in_scan;
132
133 for (slave = host_nr = 0; host_nr < MAX_SCSI_HOSTS; ++host_nr,
134 slave = 0)
135 if (scsi_hosts[host_nr].present)
136 {
137 for (dev = 0; dev < 7; ++dev)
138 if (scsi_hosts[host_nr].this_id != dev)
139 #ifdef MULTI_LUN
140 for (lun = 0; lun < 8; ++lun)
141 {
142 #else
143 {
144 lun = 0;
145 #endif
146 /*
147 * Build an INQUIRY command block.
148 */
149
150 scsi_cmd[0] = INQUIRY;
151 scsi_cmd[1] = (lun << 5) & 0xe0;
152 scsi_cmd[2] = 0;
153 scsi_cmd[3] = 0;
154 scsi_cmd[4] = 255;
155 scsi_cmd[5] = 0;
156 the_result = -1;
157 #ifdef DEBUG
158 memset ((void *) scsi_result , 0, 255);
159 #endif
160 scsi_do_cmd (host_nr, dev, (void *) scsi_cmd, (void *)
161 scsi_result, 256, scan_scsis_done,
162 SCSI_TIMEOUT, sense_buffer, 3);
163
164 /*
165 * Wait for valid result
166 */
167
168 while (the_result < 0);
169
170 if (!the_result)
171 {
172 scsi_devices[NR_SCSI_DEVICES].
173 host_no = host_nr;
174 scsi_devices[NR_SCSI_DEVICES].
175 id = dev;
176 scsi_devices[NR_SCSI_DEVICES].
177 lun = lun;
178 scsi_devices[NR_SCSI_DEVICES].
179 removable = (0x80 &
180 scsi_result[1]) >> 7;
181 /*
182 * Currently, all sequential devices are assumed to be tapes,
183 * all random devices disk, with the appropriate read only
184 * flags set for ROM / WORM treated as RO.
185 */
186
187 switch (type = scsi_result[0])
188 {
189 case TYPE_TAPE :
190 case TYPE_DISK :
191 scsi_devices[NR_SCSI_DEVICES].writeable = 1;
192 break;
193 case TYPE_WORM :
194 case TYPE_ROM :
195 scsi_devices[NR_SCSI_DEVICES].writeable = 0;
196 break;
197 default :
198 type = -1;
199 }
200
201 scsi_devices[NR_SCSI_DEVICES].random = (type == TYPE_TAPE) ? 0 : 1;
202
203 maxed = 0;
204 switch (type)
205 {
206 case -1 :
207 break;
208 case TYPE_TAPE :
209 #ifdef DEBUG
210 printk("Detected scsi tape at host %d, ID %d, lun %d \n", host_nr, dev, lun);
211 #endif
212 #ifdef CONFIG_BLK_DEV_ST
213 if (!(maxed = (NR_ST == MAX_ST)))
214 scsi_tapes[NR_ST].device = &scsi_devices[NR_SCSI_DEVICES];
215 #endif
216 break;
217 default :
218 #ifdef DEBUG
219 printk("Detected scsi disk at host %d, ID %d, lun %d \n", host_nr, dev, lun);
220 #endif
221 #ifdef CONFIG_BLK_DEV_SD
222 if (!(maxed = (NR_SD >= MAX_SD)))
223 rscsi_disks[NR_SD].device = &scsi_devices[NR_SCSI_DEVICES];
224 #endif
225 }
226
227 if (maxed)
228 {
229 printk ("scsi : already have detected maximum number of SCSI %ss Unable to \n"
230 "add drive at SCSI host %s, ID %d, LUN %d\n\r", (type == TYPE_TAPE) ?
231 "tape" : "disk", scsi_hosts[host_nr].name,
232 dev, lun);
233 type = -1;
234 break;
235 }
236
237 else if (type != -1)
238 {
239 char *p;
240 char str[25];
241 memcpy((void *) str, (void *) &scsi_result[8], 8);
242 for (p = str; (p < (str + 8)) && (*p != ' '); ++p);
243 *p++ = ' ';
244 memcpy((void *) p, (void *) &scsi_result[16], 16);
245 for (; *p != ' '; ++p);
246 *p = 0;
247
248 printk("s%c%d at scsi%d, id %d, lun %d : %s\n",
249 (type == TYPE_TAPE) ? 't' : 'd',
250 (type == TYPE_TAPE) ?
251 #ifdef CONFIG_BLK_DEV_ST
252 NR_ST
253 #else
254 -1
255 #endif
256 :
257 #ifdef CONFIG_BLK_DEV_SD
258 NR_SD
259 #else
260 -1
261 #endif
262 ,host_nr , dev, lun, p);
263 if (type == TYPE_TAPE)
264 #ifdef CONFIG_BLK_DEV_ST
265 ++NR_ST;
266 #else
267 ;
268 #endif
269
270 else
271 #ifdef CONFIG_BLK_DEV_SD
272 ++NR_SD;
273 #else
274 ;
275 #endif
276 }
277 ++slave;
278 ++NR_SCSI_DEVICES;
279 } /* if result == DID_OK ends */
280 } /* for lun ends */
281 } /* if present */
282
283 printk("scsi : detected "
284 #ifdef CONFIG_BLK_DEV_SD
285 "%d SCSI disk%s "
286 #endif
287
288 #ifdef CONFIG_BLK_DEV_ST
289 "%d tape%s "
290 #endif
291
292 "total.\n",
293
294 #ifdef CONFIG_BLK_DEV_SD
295 NR_SD, (NR_SD != 1) ? "s" : ""
296 #ifdef CONFIG_BLK_DEV_ST
297 ,
298 #endif
299 #endif
300
301 #ifdef CONFIG_BLK_DEV_ST
302 NR_ST, (NR_ST != 1) ? "s" : ""
303 #endif
304 );
305 in_scan = 0;
306 } /* scan_scsis ends */
307
308 /*
309 * We handle the timeout differently if it happens when a reset,
310 * abort, etc are in process.
311 */
312
313 static unsigned char internal_timeout[MAX_SCSI_HOSTS];
314
315 /*
316 * Flag bits for the internal_timeout array
317 */
318
319 #define NORMAL_TIMEOUT 0
320 #define IN_ABORT 1
321 #define IN_RESET 2
322 /*
323 This is our time out function, called when the timer expires for a
324 given host adapter. It will attempt to abort the currently executing
325 command, that failing perform a kernel panic.
326 */
327
328 static void scsi_times_out (int host)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
329 {
330
331 switch (internal_timeout[host] & (IN_ABORT | IN_RESET))
332 {
333 case NORMAL_TIMEOUT:
334 if (!in_scan)
335 printk("SCSI host %d timed out - aborting command \r\n",
336 host);
337
338 if (!scsi_abort (host, DID_TIME_OUT))
339 return;
340 case IN_ABORT:
341 printk("SCSI host %d abort() timed out - reseting \r\n",
342 host);
343 if (!scsi_reset (host))
344 return;
345 case IN_RESET:
346 case (IN_ABORT | IN_RESET):
347 printk("Unable to reset scsi host %d\r\n",host);
348 panic("");
349 default:
350 INTERNAL_ERROR;
351 }
352
353 }
354
355 /*
356 This is inline because we have stack problemes if we recurse to deeply.
357 */
358
359 static void internal_cmnd (int host, unsigned char target, const void *cmnd ,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
360 void *buffer, unsigned bufflen, void (*done)(int,int))
361 {
362 int temp;
363
364 #ifdef DEBUG_DELAY
365 int clock;
366 #endif
367
368 if ((host < 0) || (host > MAX_SCSI_HOSTS))
369 panic ("Host number in internal_cmnd() is out of range.\n");
370
371
372 /*
373 We will wait MIN_RESET_DELAY clock ticks after the last reset so
374 we can avoid the drive not being ready.
375 */
376 temp = last_reset[host];
377 while (jiffies < temp);
378
379 host_timeout[host] = last_cmnd[host].timeout_per_command;
380 update_timeout();
381
382 /*
383 We will use a queued command if possible, otherwise we will emulate the
384 queing and calling of completion function ourselves.
385 */
386 #ifdef DEBUG
387 printk("internal_cmnd (host = %d, target = %d, command = %08x, buffer = %08x, \n"
388 "bufflen = %d, done = %08x)\n", host, target, cmnd, buffer, bufflen, done);
389 #endif
390
391
392 if (scsi_hosts[host].can_queue)
393 {
394 #ifdef DEBUG
395 printk("queuecommand : routine at %08x\n",
396 scsi_hosts[host].queuecommand);
397 #endif
398 scsi_hosts[host].queuecommand (target, cmnd, buffer, bufflen,
399 done);
400 }
401 else
402 {
403
404 #ifdef DEBUG
405 printk("command() : routine at %08x\n", scsi_hosts[host].command);
406 #endif
407 temp=scsi_hosts[host].command (target, cmnd, buffer, bufflen);
408
409 #ifdef DEBUG_DELAY
410 clock = jiffies + 400;
411 while (jiffies < clock);
412 printk("done(host = %d, result = %04x) : routine at %08x\n", host, temp, done);
413 #endif
414 done(host, temp);
415 }
416 #ifdef DEBUG
417 printk("leaving internal_cmnd()\n");
418 #endif
419 }
420
421 static void scsi_request_sense (int host, unsigned char target,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
422 unsigned char lun)
423 {
424 cli();
425 host_timeout[host] = SENSE_TIMEOUT;
426 update_timeout();
427 last_cmnd[host].flags |= WAS_SENSE;
428 sti();
429
430 last_cmnd[host].sense_cmnd[1] = lun << 5;
431
432 internal_cmnd (host, target, (void *) last_cmnd[host].sense_cmnd,
433 (void *) last_cmnd[host].sense_buffer, SENSE_LENGTH,
434 scsi_done);
435 }
436
437
438
439
440
441 /*
442 scsi_do_cmd sends all the commands out to the low-level driver. It
443 handles the specifics required for each low level driver - ie queued
444 or non queud. It also prevents conflicts when different high level
445 drivers go for the same host at the same time.
446 */
447
448 void scsi_do_cmd (int host, unsigned char target, const void *cmnd ,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
449 void *buffer, unsigned bufflen, void (*done)(int,int),
450 int timeout, unsigned char *sense_buffer, int retries
451 )
452 {
453 int ok = 0;
454
455 #ifdef DEBUG
456 int i;
457 printk ("scsi_do_cmd (host = %d, target = %d, buffer =%08x, "
458 "bufflen = %d, done = %08x, timeout = %d, retries = %d)\n"
459 "command : " , host, target, buffer, bufflen, done, timeout, retries);
460 for (i = 0; i < 10; ++i)
461 printk ("%02x ", ((unsigned char *) cmnd)[i]);
462 printk("\n");
463 #endif
464
465 if ((host >= MAX_SCSI_HOSTS) || !scsi_hosts[host].present)
466 {
467 printk ("Invalid or not present host number. %d\n", host);
468 panic("");
469 }
470
471
472 /*
473 We must prevent reentrancy to the lowlevel host driver. This prevents
474 it - we enter a loop until the host we want to talk to is not busy.
475 Race conditions are prevented, as interrupts are disabled inbetween the
476 time we check for the host being not busy, and the time we mark it busy
477 ourselves.
478 */
479
480 do {
481 cli();
482 if (host_busy[host])
483 {
484 sti();
485 #ifdef DEBUG
486 printk("Host %d is busy.\n", host);
487 #endif
488 while (host_busy[host]);
489 #ifdef DEBUG
490 printk("Host %d is no longer busy.\n", host);
491 #endif
492 }
493 else
494 {
495 host_busy[host] = 1;
496 ok = 1;
497 sti();
498 }
499 } while (!ok);
500
501
502 /*
503 Our own function scsi_done (which marks the host as not busy, disables
504 the timeout counter, etc) will be called by us or by the
505 scsi_hosts[host].queuecommand() function needs to also call
506 the completion function for the high level driver.
507
508 */
509
510 memcpy ((void *) last_cmnd[host].cmnd , (void *) cmnd, 10);
511 last_cmnd[host].host = host;
512 last_cmnd[host].target = target;
513 last_cmnd[host].lun = (last_cmnd[host].cmnd[1] >> 5);
514 last_cmnd[host].bufflen = bufflen;
515 last_cmnd[host].buffer = buffer;
516 last_cmnd[host].sense_buffer = sense_buffer;
517 last_cmnd[host].flags=0;
518 last_cmnd[host].retries=0;
519 last_cmnd[host].allowed=retries;
520 last_cmnd[host].done = done;
521 last_cmnd[host].timeout_per_command = timeout;
522
523 /* Start the timer ticking. */
524
525 internal_timeout[host] = 0;
526 internal_cmnd (host, target, cmnd , buffer, bufflen, scsi_done);
527
528 #ifdef DEBUG
529 printk ("Leaving scsi_do_cmd()\n");
530 #endif
531 }
532
533
534 /*
535 The scsi_done() function disables the timeout timer for the scsi host,
536 marks the host as not busy, and calls the user specified completion
537 function for that host's current command.
538 */
539
540 static void reset (int host)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
541 {
542 #ifdef DEBUG
543 printk("reset(%d)\n", host);
544 #endif
545
546 last_cmnd[host].flags |= (WAS_RESET | IS_RESETTING);
547 scsi_reset(host);
548
549 #ifdef DEBUG
550 printk("performing request sense\n");
551 #endif
552
553 scsi_request_sense (host, last_cmnd[host].target, last_cmnd[host].lun);
554 }
555
556
557
558 static int check_sense (int host)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
559 {
560 if (((sense_buffer[0] & 0x70) >> 4) == 7)
561 switch (sense_buffer[2] & 0xf)
562 {
563 case NO_SENSE:
564 case RECOVERED_ERROR:
565 return 0;
566
567 case ABORTED_COMMAND:
568 case NOT_READY:
569 case UNIT_ATTENTION:
570 return SUGGEST_RETRY;
571
572 /* these three are not supported */
573 case COPY_ABORTED:
574 case VOLUME_OVERFLOW:
575 case MISCOMPARE:
576
577 case MEDIUM_ERROR:
578 return SUGGEST_REMAP;
579 case BLANK_CHECK:
580 case DATA_PROTECT:
581 case HARDWARE_ERROR:
582 case ILLEGAL_REQUEST:
583 default:
584 return SUGGEST_ABORT;
585 }
586 else
587 return SUGGEST_RETRY;
588 }
589
590 static void scsi_done (int host, int result)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
591 {
592 int status=0;
593 int exit=0;
594 int checked;
595 int oldto;
596 oldto = host_timeout[host];
597 host_timeout[host] = 0;
598 update_timeout();
599
600 #define FINISHED 0
601 #define MAYREDO 1
602 #define REDO 3
603
604 #ifdef DEBUG
605 printk("In scsi_done(host = %d, result = %06x)\n", host, result);
606 #endif
607 if (host > MAX_SCSI_HOSTS || host < 0)
608 {
609 host_timeout[host] = 0;
610 update_timeout();
611 panic("scsi_done() called with invalid host number.\n");
612 }
613
614 switch (host_byte(result))
615 {
616 case DID_OK:
617 if (last_cmnd[host].flags & IS_RESETTING)
618 {
619 last_cmnd[host].flags &= ~IS_RESETTING;
620 status = REDO;
621 break;
622 }
623
624 if (status_byte(result) && (last_cmnd[host].flags &
625 WAS_SENSE))
626 {
627 last_cmnd[host].flags &= ~WAS_SENSE;
628 cli();
629 internal_timeout[host] &= ~SENSE_TIMEOUT;
630 sti();
631
632 if (!(last_cmnd[host].flags & WAS_RESET))
633 reset(host);
634 else
635 {
636 exit = (DRIVER_HARD | SUGGEST_ABORT);
637 status = FINISHED;
638 }
639 }
640 else switch(msg_byte(result))
641 {
642 case COMMAND_COMPLETE:
643 switch (status_byte(result))
644 {
645 case GOOD:
646 if (last_cmnd[host].flags & WAS_SENSE)
647 {
648 #ifdef DEBUG
649 printk ("In scsi_done, GOOD status, COMMAND COMPLETE, parsing sense information.\n");
650 #endif
651
652 last_cmnd[host].flags &= ~WAS_SENSE;
653 cli();
654 internal_timeout[host] &= ~SENSE_TIMEOUT;
655 sti();
656
657 switch (checked = check_sense(host))
658 {
659 case 0:
660 #ifdef DEBUG
661 printk("NO SENSE. status = REDO\n");
662 #endif
663
664 host_timeout[host] = oldto;
665 update_timeout();
666 status = REDO;
667 break;
668 case SUGGEST_REMAP:
669 case SUGGEST_RETRY:
670 #ifdef DEBUG
671 printk("SENSE SUGGEST REMAP or SUGGEST RETRY - status = MAYREDO\n");
672 #endif
673
674 status = MAYREDO;
675 exit = SUGGEST_RETRY;
676 break;
677 case SUGGEST_ABORT:
678 #ifdef DEBUG
679 printk("SENSE SUGGEST ABORT - status = FINISHED");
680 #endif
681
682 status = FINISHED;
683 exit = DRIVER_SENSE;
684 break;
685 default:
686 printk ("Internal error %s %s \n", __FILE__,
687 __LINE__);
688 }
689 }
690 else
691 {
692 #ifdef DEBUG
693 printk("COMMAND COMPLETE message returned, status = FINISHED. \n");
694 #endif
695
696 exit = DRIVER_OK;
697 status = FINISHED;
698 }
699 break;
700
701 case CHECK_CONDITION:
702
703 #ifdef DEBUG
704 printk("CHECK CONDITION message returned, performing request sense.\n");
705 #endif
706
707 scsi_request_sense (host, last_cmnd[host].target, last_cmnd[host].lun);
708 break;
709
710 case CONDITION_GOOD:
711 case INTERMEDIATE_GOOD:
712 case INTERMEDIATE_C_GOOD:
713 #ifdef DEBUG
714 printk("CONDITION GOOD, INTERMEDIATE GOOD, or INTERMEDIATE CONDITION GOOD recieved and ignored. \n");
715 #endif
716 break;
717
718 case BUSY:
719 #ifdef DEBUG
720 printk("BUSY message returned, performing REDO");
721 #endif
722 host_timeout[host] = oldto;
723 update_timeout();
724 status = REDO;
725 break;
726
727 case RESERVATION_CONFLICT:
728 reset(host);
729 exit = DRIVER_SOFT | SUGGEST_ABORT;
730 status = MAYREDO;
731 break;
732 default:
733 printk ("Internal error %s %s \n"
734 "status byte = %d \n", __FILE__,
735 __LINE__, status_byte(result));
736
737 }
738 break;
739 default:
740 panic ("unsupported message byte recieved.");
741 }
742 break;
743 case DID_TIME_OUT:
744 #ifdef DEBUG
745 printk("Host returned DID_TIME_OUT - ");
746 #endif
747
748 if (last_cmnd[host].flags & WAS_TIMEDOUT)
749 {
750 #ifdef DEBUG
751 printk("Aborting\n");
752 #endif
753 exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
754 }
755 else
756 {
757 #ifdef DEBUG
758 printk ("Retrying.\n");
759 #endif
760 last_cmnd[host].flags |= WAS_TIMEDOUT;
761 status = REDO;
762 }
763 break;
764 case DID_BUS_BUSY:
765 case DID_PARITY:
766 status = REDO;
767 break;
768 case DID_NO_CONNECT:
769 #ifdef DEBUG
770 printk("Couldn't connect.\n");
771 #endif
772 exit = (DRIVER_HARD | SUGGEST_ABORT);
773 break;
774 case DID_ERROR:
775 status = MAYREDO;
776 exit = (DRIVER_HARD | SUGGEST_ABORT);
777 break;
778 case DID_BAD_TARGET:
779 case DID_ABORT:
780 exit = (DRIVER_INVALID | SUGGEST_ABORT);
781 break;
782 default :
783 exit = (DRIVER_ERROR | SUGGEST_DIE);
784 }
785
786 switch (status)
787 {
788 case FINISHED:
789 break;
790 case MAYREDO:
791
792 #ifdef DEBUG
793 printk("In MAYREDO, allowing %d retries, have %d\n\r",
794 last_cmnd[host].allowed, last_cmnd[host].retries);
795 #endif
796
797 if ((++last_cmnd[host].retries) < last_cmnd[host].allowed)
798 {
799 if ((last_cmnd[host].retries >= (last_cmnd[host].allowed >> 1))
800 && !(last_cmnd[host].flags & WAS_RESET))
801 reset(host);
802 break;
803
804 }
805 else
806 {
807 status = FINISHED;
808 break;
809 }
810 /* fall through to REDO */
811
812 case REDO:
813 if (last_cmnd[host].flags & WAS_SENSE)
814 scsi_request_sense (host, last_cmnd[host].target,
815 last_cmnd[host].lun);
816 else
817 internal_cmnd (host, last_cmnd[host].target,
818 last_cmnd[host].cmnd,
819 last_cmnd[host].buffer,
820 last_cmnd[host].bufflen, scsi_done);
821 break;
822 default:
823 INTERNAL_ERROR;
824 }
825
826 if (status == FINISHED)
827 {
828 #ifdef DEBUG
829 printk("Calling done function - at address %08x\n", last_cmnd[host].done);
830 #endif
831 host_busy[host] = 0;
832 last_cmnd[host].done (host, (result | ((exit & 0xff) << 24)));
833 }
834
835
836 #undef FINISHED
837 #undef REDO
838 #undef MAYREDO
839
840 }
841
842 /*
843 The scsi_abort function interfaces with the abort() function of the host
844 we are aborting, and causes the current command to not complete. The
845 caller should deal with any error messages or status returned on the
846 next call.
847
848 This will not be called rentrantly for a given host.
849 */
850
851 /*
852 Since we're nice guys and specified that abort() and reset()
853 can be non-reentrant. The internal_timeout flags are used for
854 this.
855 */
856
857
858 int scsi_abort (int host, int why)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
859 {
860 int temp, oldto;
861
862 while(1)
863 {
864 cli();
865 if (internal_timeout[host] & IN_ABORT)
866 {
867 sti();
868 while (internal_timeout[host] & IN_ABORT);
869 }
870 else
871 {
872 oldto = host_timeout[host];
873 internal_timeout[host] |= IN_ABORT;
874 host_timeout[host] = ABORT_TIMEOUT;
875 update_timeout();
876
877
878 sti();
879 if (!host_busy[host] || !scsi_hosts[host].abort(why))
880 temp = 0;
881 else
882 temp = 1;
883
884 cli();
885 internal_timeout[host] &= ~IN_ABORT;
886 host_timeout[host]=oldto;
887 update_timeout();
888 sti();
889 return temp;
890 }
891 }
892 }
893
894 int scsi_reset (int host)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
895 {
896 int temp, oldto;
897
898 while (1) {
899 cli();
900 if (internal_timeout[host] & IN_RESET)
901 {
902 sti();
903 while (internal_timeout[host] & IN_RESET);
904 }
905 else
906 {
907 oldto = host_timeout[host];
908 host_timeout[host] = RESET_TIMEOUT;
909 update_timeout();
910 internal_timeout[host] |= IN_RESET;
911
912 if (host_busy[host])
913 {
914 sti();
915 if (!(last_cmnd[host].flags & IS_RESETTING) && !(internal_timeout[host] & IN_ABORT))
916 scsi_abort(host, DID_RESET);
917
918 temp = scsi_hosts[host].reset();
919 }
920 else
921 {
922 host_busy[host]=1;
923
924 sti();
925 temp = scsi_hosts[host].reset();
926 last_reset[host] = jiffies;
927 host_busy[host]=0;
928 }
929
930 cli();
931 host_timeout[host] = oldto;
932 update_timeout();
933 internal_timeout[host] &= ~IN_RESET;
934 sti();
935 return temp;
936 }
937 }
938 }
939
940
941 static void scsi_main_timeout(void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
942 {
943 /*
944 We must not enter update_timeout with a timeout condition still pending.
945 */
946
947 int i, timed_out;
948
949 do {
950 cli();
951
952 /*
953 Find all timers such that they have 0 or negative (shouldn't happen)
954 time remaining on them.
955 */
956
957 for (i = timed_out = 0; i < MAX_SCSI_HOSTS; ++i)
958 if (host_timeout[i] != 0 && host_timeout[i] <= time_elapsed)
959 {
960 sti();
961 host_timeout[i] = 0;
962 scsi_times_out(i);
963 ++timed_out;
964 }
965
966 update_timeout();
967 } while (timed_out);
968 sti();
969 }
970
971 /*
972 These are used to keep track of things.
973 */
974
975 static int time_start, time_elapsed;
976
977 /*
978 The strategy is to cause the timer code to call scsi_times_out()
979 when the soonest timeout is pending.
980 */
981
982 static void update_timeout(void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
983 {
984 unsigned int i, least, used;
985
986 cli();
987
988 /*
989 Figure out how much time has passed since the last time the timeouts
990 were updated
991 */
992 used = (time_start) ? (jiffies - time_start) : 0;
993
994 /*
995 Find out what is due to timeout soonest, and adjust all timeouts for
996 the amount of time that has passed since the last time we called
997 update_timeout.
998 */
999
1000 for (i = 0, least = 0xffffffff; i < MAX_SCSI_HOSTS; ++i)
1001 if (host_timeout[i] > 0 && (host_timeout[i] -= used) < least)
1002 least = host_timeout[i];
1003
1004 /*
1005 If something is due to timeout again, then we will set the next timeout
1006 interrupt to occur. Otherwise, timeouts are disabled.
1007 */
1008
1009 if (least != 0xffffffff)
1010 {
1011 time_start = jiffies;
1012 timer_table[SCSI_TIMER].expires = (time_elapsed = least) + jiffies;
1013 timer_active |= 1 << SCSI_TIMER;
1014 }
1015 else
1016 {
1017 timer_table[SCSI_TIMER].expires = time_start = time_elapsed = 0;
1018 timer_active &= ~(1 << SCSI_TIMER);
1019 }
1020 sti();
1021 }
1022 /*
1023 scsi_dev_init() is our initialization routine, which inturn calls host
1024 initialization, bus scanning, and sd/st initialization routines. It
1025 should be called from main().
1026 */
1027
1028 static unsigned char generic_sense[6] = {REQUEST_SENSE, 0,0,0, 255, 0};
1029 void scsi_dev_init (void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1030 {
1031 int i;
1032 #ifdef FOO_ON_YOU
1033 return;
1034 #endif
1035 timer_table[SCSI_TIMER].fn = scsi_main_timeout;
1036 timer_table[SCSI_TIMER].expires = 0;
1037
1038 scsi_init(); /* initialize all hosts */
1039 /*
1040 * Set up sense command in each host structure.
1041 */
1042
1043 for (i = 0; i < MAX_SCSI_HOSTS; ++i)
1044 {
1045 memcpy ((void *) last_cmnd[i].sense_cmnd, (void *) generic_sense,
1046 6);
1047 last_reset[i] = 0;
1048 }
1049
1050 scan_scsis(); /* scan for scsi devices */
1051
1052 #ifdef CONFIG_BLK_DEV_SD
1053 sd_init(); /* init scsi disks */
1054 #endif
1055
1056 #ifdef CONFIG_BLK_DEV_ST
1057 st_init(); /* init scsi tapes */
1058 #endif
1059 }
1060 #endif