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