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 (2*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 (1*HZ)
205 #define RESET_TIMEOUT (5*HZ)
206 #define ABORT_TIMEOUT (5*HZ)
207 #endif
208
209 #define MIN_RESET_DELAY (3*HZ)
210
211
212 #define MIN_RESET_PERIOD (15*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 (host_byte(SCpnt->result) != DID_OK) {
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 #define IN_RESET3 8
823
824
825
826
827
828
829
830 static void scsi_times_out (Scsi_Cmnd * SCpnt)
831 {
832
833 switch (SCpnt->internal_timeout & (IN_ABORT | IN_RESET | IN_RESET2 | IN_RESET3))
834 {
835 case NORMAL_TIMEOUT:
836 {
837 #ifdef DEBUG_TIMEOUT
838 scsi_dump_status();
839 #endif
840 }
841
842 if (!scsi_abort (SCpnt, DID_TIME_OUT))
843 return;
844 case IN_ABORT:
845 printk("SCSI host %d abort (pid %ld) timed out - resetting\n",
846 SCpnt->host->host_no, SCpnt->pid);
847 if (!scsi_reset (SCpnt, SCSI_RESET_ASYNCHRONOUS))
848 return;
849 case IN_RESET:
850 case (IN_ABORT | IN_RESET):
851
852
853
854
855 printk("SCSI host %d channel %d reset (pid %ld) timed out - "
856 "trying harder\n",
857 SCpnt->host->host_no, SCpnt->channel, SCpnt->pid);
858 SCpnt->internal_timeout &= ~IN_RESET;
859 SCpnt->internal_timeout |= IN_RESET2;
860 scsi_reset (SCpnt,
861 SCSI_RESET_ASYNCHRONOUS | SCSI_RESET_SUGGEST_BUS_RESET);
862 return;
863
864 case (IN_ABORT | IN_RESET | IN_RESET2):
865
866
867
868
869 printk("SCSI host %d reset (pid %ld) timed out - trying to shake it loose\n",
870 SCpnt->host->host_no, SCpnt->pid);
871 SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2);
872 SCpnt->internal_timeout |= IN_RESET3;
873 scsi_reset (SCpnt,
874 SCSI_RESET_ASYNCHRONOUS | SCSI_RESET_SUGGEST_HOST_RESET);
875 return;
876
877 default:
878 printk("SCSI host %d reset (pid %ld) timed out again -\n",
879 SCpnt->host->host_no, SCpnt->pid);
880 printk("probably an unrecoverable SCSI bus or device hang.\n");
881 return;
882
883 }
884
885 }
886
887
888
889
890
891
892
893
894
895 Scsi_Cmnd * request_queueable (struct request * req, Scsi_Device * device)
896 {
897 Scsi_Cmnd * SCpnt = NULL;
898 int tablesize;
899 Scsi_Cmnd * found = NULL;
900 struct buffer_head * bh, *bhp;
901
902 if (!device)
903 panic ("No device passed to request_queueable().\n");
904
905 if (req && req->rq_status == RQ_INACTIVE)
906 panic("Inactive in request_queueable");
907
908
909
910
911
912
913
914 if (!device->single_lun) {
915 SCpnt = device->device_queue;
916 while(SCpnt){
917 if(SCpnt->request.rq_status == RQ_INACTIVE) break;
918 SCpnt = SCpnt->device_next;
919 }
920 } else {
921 SCpnt = device->host->host_queue;
922 while(SCpnt){
923 if(SCpnt->channel == device->channel
924 && SCpnt->target == device->id) {
925 if (SCpnt->lun == device->lun) {
926 if(found == NULL
927 && SCpnt->request.rq_status == RQ_INACTIVE)
928 {
929 found=SCpnt;
930 }
931 }
932 if(SCpnt->request.rq_status != RQ_INACTIVE) {
933
934
935
936
937
938 return NULL;
939 }
940 }
941 SCpnt = SCpnt->next;
942 }
943 SCpnt = found;
944 }
945
946 if (!SCpnt) return NULL;
947
948 if (SCSI_BLOCK(device->host)) return NULL;
949
950 if (req) {
951 memcpy(&SCpnt->request, req, sizeof(struct request));
952 tablesize = device->host->sg_tablesize;
953 bhp = bh = req->bh;
954 if(!tablesize) bh = NULL;
955
956
957
958
959 while(req->nr_sectors && bh){
960 bhp = bhp->b_reqnext;
961 if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
962 req->nr_sectors -= bh->b_size >> 9;
963 req->sector += bh->b_size >> 9;
964 if(!tablesize) break;
965 bh = bhp;
966 }
967 if(req->nr_sectors && bh && bh->b_reqnext){
968 SCpnt->request.bhtail = bh;
969 req->bh = bh->b_reqnext;
970 bh->b_reqnext = NULL;
971 bh = req->bh;
972
973
974 SCpnt->request.nr_sectors -= req->nr_sectors;
975 req->current_nr_sectors = bh->b_size >> 9;
976 req->buffer = bh->b_data;
977 SCpnt->request.sem = NULL;
978 } else {
979 req->rq_status = RQ_INACTIVE;
980 wake_up(&wait_for_request);
981 }
982 } else {
983 SCpnt->request.rq_status = RQ_SCSI_BUSY;
984 SCpnt->request.sem = NULL;
985
986 }
987
988 SCpnt->use_sg = 0;
989 SCpnt->old_use_sg = 0;
990 SCpnt->transfersize = 0;
991 SCpnt->underflow = 0;
992 SCpnt->cmd_len = 0;
993
994
995
996
997 SCpnt->channel = device->channel;
998 SCpnt->lun = device->lun;
999 SCpnt->target = device->id;
1000
1001 return SCpnt;
1002 }
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014 Scsi_Cmnd * allocate_device (struct request ** reqp, Scsi_Device * device,
1015 int wait)
1016 {
1017 kdev_t dev;
1018 struct request * req = NULL;
1019 int tablesize;
1020 unsigned int flags;
1021 struct buffer_head * bh, *bhp;
1022 struct Scsi_Host * host;
1023 Scsi_Cmnd * SCpnt = NULL;
1024 Scsi_Cmnd * SCwait = NULL;
1025 Scsi_Cmnd * found = NULL;
1026
1027 if (!device)
1028 panic ("No device passed to allocate_device().\n");
1029
1030 if (reqp) req = *reqp;
1031
1032
1033 if (req) {
1034 if(req->rq_status == RQ_INACTIVE) return NULL;
1035 dev = req->rq_dev;
1036 } else
1037 dev = 0;
1038
1039 host = device->host;
1040
1041 if (intr_count && SCSI_BLOCK(host)) return NULL;
1042
1043 while (1==1){
1044 if (!device->single_lun) {
1045 SCpnt = device->device_queue;
1046 while(SCpnt){
1047 SCwait = SCpnt;
1048 if(SCpnt->request.rq_status == RQ_INACTIVE) break;
1049 SCpnt = SCpnt->device_next;
1050 }
1051 } else {
1052 SCpnt = device->host->host_queue;
1053 while(SCpnt){
1054 if(SCpnt->channel == device->channel
1055 && SCpnt->target == device->id) {
1056 if (SCpnt->lun == device->lun) {
1057 SCwait = SCpnt;
1058 if(found == NULL
1059 && SCpnt->request.rq_status == RQ_INACTIVE)
1060 {
1061 found=SCpnt;
1062 }
1063 }
1064 if(SCpnt->request.rq_status != RQ_INACTIVE) {
1065
1066
1067
1068
1069
1070 found = NULL;
1071 break;
1072 }
1073 }
1074 SCpnt = SCpnt->next;
1075 }
1076 SCpnt = found;
1077 }
1078
1079 save_flags(flags);
1080 cli();
1081
1082
1083 if (req && (req->rq_status == RQ_INACTIVE || req->rq_dev != dev)) {
1084 restore_flags(flags);
1085 return NULL;
1086 }
1087 if (!SCpnt || SCpnt->request.rq_status != RQ_INACTIVE)
1088 {
1089 #if 1
1090 if (wait && SCwait && SCwait->request.rq_status != RQ_INACTIVE){
1091 sleep_on(&device->device_wait);
1092 restore_flags(flags);
1093 } else {
1094 restore_flags(flags);
1095 if (!wait) return NULL;
1096 if (!SCwait) {
1097 printk("Attempt to allocate device channel %d,"
1098 " target %d, lun %d\n", device->channel,
1099 device->id, device->lun);
1100 panic("No device found in allocate_device\n");
1101 }
1102 }
1103 #else
1104 restore_flags(flags);
1105 if(!wait) return NULL;
1106 if (!SCwait) {
1107 printk("Attempt to allocate device channel %d, target"
1108 " %d, lun %d\n", device->channel, device->id,
1109 device->lun);
1110 panic("No device found in allocate_device\n");
1111 }
1112 SCSI_SLEEP(&device->device_wait,
1113 (SCwait->request.rq_status != RQ_INACTIVE));
1114 #endif
1115 } else {
1116 if (req) {
1117 memcpy(&SCpnt->request, req, sizeof(struct request));
1118 tablesize = device->host->sg_tablesize;
1119 bhp = bh = req->bh;
1120 if(!tablesize) bh = NULL;
1121
1122
1123
1124
1125 while(req->nr_sectors && bh){
1126 bhp = bhp->b_reqnext;
1127 if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
1128 req->nr_sectors -= bh->b_size >> 9;
1129 req->sector += bh->b_size >> 9;
1130 if(!tablesize) break;
1131 bh = bhp;
1132 }
1133 if(req->nr_sectors && bh && bh->b_reqnext){
1134 SCpnt->request.bhtail = bh;
1135 req->bh = bh->b_reqnext;
1136 bh->b_reqnext = NULL;
1137 bh = req->bh;
1138
1139 SCpnt->request.nr_sectors -= req->nr_sectors;
1140 req->current_nr_sectors = bh->b_size >> 9;
1141 req->buffer = bh->b_data;
1142 SCpnt->request.sem = NULL;
1143 }
1144 else
1145 {
1146 req->rq_status = RQ_INACTIVE;
1147 *reqp = req->next;
1148 wake_up(&wait_for_request);
1149 }
1150 } else {
1151 SCpnt->request.rq_status = RQ_SCSI_BUSY;
1152 SCpnt->request.sem = NULL;
1153
1154 }
1155 restore_flags(flags);
1156 break;
1157 }
1158 }
1159
1160 SCpnt->use_sg = 0;
1161 SCpnt->old_use_sg = 0;
1162 SCpnt->transfersize = 0;
1163 SCpnt->cmd_len = 0;
1164
1165 SCpnt->underflow = 0;
1166
1167
1168
1169
1170 SCpnt->channel = device->channel;
1171 SCpnt->lun = device->lun;
1172 SCpnt->target = device->id;
1173
1174 return SCpnt;
1175 }
1176
1177
1178
1179
1180
1181 inline void internal_cmnd (Scsi_Cmnd * SCpnt)
1182 {
1183 int temp;
1184 struct Scsi_Host * host;
1185 unsigned int flags;
1186 #ifdef DEBUG_DELAY
1187 int clock;
1188 #endif
1189
1190 host = SCpnt->host;
1191
1192
1193
1194
1195
1196 save_flags(flags);
1197 cli();
1198
1199 if (++serial_number == 0) serial_number = 1;
1200 SCpnt->serial_number = serial_number;
1201 sti();
1202 temp = host->last_reset + MIN_RESET_DELAY;
1203 while (jiffies < temp);
1204 restore_flags(flags);
1205
1206 update_timeout(SCpnt, SCpnt->timeout_per_command);
1207
1208
1209
1210
1211
1212 #ifdef DEBUG
1213 printk("internal_cmnd (host = %d, channel = %d, target = %d, "
1214 "command = %p, buffer = %p, \nbufflen = %d, done = %p)\n",
1215 SCpnt->host->host_no, SCpnt->channel, SCpnt->target, SCpnt->cmnd,
1216 SCpnt->buffer, SCpnt->bufflen, SCpnt->done);
1217 #endif
1218
1219 if (host->can_queue)
1220 {
1221 #ifdef DEBUG
1222 printk("queuecommand : routine at %p\n",
1223 host->hostt->queuecommand);
1224 #endif
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234 if(!intr_count && SCpnt->host->irq)
1235 disable_irq(SCpnt->host->irq);
1236
1237 host->hostt->queuecommand (SCpnt, scsi_done);
1238
1239 if(!intr_count && SCpnt->host->irq)
1240 enable_irq(SCpnt->host->irq);
1241 }
1242 else
1243 {
1244
1245 #ifdef DEBUG
1246 printk("command() : routine at %p\n", host->hostt->command);
1247 #endif
1248 temp=host->hostt->command (SCpnt);
1249 SCpnt->result = temp;
1250 #ifdef DEBUG_DELAY
1251 clock = jiffies + 4 * HZ;
1252 while (jiffies < clock);
1253 printk("done(host = %d, result = %04x) : routine at %p\n",
1254 host->host_no, temp, host->hostt->command);
1255 #endif
1256 scsi_done(SCpnt);
1257 }
1258 #ifdef DEBUG
1259 printk("leaving internal_cmnd()\n");
1260 #endif
1261 }
1262
1263 static void scsi_request_sense (Scsi_Cmnd * SCpnt)
1264 {
1265 unsigned int flags;
1266
1267 save_flags(flags);
1268 cli();
1269 SCpnt->flags |= WAS_SENSE | ASKED_FOR_SENSE;
1270 update_timeout(SCpnt, SENSE_TIMEOUT);
1271 restore_flags(flags);
1272
1273
1274 memcpy ((void *) SCpnt->cmnd , (void *) generic_sense,
1275 sizeof(generic_sense));
1276
1277 SCpnt->cmnd[1] = SCpnt->lun << 5;
1278 SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);
1279
1280 SCpnt->request_buffer = &SCpnt->sense_buffer;
1281 SCpnt->request_bufflen = sizeof(SCpnt->sense_buffer);
1282 SCpnt->use_sg = 0;
1283 SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
1284 internal_cmnd (SCpnt);
1285 }
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296 void scsi_do_cmd (Scsi_Cmnd * SCpnt, const void *cmnd ,
1297 void *buffer, unsigned bufflen, void (*done)(Scsi_Cmnd *),
1298 int timeout, int retries)
1299 {
1300 unsigned long flags;
1301 struct Scsi_Host * host = SCpnt->host;
1302
1303 #ifdef DEBUG
1304 {
1305 int i;
1306 int target = SCpnt->target;
1307 printk ("scsi_do_cmd (host = %d, channel = %d target = %d, "
1308 "buffer =%p, bufflen = %d, done = %p, timeout = %d, "
1309 "retries = %d)\n"
1310 "command : " , host->host_no, SCpnt->channel, target, buffer,
1311 bufflen, done, timeout, retries);
1312 for (i = 0; i < 10; ++i)
1313 printk ("%02x ", ((unsigned char *) cmnd)[i]);
1314 printk("\n");
1315 }
1316 #endif
1317
1318 if (!host)
1319 {
1320 panic ("Invalid or not present host.\n");
1321 }
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332 save_flags(flags);
1333 cli();
1334 SCpnt->pid = scsi_pid++;
1335
1336 while (SCSI_BLOCK(host)) {
1337 restore_flags(flags);
1338 SCSI_SLEEP(&host->host_wait, SCSI_BLOCK(host));
1339 cli();
1340 }
1341
1342 if (host->block) host_active = host;
1343
1344 host->host_busy++;
1345 restore_flags(flags);
1346
1347
1348
1349
1350
1351
1352
1353
1354 memcpy ((void *) SCpnt->data_cmnd , (const void *) cmnd, 12);
1355 #if 0
1356 SCpnt->host = host;
1357 SCpnt->channel = channel;
1358 SCpnt->target = target;
1359 SCpnt->lun = (SCpnt->data_cmnd[1] >> 5);
1360 #endif
1361 SCpnt->reset_chain = NULL;
1362 SCpnt->serial_number = 0;
1363 SCpnt->bufflen = bufflen;
1364 SCpnt->buffer = buffer;
1365 SCpnt->flags=0;
1366 SCpnt->retries=0;
1367 SCpnt->allowed=retries;
1368 SCpnt->done = done;
1369 SCpnt->timeout_per_command = timeout;
1370
1371 memcpy ((void *) SCpnt->cmnd , (const void *) cmnd, 12);
1372
1373
1374
1375 memset ((void *) SCpnt->sense_buffer, 0, sizeof SCpnt->sense_buffer);
1376 SCpnt->request_buffer = buffer;
1377 SCpnt->request_bufflen = bufflen;
1378 SCpnt->old_use_sg = SCpnt->use_sg;
1379 if (SCpnt->cmd_len == 0)
1380 SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
1381 SCpnt->old_cmd_len = SCpnt->cmd_len;
1382
1383
1384
1385 SCpnt->internal_timeout = 0;
1386 SCpnt->abort_reason = 0;
1387 internal_cmnd (SCpnt);
1388
1389 #ifdef DEBUG
1390 printk ("Leaving scsi_do_cmd()\n");
1391 #endif
1392 }
1393
1394 static int check_sense (Scsi_Cmnd * SCpnt)
1395 {
1396
1397
1398
1399
1400 if (((SCpnt->sense_buffer[0] & 0x70) >> 4) != 7) {
1401 if(!(SCpnt->flags & ASKED_FOR_SENSE))
1402 return SUGGEST_SENSE;
1403 else
1404 return SUGGEST_RETRY;
1405 }
1406
1407 SCpnt->flags &= ~ASKED_FOR_SENSE;
1408
1409 #ifdef DEBUG_INIT
1410 printk("scsi%d, channel%d : ", SCpnt->host->host_no, SCpnt->channel);
1411 print_sense("", SCpnt);
1412 printk("\n");
1413 #endif
1414 if (SCpnt->sense_buffer[2] & 0xe0)
1415 return SUGGEST_ABORT;
1416
1417 switch (SCpnt->sense_buffer[2] & 0xf)
1418 {
1419 case NO_SENSE:
1420 return 0;
1421 case RECOVERED_ERROR:
1422 return SUGGEST_IS_OK;
1423
1424 case ABORTED_COMMAND:
1425 return SUGGEST_RETRY;
1426 case NOT_READY:
1427 case UNIT_ATTENTION:
1428
1429
1430
1431
1432
1433
1434 if( SCpnt->device->expecting_cc_ua )
1435 {
1436 SCpnt->device->expecting_cc_ua = 0;
1437 return SUGGEST_RETRY;
1438 }
1439 return SUGGEST_ABORT;
1440
1441
1442 case COPY_ABORTED:
1443 case VOLUME_OVERFLOW:
1444 case MISCOMPARE:
1445
1446 case MEDIUM_ERROR:
1447 return SUGGEST_REMAP;
1448 case BLANK_CHECK:
1449 case DATA_PROTECT:
1450 case HARDWARE_ERROR:
1451 case ILLEGAL_REQUEST:
1452 default:
1453 return SUGGEST_ABORT;
1454 }
1455 }
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478 static void scsi_done (Scsi_Cmnd * SCpnt)
1479 {
1480 int status=0;
1481 int exit=0;
1482 int checked;
1483 int oldto;
1484 struct Scsi_Host * host = SCpnt->host;
1485 int result = SCpnt->result;
1486 SCpnt->serial_number = 0;
1487 oldto = update_timeout(SCpnt, 0);
1488
1489 #ifdef DEBUG_TIMEOUT
1490 if(result) printk("Non-zero result in scsi_done %x %d:%d\n",
1491 result, SCpnt->target, SCpnt->lun);
1492 #endif
1493
1494
1495
1496
1497 if(host_byte(result) == DID_ABORT && SCpnt->abort_reason)
1498 SCpnt->result = result = (result & 0xff00ffff) |
1499 (SCpnt->abort_reason << 16);
1500
1501
1502 #define FINISHED 0
1503 #define MAYREDO 1
1504 #define REDO 3
1505 #define PENDING 4
1506
1507 #ifdef DEBUG
1508 printk("In scsi_done(host = %d, result = %06x)\n", host->host_no, result);
1509 #endif
1510
1511 if(SCpnt->flags & WAS_SENSE)
1512 {
1513 SCpnt->use_sg = SCpnt->old_use_sg;
1514 SCpnt->cmd_len = SCpnt->old_cmd_len;
1515 }
1516
1517 switch (host_byte(result))
1518 {
1519 case DID_OK:
1520 if (status_byte(result) && (SCpnt->flags & WAS_SENSE))
1521
1522 {
1523 SCpnt->flags &= ~WAS_SENSE;
1524 #if 0
1525 SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
1526 #endif
1527
1528 if (!(SCpnt->flags & WAS_RESET))
1529 {
1530 printk("scsi%d : channel %d target %d lun %d request sense"
1531 " failed, performing reset.\n",
1532 SCpnt->host->host_no, SCpnt->channel, SCpnt->target,
1533 SCpnt->lun);
1534 scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
1535 return;
1536 }
1537 else
1538 {
1539 exit = (DRIVER_HARD | SUGGEST_ABORT);
1540 status = FINISHED;
1541 }
1542 }
1543 else switch(msg_byte(result))
1544 {
1545 case COMMAND_COMPLETE:
1546 switch (status_byte(result))
1547 {
1548 case GOOD:
1549 if (SCpnt->flags & WAS_SENSE)
1550 {
1551 #ifdef DEBUG
1552 printk ("In scsi_done, GOOD status, COMMAND COMPLETE, parsing sense information.\n");
1553 #endif
1554 SCpnt->flags &= ~WAS_SENSE;
1555 #if 0
1556 SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
1557 #endif
1558
1559 switch (checked = check_sense(SCpnt))
1560 {
1561 case SUGGEST_SENSE:
1562 case 0:
1563 #ifdef DEBUG
1564 printk("NO SENSE. status = REDO\n");
1565 #endif
1566 update_timeout(SCpnt, oldto);
1567 status = REDO;
1568 break;
1569 case SUGGEST_IS_OK:
1570 break;
1571 case SUGGEST_REMAP:
1572 case SUGGEST_RETRY:
1573 #ifdef DEBUG
1574 printk("SENSE SUGGEST REMAP or SUGGEST RETRY - status = MAYREDO\n");
1575 #endif
1576 status = MAYREDO;
1577 exit = DRIVER_SENSE | SUGGEST_RETRY;
1578 break;
1579 case SUGGEST_ABORT:
1580 #ifdef DEBUG
1581 printk("SENSE SUGGEST ABORT - status = FINISHED");
1582 #endif
1583 status = FINISHED;
1584 exit = DRIVER_SENSE | SUGGEST_ABORT;
1585 break;
1586 default:
1587 printk ("Internal error %s %d \n", __FILE__,
1588 __LINE__);
1589 }
1590 }
1591 else
1592 {
1593 #ifdef DEBUG
1594 printk("COMMAND COMPLETE message returned, status = FINISHED. \n");
1595 #endif
1596 exit = DRIVER_OK;
1597 status = FINISHED;
1598 }
1599 break;
1600
1601 case CHECK_CONDITION:
1602 switch (check_sense(SCpnt))
1603 {
1604 case 0:
1605 update_timeout(SCpnt, oldto);
1606 status = REDO;
1607 break;
1608 case SUGGEST_REMAP:
1609 case SUGGEST_RETRY:
1610 status = MAYREDO;
1611 exit = DRIVER_SENSE | SUGGEST_RETRY;
1612 break;
1613 case SUGGEST_ABORT:
1614 status = FINISHED;
1615 exit = DRIVER_SENSE | SUGGEST_ABORT;
1616 break;
1617 case SUGGEST_SENSE:
1618 scsi_request_sense (SCpnt);
1619 status = PENDING;
1620 break;
1621 }
1622 break;
1623
1624 case CONDITION_GOOD:
1625 case INTERMEDIATE_GOOD:
1626 case INTERMEDIATE_C_GOOD:
1627 break;
1628
1629 case BUSY:
1630 update_timeout(SCpnt, oldto);
1631 status = REDO;
1632 break;
1633
1634 case RESERVATION_CONFLICT:
1635 printk("scsi%d, channel %d : RESERVATION CONFLICT performing"
1636 " reset.\n", SCpnt->host->host_no, SCpnt->channel);
1637 scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
1638 return;
1639 #if 0
1640 exit = DRIVER_SOFT | SUGGEST_ABORT;
1641 status = MAYREDO;
1642 break;
1643 #endif
1644 default:
1645 printk ("Internal error %s %d \n"
1646 "status byte = %d \n", __FILE__,
1647 __LINE__, status_byte(result));
1648
1649 }
1650 break;
1651 default:
1652 panic("scsi: unsupported message byte %d received\n",
1653 msg_byte(result));
1654 }
1655 break;
1656 case DID_TIME_OUT:
1657 #ifdef DEBUG
1658 printk("Host returned DID_TIME_OUT - ");
1659 #endif
1660
1661 if (SCpnt->flags & WAS_TIMEDOUT)
1662 {
1663 #ifdef DEBUG
1664 printk("Aborting\n");
1665 #endif
1666
1667
1668
1669
1670 if (SCpnt->cmnd[0] != TEST_UNIT_READY &&
1671 SCpnt->cmnd[0] != INQUIRY)
1672 status = MAYREDO;
1673 exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
1674 }
1675 else
1676 {
1677 #ifdef DEBUG
1678 printk ("Retrying.\n");
1679 #endif
1680 SCpnt->flags |= WAS_TIMEDOUT;
1681 SCpnt->internal_timeout &= ~IN_ABORT;
1682 status = REDO;
1683 }
1684 break;
1685 case DID_BUS_BUSY:
1686 case DID_PARITY:
1687 status = REDO;
1688 break;
1689 case DID_NO_CONNECT:
1690 #ifdef DEBUG
1691 printk("Couldn't connect.\n");
1692 #endif
1693 exit = (DRIVER_HARD | SUGGEST_ABORT);
1694 break;
1695 case DID_ERROR:
1696 status = MAYREDO;
1697 exit = (DRIVER_HARD | SUGGEST_ABORT);
1698 break;
1699 case DID_BAD_TARGET:
1700 case DID_ABORT:
1701 exit = (DRIVER_INVALID | SUGGEST_ABORT);
1702 break;
1703 case DID_RESET:
1704 if (SCpnt->flags & IS_RESETTING)
1705 {
1706 SCpnt->flags &= ~IS_RESETTING;
1707 status = REDO;
1708 break;
1709 }
1710
1711 if(msg_byte(result) == GOOD &&
1712 status_byte(result) == CHECK_CONDITION) {
1713 switch (check_sense(SCpnt)) {
1714 case 0:
1715 update_timeout(SCpnt, oldto);
1716 status = REDO;
1717 break;
1718 case SUGGEST_REMAP:
1719 case SUGGEST_RETRY:
1720 status = MAYREDO;
1721 exit = DRIVER_SENSE | SUGGEST_RETRY;
1722 break;
1723 case SUGGEST_ABORT:
1724 status = FINISHED;
1725 exit = DRIVER_SENSE | SUGGEST_ABORT;
1726 break;
1727 case SUGGEST_SENSE:
1728 scsi_request_sense (SCpnt);
1729 status = PENDING;
1730 break;
1731 }
1732 } else {
1733 status=REDO;
1734 exit = SUGGEST_RETRY;
1735 }
1736 break;
1737 default :
1738 exit = (DRIVER_ERROR | SUGGEST_DIE);
1739 }
1740
1741 switch (status)
1742 {
1743 case FINISHED:
1744 case PENDING:
1745 break;
1746 case MAYREDO:
1747 #ifdef DEBUG
1748 printk("In MAYREDO, allowing %d retries, have %d\n",
1749 SCpnt->allowed, SCpnt->retries);
1750 #endif
1751 if ((++SCpnt->retries) < SCpnt->allowed)
1752 {
1753 if ((SCpnt->retries >= (SCpnt->allowed >> 1))
1754 && !(jiffies < SCpnt->host->last_reset + MIN_RESET_PERIOD)
1755 && !(SCpnt->flags & WAS_RESET))
1756 {
1757 printk("scsi%d channel %d : resetting for second half of retries.\n",
1758 SCpnt->host->host_no, SCpnt->channel);
1759 scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
1760 break;
1761 }
1762
1763 }
1764 else
1765 {
1766 status = FINISHED;
1767 break;
1768 }
1769
1770
1771 case REDO:
1772
1773 if (SCpnt->flags & WAS_SENSE)
1774 scsi_request_sense(SCpnt);
1775 else
1776 {
1777 memcpy ((void *) SCpnt->cmnd,
1778 (void*) SCpnt->data_cmnd,
1779 sizeof(SCpnt->data_cmnd));
1780 SCpnt->request_buffer = SCpnt->buffer;
1781 SCpnt->request_bufflen = SCpnt->bufflen;
1782 SCpnt->use_sg = SCpnt->old_use_sg;
1783 SCpnt->cmd_len = SCpnt->old_cmd_len;
1784 internal_cmnd (SCpnt);
1785 }
1786 break;
1787 default:
1788 INTERNAL_ERROR;
1789 }
1790
1791 if (status == FINISHED) {
1792 #ifdef DEBUG
1793 printk("Calling done function - at address %p\n", SCpnt->done);
1794 #endif
1795 host->host_busy--;
1796
1797 if (host->block && host->host_busy == 0) {
1798 host_active = NULL;
1799
1800
1801 if (MAJOR(SCpnt->request.rq_dev) != SCSI_DISK_MAJOR &&
1802 MAJOR(SCpnt->request.rq_dev) != SCSI_CDROM_MAJOR) {
1803 struct Scsi_Host * next;
1804
1805 for (next = host->block; next != host; next = next->block)
1806 wake_up(&next->host_wait);
1807 }
1808
1809 }
1810
1811 wake_up(&host->host_wait);
1812 SCpnt->result = result | ((exit & 0xff) << 24);
1813 SCpnt->use_sg = SCpnt->old_use_sg;
1814 SCpnt->cmd_len = SCpnt->old_cmd_len;
1815 SCpnt->done (SCpnt);
1816 }
1817
1818 #undef FINISHED
1819 #undef REDO
1820 #undef MAYREDO
1821 #undef PENDING
1822 }
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840 int scsi_abort (Scsi_Cmnd * SCpnt, int why)
1841 {
1842 int oldto;
1843 unsigned long flags;
1844 struct Scsi_Host * host = SCpnt->host;
1845
1846 while(1)
1847 {
1848 save_flags(flags);
1849 cli();
1850
1851
1852
1853
1854
1855 if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
1856 restore_flags(flags);
1857 return 0;
1858 }
1859
1860 if (SCpnt->internal_timeout & IN_ABORT)
1861 {
1862 restore_flags(flags);
1863 while (SCpnt->internal_timeout & IN_ABORT)
1864 barrier();
1865 }
1866 else
1867 {
1868 SCpnt->internal_timeout |= IN_ABORT;
1869 oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
1870
1871 if ((SCpnt->flags & IS_RESETTING) && SCpnt->device->soft_reset) {
1872
1873
1874
1875 printk("Stale command on %d %d:%d appears to have died when"
1876 " the bus was reset\n",
1877 SCpnt->channel, SCpnt->target, SCpnt->lun);
1878 }
1879
1880 restore_flags(flags);
1881 if (!host->host_busy) {
1882 SCpnt->internal_timeout &= ~IN_ABORT;
1883 update_timeout(SCpnt, oldto);
1884 return 0;
1885 }
1886 printk("scsi : aborting command due to timeout : pid %lu, scsi%d,"
1887 " channel %d, id %d, lun %d ",
1888 SCpnt->pid, SCpnt->host->host_no, (int) SCpnt->channel,
1889 (int) SCpnt->target, (int) SCpnt->lun);
1890 print_command (SCpnt->cmnd);
1891 if (SCpnt->serial_number != SCpnt->serial_number_at_timeout)
1892 return 0;
1893 SCpnt->abort_reason = why;
1894 switch(host->hostt->abort(SCpnt)) {
1895
1896
1897
1898
1899 case SCSI_ABORT_BUSY:
1900
1901
1902 case SCSI_ABORT_SNOOZE:
1903 if(why == DID_TIME_OUT) {
1904 save_flags(flags);
1905 cli();
1906 SCpnt->internal_timeout &= ~IN_ABORT;
1907 if(SCpnt->flags & WAS_TIMEDOUT) {
1908 restore_flags(flags);
1909 return 1;
1910
1911
1912
1913 } else {
1914 SCpnt->flags |= WAS_TIMEDOUT;
1915 oldto = SCpnt->timeout_per_command;
1916 update_timeout(SCpnt, oldto);
1917 }
1918 restore_flags(flags);
1919 }
1920 return 0;
1921 case SCSI_ABORT_PENDING:
1922 if(why != DID_TIME_OUT) {
1923 save_flags(flags);
1924 cli();
1925 update_timeout(SCpnt, oldto);
1926 restore_flags(flags);
1927 }
1928 return 0;
1929 case SCSI_ABORT_SUCCESS:
1930
1931
1932
1933 SCpnt->internal_timeout &= ~IN_ABORT;
1934 return 0;
1935 case SCSI_ABORT_NOT_RUNNING:
1936 SCpnt->internal_timeout &= ~IN_ABORT;
1937 update_timeout(SCpnt, 0);
1938 return 0;
1939 case SCSI_ABORT_ERROR:
1940 default:
1941 SCpnt->internal_timeout &= ~IN_ABORT;
1942 return 1;
1943 }
1944 }
1945 }
1946 }
1947
1948
1949
1950
1951 static inline void scsi_mark_device_reset(Scsi_Device *Device)
1952 {
1953 Device->was_reset = 1;
1954 Device->expecting_cc_ua = 1;
1955 }
1956
1957
1958
1959
1960 void scsi_mark_host_reset(struct Scsi_Host *Host)
1961 {
1962 Scsi_Cmnd *SCpnt;
1963 for (SCpnt = Host->host_queue; SCpnt; SCpnt = SCpnt->next)
1964 scsi_mark_device_reset(SCpnt->device);
1965 }
1966
1967
1968
1969
1970 void scsi_mark_bus_reset(struct Scsi_Host *Host, int channel)
1971 {
1972 Scsi_Cmnd *SCpnt;
1973 for (SCpnt = Host->host_queue; SCpnt; SCpnt = SCpnt->next)
1974 if (SCpnt->channel == channel)
1975 scsi_mark_device_reset(SCpnt->device);
1976 }
1977
1978
1979 int scsi_reset (Scsi_Cmnd * SCpnt, unsigned int reset_flags)
1980 {
1981 int temp;
1982 unsigned long flags;
1983 Scsi_Cmnd * SCpnt1;
1984 struct Scsi_Host * host = SCpnt->host;
1985
1986 printk("SCSI bus is being reset for host %d channel %d.\n",
1987 host->host_no, SCpnt->channel);
1988
1989 #if 0
1990
1991
1992
1993
1994
1995
1996
1997 SCpnt->host->suggest_bus_reset = FALSE;
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014 SCpnt1 = host->host_queue;
2015 while(SCpnt1) {
2016 if( SCpnt1->request.rq_status != RQ_INACTIVE
2017 && (SCpnt1->flags & (WAS_RESET | IS_RESETTING)) == 0 )
2018 break;
2019 SCpnt1 = SCpnt1->next;
2020 }
2021 if( SCpnt1 == NULL ) {
2022 reset_flags |= SCSI_RESET_SUGGEST_BUS_RESET;
2023 }
2024
2025
2026
2027
2028
2029
2030
2031
2032 if( reset_flags & SCSI_RESET_SUGGEST_BUS_RESET ) {
2033 SCpnt->host->suggest_bus_reset = TRUE;
2034 }
2035 #endif
2036
2037 while (1) {
2038 save_flags(flags);
2039 cli();
2040
2041
2042
2043
2044
2045 if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
2046 restore_flags(flags);
2047 return 0;
2048 }
2049
2050 if (SCpnt->internal_timeout & IN_RESET)
2051 {
2052 restore_flags(flags);
2053 while (SCpnt->internal_timeout & IN_RESET)
2054 barrier();
2055 }
2056 else
2057 {
2058 SCpnt->internal_timeout |= IN_RESET;
2059 update_timeout(SCpnt, RESET_TIMEOUT);
2060
2061 if (host->host_busy)
2062 {
2063 restore_flags(flags);
2064 SCpnt1 = host->host_queue;
2065 while(SCpnt1) {
2066 if (SCpnt1->request.rq_status != RQ_INACTIVE) {
2067 #if 0
2068 if (!(SCpnt1->flags & IS_RESETTING) &&
2069 !(SCpnt1->internal_timeout & IN_ABORT))
2070 scsi_abort(SCpnt1, DID_RESET);
2071 #endif
2072 SCpnt1->flags |= (WAS_RESET | IS_RESETTING);
2073 }
2074 SCpnt1 = SCpnt1->next;
2075 }
2076
2077 host->last_reset = jiffies;
2078 temp = host->hostt->reset(SCpnt, reset_flags);
2079 host->last_reset = jiffies;
2080 }
2081 else
2082 {
2083 if (!host->block) host->host_busy++;
2084 restore_flags(flags);
2085 host->last_reset = jiffies;
2086 SCpnt->flags |= (WAS_RESET | IS_RESETTING);
2087 temp = host->hostt->reset(SCpnt, reset_flags);
2088 host->last_reset = jiffies;
2089 if (!host->block) host->host_busy--;
2090 }
2091
2092 #ifdef DEBUG
2093 printk("scsi reset function returned %d\n", temp);
2094 #endif
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105 switch(temp & SCSI_RESET_ACTION) {
2106 case SCSI_RESET_SUCCESS:
2107 if (temp & SCSI_RESET_HOST_RESET)
2108 scsi_mark_host_reset(host);
2109 else if (temp & SCSI_RESET_BUS_RESET)
2110 scsi_mark_bus_reset(host, SCpnt->channel);
2111 else scsi_mark_device_reset(SCpnt->device);
2112 save_flags(flags);
2113 cli();
2114 SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
2115 restore_flags(flags);
2116 return 0;
2117 case SCSI_RESET_PENDING:
2118 if (temp & SCSI_RESET_HOST_RESET)
2119 scsi_mark_host_reset(host);
2120 else if (temp & SCSI_RESET_BUS_RESET)
2121 scsi_mark_bus_reset(host, SCpnt->channel);
2122 else scsi_mark_device_reset(SCpnt->device);
2123 case SCSI_RESET_NOT_RUNNING:
2124 return 0;
2125 case SCSI_RESET_PUNT:
2126 SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
2127 scsi_request_sense (SCpnt);
2128 return 0;
2129 case SCSI_RESET_WAKEUP:
2130 if (temp & SCSI_RESET_HOST_RESET)
2131 scsi_mark_host_reset(host);
2132 else if (temp & SCSI_RESET_BUS_RESET)
2133 scsi_mark_bus_reset(host, SCpnt->channel);
2134 else scsi_mark_device_reset(SCpnt->device);
2135 SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
2136 scsi_request_sense (SCpnt);
2137
2138
2139
2140
2141
2142
2143 if( temp & SCSI_RESET_HOST_RESET )
2144 {
2145 SCpnt1 = host->host_queue;
2146 while(SCpnt1) {
2147 if (SCpnt1->request.rq_status != RQ_INACTIVE
2148 && SCpnt1 != SCpnt)
2149 scsi_request_sense (SCpnt1);
2150 SCpnt1 = SCpnt1->next;
2151 }
2152 } else if( temp & SCSI_RESET_BUS_RESET ) {
2153 SCpnt1 = host->host_queue;
2154 while(SCpnt1) {
2155 if(SCpnt1->request.rq_status != RQ_INACTIVE
2156 && SCpnt1 != SCpnt
2157 && SCpnt1->channel == SCpnt->channel)
2158 scsi_request_sense (SCpnt);
2159 SCpnt1 = SCpnt1->next;
2160 }
2161 }
2162 return 0;
2163 case SCSI_RESET_SNOOZE:
2164
2165
2166
2167
2168
2169 save_flags(flags);
2170 cli();
2171 SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
2172 update_timeout(SCpnt, 0);
2173 restore_flags(flags);
2174
2175 case SCSI_RESET_ERROR:
2176 default:
2177 return 1;
2178 }
2179
2180 return temp;
2181 }
2182 }
2183 }
2184
2185
2186 static void scsi_main_timeout(void)
2187 {
2188
2189
2190
2191
2192 int timed_out;
2193 unsigned long flags;
2194 struct Scsi_Host * host;
2195 Scsi_Cmnd * SCpnt = NULL;
2196
2197 save_flags(flags);
2198 cli();
2199
2200 update_timeout(NULL, 0);
2201
2202
2203
2204
2205
2206 timed_out = 0;
2207 for (host = scsi_hostlist; host; host = host->next) {
2208 for (SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
2209 if (SCpnt->timeout == -1)
2210 {
2211 SCpnt->timeout = 0;
2212 SCpnt->serial_number_at_timeout = SCpnt->serial_number;
2213 ++timed_out;
2214 }
2215 }
2216 if (timed_out > 0) {
2217 for (host = scsi_hostlist; host; host = host->next) {
2218 for (SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
2219 if (SCpnt->serial_number_at_timeout > 0 &&
2220 SCpnt->serial_number_at_timeout == SCpnt->serial_number)
2221 {
2222 restore_flags(flags);
2223 scsi_times_out(SCpnt);
2224 SCpnt->serial_number_at_timeout = 0;
2225 cli();
2226 }
2227 }
2228 }
2229 restore_flags(flags);
2230 }
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240 static int update_timeout(Scsi_Cmnd * SCset, int timeout)
2241 {
2242 unsigned int least, used;
2243 unsigned int oldto;
2244 unsigned long flags;
2245 struct Scsi_Host * host;
2246 Scsi_Cmnd * SCpnt = NULL;
2247
2248 save_flags(flags);
2249 cli();
2250
2251 oldto = 0;
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263 if (jiffies == time_start && timer_table[SCSI_TIMER].expires > 0) {
2264 if(SCset){
2265 oldto = SCset->timeout;
2266 SCset->timeout = timeout;
2267 }
2268 restore_flags(flags);
2269 return oldto;
2270 }
2271
2272
2273
2274
2275
2276 used = (time_start) ? (jiffies - time_start) : 0;
2277
2278
2279
2280
2281
2282
2283
2284 oldto = 0;
2285
2286 if(SCset){
2287 oldto = SCset->timeout - used;
2288 SCset->timeout = timeout;
2289 }
2290
2291 least = 0xffffffff;
2292
2293 for(host = scsi_hostlist; host; host = host->next)
2294 for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
2295 if (SCpnt->timeout > 0) {
2296 if (SCpnt != SCset)
2297 SCpnt->timeout -= used;
2298 if(SCpnt->timeout <= 0) SCpnt->timeout = -1;
2299 if(SCpnt->timeout > 0 && SCpnt->timeout < least)
2300 least = SCpnt->timeout;
2301 }
2302
2303
2304
2305
2306
2307
2308 if (least != 0xffffffff)
2309 {
2310 time_start = jiffies;
2311 timer_table[SCSI_TIMER].expires = (time_elapsed = least) + jiffies;
2312 timer_active |= 1 << SCSI_TIMER;
2313 }
2314 else
2315 {
2316 timer_table[SCSI_TIMER].expires = time_start = time_elapsed = 0;
2317 timer_active &= ~(1 << SCSI_TIMER);
2318 }
2319 restore_flags(flags);
2320 return oldto;
2321 }
2322
2323 #ifdef CONFIG_MODULES
2324 static int scsi_register_host(Scsi_Host_Template *);
2325 static void scsi_unregister_host(Scsi_Host_Template *);
2326 #endif
2327
2328 void *scsi_malloc(unsigned int len)
2329 {
2330 unsigned int nbits, mask;
2331 unsigned long flags;
2332 int i, j;
2333 if(len % SECTOR_SIZE != 0 || len > PAGE_SIZE)
2334 return NULL;
2335
2336 save_flags(flags);
2337 cli();
2338 nbits = len >> 9;
2339 mask = (1 << nbits) - 1;
2340
2341 for(i=0;i < dma_sectors / SECTORS_PER_PAGE; i++)
2342 for(j=0; j<=SECTORS_PER_PAGE - nbits; j++){
2343 if ((dma_malloc_freelist[i] & (mask << j)) == 0){
2344 dma_malloc_freelist[i] |= (mask << j);
2345 restore_flags(flags);
2346 dma_free_sectors -= nbits;
2347 #ifdef DEBUG
2348 printk("SMalloc: %d %p\n",len, dma_malloc_pages[i] + (j << 9));
2349 #endif
2350 return (void *) ((unsigned long) dma_malloc_pages[i] + (j << 9));
2351 }
2352 }
2353 restore_flags(flags);
2354 return NULL;
2355 }
2356
2357 int scsi_free(void *obj, unsigned int len)
2358 {
2359 unsigned int page, sector, nbits, mask;
2360 unsigned long flags;
2361
2362 #ifdef DEBUG
2363 printk("scsi_free %p %d\n",obj, len);
2364 #endif
2365
2366 for (page = 0; page < dma_sectors / SECTORS_PER_PAGE; page++) {
2367 unsigned long page_addr = (unsigned long) dma_malloc_pages[page];
2368 if ((unsigned long) obj >= page_addr &&
2369 (unsigned long) obj < page_addr + PAGE_SIZE)
2370 {
2371 sector = (((unsigned long) obj) - page_addr) >> 9;
2372
2373 nbits = len >> 9;
2374 mask = (1 << nbits) - 1;
2375
2376 if ((mask << sector) >= (1 << SECTORS_PER_PAGE))
2377 panic ("scsi_free:Bad memory alignment");
2378
2379 save_flags(flags);
2380 cli();
2381 if((dma_malloc_freelist[page] & (mask << sector)) != (mask<<sector))
2382 panic("scsi_free:Trying to free unused memory");
2383
2384 dma_free_sectors += nbits;
2385 dma_malloc_freelist[page] &= ~(mask << sector);
2386 restore_flags(flags);
2387 return 0;
2388 }
2389 }
2390 panic("scsi_free:Bad offset");
2391 }
2392
2393
2394 int scsi_loadable_module_flag;
2395
2396 void * scsi_init_malloc(unsigned int size, int priority)
2397 {
2398 void * retval;
2399
2400
2401
2402
2403
2404 if ((size % PAGE_SIZE) == 0) {
2405 int order, a_size;
2406 for (order = 0, a_size = PAGE_SIZE;
2407 a_size < size; order++, a_size <<= 1)
2408 ;
2409 retval = (void *) __get_dma_pages(priority & GFP_LEVEL_MASK,
2410 order);
2411 } else
2412 retval = kmalloc(size, priority);
2413
2414 if (retval)
2415 memset(retval, 0, size);
2416 return retval;
2417 }
2418
2419
2420 void scsi_init_free(char * ptr, unsigned int size)
2421 {
2422
2423
2424
2425
2426
2427 if ((size % PAGE_SIZE) == 0) {
2428 int order, a_size;
2429
2430 for (order = 0, a_size = PAGE_SIZE;
2431 a_size < size; order++, a_size <<= 1)
2432 ;
2433 free_pages((unsigned long)ptr, order);
2434 } else
2435 kfree(ptr);
2436 }
2437
2438 void scsi_build_commandblocks(Scsi_Device * SDpnt)
2439 {
2440 struct Scsi_Host *host = SDpnt->host;
2441 int j;
2442 Scsi_Cmnd * SCpnt;
2443
2444 if (SDpnt->queue_depth == 0)
2445 SDpnt->queue_depth = host->cmd_per_lun;
2446 SDpnt->device_queue = NULL;
2447
2448 for(j=0;j<SDpnt->queue_depth;j++){
2449 SCpnt = (Scsi_Cmnd *)
2450 scsi_init_malloc(sizeof(Scsi_Cmnd),
2451 GFP_ATOMIC |
2452 (host->unchecked_isa_dma ? GFP_DMA : 0));
2453 SCpnt->host = host;
2454 SCpnt->device = SDpnt;
2455 SCpnt->target = SDpnt->id;
2456 SCpnt->lun = SDpnt->lun;
2457 SCpnt->channel = SDpnt->channel;
2458 SCpnt->request.rq_status = RQ_INACTIVE;
2459 SCpnt->use_sg = 0;
2460 SCpnt->old_use_sg = 0;
2461 SCpnt->old_cmd_len = 0;
2462 SCpnt->timeout = 0;
2463 SCpnt->underflow = 0;
2464 SCpnt->transfersize = 0;
2465 SCpnt->serial_number = 0;
2466 SCpnt->serial_number_at_timeout = 0;
2467 SCpnt->host_scribble = NULL;
2468 if(host->host_queue)
2469 host->host_queue->prev = SCpnt;
2470 SCpnt->next = host->host_queue;
2471 SCpnt->prev = NULL;
2472 host->host_queue = SCpnt;
2473 SCpnt->device_next = SDpnt->device_queue;
2474 SDpnt->device_queue = SCpnt;
2475 }
2476 SDpnt->has_cmdblocks = 1;
2477 }
2478
2479
2480
2481
2482
2483
2484 int scsi_dev_init(void)
2485 {
2486 Scsi_Device * SDpnt;
2487 struct Scsi_Host * shpnt;
2488 struct Scsi_Device_Template * sdtpnt;
2489 #ifdef FOO_ON_YOU
2490 return;
2491 #endif
2492
2493
2494 dispatch_scsi_info_ptr = dispatch_scsi_info;
2495
2496
2497 scsi_loadable_module_flag = 0;
2498
2499 timer_table[SCSI_TIMER].fn = scsi_main_timeout;
2500 timer_table[SCSI_TIMER].expires = 0;
2501
2502 #ifdef CONFIG_MODULES
2503 register_symtab(&scsi_symbol_table);
2504 #endif
2505
2506
2507 #if CONFIG_PROC_FS
2508 proc_scsi_register(0, &proc_scsi_scsi);
2509 #endif
2510
2511
2512 scsi_init();
2513
2514 scsi_devices = (Scsi_Device *) NULL;
2515
2516 for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next) {
2517 scan_scsis(shpnt,0,0,0,0);
2518 if (shpnt->select_queue_depths != NULL)
2519 (shpnt->select_queue_depths)(shpnt, scsi_devices);
2520 }
2521
2522 printk("scsi : detected ");
2523 for (sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2524 if (sdtpnt->dev_noticed && sdtpnt->name)
2525 printk("%d SCSI %s%s ", sdtpnt->dev_noticed, sdtpnt->name,
2526 (sdtpnt->dev_noticed != 1) ? "s" : "");
2527 printk("total.\n");
2528
2529 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2530 if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
2531
2532 for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
2533 SDpnt->scsi_request_fn = NULL;
2534 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2535 if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
2536 if(SDpnt->attached) scsi_build_commandblocks(SDpnt);
2537 }
2538
2539
2540
2541
2542
2543 resize_dma_pool();
2544
2545
2546
2547
2548
2549 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2550 if(sdtpnt->finish && sdtpnt->nr_dev)
2551 (*sdtpnt->finish)();
2552
2553 scsi_loadable_module_flag = 1;
2554
2555 return 0;
2556 }
2557
2558 static void print_inquiry(unsigned char *data)
2559 {
2560 int i;
2561
2562 printk(" Vendor: ");
2563 for (i = 8; i < 16; i++)
2564 {
2565 if (data[i] >= 0x20 && i < data[4] + 5)
2566 printk("%c", data[i]);
2567 else
2568 printk(" ");
2569 }
2570
2571 printk(" Model: ");
2572 for (i = 16; i < 32; i++)
2573 {
2574 if (data[i] >= 0x20 && i < data[4] + 5)
2575 printk("%c", data[i]);
2576 else
2577 printk(" ");
2578 }
2579
2580 printk(" Rev: ");
2581 for (i = 32; i < 36; i++)
2582 {
2583 if (data[i] >= 0x20 && i < data[4] + 5)
2584 printk("%c", data[i]);
2585 else
2586 printk(" ");
2587 }
2588
2589 printk("\n");
2590
2591 i = data[0] & 0x1f;
2592
2593 printk(" Type: %s ",
2594 i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] : "Unknown " );
2595 printk(" ANSI SCSI revision: %02x", data[2] & 0x07);
2596 if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
2597 printk(" CCS\n");
2598 else
2599 printk("\n");
2600 }
2601
2602
2603 #ifdef CONFIG_PROC_FS
2604 int scsi_proc_info(char *buffer, char **start, off_t offset, int length,
2605 int hostno, int inout)
2606 {
2607 Scsi_Cmnd *SCpnt;
2608 struct Scsi_Device_Template *SDTpnt;
2609 Scsi_Device *scd, *scd_h = NULL;
2610 struct Scsi_Host *HBA_ptr;
2611 char *p;
2612 int host, channel, id, lun;
2613 int size, len = 0;
2614 off_t begin = 0;
2615 off_t pos = 0;
2616
2617 scd = scsi_devices;
2618 HBA_ptr = scsi_hostlist;
2619
2620 if(inout == 0) {
2621 size = sprintf(buffer+len,"Attached devices: %s\n", (scd)?"":"none");
2622 len += size;
2623 pos = begin + len;
2624 while (HBA_ptr) {
2625 #if 0
2626 size += sprintf(buffer+len,"scsi%2d: %s\n", (int) HBA_ptr->host_no,
2627 HBA_ptr->hostt->procname);
2628 len += size;
2629 pos = begin + len;
2630 #endif
2631 scd = scsi_devices;
2632 while (scd) {
2633 if (scd->host == HBA_ptr) {
2634 proc_print_scsidevice(scd, buffer, &size, len);
2635 len += size;
2636 pos = begin + len;
2637
2638 if (pos < offset) {
2639 len = 0;
2640 begin = pos;
2641 }
2642 if (pos > offset + length)
2643 goto stop_output;
2644 }
2645 scd = scd->next;
2646 }
2647 HBA_ptr = HBA_ptr->next;
2648 }
2649
2650 stop_output:
2651 *start=buffer+(offset-begin);
2652 len-=(offset-begin);
2653 if(len>length)
2654 len = length;
2655 return (len);
2656 }
2657
2658 if(!buffer || length < 25 || strncmp("scsi", buffer, 4))
2659 return(-EINVAL);
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672 if(!strncmp("add-single-device", buffer + 5, 17)) {
2673 p = buffer + 23;
2674
2675 host = simple_strtoul(p, &p, 0);
2676 channel = simple_strtoul(p+1, &p, 0);
2677 id = simple_strtoul(p+1, &p, 0);
2678 lun = simple_strtoul(p+1, &p, 0);
2679
2680 printk("scsi singledevice %d %d %d %d\n", host, channel,
2681 id, lun);
2682
2683 while(scd && (scd->host->host_no != host
2684 || scd->channel != channel
2685 || scd->id != id
2686 || scd->lun != lun)) {
2687 scd = scd->next;
2688 }
2689 if(scd)
2690 return(-ENOSYS);
2691 while(HBA_ptr && HBA_ptr->host_no != host)
2692 HBA_ptr = HBA_ptr->next;
2693
2694 if(!HBA_ptr)
2695 return(-ENXIO);
2696
2697 scan_scsis (HBA_ptr, 1, channel, id, lun);
2698 return(length);
2699
2700 }
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713 else if(!strncmp("remove-single-device", buffer + 5, 20)) {
2714 p = buffer + 26;
2715
2716 host = simple_strtoul(p, &p, 0);
2717 channel = simple_strtoul(p+1, &p, 0);
2718 id = simple_strtoul(p+1, &p, 0);
2719 lun = simple_strtoul(p+1, &p, 0);
2720
2721 while(scd != NULL) {
2722 if(scd->host->host_no == host
2723 && scd->channel == channel
2724 && scd->id == id
2725 && scd->lun == lun){
2726 break;
2727 }
2728 scd_h = scd;
2729 scd = scd->next;
2730 }
2731
2732 if(scd == NULL)
2733 return(-ENODEV);
2734
2735 if(scd->access_count)
2736 return(-EBUSY);
2737
2738 SDTpnt = scsi_devicelist;
2739 while(SDTpnt != NULL) {
2740 if(SDTpnt->detach) (*SDTpnt->detach)(scd);
2741 SDTpnt = SDTpnt->next;
2742 }
2743
2744 if(scd->attached == 0) {
2745
2746
2747
2748
2749 for(SCpnt=scd->host->host_queue; SCpnt; SCpnt = SCpnt->next){
2750 if(SCpnt->device == scd) {
2751 if(SCpnt->prev != NULL)
2752 SCpnt->prev->next = SCpnt->next;
2753 if(SCpnt->next != NULL)
2754 SCpnt->next->prev = SCpnt->prev;
2755 if(SCpnt == scd->host->host_queue)
2756 scd->host->host_queue = SCpnt->next;
2757 scsi_init_free((char *) SCpnt, sizeof(*SCpnt));
2758 }
2759 }
2760
2761 if(scd_h != NULL) {
2762 scd_h->next = scd->next;
2763 } else if (scsi_devices == scd) {
2764
2765 scsi_devices = scd->next;
2766 }
2767 scsi_init_free((char *) scd, sizeof(Scsi_Device));
2768 } else {
2769 return(-EBUSY);
2770 }
2771 return(0);
2772 }
2773 return(-EINVAL);
2774 }
2775 #endif
2776
2777
2778
2779
2780
2781 static void resize_dma_pool(void)
2782 {
2783 int i;
2784 unsigned long size;
2785 struct Scsi_Host * shpnt;
2786 struct Scsi_Host * host = NULL;
2787 Scsi_Device * SDpnt;
2788 unsigned long flags;
2789 FreeSectorBitmap * new_dma_malloc_freelist = NULL;
2790 unsigned int new_dma_sectors = 0;
2791 unsigned int new_need_isa_buffer = 0;
2792 unsigned char ** new_dma_malloc_pages = NULL;
2793
2794 if( !scsi_devices )
2795 {
2796
2797
2798
2799 if( dma_free_sectors != dma_sectors )
2800 panic("SCSI DMA pool memory leak %d %d\n",dma_free_sectors,dma_sectors);
2801
2802 for(i=0; i < dma_sectors / SECTORS_PER_PAGE; i++)
2803 scsi_init_free(dma_malloc_pages[i], PAGE_SIZE);
2804 if (dma_malloc_pages)
2805 scsi_init_free((char *) dma_malloc_pages,
2806 (dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_pages));
2807 dma_malloc_pages = NULL;
2808 if (dma_malloc_freelist)
2809 scsi_init_free((char *) dma_malloc_freelist,
2810 (dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_freelist));
2811 dma_malloc_freelist = NULL;
2812 dma_sectors = 0;
2813 dma_free_sectors = 0;
2814 return;
2815 }
2816
2817
2818 new_dma_sectors = 2*SECTORS_PER_PAGE;
2819
2820 if (high_memory-1 > ISA_DMA_THRESHOLD)
2821 scsi_need_isa_bounce_buffers = 1;
2822 else
2823 scsi_need_isa_bounce_buffers = 0;
2824
2825 if (scsi_devicelist)
2826 for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2827 new_dma_sectors += SECTORS_PER_PAGE;
2828
2829 for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
2830 host = SDpnt->host;
2831
2832 if(SDpnt->type != TYPE_TAPE)
2833 new_dma_sectors += ((host->sg_tablesize *
2834 sizeof(struct scatterlist) + 511) >> 9) *
2835 SDpnt->queue_depth;
2836
2837 if(host->unchecked_isa_dma &&
2838 scsi_need_isa_bounce_buffers &&
2839 SDpnt->type != TYPE_TAPE) {
2840 new_dma_sectors += (PAGE_SIZE >> 9) * host->sg_tablesize *
2841 SDpnt->queue_depth;
2842 new_need_isa_buffer++;
2843 }
2844 }
2845
2846
2847 new_dma_sectors = (new_dma_sectors + 15) & 0xfff0;
2848
2849
2850
2851
2852
2853
2854 if( new_dma_sectors < dma_sectors )
2855 new_dma_sectors = dma_sectors;
2856
2857 if (new_dma_sectors)
2858 {
2859 size = (new_dma_sectors / SECTORS_PER_PAGE)*sizeof(FreeSectorBitmap);
2860 new_dma_malloc_freelist = (FreeSectorBitmap *) scsi_init_malloc(size, GFP_ATOMIC);
2861 memset(new_dma_malloc_freelist, 0, size);
2862
2863 size = (new_dma_sectors / SECTORS_PER_PAGE)*sizeof(*new_dma_malloc_pages);
2864 new_dma_malloc_pages = (unsigned char **) scsi_init_malloc(size, GFP_ATOMIC);
2865 memset(new_dma_malloc_pages, 0, size);
2866 }
2867
2868
2869
2870
2871 if( new_dma_sectors > dma_sectors ) {
2872 for(i=dma_sectors / SECTORS_PER_PAGE; i< new_dma_sectors / SECTORS_PER_PAGE; i++)
2873 new_dma_malloc_pages[i] = (unsigned char *)
2874 scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
2875 }
2876
2877
2878
2879
2880 save_flags(flags);
2881 cli();
2882 if (dma_malloc_freelist)
2883 {
2884 size = (dma_sectors / SECTORS_PER_PAGE)*sizeof(FreeSectorBitmap);
2885 memcpy(new_dma_malloc_freelist, dma_malloc_freelist, size);
2886 scsi_init_free((char *) dma_malloc_freelist, size);
2887 }
2888 dma_malloc_freelist = new_dma_malloc_freelist;
2889
2890 if (dma_malloc_pages)
2891 {
2892 size = (dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_pages);
2893 memcpy(new_dma_malloc_pages, dma_malloc_pages, size);
2894 scsi_init_free((char *) dma_malloc_pages, size);
2895 }
2896
2897 dma_free_sectors += new_dma_sectors - dma_sectors;
2898 dma_malloc_pages = new_dma_malloc_pages;
2899 dma_sectors = new_dma_sectors;
2900 need_isa_buffer = new_need_isa_buffer;
2901 restore_flags(flags);
2902 }
2903
2904 #ifdef CONFIG_MODULES
2905
2906
2907
2908
2909
2910 static int scsi_register_host(Scsi_Host_Template * tpnt)
2911 {
2912 int pcount;
2913 struct Scsi_Host * shpnt;
2914 Scsi_Device * SDpnt;
2915 struct Scsi_Device_Template * sdtpnt;
2916 const char * name;
2917
2918 if (tpnt->next || !tpnt->detect) return 1;
2919
2920
2921 pcount = next_scsi_host;
2922 if ((tpnt->present = tpnt->detect(tpnt)))
2923 {
2924 if(pcount == next_scsi_host) {
2925 if(tpnt->present > 1) {
2926 printk("Failure to register low-level scsi driver");
2927 scsi_unregister_host(tpnt);
2928 return 1;
2929 }
2930
2931
2932
2933 scsi_register(tpnt,0);
2934 }
2935 tpnt->next = scsi_hosts;
2936 scsi_hosts = tpnt;
2937
2938
2939 #if CONFIG_PROC_FS
2940 build_proc_dir_entries(tpnt);
2941 #endif
2942
2943 for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2944 if(shpnt->hostt == tpnt)
2945 {
2946 if(tpnt->info)
2947 name = tpnt->info(shpnt);
2948 else
2949 name = tpnt->name;
2950 printk ("scsi%d : %s\n",
2951 shpnt->host_no, name);
2952 }
2953
2954 printk ("scsi : %d host%s.\n", next_scsi_host,
2955 (next_scsi_host == 1) ? "" : "s");
2956
2957 scsi_make_blocked_list();
2958
2959
2960
2961
2962
2963 for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2964 if(shpnt->hostt == tpnt) scan_scsis(shpnt,0,0,0,0);
2965
2966 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2967 if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
2968
2969
2970
2971 for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
2972 if(SDpnt->host->hostt == tpnt)
2973 {
2974 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2975 if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
2976 if(SDpnt->attached) scsi_build_commandblocks(SDpnt);
2977 }
2978
2979
2980
2981
2982 resize_dma_pool();
2983
2984
2985
2986 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2987 if(sdtpnt->finish && sdtpnt->nr_dev)
2988 (*sdtpnt->finish)();
2989 }
2990
2991 #if defined(USE_STATIC_SCSI_MEMORY)
2992 printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
2993 (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
2994 (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
2995 (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
2996 #endif
2997
2998 MOD_INC_USE_COUNT;
2999 return 0;
3000 }
3001
3002
3003
3004
3005
3006 static void scsi_unregister_host(Scsi_Host_Template * tpnt)
3007 {
3008 Scsi_Host_Template * SHT, *SHTp;
3009 Scsi_Device *sdpnt, * sdppnt, * sdpnt1;
3010 Scsi_Cmnd * SCpnt;
3011 unsigned long flags;
3012 struct Scsi_Device_Template * sdtpnt;
3013 struct Scsi_Host * shpnt, *sh1;
3014 int pcount;
3015
3016
3017
3018
3019 for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
3020 if(sdpnt->host->hostt == tpnt && sdpnt->host->hostt->usage_count
3021 && *sdpnt->host->hostt->usage_count) return;
3022
3023 for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
3024 {
3025 if (shpnt->hostt != tpnt) continue;
3026 for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
3027 {
3028 save_flags(flags);
3029 cli();
3030 if(SCpnt->request.rq_status != RQ_INACTIVE) {
3031 restore_flags(flags);
3032 for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
3033 if(SCpnt->request.rq_status == RQ_SCSI_DISCONNECTING)
3034 SCpnt->request.rq_status = RQ_INACTIVE;
3035 printk("Device busy???\n");
3036 return;
3037 }
3038 SCpnt->request.rq_status = RQ_SCSI_DISCONNECTING;
3039 restore_flags(flags);
3040 }
3041 }
3042
3043
3044 for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
3045 if(sdpnt->host->hostt == tpnt)
3046 {
3047 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
3048 if(sdtpnt->detach) (*sdtpnt->detach)(sdpnt);
3049
3050 if (sdpnt->attached) {
3051 printk("Attached usage count = %d\n", sdpnt->attached);
3052 return;
3053 }
3054 }
3055
3056
3057
3058 for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
3059 if(sdpnt->host->hostt == tpnt)
3060 while (sdpnt->host->host_queue) {
3061 SCpnt = sdpnt->host->host_queue->next;
3062 scsi_init_free((char *) sdpnt->host->host_queue, sizeof(Scsi_Cmnd));
3063 sdpnt->host->host_queue = SCpnt;
3064 if (SCpnt) SCpnt->prev = NULL;
3065 sdpnt->has_cmdblocks = 0;
3066 }
3067
3068
3069
3070 sdppnt = NULL;
3071 for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt1)
3072 {
3073 sdpnt1 = sdpnt->next;
3074 if (sdpnt->host->hostt == tpnt) {
3075 if (sdppnt)
3076 sdppnt->next = sdpnt->next;
3077 else
3078 scsi_devices = sdpnt->next;
3079 scsi_init_free((char *) sdpnt, sizeof (Scsi_Device));
3080 } else
3081 sdppnt = sdpnt;
3082 }
3083
3084
3085
3086
3087 shpnt = scsi_hostlist;
3088 while(shpnt) {
3089 sh1 = shpnt->next;
3090 if(shpnt->hostt == tpnt) {
3091 if(shpnt->loaded_as_module) {
3092 pcount = next_scsi_host;
3093
3094 #if CONFIG_PROC_FS
3095 proc_scsi_unregister(tpnt->proc_dir,
3096 shpnt->host_no + PROC_SCSI_FILE);
3097 #endif
3098 if(tpnt->release)
3099 (*tpnt->release)(shpnt);
3100 else {
3101
3102
3103
3104
3105 if (shpnt->irq) free_irq(shpnt->irq, NULL);
3106 if (shpnt->dma_channel != 0xff) free_dma(shpnt->dma_channel);
3107 if (shpnt->io_port && shpnt->n_io_port)
3108 release_region(shpnt->io_port, shpnt->n_io_port);
3109 }
3110 if(pcount == next_scsi_host) scsi_unregister(shpnt);
3111 tpnt->present--;
3112 }
3113 }
3114 shpnt = sh1;
3115 }
3116
3117
3118
3119
3120
3121
3122 if( !scsi_devices )
3123 resize_dma_pool();
3124
3125 printk ("scsi : %d host%s.\n", next_scsi_host,
3126 (next_scsi_host == 1) ? "" : "s");
3127
3128 #if defined(USE_STATIC_SCSI_MEMORY)
3129 printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
3130 (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
3131 (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
3132 (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
3133 #endif
3134
3135 scsi_make_blocked_list();
3136
3137
3138
3139 if (tpnt->present) return;
3140
3141
3142
3143 for(SHTp=NULL, SHT=scsi_hosts; SHT; SHTp=SHT, SHT=SHT->next)
3144 if(SHT == tpnt) {
3145 if(SHTp)
3146 SHTp->next = SHT->next;
3147 else
3148 scsi_hosts = SHT->next;
3149 SHT->next = NULL;
3150 break;
3151 }
3152
3153
3154 #if CONFIG_PROC_FS
3155 proc_scsi_unregister(tpnt->proc_dir, tpnt->proc_dir->low_ino);
3156 #endif
3157 MOD_DEC_USE_COUNT;
3158 }
3159
3160
3161
3162
3163
3164 static int scsi_register_device_module(struct Scsi_Device_Template * tpnt)
3165 {
3166 Scsi_Device * SDpnt;
3167
3168 if (tpnt->next) return 1;
3169
3170 scsi_register_device(tpnt);
3171
3172
3173
3174
3175 for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
3176 if(tpnt->detect) SDpnt->attached += (*tpnt->detect)(SDpnt);
3177
3178
3179
3180
3181
3182 if(tpnt->init && tpnt->dev_noticed)
3183 if ((*tpnt->init)()) return 1;
3184
3185
3186
3187
3188 for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
3189 {
3190 if(tpnt->attach) (*tpnt->attach)(SDpnt);
3191
3192
3193
3194
3195 if(SDpnt->attached && SDpnt->has_cmdblocks == 0)
3196 scsi_build_commandblocks(SDpnt);
3197 }
3198
3199
3200
3201
3202 if(tpnt->finish && tpnt->nr_dev) (*tpnt->finish)();
3203 MOD_INC_USE_COUNT;
3204 return 0;
3205 }
3206
3207 static int scsi_unregister_device(struct Scsi_Device_Template * tpnt)
3208 {
3209 Scsi_Device * SDpnt;
3210 Scsi_Cmnd * SCpnt;
3211 struct Scsi_Device_Template * spnt;
3212 struct Scsi_Device_Template * prev_spnt;
3213
3214
3215
3216
3217 if( *tpnt->usage_count != 0) return 0;
3218
3219
3220
3221
3222 for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
3223 {
3224 if(tpnt->detach) (*tpnt->detach)(SDpnt);
3225 if(SDpnt->attached == 0)
3226 {
3227
3228
3229
3230
3231 for(SCpnt = SDpnt->host->host_queue; SCpnt; SCpnt = SCpnt->next)
3232 {
3233 if(SCpnt->device == SDpnt)
3234 {
3235 if(SCpnt->prev != NULL)
3236 SCpnt->prev->next = SCpnt->next;
3237 if(SCpnt->next != NULL)
3238 SCpnt->next->prev = SCpnt->prev;
3239 if(SCpnt == SDpnt->host->host_queue)
3240 SDpnt->host->host_queue = SCpnt->next;
3241 scsi_init_free((char *) SCpnt, sizeof(*SCpnt));
3242 }
3243 }
3244 SDpnt->has_cmdblocks = 0;
3245 }
3246 }
3247
3248
3249
3250 spnt = scsi_devicelist;
3251 prev_spnt = NULL;
3252 while(spnt != tpnt)
3253 {
3254 prev_spnt = spnt;
3255 spnt = spnt->next;
3256 }
3257 if(prev_spnt == NULL)
3258 scsi_devicelist = tpnt->next;
3259 else
3260 prev_spnt->next = spnt->next;
3261
3262 MOD_DEC_USE_COUNT;
3263
3264
3265
3266
3267 return 0;
3268 }
3269
3270
3271 int scsi_register_module(int module_type, void * ptr)
3272 {
3273 switch(module_type){
3274 case MODULE_SCSI_HA:
3275 return scsi_register_host((Scsi_Host_Template *) ptr);
3276
3277
3278 case MODULE_SCSI_DEV:
3279 #ifdef CONFIG_KERNELD
3280 if (scsi_hosts == NULL)
3281 request_module("scsi_hostadapter");
3282 #endif
3283 return scsi_register_device_module((struct Scsi_Device_Template *) ptr);
3284
3285
3286
3287 case MODULE_SCSI_CONST:
3288
3289
3290
3291 case MODULE_SCSI_IOCTL:
3292
3293 default:
3294 return 1;
3295 }
3296 }
3297
3298 void scsi_unregister_module(int module_type, void * ptr)
3299 {
3300 switch(module_type) {
3301 case MODULE_SCSI_HA:
3302 scsi_unregister_host((Scsi_Host_Template *) ptr);
3303 break;
3304 case MODULE_SCSI_DEV:
3305 scsi_unregister_device((struct Scsi_Device_Template *) ptr);
3306 break;
3307
3308 case MODULE_SCSI_CONST:
3309 case MODULE_SCSI_IOCTL:
3310 break;
3311 default:
3312 }
3313 return;
3314 }
3315
3316 #endif
3317
3318 #ifdef DEBUG_TIMEOUT
3319 static void
3320 scsi_dump_status(void)
3321 {
3322 int i;
3323 struct Scsi_Host * shpnt;
3324 Scsi_Cmnd * SCpnt;
3325 printk("Dump of scsi parameters:\n");
3326 i = 0;
3327 for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
3328 for(SCpnt=shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
3329 {
3330
3331 printk("(%d) %d:%d:%d:%d (%s %ld %ld %ld %d) (%d %d %x) (%d %d %d) %x %x %x\n",
3332 i++, SCpnt->host->host_no,
3333 SCpnt->channel,
3334 SCpnt->target,
3335 SCpnt->lun,
3336 kdevname(SCpnt->request.rq_dev),
3337 SCpnt->request.sector,
3338 SCpnt->request.nr_sectors,
3339 SCpnt->request.current_nr_sectors,
3340 SCpnt->use_sg,
3341 SCpnt->retries,
3342 SCpnt->allowed,
3343 SCpnt->flags,
3344 SCpnt->timeout_per_command,
3345 SCpnt->timeout,
3346 SCpnt->internal_timeout,
3347 SCpnt->cmnd[0],
3348 SCpnt->sense_buffer[2],
3349 SCpnt->result);
3350 }
3351 printk("wait_for_request = %p\n", wait_for_request);
3352
3353 printk("Dump of pending block device requests\n");
3354 for(i=0; i<MAX_BLKDEV; i++)
3355 if(blk_dev[i].current_request)
3356 {
3357 struct request * req;
3358 printk("%d: ", i);
3359 req = blk_dev[i].current_request;
3360 while(req) {
3361 printk("(%s %d %ld %ld %ld) ",
3362 kdevname(req->rq_dev),
3363 req->cmd,
3364 req->sector,
3365 req->nr_sectors,
3366 req->current_nr_sectors);
3367 req = req->next;
3368 }
3369 printk("\n");
3370 }
3371 }
3372 #endif
3373
3374 #ifdef MODULE
3375
3376 int init_module(void) {
3377 unsigned long size;
3378
3379
3380
3381
3382 dispatch_scsi_info_ptr = dispatch_scsi_info;
3383
3384 timer_table[SCSI_TIMER].fn = scsi_main_timeout;
3385 timer_table[SCSI_TIMER].expires = 0;
3386 register_symtab(&scsi_symbol_table);
3387 scsi_loadable_module_flag = 1;
3388
3389
3390 #if CONFIG_PROC_FS
3391 proc_scsi_register(0, &proc_scsi_scsi);
3392 #endif
3393
3394
3395 dma_sectors = PAGE_SIZE / SECTOR_SIZE;
3396 dma_free_sectors= dma_sectors;
3397
3398
3399
3400
3401
3402
3403 size = (dma_sectors / SECTORS_PER_PAGE)*sizeof(FreeSectorBitmap);
3404 dma_malloc_freelist = (unsigned char *) scsi_init_malloc(size, GFP_ATOMIC);
3405 memset(dma_malloc_freelist, 0, size);
3406
3407
3408 dma_malloc_pages = (unsigned char **)
3409 scsi_init_malloc((dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_pages), GFP_ATOMIC);
3410 dma_malloc_pages[0] = (unsigned char *)
3411 scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
3412 return 0;
3413 }
3414
3415 void cleanup_module( void)
3416 {
3417 #if CONFIG_PROC_FS
3418 proc_scsi_unregister(0, PROC_SCSI_SCSI);
3419 #endif
3420
3421
3422 dispatch_scsi_info_ptr = 0L;
3423
3424
3425
3426
3427 resize_dma_pool();
3428
3429 timer_table[SCSI_TIMER].fn = NULL;
3430 timer_table[SCSI_TIMER].expires = 0;
3431 }
3432 #endif
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451