MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / scsi / sd.c
blob204b711680cac0262789ce6247cd3178cb971eec
1 /*
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>
37 #include <linux/fs.h>
38 #include <linux/kernel.h>
39 #include <linux/sched.h>
40 #include <linux/mm.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
69 * much numberspace.
71 #define SD_MAJORS 16
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);
92 struct scsi_disk {
93 struct scsi_driver *driver; /* always &sd_template */
94 struct scsi_device *device;
95 struct kref kref;
96 struct gendisk *disk;
97 unsigned int openers; /* protected by BKL for now, yuck */
98 sector_t capacity; /* size in 512-byte sectors */
99 u32 index;
100 u8 media_present;
101 u8 write_prot;
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,
128 .gendrv = {
129 .name = "sd",
130 .probe = sd_probe,
131 .remove = sd_remove,
132 .shutdown = sd_shutdown,
134 .rescan = sd_rescan,
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
144 * 31 20 19 8 7 4 3 0
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
149 * for major1, ...
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)
155 switch (major_idx) {
156 case 0:
157 return SCSI_DISK0_MAJOR;
158 case 1 ... 7:
159 return SCSI_DISK1_MAJOR + major_idx - 1;
160 case 8 ... 15:
161 return SCSI_DISK8_MAJOR + major_idx - 8;
162 default:
163 BUG();
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;
179 down(&sd_ref_sem);
180 if (disk->private_data == NULL)
181 goto out;
182 sdkp = scsi_disk(disk);
183 kref_get(&sdkp->kref);
184 if (scsi_device_get(sdkp->device))
185 goto out_put;
186 up(&sd_ref_sem);
187 return sdkp;
189 out_put:
190 kref_put(&sdkp->kref, scsi_disk_release);
191 sdkp = NULL;
192 out:
193 up(&sd_ref_sem);
194 return sdkp;
197 static void scsi_disk_put(struct scsi_disk *sdkp)
199 down(&sd_ref_sem);
200 scsi_device_put(sdkp->device);
201 kref_put(&sdkp->kref, scsi_disk_release);
202 up(&sd_ref_sem);
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;
217 sector_t block;
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))
229 return 0;
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;
236 else
237 SCpnt->sc_data_direction = DMA_NONE;
239 this_count = rq->data_len;
240 if (rq->timeout)
241 timeout = rq->timeout;
243 SCpnt->transfersize = rq->data_len;
244 goto queue;
248 * we only do REQ_CMD and REQ_BLOCK_PC
250 if (!(SCpnt->request->flags & REQ_CMD))
251 return 0;
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));
265 return 0;
268 if (sdp->changed) {
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"); */
274 return 0;
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
288 * for this.
290 if (sdp->sector_size == 1024) {
291 if ((block & 1) || (SCpnt->request->nr_sectors & 1)) {
292 printk(KERN_ERR "sd: Bad block number requested");
293 return 0;
294 } else {
295 block = block >> 1;
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");
302 return 0;
303 } else {
304 block = block >> 2;
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");
311 return 0;
312 } else {
313 block = block >> 3;
314 this_count = this_count >> 3;
317 if (rq_data_dir(SCpnt->request) == WRITE) {
318 if (!sdp->writeable) {
319 return 0;
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;
326 } else {
327 printk(KERN_ERR "sd: Unknown command %lx\n",
328 SCpnt->request->flags);
329 /* overkill panic("Unknown sd command %lx\n", SCpnt->request->flags); */
330 return 0;
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));
337 SCpnt->cmnd[1] = 0;
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)
357 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;
367 } else {
368 if (this_count > 0xff)
369 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;
375 SCpnt->cmnd[5] = 0;
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;
388 queue:
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
400 * queued.
402 return 1;
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
411 * of error.
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;
423 int retval;
425 if (!(sdkp = scsi_disk_get(disk)))
426 return -ENXIO;
429 SCSI_LOG_HLQUEUE(3, printk("sd_open: disk=%s\n", disk->disk_name));
431 sdev = sdkp->device;
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.
437 retval = -ENXIO;
438 if (!scsi_block_when_processing_errors(sdev))
439 goto error_out;
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.
447 retval = -ENOMEDIUM;
448 if (sdev->removable && !sdkp->media_present &&
449 !(filp->f_flags & O_NDELAY))
450 goto error_out;
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.
456 retval = -EROFS;
457 if (sdkp->write_prot && (filp->f_mode & FMODE_WRITE))
458 goto error_out;
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.
466 retval = -ENXIO;
467 if (!scsi_device_online(sdev))
468 goto error_out;
470 if (!sdkp->openers++ && sdev->removable) {
471 if (scsi_block_when_processing_errors(sdev))
472 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
475 return 0;
477 error_out:
478 scsi_disk_put(sdkp);
479 return retval;
483 * sd_release - invoked when the (last) close(2) is called on this
484 * scsi disk.
485 * @inode: only i_rdev member may be used
486 * @filp: only f_mode and f_flags may be used
488 * Returns 0.
490 * Note: may block (uninterruptible) if error recovery is underway
491 * on this disk.
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"?
510 scsi_disk_put(sdkp);
511 return 0;
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;
519 int diskinfo[4];
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);
529 else
530 scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
532 if (put_user(diskinfo[0], &loc->heads))
533 return -EFAULT;
534 if (put_user(diskinfo[1], &loc->sectors))
535 return -EFAULT;
536 if (put_user(diskinfo[2], &loc->cylinders))
537 return -EFAULT;
538 if (put_user((unsigned)get_start_sect(bdev),
539 (unsigned long __user *)&loc->start))
540 return -EFAULT;
541 return 0;
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;
565 int error;
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))
577 return -ENODEV;
579 if (cmd == HDIO_GETGEO) {
580 if (!arg)
581 return -EINVAL;
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
588 * resolved.
590 switch (cmd) {
591 case SCSI_IOCTL_GET_IDLUN:
592 case SCSI_IOCTL_GET_BUS_NUMBER:
593 return scsi_ioctl(sdp, cmd, p);
594 default:
595 error = scsi_cmd_ioctl(filp, disk, cmd, p);
596 if (error != -ENOTTY)
597 return error;
599 return scsi_ioctl(sdp, cmd, p);
602 static void set_media_not_present(struct scsi_disk *sdkp)
604 sdkp->media_present = 0;
605 sdkp->capacity = 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;
621 int retval;
623 SCSI_LOG_HLQUEUE(3, printk("sd_media_changed: disk=%s\n",
624 disk->disk_name));
626 if (!sdp->removable)
627 return 0;
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))
636 goto not_present;
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.
647 retval = -ENODEV;
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
655 * available again.
657 if (retval)
658 goto not_present;
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;
668 sdp->changed = 0;
670 return retval;
672 not_present:
673 set_media_not_present(sdkp);
674 return 1;
677 static int sd_sync_cache(struct scsi_device *sdp)
679 struct scsi_request *sreq;
680 int retries, res;
682 if (!scsi_device_online(sdp))
683 return -ENODEV;
685 sreq = scsi_allocate_request(sdp, GFP_KERNEL);
686 if (!sreq) {
687 printk("FAILED\n No memory for request\n");
688 return -ENOMEM;
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
698 * flush everything.
700 scsi_wait_req(sreq, cmd, NULL, 0, SD_TIMEOUT, SD_MAX_RETRIES);
701 if (sreq->sr_result == 0)
702 break;
705 res = sreq->sr_result;
706 if (res) {
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);
716 return res;
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);
724 if (!sdkp)
725 return -ENODEV;
727 if (!sdkp->WCE)
728 return 0;
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,
741 .open = sd_open,
742 .release = sd_release,
743 .ioctl = sd_ioctl,
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));
765 if (0 != 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]));
771 #endif
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]) {
782 case MEDIUM_ERROR:
783 if (!(SCpnt->sense_buffer[0] & 0x80))
784 break;
785 if (!blk_fs_request(SCpnt->request))
786 break;
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) {
794 case 1024:
795 error_sector <<= 1;
796 if (block_sectors < 2)
797 block_sectors = 2;
798 break;
799 case 2048:
800 error_sector <<= 2;
801 if (block_sectors < 4)
802 block_sectors = 4;
803 break;
804 case 4096:
805 error_sector <<=3;
806 if (block_sectors < 8)
807 block_sectors = 8;
808 break;
809 case 256:
810 error_sector >>= 1;
811 break;
812 default:
813 break;
816 error_sector &= ~(block_sectors - 1);
817 good_bytes = (error_sector - SCpnt->request->sector) << 9;
818 if (good_bytes < 0 || good_bytes >= this_count)
819 good_bytes = 0;
820 break;
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
826 * as a hard error.
828 scsi_print_sense("sd", SCpnt);
829 SCpnt->result = 0;
830 SCpnt->sense_buffer[0] = 0x0;
831 good_bytes = this_count;
832 break;
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;
843 break;
845 default:
846 break;
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)
859 if (!srp->sr_result)
860 return 0;
861 if (!(driver_byte(srp->sr_result) & DRIVER_SENSE))
862 return 0;
863 if (srp->sr_sense_buffer[2] != NOT_READY &&
864 srp->sr_sense_buffer[2] != UNIT_ATTENTION)
865 return 0;
866 if (srp->sr_sense_buffer[12] != 0x3A) /* medium not present */
867 return 0;
869 set_media_not_present(sdkp);
870 return 1;
874 * spinup disk - called only in sd_revalidate_disk()
876 static void
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;
884 spintime = 0;
886 /* Spin up drives, as required. Only do this at boot time */
887 /* Spinup needs to be done for module loads too. */
888 do {
889 retries = 0;
891 do {
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;
904 retries++;
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
913 * this crap.
915 if (media_not_present(sdkp, SRpnt))
916 return;
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);
923 break;
927 * The device does not want the automatic start to be issued.
929 if (sdkp->device->no_start_on_add) {
930 break;
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) {
946 unsigned long time1;
947 if (!spintime) {
948 printk(KERN_NOTICE "%s: Spinning up disk...",
949 diskname);
950 cmd[0] = START_STOP;
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;
964 spintime = 1;
965 time1 = HZ;
966 /* Wait 1 second for next try */
967 do {
968 current->state = TASK_UNINTERRUPTIBLE;
969 time1 = schedule_timeout(time1);
970 } while(time1);
971 printk(".");
972 } else {
973 /* we don't understand the sense code, so it's
974 * probably pointless to loop */
975 if(!spintime) {
976 printk(KERN_NOTICE "%s: Unit Not Ready, sense:\n", diskname);
977 scsi_print_req_sense("", SRpnt);
979 break;
982 } while (spintime &&
983 time_after(spintime_value + 100 * HZ, jiffies));
985 if (spintime) {
986 if (scsi_status_is_good(the_result))
987 printk("ready\n");
988 else
989 printk("not responding...\n");
994 * read disk capacity
996 static void
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;
1003 int longrc = 0;
1005 repeat:
1006 retries = 3;
1007 do {
1008 if (longrc) {
1009 memset((void *) cmd, 0, 16);
1010 cmd[0] = SERVICE_ACTION_IN;
1011 cmd[1] = SAI_READ_CAPACITY_16;
1012 cmd[13] = 12;
1013 memset((void *) buffer, 0, 12);
1014 } else {
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))
1029 return;
1031 the_result = SRpnt->sr_result;
1032 retries--;
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",
1039 diskname, diskname,
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);
1047 else
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)
1054 sdp->changed = 1;
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 */
1061 return;
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",
1066 diskname, diskname,
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",
1072 diskname);
1074 sdkp->capacity = 1 + (sector_t) 0xffffffff;
1075 goto got_data;
1078 if (!longrc) {
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);
1086 longrc = 1;
1087 goto repeat;
1088 } else {
1089 printk(KERN_ERR "%s: too big for kernel. Assuming maximum 2Tb\n", diskname);
1092 sdkp->capacity = 1 + (((sector_t)buffer[0] << 24) |
1093 (buffer[1] << 16) |
1094 (buffer[2] << 8) |
1095 buffer[3]);
1096 } else {
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];
1110 got_data:
1111 if (sector_size == 0) {
1112 sector_size = 512;
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.
1130 sdkp->capacity = 0;
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;
1141 sector_t mb;
1143 blk_queue_hardsect_size(queue, hard_sector);
1144 /* avoid 64-bit division on 32-bit platforms */
1145 mb = sz >> 1;
1146 sector_div(sz, 1250);
1147 mb -= sz - 974;
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 */
1170 static inline int
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
1182 static void
1183 sd_read_write_protect_flag(struct scsi_disk *sdkp, char *diskname,
1184 struct scsi_request *SRpnt, unsigned char *buffer) {
1185 int res;
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);
1191 return;
1194 if (sdkp->device->use_192_bytes_for_3f) {
1195 res = sd_do_mode_sense(SRpnt, 0, 0x3F, buffer, 192, &data);
1196 } else {
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
1208 * CDB.
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,
1218 &data);
1221 if (!scsi_status_is_good(res)) {
1222 printk(KERN_WARNING
1223 "%s: test WP failed, assume Write Enabled\n", diskname);
1224 } else {
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
1238 static void
1239 sd_read_cache_type(struct scsi_disk *sdkp, char *diskname,
1240 struct scsi_request *SRpnt, unsigned char *buffer) {
1241 int len = 0, res;
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)
1248 goto defaults;
1250 /* cautiously ask */
1251 res = sd_do_mode_sense(SRpnt, dbd, modepage, buffer, 4, &data);
1253 if (!scsi_status_is_good(res))
1254 goto bad_sense;
1256 /* that went OK, now ask for the proper length */
1257 len = data.length;
1260 * We're only interested in the first three bytes, actually.
1261 * But the data cache page is defined for the first 20.
1263 if (len < 3)
1264 goto bad_sense;
1265 if (len > 20)
1266 len = 20;
1268 /* Take headers and block descriptors into account */
1269 len += data.header_length + data.block_descriptor_length;
1271 /* Get the data */
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)"
1279 int ct = 0;
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]);
1291 return;
1294 bad_sense:
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",
1301 diskname);
1302 } else {
1303 printk(KERN_ERR "%s: asking for cache data failed\n",
1304 diskname);
1307 defaults:
1308 printk(KERN_ERR "%s: assuming drive cache: write through\n",
1309 diskname);
1310 sdkp->WCE = 0;
1311 sdkp->RCD = 0;
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))
1333 goto out;
1335 sreq = scsi_allocate_request(sdp, GFP_KERNEL);
1336 if (!sreq) {
1337 printk(KERN_WARNING "(sd_revalidate_disk:) Request allocation "
1338 "failure.\n");
1339 goto out;
1342 buffer = kmalloc(512, GFP_KERNEL | __GFP_DMA);
1343 if (!buffer) {
1344 printk(KERN_WARNING "(sd_revalidate_disk:) Memory allocation "
1345 "failure.\n");
1346 goto out_release_request;
1349 /* defaults, until the device tells us otherwise */
1350 sdp->sector_size = 512;
1351 sdkp->capacity = 0;
1352 sdkp->media_present = 1;
1353 sdkp->write_prot = 0;
1354 sdkp->WCE = 0;
1355 sdkp->RCD = 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);
1365 if (sdp->removable)
1366 sd_read_write_protect_flag(sdkp, disk->disk_name,
1367 sreq, buffer);
1368 sd_read_cache_type(sdkp, disk->disk_name, sreq, buffer);
1371 set_capacity(disk, sdkp->capacity);
1372 kfree(buffer);
1374 out_release_request:
1375 scsi_release_request(sreq);
1376 out:
1377 return 0;
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;
1402 struct gendisk *gd;
1403 u32 index;
1404 int error;
1406 error = -ENODEV;
1407 if ((sdp->type != TYPE_DISK) && (sdp->type != TYPE_MOD))
1408 goto out;
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));
1413 error = -ENOMEM;
1414 sdkp = kmalloc(sizeof(*sdkp), GFP_KERNEL);
1415 if (!sdkp)
1416 goto out;
1418 memset (sdkp, 0, sizeof(*sdkp));
1419 kref_init(&sdkp->kref);
1421 gd = alloc_disk(16);
1422 if (!gd)
1423 goto out_free;
1425 if (!idr_pre_get(&sd_index_idr, GFP_KERNEL))
1426 goto out_put;
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)
1433 error = -EBUSY;
1434 if (error)
1435 goto out_put;
1437 sdkp->device = sdp;
1438 sdkp->driver = &sd_template;
1439 sdkp->disk = gd;
1440 sdkp->index = index;
1441 sdkp->openers = 0;
1443 if (!sdp->timeout) {
1444 if (sdp->type == TYPE_DISK)
1445 sdp->timeout = SD_TIMEOUT;
1446 else
1447 sdp->timeout = SD_MOD_TIMEOUT;
1450 gd->major = sd_major((index & 0xf0) >> 4);
1451 gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
1452 gd->minors = 16;
1453 gd->fops = &sd_fops;
1455 if (index < 26) {
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);
1460 } else {
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;
1476 if (sdp->removable)
1477 gd->flags |= GENHD_FL_REMOVABLE;
1478 gd->queue = sdkp->device->request_queue;
1480 dev_set_drvdata(dev, sdkp);
1481 add_disk(gd);
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,
1486 sdp->id, sdp->lun);
1488 #if ( defined CONFIG_ARCH_W341 ) || ( defined CONFIG_ARCH_W345 )
1489 set_usbsd_disk(gd->disk_name);
1490 #endif
1491 return 0;
1493 out_put:
1494 put_disk(gd);
1495 out_free:
1496 kfree(sdkp);
1497 out:
1498 return error;
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);
1518 #endif
1519 del_gendisk(sdkp->disk);
1520 sd_shutdown(dev);
1521 down(&sd_ref_sem);
1522 kref_put(&sdkp->kref, scsi_disk_release);
1523 up(&sd_ref_sem);
1525 return 0;
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;
1548 put_disk(disk);
1550 kfree(sdkp);
1554 * Send a SYNCHRONIZE CACHE instruction down to the device through
1555 * the normal SCSI command structure. Wait for the command to
1556 * complete.
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);
1563 if (!sdkp)
1564 return; /* this can happen */
1566 if (!sdkp->WCE)
1567 return;
1569 printk(KERN_NOTICE "Synchronizing SCSI cache for disk %s: \n",
1570 sdkp->disk->disk_name);
1571 sd_sync_cache(sdp);
1575 * init_sd - entry point for this driver (both when built in or when
1576 * a module).
1578 * Note: this function registers this driver with the scsi mid-level.
1580 static int __init init_sd(void)
1582 int majors = 0, i;
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)
1588 majors++;
1590 if (!majors)
1591 return -ENODEV;
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)
1603 int i;
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);