drivers/char/Kconfig: don't mess it up for everyone else
[linux-2.6.32.60-moxart.git] / drivers / scsi / sd.c
bloba5b55fe2f2b539cd1e0c59704ae47679c51f21e1
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 <linux/string_helpers.h>
51 #include <linux/async.h>
52 #include <asm/uaccess.h>
53 #include <asm/unaligned.h>
55 #include <scsi/scsi.h>
56 #include <scsi/scsi_cmnd.h>
57 #include <scsi/scsi_dbg.h>
58 #include <scsi/scsi_device.h>
59 #include <scsi/scsi_driver.h>
60 #include <scsi/scsi_eh.h>
61 #include <scsi/scsi_host.h>
62 #include <scsi/scsi_ioctl.h>
63 #include <scsi/scsicam.h>
65 #include "sd.h"
66 #include "scsi_logging.h"
68 MODULE_AUTHOR("Eric Youngdale");
69 MODULE_DESCRIPTION("SCSI disk (sd) driver");
70 MODULE_LICENSE("GPL");
72 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR);
73 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR);
74 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR);
75 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR);
76 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR);
77 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR);
78 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR);
79 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR);
80 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR);
81 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR);
82 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR);
83 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR);
84 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
85 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
86 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
87 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
88 MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK);
89 MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD);
90 MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC);
92 #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
93 #define SD_MINORS 16
94 #else
95 #define SD_MINORS 0
96 #endif
98 static int sd_revalidate_disk(struct gendisk *);
99 static int sd_probe(struct device *);
100 static int sd_remove(struct device *);
101 static void sd_shutdown(struct device *);
102 static int sd_suspend(struct device *, pm_message_t state);
103 static int sd_resume(struct device *);
104 static void sd_rescan(struct device *);
105 static int sd_done(struct scsi_cmnd *);
106 static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
107 static void scsi_disk_release(struct device *cdev);
108 static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *);
109 static void sd_print_result(struct scsi_disk *, int);
111 static DEFINE_SPINLOCK(sd_index_lock);
112 static DEFINE_IDA(sd_index_ida);
114 /* This semaphore is used to mediate the 0->1 reference get in the
115 * face of object destruction (i.e. we can't allow a get on an
116 * object after last put) */
117 static DEFINE_MUTEX(sd_ref_mutex);
119 struct kmem_cache *sd_cdb_cache;
120 mempool_t *sd_cdb_pool;
122 static const char *sd_cache_types[] = {
123 "write through", "none", "write back",
124 "write back, no read (daft)"
127 static ssize_t
128 sd_store_cache_type(struct device *dev, struct device_attribute *attr,
129 const char *buf, size_t count)
131 int i, ct = -1, rcd, wce, sp;
132 struct scsi_disk *sdkp = to_scsi_disk(dev);
133 struct scsi_device *sdp = sdkp->device;
134 char buffer[64];
135 char *buffer_data;
136 struct scsi_mode_data data;
137 struct scsi_sense_hdr sshdr;
138 int len;
140 if (sdp->type != TYPE_DISK)
141 /* no cache control on RBC devices; theoretically they
142 * can do it, but there's probably so many exceptions
143 * it's not worth the risk */
144 return -EINVAL;
146 for (i = 0; i < ARRAY_SIZE(sd_cache_types); i++) {
147 const int len = strlen(sd_cache_types[i]);
148 if (strncmp(sd_cache_types[i], buf, len) == 0 &&
149 buf[len] == '\n') {
150 ct = i;
151 break;
154 if (ct < 0)
155 return -EINVAL;
156 rcd = ct & 0x01 ? 1 : 0;
157 wce = ct & 0x02 ? 1 : 0;
158 if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT,
159 SD_MAX_RETRIES, &data, NULL))
160 return -EINVAL;
161 len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
162 data.block_descriptor_length);
163 buffer_data = buffer + data.header_length +
164 data.block_descriptor_length;
165 buffer_data[2] &= ~0x05;
166 buffer_data[2] |= wce << 2 | rcd;
167 sp = buffer_data[0] & 0x80 ? 1 : 0;
169 if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT,
170 SD_MAX_RETRIES, &data, &sshdr)) {
171 if (scsi_sense_valid(&sshdr))
172 sd_print_sense_hdr(sdkp, &sshdr);
173 return -EINVAL;
175 revalidate_disk(sdkp->disk);
176 return count;
179 static ssize_t
180 sd_store_manage_start_stop(struct device *dev, struct device_attribute *attr,
181 const char *buf, size_t count)
183 struct scsi_disk *sdkp = to_scsi_disk(dev);
184 struct scsi_device *sdp = sdkp->device;
186 if (!capable(CAP_SYS_ADMIN))
187 return -EACCES;
189 sdp->manage_start_stop = simple_strtoul(buf, NULL, 10);
191 return count;
194 static ssize_t
195 sd_store_allow_restart(struct device *dev, struct device_attribute *attr,
196 const char *buf, size_t count)
198 struct scsi_disk *sdkp = to_scsi_disk(dev);
199 struct scsi_device *sdp = sdkp->device;
201 if (!capable(CAP_SYS_ADMIN))
202 return -EACCES;
204 if (sdp->type != TYPE_DISK)
205 return -EINVAL;
207 sdp->allow_restart = simple_strtoul(buf, NULL, 10);
209 return count;
212 static ssize_t
213 sd_show_cache_type(struct device *dev, struct device_attribute *attr,
214 char *buf)
216 struct scsi_disk *sdkp = to_scsi_disk(dev);
217 int ct = sdkp->RCD + 2*sdkp->WCE;
219 return snprintf(buf, 40, "%s\n", sd_cache_types[ct]);
222 static ssize_t
223 sd_show_fua(struct device *dev, struct device_attribute *attr, char *buf)
225 struct scsi_disk *sdkp = to_scsi_disk(dev);
227 return snprintf(buf, 20, "%u\n", sdkp->DPOFUA);
230 static ssize_t
231 sd_show_manage_start_stop(struct device *dev, struct device_attribute *attr,
232 char *buf)
234 struct scsi_disk *sdkp = to_scsi_disk(dev);
235 struct scsi_device *sdp = sdkp->device;
237 return snprintf(buf, 20, "%u\n", sdp->manage_start_stop);
240 static ssize_t
241 sd_show_allow_restart(struct device *dev, struct device_attribute *attr,
242 char *buf)
244 struct scsi_disk *sdkp = to_scsi_disk(dev);
246 return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart);
249 static ssize_t
250 sd_show_protection_type(struct device *dev, struct device_attribute *attr,
251 char *buf)
253 struct scsi_disk *sdkp = to_scsi_disk(dev);
255 return snprintf(buf, 20, "%u\n", sdkp->protection_type);
258 static ssize_t
259 sd_show_app_tag_own(struct device *dev, struct device_attribute *attr,
260 char *buf)
262 struct scsi_disk *sdkp = to_scsi_disk(dev);
264 return snprintf(buf, 20, "%u\n", sdkp->ATO);
267 static struct device_attribute sd_disk_attrs[] = {
268 __ATTR(cache_type, S_IRUGO|S_IWUSR, sd_show_cache_type,
269 sd_store_cache_type),
270 __ATTR(FUA, S_IRUGO, sd_show_fua, NULL),
271 __ATTR(allow_restart, S_IRUGO|S_IWUSR, sd_show_allow_restart,
272 sd_store_allow_restart),
273 __ATTR(manage_start_stop, S_IRUGO|S_IWUSR, sd_show_manage_start_stop,
274 sd_store_manage_start_stop),
275 __ATTR(protection_type, S_IRUGO, sd_show_protection_type, NULL),
276 __ATTR(app_tag_own, S_IRUGO, sd_show_app_tag_own, NULL),
277 __ATTR_NULL,
280 static struct class sd_disk_class = {
281 .name = "scsi_disk",
282 .owner = THIS_MODULE,
283 .dev_release = scsi_disk_release,
284 .dev_attrs = sd_disk_attrs,
287 static struct scsi_driver sd_template = {
288 .owner = THIS_MODULE,
289 .gendrv = {
290 .name = "sd",
291 .probe = sd_probe,
292 .remove = sd_remove,
293 .suspend = sd_suspend,
294 .resume = sd_resume,
295 .shutdown = sd_shutdown,
297 .rescan = sd_rescan,
298 .done = sd_done,
302 * Device no to disk mapping:
304 * major disc2 disc p1
305 * |............|.............|....|....| <- dev_t
306 * 31 20 19 8 7 4 3 0
308 * Inside a major, we have 16k disks, however mapped non-
309 * contiguously. The first 16 disks are for major0, the next
310 * ones with major1, ... Disk 256 is for major0 again, disk 272
311 * for major1, ...
312 * As we stay compatible with our numbering scheme, we can reuse
313 * the well-know SCSI majors 8, 65--71, 136--143.
315 static int sd_major(int major_idx)
317 switch (major_idx) {
318 case 0:
319 return SCSI_DISK0_MAJOR;
320 case 1 ... 7:
321 return SCSI_DISK1_MAJOR + major_idx - 1;
322 case 8 ... 15:
323 return SCSI_DISK8_MAJOR + major_idx - 8;
324 default:
325 BUG();
326 return 0; /* shut up gcc */
330 static struct scsi_disk *__scsi_disk_get(struct gendisk *disk)
332 struct scsi_disk *sdkp = NULL;
334 if (disk->private_data) {
335 sdkp = scsi_disk(disk);
336 if (scsi_device_get(sdkp->device) == 0)
337 get_device(&sdkp->dev);
338 else
339 sdkp = NULL;
341 return sdkp;
344 static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
346 struct scsi_disk *sdkp;
348 mutex_lock(&sd_ref_mutex);
349 sdkp = __scsi_disk_get(disk);
350 mutex_unlock(&sd_ref_mutex);
351 return sdkp;
354 static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev)
356 struct scsi_disk *sdkp;
358 mutex_lock(&sd_ref_mutex);
359 sdkp = dev_get_drvdata(dev);
360 if (sdkp)
361 sdkp = __scsi_disk_get(sdkp->disk);
362 mutex_unlock(&sd_ref_mutex);
363 return sdkp;
366 static void scsi_disk_put(struct scsi_disk *sdkp)
368 struct scsi_device *sdev = sdkp->device;
370 mutex_lock(&sd_ref_mutex);
371 put_device(&sdkp->dev);
372 scsi_device_put(sdev);
373 mutex_unlock(&sd_ref_mutex);
376 static void sd_prot_op(struct scsi_cmnd *scmd, unsigned int dif)
378 unsigned int prot_op = SCSI_PROT_NORMAL;
379 unsigned int dix = scsi_prot_sg_count(scmd);
381 if (scmd->sc_data_direction == DMA_FROM_DEVICE) {
382 if (dif && dix)
383 prot_op = SCSI_PROT_READ_PASS;
384 else if (dif && !dix)
385 prot_op = SCSI_PROT_READ_STRIP;
386 else if (!dif && dix)
387 prot_op = SCSI_PROT_READ_INSERT;
388 } else {
389 if (dif && dix)
390 prot_op = SCSI_PROT_WRITE_PASS;
391 else if (dif && !dix)
392 prot_op = SCSI_PROT_WRITE_INSERT;
393 else if (!dif && dix)
394 prot_op = SCSI_PROT_WRITE_STRIP;
397 scsi_set_prot_op(scmd, prot_op);
398 scsi_set_prot_type(scmd, dif);
402 * sd_init_command - build a scsi (read or write) command from
403 * information in the request structure.
404 * @SCpnt: pointer to mid-level's per scsi command structure that
405 * contains request and into which the scsi command is written
407 * Returns 1 if successful and 0 if error (or cannot be done now).
409 static int sd_prep_fn(struct request_queue *q, struct request *rq)
411 struct scsi_cmnd *SCpnt;
412 struct scsi_device *sdp = q->queuedata;
413 struct gendisk *disk = rq->rq_disk;
414 struct scsi_disk *sdkp;
415 sector_t block = blk_rq_pos(rq);
416 sector_t threshold;
417 unsigned int this_count = blk_rq_sectors(rq);
418 int ret, host_dif;
419 unsigned char protect;
421 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
422 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
423 goto out;
424 } else if (rq->cmd_type != REQ_TYPE_FS) {
425 ret = BLKPREP_KILL;
426 goto out;
428 ret = scsi_setup_fs_cmnd(sdp, rq);
429 if (ret != BLKPREP_OK)
430 goto out;
431 SCpnt = rq->special;
432 sdkp = scsi_disk(disk);
434 /* from here on until we're complete, any goto out
435 * is used for a killable error condition */
436 ret = BLKPREP_KILL;
438 SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
439 "sd_init_command: block=%llu, "
440 "count=%d\n",
441 (unsigned long long)block,
442 this_count));
444 if (!sdp || !scsi_device_online(sdp) ||
445 block + blk_rq_sectors(rq) > get_capacity(disk)) {
446 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
447 "Finishing %u sectors\n",
448 blk_rq_sectors(rq)));
449 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
450 "Retry with 0x%p\n", SCpnt));
451 goto out;
454 if (sdp->changed) {
456 * quietly refuse to do anything to a changed disc until
457 * the changed bit has been reset
459 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
460 goto out;
464 * Some SD card readers can't handle multi-sector accesses which touch
465 * the last one or two hardware sectors. Split accesses as needed.
467 threshold = get_capacity(disk) - SD_LAST_BUGGY_SECTORS *
468 (sdp->sector_size / 512);
470 if (unlikely(sdp->last_sector_bug && block + this_count > threshold)) {
471 if (block < threshold) {
472 /* Access up to the threshold but not beyond */
473 this_count = threshold - block;
474 } else {
475 /* Access only a single hardware sector */
476 this_count = sdp->sector_size / 512;
480 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n",
481 (unsigned long long)block));
484 * If we have a 1K hardware sectorsize, prevent access to single
485 * 512 byte sectors. In theory we could handle this - in fact
486 * the scsi cdrom driver must be able to handle this because
487 * we typically use 1K blocksizes, and cdroms typically have
488 * 2K hardware sectorsizes. Of course, things are simpler
489 * with the cdrom, since it is read-only. For performance
490 * reasons, the filesystems should be able to handle this
491 * and not force the scsi disk driver to use bounce buffers
492 * for this.
494 if (sdp->sector_size == 1024) {
495 if ((block & 1) || (blk_rq_sectors(rq) & 1)) {
496 scmd_printk(KERN_ERR, SCpnt,
497 "Bad block number requested\n");
498 goto out;
499 } else {
500 block = block >> 1;
501 this_count = this_count >> 1;
504 if (sdp->sector_size == 2048) {
505 if ((block & 3) || (blk_rq_sectors(rq) & 3)) {
506 scmd_printk(KERN_ERR, SCpnt,
507 "Bad block number requested\n");
508 goto out;
509 } else {
510 block = block >> 2;
511 this_count = this_count >> 2;
514 if (sdp->sector_size == 4096) {
515 if ((block & 7) || (blk_rq_sectors(rq) & 7)) {
516 scmd_printk(KERN_ERR, SCpnt,
517 "Bad block number requested\n");
518 goto out;
519 } else {
520 block = block >> 3;
521 this_count = this_count >> 3;
524 if (rq_data_dir(rq) == WRITE) {
525 if (!sdp->writeable) {
526 goto out;
528 SCpnt->cmnd[0] = WRITE_6;
529 SCpnt->sc_data_direction = DMA_TO_DEVICE;
531 if (blk_integrity_rq(rq) &&
532 sd_dif_prepare(rq, block, sdp->sector_size) == -EIO)
533 goto out;
535 } else if (rq_data_dir(rq) == READ) {
536 SCpnt->cmnd[0] = READ_6;
537 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
538 } else {
539 scmd_printk(KERN_ERR, SCpnt, "Unknown command %x\n", rq->cmd_flags);
540 goto out;
543 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
544 "%s %d/%u 512 byte blocks.\n",
545 (rq_data_dir(rq) == WRITE) ?
546 "writing" : "reading", this_count,
547 blk_rq_sectors(rq)));
549 /* Set RDPROTECT/WRPROTECT if disk is formatted with DIF */
550 host_dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type);
551 if (host_dif)
552 protect = 1 << 5;
553 else
554 protect = 0;
556 if (host_dif == SD_DIF_TYPE2_PROTECTION) {
557 SCpnt->cmnd = mempool_alloc(sd_cdb_pool, GFP_ATOMIC);
559 if (unlikely(SCpnt->cmnd == NULL)) {
560 ret = BLKPREP_DEFER;
561 goto out;
564 SCpnt->cmd_len = SD_EXT_CDB_SIZE;
565 memset(SCpnt->cmnd, 0, SCpnt->cmd_len);
566 SCpnt->cmnd[0] = VARIABLE_LENGTH_CMD;
567 SCpnt->cmnd[7] = 0x18;
568 SCpnt->cmnd[9] = (rq_data_dir(rq) == READ) ? READ_32 : WRITE_32;
569 SCpnt->cmnd[10] = protect | (blk_fua_rq(rq) ? 0x8 : 0);
571 /* LBA */
572 SCpnt->cmnd[12] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
573 SCpnt->cmnd[13] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
574 SCpnt->cmnd[14] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
575 SCpnt->cmnd[15] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
576 SCpnt->cmnd[16] = (unsigned char) (block >> 24) & 0xff;
577 SCpnt->cmnd[17] = (unsigned char) (block >> 16) & 0xff;
578 SCpnt->cmnd[18] = (unsigned char) (block >> 8) & 0xff;
579 SCpnt->cmnd[19] = (unsigned char) block & 0xff;
581 /* Expected Indirect LBA */
582 SCpnt->cmnd[20] = (unsigned char) (block >> 24) & 0xff;
583 SCpnt->cmnd[21] = (unsigned char) (block >> 16) & 0xff;
584 SCpnt->cmnd[22] = (unsigned char) (block >> 8) & 0xff;
585 SCpnt->cmnd[23] = (unsigned char) block & 0xff;
587 /* Transfer length */
588 SCpnt->cmnd[28] = (unsigned char) (this_count >> 24) & 0xff;
589 SCpnt->cmnd[29] = (unsigned char) (this_count >> 16) & 0xff;
590 SCpnt->cmnd[30] = (unsigned char) (this_count >> 8) & 0xff;
591 SCpnt->cmnd[31] = (unsigned char) this_count & 0xff;
592 } else if (block > 0xffffffff) {
593 SCpnt->cmnd[0] += READ_16 - READ_6;
594 SCpnt->cmnd[1] = protect | (blk_fua_rq(rq) ? 0x8 : 0);
595 SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
596 SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
597 SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
598 SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
599 SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
600 SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
601 SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
602 SCpnt->cmnd[9] = (unsigned char) block & 0xff;
603 SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
604 SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
605 SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
606 SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
607 SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
608 } else if ((this_count > 0xff) || (block > 0x1fffff) ||
609 scsi_device_protection(SCpnt->device) ||
610 SCpnt->device->use_10_for_rw) {
611 if (this_count > 0xffff)
612 this_count = 0xffff;
614 SCpnt->cmnd[0] += READ_10 - READ_6;
615 SCpnt->cmnd[1] = protect | (blk_fua_rq(rq) ? 0x8 : 0);
616 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
617 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
618 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
619 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
620 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
621 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
622 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
623 } else {
624 if (unlikely(blk_fua_rq(rq))) {
626 * This happens only if this drive failed
627 * 10byte rw command with ILLEGAL_REQUEST
628 * during operation and thus turned off
629 * use_10_for_rw.
631 scmd_printk(KERN_ERR, SCpnt,
632 "FUA write on READ/WRITE(6) drive\n");
633 goto out;
636 SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
637 SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
638 SCpnt->cmnd[3] = (unsigned char) block & 0xff;
639 SCpnt->cmnd[4] = (unsigned char) this_count;
640 SCpnt->cmnd[5] = 0;
642 SCpnt->sdb.length = this_count * sdp->sector_size;
644 /* If DIF or DIX is enabled, tell HBA how to handle request */
645 if (host_dif || scsi_prot_sg_count(SCpnt))
646 sd_prot_op(SCpnt, host_dif);
649 * We shouldn't disconnect in the middle of a sector, so with a dumb
650 * host adapter, it's safe to assume that we can at least transfer
651 * this many bytes between each connect / disconnect.
653 SCpnt->transfersize = sdp->sector_size;
654 SCpnt->underflow = this_count << 9;
655 SCpnt->allowed = SD_MAX_RETRIES;
658 * This indicates that the command is ready from our end to be
659 * queued.
661 ret = BLKPREP_OK;
662 out:
663 return scsi_prep_return(q, rq, ret);
667 * sd_open - open a scsi disk device
668 * @inode: only i_rdev member may be used
669 * @filp: only f_mode and f_flags may be used
671 * Returns 0 if successful. Returns a negated errno value in case
672 * of error.
674 * Note: This can be called from a user context (e.g. fsck(1) )
675 * or from within the kernel (e.g. as a result of a mount(1) ).
676 * In the latter case @inode and @filp carry an abridged amount
677 * of information as noted above.
679 static int sd_open(struct block_device *bdev, fmode_t mode)
681 struct scsi_disk *sdkp = scsi_disk_get(bdev->bd_disk);
682 struct scsi_device *sdev;
683 int retval;
685 if (!sdkp)
686 return -ENXIO;
688 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
690 sdev = sdkp->device;
693 * If the device is in error recovery, wait until it is done.
694 * If the device is offline, then disallow any access to it.
696 retval = -ENXIO;
697 if (!scsi_block_when_processing_errors(sdev))
698 goto error_out;
700 if (sdev->removable || sdkp->write_prot)
701 check_disk_change(bdev);
704 * If the drive is empty, just let the open fail.
706 retval = -ENOMEDIUM;
707 if (sdev->removable && !sdkp->media_present && !(mode & FMODE_NDELAY))
708 goto error_out;
711 * If the device has the write protect tab set, have the open fail
712 * if the user expects to be able to write to the thing.
714 retval = -EROFS;
715 if (sdkp->write_prot && (mode & FMODE_WRITE))
716 goto error_out;
719 * It is possible that the disk changing stuff resulted in
720 * the device being taken offline. If this is the case,
721 * report this to the user, and don't pretend that the
722 * open actually succeeded.
724 retval = -ENXIO;
725 if (!scsi_device_online(sdev))
726 goto error_out;
728 if (!sdkp->openers++ && sdev->removable) {
729 if (scsi_block_when_processing_errors(sdev))
730 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
733 return 0;
735 error_out:
736 scsi_disk_put(sdkp);
737 return retval;
741 * sd_release - invoked when the (last) close(2) is called on this
742 * scsi disk.
743 * @inode: only i_rdev member may be used
744 * @filp: only f_mode and f_flags may be used
746 * Returns 0.
748 * Note: may block (uninterruptible) if error recovery is underway
749 * on this disk.
751 static int sd_release(struct gendisk *disk, fmode_t mode)
753 struct scsi_disk *sdkp = scsi_disk(disk);
754 struct scsi_device *sdev = sdkp->device;
756 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
758 if (!--sdkp->openers && sdev->removable) {
759 if (scsi_block_when_processing_errors(sdev))
760 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
764 * XXX and what if there are packets in flight and this close()
765 * XXX is followed by a "rmmod sd_mod"?
767 scsi_disk_put(sdkp);
768 return 0;
771 static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
773 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
774 struct scsi_device *sdp = sdkp->device;
775 struct Scsi_Host *host = sdp->host;
776 int diskinfo[4];
778 /* default to most commonly used values */
779 diskinfo[0] = 0x40; /* 1 << 6 */
780 diskinfo[1] = 0x20; /* 1 << 5 */
781 diskinfo[2] = sdkp->capacity >> 11;
783 /* override with calculated, extended default, or driver values */
784 if (host->hostt->bios_param)
785 host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo);
786 else
787 scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
789 geo->heads = diskinfo[0];
790 geo->sectors = diskinfo[1];
791 geo->cylinders = diskinfo[2];
792 return 0;
796 * sd_ioctl - process an ioctl
797 * @inode: only i_rdev/i_bdev members may be used
798 * @filp: only f_mode and f_flags may be used
799 * @cmd: ioctl command number
800 * @arg: this is third argument given to ioctl(2) system call.
801 * Often contains a pointer.
803 * Returns 0 if successful (some ioctls return postive numbers on
804 * success as well). Returns a negated errno value in case of error.
806 * Note: most ioctls are forward onto the block subsystem or further
807 * down in the scsi subsystem.
809 static int sd_ioctl(struct block_device *bdev, fmode_t mode,
810 unsigned int cmd, unsigned long arg)
812 struct gendisk *disk = bdev->bd_disk;
813 struct scsi_device *sdp = scsi_disk(disk)->device;
814 void __user *p = (void __user *)arg;
815 int error;
817 SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
818 disk->disk_name, cmd));
820 error = scsi_verify_blk_ioctl(bdev, cmd);
821 if (error < 0)
822 return error;
825 * If we are in the middle of error recovery, don't let anyone
826 * else try and use this device. Also, if error recovery fails, it
827 * may try and take the device offline, in which case all further
828 * access to the device is prohibited.
830 error = scsi_nonblockable_ioctl(sdp, cmd, p,
831 (mode & FMODE_NDELAY) != 0);
832 if (!scsi_block_when_processing_errors(sdp) || !error)
833 return error;
836 * Send SCSI addressing ioctls directly to mid level, send other
837 * ioctls to block level and then onto mid level if they can't be
838 * resolved.
840 switch (cmd) {
841 case SCSI_IOCTL_GET_IDLUN:
842 case SCSI_IOCTL_GET_BUS_NUMBER:
843 return scsi_ioctl(sdp, cmd, p);
844 default:
845 error = scsi_cmd_blk_ioctl(bdev, mode, cmd, p);
846 if (error != -ENOTTY)
847 return error;
849 return scsi_ioctl(sdp, cmd, p);
852 static void set_media_not_present(struct scsi_disk *sdkp)
854 sdkp->media_present = 0;
855 sdkp->capacity = 0;
856 sdkp->device->changed = 1;
860 * sd_media_changed - check if our medium changed
861 * @disk: kernel device descriptor
863 * Returns 0 if not applicable or no change; 1 if change
865 * Note: this function is invoked from the block subsystem.
867 static int sd_media_changed(struct gendisk *disk)
869 struct scsi_disk *sdkp = scsi_disk(disk);
870 struct scsi_device *sdp = sdkp->device;
871 struct scsi_sense_hdr *sshdr = NULL;
872 int retval;
874 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_media_changed\n"));
876 if (!sdp->removable)
877 return 0;
880 * If the device is offline, don't send any commands - just pretend as
881 * if the command failed. If the device ever comes back online, we
882 * can deal with it then. It is only because of unrecoverable errors
883 * that we would ever take a device offline in the first place.
885 if (!scsi_device_online(sdp)) {
886 set_media_not_present(sdkp);
887 retval = 1;
888 goto out;
892 * Using TEST_UNIT_READY enables differentiation between drive with
893 * no cartridge loaded - NOT READY, drive with changed cartridge -
894 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
896 * Drives that auto spin down. eg iomega jaz 1G, will be started
897 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
898 * sd_revalidate() is called.
900 retval = -ENODEV;
902 if (scsi_block_when_processing_errors(sdp)) {
903 sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
904 retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES,
905 sshdr);
909 * Unable to test, unit probably not ready. This usually
910 * means there is no disc in the drive. Mark as changed,
911 * and we will figure it out later once the drive is
912 * available again.
914 if (retval || (scsi_sense_valid(sshdr) &&
915 /* 0x3a is medium not present */
916 sshdr->asc == 0x3a)) {
917 set_media_not_present(sdkp);
918 retval = 1;
919 goto out;
923 * For removable scsi disk we have to recognise the presence
924 * of a disk in the drive. This is kept in the struct scsi_disk
925 * struct and tested at open ! Daniel Roche (dan@lectra.fr)
927 sdkp->media_present = 1;
929 retval = sdp->changed;
930 sdp->changed = 0;
931 out:
932 if (retval != sdkp->previous_state)
933 sdev_evt_send_simple(sdp, SDEV_EVT_MEDIA_CHANGE, GFP_KERNEL);
934 sdkp->previous_state = retval;
935 kfree(sshdr);
936 return retval;
939 static int sd_sync_cache(struct scsi_disk *sdkp)
941 int retries, res;
942 struct scsi_device *sdp = sdkp->device;
943 struct scsi_sense_hdr sshdr;
945 if (!scsi_device_online(sdp))
946 return -ENODEV;
949 for (retries = 3; retries > 0; --retries) {
950 unsigned char cmd[10] = { 0 };
952 cmd[0] = SYNCHRONIZE_CACHE;
954 * Leave the rest of the command zero to indicate
955 * flush everything.
957 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
958 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
959 if (res == 0)
960 break;
963 if (res) {
964 sd_print_result(sdkp, res);
965 if (driver_byte(res) & DRIVER_SENSE)
966 sd_print_sense_hdr(sdkp, &sshdr);
969 if (res)
970 return -EIO;
971 return 0;
974 static void sd_prepare_flush(struct request_queue *q, struct request *rq)
976 rq->cmd_type = REQ_TYPE_BLOCK_PC;
977 rq->timeout = SD_TIMEOUT;
978 rq->retries = SD_MAX_RETRIES;
979 rq->cmd[0] = SYNCHRONIZE_CACHE;
980 rq->cmd_len = 10;
983 static void sd_rescan(struct device *dev)
985 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
987 if (sdkp) {
988 revalidate_disk(sdkp->disk);
989 scsi_disk_put(sdkp);
994 #ifdef CONFIG_COMPAT
996 * This gets directly called from VFS. When the ioctl
997 * is not recognized we go back to the other translation paths.
999 static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode,
1000 unsigned int cmd, unsigned long arg)
1002 struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
1003 int ret;
1005 ret = scsi_verify_blk_ioctl(bdev, cmd);
1006 if (ret < 0)
1007 return -ENOIOCTLCMD;
1010 * If we are in the middle of error recovery, don't let anyone
1011 * else try and use this device. Also, if error recovery fails, it
1012 * may try and take the device offline, in which case all further
1013 * access to the device is prohibited.
1015 if (!scsi_block_when_processing_errors(sdev))
1016 return -ENODEV;
1018 if (sdev->host->hostt->compat_ioctl) {
1019 ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
1021 return ret;
1025 * Let the static ioctl translation table take care of it.
1027 return -ENOIOCTLCMD;
1029 #endif
1031 static const struct block_device_operations sd_fops = {
1032 .owner = THIS_MODULE,
1033 .open = sd_open,
1034 .release = sd_release,
1035 .locked_ioctl = sd_ioctl,
1036 .getgeo = sd_getgeo,
1037 #ifdef CONFIG_COMPAT
1038 .compat_ioctl = sd_compat_ioctl,
1039 #endif
1040 .media_changed = sd_media_changed,
1041 .revalidate_disk = sd_revalidate_disk,
1044 static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
1046 u64 start_lba = blk_rq_pos(scmd->request);
1047 u64 end_lba = blk_rq_pos(scmd->request) + (scsi_bufflen(scmd) / 512);
1048 u64 bad_lba;
1049 int info_valid;
1051 * resid is optional but mostly filled in. When it's unused,
1052 * its value is zero, so we assume the whole buffer transferred
1054 unsigned int transferred = scsi_bufflen(scmd) - scsi_get_resid(scmd);
1055 unsigned int good_bytes;
1057 if (!blk_fs_request(scmd->request))
1058 return 0;
1060 info_valid = scsi_get_sense_info_fld(scmd->sense_buffer,
1061 SCSI_SENSE_BUFFERSIZE,
1062 &bad_lba);
1063 if (!info_valid)
1064 return 0;
1066 if (scsi_bufflen(scmd) <= scmd->device->sector_size)
1067 return 0;
1069 if (scmd->device->sector_size < 512) {
1070 /* only legitimate sector_size here is 256 */
1071 start_lba <<= 1;
1072 end_lba <<= 1;
1073 } else {
1074 /* be careful ... don't want any overflows */
1075 u64 factor = scmd->device->sector_size / 512;
1076 do_div(start_lba, factor);
1077 do_div(end_lba, factor);
1080 /* The bad lba was reported incorrectly, we have no idea where
1081 * the error is.
1083 if (bad_lba < start_lba || bad_lba >= end_lba)
1084 return 0;
1086 /* This computation should always be done in terms of
1087 * the resolution of the device's medium.
1089 good_bytes = (bad_lba - start_lba) * scmd->device->sector_size;
1090 return min(good_bytes, transferred);
1094 * sd_done - bottom half handler: called when the lower level
1095 * driver has completed (successfully or otherwise) a scsi command.
1096 * @SCpnt: mid-level's per command structure.
1098 * Note: potentially run from within an ISR. Must not block.
1100 static int sd_done(struct scsi_cmnd *SCpnt)
1102 int result = SCpnt->result;
1103 unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt);
1104 struct scsi_sense_hdr sshdr;
1105 struct scsi_disk *sdkp = scsi_disk(SCpnt->request->rq_disk);
1106 int sense_valid = 0;
1107 int sense_deferred = 0;
1109 if (result) {
1110 sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
1111 if (sense_valid)
1112 sense_deferred = scsi_sense_is_deferred(&sshdr);
1114 #ifdef CONFIG_SCSI_LOGGING
1115 SCSI_LOG_HLCOMPLETE(1, scsi_print_result(SCpnt));
1116 if (sense_valid) {
1117 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
1118 "sd_done: sb[respc,sk,asc,"
1119 "ascq]=%x,%x,%x,%x\n",
1120 sshdr.response_code,
1121 sshdr.sense_key, sshdr.asc,
1122 sshdr.ascq));
1124 #endif
1125 if (driver_byte(result) != DRIVER_SENSE &&
1126 (!sense_valid || sense_deferred))
1127 goto out;
1129 switch (sshdr.sense_key) {
1130 case HARDWARE_ERROR:
1131 case MEDIUM_ERROR:
1132 good_bytes = sd_completed_bytes(SCpnt);
1133 break;
1134 case RECOVERED_ERROR:
1135 good_bytes = scsi_bufflen(SCpnt);
1136 break;
1137 case NO_SENSE:
1138 /* This indicates a false check condition, so ignore it. An
1139 * unknown amount of data was transferred so treat it as an
1140 * error.
1142 scsi_print_sense("sd", SCpnt);
1143 SCpnt->result = 0;
1144 memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1145 break;
1146 case ABORTED_COMMAND:
1147 if (sshdr.asc == 0x10) { /* DIF: Disk detected corruption */
1148 scsi_print_result(SCpnt);
1149 scsi_print_sense("sd", SCpnt);
1150 good_bytes = sd_completed_bytes(SCpnt);
1152 break;
1153 case ILLEGAL_REQUEST:
1154 if (sshdr.asc == 0x10) { /* DIX: HBA detected corruption */
1155 scsi_print_result(SCpnt);
1156 scsi_print_sense("sd", SCpnt);
1157 good_bytes = sd_completed_bytes(SCpnt);
1159 break;
1160 default:
1161 break;
1163 out:
1164 if (rq_data_dir(SCpnt->request) == READ && scsi_prot_sg_count(SCpnt))
1165 sd_dif_complete(SCpnt, good_bytes);
1167 if (scsi_host_dif_capable(sdkp->device->host, sdkp->protection_type)
1168 == SD_DIF_TYPE2_PROTECTION && SCpnt->cmnd != SCpnt->request->cmd)
1169 mempool_free(SCpnt->cmnd, sd_cdb_pool);
1171 return good_bytes;
1174 static int media_not_present(struct scsi_disk *sdkp,
1175 struct scsi_sense_hdr *sshdr)
1178 if (!scsi_sense_valid(sshdr))
1179 return 0;
1180 /* not invoked for commands that could return deferred errors */
1181 if (sshdr->sense_key != NOT_READY &&
1182 sshdr->sense_key != UNIT_ATTENTION)
1183 return 0;
1184 if (sshdr->asc != 0x3A) /* medium not present */
1185 return 0;
1187 set_media_not_present(sdkp);
1188 return 1;
1192 * spinup disk - called only in sd_revalidate_disk()
1194 static void
1195 sd_spinup_disk(struct scsi_disk *sdkp)
1197 unsigned char cmd[10];
1198 unsigned long spintime_expire = 0;
1199 int retries, spintime;
1200 unsigned int the_result;
1201 struct scsi_sense_hdr sshdr;
1202 int sense_valid = 0;
1204 spintime = 0;
1206 /* Spin up drives, as required. Only do this at boot time */
1207 /* Spinup needs to be done for module loads too. */
1208 do {
1209 retries = 0;
1211 do {
1212 cmd[0] = TEST_UNIT_READY;
1213 memset((void *) &cmd[1], 0, 9);
1215 the_result = scsi_execute_req(sdkp->device, cmd,
1216 DMA_NONE, NULL, 0,
1217 &sshdr, SD_TIMEOUT,
1218 SD_MAX_RETRIES, NULL);
1221 * If the drive has indicated to us that it
1222 * doesn't have any media in it, don't bother
1223 * with any more polling.
1225 if (media_not_present(sdkp, &sshdr))
1226 return;
1228 if (the_result)
1229 sense_valid = scsi_sense_valid(&sshdr);
1230 retries++;
1231 } while (retries < 3 &&
1232 (!scsi_status_is_good(the_result) ||
1233 ((driver_byte(the_result) & DRIVER_SENSE) &&
1234 sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
1236 if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
1237 /* no sense, TUR either succeeded or failed
1238 * with a status error */
1239 if(!spintime && !scsi_status_is_good(the_result)) {
1240 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1241 sd_print_result(sdkp, the_result);
1243 break;
1247 * The device does not want the automatic start to be issued.
1249 if (sdkp->device->no_start_on_add)
1250 break;
1252 if (sense_valid && sshdr.sense_key == NOT_READY) {
1253 if (sshdr.asc == 4 && sshdr.ascq == 3)
1254 break; /* manual intervention required */
1255 if (sshdr.asc == 4 && sshdr.ascq == 0xb)
1256 break; /* standby */
1257 if (sshdr.asc == 4 && sshdr.ascq == 0xc)
1258 break; /* unavailable */
1260 * Issue command to spin up drive when not ready
1262 if (!spintime) {
1263 sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
1264 cmd[0] = START_STOP;
1265 cmd[1] = 1; /* Return immediately */
1266 memset((void *) &cmd[2], 0, 8);
1267 cmd[4] = 1; /* Start spin cycle */
1268 if (sdkp->device->start_stop_pwr_cond)
1269 cmd[4] |= 1 << 4;
1270 scsi_execute_req(sdkp->device, cmd, DMA_NONE,
1271 NULL, 0, &sshdr,
1272 SD_TIMEOUT, SD_MAX_RETRIES,
1273 NULL);
1274 spintime_expire = jiffies + 100 * HZ;
1275 spintime = 1;
1277 /* Wait 1 second for next try */
1278 msleep(1000);
1279 printk(".");
1282 * Wait for USB flash devices with slow firmware.
1283 * Yes, this sense key/ASC combination shouldn't
1284 * occur here. It's characteristic of these devices.
1286 } else if (sense_valid &&
1287 sshdr.sense_key == UNIT_ATTENTION &&
1288 sshdr.asc == 0x28) {
1289 if (!spintime) {
1290 spintime_expire = jiffies + 5 * HZ;
1291 spintime = 1;
1293 /* Wait 1 second for next try */
1294 msleep(1000);
1295 } else {
1296 /* we don't understand the sense code, so it's
1297 * probably pointless to loop */
1298 if(!spintime) {
1299 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1300 sd_print_sense_hdr(sdkp, &sshdr);
1302 break;
1305 } while (spintime && time_before_eq(jiffies, spintime_expire));
1307 if (spintime) {
1308 if (scsi_status_is_good(the_result))
1309 printk("ready\n");
1310 else
1311 printk("not responding...\n");
1317 * Determine whether disk supports Data Integrity Field.
1319 void sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer)
1321 struct scsi_device *sdp = sdkp->device;
1322 u8 type;
1324 if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0)
1325 return;
1327 type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */
1329 if (type == sdkp->protection_type || !sdkp->first_scan)
1330 return;
1332 sdkp->protection_type = type;
1334 if (type > SD_DIF_TYPE3_PROTECTION) {
1335 sd_printk(KERN_ERR, sdkp, "formatted with unsupported " \
1336 "protection type %u. Disabling disk!\n", type);
1337 sdkp->capacity = 0;
1338 return;
1341 if (scsi_host_dif_capable(sdp->host, type))
1342 sd_printk(KERN_NOTICE, sdkp,
1343 "Enabling DIF Type %u protection\n", type);
1344 else
1345 sd_printk(KERN_NOTICE, sdkp,
1346 "Disabling DIF Type %u protection\n", type);
1349 static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp,
1350 struct scsi_sense_hdr *sshdr, int sense_valid,
1351 int the_result)
1353 sd_print_result(sdkp, the_result);
1354 if (driver_byte(the_result) & DRIVER_SENSE)
1355 sd_print_sense_hdr(sdkp, sshdr);
1356 else
1357 sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
1360 * Set dirty bit for removable devices if not ready -
1361 * sometimes drives will not report this properly.
1363 if (sdp->removable &&
1364 sense_valid && sshdr->sense_key == NOT_READY)
1365 sdp->changed = 1;
1368 * We used to set media_present to 0 here to indicate no media
1369 * in the drive, but some drives fail read capacity even with
1370 * media present, so we can't do that.
1372 sdkp->capacity = 0; /* unknown mapped to zero - as usual */
1375 #define RC16_LEN 32
1376 #if RC16_LEN > SD_BUF_SIZE
1377 #error RC16_LEN must not be more than SD_BUF_SIZE
1378 #endif
1380 static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
1381 unsigned char *buffer)
1383 unsigned char cmd[16];
1384 struct scsi_sense_hdr sshdr;
1385 int sense_valid = 0;
1386 int the_result;
1387 int retries = 3;
1388 unsigned int alignment;
1389 unsigned long long lba;
1390 unsigned sector_size;
1392 do {
1393 memset(cmd, 0, 16);
1394 cmd[0] = SERVICE_ACTION_IN;
1395 cmd[1] = SAI_READ_CAPACITY_16;
1396 cmd[13] = RC16_LEN;
1397 memset(buffer, 0, RC16_LEN);
1399 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
1400 buffer, RC16_LEN, &sshdr,
1401 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
1403 if (media_not_present(sdkp, &sshdr))
1404 return -ENODEV;
1406 if (the_result) {
1407 sense_valid = scsi_sense_valid(&sshdr);
1408 if (sense_valid &&
1409 sshdr.sense_key == ILLEGAL_REQUEST &&
1410 (sshdr.asc == 0x20 || sshdr.asc == 0x24) &&
1411 sshdr.ascq == 0x00)
1412 /* Invalid Command Operation Code or
1413 * Invalid Field in CDB, just retry
1414 * silently with RC10 */
1415 return -EINVAL;
1417 retries--;
1419 } while (the_result && retries);
1421 if (the_result) {
1422 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY(16) failed\n");
1423 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
1424 return -EINVAL;
1427 sector_size = get_unaligned_be32(&buffer[8]);
1428 lba = get_unaligned_be64(&buffer[0]);
1430 sd_read_protection_type(sdkp, buffer);
1432 if ((sizeof(sdkp->capacity) == 4) && (lba >= 0xffffffffULL)) {
1433 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
1434 "kernel compiled with support for large block "
1435 "devices.\n");
1436 sdkp->capacity = 0;
1437 return -EOVERFLOW;
1440 /* Logical blocks per physical block exponent */
1441 sdkp->hw_sector_size = (1 << (buffer[13] & 0xf)) * sector_size;
1443 /* Lowest aligned logical block */
1444 alignment = ((buffer[14] & 0x3f) << 8 | buffer[15]) * sector_size;
1445 blk_queue_alignment_offset(sdp->request_queue, alignment);
1446 if (alignment && sdkp->first_scan)
1447 sd_printk(KERN_NOTICE, sdkp,
1448 "physical block alignment offset: %u\n", alignment);
1450 sdkp->capacity = lba + 1;
1451 return sector_size;
1454 static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,
1455 unsigned char *buffer)
1457 unsigned char cmd[16];
1458 struct scsi_sense_hdr sshdr;
1459 int sense_valid = 0;
1460 int the_result;
1461 int retries = 3;
1462 sector_t lba;
1463 unsigned sector_size;
1465 do {
1466 cmd[0] = READ_CAPACITY;
1467 memset(&cmd[1], 0, 9);
1468 memset(buffer, 0, 8);
1470 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
1471 buffer, 8, &sshdr,
1472 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
1474 if (media_not_present(sdkp, &sshdr))
1475 return -ENODEV;
1477 if (the_result)
1478 sense_valid = scsi_sense_valid(&sshdr);
1479 retries--;
1481 } while (the_result && retries);
1483 if (the_result) {
1484 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY failed\n");
1485 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
1486 return -EINVAL;
1489 sector_size = get_unaligned_be32(&buffer[4]);
1490 lba = get_unaligned_be32(&buffer[0]);
1492 if ((sizeof(sdkp->capacity) == 4) && (lba == 0xffffffff)) {
1493 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
1494 "kernel compiled with support for large block "
1495 "devices.\n");
1496 sdkp->capacity = 0;
1497 return -EOVERFLOW;
1500 sdkp->capacity = lba + 1;
1501 sdkp->hw_sector_size = sector_size;
1502 return sector_size;
1505 static int sd_try_rc16_first(struct scsi_device *sdp)
1507 if (sdp->scsi_level > SCSI_SPC_2)
1508 return 1;
1509 if (scsi_device_protection(sdp))
1510 return 1;
1511 return 0;
1515 * read disk capacity
1517 static void
1518 sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
1520 int sector_size;
1521 struct scsi_device *sdp = sdkp->device;
1522 sector_t old_capacity = sdkp->capacity;
1524 if (sd_try_rc16_first(sdp)) {
1525 sector_size = read_capacity_16(sdkp, sdp, buffer);
1526 if (sector_size == -EOVERFLOW)
1527 goto got_data;
1528 if (sector_size == -ENODEV)
1529 return;
1530 if (sector_size < 0)
1531 sector_size = read_capacity_10(sdkp, sdp, buffer);
1532 if (sector_size < 0)
1533 return;
1534 } else {
1535 sector_size = read_capacity_10(sdkp, sdp, buffer);
1536 if (sector_size == -EOVERFLOW)
1537 goto got_data;
1538 if (sector_size < 0)
1539 return;
1540 if ((sizeof(sdkp->capacity) > 4) &&
1541 (sdkp->capacity > 0xffffffffULL)) {
1542 int old_sector_size = sector_size;
1543 sd_printk(KERN_NOTICE, sdkp, "Very big device. "
1544 "Trying to use READ CAPACITY(16).\n");
1545 sector_size = read_capacity_16(sdkp, sdp, buffer);
1546 if (sector_size < 0) {
1547 sd_printk(KERN_NOTICE, sdkp,
1548 "Using 0xffffffff as device size\n");
1549 sdkp->capacity = 1 + (sector_t) 0xffffffff;
1550 sector_size = old_sector_size;
1551 goto got_data;
1556 /* Some devices are known to return the total number of blocks,
1557 * not the highest block number. Some devices have versions
1558 * which do this and others which do not. Some devices we might
1559 * suspect of doing this but we don't know for certain.
1561 * If we know the reported capacity is wrong, decrement it. If
1562 * we can only guess, then assume the number of blocks is even
1563 * (usually true but not always) and err on the side of lowering
1564 * the capacity.
1566 if (sdp->fix_capacity ||
1567 (sdp->guess_capacity && (sdkp->capacity & 0x01))) {
1568 sd_printk(KERN_INFO, sdkp, "Adjusting the sector count "
1569 "from its reported value: %llu\n",
1570 (unsigned long long) sdkp->capacity);
1571 --sdkp->capacity;
1574 got_data:
1575 if (sector_size == 0) {
1576 sector_size = 512;
1577 sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
1578 "assuming 512.\n");
1581 if (sector_size != 512 &&
1582 sector_size != 1024 &&
1583 sector_size != 2048 &&
1584 sector_size != 4096 &&
1585 sector_size != 256) {
1586 sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
1587 sector_size);
1589 * The user might want to re-format the drive with
1590 * a supported sectorsize. Once this happens, it
1591 * would be relatively trivial to set the thing up.
1592 * For this reason, we leave the thing in the table.
1594 sdkp->capacity = 0;
1596 * set a bogus sector size so the normal read/write
1597 * logic in the block layer will eventually refuse any
1598 * request on this device without tripping over power
1599 * of two sector size assumptions
1601 sector_size = 512;
1603 blk_queue_logical_block_size(sdp->request_queue, sector_size);
1606 char cap_str_2[10], cap_str_10[10];
1607 u64 sz = (u64)sdkp->capacity << ilog2(sector_size);
1609 string_get_size(sz, STRING_UNITS_2, cap_str_2,
1610 sizeof(cap_str_2));
1611 string_get_size(sz, STRING_UNITS_10, cap_str_10,
1612 sizeof(cap_str_10));
1614 if (sdkp->first_scan || old_capacity != sdkp->capacity) {
1615 sd_printk(KERN_NOTICE, sdkp,
1616 "%llu %d-byte logical blocks: (%s/%s)\n",
1617 (unsigned long long)sdkp->capacity,
1618 sector_size, cap_str_10, cap_str_2);
1620 if (sdkp->hw_sector_size != sector_size)
1621 sd_printk(KERN_NOTICE, sdkp,
1622 "%u-byte physical blocks\n",
1623 sdkp->hw_sector_size);
1627 /* Rescale capacity to 512-byte units */
1628 if (sector_size == 4096)
1629 sdkp->capacity <<= 3;
1630 else if (sector_size == 2048)
1631 sdkp->capacity <<= 2;
1632 else if (sector_size == 1024)
1633 sdkp->capacity <<= 1;
1634 else if (sector_size == 256)
1635 sdkp->capacity >>= 1;
1637 blk_queue_physical_block_size(sdp->request_queue, sdkp->hw_sector_size);
1638 sdkp->device->sector_size = sector_size;
1641 /* called with buffer of length 512 */
1642 static inline int
1643 sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
1644 unsigned char *buffer, int len, struct scsi_mode_data *data,
1645 struct scsi_sense_hdr *sshdr)
1647 return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
1648 SD_TIMEOUT, SD_MAX_RETRIES, data,
1649 sshdr);
1653 * read write protect setting, if possible - called only in sd_revalidate_disk()
1654 * called with buffer of length SD_BUF_SIZE
1656 static void
1657 sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
1659 int res;
1660 struct scsi_device *sdp = sdkp->device;
1661 struct scsi_mode_data data;
1662 int old_wp = sdkp->write_prot;
1664 set_disk_ro(sdkp->disk, 0);
1665 if (sdp->skip_ms_page_3f) {
1666 sd_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
1667 return;
1670 if (sdp->use_192_bytes_for_3f) {
1671 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
1672 } else {
1674 * First attempt: ask for all pages (0x3F), but only 4 bytes.
1675 * We have to start carefully: some devices hang if we ask
1676 * for more than is available.
1678 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
1681 * Second attempt: ask for page 0 When only page 0 is
1682 * implemented, a request for page 3F may return Sense Key
1683 * 5: Illegal Request, Sense Code 24: Invalid field in
1684 * CDB.
1686 if (!scsi_status_is_good(res))
1687 res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
1690 * Third attempt: ask 255 bytes, as we did earlier.
1692 if (!scsi_status_is_good(res))
1693 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
1694 &data, NULL);
1697 if (!scsi_status_is_good(res)) {
1698 sd_printk(KERN_WARNING, sdkp,
1699 "Test WP failed, assume Write Enabled\n");
1700 } else {
1701 sdkp->write_prot = ((data.device_specific & 0x80) != 0);
1702 set_disk_ro(sdkp->disk, sdkp->write_prot);
1703 if (sdkp->first_scan || old_wp != sdkp->write_prot) {
1704 sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
1705 sdkp->write_prot ? "on" : "off");
1706 sd_printk(KERN_DEBUG, sdkp,
1707 "Mode Sense: %02x %02x %02x %02x\n",
1708 buffer[0], buffer[1], buffer[2], buffer[3]);
1714 * sd_read_cache_type - called only from sd_revalidate_disk()
1715 * called with buffer of length SD_BUF_SIZE
1717 static void
1718 sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
1720 int len = 0, res;
1721 struct scsi_device *sdp = sdkp->device;
1723 int dbd;
1724 int modepage;
1725 struct scsi_mode_data data;
1726 struct scsi_sense_hdr sshdr;
1727 int old_wce = sdkp->WCE;
1728 int old_rcd = sdkp->RCD;
1729 int old_dpofua = sdkp->DPOFUA;
1731 if (sdp->skip_ms_page_8)
1732 goto defaults;
1734 if (sdp->type == TYPE_RBC) {
1735 modepage = 6;
1736 dbd = 8;
1737 } else {
1738 modepage = 8;
1739 dbd = 0;
1742 /* cautiously ask */
1743 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr);
1745 if (!scsi_status_is_good(res))
1746 goto bad_sense;
1748 if (!data.header_length) {
1749 modepage = 6;
1750 sd_printk(KERN_ERR, sdkp, "Missing header in MODE_SENSE response\n");
1753 /* that went OK, now ask for the proper length */
1754 len = data.length;
1757 * We're only interested in the first three bytes, actually.
1758 * But the data cache page is defined for the first 20.
1760 if (len < 3)
1761 goto bad_sense;
1762 if (len > 20)
1763 len = 20;
1765 /* Take headers and block descriptors into account */
1766 len += data.header_length + data.block_descriptor_length;
1767 if (len > SD_BUF_SIZE)
1768 goto bad_sense;
1770 /* Get the data */
1771 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr);
1773 if (scsi_status_is_good(res)) {
1774 int offset = data.header_length + data.block_descriptor_length;
1776 if (offset >= SD_BUF_SIZE - 2) {
1777 sd_printk(KERN_ERR, sdkp, "Malformed MODE SENSE response\n");
1778 goto defaults;
1781 if ((buffer[offset] & 0x3f) != modepage) {
1782 sd_printk(KERN_ERR, sdkp, "Got wrong page\n");
1783 goto defaults;
1786 if (modepage == 8) {
1787 sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
1788 sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
1789 } else {
1790 sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
1791 sdkp->RCD = 0;
1794 sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
1795 if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
1796 sd_printk(KERN_NOTICE, sdkp,
1797 "Uses READ/WRITE(6), disabling FUA\n");
1798 sdkp->DPOFUA = 0;
1801 if (sdkp->first_scan || old_wce != sdkp->WCE ||
1802 old_rcd != sdkp->RCD || old_dpofua != sdkp->DPOFUA)
1803 sd_printk(KERN_NOTICE, sdkp,
1804 "Write cache: %s, read cache: %s, %s\n",
1805 sdkp->WCE ? "enabled" : "disabled",
1806 sdkp->RCD ? "disabled" : "enabled",
1807 sdkp->DPOFUA ? "supports DPO and FUA"
1808 : "doesn't support DPO or FUA");
1810 return;
1813 bad_sense:
1814 if (scsi_sense_valid(&sshdr) &&
1815 sshdr.sense_key == ILLEGAL_REQUEST &&
1816 sshdr.asc == 0x24 && sshdr.ascq == 0x0)
1817 /* Invalid field in CDB */
1818 sd_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
1819 else
1820 sd_printk(KERN_ERR, sdkp, "Asking for cache data failed\n");
1822 defaults:
1823 sd_printk(KERN_ERR, sdkp, "Assuming drive cache: write through\n");
1824 sdkp->WCE = 0;
1825 sdkp->RCD = 0;
1826 sdkp->DPOFUA = 0;
1830 * The ATO bit indicates whether the DIF application tag is available
1831 * for use by the operating system.
1833 void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
1835 int res, offset;
1836 struct scsi_device *sdp = sdkp->device;
1837 struct scsi_mode_data data;
1838 struct scsi_sense_hdr sshdr;
1840 if (sdp->type != TYPE_DISK)
1841 return;
1843 if (sdkp->protection_type == 0)
1844 return;
1846 res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT,
1847 SD_MAX_RETRIES, &data, &sshdr);
1849 if (!scsi_status_is_good(res) || !data.header_length ||
1850 data.length < 6) {
1851 sd_printk(KERN_WARNING, sdkp,
1852 "getting Control mode page failed, assume no ATO\n");
1854 if (scsi_sense_valid(&sshdr))
1855 sd_print_sense_hdr(sdkp, &sshdr);
1857 return;
1860 offset = data.header_length + data.block_descriptor_length;
1862 if ((buffer[offset] & 0x3f) != 0x0a) {
1863 sd_printk(KERN_ERR, sdkp, "ATO Got wrong page\n");
1864 return;
1867 if ((buffer[offset + 5] & 0x80) == 0)
1868 return;
1870 sdkp->ATO = 1;
1872 return;
1876 * sd_read_block_limits - Query disk device for preferred I/O sizes.
1877 * @disk: disk to query
1879 static void sd_read_block_limits(struct scsi_disk *sdkp)
1881 unsigned int sector_sz = sdkp->device->sector_size;
1882 char *buffer;
1884 /* Block Limits VPD */
1885 buffer = scsi_get_vpd_page(sdkp->device, 0xb0);
1887 if (buffer == NULL)
1888 return;
1890 blk_queue_io_min(sdkp->disk->queue,
1891 get_unaligned_be16(&buffer[6]) * sector_sz);
1892 blk_queue_io_opt(sdkp->disk->queue,
1893 get_unaligned_be32(&buffer[12]) * sector_sz);
1895 kfree(buffer);
1899 * sd_read_block_characteristics - Query block dev. characteristics
1900 * @disk: disk to query
1902 static void sd_read_block_characteristics(struct scsi_disk *sdkp)
1904 char *buffer;
1905 u16 rot;
1907 /* Block Device Characteristics VPD */
1908 buffer = scsi_get_vpd_page(sdkp->device, 0xb1);
1910 if (buffer == NULL)
1911 return;
1913 rot = get_unaligned_be16(&buffer[4]);
1915 if (rot == 1)
1916 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, sdkp->disk->queue);
1918 kfree(buffer);
1921 static int sd_try_extended_inquiry(struct scsi_device *sdp)
1924 * Although VPD inquiries can go to SCSI-2 type devices,
1925 * some USB ones crash on receiving them, and the pages
1926 * we currently ask for are for SPC-3 and beyond
1928 if (sdp->scsi_level > SCSI_SPC_2)
1929 return 1;
1930 return 0;
1934 * sd_revalidate_disk - called the first time a new disk is seen,
1935 * performs disk spin up, read_capacity, etc.
1936 * @disk: struct gendisk we care about
1938 static int sd_revalidate_disk(struct gendisk *disk)
1940 struct scsi_disk *sdkp = scsi_disk(disk);
1941 struct scsi_device *sdp = sdkp->device;
1942 unsigned char *buffer;
1943 unsigned ordered;
1945 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
1946 "sd_revalidate_disk\n"));
1949 * If the device is offline, don't try and read capacity or any
1950 * of the other niceties.
1952 if (!scsi_device_online(sdp))
1953 goto out;
1955 buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL);
1956 if (!buffer) {
1957 sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
1958 "allocation failure.\n");
1959 goto out;
1962 sd_spinup_disk(sdkp);
1965 * Without media there is no reason to ask; moreover, some devices
1966 * react badly if we do.
1968 if (sdkp->media_present) {
1969 sd_read_capacity(sdkp, buffer);
1971 if (sd_try_extended_inquiry(sdp)) {
1972 sd_read_block_limits(sdkp);
1973 sd_read_block_characteristics(sdkp);
1976 sd_read_write_protect_flag(sdkp, buffer);
1977 sd_read_cache_type(sdkp, buffer);
1978 sd_read_app_tag_own(sdkp, buffer);
1981 sdkp->first_scan = 0;
1984 * We now have all cache related info, determine how we deal
1985 * with ordered requests. Note that as the current SCSI
1986 * dispatch function can alter request order, we cannot use
1987 * QUEUE_ORDERED_TAG_* even when ordered tag is supported.
1989 if (sdkp->WCE)
1990 ordered = sdkp->DPOFUA
1991 ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH;
1992 else
1993 ordered = QUEUE_ORDERED_DRAIN;
1995 blk_queue_ordered(sdkp->disk->queue, ordered, sd_prepare_flush);
1997 set_capacity(disk, sdkp->capacity);
1998 kfree(buffer);
2000 out:
2001 return 0;
2005 * sd_format_disk_name - format disk name
2006 * @prefix: name prefix - ie. "sd" for SCSI disks
2007 * @index: index of the disk to format name for
2008 * @buf: output buffer
2009 * @buflen: length of the output buffer
2011 * SCSI disk names starts at sda. The 26th device is sdz and the
2012 * 27th is sdaa. The last one for two lettered suffix is sdzz
2013 * which is followed by sdaaa.
2015 * This is basically 26 base counting with one extra 'nil' entry
2016 * at the beggining from the second digit on and can be
2017 * determined using similar method as 26 base conversion with the
2018 * index shifted -1 after each digit is computed.
2020 * CONTEXT:
2021 * Don't care.
2023 * RETURNS:
2024 * 0 on success, -errno on failure.
2026 static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen)
2028 const int base = 'z' - 'a' + 1;
2029 char *begin = buf + strlen(prefix);
2030 char *end = buf + buflen;
2031 char *p;
2032 int unit;
2034 p = end - 1;
2035 *p = '\0';
2036 unit = base;
2037 do {
2038 if (p == begin)
2039 return -EINVAL;
2040 *--p = 'a' + (index % unit);
2041 index = (index / unit) - 1;
2042 } while (index >= 0);
2044 memmove(begin, p, end - p);
2045 memcpy(buf, prefix, strlen(prefix));
2047 return 0;
2051 * The asynchronous part of sd_probe
2053 static void sd_probe_async(void *data, async_cookie_t cookie)
2055 struct scsi_disk *sdkp = data;
2056 struct scsi_device *sdp;
2057 struct gendisk *gd;
2058 u32 index;
2059 struct device *dev;
2061 sdp = sdkp->device;
2062 gd = sdkp->disk;
2063 index = sdkp->index;
2064 dev = &sdp->sdev_gendev;
2066 gd->major = sd_major((index & 0xf0) >> 4);
2067 gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
2068 gd->minors = SD_MINORS;
2070 gd->fops = &sd_fops;
2071 gd->private_data = &sdkp->driver;
2072 gd->queue = sdkp->device->request_queue;
2074 /* defaults, until the device tells us otherwise */
2075 sdp->sector_size = 512;
2076 sdkp->capacity = 0;
2077 sdkp->media_present = 1;
2078 sdkp->write_prot = 0;
2079 sdkp->WCE = 0;
2080 sdkp->RCD = 0;
2081 sdkp->ATO = 0;
2082 sdkp->first_scan = 1;
2084 sd_revalidate_disk(gd);
2086 blk_queue_prep_rq(sdp->request_queue, sd_prep_fn);
2088 gd->driverfs_dev = &sdp->sdev_gendev;
2089 gd->flags = GENHD_FL_EXT_DEVT | GENHD_FL_DRIVERFS;
2090 if (sdp->removable)
2091 gd->flags |= GENHD_FL_REMOVABLE;
2093 dev_set_drvdata(dev, sdkp);
2094 add_disk(gd);
2095 sd_dif_config_host(sdkp);
2097 sd_revalidate_disk(gd);
2099 sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
2100 sdp->removable ? "removable " : "");
2101 put_device(&sdkp->dev);
2105 * sd_probe - called during driver initialization and whenever a
2106 * new scsi device is attached to the system. It is called once
2107 * for each scsi device (not just disks) present.
2108 * @dev: pointer to device object
2110 * Returns 0 if successful (or not interested in this scsi device
2111 * (e.g. scanner)); 1 when there is an error.
2113 * Note: this function is invoked from the scsi mid-level.
2114 * This function sets up the mapping between a given
2115 * <host,channel,id,lun> (found in sdp) and new device name
2116 * (e.g. /dev/sda). More precisely it is the block device major
2117 * and minor number that is chosen here.
2119 * Assume sd_attach is not re-entrant (for time being)
2120 * Also think about sd_attach() and sd_remove() running coincidentally.
2122 static int sd_probe(struct device *dev)
2124 struct scsi_device *sdp = to_scsi_device(dev);
2125 struct scsi_disk *sdkp;
2126 struct gendisk *gd;
2127 u32 index;
2128 int error;
2130 error = -ENODEV;
2131 if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
2132 goto out;
2134 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
2135 "sd_attach\n"));
2137 error = -ENOMEM;
2138 sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
2139 if (!sdkp)
2140 goto out;
2142 gd = alloc_disk(SD_MINORS);
2143 if (!gd)
2144 goto out_free;
2146 do {
2147 if (!ida_pre_get(&sd_index_ida, GFP_KERNEL))
2148 goto out_put;
2150 spin_lock(&sd_index_lock);
2151 error = ida_get_new(&sd_index_ida, &index);
2152 spin_unlock(&sd_index_lock);
2153 } while (error == -EAGAIN);
2155 if (error)
2156 goto out_put;
2158 if (index >= SD_MAX_DISKS) {
2159 error = -ENODEV;
2160 sdev_printk(KERN_WARNING, sdp, "SCSI disk (sd) name space exhausted.\n");
2161 goto out_free_index;
2164 error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN);
2165 if (error)
2166 goto out_free_index;
2168 sdkp->device = sdp;
2169 sdkp->driver = &sd_template;
2170 sdkp->disk = gd;
2171 sdkp->index = index;
2172 sdkp->openers = 0;
2173 sdkp->previous_state = 1;
2175 if (!sdp->request_queue->rq_timeout) {
2176 if (sdp->type != TYPE_MOD)
2177 blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT);
2178 else
2179 blk_queue_rq_timeout(sdp->request_queue,
2180 SD_MOD_TIMEOUT);
2183 device_initialize(&sdkp->dev);
2184 sdkp->dev.parent = &sdp->sdev_gendev;
2185 sdkp->dev.class = &sd_disk_class;
2186 dev_set_name(&sdkp->dev, dev_name(&sdp->sdev_gendev));
2188 if (device_add(&sdkp->dev))
2189 goto out_free_index;
2191 get_device(&sdp->sdev_gendev);
2193 get_device(&sdkp->dev); /* prevent release before async_schedule */
2194 async_schedule(sd_probe_async, sdkp);
2196 return 0;
2198 out_free_index:
2199 spin_lock(&sd_index_lock);
2200 ida_remove(&sd_index_ida, index);
2201 spin_unlock(&sd_index_lock);
2202 out_put:
2203 put_disk(gd);
2204 out_free:
2205 kfree(sdkp);
2206 out:
2207 return error;
2211 * sd_remove - called whenever a scsi disk (previously recognized by
2212 * sd_probe) is detached from the system. It is called (potentially
2213 * multiple times) during sd module unload.
2214 * @sdp: pointer to mid level scsi device object
2216 * Note: this function is invoked from the scsi mid-level.
2217 * This function potentially frees up a device name (e.g. /dev/sdc)
2218 * that could be re-used by a subsequent sd_probe().
2219 * This function is not called when the built-in sd driver is "exit-ed".
2221 static int sd_remove(struct device *dev)
2223 struct scsi_disk *sdkp;
2225 async_synchronize_full();
2226 sdkp = dev_get_drvdata(dev);
2227 blk_queue_prep_rq(sdkp->device->request_queue, scsi_prep_fn);
2228 device_del(&sdkp->dev);
2229 del_gendisk(sdkp->disk);
2230 sd_shutdown(dev);
2232 mutex_lock(&sd_ref_mutex);
2233 dev_set_drvdata(dev, NULL);
2234 put_device(&sdkp->dev);
2235 mutex_unlock(&sd_ref_mutex);
2237 return 0;
2241 * scsi_disk_release - Called to free the scsi_disk structure
2242 * @dev: pointer to embedded class device
2244 * sd_ref_mutex must be held entering this routine. Because it is
2245 * called on last put, you should always use the scsi_disk_get()
2246 * scsi_disk_put() helpers which manipulate the semaphore directly
2247 * and never do a direct put_device.
2249 static void scsi_disk_release(struct device *dev)
2251 struct scsi_disk *sdkp = to_scsi_disk(dev);
2252 struct gendisk *disk = sdkp->disk;
2254 spin_lock(&sd_index_lock);
2255 ida_remove(&sd_index_ida, sdkp->index);
2256 spin_unlock(&sd_index_lock);
2258 disk->private_data = NULL;
2259 put_disk(disk);
2260 put_device(&sdkp->device->sdev_gendev);
2262 kfree(sdkp);
2265 static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
2267 unsigned char cmd[6] = { START_STOP }; /* START_VALID */
2268 struct scsi_sense_hdr sshdr;
2269 struct scsi_device *sdp = sdkp->device;
2270 int res;
2272 if (start)
2273 cmd[4] |= 1; /* START */
2275 if (sdp->start_stop_pwr_cond)
2276 cmd[4] |= start ? 1 << 4 : 3 << 4; /* Active or Standby */
2278 if (!scsi_device_online(sdp))
2279 return -ENODEV;
2281 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
2282 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
2283 if (res) {
2284 sd_printk(KERN_WARNING, sdkp, "START_STOP FAILED\n");
2285 sd_print_result(sdkp, res);
2286 if (driver_byte(res) & DRIVER_SENSE)
2287 sd_print_sense_hdr(sdkp, &sshdr);
2290 return res;
2294 * Send a SYNCHRONIZE CACHE instruction down to the device through
2295 * the normal SCSI command structure. Wait for the command to
2296 * complete.
2298 static void sd_shutdown(struct device *dev)
2300 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
2302 if (!sdkp)
2303 return; /* this can happen */
2305 if (sdkp->WCE) {
2306 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
2307 sd_sync_cache(sdkp);
2310 if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
2311 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
2312 sd_start_stop_device(sdkp, 0);
2315 scsi_disk_put(sdkp);
2318 static int sd_suspend(struct device *dev, pm_message_t mesg)
2320 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
2321 int ret = 0;
2323 if (!sdkp)
2324 return 0; /* this can happen */
2326 if (sdkp->WCE) {
2327 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
2328 ret = sd_sync_cache(sdkp);
2329 if (ret)
2330 goto done;
2333 if ((mesg.event & PM_EVENT_SLEEP) && sdkp->device->manage_start_stop) {
2334 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
2335 ret = sd_start_stop_device(sdkp, 0);
2338 done:
2339 scsi_disk_put(sdkp);
2340 return ret;
2343 static int sd_resume(struct device *dev)
2345 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
2346 int ret = 0;
2348 if (!sdkp->device->manage_start_stop)
2349 goto done;
2351 sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
2352 ret = sd_start_stop_device(sdkp, 1);
2354 done:
2355 scsi_disk_put(sdkp);
2356 return ret;
2360 * init_sd - entry point for this driver (both when built in or when
2361 * a module).
2363 * Note: this function registers this driver with the scsi mid-level.
2365 static int __init init_sd(void)
2367 int majors = 0, i, err;
2369 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
2371 for (i = 0; i < SD_MAJORS; i++)
2372 if (register_blkdev(sd_major(i), "sd") == 0)
2373 majors++;
2375 if (!majors)
2376 return -ENODEV;
2378 err = class_register(&sd_disk_class);
2379 if (err)
2380 goto err_out;
2382 err = scsi_register_driver(&sd_template.gendrv);
2383 if (err)
2384 goto err_out_class;
2386 sd_cdb_cache = kmem_cache_create("sd_ext_cdb", SD_EXT_CDB_SIZE,
2387 0, 0, NULL);
2388 if (!sd_cdb_cache) {
2389 printk(KERN_ERR "sd: can't init extended cdb cache\n");
2390 goto err_out_class;
2393 sd_cdb_pool = mempool_create_slab_pool(SD_MEMPOOL_SIZE, sd_cdb_cache);
2394 if (!sd_cdb_pool) {
2395 printk(KERN_ERR "sd: can't init extended cdb pool\n");
2396 goto err_out_cache;
2399 return 0;
2401 err_out_cache:
2402 kmem_cache_destroy(sd_cdb_cache);
2404 err_out_class:
2405 class_unregister(&sd_disk_class);
2406 err_out:
2407 for (i = 0; i < SD_MAJORS; i++)
2408 unregister_blkdev(sd_major(i), "sd");
2409 return err;
2413 * exit_sd - exit point for this driver (when it is a module).
2415 * Note: this function unregisters this driver from the scsi mid-level.
2417 static void __exit exit_sd(void)
2419 int i;
2421 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
2423 mempool_destroy(sd_cdb_pool);
2424 kmem_cache_destroy(sd_cdb_cache);
2426 scsi_unregister_driver(&sd_template.gendrv);
2427 class_unregister(&sd_disk_class);
2429 for (i = 0; i < SD_MAJORS; i++)
2430 unregister_blkdev(sd_major(i), "sd");
2433 module_init(init_sd);
2434 module_exit(exit_sd);
2436 static void sd_print_sense_hdr(struct scsi_disk *sdkp,
2437 struct scsi_sense_hdr *sshdr)
2439 sd_printk(KERN_INFO, sdkp, "");
2440 scsi_show_sense_hdr(sshdr);
2441 sd_printk(KERN_INFO, sdkp, "");
2442 scsi_show_extd_sense(sshdr->asc, sshdr->ascq);
2445 static void sd_print_result(struct scsi_disk *sdkp, int result)
2447 sd_printk(KERN_INFO, sdkp, "");
2448 scsi_show_result(result);