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