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