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