2 * sd.c Copyright (C) 1992 Drew Eckhardt
3 * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
5 * Linux scsi disk driver
6 * Initial versions: Drew Eckhardt
7 * Subsequent revisions: Eric Youngdale
11 * Modified by Eric Youngdale ericy@andante.org to
12 * add scatter-gather, multiple outstanding request, and other
15 * Modified by Eric Youngdale eric@andante.org to support loadable
16 * low-level scsi drivers.
18 * Modified by Jirka Hanika geo@ff.cuni.cz to support more
19 * scsi disks using eight major numbers.
21 * Modified by Richard Gooch rgooch@atnf.csiro.au to support devfs.
24 #include <linux/config.h>
25 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/sched.h>
31 #include <linux/string.h>
32 #include <linux/hdreg.h>
33 #include <linux/errno.h>
34 #include <linux/interrupt.h>
35 #include <linux/init.h>
37 #include <linux/smp.h>
39 #include <asm/uaccess.h>
40 #include <asm/system.h>
43 #define MAJOR_NR SCSI_DISK0_MAJOR
44 #include <linux/blk.h>
45 #include <linux/blkpg.h>
49 #include <scsi/scsi_ioctl.h>
50 #include "constants.h"
51 #include <scsi/scsicam.h> /* must follow "hosts.h" */
53 #include <linux/genhd.h>
56 * static const char RCSid[] = "$Header:";
59 #define SD_MAJOR(i) (!(i) ? SCSI_DISK0_MAJOR : SCSI_DISK1_MAJOR-1+(i))
61 #define SCSI_DISKS_PER_MAJOR 16
62 #define SD_MAJOR_NUMBER(i) SD_MAJOR((i) >> 8)
63 #define SD_MINOR_NUMBER(i) ((i) & 255)
64 #define MKDEV_SD_PARTITION(i) MKDEV(SD_MAJOR_NUMBER(i), (i) & 255)
65 #define MKDEV_SD(index) MKDEV_SD_PARTITION((index) << 4)
66 #define N_USED_SCSI_DISKS (sd_template.dev_max + SCSI_DISKS_PER_MAJOR - 1)
67 #define N_USED_SD_MAJORS (N_USED_SCSI_DISKS / SCSI_DISKS_PER_MAJOR)
72 * Time out in seconds for disks and Magneto-opticals (which are slower).
75 #define SD_TIMEOUT (30 * HZ)
76 #define SD_MOD_TIMEOUT (75 * HZ)
80 static Scsi_Disk
*rscsi_disks
= NULL
;
82 static int *sd_blocksizes
;
83 static int *sd_hardsizes
; /* Hardware sector size */
85 static int check_scsidisk_media_change(kdev_t
);
86 static int fop_revalidate_scsidisk(kdev_t
);
88 static int sd_init_onedisk(int);
91 static int sd_init(void);
92 static void sd_finish(void);
93 static int sd_attach(Scsi_Device
*);
94 static int sd_detect(Scsi_Device
*);
95 static void sd_detach(Scsi_Device
*);
96 static void rw_intr(Scsi_Cmnd
* SCpnt
);
98 static int sd_init_command(Scsi_Cmnd
*);
100 #if defined(CONFIG_PPC)
102 * Moved from arch/ppc/pmac_setup.c. This is where it really belongs.
105 sd_find_target(void *host
, int tgt
)
109 for (dp
= rscsi_disks
, i
= 0; i
< sd_template
.dev_max
; ++i
, ++dp
)
110 if (dp
->device
!= NULL
&& dp
->device
->host
== host
111 && dp
->device
->id
== tgt
)
117 static int sd_ioctl(struct inode
* inode
, struct file
* file
, unsigned int cmd
, unsigned long arg
)
119 kdev_t dev
= inode
->i_rdev
;
120 struct Scsi_Host
* host
;
124 SDev
= rscsi_disks
[DEVICE_NR(dev
)].device
;
126 * If we are in the middle of error recovery, don't let anyone
127 * else try and use this device. Also, if error recovery fails, it
128 * may try and take the device offline, in which case all further
129 * access to the device is prohibited.
132 if( !scsi_block_when_processing_errors(SDev
) )
139 case HDIO_GETGEO
: /* Return BIOS disk parameters */
141 struct hd_geometry
*loc
= (struct hd_geometry
*) arg
;
145 host
= rscsi_disks
[DEVICE_NR(dev
)].device
->host
;
147 /* default to most commonly used values */
151 diskinfo
[2] = rscsi_disks
[DEVICE_NR(dev
)].capacity
>> 11;
153 /* override with calculated, extended default, or driver values */
155 if(host
->hostt
->bios_param
!= NULL
)
156 host
->hostt
->bios_param(&rscsi_disks
[DEVICE_NR(dev
)],
159 else scsicam_bios_param(&rscsi_disks
[DEVICE_NR(dev
)],
162 if (put_user(diskinfo
[0], &loc
->heads
) ||
163 put_user(diskinfo
[1], &loc
->sectors
) ||
164 put_user(diskinfo
[2], &loc
->cylinders
) ||
165 put_user(sd
[SD_PARTITION(inode
->i_rdev
)].start_sect
, &loc
->start
))
169 case HDIO_GETGEO_BIG
:
171 struct hd_big_geometry
*loc
= (struct hd_big_geometry
*) arg
;
176 host
= rscsi_disks
[DEVICE_NR(dev
)].device
->host
;
178 /* default to most commonly used values */
182 diskinfo
[2] = rscsi_disks
[DEVICE_NR(dev
)].capacity
>> 11;
184 /* override with calculated, extended default, or driver values */
186 if(host
->hostt
->bios_param
!= NULL
)
187 host
->hostt
->bios_param(&rscsi_disks
[DEVICE_NR(dev
)],
190 else scsicam_bios_param(&rscsi_disks
[DEVICE_NR(dev
)],
193 if (put_user(diskinfo
[0], &loc
->heads
) ||
194 put_user(diskinfo
[1], &loc
->sectors
) ||
195 put_user(diskinfo
[2], (unsigned int *) &loc
->cylinders
) ||
196 put_user(sd
[SD_PARTITION(inode
->i_rdev
)].start_sect
, &loc
->start
))
200 case BLKGETSIZE
: /* Return device size */
203 return put_user(sd
[SD_PARTITION(inode
->i_rdev
)].nr_sects
, (long *) arg
);
214 return blk_ioctl(inode
->i_rdev
, cmd
, arg
);
216 case BLKRRPART
: /* Re-read partition tables */
217 if (!capable(CAP_SYS_ADMIN
))
219 return revalidate_scsidisk(dev
, 1);
222 return scsi_ioctl(rscsi_disks
[DEVICE_NR(dev
)].device
, cmd
, (void *) arg
);
226 static void sd_devname(unsigned int disknum
, char *buffer
)
229 sprintf(buffer
, "sd%c", 'a' + disknum
);
234 * For larger numbers of disks, we need to go to a new
239 sprintf(buffer
, "sd%c%c", 'a' + min1
- 1, 'a' + min2
);
243 struct Scsi_Device_Template sd_template
= {
247 major
:SCSI_DISK0_MAJOR
,
249 * Secondary range of majors that this driver handles.
251 min_major
:SCSI_DISK1_MAJOR
,
252 max_major
:SCSI_DISK7_MAJOR
,
259 init_command
:sd_init_command
,
262 static request_queue_t
*sd_find_queue(kdev_t dev
)
266 target
= DEVICE_NR(dev
);
268 dpnt
= &rscsi_disks
[target
];
270 return NULL
; /* No such device */
271 return &dpnt
->device
->request_queue
;
274 static int sd_init_command(Scsi_Cmnd
* SCpnt
)
276 int dev
, devm
, block
, this_count
;
278 #if CONFIG_SCSI_LOGGING
282 devm
= SD_PARTITION(SCpnt
->request
.rq_dev
);
283 dev
= DEVICE_NR(SCpnt
->request
.rq_dev
);
285 block
= SCpnt
->request
.sector
;
286 this_count
= SCpnt
->request_bufflen
>> 9;
288 SCSI_LOG_HLQUEUE(1, printk("Doing sd request, dev = %d, block = %d\n", devm
, block
));
290 dpnt
= &rscsi_disks
[dev
];
291 if (devm
>= (sd_template
.dev_max
<< 4) ||
293 !dpnt
->device
->online
||
294 block
+ SCpnt
->request
.nr_sectors
> sd
[devm
].nr_sects
) {
295 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", SCpnt
->request
.nr_sectors
));
296 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt
));
299 block
+= sd
[devm
].start_sect
;
300 if (dpnt
->device
->changed
) {
302 * quietly refuse to do anything to a changed disc until the changed
305 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
308 SCSI_LOG_HLQUEUE(2, sd_devname(devm
, nbuff
));
309 SCSI_LOG_HLQUEUE(2, printk("%s : real dev = /dev/%d, block = %d\n",
313 * If we have a 1K hardware sectorsize, prevent access to single
314 * 512 byte sectors. In theory we could handle this - in fact
315 * the scsi cdrom driver must be able to handle this because
316 * we typically use 1K blocksizes, and cdroms typically have
317 * 2K hardware sectorsizes. Of course, things are simpler
318 * with the cdrom, since it is read-only. For performance
319 * reasons, the filesystems should be able to handle this
320 * and not force the scsi disk driver to use bounce buffers
323 if (dpnt
->device
->sector_size
== 1024) {
324 if ((block
& 1) || (SCpnt
->request
.nr_sectors
& 1)) {
325 printk("sd.c:Bad block number requested");
329 this_count
= this_count
>> 1;
332 if (dpnt
->device
->sector_size
== 2048) {
333 if ((block
& 3) || (SCpnt
->request
.nr_sectors
& 3)) {
334 printk("sd.c:Bad block number requested");
338 this_count
= this_count
>> 2;
341 if (dpnt
->device
->sector_size
== 4096) {
342 if ((block
& 7) || (SCpnt
->request
.nr_sectors
& 7)) {
343 printk("sd.c:Bad block number requested");
347 this_count
= this_count
>> 3;
350 switch (SCpnt
->request
.cmd
) {
352 if (!dpnt
->device
->writeable
) {
355 SCpnt
->cmnd
[0] = WRITE_6
;
356 SCpnt
->sc_data_direction
= SCSI_DATA_WRITE
;
359 SCpnt
->cmnd
[0] = READ_6
;
360 SCpnt
->sc_data_direction
= SCSI_DATA_READ
;
363 panic("Unknown sd command %d\n", SCpnt
->request
.cmd
);
366 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
368 (SCpnt
->request
.cmd
== WRITE
) ? "writing" : "reading",
369 this_count
, SCpnt
->request
.nr_sectors
));
371 SCpnt
->cmnd
[1] = (SCpnt
->lun
<< 5) & 0xe0;
373 if (((this_count
> 0xff) || (block
> 0x1fffff)) || SCpnt
->device
->ten
) {
374 if (this_count
> 0xffff)
377 SCpnt
->cmnd
[0] += READ_10
- READ_6
;
378 SCpnt
->cmnd
[2] = (unsigned char) (block
>> 24) & 0xff;
379 SCpnt
->cmnd
[3] = (unsigned char) (block
>> 16) & 0xff;
380 SCpnt
->cmnd
[4] = (unsigned char) (block
>> 8) & 0xff;
381 SCpnt
->cmnd
[5] = (unsigned char) block
& 0xff;
382 SCpnt
->cmnd
[6] = SCpnt
->cmnd
[9] = 0;
383 SCpnt
->cmnd
[7] = (unsigned char) (this_count
>> 8) & 0xff;
384 SCpnt
->cmnd
[8] = (unsigned char) this_count
& 0xff;
386 if (this_count
> 0xff)
389 SCpnt
->cmnd
[1] |= (unsigned char) ((block
>> 16) & 0x1f);
390 SCpnt
->cmnd
[2] = (unsigned char) ((block
>> 8) & 0xff);
391 SCpnt
->cmnd
[3] = (unsigned char) block
& 0xff;
392 SCpnt
->cmnd
[4] = (unsigned char) this_count
;
397 * We shouldn't disconnect in the middle of a sector, so with a dumb
398 * host adapter, it's safe to assume that we can at least transfer
399 * this many bytes between each connect / disconnect.
401 SCpnt
->transfersize
= dpnt
->device
->sector_size
;
402 SCpnt
->underflow
= this_count
<< 9;
404 SCpnt
->allowed
= MAX_RETRIES
;
405 SCpnt
->timeout_per_command
= (SCpnt
->device
->type
== TYPE_DISK
?
406 SD_TIMEOUT
: SD_MOD_TIMEOUT
);
409 * This is the completion routine we use. This is matched in terms
410 * of capability to this function.
412 SCpnt
->done
= rw_intr
;
415 * This indicates that the command is ready from our end to be
421 static int sd_open(struct inode
*inode
, struct file
*filp
)
425 target
= DEVICE_NR(inode
->i_rdev
);
427 SCSI_LOG_HLQUEUE(1, printk("target=%d, max=%d\n", target
, sd_template
.dev_max
));
429 if (target
>= sd_template
.dev_max
|| !rscsi_disks
[target
].device
)
430 return -ENXIO
; /* No such device */
433 * If the device is in error recovery, wait until it is done.
434 * If the device is offline, then disallow any access to it.
436 if (!scsi_block_when_processing_errors(rscsi_disks
[target
].device
)) {
440 * Make sure that only one process can do a check_change_disk at one time.
441 * This is also used to lock out further access when the partition table
445 while (rscsi_disks
[target
].device
->busy
)
447 if (rscsi_disks
[target
].device
->removable
) {
448 check_disk_change(inode
->i_rdev
);
451 * If the drive is empty, just let the open fail.
453 if (!rscsi_disks
[target
].ready
)
457 * Similarly, if the device has the write protect tab set,
458 * have the open fail if the user expects to be able to write
461 if ((rscsi_disks
[target
].write_prot
) && (filp
->f_mode
& 2))
464 SDev
= rscsi_disks
[target
].device
;
466 * It is possible that the disk changing stuff resulted in the device
467 * being taken offline. If this is the case, report this to the user,
468 * and don't pretend that
469 * the open actually succeeded.
475 * See if we are requesting a non-existent partition. Do this
476 * after checking for disk change.
478 if (sd_sizes
[SD_PARTITION(inode
->i_rdev
)] == 0)
482 if (!SDev
->access_count
)
483 if (scsi_block_when_processing_errors(SDev
))
484 scsi_ioctl(SDev
, SCSI_IOCTL_DOORLOCK
, NULL
);
486 SDev
->access_count
++;
487 if (SDev
->host
->hostt
->module
)
488 __MOD_INC_USE_COUNT(SDev
->host
->hostt
->module
);
489 if (sd_template
.module
)
490 __MOD_INC_USE_COUNT(sd_template
.module
);
494 static int sd_release(struct inode
*inode
, struct file
*file
)
499 target
= DEVICE_NR(inode
->i_rdev
);
500 SDev
= rscsi_disks
[target
].device
;
502 SDev
->access_count
--;
504 if (SDev
->removable
) {
505 if (!SDev
->access_count
)
506 if (scsi_block_when_processing_errors(SDev
))
507 scsi_ioctl(SDev
, SCSI_IOCTL_DOORUNLOCK
, NULL
);
509 if (SDev
->host
->hostt
->module
)
510 __MOD_DEC_USE_COUNT(SDev
->host
->hostt
->module
);
511 if (sd_template
.module
)
512 __MOD_DEC_USE_COUNT(sd_template
.module
);
516 static struct block_device_operations sd_fops
=
521 check_media_change
: check_scsidisk_media_change
,
522 revalidate
: fop_revalidate_scsidisk
526 * If we need more than one SCSI disk major (i.e. more than
527 * 16 SCSI disks), we'll have to kmalloc() more gendisks later.
530 static struct gendisk sd_gendisk
=
532 SCSI_DISK0_MAJOR
, /* Major number */
533 "sd", /* Major name */
534 4, /* Bits to shift to get real from partition */
535 1 << 4, /* Number of partitions per real */
536 NULL
, /* hd struct */
537 NULL
, /* block sizes */
541 &sd_fops
, /* file operations */
544 static struct gendisk
*sd_gendisks
= &sd_gendisk
;
546 #define SD_GENDISK(i) sd_gendisks[(i) / SCSI_DISKS_PER_MAJOR]
547 #define LAST_SD_GENDISK sd_gendisks[N_USED_SD_MAJORS - 1]
550 * rw_intr is the interrupt routine for the device driver.
551 * It will be notified on the end of a SCSI read / write, and
552 * will take one of several actions based on success or failure.
555 static void rw_intr(Scsi_Cmnd
* SCpnt
)
557 int result
= SCpnt
->result
;
558 #if CONFIG_SCSI_LOGGING
561 int this_count
= SCpnt
->bufflen
>> 9;
562 int good_sectors
= (result
== 0 ? this_count
: 0);
563 int block_sectors
= 1;
565 SCSI_LOG_HLCOMPLETE(1, sd_devname(DEVICE_NR(SCpnt
->request
.rq_dev
), nbuff
));
567 SCSI_LOG_HLCOMPLETE(1, printk("%s : rw_intr(%d, %x [%x %x])\n", nbuff
,
568 SCpnt
->host
->host_no
,
570 SCpnt
->sense_buffer
[0],
571 SCpnt
->sense_buffer
[2]));
574 Handle MEDIUM ERRORs that indicate partial success. Since this is a
575 relatively rare error condition, no care is taken to avoid
576 unnecessary additional work such as memcpy's that could be avoided.
579 /* An error occurred */
580 if (driver_byte(result
) != 0) {
581 /* Sense data is valid */
582 if (SCpnt
->sense_buffer
[0] == 0xF0 && SCpnt
->sense_buffer
[2] == MEDIUM_ERROR
) {
583 long error_sector
= (SCpnt
->sense_buffer
[3] << 24) |
584 (SCpnt
->sense_buffer
[4] << 16) |
585 (SCpnt
->sense_buffer
[5] << 8) |
586 SCpnt
->sense_buffer
[6];
587 if (SCpnt
->request
.bh
!= NULL
)
588 block_sectors
= SCpnt
->request
.bh
->b_size
>> 9;
589 switch (SCpnt
->device
->sector_size
) {
592 if (block_sectors
< 2)
597 if (block_sectors
< 4)
602 if (block_sectors
< 8)
611 error_sector
-= sd
[SD_PARTITION(SCpnt
->request
.rq_dev
)].start_sect
;
612 error_sector
&= ~(block_sectors
- 1);
613 good_sectors
= error_sector
- SCpnt
->request
.sector
;
614 if (good_sectors
< 0 || good_sectors
>= this_count
)
617 if (SCpnt
->sense_buffer
[2] == ILLEGAL_REQUEST
) {
618 if (SCpnt
->device
->ten
== 1) {
619 if (SCpnt
->cmnd
[0] == READ_10
||
620 SCpnt
->cmnd
[0] == WRITE_10
)
621 SCpnt
->device
->ten
= 0;
626 * This calls the generic completion function, now that we know
627 * how many actual sectors finished, and how many sectors we need
628 * to say have failed.
630 scsi_io_completion(SCpnt
, good_sectors
, block_sectors
);
633 * requeue_sd_request() is the request handler function for the sd driver.
634 * Its function in life is to take block device requests, and translate
635 * them to SCSI commands.
639 static int check_scsidisk_media_change(kdev_t full_dev
)
646 target
= DEVICE_NR(full_dev
);
647 SDev
= rscsi_disks
[target
].device
;
649 if (target
>= sd_template
.dev_max
|| !SDev
) {
650 printk("SCSI disk request error: invalid device.\n");
653 if (!SDev
->removable
)
657 * If the device is offline, don't send any commands - just pretend as
658 * if the command failed. If the device ever comes back online, we
659 * can deal with it then. It is only because of unrecoverable errors
660 * that we would ever take a device offline in the first place.
662 if (SDev
->online
== FALSE
) {
663 rscsi_disks
[target
].ready
= 0;
665 return 1; /* This will force a flush, if called from
666 * check_disk_change */
669 /* Using Start/Stop enables differentiation between drive with
670 * no cartridge loaded - NOT READY, drive with changed cartridge -
671 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
672 * This also handles drives that auto spin down. eg iomega jaz 1GB
673 * as this will spin up the drive.
676 if (scsi_block_when_processing_errors(SDev
))
677 retval
= scsi_ioctl(SDev
, SCSI_IOCTL_START_UNIT
, NULL
);
679 if (retval
) { /* Unable to test, unit probably not ready.
680 * This usually means there is no disc in the
681 * drive. Mark as changed, and we will figure
682 * it out later once the drive is available
685 rscsi_disks
[target
].ready
= 0;
687 return 1; /* This will force a flush, if called from
688 * check_disk_change */
691 * for removable scsi disk ( FLOPTICAL ) we have to recognise the
692 * presence of disk in the drive. This is kept in the Scsi_Disk
693 * struct and tested at open ! Daniel Roche ( dan@lectra.fr )
696 rscsi_disks
[target
].ready
= 1; /* FLOPTICAL */
698 retval
= SDev
->changed
;
704 static int sd_init_onedisk(int i
)
706 unsigned char cmd
[10];
708 unsigned char *buffer
;
709 unsigned long spintime_value
= 0;
710 int the_result
, retries
, spintime
;
715 * Get the name of the disk, in case we need to log it somewhere.
717 sd_devname(i
, nbuff
);
720 * If the device is offline, don't try and read capacity or any of the other
723 if (rscsi_disks
[i
].device
->online
== FALSE
) {
726 /* We need to retry the READ_CAPACITY because a UNIT_ATTENTION is
727 * considered a fatal error, and many devices report such an error
728 * just after a scsi bus reset.
731 SRpnt
= scsi_allocate_request(rscsi_disks
[i
].device
);
733 buffer
= (unsigned char *) scsi_malloc(512);
737 /* Spin up drives, as required. Only do this at boot time */
738 /* Spinup needs to be done for module loads too. */
742 while (retries
< 3) {
743 cmd
[0] = TEST_UNIT_READY
;
744 cmd
[1] = (rscsi_disks
[i
].device
->lun
<< 5) & 0xe0;
745 memset((void *) &cmd
[2], 0, 8);
746 SRpnt
->sr_cmd_len
= 0;
747 SRpnt
->sr_sense_buffer
[0] = 0;
748 SRpnt
->sr_sense_buffer
[2] = 0;
749 SRpnt
->sr_data_direction
= SCSI_DATA_READ
;
751 scsi_wait_req (SRpnt
, (void *) cmd
, (void *) buffer
,
752 0/*512*/, SD_TIMEOUT
, MAX_RETRIES
);
754 the_result
= SRpnt
->sr_result
;
757 || SRpnt
->sr_sense_buffer
[2] != UNIT_ATTENTION
)
762 * If the drive has indicated to us that it doesn't have
763 * any media in it, don't bother with any of the rest of
767 && ((driver_byte(the_result
) & DRIVER_SENSE
) != 0)
768 && SRpnt
->sr_sense_buffer
[2] == UNIT_ATTENTION
769 && SRpnt
->sr_sense_buffer
[12] == 0x3A ) {
770 rscsi_disks
[i
].capacity
= 0x1fffff;
772 rscsi_disks
[i
].device
->changed
= 1;
773 rscsi_disks
[i
].ready
= 0;
777 /* Look for non-removable devices that return NOT_READY.
778 * Issue command to spin up drive for these cases. */
779 if (the_result
&& !rscsi_disks
[i
].device
->removable
&&
780 SRpnt
->sr_sense_buffer
[2] == NOT_READY
) {
783 printk("%s: Spinning up disk...", nbuff
);
785 cmd
[1] = (rscsi_disks
[i
].device
->lun
<< 5) & 0xe0;
786 cmd
[1] |= 1; /* Return immediately */
787 memset((void *) &cmd
[2], 0, 8);
788 cmd
[4] = 1; /* Start spin cycle */
789 SRpnt
->sr_cmd_len
= 0;
790 SRpnt
->sr_sense_buffer
[0] = 0;
791 SRpnt
->sr_sense_buffer
[2] = 0;
793 SRpnt
->sr_data_direction
= SCSI_DATA_READ
;
794 scsi_wait_req(SRpnt
, (void *) cmd
, (void *) buffer
,
795 0/*512*/, SD_TIMEOUT
, MAX_RETRIES
);
798 spintime_value
= jiffies
;
800 /* Wait 1 second for next try */
802 current
->state
= TASK_UNINTERRUPTIBLE
;
803 time1
= schedule_timeout(time1
);
807 } while (the_result
&& spintime
&& time_after(spintime_value
+ 100 * HZ
, jiffies
));
810 printk("not responding...\n");
816 cmd
[0] = READ_CAPACITY
;
817 cmd
[1] = (rscsi_disks
[i
].device
->lun
<< 5) & 0xe0;
818 memset((void *) &cmd
[2], 0, 8);
819 memset((void *) buffer
, 0, 8);
820 SRpnt
->sr_cmd_len
= 0;
821 SRpnt
->sr_sense_buffer
[0] = 0;
822 SRpnt
->sr_sense_buffer
[2] = 0;
824 SRpnt
->sr_data_direction
= SCSI_DATA_READ
;
825 scsi_wait_req(SRpnt
, (void *) cmd
, (void *) buffer
,
826 8, SD_TIMEOUT
, MAX_RETRIES
);
828 the_result
= SRpnt
->sr_result
;
831 } while (the_result
&& retries
);
834 * The SCSI standard says:
835 * "READ CAPACITY is necessary for self configuring software"
836 * While not mandatory, support of READ CAPACITY is strongly encouraged.
837 * We used to die if we couldn't successfully do a READ CAPACITY.
838 * But, now we go on about our way. The side effects of this are
840 * 1. We can't know block size with certainty. I have said "512 bytes
841 * is it" as this is most common.
843 * 2. Recovery from when some one attempts to read past the end of the
844 * raw device will be slower.
848 printk("%s : READ CAPACITY failed.\n"
849 "%s : status = %x, message = %02x, host = %d, driver = %02x \n",
851 status_byte(the_result
),
852 msg_byte(the_result
),
853 host_byte(the_result
),
854 driver_byte(the_result
)
856 if (driver_byte(the_result
) & DRIVER_SENSE
)
857 printk("%s : extended sense code = %1x \n",
858 nbuff
, SRpnt
->sr_sense_buffer
[2] & 0xf);
860 printk("%s : sense not available. \n", nbuff
);
862 printk("%s : block size assumed to be 512 bytes, disk size 1GB. \n",
864 rscsi_disks
[i
].capacity
= 0x1fffff;
867 /* Set dirty bit for removable devices if not ready - sometimes drives
868 * will not report this properly. */
869 if (rscsi_disks
[i
].device
->removable
&&
870 SRpnt
->sr_sense_buffer
[2] == NOT_READY
)
871 rscsi_disks
[i
].device
->changed
= 1;
875 * FLOPTICAL , if read_capa is ok , drive is assumed to be ready
877 rscsi_disks
[i
].ready
= 1;
879 rscsi_disks
[i
].capacity
= 1 + ((buffer
[0] << 24) |
884 sector_size
= (buffer
[4] << 24) |
885 (buffer
[5] << 16) | (buffer
[6] << 8) | buffer
[7];
887 if (sector_size
== 0) {
889 printk("%s : sector size 0 reported, assuming 512.\n", nbuff
);
891 if (sector_size
!= 512 &&
892 sector_size
!= 1024 &&
893 sector_size
!= 2048 &&
894 sector_size
!= 4096 &&
895 sector_size
!= 256) {
896 printk("%s : unsupported sector size %d.\n",
899 * The user might want to re-format the drive with
900 * a supported sectorsize. Once this happens, it
901 * would be relatively trivial to set the thing up.
902 * For this reason, we leave the thing in the table.
904 rscsi_disks
[i
].capacity
= 0;
906 if (sector_size
> 1024) {
910 * We must fix the sd_blocksizes and sd_hardsizes
911 * to allow us to read the partition tables.
912 * The disk reading code does not allow for reading
913 * of partial sectors.
915 for (m
= i
<< 4; m
< ((i
+ 1) << 4); m
++) {
916 sd_blocksizes
[m
] = sector_size
;
920 * The msdos fs needs to know the hardware sector size
921 * So I have created this table. See ll_rw_blk.c
922 * Jacques Gelinas (Jacques@solucorp.qc.ca)
926 int hard_sector
= sector_size
;
927 /* There are 16 minors allocated for each major device */
928 for (m
= i
<< 4; m
< ((i
+ 1) << 4); m
++) {
929 sd_hardsizes
[m
] = hard_sector
;
931 mb
= rscsi_disks
[i
].capacity
/ 1024 * hard_sector
/ 1024;
932 /* sz = div(m/100, 10); this seems to not be in the libr */
935 sz_rem
= m
- (10 * sz_quot
);
936 printk("SCSI device %s: hdwr sector= %d bytes."
937 " Sectors= %d [%d MB] [%d.%1d GB]\n",
938 nbuff
, hard_sector
, rscsi_disks
[i
].capacity
,
939 mb
, sz_quot
, sz_rem
);
941 if (sector_size
== 4096)
942 rscsi_disks
[i
].capacity
<<= 3;
943 if (sector_size
== 2048)
944 rscsi_disks
[i
].capacity
<<= 2; /* Change into 512 byte sectors */
945 if (sector_size
== 1024)
946 rscsi_disks
[i
].capacity
<<= 1; /* Change into 512 byte sectors */
947 if (sector_size
== 256)
948 rscsi_disks
[i
].capacity
>>= 1; /* Change into 512 byte sectors */
953 * Unless otherwise specified, this is not write protected.
955 rscsi_disks
[i
].write_prot
= 0;
956 if (rscsi_disks
[i
].device
->removable
&& rscsi_disks
[i
].ready
) {
960 * For removable scsi disk ( FLOPTICAL ) we have to recognise
961 * the Write Protect Flag. This flag is kept in the Scsi_Disk
962 * struct and tested at open !
963 * Daniel Roche ( dan@lectra.fr )
965 * Changed to get all pages (0x3f) rather than page 1 to
966 * get around devices which do not have a page 1. Since
967 * we're only interested in the header anyway, this should
969 * -- Matthew Dharm (mdharm-scsi@one-eyed-alien.net)
972 memset((void *) &cmd
[0], 0, 8);
974 cmd
[1] = (rscsi_disks
[i
].device
->lun
<< 5) & 0xe0;
975 cmd
[2] = 0x3f; /* Get all pages */
976 cmd
[4] = 8; /* But we only want the 8 byte header */
977 SRpnt
->sr_cmd_len
= 0;
978 SRpnt
->sr_sense_buffer
[0] = 0;
979 SRpnt
->sr_sense_buffer
[2] = 0;
981 /* same code as READCAPA !! */
982 SRpnt
->sr_data_direction
= SCSI_DATA_READ
;
983 scsi_wait_req(SRpnt
, (void *) cmd
, (void *) buffer
,
984 512, SD_TIMEOUT
, MAX_RETRIES
);
986 the_result
= SRpnt
->sr_result
;
989 printk("%s: test WP failed, assume Write Protected\n", nbuff
);
990 rscsi_disks
[i
].write_prot
= 1;
992 rscsi_disks
[i
].write_prot
= ((buffer
[2] & 0x80) != 0);
993 printk("%s: Write Protect is %s\n", nbuff
,
994 rscsi_disks
[i
].write_prot
? "on" : "off");
997 } /* check for write protect */
998 SRpnt
->sr_device
->ten
= 1;
999 SRpnt
->sr_device
->remap
= 1;
1000 SRpnt
->sr_device
->sector_size
= sector_size
;
1001 /* Wake up a process waiting for device */
1002 scsi_release_request(SRpnt
);
1005 scsi_free(buffer
, 512);
1010 * The sd_init() function looks at all SCSI drives present, determines
1011 * their size, and reads partition table entries for them.
1014 static int sd_registered
= 0;
1016 static int sd_init()
1020 if (sd_template
.dev_noticed
== 0)
1024 sd_template
.dev_max
= sd_template
.dev_noticed
+ SD_EXTRA_DEVS
;
1026 if (sd_template
.dev_max
> N_SD_MAJORS
* SCSI_DISKS_PER_MAJOR
)
1027 sd_template
.dev_max
= N_SD_MAJORS
* SCSI_DISKS_PER_MAJOR
;
1029 if (!sd_registered
) {
1030 for (i
= 0; i
<= (sd_template
.dev_max
- 1) / SCSI_DISKS_PER_MAJOR
; i
++) {
1031 if (devfs_register_blkdev(SD_MAJOR(i
), "sd", &sd_fops
)) {
1032 printk("Unable to get major %d for SCSI disk\n", SD_MAJOR(i
));
1038 /* We do not support attaching loadable devices yet. */
1042 rscsi_disks
= (Scsi_Disk
*)
1043 kmalloc(sd_template
.dev_max
* sizeof(Scsi_Disk
), GFP_ATOMIC
);
1044 memset(rscsi_disks
, 0, sd_template
.dev_max
* sizeof(Scsi_Disk
));
1046 /* for every (necessary) major: */
1047 sd_sizes
= (int *) kmalloc((sd_template
.dev_max
<< 4) * sizeof(int), GFP_ATOMIC
);
1048 memset(sd_sizes
, 0, (sd_template
.dev_max
<< 4) * sizeof(int));
1050 sd_blocksizes
= (int *) kmalloc((sd_template
.dev_max
<< 4) * sizeof(int), GFP_ATOMIC
);
1051 sd_hardsizes
= (int *) kmalloc((sd_template
.dev_max
<< 4) * sizeof(int), GFP_ATOMIC
);
1053 for (i
= 0; i
< sd_template
.dev_max
<< 4; i
++) {
1054 sd_blocksizes
[i
] = 1024;
1055 sd_hardsizes
[i
] = 512;
1058 for (i
= 0; i
< N_USED_SD_MAJORS
; i
++) {
1059 blksize_size
[SD_MAJOR(i
)] = sd_blocksizes
+ i
* (SCSI_DISKS_PER_MAJOR
<< 4);
1060 hardsect_size
[SD_MAJOR(i
)] = sd_hardsizes
+ i
* (SCSI_DISKS_PER_MAJOR
<< 4);
1062 sd
= (struct hd_struct
*) kmalloc((sd_template
.dev_max
<< 4) *
1063 sizeof(struct hd_struct
),
1065 memset(sd
, 0, (sd_template
.dev_max
<< 4) * sizeof(struct hd_struct
));
1067 if (N_USED_SD_MAJORS
> 1)
1068 sd_gendisks
= (struct gendisk
*)
1069 kmalloc(N_USED_SD_MAJORS
* sizeof(struct gendisk
), GFP_ATOMIC
);
1070 for (i
= 0; i
< N_USED_SD_MAJORS
; i
++) {
1071 sd_gendisks
[i
] = sd_gendisk
;
1072 sd_gendisks
[i
].de_arr
= kmalloc (SCSI_DISKS_PER_MAJOR
* sizeof *sd_gendisks
[i
].de_arr
,
1074 memset (sd_gendisks
[i
].de_arr
, 0,
1075 SCSI_DISKS_PER_MAJOR
* sizeof *sd_gendisks
[i
].de_arr
);
1076 sd_gendisks
[i
].flags
= kmalloc (SCSI_DISKS_PER_MAJOR
* sizeof *sd_gendisks
[i
].flags
,
1078 memset (sd_gendisks
[i
].flags
, 0,
1079 SCSI_DISKS_PER_MAJOR
* sizeof *sd_gendisks
[i
].flags
);
1080 sd_gendisks
[i
].major
= SD_MAJOR(i
);
1081 sd_gendisks
[i
].major_name
= "sd";
1082 sd_gendisks
[i
].minor_shift
= 4;
1083 sd_gendisks
[i
].max_p
= 1 << 4;
1084 sd_gendisks
[i
].part
= sd
+ (i
* SCSI_DISKS_PER_MAJOR
<< 4);
1085 sd_gendisks
[i
].sizes
= sd_sizes
+ (i
* SCSI_DISKS_PER_MAJOR
<< 4);
1086 sd_gendisks
[i
].nr_real
= 0;
1087 sd_gendisks
[i
].next
= sd_gendisks
+ i
+ 1;
1088 sd_gendisks
[i
].real_devices
=
1089 (void *) (rscsi_disks
+ i
* SCSI_DISKS_PER_MAJOR
);
1092 LAST_SD_GENDISK
.next
= NULL
;
1097 static void sd_finish()
1099 struct gendisk
*gendisk
;
1102 for (i
= 0; i
<= (sd_template
.dev_max
- 1) / SCSI_DISKS_PER_MAJOR
; i
++) {
1103 blk_dev
[SD_MAJOR(i
)].queue
= sd_find_queue
;
1105 for (gendisk
= gendisk_head
; gendisk
!= NULL
; gendisk
= gendisk
->next
)
1106 if (gendisk
== sd_gendisks
)
1108 if (gendisk
== NULL
) {
1109 LAST_SD_GENDISK
.next
= gendisk_head
;
1110 gendisk_head
= sd_gendisks
;
1112 for (i
= 0; i
< sd_template
.dev_max
; ++i
)
1113 if (!rscsi_disks
[i
].capacity
&& rscsi_disks
[i
].device
) {
1115 if (!rscsi_disks
[i
].has_part_table
) {
1116 sd_sizes
[i
<< 4] = rscsi_disks
[i
].capacity
;
1117 register_disk(&SD_GENDISK(i
), MKDEV_SD(i
),
1119 rscsi_disks
[i
].capacity
);
1120 rscsi_disks
[i
].has_part_table
= 1;
1123 /* If our host adapter is capable of scatter-gather, then we increase
1124 * the read-ahead to 60 blocks (120 sectors). If not, we use
1125 * a two block (4 sector) read ahead. We can only respect this with the
1126 * granularity of every 16 disks (one device major).
1128 for (i
= 0; i
< N_USED_SD_MAJORS
; i
++) {
1129 read_ahead
[SD_MAJOR(i
)] =
1130 (rscsi_disks
[i
* SCSI_DISKS_PER_MAJOR
].device
1131 && rscsi_disks
[i
* SCSI_DISKS_PER_MAJOR
].device
->host
->sg_tablesize
)
1132 ? 120 /* 120 sector read-ahead */
1133 : 4; /* 4 sector read-ahead */
1139 static int sd_detect(Scsi_Device
* SDp
)
1142 if (SDp
->type
!= TYPE_DISK
&& SDp
->type
!= TYPE_MOD
)
1145 sd_devname(sd_template
.dev_noticed
++, nbuff
);
1146 printk("Detected scsi %sdisk %s at scsi%d, channel %d, id %d, lun %d\n",
1147 SDp
->removable
? "removable " : "",
1149 SDp
->host
->host_no
, SDp
->channel
, SDp
->id
, SDp
->lun
);
1154 static int sd_attach(Scsi_Device
* SDp
)
1156 unsigned int devnum
;
1160 if (SDp
->type
!= TYPE_DISK
&& SDp
->type
!= TYPE_MOD
)
1163 if (sd_template
.nr_dev
>= sd_template
.dev_max
) {
1167 for (dpnt
= rscsi_disks
, i
= 0; i
< sd_template
.dev_max
; i
++, dpnt
++)
1171 if (i
>= sd_template
.dev_max
)
1172 panic("scsi_devices corrupt (sd)");
1174 rscsi_disks
[i
].device
= SDp
;
1175 rscsi_disks
[i
].has_part_table
= 0;
1176 sd_template
.nr_dev
++;
1177 SD_GENDISK(i
).nr_real
++;
1178 devnum
= i
% SCSI_DISKS_PER_MAJOR
;
1179 SD_GENDISK(i
).de_arr
[devnum
] = SDp
->de
;
1181 SD_GENDISK(i
).flags
[devnum
] |= GENHD_FL_REMOVABLE
;
1185 #define DEVICE_BUSY rscsi_disks[target].device->busy
1186 #define USAGE rscsi_disks[target].device->access_count
1187 #define CAPACITY rscsi_disks[target].capacity
1188 #define MAYBE_REINIT sd_init_onedisk(target)
1190 /* This routine is called to flush all partitions and partition tables
1191 * for a changed scsi disk, and then re-read the new partition table.
1192 * If we are revalidating a disk because of a media change, then we
1193 * enter with usage == 0. If we are using an ioctl, we automatically have
1194 * usage == 1 (we need an open channel to use an ioctl :-), so this
1197 int revalidate_scsidisk(kdev_t dev
, int maxusage
)
1204 target
= DEVICE_NR(dev
);
1206 if (DEVICE_BUSY
|| USAGE
> maxusage
) {
1207 printk("Device busy for revalidation (usage=%d)\n", USAGE
);
1212 max_p
= sd_gendisks
->max_p
;
1213 start
= target
<< sd_gendisks
->minor_shift
;
1215 for (i
= max_p
- 1; i
>= 0; i
--) {
1216 int index
= start
+ i
;
1217 kdev_t devi
= MKDEV_SD_PARTITION(index
);
1218 struct super_block
*sb
= get_super(devi
);
1221 invalidate_inodes(sb
);
1222 invalidate_buffers(devi
);
1223 sd_gendisks
->part
[index
].start_sect
= 0;
1224 sd_gendisks
->part
[index
].nr_sects
= 0;
1226 * Reset the blocksize for everything so that we can read
1227 * the partition table. Technically we will determine the
1228 * correct block size when we revalidate, but we do this just
1229 * to make sure that everything remains consistent.
1231 sd_blocksizes
[index
] = 1024;
1232 if (rscsi_disks
[target
].device
->sector_size
== 2048)
1233 sd_blocksizes
[index
] = 2048;
1235 sd_blocksizes
[index
] = 1024;
1242 grok_partitions(&SD_GENDISK(target
), target
% SCSI_DISKS_PER_MAJOR
,
1249 static int fop_revalidate_scsidisk(kdev_t dev
)
1251 return revalidate_scsidisk(dev
, 0);
1253 static void sd_detach(Scsi_Device
* SDp
)
1260 for (dpnt
= rscsi_disks
, i
= 0; i
< sd_template
.dev_max
; i
++, dpnt
++)
1261 if (dpnt
->device
== SDp
) {
1263 /* If we are disconnecting a disk driver, sync and invalidate
1265 max_p
= sd_gendisk
.max_p
;
1266 start
= i
<< sd_gendisk
.minor_shift
;
1268 for (j
= max_p
- 1; j
>= 0; j
--) {
1269 int index
= start
+ j
;
1270 kdev_t devi
= MKDEV_SD_PARTITION(index
);
1271 struct super_block
*sb
= get_super(devi
);
1274 invalidate_inodes(sb
);
1275 invalidate_buffers(devi
);
1276 sd_gendisks
->part
[index
].start_sect
= 0;
1277 sd_gendisks
->part
[index
].nr_sects
= 0;
1278 sd_sizes
[index
] = 0;
1280 devfs_register_partitions (&SD_GENDISK (i
),
1281 SD_MINOR_NUMBER (start
), 1);
1282 /* unregister_disk() */
1283 dpnt
->has_part_table
= 0;
1284 dpnt
->device
= NULL
;
1287 sd_template
.dev_noticed
--;
1288 sd_template
.nr_dev
--;
1289 SD_GENDISK(i
).nr_real
--;
1297 int init_module(void)
1299 sd_template
.module
= &__this_module
;
1300 return scsi_register_module(MODULE_SCSI_DEV
, &sd_template
);
1302 void cleanup_module(void)
1304 struct gendisk
**prev_sdgd_link
;
1305 struct gendisk
*sdgd
;
1309 scsi_unregister_module(MODULE_SCSI_DEV
, &sd_template
);
1311 for (i
= 0; i
<= (sd_template
.dev_max
- 1) / SCSI_DISKS_PER_MAJOR
; i
++)
1312 devfs_unregister_blkdev(SD_MAJOR(i
), "sd");
1315 if (rscsi_disks
!= NULL
) {
1316 kfree((char *) rscsi_disks
);
1317 kfree((char *) sd_sizes
);
1318 kfree((char *) sd_blocksizes
);
1319 kfree((char *) sd_hardsizes
);
1323 * Now remove sd_gendisks from the linked list
1325 prev_sdgd_link
= &gendisk_head
;
1326 while ((sdgd
= *prev_sdgd_link
) != NULL
) {
1327 if (sdgd
>= sd_gendisks
&& sdgd
<= &LAST_SD_GENDISK
) {
1329 *prev_sdgd_link
= sdgd
->next
;
1332 prev_sdgd_link
= &sdgd
->next
;
1335 if (removed
!= N_USED_SD_MAJORS
)
1336 printk("%s %d sd_gendisks in disk chain",
1337 removed
> N_USED_SD_MAJORS
? "total" : "just", removed
);
1340 for (i
= 0; i
<= (sd_template
.dev_max
- 1) / SCSI_DISKS_PER_MAJOR
; i
++) {
1341 blk_size
[SD_MAJOR(i
)] = NULL
;
1342 hardsect_size
[SD_MAJOR(i
)] = NULL
;
1343 read_ahead
[SD_MAJOR(i
)] = 0;
1345 sd_template
.dev_max
= 0;
1346 if (sd_gendisks
!= &sd_gendisk
)
1352 * Overrides for Emacs so that we almost follow Linus's tabbing style.
1353 * Emacs will notice this stuff at the end of the file and automatically
1354 * adjust the settings for this buffer only. This must remain at the end
1356 * ---------------------------------------------------------------------------
1359 * c-brace-imaginary-offset: 0
1360 * c-brace-offset: -4
1361 * c-argdecl-indent: 4
1362 * c-label-offset: -4
1363 * c-continued-statement-offset: 4
1364 * c-continued-brace-offset: 0
1365 * indent-tabs-mode: nil