Remove GENHD_FL_DRIVERFS
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / sd.c
blob466fae8ef770daccbfa69ee85fc82054e6b22388
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 ssize_t
268 sd_show_thin_provisioning(struct device *dev, struct device_attribute *attr,
269 char *buf)
271 struct scsi_disk *sdkp = to_scsi_disk(dev);
273 return snprintf(buf, 20, "%u\n", sdkp->thin_provisioning);
276 static struct device_attribute sd_disk_attrs[] = {
277 __ATTR(cache_type, S_IRUGO|S_IWUSR, sd_show_cache_type,
278 sd_store_cache_type),
279 __ATTR(FUA, S_IRUGO, sd_show_fua, NULL),
280 __ATTR(allow_restart, S_IRUGO|S_IWUSR, sd_show_allow_restart,
281 sd_store_allow_restart),
282 __ATTR(manage_start_stop, S_IRUGO|S_IWUSR, sd_show_manage_start_stop,
283 sd_store_manage_start_stop),
284 __ATTR(protection_type, S_IRUGO, sd_show_protection_type, NULL),
285 __ATTR(app_tag_own, S_IRUGO, sd_show_app_tag_own, NULL),
286 __ATTR(thin_provisioning, S_IRUGO, sd_show_thin_provisioning, NULL),
287 __ATTR_NULL,
290 static struct class sd_disk_class = {
291 .name = "scsi_disk",
292 .owner = THIS_MODULE,
293 .dev_release = scsi_disk_release,
294 .dev_attrs = sd_disk_attrs,
297 static struct scsi_driver sd_template = {
298 .owner = THIS_MODULE,
299 .gendrv = {
300 .name = "sd",
301 .probe = sd_probe,
302 .remove = sd_remove,
303 .suspend = sd_suspend,
304 .resume = sd_resume,
305 .shutdown = sd_shutdown,
307 .rescan = sd_rescan,
308 .done = sd_done,
312 * Device no to disk mapping:
314 * major disc2 disc p1
315 * |............|.............|....|....| <- dev_t
316 * 31 20 19 8 7 4 3 0
318 * Inside a major, we have 16k disks, however mapped non-
319 * contiguously. The first 16 disks are for major0, the next
320 * ones with major1, ... Disk 256 is for major0 again, disk 272
321 * for major1, ...
322 * As we stay compatible with our numbering scheme, we can reuse
323 * the well-know SCSI majors 8, 65--71, 136--143.
325 static int sd_major(int major_idx)
327 switch (major_idx) {
328 case 0:
329 return SCSI_DISK0_MAJOR;
330 case 1 ... 7:
331 return SCSI_DISK1_MAJOR + major_idx - 1;
332 case 8 ... 15:
333 return SCSI_DISK8_MAJOR + major_idx - 8;
334 default:
335 BUG();
336 return 0; /* shut up gcc */
340 static struct scsi_disk *__scsi_disk_get(struct gendisk *disk)
342 struct scsi_disk *sdkp = NULL;
344 if (disk->private_data) {
345 sdkp = scsi_disk(disk);
346 if (scsi_device_get(sdkp->device) == 0)
347 get_device(&sdkp->dev);
348 else
349 sdkp = NULL;
351 return sdkp;
354 static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
356 struct scsi_disk *sdkp;
358 mutex_lock(&sd_ref_mutex);
359 sdkp = __scsi_disk_get(disk);
360 mutex_unlock(&sd_ref_mutex);
361 return sdkp;
364 static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev)
366 struct scsi_disk *sdkp;
368 mutex_lock(&sd_ref_mutex);
369 sdkp = dev_get_drvdata(dev);
370 if (sdkp)
371 sdkp = __scsi_disk_get(sdkp->disk);
372 mutex_unlock(&sd_ref_mutex);
373 return sdkp;
376 static void scsi_disk_put(struct scsi_disk *sdkp)
378 struct scsi_device *sdev = sdkp->device;
380 mutex_lock(&sd_ref_mutex);
381 put_device(&sdkp->dev);
382 scsi_device_put(sdev);
383 mutex_unlock(&sd_ref_mutex);
386 static void sd_prot_op(struct scsi_cmnd *scmd, unsigned int dif)
388 unsigned int prot_op = SCSI_PROT_NORMAL;
389 unsigned int dix = scsi_prot_sg_count(scmd);
391 if (scmd->sc_data_direction == DMA_FROM_DEVICE) {
392 if (dif && dix)
393 prot_op = SCSI_PROT_READ_PASS;
394 else if (dif && !dix)
395 prot_op = SCSI_PROT_READ_STRIP;
396 else if (!dif && dix)
397 prot_op = SCSI_PROT_READ_INSERT;
398 } else {
399 if (dif && dix)
400 prot_op = SCSI_PROT_WRITE_PASS;
401 else if (dif && !dix)
402 prot_op = SCSI_PROT_WRITE_INSERT;
403 else if (!dif && dix)
404 prot_op = SCSI_PROT_WRITE_STRIP;
407 scsi_set_prot_op(scmd, prot_op);
408 scsi_set_prot_type(scmd, dif);
412 * sd_prepare_discard - unmap blocks on thinly provisioned device
413 * @rq: Request to prepare
415 * Will issue either UNMAP or WRITE SAME(16) depending on preference
416 * indicated by target device.
418 static int sd_prepare_discard(struct request *rq)
420 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
421 struct bio *bio = rq->bio;
422 sector_t sector = bio->bi_sector;
423 unsigned int num = bio_sectors(bio);
425 if (sdkp->device->sector_size == 4096) {
426 sector >>= 3;
427 num >>= 3;
430 rq->cmd_type = REQ_TYPE_BLOCK_PC;
431 rq->timeout = SD_TIMEOUT;
433 memset(rq->cmd, 0, rq->cmd_len);
435 if (sdkp->unmap) {
436 char *buf = kmap_atomic(bio_page(bio), KM_USER0);
438 rq->cmd[0] = UNMAP;
439 rq->cmd[8] = 24;
440 rq->cmd_len = 10;
442 /* Ensure that data length matches payload */
443 rq->__data_len = bio->bi_size = bio->bi_io_vec->bv_len = 24;
445 put_unaligned_be16(6 + 16, &buf[0]);
446 put_unaligned_be16(16, &buf[2]);
447 put_unaligned_be64(sector, &buf[8]);
448 put_unaligned_be32(num, &buf[16]);
450 kunmap_atomic(buf, KM_USER0);
451 } else {
452 rq->cmd[0] = WRITE_SAME_16;
453 rq->cmd[1] = 0x8; /* UNMAP */
454 put_unaligned_be64(sector, &rq->cmd[2]);
455 put_unaligned_be32(num, &rq->cmd[10]);
456 rq->cmd_len = 16;
459 return BLKPREP_OK;
463 * sd_init_command - build a scsi (read or write) command from
464 * information in the request structure.
465 * @SCpnt: pointer to mid-level's per scsi command structure that
466 * contains request and into which the scsi command is written
468 * Returns 1 if successful and 0 if error (or cannot be done now).
470 static int sd_prep_fn(struct request_queue *q, struct request *rq)
472 struct scsi_cmnd *SCpnt;
473 struct scsi_device *sdp = q->queuedata;
474 struct gendisk *disk = rq->rq_disk;
475 struct scsi_disk *sdkp;
476 sector_t block = blk_rq_pos(rq);
477 sector_t threshold;
478 unsigned int this_count = blk_rq_sectors(rq);
479 int ret, host_dif;
480 unsigned char protect;
483 * Discard request come in as REQ_TYPE_FS but we turn them into
484 * block PC requests to make life easier.
486 if (blk_discard_rq(rq))
487 ret = sd_prepare_discard(rq);
489 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
490 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
491 goto out;
492 } else if (rq->cmd_type != REQ_TYPE_FS) {
493 ret = BLKPREP_KILL;
494 goto out;
496 ret = scsi_setup_fs_cmnd(sdp, rq);
497 if (ret != BLKPREP_OK)
498 goto out;
499 SCpnt = rq->special;
500 sdkp = scsi_disk(disk);
502 /* from here on until we're complete, any goto out
503 * is used for a killable error condition */
504 ret = BLKPREP_KILL;
506 SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
507 "sd_init_command: block=%llu, "
508 "count=%d\n",
509 (unsigned long long)block,
510 this_count));
512 if (!sdp || !scsi_device_online(sdp) ||
513 block + blk_rq_sectors(rq) > get_capacity(disk)) {
514 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
515 "Finishing %u sectors\n",
516 blk_rq_sectors(rq)));
517 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
518 "Retry with 0x%p\n", SCpnt));
519 goto out;
522 if (sdp->changed) {
524 * quietly refuse to do anything to a changed disc until
525 * the changed bit has been reset
527 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
528 goto out;
532 * Some SD card readers can't handle multi-sector accesses which touch
533 * the last one or two hardware sectors. Split accesses as needed.
535 threshold = get_capacity(disk) - SD_LAST_BUGGY_SECTORS *
536 (sdp->sector_size / 512);
538 if (unlikely(sdp->last_sector_bug && block + this_count > threshold)) {
539 if (block < threshold) {
540 /* Access up to the threshold but not beyond */
541 this_count = threshold - block;
542 } else {
543 /* Access only a single hardware sector */
544 this_count = sdp->sector_size / 512;
548 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n",
549 (unsigned long long)block));
552 * If we have a 1K hardware sectorsize, prevent access to single
553 * 512 byte sectors. In theory we could handle this - in fact
554 * the scsi cdrom driver must be able to handle this because
555 * we typically use 1K blocksizes, and cdroms typically have
556 * 2K hardware sectorsizes. Of course, things are simpler
557 * with the cdrom, since it is read-only. For performance
558 * reasons, the filesystems should be able to handle this
559 * and not force the scsi disk driver to use bounce buffers
560 * for this.
562 if (sdp->sector_size == 1024) {
563 if ((block & 1) || (blk_rq_sectors(rq) & 1)) {
564 scmd_printk(KERN_ERR, SCpnt,
565 "Bad block number requested\n");
566 goto out;
567 } else {
568 block = block >> 1;
569 this_count = this_count >> 1;
572 if (sdp->sector_size == 2048) {
573 if ((block & 3) || (blk_rq_sectors(rq) & 3)) {
574 scmd_printk(KERN_ERR, SCpnt,
575 "Bad block number requested\n");
576 goto out;
577 } else {
578 block = block >> 2;
579 this_count = this_count >> 2;
582 if (sdp->sector_size == 4096) {
583 if ((block & 7) || (blk_rq_sectors(rq) & 7)) {
584 scmd_printk(KERN_ERR, SCpnt,
585 "Bad block number requested\n");
586 goto out;
587 } else {
588 block = block >> 3;
589 this_count = this_count >> 3;
592 if (rq_data_dir(rq) == WRITE) {
593 if (!sdp->writeable) {
594 goto out;
596 SCpnt->cmnd[0] = WRITE_6;
597 SCpnt->sc_data_direction = DMA_TO_DEVICE;
599 if (blk_integrity_rq(rq) &&
600 sd_dif_prepare(rq, block, sdp->sector_size) == -EIO)
601 goto out;
603 } else if (rq_data_dir(rq) == READ) {
604 SCpnt->cmnd[0] = READ_6;
605 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
606 } else {
607 scmd_printk(KERN_ERR, SCpnt, "Unknown command %x\n", rq->cmd_flags);
608 goto out;
611 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
612 "%s %d/%u 512 byte blocks.\n",
613 (rq_data_dir(rq) == WRITE) ?
614 "writing" : "reading", this_count,
615 blk_rq_sectors(rq)));
617 /* Set RDPROTECT/WRPROTECT if disk is formatted with DIF */
618 host_dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type);
619 if (host_dif)
620 protect = 1 << 5;
621 else
622 protect = 0;
624 if (host_dif == SD_DIF_TYPE2_PROTECTION) {
625 SCpnt->cmnd = mempool_alloc(sd_cdb_pool, GFP_ATOMIC);
627 if (unlikely(SCpnt->cmnd == NULL)) {
628 ret = BLKPREP_DEFER;
629 goto out;
632 SCpnt->cmd_len = SD_EXT_CDB_SIZE;
633 memset(SCpnt->cmnd, 0, SCpnt->cmd_len);
634 SCpnt->cmnd[0] = VARIABLE_LENGTH_CMD;
635 SCpnt->cmnd[7] = 0x18;
636 SCpnt->cmnd[9] = (rq_data_dir(rq) == READ) ? READ_32 : WRITE_32;
637 SCpnt->cmnd[10] = protect | (blk_fua_rq(rq) ? 0x8 : 0);
639 /* LBA */
640 SCpnt->cmnd[12] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
641 SCpnt->cmnd[13] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
642 SCpnt->cmnd[14] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
643 SCpnt->cmnd[15] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
644 SCpnt->cmnd[16] = (unsigned char) (block >> 24) & 0xff;
645 SCpnt->cmnd[17] = (unsigned char) (block >> 16) & 0xff;
646 SCpnt->cmnd[18] = (unsigned char) (block >> 8) & 0xff;
647 SCpnt->cmnd[19] = (unsigned char) block & 0xff;
649 /* Expected Indirect LBA */
650 SCpnt->cmnd[20] = (unsigned char) (block >> 24) & 0xff;
651 SCpnt->cmnd[21] = (unsigned char) (block >> 16) & 0xff;
652 SCpnt->cmnd[22] = (unsigned char) (block >> 8) & 0xff;
653 SCpnt->cmnd[23] = (unsigned char) block & 0xff;
655 /* Transfer length */
656 SCpnt->cmnd[28] = (unsigned char) (this_count >> 24) & 0xff;
657 SCpnt->cmnd[29] = (unsigned char) (this_count >> 16) & 0xff;
658 SCpnt->cmnd[30] = (unsigned char) (this_count >> 8) & 0xff;
659 SCpnt->cmnd[31] = (unsigned char) this_count & 0xff;
660 } else if (block > 0xffffffff) {
661 SCpnt->cmnd[0] += READ_16 - READ_6;
662 SCpnt->cmnd[1] = protect | (blk_fua_rq(rq) ? 0x8 : 0);
663 SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
664 SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
665 SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
666 SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
667 SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
668 SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
669 SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
670 SCpnt->cmnd[9] = (unsigned char) block & 0xff;
671 SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
672 SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
673 SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
674 SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
675 SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
676 } else if ((this_count > 0xff) || (block > 0x1fffff) ||
677 scsi_device_protection(SCpnt->device) ||
678 SCpnt->device->use_10_for_rw) {
679 if (this_count > 0xffff)
680 this_count = 0xffff;
682 SCpnt->cmnd[0] += READ_10 - READ_6;
683 SCpnt->cmnd[1] = protect | (blk_fua_rq(rq) ? 0x8 : 0);
684 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
685 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
686 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
687 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
688 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
689 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
690 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
691 } else {
692 if (unlikely(blk_fua_rq(rq))) {
694 * This happens only if this drive failed
695 * 10byte rw command with ILLEGAL_REQUEST
696 * during operation and thus turned off
697 * use_10_for_rw.
699 scmd_printk(KERN_ERR, SCpnt,
700 "FUA write on READ/WRITE(6) drive\n");
701 goto out;
704 SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
705 SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
706 SCpnt->cmnd[3] = (unsigned char) block & 0xff;
707 SCpnt->cmnd[4] = (unsigned char) this_count;
708 SCpnt->cmnd[5] = 0;
710 SCpnt->sdb.length = this_count * sdp->sector_size;
712 /* If DIF or DIX is enabled, tell HBA how to handle request */
713 if (host_dif || scsi_prot_sg_count(SCpnt))
714 sd_prot_op(SCpnt, host_dif);
717 * We shouldn't disconnect in the middle of a sector, so with a dumb
718 * host adapter, it's safe to assume that we can at least transfer
719 * this many bytes between each connect / disconnect.
721 SCpnt->transfersize = sdp->sector_size;
722 SCpnt->underflow = this_count << 9;
723 SCpnt->allowed = SD_MAX_RETRIES;
726 * This indicates that the command is ready from our end to be
727 * queued.
729 ret = BLKPREP_OK;
730 out:
731 return scsi_prep_return(q, rq, ret);
735 * sd_open - open a scsi disk device
736 * @inode: only i_rdev member may be used
737 * @filp: only f_mode and f_flags may be used
739 * Returns 0 if successful. Returns a negated errno value in case
740 * of error.
742 * Note: This can be called from a user context (e.g. fsck(1) )
743 * or from within the kernel (e.g. as a result of a mount(1) ).
744 * In the latter case @inode and @filp carry an abridged amount
745 * of information as noted above.
747 static int sd_open(struct block_device *bdev, fmode_t mode)
749 struct scsi_disk *sdkp = scsi_disk_get(bdev->bd_disk);
750 struct scsi_device *sdev;
751 int retval;
753 if (!sdkp)
754 return -ENXIO;
756 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
758 sdev = sdkp->device;
761 * If the device is in error recovery, wait until it is done.
762 * If the device is offline, then disallow any access to it.
764 retval = -ENXIO;
765 if (!scsi_block_when_processing_errors(sdev))
766 goto error_out;
768 if (sdev->removable || sdkp->write_prot)
769 check_disk_change(bdev);
772 * If the drive is empty, just let the open fail.
774 retval = -ENOMEDIUM;
775 if (sdev->removable && !sdkp->media_present && !(mode & FMODE_NDELAY))
776 goto error_out;
779 * If the device has the write protect tab set, have the open fail
780 * if the user expects to be able to write to the thing.
782 retval = -EROFS;
783 if (sdkp->write_prot && (mode & FMODE_WRITE))
784 goto error_out;
787 * It is possible that the disk changing stuff resulted in
788 * the device being taken offline. If this is the case,
789 * report this to the user, and don't pretend that the
790 * open actually succeeded.
792 retval = -ENXIO;
793 if (!scsi_device_online(sdev))
794 goto error_out;
796 if (!sdkp->openers++ && sdev->removable) {
797 if (scsi_block_when_processing_errors(sdev))
798 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
801 return 0;
803 error_out:
804 scsi_disk_put(sdkp);
805 return retval;
809 * sd_release - invoked when the (last) close(2) is called on this
810 * scsi disk.
811 * @inode: only i_rdev member may be used
812 * @filp: only f_mode and f_flags may be used
814 * Returns 0.
816 * Note: may block (uninterruptible) if error recovery is underway
817 * on this disk.
819 static int sd_release(struct gendisk *disk, fmode_t mode)
821 struct scsi_disk *sdkp = scsi_disk(disk);
822 struct scsi_device *sdev = sdkp->device;
824 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
826 if (!--sdkp->openers && sdev->removable) {
827 if (scsi_block_when_processing_errors(sdev))
828 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
832 * XXX and what if there are packets in flight and this close()
833 * XXX is followed by a "rmmod sd_mod"?
835 scsi_disk_put(sdkp);
836 return 0;
839 static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
841 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
842 struct scsi_device *sdp = sdkp->device;
843 struct Scsi_Host *host = sdp->host;
844 int diskinfo[4];
846 /* default to most commonly used values */
847 diskinfo[0] = 0x40; /* 1 << 6 */
848 diskinfo[1] = 0x20; /* 1 << 5 */
849 diskinfo[2] = sdkp->capacity >> 11;
851 /* override with calculated, extended default, or driver values */
852 if (host->hostt->bios_param)
853 host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo);
854 else
855 scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
857 geo->heads = diskinfo[0];
858 geo->sectors = diskinfo[1];
859 geo->cylinders = diskinfo[2];
860 return 0;
864 * sd_ioctl - process an ioctl
865 * @inode: only i_rdev/i_bdev members may be used
866 * @filp: only f_mode and f_flags may be used
867 * @cmd: ioctl command number
868 * @arg: this is third argument given to ioctl(2) system call.
869 * Often contains a pointer.
871 * Returns 0 if successful (some ioctls return postive numbers on
872 * success as well). Returns a negated errno value in case of error.
874 * Note: most ioctls are forward onto the block subsystem or further
875 * down in the scsi subsystem.
877 static int sd_ioctl(struct block_device *bdev, fmode_t mode,
878 unsigned int cmd, unsigned long arg)
880 struct gendisk *disk = bdev->bd_disk;
881 struct scsi_device *sdp = scsi_disk(disk)->device;
882 void __user *p = (void __user *)arg;
883 int error;
885 SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
886 disk->disk_name, cmd));
889 * If we are in the middle of error recovery, don't let anyone
890 * else try and use this device. Also, if error recovery fails, it
891 * may try and take the device offline, in which case all further
892 * access to the device is prohibited.
894 error = scsi_nonblockable_ioctl(sdp, cmd, p,
895 (mode & FMODE_NDELAY) != 0);
896 if (!scsi_block_when_processing_errors(sdp) || !error)
897 return error;
900 * Send SCSI addressing ioctls directly to mid level, send other
901 * ioctls to block level and then onto mid level if they can't be
902 * resolved.
904 switch (cmd) {
905 case SCSI_IOCTL_GET_IDLUN:
906 case SCSI_IOCTL_GET_BUS_NUMBER:
907 return scsi_ioctl(sdp, cmd, p);
908 default:
909 error = scsi_cmd_ioctl(disk->queue, disk, mode, cmd, p);
910 if (error != -ENOTTY)
911 return error;
913 return scsi_ioctl(sdp, cmd, p);
916 static void set_media_not_present(struct scsi_disk *sdkp)
918 sdkp->media_present = 0;
919 sdkp->capacity = 0;
920 sdkp->device->changed = 1;
924 * sd_media_changed - check if our medium changed
925 * @disk: kernel device descriptor
927 * Returns 0 if not applicable or no change; 1 if change
929 * Note: this function is invoked from the block subsystem.
931 static int sd_media_changed(struct gendisk *disk)
933 struct scsi_disk *sdkp = scsi_disk(disk);
934 struct scsi_device *sdp = sdkp->device;
935 struct scsi_sense_hdr *sshdr = NULL;
936 int retval;
938 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_media_changed\n"));
940 if (!sdp->removable)
941 return 0;
944 * If the device is offline, don't send any commands - just pretend as
945 * if the command failed. If the device ever comes back online, we
946 * can deal with it then. It is only because of unrecoverable errors
947 * that we would ever take a device offline in the first place.
949 if (!scsi_device_online(sdp)) {
950 set_media_not_present(sdkp);
951 retval = 1;
952 goto out;
956 * Using TEST_UNIT_READY enables differentiation between drive with
957 * no cartridge loaded - NOT READY, drive with changed cartridge -
958 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
960 * Drives that auto spin down. eg iomega jaz 1G, will be started
961 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
962 * sd_revalidate() is called.
964 retval = -ENODEV;
966 if (scsi_block_when_processing_errors(sdp)) {
967 sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
968 retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES,
969 sshdr);
973 * Unable to test, unit probably not ready. This usually
974 * means there is no disc in the drive. Mark as changed,
975 * and we will figure it out later once the drive is
976 * available again.
978 if (retval || (scsi_sense_valid(sshdr) &&
979 /* 0x3a is medium not present */
980 sshdr->asc == 0x3a)) {
981 set_media_not_present(sdkp);
982 retval = 1;
983 goto out;
987 * For removable scsi disk we have to recognise the presence
988 * of a disk in the drive. This is kept in the struct scsi_disk
989 * struct and tested at open ! Daniel Roche (dan@lectra.fr)
991 sdkp->media_present = 1;
993 retval = sdp->changed;
994 sdp->changed = 0;
995 out:
996 if (retval != sdkp->previous_state)
997 sdev_evt_send_simple(sdp, SDEV_EVT_MEDIA_CHANGE, GFP_KERNEL);
998 sdkp->previous_state = retval;
999 kfree(sshdr);
1000 return retval;
1003 static int sd_sync_cache(struct scsi_disk *sdkp)
1005 int retries, res;
1006 struct scsi_device *sdp = sdkp->device;
1007 struct scsi_sense_hdr sshdr;
1009 if (!scsi_device_online(sdp))
1010 return -ENODEV;
1013 for (retries = 3; retries > 0; --retries) {
1014 unsigned char cmd[10] = { 0 };
1016 cmd[0] = SYNCHRONIZE_CACHE;
1018 * Leave the rest of the command zero to indicate
1019 * flush everything.
1021 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
1022 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
1023 if (res == 0)
1024 break;
1027 if (res) {
1028 sd_print_result(sdkp, res);
1029 if (driver_byte(res) & DRIVER_SENSE)
1030 sd_print_sense_hdr(sdkp, &sshdr);
1033 if (res)
1034 return -EIO;
1035 return 0;
1038 static void sd_prepare_flush(struct request_queue *q, struct request *rq)
1040 rq->cmd_type = REQ_TYPE_BLOCK_PC;
1041 rq->timeout = SD_TIMEOUT;
1042 rq->cmd[0] = SYNCHRONIZE_CACHE;
1043 rq->cmd_len = 10;
1046 static void sd_rescan(struct device *dev)
1048 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
1050 if (sdkp) {
1051 revalidate_disk(sdkp->disk);
1052 scsi_disk_put(sdkp);
1057 #ifdef CONFIG_COMPAT
1059 * This gets directly called from VFS. When the ioctl
1060 * is not recognized we go back to the other translation paths.
1062 static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode,
1063 unsigned int cmd, unsigned long arg)
1065 struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
1068 * If we are in the middle of error recovery, don't let anyone
1069 * else try and use this device. Also, if error recovery fails, it
1070 * may try and take the device offline, in which case all further
1071 * access to the device is prohibited.
1073 if (!scsi_block_when_processing_errors(sdev))
1074 return -ENODEV;
1076 if (sdev->host->hostt->compat_ioctl) {
1077 int ret;
1079 ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
1081 return ret;
1085 * Let the static ioctl translation table take care of it.
1087 return -ENOIOCTLCMD;
1089 #endif
1091 static const struct block_device_operations sd_fops = {
1092 .owner = THIS_MODULE,
1093 .open = sd_open,
1094 .release = sd_release,
1095 .locked_ioctl = sd_ioctl,
1096 .getgeo = sd_getgeo,
1097 #ifdef CONFIG_COMPAT
1098 .compat_ioctl = sd_compat_ioctl,
1099 #endif
1100 .media_changed = sd_media_changed,
1101 .revalidate_disk = sd_revalidate_disk,
1104 static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
1106 u64 start_lba = blk_rq_pos(scmd->request);
1107 u64 end_lba = blk_rq_pos(scmd->request) + (scsi_bufflen(scmd) / 512);
1108 u64 bad_lba;
1109 int info_valid;
1111 if (!blk_fs_request(scmd->request))
1112 return 0;
1114 info_valid = scsi_get_sense_info_fld(scmd->sense_buffer,
1115 SCSI_SENSE_BUFFERSIZE,
1116 &bad_lba);
1117 if (!info_valid)
1118 return 0;
1120 if (scsi_bufflen(scmd) <= scmd->device->sector_size)
1121 return 0;
1123 if (scmd->device->sector_size < 512) {
1124 /* only legitimate sector_size here is 256 */
1125 start_lba <<= 1;
1126 end_lba <<= 1;
1127 } else {
1128 /* be careful ... don't want any overflows */
1129 u64 factor = scmd->device->sector_size / 512;
1130 do_div(start_lba, factor);
1131 do_div(end_lba, factor);
1134 /* The bad lba was reported incorrectly, we have no idea where
1135 * the error is.
1137 if (bad_lba < start_lba || bad_lba >= end_lba)
1138 return 0;
1140 /* This computation should always be done in terms of
1141 * the resolution of the device's medium.
1143 return (bad_lba - start_lba) * scmd->device->sector_size;
1147 * sd_done - bottom half handler: called when the lower level
1148 * driver has completed (successfully or otherwise) a scsi command.
1149 * @SCpnt: mid-level's per command structure.
1151 * Note: potentially run from within an ISR. Must not block.
1153 static int sd_done(struct scsi_cmnd *SCpnt)
1155 int result = SCpnt->result;
1156 unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt);
1157 struct scsi_sense_hdr sshdr;
1158 struct scsi_disk *sdkp = scsi_disk(SCpnt->request->rq_disk);
1159 int sense_valid = 0;
1160 int sense_deferred = 0;
1162 if (result) {
1163 sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
1164 if (sense_valid)
1165 sense_deferred = scsi_sense_is_deferred(&sshdr);
1167 #ifdef CONFIG_SCSI_LOGGING
1168 SCSI_LOG_HLCOMPLETE(1, scsi_print_result(SCpnt));
1169 if (sense_valid) {
1170 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
1171 "sd_done: sb[respc,sk,asc,"
1172 "ascq]=%x,%x,%x,%x\n",
1173 sshdr.response_code,
1174 sshdr.sense_key, sshdr.asc,
1175 sshdr.ascq));
1177 #endif
1178 if (driver_byte(result) != DRIVER_SENSE &&
1179 (!sense_valid || sense_deferred))
1180 goto out;
1182 switch (sshdr.sense_key) {
1183 case HARDWARE_ERROR:
1184 case MEDIUM_ERROR:
1185 good_bytes = sd_completed_bytes(SCpnt);
1186 break;
1187 case RECOVERED_ERROR:
1188 good_bytes = scsi_bufflen(SCpnt);
1189 break;
1190 case NO_SENSE:
1191 /* This indicates a false check condition, so ignore it. An
1192 * unknown amount of data was transferred so treat it as an
1193 * error.
1195 scsi_print_sense("sd", SCpnt);
1196 SCpnt->result = 0;
1197 memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1198 break;
1199 case ABORTED_COMMAND: /* DIF: Target detected corruption */
1200 case ILLEGAL_REQUEST: /* DIX: Host detected corruption */
1201 if (sshdr.asc == 0x10)
1202 good_bytes = sd_completed_bytes(SCpnt);
1203 break;
1204 default:
1205 break;
1207 out:
1208 if (rq_data_dir(SCpnt->request) == READ && scsi_prot_sg_count(SCpnt))
1209 sd_dif_complete(SCpnt, good_bytes);
1211 if (scsi_host_dif_capable(sdkp->device->host, sdkp->protection_type)
1212 == SD_DIF_TYPE2_PROTECTION && SCpnt->cmnd != SCpnt->request->cmd) {
1214 /* We have to print a failed command here as the
1215 * extended CDB gets freed before scsi_io_completion()
1216 * is called.
1218 if (result)
1219 scsi_print_command(SCpnt);
1221 mempool_free(SCpnt->cmnd, sd_cdb_pool);
1222 SCpnt->cmnd = NULL;
1223 SCpnt->cmd_len = 0;
1226 return good_bytes;
1229 static int media_not_present(struct scsi_disk *sdkp,
1230 struct scsi_sense_hdr *sshdr)
1233 if (!scsi_sense_valid(sshdr))
1234 return 0;
1235 /* not invoked for commands that could return deferred errors */
1236 if (sshdr->sense_key != NOT_READY &&
1237 sshdr->sense_key != UNIT_ATTENTION)
1238 return 0;
1239 if (sshdr->asc != 0x3A) /* medium not present */
1240 return 0;
1242 set_media_not_present(sdkp);
1243 return 1;
1247 * spinup disk - called only in sd_revalidate_disk()
1249 static void
1250 sd_spinup_disk(struct scsi_disk *sdkp)
1252 unsigned char cmd[10];
1253 unsigned long spintime_expire = 0;
1254 int retries, spintime;
1255 unsigned int the_result;
1256 struct scsi_sense_hdr sshdr;
1257 int sense_valid = 0;
1259 spintime = 0;
1261 /* Spin up drives, as required. Only do this at boot time */
1262 /* Spinup needs to be done for module loads too. */
1263 do {
1264 retries = 0;
1266 do {
1267 cmd[0] = TEST_UNIT_READY;
1268 memset((void *) &cmd[1], 0, 9);
1270 the_result = scsi_execute_req(sdkp->device, cmd,
1271 DMA_NONE, NULL, 0,
1272 &sshdr, SD_TIMEOUT,
1273 SD_MAX_RETRIES, NULL);
1276 * If the drive has indicated to us that it
1277 * doesn't have any media in it, don't bother
1278 * with any more polling.
1280 if (media_not_present(sdkp, &sshdr))
1281 return;
1283 if (the_result)
1284 sense_valid = scsi_sense_valid(&sshdr);
1285 retries++;
1286 } while (retries < 3 &&
1287 (!scsi_status_is_good(the_result) ||
1288 ((driver_byte(the_result) & DRIVER_SENSE) &&
1289 sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
1291 if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
1292 /* no sense, TUR either succeeded or failed
1293 * with a status error */
1294 if(!spintime && !scsi_status_is_good(the_result)) {
1295 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1296 sd_print_result(sdkp, the_result);
1298 break;
1302 * The device does not want the automatic start to be issued.
1304 if (sdkp->device->no_start_on_add)
1305 break;
1307 if (sense_valid && sshdr.sense_key == NOT_READY) {
1308 if (sshdr.asc == 4 && sshdr.ascq == 3)
1309 break; /* manual intervention required */
1310 if (sshdr.asc == 4 && sshdr.ascq == 0xb)
1311 break; /* standby */
1312 if (sshdr.asc == 4 && sshdr.ascq == 0xc)
1313 break; /* unavailable */
1315 * Issue command to spin up drive when not ready
1317 if (!spintime) {
1318 sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
1319 cmd[0] = START_STOP;
1320 cmd[1] = 1; /* Return immediately */
1321 memset((void *) &cmd[2], 0, 8);
1322 cmd[4] = 1; /* Start spin cycle */
1323 if (sdkp->device->start_stop_pwr_cond)
1324 cmd[4] |= 1 << 4;
1325 scsi_execute_req(sdkp->device, cmd, DMA_NONE,
1326 NULL, 0, &sshdr,
1327 SD_TIMEOUT, SD_MAX_RETRIES,
1328 NULL);
1329 spintime_expire = jiffies + 100 * HZ;
1330 spintime = 1;
1332 /* Wait 1 second for next try */
1333 msleep(1000);
1334 printk(".");
1337 * Wait for USB flash devices with slow firmware.
1338 * Yes, this sense key/ASC combination shouldn't
1339 * occur here. It's characteristic of these devices.
1341 } else if (sense_valid &&
1342 sshdr.sense_key == UNIT_ATTENTION &&
1343 sshdr.asc == 0x28) {
1344 if (!spintime) {
1345 spintime_expire = jiffies + 5 * HZ;
1346 spintime = 1;
1348 /* Wait 1 second for next try */
1349 msleep(1000);
1350 } else {
1351 /* we don't understand the sense code, so it's
1352 * probably pointless to loop */
1353 if(!spintime) {
1354 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1355 sd_print_sense_hdr(sdkp, &sshdr);
1357 break;
1360 } while (spintime && time_before_eq(jiffies, spintime_expire));
1362 if (spintime) {
1363 if (scsi_status_is_good(the_result))
1364 printk("ready\n");
1365 else
1366 printk("not responding...\n");
1372 * Determine whether disk supports Data Integrity Field.
1374 void sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer)
1376 struct scsi_device *sdp = sdkp->device;
1377 u8 type;
1379 if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0)
1380 return;
1382 type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */
1384 if (type == sdkp->protection_type || !sdkp->first_scan)
1385 return;
1387 sdkp->protection_type = type;
1389 if (type > SD_DIF_TYPE3_PROTECTION) {
1390 sd_printk(KERN_ERR, sdkp, "formatted with unsupported " \
1391 "protection type %u. Disabling disk!\n", type);
1392 sdkp->capacity = 0;
1393 return;
1396 if (scsi_host_dif_capable(sdp->host, type))
1397 sd_printk(KERN_NOTICE, sdkp,
1398 "Enabling DIF Type %u protection\n", type);
1399 else
1400 sd_printk(KERN_NOTICE, sdkp,
1401 "Disabling DIF Type %u protection\n", type);
1404 static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp,
1405 struct scsi_sense_hdr *sshdr, int sense_valid,
1406 int the_result)
1408 sd_print_result(sdkp, the_result);
1409 if (driver_byte(the_result) & DRIVER_SENSE)
1410 sd_print_sense_hdr(sdkp, sshdr);
1411 else
1412 sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
1415 * Set dirty bit for removable devices if not ready -
1416 * sometimes drives will not report this properly.
1418 if (sdp->removable &&
1419 sense_valid && sshdr->sense_key == NOT_READY)
1420 sdp->changed = 1;
1423 * We used to set media_present to 0 here to indicate no media
1424 * in the drive, but some drives fail read capacity even with
1425 * media present, so we can't do that.
1427 sdkp->capacity = 0; /* unknown mapped to zero - as usual */
1430 #define RC16_LEN 32
1431 #if RC16_LEN > SD_BUF_SIZE
1432 #error RC16_LEN must not be more than SD_BUF_SIZE
1433 #endif
1435 static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
1436 unsigned char *buffer)
1438 unsigned char cmd[16];
1439 struct scsi_sense_hdr sshdr;
1440 int sense_valid = 0;
1441 int the_result;
1442 int retries = 3;
1443 unsigned int alignment;
1444 unsigned long long lba;
1445 unsigned sector_size;
1447 do {
1448 memset(cmd, 0, 16);
1449 cmd[0] = SERVICE_ACTION_IN;
1450 cmd[1] = SAI_READ_CAPACITY_16;
1451 cmd[13] = RC16_LEN;
1452 memset(buffer, 0, RC16_LEN);
1454 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
1455 buffer, RC16_LEN, &sshdr,
1456 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
1458 if (media_not_present(sdkp, &sshdr))
1459 return -ENODEV;
1461 if (the_result) {
1462 sense_valid = scsi_sense_valid(&sshdr);
1463 if (sense_valid &&
1464 sshdr.sense_key == ILLEGAL_REQUEST &&
1465 (sshdr.asc == 0x20 || sshdr.asc == 0x24) &&
1466 sshdr.ascq == 0x00)
1467 /* Invalid Command Operation Code or
1468 * Invalid Field in CDB, just retry
1469 * silently with RC10 */
1470 return -EINVAL;
1472 retries--;
1474 } while (the_result && retries);
1476 if (the_result) {
1477 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY(16) failed\n");
1478 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
1479 return -EINVAL;
1482 sector_size = get_unaligned_be32(&buffer[8]);
1483 lba = get_unaligned_be64(&buffer[0]);
1485 sd_read_protection_type(sdkp, buffer);
1487 if ((sizeof(sdkp->capacity) == 4) && (lba >= 0xffffffffULL)) {
1488 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
1489 "kernel compiled with support for large block "
1490 "devices.\n");
1491 sdkp->capacity = 0;
1492 return -EOVERFLOW;
1495 /* Logical blocks per physical block exponent */
1496 sdkp->hw_sector_size = (1 << (buffer[13] & 0xf)) * sector_size;
1498 /* Lowest aligned logical block */
1499 alignment = ((buffer[14] & 0x3f) << 8 | buffer[15]) * sector_size;
1500 blk_queue_alignment_offset(sdp->request_queue, alignment);
1501 if (alignment && sdkp->first_scan)
1502 sd_printk(KERN_NOTICE, sdkp,
1503 "physical block alignment offset: %u\n", alignment);
1505 if (buffer[14] & 0x80) { /* TPE */
1506 struct request_queue *q = sdp->request_queue;
1508 sdkp->thin_provisioning = 1;
1509 q->limits.discard_granularity = sdkp->hw_sector_size;
1510 q->limits.max_discard_sectors = 0xffffffff;
1512 if (buffer[14] & 0x40) /* TPRZ */
1513 q->limits.discard_zeroes_data = 1;
1515 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
1518 sdkp->capacity = lba + 1;
1519 return sector_size;
1522 static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,
1523 unsigned char *buffer)
1525 unsigned char cmd[16];
1526 struct scsi_sense_hdr sshdr;
1527 int sense_valid = 0;
1528 int the_result;
1529 int retries = 3;
1530 sector_t lba;
1531 unsigned sector_size;
1533 do {
1534 cmd[0] = READ_CAPACITY;
1535 memset(&cmd[1], 0, 9);
1536 memset(buffer, 0, 8);
1538 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
1539 buffer, 8, &sshdr,
1540 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
1542 if (media_not_present(sdkp, &sshdr))
1543 return -ENODEV;
1545 if (the_result)
1546 sense_valid = scsi_sense_valid(&sshdr);
1547 retries--;
1549 } while (the_result && retries);
1551 if (the_result) {
1552 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY failed\n");
1553 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
1554 return -EINVAL;
1557 sector_size = get_unaligned_be32(&buffer[4]);
1558 lba = get_unaligned_be32(&buffer[0]);
1560 if ((sizeof(sdkp->capacity) == 4) && (lba == 0xffffffff)) {
1561 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
1562 "kernel compiled with support for large block "
1563 "devices.\n");
1564 sdkp->capacity = 0;
1565 return -EOVERFLOW;
1568 sdkp->capacity = lba + 1;
1569 sdkp->hw_sector_size = sector_size;
1570 return sector_size;
1573 static int sd_try_rc16_first(struct scsi_device *sdp)
1575 if (sdp->scsi_level > SCSI_SPC_2)
1576 return 1;
1577 if (scsi_device_protection(sdp))
1578 return 1;
1579 return 0;
1583 * read disk capacity
1585 static void
1586 sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
1588 int sector_size;
1589 struct scsi_device *sdp = sdkp->device;
1590 sector_t old_capacity = sdkp->capacity;
1592 if (sd_try_rc16_first(sdp)) {
1593 sector_size = read_capacity_16(sdkp, sdp, buffer);
1594 if (sector_size == -EOVERFLOW)
1595 goto got_data;
1596 if (sector_size == -ENODEV)
1597 return;
1598 if (sector_size < 0)
1599 sector_size = read_capacity_10(sdkp, sdp, buffer);
1600 if (sector_size < 0)
1601 return;
1602 } else {
1603 sector_size = read_capacity_10(sdkp, sdp, buffer);
1604 if (sector_size == -EOVERFLOW)
1605 goto got_data;
1606 if (sector_size < 0)
1607 return;
1608 if ((sizeof(sdkp->capacity) > 4) &&
1609 (sdkp->capacity > 0xffffffffULL)) {
1610 int old_sector_size = sector_size;
1611 sd_printk(KERN_NOTICE, sdkp, "Very big device. "
1612 "Trying to use READ CAPACITY(16).\n");
1613 sector_size = read_capacity_16(sdkp, sdp, buffer);
1614 if (sector_size < 0) {
1615 sd_printk(KERN_NOTICE, sdkp,
1616 "Using 0xffffffff as device size\n");
1617 sdkp->capacity = 1 + (sector_t) 0xffffffff;
1618 sector_size = old_sector_size;
1619 goto got_data;
1624 /* Some devices are known to return the total number of blocks,
1625 * not the highest block number. Some devices have versions
1626 * which do this and others which do not. Some devices we might
1627 * suspect of doing this but we don't know for certain.
1629 * If we know the reported capacity is wrong, decrement it. If
1630 * we can only guess, then assume the number of blocks is even
1631 * (usually true but not always) and err on the side of lowering
1632 * the capacity.
1634 if (sdp->fix_capacity ||
1635 (sdp->guess_capacity && (sdkp->capacity & 0x01))) {
1636 sd_printk(KERN_INFO, sdkp, "Adjusting the sector count "
1637 "from its reported value: %llu\n",
1638 (unsigned long long) sdkp->capacity);
1639 --sdkp->capacity;
1642 got_data:
1643 if (sector_size == 0) {
1644 sector_size = 512;
1645 sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
1646 "assuming 512.\n");
1649 if (sector_size != 512 &&
1650 sector_size != 1024 &&
1651 sector_size != 2048 &&
1652 sector_size != 4096 &&
1653 sector_size != 256) {
1654 sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
1655 sector_size);
1657 * The user might want to re-format the drive with
1658 * a supported sectorsize. Once this happens, it
1659 * would be relatively trivial to set the thing up.
1660 * For this reason, we leave the thing in the table.
1662 sdkp->capacity = 0;
1664 * set a bogus sector size so the normal read/write
1665 * logic in the block layer will eventually refuse any
1666 * request on this device without tripping over power
1667 * of two sector size assumptions
1669 sector_size = 512;
1671 blk_queue_logical_block_size(sdp->request_queue, sector_size);
1674 char cap_str_2[10], cap_str_10[10];
1675 u64 sz = (u64)sdkp->capacity << ilog2(sector_size);
1677 string_get_size(sz, STRING_UNITS_2, cap_str_2,
1678 sizeof(cap_str_2));
1679 string_get_size(sz, STRING_UNITS_10, cap_str_10,
1680 sizeof(cap_str_10));
1682 if (sdkp->first_scan || old_capacity != sdkp->capacity) {
1683 sd_printk(KERN_NOTICE, sdkp,
1684 "%llu %d-byte logical blocks: (%s/%s)\n",
1685 (unsigned long long)sdkp->capacity,
1686 sector_size, cap_str_10, cap_str_2);
1688 if (sdkp->hw_sector_size != sector_size)
1689 sd_printk(KERN_NOTICE, sdkp,
1690 "%u-byte physical blocks\n",
1691 sdkp->hw_sector_size);
1695 /* Rescale capacity to 512-byte units */
1696 if (sector_size == 4096)
1697 sdkp->capacity <<= 3;
1698 else if (sector_size == 2048)
1699 sdkp->capacity <<= 2;
1700 else if (sector_size == 1024)
1701 sdkp->capacity <<= 1;
1702 else if (sector_size == 256)
1703 sdkp->capacity >>= 1;
1705 blk_queue_physical_block_size(sdp->request_queue, sdkp->hw_sector_size);
1706 sdkp->device->sector_size = sector_size;
1709 /* called with buffer of length 512 */
1710 static inline int
1711 sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
1712 unsigned char *buffer, int len, struct scsi_mode_data *data,
1713 struct scsi_sense_hdr *sshdr)
1715 return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
1716 SD_TIMEOUT, SD_MAX_RETRIES, data,
1717 sshdr);
1721 * read write protect setting, if possible - called only in sd_revalidate_disk()
1722 * called with buffer of length SD_BUF_SIZE
1724 static void
1725 sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
1727 int res;
1728 struct scsi_device *sdp = sdkp->device;
1729 struct scsi_mode_data data;
1730 int old_wp = sdkp->write_prot;
1732 set_disk_ro(sdkp->disk, 0);
1733 if (sdp->skip_ms_page_3f) {
1734 sd_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
1735 return;
1738 if (sdp->use_192_bytes_for_3f) {
1739 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
1740 } else {
1742 * First attempt: ask for all pages (0x3F), but only 4 bytes.
1743 * We have to start carefully: some devices hang if we ask
1744 * for more than is available.
1746 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
1749 * Second attempt: ask for page 0 When only page 0 is
1750 * implemented, a request for page 3F may return Sense Key
1751 * 5: Illegal Request, Sense Code 24: Invalid field in
1752 * CDB.
1754 if (!scsi_status_is_good(res))
1755 res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
1758 * Third attempt: ask 255 bytes, as we did earlier.
1760 if (!scsi_status_is_good(res))
1761 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
1762 &data, NULL);
1765 if (!scsi_status_is_good(res)) {
1766 sd_printk(KERN_WARNING, sdkp,
1767 "Test WP failed, assume Write Enabled\n");
1768 } else {
1769 sdkp->write_prot = ((data.device_specific & 0x80) != 0);
1770 set_disk_ro(sdkp->disk, sdkp->write_prot);
1771 if (sdkp->first_scan || old_wp != sdkp->write_prot) {
1772 sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
1773 sdkp->write_prot ? "on" : "off");
1774 sd_printk(KERN_DEBUG, sdkp,
1775 "Mode Sense: %02x %02x %02x %02x\n",
1776 buffer[0], buffer[1], buffer[2], buffer[3]);
1782 * sd_read_cache_type - called only from sd_revalidate_disk()
1783 * called with buffer of length SD_BUF_SIZE
1785 static void
1786 sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
1788 int len = 0, res;
1789 struct scsi_device *sdp = sdkp->device;
1791 int dbd;
1792 int modepage;
1793 struct scsi_mode_data data;
1794 struct scsi_sense_hdr sshdr;
1795 int old_wce = sdkp->WCE;
1796 int old_rcd = sdkp->RCD;
1797 int old_dpofua = sdkp->DPOFUA;
1799 if (sdp->skip_ms_page_8)
1800 goto defaults;
1802 if (sdp->type == TYPE_RBC) {
1803 modepage = 6;
1804 dbd = 8;
1805 } else {
1806 modepage = 8;
1807 dbd = 0;
1810 /* cautiously ask */
1811 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr);
1813 if (!scsi_status_is_good(res))
1814 goto bad_sense;
1816 if (!data.header_length) {
1817 modepage = 6;
1818 sd_printk(KERN_ERR, sdkp, "Missing header in MODE_SENSE response\n");
1821 /* that went OK, now ask for the proper length */
1822 len = data.length;
1825 * We're only interested in the first three bytes, actually.
1826 * But the data cache page is defined for the first 20.
1828 if (len < 3)
1829 goto bad_sense;
1830 if (len > 20)
1831 len = 20;
1833 /* Take headers and block descriptors into account */
1834 len += data.header_length + data.block_descriptor_length;
1835 if (len > SD_BUF_SIZE)
1836 goto bad_sense;
1838 /* Get the data */
1839 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr);
1841 if (scsi_status_is_good(res)) {
1842 int offset = data.header_length + data.block_descriptor_length;
1844 if (offset >= SD_BUF_SIZE - 2) {
1845 sd_printk(KERN_ERR, sdkp, "Malformed MODE SENSE response\n");
1846 goto defaults;
1849 if ((buffer[offset] & 0x3f) != modepage) {
1850 sd_printk(KERN_ERR, sdkp, "Got wrong page\n");
1851 goto defaults;
1854 if (modepage == 8) {
1855 sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
1856 sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
1857 } else {
1858 sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
1859 sdkp->RCD = 0;
1862 sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
1863 if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
1864 sd_printk(KERN_NOTICE, sdkp,
1865 "Uses READ/WRITE(6), disabling FUA\n");
1866 sdkp->DPOFUA = 0;
1869 if (sdkp->first_scan || old_wce != sdkp->WCE ||
1870 old_rcd != sdkp->RCD || old_dpofua != sdkp->DPOFUA)
1871 sd_printk(KERN_NOTICE, sdkp,
1872 "Write cache: %s, read cache: %s, %s\n",
1873 sdkp->WCE ? "enabled" : "disabled",
1874 sdkp->RCD ? "disabled" : "enabled",
1875 sdkp->DPOFUA ? "supports DPO and FUA"
1876 : "doesn't support DPO or FUA");
1878 return;
1881 bad_sense:
1882 if (scsi_sense_valid(&sshdr) &&
1883 sshdr.sense_key == ILLEGAL_REQUEST &&
1884 sshdr.asc == 0x24 && sshdr.ascq == 0x0)
1885 /* Invalid field in CDB */
1886 sd_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
1887 else
1888 sd_printk(KERN_ERR, sdkp, "Asking for cache data failed\n");
1890 defaults:
1891 sd_printk(KERN_ERR, sdkp, "Assuming drive cache: write through\n");
1892 sdkp->WCE = 0;
1893 sdkp->RCD = 0;
1894 sdkp->DPOFUA = 0;
1898 * The ATO bit indicates whether the DIF application tag is available
1899 * for use by the operating system.
1901 void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
1903 int res, offset;
1904 struct scsi_device *sdp = sdkp->device;
1905 struct scsi_mode_data data;
1906 struct scsi_sense_hdr sshdr;
1908 if (sdp->type != TYPE_DISK)
1909 return;
1911 if (sdkp->protection_type == 0)
1912 return;
1914 res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT,
1915 SD_MAX_RETRIES, &data, &sshdr);
1917 if (!scsi_status_is_good(res) || !data.header_length ||
1918 data.length < 6) {
1919 sd_printk(KERN_WARNING, sdkp,
1920 "getting Control mode page failed, assume no ATO\n");
1922 if (scsi_sense_valid(&sshdr))
1923 sd_print_sense_hdr(sdkp, &sshdr);
1925 return;
1928 offset = data.header_length + data.block_descriptor_length;
1930 if ((buffer[offset] & 0x3f) != 0x0a) {
1931 sd_printk(KERN_ERR, sdkp, "ATO Got wrong page\n");
1932 return;
1935 if ((buffer[offset + 5] & 0x80) == 0)
1936 return;
1938 sdkp->ATO = 1;
1940 return;
1944 * sd_read_block_limits - Query disk device for preferred I/O sizes.
1945 * @disk: disk to query
1947 static void sd_read_block_limits(struct scsi_disk *sdkp)
1949 struct request_queue *q = sdkp->disk->queue;
1950 unsigned int sector_sz = sdkp->device->sector_size;
1951 const int vpd_len = 32;
1952 unsigned char *buffer = kmalloc(vpd_len, GFP_KERNEL);
1954 if (!buffer ||
1955 /* Block Limits VPD */
1956 scsi_get_vpd_page(sdkp->device, 0xb0, buffer, vpd_len))
1957 goto out;
1959 blk_queue_io_min(sdkp->disk->queue,
1960 get_unaligned_be16(&buffer[6]) * sector_sz);
1961 blk_queue_io_opt(sdkp->disk->queue,
1962 get_unaligned_be32(&buffer[12]) * sector_sz);
1964 /* Thin provisioning enabled and page length indicates TP support */
1965 if (sdkp->thin_provisioning && buffer[3] == 0x3c) {
1966 unsigned int lba_count, desc_count, granularity;
1968 lba_count = get_unaligned_be32(&buffer[20]);
1969 desc_count = get_unaligned_be32(&buffer[24]);
1971 if (lba_count) {
1972 q->limits.max_discard_sectors =
1973 lba_count * sector_sz >> 9;
1975 if (desc_count)
1976 sdkp->unmap = 1;
1979 granularity = get_unaligned_be32(&buffer[28]);
1981 if (granularity)
1982 q->limits.discard_granularity = granularity * sector_sz;
1984 if (buffer[32] & 0x80)
1985 q->limits.discard_alignment =
1986 get_unaligned_be32(&buffer[32]) & ~(1 << 31);
1989 out:
1990 kfree(buffer);
1994 * sd_read_block_characteristics - Query block dev. characteristics
1995 * @disk: disk to query
1997 static void sd_read_block_characteristics(struct scsi_disk *sdkp)
1999 unsigned char *buffer;
2000 u16 rot;
2001 const int vpd_len = 32;
2003 buffer = kmalloc(vpd_len, GFP_KERNEL);
2005 if (!buffer ||
2006 /* Block Device Characteristics VPD */
2007 scsi_get_vpd_page(sdkp->device, 0xb1, buffer, vpd_len))
2008 goto out;
2010 rot = get_unaligned_be16(&buffer[4]);
2012 if (rot == 1)
2013 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, sdkp->disk->queue);
2015 out:
2016 kfree(buffer);
2019 static int sd_try_extended_inquiry(struct scsi_device *sdp)
2022 * Although VPD inquiries can go to SCSI-2 type devices,
2023 * some USB ones crash on receiving them, and the pages
2024 * we currently ask for are for SPC-3 and beyond
2026 if (sdp->scsi_level > SCSI_SPC_2)
2027 return 1;
2028 return 0;
2032 * sd_revalidate_disk - called the first time a new disk is seen,
2033 * performs disk spin up, read_capacity, etc.
2034 * @disk: struct gendisk we care about
2036 static int sd_revalidate_disk(struct gendisk *disk)
2038 struct scsi_disk *sdkp = scsi_disk(disk);
2039 struct scsi_device *sdp = sdkp->device;
2040 unsigned char *buffer;
2041 unsigned ordered;
2043 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
2044 "sd_revalidate_disk\n"));
2047 * If the device is offline, don't try and read capacity or any
2048 * of the other niceties.
2050 if (!scsi_device_online(sdp))
2051 goto out;
2053 buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL);
2054 if (!buffer) {
2055 sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
2056 "allocation failure.\n");
2057 goto out;
2060 sd_spinup_disk(sdkp);
2063 * Without media there is no reason to ask; moreover, some devices
2064 * react badly if we do.
2066 if (sdkp->media_present) {
2067 sd_read_capacity(sdkp, buffer);
2069 if (sd_try_extended_inquiry(sdp)) {
2070 sd_read_block_limits(sdkp);
2071 sd_read_block_characteristics(sdkp);
2074 sd_read_write_protect_flag(sdkp, buffer);
2075 sd_read_cache_type(sdkp, buffer);
2076 sd_read_app_tag_own(sdkp, buffer);
2079 sdkp->first_scan = 0;
2082 * We now have all cache related info, determine how we deal
2083 * with ordered requests. Note that as the current SCSI
2084 * dispatch function can alter request order, we cannot use
2085 * QUEUE_ORDERED_TAG_* even when ordered tag is supported.
2087 if (sdkp->WCE)
2088 ordered = sdkp->DPOFUA
2089 ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH;
2090 else
2091 ordered = QUEUE_ORDERED_DRAIN;
2093 blk_queue_ordered(sdkp->disk->queue, ordered, sd_prepare_flush);
2095 set_capacity(disk, sdkp->capacity);
2096 kfree(buffer);
2098 out:
2099 return 0;
2103 * sd_format_disk_name - format disk name
2104 * @prefix: name prefix - ie. "sd" for SCSI disks
2105 * @index: index of the disk to format name for
2106 * @buf: output buffer
2107 * @buflen: length of the output buffer
2109 * SCSI disk names starts at sda. The 26th device is sdz and the
2110 * 27th is sdaa. The last one for two lettered suffix is sdzz
2111 * which is followed by sdaaa.
2113 * This is basically 26 base counting with one extra 'nil' entry
2114 * at the beggining from the second digit on and can be
2115 * determined using similar method as 26 base conversion with the
2116 * index shifted -1 after each digit is computed.
2118 * CONTEXT:
2119 * Don't care.
2121 * RETURNS:
2122 * 0 on success, -errno on failure.
2124 static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen)
2126 const int base = 'z' - 'a' + 1;
2127 char *begin = buf + strlen(prefix);
2128 char *end = buf + buflen;
2129 char *p;
2130 int unit;
2132 p = end - 1;
2133 *p = '\0';
2134 unit = base;
2135 do {
2136 if (p == begin)
2137 return -EINVAL;
2138 *--p = 'a' + (index % unit);
2139 index = (index / unit) - 1;
2140 } while (index >= 0);
2142 memmove(begin, p, end - p);
2143 memcpy(buf, prefix, strlen(prefix));
2145 return 0;
2149 * The asynchronous part of sd_probe
2151 static void sd_probe_async(void *data, async_cookie_t cookie)
2153 struct scsi_disk *sdkp = data;
2154 struct scsi_device *sdp;
2155 struct gendisk *gd;
2156 u32 index;
2157 struct device *dev;
2159 sdp = sdkp->device;
2160 gd = sdkp->disk;
2161 index = sdkp->index;
2162 dev = &sdp->sdev_gendev;
2164 if (index < SD_MAX_DISKS) {
2165 gd->major = sd_major((index & 0xf0) >> 4);
2166 gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
2167 gd->minors = SD_MINORS;
2169 gd->fops = &sd_fops;
2170 gd->private_data = &sdkp->driver;
2171 gd->queue = sdkp->device->request_queue;
2173 /* defaults, until the device tells us otherwise */
2174 sdp->sector_size = 512;
2175 sdkp->capacity = 0;
2176 sdkp->media_present = 1;
2177 sdkp->write_prot = 0;
2178 sdkp->WCE = 0;
2179 sdkp->RCD = 0;
2180 sdkp->ATO = 0;
2181 sdkp->first_scan = 1;
2183 sd_revalidate_disk(gd);
2185 blk_queue_prep_rq(sdp->request_queue, sd_prep_fn);
2187 gd->driverfs_dev = &sdp->sdev_gendev;
2188 gd->flags = GENHD_FL_EXT_DEVT;
2189 if (sdp->removable)
2190 gd->flags |= GENHD_FL_REMOVABLE;
2192 dev_set_drvdata(dev, sdkp);
2193 add_disk(gd);
2194 sd_dif_config_host(sdkp);
2196 sd_revalidate_disk(gd);
2198 sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
2199 sdp->removable ? "removable " : "");
2200 put_device(&sdkp->dev);
2204 * sd_probe - called during driver initialization and whenever a
2205 * new scsi device is attached to the system. It is called once
2206 * for each scsi device (not just disks) present.
2207 * @dev: pointer to device object
2209 * Returns 0 if successful (or not interested in this scsi device
2210 * (e.g. scanner)); 1 when there is an error.
2212 * Note: this function is invoked from the scsi mid-level.
2213 * This function sets up the mapping between a given
2214 * <host,channel,id,lun> (found in sdp) and new device name
2215 * (e.g. /dev/sda). More precisely it is the block device major
2216 * and minor number that is chosen here.
2218 * Assume sd_attach is not re-entrant (for time being)
2219 * Also think about sd_attach() and sd_remove() running coincidentally.
2221 static int sd_probe(struct device *dev)
2223 struct scsi_device *sdp = to_scsi_device(dev);
2224 struct scsi_disk *sdkp;
2225 struct gendisk *gd;
2226 u32 index;
2227 int error;
2229 error = -ENODEV;
2230 if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
2231 goto out;
2233 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
2234 "sd_attach\n"));
2236 error = -ENOMEM;
2237 sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
2238 if (!sdkp)
2239 goto out;
2241 gd = alloc_disk(SD_MINORS);
2242 if (!gd)
2243 goto out_free;
2245 do {
2246 if (!ida_pre_get(&sd_index_ida, GFP_KERNEL))
2247 goto out_put;
2249 spin_lock(&sd_index_lock);
2250 error = ida_get_new(&sd_index_ida, &index);
2251 spin_unlock(&sd_index_lock);
2252 } while (error == -EAGAIN);
2254 if (error)
2255 goto out_put;
2257 error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN);
2258 if (error)
2259 goto out_free_index;
2261 sdkp->device = sdp;
2262 sdkp->driver = &sd_template;
2263 sdkp->disk = gd;
2264 sdkp->index = index;
2265 sdkp->openers = 0;
2266 sdkp->previous_state = 1;
2268 if (!sdp->request_queue->rq_timeout) {
2269 if (sdp->type != TYPE_MOD)
2270 blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT);
2271 else
2272 blk_queue_rq_timeout(sdp->request_queue,
2273 SD_MOD_TIMEOUT);
2276 device_initialize(&sdkp->dev);
2277 sdkp->dev.parent = &sdp->sdev_gendev;
2278 sdkp->dev.class = &sd_disk_class;
2279 dev_set_name(&sdkp->dev, dev_name(&sdp->sdev_gendev));
2281 if (device_add(&sdkp->dev))
2282 goto out_free_index;
2284 get_device(&sdp->sdev_gendev);
2286 get_device(&sdkp->dev); /* prevent release before async_schedule */
2287 async_schedule(sd_probe_async, sdkp);
2289 return 0;
2291 out_free_index:
2292 spin_lock(&sd_index_lock);
2293 ida_remove(&sd_index_ida, index);
2294 spin_unlock(&sd_index_lock);
2295 out_put:
2296 put_disk(gd);
2297 out_free:
2298 kfree(sdkp);
2299 out:
2300 return error;
2304 * sd_remove - called whenever a scsi disk (previously recognized by
2305 * sd_probe) is detached from the system. It is called (potentially
2306 * multiple times) during sd module unload.
2307 * @sdp: pointer to mid level scsi device object
2309 * Note: this function is invoked from the scsi mid-level.
2310 * This function potentially frees up a device name (e.g. /dev/sdc)
2311 * that could be re-used by a subsequent sd_probe().
2312 * This function is not called when the built-in sd driver is "exit-ed".
2314 static int sd_remove(struct device *dev)
2316 struct scsi_disk *sdkp;
2318 async_synchronize_full();
2319 sdkp = dev_get_drvdata(dev);
2320 blk_queue_prep_rq(sdkp->device->request_queue, scsi_prep_fn);
2321 device_del(&sdkp->dev);
2322 del_gendisk(sdkp->disk);
2323 sd_shutdown(dev);
2325 mutex_lock(&sd_ref_mutex);
2326 dev_set_drvdata(dev, NULL);
2327 put_device(&sdkp->dev);
2328 mutex_unlock(&sd_ref_mutex);
2330 return 0;
2334 * scsi_disk_release - Called to free the scsi_disk structure
2335 * @dev: pointer to embedded class device
2337 * sd_ref_mutex must be held entering this routine. Because it is
2338 * called on last put, you should always use the scsi_disk_get()
2339 * scsi_disk_put() helpers which manipulate the semaphore directly
2340 * and never do a direct put_device.
2342 static void scsi_disk_release(struct device *dev)
2344 struct scsi_disk *sdkp = to_scsi_disk(dev);
2345 struct gendisk *disk = sdkp->disk;
2347 spin_lock(&sd_index_lock);
2348 ida_remove(&sd_index_ida, sdkp->index);
2349 spin_unlock(&sd_index_lock);
2351 disk->private_data = NULL;
2352 put_disk(disk);
2353 put_device(&sdkp->device->sdev_gendev);
2355 kfree(sdkp);
2358 static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
2360 unsigned char cmd[6] = { START_STOP }; /* START_VALID */
2361 struct scsi_sense_hdr sshdr;
2362 struct scsi_device *sdp = sdkp->device;
2363 int res;
2365 if (start)
2366 cmd[4] |= 1; /* START */
2368 if (sdp->start_stop_pwr_cond)
2369 cmd[4] |= start ? 1 << 4 : 3 << 4; /* Active or Standby */
2371 if (!scsi_device_online(sdp))
2372 return -ENODEV;
2374 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
2375 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
2376 if (res) {
2377 sd_printk(KERN_WARNING, sdkp, "START_STOP FAILED\n");
2378 sd_print_result(sdkp, res);
2379 if (driver_byte(res) & DRIVER_SENSE)
2380 sd_print_sense_hdr(sdkp, &sshdr);
2383 return res;
2387 * Send a SYNCHRONIZE CACHE instruction down to the device through
2388 * the normal SCSI command structure. Wait for the command to
2389 * complete.
2391 static void sd_shutdown(struct device *dev)
2393 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
2395 if (!sdkp)
2396 return; /* this can happen */
2398 if (sdkp->WCE) {
2399 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
2400 sd_sync_cache(sdkp);
2403 if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
2404 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
2405 sd_start_stop_device(sdkp, 0);
2408 scsi_disk_put(sdkp);
2411 static int sd_suspend(struct device *dev, pm_message_t mesg)
2413 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
2414 int ret = 0;
2416 if (!sdkp)
2417 return 0; /* this can happen */
2419 if (sdkp->WCE) {
2420 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
2421 ret = sd_sync_cache(sdkp);
2422 if (ret)
2423 goto done;
2426 if ((mesg.event & PM_EVENT_SLEEP) && sdkp->device->manage_start_stop) {
2427 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
2428 ret = sd_start_stop_device(sdkp, 0);
2431 done:
2432 scsi_disk_put(sdkp);
2433 return ret;
2436 static int sd_resume(struct device *dev)
2438 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
2439 int ret = 0;
2441 if (!sdkp->device->manage_start_stop)
2442 goto done;
2444 sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
2445 ret = sd_start_stop_device(sdkp, 1);
2447 done:
2448 scsi_disk_put(sdkp);
2449 return ret;
2453 * init_sd - entry point for this driver (both when built in or when
2454 * a module).
2456 * Note: this function registers this driver with the scsi mid-level.
2458 static int __init init_sd(void)
2460 int majors = 0, i, err;
2462 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
2464 for (i = 0; i < SD_MAJORS; i++)
2465 if (register_blkdev(sd_major(i), "sd") == 0)
2466 majors++;
2468 if (!majors)
2469 return -ENODEV;
2471 err = class_register(&sd_disk_class);
2472 if (err)
2473 goto err_out;
2475 err = scsi_register_driver(&sd_template.gendrv);
2476 if (err)
2477 goto err_out_class;
2479 sd_cdb_cache = kmem_cache_create("sd_ext_cdb", SD_EXT_CDB_SIZE,
2480 0, 0, NULL);
2481 if (!sd_cdb_cache) {
2482 printk(KERN_ERR "sd: can't init extended cdb cache\n");
2483 goto err_out_class;
2486 sd_cdb_pool = mempool_create_slab_pool(SD_MEMPOOL_SIZE, sd_cdb_cache);
2487 if (!sd_cdb_pool) {
2488 printk(KERN_ERR "sd: can't init extended cdb pool\n");
2489 goto err_out_cache;
2492 return 0;
2494 err_out_cache:
2495 kmem_cache_destroy(sd_cdb_cache);
2497 err_out_class:
2498 class_unregister(&sd_disk_class);
2499 err_out:
2500 for (i = 0; i < SD_MAJORS; i++)
2501 unregister_blkdev(sd_major(i), "sd");
2502 return err;
2506 * exit_sd - exit point for this driver (when it is a module).
2508 * Note: this function unregisters this driver from the scsi mid-level.
2510 static void __exit exit_sd(void)
2512 int i;
2514 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
2516 mempool_destroy(sd_cdb_pool);
2517 kmem_cache_destroy(sd_cdb_cache);
2519 scsi_unregister_driver(&sd_template.gendrv);
2520 class_unregister(&sd_disk_class);
2522 for (i = 0; i < SD_MAJORS; i++)
2523 unregister_blkdev(sd_major(i), "sd");
2526 module_init(init_sd);
2527 module_exit(exit_sd);
2529 static void sd_print_sense_hdr(struct scsi_disk *sdkp,
2530 struct scsi_sense_hdr *sshdr)
2532 sd_printk(KERN_INFO, sdkp, "");
2533 scsi_show_sense_hdr(sshdr);
2534 sd_printk(KERN_INFO, sdkp, "");
2535 scsi_show_extd_sense(sshdr->asc, sshdr->ascq);
2538 static void sd_print_result(struct scsi_disk *sdkp, int result)
2540 sd_printk(KERN_INFO, sdkp, "");
2541 scsi_show_result(result);