This source file includes following definitions.
- sd_open
- sd_release
- sd_geninit
- rw_intr
- do_sd_request
- requeue_sd_request
- check_scsidisk_media_change
- sd_init_done
- sd_init_onedisk
- sd_init
- sd_finish
- sd_detect
- sd_attach
- revalidate_scsidisk
- fop_revalidate_scsidisk
- sd_detach
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <linux/fs.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/mm.h>
23 #include <linux/string.h>
24 #include <linux/errno.h>
25 #include <asm/system.h>
26
27 #define MAJOR_NR SCSI_DISK_MAJOR
28 #include "../block/blk.h"
29 #include "scsi.h"
30 #include "hosts.h"
31 #include "sd.h"
32 #include "scsi_ioctl.h"
33 #include "constants.h"
34
35 #include <linux/genhd.h>
36
37
38
39
40
41 #define MAX_RETRIES 5
42
43
44
45
46
47 #define SD_TIMEOUT 600
48 #define SD_MOD_TIMEOUT 750
49
50 #define CLUSTERABLE_DEVICE(SC) (SC->host->hostt->use_clustering && \
51 SC->device->type != TYPE_MOD)
52
53 struct hd_struct * sd;
54 int revalidate_scsidisk(int dev, int maxusage);
55
56 Scsi_Disk * rscsi_disks = NULL;
57 static int * sd_sizes;
58 static int * sd_blocksizes;
59 static int * sd_hardsizes;
60
61 extern int sd_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
62
63 static int check_scsidisk_media_change(dev_t);
64 static int fop_revalidate_scsidisk(dev_t);
65
66 static sd_init_onedisk(int);
67
68 static void requeue_sd_request (Scsi_Cmnd * SCpnt);
69
70 static void sd_init(void);
71 static void sd_finish(void);
72 static int sd_attach(Scsi_Device *);
73 static int sd_detect(Scsi_Device *);
74 static void sd_detach(Scsi_Device *);
75
76 struct Scsi_Device_Template sd_template = {NULL, "disk", "sd", TYPE_DISK,
77 SCSI_DISK_MAJOR, 0, 0, 0, 1,
78 sd_detect, sd_init,
79 sd_finish, sd_attach, sd_detach};
80
81 static int sd_open(struct inode * inode, struct file * filp)
82 {
83 int target;
84 target = DEVICE_NR(MINOR(inode->i_rdev));
85
86 if(target >= sd_template.dev_max || !rscsi_disks[target].device)
87 return -ENXIO;
88
89
90
91
92 if(sd_sizes[MINOR(inode->i_rdev)] == 0)
93 return -ENXIO;
94
95
96
97
98 while (rscsi_disks[target].device->busy);
99
100 if(rscsi_disks[target].device->removable) {
101 check_disk_change(inode->i_rdev);
102
103 if(!rscsi_disks[target].device->access_count)
104 sd_ioctl(inode, NULL, SCSI_IOCTL_DOORLOCK, 0);
105 };
106 rscsi_disks[target].device->access_count++;
107 if (rscsi_disks[target].device->host->hostt->usage_count)
108 (*rscsi_disks[target].device->host->hostt->usage_count)++;
109 return 0;
110 }
111
112 static void sd_release(struct inode * inode, struct file * file)
113 {
114 int target;
115 sync_dev(inode->i_rdev);
116
117 target = DEVICE_NR(MINOR(inode->i_rdev));
118
119 rscsi_disks[target].device->access_count--;
120 if (rscsi_disks[target].device->host->hostt->usage_count)
121 (*rscsi_disks[target].device->host->hostt->usage_count)--;
122
123 if(rscsi_disks[target].device->removable) {
124 if(!rscsi_disks[target].device->access_count)
125 sd_ioctl(inode, NULL, SCSI_IOCTL_DOORUNLOCK, 0);
126 };
127 }
128
129 static void sd_geninit(void);
130
131 static struct file_operations sd_fops = {
132 NULL,
133 block_read,
134 block_write,
135 NULL,
136 NULL,
137 sd_ioctl,
138 NULL,
139 sd_open,
140 sd_release,
141 block_fsync,
142 NULL,
143 check_scsidisk_media_change,
144 fop_revalidate_scsidisk
145 };
146
147 static struct gendisk sd_gendisk = {
148 MAJOR_NR,
149 "sd",
150 4,
151 1 << 4,
152 0,
153 sd_geninit,
154 NULL,
155 NULL,
156 0,
157 NULL,
158 NULL
159 };
160
161 static void sd_geninit (void)
162 {
163 int i;
164
165 for (i = 0; i < sd_template.dev_max; ++i)
166 if(rscsi_disks[i].device)
167 sd[i << 4].nr_sects = rscsi_disks[i].capacity;
168 #if 0
169
170 sd_gendisk.nr_real = sd_template.dev_max;
171 #endif
172 }
173
174
175
176
177
178
179
180 static void rw_intr (Scsi_Cmnd *SCpnt)
181 {
182 int result = SCpnt->result;
183 int this_count = SCpnt->bufflen >> 9;
184
185 #ifdef DEBUG
186 printk("sd%c : rw_intr(%d, %d)\n", 'a' + MINOR(SCpnt->request.dev), SCpnt->host->host_no, result);
187 #endif
188
189
190
191
192
193
194
195 if (!result) {
196
197 #ifdef DEBUG
198 printk("sd%c : %d sectors remain.\n", 'a' + MINOR(SCpnt->request.dev), SCpnt->request.nr_sectors);
199 printk("use_sg is %d\n ",SCpnt->use_sg);
200 #endif
201 if (SCpnt->use_sg) {
202 struct scatterlist * sgpnt;
203 int i;
204 sgpnt = (struct scatterlist *) SCpnt->buffer;
205 for(i=0; i<SCpnt->use_sg; i++) {
206 #ifdef DEBUG
207 printk(":%x %x %d\n",sgpnt[i].alt_address, sgpnt[i].address, sgpnt[i].length);
208 #endif
209 if (sgpnt[i].alt_address) {
210 if (SCpnt->request.cmd == READ)
211 memcpy(sgpnt[i].alt_address, sgpnt[i].address, sgpnt[i].length);
212 scsi_free(sgpnt[i].address, sgpnt[i].length);
213 };
214 };
215 scsi_free(SCpnt->buffer, SCpnt->sglist_len);
216 } else {
217 if (SCpnt->buffer != SCpnt->request.buffer) {
218 #ifdef DEBUG
219 printk("nosg: %x %x %d\n",SCpnt->request.buffer, SCpnt->buffer,
220 SCpnt->bufflen);
221 #endif
222 if (SCpnt->request.cmd == READ)
223 memcpy(SCpnt->request.buffer, SCpnt->buffer,
224 SCpnt->bufflen);
225 scsi_free(SCpnt->buffer, SCpnt->bufflen);
226 };
227 };
228
229
230
231
232
233 if (SCpnt->request.nr_sectors > this_count)
234 {
235 SCpnt->request.errors = 0;
236
237 if (!SCpnt->request.bh)
238 {
239 #ifdef DEBUG
240 printk("sd%c : handling page request, no buffer\n",
241 'a' + MINOR(SCpnt->request.dev));
242 #endif
243
244
245
246
247 panic("sd.c: linked page request (%lx %x)",
248 SCpnt->request.sector, this_count);
249 }
250 }
251 SCpnt = end_scsi_request(SCpnt, 1, this_count);
252 requeue_sd_request(SCpnt);
253 return;
254 }
255
256
257 if (SCpnt->use_sg) {
258 struct scatterlist * sgpnt;
259 int i;
260 sgpnt = (struct scatterlist *) SCpnt->buffer;
261 for(i=0; i<SCpnt->use_sg; i++) {
262 #ifdef DEBUG
263 printk("err: %x %x %d\n",SCpnt->request.buffer, SCpnt->buffer,
264 SCpnt->bufflen);
265 #endif
266 if (sgpnt[i].alt_address) {
267 scsi_free(sgpnt[i].address, sgpnt[i].length);
268 };
269 };
270 scsi_free(SCpnt->buffer, SCpnt->sglist_len);
271 } else {
272 #ifdef DEBUG
273 printk("nosgerr: %x %x %d\n",SCpnt->request.buffer, SCpnt->buffer,
274 SCpnt->bufflen);
275 #endif
276 if (SCpnt->buffer != SCpnt->request.buffer)
277 scsi_free(SCpnt->buffer, SCpnt->bufflen);
278 };
279
280
281
282
283
284
285
286 if (driver_byte(result) != 0) {
287 if (suggestion(result) == SUGGEST_REMAP) {
288 #ifdef REMAP
289
290
291
292
293 if rscsi_disks[DEVICE_NR(SCpnt->request.dev)].remap
294 {
295 result = 0;
296 }
297 else
298
299 #endif
300 }
301
302 if ((SCpnt->sense_buffer[0] & 0x7f) == 0x70) {
303 if ((SCpnt->sense_buffer[2] & 0xf) == UNIT_ATTENTION) {
304 if(rscsi_disks[DEVICE_NR(SCpnt->request.dev)].device->removable) {
305
306
307
308 rscsi_disks[DEVICE_NR(SCpnt->request.dev)].device->changed = 1;
309 SCpnt = end_scsi_request(SCpnt, 0, this_count);
310 requeue_sd_request(SCpnt);
311 return;
312 }
313 }
314 }
315
316
317
318
319
320
321
322
323
324 if (SCpnt->sense_buffer[2] == ILLEGAL_REQUEST) {
325 if (rscsi_disks[DEVICE_NR(SCpnt->request.dev)].ten) {
326 rscsi_disks[DEVICE_NR(SCpnt->request.dev)].ten = 0;
327 requeue_sd_request(SCpnt);
328 result = 0;
329 } else {
330 }
331 }
332 }
333 if (result) {
334 printk("SCSI disk error : host %d id %d lun %d return code = %x\n",
335 rscsi_disks[DEVICE_NR(SCpnt->request.dev)].device->host->host_no,
336 rscsi_disks[DEVICE_NR(SCpnt->request.dev)].device->id,
337 rscsi_disks[DEVICE_NR(SCpnt->request.dev)].device->lun, result);
338
339 if (driver_byte(result) & DRIVER_SENSE)
340 print_sense("sd", SCpnt);
341 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.current_nr_sectors);
342 requeue_sd_request(SCpnt);
343 return;
344 }
345 }
346
347
348
349
350
351
352
353 static void do_sd_request (void)
354 {
355 Scsi_Cmnd * SCpnt = NULL;
356 struct request * req = NULL;
357 unsigned long flags;
358 int flag = 0;
359
360 while (1==1){
361 save_flags(flags);
362 cli();
363 if (CURRENT != NULL && CURRENT->dev == -1) {
364 restore_flags(flags);
365 return;
366 };
367
368 INIT_SCSI_REQUEST;
369
370
371
372
373
374
375
376
377
378
379
380
381
382 if (flag++ == 0)
383 SCpnt = allocate_device(&CURRENT,
384 rscsi_disks[DEVICE_NR(MINOR(CURRENT->dev))].device, 0);
385 else SCpnt = NULL;
386 restore_flags(flags);
387
388
389
390
391
392
393
394
395 if (!SCpnt && sd_template.nr_dev > 1){
396 struct request *req1;
397 req1 = NULL;
398 save_flags(flags);
399 cli();
400 req = CURRENT;
401 while(req){
402 SCpnt = request_queueable(req,
403 rscsi_disks[DEVICE_NR(MINOR(req->dev))].device);
404 if(SCpnt) break;
405 req1 = req;
406 req = req->next;
407 };
408 if (SCpnt && req->dev == -1) {
409 if (req == CURRENT)
410 CURRENT = CURRENT->next;
411 else
412 req1->next = req->next;
413 };
414 restore_flags(flags);
415 };
416
417 if (!SCpnt) return;
418
419
420 requeue_sd_request(SCpnt);
421 };
422 }
423
424 static void requeue_sd_request (Scsi_Cmnd * SCpnt)
425 {
426 int dev, block, this_count;
427 unsigned char cmd[10];
428 int bounce_size, contiguous;
429 int max_sg;
430 struct buffer_head * bh, *bhp;
431 char * buff, *bounce_buffer;
432
433 repeat:
434
435 if(!SCpnt || SCpnt->request.dev <= 0) {
436 do_sd_request();
437 return;
438 }
439
440 dev = MINOR(SCpnt->request.dev);
441 block = SCpnt->request.sector;
442 this_count = 0;
443
444 #ifdef DEBUG
445 printk("Doing sd request, dev = %d, block = %d\n", dev, block);
446 #endif
447
448 if (dev >= (sd_template.dev_max << 4) ||
449 !rscsi_disks[DEVICE_NR(dev)].device ||
450 block + SCpnt->request.nr_sectors > sd[dev].nr_sects)
451 {
452 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
453 goto repeat;
454 }
455
456 block += sd[dev].start_sect;
457 dev = DEVICE_NR(dev);
458
459 if (rscsi_disks[dev].device->changed)
460 {
461
462
463
464
465 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
466 goto repeat;
467 }
468
469 #ifdef DEBUG
470 printk("sd%c : real dev = /dev/sd%c, block = %d\n", 'a' + MINOR(SCpnt->request.dev), dev, block);
471 #endif
472
473
474
475
476
477
478
479
480
481
482
483
484 if (rscsi_disks[dev].sector_size == 1024)
485 if((block & 1) || (SCpnt->request.nr_sectors & 1)) {
486 printk("sd.c:Bad block number requested");
487 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
488 goto repeat;
489 }
490
491 switch (SCpnt->request.cmd)
492 {
493 case WRITE :
494 if (!rscsi_disks[dev].device->writeable)
495 {
496 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
497 goto repeat;
498 }
499 cmd[0] = WRITE_6;
500 break;
501 case READ :
502 cmd[0] = READ_6;
503 break;
504 default :
505 panic ("Unknown sd command %d\n", SCpnt->request.cmd);
506 }
507
508 SCpnt->this_count = 0;
509
510
511
512 contiguous = (!CLUSTERABLE_DEVICE(SCpnt) ? 0 :1);
513 bounce_buffer = NULL;
514 bounce_size = (SCpnt->request.nr_sectors << 9);
515
516
517
518
519
520
521 if (contiguous && SCpnt->request.bh &&
522 ((long) SCpnt->request.bh->b_data) + (SCpnt->request.nr_sectors << 9) - 1 >
523 ISA_DMA_THRESHOLD && SCpnt->host->unchecked_isa_dma) {
524 if(((long) SCpnt->request.bh->b_data) > ISA_DMA_THRESHOLD)
525 bounce_buffer = (char *) scsi_malloc(bounce_size);
526 if(!bounce_buffer) contiguous = 0;
527 };
528
529 if(contiguous && SCpnt->request.bh && SCpnt->request.bh->b_reqnext)
530 for(bh = SCpnt->request.bh, bhp = bh->b_reqnext; bhp; bh = bhp,
531 bhp = bhp->b_reqnext) {
532 if(!CONTIGUOUS_BUFFERS(bh,bhp)) {
533 if(bounce_buffer) scsi_free(bounce_buffer, bounce_size);
534 contiguous = 0;
535 break;
536 }
537 };
538 if (!SCpnt->request.bh || contiguous) {
539
540
541 this_count = SCpnt->request.nr_sectors;
542 buff = SCpnt->request.buffer;
543 SCpnt->use_sg = 0;
544
545 } else if (SCpnt->host->sg_tablesize == 0 ||
546 (need_isa_buffer &&
547 dma_free_sectors <= 10)) {
548
549
550
551
552
553
554
555
556 if (SCpnt->host->sg_tablesize != 0 &&
557 need_isa_buffer &&
558 dma_free_sectors <= 10)
559 printk("Warning: SCSI DMA buffer space running low. Using non scatter-gather I/O.\n");
560
561 this_count = SCpnt->request.current_nr_sectors;
562 buff = SCpnt->request.buffer;
563 SCpnt->use_sg = 0;
564
565 } else {
566
567
568 struct scatterlist * sgpnt;
569 int count, this_count_max;
570 int counted;
571
572 bh = SCpnt->request.bh;
573 this_count = 0;
574 this_count_max = (rscsi_disks[dev].ten ? 0xffff : 0xff);
575 count = 0;
576 bhp = NULL;
577 while(bh) {
578 if ((this_count + (bh->b_size >> 9)) > this_count_max) break;
579 if(!bhp || !CONTIGUOUS_BUFFERS(bhp,bh) ||
580 !CLUSTERABLE_DEVICE(SCpnt) ||
581 (SCpnt->host->unchecked_isa_dma &&
582 ((unsigned long) bh->b_data-1) == ISA_DMA_THRESHOLD)) {
583 if (count < SCpnt->host->sg_tablesize) count++;
584 else break;
585 };
586 this_count += (bh->b_size >> 9);
587 bhp = bh;
588 bh = bh->b_reqnext;
589 };
590 #if 0
591 if(SCpnt->host->unchecked_isa_dma &&
592 ((unsigned int) SCpnt->request.bh->b_data-1) == ISA_DMA_THRESHOLD) count--;
593 #endif
594 SCpnt->use_sg = count;
595 count = 512;
596 while( count < (SCpnt->use_sg * sizeof(struct scatterlist)))
597 count = count << 1;
598 SCpnt->sglist_len = count;
599 max_sg = count / sizeof(struct scatterlist);
600 if(SCpnt->host->sg_tablesize < max_sg) max_sg = SCpnt->host->sg_tablesize;
601 sgpnt = (struct scatterlist * ) scsi_malloc(count);
602 memset(sgpnt, 0, count);
603 if (!sgpnt) {
604 printk("Warning - running *really* short on DMA buffers\n");
605 SCpnt->use_sg = 0;
606 this_count = SCpnt->request.current_nr_sectors;
607 buff = SCpnt->request.buffer;
608 } else {
609 buff = (char *) sgpnt;
610 counted = 0;
611 for(count = 0, bh = SCpnt->request.bh, bhp = bh->b_reqnext;
612 count < SCpnt->use_sg && bh;
613 count++, bh = bhp) {
614
615 bhp = bh->b_reqnext;
616
617 if(!sgpnt[count].address) sgpnt[count].address = bh->b_data;
618 sgpnt[count].length += bh->b_size;
619 counted += bh->b_size >> 9;
620
621 if (((long) sgpnt[count].address) + sgpnt[count].length - 1 >
622 ISA_DMA_THRESHOLD && (SCpnt->host->unchecked_isa_dma) &&
623 !sgpnt[count].alt_address) {
624 sgpnt[count].alt_address = sgpnt[count].address;
625
626
627
628 if(dma_free_sectors < (sgpnt[count].length >> 9) + 10) {
629 sgpnt[count].address = NULL;
630 } else {
631 sgpnt[count].address = (char *) scsi_malloc(sgpnt[count].length);
632 };
633
634
635
636
637 if(sgpnt[count].address == NULL){
638 #if 0
639 printk("Warning: Running low on SCSI DMA buffers");
640
641 while(--count >= 0){
642 if(sgpnt[count].alt_address)
643 scsi_free(sgpnt[count].address, sgpnt[count].length);
644 };
645 this_count = SCpnt->request.current_nr_sectors;
646 buff = SCpnt->request.buffer;
647 SCpnt->use_sg = 0;
648 scsi_free(sgpnt, SCpnt->sglist_len);
649 #endif
650 SCpnt->use_sg = count;
651 this_count = counted -= bh->b_size >> 9;
652 break;
653 };
654
655 };
656
657
658
659
660
661 if(bhp && CONTIGUOUS_BUFFERS(bh,bhp) && CLUSTERABLE_DEVICE(SCpnt)) {
662 char * tmp;
663
664 if (((long) sgpnt[count].address) + sgpnt[count].length +
665 bhp->b_size - 1 > ISA_DMA_THRESHOLD &&
666 (SCpnt->host->unchecked_isa_dma) &&
667 !sgpnt[count].alt_address) continue;
668
669 if(!sgpnt[count].alt_address) {count--; continue; }
670 if(dma_free_sectors > 10)
671 tmp = (char *) scsi_malloc(sgpnt[count].length + bhp->b_size);
672 else {
673 tmp = NULL;
674 max_sg = SCpnt->use_sg;
675 };
676 if(tmp){
677 scsi_free(sgpnt[count].address, sgpnt[count].length);
678 sgpnt[count].address = tmp;
679 count--;
680 continue;
681 };
682
683
684
685
686 if (SCpnt->use_sg < max_sg) SCpnt->use_sg++;
687 };
688 };
689
690 this_count = counted;
691
692 if(count < SCpnt->use_sg || SCpnt->use_sg > SCpnt->host->sg_tablesize){
693 bh = SCpnt->request.bh;
694 printk("Use sg, count %d %x %d\n", SCpnt->use_sg, count, dma_free_sectors);
695 printk("maxsg = %x, counted = %d this_count = %d\n", max_sg, counted, this_count);
696 while(bh){
697 printk("[%p %lx] ", bh->b_data, bh->b_size);
698 bh = bh->b_reqnext;
699 };
700 if(SCpnt->use_sg < 16)
701 for(count=0; count<SCpnt->use_sg; count++)
702 printk("{%d:%p %p %d} ", count,
703 sgpnt[count].address,
704 sgpnt[count].alt_address,
705 sgpnt[count].length);
706 panic("Ooops");
707 };
708
709 if (SCpnt->request.cmd == WRITE)
710 for(count=0; count<SCpnt->use_sg; count++)
711 if(sgpnt[count].alt_address)
712 memcpy(sgpnt[count].address, sgpnt[count].alt_address,
713 sgpnt[count].length);
714 };
715 };
716
717
718
719 if(SCpnt->use_sg == 0){
720 if (((long) buff) + (this_count << 9) - 1 > ISA_DMA_THRESHOLD &&
721 (SCpnt->host->unchecked_isa_dma)) {
722 if(bounce_buffer)
723 buff = bounce_buffer;
724 else
725 buff = (char *) scsi_malloc(this_count << 9);
726 if(buff == NULL) {
727 this_count = SCpnt->request.current_nr_sectors;
728 buff = (char *) scsi_malloc(this_count << 9);
729 if(!buff) panic("Ran out of DMA buffers.");
730 };
731 if (SCpnt->request.cmd == WRITE)
732 memcpy(buff, (char *)SCpnt->request.buffer, this_count << 9);
733 };
734 };
735 #ifdef DEBUG
736 printk("sd%c : %s %d/%d 512 byte blocks.\n", 'a' + MINOR(SCpnt->request.dev),
737 (SCpnt->request.cmd == WRITE) ? "writing" : "reading",
738 this_count, SCpnt->request.nr_sectors);
739 #endif
740
741 cmd[1] = (SCpnt->lun << 5) & 0xe0;
742
743 if (rscsi_disks[dev].sector_size == 1024){
744 if(block & 1) panic("sd.c:Bad block number requested");
745 if(this_count & 1) panic("sd.c:Bad block number requested");
746 block = block >> 1;
747 this_count = this_count >> 1;
748 };
749
750 if (rscsi_disks[dev].sector_size == 256){
751 block = block << 1;
752 this_count = this_count << 1;
753 };
754
755 if (((this_count > 0xff) || (block > 0x1fffff)) && rscsi_disks[dev].ten)
756 {
757 if (this_count > 0xffff)
758 this_count = 0xffff;
759
760 cmd[0] += READ_10 - READ_6 ;
761 cmd[2] = (unsigned char) (block >> 24) & 0xff;
762 cmd[3] = (unsigned char) (block >> 16) & 0xff;
763 cmd[4] = (unsigned char) (block >> 8) & 0xff;
764 cmd[5] = (unsigned char) block & 0xff;
765 cmd[6] = cmd[9] = 0;
766 cmd[7] = (unsigned char) (this_count >> 8) & 0xff;
767 cmd[8] = (unsigned char) this_count & 0xff;
768 }
769 else
770 {
771 if (this_count > 0xff)
772 this_count = 0xff;
773
774 cmd[1] |= (unsigned char) ((block >> 16) & 0x1f);
775 cmd[2] = (unsigned char) ((block >> 8) & 0xff);
776 cmd[3] = (unsigned char) block & 0xff;
777 cmd[4] = (unsigned char) this_count;
778 cmd[5] = 0;
779 }
780
781
782
783
784
785
786
787 SCpnt->transfersize = rscsi_disks[dev].sector_size;
788 SCpnt->underflow = this_count << 9;
789 scsi_do_cmd (SCpnt, (void *) cmd, buff,
790 this_count * rscsi_disks[dev].sector_size,
791 rw_intr,
792 (SCpnt->device->type == TYPE_DISK ?
793 SD_TIMEOUT : SD_MOD_TIMEOUT),
794 MAX_RETRIES);
795 }
796
797 static int check_scsidisk_media_change(dev_t full_dev){
798 int retval;
799 int target;
800 struct inode inode;
801 int flag = 0;
802
803 target = DEVICE_NR(MINOR(full_dev));
804
805 if (target >= sd_template.dev_max ||
806 !rscsi_disks[target].device) {
807 printk("SCSI disk request error: invalid device.\n");
808 return 0;
809 };
810
811 if(!rscsi_disks[target].device->removable) return 0;
812
813 inode.i_rdev = full_dev;
814 retval = sd_ioctl(&inode, NULL, SCSI_IOCTL_TEST_UNIT_READY, 0);
815
816 if(retval){
817
818
819
820
821 rscsi_disks[target].device->changed = 1;
822 return 1;
823
824 };
825
826 retval = rscsi_disks[target].device->changed;
827 if(!flag) rscsi_disks[target].device->changed = 0;
828 return retval;
829 }
830
831 static void sd_init_done (Scsi_Cmnd * SCpnt)
832 {
833 struct request * req;
834
835 req = &SCpnt->request;
836 req->dev = 0xfffe;
837
838 if (req->sem != NULL) {
839 up(req->sem);
840 }
841 }
842
843 static int sd_init_onedisk(int i)
844 {
845 unsigned char cmd[10];
846 unsigned char *buffer;
847 char spintime;
848 int the_result, retries;
849 Scsi_Cmnd * SCpnt;
850
851
852
853
854
855 SCpnt = allocate_device(NULL, rscsi_disks[i].device, 1);
856 buffer = (unsigned char *) scsi_malloc(512);
857
858 spintime = 0;
859
860
861 if (current == task[0]){
862 do{
863 cmd[0] = TEST_UNIT_READY;
864 cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
865 memset ((void *) &cmd[2], 0, 8);
866 SCpnt->request.dev = 0xffff;
867 SCpnt->cmd_len = 0;
868 SCpnt->sense_buffer[0] = 0;
869 SCpnt->sense_buffer[2] = 0;
870
871 scsi_do_cmd (SCpnt,
872 (void *) cmd, (void *) buffer,
873 512, sd_init_done, SD_TIMEOUT,
874 MAX_RETRIES);
875
876 while(SCpnt->request.dev != 0xfffe);
877
878 the_result = SCpnt->result;
879
880
881
882 if(the_result && !rscsi_disks[i].device->removable &&
883 SCpnt->sense_buffer[2] == NOT_READY) {
884 int time1;
885 if(!spintime){
886 printk( "sd%c: Spinning up disk...", 'a' + i );
887 cmd[0] = START_STOP;
888 cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
889 cmd[1] |= 1;
890 memset ((void *) &cmd[2], 0, 8);
891 cmd[4] = 1;
892 SCpnt->request.dev = 0xffff;
893 SCpnt->cmd_len = 0;
894 SCpnt->sense_buffer[0] = 0;
895 SCpnt->sense_buffer[2] = 0;
896
897 scsi_do_cmd (SCpnt,
898 (void *) cmd, (void *) buffer,
899 512, sd_init_done, SD_TIMEOUT,
900 MAX_RETRIES);
901
902 while(SCpnt->request.dev != 0xfffe);
903
904 spintime = jiffies;
905 };
906
907 time1 = jiffies;
908 while(jiffies < time1 + HZ);
909 printk( "." );
910 };
911 } while(the_result && spintime && spintime+5000 > jiffies);
912 if (spintime) {
913 if (the_result)
914 printk( "not responding...\n" );
915 else
916 printk( "ready\n" );
917 }
918 };
919
920
921 retries = 3;
922 do {
923 cmd[0] = READ_CAPACITY;
924 cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
925 memset ((void *) &cmd[2], 0, 8);
926 memset ((void *) buffer, 0, 8);
927 SCpnt->request.dev = 0xffff;
928 SCpnt->cmd_len = 0;
929 SCpnt->sense_buffer[0] = 0;
930 SCpnt->sense_buffer[2] = 0;
931
932 scsi_do_cmd (SCpnt,
933 (void *) cmd, (void *) buffer,
934 8, sd_init_done, SD_TIMEOUT,
935 MAX_RETRIES);
936
937 if (current == task[0])
938 while(SCpnt->request.dev != 0xfffe);
939 else
940 if (SCpnt->request.dev != 0xfffe){
941 struct semaphore sem = MUTEX_LOCKED;
942 SCpnt->request.sem = &sem;
943 down(&sem);
944
945 while (SCpnt->request.dev != 0xfffe) schedule();
946 };
947
948 the_result = SCpnt->result;
949 retries--;
950
951 } while(the_result && retries);
952
953 SCpnt->request.dev = -1;
954
955 wake_up(&SCpnt->device->device_wait);
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972 if (the_result)
973 {
974 printk ("sd%c : READ CAPACITY failed.\n"
975 "sd%c : status = %x, message = %02x, host = %d, driver = %02x \n",
976 'a' + i, 'a' + i,
977 status_byte(the_result),
978 msg_byte(the_result),
979 host_byte(the_result),
980 driver_byte(the_result)
981 );
982 if (driver_byte(the_result) & DRIVER_SENSE)
983 printk("sd%c : extended sense code = %1x \n", 'a' + i, SCpnt->sense_buffer[2] & 0xf);
984 else
985 printk("sd%c : sense not available. \n", 'a' + i);
986
987 printk("sd%c : block size assumed to be 512 bytes, disk size 1GB. \n", 'a' + i);
988 rscsi_disks[i].capacity = 0x1fffff;
989 rscsi_disks[i].sector_size = 512;
990
991
992
993 if(rscsi_disks[i].device->removable &&
994 SCpnt->sense_buffer[2] == NOT_READY)
995 rscsi_disks[i].device->changed = 1;
996
997 }
998 else
999 {
1000 rscsi_disks[i].capacity = (buffer[0] << 24) |
1001 (buffer[1] << 16) |
1002 (buffer[2] << 8) |
1003 buffer[3];
1004
1005 rscsi_disks[i].sector_size = (buffer[4] << 24) |
1006 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
1007
1008 if (rscsi_disks[i].sector_size != 512 &&
1009 rscsi_disks[i].sector_size != 1024 &&
1010 rscsi_disks[i].sector_size != 256)
1011 {
1012 printk ("sd%c : unsupported sector size %d.\n",
1013 'a' + i, rscsi_disks[i].sector_size);
1014 if(rscsi_disks[i].device->removable){
1015 rscsi_disks[i].capacity = 0;
1016 } else {
1017 printk ("scsi : deleting disk entry.\n");
1018 rscsi_disks[i].device = NULL;
1019 sd_template.nr_dev--;
1020 return i;
1021 };
1022 }
1023 {
1024
1025
1026
1027
1028
1029 int m;
1030 int hard_sector = rscsi_disks[i].sector_size;
1031
1032 for (m=i<<4; m<((i+1)<<4); m++){
1033 sd_hardsizes[m] = hard_sector;
1034 }
1035 printk ("SCSI Hardware sector size is %d bytes on device sd%c\n"
1036 ,hard_sector,i+'a');
1037 }
1038 if(rscsi_disks[i].sector_size == 1024)
1039 rscsi_disks[i].capacity <<= 1;
1040 if(rscsi_disks[i].sector_size == 256)
1041 rscsi_disks[i].capacity >>= 1;
1042 }
1043
1044 rscsi_disks[i].ten = 1;
1045 rscsi_disks[i].remap = 1;
1046 scsi_free(buffer, 512);
1047 return i;
1048 }
1049
1050
1051
1052
1053
1054
1055
1056 static void sd_init()
1057 {
1058 int i;
1059 static int sd_registered = 0;
1060
1061 if (sd_template.dev_noticed == 0) return;
1062
1063 if(!sd_registered) {
1064 if (register_blkdev(MAJOR_NR,"sd",&sd_fops)) {
1065 printk("Unable to get major %d for SCSI disk\n",MAJOR_NR);
1066 return;
1067 }
1068 sd_registered++;
1069 }
1070
1071
1072 if(rscsi_disks) return;
1073
1074 sd_template.dev_max = sd_template.dev_noticed + SD_EXTRA_DEVS;
1075
1076 rscsi_disks = (Scsi_Disk *)
1077 scsi_init_malloc(sd_template.dev_max * sizeof(Scsi_Disk), GFP_ATOMIC);
1078 memset(rscsi_disks, 0, sd_template.dev_max * sizeof(Scsi_Disk));
1079
1080 sd_sizes = (int *) scsi_init_malloc((sd_template.dev_max << 4) *
1081 sizeof(int), GFP_ATOMIC);
1082 memset(sd_sizes, 0, (sd_template.dev_max << 4) * sizeof(int));
1083
1084 sd_blocksizes = (int *) scsi_init_malloc((sd_template.dev_max << 4) *
1085 sizeof(int), GFP_ATOMIC);
1086
1087 sd_hardsizes = (int *) scsi_init_malloc((sd_template.dev_max << 4) *
1088 sizeof(struct hd_struct), GFP_ATOMIC);
1089
1090 for(i=0;i<(sd_template.dev_max << 4);i++){
1091 sd_blocksizes[i] = 1024;
1092 sd_hardsizes[i] = 512;
1093 }
1094 blksize_size[MAJOR_NR] = sd_blocksizes;
1095 hardsect_size[MAJOR_NR] = sd_hardsizes;
1096 sd = (struct hd_struct *) scsi_init_malloc((sd_template.dev_max << 4) *
1097 sizeof(struct hd_struct),
1098 GFP_ATOMIC);
1099
1100
1101 sd_gendisk.max_nr = sd_template.dev_max;
1102 sd_gendisk.part = sd;
1103 sd_gendisk.sizes = sd_sizes;
1104 sd_gendisk.real_devices = (void *) rscsi_disks;
1105
1106 }
1107
1108 static void sd_finish()
1109 {
1110 int i;
1111
1112 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1113
1114 sd_gendisk.next = gendisk_head;
1115 gendisk_head = &sd_gendisk;
1116
1117 for (i = 0; i < sd_template.dev_max; ++i)
1118 if (!rscsi_disks[i].capacity &&
1119 rscsi_disks[i].device)
1120 {
1121 i = sd_init_onedisk(i);
1122 if (scsi_loadable_module_flag
1123 && !rscsi_disks[i].has_part_table) {
1124 sd_sizes[i << 4] = rscsi_disks[i].capacity;
1125 revalidate_scsidisk(i << 4, 0);
1126 }
1127 rscsi_disks[i].has_part_table = 1;
1128 }
1129
1130
1131
1132
1133 if(rscsi_disks[0].device && rscsi_disks[0].device->host->sg_tablesize)
1134 read_ahead[MAJOR_NR] = 120;
1135
1136 else
1137 read_ahead[MAJOR_NR] = 4;
1138
1139 return;
1140 }
1141
1142 static int sd_detect(Scsi_Device * SDp){
1143 if(SDp->type != TYPE_DISK && SDp->type != TYPE_MOD) return 0;
1144
1145 printk("Detected scsi disk sd%c at scsi%d, id %d, lun %d\n",
1146 'a'+ (sd_template.dev_noticed++),
1147 SDp->host->host_no , SDp->id, SDp->lun);
1148
1149 return 1;
1150
1151 }
1152
1153 static int sd_attach(Scsi_Device * SDp){
1154 Scsi_Disk * dpnt;
1155 int i;
1156
1157 if(SDp->type != TYPE_DISK && SDp->type != TYPE_MOD) return 0;
1158
1159 if(sd_template.nr_dev >= sd_template.dev_max) {
1160 SDp->attached--;
1161 return 1;
1162 }
1163
1164 for(dpnt = rscsi_disks, i=0; i<sd_template.dev_max; i++, dpnt++)
1165 if(!dpnt->device) break;
1166
1167 if(i >= sd_template.dev_max) panic ("scsi_devices corrupt (sd)");
1168
1169 SDp->scsi_request_fn = do_sd_request;
1170 rscsi_disks[i].device = SDp;
1171 rscsi_disks[i].has_part_table = 0;
1172 sd_template.nr_dev++;
1173 sd_gendisk.nr_real++;
1174 return 0;
1175 }
1176
1177 #define DEVICE_BUSY rscsi_disks[target].device->busy
1178 #define USAGE rscsi_disks[target].device->access_count
1179 #define CAPACITY rscsi_disks[target].capacity
1180 #define MAYBE_REINIT sd_init_onedisk(target)
1181 #define GENDISK_STRUCT sd_gendisk
1182
1183
1184
1185
1186
1187
1188
1189
1190 int revalidate_scsidisk(int dev, int maxusage){
1191 int target, major;
1192 struct gendisk * gdev;
1193 unsigned long flags;
1194 int max_p;
1195 int start;
1196 int i;
1197
1198 target = DEVICE_NR(MINOR(dev));
1199 gdev = &GENDISK_STRUCT;
1200
1201 save_flags(flags);
1202 cli();
1203 if (DEVICE_BUSY || USAGE > maxusage) {
1204 restore_flags(flags);
1205 printk("Device busy for revalidation (usage=%d)\n", USAGE);
1206 return -EBUSY;
1207 };
1208 DEVICE_BUSY = 1;
1209 restore_flags(flags);
1210
1211 max_p = gdev->max_p;
1212 start = target << gdev->minor_shift;
1213 major = MAJOR_NR << 8;
1214
1215 for (i=max_p - 1; i >=0 ; i--) {
1216 sync_dev(major | start | i);
1217 invalidate_inodes(major | start | i);
1218 invalidate_buffers(major | start | i);
1219 gdev->part[start+i].start_sect = 0;
1220 gdev->part[start+i].nr_sects = 0;
1221 };
1222
1223 #ifdef MAYBE_REINIT
1224 MAYBE_REINIT;
1225 #endif
1226
1227 gdev->part[start].nr_sects = CAPACITY;
1228 resetup_one_dev(gdev, target);
1229
1230 DEVICE_BUSY = 0;
1231 return 0;
1232 }
1233
1234 static int fop_revalidate_scsidisk(dev_t dev){
1235 return revalidate_scsidisk(dev, 0);
1236 }
1237
1238
1239 static void sd_detach(Scsi_Device * SDp)
1240 {
1241 Scsi_Disk * dpnt;
1242 int i;
1243 int max_p;
1244 int major;
1245 int start;
1246
1247 for(dpnt = rscsi_disks, i=0; i<sd_template.dev_max; i++, dpnt++)
1248 if(dpnt->device == SDp) {
1249
1250
1251 max_p = sd_gendisk.max_p;
1252 start = i << sd_gendisk.minor_shift;
1253 major = MAJOR_NR << 8;
1254
1255 for (i=max_p - 1; i >=0 ; i--) {
1256 sync_dev(major | start | i);
1257 invalidate_inodes(major | start | i);
1258 invalidate_buffers(major | start | i);
1259 sd_gendisk.part[start+i].start_sect = 0;
1260 sd_gendisk.part[start+i].nr_sects = 0;
1261 sd_sizes[start+i] = 0;
1262 };
1263
1264 dpnt->has_part_table = 0;
1265 dpnt->device = NULL;
1266 dpnt->capacity = 0;
1267 SDp->attached--;
1268 sd_template.dev_noticed--;
1269 sd_template.nr_dev--;
1270 sd_gendisk.nr_real--;
1271 return;
1272 }
1273 return;
1274 }