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\r", 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 \r\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 \r\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\r\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 default :
967 exit = (DRIVER_ERROR | SUGGEST_DIE);
968 }
969
970 switch (status)
971 {
972 case FINISHED:
973 case PENDING:
974 break;
975 case MAYREDO:
976
977 #ifdef DEBUG
978 printk("In MAYREDO, allowing %d retries, have %d\n\r",
979 SCpnt->allowed, SCpnt->retries);
980 #endif
981
982 if ((++SCpnt->retries) < SCpnt->allowed)
983 {
984 if ((SCpnt->retries >= (SCpnt->allowed >> 1))
985 && !(SCpnt->flags & WAS_RESET))
986 {
987 reset(SCpnt);
988 break;
989 }
990
991 }
992 else
993 {
994 status = FINISHED;
995 break;
996 }
997
998
999 case REDO:
1000 if (SCpnt->flags & WAS_SENSE)
1001 scsi_request_sense(SCpnt);
1002 else
1003 {
1004 memcpy ((void *) SCpnt->cmnd,
1005 (void*) SCpnt->data_cmnd,
1006 sizeof(SCpnt->data_cmnd));
1007 SCpnt->request_buffer = SCpnt->buffer;
1008 SCpnt->request_bufflen = SCpnt->bufflen;
1009 internal_cmnd (SCpnt);
1010 };
1011 break;
1012 default:
1013 INTERNAL_ERROR;
1014 }
1015
1016 if (status == FINISHED)
1017 {
1018 #ifdef DEBUG
1019 printk("Calling done function - at address %08x\n", SCpnt->done);
1020 #endif
1021 host_busy[host]--;
1022 wake_up(&host_wait[host]);
1023 SCpnt->result = result | ((exit & 0xff) << 24);
1024 SCpnt->done (SCpnt);
1025 }
1026
1027
1028 #undef FINISHED
1029 #undef REDO
1030 #undef MAYREDO
1031 #undef PENDING
1032 }
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050 int scsi_abort (Scsi_Cmnd * SCpnt, int why)
1051 {
1052 int temp, oldto;
1053 int host = SCpnt->host;
1054
1055 while(1)
1056 {
1057 cli();
1058 if (SCpnt->internal_timeout & IN_ABORT)
1059 {
1060 sti();
1061 while (SCpnt->internal_timeout & IN_ABORT);
1062 }
1063 else
1064 {
1065 SCpnt->internal_timeout |= IN_ABORT;
1066 oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
1067
1068
1069 sti();
1070 if (!host_busy[host] || !scsi_hosts[host].abort(SCpnt, why))
1071 temp = 0;
1072 else
1073 temp = 1;
1074
1075 cli();
1076 SCpnt->internal_timeout &= ~IN_ABORT;
1077 update_timeout(SCpnt, oldto);
1078 sti();
1079 return temp;
1080 }
1081 }
1082 }
1083
1084 int scsi_reset (Scsi_Cmnd * SCpnt)
1085 {
1086 int temp, oldto;
1087 Scsi_Cmnd * SCpnt1;
1088 int host = SCpnt->host;
1089
1090 #ifdef DEBUG
1091 printk("Danger Will Robinson! - SCSI bus for host %d is being reset.\n",host);
1092 #endif
1093 while (1) {
1094 cli();
1095 if (SCpnt->internal_timeout & IN_RESET)
1096 {
1097 sti();
1098 while (SCpnt->internal_timeout & IN_RESET);
1099 }
1100 else
1101 {
1102 SCpnt->internal_timeout |= IN_RESET;
1103 oldto = update_timeout(SCpnt, RESET_TIMEOUT);
1104
1105 if (host_busy[host])
1106 {
1107 sti();
1108 SCpnt1 = host_queue[host];
1109 while(SCpnt1) {
1110 if ((SCpnt1->request.dev > 0) &&
1111 !(SCpnt1->flags & IS_RESETTING) &&
1112 !(SCpnt1->internal_timeout & IN_ABORT))
1113 scsi_abort(SCpnt1, DID_RESET);
1114 SCpnt1 = SCpnt1->next;
1115 };
1116
1117 temp = scsi_hosts[host].reset();
1118 }
1119 else
1120 {
1121 host_busy[host]++;
1122
1123 sti();
1124 temp = scsi_hosts[host].reset();
1125 last_reset[host] = jiffies;
1126 host_busy[host]--;
1127 }
1128
1129 cli();
1130 SCpnt->internal_timeout &= ~IN_RESET;
1131 update_timeout(SCpnt, oldto);
1132 sti();
1133 return temp;
1134 }
1135 }
1136 }
1137
1138
1139 static void scsi_main_timeout(void)
1140 {
1141
1142
1143
1144
1145 int timed_out, host;
1146 Scsi_Cmnd * SCpnt = NULL;
1147
1148 do {
1149 cli();
1150
1151
1152
1153
1154
1155
1156 timed_out = 0;
1157 for(host = 0; host < max_scsi_hosts; host++) {
1158 SCpnt = host_queue[host];
1159 while (SCpnt){
1160 if (SCpnt->timeout != 0 && SCpnt->timeout <= time_elapsed)
1161 {
1162 sti();
1163 SCpnt->timeout = 0;
1164 scsi_times_out(SCpnt);
1165 ++timed_out;
1166 cli();
1167 }
1168 SCpnt = SCpnt->next;
1169 };
1170 };
1171 update_timeout(NULL, 0);
1172 } while (timed_out);
1173 sti();
1174 }
1175
1176
1177
1178
1179
1180 static int time_start, time_elapsed;
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190 static int update_timeout(Scsi_Cmnd * SCset, int timeout)
1191 {
1192 unsigned int least, used, host;
1193 unsigned int oldto;
1194 Scsi_Cmnd * SCpnt = NULL;
1195
1196 cli();
1197
1198
1199
1200
1201
1202 used = (time_start) ? (jiffies - time_start) : 0;
1203
1204
1205
1206
1207
1208
1209
1210 oldto = 0;
1211
1212 if(SCset){
1213 oldto = SCset->timeout - used;
1214 SCset->timeout = timeout + used;
1215 };
1216
1217 least = 0xffffffff;
1218
1219 for(host = 0; host < max_scsi_hosts; host++) {
1220 SCpnt = host_queue[host];
1221 while (SCpnt){
1222 if (SCpnt->timeout > 0 && (SCpnt->timeout -= used) < least)
1223 least = SCpnt->timeout;
1224 SCpnt = SCpnt->next;
1225 };
1226 };
1227
1228
1229
1230
1231
1232
1233 if (least != 0xffffffff)
1234 {
1235 time_start = jiffies;
1236 timer_table[SCSI_TIMER].expires = (time_elapsed = least) + jiffies;
1237 timer_active |= 1 << SCSI_TIMER;
1238 }
1239 else
1240 {
1241 timer_table[SCSI_TIMER].expires = time_start = time_elapsed = 0;
1242 timer_active &= ~(1 << SCSI_TIMER);
1243 }
1244 sti();
1245 return oldto;
1246 }
1247
1248
1249 static unsigned short * dma_malloc_freelist = NULL;
1250 static unsigned int dma_sectors = 0;
1251 unsigned int dma_free_sectors = 0;
1252 unsigned int need_isa_buffer = 0;
1253 static unsigned char * dma_malloc_buffer = NULL;
1254
1255 char *scsi_malloc(unsigned int len)
1256 {
1257 unsigned int nbits, mask;
1258 int i, j;
1259 if((len & 0x1ff) || len > 4096)
1260 panic("Inappropriate buffer size requested");
1261
1262 cli();
1263 nbits = len >> 9;
1264 mask = (1 << nbits) - 1;
1265
1266 for(i=0;i < (dma_sectors >> 4); i++)
1267 for(j=0; j<17-nbits; j++){
1268 if ((dma_malloc_freelist[i] & (mask << j)) == 0){
1269 dma_malloc_freelist[i] |= (mask << j);
1270 sti();
1271 dma_free_sectors -= nbits;
1272 #ifdef DEBUG
1273 printk("SMalloc: %d %x ",len, dma_malloc_buffer + (i << 13) + (j << 9));
1274 #endif
1275 return (void*) ((unsigned long) dma_malloc_buffer + (i << 13) + (j << 9));
1276 };
1277 };
1278 sti();
1279 return NULL;
1280 }
1281
1282 int scsi_free(char *obj, unsigned int len)
1283 {
1284 int offset;
1285 int page, sector, nbits, mask;
1286
1287 #ifdef DEBUG
1288 printk("Sfree %x %d\n",obj, len);
1289 #endif
1290
1291 offset = ((int) obj) - ((int) dma_malloc_buffer);
1292
1293 if (offset < 0) panic("Bad offset");
1294 page = offset >> 13;
1295 sector = offset >> 9;
1296 if(sector >= dma_sectors) panic ("Bad page");
1297
1298 sector = (offset >> 9) & 15;
1299 nbits = len >> 9;
1300 mask = (1 << nbits) - 1;
1301
1302 if ((mask << sector) > 0xffff) panic ("Bad memory alignment");
1303
1304 cli();
1305 if(dma_malloc_freelist[page] & (mask << sector) != (mask<<sector))
1306 panic("Trying to free unused memory");
1307
1308 dma_free_sectors += nbits;
1309 dma_malloc_freelist[page] &= ~(mask << sector);
1310 sti();
1311 return 0;
1312 }
1313
1314
1315
1316
1317
1318
1319
1320 unsigned long scsi_dev_init (unsigned long memory_start,unsigned long memory_end)
1321 {
1322 int i;
1323 int host;
1324 Scsi_Cmnd * SCpnt;
1325 #ifdef FOO_ON_YOU
1326 return;
1327 #endif
1328 timer_table[SCSI_TIMER].fn = scsi_main_timeout;
1329 timer_table[SCSI_TIMER].expires = 0;
1330
1331 scsi_init();
1332
1333 for (i = 0; i < max_scsi_hosts; ++i)
1334 last_reset[i] = 0;
1335
1336 scsi_devices = (Scsi_Device *) memory_start;
1337 scan_scsis();
1338 memory_start += NR_SCSI_DEVICES * sizeof(Scsi_Device);
1339
1340 memory_start = sd_init1(memory_start, memory_end);
1341 memory_start = st_init1(memory_start, memory_end);
1342 memory_start = sr_init1(memory_start, memory_end);
1343
1344 last_cmnd = (Scsi_Cmnd *) memory_start;
1345
1346 SCpnt = last_cmnd;
1347
1348 for (i=0; i< NR_SCSI_DEVICES; i++) {
1349 int j;
1350 switch (scsi_devices[i].type)
1351 {
1352 case TYPE_TAPE :
1353 st_attach(&scsi_devices[i]);
1354 break;
1355 case TYPE_ROM:
1356 sr_attach(&scsi_devices[i]);
1357 break;
1358 case TYPE_DISK:
1359 case TYPE_MOD:
1360 sd_attach(&scsi_devices[i]);
1361 default:
1362 break;
1363 };
1364 if(scsi_devices[i].type != -1){
1365 for(j=0;j<scsi_hosts[scsi_devices[i].host_no].cmd_per_lun;j++){
1366 SCpnt->host = scsi_devices[i].host_no;
1367 SCpnt->target = scsi_devices[i].id;
1368 SCpnt->lun = scsi_devices[i].lun;
1369 SCpnt->index = i;
1370 SCpnt->request.dev = -1;
1371 SCpnt->use_sg = 0;
1372 host = scsi_devices[i].host_no;
1373 if(host_queue[host])
1374 host_queue[host]->prev = SCpnt;
1375 SCpnt->next = host_queue[host];
1376 SCpnt->prev = NULL;
1377 host_queue[host] = SCpnt;
1378 SCpnt++;
1379 };
1380 };
1381 };
1382
1383 memory_start = (int) SCpnt;
1384
1385 if (NR_SD > 0 || NR_SR > 0 || NR_ST > 0)
1386 dma_sectors = 16;
1387
1388 for (i = 0; i < NR_SCSI_DEVICES; ++i) {
1389 int host;
1390 host = scsi_devices[i].host_no;
1391
1392 if(scsi_devices[i].type != TYPE_TAPE)
1393 dma_sectors += ((scsi_hosts[host].sg_tablesize *
1394 sizeof(struct scatterlist) + 511) >> 9) *
1395 scsi_hosts[host].cmd_per_lun;
1396
1397 if(scsi_hosts[host].unchecked_isa_dma &&
1398 memory_end > ISA_DMA_THRESHOLD &&
1399 scsi_devices[i].type != TYPE_TAPE) {
1400 dma_sectors += 32 * scsi_hosts[host].cmd_per_lun;
1401 need_isa_buffer++;
1402 };
1403 };
1404
1405 dma_sectors = (dma_sectors + 15) & 0xfff0;
1406 dma_free_sectors = dma_sectors;
1407
1408 memory_start = (memory_start + 3) & 0xfffffffc;
1409 dma_malloc_freelist = (unsigned short *) memory_start;
1410 memory_start += dma_sectors >> 3;
1411 memset(dma_malloc_freelist, 0, dma_sectors >> 3);
1412
1413 if(memory_start & 1) memory_start++;
1414
1415 dma_malloc_buffer = (char *) memory_start;
1416 memory_start += dma_sectors << 9;
1417
1418 memory_start = sd_init(memory_start, memory_end);
1419 memory_start = st_init(memory_start, memory_end);
1420 memory_start = sr_init(memory_start, memory_end);
1421 return memory_start;
1422 }
1423
1424 static void print_inquiry(unsigned char *data)
1425 {
1426 int i;
1427
1428 printk(" Vendor: ");
1429 for (i = 8; i < 16; i++)
1430 {
1431 if (data[i] >= 20 && i < data[4] + 5)
1432 printk("%c", data[i]);
1433 else
1434 printk(" ");
1435 }
1436
1437 printk(" Model: ");
1438 for (i = 16; i < 32; i++)
1439 {
1440 if (data[i] >= 20 && i < data[4] + 5)
1441 printk("%c", data[i]);
1442 else
1443 printk(" ");
1444 }
1445
1446 printk(" Rev: ");
1447 for (i = 32; i < 36; i++)
1448 {
1449 if (data[i] >= 20 && i < data[4] + 5)
1450 printk("%c", data[i]);
1451 else
1452 printk(" ");
1453 }
1454
1455 printk("\n");
1456
1457 i = data[0] & 0x1f;
1458
1459 printk(" Type: %s ", i == 0x00 ? "Direct-Access " :
1460 i == 0x01 ? "Sequential-Access" :
1461 i == 0x02 ? "Printer " :
1462 i == 0x03 ? "Processor " :
1463 i == 0x04 ? "WORM " :
1464 i == 0x05 ? "CD-ROM " :
1465 i == 0x06 ? "Scanner " :
1466 i == 0x07 ? "Optical Device " :
1467 i == 0x08 ? "Medium Changer " :
1468 i == 0x09 ? "Communications " :
1469 "Unknown " );
1470 printk(" ANSI SCSI revision: %02x", data[2] & 0x07);
1471 if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
1472 printk(" CCS\n");
1473 else
1474 printk("\n");
1475 }