This source file includes following definitions.
- blacklisted
- scan_scsis_done
- scsi_luns_setup
- scan_scsis
- scsi_times_out
- request_queueable
- allocate_device
- internal_cmnd
- scsi_request_sense
- scsi_do_cmd
- reset
- check_sense
- scsi_done
- scsi_abort
- scsi_reset
- scsi_main_timeout
- update_timeout
- scsi_malloc
- scsi_free
- scsi_init_malloc
- scsi_init_free
- scsi_dev_init
- print_inquiry
- scsi_dump_status
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include <asm/system.h>
21 #include <linux/sched.h>
22 #include <linux/timer.h>
23 #include <linux/string.h>
24 #include <linux/malloc.h>
25 #include <asm/irq.h>
26
27 #include "../block/blk.h"
28 #include "scsi.h"
29 #include "hosts.h"
30 #include "constants.h"
31
32
33
34
35
36
37 const unsigned char scsi_command_size[8] = { 6, 10, 10, 12, 12, 12, 10, 10 };
38
39 #define INTERNAL_ERROR (panic ("Internal error in file %s, line %d.\n", __FILE__, __LINE__))
40
41 static void scsi_done (Scsi_Cmnd *SCpnt);
42 static int update_timeout (Scsi_Cmnd *, int);
43 static void print_inquiry(unsigned char *data);
44 static void scsi_times_out (Scsi_Cmnd * SCpnt);
45
46 static int time_start;
47 static int time_elapsed;
48
49 #define MAX_SCSI_DEVICE_CODE 10
50 const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] =
51 {
52 "Direct-Access ",
53 "Sequential-Access",
54 "Printer ",
55 "Processor ",
56 "WORM ",
57 "CD-ROM ",
58 "Scanner ",
59 "Optical Device ",
60 "Medium Changer ",
61 "Communications "
62 };
63
64
65
66
67
68
69
70
71 Scsi_Device * scsi_devices = NULL;
72
73 static unsigned char generic_sense[6] = {REQUEST_SENSE, 0,0,0, 255, 0};
74
75
76 Scsi_Cmnd * last_cmnd = NULL;
77
78
79
80
81
82
83
84 #define WAS_RESET 0x01
85 #define WAS_TIMEDOUT 0x02
86 #define WAS_SENSE 0x04
87 #define IS_RESETTING 0x08
88 #define IS_ABORTING 0x10
89 #define ASKED_FOR_SENSE 0x20
90
91
92
93
94
95
96
97
98
99
100
101 #ifdef DEBUG_TIMEOUT
102 static void scsi_dump_status(void);
103 #endif
104
105
106 #ifdef DEBUG
107 #define SCSI_TIMEOUT 500
108 #else
109 #define SCSI_TIMEOUT 100
110 #endif
111
112 #ifdef DEBUG
113 #define SENSE_TIMEOUT SCSI_TIMEOUT
114 #define ABORT_TIMEOUT SCSI_TIMEOUT
115 #define RESET_TIMEOUT SCSI_TIMEOUT
116 #else
117 #define SENSE_TIMEOUT 50
118 #define RESET_TIMEOUT 50
119 #define ABORT_TIMEOUT 50
120 #define MIN_RESET_DELAY 100
121 #endif
122
123
124
125
126
127 struct blist{
128 char * vendor;
129 char * model;
130 char * revision;
131 };
132
133 static struct blist blacklist[] =
134 {
135 {"CHINON","CD-ROM CDS-431","H42"},
136 {"CHINON","CD-ROM CDS-535","Q14"},
137 {"DENON","DRD-25X","V"},
138 {"IMS", "CDD521/10","2.06"},
139 {"MAXTOR","XT-3280","PR02"},
140 {"MAXTOR","XT-4380S","B3C"},
141 {"MAXTOR","MXT-1240S","I1.2"},
142 {"MAXTOR","XT-4170S","B5A"},
143 {"MAXTOR","XT-8760S","B7B"},
144 {"NEC","CD-ROM DRIVE:841","1.0"},
145 {"RODIME","RO3000S","2.33"},
146 {"SEAGATE", "ST157N", "\004|j"},
147
148 {"SEAGATE", "ST296","921"},
149 {"SONY","CD-ROM CDU-541","4.3d"},
150 {"TANDBERG","TDC 3600","U07"},
151 {"TEAC","CD-ROM","1.06"},
152
153 {"TEXEL","CD-ROM","1.06"},
154
155 {NULL, NULL, NULL}};
156
157 static int blacklisted(unsigned char * response_data){
158 int i = 0;
159 unsigned char * pnt;
160 for(i=0; 1; i++){
161 if(blacklist[i].vendor == NULL) return 0;
162 pnt = &response_data[8];
163 while(*pnt && *pnt == ' ') pnt++;
164 if(memcmp(blacklist[i].vendor, pnt,
165 strlen(blacklist[i].vendor))) continue;
166 pnt = &response_data[16];
167 while(*pnt && *pnt == ' ') pnt++;
168 if(memcmp(blacklist[i].model, pnt,
169 strlen(blacklist[i].model))) continue;
170 return 1;
171 };
172 };
173
174
175
176
177
178
179
180
181
182
183 static volatile int in_scan = 0;
184 static int the_result;
185 static void scan_scsis_done (Scsi_Cmnd * SCpnt)
186 {
187
188 #ifdef DEBUG
189 printk ("scan_scsis_done(%d, %06x)\n", SCpnt->host, SCpnt->result);
190 #endif
191 SCpnt->request.dev = 0xfffe;
192 }
193
194 #ifdef NO_MULTI_LUN
195 static int max_scsi_luns = 1;
196 #else
197 static int max_scsi_luns = 8;
198 #endif
199
200 void scsi_luns_setup(char *str, int *ints) {
201 if (ints[0] != 1)
202 printk("scsi_luns_setup : usage max_scsi_luns=n (n should be between 1 and 8)\n");
203 else
204 max_scsi_luns = ints[1];
205 }
206
207
208
209
210
211
212
213
214
215 static void scan_scsis (struct Scsi_Host * shpnt)
216 {
217 int dev, lun, type;
218 unsigned char scsi_cmd [12];
219 unsigned char scsi_result [256];
220 Scsi_Device * SDpnt, *SDtail;
221 struct Scsi_Device_Template * sdtpnt;
222 Scsi_Cmnd SCmd;
223
224 ++in_scan;
225 lun = 0;
226 type = -1;
227 SCmd.next = NULL;
228 SCmd.prev = NULL;
229 SDpnt = (Scsi_Device *) scsi_init_malloc(sizeof (Scsi_Device));
230 SDtail = scsi_devices;
231 if(scsi_devices) {
232 while(SDtail->next) SDtail = SDtail->next;
233 }
234
235 shpnt->host_queue = &SCmd;
236
237 for (dev = 0; dev < 8; ++dev)
238 if (shpnt->this_id != dev)
239
240
241
242
243 for (lun = 0; lun < max_scsi_luns; ++lun)
244 {
245 SDpnt->host = shpnt;
246 SDpnt->id = dev;
247 SDpnt->lun = lun;
248 SDpnt->device_wait = NULL;
249 SDpnt->next = NULL;
250 SDpnt->attached = 0;
251
252
253
254
255 SDpnt->borken = 1;
256
257 scsi_cmd[0] = TEST_UNIT_READY;
258 scsi_cmd[1] = lun << 5;
259 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
260 scsi_cmd[4] = 0;
261
262 SCmd.host = shpnt;
263 SCmd.target = dev;
264 SCmd.lun = lun;
265
266 SCmd.request.dev = 0xffff;
267 SCmd.use_sg = 0;
268 SCmd.old_use_sg = 0;
269 SCmd.transfersize = 0;
270 SCmd.underflow = 0;
271
272 scsi_do_cmd (&SCmd,
273 (void *) scsi_cmd, (void *)
274 scsi_result, 256, scan_scsis_done,
275 SCSI_TIMEOUT + 400, 5);
276
277 while (SCmd.request.dev != 0xfffe);
278 #if defined(DEBUG) || defined(DEBUG_INIT)
279 printk("scsi: scan SCSIS id %d lun %d\n", dev, lun);
280 printk("scsi: return code %08x\n", SCmd.result);
281 #endif
282
283
284 if(SCmd.result) {
285 if ((driver_byte(SCmd.result) & DRIVER_SENSE) &&
286 ((SCmd.sense_buffer[0] & 0x70) >> 4) == 7) {
287 if (SCmd.sense_buffer[2] &0xe0)
288 continue;
289 if(((SCmd.sense_buffer[2] & 0xf) != NOT_READY) &&
290 ((SCmd.sense_buffer[2] & 0xf) != UNIT_ATTENTION))
291 continue;
292 }
293 else
294 break;
295 };
296
297 #if defined (DEBUG) || defined(DEBUG_INIT)
298 printk("scsi: performing INQUIRY\n");
299 #endif
300
301
302
303
304
305 scsi_cmd[0] = INQUIRY;
306 scsi_cmd[1] = (lun << 5) & 0xe0;
307 scsi_cmd[2] = 0;
308 scsi_cmd[3] = 0;
309 scsi_cmd[4] = 255;
310 scsi_cmd[5] = 0;
311
312 SCmd.request.dev = 0xffff;
313
314 scsi_do_cmd (&SCmd,
315 (void *) scsi_cmd, (void *)
316 scsi_result, 256, scan_scsis_done,
317 SCSI_TIMEOUT, 3);
318
319 while (SCmd.request.dev != 0xfffe);
320
321 the_result = SCmd.result;
322
323 #if defined(DEBUG) || defined(DEBUG_INIT)
324 if (!the_result)
325 printk("scsi: INQUIRY successful\n");
326 else
327 printk("scsi: INQUIRY failed with code %08x\n");
328 #endif
329
330 if(the_result) break;
331
332
333
334 if (!the_result)
335 {
336
337 if (!strncmp(scsi_result+8,"TOSHIBA",7) &&
338 !strncmp(scsi_result+16,"CD-ROM",6) &&
339 scsi_result[0] == TYPE_DISK) {
340 scsi_result[0] = TYPE_ROM;
341 scsi_result[1] |= 0x80;
342 }
343 SDpnt->removable = (0x80 &
344 scsi_result[1]) >> 7;
345 SDpnt->lockable = SDpnt->removable;
346 SDpnt->changed = 0;
347 SDpnt->access_count = 0;
348 SDpnt->busy = 0;
349
350
351
352
353
354
355 switch (type = scsi_result[0])
356 {
357 case TYPE_TAPE :
358 case TYPE_DISK :
359 case TYPE_MOD :
360 SDpnt->writeable = 1;
361 break;
362 case TYPE_WORM :
363 case TYPE_ROM :
364 SDpnt->writeable = 0;
365 break;
366 default :
367 #if 0
368 #ifdef DEBUG
369 printk("scsi: unknown type %d\n", type);
370 print_inquiry(scsi_result);
371 #endif
372 #endif
373 type = -1;
374 }
375
376 SDpnt->soft_reset =
377 (scsi_result[7] & 1) && ((scsi_result[3] & 7) == 2);
378 SDpnt->random = (type == TYPE_TAPE) ? 0 : 1;
379 SDpnt->type = type;
380
381 if (type != -1)
382 {
383 struct Scsi_Device_Template * sdtpnt;
384 print_inquiry(scsi_result);
385
386 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
387 if(sdtpnt->detect) SDpnt->attached +=
388 (*sdtpnt->detect)(SDpnt);
389
390 SDpnt->scsi_level = scsi_result[2] & 0x07;
391 if (SDpnt->scsi_level >= 2 ||
392 (SDpnt->scsi_level == 1 &&
393 (scsi_result[3] & 0x0f) == 1))
394 SDpnt->scsi_level++;
395
396
397
398
399
400 SDpnt->tagged_queue = 0;
401
402 if ((SDpnt->scsi_level >= SCSI_2) &&
403 (scsi_result[7] & 2)) {
404 SDpnt->tagged_supported = 1;
405 SDpnt->current_tag = 0;
406 }
407
408
409
410
411
412
413 SDpnt->disconnect = 0;
414
415
416
417
418
419
420
421
422
423 if(strncmp("TEXEL", (char *) &scsi_result[8], 5) != 0 ||
424 strncmp("CD-ROM", (char *) &scsi_result[16], 6) != 0
425
426
427
428
429
430
431 #ifdef notyet
432 || (strncmp("1.06", (char *) &scsi_result[[, 4) != 0)))
433 #endif
434 )
435 SDpnt->borken = 0;
436
437
438
439
440 if(memcmp("INSITE", &scsi_result[8], 6) == 0 &&
441 (memcmp("Floptical F*8I", &scsi_result[16], 16) == 0
442 || memcmp("I325VM", &scsi_result[16], 6) == 0)) {
443 printk("Unlocked floptical drive.\n");
444 SDpnt->lockable = 0;
445 scsi_cmd[0] = MODE_SENSE;
446 scsi_cmd[1] = (lun << 5) & 0xe0;
447 scsi_cmd[2] = 0x2e;
448 scsi_cmd[3] = 0;
449 scsi_cmd[4] = 0x2a;
450 scsi_cmd[5] = 0;
451
452 SCmd.request.dev = 0xffff;
453
454 scsi_do_cmd (&SCmd,
455 (void *) scsi_cmd, (void *)
456 scsi_result, 0x2a, scan_scsis_done,
457 SCSI_TIMEOUT, 3);
458
459 while (SCmd.request.dev != 0xfffe);
460 };
461
462 if(SDtail)
463 SDtail->next = SDpnt;
464 else
465 scsi_devices = SDpnt;
466 SDtail = SDpnt;
467
468 SDpnt = (Scsi_Device *) scsi_init_malloc(sizeof (Scsi_Device));
469
470
471 if(blacklisted(scsi_result)) break;
472
473 if ((scsi_result[2] & 0x07) == 0)
474 break;
475
476
477 if((scsi_result[2] & 0x07) == 1 &&
478 (scsi_result[3] & 0x0f) == 0) break;
479 }
480 }
481 }
482 shpnt->host_queue = NULL;
483
484 printk("scsi : detected ");
485 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
486 if(sdtpnt->dev_noticed && sdtpnt->name)
487 printk("%d SCSI %s%s ", sdtpnt->dev_noticed, sdtpnt->name,
488 (sdtpnt->dev_noticed != 1) ? "s" : "");
489
490 printk("total.\n");
491
492
493 scsi_init_free((char *) SDpnt, sizeof(Scsi_Device));
494
495 in_scan = 0;
496 }
497
498
499
500
501
502 #define NORMAL_TIMEOUT 0
503 #define IN_ABORT 1
504 #define IN_RESET 2
505
506
507
508
509
510
511 static void scsi_times_out (Scsi_Cmnd * SCpnt)
512 {
513
514 switch (SCpnt->internal_timeout & (IN_ABORT | IN_RESET))
515 {
516 case NORMAL_TIMEOUT:
517 if (!in_scan) {
518 printk("SCSI host %d timed out - aborting command\n",
519 SCpnt->host->host_no);
520 #ifdef DEBUG_TIMEOUT
521 scsi_dump_status();
522 #endif
523 }
524
525 if (!scsi_abort (SCpnt, DID_TIME_OUT))
526 return;
527 case IN_ABORT:
528 printk("SCSI host %d abort() timed out - reseting\n",
529 SCpnt->host->host_no);
530 if (!scsi_reset (SCpnt))
531 return;
532 case IN_RESET:
533 case (IN_ABORT | IN_RESET):
534
535
536
537 printk("Unable to reset scsi host %d - ",SCpnt->host->host_no);
538 printk("probably a SCSI bus hang.\n");
539 return;
540
541 default:
542 INTERNAL_ERROR;
543 }
544
545 }
546
547
548
549
550
551
552
553
554 Scsi_Cmnd * request_queueable (struct request * req, Scsi_Device * device)
555 {
556 Scsi_Cmnd * SCpnt = NULL;
557 int tablesize;
558 struct buffer_head * bh, *bhp;
559
560 if (!device)
561 panic ("No device passed to allocate_device().\n");
562
563 if (req && req->dev <= 0)
564 panic("Invalid device in allocate_device");
565
566 SCpnt = device->host->host_queue;
567 while(SCpnt){
568 if(SCpnt->target == device->id &&
569 SCpnt->lun == device->lun)
570 if(SCpnt->request.dev < 0) break;
571 SCpnt = SCpnt->next;
572 };
573
574 if (!SCpnt) return NULL;
575
576 if (device->host->hostt->can_queue
577 && device->host->host_busy >= device->host->hostt->can_queue) return NULL;
578
579 if (req) {
580 memcpy(&SCpnt->request, req, sizeof(struct request));
581 tablesize = device->host->sg_tablesize;
582 bhp = bh = req->bh;
583 if(!tablesize) bh = NULL;
584
585
586 while(req->nr_sectors && bh){
587 bhp = bhp->b_reqnext;
588 if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
589 req->nr_sectors -= bh->b_size >> 9;
590 req->sector += bh->b_size >> 9;
591 if(!tablesize) break;
592 bh = bhp;
593 };
594 if(req->nr_sectors && bh && bh->b_reqnext){
595 SCpnt->request.bhtail = bh;
596 req->bh = bh->b_reqnext;
597 bh->b_reqnext = NULL;
598 bh = req->bh;
599
600
601 SCpnt->request.nr_sectors -= req->nr_sectors;
602 req->current_nr_sectors = bh->b_size >> 9;
603 req->buffer = bh->b_data;
604 SCpnt->request.sem = NULL;
605 } else {
606 req->dev = -1;
607 wake_up(&wait_for_request);
608 };
609 } else {
610 SCpnt->request.dev = 0xffff;
611 SCpnt->request.sem = NULL;
612 };
613
614 SCpnt->use_sg = 0;
615 SCpnt->old_use_sg = 0;
616 SCpnt->transfersize = 0;
617 SCpnt->underflow = 0;
618 return SCpnt;
619 }
620
621
622
623
624
625
626
627
628
629
630 Scsi_Cmnd * allocate_device (struct request ** reqp, Scsi_Device * device,
631 int wait)
632 {
633 int dev = -1;
634 struct request * req = NULL;
635 int tablesize;
636 struct buffer_head * bh, *bhp;
637 struct Scsi_Host * host;
638 Scsi_Cmnd * SCpnt = NULL;
639 Scsi_Cmnd * SCwait = NULL;
640
641 if (!device)
642 panic ("No device passed to allocate_device().\n");
643
644 if (reqp) req = *reqp;
645
646
647 if (req && (dev = req->dev) <= 0) return NULL;
648
649 host = device->host;
650
651 while (1==1){
652 SCpnt = host->host_queue;
653 while(SCpnt){
654 if(SCpnt->target == device->id &&
655 SCpnt->lun == device->lun) {
656 SCwait = SCpnt;
657 if(SCpnt->request.dev < 0) break;
658 };
659 SCpnt = SCpnt->next;
660 };
661 cli();
662
663 if (req && ((req->dev < 0) || (req->dev != dev))) {
664 sti();
665 return NULL;
666 };
667 if (!SCpnt || SCpnt->request.dev >= 0)
668 {
669 sti();
670 if(!wait) return NULL;
671 if (!SCwait) {
672 printk("Attempt to allocate device target %d, lun %d\n",
673 device->id ,device->lun);
674 panic("No device found in allocate_device\n");
675 };
676 SCSI_SLEEP(&device->device_wait,
677 (SCwait->request.dev > 0));
678 } else {
679 if (req) {
680 memcpy(&SCpnt->request, req, sizeof(struct request));
681 tablesize = device->host->sg_tablesize;
682 bhp = bh = req->bh;
683 if(!tablesize) bh = NULL;
684
685
686 while(req->nr_sectors && bh){
687 bhp = bhp->b_reqnext;
688 if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
689 req->nr_sectors -= bh->b_size >> 9;
690 req->sector += bh->b_size >> 9;
691 if(!tablesize) break;
692 bh = bhp;
693 };
694 if(req->nr_sectors && bh && bh->b_reqnext){
695 SCpnt->request.bhtail = bh;
696 req->bh = bh->b_reqnext;
697 bh->b_reqnext = NULL;
698 bh = req->bh;
699
700 SCpnt->request.nr_sectors -= req->nr_sectors;
701 req->current_nr_sectors = bh->b_size >> 9;
702 req->buffer = bh->b_data;
703 SCpnt->request.sem = NULL;
704 }
705 else
706 {
707 req->dev = -1;
708 *reqp = req->next;
709 wake_up(&wait_for_request);
710 };
711 } else {
712 SCpnt->request.dev = 0xffff;
713 SCpnt->request.sem = NULL;
714 };
715 sti();
716 break;
717 };
718 };
719
720 SCpnt->use_sg = 0;
721 SCpnt->old_use_sg = 0;
722 SCpnt->transfersize = 0;
723 SCpnt->underflow = 0;
724 return SCpnt;
725 }
726
727
728
729
730
731 inline void internal_cmnd (Scsi_Cmnd * SCpnt)
732 {
733 int temp;
734 struct Scsi_Host * host;
735 #ifdef DEBUG_DELAY
736 int clock;
737 #endif
738
739 if ((unsigned long) &SCpnt < current->kernel_stack_page)
740 panic("Kernel stack overflow.");
741
742 host = SCpnt->host;
743
744
745
746
747
748 temp = host->last_reset;
749 while (jiffies < temp);
750
751 update_timeout(SCpnt, SCpnt->timeout_per_command);
752
753
754
755
756
757 #ifdef DEBUG
758 printk("internal_cmnd (host = %d, target = %d, command = %08x, buffer = %08x, \n"
759 "bufflen = %d, done = %08x)\n", SCpnt->host->host_no, SCpnt->target, SCpnt->cmnd, SCpnt->buffer, SCpnt->bufflen, SCpnt->done);
760 #endif
761
762 if (host->hostt->can_queue)
763 {
764 extern unsigned long intr_count;
765 #ifdef DEBUG
766 printk("queuecommand : routine at %08x\n",
767 host->hostt->queuecommand);
768 #endif
769
770
771
772
773
774
775
776
777 if(!intr_count && SCpnt->host->irq)
778 disable_irq(SCpnt->host->irq);
779
780 host->hostt->queuecommand (SCpnt, scsi_done);
781
782 if(!intr_count && SCpnt->host->irq)
783 enable_irq(SCpnt->host->irq);
784 }
785 else
786 {
787
788 #ifdef DEBUG
789 printk("command() : routine at %08x\n", host->hostt->command);
790 #endif
791 temp=host->hostt->command (SCpnt);
792 SCpnt->result = temp;
793 #ifdef DEBUG_DELAY
794 clock = jiffies + 400;
795 while (jiffies < clock);
796 printk("done(host = %d, result = %04x) : routine at %08x\n", host->host_no, temp, done);
797 #endif
798 scsi_done(SCpnt);
799 }
800 #ifdef DEBUG
801 printk("leaving internal_cmnd()\n");
802 #endif
803 }
804
805 static void scsi_request_sense (Scsi_Cmnd * SCpnt)
806 {
807 cli();
808 SCpnt->flags |= WAS_SENSE | ASKED_FOR_SENSE;
809 update_timeout(SCpnt, SENSE_TIMEOUT);
810 sti();
811
812
813 memcpy ((void *) SCpnt->cmnd , (void *) generic_sense,
814 sizeof(generic_sense));
815
816 SCpnt->cmnd[1] = SCpnt->lun << 5;
817 SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);
818
819 SCpnt->request_buffer = &SCpnt->sense_buffer;
820 SCpnt->request_bufflen = sizeof(SCpnt->sense_buffer);
821 SCpnt->use_sg = 0;
822 internal_cmnd (SCpnt);
823 SCpnt->use_sg = SCpnt->old_use_sg;
824 }
825
826
827
828
829
830
831
832
833
834
835 void scsi_do_cmd (Scsi_Cmnd * SCpnt, const void *cmnd ,
836 void *buffer, unsigned bufflen, void (*done)(Scsi_Cmnd *),
837 int timeout, int retries
838 )
839 {
840 struct Scsi_Host * host = SCpnt->host;
841
842 #ifdef DEBUG
843 {
844 int i;
845 int target = SCpnt->target;
846 printk ("scsi_do_cmd (host = %d, target = %d, buffer =%08x, "
847 "bufflen = %d, done = %08x, timeout = %d, retries = %d)\n"
848 "command : " , host->host_no, target, buffer, bufflen, done, timeout, retries);
849 for (i = 0; i < 10; ++i)
850 printk ("%02x ", ((unsigned char *) cmnd)[i]);
851 printk("\n");
852 };
853 #endif
854
855 if (!host)
856 {
857 panic ("Invalid or not present host. %d\n", host->host_no);
858 }
859
860
861
862
863
864
865
866
867
868
869 while (1==1){
870 cli();
871 if (host->hostt->can_queue
872 && host->host_busy >= host->hostt->can_queue)
873 {
874 sti();
875 SCSI_SLEEP(&host->host_wait,
876 (host->host_busy >= host->hostt->can_queue));
877 } else {
878 host->host_busy++;
879 sti();
880 break;
881 };
882 };
883
884
885
886
887
888
889
890
891 memcpy ((void *) SCpnt->data_cmnd , (void *) cmnd, 12);
892 #if 0
893 SCpnt->host = host;
894 SCpnt->target = target;
895 SCpnt->lun = (SCpnt->data_cmnd[1] >> 5);
896 #endif
897 SCpnt->bufflen = bufflen;
898 SCpnt->buffer = buffer;
899 SCpnt->flags=0;
900 SCpnt->retries=0;
901 SCpnt->allowed=retries;
902 SCpnt->done = done;
903 SCpnt->timeout_per_command = timeout;
904
905 memcpy ((void *) SCpnt->cmnd , (void *) cmnd, 12);
906
907
908 memset ((void *) SCpnt->sense_buffer, 0, sizeof SCpnt->sense_buffer);
909 SCpnt->request_buffer = buffer;
910 SCpnt->request_bufflen = bufflen;
911 SCpnt->old_use_sg = SCpnt->use_sg;
912
913
914
915 SCpnt->internal_timeout = 0;
916 SCpnt->abort_reason = 0;
917 internal_cmnd (SCpnt);
918
919 #ifdef DEBUG
920 printk ("Leaving scsi_do_cmd()\n");
921 #endif
922 }
923
924
925
926
927
928
929
930
931
932 static void reset (Scsi_Cmnd * SCpnt)
933 {
934 #ifdef DEBUG
935 printk("scsi: reset(%d)\n", SCpnt->host->host_no);
936 #endif
937
938 SCpnt->flags |= (WAS_RESET | IS_RESETTING);
939 scsi_reset(SCpnt);
940
941 #ifdef DEBUG
942 printk("performing request sense\n");
943 #endif
944
945 #if 0
946 if(SCpnt->flags & NEEDS_JUMPSTART) {
947 SCpnt->flags &= ~NEEDS_JUMPSTART;
948 scsi_request_sense (SCpnt);
949 };
950 #endif
951 }
952
953
954
955 static int check_sense (Scsi_Cmnd * SCpnt)
956 {
957
958
959
960 if (((SCpnt->sense_buffer[0] & 0x70) >> 4) != 7) {
961 if(!(SCpnt->flags & ASKED_FOR_SENSE))
962 return SUGGEST_SENSE;
963 else
964 return SUGGEST_RETRY;
965 }
966
967 SCpnt->flags &= ~ASKED_FOR_SENSE;
968
969 #ifdef DEBUG_INIT
970 printk("scsi%d : ", SCpnt->host->host_no);
971 print_sense("", SCpnt);
972 printk("\n");
973 #endif
974 if (SCpnt->sense_buffer[2] &0xe0)
975 return SUGGEST_ABORT;
976
977 switch (SCpnt->sense_buffer[2] & 0xf)
978 {
979 case NO_SENSE:
980 return 0;
981 case RECOVERED_ERROR:
982 if (SCpnt->device->type == TYPE_TAPE)
983 return SUGGEST_IS_OK;
984 else
985 return 0;
986
987 case ABORTED_COMMAND:
988 return SUGGEST_RETRY;
989 case NOT_READY:
990 case UNIT_ATTENTION:
991 return SUGGEST_ABORT;
992
993
994 case COPY_ABORTED:
995 case VOLUME_OVERFLOW:
996 case MISCOMPARE:
997
998 case MEDIUM_ERROR:
999 return SUGGEST_REMAP;
1000 case BLANK_CHECK:
1001 case DATA_PROTECT:
1002 case HARDWARE_ERROR:
1003 case ILLEGAL_REQUEST:
1004 default:
1005 return SUGGEST_ABORT;
1006 }
1007 }
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030 static void scsi_done (Scsi_Cmnd * SCpnt)
1031 {
1032 int status=0;
1033 int exit=0;
1034 int checked;
1035 int oldto;
1036 struct Scsi_Host * host = SCpnt->host;
1037 int result = SCpnt->result;
1038 oldto = update_timeout(SCpnt, 0);
1039
1040 #ifdef DEBUG_TIMEOUT
1041 if(result) printk("Non-zero result in scsi_done %x %d:%d\n",
1042 result, SCpnt->target, SCpnt->lun);
1043 #endif
1044
1045
1046
1047 if(host_byte(result) == DID_ABORT && SCpnt->abort_reason)
1048 SCpnt->result = result = (result & 0xff00ffff) |
1049 (SCpnt->abort_reason << 16);
1050
1051
1052 #define FINISHED 0
1053 #define MAYREDO 1
1054 #define REDO 3
1055 #define PENDING 4
1056
1057 #ifdef DEBUG
1058 printk("In scsi_done(host = %d, result = %06x)\n", host->host_no, result);
1059 #endif
1060 switch (host_byte(result))
1061 {
1062 case DID_OK:
1063 if (status_byte(result) && (SCpnt->flags & WAS_SENSE))
1064
1065 {
1066 SCpnt->flags &= ~WAS_SENSE;
1067 SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
1068
1069 if (!(SCpnt->flags & WAS_RESET))
1070 {
1071 printk("scsi%d : target %d lun %d request sense failed, performing reset.\n",
1072 SCpnt->host->host_no, SCpnt->target, SCpnt->lun);
1073 reset(SCpnt);
1074 return;
1075 }
1076 else
1077 {
1078 exit = (DRIVER_HARD | SUGGEST_ABORT);
1079 status = FINISHED;
1080 }
1081 }
1082 else switch(msg_byte(result))
1083 {
1084 case COMMAND_COMPLETE:
1085 switch (status_byte(result))
1086 {
1087 case GOOD:
1088 if (SCpnt->flags & WAS_SENSE)
1089 {
1090 #ifdef DEBUG
1091 printk ("In scsi_done, GOOD status, COMMAND COMPLETE, parsing sense information.\n");
1092 #endif
1093
1094 SCpnt->flags &= ~WAS_SENSE;
1095 SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
1096
1097 switch (checked = check_sense(SCpnt))
1098 {
1099 case SUGGEST_SENSE:
1100 case 0:
1101 #ifdef DEBUG
1102 printk("NO SENSE. status = REDO\n");
1103 #endif
1104
1105 update_timeout(SCpnt, oldto);
1106 status = REDO;
1107 break;
1108 case SUGGEST_IS_OK:
1109 break;
1110 case SUGGEST_REMAP:
1111 case SUGGEST_RETRY:
1112 #ifdef DEBUG
1113 printk("SENSE SUGGEST REMAP or SUGGEST RETRY - status = MAYREDO\n");
1114 #endif
1115
1116 status = MAYREDO;
1117 exit = DRIVER_SENSE | SUGGEST_RETRY;
1118 break;
1119 case SUGGEST_ABORT:
1120 #ifdef DEBUG
1121 printk("SENSE SUGGEST ABORT - status = FINISHED");
1122 #endif
1123
1124 status = FINISHED;
1125 exit = DRIVER_SENSE | SUGGEST_ABORT;
1126 break;
1127 default:
1128 printk ("Internal error %s %d \n", __FILE__,
1129 __LINE__);
1130 }
1131 }
1132 else
1133 {
1134 #ifdef DEBUG
1135 printk("COMMAND COMPLETE message returned, status = FINISHED. \n");
1136 #endif
1137
1138 exit = DRIVER_OK;
1139 status = FINISHED;
1140 }
1141 break;
1142
1143 case CHECK_CONDITION:
1144 switch (check_sense(SCpnt))
1145 {
1146 case 0:
1147 update_timeout(SCpnt, oldto);
1148 status = REDO;
1149 break;
1150 case SUGGEST_REMAP:
1151 case SUGGEST_RETRY:
1152 status = MAYREDO;
1153 exit = DRIVER_SENSE | SUGGEST_RETRY;
1154 break;
1155 case SUGGEST_ABORT:
1156 status = FINISHED;
1157 exit = DRIVER_SENSE | SUGGEST_ABORT;
1158 break;
1159 case SUGGEST_SENSE:
1160 scsi_request_sense (SCpnt);
1161 status = PENDING;
1162 break;
1163 }
1164 break;
1165
1166 case CONDITION_GOOD:
1167 case INTERMEDIATE_GOOD:
1168 case INTERMEDIATE_C_GOOD:
1169 break;
1170
1171 case BUSY:
1172 update_timeout(SCpnt, oldto);
1173 status = REDO;
1174 break;
1175
1176 case RESERVATION_CONFLICT:
1177 printk("scsi%d : RESERVATION CONFLICT performing reset.\n",
1178 SCpnt->host->host_no);
1179 reset(SCpnt);
1180 return;
1181 #if 0
1182 exit = DRIVER_SOFT | SUGGEST_ABORT;
1183 status = MAYREDO;
1184 break;
1185 #endif
1186 default:
1187 printk ("Internal error %s %d \n"
1188 "status byte = %d \n", __FILE__,
1189 __LINE__, status_byte(result));
1190
1191 }
1192 break;
1193 default:
1194 panic("scsi: unsupported message byte %d recieved\n", msg_byte(result));
1195 }
1196 break;
1197 case DID_TIME_OUT:
1198 #ifdef DEBUG
1199 printk("Host returned DID_TIME_OUT - ");
1200 #endif
1201
1202 if (SCpnt->flags & WAS_TIMEDOUT)
1203 {
1204 #ifdef DEBUG
1205 printk("Aborting\n");
1206 #endif
1207 exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
1208 }
1209 else
1210 {
1211 #ifdef DEBUG
1212 printk ("Retrying.\n");
1213 #endif
1214 SCpnt->flags |= WAS_TIMEDOUT;
1215 SCpnt->internal_timeout &= ~IN_ABORT;
1216 status = REDO;
1217 }
1218 break;
1219 case DID_BUS_BUSY:
1220 case DID_PARITY:
1221 status = REDO;
1222 break;
1223 case DID_NO_CONNECT:
1224 #ifdef DEBUG
1225 printk("Couldn't connect.\n");
1226 #endif
1227 exit = (DRIVER_HARD | SUGGEST_ABORT);
1228 break;
1229 case DID_ERROR:
1230 status = MAYREDO;
1231 exit = (DRIVER_HARD | SUGGEST_ABORT);
1232 break;
1233 case DID_BAD_TARGET:
1234 case DID_ABORT:
1235 exit = (DRIVER_INVALID | SUGGEST_ABORT);
1236 break;
1237 case DID_RESET:
1238 if (SCpnt->flags & IS_RESETTING)
1239 {
1240 SCpnt->flags &= ~IS_RESETTING;
1241 status = REDO;
1242 break;
1243 }
1244
1245 if(msg_byte(result) == GOOD &&
1246 status_byte(result) == CHECK_CONDITION) {
1247 switch (check_sense(SCpnt)) {
1248 case 0:
1249 update_timeout(SCpnt, oldto);
1250 status = REDO;
1251 break;
1252 case SUGGEST_REMAP:
1253 case SUGGEST_RETRY:
1254 status = MAYREDO;
1255 exit = DRIVER_SENSE | SUGGEST_RETRY;
1256 break;
1257 case SUGGEST_ABORT:
1258 status = FINISHED;
1259 exit = DRIVER_SENSE | SUGGEST_ABORT;
1260 break;
1261 case SUGGEST_SENSE:
1262 scsi_request_sense (SCpnt);
1263 status = PENDING;
1264 break;
1265 }
1266 } else {
1267 status=REDO;
1268 exit = SUGGEST_RETRY;
1269 }
1270 break;
1271 default :
1272 exit = (DRIVER_ERROR | SUGGEST_DIE);
1273 }
1274
1275 switch (status)
1276 {
1277 case FINISHED:
1278 case PENDING:
1279 break;
1280 case MAYREDO:
1281
1282 #ifdef DEBUG
1283 printk("In MAYREDO, allowing %d retries, have %d\n",
1284 SCpnt->allowed, SCpnt->retries);
1285 #endif
1286
1287 if ((++SCpnt->retries) < SCpnt->allowed)
1288 {
1289 if ((SCpnt->retries >= (SCpnt->allowed >> 1))
1290 && !(SCpnt->flags & WAS_RESET))
1291 {
1292 printk("scsi%d : reseting for second half of retries.\n",
1293 SCpnt->host->host_no);
1294 reset(SCpnt);
1295 break;
1296 }
1297
1298 }
1299 else
1300 {
1301 status = FINISHED;
1302 break;
1303 }
1304
1305
1306 case REDO:
1307 if (SCpnt->flags & WAS_SENSE)
1308 scsi_request_sense(SCpnt);
1309 else
1310 {
1311 memcpy ((void *) SCpnt->cmnd,
1312 (void*) SCpnt->data_cmnd,
1313 sizeof(SCpnt->data_cmnd));
1314 SCpnt->request_buffer = SCpnt->buffer;
1315 SCpnt->request_bufflen = SCpnt->bufflen;
1316 SCpnt->use_sg = SCpnt->old_use_sg;
1317 internal_cmnd (SCpnt);
1318 };
1319 break;
1320 default:
1321 INTERNAL_ERROR;
1322 }
1323
1324 if (status == FINISHED)
1325 {
1326 #ifdef DEBUG
1327 printk("Calling done function - at address %08x\n", SCpnt->done);
1328 #endif
1329 host->host_busy--;
1330 wake_up(&host->host_wait);
1331 SCpnt->result = result | ((exit & 0xff) << 24);
1332 SCpnt->use_sg = SCpnt->old_use_sg;
1333 SCpnt->done (SCpnt);
1334 }
1335
1336
1337 #undef FINISHED
1338 #undef REDO
1339 #undef MAYREDO
1340 #undef PENDING
1341 }
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359 int scsi_abort (Scsi_Cmnd * SCpnt, int why)
1360 {
1361 int oldto;
1362 struct Scsi_Host * host = SCpnt->host;
1363
1364 while(1)
1365 {
1366 cli();
1367 if (SCpnt->internal_timeout & IN_ABORT)
1368 {
1369 sti();
1370 while (SCpnt->internal_timeout & IN_ABORT);
1371 }
1372 else
1373 {
1374 SCpnt->internal_timeout |= IN_ABORT;
1375 oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
1376
1377 if ((SCpnt->flags & IS_RESETTING) &&
1378 SCpnt->device->soft_reset) {
1379
1380
1381 printk("Stale command on %d:%d appears to have died when"
1382 " the bus was reset\n", SCpnt->target, SCpnt->lun);
1383 }
1384
1385 sti();
1386 if (!host->host_busy) {
1387 SCpnt->internal_timeout &= ~IN_ABORT;
1388 update_timeout(SCpnt, oldto);
1389 return 0;
1390 }
1391 SCpnt->abort_reason = why;
1392 switch(host->hostt->abort(SCpnt)) {
1393
1394
1395
1396
1397 case SCSI_ABORT_BUSY:
1398
1399 case SCSI_ABORT_SNOOZE:
1400 if(why == DID_TIME_OUT) {
1401 cli();
1402 SCpnt->internal_timeout &= ~IN_ABORT;
1403 if(SCpnt->flags & WAS_TIMEDOUT) {
1404 sti();
1405 return 1;
1406
1407
1408 } else {
1409 SCpnt->flags |= WAS_TIMEDOUT;
1410 oldto = SCpnt->timeout_per_command;
1411 update_timeout(SCpnt, oldto);
1412 }
1413 sti();
1414 }
1415 return 0;
1416 case SCSI_ABORT_PENDING:
1417 if(why != DID_TIME_OUT) {
1418 cli();
1419 update_timeout(SCpnt, oldto);
1420 sti();
1421 }
1422 return 0;
1423 case SCSI_ABORT_SUCCESS:
1424
1425
1426 case SCSI_ABORT_NOT_RUNNING:
1427 SCpnt->internal_timeout &= ~IN_ABORT;
1428 return 0;
1429 case SCSI_ABORT_ERROR:
1430 default:
1431 SCpnt->internal_timeout &= ~IN_ABORT;
1432 return 1;
1433 }
1434 }
1435 }
1436 }
1437
1438 int scsi_reset (Scsi_Cmnd * SCpnt)
1439 {
1440 int temp, oldto;
1441 Scsi_Cmnd * SCpnt1;
1442 struct Scsi_Host * host = SCpnt->host;
1443
1444 #ifdef DEBUG
1445 printk("Danger Will Robinson! - SCSI bus for host %d is being reset.\n",host->host_no);
1446 #endif
1447 while (1) {
1448 cli();
1449 if (SCpnt->internal_timeout & IN_RESET)
1450 {
1451 sti();
1452 while (SCpnt->internal_timeout & IN_RESET);
1453 }
1454 else
1455 {
1456 SCpnt->internal_timeout |= IN_RESET;
1457 oldto = update_timeout(SCpnt, RESET_TIMEOUT);
1458
1459 if (host->host_busy)
1460 {
1461 sti();
1462 SCpnt1 = host->host_queue;
1463 while(SCpnt1) {
1464 if (SCpnt1->request.dev > 0) {
1465 #if 0
1466 if (!(SCpnt1->flags & IS_RESETTING) &&
1467 !(SCpnt1->internal_timeout & IN_ABORT))
1468 scsi_abort(SCpnt1, DID_RESET);
1469 #endif
1470 SCpnt1->flags |= IS_RESETTING;
1471 }
1472 SCpnt1 = SCpnt1->next;
1473 };
1474
1475 temp = host->hostt->reset(SCpnt);
1476 }
1477 else
1478 {
1479 host->host_busy++;
1480
1481 sti();
1482 temp = host->hostt->reset(SCpnt);
1483 host->last_reset = jiffies;
1484 host->host_busy--;
1485 }
1486
1487 #ifdef DEBUG
1488 printk("scsi reset function returned %d\n", temp);
1489 #endif
1490 switch(temp) {
1491 case SCSI_RESET_SUCCESS:
1492 cli();
1493 SCpnt->internal_timeout &= ~IN_RESET;
1494 update_timeout(SCpnt, oldto);
1495 sti();
1496 return 0;
1497 case SCSI_RESET_PENDING:
1498 return 0;
1499 case SCSI_RESET_PUNT:
1500 case SCSI_RESET_WAKEUP:
1501 SCpnt->internal_timeout &= ~IN_RESET;
1502 scsi_request_sense (SCpnt);
1503 return 0;
1504 case SCSI_RESET_SNOOZE:
1505
1506
1507
1508
1509 cli();
1510 SCpnt->internal_timeout &= ~IN_RESET;
1511 update_timeout(SCpnt, 0);
1512 sti();
1513
1514 case SCSI_RESET_ERROR:
1515 default:
1516 return 1;
1517 }
1518
1519 return temp;
1520 }
1521 }
1522 }
1523
1524
1525 static void scsi_main_timeout(void)
1526 {
1527
1528
1529
1530
1531 int timed_out;
1532 struct Scsi_Host * host;
1533 Scsi_Cmnd * SCpnt = NULL;
1534
1535 do {
1536 cli();
1537
1538 update_timeout(NULL, 0);
1539
1540
1541
1542
1543
1544 timed_out = 0;
1545 for(host = scsi_hostlist; host; host = host->next) {
1546 for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
1547 if (SCpnt->timeout == -1)
1548 {
1549 sti();
1550 SCpnt->timeout = 0;
1551 scsi_times_out(SCpnt);
1552 ++timed_out;
1553 cli();
1554 }
1555 };
1556 } while (timed_out);
1557 sti();
1558 }
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568 static int update_timeout(Scsi_Cmnd * SCset, int timeout)
1569 {
1570 unsigned int least, used;
1571 unsigned int oldto;
1572 struct Scsi_Host * host;
1573 Scsi_Cmnd * SCpnt = NULL;
1574
1575 cli();
1576
1577
1578
1579
1580
1581 used = (time_start) ? (jiffies - time_start) : 0;
1582
1583
1584
1585
1586
1587
1588
1589 oldto = 0;
1590
1591 if(SCset){
1592 oldto = SCset->timeout - used;
1593 SCset->timeout = timeout + used;
1594 };
1595
1596 least = 0xffffffff;
1597
1598 for(host = scsi_hostlist; host; host = host->next)
1599 for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
1600 if (SCpnt->timeout > 0) {
1601 SCpnt->timeout -= used;
1602 if(SCpnt->timeout <= 0) SCpnt->timeout = -1;
1603 if(SCpnt->timeout > 0 && SCpnt->timeout < least)
1604 least = SCpnt->timeout;
1605 };
1606
1607
1608
1609
1610
1611
1612 if (least != 0xffffffff)
1613 {
1614 time_start = jiffies;
1615 timer_table[SCSI_TIMER].expires = (time_elapsed = least) + jiffies;
1616 timer_active |= 1 << SCSI_TIMER;
1617 }
1618 else
1619 {
1620 timer_table[SCSI_TIMER].expires = time_start = time_elapsed = 0;
1621 timer_active &= ~(1 << SCSI_TIMER);
1622 }
1623 sti();
1624 return oldto;
1625 }
1626
1627
1628 static unsigned short * dma_malloc_freelist = NULL;
1629 static unsigned int dma_sectors = 0;
1630 unsigned int dma_free_sectors = 0;
1631 unsigned int need_isa_buffer = 0;
1632 static unsigned char * dma_malloc_buffer = NULL;
1633
1634 void *scsi_malloc(unsigned int len)
1635 {
1636 unsigned int nbits, mask;
1637 int i, j;
1638 if((len & 0x1ff) || len > 8192)
1639 return NULL;
1640
1641 cli();
1642 nbits = len >> 9;
1643 mask = (1 << nbits) - 1;
1644
1645 for(i=0;i < (dma_sectors >> 4); i++)
1646 for(j=0; j<17-nbits; j++){
1647 if ((dma_malloc_freelist[i] & (mask << j)) == 0){
1648 dma_malloc_freelist[i] |= (mask << j);
1649 sti();
1650 dma_free_sectors -= nbits;
1651 #ifdef DEBUG
1652 printk("SMalloc: %d %x ",len, dma_malloc_buffer + (i << 13) + (j << 9));
1653 #endif
1654 return (void *) ((unsigned long) dma_malloc_buffer + (i << 13) + (j << 9));
1655 };
1656 };
1657 sti();
1658 return NULL;
1659 }
1660
1661 int scsi_free(void *obj, unsigned int len)
1662 {
1663 int offset;
1664 int page, sector, nbits, mask;
1665
1666 #ifdef DEBUG
1667 printk("Sfree %x %d\n",obj, len);
1668 #endif
1669
1670 offset = ((int) obj) - ((int) dma_malloc_buffer);
1671
1672 if (offset < 0) panic("Bad offset");
1673 page = offset >> 13;
1674 sector = offset >> 9;
1675 if(sector >= dma_sectors) panic ("Bad page");
1676
1677 sector = (offset >> 9) & 15;
1678 nbits = len >> 9;
1679 mask = (1 << nbits) - 1;
1680
1681 if ((mask << sector) > 0xffff) panic ("Bad memory alignment");
1682
1683 cli();
1684 if(dma_malloc_freelist[page] & (mask << sector) != (mask<<sector))
1685 panic("Trying to free unused memory");
1686
1687 dma_free_sectors += nbits;
1688 dma_malloc_freelist[page] &= ~(mask << sector);
1689 sti();
1690 return 0;
1691 }
1692
1693
1694
1695
1696
1697
1698 static unsigned int scsi_init_memory_start = 0;
1699 int scsi_loadable_module_flag;
1700
1701 void * scsi_init_malloc(unsigned int size)
1702 {
1703 unsigned int retval;
1704 if(scsi_loadable_module_flag) {
1705 retval = (unsigned int) kmalloc(size, GFP_ATOMIC);
1706 } else {
1707 retval = scsi_init_memory_start;
1708 scsi_init_memory_start += size;
1709 }
1710 return (void *) retval;
1711 }
1712
1713
1714 void scsi_init_free(char * ptr, unsigned int size)
1715 {
1716
1717 if((unsigned int) ptr < scsi_loadable_module_flag) {
1718 kfree(ptr);
1719 } else {
1720 if(((unsigned int) ptr) + size == scsi_init_memory_start)
1721 scsi_init_memory_start = (unsigned int) ptr;
1722 }
1723 }
1724
1725
1726
1727
1728
1729
1730
1731 unsigned long scsi_dev_init (unsigned long memory_start,unsigned long memory_end)
1732 {
1733 struct Scsi_Host * host = NULL;
1734 Scsi_Device * SDpnt;
1735 struct Scsi_Host * shpnt;
1736 struct Scsi_Device_Template * sdtpnt;
1737 Scsi_Cmnd * SCpnt;
1738 #ifdef FOO_ON_YOU
1739 return;
1740 #endif
1741
1742
1743 scsi_loadable_module_flag = 0;
1744 scsi_init_memory_start = memory_start;
1745
1746 timer_table[SCSI_TIMER].fn = scsi_main_timeout;
1747 timer_table[SCSI_TIMER].expires = 0;
1748
1749
1750 scsi_init();
1751
1752 scsi_devices = (Scsi_Device *) NULL;
1753
1754 for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
1755 scan_scsis(shpnt);
1756
1757 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
1758 if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
1759
1760 for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
1761 int j;
1762 SDpnt->scsi_request_fn = NULL;
1763 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
1764 if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
1765 if(SDpnt->type != -1){
1766 for(j=0;j<SDpnt->host->hostt->cmd_per_lun;j++){
1767 SCpnt = (Scsi_Cmnd *) scsi_init_malloc(sizeof(Scsi_Cmnd));
1768 SCpnt->host = SDpnt->host;
1769 SCpnt->device = SDpnt;
1770 SCpnt->target = SDpnt->id;
1771 SCpnt->lun = SDpnt->lun;
1772 SCpnt->request.dev = -1;
1773 SCpnt->use_sg = 0;
1774 SCpnt->old_use_sg = 0;
1775 SCpnt->underflow = 0;
1776 SCpnt->transfersize = 0;
1777 SCpnt->host_scribble = NULL;
1778 host = SDpnt->host;
1779 if(host->host_queue)
1780 host->host_queue->prev = SCpnt;
1781 SCpnt->next = host->host_queue;
1782 SCpnt->prev = NULL;
1783 host->host_queue = SCpnt;
1784 };
1785 };
1786 };
1787
1788 if (scsi_devicelist)
1789 dma_sectors = 16;
1790
1791 for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
1792 host = SDpnt->host;
1793
1794 if(SDpnt->type != TYPE_TAPE)
1795 dma_sectors += ((host->sg_tablesize *
1796 sizeof(struct scatterlist) + 511) >> 9) *
1797 host->hostt->cmd_per_lun;
1798
1799 if(host->unchecked_isa_dma &&
1800 memory_end > ISA_DMA_THRESHOLD &&
1801 SDpnt->type != TYPE_TAPE) {
1802 dma_sectors += (PAGE_SIZE >> 9) * host->sg_tablesize *
1803 host->hostt->cmd_per_lun;
1804 need_isa_buffer++;
1805 };
1806 };
1807
1808 dma_sectors = (dma_sectors + 15) & 0xfff0;
1809 dma_free_sectors = dma_sectors;
1810
1811 scsi_init_memory_start = (scsi_init_memory_start + 3) & 0xfffffffc;
1812 dma_malloc_freelist = (unsigned short *)
1813 scsi_init_malloc(dma_sectors >> 3);
1814 memset(dma_malloc_freelist, 0, dma_sectors >> 3);
1815
1816
1817 if(scsi_init_memory_start & 1) scsi_init_memory_start++;
1818
1819 dma_malloc_buffer = (unsigned char *)
1820 scsi_init_malloc(dma_sectors << 9);
1821
1822
1823
1824 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
1825 if(sdtpnt->finish && sdtpnt->nr_dev)
1826 (*sdtpnt->finish)();
1827
1828 scsi_loadable_module_flag = 1;
1829 return scsi_init_memory_start;
1830 }
1831
1832 static void print_inquiry(unsigned char *data)
1833 {
1834 int i;
1835
1836 printk(" Vendor: ");
1837 for (i = 8; i < 16; i++)
1838 {
1839 if (data[i] >= 0x20 && i < data[4] + 5)
1840 printk("%c", data[i]);
1841 else
1842 printk(" ");
1843 }
1844
1845 printk(" Model: ");
1846 for (i = 16; i < 32; i++)
1847 {
1848 if (data[i] >= 0x20 && i < data[4] + 5)
1849 printk("%c", data[i]);
1850 else
1851 printk(" ");
1852 }
1853
1854 printk(" Rev: ");
1855 for (i = 32; i < 36; i++)
1856 {
1857 if (data[i] >= 0x20 && i < data[4] + 5)
1858 printk("%c", data[i]);
1859 else
1860 printk(" ");
1861 }
1862
1863 printk("\n");
1864
1865 i = data[0] & 0x1f;
1866
1867 printk(" Type: %s ",
1868 i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] : "Unknown " );
1869 printk(" ANSI SCSI revision: %02x", data[2] & 0x07);
1870 if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
1871 printk(" CCS\n");
1872 else
1873 printk("\n");
1874 }
1875
1876 #ifdef DEBUG_TIMEOUT
1877 static void
1878 scsi_dump_status(void)
1879 {
1880 int i, i1;
1881 Scsi_Host * shpnt;
1882 Scsi_Cmnd * SCpnt;
1883 printk("Dump of scsi parameters:\n");
1884 for(shpnt = scsi_hosts; shpnt; shpnt = shpnt->next)
1885 for(SCpnt=shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
1886 {
1887
1888 printk("(%d) %d:%d:%d (%4.4x %d %d %d %d) (%d %d %x) (%d %d %d) %x %x %x\n",
1889 i, SCpnt->host->host_no,
1890 SCpnt->target,
1891 SCpnt->lun,
1892 SCpnt->request.dev,
1893 SCpnt->request.sector,
1894 SCpnt->request.nr_sectors,
1895 SCpnt->request.current_nr_sectors,
1896 SCpnt->use_sg,
1897 SCpnt->retries,
1898 SCpnt->allowed,
1899 SCpnt->flags,
1900 SCpnt->timeout_per_command,
1901 SCpnt->timeout,
1902 SCpnt->internal_timeout,
1903 SCpnt->cmnd[0],
1904 SCpnt->sense_buffer[2],
1905 SCpnt->result);
1906 };
1907 printk("wait_for_request = %x\n", wait_for_request);
1908
1909 printk("Dump of pending block device requests\n");
1910 for(i=0; i<MAX_BLKDEV; i++)
1911 if(blk_dev[i].current_request)
1912 {
1913 struct request * req;
1914 printk("%d: ", i);
1915 req = blk_dev[i].current_request;
1916 while(req) {
1917 printk("(%x %d %d %d %d) ",
1918 req->dev,
1919 req->cmd,
1920 req->sector,
1921 req->nr_sectors,
1922 req->current_nr_sectors);
1923 req = req->next;
1924 }
1925 printk("\n");
1926 }
1927 }
1928 #endif
1929
1930