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 <asm/uaccess.h>
54 #include <scsi/scsi.h>
55 #include <scsi/scsi_cmnd.h>
56 #include <scsi/scsi_dbg.h>
57 #include <scsi/scsi_device.h>
58 #include <scsi/scsi_driver.h>
59 #include <scsi/scsi_eh.h>
60 #include <scsi/scsi_host.h>
61 #include <scsi/scsi_ioctl.h>
62 #include <scsi/scsi_request.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
75 * This is limited by the naming scheme enforced in sd_probe,
76 * add another character to it if you really need more disks.
78 #define SD_MAX_DISKS (((26 * 26) + 26 + 1) * 26)
81 * Time out in seconds for disks and Magneto-opticals (which are slower).
83 #define SD_TIMEOUT (30 * HZ)
84 #define SD_MOD_TIMEOUT (75 * HZ)
87 * Number of allowed retries
89 #define SD_MAX_RETRIES 5
90 #define SD_PASSTHROUGH_RETRIES 1
92 static void scsi_disk_release(struct kref *kref);
95 struct scsi_driver *driver; /* always &sd_template */
96 struct scsi_device *device;
99 unsigned int openers; /* protected by BKL for now, yuck */
100 sector_t capacity; /* size in 512-byte sectors */
104 unsigned WCE : 1; /* state of disk WCE bit */
105 unsigned RCD : 1; /* state of disk RCD bit, unused */
108 static DEFINE_IDR(sd_index_idr);
109 static DEFINE_SPINLOCK(sd_index_lock);
111 /* This semaphore is used to mediate the 0->1 reference get in the
112 * face of object destruction (i.e. we can't allow a get on an
113 * object after last put) */
114 static DECLARE_MUTEX(sd_ref_sem);
116 static int sd_revalidate_disk(struct gendisk *disk);
117 static void sd_rw_intr(struct scsi_cmnd * SCpnt);
119 static int sd_probe(struct device *);
120 static int sd_remove(struct device *);
121 static void sd_shutdown(struct device *dev);
122 static void sd_rescan(struct device *);
123 static int sd_init_command(struct scsi_cmnd *);
124 static int sd_issue_flush(struct device *, sector_t *);
125 static void sd_end_flush(request_queue_t *, struct request *);
126 static int sd_prepare_flush(request_queue_t *, struct request *);
127 static void sd_read_capacity(struct scsi_disk *sdkp, char *diskname,
128 struct scsi_request *SRpnt, unsigned char *buffer);
130 static struct scsi_driver sd_template = {
131 .owner = THIS_MODULE,
136 .shutdown = sd_shutdown,
139 .init_command = sd_init_command,
140 .issue_flush = sd_issue_flush,
141 .prepare_flush = sd_prepare_flush,
142 .end_flush = sd_end_flush,
146 * Device no to disk mapping:
148 * major disc2 disc p1
149 * |............|.............|....|....| <- dev_t
152 * Inside a major, we have 16k disks, however mapped non-
153 * contiguously. The first 16 disks are for major0, the next
154 * ones with major1, ... Disk 256 is for major0 again, disk 272
156 * As we stay compatible with our numbering scheme, we can reuse
157 * the well-know SCSI majors 8, 65--71, 136--143.
159 static int sd_major(int major_idx)
163 return SCSI_DISK0_MAJOR;
165 return SCSI_DISK1_MAJOR + major_idx - 1;
167 return SCSI_DISK8_MAJOR + major_idx - 8;
170 return 0; /* shut up gcc */
174 #define to_scsi_disk(obj) container_of(obj,struct scsi_disk,kref)
176 static inline struct scsi_disk *scsi_disk(struct gendisk *disk)
178 return container_of(disk->private_data, struct scsi_disk, driver);
181 static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
183 struct scsi_disk *sdkp = NULL;
186 if (disk->private_data == NULL)
188 sdkp = scsi_disk(disk);
189 kref_get(&sdkp->kref);
190 if (scsi_device_get(sdkp->device))
196 kref_put(&sdkp->kref, scsi_disk_release);
203 static void scsi_disk_put(struct scsi_disk *sdkp)
205 struct scsi_device *sdev = sdkp->device;
208 kref_put(&sdkp->kref, scsi_disk_release);
209 scsi_device_put(sdev);
214 * sd_init_command - build a scsi (read or write) command from
215 * information in the request structure.
216 * @SCpnt: pointer to mid-level's per scsi command structure that
217 * contains request and into which the scsi command is written
219 * Returns 1 if successful and 0 if error (or cannot be done now).
221 static int sd_init_command(struct scsi_cmnd * SCpnt)
223 unsigned int this_count, timeout;
224 struct gendisk *disk;
226 struct scsi_device *sdp = SCpnt->device;
227 struct request *rq = SCpnt->request;
229 timeout = sdp->timeout;
232 * SG_IO from block layer already setup, just copy cdb basically
234 if (blk_pc_request(rq)) {
235 if (sizeof(rq->cmd) > sizeof(SCpnt->cmnd))
238 memcpy(SCpnt->cmnd, rq->cmd, sizeof(SCpnt->cmnd));
239 if (rq_data_dir(rq) == WRITE)
240 SCpnt->sc_data_direction = DMA_TO_DEVICE;
241 else if (rq->data_len)
242 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
244 SCpnt->sc_data_direction = DMA_NONE;
246 this_count = rq->data_len;
248 timeout = rq->timeout;
250 SCpnt->transfersize = rq->data_len;
251 SCpnt->allowed = SD_PASSTHROUGH_RETRIES;
256 * we only do REQ_CMD and REQ_BLOCK_PC
258 if (!blk_fs_request(rq))
263 this_count = SCpnt->request_bufflen >> 9;
265 SCSI_LOG_HLQUEUE(1, printk("sd_init_command: disk=%s, block=%llu, "
266 "count=%d\n", disk->disk_name,
267 (unsigned long long)block, this_count));
269 if (!sdp || !scsi_device_online(sdp) ||
270 block + rq->nr_sectors > get_capacity(disk)) {
271 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
273 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
279 * quietly refuse to do anything to a changed disc until
280 * the changed bit has been reset
282 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
285 SCSI_LOG_HLQUEUE(2, printk("%s : block=%llu\n",
286 disk->disk_name, (unsigned long long)block));
289 * If we have a 1K hardware sectorsize, prevent access to single
290 * 512 byte sectors. In theory we could handle this - in fact
291 * the scsi cdrom driver must be able to handle this because
292 * we typically use 1K blocksizes, and cdroms typically have
293 * 2K hardware sectorsizes. Of course, things are simpler
294 * with the cdrom, since it is read-only. For performance
295 * reasons, the filesystems should be able to handle this
296 * and not force the scsi disk driver to use bounce buffers
299 if (sdp->sector_size == 1024) {
300 if ((block & 1) || (rq->nr_sectors & 1)) {
301 printk(KERN_ERR "sd: Bad block number requested");
305 this_count = this_count >> 1;
308 if (sdp->sector_size == 2048) {
309 if ((block & 3) || (rq->nr_sectors & 3)) {
310 printk(KERN_ERR "sd: Bad block number requested");
314 this_count = this_count >> 2;
317 if (sdp->sector_size == 4096) {
318 if ((block & 7) || (rq->nr_sectors & 7)) {
319 printk(KERN_ERR "sd: Bad block number requested");
323 this_count = this_count >> 3;
326 if (rq_data_dir(rq) == WRITE) {
327 if (!sdp->writeable) {
330 SCpnt->cmnd[0] = WRITE_6;
331 SCpnt->sc_data_direction = DMA_TO_DEVICE;
332 } else if (rq_data_dir(rq) == READ) {
333 SCpnt->cmnd[0] = READ_6;
334 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
336 printk(KERN_ERR "sd: Unknown command %lx\n", rq->flags);
337 /* overkill panic("Unknown sd command %lx\n", rq->flags); */
341 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
342 disk->disk_name, (rq_data_dir(rq) == WRITE) ?
343 "writing" : "reading", this_count, rq->nr_sectors));
347 if (block > 0xffffffff) {
348 SCpnt->cmnd[0] += READ_16 - READ_6;
349 SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
350 SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
351 SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
352 SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
353 SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
354 SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
355 SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
356 SCpnt->cmnd[9] = (unsigned char) block & 0xff;
357 SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
358 SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
359 SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
360 SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
361 SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
362 } else if ((this_count > 0xff) || (block > 0x1fffff) ||
363 SCpnt->device->use_10_for_rw) {
364 if (this_count > 0xffff)
367 SCpnt->cmnd[0] += READ_10 - READ_6;
368 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
369 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
370 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
371 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
372 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
373 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
374 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
376 SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
377 SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
378 SCpnt->cmnd[3] = (unsigned char) block & 0xff;
379 SCpnt->cmnd[4] = (unsigned char) this_count;
382 SCpnt->request_bufflen = SCpnt->bufflen =
383 this_count * sdp->sector_size;
386 * We shouldn't disconnect in the middle of a sector, so with a dumb
387 * host adapter, it's safe to assume that we can at least transfer
388 * this many bytes between each connect / disconnect.
390 SCpnt->transfersize = sdp->sector_size;
391 SCpnt->underflow = this_count << 9;
392 SCpnt->allowed = SD_MAX_RETRIES;
395 SCpnt->timeout_per_command = timeout;
398 * This is the completion routine we use. This is matched in terms
399 * of capability to this function.
401 SCpnt->done = sd_rw_intr;
404 * This indicates that the command is ready from our end to be
411 * sd_open - open a scsi disk device
412 * @inode: only i_rdev member may be used
413 * @filp: only f_mode and f_flags may be used
415 * Returns 0 if successful. Returns a negated errno value in case
418 * Note: This can be called from a user context (e.g. fsck(1) )
419 * or from within the kernel (e.g. as a result of a mount(1) ).
420 * In the latter case @inode and @filp carry an abridged amount
421 * of information as noted above.
423 static int sd_open(struct inode *inode, struct file *filp)
425 struct gendisk *disk = inode->i_bdev->bd_disk;
426 struct scsi_disk *sdkp;
427 struct scsi_device *sdev;
430 if (!(sdkp = scsi_disk_get(disk)))
434 SCSI_LOG_HLQUEUE(3, printk("sd_open: disk=%s\n", disk->disk_name));
439 * If the device is in error recovery, wait until it is done.
440 * If the device is offline, then disallow any access to it.
443 if (!scsi_block_when_processing_errors(sdev))
446 if (sdev->removable || sdkp->write_prot)
447 check_disk_change(inode->i_bdev);
450 * If the drive is empty, just let the open fail.
453 if (sdev->removable && !sdkp->media_present &&
454 !(filp->f_flags & O_NDELAY))
458 * If the device has the write protect tab set, have the open fail
459 * if the user expects to be able to write to the thing.
462 if (sdkp->write_prot && (filp->f_mode & FMODE_WRITE))
466 * It is possible that the disk changing stuff resulted in
467 * the device being taken offline. If this is the case,
468 * report this to the user, and don't pretend that the
469 * open actually succeeded.
472 if (!scsi_device_online(sdev))
475 if (!sdkp->openers++ && sdev->removable) {
476 if (scsi_block_when_processing_errors(sdev))
477 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
488 * sd_release - invoked when the (last) close(2) is called on this
490 * @inode: only i_rdev member may be used
491 * @filp: only f_mode and f_flags may be used
495 * Note: may block (uninterruptible) if error recovery is underway
498 static int sd_release(struct inode *inode, struct file *filp)
500 struct gendisk *disk = inode->i_bdev->bd_disk;
501 struct scsi_disk *sdkp = scsi_disk(disk);
502 struct scsi_device *sdev = sdkp->device;
504 SCSI_LOG_HLQUEUE(3, printk("sd_release: disk=%s\n", disk->disk_name));
506 if (!--sdkp->openers && sdev->removable) {
507 if (scsi_block_when_processing_errors(sdev))
508 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
512 * XXX and what if there are packets in flight and this close()
513 * XXX is followed by a "rmmod sd_mod"?
519 static int sd_hdio_getgeo(struct block_device *bdev, struct hd_geometry __user *loc)
521 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
522 struct scsi_device *sdp = sdkp->device;
523 struct Scsi_Host *host = sdp->host;
526 /* default to most commonly used values */
527 diskinfo[0] = 0x40; /* 1 << 6 */
528 diskinfo[1] = 0x20; /* 1 << 5 */
529 diskinfo[2] = sdkp->capacity >> 11;
531 /* override with calculated, extended default, or driver values */
532 if (host->hostt->bios_param)
533 host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo);
535 scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
537 if (put_user(diskinfo[0], &loc->heads))
539 if (put_user(diskinfo[1], &loc->sectors))
541 if (put_user(diskinfo[2], &loc->cylinders))
543 if (put_user((unsigned)get_start_sect(bdev),
544 (unsigned long __user *)&loc->start))
550 * sd_ioctl - process an ioctl
551 * @inode: only i_rdev/i_bdev members may be used
552 * @filp: only f_mode and f_flags may be used
553 * @cmd: ioctl command number
554 * @arg: this is third argument given to ioctl(2) system call.
555 * Often contains a pointer.
557 * Returns 0 if successful (some ioctls return postive numbers on
558 * success as well). Returns a negated errno value in case of error.
560 * Note: most ioctls are forward onto the block subsystem or further
561 * down in the scsi subsytem.
563 static int sd_ioctl(struct inode * inode, struct file * filp,
564 unsigned int cmd, unsigned long arg)
566 struct block_device *bdev = inode->i_bdev;
567 struct gendisk *disk = bdev->bd_disk;
568 struct scsi_device *sdp = scsi_disk(disk)->device;
569 void __user *p = (void __user *)arg;
572 SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
573 disk->disk_name, cmd));
576 * If we are in the middle of error recovery, don't let anyone
577 * else try and use this device. Also, if error recovery fails, it
578 * may try and take the device offline, in which case all further
579 * access to the device is prohibited.
581 error = scsi_nonblockable_ioctl(sdp, cmd, p, filp);
582 if (!scsi_block_when_processing_errors(sdp) || !error)
585 if (cmd == HDIO_GETGEO) {
588 return sd_hdio_getgeo(bdev, p);
592 * Send SCSI addressing ioctls directly to mid level, send other
593 * ioctls to block level and then onto mid level if they can't be
597 case SCSI_IOCTL_GET_IDLUN:
598 case SCSI_IOCTL_GET_BUS_NUMBER:
599 return scsi_ioctl(sdp, cmd, p);
601 error = scsi_cmd_ioctl(filp, disk, cmd, p);
602 if (error != -ENOTTY)
605 return scsi_ioctl(sdp, cmd, p);
608 static void set_media_not_present(struct scsi_disk *sdkp)
610 sdkp->media_present = 0;
612 sdkp->device->changed = 1;
616 * sd_media_changed - check if our medium changed
617 * @disk: kernel device descriptor
619 * Returns 0 if not applicable or no change; 1 if change
621 * Note: this function is invoked from the block subsystem.
623 static int sd_media_changed(struct gendisk *disk)
625 struct scsi_disk *sdkp = scsi_disk(disk);
626 struct scsi_device *sdp = sdkp->device;
629 SCSI_LOG_HLQUEUE(3, printk("sd_media_changed: disk=%s\n",
636 * If the device is offline, don't send any commands - just pretend as
637 * if the command failed. If the device ever comes back online, we
638 * can deal with it then. It is only because of unrecoverable errors
639 * that we would ever take a device offline in the first place.
641 if (!scsi_device_online(sdp))
645 * Using TEST_UNIT_READY enables differentiation between drive with
646 * no cartridge loaded - NOT READY, drive with changed cartridge -
647 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
649 * Drives that auto spin down. eg iomega jaz 1G, will be started
650 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
651 * sd_revalidate() is called.
654 if (scsi_block_when_processing_errors(sdp))
655 retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES);
658 * Unable to test, unit probably not ready. This usually
659 * means there is no disc in the drive. Mark as changed,
660 * and we will figure it out later once the drive is
667 * For removable scsi disk we have to recognise the presence
668 * of a disk in the drive. This is kept in the struct scsi_disk
669 * struct and tested at open ! Daniel Roche (dan@lectra.fr)
671 sdkp->media_present = 1;
673 retval = sdp->changed;
679 set_media_not_present(sdkp);
683 static int sd_sync_cache(struct scsi_device *sdp)
685 struct scsi_request *sreq;
688 if (!scsi_device_online(sdp))
691 sreq = scsi_allocate_request(sdp, GFP_KERNEL);
693 printk("FAILED\n No memory for request\n");
697 sreq->sr_data_direction = DMA_NONE;
698 for (retries = 3; retries > 0; --retries) {
699 unsigned char cmd[10] = { 0 };
701 cmd[0] = SYNCHRONIZE_CACHE;
703 * Leave the rest of the command zero to indicate
706 scsi_wait_req(sreq, cmd, NULL, 0, SD_TIMEOUT, SD_MAX_RETRIES);
707 if (sreq->sr_result == 0)
711 res = sreq->sr_result;
713 printk(KERN_WARNING "FAILED\n status = %x, message = %02x, "
714 "host = %d, driver = %02x\n ",
715 status_byte(res), msg_byte(res),
716 host_byte(res), driver_byte(res));
717 if (driver_byte(res) & DRIVER_SENSE)
718 scsi_print_req_sense("sd", sreq);
721 scsi_release_request(sreq);
725 static int sd_issue_flush(struct device *dev, sector_t *error_sector)
727 struct scsi_device *sdp = to_scsi_device(dev);
728 struct scsi_disk *sdkp = dev_get_drvdata(dev);
736 return sd_sync_cache(sdp);
739 static void sd_end_flush(request_queue_t *q, struct request *flush_rq)
741 struct request *rq = flush_rq->end_io_data;
742 struct scsi_cmnd *cmd = rq->special;
743 unsigned int bytes = rq->hard_nr_sectors << 9;
745 if (!flush_rq->errors) {
746 spin_unlock(q->queue_lock);
747 scsi_io_completion(cmd, bytes, 0);
748 spin_lock(q->queue_lock);
749 } else if (blk_barrier_postflush(rq)) {
750 spin_unlock(q->queue_lock);
751 scsi_io_completion(cmd, 0, bytes);
752 spin_lock(q->queue_lock);
755 * force journal abort of barriers
757 end_that_request_first(rq, -EOPNOTSUPP, rq->hard_nr_sectors);
758 end_that_request_last(rq);
762 static int sd_prepare_flush(request_queue_t *q, struct request *rq)
764 struct scsi_device *sdev = q->queuedata;
765 struct scsi_disk *sdkp = dev_get_drvdata(&sdev->sdev_gendev);
768 memset(rq->cmd, 0, sizeof(rq->cmd));
769 rq->flags |= REQ_BLOCK_PC | REQ_SOFTBARRIER;
770 rq->timeout = SD_TIMEOUT;
771 rq->cmd[0] = SYNCHRONIZE_CACHE;
778 static void sd_rescan(struct device *dev)
780 struct scsi_disk *sdkp = dev_get_drvdata(dev);
781 sd_revalidate_disk(sdkp->disk);
787 * This gets directly called from VFS. When the ioctl
788 * is not recognized we go back to the other translation paths.
790 static long sd_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
792 struct block_device *bdev = file->f_dentry->d_inode->i_bdev;
793 struct gendisk *disk = bdev->bd_disk;
794 struct scsi_device *sdev = scsi_disk(disk)->device;
797 * If we are in the middle of error recovery, don't let anyone
798 * else try and use this device. Also, if error recovery fails, it
799 * may try and take the device offline, in which case all further
800 * access to the device is prohibited.
802 if (!scsi_block_when_processing_errors(sdev))
805 if (sdev->host->hostt->compat_ioctl) {
808 ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
814 * Let the static ioctl translation table take care of it.
820 static struct block_device_operations sd_fops = {
821 .owner = THIS_MODULE,
823 .release = sd_release,
826 .compat_ioctl = sd_compat_ioctl,
828 .media_changed = sd_media_changed,
829 .revalidate_disk = sd_revalidate_disk,
833 * sd_rw_intr - bottom half handler: called when the lower level
834 * driver has completed (successfully or otherwise) a scsi command.
835 * @SCpnt: mid-level's per command structure.
837 * Note: potentially run from within an ISR. Must not block.
839 static void sd_rw_intr(struct scsi_cmnd * SCpnt)
841 int result = SCpnt->result;
842 int this_count = SCpnt->bufflen;
843 int good_bytes = (result == 0 ? this_count : 0);
844 sector_t block_sectors = 1;
846 sector_t error_sector;
847 struct scsi_sense_hdr sshdr;
849 int sense_deferred = 0;
853 sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
855 sense_deferred = scsi_sense_is_deferred(&sshdr);
858 #ifdef CONFIG_SCSI_LOGGING
859 SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: %s: res=0x%x\n",
860 SCpnt->request->rq_disk->disk_name, result));
862 SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: sb[respc,sk,asc,"
863 "ascq]=%x,%x,%x,%x\n", sshdr.response_code,
864 sshdr.sense_key, sshdr.asc, sshdr.ascq));
868 Handle MEDIUM ERRORs that indicate partial success. Since this is a
869 relatively rare error condition, no care is taken to avoid
870 unnecessary additional work such as memcpy's that could be avoided.
874 * If SG_IO from block layer then set good_bytes to stop retries;
875 * else if errors, check them, and if necessary prepare for
878 if (blk_pc_request(SCpnt->request))
879 good_bytes = this_count;
880 else if (driver_byte(result) != 0 &&
881 sense_valid && !sense_deferred) {
882 switch (sshdr.sense_key) {
884 if (!blk_fs_request(SCpnt->request))
886 info_valid = scsi_get_sense_info_fld(
887 SCpnt->sense_buffer, SCSI_SENSE_BUFFERSIZE,
890 * May want to warn and skip if following cast results
891 * in actual truncation (if sector_t < 64 bits)
893 error_sector = (sector_t)first_err_block;
894 if (SCpnt->request->bio != NULL)
895 block_sectors = bio_sectors(SCpnt->request->bio);
896 switch (SCpnt->device->sector_size) {
899 if (block_sectors < 2)
904 if (block_sectors < 4)
909 if (block_sectors < 8)
919 error_sector &= ~(block_sectors - 1);
920 good_bytes = (error_sector - SCpnt->request->sector) << 9;
921 if (good_bytes < 0 || good_bytes >= this_count)
925 case RECOVERED_ERROR: /* an error occurred, but it recovered */
926 case NO_SENSE: /* LLDD got sense data */
928 * Inform the user, but make sure that it's not treated
931 scsi_print_sense("sd", SCpnt);
933 memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
934 good_bytes = this_count;
937 case ILLEGAL_REQUEST:
938 if (SCpnt->device->use_10_for_rw &&
939 (SCpnt->cmnd[0] == READ_10 ||
940 SCpnt->cmnd[0] == WRITE_10))
941 SCpnt->device->use_10_for_rw = 0;
942 if (SCpnt->device->use_10_for_ms &&
943 (SCpnt->cmnd[0] == MODE_SENSE_10 ||
944 SCpnt->cmnd[0] == MODE_SELECT_10))
945 SCpnt->device->use_10_for_ms = 0;
953 * This calls the generic completion function, now that we know
954 * how many actual sectors finished, and how many sectors we need
955 * to say have failed.
957 scsi_io_completion(SCpnt, good_bytes, block_sectors << 9);
960 static int media_not_present(struct scsi_disk *sdkp, struct scsi_request *srp)
962 struct scsi_sense_hdr sshdr;
966 if (!(driver_byte(srp->sr_result) & DRIVER_SENSE))
968 /* not invoked for commands that could return deferred errors */
969 if (scsi_request_normalize_sense(srp, &sshdr)) {
970 if (sshdr.sense_key != NOT_READY &&
971 sshdr.sense_key != UNIT_ATTENTION)
973 if (sshdr.asc != 0x3A) /* medium not present */
976 set_media_not_present(sdkp);
981 * spinup disk - called only in sd_revalidate_disk()
984 sd_spinup_disk(struct scsi_disk *sdkp, char *diskname,
985 struct scsi_request *SRpnt, unsigned char *buffer) {
986 unsigned char cmd[10];
987 unsigned long spintime_value = 0;
988 int retries, spintime;
989 unsigned int the_result;
990 struct scsi_sense_hdr sshdr;
995 /* Spin up drives, as required. Only do this at boot time */
996 /* Spinup needs to be done for module loads too. */
1001 cmd[0] = TEST_UNIT_READY;
1002 memset((void *) &cmd[1], 0, 9);
1004 SRpnt->sr_cmd_len = 0;
1005 memset(SRpnt->sr_sense_buffer, 0,
1006 SCSI_SENSE_BUFFERSIZE);
1007 SRpnt->sr_data_direction = DMA_NONE;
1009 scsi_wait_req (SRpnt, (void *) cmd, (void *) buffer,
1010 0/*512*/, SD_TIMEOUT, SD_MAX_RETRIES);
1012 the_result = SRpnt->sr_result;
1014 sense_valid = scsi_request_normalize_sense(
1017 } while (retries < 3 &&
1018 (!scsi_status_is_good(the_result) ||
1019 ((driver_byte(the_result) & DRIVER_SENSE) &&
1020 sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
1023 * If the drive has indicated to us that it doesn't have
1024 * any media in it, don't bother with any of the rest of
1027 if (media_not_present(sdkp, SRpnt))
1030 if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
1031 /* no sense, TUR either succeeded or failed
1032 * with a status error */
1033 if(!spintime && !scsi_status_is_good(the_result))
1034 printk(KERN_NOTICE "%s: Unit Not Ready, "
1035 "error = 0x%x\n", diskname, the_result);
1040 * The device does not want the automatic start to be issued.
1042 if (sdkp->device->no_start_on_add) {
1047 * If manual intervention is required, or this is an
1048 * absent USB storage device, a spinup is meaningless.
1051 sshdr.sense_key == NOT_READY &&
1052 sshdr.asc == 4 && sshdr.ascq == 3) {
1053 break; /* manual intervention required */
1056 * Issue command to spin up drive when not ready
1058 } else if (sense_valid && sshdr.sense_key == NOT_READY) {
1060 printk(KERN_NOTICE "%s: Spinning up disk...",
1062 cmd[0] = START_STOP;
1063 cmd[1] = 1; /* Return immediately */
1064 memset((void *) &cmd[2], 0, 8);
1065 cmd[4] = 1; /* Start spin cycle */
1066 SRpnt->sr_cmd_len = 0;
1067 memset(SRpnt->sr_sense_buffer, 0,
1068 SCSI_SENSE_BUFFERSIZE);
1070 SRpnt->sr_data_direction = DMA_NONE;
1071 scsi_wait_req(SRpnt, (void *)cmd,
1072 (void *) buffer, 0/*512*/,
1073 SD_TIMEOUT, SD_MAX_RETRIES);
1074 spintime_value = jiffies;
1077 /* Wait 1 second for next try */
1081 /* we don't understand the sense code, so it's
1082 * probably pointless to loop */
1084 printk(KERN_NOTICE "%s: Unit Not Ready, "
1085 "sense:\n", diskname);
1086 scsi_print_req_sense("", SRpnt);
1091 } while (spintime &&
1092 time_after(spintime_value + 100 * HZ, jiffies));
1095 if (scsi_status_is_good(the_result))
1098 printk("not responding...\n");
1103 * read disk capacity
1106 sd_read_capacity(struct scsi_disk *sdkp, char *diskname,
1107 struct scsi_request *SRpnt, unsigned char *buffer) {
1108 unsigned char cmd[16];
1109 struct scsi_device *sdp = sdkp->device;
1110 int the_result, retries;
1111 int sector_size = 0;
1113 struct scsi_sense_hdr sshdr;
1114 int sense_valid = 0;
1120 memset((void *) cmd, 0, 16);
1121 cmd[0] = SERVICE_ACTION_IN;
1122 cmd[1] = SAI_READ_CAPACITY_16;
1124 memset((void *) buffer, 0, 12);
1126 cmd[0] = READ_CAPACITY;
1127 memset((void *) &cmd[1], 0, 9);
1128 memset((void *) buffer, 0, 8);
1131 SRpnt->sr_cmd_len = 0;
1132 memset(SRpnt->sr_sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1133 SRpnt->sr_data_direction = DMA_FROM_DEVICE;
1135 scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
1136 longrc ? 12 : 8, SD_TIMEOUT, SD_MAX_RETRIES);
1138 if (media_not_present(sdkp, SRpnt))
1141 the_result = SRpnt->sr_result;
1143 sense_valid = scsi_request_normalize_sense(SRpnt,
1147 } while (the_result && retries);
1149 if (the_result && !longrc) {
1150 printk(KERN_NOTICE "%s : READ CAPACITY failed.\n"
1151 "%s : status=%x, message=%02x, host=%d, driver=%02x \n",
1153 status_byte(the_result),
1154 msg_byte(the_result),
1155 host_byte(the_result),
1156 driver_byte(the_result));
1158 if (driver_byte(the_result) & DRIVER_SENSE)
1159 scsi_print_req_sense("sd", SRpnt);
1161 printk("%s : sense not available. \n", diskname);
1163 /* Set dirty bit for removable devices if not ready -
1164 * sometimes drives will not report this properly. */
1165 if (sdp->removable &&
1166 sense_valid && sshdr.sense_key == NOT_READY)
1169 /* Either no media are present but the drive didn't tell us,
1170 or they are present but the read capacity command fails */
1171 /* sdkp->media_present = 0; -- not always correct */
1172 sdkp->capacity = 0x200000; /* 1 GB - random */
1175 } else if (the_result && longrc) {
1176 /* READ CAPACITY(16) has been failed */
1177 printk(KERN_NOTICE "%s : READ CAPACITY(16) failed.\n"
1178 "%s : status=%x, message=%02x, host=%d, driver=%02x \n",
1180 status_byte(the_result),
1181 msg_byte(the_result),
1182 host_byte(the_result),
1183 driver_byte(the_result));
1184 printk(KERN_NOTICE "%s : use 0xffffffff as device size\n",
1187 sdkp->capacity = 1 + (sector_t) 0xffffffff;
1192 sector_size = (buffer[4] << 24) |
1193 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
1194 if (buffer[0] == 0xff && buffer[1] == 0xff &&
1195 buffer[2] == 0xff && buffer[3] == 0xff) {
1196 if(sizeof(sdkp->capacity) > 4) {
1197 printk(KERN_NOTICE "%s : very big device. try to use"
1198 " READ CAPACITY(16).\n", diskname);
1202 printk(KERN_ERR "%s: too big for this kernel. Use a "
1203 "kernel compiled with support for large block "
1204 "devices.\n", diskname);
1208 sdkp->capacity = 1 + (((sector_t)buffer[0] << 24) |
1213 sdkp->capacity = 1 + (((u64)buffer[0] << 56) |
1214 ((u64)buffer[1] << 48) |
1215 ((u64)buffer[2] << 40) |
1216 ((u64)buffer[3] << 32) |
1217 ((sector_t)buffer[4] << 24) |
1218 ((sector_t)buffer[5] << 16) |
1219 ((sector_t)buffer[6] << 8) |
1220 (sector_t)buffer[7]);
1222 sector_size = (buffer[8] << 24) |
1223 (buffer[9] << 16) | (buffer[10] << 8) | buffer[11];
1226 /* Some devices return the total number of sectors, not the
1227 * highest sector number. Make the necessary adjustment. */
1228 if (sdp->fix_capacity)
1232 if (sector_size == 0) {
1234 printk(KERN_NOTICE "%s : sector size 0 reported, "
1235 "assuming 512.\n", diskname);
1238 if (sector_size != 512 &&
1239 sector_size != 1024 &&
1240 sector_size != 2048 &&
1241 sector_size != 4096 &&
1242 sector_size != 256) {
1243 printk(KERN_NOTICE "%s : unsupported sector size "
1244 "%d.\n", diskname, sector_size);
1246 * The user might want to re-format the drive with
1247 * a supported sectorsize. Once this happens, it
1248 * would be relatively trivial to set the thing up.
1249 * For this reason, we leave the thing in the table.
1253 * set a bogus sector size so the normal read/write
1254 * logic in the block layer will eventually refuse any
1255 * request on this device without tripping over power
1256 * of two sector size assumptions
1262 * The msdos fs needs to know the hardware sector size
1263 * So I have created this table. See ll_rw_blk.c
1264 * Jacques Gelinas (Jacques@solucorp.qc.ca)
1266 int hard_sector = sector_size;
1267 sector_t sz = sdkp->capacity * (hard_sector/256);
1268 request_queue_t *queue = sdp->request_queue;
1271 blk_queue_hardsect_size(queue, hard_sector);
1272 /* avoid 64-bit division on 32-bit platforms */
1274 sector_div(sz, 1250);
1276 sector_div(mb, 1950);
1278 printk(KERN_NOTICE "SCSI device %s: "
1279 "%llu %d-byte hdwr sectors (%llu MB)\n",
1280 diskname, (unsigned long long)sdkp->capacity,
1281 hard_sector, (unsigned long long)mb);
1284 /* Rescale capacity to 512-byte units */
1285 if (sector_size == 4096)
1286 sdkp->capacity <<= 3;
1287 else if (sector_size == 2048)
1288 sdkp->capacity <<= 2;
1289 else if (sector_size == 1024)
1290 sdkp->capacity <<= 1;
1291 else if (sector_size == 256)
1292 sdkp->capacity >>= 1;
1294 sdkp->device->sector_size = sector_size;
1297 /* called with buffer of length 512 */
1299 sd_do_mode_sense(struct scsi_request *SRpnt, int dbd, int modepage,
1300 unsigned char *buffer, int len, struct scsi_mode_data *data)
1302 return __scsi_mode_sense(SRpnt, dbd, modepage, buffer, len,
1303 SD_TIMEOUT, SD_MAX_RETRIES, data);
1307 * read write protect setting, if possible - called only in sd_revalidate_disk()
1308 * called with buffer of length 512
1311 sd_read_write_protect_flag(struct scsi_disk *sdkp, char *diskname,
1312 struct scsi_request *SRpnt, unsigned char *buffer) {
1314 struct scsi_mode_data data;
1316 set_disk_ro(sdkp->disk, 0);
1317 if (sdkp->device->skip_ms_page_3f) {
1318 printk(KERN_NOTICE "%s: assuming Write Enabled\n", diskname);
1322 if (sdkp->device->use_192_bytes_for_3f) {
1323 res = sd_do_mode_sense(SRpnt, 0, 0x3F, buffer, 192, &data);
1326 * First attempt: ask for all pages (0x3F), but only 4 bytes.
1327 * We have to start carefully: some devices hang if we ask
1328 * for more than is available.
1330 res = sd_do_mode_sense(SRpnt, 0, 0x3F, buffer, 4, &data);
1333 * Second attempt: ask for page 0 When only page 0 is
1334 * implemented, a request for page 3F may return Sense Key
1335 * 5: Illegal Request, Sense Code 24: Invalid field in
1338 if (!scsi_status_is_good(res))
1339 res = sd_do_mode_sense(SRpnt, 0, 0, buffer, 4, &data);
1342 * Third attempt: ask 255 bytes, as we did earlier.
1344 if (!scsi_status_is_good(res))
1345 res = sd_do_mode_sense(SRpnt, 0, 0x3F, buffer, 255,
1349 if (!scsi_status_is_good(res)) {
1351 "%s: test WP failed, assume Write Enabled\n", diskname);
1353 sdkp->write_prot = ((data.device_specific & 0x80) != 0);
1354 set_disk_ro(sdkp->disk, sdkp->write_prot);
1355 printk(KERN_NOTICE "%s: Write Protect is %s\n", diskname,
1356 sdkp->write_prot ? "on" : "off");
1357 printk(KERN_DEBUG "%s: Mode Sense: %02x %02x %02x %02x\n",
1358 diskname, buffer[0], buffer[1], buffer[2], buffer[3]);
1363 * sd_read_cache_type - called only from sd_revalidate_disk()
1364 * called with buffer of length 512
1367 sd_read_cache_type(struct scsi_disk *sdkp, char *diskname,
1368 struct scsi_request *SRpnt, unsigned char *buffer)
1374 struct scsi_mode_data data;
1375 struct scsi_sense_hdr sshdr;
1377 if (sdkp->device->skip_ms_page_8)
1380 if (sdkp->device->type == TYPE_RBC) {
1388 /* cautiously ask */
1389 res = sd_do_mode_sense(SRpnt, dbd, modepage, buffer, 4, &data);
1391 if (!scsi_status_is_good(res))
1394 /* that went OK, now ask for the proper length */
1398 * We're only interested in the first three bytes, actually.
1399 * But the data cache page is defined for the first 20.
1406 /* Take headers and block descriptors into account */
1407 len += data.header_length + data.block_descriptor_length;
1410 res = sd_do_mode_sense(SRpnt, dbd, modepage, buffer, len, &data);
1412 if (scsi_status_is_good(res)) {
1413 const char *types[] = {
1414 "write through", "none", "write back",
1415 "write back, no read (daft)"
1418 int offset = data.header_length + data.block_descriptor_length;
1420 if ((buffer[offset] & 0x3f) != modepage) {
1421 printk(KERN_ERR "%s: got wrong page\n", diskname);
1425 if (modepage == 8) {
1426 sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
1427 sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
1429 sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
1433 ct = sdkp->RCD + 2*sdkp->WCE;
1435 printk(KERN_NOTICE "SCSI device %s: drive cache: %s\n",
1436 diskname, types[ct]);
1442 if (scsi_request_normalize_sense(SRpnt, &sshdr) &&
1443 sshdr.sense_key == ILLEGAL_REQUEST &&
1444 sshdr.asc == 0x24 && sshdr.ascq == 0x0)
1445 printk(KERN_NOTICE "%s: cache data unavailable\n",
1446 diskname); /* Invalid field in CDB */
1448 printk(KERN_ERR "%s: asking for cache data failed\n",
1452 printk(KERN_ERR "%s: assuming drive cache: write through\n",
1459 * sd_revalidate_disk - called the first time a new disk is seen,
1460 * performs disk spin up, read_capacity, etc.
1461 * @disk: struct gendisk we care about
1463 static int sd_revalidate_disk(struct gendisk *disk)
1465 struct scsi_disk *sdkp = scsi_disk(disk);
1466 struct scsi_device *sdp = sdkp->device;
1467 struct scsi_request *sreq;
1468 unsigned char *buffer;
1470 SCSI_LOG_HLQUEUE(3, printk("sd_revalidate_disk: disk=%s\n", disk->disk_name));
1473 * If the device is offline, don't try and read capacity or any
1474 * of the other niceties.
1476 if (!scsi_device_online(sdp))
1479 sreq = scsi_allocate_request(sdp, GFP_KERNEL);
1481 printk(KERN_WARNING "(sd_revalidate_disk:) Request allocation "
1486 buffer = kmalloc(512, GFP_KERNEL | __GFP_DMA);
1488 printk(KERN_WARNING "(sd_revalidate_disk:) Memory allocation "
1490 goto out_release_request;
1493 /* defaults, until the device tells us otherwise */
1494 sdp->sector_size = 512;
1496 sdkp->media_present = 1;
1497 sdkp->write_prot = 0;
1501 sd_spinup_disk(sdkp, disk->disk_name, sreq, buffer);
1504 * Without media there is no reason to ask; moreover, some devices
1505 * react badly if we do.
1507 if (sdkp->media_present) {
1508 sd_read_capacity(sdkp, disk->disk_name, sreq, buffer);
1510 sd_read_write_protect_flag(sdkp, disk->disk_name,
1512 sd_read_cache_type(sdkp, disk->disk_name, sreq, buffer);
1515 set_capacity(disk, sdkp->capacity);
1518 out_release_request:
1519 scsi_release_request(sreq);
1525 * sd_probe - called during driver initialization and whenever a
1526 * new scsi device is attached to the system. It is called once
1527 * for each scsi device (not just disks) present.
1528 * @dev: pointer to device object
1530 * Returns 0 if successful (or not interested in this scsi device
1531 * (e.g. scanner)); 1 when there is an error.
1533 * Note: this function is invoked from the scsi mid-level.
1534 * This function sets up the mapping between a given
1535 * <host,channel,id,lun> (found in sdp) and new device name
1536 * (e.g. /dev/sda). More precisely it is the block device major
1537 * and minor number that is chosen here.
1539 * Assume sd_attach is not re-entrant (for time being)
1540 * Also think about sd_attach() and sd_remove() running coincidentally.
1542 static int sd_probe(struct device *dev)
1544 struct scsi_device *sdp = to_scsi_device(dev);
1545 struct scsi_disk *sdkp;
1551 if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
1554 SCSI_LOG_HLQUEUE(3, printk("sd_attach: scsi device: <%d,%d,%d,%d>\n",
1555 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun));
1558 sdkp = kmalloc(sizeof(*sdkp), GFP_KERNEL);
1562 memset (sdkp, 0, sizeof(*sdkp));
1563 kref_init(&sdkp->kref);
1565 gd = alloc_disk(16);
1569 if (!idr_pre_get(&sd_index_idr, GFP_KERNEL))
1572 spin_lock(&sd_index_lock);
1573 error = idr_get_new(&sd_index_idr, NULL, &index);
1574 spin_unlock(&sd_index_lock);
1576 if (index >= SD_MAX_DISKS)
1582 sdkp->driver = &sd_template;
1584 sdkp->index = index;
1587 if (!sdp->timeout) {
1588 if (sdp->type != TYPE_MOD)
1589 sdp->timeout = SD_TIMEOUT;
1591 sdp->timeout = SD_MOD_TIMEOUT;
1594 gd->major = sd_major((index & 0xf0) >> 4);
1595 gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
1597 gd->fops = &sd_fops;
1600 sprintf(gd->disk_name, "sd%c", 'a' + index % 26);
1601 } else if (index < (26 + 1) * 26) {
1602 sprintf(gd->disk_name, "sd%c%c",
1603 'a' + index / 26 - 1,'a' + index % 26);
1605 const unsigned int m1 = (index / 26 - 1) / 26 - 1;
1606 const unsigned int m2 = (index / 26 - 1) % 26;
1607 const unsigned int m3 = index % 26;
1608 sprintf(gd->disk_name, "sd%c%c%c",
1609 'a' + m1, 'a' + m2, 'a' + m3);
1612 strcpy(gd->devfs_name, sdp->devfs_name);
1614 gd->private_data = &sdkp->driver;
1616 sd_revalidate_disk(gd);
1618 gd->driverfs_dev = &sdp->sdev_gendev;
1619 gd->flags = GENHD_FL_DRIVERFS;
1621 gd->flags |= GENHD_FL_REMOVABLE;
1622 gd->queue = sdkp->device->request_queue;
1624 dev_set_drvdata(dev, sdkp);
1627 printk(KERN_NOTICE "Attached scsi %sdisk %s at scsi%d, channel %d, "
1628 "id %d, lun %d\n", sdp->removable ? "removable " : "",
1629 gd->disk_name, sdp->host->host_no, sdp->channel,
1643 * sd_remove - called whenever a scsi disk (previously recognized by
1644 * sd_probe) is detached from the system. It is called (potentially
1645 * multiple times) during sd module unload.
1646 * @sdp: pointer to mid level scsi device object
1648 * Note: this function is invoked from the scsi mid-level.
1649 * This function potentially frees up a device name (e.g. /dev/sdc)
1650 * that could be re-used by a subsequent sd_probe().
1651 * This function is not called when the built-in sd driver is "exit-ed".
1653 static int sd_remove(struct device *dev)
1655 struct scsi_disk *sdkp = dev_get_drvdata(dev);
1657 del_gendisk(sdkp->disk);
1660 kref_put(&sdkp->kref, scsi_disk_release);
1667 * scsi_disk_release - Called to free the scsi_disk structure
1668 * @kref: pointer to embedded kref
1670 * sd_ref_sem must be held entering this routine. Because it is
1671 * called on last put, you should always use the scsi_disk_get()
1672 * scsi_disk_put() helpers which manipulate the semaphore directly
1673 * and never do a direct kref_put().
1675 static void scsi_disk_release(struct kref *kref)
1677 struct scsi_disk *sdkp = to_scsi_disk(kref);
1678 struct gendisk *disk = sdkp->disk;
1680 spin_lock(&sd_index_lock);
1681 idr_remove(&sd_index_idr, sdkp->index);
1682 spin_unlock(&sd_index_lock);
1684 disk->private_data = NULL;
1692 * Send a SYNCHRONIZE CACHE instruction down to the device through
1693 * the normal SCSI command structure. Wait for the command to
1696 static void sd_shutdown(struct device *dev)
1698 struct scsi_device *sdp = to_scsi_device(dev);
1699 struct scsi_disk *sdkp = dev_get_drvdata(dev);
1702 return; /* this can happen */
1707 printk(KERN_NOTICE "Synchronizing SCSI cache for disk %s: \n",
1708 sdkp->disk->disk_name);
1713 * init_sd - entry point for this driver (both when built in or when
1716 * Note: this function registers this driver with the scsi mid-level.
1718 static int __init init_sd(void)
1722 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
1724 for (i = 0; i < SD_MAJORS; i++)
1725 if (register_blkdev(sd_major(i), "sd") == 0)
1731 return scsi_register_driver(&sd_template.gendrv);
1735 * exit_sd - exit point for this driver (when it is a module).
1737 * Note: this function unregisters this driver from the scsi mid-level.
1739 static void __exit exit_sd(void)
1743 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
1745 scsi_unregister_driver(&sd_template.gendrv);
1746 for (i = 0; i < SD_MAJORS; i++)
1747 unregister_blkdev(sd_major(i), "sd");
1750 MODULE_LICENSE("GPL");
1751 MODULE_AUTHOR("Eric Youngdale");
1752 MODULE_DESCRIPTION("SCSI disk (sd) driver");
1754 module_init(init_sd);
1755 module_exit(exit_sd);