[MIPS] Use "=R" constraint to avoid compiler errors in cmpxchg().
[linux-2.6/kmemtrace.git] / drivers / scsi / sd.c
blob9d9872347f56a9660c52da7bc76510cd3ec614a1
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 <linux/delay.h>
52 #include <linux/mutex.h>
53 #include <asm/uaccess.h>
55 #include <scsi/scsi.h>
56 #include <scsi/scsi_cmnd.h>
57 #include <scsi/scsi_dbg.h>
58 #include <scsi/scsi_device.h>
59 #include <scsi/scsi_driver.h>
60 #include <scsi/scsi_eh.h>
61 #include <scsi/scsi_host.h>
62 #include <scsi/scsi_ioctl.h>
63 #include <scsi/scsicam.h>
65 #include "scsi_logging.h"
68 * More than enough for everybody ;) The huge number of majors
69 * is a leftover from 16bit dev_t days, we don't really need that
70 * much numberspace.
72 #define SD_MAJORS 16
75 * This is limited by the naming scheme enforced in sd_probe,
76 * add another character to it if you really need more disks.
78 #define SD_MAX_DISKS (((26 * 26) + 26 + 1) * 26)
81 * Time out in seconds for disks and Magneto-opticals (which are slower).
83 #define SD_TIMEOUT (30 * HZ)
84 #define SD_MOD_TIMEOUT (75 * HZ)
87 * Number of allowed retries
89 #define SD_MAX_RETRIES 5
90 #define SD_PASSTHROUGH_RETRIES 1
93 * Size of the initial data buffer for mode and read capacity data
95 #define SD_BUF_SIZE 512
97 static void scsi_disk_release(struct kref *kref);
99 struct scsi_disk {
100 struct scsi_driver *driver; /* always &sd_template */
101 struct scsi_device *device;
102 struct kref kref;
103 struct gendisk *disk;
104 unsigned int openers; /* protected by BKL for now, yuck */
105 sector_t capacity; /* size in 512-byte sectors */
106 u32 index;
107 u8 media_present;
108 u8 write_prot;
109 unsigned WCE : 1; /* state of disk WCE bit */
110 unsigned RCD : 1; /* state of disk RCD bit, unused */
111 unsigned DPOFUA : 1; /* state of disk DPOFUA bit */
114 static DEFINE_IDR(sd_index_idr);
115 static DEFINE_SPINLOCK(sd_index_lock);
117 /* This semaphore is used to mediate the 0->1 reference get in the
118 * face of object destruction (i.e. we can't allow a get on an
119 * object after last put) */
120 static DEFINE_MUTEX(sd_ref_mutex);
122 static int sd_revalidate_disk(struct gendisk *disk);
123 static void sd_rw_intr(struct scsi_cmnd * SCpnt);
125 static int sd_probe(struct device *);
126 static int sd_remove(struct device *);
127 static void sd_shutdown(struct device *dev);
128 static void sd_rescan(struct device *);
129 static int sd_init_command(struct scsi_cmnd *);
130 static int sd_issue_flush(struct device *, sector_t *);
131 static void sd_prepare_flush(request_queue_t *, struct request *);
132 static void sd_read_capacity(struct scsi_disk *sdkp, char *diskname,
133 unsigned char *buffer);
135 static struct scsi_driver sd_template = {
136 .owner = THIS_MODULE,
137 .gendrv = {
138 .name = "sd",
139 .probe = sd_probe,
140 .remove = sd_remove,
141 .shutdown = sd_shutdown,
143 .rescan = sd_rescan,
144 .init_command = sd_init_command,
145 .issue_flush = sd_issue_flush,
149 * Device no to disk mapping:
151 * major disc2 disc p1
152 * |............|.............|....|....| <- dev_t
153 * 31 20 19 8 7 4 3 0
155 * Inside a major, we have 16k disks, however mapped non-
156 * contiguously. The first 16 disks are for major0, the next
157 * ones with major1, ... Disk 256 is for major0 again, disk 272
158 * for major1, ...
159 * As we stay compatible with our numbering scheme, we can reuse
160 * the well-know SCSI majors 8, 65--71, 136--143.
162 static int sd_major(int major_idx)
164 switch (major_idx) {
165 case 0:
166 return SCSI_DISK0_MAJOR;
167 case 1 ... 7:
168 return SCSI_DISK1_MAJOR + major_idx - 1;
169 case 8 ... 15:
170 return SCSI_DISK8_MAJOR + major_idx - 8;
171 default:
172 BUG();
173 return 0; /* shut up gcc */
177 #define to_scsi_disk(obj) container_of(obj,struct scsi_disk,kref)
179 static inline struct scsi_disk *scsi_disk(struct gendisk *disk)
181 return container_of(disk->private_data, struct scsi_disk, driver);
184 static struct scsi_disk *__scsi_disk_get(struct gendisk *disk)
186 struct scsi_disk *sdkp = NULL;
188 if (disk->private_data) {
189 sdkp = scsi_disk(disk);
190 if (scsi_device_get(sdkp->device) == 0)
191 kref_get(&sdkp->kref);
192 else
193 sdkp = NULL;
195 return sdkp;
198 static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
200 struct scsi_disk *sdkp;
202 mutex_lock(&sd_ref_mutex);
203 sdkp = __scsi_disk_get(disk);
204 mutex_unlock(&sd_ref_mutex);
205 return sdkp;
208 static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev)
210 struct scsi_disk *sdkp;
212 mutex_lock(&sd_ref_mutex);
213 sdkp = dev_get_drvdata(dev);
214 if (sdkp)
215 sdkp = __scsi_disk_get(sdkp->disk);
216 mutex_unlock(&sd_ref_mutex);
217 return sdkp;
220 static void scsi_disk_put(struct scsi_disk *sdkp)
222 struct scsi_device *sdev = sdkp->device;
224 mutex_lock(&sd_ref_mutex);
225 kref_put(&sdkp->kref, scsi_disk_release);
226 scsi_device_put(sdev);
227 mutex_unlock(&sd_ref_mutex);
231 * sd_init_command - build a scsi (read or write) command from
232 * information in the request structure.
233 * @SCpnt: pointer to mid-level's per scsi command structure that
234 * contains request and into which the scsi command is written
236 * Returns 1 if successful and 0 if error (or cannot be done now).
238 static int sd_init_command(struct scsi_cmnd * SCpnt)
240 struct scsi_device *sdp = SCpnt->device;
241 struct request *rq = SCpnt->request;
242 struct gendisk *disk = rq->rq_disk;
243 sector_t block = rq->sector;
244 unsigned int this_count = SCpnt->request_bufflen >> 9;
245 unsigned int timeout = sdp->timeout;
247 SCSI_LOG_HLQUEUE(1, printk("sd_init_command: disk=%s, block=%llu, "
248 "count=%d\n", disk->disk_name,
249 (unsigned long long)block, this_count));
251 if (!sdp || !scsi_device_online(sdp) ||
252 block + rq->nr_sectors > get_capacity(disk)) {
253 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
254 rq->nr_sectors));
255 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
256 return 0;
259 if (sdp->changed) {
261 * quietly refuse to do anything to a changed disc until
262 * the changed bit has been reset
264 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
265 return 0;
267 SCSI_LOG_HLQUEUE(2, printk("%s : block=%llu\n",
268 disk->disk_name, (unsigned long long)block));
271 * If we have a 1K hardware sectorsize, prevent access to single
272 * 512 byte sectors. In theory we could handle this - in fact
273 * the scsi cdrom driver must be able to handle this because
274 * we typically use 1K blocksizes, and cdroms typically have
275 * 2K hardware sectorsizes. Of course, things are simpler
276 * with the cdrom, since it is read-only. For performance
277 * reasons, the filesystems should be able to handle this
278 * and not force the scsi disk driver to use bounce buffers
279 * for this.
281 if (sdp->sector_size == 1024) {
282 if ((block & 1) || (rq->nr_sectors & 1)) {
283 printk(KERN_ERR "sd: Bad block number requested");
284 return 0;
285 } else {
286 block = block >> 1;
287 this_count = this_count >> 1;
290 if (sdp->sector_size == 2048) {
291 if ((block & 3) || (rq->nr_sectors & 3)) {
292 printk(KERN_ERR "sd: Bad block number requested");
293 return 0;
294 } else {
295 block = block >> 2;
296 this_count = this_count >> 2;
299 if (sdp->sector_size == 4096) {
300 if ((block & 7) || (rq->nr_sectors & 7)) {
301 printk(KERN_ERR "sd: Bad block number requested");
302 return 0;
303 } else {
304 block = block >> 3;
305 this_count = this_count >> 3;
308 if (rq_data_dir(rq) == WRITE) {
309 if (!sdp->writeable) {
310 return 0;
312 SCpnt->cmnd[0] = WRITE_6;
313 SCpnt->sc_data_direction = DMA_TO_DEVICE;
314 } else if (rq_data_dir(rq) == READ) {
315 SCpnt->cmnd[0] = READ_6;
316 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
317 } else {
318 printk(KERN_ERR "sd: Unknown command %lx\n", rq->flags);
319 /* overkill panic("Unknown sd command %lx\n", rq->flags); */
320 return 0;
323 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
324 disk->disk_name, (rq_data_dir(rq) == WRITE) ?
325 "writing" : "reading", this_count, rq->nr_sectors));
327 SCpnt->cmnd[1] = 0;
329 if (block > 0xffffffff) {
330 SCpnt->cmnd[0] += READ_16 - READ_6;
331 SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
332 SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
333 SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
334 SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
335 SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
336 SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
337 SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
338 SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
339 SCpnt->cmnd[9] = (unsigned char) block & 0xff;
340 SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
341 SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
342 SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
343 SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
344 SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
345 } else if ((this_count > 0xff) || (block > 0x1fffff) ||
346 SCpnt->device->use_10_for_rw) {
347 if (this_count > 0xffff)
348 this_count = 0xffff;
350 SCpnt->cmnd[0] += READ_10 - READ_6;
351 SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
352 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
353 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
354 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
355 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
356 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
357 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
358 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
359 } else {
360 if (unlikely(blk_fua_rq(rq))) {
362 * This happens only if this drive failed
363 * 10byte rw command with ILLEGAL_REQUEST
364 * during operation and thus turned off
365 * use_10_for_rw.
367 printk(KERN_ERR "sd: FUA write on READ/WRITE(6) drive\n");
368 return 0;
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;
387 SCpnt->allowed = SD_MAX_RETRIES;
388 SCpnt->timeout_per_command = timeout;
391 * This is the completion routine we use. This is matched in terms
392 * of capability to this function.
394 SCpnt->done = sd_rw_intr;
397 * This indicates that the command is ready from our end to be
398 * queued.
400 return 1;
404 * sd_open - open a scsi disk device
405 * @inode: only i_rdev member may be used
406 * @filp: only f_mode and f_flags may be used
408 * Returns 0 if successful. Returns a negated errno value in case
409 * of error.
411 * Note: This can be called from a user context (e.g. fsck(1) )
412 * or from within the kernel (e.g. as a result of a mount(1) ).
413 * In the latter case @inode and @filp carry an abridged amount
414 * of information as noted above.
416 static int sd_open(struct inode *inode, struct file *filp)
418 struct gendisk *disk = inode->i_bdev->bd_disk;
419 struct scsi_disk *sdkp;
420 struct scsi_device *sdev;
421 int retval;
423 if (!(sdkp = scsi_disk_get(disk)))
424 return -ENXIO;
427 SCSI_LOG_HLQUEUE(3, printk("sd_open: disk=%s\n", disk->disk_name));
429 sdev = sdkp->device;
432 * If the device is in error recovery, wait until it is done.
433 * If the device is offline, then disallow any access to it.
435 retval = -ENXIO;
436 if (!scsi_block_when_processing_errors(sdev))
437 goto error_out;
439 if (sdev->removable || sdkp->write_prot)
440 check_disk_change(inode->i_bdev);
443 * If the drive is empty, just let the open fail.
445 retval = -ENOMEDIUM;
446 if (sdev->removable && !sdkp->media_present &&
447 !(filp->f_flags & O_NDELAY))
448 goto error_out;
451 * If the device has the write protect tab set, have the open fail
452 * if the user expects to be able to write to the thing.
454 retval = -EROFS;
455 if (sdkp->write_prot && (filp->f_mode & FMODE_WRITE))
456 goto error_out;
459 * It is possible that the disk changing stuff resulted in
460 * the device being taken offline. If this is the case,
461 * report this to the user, and don't pretend that the
462 * open actually succeeded.
464 retval = -ENXIO;
465 if (!scsi_device_online(sdev))
466 goto error_out;
468 if (!sdkp->openers++ && sdev->removable) {
469 if (scsi_block_when_processing_errors(sdev))
470 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
473 return 0;
475 error_out:
476 scsi_disk_put(sdkp);
477 return retval;
481 * sd_release - invoked when the (last) close(2) is called on this
482 * scsi disk.
483 * @inode: only i_rdev member may be used
484 * @filp: only f_mode and f_flags may be used
486 * Returns 0.
488 * Note: may block (uninterruptible) if error recovery is underway
489 * on this disk.
491 static int sd_release(struct inode *inode, struct file *filp)
493 struct gendisk *disk = inode->i_bdev->bd_disk;
494 struct scsi_disk *sdkp = scsi_disk(disk);
495 struct scsi_device *sdev = sdkp->device;
497 SCSI_LOG_HLQUEUE(3, printk("sd_release: disk=%s\n", disk->disk_name));
499 if (!--sdkp->openers && sdev->removable) {
500 if (scsi_block_when_processing_errors(sdev))
501 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
505 * XXX and what if there are packets in flight and this close()
506 * XXX is followed by a "rmmod sd_mod"?
508 scsi_disk_put(sdkp);
509 return 0;
512 static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
514 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
515 struct scsi_device *sdp = sdkp->device;
516 struct Scsi_Host *host = sdp->host;
517 int diskinfo[4];
519 /* default to most commonly used values */
520 diskinfo[0] = 0x40; /* 1 << 6 */
521 diskinfo[1] = 0x20; /* 1 << 5 */
522 diskinfo[2] = sdkp->capacity >> 11;
524 /* override with calculated, extended default, or driver values */
525 if (host->hostt->bios_param)
526 host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo);
527 else
528 scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
530 geo->heads = diskinfo[0];
531 geo->sectors = diskinfo[1];
532 geo->cylinders = diskinfo[2];
533 return 0;
537 * sd_ioctl - process an ioctl
538 * @inode: only i_rdev/i_bdev members may be used
539 * @filp: only f_mode and f_flags may be used
540 * @cmd: ioctl command number
541 * @arg: this is third argument given to ioctl(2) system call.
542 * Often contains a pointer.
544 * Returns 0 if successful (some ioctls return postive numbers on
545 * success as well). Returns a negated errno value in case of error.
547 * Note: most ioctls are forward onto the block subsystem or further
548 * down in the scsi subsytem.
550 static int sd_ioctl(struct inode * inode, struct file * filp,
551 unsigned int cmd, unsigned long arg)
553 struct block_device *bdev = inode->i_bdev;
554 struct gendisk *disk = bdev->bd_disk;
555 struct scsi_device *sdp = scsi_disk(disk)->device;
556 void __user *p = (void __user *)arg;
557 int error;
559 SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
560 disk->disk_name, cmd));
563 * If we are in the middle of error recovery, don't let anyone
564 * else try and use this device. Also, if error recovery fails, it
565 * may try and take the device offline, in which case all further
566 * access to the device is prohibited.
568 error = scsi_nonblockable_ioctl(sdp, cmd, p, filp);
569 if (!scsi_block_when_processing_errors(sdp) || !error)
570 return error;
573 * Send SCSI addressing ioctls directly to mid level, send other
574 * ioctls to block level and then onto mid level if they can't be
575 * resolved.
577 switch (cmd) {
578 case SCSI_IOCTL_GET_IDLUN:
579 case SCSI_IOCTL_GET_BUS_NUMBER:
580 return scsi_ioctl(sdp, cmd, p);
581 default:
582 error = scsi_cmd_ioctl(filp, disk, cmd, p);
583 if (error != -ENOTTY)
584 return error;
586 return scsi_ioctl(sdp, cmd, p);
589 static void set_media_not_present(struct scsi_disk *sdkp)
591 sdkp->media_present = 0;
592 sdkp->capacity = 0;
593 sdkp->device->changed = 1;
597 * sd_media_changed - check if our medium changed
598 * @disk: kernel device descriptor
600 * Returns 0 if not applicable or no change; 1 if change
602 * Note: this function is invoked from the block subsystem.
604 static int sd_media_changed(struct gendisk *disk)
606 struct scsi_disk *sdkp = scsi_disk(disk);
607 struct scsi_device *sdp = sdkp->device;
608 int retval;
610 SCSI_LOG_HLQUEUE(3, printk("sd_media_changed: disk=%s\n",
611 disk->disk_name));
613 if (!sdp->removable)
614 return 0;
617 * If the device is offline, don't send any commands - just pretend as
618 * if the command failed. If the device ever comes back online, we
619 * can deal with it then. It is only because of unrecoverable errors
620 * that we would ever take a device offline in the first place.
622 if (!scsi_device_online(sdp))
623 goto not_present;
626 * Using TEST_UNIT_READY enables differentiation between drive with
627 * no cartridge loaded - NOT READY, drive with changed cartridge -
628 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
630 * Drives that auto spin down. eg iomega jaz 1G, will be started
631 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
632 * sd_revalidate() is called.
634 retval = -ENODEV;
635 if (scsi_block_when_processing_errors(sdp))
636 retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES);
639 * Unable to test, unit probably not ready. This usually
640 * means there is no disc in the drive. Mark as changed,
641 * and we will figure it out later once the drive is
642 * available again.
644 if (retval)
645 goto not_present;
648 * For removable scsi disk we have to recognise the presence
649 * of a disk in the drive. This is kept in the struct scsi_disk
650 * struct and tested at open ! Daniel Roche (dan@lectra.fr)
652 sdkp->media_present = 1;
654 retval = sdp->changed;
655 sdp->changed = 0;
657 return retval;
659 not_present:
660 set_media_not_present(sdkp);
661 return 1;
664 static int sd_sync_cache(struct scsi_device *sdp)
666 int retries, res;
667 struct scsi_sense_hdr sshdr;
669 if (!scsi_device_online(sdp))
670 return -ENODEV;
673 for (retries = 3; retries > 0; --retries) {
674 unsigned char cmd[10] = { 0 };
676 cmd[0] = SYNCHRONIZE_CACHE;
678 * Leave the rest of the command zero to indicate
679 * flush everything.
681 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
682 SD_TIMEOUT, SD_MAX_RETRIES);
683 if (res == 0)
684 break;
687 if (res) { printk(KERN_WARNING "FAILED\n status = %x, message = %02x, "
688 "host = %d, driver = %02x\n ",
689 status_byte(res), msg_byte(res),
690 host_byte(res), driver_byte(res));
691 if (driver_byte(res) & DRIVER_SENSE)
692 scsi_print_sense_hdr("sd", &sshdr);
695 return res;
698 static int sd_issue_flush(struct device *dev, sector_t *error_sector)
700 int ret = 0;
701 struct scsi_device *sdp = to_scsi_device(dev);
702 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
704 if (!sdkp)
705 return -ENODEV;
707 if (sdkp->WCE)
708 ret = sd_sync_cache(sdp);
709 scsi_disk_put(sdkp);
710 return ret;
713 static void sd_prepare_flush(request_queue_t *q, struct request *rq)
715 memset(rq->cmd, 0, sizeof(rq->cmd));
716 rq->flags |= REQ_BLOCK_PC;
717 rq->timeout = SD_TIMEOUT;
718 rq->cmd[0] = SYNCHRONIZE_CACHE;
719 rq->cmd_len = 10;
722 static void sd_rescan(struct device *dev)
724 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
726 if (sdkp) {
727 sd_revalidate_disk(sdkp->disk);
728 scsi_disk_put(sdkp);
733 #ifdef CONFIG_COMPAT
735 * This gets directly called from VFS. When the ioctl
736 * is not recognized we go back to the other translation paths.
738 static long sd_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
740 struct block_device *bdev = file->f_dentry->d_inode->i_bdev;
741 struct gendisk *disk = bdev->bd_disk;
742 struct scsi_device *sdev = scsi_disk(disk)->device;
745 * If we are in the middle of error recovery, don't let anyone
746 * else try and use this device. Also, if error recovery fails, it
747 * may try and take the device offline, in which case all further
748 * access to the device is prohibited.
750 if (!scsi_block_when_processing_errors(sdev))
751 return -ENODEV;
753 if (sdev->host->hostt->compat_ioctl) {
754 int ret;
756 ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
758 return ret;
762 * Let the static ioctl translation table take care of it.
764 return -ENOIOCTLCMD;
766 #endif
768 static struct block_device_operations sd_fops = {
769 .owner = THIS_MODULE,
770 .open = sd_open,
771 .release = sd_release,
772 .ioctl = sd_ioctl,
773 .getgeo = sd_getgeo,
774 #ifdef CONFIG_COMPAT
775 .compat_ioctl = sd_compat_ioctl,
776 #endif
777 .media_changed = sd_media_changed,
778 .revalidate_disk = sd_revalidate_disk,
782 * sd_rw_intr - bottom half handler: called when the lower level
783 * driver has completed (successfully or otherwise) a scsi command.
784 * @SCpnt: mid-level's per command structure.
786 * Note: potentially run from within an ISR. Must not block.
788 static void sd_rw_intr(struct scsi_cmnd * SCpnt)
790 int result = SCpnt->result;
791 int this_count = SCpnt->bufflen;
792 int good_bytes = (result == 0 ? this_count : 0);
793 sector_t block_sectors = 1;
794 u64 first_err_block;
795 sector_t error_sector;
796 struct scsi_sense_hdr sshdr;
797 int sense_valid = 0;
798 int sense_deferred = 0;
799 int info_valid;
801 if (result) {
802 sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
803 if (sense_valid)
804 sense_deferred = scsi_sense_is_deferred(&sshdr);
807 #ifdef CONFIG_SCSI_LOGGING
808 SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: %s: res=0x%x\n",
809 SCpnt->request->rq_disk->disk_name, result));
810 if (sense_valid) {
811 SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: sb[respc,sk,asc,"
812 "ascq]=%x,%x,%x,%x\n", sshdr.response_code,
813 sshdr.sense_key, sshdr.asc, sshdr.ascq));
815 #endif
817 Handle MEDIUM ERRORs that indicate partial success. Since this is a
818 relatively rare error condition, no care is taken to avoid
819 unnecessary additional work such as memcpy's that could be avoided.
821 if (driver_byte(result) != 0 &&
822 sense_valid && !sense_deferred) {
823 switch (sshdr.sense_key) {
824 case MEDIUM_ERROR:
825 if (!blk_fs_request(SCpnt->request))
826 break;
827 info_valid = scsi_get_sense_info_fld(
828 SCpnt->sense_buffer, SCSI_SENSE_BUFFERSIZE,
829 &first_err_block);
831 * May want to warn and skip if following cast results
832 * in actual truncation (if sector_t < 64 bits)
834 error_sector = (sector_t)first_err_block;
835 if (SCpnt->request->bio != NULL)
836 block_sectors = bio_sectors(SCpnt->request->bio);
837 switch (SCpnt->device->sector_size) {
838 case 1024:
839 error_sector <<= 1;
840 if (block_sectors < 2)
841 block_sectors = 2;
842 break;
843 case 2048:
844 error_sector <<= 2;
845 if (block_sectors < 4)
846 block_sectors = 4;
847 break;
848 case 4096:
849 error_sector <<=3;
850 if (block_sectors < 8)
851 block_sectors = 8;
852 break;
853 case 256:
854 error_sector >>= 1;
855 break;
856 default:
857 break;
860 error_sector &= ~(block_sectors - 1);
861 good_bytes = (error_sector - SCpnt->request->sector) << 9;
862 if (good_bytes < 0 || good_bytes >= this_count)
863 good_bytes = 0;
864 break;
866 case RECOVERED_ERROR: /* an error occurred, but it recovered */
867 case NO_SENSE: /* LLDD got sense data */
869 * Inform the user, but make sure that it's not treated
870 * as a hard error.
872 scsi_print_sense("sd", SCpnt);
873 SCpnt->result = 0;
874 memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
875 good_bytes = this_count;
876 break;
878 case ILLEGAL_REQUEST:
879 if (SCpnt->device->use_10_for_rw &&
880 (SCpnt->cmnd[0] == READ_10 ||
881 SCpnt->cmnd[0] == WRITE_10))
882 SCpnt->device->use_10_for_rw = 0;
883 if (SCpnt->device->use_10_for_ms &&
884 (SCpnt->cmnd[0] == MODE_SENSE_10 ||
885 SCpnt->cmnd[0] == MODE_SELECT_10))
886 SCpnt->device->use_10_for_ms = 0;
887 break;
889 default:
890 break;
894 * This calls the generic completion function, now that we know
895 * how many actual sectors finished, and how many sectors we need
896 * to say have failed.
898 scsi_io_completion(SCpnt, good_bytes, block_sectors << 9);
901 static int media_not_present(struct scsi_disk *sdkp,
902 struct scsi_sense_hdr *sshdr)
905 if (!scsi_sense_valid(sshdr))
906 return 0;
907 /* not invoked for commands that could return deferred errors */
908 if (sshdr->sense_key != NOT_READY &&
909 sshdr->sense_key != UNIT_ATTENTION)
910 return 0;
911 if (sshdr->asc != 0x3A) /* medium not present */
912 return 0;
914 set_media_not_present(sdkp);
915 return 1;
919 * spinup disk - called only in sd_revalidate_disk()
921 static void
922 sd_spinup_disk(struct scsi_disk *sdkp, char *diskname)
924 unsigned char cmd[10];
925 unsigned long spintime_expire = 0;
926 int retries, spintime;
927 unsigned int the_result;
928 struct scsi_sense_hdr sshdr;
929 int sense_valid = 0;
931 spintime = 0;
933 /* Spin up drives, as required. Only do this at boot time */
934 /* Spinup needs to be done for module loads too. */
935 do {
936 retries = 0;
938 do {
939 cmd[0] = TEST_UNIT_READY;
940 memset((void *) &cmd[1], 0, 9);
942 the_result = scsi_execute_req(sdkp->device, cmd,
943 DMA_NONE, NULL, 0,
944 &sshdr, SD_TIMEOUT,
945 SD_MAX_RETRIES);
947 if (the_result)
948 sense_valid = scsi_sense_valid(&sshdr);
949 retries++;
950 } while (retries < 3 &&
951 (!scsi_status_is_good(the_result) ||
952 ((driver_byte(the_result) & DRIVER_SENSE) &&
953 sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
956 * If the drive has indicated to us that it doesn't have
957 * any media in it, don't bother with any of the rest of
958 * this crap.
960 if (media_not_present(sdkp, &sshdr))
961 return;
963 if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
964 /* no sense, TUR either succeeded or failed
965 * with a status error */
966 if(!spintime && !scsi_status_is_good(the_result))
967 printk(KERN_NOTICE "%s: Unit Not Ready, "
968 "error = 0x%x\n", diskname, the_result);
969 break;
973 * The device does not want the automatic start to be issued.
975 if (sdkp->device->no_start_on_add) {
976 break;
980 * If manual intervention is required, or this is an
981 * absent USB storage device, a spinup is meaningless.
983 if (sense_valid &&
984 sshdr.sense_key == NOT_READY &&
985 sshdr.asc == 4 && sshdr.ascq == 3) {
986 break; /* manual intervention required */
989 * Issue command to spin up drive when not ready
991 } else if (sense_valid && sshdr.sense_key == NOT_READY) {
992 if (!spintime) {
993 printk(KERN_NOTICE "%s: Spinning up disk...",
994 diskname);
995 cmd[0] = START_STOP;
996 cmd[1] = 1; /* Return immediately */
997 memset((void *) &cmd[2], 0, 8);
998 cmd[4] = 1; /* Start spin cycle */
999 scsi_execute_req(sdkp->device, cmd, DMA_NONE,
1000 NULL, 0, &sshdr,
1001 SD_TIMEOUT, SD_MAX_RETRIES);
1002 spintime_expire = jiffies + 100 * HZ;
1003 spintime = 1;
1005 /* Wait 1 second for next try */
1006 msleep(1000);
1007 printk(".");
1010 * Wait for USB flash devices with slow firmware.
1011 * Yes, this sense key/ASC combination shouldn't
1012 * occur here. It's characteristic of these devices.
1014 } else if (sense_valid &&
1015 sshdr.sense_key == UNIT_ATTENTION &&
1016 sshdr.asc == 0x28) {
1017 if (!spintime) {
1018 spintime_expire = jiffies + 5 * HZ;
1019 spintime = 1;
1021 /* Wait 1 second for next try */
1022 msleep(1000);
1023 } else {
1024 /* we don't understand the sense code, so it's
1025 * probably pointless to loop */
1026 if(!spintime) {
1027 printk(KERN_NOTICE "%s: Unit Not Ready, "
1028 "sense:\n", diskname);
1029 scsi_print_sense_hdr("", &sshdr);
1031 break;
1034 } while (spintime && time_before_eq(jiffies, spintime_expire));
1036 if (spintime) {
1037 if (scsi_status_is_good(the_result))
1038 printk("ready\n");
1039 else
1040 printk("not responding...\n");
1045 * read disk capacity
1047 static void
1048 sd_read_capacity(struct scsi_disk *sdkp, char *diskname,
1049 unsigned char *buffer)
1051 unsigned char cmd[16];
1052 int the_result, retries;
1053 int sector_size = 0;
1054 int longrc = 0;
1055 struct scsi_sense_hdr sshdr;
1056 int sense_valid = 0;
1057 struct scsi_device *sdp = sdkp->device;
1059 repeat:
1060 retries = 3;
1061 do {
1062 if (longrc) {
1063 memset((void *) cmd, 0, 16);
1064 cmd[0] = SERVICE_ACTION_IN;
1065 cmd[1] = SAI_READ_CAPACITY_16;
1066 cmd[13] = 12;
1067 memset((void *) buffer, 0, 12);
1068 } else {
1069 cmd[0] = READ_CAPACITY;
1070 memset((void *) &cmd[1], 0, 9);
1071 memset((void *) buffer, 0, 8);
1074 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
1075 buffer, longrc ? 12 : 8, &sshdr,
1076 SD_TIMEOUT, SD_MAX_RETRIES);
1078 if (media_not_present(sdkp, &sshdr))
1079 return;
1081 if (the_result)
1082 sense_valid = scsi_sense_valid(&sshdr);
1083 retries--;
1085 } while (the_result && retries);
1087 if (the_result && !longrc) {
1088 printk(KERN_NOTICE "%s : READ CAPACITY failed.\n"
1089 "%s : status=%x, message=%02x, host=%d, driver=%02x \n",
1090 diskname, diskname,
1091 status_byte(the_result),
1092 msg_byte(the_result),
1093 host_byte(the_result),
1094 driver_byte(the_result));
1096 if (driver_byte(the_result) & DRIVER_SENSE)
1097 scsi_print_sense_hdr("sd", &sshdr);
1098 else
1099 printk("%s : sense not available. \n", diskname);
1101 /* Set dirty bit for removable devices if not ready -
1102 * sometimes drives will not report this properly. */
1103 if (sdp->removable &&
1104 sense_valid && sshdr.sense_key == NOT_READY)
1105 sdp->changed = 1;
1107 /* Either no media are present but the drive didn't tell us,
1108 or they are present but the read capacity command fails */
1109 /* sdkp->media_present = 0; -- not always correct */
1110 sdkp->capacity = 0x200000; /* 1 GB - random */
1112 return;
1113 } else if (the_result && longrc) {
1114 /* READ CAPACITY(16) has been failed */
1115 printk(KERN_NOTICE "%s : READ CAPACITY(16) failed.\n"
1116 "%s : status=%x, message=%02x, host=%d, driver=%02x \n",
1117 diskname, diskname,
1118 status_byte(the_result),
1119 msg_byte(the_result),
1120 host_byte(the_result),
1121 driver_byte(the_result));
1122 printk(KERN_NOTICE "%s : use 0xffffffff as device size\n",
1123 diskname);
1125 sdkp->capacity = 1 + (sector_t) 0xffffffff;
1126 goto got_data;
1129 if (!longrc) {
1130 sector_size = (buffer[4] << 24) |
1131 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
1132 if (buffer[0] == 0xff && buffer[1] == 0xff &&
1133 buffer[2] == 0xff && buffer[3] == 0xff) {
1134 if(sizeof(sdkp->capacity) > 4) {
1135 printk(KERN_NOTICE "%s : very big device. try to use"
1136 " READ CAPACITY(16).\n", diskname);
1137 longrc = 1;
1138 goto repeat;
1140 printk(KERN_ERR "%s: too big for this kernel. Use a "
1141 "kernel compiled with support for large block "
1142 "devices.\n", diskname);
1143 sdkp->capacity = 0;
1144 goto got_data;
1146 sdkp->capacity = 1 + (((sector_t)buffer[0] << 24) |
1147 (buffer[1] << 16) |
1148 (buffer[2] << 8) |
1149 buffer[3]);
1150 } else {
1151 sdkp->capacity = 1 + (((u64)buffer[0] << 56) |
1152 ((u64)buffer[1] << 48) |
1153 ((u64)buffer[2] << 40) |
1154 ((u64)buffer[3] << 32) |
1155 ((sector_t)buffer[4] << 24) |
1156 ((sector_t)buffer[5] << 16) |
1157 ((sector_t)buffer[6] << 8) |
1158 (sector_t)buffer[7]);
1160 sector_size = (buffer[8] << 24) |
1161 (buffer[9] << 16) | (buffer[10] << 8) | buffer[11];
1164 /* Some devices return the total number of sectors, not the
1165 * highest sector number. Make the necessary adjustment. */
1166 if (sdp->fix_capacity)
1167 --sdkp->capacity;
1169 got_data:
1170 if (sector_size == 0) {
1171 sector_size = 512;
1172 printk(KERN_NOTICE "%s : sector size 0 reported, "
1173 "assuming 512.\n", diskname);
1176 if (sector_size != 512 &&
1177 sector_size != 1024 &&
1178 sector_size != 2048 &&
1179 sector_size != 4096 &&
1180 sector_size != 256) {
1181 printk(KERN_NOTICE "%s : unsupported sector size "
1182 "%d.\n", diskname, sector_size);
1184 * The user might want to re-format the drive with
1185 * a supported sectorsize. Once this happens, it
1186 * would be relatively trivial to set the thing up.
1187 * For this reason, we leave the thing in the table.
1189 sdkp->capacity = 0;
1191 * set a bogus sector size so the normal read/write
1192 * logic in the block layer will eventually refuse any
1193 * request on this device without tripping over power
1194 * of two sector size assumptions
1196 sector_size = 512;
1200 * The msdos fs needs to know the hardware sector size
1201 * So I have created this table. See ll_rw_blk.c
1202 * Jacques Gelinas (Jacques@solucorp.qc.ca)
1204 int hard_sector = sector_size;
1205 sector_t sz = (sdkp->capacity/2) * (hard_sector/256);
1206 request_queue_t *queue = sdp->request_queue;
1207 sector_t mb = sz;
1209 blk_queue_hardsect_size(queue, hard_sector);
1210 /* avoid 64-bit division on 32-bit platforms */
1211 sector_div(sz, 625);
1212 mb -= sz - 974;
1213 sector_div(mb, 1950);
1215 printk(KERN_NOTICE "SCSI device %s: "
1216 "%llu %d-byte hdwr sectors (%llu MB)\n",
1217 diskname, (unsigned long long)sdkp->capacity,
1218 hard_sector, (unsigned long long)mb);
1221 /* Rescale capacity to 512-byte units */
1222 if (sector_size == 4096)
1223 sdkp->capacity <<= 3;
1224 else if (sector_size == 2048)
1225 sdkp->capacity <<= 2;
1226 else if (sector_size == 1024)
1227 sdkp->capacity <<= 1;
1228 else if (sector_size == 256)
1229 sdkp->capacity >>= 1;
1231 sdkp->device->sector_size = sector_size;
1234 /* called with buffer of length 512 */
1235 static inline int
1236 sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
1237 unsigned char *buffer, int len, struct scsi_mode_data *data,
1238 struct scsi_sense_hdr *sshdr)
1240 return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
1241 SD_TIMEOUT, SD_MAX_RETRIES, data,
1242 sshdr);
1246 * read write protect setting, if possible - called only in sd_revalidate_disk()
1247 * called with buffer of length SD_BUF_SIZE
1249 static void
1250 sd_read_write_protect_flag(struct scsi_disk *sdkp, char *diskname,
1251 unsigned char *buffer)
1253 int res;
1254 struct scsi_device *sdp = sdkp->device;
1255 struct scsi_mode_data data;
1257 set_disk_ro(sdkp->disk, 0);
1258 if (sdp->skip_ms_page_3f) {
1259 printk(KERN_NOTICE "%s: assuming Write Enabled\n", diskname);
1260 return;
1263 if (sdp->use_192_bytes_for_3f) {
1264 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
1265 } else {
1267 * First attempt: ask for all pages (0x3F), but only 4 bytes.
1268 * We have to start carefully: some devices hang if we ask
1269 * for more than is available.
1271 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
1274 * Second attempt: ask for page 0 When only page 0 is
1275 * implemented, a request for page 3F may return Sense Key
1276 * 5: Illegal Request, Sense Code 24: Invalid field in
1277 * CDB.
1279 if (!scsi_status_is_good(res))
1280 res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
1283 * Third attempt: ask 255 bytes, as we did earlier.
1285 if (!scsi_status_is_good(res))
1286 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
1287 &data, NULL);
1290 if (!scsi_status_is_good(res)) {
1291 printk(KERN_WARNING
1292 "%s: test WP failed, assume Write Enabled\n", diskname);
1293 } else {
1294 sdkp->write_prot = ((data.device_specific & 0x80) != 0);
1295 set_disk_ro(sdkp->disk, sdkp->write_prot);
1296 printk(KERN_NOTICE "%s: Write Protect is %s\n", diskname,
1297 sdkp->write_prot ? "on" : "off");
1298 printk(KERN_DEBUG "%s: Mode Sense: %02x %02x %02x %02x\n",
1299 diskname, buffer[0], buffer[1], buffer[2], buffer[3]);
1304 * sd_read_cache_type - called only from sd_revalidate_disk()
1305 * called with buffer of length SD_BUF_SIZE
1307 static void
1308 sd_read_cache_type(struct scsi_disk *sdkp, char *diskname,
1309 unsigned char *buffer)
1311 int len = 0, res;
1312 struct scsi_device *sdp = sdkp->device;
1314 int dbd;
1315 int modepage;
1316 struct scsi_mode_data data;
1317 struct scsi_sense_hdr sshdr;
1319 if (sdp->skip_ms_page_8)
1320 goto defaults;
1322 if (sdp->type == TYPE_RBC) {
1323 modepage = 6;
1324 dbd = 8;
1325 } else {
1326 modepage = 8;
1327 dbd = 0;
1330 /* cautiously ask */
1331 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr);
1333 if (!scsi_status_is_good(res))
1334 goto bad_sense;
1336 /* that went OK, now ask for the proper length */
1337 len = data.length;
1340 * We're only interested in the first three bytes, actually.
1341 * But the data cache page is defined for the first 20.
1343 if (len < 3)
1344 goto bad_sense;
1345 if (len > 20)
1346 len = 20;
1348 /* Take headers and block descriptors into account */
1349 len += data.header_length + data.block_descriptor_length;
1350 if (len > SD_BUF_SIZE)
1351 goto bad_sense;
1353 /* Get the data */
1354 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr);
1356 if (scsi_status_is_good(res)) {
1357 const char *types[] = {
1358 "write through", "none", "write back",
1359 "write back, no read (daft)"
1361 int ct = 0;
1362 int offset = data.header_length + data.block_descriptor_length;
1364 if (offset >= SD_BUF_SIZE - 2) {
1365 printk(KERN_ERR "%s: malformed MODE SENSE response",
1366 diskname);
1367 goto defaults;
1370 if ((buffer[offset] & 0x3f) != modepage) {
1371 printk(KERN_ERR "%s: got wrong page\n", diskname);
1372 goto defaults;
1375 if (modepage == 8) {
1376 sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
1377 sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
1378 } else {
1379 sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
1380 sdkp->RCD = 0;
1383 sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
1384 if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
1385 printk(KERN_NOTICE "SCSI device %s: uses "
1386 "READ/WRITE(6), disabling FUA\n", diskname);
1387 sdkp->DPOFUA = 0;
1390 ct = sdkp->RCD + 2*sdkp->WCE;
1392 printk(KERN_NOTICE "SCSI device %s: drive cache: %s%s\n",
1393 diskname, types[ct],
1394 sdkp->DPOFUA ? " w/ FUA" : "");
1396 return;
1399 bad_sense:
1400 if (scsi_sense_valid(&sshdr) &&
1401 sshdr.sense_key == ILLEGAL_REQUEST &&
1402 sshdr.asc == 0x24 && sshdr.ascq == 0x0)
1403 printk(KERN_NOTICE "%s: cache data unavailable\n",
1404 diskname); /* Invalid field in CDB */
1405 else
1406 printk(KERN_ERR "%s: asking for cache data failed\n",
1407 diskname);
1409 defaults:
1410 printk(KERN_ERR "%s: assuming drive cache: write through\n",
1411 diskname);
1412 sdkp->WCE = 0;
1413 sdkp->RCD = 0;
1414 sdkp->DPOFUA = 0;
1418 * sd_revalidate_disk - called the first time a new disk is seen,
1419 * performs disk spin up, read_capacity, etc.
1420 * @disk: struct gendisk we care about
1422 static int sd_revalidate_disk(struct gendisk *disk)
1424 struct scsi_disk *sdkp = scsi_disk(disk);
1425 struct scsi_device *sdp = sdkp->device;
1426 unsigned char *buffer;
1427 unsigned ordered;
1429 SCSI_LOG_HLQUEUE(3, printk("sd_revalidate_disk: disk=%s\n", disk->disk_name));
1432 * If the device is offline, don't try and read capacity or any
1433 * of the other niceties.
1435 if (!scsi_device_online(sdp))
1436 goto out;
1438 buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL | __GFP_DMA);
1439 if (!buffer) {
1440 printk(KERN_WARNING "(sd_revalidate_disk:) Memory allocation "
1441 "failure.\n");
1442 goto out;
1445 /* defaults, until the device tells us otherwise */
1446 sdp->sector_size = 512;
1447 sdkp->capacity = 0;
1448 sdkp->media_present = 1;
1449 sdkp->write_prot = 0;
1450 sdkp->WCE = 0;
1451 sdkp->RCD = 0;
1453 sd_spinup_disk(sdkp, disk->disk_name);
1456 * Without media there is no reason to ask; moreover, some devices
1457 * react badly if we do.
1459 if (sdkp->media_present) {
1460 sd_read_capacity(sdkp, disk->disk_name, buffer);
1461 sd_read_write_protect_flag(sdkp, disk->disk_name, buffer);
1462 sd_read_cache_type(sdkp, disk->disk_name, buffer);
1466 * We now have all cache related info, determine how we deal
1467 * with ordered requests. Note that as the current SCSI
1468 * dispatch function can alter request order, we cannot use
1469 * QUEUE_ORDERED_TAG_* even when ordered tag is supported.
1471 if (sdkp->WCE)
1472 ordered = sdkp->DPOFUA
1473 ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH;
1474 else
1475 ordered = QUEUE_ORDERED_DRAIN;
1477 blk_queue_ordered(sdkp->disk->queue, ordered, sd_prepare_flush);
1479 set_capacity(disk, sdkp->capacity);
1480 kfree(buffer);
1482 out:
1483 return 0;
1487 * sd_probe - called during driver initialization and whenever a
1488 * new scsi device is attached to the system. It is called once
1489 * for each scsi device (not just disks) present.
1490 * @dev: pointer to device object
1492 * Returns 0 if successful (or not interested in this scsi device
1493 * (e.g. scanner)); 1 when there is an error.
1495 * Note: this function is invoked from the scsi mid-level.
1496 * This function sets up the mapping between a given
1497 * <host,channel,id,lun> (found in sdp) and new device name
1498 * (e.g. /dev/sda). More precisely it is the block device major
1499 * and minor number that is chosen here.
1501 * Assume sd_attach is not re-entrant (for time being)
1502 * Also think about sd_attach() and sd_remove() running coincidentally.
1504 static int sd_probe(struct device *dev)
1506 struct scsi_device *sdp = to_scsi_device(dev);
1507 struct scsi_disk *sdkp;
1508 struct gendisk *gd;
1509 u32 index;
1510 int error;
1512 error = -ENODEV;
1513 if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
1514 goto out;
1516 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
1517 "sd_attach\n"));
1519 error = -ENOMEM;
1520 sdkp = kmalloc(sizeof(*sdkp), GFP_KERNEL);
1521 if (!sdkp)
1522 goto out;
1524 memset (sdkp, 0, sizeof(*sdkp));
1525 kref_init(&sdkp->kref);
1527 gd = alloc_disk(16);
1528 if (!gd)
1529 goto out_free;
1531 if (!idr_pre_get(&sd_index_idr, GFP_KERNEL))
1532 goto out_put;
1534 spin_lock(&sd_index_lock);
1535 error = idr_get_new(&sd_index_idr, NULL, &index);
1536 spin_unlock(&sd_index_lock);
1538 if (index >= SD_MAX_DISKS)
1539 error = -EBUSY;
1540 if (error)
1541 goto out_put;
1543 get_device(&sdp->sdev_gendev);
1544 sdkp->device = sdp;
1545 sdkp->driver = &sd_template;
1546 sdkp->disk = gd;
1547 sdkp->index = index;
1548 sdkp->openers = 0;
1550 if (!sdp->timeout) {
1551 if (sdp->type != TYPE_MOD)
1552 sdp->timeout = SD_TIMEOUT;
1553 else
1554 sdp->timeout = SD_MOD_TIMEOUT;
1557 gd->major = sd_major((index & 0xf0) >> 4);
1558 gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
1559 gd->minors = 16;
1560 gd->fops = &sd_fops;
1562 if (index < 26) {
1563 sprintf(gd->disk_name, "sd%c", 'a' + index % 26);
1564 } else if (index < (26 + 1) * 26) {
1565 sprintf(gd->disk_name, "sd%c%c",
1566 'a' + index / 26 - 1,'a' + index % 26);
1567 } else {
1568 const unsigned int m1 = (index / 26 - 1) / 26 - 1;
1569 const unsigned int m2 = (index / 26 - 1) % 26;
1570 const unsigned int m3 = index % 26;
1571 sprintf(gd->disk_name, "sd%c%c%c",
1572 'a' + m1, 'a' + m2, 'a' + m3);
1575 strcpy(gd->devfs_name, sdp->devfs_name);
1577 gd->private_data = &sdkp->driver;
1578 gd->queue = sdkp->device->request_queue;
1580 sd_revalidate_disk(gd);
1582 gd->driverfs_dev = &sdp->sdev_gendev;
1583 gd->flags = GENHD_FL_DRIVERFS;
1584 if (sdp->removable)
1585 gd->flags |= GENHD_FL_REMOVABLE;
1587 dev_set_drvdata(dev, sdkp);
1588 add_disk(gd);
1590 sdev_printk(KERN_NOTICE, sdp, "Attached scsi %sdisk %s\n",
1591 sdp->removable ? "removable " : "", gd->disk_name);
1593 return 0;
1595 out_put:
1596 put_disk(gd);
1597 out_free:
1598 kfree(sdkp);
1599 out:
1600 return error;
1604 * sd_remove - called whenever a scsi disk (previously recognized by
1605 * sd_probe) is detached from the system. It is called (potentially
1606 * multiple times) during sd module unload.
1607 * @sdp: pointer to mid level scsi device object
1609 * Note: this function is invoked from the scsi mid-level.
1610 * This function potentially frees up a device name (e.g. /dev/sdc)
1611 * that could be re-used by a subsequent sd_probe().
1612 * This function is not called when the built-in sd driver is "exit-ed".
1614 static int sd_remove(struct device *dev)
1616 struct scsi_disk *sdkp = dev_get_drvdata(dev);
1618 del_gendisk(sdkp->disk);
1619 sd_shutdown(dev);
1621 mutex_lock(&sd_ref_mutex);
1622 dev_set_drvdata(dev, NULL);
1623 kref_put(&sdkp->kref, scsi_disk_release);
1624 mutex_unlock(&sd_ref_mutex);
1626 return 0;
1630 * scsi_disk_release - Called to free the scsi_disk structure
1631 * @kref: pointer to embedded kref
1633 * sd_ref_mutex must be held entering this routine. Because it is
1634 * called on last put, you should always use the scsi_disk_get()
1635 * scsi_disk_put() helpers which manipulate the semaphore directly
1636 * and never do a direct kref_put().
1638 static void scsi_disk_release(struct kref *kref)
1640 struct scsi_disk *sdkp = to_scsi_disk(kref);
1641 struct gendisk *disk = sdkp->disk;
1643 spin_lock(&sd_index_lock);
1644 idr_remove(&sd_index_idr, sdkp->index);
1645 spin_unlock(&sd_index_lock);
1647 disk->private_data = NULL;
1648 put_disk(disk);
1649 put_device(&sdkp->device->sdev_gendev);
1651 kfree(sdkp);
1655 * Send a SYNCHRONIZE CACHE instruction down to the device through
1656 * the normal SCSI command structure. Wait for the command to
1657 * complete.
1659 static void sd_shutdown(struct device *dev)
1661 struct scsi_device *sdp = to_scsi_device(dev);
1662 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
1664 if (!sdkp)
1665 return; /* this can happen */
1667 if (sdkp->WCE) {
1668 printk(KERN_NOTICE "Synchronizing SCSI cache for disk %s: \n",
1669 sdkp->disk->disk_name);
1670 sd_sync_cache(sdp);
1672 scsi_disk_put(sdkp);
1676 * init_sd - entry point for this driver (both when built in or when
1677 * a module).
1679 * Note: this function registers this driver with the scsi mid-level.
1681 static int __init init_sd(void)
1683 int majors = 0, i;
1685 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
1687 for (i = 0; i < SD_MAJORS; i++)
1688 if (register_blkdev(sd_major(i), "sd") == 0)
1689 majors++;
1691 if (!majors)
1692 return -ENODEV;
1694 return scsi_register_driver(&sd_template.gendrv);
1698 * exit_sd - exit point for this driver (when it is a module).
1700 * Note: this function unregisters this driver from the scsi mid-level.
1702 static void __exit exit_sd(void)
1704 int i;
1706 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
1708 scsi_unregister_driver(&sd_template.gendrv);
1709 for (i = 0; i < SD_MAJORS; i++)
1710 unregister_blkdev(sd_major(i), "sd");
1713 MODULE_LICENSE("GPL");
1714 MODULE_AUTHOR("Eric Youngdale");
1715 MODULE_DESCRIPTION("SCSI disk (sd) driver");
1717 module_init(init_sd);
1718 module_exit(exit_sd);