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