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
8 * Modification history:
9 * - Drew Eckhardt <drew@colorado.edu> original
10 * - Eric Youngdale <eric@andante.org> add scatter-gather, multiple
11 * outstanding request, and other enhancements.
12 * Support loadable low-level scsi drivers.
13 * - Jirka Hanika <geo@ff.cuni.cz> support more scsi disks using
14 * eight major numbers.
15 * - Richard Gooch <rgooch@atnf.csiro.au> support devfs.
16 * - Torben Mathiasen <tmm@image.dk> Resource allocation fixes in
17 * sd_init and cleanups.
18 * - Alex Davis <letmein@erols.com> Fix problem where partition info
19 * not being read in sd_open. Fix problem where removable media
20 * could be ejected after sd_open.
21 * - Douglas Gilbert <dgilbert@interlog.com> cleanup for lk 2.5.x
22 * - Badari Pulavarty <pbadari@us.ibm.com>, Matthew Wilcox
23 * <willy@debian.org>, Kurt Garloff <garloff@suse.de>:
24 * Support 32k/1M disks.
26 * Logging policy (needs CONFIG_SCSI_LOGGING defined):
27 * - setting up transfer: SCSI_LOG_HLQUEUE levels 1 and 2
28 * - end of transfer (bh + scsi_lib): SCSI_LOG_HLCOMPLETE level 1
29 * - entering sd_ioctl: SCSI_LOG_IOCTL level 1
30 * - entering other commands: SCSI_LOG_HLQUEUE level 3
31 * Note: when the logging level is set by the user, it must be greater
32 * than the level indicated above to trigger output.
35 #include <linux/config.h>
36 #include <linux/module.h>
38 #include <linux/kernel.h>
39 #include <linux/sched.h>
41 #include <linux/bio.h>
42 #include <linux/genhd.h>
43 #include <linux/hdreg.h>
44 #include <linux/errno.h>
45 #include <linux/interrupt.h>
46 #include <linux/init.h>
47 #include <linux/reboot.h>
48 #include <linux/vmalloc.h>
49 #include <linux/blkdev.h>
50 #include <linux/blkpg.h>
51 #include <linux/kref.h>
52 #include <asm/uaccess.h>
57 #include <scsi/scsi_driver.h>
58 #include <scsi/scsi_ioctl.h>
59 #include <scsi/scsicam.h>
61 #include "scsi_logging.h"
65 * Remaining dev_t-handling stuff
68 #define SD_DISKS 32768 /* anything between 256 and 262144 */
71 * Time out in seconds for disks and Magneto-opticals (which are slower).
73 #define SD_TIMEOUT (30 * HZ)
74 #define SD_MOD_TIMEOUT (75 * HZ)
77 * Number of allowed retries
79 #define SD_MAX_RETRIES 5
81 static void scsi_disk_release(struct kref *kref);
84 struct scsi_driver *driver; /* always &sd_template */
85 struct scsi_device *device;
88 unsigned int openers; /* protected by BKL for now, yuck */
89 sector_t capacity; /* size in 512-byte sectors */
93 unsigned WCE : 1; /* state of disk WCE bit */
94 unsigned RCD : 1; /* state of disk RCD bit, unused */
98 static unsigned long sd_index_bits[SD_DISKS / BITS_PER_LONG];
99 static spinlock_t sd_index_lock = SPIN_LOCK_UNLOCKED;
101 /* This semaphore is used to mediate the 0->1 reference get in the
102 * face of object destruction (i.e. we can't allow a get on an
103 * object after last put) */
104 static DECLARE_MUTEX(sd_ref_sem);
106 static int sd_revalidate_disk(struct gendisk *disk);
107 static void sd_rw_intr(struct scsi_cmnd * SCpnt);
109 static int sd_probe(struct device *);
110 static int sd_remove(struct device *);
111 static void sd_shutdown(struct device *dev);
112 static void sd_rescan(struct device *);
113 static int sd_init_command(struct scsi_cmnd *);
114 static void sd_read_capacity(struct scsi_disk *sdkp, char *diskname,
115 struct scsi_request *SRpnt, unsigned char *buffer);
117 static struct scsi_driver sd_template = {
118 .owner = THIS_MODULE,
123 .shutdown = sd_shutdown,
126 .init_command = sd_init_command,
129 /* Device no to disk mapping:
131 * major disc2 disc p1
132 * |............|.............|....|....| <- dev_t
135 * Inside a major, we have 16k disks, however mapped non-
136 * contiguously. The first 16 disks are for major0, the next
137 * ones with major1, ... Disk 256 is for major0 again, disk 272
139 * As we stay compatible with our numbering scheme, we can reuse
140 * the well-know SCSI majors 8, 65--71, 136--143.
143 static int sd_major(int major_idx)
147 return SCSI_DISK0_MAJOR;
149 return SCSI_DISK1_MAJOR + major_idx - 1;
151 return SCSI_DISK8_MAJOR + major_idx - 8;
154 return 0; /* shut up gcc */
158 static unsigned int make_sd_dev(unsigned int sd_nr, unsigned int part)
160 return (part & 0xf) | ((sd_nr & 0xf) << 4) |
161 (sd_major((sd_nr & 0xf0) >> 4) << 20) | (sd_nr & 0xfff00);
164 /* reverse mapping dev -> (sd_nr, part) not currently needed */
166 #define to_scsi_disk(obj) container_of(obj,struct scsi_disk,kref)
168 static inline struct scsi_disk *scsi_disk(struct gendisk *disk)
170 return container_of(disk->private_data, struct scsi_disk, driver);
173 static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
175 struct scsi_disk *sdkp = NULL;
178 if (disk->private_data == NULL)
180 sdkp = scsi_disk(disk);
181 if (!kref_get(&sdkp->kref))
188 static void scsi_disk_put(struct scsi_disk *sdkp)
191 kref_put(&sdkp->kref);
196 * sd_init_command - build a scsi (read or write) command from
197 * information in the request structure.
198 * @SCpnt: pointer to mid-level's per scsi command structure that
199 * contains request and into which the scsi command is written
201 * Returns 1 if successful and 0 if error (or cannot be done now).
203 static int sd_init_command(struct scsi_cmnd * SCpnt)
205 unsigned int this_count, timeout;
206 struct gendisk *disk;
208 struct scsi_device *sdp = SCpnt->device;
210 timeout = SD_TIMEOUT;
211 if (SCpnt->device->type != TYPE_DISK)
212 timeout = SD_MOD_TIMEOUT;
215 * these are already setup, just copy cdb basically
217 if (SCpnt->request->flags & REQ_BLOCK_PC) {
218 struct request *rq = SCpnt->request;
220 if (sizeof(rq->cmd) > sizeof(SCpnt->cmnd))
223 memcpy(SCpnt->cmnd, rq->cmd, sizeof(SCpnt->cmnd));
224 if (rq_data_dir(rq) == WRITE)
225 SCpnt->sc_data_direction = DMA_TO_DEVICE;
226 else if (rq->data_len)
227 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
229 SCpnt->sc_data_direction = DMA_NONE;
231 this_count = rq->data_len;
233 timeout = rq->timeout;
235 SCpnt->transfersize = rq->data_len;
240 * we only do REQ_CMD and REQ_BLOCK_PC
242 if (!(SCpnt->request->flags & REQ_CMD))
245 disk = SCpnt->request->rq_disk;
246 block = SCpnt->request->sector;
247 this_count = SCpnt->request_bufflen >> 9;
249 SCSI_LOG_HLQUEUE(1, printk("sd_init_command: disk=%s, block=%llu, "
250 "count=%d\n", disk->disk_name, (unsigned long long)block, this_count));
252 if (!sdp || !scsi_device_online(sdp) ||
253 block + SCpnt->request->nr_sectors > get_capacity(disk)) {
254 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
255 SCpnt->request->nr_sectors));
256 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
262 * quietly refuse to do anything to a changed disc until
263 * the changed bit has been reset
265 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
268 SCSI_LOG_HLQUEUE(2, printk("%s : block=%llu\n",
269 disk->disk_name, (unsigned long long)block));
272 * If we have a 1K hardware sectorsize, prevent access to single
273 * 512 byte sectors. In theory we could handle this - in fact
274 * the scsi cdrom driver must be able to handle this because
275 * we typically use 1K blocksizes, and cdroms typically have
276 * 2K hardware sectorsizes. Of course, things are simpler
277 * with the cdrom, since it is read-only. For performance
278 * reasons, the filesystems should be able to handle this
279 * and not force the scsi disk driver to use bounce buffers
282 if (sdp->sector_size == 1024) {
283 if ((block & 1) || (SCpnt->request->nr_sectors & 1)) {
284 printk(KERN_ERR "sd: Bad block number requested");
288 this_count = this_count >> 1;
291 if (sdp->sector_size == 2048) {
292 if ((block & 3) || (SCpnt->request->nr_sectors & 3)) {
293 printk(KERN_ERR "sd: Bad block number requested");
297 this_count = this_count >> 2;
300 if (sdp->sector_size == 4096) {
301 if ((block & 7) || (SCpnt->request->nr_sectors & 7)) {
302 printk(KERN_ERR "sd: Bad block number requested");
306 this_count = this_count >> 3;
309 if (rq_data_dir(SCpnt->request) == WRITE) {
310 if (!sdp->writeable) {
313 SCpnt->cmnd[0] = WRITE_6;
314 SCpnt->sc_data_direction = DMA_TO_DEVICE;
315 } else if (rq_data_dir(SCpnt->request) == READ) {
316 SCpnt->cmnd[0] = READ_6;
317 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
319 printk(KERN_ERR "sd: Unknown command %lx\n",
320 SCpnt->request->flags);
321 /* overkill panic("Unknown sd command %lx\n", SCpnt->request->flags); */
325 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
326 disk->disk_name, (rq_data_dir(SCpnt->request) == WRITE) ?
327 "writing" : "reading", this_count, SCpnt->request->nr_sectors));
331 if (block > 0xffffffff) {
332 SCpnt->cmnd[0] += READ_16 - READ_6;
333 SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
334 SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
335 SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
336 SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
337 SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
338 SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
339 SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
340 SCpnt->cmnd[9] = (unsigned char) block & 0xff;
341 SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
342 SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
343 SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
344 SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
345 SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
346 } else if ((this_count > 0xff) || (block > 0x1fffff) ||
347 SCpnt->device->use_10_for_rw) {
348 if (this_count > 0xffff)
351 SCpnt->cmnd[0] += READ_10 - READ_6;
352 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
353 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
354 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
355 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
356 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
357 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
358 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
360 if (this_count > 0xff)
363 SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
364 SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
365 SCpnt->cmnd[3] = (unsigned char) block & 0xff;
366 SCpnt->cmnd[4] = (unsigned char) this_count;
369 SCpnt->request_bufflen = SCpnt->bufflen =
370 this_count * sdp->sector_size;
373 * We shouldn't disconnect in the middle of a sector, so with a dumb
374 * host adapter, it's safe to assume that we can at least transfer
375 * this many bytes between each connect / disconnect.
377 SCpnt->transfersize = sdp->sector_size;
378 SCpnt->underflow = this_count << 9;
381 SCpnt->allowed = SD_MAX_RETRIES;
382 SCpnt->timeout_per_command = timeout;
385 * This is the completion routine we use. This is matched in terms
386 * of capability to this function.
388 SCpnt->done = sd_rw_intr;
391 * This indicates that the command is ready from our end to be
398 * sd_open - open a scsi disk device
399 * @inode: only i_rdev member may be used
400 * @filp: only f_mode and f_flags may be used
402 * Returns 0 if successful. Returns a negated errno value in case
405 * Note: This can be called from a user context (e.g. fsck(1) )
406 * or from within the kernel (e.g. as a result of a mount(1) ).
407 * In the latter case @inode and @filp carry an abridged amount
408 * of information as noted above.
410 static int sd_open(struct inode *inode, struct file *filp)
412 struct gendisk *disk = inode->i_bdev->bd_disk;
413 struct scsi_disk *sdkp;
414 struct scsi_device *sdev;
417 if (!(sdkp = scsi_disk_get(disk)))
421 SCSI_LOG_HLQUEUE(3, printk("sd_open: disk=%s\n", disk->disk_name));
426 * If the device is in error recovery, wait until it is done.
427 * If the device is offline, then disallow any access to it.
430 if (!scsi_block_when_processing_errors(sdev))
433 if (sdev->removable || sdkp->write_prot)
434 check_disk_change(inode->i_bdev);
437 * If the drive is empty, just let the open fail.
440 if (sdev->removable && !sdkp->media_present &&
441 !(filp->f_flags & O_NDELAY))
445 * If the device has the write protect tab set, have the open fail
446 * if the user expects to be able to write to the thing.
449 if (sdkp->write_prot && (filp->f_mode & FMODE_WRITE))
453 * It is possible that the disk changing stuff resulted in
454 * the device being taken offline. If this is the case,
455 * report this to the user, and don't pretend that the
456 * open actually succeeded.
459 if (!scsi_device_online(sdev))
462 if (!sdkp->openers++ && sdev->removable) {
463 if (scsi_block_when_processing_errors(sdev))
464 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
475 * sd_release - invoked when the (last) close(2) is called on this
477 * @inode: only i_rdev member may be used
478 * @filp: only f_mode and f_flags may be used
482 * Note: may block (uninterruptible) if error recovery is underway
485 static int sd_release(struct inode *inode, struct file *filp)
487 struct gendisk *disk = inode->i_bdev->bd_disk;
488 struct scsi_disk *sdkp = scsi_disk(disk);
489 struct scsi_device *sdev = sdkp->device;
491 SCSI_LOG_HLQUEUE(3, printk("sd_release: disk=%s\n", disk->disk_name));
493 if (!--sdkp->openers && sdev->removable) {
494 if (scsi_block_when_processing_errors(sdev))
495 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
499 * XXX and what if there are packets in flight and this close()
500 * XXX is followed by a "rmmod sd_mod"?
506 static int sd_hdio_getgeo(struct block_device *bdev, struct hd_geometry *loc)
508 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
509 struct scsi_device *sdp = sdkp->device;
510 struct Scsi_Host *host = sdp->host;
513 /* default to most commonly used values */
514 diskinfo[0] = 0x40; /* 1 << 6 */
515 diskinfo[1] = 0x20; /* 1 << 5 */
516 diskinfo[2] = sdkp->capacity >> 11;
518 /* override with calculated, extended default, or driver values */
519 if (host->hostt->bios_param)
520 host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo);
522 scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
524 if (put_user(diskinfo[0], &loc->heads))
526 if (put_user(diskinfo[1], &loc->sectors))
528 if (put_user(diskinfo[2], &loc->cylinders))
530 if (put_user((unsigned)get_start_sect(bdev),
531 (unsigned long *)&loc->start))
537 * sd_ioctl - process an ioctl
538 * @inode: only i_rdev/i_bdev members may be used
539 * @filp: only f_mode and f_flags may be used
540 * @cmd: ioctl command number
541 * @arg: this is third argument given to ioctl(2) system call.
542 * Often contains a pointer.
544 * Returns 0 if successful (some ioctls return postive numbers on
545 * success as well). Returns a negated errno value in case of error.
547 * Note: most ioctls are forward onto the block subsystem or further
548 * down in the scsi subsytem.
550 static int sd_ioctl(struct inode * inode, struct file * filp,
551 unsigned int cmd, unsigned long arg)
553 struct block_device *bdev = inode->i_bdev;
554 struct gendisk *disk = bdev->bd_disk;
555 struct scsi_device *sdp = scsi_disk(disk)->device;
558 SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
559 disk->disk_name, cmd));
562 * If we are in the middle of error recovery, don't let anyone
563 * else try and use this device. Also, if error recovery fails, it
564 * may try and take the device offline, in which case all further
565 * access to the device is prohibited.
567 if (!scsi_block_when_processing_errors(sdp))
570 if (cmd == HDIO_GETGEO) {
573 return sd_hdio_getgeo(bdev, (struct hd_geometry *)arg);
577 * Send SCSI addressing ioctls directly to mid level, send other
578 * ioctls to block level and then onto mid level if they can't be
582 case SCSI_IOCTL_GET_IDLUN:
583 case SCSI_IOCTL_GET_BUS_NUMBER:
584 return scsi_ioctl(sdp, cmd, (void *)arg);
586 error = scsi_cmd_ioctl(disk, cmd, arg);
587 if (error != -ENOTTY)
590 return scsi_ioctl(sdp, cmd, (void *)arg);
593 static void set_media_not_present(struct scsi_disk *sdkp)
595 sdkp->media_present = 0;
597 sdkp->device->changed = 1;
601 * sd_media_changed - check if our medium changed
602 * @disk: kernel device descriptor
604 * Returns 0 if not applicable or no change; 1 if change
606 * Note: this function is invoked from the block subsystem.
608 static int sd_media_changed(struct gendisk *disk)
610 struct scsi_disk *sdkp = scsi_disk(disk);
611 struct scsi_device *sdp = sdkp->device;
614 SCSI_LOG_HLQUEUE(3, printk("sd_media_changed: disk=%s\n",
621 * If the device is offline, don't send any commands - just pretend as
622 * if the command failed. If the device ever comes back online, we
623 * can deal with it then. It is only because of unrecoverable errors
624 * that we would ever take a device offline in the first place.
626 if (!scsi_device_online(sdp))
630 * Using TEST_UNIT_READY enables differentiation between drive with
631 * no cartridge loaded - NOT READY, drive with changed cartridge -
632 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
634 * Drives that auto spin down. eg iomega jaz 1G, will be started
635 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
636 * sd_revalidate() is called.
639 if (scsi_block_when_processing_errors(sdp))
640 retval = scsi_ioctl(sdp, SCSI_IOCTL_TEST_UNIT_READY, NULL);
643 * Unable to test, unit probably not ready. This usually
644 * means there is no disc in the drive. Mark as changed,
645 * and we will figure it out later once the drive is
652 * For removable scsi disk we have to recognise the presence
653 * of a disk in the drive. This is kept in the struct scsi_disk
654 * struct and tested at open ! Daniel Roche (dan@lectra.fr)
656 sdkp->media_present = 1;
658 retval = sdp->changed;
664 set_media_not_present(sdkp);
668 static void sd_rescan(struct device *dev)
670 struct scsi_disk *sdkp = dev_get_drvdata(dev);
671 sd_revalidate_disk(sdkp->disk);
674 static struct block_device_operations sd_fops = {
675 .owner = THIS_MODULE,
677 .release = sd_release,
679 .media_changed = sd_media_changed,
680 .revalidate_disk = sd_revalidate_disk,
684 * sd_rw_intr - bottom half handler: called when the lower level
685 * driver has completed (successfully or otherwise) a scsi command.
686 * @SCpnt: mid-level's per command structure.
688 * Note: potentially run from within an ISR. Must not block.
690 static void sd_rw_intr(struct scsi_cmnd * SCpnt)
692 int result = SCpnt->result;
693 int this_count = SCpnt->bufflen;
694 int good_bytes = (result == 0 ? this_count : 0);
695 sector_t block_sectors = 1;
696 sector_t error_sector;
697 #ifdef CONFIG_SCSI_LOGGING
698 SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: %s: res=0x%x\n",
699 SCpnt->request->rq_disk->disk_name, result));
701 SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: sb[0,2,asc,ascq]"
702 "=%x,%x,%x,%x\n", SCpnt->sense_buffer[0],
703 SCpnt->sense_buffer[2], SCpnt->sense_buffer[12],
704 SCpnt->sense_buffer[13]));
708 Handle MEDIUM ERRORs that indicate partial success. Since this is a
709 relatively rare error condition, no care is taken to avoid
710 unnecessary additional work such as memcpy's that could be avoided.
713 /* An error occurred */
714 if (driver_byte(result) != 0 && /* An error occurred */
715 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
716 switch (SCpnt->sense_buffer[2]) {
718 if (!(SCpnt->sense_buffer[0] & 0x80))
720 if (!blk_fs_request(SCpnt->request))
722 error_sector = (SCpnt->sense_buffer[3] << 24) |
723 (SCpnt->sense_buffer[4] << 16) |
724 (SCpnt->sense_buffer[5] << 8) |
725 SCpnt->sense_buffer[6];
726 if (SCpnt->request->bio != NULL)
727 block_sectors = bio_sectors(SCpnt->request->bio);
728 switch (SCpnt->device->sector_size) {
731 if (block_sectors < 2)
736 if (block_sectors < 4)
741 if (block_sectors < 8)
751 error_sector &= ~(block_sectors - 1);
752 good_bytes = (error_sector - SCpnt->request->sector) << 9;
753 if (good_bytes < 0 || good_bytes >= this_count)
757 case RECOVERED_ERROR:
759 * An error occurred, but it recovered. Inform the
760 * user, but make sure that it's not treated as a
763 print_sense("sd", SCpnt);
765 SCpnt->sense_buffer[0] = 0x0;
766 good_bytes = this_count;
769 case ILLEGAL_REQUEST:
770 if (SCpnt->device->use_10_for_rw &&
771 (SCpnt->cmnd[0] == READ_10 ||
772 SCpnt->cmnd[0] == WRITE_10))
773 SCpnt->device->use_10_for_rw = 0;
774 if (SCpnt->device->use_10_for_ms &&
775 (SCpnt->cmnd[0] == MODE_SENSE_10 ||
776 SCpnt->cmnd[0] == MODE_SELECT_10))
777 SCpnt->device->use_10_for_ms = 0;
785 * This calls the generic completion function, now that we know
786 * how many actual sectors finished, and how many sectors we need
787 * to say have failed.
789 scsi_io_completion(SCpnt, good_bytes, block_sectors << 9);
792 static int media_not_present(struct scsi_disk *sdkp, struct scsi_request *srp)
796 if (!(driver_byte(srp->sr_result) & DRIVER_SENSE))
798 if (srp->sr_sense_buffer[2] != NOT_READY &&
799 srp->sr_sense_buffer[2] != UNIT_ATTENTION)
801 if (srp->sr_sense_buffer[12] != 0x3A) /* medium not present */
804 set_media_not_present(sdkp);
809 * spinup disk - called only in sd_revalidate_disk()
812 sd_spinup_disk(struct scsi_disk *sdkp, char *diskname,
813 struct scsi_request *SRpnt, unsigned char *buffer) {
814 unsigned char cmd[10];
815 unsigned long spintime_value = 0;
816 int retries, spintime;
817 unsigned int the_result;
821 /* Spin up drives, as required. Only do this at boot time */
822 /* Spinup needs to be done for module loads too. */
827 cmd[0] = TEST_UNIT_READY;
828 memset((void *) &cmd[1], 0, 9);
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 = DMA_NONE;
835 scsi_wait_req (SRpnt, (void *) cmd, (void *) buffer,
836 0/*512*/, SD_TIMEOUT, SD_MAX_RETRIES);
838 the_result = SRpnt->sr_result;
840 } while (retries < 3 &&
841 (!scsi_status_is_good(the_result) ||
842 ((driver_byte(the_result) & DRIVER_SENSE) &&
843 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
850 if (media_not_present(sdkp, SRpnt))
853 if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
854 /* no sense, TUR either succeeded or failed
855 * with a status error */
856 if(!spintime && !scsi_status_is_good(the_result))
857 printk(KERN_NOTICE "%s: Unit Not Ready, error = 0x%x\n", diskname, the_result);
862 * The device does not want the automatic start to be issued.
864 if (sdkp->device->no_start_on_add) {
869 * If manual intervention is required, or this is an
870 * absent USB storage device, a spinup is meaningless.
872 if (SRpnt->sr_sense_buffer[2] == NOT_READY &&
873 SRpnt->sr_sense_buffer[12] == 4 /* not ready */ &&
874 SRpnt->sr_sense_buffer[13] == 3) {
875 break; /* manual intervention required */
878 * Issue command to spin up drive when not ready
880 } else if (SRpnt->sr_sense_buffer[2] == NOT_READY) {
883 printk(KERN_NOTICE "%s: Spinning up disk...",
886 cmd[1] = 1; /* Return immediately */
887 memset((void *) &cmd[2], 0, 8);
888 cmd[4] = 1; /* Start spin cycle */
889 SRpnt->sr_cmd_len = 0;
890 SRpnt->sr_sense_buffer[0] = 0;
891 SRpnt->sr_sense_buffer[2] = 0;
893 SRpnt->sr_data_direction = DMA_NONE;
894 scsi_wait_req(SRpnt, (void *)cmd,
895 (void *) buffer, 0/*512*/,
896 SD_TIMEOUT, SD_MAX_RETRIES);
897 spintime_value = jiffies;
901 /* Wait 1 second for next try */
903 current->state = TASK_UNINTERRUPTIBLE;
904 time1 = schedule_timeout(time1);
908 /* we don't understand the sense code, so it's
909 * probably pointless to loop */
911 printk(KERN_NOTICE "%s: Unit Not Ready, sense:\n", diskname);
912 print_req_sense("", SRpnt);
918 time_after(spintime_value + 100 * HZ, jiffies));
921 if (scsi_status_is_good(the_result))
924 printk("not responding...\n");
932 sd_read_capacity(struct scsi_disk *sdkp, char *diskname,
933 struct scsi_request *SRpnt, unsigned char *buffer) {
934 unsigned char cmd[16];
935 struct scsi_device *sdp = sdkp->device;
936 int the_result, retries;
944 memset((void *) cmd, 0, 16);
945 cmd[0] = SERVICE_ACTION_IN;
946 cmd[1] = SAI_READ_CAPACITY_16;
948 memset((void *) buffer, 0, 12);
950 cmd[0] = READ_CAPACITY;
951 memset((void *) &cmd[1], 0, 9);
952 memset((void *) buffer, 0, 8);
955 SRpnt->sr_cmd_len = 0;
956 SRpnt->sr_sense_buffer[0] = 0;
957 SRpnt->sr_sense_buffer[2] = 0;
958 SRpnt->sr_data_direction = DMA_FROM_DEVICE;
960 scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
961 longrc ? 12 : 8, SD_TIMEOUT, SD_MAX_RETRIES);
963 if (media_not_present(sdkp, SRpnt))
966 the_result = SRpnt->sr_result;
969 } while (the_result && retries);
971 if (the_result && !longrc) {
972 printk(KERN_NOTICE "%s : READ CAPACITY failed.\n"
973 "%s : status=%x, message=%02x, host=%d, driver=%02x \n",
975 status_byte(the_result),
976 msg_byte(the_result),
977 host_byte(the_result),
978 driver_byte(the_result));
980 if (driver_byte(the_result) & DRIVER_SENSE)
981 print_req_sense("sd", SRpnt);
983 printk("%s : sense not available. \n", diskname);
985 /* Set dirty bit for removable devices if not ready -
986 * sometimes drives will not report this properly. */
987 if (sdp->removable &&
988 SRpnt->sr_sense_buffer[2] == NOT_READY)
991 /* Either no media are present but the drive didn't tell us,
992 or they are present but the read capacity command fails */
993 /* sdkp->media_present = 0; -- not always correct */
994 sdkp->capacity = 0x200000; /* 1 GB - random */
997 } else if (the_result && longrc) {
998 /* READ CAPACITY(16) has been failed */
999 printk(KERN_NOTICE "%s : READ CAPACITY(16) failed.\n"
1000 "%s : status=%x, message=%02x, host=%d, driver=%02x \n",
1002 status_byte(the_result),
1003 msg_byte(the_result),
1004 host_byte(the_result),
1005 driver_byte(the_result));
1006 printk(KERN_NOTICE "%s : use 0xffffffff as device size\n",
1009 sdkp->capacity = 1 + (sector_t) 0xffffffff;
1014 sector_size = (buffer[4] << 24) |
1015 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
1016 if (buffer[0] == 0xff && buffer[1] == 0xff &&
1017 buffer[2] == 0xff && buffer[3] == 0xff) {
1018 if(sizeof(sdkp->capacity) > 4) {
1019 printk(KERN_NOTICE "%s : very big device. try to use"
1020 " READ CAPACITY(16).\n", diskname);
1024 printk(KERN_ERR "%s: too big for kernel. Assuming maximum 2Tb\n", diskname);
1027 sdkp->capacity = 1 + (((sector_t)buffer[0] << 24) |
1032 sdkp->capacity = 1 + (((u64)buffer[0] << 56) |
1033 ((u64)buffer[1] << 48) |
1034 ((u64)buffer[2] << 40) |
1035 ((u64)buffer[3] << 32) |
1036 ((sector_t)buffer[4] << 24) |
1037 ((sector_t)buffer[5] << 16) |
1038 ((sector_t)buffer[6] << 8) |
1039 (sector_t)buffer[7]);
1041 sector_size = (buffer[8] << 24) |
1042 (buffer[9] << 16) | (buffer[10] << 8) | buffer[11];
1046 if (sector_size == 0) {
1048 printk(KERN_NOTICE "%s : sector size 0 reported, "
1049 "assuming 512.\n", diskname);
1052 if (sector_size != 512 &&
1053 sector_size != 1024 &&
1054 sector_size != 2048 &&
1055 sector_size != 4096 &&
1056 sector_size != 256) {
1057 printk(KERN_NOTICE "%s : unsupported sector size "
1058 "%d.\n", diskname, sector_size);
1060 * The user might want to re-format the drive with
1061 * a supported sectorsize. Once this happens, it
1062 * would be relatively trivial to set the thing up.
1063 * For this reason, we leave the thing in the table.
1069 * The msdos fs needs to know the hardware sector size
1070 * So I have created this table. See ll_rw_blk.c
1071 * Jacques Gelinas (Jacques@solucorp.qc.ca)
1073 int hard_sector = sector_size;
1074 sector_t sz = sdkp->capacity * (hard_sector/256);
1075 request_queue_t *queue = sdp->request_queue;
1078 blk_queue_hardsect_size(queue, hard_sector);
1079 /* avoid 64-bit division on 32-bit platforms */
1081 sector_div(sz, 1250);
1083 sector_div(mb, 1950);
1085 printk(KERN_NOTICE "SCSI device %s: "
1086 "%llu %d-byte hdwr sectors (%llu MB)\n",
1087 diskname, (unsigned long long)sdkp->capacity,
1088 hard_sector, (unsigned long long)mb);
1091 /* Rescale capacity to 512-byte units */
1092 if (sector_size == 4096)
1093 sdkp->capacity <<= 3;
1094 else if (sector_size == 2048)
1095 sdkp->capacity <<= 2;
1096 else if (sector_size == 1024)
1097 sdkp->capacity <<= 1;
1098 else if (sector_size == 256)
1099 sdkp->capacity >>= 1;
1101 sdkp->device->sector_size = sector_size;
1104 /* called with buffer of length 512 */
1106 sd_do_mode_sense(struct scsi_request *SRpnt, int dbd, int modepage,
1107 unsigned char *buffer, int len, struct scsi_mode_data *data)
1109 return __scsi_mode_sense(SRpnt, dbd, modepage, buffer, len,
1110 SD_TIMEOUT, SD_MAX_RETRIES, data);
1114 * read write protect setting, if possible - called only in sd_revalidate_disk()
1115 * called with buffer of length 512
1118 sd_read_write_protect_flag(struct scsi_disk *sdkp, char *diskname,
1119 struct scsi_request *SRpnt, unsigned char *buffer) {
1121 struct scsi_mode_data data;
1123 set_disk_ro(sdkp->disk, 0);
1124 if (sdkp->device->skip_ms_page_3f) {
1125 printk(KERN_NOTICE "%s: assuming Write Enabled\n", diskname);
1129 if (sdkp->device->use_192_bytes_for_3f) {
1130 res = sd_do_mode_sense(SRpnt, 0, 0x3F, buffer, 192, &data);
1133 * First attempt: ask for all pages (0x3F), but only 4 bytes.
1134 * We have to start carefully: some devices hang if we ask
1135 * for more than is available.
1137 res = sd_do_mode_sense(SRpnt, 0, 0x3F, buffer, 4, &data);
1140 * Second attempt: ask for page 0 When only page 0 is
1141 * implemented, a request for page 3F may return Sense Key
1142 * 5: Illegal Request, Sense Code 24: Invalid field in
1145 if (!scsi_status_is_good(res))
1146 res = sd_do_mode_sense(SRpnt, 0, 0, buffer, 4, &data);
1149 * Third attempt: ask 255 bytes, as we did earlier.
1151 if (!scsi_status_is_good(res))
1152 res = sd_do_mode_sense(SRpnt, 0, 0x3F, buffer, 255,
1156 if (!scsi_status_is_good(res)) {
1158 "%s: test WP failed, assume Write Enabled\n", diskname);
1160 sdkp->write_prot = ((data.device_specific & 0x80) != 0);
1161 set_disk_ro(sdkp->disk, sdkp->write_prot);
1162 printk(KERN_NOTICE "%s: Write Protect is %s\n", diskname,
1163 sdkp->write_prot ? "on" : "off");
1164 printk(KERN_DEBUG "%s: Mode Sense: %02x %02x %02x %02x\n",
1165 diskname, buffer[0], buffer[1], buffer[2], buffer[3]);
1170 * sd_read_cache_type - called only from sd_revalidate_disk()
1171 * called with buffer of length 512
1174 sd_read_cache_type(struct scsi_disk *sdkp, char *diskname,
1175 struct scsi_request *SRpnt, unsigned char *buffer) {
1178 const int dbd = 0; /* DBD */
1179 const int modepage = 0x08; /* current values, cache page */
1180 struct scsi_mode_data data;
1182 if (sdkp->device->skip_ms_page_8)
1185 /* cautiously ask */
1186 res = sd_do_mode_sense(SRpnt, dbd, modepage, buffer, 4, &data);
1188 if (!scsi_status_is_good(res))
1191 /* that went OK, now ask for the proper length */
1195 * We're only interested in the first three bytes, actually.
1196 * But the data cache page is defined for the first 20.
1203 /* Take headers and block descriptors into account */
1204 len += data.header_length + data.block_descriptor_length;
1207 res = sd_do_mode_sense(SRpnt, dbd, modepage, buffer, len, &data);
1209 if (scsi_status_is_good(res)) {
1210 const char *types[] = {
1211 "write through", "none", "write back",
1212 "write back, no read (daft)"
1215 int offset = data.header_length +
1216 data.block_descriptor_length + 2;
1218 sdkp->WCE = ((buffer[offset] & 0x04) != 0);
1219 sdkp->RCD = ((buffer[offset] & 0x01) != 0);
1221 ct = sdkp->RCD + 2*sdkp->WCE;
1223 printk(KERN_NOTICE "SCSI device %s: drive cache: %s\n",
1224 diskname, types[ct]);
1230 if ((SRpnt->sr_sense_buffer[0] & 0x70) == 0x70
1231 && (SRpnt->sr_sense_buffer[2] & 0x0f) == ILLEGAL_REQUEST
1232 /* ASC 0x24 ASCQ 0x00: Invalid field in CDB */
1233 && SRpnt->sr_sense_buffer[12] == 0x24
1234 && SRpnt->sr_sense_buffer[13] == 0x00) {
1235 printk(KERN_NOTICE "%s: cache data unavailable\n",
1238 printk(KERN_ERR "%s: asking for cache data failed\n",
1243 printk(KERN_ERR "%s: assuming drive cache: write through\n",
1250 * sd_revalidate_disk - called the first time a new disk is seen,
1251 * performs disk spin up, read_capacity, etc.
1252 * @disk: struct gendisk we care about
1254 static int sd_revalidate_disk(struct gendisk *disk)
1256 struct scsi_disk *sdkp = scsi_disk(disk);
1257 struct scsi_device *sdp = sdkp->device;
1258 struct scsi_request *sreq;
1259 unsigned char *buffer;
1261 SCSI_LOG_HLQUEUE(3, printk("sd_revalidate_disk: disk=%s\n", disk->disk_name));
1264 * If the device is offline, don't try and read capacity or any
1265 * of the other niceties.
1267 if (!scsi_device_online(sdp))
1270 sreq = scsi_allocate_request(sdp, GFP_KERNEL);
1272 printk(KERN_WARNING "(sd_revalidate_disk:) Request allocation "
1277 buffer = kmalloc(512, GFP_KERNEL | __GFP_DMA);
1279 printk(KERN_WARNING "(sd_revalidate_disk:) Memory allocation "
1281 goto out_release_request;
1284 /* defaults, until the device tells us otherwise */
1285 sdp->sector_size = 512;
1287 sdkp->media_present = 1;
1288 sdkp->write_prot = 0;
1292 sd_spinup_disk(sdkp, disk->disk_name, sreq, buffer);
1295 * Without media there is no reason to ask; moreover, some devices
1296 * react badly if we do.
1298 if (sdkp->media_present) {
1299 sd_read_capacity(sdkp, disk->disk_name, sreq, buffer);
1301 sd_read_write_protect_flag(sdkp, disk->disk_name,
1303 sd_read_cache_type(sdkp, disk->disk_name, sreq, buffer);
1306 set_capacity(disk, sdkp->capacity);
1309 out_release_request:
1310 scsi_release_request(sreq);
1316 * sd_probe - called during driver initialization and whenever a
1317 * new scsi device is attached to the system. It is called once
1318 * for each scsi device (not just disks) present.
1319 * @dev: pointer to device object
1321 * Returns 0 if successful (or not interested in this scsi device
1322 * (e.g. scanner)); 1 when there is an error.
1324 * Note: this function is invoked from the scsi mid-level.
1325 * This function sets up the mapping between a given
1326 * <host,channel,id,lun> (found in sdp) and new device name
1327 * (e.g. /dev/sda). More precisely it is the block device major
1328 * and minor number that is chosen here.
1330 * Assume sd_attach is not re-entrant (for time being)
1331 * Also think about sd_attach() and sd_remove() running coincidentally.
1333 static int sd_probe(struct device *dev)
1335 struct scsi_device *sdp = to_scsi_device(dev);
1336 struct scsi_disk *sdkp;
1342 if ((sdp->type != TYPE_DISK) && (sdp->type != TYPE_MOD))
1345 if ((error = scsi_device_get(sdp)) != 0)
1348 SCSI_LOG_HLQUEUE(3, printk("sd_attach: scsi device: <%d,%d,%d,%d>\n",
1349 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun));
1352 sdkp = kmalloc(sizeof(*sdkp), GFP_KERNEL);
1356 memset (sdkp, 0, sizeof(*sdkp));
1357 kref_init(&sdkp->kref, scsi_disk_release);
1359 /* Note: We can accomodate 64 partitions, but the genhd code
1360 * assumes partitions allocate consecutive minors, which they don't.
1361 * So for now stay with max 16 partitions and leave two spare bits.
1362 * Later, we may change the genhd code and the alloc_disk() call
1363 * and the ->minors assignment here. KG, 2004-02-10
1365 gd = alloc_disk(16);
1369 spin_lock(&sd_index_lock);
1370 index = find_first_zero_bit(sd_index_bits, SD_DISKS);
1371 if (index == SD_DISKS) {
1372 spin_unlock(&sd_index_lock);
1376 __set_bit(index, sd_index_bits);
1377 spin_unlock(&sd_index_lock);
1380 sdkp->driver = &sd_template;
1382 sdkp->index = index;
1385 devno = make_sd_dev(index, 0);
1386 gd->major = MAJOR(devno);
1387 gd->first_minor = MINOR(devno);
1389 gd->fops = &sd_fops;
1392 sprintf(gd->disk_name, "sd%c", 'a' + index % 26);
1393 } else if (index < (26*27)) {
1394 sprintf(gd->disk_name, "sd%c%c",
1395 'a' + index / 26 - 1,'a' + index % 26);
1397 const unsigned int m1 = (index / 26 - 1) / 26 - 1;
1398 const unsigned int m2 = (index / 26 - 1) % 26;
1399 const unsigned int m3 = index % 26;
1400 sprintf(gd->disk_name, "sd%c%c%c",
1401 'a' + m1, 'a' + m2, 'a' + m3);
1404 strcpy(gd->devfs_name, sdp->devfs_name);
1406 gd->private_data = &sdkp->driver;
1408 sd_revalidate_disk(gd);
1410 gd->driverfs_dev = &sdp->sdev_gendev;
1411 gd->flags = GENHD_FL_DRIVERFS;
1413 gd->flags |= GENHD_FL_REMOVABLE;
1414 gd->queue = sdkp->device->request_queue;
1416 dev_set_drvdata(dev, sdkp);
1419 printk(KERN_NOTICE "Attached scsi %sdisk %s at scsi%d, channel %d, "
1420 "id %d, lun %d\n", sdp->removable ? "removable " : "",
1421 gd->disk_name, sdp->host->host_no, sdp->channel,
1431 scsi_device_put(sdp);
1437 * sd_remove - called whenever a scsi disk (previously recognized by
1438 * sd_probe) is detached from the system. It is called (potentially
1439 * multiple times) during sd module unload.
1440 * @sdp: pointer to mid level scsi device object
1442 * Note: this function is invoked from the scsi mid-level.
1443 * This function potentially frees up a device name (e.g. /dev/sdc)
1444 * that could be re-used by a subsequent sd_probe().
1445 * This function is not called when the built-in sd driver is "exit-ed".
1447 static int sd_remove(struct device *dev)
1449 struct scsi_disk *sdkp = dev_get_drvdata(dev);
1451 del_gendisk(sdkp->disk);
1453 scsi_disk_put(sdkp);
1459 * scsi_disk_release - Called to free the scsi_disk structure
1460 * @kref: pointer to embedded kref
1462 * sd_ref_sem must be held entering this routine. Because it is
1463 * called on last put, you should always use the scsi_disk_get()
1464 * scsi_disk_put() helpers which manipulate the semaphore directly
1465 * and never do a direct kref_put().
1467 static void scsi_disk_release(struct kref *kref)
1469 struct scsi_disk *sdkp = to_scsi_disk(kref);
1470 struct scsi_device *sdev = sdkp->device;
1471 struct gendisk *disk = sdkp->disk;
1473 spin_lock(&sd_index_lock);
1474 clear_bit(sdkp->index, sd_index_bits);
1475 spin_unlock(&sd_index_lock);
1477 disk->private_data = NULL;
1483 scsi_device_put(sdev);
1487 * Send a SYNCHRONIZE CACHE instruction down to the device through
1488 * the normal SCSI command structure. Wait for the command to
1491 static void sd_shutdown(struct device *dev)
1493 struct scsi_device *sdp = to_scsi_device(dev);
1494 struct scsi_disk *sdkp;
1495 struct scsi_request *sreq;
1498 sdkp = dev_get_drvdata(dev);
1500 return; /* this can happen */
1502 if (!scsi_device_online(sdp) || !sdkp->WCE)
1505 printk(KERN_NOTICE "Synchronizing SCSI cache for disk %s: ",
1506 sdkp->disk->disk_name);
1508 sreq = scsi_allocate_request(sdp, GFP_KERNEL);
1510 printk("FAILED\n No memory for request\n");
1514 sreq->sr_data_direction = DMA_NONE;
1515 for (retries = 3; retries > 0; --retries) {
1516 unsigned char cmd[10] = { 0 };
1518 cmd[0] = SYNCHRONIZE_CACHE;
1520 * Leave the rest of the command zero to indicate
1523 scsi_wait_req(sreq, cmd, NULL, 0, SD_TIMEOUT, SD_MAX_RETRIES);
1524 if (sreq->sr_result == 0)
1528 res = sreq->sr_result;
1530 printk(KERN_WARNING "FAILED\n status = %x, message = %02x, "
1531 "host = %d, driver = %02x\n ",
1532 status_byte(res), msg_byte(res),
1533 host_byte(res), driver_byte(res));
1534 if (driver_byte(res) & DRIVER_SENSE)
1535 print_req_sense("sd", sreq);
1538 scsi_release_request(sreq);
1543 * init_sd - entry point for this driver (both when built in or when
1546 * Note: this function registers this driver with the scsi mid-level.
1548 static int __init init_sd(void)
1552 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
1554 for (i = 0; i < SD_MAJORS; i++)
1555 if (register_blkdev(sd_major(i), "sd") == 0)
1561 return scsi_register_driver(&sd_template.gendrv);
1565 * exit_sd - exit point for this driver (when it is a module).
1567 * Note: this function unregisters this driver from the scsi mid-level.
1569 static void __exit exit_sd(void)
1573 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
1575 scsi_unregister_driver(&sd_template.gendrv);
1576 for (i = 0; i < SD_MAJORS; i++)
1577 unregister_blkdev(sd_major(i), "sd");
1580 MODULE_LICENSE("GPL");
1581 MODULE_AUTHOR("Eric Youngdale");
1582 MODULE_DESCRIPTION("SCSI disk (sd) driver");
1584 module_init(init_sd);
1585 module_exit(exit_sd);