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