* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-0.99 / drivers / scsi / scsi.c
bloba2083b8278783f0553b07aa85fb1001e133269ba
1 /*
2 * scsi.c Copyright (C) 1992 Drew Eckhardt
3 * generic mid-level SCSI driver by
4 * Drew Eckhardt
6 * <drew@colorado.edu>
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
15 * enhancements.
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"
24 #include "scsi.h"
25 #include "hosts.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] =
48 "Direct-Access ",
49 "Sequential-Access",
50 "Printer ",
51 "Processor ",
52 "WORM ",
53 "CD-ROM ",
54 "Scanner ",
55 "Optical Device ",
56 "Medium Changer ",
57 "Communications "
62 global variables :
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
65 (host, id, LUN)
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
97 * respectively.
100 #ifdef DEBUG
101 #define SCSI_TIMEOUT 500
102 #else
103 #define SCSI_TIMEOUT 100
104 #endif
106 #ifdef DEBUG
107 #define SENSE_TIMEOUT SCSI_TIMEOUT
108 #define ABORT_TIMEOUT SCSI_TIMEOUT
109 #define RESET_TIMEOUT SCSI_TIMEOUT
110 #else
111 #define SENSE_TIMEOUT 50
112 #define RESET_TIMEOUT 50
113 #define ABORT_TIMEOUT 50
114 #define MIN_RESET_DELAY 100
115 #endif
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
119 lock up. */
121 struct blist{
122 char * vendor;
123 char * model;
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.*/
141 {NULL, NULL, NULL}};
143 static int blacklisted(unsigned char * response_data){
144 int i = 0;
145 unsigned char * pnt;
146 for(i=0; 1; i++){
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;
156 return 1;
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)
174 #ifdef DEBUG
175 printk ("scan_scsis_done(%d, %06x)\n", SCpnt->host, SCpnt->result);
176 #endif
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)
189 int dev, lun, type;
190 unsigned char scsi_cmd [12];
191 unsigned char scsi_result [256];
192 struct Scsi_Host * shpnt;
193 Scsi_Cmnd SCmd;
195 ++in_scan;
196 lun = 0;
198 SCmd.next = NULL;
199 SCmd.prev = NULL;
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.
210 #ifdef NO_MULTI_LUN
211 for (lun = 0; lun < 1; ++lun)
212 #else
213 for (lun = 0; lun < 8; ++lun)
214 #endif
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;
230 scsi_cmd[4] = 0;
232 SCmd.host = shpnt;
233 SCmd.target = dev;
234 SCmd.lun = lun;
236 SCmd.request.dev = 0xffff; /* Mark not busy */
237 SCmd.use_sg = 0;
238 SCmd.old_use_sg = 0;
239 SCmd.transfersize = 0;
240 SCmd.underflow = 0;
241 SCmd.index = NR_SCSI_DEVICES;
243 scsi_do_cmd (&SCmd,
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);
252 #endif
255 if(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))
262 continue;
264 else
265 break;
268 #if defined (DEBUG) || defined(DEBUG_INIT)
269 printk("scsi: performing INQUIRY\n");
270 #endif
273 * Build an INQUIRY command block.
276 scsi_cmd[0] = INQUIRY;
277 scsi_cmd[1] = (lun << 5) & 0xe0;
278 scsi_cmd[2] = 0;
279 scsi_cmd[3] = 0;
280 scsi_cmd[4] = 255;
281 scsi_cmd[5] = 0;
283 SCmd.request.dev = 0xffff; /* Mark not busy */
285 scsi_do_cmd (&SCmd,
286 (void *) scsi_cmd, (void *)
287 scsi_result, 256, scan_scsis_done,
288 SCSI_TIMEOUT, 3);
290 while (SCmd.request.dev != 0xfffe);
292 the_result = SCmd.result;
294 #if defined(DEBUG) || defined(DEBUG_INIT)
295 if (!the_result)
296 printk("scsi: INQUIRY successful\n");
297 else
298 printk("scsi: INQUIRY failed with code %08x\n");
299 #endif
301 if(the_result) break;
303 /* skip other luns on this device */
305 if (!the_result)
307 scsi_devices[NR_SCSI_DEVICES].
308 removable = (0x80 &
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].
313 changed = 0;
314 scsi_devices[NR_SCSI_DEVICES].
315 access_count = 0;
316 scsi_devices[NR_SCSI_DEVICES].
317 busy = 0;
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])
326 case TYPE_TAPE :
327 case TYPE_DISK :
328 case TYPE_MOD :
329 scsi_devices[NR_SCSI_DEVICES].writeable = 1;
330 break;
331 case TYPE_WORM :
332 case TYPE_ROM :
333 scsi_devices[NR_SCSI_DEVICES].writeable = 0;
334 break;
335 default :
336 #if 0
337 #ifdef DEBUG
338 printk("scsi: unknown type %d\n", type);
339 print_inquiry(scsi_result);
340 #endif
341 #endif
342 type = -1;
345 scsi_devices[NR_SCSI_DEVICES].random =
346 (type == TYPE_TAPE) ? 0 : 1;
347 scsi_devices[NR_SCSI_DEVICES].type = type;
349 if (type != -1)
351 print_inquiry(scsi_result);
352 switch(type){
353 case TYPE_TAPE:
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;
357 break;
358 case TYPE_ROM:
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;
362 break;
363 case TYPE_DISK:
364 case TYPE_MOD:
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;
368 break;
369 default:
370 break;
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
396 * loop.
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
406 * a TEXEL drive.
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.
417 #ifdef notyet
418 || (strncmp("1.06", (char *) &scsi_result[[, 4) != 0)
419 #endif
421 scsi_devices[NR_SCSI_DEVICES].borken = 0;
424 /* These devices need this "key" to unlock the device
425 so we can use it */
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;
433 scsi_cmd[2] = 0x2e;
434 scsi_cmd[3] = 0;
435 scsi_cmd[4] = 0x2a;
436 scsi_cmd[5] = 0;
438 SCmd.request.dev = 0xffff; /* Mark not busy */
440 scsi_do_cmd (&SCmd,
441 (void *) scsi_cmd, (void *)
442 scsi_result, 0x2a, scan_scsis_done,
443 SCSI_TIMEOUT, 3);
445 while (SCmd.request.dev != 0xfffe);
448 ++NR_SCSI_DEVICES;
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 */
458 } /* for lun ends */
459 shpnt->host_queue = NULL; /* No longer needed here */
460 } /* if present */
462 printk("scsi : detected ");
463 if(NR_SD != -1)
464 printk("%d SCSI disk%s ", MAX_SD, (MAX_SD != 1) ? "s" : "");
466 if(NR_ST != -1)
467 printk("%d tape%s ", MAX_ST, (MAX_ST != 1) ? "s" : "");
469 if(NR_SR != -1)
470 printk("%d CD-ROM drive%s ", MAX_SR, (MAX_SR != 1) ? "s" : "");
472 printk("total.\n");
473 in_scan = 0;
474 } /* scan_scsis ends */
477 * Flag bits for the internal_timeout array
480 #define NORMAL_TIMEOUT 0
481 #define IN_ABORT 1
482 #define IN_RESET 2
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))
494 case NORMAL_TIMEOUT:
495 if (!in_scan)
496 printk("SCSI host %d timed out - aborting command\n",
497 SCpnt->host->host_no);
499 if (!scsi_abort (SCpnt, DID_TIME_OUT))
500 return;
501 case IN_ABORT:
502 printk("SCSI host %d abort() timed out - reseting\n",
503 SCpnt->host->host_no);
504 if (!scsi_reset (SCpnt))
505 return;
506 case IN_RESET:
507 case (IN_ABORT | IN_RESET):
508 panic("Unable to reset scsi host %d\n",SCpnt->host->host_no);
509 default:
510 INTERNAL_ERROR;
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;
525 int tablesize;
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;
535 while(SCpnt){
536 if(SCpnt->target == scsi_devices[index].id &&
537 SCpnt->lun == scsi_devices[index].lun)
538 if(SCpnt->request.dev < 0) break;
539 SCpnt = SCpnt->next;
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;
547 if (req) {
548 memcpy(&SCpnt->request, req, sizeof(struct request));
549 tablesize = scsi_devices[index].host->sg_tablesize;
550 bh = req->bh;
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){
555 tablesize--;
556 req->nr_sectors -= bh->b_size >> 9;
557 req->sector += bh->b_size >> 9;
558 if(!tablesize) break;
559 bh = bh->b_reqnext;
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;
565 bh = req->bh;
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 */
572 } else
573 req->dev = -1;
575 } else {
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;
584 return SCpnt;
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)
598 int dev = -1;
599 struct request * req = NULL;
600 int tablesize;
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;
616 while (1==1){
617 SCpnt = host->host_queue;
618 while(SCpnt){
619 if(SCpnt->target == scsi_devices[index].id &&
620 SCpnt->lun == scsi_devices[index].lun) {
621 SCwait = SCpnt;
622 if(SCpnt->request.dev < 0) break;
624 SCpnt = SCpnt->next;
626 cli();
627 /* See if this request has already been queued by an interrupt routine */
628 if (req && ((req->dev < 0) || (req->dev != dev))) {
629 sti();
630 return NULL;
632 if (!SCpnt || SCpnt->request.dev >= 0) /* Might have changed */
634 sti();
635 if(!wait) return NULL;
636 if (!SCwait) {
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));
643 } else {
644 if (req) {
645 memcpy(&SCpnt->request, req, sizeof(struct request));
646 tablesize = scsi_devices[index].host->sg_tablesize;
647 bh = req->bh;
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){
652 tablesize--;
653 req->nr_sectors -= bh->b_size >> 9;
654 req->sector += bh->b_size >> 9;
655 if(!tablesize) break;
656 bh = bh->b_reqnext;
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;
662 bh = req->bh;
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 */
669 else
671 req->dev = -1;
672 *reqp = req->next;
674 } else {
675 SCpnt->request.dev = 0xffff; /* Busy */
676 SCpnt->request.waiting = NULL; /* And no one is waiting for this to complete */
678 sti();
679 break;
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 */
687 return SCpnt;
691 This is inline because we have stack problemes if we recurse to deeply.
694 inline void internal_cmnd (Scsi_Cmnd * SCpnt)
696 int temp;
697 struct Scsi_Host * host;
698 #ifdef DEBUG_DELAY
699 int clock;
700 #endif
702 if ((unsigned long) &SCpnt < current->kernel_stack_page)
703 panic("Kernel stack overflow.");
705 host = SCpnt->host;
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.
720 #ifdef DEBUG
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);
723 #endif
725 if (host->hostt->can_queue)
727 #ifdef DEBUG
728 printk("queuecommand : routine at %08x\n",
729 host->hostt->queuecommand);
730 #endif
731 host->hostt->queuecommand (SCpnt, scsi_done);
733 else
736 #ifdef DEBUG
737 printk("command() : routine at %08x\n", host->hostt->command);
738 #endif
739 temp=host->hostt->command (SCpnt);
740 SCpnt->result = temp;
741 #ifdef DEBUG_DELAY
742 clock = jiffies + 400;
743 while (jiffies < clock);
744 printk("done(host = %d, result = %04x) : routine at %08x\n", host->host_no, temp, done);
745 #endif
746 scsi_done(SCpnt);
748 #ifdef DEBUG
749 printk("leaving internal_cmnd()\n");
750 #endif
753 static void scsi_request_sense (Scsi_Cmnd * SCpnt)
755 cli();
756 SCpnt->flags |= WAS_SENSE | ASKED_FOR_SENSE;
757 update_timeout(SCpnt, SENSE_TIMEOUT);
758 sti();
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);
769 SCpnt->use_sg = 0;
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;
790 #ifdef DEBUG
792 int i;
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]);
799 printk("\n");
801 #endif
803 if (!host)
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
814 ourselves.
817 while (1==1){
818 cli();
819 if (host->hostt->can_queue
820 && host->host_busy >= host->hostt->can_queue)
822 sti();
823 SCSI_SLEEP(&host->host_wait,
824 (host->host_busy >= host->hostt->can_queue));
825 } else {
826 host->host_busy++;
827 sti();
828 break;
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);
840 #if 0
841 SCpnt->host = host;
842 SCpnt->target = target;
843 SCpnt->lun = (SCpnt->data_cmnd[1] >> 5);
844 #endif
845 SCpnt->bufflen = bufflen;
846 SCpnt->buffer = buffer;
847 SCpnt->flags=0;
848 SCpnt->retries=0;
849 SCpnt->allowed=retries;
850 SCpnt->done = done;
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);
866 #ifdef DEBUG
867 printk ("Leaving scsi_do_cmd()\n");
868 #endif
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)
881 #ifdef DEBUG
882 printk("scsi: reset(%d)\n", SCpnt->host->host_no);
883 #endif
885 SCpnt->flags |= (WAS_RESET | IS_RESETTING);
886 scsi_reset(SCpnt);
888 #ifdef DEBUG
889 printk("performing request sense\n");
890 #endif
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
904 confused. */
905 if (((SCpnt->sense_buffer[0] & 0x70) >> 4) != 7) {
906 if(!(SCpnt->flags & ASKED_FOR_SENSE))
907 return SUGGEST_SENSE;
908 else
909 return SUGGEST_RETRY;
912 SCpnt->flags &= ~ASKED_FOR_SENSE;
914 #ifdef DEBUG_INIT
915 printk("scsi%d : ", SCpnt->host->host_no);
916 print_sense("", SCpnt);
917 printk("\n");
918 #endif
919 if (SCpnt->sense_buffer[2] &0xe0)
920 return SUGGEST_ABORT;
922 switch (SCpnt->sense_buffer[2] & 0xf)
924 case NO_SENSE:
925 return 0;
926 case RECOVERED_ERROR:
927 if (scsi_devices[SCpnt->index].type == TYPE_TAPE)
928 return SUGGEST_IS_OK;
929 else
930 return 0;
932 case ABORTED_COMMAND:
933 return SUGGEST_RETRY;
934 case NOT_READY:
935 case UNIT_ATTENTION:
936 return SUGGEST_ABORT;
938 /* these three are not supported */
939 case COPY_ABORTED:
940 case VOLUME_OVERFLOW:
941 case MISCOMPARE:
943 case MEDIUM_ERROR:
944 return SUGGEST_REMAP;
945 case BLANK_CHECK:
946 case DATA_PROTECT:
947 case HARDWARE_ERROR:
948 case ILLEGAL_REQUEST:
949 default:
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)
977 int status=0;
978 int exit=0;
979 int checked;
980 int oldto;
981 struct Scsi_Host * host = SCpnt->host;
982 int result = SCpnt->result;
983 oldto = update_timeout(SCpnt, 0);
985 #define FINISHED 0
986 #define MAYREDO 1
987 #define REDO 3
988 #define PENDING 4
990 #ifdef DEBUG
991 printk("In scsi_done(host = %d, result = %06x)\n", host->host_no, result);
992 #endif
993 switch (host_byte(result))
995 case DID_OK:
996 if (SCpnt->flags & IS_RESETTING)
998 SCpnt->flags &= ~IS_RESETTING;
999 status = REDO;
1000 break;
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);
1013 reset(SCpnt);
1014 return;
1016 else
1018 exit = (DRIVER_HARD | SUGGEST_ABORT);
1019 status = FINISHED;
1022 else switch(msg_byte(result))
1024 case COMMAND_COMPLETE:
1025 switch (status_byte(result))
1027 case GOOD:
1028 if (SCpnt->flags & WAS_SENSE)
1030 #ifdef DEBUG
1031 printk ("In scsi_done, GOOD status, COMMAND COMPLETE, parsing sense information.\n");
1032 #endif
1034 SCpnt->flags &= ~WAS_SENSE;
1035 SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
1037 switch (checked = check_sense(SCpnt))
1039 case SUGGEST_SENSE:
1040 case 0:
1041 #ifdef DEBUG
1042 printk("NO SENSE. status = REDO\n");
1043 #endif
1045 update_timeout(SCpnt, oldto);
1046 status = REDO;
1047 break;
1048 case SUGGEST_IS_OK:
1049 break;
1050 case SUGGEST_REMAP:
1051 case SUGGEST_RETRY:
1052 #ifdef DEBUG
1053 printk("SENSE SUGGEST REMAP or SUGGEST RETRY - status = MAYREDO\n");
1054 #endif
1056 status = MAYREDO;
1057 exit = DRIVER_SENSE | SUGGEST_RETRY;
1058 break;
1059 case SUGGEST_ABORT:
1060 #ifdef DEBUG
1061 printk("SENSE SUGGEST ABORT - status = FINISHED");
1062 #endif
1064 status = FINISHED;
1065 exit = DRIVER_SENSE | SUGGEST_ABORT;
1066 break;
1067 default:
1068 printk ("Internal error %s %d \n", __FILE__,
1069 __LINE__);
1072 else
1074 #ifdef DEBUG
1075 printk("COMMAND COMPLETE message returned, status = FINISHED. \n");
1076 #endif
1078 exit = DRIVER_OK;
1079 status = FINISHED;
1081 break;
1083 case CHECK_CONDITION:
1084 switch (check_sense(SCpnt))
1086 case 0:
1087 update_timeout(SCpnt, oldto);
1088 status = REDO;
1089 break;
1090 case SUGGEST_REMAP:
1091 case SUGGEST_RETRY:
1092 status = MAYREDO;
1093 exit = DRIVER_SENSE | SUGGEST_RETRY;
1094 break;
1095 case SUGGEST_ABORT:
1096 status = FINISHED;
1097 exit = DRIVER_SENSE | SUGGEST_ABORT;
1098 break;
1099 case SUGGEST_SENSE:
1100 scsi_request_sense (SCpnt);
1101 status = PENDING;
1102 break;
1104 break;
1106 case CONDITION_GOOD:
1107 case INTERMEDIATE_GOOD:
1108 case INTERMEDIATE_C_GOOD:
1109 break;
1111 case BUSY:
1112 update_timeout(SCpnt, oldto);
1113 status = REDO;
1114 break;
1116 case RESERVATION_CONFLICT:
1117 printk("scsi%d : RESERVATION CONFLICT performing reset.\n",
1118 SCpnt->host->host_no);
1119 reset(SCpnt);
1120 return;
1121 #if 0
1122 exit = DRIVER_SOFT | SUGGEST_ABORT;
1123 status = MAYREDO;
1124 break;
1125 #endif
1126 default:
1127 printk ("Internal error %s %d \n"
1128 "status byte = %d \n", __FILE__,
1129 __LINE__, status_byte(result));
1132 break;
1133 default:
1134 panic("scsi: unsupported message byte %d recieved\n", msg_byte(result));
1136 break;
1137 case DID_TIME_OUT:
1138 #ifdef DEBUG
1139 printk("Host returned DID_TIME_OUT - ");
1140 #endif
1142 if (SCpnt->flags & WAS_TIMEDOUT)
1144 #ifdef DEBUG
1145 printk("Aborting\n");
1146 #endif
1147 exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
1149 else
1151 #ifdef DEBUG
1152 printk ("Retrying.\n");
1153 #endif
1154 SCpnt->flags |= WAS_TIMEDOUT;
1155 status = REDO;
1157 break;
1158 case DID_BUS_BUSY:
1159 case DID_PARITY:
1160 status = REDO;
1161 break;
1162 case DID_NO_CONNECT:
1163 #ifdef DEBUG
1164 printk("Couldn't connect.\n");
1165 #endif
1166 exit = (DRIVER_HARD | SUGGEST_ABORT);
1167 break;
1168 case DID_ERROR:
1169 status = MAYREDO;
1170 exit = (DRIVER_HARD | SUGGEST_ABORT);
1171 break;
1172 case DID_BAD_TARGET:
1173 case DID_ABORT:
1174 exit = (DRIVER_INVALID | SUGGEST_ABORT);
1175 break;
1176 case DID_RESET:
1177 if(msg_byte(result) == GOOD &&
1178 status_byte(result) == CHECK_CONDITION) {
1179 switch (check_sense(SCpnt)) {
1180 case 0:
1181 update_timeout(SCpnt, oldto);
1182 status = REDO;
1183 break;
1184 case SUGGEST_REMAP:
1185 case SUGGEST_RETRY:
1186 status = MAYREDO;
1187 exit = DRIVER_SENSE | SUGGEST_RETRY;
1188 break;
1189 case SUGGEST_ABORT:
1190 status = FINISHED;
1191 exit = DRIVER_SENSE | SUGGEST_ABORT;
1192 break;
1193 case SUGGEST_SENSE:
1194 scsi_request_sense (SCpnt);
1195 status = PENDING;
1196 break;
1198 } else {
1199 status=REDO;
1200 exit = SUGGEST_RETRY;
1202 break;
1203 default :
1204 exit = (DRIVER_ERROR | SUGGEST_DIE);
1207 switch (status)
1209 case FINISHED:
1210 case PENDING:
1211 break;
1212 case MAYREDO:
1214 #ifdef DEBUG
1215 printk("In MAYREDO, allowing %d retries, have %d\n",
1216 SCpnt->allowed, SCpnt->retries);
1217 #endif
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);
1226 reset(SCpnt);
1227 break;
1231 else
1233 status = FINISHED;
1234 break;
1236 /* fall through to REDO */
1238 case REDO:
1239 if (SCpnt->flags & WAS_SENSE)
1240 scsi_request_sense(SCpnt);
1241 else
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);
1251 break;
1252 default:
1253 INTERNAL_ERROR;
1256 if (status == FINISHED)
1258 #ifdef DEBUG
1259 printk("Calling done function - at address %08x\n", SCpnt->done);
1260 #endif
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);
1269 #undef FINISHED
1270 #undef REDO
1271 #undef MAYREDO
1272 #undef PENDING
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
1279 next call.
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
1287 this.
1291 int scsi_abort (Scsi_Cmnd * SCpnt, int why)
1293 int temp, oldto;
1294 struct Scsi_Host * host = SCpnt->host;
1296 while(1)
1298 cli();
1299 if (SCpnt->internal_timeout & IN_ABORT)
1301 sti();
1302 while (SCpnt->internal_timeout & IN_ABORT);
1304 else
1306 SCpnt->internal_timeout |= IN_ABORT;
1307 oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
1310 sti();
1311 if (!host->host_busy || !host->hostt->abort(SCpnt, why))
1312 temp = 0;
1313 else
1314 temp = 1;
1316 cli();
1317 SCpnt->internal_timeout &= ~IN_ABORT;
1318 update_timeout(SCpnt, oldto);
1319 sti();
1320 return temp;
1325 int scsi_reset (Scsi_Cmnd * SCpnt)
1327 int temp, oldto;
1328 Scsi_Cmnd * SCpnt1;
1329 struct Scsi_Host * host = SCpnt->host;
1331 #ifdef DEBUG
1332 printk("Danger Will Robinson! - SCSI bus for host %d is being reset.\n",host->host_no);
1333 #endif
1334 while (1) {
1335 cli();
1336 if (SCpnt->internal_timeout & IN_RESET)
1338 sti();
1339 while (SCpnt->internal_timeout & IN_RESET);
1341 else
1343 SCpnt->internal_timeout |= IN_RESET;
1344 oldto = update_timeout(SCpnt, RESET_TIMEOUT);
1346 if (host->host_busy)
1348 sti();
1349 SCpnt1 = host->host_queue;
1350 while(SCpnt1) {
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);
1360 else
1362 host->host_busy++;
1364 sti();
1365 temp = host->hostt->reset(SCpnt);
1366 host->last_reset = jiffies;
1367 host->host_busy--;
1370 cli();
1371 SCpnt->internal_timeout &= ~IN_RESET;
1372 update_timeout(SCpnt, oldto);
1373 sti();
1374 return temp;
1380 static void scsi_main_timeout(void)
1383 We must not enter update_timeout with a timeout condition still pending.
1386 int timed_out;
1387 struct Scsi_Host * host;
1388 Scsi_Cmnd * SCpnt = NULL;
1390 do {
1391 cli();
1394 Find all timers such that they have 0 or negative (shouldn't happen)
1395 time remaining on them.
1398 timed_out = 0;
1399 for(host = scsi_hostlist; host; host = host->next) {
1400 SCpnt = host->host_queue;
1401 while (SCpnt){
1402 if (SCpnt->timeout > 0 && SCpnt->timeout <= time_elapsed)
1404 sti();
1405 SCpnt->timeout = 0;
1406 scsi_times_out(SCpnt);
1407 ++timed_out;
1408 cli();
1410 SCpnt = SCpnt->next;
1413 update_timeout(NULL, 0);
1414 } while (timed_out);
1415 sti();
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;
1429 unsigned int oldto;
1430 struct Scsi_Host * host;
1431 Scsi_Cmnd * SCpnt = NULL;
1433 cli();
1436 Figure out how much time has passed since the last time the timeouts
1437 were updated
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
1444 update_timeout.
1447 oldto = 0;
1449 if(SCset){
1450 oldto = SCset->timeout - used;
1451 SCset->timeout = timeout + used;
1454 least = 0xffffffff;
1456 for(host = scsi_hostlist; host; host = host->next) {
1457 SCpnt = host->host_queue;
1458 while (SCpnt){
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;
1476 else
1478 timer_table[SCSI_TIMER].expires = time_start = time_elapsed = 0;
1479 timer_active &= ~(1 << SCSI_TIMER);
1481 sti();
1482 return oldto;
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;
1495 int i, j;
1496 if((len & 0x1ff) || len > 4096)
1497 panic("Inappropriate buffer size requested");
1499 cli();
1500 nbits = len >> 9;
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);
1507 sti();
1508 dma_free_sectors -= nbits;
1509 #ifdef DEBUG
1510 printk("SMalloc: %d %x ",len, dma_malloc_buffer + (i << 13) + (j << 9));
1511 #endif
1512 return (void *) ((unsigned long) dma_malloc_buffer + (i << 13) + (j << 9));
1515 sti();
1516 return NULL; /* Nope. No more */
1519 int scsi_free(void *obj, unsigned int len)
1521 int offset;
1522 int page, sector, nbits, mask;
1524 #ifdef DEBUG
1525 printk("Sfree %x %d\n",obj, len);
1526 #endif
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;
1536 nbits = len >> 9;
1537 mask = (1 << nbits) - 1;
1539 if ((mask << sector) > 0xffff) panic ("Bad memory alignment");
1541 cli();
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);
1547 sti();
1548 return 0;
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)
1559 int i;
1560 struct Scsi_Host * host;
1561 Scsi_Cmnd * SCpnt;
1562 #ifdef FOO_ON_YOU
1563 return;
1564 #endif
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;
1582 SCpnt = last_cmnd;
1584 for (i=0; i< NR_SCSI_DEVICES; i++) {
1585 int j;
1586 switch (scsi_devices[i].type)
1588 case TYPE_TAPE :
1589 st_attach(&scsi_devices[i]);
1590 break;
1591 case TYPE_ROM:
1592 sr_attach(&scsi_devices[i]);
1593 break;
1594 case TYPE_DISK:
1595 case TYPE_MOD:
1596 sd_attach(&scsi_devices[i]);
1597 default:
1598 break;
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;
1606 SCpnt->index = i;
1607 SCpnt->request.dev = -1; /* Mark not busy */
1608 SCpnt->use_sg = 0;
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;
1617 SCpnt->prev = NULL;
1618 host->host_queue = SCpnt;
1619 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;
1643 need_isa_buffer++;
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)
1670 int i;
1672 printk(" Vendor: ");
1673 for (i = 8; i < 16; i++)
1675 if (data[i] >= 0x20 && i < data[4] + 5)
1676 printk("%c", data[i]);
1677 else
1678 printk(" ");
1681 printk(" Model: ");
1682 for (i = 16; i < 32; i++)
1684 if (data[i] >= 0x20 && i < data[4] + 5)
1685 printk("%c", data[i]);
1686 else
1687 printk(" ");
1690 printk(" Rev: ");
1691 for (i = 32; i < 36; i++)
1693 if (data[i] >= 0x20 && i < data[4] + 5)
1694 printk("%c", data[i]);
1695 else
1696 printk(" ");
1699 printk("\n");
1701 i = data[0] & 0x1f;
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)
1707 printk(" CCS\n");
1708 else
1709 printk("\n");