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 <asm/uaccess.h>
53 #include <scsi/scsi.h>
54 #include <scsi/scsi_cmnd.h>
55 #include <scsi/scsi_dbg.h>
56 #include <scsi/scsi_device.h>
57 #include <scsi/scsi_driver.h>
58 #include <scsi/scsi_eh.h>
59 #include <scsi/scsi_host.h>
60 #include <scsi/scsi_ioctl.h>
61 #include <scsi/scsi_request.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
90 static void scsi_disk_release(struct kref
*kref
);
93 struct scsi_driver
*driver
; /* always &sd_template */
94 struct scsi_device
*device
;
97 unsigned int openers
; /* protected by BKL for now, yuck */
98 sector_t capacity
; /* size in 512-byte sectors */
102 unsigned WCE
: 1; /* state of disk WCE bit */
103 unsigned RCD
: 1; /* state of disk RCD bit, unused */
106 static DEFINE_IDR(sd_index_idr
);
107 static spinlock_t sd_index_lock
= SPIN_LOCK_UNLOCKED
;
109 /* This semaphore is used to mediate the 0->1 reference get in the
110 * face of object destruction (i.e. we can't allow a get on an
111 * object after last put) */
112 static DECLARE_MUTEX(sd_ref_sem
);
114 static int sd_revalidate_disk(struct gendisk
*disk
);
115 static void sd_rw_intr(struct scsi_cmnd
* SCpnt
);
117 static int sd_probe(struct device
*);
118 static int sd_remove(struct device
*);
119 static void sd_shutdown(struct device
*dev
);
120 static void sd_rescan(struct device
*);
121 static int sd_init_command(struct scsi_cmnd
*);
122 static int sd_issue_flush(struct device
*, sector_t
*);
123 static void sd_read_capacity(struct scsi_disk
*sdkp
, char *diskname
,
124 struct scsi_request
*SRpnt
, unsigned char *buffer
);
126 static struct scsi_driver sd_template
= {
127 .owner
= THIS_MODULE
,
132 .shutdown
= sd_shutdown
,
135 .init_command
= sd_init_command
,
136 .issue_flush
= sd_issue_flush
,
140 * Device no to disk mapping:
142 * major disc2 disc p1
143 * |............|.............|....|....| <- dev_t
146 * Inside a major, we have 16k disks, however mapped non-
147 * contiguously. The first 16 disks are for major0, the next
148 * ones with major1, ... Disk 256 is for major0 again, disk 272
150 * As we stay compatible with our numbering scheme, we can reuse
151 * the well-know SCSI majors 8, 65--71, 136--143.
153 static int sd_major(int major_idx
)
157 return SCSI_DISK0_MAJOR
;
159 return SCSI_DISK1_MAJOR
+ major_idx
- 1;
161 return SCSI_DISK8_MAJOR
+ major_idx
- 8;
164 return 0; /* shut up gcc */
168 #define to_scsi_disk(obj) container_of(obj,struct scsi_disk,kref)
170 static inline struct scsi_disk
*scsi_disk(struct gendisk
*disk
)
172 return container_of(disk
->private_data
, struct scsi_disk
, driver
);
175 static struct scsi_disk
*scsi_disk_get(struct gendisk
*disk
)
177 struct scsi_disk
*sdkp
= NULL
;
180 if (disk
->private_data
== NULL
)
182 sdkp
= scsi_disk(disk
);
183 kref_get(&sdkp
->kref
);
184 if (scsi_device_get(sdkp
->device
))
190 kref_put(&sdkp
->kref
, scsi_disk_release
);
197 static void scsi_disk_put(struct scsi_disk
*sdkp
)
200 scsi_device_put(sdkp
->device
);
201 kref_put(&sdkp
->kref
, scsi_disk_release
);
206 * sd_init_command - build a scsi (read or write) command from
207 * information in the request structure.
208 * @SCpnt: pointer to mid-level's per scsi command structure that
209 * contains request and into which the scsi command is written
211 * Returns 1 if successful and 0 if error (or cannot be done now).
213 static int sd_init_command(struct scsi_cmnd
* SCpnt
)
215 unsigned int this_count
, timeout
;
216 struct gendisk
*disk
;
218 struct scsi_device
*sdp
= SCpnt
->device
;
220 timeout
= sdp
->timeout
;
223 * these are already setup, just copy cdb basically
225 if (SCpnt
->request
->flags
& REQ_BLOCK_PC
) {
226 struct request
*rq
= SCpnt
->request
;
228 if (sizeof(rq
->cmd
) > sizeof(SCpnt
->cmnd
))
231 memcpy(SCpnt
->cmnd
, rq
->cmd
, sizeof(SCpnt
->cmnd
));
232 if (rq_data_dir(rq
) == WRITE
)
233 SCpnt
->sc_data_direction
= DMA_TO_DEVICE
;
234 else if (rq
->data_len
)
235 SCpnt
->sc_data_direction
= DMA_FROM_DEVICE
;
237 SCpnt
->sc_data_direction
= DMA_NONE
;
239 this_count
= rq
->data_len
;
241 timeout
= rq
->timeout
;
243 SCpnt
->transfersize
= rq
->data_len
;
248 * we only do REQ_CMD and REQ_BLOCK_PC
250 if (!(SCpnt
->request
->flags
& REQ_CMD
))
253 disk
= SCpnt
->request
->rq_disk
;
254 block
= SCpnt
->request
->sector
;
255 this_count
= SCpnt
->request_bufflen
>> 9;
257 SCSI_LOG_HLQUEUE(1, printk("sd_init_command: disk=%s, block=%llu, "
258 "count=%d\n", disk
->disk_name
, (unsigned long long)block
, this_count
));
260 if (!sdp
|| !scsi_device_online(sdp
) ||
261 block
+ SCpnt
->request
->nr_sectors
> get_capacity(disk
)) {
262 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
263 SCpnt
->request
->nr_sectors
));
264 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt
));
270 * quietly refuse to do anything to a changed disc until
271 * the changed bit has been reset
273 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
276 SCSI_LOG_HLQUEUE(2, printk("%s : block=%llu\n",
277 disk
->disk_name
, (unsigned long long)block
));
280 * If we have a 1K hardware sectorsize, prevent access to single
281 * 512 byte sectors. In theory we could handle this - in fact
282 * the scsi cdrom driver must be able to handle this because
283 * we typically use 1K blocksizes, and cdroms typically have
284 * 2K hardware sectorsizes. Of course, things are simpler
285 * with the cdrom, since it is read-only. For performance
286 * reasons, the filesystems should be able to handle this
287 * and not force the scsi disk driver to use bounce buffers
290 if (sdp
->sector_size
== 1024) {
291 if ((block
& 1) || (SCpnt
->request
->nr_sectors
& 1)) {
292 printk(KERN_ERR
"sd: Bad block number requested");
296 this_count
= this_count
>> 1;
299 if (sdp
->sector_size
== 2048) {
300 if ((block
& 3) || (SCpnt
->request
->nr_sectors
& 3)) {
301 printk(KERN_ERR
"sd: Bad block number requested");
305 this_count
= this_count
>> 2;
308 if (sdp
->sector_size
== 4096) {
309 if ((block
& 7) || (SCpnt
->request
->nr_sectors
& 7)) {
310 printk(KERN_ERR
"sd: Bad block number requested");
314 this_count
= this_count
>> 3;
317 if (rq_data_dir(SCpnt
->request
) == WRITE
) {
318 if (!sdp
->writeable
) {
321 SCpnt
->cmnd
[0] = WRITE_6
;
322 SCpnt
->sc_data_direction
= DMA_TO_DEVICE
;
323 } else if (rq_data_dir(SCpnt
->request
) == READ
) {
324 SCpnt
->cmnd
[0] = READ_6
;
325 SCpnt
->sc_data_direction
= DMA_FROM_DEVICE
;
327 printk(KERN_ERR
"sd: Unknown command %lx\n",
328 SCpnt
->request
->flags
);
329 /* overkill panic("Unknown sd command %lx\n", SCpnt->request->flags); */
333 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
334 disk
->disk_name
, (rq_data_dir(SCpnt
->request
) == WRITE
) ?
335 "writing" : "reading", this_count
, SCpnt
->request
->nr_sectors
));
339 if (block
> 0xffffffff) {
340 SCpnt
->cmnd
[0] += READ_16
- READ_6
;
341 SCpnt
->cmnd
[2] = sizeof(block
) > 4 ? (unsigned char) (block
>> 56) & 0xff : 0;
342 SCpnt
->cmnd
[3] = sizeof(block
) > 4 ? (unsigned char) (block
>> 48) & 0xff : 0;
343 SCpnt
->cmnd
[4] = sizeof(block
) > 4 ? (unsigned char) (block
>> 40) & 0xff : 0;
344 SCpnt
->cmnd
[5] = sizeof(block
) > 4 ? (unsigned char) (block
>> 32) & 0xff : 0;
345 SCpnt
->cmnd
[6] = (unsigned char) (block
>> 24) & 0xff;
346 SCpnt
->cmnd
[7] = (unsigned char) (block
>> 16) & 0xff;
347 SCpnt
->cmnd
[8] = (unsigned char) (block
>> 8) & 0xff;
348 SCpnt
->cmnd
[9] = (unsigned char) block
& 0xff;
349 SCpnt
->cmnd
[10] = (unsigned char) (this_count
>> 24) & 0xff;
350 SCpnt
->cmnd
[11] = (unsigned char) (this_count
>> 16) & 0xff;
351 SCpnt
->cmnd
[12] = (unsigned char) (this_count
>> 8) & 0xff;
352 SCpnt
->cmnd
[13] = (unsigned char) this_count
& 0xff;
353 SCpnt
->cmnd
[14] = SCpnt
->cmnd
[15] = 0;
354 } else if ((this_count
> 0xff) || (block
> 0x1fffff) ||
355 SCpnt
->device
->use_10_for_rw
) {
356 if (this_count
> 0xffff)
359 SCpnt
->cmnd
[0] += READ_10
- READ_6
;
360 SCpnt
->cmnd
[2] = (unsigned char) (block
>> 24) & 0xff;
361 SCpnt
->cmnd
[3] = (unsigned char) (block
>> 16) & 0xff;
362 SCpnt
->cmnd
[4] = (unsigned char) (block
>> 8) & 0xff;
363 SCpnt
->cmnd
[5] = (unsigned char) block
& 0xff;
364 SCpnt
->cmnd
[6] = SCpnt
->cmnd
[9] = 0;
365 SCpnt
->cmnd
[7] = (unsigned char) (this_count
>> 8) & 0xff;
366 SCpnt
->cmnd
[8] = (unsigned char) this_count
& 0xff;
368 if (this_count
> 0xff)
371 SCpnt
->cmnd
[1] |= (unsigned char) ((block
>> 16) & 0x1f);
372 SCpnt
->cmnd
[2] = (unsigned char) ((block
>> 8) & 0xff);
373 SCpnt
->cmnd
[3] = (unsigned char) block
& 0xff;
374 SCpnt
->cmnd
[4] = (unsigned char) this_count
;
377 SCpnt
->request_bufflen
= SCpnt
->bufflen
=
378 this_count
* sdp
->sector_size
;
381 * We shouldn't disconnect in the middle of a sector, so with a dumb
382 * host adapter, it's safe to assume that we can at least transfer
383 * this many bytes between each connect / disconnect.
385 SCpnt
->transfersize
= sdp
->sector_size
;
386 SCpnt
->underflow
= this_count
<< 9;
389 SCpnt
->allowed
= SD_MAX_RETRIES
;
390 SCpnt
->timeout_per_command
= timeout
;
393 * This is the completion routine we use. This is matched in terms
394 * of capability to this function.
396 SCpnt
->done
= sd_rw_intr
;
399 * This indicates that the command is ready from our end to be
406 * sd_open - open a scsi disk device
407 * @inode: only i_rdev member may be used
408 * @filp: only f_mode and f_flags may be used
410 * Returns 0 if successful. Returns a negated errno value in case
413 * Note: This can be called from a user context (e.g. fsck(1) )
414 * or from within the kernel (e.g. as a result of a mount(1) ).
415 * In the latter case @inode and @filp carry an abridged amount
416 * of information as noted above.
418 static int sd_open(struct inode
*inode
, struct file
*filp
)
420 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
421 struct scsi_disk
*sdkp
;
422 struct scsi_device
*sdev
;
425 if (!(sdkp
= scsi_disk_get(disk
)))
429 SCSI_LOG_HLQUEUE(3, printk("sd_open: disk=%s\n", disk
->disk_name
));
434 * If the device is in error recovery, wait until it is done.
435 * If the device is offline, then disallow any access to it.
438 if (!scsi_block_when_processing_errors(sdev
))
441 if (sdev
->removable
|| sdkp
->write_prot
)
442 check_disk_change(inode
->i_bdev
);
445 * If the drive is empty, just let the open fail.
448 if (sdev
->removable
&& !sdkp
->media_present
&&
449 !(filp
->f_flags
& O_NDELAY
))
453 * If the device has the write protect tab set, have the open fail
454 * if the user expects to be able to write to the thing.
457 if (sdkp
->write_prot
&& (filp
->f_mode
& FMODE_WRITE
))
461 * It is possible that the disk changing stuff resulted in
462 * the device being taken offline. If this is the case,
463 * report this to the user, and don't pretend that the
464 * open actually succeeded.
467 if (!scsi_device_online(sdev
))
470 if (!sdkp
->openers
++ && sdev
->removable
) {
471 if (scsi_block_when_processing_errors(sdev
))
472 scsi_set_medium_removal(sdev
, SCSI_REMOVAL_PREVENT
);
483 * sd_release - invoked when the (last) close(2) is called on this
485 * @inode: only i_rdev member may be used
486 * @filp: only f_mode and f_flags may be used
490 * Note: may block (uninterruptible) if error recovery is underway
493 static int sd_release(struct inode
*inode
, struct file
*filp
)
495 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
496 struct scsi_disk
*sdkp
= scsi_disk(disk
);
497 struct scsi_device
*sdev
= sdkp
->device
;
499 SCSI_LOG_HLQUEUE(3, printk("sd_release: disk=%s\n", disk
->disk_name
));
501 if (!--sdkp
->openers
&& sdev
->removable
) {
502 if (scsi_block_when_processing_errors(sdev
))
503 scsi_set_medium_removal(sdev
, SCSI_REMOVAL_ALLOW
);
507 * XXX and what if there are packets in flight and this close()
508 * XXX is followed by a "rmmod sd_mod"?
514 static int sd_hdio_getgeo(struct block_device
*bdev
, struct hd_geometry __user
*loc
)
516 struct scsi_disk
*sdkp
= scsi_disk(bdev
->bd_disk
);
517 struct scsi_device
*sdp
= sdkp
->device
;
518 struct Scsi_Host
*host
= sdp
->host
;
521 /* default to most commonly used values */
522 diskinfo
[0] = 0x40; /* 1 << 6 */
523 diskinfo
[1] = 0x20; /* 1 << 5 */
524 diskinfo
[2] = sdkp
->capacity
>> 11;
526 /* override with calculated, extended default, or driver values */
527 if (host
->hostt
->bios_param
)
528 host
->hostt
->bios_param(sdp
, bdev
, sdkp
->capacity
, diskinfo
);
530 scsicam_bios_param(bdev
, sdkp
->capacity
, diskinfo
);
532 if (put_user(diskinfo
[0], &loc
->heads
))
534 if (put_user(diskinfo
[1], &loc
->sectors
))
536 if (put_user(diskinfo
[2], &loc
->cylinders
))
538 if (put_user((unsigned)get_start_sect(bdev
),
539 (unsigned long __user
*)&loc
->start
))
545 * sd_ioctl - process an ioctl
546 * @inode: only i_rdev/i_bdev members may be used
547 * @filp: only f_mode and f_flags may be used
548 * @cmd: ioctl command number
549 * @arg: this is third argument given to ioctl(2) system call.
550 * Often contains a pointer.
552 * Returns 0 if successful (some ioctls return postive numbers on
553 * success as well). Returns a negated errno value in case of error.
555 * Note: most ioctls are forward onto the block subsystem or further
556 * down in the scsi subsytem.
558 static int sd_ioctl(struct inode
* inode
, struct file
* filp
,
559 unsigned int cmd
, unsigned long arg
)
561 struct block_device
*bdev
= inode
->i_bdev
;
562 struct gendisk
*disk
= bdev
->bd_disk
;
563 struct scsi_device
*sdp
= scsi_disk(disk
)->device
;
564 void __user
*p
= (void __user
*)arg
;
567 SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
568 disk
->disk_name
, cmd
));
571 * If we are in the middle of error recovery, don't let anyone
572 * else try and use this device. Also, if error recovery fails, it
573 * may try and take the device offline, in which case all further
574 * access to the device is prohibited.
576 if (!scsi_block_when_processing_errors(sdp
))
579 if (cmd
== HDIO_GETGEO
) {
582 return sd_hdio_getgeo(bdev
, p
);
586 * Send SCSI addressing ioctls directly to mid level, send other
587 * ioctls to block level and then onto mid level if they can't be
591 case SCSI_IOCTL_GET_IDLUN
:
592 case SCSI_IOCTL_GET_BUS_NUMBER
:
593 return scsi_ioctl(sdp
, cmd
, p
);
595 error
= scsi_cmd_ioctl(filp
, disk
, cmd
, p
);
596 if (error
!= -ENOTTY
)
599 return scsi_ioctl(sdp
, cmd
, p
);
602 static void set_media_not_present(struct scsi_disk
*sdkp
)
604 sdkp
->media_present
= 0;
606 sdkp
->device
->changed
= 1;
610 * sd_media_changed - check if our medium changed
611 * @disk: kernel device descriptor
613 * Returns 0 if not applicable or no change; 1 if change
615 * Note: this function is invoked from the block subsystem.
617 static int sd_media_changed(struct gendisk
*disk
)
619 struct scsi_disk
*sdkp
= scsi_disk(disk
);
620 struct scsi_device
*sdp
= sdkp
->device
;
623 SCSI_LOG_HLQUEUE(3, printk("sd_media_changed: disk=%s\n",
630 * If the device is offline, don't send any commands - just pretend as
631 * if the command failed. If the device ever comes back online, we
632 * can deal with it then. It is only because of unrecoverable errors
633 * that we would ever take a device offline in the first place.
635 if (!scsi_device_online(sdp
))
639 * Using TEST_UNIT_READY enables differentiation between drive with
640 * no cartridge loaded - NOT READY, drive with changed cartridge -
641 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
643 * Drives that auto spin down. eg iomega jaz 1G, will be started
644 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
645 * sd_revalidate() is called.
648 if (scsi_block_when_processing_errors(sdp
))
649 retval
= scsi_test_unit_ready(sdp
, SD_TIMEOUT
, SD_MAX_RETRIES
);
652 * Unable to test, unit probably not ready. This usually
653 * means there is no disc in the drive. Mark as changed,
654 * and we will figure it out later once the drive is
661 * For removable scsi disk we have to recognise the presence
662 * of a disk in the drive. This is kept in the struct scsi_disk
663 * struct and tested at open ! Daniel Roche (dan@lectra.fr)
665 sdkp
->media_present
= 1;
667 retval
= sdp
->changed
;
673 set_media_not_present(sdkp
);
677 static int sd_sync_cache(struct scsi_device
*sdp
)
679 struct scsi_request
*sreq
;
682 if (!scsi_device_online(sdp
))
685 sreq
= scsi_allocate_request(sdp
, GFP_KERNEL
);
687 printk("FAILED\n No memory for request\n");
691 sreq
->sr_data_direction
= DMA_NONE
;
692 for (retries
= 3; retries
> 0; --retries
) {
693 unsigned char cmd
[10] = { 0 };
695 cmd
[0] = SYNCHRONIZE_CACHE
;
697 * Leave the rest of the command zero to indicate
700 scsi_wait_req(sreq
, cmd
, NULL
, 0, SD_TIMEOUT
, SD_MAX_RETRIES
);
701 if (sreq
->sr_result
== 0)
705 res
= sreq
->sr_result
;
707 printk(KERN_WARNING
"FAILED\n status = %x, message = %02x, "
708 "host = %d, driver = %02x\n ",
709 status_byte(res
), msg_byte(res
),
710 host_byte(res
), driver_byte(res
));
711 if (driver_byte(res
) & DRIVER_SENSE
)
712 scsi_print_req_sense("sd", sreq
);
715 scsi_release_request(sreq
);
719 static int sd_issue_flush(struct device
*dev
, sector_t
*error_sector
)
721 struct scsi_device
*sdp
= to_scsi_device(dev
);
722 struct scsi_disk
*sdkp
= dev_get_drvdata(dev
);
730 return sd_sync_cache(sdp
);
733 static void sd_rescan(struct device
*dev
)
735 struct scsi_disk
*sdkp
= dev_get_drvdata(dev
);
736 sd_revalidate_disk(sdkp
->disk
);
739 static struct block_device_operations sd_fops
= {
740 .owner
= THIS_MODULE
,
742 .release
= sd_release
,
744 .media_changed
= sd_media_changed
,
745 .revalidate_disk
= sd_revalidate_disk
,
749 * sd_rw_intr - bottom half handler: called when the lower level
750 * driver has completed (successfully or otherwise) a scsi command.
751 * @SCpnt: mid-level's per command structure.
753 * Note: potentially run from within an ISR. Must not block.
755 static void sd_rw_intr(struct scsi_cmnd
* SCpnt
)
757 int result
= SCpnt
->result
;
758 int this_count
= SCpnt
->bufflen
;
759 int good_bytes
= (result
== 0 ? this_count
: 0);
760 sector_t block_sectors
= 1;
761 sector_t error_sector
;
762 #ifdef CONFIG_SCSI_LOGGING
763 SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: %s: res=0x%x\n",
764 SCpnt
->request
->rq_disk
->disk_name
, result
));
766 SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: sb[0,2,asc,ascq]"
767 "=%x,%x,%x,%x\n", SCpnt
->sense_buffer
[0],
768 SCpnt
->sense_buffer
[2], SCpnt
->sense_buffer
[12],
769 SCpnt
->sense_buffer
[13]));
773 Handle MEDIUM ERRORs that indicate partial success. Since this is a
774 relatively rare error condition, no care is taken to avoid
775 unnecessary additional work such as memcpy's that could be avoided.
778 /* An error occurred */
779 if (driver_byte(result
) != 0 && /* An error occurred */
780 (SCpnt
->sense_buffer
[0] & 0x7f) == 0x70) { /* Sense current */
781 switch (SCpnt
->sense_buffer
[2]) {
783 if (!(SCpnt
->sense_buffer
[0] & 0x80))
785 if (!blk_fs_request(SCpnt
->request
))
787 error_sector
= (SCpnt
->sense_buffer
[3] << 24) |
788 (SCpnt
->sense_buffer
[4] << 16) |
789 (SCpnt
->sense_buffer
[5] << 8) |
790 SCpnt
->sense_buffer
[6];
791 if (SCpnt
->request
->bio
!= NULL
)
792 block_sectors
= bio_sectors(SCpnt
->request
->bio
);
793 switch (SCpnt
->device
->sector_size
) {
796 if (block_sectors
< 2)
801 if (block_sectors
< 4)
806 if (block_sectors
< 8)
816 error_sector
&= ~(block_sectors
- 1);
817 good_bytes
= (error_sector
- SCpnt
->request
->sector
) << 9;
818 if (good_bytes
< 0 || good_bytes
>= this_count
)
822 case RECOVERED_ERROR
: /* an error occurred, but it recovered */
823 case NO_SENSE
: /* LLDD got sense data */
825 * Inform the user, but make sure that it's not treated
828 scsi_print_sense("sd", SCpnt
);
830 SCpnt
->sense_buffer
[0] = 0x0;
831 good_bytes
= this_count
;
834 case ILLEGAL_REQUEST
:
835 if (SCpnt
->device
->use_10_for_rw
&&
836 (SCpnt
->cmnd
[0] == READ_10
||
837 SCpnt
->cmnd
[0] == WRITE_10
))
838 SCpnt
->device
->use_10_for_rw
= 0;
839 if (SCpnt
->device
->use_10_for_ms
&&
840 (SCpnt
->cmnd
[0] == MODE_SENSE_10
||
841 SCpnt
->cmnd
[0] == MODE_SELECT_10
))
842 SCpnt
->device
->use_10_for_ms
= 0;
850 * This calls the generic completion function, now that we know
851 * how many actual sectors finished, and how many sectors we need
852 * to say have failed.
854 scsi_io_completion(SCpnt
, good_bytes
, block_sectors
<< 9);
857 static int media_not_present(struct scsi_disk
*sdkp
, struct scsi_request
*srp
)
861 if (!(driver_byte(srp
->sr_result
) & DRIVER_SENSE
))
863 if (srp
->sr_sense_buffer
[2] != NOT_READY
&&
864 srp
->sr_sense_buffer
[2] != UNIT_ATTENTION
)
866 if (srp
->sr_sense_buffer
[12] != 0x3A) /* medium not present */
869 set_media_not_present(sdkp
);
874 * spinup disk - called only in sd_revalidate_disk()
877 sd_spinup_disk(struct scsi_disk
*sdkp
, char *diskname
,
878 struct scsi_request
*SRpnt
, unsigned char *buffer
) {
879 unsigned char cmd
[10];
880 unsigned long spintime_value
= 0;
881 int retries
, spintime
;
882 unsigned int the_result
;
886 /* Spin up drives, as required. Only do this at boot time */
887 /* Spinup needs to be done for module loads too. */
892 cmd
[0] = TEST_UNIT_READY
;
893 memset((void *) &cmd
[1], 0, 9);
895 SRpnt
->sr_cmd_len
= 0;
896 SRpnt
->sr_sense_buffer
[0] = 0;
897 SRpnt
->sr_sense_buffer
[2] = 0;
898 SRpnt
->sr_data_direction
= DMA_NONE
;
900 scsi_wait_req (SRpnt
, (void *) cmd
, (void *) buffer
,
901 0/*512*/, SD_TIMEOUT
, SD_MAX_RETRIES
);
903 the_result
= SRpnt
->sr_result
;
905 } while (retries
< 3 &&
906 (!scsi_status_is_good(the_result
) ||
907 ((driver_byte(the_result
) & DRIVER_SENSE
) &&
908 SRpnt
->sr_sense_buffer
[2] == UNIT_ATTENTION
)));
911 * If the drive has indicated to us that it doesn't have
912 * any media in it, don't bother with any of the rest of
915 if (media_not_present(sdkp
, SRpnt
))
918 if ((driver_byte(the_result
) & DRIVER_SENSE
) == 0) {
919 /* no sense, TUR either succeeded or failed
920 * with a status error */
921 if(!spintime
&& !scsi_status_is_good(the_result
))
922 printk(KERN_NOTICE
"%s: Unit Not Ready, error = 0x%x\n", diskname
, the_result
);
927 * The device does not want the automatic start to be issued.
929 if (sdkp
->device
->no_start_on_add
) {
934 * If manual intervention is required, or this is an
935 * absent USB storage device, a spinup is meaningless.
937 if (SRpnt
->sr_sense_buffer
[2] == NOT_READY
&&
938 SRpnt
->sr_sense_buffer
[12] == 4 /* not ready */ &&
939 SRpnt
->sr_sense_buffer
[13] == 3) {
940 break; /* manual intervention required */
943 * Issue command to spin up drive when not ready
945 } else if (SRpnt
->sr_sense_buffer
[2] == NOT_READY
) {
948 printk(KERN_NOTICE
"%s: Spinning up disk...",
951 cmd
[1] = 1; /* Return immediately */
952 memset((void *) &cmd
[2], 0, 8);
953 cmd
[4] = 1; /* Start spin cycle */
954 SRpnt
->sr_cmd_len
= 0;
955 SRpnt
->sr_sense_buffer
[0] = 0;
956 SRpnt
->sr_sense_buffer
[2] = 0;
958 SRpnt
->sr_data_direction
= DMA_NONE
;
959 scsi_wait_req(SRpnt
, (void *)cmd
,
960 (void *) buffer
, 0/*512*/,
961 SD_TIMEOUT
, SD_MAX_RETRIES
);
962 spintime_value
= jiffies
;
966 /* Wait 1 second for next try */
968 current
->state
= TASK_UNINTERRUPTIBLE
;
969 time1
= schedule_timeout(time1
);
973 /* we don't understand the sense code, so it's
974 * probably pointless to loop */
976 printk(KERN_NOTICE
"%s: Unit Not Ready, sense:\n", diskname
);
977 scsi_print_req_sense("", SRpnt
);
983 time_after(spintime_value
+ 100 * HZ
, jiffies
));
986 if (scsi_status_is_good(the_result
))
989 printk("not responding...\n");
997 sd_read_capacity(struct scsi_disk
*sdkp
, char *diskname
,
998 struct scsi_request
*SRpnt
, unsigned char *buffer
) {
999 unsigned char cmd
[16];
1000 struct scsi_device
*sdp
= sdkp
->device
;
1001 int the_result
, retries
;
1002 int sector_size
= 0;
1009 memset((void *) cmd
, 0, 16);
1010 cmd
[0] = SERVICE_ACTION_IN
;
1011 cmd
[1] = SAI_READ_CAPACITY_16
;
1013 memset((void *) buffer
, 0, 12);
1015 cmd
[0] = READ_CAPACITY
;
1016 memset((void *) &cmd
[1], 0, 9);
1017 memset((void *) buffer
, 0, 8);
1020 SRpnt
->sr_cmd_len
= 0;
1021 SRpnt
->sr_sense_buffer
[0] = 0;
1022 SRpnt
->sr_sense_buffer
[2] = 0;
1023 SRpnt
->sr_data_direction
= DMA_FROM_DEVICE
;
1025 scsi_wait_req(SRpnt
, (void *) cmd
, (void *) buffer
,
1026 longrc
? 12 : 8, SD_TIMEOUT
, SD_MAX_RETRIES
);
1028 if (media_not_present(sdkp
, SRpnt
))
1031 the_result
= SRpnt
->sr_result
;
1034 } while (the_result
&& retries
);
1036 if (the_result
&& !longrc
) {
1037 printk(KERN_NOTICE
"%s : READ CAPACITY failed.\n"
1038 "%s : status=%x, message=%02x, host=%d, driver=%02x \n",
1040 status_byte(the_result
),
1041 msg_byte(the_result
),
1042 host_byte(the_result
),
1043 driver_byte(the_result
));
1045 if (driver_byte(the_result
) & DRIVER_SENSE
)
1046 scsi_print_req_sense("sd", SRpnt
);
1048 printk("%s : sense not available. \n", diskname
);
1050 /* Set dirty bit for removable devices if not ready -
1051 * sometimes drives will not report this properly. */
1052 if (sdp
->removable
&&
1053 SRpnt
->sr_sense_buffer
[2] == NOT_READY
)
1056 /* Either no media are present but the drive didn't tell us,
1057 or they are present but the read capacity command fails */
1058 /* sdkp->media_present = 0; -- not always correct */
1059 sdkp
->capacity
= 0x200000; /* 1 GB - random */
1062 } else if (the_result
&& longrc
) {
1063 /* READ CAPACITY(16) has been failed */
1064 printk(KERN_NOTICE
"%s : READ CAPACITY(16) failed.\n"
1065 "%s : status=%x, message=%02x, host=%d, driver=%02x \n",
1067 status_byte(the_result
),
1068 msg_byte(the_result
),
1069 host_byte(the_result
),
1070 driver_byte(the_result
));
1071 printk(KERN_NOTICE
"%s : use 0xffffffff as device size\n",
1074 sdkp
->capacity
= 1 + (sector_t
) 0xffffffff;
1079 sector_size
= (buffer
[4] << 24) |
1080 (buffer
[5] << 16) | (buffer
[6] << 8) | buffer
[7];
1081 if (buffer
[0] == 0xff && buffer
[1] == 0xff &&
1082 buffer
[2] == 0xff && buffer
[3] == 0xff) {
1083 if(sizeof(sdkp
->capacity
) > 4) {
1084 printk(KERN_NOTICE
"%s : very big device. try to use"
1085 " READ CAPACITY(16).\n", diskname
);
1089 printk(KERN_ERR
"%s: too big for kernel. Assuming maximum 2Tb\n", diskname
);
1092 sdkp
->capacity
= 1 + (((sector_t
)buffer
[0] << 24) |
1097 sdkp
->capacity
= 1 + (((u64
)buffer
[0] << 56) |
1098 ((u64
)buffer
[1] << 48) |
1099 ((u64
)buffer
[2] << 40) |
1100 ((u64
)buffer
[3] << 32) |
1101 ((sector_t
)buffer
[4] << 24) |
1102 ((sector_t
)buffer
[5] << 16) |
1103 ((sector_t
)buffer
[6] << 8) |
1104 (sector_t
)buffer
[7]);
1106 sector_size
= (buffer
[8] << 24) |
1107 (buffer
[9] << 16) | (buffer
[10] << 8) | buffer
[11];
1111 if (sector_size
== 0) {
1113 printk(KERN_NOTICE
"%s : sector size 0 reported, "
1114 "assuming 512.\n", diskname
);
1117 if (sector_size
!= 512 &&
1118 sector_size
!= 1024 &&
1119 sector_size
!= 2048 &&
1120 sector_size
!= 4096 &&
1121 sector_size
!= 256) {
1122 printk(KERN_NOTICE
"%s : unsupported sector size "
1123 "%d.\n", diskname
, sector_size
);
1125 * The user might want to re-format the drive with
1126 * a supported sectorsize. Once this happens, it
1127 * would be relatively trivial to set the thing up.
1128 * For this reason, we leave the thing in the table.
1134 * The msdos fs needs to know the hardware sector size
1135 * So I have created this table. See ll_rw_blk.c
1136 * Jacques Gelinas (Jacques@solucorp.qc.ca)
1138 int hard_sector
= sector_size
;
1139 sector_t sz
= sdkp
->capacity
* (hard_sector
/256);
1140 request_queue_t
*queue
= sdp
->request_queue
;
1143 blk_queue_hardsect_size(queue
, hard_sector
);
1144 /* avoid 64-bit division on 32-bit platforms */
1146 sector_div(sz
, 1250);
1148 sector_div(mb
, 1950);
1150 printk(KERN_NOTICE
"SCSI device %s: "
1151 "%llu %d-byte hdwr sectors (%llu MB)\n",
1152 diskname
, (unsigned long long)sdkp
->capacity
,
1153 hard_sector
, (unsigned long long)mb
);
1156 /* Rescale capacity to 512-byte units */
1157 if (sector_size
== 4096)
1158 sdkp
->capacity
<<= 3;
1159 else if (sector_size
== 2048)
1160 sdkp
->capacity
<<= 2;
1161 else if (sector_size
== 1024)
1162 sdkp
->capacity
<<= 1;
1163 else if (sector_size
== 256)
1164 sdkp
->capacity
>>= 1;
1166 sdkp
->device
->sector_size
= sector_size
;
1169 /* called with buffer of length 512 */
1171 sd_do_mode_sense(struct scsi_request
*SRpnt
, int dbd
, int modepage
,
1172 unsigned char *buffer
, int len
, struct scsi_mode_data
*data
)
1174 return __scsi_mode_sense(SRpnt
, dbd
, modepage
, buffer
, len
,
1175 SD_TIMEOUT
, SD_MAX_RETRIES
, data
);
1179 * read write protect setting, if possible - called only in sd_revalidate_disk()
1180 * called with buffer of length 512
1183 sd_read_write_protect_flag(struct scsi_disk
*sdkp
, char *diskname
,
1184 struct scsi_request
*SRpnt
, unsigned char *buffer
) {
1186 struct scsi_mode_data data
;
1188 set_disk_ro(sdkp
->disk
, 0);
1189 if (sdkp
->device
->skip_ms_page_3f
) {
1190 printk(KERN_NOTICE
"%s: assuming Write Enabled\n", diskname
);
1194 if (sdkp
->device
->use_192_bytes_for_3f
) {
1195 res
= sd_do_mode_sense(SRpnt
, 0, 0x3F, buffer
, 192, &data
);
1198 * First attempt: ask for all pages (0x3F), but only 4 bytes.
1199 * We have to start carefully: some devices hang if we ask
1200 * for more than is available.
1202 res
= sd_do_mode_sense(SRpnt
, 0, 0x3F, buffer
, 4, &data
);
1205 * Second attempt: ask for page 0 When only page 0 is
1206 * implemented, a request for page 3F may return Sense Key
1207 * 5: Illegal Request, Sense Code 24: Invalid field in
1210 if (!scsi_status_is_good(res
))
1211 res
= sd_do_mode_sense(SRpnt
, 0, 0, buffer
, 4, &data
);
1214 * Third attempt: ask 255 bytes, as we did earlier.
1216 if (!scsi_status_is_good(res
))
1217 res
= sd_do_mode_sense(SRpnt
, 0, 0x3F, buffer
, 255,
1221 if (!scsi_status_is_good(res
)) {
1223 "%s: test WP failed, assume Write Enabled\n", diskname
);
1225 sdkp
->write_prot
= ((data
.device_specific
& 0x80) != 0);
1226 set_disk_ro(sdkp
->disk
, sdkp
->write_prot
);
1227 printk(KERN_NOTICE
"%s: Write Protect is %s\n", diskname
,
1228 sdkp
->write_prot
? "on" : "off");
1229 printk(KERN_DEBUG
"%s: Mode Sense: %02x %02x %02x %02x\n",
1230 diskname
, buffer
[0], buffer
[1], buffer
[2], buffer
[3]);
1235 * sd_read_cache_type - called only from sd_revalidate_disk()
1236 * called with buffer of length 512
1239 sd_read_cache_type(struct scsi_disk
*sdkp
, char *diskname
,
1240 struct scsi_request
*SRpnt
, unsigned char *buffer
) {
1243 const int dbd
= 0; /* DBD */
1244 const int modepage
= 0x08; /* current values, cache page */
1245 struct scsi_mode_data data
;
1247 if (sdkp
->device
->skip_ms_page_8
)
1250 /* cautiously ask */
1251 res
= sd_do_mode_sense(SRpnt
, dbd
, modepage
, buffer
, 4, &data
);
1253 if (!scsi_status_is_good(res
))
1256 /* that went OK, now ask for the proper length */
1260 * We're only interested in the first three bytes, actually.
1261 * But the data cache page is defined for the first 20.
1268 /* Take headers and block descriptors into account */
1269 len
+= data
.header_length
+ data
.block_descriptor_length
;
1272 res
= sd_do_mode_sense(SRpnt
, dbd
, modepage
, buffer
, len
, &data
);
1274 if (scsi_status_is_good(res
)) {
1275 const char *types
[] = {
1276 "write through", "none", "write back",
1277 "write back, no read (daft)"
1280 int offset
= data
.header_length
+
1281 data
.block_descriptor_length
+ 2;
1283 sdkp
->WCE
= ((buffer
[offset
] & 0x04) != 0);
1284 sdkp
->RCD
= ((buffer
[offset
] & 0x01) != 0);
1286 ct
= sdkp
->RCD
+ 2*sdkp
->WCE
;
1288 printk(KERN_NOTICE
"SCSI device %s: drive cache: %s\n",
1289 diskname
, types
[ct
]);
1295 if ((SRpnt
->sr_sense_buffer
[0] & 0x70) == 0x70
1296 && (SRpnt
->sr_sense_buffer
[2] & 0x0f) == ILLEGAL_REQUEST
1297 /* ASC 0x24 ASCQ 0x00: Invalid field in CDB */
1298 && SRpnt
->sr_sense_buffer
[12] == 0x24
1299 && SRpnt
->sr_sense_buffer
[13] == 0x00) {
1300 printk(KERN_NOTICE
"%s: cache data unavailable\n",
1303 printk(KERN_ERR
"%s: asking for cache data failed\n",
1308 printk(KERN_ERR
"%s: assuming drive cache: write through\n",
1315 * sd_revalidate_disk - called the first time a new disk is seen,
1316 * performs disk spin up, read_capacity, etc.
1317 * @disk: struct gendisk we care about
1319 static int sd_revalidate_disk(struct gendisk
*disk
)
1321 struct scsi_disk
*sdkp
= scsi_disk(disk
);
1322 struct scsi_device
*sdp
= sdkp
->device
;
1323 struct scsi_request
*sreq
;
1324 unsigned char *buffer
;
1326 SCSI_LOG_HLQUEUE(3, printk("sd_revalidate_disk: disk=%s\n", disk
->disk_name
));
1329 * If the device is offline, don't try and read capacity or any
1330 * of the other niceties.
1332 if (!scsi_device_online(sdp
))
1335 sreq
= scsi_allocate_request(sdp
, GFP_KERNEL
);
1337 printk(KERN_WARNING
"(sd_revalidate_disk:) Request allocation "
1342 buffer
= kmalloc(512, GFP_KERNEL
| __GFP_DMA
);
1344 printk(KERN_WARNING
"(sd_revalidate_disk:) Memory allocation "
1346 goto out_release_request
;
1349 /* defaults, until the device tells us otherwise */
1350 sdp
->sector_size
= 512;
1352 sdkp
->media_present
= 1;
1353 sdkp
->write_prot
= 0;
1357 sd_spinup_disk(sdkp
, disk
->disk_name
, sreq
, buffer
);
1360 * Without media there is no reason to ask; moreover, some devices
1361 * react badly if we do.
1363 if (sdkp
->media_present
) {
1364 sd_read_capacity(sdkp
, disk
->disk_name
, sreq
, buffer
);
1366 sd_read_write_protect_flag(sdkp
, disk
->disk_name
,
1368 sd_read_cache_type(sdkp
, disk
->disk_name
, sreq
, buffer
);
1371 set_capacity(disk
, sdkp
->capacity
);
1374 out_release_request
:
1375 scsi_release_request(sreq
);
1381 * sd_probe - called during driver initialization and whenever a
1382 * new scsi device is attached to the system. It is called once
1383 * for each scsi device (not just disks) present.
1384 * @dev: pointer to device object
1386 * Returns 0 if successful (or not interested in this scsi device
1387 * (e.g. scanner)); 1 when there is an error.
1389 * Note: this function is invoked from the scsi mid-level.
1390 * This function sets up the mapping between a given
1391 * <host,channel,id,lun> (found in sdp) and new device name
1392 * (e.g. /dev/sda). More precisely it is the block device major
1393 * and minor number that is chosen here.
1395 * Assume sd_attach is not re-entrant (for time being)
1396 * Also think about sd_attach() and sd_remove() running coincidentally.
1398 static int sd_probe(struct device
*dev
)
1400 struct scsi_device
*sdp
= to_scsi_device(dev
);
1401 struct scsi_disk
*sdkp
;
1407 if ((sdp
->type
!= TYPE_DISK
) && (sdp
->type
!= TYPE_MOD
))
1410 SCSI_LOG_HLQUEUE(3, printk("sd_attach: scsi device: <%d,%d,%d,%d>\n",
1411 sdp
->host
->host_no
, sdp
->channel
, sdp
->id
, sdp
->lun
));
1414 sdkp
= kmalloc(sizeof(*sdkp
), GFP_KERNEL
);
1418 memset (sdkp
, 0, sizeof(*sdkp
));
1419 kref_init(&sdkp
->kref
);
1421 gd
= alloc_disk(16);
1425 if (!idr_pre_get(&sd_index_idr
, GFP_KERNEL
))
1428 spin_lock(&sd_index_lock
);
1429 error
= idr_get_new(&sd_index_idr
, NULL
, &index
);
1430 spin_unlock(&sd_index_lock
);
1432 if (index
>= SD_MAX_DISKS
)
1438 sdkp
->driver
= &sd_template
;
1440 sdkp
->index
= index
;
1443 if (!sdp
->timeout
) {
1444 if (sdp
->type
== TYPE_DISK
)
1445 sdp
->timeout
= SD_TIMEOUT
;
1447 sdp
->timeout
= SD_MOD_TIMEOUT
;
1450 gd
->major
= sd_major((index
& 0xf0) >> 4);
1451 gd
->first_minor
= ((index
& 0xf) << 4) | (index
& 0xfff00);
1453 gd
->fops
= &sd_fops
;
1456 sprintf(gd
->disk_name
, "sd%c", 'a' + index
% 26);
1457 } else if (index
< (26 + 1) * 26) {
1458 sprintf(gd
->disk_name
, "sd%c%c",
1459 'a' + index
/ 26 - 1,'a' + index
% 26);
1461 const unsigned int m1
= (index
/ 26 - 1) / 26 - 1;
1462 const unsigned int m2
= (index
/ 26 - 1) % 26;
1463 const unsigned int m3
= index
% 26;
1464 sprintf(gd
->disk_name
, "sd%c%c%c",
1465 'a' + m1
, 'a' + m2
, 'a' + m3
);
1468 strcpy(gd
->devfs_name
, sdp
->devfs_name
);
1470 gd
->private_data
= &sdkp
->driver
;
1472 sd_revalidate_disk(gd
);
1474 gd
->driverfs_dev
= &sdp
->sdev_gendev
;
1475 gd
->flags
= GENHD_FL_DRIVERFS
;
1477 gd
->flags
|= GENHD_FL_REMOVABLE
;
1478 gd
->queue
= sdkp
->device
->request_queue
;
1480 dev_set_drvdata(dev
, sdkp
);
1483 printk(KERN_NOTICE
"Attached scsi %sdisk %s at scsi%d, channel %d, "
1484 "id %d, lun %d\n", sdp
->removable
? "removable " : "",
1485 gd
->disk_name
, sdp
->host
->host_no
, sdp
->channel
,
1488 #if ( defined CONFIG_ARCH_W341 ) || ( defined CONFIG_ARCH_W345 )
1489 set_usbsd_disk(gd
->disk_name
);
1502 * sd_remove - called whenever a scsi disk (previously recognized by
1503 * sd_probe) is detached from the system. It is called (potentially
1504 * multiple times) during sd module unload.
1505 * @sdp: pointer to mid level scsi device object
1507 * Note: this function is invoked from the scsi mid-level.
1508 * This function potentially frees up a device name (e.g. /dev/sdc)
1509 * that could be re-used by a subsequent sd_probe().
1510 * This function is not called when the built-in sd driver is "exit-ed".
1512 static int sd_remove(struct device
*dev
)
1514 struct scsi_disk
*sdkp
= dev_get_drvdata(dev
);
1516 #if ( defined CONFIG_ARCH_W341 ) || ( defined CONFIG_ARCH_W345 )
1517 set_usbsd_disk(sdkp
->disk
->disk_name
);
1519 del_gendisk(sdkp
->disk
);
1522 kref_put(&sdkp
->kref
, scsi_disk_release
);
1529 * scsi_disk_release - Called to free the scsi_disk structure
1530 * @kref: pointer to embedded kref
1532 * sd_ref_sem must be held entering this routine. Because it is
1533 * called on last put, you should always use the scsi_disk_get()
1534 * scsi_disk_put() helpers which manipulate the semaphore directly
1535 * and never do a direct kref_put().
1537 static void scsi_disk_release(struct kref
*kref
)
1539 struct scsi_disk
*sdkp
= to_scsi_disk(kref
);
1540 struct gendisk
*disk
= sdkp
->disk
;
1542 spin_lock(&sd_index_lock
);
1543 idr_remove(&sd_index_idr
, sdkp
->index
);
1544 spin_unlock(&sd_index_lock
);
1546 disk
->private_data
= NULL
;
1554 * Send a SYNCHRONIZE CACHE instruction down to the device through
1555 * the normal SCSI command structure. Wait for the command to
1558 static void sd_shutdown(struct device
*dev
)
1560 struct scsi_device
*sdp
= to_scsi_device(dev
);
1561 struct scsi_disk
*sdkp
= dev_get_drvdata(dev
);
1564 return; /* this can happen */
1569 printk(KERN_NOTICE
"Synchronizing SCSI cache for disk %s: \n",
1570 sdkp
->disk
->disk_name
);
1575 * init_sd - entry point for this driver (both when built in or when
1578 * Note: this function registers this driver with the scsi mid-level.
1580 static int __init
init_sd(void)
1584 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
1586 for (i
= 0; i
< SD_MAJORS
; i
++)
1587 if (register_blkdev(sd_major(i
), "sd") == 0)
1593 return scsi_register_driver(&sd_template
.gendrv
);
1597 * exit_sd - exit point for this driver (when it is a module).
1599 * Note: this function unregisters this driver from the scsi mid-level.
1601 static void __exit
exit_sd(void)
1605 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
1607 scsi_unregister_driver(&sd_template
.gendrv
);
1608 for (i
= 0; i
< SD_MAJORS
; i
++)
1609 unregister_blkdev(sd_major(i
), "sd");
1612 MODULE_LICENSE("GPL");
1613 MODULE_AUTHOR("Eric Youngdale");
1614 MODULE_DESCRIPTION("SCSI disk (sd) driver");
1616 module_init(init_sd
);
1617 module_exit(exit_sd
);