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/idr.h>
46 #include <linux/interrupt.h>
47 #include <linux/init.h>
48 #include <linux/blkdev.h>
49 #include <linux/blkpg.h>
50 #include <linux/kref.h>
51 #include <linux/delay.h>
52 #include <linux/mutex.h>
53 #include <asm/uaccess.h>
55 #include <scsi/scsi.h>
56 #include <scsi/scsi_cmnd.h>
57 #include <scsi/scsi_dbg.h>
58 #include <scsi/scsi_device.h>
59 #include <scsi/scsi_driver.h>
60 #include <scsi/scsi_eh.h>
61 #include <scsi/scsi_host.h>
62 #include <scsi/scsi_ioctl.h>
63 #include <scsi/scsicam.h>
65 #include "scsi_logging.h"
68 * More than enough for everybody ;) The huge number of majors
69 * is a leftover from 16bit dev_t days, we don't really need that
74 MODULE_AUTHOR("Eric Youngdale");
75 MODULE_DESCRIPTION("SCSI disk (sd) driver");
76 MODULE_LICENSE("GPL");
78 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR);
79 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR);
80 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR);
81 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR);
82 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR);
83 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR);
84 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR);
85 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR);
86 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR);
87 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR);
88 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR);
89 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR);
90 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
91 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
92 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
93 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
96 * This is limited by the naming scheme enforced in sd_probe,
97 * add another character to it if you really need more disks.
99 #define SD_MAX_DISKS (((26 * 26) + 26 + 1) * 26)
102 * Time out in seconds for disks and Magneto-opticals (which are slower).
104 #define SD_TIMEOUT (30 * HZ)
105 #define SD_MOD_TIMEOUT (75 * HZ)
108 * Number of allowed retries
110 #define SD_MAX_RETRIES 5
111 #define SD_PASSTHROUGH_RETRIES 1
114 * Size of the initial data buffer for mode and read capacity data
116 #define SD_BUF_SIZE 512
118 static void scsi_disk_release(struct kref *kref);
121 struct scsi_driver *driver; /* always &sd_template */
122 struct scsi_device *device;
124 struct gendisk *disk;
125 unsigned int openers; /* protected by BKL for now, yuck */
126 sector_t capacity; /* size in 512-byte sectors */
130 unsigned WCE : 1; /* state of disk WCE bit */
131 unsigned RCD : 1; /* state of disk RCD bit, unused */
132 unsigned DPOFUA : 1; /* state of disk DPOFUA bit */
135 static DEFINE_IDR(sd_index_idr);
136 static DEFINE_SPINLOCK(sd_index_lock);
138 /* This semaphore is used to mediate the 0->1 reference get in the
139 * face of object destruction (i.e. we can't allow a get on an
140 * object after last put) */
141 static DEFINE_MUTEX(sd_ref_mutex);
143 static int sd_revalidate_disk(struct gendisk *disk);
144 static void sd_rw_intr(struct scsi_cmnd * SCpnt);
146 static int sd_probe(struct device *);
147 static int sd_remove(struct device *);
148 static void sd_shutdown(struct device *dev);
149 static void sd_rescan(struct device *);
150 static int sd_init_command(struct scsi_cmnd *);
151 static int sd_issue_flush(struct device *, sector_t *);
152 static void sd_prepare_flush(request_queue_t *, struct request *);
153 static void sd_read_capacity(struct scsi_disk *sdkp, char *diskname,
154 unsigned char *buffer);
156 static struct scsi_driver sd_template = {
157 .owner = THIS_MODULE,
162 .shutdown = sd_shutdown,
165 .init_command = sd_init_command,
166 .issue_flush = sd_issue_flush,
170 * Device no to disk mapping:
172 * major disc2 disc p1
173 * |............|.............|....|....| <- dev_t
176 * Inside a major, we have 16k disks, however mapped non-
177 * contiguously. The first 16 disks are for major0, the next
178 * ones with major1, ... Disk 256 is for major0 again, disk 272
180 * As we stay compatible with our numbering scheme, we can reuse
181 * the well-know SCSI majors 8, 65--71, 136--143.
183 static int sd_major(int major_idx)
187 return SCSI_DISK0_MAJOR;
189 return SCSI_DISK1_MAJOR + major_idx - 1;
191 return SCSI_DISK8_MAJOR + major_idx - 8;
194 return 0; /* shut up gcc */
198 #define to_scsi_disk(obj) container_of(obj,struct scsi_disk,kref)
200 static inline struct scsi_disk *scsi_disk(struct gendisk *disk)
202 return container_of(disk->private_data, struct scsi_disk, driver);
205 static struct scsi_disk *__scsi_disk_get(struct gendisk *disk)
207 struct scsi_disk *sdkp = NULL;
209 if (disk->private_data) {
210 sdkp = scsi_disk(disk);
211 if (scsi_device_get(sdkp->device) == 0)
212 kref_get(&sdkp->kref);
219 static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
221 struct scsi_disk *sdkp;
223 mutex_lock(&sd_ref_mutex);
224 sdkp = __scsi_disk_get(disk);
225 mutex_unlock(&sd_ref_mutex);
229 static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev)
231 struct scsi_disk *sdkp;
233 mutex_lock(&sd_ref_mutex);
234 sdkp = dev_get_drvdata(dev);
236 sdkp = __scsi_disk_get(sdkp->disk);
237 mutex_unlock(&sd_ref_mutex);
241 static void scsi_disk_put(struct scsi_disk *sdkp)
243 struct scsi_device *sdev = sdkp->device;
245 mutex_lock(&sd_ref_mutex);
246 kref_put(&sdkp->kref, scsi_disk_release);
247 scsi_device_put(sdev);
248 mutex_unlock(&sd_ref_mutex);
252 * sd_init_command - build a scsi (read or write) command from
253 * information in the request structure.
254 * @SCpnt: pointer to mid-level's per scsi command structure that
255 * contains request and into which the scsi command is written
257 * Returns 1 if successful and 0 if error (or cannot be done now).
259 static int sd_init_command(struct scsi_cmnd * SCpnt)
261 struct scsi_device *sdp = SCpnt->device;
262 struct request *rq = SCpnt->request;
263 struct gendisk *disk = rq->rq_disk;
264 sector_t block = rq->sector;
265 unsigned int this_count = SCpnt->request_bufflen >> 9;
266 unsigned int timeout = sdp->timeout;
268 SCSI_LOG_HLQUEUE(1, printk("sd_init_command: disk=%s, block=%llu, "
269 "count=%d\n", disk->disk_name,
270 (unsigned long long)block, this_count));
272 if (!sdp || !scsi_device_online(sdp) ||
273 block + rq->nr_sectors > get_capacity(disk)) {
274 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
276 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
282 * quietly refuse to do anything to a changed disc until
283 * the changed bit has been reset
285 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
288 SCSI_LOG_HLQUEUE(2, printk("%s : block=%llu\n",
289 disk->disk_name, (unsigned long long)block));
292 * If we have a 1K hardware sectorsize, prevent access to single
293 * 512 byte sectors. In theory we could handle this - in fact
294 * the scsi cdrom driver must be able to handle this because
295 * we typically use 1K blocksizes, and cdroms typically have
296 * 2K hardware sectorsizes. Of course, things are simpler
297 * with the cdrom, since it is read-only. For performance
298 * reasons, the filesystems should be able to handle this
299 * and not force the scsi disk driver to use bounce buffers
302 if (sdp->sector_size == 1024) {
303 if ((block & 1) || (rq->nr_sectors & 1)) {
304 printk(KERN_ERR "sd: Bad block number requested");
308 this_count = this_count >> 1;
311 if (sdp->sector_size == 2048) {
312 if ((block & 3) || (rq->nr_sectors & 3)) {
313 printk(KERN_ERR "sd: Bad block number requested");
317 this_count = this_count >> 2;
320 if (sdp->sector_size == 4096) {
321 if ((block & 7) || (rq->nr_sectors & 7)) {
322 printk(KERN_ERR "sd: Bad block number requested");
326 this_count = this_count >> 3;
329 if (rq_data_dir(rq) == WRITE) {
330 if (!sdp->writeable) {
333 SCpnt->cmnd[0] = WRITE_6;
334 SCpnt->sc_data_direction = DMA_TO_DEVICE;
335 } else if (rq_data_dir(rq) == READ) {
336 SCpnt->cmnd[0] = READ_6;
337 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
339 printk(KERN_ERR "sd: Unknown command %lx\n", rq->flags);
340 /* overkill panic("Unknown sd command %lx\n", rq->flags); */
344 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
345 disk->disk_name, (rq_data_dir(rq) == WRITE) ?
346 "writing" : "reading", this_count, rq->nr_sectors));
350 if (block > 0xffffffff) {
351 SCpnt->cmnd[0] += READ_16 - READ_6;
352 SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
353 SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
354 SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
355 SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
356 SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
357 SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
358 SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
359 SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
360 SCpnt->cmnd[9] = (unsigned char) block & 0xff;
361 SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
362 SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
363 SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
364 SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
365 SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
366 } else if ((this_count > 0xff) || (block > 0x1fffff) ||
367 SCpnt->device->use_10_for_rw) {
368 if (this_count > 0xffff)
371 SCpnt->cmnd[0] += READ_10 - READ_6;
372 SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
373 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
374 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
375 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
376 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
377 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
378 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
379 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
381 if (unlikely(blk_fua_rq(rq))) {
383 * This happens only if this drive failed
384 * 10byte rw command with ILLEGAL_REQUEST
385 * during operation and thus turned off
388 printk(KERN_ERR "sd: FUA write on READ/WRITE(6) drive\n");
392 SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
393 SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
394 SCpnt->cmnd[3] = (unsigned char) block & 0xff;
395 SCpnt->cmnd[4] = (unsigned char) this_count;
398 SCpnt->request_bufflen = SCpnt->bufflen =
399 this_count * sdp->sector_size;
402 * We shouldn't disconnect in the middle of a sector, so with a dumb
403 * host adapter, it's safe to assume that we can at least transfer
404 * this many bytes between each connect / disconnect.
406 SCpnt->transfersize = sdp->sector_size;
407 SCpnt->underflow = this_count << 9;
408 SCpnt->allowed = SD_MAX_RETRIES;
409 SCpnt->timeout_per_command = timeout;
412 * This is the completion routine we use. This is matched in terms
413 * of capability to this function.
415 SCpnt->done = sd_rw_intr;
418 * This indicates that the command is ready from our end to be
425 * sd_open - open a scsi disk device
426 * @inode: only i_rdev member may be used
427 * @filp: only f_mode and f_flags may be used
429 * Returns 0 if successful. Returns a negated errno value in case
432 * Note: This can be called from a user context (e.g. fsck(1) )
433 * or from within the kernel (e.g. as a result of a mount(1) ).
434 * In the latter case @inode and @filp carry an abridged amount
435 * of information as noted above.
437 static int sd_open(struct inode *inode, struct file *filp)
439 struct gendisk *disk = inode->i_bdev->bd_disk;
440 struct scsi_disk *sdkp;
441 struct scsi_device *sdev;
444 if (!(sdkp = scsi_disk_get(disk)))
448 SCSI_LOG_HLQUEUE(3, printk("sd_open: disk=%s\n", disk->disk_name));
453 * If the device is in error recovery, wait until it is done.
454 * If the device is offline, then disallow any access to it.
457 if (!scsi_block_when_processing_errors(sdev))
460 if (sdev->removable || sdkp->write_prot)
461 check_disk_change(inode->i_bdev);
464 * If the drive is empty, just let the open fail.
467 if (sdev->removable && !sdkp->media_present &&
468 !(filp->f_flags & O_NDELAY))
472 * If the device has the write protect tab set, have the open fail
473 * if the user expects to be able to write to the thing.
476 if (sdkp->write_prot && (filp->f_mode & FMODE_WRITE))
480 * It is possible that the disk changing stuff resulted in
481 * the device being taken offline. If this is the case,
482 * report this to the user, and don't pretend that the
483 * open actually succeeded.
486 if (!scsi_device_online(sdev))
489 if (!sdkp->openers++ && sdev->removable) {
490 if (scsi_block_when_processing_errors(sdev))
491 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
502 * sd_release - invoked when the (last) close(2) is called on this
504 * @inode: only i_rdev member may be used
505 * @filp: only f_mode and f_flags may be used
509 * Note: may block (uninterruptible) if error recovery is underway
512 static int sd_release(struct inode *inode, struct file *filp)
514 struct gendisk *disk = inode->i_bdev->bd_disk;
515 struct scsi_disk *sdkp = scsi_disk(disk);
516 struct scsi_device *sdev = sdkp->device;
518 SCSI_LOG_HLQUEUE(3, printk("sd_release: disk=%s\n", disk->disk_name));
520 if (!--sdkp->openers && sdev->removable) {
521 if (scsi_block_when_processing_errors(sdev))
522 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
526 * XXX and what if there are packets in flight and this close()
527 * XXX is followed by a "rmmod sd_mod"?
533 static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
535 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
536 struct scsi_device *sdp = sdkp->device;
537 struct Scsi_Host *host = sdp->host;
540 /* default to most commonly used values */
541 diskinfo[0] = 0x40; /* 1 << 6 */
542 diskinfo[1] = 0x20; /* 1 << 5 */
543 diskinfo[2] = sdkp->capacity >> 11;
545 /* override with calculated, extended default, or driver values */
546 if (host->hostt->bios_param)
547 host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo);
549 scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
551 geo->heads = diskinfo[0];
552 geo->sectors = diskinfo[1];
553 geo->cylinders = diskinfo[2];
558 * sd_ioctl - process an ioctl
559 * @inode: only i_rdev/i_bdev members may be used
560 * @filp: only f_mode and f_flags may be used
561 * @cmd: ioctl command number
562 * @arg: this is third argument given to ioctl(2) system call.
563 * Often contains a pointer.
565 * Returns 0 if successful (some ioctls return postive numbers on
566 * success as well). Returns a negated errno value in case of error.
568 * Note: most ioctls are forward onto the block subsystem or further
569 * down in the scsi subsytem.
571 static int sd_ioctl(struct inode * inode, struct file * filp,
572 unsigned int cmd, unsigned long arg)
574 struct block_device *bdev = inode->i_bdev;
575 struct gendisk *disk = bdev->bd_disk;
576 struct scsi_device *sdp = scsi_disk(disk)->device;
577 void __user *p = (void __user *)arg;
580 SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
581 disk->disk_name, cmd));
584 * If we are in the middle of error recovery, don't let anyone
585 * else try and use this device. Also, if error recovery fails, it
586 * may try and take the device offline, in which case all further
587 * access to the device is prohibited.
589 error = scsi_nonblockable_ioctl(sdp, cmd, p, filp);
590 if (!scsi_block_when_processing_errors(sdp) || !error)
594 * Send SCSI addressing ioctls directly to mid level, send other
595 * ioctls to block level and then onto mid level if they can't be
599 case SCSI_IOCTL_GET_IDLUN:
600 case SCSI_IOCTL_GET_BUS_NUMBER:
601 return scsi_ioctl(sdp, cmd, p);
603 error = scsi_cmd_ioctl(filp, disk, cmd, p);
604 if (error != -ENOTTY)
607 return scsi_ioctl(sdp, cmd, p);
610 static void set_media_not_present(struct scsi_disk *sdkp)
612 sdkp->media_present = 0;
614 sdkp->device->changed = 1;
618 * sd_media_changed - check if our medium changed
619 * @disk: kernel device descriptor
621 * Returns 0 if not applicable or no change; 1 if change
623 * Note: this function is invoked from the block subsystem.
625 static int sd_media_changed(struct gendisk *disk)
627 struct scsi_disk *sdkp = scsi_disk(disk);
628 struct scsi_device *sdp = sdkp->device;
631 SCSI_LOG_HLQUEUE(3, printk("sd_media_changed: disk=%s\n",
638 * If the device is offline, don't send any commands - just pretend as
639 * if the command failed. If the device ever comes back online, we
640 * can deal with it then. It is only because of unrecoverable errors
641 * that we would ever take a device offline in the first place.
643 if (!scsi_device_online(sdp))
647 * Using TEST_UNIT_READY enables differentiation between drive with
648 * no cartridge loaded - NOT READY, drive with changed cartridge -
649 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
651 * Drives that auto spin down. eg iomega jaz 1G, will be started
652 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
653 * sd_revalidate() is called.
656 if (scsi_block_when_processing_errors(sdp))
657 retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES);
660 * Unable to test, unit probably not ready. This usually
661 * means there is no disc in the drive. Mark as changed,
662 * and we will figure it out later once the drive is
669 * For removable scsi disk we have to recognise the presence
670 * of a disk in the drive. This is kept in the struct scsi_disk
671 * struct and tested at open ! Daniel Roche (dan@lectra.fr)
673 sdkp->media_present = 1;
675 retval = sdp->changed;
681 set_media_not_present(sdkp);
685 static int sd_sync_cache(struct scsi_device *sdp)
688 struct scsi_sense_hdr sshdr;
690 if (!scsi_device_online(sdp))
694 for (retries = 3; retries > 0; --retries) {
695 unsigned char cmd[10] = { 0 };
697 cmd[0] = SYNCHRONIZE_CACHE;
699 * Leave the rest of the command zero to indicate
702 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
703 SD_TIMEOUT, SD_MAX_RETRIES);
708 if (res) { printk(KERN_WARNING "FAILED\n status = %x, message = %02x, "
709 "host = %d, driver = %02x\n ",
710 status_byte(res), msg_byte(res),
711 host_byte(res), driver_byte(res));
712 if (driver_byte(res) & DRIVER_SENSE)
713 scsi_print_sense_hdr("sd", &sshdr);
719 static int sd_issue_flush(struct device *dev, sector_t *error_sector)
722 struct scsi_device *sdp = to_scsi_device(dev);
723 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
729 ret = sd_sync_cache(sdp);
734 static void sd_prepare_flush(request_queue_t *q, struct request *rq)
736 memset(rq->cmd, 0, sizeof(rq->cmd));
737 rq->flags |= REQ_BLOCK_PC;
738 rq->timeout = SD_TIMEOUT;
739 rq->cmd[0] = SYNCHRONIZE_CACHE;
743 static void sd_rescan(struct device *dev)
745 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
748 sd_revalidate_disk(sdkp->disk);
756 * This gets directly called from VFS. When the ioctl
757 * is not recognized we go back to the other translation paths.
759 static long sd_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
761 struct block_device *bdev = file->f_dentry->d_inode->i_bdev;
762 struct gendisk *disk = bdev->bd_disk;
763 struct scsi_device *sdev = scsi_disk(disk)->device;
766 * If we are in the middle of error recovery, don't let anyone
767 * else try and use this device. Also, if error recovery fails, it
768 * may try and take the device offline, in which case all further
769 * access to the device is prohibited.
771 if (!scsi_block_when_processing_errors(sdev))
774 if (sdev->host->hostt->compat_ioctl) {
777 ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
783 * Let the static ioctl translation table take care of it.
789 static struct block_device_operations sd_fops = {
790 .owner = THIS_MODULE,
792 .release = sd_release,
796 .compat_ioctl = sd_compat_ioctl,
798 .media_changed = sd_media_changed,
799 .revalidate_disk = sd_revalidate_disk,
803 * sd_rw_intr - bottom half handler: called when the lower level
804 * driver has completed (successfully or otherwise) a scsi command.
805 * @SCpnt: mid-level's per command structure.
807 * Note: potentially run from within an ISR. Must not block.
809 static void sd_rw_intr(struct scsi_cmnd * SCpnt)
811 int result = SCpnt->result;
812 int this_count = SCpnt->bufflen;
813 int good_bytes = (result == 0 ? this_count : 0);
814 sector_t block_sectors = 1;
816 sector_t error_sector;
817 struct scsi_sense_hdr sshdr;
819 int sense_deferred = 0;
823 sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
825 sense_deferred = scsi_sense_is_deferred(&sshdr);
828 #ifdef CONFIG_SCSI_LOGGING
829 SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: %s: res=0x%x\n",
830 SCpnt->request->rq_disk->disk_name, result));
832 SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: sb[respc,sk,asc,"
833 "ascq]=%x,%x,%x,%x\n", sshdr.response_code,
834 sshdr.sense_key, sshdr.asc, sshdr.ascq));
838 Handle MEDIUM ERRORs that indicate partial success. Since this is a
839 relatively rare error condition, no care is taken to avoid
840 unnecessary additional work such as memcpy's that could be avoided.
842 if (driver_byte(result) != 0 &&
843 sense_valid && !sense_deferred) {
844 switch (sshdr.sense_key) {
846 if (!blk_fs_request(SCpnt->request))
848 info_valid = scsi_get_sense_info_fld(
849 SCpnt->sense_buffer, SCSI_SENSE_BUFFERSIZE,
852 * May want to warn and skip if following cast results
853 * in actual truncation (if sector_t < 64 bits)
855 error_sector = (sector_t)first_err_block;
856 if (SCpnt->request->bio != NULL)
857 block_sectors = bio_sectors(SCpnt->request->bio);
858 switch (SCpnt->device->sector_size) {
861 if (block_sectors < 2)
866 if (block_sectors < 4)
871 if (block_sectors < 8)
881 error_sector &= ~(block_sectors - 1);
882 good_bytes = (error_sector - SCpnt->request->sector) << 9;
883 if (good_bytes < 0 || good_bytes >= this_count)
887 case RECOVERED_ERROR: /* an error occurred, but it recovered */
888 case NO_SENSE: /* LLDD got sense data */
890 * Inform the user, but make sure that it's not treated
893 scsi_print_sense("sd", SCpnt);
895 memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
896 good_bytes = this_count;
899 case ILLEGAL_REQUEST:
900 if (SCpnt->device->use_10_for_rw &&
901 (SCpnt->cmnd[0] == READ_10 ||
902 SCpnt->cmnd[0] == WRITE_10))
903 SCpnt->device->use_10_for_rw = 0;
904 if (SCpnt->device->use_10_for_ms &&
905 (SCpnt->cmnd[0] == MODE_SENSE_10 ||
906 SCpnt->cmnd[0] == MODE_SELECT_10))
907 SCpnt->device->use_10_for_ms = 0;
915 * This calls the generic completion function, now that we know
916 * how many actual sectors finished, and how many sectors we need
917 * to say have failed.
919 scsi_io_completion(SCpnt, good_bytes, block_sectors << 9);
922 static int media_not_present(struct scsi_disk *sdkp,
923 struct scsi_sense_hdr *sshdr)
926 if (!scsi_sense_valid(sshdr))
928 /* not invoked for commands that could return deferred errors */
929 if (sshdr->sense_key != NOT_READY &&
930 sshdr->sense_key != UNIT_ATTENTION)
932 if (sshdr->asc != 0x3A) /* medium not present */
935 set_media_not_present(sdkp);
940 * spinup disk - called only in sd_revalidate_disk()
943 sd_spinup_disk(struct scsi_disk *sdkp, char *diskname)
945 unsigned char cmd[10];
946 unsigned long spintime_expire = 0;
947 int retries, spintime;
948 unsigned int the_result;
949 struct scsi_sense_hdr sshdr;
954 /* Spin up drives, as required. Only do this at boot time */
955 /* Spinup needs to be done for module loads too. */
960 cmd[0] = TEST_UNIT_READY;
961 memset((void *) &cmd[1], 0, 9);
963 the_result = scsi_execute_req(sdkp->device, cmd,
969 sense_valid = scsi_sense_valid(&sshdr);
971 } while (retries < 3 &&
972 (!scsi_status_is_good(the_result) ||
973 ((driver_byte(the_result) & DRIVER_SENSE) &&
974 sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
977 * If the drive has indicated to us that it doesn't have
978 * any media in it, don't bother with any of the rest of
981 if (media_not_present(sdkp, &sshdr))
984 if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
985 /* no sense, TUR either succeeded or failed
986 * with a status error */
987 if(!spintime && !scsi_status_is_good(the_result))
988 printk(KERN_NOTICE "%s: Unit Not Ready, "
989 "error = 0x%x\n", diskname, the_result);
994 * The device does not want the automatic start to be issued.
996 if (sdkp->device->no_start_on_add) {
1001 * If manual intervention is required, or this is an
1002 * absent USB storage device, a spinup is meaningless.
1005 sshdr.sense_key == NOT_READY &&
1006 sshdr.asc == 4 && sshdr.ascq == 3) {
1007 break; /* manual intervention required */
1010 * Issue command to spin up drive when not ready
1012 } else if (sense_valid && sshdr.sense_key == NOT_READY) {
1014 printk(KERN_NOTICE "%s: Spinning up disk...",
1016 cmd[0] = START_STOP;
1017 cmd[1] = 1; /* Return immediately */
1018 memset((void *) &cmd[2], 0, 8);
1019 cmd[4] = 1; /* Start spin cycle */
1020 scsi_execute_req(sdkp->device, cmd, DMA_NONE,
1022 SD_TIMEOUT, SD_MAX_RETRIES);
1023 spintime_expire = jiffies + 100 * HZ;
1026 /* Wait 1 second for next try */
1031 * Wait for USB flash devices with slow firmware.
1032 * Yes, this sense key/ASC combination shouldn't
1033 * occur here. It's characteristic of these devices.
1035 } else if (sense_valid &&
1036 sshdr.sense_key == UNIT_ATTENTION &&
1037 sshdr.asc == 0x28) {
1039 spintime_expire = jiffies + 5 * HZ;
1042 /* Wait 1 second for next try */
1045 /* we don't understand the sense code, so it's
1046 * probably pointless to loop */
1048 printk(KERN_NOTICE "%s: Unit Not Ready, "
1049 "sense:\n", diskname);
1050 scsi_print_sense_hdr("", &sshdr);
1055 } while (spintime && time_before_eq(jiffies, spintime_expire));
1058 if (scsi_status_is_good(the_result))
1061 printk("not responding...\n");
1066 * read disk capacity
1069 sd_read_capacity(struct scsi_disk *sdkp, char *diskname,
1070 unsigned char *buffer)
1072 unsigned char cmd[16];
1073 int the_result, retries;
1074 int sector_size = 0;
1076 struct scsi_sense_hdr sshdr;
1077 int sense_valid = 0;
1078 struct scsi_device *sdp = sdkp->device;
1084 memset((void *) cmd, 0, 16);
1085 cmd[0] = SERVICE_ACTION_IN;
1086 cmd[1] = SAI_READ_CAPACITY_16;
1088 memset((void *) buffer, 0, 12);
1090 cmd[0] = READ_CAPACITY;
1091 memset((void *) &cmd[1], 0, 9);
1092 memset((void *) buffer, 0, 8);
1095 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
1096 buffer, longrc ? 12 : 8, &sshdr,
1097 SD_TIMEOUT, SD_MAX_RETRIES);
1099 if (media_not_present(sdkp, &sshdr))
1103 sense_valid = scsi_sense_valid(&sshdr);
1106 } while (the_result && retries);
1108 if (the_result && !longrc) {
1109 printk(KERN_NOTICE "%s : READ CAPACITY failed.\n"
1110 "%s : status=%x, message=%02x, host=%d, driver=%02x \n",
1112 status_byte(the_result),
1113 msg_byte(the_result),
1114 host_byte(the_result),
1115 driver_byte(the_result));
1117 if (driver_byte(the_result) & DRIVER_SENSE)
1118 scsi_print_sense_hdr("sd", &sshdr);
1120 printk("%s : sense not available. \n", diskname);
1122 /* Set dirty bit for removable devices if not ready -
1123 * sometimes drives will not report this properly. */
1124 if (sdp->removable &&
1125 sense_valid && sshdr.sense_key == NOT_READY)
1128 /* Either no media are present but the drive didn't tell us,
1129 or they are present but the read capacity command fails */
1130 /* sdkp->media_present = 0; -- not always correct */
1131 sdkp->capacity = 0x200000; /* 1 GB - random */
1134 } else if (the_result && longrc) {
1135 /* READ CAPACITY(16) has been failed */
1136 printk(KERN_NOTICE "%s : READ CAPACITY(16) failed.\n"
1137 "%s : status=%x, message=%02x, host=%d, driver=%02x \n",
1139 status_byte(the_result),
1140 msg_byte(the_result),
1141 host_byte(the_result),
1142 driver_byte(the_result));
1143 printk(KERN_NOTICE "%s : use 0xffffffff as device size\n",
1146 sdkp->capacity = 1 + (sector_t) 0xffffffff;
1151 sector_size = (buffer[4] << 24) |
1152 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
1153 if (buffer[0] == 0xff && buffer[1] == 0xff &&
1154 buffer[2] == 0xff && buffer[3] == 0xff) {
1155 if(sizeof(sdkp->capacity) > 4) {
1156 printk(KERN_NOTICE "%s : very big device. try to use"
1157 " READ CAPACITY(16).\n", diskname);
1161 printk(KERN_ERR "%s: too big for this kernel. Use a "
1162 "kernel compiled with support for large block "
1163 "devices.\n", diskname);
1167 sdkp->capacity = 1 + (((sector_t)buffer[0] << 24) |
1172 sdkp->capacity = 1 + (((u64)buffer[0] << 56) |
1173 ((u64)buffer[1] << 48) |
1174 ((u64)buffer[2] << 40) |
1175 ((u64)buffer[3] << 32) |
1176 ((sector_t)buffer[4] << 24) |
1177 ((sector_t)buffer[5] << 16) |
1178 ((sector_t)buffer[6] << 8) |
1179 (sector_t)buffer[7]);
1181 sector_size = (buffer[8] << 24) |
1182 (buffer[9] << 16) | (buffer[10] << 8) | buffer[11];
1185 /* Some devices return the total number of sectors, not the
1186 * highest sector number. Make the necessary adjustment. */
1187 if (sdp->fix_capacity)
1191 if (sector_size == 0) {
1193 printk(KERN_NOTICE "%s : sector size 0 reported, "
1194 "assuming 512.\n", diskname);
1197 if (sector_size != 512 &&
1198 sector_size != 1024 &&
1199 sector_size != 2048 &&
1200 sector_size != 4096 &&
1201 sector_size != 256) {
1202 printk(KERN_NOTICE "%s : unsupported sector size "
1203 "%d.\n", diskname, sector_size);
1205 * The user might want to re-format the drive with
1206 * a supported sectorsize. Once this happens, it
1207 * would be relatively trivial to set the thing up.
1208 * For this reason, we leave the thing in the table.
1212 * set a bogus sector size so the normal read/write
1213 * logic in the block layer will eventually refuse any
1214 * request on this device without tripping over power
1215 * of two sector size assumptions
1221 * The msdos fs needs to know the hardware sector size
1222 * So I have created this table. See ll_rw_blk.c
1223 * Jacques Gelinas (Jacques@solucorp.qc.ca)
1225 int hard_sector = sector_size;
1226 sector_t sz = (sdkp->capacity/2) * (hard_sector/256);
1227 request_queue_t *queue = sdp->request_queue;
1230 blk_queue_hardsect_size(queue, hard_sector);
1231 /* avoid 64-bit division on 32-bit platforms */
1232 sector_div(sz, 625);
1234 sector_div(mb, 1950);
1236 printk(KERN_NOTICE "SCSI device %s: "
1237 "%llu %d-byte hdwr sectors (%llu MB)\n",
1238 diskname, (unsigned long long)sdkp->capacity,
1239 hard_sector, (unsigned long long)mb);
1242 /* Rescale capacity to 512-byte units */
1243 if (sector_size == 4096)
1244 sdkp->capacity <<= 3;
1245 else if (sector_size == 2048)
1246 sdkp->capacity <<= 2;
1247 else if (sector_size == 1024)
1248 sdkp->capacity <<= 1;
1249 else if (sector_size == 256)
1250 sdkp->capacity >>= 1;
1252 sdkp->device->sector_size = sector_size;
1255 /* called with buffer of length 512 */
1257 sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
1258 unsigned char *buffer, int len, struct scsi_mode_data *data,
1259 struct scsi_sense_hdr *sshdr)
1261 return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
1262 SD_TIMEOUT, SD_MAX_RETRIES, data,
1267 * read write protect setting, if possible - called only in sd_revalidate_disk()
1268 * called with buffer of length SD_BUF_SIZE
1271 sd_read_write_protect_flag(struct scsi_disk *sdkp, char *diskname,
1272 unsigned char *buffer)
1275 struct scsi_device *sdp = sdkp->device;
1276 struct scsi_mode_data data;
1278 set_disk_ro(sdkp->disk, 0);
1279 if (sdp->skip_ms_page_3f) {
1280 printk(KERN_NOTICE "%s: assuming Write Enabled\n", diskname);
1284 if (sdp->use_192_bytes_for_3f) {
1285 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
1288 * First attempt: ask for all pages (0x3F), but only 4 bytes.
1289 * We have to start carefully: some devices hang if we ask
1290 * for more than is available.
1292 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
1295 * Second attempt: ask for page 0 When only page 0 is
1296 * implemented, a request for page 3F may return Sense Key
1297 * 5: Illegal Request, Sense Code 24: Invalid field in
1300 if (!scsi_status_is_good(res))
1301 res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
1304 * Third attempt: ask 255 bytes, as we did earlier.
1306 if (!scsi_status_is_good(res))
1307 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
1311 if (!scsi_status_is_good(res)) {
1313 "%s: test WP failed, assume Write Enabled\n", diskname);
1315 sdkp->write_prot = ((data.device_specific & 0x80) != 0);
1316 set_disk_ro(sdkp->disk, sdkp->write_prot);
1317 printk(KERN_NOTICE "%s: Write Protect is %s\n", diskname,
1318 sdkp->write_prot ? "on" : "off");
1319 printk(KERN_DEBUG "%s: Mode Sense: %02x %02x %02x %02x\n",
1320 diskname, buffer[0], buffer[1], buffer[2], buffer[3]);
1325 * sd_read_cache_type - called only from sd_revalidate_disk()
1326 * called with buffer of length SD_BUF_SIZE
1329 sd_read_cache_type(struct scsi_disk *sdkp, char *diskname,
1330 unsigned char *buffer)
1333 struct scsi_device *sdp = sdkp->device;
1337 struct scsi_mode_data data;
1338 struct scsi_sense_hdr sshdr;
1340 if (sdp->skip_ms_page_8)
1343 if (sdp->type == TYPE_RBC) {
1351 /* cautiously ask */
1352 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr);
1354 if (!scsi_status_is_good(res))
1357 if (!data.header_length) {
1359 printk(KERN_ERR "%s: missing header in MODE_SENSE response\n",
1363 /* that went OK, now ask for the proper length */
1367 * We're only interested in the first three bytes, actually.
1368 * But the data cache page is defined for the first 20.
1375 /* Take headers and block descriptors into account */
1376 len += data.header_length + data.block_descriptor_length;
1377 if (len > SD_BUF_SIZE)
1381 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr);
1383 if (scsi_status_is_good(res)) {
1384 const char *types[] = {
1385 "write through", "none", "write back",
1386 "write back, no read (daft)"
1389 int offset = data.header_length + data.block_descriptor_length;
1391 if (offset >= SD_BUF_SIZE - 2) {
1392 printk(KERN_ERR "%s: malformed MODE SENSE response",
1397 if ((buffer[offset] & 0x3f) != modepage) {
1398 printk(KERN_ERR "%s: got wrong page\n", diskname);
1402 if (modepage == 8) {
1403 sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
1404 sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
1406 sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
1410 sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
1411 if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
1412 printk(KERN_NOTICE "SCSI device %s: uses "
1413 "READ/WRITE(6), disabling FUA\n", diskname);
1417 ct = sdkp->RCD + 2*sdkp->WCE;
1419 printk(KERN_NOTICE "SCSI device %s: drive cache: %s%s\n",
1420 diskname, types[ct],
1421 sdkp->DPOFUA ? " w/ FUA" : "");
1427 if (scsi_sense_valid(&sshdr) &&
1428 sshdr.sense_key == ILLEGAL_REQUEST &&
1429 sshdr.asc == 0x24 && sshdr.ascq == 0x0)
1430 printk(KERN_NOTICE "%s: cache data unavailable\n",
1431 diskname); /* Invalid field in CDB */
1433 printk(KERN_ERR "%s: asking for cache data failed\n",
1437 printk(KERN_ERR "%s: assuming drive cache: write through\n",
1445 * sd_revalidate_disk - called the first time a new disk is seen,
1446 * performs disk spin up, read_capacity, etc.
1447 * @disk: struct gendisk we care about
1449 static int sd_revalidate_disk(struct gendisk *disk)
1451 struct scsi_disk *sdkp = scsi_disk(disk);
1452 struct scsi_device *sdp = sdkp->device;
1453 unsigned char *buffer;
1456 SCSI_LOG_HLQUEUE(3, printk("sd_revalidate_disk: disk=%s\n", disk->disk_name));
1459 * If the device is offline, don't try and read capacity or any
1460 * of the other niceties.
1462 if (!scsi_device_online(sdp))
1465 buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL | __GFP_DMA);
1467 printk(KERN_WARNING "(sd_revalidate_disk:) Memory allocation "
1472 /* defaults, until the device tells us otherwise */
1473 sdp->sector_size = 512;
1475 sdkp->media_present = 1;
1476 sdkp->write_prot = 0;
1480 sd_spinup_disk(sdkp, disk->disk_name);
1483 * Without media there is no reason to ask; moreover, some devices
1484 * react badly if we do.
1486 if (sdkp->media_present) {
1487 sd_read_capacity(sdkp, disk->disk_name, buffer);
1488 sd_read_write_protect_flag(sdkp, disk->disk_name, buffer);
1489 sd_read_cache_type(sdkp, disk->disk_name, buffer);
1493 * We now have all cache related info, determine how we deal
1494 * with ordered requests. Note that as the current SCSI
1495 * dispatch function can alter request order, we cannot use
1496 * QUEUE_ORDERED_TAG_* even when ordered tag is supported.
1499 ordered = sdkp->DPOFUA
1500 ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH;
1502 ordered = QUEUE_ORDERED_DRAIN;
1504 blk_queue_ordered(sdkp->disk->queue, ordered, sd_prepare_flush);
1506 set_capacity(disk, sdkp->capacity);
1514 * sd_probe - called during driver initialization and whenever a
1515 * new scsi device is attached to the system. It is called once
1516 * for each scsi device (not just disks) present.
1517 * @dev: pointer to device object
1519 * Returns 0 if successful (or not interested in this scsi device
1520 * (e.g. scanner)); 1 when there is an error.
1522 * Note: this function is invoked from the scsi mid-level.
1523 * This function sets up the mapping between a given
1524 * <host,channel,id,lun> (found in sdp) and new device name
1525 * (e.g. /dev/sda). More precisely it is the block device major
1526 * and minor number that is chosen here.
1528 * Assume sd_attach is not re-entrant (for time being)
1529 * Also think about sd_attach() and sd_remove() running coincidentally.
1531 static int sd_probe(struct device *dev)
1533 struct scsi_device *sdp = to_scsi_device(dev);
1534 struct scsi_disk *sdkp;
1540 if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
1543 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
1547 sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
1551 kref_init(&sdkp->kref);
1553 gd = alloc_disk(16);
1557 if (!idr_pre_get(&sd_index_idr, GFP_KERNEL))
1560 spin_lock(&sd_index_lock);
1561 error = idr_get_new(&sd_index_idr, NULL, &index);
1562 spin_unlock(&sd_index_lock);
1564 if (index >= SD_MAX_DISKS)
1569 get_device(&sdp->sdev_gendev);
1571 sdkp->driver = &sd_template;
1573 sdkp->index = index;
1576 if (!sdp->timeout) {
1577 if (sdp->type != TYPE_MOD)
1578 sdp->timeout = SD_TIMEOUT;
1580 sdp->timeout = SD_MOD_TIMEOUT;
1583 gd->major = sd_major((index & 0xf0) >> 4);
1584 gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
1586 gd->fops = &sd_fops;
1589 sprintf(gd->disk_name, "sd%c", 'a' + index % 26);
1590 } else if (index < (26 + 1) * 26) {
1591 sprintf(gd->disk_name, "sd%c%c",
1592 'a' + index / 26 - 1,'a' + index % 26);
1594 const unsigned int m1 = (index / 26 - 1) / 26 - 1;
1595 const unsigned int m2 = (index / 26 - 1) % 26;
1596 const unsigned int m3 = index % 26;
1597 sprintf(gd->disk_name, "sd%c%c%c",
1598 'a' + m1, 'a' + m2, 'a' + m3);
1601 gd->private_data = &sdkp->driver;
1602 gd->queue = sdkp->device->request_queue;
1604 sd_revalidate_disk(gd);
1606 gd->driverfs_dev = &sdp->sdev_gendev;
1607 gd->flags = GENHD_FL_DRIVERFS;
1609 gd->flags |= GENHD_FL_REMOVABLE;
1611 dev_set_drvdata(dev, sdkp);
1614 sdev_printk(KERN_NOTICE, sdp, "Attached scsi %sdisk %s\n",
1615 sdp->removable ? "removable " : "", gd->disk_name);
1628 * sd_remove - called whenever a scsi disk (previously recognized by
1629 * sd_probe) is detached from the system. It is called (potentially
1630 * multiple times) during sd module unload.
1631 * @sdp: pointer to mid level scsi device object
1633 * Note: this function is invoked from the scsi mid-level.
1634 * This function potentially frees up a device name (e.g. /dev/sdc)
1635 * that could be re-used by a subsequent sd_probe().
1636 * This function is not called when the built-in sd driver is "exit-ed".
1638 static int sd_remove(struct device *dev)
1640 struct scsi_disk *sdkp = dev_get_drvdata(dev);
1642 del_gendisk(sdkp->disk);
1645 mutex_lock(&sd_ref_mutex);
1646 dev_set_drvdata(dev, NULL);
1647 kref_put(&sdkp->kref, scsi_disk_release);
1648 mutex_unlock(&sd_ref_mutex);
1654 * scsi_disk_release - Called to free the scsi_disk structure
1655 * @kref: pointer to embedded kref
1657 * sd_ref_mutex must be held entering this routine. Because it is
1658 * called on last put, you should always use the scsi_disk_get()
1659 * scsi_disk_put() helpers which manipulate the semaphore directly
1660 * and never do a direct kref_put().
1662 static void scsi_disk_release(struct kref *kref)
1664 struct scsi_disk *sdkp = to_scsi_disk(kref);
1665 struct gendisk *disk = sdkp->disk;
1667 spin_lock(&sd_index_lock);
1668 idr_remove(&sd_index_idr, sdkp->index);
1669 spin_unlock(&sd_index_lock);
1671 disk->private_data = NULL;
1673 put_device(&sdkp->device->sdev_gendev);
1679 * Send a SYNCHRONIZE CACHE instruction down to the device through
1680 * the normal SCSI command structure. Wait for the command to
1683 static void sd_shutdown(struct device *dev)
1685 struct scsi_device *sdp = to_scsi_device(dev);
1686 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
1689 return; /* this can happen */
1692 printk(KERN_NOTICE "Synchronizing SCSI cache for disk %s: \n",
1693 sdkp->disk->disk_name);
1696 scsi_disk_put(sdkp);
1700 * init_sd - entry point for this driver (both when built in or when
1703 * Note: this function registers this driver with the scsi mid-level.
1705 static int __init init_sd(void)
1709 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
1711 for (i = 0; i < SD_MAJORS; i++)
1712 if (register_blkdev(sd_major(i), "sd") == 0)
1718 return scsi_register_driver(&sd_template.gendrv);
1722 * exit_sd - exit point for this driver (when it is a module).
1724 * Note: this function unregisters this driver from the scsi mid-level.
1726 static void __exit exit_sd(void)
1730 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
1732 scsi_unregister_driver(&sd_template.gendrv);
1733 for (i = 0; i < SD_MAJORS; i++)
1734 unregister_blkdev(sd_major(i), "sd");
1737 module_init(init_sd);
1738 module_exit(exit_sd);