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