[SCSI] sd: fix up start/stop messages for new sd_printk() API
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / sd.c
blobb044dcf73427bf26d5f011aedf98954b8f0a5aad
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>
52 #include <scsi/scsi.h>
53 #include <scsi/scsi_cmnd.h>
54 #include <scsi/scsi_dbg.h>
55 #include <scsi/scsi_device.h>
56 #include <scsi/scsi_driver.h>
57 #include <scsi/scsi_eh.h>
58 #include <scsi/scsi_host.h>
59 #include <scsi/scsi_ioctl.h>
60 #include <scsi/scsicam.h>
61 #include <scsi/sd.h>
63 #include "scsi_logging.h"
65 MODULE_AUTHOR("Eric Youngdale");
66 MODULE_DESCRIPTION("SCSI disk (sd) driver");
67 MODULE_LICENSE("GPL");
69 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR);
70 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR);
71 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR);
72 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR);
73 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR);
74 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR);
75 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR);
76 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR);
77 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR);
78 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR);
79 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR);
80 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR);
81 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
82 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
83 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
84 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
86 static DEFINE_IDR(sd_index_idr);
87 static DEFINE_SPINLOCK(sd_index_lock);
89 /* This semaphore is used to mediate the 0->1 reference get in the
90 * face of object destruction (i.e. we can't allow a get on an
91 * object after last put) */
92 static DEFINE_MUTEX(sd_ref_mutex);
94 static const char *sd_cache_types[] = {
95 "write through", "none", "write back",
96 "write back, no read (daft)"
99 static ssize_t sd_store_cache_type(struct class_device *cdev, const char *buf,
100 size_t count)
102 int i, ct = -1, rcd, wce, sp;
103 struct scsi_disk *sdkp = to_scsi_disk(cdev);
104 struct scsi_device *sdp = sdkp->device;
105 char buffer[64];
106 char *buffer_data;
107 struct scsi_mode_data data;
108 struct scsi_sense_hdr sshdr;
109 int len;
111 if (sdp->type != TYPE_DISK)
112 /* no cache control on RBC devices; theoretically they
113 * can do it, but there's probably so many exceptions
114 * it's not worth the risk */
115 return -EINVAL;
117 for (i = 0; i < ARRAY_SIZE(sd_cache_types); i++) {
118 const int len = strlen(sd_cache_types[i]);
119 if (strncmp(sd_cache_types[i], buf, len) == 0 &&
120 buf[len] == '\n') {
121 ct = i;
122 break;
125 if (ct < 0)
126 return -EINVAL;
127 rcd = ct & 0x01 ? 1 : 0;
128 wce = ct & 0x02 ? 1 : 0;
129 if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT,
130 SD_MAX_RETRIES, &data, NULL))
131 return -EINVAL;
132 len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
133 data.block_descriptor_length);
134 buffer_data = buffer + data.header_length +
135 data.block_descriptor_length;
136 buffer_data[2] &= ~0x05;
137 buffer_data[2] |= wce << 2 | rcd;
138 sp = buffer_data[0] & 0x80 ? 1 : 0;
140 if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT,
141 SD_MAX_RETRIES, &data, &sshdr)) {
142 if (scsi_sense_valid(&sshdr))
143 sd_print_sense_hdr(sdkp, &sshdr);
144 return -EINVAL;
146 sd_revalidate_disk(sdkp->disk);
147 return count;
150 static ssize_t sd_store_manage_start_stop(struct class_device *cdev,
151 const char *buf, size_t count)
153 struct scsi_disk *sdkp = to_scsi_disk(cdev);
154 struct scsi_device *sdp = sdkp->device;
156 if (!capable(CAP_SYS_ADMIN))
157 return -EACCES;
159 sdp->manage_start_stop = simple_strtoul(buf, NULL, 10);
161 return count;
164 static ssize_t sd_store_allow_restart(struct class_device *cdev, const char *buf,
165 size_t count)
167 struct scsi_disk *sdkp = to_scsi_disk(cdev);
168 struct scsi_device *sdp = sdkp->device;
170 if (!capable(CAP_SYS_ADMIN))
171 return -EACCES;
173 if (sdp->type != TYPE_DISK)
174 return -EINVAL;
176 sdp->allow_restart = simple_strtoul(buf, NULL, 10);
178 return count;
181 static ssize_t sd_show_cache_type(struct class_device *cdev, char *buf)
183 struct scsi_disk *sdkp = to_scsi_disk(cdev);
184 int ct = sdkp->RCD + 2*sdkp->WCE;
186 return snprintf(buf, 40, "%s\n", sd_cache_types[ct]);
189 static ssize_t sd_show_fua(struct class_device *cdev, char *buf)
191 struct scsi_disk *sdkp = to_scsi_disk(cdev);
193 return snprintf(buf, 20, "%u\n", sdkp->DPOFUA);
196 static ssize_t sd_show_manage_start_stop(struct class_device *cdev, char *buf)
198 struct scsi_disk *sdkp = to_scsi_disk(cdev);
199 struct scsi_device *sdp = sdkp->device;
201 return snprintf(buf, 20, "%u\n", sdp->manage_start_stop);
204 static ssize_t sd_show_allow_restart(struct class_device *cdev, char *buf)
206 struct scsi_disk *sdkp = to_scsi_disk(cdev);
208 return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart);
211 static struct class_device_attribute sd_disk_attrs[] = {
212 __ATTR(cache_type, S_IRUGO|S_IWUSR, sd_show_cache_type,
213 sd_store_cache_type),
214 __ATTR(FUA, S_IRUGO, sd_show_fua, NULL),
215 __ATTR(allow_restart, S_IRUGO|S_IWUSR, sd_show_allow_restart,
216 sd_store_allow_restart),
217 __ATTR(manage_start_stop, S_IRUGO|S_IWUSR, sd_show_manage_start_stop,
218 sd_store_manage_start_stop),
219 __ATTR_NULL,
222 static struct class sd_disk_class = {
223 .name = "scsi_disk",
224 .owner = THIS_MODULE,
225 .release = scsi_disk_release,
226 .class_dev_attrs = sd_disk_attrs,
229 static struct scsi_driver sd_template = {
230 .owner = THIS_MODULE,
231 .gendrv = {
232 .name = "sd",
233 .probe = sd_probe,
234 .remove = sd_remove,
235 .suspend = sd_suspend,
236 .resume = sd_resume,
237 .shutdown = sd_shutdown,
239 .rescan = sd_rescan,
240 .init_command = sd_init_command,
241 .issue_flush = sd_issue_flush,
245 * Device no to disk mapping:
247 * major disc2 disc p1
248 * |............|.............|....|....| <- dev_t
249 * 31 20 19 8 7 4 3 0
251 * Inside a major, we have 16k disks, however mapped non-
252 * contiguously. The first 16 disks are for major0, the next
253 * ones with major1, ... Disk 256 is for major0 again, disk 272
254 * for major1, ...
255 * As we stay compatible with our numbering scheme, we can reuse
256 * the well-know SCSI majors 8, 65--71, 136--143.
258 static int sd_major(int major_idx)
260 switch (major_idx) {
261 case 0:
262 return SCSI_DISK0_MAJOR;
263 case 1 ... 7:
264 return SCSI_DISK1_MAJOR + major_idx - 1;
265 case 8 ... 15:
266 return SCSI_DISK8_MAJOR + major_idx - 8;
267 default:
268 BUG();
269 return 0; /* shut up gcc */
273 static inline struct scsi_disk *scsi_disk(struct gendisk *disk)
275 return container_of(disk->private_data, struct scsi_disk, driver);
278 static struct scsi_disk *__scsi_disk_get(struct gendisk *disk)
280 struct scsi_disk *sdkp = NULL;
282 if (disk->private_data) {
283 sdkp = scsi_disk(disk);
284 if (scsi_device_get(sdkp->device) == 0)
285 class_device_get(&sdkp->cdev);
286 else
287 sdkp = NULL;
289 return sdkp;
292 static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
294 struct scsi_disk *sdkp;
296 mutex_lock(&sd_ref_mutex);
297 sdkp = __scsi_disk_get(disk);
298 mutex_unlock(&sd_ref_mutex);
299 return sdkp;
302 static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev)
304 struct scsi_disk *sdkp;
306 mutex_lock(&sd_ref_mutex);
307 sdkp = dev_get_drvdata(dev);
308 if (sdkp)
309 sdkp = __scsi_disk_get(sdkp->disk);
310 mutex_unlock(&sd_ref_mutex);
311 return sdkp;
314 static void scsi_disk_put(struct scsi_disk *sdkp)
316 struct scsi_device *sdev = sdkp->device;
318 mutex_lock(&sd_ref_mutex);
319 class_device_put(&sdkp->cdev);
320 scsi_device_put(sdev);
321 mutex_unlock(&sd_ref_mutex);
325 * sd_init_command - build a scsi (read or write) command from
326 * information in the request structure.
327 * @SCpnt: pointer to mid-level's per scsi command structure that
328 * contains request and into which the scsi command is written
330 * Returns 1 if successful and 0 if error (or cannot be done now).
332 static int sd_init_command(struct scsi_cmnd * SCpnt)
334 struct scsi_device *sdp = SCpnt->device;
335 struct request *rq = SCpnt->request;
336 struct gendisk *disk = rq->rq_disk;
337 sector_t block = rq->sector;
338 unsigned int this_count = SCpnt->request_bufflen >> 9;
339 unsigned int timeout = sdp->timeout;
341 SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
342 "sd_init_command: block=%llu, "
343 "count=%d\n",
344 (unsigned long long)block,
345 this_count));
347 if (!sdp || !scsi_device_online(sdp) ||
348 block + rq->nr_sectors > get_capacity(disk)) {
349 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
350 "Finishing %ld sectors\n",
351 rq->nr_sectors));
352 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
353 "Retry with 0x%p\n", SCpnt));
354 return 0;
357 if (sdp->changed) {
359 * quietly refuse to do anything to a changed disc until
360 * the changed bit has been reset
362 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
363 return 0;
365 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n",
366 (unsigned long long)block));
369 * If we have a 1K hardware sectorsize, prevent access to single
370 * 512 byte sectors. In theory we could handle this - in fact
371 * the scsi cdrom driver must be able to handle this because
372 * we typically use 1K blocksizes, and cdroms typically have
373 * 2K hardware sectorsizes. Of course, things are simpler
374 * with the cdrom, since it is read-only. For performance
375 * reasons, the filesystems should be able to handle this
376 * and not force the scsi disk driver to use bounce buffers
377 * for this.
379 if (sdp->sector_size == 1024) {
380 if ((block & 1) || (rq->nr_sectors & 1)) {
381 scmd_printk(KERN_ERR, SCpnt,
382 "Bad block number requested\n");
383 return 0;
384 } else {
385 block = block >> 1;
386 this_count = this_count >> 1;
389 if (sdp->sector_size == 2048) {
390 if ((block & 3) || (rq->nr_sectors & 3)) {
391 scmd_printk(KERN_ERR, SCpnt,
392 "Bad block number requested\n");
393 return 0;
394 } else {
395 block = block >> 2;
396 this_count = this_count >> 2;
399 if (sdp->sector_size == 4096) {
400 if ((block & 7) || (rq->nr_sectors & 7)) {
401 scmd_printk(KERN_ERR, SCpnt,
402 "Bad block number requested\n");
403 return 0;
404 } else {
405 block = block >> 3;
406 this_count = this_count >> 3;
409 if (rq_data_dir(rq) == WRITE) {
410 if (!sdp->writeable) {
411 return 0;
413 SCpnt->cmnd[0] = WRITE_6;
414 SCpnt->sc_data_direction = DMA_TO_DEVICE;
415 } else if (rq_data_dir(rq) == READ) {
416 SCpnt->cmnd[0] = READ_6;
417 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
418 } else {
419 scmd_printk(KERN_ERR, SCpnt, "Unknown command %x\n", rq->cmd_flags);
420 return 0;
423 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
424 "%s %d/%ld 512 byte blocks.\n",
425 (rq_data_dir(rq) == WRITE) ?
426 "writing" : "reading", this_count,
427 rq->nr_sectors));
429 SCpnt->cmnd[1] = 0;
431 if (block > 0xffffffff) {
432 SCpnt->cmnd[0] += READ_16 - READ_6;
433 SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
434 SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
435 SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
436 SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
437 SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
438 SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
439 SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
440 SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
441 SCpnt->cmnd[9] = (unsigned char) block & 0xff;
442 SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
443 SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
444 SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
445 SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
446 SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
447 } else if ((this_count > 0xff) || (block > 0x1fffff) ||
448 SCpnt->device->use_10_for_rw) {
449 if (this_count > 0xffff)
450 this_count = 0xffff;
452 SCpnt->cmnd[0] += READ_10 - READ_6;
453 SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
454 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
455 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
456 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
457 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
458 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
459 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
460 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
461 } else {
462 if (unlikely(blk_fua_rq(rq))) {
464 * This happens only if this drive failed
465 * 10byte rw command with ILLEGAL_REQUEST
466 * during operation and thus turned off
467 * use_10_for_rw.
469 scmd_printk(KERN_ERR, SCpnt,
470 "FUA write on READ/WRITE(6) drive\n");
471 return 0;
474 SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
475 SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
476 SCpnt->cmnd[3] = (unsigned char) block & 0xff;
477 SCpnt->cmnd[4] = (unsigned char) this_count;
478 SCpnt->cmnd[5] = 0;
480 SCpnt->request_bufflen = this_count * sdp->sector_size;
483 * We shouldn't disconnect in the middle of a sector, so with a dumb
484 * host adapter, it's safe to assume that we can at least transfer
485 * this many bytes between each connect / disconnect.
487 SCpnt->transfersize = sdp->sector_size;
488 SCpnt->underflow = this_count << 9;
489 SCpnt->allowed = SD_MAX_RETRIES;
490 SCpnt->timeout_per_command = timeout;
493 * This is the completion routine we use. This is matched in terms
494 * of capability to this function.
496 SCpnt->done = sd_rw_intr;
499 * This indicates that the command is ready from our end to be
500 * queued.
502 return 1;
506 * sd_open - open a scsi disk device
507 * @inode: only i_rdev member may be used
508 * @filp: only f_mode and f_flags may be used
510 * Returns 0 if successful. Returns a negated errno value in case
511 * of error.
513 * Note: This can be called from a user context (e.g. fsck(1) )
514 * or from within the kernel (e.g. as a result of a mount(1) ).
515 * In the latter case @inode and @filp carry an abridged amount
516 * of information as noted above.
518 static int sd_open(struct inode *inode, struct file *filp)
520 struct gendisk *disk = inode->i_bdev->bd_disk;
521 struct scsi_disk *sdkp;
522 struct scsi_device *sdev;
523 int retval;
525 if (!(sdkp = scsi_disk_get(disk)))
526 return -ENXIO;
529 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
531 sdev = sdkp->device;
534 * If the device is in error recovery, wait until it is done.
535 * If the device is offline, then disallow any access to it.
537 retval = -ENXIO;
538 if (!scsi_block_when_processing_errors(sdev))
539 goto error_out;
541 if (sdev->removable || sdkp->write_prot)
542 check_disk_change(inode->i_bdev);
545 * If the drive is empty, just let the open fail.
547 retval = -ENOMEDIUM;
548 if (sdev->removable && !sdkp->media_present &&
549 !(filp->f_flags & O_NDELAY))
550 goto error_out;
553 * If the device has the write protect tab set, have the open fail
554 * if the user expects to be able to write to the thing.
556 retval = -EROFS;
557 if (sdkp->write_prot && (filp->f_mode & FMODE_WRITE))
558 goto error_out;
561 * It is possible that the disk changing stuff resulted in
562 * the device being taken offline. If this is the case,
563 * report this to the user, and don't pretend that the
564 * open actually succeeded.
566 retval = -ENXIO;
567 if (!scsi_device_online(sdev))
568 goto error_out;
570 if (!sdkp->openers++ && sdev->removable) {
571 if (scsi_block_when_processing_errors(sdev))
572 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
575 return 0;
577 error_out:
578 scsi_disk_put(sdkp);
579 return retval;
583 * sd_release - invoked when the (last) close(2) is called on this
584 * scsi disk.
585 * @inode: only i_rdev member may be used
586 * @filp: only f_mode and f_flags may be used
588 * Returns 0.
590 * Note: may block (uninterruptible) if error recovery is underway
591 * on this disk.
593 static int sd_release(struct inode *inode, struct file *filp)
595 struct gendisk *disk = inode->i_bdev->bd_disk;
596 struct scsi_disk *sdkp = scsi_disk(disk);
597 struct scsi_device *sdev = sdkp->device;
599 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
601 if (!--sdkp->openers && sdev->removable) {
602 if (scsi_block_when_processing_errors(sdev))
603 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
607 * XXX and what if there are packets in flight and this close()
608 * XXX is followed by a "rmmod sd_mod"?
610 scsi_disk_put(sdkp);
611 return 0;
614 static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
616 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
617 struct scsi_device *sdp = sdkp->device;
618 struct Scsi_Host *host = sdp->host;
619 int diskinfo[4];
621 /* default to most commonly used values */
622 diskinfo[0] = 0x40; /* 1 << 6 */
623 diskinfo[1] = 0x20; /* 1 << 5 */
624 diskinfo[2] = sdkp->capacity >> 11;
626 /* override with calculated, extended default, or driver values */
627 if (host->hostt->bios_param)
628 host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo);
629 else
630 scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
632 geo->heads = diskinfo[0];
633 geo->sectors = diskinfo[1];
634 geo->cylinders = diskinfo[2];
635 return 0;
639 * sd_ioctl - process an ioctl
640 * @inode: only i_rdev/i_bdev members may be used
641 * @filp: only f_mode and f_flags may be used
642 * @cmd: ioctl command number
643 * @arg: this is third argument given to ioctl(2) system call.
644 * Often contains a pointer.
646 * Returns 0 if successful (some ioctls return postive numbers on
647 * success as well). Returns a negated errno value in case of error.
649 * Note: most ioctls are forward onto the block subsystem or further
650 * down in the scsi subsytem.
652 static int sd_ioctl(struct inode * inode, struct file * filp,
653 unsigned int cmd, unsigned long arg)
655 struct block_device *bdev = inode->i_bdev;
656 struct gendisk *disk = bdev->bd_disk;
657 struct scsi_device *sdp = scsi_disk(disk)->device;
658 void __user *p = (void __user *)arg;
659 int error;
661 SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
662 disk->disk_name, cmd));
665 * If we are in the middle of error recovery, don't let anyone
666 * else try and use this device. Also, if error recovery fails, it
667 * may try and take the device offline, in which case all further
668 * access to the device is prohibited.
670 error = scsi_nonblockable_ioctl(sdp, cmd, p, filp);
671 if (!scsi_block_when_processing_errors(sdp) || !error)
672 return error;
675 * Send SCSI addressing ioctls directly to mid level, send other
676 * ioctls to block level and then onto mid level if they can't be
677 * resolved.
679 switch (cmd) {
680 case SCSI_IOCTL_GET_IDLUN:
681 case SCSI_IOCTL_GET_BUS_NUMBER:
682 return scsi_ioctl(sdp, cmd, p);
683 default:
684 error = scsi_cmd_ioctl(filp, disk, cmd, p);
685 if (error != -ENOTTY)
686 return error;
688 return scsi_ioctl(sdp, cmd, p);
691 static void set_media_not_present(struct scsi_disk *sdkp)
693 sdkp->media_present = 0;
694 sdkp->capacity = 0;
695 sdkp->device->changed = 1;
699 * sd_media_changed - check if our medium changed
700 * @disk: kernel device descriptor
702 * Returns 0 if not applicable or no change; 1 if change
704 * Note: this function is invoked from the block subsystem.
706 static int sd_media_changed(struct gendisk *disk)
708 struct scsi_disk *sdkp = scsi_disk(disk);
709 struct scsi_device *sdp = sdkp->device;
710 int retval;
712 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_media_changed\n"));
714 if (!sdp->removable)
715 return 0;
718 * If the device is offline, don't send any commands - just pretend as
719 * if the command failed. If the device ever comes back online, we
720 * can deal with it then. It is only because of unrecoverable errors
721 * that we would ever take a device offline in the first place.
723 if (!scsi_device_online(sdp))
724 goto not_present;
727 * Using TEST_UNIT_READY enables differentiation between drive with
728 * no cartridge loaded - NOT READY, drive with changed cartridge -
729 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
731 * Drives that auto spin down. eg iomega jaz 1G, will be started
732 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
733 * sd_revalidate() is called.
735 retval = -ENODEV;
736 if (scsi_block_when_processing_errors(sdp))
737 retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES);
740 * Unable to test, unit probably not ready. This usually
741 * means there is no disc in the drive. Mark as changed,
742 * and we will figure it out later once the drive is
743 * available again.
745 if (retval)
746 goto not_present;
749 * For removable scsi disk we have to recognise the presence
750 * of a disk in the drive. This is kept in the struct scsi_disk
751 * struct and tested at open ! Daniel Roche (dan@lectra.fr)
753 sdkp->media_present = 1;
755 retval = sdp->changed;
756 sdp->changed = 0;
758 return retval;
760 not_present:
761 set_media_not_present(sdkp);
762 return 1;
765 static int sd_sync_cache(struct scsi_disk *sdkp)
767 int retries, res;
768 struct scsi_device *sdp = sdkp->device;
769 struct scsi_sense_hdr sshdr;
771 if (!scsi_device_online(sdp))
772 return -ENODEV;
775 for (retries = 3; retries > 0; --retries) {
776 unsigned char cmd[10] = { 0 };
778 cmd[0] = SYNCHRONIZE_CACHE;
780 * Leave the rest of the command zero to indicate
781 * flush everything.
783 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
784 SD_TIMEOUT, SD_MAX_RETRIES);
785 if (res == 0)
786 break;
789 if (res) {
790 sd_print_result(sdkp, res);
791 if (driver_byte(res) & DRIVER_SENSE)
792 sd_print_sense_hdr(sdkp, &sshdr);
795 if (res)
796 return -EIO;
797 return 0;
800 static int sd_issue_flush(struct device *dev, sector_t *error_sector)
802 int ret = 0;
803 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
805 if (!sdkp)
806 return -ENODEV;
808 if (sdkp->WCE)
809 ret = sd_sync_cache(sdkp);
810 scsi_disk_put(sdkp);
811 return ret;
814 static void sd_prepare_flush(request_queue_t *q, struct request *rq)
816 memset(rq->cmd, 0, sizeof(rq->cmd));
817 rq->cmd_type = REQ_TYPE_BLOCK_PC;
818 rq->timeout = SD_TIMEOUT;
819 rq->cmd[0] = SYNCHRONIZE_CACHE;
820 rq->cmd_len = 10;
823 static void sd_rescan(struct device *dev)
825 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
827 if (sdkp) {
828 sd_revalidate_disk(sdkp->disk);
829 scsi_disk_put(sdkp);
834 #ifdef CONFIG_COMPAT
836 * This gets directly called from VFS. When the ioctl
837 * is not recognized we go back to the other translation paths.
839 static long sd_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
841 struct block_device *bdev = file->f_path.dentry->d_inode->i_bdev;
842 struct gendisk *disk = bdev->bd_disk;
843 struct scsi_device *sdev = scsi_disk(disk)->device;
846 * If we are in the middle of error recovery, don't let anyone
847 * else try and use this device. Also, if error recovery fails, it
848 * may try and take the device offline, in which case all further
849 * access to the device is prohibited.
851 if (!scsi_block_when_processing_errors(sdev))
852 return -ENODEV;
854 if (sdev->host->hostt->compat_ioctl) {
855 int ret;
857 ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
859 return ret;
863 * Let the static ioctl translation table take care of it.
865 return -ENOIOCTLCMD;
867 #endif
869 static struct block_device_operations sd_fops = {
870 .owner = THIS_MODULE,
871 .open = sd_open,
872 .release = sd_release,
873 .ioctl = sd_ioctl,
874 .getgeo = sd_getgeo,
875 #ifdef CONFIG_COMPAT
876 .compat_ioctl = sd_compat_ioctl,
877 #endif
878 .media_changed = sd_media_changed,
879 .revalidate_disk = sd_revalidate_disk,
883 * sd_rw_intr - bottom half handler: called when the lower level
884 * driver has completed (successfully or otherwise) a scsi command.
885 * @SCpnt: mid-level's per command structure.
887 * Note: potentially run from within an ISR. Must not block.
889 static void sd_rw_intr(struct scsi_cmnd * SCpnt)
891 int result = SCpnt->result;
892 unsigned int xfer_size = SCpnt->request_bufflen;
893 unsigned int good_bytes = result ? 0 : xfer_size;
894 u64 start_lba = SCpnt->request->sector;
895 u64 bad_lba;
896 struct scsi_sense_hdr sshdr;
897 int sense_valid = 0;
898 int sense_deferred = 0;
899 int info_valid;
901 if (result) {
902 sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
903 if (sense_valid)
904 sense_deferred = scsi_sense_is_deferred(&sshdr);
906 #ifdef CONFIG_SCSI_LOGGING
907 SCSI_LOG_HLCOMPLETE(1, scsi_print_result(SCpnt));
908 if (sense_valid) {
909 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
910 "sd_rw_intr: sb[respc,sk,asc,"
911 "ascq]=%x,%x,%x,%x\n",
912 sshdr.response_code,
913 sshdr.sense_key, sshdr.asc,
914 sshdr.ascq));
916 #endif
917 if (driver_byte(result) != DRIVER_SENSE &&
918 (!sense_valid || sense_deferred))
919 goto out;
921 switch (sshdr.sense_key) {
922 case HARDWARE_ERROR:
923 case MEDIUM_ERROR:
924 if (!blk_fs_request(SCpnt->request))
925 goto out;
926 info_valid = scsi_get_sense_info_fld(SCpnt->sense_buffer,
927 SCSI_SENSE_BUFFERSIZE,
928 &bad_lba);
929 if (!info_valid)
930 goto out;
931 if (xfer_size <= SCpnt->device->sector_size)
932 goto out;
933 switch (SCpnt->device->sector_size) {
934 case 256:
935 start_lba <<= 1;
936 break;
937 case 512:
938 break;
939 case 1024:
940 start_lba >>= 1;
941 break;
942 case 2048:
943 start_lba >>= 2;
944 break;
945 case 4096:
946 start_lba >>= 3;
947 break;
948 default:
949 /* Print something here with limiting frequency. */
950 goto out;
951 break;
953 /* This computation should always be done in terms of
954 * the resolution of the device's medium.
956 good_bytes = (bad_lba - start_lba)*SCpnt->device->sector_size;
957 break;
958 case RECOVERED_ERROR:
959 case NO_SENSE:
960 /* Inform the user, but make sure that it's not treated
961 * as a hard error.
963 scsi_print_sense("sd", SCpnt);
964 SCpnt->result = 0;
965 memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
966 good_bytes = xfer_size;
967 break;
968 case ILLEGAL_REQUEST:
969 if (SCpnt->device->use_10_for_rw &&
970 (SCpnt->cmnd[0] == READ_10 ||
971 SCpnt->cmnd[0] == WRITE_10))
972 SCpnt->device->use_10_for_rw = 0;
973 if (SCpnt->device->use_10_for_ms &&
974 (SCpnt->cmnd[0] == MODE_SENSE_10 ||
975 SCpnt->cmnd[0] == MODE_SELECT_10))
976 SCpnt->device->use_10_for_ms = 0;
977 break;
978 default:
979 break;
981 out:
982 scsi_io_completion(SCpnt, good_bytes);
985 static int media_not_present(struct scsi_disk *sdkp,
986 struct scsi_sense_hdr *sshdr)
989 if (!scsi_sense_valid(sshdr))
990 return 0;
991 /* not invoked for commands that could return deferred errors */
992 if (sshdr->sense_key != NOT_READY &&
993 sshdr->sense_key != UNIT_ATTENTION)
994 return 0;
995 if (sshdr->asc != 0x3A) /* medium not present */
996 return 0;
998 set_media_not_present(sdkp);
999 return 1;
1003 * spinup disk - called only in sd_revalidate_disk()
1005 static void
1006 sd_spinup_disk(struct scsi_disk *sdkp)
1008 unsigned char cmd[10];
1009 unsigned long spintime_expire = 0;
1010 int retries, spintime;
1011 unsigned int the_result;
1012 struct scsi_sense_hdr sshdr;
1013 int sense_valid = 0;
1015 spintime = 0;
1017 /* Spin up drives, as required. Only do this at boot time */
1018 /* Spinup needs to be done for module loads too. */
1019 do {
1020 retries = 0;
1022 do {
1023 cmd[0] = TEST_UNIT_READY;
1024 memset((void *) &cmd[1], 0, 9);
1026 the_result = scsi_execute_req(sdkp->device, cmd,
1027 DMA_NONE, NULL, 0,
1028 &sshdr, SD_TIMEOUT,
1029 SD_MAX_RETRIES);
1032 * If the drive has indicated to us that it
1033 * doesn't have any media in it, don't bother
1034 * with any more polling.
1036 if (media_not_present(sdkp, &sshdr))
1037 return;
1039 if (the_result)
1040 sense_valid = scsi_sense_valid(&sshdr);
1041 retries++;
1042 } while (retries < 3 &&
1043 (!scsi_status_is_good(the_result) ||
1044 ((driver_byte(the_result) & DRIVER_SENSE) &&
1045 sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
1047 if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
1048 /* no sense, TUR either succeeded or failed
1049 * with a status error */
1050 if(!spintime && !scsi_status_is_good(the_result)) {
1051 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1052 sd_print_result(sdkp, the_result);
1054 break;
1058 * The device does not want the automatic start to be issued.
1060 if (sdkp->device->no_start_on_add) {
1061 break;
1065 * If manual intervention is required, or this is an
1066 * absent USB storage device, a spinup is meaningless.
1068 if (sense_valid &&
1069 sshdr.sense_key == NOT_READY &&
1070 sshdr.asc == 4 && sshdr.ascq == 3) {
1071 break; /* manual intervention required */
1074 * Issue command to spin up drive when not ready
1076 } else if (sense_valid && sshdr.sense_key == NOT_READY) {
1077 if (!spintime) {
1078 sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
1079 cmd[0] = START_STOP;
1080 cmd[1] = 1; /* Return immediately */
1081 memset((void *) &cmd[2], 0, 8);
1082 cmd[4] = 1; /* Start spin cycle */
1083 scsi_execute_req(sdkp->device, cmd, DMA_NONE,
1084 NULL, 0, &sshdr,
1085 SD_TIMEOUT, SD_MAX_RETRIES);
1086 spintime_expire = jiffies + 100 * HZ;
1087 spintime = 1;
1089 /* Wait 1 second for next try */
1090 msleep(1000);
1091 printk(".");
1094 * Wait for USB flash devices with slow firmware.
1095 * Yes, this sense key/ASC combination shouldn't
1096 * occur here. It's characteristic of these devices.
1098 } else if (sense_valid &&
1099 sshdr.sense_key == UNIT_ATTENTION &&
1100 sshdr.asc == 0x28) {
1101 if (!spintime) {
1102 spintime_expire = jiffies + 5 * HZ;
1103 spintime = 1;
1105 /* Wait 1 second for next try */
1106 msleep(1000);
1107 } else {
1108 /* we don't understand the sense code, so it's
1109 * probably pointless to loop */
1110 if(!spintime) {
1111 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1112 sd_print_sense_hdr(sdkp, &sshdr);
1114 break;
1117 } while (spintime && time_before_eq(jiffies, spintime_expire));
1119 if (spintime) {
1120 if (scsi_status_is_good(the_result))
1121 printk("ready\n");
1122 else
1123 printk("not responding...\n");
1128 * read disk capacity
1130 static void
1131 sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
1133 unsigned char cmd[16];
1134 int the_result, retries;
1135 int sector_size = 0;
1136 int longrc = 0;
1137 struct scsi_sense_hdr sshdr;
1138 int sense_valid = 0;
1139 struct scsi_device *sdp = sdkp->device;
1141 repeat:
1142 retries = 3;
1143 do {
1144 if (longrc) {
1145 memset((void *) cmd, 0, 16);
1146 cmd[0] = SERVICE_ACTION_IN;
1147 cmd[1] = SAI_READ_CAPACITY_16;
1148 cmd[13] = 12;
1149 memset((void *) buffer, 0, 12);
1150 } else {
1151 cmd[0] = READ_CAPACITY;
1152 memset((void *) &cmd[1], 0, 9);
1153 memset((void *) buffer, 0, 8);
1156 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
1157 buffer, longrc ? 12 : 8, &sshdr,
1158 SD_TIMEOUT, SD_MAX_RETRIES);
1160 if (media_not_present(sdkp, &sshdr))
1161 return;
1163 if (the_result)
1164 sense_valid = scsi_sense_valid(&sshdr);
1165 retries--;
1167 } while (the_result && retries);
1169 if (the_result && !longrc) {
1170 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY failed\n");
1171 sd_print_result(sdkp, the_result);
1172 if (driver_byte(the_result) & DRIVER_SENSE)
1173 sd_print_sense_hdr(sdkp, &sshdr);
1174 else
1175 sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
1177 /* Set dirty bit for removable devices if not ready -
1178 * sometimes drives will not report this properly. */
1179 if (sdp->removable &&
1180 sense_valid && sshdr.sense_key == NOT_READY)
1181 sdp->changed = 1;
1183 /* Either no media are present but the drive didn't tell us,
1184 or they are present but the read capacity command fails */
1185 /* sdkp->media_present = 0; -- not always correct */
1186 sdkp->capacity = 0; /* unknown mapped to zero - as usual */
1188 return;
1189 } else if (the_result && longrc) {
1190 /* READ CAPACITY(16) has been failed */
1191 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY(16) failed\n");
1192 sd_print_result(sdkp, the_result);
1193 sd_printk(KERN_NOTICE, sdkp, "Use 0xffffffff as device size\n");
1195 sdkp->capacity = 1 + (sector_t) 0xffffffff;
1196 goto got_data;
1199 if (!longrc) {
1200 sector_size = (buffer[4] << 24) |
1201 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
1202 if (buffer[0] == 0xff && buffer[1] == 0xff &&
1203 buffer[2] == 0xff && buffer[3] == 0xff) {
1204 if(sizeof(sdkp->capacity) > 4) {
1205 sd_printk(KERN_NOTICE, sdkp, "Very big device. "
1206 "Trying to use READ CAPACITY(16).\n");
1207 longrc = 1;
1208 goto repeat;
1210 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use "
1211 "a kernel compiled with support for large "
1212 "block devices.\n");
1213 sdkp->capacity = 0;
1214 goto got_data;
1216 sdkp->capacity = 1 + (((sector_t)buffer[0] << 24) |
1217 (buffer[1] << 16) |
1218 (buffer[2] << 8) |
1219 buffer[3]);
1220 } else {
1221 sdkp->capacity = 1 + (((u64)buffer[0] << 56) |
1222 ((u64)buffer[1] << 48) |
1223 ((u64)buffer[2] << 40) |
1224 ((u64)buffer[3] << 32) |
1225 ((sector_t)buffer[4] << 24) |
1226 ((sector_t)buffer[5] << 16) |
1227 ((sector_t)buffer[6] << 8) |
1228 (sector_t)buffer[7]);
1230 sector_size = (buffer[8] << 24) |
1231 (buffer[9] << 16) | (buffer[10] << 8) | buffer[11];
1234 /* Some devices return the total number of sectors, not the
1235 * highest sector number. Make the necessary adjustment. */
1236 if (sdp->fix_capacity) {
1237 --sdkp->capacity;
1239 /* Some devices have version which report the correct sizes
1240 * and others which do not. We guess size according to a heuristic
1241 * and err on the side of lowering the capacity. */
1242 } else {
1243 if (sdp->guess_capacity)
1244 if (sdkp->capacity & 0x01) /* odd sizes are odd */
1245 --sdkp->capacity;
1248 got_data:
1249 if (sector_size == 0) {
1250 sector_size = 512;
1251 sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
1252 "assuming 512.\n");
1255 if (sector_size != 512 &&
1256 sector_size != 1024 &&
1257 sector_size != 2048 &&
1258 sector_size != 4096 &&
1259 sector_size != 256) {
1260 sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
1261 sector_size);
1263 * The user might want to re-format the drive with
1264 * a supported sectorsize. Once this happens, it
1265 * would be relatively trivial to set the thing up.
1266 * For this reason, we leave the thing in the table.
1268 sdkp->capacity = 0;
1270 * set a bogus sector size so the normal read/write
1271 * logic in the block layer will eventually refuse any
1272 * request on this device without tripping over power
1273 * of two sector size assumptions
1275 sector_size = 512;
1279 * The msdos fs needs to know the hardware sector size
1280 * So I have created this table. See ll_rw_blk.c
1281 * Jacques Gelinas (Jacques@solucorp.qc.ca)
1283 int hard_sector = sector_size;
1284 sector_t sz = (sdkp->capacity/2) * (hard_sector/256);
1285 request_queue_t *queue = sdp->request_queue;
1286 sector_t mb = sz;
1288 blk_queue_hardsect_size(queue, hard_sector);
1289 /* avoid 64-bit division on 32-bit platforms */
1290 sector_div(sz, 625);
1291 mb -= sz - 974;
1292 sector_div(mb, 1950);
1294 sd_printk(KERN_NOTICE, sdkp,
1295 "%llu %d-byte hardware sectors (%llu MB)\n",
1296 (unsigned long long)sdkp->capacity,
1297 hard_sector, (unsigned long long)mb);
1300 /* Rescale capacity to 512-byte units */
1301 if (sector_size == 4096)
1302 sdkp->capacity <<= 3;
1303 else if (sector_size == 2048)
1304 sdkp->capacity <<= 2;
1305 else if (sector_size == 1024)
1306 sdkp->capacity <<= 1;
1307 else if (sector_size == 256)
1308 sdkp->capacity >>= 1;
1310 sdkp->device->sector_size = sector_size;
1313 /* called with buffer of length 512 */
1314 static inline int
1315 sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
1316 unsigned char *buffer, int len, struct scsi_mode_data *data,
1317 struct scsi_sense_hdr *sshdr)
1319 return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
1320 SD_TIMEOUT, SD_MAX_RETRIES, data,
1321 sshdr);
1325 * read write protect setting, if possible - called only in sd_revalidate_disk()
1326 * called with buffer of length SD_BUF_SIZE
1328 static void
1329 sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
1331 int res;
1332 struct scsi_device *sdp = sdkp->device;
1333 struct scsi_mode_data data;
1335 set_disk_ro(sdkp->disk, 0);
1336 if (sdp->skip_ms_page_3f) {
1337 sd_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
1338 return;
1341 if (sdp->use_192_bytes_for_3f) {
1342 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
1343 } else {
1345 * First attempt: ask for all pages (0x3F), but only 4 bytes.
1346 * We have to start carefully: some devices hang if we ask
1347 * for more than is available.
1349 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
1352 * Second attempt: ask for page 0 When only page 0 is
1353 * implemented, a request for page 3F may return Sense Key
1354 * 5: Illegal Request, Sense Code 24: Invalid field in
1355 * CDB.
1357 if (!scsi_status_is_good(res))
1358 res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
1361 * Third attempt: ask 255 bytes, as we did earlier.
1363 if (!scsi_status_is_good(res))
1364 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
1365 &data, NULL);
1368 if (!scsi_status_is_good(res)) {
1369 sd_printk(KERN_WARNING, sdkp,
1370 "Test WP failed, assume Write Enabled\n");
1371 } else {
1372 sdkp->write_prot = ((data.device_specific & 0x80) != 0);
1373 set_disk_ro(sdkp->disk, sdkp->write_prot);
1374 sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
1375 sdkp->write_prot ? "on" : "off");
1376 sd_printk(KERN_DEBUG, sdkp,
1377 "Mode Sense: %02x %02x %02x %02x\n",
1378 buffer[0], buffer[1], buffer[2], buffer[3]);
1383 * sd_read_cache_type - called only from sd_revalidate_disk()
1384 * called with buffer of length SD_BUF_SIZE
1386 static void
1387 sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
1389 int len = 0, res;
1390 struct scsi_device *sdp = sdkp->device;
1392 int dbd;
1393 int modepage;
1394 struct scsi_mode_data data;
1395 struct scsi_sense_hdr sshdr;
1397 if (sdp->skip_ms_page_8)
1398 goto defaults;
1400 if (sdp->type == TYPE_RBC) {
1401 modepage = 6;
1402 dbd = 8;
1403 } else {
1404 modepage = 8;
1405 dbd = 0;
1408 /* cautiously ask */
1409 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr);
1411 if (!scsi_status_is_good(res))
1412 goto bad_sense;
1414 if (!data.header_length) {
1415 modepage = 6;
1416 sd_printk(KERN_ERR, sdkp, "Missing header in MODE_SENSE response\n");
1419 /* that went OK, now ask for the proper length */
1420 len = data.length;
1423 * We're only interested in the first three bytes, actually.
1424 * But the data cache page is defined for the first 20.
1426 if (len < 3)
1427 goto bad_sense;
1428 if (len > 20)
1429 len = 20;
1431 /* Take headers and block descriptors into account */
1432 len += data.header_length + data.block_descriptor_length;
1433 if (len > SD_BUF_SIZE)
1434 goto bad_sense;
1436 /* Get the data */
1437 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr);
1439 if (scsi_status_is_good(res)) {
1440 int offset = data.header_length + data.block_descriptor_length;
1442 if (offset >= SD_BUF_SIZE - 2) {
1443 sd_printk(KERN_ERR, sdkp, "Malformed MODE SENSE response\n");
1444 goto defaults;
1447 if ((buffer[offset] & 0x3f) != modepage) {
1448 sd_printk(KERN_ERR, sdkp, "Got wrong page\n");
1449 goto defaults;
1452 if (modepage == 8) {
1453 sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
1454 sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
1455 } else {
1456 sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
1457 sdkp->RCD = 0;
1460 sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
1461 if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
1462 sd_printk(KERN_NOTICE, sdkp,
1463 "Uses READ/WRITE(6), disabling FUA\n");
1464 sdkp->DPOFUA = 0;
1467 sd_printk(KERN_NOTICE, sdkp,
1468 "Write cache: %s, read cache: %s, %s\n",
1469 sdkp->WCE ? "enabled" : "disabled",
1470 sdkp->RCD ? "disabled" : "enabled",
1471 sdkp->DPOFUA ? "supports DPO and FUA"
1472 : "doesn't support DPO or FUA");
1474 return;
1477 bad_sense:
1478 if (scsi_sense_valid(&sshdr) &&
1479 sshdr.sense_key == ILLEGAL_REQUEST &&
1480 sshdr.asc == 0x24 && sshdr.ascq == 0x0)
1481 /* Invalid field in CDB */
1482 sd_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
1483 else
1484 sd_printk(KERN_ERR, sdkp, "Asking for cache data failed\n");
1486 defaults:
1487 sd_printk(KERN_ERR, sdkp, "Assuming drive cache: write through\n");
1488 sdkp->WCE = 0;
1489 sdkp->RCD = 0;
1490 sdkp->DPOFUA = 0;
1494 * sd_revalidate_disk - called the first time a new disk is seen,
1495 * performs disk spin up, read_capacity, etc.
1496 * @disk: struct gendisk we care about
1498 static int sd_revalidate_disk(struct gendisk *disk)
1500 struct scsi_disk *sdkp = scsi_disk(disk);
1501 struct scsi_device *sdp = sdkp->device;
1502 unsigned char *buffer;
1503 unsigned ordered;
1505 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
1506 "sd_revalidate_disk\n"));
1509 * If the device is offline, don't try and read capacity or any
1510 * of the other niceties.
1512 if (!scsi_device_online(sdp))
1513 goto out;
1515 buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL | __GFP_DMA);
1516 if (!buffer) {
1517 sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
1518 "allocation failure.\n");
1519 goto out;
1522 /* defaults, until the device tells us otherwise */
1523 sdp->sector_size = 512;
1524 sdkp->capacity = 0;
1525 sdkp->media_present = 1;
1526 sdkp->write_prot = 0;
1527 sdkp->WCE = 0;
1528 sdkp->RCD = 0;
1530 sd_spinup_disk(sdkp);
1533 * Without media there is no reason to ask; moreover, some devices
1534 * react badly if we do.
1536 if (sdkp->media_present) {
1537 sd_read_capacity(sdkp, buffer);
1538 sd_read_write_protect_flag(sdkp, buffer);
1539 sd_read_cache_type(sdkp, buffer);
1543 * We now have all cache related info, determine how we deal
1544 * with ordered requests. Note that as the current SCSI
1545 * dispatch function can alter request order, we cannot use
1546 * QUEUE_ORDERED_TAG_* even when ordered tag is supported.
1548 if (sdkp->WCE)
1549 ordered = sdkp->DPOFUA
1550 ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH;
1551 else
1552 ordered = QUEUE_ORDERED_DRAIN;
1554 blk_queue_ordered(sdkp->disk->queue, ordered, sd_prepare_flush);
1556 set_capacity(disk, sdkp->capacity);
1557 kfree(buffer);
1559 out:
1560 return 0;
1564 * sd_probe - called during driver initialization and whenever a
1565 * new scsi device is attached to the system. It is called once
1566 * for each scsi device (not just disks) present.
1567 * @dev: pointer to device object
1569 * Returns 0 if successful (or not interested in this scsi device
1570 * (e.g. scanner)); 1 when there is an error.
1572 * Note: this function is invoked from the scsi mid-level.
1573 * This function sets up the mapping between a given
1574 * <host,channel,id,lun> (found in sdp) and new device name
1575 * (e.g. /dev/sda). More precisely it is the block device major
1576 * and minor number that is chosen here.
1578 * Assume sd_attach is not re-entrant (for time being)
1579 * Also think about sd_attach() and sd_remove() running coincidentally.
1581 static int sd_probe(struct device *dev)
1583 struct scsi_device *sdp = to_scsi_device(dev);
1584 struct scsi_disk *sdkp;
1585 struct gendisk *gd;
1586 u32 index;
1587 int error;
1589 error = -ENODEV;
1590 if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
1591 goto out;
1593 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
1594 "sd_attach\n"));
1596 error = -ENOMEM;
1597 sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
1598 if (!sdkp)
1599 goto out;
1601 gd = alloc_disk(16);
1602 if (!gd)
1603 goto out_free;
1605 if (!idr_pre_get(&sd_index_idr, GFP_KERNEL))
1606 goto out_put;
1608 spin_lock(&sd_index_lock);
1609 error = idr_get_new(&sd_index_idr, NULL, &index);
1610 spin_unlock(&sd_index_lock);
1612 if (index >= SD_MAX_DISKS)
1613 error = -EBUSY;
1614 if (error)
1615 goto out_put;
1617 sdkp->device = sdp;
1618 sdkp->driver = &sd_template;
1619 sdkp->disk = gd;
1620 sdkp->index = index;
1621 sdkp->openers = 0;
1623 if (!sdp->timeout) {
1624 if (sdp->type != TYPE_MOD)
1625 sdp->timeout = SD_TIMEOUT;
1626 else
1627 sdp->timeout = SD_MOD_TIMEOUT;
1630 class_device_initialize(&sdkp->cdev);
1631 sdkp->cdev.dev = &sdp->sdev_gendev;
1632 sdkp->cdev.class = &sd_disk_class;
1633 strncpy(sdkp->cdev.class_id, sdp->sdev_gendev.bus_id, BUS_ID_SIZE);
1635 if (class_device_add(&sdkp->cdev))
1636 goto out_put;
1638 get_device(&sdp->sdev_gendev);
1640 gd->major = sd_major((index & 0xf0) >> 4);
1641 gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
1642 gd->minors = 16;
1643 gd->fops = &sd_fops;
1645 if (index < 26) {
1646 sprintf(gd->disk_name, "sd%c", 'a' + index % 26);
1647 } else if (index < (26 + 1) * 26) {
1648 sprintf(gd->disk_name, "sd%c%c",
1649 'a' + index / 26 - 1,'a' + index % 26);
1650 } else {
1651 const unsigned int m1 = (index / 26 - 1) / 26 - 1;
1652 const unsigned int m2 = (index / 26 - 1) % 26;
1653 const unsigned int m3 = index % 26;
1654 sprintf(gd->disk_name, "sd%c%c%c",
1655 'a' + m1, 'a' + m2, 'a' + m3);
1658 gd->private_data = &sdkp->driver;
1659 gd->queue = sdkp->device->request_queue;
1661 sd_revalidate_disk(gd);
1663 gd->driverfs_dev = &sdp->sdev_gendev;
1664 gd->flags = GENHD_FL_DRIVERFS;
1665 if (sdp->removable)
1666 gd->flags |= GENHD_FL_REMOVABLE;
1668 dev_set_drvdata(dev, sdkp);
1669 add_disk(gd);
1671 sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
1672 sdp->removable ? "removable " : "");
1674 return 0;
1676 out_put:
1677 put_disk(gd);
1678 out_free:
1679 kfree(sdkp);
1680 out:
1681 return error;
1685 * sd_remove - called whenever a scsi disk (previously recognized by
1686 * sd_probe) is detached from the system. It is called (potentially
1687 * multiple times) during sd module unload.
1688 * @sdp: pointer to mid level scsi device object
1690 * Note: this function is invoked from the scsi mid-level.
1691 * This function potentially frees up a device name (e.g. /dev/sdc)
1692 * that could be re-used by a subsequent sd_probe().
1693 * This function is not called when the built-in sd driver is "exit-ed".
1695 static int sd_remove(struct device *dev)
1697 struct scsi_disk *sdkp = dev_get_drvdata(dev);
1699 class_device_del(&sdkp->cdev);
1700 del_gendisk(sdkp->disk);
1701 sd_shutdown(dev);
1703 mutex_lock(&sd_ref_mutex);
1704 dev_set_drvdata(dev, NULL);
1705 class_device_put(&sdkp->cdev);
1706 mutex_unlock(&sd_ref_mutex);
1708 return 0;
1712 * scsi_disk_release - Called to free the scsi_disk structure
1713 * @cdev: pointer to embedded class device
1715 * sd_ref_mutex must be held entering this routine. Because it is
1716 * called on last put, you should always use the scsi_disk_get()
1717 * scsi_disk_put() helpers which manipulate the semaphore directly
1718 * and never do a direct class_device_put().
1720 static void scsi_disk_release(struct class_device *cdev)
1722 struct scsi_disk *sdkp = to_scsi_disk(cdev);
1723 struct gendisk *disk = sdkp->disk;
1725 spin_lock(&sd_index_lock);
1726 idr_remove(&sd_index_idr, sdkp->index);
1727 spin_unlock(&sd_index_lock);
1729 disk->private_data = NULL;
1730 put_disk(disk);
1731 put_device(&sdkp->device->sdev_gendev);
1733 kfree(sdkp);
1736 static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
1738 unsigned char cmd[6] = { START_STOP }; /* START_VALID */
1739 struct scsi_sense_hdr sshdr;
1740 struct scsi_device *sdp = sdkp->device;
1741 int res;
1743 if (start)
1744 cmd[4] |= 1; /* START */
1746 if (!scsi_device_online(sdp))
1747 return -ENODEV;
1749 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
1750 SD_TIMEOUT, SD_MAX_RETRIES);
1751 if (res) {
1752 sd_printk(KERN_WARNING, sdkp, "START_STOP FAILED\n");
1753 sd_print_result(sdkp, res);
1754 if (driver_byte(res) & DRIVER_SENSE)
1755 sd_print_sense_hdr(sdkp, &sshdr);
1758 return res;
1762 * Send a SYNCHRONIZE CACHE instruction down to the device through
1763 * the normal SCSI command structure. Wait for the command to
1764 * complete.
1766 static void sd_shutdown(struct device *dev)
1768 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
1770 if (!sdkp)
1771 return; /* this can happen */
1773 if (sdkp->WCE) {
1774 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
1775 sd_sync_cache(sdkp);
1778 if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
1779 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
1780 sd_start_stop_device(sdkp, 0);
1783 scsi_disk_put(sdkp);
1786 static int sd_suspend(struct device *dev, pm_message_t mesg)
1788 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
1789 int ret;
1791 if (!sdkp)
1792 return 0; /* this can happen */
1794 if (sdkp->WCE) {
1795 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
1796 ret = sd_sync_cache(sdkp);
1797 if (ret)
1798 return ret;
1801 if (mesg.event == PM_EVENT_SUSPEND &&
1802 sdkp->device->manage_start_stop) {
1803 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
1804 ret = sd_start_stop_device(sdkp, 0);
1805 if (ret)
1806 return ret;
1809 return 0;
1812 static int sd_resume(struct device *dev)
1814 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
1816 if (!sdkp->device->manage_start_stop)
1817 return 0;
1819 sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
1821 return sd_start_stop_device(sdkp, 1);
1825 * init_sd - entry point for this driver (both when built in or when
1826 * a module).
1828 * Note: this function registers this driver with the scsi mid-level.
1830 static int __init init_sd(void)
1832 int majors = 0, i, err;
1834 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
1836 for (i = 0; i < SD_MAJORS; i++)
1837 if (register_blkdev(sd_major(i), "sd") == 0)
1838 majors++;
1840 if (!majors)
1841 return -ENODEV;
1843 err = class_register(&sd_disk_class);
1844 if (err)
1845 goto err_out;
1847 err = scsi_register_driver(&sd_template.gendrv);
1848 if (err)
1849 goto err_out_class;
1851 return 0;
1853 err_out_class:
1854 class_unregister(&sd_disk_class);
1855 err_out:
1856 for (i = 0; i < SD_MAJORS; i++)
1857 unregister_blkdev(sd_major(i), "sd");
1858 return err;
1862 * exit_sd - exit point for this driver (when it is a module).
1864 * Note: this function unregisters this driver from the scsi mid-level.
1866 static void __exit exit_sd(void)
1868 int i;
1870 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
1872 scsi_unregister_driver(&sd_template.gendrv);
1873 class_unregister(&sd_disk_class);
1875 for (i = 0; i < SD_MAJORS; i++)
1876 unregister_blkdev(sd_major(i), "sd");
1879 module_init(init_sd);
1880 module_exit(exit_sd);
1882 static void sd_print_sense_hdr(struct scsi_disk *sdkp,
1883 struct scsi_sense_hdr *sshdr)
1885 sd_printk(KERN_INFO, sdkp, "");
1886 scsi_show_sense_hdr(sshdr);
1887 sd_printk(KERN_INFO, sdkp, "");
1888 scsi_show_extd_sense(sshdr->asc, sshdr->ascq);
1891 static void sd_print_result(struct scsi_disk *sdkp, int result)
1893 sd_printk(KERN_INFO, sdkp, "");
1894 scsi_show_result(result);