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