2 * scsi.c Copyright (C) 1992 Drew Eckhardt
3 * generic mid-level SCSI driver by
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-muenchen.de>
13 * Modified by Eric Youngdale eric@tantalus.nrl.navy.mil to
14 * add scatter-gather, multiple outstanding request, and other
18 #include <asm/system.h>
19 #include <linux/sched.h>
20 #include <linux/timer.h>
21 #include <linux/string.h>
23 #include "../block/blk.h"
26 #include "constants.h"
29 static const char RCSid[] = "$Header: /usr/src/linux/kernel/blk_drv/scsi/RCS/scsi.c,v 1.5 1993/09/24 12:45:18 drew Exp drew $";
32 /* Command groups 3 and 4 are reserved and should never be used. */
33 const unsigned char scsi_command_size
[8] = { 6, 10, 10, 12, 12, 12, 10, 10 };
35 #define INTERNAL_ERROR (panic ("Internal error in file %s, line %d.\n", __FILE__, __LINE__))
37 static void scsi_done (Scsi_Cmnd
*SCpnt
);
38 static int update_timeout (Scsi_Cmnd
*, int);
39 static void print_inquiry(unsigned char *data
);
40 static void scsi_times_out (Scsi_Cmnd
* SCpnt
);
42 static int time_start
;
43 static int time_elapsed
;
45 #define MAX_SCSI_DEVICE_CODE 10
46 const char *const scsi_device_types
[MAX_SCSI_DEVICE_CODE
] =
63 NR_SCSI_DEVICES is the number of SCSI devices we have detected,
64 scsi_devices an array of these specifing the address for each
68 int NR_SCSI_DEVICES
=0;
70 Scsi_Device
* scsi_devices
= NULL
;
72 static unsigned char generic_sense
[6] = {REQUEST_SENSE
, 0,0,0, 255, 0};
74 /* We make this not static so that we can read the array with gdb. */
75 /* static */ Scsi_Cmnd
* last_cmnd
= NULL
;
78 * As the scsi do command functions are inteligent, and may need to
79 * redo a command, we need to keep track of the last command
80 * executed on each one.
83 #define WAS_RESET 0x01
84 #define WAS_TIMEDOUT 0x02
85 #define WAS_SENSE 0x04
86 #define IS_RESETTING 0x08
87 #define ASKED_FOR_SENSE 0x10
88 /* #define NEEDS_JUMPSTART 0x20 defined in hosts.h */
91 * This is the number of clock ticks we should wait before we time out
92 * and abort the command. This is for where the scsi.c module generates
93 * the command, not where it originates from a higher level, in which
94 * case the timeout is specified there.
96 * ABORT_TIMEOUT and RESET_TIMEOUT are the timeouts for RESET and ABORT
101 #define SCSI_TIMEOUT 500
103 #define SCSI_TIMEOUT 100
107 #define SENSE_TIMEOUT SCSI_TIMEOUT
108 #define ABORT_TIMEOUT SCSI_TIMEOUT
109 #define RESET_TIMEOUT SCSI_TIMEOUT
111 #define SENSE_TIMEOUT 50
112 #define RESET_TIMEOUT 50
113 #define ABORT_TIMEOUT 50
114 #define MIN_RESET_DELAY 100
117 /* The following devices are known not to tolerate a lun != 0 scan for
118 one reason or another. Some will respond to all luns, others will
124 char * revision
; /* Latest revision known to be bad. Not used yet */
127 static struct blist blacklist
[] =
129 {"DENON","DRD-25X","V"}, /* A cdrom that locks up when probed at lun != 0 */
130 {"MAXTOR","XT-4380S","B3C"}, /* Locks-up when LUN>0 polled. */
131 {"MAXTOR","MXT-1240S","I1.2"}, /* Locks up when LUN > 0 polled */
132 {"MAXTOR","XT-4170S","B5A"}, /* Locks-up sometimes when LUN>0 polled. */
133 {"NEC","CD-ROM DRIVE:841","1.0"}, /* Locks-up when LUN>0 polled. */
134 {"SEAGATE", "ST157N", "\004|j"}, /* causes failed REQUEST SENSE on lun 1 for aha152x
135 * controller, which causes SCSI code to reset bus.*/
136 {"SEAGATE", "ST296","921"}, /* Responds to all lun */
137 {"SONY","CD-ROM CDU-541","4.3d"},
138 {"TANDBERG","TDC 3600","U07"}, /* Locks up if polled for lun != 0 */
139 {"TEXEL","CD-ROM","1.06"}, /* causes failed REQUEST SENSE on lun 1 for seagate
140 * controller, which causes SCSI code to reset bus.*/
143 static int blacklisted(unsigned char * response_data
){
147 if(blacklist
[i
].vendor
== NULL
) return 0;
148 pnt
= &response_data
[8];
149 while(*pnt
&& *pnt
== ' ') pnt
++;
150 if(memcmp(blacklist
[i
].vendor
, pnt
,
151 strlen(blacklist
[i
].vendor
))) continue;
152 pnt
= &response_data
[16];
153 while(*pnt
&& *pnt
== ' ') pnt
++;
154 if(memcmp(blacklist
[i
].model
, pnt
,
155 strlen(blacklist
[i
].model
))) continue;
161 * As the actual SCSI command runs in the background, we must set up a
162 * flag that tells scan_scsis() when the result it has is valid.
163 * scan_scsis can set the_result to -1, and watch for it to become the
164 * actual return code for that call. the scan_scsis_done function() is
165 * our user specified completion function that is passed on to the
166 * scsi_do_cmd() function.
169 static volatile int in_scan
= 0;
170 static int the_result
;
171 static void scan_scsis_done (Scsi_Cmnd
* SCpnt
)
175 printk ("scan_scsis_done(%d, %06x)\n", SCpnt
->host
, SCpnt
->result
);
177 SCpnt
->request
.dev
= 0xfffe;
180 * Detecting SCSI devices :
181 * We scan all present host adapter's busses, from ID 0 to ID 6.
182 * We use the INQUIRY command, determine device type, and pass the ID /
183 * lun address of all sequential devices to the tape driver, all random
184 * devices to the disk driver.
187 static void scan_scsis (void)
190 unsigned char scsi_cmd
[12];
191 unsigned char scsi_result
[256];
192 struct Scsi_Host
* shpnt
;
200 for (shpnt
= scsi_hostlist
; shpnt
; shpnt
= shpnt
->next
)
202 shpnt
->host_queue
= &SCmd
; /* We need this so that
203 commands can time out */
204 for (dev
= 0; dev
< 8; ++dev
)
205 if (shpnt
->this_id
!= dev
)
207 * We need the for so our continue, etc. work fine.
211 for (lun
= 0; lun
< 1; ++lun
)
213 for (lun
= 0; lun
< 8; ++lun
)
216 scsi_devices
[NR_SCSI_DEVICES
].host
= shpnt
;
217 scsi_devices
[NR_SCSI_DEVICES
].id
= dev
;
218 scsi_devices
[NR_SCSI_DEVICES
].lun
= lun
;
219 scsi_devices
[NR_SCSI_DEVICES
].index
= NR_SCSI_DEVICES
;
220 scsi_devices
[NR_SCSI_DEVICES
].device_wait
= NULL
;
222 * Assume that the device will have handshaking problems, and then
223 * fix this field later if it turns out it doesn't.
225 scsi_devices
[NR_SCSI_DEVICES
].borken
= 1;
227 scsi_cmd
[0] = TEST_UNIT_READY
;
228 scsi_cmd
[1] = lun
<< 5;
229 scsi_cmd
[2] = scsi_cmd
[3] = scsi_cmd
[5] = 0;
236 SCmd
.request
.dev
= 0xffff; /* Mark not busy */
239 SCmd
.transfersize
= 0;
241 SCmd
.index
= NR_SCSI_DEVICES
;
244 (void *) scsi_cmd
, (void *)
245 scsi_result
, 256, scan_scsis_done
,
246 SCSI_TIMEOUT
+ 400, 5);
248 while (SCmd
.request
.dev
!= 0xfffe);
249 #if defined(DEBUG) || defined(DEBUG_INIT)
250 printk("scsi: scan SCSIS id %d lun %d\n", dev
, lun
);
251 printk("scsi: return code %08x\n", SCmd
.result
);
256 if ((driver_byte(SCmd
.result
) & DRIVER_SENSE
) &&
257 ((SCmd
.sense_buffer
[0] & 0x70) >> 4) == 7) {
258 if (SCmd
.sense_buffer
[2] &0xe0)
259 continue; /* No devices here... */
260 if(((SCmd
.sense_buffer
[2] & 0xf) != NOT_READY
) &&
261 ((SCmd
.sense_buffer
[2] & 0xf) != UNIT_ATTENTION
))
268 #if defined (DEBUG) || defined(DEBUG_INIT)
269 printk("scsi: performing INQUIRY\n");
273 * Build an INQUIRY command block.
276 scsi_cmd
[0] = INQUIRY
;
277 scsi_cmd
[1] = (lun
<< 5) & 0xe0;
283 SCmd
.request
.dev
= 0xffff; /* Mark not busy */
286 (void *) scsi_cmd
, (void *)
287 scsi_result
, 256, scan_scsis_done
,
290 while (SCmd
.request
.dev
!= 0xfffe);
292 the_result
= SCmd
.result
;
294 #if defined(DEBUG) || defined(DEBUG_INIT)
296 printk("scsi: INQUIRY successful\n");
298 printk("scsi: INQUIRY failed with code %08x\n");
301 if(the_result
) break;
303 /* skip other luns on this device */
307 scsi_devices
[NR_SCSI_DEVICES
].
309 scsi_result
[1]) >> 7;
310 scsi_devices
[NR_SCSI_DEVICES
].lockable
=
311 scsi_devices
[NR_SCSI_DEVICES
].removable
;
312 scsi_devices
[NR_SCSI_DEVICES
].
314 scsi_devices
[NR_SCSI_DEVICES
].
316 scsi_devices
[NR_SCSI_DEVICES
].
319 * Currently, all sequential devices are assumed to be tapes,
320 * all random devices disk, with the appropriate read only
321 * flags set for ROM / WORM treated as RO.
324 switch (type
= scsi_result
[0])
329 scsi_devices
[NR_SCSI_DEVICES
].writeable
= 1;
333 scsi_devices
[NR_SCSI_DEVICES
].writeable
= 0;
338 printk("scsi: unknown type %d\n", type
);
339 print_inquiry(scsi_result
);
345 scsi_devices
[NR_SCSI_DEVICES
].random
=
346 (type
== TYPE_TAPE
) ? 0 : 1;
347 scsi_devices
[NR_SCSI_DEVICES
].type
= type
;
351 print_inquiry(scsi_result
);
354 printk("Detected scsi tape st%d at scsi%d, id %d, lun %d\n", MAX_ST
,
355 shpnt
->host_no
, dev
, lun
);
356 if(NR_ST
!= -1) ++MAX_ST
;
359 printk("Detected scsi CD-ROM sr%d at scsi%d, id %d, lun %d\n", MAX_SR
,
360 shpnt
->host_no
, dev
, lun
);
361 if(NR_SR
!= -1) ++MAX_SR
;
365 printk("Detected scsi disk sd%c at scsi%d, id %d, lun %d\n", 'a'+MAX_SD
,
366 shpnt
->host_no
, dev
, lun
);
367 if(NR_SD
!= -1) ++MAX_SD
;
373 if(NR_SG
!= -1) ++MAX_SG
;
375 scsi_devices
[NR_SCSI_DEVICES
].scsi_level
=
376 scsi_result
[2] & 0x07;
377 if (scsi_devices
[NR_SCSI_DEVICES
].scsi_level
>= 2 ||
378 (scsi_devices
[NR_SCSI_DEVICES
].scsi_level
== 1 &&
379 (scsi_result
[3] & 0x0f) == 1))
380 scsi_devices
[NR_SCSI_DEVICES
].scsi_level
++;
382 * Set the tagged_queue flag for SCSI-II devices that purport to support
383 * tagged queuing in the INQUIRY data.
386 scsi_devices
[NR_SCSI_DEVICES
].tagged_queue
= 0;
388 if ((scsi_devices
[NR_SCSI_DEVICES
].scsi_level
== SCSI_2
) &&
389 (scsi_result
[7] & 2)) {
390 scsi_devices
[NR_SCSI_DEVICES
].tagged_supported
= 1;
391 scsi_devices
[NR_SCSI_DEVICES
].current_tag
= 0;
395 * Accomodate drivers that want to sleep when they should be in a polling
399 scsi_devices
[NR_SCSI_DEVICES
].disconnect
= 0;
402 * Some revisions of the Texel CD ROM drives have handshaking
403 * problems when used with the Seagate controllers. Before we
404 * know what type of device we're talking to, we assume it's
405 * borken and then change it here if it turns out that it isn't
409 if(strncmp("TEXEL", (char *) &scsi_result
[8], 5) != 0 ||
410 strncmp("CD-ROM", (char *) &scsi_result
[16], 6) != 0
412 * XXX 1.06 has problems, some one should figure out the others too so
413 * ALL TEXEL drives don't suffer in performance, especially when I finish
414 * integrating my seagate patches which do multiple I_T_L nexuses.
418 || (strncmp("1.06", (char *) &scsi_result
[[, 4) != 0)
421 scsi_devices
[NR_SCSI_DEVICES
].borken
= 0;
424 /* These devices need this "key" to unlock the device
426 if(memcmp("INSITE", &scsi_result
[8], 6) == 0 &&
427 (memcmp("Floptical F*8I", &scsi_result
[16], 16) == 0
428 || memcmp("I325VM", &scsi_result
[16], 6) == 0)) {
429 printk("Unlocked floptical drive.\n");
430 scsi_devices
[NR_SCSI_DEVICES
].lockable
= 0;
431 scsi_cmd
[0] = MODE_SENSE
;
432 scsi_cmd
[1] = (lun
<< 5) & 0xe0;
438 SCmd
.request
.dev
= 0xffff; /* Mark not busy */
441 (void *) scsi_cmd
, (void *)
442 scsi_result
, 0x2a, scan_scsis_done
,
445 while (SCmd
.request
.dev
!= 0xfffe);
449 /* Some scsi devices cannot be polled for lun != 0
450 due to firmware bugs */
451 if(blacklisted(scsi_result
)) break;
452 /* Some scsi-1 peripherals do not handle lun != 0.
453 I am assuming that scsi-2 peripherals do better */
454 if((scsi_result
[2] & 0x07) == 1 &&
455 (scsi_result
[3] & 0x0f) == 0) break;
457 } /* if result == DID_OK ends */
459 shpnt
->host_queue
= NULL
; /* No longer needed here */
462 printk("scsi : detected ");
464 printk("%d SCSI disk%s ", MAX_SD
, (MAX_SD
!= 1) ? "s" : "");
467 printk("%d tape%s ", MAX_ST
, (MAX_ST
!= 1) ? "s" : "");
470 printk("%d CD-ROM drive%s ", MAX_SR
, (MAX_SR
!= 1) ? "s" : "");
474 } /* scan_scsis ends */
477 * Flag bits for the internal_timeout array
480 #define NORMAL_TIMEOUT 0
484 This is our time out function, called when the timer expires for a
485 given host adapter. It will attempt to abort the currently executing
486 command, that failing perform a kernel panic.
489 static void scsi_times_out (Scsi_Cmnd
* SCpnt
)
492 switch (SCpnt
->internal_timeout
& (IN_ABORT
| IN_RESET
))
496 printk("SCSI host %d timed out - aborting command\n",
497 SCpnt
->host
->host_no
);
499 if (!scsi_abort (SCpnt
, DID_TIME_OUT
))
502 printk("SCSI host %d abort() timed out - reseting\n",
503 SCpnt
->host
->host_no
);
504 if (!scsi_reset (SCpnt
))
507 case (IN_ABORT
| IN_RESET
):
508 panic("Unable to reset scsi host %d\n",SCpnt
->host
->host_no
);
516 /* This function takes a quick look at a request, and decides if it
517 can be queued now, or if there would be a stall while waiting for
518 something else to finish. This routine assumes that interrupts are
519 turned off when entering the routine. It is the responsibility
520 of the calling code to ensure that this is the case. */
522 Scsi_Cmnd
* request_queueable (struct request
* req
, int index
)
524 Scsi_Cmnd
* SCpnt
= NULL
;
526 struct buffer_head
* bh
;
528 if ((index
< 0) || (index
> NR_SCSI_DEVICES
))
529 panic ("Index number in allocate_device() is out of range.\n");
531 if (req
&& req
->dev
<= 0)
532 panic("Invalid device in allocate_device");
534 SCpnt
= scsi_devices
[index
].host
->host_queue
;
536 if(SCpnt
->target
== scsi_devices
[index
].id
&&
537 SCpnt
->lun
== scsi_devices
[index
].lun
)
538 if(SCpnt
->request
.dev
< 0) break;
542 if (!SCpnt
) return NULL
;
544 if (scsi_devices
[index
].host
->hostt
->can_queue
545 && scsi_devices
[index
].host
->host_busy
>= scsi_devices
[index
].host
->hostt
->can_queue
) return NULL
;
548 memcpy(&SCpnt
->request
, req
, sizeof(struct request
));
549 tablesize
= scsi_devices
[index
].host
->sg_tablesize
;
551 if(!tablesize
) bh
= NULL
;
552 /* Take a quick look through the table to see how big it is. We already
553 have our copy of req, so we can mess with that if we want to. */
554 while(req
->nr_sectors
&& bh
){
556 req
->nr_sectors
-= bh
->b_size
>> 9;
557 req
->sector
+= bh
->b_size
>> 9;
558 if(!tablesize
) break;
561 if(req
->nr_sectors
&& bh
&& bh
->b_reqnext
){ /* Any leftovers? */
562 SCpnt
->request
.bhtail
= bh
;
563 req
->bh
= bh
->b_reqnext
; /* Divide request */
564 bh
->b_reqnext
= NULL
;
567 /* Now reset things so that req looks OK */
568 SCpnt
->request
.nr_sectors
-= req
->nr_sectors
;
569 req
->current_nr_sectors
= bh
->b_size
>> 9;
570 req
->buffer
= bh
->b_data
;
571 SCpnt
->request
.waiting
= NULL
; /* Wait until whole thing done */
576 SCpnt
->request
.dev
= 0xffff; /* Busy, but no request */
577 SCpnt
->request
.waiting
= NULL
; /* And no one is waiting for the device either */
580 SCpnt
->use_sg
= 0; /* Reset the scatter-gather flag */
581 SCpnt
->old_use_sg
= 0;
582 SCpnt
->transfersize
= 0;
583 SCpnt
->underflow
= 0;
587 /* This function returns a structure pointer that will be valid for
588 the device. The wait parameter tells us whether we should wait for
589 the unit to become free or not. We are also able to tell this routine
590 not to return a descriptor if the host is unable to accept any more
591 commands for the time being. We need to keep in mind that there is no
592 guarantee that the host remain not busy. Keep in mind the
593 request_queueable function also knows the internal allocation scheme
594 of the packets for each device */
596 Scsi_Cmnd
* allocate_device (struct request
** reqp
, int index
, int wait
)
599 struct request
* req
= NULL
;
601 struct buffer_head
* bh
;
602 struct Scsi_Host
* host
;
603 Scsi_Cmnd
* SCpnt
= NULL
;
604 Scsi_Cmnd
* SCwait
= NULL
;
606 if ((index
< 0) || (index
> NR_SCSI_DEVICES
))
607 panic ("Index number in allocate_device() is out of range.\n");
609 if (reqp
) req
= *reqp
;
611 /* See if this request has already been queued by an interrupt routine */
612 if (req
&& (dev
= req
->dev
) <= 0) return NULL
;
614 host
= scsi_devices
[index
].host
;
617 SCpnt
= host
->host_queue
;
619 if(SCpnt
->target
== scsi_devices
[index
].id
&&
620 SCpnt
->lun
== scsi_devices
[index
].lun
) {
622 if(SCpnt
->request
.dev
< 0) break;
627 /* See if this request has already been queued by an interrupt routine */
628 if (req
&& ((req
->dev
< 0) || (req
->dev
!= dev
))) {
632 if (!SCpnt
|| SCpnt
->request
.dev
>= 0) /* Might have changed */
635 if(!wait
) return NULL
;
637 printk("Attempt to allocate device index %d, target %d, lun %d\n",
638 index
, scsi_devices
[index
].id
,scsi_devices
[index
].lun
);
639 panic("No device found in allocate_device\n");
641 SCSI_SLEEP(&scsi_devices
[SCwait
->index
].device_wait
,
642 (SCwait
->request
.dev
> 0));
645 memcpy(&SCpnt
->request
, req
, sizeof(struct request
));
646 tablesize
= scsi_devices
[index
].host
->sg_tablesize
;
648 if(!tablesize
) bh
= NULL
;
649 /* Take a quick look through the table to see how big it is. We already
650 have our copy of req, so we can mess with that if we want to. */
651 while(req
->nr_sectors
&& bh
){
653 req
->nr_sectors
-= bh
->b_size
>> 9;
654 req
->sector
+= bh
->b_size
>> 9;
655 if(!tablesize
) break;
658 if(req
->nr_sectors
&& bh
&& bh
->b_reqnext
){ /* Any leftovers? */
659 SCpnt
->request
.bhtail
= bh
;
660 req
->bh
= bh
->b_reqnext
; /* Divide request */
661 bh
->b_reqnext
= NULL
;
663 /* Now reset things so that req looks OK */
664 SCpnt
->request
.nr_sectors
-= req
->nr_sectors
;
665 req
->current_nr_sectors
= bh
->b_size
>> 9;
666 req
->buffer
= bh
->b_data
;
667 SCpnt
->request
.waiting
= NULL
; /* Wait until whole thing done */
675 SCpnt
->request
.dev
= 0xffff; /* Busy */
676 SCpnt
->request
.waiting
= NULL
; /* And no one is waiting for this to complete */
683 SCpnt
->use_sg
= 0; /* Reset the scatter-gather flag */
684 SCpnt
->old_use_sg
= 0;
685 SCpnt
->transfersize
= 0; /* No default transfer size */
686 SCpnt
->underflow
= 0; /* Do not flag underflow conditions */
691 This is inline because we have stack problemes if we recurse to deeply.
694 inline void internal_cmnd (Scsi_Cmnd
* SCpnt
)
697 struct Scsi_Host
* host
;
702 if ((unsigned long) &SCpnt
< current
->kernel_stack_page
)
703 panic("Kernel stack overflow.");
708 We will wait MIN_RESET_DELAY clock ticks after the last reset so
709 we can avoid the drive not being ready.
711 temp
= host
->last_reset
;
712 while (jiffies
< temp
);
714 update_timeout(SCpnt
, SCpnt
->timeout_per_command
);
717 We will use a queued command if possible, otherwise we will emulate the
718 queing and calling of completion function ourselves.
721 printk("internal_cmnd (host = %d, target = %d, command = %08x, buffer = %08x, \n"
722 "bufflen = %d, done = %08x)\n", SCpnt
->host
->host_no
, SCpnt
->target
, SCpnt
->cmnd
, SCpnt
->buffer
, SCpnt
->bufflen
, SCpnt
->done
);
725 if (host
->hostt
->can_queue
)
728 printk("queuecommand : routine at %08x\n",
729 host
->hostt
->queuecommand
);
731 host
->hostt
->queuecommand (SCpnt
, scsi_done
);
737 printk("command() : routine at %08x\n", host
->hostt
->command
);
739 temp
=host
->hostt
->command (SCpnt
);
740 SCpnt
->result
= temp
;
742 clock
= jiffies
+ 400;
743 while (jiffies
< clock
);
744 printk("done(host = %d, result = %04x) : routine at %08x\n", host
->host_no
, temp
, done
);
749 printk("leaving internal_cmnd()\n");
753 static void scsi_request_sense (Scsi_Cmnd
* SCpnt
)
756 SCpnt
->flags
|= WAS_SENSE
| ASKED_FOR_SENSE
;
757 update_timeout(SCpnt
, SENSE_TIMEOUT
);
761 memcpy ((void *) SCpnt
->cmnd
, (void *) generic_sense
,
762 sizeof(generic_sense
));
764 SCpnt
->cmnd
[1] = SCpnt
->lun
<< 5;
765 SCpnt
->cmnd
[4] = sizeof(SCpnt
->sense_buffer
);
767 SCpnt
->request_buffer
= &SCpnt
->sense_buffer
;
768 SCpnt
->request_bufflen
= sizeof(SCpnt
->sense_buffer
);
770 internal_cmnd (SCpnt
);
771 SCpnt
->use_sg
= SCpnt
->old_use_sg
;
777 scsi_do_cmd sends all the commands out to the low-level driver. It
778 handles the specifics required for each low level driver - ie queued
779 or non queud. It also prevents conflicts when different high level
780 drivers go for the same host at the same time.
783 void scsi_do_cmd (Scsi_Cmnd
* SCpnt
, const void *cmnd
,
784 void *buffer
, unsigned bufflen
, void (*done
)(Scsi_Cmnd
*),
785 int timeout
, int retries
788 struct Scsi_Host
* host
= SCpnt
->host
;
793 int target
= SCpnt
->target
;
794 printk ("scsi_do_cmd (host = %d, target = %d, buffer =%08x, "
795 "bufflen = %d, done = %08x, timeout = %d, retries = %d)\n"
796 "command : " , host
->host_no
, target
, buffer
, bufflen
, done
, timeout
, retries
);
797 for (i
= 0; i
< 10; ++i
)
798 printk ("%02x ", ((unsigned char *) cmnd
)[i
]);
805 panic ("Invalid or not present host. %d\n", host
->host_no
);
810 We must prevent reentrancy to the lowlevel host driver. This prevents
811 it - we enter a loop until the host we want to talk to is not busy.
812 Race conditions are prevented, as interrupts are disabled inbetween the
813 time we check for the host being not busy, and the time we mark it busy
819 if (host
->hostt
->can_queue
820 && host
->host_busy
>= host
->hostt
->can_queue
)
823 SCSI_SLEEP(&host
->host_wait
,
824 (host
->host_busy
>= host
->hostt
->can_queue
));
832 Our own function scsi_done (which marks the host as not busy, disables
833 the timeout counter, etc) will be called by us or by the
834 scsi_hosts[host].queuecommand() function needs to also call
835 the completion function for the high level driver.
839 memcpy ((void *) SCpnt
->data_cmnd
, (void *) cmnd
, 12);
842 SCpnt
->target
= target
;
843 SCpnt
->lun
= (SCpnt
->data_cmnd
[1] >> 5);
845 SCpnt
->bufflen
= bufflen
;
846 SCpnt
->buffer
= buffer
;
849 SCpnt
->allowed
=retries
;
851 SCpnt
->timeout_per_command
= timeout
;
853 memcpy ((void *) SCpnt
->cmnd
, (void *) cmnd
, 12);
854 /* Zero the sense buffer. Some host adapters automatically request
855 sense on error. 0 is not a valid sense code. */
856 memset ((void *) SCpnt
->sense_buffer
, 0, sizeof SCpnt
->sense_buffer
);
857 SCpnt
->request_buffer
= buffer
;
858 SCpnt
->request_bufflen
= bufflen
;
859 SCpnt
->old_use_sg
= SCpnt
->use_sg
;
861 /* Start the timer ticking. */
863 SCpnt
->internal_timeout
= 0;
864 internal_cmnd (SCpnt
);
867 printk ("Leaving scsi_do_cmd()\n");
874 The scsi_done() function disables the timeout timer for the scsi host,
875 marks the host as not busy, and calls the user specified completion
876 function for that host's current command.
879 static void reset (Scsi_Cmnd
* SCpnt
)
882 printk("scsi: reset(%d)\n", SCpnt
->host
->host_no
);
885 SCpnt
->flags
|= (WAS_RESET
| IS_RESETTING
);
889 printk("performing request sense\n");
892 if(SCpnt
->flags
& NEEDS_JUMPSTART
) {
893 SCpnt
->flags
&= ~NEEDS_JUMPSTART
;
894 scsi_request_sense (SCpnt
);
900 static int check_sense (Scsi_Cmnd
* SCpnt
)
902 /* If there is no sense information, request it. If we have already
903 requested it, there is no point in asking again - the firmware must be
905 if (((SCpnt
->sense_buffer
[0] & 0x70) >> 4) != 7) {
906 if(!(SCpnt
->flags
& ASKED_FOR_SENSE
))
907 return SUGGEST_SENSE
;
909 return SUGGEST_RETRY
;
912 SCpnt
->flags
&= ~ASKED_FOR_SENSE
;
915 printk("scsi%d : ", SCpnt
->host
->host_no
);
916 print_sense("", SCpnt
);
919 if (SCpnt
->sense_buffer
[2] &0xe0)
920 return SUGGEST_ABORT
;
922 switch (SCpnt
->sense_buffer
[2] & 0xf)
926 case RECOVERED_ERROR
:
927 if (scsi_devices
[SCpnt
->index
].type
== TYPE_TAPE
)
928 return SUGGEST_IS_OK
;
932 case ABORTED_COMMAND
:
933 return SUGGEST_RETRY
;
936 return SUGGEST_ABORT
;
938 /* these three are not supported */
940 case VOLUME_OVERFLOW
:
944 return SUGGEST_REMAP
;
948 case ILLEGAL_REQUEST
:
950 return SUGGEST_ABORT
;
954 /* This function is the mid-level interrupt routine, which decides how
955 * to handle error conditions. Each invocation of this function must
956 * do one and *only* one of the following:
958 * (1) Call last_cmnd[host].done. This is done for fatal errors and
959 * normal completion, and indicates that the handling for this
960 * request is complete.
961 * (2) Call internal_cmnd to requeue the command. This will result in
962 * scsi_done being called again when the retry is complete.
963 * (3) Call scsi_request_sense. This asks the host adapter/drive for
964 * more information about the error condition. When the information
965 * is available, scsi_done will be called again.
966 * (4) Call reset(). This is sort of a last resort, and the idea is that
967 * this may kick things loose and get the drive working again. reset()
968 * automatically calls scsi_request_sense, and thus scsi_done will be
969 * called again once the reset is complete.
971 * If none of the above actions are taken, the drive in question
972 * will hang. If more than one of the above actions are taken by
973 * scsi_done, then unpredictable behavior will result.
975 static void scsi_done (Scsi_Cmnd
* SCpnt
)
981 struct Scsi_Host
* host
= SCpnt
->host
;
982 int result
= SCpnt
->result
;
983 oldto
= update_timeout(SCpnt
, 0);
991 printk("In scsi_done(host = %d, result = %06x)\n", host
->host_no
, result
);
993 switch (host_byte(result
))
996 if (SCpnt
->flags
& IS_RESETTING
)
998 SCpnt
->flags
&= ~IS_RESETTING
;
1003 if (status_byte(result
) && (SCpnt
->flags
& WAS_SENSE
))
1004 /* Failed to obtain sense information */
1006 SCpnt
->flags
&= ~WAS_SENSE
;
1007 SCpnt
->internal_timeout
&= ~SENSE_TIMEOUT
;
1009 if (!(SCpnt
->flags
& WAS_RESET
))
1011 printk("scsi%d : target %d lun %d request sense failed, performing reset.\n",
1012 SCpnt
->host
->host_no
, SCpnt
->target
, SCpnt
->lun
);
1018 exit
= (DRIVER_HARD
| SUGGEST_ABORT
);
1022 else switch(msg_byte(result
))
1024 case COMMAND_COMPLETE
:
1025 switch (status_byte(result
))
1028 if (SCpnt
->flags
& WAS_SENSE
)
1031 printk ("In scsi_done, GOOD status, COMMAND COMPLETE, parsing sense information.\n");
1034 SCpnt
->flags
&= ~WAS_SENSE
;
1035 SCpnt
->internal_timeout
&= ~SENSE_TIMEOUT
;
1037 switch (checked
= check_sense(SCpnt
))
1042 printk("NO SENSE. status = REDO\n");
1045 update_timeout(SCpnt
, oldto
);
1053 printk("SENSE SUGGEST REMAP or SUGGEST RETRY - status = MAYREDO\n");
1057 exit
= DRIVER_SENSE
| SUGGEST_RETRY
;
1061 printk("SENSE SUGGEST ABORT - status = FINISHED");
1065 exit
= DRIVER_SENSE
| SUGGEST_ABORT
;
1068 printk ("Internal error %s %d \n", __FILE__
,
1075 printk("COMMAND COMPLETE message returned, status = FINISHED. \n");
1083 case CHECK_CONDITION
:
1084 switch (check_sense(SCpnt
))
1087 update_timeout(SCpnt
, oldto
);
1093 exit
= DRIVER_SENSE
| SUGGEST_RETRY
;
1097 exit
= DRIVER_SENSE
| SUGGEST_ABORT
;
1100 scsi_request_sense (SCpnt
);
1106 case CONDITION_GOOD
:
1107 case INTERMEDIATE_GOOD
:
1108 case INTERMEDIATE_C_GOOD
:
1112 update_timeout(SCpnt
, oldto
);
1116 case RESERVATION_CONFLICT
:
1117 printk("scsi%d : RESERVATION CONFLICT performing reset.\n",
1118 SCpnt
->host
->host_no
);
1122 exit
= DRIVER_SOFT
| SUGGEST_ABORT
;
1127 printk ("Internal error %s %d \n"
1128 "status byte = %d \n", __FILE__
,
1129 __LINE__
, status_byte(result
));
1134 panic("scsi: unsupported message byte %d recieved\n", msg_byte(result
));
1139 printk("Host returned DID_TIME_OUT - ");
1142 if (SCpnt
->flags
& WAS_TIMEDOUT
)
1145 printk("Aborting\n");
1147 exit
= (DRIVER_TIMEOUT
| SUGGEST_ABORT
);
1152 printk ("Retrying.\n");
1154 SCpnt
->flags
|= WAS_TIMEDOUT
;
1162 case DID_NO_CONNECT
:
1164 printk("Couldn't connect.\n");
1166 exit
= (DRIVER_HARD
| SUGGEST_ABORT
);
1170 exit
= (DRIVER_HARD
| SUGGEST_ABORT
);
1172 case DID_BAD_TARGET
:
1174 exit
= (DRIVER_INVALID
| SUGGEST_ABORT
);
1177 if(msg_byte(result
) == GOOD
&&
1178 status_byte(result
) == CHECK_CONDITION
) {
1179 switch (check_sense(SCpnt
)) {
1181 update_timeout(SCpnt
, oldto
);
1187 exit
= DRIVER_SENSE
| SUGGEST_RETRY
;
1191 exit
= DRIVER_SENSE
| SUGGEST_ABORT
;
1194 scsi_request_sense (SCpnt
);
1200 exit
= SUGGEST_RETRY
;
1204 exit
= (DRIVER_ERROR
| SUGGEST_DIE
);
1215 printk("In MAYREDO, allowing %d retries, have %d\n",
1216 SCpnt
->allowed
, SCpnt
->retries
);
1219 if ((++SCpnt
->retries
) < SCpnt
->allowed
)
1221 if ((SCpnt
->retries
>= (SCpnt
->allowed
>> 1))
1222 && !(SCpnt
->flags
& WAS_RESET
))
1224 printk("scsi%d : reseting for second half of retries.\n",
1225 SCpnt
->host
->host_no
);
1236 /* fall through to REDO */
1239 if (SCpnt
->flags
& WAS_SENSE
)
1240 scsi_request_sense(SCpnt
);
1243 memcpy ((void *) SCpnt
->cmnd
,
1244 (void*) SCpnt
->data_cmnd
,
1245 sizeof(SCpnt
->data_cmnd
));
1246 SCpnt
->request_buffer
= SCpnt
->buffer
;
1247 SCpnt
->request_bufflen
= SCpnt
->bufflen
;
1248 SCpnt
->use_sg
= SCpnt
->old_use_sg
;
1249 internal_cmnd (SCpnt
);
1256 if (status
== FINISHED
)
1259 printk("Calling done function - at address %08x\n", SCpnt
->done
);
1261 host
->host_busy
--; /* Indicate that we are free */
1262 wake_up(&host
->host_wait
);
1263 SCpnt
->result
= result
| ((exit
& 0xff) << 24);
1264 SCpnt
->use_sg
= SCpnt
->old_use_sg
;
1265 SCpnt
->done (SCpnt
);
1276 The scsi_abort function interfaces with the abort() function of the host
1277 we are aborting, and causes the current command to not complete. The
1278 caller should deal with any error messages or status returned on the
1281 This will not be called rentrantly for a given host.
1285 Since we're nice guys and specified that abort() and reset()
1286 can be non-reentrant. The internal_timeout flags are used for
1291 int scsi_abort (Scsi_Cmnd
* SCpnt
, int why
)
1294 struct Scsi_Host
* host
= SCpnt
->host
;
1299 if (SCpnt
->internal_timeout
& IN_ABORT
)
1302 while (SCpnt
->internal_timeout
& IN_ABORT
);
1306 SCpnt
->internal_timeout
|= IN_ABORT
;
1307 oldto
= update_timeout(SCpnt
, ABORT_TIMEOUT
);
1311 if (!host
->host_busy
|| !host
->hostt
->abort(SCpnt
, why
))
1317 SCpnt
->internal_timeout
&= ~IN_ABORT
;
1318 update_timeout(SCpnt
, oldto
);
1325 int scsi_reset (Scsi_Cmnd
* SCpnt
)
1329 struct Scsi_Host
* host
= SCpnt
->host
;
1332 printk("Danger Will Robinson! - SCSI bus for host %d is being reset.\n",host
->host_no
);
1336 if (SCpnt
->internal_timeout
& IN_RESET
)
1339 while (SCpnt
->internal_timeout
& IN_RESET
);
1343 SCpnt
->internal_timeout
|= IN_RESET
;
1344 oldto
= update_timeout(SCpnt
, RESET_TIMEOUT
);
1346 if (host
->host_busy
)
1349 SCpnt1
= host
->host_queue
;
1351 if ((SCpnt1
->request
.dev
> 0) &&
1352 !(SCpnt1
->flags
& IS_RESETTING
) &&
1353 !(SCpnt1
->internal_timeout
& IN_ABORT
))
1354 scsi_abort(SCpnt1
, DID_RESET
);
1355 SCpnt1
= SCpnt1
->next
;
1358 temp
= host
->hostt
->reset(SCpnt
);
1365 temp
= host
->hostt
->reset(SCpnt
);
1366 host
->last_reset
= jiffies
;
1371 SCpnt
->internal_timeout
&= ~IN_RESET
;
1372 update_timeout(SCpnt
, oldto
);
1380 static void scsi_main_timeout(void)
1383 We must not enter update_timeout with a timeout condition still pending.
1387 struct Scsi_Host
* host
;
1388 Scsi_Cmnd
* SCpnt
= NULL
;
1394 Find all timers such that they have 0 or negative (shouldn't happen)
1395 time remaining on them.
1399 for(host
= scsi_hostlist
; host
; host
= host
->next
) {
1400 SCpnt
= host
->host_queue
;
1402 if (SCpnt
->timeout
> 0 && SCpnt
->timeout
<= time_elapsed
)
1406 scsi_times_out(SCpnt
);
1410 SCpnt
= SCpnt
->next
;
1413 update_timeout(NULL
, 0);
1414 } while (timed_out
);
1419 The strategy is to cause the timer code to call scsi_times_out()
1420 when the soonest timeout is pending.
1421 The arguments are used when we are queueing a new command, because
1422 we do not want to subtract the time used from this time, but when we
1423 set the timer, we want to take this value into account.
1426 static int update_timeout(Scsi_Cmnd
* SCset
, int timeout
)
1428 unsigned int least
, used
;
1430 struct Scsi_Host
* host
;
1431 Scsi_Cmnd
* SCpnt
= NULL
;
1436 Figure out how much time has passed since the last time the timeouts
1439 used
= (time_start
) ? (jiffies
- time_start
) : 0;
1442 Find out what is due to timeout soonest, and adjust all timeouts for
1443 the amount of time that has passed since the last time we called
1450 oldto
= SCset
->timeout
- used
;
1451 SCset
->timeout
= timeout
+ used
;
1456 for(host
= scsi_hostlist
; host
; host
= host
->next
) {
1457 SCpnt
= host
->host_queue
;
1459 if (SCpnt
->timeout
> 0 && (SCpnt
->timeout
-= used
) < least
)
1460 least
= SCpnt
->timeout
;
1461 SCpnt
= SCpnt
->next
;
1466 If something is due to timeout again, then we will set the next timeout
1467 interrupt to occur. Otherwise, timeouts are disabled.
1470 if (least
!= 0xffffffff)
1472 time_start
= jiffies
;
1473 timer_table
[SCSI_TIMER
].expires
= (time_elapsed
= least
) + jiffies
;
1474 timer_active
|= 1 << SCSI_TIMER
;
1478 timer_table
[SCSI_TIMER
].expires
= time_start
= time_elapsed
= 0;
1479 timer_active
&= ~(1 << SCSI_TIMER
);
1486 static unsigned short * dma_malloc_freelist
= NULL
;
1487 static unsigned int dma_sectors
= 0;
1488 unsigned int dma_free_sectors
= 0;
1489 unsigned int need_isa_buffer
= 0;
1490 static unsigned char * dma_malloc_buffer
= NULL
;
1492 void *scsi_malloc(unsigned int len
)
1494 unsigned int nbits
, mask
;
1496 if((len
& 0x1ff) || len
> 4096)
1497 panic("Inappropriate buffer size requested");
1501 mask
= (1 << nbits
) - 1;
1503 for(i
=0;i
< (dma_sectors
>> 4); i
++)
1504 for(j
=0; j
<17-nbits
; j
++){
1505 if ((dma_malloc_freelist
[i
] & (mask
<< j
)) == 0){
1506 dma_malloc_freelist
[i
] |= (mask
<< j
);
1508 dma_free_sectors
-= nbits
;
1510 printk("SMalloc: %d %x ",len
, dma_malloc_buffer
+ (i
<< 13) + (j
<< 9));
1512 return (void *) ((unsigned long) dma_malloc_buffer
+ (i
<< 13) + (j
<< 9));
1516 return NULL
; /* Nope. No more */
1519 int scsi_free(void *obj
, unsigned int len
)
1522 int page
, sector
, nbits
, mask
;
1525 printk("Sfree %x %d\n",obj
, len
);
1528 offset
= ((int) obj
) - ((int) dma_malloc_buffer
);
1530 if (offset
< 0) panic("Bad offset");
1531 page
= offset
>> 13;
1532 sector
= offset
>> 9;
1533 if(sector
>= dma_sectors
) panic ("Bad page");
1535 sector
= (offset
>> 9) & 15;
1537 mask
= (1 << nbits
) - 1;
1539 if ((mask
<< sector
) > 0xffff) panic ("Bad memory alignment");
1542 if(dma_malloc_freelist
[page
] & (mask
<< sector
) != (mask
<<sector
))
1543 panic("Trying to free unused memory");
1545 dma_free_sectors
+= nbits
;
1546 dma_malloc_freelist
[page
] &= ~(mask
<< sector
);
1552 scsi_dev_init() is our initialization routine, which inturn calls host
1553 initialization, bus scanning, and sd/st initialization routines. It
1554 should be called from main().
1557 unsigned long scsi_dev_init (unsigned long memory_start
,unsigned long memory_end
)
1560 struct Scsi_Host
* host
;
1565 timer_table
[SCSI_TIMER
].fn
= scsi_main_timeout
;
1566 timer_table
[SCSI_TIMER
].expires
= 0;
1568 /* initialize all hosts */
1569 memory_start
= scsi_init(memory_start
, memory_end
);
1571 scsi_devices
= (Scsi_Device
*) memory_start
;
1572 scan_scsis(); /* scan for scsi devices */
1573 memory_start
+= NR_SCSI_DEVICES
* sizeof(Scsi_Device
);
1575 memory_start
= sd_init1(memory_start
, memory_end
);
1576 memory_start
= st_init1(memory_start
, memory_end
);
1577 memory_start
= sr_init1(memory_start
, memory_end
);
1578 memory_start
= sg_init1(memory_start
, memory_end
);
1580 last_cmnd
= (Scsi_Cmnd
*) memory_start
;
1584 for (i
=0; i
< NR_SCSI_DEVICES
; i
++) {
1586 switch (scsi_devices
[i
].type
)
1589 st_attach(&scsi_devices
[i
]);
1592 sr_attach(&scsi_devices
[i
]);
1596 sd_attach(&scsi_devices
[i
]);
1600 sg_attach(&scsi_devices
[i
]);
1601 if(scsi_devices
[i
].type
!= -1){
1602 for(j
=0;j
<scsi_devices
[i
].host
->hostt
->cmd_per_lun
;j
++){
1603 SCpnt
->host
= scsi_devices
[i
].host
;
1604 SCpnt
->target
= scsi_devices
[i
].id
;
1605 SCpnt
->lun
= scsi_devices
[i
].lun
;
1607 SCpnt
->request
.dev
= -1; /* Mark not busy */
1609 SCpnt
->old_use_sg
= 0;
1610 SCpnt
->underflow
= 0;
1611 SCpnt
->transfersize
= 0;
1612 SCpnt
->host_scribble
= NULL
;
1613 host
= scsi_devices
[i
].host
;
1614 if(host
->host_queue
)
1615 host
->host_queue
->prev
= SCpnt
;
1616 SCpnt
->next
= host
->host_queue
;
1618 host
->host_queue
= SCpnt
;
1624 memory_start
= (int) SCpnt
;
1626 if (NR_SD
> 0 || NR_SR
> 0 || NR_ST
> 0)
1627 dma_sectors
= 16; /* Base value we use */
1629 for (i
= 0; i
< NR_SCSI_DEVICES
; ++i
) {
1630 struct Scsi_Host
* host
;
1631 host
= scsi_devices
[i
].host
;
1633 if(scsi_devices
[i
].type
!= TYPE_TAPE
)
1634 dma_sectors
+= ((host
->sg_tablesize
*
1635 sizeof(struct scatterlist
) + 511) >> 9) *
1636 host
->hostt
->cmd_per_lun
;
1638 if(host
->unchecked_isa_dma
&&
1639 memory_end
> ISA_DMA_THRESHOLD
&&
1640 scsi_devices
[i
].type
!= TYPE_TAPE
) {
1641 dma_sectors
+= (PAGE_SIZE
>> 9) * host
->sg_tablesize
*
1642 host
->hostt
->cmd_per_lun
;
1647 dma_sectors
= (dma_sectors
+ 15) & 0xfff0;
1648 dma_free_sectors
= dma_sectors
; /* This must be a multiple of 16 */
1650 memory_start
= (memory_start
+ 3) & 0xfffffffc;
1651 dma_malloc_freelist
= (unsigned short *) memory_start
;
1652 memory_start
+= dma_sectors
>> 3;
1653 memset(dma_malloc_freelist
, 0, dma_sectors
>> 3);
1655 if(memory_start
& 1) memory_start
++; /* Some host adapters require
1656 buffers to be word aligned */
1657 dma_malloc_buffer
= (unsigned char *) memory_start
;
1658 memory_start
+= dma_sectors
<< 9;
1660 memory_start
= sd_init(memory_start
, memory_end
); /* init scsi disks */
1661 memory_start
= st_init(memory_start
, memory_end
); /* init scsi tapes */
1662 memory_start
= sr_init(memory_start
, memory_end
); /* init scsi CDROMs */
1663 memory_start
= sg_init(memory_start
, memory_end
); /* init scsi generic */
1665 return memory_start
;
1668 static void print_inquiry(unsigned char *data
)
1672 printk(" Vendor: ");
1673 for (i
= 8; i
< 16; i
++)
1675 if (data
[i
] >= 0x20 && i
< data
[4] + 5)
1676 printk("%c", data
[i
]);
1682 for (i
= 16; i
< 32; i
++)
1684 if (data
[i
] >= 0x20 && i
< data
[4] + 5)
1685 printk("%c", data
[i
]);
1691 for (i
= 32; i
< 36; i
++)
1693 if (data
[i
] >= 0x20 && i
< data
[4] + 5)
1694 printk("%c", data
[i
]);
1703 printk(" Type: %s ",
1704 i
< MAX_SCSI_DEVICE_CODE
? scsi_device_types
[i
] : "Unknown " );
1705 printk(" ANSI SCSI revision: %02x", data
[2] & 0x07);
1706 if ((data
[2] & 0x07) == 1 && (data
[3] & 0x0f) == 1)