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