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/scsicam.h>
64 #include "scsi_logging.h"
67 * More than enough for everybody ;) The huge number of majors
68 * is a leftover from 16bit dev_t days, we don't really need that
74 * This is limited by the naming scheme enforced in sd_probe,
75 * add another character to it if you really need more disks.
77 #define SD_MAX_DISKS (((26 * 26) + 26 + 1) * 26)
80 * Time out in seconds for disks and Magneto-opticals (which are slower).
82 #define SD_TIMEOUT (30 * HZ)
83 #define SD_MOD_TIMEOUT (75 * HZ)
86 * Number of allowed retries
88 #define SD_MAX_RETRIES 5
89 #define SD_PASSTHROUGH_RETRIES 1
91 static void scsi_disk_release(struct kref
*kref
);
94 struct scsi_driver
*driver
; /* always &sd_template */
95 struct scsi_device
*device
;
98 unsigned int openers
; /* protected by BKL for now, yuck */
99 sector_t capacity
; /* size in 512-byte sectors */
103 unsigned WCE
: 1; /* state of disk WCE bit */
104 unsigned RCD
: 1; /* state of disk RCD bit, unused */
107 static DEFINE_IDR(sd_index_idr
);
108 static DEFINE_SPINLOCK(sd_index_lock
);
110 /* This semaphore is used to mediate the 0->1 reference get in the
111 * face of object destruction (i.e. we can't allow a get on an
112 * object after last put) */
113 static DECLARE_MUTEX(sd_ref_sem
);
115 static int sd_revalidate_disk(struct gendisk
*disk
);
116 static void sd_rw_intr(struct scsi_cmnd
* SCpnt
);
118 static int sd_probe(struct device
*);
119 static int sd_remove(struct device
*);
120 static void sd_shutdown(struct device
*dev
);
121 static void sd_rescan(struct device
*);
122 static int sd_init_command(struct scsi_cmnd
*);
123 static int sd_issue_flush(struct device
*, sector_t
*);
124 static void sd_end_flush(request_queue_t
*, struct request
*);
125 static int sd_prepare_flush(request_queue_t
*, struct request
*);
126 static void sd_read_capacity(struct scsi_disk
*sdkp
, char *diskname
,
127 unsigned char *buffer
);
129 static struct scsi_driver sd_template
= {
130 .owner
= THIS_MODULE
,
135 .shutdown
= sd_shutdown
,
138 .init_command
= sd_init_command
,
139 .issue_flush
= sd_issue_flush
,
140 .prepare_flush
= sd_prepare_flush
,
141 .end_flush
= sd_end_flush
,
145 * Device no to disk mapping:
147 * major disc2 disc p1
148 * |............|.............|....|....| <- dev_t
151 * Inside a major, we have 16k disks, however mapped non-
152 * contiguously. The first 16 disks are for major0, the next
153 * ones with major1, ... Disk 256 is for major0 again, disk 272
155 * As we stay compatible with our numbering scheme, we can reuse
156 * the well-know SCSI majors 8, 65--71, 136--143.
158 static int sd_major(int major_idx
)
162 return SCSI_DISK0_MAJOR
;
164 return SCSI_DISK1_MAJOR
+ major_idx
- 1;
166 return SCSI_DISK8_MAJOR
+ major_idx
- 8;
169 return 0; /* shut up gcc */
173 #define to_scsi_disk(obj) container_of(obj,struct scsi_disk,kref)
175 static inline struct scsi_disk
*scsi_disk(struct gendisk
*disk
)
177 return container_of(disk
->private_data
, struct scsi_disk
, driver
);
180 static struct scsi_disk
*__scsi_disk_get(struct gendisk
*disk
)
182 struct scsi_disk
*sdkp
= NULL
;
184 if (disk
->private_data
) {
185 sdkp
= scsi_disk(disk
);
186 if (scsi_device_get(sdkp
->device
) == 0)
187 kref_get(&sdkp
->kref
);
194 static struct scsi_disk
*scsi_disk_get(struct gendisk
*disk
)
196 struct scsi_disk
*sdkp
;
199 sdkp
= __scsi_disk_get(disk
);
204 static struct scsi_disk
*scsi_disk_get_from_dev(struct device
*dev
)
206 struct scsi_disk
*sdkp
;
209 sdkp
= dev_get_drvdata(dev
);
211 sdkp
= __scsi_disk_get(sdkp
->disk
);
216 static void scsi_disk_put(struct scsi_disk
*sdkp
)
218 struct scsi_device
*sdev
= sdkp
->device
;
221 kref_put(&sdkp
->kref
, scsi_disk_release
);
222 scsi_device_put(sdev
);
227 * sd_init_command - build a scsi (read or write) command from
228 * information in the request structure.
229 * @SCpnt: pointer to mid-level's per scsi command structure that
230 * contains request and into which the scsi command is written
232 * Returns 1 if successful and 0 if error (or cannot be done now).
234 static int sd_init_command(struct scsi_cmnd
* SCpnt
)
236 unsigned int this_count
, timeout
;
237 struct gendisk
*disk
;
239 struct scsi_device
*sdp
= SCpnt
->device
;
240 struct request
*rq
= SCpnt
->request
;
242 timeout
= sdp
->timeout
;
245 * SG_IO from block layer already setup, just copy cdb basically
247 if (blk_pc_request(rq
)) {
248 if (sizeof(rq
->cmd
) > sizeof(SCpnt
->cmnd
))
251 memcpy(SCpnt
->cmnd
, rq
->cmd
, sizeof(SCpnt
->cmnd
));
252 SCpnt
->cmd_len
= rq
->cmd_len
;
253 if (rq_data_dir(rq
) == WRITE
)
254 SCpnt
->sc_data_direction
= DMA_TO_DEVICE
;
255 else if (rq
->data_len
)
256 SCpnt
->sc_data_direction
= DMA_FROM_DEVICE
;
258 SCpnt
->sc_data_direction
= DMA_NONE
;
260 this_count
= rq
->data_len
;
262 timeout
= rq
->timeout
;
264 SCpnt
->transfersize
= rq
->data_len
;
265 SCpnt
->allowed
= SD_PASSTHROUGH_RETRIES
;
270 * we only do REQ_CMD and REQ_BLOCK_PC
272 if (!blk_fs_request(rq
))
277 this_count
= SCpnt
->request_bufflen
>> 9;
279 SCSI_LOG_HLQUEUE(1, printk("sd_init_command: disk=%s, block=%llu, "
280 "count=%d\n", disk
->disk_name
,
281 (unsigned long long)block
, this_count
));
283 if (!sdp
|| !scsi_device_online(sdp
) ||
284 block
+ rq
->nr_sectors
> get_capacity(disk
)) {
285 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
287 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt
));
293 * quietly refuse to do anything to a changed disc until
294 * the changed bit has been reset
296 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
299 SCSI_LOG_HLQUEUE(2, printk("%s : block=%llu\n",
300 disk
->disk_name
, (unsigned long long)block
));
303 * If we have a 1K hardware sectorsize, prevent access to single
304 * 512 byte sectors. In theory we could handle this - in fact
305 * the scsi cdrom driver must be able to handle this because
306 * we typically use 1K blocksizes, and cdroms typically have
307 * 2K hardware sectorsizes. Of course, things are simpler
308 * with the cdrom, since it is read-only. For performance
309 * reasons, the filesystems should be able to handle this
310 * and not force the scsi disk driver to use bounce buffers
313 if (sdp
->sector_size
== 1024) {
314 if ((block
& 1) || (rq
->nr_sectors
& 1)) {
315 printk(KERN_ERR
"sd: Bad block number requested");
319 this_count
= this_count
>> 1;
322 if (sdp
->sector_size
== 2048) {
323 if ((block
& 3) || (rq
->nr_sectors
& 3)) {
324 printk(KERN_ERR
"sd: Bad block number requested");
328 this_count
= this_count
>> 2;
331 if (sdp
->sector_size
== 4096) {
332 if ((block
& 7) || (rq
->nr_sectors
& 7)) {
333 printk(KERN_ERR
"sd: Bad block number requested");
337 this_count
= this_count
>> 3;
340 if (rq_data_dir(rq
) == WRITE
) {
341 if (!sdp
->writeable
) {
344 SCpnt
->cmnd
[0] = WRITE_6
;
345 SCpnt
->sc_data_direction
= DMA_TO_DEVICE
;
346 } else if (rq_data_dir(rq
) == READ
) {
347 SCpnt
->cmnd
[0] = READ_6
;
348 SCpnt
->sc_data_direction
= DMA_FROM_DEVICE
;
350 printk(KERN_ERR
"sd: Unknown command %lx\n", rq
->flags
);
351 /* overkill panic("Unknown sd command %lx\n", rq->flags); */
355 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
356 disk
->disk_name
, (rq_data_dir(rq
) == WRITE
) ?
357 "writing" : "reading", this_count
, rq
->nr_sectors
));
361 if (block
> 0xffffffff) {
362 SCpnt
->cmnd
[0] += READ_16
- READ_6
;
363 SCpnt
->cmnd
[2] = sizeof(block
) > 4 ? (unsigned char) (block
>> 56) & 0xff : 0;
364 SCpnt
->cmnd
[3] = sizeof(block
) > 4 ? (unsigned char) (block
>> 48) & 0xff : 0;
365 SCpnt
->cmnd
[4] = sizeof(block
) > 4 ? (unsigned char) (block
>> 40) & 0xff : 0;
366 SCpnt
->cmnd
[5] = sizeof(block
) > 4 ? (unsigned char) (block
>> 32) & 0xff : 0;
367 SCpnt
->cmnd
[6] = (unsigned char) (block
>> 24) & 0xff;
368 SCpnt
->cmnd
[7] = (unsigned char) (block
>> 16) & 0xff;
369 SCpnt
->cmnd
[8] = (unsigned char) (block
>> 8) & 0xff;
370 SCpnt
->cmnd
[9] = (unsigned char) block
& 0xff;
371 SCpnt
->cmnd
[10] = (unsigned char) (this_count
>> 24) & 0xff;
372 SCpnt
->cmnd
[11] = (unsigned char) (this_count
>> 16) & 0xff;
373 SCpnt
->cmnd
[12] = (unsigned char) (this_count
>> 8) & 0xff;
374 SCpnt
->cmnd
[13] = (unsigned char) this_count
& 0xff;
375 SCpnt
->cmnd
[14] = SCpnt
->cmnd
[15] = 0;
376 } else if ((this_count
> 0xff) || (block
> 0x1fffff) ||
377 SCpnt
->device
->use_10_for_rw
) {
378 if (this_count
> 0xffff)
381 SCpnt
->cmnd
[0] += READ_10
- READ_6
;
382 SCpnt
->cmnd
[2] = (unsigned char) (block
>> 24) & 0xff;
383 SCpnt
->cmnd
[3] = (unsigned char) (block
>> 16) & 0xff;
384 SCpnt
->cmnd
[4] = (unsigned char) (block
>> 8) & 0xff;
385 SCpnt
->cmnd
[5] = (unsigned char) block
& 0xff;
386 SCpnt
->cmnd
[6] = SCpnt
->cmnd
[9] = 0;
387 SCpnt
->cmnd
[7] = (unsigned char) (this_count
>> 8) & 0xff;
388 SCpnt
->cmnd
[8] = (unsigned char) this_count
& 0xff;
390 SCpnt
->cmnd
[1] |= (unsigned char) ((block
>> 16) & 0x1f);
391 SCpnt
->cmnd
[2] = (unsigned char) ((block
>> 8) & 0xff);
392 SCpnt
->cmnd
[3] = (unsigned char) block
& 0xff;
393 SCpnt
->cmnd
[4] = (unsigned char) this_count
;
396 SCpnt
->request_bufflen
= SCpnt
->bufflen
=
397 this_count
* sdp
->sector_size
;
400 * We shouldn't disconnect in the middle of a sector, so with a dumb
401 * host adapter, it's safe to assume that we can at least transfer
402 * this many bytes between each connect / disconnect.
404 SCpnt
->transfersize
= sdp
->sector_size
;
405 SCpnt
->underflow
= this_count
<< 9;
406 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_hdio_getgeo(struct block_device
*bdev
, struct hd_geometry __user
*loc
)
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 if (put_user(diskinfo
[0], &loc
->heads
))
553 if (put_user(diskinfo
[1], &loc
->sectors
))
555 if (put_user(diskinfo
[2], &loc
->cylinders
))
557 if (put_user((unsigned)get_start_sect(bdev
),
558 (unsigned long __user
*)&loc
->start
))
564 * sd_ioctl - process an ioctl
565 * @inode: only i_rdev/i_bdev members may be used
566 * @filp: only f_mode and f_flags may be used
567 * @cmd: ioctl command number
568 * @arg: this is third argument given to ioctl(2) system call.
569 * Often contains a pointer.
571 * Returns 0 if successful (some ioctls return postive numbers on
572 * success as well). Returns a negated errno value in case of error.
574 * Note: most ioctls are forward onto the block subsystem or further
575 * down in the scsi subsytem.
577 static int sd_ioctl(struct inode
* inode
, struct file
* filp
,
578 unsigned int cmd
, unsigned long arg
)
580 struct block_device
*bdev
= inode
->i_bdev
;
581 struct gendisk
*disk
= bdev
->bd_disk
;
582 struct scsi_device
*sdp
= scsi_disk(disk
)->device
;
583 void __user
*p
= (void __user
*)arg
;
586 SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
587 disk
->disk_name
, cmd
));
590 * If we are in the middle of error recovery, don't let anyone
591 * else try and use this device. Also, if error recovery fails, it
592 * may try and take the device offline, in which case all further
593 * access to the device is prohibited.
595 error
= scsi_nonblockable_ioctl(sdp
, cmd
, p
, filp
);
596 if (!scsi_block_when_processing_errors(sdp
) || !error
)
599 if (cmd
== HDIO_GETGEO
) {
602 return sd_hdio_getgeo(bdev
, p
);
606 * Send SCSI addressing ioctls directly to mid level, send other
607 * ioctls to block level and then onto mid level if they can't be
611 case SCSI_IOCTL_GET_IDLUN
:
612 case SCSI_IOCTL_GET_BUS_NUMBER
:
613 return scsi_ioctl(sdp
, cmd
, p
);
615 error
= scsi_cmd_ioctl(filp
, disk
, cmd
, p
);
616 if (error
!= -ENOTTY
)
619 return scsi_ioctl(sdp
, cmd
, p
);
622 static void set_media_not_present(struct scsi_disk
*sdkp
)
624 sdkp
->media_present
= 0;
626 sdkp
->device
->changed
= 1;
630 * sd_media_changed - check if our medium changed
631 * @disk: kernel device descriptor
633 * Returns 0 if not applicable or no change; 1 if change
635 * Note: this function is invoked from the block subsystem.
637 static int sd_media_changed(struct gendisk
*disk
)
639 struct scsi_disk
*sdkp
= scsi_disk(disk
);
640 struct scsi_device
*sdp
= sdkp
->device
;
643 SCSI_LOG_HLQUEUE(3, printk("sd_media_changed: disk=%s\n",
650 * If the device is offline, don't send any commands - just pretend as
651 * if the command failed. If the device ever comes back online, we
652 * can deal with it then. It is only because of unrecoverable errors
653 * that we would ever take a device offline in the first place.
655 if (!scsi_device_online(sdp
))
659 * Using TEST_UNIT_READY enables differentiation between drive with
660 * no cartridge loaded - NOT READY, drive with changed cartridge -
661 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
663 * Drives that auto spin down. eg iomega jaz 1G, will be started
664 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
665 * sd_revalidate() is called.
668 if (scsi_block_when_processing_errors(sdp
))
669 retval
= scsi_test_unit_ready(sdp
, SD_TIMEOUT
, SD_MAX_RETRIES
);
672 * Unable to test, unit probably not ready. This usually
673 * means there is no disc in the drive. Mark as changed,
674 * and we will figure it out later once the drive is
681 * For removable scsi disk we have to recognise the presence
682 * of a disk in the drive. This is kept in the struct scsi_disk
683 * struct and tested at open ! Daniel Roche (dan@lectra.fr)
685 sdkp
->media_present
= 1;
687 retval
= sdp
->changed
;
693 set_media_not_present(sdkp
);
697 static int sd_sync_cache(struct scsi_device
*sdp
)
700 struct scsi_sense_hdr sshdr
;
702 if (!scsi_device_online(sdp
))
706 for (retries
= 3; retries
> 0; --retries
) {
707 unsigned char cmd
[10] = { 0 };
709 cmd
[0] = SYNCHRONIZE_CACHE
;
711 * Leave the rest of the command zero to indicate
714 res
= scsi_execute_req(sdp
, cmd
, DMA_NONE
, NULL
, 0, &sshdr
,
715 SD_TIMEOUT
, SD_MAX_RETRIES
);
720 if (res
) { printk(KERN_WARNING
"FAILED\n status = %x, message = %02x, "
721 "host = %d, driver = %02x\n ",
722 status_byte(res
), msg_byte(res
),
723 host_byte(res
), driver_byte(res
));
724 if (driver_byte(res
) & DRIVER_SENSE
)
725 scsi_print_sense_hdr("sd", &sshdr
);
731 static int sd_issue_flush(struct device
*dev
, sector_t
*error_sector
)
734 struct scsi_device
*sdp
= to_scsi_device(dev
);
735 struct scsi_disk
*sdkp
= scsi_disk_get_from_dev(dev
);
741 ret
= sd_sync_cache(sdp
);
746 static void sd_end_flush(request_queue_t
*q
, struct request
*flush_rq
)
748 struct request
*rq
= flush_rq
->end_io_data
;
749 struct scsi_cmnd
*cmd
= rq
->special
;
750 unsigned int bytes
= rq
->hard_nr_sectors
<< 9;
752 if (!flush_rq
->errors
) {
753 spin_unlock(q
->queue_lock
);
754 scsi_io_completion(cmd
, bytes
, 0);
755 spin_lock(q
->queue_lock
);
756 } else if (blk_barrier_postflush(rq
)) {
757 spin_unlock(q
->queue_lock
);
758 scsi_io_completion(cmd
, 0, bytes
);
759 spin_lock(q
->queue_lock
);
762 * force journal abort of barriers
764 end_that_request_first(rq
, -EOPNOTSUPP
, rq
->hard_nr_sectors
);
765 end_that_request_last(rq
);
769 static int sd_prepare_flush(request_queue_t
*q
, struct request
*rq
)
771 struct scsi_device
*sdev
= q
->queuedata
;
772 struct scsi_disk
*sdkp
= dev_get_drvdata(&sdev
->sdev_gendev
);
774 if (!sdkp
|| !sdkp
->WCE
)
777 memset(rq
->cmd
, 0, sizeof(rq
->cmd
));
778 rq
->flags
|= REQ_BLOCK_PC
| REQ_SOFTBARRIER
;
779 rq
->timeout
= SD_TIMEOUT
;
780 rq
->cmd
[0] = SYNCHRONIZE_CACHE
;
784 static void sd_rescan(struct device
*dev
)
786 struct scsi_disk
*sdkp
= scsi_disk_get_from_dev(dev
);
789 sd_revalidate_disk(sdkp
->disk
);
797 * This gets directly called from VFS. When the ioctl
798 * is not recognized we go back to the other translation paths.
800 static long sd_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
802 struct block_device
*bdev
= file
->f_dentry
->d_inode
->i_bdev
;
803 struct gendisk
*disk
= bdev
->bd_disk
;
804 struct scsi_device
*sdev
= scsi_disk(disk
)->device
;
807 * If we are in the middle of error recovery, don't let anyone
808 * else try and use this device. Also, if error recovery fails, it
809 * may try and take the device offline, in which case all further
810 * access to the device is prohibited.
812 if (!scsi_block_when_processing_errors(sdev
))
815 if (sdev
->host
->hostt
->compat_ioctl
) {
818 ret
= sdev
->host
->hostt
->compat_ioctl(sdev
, cmd
, (void __user
*)arg
);
824 * Let the static ioctl translation table take care of it.
830 static struct block_device_operations sd_fops
= {
831 .owner
= THIS_MODULE
,
833 .release
= sd_release
,
836 .compat_ioctl
= sd_compat_ioctl
,
838 .media_changed
= sd_media_changed
,
839 .revalidate_disk
= sd_revalidate_disk
,
843 * sd_rw_intr - bottom half handler: called when the lower level
844 * driver has completed (successfully or otherwise) a scsi command.
845 * @SCpnt: mid-level's per command structure.
847 * Note: potentially run from within an ISR. Must not block.
849 static void sd_rw_intr(struct scsi_cmnd
* SCpnt
)
851 int result
= SCpnt
->result
;
852 int this_count
= SCpnt
->bufflen
;
853 int good_bytes
= (result
== 0 ? this_count
: 0);
854 sector_t block_sectors
= 1;
856 sector_t error_sector
;
857 struct scsi_sense_hdr sshdr
;
859 int sense_deferred
= 0;
863 sense_valid
= scsi_command_normalize_sense(SCpnt
, &sshdr
);
865 sense_deferred
= scsi_sense_is_deferred(&sshdr
);
868 #ifdef CONFIG_SCSI_LOGGING
869 SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: %s: res=0x%x\n",
870 SCpnt
->request
->rq_disk
->disk_name
, result
));
872 SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: sb[respc,sk,asc,"
873 "ascq]=%x,%x,%x,%x\n", sshdr
.response_code
,
874 sshdr
.sense_key
, sshdr
.asc
, sshdr
.ascq
));
878 Handle MEDIUM ERRORs that indicate partial success. Since this is a
879 relatively rare error condition, no care is taken to avoid
880 unnecessary additional work such as memcpy's that could be avoided.
884 * If SG_IO from block layer then set good_bytes to stop retries;
885 * else if errors, check them, and if necessary prepare for
888 if (blk_pc_request(SCpnt
->request
))
889 good_bytes
= this_count
;
890 else if (driver_byte(result
) != 0 &&
891 sense_valid
&& !sense_deferred
) {
892 switch (sshdr
.sense_key
) {
894 if (!blk_fs_request(SCpnt
->request
))
896 info_valid
= scsi_get_sense_info_fld(
897 SCpnt
->sense_buffer
, SCSI_SENSE_BUFFERSIZE
,
900 * May want to warn and skip if following cast results
901 * in actual truncation (if sector_t < 64 bits)
903 error_sector
= (sector_t
)first_err_block
;
904 if (SCpnt
->request
->bio
!= NULL
)
905 block_sectors
= bio_sectors(SCpnt
->request
->bio
);
906 switch (SCpnt
->device
->sector_size
) {
909 if (block_sectors
< 2)
914 if (block_sectors
< 4)
919 if (block_sectors
< 8)
929 error_sector
&= ~(block_sectors
- 1);
930 good_bytes
= (error_sector
- SCpnt
->request
->sector
) << 9;
931 if (good_bytes
< 0 || good_bytes
>= this_count
)
935 case RECOVERED_ERROR
: /* an error occurred, but it recovered */
936 case NO_SENSE
: /* LLDD got sense data */
938 * Inform the user, but make sure that it's not treated
941 scsi_print_sense("sd", SCpnt
);
943 memset(SCpnt
->sense_buffer
, 0, SCSI_SENSE_BUFFERSIZE
);
944 good_bytes
= this_count
;
947 case ILLEGAL_REQUEST
:
948 if (SCpnt
->device
->use_10_for_rw
&&
949 (SCpnt
->cmnd
[0] == READ_10
||
950 SCpnt
->cmnd
[0] == WRITE_10
))
951 SCpnt
->device
->use_10_for_rw
= 0;
952 if (SCpnt
->device
->use_10_for_ms
&&
953 (SCpnt
->cmnd
[0] == MODE_SENSE_10
||
954 SCpnt
->cmnd
[0] == MODE_SELECT_10
))
955 SCpnt
->device
->use_10_for_ms
= 0;
963 * This calls the generic completion function, now that we know
964 * how many actual sectors finished, and how many sectors we need
965 * to say have failed.
967 scsi_io_completion(SCpnt
, good_bytes
, block_sectors
<< 9);
970 static int media_not_present(struct scsi_disk
*sdkp
,
971 struct scsi_sense_hdr
*sshdr
)
974 if (!scsi_sense_valid(sshdr
))
976 /* not invoked for commands that could return deferred errors */
977 if (sshdr
->sense_key
!= NOT_READY
&&
978 sshdr
->sense_key
!= UNIT_ATTENTION
)
980 if (sshdr
->asc
!= 0x3A) /* medium not present */
983 set_media_not_present(sdkp
);
988 * spinup disk - called only in sd_revalidate_disk()
991 sd_spinup_disk(struct scsi_disk
*sdkp
, char *diskname
)
993 unsigned char cmd
[10];
994 unsigned long spintime_expire
= 0;
995 int retries
, spintime
;
996 unsigned int the_result
;
997 struct scsi_sense_hdr sshdr
;
1002 /* Spin up drives, as required. Only do this at boot time */
1003 /* Spinup needs to be done for module loads too. */
1008 cmd
[0] = TEST_UNIT_READY
;
1009 memset((void *) &cmd
[1], 0, 9);
1011 the_result
= scsi_execute_req(sdkp
->device
, cmd
,
1017 sense_valid
= scsi_sense_valid(&sshdr
);
1019 } while (retries
< 3 &&
1020 (!scsi_status_is_good(the_result
) ||
1021 ((driver_byte(the_result
) & DRIVER_SENSE
) &&
1022 sense_valid
&& sshdr
.sense_key
== UNIT_ATTENTION
)));
1025 * If the drive has indicated to us that it doesn't have
1026 * any media in it, don't bother with any of the rest of
1029 if (media_not_present(sdkp
, &sshdr
))
1032 if ((driver_byte(the_result
) & DRIVER_SENSE
) == 0) {
1033 /* no sense, TUR either succeeded or failed
1034 * with a status error */
1035 if(!spintime
&& !scsi_status_is_good(the_result
))
1036 printk(KERN_NOTICE
"%s: Unit Not Ready, "
1037 "error = 0x%x\n", diskname
, the_result
);
1042 * The device does not want the automatic start to be issued.
1044 if (sdkp
->device
->no_start_on_add
) {
1049 * If manual intervention is required, or this is an
1050 * absent USB storage device, a spinup is meaningless.
1053 sshdr
.sense_key
== NOT_READY
&&
1054 sshdr
.asc
== 4 && sshdr
.ascq
== 3) {
1055 break; /* manual intervention required */
1058 * Issue command to spin up drive when not ready
1060 } else if (sense_valid
&& sshdr
.sense_key
== NOT_READY
) {
1062 printk(KERN_NOTICE
"%s: Spinning up disk...",
1064 cmd
[0] = START_STOP
;
1065 cmd
[1] = 1; /* Return immediately */
1066 memset((void *) &cmd
[2], 0, 8);
1067 cmd
[4] = 1; /* Start spin cycle */
1068 scsi_execute_req(sdkp
->device
, cmd
, DMA_NONE
,
1070 SD_TIMEOUT
, SD_MAX_RETRIES
);
1071 spintime_expire
= jiffies
+ 100 * HZ
;
1074 /* Wait 1 second for next try */
1079 * Wait for USB flash devices with slow firmware.
1080 * Yes, this sense key/ASC combination shouldn't
1081 * occur here. It's characteristic of these devices.
1083 } else if (sense_valid
&&
1084 sshdr
.sense_key
== UNIT_ATTENTION
&&
1085 sshdr
.asc
== 0x28) {
1087 spintime_expire
= jiffies
+ 5 * HZ
;
1090 /* Wait 1 second for next try */
1093 /* we don't understand the sense code, so it's
1094 * probably pointless to loop */
1096 printk(KERN_NOTICE
"%s: Unit Not Ready, "
1097 "sense:\n", diskname
);
1098 scsi_print_sense_hdr("", &sshdr
);
1103 } while (spintime
&& time_before_eq(jiffies
, spintime_expire
));
1106 if (scsi_status_is_good(the_result
))
1109 printk("not responding...\n");
1114 * read disk capacity
1117 sd_read_capacity(struct scsi_disk
*sdkp
, char *diskname
,
1118 unsigned char *buffer
)
1120 unsigned char cmd
[16];
1121 int the_result
, retries
;
1122 int sector_size
= 0;
1124 struct scsi_sense_hdr sshdr
;
1125 int sense_valid
= 0;
1126 struct scsi_device
*sdp
= sdkp
->device
;
1132 memset((void *) cmd
, 0, 16);
1133 cmd
[0] = SERVICE_ACTION_IN
;
1134 cmd
[1] = SAI_READ_CAPACITY_16
;
1136 memset((void *) buffer
, 0, 12);
1138 cmd
[0] = READ_CAPACITY
;
1139 memset((void *) &cmd
[1], 0, 9);
1140 memset((void *) buffer
, 0, 8);
1143 the_result
= scsi_execute_req(sdp
, cmd
, DMA_FROM_DEVICE
,
1144 buffer
, longrc
? 12 : 8, &sshdr
,
1145 SD_TIMEOUT
, SD_MAX_RETRIES
);
1147 if (media_not_present(sdkp
, &sshdr
))
1151 sense_valid
= scsi_sense_valid(&sshdr
);
1154 } while (the_result
&& retries
);
1156 if (the_result
&& !longrc
) {
1157 printk(KERN_NOTICE
"%s : READ CAPACITY failed.\n"
1158 "%s : status=%x, message=%02x, host=%d, driver=%02x \n",
1160 status_byte(the_result
),
1161 msg_byte(the_result
),
1162 host_byte(the_result
),
1163 driver_byte(the_result
));
1165 if (driver_byte(the_result
) & DRIVER_SENSE
)
1166 scsi_print_sense_hdr("sd", &sshdr
);
1168 printk("%s : sense not available. \n", diskname
);
1170 /* Set dirty bit for removable devices if not ready -
1171 * sometimes drives will not report this properly. */
1172 if (sdp
->removable
&&
1173 sense_valid
&& sshdr
.sense_key
== NOT_READY
)
1176 /* Either no media are present but the drive didn't tell us,
1177 or they are present but the read capacity command fails */
1178 /* sdkp->media_present = 0; -- not always correct */
1179 sdkp
->capacity
= 0x200000; /* 1 GB - random */
1182 } else if (the_result
&& longrc
) {
1183 /* READ CAPACITY(16) has been failed */
1184 printk(KERN_NOTICE
"%s : READ CAPACITY(16) failed.\n"
1185 "%s : status=%x, message=%02x, host=%d, driver=%02x \n",
1187 status_byte(the_result
),
1188 msg_byte(the_result
),
1189 host_byte(the_result
),
1190 driver_byte(the_result
));
1191 printk(KERN_NOTICE
"%s : use 0xffffffff as device size\n",
1194 sdkp
->capacity
= 1 + (sector_t
) 0xffffffff;
1199 sector_size
= (buffer
[4] << 24) |
1200 (buffer
[5] << 16) | (buffer
[6] << 8) | buffer
[7];
1201 if (buffer
[0] == 0xff && buffer
[1] == 0xff &&
1202 buffer
[2] == 0xff && buffer
[3] == 0xff) {
1203 if(sizeof(sdkp
->capacity
) > 4) {
1204 printk(KERN_NOTICE
"%s : very big device. try to use"
1205 " READ CAPACITY(16).\n", diskname
);
1209 printk(KERN_ERR
"%s: too big for this kernel. Use a "
1210 "kernel compiled with support for large block "
1211 "devices.\n", diskname
);
1215 sdkp
->capacity
= 1 + (((sector_t
)buffer
[0] << 24) |
1220 sdkp
->capacity
= 1 + (((u64
)buffer
[0] << 56) |
1221 ((u64
)buffer
[1] << 48) |
1222 ((u64
)buffer
[2] << 40) |
1223 ((u64
)buffer
[3] << 32) |
1224 ((sector_t
)buffer
[4] << 24) |
1225 ((sector_t
)buffer
[5] << 16) |
1226 ((sector_t
)buffer
[6] << 8) |
1227 (sector_t
)buffer
[7]);
1229 sector_size
= (buffer
[8] << 24) |
1230 (buffer
[9] << 16) | (buffer
[10] << 8) | buffer
[11];
1233 /* Some devices return the total number of sectors, not the
1234 * highest sector number. Make the necessary adjustment. */
1235 if (sdp
->fix_capacity
)
1239 if (sector_size
== 0) {
1241 printk(KERN_NOTICE
"%s : sector size 0 reported, "
1242 "assuming 512.\n", diskname
);
1245 if (sector_size
!= 512 &&
1246 sector_size
!= 1024 &&
1247 sector_size
!= 2048 &&
1248 sector_size
!= 4096 &&
1249 sector_size
!= 256) {
1250 printk(KERN_NOTICE
"%s : unsupported sector size "
1251 "%d.\n", diskname
, sector_size
);
1253 * The user might want to re-format the drive with
1254 * a supported sectorsize. Once this happens, it
1255 * would be relatively trivial to set the thing up.
1256 * For this reason, we leave the thing in the table.
1260 * set a bogus sector size so the normal read/write
1261 * logic in the block layer will eventually refuse any
1262 * request on this device without tripping over power
1263 * of two sector size assumptions
1269 * The msdos fs needs to know the hardware sector size
1270 * So I have created this table. See ll_rw_blk.c
1271 * Jacques Gelinas (Jacques@solucorp.qc.ca)
1273 int hard_sector
= sector_size
;
1274 sector_t sz
= (sdkp
->capacity
/2) * (hard_sector
/256);
1275 request_queue_t
*queue
= sdp
->request_queue
;
1278 blk_queue_hardsect_size(queue
, hard_sector
);
1279 /* avoid 64-bit division on 32-bit platforms */
1280 sector_div(sz
, 625);
1282 sector_div(mb
, 1950);
1284 printk(KERN_NOTICE
"SCSI device %s: "
1285 "%llu %d-byte hdwr sectors (%llu MB)\n",
1286 diskname
, (unsigned long long)sdkp
->capacity
,
1287 hard_sector
, (unsigned long long)mb
);
1290 /* Rescale capacity to 512-byte units */
1291 if (sector_size
== 4096)
1292 sdkp
->capacity
<<= 3;
1293 else if (sector_size
== 2048)
1294 sdkp
->capacity
<<= 2;
1295 else if (sector_size
== 1024)
1296 sdkp
->capacity
<<= 1;
1297 else if (sector_size
== 256)
1298 sdkp
->capacity
>>= 1;
1300 sdkp
->device
->sector_size
= sector_size
;
1303 /* called with buffer of length 512 */
1305 sd_do_mode_sense(struct scsi_device
*sdp
, int dbd
, int modepage
,
1306 unsigned char *buffer
, int len
, struct scsi_mode_data
*data
,
1307 struct scsi_sense_hdr
*sshdr
)
1309 return scsi_mode_sense(sdp
, dbd
, modepage
, buffer
, len
,
1310 SD_TIMEOUT
, SD_MAX_RETRIES
, data
,
1315 * read write protect setting, if possible - called only in sd_revalidate_disk()
1316 * called with buffer of length 512
1319 sd_read_write_protect_flag(struct scsi_disk
*sdkp
, char *diskname
,
1320 unsigned char *buffer
)
1323 struct scsi_device
*sdp
= sdkp
->device
;
1324 struct scsi_mode_data data
;
1326 set_disk_ro(sdkp
->disk
, 0);
1327 if (sdp
->skip_ms_page_3f
) {
1328 printk(KERN_NOTICE
"%s: assuming Write Enabled\n", diskname
);
1332 if (sdp
->use_192_bytes_for_3f
) {
1333 res
= sd_do_mode_sense(sdp
, 0, 0x3F, buffer
, 192, &data
, NULL
);
1336 * First attempt: ask for all pages (0x3F), but only 4 bytes.
1337 * We have to start carefully: some devices hang if we ask
1338 * for more than is available.
1340 res
= sd_do_mode_sense(sdp
, 0, 0x3F, buffer
, 4, &data
, NULL
);
1343 * Second attempt: ask for page 0 When only page 0 is
1344 * implemented, a request for page 3F may return Sense Key
1345 * 5: Illegal Request, Sense Code 24: Invalid field in
1348 if (!scsi_status_is_good(res
))
1349 res
= sd_do_mode_sense(sdp
, 0, 0, buffer
, 4, &data
, NULL
);
1352 * Third attempt: ask 255 bytes, as we did earlier.
1354 if (!scsi_status_is_good(res
))
1355 res
= sd_do_mode_sense(sdp
, 0, 0x3F, buffer
, 255,
1359 if (!scsi_status_is_good(res
)) {
1361 "%s: test WP failed, assume Write Enabled\n", diskname
);
1363 sdkp
->write_prot
= ((data
.device_specific
& 0x80) != 0);
1364 set_disk_ro(sdkp
->disk
, sdkp
->write_prot
);
1365 printk(KERN_NOTICE
"%s: Write Protect is %s\n", diskname
,
1366 sdkp
->write_prot
? "on" : "off");
1367 printk(KERN_DEBUG
"%s: Mode Sense: %02x %02x %02x %02x\n",
1368 diskname
, buffer
[0], buffer
[1], buffer
[2], buffer
[3]);
1373 * sd_read_cache_type - called only from sd_revalidate_disk()
1374 * called with buffer of length 512
1377 sd_read_cache_type(struct scsi_disk
*sdkp
, char *diskname
,
1378 unsigned char *buffer
)
1381 struct scsi_device
*sdp
= sdkp
->device
;
1385 struct scsi_mode_data data
;
1386 struct scsi_sense_hdr sshdr
;
1388 if (sdp
->skip_ms_page_8
)
1391 if (sdp
->type
== TYPE_RBC
) {
1399 /* cautiously ask */
1400 res
= sd_do_mode_sense(sdp
, dbd
, modepage
, buffer
, 4, &data
, &sshdr
);
1402 if (!scsi_status_is_good(res
))
1405 /* that went OK, now ask for the proper length */
1409 * We're only interested in the first three bytes, actually.
1410 * But the data cache page is defined for the first 20.
1417 /* Take headers and block descriptors into account */
1418 len
+= data
.header_length
+ data
.block_descriptor_length
;
1421 res
= sd_do_mode_sense(sdp
, dbd
, modepage
, buffer
, len
, &data
, &sshdr
);
1423 if (scsi_status_is_good(res
)) {
1424 const char *types
[] = {
1425 "write through", "none", "write back",
1426 "write back, no read (daft)"
1429 int offset
= data
.header_length
+ data
.block_descriptor_length
;
1431 if ((buffer
[offset
] & 0x3f) != modepage
) {
1432 printk(KERN_ERR
"%s: got wrong page\n", diskname
);
1436 if (modepage
== 8) {
1437 sdkp
->WCE
= ((buffer
[offset
+ 2] & 0x04) != 0);
1438 sdkp
->RCD
= ((buffer
[offset
+ 2] & 0x01) != 0);
1440 sdkp
->WCE
= ((buffer
[offset
+ 2] & 0x01) == 0);
1444 ct
= sdkp
->RCD
+ 2*sdkp
->WCE
;
1446 printk(KERN_NOTICE
"SCSI device %s: drive cache: %s\n",
1447 diskname
, types
[ct
]);
1453 if (scsi_sense_valid(&sshdr
) &&
1454 sshdr
.sense_key
== ILLEGAL_REQUEST
&&
1455 sshdr
.asc
== 0x24 && sshdr
.ascq
== 0x0)
1456 printk(KERN_NOTICE
"%s: cache data unavailable\n",
1457 diskname
); /* Invalid field in CDB */
1459 printk(KERN_ERR
"%s: asking for cache data failed\n",
1463 printk(KERN_ERR
"%s: assuming drive cache: write through\n",
1470 * sd_revalidate_disk - called the first time a new disk is seen,
1471 * performs disk spin up, read_capacity, etc.
1472 * @disk: struct gendisk we care about
1474 static int sd_revalidate_disk(struct gendisk
*disk
)
1476 struct scsi_disk
*sdkp
= scsi_disk(disk
);
1477 struct scsi_device
*sdp
= sdkp
->device
;
1478 unsigned char *buffer
;
1480 SCSI_LOG_HLQUEUE(3, printk("sd_revalidate_disk: disk=%s\n", disk
->disk_name
));
1483 * If the device is offline, don't try and read capacity or any
1484 * of the other niceties.
1486 if (!scsi_device_online(sdp
))
1489 buffer
= kmalloc(512, GFP_KERNEL
| __GFP_DMA
);
1491 printk(KERN_WARNING
"(sd_revalidate_disk:) Memory allocation "
1496 /* defaults, until the device tells us otherwise */
1497 sdp
->sector_size
= 512;
1499 sdkp
->media_present
= 1;
1500 sdkp
->write_prot
= 0;
1504 sd_spinup_disk(sdkp
, disk
->disk_name
);
1507 * Without media there is no reason to ask; moreover, some devices
1508 * react badly if we do.
1510 if (sdkp
->media_present
) {
1511 sd_read_capacity(sdkp
, disk
->disk_name
, buffer
);
1513 sd_read_write_protect_flag(sdkp
, disk
->disk_name
,
1515 sd_read_cache_type(sdkp
, disk
->disk_name
, buffer
);
1518 set_capacity(disk
, sdkp
->capacity
);
1526 * sd_probe - called during driver initialization and whenever a
1527 * new scsi device is attached to the system. It is called once
1528 * for each scsi device (not just disks) present.
1529 * @dev: pointer to device object
1531 * Returns 0 if successful (or not interested in this scsi device
1532 * (e.g. scanner)); 1 when there is an error.
1534 * Note: this function is invoked from the scsi mid-level.
1535 * This function sets up the mapping between a given
1536 * <host,channel,id,lun> (found in sdp) and new device name
1537 * (e.g. /dev/sda). More precisely it is the block device major
1538 * and minor number that is chosen here.
1540 * Assume sd_attach is not re-entrant (for time being)
1541 * Also think about sd_attach() and sd_remove() running coincidentally.
1543 static int sd_probe(struct device
*dev
)
1545 struct scsi_device
*sdp
= to_scsi_device(dev
);
1546 struct scsi_disk
*sdkp
;
1552 if (sdp
->type
!= TYPE_DISK
&& sdp
->type
!= TYPE_MOD
&& sdp
->type
!= TYPE_RBC
)
1555 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO
, sdp
,
1559 sdkp
= kmalloc(sizeof(*sdkp
), GFP_KERNEL
);
1563 memset (sdkp
, 0, sizeof(*sdkp
));
1564 kref_init(&sdkp
->kref
);
1566 gd
= alloc_disk(16);
1570 if (!idr_pre_get(&sd_index_idr
, GFP_KERNEL
))
1573 spin_lock(&sd_index_lock
);
1574 error
= idr_get_new(&sd_index_idr
, NULL
, &index
);
1575 spin_unlock(&sd_index_lock
);
1577 if (index
>= SD_MAX_DISKS
)
1582 get_device(&sdp
->sdev_gendev
);
1584 sdkp
->driver
= &sd_template
;
1586 sdkp
->index
= index
;
1589 if (!sdp
->timeout
) {
1590 if (sdp
->type
!= TYPE_MOD
)
1591 sdp
->timeout
= SD_TIMEOUT
;
1593 sdp
->timeout
= SD_MOD_TIMEOUT
;
1596 gd
->major
= sd_major((index
& 0xf0) >> 4);
1597 gd
->first_minor
= ((index
& 0xf) << 4) | (index
& 0xfff00);
1599 gd
->fops
= &sd_fops
;
1602 sprintf(gd
->disk_name
, "sd%c", 'a' + index
% 26);
1603 } else if (index
< (26 + 1) * 26) {
1604 sprintf(gd
->disk_name
, "sd%c%c",
1605 'a' + index
/ 26 - 1,'a' + index
% 26);
1607 const unsigned int m1
= (index
/ 26 - 1) / 26 - 1;
1608 const unsigned int m2
= (index
/ 26 - 1) % 26;
1609 const unsigned int m3
= index
% 26;
1610 sprintf(gd
->disk_name
, "sd%c%c%c",
1611 'a' + m1
, 'a' + m2
, 'a' + m3
);
1614 strcpy(gd
->devfs_name
, sdp
->devfs_name
);
1616 gd
->private_data
= &sdkp
->driver
;
1618 sd_revalidate_disk(gd
);
1620 gd
->driverfs_dev
= &sdp
->sdev_gendev
;
1621 gd
->flags
= GENHD_FL_DRIVERFS
;
1623 gd
->flags
|= GENHD_FL_REMOVABLE
;
1624 gd
->queue
= sdkp
->device
->request_queue
;
1626 dev_set_drvdata(dev
, sdkp
);
1629 sdev_printk(KERN_NOTICE
, sdp
, "Attached scsi %sdisk %s\n",
1630 sdp
->removable
? "removable " : "", gd
->disk_name
);
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
);
1661 dev_set_drvdata(dev
, NULL
);
1662 kref_put(&sdkp
->kref
, scsi_disk_release
);
1669 * scsi_disk_release - Called to free the scsi_disk structure
1670 * @kref: pointer to embedded kref
1672 * sd_ref_sem must be held entering this routine. Because it is
1673 * called on last put, you should always use the scsi_disk_get()
1674 * scsi_disk_put() helpers which manipulate the semaphore directly
1675 * and never do a direct kref_put().
1677 static void scsi_disk_release(struct kref
*kref
)
1679 struct scsi_disk
*sdkp
= to_scsi_disk(kref
);
1680 struct gendisk
*disk
= sdkp
->disk
;
1682 spin_lock(&sd_index_lock
);
1683 idr_remove(&sd_index_idr
, sdkp
->index
);
1684 spin_unlock(&sd_index_lock
);
1686 disk
->private_data
= NULL
;
1688 put_device(&sdkp
->device
->sdev_gendev
);
1694 * Send a SYNCHRONIZE CACHE instruction down to the device through
1695 * the normal SCSI command structure. Wait for the command to
1698 static void sd_shutdown(struct device
*dev
)
1700 struct scsi_device
*sdp
= to_scsi_device(dev
);
1701 struct scsi_disk
*sdkp
= scsi_disk_get_from_dev(dev
);
1704 return; /* this can happen */
1707 printk(KERN_NOTICE
"Synchronizing SCSI cache for disk %s: \n",
1708 sdkp
->disk
->disk_name
);
1711 scsi_disk_put(sdkp
);
1715 * init_sd - entry point for this driver (both when built in or when
1718 * Note: this function registers this driver with the scsi mid-level.
1720 static int __init
init_sd(void)
1724 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
1726 for (i
= 0; i
< SD_MAJORS
; i
++)
1727 if (register_blkdev(sd_major(i
), "sd") == 0)
1733 return scsi_register_driver(&sd_template
.gendrv
);
1737 * exit_sd - exit point for this driver (when it is a module).
1739 * Note: this function unregisters this driver from the scsi mid-level.
1741 static void __exit
exit_sd(void)
1745 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
1747 scsi_unregister_driver(&sd_template
.gendrv
);
1748 for (i
= 0; i
< SD_MAJORS
; i
++)
1749 unregister_blkdev(sd_major(i
), "sd");
1752 MODULE_LICENSE("GPL");
1753 MODULE_AUTHOR("Eric Youngdale");
1754 MODULE_DESCRIPTION("SCSI disk (sd) driver");
1756 module_init(init_sd
);
1757 module_exit(exit_sd
);