Import 2.3.39pre2
[davej-history.git] / drivers / scsi / sd.c
blob6345505b31bcc3f188b2b0a3e3fad98124bafab5
1 /*
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
9 * <drew@colorado.edu>
11 * Modified by Eric Youngdale ericy@andante.org to
12 * add scatter-gather, multiple outstanding request, and other
13 * enhancements.
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.
22 #include <linux/module.h>
23 #ifdef MODULE
25 * This is a variable in scsi.c that is set when we are processing something
26 * after boot time. By definition, this is true when we are a loadable module
27 * ourselves.
29 #define MODULE_FLAG 1
30 #else
31 #define MODULE_FLAG scsi_loadable_module_flag
32 #endif /* MODULE */
34 #include <linux/fs.h>
35 #include <linux/kernel.h>
36 #include <linux/sched.h>
37 #include <linux/mm.h>
38 #include <linux/string.h>
39 #include <linux/hdreg.h>
40 #include <linux/errno.h>
41 #include <linux/interrupt.h>
42 #include <linux/init.h>
44 #include <linux/smp.h>
46 #include <asm/uaccess.h>
47 #include <asm/system.h>
48 #include <asm/io.h>
50 #define MAJOR_NR SCSI_DISK0_MAJOR
51 #include <linux/blk.h>
52 #include <linux/blkpg.h>
53 #include "scsi.h"
54 #include "hosts.h"
55 #include "sd.h"
56 #include <scsi/scsi_ioctl.h>
57 #include "constants.h"
58 #include <scsi/scsicam.h> /* must follow "hosts.h" */
60 #include <linux/genhd.h>
63 * static const char RCSid[] = "$Header:";
66 #define SD_MAJOR(i) (!(i) ? SCSI_DISK0_MAJOR : SCSI_DISK1_MAJOR-1+(i))
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)
76 #define MAX_RETRIES 5
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 struct hd_struct *sd;
87 static Scsi_Disk *rscsi_disks = NULL;
88 static int *sd_sizes;
89 static int *sd_blocksizes;
90 static int *sd_hardsizes; /* Hardware sector size */
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 void rw_intr(Scsi_Cmnd * SCpnt);
105 static int sd_init_command(Scsi_Cmnd *);
107 #if defined(CONFIG_PPC)
109 * Moved from arch/ppc/pmac_setup.c. This is where it really belongs.
111 kdev_t __init
112 sd_find_target(void *host, int tgt)
114 Scsi_Disk *dp;
115 int i;
116 for (dp = rscsi_disks, i = 0; i < sd_template.dev_max; ++i, ++dp)
117 if (dp->device != NULL && dp->device->host == host
118 && dp->device->id == tgt)
119 return MKDEV_SD(i);
120 return 0;
122 #endif
124 static int sd_ioctl(struct inode * inode, struct file * file, unsigned int cmd, unsigned long arg)
126 kdev_t dev = inode->i_rdev;
127 struct Scsi_Host * host;
128 Scsi_Device * SDev;
129 int diskinfo[4];
130 struct hd_geometry *loc = (struct hd_geometry *) arg;
132 SDev = rscsi_disks[DEVICE_NR(dev)].device;
134 * If we are in the middle of error recovery, don't let anyone
135 * else try and use this device. Also, if error recovery fails, it
136 * may try and take the device offline, in which case all further
137 * access to the device is prohibited.
140 if( !scsi_block_when_processing_errors(SDev) )
142 return -ENODEV;
145 switch (cmd)
147 case HDIO_GETGEO: /* Return BIOS disk parameters */
148 if(!loc)
149 return -EINVAL;
151 host = rscsi_disks[DEVICE_NR(dev)].device->host;
153 /* default to most commonly used values */
155 diskinfo[0] = 0x40;
156 diskinfo[1] = 0x20;
157 diskinfo[2] = rscsi_disks[DEVICE_NR(dev)].capacity >> 11;
159 /* override with calculated, extended default, or driver values */
161 if(host->hostt->bios_param != NULL)
162 host->hostt->bios_param(&rscsi_disks[DEVICE_NR(dev)],
163 dev,
164 &diskinfo[0]);
165 else scsicam_bios_param(&rscsi_disks[DEVICE_NR(dev)],
166 dev, &diskinfo[0]);
168 if (put_user(diskinfo[0], &loc->heads) ||
169 put_user(diskinfo[1], &loc->sectors) ||
170 put_user(diskinfo[2], &loc->cylinders) ||
171 put_user(sd[SD_PARTITION(inode->i_rdev)].start_sect, &loc->start))
172 return -EFAULT;
173 return 0;
174 case BLKGETSIZE: /* Return device size */
175 if (!arg)
176 return -EINVAL;
177 return put_user(sd[SD_PARTITION(inode->i_rdev)].nr_sects, (long *) arg);
179 case BLKROSET:
180 case BLKROGET:
181 case BLKRASET:
182 case BLKRAGET:
183 case BLKFLSBUF:
184 case BLKSSZGET:
185 case BLKPG:
186 return blk_ioctl(inode->i_rdev, cmd, arg);
188 case BLKRRPART: /* Re-read partition tables */
189 if (!capable(CAP_SYS_ADMIN))
190 return -EACCES;
191 return revalidate_scsidisk(dev, 1);
193 default:
194 return scsi_ioctl(rscsi_disks[DEVICE_NR(dev)].device , cmd, (void *) arg);
198 static void sd_devname(unsigned int disknum, char *buffer)
200 if (disknum < 26)
201 sprintf(buffer, "sd%c", 'a' + disknum);
202 else {
203 unsigned int min1;
204 unsigned int min2;
206 * For larger numbers of disks, we need to go to a new
207 * naming scheme.
209 min1 = disknum / 26;
210 min2 = disknum % 26;
211 sprintf(buffer, "sd%c%c", 'a' + min1 - 1, 'a' + min2);
215 struct Scsi_Device_Template sd_template = {
216 name:"disk",
217 tag:"sd",
218 scsi_type:TYPE_DISK,
219 major:SCSI_DISK0_MAJOR,
221 * Secondary range of majors that this driver handles.
223 min_major:SCSI_DISK1_MAJOR,
224 max_major:SCSI_DISK7_MAJOR,
225 blk:1,
226 detect:sd_detect,
227 init:sd_init,
228 finish:sd_finish,
229 attach:sd_attach,
230 detach:sd_detach,
231 init_command:sd_init_command,
234 static request_queue_t *sd_find_queue(kdev_t dev)
236 Scsi_Disk *dpnt;
237 int target;
238 target = DEVICE_NR(dev);
240 dpnt = &rscsi_disks[target];
241 if (!dpnt)
242 return NULL; /* No such device */
243 return &dpnt->device->request_queue;
246 static int sd_init_command(Scsi_Cmnd * SCpnt)
248 int dev, devm, block, this_count;
249 Scsi_Disk *dpnt;
250 #if CONFIG_SCSI_LOGGING
251 char nbuff[6];
252 #endif
254 devm = SD_PARTITION(SCpnt->request.rq_dev);
255 dev = DEVICE_NR(SCpnt->request.rq_dev);
257 block = SCpnt->request.sector;
258 this_count = SCpnt->request_bufflen >> 9;
260 SCSI_LOG_HLQUEUE(1, printk("Doing sd request, dev = %d, block = %d\n", devm, block));
262 dpnt = &rscsi_disks[dev];
263 if (devm >= (sd_template.dev_max << 4) ||
264 !dpnt ||
265 !dpnt->device->online ||
266 block + SCpnt->request.nr_sectors > sd[devm].nr_sects) {
267 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", SCpnt->request.nr_sectors));
268 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
269 return 0;
271 block += sd[devm].start_sect;
272 if (dpnt->device->changed) {
274 * quietly refuse to do anything to a changed disc until the changed
275 * bit has been reset
277 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
278 return 0;
280 SCSI_LOG_HLQUEUE(2, sd_devname(devm, nbuff));
281 SCSI_LOG_HLQUEUE(2, printk("%s : real dev = /dev/%d, block = %d\n",
282 nbuff, dev, block));
285 * If we have a 1K hardware sectorsize, prevent access to single
286 * 512 byte sectors. In theory we could handle this - in fact
287 * the scsi cdrom driver must be able to handle this because
288 * we typically use 1K blocksizes, and cdroms typically have
289 * 2K hardware sectorsizes. Of course, things are simpler
290 * with the cdrom, since it is read-only. For performance
291 * reasons, the filesystems should be able to handle this
292 * and not force the scsi disk driver to use bounce buffers
293 * for this.
295 if (dpnt->device->sector_size == 1024) {
296 if ((block & 1) || (SCpnt->request.nr_sectors & 1)) {
297 printk("sd.c:Bad block number requested");
298 return 0;
299 } else {
300 block = block >> 1;
301 this_count = this_count >> 1;
304 if (dpnt->device->sector_size == 2048) {
305 if ((block & 3) || (SCpnt->request.nr_sectors & 3)) {
306 printk("sd.c:Bad block number requested");
307 return 0;
308 } else {
309 block = block >> 2;
310 this_count = this_count >> 2;
313 switch (SCpnt->request.cmd) {
314 case WRITE:
315 if (!dpnt->device->writeable) {
316 return 0;
318 SCpnt->cmnd[0] = WRITE_6;
319 break;
320 case READ:
321 SCpnt->cmnd[0] = READ_6;
322 break;
323 default:
324 panic("Unknown sd command %d\n", SCpnt->request.cmd);
327 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
328 nbuff,
329 (SCpnt->request.cmd == WRITE) ? "writing" : "reading",
330 this_count, SCpnt->request.nr_sectors));
332 SCpnt->cmnd[1] = (SCpnt->lun << 5) & 0xe0;
334 if (((this_count > 0xff) || (block > 0x1fffff)) || SCpnt->device->ten) {
335 if (this_count > 0xffff)
336 this_count = 0xffff;
338 SCpnt->cmnd[0] += READ_10 - READ_6;
339 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
340 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
341 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
342 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
343 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
344 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
345 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
346 } else {
347 if (this_count > 0xff)
348 this_count = 0xff;
350 SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
351 SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
352 SCpnt->cmnd[3] = (unsigned char) block & 0xff;
353 SCpnt->cmnd[4] = (unsigned char) this_count;
354 SCpnt->cmnd[5] = 0;
358 * We shouldn't disconnect in the middle of a sector, so with a dumb
359 * host adapter, it's safe to assume that we can at least transfer
360 * this many bytes between each connect / disconnect.
362 SCpnt->transfersize = dpnt->device->sector_size;
363 SCpnt->underflow = this_count << 9;
365 SCpnt->allowed = MAX_RETRIES;
366 SCpnt->timeout_per_command = (SCpnt->device->type == TYPE_DISK ?
367 SD_TIMEOUT : SD_MOD_TIMEOUT);
370 * This is the completion routine we use. This is matched in terms
371 * of capability to this function.
373 SCpnt->done = rw_intr;
376 * This indicates that the command is ready from our end to be
377 * queued.
379 return 1;
382 static int sd_open(struct inode *inode, struct file *filp)
384 int target;
385 target = DEVICE_NR(inode->i_rdev);
387 SCSI_LOG_HLQUEUE(1, printk("target=%d, max=%d\n", target, sd_template.dev_max));
389 if (target >= sd_template.dev_max || !rscsi_disks[target].device)
390 return -ENXIO; /* No such device */
393 * If the device is in error recovery, wait until it is done.
394 * If the device is offline, then disallow any access to it.
396 if (!scsi_block_when_processing_errors(rscsi_disks[target].device)) {
397 return -ENXIO;
400 * Make sure that only one process can do a check_change_disk at one time.
401 * This is also used to lock out further access when the partition table
402 * is being re-read.
405 while (rscsi_disks[target].device->busy)
406 barrier();
407 if (rscsi_disks[target].device->removable) {
408 check_disk_change(inode->i_rdev);
411 * If the drive is empty, just let the open fail.
413 if (!rscsi_disks[target].ready)
414 return -ENXIO;
417 * Similarly, if the device has the write protect tab set,
418 * have the open fail if the user expects to be able to write
419 * to the thing.
421 if ((rscsi_disks[target].write_prot) && (filp->f_mode & 2))
422 return -EROFS;
425 * It is possible that the disk changing stuff resulted in the device
426 * being taken offline. If this is the case, report this to the user,
427 * and don't pretend that
428 * the open actually succeeded.
430 if (!rscsi_disks[target].device->online) {
431 return -ENXIO;
434 * See if we are requesting a non-existent partition. Do this
435 * after checking for disk change.
437 if (sd_sizes[SD_PARTITION(inode->i_rdev)] == 0)
438 return -ENXIO;
440 if (rscsi_disks[target].device->removable)
441 if (!rscsi_disks[target].device->access_count)
442 sd_ioctl(inode, NULL, SCSI_IOCTL_DOORLOCK, 0);
444 rscsi_disks[target].device->access_count++;
445 if (rscsi_disks[target].device->host->hostt->module)
446 __MOD_INC_USE_COUNT(rscsi_disks[target].device->host->hostt->module);
447 if (sd_template.module)
448 __MOD_INC_USE_COUNT(sd_template.module);
449 return 0;
452 static int sd_release(struct inode *inode, struct file *file)
454 int target;
456 target = DEVICE_NR(inode->i_rdev);
458 rscsi_disks[target].device->access_count--;
460 if (rscsi_disks[target].device->removable) {
461 if (!rscsi_disks[target].device->access_count)
462 sd_ioctl(inode, NULL, SCSI_IOCTL_DOORUNLOCK, 0);
464 if (rscsi_disks[target].device->host->hostt->module)
465 __MOD_DEC_USE_COUNT(rscsi_disks[target].device->host->hostt->module);
466 if (sd_template.module)
467 __MOD_DEC_USE_COUNT(sd_template.module);
468 return 0;
471 static void sd_geninit(struct gendisk *);
473 static struct block_device_operations sd_fops =
475 open: sd_open,
476 release: sd_release,
477 ioctl: sd_ioctl,
478 check_media_change: check_scsidisk_media_change,
479 revalidate: fop_revalidate_scsidisk
483 * If we need more than one SCSI disk major (i.e. more than
484 * 16 SCSI disks), we'll have to kmalloc() more gendisks later.
487 static struct gendisk sd_gendisk =
489 SCSI_DISK0_MAJOR, /* Major number */
490 "sd", /* Major name */
491 4, /* Bits to shift to get real from partition */
492 1 << 4, /* Number of partitions per real */
493 0, /* maximum number of real */
494 sd_geninit, /* init function */
495 NULL, /* hd struct */
496 NULL, /* block sizes */
497 0, /* number */
498 NULL, /* internal */
499 NULL /* next */
502 static struct gendisk *sd_gendisks = &sd_gendisk;
504 #define SD_GENDISK(i) sd_gendisks[(i) / SCSI_DISKS_PER_MAJOR]
505 #define LAST_SD_GENDISK sd_gendisks[N_USED_SD_MAJORS - 1]
507 static void sd_geninit(struct gendisk *ignored)
509 int i;
511 for (i = 0; i < sd_template.dev_max; ++i)
512 if (rscsi_disks[i].device)
513 sd[i << 4].nr_sects = rscsi_disks[i].capacity;
517 * rw_intr is the interrupt routine for the device driver.
518 * It will be notified on the end of a SCSI read / write, and
519 * will take one of several actions based on success or failure.
522 static void rw_intr(Scsi_Cmnd * SCpnt)
524 int result = SCpnt->result;
525 #if CONFIG_SCSI_LOGGING
526 char nbuff[6];
527 #endif
528 int this_count = SCpnt->bufflen >> 9;
529 int good_sectors = (result == 0 ? this_count : 0);
530 int block_sectors = 1;
532 SCSI_LOG_HLCOMPLETE(1, sd_devname(DEVICE_NR(SCpnt->request.rq_dev), nbuff));
534 SCSI_LOG_HLCOMPLETE(1, printk("%s : rw_intr(%d, %x [%x %x])\n", nbuff,
535 SCpnt->host->host_no,
536 result,
537 SCpnt->sense_buffer[0],
538 SCpnt->sense_buffer[2]));
541 Handle MEDIUM ERRORs that indicate partial success. Since this is a
542 relatively rare error condition, no care is taken to avoid
543 unnecessary additional work such as memcpy's that could be avoided.
546 /* An error occurred */
547 if (driver_byte(result) != 0) {
548 /* Sense data is valid */
549 if (SCpnt->sense_buffer[0] == 0xF0 && SCpnt->sense_buffer[2] == MEDIUM_ERROR) {
550 long error_sector = (SCpnt->sense_buffer[3] << 24) |
551 (SCpnt->sense_buffer[4] << 16) |
552 (SCpnt->sense_buffer[5] << 8) |
553 SCpnt->sense_buffer[6];
554 if (SCpnt->request.bh != NULL)
555 block_sectors = SCpnt->request.bh->b_size >> 9;
556 switch (SCpnt->device->sector_size) {
557 case 1024:
558 error_sector <<= 1;
559 if (block_sectors < 2)
560 block_sectors = 2;
561 break;
562 case 2048:
563 error_sector <<= 2;
564 if (block_sectors < 4)
565 block_sectors = 4;
566 break;
567 case 256:
568 error_sector >>= 1;
569 break;
570 default:
571 break;
573 error_sector -= sd[SD_PARTITION(SCpnt->request.rq_dev)].start_sect;
574 error_sector &= ~(block_sectors - 1);
575 good_sectors = error_sector - SCpnt->request.sector;
576 if (good_sectors < 0 || good_sectors >= this_count)
577 good_sectors = 0;
579 if (SCpnt->sense_buffer[2] == ILLEGAL_REQUEST) {
580 if (SCpnt->device->ten == 1) {
581 if (SCpnt->cmnd[0] == READ_10 ||
582 SCpnt->cmnd[0] == WRITE_10)
583 SCpnt->device->ten = 0;
588 * This calls the generic completion function, now that we know
589 * how many actual sectors finished, and how many sectors we need
590 * to say have failed.
592 scsi_io_completion(SCpnt, good_sectors, block_sectors);
595 * requeue_sd_request() is the request handler function for the sd driver.
596 * Its function in life is to take block device requests, and translate
597 * them to SCSI commands.
601 static int check_scsidisk_media_change(kdev_t full_dev)
603 int retval;
604 int target;
605 struct inode inode;
606 int flag = 0;
608 target = DEVICE_NR(full_dev);
610 if (target >= sd_template.dev_max ||
611 !rscsi_disks[target].device) {
612 printk("SCSI disk request error: invalid device.\n");
613 return 0;
615 if (!rscsi_disks[target].device->removable)
616 return 0;
619 * If the device is offline, don't send any commands - just pretend as
620 * if the command failed. If the device ever comes back online, we
621 * can deal with it then. It is only because of unrecoverable errors
622 * that we would ever take a device offline in the first place.
624 if (rscsi_disks[target].device->online == FALSE) {
625 rscsi_disks[target].ready = 0;
626 rscsi_disks[target].device->changed = 1;
627 return 1; /* This will force a flush, if called from
628 * check_disk_change */
630 inode.i_rdev = full_dev; /* This is all we really need here */
632 /* Using Start/Stop enables differentiation between drive with
633 * no cartridge loaded - NOT READY, drive with changed cartridge -
634 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
635 * This also handles drives that auto spin down. eg iomega jaz 1GB
636 * as this will spin up the drive.
638 retval = sd_ioctl(&inode, NULL, SCSI_IOCTL_START_UNIT, 0);
640 if (retval) { /* Unable to test, unit probably not ready.
641 * This usually means there is no disc in the
642 * drive. Mark as changed, and we will figure
643 * it out later once the drive is available
644 * again. */
646 rscsi_disks[target].ready = 0;
647 rscsi_disks[target].device->changed = 1;
648 return 1; /* This will force a flush, if called from
649 * check_disk_change */
652 * for removable scsi disk ( FLOPTICAL ) we have to recognise the
653 * presence of disk in the drive. This is kept in the Scsi_Disk
654 * struct and tested at open ! Daniel Roche ( dan@lectra.fr )
657 rscsi_disks[target].ready = 1; /* FLOPTICAL */
659 retval = rscsi_disks[target].device->changed;
660 if (!flag)
661 rscsi_disks[target].device->changed = 0;
662 return retval;
665 static void sd_init_done(Scsi_Cmnd * SCpnt)
667 struct request *req;
669 req = &SCpnt->request;
670 req->rq_status = RQ_SCSI_DONE; /* Busy, but indicate request done */
672 if (req->sem != NULL) {
673 up(req->sem);
676 static int sd_init_onedisk(int i)
678 unsigned char cmd[10];
679 char nbuff[6];
680 unsigned char *buffer;
681 unsigned long spintime_value = 0;
682 int the_result, retries, spintime;
683 int sector_size;
684 Scsi_Cmnd *SCpnt;
687 * Get the name of the disk, in case we need to log it somewhere.
689 sd_devname(i, nbuff);
692 * If the device is offline, don't try and read capacity or any of the other
693 * nicities.
695 if (rscsi_disks[i].device->online == FALSE) {
696 return i;
698 /* We need to retry the READ_CAPACITY because a UNIT_ATTENTION is
699 * considered a fatal error, and many devices report such an error
700 * just after a scsi bus reset.
703 SCpnt = scsi_allocate_device(rscsi_disks[i].device, 1, FALSE);
705 buffer = (unsigned char *) scsi_malloc(512);
707 spintime = 0;
709 /* Spin up drives, as required. Only do this at boot time */
710 /* Spinup needs to be done for module loads too. */
711 do {
712 retries = 0;
714 while (retries < 3) {
715 cmd[0] = TEST_UNIT_READY;
716 cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
717 memset((void *) &cmd[2], 0, 8);
718 SCpnt->cmd_len = 0;
719 SCpnt->sense_buffer[0] = 0;
720 SCpnt->sense_buffer[2] = 0;
722 scsi_wait_cmd (SCpnt, (void *) cmd, (void *) buffer,
723 0/*512*/, sd_init_done, SD_TIMEOUT, MAX_RETRIES);
725 the_result = SCpnt->result;
726 retries++;
727 if (the_result == 0
728 || SCpnt->sense_buffer[2] != UNIT_ATTENTION)
729 break;
732 /* Look for non-removable devices that return NOT_READY.
733 * Issue command to spin up drive for these cases. */
734 if (the_result && !rscsi_disks[i].device->removable &&
735 SCpnt->sense_buffer[2] == NOT_READY) {
736 unsigned long time1;
737 if (!spintime) {
738 printk("%s: Spinning up disk...", nbuff);
739 cmd[0] = START_STOP;
740 cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
741 cmd[1] |= 1; /* Return immediately */
742 memset((void *) &cmd[2], 0, 8);
743 cmd[4] = 1; /* Start spin cycle */
744 SCpnt->cmd_len = 0;
745 SCpnt->sense_buffer[0] = 0;
746 SCpnt->sense_buffer[2] = 0;
748 scsi_wait_cmd(SCpnt, (void *) cmd, (void *) buffer,
749 512, sd_init_done, SD_TIMEOUT, MAX_RETRIES);
751 spintime = 1;
752 spintime_value = jiffies;
753 time1 = HZ;
754 /* Wait 1 second for next try */
755 do {
756 current->state = TASK_UNINTERRUPTIBLE;
757 time1 = schedule_timeout(time1);
758 } while(time1);
759 printk(".");
761 } while (the_result && spintime && time_after(spintime_value + 100 * HZ, jiffies));
762 if (spintime) {
763 if (the_result)
764 printk("not responding...\n");
765 else
766 printk("ready\n");
768 retries = 3;
769 do {
770 cmd[0] = READ_CAPACITY;
771 cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
772 memset((void *) &cmd[2], 0, 8);
773 memset((void *) buffer, 0, 8);
774 SCpnt->cmd_len = 0;
775 SCpnt->sense_buffer[0] = 0;
776 SCpnt->sense_buffer[2] = 0;
778 scsi_wait_cmd(SCpnt, (void *) cmd, (void *) buffer,
779 8, sd_init_done, SD_TIMEOUT, MAX_RETRIES);
781 the_result = SCpnt->result;
782 retries--;
784 } while (the_result && retries);
787 * The SCSI standard says:
788 * "READ CAPACITY is necessary for self configuring software"
789 * While not mandatory, support of READ CAPACITY is strongly encouraged.
790 * We used to die if we couldn't successfully do a READ CAPACITY.
791 * But, now we go on about our way. The side effects of this are
793 * 1. We can't know block size with certainty. I have said "512 bytes
794 * is it" as this is most common.
796 * 2. Recovery from when some one attempts to read past the end of the
797 * raw device will be slower.
800 if (the_result) {
801 printk("%s : READ CAPACITY failed.\n"
802 "%s : status = %x, message = %02x, host = %d, driver = %02x \n",
803 nbuff, nbuff,
804 status_byte(the_result),
805 msg_byte(the_result),
806 host_byte(the_result),
807 driver_byte(the_result)
809 if (driver_byte(the_result) & DRIVER_SENSE)
810 printk("%s : extended sense code = %1x \n",
811 nbuff, SCpnt->sense_buffer[2] & 0xf);
812 else
813 printk("%s : sense not available. \n", nbuff);
815 printk("%s : block size assumed to be 512 bytes, disk size 1GB. \n",
816 nbuff);
817 rscsi_disks[i].capacity = 0x1fffff;
818 sector_size = 512;
820 /* Set dirty bit for removable devices if not ready - sometimes drives
821 * will not report this properly. */
822 if (rscsi_disks[i].device->removable &&
823 SCpnt->sense_buffer[2] == NOT_READY)
824 rscsi_disks[i].device->changed = 1;
826 } else {
828 * FLOPTICAL , if read_capa is ok , drive is assumed to be ready
830 rscsi_disks[i].ready = 1;
832 rscsi_disks[i].capacity = 1 + ((buffer[0] << 24) |
833 (buffer[1] << 16) |
834 (buffer[2] << 8) |
835 buffer[3]);
837 sector_size = (buffer[4] << 24) |
838 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
840 if (sector_size == 0) {
841 sector_size = 512;
842 printk("%s : sector size 0 reported, assuming 512.\n", nbuff);
844 if (sector_size != 512 &&
845 sector_size != 1024 &&
846 sector_size != 2048 &&
847 sector_size != 4096 &&
848 sector_size != 256) {
849 printk("%s : unsupported sector size %d.\n",
850 nbuff, sector_size);
852 * The user might want to re-format the drive with
853 * a supported sectorsize. Once this happens, it
854 * would be relatively trivial to set the thing up.
855 * For this reason, we leave the thing in the table.
857 rscsi_disks[i].capacity = 0;
859 if (sector_size == 2048) {
860 int m;
863 * We must fix the sd_blocksizes and sd_hardsizes
864 * to allow us to read the partition tables.
865 * The disk reading code does not allow for reading
866 * of partial sectors.
868 for (m = i << 4; m < ((i + 1) << 4); m++) {
869 sd_blocksizes[m] = 2048;
873 * The msdos fs needs to know the hardware sector size
874 * So I have created this table. See ll_rw_blk.c
875 * Jacques Gelinas (Jacques@solucorp.qc.ca)
877 int m, mb;
878 int sz_quot, sz_rem;
879 int hard_sector = sector_size;
880 /* There are 16 minors allocated for each major device */
881 for (m = i << 4; m < ((i + 1) << 4); m++) {
882 sd_hardsizes[m] = hard_sector;
884 mb = rscsi_disks[i].capacity / 1024 * hard_sector / 1024;
885 /* sz = div(m/100, 10); this seems to not be in the libr */
886 m = (mb + 50) / 100;
887 sz_quot = m / 10;
888 sz_rem = m - (10 * sz_quot);
889 printk("SCSI device %s: hdwr sector= %d bytes."
890 " Sectors= %d [%d MB] [%d.%1d GB]\n",
891 nbuff, hard_sector, rscsi_disks[i].capacity,
892 mb, sz_quot, sz_rem);
894 if (sector_size == 4096)
895 rscsi_disks[i].capacity <<= 3;
896 if (sector_size == 2048)
897 rscsi_disks[i].capacity <<= 2; /* Change into 512 byte sectors */
898 if (sector_size == 1024)
899 rscsi_disks[i].capacity <<= 1; /* Change into 512 byte sectors */
900 if (sector_size == 256)
901 rscsi_disks[i].capacity >>= 1; /* Change into 512 byte sectors */
906 * Unless otherwise specified, this is not write protected.
908 rscsi_disks[i].write_prot = 0;
909 if (rscsi_disks[i].device->removable && rscsi_disks[i].ready) {
910 /* FLOPTICAL */
913 * for removable scsi disk ( FLOPTICAL ) we have to recognise
914 * the Write Protect Flag. This flag is kept in the Scsi_Disk struct
915 * and tested at open !
916 * Daniel Roche ( dan@lectra.fr )
919 memset((void *) &cmd[0], 0, 8);
920 cmd[0] = MODE_SENSE;
921 cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
922 cmd[2] = 1; /* page code 1 ?? */
923 cmd[4] = 12;
924 SCpnt->cmd_len = 0;
925 SCpnt->sense_buffer[0] = 0;
926 SCpnt->sense_buffer[2] = 0;
928 /* same code as READCAPA !! */
929 scsi_wait_cmd(SCpnt, (void *) cmd, (void *) buffer,
930 512, sd_init_done, SD_TIMEOUT, MAX_RETRIES);
932 the_result = SCpnt->result;
934 if (the_result) {
935 printk("%s: test WP failed, assume Write Protected\n", nbuff);
936 rscsi_disks[i].write_prot = 1;
937 } else {
938 rscsi_disks[i].write_prot = ((buffer[2] & 0x80) != 0);
939 printk("%s: Write Protect is %s\n", nbuff,
940 rscsi_disks[i].write_prot ? "on" : "off");
943 } /* check for write protect */
944 SCpnt->device->ten = 1;
945 SCpnt->device->remap = 1;
946 SCpnt->device->sector_size = sector_size;
947 /* Wake up a process waiting for device */
948 scsi_release_command(SCpnt);
949 SCpnt = NULL;
951 scsi_free(buffer, 512);
952 return i;
956 * The sd_init() function looks at all SCSI drives present, determines
957 * their size, and reads partition table entries for them.
960 static int sd_registered = 0;
962 static int sd_init()
964 int i;
966 if (sd_template.dev_noticed == 0)
967 return 0;
969 if (!rscsi_disks)
970 sd_template.dev_max = sd_template.dev_noticed + SD_EXTRA_DEVS;
972 if (sd_template.dev_max > N_SD_MAJORS * SCSI_DISKS_PER_MAJOR)
973 sd_template.dev_max = N_SD_MAJORS * SCSI_DISKS_PER_MAJOR;
975 if (!sd_registered) {
976 for (i = 0; i <= (sd_template.dev_max - 1) / SCSI_DISKS_PER_MAJOR; i++) {
977 if (register_blkdev(SD_MAJOR(i), "sd", &sd_fops)) {
978 printk("Unable to get major %d for SCSI disk\n", SD_MAJOR(i));
979 return 1;
982 sd_registered++;
984 /* We do not support attaching loadable devices yet. */
985 if (rscsi_disks)
986 return 0;
988 rscsi_disks = (Scsi_Disk *)
989 scsi_init_malloc(sd_template.dev_max * sizeof(Scsi_Disk), GFP_ATOMIC);
990 memset(rscsi_disks, 0, sd_template.dev_max * sizeof(Scsi_Disk));
992 /* for every (necessary) major: */
993 sd_sizes = (int *) scsi_init_malloc((sd_template.dev_max << 4) * sizeof(int), GFP_ATOMIC);
994 memset(sd_sizes, 0, (sd_template.dev_max << 4) * sizeof(int));
996 sd_blocksizes = (int *) scsi_init_malloc((sd_template.dev_max << 4) * sizeof(int), GFP_ATOMIC);
997 sd_hardsizes = (int *) scsi_init_malloc((sd_template.dev_max << 4) * sizeof(int), GFP_ATOMIC);
999 for (i = 0; i < sd_template.dev_max << 4; i++) {
1000 sd_blocksizes[i] = 1024;
1001 sd_hardsizes[i] = 512;
1004 for (i = 0; i < N_USED_SD_MAJORS; i++) {
1005 blksize_size[SD_MAJOR(i)] = sd_blocksizes + i * (SCSI_DISKS_PER_MAJOR << 4);
1006 hardsect_size[SD_MAJOR(i)] = sd_hardsizes + i * (SCSI_DISKS_PER_MAJOR << 4);
1008 sd = (struct hd_struct *) scsi_init_malloc((sd_template.dev_max << 4) *
1009 sizeof(struct hd_struct),
1010 GFP_ATOMIC);
1012 if (N_USED_SD_MAJORS > 1)
1013 sd_gendisks = (struct gendisk *)
1014 kmalloc(N_USED_SD_MAJORS * sizeof(struct gendisk), GFP_ATOMIC);
1015 for (i = 0; i < N_USED_SD_MAJORS; i++) {
1016 sd_gendisks[i].major = SD_MAJOR(i);
1017 sd_gendisks[i].major_name = "sd";
1018 sd_gendisks[i].minor_shift = 4;
1019 sd_gendisks[i].max_p = 1 << 4;
1020 sd_gendisks[i].max_nr = SCSI_DISKS_PER_MAJOR;
1021 sd_gendisks[i].init = sd_geninit;
1022 sd_gendisks[i].part = sd + (i * SCSI_DISKS_PER_MAJOR << 4);
1023 sd_gendisks[i].sizes = sd_sizes + (i * SCSI_DISKS_PER_MAJOR << 4);
1024 sd_gendisks[i].nr_real = 0;
1025 sd_gendisks[i].next = sd_gendisks + i + 1;
1026 sd_gendisks[i].real_devices =
1027 (void *) (rscsi_disks + i * SCSI_DISKS_PER_MAJOR);
1030 LAST_SD_GENDISK.max_nr =
1031 (sd_template.dev_max - 1) % SCSI_DISKS_PER_MAJOR + 1;
1032 LAST_SD_GENDISK.next = NULL;
1033 return 0;
1037 static void sd_finish()
1039 struct gendisk *gendisk;
1040 int i;
1042 for (i = 0; i <= (sd_template.dev_max - 1) / SCSI_DISKS_PER_MAJOR; i++) {
1043 blk_dev[SD_MAJOR(i)].queue = sd_find_queue;
1045 for (gendisk = gendisk_head; gendisk != NULL; gendisk = gendisk->next)
1046 if (gendisk == sd_gendisks)
1047 break;
1048 if (gendisk == NULL) {
1049 LAST_SD_GENDISK.next = gendisk_head;
1050 gendisk_head = sd_gendisks;
1052 for (i = 0; i < sd_template.dev_max; ++i)
1053 if (!rscsi_disks[i].capacity &&
1054 rscsi_disks[i].device) {
1055 if (MODULE_FLAG
1056 && !rscsi_disks[i].has_part_table) {
1057 sd_sizes[i << 4] = rscsi_disks[i].capacity;
1058 /* revalidate does sd_init_onedisk via MAYBE_REINIT */
1059 revalidate_scsidisk(MKDEV_SD(i), 0);
1060 } else
1061 i = sd_init_onedisk(i);
1062 rscsi_disks[i].has_part_table = 1;
1064 /* If our host adapter is capable of scatter-gather, then we increase
1065 * the read-ahead to 60 blocks (120 sectors). If not, we use
1066 * a two block (4 sector) read ahead. We can only respect this with the
1067 * granularity of every 16 disks (one device major).
1069 for (i = 0; i < N_USED_SD_MAJORS; i++) {
1070 read_ahead[SD_MAJOR(i)] =
1071 (rscsi_disks[i * SCSI_DISKS_PER_MAJOR].device
1072 && rscsi_disks[i * SCSI_DISKS_PER_MAJOR].device->host->sg_tablesize)
1073 ? 120 /* 120 sector read-ahead */
1074 : 4; /* 4 sector read-ahead */
1077 return;
1080 static int sd_detect(Scsi_Device * SDp)
1082 char nbuff[6];
1083 if (SDp->type != TYPE_DISK && SDp->type != TYPE_MOD)
1084 return 0;
1086 sd_devname(sd_template.dev_noticed++, nbuff);
1087 printk("Detected scsi %sdisk %s at scsi%d, channel %d, id %d, lun %d\n",
1088 SDp->removable ? "removable " : "",
1089 nbuff,
1090 SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
1092 return 1;
1094 static int sd_attach(Scsi_Device * SDp)
1096 Scsi_Disk *dpnt;
1097 int i;
1099 if (SDp->type != TYPE_DISK && SDp->type != TYPE_MOD)
1100 return 0;
1102 if (sd_template.nr_dev >= sd_template.dev_max) {
1103 SDp->attached--;
1104 return 1;
1106 for (dpnt = rscsi_disks, i = 0; i < sd_template.dev_max; i++, dpnt++)
1107 if (!dpnt->device)
1108 break;
1110 if (i >= sd_template.dev_max)
1111 panic("scsi_devices corrupt (sd)");
1113 rscsi_disks[i].device = SDp;
1114 rscsi_disks[i].has_part_table = 0;
1115 sd_template.nr_dev++;
1116 SD_GENDISK(i).nr_real++;
1117 return 0;
1120 #define DEVICE_BUSY rscsi_disks[target].device->busy
1121 #define USAGE rscsi_disks[target].device->access_count
1122 #define CAPACITY rscsi_disks[target].capacity
1123 #define MAYBE_REINIT sd_init_onedisk(target)
1125 /* This routine is called to flush all partitions and partition tables
1126 * for a changed scsi disk, and then re-read the new partition table.
1127 * If we are revalidating a disk because of a media change, then we
1128 * enter with usage == 0. If we are using an ioctl, we automatically have
1129 * usage == 1 (we need an open channel to use an ioctl :-), so this
1130 * is our limit.
1132 int revalidate_scsidisk(kdev_t dev, int maxusage)
1134 int target;
1135 int max_p;
1136 int start;
1137 int i;
1139 target = DEVICE_NR(dev);
1141 if (DEVICE_BUSY || USAGE > maxusage) {
1142 printk("Device busy for revalidation (usage=%d)\n", USAGE);
1143 return -EBUSY;
1145 DEVICE_BUSY = 1;
1147 max_p = sd_gendisks->max_p;
1148 start = target << sd_gendisks->minor_shift;
1150 for (i = max_p - 1; i >= 0; i--) {
1151 int index = start + i;
1152 kdev_t devi = MKDEV_SD_PARTITION(index);
1153 struct super_block *sb = get_super(devi);
1154 sync_dev(devi);
1155 if (sb)
1156 invalidate_inodes(sb);
1157 invalidate_buffers(devi);
1158 sd_gendisks->part[index].start_sect = 0;
1159 sd_gendisks->part[index].nr_sects = 0;
1161 * Reset the blocksize for everything so that we can read
1162 * the partition table. Technically we will determine the
1163 * correct block size when we revalidate, but we do this just
1164 * to make sure that everything remains consistent.
1166 sd_blocksizes[index] = 1024;
1167 if (rscsi_disks[target].device->sector_size == 2048)
1168 sd_blocksizes[index] = 2048;
1169 else
1170 sd_blocksizes[index] = 1024;
1173 #ifdef MAYBE_REINIT
1174 MAYBE_REINIT;
1175 #endif
1177 sd_gendisks->part[start].nr_sects = CAPACITY;
1178 if (!rscsi_disks[target].device)
1179 return -EBUSY;
1180 resetup_one_dev(&SD_GENDISK(target),
1181 target % SCSI_DISKS_PER_MAJOR);
1183 DEVICE_BUSY = 0;
1184 return 0;
1187 static int fop_revalidate_scsidisk(kdev_t dev)
1189 return revalidate_scsidisk(dev, 0);
1191 static void sd_detach(Scsi_Device * SDp)
1193 Scsi_Disk *dpnt;
1194 int i, j;
1195 int max_p;
1196 int start;
1198 for (dpnt = rscsi_disks, i = 0; i < sd_template.dev_max; i++, dpnt++)
1199 if (dpnt->device == SDp) {
1201 /* If we are disconnecting a disk driver, sync and invalidate
1202 * everything */
1203 max_p = sd_gendisk.max_p;
1204 start = i << sd_gendisk.minor_shift;
1206 for (j = max_p - 1; j >= 0; j--) {
1207 int index = start + j;
1208 kdev_t devi = MKDEV_SD_PARTITION(index);
1209 struct super_block *sb = get_super(devi);
1210 sync_dev(devi);
1211 if (sb)
1212 invalidate_inodes(sb);
1213 invalidate_buffers(devi);
1214 sd_gendisks->part[index].start_sect = 0;
1215 sd_gendisks->part[index].nr_sects = 0;
1216 sd_sizes[index] = 0;
1218 dpnt->has_part_table = 0;
1219 dpnt->device = NULL;
1220 dpnt->capacity = 0;
1221 SDp->attached--;
1222 sd_template.dev_noticed--;
1223 sd_template.nr_dev--;
1224 SD_GENDISK(i).nr_real--;
1225 return;
1227 return;
1230 #ifdef MODULE
1232 int init_module(void)
1234 sd_template.module = &__this_module;
1235 return scsi_register_module(MODULE_SCSI_DEV, &sd_template);
1237 void cleanup_module(void)
1239 struct gendisk **prev_sdgd_link;
1240 struct gendisk *sdgd;
1241 int i;
1242 int removed = 0;
1244 scsi_unregister_module(MODULE_SCSI_DEV, &sd_template);
1246 for (i = 0; i <= (sd_template.dev_max - 1) / SCSI_DISKS_PER_MAJOR; i++)
1247 unregister_blkdev(SD_MAJOR(i), "sd");
1249 sd_registered--;
1250 if (rscsi_disks != NULL) {
1251 scsi_init_free((char *) rscsi_disks,
1252 sd_template.dev_max * sizeof(Scsi_Disk));
1253 scsi_init_free((char *) sd_sizes, sd_template.dev_max * sizeof(int));
1254 scsi_init_free((char *) sd_blocksizes, sd_template.dev_max * sizeof(int));
1255 scsi_init_free((char *) sd_hardsizes, sd_template.dev_max * sizeof(int));
1256 scsi_init_free((char *) sd,
1257 (sd_template.dev_max << 4) * sizeof(struct hd_struct));
1260 * Now remove sd_gendisks from the linked list
1262 prev_sdgd_link = &gendisk_head;
1263 while ((sdgd = *prev_sdgd_link) != NULL) {
1264 if (sdgd >= sd_gendisks && sdgd <= &LAST_SD_GENDISK) {
1265 removed++;
1266 *prev_sdgd_link = sdgd->next;
1267 continue;
1269 prev_sdgd_link = &sdgd->next;
1272 if (removed != N_USED_SD_MAJORS)
1273 printk("%s %d sd_gendisks in disk chain",
1274 removed > N_USED_SD_MAJORS ? "total" : "just", removed);
1277 for (i = 0; i <= (sd_template.dev_max - 1) / SCSI_DISKS_PER_MAJOR; i++) {
1278 blk_cleanup_queue(BLK_DEFAULT_QUEUE(SD_MAJOR(i)));
1279 blk_size[SD_MAJOR(i)] = NULL;
1280 hardsect_size[SD_MAJOR(i)] = NULL;
1281 read_ahead[SD_MAJOR(i)] = 0;
1283 sd_template.dev_max = 0;
1284 if (sd_gendisks != &sd_gendisk)
1285 kfree(sd_gendisks);
1287 #endif /* MODULE */
1290 * Overrides for Emacs so that we almost follow Linus's tabbing style.
1291 * Emacs will notice this stuff at the end of the file and automatically
1292 * adjust the settings for this buffer only. This must remain at the end
1293 * of the file.
1294 * ---------------------------------------------------------------------------
1295 * Local variables:
1296 * c-indent-level: 4
1297 * c-brace-imaginary-offset: 0
1298 * c-brace-offset: -4
1299 * c-argdecl-indent: 4
1300 * c-label-offset: -4
1301 * c-continued-statement-offset: 4
1302 * c-continued-brace-offset: 0
1303 * indent-tabs-mode: nil
1304 * tab-width: 8
1305 * End: