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