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