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