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>
37 #include <linux/kernel.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 <linux/slab.h>
53 #include <asm/uaccess.h>
54 #include <asm/unaligned.h>
56 #include <scsi/scsi.h>
57 #include <scsi/scsi_cmnd.h>
58 #include <scsi/scsi_dbg.h>
59 #include <scsi/scsi_device.h>
60 #include <scsi/scsi_driver.h>
61 #include <scsi/scsi_eh.h>
62 #include <scsi/scsi_host.h>
63 #include <scsi/scsi_ioctl.h>
64 #include <scsi/scsicam.h>
67 #include "scsi_logging.h"
69 MODULE_AUTHOR("Eric Youngdale");
70 MODULE_DESCRIPTION("SCSI disk (sd) driver");
71 MODULE_LICENSE("GPL");
73 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR);
74 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR);
75 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR);
76 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR);
77 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR);
78 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR);
79 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR);
80 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR);
81 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR);
82 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR);
83 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR);
84 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR);
85 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
86 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
87 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
88 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
89 MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK);
90 MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD);
91 MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC);
93 #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
99 static void sd_config_discard(struct scsi_disk *, unsigned int);
100 static int sd_revalidate_disk(struct gendisk *);
101 static void sd_unlock_native_capacity(struct gendisk *disk);
102 static int sd_probe(struct device *);
103 static int sd_remove(struct device *);
104 static void sd_shutdown(struct device *);
105 static int sd_suspend(struct device *, pm_message_t state);
106 static int sd_resume(struct device *);
107 static void sd_rescan(struct device *);
108 static int sd_done(struct scsi_cmnd *);
109 static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
110 static void scsi_disk_release(struct device *cdev);
111 static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *);
112 static void sd_print_result(struct scsi_disk *, int);
114 static DEFINE_SPINLOCK(sd_index_lock);
115 static DEFINE_IDA(sd_index_ida);
117 /* This semaphore is used to mediate the 0->1 reference get in the
118 * face of object destruction (i.e. we can't allow a get on an
119 * object after last put) */
120 static DEFINE_MUTEX(sd_ref_mutex);
122 static struct kmem_cache *sd_cdb_cache;
123 static mempool_t *sd_cdb_pool;
125 static const char *sd_cache_types[] = {
126 "write through", "none", "write back",
127 "write back, no read (daft)"
131 sd_store_cache_type(struct device *dev, struct device_attribute *attr,
132 const char *buf, size_t count)
134 int i, ct = -1, rcd, wce, sp;
135 struct scsi_disk *sdkp = to_scsi_disk(dev);
136 struct scsi_device *sdp = sdkp->device;
139 struct scsi_mode_data data;
140 struct scsi_sense_hdr sshdr;
141 static const char temp[] = "temporary ";
144 if (sdp->type != TYPE_DISK)
145 /* no cache control on RBC devices; theoretically they
146 * can do it, but there's probably so many exceptions
147 * it's not worth the risk */
150 if (strncmp(buf, temp, sizeof(temp) - 1) == 0) {
151 buf += sizeof(temp) - 1;
152 sdkp->cache_override = 1;
154 sdkp->cache_override = 0;
157 for (i = 0; i < ARRAY_SIZE(sd_cache_types); i++) {
158 len = strlen(sd_cache_types[i]);
159 if (strncmp(sd_cache_types[i], buf, len) == 0 &&
167 rcd = ct & 0x01 ? 1 : 0;
168 wce = ct & 0x02 ? 1 : 0;
170 if (sdkp->cache_override) {
176 if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT,
177 SD_MAX_RETRIES, &data, NULL))
179 len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
180 data.block_descriptor_length);
181 buffer_data = buffer + data.header_length +
182 data.block_descriptor_length;
183 buffer_data[2] &= ~0x05;
184 buffer_data[2] |= wce << 2 | rcd;
185 sp = buffer_data[0] & 0x80 ? 1 : 0;
187 if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT,
188 SD_MAX_RETRIES, &data, &sshdr)) {
189 if (scsi_sense_valid(&sshdr))
190 sd_print_sense_hdr(sdkp, &sshdr);
193 revalidate_disk(sdkp->disk);
198 sd_store_manage_start_stop(struct device *dev, struct device_attribute *attr,
199 const char *buf, size_t count)
201 struct scsi_disk *sdkp = to_scsi_disk(dev);
202 struct scsi_device *sdp = sdkp->device;
204 if (!capable(CAP_SYS_ADMIN))
207 sdp->manage_start_stop = simple_strtoul(buf, NULL, 10);
213 sd_store_allow_restart(struct device *dev, struct device_attribute *attr,
214 const char *buf, size_t count)
216 struct scsi_disk *sdkp = to_scsi_disk(dev);
217 struct scsi_device *sdp = sdkp->device;
219 if (!capable(CAP_SYS_ADMIN))
222 if (sdp->type != TYPE_DISK)
225 sdp->allow_restart = simple_strtoul(buf, NULL, 10);
231 sd_show_cache_type(struct device *dev, struct device_attribute *attr,
234 struct scsi_disk *sdkp = to_scsi_disk(dev);
235 int ct = sdkp->RCD + 2*sdkp->WCE;
237 return snprintf(buf, 40, "%s\n", sd_cache_types[ct]);
241 sd_show_fua(struct device *dev, struct device_attribute *attr, char *buf)
243 struct scsi_disk *sdkp = to_scsi_disk(dev);
245 return snprintf(buf, 20, "%u\n", sdkp->DPOFUA);
249 sd_show_manage_start_stop(struct device *dev, struct device_attribute *attr,
252 struct scsi_disk *sdkp = to_scsi_disk(dev);
253 struct scsi_device *sdp = sdkp->device;
255 return snprintf(buf, 20, "%u\n", sdp->manage_start_stop);
259 sd_show_allow_restart(struct device *dev, struct device_attribute *attr,
262 struct scsi_disk *sdkp = to_scsi_disk(dev);
264 return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart);
268 sd_show_protection_type(struct device *dev, struct device_attribute *attr,
271 struct scsi_disk *sdkp = to_scsi_disk(dev);
273 return snprintf(buf, 20, "%u\n", sdkp->protection_type);
277 sd_show_protection_mode(struct device *dev, struct device_attribute *attr,
280 struct scsi_disk *sdkp = to_scsi_disk(dev);
281 struct scsi_device *sdp = sdkp->device;
282 unsigned int dif, dix;
284 dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type);
285 dix = scsi_host_dix_capable(sdp->host, sdkp->protection_type);
287 if (!dix && scsi_host_dix_capable(sdp->host, SD_DIF_TYPE0_PROTECTION)) {
293 return snprintf(buf, 20, "none\n");
295 return snprintf(buf, 20, "%s%u\n", dix ? "dix" : "dif", dif);
299 sd_show_app_tag_own(struct device *dev, struct device_attribute *attr,
302 struct scsi_disk *sdkp = to_scsi_disk(dev);
304 return snprintf(buf, 20, "%u\n", sdkp->ATO);
308 sd_show_thin_provisioning(struct device *dev, struct device_attribute *attr,
311 struct scsi_disk *sdkp = to_scsi_disk(dev);
313 return snprintf(buf, 20, "%u\n", sdkp->lbpme);
316 static const char *lbp_mode[] = {
317 [SD_LBP_FULL] = "full",
318 [SD_LBP_UNMAP] = "unmap",
319 [SD_LBP_WS16] = "writesame_16",
320 [SD_LBP_WS10] = "writesame_10",
321 [SD_LBP_ZERO] = "writesame_zero",
322 [SD_LBP_DISABLE] = "disabled",
326 sd_show_provisioning_mode(struct device *dev, struct device_attribute *attr,
329 struct scsi_disk *sdkp = to_scsi_disk(dev);
331 return snprintf(buf, 20, "%s\n", lbp_mode[sdkp->provisioning_mode]);
335 sd_store_provisioning_mode(struct device *dev, struct device_attribute *attr,
336 const char *buf, size_t count)
338 struct scsi_disk *sdkp = to_scsi_disk(dev);
339 struct scsi_device *sdp = sdkp->device;
341 if (!capable(CAP_SYS_ADMIN))
344 if (sdp->type != TYPE_DISK)
347 if (!strncmp(buf, lbp_mode[SD_LBP_UNMAP], 20))
348 sd_config_discard(sdkp, SD_LBP_UNMAP);
349 else if (!strncmp(buf, lbp_mode[SD_LBP_WS16], 20))
350 sd_config_discard(sdkp, SD_LBP_WS16);
351 else if (!strncmp(buf, lbp_mode[SD_LBP_WS10], 20))
352 sd_config_discard(sdkp, SD_LBP_WS10);
353 else if (!strncmp(buf, lbp_mode[SD_LBP_ZERO], 20))
354 sd_config_discard(sdkp, SD_LBP_ZERO);
355 else if (!strncmp(buf, lbp_mode[SD_LBP_DISABLE], 20))
356 sd_config_discard(sdkp, SD_LBP_DISABLE);
363 static struct device_attribute sd_disk_attrs[] = {
364 __ATTR(cache_type, S_IRUGO|S_IWUSR, sd_show_cache_type,
365 sd_store_cache_type),
366 __ATTR(FUA, S_IRUGO, sd_show_fua, NULL),
367 __ATTR(allow_restart, S_IRUGO|S_IWUSR, sd_show_allow_restart,
368 sd_store_allow_restart),
369 __ATTR(manage_start_stop, S_IRUGO|S_IWUSR, sd_show_manage_start_stop,
370 sd_store_manage_start_stop),
371 __ATTR(protection_type, S_IRUGO, sd_show_protection_type, NULL),
372 __ATTR(protection_mode, S_IRUGO, sd_show_protection_mode, NULL),
373 __ATTR(app_tag_own, S_IRUGO, sd_show_app_tag_own, NULL),
374 __ATTR(thin_provisioning, S_IRUGO, sd_show_thin_provisioning, NULL),
375 __ATTR(provisioning_mode, S_IRUGO|S_IWUSR, sd_show_provisioning_mode,
376 sd_store_provisioning_mode),
380 static struct class sd_disk_class = {
382 .owner = THIS_MODULE,
383 .dev_release = scsi_disk_release,
384 .dev_attrs = sd_disk_attrs,
387 static struct scsi_driver sd_template = {
388 .owner = THIS_MODULE,
393 .suspend = sd_suspend,
395 .shutdown = sd_shutdown,
402 * Device no to disk mapping:
404 * major disc2 disc p1
405 * |............|.............|....|....| <- dev_t
408 * Inside a major, we have 16k disks, however mapped non-
409 * contiguously. The first 16 disks are for major0, the next
410 * ones with major1, ... Disk 256 is for major0 again, disk 272
412 * As we stay compatible with our numbering scheme, we can reuse
413 * the well-know SCSI majors 8, 65--71, 136--143.
415 static int sd_major(int major_idx)
419 return SCSI_DISK0_MAJOR;
421 return SCSI_DISK1_MAJOR + major_idx - 1;
423 return SCSI_DISK8_MAJOR + major_idx - 8;
426 return 0; /* shut up gcc */
430 static struct scsi_disk *__scsi_disk_get(struct gendisk *disk)
432 struct scsi_disk *sdkp = NULL;
434 if (disk->private_data) {
435 sdkp = scsi_disk(disk);
436 if (scsi_device_get(sdkp->device) == 0)
437 get_device(&sdkp->dev);
444 static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
446 struct scsi_disk *sdkp;
448 mutex_lock(&sd_ref_mutex);
449 sdkp = __scsi_disk_get(disk);
450 mutex_unlock(&sd_ref_mutex);
454 static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev)
456 struct scsi_disk *sdkp;
458 mutex_lock(&sd_ref_mutex);
459 sdkp = dev_get_drvdata(dev);
461 sdkp = __scsi_disk_get(sdkp->disk);
462 mutex_unlock(&sd_ref_mutex);
466 static void scsi_disk_put(struct scsi_disk *sdkp)
468 struct scsi_device *sdev = sdkp->device;
470 mutex_lock(&sd_ref_mutex);
471 put_device(&sdkp->dev);
472 scsi_device_put(sdev);
473 mutex_unlock(&sd_ref_mutex);
476 static void sd_prot_op(struct scsi_cmnd *scmd, unsigned int dif)
478 unsigned int prot_op = SCSI_PROT_NORMAL;
479 unsigned int dix = scsi_prot_sg_count(scmd);
481 if (scmd->sc_data_direction == DMA_FROM_DEVICE) {
483 prot_op = SCSI_PROT_READ_PASS;
484 else if (dif && !dix)
485 prot_op = SCSI_PROT_READ_STRIP;
486 else if (!dif && dix)
487 prot_op = SCSI_PROT_READ_INSERT;
490 prot_op = SCSI_PROT_WRITE_PASS;
491 else if (dif && !dix)
492 prot_op = SCSI_PROT_WRITE_INSERT;
493 else if (!dif && dix)
494 prot_op = SCSI_PROT_WRITE_STRIP;
497 scsi_set_prot_op(scmd, prot_op);
498 scsi_set_prot_type(scmd, dif);
501 static void sd_config_discard(struct scsi_disk *sdkp, unsigned int mode)
503 struct request_queue *q = sdkp->disk->queue;
504 unsigned int logical_block_size = sdkp->device->sector_size;
505 unsigned int max_blocks = 0;
507 q->limits.discard_zeroes_data = sdkp->lbprz;
508 q->limits.discard_alignment = sdkp->unmap_alignment *
510 q->limits.discard_granularity =
511 max(sdkp->physical_block_size,
512 sdkp->unmap_granularity * logical_block_size);
517 q->limits.max_discard_sectors = 0;
518 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
522 max_blocks = min_not_zero(sdkp->max_unmap_blocks, 0xffffffff);
526 if (sdkp->device->unmap_limit_for_ws)
527 max_blocks = sdkp->max_unmap_blocks;
529 max_blocks = sdkp->max_ws_blocks;
531 max_blocks = min_not_zero(max_blocks, 0xffffffff);
535 if (sdkp->device->unmap_limit_for_ws)
536 max_blocks = sdkp->max_unmap_blocks;
538 max_blocks = sdkp->max_ws_blocks;
540 max_blocks = min_not_zero(max_blocks, (u32)0xffff);
544 max_blocks = min_not_zero(sdkp->max_ws_blocks, (u32)0xffff);
545 q->limits.discard_zeroes_data = 1;
549 q->limits.max_discard_sectors = max_blocks * (logical_block_size >> 9);
550 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
552 sdkp->provisioning_mode = mode;
556 * scsi_setup_discard_cmnd - unmap blocks on thinly provisioned device
557 * @sdp: scsi device to operate one
558 * @rq: Request to prepare
560 * Will issue either UNMAP or WRITE SAME(16) depending on preference
561 * indicated by target device.
563 static int scsi_setup_discard_cmnd(struct scsi_device *sdp, struct request *rq)
565 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
566 struct bio *bio = rq->bio;
567 sector_t sector = bio->bi_sector;
568 unsigned int nr_sectors = bio_sectors(bio);
574 if (sdkp->device->sector_size == 4096) {
579 rq->timeout = SD_TIMEOUT;
581 memset(rq->cmd, 0, rq->cmd_len);
583 page = alloc_page(GFP_ATOMIC | __GFP_ZERO);
585 return BLKPREP_DEFER;
587 switch (sdkp->provisioning_mode) {
589 buf = page_address(page);
595 put_unaligned_be16(6 + 16, &buf[0]);
596 put_unaligned_be16(16, &buf[2]);
597 put_unaligned_be64(sector, &buf[8]);
598 put_unaligned_be32(nr_sectors, &buf[16]);
605 rq->cmd[0] = WRITE_SAME_16;
606 rq->cmd[1] = 0x8; /* UNMAP */
607 put_unaligned_be64(sector, &rq->cmd[2]);
608 put_unaligned_be32(nr_sectors, &rq->cmd[10]);
610 len = sdkp->device->sector_size;
616 rq->cmd[0] = WRITE_SAME;
617 if (sdkp->provisioning_mode == SD_LBP_WS10)
618 rq->cmd[1] = 0x8; /* UNMAP */
619 put_unaligned_be32(sector, &rq->cmd[2]);
620 put_unaligned_be16(nr_sectors, &rq->cmd[7]);
622 len = sdkp->device->sector_size;
630 blk_add_request_payload(rq, page, len);
631 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
632 rq->buffer = page_address(page);
635 if (ret != BLKPREP_OK) {
642 static int scsi_setup_flush_cmnd(struct scsi_device *sdp, struct request *rq)
644 rq->timeout = SD_FLUSH_TIMEOUT;
645 rq->retries = SD_MAX_RETRIES;
646 rq->cmd[0] = SYNCHRONIZE_CACHE;
649 return scsi_setup_blk_pc_cmnd(sdp, rq);
652 static void sd_unprep_fn(struct request_queue *q, struct request *rq)
654 struct scsi_cmnd *SCpnt = rq->special;
656 if (rq->cmd_flags & REQ_DISCARD) {
657 free_page((unsigned long)rq->buffer);
660 if (SCpnt->cmnd != rq->cmd) {
661 mempool_free(SCpnt->cmnd, sd_cdb_pool);
668 * sd_init_command - build a scsi (read or write) command from
669 * information in the request structure.
670 * @SCpnt: pointer to mid-level's per scsi command structure that
671 * contains request and into which the scsi command is written
673 * Returns 1 if successful and 0 if error (or cannot be done now).
675 static int sd_prep_fn(struct request_queue *q, struct request *rq)
677 struct scsi_cmnd *SCpnt;
678 struct scsi_device *sdp = q->queuedata;
679 struct gendisk *disk = rq->rq_disk;
680 struct scsi_disk *sdkp;
681 sector_t block = blk_rq_pos(rq);
683 unsigned int this_count = blk_rq_sectors(rq);
685 unsigned char protect;
688 * Discard request come in as REQ_TYPE_FS but we turn them into
689 * block PC requests to make life easier.
691 if (rq->cmd_flags & REQ_DISCARD) {
692 ret = scsi_setup_discard_cmnd(sdp, rq);
694 } else if (rq->cmd_flags & REQ_FLUSH) {
695 ret = scsi_setup_flush_cmnd(sdp, rq);
697 } else if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
698 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
700 } else if (rq->cmd_type != REQ_TYPE_FS) {
704 ret = scsi_setup_fs_cmnd(sdp, rq);
705 if (ret != BLKPREP_OK)
708 sdkp = scsi_disk(disk);
710 /* from here on until we're complete, any goto out
711 * is used for a killable error condition */
714 SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
715 "sd_init_command: block=%llu, "
717 (unsigned long long)block,
720 if (!sdp || !scsi_device_online(sdp) ||
721 block + blk_rq_sectors(rq) > get_capacity(disk)) {
722 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
723 "Finishing %u sectors\n",
724 blk_rq_sectors(rq)));
725 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
726 "Retry with 0x%p\n", SCpnt));
732 * quietly refuse to do anything to a changed disc until
733 * the changed bit has been reset
735 /* printk("SCSI disk has been changed or is not present. Prohibiting further I/O.\n"); */
740 * Some SD card readers can't handle multi-sector accesses which touch
741 * the last one or two hardware sectors. Split accesses as needed.
743 threshold = get_capacity(disk) - SD_LAST_BUGGY_SECTORS *
744 (sdp->sector_size / 512);
746 if (unlikely(sdp->last_sector_bug && block + this_count > threshold)) {
747 if (block < threshold) {
748 /* Access up to the threshold but not beyond */
749 this_count = threshold - block;
751 /* Access only a single hardware sector */
752 this_count = sdp->sector_size / 512;
756 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n",
757 (unsigned long long)block));
760 * If we have a 1K hardware sectorsize, prevent access to single
761 * 512 byte sectors. In theory we could handle this - in fact
762 * the scsi cdrom driver must be able to handle this because
763 * we typically use 1K blocksizes, and cdroms typically have
764 * 2K hardware sectorsizes. Of course, things are simpler
765 * with the cdrom, since it is read-only. For performance
766 * reasons, the filesystems should be able to handle this
767 * and not force the scsi disk driver to use bounce buffers
770 if (sdp->sector_size == 1024) {
771 if ((block & 1) || (blk_rq_sectors(rq) & 1)) {
772 scmd_printk(KERN_ERR, SCpnt,
773 "Bad block number requested\n");
777 this_count = this_count >> 1;
780 if (sdp->sector_size == 2048) {
781 if ((block & 3) || (blk_rq_sectors(rq) & 3)) {
782 scmd_printk(KERN_ERR, SCpnt,
783 "Bad block number requested\n");
787 this_count = this_count >> 2;
790 if (sdp->sector_size == 4096) {
791 if ((block & 7) || (blk_rq_sectors(rq) & 7)) {
792 scmd_printk(KERN_ERR, SCpnt,
793 "Bad block number requested\n");
797 this_count = this_count >> 3;
800 if (rq_data_dir(rq) == WRITE) {
801 if (!sdp->writeable) {
804 SCpnt->cmnd[0] = WRITE_6;
805 SCpnt->sc_data_direction = DMA_TO_DEVICE;
807 if (blk_integrity_rq(rq) &&
808 sd_dif_prepare(rq, block, sdp->sector_size) == -EIO)
811 } else if (rq_data_dir(rq) == READ) {
812 SCpnt->cmnd[0] = READ_6;
813 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
815 scmd_printk(KERN_ERR, SCpnt, "Unknown command %x\n", rq->cmd_flags);
819 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
820 "%s %d/%u 512 byte blocks.\n",
821 (rq_data_dir(rq) == WRITE) ?
822 "writing" : "reading", this_count,
823 blk_rq_sectors(rq)));
825 /* Set RDPROTECT/WRPROTECT if disk is formatted with DIF */
826 host_dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type);
832 if (host_dif == SD_DIF_TYPE2_PROTECTION) {
833 SCpnt->cmnd = mempool_alloc(sd_cdb_pool, GFP_ATOMIC);
835 if (unlikely(SCpnt->cmnd == NULL)) {
840 SCpnt->cmd_len = SD_EXT_CDB_SIZE;
841 memset(SCpnt->cmnd, 0, SCpnt->cmd_len);
842 SCpnt->cmnd[0] = VARIABLE_LENGTH_CMD;
843 SCpnt->cmnd[7] = 0x18;
844 SCpnt->cmnd[9] = (rq_data_dir(rq) == READ) ? READ_32 : WRITE_32;
845 SCpnt->cmnd[10] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
848 SCpnt->cmnd[12] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
849 SCpnt->cmnd[13] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
850 SCpnt->cmnd[14] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
851 SCpnt->cmnd[15] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
852 SCpnt->cmnd[16] = (unsigned char) (block >> 24) & 0xff;
853 SCpnt->cmnd[17] = (unsigned char) (block >> 16) & 0xff;
854 SCpnt->cmnd[18] = (unsigned char) (block >> 8) & 0xff;
855 SCpnt->cmnd[19] = (unsigned char) block & 0xff;
857 /* Expected Indirect LBA */
858 SCpnt->cmnd[20] = (unsigned char) (block >> 24) & 0xff;
859 SCpnt->cmnd[21] = (unsigned char) (block >> 16) & 0xff;
860 SCpnt->cmnd[22] = (unsigned char) (block >> 8) & 0xff;
861 SCpnt->cmnd[23] = (unsigned char) block & 0xff;
863 /* Transfer length */
864 SCpnt->cmnd[28] = (unsigned char) (this_count >> 24) & 0xff;
865 SCpnt->cmnd[29] = (unsigned char) (this_count >> 16) & 0xff;
866 SCpnt->cmnd[30] = (unsigned char) (this_count >> 8) & 0xff;
867 SCpnt->cmnd[31] = (unsigned char) this_count & 0xff;
868 } else if (block > 0xffffffff) {
869 SCpnt->cmnd[0] += READ_16 - READ_6;
870 SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
871 SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
872 SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
873 SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
874 SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
875 SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
876 SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
877 SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
878 SCpnt->cmnd[9] = (unsigned char) block & 0xff;
879 SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
880 SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
881 SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
882 SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
883 SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
884 } else if ((this_count > 0xff) || (block > 0x1fffff) ||
885 scsi_device_protection(SCpnt->device) ||
886 SCpnt->device->use_10_for_rw) {
887 if (this_count > 0xffff)
890 SCpnt->cmnd[0] += READ_10 - READ_6;
891 SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
892 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
893 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
894 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
895 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
896 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
897 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
898 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
900 if (unlikely(rq->cmd_flags & REQ_FUA)) {
902 * This happens only if this drive failed
903 * 10byte rw command with ILLEGAL_REQUEST
904 * during operation and thus turned off
907 scmd_printk(KERN_ERR, SCpnt,
908 "FUA write on READ/WRITE(6) drive\n");
912 SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
913 SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
914 SCpnt->cmnd[3] = (unsigned char) block & 0xff;
915 SCpnt->cmnd[4] = (unsigned char) this_count;
918 SCpnt->sdb.length = this_count * sdp->sector_size;
920 /* If DIF or DIX is enabled, tell HBA how to handle request */
921 if (host_dif || scsi_prot_sg_count(SCpnt))
922 sd_prot_op(SCpnt, host_dif);
925 * We shouldn't disconnect in the middle of a sector, so with a dumb
926 * host adapter, it's safe to assume that we can at least transfer
927 * this many bytes between each connect / disconnect.
929 SCpnt->transfersize = sdp->sector_size;
930 SCpnt->underflow = this_count << 9;
931 SCpnt->allowed = SD_MAX_RETRIES;
934 * This indicates that the command is ready from our end to be
939 return scsi_prep_return(q, rq, ret);
943 * sd_open - open a scsi disk device
944 * @inode: only i_rdev member may be used
945 * @filp: only f_mode and f_flags may be used
947 * Returns 0 if successful. Returns a negated errno value in case
950 * Note: This can be called from a user context (e.g. fsck(1) )
951 * or from within the kernel (e.g. as a result of a mount(1) ).
952 * In the latter case @inode and @filp carry an abridged amount
953 * of information as noted above.
955 * Locking: called with bdev->bd_mutex held.
957 static int sd_open(struct block_device *bdev, fmode_t mode)
959 struct scsi_disk *sdkp = scsi_disk_get(bdev->bd_disk);
960 struct scsi_device *sdev;
966 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
970 retval = scsi_autopm_get_device(sdev);
975 * If the device is in error recovery, wait until it is done.
976 * If the device is offline, then disallow any access to it.
979 if (!scsi_block_when_processing_errors(sdev))
982 if (sdev->removable || sdkp->write_prot)
983 check_disk_change(bdev);
986 * If the drive is empty, just let the open fail.
989 if (sdev->removable && !sdkp->media_present && !(mode & FMODE_NDELAY))
993 * If the device has the write protect tab set, have the open fail
994 * if the user expects to be able to write to the thing.
997 if (sdkp->write_prot && (mode & FMODE_WRITE))
1001 * It is possible that the disk changing stuff resulted in
1002 * the device being taken offline. If this is the case,
1003 * report this to the user, and don't pretend that the
1004 * open actually succeeded.
1007 if (!scsi_device_online(sdev))
1010 if ((atomic_inc_return(&sdkp->openers) == 1) && sdev->removable) {
1011 if (scsi_block_when_processing_errors(sdev))
1012 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
1018 scsi_autopm_put_device(sdev);
1020 scsi_disk_put(sdkp);
1025 * sd_release - invoked when the (last) close(2) is called on this
1027 * @inode: only i_rdev member may be used
1028 * @filp: only f_mode and f_flags may be used
1032 * Note: may block (uninterruptible) if error recovery is underway
1035 * Locking: called with bdev->bd_mutex held.
1037 static int sd_release(struct gendisk *disk, fmode_t mode)
1039 struct scsi_disk *sdkp = scsi_disk(disk);
1040 struct scsi_device *sdev = sdkp->device;
1042 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
1044 if (atomic_dec_return(&sdkp->openers) == 0 && sdev->removable) {
1045 if (scsi_block_when_processing_errors(sdev))
1046 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
1050 * XXX and what if there are packets in flight and this close()
1051 * XXX is followed by a "rmmod sd_mod"?
1054 scsi_autopm_put_device(sdev);
1055 scsi_disk_put(sdkp);
1059 static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1061 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
1062 struct scsi_device *sdp = sdkp->device;
1063 struct Scsi_Host *host = sdp->host;
1064 sector_t capacity = logical_to_sectors(sdp, sdkp->capacity);
1067 /* default to most commonly used values */
1068 diskinfo[0] = 0x40; /* 1 << 6 */
1069 diskinfo[1] = 0x20; /* 1 << 5 */
1070 diskinfo[2] = capacity >> 11;
1072 /* override with calculated, extended default, or driver values */
1073 if (host->hostt->bios_param)
1074 host->hostt->bios_param(sdp, bdev, capacity, diskinfo);
1076 scsicam_bios_param(bdev, capacity, diskinfo);
1078 geo->heads = diskinfo[0];
1079 geo->sectors = diskinfo[1];
1080 geo->cylinders = diskinfo[2];
1085 * sd_ioctl - process an ioctl
1086 * @inode: only i_rdev/i_bdev members may be used
1087 * @filp: only f_mode and f_flags may be used
1088 * @cmd: ioctl command number
1089 * @arg: this is third argument given to ioctl(2) system call.
1090 * Often contains a pointer.
1092 * Returns 0 if successful (some ioctls return positive numbers on
1093 * success as well). Returns a negated errno value in case of error.
1095 * Note: most ioctls are forward onto the block subsystem or further
1096 * down in the scsi subsystem.
1098 static int sd_ioctl(struct block_device *bdev, fmode_t mode,
1099 unsigned int cmd, unsigned long arg)
1101 struct gendisk *disk = bdev->bd_disk;
1102 struct scsi_disk *sdkp = scsi_disk(disk);
1103 struct scsi_device *sdp = sdkp->device;
1104 void __user *p = (void __user *)arg;
1107 SCSI_LOG_IOCTL(1, sd_printk(KERN_INFO, sdkp, "sd_ioctl: disk=%s, "
1108 "cmd=0x%x\n", disk->disk_name, cmd));
1110 error = scsi_verify_blk_ioctl(bdev, cmd);
1115 * If we are in the middle of error recovery, don't let anyone
1116 * else try and use this device. Also, if error recovery fails, it
1117 * may try and take the device offline, in which case all further
1118 * access to the device is prohibited.
1120 error = scsi_nonblockable_ioctl(sdp, cmd, p,
1121 (mode & FMODE_NDELAY) != 0);
1122 if (!scsi_block_when_processing_errors(sdp) || !error)
1126 * Send SCSI addressing ioctls directly to mid level, send other
1127 * ioctls to block level and then onto mid level if they can't be
1131 case SCSI_IOCTL_GET_IDLUN:
1132 case SCSI_IOCTL_GET_BUS_NUMBER:
1133 error = scsi_ioctl(sdp, cmd, p);
1136 error = scsi_cmd_blk_ioctl(bdev, mode, cmd, p);
1137 if (error != -ENOTTY)
1139 error = scsi_ioctl(sdp, cmd, p);
1146 static void set_media_not_present(struct scsi_disk *sdkp)
1148 if (sdkp->media_present)
1149 sdkp->device->changed = 1;
1151 if (sdkp->device->removable) {
1152 sdkp->media_present = 0;
1157 static int media_not_present(struct scsi_disk *sdkp,
1158 struct scsi_sense_hdr *sshdr)
1160 if (!scsi_sense_valid(sshdr))
1163 /* not invoked for commands that could return deferred errors */
1164 switch (sshdr->sense_key) {
1165 case UNIT_ATTENTION:
1167 /* medium not present */
1168 if (sshdr->asc == 0x3A) {
1169 set_media_not_present(sdkp);
1177 * sd_check_events - check media events
1178 * @disk: kernel device descriptor
1179 * @clearing: disk events currently being cleared
1181 * Returns mask of DISK_EVENT_*.
1183 * Note: this function is invoked from the block subsystem.
1185 static unsigned int sd_check_events(struct gendisk *disk, unsigned int clearing)
1187 struct scsi_disk *sdkp = scsi_disk(disk);
1188 struct scsi_device *sdp = sdkp->device;
1189 struct scsi_sense_hdr *sshdr = NULL;
1192 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_check_events\n"));
1195 * If the device is offline, don't send any commands - just pretend as
1196 * if the command failed. If the device ever comes back online, we
1197 * can deal with it then. It is only because of unrecoverable errors
1198 * that we would ever take a device offline in the first place.
1200 if (!scsi_device_online(sdp)) {
1201 set_media_not_present(sdkp);
1206 * Using TEST_UNIT_READY enables differentiation between drive with
1207 * no cartridge loaded - NOT READY, drive with changed cartridge -
1208 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
1210 * Drives that auto spin down. eg iomega jaz 1G, will be started
1211 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
1212 * sd_revalidate() is called.
1216 if (scsi_block_when_processing_errors(sdp)) {
1217 sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
1218 retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES,
1222 /* failed to execute TUR, assume media not present */
1223 if (host_byte(retval)) {
1224 set_media_not_present(sdkp);
1228 if (media_not_present(sdkp, sshdr))
1232 * For removable scsi disk we have to recognise the presence
1233 * of a disk in the drive.
1235 if (!sdkp->media_present)
1237 sdkp->media_present = 1;
1240 * sdp->changed is set under the following conditions:
1242 * Medium present state has changed in either direction.
1243 * Device has indicated UNIT_ATTENTION.
1246 retval = sdp->changed ? DISK_EVENT_MEDIA_CHANGE : 0;
1251 static int sd_sync_cache(struct scsi_disk *sdkp)
1254 struct scsi_device *sdp = sdkp->device;
1255 struct scsi_sense_hdr sshdr;
1257 if (!scsi_device_online(sdp))
1261 for (retries = 3; retries > 0; --retries) {
1262 unsigned char cmd[10] = { 0 };
1264 cmd[0] = SYNCHRONIZE_CACHE;
1266 * Leave the rest of the command zero to indicate
1269 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
1270 SD_FLUSH_TIMEOUT, SD_MAX_RETRIES, NULL);
1276 sd_print_result(sdkp, res);
1277 if (driver_byte(res) & DRIVER_SENSE)
1278 sd_print_sense_hdr(sdkp, &sshdr);
1286 static void sd_rescan(struct device *dev)
1288 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
1291 revalidate_disk(sdkp->disk);
1292 scsi_disk_put(sdkp);
1297 #ifdef CONFIG_COMPAT
1299 * This gets directly called from VFS. When the ioctl
1300 * is not recognized we go back to the other translation paths.
1302 static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode,
1303 unsigned int cmd, unsigned long arg)
1305 struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
1308 ret = scsi_verify_blk_ioctl(bdev, cmd);
1310 return -ENOIOCTLCMD;
1313 * If we are in the middle of error recovery, don't let anyone
1314 * else try and use this device. Also, if error recovery fails, it
1315 * may try and take the device offline, in which case all further
1316 * access to the device is prohibited.
1318 if (!scsi_block_when_processing_errors(sdev))
1321 if (sdev->host->hostt->compat_ioctl) {
1322 ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
1328 * Let the static ioctl translation table take care of it.
1330 return -ENOIOCTLCMD;
1334 static const struct block_device_operations sd_fops = {
1335 .owner = THIS_MODULE,
1337 .release = sd_release,
1339 .getgeo = sd_getgeo,
1340 #ifdef CONFIG_COMPAT
1341 .compat_ioctl = sd_compat_ioctl,
1343 .check_events = sd_check_events,
1344 .revalidate_disk = sd_revalidate_disk,
1345 .unlock_native_capacity = sd_unlock_native_capacity,
1348 static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
1350 u64 start_lba = blk_rq_pos(scmd->request);
1351 u64 end_lba = blk_rq_pos(scmd->request) + (scsi_bufflen(scmd) / 512);
1352 u64 factor = scmd->device->sector_size / 512;
1356 * resid is optional but mostly filled in. When it's unused,
1357 * its value is zero, so we assume the whole buffer transferred
1359 unsigned int transferred = scsi_bufflen(scmd) - scsi_get_resid(scmd);
1360 unsigned int good_bytes;
1362 if (scmd->request->cmd_type != REQ_TYPE_FS)
1365 info_valid = scsi_get_sense_info_fld(scmd->sense_buffer,
1366 SCSI_SENSE_BUFFERSIZE,
1371 if (scsi_bufflen(scmd) <= scmd->device->sector_size)
1374 /* be careful ... don't want any overflows */
1375 do_div(start_lba, factor);
1376 do_div(end_lba, factor);
1378 /* The bad lba was reported incorrectly, we have no idea where
1381 if (bad_lba < start_lba || bad_lba >= end_lba)
1384 /* This computation should always be done in terms of
1385 * the resolution of the device's medium.
1387 good_bytes = (bad_lba - start_lba) * scmd->device->sector_size;
1388 return min(good_bytes, transferred);
1392 * sd_done - bottom half handler: called when the lower level
1393 * driver has completed (successfully or otherwise) a scsi command.
1394 * @SCpnt: mid-level's per command structure.
1396 * Note: potentially run from within an ISR. Must not block.
1398 static int sd_done(struct scsi_cmnd *SCpnt)
1400 int result = SCpnt->result;
1401 unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt);
1402 struct scsi_sense_hdr sshdr;
1403 struct scsi_disk *sdkp = scsi_disk(SCpnt->request->rq_disk);
1404 int sense_valid = 0;
1405 int sense_deferred = 0;
1406 unsigned char op = SCpnt->cmnd[0];
1408 if ((SCpnt->request->cmd_flags & REQ_DISCARD) && !result)
1409 scsi_set_resid(SCpnt, 0);
1412 sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
1414 sense_deferred = scsi_sense_is_deferred(&sshdr);
1416 #ifdef CONFIG_SCSI_LOGGING
1417 SCSI_LOG_HLCOMPLETE(1, scsi_print_result(SCpnt));
1419 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
1420 "sd_done: sb[respc,sk,asc,"
1421 "ascq]=%x,%x,%x,%x\n",
1422 sshdr.response_code,
1423 sshdr.sense_key, sshdr.asc,
1427 if (driver_byte(result) != DRIVER_SENSE &&
1428 (!sense_valid || sense_deferred))
1431 switch (sshdr.sense_key) {
1432 case HARDWARE_ERROR:
1434 good_bytes = sd_completed_bytes(SCpnt);
1436 case RECOVERED_ERROR:
1437 good_bytes = scsi_bufflen(SCpnt);
1440 /* This indicates a false check condition, so ignore it. An
1441 * unknown amount of data was transferred so treat it as an
1444 scsi_print_sense("sd", SCpnt);
1446 memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1448 case ABORTED_COMMAND:
1449 if (sshdr.asc == 0x10) /* DIF: Target detected corruption */
1450 good_bytes = sd_completed_bytes(SCpnt);
1452 case ILLEGAL_REQUEST:
1453 if (sshdr.asc == 0x10) /* DIX: Host detected corruption */
1454 good_bytes = sd_completed_bytes(SCpnt);
1455 /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */
1456 if ((sshdr.asc == 0x20 || sshdr.asc == 0x24) &&
1457 (op == UNMAP || op == WRITE_SAME_16 || op == WRITE_SAME))
1458 sd_config_discard(sdkp, SD_LBP_DISABLE);
1464 if (rq_data_dir(SCpnt->request) == READ && scsi_prot_sg_count(SCpnt))
1465 sd_dif_complete(SCpnt, good_bytes);
1471 * spinup disk - called only in sd_revalidate_disk()
1474 sd_spinup_disk(struct scsi_disk *sdkp)
1476 unsigned char cmd[10];
1477 unsigned long spintime_expire = 0;
1478 int retries, spintime;
1479 unsigned int the_result;
1480 struct scsi_sense_hdr sshdr;
1481 int sense_valid = 0;
1485 /* Spin up drives, as required. Only do this at boot time */
1486 /* Spinup needs to be done for module loads too. */
1491 cmd[0] = TEST_UNIT_READY;
1492 memset((void *) &cmd[1], 0, 9);
1494 the_result = scsi_execute_req(sdkp->device, cmd,
1497 SD_MAX_RETRIES, NULL);
1500 * If the drive has indicated to us that it
1501 * doesn't have any media in it, don't bother
1502 * with any more polling.
1504 if (media_not_present(sdkp, &sshdr))
1508 sense_valid = scsi_sense_valid(&sshdr);
1510 } while (retries < 3 &&
1511 (!scsi_status_is_good(the_result) ||
1512 ((driver_byte(the_result) & DRIVER_SENSE) &&
1513 sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
1515 if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
1516 /* no sense, TUR either succeeded or failed
1517 * with a status error */
1518 if(!spintime && !scsi_status_is_good(the_result)) {
1519 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1520 sd_print_result(sdkp, the_result);
1526 * The device does not want the automatic start to be issued.
1528 if (sdkp->device->no_start_on_add)
1531 if (sense_valid && sshdr.sense_key == NOT_READY) {
1532 if (sshdr.asc == 4 && sshdr.ascq == 3)
1533 break; /* manual intervention required */
1534 if (sshdr.asc == 4 && sshdr.ascq == 0xb)
1535 break; /* standby */
1536 if (sshdr.asc == 4 && sshdr.ascq == 0xc)
1537 break; /* unavailable */
1539 * Issue command to spin up drive when not ready
1542 sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
1543 cmd[0] = START_STOP;
1544 cmd[1] = 1; /* Return immediately */
1545 memset((void *) &cmd[2], 0, 8);
1546 cmd[4] = 1; /* Start spin cycle */
1547 if (sdkp->device->start_stop_pwr_cond)
1549 scsi_execute_req(sdkp->device, cmd, DMA_NONE,
1551 SD_TIMEOUT, SD_MAX_RETRIES,
1553 spintime_expire = jiffies + 100 * HZ;
1556 /* Wait 1 second for next try */
1561 * Wait for USB flash devices with slow firmware.
1562 * Yes, this sense key/ASC combination shouldn't
1563 * occur here. It's characteristic of these devices.
1565 } else if (sense_valid &&
1566 sshdr.sense_key == UNIT_ATTENTION &&
1567 sshdr.asc == 0x28) {
1569 spintime_expire = jiffies + 5 * HZ;
1572 /* Wait 1 second for next try */
1575 /* we don't understand the sense code, so it's
1576 * probably pointless to loop */
1578 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1579 sd_print_sense_hdr(sdkp, &sshdr);
1584 } while (spintime && time_before_eq(jiffies, spintime_expire));
1587 if (scsi_status_is_good(the_result))
1590 printk("not responding...\n");
1596 * Determine whether disk supports Data Integrity Field.
1598 static void sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer)
1600 struct scsi_device *sdp = sdkp->device;
1603 if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0)
1606 type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */
1608 if (type == sdkp->protection_type || !sdkp->first_scan)
1611 sdkp->protection_type = type;
1613 if (type > SD_DIF_TYPE3_PROTECTION) {
1614 sd_printk(KERN_ERR, sdkp, "formatted with unsupported " \
1615 "protection type %u. Disabling disk!\n", type);
1620 if (scsi_host_dif_capable(sdp->host, type))
1621 sd_printk(KERN_NOTICE, sdkp,
1622 "Enabling DIF Type %u protection\n", type);
1624 sd_printk(KERN_NOTICE, sdkp,
1625 "Disabling DIF Type %u protection\n", type);
1628 static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp,
1629 struct scsi_sense_hdr *sshdr, int sense_valid,
1632 sd_print_result(sdkp, the_result);
1633 if (driver_byte(the_result) & DRIVER_SENSE)
1634 sd_print_sense_hdr(sdkp, sshdr);
1636 sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
1639 * Set dirty bit for removable devices if not ready -
1640 * sometimes drives will not report this properly.
1642 if (sdp->removable &&
1643 sense_valid && sshdr->sense_key == NOT_READY)
1644 set_media_not_present(sdkp);
1647 * We used to set media_present to 0 here to indicate no media
1648 * in the drive, but some drives fail read capacity even with
1649 * media present, so we can't do that.
1651 sdkp->capacity = 0; /* unknown mapped to zero - as usual */
1655 #if RC16_LEN > SD_BUF_SIZE
1656 #error RC16_LEN must not be more than SD_BUF_SIZE
1659 #define READ_CAPACITY_RETRIES_ON_RESET 10
1662 * Ensure that we don't overflow sector_t when CONFIG_LBDAF is not set
1663 * and the reported logical block size is bigger than 512 bytes. Note
1664 * that last_sector is a u64 and therefore logical_to_sectors() is not
1667 static bool sd_addressable_capacity(u64 lba, unsigned int sector_size)
1669 u64 last_sector = (lba + 1ULL) << (ilog2(sector_size) - 9);
1671 if (sizeof(sector_t) == 4 && last_sector > 0xffffffffULL)
1677 static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
1678 unsigned char *buffer)
1680 unsigned char cmd[16];
1681 struct scsi_sense_hdr sshdr;
1682 int sense_valid = 0;
1684 int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
1685 unsigned int alignment;
1686 unsigned long long lba;
1687 unsigned sector_size;
1689 if (sdp->no_read_capacity_16)
1694 cmd[0] = SERVICE_ACTION_IN;
1695 cmd[1] = SAI_READ_CAPACITY_16;
1697 memset(buffer, 0, RC16_LEN);
1699 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
1700 buffer, RC16_LEN, &sshdr,
1701 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
1703 if (media_not_present(sdkp, &sshdr))
1707 sense_valid = scsi_sense_valid(&sshdr);
1709 sshdr.sense_key == ILLEGAL_REQUEST &&
1710 (sshdr.asc == 0x20 || sshdr.asc == 0x24) &&
1712 /* Invalid Command Operation Code or
1713 * Invalid Field in CDB, just retry
1714 * silently with RC10 */
1717 sshdr.sense_key == UNIT_ATTENTION &&
1718 sshdr.asc == 0x29 && sshdr.ascq == 0x00)
1719 /* Device reset might occur several times,
1720 * give it one more chance */
1721 if (--reset_retries > 0)
1726 } while (the_result && retries);
1729 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY(16) failed\n");
1730 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
1734 sector_size = get_unaligned_be32(&buffer[8]);
1735 lba = get_unaligned_be64(&buffer[0]);
1737 sd_read_protection_type(sdkp, buffer);
1739 if (!sd_addressable_capacity(lba, sector_size)) {
1740 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
1741 "kernel compiled with support for large block "
1747 /* Logical blocks per physical block exponent */
1748 sdkp->physical_block_size = (1 << (buffer[13] & 0xf)) * sector_size;
1750 /* Lowest aligned logical block */
1751 alignment = ((buffer[14] & 0x3f) << 8 | buffer[15]) * sector_size;
1752 blk_queue_alignment_offset(sdp->request_queue, alignment);
1753 if (alignment && sdkp->first_scan)
1754 sd_printk(KERN_NOTICE, sdkp,
1755 "physical block alignment offset: %u\n", alignment);
1757 if (buffer[14] & 0x80) { /* LBPME */
1760 if (buffer[14] & 0x40) /* LBPRZ */
1763 sd_config_discard(sdkp, SD_LBP_WS16);
1766 sdkp->capacity = lba + 1;
1770 static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,
1771 unsigned char *buffer)
1773 unsigned char cmd[16];
1774 struct scsi_sense_hdr sshdr;
1775 int sense_valid = 0;
1777 int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
1779 unsigned sector_size;
1782 cmd[0] = READ_CAPACITY;
1783 memset(&cmd[1], 0, 9);
1784 memset(buffer, 0, 8);
1786 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
1788 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
1790 if (media_not_present(sdkp, &sshdr))
1794 sense_valid = scsi_sense_valid(&sshdr);
1796 sshdr.sense_key == UNIT_ATTENTION &&
1797 sshdr.asc == 0x29 && sshdr.ascq == 0x00)
1798 /* Device reset might occur several times,
1799 * give it one more chance */
1800 if (--reset_retries > 0)
1805 } while (the_result && retries);
1808 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY failed\n");
1809 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
1813 sector_size = get_unaligned_be32(&buffer[4]);
1814 lba = get_unaligned_be32(&buffer[0]);
1816 if (sdp->no_read_capacity_16 && (lba == 0xffffffff)) {
1817 /* Some buggy (usb cardreader) devices return an lba of
1818 0xffffffff when the want to report a size of 0 (with
1819 which they really mean no media is present) */
1821 sdkp->physical_block_size = sector_size;
1825 if (!sd_addressable_capacity(lba, sector_size)) {
1826 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
1827 "kernel compiled with support for large block "
1833 sdkp->capacity = lba + 1;
1834 sdkp->physical_block_size = sector_size;
1838 static int sd_try_rc16_first(struct scsi_device *sdp)
1840 if (sdp->host->max_cmd_len < 16)
1842 if (sdp->scsi_level > SCSI_SPC_2)
1844 if (scsi_device_protection(sdp))
1850 * read disk capacity
1853 sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
1856 struct scsi_device *sdp = sdkp->device;
1857 sector_t old_capacity = sdkp->capacity;
1859 if (sd_try_rc16_first(sdp)) {
1860 sector_size = read_capacity_16(sdkp, sdp, buffer);
1861 if (sector_size == -EOVERFLOW)
1863 if (sector_size == -ENODEV)
1865 if (sector_size < 0)
1866 sector_size = read_capacity_10(sdkp, sdp, buffer);
1867 if (sector_size < 0)
1870 sector_size = read_capacity_10(sdkp, sdp, buffer);
1871 if (sector_size == -EOVERFLOW)
1873 if (sector_size < 0)
1875 if ((sizeof(sdkp->capacity) > 4) &&
1876 (sdkp->capacity > 0xffffffffULL)) {
1877 int old_sector_size = sector_size;
1878 sd_printk(KERN_NOTICE, sdkp, "Very big device. "
1879 "Trying to use READ CAPACITY(16).\n");
1880 sector_size = read_capacity_16(sdkp, sdp, buffer);
1881 if (sector_size < 0) {
1882 sd_printk(KERN_NOTICE, sdkp,
1883 "Using 0xffffffff as device size\n");
1884 sdkp->capacity = 1 + (sector_t) 0xffffffff;
1885 sector_size = old_sector_size;
1891 /* Some devices are known to return the total number of blocks,
1892 * not the highest block number. Some devices have versions
1893 * which do this and others which do not. Some devices we might
1894 * suspect of doing this but we don't know for certain.
1896 * If we know the reported capacity is wrong, decrement it. If
1897 * we can only guess, then assume the number of blocks is even
1898 * (usually true but not always) and err on the side of lowering
1901 if (sdp->fix_capacity ||
1902 (sdp->guess_capacity && (sdkp->capacity & 0x01))) {
1903 sd_printk(KERN_INFO, sdkp, "Adjusting the sector count "
1904 "from its reported value: %llu\n",
1905 (unsigned long long) sdkp->capacity);
1910 if (sector_size == 0) {
1912 sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
1916 if (sector_size != 512 &&
1917 sector_size != 1024 &&
1918 sector_size != 2048 &&
1919 sector_size != 4096) {
1920 sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
1923 * The user might want to re-format the drive with
1924 * a supported sectorsize. Once this happens, it
1925 * would be relatively trivial to set the thing up.
1926 * For this reason, we leave the thing in the table.
1930 * set a bogus sector size so the normal read/write
1931 * logic in the block layer will eventually refuse any
1932 * request on this device without tripping over power
1933 * of two sector size assumptions
1937 blk_queue_logical_block_size(sdp->request_queue, sector_size);
1940 char cap_str_2[10], cap_str_10[10];
1941 u64 sz = (u64)sdkp->capacity << ilog2(sector_size);
1943 string_get_size(sz, STRING_UNITS_2, cap_str_2,
1945 string_get_size(sz, STRING_UNITS_10, cap_str_10,
1946 sizeof(cap_str_10));
1948 if (sdkp->first_scan || old_capacity != sdkp->capacity) {
1949 sd_printk(KERN_NOTICE, sdkp,
1950 "%llu %d-byte logical blocks: (%s/%s)\n",
1951 (unsigned long long)sdkp->capacity,
1952 sector_size, cap_str_10, cap_str_2);
1954 if (sdkp->physical_block_size != sector_size)
1955 sd_printk(KERN_NOTICE, sdkp,
1956 "%u-byte physical blocks\n",
1957 sdkp->physical_block_size);
1961 blk_queue_physical_block_size(sdp->request_queue,
1962 sdkp->physical_block_size);
1963 sdkp->device->sector_size = sector_size;
1966 /* called with buffer of length 512 */
1968 sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
1969 unsigned char *buffer, int len, struct scsi_mode_data *data,
1970 struct scsi_sense_hdr *sshdr)
1972 return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
1973 SD_TIMEOUT, SD_MAX_RETRIES, data,
1978 * read write protect setting, if possible - called only in sd_revalidate_disk()
1979 * called with buffer of length SD_BUF_SIZE
1982 sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
1985 struct scsi_device *sdp = sdkp->device;
1986 struct scsi_mode_data data;
1987 int old_wp = sdkp->write_prot;
1989 set_disk_ro(sdkp->disk, 0);
1990 if (sdp->skip_ms_page_3f) {
1991 sd_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
1995 if (sdp->use_192_bytes_for_3f) {
1996 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
1999 * First attempt: ask for all pages (0x3F), but only 4 bytes.
2000 * We have to start carefully: some devices hang if we ask
2001 * for more than is available.
2003 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
2006 * Second attempt: ask for page 0 When only page 0 is
2007 * implemented, a request for page 3F may return Sense Key
2008 * 5: Illegal Request, Sense Code 24: Invalid field in
2011 if (!scsi_status_is_good(res))
2012 res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
2015 * Third attempt: ask 255 bytes, as we did earlier.
2017 if (!scsi_status_is_good(res))
2018 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
2022 if (!scsi_status_is_good(res)) {
2023 sd_printk(KERN_WARNING, sdkp,
2024 "Test WP failed, assume Write Enabled\n");
2026 sdkp->write_prot = ((data.device_specific & 0x80) != 0);
2027 set_disk_ro(sdkp->disk, sdkp->write_prot);
2028 if (sdkp->first_scan || old_wp != sdkp->write_prot) {
2029 sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
2030 sdkp->write_prot ? "on" : "off");
2031 sd_printk(KERN_DEBUG, sdkp,
2032 "Mode Sense: %02x %02x %02x %02x\n",
2033 buffer[0], buffer[1], buffer[2], buffer[3]);
2039 * sd_read_cache_type - called only from sd_revalidate_disk()
2040 * called with buffer of length SD_BUF_SIZE
2043 sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
2046 struct scsi_device *sdp = sdkp->device;
2051 struct scsi_mode_data data;
2052 struct scsi_sense_hdr sshdr;
2053 int old_wce = sdkp->WCE;
2054 int old_rcd = sdkp->RCD;
2055 int old_dpofua = sdkp->DPOFUA;
2058 if (sdkp->cache_override)
2062 if (sdp->skip_ms_page_8) {
2063 if (sdp->type == TYPE_RBC)
2066 if (sdp->skip_ms_page_3f)
2069 if (sdp->use_192_bytes_for_3f)
2073 } else if (sdp->type == TYPE_RBC) {
2081 /* cautiously ask */
2082 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, first_len,
2085 if (!scsi_status_is_good(res))
2088 if (!data.header_length) {
2091 sd_printk(KERN_ERR, sdkp, "Missing header in MODE_SENSE response\n");
2094 /* that went OK, now ask for the proper length */
2098 * We're only interested in the first three bytes, actually.
2099 * But the data cache page is defined for the first 20.
2103 else if (len > SD_BUF_SIZE) {
2104 sd_printk(KERN_NOTICE, sdkp, "Truncating mode parameter "
2105 "data from %d to %d bytes\n", len, SD_BUF_SIZE);
2108 if (modepage == 0x3F && sdp->use_192_bytes_for_3f)
2112 if (len > first_len)
2113 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len,
2116 if (scsi_status_is_good(res)) {
2117 int offset = data.header_length + data.block_descriptor_length;
2119 while (offset < len) {
2120 u8 page_code = buffer[offset] & 0x3F;
2121 u8 spf = buffer[offset] & 0x40;
2123 if (page_code == 8 || page_code == 6) {
2124 /* We're interested only in the first 3 bytes.
2126 if (len - offset <= 2) {
2127 sd_printk(KERN_ERR, sdkp, "Incomplete "
2128 "mode parameter data\n");
2131 modepage = page_code;
2135 /* Go to the next page */
2136 if (spf && len - offset > 3)
2137 offset += 4 + (buffer[offset+2] << 8) +
2139 else if (!spf && len - offset > 1)
2140 offset += 2 + buffer[offset+1];
2142 sd_printk(KERN_ERR, sdkp, "Incomplete "
2143 "mode parameter data\n");
2149 sd_printk(KERN_ERR, sdkp, "No Caching mode page found\n");
2153 if (modepage == 8) {
2154 sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
2155 sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
2157 sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
2161 sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
2162 if (sdp->broken_fua) {
2163 sd_printk(KERN_NOTICE, sdkp, "Disabling FUA\n");
2165 } else if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
2166 sd_printk(KERN_NOTICE, sdkp,
2167 "Uses READ/WRITE(6), disabling FUA\n");
2171 if (sdkp->first_scan || old_wce != sdkp->WCE ||
2172 old_rcd != sdkp->RCD || old_dpofua != sdkp->DPOFUA)
2173 sd_printk(KERN_NOTICE, sdkp,
2174 "Write cache: %s, read cache: %s, %s\n",
2175 sdkp->WCE ? "enabled" : "disabled",
2176 sdkp->RCD ? "disabled" : "enabled",
2177 sdkp->DPOFUA ? "supports DPO and FUA"
2178 : "doesn't support DPO or FUA");
2184 if (scsi_sense_valid(&sshdr) &&
2185 sshdr.sense_key == ILLEGAL_REQUEST &&
2186 sshdr.asc == 0x24 && sshdr.ascq == 0x0)
2187 /* Invalid field in CDB */
2188 sd_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
2190 sd_printk(KERN_ERR, sdkp, "Asking for cache data failed\n");
2193 sd_printk(KERN_ERR, sdkp, "Assuming drive cache: write through\n");
2200 * The ATO bit indicates whether the DIF application tag is available
2201 * for use by the operating system.
2203 static void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
2206 struct scsi_device *sdp = sdkp->device;
2207 struct scsi_mode_data data;
2208 struct scsi_sense_hdr sshdr;
2210 if (sdp->type != TYPE_DISK)
2213 if (sdkp->protection_type == 0)
2216 res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT,
2217 SD_MAX_RETRIES, &data, &sshdr);
2219 if (!scsi_status_is_good(res) || !data.header_length ||
2221 sd_printk(KERN_WARNING, sdkp,
2222 "getting Control mode page failed, assume no ATO\n");
2224 if (scsi_sense_valid(&sshdr))
2225 sd_print_sense_hdr(sdkp, &sshdr);
2230 offset = data.header_length + data.block_descriptor_length;
2232 if ((buffer[offset] & 0x3f) != 0x0a) {
2233 sd_printk(KERN_ERR, sdkp, "ATO Got wrong page\n");
2237 if ((buffer[offset + 5] & 0x80) == 0)
2246 * sd_read_block_limits - Query disk device for preferred I/O sizes.
2247 * @disk: disk to query
2249 static void sd_read_block_limits(struct scsi_disk *sdkp)
2251 unsigned int sector_sz = sdkp->device->sector_size;
2252 const int vpd_len = 64;
2253 unsigned char *buffer = kmalloc(vpd_len, GFP_KERNEL);
2256 /* Block Limits VPD */
2257 scsi_get_vpd_page(sdkp->device, 0xb0, buffer, vpd_len))
2260 blk_queue_io_min(sdkp->disk->queue,
2261 get_unaligned_be16(&buffer[6]) * sector_sz);
2262 blk_queue_io_opt(sdkp->disk->queue,
2263 get_unaligned_be32(&buffer[12]) * sector_sz);
2265 if (buffer[3] == 0x3c) {
2266 unsigned int lba_count, desc_count;
2268 sdkp->max_ws_blocks =
2269 (u32) min_not_zero(get_unaligned_be64(&buffer[36]),
2275 lba_count = get_unaligned_be32(&buffer[20]);
2276 desc_count = get_unaligned_be32(&buffer[24]);
2278 if (lba_count && desc_count)
2279 sdkp->max_unmap_blocks = lba_count;
2281 sdkp->unmap_granularity = get_unaligned_be32(&buffer[28]);
2283 if (buffer[32] & 0x80)
2284 sdkp->unmap_alignment =
2285 get_unaligned_be32(&buffer[32]) & ~(1 << 31);
2287 if (!sdkp->lbpvpd) { /* LBP VPD page not provided */
2289 if (sdkp->max_unmap_blocks)
2290 sd_config_discard(sdkp, SD_LBP_UNMAP);
2292 sd_config_discard(sdkp, SD_LBP_WS16);
2294 } else { /* LBP VPD page tells us what to use */
2296 if (sdkp->lbpu && sdkp->max_unmap_blocks)
2297 sd_config_discard(sdkp, SD_LBP_UNMAP);
2298 else if (sdkp->lbpws)
2299 sd_config_discard(sdkp, SD_LBP_WS16);
2300 else if (sdkp->lbpws10)
2301 sd_config_discard(sdkp, SD_LBP_WS10);
2303 sd_config_discard(sdkp, SD_LBP_DISABLE);
2312 * sd_read_block_characteristics - Query block dev. characteristics
2313 * @disk: disk to query
2315 static void sd_read_block_characteristics(struct scsi_disk *sdkp)
2317 unsigned char *buffer;
2319 const int vpd_len = 64;
2321 buffer = kmalloc(vpd_len, GFP_KERNEL);
2324 /* Block Device Characteristics VPD */
2325 scsi_get_vpd_page(sdkp->device, 0xb1, buffer, vpd_len))
2328 rot = get_unaligned_be16(&buffer[4]);
2331 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, sdkp->disk->queue);
2338 * sd_read_block_provisioning - Query provisioning VPD page
2339 * @disk: disk to query
2341 static void sd_read_block_provisioning(struct scsi_disk *sdkp)
2343 unsigned char *buffer;
2344 const int vpd_len = 8;
2346 if (sdkp->lbpme == 0)
2349 buffer = kmalloc(vpd_len, GFP_KERNEL);
2351 if (!buffer || scsi_get_vpd_page(sdkp->device, 0xb2, buffer, vpd_len))
2355 sdkp->lbpu = (buffer[5] >> 7) & 1; /* UNMAP */
2356 sdkp->lbpws = (buffer[5] >> 6) & 1; /* WRITE SAME(16) with UNMAP */
2357 sdkp->lbpws10 = (buffer[5] >> 5) & 1; /* WRITE SAME(10) with UNMAP */
2363 static int sd_try_extended_inquiry(struct scsi_device *sdp)
2366 * Although VPD inquiries can go to SCSI-2 type devices,
2367 * some USB ones crash on receiving them, and the pages
2368 * we currently ask for are for SPC-3 and beyond
2370 if (sdp->scsi_level > SCSI_SPC_2)
2376 * sd_revalidate_disk - called the first time a new disk is seen,
2377 * performs disk spin up, read_capacity, etc.
2378 * @disk: struct gendisk we care about
2380 static int sd_revalidate_disk(struct gendisk *disk)
2382 struct scsi_disk *sdkp = scsi_disk(disk);
2383 struct scsi_device *sdp = sdkp->device;
2384 unsigned char *buffer;
2387 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
2388 "sd_revalidate_disk\n"));
2391 * If the device is offline, don't try and read capacity or any
2392 * of the other niceties.
2394 if (!scsi_device_online(sdp))
2397 buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL);
2399 sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
2400 "allocation failure.\n");
2404 sd_spinup_disk(sdkp);
2407 * Without media there is no reason to ask; moreover, some devices
2408 * react badly if we do.
2410 if (sdkp->media_present) {
2411 sd_read_capacity(sdkp, buffer);
2413 if (sd_try_extended_inquiry(sdp)) {
2414 sd_read_block_provisioning(sdkp);
2415 sd_read_block_limits(sdkp);
2416 sd_read_block_characteristics(sdkp);
2419 sd_read_write_protect_flag(sdkp, buffer);
2420 sd_read_cache_type(sdkp, buffer);
2421 sd_read_app_tag_own(sdkp, buffer);
2424 sdkp->first_scan = 0;
2427 * We now have all cache related info, determine how we deal
2428 * with flush requests.
2436 blk_queue_flush(sdkp->disk->queue, flush);
2438 set_capacity(disk, logical_to_sectors(sdp, sdkp->capacity));
2446 * sd_unlock_native_capacity - unlock native capacity
2447 * @disk: struct gendisk to set capacity for
2449 * Block layer calls this function if it detects that partitions
2450 * on @disk reach beyond the end of the device. If the SCSI host
2451 * implements ->unlock_native_capacity() method, it's invoked to
2452 * give it a chance to adjust the device capacity.
2455 * Defined by block layer. Might sleep.
2457 static void sd_unlock_native_capacity(struct gendisk *disk)
2459 struct scsi_device *sdev = scsi_disk(disk)->device;
2461 if (sdev->host->hostt->unlock_native_capacity)
2462 sdev->host->hostt->unlock_native_capacity(sdev);
2466 * sd_format_disk_name - format disk name
2467 * @prefix: name prefix - ie. "sd" for SCSI disks
2468 * @index: index of the disk to format name for
2469 * @buf: output buffer
2470 * @buflen: length of the output buffer
2472 * SCSI disk names starts at sda. The 26th device is sdz and the
2473 * 27th is sdaa. The last one for two lettered suffix is sdzz
2474 * which is followed by sdaaa.
2476 * This is basically 26 base counting with one extra 'nil' entry
2477 * at the beginning from the second digit on and can be
2478 * determined using similar method as 26 base conversion with the
2479 * index shifted -1 after each digit is computed.
2485 * 0 on success, -errno on failure.
2487 static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen)
2489 const int base = 'z' - 'a' + 1;
2490 char *begin = buf + strlen(prefix);
2491 char *end = buf + buflen;
2501 *--p = 'a' + (index % unit);
2502 index = (index / unit) - 1;
2503 } while (index >= 0);
2505 memmove(begin, p, end - p);
2506 memcpy(buf, prefix, strlen(prefix));
2512 * The asynchronous part of sd_probe
2514 static void sd_probe_async(void *data, async_cookie_t cookie)
2516 struct scsi_disk *sdkp = data;
2517 struct scsi_device *sdp;
2524 index = sdkp->index;
2525 dev = &sdp->sdev_gendev;
2527 gd->major = sd_major((index & 0xf0) >> 4);
2528 gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
2529 gd->minors = SD_MINORS;
2531 gd->fops = &sd_fops;
2532 gd->private_data = &sdkp->driver;
2533 gd->queue = sdkp->device->request_queue;
2535 /* defaults, until the device tells us otherwise */
2536 sdp->sector_size = 512;
2538 sdkp->media_present = 1;
2539 sdkp->write_prot = 0;
2540 sdkp->cache_override = 0;
2544 sdkp->first_scan = 1;
2546 sd_revalidate_disk(gd);
2548 blk_queue_prep_rq(sdp->request_queue, sd_prep_fn);
2549 blk_queue_unprep_rq(sdp->request_queue, sd_unprep_fn);
2551 gd->driverfs_dev = &sdp->sdev_gendev;
2552 gd->flags = GENHD_FL_EXT_DEVT;
2553 if (sdp->removable) {
2554 gd->flags |= GENHD_FL_REMOVABLE;
2555 gd->events |= DISK_EVENT_MEDIA_CHANGE;
2559 sd_dif_config_host(sdkp);
2561 sd_revalidate_disk(gd);
2563 sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
2564 sdp->removable ? "removable " : "");
2565 scsi_autopm_put_device(sdp);
2566 put_device(&sdkp->dev);
2570 * sd_probe - called during driver initialization and whenever a
2571 * new scsi device is attached to the system. It is called once
2572 * for each scsi device (not just disks) present.
2573 * @dev: pointer to device object
2575 * Returns 0 if successful (or not interested in this scsi device
2576 * (e.g. scanner)); 1 when there is an error.
2578 * Note: this function is invoked from the scsi mid-level.
2579 * This function sets up the mapping between a given
2580 * <host,channel,id,lun> (found in sdp) and new device name
2581 * (e.g. /dev/sda). More precisely it is the block device major
2582 * and minor number that is chosen here.
2584 * Assume sd_attach is not re-entrant (for time being)
2585 * Also think about sd_attach() and sd_remove() running coincidentally.
2587 static int sd_probe(struct device *dev)
2589 struct scsi_device *sdp = to_scsi_device(dev);
2590 struct scsi_disk *sdkp;
2596 if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
2599 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
2603 sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
2607 gd = alloc_disk(SD_MINORS);
2612 if (!ida_pre_get(&sd_index_ida, GFP_KERNEL))
2615 spin_lock(&sd_index_lock);
2616 error = ida_get_new(&sd_index_ida, &index);
2617 spin_unlock(&sd_index_lock);
2618 } while (error == -EAGAIN);
2621 sdev_printk(KERN_WARNING, sdp, "sd_probe: memory exhausted.\n");
2625 error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN);
2627 sdev_printk(KERN_WARNING, sdp, "SCSI disk (sd) name length exceeded.\n");
2628 goto out_free_index;
2632 sdkp->driver = &sd_template;
2634 sdkp->index = index;
2635 atomic_set(&sdkp->openers, 0);
2637 if (!sdp->request_queue->rq_timeout) {
2638 if (sdp->type != TYPE_MOD)
2639 blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT);
2641 blk_queue_rq_timeout(sdp->request_queue,
2645 device_initialize(&sdkp->dev);
2646 sdkp->dev.parent = dev;
2647 sdkp->dev.class = &sd_disk_class;
2648 dev_set_name(&sdkp->dev, dev_name(dev));
2650 if (device_add(&sdkp->dev))
2651 goto out_free_index;
2654 dev_set_drvdata(dev, sdkp);
2656 get_device(&sdkp->dev); /* prevent release before async_schedule */
2657 async_schedule(sd_probe_async, sdkp);
2662 spin_lock(&sd_index_lock);
2663 ida_remove(&sd_index_ida, index);
2664 spin_unlock(&sd_index_lock);
2674 * sd_remove - called whenever a scsi disk (previously recognized by
2675 * sd_probe) is detached from the system. It is called (potentially
2676 * multiple times) during sd module unload.
2677 * @sdp: pointer to mid level scsi device object
2679 * Note: this function is invoked from the scsi mid-level.
2680 * This function potentially frees up a device name (e.g. /dev/sdc)
2681 * that could be re-used by a subsequent sd_probe().
2682 * This function is not called when the built-in sd driver is "exit-ed".
2684 static int sd_remove(struct device *dev)
2686 struct scsi_disk *sdkp;
2688 sdkp = dev_get_drvdata(dev);
2689 scsi_autopm_get_device(sdkp->device);
2691 async_synchronize_full();
2692 blk_queue_prep_rq(sdkp->device->request_queue, scsi_prep_fn);
2693 blk_queue_unprep_rq(sdkp->device->request_queue, NULL);
2694 device_del(&sdkp->dev);
2695 del_gendisk(sdkp->disk);
2698 mutex_lock(&sd_ref_mutex);
2699 dev_set_drvdata(dev, NULL);
2700 put_device(&sdkp->dev);
2701 mutex_unlock(&sd_ref_mutex);
2707 * scsi_disk_release - Called to free the scsi_disk structure
2708 * @dev: pointer to embedded class device
2710 * sd_ref_mutex must be held entering this routine. Because it is
2711 * called on last put, you should always use the scsi_disk_get()
2712 * scsi_disk_put() helpers which manipulate the semaphore directly
2713 * and never do a direct put_device.
2715 static void scsi_disk_release(struct device *dev)
2717 struct scsi_disk *sdkp = to_scsi_disk(dev);
2718 struct gendisk *disk = sdkp->disk;
2720 spin_lock(&sd_index_lock);
2721 ida_remove(&sd_index_ida, sdkp->index);
2722 spin_unlock(&sd_index_lock);
2724 disk->private_data = NULL;
2726 put_device(&sdkp->device->sdev_gendev);
2731 static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
2733 unsigned char cmd[6] = { START_STOP }; /* START_VALID */
2734 struct scsi_sense_hdr sshdr;
2735 struct scsi_device *sdp = sdkp->device;
2739 cmd[4] |= 1; /* START */
2741 if (sdp->start_stop_pwr_cond)
2742 cmd[4] |= start ? 1 << 4 : 3 << 4; /* Active or Standby */
2744 if (!scsi_device_online(sdp))
2747 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
2748 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
2750 sd_printk(KERN_WARNING, sdkp, "START_STOP FAILED\n");
2751 sd_print_result(sdkp, res);
2752 if (driver_byte(res) & DRIVER_SENSE)
2753 sd_print_sense_hdr(sdkp, &sshdr);
2760 * Send a SYNCHRONIZE CACHE instruction down to the device through
2761 * the normal SCSI command structure. Wait for the command to
2764 static void sd_shutdown(struct device *dev)
2766 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
2769 return; /* this can happen */
2772 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
2773 sd_sync_cache(sdkp);
2776 if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
2777 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
2778 sd_start_stop_device(sdkp, 0);
2781 scsi_disk_put(sdkp);
2784 static int sd_suspend(struct device *dev, pm_message_t mesg)
2786 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
2789 if (!sdkp) /* E.g.: runtime suspend following sd_remove() */
2793 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
2794 ret = sd_sync_cache(sdkp);
2799 if ((mesg.event & PM_EVENT_SLEEP) && sdkp->device->manage_start_stop) {
2800 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
2801 ret = sd_start_stop_device(sdkp, 0);
2805 scsi_disk_put(sdkp);
2809 static int sd_resume(struct device *dev)
2811 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
2814 if (!sdkp) /* E.g.: runtime resume at the start of sd_probe() */
2817 if (!sdkp->device->manage_start_stop)
2820 sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
2821 ret = sd_start_stop_device(sdkp, 1);
2824 scsi_disk_put(sdkp);
2829 * init_sd - entry point for this driver (both when built in or when
2832 * Note: this function registers this driver with the scsi mid-level.
2834 static int __init init_sd(void)
2836 int majors = 0, i, err;
2838 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
2840 for (i = 0; i < SD_MAJORS; i++)
2841 if (register_blkdev(sd_major(i), "sd") == 0)
2847 err = class_register(&sd_disk_class);
2851 sd_cdb_cache = kmem_cache_create("sd_ext_cdb", SD_EXT_CDB_SIZE,
2853 if (!sd_cdb_cache) {
2854 printk(KERN_ERR "sd: can't init extended cdb cache\n");
2858 sd_cdb_pool = mempool_create_slab_pool(SD_MEMPOOL_SIZE, sd_cdb_cache);
2860 printk(KERN_ERR "sd: can't init extended cdb pool\n");
2864 err = scsi_register_driver(&sd_template.gendrv);
2866 goto err_out_driver;
2871 mempool_destroy(sd_cdb_pool);
2874 kmem_cache_destroy(sd_cdb_cache);
2877 class_unregister(&sd_disk_class);
2879 for (i = 0; i < SD_MAJORS; i++)
2880 unregister_blkdev(sd_major(i), "sd");
2885 * exit_sd - exit point for this driver (when it is a module).
2887 * Note: this function unregisters this driver from the scsi mid-level.
2889 static void __exit exit_sd(void)
2893 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
2895 scsi_unregister_driver(&sd_template.gendrv);
2896 mempool_destroy(sd_cdb_pool);
2897 kmem_cache_destroy(sd_cdb_cache);
2899 class_unregister(&sd_disk_class);
2901 for (i = 0; i < SD_MAJORS; i++)
2902 unregister_blkdev(sd_major(i), "sd");
2905 module_init(init_sd);
2906 module_exit(exit_sd);
2908 static void sd_print_sense_hdr(struct scsi_disk *sdkp,
2909 struct scsi_sense_hdr *sshdr)
2911 sd_printk(KERN_INFO, sdkp, " ");
2912 scsi_show_sense_hdr(sshdr);
2913 sd_printk(KERN_INFO, sdkp, " ");
2914 scsi_show_extd_sense(sshdr->asc, sshdr->ascq);
2917 static void sd_print_result(struct scsi_disk *sdkp, int result)
2919 sd_printk(KERN_INFO, sdkp, " ");
2920 scsi_show_result(result);