1 // SPDX-License-Identifier: GPL-2.0-only
3 * sd.c Copyright (C) 1992 Drew Eckhardt
4 * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
6 * Linux scsi disk driver
7 * Initial versions: Drew Eckhardt
8 * Subsequent revisions: Eric Youngdale
9 * Modification history:
10 * - Drew Eckhardt <drew@colorado.edu> original
11 * - Eric Youngdale <eric@andante.org> add scatter-gather, multiple
12 * outstanding request, and other enhancements.
13 * Support loadable low-level scsi drivers.
14 * - Jirka Hanika <geo@ff.cuni.cz> support more scsi disks using
15 * eight major numbers.
16 * - Richard Gooch <rgooch@atnf.csiro.au> support devfs.
17 * - Torben Mathiasen <tmm@image.dk> Resource allocation fixes in
18 * sd_init and cleanups.
19 * - Alex Davis <letmein@erols.com> Fix problem where partition info
20 * not being read in sd_open. Fix problem where removable media
21 * could be ejected after sd_open.
22 * - Douglas Gilbert <dgilbert@interlog.com> cleanup for lk 2.5.x
23 * - Badari Pulavarty <pbadari@us.ibm.com>, Matthew Wilcox
24 * <willy@debian.org>, Kurt Garloff <garloff@suse.de>:
25 * Support 32k/1M disks.
27 * Logging policy (needs CONFIG_SCSI_LOGGING defined):
28 * - setting up transfer: SCSI_LOG_HLQUEUE levels 1 and 2
29 * - end of transfer (bh + scsi_lib): SCSI_LOG_HLCOMPLETE level 1
30 * - entering sd_ioctl: SCSI_LOG_IOCTL level 1
31 * - entering other commands: SCSI_LOG_HLQUEUE level 3
32 * Note: when the logging level is set by the user, it must be greater
33 * than the level indicated above to trigger output.
36 #include <linux/module.h>
38 #include <linux/kernel.h>
40 #include <linux/bio.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/blk-pm.h>
49 #include <linux/delay.h>
50 #include <linux/major.h>
51 #include <linux/mutex.h>
52 #include <linux/string_helpers.h>
53 #include <linux/slab.h>
54 #include <linux/sed-opal.h>
55 #include <linux/pm_runtime.h>
57 #include <linux/t10-pi.h>
58 #include <linux/uaccess.h>
59 #include <asm/unaligned.h>
61 #include <scsi/scsi.h>
62 #include <scsi/scsi_cmnd.h>
63 #include <scsi/scsi_dbg.h>
64 #include <scsi/scsi_device.h>
65 #include <scsi/scsi_driver.h>
66 #include <scsi/scsi_eh.h>
67 #include <scsi/scsi_host.h>
68 #include <scsi/scsi_ioctl.h>
69 #include <scsi/scsicam.h>
72 #include "scsi_priv.h"
73 #include "scsi_logging.h"
75 MODULE_AUTHOR("Eric Youngdale");
76 MODULE_DESCRIPTION("SCSI disk (sd) driver");
77 MODULE_LICENSE("GPL");
79 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR);
80 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR);
81 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR);
82 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR);
83 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR);
84 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR);
85 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR);
86 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR);
87 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR);
88 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR);
89 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR);
90 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR);
91 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
92 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
93 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
94 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
95 MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK);
96 MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD);
97 MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC);
98 MODULE_ALIAS_SCSI_DEVICE(TYPE_ZBC);
102 static void sd_config_discard(struct scsi_disk *, unsigned int);
103 static void sd_config_write_same(struct scsi_disk *);
104 static int sd_revalidate_disk(struct gendisk *);
105 static void sd_unlock_native_capacity(struct gendisk *disk);
106 static int sd_probe(struct device *);
107 static int sd_remove(struct device *);
108 static void sd_shutdown(struct device *);
109 static int sd_suspend_system(struct device *);
110 static int sd_suspend_runtime(struct device *);
111 static int sd_resume_system(struct device *);
112 static int sd_resume_runtime(struct device *);
113 static void sd_rescan(struct device *);
114 static blk_status_t sd_init_command(struct scsi_cmnd *SCpnt);
115 static void sd_uninit_command(struct scsi_cmnd *SCpnt);
116 static int sd_done(struct scsi_cmnd *);
117 static void sd_eh_reset(struct scsi_cmnd *);
118 static int sd_eh_action(struct scsi_cmnd *, int);
119 static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
120 static void scsi_disk_release(struct device *cdev);
122 static DEFINE_IDA(sd_index_ida);
124 static struct kmem_cache *sd_cdb_cache;
125 static mempool_t *sd_cdb_pool;
126 static mempool_t *sd_page_pool;
127 static struct lock_class_key sd_bio_compl_lkclass;
129 static const char *sd_cache_types[] = {
130 "write through", "none", "write back",
131 "write back, no read (daft)"
134 static void sd_set_flush_flag(struct scsi_disk *sdkp)
136 bool wc = false, fua = false;
144 blk_queue_write_cache(sdkp->disk->queue, wc, fua);
148 cache_type_store(struct device *dev, struct device_attribute *attr,
149 const char *buf, size_t count)
151 int ct, rcd, wce, sp;
152 struct scsi_disk *sdkp = to_scsi_disk(dev);
153 struct scsi_device *sdp = sdkp->device;
156 struct scsi_mode_data data;
157 struct scsi_sense_hdr sshdr;
158 static const char temp[] = "temporary ";
161 if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC)
162 /* no cache control on RBC devices; theoretically they
163 * can do it, but there's probably so many exceptions
164 * it's not worth the risk */
167 if (strncmp(buf, temp, sizeof(temp) - 1) == 0) {
168 buf += sizeof(temp) - 1;
169 sdkp->cache_override = 1;
171 sdkp->cache_override = 0;
174 ct = sysfs_match_string(sd_cache_types, buf);
178 rcd = ct & 0x01 ? 1 : 0;
179 wce = (ct & 0x02) && !sdkp->write_prot ? 1 : 0;
181 if (sdkp->cache_override) {
184 sd_set_flush_flag(sdkp);
188 if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT,
189 sdkp->max_retries, &data, NULL))
191 len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
192 data.block_descriptor_length);
193 buffer_data = buffer + data.header_length +
194 data.block_descriptor_length;
195 buffer_data[2] &= ~0x05;
196 buffer_data[2] |= wce << 2 | rcd;
197 sp = buffer_data[0] & 0x80 ? 1 : 0;
198 buffer_data[0] &= ~0x80;
201 * Ensure WP, DPOFUA, and RESERVED fields are cleared in
202 * received mode parameter buffer before doing MODE SELECT.
204 data.device_specific = 0;
206 if (scsi_mode_select(sdp, 1, sp, buffer_data, len, SD_TIMEOUT,
207 sdkp->max_retries, &data, &sshdr)) {
208 if (scsi_sense_valid(&sshdr))
209 sd_print_sense_hdr(sdkp, &sshdr);
212 sd_revalidate_disk(sdkp->disk);
217 manage_start_stop_show(struct device *dev, struct device_attribute *attr,
220 struct scsi_disk *sdkp = to_scsi_disk(dev);
221 struct scsi_device *sdp = sdkp->device;
223 return sprintf(buf, "%u\n", sdp->manage_start_stop);
227 manage_start_stop_store(struct device *dev, struct device_attribute *attr,
228 const char *buf, size_t count)
230 struct scsi_disk *sdkp = to_scsi_disk(dev);
231 struct scsi_device *sdp = sdkp->device;
234 if (!capable(CAP_SYS_ADMIN))
237 if (kstrtobool(buf, &v))
240 sdp->manage_start_stop = v;
244 static DEVICE_ATTR_RW(manage_start_stop);
247 allow_restart_show(struct device *dev, struct device_attribute *attr, char *buf)
249 struct scsi_disk *sdkp = to_scsi_disk(dev);
251 return sprintf(buf, "%u\n", sdkp->device->allow_restart);
255 allow_restart_store(struct device *dev, struct device_attribute *attr,
256 const char *buf, size_t count)
259 struct scsi_disk *sdkp = to_scsi_disk(dev);
260 struct scsi_device *sdp = sdkp->device;
262 if (!capable(CAP_SYS_ADMIN))
265 if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC)
268 if (kstrtobool(buf, &v))
271 sdp->allow_restart = v;
275 static DEVICE_ATTR_RW(allow_restart);
278 cache_type_show(struct device *dev, struct device_attribute *attr, char *buf)
280 struct scsi_disk *sdkp = to_scsi_disk(dev);
281 int ct = sdkp->RCD + 2*sdkp->WCE;
283 return sprintf(buf, "%s\n", sd_cache_types[ct]);
285 static DEVICE_ATTR_RW(cache_type);
288 FUA_show(struct device *dev, struct device_attribute *attr, char *buf)
290 struct scsi_disk *sdkp = to_scsi_disk(dev);
292 return sprintf(buf, "%u\n", sdkp->DPOFUA);
294 static DEVICE_ATTR_RO(FUA);
297 protection_type_show(struct device *dev, struct device_attribute *attr,
300 struct scsi_disk *sdkp = to_scsi_disk(dev);
302 return sprintf(buf, "%u\n", sdkp->protection_type);
306 protection_type_store(struct device *dev, struct device_attribute *attr,
307 const char *buf, size_t count)
309 struct scsi_disk *sdkp = to_scsi_disk(dev);
313 if (!capable(CAP_SYS_ADMIN))
316 err = kstrtouint(buf, 10, &val);
321 if (val <= T10_PI_TYPE3_PROTECTION)
322 sdkp->protection_type = val;
326 static DEVICE_ATTR_RW(protection_type);
329 protection_mode_show(struct device *dev, struct device_attribute *attr,
332 struct scsi_disk *sdkp = to_scsi_disk(dev);
333 struct scsi_device *sdp = sdkp->device;
334 unsigned int dif, dix;
336 dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type);
337 dix = scsi_host_dix_capable(sdp->host, sdkp->protection_type);
339 if (!dix && scsi_host_dix_capable(sdp->host, T10_PI_TYPE0_PROTECTION)) {
345 return sprintf(buf, "none\n");
347 return sprintf(buf, "%s%u\n", dix ? "dix" : "dif", dif);
349 static DEVICE_ATTR_RO(protection_mode);
352 app_tag_own_show(struct device *dev, struct device_attribute *attr, char *buf)
354 struct scsi_disk *sdkp = to_scsi_disk(dev);
356 return sprintf(buf, "%u\n", sdkp->ATO);
358 static DEVICE_ATTR_RO(app_tag_own);
361 thin_provisioning_show(struct device *dev, struct device_attribute *attr,
364 struct scsi_disk *sdkp = to_scsi_disk(dev);
366 return sprintf(buf, "%u\n", sdkp->lbpme);
368 static DEVICE_ATTR_RO(thin_provisioning);
370 /* sysfs_match_string() requires dense arrays */
371 static const char *lbp_mode[] = {
372 [SD_LBP_FULL] = "full",
373 [SD_LBP_UNMAP] = "unmap",
374 [SD_LBP_WS16] = "writesame_16",
375 [SD_LBP_WS10] = "writesame_10",
376 [SD_LBP_ZERO] = "writesame_zero",
377 [SD_LBP_DISABLE] = "disabled",
381 provisioning_mode_show(struct device *dev, struct device_attribute *attr,
384 struct scsi_disk *sdkp = to_scsi_disk(dev);
386 return sprintf(buf, "%s\n", lbp_mode[sdkp->provisioning_mode]);
390 provisioning_mode_store(struct device *dev, struct device_attribute *attr,
391 const char *buf, size_t count)
393 struct scsi_disk *sdkp = to_scsi_disk(dev);
394 struct scsi_device *sdp = sdkp->device;
397 if (!capable(CAP_SYS_ADMIN))
400 if (sd_is_zoned(sdkp)) {
401 sd_config_discard(sdkp, SD_LBP_DISABLE);
405 if (sdp->type != TYPE_DISK)
408 mode = sysfs_match_string(lbp_mode, buf);
412 sd_config_discard(sdkp, mode);
416 static DEVICE_ATTR_RW(provisioning_mode);
418 /* sysfs_match_string() requires dense arrays */
419 static const char *zeroing_mode[] = {
420 [SD_ZERO_WRITE] = "write",
421 [SD_ZERO_WS] = "writesame",
422 [SD_ZERO_WS16_UNMAP] = "writesame_16_unmap",
423 [SD_ZERO_WS10_UNMAP] = "writesame_10_unmap",
427 zeroing_mode_show(struct device *dev, struct device_attribute *attr,
430 struct scsi_disk *sdkp = to_scsi_disk(dev);
432 return sprintf(buf, "%s\n", zeroing_mode[sdkp->zeroing_mode]);
436 zeroing_mode_store(struct device *dev, struct device_attribute *attr,
437 const char *buf, size_t count)
439 struct scsi_disk *sdkp = to_scsi_disk(dev);
442 if (!capable(CAP_SYS_ADMIN))
445 mode = sysfs_match_string(zeroing_mode, buf);
449 sdkp->zeroing_mode = mode;
453 static DEVICE_ATTR_RW(zeroing_mode);
456 max_medium_access_timeouts_show(struct device *dev,
457 struct device_attribute *attr, char *buf)
459 struct scsi_disk *sdkp = to_scsi_disk(dev);
461 return sprintf(buf, "%u\n", sdkp->max_medium_access_timeouts);
465 max_medium_access_timeouts_store(struct device *dev,
466 struct device_attribute *attr, const char *buf,
469 struct scsi_disk *sdkp = to_scsi_disk(dev);
472 if (!capable(CAP_SYS_ADMIN))
475 err = kstrtouint(buf, 10, &sdkp->max_medium_access_timeouts);
477 return err ? err : count;
479 static DEVICE_ATTR_RW(max_medium_access_timeouts);
482 max_write_same_blocks_show(struct device *dev, struct device_attribute *attr,
485 struct scsi_disk *sdkp = to_scsi_disk(dev);
487 return sprintf(buf, "%u\n", sdkp->max_ws_blocks);
491 max_write_same_blocks_store(struct device *dev, struct device_attribute *attr,
492 const char *buf, size_t count)
494 struct scsi_disk *sdkp = to_scsi_disk(dev);
495 struct scsi_device *sdp = sdkp->device;
499 if (!capable(CAP_SYS_ADMIN))
502 if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC)
505 err = kstrtoul(buf, 10, &max);
511 sdp->no_write_same = 1;
512 else if (max <= SD_MAX_WS16_BLOCKS) {
513 sdp->no_write_same = 0;
514 sdkp->max_ws_blocks = max;
517 sd_config_write_same(sdkp);
521 static DEVICE_ATTR_RW(max_write_same_blocks);
524 zoned_cap_show(struct device *dev, struct device_attribute *attr, char *buf)
526 struct scsi_disk *sdkp = to_scsi_disk(dev);
528 if (sdkp->device->type == TYPE_ZBC)
529 return sprintf(buf, "host-managed\n");
530 if (sdkp->zoned == 1)
531 return sprintf(buf, "host-aware\n");
532 if (sdkp->zoned == 2)
533 return sprintf(buf, "drive-managed\n");
534 return sprintf(buf, "none\n");
536 static DEVICE_ATTR_RO(zoned_cap);
539 max_retries_store(struct device *dev, struct device_attribute *attr,
540 const char *buf, size_t count)
542 struct scsi_disk *sdkp = to_scsi_disk(dev);
543 struct scsi_device *sdev = sdkp->device;
546 err = kstrtoint(buf, 10, &retries);
550 if (retries == SCSI_CMD_RETRIES_NO_LIMIT || retries <= SD_MAX_RETRIES) {
551 sdkp->max_retries = retries;
555 sdev_printk(KERN_ERR, sdev, "max_retries must be between -1 and %d\n",
561 max_retries_show(struct device *dev, struct device_attribute *attr,
564 struct scsi_disk *sdkp = to_scsi_disk(dev);
566 return sprintf(buf, "%d\n", sdkp->max_retries);
569 static DEVICE_ATTR_RW(max_retries);
571 static struct attribute *sd_disk_attrs[] = {
572 &dev_attr_cache_type.attr,
574 &dev_attr_allow_restart.attr,
575 &dev_attr_manage_start_stop.attr,
576 &dev_attr_protection_type.attr,
577 &dev_attr_protection_mode.attr,
578 &dev_attr_app_tag_own.attr,
579 &dev_attr_thin_provisioning.attr,
580 &dev_attr_provisioning_mode.attr,
581 &dev_attr_zeroing_mode.attr,
582 &dev_attr_max_write_same_blocks.attr,
583 &dev_attr_max_medium_access_timeouts.attr,
584 &dev_attr_zoned_cap.attr,
585 &dev_attr_max_retries.attr,
588 ATTRIBUTE_GROUPS(sd_disk);
590 static struct class sd_disk_class = {
592 .owner = THIS_MODULE,
593 .dev_release = scsi_disk_release,
594 .dev_groups = sd_disk_groups,
597 static const struct dev_pm_ops sd_pm_ops = {
598 .suspend = sd_suspend_system,
599 .resume = sd_resume_system,
600 .poweroff = sd_suspend_system,
601 .restore = sd_resume_system,
602 .runtime_suspend = sd_suspend_runtime,
603 .runtime_resume = sd_resume_runtime,
606 static struct scsi_driver sd_template = {
609 .owner = THIS_MODULE,
611 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
613 .shutdown = sd_shutdown,
617 .init_command = sd_init_command,
618 .uninit_command = sd_uninit_command,
620 .eh_action = sd_eh_action,
621 .eh_reset = sd_eh_reset,
625 * Don't request a new module, as that could deadlock in multipath
628 static void sd_default_probe(dev_t devt)
633 * Device no to disk mapping:
635 * major disc2 disc p1
636 * |............|.............|....|....| <- dev_t
639 * Inside a major, we have 16k disks, however mapped non-
640 * contiguously. The first 16 disks are for major0, the next
641 * ones with major1, ... Disk 256 is for major0 again, disk 272
643 * As we stay compatible with our numbering scheme, we can reuse
644 * the well-know SCSI majors 8, 65--71, 136--143.
646 static int sd_major(int major_idx)
650 return SCSI_DISK0_MAJOR;
652 return SCSI_DISK1_MAJOR + major_idx - 1;
654 return SCSI_DISK8_MAJOR + major_idx - 8;
657 return 0; /* shut up gcc */
661 #ifdef CONFIG_BLK_SED_OPAL
662 static int sd_sec_submit(void *data, u16 spsp, u8 secp, void *buffer,
663 size_t len, bool send)
665 struct scsi_disk *sdkp = data;
666 struct scsi_device *sdev = sdkp->device;
670 cdb[0] = send ? SECURITY_PROTOCOL_OUT : SECURITY_PROTOCOL_IN;
672 put_unaligned_be16(spsp, &cdb[2]);
673 put_unaligned_be32(len, &cdb[6]);
675 ret = scsi_execute(sdev, cdb, send ? DMA_TO_DEVICE : DMA_FROM_DEVICE,
676 buffer, len, NULL, NULL, SD_TIMEOUT, sdkp->max_retries, 0,
678 return ret <= 0 ? ret : -EIO;
680 #endif /* CONFIG_BLK_SED_OPAL */
683 * Look up the DIX operation based on whether the command is read or
684 * write and whether dix and dif are enabled.
686 static unsigned int sd_prot_op(bool write, bool dix, bool dif)
688 /* Lookup table: bit 2 (write), bit 1 (dix), bit 0 (dif) */
689 static const unsigned int ops[] = { /* wrt dix dif */
690 SCSI_PROT_NORMAL, /* 0 0 0 */
691 SCSI_PROT_READ_STRIP, /* 0 0 1 */
692 SCSI_PROT_READ_INSERT, /* 0 1 0 */
693 SCSI_PROT_READ_PASS, /* 0 1 1 */
694 SCSI_PROT_NORMAL, /* 1 0 0 */
695 SCSI_PROT_WRITE_INSERT, /* 1 0 1 */
696 SCSI_PROT_WRITE_STRIP, /* 1 1 0 */
697 SCSI_PROT_WRITE_PASS, /* 1 1 1 */
700 return ops[write << 2 | dix << 1 | dif];
704 * Returns a mask of the protection flags that are valid for a given DIX
707 static unsigned int sd_prot_flag_mask(unsigned int prot_op)
709 static const unsigned int flag_mask[] = {
710 [SCSI_PROT_NORMAL] = 0,
712 [SCSI_PROT_READ_STRIP] = SCSI_PROT_TRANSFER_PI |
713 SCSI_PROT_GUARD_CHECK |
714 SCSI_PROT_REF_CHECK |
715 SCSI_PROT_REF_INCREMENT,
717 [SCSI_PROT_READ_INSERT] = SCSI_PROT_REF_INCREMENT |
718 SCSI_PROT_IP_CHECKSUM,
720 [SCSI_PROT_READ_PASS] = SCSI_PROT_TRANSFER_PI |
721 SCSI_PROT_GUARD_CHECK |
722 SCSI_PROT_REF_CHECK |
723 SCSI_PROT_REF_INCREMENT |
724 SCSI_PROT_IP_CHECKSUM,
726 [SCSI_PROT_WRITE_INSERT] = SCSI_PROT_TRANSFER_PI |
727 SCSI_PROT_REF_INCREMENT,
729 [SCSI_PROT_WRITE_STRIP] = SCSI_PROT_GUARD_CHECK |
730 SCSI_PROT_REF_CHECK |
731 SCSI_PROT_REF_INCREMENT |
732 SCSI_PROT_IP_CHECKSUM,
734 [SCSI_PROT_WRITE_PASS] = SCSI_PROT_TRANSFER_PI |
735 SCSI_PROT_GUARD_CHECK |
736 SCSI_PROT_REF_CHECK |
737 SCSI_PROT_REF_INCREMENT |
738 SCSI_PROT_IP_CHECKSUM,
741 return flag_mask[prot_op];
744 static unsigned char sd_setup_protect_cmnd(struct scsi_cmnd *scmd,
745 unsigned int dix, unsigned int dif)
747 struct request *rq = scsi_cmd_to_rq(scmd);
748 struct bio *bio = rq->bio;
749 unsigned int prot_op = sd_prot_op(rq_data_dir(rq), dix, dif);
750 unsigned int protect = 0;
752 if (dix) { /* DIX Type 0, 1, 2, 3 */
753 if (bio_integrity_flagged(bio, BIP_IP_CHECKSUM))
754 scmd->prot_flags |= SCSI_PROT_IP_CHECKSUM;
756 if (bio_integrity_flagged(bio, BIP_CTRL_NOCHECK) == false)
757 scmd->prot_flags |= SCSI_PROT_GUARD_CHECK;
760 if (dif != T10_PI_TYPE3_PROTECTION) { /* DIX/DIF Type 0, 1, 2 */
761 scmd->prot_flags |= SCSI_PROT_REF_INCREMENT;
763 if (bio_integrity_flagged(bio, BIP_CTRL_NOCHECK) == false)
764 scmd->prot_flags |= SCSI_PROT_REF_CHECK;
767 if (dif) { /* DIX/DIF Type 1, 2, 3 */
768 scmd->prot_flags |= SCSI_PROT_TRANSFER_PI;
770 if (bio_integrity_flagged(bio, BIP_DISK_NOCHECK))
771 protect = 3 << 5; /* Disable target PI checking */
773 protect = 1 << 5; /* Enable target PI checking */
776 scsi_set_prot_op(scmd, prot_op);
777 scsi_set_prot_type(scmd, dif);
778 scmd->prot_flags &= sd_prot_flag_mask(prot_op);
783 static void sd_config_discard(struct scsi_disk *sdkp, unsigned int mode)
785 struct request_queue *q = sdkp->disk->queue;
786 unsigned int logical_block_size = sdkp->device->sector_size;
787 unsigned int max_blocks = 0;
789 q->limits.discard_alignment =
790 sdkp->unmap_alignment * logical_block_size;
791 q->limits.discard_granularity =
792 max(sdkp->physical_block_size,
793 sdkp->unmap_granularity * logical_block_size);
794 sdkp->provisioning_mode = mode;
800 blk_queue_max_discard_sectors(q, 0);
801 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q);
805 max_blocks = min_not_zero(sdkp->max_unmap_blocks,
806 (u32)SD_MAX_WS16_BLOCKS);
810 if (sdkp->device->unmap_limit_for_ws)
811 max_blocks = sdkp->max_unmap_blocks;
813 max_blocks = sdkp->max_ws_blocks;
815 max_blocks = min_not_zero(max_blocks, (u32)SD_MAX_WS16_BLOCKS);
819 if (sdkp->device->unmap_limit_for_ws)
820 max_blocks = sdkp->max_unmap_blocks;
822 max_blocks = sdkp->max_ws_blocks;
824 max_blocks = min_not_zero(max_blocks, (u32)SD_MAX_WS10_BLOCKS);
828 max_blocks = min_not_zero(sdkp->max_ws_blocks,
829 (u32)SD_MAX_WS10_BLOCKS);
833 blk_queue_max_discard_sectors(q, max_blocks * (logical_block_size >> 9));
834 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
837 static blk_status_t sd_setup_unmap_cmnd(struct scsi_cmnd *cmd)
839 struct scsi_device *sdp = cmd->device;
840 struct request *rq = scsi_cmd_to_rq(cmd);
841 struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
842 u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
843 u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
844 unsigned int data_len = 24;
847 rq->special_vec.bv_page = mempool_alloc(sd_page_pool, GFP_ATOMIC);
848 if (!rq->special_vec.bv_page)
849 return BLK_STS_RESOURCE;
850 clear_highpage(rq->special_vec.bv_page);
851 rq->special_vec.bv_offset = 0;
852 rq->special_vec.bv_len = data_len;
853 rq->rq_flags |= RQF_SPECIAL_PAYLOAD;
856 cmd->cmnd[0] = UNMAP;
859 buf = bvec_virt(&rq->special_vec);
860 put_unaligned_be16(6 + 16, &buf[0]);
861 put_unaligned_be16(16, &buf[2]);
862 put_unaligned_be64(lba, &buf[8]);
863 put_unaligned_be32(nr_blocks, &buf[16]);
865 cmd->allowed = sdkp->max_retries;
866 cmd->transfersize = data_len;
867 rq->timeout = SD_TIMEOUT;
869 return scsi_alloc_sgtables(cmd);
872 static blk_status_t sd_setup_write_same16_cmnd(struct scsi_cmnd *cmd,
875 struct scsi_device *sdp = cmd->device;
876 struct request *rq = scsi_cmd_to_rq(cmd);
877 struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
878 u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
879 u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
880 u32 data_len = sdp->sector_size;
882 rq->special_vec.bv_page = mempool_alloc(sd_page_pool, GFP_ATOMIC);
883 if (!rq->special_vec.bv_page)
884 return BLK_STS_RESOURCE;
885 clear_highpage(rq->special_vec.bv_page);
886 rq->special_vec.bv_offset = 0;
887 rq->special_vec.bv_len = data_len;
888 rq->rq_flags |= RQF_SPECIAL_PAYLOAD;
891 cmd->cmnd[0] = WRITE_SAME_16;
893 cmd->cmnd[1] = 0x8; /* UNMAP */
894 put_unaligned_be64(lba, &cmd->cmnd[2]);
895 put_unaligned_be32(nr_blocks, &cmd->cmnd[10]);
897 cmd->allowed = sdkp->max_retries;
898 cmd->transfersize = data_len;
899 rq->timeout = unmap ? SD_TIMEOUT : SD_WRITE_SAME_TIMEOUT;
901 return scsi_alloc_sgtables(cmd);
904 static blk_status_t sd_setup_write_same10_cmnd(struct scsi_cmnd *cmd,
907 struct scsi_device *sdp = cmd->device;
908 struct request *rq = scsi_cmd_to_rq(cmd);
909 struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
910 u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
911 u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
912 u32 data_len = sdp->sector_size;
914 rq->special_vec.bv_page = mempool_alloc(sd_page_pool, GFP_ATOMIC);
915 if (!rq->special_vec.bv_page)
916 return BLK_STS_RESOURCE;
917 clear_highpage(rq->special_vec.bv_page);
918 rq->special_vec.bv_offset = 0;
919 rq->special_vec.bv_len = data_len;
920 rq->rq_flags |= RQF_SPECIAL_PAYLOAD;
923 cmd->cmnd[0] = WRITE_SAME;
925 cmd->cmnd[1] = 0x8; /* UNMAP */
926 put_unaligned_be32(lba, &cmd->cmnd[2]);
927 put_unaligned_be16(nr_blocks, &cmd->cmnd[7]);
929 cmd->allowed = sdkp->max_retries;
930 cmd->transfersize = data_len;
931 rq->timeout = unmap ? SD_TIMEOUT : SD_WRITE_SAME_TIMEOUT;
933 return scsi_alloc_sgtables(cmd);
936 static blk_status_t sd_setup_write_zeroes_cmnd(struct scsi_cmnd *cmd)
938 struct request *rq = scsi_cmd_to_rq(cmd);
939 struct scsi_device *sdp = cmd->device;
940 struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
941 u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
942 u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
944 if (!(rq->cmd_flags & REQ_NOUNMAP)) {
945 switch (sdkp->zeroing_mode) {
946 case SD_ZERO_WS16_UNMAP:
947 return sd_setup_write_same16_cmnd(cmd, true);
948 case SD_ZERO_WS10_UNMAP:
949 return sd_setup_write_same10_cmnd(cmd, true);
953 if (sdp->no_write_same) {
954 rq->rq_flags |= RQF_QUIET;
955 return BLK_STS_TARGET;
958 if (sdkp->ws16 || lba > 0xffffffff || nr_blocks > 0xffff)
959 return sd_setup_write_same16_cmnd(cmd, false);
961 return sd_setup_write_same10_cmnd(cmd, false);
964 static void sd_config_write_same(struct scsi_disk *sdkp)
966 struct request_queue *q = sdkp->disk->queue;
967 unsigned int logical_block_size = sdkp->device->sector_size;
969 if (sdkp->device->no_write_same) {
970 sdkp->max_ws_blocks = 0;
974 /* Some devices can not handle block counts above 0xffff despite
975 * supporting WRITE SAME(16). Consequently we default to 64k
976 * blocks per I/O unless the device explicitly advertises a
979 if (sdkp->max_ws_blocks > SD_MAX_WS10_BLOCKS)
980 sdkp->max_ws_blocks = min_not_zero(sdkp->max_ws_blocks,
981 (u32)SD_MAX_WS16_BLOCKS);
982 else if (sdkp->ws16 || sdkp->ws10 || sdkp->device->no_report_opcodes)
983 sdkp->max_ws_blocks = min_not_zero(sdkp->max_ws_blocks,
984 (u32)SD_MAX_WS10_BLOCKS);
986 sdkp->device->no_write_same = 1;
987 sdkp->max_ws_blocks = 0;
990 if (sdkp->lbprz && sdkp->lbpws)
991 sdkp->zeroing_mode = SD_ZERO_WS16_UNMAP;
992 else if (sdkp->lbprz && sdkp->lbpws10)
993 sdkp->zeroing_mode = SD_ZERO_WS10_UNMAP;
994 else if (sdkp->max_ws_blocks)
995 sdkp->zeroing_mode = SD_ZERO_WS;
997 sdkp->zeroing_mode = SD_ZERO_WRITE;
999 if (sdkp->max_ws_blocks &&
1000 sdkp->physical_block_size > logical_block_size) {
1002 * Reporting a maximum number of blocks that is not aligned
1003 * on the device physical size would cause a large write same
1004 * request to be split into physically unaligned chunks by
1005 * __blkdev_issue_write_zeroes() and __blkdev_issue_write_same()
1006 * even if the caller of these functions took care to align the
1007 * large request. So make sure the maximum reported is aligned
1008 * to the device physical block size. This is only an optional
1009 * optimization for regular disks, but this is mandatory to
1010 * avoid failure of large write same requests directed at
1011 * sequential write required zones of host-managed ZBC disks.
1013 sdkp->max_ws_blocks =
1014 round_down(sdkp->max_ws_blocks,
1015 bytes_to_logical(sdkp->device,
1016 sdkp->physical_block_size));
1020 blk_queue_max_write_same_sectors(q, sdkp->max_ws_blocks *
1021 (logical_block_size >> 9));
1022 blk_queue_max_write_zeroes_sectors(q, sdkp->max_ws_blocks *
1023 (logical_block_size >> 9));
1027 * sd_setup_write_same_cmnd - write the same data to multiple blocks
1028 * @cmd: command to prepare
1030 * Will set up either WRITE SAME(10) or WRITE SAME(16) depending on
1031 * the preference indicated by the target device.
1033 static blk_status_t sd_setup_write_same_cmnd(struct scsi_cmnd *cmd)
1035 struct request *rq = scsi_cmd_to_rq(cmd);
1036 struct scsi_device *sdp = cmd->device;
1037 struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
1038 struct bio *bio = rq->bio;
1039 u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
1040 u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
1043 if (sdkp->device->no_write_same)
1044 return BLK_STS_TARGET;
1046 BUG_ON(bio_offset(bio) || bio_iovec(bio).bv_len != sdp->sector_size);
1048 rq->timeout = SD_WRITE_SAME_TIMEOUT;
1050 if (sdkp->ws16 || lba > 0xffffffff || nr_blocks > 0xffff) {
1052 cmd->cmnd[0] = WRITE_SAME_16;
1053 put_unaligned_be64(lba, &cmd->cmnd[2]);
1054 put_unaligned_be32(nr_blocks, &cmd->cmnd[10]);
1057 cmd->cmnd[0] = WRITE_SAME;
1058 put_unaligned_be32(lba, &cmd->cmnd[2]);
1059 put_unaligned_be16(nr_blocks, &cmd->cmnd[7]);
1062 cmd->transfersize = sdp->sector_size;
1063 cmd->allowed = sdkp->max_retries;
1066 * For WRITE SAME the data transferred via the DATA OUT buffer is
1067 * different from the amount of data actually written to the target.
1069 * We set up __data_len to the amount of data transferred via the
1070 * DATA OUT buffer so that blk_rq_map_sg sets up the proper S/G list
1071 * to transfer a single sector of data first, but then reset it to
1072 * the amount of data to be written right after so that the I/O path
1073 * knows how much to actually write.
1075 rq->__data_len = sdp->sector_size;
1076 ret = scsi_alloc_sgtables(cmd);
1077 rq->__data_len = blk_rq_bytes(rq);
1082 static blk_status_t sd_setup_flush_cmnd(struct scsi_cmnd *cmd)
1084 struct request *rq = scsi_cmd_to_rq(cmd);
1085 struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
1087 /* flush requests don't perform I/O, zero the S/G table */
1088 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
1090 cmd->cmnd[0] = SYNCHRONIZE_CACHE;
1092 cmd->transfersize = 0;
1093 cmd->allowed = sdkp->max_retries;
1095 rq->timeout = rq->q->rq_timeout * SD_FLUSH_TIMEOUT_MULTIPLIER;
1099 static blk_status_t sd_setup_rw32_cmnd(struct scsi_cmnd *cmd, bool write,
1100 sector_t lba, unsigned int nr_blocks,
1101 unsigned char flags)
1103 cmd->cmnd = mempool_alloc(sd_cdb_pool, GFP_ATOMIC);
1104 if (unlikely(cmd->cmnd == NULL))
1105 return BLK_STS_RESOURCE;
1107 cmd->cmd_len = SD_EXT_CDB_SIZE;
1108 memset(cmd->cmnd, 0, cmd->cmd_len);
1110 cmd->cmnd[0] = VARIABLE_LENGTH_CMD;
1111 cmd->cmnd[7] = 0x18; /* Additional CDB len */
1112 cmd->cmnd[9] = write ? WRITE_32 : READ_32;
1113 cmd->cmnd[10] = flags;
1114 put_unaligned_be64(lba, &cmd->cmnd[12]);
1115 put_unaligned_be32(lba, &cmd->cmnd[20]); /* Expected Indirect LBA */
1116 put_unaligned_be32(nr_blocks, &cmd->cmnd[28]);
1121 static blk_status_t sd_setup_rw16_cmnd(struct scsi_cmnd *cmd, bool write,
1122 sector_t lba, unsigned int nr_blocks,
1123 unsigned char flags)
1126 cmd->cmnd[0] = write ? WRITE_16 : READ_16;
1127 cmd->cmnd[1] = flags;
1130 put_unaligned_be64(lba, &cmd->cmnd[2]);
1131 put_unaligned_be32(nr_blocks, &cmd->cmnd[10]);
1136 static blk_status_t sd_setup_rw10_cmnd(struct scsi_cmnd *cmd, bool write,
1137 sector_t lba, unsigned int nr_blocks,
1138 unsigned char flags)
1141 cmd->cmnd[0] = write ? WRITE_10 : READ_10;
1142 cmd->cmnd[1] = flags;
1145 put_unaligned_be32(lba, &cmd->cmnd[2]);
1146 put_unaligned_be16(nr_blocks, &cmd->cmnd[7]);
1151 static blk_status_t sd_setup_rw6_cmnd(struct scsi_cmnd *cmd, bool write,
1152 sector_t lba, unsigned int nr_blocks,
1153 unsigned char flags)
1155 /* Avoid that 0 blocks gets translated into 256 blocks. */
1156 if (WARN_ON_ONCE(nr_blocks == 0))
1157 return BLK_STS_IOERR;
1159 if (unlikely(flags & 0x8)) {
1161 * This happens only if this drive failed 10byte rw
1162 * command with ILLEGAL_REQUEST during operation and
1163 * thus turned off use_10_for_rw.
1165 scmd_printk(KERN_ERR, cmd, "FUA write on READ/WRITE(6) drive\n");
1166 return BLK_STS_IOERR;
1170 cmd->cmnd[0] = write ? WRITE_6 : READ_6;
1171 cmd->cmnd[1] = (lba >> 16) & 0x1f;
1172 cmd->cmnd[2] = (lba >> 8) & 0xff;
1173 cmd->cmnd[3] = lba & 0xff;
1174 cmd->cmnd[4] = nr_blocks;
1180 static blk_status_t sd_setup_read_write_cmnd(struct scsi_cmnd *cmd)
1182 struct request *rq = scsi_cmd_to_rq(cmd);
1183 struct scsi_device *sdp = cmd->device;
1184 struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
1185 sector_t lba = sectors_to_logical(sdp, blk_rq_pos(rq));
1187 unsigned int nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
1188 unsigned int mask = logical_to_sectors(sdp, 1) - 1;
1189 bool write = rq_data_dir(rq) == WRITE;
1190 unsigned char protect, fua;
1195 ret = scsi_alloc_sgtables(cmd);
1196 if (ret != BLK_STS_OK)
1199 ret = BLK_STS_IOERR;
1200 if (!scsi_device_online(sdp) || sdp->changed) {
1201 scmd_printk(KERN_ERR, cmd, "device offline or changed\n");
1205 if (blk_rq_pos(rq) + blk_rq_sectors(rq) > get_capacity(rq->q->disk)) {
1206 scmd_printk(KERN_ERR, cmd, "access beyond end of device\n");
1210 if ((blk_rq_pos(rq) & mask) || (blk_rq_sectors(rq) & mask)) {
1211 scmd_printk(KERN_ERR, cmd, "request not aligned to the logical block size\n");
1216 * Some SD card readers can't handle accesses which touch the
1217 * last one or two logical blocks. Split accesses as needed.
1219 threshold = sdkp->capacity - SD_LAST_BUGGY_SECTORS;
1221 if (unlikely(sdp->last_sector_bug && lba + nr_blocks > threshold)) {
1222 if (lba < threshold) {
1223 /* Access up to the threshold but not beyond */
1224 nr_blocks = threshold - lba;
1226 /* Access only a single logical block */
1231 if (req_op(rq) == REQ_OP_ZONE_APPEND) {
1232 ret = sd_zbc_prepare_zone_append(cmd, &lba, nr_blocks);
1237 fua = rq->cmd_flags & REQ_FUA ? 0x8 : 0;
1238 dix = scsi_prot_sg_count(cmd);
1239 dif = scsi_host_dif_capable(cmd->device->host, sdkp->protection_type);
1242 protect = sd_setup_protect_cmnd(cmd, dix, dif);
1246 if (protect && sdkp->protection_type == T10_PI_TYPE2_PROTECTION) {
1247 ret = sd_setup_rw32_cmnd(cmd, write, lba, nr_blocks,
1249 } else if (sdp->use_16_for_rw || (nr_blocks > 0xffff)) {
1250 ret = sd_setup_rw16_cmnd(cmd, write, lba, nr_blocks,
1252 } else if ((nr_blocks > 0xff) || (lba > 0x1fffff) ||
1253 sdp->use_10_for_rw || protect) {
1254 ret = sd_setup_rw10_cmnd(cmd, write, lba, nr_blocks,
1257 ret = sd_setup_rw6_cmnd(cmd, write, lba, nr_blocks,
1261 if (unlikely(ret != BLK_STS_OK))
1265 * We shouldn't disconnect in the middle of a sector, so with a dumb
1266 * host adapter, it's safe to assume that we can at least transfer
1267 * this many bytes between each connect / disconnect.
1269 cmd->transfersize = sdp->sector_size;
1270 cmd->underflow = nr_blocks << 9;
1271 cmd->allowed = sdkp->max_retries;
1272 cmd->sdb.length = nr_blocks * sdp->sector_size;
1275 scmd_printk(KERN_INFO, cmd,
1276 "%s: block=%llu, count=%d\n", __func__,
1277 (unsigned long long)blk_rq_pos(rq),
1278 blk_rq_sectors(rq)));
1280 scmd_printk(KERN_INFO, cmd,
1281 "%s %d/%u 512 byte blocks.\n",
1282 write ? "writing" : "reading", nr_blocks,
1283 blk_rq_sectors(rq)));
1286 * This indicates that the command is ready from our end to be queued.
1290 scsi_free_sgtables(cmd);
1294 static blk_status_t sd_init_command(struct scsi_cmnd *cmd)
1296 struct request *rq = scsi_cmd_to_rq(cmd);
1298 switch (req_op(rq)) {
1299 case REQ_OP_DISCARD:
1300 switch (scsi_disk(rq->q->disk)->provisioning_mode) {
1302 return sd_setup_unmap_cmnd(cmd);
1304 return sd_setup_write_same16_cmnd(cmd, true);
1306 return sd_setup_write_same10_cmnd(cmd, true);
1308 return sd_setup_write_same10_cmnd(cmd, false);
1310 return BLK_STS_TARGET;
1312 case REQ_OP_WRITE_ZEROES:
1313 return sd_setup_write_zeroes_cmnd(cmd);
1314 case REQ_OP_WRITE_SAME:
1315 return sd_setup_write_same_cmnd(cmd);
1317 return sd_setup_flush_cmnd(cmd);
1320 case REQ_OP_ZONE_APPEND:
1321 return sd_setup_read_write_cmnd(cmd);
1322 case REQ_OP_ZONE_RESET:
1323 return sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_RESET_WRITE_POINTER,
1325 case REQ_OP_ZONE_RESET_ALL:
1326 return sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_RESET_WRITE_POINTER,
1328 case REQ_OP_ZONE_OPEN:
1329 return sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_OPEN_ZONE, false);
1330 case REQ_OP_ZONE_CLOSE:
1331 return sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_CLOSE_ZONE, false);
1332 case REQ_OP_ZONE_FINISH:
1333 return sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_FINISH_ZONE, false);
1336 return BLK_STS_NOTSUPP;
1340 static void sd_uninit_command(struct scsi_cmnd *SCpnt)
1342 struct request *rq = scsi_cmd_to_rq(SCpnt);
1345 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1346 mempool_free(rq->special_vec.bv_page, sd_page_pool);
1348 if (SCpnt->cmnd != scsi_req(rq)->cmd) {
1352 mempool_free(cmnd, sd_cdb_pool);
1356 static bool sd_need_revalidate(struct block_device *bdev,
1357 struct scsi_disk *sdkp)
1359 if (sdkp->device->removable || sdkp->write_prot) {
1360 if (bdev_check_media_change(bdev))
1365 * Force a full rescan after ioctl(BLKRRPART). While the disk state has
1366 * nothing to do with partitions, BLKRRPART is used to force a full
1367 * revalidate after things like a format for historical reasons.
1369 return test_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
1373 * sd_open - open a scsi disk device
1374 * @bdev: Block device of the scsi disk to open
1375 * @mode: FMODE_* mask
1377 * Returns 0 if successful. Returns a negated errno value in case
1380 * Note: This can be called from a user context (e.g. fsck(1) )
1381 * or from within the kernel (e.g. as a result of a mount(1) ).
1382 * In the latter case @inode and @filp carry an abridged amount
1383 * of information as noted above.
1385 * Locking: called with bdev->bd_disk->open_mutex held.
1387 static int sd_open(struct block_device *bdev, fmode_t mode)
1389 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
1390 struct scsi_device *sdev = sdkp->device;
1393 if (scsi_device_get(sdev))
1396 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
1399 * If the device is in error recovery, wait until it is done.
1400 * If the device is offline, then disallow any access to it.
1403 if (!scsi_block_when_processing_errors(sdev))
1406 if (sd_need_revalidate(bdev, sdkp))
1407 sd_revalidate_disk(bdev->bd_disk);
1410 * If the drive is empty, just let the open fail.
1412 retval = -ENOMEDIUM;
1413 if (sdev->removable && !sdkp->media_present && !(mode & FMODE_NDELAY))
1417 * If the device has the write protect tab set, have the open fail
1418 * if the user expects to be able to write to the thing.
1421 if (sdkp->write_prot && (mode & FMODE_WRITE))
1425 * It is possible that the disk changing stuff resulted in
1426 * the device being taken offline. If this is the case,
1427 * report this to the user, and don't pretend that the
1428 * open actually succeeded.
1431 if (!scsi_device_online(sdev))
1434 if ((atomic_inc_return(&sdkp->openers) == 1) && sdev->removable) {
1435 if (scsi_block_when_processing_errors(sdev))
1436 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
1442 scsi_device_put(sdev);
1447 * sd_release - invoked when the (last) close(2) is called on this
1449 * @disk: disk to release
1450 * @mode: FMODE_* mask
1454 * Note: may block (uninterruptible) if error recovery is underway
1457 * Locking: called with bdev->bd_disk->open_mutex held.
1459 static void sd_release(struct gendisk *disk, fmode_t mode)
1461 struct scsi_disk *sdkp = scsi_disk(disk);
1462 struct scsi_device *sdev = sdkp->device;
1464 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
1466 if (atomic_dec_return(&sdkp->openers) == 0 && sdev->removable) {
1467 if (scsi_block_when_processing_errors(sdev))
1468 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
1471 scsi_device_put(sdev);
1474 static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1476 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
1477 struct scsi_device *sdp = sdkp->device;
1478 struct Scsi_Host *host = sdp->host;
1479 sector_t capacity = logical_to_sectors(sdp, sdkp->capacity);
1482 /* default to most commonly used values */
1483 diskinfo[0] = 0x40; /* 1 << 6 */
1484 diskinfo[1] = 0x20; /* 1 << 5 */
1485 diskinfo[2] = capacity >> 11;
1487 /* override with calculated, extended default, or driver values */
1488 if (host->hostt->bios_param)
1489 host->hostt->bios_param(sdp, bdev, capacity, diskinfo);
1491 scsicam_bios_param(bdev, capacity, diskinfo);
1493 geo->heads = diskinfo[0];
1494 geo->sectors = diskinfo[1];
1495 geo->cylinders = diskinfo[2];
1500 * sd_ioctl - process an ioctl
1501 * @bdev: target block device
1502 * @mode: FMODE_* mask
1503 * @cmd: ioctl command number
1504 * @arg: this is third argument given to ioctl(2) system call.
1505 * Often contains a pointer.
1507 * Returns 0 if successful (some ioctls return positive numbers on
1508 * success as well). Returns a negated errno value in case of error.
1510 * Note: most ioctls are forward onto the block subsystem or further
1511 * down in the scsi subsystem.
1513 static int sd_ioctl(struct block_device *bdev, fmode_t mode,
1514 unsigned int cmd, unsigned long arg)
1516 struct gendisk *disk = bdev->bd_disk;
1517 struct scsi_disk *sdkp = scsi_disk(disk);
1518 struct scsi_device *sdp = sdkp->device;
1519 void __user *p = (void __user *)arg;
1522 SCSI_LOG_IOCTL(1, sd_printk(KERN_INFO, sdkp, "sd_ioctl: disk=%s, "
1523 "cmd=0x%x\n", disk->disk_name, cmd));
1525 if (bdev_is_partition(bdev) && !capable(CAP_SYS_RAWIO))
1526 return -ENOIOCTLCMD;
1529 * If we are in the middle of error recovery, don't let anyone
1530 * else try and use this device. Also, if error recovery fails, it
1531 * may try and take the device offline, in which case all further
1532 * access to the device is prohibited.
1534 error = scsi_ioctl_block_when_processing_errors(sdp, cmd,
1535 (mode & FMODE_NDELAY) != 0);
1539 if (is_sed_ioctl(cmd))
1540 return sed_ioctl(sdkp->opal_dev, cmd, p);
1541 return scsi_ioctl(sdp, mode, cmd, p);
1544 static void set_media_not_present(struct scsi_disk *sdkp)
1546 if (sdkp->media_present)
1547 sdkp->device->changed = 1;
1549 if (sdkp->device->removable) {
1550 sdkp->media_present = 0;
1555 static int media_not_present(struct scsi_disk *sdkp,
1556 struct scsi_sense_hdr *sshdr)
1558 if (!scsi_sense_valid(sshdr))
1561 /* not invoked for commands that could return deferred errors */
1562 switch (sshdr->sense_key) {
1563 case UNIT_ATTENTION:
1565 /* medium not present */
1566 if (sshdr->asc == 0x3A) {
1567 set_media_not_present(sdkp);
1575 * sd_check_events - check media events
1576 * @disk: kernel device descriptor
1577 * @clearing: disk events currently being cleared
1579 * Returns mask of DISK_EVENT_*.
1581 * Note: this function is invoked from the block subsystem.
1583 static unsigned int sd_check_events(struct gendisk *disk, unsigned int clearing)
1585 struct scsi_disk *sdkp = disk->private_data;
1586 struct scsi_device *sdp;
1594 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_check_events\n"));
1597 * If the device is offline, don't send any commands - just pretend as
1598 * if the command failed. If the device ever comes back online, we
1599 * can deal with it then. It is only because of unrecoverable errors
1600 * that we would ever take a device offline in the first place.
1602 if (!scsi_device_online(sdp)) {
1603 set_media_not_present(sdkp);
1608 * Using TEST_UNIT_READY enables differentiation between drive with
1609 * no cartridge loaded - NOT READY, drive with changed cartridge -
1610 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
1612 * Drives that auto spin down. eg iomega jaz 1G, will be started
1613 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
1614 * sd_revalidate() is called.
1616 if (scsi_block_when_processing_errors(sdp)) {
1617 struct scsi_sense_hdr sshdr = { 0, };
1619 retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, sdkp->max_retries,
1622 /* failed to execute TUR, assume media not present */
1623 if (retval < 0 || host_byte(retval)) {
1624 set_media_not_present(sdkp);
1628 if (media_not_present(sdkp, &sshdr))
1633 * For removable scsi disk we have to recognise the presence
1634 * of a disk in the drive.
1636 if (!sdkp->media_present)
1638 sdkp->media_present = 1;
1641 * sdp->changed is set under the following conditions:
1643 * Medium present state has changed in either direction.
1644 * Device has indicated UNIT_ATTENTION.
1646 disk_changed = sdp->changed;
1648 return disk_changed ? DISK_EVENT_MEDIA_CHANGE : 0;
1651 static int sd_sync_cache(struct scsi_disk *sdkp, struct scsi_sense_hdr *sshdr)
1654 struct scsi_device *sdp = sdkp->device;
1655 const int timeout = sdp->request_queue->rq_timeout
1656 * SD_FLUSH_TIMEOUT_MULTIPLIER;
1657 struct scsi_sense_hdr my_sshdr;
1659 if (!scsi_device_online(sdp))
1662 /* caller might not be interested in sense, but we need it */
1666 for (retries = 3; retries > 0; --retries) {
1667 unsigned char cmd[10] = { 0 };
1669 cmd[0] = SYNCHRONIZE_CACHE;
1671 * Leave the rest of the command zero to indicate
1674 res = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, sshdr,
1675 timeout, sdkp->max_retries, 0, RQF_PM, NULL);
1681 sd_print_result(sdkp, "Synchronize Cache(10) failed", res);
1686 if (scsi_status_is_check_condition(res) &&
1687 scsi_sense_valid(sshdr)) {
1688 sd_print_sense_hdr(sdkp, sshdr);
1690 /* we need to evaluate the error return */
1691 if (sshdr->asc == 0x3a || /* medium not present */
1692 sshdr->asc == 0x20 || /* invalid command */
1693 (sshdr->asc == 0x74 && sshdr->ascq == 0x71)) /* drive is password locked */
1694 /* this is no error here */
1698 switch (host_byte(res)) {
1699 /* ignore errors due to racing a disconnection */
1700 case DID_BAD_TARGET:
1701 case DID_NO_CONNECT:
1703 /* signal the upper layer it might try again */
1707 case DID_SOFT_ERROR:
1716 static void sd_rescan(struct device *dev)
1718 struct scsi_disk *sdkp = dev_get_drvdata(dev);
1720 sd_revalidate_disk(sdkp->disk);
1723 static int sd_get_unique_id(struct gendisk *disk, u8 id[16],
1724 enum blk_unique_id type)
1726 struct scsi_device *sdev = scsi_disk(disk)->device;
1727 const struct scsi_vpd *vpd;
1728 const unsigned char *d;
1729 int ret = -ENXIO, len;
1732 vpd = rcu_dereference(sdev->vpd_pg83);
1737 for (d = vpd->data + 4; d < vpd->data + vpd->len; d += d[3] + 4) {
1738 /* we only care about designators with LU association */
1739 if (((d[1] >> 4) & 0x3) != 0x00)
1741 if ((d[1] & 0xf) != type)
1745 * Only exit early if a 16-byte descriptor was found. Otherwise
1746 * keep looking as one with more entropy might still show up.
1749 if (len != 8 && len != 12 && len != 16)
1752 memcpy(id, d + 4, len);
1761 static char sd_pr_type(enum pr_type type)
1764 case PR_WRITE_EXCLUSIVE:
1766 case PR_EXCLUSIVE_ACCESS:
1768 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1770 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1772 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1774 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1781 static int sd_pr_command(struct block_device *bdev, u8 sa,
1782 u64 key, u64 sa_key, u8 type, u8 flags)
1784 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
1785 struct scsi_device *sdev = sdkp->device;
1786 struct scsi_sense_hdr sshdr;
1788 u8 cmd[16] = { 0, };
1789 u8 data[24] = { 0, };
1791 cmd[0] = PERSISTENT_RESERVE_OUT;
1794 put_unaligned_be32(sizeof(data), &cmd[5]);
1796 put_unaligned_be64(key, &data[0]);
1797 put_unaligned_be64(sa_key, &data[8]);
1800 result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, &data, sizeof(data),
1801 &sshdr, SD_TIMEOUT, sdkp->max_retries, NULL);
1803 if (scsi_status_is_check_condition(result) &&
1804 scsi_sense_valid(&sshdr)) {
1805 sdev_printk(KERN_INFO, sdev, "PR command failed: %d\n", result);
1806 scsi_print_sense_hdr(sdev, NULL, &sshdr);
1812 static int sd_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
1815 if (flags & ~PR_FL_IGNORE_KEY)
1817 return sd_pr_command(bdev, (flags & PR_FL_IGNORE_KEY) ? 0x06 : 0x00,
1818 old_key, new_key, 0,
1819 (1 << 0) /* APTPL */);
1822 static int sd_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
1827 return sd_pr_command(bdev, 0x01, key, 0, sd_pr_type(type), 0);
1830 static int sd_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1832 return sd_pr_command(bdev, 0x02, key, 0, sd_pr_type(type), 0);
1835 static int sd_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
1836 enum pr_type type, bool abort)
1838 return sd_pr_command(bdev, abort ? 0x05 : 0x04, old_key, new_key,
1839 sd_pr_type(type), 0);
1842 static int sd_pr_clear(struct block_device *bdev, u64 key)
1844 return sd_pr_command(bdev, 0x03, key, 0, 0, 0);
1847 static const struct pr_ops sd_pr_ops = {
1848 .pr_register = sd_pr_register,
1849 .pr_reserve = sd_pr_reserve,
1850 .pr_release = sd_pr_release,
1851 .pr_preempt = sd_pr_preempt,
1852 .pr_clear = sd_pr_clear,
1855 static void scsi_disk_free_disk(struct gendisk *disk)
1857 struct scsi_disk *sdkp = scsi_disk(disk);
1859 put_device(&sdkp->disk_dev);
1862 static const struct block_device_operations sd_fops = {
1863 .owner = THIS_MODULE,
1865 .release = sd_release,
1867 .getgeo = sd_getgeo,
1868 .compat_ioctl = blkdev_compat_ptr_ioctl,
1869 .check_events = sd_check_events,
1870 .unlock_native_capacity = sd_unlock_native_capacity,
1871 .report_zones = sd_zbc_report_zones,
1872 .get_unique_id = sd_get_unique_id,
1873 .free_disk = scsi_disk_free_disk,
1874 .pr_ops = &sd_pr_ops,
1878 * sd_eh_reset - reset error handling callback
1879 * @scmd: sd-issued command that has failed
1881 * This function is called by the SCSI midlayer before starting
1882 * SCSI EH. When counting medium access failures we have to be
1883 * careful to register it only only once per device and SCSI EH run;
1884 * there might be several timed out commands which will cause the
1885 * 'max_medium_access_timeouts' counter to trigger after the first
1886 * SCSI EH run already and set the device to offline.
1887 * So this function resets the internal counter before starting SCSI EH.
1889 static void sd_eh_reset(struct scsi_cmnd *scmd)
1891 struct scsi_disk *sdkp = scsi_disk(scsi_cmd_to_rq(scmd)->q->disk);
1893 /* New SCSI EH run, reset gate variable */
1894 sdkp->ignore_medium_access_errors = false;
1898 * sd_eh_action - error handling callback
1899 * @scmd: sd-issued command that has failed
1900 * @eh_disp: The recovery disposition suggested by the midlayer
1902 * This function is called by the SCSI midlayer upon completion of an
1903 * error test command (currently TEST UNIT READY). The result of sending
1904 * the eh command is passed in eh_disp. We're looking for devices that
1905 * fail medium access commands but are OK with non access commands like
1906 * test unit ready (so wrongly see the device as having a successful
1909 static int sd_eh_action(struct scsi_cmnd *scmd, int eh_disp)
1911 struct scsi_disk *sdkp = scsi_disk(scsi_cmd_to_rq(scmd)->q->disk);
1912 struct scsi_device *sdev = scmd->device;
1914 if (!scsi_device_online(sdev) ||
1915 !scsi_medium_access_command(scmd) ||
1916 host_byte(scmd->result) != DID_TIME_OUT ||
1921 * The device has timed out executing a medium access command.
1922 * However, the TEST UNIT READY command sent during error
1923 * handling completed successfully. Either the device is in the
1924 * process of recovering or has it suffered an internal failure
1925 * that prevents access to the storage medium.
1927 if (!sdkp->ignore_medium_access_errors) {
1928 sdkp->medium_access_timed_out++;
1929 sdkp->ignore_medium_access_errors = true;
1933 * If the device keeps failing read/write commands but TEST UNIT
1934 * READY always completes successfully we assume that medium
1935 * access is no longer possible and take the device offline.
1937 if (sdkp->medium_access_timed_out >= sdkp->max_medium_access_timeouts) {
1938 scmd_printk(KERN_ERR, scmd,
1939 "Medium access timeout failure. Offlining disk!\n");
1940 mutex_lock(&sdev->state_mutex);
1941 scsi_device_set_state(sdev, SDEV_OFFLINE);
1942 mutex_unlock(&sdev->state_mutex);
1950 static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
1952 struct request *req = scsi_cmd_to_rq(scmd);
1953 struct scsi_device *sdev = scmd->device;
1954 unsigned int transferred, good_bytes;
1955 u64 start_lba, end_lba, bad_lba;
1958 * Some commands have a payload smaller than the device logical
1959 * block size (e.g. INQUIRY on a 4K disk).
1961 if (scsi_bufflen(scmd) <= sdev->sector_size)
1964 /* Check if we have a 'bad_lba' information */
1965 if (!scsi_get_sense_info_fld(scmd->sense_buffer,
1966 SCSI_SENSE_BUFFERSIZE,
1971 * If the bad lba was reported incorrectly, we have no idea where
1974 start_lba = sectors_to_logical(sdev, blk_rq_pos(req));
1975 end_lba = start_lba + bytes_to_logical(sdev, scsi_bufflen(scmd));
1976 if (bad_lba < start_lba || bad_lba >= end_lba)
1980 * resid is optional but mostly filled in. When it's unused,
1981 * its value is zero, so we assume the whole buffer transferred
1983 transferred = scsi_bufflen(scmd) - scsi_get_resid(scmd);
1985 /* This computation should always be done in terms of the
1986 * resolution of the device's medium.
1988 good_bytes = logical_to_bytes(sdev, bad_lba - start_lba);
1990 return min(good_bytes, transferred);
1994 * sd_done - bottom half handler: called when the lower level
1995 * driver has completed (successfully or otherwise) a scsi command.
1996 * @SCpnt: mid-level's per command structure.
1998 * Note: potentially run from within an ISR. Must not block.
2000 static int sd_done(struct scsi_cmnd *SCpnt)
2002 int result = SCpnt->result;
2003 unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt);
2004 unsigned int sector_size = SCpnt->device->sector_size;
2006 struct scsi_sense_hdr sshdr;
2007 struct request *req = scsi_cmd_to_rq(SCpnt);
2008 struct scsi_disk *sdkp = scsi_disk(req->q->disk);
2009 int sense_valid = 0;
2010 int sense_deferred = 0;
2012 switch (req_op(req)) {
2013 case REQ_OP_DISCARD:
2014 case REQ_OP_WRITE_ZEROES:
2015 case REQ_OP_WRITE_SAME:
2016 case REQ_OP_ZONE_RESET:
2017 case REQ_OP_ZONE_RESET_ALL:
2018 case REQ_OP_ZONE_OPEN:
2019 case REQ_OP_ZONE_CLOSE:
2020 case REQ_OP_ZONE_FINISH:
2022 good_bytes = blk_rq_bytes(req);
2023 scsi_set_resid(SCpnt, 0);
2026 scsi_set_resid(SCpnt, blk_rq_bytes(req));
2031 * In case of bogus fw or device, we could end up having
2032 * an unaligned partial completion. Check this here and force
2035 resid = scsi_get_resid(SCpnt);
2036 if (resid & (sector_size - 1)) {
2037 sd_printk(KERN_INFO, sdkp,
2038 "Unaligned partial completion (resid=%u, sector_sz=%u)\n",
2039 resid, sector_size);
2040 scsi_print_command(SCpnt);
2041 resid = min(scsi_bufflen(SCpnt),
2042 round_up(resid, sector_size));
2043 scsi_set_resid(SCpnt, resid);
2048 sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
2050 sense_deferred = scsi_sense_is_deferred(&sshdr);
2052 sdkp->medium_access_timed_out = 0;
2054 if (!scsi_status_is_check_condition(result) &&
2055 (!sense_valid || sense_deferred))
2058 switch (sshdr.sense_key) {
2059 case HARDWARE_ERROR:
2061 good_bytes = sd_completed_bytes(SCpnt);
2063 case RECOVERED_ERROR:
2064 good_bytes = scsi_bufflen(SCpnt);
2067 /* This indicates a false check condition, so ignore it. An
2068 * unknown amount of data was transferred so treat it as an
2072 memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
2074 case ABORTED_COMMAND:
2075 if (sshdr.asc == 0x10) /* DIF: Target detected corruption */
2076 good_bytes = sd_completed_bytes(SCpnt);
2078 case ILLEGAL_REQUEST:
2079 switch (sshdr.asc) {
2080 case 0x10: /* DIX: Host detected corruption */
2081 good_bytes = sd_completed_bytes(SCpnt);
2083 case 0x20: /* INVALID COMMAND OPCODE */
2084 case 0x24: /* INVALID FIELD IN CDB */
2085 switch (SCpnt->cmnd[0]) {
2087 sd_config_discard(sdkp, SD_LBP_DISABLE);
2091 if (SCpnt->cmnd[1] & 8) { /* UNMAP */
2092 sd_config_discard(sdkp, SD_LBP_DISABLE);
2094 sdkp->device->no_write_same = 1;
2095 sd_config_write_same(sdkp);
2096 req->rq_flags |= RQF_QUIET;
2107 if (sd_is_zoned(sdkp))
2108 good_bytes = sd_zbc_complete(SCpnt, good_bytes, &sshdr);
2110 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
2111 "sd_done: completed %d of %d bytes\n",
2112 good_bytes, scsi_bufflen(SCpnt)));
2118 * spinup disk - called only in sd_revalidate_disk()
2121 sd_spinup_disk(struct scsi_disk *sdkp)
2123 unsigned char cmd[10];
2124 unsigned long spintime_expire = 0;
2125 int retries, spintime;
2126 unsigned int the_result;
2127 struct scsi_sense_hdr sshdr;
2128 int sense_valid = 0;
2132 /* Spin up drives, as required. Only do this at boot time */
2133 /* Spinup needs to be done for module loads too. */
2138 bool media_was_present = sdkp->media_present;
2140 cmd[0] = TEST_UNIT_READY;
2141 memset((void *) &cmd[1], 0, 9);
2143 the_result = scsi_execute_req(sdkp->device, cmd,
2146 sdkp->max_retries, NULL);
2149 * If the drive has indicated to us that it
2150 * doesn't have any media in it, don't bother
2151 * with any more polling.
2153 if (media_not_present(sdkp, &sshdr)) {
2154 if (media_was_present)
2155 sd_printk(KERN_NOTICE, sdkp, "Media removed, stopped polling\n");
2160 sense_valid = scsi_sense_valid(&sshdr);
2162 } while (retries < 3 &&
2163 (!scsi_status_is_good(the_result) ||
2164 (scsi_status_is_check_condition(the_result) &&
2165 sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
2167 if (!scsi_status_is_check_condition(the_result)) {
2168 /* no sense, TUR either succeeded or failed
2169 * with a status error */
2170 if(!spintime && !scsi_status_is_good(the_result)) {
2171 sd_print_result(sdkp, "Test Unit Ready failed",
2178 * The device does not want the automatic start to be issued.
2180 if (sdkp->device->no_start_on_add)
2183 if (sense_valid && sshdr.sense_key == NOT_READY) {
2184 if (sshdr.asc == 4 && sshdr.ascq == 3)
2185 break; /* manual intervention required */
2186 if (sshdr.asc == 4 && sshdr.ascq == 0xb)
2187 break; /* standby */
2188 if (sshdr.asc == 4 && sshdr.ascq == 0xc)
2189 break; /* unavailable */
2190 if (sshdr.asc == 4 && sshdr.ascq == 0x1b)
2191 break; /* sanitize in progress */
2193 * Issue command to spin up drive when not ready
2196 sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
2197 cmd[0] = START_STOP;
2198 cmd[1] = 1; /* Return immediately */
2199 memset((void *) &cmd[2], 0, 8);
2200 cmd[4] = 1; /* Start spin cycle */
2201 if (sdkp->device->start_stop_pwr_cond)
2203 scsi_execute_req(sdkp->device, cmd, DMA_NONE,
2205 SD_TIMEOUT, sdkp->max_retries,
2207 spintime_expire = jiffies + 100 * HZ;
2210 /* Wait 1 second for next try */
2212 printk(KERN_CONT ".");
2215 * Wait for USB flash devices with slow firmware.
2216 * Yes, this sense key/ASC combination shouldn't
2217 * occur here. It's characteristic of these devices.
2219 } else if (sense_valid &&
2220 sshdr.sense_key == UNIT_ATTENTION &&
2221 sshdr.asc == 0x28) {
2223 spintime_expire = jiffies + 5 * HZ;
2226 /* Wait 1 second for next try */
2229 /* we don't understand the sense code, so it's
2230 * probably pointless to loop */
2232 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
2233 sd_print_sense_hdr(sdkp, &sshdr);
2238 } while (spintime && time_before_eq(jiffies, spintime_expire));
2241 if (scsi_status_is_good(the_result))
2242 printk(KERN_CONT "ready\n");
2244 printk(KERN_CONT "not responding...\n");
2249 * Determine whether disk supports Data Integrity Field.
2251 static int sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer)
2253 struct scsi_device *sdp = sdkp->device;
2257 if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0) {
2258 sdkp->protection_type = 0;
2262 type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */
2264 if (type > T10_PI_TYPE3_PROTECTION)
2266 else if (scsi_host_dif_capable(sdp->host, type))
2269 if (sdkp->first_scan || type != sdkp->protection_type)
2272 sd_printk(KERN_ERR, sdkp, "formatted with unsupported" \
2273 " protection type %u. Disabling disk!\n",
2277 sd_printk(KERN_NOTICE, sdkp,
2278 "Enabling DIF Type %u protection\n", type);
2281 sd_printk(KERN_NOTICE, sdkp,
2282 "Disabling DIF Type %u protection\n", type);
2286 sdkp->protection_type = type;
2291 static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp,
2292 struct scsi_sense_hdr *sshdr, int sense_valid,
2296 sd_print_sense_hdr(sdkp, sshdr);
2298 sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
2301 * Set dirty bit for removable devices if not ready -
2302 * sometimes drives will not report this properly.
2304 if (sdp->removable &&
2305 sense_valid && sshdr->sense_key == NOT_READY)
2306 set_media_not_present(sdkp);
2309 * We used to set media_present to 0 here to indicate no media
2310 * in the drive, but some drives fail read capacity even with
2311 * media present, so we can't do that.
2313 sdkp->capacity = 0; /* unknown mapped to zero - as usual */
2317 #if RC16_LEN > SD_BUF_SIZE
2318 #error RC16_LEN must not be more than SD_BUF_SIZE
2321 #define READ_CAPACITY_RETRIES_ON_RESET 10
2323 static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
2324 unsigned char *buffer)
2326 unsigned char cmd[16];
2327 struct scsi_sense_hdr sshdr;
2328 int sense_valid = 0;
2330 int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
2331 unsigned int alignment;
2332 unsigned long long lba;
2333 unsigned sector_size;
2335 if (sdp->no_read_capacity_16)
2340 cmd[0] = SERVICE_ACTION_IN_16;
2341 cmd[1] = SAI_READ_CAPACITY_16;
2343 memset(buffer, 0, RC16_LEN);
2345 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
2346 buffer, RC16_LEN, &sshdr,
2347 SD_TIMEOUT, sdkp->max_retries, NULL);
2349 if (media_not_present(sdkp, &sshdr))
2352 if (the_result > 0) {
2353 sense_valid = scsi_sense_valid(&sshdr);
2355 sshdr.sense_key == ILLEGAL_REQUEST &&
2356 (sshdr.asc == 0x20 || sshdr.asc == 0x24) &&
2358 /* Invalid Command Operation Code or
2359 * Invalid Field in CDB, just retry
2360 * silently with RC10 */
2363 sshdr.sense_key == UNIT_ATTENTION &&
2364 sshdr.asc == 0x29 && sshdr.ascq == 0x00)
2365 /* Device reset might occur several times,
2366 * give it one more chance */
2367 if (--reset_retries > 0)
2372 } while (the_result && retries);
2375 sd_print_result(sdkp, "Read Capacity(16) failed", the_result);
2376 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
2380 sector_size = get_unaligned_be32(&buffer[8]);
2381 lba = get_unaligned_be64(&buffer[0]);
2383 if (sd_read_protection_type(sdkp, buffer) < 0) {
2388 /* Logical blocks per physical block exponent */
2389 sdkp->physical_block_size = (1 << (buffer[13] & 0xf)) * sector_size;
2392 sdkp->rc_basis = (buffer[12] >> 4) & 0x3;
2394 /* Lowest aligned logical block */
2395 alignment = ((buffer[14] & 0x3f) << 8 | buffer[15]) * sector_size;
2396 blk_queue_alignment_offset(sdp->request_queue, alignment);
2397 if (alignment && sdkp->first_scan)
2398 sd_printk(KERN_NOTICE, sdkp,
2399 "physical block alignment offset: %u\n", alignment);
2401 if (buffer[14] & 0x80) { /* LBPME */
2404 if (buffer[14] & 0x40) /* LBPRZ */
2407 sd_config_discard(sdkp, SD_LBP_WS16);
2410 sdkp->capacity = lba + 1;
2414 static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,
2415 unsigned char *buffer)
2417 unsigned char cmd[16];
2418 struct scsi_sense_hdr sshdr;
2419 int sense_valid = 0;
2421 int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
2423 unsigned sector_size;
2426 cmd[0] = READ_CAPACITY;
2427 memset(&cmd[1], 0, 9);
2428 memset(buffer, 0, 8);
2430 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
2432 SD_TIMEOUT, sdkp->max_retries, NULL);
2434 if (media_not_present(sdkp, &sshdr))
2437 if (the_result > 0) {
2438 sense_valid = scsi_sense_valid(&sshdr);
2440 sshdr.sense_key == UNIT_ATTENTION &&
2441 sshdr.asc == 0x29 && sshdr.ascq == 0x00)
2442 /* Device reset might occur several times,
2443 * give it one more chance */
2444 if (--reset_retries > 0)
2449 } while (the_result && retries);
2452 sd_print_result(sdkp, "Read Capacity(10) failed", the_result);
2453 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
2457 sector_size = get_unaligned_be32(&buffer[4]);
2458 lba = get_unaligned_be32(&buffer[0]);
2460 if (sdp->no_read_capacity_16 && (lba == 0xffffffff)) {
2461 /* Some buggy (usb cardreader) devices return an lba of
2462 0xffffffff when the want to report a size of 0 (with
2463 which they really mean no media is present) */
2465 sdkp->physical_block_size = sector_size;
2469 sdkp->capacity = lba + 1;
2470 sdkp->physical_block_size = sector_size;
2474 static int sd_try_rc16_first(struct scsi_device *sdp)
2476 if (sdp->host->max_cmd_len < 16)
2478 if (sdp->try_rc_10_first)
2480 if (sdp->scsi_level > SCSI_SPC_2)
2482 if (scsi_device_protection(sdp))
2488 * read disk capacity
2491 sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
2494 struct scsi_device *sdp = sdkp->device;
2496 if (sd_try_rc16_first(sdp)) {
2497 sector_size = read_capacity_16(sdkp, sdp, buffer);
2498 if (sector_size == -EOVERFLOW)
2500 if (sector_size == -ENODEV)
2502 if (sector_size < 0)
2503 sector_size = read_capacity_10(sdkp, sdp, buffer);
2504 if (sector_size < 0)
2507 sector_size = read_capacity_10(sdkp, sdp, buffer);
2508 if (sector_size == -EOVERFLOW)
2510 if (sector_size < 0)
2512 if ((sizeof(sdkp->capacity) > 4) &&
2513 (sdkp->capacity > 0xffffffffULL)) {
2514 int old_sector_size = sector_size;
2515 sd_printk(KERN_NOTICE, sdkp, "Very big device. "
2516 "Trying to use READ CAPACITY(16).\n");
2517 sector_size = read_capacity_16(sdkp, sdp, buffer);
2518 if (sector_size < 0) {
2519 sd_printk(KERN_NOTICE, sdkp,
2520 "Using 0xffffffff as device size\n");
2521 sdkp->capacity = 1 + (sector_t) 0xffffffff;
2522 sector_size = old_sector_size;
2525 /* Remember that READ CAPACITY(16) succeeded */
2526 sdp->try_rc_10_first = 0;
2530 /* Some devices are known to return the total number of blocks,
2531 * not the highest block number. Some devices have versions
2532 * which do this and others which do not. Some devices we might
2533 * suspect of doing this but we don't know for certain.
2535 * If we know the reported capacity is wrong, decrement it. If
2536 * we can only guess, then assume the number of blocks is even
2537 * (usually true but not always) and err on the side of lowering
2540 if (sdp->fix_capacity ||
2541 (sdp->guess_capacity && (sdkp->capacity & 0x01))) {
2542 sd_printk(KERN_INFO, sdkp, "Adjusting the sector count "
2543 "from its reported value: %llu\n",
2544 (unsigned long long) sdkp->capacity);
2549 if (sector_size == 0) {
2551 sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
2555 if (sector_size != 512 &&
2556 sector_size != 1024 &&
2557 sector_size != 2048 &&
2558 sector_size != 4096) {
2559 sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
2562 * The user might want to re-format the drive with
2563 * a supported sectorsize. Once this happens, it
2564 * would be relatively trivial to set the thing up.
2565 * For this reason, we leave the thing in the table.
2569 * set a bogus sector size so the normal read/write
2570 * logic in the block layer will eventually refuse any
2571 * request on this device without tripping over power
2572 * of two sector size assumptions
2576 blk_queue_logical_block_size(sdp->request_queue, sector_size);
2577 blk_queue_physical_block_size(sdp->request_queue,
2578 sdkp->physical_block_size);
2579 sdkp->device->sector_size = sector_size;
2581 if (sdkp->capacity > 0xffffffff)
2582 sdp->use_16_for_rw = 1;
2587 * Print disk capacity
2590 sd_print_capacity(struct scsi_disk *sdkp,
2591 sector_t old_capacity)
2593 int sector_size = sdkp->device->sector_size;
2594 char cap_str_2[10], cap_str_10[10];
2596 if (!sdkp->first_scan && old_capacity == sdkp->capacity)
2599 string_get_size(sdkp->capacity, sector_size,
2600 STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
2601 string_get_size(sdkp->capacity, sector_size,
2602 STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
2604 sd_printk(KERN_NOTICE, sdkp,
2605 "%llu %d-byte logical blocks: (%s/%s)\n",
2606 (unsigned long long)sdkp->capacity,
2607 sector_size, cap_str_10, cap_str_2);
2609 if (sdkp->physical_block_size != sector_size)
2610 sd_printk(KERN_NOTICE, sdkp,
2611 "%u-byte physical blocks\n",
2612 sdkp->physical_block_size);
2615 /* called with buffer of length 512 */
2617 sd_do_mode_sense(struct scsi_disk *sdkp, int dbd, int modepage,
2618 unsigned char *buffer, int len, struct scsi_mode_data *data,
2619 struct scsi_sense_hdr *sshdr)
2622 * If we must use MODE SENSE(10), make sure that the buffer length
2623 * is at least 8 bytes so that the mode sense header fits.
2625 if (sdkp->device->use_10_for_ms && len < 8)
2628 return scsi_mode_sense(sdkp->device, dbd, modepage, buffer, len,
2629 SD_TIMEOUT, sdkp->max_retries, data,
2634 * read write protect setting, if possible - called only in sd_revalidate_disk()
2635 * called with buffer of length SD_BUF_SIZE
2638 sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
2641 struct scsi_device *sdp = sdkp->device;
2642 struct scsi_mode_data data;
2643 int old_wp = sdkp->write_prot;
2645 set_disk_ro(sdkp->disk, 0);
2646 if (sdp->skip_ms_page_3f) {
2647 sd_first_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
2651 if (sdp->use_192_bytes_for_3f) {
2652 res = sd_do_mode_sense(sdkp, 0, 0x3F, buffer, 192, &data, NULL);
2655 * First attempt: ask for all pages (0x3F), but only 4 bytes.
2656 * We have to start carefully: some devices hang if we ask
2657 * for more than is available.
2659 res = sd_do_mode_sense(sdkp, 0, 0x3F, buffer, 4, &data, NULL);
2662 * Second attempt: ask for page 0 When only page 0 is
2663 * implemented, a request for page 3F may return Sense Key
2664 * 5: Illegal Request, Sense Code 24: Invalid field in
2668 res = sd_do_mode_sense(sdkp, 0, 0, buffer, 4, &data, NULL);
2671 * Third attempt: ask 255 bytes, as we did earlier.
2674 res = sd_do_mode_sense(sdkp, 0, 0x3F, buffer, 255,
2679 sd_first_printk(KERN_WARNING, sdkp,
2680 "Test WP failed, assume Write Enabled\n");
2682 sdkp->write_prot = ((data.device_specific & 0x80) != 0);
2683 set_disk_ro(sdkp->disk, sdkp->write_prot);
2684 if (sdkp->first_scan || old_wp != sdkp->write_prot) {
2685 sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
2686 sdkp->write_prot ? "on" : "off");
2687 sd_printk(KERN_DEBUG, sdkp, "Mode Sense: %4ph\n", buffer);
2693 * sd_read_cache_type - called only from sd_revalidate_disk()
2694 * called with buffer of length SD_BUF_SIZE
2697 sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
2700 struct scsi_device *sdp = sdkp->device;
2705 struct scsi_mode_data data;
2706 struct scsi_sense_hdr sshdr;
2707 int old_wce = sdkp->WCE;
2708 int old_rcd = sdkp->RCD;
2709 int old_dpofua = sdkp->DPOFUA;
2712 if (sdkp->cache_override)
2716 if (sdp->skip_ms_page_8) {
2717 if (sdp->type == TYPE_RBC)
2720 if (sdp->skip_ms_page_3f)
2723 if (sdp->use_192_bytes_for_3f)
2727 } else if (sdp->type == TYPE_RBC) {
2735 /* cautiously ask */
2736 res = sd_do_mode_sense(sdkp, dbd, modepage, buffer, first_len,
2742 if (!data.header_length) {
2745 sd_first_printk(KERN_ERR, sdkp,
2746 "Missing header in MODE_SENSE response\n");
2749 /* that went OK, now ask for the proper length */
2753 * We're only interested in the first three bytes, actually.
2754 * But the data cache page is defined for the first 20.
2758 else if (len > SD_BUF_SIZE) {
2759 sd_first_printk(KERN_NOTICE, sdkp, "Truncating mode parameter "
2760 "data from %d to %d bytes\n", len, SD_BUF_SIZE);
2763 if (modepage == 0x3F && sdp->use_192_bytes_for_3f)
2767 if (len > first_len)
2768 res = sd_do_mode_sense(sdkp, dbd, modepage, buffer, len,
2772 int offset = data.header_length + data.block_descriptor_length;
2774 while (offset < len) {
2775 u8 page_code = buffer[offset] & 0x3F;
2776 u8 spf = buffer[offset] & 0x40;
2778 if (page_code == 8 || page_code == 6) {
2779 /* We're interested only in the first 3 bytes.
2781 if (len - offset <= 2) {
2782 sd_first_printk(KERN_ERR, sdkp,
2783 "Incomplete mode parameter "
2787 modepage = page_code;
2791 /* Go to the next page */
2792 if (spf && len - offset > 3)
2793 offset += 4 + (buffer[offset+2] << 8) +
2795 else if (!spf && len - offset > 1)
2796 offset += 2 + buffer[offset+1];
2798 sd_first_printk(KERN_ERR, sdkp,
2800 "parameter data\n");
2806 sd_first_printk(KERN_WARNING, sdkp,
2807 "No Caching mode page found\n");
2811 if (modepage == 8) {
2812 sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
2813 sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
2815 sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
2819 sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
2820 if (sdp->broken_fua) {
2821 sd_first_printk(KERN_NOTICE, sdkp, "Disabling FUA\n");
2823 } else if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw &&
2824 !sdkp->device->use_16_for_rw) {
2825 sd_first_printk(KERN_NOTICE, sdkp,
2826 "Uses READ/WRITE(6), disabling FUA\n");
2830 /* No cache flush allowed for write protected devices */
2831 if (sdkp->WCE && sdkp->write_prot)
2834 if (sdkp->first_scan || old_wce != sdkp->WCE ||
2835 old_rcd != sdkp->RCD || old_dpofua != sdkp->DPOFUA)
2836 sd_printk(KERN_NOTICE, sdkp,
2837 "Write cache: %s, read cache: %s, %s\n",
2838 sdkp->WCE ? "enabled" : "disabled",
2839 sdkp->RCD ? "disabled" : "enabled",
2840 sdkp->DPOFUA ? "supports DPO and FUA"
2841 : "doesn't support DPO or FUA");
2847 if (scsi_sense_valid(&sshdr) &&
2848 sshdr.sense_key == ILLEGAL_REQUEST &&
2849 sshdr.asc == 0x24 && sshdr.ascq == 0x0)
2850 /* Invalid field in CDB */
2851 sd_first_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
2853 sd_first_printk(KERN_ERR, sdkp,
2854 "Asking for cache data failed\n");
2857 if (sdp->wce_default_on) {
2858 sd_first_printk(KERN_NOTICE, sdkp,
2859 "Assuming drive cache: write back\n");
2862 sd_first_printk(KERN_WARNING, sdkp,
2863 "Assuming drive cache: write through\n");
2871 * The ATO bit indicates whether the DIF application tag is available
2872 * for use by the operating system.
2874 static void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
2877 struct scsi_device *sdp = sdkp->device;
2878 struct scsi_mode_data data;
2879 struct scsi_sense_hdr sshdr;
2881 if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC)
2884 if (sdkp->protection_type == 0)
2887 res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT,
2888 sdkp->max_retries, &data, &sshdr);
2890 if (res < 0 || !data.header_length ||
2892 sd_first_printk(KERN_WARNING, sdkp,
2893 "getting Control mode page failed, assume no ATO\n");
2895 if (scsi_sense_valid(&sshdr))
2896 sd_print_sense_hdr(sdkp, &sshdr);
2901 offset = data.header_length + data.block_descriptor_length;
2903 if ((buffer[offset] & 0x3f) != 0x0a) {
2904 sd_first_printk(KERN_ERR, sdkp, "ATO Got wrong page\n");
2908 if ((buffer[offset + 5] & 0x80) == 0)
2917 * sd_read_block_limits - Query disk device for preferred I/O sizes.
2918 * @sdkp: disk to query
2920 static void sd_read_block_limits(struct scsi_disk *sdkp)
2922 unsigned int sector_sz = sdkp->device->sector_size;
2923 const int vpd_len = 64;
2924 unsigned char *buffer = kmalloc(vpd_len, GFP_KERNEL);
2927 /* Block Limits VPD */
2928 scsi_get_vpd_page(sdkp->device, 0xb0, buffer, vpd_len))
2931 blk_queue_io_min(sdkp->disk->queue,
2932 get_unaligned_be16(&buffer[6]) * sector_sz);
2934 sdkp->max_xfer_blocks = get_unaligned_be32(&buffer[8]);
2935 sdkp->opt_xfer_blocks = get_unaligned_be32(&buffer[12]);
2937 if (buffer[3] == 0x3c) {
2938 unsigned int lba_count, desc_count;
2940 sdkp->max_ws_blocks = (u32)get_unaligned_be64(&buffer[36]);
2945 lba_count = get_unaligned_be32(&buffer[20]);
2946 desc_count = get_unaligned_be32(&buffer[24]);
2948 if (lba_count && desc_count)
2949 sdkp->max_unmap_blocks = lba_count;
2951 sdkp->unmap_granularity = get_unaligned_be32(&buffer[28]);
2953 if (buffer[32] & 0x80)
2954 sdkp->unmap_alignment =
2955 get_unaligned_be32(&buffer[32]) & ~(1 << 31);
2957 if (!sdkp->lbpvpd) { /* LBP VPD page not provided */
2959 if (sdkp->max_unmap_blocks)
2960 sd_config_discard(sdkp, SD_LBP_UNMAP);
2962 sd_config_discard(sdkp, SD_LBP_WS16);
2964 } else { /* LBP VPD page tells us what to use */
2965 if (sdkp->lbpu && sdkp->max_unmap_blocks)
2966 sd_config_discard(sdkp, SD_LBP_UNMAP);
2967 else if (sdkp->lbpws)
2968 sd_config_discard(sdkp, SD_LBP_WS16);
2969 else if (sdkp->lbpws10)
2970 sd_config_discard(sdkp, SD_LBP_WS10);
2972 sd_config_discard(sdkp, SD_LBP_DISABLE);
2981 * sd_read_block_characteristics - Query block dev. characteristics
2982 * @sdkp: disk to query
2984 static void sd_read_block_characteristics(struct scsi_disk *sdkp)
2986 struct request_queue *q = sdkp->disk->queue;
2987 unsigned char *buffer;
2989 const int vpd_len = 64;
2991 buffer = kmalloc(vpd_len, GFP_KERNEL);
2994 /* Block Device Characteristics VPD */
2995 scsi_get_vpd_page(sdkp->device, 0xb1, buffer, vpd_len))
2998 rot = get_unaligned_be16(&buffer[4]);
3001 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
3002 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, q);
3005 if (sdkp->device->type == TYPE_ZBC) {
3007 blk_queue_set_zoned(sdkp->disk, BLK_ZONED_HM);
3009 sdkp->zoned = (buffer[8] >> 4) & 3;
3010 if (sdkp->zoned == 1) {
3012 blk_queue_set_zoned(sdkp->disk, BLK_ZONED_HA);
3014 /* Regular disk or drive managed disk */
3015 blk_queue_set_zoned(sdkp->disk, BLK_ZONED_NONE);
3019 if (!sdkp->first_scan)
3022 if (blk_queue_is_zoned(q)) {
3023 sd_printk(KERN_NOTICE, sdkp, "Host-%s zoned block device\n",
3024 q->limits.zoned == BLK_ZONED_HM ? "managed" : "aware");
3026 if (sdkp->zoned == 1)
3027 sd_printk(KERN_NOTICE, sdkp,
3028 "Host-aware SMR disk used as regular disk\n");
3029 else if (sdkp->zoned == 2)
3030 sd_printk(KERN_NOTICE, sdkp,
3031 "Drive-managed SMR disk\n");
3039 * sd_read_block_provisioning - Query provisioning VPD page
3040 * @sdkp: disk to query
3042 static void sd_read_block_provisioning(struct scsi_disk *sdkp)
3044 unsigned char *buffer;
3045 const int vpd_len = 8;
3047 if (sdkp->lbpme == 0)
3050 buffer = kmalloc(vpd_len, GFP_KERNEL);
3052 if (!buffer || scsi_get_vpd_page(sdkp->device, 0xb2, buffer, vpd_len))
3056 sdkp->lbpu = (buffer[5] >> 7) & 1; /* UNMAP */
3057 sdkp->lbpws = (buffer[5] >> 6) & 1; /* WRITE SAME(16) with UNMAP */
3058 sdkp->lbpws10 = (buffer[5] >> 5) & 1; /* WRITE SAME(10) with UNMAP */
3064 static void sd_read_write_same(struct scsi_disk *sdkp, unsigned char *buffer)
3066 struct scsi_device *sdev = sdkp->device;
3068 if (sdev->host->no_write_same) {
3069 sdev->no_write_same = 1;
3074 if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, INQUIRY) < 0) {
3075 /* too large values might cause issues with arcmsr */
3076 int vpd_buf_len = 64;
3078 sdev->no_report_opcodes = 1;
3080 /* Disable WRITE SAME if REPORT SUPPORTED OPERATION
3081 * CODES is unsupported and the device has an ATA
3082 * Information VPD page (SAT).
3084 if (!scsi_get_vpd_page(sdev, 0x89, buffer, vpd_buf_len))
3085 sdev->no_write_same = 1;
3088 if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, WRITE_SAME_16) == 1)
3091 if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, WRITE_SAME) == 1)
3095 static void sd_read_security(struct scsi_disk *sdkp, unsigned char *buffer)
3097 struct scsi_device *sdev = sdkp->device;
3099 if (!sdev->security_supported)
3102 if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE,
3103 SECURITY_PROTOCOL_IN) == 1 &&
3104 scsi_report_opcode(sdev, buffer, SD_BUF_SIZE,
3105 SECURITY_PROTOCOL_OUT) == 1)
3109 static inline sector_t sd64_to_sectors(struct scsi_disk *sdkp, u8 *buf)
3111 return logical_to_sectors(sdkp->device, get_unaligned_be64(buf));
3115 * sd_read_cpr - Query concurrent positioning ranges
3116 * @sdkp: disk to query
3118 static void sd_read_cpr(struct scsi_disk *sdkp)
3120 struct blk_independent_access_ranges *iars = NULL;
3121 unsigned char *buffer = NULL;
3122 unsigned int nr_cpr = 0;
3123 int i, vpd_len, buf_len = SD_BUF_SIZE;
3127 * We need to have the capacity set first for the block layer to be
3128 * able to check the ranges.
3130 if (sdkp->first_scan)
3133 if (!sdkp->capacity)
3137 * Concurrent Positioning Ranges VPD: there can be at most 256 ranges,
3138 * leading to a maximum page size of 64 + 256*32 bytes.
3140 buf_len = 64 + 256*32;
3141 buffer = kmalloc(buf_len, GFP_KERNEL);
3142 if (!buffer || scsi_get_vpd_page(sdkp->device, 0xb9, buffer, buf_len))
3145 /* We must have at least a 64B header and one 32B range descriptor */
3146 vpd_len = get_unaligned_be16(&buffer[2]) + 3;
3147 if (vpd_len > buf_len || vpd_len < 64 + 32 || (vpd_len & 31)) {
3148 sd_printk(KERN_ERR, sdkp,
3149 "Invalid Concurrent Positioning Ranges VPD page\n");
3153 nr_cpr = (vpd_len - 64) / 32;
3159 iars = disk_alloc_independent_access_ranges(sdkp->disk, nr_cpr);
3166 for (i = 0; i < nr_cpr; i++, desc += 32) {
3168 sd_printk(KERN_ERR, sdkp,
3169 "Invalid Concurrent Positioning Range number\n");
3174 iars->ia_range[i].sector = sd64_to_sectors(sdkp, desc + 8);
3175 iars->ia_range[i].nr_sectors = sd64_to_sectors(sdkp, desc + 16);
3179 disk_set_independent_access_ranges(sdkp->disk, iars);
3180 if (nr_cpr && sdkp->nr_actuators != nr_cpr) {
3181 sd_printk(KERN_NOTICE, sdkp,
3182 "%u concurrent positioning ranges\n", nr_cpr);
3183 sdkp->nr_actuators = nr_cpr;
3190 * Determine the device's preferred I/O size for reads and writes
3191 * unless the reported value is unreasonably small, large, not a
3192 * multiple of the physical block size, or simply garbage.
3194 static bool sd_validate_opt_xfer_size(struct scsi_disk *sdkp,
3195 unsigned int dev_max)
3197 struct scsi_device *sdp = sdkp->device;
3198 unsigned int opt_xfer_bytes =
3199 logical_to_bytes(sdp, sdkp->opt_xfer_blocks);
3201 if (sdkp->opt_xfer_blocks == 0)
3204 if (sdkp->opt_xfer_blocks > dev_max) {
3205 sd_first_printk(KERN_WARNING, sdkp,
3206 "Optimal transfer size %u logical blocks " \
3207 "> dev_max (%u logical blocks)\n",
3208 sdkp->opt_xfer_blocks, dev_max);
3212 if (sdkp->opt_xfer_blocks > SD_DEF_XFER_BLOCKS) {
3213 sd_first_printk(KERN_WARNING, sdkp,
3214 "Optimal transfer size %u logical blocks " \
3215 "> sd driver limit (%u logical blocks)\n",
3216 sdkp->opt_xfer_blocks, SD_DEF_XFER_BLOCKS);
3220 if (opt_xfer_bytes < PAGE_SIZE) {
3221 sd_first_printk(KERN_WARNING, sdkp,
3222 "Optimal transfer size %u bytes < " \
3223 "PAGE_SIZE (%u bytes)\n",
3224 opt_xfer_bytes, (unsigned int)PAGE_SIZE);
3228 if (opt_xfer_bytes & (sdkp->physical_block_size - 1)) {
3229 sd_first_printk(KERN_WARNING, sdkp,
3230 "Optimal transfer size %u bytes not a " \
3231 "multiple of physical block size (%u bytes)\n",
3232 opt_xfer_bytes, sdkp->physical_block_size);
3236 sd_first_printk(KERN_INFO, sdkp, "Optimal transfer size %u bytes\n",
3242 * sd_revalidate_disk - called the first time a new disk is seen,
3243 * performs disk spin up, read_capacity, etc.
3244 * @disk: struct gendisk we care about
3246 static int sd_revalidate_disk(struct gendisk *disk)
3248 struct scsi_disk *sdkp = scsi_disk(disk);
3249 struct scsi_device *sdp = sdkp->device;
3250 struct request_queue *q = sdkp->disk->queue;
3251 sector_t old_capacity = sdkp->capacity;
3252 unsigned char *buffer;
3253 unsigned int dev_max, rw_max;
3255 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
3256 "sd_revalidate_disk\n"));
3259 * If the device is offline, don't try and read capacity or any
3260 * of the other niceties.
3262 if (!scsi_device_online(sdp))
3265 buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL);
3267 sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
3268 "allocation failure.\n");
3272 sd_spinup_disk(sdkp);
3275 * Without media there is no reason to ask; moreover, some devices
3276 * react badly if we do.
3278 if (sdkp->media_present) {
3279 sd_read_capacity(sdkp, buffer);
3282 * set the default to rotational. All non-rotational devices
3283 * support the block characteristics VPD page, which will
3284 * cause this to be updated correctly and any device which
3285 * doesn't support it should be treated as rotational.
3287 blk_queue_flag_clear(QUEUE_FLAG_NONROT, q);
3288 blk_queue_flag_set(QUEUE_FLAG_ADD_RANDOM, q);
3290 if (scsi_device_supports_vpd(sdp)) {
3291 sd_read_block_provisioning(sdkp);
3292 sd_read_block_limits(sdkp);
3293 sd_read_block_characteristics(sdkp);
3294 sd_zbc_read_zones(sdkp, buffer);
3297 sd_print_capacity(sdkp, old_capacity);
3299 sd_read_write_protect_flag(sdkp, buffer);
3300 sd_read_cache_type(sdkp, buffer);
3301 sd_read_app_tag_own(sdkp, buffer);
3302 sd_read_write_same(sdkp, buffer);
3303 sd_read_security(sdkp, buffer);
3308 * We now have all cache related info, determine how we deal
3309 * with flush requests.
3311 sd_set_flush_flag(sdkp);
3313 /* Initial block count limit based on CDB TRANSFER LENGTH field size. */
3314 dev_max = sdp->use_16_for_rw ? SD_MAX_XFER_BLOCKS : SD_DEF_XFER_BLOCKS;
3316 /* Some devices report a maximum block count for READ/WRITE requests. */
3317 dev_max = min_not_zero(dev_max, sdkp->max_xfer_blocks);
3318 q->limits.max_dev_sectors = logical_to_sectors(sdp, dev_max);
3320 if (sd_validate_opt_xfer_size(sdkp, dev_max)) {
3321 q->limits.io_opt = logical_to_bytes(sdp, sdkp->opt_xfer_blocks);
3322 rw_max = logical_to_sectors(sdp, sdkp->opt_xfer_blocks);
3324 q->limits.io_opt = 0;
3325 rw_max = min_not_zero(logical_to_sectors(sdp, dev_max),
3326 (sector_t)BLK_DEF_MAX_SECTORS);
3329 /* Do not exceed controller limit */
3330 rw_max = min(rw_max, queue_max_hw_sectors(q));
3333 * Only update max_sectors if previously unset or if the current value
3334 * exceeds the capabilities of the hardware.
3336 if (sdkp->first_scan ||
3337 q->limits.max_sectors > q->limits.max_dev_sectors ||
3338 q->limits.max_sectors > q->limits.max_hw_sectors)
3339 q->limits.max_sectors = rw_max;
3341 sdkp->first_scan = 0;
3343 set_capacity_and_notify(disk, logical_to_sectors(sdp, sdkp->capacity));
3344 sd_config_write_same(sdkp);
3348 * For a zoned drive, revalidating the zones can be done only once
3349 * the gendisk capacity is set. So if this fails, set back the gendisk
3352 if (sd_zbc_revalidate_zones(sdkp))
3353 set_capacity_and_notify(disk, 0);
3360 * sd_unlock_native_capacity - unlock native capacity
3361 * @disk: struct gendisk to set capacity for
3363 * Block layer calls this function if it detects that partitions
3364 * on @disk reach beyond the end of the device. If the SCSI host
3365 * implements ->unlock_native_capacity() method, it's invoked to
3366 * give it a chance to adjust the device capacity.
3369 * Defined by block layer. Might sleep.
3371 static void sd_unlock_native_capacity(struct gendisk *disk)
3373 struct scsi_device *sdev = scsi_disk(disk)->device;
3375 if (sdev->host->hostt->unlock_native_capacity)
3376 sdev->host->hostt->unlock_native_capacity(sdev);
3380 * sd_format_disk_name - format disk name
3381 * @prefix: name prefix - ie. "sd" for SCSI disks
3382 * @index: index of the disk to format name for
3383 * @buf: output buffer
3384 * @buflen: length of the output buffer
3386 * SCSI disk names starts at sda. The 26th device is sdz and the
3387 * 27th is sdaa. The last one for two lettered suffix is sdzz
3388 * which is followed by sdaaa.
3390 * This is basically 26 base counting with one extra 'nil' entry
3391 * at the beginning from the second digit on and can be
3392 * determined using similar method as 26 base conversion with the
3393 * index shifted -1 after each digit is computed.
3399 * 0 on success, -errno on failure.
3401 static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen)
3403 const int base = 'z' - 'a' + 1;
3404 char *begin = buf + strlen(prefix);
3405 char *end = buf + buflen;
3415 *--p = 'a' + (index % unit);
3416 index = (index / unit) - 1;
3417 } while (index >= 0);
3419 memmove(begin, p, end - p);
3420 memcpy(buf, prefix, strlen(prefix));
3426 * sd_probe - called during driver initialization and whenever a
3427 * new scsi device is attached to the system. It is called once
3428 * for each scsi device (not just disks) present.
3429 * @dev: pointer to device object
3431 * Returns 0 if successful (or not interested in this scsi device
3432 * (e.g. scanner)); 1 when there is an error.
3434 * Note: this function is invoked from the scsi mid-level.
3435 * This function sets up the mapping between a given
3436 * <host,channel,id,lun> (found in sdp) and new device name
3437 * (e.g. /dev/sda). More precisely it is the block device major
3438 * and minor number that is chosen here.
3440 * Assume sd_probe is not re-entrant (for time being)
3441 * Also think about sd_probe() and sd_remove() running coincidentally.
3443 static int sd_probe(struct device *dev)
3445 struct scsi_device *sdp = to_scsi_device(dev);
3446 struct scsi_disk *sdkp;
3451 scsi_autopm_get_device(sdp);
3453 if (sdp->type != TYPE_DISK &&
3454 sdp->type != TYPE_ZBC &&
3455 sdp->type != TYPE_MOD &&
3456 sdp->type != TYPE_RBC)
3459 if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED) && sdp->type == TYPE_ZBC) {
3460 sdev_printk(KERN_WARNING, sdp,
3461 "Unsupported ZBC host-managed device.\n");
3465 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
3469 sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
3473 gd = __alloc_disk_node(sdp->request_queue, NUMA_NO_NODE,
3474 &sd_bio_compl_lkclass);
3478 index = ida_alloc(&sd_index_ida, GFP_KERNEL);
3480 sdev_printk(KERN_WARNING, sdp, "sd_probe: memory exhausted.\n");
3484 error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN);
3486 sdev_printk(KERN_WARNING, sdp, "SCSI disk (sd) name length exceeded.\n");
3487 goto out_free_index;
3492 sdkp->index = index;
3493 sdkp->max_retries = SD_MAX_RETRIES;
3494 atomic_set(&sdkp->openers, 0);
3495 atomic_set(&sdkp->device->ioerr_cnt, 0);
3497 if (!sdp->request_queue->rq_timeout) {
3498 if (sdp->type != TYPE_MOD)
3499 blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT);
3501 blk_queue_rq_timeout(sdp->request_queue,
3505 device_initialize(&sdkp->disk_dev);
3506 sdkp->disk_dev.parent = get_device(dev);
3507 sdkp->disk_dev.class = &sd_disk_class;
3508 dev_set_name(&sdkp->disk_dev, "%s", dev_name(dev));
3510 error = device_add(&sdkp->disk_dev);
3512 put_device(&sdkp->disk_dev);
3516 dev_set_drvdata(dev, sdkp);
3518 gd->major = sd_major((index & 0xf0) >> 4);
3519 gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
3520 gd->minors = SD_MINORS;
3522 gd->fops = &sd_fops;
3523 gd->private_data = sdkp;
3525 /* defaults, until the device tells us otherwise */
3526 sdp->sector_size = 512;
3528 sdkp->media_present = 1;
3529 sdkp->write_prot = 0;
3530 sdkp->cache_override = 0;
3534 sdkp->first_scan = 1;
3535 sdkp->max_medium_access_timeouts = SD_MAX_MEDIUM_TIMEOUTS;
3537 sd_revalidate_disk(gd);
3539 if (sdp->removable) {
3540 gd->flags |= GENHD_FL_REMOVABLE;
3541 gd->events |= DISK_EVENT_MEDIA_CHANGE;
3542 gd->event_flags = DISK_EVENT_FLAG_POLL | DISK_EVENT_FLAG_UEVENT;
3545 blk_pm_runtime_init(sdp->request_queue, dev);
3546 if (sdp->rpm_autosuspend) {
3547 pm_runtime_set_autosuspend_delay(dev,
3548 sdp->host->hostt->rpm_autosuspend_delay);
3551 error = device_add_disk(dev, gd, NULL);
3553 put_device(&sdkp->disk_dev);
3558 sd_dif_config_host(sdkp);
3560 sd_revalidate_disk(gd);
3562 if (sdkp->security) {
3563 sdkp->opal_dev = init_opal_dev(sdkp, &sd_sec_submit);
3565 sd_printk(KERN_NOTICE, sdkp, "supports TCG Opal\n");
3568 sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
3569 sdp->removable ? "removable " : "");
3570 scsi_autopm_put_device(sdp);
3575 ida_free(&sd_index_ida, index);
3579 sd_zbc_release_disk(sdkp);
3582 scsi_autopm_put_device(sdp);
3587 * sd_remove - called whenever a scsi disk (previously recognized by
3588 * sd_probe) is detached from the system. It is called (potentially
3589 * multiple times) during sd module unload.
3590 * @dev: pointer to device object
3592 * Note: this function is invoked from the scsi mid-level.
3593 * This function potentially frees up a device name (e.g. /dev/sdc)
3594 * that could be re-used by a subsequent sd_probe().
3595 * This function is not called when the built-in sd driver is "exit-ed".
3597 static int sd_remove(struct device *dev)
3599 struct scsi_disk *sdkp = dev_get_drvdata(dev);
3601 scsi_autopm_get_device(sdkp->device);
3603 device_del(&sdkp->disk_dev);
3604 del_gendisk(sdkp->disk);
3607 put_disk(sdkp->disk);
3611 static void scsi_disk_release(struct device *dev)
3613 struct scsi_disk *sdkp = to_scsi_disk(dev);
3615 ida_free(&sd_index_ida, sdkp->index);
3616 sd_zbc_release_disk(sdkp);
3617 put_device(&sdkp->device->sdev_gendev);
3618 free_opal_dev(sdkp->opal_dev);
3623 static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
3625 unsigned char cmd[6] = { START_STOP }; /* START_VALID */
3626 struct scsi_sense_hdr sshdr;
3627 struct scsi_device *sdp = sdkp->device;
3631 cmd[4] |= 1; /* START */
3633 if (sdp->start_stop_pwr_cond)
3634 cmd[4] |= start ? 1 << 4 : 3 << 4; /* Active or Standby */
3636 if (!scsi_device_online(sdp))
3639 res = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr,
3640 SD_TIMEOUT, sdkp->max_retries, 0, RQF_PM, NULL);
3642 sd_print_result(sdkp, "Start/Stop Unit failed", res);
3643 if (res > 0 && scsi_sense_valid(&sshdr)) {
3644 sd_print_sense_hdr(sdkp, &sshdr);
3645 /* 0x3a is medium not present */
3646 if (sshdr.asc == 0x3a)
3651 /* SCSI error codes must not go to the generic layer */
3659 * Send a SYNCHRONIZE CACHE instruction down to the device through
3660 * the normal SCSI command structure. Wait for the command to
3663 static void sd_shutdown(struct device *dev)
3665 struct scsi_disk *sdkp = dev_get_drvdata(dev);
3668 return; /* this can happen */
3670 if (pm_runtime_suspended(dev))
3673 if (sdkp->WCE && sdkp->media_present) {
3674 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
3675 sd_sync_cache(sdkp, NULL);
3678 if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
3679 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
3680 sd_start_stop_device(sdkp, 0);
3684 static int sd_suspend_common(struct device *dev, bool ignore_stop_errors)
3686 struct scsi_disk *sdkp = dev_get_drvdata(dev);
3687 struct scsi_sense_hdr sshdr;
3690 if (!sdkp) /* E.g.: runtime suspend following sd_remove() */
3693 if (sdkp->WCE && sdkp->media_present) {
3694 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
3695 ret = sd_sync_cache(sdkp, &sshdr);
3698 /* ignore OFFLINE device */
3702 if (!scsi_sense_valid(&sshdr) ||
3703 sshdr.sense_key != ILLEGAL_REQUEST)
3707 * sshdr.sense_key == ILLEGAL_REQUEST means this drive
3708 * doesn't support sync. There's not much to do and
3709 * suspend shouldn't fail.
3715 if (sdkp->device->manage_start_stop) {
3716 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
3717 /* an error is not worth aborting a system sleep */
3718 ret = sd_start_stop_device(sdkp, 0);
3719 if (ignore_stop_errors)
3726 static int sd_suspend_system(struct device *dev)
3728 if (pm_runtime_suspended(dev))
3731 return sd_suspend_common(dev, true);
3734 static int sd_suspend_runtime(struct device *dev)
3736 return sd_suspend_common(dev, false);
3739 static int sd_resume(struct device *dev)
3741 struct scsi_disk *sdkp = dev_get_drvdata(dev);
3744 if (!sdkp) /* E.g.: runtime resume at the start of sd_probe() */
3747 if (!sdkp->device->manage_start_stop)
3750 sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
3751 ret = sd_start_stop_device(sdkp, 1);
3753 opal_unlock_from_suspend(sdkp->opal_dev);
3757 static int sd_resume_system(struct device *dev)
3759 if (pm_runtime_suspended(dev))
3762 return sd_resume(dev);
3765 static int sd_resume_runtime(struct device *dev)
3767 struct scsi_disk *sdkp = dev_get_drvdata(dev);
3768 struct scsi_device *sdp;
3770 if (!sdkp) /* E.g.: runtime resume at the start of sd_probe() */
3775 if (sdp->ignore_media_change) {
3776 /* clear the device's sense data */
3777 static const u8 cmd[10] = { REQUEST_SENSE };
3779 if (scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL,
3780 NULL, sdp->request_queue->rq_timeout, 1, 0,
3782 sd_printk(KERN_NOTICE, sdkp,
3783 "Failed to clear sense data\n");
3786 return sd_resume(dev);
3790 * init_sd - entry point for this driver (both when built in or when
3793 * Note: this function registers this driver with the scsi mid-level.
3795 static int __init init_sd(void)
3797 int majors = 0, i, err;
3799 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
3801 for (i = 0; i < SD_MAJORS; i++) {
3802 if (__register_blkdev(sd_major(i), "sd", sd_default_probe))
3810 err = class_register(&sd_disk_class);
3814 sd_cdb_cache = kmem_cache_create("sd_ext_cdb", SD_EXT_CDB_SIZE,
3816 if (!sd_cdb_cache) {
3817 printk(KERN_ERR "sd: can't init extended cdb cache\n");
3822 sd_cdb_pool = mempool_create_slab_pool(SD_MEMPOOL_SIZE, sd_cdb_cache);
3824 printk(KERN_ERR "sd: can't init extended cdb pool\n");
3829 sd_page_pool = mempool_create_page_pool(SD_MEMPOOL_SIZE, 0);
3830 if (!sd_page_pool) {
3831 printk(KERN_ERR "sd: can't init discard page pool\n");
3836 err = scsi_register_driver(&sd_template.gendrv);
3838 goto err_out_driver;
3843 mempool_destroy(sd_page_pool);
3846 mempool_destroy(sd_cdb_pool);
3849 kmem_cache_destroy(sd_cdb_cache);
3852 class_unregister(&sd_disk_class);
3854 for (i = 0; i < SD_MAJORS; i++)
3855 unregister_blkdev(sd_major(i), "sd");
3860 * exit_sd - exit point for this driver (when it is a module).
3862 * Note: this function unregisters this driver from the scsi mid-level.
3864 static void __exit exit_sd(void)
3868 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
3870 scsi_unregister_driver(&sd_template.gendrv);
3871 mempool_destroy(sd_cdb_pool);
3872 mempool_destroy(sd_page_pool);
3873 kmem_cache_destroy(sd_cdb_cache);
3875 class_unregister(&sd_disk_class);
3877 for (i = 0; i < SD_MAJORS; i++)
3878 unregister_blkdev(sd_major(i), "sd");
3881 module_init(init_sd);
3882 module_exit(exit_sd);
3884 void sd_print_sense_hdr(struct scsi_disk *sdkp, struct scsi_sense_hdr *sshdr)
3886 scsi_print_sense_hdr(sdkp->device,
3887 sdkp->disk ? sdkp->disk->disk_name : NULL, sshdr);
3890 void sd_print_result(const struct scsi_disk *sdkp, const char *msg, int result)
3892 const char *hb_string = scsi_hostbyte_string(result);
3895 sd_printk(KERN_INFO, sdkp,
3896 "%s: Result: hostbyte=%s driverbyte=%s\n", msg,
3897 hb_string ? hb_string : "invalid",
3900 sd_printk(KERN_INFO, sdkp,
3901 "%s: Result: hostbyte=0x%02x driverbyte=%s\n",
3902 msg, host_byte(result), "DRIVER_OK");