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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #include <asm/system.h>
19 #include <linux/sched.h>
20 #include <linux/timer.h>
21 #include <linux/string.h>
22
23 #include "../blk.h"
24 #include "scsi.h"
25 #include "hosts.h"
26
27
28
29
30
31 #define INTERNAL_ERROR (printk ("Internal error in file %s, line %d.\n", __FILE__, __LINE__), panic(""))
32
33 static void scsi_done (Scsi_Cmnd *SCpnt);
34 static int update_timeout (Scsi_Cmnd *, int);
35 static void print_inquiry(unsigned char *data);
36 static void scsi_times_out (Scsi_Cmnd * SCpnt);
37
38 static int time_start;
39 static int time_elapsed;
40
41
42
43
44
45
46
47
48 int NR_SCSI_DEVICES=0;
49
50 Scsi_Device * scsi_devices = NULL;
51
52 static unsigned char generic_sense[6] = {REQUEST_SENSE, 0,0,0, 255, 0};
53
54
55 Scsi_Cmnd * last_cmnd = NULL;
56
57
58
59
60
61
62
63 #define WAS_RESET 0x01
64 #define WAS_TIMEDOUT 0x02
65 #define WAS_SENSE 0x04
66 #define IS_RESETTING 0x08
67
68 extern int last_reset[];
69
70
71
72
73
74
75
76
77
78
79
80 #ifdef DEBUG
81 #define SCSI_TIMEOUT 500
82 #else
83 #define SCSI_TIMEOUT 100
84 #endif
85
86 #ifdef DEBUG
87 #define SENSE_TIMEOUT SCSI_TIMEOUT
88 #define ABORT_TIMEOUT SCSI_TIMEOUT
89 #define RESET_TIMEOUT SCSI_TIMEOUT
90 #else
91 #define SENSE_TIMEOUT 50
92 #define RESET_TIMEOUT 50
93 #define ABORT_TIMEOUT 50
94 #define MIN_RESET_DELAY 25
95 #endif
96
97
98
99
100
101 struct blist{
102 char * vendor;
103 char * model;
104 char * revision;
105 };
106
107 static struct blist blacklist[] =
108 {{"TANDBERG","TDC 3600","U07"},
109 {"SEAGATE","ST296","921"},
110 {"SONY","CD-ROM CDU-541","4.3d"},
111 {"DENON","DRD-25X","V"},
112 {NULL, NULL, NULL}};
113
114 static int blacklisted(char * response_data){
115 int i = 0;
116 char * pnt;
117 for(i=0; 1; i++){
118 if(blacklist[i].vendor == NULL) return 0;
119 pnt = &response_data[8];
120 while(*pnt && *pnt == ' ') pnt++;
121 if(strncmp(blacklist[i].vendor, pnt,
122 strlen(blacklist[i].vendor))) continue;
123 pnt = &response_data[16];
124 while(*pnt && *pnt == ' ') pnt++;
125 if(strncmp(blacklist[i].model, pnt,
126 strlen(blacklist[i].model))) continue;
127 return 1;
128 };
129 };
130
131
132
133
134
135
136
137
138
139
140 static volatile int in_scan = 0;
141 static int the_result;
142 static void scan_scsis_done (Scsi_Cmnd * SCpnt)
143 {
144
145 #ifdef DEBUG
146 printk ("scan_scsis_done(%d, %06x)\n", SCpnt->host, SCpnt->result);
147 #endif
148 SCpnt->request.dev = 0xfffe;
149 }
150
151
152
153
154
155
156
157
158 static void scan_scsis (void)
159 {
160 int host_nr , dev, lun, type;
161 unsigned char scsi_cmd [12];
162 unsigned char scsi_result [256];
163 Scsi_Cmnd SCmd;
164
165 ++in_scan;
166 lun = 0;
167
168 SCmd.next = NULL;
169 SCmd.prev = NULL;
170 for (host_nr = 0; host_nr < max_scsi_hosts; ++host_nr)
171 if (scsi_hosts[host_nr].present)
172 {
173 host_queue[host_nr] = &SCmd;
174
175 for (dev = 0; dev < 8; ++dev)
176 if (scsi_hosts[host_nr].this_id != dev)
177 for (lun = 0; lun < 8; ++lun)
178 {
179 scsi_devices[NR_SCSI_DEVICES].host_no = host_nr;
180 scsi_devices[NR_SCSI_DEVICES].id = dev;
181 scsi_devices[NR_SCSI_DEVICES].lun = lun;
182 scsi_devices[NR_SCSI_DEVICES].index = NR_SCSI_DEVICES;
183 scsi_devices[NR_SCSI_DEVICES].device_wait = NULL;
184
185 scsi_cmd[0] = TEST_UNIT_READY;
186 scsi_cmd[1] = lun << 5;
187 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
188 scsi_cmd[4] = 0;
189
190 SCmd.host = host_nr;
191 SCmd.target = dev;
192 SCmd.lun = lun;
193
194 SCmd.request.dev = 0xffff;
195 SCmd.use_sg = 0;
196
197 scsi_do_cmd (&SCmd,
198 (void *) scsi_cmd, (void *)
199 scsi_result, 256, scan_scsis_done,
200 SCSI_TIMEOUT + 400, 3);
201
202 while (SCmd.request.dev != 0xfffe);
203
204 if(SCmd.result) {
205 if ((driver_byte(SCmd.result) & DRIVER_SENSE) &&
206 ((SCmd.sense_buffer[0] & 0x70) >> 4) == 7) {
207 if (SCmd.sense_buffer[2] &0xe0)
208 continue;
209 if(((SCmd.sense_buffer[2] & 0xf) != NOT_READY) &&
210 ((SCmd.sense_buffer[2] & 0xf) != UNIT_ATTENTION))
211 continue;
212 }
213 else
214 break;
215 };
216
217
218
219
220
221 scsi_cmd[0] = INQUIRY;
222 scsi_cmd[1] = (lun << 5) & 0xe0;
223 scsi_cmd[2] = 0;
224 scsi_cmd[3] = 0;
225 scsi_cmd[4] = 255;
226 scsi_cmd[5] = 0;
227
228 SCmd.request.dev = 0xffff;
229
230 scsi_do_cmd (&SCmd,
231 (void *) scsi_cmd, (void *)
232 scsi_result, 256, scan_scsis_done,
233 SCSI_TIMEOUT, 3);
234
235 while (SCmd.request.dev != 0xfffe);
236
237 the_result = SCmd.result;
238
239 if(the_result) break;
240
241
242 if (!the_result)
243 {
244 scsi_devices[NR_SCSI_DEVICES].
245 removable = (0x80 &
246 scsi_result[1]) >> 7;
247 scsi_devices[NR_SCSI_DEVICES].
248 changed = 0;
249 scsi_devices[NR_SCSI_DEVICES].
250 access_count = 0;
251 scsi_devices[NR_SCSI_DEVICES].
252 busy = 0;
253
254
255
256
257
258
259 switch (type = scsi_result[0])
260 {
261 case TYPE_TAPE :
262 case TYPE_DISK :
263 case TYPE_MOD :
264 scsi_devices[NR_SCSI_DEVICES].writeable = 1;
265 break;
266 case TYPE_WORM :
267 case TYPE_ROM :
268 scsi_devices[NR_SCSI_DEVICES].writeable = 0;
269 break;
270 default :
271 type = -1;
272 }
273
274 scsi_devices[NR_SCSI_DEVICES].random =
275 (type == TYPE_TAPE) ? 0 : 1;
276 scsi_devices[NR_SCSI_DEVICES].type = type;
277
278 if (type != -1)
279 {
280 print_inquiry(scsi_result);
281 switch(type){
282 case TYPE_TAPE:
283 printk("Detected scsi tape st%d at scsi%d, id %d, lun %d\n", MAX_ST,
284 host_nr , dev, lun);
285 if(NR_ST != -1) ++MAX_ST;
286 break;
287 case TYPE_ROM:
288 printk("Detected scsi CD-ROM sr%d at scsi%d, id %d, lun %d\n", MAX_SR,
289 host_nr , dev, lun);
290 if(NR_SR != -1) ++MAX_SR;
291 break;
292 case TYPE_DISK:
293 case TYPE_MOD:
294 printk("Detected scsi disk sd%d at scsi%d, id %d, lun %d\n", MAX_SD,
295 host_nr , dev, lun);
296 if(NR_SD != -1) ++MAX_SD;
297 break;
298 default:
299 break;
300 };
301
302 scsi_devices[NR_SCSI_DEVICES].scsi_level =
303 scsi_result[2] & 0x07;
304 if (scsi_devices[NR_SCSI_DEVICES].scsi_level >= 2 ||
305 (scsi_devices[NR_SCSI_DEVICES].scsi_level == 1 &&
306 (scsi_result[3] & 0x0f) == 1))
307 scsi_devices[NR_SCSI_DEVICES].scsi_level++;
308
309
310
311 if(strncmp("INSITE", &scsi_result[8], 6) == 0 &&
312 strncmp("Floptical F*8I", &scsi_result[16], 16) == 0) {
313 scsi_cmd[0] = MODE_SENSE;
314 scsi_cmd[1] = (lun << 5) & 0xe0;
315 scsi_cmd[2] = 0x2e;
316 scsi_cmd[3] = 0;
317 scsi_cmd[4] = 0x2a;
318 scsi_cmd[5] = 0;
319
320 SCmd.request.dev = 0xffff;
321
322 scsi_do_cmd (&SCmd,
323 (void *) scsi_cmd, (void *)
324 scsi_result, 0x2a, scan_scsis_done,
325 SCSI_TIMEOUT, 3);
326
327 while (SCmd.request.dev != 0xfffe);
328 };
329
330 ++NR_SCSI_DEVICES;
331
332
333 if(blacklisted(scsi_result)) break;
334
335
336 if((scsi_result[2] & 0x07) == 1 &&
337 (scsi_result[3] & 0x0f) == 0) break;
338 }
339 }
340 }
341 host_queue[host_nr] = NULL;
342 }
343
344 for (host_nr = 0; host_nr < max_scsi_hosts; ++host_nr)
345 if (scsi_hosts[host_nr].present)
346 if(host_queue[host_nr]) panic("host_queue not cleared");
347
348 printk("scsi : detected ");
349 if(NR_SD != -1)
350 printk("%d SCSI disk%s ", MAX_SD, (MAX_SD != 1) ? "s" : "");
351
352 if(NR_ST != -1)
353 printk("%d tape%s ", MAX_ST, (MAX_ST != 1) ? "s" : "");
354
355 if(NR_SR != -1)
356 printk("%d CD-ROM drive%s ", MAX_SR, (MAX_SR != 1) ? "s" : "");
357
358 printk("total.\n");
359 in_scan = 0;
360 }
361
362
363
364
365
366 #define NORMAL_TIMEOUT 0
367 #define IN_ABORT 1
368 #define IN_RESET 2
369
370
371
372
373
374
375 static void scsi_times_out (Scsi_Cmnd * SCpnt)
376 {
377
378 switch (SCpnt->internal_timeout & (IN_ABORT | IN_RESET))
379 {
380 case NORMAL_TIMEOUT:
381 if (!in_scan)
382 printk("SCSI host %d timed out - aborting command\n",
383 SCpnt->host);
384
385 if (!scsi_abort (SCpnt, DID_TIME_OUT))
386 return;
387 case IN_ABORT:
388 printk("SCSI host %d abort() timed out - reseting\n",
389 SCpnt->host);
390 if (!scsi_reset (SCpnt))
391 return;
392 case IN_RESET:
393 case (IN_ABORT | IN_RESET):
394 printk("Unable to reset scsi host %d\n",SCpnt->host);
395 panic("");
396 default:
397 INTERNAL_ERROR;
398 }
399
400 }
401
402
403
404
405
406
407
408
409 Scsi_Cmnd * request_queueable (struct request * req, int index)
410 {
411 int host;
412 Scsi_Cmnd * SCpnt = NULL;
413
414 if ((index < 0) || (index > NR_SCSI_DEVICES))
415 panic ("Index number in allocate_device() is out of range.\n");
416
417 if (req && req->dev <= 0)
418 panic("Invalid device in allocate_device");
419
420 host = scsi_devices[index].host_no;
421 SCpnt = host_queue[host];
422 while(SCpnt){
423 if(SCpnt->target == scsi_devices[index].id &&
424 SCpnt->lun == scsi_devices[index].lun)
425 if(SCpnt->request.dev < 0) break;
426 SCpnt = SCpnt->next;
427 };
428
429 if (!SCpnt) return NULL;
430
431 if (scsi_hosts[host].can_queue
432 && host_busy[host] >= scsi_hosts[host].can_queue) return NULL;
433
434 if (req) {
435 memcpy(&SCpnt->request, req, sizeof(struct request));
436 req->dev = -1;
437 } else {
438 SCpnt->request.dev = 0xffff;
439 SCpnt->request.waiting = NULL;
440 };
441
442 SCpnt->use_sg = 0;
443 return SCpnt;
444 }
445
446
447
448
449
450
451
452
453
454
455 Scsi_Cmnd * allocate_device (struct request ** reqp, int index, int wait)
456 {
457 int host, dev = -1;
458 struct request * req = NULL;
459 Scsi_Cmnd * SCpnt = NULL;
460 Scsi_Cmnd * SCwait = NULL;
461
462 if ((index < 0) || (index > NR_SCSI_DEVICES))
463 panic ("Index number in allocate_device() is out of range.\n");
464
465 if (reqp) req = *reqp;
466
467 if (req && (dev = req->dev) <= 0)
468 panic("Invalid device in allocate_device");
469
470 host = scsi_devices[index].host_no;
471
472 while (1==1){
473 SCpnt = host_queue[host];
474 while(SCpnt){
475 if(SCpnt->target == scsi_devices[index].id &&
476 SCpnt->lun == scsi_devices[index].lun) {
477 SCwait = SCpnt;
478 if(SCpnt->request.dev < 0) break;
479 };
480 SCpnt = SCpnt->next;
481 };
482 cli();
483
484 if (req && ((req->dev < 0) || (req->dev != dev))) return NULL;
485 if (!SCpnt || SCpnt->request.dev >= 0)
486 {
487 sti();
488 if(!wait) return NULL;
489 if (!SCwait) {
490 printk("Attempt to allocate device index %d, target %d, lun %d\n",
491 index, scsi_devices[index].id ,scsi_devices[index].lun);
492 panic("No device found in allocate_device\n");
493 };
494 SCSI_SLEEP(&scsi_devices[SCwait->index].device_wait,
495 (SCwait->request.dev > 0));
496 } else {
497 if (req) {
498 memcpy(&SCpnt->request, req, sizeof(struct request));
499 req->dev = -1;
500 *reqp = req->next;
501 } else {
502 SCpnt->request.dev = 0xffff;
503 SCpnt->request.waiting = NULL;
504 };
505 sti();
506 break;
507 };
508 };
509
510 SCpnt->use_sg = 0;
511 return SCpnt;
512 }
513
514
515
516
517
518 inline void internal_cmnd (Scsi_Cmnd * SCpnt)
519 {
520 int temp, host;
521
522 #ifdef DEBUG_DELAY
523 int clock;
524 #endif
525
526 host = SCpnt->host;
527
528 if ((host < 0) || (host > max_scsi_hosts))
529 panic ("Host number in internal_cmnd() is out of range.\n");
530
531
532
533
534
535 temp = last_reset[host];
536 while (jiffies < temp);
537
538 update_timeout(SCpnt, SCpnt->timeout_per_command);
539
540
541
542
543
544 #ifdef DEBUG
545 printk("internal_cmnd (host = %d, target = %d, command = %08x, buffer = %08x, \n"
546 "bufflen = %d, done = %08x)\n", SCpnt->host, SCpnt->target, SCpnt->cmnd, SCpnt->buffer, SCpnt->bufflen, SCpnt->done);
547 #endif
548
549 if (scsi_hosts[host].can_queue)
550 {
551 #ifdef DEBUG
552 printk("queuecommand : routine at %08x\n",
553 scsi_hosts[host].queuecommand);
554 #endif
555 scsi_hosts[host].queuecommand (SCpnt, scsi_done);
556 }
557 else
558 {
559
560 #ifdef DEBUG
561 printk("command() : routine at %08x\n", scsi_hosts[host].command);
562 #endif
563 temp=scsi_hosts[host].command (SCpnt);
564 SCpnt->result = temp;
565 #ifdef DEBUG_DELAY
566 clock = jiffies + 400;
567 while (jiffies < clock);
568 printk("done(host = %d, result = %04x) : routine at %08x\n", host, temp, done);
569 #endif
570 scsi_done(SCpnt);
571 }
572 #ifdef DEBUG
573 printk("leaving internal_cmnd()\n");
574 #endif
575 }
576
577 static void scsi_request_sense (Scsi_Cmnd * SCpnt)
578 {
579 cli();
580 SCpnt->flags |= WAS_SENSE;
581 update_timeout(SCpnt, SENSE_TIMEOUT);
582 sti();
583
584
585 memcpy ((void *) SCpnt->cmnd , (void *) generic_sense,
586 sizeof(generic_sense));
587
588 SCpnt->cmnd[1] = SCpnt->lun << 5;
589 SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);
590
591 SCpnt->request_buffer = &SCpnt->sense_buffer;
592 SCpnt->request_bufflen = sizeof(SCpnt->sense_buffer);
593 internal_cmnd (SCpnt);
594 }
595
596
597
598
599
600
601
602
603
604
605 void scsi_do_cmd (Scsi_Cmnd * SCpnt, const void *cmnd ,
606 void *buffer, unsigned bufflen, void (*done)(Scsi_Cmnd *),
607 int timeout, int retries
608 )
609 {
610 int host = SCpnt->host;
611
612 #ifdef DEBUG
613 {
614 int i;
615 int target = SCpnt->target;
616 printk ("scsi_do_cmd (host = %d, target = %d, buffer =%08x, "
617 "bufflen = %d, done = %08x, timeout = %d, retries = %d)\n"
618 "command : " , host, target, buffer, bufflen, done, timeout, retries);
619 for (i = 0; i < 10; ++i)
620 printk ("%02x ", ((unsigned char *) cmnd)[i]);
621 printk("\n");
622 };
623 #endif
624
625 if ((host < 0) || (host >= max_scsi_hosts) || !scsi_hosts[host].present)
626 {
627 printk ("Invalid or not present host number. %d\n", host);
628 panic("");
629 }
630
631
632
633
634
635
636
637
638
639
640 while (1==1){
641 cli();
642 if (scsi_hosts[host].can_queue
643 && host_busy[host] >= scsi_hosts[host].can_queue)
644 {
645 sti();
646 SCSI_SLEEP(&host_wait[host],
647 (host_busy[host] >= scsi_hosts[host].can_queue));
648 } else {
649 host_busy[host]++;
650 sti();
651 break;
652 };
653 };
654
655
656
657
658
659
660
661
662 memcpy ((void *) SCpnt->data_cmnd , (void *) cmnd, 10);
663 #if 0
664 SCpnt->host = host;
665 SCpnt->target = target;
666 SCpnt->lun = (SCpnt->data_cmnd[1] >> 5);
667 #endif
668 SCpnt->bufflen = bufflen;
669 SCpnt->buffer = buffer;
670 SCpnt->flags=0;
671 SCpnt->retries=0;
672 SCpnt->allowed=retries;
673 SCpnt->done = done;
674 SCpnt->timeout_per_command = timeout;
675
676 memcpy ((void *) SCpnt->cmnd , (void *) cmnd, 10);
677 SCpnt->request_buffer = buffer;
678 SCpnt->request_bufflen = bufflen;
679
680
681
682 SCpnt->internal_timeout = 0;
683 internal_cmnd (SCpnt);
684
685 #ifdef DEBUG
686 printk ("Leaving scsi_do_cmd()\n");
687 #endif
688 }
689
690
691
692
693
694
695
696
697
698 static void reset (Scsi_Cmnd * SCpnt)
699 {
700 #ifdef DEBUG
701 printk("reset(%d)\n", SCpnt->host);
702 #endif
703
704 SCpnt->flags |= (WAS_RESET | IS_RESETTING);
705 scsi_reset(SCpnt);
706
707 #ifdef DEBUG
708 printk("performing request sense\n");
709 #endif
710
711 scsi_request_sense (SCpnt);
712 }
713
714
715
716 static int check_sense (Scsi_Cmnd * SCpnt)
717 {
718 if (((SCpnt->sense_buffer[0] & 0x70) >> 4) == 7) {
719 if (SCpnt->sense_buffer[2] &0xe0)
720 return SUGGEST_ABORT;
721 switch (SCpnt->sense_buffer[2] & 0xf)
722 {
723 case NO_SENSE:
724 case RECOVERED_ERROR:
725 return 0;
726
727 case ABORTED_COMMAND:
728 case NOT_READY:
729 return SUGGEST_RETRY;
730 case UNIT_ATTENTION:
731 return SUGGEST_ABORT;
732
733
734 case COPY_ABORTED:
735 case VOLUME_OVERFLOW:
736 case MISCOMPARE:
737
738 case MEDIUM_ERROR:
739 return SUGGEST_REMAP;
740 case BLANK_CHECK:
741 case DATA_PROTECT:
742 case HARDWARE_ERROR:
743 case ILLEGAL_REQUEST:
744 default:
745 return SUGGEST_ABORT;
746 }
747 }
748 else
749 return SUGGEST_RETRY;
750 }
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773 static void scsi_done (Scsi_Cmnd * SCpnt)
774 {
775 int status=0;
776 int exit=0;
777 int checked;
778 int oldto;
779 int host = SCpnt->host;
780 int result = SCpnt->result;
781 oldto = update_timeout(SCpnt, 0);
782
783 #define FINISHED 0
784 #define MAYREDO 1
785 #define REDO 3
786 #define PENDING 4
787
788 #ifdef DEBUG
789 printk("In scsi_done(host = %d, result = %06x)\n", host, result);
790 #endif
791 if (host > max_scsi_hosts || host < 0)
792 {
793 update_timeout(SCpnt, 0);
794 panic("scsi_done() called with invalid host number.\n");
795 }
796
797 switch (host_byte(result))
798 {
799 case DID_OK:
800 if (SCpnt->flags & IS_RESETTING)
801 {
802 SCpnt->flags &= ~IS_RESETTING;
803 status = REDO;
804 break;
805 }
806
807 if (status_byte(result) && (SCpnt->flags &
808 WAS_SENSE))
809 {
810 SCpnt->flags &= ~WAS_SENSE;
811 SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
812
813 if (!(SCpnt->flags & WAS_RESET))
814 {
815 reset(SCpnt);
816 return;
817 }
818 else
819 {
820 exit = (DRIVER_HARD | SUGGEST_ABORT);
821 status = FINISHED;
822 }
823 }
824 else switch(msg_byte(result))
825 {
826 case COMMAND_COMPLETE:
827 switch (status_byte(result))
828 {
829 case GOOD:
830 if (SCpnt->flags & WAS_SENSE)
831 {
832 #ifdef DEBUG
833 printk ("In scsi_done, GOOD status, COMMAND COMPLETE, parsing sense information.\n");
834 #endif
835
836 SCpnt->flags &= ~WAS_SENSE;
837 SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
838
839 switch (checked = check_sense(SCpnt))
840 {
841 case 0:
842 #ifdef DEBUG
843 printk("NO SENSE. status = REDO\n");
844 #endif
845
846 update_timeout(SCpnt, oldto);
847 status = REDO;
848 break;
849 case SUGGEST_REMAP:
850 case SUGGEST_RETRY:
851 #ifdef DEBUG
852 printk("SENSE SUGGEST REMAP or SUGGEST RETRY - status = MAYREDO\n");
853 #endif
854
855 status = MAYREDO;
856 exit = DRIVER_SENSE | SUGGEST_RETRY;
857 break;
858 case SUGGEST_ABORT:
859 #ifdef DEBUG
860 printk("SENSE SUGGEST ABORT - status = FINISHED");
861 #endif
862
863 status = FINISHED;
864 exit = DRIVER_SENSE | SUGGEST_ABORT;
865 break;
866 default:
867 printk ("Internal error %s %s \n", __FILE__,
868 __LINE__);
869 }
870 }
871 else
872 {
873 #ifdef DEBUG
874 printk("COMMAND COMPLETE message returned, status = FINISHED. \n");
875 #endif
876
877 exit = DRIVER_OK;
878 status = FINISHED;
879 }
880 break;
881
882 case CHECK_CONDITION:
883
884 #ifdef DEBUG
885 printk("CHECK CONDITION message returned, performing request sense.\n");
886 #endif
887
888 scsi_request_sense (SCpnt);
889 status = PENDING;
890 break;
891
892 case CONDITION_GOOD:
893 case INTERMEDIATE_GOOD:
894 case INTERMEDIATE_C_GOOD:
895 #ifdef DEBUG
896 printk("CONDITION GOOD, INTERMEDIATE GOOD, or INTERMEDIATE CONDITION GOOD recieved and ignored. \n");
897 #endif
898 break;
899
900 case BUSY:
901 #ifdef DEBUG
902 printk("BUSY message returned, performing REDO");
903 #endif
904 update_timeout(SCpnt, oldto);
905 status = REDO;
906 break;
907
908 case RESERVATION_CONFLICT:
909 reset(SCpnt);
910 return;
911 #if 0
912 exit = DRIVER_SOFT | SUGGEST_ABORT;
913 status = MAYREDO;
914 break;
915 #endif
916 default:
917 printk ("Internal error %s %s \n"
918 "status byte = %d \n", __FILE__,
919 __LINE__, status_byte(result));
920
921 }
922 break;
923 default:
924 panic ("unsupported message byte recieved.");
925 }
926 break;
927 case DID_TIME_OUT:
928 #ifdef DEBUG
929 printk("Host returned DID_TIME_OUT - ");
930 #endif
931
932 if (SCpnt->flags & WAS_TIMEDOUT)
933 {
934 #ifdef DEBUG
935 printk("Aborting\n");
936 #endif
937 exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
938 }
939 else
940 {
941 #ifdef DEBUG
942 printk ("Retrying.\n");
943 #endif
944 SCpnt->flags |= WAS_TIMEDOUT;
945 status = REDO;
946 }
947 break;
948 case DID_BUS_BUSY:
949 case DID_PARITY:
950 status = REDO;
951 break;
952 case DID_NO_CONNECT:
953 #ifdef DEBUG
954 printk("Couldn't connect.\n");
955 #endif
956 exit = (DRIVER_HARD | SUGGEST_ABORT);
957 break;
958 case DID_ERROR:
959 status = MAYREDO;
960 exit = (DRIVER_HARD | SUGGEST_ABORT);
961 break;
962 case DID_BAD_TARGET:
963 case DID_ABORT:
964 exit = (DRIVER_INVALID | SUGGEST_ABORT);
965 break;
966 case DID_RESET:
967 if(msg_byte(result) == GOOD &&
968 status_byte(result) == CHECK_CONDITION) {
969 scsi_request_sense (SCpnt);
970 status = PENDING;
971 break;
972 };
973 status=REDO;
974 exit = SUGGEST_RETRY;
975 break;
976 default :
977 exit = (DRIVER_ERROR | SUGGEST_DIE);
978 }
979
980 switch (status)
981 {
982 case FINISHED:
983 case PENDING:
984 break;
985 case MAYREDO:
986
987 #ifdef DEBUG
988 printk("In MAYREDO, allowing %d retries, have %d\n",
989 SCpnt->allowed, SCpnt->retries);
990 #endif
991
992 if ((++SCpnt->retries) < SCpnt->allowed)
993 {
994 if ((SCpnt->retries >= (SCpnt->allowed >> 1))
995 && !(SCpnt->flags & WAS_RESET))
996 {
997 reset(SCpnt);
998 break;
999 }
1000
1001 }
1002 else
1003 {
1004 status = FINISHED;
1005 break;
1006 }
1007
1008
1009 case REDO:
1010 if (SCpnt->flags & WAS_SENSE)
1011 scsi_request_sense(SCpnt);
1012 else
1013 {
1014 memcpy ((void *) SCpnt->cmnd,
1015 (void*) SCpnt->data_cmnd,
1016 sizeof(SCpnt->data_cmnd));
1017 SCpnt->request_buffer = SCpnt->buffer;
1018 SCpnt->request_bufflen = SCpnt->bufflen;
1019 internal_cmnd (SCpnt);
1020 };
1021 break;
1022 default:
1023 INTERNAL_ERROR;
1024 }
1025
1026 if (status == FINISHED)
1027 {
1028 #ifdef DEBUG
1029 printk("Calling done function - at address %08x\n", SCpnt->done);
1030 #endif
1031 host_busy[host]--;
1032 wake_up(&host_wait[host]);
1033 SCpnt->result = result | ((exit & 0xff) << 24);
1034 SCpnt->done (SCpnt);
1035 }
1036
1037
1038 #undef FINISHED
1039 #undef REDO
1040 #undef MAYREDO
1041 #undef PENDING
1042 }
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060 int scsi_abort (Scsi_Cmnd * SCpnt, int why)
1061 {
1062 int temp, oldto;
1063 int host = SCpnt->host;
1064
1065 while(1)
1066 {
1067 cli();
1068 if (SCpnt->internal_timeout & IN_ABORT)
1069 {
1070 sti();
1071 while (SCpnt->internal_timeout & IN_ABORT);
1072 }
1073 else
1074 {
1075 SCpnt->internal_timeout |= IN_ABORT;
1076 oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
1077
1078
1079 sti();
1080 if (!host_busy[host] || !scsi_hosts[host].abort(SCpnt, why))
1081 temp = 0;
1082 else
1083 temp = 1;
1084
1085 cli();
1086 SCpnt->internal_timeout &= ~IN_ABORT;
1087 update_timeout(SCpnt, oldto);
1088 sti();
1089 return temp;
1090 }
1091 }
1092 }
1093
1094 int scsi_reset (Scsi_Cmnd * SCpnt)
1095 {
1096 int temp, oldto;
1097 Scsi_Cmnd * SCpnt1;
1098 int host = SCpnt->host;
1099
1100 #ifdef DEBUG
1101 printk("Danger Will Robinson! - SCSI bus for host %d is being reset.\n",host);
1102 #endif
1103 while (1) {
1104 cli();
1105 if (SCpnt->internal_timeout & IN_RESET)
1106 {
1107 sti();
1108 while (SCpnt->internal_timeout & IN_RESET);
1109 }
1110 else
1111 {
1112 SCpnt->internal_timeout |= IN_RESET;
1113 oldto = update_timeout(SCpnt, RESET_TIMEOUT);
1114
1115 if (host_busy[host])
1116 {
1117 sti();
1118 SCpnt1 = host_queue[host];
1119 while(SCpnt1) {
1120 if ((SCpnt1->request.dev > 0) &&
1121 !(SCpnt1->flags & IS_RESETTING) &&
1122 !(SCpnt1->internal_timeout & IN_ABORT))
1123 scsi_abort(SCpnt1, DID_RESET);
1124 SCpnt1 = SCpnt1->next;
1125 };
1126
1127 temp = scsi_hosts[host].reset();
1128 }
1129 else
1130 {
1131 host_busy[host]++;
1132
1133 sti();
1134 temp = scsi_hosts[host].reset();
1135 last_reset[host] = jiffies;
1136 host_busy[host]--;
1137 }
1138
1139 cli();
1140 SCpnt->internal_timeout &= ~IN_RESET;
1141 update_timeout(SCpnt, oldto);
1142 sti();
1143 return temp;
1144 }
1145 }
1146 }
1147
1148
1149 static void scsi_main_timeout(void)
1150 {
1151
1152
1153
1154
1155 int timed_out, host;
1156 Scsi_Cmnd * SCpnt = NULL;
1157
1158 do {
1159 cli();
1160
1161
1162
1163
1164
1165
1166 timed_out = 0;
1167 for(host = 0; host < max_scsi_hosts; host++) {
1168 SCpnt = host_queue[host];
1169 while (SCpnt){
1170 if (SCpnt->timeout != 0 && SCpnt->timeout <= time_elapsed)
1171 {
1172 sti();
1173 SCpnt->timeout = 0;
1174 scsi_times_out(SCpnt);
1175 ++timed_out;
1176 cli();
1177 }
1178 SCpnt = SCpnt->next;
1179 };
1180 };
1181 update_timeout(NULL, 0);
1182 } while (timed_out);
1183 sti();
1184 }
1185
1186
1187
1188
1189
1190 static int time_start, time_elapsed;
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200 static int update_timeout(Scsi_Cmnd * SCset, int timeout)
1201 {
1202 unsigned int least, used, host;
1203 unsigned int oldto;
1204 Scsi_Cmnd * SCpnt = NULL;
1205
1206 cli();
1207
1208
1209
1210
1211
1212 used = (time_start) ? (jiffies - time_start) : 0;
1213
1214
1215
1216
1217
1218
1219
1220 oldto = 0;
1221
1222 if(SCset){
1223 oldto = SCset->timeout - used;
1224 SCset->timeout = timeout + used;
1225 };
1226
1227 least = 0xffffffff;
1228
1229 for(host = 0; host < max_scsi_hosts; host++) {
1230 SCpnt = host_queue[host];
1231 while (SCpnt){
1232 if (SCpnt->timeout > 0 && (SCpnt->timeout -= used) < least)
1233 least = SCpnt->timeout;
1234 SCpnt = SCpnt->next;
1235 };
1236 };
1237
1238
1239
1240
1241
1242
1243 if (least != 0xffffffff)
1244 {
1245 time_start = jiffies;
1246 timer_table[SCSI_TIMER].expires = (time_elapsed = least) + jiffies;
1247 timer_active |= 1 << SCSI_TIMER;
1248 }
1249 else
1250 {
1251 timer_table[SCSI_TIMER].expires = time_start = time_elapsed = 0;
1252 timer_active &= ~(1 << SCSI_TIMER);
1253 }
1254 sti();
1255 return oldto;
1256 }
1257
1258
1259 static unsigned short * dma_malloc_freelist = NULL;
1260 static unsigned int dma_sectors = 0;
1261 unsigned int dma_free_sectors = 0;
1262 unsigned int need_isa_buffer = 0;
1263 static unsigned char * dma_malloc_buffer = NULL;
1264
1265 char *scsi_malloc(unsigned int len)
1266 {
1267 unsigned int nbits, mask;
1268 int i, j;
1269 if((len & 0x1ff) || len > 4096)
1270 panic("Inappropriate buffer size requested");
1271
1272 cli();
1273 nbits = len >> 9;
1274 mask = (1 << nbits) - 1;
1275
1276 for(i=0;i < (dma_sectors >> 4); i++)
1277 for(j=0; j<17-nbits; j++){
1278 if ((dma_malloc_freelist[i] & (mask << j)) == 0){
1279 dma_malloc_freelist[i] |= (mask << j);
1280 sti();
1281 dma_free_sectors -= nbits;
1282 #ifdef DEBUG
1283 printk("SMalloc: %d %x ",len, dma_malloc_buffer + (i << 13) + (j << 9));
1284 #endif
1285 return (void*) ((unsigned long) dma_malloc_buffer + (i << 13) + (j << 9));
1286 };
1287 };
1288 sti();
1289 return NULL;
1290 }
1291
1292 int scsi_free(char *obj, unsigned int len)
1293 {
1294 int offset;
1295 int page, sector, nbits, mask;
1296
1297 #ifdef DEBUG
1298 printk("Sfree %x %d\n",obj, len);
1299 #endif
1300
1301 offset = ((int) obj) - ((int) dma_malloc_buffer);
1302
1303 if (offset < 0) panic("Bad offset");
1304 page = offset >> 13;
1305 sector = offset >> 9;
1306 if(sector >= dma_sectors) panic ("Bad page");
1307
1308 sector = (offset >> 9) & 15;
1309 nbits = len >> 9;
1310 mask = (1 << nbits) - 1;
1311
1312 if ((mask << sector) > 0xffff) panic ("Bad memory alignment");
1313
1314 cli();
1315 if(dma_malloc_freelist[page] & (mask << sector) != (mask<<sector))
1316 panic("Trying to free unused memory");
1317
1318 dma_free_sectors += nbits;
1319 dma_malloc_freelist[page] &= ~(mask << sector);
1320 sti();
1321 return 0;
1322 }
1323
1324
1325
1326
1327
1328
1329
1330 unsigned long scsi_dev_init (unsigned long memory_start,unsigned long memory_end)
1331 {
1332 int i;
1333 int host;
1334 Scsi_Cmnd * SCpnt;
1335 #ifdef FOO_ON_YOU
1336 return;
1337 #endif
1338 timer_table[SCSI_TIMER].fn = scsi_main_timeout;
1339 timer_table[SCSI_TIMER].expires = 0;
1340
1341 scsi_init();
1342
1343 for (i = 0; i < max_scsi_hosts; ++i)
1344 last_reset[i] = 0;
1345
1346 scsi_devices = (Scsi_Device *) memory_start;
1347 scan_scsis();
1348 memory_start += NR_SCSI_DEVICES * sizeof(Scsi_Device);
1349
1350 memory_start = sd_init1(memory_start, memory_end);
1351 memory_start = st_init1(memory_start, memory_end);
1352 memory_start = sr_init1(memory_start, memory_end);
1353
1354 last_cmnd = (Scsi_Cmnd *) memory_start;
1355
1356 SCpnt = last_cmnd;
1357
1358 for (i=0; i< NR_SCSI_DEVICES; i++) {
1359 int j;
1360 switch (scsi_devices[i].type)
1361 {
1362 case TYPE_TAPE :
1363 st_attach(&scsi_devices[i]);
1364 break;
1365 case TYPE_ROM:
1366 sr_attach(&scsi_devices[i]);
1367 break;
1368 case TYPE_DISK:
1369 case TYPE_MOD:
1370 sd_attach(&scsi_devices[i]);
1371 default:
1372 break;
1373 };
1374 if(scsi_devices[i].type != -1){
1375 for(j=0;j<scsi_hosts[scsi_devices[i].host_no].cmd_per_lun;j++){
1376 SCpnt->host = scsi_devices[i].host_no;
1377 SCpnt->target = scsi_devices[i].id;
1378 SCpnt->lun = scsi_devices[i].lun;
1379 SCpnt->index = i;
1380 SCpnt->request.dev = -1;
1381 SCpnt->use_sg = 0;
1382 host = scsi_devices[i].host_no;
1383 if(host_queue[host])
1384 host_queue[host]->prev = SCpnt;
1385 SCpnt->next = host_queue[host];
1386 SCpnt->prev = NULL;
1387 host_queue[host] = SCpnt;
1388 SCpnt++;
1389 };
1390 };
1391 };
1392
1393 memory_start = (int) SCpnt;
1394
1395 if (NR_SD > 0 || NR_SR > 0 || NR_ST > 0)
1396 dma_sectors = 16;
1397
1398 for (i = 0; i < NR_SCSI_DEVICES; ++i) {
1399 int host;
1400 host = scsi_devices[i].host_no;
1401
1402 if(scsi_devices[i].type != TYPE_TAPE)
1403 dma_sectors += ((scsi_hosts[host].sg_tablesize *
1404 sizeof(struct scatterlist) + 511) >> 9) *
1405 scsi_hosts[host].cmd_per_lun;
1406
1407 if(scsi_hosts[host].unchecked_isa_dma &&
1408 memory_end > ISA_DMA_THRESHOLD &&
1409 scsi_devices[i].type != TYPE_TAPE) {
1410 dma_sectors += 32 * scsi_hosts[host].cmd_per_lun;
1411 need_isa_buffer++;
1412 };
1413 };
1414
1415 dma_sectors = (dma_sectors + 15) & 0xfff0;
1416 dma_free_sectors = dma_sectors;
1417
1418 memory_start = (memory_start + 3) & 0xfffffffc;
1419 dma_malloc_freelist = (unsigned short *) memory_start;
1420 memory_start += dma_sectors >> 3;
1421 memset(dma_malloc_freelist, 0, dma_sectors >> 3);
1422
1423 if(memory_start & 1) memory_start++;
1424
1425 dma_malloc_buffer = (char *) memory_start;
1426 memory_start += dma_sectors << 9;
1427
1428 memory_start = sd_init(memory_start, memory_end);
1429 memory_start = st_init(memory_start, memory_end);
1430 memory_start = sr_init(memory_start, memory_end);
1431 return memory_start;
1432 }
1433
1434 static void print_inquiry(unsigned char *data)
1435 {
1436 int i;
1437
1438 printk(" Vendor: ");
1439 for (i = 8; i < 16; i++)
1440 {
1441 if (data[i] >= 20 && i < data[4] + 5)
1442 printk("%c", data[i]);
1443 else
1444 printk(" ");
1445 }
1446
1447 printk(" Model: ");
1448 for (i = 16; i < 32; i++)
1449 {
1450 if (data[i] >= 20 && i < data[4] + 5)
1451 printk("%c", data[i]);
1452 else
1453 printk(" ");
1454 }
1455
1456 printk(" Rev: ");
1457 for (i = 32; i < 36; i++)
1458 {
1459 if (data[i] >= 20 && i < data[4] + 5)
1460 printk("%c", data[i]);
1461 else
1462 printk(" ");
1463 }
1464
1465 printk("\n");
1466
1467 i = data[0] & 0x1f;
1468
1469 printk(" Type: %s ", i == 0x00 ? "Direct-Access " :
1470 i == 0x01 ? "Sequential-Access" :
1471 i == 0x02 ? "Printer " :
1472 i == 0x03 ? "Processor " :
1473 i == 0x04 ? "WORM " :
1474 i == 0x05 ? "CD-ROM " :
1475 i == 0x06 ? "Scanner " :
1476 i == 0x07 ? "Optical Device " :
1477 i == 0x08 ? "Medium Changer " :
1478 i == 0x09 ? "Communications " :
1479 "Unknown " );
1480 printk(" ANSI SCSI revision: %02x", data[2] & 0x07);
1481 if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
1482 printk(" CCS\n");
1483 else
1484 printk("\n");
1485 }