This source file includes following definitions.
- blacklisted
- scsi_make_blocked_list
- scan_scsis_done
- scsi_luns_setup
- scan_scsis
- scsi_times_out
- request_queueable
- allocate_device
- internal_cmnd
- scsi_request_sense
- scsi_do_cmd
- reset
- check_sense
- scsi_done
- scsi_abort
- scsi_reset
- scsi_main_timeout
- update_timeout
- scsi_malloc
- scsi_free
- scsi_init_malloc
- scsi_init_free
- scsi_dev_init
- print_inquiry
- scsi_register_host
- scsi_unregister_host
- scsi_register_module
- scsi_unregister_module
- scsi_dump_status
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 #include <asm/system.h>
22 #include <linux/sched.h>
23 #include <linux/timer.h>
24 #include <linux/string.h>
25 #include <linux/malloc.h>
26 #include <asm/irq.h>
27 #include <asm/dma.h>
28 #include <linux/ioport.h>
29
30 #include "../block/blk.h"
31 #include "scsi.h"
32 #include "hosts.h"
33 #include "constants.h"
34
35 #undef USE_STATIC_SCSI_MEMORY
36
37
38
39
40
41
42 const unsigned char scsi_command_size[8] = { 6, 10, 10, 12, 12, 12, 10, 10 };
43
44 #define INTERNAL_ERROR (panic ("Internal error in file %s, line %d.\n", __FILE__, __LINE__))
45
46 static void scsi_done (Scsi_Cmnd *SCpnt);
47 static int update_timeout (Scsi_Cmnd *, int);
48 static void print_inquiry(unsigned char *data);
49 static void scsi_times_out (Scsi_Cmnd * SCpnt, int pid);
50
51 static int time_start;
52 static int time_elapsed;
53 static volatile struct Scsi_Host * host_active = NULL;
54 #define SCSI_BLOCK(HOST) ((HOST->block && host_active && HOST != host_active) \
55 || (HOST->can_queue && HOST->host_busy >= HOST->can_queue))
56
57 #define MAX_SCSI_DEVICE_CODE 10
58 const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] =
59 {
60 "Direct-Access ",
61 "Sequential-Access",
62 "Printer ",
63 "Processor ",
64 "WORM ",
65 "CD-ROM ",
66 "Scanner ",
67 "Optical Device ",
68 "Medium Changer ",
69 "Communications "
70 };
71
72
73
74
75
76
77
78
79 Scsi_Device * scsi_devices = NULL;
80
81
82 unsigned long scsi_pid = 0;
83
84 static unsigned char generic_sense[6] = {REQUEST_SENSE, 0,0,0, 255, 0};
85
86
87 Scsi_Cmnd * last_cmnd = NULL;
88
89
90
91
92
93
94
95 #define WAS_RESET 0x01
96 #define WAS_TIMEDOUT 0x02
97 #define WAS_SENSE 0x04
98 #define IS_RESETTING 0x08
99 #define IS_ABORTING 0x10
100 #define ASKED_FOR_SENSE 0x20
101
102
103
104
105
106
107
108
109
110
111
112 #ifdef DEBUG_TIMEOUT
113 static void scsi_dump_status(void);
114 #endif
115
116
117 #ifdef DEBUG
118 #define SCSI_TIMEOUT 500
119 #else
120 #define SCSI_TIMEOUT 100
121 #endif
122
123 #ifdef DEBUG
124 #define SENSE_TIMEOUT SCSI_TIMEOUT
125 #define ABORT_TIMEOUT SCSI_TIMEOUT
126 #define RESET_TIMEOUT SCSI_TIMEOUT
127 #else
128 #define SENSE_TIMEOUT 50
129 #define RESET_TIMEOUT 50
130 #define ABORT_TIMEOUT 50
131 #endif
132
133 #define MIN_RESET_DELAY 100
134
135
136 #define MIN_RESET_PERIOD 1000
137
138
139
140
141
142 struct blist{
143 char * vendor;
144 char * model;
145 char * revision;
146 };
147
148 static struct blist blacklist[] =
149 {
150 {"CHINON","CD-ROM CDS-431","H42"},
151 {"CHINON","CD-ROM CDS-535","Q14"},
152 {"DENON","DRD-25X","V"},
153 {"HITACHI","DK312C","CM81"},
154 {"HITACHI","DK314C","CR21" },
155 {"IMS", "CDD521/10","2.06"},
156 {"MAXTOR","XT-3280","PR02"},
157 {"MAXTOR","XT-4380S","B3C"},
158 {"MAXTOR","MXT-1240S","I1.2"},
159 {"MAXTOR","XT-4170S","B5A"},
160 {"MAXTOR","XT-8760S","B7B"},
161 {"NEC","CD-ROM DRIVE:841","1.0"},
162 {"RODIME","RO3000S","2.33"},
163 {"SEAGATE", "ST157N", "\004|j"},
164
165 {"SEAGATE", "ST296","921"},
166 {"SONY","CD-ROM CDU-541","4.3d"},
167 {"SONY","CD-ROM CDU-55S","1.0i"},
168 {"TANDBERG","TDC 3600","U07"},
169 {"TEAC","CD-ROM","1.06"},
170
171 {"TEXEL","CD-ROM","1.06"},
172
173 {"QUANTUM","LPS525S","3110"},
174 {"QUANTUM","PD1225S","3110"},
175 {"MEDIAVIS","CDR-H93MV","1.31"},
176 {"SANKYO", "CP525","6.64"},
177 {"HP", "C1750A", "3226"},
178 {"HP", "C1790A", ""},
179 {"HP", "C2500A", ""},
180 {NULL, NULL, NULL}};
181
182 static int blacklisted(unsigned char * response_data){
183 int i = 0;
184 unsigned char * pnt;
185 for(i=0; 1; i++){
186 if(blacklist[i].vendor == NULL) return 0;
187 pnt = &response_data[8];
188 while(*pnt && *pnt == ' ') pnt++;
189 if(memcmp(blacklist[i].vendor, pnt,
190 strlen(blacklist[i].vendor))) continue;
191 pnt = &response_data[16];
192 while(*pnt && *pnt == ' ') pnt++;
193 if(memcmp(blacklist[i].model, pnt,
194 strlen(blacklist[i].model))) continue;
195 return 1;
196 }
197 }
198
199
200
201
202
203
204
205
206
207
208 volatile int in_scan_scsis = 0;
209 static int the_result;
210
211 void scsi_make_blocked_list(void) {
212 int block_count = 0, index;
213 unsigned int flags;
214 struct Scsi_Host * sh[128], * shpnt;
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234 save_flags(flags);
235 cli();
236 host_active = NULL;
237
238 for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next) {
239
240 #if 0
241
242
243
244
245
246 if (shpnt->unchecked_isa_dma) shpnt->wish_block = 1;
247 #endif
248
249 if (shpnt->wish_block) sh[block_count++] = shpnt;
250 }
251
252 if (block_count == 1) sh[0]->block = NULL;
253
254 else if (block_count > 1) {
255
256 for(index = 0; index < block_count - 1; index++) {
257 sh[index]->block = sh[index + 1];
258 printk("scsi%d : added to blocked host list.\n",
259 sh[index]->host_no);
260 }
261
262 sh[block_count - 1]->block = sh[0];
263 printk("scsi%d : added to blocked host list.\n",
264 sh[index]->host_no);
265 }
266
267 restore_flags(flags);
268 }
269
270 static void scan_scsis_done (Scsi_Cmnd * SCpnt)
271 {
272
273 #ifdef DEBUG
274 printk ("scan_scsis_done(%d, %06x)\n", SCpnt->host, SCpnt->result);
275 #endif
276 SCpnt->request.dev = 0xfffe;
277
278 if (SCpnt->request.sem != NULL)
279 up(SCpnt->request.sem);
280 }
281
282 #ifdef CONFIG_SCSI_MULTI_LUN
283 static int max_scsi_luns = 8;
284 #else
285 static int max_scsi_luns = 1;
286 #endif
287
288 void scsi_luns_setup(char *str, int *ints) {
289 if (ints[0] != 1)
290 printk("scsi_luns_setup : usage max_scsi_luns=n (n should be between 1 and 8)\n");
291 else
292 max_scsi_luns = ints[1];
293 }
294
295
296
297
298
299
300
301
302
303 void scan_scsis (struct Scsi_Host * shpnt)
304 {
305 int dev, lun, type;
306 unsigned char scsi_cmd [12];
307 unsigned char scsi_result0 [256];
308 unsigned char * scsi_result;
309 Scsi_Device * SDpnt, *SDtail;
310 struct Scsi_Device_Template * sdtpnt;
311 Scsi_Cmnd *SCpnt;
312
313 ++in_scan_scsis;
314 lun = 0;
315 type = -1;
316 SCpnt = (Scsi_Cmnd *) scsi_init_malloc(sizeof(Scsi_Cmnd), GFP_ATOMIC|GFP_DMA);
317 SDpnt = (Scsi_Device *) scsi_init_malloc(sizeof (Scsi_Device), GFP_ATOMIC);
318 SDtail = scsi_devices;
319
320 if(scsi_devices) while(SDtail->next) SDtail = SDtail->next;
321
322
323 scsi_result = ((current == task[0] || !shpnt->unchecked_isa_dma)
324 ? &scsi_result0[0] : scsi_malloc(512));
325
326
327 shpnt->host_queue = SCpnt;
328
329 for (dev = 0; dev < 8; ++dev)
330 if (shpnt->this_id != dev)
331
332
333
334
335 for (lun = 0; lun < max_scsi_luns; ++lun)
336 {
337 memset(SDpnt, 0, sizeof(Scsi_Device));
338 SDpnt->host = shpnt;
339 SDpnt->id = dev;
340 SDpnt->lun = lun;
341
342
343 SDpnt->type = -1;
344
345
346
347
348 SDpnt->borken = 1;
349
350 scsi_cmd[0] = TEST_UNIT_READY;
351 scsi_cmd[1] = lun << 5;
352 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[4] = scsi_cmd[5] = 0;
353
354 memset(SCpnt, 0, sizeof(Scsi_Cmnd));
355 SCpnt->host = SDpnt->host;
356 SCpnt->device = SDpnt;
357 SCpnt->target = SDpnt->id;
358 SCpnt->lun = SDpnt->lun;
359
360
361 SCpnt->request.sem = NULL;
362
363 SCpnt->request.dev = 0xffff;
364
365 scsi_do_cmd (SCpnt, (void *) scsi_cmd, (void *) scsi_result,
366 256, scan_scsis_done, SCSI_TIMEOUT + 400, 5);
367
368
369
370
371 if (current == task[0])
372 while (SCpnt->request.dev != 0xfffe) barrier();
373 else if (SCpnt->request.dev != 0xfffe) {
374 struct semaphore sem = MUTEX_LOCKED;
375
376 SCpnt->request.sem = &sem;
377 down(&sem);
378
379
380 while (SCpnt->request.dev != 0xfffe) schedule();
381 }
382
383 #if defined(DEBUG) || defined(DEBUG_INIT)
384 printk("scsi: scan SCSIS id %d lun %d\n", dev, lun);
385 printk("scsi: return code %08x\n", SCpnt->result);
386 #endif
387
388
389 if(SCpnt->result) {
390 if (((driver_byte(SCpnt->result) & DRIVER_SENSE) ||
391 (status_byte(SCpnt->result) & CHECK_CONDITION)) &&
392 ((SCpnt->sense_buffer[0] & 0x70) >> 4) == 7) {
393 if (SCpnt->sense_buffer[2] &0xe0)
394 continue;
395 if(((SCpnt->sense_buffer[2] & 0xf) != NOT_READY) &&
396 ((SCpnt->sense_buffer[2] & 0xf) != UNIT_ATTENTION))
397 continue;
398 }
399 else
400 break;
401 }
402
403 #if defined (DEBUG) || defined(DEBUG_INIT)
404 printk("scsi: performing INQUIRY\n");
405 #endif
406
407
408
409
410 scsi_cmd[0] = INQUIRY;
411 scsi_cmd[1] = (lun << 5) & 0xe0;
412 scsi_cmd[2] = 0;
413 scsi_cmd[3] = 0;
414 scsi_cmd[4] = 255;
415 scsi_cmd[5] = 0;
416
417 SCpnt->request.dev = 0xffff;
418 SCpnt->cmd_len = 0;
419
420 scsi_do_cmd (SCpnt, (void *) scsi_cmd, (void *) scsi_result,
421 256, scan_scsis_done, SCSI_TIMEOUT, 3);
422
423 if (current == task[0])
424 while (SCpnt->request.dev != 0xfffe) barrier();
425 else if (SCpnt->request.dev != 0xfffe) {
426 struct semaphore sem = MUTEX_LOCKED;
427
428 SCpnt->request.sem = &sem;
429 down(&sem);
430
431
432 while (SCpnt->request.dev != 0xfffe) schedule();
433 }
434
435 the_result = SCpnt->result;
436
437 #if defined(DEBUG) || defined(DEBUG_INIT)
438 if (!the_result)
439 printk("scsi: INQUIRY successful\n");
440 else
441 printk("scsi: INQUIRY failed with code %08x\n", the_result);
442 #endif
443
444 if(the_result) break;
445
446
447
448 if (!the_result)
449 {
450
451 if (!strncmp(scsi_result+8,"TOSHIBA",7) &&
452 !strncmp(scsi_result+16,"CD-ROM",6) &&
453 scsi_result[0] == TYPE_DISK) {
454 scsi_result[0] = TYPE_ROM;
455 scsi_result[1] |= 0x80;
456 }
457
458 if (!strncmp(scsi_result+8,"NEC",3)) {
459 if (!strncmp(scsi_result+16,"CD-ROM DRIVE:84 ",16) ||
460 !strncmp(scsi_result+16,"CD-ROM DRIVE:25",15))
461 SDpnt->manufacturer = SCSI_MAN_NEC_OLDCDR;
462 else
463 SDpnt->manufacturer = SCSI_MAN_NEC;
464 } else if (!strncmp(scsi_result+8,"TOSHIBA",7))
465 SDpnt->manufacturer = SCSI_MAN_TOSHIBA;
466 else
467 SDpnt->manufacturer = SCSI_MAN_UNKNOWN;
468
469 SDpnt->removable = (0x80 &
470 scsi_result[1]) >> 7;
471 SDpnt->lockable = SDpnt->removable;
472 SDpnt->changed = 0;
473 SDpnt->access_count = 0;
474 SDpnt->busy = 0;
475
476
477
478
479
480
481 switch (type = (scsi_result[0] & 0x1f))
482 {
483 case TYPE_TAPE :
484 case TYPE_DISK :
485 case TYPE_MOD :
486 case TYPE_PROCESSOR :
487 case TYPE_SCANNER :
488 SDpnt->writeable = 1;
489 break;
490 case TYPE_WORM :
491 case TYPE_ROM :
492 SDpnt->writeable = 0;
493 break;
494 default :
495 #if 0
496 #ifdef DEBUG
497 printk("scsi: unknown type %d\n", type);
498 print_inquiry(scsi_result);
499 #endif
500 type = -1;
501 #endif
502 }
503
504 SDpnt->soft_reset =
505 (scsi_result[7] & 1) && ((scsi_result[3] & 7) == 2);
506 SDpnt->random = (type == TYPE_TAPE) ? 0 : 1;
507 SDpnt->type = (type & 0x1f);
508
509 if (type != -1)
510 {
511 print_inquiry(scsi_result);
512
513 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
514 if(sdtpnt->detect) SDpnt->attached +=
515 (*sdtpnt->detect)(SDpnt);
516
517 SDpnt->scsi_level = scsi_result[2] & 0x07;
518 if (SDpnt->scsi_level >= 2 ||
519 (SDpnt->scsi_level == 1 &&
520 (scsi_result[3] & 0x0f) == 1))
521 SDpnt->scsi_level++;
522
523
524
525
526
527 SDpnt->tagged_queue = 0;
528
529 if ((SDpnt->scsi_level >= SCSI_2) &&
530 (scsi_result[7] & 2)) {
531 SDpnt->tagged_supported = 1;
532 SDpnt->current_tag = 0;
533 }
534
535
536
537
538
539
540 SDpnt->disconnect = 0;
541
542
543
544
545
546
547
548
549
550 if(strncmp("TEXEL", (char *) &scsi_result[8], 5) != 0 ||
551 strncmp("CD-ROM", (char *) &scsi_result[16], 6) != 0
552
553
554
555
556
557
558 #ifdef notyet
559 || (strncmp("1.06", (char *) &scsi_result[[, 4) != 0)))
560 #endif
561 )
562 SDpnt->borken = 0;
563
564
565
566
567 if(memcmp("INSITE", &scsi_result[8], 6) == 0 &&
568 (memcmp("Floptical F*8I", &scsi_result[16], 16) == 0
569 || memcmp("I325VM", &scsi_result[16], 6) == 0)) {
570 printk("Unlocked floptical drive.\n");
571 SDpnt->lockable = 0;
572 scsi_cmd[0] = MODE_SENSE;
573 scsi_cmd[1] = (lun << 5) & 0xe0;
574 scsi_cmd[2] = 0x2e;
575 scsi_cmd[3] = 0;
576 scsi_cmd[4] = 0x2a;
577 scsi_cmd[5] = 0;
578
579 SCpnt->request.dev = 0xffff;
580 SCpnt->cmd_len = 0;
581
582 scsi_do_cmd (SCpnt, (void *) scsi_cmd,
583 (void *) scsi_result, 0x2a, scan_scsis_done,
584 SCSI_TIMEOUT, 3);
585
586 if (current == task[0])
587 while (SCpnt->request.dev != 0xfffe) barrier();
588 else if (SCpnt->request.dev != 0xfffe) {
589 struct semaphore sem = MUTEX_LOCKED;
590
591 SCpnt->request.sem = &sem;
592 down(&sem);
593
594
595 while (SCpnt->request.dev != 0xfffe) schedule();
596 }
597 }
598
599 if(SDtail)
600 SDtail->next = SDpnt;
601 else
602 scsi_devices = SDpnt;
603 SDtail = SDpnt;
604
605 SDpnt = (Scsi_Device *) scsi_init_malloc(sizeof (Scsi_Device), GFP_ATOMIC);
606
607
608 if(blacklisted(scsi_result)) break;
609
610 if ((scsi_result[2] & 0x07) == 0)
611 break;
612
613
614 if((scsi_result[2] & 0x07) == 1 &&
615 (scsi_result[3] & 0x0f) == 0) break;
616 }
617 }
618 }
619
620 shpnt->host_queue = NULL;
621
622
623 scsi_init_free((char *) SDpnt, sizeof(Scsi_Device));
624
625 scsi_init_free((char *) SCpnt, sizeof(Scsi_Cmnd));
626
627
628
629 if (scsi_result != &scsi_result0[0]) scsi_free(scsi_result, 512);
630
631 in_scan_scsis = 0;
632 }
633
634
635
636
637
638 #define NORMAL_TIMEOUT 0
639 #define IN_ABORT 1
640 #define IN_RESET 2
641
642
643
644
645
646
647 static void scsi_times_out (Scsi_Cmnd * SCpnt, int pid)
648 {
649
650 switch (SCpnt->internal_timeout & (IN_ABORT | IN_RESET))
651 {
652 case NORMAL_TIMEOUT:
653 if (!in_scan_scsis) {
654 #ifdef DEBUG_TIMEOUT
655 scsi_dump_status();
656 #endif
657 }
658
659 if (!scsi_abort (SCpnt, DID_TIME_OUT, pid))
660 return;
661 case IN_ABORT:
662 printk("SCSI host %d abort() timed out - resetting\n",
663 SCpnt->host->host_no);
664 if (!scsi_reset (SCpnt))
665 return;
666 case IN_RESET:
667 case (IN_ABORT | IN_RESET):
668
669
670
671 printk("Unable to reset scsi host %d - ",SCpnt->host->host_no);
672 printk("probably a SCSI bus hang.\n");
673 return;
674
675 default:
676 INTERNAL_ERROR;
677 }
678
679 }
680
681
682
683
684
685
686
687
688 Scsi_Cmnd * request_queueable (struct request * req, Scsi_Device * device)
689 {
690 Scsi_Cmnd * SCpnt = NULL;
691 int tablesize;
692 struct buffer_head * bh, *bhp;
693
694 if (!device)
695 panic ("No device passed to request_queueable().\n");
696
697 if (req && req->dev <= 0)
698 panic("Invalid device in request_queueable");
699
700 SCpnt = device->host->host_queue;
701 while(SCpnt){
702 if(SCpnt->target == device->id &&
703 SCpnt->lun == device->lun)
704 if(SCpnt->request.dev < 0) break;
705 SCpnt = SCpnt->next;
706 }
707
708 if (!SCpnt) return NULL;
709
710 if (SCSI_BLOCK(device->host)) return NULL;
711
712 if (req) {
713 memcpy(&SCpnt->request, req, sizeof(struct request));
714 tablesize = device->host->sg_tablesize;
715 bhp = bh = req->bh;
716 if(!tablesize) bh = NULL;
717
718
719 while(req->nr_sectors && bh){
720 bhp = bhp->b_reqnext;
721 if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
722 req->nr_sectors -= bh->b_size >> 9;
723 req->sector += bh->b_size >> 9;
724 if(!tablesize) break;
725 bh = bhp;
726 }
727 if(req->nr_sectors && bh && bh->b_reqnext){
728 SCpnt->request.bhtail = bh;
729 req->bh = bh->b_reqnext;
730 bh->b_reqnext = NULL;
731 bh = req->bh;
732
733
734 SCpnt->request.nr_sectors -= req->nr_sectors;
735 req->current_nr_sectors = bh->b_size >> 9;
736 req->buffer = bh->b_data;
737 SCpnt->request.sem = NULL;
738 } else {
739 req->dev = -1;
740 wake_up(&wait_for_request);
741 }
742 } else {
743 SCpnt->request.dev = 0xffff;
744 SCpnt->request.sem = NULL;
745 }
746
747 SCpnt->use_sg = 0;
748 SCpnt->old_use_sg = 0;
749 SCpnt->transfersize = 0;
750 SCpnt->underflow = 0;
751 SCpnt->cmd_len = 0;
752 return SCpnt;
753 }
754
755
756
757
758
759
760
761
762
763
764 Scsi_Cmnd * allocate_device (struct request ** reqp, Scsi_Device * device,
765 int wait)
766 {
767 int dev = -1;
768 struct request * req = NULL;
769 int tablesize;
770 unsigned int flags;
771 struct buffer_head * bh, *bhp;
772 struct Scsi_Host * host;
773 Scsi_Cmnd * SCpnt = NULL;
774 Scsi_Cmnd * SCwait = NULL;
775
776 if (!device)
777 panic ("No device passed to allocate_device().\n");
778
779 if (reqp) req = *reqp;
780
781
782 if (req && (dev = req->dev) <= 0) return NULL;
783
784 host = device->host;
785
786 if (intr_count && SCSI_BLOCK(host)) return NULL;
787
788 while (1==1){
789 SCpnt = host->host_queue;
790 while(SCpnt){
791 if(SCpnt->target == device->id &&
792 SCpnt->lun == device->lun) {
793 SCwait = SCpnt;
794 if(SCpnt->request.dev < 0) break;
795 }
796 SCpnt = SCpnt->next;
797 }
798 save_flags(flags);
799 cli();
800
801 if (req && ((req->dev < 0) || (req->dev != dev))) {
802 restore_flags(flags);
803 return NULL;
804 }
805 if (!SCpnt || SCpnt->request.dev >= 0)
806 {
807 restore_flags(flags);
808 if(!wait) return NULL;
809 if (!SCwait) {
810 printk("Attempt to allocate device target %d, lun %d\n",
811 device->id ,device->lun);
812 panic("No device found in allocate_device\n");
813 }
814 SCSI_SLEEP(&device->device_wait,
815 (SCwait->request.dev > 0));
816 } else {
817 if (req) {
818 memcpy(&SCpnt->request, req, sizeof(struct request));
819 tablesize = device->host->sg_tablesize;
820 bhp = bh = req->bh;
821 if(!tablesize) bh = NULL;
822
823
824 while(req->nr_sectors && bh){
825 bhp = bhp->b_reqnext;
826 if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
827 req->nr_sectors -= bh->b_size >> 9;
828 req->sector += bh->b_size >> 9;
829 if(!tablesize) break;
830 bh = bhp;
831 }
832 if(req->nr_sectors && bh && bh->b_reqnext){
833 SCpnt->request.bhtail = bh;
834 req->bh = bh->b_reqnext;
835 bh->b_reqnext = NULL;
836 bh = req->bh;
837
838 SCpnt->request.nr_sectors -= req->nr_sectors;
839 req->current_nr_sectors = bh->b_size >> 9;
840 req->buffer = bh->b_data;
841 SCpnt->request.sem = NULL;
842 }
843 else
844 {
845 req->dev = -1;
846 *reqp = req->next;
847 wake_up(&wait_for_request);
848 }
849 } else {
850 SCpnt->request.dev = 0xffff;
851 SCpnt->request.sem = NULL;
852 }
853 restore_flags(flags);
854 break;
855 }
856 }
857
858 SCpnt->use_sg = 0;
859 SCpnt->old_use_sg = 0;
860 SCpnt->transfersize = 0;
861 SCpnt->cmd_len = 0;
862 SCpnt->underflow = 0;
863 return SCpnt;
864 }
865
866
867
868
869
870 inline void internal_cmnd (Scsi_Cmnd * SCpnt)
871 {
872 int temp;
873 struct Scsi_Host * host;
874 unsigned int flags;
875 #ifdef DEBUG_DELAY
876 int clock;
877 #endif
878
879 if ((unsigned long) &SCpnt < current->kernel_stack_page)
880 panic("Kernel stack overflow.");
881
882 host = SCpnt->host;
883
884
885
886
887
888 save_flags(flags);
889 sti();
890 temp = host->last_reset + MIN_RESET_DELAY;
891 while (jiffies < temp);
892 restore_flags(flags);
893
894 update_timeout(SCpnt, SCpnt->timeout_per_command);
895
896
897
898
899
900 #ifdef DEBUG
901 printk("internal_cmnd (host = %d, target = %d, command = %08x, buffer = %08x, \n"
902 "bufflen = %d, done = %08x)\n", SCpnt->host->host_no, SCpnt->target, SCpnt->cmnd, SCpnt->buffer, SCpnt->bufflen, SCpnt->done);
903 #endif
904
905 if (host->can_queue)
906 {
907 #ifdef DEBUG
908 printk("queuecommand : routine at %08x\n",
909 host->hostt->queuecommand);
910 #endif
911
912
913
914
915
916
917
918
919 if(!intr_count && SCpnt->host->irq)
920 disable_irq(SCpnt->host->irq);
921
922 host->hostt->queuecommand (SCpnt, scsi_done);
923
924 if(!intr_count && SCpnt->host->irq)
925 enable_irq(SCpnt->host->irq);
926 }
927 else
928 {
929
930 #ifdef DEBUG
931 printk("command() : routine at %08x\n", host->hostt->command);
932 #endif
933 temp=host->hostt->command (SCpnt);
934 SCpnt->result = temp;
935 #ifdef DEBUG_DELAY
936 clock = jiffies + 400;
937 while (jiffies < clock);
938 printk("done(host = %d, result = %04x) : routine at %08x\n", host->host_no, temp);
939 #endif
940 scsi_done(SCpnt);
941 }
942 #ifdef DEBUG
943 printk("leaving internal_cmnd()\n");
944 #endif
945 }
946
947 static void scsi_request_sense (Scsi_Cmnd * SCpnt)
948 {
949 unsigned int flags;
950
951 save_flags(flags);
952 cli();
953 SCpnt->flags |= WAS_SENSE | ASKED_FOR_SENSE;
954 update_timeout(SCpnt, SENSE_TIMEOUT);
955 restore_flags(flags);
956
957
958 memcpy ((void *) SCpnt->cmnd , (void *) generic_sense,
959 sizeof(generic_sense));
960
961 SCpnt->cmnd[1] = SCpnt->lun << 5;
962 SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);
963
964 SCpnt->request_buffer = &SCpnt->sense_buffer;
965 SCpnt->request_bufflen = sizeof(SCpnt->sense_buffer);
966 SCpnt->use_sg = 0;
967 SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
968 internal_cmnd (SCpnt);
969 }
970
971
972
973
974
975
976
977
978
979
980 void scsi_do_cmd (Scsi_Cmnd * SCpnt, const void *cmnd ,
981 void *buffer, unsigned bufflen, void (*done)(Scsi_Cmnd *),
982 int timeout, int retries
983 )
984 {
985 unsigned long flags;
986 struct Scsi_Host * host = SCpnt->host;
987
988 #ifdef DEBUG
989 {
990 int i;
991 int target = SCpnt->target;
992 printk ("scsi_do_cmd (host = %d, target = %d, buffer =%08x, "
993 "bufflen = %d, done = %08x, timeout = %d, retries = %d)\n"
994 "command : " , host->host_no, target, buffer, bufflen, done, timeout, retries);
995 for (i = 0; i < 10; ++i)
996 printk ("%02x ", ((unsigned char *) cmnd)[i]);
997 printk("\n");
998 }
999 #endif
1000
1001 if (!host)
1002 {
1003 panic ("Invalid or not present host.\n");
1004 }
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015 save_flags(flags);
1016 cli();
1017 SCpnt->pid = scsi_pid++;
1018
1019 while (SCSI_BLOCK(host)) {
1020 restore_flags(flags);
1021 SCSI_SLEEP(&host->host_wait, SCSI_BLOCK(host));
1022 cli();
1023 }
1024
1025 if (host->block) host_active = host;
1026
1027 host->host_busy++;
1028 restore_flags(flags);
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038 memcpy ((void *) SCpnt->data_cmnd , (void *) cmnd, 12);
1039 #if 0
1040 SCpnt->host = host;
1041 SCpnt->target = target;
1042 SCpnt->lun = (SCpnt->data_cmnd[1] >> 5);
1043 #endif
1044 SCpnt->bufflen = bufflen;
1045 SCpnt->buffer = buffer;
1046 SCpnt->flags=0;
1047 SCpnt->retries=0;
1048 SCpnt->allowed=retries;
1049 SCpnt->done = done;
1050 SCpnt->timeout_per_command = timeout;
1051
1052 memcpy ((void *) SCpnt->cmnd , (void *) cmnd, 12);
1053
1054
1055 memset ((void *) SCpnt->sense_buffer, 0, sizeof SCpnt->sense_buffer);
1056 SCpnt->request_buffer = buffer;
1057 SCpnt->request_bufflen = bufflen;
1058 SCpnt->old_use_sg = SCpnt->use_sg;
1059 if (SCpnt->cmd_len == 0)
1060 SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
1061 SCpnt->old_cmd_len = SCpnt->cmd_len;
1062
1063
1064
1065 SCpnt->internal_timeout = 0;
1066 SCpnt->abort_reason = 0;
1067 internal_cmnd (SCpnt);
1068
1069 #ifdef DEBUG
1070 printk ("Leaving scsi_do_cmd()\n");
1071 #endif
1072 }
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082 static void reset (Scsi_Cmnd * SCpnt)
1083 {
1084 #ifdef DEBUG
1085 printk("scsi: reset(%d)\n", SCpnt->host->host_no);
1086 #endif
1087
1088 SCpnt->flags |= (WAS_RESET | IS_RESETTING);
1089 scsi_reset(SCpnt);
1090
1091 #ifdef DEBUG
1092 printk("performing request sense\n");
1093 #endif
1094
1095 #if 0
1096 if(SCpnt->flags & NEEDS_JUMPSTART) {
1097 SCpnt->flags &= ~NEEDS_JUMPSTART;
1098 scsi_request_sense (SCpnt);
1099 }
1100 #endif
1101 }
1102
1103
1104
1105 static int check_sense (Scsi_Cmnd * SCpnt)
1106 {
1107
1108
1109
1110 if (((SCpnt->sense_buffer[0] & 0x70) >> 4) != 7) {
1111 if(!(SCpnt->flags & ASKED_FOR_SENSE))
1112 return SUGGEST_SENSE;
1113 else
1114 return SUGGEST_RETRY;
1115 }
1116
1117 SCpnt->flags &= ~ASKED_FOR_SENSE;
1118
1119 #ifdef DEBUG_INIT
1120 printk("scsi%d : ", SCpnt->host->host_no);
1121 print_sense("", SCpnt);
1122 printk("\n");
1123 #endif
1124 if (SCpnt->sense_buffer[2] &0xe0)
1125 return SUGGEST_ABORT;
1126
1127 switch (SCpnt->sense_buffer[2] & 0xf)
1128 {
1129 case NO_SENSE:
1130 return 0;
1131 case RECOVERED_ERROR:
1132 return SUGGEST_IS_OK;
1133
1134 case ABORTED_COMMAND:
1135 return SUGGEST_RETRY;
1136 case NOT_READY:
1137 case UNIT_ATTENTION:
1138 return SUGGEST_ABORT;
1139
1140
1141 case COPY_ABORTED:
1142 case VOLUME_OVERFLOW:
1143 case MISCOMPARE:
1144
1145 case MEDIUM_ERROR:
1146 return SUGGEST_REMAP;
1147 case BLANK_CHECK:
1148 case DATA_PROTECT:
1149 case HARDWARE_ERROR:
1150 case ILLEGAL_REQUEST:
1151 default:
1152 return SUGGEST_ABORT;
1153 }
1154 }
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177 static void scsi_done (Scsi_Cmnd * SCpnt)
1178 {
1179 int status=0;
1180 int exit=0;
1181 int checked;
1182 int oldto;
1183 struct Scsi_Host * host = SCpnt->host;
1184 int result = SCpnt->result;
1185 oldto = update_timeout(SCpnt, 0);
1186
1187 #ifdef DEBUG_TIMEOUT
1188 if(result) printk("Non-zero result in scsi_done %x %d:%d\n",
1189 result, SCpnt->target, SCpnt->lun);
1190 #endif
1191
1192
1193
1194 if(host_byte(result) == DID_ABORT && SCpnt->abort_reason)
1195 SCpnt->result = result = (result & 0xff00ffff) |
1196 (SCpnt->abort_reason << 16);
1197
1198
1199 #define FINISHED 0
1200 #define MAYREDO 1
1201 #define REDO 3
1202 #define PENDING 4
1203
1204 #ifdef DEBUG
1205 printk("In scsi_done(host = %d, result = %06x)\n", host->host_no, result);
1206 #endif
1207
1208 if(SCpnt->flags & WAS_SENSE)
1209 {
1210 SCpnt->use_sg = SCpnt->old_use_sg;
1211 SCpnt->cmd_len = SCpnt->old_cmd_len;
1212 }
1213
1214 switch (host_byte(result))
1215 {
1216 case DID_OK:
1217 if (status_byte(result) && (SCpnt->flags & WAS_SENSE))
1218
1219 {
1220 SCpnt->flags &= ~WAS_SENSE;
1221 SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
1222
1223 if (!(SCpnt->flags & WAS_RESET))
1224 {
1225 printk("scsi%d : target %d lun %d request sense failed, performing reset.\n",
1226 SCpnt->host->host_no, SCpnt->target, SCpnt->lun);
1227 reset(SCpnt);
1228 return;
1229 }
1230 else
1231 {
1232 exit = (DRIVER_HARD | SUGGEST_ABORT);
1233 status = FINISHED;
1234 }
1235 }
1236 else switch(msg_byte(result))
1237 {
1238 case COMMAND_COMPLETE:
1239 switch (status_byte(result))
1240 {
1241 case GOOD:
1242 if (SCpnt->flags & WAS_SENSE)
1243 {
1244 #ifdef DEBUG
1245 printk ("In scsi_done, GOOD status, COMMAND COMPLETE, parsing sense information.\n");
1246 #endif
1247
1248 SCpnt->flags &= ~WAS_SENSE;
1249 SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
1250
1251 switch (checked = check_sense(SCpnt))
1252 {
1253 case SUGGEST_SENSE:
1254 case 0:
1255 #ifdef DEBUG
1256 printk("NO SENSE. status = REDO\n");
1257 #endif
1258
1259 update_timeout(SCpnt, oldto);
1260 status = REDO;
1261 break;
1262 case SUGGEST_IS_OK:
1263 break;
1264 case SUGGEST_REMAP:
1265 case SUGGEST_RETRY:
1266 #ifdef DEBUG
1267 printk("SENSE SUGGEST REMAP or SUGGEST RETRY - status = MAYREDO\n");
1268 #endif
1269
1270 status = MAYREDO;
1271 exit = DRIVER_SENSE | SUGGEST_RETRY;
1272 break;
1273 case SUGGEST_ABORT:
1274 #ifdef DEBUG
1275 printk("SENSE SUGGEST ABORT - status = FINISHED");
1276 #endif
1277
1278 status = FINISHED;
1279 exit = DRIVER_SENSE | SUGGEST_ABORT;
1280 break;
1281 default:
1282 printk ("Internal error %s %d \n", __FILE__,
1283 __LINE__);
1284 }
1285 }
1286 else
1287 {
1288 #ifdef DEBUG
1289 printk("COMMAND COMPLETE message returned, status = FINISHED. \n");
1290 #endif
1291
1292 exit = DRIVER_OK;
1293 status = FINISHED;
1294 }
1295 break;
1296
1297 case CHECK_CONDITION:
1298 switch (check_sense(SCpnt))
1299 {
1300 case 0:
1301 update_timeout(SCpnt, oldto);
1302 status = REDO;
1303 break;
1304 case SUGGEST_REMAP:
1305 case SUGGEST_RETRY:
1306 status = MAYREDO;
1307 exit = DRIVER_SENSE | SUGGEST_RETRY;
1308 break;
1309 case SUGGEST_ABORT:
1310 status = FINISHED;
1311 exit = DRIVER_SENSE | SUGGEST_ABORT;
1312 break;
1313 case SUGGEST_SENSE:
1314 scsi_request_sense (SCpnt);
1315 status = PENDING;
1316 break;
1317 }
1318 break;
1319
1320 case CONDITION_GOOD:
1321 case INTERMEDIATE_GOOD:
1322 case INTERMEDIATE_C_GOOD:
1323 break;
1324
1325 case BUSY:
1326 update_timeout(SCpnt, oldto);
1327 status = REDO;
1328 break;
1329
1330 case RESERVATION_CONFLICT:
1331 printk("scsi%d : RESERVATION CONFLICT performing reset.\n",
1332 SCpnt->host->host_no);
1333 reset(SCpnt);
1334 return;
1335 #if 0
1336 exit = DRIVER_SOFT | SUGGEST_ABORT;
1337 status = MAYREDO;
1338 break;
1339 #endif
1340 default:
1341 printk ("Internal error %s %d \n"
1342 "status byte = %d \n", __FILE__,
1343 __LINE__, status_byte(result));
1344
1345 }
1346 break;
1347 default:
1348 panic("scsi: unsupported message byte %d received\n", msg_byte(result));
1349 }
1350 break;
1351 case DID_TIME_OUT:
1352 #ifdef DEBUG
1353 printk("Host returned DID_TIME_OUT - ");
1354 #endif
1355
1356 if (SCpnt->flags & WAS_TIMEDOUT)
1357 {
1358 #ifdef DEBUG
1359 printk("Aborting\n");
1360 #endif
1361 exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
1362 }
1363 else
1364 {
1365 #ifdef DEBUG
1366 printk ("Retrying.\n");
1367 #endif
1368 SCpnt->flags |= WAS_TIMEDOUT;
1369 SCpnt->internal_timeout &= ~IN_ABORT;
1370 status = REDO;
1371 }
1372 break;
1373 case DID_BUS_BUSY:
1374 case DID_PARITY:
1375 status = REDO;
1376 break;
1377 case DID_NO_CONNECT:
1378 #ifdef DEBUG
1379 printk("Couldn't connect.\n");
1380 #endif
1381 exit = (DRIVER_HARD | SUGGEST_ABORT);
1382 break;
1383 case DID_ERROR:
1384 status = MAYREDO;
1385 exit = (DRIVER_HARD | SUGGEST_ABORT);
1386 break;
1387 case DID_BAD_TARGET:
1388 case DID_ABORT:
1389 exit = (DRIVER_INVALID | SUGGEST_ABORT);
1390 break;
1391 case DID_RESET:
1392 if (SCpnt->flags & IS_RESETTING)
1393 {
1394 SCpnt->flags &= ~IS_RESETTING;
1395 status = REDO;
1396 break;
1397 }
1398
1399 if(msg_byte(result) == GOOD &&
1400 status_byte(result) == CHECK_CONDITION) {
1401 switch (check_sense(SCpnt)) {
1402 case 0:
1403 update_timeout(SCpnt, oldto);
1404 status = REDO;
1405 break;
1406 case SUGGEST_REMAP:
1407 case SUGGEST_RETRY:
1408 status = MAYREDO;
1409 exit = DRIVER_SENSE | SUGGEST_RETRY;
1410 break;
1411 case SUGGEST_ABORT:
1412 status = FINISHED;
1413 exit = DRIVER_SENSE | SUGGEST_ABORT;
1414 break;
1415 case SUGGEST_SENSE:
1416 scsi_request_sense (SCpnt);
1417 status = PENDING;
1418 break;
1419 }
1420 } else {
1421 status=REDO;
1422 exit = SUGGEST_RETRY;
1423 }
1424 break;
1425 default :
1426 exit = (DRIVER_ERROR | SUGGEST_DIE);
1427 }
1428
1429 switch (status)
1430 {
1431 case FINISHED:
1432 case PENDING:
1433 break;
1434 case MAYREDO:
1435
1436 #ifdef DEBUG
1437 printk("In MAYREDO, allowing %d retries, have %d\n",
1438 SCpnt->allowed, SCpnt->retries);
1439 #endif
1440
1441 if ((++SCpnt->retries) < SCpnt->allowed)
1442 {
1443 if ((SCpnt->retries >= (SCpnt->allowed >> 1))
1444 && !(jiffies < SCpnt->host->last_reset + MIN_RESET_PERIOD)
1445 && !(SCpnt->flags & WAS_RESET))
1446 {
1447 printk("scsi%d : resetting for second half of retries.\n",
1448 SCpnt->host->host_no);
1449 reset(SCpnt);
1450 break;
1451 }
1452
1453 }
1454 else
1455 {
1456 status = FINISHED;
1457 break;
1458 }
1459
1460
1461 case REDO:
1462
1463 if (SCpnt->flags & WAS_SENSE)
1464 scsi_request_sense(SCpnt);
1465 else
1466 {
1467 memcpy ((void *) SCpnt->cmnd,
1468 (void*) SCpnt->data_cmnd,
1469 sizeof(SCpnt->data_cmnd));
1470 SCpnt->request_buffer = SCpnt->buffer;
1471 SCpnt->request_bufflen = SCpnt->bufflen;
1472 SCpnt->use_sg = SCpnt->old_use_sg;
1473 SCpnt->cmd_len = SCpnt->old_cmd_len;
1474 internal_cmnd (SCpnt);
1475 }
1476 break;
1477 default:
1478 INTERNAL_ERROR;
1479 }
1480
1481
1482 if (status == FINISHED) {
1483 #ifdef DEBUG
1484 printk("Calling done function - at address %08x\n", SCpnt->done);
1485 #endif
1486 host->host_busy--;
1487
1488 if (host->block && host->host_busy == 0) {
1489 host_active = NULL;
1490
1491
1492 if (MAJOR(SCpnt->request.dev) != SCSI_DISK_MAJOR &&
1493 MAJOR(SCpnt->request.dev) != SCSI_CDROM_MAJOR) {
1494 struct Scsi_Host * next;
1495
1496 for (next = host->block; next != host; next = next->block)
1497 wake_up(&next->host_wait);
1498 }
1499
1500 }
1501
1502 wake_up(&host->host_wait);
1503 SCpnt->result = result | ((exit & 0xff) << 24);
1504 SCpnt->use_sg = SCpnt->old_use_sg;
1505 SCpnt->cmd_len = SCpnt->old_cmd_len;
1506 SCpnt->done (SCpnt);
1507 }
1508
1509 #undef FINISHED
1510 #undef REDO
1511 #undef MAYREDO
1512 #undef PENDING
1513 }
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531 int scsi_abort (Scsi_Cmnd * SCpnt, int why, int pid)
1532 {
1533 int oldto;
1534 unsigned long flags;
1535 struct Scsi_Host * host = SCpnt->host;
1536
1537 while(1)
1538 {
1539 save_flags(flags);
1540 cli();
1541
1542
1543
1544
1545
1546 if (SCpnt->request.dev == -1 || pid != SCpnt->pid) {
1547 restore_flags(flags);
1548 return 0;
1549 }
1550
1551 if (SCpnt->internal_timeout & IN_ABORT)
1552 {
1553 restore_flags(flags);
1554 while (SCpnt->internal_timeout & IN_ABORT)
1555 barrier();
1556 }
1557 else
1558 {
1559 SCpnt->internal_timeout |= IN_ABORT;
1560 oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
1561
1562 if ((SCpnt->flags & IS_RESETTING) &&
1563 SCpnt->device->soft_reset) {
1564
1565
1566 printk("Stale command on %d:%d appears to have died when"
1567 " the bus was reset\n", SCpnt->target, SCpnt->lun);
1568 }
1569
1570 restore_flags(flags);
1571 if (!host->host_busy) {
1572 SCpnt->internal_timeout &= ~IN_ABORT;
1573 update_timeout(SCpnt, oldto);
1574 return 0;
1575 }
1576 printk("scsi : aborting command due to timeout : pid %lu, scsi%d, id %d, lun %d ",
1577 SCpnt->pid, SCpnt->host->host_no, (int) SCpnt->target, (int)
1578 SCpnt->lun);
1579 print_command (SCpnt->cmnd);
1580 if (SCpnt->request.dev == -1 || pid != SCpnt->pid)
1581 return 0;
1582 SCpnt->abort_reason = why;
1583 switch(host->hostt->abort(SCpnt)) {
1584
1585
1586
1587
1588 case SCSI_ABORT_BUSY:
1589
1590 case SCSI_ABORT_SNOOZE:
1591 if(why == DID_TIME_OUT) {
1592 save_flags(flags);
1593 cli();
1594 SCpnt->internal_timeout &= ~IN_ABORT;
1595 if(SCpnt->flags & WAS_TIMEDOUT) {
1596 restore_flags(flags);
1597 return 1;
1598
1599
1600 } else {
1601 SCpnt->flags |= WAS_TIMEDOUT;
1602 oldto = SCpnt->timeout_per_command;
1603 update_timeout(SCpnt, oldto);
1604 }
1605 restore_flags(flags);
1606 }
1607 return 0;
1608 case SCSI_ABORT_PENDING:
1609 if(why != DID_TIME_OUT) {
1610 save_flags(flags);
1611 cli();
1612 update_timeout(SCpnt, oldto);
1613 restore_flags(flags);
1614 }
1615 return 0;
1616 case SCSI_ABORT_SUCCESS:
1617
1618
1619 case SCSI_ABORT_NOT_RUNNING:
1620 SCpnt->internal_timeout &= ~IN_ABORT;
1621 update_timeout(SCpnt, 0);
1622 return 0;
1623 case SCSI_ABORT_ERROR:
1624 default:
1625 SCpnt->internal_timeout &= ~IN_ABORT;
1626 return 1;
1627 }
1628 }
1629 }
1630 }
1631
1632 int scsi_reset (Scsi_Cmnd * SCpnt)
1633 {
1634 int temp, oldto;
1635 unsigned long flags;
1636 Scsi_Cmnd * SCpnt1;
1637 struct Scsi_Host * host = SCpnt->host;
1638
1639 #ifdef DEBUG
1640 printk("Danger Will Robinson! - SCSI bus for host %d is being reset.\n",host->host_no);
1641 #endif
1642 while (1) {
1643 save_flags(flags);
1644 cli();
1645 if (SCpnt->internal_timeout & IN_RESET)
1646 {
1647 restore_flags(flags);
1648 while (SCpnt->internal_timeout & IN_RESET)
1649 barrier();
1650 }
1651 else
1652 {
1653 SCpnt->internal_timeout |= IN_RESET;
1654 oldto = update_timeout(SCpnt, RESET_TIMEOUT);
1655
1656 if (host->host_busy)
1657 {
1658 restore_flags(flags);
1659 SCpnt1 = host->host_queue;
1660 while(SCpnt1) {
1661 if (SCpnt1->request.dev > 0) {
1662 #if 0
1663 if (!(SCpnt1->flags & IS_RESETTING) &&
1664 !(SCpnt1->internal_timeout & IN_ABORT))
1665 scsi_abort(SCpnt1, DID_RESET, SCpnt->pid);
1666 #endif
1667 SCpnt1->flags |= IS_RESETTING;
1668 }
1669 SCpnt1 = SCpnt1->next;
1670 }
1671
1672 host->last_reset = jiffies;
1673 temp = host->hostt->reset(SCpnt);
1674 host->last_reset = jiffies;
1675 }
1676 else
1677 {
1678 if (!host->block) host->host_busy++;
1679 restore_flags(flags);
1680 host->last_reset = jiffies;
1681 temp = host->hostt->reset(SCpnt);
1682 host->last_reset = jiffies;
1683 if (!host->block) host->host_busy--;
1684 }
1685
1686 #ifdef DEBUG
1687 printk("scsi reset function returned %d\n", temp);
1688 #endif
1689 switch(temp) {
1690 case SCSI_RESET_SUCCESS:
1691 save_flags(flags);
1692 cli();
1693 SCpnt->internal_timeout &= ~IN_RESET;
1694 update_timeout(SCpnt, oldto);
1695 restore_flags(flags);
1696 return 0;
1697 case SCSI_RESET_PENDING:
1698 return 0;
1699 case SCSI_RESET_PUNT:
1700 case SCSI_RESET_WAKEUP:
1701 SCpnt->internal_timeout &= ~IN_RESET;
1702 scsi_request_sense (SCpnt);
1703 return 0;
1704 case SCSI_RESET_SNOOZE:
1705
1706
1707
1708
1709 save_flags(flags);
1710 cli();
1711 SCpnt->internal_timeout &= ~IN_RESET;
1712 update_timeout(SCpnt, 0);
1713 restore_flags(flags);
1714
1715 case SCSI_RESET_ERROR:
1716 default:
1717 return 1;
1718 }
1719
1720 return temp;
1721 }
1722 }
1723 }
1724
1725
1726 static void scsi_main_timeout(void)
1727 {
1728
1729
1730
1731
1732 int timed_out, pid;
1733 unsigned long flags;
1734 struct Scsi_Host * host;
1735 Scsi_Cmnd * SCpnt = NULL;
1736
1737 do {
1738 save_flags(flags);
1739 cli();
1740
1741 update_timeout(NULL, 0);
1742
1743
1744
1745
1746
1747 timed_out = 0;
1748 for(host = scsi_hostlist; host; host = host->next) {
1749 for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
1750 if (SCpnt->timeout == -1)
1751 {
1752 SCpnt->timeout = 0;
1753 pid = SCpnt->pid;
1754 restore_flags(flags);
1755 scsi_times_out(SCpnt, pid);
1756 ++timed_out;
1757 save_flags(flags);
1758 cli();
1759 }
1760 }
1761 } while (timed_out);
1762 restore_flags(flags);
1763 }
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773 static int update_timeout(Scsi_Cmnd * SCset, int timeout)
1774 {
1775 unsigned int least, used;
1776 unsigned int oldto;
1777 unsigned long flags;
1778 struct Scsi_Host * host;
1779 Scsi_Cmnd * SCpnt = NULL;
1780
1781 save_flags(flags);
1782 cli();
1783
1784
1785
1786
1787
1788 used = (time_start) ? (jiffies - time_start) : 0;
1789
1790
1791
1792
1793
1794
1795
1796 oldto = 0;
1797
1798 if(SCset){
1799 oldto = SCset->timeout - used;
1800 SCset->timeout = timeout + used;
1801 }
1802
1803 least = 0xffffffff;
1804
1805 for(host = scsi_hostlist; host; host = host->next)
1806 for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
1807 if (SCpnt->timeout > 0) {
1808 SCpnt->timeout -= used;
1809 if(SCpnt->timeout <= 0) SCpnt->timeout = -1;
1810 if(SCpnt->timeout > 0 && SCpnt->timeout < least)
1811 least = SCpnt->timeout;
1812 }
1813
1814
1815
1816
1817
1818
1819 if (least != 0xffffffff)
1820 {
1821 time_start = jiffies;
1822 timer_table[SCSI_TIMER].expires = (time_elapsed = least) + jiffies;
1823 timer_active |= 1 << SCSI_TIMER;
1824 }
1825 else
1826 {
1827 timer_table[SCSI_TIMER].expires = time_start = time_elapsed = 0;
1828 timer_active &= ~(1 << SCSI_TIMER);
1829 }
1830 restore_flags(flags);
1831 return oldto;
1832 }
1833
1834
1835 static unsigned char * dma_malloc_freelist = NULL;
1836 static int scsi_need_isa_bounce_buffers;
1837 static unsigned int dma_sectors = 0;
1838 unsigned int dma_free_sectors = 0;
1839 unsigned int need_isa_buffer = 0;
1840 static unsigned char ** dma_malloc_pages = NULL;
1841 #define MALLOC_PAGEBITS 12
1842
1843 static int scsi_register_host(Scsi_Host_Template *);
1844 static void scsi_unregister_host(Scsi_Host_Template *);
1845
1846 void *scsi_malloc(unsigned int len)
1847 {
1848 unsigned int nbits, mask;
1849 unsigned long flags;
1850 int i, j;
1851 if((len & 0x1ff) || len > (1<<MALLOC_PAGEBITS))
1852 return NULL;
1853
1854 save_flags(flags);
1855 cli();
1856 nbits = len >> 9;
1857 mask = (1 << nbits) - 1;
1858
1859 for(i=0;i < (dma_sectors >> (MALLOC_PAGEBITS - 9)); i++)
1860 for(j=0; j<=(sizeof(*dma_malloc_freelist) * 8) - nbits; j++){
1861 if ((dma_malloc_freelist[i] & (mask << j)) == 0){
1862 dma_malloc_freelist[i] |= (mask << j);
1863 restore_flags(flags);
1864 dma_free_sectors -= nbits;
1865 #ifdef DEBUG
1866 printk("SMalloc: %d %x ",len, dma_malloc_pages[i] + (j << 9));
1867 #endif
1868 return (void *) ((unsigned long) dma_malloc_pages[i] + (j << 9));
1869 }
1870 }
1871 restore_flags(flags);
1872 return NULL;
1873 }
1874
1875 int scsi_free(void *obj, unsigned int len)
1876 {
1877 int page, sector, nbits, mask;
1878 long offset;
1879 unsigned long flags;
1880
1881 #ifdef DEBUG
1882 printk("Sfree %x %d\n",obj, len);
1883 #endif
1884
1885 offset = -1;
1886 for (page = 0; page < (dma_sectors >> 3); page++)
1887 if ((unsigned long) obj >= (unsigned long) dma_malloc_pages[page] &&
1888 (unsigned long) obj < (unsigned long) dma_malloc_pages[page] + (1 << MALLOC_PAGEBITS))
1889 {
1890 offset = ((unsigned long) obj) - ((unsigned long)dma_malloc_pages[page]);
1891 break;
1892 }
1893
1894 if (page == (dma_sectors >> 3)) panic("Bad offset");
1895 sector = offset >> 9;
1896 if(sector >= dma_sectors) panic ("Bad page");
1897
1898 sector = (offset >> 9) & (sizeof(*dma_malloc_freelist) * 8 - 1);
1899 nbits = len >> 9;
1900 mask = (1 << nbits) - 1;
1901
1902 if ((mask << sector) > 0xffff) panic ("Bad memory alignment");
1903
1904 save_flags(flags);
1905 cli();
1906 if(dma_malloc_freelist[page] & (mask << sector) != (mask<<sector))
1907 panic("Trying to free unused memory");
1908
1909 dma_free_sectors += nbits;
1910 dma_malloc_freelist[page] &= ~(mask << sector);
1911 restore_flags(flags);
1912 return 0;
1913 }
1914
1915
1916
1917
1918
1919
1920 static unsigned long scsi_init_memory_start = 0;
1921 static unsigned long scsi_memory_lower_value = 0;
1922 static unsigned long scsi_memory_upper_value = 0;
1923 int scsi_loadable_module_flag;
1924
1925 void * scsi_init_malloc(unsigned int size, int priority)
1926 {
1927 unsigned long retval;
1928
1929
1930 #if defined(USE_STATIC_SCSI_MEMORY)
1931 if(scsi_loadable_module_flag && !(priority & GFP_DMA))
1932 #else
1933 if(scsi_loadable_module_flag)
1934 #endif
1935 retval = (unsigned long) kmalloc(size, priority);
1936 else {
1937
1938
1939
1940
1941 size = (size + 15) & ~15;
1942
1943 if(scsi_loadable_module_flag &&
1944 (scsi_init_memory_start + size) > scsi_memory_upper_value) {
1945 retval = 0;
1946 printk("scsi_init_malloc: no more statically allocated memory.\n");
1947 }
1948 else {
1949 retval = scsi_init_memory_start;
1950 scsi_init_memory_start += size;
1951 }
1952 }
1953 memset((void *) retval, 0, size);
1954 return (void *) retval;
1955 }
1956
1957
1958 void scsi_init_free(char * ptr, unsigned int size)
1959 {
1960
1961 if((unsigned long) ptr >= scsi_init_memory_start ||
1962 (unsigned long) ptr < scsi_memory_lower_value) kfree(ptr);
1963 else {
1964 size = (size + 15) & ~15;
1965
1966 if(((unsigned long) ptr) + size == scsi_init_memory_start)
1967 scsi_init_memory_start = (unsigned long) ptr;
1968 }
1969 }
1970
1971
1972
1973
1974
1975
1976
1977 unsigned long scsi_dev_init (unsigned long memory_start,unsigned long memory_end)
1978 {
1979 struct Scsi_Host * host = NULL;
1980 Scsi_Device * SDpnt;
1981 struct Scsi_Host * shpnt;
1982 struct Scsi_Device_Template * sdtpnt;
1983 Scsi_Cmnd * SCpnt;
1984 int i;
1985 #ifdef FOO_ON_YOU
1986 return;
1987 #endif
1988
1989
1990 scsi_loadable_module_flag = 0;
1991
1992 scsi_init_memory_start = (memory_start + 15) & ~ 15;
1993 scsi_memory_lower_value = scsi_init_memory_start;
1994
1995 timer_table[SCSI_TIMER].fn = scsi_main_timeout;
1996 timer_table[SCSI_TIMER].expires = 0;
1997
1998
1999 scsi_init();
2000
2001 scsi_devices = (Scsi_Device *) NULL;
2002
2003 for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
2004 scan_scsis(shpnt);
2005
2006 printk("scsi : detected ");
2007 for (sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2008 if (sdtpnt->dev_noticed && sdtpnt->name)
2009 printk("%d SCSI %s%s ", sdtpnt->dev_noticed, sdtpnt->name,
2010 (sdtpnt->dev_noticed != 1) ? "s" : "");
2011 printk("total.\n");
2012
2013 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2014 if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
2015
2016 for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
2017 int j;
2018 SDpnt->scsi_request_fn = NULL;
2019 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2020 if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
2021
2022 if(SDpnt->attached){
2023 for(j=0;j<SDpnt->host->cmd_per_lun;j++){
2024 SCpnt = (Scsi_Cmnd *) scsi_init_malloc(sizeof(Scsi_Cmnd), GFP_ATOMIC);
2025 SCpnt->host = SDpnt->host;
2026 SCpnt->device = SDpnt;
2027 SCpnt->target = SDpnt->id;
2028 SCpnt->lun = SDpnt->lun;
2029 SCpnt->request.dev = -1;
2030 SCpnt->use_sg = 0;
2031 SCpnt->old_use_sg = 0;
2032 SCpnt->old_cmd_len = 0;
2033 SCpnt->timeout = 0;
2034 SCpnt->underflow = 0;
2035 SCpnt->transfersize = 0;
2036 SCpnt->host_scribble = NULL;
2037 host = SDpnt->host;
2038 if(host->host_queue)
2039 host->host_queue->prev = SCpnt;
2040 SCpnt->next = host->host_queue;
2041 SCpnt->prev = NULL;
2042 host->host_queue = SCpnt;
2043 }
2044 }
2045 }
2046
2047 if (scsi_devicelist)
2048 dma_sectors = 16;
2049
2050 if (memory_end-1 > ISA_DMA_THRESHOLD)
2051 scsi_need_isa_bounce_buffers = 1;
2052 else
2053 scsi_need_isa_bounce_buffers = 0;
2054
2055 for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
2056 host = SDpnt->host;
2057
2058 if(SDpnt->type != TYPE_TAPE)
2059 dma_sectors += ((host->sg_tablesize *
2060 sizeof(struct scatterlist) + 511) >> 9) *
2061 host->cmd_per_lun;
2062
2063 if(host->unchecked_isa_dma &&
2064 memory_end - 1 > ISA_DMA_THRESHOLD &&
2065 SDpnt->type != TYPE_TAPE) {
2066 dma_sectors += (PAGE_SIZE >> 9) * host->sg_tablesize *
2067 host->cmd_per_lun;
2068 need_isa_buffer++;
2069 }
2070 }
2071
2072 dma_sectors = (dma_sectors + 15) & 0xfff0;
2073 dma_free_sectors = dma_sectors;
2074
2075 dma_malloc_freelist = (unsigned char *)
2076 scsi_init_malloc(dma_sectors >> 3, GFP_ATOMIC);
2077 memset(dma_malloc_freelist, 0, dma_sectors >> 3);
2078
2079 dma_malloc_pages = (unsigned char **)
2080 scsi_init_malloc(dma_sectors >> 1, GFP_ATOMIC);
2081 memset(dma_malloc_pages, 0, dma_sectors >> 1);
2082
2083 for(i=0; i< dma_sectors >> 3; i++)
2084 dma_malloc_pages[i] = (unsigned char *)
2085 scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
2086
2087
2088
2089
2090 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2091 if(sdtpnt->finish && sdtpnt->nr_dev)
2092 (*sdtpnt->finish)();
2093
2094 scsi_loadable_module_flag = 1;
2095
2096
2097
2098
2099
2100 #if defined(USE_STATIC_SCSI_MEMORY)
2101 scsi_memory_upper_value = scsi_init_memory_start + 256 * 1024;
2102 printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
2103 (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
2104 (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
2105 (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
2106 return scsi_memory_upper_value;
2107 #else
2108 return scsi_init_memory_start;
2109 #endif
2110 }
2111
2112 static void print_inquiry(unsigned char *data)
2113 {
2114 int i;
2115
2116 printk(" Vendor: ");
2117 for (i = 8; i < 16; i++)
2118 {
2119 if (data[i] >= 0x20 && i < data[4] + 5)
2120 printk("%c", data[i]);
2121 else
2122 printk(" ");
2123 }
2124
2125 printk(" Model: ");
2126 for (i = 16; i < 32; i++)
2127 {
2128 if (data[i] >= 0x20 && i < data[4] + 5)
2129 printk("%c", data[i]);
2130 else
2131 printk(" ");
2132 }
2133
2134 printk(" Rev: ");
2135 for (i = 32; i < 36; i++)
2136 {
2137 if (data[i] >= 0x20 && i < data[4] + 5)
2138 printk("%c", data[i]);
2139 else
2140 printk(" ");
2141 }
2142
2143 printk("\n");
2144
2145 i = data[0] & 0x1f;
2146
2147 printk(" Type: %s ",
2148 i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] : "Unknown " );
2149 printk(" ANSI SCSI revision: %02x", data[2] & 0x07);
2150 if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
2151 printk(" CCS\n");
2152 else
2153 printk("\n");
2154 }
2155
2156
2157
2158
2159
2160 static int scsi_register_host(Scsi_Host_Template * tpnt)
2161 {
2162 int pcount;
2163 struct Scsi_Host * shpnt;
2164 struct Scsi_Host * host = NULL;
2165 unsigned long flags;
2166 Scsi_Device * SDpnt;
2167 Scsi_Cmnd * SCpnt;
2168 struct Scsi_Device_Template * sdtpnt;
2169 int j, i;
2170 const char * name;
2171
2172 if (tpnt->next || !tpnt->detect) return 1;
2173
2174 pcount = next_scsi_host;
2175 if ((tpnt->present = tpnt->detect(tpnt)))
2176 {
2177 if(pcount == next_scsi_host) {
2178 if(tpnt->present > 1) {
2179 printk("Failure to register low-level scsi driver");
2180 scsi_unregister_host(tpnt);
2181 return 1;
2182 }
2183
2184
2185 scsi_register(tpnt,0);
2186 }
2187 tpnt->next = scsi_hosts;
2188 scsi_hosts = tpnt;
2189
2190 for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2191 if(shpnt->hostt == tpnt)
2192 {
2193 if(tpnt->info)
2194 name = tpnt->info(shpnt);
2195 else
2196 name = tpnt->name;
2197 printk ("scsi%d : %s\n",
2198 shpnt->host_no, name);
2199 }
2200
2201 printk ("scsi : %d host%s.\n", next_scsi_host,
2202 (next_scsi_host == 1) ? "" : "s");
2203
2204 scsi_make_blocked_list();
2205
2206
2207
2208
2209 for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2210 if(shpnt->hostt == tpnt) scan_scsis(shpnt);
2211
2212 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2213 if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
2214
2215
2216
2217 for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
2218 if(SDpnt->host->hostt == tpnt)
2219 {
2220 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2221 if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
2222 if(SDpnt->attached){
2223 for(j=0;j<SDpnt->host->cmd_per_lun;j++){
2224 SCpnt = (Scsi_Cmnd *) scsi_init_malloc(sizeof(Scsi_Cmnd), GFP_ATOMIC);
2225 SCpnt->host = SDpnt->host;
2226 SCpnt->device = SDpnt;
2227 SCpnt->target = SDpnt->id;
2228 SCpnt->lun = SDpnt->lun;
2229 SCpnt->request.dev = -1;
2230 SCpnt->request.sem = NULL;
2231 SCpnt->use_sg = 0;
2232 SCpnt->old_use_sg = 0;
2233 SCpnt->underflow = 0;
2234 SCpnt->timeout = 0;
2235 SCpnt->transfersize = 0;
2236 SCpnt->host_scribble = NULL;
2237 host = SDpnt->host;
2238 SCpnt->next = host->host_queue;
2239 SCpnt->prev = NULL;
2240 host->host_queue = SCpnt;
2241 if(host->host_queue)
2242 host->host_queue->prev = SCpnt;
2243 }
2244 }
2245 }
2246
2247 {
2248 unsigned char * new_dma_malloc_freelist = NULL;
2249 unsigned int new_dma_sectors = 0;
2250 unsigned int new_need_isa_buffer = 0;
2251 unsigned char ** new_dma_malloc_pages = NULL;
2252
2253 if (scsi_devicelist)
2254 new_dma_sectors = 16;
2255
2256 for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
2257 host = SDpnt->host;
2258
2259 if(SDpnt->type != TYPE_TAPE)
2260 new_dma_sectors += ((host->sg_tablesize *
2261 sizeof(struct scatterlist) + 511) >> 9) *
2262 host->cmd_per_lun;
2263
2264 if(host->unchecked_isa_dma &&
2265 scsi_need_isa_bounce_buffers &&
2266 SDpnt->type != TYPE_TAPE) {
2267 new_dma_sectors += (PAGE_SIZE >> 9) * host->sg_tablesize *
2268 host->cmd_per_lun;
2269 new_need_isa_buffer++;
2270 }
2271 }
2272
2273 new_dma_sectors = (new_dma_sectors + 15) & 0xfff0;
2274
2275 new_dma_malloc_freelist = (unsigned char *)
2276 scsi_init_malloc(new_dma_sectors >> 3, GFP_ATOMIC);
2277 memset(new_dma_malloc_freelist, 0, new_dma_sectors >> 3);
2278
2279 new_dma_malloc_pages = (unsigned char **)
2280 scsi_init_malloc(new_dma_sectors >> 1, GFP_ATOMIC);
2281 memset(new_dma_malloc_pages, 0, new_dma_sectors >> 1);
2282
2283 for(i=dma_sectors >> 3; i< new_dma_sectors >> 3; i++)
2284 new_dma_malloc_pages[i] = (unsigned char *)
2285 scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
2286
2287
2288
2289
2290 save_flags(flags);
2291 cli();
2292 memcpy(new_dma_malloc_freelist, dma_malloc_freelist, dma_sectors >> 3);
2293 scsi_init_free(dma_malloc_freelist, dma_sectors>>3);
2294 dma_malloc_freelist = new_dma_malloc_freelist;
2295
2296 memcpy(new_dma_malloc_pages, dma_malloc_pages, dma_sectors >> 1);
2297 scsi_init_free((char *) dma_malloc_pages, dma_sectors>>1);
2298
2299 dma_free_sectors += new_dma_sectors - dma_sectors;
2300 dma_malloc_pages = new_dma_malloc_pages;
2301 dma_sectors = new_dma_sectors;
2302 need_isa_buffer = new_need_isa_buffer;
2303 restore_flags(flags);
2304
2305
2306 }
2307
2308 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2309 if(sdtpnt->finish && sdtpnt->nr_dev)
2310 (*sdtpnt->finish)();
2311 }
2312
2313 #if defined(USE_STATIC_SCSI_MEMORY)
2314 printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
2315 (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
2316 (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
2317 (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
2318 #endif
2319
2320 return 0;
2321 }
2322
2323
2324
2325
2326
2327 static void scsi_unregister_host(Scsi_Host_Template * tpnt)
2328 {
2329 Scsi_Host_Template * SHT, *SHTp;
2330 Scsi_Device *sdpnt, * sdppnt, * sdpnt1;
2331 Scsi_Cmnd * SCpnt;
2332 unsigned long flags;
2333 struct Scsi_Device_Template * sdtpnt;
2334 struct Scsi_Host * shpnt, *sh1;
2335 int pcount;
2336
2337
2338
2339
2340 for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
2341 if(sdpnt->host->hostt == tpnt && sdpnt->host->hostt->usage_count
2342 && *sdpnt->host->hostt->usage_count) return;
2343
2344 for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
2345 {
2346 if (shpnt->hostt != tpnt) continue;
2347 for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
2348 {
2349 save_flags(flags);
2350 cli();
2351 if(SCpnt->request.dev != -1) {
2352 restore_flags(flags);
2353 for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
2354 if(SCpnt->request.dev == 0xffe0) SCpnt->request.dev = -1;
2355 printk("Device busy???\n");
2356 return;
2357 }
2358 SCpnt->request.dev = 0xffe0;
2359 restore_flags(flags);
2360 }
2361 }
2362
2363
2364 for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
2365 if(sdpnt->host->hostt == tpnt)
2366 {
2367 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2368 if(sdtpnt->detach) (*sdtpnt->detach)(sdpnt);
2369
2370 if (sdpnt->attached) {
2371 printk("Attached usage count = %d\n", sdpnt->attached);
2372 return;
2373 }
2374 }
2375
2376
2377
2378 for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
2379 if(sdpnt->host->hostt == tpnt)
2380 while (sdpnt->host->host_queue) {
2381 SCpnt = sdpnt->host->host_queue->next;
2382 scsi_init_free((char *) sdpnt->host->host_queue, sizeof(Scsi_Cmnd));
2383 sdpnt->host->host_queue = SCpnt;
2384 if (SCpnt) SCpnt->prev = NULL;
2385 }
2386
2387
2388
2389 sdppnt = NULL;
2390 for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt1)
2391 {
2392 sdpnt1 = sdpnt->next;
2393 if (sdpnt->host->hostt == tpnt) {
2394 if (sdppnt)
2395 sdppnt->next = sdpnt->next;
2396 else
2397 scsi_devices = sdpnt->next;
2398 scsi_init_free((char *) sdpnt, sizeof (Scsi_Device));
2399 } else
2400 sdppnt = sdpnt;
2401 }
2402
2403
2404
2405
2406 shpnt = scsi_hostlist;
2407 while(shpnt) {
2408 sh1 = shpnt->next;
2409 if(shpnt->hostt == tpnt) {
2410 if(shpnt->loaded_as_module) {
2411 pcount = next_scsi_host;
2412 if(tpnt->release)
2413 (*tpnt->release)(shpnt);
2414 else {
2415
2416
2417 if (shpnt->irq) free_irq(shpnt->irq);
2418 if (shpnt->dma_channel != 0xff) free_dma(shpnt->dma_channel);
2419 if (shpnt->io_port && shpnt->n_io_port)
2420 release_region(shpnt->io_port, shpnt->n_io_port);
2421 }
2422 if(pcount == next_scsi_host) scsi_unregister(shpnt);
2423 tpnt->present--;
2424 }
2425 }
2426 shpnt = sh1;
2427 }
2428
2429 printk ("scsi : %d host%s.\n", next_scsi_host,
2430 (next_scsi_host == 1) ? "" : "s");
2431
2432 #if defined(USE_STATIC_SCSI_MEMORY)
2433 printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
2434 (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
2435 (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
2436 (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
2437 #endif
2438
2439 scsi_make_blocked_list();
2440
2441
2442
2443 if (tpnt->present) return;
2444
2445
2446
2447 for(SHTp=NULL, SHT=scsi_hosts; SHT; SHTp=SHT, SHT=SHT->next)
2448 if(SHT == tpnt) {
2449 if(SHTp)
2450 SHTp->next = SHT->next;
2451 else
2452 scsi_hosts = SHT->next;
2453 SHT->next = NULL;
2454 break;
2455 }
2456 }
2457
2458 int scsi_register_module(int module_type, void * ptr)
2459 {
2460 switch(module_type){
2461 case MODULE_SCSI_HA:
2462 return scsi_register_host((Scsi_Host_Template *) ptr);
2463
2464
2465
2466 case MODULE_SCSI_CONST:
2467
2468
2469
2470 case MODULE_SCSI_IOCTL:
2471
2472
2473 case MODULE_SCSI_DEV:
2474 default:
2475 return 1;
2476 }
2477 }
2478
2479 void scsi_unregister_module(int module_type, void * ptr)
2480 {
2481 switch(module_type) {
2482 case MODULE_SCSI_HA:
2483 scsi_unregister_host((Scsi_Host_Template *) ptr);
2484 break;
2485
2486 case MODULE_SCSI_CONST:
2487 case MODULE_SCSI_IOCTL:
2488 case MODULE_SCSI_DEV:
2489 default:
2490 }
2491 return;
2492 }
2493
2494 #ifdef DEBUG_TIMEOUT
2495 static void
2496 scsi_dump_status(void)
2497 {
2498 int i;
2499 struct Scsi_Host * shpnt;
2500 Scsi_Cmnd * SCpnt;
2501 printk("Dump of scsi parameters:\n");
2502 i = 0;
2503 for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
2504 for(SCpnt=shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
2505 {
2506
2507 printk("(%d) %d:%d:%d (%4.4x %ld %ld %ld %ld) (%d %d %x) (%d %d %d) %x %x %x\n",
2508 i++, SCpnt->host->host_no,
2509 SCpnt->target,
2510 SCpnt->lun,
2511 SCpnt->request.dev,
2512 SCpnt->request.sector,
2513 SCpnt->request.nr_sectors,
2514 SCpnt->request.current_nr_sectors,
2515 SCpnt->use_sg,
2516 SCpnt->retries,
2517 SCpnt->allowed,
2518 SCpnt->flags,
2519 SCpnt->timeout_per_command,
2520 SCpnt->timeout,
2521 SCpnt->internal_timeout,
2522 SCpnt->cmnd[0],
2523 SCpnt->sense_buffer[2],
2524 SCpnt->result);
2525 }
2526 printk("wait_for_request = %p\n", wait_for_request);
2527
2528 printk("Dump of pending block device requests\n");
2529 for(i=0; i<MAX_BLKDEV; i++)
2530 if(blk_dev[i].current_request)
2531 {
2532 struct request * req;
2533 printk("%d: ", i);
2534 req = blk_dev[i].current_request;
2535 while(req) {
2536 printk("(%x %d %ld %ld %ld) ",
2537 req->dev,
2538 req->cmd,
2539 req->sector,
2540 req->nr_sectors,
2541 req->current_nr_sectors);
2542 req = req->next;
2543 }
2544 printk("\n");
2545 }
2546 }
2547 #endif
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564