[SCSI] sd: fixes from upstream
[tomato.git] / release / src-rt / linux / linux-2.6 / drivers / scsi / sd.c
blob2e5d9d4374f6a00458c88a84ad99a9dab9addc31
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/module.h>
36 #include <linux/fs.h>
37 #include <linux/kernel.h>
38 #include <linux/mm.h>
39 #include <linux/bio.h>
40 #include <linux/genhd.h>
41 #include <linux/hdreg.h>
42 #include <linux/errno.h>
43 #include <linux/idr.h>
44 #include <linux/interrupt.h>
45 #include <linux/init.h>
46 #include <linux/blkdev.h>
47 #include <linux/blkpg.h>
48 #include <linux/delay.h>
49 #include <linux/mutex.h>
50 #include <asm/uaccess.h>
51 #include <asm/unaligned.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/scsicam.h>
62 #include <scsi/sd.h>
64 #include "scsi_logging.h"
66 MODULE_AUTHOR("Eric Youngdale");
67 MODULE_DESCRIPTION("SCSI disk (sd) driver");
68 MODULE_LICENSE("GPL");
70 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR);
71 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR);
72 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR);
73 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR);
74 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR);
75 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR);
76 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR);
77 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR);
78 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR);
79 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR);
80 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR);
81 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR);
82 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
83 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
84 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
85 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
86 MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK);
87 MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD);
88 MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC);
90 static int sd_revalidate_disk(struct gendisk *disk);
91 static void sd_rw_intr(struct scsi_cmnd * SCpnt);
92 static int sd_probe(struct device *);
93 static int sd_remove(struct device *);
94 static void sd_shutdown(struct device *dev);
95 static int sd_suspend(struct device *dev, pm_message_t state);
96 static int sd_resume(struct device *dev);
97 static void sd_rescan(struct device *);
98 static int sd_init_command(struct scsi_cmnd *);
99 static int sd_issue_flush(struct device *, sector_t *);
100 static void sd_prepare_flush(request_queue_t *, struct request *);
101 static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
102 static void scsi_disk_release(struct class_device *cdev);
103 static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *);
104 static void sd_print_result(struct scsi_disk *, int);
106 static DEFINE_IDA(sd_index_ida);
108 /* This semaphore is used to mediate the 0->1 reference get in the
109 * face of object destruction (i.e. we can't allow a get on an
110 * object after last put) */
111 static DEFINE_MUTEX(sd_ref_mutex);
113 static const char *sd_cache_types[] = {
114 "write through", "none", "write back",
115 "write back, no read (daft)"
118 static ssize_t sd_store_cache_type(struct class_device *cdev, const char *buf,
119 size_t count)
121 int i, ct = -1, rcd, wce, sp;
122 struct scsi_disk *sdkp = to_scsi_disk(cdev);
123 struct scsi_device *sdp = sdkp->device;
124 char buffer[64];
125 char *buffer_data;
126 struct scsi_mode_data data;
127 struct scsi_sense_hdr sshdr;
128 int len;
130 if (sdp->type != TYPE_DISK)
131 /* no cache control on RBC devices; theoretically they
132 * can do it, but there's probably so many exceptions
133 * it's not worth the risk */
134 return -EINVAL;
136 for (i = 0; i < ARRAY_SIZE(sd_cache_types); i++) {
137 const int len = strlen(sd_cache_types[i]);
138 if (strncmp(sd_cache_types[i], buf, len) == 0 &&
139 buf[len] == '\n') {
140 ct = i;
141 break;
144 if (ct < 0)
145 return -EINVAL;
146 rcd = ct & 0x01 ? 1 : 0;
147 wce = ct & 0x02 ? 1 : 0;
148 if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT,
149 SD_MAX_RETRIES, &data, NULL))
150 return -EINVAL;
151 len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
152 data.block_descriptor_length);
153 buffer_data = buffer + data.header_length +
154 data.block_descriptor_length;
155 buffer_data[2] &= ~0x05;
156 buffer_data[2] |= wce << 2 | rcd;
157 sp = buffer_data[0] & 0x80 ? 1 : 0;
159 if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT,
160 SD_MAX_RETRIES, &data, &sshdr)) {
161 if (scsi_sense_valid(&sshdr))
162 sd_print_sense_hdr(sdkp, &sshdr);
163 return -EINVAL;
165 sd_revalidate_disk(sdkp->disk);
166 return count;
169 static ssize_t sd_store_manage_start_stop(struct class_device *cdev,
170 const char *buf, size_t count)
172 struct scsi_disk *sdkp = to_scsi_disk(cdev);
173 struct scsi_device *sdp = sdkp->device;
175 if (!capable(CAP_SYS_ADMIN))
176 return -EACCES;
178 sdp->manage_start_stop = simple_strtoul(buf, NULL, 10);
180 return count;
183 static ssize_t sd_store_allow_restart(struct class_device *cdev, const char *buf,
184 size_t count)
186 struct scsi_disk *sdkp = to_scsi_disk(cdev);
187 struct scsi_device *sdp = sdkp->device;
189 if (!capable(CAP_SYS_ADMIN))
190 return -EACCES;
192 if (sdp->type != TYPE_DISK)
193 return -EINVAL;
195 sdp->allow_restart = simple_strtoul(buf, NULL, 10);
197 return count;
200 static ssize_t sd_show_cache_type(struct class_device *cdev, char *buf)
202 struct scsi_disk *sdkp = to_scsi_disk(cdev);
203 int ct = sdkp->RCD + 2*sdkp->WCE;
205 return snprintf(buf, 40, "%s\n", sd_cache_types[ct]);
208 static ssize_t sd_show_fua(struct class_device *cdev, char *buf)
210 struct scsi_disk *sdkp = to_scsi_disk(cdev);
212 return snprintf(buf, 20, "%u\n", sdkp->DPOFUA);
215 static ssize_t sd_show_manage_start_stop(struct class_device *cdev, char *buf)
217 struct scsi_disk *sdkp = to_scsi_disk(cdev);
218 struct scsi_device *sdp = sdkp->device;
220 return snprintf(buf, 20, "%u\n", sdp->manage_start_stop);
223 static ssize_t sd_show_allow_restart(struct class_device *cdev, char *buf)
225 struct scsi_disk *sdkp = to_scsi_disk(cdev);
227 return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart);
230 static struct class_device_attribute sd_disk_attrs[] = {
231 __ATTR(cache_type, S_IRUGO|S_IWUSR, sd_show_cache_type,
232 sd_store_cache_type),
233 __ATTR(FUA, S_IRUGO, sd_show_fua, NULL),
234 __ATTR(allow_restart, S_IRUGO|S_IWUSR, sd_show_allow_restart,
235 sd_store_allow_restart),
236 __ATTR(manage_start_stop, S_IRUGO|S_IWUSR, sd_show_manage_start_stop,
237 sd_store_manage_start_stop),
238 __ATTR_NULL,
241 static struct class sd_disk_class = {
242 .name = "scsi_disk",
243 .owner = THIS_MODULE,
244 .release = scsi_disk_release,
245 .class_dev_attrs = sd_disk_attrs,
248 static struct scsi_driver sd_template = {
249 .owner = THIS_MODULE,
250 .gendrv = {
251 .name = "sd",
252 .probe = sd_probe,
253 .remove = sd_remove,
254 .suspend = sd_suspend,
255 .resume = sd_resume,
256 .shutdown = sd_shutdown,
258 .rescan = sd_rescan,
259 .init_command = sd_init_command,
260 .issue_flush = sd_issue_flush,
264 * Device no to disk mapping:
266 * major disc2 disc p1
267 * |............|.............|....|....| <- dev_t
268 * 31 20 19 8 7 4 3 0
270 * Inside a major, we have 16k disks, however mapped non-
271 * contiguously. The first 16 disks are for major0, the next
272 * ones with major1, ... Disk 256 is for major0 again, disk 272
273 * for major1, ...
274 * As we stay compatible with our numbering scheme, we can reuse
275 * the well-know SCSI majors 8, 65--71, 136--143.
277 static int sd_major(int major_idx)
279 switch (major_idx) {
280 case 0:
281 return SCSI_DISK0_MAJOR;
282 case 1 ... 7:
283 return SCSI_DISK1_MAJOR + major_idx - 1;
284 case 8 ... 15:
285 return SCSI_DISK8_MAJOR + major_idx - 8;
286 default:
287 BUG();
288 return 0; /* shut up gcc */
292 static struct scsi_disk *__scsi_disk_get(struct gendisk *disk)
294 struct scsi_disk *sdkp = NULL;
296 if (disk->private_data) {
297 sdkp = scsi_disk(disk);
298 if (scsi_device_get(sdkp->device) == 0)
299 class_device_get(&sdkp->cdev);
300 else
301 sdkp = NULL;
303 return sdkp;
306 static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
308 struct scsi_disk *sdkp;
310 mutex_lock(&sd_ref_mutex);
311 sdkp = __scsi_disk_get(disk);
312 mutex_unlock(&sd_ref_mutex);
313 return sdkp;
316 static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev)
318 struct scsi_disk *sdkp;
320 mutex_lock(&sd_ref_mutex);
321 sdkp = dev_get_drvdata(dev);
322 if (sdkp)
323 sdkp = __scsi_disk_get(sdkp->disk);
324 mutex_unlock(&sd_ref_mutex);
325 return sdkp;
328 static void scsi_disk_put(struct scsi_disk *sdkp)
330 struct scsi_device *sdev = sdkp->device;
332 mutex_lock(&sd_ref_mutex);
333 class_device_put(&sdkp->cdev);
334 scsi_device_put(sdev);
335 mutex_unlock(&sd_ref_mutex);
339 * sd_init_command - build a scsi (read or write) command from
340 * information in the request structure.
341 * @SCpnt: pointer to mid-level's per scsi command structure that
342 * contains request and into which the scsi command is written
344 * Returns 1 if successful and 0 if error (or cannot be done now).
346 static int sd_init_command(struct scsi_cmnd * SCpnt)
348 struct scsi_device *sdp = SCpnt->device;
349 struct request *rq = SCpnt->request;
350 struct gendisk *disk = rq->rq_disk;
351 sector_t block = rq->sector;
352 sector_t threshold;
353 unsigned int this_count = SCpnt->request_bufflen >> 9;
354 unsigned int timeout = sdp->timeout;
356 SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
357 "sd_init_command: block=%llu, "
358 "count=%d\n",
359 (unsigned long long)block,
360 this_count));
362 if (!sdp || !scsi_device_online(sdp) ||
363 block + rq->nr_sectors > get_capacity(disk)) {
364 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
365 "Finishing %ld sectors\n",
366 rq->nr_sectors));
367 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
368 "Retry with 0x%p\n", SCpnt));
369 return 0;
372 if (sdp->changed) {
374 * quietly refuse to do anything to a changed disc until
375 * the changed bit has been reset
377 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
378 return 0;
382 * Some SD card readers can't handle multi-sector accesses which touch
383 * the last one or two hardware sectors. Split accesses as needed.
385 threshold = get_capacity(disk) - SD_LAST_BUGGY_SECTORS *
386 (sdp->sector_size / 512);
388 if (unlikely(sdp->last_sector_bug && block + this_count > threshold)) {
389 if (block < threshold) {
390 /* Access up to the threshold but not beyond */
391 this_count = threshold - block;
392 } else {
393 /* Access only a single hardware sector */
394 this_count = sdp->sector_size / 512;
398 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n",
399 (unsigned long long)block));
402 * If we have a 1K hardware sectorsize, prevent access to single
403 * 512 byte sectors. In theory we could handle this - in fact
404 * the scsi cdrom driver must be able to handle this because
405 * we typically use 1K blocksizes, and cdroms typically have
406 * 2K hardware sectorsizes. Of course, things are simpler
407 * with the cdrom, since it is read-only. For performance
408 * reasons, the filesystems should be able to handle this
409 * and not force the scsi disk driver to use bounce buffers
410 * for this.
412 if (sdp->sector_size == 1024) {
413 if ((block & 1) || (rq->nr_sectors & 1)) {
414 scmd_printk(KERN_ERR, SCpnt,
415 "Bad block number requested\n");
416 return 0;
417 } else {
418 block = block >> 1;
419 this_count = this_count >> 1;
422 if (sdp->sector_size == 2048) {
423 if ((block & 3) || (rq->nr_sectors & 3)) {
424 scmd_printk(KERN_ERR, SCpnt,
425 "Bad block number requested\n");
426 return 0;
427 } else {
428 block = block >> 2;
429 this_count = this_count >> 2;
432 if (sdp->sector_size == 4096) {
433 if ((block & 7) || (rq->nr_sectors & 7)) {
434 scmd_printk(KERN_ERR, SCpnt,
435 "Bad block number requested\n");
436 return 0;
437 } else {
438 block = block >> 3;
439 this_count = this_count >> 3;
442 if (rq_data_dir(rq) == WRITE) {
443 if (!sdp->writeable) {
444 return 0;
446 SCpnt->cmnd[0] = WRITE_6;
447 SCpnt->sc_data_direction = DMA_TO_DEVICE;
448 } else if (rq_data_dir(rq) == READ) {
449 SCpnt->cmnd[0] = READ_6;
450 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
451 } else {
452 scmd_printk(KERN_ERR, SCpnt, "Unknown command %x\n", rq->cmd_flags);
453 return 0;
456 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
457 "%s %d/%ld 512 byte blocks.\n",
458 (rq_data_dir(rq) == WRITE) ?
459 "writing" : "reading", this_count,
460 rq->nr_sectors));
462 SCpnt->cmnd[1] = 0;
464 if (block > 0xffffffff) {
465 SCpnt->cmnd[0] += READ_16 - READ_6;
466 SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
467 SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
468 SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
469 SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
470 SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
471 SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
472 SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
473 SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
474 SCpnt->cmnd[9] = (unsigned char) block & 0xff;
475 SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
476 SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
477 SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
478 SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
479 SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
480 } else if ((this_count > 0xff) || (block > 0x1fffff) ||
481 SCpnt->device->use_10_for_rw) {
482 if (this_count > 0xffff)
483 this_count = 0xffff;
485 SCpnt->cmnd[0] += READ_10 - READ_6;
486 SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
487 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
488 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
489 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
490 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
491 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
492 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
493 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
494 } else {
495 if (unlikely(blk_fua_rq(rq))) {
497 * This happens only if this drive failed
498 * 10byte rw command with ILLEGAL_REQUEST
499 * during operation and thus turned off
500 * use_10_for_rw.
502 scmd_printk(KERN_ERR, SCpnt,
503 "FUA write on READ/WRITE(6) drive\n");
504 return 0;
507 SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
508 SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
509 SCpnt->cmnd[3] = (unsigned char) block & 0xff;
510 SCpnt->cmnd[4] = (unsigned char) this_count;
511 SCpnt->cmnd[5] = 0;
513 SCpnt->request_bufflen = this_count * sdp->sector_size;
516 * We shouldn't disconnect in the middle of a sector, so with a dumb
517 * host adapter, it's safe to assume that we can at least transfer
518 * this many bytes between each connect / disconnect.
520 SCpnt->transfersize = sdp->sector_size;
521 SCpnt->underflow = this_count << 9;
522 SCpnt->allowed = SD_MAX_RETRIES;
523 SCpnt->timeout_per_command = timeout;
526 * This is the completion routine we use. This is matched in terms
527 * of capability to this function.
529 SCpnt->done = sd_rw_intr;
532 * This indicates that the command is ready from our end to be
533 * queued.
535 return 1;
539 * sd_open - open a scsi disk device
540 * @inode: only i_rdev member may be used
541 * @filp: only f_mode and f_flags may be used
543 * Returns 0 if successful. Returns a negated errno value in case
544 * of error.
546 * Note: This can be called from a user context (e.g. fsck(1) )
547 * or from within the kernel (e.g. as a result of a mount(1) ).
548 * In the latter case @inode and @filp carry an abridged amount
549 * of information as noted above.
551 static int sd_open(struct inode *inode, struct file *filp)
553 struct gendisk *disk = inode->i_bdev->bd_disk;
554 struct scsi_disk *sdkp;
555 struct scsi_device *sdev;
556 int retval;
558 if (!(sdkp = scsi_disk_get(disk)))
559 return -ENXIO;
562 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
564 sdev = sdkp->device;
567 * If the device is in error recovery, wait until it is done.
568 * If the device is offline, then disallow any access to it.
570 retval = -ENXIO;
571 if (!scsi_block_when_processing_errors(sdev))
572 goto error_out;
574 if (sdev->removable || sdkp->write_prot)
575 check_disk_change(inode->i_bdev);
578 * If the drive is empty, just let the open fail.
580 retval = -ENOMEDIUM;
581 if (sdev->removable && !sdkp->media_present &&
582 !(filp->f_flags & O_NDELAY))
583 goto error_out;
586 * If the device has the write protect tab set, have the open fail
587 * if the user expects to be able to write to the thing.
589 retval = -EROFS;
590 if (sdkp->write_prot && (filp->f_mode & FMODE_WRITE))
591 goto error_out;
594 * It is possible that the disk changing stuff resulted in
595 * the device being taken offline. If this is the case,
596 * report this to the user, and don't pretend that the
597 * open actually succeeded.
599 retval = -ENXIO;
600 if (!scsi_device_online(sdev))
601 goto error_out;
603 if (!sdkp->openers++ && sdev->removable) {
604 if (scsi_block_when_processing_errors(sdev))
605 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
608 return 0;
610 error_out:
611 scsi_disk_put(sdkp);
612 return retval;
616 * sd_release - invoked when the (last) close(2) is called on this
617 * scsi disk.
618 * @inode: only i_rdev member may be used
619 * @filp: only f_mode and f_flags may be used
621 * Returns 0.
623 * Note: may block (uninterruptible) if error recovery is underway
624 * on this disk.
626 static int sd_release(struct inode *inode, struct file *filp)
628 struct gendisk *disk = inode->i_bdev->bd_disk;
629 struct scsi_disk *sdkp = scsi_disk(disk);
630 struct scsi_device *sdev = sdkp->device;
632 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
634 if (!--sdkp->openers && sdev->removable) {
635 if (scsi_block_when_processing_errors(sdev))
636 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
640 * XXX and what if there are packets in flight and this close()
641 * XXX is followed by a "rmmod sd_mod"?
643 scsi_disk_put(sdkp);
644 return 0;
647 static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
649 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
650 struct scsi_device *sdp = sdkp->device;
651 struct Scsi_Host *host = sdp->host;
652 int diskinfo[4];
654 /* default to most commonly used values */
655 diskinfo[0] = 0x40; /* 1 << 6 */
656 diskinfo[1] = 0x20; /* 1 << 5 */
657 diskinfo[2] = sdkp->capacity >> 11;
659 /* override with calculated, extended default, or driver values */
660 if (host->hostt->bios_param)
661 host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo);
662 else
663 scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
665 geo->heads = diskinfo[0];
666 geo->sectors = diskinfo[1];
667 geo->cylinders = diskinfo[2];
668 return 0;
672 * sd_ioctl - process an ioctl
673 * @inode: only i_rdev/i_bdev members may be used
674 * @filp: only f_mode and f_flags may be used
675 * @cmd: ioctl command number
676 * @arg: this is third argument given to ioctl(2) system call.
677 * Often contains a pointer.
679 * Returns 0 if successful (some ioctls return postive numbers on
680 * success as well). Returns a negated errno value in case of error.
682 * Note: most ioctls are forward onto the block subsystem or further
683 * down in the scsi subsytem.
685 static int sd_ioctl(struct inode * inode, struct file * filp,
686 unsigned int cmd, unsigned long arg)
688 struct block_device *bdev = inode->i_bdev;
689 struct gendisk *disk = bdev->bd_disk;
690 struct scsi_device *sdp = scsi_disk(disk)->device;
691 void __user *p = (void __user *)arg;
692 int error;
694 SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
695 disk->disk_name, cmd));
698 * If we are in the middle of error recovery, don't let anyone
699 * else try and use this device. Also, if error recovery fails, it
700 * may try and take the device offline, in which case all further
701 * access to the device is prohibited.
703 error = scsi_nonblockable_ioctl(sdp, cmd, p, filp);
704 if (!scsi_block_when_processing_errors(sdp) || !error)
705 return error;
708 * Send SCSI addressing ioctls directly to mid level, send other
709 * ioctls to block level and then onto mid level if they can't be
710 * resolved.
712 switch (cmd) {
713 case SCSI_IOCTL_GET_IDLUN:
714 case SCSI_IOCTL_GET_BUS_NUMBER:
715 return scsi_ioctl(sdp, cmd, p);
716 default:
717 error = scsi_cmd_ioctl(filp, disk, cmd, p);
718 if (error != -ENOTTY)
719 return error;
721 return scsi_ioctl(sdp, cmd, p);
724 static void set_media_not_present(struct scsi_disk *sdkp)
726 sdkp->media_present = 0;
727 sdkp->capacity = 0;
728 sdkp->device->changed = 1;
732 * sd_media_changed - check if our medium changed
733 * @disk: kernel device descriptor
735 * Returns 0 if not applicable or no change; 1 if change
737 * Note: this function is invoked from the block subsystem.
739 static int sd_media_changed(struct gendisk *disk)
741 struct scsi_disk *sdkp = scsi_disk(disk);
742 struct scsi_device *sdp = sdkp->device;
743 int retval;
745 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_media_changed\n"));
747 if (!sdp->removable)
748 return 0;
751 * If the device is offline, don't send any commands - just pretend as
752 * if the command failed. If the device ever comes back online, we
753 * can deal with it then. It is only because of unrecoverable errors
754 * that we would ever take a device offline in the first place.
756 if (!scsi_device_online(sdp))
757 goto not_present;
760 * Using TEST_UNIT_READY enables differentiation between drive with
761 * no cartridge loaded - NOT READY, drive with changed cartridge -
762 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
764 * Drives that auto spin down. eg iomega jaz 1G, will be started
765 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
766 * sd_revalidate() is called.
768 retval = -ENODEV;
769 if (scsi_block_when_processing_errors(sdp))
770 retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES);
773 * Unable to test, unit probably not ready. This usually
774 * means there is no disc in the drive. Mark as changed,
775 * and we will figure it out later once the drive is
776 * available again.
778 if (retval)
779 goto not_present;
782 * For removable scsi disk we have to recognise the presence
783 * of a disk in the drive. This is kept in the struct scsi_disk
784 * struct and tested at open ! Daniel Roche (dan@lectra.fr)
786 sdkp->media_present = 1;
788 retval = sdp->changed;
789 sdp->changed = 0;
791 return retval;
793 not_present:
794 set_media_not_present(sdkp);
795 return 1;
798 static int sd_sync_cache(struct scsi_disk *sdkp)
800 int retries, res;
801 struct scsi_device *sdp = sdkp->device;
802 struct scsi_sense_hdr sshdr;
804 if (!scsi_device_online(sdp))
805 return -ENODEV;
808 for (retries = 3; retries > 0; --retries) {
809 unsigned char cmd[10] = { 0 };
811 cmd[0] = SYNCHRONIZE_CACHE;
813 * Leave the rest of the command zero to indicate
814 * flush everything.
816 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
817 SD_TIMEOUT, SD_MAX_RETRIES);
818 if (res == 0)
819 break;
822 if (res) {
823 sd_print_result(sdkp, res);
824 if (driver_byte(res) & DRIVER_SENSE)
825 sd_print_sense_hdr(sdkp, &sshdr);
828 if (res)
829 return -EIO;
830 return 0;
833 static int sd_issue_flush(struct device *dev, sector_t *error_sector)
835 int ret = 0;
836 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
838 if (!sdkp)
839 return -ENODEV;
841 if (sdkp->WCE)
842 ret = sd_sync_cache(sdkp);
843 scsi_disk_put(sdkp);
844 return ret;
847 static void sd_prepare_flush(request_queue_t *q, struct request *rq)
849 memset(rq->cmd, 0, sizeof(rq->cmd));
850 rq->cmd_type = REQ_TYPE_BLOCK_PC;
851 rq->timeout = SD_TIMEOUT;
852 rq->cmd[0] = SYNCHRONIZE_CACHE;
853 rq->cmd_len = 10;
856 static void sd_rescan(struct device *dev)
858 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
860 if (sdkp) {
861 sd_revalidate_disk(sdkp->disk);
862 scsi_disk_put(sdkp);
867 #ifdef CONFIG_COMPAT
869 * This gets directly called from VFS. When the ioctl
870 * is not recognized we go back to the other translation paths.
872 static long sd_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
874 struct block_device *bdev = file->f_path.dentry->d_inode->i_bdev;
875 struct gendisk *disk = bdev->bd_disk;
876 struct scsi_device *sdev = scsi_disk(disk)->device;
879 * If we are in the middle of error recovery, don't let anyone
880 * else try and use this device. Also, if error recovery fails, it
881 * may try and take the device offline, in which case all further
882 * access to the device is prohibited.
884 if (!scsi_block_when_processing_errors(sdev))
885 return -ENODEV;
887 if (sdev->host->hostt->compat_ioctl) {
888 int ret;
890 ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
892 return ret;
896 * Let the static ioctl translation table take care of it.
898 return -ENOIOCTLCMD;
900 #endif
902 static struct block_device_operations sd_fops = {
903 .owner = THIS_MODULE,
904 .open = sd_open,
905 .release = sd_release,
906 .ioctl = sd_ioctl,
907 .getgeo = sd_getgeo,
908 #ifdef CONFIG_COMPAT
909 .compat_ioctl = sd_compat_ioctl,
910 #endif
911 .media_changed = sd_media_changed,
912 .revalidate_disk = sd_revalidate_disk,
916 * sd_rw_intr - bottom half handler: called when the lower level
917 * driver has completed (successfully or otherwise) a scsi command.
918 * @SCpnt: mid-level's per command structure.
920 * Note: potentially run from within an ISR. Must not block.
922 static void sd_rw_intr(struct scsi_cmnd * SCpnt)
924 int result = SCpnt->result;
925 unsigned int xfer_size = SCpnt->request_bufflen;
926 unsigned int good_bytes = result ? 0 : xfer_size;
927 u64 start_lba = SCpnt->request->sector;
928 u64 end_lba = SCpnt->request->sector + (xfer_size / 512);
929 u64 bad_lba;
930 struct scsi_sense_hdr sshdr;
931 int sense_valid = 0;
932 int sense_deferred = 0;
933 int info_valid;
935 if (result) {
936 sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
937 if (sense_valid)
938 sense_deferred = scsi_sense_is_deferred(&sshdr);
940 #ifdef CONFIG_SCSI_LOGGING
941 SCSI_LOG_HLCOMPLETE(1, scsi_print_result(SCpnt));
942 if (sense_valid) {
943 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
944 "sd_rw_intr: sb[respc,sk,asc,"
945 "ascq]=%x,%x,%x,%x\n",
946 sshdr.response_code,
947 sshdr.sense_key, sshdr.asc,
948 sshdr.ascq));
950 #endif
951 if (driver_byte(result) != DRIVER_SENSE &&
952 (!sense_valid || sense_deferred))
953 goto out;
955 switch (sshdr.sense_key) {
956 case HARDWARE_ERROR:
957 case MEDIUM_ERROR:
958 if (!blk_fs_request(SCpnt->request))
959 goto out;
960 info_valid = scsi_get_sense_info_fld(SCpnt->sense_buffer,
961 SCSI_SENSE_BUFFERSIZE,
962 &bad_lba);
963 if (!info_valid)
964 goto out;
965 if (xfer_size <= SCpnt->device->sector_size)
966 goto out;
967 if (SCpnt->device->sector_size < 512) {
968 /* only legitimate sector_size here is 256 */
969 start_lba <<= 1;
970 end_lba <<= 1;
971 } else {
972 /* be careful ... don't want any overflows */
973 u64 factor = SCpnt->device->sector_size / 512;
974 do_div(start_lba, factor);
975 do_div(end_lba, factor);
978 if (bad_lba < start_lba || bad_lba >= end_lba)
979 /* the bad lba was reported incorrectly, we have
980 * no idea where the error is
982 goto out;
984 /* This computation should always be done in terms of
985 * the resolution of the device's medium.
987 good_bytes = (bad_lba - start_lba)*SCpnt->device->sector_size;
988 break;
989 case RECOVERED_ERROR:
990 good_bytes = xfer_size;
991 break;
992 case NO_SENSE:
993 /* This indicates a false check condition, so ignore it. An
994 * unknown amount of data was transferred so treat it as an
995 * error.
997 scsi_print_sense("sd", SCpnt);
998 SCpnt->result = 0;
999 memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1000 break;
1001 case ILLEGAL_REQUEST:
1002 if (SCpnt->device->use_10_for_rw &&
1003 (SCpnt->cmnd[0] == READ_10 ||
1004 SCpnt->cmnd[0] == WRITE_10))
1005 SCpnt->device->use_10_for_rw = 0;
1006 if (SCpnt->device->use_10_for_ms &&
1007 (SCpnt->cmnd[0] == MODE_SENSE_10 ||
1008 SCpnt->cmnd[0] == MODE_SELECT_10))
1009 SCpnt->device->use_10_for_ms = 0;
1010 break;
1011 default:
1012 break;
1014 out:
1015 scsi_io_completion(SCpnt, good_bytes);
1018 static int media_not_present(struct scsi_disk *sdkp,
1019 struct scsi_sense_hdr *sshdr)
1022 if (!scsi_sense_valid(sshdr))
1023 return 0;
1024 /* not invoked for commands that could return deferred errors */
1025 if (sshdr->sense_key != NOT_READY &&
1026 sshdr->sense_key != UNIT_ATTENTION)
1027 return 0;
1028 if (sshdr->asc != 0x3A) /* medium not present */
1029 return 0;
1031 set_media_not_present(sdkp);
1032 return 1;
1036 * spinup disk - called only in sd_revalidate_disk()
1038 static void
1039 sd_spinup_disk(struct scsi_disk *sdkp)
1041 unsigned char cmd[10];
1042 unsigned long spintime_expire = 0;
1043 int retries, spintime;
1044 unsigned int the_result;
1045 struct scsi_sense_hdr sshdr;
1046 int sense_valid = 0;
1048 spintime = 0;
1050 /* Spin up drives, as required. Only do this at boot time */
1051 /* Spinup needs to be done for module loads too. */
1052 do {
1053 retries = 0;
1055 do {
1056 cmd[0] = TEST_UNIT_READY;
1057 memset((void *) &cmd[1], 0, 9);
1059 the_result = scsi_execute_req(sdkp->device, cmd,
1060 DMA_NONE, NULL, 0,
1061 &sshdr, SD_TIMEOUT,
1062 SD_MAX_RETRIES);
1065 * If the drive has indicated to us that it
1066 * doesn't have any media in it, don't bother
1067 * with any more polling.
1069 if (media_not_present(sdkp, &sshdr))
1070 return;
1072 if (the_result)
1073 sense_valid = scsi_sense_valid(&sshdr);
1074 retries++;
1075 } while (retries < 3 &&
1076 (!scsi_status_is_good(the_result) ||
1077 ((driver_byte(the_result) & DRIVER_SENSE) &&
1078 sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
1080 if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
1081 /* no sense, TUR either succeeded or failed
1082 * with a status error */
1083 if(!spintime && !scsi_status_is_good(the_result)) {
1084 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1085 sd_print_result(sdkp, the_result);
1087 break;
1091 * The device does not want the automatic start to be issued.
1093 if (sdkp->device->no_start_on_add)
1094 break;
1096 if (sense_valid && sshdr.sense_key == NOT_READY) {
1097 if (sshdr.asc == 4 && sshdr.ascq == 3)
1098 break; /* manual intervention required */
1099 if (sshdr.asc == 4 && sshdr.ascq == 0xb)
1100 break; /* standby */
1101 if (sshdr.asc == 4 && sshdr.ascq == 0xc)
1102 break; /* unavailable */
1104 * Issue command to spin up drive when not ready
1106 if (!spintime) {
1107 sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
1108 cmd[0] = START_STOP;
1109 cmd[1] = 1; /* Return immediately */
1110 memset((void *) &cmd[2], 0, 8);
1111 cmd[4] = 1; /* Start spin cycle */
1112 scsi_execute_req(sdkp->device, cmd, DMA_NONE,
1113 NULL, 0, &sshdr,
1114 SD_TIMEOUT, SD_MAX_RETRIES);
1115 spintime_expire = jiffies + 100 * HZ;
1116 spintime = 1;
1118 /* Wait 1 second for next try */
1119 msleep(1000);
1120 printk(".");
1123 * Wait for USB flash devices with slow firmware.
1124 * Yes, this sense key/ASC combination shouldn't
1125 * occur here. It's characteristic of these devices.
1127 } else if (sense_valid &&
1128 sshdr.sense_key == UNIT_ATTENTION &&
1129 sshdr.asc == 0x28) {
1130 if (!spintime) {
1131 spintime_expire = jiffies + 5 * HZ;
1132 spintime = 1;
1134 /* Wait 1 second for next try */
1135 msleep(1000);
1136 } else {
1137 /* we don't understand the sense code, so it's
1138 * probably pointless to loop */
1139 if(!spintime) {
1140 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1141 sd_print_sense_hdr(sdkp, &sshdr);
1143 break;
1146 } while (spintime && time_before_eq(jiffies, spintime_expire));
1148 if (spintime) {
1149 if (scsi_status_is_good(the_result))
1150 printk("ready\n");
1151 else
1152 printk("not responding...\n");
1157 * read disk capacity
1159 static void
1160 sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
1162 unsigned char cmd[16];
1163 int the_result, retries;
1164 int sector_size = 0;
1165 int longrc = 0;
1166 struct scsi_sense_hdr sshdr;
1167 int sense_valid = 0;
1168 struct scsi_device *sdp = sdkp->device;
1169 sector_t old_capacity = sdkp->capacity;
1171 repeat:
1172 retries = 3;
1173 do {
1174 if (longrc) {
1175 memset((void *) cmd, 0, 16);
1176 cmd[0] = SERVICE_ACTION_IN;
1177 cmd[1] = SAI_READ_CAPACITY_16;
1178 cmd[13] = 12;
1179 memset((void *) buffer, 0, 12);
1180 } else {
1181 cmd[0] = READ_CAPACITY;
1182 memset((void *) &cmd[1], 0, 9);
1183 memset((void *) buffer, 0, 8);
1186 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
1187 buffer, longrc ? 12 : 8, &sshdr,
1188 SD_TIMEOUT, SD_MAX_RETRIES);
1190 if (media_not_present(sdkp, &sshdr))
1191 return;
1193 if (the_result)
1194 sense_valid = scsi_sense_valid(&sshdr);
1195 retries--;
1197 } while (the_result && retries);
1199 if (the_result && !longrc) {
1200 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY failed\n");
1201 sd_print_result(sdkp, the_result);
1202 if (driver_byte(the_result) & DRIVER_SENSE)
1203 sd_print_sense_hdr(sdkp, &sshdr);
1204 else
1205 sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
1207 /* Set dirty bit for removable devices if not ready -
1208 * sometimes drives will not report this properly. */
1209 if (sdp->removable &&
1210 sense_valid && sshdr.sense_key == NOT_READY)
1211 sdp->changed = 1;
1213 /* Either no media are present but the drive didn't tell us,
1214 or they are present but the read capacity command fails */
1215 /* sdkp->media_present = 0; -- not always correct */
1216 sdkp->capacity = 0; /* unknown mapped to zero - as usual */
1218 return;
1219 } else if (the_result && longrc) {
1220 /* READ CAPACITY(16) has been failed */
1221 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY(16) failed\n");
1222 sd_print_result(sdkp, the_result);
1223 sd_printk(KERN_NOTICE, sdkp, "Use 0xffffffff as device size\n");
1225 sdkp->capacity = 1 + (sector_t) 0xffffffff;
1226 goto got_data;
1229 if (!longrc) {
1230 sector_size = get_unaligned_be32(&buffer[4]);
1231 if (buffer[0] == 0xff && buffer[1] == 0xff &&
1232 buffer[2] == 0xff && buffer[3] == 0xff) {
1233 if(sizeof(sdkp->capacity) > 4) {
1234 sd_printk(KERN_NOTICE, sdkp, "Very big device. "
1235 "Trying to use READ CAPACITY(16).\n");
1236 longrc = 1;
1237 goto repeat;
1239 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use "
1240 "a kernel compiled with support for large "
1241 "block devices.\n");
1242 sdkp->capacity = 0;
1243 goto got_data;
1245 sdkp->capacity = 1 + (sector_t)get_unaligned_be32(&buffer[0]);
1246 } else {
1247 sdkp->capacity = 1 + get_unaligned_be64(&buffer[0]);
1249 sector_size = get_unaligned_be32(&buffer[8]);
1252 /* Some devices are known to return the total number of blocks,
1253 * not the highest block number. Some devices have versions
1254 * which do this and others which do not. Some devices we might
1255 * suspect of doing this but we don't know for certain.
1257 * If we know the reported capacity is wrong, decrement it. If
1258 * we can only guess, then assume the number of blocks is even
1259 * (usually true but not always) and err on the side of lowering
1260 * the capacity.
1262 if (sdp->fix_capacity ||
1263 (sdp->guess_capacity && (sdkp->capacity & 0x01))) {
1264 sd_printk(KERN_INFO, sdkp, "Adjusting the sector count "
1265 "from its reported value: %llu\n",
1266 (unsigned long long) sdkp->capacity);
1267 --sdkp->capacity;
1270 got_data:
1271 if (sector_size == 0) {
1272 sector_size = 512;
1273 sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
1274 "assuming 512.\n");
1277 if (sector_size != 512 &&
1278 sector_size != 1024 &&
1279 sector_size != 2048 &&
1280 sector_size != 4096 &&
1281 sector_size != 256) {
1282 sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
1283 sector_size);
1285 * The user might want to re-format the drive with
1286 * a supported sectorsize. Once this happens, it
1287 * would be relatively trivial to set the thing up.
1288 * For this reason, we leave the thing in the table.
1290 sdkp->capacity = 0;
1292 * set a bogus sector size so the normal read/write
1293 * logic in the block layer will eventually refuse any
1294 * request on this device without tripping over power
1295 * of two sector size assumptions
1297 sector_size = 512;
1301 * The msdos fs needs to know the hardware sector size
1302 * So I have created this table. See ll_rw_blk.c
1303 * Jacques Gelinas (Jacques@solucorp.qc.ca)
1305 int hard_sector = sector_size;
1306 sector_t sz = (sdkp->capacity/2) * (hard_sector/256);
1307 request_queue_t *queue = sdp->request_queue;
1308 sector_t mb = sz;
1310 blk_queue_hardsect_size(queue, hard_sector);
1311 /* avoid 64-bit division on 32-bit platforms */
1312 sector_div(sz, 625);
1313 mb -= sz - 974;
1314 sector_div(mb, 1950);
1316 if (sdkp->first_scan || old_capacity != sdkp->capacity)
1317 sd_printk(KERN_NOTICE, sdkp,
1318 "%llu %d-byte hardware sectors (%llu MB)\n",
1319 (unsigned long long)sdkp->capacity,
1320 hard_sector, (unsigned long long)mb);
1323 /* Rescale capacity to 512-byte units */
1324 if (sector_size == 4096)
1325 sdkp->capacity <<= 3;
1326 else if (sector_size == 2048)
1327 sdkp->capacity <<= 2;
1328 else if (sector_size == 1024)
1329 sdkp->capacity <<= 1;
1330 else if (sector_size == 256)
1331 sdkp->capacity >>= 1;
1333 sdkp->device->sector_size = sector_size;
1336 /* called with buffer of length 512 */
1337 static inline int
1338 sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
1339 unsigned char *buffer, int len, struct scsi_mode_data *data,
1340 struct scsi_sense_hdr *sshdr)
1342 return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
1343 SD_TIMEOUT, SD_MAX_RETRIES, data,
1344 sshdr);
1348 * read write protect setting, if possible - called only in sd_revalidate_disk()
1349 * called with buffer of length SD_BUF_SIZE
1351 static void
1352 sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
1354 int res;
1355 struct scsi_device *sdp = sdkp->device;
1356 struct scsi_mode_data data;
1357 int old_wp = sdkp->write_prot;
1359 set_disk_ro(sdkp->disk, 0);
1360 if (sdp->skip_ms_page_3f) {
1361 sd_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
1362 return;
1365 if (sdp->use_192_bytes_for_3f) {
1366 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
1367 } else {
1369 * First attempt: ask for all pages (0x3F), but only 4 bytes.
1370 * We have to start carefully: some devices hang if we ask
1371 * for more than is available.
1373 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
1376 * Second attempt: ask for page 0 When only page 0 is
1377 * implemented, a request for page 3F may return Sense Key
1378 * 5: Illegal Request, Sense Code 24: Invalid field in
1379 * CDB.
1381 if (!scsi_status_is_good(res))
1382 res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
1385 * Third attempt: ask 255 bytes, as we did earlier.
1387 if (!scsi_status_is_good(res))
1388 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
1389 &data, NULL);
1392 if (!scsi_status_is_good(res)) {
1393 sd_printk(KERN_WARNING, sdkp,
1394 "Test WP failed, assume Write Enabled\n");
1395 } else {
1396 sdkp->write_prot = ((data.device_specific & 0x80) != 0);
1397 set_disk_ro(sdkp->disk, sdkp->write_prot);
1398 if (sdkp->first_scan || old_wp != sdkp->write_prot) {
1399 sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
1400 sdkp->write_prot ? "on" : "off");
1401 sd_printk(KERN_DEBUG, sdkp,
1402 "Mode Sense: %02x %02x %02x %02x\n",
1403 buffer[0], buffer[1], buffer[2], buffer[3]);
1409 * sd_read_cache_type - called only from sd_revalidate_disk()
1410 * called with buffer of length SD_BUF_SIZE
1412 static void
1413 sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
1415 int len = 0, res;
1416 struct scsi_device *sdp = sdkp->device;
1417 int old_wce = sdkp->WCE;
1418 int old_rcd = sdkp->RCD;
1419 int old_dpofua = sdkp->DPOFUA;
1421 int dbd;
1422 int modepage;
1423 struct scsi_mode_data data;
1424 struct scsi_sense_hdr sshdr;
1426 if (sdp->skip_ms_page_8)
1427 goto defaults;
1429 if (sdp->type == TYPE_RBC) {
1430 modepage = 6;
1431 dbd = 8;
1432 } else {
1433 modepage = 8;
1434 dbd = 0;
1437 /* cautiously ask */
1438 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr);
1440 if (!scsi_status_is_good(res))
1441 goto bad_sense;
1443 if (!data.header_length) {
1444 modepage = 6;
1445 sd_printk(KERN_ERR, sdkp, "Missing header in MODE_SENSE response\n");
1448 /* that went OK, now ask for the proper length */
1449 len = data.length;
1452 * We're only interested in the first three bytes, actually.
1453 * But the data cache page is defined for the first 20.
1455 if (len < 3)
1456 goto bad_sense;
1457 if (len > 20)
1458 len = 20;
1460 /* Take headers and block descriptors into account */
1461 len += data.header_length + data.block_descriptor_length;
1462 if (len > SD_BUF_SIZE)
1463 goto bad_sense;
1465 /* Get the data */
1466 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr);
1468 if (scsi_status_is_good(res)) {
1469 int offset = data.header_length + data.block_descriptor_length;
1471 if (offset >= SD_BUF_SIZE - 2) {
1472 sd_printk(KERN_ERR, sdkp, "Malformed MODE SENSE response\n");
1473 goto defaults;
1476 if ((buffer[offset] & 0x3f) != modepage) {
1477 sd_printk(KERN_ERR, sdkp, "Got wrong page\n");
1478 goto defaults;
1481 if (modepage == 8) {
1482 sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
1483 sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
1484 } else {
1485 sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
1486 sdkp->RCD = 0;
1489 sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
1490 if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
1491 sd_printk(KERN_NOTICE, sdkp,
1492 "Uses READ/WRITE(6), disabling FUA\n");
1493 sdkp->DPOFUA = 0;
1496 if (sdkp->first_scan || old_wce != sdkp->WCE ||
1497 old_rcd != sdkp->RCD || old_dpofua != sdkp->DPOFUA)
1498 sd_printk(KERN_NOTICE, sdkp,
1499 "Write cache: %s, read cache: %s, %s\n",
1500 sdkp->WCE ? "enabled" : "disabled",
1501 sdkp->RCD ? "disabled" : "enabled",
1502 sdkp->DPOFUA ? "supports DPO and FUA"
1503 : "doesn't support DPO or FUA");
1505 return;
1508 bad_sense:
1509 if (scsi_sense_valid(&sshdr) &&
1510 sshdr.sense_key == ILLEGAL_REQUEST &&
1511 sshdr.asc == 0x24 && sshdr.ascq == 0x0)
1512 /* Invalid field in CDB */
1513 sd_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
1514 else
1515 sd_printk(KERN_ERR, sdkp, "Asking for cache data failed\n");
1517 defaults:
1518 sd_printk(KERN_ERR, sdkp, "Assuming drive cache: write through\n");
1519 sdkp->WCE = 0;
1520 sdkp->RCD = 0;
1521 sdkp->DPOFUA = 0;
1525 * sd_revalidate_disk - called the first time a new disk is seen,
1526 * performs disk spin up, read_capacity, etc.
1527 * @disk: struct gendisk we care about
1529 static int sd_revalidate_disk(struct gendisk *disk)
1531 struct scsi_disk *sdkp = scsi_disk(disk);
1532 struct scsi_device *sdp = sdkp->device;
1533 unsigned char *buffer;
1534 unsigned ordered;
1536 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
1537 "sd_revalidate_disk\n"));
1540 * If the device is offline, don't try and read capacity or any
1541 * of the other niceties.
1543 if (!scsi_device_online(sdp))
1544 goto out;
1546 buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL);
1547 if (!buffer) {
1548 sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
1549 "allocation failure.\n");
1550 goto out;
1553 sd_spinup_disk(sdkp);
1556 * Without media there is no reason to ask; moreover, some devices
1557 * react badly if we do.
1559 if (sdkp->media_present) {
1560 sd_read_capacity(sdkp, buffer);
1561 sd_read_write_protect_flag(sdkp, buffer);
1562 sd_read_cache_type(sdkp, buffer);
1565 sdkp->first_scan = 0;
1568 * We now have all cache related info, determine how we deal
1569 * with ordered requests. Note that as the current SCSI
1570 * dispatch function can alter request order, we cannot use
1571 * QUEUE_ORDERED_TAG_* even when ordered tag is supported.
1573 if (sdkp->WCE)
1574 ordered = sdkp->DPOFUA
1575 ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH;
1576 else
1577 ordered = QUEUE_ORDERED_DRAIN;
1579 blk_queue_ordered(sdkp->disk->queue, ordered, sd_prepare_flush);
1581 set_capacity(disk, sdkp->capacity);
1582 kfree(buffer);
1584 out:
1585 return 0;
1589 * sd_probe - called during driver initialization and whenever a
1590 * new scsi device is attached to the system. It is called once
1591 * for each scsi device (not just disks) present.
1592 * @dev: pointer to device object
1594 * Returns 0 if successful (or not interested in this scsi device
1595 * (e.g. scanner)); 1 when there is an error.
1597 * Note: this function is invoked from the scsi mid-level.
1598 * This function sets up the mapping between a given
1599 * <host,channel,id,lun> (found in sdp) and new device name
1600 * (e.g. /dev/sda). More precisely it is the block device major
1601 * and minor number that is chosen here.
1603 * Assume sd_attach is not re-entrant (for time being)
1604 * Also think about sd_attach() and sd_remove() running coincidentally.
1606 static int sd_probe(struct device *dev)
1608 struct scsi_device *sdp = to_scsi_device(dev);
1609 struct scsi_disk *sdkp;
1610 struct gendisk *gd;
1611 u32 index;
1612 int error;
1614 error = -ENODEV;
1615 if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
1616 goto out;
1618 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
1619 "sd_attach\n"));
1621 error = -ENOMEM;
1622 sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
1623 if (!sdkp)
1624 goto out;
1626 gd = alloc_disk(16);
1627 if (!gd)
1628 goto out_free;
1630 do {
1631 if (!ida_pre_get(&sd_index_ida, GFP_KERNEL))
1632 goto out_put;
1634 error = ida_get_new(&sd_index_ida, &index);
1635 } while (error == -EAGAIN);
1637 if (error)
1638 goto out_put;
1640 error = -EBUSY;
1641 if (index >= SD_MAX_DISKS)
1642 goto out_free_index;
1644 sdkp->device = sdp;
1645 sdkp->driver = &sd_template;
1646 sdkp->disk = gd;
1647 sdkp->index = index;
1648 sdkp->openers = 0;
1650 if (!sdp->timeout) {
1651 if (sdp->type != TYPE_MOD)
1652 sdp->timeout = SD_TIMEOUT;
1653 else
1654 sdp->timeout = SD_MOD_TIMEOUT;
1657 class_device_initialize(&sdkp->cdev);
1658 sdkp->cdev.dev = &sdp->sdev_gendev;
1659 sdkp->cdev.class = &sd_disk_class;
1660 strncpy(sdkp->cdev.class_id, sdp->sdev_gendev.bus_id, BUS_ID_SIZE);
1662 if (class_device_add(&sdkp->cdev))
1663 goto out_free_index;
1665 get_device(&sdp->sdev_gendev);
1667 gd->major = sd_major((index & 0xf0) >> 4);
1668 gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
1669 gd->minors = 16;
1670 gd->fops = &sd_fops;
1672 if (index < 26) {
1673 sprintf(gd->disk_name, "sd%c", 'a' + index % 26);
1674 } else if (index < (26 + 1) * 26) {
1675 sprintf(gd->disk_name, "sd%c%c",
1676 'a' + index / 26 - 1,'a' + index % 26);
1677 } else {
1678 const unsigned int m1 = (index / 26 - 1) / 26 - 1;
1679 const unsigned int m2 = (index / 26 - 1) % 26;
1680 const unsigned int m3 = index % 26;
1681 sprintf(gd->disk_name, "sd%c%c%c",
1682 'a' + m1, 'a' + m2, 'a' + m3);
1685 gd->private_data = &sdkp->driver;
1686 gd->queue = sdkp->device->request_queue;
1688 /* defaults, until the device tells us otherwise */
1689 sdp->sector_size = 512;
1690 sdkp->capacity = 0;
1691 sdkp->media_present = 1;
1692 sdkp->write_prot = 0;
1693 sdkp->WCE = 0;
1694 sdkp->RCD = 0;
1695 sdkp->first_scan = 1;
1697 sd_revalidate_disk(gd);
1699 gd->driverfs_dev = &sdp->sdev_gendev;
1700 gd->flags = GENHD_FL_DRIVERFS;
1701 if (sdp->removable)
1702 gd->flags |= GENHD_FL_REMOVABLE;
1704 dev_set_drvdata(dev, sdkp);
1705 add_disk(gd);
1707 sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
1708 sdp->removable ? "removable " : "");
1710 return 0;
1712 out_free_index:
1713 ida_remove(&sd_index_ida, index);
1714 out_put:
1715 put_disk(gd);
1716 out_free:
1717 kfree(sdkp);
1718 out:
1719 return error;
1723 * sd_remove - called whenever a scsi disk (previously recognized by
1724 * sd_probe) is detached from the system. It is called (potentially
1725 * multiple times) during sd module unload.
1726 * @sdp: pointer to mid level scsi device object
1728 * Note: this function is invoked from the scsi mid-level.
1729 * This function potentially frees up a device name (e.g. /dev/sdc)
1730 * that could be re-used by a subsequent sd_probe().
1731 * This function is not called when the built-in sd driver is "exit-ed".
1733 static int sd_remove(struct device *dev)
1735 struct scsi_disk *sdkp = dev_get_drvdata(dev);
1737 class_device_del(&sdkp->cdev);
1738 del_gendisk(sdkp->disk);
1739 sd_shutdown(dev);
1741 mutex_lock(&sd_ref_mutex);
1742 dev_set_drvdata(dev, NULL);
1743 class_device_put(&sdkp->cdev);
1744 mutex_unlock(&sd_ref_mutex);
1746 return 0;
1750 * scsi_disk_release - Called to free the scsi_disk structure
1751 * @cdev: pointer to embedded class device
1753 * sd_ref_mutex must be held entering this routine. Because it is
1754 * called on last put, you should always use the scsi_disk_get()
1755 * scsi_disk_put() helpers which manipulate the semaphore directly
1756 * and never do a direct class_device_put().
1758 static void scsi_disk_release(struct class_device *cdev)
1760 struct scsi_disk *sdkp = to_scsi_disk(cdev);
1761 struct gendisk *disk = sdkp->disk;
1763 ida_remove(&sd_index_ida, sdkp->index);
1765 disk->private_data = NULL;
1766 put_disk(disk);
1767 put_device(&sdkp->device->sdev_gendev);
1769 kfree(sdkp);
1772 static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
1774 unsigned char cmd[6] = { START_STOP }; /* START_VALID */
1775 struct scsi_sense_hdr sshdr;
1776 struct scsi_device *sdp = sdkp->device;
1777 int res;
1779 if (start)
1780 cmd[4] |= 1; /* START */
1782 if (!scsi_device_online(sdp))
1783 return -ENODEV;
1785 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
1786 SD_TIMEOUT, SD_MAX_RETRIES);
1787 if (res) {
1788 sd_printk(KERN_WARNING, sdkp, "START_STOP FAILED\n");
1789 sd_print_result(sdkp, res);
1790 if (driver_byte(res) & DRIVER_SENSE)
1791 sd_print_sense_hdr(sdkp, &sshdr);
1794 return res;
1798 * Send a SYNCHRONIZE CACHE instruction down to the device through
1799 * the normal SCSI command structure. Wait for the command to
1800 * complete.
1802 static void sd_shutdown(struct device *dev)
1804 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
1806 if (!sdkp)
1807 return; /* this can happen */
1809 if (sdkp->WCE) {
1810 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
1811 sd_sync_cache(sdkp);
1814 if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
1815 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
1816 sd_start_stop_device(sdkp, 0);
1819 scsi_disk_put(sdkp);
1822 static int sd_suspend(struct device *dev, pm_message_t mesg)
1824 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
1825 int ret = 0;
1827 if (!sdkp)
1828 return 0; /* this can happen */
1830 if (sdkp->WCE) {
1831 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
1832 ret = sd_sync_cache(sdkp);
1833 if (ret)
1834 goto done;
1837 if (mesg.event == PM_EVENT_SUSPEND &&
1838 sdkp->device->manage_start_stop) {
1839 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
1840 ret = sd_start_stop_device(sdkp, 0);
1843 done:
1844 scsi_disk_put(sdkp);
1845 return ret;
1848 static int sd_resume(struct device *dev)
1850 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
1851 int ret = 0;
1853 if (!sdkp->device->manage_start_stop)
1854 goto done;
1856 sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
1857 ret = sd_start_stop_device(sdkp, 1);
1859 done:
1860 scsi_disk_put(sdkp);
1861 return ret;
1865 * init_sd - entry point for this driver (both when built in or when
1866 * a module).
1868 * Note: this function registers this driver with the scsi mid-level.
1870 static int __init init_sd(void)
1872 int majors = 0, i, err;
1874 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
1876 for (i = 0; i < SD_MAJORS; i++)
1877 if (register_blkdev(sd_major(i), "sd") == 0)
1878 majors++;
1880 if (!majors)
1881 return -ENODEV;
1883 err = class_register(&sd_disk_class);
1884 if (err)
1885 goto err_out;
1887 err = scsi_register_driver(&sd_template.gendrv);
1888 if (err)
1889 goto err_out_class;
1891 return 0;
1893 err_out_class:
1894 class_unregister(&sd_disk_class);
1895 err_out:
1896 for (i = 0; i < SD_MAJORS; i++)
1897 unregister_blkdev(sd_major(i), "sd");
1898 return err;
1902 * exit_sd - exit point for this driver (when it is a module).
1904 * Note: this function unregisters this driver from the scsi mid-level.
1906 static void __exit exit_sd(void)
1908 int i;
1910 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
1912 scsi_unregister_driver(&sd_template.gendrv);
1913 class_unregister(&sd_disk_class);
1915 for (i = 0; i < SD_MAJORS; i++)
1916 unregister_blkdev(sd_major(i), "sd");
1919 module_init(init_sd);
1920 module_exit(exit_sd);
1922 static void sd_print_sense_hdr(struct scsi_disk *sdkp,
1923 struct scsi_sense_hdr *sshdr)
1925 sd_printk(KERN_INFO, sdkp, "");
1926 scsi_show_sense_hdr(sshdr);
1927 sd_printk(KERN_INFO, sdkp, "");
1928 scsi_show_extd_sense(sshdr->asc, sshdr->ascq);
1931 static void sd_print_result(struct scsi_disk *sdkp, int result)
1933 sd_printk(KERN_INFO, sdkp, "");
1934 scsi_show_result(result);