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.
23
* Modified by Torben Mathiasen tmm@image.dk
24
* Resource allocation fixes in sd_init and cleanups.
26
* Modified by Alex Davis <letmein@erols.com>
27
* Fix problem where partition info not being read in sd_open.
29
* Modified by Alex Davis <letmein@erols.com>
30
* Fix problem where removable media could be ejected after sd_open.
33
#include <xeno/config.h>
34
#include <xeno/module.h>
35
#include <xeno/sched.h>
36
#include <xeno/hdreg.h>
37
#include <xeno/init.h>
39
#include <asm/uaccess.h>
40
#include <asm/system.h>
43
#define MAJOR_NR SCSI_DISK0_MAJOR
45
#include <xeno/blkpg.h>
49
#include <scsi/scsi_ioctl.h>
50
#include "constants.h"
51
#include <scsi/scsicam.h> /* must follow "hosts.h" */
53
#include <xeno/genhd.h>
55
#include <asm/domain_page.h> /* SMH: for [un_]map_domain_mem() */
58
* static const char RCSid[] = "$Header:";
61
/* system major --> sd_gendisks index */
62
#define SD_MAJOR_IDX(i) (MAJOR(i) & SD_MAJOR_MASK)
63
/* sd_gendisks index --> system major */
64
#define SD_MAJOR(i) (!(i) ? SCSI_DISK0_MAJOR : SCSI_DISK1_MAJOR-1+(i))
66
#define SD_PARTITION(dev) ((SD_MAJOR_IDX(dev) << 8) | (MINOR(dev) & 255))
68
#define SCSI_DISKS_PER_MAJOR 16
69
#define SD_MAJOR_NUMBER(i) SD_MAJOR((i) >> 8)
70
#define SD_MINOR_NUMBER(i) ((i) & 255)
71
#define MKDEV_SD_PARTITION(i) MKDEV(SD_MAJOR_NUMBER(i), (i) & 255)
72
#define MKDEV_SD(index) MKDEV_SD_PARTITION((index) << 4)
73
#define N_USED_SCSI_DISKS (sd_template.dev_max + SCSI_DISKS_PER_MAJOR - 1)
74
#define N_USED_SD_MAJORS (N_USED_SCSI_DISKS / SCSI_DISKS_PER_MAJOR)
79
* Time out in seconds for disks and Magneto-opticals (which are slower).
82
#define SD_TIMEOUT (30 * HZ)
83
#define SD_MOD_TIMEOUT (75 * HZ)
85
static Scsi_Disk *rscsi_disks;
86
static struct gendisk *sd_gendisks;
88
static int *sd_blocksizes;
89
static int *sd_hardsizes; /* Hardware sector size */
90
static int *sd_max_sectors;
92
static int check_scsidisk_media_change(kdev_t);
93
static int fop_revalidate_scsidisk(kdev_t);
95
static int sd_init_onedisk(int);
98
static int sd_init(void);
99
static void sd_finish(void);
100
static int sd_attach(Scsi_Device *);
101
static int sd_detect(Scsi_Device *);
102
static void sd_detach(Scsi_Device *);
103
static int sd_init_command(Scsi_Cmnd *);
105
static struct Scsi_Device_Template sd_template = {
109
major:SCSI_DISK0_MAJOR,
111
* Secondary range of majors that this driver handles.
113
min_major:SCSI_DISK1_MAJOR,
114
max_major:SCSI_DISK7_MAJOR,
121
init_command:sd_init_command,
125
static void rw_intr(Scsi_Cmnd * SCpnt);
127
#if defined(CONFIG_PPC)
129
* Moved from arch/ppc/pmac_setup.c. This is where it really belongs.
132
sd_find_target(void *host, int tgt)
136
for (dp = rscsi_disks, i = 0; i < sd_template.dev_max; ++i, ++dp)
137
if (dp->device != NULL && dp->device->host == host
138
&& dp->device->id == tgt)
144
static int sd_ioctl(struct inode * inode, struct file * file, unsigned int cmd, unsigned long arg)
146
kdev_t dev = inode->i_rdev;
147
struct Scsi_Host * host;
151
SDev = rscsi_disks[DEVICE_NR(dev)].device;
156
* If we are in the middle of error recovery, don't let anyone
157
* else try and use this device. Also, if error recovery fails, it
158
* may try and take the device offline, in which case all further
159
* access to the device is prohibited.
162
if( !scsi_block_when_processing_errors(SDev) )
169
case HDIO_GETGEO: /* Return BIOS disk parameters */
171
struct hd_geometry *loc = (struct hd_geometry *) arg;
175
host = rscsi_disks[DEVICE_NR(dev)].device->host;
177
/* default to most commonly used values */
182
rscsi_disks[DEVICE_NR(dev)].capacity >> 11;
184
/* override with calculated, extended default,
187
if(host->hostt->bios_param != NULL)
188
host->hostt->bios_param(
189
&rscsi_disks[DEVICE_NR(dev)], dev,
191
else scsicam_bios_param(&rscsi_disks[DEVICE_NR(dev)],
194
if (put_user(diskinfo[0], &loc->heads) ||
195
put_user(diskinfo[1], &loc->sectors) ||
196
put_user(diskinfo[2], &loc->cylinders) ||
197
put_user(sd_gendisks[SD_MAJOR_IDX(
198
inode->i_rdev)].part[MINOR(
199
inode->i_rdev)].start_sect, &loc->start))
203
case HDIO_GETGEO_BIG:
205
struct hd_big_geometry *loc =
206
(struct hd_big_geometry *) arg;
211
host = rscsi_disks[DEVICE_NR(dev)].device->host;
213
/* default to most commonly used values */
218
rscsi_disks[DEVICE_NR(dev)].capacity >> 11;
220
/* override with calculated, extended default,
223
if(host->hostt->bios_param != NULL)
224
host->hostt->bios_param(
225
&rscsi_disks[DEVICE_NR(dev)], dev,
227
else scsicam_bios_param(&rscsi_disks[DEVICE_NR(dev)],
230
if (put_user(diskinfo[0], &loc->heads) ||
231
put_user(diskinfo[1], &loc->sectors) ||
232
put_user(diskinfo[2],
233
(unsigned int *) &loc->cylinders) ||
234
put_user(sd_gendisks[SD_MAJOR_IDX(
235
inode->i_rdev)].part[MINOR(
236
inode->i_rdev)].start_sect, &loc->start))
254
return blk_ioctl(inode->i_rdev, cmd, arg);
256
case BLKRRPART: /* Re-read partition tables */
257
if (!capable(CAP_SYS_ADMIN))
259
return revalidate_scsidisk(dev, 1);
263
return scsi_ioctl(rscsi_disks[DEVICE_NR(dev)].device,
268
static void sd_devname(unsigned int disknum, char *buffer)
271
sprintf(buffer, "sd%c", 'a' + disknum);
276
* For larger numbers of disks, we need to go to a new
281
sprintf(buffer, "sd%c%c", 'a' + min1 - 1, 'a' + min2);
285
static request_queue_t *sd_find_queue(kdev_t dev)
289
target = DEVICE_NR(dev);
291
dpnt = &rscsi_disks[target];
293
return NULL; /* No such device */
294
return &dpnt->device->request_queue;
297
static int sd_init_command(Scsi_Cmnd * SCpnt)
299
int dev, block, this_count;
300
struct hd_struct *ppnt;
302
#if CONFIG_SCSI_LOGGING
306
ppnt = &sd_gendisks[SD_MAJOR_IDX(SCpnt->request.rq_dev)].part[MINOR(SCpnt->request.rq_dev)];
307
dev = DEVICE_NR(SCpnt->request.rq_dev);
309
block = SCpnt->request.sector;
310
this_count = SCpnt->request_bufflen >> 9;
312
SCSI_LOG_HLQUEUE(1, printk("Doing sd request, dev = 0x%x, block = %d\n",
313
SCpnt->request.rq_dev, block));
315
dpnt = &rscsi_disks[dev];
316
if (dev >= sd_template.dev_max ||
318
!dpnt->device->online ||
319
block + SCpnt->request.nr_sectors > ppnt->nr_sects) {
321
SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
322
SCpnt->request.nr_sectors));
323
SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
326
block += ppnt->start_sect;
327
if (dpnt->device->changed) {
329
* quietly refuse to do anything to a changed disc until the changed
332
/* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
335
SCSI_LOG_HLQUEUE(2, sd_devname(dev, nbuff));
336
SCSI_LOG_HLQUEUE(2, printk("%s : real dev = /dev/%d, block = %d\n",
340
* If we have a 1K hardware sectorsize, prevent access to single
341
* 512 byte sectors. In theory we could handle this - in fact
342
* the scsi cdrom driver must be able to handle this because
343
* we typically use 1K blocksizes, and cdroms typically have
344
* 2K hardware sectorsizes. Of course, things are simpler
345
* with the cdrom, since it is read-only. For performance
346
* reasons, the filesystems should be able to handle this
347
* and not force the scsi disk driver to use bounce buffers
350
if (dpnt->device->sector_size == 1024) {
351
if ((block & 1) || (SCpnt->request.nr_sectors & 1)) {
352
printk("sd.c:Bad block number requested");
356
this_count = this_count >> 1;
359
if (dpnt->device->sector_size == 2048) {
360
if ((block & 3) || (SCpnt->request.nr_sectors & 3)) {
361
printk("sd.c:Bad block number requested");
365
this_count = this_count >> 2;
368
if (dpnt->device->sector_size == 4096) {
369
if ((block & 7) || (SCpnt->request.nr_sectors & 7)) {
370
printk("sd.c:Bad block number requested");
374
this_count = this_count >> 3;
377
switch (SCpnt->request.cmd) {
379
if (!dpnt->device->writeable) {
382
SCpnt->cmnd[0] = WRITE_6;
383
SCpnt->sc_data_direction = SCSI_DATA_WRITE;
386
SCpnt->cmnd[0] = READ_6;
387
SCpnt->sc_data_direction = SCSI_DATA_READ;
390
panic("Unknown sd command %d\n", SCpnt->request.cmd);
393
SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n", nbuff,
394
(SCpnt->request.cmd == WRITE) ? "writing" :
395
"reading", this_count,
396
SCpnt->request.nr_sectors));
398
SCpnt->cmnd[1] = (SCpnt->device->scsi_level <= SCSI_2) ?
399
((SCpnt->lun << 5) & 0xe0) : 0;
401
if (((this_count > 0xff) || (block > 0x1fffff)) || SCpnt->device->ten) {
402
if (this_count > 0xffff)
405
SCpnt->cmnd[0] += READ_10 - READ_6;
406
SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
407
SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
408
SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
409
SCpnt->cmnd[5] = (unsigned char) block & 0xff;
410
SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
411
SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
412
SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
414
if (this_count > 0xff)
417
SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
418
SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
419
SCpnt->cmnd[3] = (unsigned char) block & 0xff;
420
SCpnt->cmnd[4] = (unsigned char) this_count;
425
* We shouldn't disconnect in the middle of a sector, so with a dumb
426
* host adapter, it's safe to assume that we can at least transfer
427
* this many bytes between each connect / disconnect.
429
SCpnt->transfersize = dpnt->device->sector_size;
430
SCpnt->underflow = this_count << 9;
432
SCpnt->allowed = MAX_RETRIES;
433
SCpnt->timeout_per_command = (SCpnt->device->type == TYPE_DISK ?
434
SD_TIMEOUT : SD_MOD_TIMEOUT);
437
* This is the completion routine we use. This is matched in terms
438
* of capability to this function.
440
SCpnt->done = rw_intr;
443
* This indicates that the command is ready from our end to be
449
static int sd_open(struct inode *inode, struct file *filp)
451
int target, retval = -ENXIO;
453
target = DEVICE_NR(inode->i_rdev);
455
SCSI_LOG_HLQUEUE(1, printk("target=%d, max=%d\n", target, sd_template.dev_max));
457
if (target >= sd_template.dev_max || !rscsi_disks[target].device)
458
return -ENXIO; /* No such device */
461
* If the device is in error recovery, wait until it is done.
462
* If the device is offline, then disallow any access to it.
464
if (!scsi_block_when_processing_errors(rscsi_disks[target].device)) {
468
* Make sure that only one process can do a check_change_disk at one time.
469
* This is also used to lock out further access when the partition table
473
while (rscsi_disks[target].device->busy) {
478
* The following code can sleep.
479
* Module unloading must be prevented
481
SDev = rscsi_disks[target].device;
482
if (SDev->host->hostt->module)
483
__MOD_INC_USE_COUNT(SDev->host->hostt->module);
484
if (sd_template.module)
485
__MOD_INC_USE_COUNT(sd_template.module);
486
SDev->access_count++;
489
if (rscsi_disks[target].device->removable) {
490
SDev->allow_revalidate = 1;
491
check_disk_change(inode->i_rdev);
492
SDev->allow_revalidate = 0;
496
* If the drive is empty, just let the open fail.
498
if ((!rscsi_disks[target].ready) && !(filp->f_flags & O_NDELAY)) {
504
* Similarly, if the device has the write protect tab set,
505
* have the open fail if the user expects to be able to write
508
if ((rscsi_disks[target].write_prot) && (filp->f_mode & 2)) {
516
* It is possible that the disk changing stuff resulted in the device
517
* being taken offline. If this is the case, report this to the user,
518
* and don't pretend that
519
* the open actually succeeded.
525
* See if we are requesting a non-existent partition. Do this
526
* after checking for disk change.
528
if (sd_sizes[SD_PARTITION(inode->i_rdev)] == 0) {
533
if (SDev->access_count==1)
534
if (scsi_block_when_processing_errors(SDev))
535
scsi_ioctl(SDev, SCSI_IOCTL_DOORLOCK, NULL);
541
SDev->access_count--;
542
if (SDev->host->hostt->module)
543
__MOD_DEC_USE_COUNT(SDev->host->hostt->module);
544
if (sd_template.module)
545
__MOD_DEC_USE_COUNT(sd_template.module);
549
static int sd_release(struct inode *inode, struct file *file)
554
target = DEVICE_NR(inode->i_rdev);
555
SDev = rscsi_disks[target].device;
559
SDev->access_count--;
561
if (SDev->removable) {
562
if (!SDev->access_count)
563
if (scsi_block_when_processing_errors(SDev))
564
scsi_ioctl(SDev, SCSI_IOCTL_DOORUNLOCK, NULL);
566
if (SDev->host->hostt->module)
567
__MOD_DEC_USE_COUNT(SDev->host->hostt->module);
568
if (sd_template.module)
569
__MOD_DEC_USE_COUNT(sd_template.module);
573
static struct block_device_operations sd_fops =
575
/* owner: THIS_MODULE, */
579
check_media_change: check_scsidisk_media_change,
580
revalidate: fop_revalidate_scsidisk
584
* If we need more than one SCSI disk major (i.e. more than
585
* 16 SCSI disks), we'll have to kmalloc() more gendisks later.
588
static struct gendisk sd_gendisk =
590
major: SCSI_DISK0_MAJOR,
597
#define SD_GENDISK(i) sd_gendisks[(i) / SCSI_DISKS_PER_MAJOR]
600
* rw_intr is the interrupt routine for the device driver.
601
* It will be notified on the end of a SCSI read / write, and
602
* will take one of several actions based on success or failure.
605
static void rw_intr(Scsi_Cmnd * SCpnt)
607
int result = SCpnt->result;
608
#if CONFIG_SCSI_LOGGING
611
int this_count = SCpnt->bufflen >> 9;
612
int good_sectors = (result == 0 ? this_count : 0);
613
int block_sectors = 1;
616
SCSI_LOG_HLCOMPLETE(1, sd_devname(DEVICE_NR(SCpnt->request.rq_dev),
619
SCSI_LOG_HLCOMPLETE(1, printk("%s : rw_intr(%d, %x [%x %x])\n", nbuff,
620
SCpnt->host->host_no,
622
SCpnt->sense_buffer[0],
623
SCpnt->sense_buffer[2]));
626
Handle MEDIUM ERRORs that indicate partial success. Since this is a
627
relatively rare error condition, no care is taken to avoid
628
unnecessary additional work such as memcpy's that could be avoided.
631
/* An error occurred */
632
if (driver_byte(result) != 0 && /* An error occured */
633
SCpnt->sense_buffer[0] == 0xF0) { /* Sense data is valid */
634
switch (SCpnt->sense_buffer[2]) {
636
error_sector = (SCpnt->sense_buffer[3] << 24) |
637
(SCpnt->sense_buffer[4] << 16) |
638
(SCpnt->sense_buffer[5] << 8) |
639
SCpnt->sense_buffer[6];
640
if (SCpnt->request.bh != NULL)
641
block_sectors = SCpnt->request.bh->b_size >> 9;
642
switch (SCpnt->device->sector_size) {
645
if (block_sectors < 2)
650
if (block_sectors < 4)
655
if (block_sectors < 8)
664
error_sector -= sd_gendisks[SD_MAJOR_IDX(
665
SCpnt->request.rq_dev)].part[MINOR(
666
SCpnt->request.rq_dev)].start_sect;
667
error_sector &= ~(block_sectors - 1);
668
good_sectors = error_sector - SCpnt->request.sector;
669
if (good_sectors < 0 || good_sectors >= this_count)
673
case RECOVERED_ERROR:
675
* An error occured, but it recovered. Inform the
676
* user, but make sure that it's not treated as a
679
print_sense("sd", SCpnt);
681
SCpnt->sense_buffer[0] = 0x0;
682
good_sectors = this_count;
685
case ILLEGAL_REQUEST:
686
if (SCpnt->device->ten == 1) {
687
if (SCpnt->cmnd[0] == READ_10 ||
688
SCpnt->cmnd[0] == WRITE_10)
689
SCpnt->device->ten = 0;
698
* This calls the generic completion function, now that we know
699
* how many actual sectors finished, and how many sectors we need
700
* to say have failed.
702
scsi_io_completion(SCpnt, good_sectors, block_sectors);
705
* requeue_sd_request() is the request handler function for the sd driver.
706
* Its function in life is to take block device requests, and translate
707
* them to SCSI commands.
711
static int check_scsidisk_media_change(kdev_t full_dev)
718
target = DEVICE_NR(full_dev);
719
SDev = rscsi_disks[target].device;
721
if (target >= sd_template.dev_max || !SDev) {
722
printk("SCSI disk request error: invalid device.\n");
725
if (!SDev->removable)
729
* If the device is offline, don't send any commands - just pretend as
730
* if the command failed. If the device ever comes back online, we
731
* can deal with it then. It is only because of unrecoverable errors
732
* that we would ever take a device offline in the first place.
734
if (SDev->online == FALSE) {
735
rscsi_disks[target].ready = 0;
737
return 1; /* This will force a flush, if called from
738
* check_disk_change */
741
/* Using Start/Stop enables differentiation between drive with
742
* no cartridge loaded - NOT READY, drive with changed cartridge -
743
* UNIT ATTENTION, or with same cartridge - GOOD STATUS.
744
* This also handles drives that auto spin down. eg iomega jaz 1GB
745
* as this will spin up the drive.
748
if (scsi_block_when_processing_errors(SDev))
749
retval = scsi_ioctl(SDev, SCSI_IOCTL_START_UNIT, NULL);
751
if (retval) { /* Unable to test, unit probably not ready.
752
* This usually means there is no disc in the
753
* drive. Mark as changed, and we will figure
754
* it out later once the drive is available
757
rscsi_disks[target].ready = 0;
759
return 1; /* This will force a flush, if called from
760
* check_disk_change */
763
* for removable scsi disk ( FLOPTICAL ) we have to recognise the
764
* presence of disk in the drive. This is kept in the Scsi_Disk
765
* struct and tested at open ! Daniel Roche ( dan@lectra.fr )
768
rscsi_disks[target].ready = 1; /* FLOPTICAL */
770
retval = SDev->changed;
776
static int sd_init_onedisk(int i)
778
unsigned char cmd[10];
780
unsigned char *buffer;
781
unsigned long spintime_value = 0;
782
int the_result, retries, spintime;
787
* Get the name of the disk, in case we need to log it somewhere.
789
sd_devname(i, nbuff);
792
* If the device is offline, don't try and read capacity or any
793
* of the other niceties.
795
if (rscsi_disks[i].device->online == FALSE)
799
* We need to retry the READ_CAPACITY because a UNIT_ATTENTION is
800
* considered a fatal error, and many devices report such an error
801
* just after a scsi bus reset.
804
SRpnt = scsi_allocate_request(rscsi_disks[i].device);
807
"(sd_init_onedisk:) Request allocation failure.\n");
811
buffer = (unsigned char *) scsi_malloc(512);
813
printk(KERN_WARNING "(sd_init_onedisk:) Memory allocation failure.\n");
814
scsi_release_request(SRpnt);
820
/* Spin up drives, as required. Only do this at boot time */
821
/* Spinup needs to be done for module loads too. */
825
while (retries < 3) {
826
cmd[0] = TEST_UNIT_READY;
827
cmd[1] = (rscsi_disks[i].device->scsi_level <= SCSI_2) ?
828
((rscsi_disks[i].device->lun << 5) & 0xe0) : 0;
829
memset((void *) &cmd[2], 0, 8);
830
SRpnt->sr_cmd_len = 0;
831
SRpnt->sr_sense_buffer[0] = 0;
832
SRpnt->sr_sense_buffer[2] = 0;
833
SRpnt->sr_data_direction = SCSI_DATA_NONE;
835
scsi_wait_req (SRpnt, (void *) cmd, (void *) buffer,
836
0/*512*/, SD_TIMEOUT, MAX_RETRIES);
838
the_result = SRpnt->sr_result;
841
|| SRpnt->sr_sense_buffer[2] != UNIT_ATTENTION)
846
* If the drive has indicated to us that it doesn't have
847
* any media in it, don't bother with any of the rest of
851
&& ((driver_byte(the_result) & DRIVER_SENSE) != 0)
852
&& SRpnt->sr_sense_buffer[2] == UNIT_ATTENTION
853
&& SRpnt->sr_sense_buffer[12] == 0x3A ) {
854
rscsi_disks[i].capacity = 0x1fffff;
856
rscsi_disks[i].device->changed = 1;
857
rscsi_disks[i].ready = 0;
861
/* Look for non-removable devices that return NOT_READY.
862
* Issue command to spin up drive for these cases. */
863
if (the_result && !rscsi_disks[i].device->removable &&
864
SRpnt->sr_sense_buffer[2] == NOT_READY) {
867
printk("%s: Spinning up disk...", nbuff);
869
cmd[1] = (rscsi_disks[i].device->scsi_level <= SCSI_2) ?
870
((rscsi_disks[i].device->lun << 5) & 0xe0) : 0;
871
cmd[1] |= 1; /* Return immediately */
872
memset((void *) &cmd[2], 0, 8);
873
cmd[4] = 1; /* Start spin cycle */
874
SRpnt->sr_cmd_len = 0;
875
SRpnt->sr_sense_buffer[0] = 0;
876
SRpnt->sr_sense_buffer[2] = 0;
878
SRpnt->sr_data_direction = SCSI_DATA_READ;
879
scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
880
0/*512*/, SD_TIMEOUT, MAX_RETRIES);
881
spintime_value = jiffies;
885
/* Wait 1 second for next try */
887
current->state = TASK_UNINTERRUPTIBLE;
888
time1 = schedule_timeout(time1);
892
} while (the_result && spintime &&
893
time_after(spintime_value + 100 * HZ, jiffies));
896
printk("not responding...\n");
902
cmd[0] = READ_CAPACITY;
903
cmd[1] = (rscsi_disks[i].device->scsi_level <= SCSI_2) ?
904
((rscsi_disks[i].device->lun << 5) & 0xe0) : 0;
905
memset((void *) &cmd[2], 0, 8);
906
memset((void *) buffer, 0, 8);
907
SRpnt->sr_cmd_len = 0;
908
SRpnt->sr_sense_buffer[0] = 0;
909
SRpnt->sr_sense_buffer[2] = 0;
911
SRpnt->sr_data_direction = SCSI_DATA_READ;
912
scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
913
8, SD_TIMEOUT, MAX_RETRIES);
915
the_result = SRpnt->sr_result;
918
} while (the_result && retries);
921
* The SCSI standard says:
922
* "READ CAPACITY is necessary for self configuring software"
923
* While not mandatory, support of READ CAPACITY is strongly
925
* We used to die if we couldn't successfully do a READ CAPACITY.
926
* But, now we go on about our way. The side effects of this are
928
* 1. We can't know block size with certainty. I have said
929
* "512 bytes is it" as this is most common.
931
* 2. Recovery from when someone attempts to read past the
932
* end of the raw device will be slower.
936
printk("%s : READ CAPACITY failed.\n"
937
"%s : status = %x, message = %02x, host = %d, driver = %02x \n",
939
status_byte(the_result),
940
msg_byte(the_result),
941
host_byte(the_result),
942
driver_byte(the_result)
944
if (driver_byte(the_result) & DRIVER_SENSE)
945
print_req_sense("sd", SRpnt);
947
printk("%s : sense not available. \n", nbuff);
949
printk("%s : block size assumed to be 512 bytes, disk size 1GB. \n",
951
rscsi_disks[i].capacity = 0x1fffff;
954
/* Set dirty bit for removable devices if not ready -
955
* sometimes drives will not report this properly. */
956
if (rscsi_disks[i].device->removable &&
957
SRpnt->sr_sense_buffer[2] == NOT_READY)
958
rscsi_disks[i].device->changed = 1;
962
* FLOPTICAL, if read_capa is ok, drive is assumed to be ready
964
rscsi_disks[i].ready = 1;
966
rscsi_disks[i].capacity = 1 + ((buffer[0] << 24) |
971
sector_size = (buffer[4] << 24) |
972
(buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
974
if (sector_size == 0) {
976
printk("%s : sector size 0 reported, assuming 512.\n",
979
if (sector_size != 512 &&
980
sector_size != 1024 &&
981
sector_size != 2048 &&
982
sector_size != 4096 &&
983
sector_size != 256) {
984
printk("%s : unsupported sector size %d.\n",
987
* The user might want to re-format the drive with
988
* a supported sectorsize. Once this happens, it
989
* would be relatively trivial to set the thing up.
990
* For this reason, we leave the thing in the table.
992
rscsi_disks[i].capacity = 0;
994
if (sector_size > 1024) {
998
* We must fix the sd_blocksizes and sd_hardsizes
999
* to allow us to read the partition tables.
1000
* The disk reading code does not allow for reading
1001
* of partial sectors.
1003
for (m = i << 4; m < ((i + 1) << 4); m++) {
1004
sd_blocksizes[m] = sector_size;
1008
* The msdos fs needs to know the hardware sector size
1009
* So I have created this table. See ll_rw_blk.c
1010
* Jacques Gelinas (Jacques@solucorp.qc.ca)
1013
int hard_sector = sector_size;
1014
int sz = rscsi_disks[i].capacity * (hard_sector/256);
1016
/* There are 16 minors allocated for each major device */
1017
for (m = i << 4; m < ((i + 1) << 4); m++) {
1018
sd_hardsizes[m] = hard_sector;
1021
printk("SCSI device %s: "
1022
"%d %d-byte hdwr sectors (%d MB)\n",
1023
nbuff, rscsi_disks[i].capacity,
1024
hard_sector, (sz/2 - sz/1250 + 974)/1950);
1027
/* Rescale capacity to 512-byte units */
1028
if (sector_size == 4096)
1029
rscsi_disks[i].capacity <<= 3;
1030
if (sector_size == 2048)
1031
rscsi_disks[i].capacity <<= 2;
1032
if (sector_size == 1024)
1033
rscsi_disks[i].capacity <<= 1;
1034
if (sector_size == 256)
1035
rscsi_disks[i].capacity >>= 1;
1040
* Unless otherwise specified, this is not write protected.
1042
rscsi_disks[i].write_prot = 0;
1043
if (rscsi_disks[i].device->removable && rscsi_disks[i].ready) {
1047
* For removable scsi disk ( FLOPTICAL ) we have to recognise
1048
* the Write Protect Flag. This flag is kept in the Scsi_Disk
1049
* struct and tested at open !
1050
* Daniel Roche ( dan@lectra.fr )
1052
* Changed to get all pages (0x3f) rather than page 1 to
1053
* get around devices which do not have a page 1. Since
1054
* we're only interested in the header anyway, this should
1056
* -- Matthew Dharm (mdharm-scsi@one-eyed-alien.net)
1059
memset((void *) &cmd[0], 0, 8);
1060
cmd[0] = MODE_SENSE;
1061
cmd[1] = (rscsi_disks[i].device->scsi_level <= SCSI_2) ?
1062
((rscsi_disks[i].device->lun << 5) & 0xe0) : 0;
1063
cmd[2] = 0x3f; /* Get all pages */
1064
cmd[4] = 255; /* Ask for 255 bytes, even tho we want just the first 8 */
1065
SRpnt->sr_cmd_len = 0;
1066
SRpnt->sr_sense_buffer[0] = 0;
1067
SRpnt->sr_sense_buffer[2] = 0;
1069
/* same code as READCAPA !! */
1070
SRpnt->sr_data_direction = SCSI_DATA_READ;
1071
scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
1072
512, SD_TIMEOUT, MAX_RETRIES);
1074
the_result = SRpnt->sr_result;
1077
printk("%s: test WP failed, assume Write Enabled\n", nbuff);
1079
rscsi_disks[i].write_prot = ((buffer[2] & 0x80) != 0);
1080
printk("%s: Write Protect is %s\n", nbuff,
1081
rscsi_disks[i].write_prot ? "on" : "off");
1084
} /* check for write protect */
1085
SRpnt->sr_device->ten = 1;
1086
SRpnt->sr_device->remap = 1;
1087
SRpnt->sr_device->sector_size = sector_size;
1088
/* Wake up a process waiting for device */
1089
scsi_release_request(SRpnt);
1092
scsi_free(buffer, 512);
1097
* The sd_init() function looks at all SCSI drives present, determines
1098
* their size, and reads partition table entries for them.
1101
static int sd_registered;
1103
static int sd_init()
1107
if (sd_template.dev_noticed == 0)
1111
sd_template.dev_max = sd_template.dev_noticed + SD_EXTRA_DEVS;
1113
if (sd_template.dev_max > N_SD_MAJORS * SCSI_DISKS_PER_MAJOR)
1114
sd_template.dev_max = N_SD_MAJORS * SCSI_DISKS_PER_MAJOR;
1116
if (!sd_registered) {
1117
for (i = 0; i < N_USED_SD_MAJORS; i++) {
1118
#ifdef DEVFS_MUST_DIE
1119
if (devfs_register_blkdev(SD_MAJOR(i), "sd", &sd_fops)) {
1120
printk("Unable to get major %d for SCSI disk\n", SD_MAJOR(i));
1121
sd_template.dev_noticed = 0;
1128
/* We do not support attaching loadable devices yet. */
1132
rscsi_disks = kmalloc(sd_template.dev_max * sizeof(Scsi_Disk), GFP_ATOMIC);
1135
memset(rscsi_disks, 0, sd_template.dev_max * sizeof(Scsi_Disk));
1137
/* for every (necessary) major: */
1138
sd_sizes = kmalloc((sd_template.dev_max << 4) * sizeof(int), GFP_ATOMIC);
1141
memset(sd_sizes, 0, (sd_template.dev_max << 4) * sizeof(int));
1143
sd_blocksizes = kmalloc((sd_template.dev_max << 4) * sizeof(int),
1148
sd_hardsizes = kmalloc((sd_template.dev_max << 4) * sizeof(int),
1151
goto cleanup_blocksizes;
1153
sd_max_sectors = kmalloc((sd_template.dev_max << 4) * sizeof(int),
1155
if (!sd_max_sectors)
1156
goto cleanup_max_sectors;
1158
for (i = 0; i < sd_template.dev_max << 4; i++) {
1159
sd_blocksizes[i] = 1024;
1160
sd_hardsizes[i] = 512;
1162
* Allow lowlevel device drivers to generate 512k large scsi
1163
* commands if they know what they're doing and they ask for it
1164
* explicitly via the SHpnt->max_sectors API.
1166
sd_max_sectors[i] = MAX_SEGMENTS*8;
1169
for (i = 0; i < N_USED_SD_MAJORS; i++) {
1170
blksize_size[SD_MAJOR(i)] = sd_blocksizes +
1171
i * (SCSI_DISKS_PER_MAJOR << 4);
1172
hardsect_size[SD_MAJOR(i)] = sd_hardsizes +
1173
i * (SCSI_DISKS_PER_MAJOR << 4);
1174
max_sectors[SD_MAJOR(i)] = sd_max_sectors +
1175
i * (SCSI_DISKS_PER_MAJOR << 4);
1178
sd_gendisks = kmalloc(N_USED_SD_MAJORS * sizeof(struct gendisk),
1181
goto cleanup_sd_gendisks;
1182
for (i = 0; i < N_USED_SD_MAJORS; i++) {
1183
sd_gendisks[i] = sd_gendisk; /* memcpy */
1184
#ifdef DEVFS_MUST_DIE
1185
sd_gendisks[i].de_arr = kmalloc (SCSI_DISKS_PER_MAJOR *
1186
sizeof *sd_gendisks[i].de_arr,
1188
if (!sd_gendisks[i].de_arr)
1189
goto cleanup_gendisks_de_arr;
1190
memset (sd_gendisks[i].de_arr, 0,
1191
SCSI_DISKS_PER_MAJOR * sizeof *sd_gendisks[i].de_arr);
1193
sd_gendisks[i].flags = kmalloc (SCSI_DISKS_PER_MAJOR *
1194
sizeof *sd_gendisks[i].flags,
1196
if (!sd_gendisks[i].flags)
1197
goto cleanup_gendisks_flags;
1198
memset (sd_gendisks[i].flags, 0,
1199
SCSI_DISKS_PER_MAJOR * sizeof *sd_gendisks[i].flags);
1200
sd_gendisks[i].major = SD_MAJOR(i);
1201
sd_gendisks[i].major_name = "sd";
1202
sd_gendisks[i].minor_shift = 4;
1203
sd_gendisks[i].max_p = 1 << 4;
1204
sd_gendisks[i].part = kmalloc((SCSI_DISKS_PER_MAJOR << 4) *
1205
sizeof(struct hd_struct),
1207
if (!sd_gendisks[i].part)
1208
goto cleanup_gendisks_part;
1209
memset(sd_gendisks[i].part, 0, (SCSI_DISKS_PER_MAJOR << 4) *
1210
sizeof(struct hd_struct));
1211
sd_gendisks[i].sizes = sd_sizes + (i * SCSI_DISKS_PER_MAJOR << 4);
1212
sd_gendisks[i].nr_real = 0;
1213
sd_gendisks[i].real_devices =
1214
(void *) (rscsi_disks + i * SCSI_DISKS_PER_MAJOR);
1219
cleanup_gendisks_part:
1220
kfree(sd_gendisks[i].flags);
1221
cleanup_gendisks_flags:
1222
#ifdef DEVFS_MUST_DIE
1223
kfree(sd_gendisks[i].de_arr);
1224
cleanup_gendisks_de_arr:
1227
#ifdef DEVFS_MUST_DIE
1228
kfree(sd_gendisks[i].de_arr);
1230
kfree(sd_gendisks[i].flags);
1231
kfree(sd_gendisks[i].part);
1235
cleanup_sd_gendisks:
1236
kfree(sd_max_sectors);
1237
cleanup_max_sectors:
1238
kfree(sd_hardsizes);
1240
kfree(sd_blocksizes);
1247
#ifdef DEVFS_MUST_DIE
1248
for (i = 0; i < N_USED_SD_MAJORS; i++) {
1249
devfs_unregister_blkdev(SD_MAJOR(i), "sd");
1253
sd_template.dev_noticed = 0;
1258
static void sd_finish()
1262
for (i = 0; i < N_USED_SD_MAJORS; i++) {
1263
blk_dev[SD_MAJOR(i)].queue = sd_find_queue;
1264
add_gendisk(&sd_gendisks[i]);
1267
for (i = 0; i < sd_template.dev_max; ++i)
1268
if (!rscsi_disks[i].capacity && rscsi_disks[i].device) {
1270
if (!rscsi_disks[i].has_part_table) {
1271
sd_sizes[i << 4] = rscsi_disks[i].capacity;
1272
register_disk(&SD_GENDISK(i), MKDEV_SD(i),
1274
rscsi_disks[i].capacity);
1275
rscsi_disks[i].has_part_table = 1;
1279
/* If our host adapter is capable of scatter-gather, then we increase
1280
* the read-ahead to 60 blocks (120 sectors). If not, we use
1281
* a two block (4 sector) read ahead. We can only respect this with the
1282
* granularity of every 16 disks (one device major).
1284
for (i = 0; i < N_USED_SD_MAJORS; i++) {
1285
read_ahead[SD_MAJOR(i)] =
1286
(rscsi_disks[i * SCSI_DISKS_PER_MAJOR].device
1287
&& rscsi_disks[i * SCSI_DISKS_PER_MAJOR].device->host->sg_tablesize)
1288
? 120 /* 120 sector read-ahead */
1289
: 4; /* 4 sector read-ahead */
1294
/* XXX SMH: turn on some logging */
1295
scsi_logging_level = ~0;
1296
SCSI_SET_LOGGING(SCSI_LOG_HLQUEUE_SHIFT, SCSI_LOG_HLQUEUE_BITS, 1);
1304
** scsi_probe_devices:
1306
** add the scsi block devices for this domain to a xen_disk_info_t;
1307
** we assume xdi->count points to the first unused place in the array.
1309
** XXX SMH: this is a rather gross 'probe' function to allow xeno world
1310
** to grope us; this should really not be in the disk-specific code as
1311
** it should report tapes, CDs, etc. But for now this looks like the
1312
** easiest place to hook it in :-(
1316
static kdev_t scsi_devs[] = {
1317
MKDEV(SCSI_DISK0_MAJOR, 0), MKDEV(SCSI_DISK0_MAJOR, 16), /* sda, sdb */
1318
MKDEV(SCSI_DISK0_MAJOR, 32), MKDEV(SCSI_DISK0_MAJOR, 48), /* sdc, sdd */
1319
MKDEV(SCSI_DISK0_MAJOR, 64), MKDEV(SCSI_DISK0_MAJOR, 80), /* sde, sdf */
1320
MKDEV(SCSI_DISK0_MAJOR, 96), MKDEV(SCSI_DISK0_MAJOR, 112), /* sdg, sdh */
1321
MKDEV(SCSI_DISK0_MAJOR, 128), MKDEV(SCSI_DISK0_MAJOR, 144), /* sdi, sdj */
1322
MKDEV(SCSI_DISK0_MAJOR, 160), MKDEV(SCSI_DISK0_MAJOR, 176), /* sdk, sdl */
1323
MKDEV(SCSI_DISK0_MAJOR, 192), MKDEV(SCSI_DISK0_MAJOR, 208), /* sdm, sdn */
1324
MKDEV(SCSI_DISK0_MAJOR, 224), MKDEV(SCSI_DISK0_MAJOR, 240), /* sdo, sdp */
1328
void scsi_probe_devices(xen_disk_info_t *xdi)
1332
xen_disk_t *xd = &xdi->disks[xdi->count];
1334
for ( sd = rscsi_disks, i = 0; i < sd_template.dev_max; i++, sd++ )
1336
if ( sd->device == NULL )
1339
if ( xdi->count == xdi->max )
1342
/* We export 'raw' linux device numbers to domain 0. */
1343
xd->device = scsi_devs[i];
1344
xd->info = XD_TYPE_DISK; /* XXX should determine properly */
1345
xd->capacity = sd->capacity;
1356
static int sd_detect(Scsi_Device * SDp)
1358
if (SDp->type != TYPE_DISK && SDp->type != TYPE_MOD)
1360
sd_template.dev_noticed++;
1364
static int sd_attach(Scsi_Device * SDp)
1366
unsigned int devnum;
1371
if (SDp->type != TYPE_DISK && SDp->type != TYPE_MOD)
1374
if (sd_template.nr_dev >= sd_template.dev_max || rscsi_disks == NULL) {
1378
for (dpnt = rscsi_disks, i = 0; i < sd_template.dev_max; i++, dpnt++)
1382
if (i >= sd_template.dev_max) {
1383
printk(KERN_WARNING "scsi_devices corrupt (sd),"
1384
" nr_dev %d dev_max %d\n",
1385
sd_template.nr_dev, sd_template.dev_max);
1390
rscsi_disks[i].device = SDp;
1391
rscsi_disks[i].has_part_table = 0;
1392
sd_template.nr_dev++;
1393
SD_GENDISK(i).nr_real++;
1394
devnum = i % SCSI_DISKS_PER_MAJOR;
1395
#ifdef DEVFS_MUST_DIE
1396
SD_GENDISK(i).de_arr[devnum] = SDp->de;
1399
SD_GENDISK(i).flags[devnum] |= GENHD_FL_REMOVABLE;
1400
sd_devname(i, nbuff);
1401
printk("Attached scsi %sdisk %s at scsi%d, channel %d, id %d, lun %d\n",
1402
SDp->removable ? "removable " : "",
1403
nbuff, SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
1407
#define DEVICE_BUSY rscsi_disks[target].device->busy
1408
#define ALLOW_REVALIDATE rscsi_disks[target].device->allow_revalidate
1409
#define USAGE rscsi_disks[target].device->access_count
1410
#define CAPACITY rscsi_disks[target].capacity
1411
#define MAYBE_REINIT sd_init_onedisk(target)
1413
/* This routine is called to flush all partitions and partition tables
1414
* for a changed scsi disk, and then re-read the new partition table.
1415
* If we are revalidating a disk because of a media change, then we
1416
* enter with usage == 0. If we are using an ioctl, we automatically have
1417
* usage == 1 (we need an open channel to use an ioctl :-), so this
1420
int revalidate_scsidisk(kdev_t dev, int maxusage)
1422
struct gendisk *sdgd;
1428
target = DEVICE_NR(dev);
1430
if (DEVICE_BUSY || (ALLOW_REVALIDATE == 0 && USAGE > maxusage)) {
1431
printk("Device busy for revalidation (usage=%d)\n", USAGE);
1436
sdgd = &SD_GENDISK(target);
1437
max_p = sd_gendisk.max_p;
1438
start = target << sd_gendisk.minor_shift;
1440
for (i = max_p - 1; i >= 0; i--) {
1441
int index = start + i;
1442
invalidate_device(MKDEV_SD_PARTITION(index), 1);
1443
sdgd->part[SD_MINOR_NUMBER(index)].start_sect = 0;
1444
sdgd->part[SD_MINOR_NUMBER(index)].nr_sects = 0;
1446
* Reset the blocksize for everything so that we can read
1447
* the partition table. Technically we will determine the
1448
* correct block size when we revalidate, but we do this just
1449
* to make sure that everything remains consistent.
1451
sd_blocksizes[index] = 1024;
1452
if (rscsi_disks[target].device->sector_size == 2048)
1453
sd_blocksizes[index] = 2048;
1455
sd_blocksizes[index] = 1024;
1462
grok_partitions(&SD_GENDISK(target), target % SCSI_DISKS_PER_MAJOR,
1469
static int fop_revalidate_scsidisk(kdev_t dev)
1471
return revalidate_scsidisk(dev, 0);
1474
static void sd_detach(Scsi_Device * SDp)
1477
struct gendisk *sdgd;
1482
if (rscsi_disks == NULL)
1485
for (dpnt = rscsi_disks, i = 0; i < sd_template.dev_max; i++, dpnt++)
1486
if (dpnt->device == SDp) {
1488
/* If we are disconnecting a disk driver, sync and invalidate
1490
sdgd = &SD_GENDISK(i);
1491
max_p = sd_gendisk.max_p;
1492
start = i << sd_gendisk.minor_shift;
1494
for (j = max_p - 1; j >= 0; j--) {
1495
int index = start + j;
1496
invalidate_device(MKDEV_SD_PARTITION(index), 1);
1497
sdgd->part[SD_MINOR_NUMBER(index)].start_sect = 0;
1498
sdgd->part[SD_MINOR_NUMBER(index)].nr_sects = 0;
1499
sd_sizes[index] = 0;
1501
#ifdef DEVFS_MUST_DIE
1502
devfs_register_partitions (sdgd,
1503
SD_MINOR_NUMBER (start), 1);
1505
/* unregister_disk() */
1506
dpnt->has_part_table = 0;
1507
dpnt->device = NULL;
1510
sd_template.dev_noticed--;
1511
sd_template.nr_dev--;
1512
SD_GENDISK(i).nr_real--;
1518
static int __init init_sd(void)
1520
extern int scsi_register_module(int, void *);
1522
sd_template.module = THIS_MODULE;
1523
return scsi_register_module(MODULE_SCSI_DEV, &sd_template);
1526
static void __exit exit_sd(void)
1531
scsi_unregister_module(MODULE_SCSI_DEV, &sd_template);
1534
#ifdef DEVFS_MUST_DIE
1535
for (i = 0; i < N_USED_SD_MAJORS; i++)
1536
devfs_unregister_blkdev(SD_MAJOR(i), "sd");
1540
if (rscsi_disks != NULL) {
1543
kfree(sd_blocksizes);
1544
kfree(sd_hardsizes);
1545
for (i = 0; i < N_USED_SD_MAJORS; i++) {
1546
#if 0 /* XXX aren't we forgetting to deallocate something? */
1547
kfree(sd_gendisks[i].de_arr);
1548
kfree(sd_gendisks[i].flags);
1550
kfree(sd_gendisks[i].part);
1553
for (i = 0; i < N_USED_SD_MAJORS; i++) {
1554
del_gendisk(&sd_gendisks[i]);
1555
blk_size[SD_MAJOR(i)] = NULL; /* XXX blksize_size actually? */
1556
hardsect_size[SD_MAJOR(i)] = NULL;
1558
read_ahead[SD_MAJOR(i)] = 0;
1561
sd_template.dev_max = 0;
1562
if (sd_gendisks != NULL) /* kfree tests for 0, but leave explicit */
1566
module_init(init_sd);
1567
module_exit(exit_sd);
1568
MODULE_LICENSE("GPL");