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