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/bio-integrity.h>
37 #include <linux/module.h>
39 #include <linux/kernel.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/rw_hint.h>
51 #include <linux/major.h>
52 #include <linux/mutex.h>
53 #include <linux/string_helpers.h>
54 #include <linux/slab.h>
55 #include <linux/sed-opal.h>
56 #include <linux/pm_runtime.h>
58 #include <linux/t10-pi.h>
59 #include <linux/uaccess.h>
60 #include <linux/unaligned.h>
62 #include <scsi/scsi.h>
63 #include <scsi/scsi_cmnd.h>
64 #include <scsi/scsi_dbg.h>
65 #include <scsi/scsi_device.h>
66 #include <scsi/scsi_devinfo.h>
67 #include <scsi/scsi_driver.h>
68 #include <scsi/scsi_eh.h>
69 #include <scsi/scsi_host.h>
70 #include <scsi/scsi_ioctl.h>
71 #include <scsi/scsicam.h>
72 #include <scsi/scsi_common.h>
75 #include "scsi_priv.h"
76 #include "scsi_logging.h"
78 MODULE_AUTHOR("Eric Youngdale");
79 MODULE_DESCRIPTION("SCSI disk (sd) driver");
80 MODULE_LICENSE("GPL");
82 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR
);
83 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR
);
84 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR
);
85 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR
);
86 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR
);
87 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR
);
88 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR
);
89 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR
);
90 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR
);
91 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR
);
92 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR
);
93 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR
);
94 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR
);
95 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR
);
96 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR
);
97 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR
);
98 MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK
);
99 MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD
);
100 MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC
);
101 MODULE_ALIAS_SCSI_DEVICE(TYPE_ZBC
);
105 static void sd_config_discard(struct scsi_disk
*sdkp
, struct queue_limits
*lim
,
107 static void sd_config_write_same(struct scsi_disk
*sdkp
,
108 struct queue_limits
*lim
);
109 static int sd_revalidate_disk(struct gendisk
*);
110 static void sd_unlock_native_capacity(struct gendisk
*disk
);
111 static void sd_shutdown(struct device
*);
112 static void scsi_disk_release(struct device
*cdev
);
114 static DEFINE_IDA(sd_index_ida
);
116 static mempool_t
*sd_page_pool
;
117 static struct lock_class_key sd_bio_compl_lkclass
;
119 static const char *sd_cache_types
[] = {
120 "write through", "none", "write back",
121 "write back, no read (daft)"
124 static void sd_set_flush_flag(struct scsi_disk
*sdkp
,
125 struct queue_limits
*lim
)
128 lim
->features
|= BLK_FEAT_WRITE_CACHE
;
130 lim
->features
|= BLK_FEAT_FUA
;
132 lim
->features
&= ~BLK_FEAT_FUA
;
134 lim
->features
&= ~(BLK_FEAT_WRITE_CACHE
| BLK_FEAT_FUA
);
139 cache_type_store(struct device
*dev
, struct device_attribute
*attr
,
140 const char *buf
, size_t count
)
142 int ct
, rcd
, wce
, sp
;
143 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
144 struct scsi_device
*sdp
= sdkp
->device
;
147 struct scsi_mode_data data
;
148 struct scsi_sense_hdr sshdr
;
149 static const char temp
[] = "temporary ";
152 if (sdp
->type
!= TYPE_DISK
&& sdp
->type
!= TYPE_ZBC
)
153 /* no cache control on RBC devices; theoretically they
154 * can do it, but there's probably so many exceptions
155 * it's not worth the risk */
158 if (strncmp(buf
, temp
, sizeof(temp
) - 1) == 0) {
159 buf
+= sizeof(temp
) - 1;
160 sdkp
->cache_override
= 1;
162 sdkp
->cache_override
= 0;
165 ct
= sysfs_match_string(sd_cache_types
, buf
);
169 rcd
= ct
& 0x01 ? 1 : 0;
170 wce
= (ct
& 0x02) && !sdkp
->write_prot
? 1 : 0;
172 if (sdkp
->cache_override
) {
173 struct queue_limits lim
;
178 lim
= queue_limits_start_update(sdkp
->disk
->queue
);
179 sd_set_flush_flag(sdkp
, &lim
);
180 blk_mq_freeze_queue(sdkp
->disk
->queue
);
181 ret
= queue_limits_commit_update(sdkp
->disk
->queue
, &lim
);
182 blk_mq_unfreeze_queue(sdkp
->disk
->queue
);
188 if (scsi_mode_sense(sdp
, 0x08, 8, 0, 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 ret
= scsi_mode_select(sdp
, 1, sp
, buffer_data
, len
, SD_TIMEOUT
,
207 sdkp
->max_retries
, &data
, &sshdr
);
209 if (ret
> 0 && scsi_sense_valid(&sshdr
))
210 sd_print_sense_hdr(sdkp
, &sshdr
);
213 sd_revalidate_disk(sdkp
->disk
);
218 manage_start_stop_show(struct device
*dev
,
219 struct device_attribute
*attr
, char *buf
)
221 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
222 struct scsi_device
*sdp
= sdkp
->device
;
224 return sysfs_emit(buf
, "%u\n",
225 sdp
->manage_system_start_stop
&&
226 sdp
->manage_runtime_start_stop
&&
227 sdp
->manage_shutdown
);
229 static DEVICE_ATTR_RO(manage_start_stop
);
232 manage_system_start_stop_show(struct device
*dev
,
233 struct device_attribute
*attr
, char *buf
)
235 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
236 struct scsi_device
*sdp
= sdkp
->device
;
238 return sysfs_emit(buf
, "%u\n", sdp
->manage_system_start_stop
);
242 manage_system_start_stop_store(struct device
*dev
,
243 struct device_attribute
*attr
,
244 const char *buf
, size_t count
)
246 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
247 struct scsi_device
*sdp
= sdkp
->device
;
250 if (!capable(CAP_SYS_ADMIN
))
253 if (kstrtobool(buf
, &v
))
256 sdp
->manage_system_start_stop
= v
;
260 static DEVICE_ATTR_RW(manage_system_start_stop
);
263 manage_runtime_start_stop_show(struct device
*dev
,
264 struct device_attribute
*attr
, char *buf
)
266 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
267 struct scsi_device
*sdp
= sdkp
->device
;
269 return sysfs_emit(buf
, "%u\n", sdp
->manage_runtime_start_stop
);
273 manage_runtime_start_stop_store(struct device
*dev
,
274 struct device_attribute
*attr
,
275 const char *buf
, size_t count
)
277 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
278 struct scsi_device
*sdp
= sdkp
->device
;
281 if (!capable(CAP_SYS_ADMIN
))
284 if (kstrtobool(buf
, &v
))
287 sdp
->manage_runtime_start_stop
= v
;
291 static DEVICE_ATTR_RW(manage_runtime_start_stop
);
293 static ssize_t
manage_shutdown_show(struct device
*dev
,
294 struct device_attribute
*attr
, char *buf
)
296 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
297 struct scsi_device
*sdp
= sdkp
->device
;
299 return sysfs_emit(buf
, "%u\n", sdp
->manage_shutdown
);
302 static ssize_t
manage_shutdown_store(struct device
*dev
,
303 struct device_attribute
*attr
,
304 const char *buf
, size_t count
)
306 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
307 struct scsi_device
*sdp
= sdkp
->device
;
310 if (!capable(CAP_SYS_ADMIN
))
313 if (kstrtobool(buf
, &v
))
316 sdp
->manage_shutdown
= v
;
320 static DEVICE_ATTR_RW(manage_shutdown
);
323 allow_restart_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
325 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
327 return sprintf(buf
, "%u\n", sdkp
->device
->allow_restart
);
331 allow_restart_store(struct device
*dev
, struct device_attribute
*attr
,
332 const char *buf
, size_t count
)
335 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
336 struct scsi_device
*sdp
= sdkp
->device
;
338 if (!capable(CAP_SYS_ADMIN
))
341 if (sdp
->type
!= TYPE_DISK
&& sdp
->type
!= TYPE_ZBC
)
344 if (kstrtobool(buf
, &v
))
347 sdp
->allow_restart
= v
;
351 static DEVICE_ATTR_RW(allow_restart
);
354 cache_type_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
356 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
357 int ct
= sdkp
->RCD
+ 2*sdkp
->WCE
;
359 return sprintf(buf
, "%s\n", sd_cache_types
[ct
]);
361 static DEVICE_ATTR_RW(cache_type
);
364 FUA_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
366 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
368 return sprintf(buf
, "%u\n", sdkp
->DPOFUA
);
370 static DEVICE_ATTR_RO(FUA
);
373 protection_type_show(struct device
*dev
, struct device_attribute
*attr
,
376 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
378 return sprintf(buf
, "%u\n", sdkp
->protection_type
);
382 protection_type_store(struct device
*dev
, struct device_attribute
*attr
,
383 const char *buf
, size_t count
)
385 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
389 if (!capable(CAP_SYS_ADMIN
))
392 err
= kstrtouint(buf
, 10, &val
);
397 if (val
<= T10_PI_TYPE3_PROTECTION
)
398 sdkp
->protection_type
= val
;
402 static DEVICE_ATTR_RW(protection_type
);
405 protection_mode_show(struct device
*dev
, struct device_attribute
*attr
,
408 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
409 struct scsi_device
*sdp
= sdkp
->device
;
410 unsigned int dif
, dix
;
412 dif
= scsi_host_dif_capable(sdp
->host
, sdkp
->protection_type
);
413 dix
= scsi_host_dix_capable(sdp
->host
, sdkp
->protection_type
);
415 if (!dix
&& scsi_host_dix_capable(sdp
->host
, T10_PI_TYPE0_PROTECTION
)) {
421 return sprintf(buf
, "none\n");
423 return sprintf(buf
, "%s%u\n", dix
? "dix" : "dif", dif
);
425 static DEVICE_ATTR_RO(protection_mode
);
428 app_tag_own_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
430 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
432 return sprintf(buf
, "%u\n", sdkp
->ATO
);
434 static DEVICE_ATTR_RO(app_tag_own
);
437 thin_provisioning_show(struct device
*dev
, struct device_attribute
*attr
,
440 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
442 return sprintf(buf
, "%u\n", sdkp
->lbpme
);
444 static DEVICE_ATTR_RO(thin_provisioning
);
446 /* sysfs_match_string() requires dense arrays */
447 static const char *lbp_mode
[] = {
448 [SD_LBP_FULL
] = "full",
449 [SD_LBP_UNMAP
] = "unmap",
450 [SD_LBP_WS16
] = "writesame_16",
451 [SD_LBP_WS10
] = "writesame_10",
452 [SD_LBP_ZERO
] = "writesame_zero",
453 [SD_LBP_DISABLE
] = "disabled",
457 provisioning_mode_show(struct device
*dev
, struct device_attribute
*attr
,
460 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
462 return sprintf(buf
, "%s\n", lbp_mode
[sdkp
->provisioning_mode
]);
466 provisioning_mode_store(struct device
*dev
, struct device_attribute
*attr
,
467 const char *buf
, size_t count
)
469 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
470 struct scsi_device
*sdp
= sdkp
->device
;
471 struct queue_limits lim
;
474 if (!capable(CAP_SYS_ADMIN
))
477 if (sdp
->type
!= TYPE_DISK
)
480 mode
= sysfs_match_string(lbp_mode
, buf
);
484 lim
= queue_limits_start_update(sdkp
->disk
->queue
);
485 sd_config_discard(sdkp
, &lim
, mode
);
486 blk_mq_freeze_queue(sdkp
->disk
->queue
);
487 err
= queue_limits_commit_update(sdkp
->disk
->queue
, &lim
);
488 blk_mq_unfreeze_queue(sdkp
->disk
->queue
);
493 static DEVICE_ATTR_RW(provisioning_mode
);
495 /* sysfs_match_string() requires dense arrays */
496 static const char *zeroing_mode
[] = {
497 [SD_ZERO_WRITE
] = "write",
498 [SD_ZERO_WS
] = "writesame",
499 [SD_ZERO_WS16_UNMAP
] = "writesame_16_unmap",
500 [SD_ZERO_WS10_UNMAP
] = "writesame_10_unmap",
504 zeroing_mode_show(struct device
*dev
, struct device_attribute
*attr
,
507 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
509 return sprintf(buf
, "%s\n", zeroing_mode
[sdkp
->zeroing_mode
]);
513 zeroing_mode_store(struct device
*dev
, struct device_attribute
*attr
,
514 const char *buf
, size_t count
)
516 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
519 if (!capable(CAP_SYS_ADMIN
))
522 mode
= sysfs_match_string(zeroing_mode
, buf
);
526 sdkp
->zeroing_mode
= mode
;
530 static DEVICE_ATTR_RW(zeroing_mode
);
533 max_medium_access_timeouts_show(struct device
*dev
,
534 struct device_attribute
*attr
, char *buf
)
536 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
538 return sprintf(buf
, "%u\n", sdkp
->max_medium_access_timeouts
);
542 max_medium_access_timeouts_store(struct device
*dev
,
543 struct device_attribute
*attr
, const char *buf
,
546 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
549 if (!capable(CAP_SYS_ADMIN
))
552 err
= kstrtouint(buf
, 10, &sdkp
->max_medium_access_timeouts
);
554 return err
? err
: count
;
556 static DEVICE_ATTR_RW(max_medium_access_timeouts
);
559 max_write_same_blocks_show(struct device
*dev
, struct device_attribute
*attr
,
562 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
564 return sprintf(buf
, "%u\n", sdkp
->max_ws_blocks
);
568 max_write_same_blocks_store(struct device
*dev
, struct device_attribute
*attr
,
569 const char *buf
, size_t count
)
571 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
572 struct scsi_device
*sdp
= sdkp
->device
;
573 struct queue_limits lim
;
577 if (!capable(CAP_SYS_ADMIN
))
580 if (sdp
->type
!= TYPE_DISK
&& sdp
->type
!= TYPE_ZBC
)
583 err
= kstrtoul(buf
, 10, &max
);
589 sdp
->no_write_same
= 1;
590 else if (max
<= SD_MAX_WS16_BLOCKS
) {
591 sdp
->no_write_same
= 0;
592 sdkp
->max_ws_blocks
= max
;
595 lim
= queue_limits_start_update(sdkp
->disk
->queue
);
596 sd_config_write_same(sdkp
, &lim
);
597 blk_mq_freeze_queue(sdkp
->disk
->queue
);
598 err
= queue_limits_commit_update(sdkp
->disk
->queue
, &lim
);
599 blk_mq_unfreeze_queue(sdkp
->disk
->queue
);
604 static DEVICE_ATTR_RW(max_write_same_blocks
);
607 zoned_cap_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
609 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
611 if (sdkp
->device
->type
== TYPE_ZBC
)
612 return sprintf(buf
, "host-managed\n");
613 if (sdkp
->zoned
== 1)
614 return sprintf(buf
, "host-aware\n");
615 if (sdkp
->zoned
== 2)
616 return sprintf(buf
, "drive-managed\n");
617 return sprintf(buf
, "none\n");
619 static DEVICE_ATTR_RO(zoned_cap
);
622 max_retries_store(struct device
*dev
, struct device_attribute
*attr
,
623 const char *buf
, size_t count
)
625 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
626 struct scsi_device
*sdev
= sdkp
->device
;
629 err
= kstrtoint(buf
, 10, &retries
);
633 if (retries
== SCSI_CMD_RETRIES_NO_LIMIT
|| retries
<= SD_MAX_RETRIES
) {
634 sdkp
->max_retries
= retries
;
638 sdev_printk(KERN_ERR
, sdev
, "max_retries must be between -1 and %d\n",
644 max_retries_show(struct device
*dev
, struct device_attribute
*attr
,
647 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
649 return sprintf(buf
, "%d\n", sdkp
->max_retries
);
652 static DEVICE_ATTR_RW(max_retries
);
654 static struct attribute
*sd_disk_attrs
[] = {
655 &dev_attr_cache_type
.attr
,
657 &dev_attr_allow_restart
.attr
,
658 &dev_attr_manage_start_stop
.attr
,
659 &dev_attr_manage_system_start_stop
.attr
,
660 &dev_attr_manage_runtime_start_stop
.attr
,
661 &dev_attr_manage_shutdown
.attr
,
662 &dev_attr_protection_type
.attr
,
663 &dev_attr_protection_mode
.attr
,
664 &dev_attr_app_tag_own
.attr
,
665 &dev_attr_thin_provisioning
.attr
,
666 &dev_attr_provisioning_mode
.attr
,
667 &dev_attr_zeroing_mode
.attr
,
668 &dev_attr_max_write_same_blocks
.attr
,
669 &dev_attr_max_medium_access_timeouts
.attr
,
670 &dev_attr_zoned_cap
.attr
,
671 &dev_attr_max_retries
.attr
,
674 ATTRIBUTE_GROUPS(sd_disk
);
676 static struct class sd_disk_class
= {
678 .dev_release
= scsi_disk_release
,
679 .dev_groups
= sd_disk_groups
,
683 * Don't request a new module, as that could deadlock in multipath
686 static void sd_default_probe(dev_t devt
)
691 * Device no to disk mapping:
693 * major disc2 disc p1
694 * |............|.............|....|....| <- dev_t
697 * Inside a major, we have 16k disks, however mapped non-
698 * contiguously. The first 16 disks are for major0, the next
699 * ones with major1, ... Disk 256 is for major0 again, disk 272
701 * As we stay compatible with our numbering scheme, we can reuse
702 * the well-know SCSI majors 8, 65--71, 136--143.
704 static int sd_major(int major_idx
)
708 return SCSI_DISK0_MAJOR
;
710 return SCSI_DISK1_MAJOR
+ major_idx
- 1;
712 return SCSI_DISK8_MAJOR
+ major_idx
- 8;
715 return 0; /* shut up gcc */
719 #ifdef CONFIG_BLK_SED_OPAL
720 static int sd_sec_submit(void *data
, u16 spsp
, u8 secp
, void *buffer
,
721 size_t len
, bool send
)
723 struct scsi_disk
*sdkp
= data
;
724 struct scsi_device
*sdev
= sdkp
->device
;
726 const struct scsi_exec_args exec_args
= {
727 .req_flags
= BLK_MQ_REQ_PM
,
731 cdb
[0] = send
? SECURITY_PROTOCOL_OUT
: SECURITY_PROTOCOL_IN
;
733 put_unaligned_be16(spsp
, &cdb
[2]);
734 put_unaligned_be32(len
, &cdb
[6]);
736 ret
= scsi_execute_cmd(sdev
, cdb
, send
? REQ_OP_DRV_OUT
: REQ_OP_DRV_IN
,
737 buffer
, len
, SD_TIMEOUT
, sdkp
->max_retries
,
739 return ret
<= 0 ? ret
: -EIO
;
741 #endif /* CONFIG_BLK_SED_OPAL */
744 * Look up the DIX operation based on whether the command is read or
745 * write and whether dix and dif are enabled.
747 static unsigned int sd_prot_op(bool write
, bool dix
, bool dif
)
749 /* Lookup table: bit 2 (write), bit 1 (dix), bit 0 (dif) */
750 static const unsigned int ops
[] = { /* wrt dix dif */
751 SCSI_PROT_NORMAL
, /* 0 0 0 */
752 SCSI_PROT_READ_STRIP
, /* 0 0 1 */
753 SCSI_PROT_READ_INSERT
, /* 0 1 0 */
754 SCSI_PROT_READ_PASS
, /* 0 1 1 */
755 SCSI_PROT_NORMAL
, /* 1 0 0 */
756 SCSI_PROT_WRITE_INSERT
, /* 1 0 1 */
757 SCSI_PROT_WRITE_STRIP
, /* 1 1 0 */
758 SCSI_PROT_WRITE_PASS
, /* 1 1 1 */
761 return ops
[write
<< 2 | dix
<< 1 | dif
];
765 * Returns a mask of the protection flags that are valid for a given DIX
768 static unsigned int sd_prot_flag_mask(unsigned int prot_op
)
770 static const unsigned int flag_mask
[] = {
771 [SCSI_PROT_NORMAL
] = 0,
773 [SCSI_PROT_READ_STRIP
] = SCSI_PROT_TRANSFER_PI
|
774 SCSI_PROT_GUARD_CHECK
|
775 SCSI_PROT_REF_CHECK
|
776 SCSI_PROT_REF_INCREMENT
,
778 [SCSI_PROT_READ_INSERT
] = SCSI_PROT_REF_INCREMENT
|
779 SCSI_PROT_IP_CHECKSUM
,
781 [SCSI_PROT_READ_PASS
] = SCSI_PROT_TRANSFER_PI
|
782 SCSI_PROT_GUARD_CHECK
|
783 SCSI_PROT_REF_CHECK
|
784 SCSI_PROT_REF_INCREMENT
|
785 SCSI_PROT_IP_CHECKSUM
,
787 [SCSI_PROT_WRITE_INSERT
] = SCSI_PROT_TRANSFER_PI
|
788 SCSI_PROT_REF_INCREMENT
,
790 [SCSI_PROT_WRITE_STRIP
] = SCSI_PROT_GUARD_CHECK
|
791 SCSI_PROT_REF_CHECK
|
792 SCSI_PROT_REF_INCREMENT
|
793 SCSI_PROT_IP_CHECKSUM
,
795 [SCSI_PROT_WRITE_PASS
] = SCSI_PROT_TRANSFER_PI
|
796 SCSI_PROT_GUARD_CHECK
|
797 SCSI_PROT_REF_CHECK
|
798 SCSI_PROT_REF_INCREMENT
|
799 SCSI_PROT_IP_CHECKSUM
,
802 return flag_mask
[prot_op
];
805 static unsigned char sd_setup_protect_cmnd(struct scsi_cmnd
*scmd
,
806 unsigned int dix
, unsigned int dif
)
808 struct request
*rq
= scsi_cmd_to_rq(scmd
);
809 struct bio
*bio
= rq
->bio
;
810 unsigned int prot_op
= sd_prot_op(rq_data_dir(rq
), dix
, dif
);
811 unsigned int protect
= 0;
813 if (dix
) { /* DIX Type 0, 1, 2, 3 */
814 if (bio_integrity_flagged(bio
, BIP_IP_CHECKSUM
))
815 scmd
->prot_flags
|= SCSI_PROT_IP_CHECKSUM
;
817 if (bio_integrity_flagged(bio
, BIP_CTRL_NOCHECK
) == false)
818 scmd
->prot_flags
|= SCSI_PROT_GUARD_CHECK
;
821 if (dif
!= T10_PI_TYPE3_PROTECTION
) { /* DIX/DIF Type 0, 1, 2 */
822 scmd
->prot_flags
|= SCSI_PROT_REF_INCREMENT
;
824 if (bio_integrity_flagged(bio
, BIP_CTRL_NOCHECK
) == false)
825 scmd
->prot_flags
|= SCSI_PROT_REF_CHECK
;
828 if (dif
) { /* DIX/DIF Type 1, 2, 3 */
829 scmd
->prot_flags
|= SCSI_PROT_TRANSFER_PI
;
831 if (bio_integrity_flagged(bio
, BIP_DISK_NOCHECK
))
832 protect
= 3 << 5; /* Disable target PI checking */
834 protect
= 1 << 5; /* Enable target PI checking */
837 scsi_set_prot_op(scmd
, prot_op
);
838 scsi_set_prot_type(scmd
, dif
);
839 scmd
->prot_flags
&= sd_prot_flag_mask(prot_op
);
844 static void sd_disable_discard(struct scsi_disk
*sdkp
)
846 sdkp
->provisioning_mode
= SD_LBP_DISABLE
;
847 blk_queue_disable_discard(sdkp
->disk
->queue
);
850 static void sd_config_discard(struct scsi_disk
*sdkp
, struct queue_limits
*lim
,
853 unsigned int logical_block_size
= sdkp
->device
->sector_size
;
854 unsigned int max_blocks
= 0;
856 lim
->discard_alignment
= sdkp
->unmap_alignment
* logical_block_size
;
857 lim
->discard_granularity
= max(sdkp
->physical_block_size
,
858 sdkp
->unmap_granularity
* logical_block_size
);
859 sdkp
->provisioning_mode
= mode
;
868 max_blocks
= min_not_zero(sdkp
->max_unmap_blocks
,
869 (u32
)SD_MAX_WS16_BLOCKS
);
873 if (sdkp
->device
->unmap_limit_for_ws
)
874 max_blocks
= sdkp
->max_unmap_blocks
;
876 max_blocks
= sdkp
->max_ws_blocks
;
878 max_blocks
= min_not_zero(max_blocks
, (u32
)SD_MAX_WS16_BLOCKS
);
882 if (sdkp
->device
->unmap_limit_for_ws
)
883 max_blocks
= sdkp
->max_unmap_blocks
;
885 max_blocks
= sdkp
->max_ws_blocks
;
887 max_blocks
= min_not_zero(max_blocks
, (u32
)SD_MAX_WS10_BLOCKS
);
891 max_blocks
= min_not_zero(sdkp
->max_ws_blocks
,
892 (u32
)SD_MAX_WS10_BLOCKS
);
896 lim
->max_hw_discard_sectors
= max_blocks
*
897 (logical_block_size
>> SECTOR_SHIFT
);
900 static void *sd_set_special_bvec(struct request
*rq
, unsigned int data_len
)
904 page
= mempool_alloc(sd_page_pool
, GFP_ATOMIC
);
907 clear_highpage(page
);
908 bvec_set_page(&rq
->special_vec
, page
, data_len
, 0);
909 rq
->rq_flags
|= RQF_SPECIAL_PAYLOAD
;
910 return bvec_virt(&rq
->special_vec
);
913 static blk_status_t
sd_setup_unmap_cmnd(struct scsi_cmnd
*cmd
)
915 struct scsi_device
*sdp
= cmd
->device
;
916 struct request
*rq
= scsi_cmd_to_rq(cmd
);
917 struct scsi_disk
*sdkp
= scsi_disk(rq
->q
->disk
);
918 u64 lba
= sectors_to_logical(sdp
, blk_rq_pos(rq
));
919 u32 nr_blocks
= sectors_to_logical(sdp
, blk_rq_sectors(rq
));
920 unsigned int data_len
= 24;
923 buf
= sd_set_special_bvec(rq
, data_len
);
925 return BLK_STS_RESOURCE
;
928 cmd
->cmnd
[0] = UNMAP
;
931 put_unaligned_be16(6 + 16, &buf
[0]);
932 put_unaligned_be16(16, &buf
[2]);
933 put_unaligned_be64(lba
, &buf
[8]);
934 put_unaligned_be32(nr_blocks
, &buf
[16]);
936 cmd
->allowed
= sdkp
->max_retries
;
937 cmd
->transfersize
= data_len
;
938 rq
->timeout
= SD_TIMEOUT
;
940 return scsi_alloc_sgtables(cmd
);
943 static void sd_config_atomic(struct scsi_disk
*sdkp
, struct queue_limits
*lim
)
945 unsigned int logical_block_size
= sdkp
->device
->sector_size
,
946 physical_block_size_sectors
, max_atomic
, unit_min
, unit_max
;
948 if ((!sdkp
->max_atomic
&& !sdkp
->max_atomic_with_boundary
) ||
949 sdkp
->protection_type
== T10_PI_TYPE2_PROTECTION
)
952 physical_block_size_sectors
= sdkp
->physical_block_size
/
953 sdkp
->device
->sector_size
;
955 unit_min
= rounddown_pow_of_two(sdkp
->atomic_granularity
?
956 sdkp
->atomic_granularity
:
957 physical_block_size_sectors
);
960 * Only use atomic boundary when we have the odd scenario of
961 * sdkp->max_atomic == 0, which the spec does permit.
963 if (sdkp
->max_atomic
) {
964 max_atomic
= sdkp
->max_atomic
;
965 unit_max
= rounddown_pow_of_two(sdkp
->max_atomic
);
966 sdkp
->use_atomic_write_boundary
= 0;
968 max_atomic
= sdkp
->max_atomic_with_boundary
;
969 unit_max
= rounddown_pow_of_two(sdkp
->max_atomic_boundary
);
970 sdkp
->use_atomic_write_boundary
= 1;
974 * Ensure compliance with granularity and alignment. For now, keep it
975 * simple and just don't support atomic writes for values mismatched
976 * with max_{boundary}atomic, physical block size, and
977 * atomic_granularity itself.
979 * We're really being distrustful by checking unit_max also...
981 if (sdkp
->atomic_granularity
> 1) {
982 if (unit_min
> 1 && unit_min
% sdkp
->atomic_granularity
)
984 if (unit_max
> 1 && unit_max
% sdkp
->atomic_granularity
)
988 if (sdkp
->atomic_alignment
> 1) {
989 if (unit_min
> 1 && unit_min
% sdkp
->atomic_alignment
)
991 if (unit_max
> 1 && unit_max
% sdkp
->atomic_alignment
)
995 lim
->atomic_write_hw_max
= max_atomic
* logical_block_size
;
996 lim
->atomic_write_hw_boundary
= 0;
997 lim
->atomic_write_hw_unit_min
= unit_min
* logical_block_size
;
998 lim
->atomic_write_hw_unit_max
= unit_max
* logical_block_size
;
1001 static blk_status_t
sd_setup_write_same16_cmnd(struct scsi_cmnd
*cmd
,
1004 struct scsi_device
*sdp
= cmd
->device
;
1005 struct request
*rq
= scsi_cmd_to_rq(cmd
);
1006 struct scsi_disk
*sdkp
= scsi_disk(rq
->q
->disk
);
1007 u64 lba
= sectors_to_logical(sdp
, blk_rq_pos(rq
));
1008 u32 nr_blocks
= sectors_to_logical(sdp
, blk_rq_sectors(rq
));
1009 u32 data_len
= sdp
->sector_size
;
1011 if (!sd_set_special_bvec(rq
, data_len
))
1012 return BLK_STS_RESOURCE
;
1015 cmd
->cmnd
[0] = WRITE_SAME_16
;
1017 cmd
->cmnd
[1] = 0x8; /* UNMAP */
1018 put_unaligned_be64(lba
, &cmd
->cmnd
[2]);
1019 put_unaligned_be32(nr_blocks
, &cmd
->cmnd
[10]);
1021 cmd
->allowed
= sdkp
->max_retries
;
1022 cmd
->transfersize
= data_len
;
1023 rq
->timeout
= unmap
? SD_TIMEOUT
: SD_WRITE_SAME_TIMEOUT
;
1025 return scsi_alloc_sgtables(cmd
);
1028 static blk_status_t
sd_setup_write_same10_cmnd(struct scsi_cmnd
*cmd
,
1031 struct scsi_device
*sdp
= cmd
->device
;
1032 struct request
*rq
= scsi_cmd_to_rq(cmd
);
1033 struct scsi_disk
*sdkp
= scsi_disk(rq
->q
->disk
);
1034 u64 lba
= sectors_to_logical(sdp
, blk_rq_pos(rq
));
1035 u32 nr_blocks
= sectors_to_logical(sdp
, blk_rq_sectors(rq
));
1036 u32 data_len
= sdp
->sector_size
;
1038 if (!sd_set_special_bvec(rq
, data_len
))
1039 return BLK_STS_RESOURCE
;
1042 cmd
->cmnd
[0] = WRITE_SAME
;
1044 cmd
->cmnd
[1] = 0x8; /* UNMAP */
1045 put_unaligned_be32(lba
, &cmd
->cmnd
[2]);
1046 put_unaligned_be16(nr_blocks
, &cmd
->cmnd
[7]);
1048 cmd
->allowed
= sdkp
->max_retries
;
1049 cmd
->transfersize
= data_len
;
1050 rq
->timeout
= unmap
? SD_TIMEOUT
: SD_WRITE_SAME_TIMEOUT
;
1052 return scsi_alloc_sgtables(cmd
);
1055 static blk_status_t
sd_setup_write_zeroes_cmnd(struct scsi_cmnd
*cmd
)
1057 struct request
*rq
= scsi_cmd_to_rq(cmd
);
1058 struct scsi_device
*sdp
= cmd
->device
;
1059 struct scsi_disk
*sdkp
= scsi_disk(rq
->q
->disk
);
1060 u64 lba
= sectors_to_logical(sdp
, blk_rq_pos(rq
));
1061 u32 nr_blocks
= sectors_to_logical(sdp
, blk_rq_sectors(rq
));
1063 if (!(rq
->cmd_flags
& REQ_NOUNMAP
)) {
1064 switch (sdkp
->zeroing_mode
) {
1065 case SD_ZERO_WS16_UNMAP
:
1066 return sd_setup_write_same16_cmnd(cmd
, true);
1067 case SD_ZERO_WS10_UNMAP
:
1068 return sd_setup_write_same10_cmnd(cmd
, true);
1072 if (sdp
->no_write_same
) {
1073 rq
->rq_flags
|= RQF_QUIET
;
1074 return BLK_STS_TARGET
;
1077 if (sdkp
->ws16
|| lba
> 0xffffffff || nr_blocks
> 0xffff)
1078 return sd_setup_write_same16_cmnd(cmd
, false);
1080 return sd_setup_write_same10_cmnd(cmd
, false);
1083 static void sd_disable_write_same(struct scsi_disk
*sdkp
)
1085 sdkp
->device
->no_write_same
= 1;
1086 sdkp
->max_ws_blocks
= 0;
1087 blk_queue_disable_write_zeroes(sdkp
->disk
->queue
);
1090 static void sd_config_write_same(struct scsi_disk
*sdkp
,
1091 struct queue_limits
*lim
)
1093 unsigned int logical_block_size
= sdkp
->device
->sector_size
;
1095 if (sdkp
->device
->no_write_same
) {
1096 sdkp
->max_ws_blocks
= 0;
1100 /* Some devices can not handle block counts above 0xffff despite
1101 * supporting WRITE SAME(16). Consequently we default to 64k
1102 * blocks per I/O unless the device explicitly advertises a
1105 if (sdkp
->max_ws_blocks
> SD_MAX_WS10_BLOCKS
)
1106 sdkp
->max_ws_blocks
= min_not_zero(sdkp
->max_ws_blocks
,
1107 (u32
)SD_MAX_WS16_BLOCKS
);
1108 else if (sdkp
->ws16
|| sdkp
->ws10
|| sdkp
->device
->no_report_opcodes
)
1109 sdkp
->max_ws_blocks
= min_not_zero(sdkp
->max_ws_blocks
,
1110 (u32
)SD_MAX_WS10_BLOCKS
);
1112 sdkp
->device
->no_write_same
= 1;
1113 sdkp
->max_ws_blocks
= 0;
1116 if (sdkp
->lbprz
&& sdkp
->lbpws
)
1117 sdkp
->zeroing_mode
= SD_ZERO_WS16_UNMAP
;
1118 else if (sdkp
->lbprz
&& sdkp
->lbpws10
)
1119 sdkp
->zeroing_mode
= SD_ZERO_WS10_UNMAP
;
1120 else if (sdkp
->max_ws_blocks
)
1121 sdkp
->zeroing_mode
= SD_ZERO_WS
;
1123 sdkp
->zeroing_mode
= SD_ZERO_WRITE
;
1125 if (sdkp
->max_ws_blocks
&&
1126 sdkp
->physical_block_size
> logical_block_size
) {
1128 * Reporting a maximum number of blocks that is not aligned
1129 * on the device physical size would cause a large write same
1130 * request to be split into physically unaligned chunks by
1131 * __blkdev_issue_write_zeroes() even if the caller of this
1132 * functions took care to align the large request. So make sure
1133 * the maximum reported is aligned to the device physical block
1134 * size. This is only an optional optimization for regular
1135 * disks, but this is mandatory to avoid failure of large write
1136 * same requests directed at sequential write required zones of
1137 * host-managed ZBC disks.
1139 sdkp
->max_ws_blocks
=
1140 round_down(sdkp
->max_ws_blocks
,
1141 bytes_to_logical(sdkp
->device
,
1142 sdkp
->physical_block_size
));
1146 lim
->max_write_zeroes_sectors
=
1147 sdkp
->max_ws_blocks
* (logical_block_size
>> SECTOR_SHIFT
);
1150 static blk_status_t
sd_setup_flush_cmnd(struct scsi_cmnd
*cmd
)
1152 struct request
*rq
= scsi_cmd_to_rq(cmd
);
1153 struct scsi_disk
*sdkp
= scsi_disk(rq
->q
->disk
);
1155 /* flush requests don't perform I/O, zero the S/G table */
1156 memset(&cmd
->sdb
, 0, sizeof(cmd
->sdb
));
1158 if (cmd
->device
->use_16_for_sync
) {
1159 cmd
->cmnd
[0] = SYNCHRONIZE_CACHE_16
;
1162 cmd
->cmnd
[0] = SYNCHRONIZE_CACHE
;
1165 cmd
->transfersize
= 0;
1166 cmd
->allowed
= sdkp
->max_retries
;
1168 rq
->timeout
= rq
->q
->rq_timeout
* SD_FLUSH_TIMEOUT_MULTIPLIER
;
1173 * sd_group_number() - Compute the GROUP NUMBER field
1174 * @cmd: SCSI command for which to compute the value of the six-bit GROUP NUMBER
1177 * From SBC-5 r05 (https://www.t10.org/cgi-bin/ac.pl?t=f&f=sbc5r05.pdf):
1178 * 0: no relative lifetime.
1179 * 1: shortest relative lifetime.
1180 * 2: second shortest relative lifetime.
1181 * 3 - 0x3d: intermediate relative lifetimes.
1182 * 0x3e: second longest relative lifetime.
1183 * 0x3f: longest relative lifetime.
1185 static u8
sd_group_number(struct scsi_cmnd
*cmd
)
1187 const struct request
*rq
= scsi_cmd_to_rq(cmd
);
1188 struct scsi_disk
*sdkp
= scsi_disk(rq
->q
->disk
);
1193 return min3((u32
)rq
->write_hint
, (u32
)sdkp
->permanent_stream_count
,
1197 static blk_status_t
sd_setup_rw32_cmnd(struct scsi_cmnd
*cmd
, bool write
,
1198 sector_t lba
, unsigned int nr_blocks
,
1199 unsigned char flags
, unsigned int dld
)
1201 cmd
->cmd_len
= SD_EXT_CDB_SIZE
;
1202 cmd
->cmnd
[0] = VARIABLE_LENGTH_CMD
;
1203 cmd
->cmnd
[6] = sd_group_number(cmd
);
1204 cmd
->cmnd
[7] = 0x18; /* Additional CDB len */
1205 cmd
->cmnd
[9] = write
? WRITE_32
: READ_32
;
1206 cmd
->cmnd
[10] = flags
;
1207 cmd
->cmnd
[11] = dld
& 0x07;
1208 put_unaligned_be64(lba
, &cmd
->cmnd
[12]);
1209 put_unaligned_be32(lba
, &cmd
->cmnd
[20]); /* Expected Indirect LBA */
1210 put_unaligned_be32(nr_blocks
, &cmd
->cmnd
[28]);
1215 static blk_status_t
sd_setup_rw16_cmnd(struct scsi_cmnd
*cmd
, bool write
,
1216 sector_t lba
, unsigned int nr_blocks
,
1217 unsigned char flags
, unsigned int dld
)
1220 cmd
->cmnd
[0] = write
? WRITE_16
: READ_16
;
1221 cmd
->cmnd
[1] = flags
| ((dld
>> 2) & 0x01);
1222 cmd
->cmnd
[14] = ((dld
& 0x03) << 6) | sd_group_number(cmd
);
1224 put_unaligned_be64(lba
, &cmd
->cmnd
[2]);
1225 put_unaligned_be32(nr_blocks
, &cmd
->cmnd
[10]);
1230 static blk_status_t
sd_setup_rw10_cmnd(struct scsi_cmnd
*cmd
, bool write
,
1231 sector_t lba
, unsigned int nr_blocks
,
1232 unsigned char flags
)
1235 cmd
->cmnd
[0] = write
? WRITE_10
: READ_10
;
1236 cmd
->cmnd
[1] = flags
;
1237 cmd
->cmnd
[6] = sd_group_number(cmd
);
1239 put_unaligned_be32(lba
, &cmd
->cmnd
[2]);
1240 put_unaligned_be16(nr_blocks
, &cmd
->cmnd
[7]);
1245 static blk_status_t
sd_setup_rw6_cmnd(struct scsi_cmnd
*cmd
, bool write
,
1246 sector_t lba
, unsigned int nr_blocks
,
1247 unsigned char flags
)
1249 /* Avoid that 0 blocks gets translated into 256 blocks. */
1250 if (WARN_ON_ONCE(nr_blocks
== 0))
1251 return BLK_STS_IOERR
;
1253 if (unlikely(flags
& 0x8)) {
1255 * This happens only if this drive failed 10byte rw
1256 * command with ILLEGAL_REQUEST during operation and
1257 * thus turned off use_10_for_rw.
1259 scmd_printk(KERN_ERR
, cmd
, "FUA write on READ/WRITE(6) drive\n");
1260 return BLK_STS_IOERR
;
1264 cmd
->cmnd
[0] = write
? WRITE_6
: READ_6
;
1265 cmd
->cmnd
[1] = (lba
>> 16) & 0x1f;
1266 cmd
->cmnd
[2] = (lba
>> 8) & 0xff;
1267 cmd
->cmnd
[3] = lba
& 0xff;
1268 cmd
->cmnd
[4] = nr_blocks
;
1275 * Check if a command has a duration limit set. If it does, and the target
1276 * device supports CDL and the feature is enabled, return the limit
1277 * descriptor index to use. Return 0 (no limit) otherwise.
1279 static int sd_cdl_dld(struct scsi_disk
*sdkp
, struct scsi_cmnd
*scmd
)
1281 struct scsi_device
*sdp
= sdkp
->device
;
1284 if (!sdp
->cdl_supported
|| !sdp
->cdl_enable
)
1288 * Use "no limit" if the request ioprio does not specify a duration
1291 hint
= IOPRIO_PRIO_HINT(req_get_ioprio(scsi_cmd_to_rq(scmd
)));
1292 if (hint
< IOPRIO_HINT_DEV_DURATION_LIMIT_1
||
1293 hint
> IOPRIO_HINT_DEV_DURATION_LIMIT_7
)
1296 return (hint
- IOPRIO_HINT_DEV_DURATION_LIMIT_1
) + 1;
1299 static blk_status_t
sd_setup_atomic_cmnd(struct scsi_cmnd
*cmd
,
1300 sector_t lba
, unsigned int nr_blocks
,
1301 bool boundary
, unsigned char flags
)
1304 cmd
->cmnd
[0] = WRITE_ATOMIC_16
;
1305 cmd
->cmnd
[1] = flags
;
1306 put_unaligned_be64(lba
, &cmd
->cmnd
[2]);
1307 put_unaligned_be16(nr_blocks
, &cmd
->cmnd
[12]);
1309 put_unaligned_be16(nr_blocks
, &cmd
->cmnd
[10]);
1311 put_unaligned_be16(0, &cmd
->cmnd
[10]);
1312 put_unaligned_be16(nr_blocks
, &cmd
->cmnd
[12]);
1319 static blk_status_t
sd_setup_read_write_cmnd(struct scsi_cmnd
*cmd
)
1321 struct request
*rq
= scsi_cmd_to_rq(cmd
);
1322 struct scsi_device
*sdp
= cmd
->device
;
1323 struct scsi_disk
*sdkp
= scsi_disk(rq
->q
->disk
);
1324 sector_t lba
= sectors_to_logical(sdp
, blk_rq_pos(rq
));
1326 unsigned int nr_blocks
= sectors_to_logical(sdp
, blk_rq_sectors(rq
));
1327 unsigned int mask
= logical_to_sectors(sdp
, 1) - 1;
1328 bool write
= rq_data_dir(rq
) == WRITE
;
1329 unsigned char protect
, fua
;
1335 ret
= scsi_alloc_sgtables(cmd
);
1336 if (ret
!= BLK_STS_OK
)
1339 ret
= BLK_STS_IOERR
;
1340 if (!scsi_device_online(sdp
) || sdp
->changed
) {
1341 scmd_printk(KERN_ERR
, cmd
, "device offline or changed\n");
1345 if (blk_rq_pos(rq
) + blk_rq_sectors(rq
) > get_capacity(rq
->q
->disk
)) {
1346 scmd_printk(KERN_ERR
, cmd
, "access beyond end of device\n");
1350 if ((blk_rq_pos(rq
) & mask
) || (blk_rq_sectors(rq
) & mask
)) {
1351 scmd_printk(KERN_ERR
, cmd
, "request not aligned to the logical block size\n");
1356 * Some SD card readers can't handle accesses which touch the
1357 * last one or two logical blocks. Split accesses as needed.
1359 threshold
= sdkp
->capacity
- SD_LAST_BUGGY_SECTORS
;
1361 if (unlikely(sdp
->last_sector_bug
&& lba
+ nr_blocks
> threshold
)) {
1362 if (lba
< threshold
) {
1363 /* Access up to the threshold but not beyond */
1364 nr_blocks
= threshold
- lba
;
1366 /* Access only a single logical block */
1371 fua
= rq
->cmd_flags
& REQ_FUA
? 0x8 : 0;
1372 dix
= scsi_prot_sg_count(cmd
);
1373 dif
= scsi_host_dif_capable(cmd
->device
->host
, sdkp
->protection_type
);
1374 dld
= sd_cdl_dld(sdkp
, cmd
);
1377 protect
= sd_setup_protect_cmnd(cmd
, dix
, dif
);
1381 if (protect
&& sdkp
->protection_type
== T10_PI_TYPE2_PROTECTION
) {
1382 ret
= sd_setup_rw32_cmnd(cmd
, write
, lba
, nr_blocks
,
1383 protect
| fua
, dld
);
1384 } else if (rq
->cmd_flags
& REQ_ATOMIC
) {
1385 ret
= sd_setup_atomic_cmnd(cmd
, lba
, nr_blocks
,
1386 sdkp
->use_atomic_write_boundary
,
1388 } else if (sdp
->use_16_for_rw
|| (nr_blocks
> 0xffff)) {
1389 ret
= sd_setup_rw16_cmnd(cmd
, write
, lba
, nr_blocks
,
1390 protect
| fua
, dld
);
1391 } else if ((nr_blocks
> 0xff) || (lba
> 0x1fffff) ||
1392 sdp
->use_10_for_rw
|| protect
|| rq
->write_hint
) {
1393 ret
= sd_setup_rw10_cmnd(cmd
, write
, lba
, nr_blocks
,
1396 ret
= sd_setup_rw6_cmnd(cmd
, write
, lba
, nr_blocks
,
1400 if (unlikely(ret
!= BLK_STS_OK
))
1404 * We shouldn't disconnect in the middle of a sector, so with a dumb
1405 * host adapter, it's safe to assume that we can at least transfer
1406 * this many bytes between each connect / disconnect.
1408 cmd
->transfersize
= sdp
->sector_size
;
1409 cmd
->underflow
= nr_blocks
<< 9;
1410 cmd
->allowed
= sdkp
->max_retries
;
1411 cmd
->sdb
.length
= nr_blocks
* sdp
->sector_size
;
1414 scmd_printk(KERN_INFO
, cmd
,
1415 "%s: block=%llu, count=%d\n", __func__
,
1416 (unsigned long long)blk_rq_pos(rq
),
1417 blk_rq_sectors(rq
)));
1419 scmd_printk(KERN_INFO
, cmd
,
1420 "%s %d/%u 512 byte blocks.\n",
1421 write
? "writing" : "reading", nr_blocks
,
1422 blk_rq_sectors(rq
)));
1425 * This indicates that the command is ready from our end to be queued.
1429 scsi_free_sgtables(cmd
);
1433 static blk_status_t
sd_init_command(struct scsi_cmnd
*cmd
)
1435 struct request
*rq
= scsi_cmd_to_rq(cmd
);
1437 switch (req_op(rq
)) {
1438 case REQ_OP_DISCARD
:
1439 switch (scsi_disk(rq
->q
->disk
)->provisioning_mode
) {
1441 return sd_setup_unmap_cmnd(cmd
);
1443 return sd_setup_write_same16_cmnd(cmd
, true);
1445 return sd_setup_write_same10_cmnd(cmd
, true);
1447 return sd_setup_write_same10_cmnd(cmd
, false);
1449 return BLK_STS_TARGET
;
1451 case REQ_OP_WRITE_ZEROES
:
1452 return sd_setup_write_zeroes_cmnd(cmd
);
1454 return sd_setup_flush_cmnd(cmd
);
1457 return sd_setup_read_write_cmnd(cmd
);
1458 case REQ_OP_ZONE_RESET
:
1459 return sd_zbc_setup_zone_mgmt_cmnd(cmd
, ZO_RESET_WRITE_POINTER
,
1461 case REQ_OP_ZONE_RESET_ALL
:
1462 return sd_zbc_setup_zone_mgmt_cmnd(cmd
, ZO_RESET_WRITE_POINTER
,
1464 case REQ_OP_ZONE_OPEN
:
1465 return sd_zbc_setup_zone_mgmt_cmnd(cmd
, ZO_OPEN_ZONE
, false);
1466 case REQ_OP_ZONE_CLOSE
:
1467 return sd_zbc_setup_zone_mgmt_cmnd(cmd
, ZO_CLOSE_ZONE
, false);
1468 case REQ_OP_ZONE_FINISH
:
1469 return sd_zbc_setup_zone_mgmt_cmnd(cmd
, ZO_FINISH_ZONE
, false);
1472 return BLK_STS_NOTSUPP
;
1476 static void sd_uninit_command(struct scsi_cmnd
*SCpnt
)
1478 struct request
*rq
= scsi_cmd_to_rq(SCpnt
);
1480 if (rq
->rq_flags
& RQF_SPECIAL_PAYLOAD
)
1481 mempool_free(rq
->special_vec
.bv_page
, sd_page_pool
);
1484 static bool sd_need_revalidate(struct gendisk
*disk
, struct scsi_disk
*sdkp
)
1486 if (sdkp
->device
->removable
|| sdkp
->write_prot
) {
1487 if (disk_check_media_change(disk
))
1492 * Force a full rescan after ioctl(BLKRRPART). While the disk state has
1493 * nothing to do with partitions, BLKRRPART is used to force a full
1494 * revalidate after things like a format for historical reasons.
1496 return test_bit(GD_NEED_PART_SCAN
, &disk
->state
);
1500 * sd_open - open a scsi disk device
1501 * @disk: disk to open
1504 * Returns 0 if successful. Returns a negated errno value in case
1507 * Note: This can be called from a user context (e.g. fsck(1) )
1508 * or from within the kernel (e.g. as a result of a mount(1) ).
1509 * In the latter case @inode and @filp carry an abridged amount
1510 * of information as noted above.
1512 * Locking: called with disk->open_mutex held.
1514 static int sd_open(struct gendisk
*disk
, blk_mode_t mode
)
1516 struct scsi_disk
*sdkp
= scsi_disk(disk
);
1517 struct scsi_device
*sdev
= sdkp
->device
;
1520 if (scsi_device_get(sdev
))
1523 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO
, sdkp
, "sd_open\n"));
1526 * If the device is in error recovery, wait until it is done.
1527 * If the device is offline, then disallow any access to it.
1530 if (!scsi_block_when_processing_errors(sdev
))
1533 if (sd_need_revalidate(disk
, sdkp
))
1534 sd_revalidate_disk(disk
);
1537 * If the drive is empty, just let the open fail.
1539 retval
= -ENOMEDIUM
;
1540 if (sdev
->removable
&& !sdkp
->media_present
&&
1541 !(mode
& BLK_OPEN_NDELAY
))
1545 * If the device has the write protect tab set, have the open fail
1546 * if the user expects to be able to write to the thing.
1549 if (sdkp
->write_prot
&& (mode
& BLK_OPEN_WRITE
))
1553 * It is possible that the disk changing stuff resulted in
1554 * the device being taken offline. If this is the case,
1555 * report this to the user, and don't pretend that the
1556 * open actually succeeded.
1559 if (!scsi_device_online(sdev
))
1562 if ((atomic_inc_return(&sdkp
->openers
) == 1) && sdev
->removable
) {
1563 if (scsi_block_when_processing_errors(sdev
))
1564 scsi_set_medium_removal(sdev
, SCSI_REMOVAL_PREVENT
);
1570 scsi_device_put(sdev
);
1575 * sd_release - invoked when the (last) close(2) is called on this
1577 * @disk: disk to release
1581 * Note: may block (uninterruptible) if error recovery is underway
1584 * Locking: called with disk->open_mutex held.
1586 static void sd_release(struct gendisk
*disk
)
1588 struct scsi_disk
*sdkp
= scsi_disk(disk
);
1589 struct scsi_device
*sdev
= sdkp
->device
;
1591 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO
, sdkp
, "sd_release\n"));
1593 if (atomic_dec_return(&sdkp
->openers
) == 0 && sdev
->removable
) {
1594 if (scsi_block_when_processing_errors(sdev
))
1595 scsi_set_medium_removal(sdev
, SCSI_REMOVAL_ALLOW
);
1598 scsi_device_put(sdev
);
1601 static int sd_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1603 struct scsi_disk
*sdkp
= scsi_disk(bdev
->bd_disk
);
1604 struct scsi_device
*sdp
= sdkp
->device
;
1605 struct Scsi_Host
*host
= sdp
->host
;
1606 sector_t capacity
= logical_to_sectors(sdp
, sdkp
->capacity
);
1609 /* default to most commonly used values */
1610 diskinfo
[0] = 0x40; /* 1 << 6 */
1611 diskinfo
[1] = 0x20; /* 1 << 5 */
1612 diskinfo
[2] = capacity
>> 11;
1614 /* override with calculated, extended default, or driver values */
1615 if (host
->hostt
->bios_param
)
1616 host
->hostt
->bios_param(sdp
, bdev
, capacity
, diskinfo
);
1618 scsicam_bios_param(bdev
, capacity
, diskinfo
);
1620 geo
->heads
= diskinfo
[0];
1621 geo
->sectors
= diskinfo
[1];
1622 geo
->cylinders
= diskinfo
[2];
1627 * sd_ioctl - process an ioctl
1628 * @bdev: target block device
1630 * @cmd: ioctl command number
1631 * @arg: this is third argument given to ioctl(2) system call.
1632 * Often contains a pointer.
1634 * Returns 0 if successful (some ioctls return positive numbers on
1635 * success as well). Returns a negated errno value in case of error.
1637 * Note: most ioctls are forward onto the block subsystem or further
1638 * down in the scsi subsystem.
1640 static int sd_ioctl(struct block_device
*bdev
, blk_mode_t mode
,
1641 unsigned int cmd
, unsigned long arg
)
1643 struct gendisk
*disk
= bdev
->bd_disk
;
1644 struct scsi_disk
*sdkp
= scsi_disk(disk
);
1645 struct scsi_device
*sdp
= sdkp
->device
;
1646 void __user
*p
= (void __user
*)arg
;
1649 SCSI_LOG_IOCTL(1, sd_printk(KERN_INFO
, sdkp
, "sd_ioctl: disk=%s, "
1650 "cmd=0x%x\n", disk
->disk_name
, cmd
));
1652 if (bdev_is_partition(bdev
) && !capable(CAP_SYS_RAWIO
))
1653 return -ENOIOCTLCMD
;
1656 * If we are in the middle of error recovery, don't let anyone
1657 * else try and use this device. Also, if error recovery fails, it
1658 * may try and take the device offline, in which case all further
1659 * access to the device is prohibited.
1661 error
= scsi_ioctl_block_when_processing_errors(sdp
, cmd
,
1662 (mode
& BLK_OPEN_NDELAY
));
1666 if (is_sed_ioctl(cmd
))
1667 return sed_ioctl(sdkp
->opal_dev
, cmd
, p
);
1668 return scsi_ioctl(sdp
, mode
& BLK_OPEN_WRITE
, cmd
, p
);
1671 static void set_media_not_present(struct scsi_disk
*sdkp
)
1673 if (sdkp
->media_present
)
1674 sdkp
->device
->changed
= 1;
1676 if (sdkp
->device
->removable
) {
1677 sdkp
->media_present
= 0;
1682 static int media_not_present(struct scsi_disk
*sdkp
,
1683 struct scsi_sense_hdr
*sshdr
)
1685 if (!scsi_sense_valid(sshdr
))
1688 /* not invoked for commands that could return deferred errors */
1689 switch (sshdr
->sense_key
) {
1690 case UNIT_ATTENTION
:
1692 /* medium not present */
1693 if (sshdr
->asc
== 0x3A) {
1694 set_media_not_present(sdkp
);
1702 * sd_check_events - check media events
1703 * @disk: kernel device descriptor
1704 * @clearing: disk events currently being cleared
1706 * Returns mask of DISK_EVENT_*.
1708 * Note: this function is invoked from the block subsystem.
1710 static unsigned int sd_check_events(struct gendisk
*disk
, unsigned int clearing
)
1712 struct scsi_disk
*sdkp
= disk
->private_data
;
1713 struct scsi_device
*sdp
;
1721 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO
, sdkp
, "sd_check_events\n"));
1724 * If the device is offline, don't send any commands - just pretend as
1725 * if the command failed. If the device ever comes back online, we
1726 * can deal with it then. It is only because of unrecoverable errors
1727 * that we would ever take a device offline in the first place.
1729 if (!scsi_device_online(sdp
)) {
1730 set_media_not_present(sdkp
);
1735 * Using TEST_UNIT_READY enables differentiation between drive with
1736 * no cartridge loaded - NOT READY, drive with changed cartridge -
1737 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
1739 * Drives that auto spin down. eg iomega jaz 1G, will be started
1740 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
1741 * sd_revalidate() is called.
1743 if (scsi_block_when_processing_errors(sdp
)) {
1744 struct scsi_sense_hdr sshdr
= { 0, };
1746 retval
= scsi_test_unit_ready(sdp
, SD_TIMEOUT
, sdkp
->max_retries
,
1749 /* failed to execute TUR, assume media not present */
1750 if (retval
< 0 || host_byte(retval
)) {
1751 set_media_not_present(sdkp
);
1755 if (media_not_present(sdkp
, &sshdr
))
1760 * For removable scsi disk we have to recognise the presence
1761 * of a disk in the drive.
1763 if (!sdkp
->media_present
)
1765 sdkp
->media_present
= 1;
1768 * sdp->changed is set under the following conditions:
1770 * Medium present state has changed in either direction.
1771 * Device has indicated UNIT_ATTENTION.
1773 disk_changed
= sdp
->changed
;
1775 return disk_changed
? DISK_EVENT_MEDIA_CHANGE
: 0;
1778 static int sd_sync_cache(struct scsi_disk
*sdkp
)
1781 struct scsi_device
*sdp
= sdkp
->device
;
1782 const int timeout
= sdp
->request_queue
->rq_timeout
1783 * SD_FLUSH_TIMEOUT_MULTIPLIER
;
1784 /* Leave the rest of the command zero to indicate flush everything. */
1785 const unsigned char cmd
[16] = { sdp
->use_16_for_sync
?
1786 SYNCHRONIZE_CACHE_16
: SYNCHRONIZE_CACHE
};
1787 struct scsi_sense_hdr sshdr
;
1788 struct scsi_failure failure_defs
[] = {
1791 .result
= SCMD_FAILURE_RESULT_ANY
,
1795 struct scsi_failures failures
= {
1796 .failure_definitions
= failure_defs
,
1798 const struct scsi_exec_args exec_args
= {
1799 .req_flags
= BLK_MQ_REQ_PM
,
1801 .failures
= &failures
,
1804 if (!scsi_device_online(sdp
))
1807 res
= scsi_execute_cmd(sdp
, cmd
, REQ_OP_DRV_IN
, NULL
, 0, timeout
,
1808 sdkp
->max_retries
, &exec_args
);
1810 sd_print_result(sdkp
, "Synchronize Cache(10) failed", res
);
1815 if (scsi_status_is_check_condition(res
) &&
1816 scsi_sense_valid(&sshdr
)) {
1817 sd_print_sense_hdr(sdkp
, &sshdr
);
1819 /* we need to evaluate the error return */
1820 if (sshdr
.asc
== 0x3a || /* medium not present */
1821 sshdr
.asc
== 0x20 || /* invalid command */
1822 (sshdr
.asc
== 0x74 && sshdr
.ascq
== 0x71)) /* drive is password locked */
1823 /* this is no error here */
1827 * If a format is in progress or if the drive does not
1828 * support sync, there is not much we can do because
1829 * this is called during shutdown or suspend so just
1830 * return success so those operations can proceed.
1832 if ((sshdr
.asc
== 0x04 && sshdr
.ascq
== 0x04) ||
1833 sshdr
.sense_key
== ILLEGAL_REQUEST
)
1837 switch (host_byte(res
)) {
1838 /* ignore errors due to racing a disconnection */
1839 case DID_BAD_TARGET
:
1840 case DID_NO_CONNECT
:
1842 /* signal the upper layer it might try again */
1846 case DID_SOFT_ERROR
:
1855 static void sd_rescan(struct device
*dev
)
1857 struct scsi_disk
*sdkp
= dev_get_drvdata(dev
);
1859 sd_revalidate_disk(sdkp
->disk
);
1862 static int sd_get_unique_id(struct gendisk
*disk
, u8 id
[16],
1863 enum blk_unique_id type
)
1865 struct scsi_device
*sdev
= scsi_disk(disk
)->device
;
1866 const struct scsi_vpd
*vpd
;
1867 const unsigned char *d
;
1868 int ret
= -ENXIO
, len
;
1871 vpd
= rcu_dereference(sdev
->vpd_pg83
);
1876 for (d
= vpd
->data
+ 4; d
< vpd
->data
+ vpd
->len
; d
+= d
[3] + 4) {
1877 /* we only care about designators with LU association */
1878 if (((d
[1] >> 4) & 0x3) != 0x00)
1880 if ((d
[1] & 0xf) != type
)
1884 * Only exit early if a 16-byte descriptor was found. Otherwise
1885 * keep looking as one with more entropy might still show up.
1888 if (len
!= 8 && len
!= 12 && len
!= 16)
1891 memcpy(id
, d
+ 4, len
);
1900 static int sd_scsi_to_pr_err(struct scsi_sense_hdr
*sshdr
, int result
)
1902 switch (host_byte(result
)) {
1903 case DID_TRANSPORT_MARGINAL
:
1904 case DID_TRANSPORT_DISRUPTED
:
1906 return PR_STS_RETRY_PATH_FAILURE
;
1907 case DID_NO_CONNECT
:
1908 return PR_STS_PATH_FAILED
;
1909 case DID_TRANSPORT_FAILFAST
:
1910 return PR_STS_PATH_FAST_FAILED
;
1913 switch (status_byte(result
)) {
1914 case SAM_STAT_RESERVATION_CONFLICT
:
1915 return PR_STS_RESERVATION_CONFLICT
;
1916 case SAM_STAT_CHECK_CONDITION
:
1917 if (!scsi_sense_valid(sshdr
))
1918 return PR_STS_IOERR
;
1920 if (sshdr
->sense_key
== ILLEGAL_REQUEST
&&
1921 (sshdr
->asc
== 0x26 || sshdr
->asc
== 0x24))
1926 return PR_STS_IOERR
;
1930 static int sd_pr_in_command(struct block_device
*bdev
, u8 sa
,
1931 unsigned char *data
, int data_len
)
1933 struct scsi_disk
*sdkp
= scsi_disk(bdev
->bd_disk
);
1934 struct scsi_device
*sdev
= sdkp
->device
;
1935 struct scsi_sense_hdr sshdr
;
1936 u8 cmd
[10] = { PERSISTENT_RESERVE_IN
, sa
};
1937 struct scsi_failure failure_defs
[] = {
1939 .sense
= UNIT_ATTENTION
,
1940 .asc
= SCMD_FAILURE_ASC_ANY
,
1941 .ascq
= SCMD_FAILURE_ASCQ_ANY
,
1943 .result
= SAM_STAT_CHECK_CONDITION
,
1947 struct scsi_failures failures
= {
1948 .failure_definitions
= failure_defs
,
1950 const struct scsi_exec_args exec_args
= {
1952 .failures
= &failures
,
1956 put_unaligned_be16(data_len
, &cmd
[7]);
1958 result
= scsi_execute_cmd(sdev
, cmd
, REQ_OP_DRV_IN
, data
, data_len
,
1959 SD_TIMEOUT
, sdkp
->max_retries
, &exec_args
);
1960 if (scsi_status_is_check_condition(result
) &&
1961 scsi_sense_valid(&sshdr
)) {
1962 sdev_printk(KERN_INFO
, sdev
, "PR command failed: %d\n", result
);
1963 scsi_print_sense_hdr(sdev
, NULL
, &sshdr
);
1969 return sd_scsi_to_pr_err(&sshdr
, result
);
1972 static int sd_pr_read_keys(struct block_device
*bdev
, struct pr_keys
*keys_info
)
1974 int result
, i
, data_offset
, num_copy_keys
;
1975 u32 num_keys
= keys_info
->num_keys
;
1976 int data_len
= num_keys
* 8 + 8;
1979 data
= kzalloc(data_len
, GFP_KERNEL
);
1983 result
= sd_pr_in_command(bdev
, READ_KEYS
, data
, data_len
);
1987 keys_info
->generation
= get_unaligned_be32(&data
[0]);
1988 keys_info
->num_keys
= get_unaligned_be32(&data
[4]) / 8;
1991 num_copy_keys
= min(num_keys
, keys_info
->num_keys
);
1993 for (i
= 0; i
< num_copy_keys
; i
++) {
1994 keys_info
->keys
[i
] = get_unaligned_be64(&data
[data_offset
]);
2003 static int sd_pr_read_reservation(struct block_device
*bdev
,
2004 struct pr_held_reservation
*rsv
)
2006 struct scsi_disk
*sdkp
= scsi_disk(bdev
->bd_disk
);
2007 struct scsi_device
*sdev
= sdkp
->device
;
2011 result
= sd_pr_in_command(bdev
, READ_RESERVATION
, data
, sizeof(data
));
2015 len
= get_unaligned_be32(&data
[4]);
2019 /* Make sure we have at least the key and type */
2021 sdev_printk(KERN_INFO
, sdev
,
2022 "READ RESERVATION failed due to short return buffer of %d bytes\n",
2027 rsv
->generation
= get_unaligned_be32(&data
[0]);
2028 rsv
->key
= get_unaligned_be64(&data
[8]);
2029 rsv
->type
= scsi_pr_type_to_block(data
[21] & 0x0f);
2033 static int sd_pr_out_command(struct block_device
*bdev
, u8 sa
, u64 key
,
2034 u64 sa_key
, enum scsi_pr_type type
, u8 flags
)
2036 struct scsi_disk
*sdkp
= scsi_disk(bdev
->bd_disk
);
2037 struct scsi_device
*sdev
= sdkp
->device
;
2038 struct scsi_sense_hdr sshdr
;
2039 struct scsi_failure failure_defs
[] = {
2041 .sense
= UNIT_ATTENTION
,
2042 .asc
= SCMD_FAILURE_ASC_ANY
,
2043 .ascq
= SCMD_FAILURE_ASCQ_ANY
,
2045 .result
= SAM_STAT_CHECK_CONDITION
,
2049 struct scsi_failures failures
= {
2050 .failure_definitions
= failure_defs
,
2052 const struct scsi_exec_args exec_args
= {
2054 .failures
= &failures
,
2057 u8 cmd
[16] = { 0, };
2058 u8 data
[24] = { 0, };
2060 cmd
[0] = PERSISTENT_RESERVE_OUT
;
2063 put_unaligned_be32(sizeof(data
), &cmd
[5]);
2065 put_unaligned_be64(key
, &data
[0]);
2066 put_unaligned_be64(sa_key
, &data
[8]);
2069 result
= scsi_execute_cmd(sdev
, cmd
, REQ_OP_DRV_OUT
, &data
,
2070 sizeof(data
), SD_TIMEOUT
, sdkp
->max_retries
,
2073 if (scsi_status_is_check_condition(result
) &&
2074 scsi_sense_valid(&sshdr
)) {
2075 sdev_printk(KERN_INFO
, sdev
, "PR command failed: %d\n", result
);
2076 scsi_print_sense_hdr(sdev
, NULL
, &sshdr
);
2082 return sd_scsi_to_pr_err(&sshdr
, result
);
2085 static int sd_pr_register(struct block_device
*bdev
, u64 old_key
, u64 new_key
,
2088 if (flags
& ~PR_FL_IGNORE_KEY
)
2090 return sd_pr_out_command(bdev
, (flags
& PR_FL_IGNORE_KEY
) ? 0x06 : 0x00,
2091 old_key
, new_key
, 0,
2092 (1 << 0) /* APTPL */);
2095 static int sd_pr_reserve(struct block_device
*bdev
, u64 key
, enum pr_type type
,
2100 return sd_pr_out_command(bdev
, 0x01, key
, 0,
2101 block_pr_type_to_scsi(type
), 0);
2104 static int sd_pr_release(struct block_device
*bdev
, u64 key
, enum pr_type type
)
2106 return sd_pr_out_command(bdev
, 0x02, key
, 0,
2107 block_pr_type_to_scsi(type
), 0);
2110 static int sd_pr_preempt(struct block_device
*bdev
, u64 old_key
, u64 new_key
,
2111 enum pr_type type
, bool abort
)
2113 return sd_pr_out_command(bdev
, abort
? 0x05 : 0x04, old_key
, new_key
,
2114 block_pr_type_to_scsi(type
), 0);
2117 static int sd_pr_clear(struct block_device
*bdev
, u64 key
)
2119 return sd_pr_out_command(bdev
, 0x03, key
, 0, 0, 0);
2122 static const struct pr_ops sd_pr_ops
= {
2123 .pr_register
= sd_pr_register
,
2124 .pr_reserve
= sd_pr_reserve
,
2125 .pr_release
= sd_pr_release
,
2126 .pr_preempt
= sd_pr_preempt
,
2127 .pr_clear
= sd_pr_clear
,
2128 .pr_read_keys
= sd_pr_read_keys
,
2129 .pr_read_reservation
= sd_pr_read_reservation
,
2132 static void scsi_disk_free_disk(struct gendisk
*disk
)
2134 struct scsi_disk
*sdkp
= scsi_disk(disk
);
2136 put_device(&sdkp
->disk_dev
);
2139 static const struct block_device_operations sd_fops
= {
2140 .owner
= THIS_MODULE
,
2142 .release
= sd_release
,
2144 .getgeo
= sd_getgeo
,
2145 .compat_ioctl
= blkdev_compat_ptr_ioctl
,
2146 .check_events
= sd_check_events
,
2147 .unlock_native_capacity
= sd_unlock_native_capacity
,
2148 .report_zones
= sd_zbc_report_zones
,
2149 .get_unique_id
= sd_get_unique_id
,
2150 .free_disk
= scsi_disk_free_disk
,
2151 .pr_ops
= &sd_pr_ops
,
2155 * sd_eh_reset - reset error handling callback
2156 * @scmd: sd-issued command that has failed
2158 * This function is called by the SCSI midlayer before starting
2159 * SCSI EH. When counting medium access failures we have to be
2160 * careful to register it only only once per device and SCSI EH run;
2161 * there might be several timed out commands which will cause the
2162 * 'max_medium_access_timeouts' counter to trigger after the first
2163 * SCSI EH run already and set the device to offline.
2164 * So this function resets the internal counter before starting SCSI EH.
2166 static void sd_eh_reset(struct scsi_cmnd
*scmd
)
2168 struct scsi_disk
*sdkp
= scsi_disk(scsi_cmd_to_rq(scmd
)->q
->disk
);
2170 /* New SCSI EH run, reset gate variable */
2171 sdkp
->ignore_medium_access_errors
= false;
2175 * sd_eh_action - error handling callback
2176 * @scmd: sd-issued command that has failed
2177 * @eh_disp: The recovery disposition suggested by the midlayer
2179 * This function is called by the SCSI midlayer upon completion of an
2180 * error test command (currently TEST UNIT READY). The result of sending
2181 * the eh command is passed in eh_disp. We're looking for devices that
2182 * fail medium access commands but are OK with non access commands like
2183 * test unit ready (so wrongly see the device as having a successful
2186 static int sd_eh_action(struct scsi_cmnd
*scmd
, int eh_disp
)
2188 struct scsi_disk
*sdkp
= scsi_disk(scsi_cmd_to_rq(scmd
)->q
->disk
);
2189 struct scsi_device
*sdev
= scmd
->device
;
2191 if (!scsi_device_online(sdev
) ||
2192 !scsi_medium_access_command(scmd
) ||
2193 host_byte(scmd
->result
) != DID_TIME_OUT
||
2198 * The device has timed out executing a medium access command.
2199 * However, the TEST UNIT READY command sent during error
2200 * handling completed successfully. Either the device is in the
2201 * process of recovering or has it suffered an internal failure
2202 * that prevents access to the storage medium.
2204 if (!sdkp
->ignore_medium_access_errors
) {
2205 sdkp
->medium_access_timed_out
++;
2206 sdkp
->ignore_medium_access_errors
= true;
2210 * If the device keeps failing read/write commands but TEST UNIT
2211 * READY always completes successfully we assume that medium
2212 * access is no longer possible and take the device offline.
2214 if (sdkp
->medium_access_timed_out
>= sdkp
->max_medium_access_timeouts
) {
2215 scmd_printk(KERN_ERR
, scmd
,
2216 "Medium access timeout failure. Offlining disk!\n");
2217 mutex_lock(&sdev
->state_mutex
);
2218 scsi_device_set_state(sdev
, SDEV_OFFLINE
);
2219 mutex_unlock(&sdev
->state_mutex
);
2227 static unsigned int sd_completed_bytes(struct scsi_cmnd
*scmd
)
2229 struct request
*req
= scsi_cmd_to_rq(scmd
);
2230 struct scsi_device
*sdev
= scmd
->device
;
2231 unsigned int transferred
, good_bytes
;
2232 u64 start_lba
, end_lba
, bad_lba
;
2235 * Some commands have a payload smaller than the device logical
2236 * block size (e.g. INQUIRY on a 4K disk).
2238 if (scsi_bufflen(scmd
) <= sdev
->sector_size
)
2241 /* Check if we have a 'bad_lba' information */
2242 if (!scsi_get_sense_info_fld(scmd
->sense_buffer
,
2243 SCSI_SENSE_BUFFERSIZE
,
2248 * If the bad lba was reported incorrectly, we have no idea where
2251 start_lba
= sectors_to_logical(sdev
, blk_rq_pos(req
));
2252 end_lba
= start_lba
+ bytes_to_logical(sdev
, scsi_bufflen(scmd
));
2253 if (bad_lba
< start_lba
|| bad_lba
>= end_lba
)
2257 * resid is optional but mostly filled in. When it's unused,
2258 * its value is zero, so we assume the whole buffer transferred
2260 transferred
= scsi_bufflen(scmd
) - scsi_get_resid(scmd
);
2262 /* This computation should always be done in terms of the
2263 * resolution of the device's medium.
2265 good_bytes
= logical_to_bytes(sdev
, bad_lba
- start_lba
);
2267 return min(good_bytes
, transferred
);
2271 * sd_done - bottom half handler: called when the lower level
2272 * driver has completed (successfully or otherwise) a scsi command.
2273 * @SCpnt: mid-level's per command structure.
2275 * Note: potentially run from within an ISR. Must not block.
2277 static int sd_done(struct scsi_cmnd
*SCpnt
)
2279 int result
= SCpnt
->result
;
2280 unsigned int good_bytes
= result
? 0 : scsi_bufflen(SCpnt
);
2281 unsigned int sector_size
= SCpnt
->device
->sector_size
;
2283 struct scsi_sense_hdr sshdr
;
2284 struct request
*req
= scsi_cmd_to_rq(SCpnt
);
2285 struct scsi_disk
*sdkp
= scsi_disk(req
->q
->disk
);
2286 int sense_valid
= 0;
2287 int sense_deferred
= 0;
2289 switch (req_op(req
)) {
2290 case REQ_OP_DISCARD
:
2291 case REQ_OP_WRITE_ZEROES
:
2292 case REQ_OP_ZONE_RESET
:
2293 case REQ_OP_ZONE_RESET_ALL
:
2294 case REQ_OP_ZONE_OPEN
:
2295 case REQ_OP_ZONE_CLOSE
:
2296 case REQ_OP_ZONE_FINISH
:
2298 good_bytes
= blk_rq_bytes(req
);
2299 scsi_set_resid(SCpnt
, 0);
2302 scsi_set_resid(SCpnt
, blk_rq_bytes(req
));
2307 * In case of bogus fw or device, we could end up having
2308 * an unaligned partial completion. Check this here and force
2311 resid
= scsi_get_resid(SCpnt
);
2312 if (resid
& (sector_size
- 1)) {
2313 sd_printk(KERN_INFO
, sdkp
,
2314 "Unaligned partial completion (resid=%u, sector_sz=%u)\n",
2315 resid
, sector_size
);
2316 scsi_print_command(SCpnt
);
2317 resid
= min(scsi_bufflen(SCpnt
),
2318 round_up(resid
, sector_size
));
2319 scsi_set_resid(SCpnt
, resid
);
2324 sense_valid
= scsi_command_normalize_sense(SCpnt
, &sshdr
);
2326 sense_deferred
= scsi_sense_is_deferred(&sshdr
);
2328 sdkp
->medium_access_timed_out
= 0;
2330 if (!scsi_status_is_check_condition(result
) &&
2331 (!sense_valid
|| sense_deferred
))
2334 switch (sshdr
.sense_key
) {
2335 case HARDWARE_ERROR
:
2337 good_bytes
= sd_completed_bytes(SCpnt
);
2339 case RECOVERED_ERROR
:
2340 good_bytes
= scsi_bufflen(SCpnt
);
2343 /* This indicates a false check condition, so ignore it. An
2344 * unknown amount of data was transferred so treat it as an
2348 memset(SCpnt
->sense_buffer
, 0, SCSI_SENSE_BUFFERSIZE
);
2350 case ABORTED_COMMAND
:
2351 if (sshdr
.asc
== 0x10) /* DIF: Target detected corruption */
2352 good_bytes
= sd_completed_bytes(SCpnt
);
2354 case ILLEGAL_REQUEST
:
2355 switch (sshdr
.asc
) {
2356 case 0x10: /* DIX: Host detected corruption */
2357 good_bytes
= sd_completed_bytes(SCpnt
);
2359 case 0x20: /* INVALID COMMAND OPCODE */
2360 case 0x24: /* INVALID FIELD IN CDB */
2361 switch (SCpnt
->cmnd
[0]) {
2363 sd_disable_discard(sdkp
);
2367 if (SCpnt
->cmnd
[1] & 8) { /* UNMAP */
2368 sd_disable_discard(sdkp
);
2370 sd_disable_write_same(sdkp
);
2371 req
->rq_flags
|= RQF_QUIET
;
2382 if (sdkp
->device
->type
== TYPE_ZBC
)
2383 good_bytes
= sd_zbc_complete(SCpnt
, good_bytes
, &sshdr
);
2385 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO
, SCpnt
,
2386 "sd_done: completed %d of %d bytes\n",
2387 good_bytes
, scsi_bufflen(SCpnt
)));
2393 * spinup disk - called only in sd_revalidate_disk()
2396 sd_spinup_disk(struct scsi_disk
*sdkp
)
2398 static const u8 cmd
[10] = { TEST_UNIT_READY
};
2399 unsigned long spintime_expire
= 0;
2400 int spintime
, sense_valid
= 0;
2401 unsigned int the_result
;
2402 struct scsi_sense_hdr sshdr
;
2403 struct scsi_failure failure_defs
[] = {
2404 /* Do not retry Medium Not Present */
2406 .sense
= UNIT_ATTENTION
,
2408 .ascq
= SCMD_FAILURE_ASCQ_ANY
,
2409 .result
= SAM_STAT_CHECK_CONDITION
,
2414 .ascq
= SCMD_FAILURE_ASCQ_ANY
,
2415 .result
= SAM_STAT_CHECK_CONDITION
,
2417 /* Retry when scsi_status_is_good would return false 3 times */
2419 .result
= SCMD_FAILURE_STAT_ANY
,
2424 struct scsi_failures failures
= {
2425 .failure_definitions
= failure_defs
,
2427 const struct scsi_exec_args exec_args
= {
2429 .failures
= &failures
,
2434 /* Spin up drives, as required. Only do this at boot time */
2435 /* Spinup needs to be done for module loads too. */
2437 bool media_was_present
= sdkp
->media_present
;
2439 scsi_failures_reset_retries(&failures
);
2441 the_result
= scsi_execute_cmd(sdkp
->device
, cmd
, REQ_OP_DRV_IN
,
2442 NULL
, 0, SD_TIMEOUT
,
2443 sdkp
->max_retries
, &exec_args
);
2446 if (the_result
> 0) {
2448 * If the drive has indicated to us that it doesn't
2449 * have any media in it, don't bother with any more
2452 if (media_not_present(sdkp
, &sshdr
)) {
2453 if (media_was_present
)
2454 sd_printk(KERN_NOTICE
, sdkp
,
2455 "Media removed, stopped polling\n");
2458 sense_valid
= scsi_sense_valid(&sshdr
);
2461 if (!scsi_status_is_check_condition(the_result
)) {
2462 /* no sense, TUR either succeeded or failed
2463 * with a status error */
2464 if(!spintime
&& !scsi_status_is_good(the_result
)) {
2465 sd_print_result(sdkp
, "Test Unit Ready failed",
2472 * The device does not want the automatic start to be issued.
2474 if (sdkp
->device
->no_start_on_add
)
2477 if (sense_valid
&& sshdr
.sense_key
== NOT_READY
) {
2478 if (sshdr
.asc
== 4 && sshdr
.ascq
== 3)
2479 break; /* manual intervention required */
2480 if (sshdr
.asc
== 4 && sshdr
.ascq
== 0xb)
2481 break; /* standby */
2482 if (sshdr
.asc
== 4 && sshdr
.ascq
== 0xc)
2483 break; /* unavailable */
2484 if (sshdr
.asc
== 4 && sshdr
.ascq
== 0x1b)
2485 break; /* sanitize in progress */
2486 if (sshdr
.asc
== 4 && sshdr
.ascq
== 0x24)
2487 break; /* depopulation in progress */
2488 if (sshdr
.asc
== 4 && sshdr
.ascq
== 0x25)
2489 break; /* depopulation restoration in progress */
2491 * Issue command to spin up drive when not ready
2494 /* Return immediately and start spin cycle */
2495 const u8 start_cmd
[10] = {
2498 [4] = sdkp
->device
->start_stop_pwr_cond
?
2502 sd_printk(KERN_NOTICE
, sdkp
, "Spinning up disk...");
2503 scsi_execute_cmd(sdkp
->device
, start_cmd
,
2504 REQ_OP_DRV_IN
, NULL
, 0,
2505 SD_TIMEOUT
, sdkp
->max_retries
,
2507 spintime_expire
= jiffies
+ 100 * HZ
;
2510 /* Wait 1 second for next try */
2512 printk(KERN_CONT
".");
2515 * Wait for USB flash devices with slow firmware.
2516 * Yes, this sense key/ASC combination shouldn't
2517 * occur here. It's characteristic of these devices.
2519 } else if (sense_valid
&&
2520 sshdr
.sense_key
== UNIT_ATTENTION
&&
2521 sshdr
.asc
== 0x28) {
2523 spintime_expire
= jiffies
+ 5 * HZ
;
2526 /* Wait 1 second for next try */
2529 /* we don't understand the sense code, so it's
2530 * probably pointless to loop */
2532 sd_printk(KERN_NOTICE
, sdkp
, "Unit Not Ready\n");
2533 sd_print_sense_hdr(sdkp
, &sshdr
);
2538 } while (spintime
&& time_before_eq(jiffies
, spintime_expire
));
2541 if (scsi_status_is_good(the_result
))
2542 printk(KERN_CONT
"ready\n");
2544 printk(KERN_CONT
"not responding...\n");
2549 * Determine whether disk supports Data Integrity Field.
2551 static int sd_read_protection_type(struct scsi_disk
*sdkp
, unsigned char *buffer
)
2553 struct scsi_device
*sdp
= sdkp
->device
;
2556 if (scsi_device_protection(sdp
) == 0 || (buffer
[12] & 1) == 0) {
2557 sdkp
->protection_type
= 0;
2561 type
= ((buffer
[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */
2563 if (type
> T10_PI_TYPE3_PROTECTION
) {
2564 sd_printk(KERN_ERR
, sdkp
, "formatted with unsupported" \
2565 " protection type %u. Disabling disk!\n",
2567 sdkp
->protection_type
= 0;
2571 sdkp
->protection_type
= type
;
2576 static void sd_config_protection(struct scsi_disk
*sdkp
,
2577 struct queue_limits
*lim
)
2579 struct scsi_device
*sdp
= sdkp
->device
;
2581 if (IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY
))
2582 sd_dif_config_host(sdkp
, lim
);
2584 if (!sdkp
->protection_type
)
2587 if (!scsi_host_dif_capable(sdp
->host
, sdkp
->protection_type
)) {
2588 sd_first_printk(KERN_NOTICE
, sdkp
,
2589 "Disabling DIF Type %u protection\n",
2590 sdkp
->protection_type
);
2591 sdkp
->protection_type
= 0;
2594 sd_first_printk(KERN_NOTICE
, sdkp
, "Enabling DIF Type %u protection\n",
2595 sdkp
->protection_type
);
2598 static void read_capacity_error(struct scsi_disk
*sdkp
, struct scsi_device
*sdp
,
2599 struct scsi_sense_hdr
*sshdr
, int sense_valid
,
2603 sd_print_sense_hdr(sdkp
, sshdr
);
2605 sd_printk(KERN_NOTICE
, sdkp
, "Sense not available.\n");
2608 * Set dirty bit for removable devices if not ready -
2609 * sometimes drives will not report this properly.
2611 if (sdp
->removable
&&
2612 sense_valid
&& sshdr
->sense_key
== NOT_READY
)
2613 set_media_not_present(sdkp
);
2616 * We used to set media_present to 0 here to indicate no media
2617 * in the drive, but some drives fail read capacity even with
2618 * media present, so we can't do that.
2620 sdkp
->capacity
= 0; /* unknown mapped to zero - as usual */
2624 #if RC16_LEN > SD_BUF_SIZE
2625 #error RC16_LEN must not be more than SD_BUF_SIZE
2628 #define READ_CAPACITY_RETRIES_ON_RESET 10
2630 static int read_capacity_16(struct scsi_disk
*sdkp
, struct scsi_device
*sdp
,
2631 struct queue_limits
*lim
, unsigned char *buffer
)
2633 unsigned char cmd
[16];
2634 struct scsi_sense_hdr sshdr
;
2635 const struct scsi_exec_args exec_args
= {
2638 int sense_valid
= 0;
2640 int retries
= 3, reset_retries
= READ_CAPACITY_RETRIES_ON_RESET
;
2641 unsigned int alignment
;
2642 unsigned long long lba
;
2643 unsigned sector_size
;
2645 if (sdp
->no_read_capacity_16
)
2650 cmd
[0] = SERVICE_ACTION_IN_16
;
2651 cmd
[1] = SAI_READ_CAPACITY_16
;
2653 memset(buffer
, 0, RC16_LEN
);
2655 the_result
= scsi_execute_cmd(sdp
, cmd
, REQ_OP_DRV_IN
,
2656 buffer
, RC16_LEN
, SD_TIMEOUT
,
2657 sdkp
->max_retries
, &exec_args
);
2658 if (the_result
> 0) {
2659 if (media_not_present(sdkp
, &sshdr
))
2662 sense_valid
= scsi_sense_valid(&sshdr
);
2664 sshdr
.sense_key
== ILLEGAL_REQUEST
&&
2665 (sshdr
.asc
== 0x20 || sshdr
.asc
== 0x24) &&
2667 /* Invalid Command Operation Code or
2668 * Invalid Field in CDB, just retry
2669 * silently with RC10 */
2672 sshdr
.sense_key
== UNIT_ATTENTION
&&
2673 sshdr
.asc
== 0x29 && sshdr
.ascq
== 0x00)
2674 /* Device reset might occur several times,
2675 * give it one more chance */
2676 if (--reset_retries
> 0)
2681 } while (the_result
&& retries
);
2684 sd_print_result(sdkp
, "Read Capacity(16) failed", the_result
);
2685 read_capacity_error(sdkp
, sdp
, &sshdr
, sense_valid
, the_result
);
2689 sector_size
= get_unaligned_be32(&buffer
[8]);
2690 lba
= get_unaligned_be64(&buffer
[0]);
2692 if (sd_read_protection_type(sdkp
, buffer
) < 0) {
2697 /* Logical blocks per physical block exponent */
2698 sdkp
->physical_block_size
= (1 << (buffer
[13] & 0xf)) * sector_size
;
2701 sdkp
->rc_basis
= (buffer
[12] >> 4) & 0x3;
2703 /* Lowest aligned logical block */
2704 alignment
= ((buffer
[14] & 0x3f) << 8 | buffer
[15]) * sector_size
;
2705 lim
->alignment_offset
= alignment
;
2706 if (alignment
&& sdkp
->first_scan
)
2707 sd_printk(KERN_NOTICE
, sdkp
,
2708 "physical block alignment offset: %u\n", alignment
);
2710 if (buffer
[14] & 0x80) { /* LBPME */
2713 if (buffer
[14] & 0x40) /* LBPRZ */
2717 sdkp
->capacity
= lba
+ 1;
2721 static int read_capacity_10(struct scsi_disk
*sdkp
, struct scsi_device
*sdp
,
2722 unsigned char *buffer
)
2724 static const u8 cmd
[10] = { READ_CAPACITY
};
2725 struct scsi_sense_hdr sshdr
;
2726 struct scsi_failure failure_defs
[] = {
2727 /* Do not retry Medium Not Present */
2729 .sense
= UNIT_ATTENTION
,
2731 .result
= SAM_STAT_CHECK_CONDITION
,
2736 .result
= SAM_STAT_CHECK_CONDITION
,
2738 /* Device reset might occur several times so retry a lot */
2740 .sense
= UNIT_ATTENTION
,
2742 .allowed
= READ_CAPACITY_RETRIES_ON_RESET
,
2743 .result
= SAM_STAT_CHECK_CONDITION
,
2745 /* Any other error not listed above retry 3 times */
2747 .result
= SCMD_FAILURE_RESULT_ANY
,
2752 struct scsi_failures failures
= {
2753 .failure_definitions
= failure_defs
,
2755 const struct scsi_exec_args exec_args
= {
2757 .failures
= &failures
,
2759 int sense_valid
= 0;
2762 unsigned sector_size
;
2764 memset(buffer
, 0, 8);
2766 the_result
= scsi_execute_cmd(sdp
, cmd
, REQ_OP_DRV_IN
, buffer
,
2767 8, SD_TIMEOUT
, sdkp
->max_retries
,
2770 if (the_result
> 0) {
2771 sense_valid
= scsi_sense_valid(&sshdr
);
2773 if (media_not_present(sdkp
, &sshdr
))
2778 sd_print_result(sdkp
, "Read Capacity(10) failed", the_result
);
2779 read_capacity_error(sdkp
, sdp
, &sshdr
, sense_valid
, the_result
);
2783 sector_size
= get_unaligned_be32(&buffer
[4]);
2784 lba
= get_unaligned_be32(&buffer
[0]);
2786 if (sdp
->no_read_capacity_16
&& (lba
== 0xffffffff)) {
2787 /* Some buggy (usb cardreader) devices return an lba of
2788 0xffffffff when the want to report a size of 0 (with
2789 which they really mean no media is present) */
2791 sdkp
->physical_block_size
= sector_size
;
2795 sdkp
->capacity
= lba
+ 1;
2796 sdkp
->physical_block_size
= sector_size
;
2800 static int sd_try_rc16_first(struct scsi_device
*sdp
)
2802 if (sdp
->host
->max_cmd_len
< 16)
2804 if (sdp
->try_rc_10_first
)
2806 if (sdp
->scsi_level
> SCSI_SPC_2
)
2808 if (scsi_device_protection(sdp
))
2814 * read disk capacity
2817 sd_read_capacity(struct scsi_disk
*sdkp
, struct queue_limits
*lim
,
2818 unsigned char *buffer
)
2821 struct scsi_device
*sdp
= sdkp
->device
;
2823 if (sd_try_rc16_first(sdp
)) {
2824 sector_size
= read_capacity_16(sdkp
, sdp
, lim
, buffer
);
2825 if (sector_size
== -EOVERFLOW
)
2827 if (sector_size
== -ENODEV
)
2829 if (sector_size
< 0)
2830 sector_size
= read_capacity_10(sdkp
, sdp
, buffer
);
2831 if (sector_size
< 0)
2834 sector_size
= read_capacity_10(sdkp
, sdp
, buffer
);
2835 if (sector_size
== -EOVERFLOW
)
2837 if (sector_size
< 0)
2839 if ((sizeof(sdkp
->capacity
) > 4) &&
2840 (sdkp
->capacity
> 0xffffffffULL
)) {
2841 int old_sector_size
= sector_size
;
2842 sd_printk(KERN_NOTICE
, sdkp
, "Very big device. "
2843 "Trying to use READ CAPACITY(16).\n");
2844 sector_size
= read_capacity_16(sdkp
, sdp
, lim
, buffer
);
2845 if (sector_size
< 0) {
2846 sd_printk(KERN_NOTICE
, sdkp
,
2847 "Using 0xffffffff as device size\n");
2848 sdkp
->capacity
= 1 + (sector_t
) 0xffffffff;
2849 sector_size
= old_sector_size
;
2852 /* Remember that READ CAPACITY(16) succeeded */
2853 sdp
->try_rc_10_first
= 0;
2857 /* Some devices are known to return the total number of blocks,
2858 * not the highest block number. Some devices have versions
2859 * which do this and others which do not. Some devices we might
2860 * suspect of doing this but we don't know for certain.
2862 * If we know the reported capacity is wrong, decrement it. If
2863 * we can only guess, then assume the number of blocks is even
2864 * (usually true but not always) and err on the side of lowering
2867 if (sdp
->fix_capacity
||
2868 (sdp
->guess_capacity
&& (sdkp
->capacity
& 0x01))) {
2869 sd_printk(KERN_INFO
, sdkp
, "Adjusting the sector count "
2870 "from its reported value: %llu\n",
2871 (unsigned long long) sdkp
->capacity
);
2876 if (sector_size
== 0) {
2878 sd_printk(KERN_NOTICE
, sdkp
, "Sector size 0 reported, "
2882 if (sector_size
!= 512 &&
2883 sector_size
!= 1024 &&
2884 sector_size
!= 2048 &&
2885 sector_size
!= 4096) {
2886 sd_printk(KERN_NOTICE
, sdkp
, "Unsupported sector size %d.\n",
2889 * The user might want to re-format the drive with
2890 * a supported sectorsize. Once this happens, it
2891 * would be relatively trivial to set the thing up.
2892 * For this reason, we leave the thing in the table.
2896 * set a bogus sector size so the normal read/write
2897 * logic in the block layer will eventually refuse any
2898 * request on this device without tripping over power
2899 * of two sector size assumptions
2903 lim
->logical_block_size
= sector_size
;
2904 lim
->physical_block_size
= sdkp
->physical_block_size
;
2905 sdkp
->device
->sector_size
= sector_size
;
2907 if (sdkp
->capacity
> 0xffffffff)
2908 sdp
->use_16_for_rw
= 1;
2913 * Print disk capacity
2916 sd_print_capacity(struct scsi_disk
*sdkp
,
2917 sector_t old_capacity
)
2919 int sector_size
= sdkp
->device
->sector_size
;
2920 char cap_str_2
[10], cap_str_10
[10];
2922 if (!sdkp
->first_scan
&& old_capacity
== sdkp
->capacity
)
2925 string_get_size(sdkp
->capacity
, sector_size
,
2926 STRING_UNITS_2
, cap_str_2
, sizeof(cap_str_2
));
2927 string_get_size(sdkp
->capacity
, sector_size
,
2928 STRING_UNITS_10
, cap_str_10
, sizeof(cap_str_10
));
2930 sd_printk(KERN_NOTICE
, sdkp
,
2931 "%llu %d-byte logical blocks: (%s/%s)\n",
2932 (unsigned long long)sdkp
->capacity
,
2933 sector_size
, cap_str_10
, cap_str_2
);
2935 if (sdkp
->physical_block_size
!= sector_size
)
2936 sd_printk(KERN_NOTICE
, sdkp
,
2937 "%u-byte physical blocks\n",
2938 sdkp
->physical_block_size
);
2941 /* called with buffer of length 512 */
2943 sd_do_mode_sense(struct scsi_disk
*sdkp
, int dbd
, int modepage
,
2944 unsigned char *buffer
, int len
, struct scsi_mode_data
*data
,
2945 struct scsi_sense_hdr
*sshdr
)
2948 * If we must use MODE SENSE(10), make sure that the buffer length
2949 * is at least 8 bytes so that the mode sense header fits.
2951 if (sdkp
->device
->use_10_for_ms
&& len
< 8)
2954 return scsi_mode_sense(sdkp
->device
, dbd
, modepage
, 0, buffer
, len
,
2955 SD_TIMEOUT
, sdkp
->max_retries
, data
, sshdr
);
2959 * read write protect setting, if possible - called only in sd_revalidate_disk()
2960 * called with buffer of length SD_BUF_SIZE
2963 sd_read_write_protect_flag(struct scsi_disk
*sdkp
, unsigned char *buffer
)
2966 struct scsi_device
*sdp
= sdkp
->device
;
2967 struct scsi_mode_data data
;
2968 int old_wp
= sdkp
->write_prot
;
2970 set_disk_ro(sdkp
->disk
, 0);
2971 if (sdp
->skip_ms_page_3f
) {
2972 sd_first_printk(KERN_NOTICE
, sdkp
, "Assuming Write Enabled\n");
2976 if (sdp
->use_192_bytes_for_3f
) {
2977 res
= sd_do_mode_sense(sdkp
, 0, 0x3F, buffer
, 192, &data
, NULL
);
2980 * First attempt: ask for all pages (0x3F), but only 4 bytes.
2981 * We have to start carefully: some devices hang if we ask
2982 * for more than is available.
2984 res
= sd_do_mode_sense(sdkp
, 0, 0x3F, buffer
, 4, &data
, NULL
);
2987 * Second attempt: ask for page 0 When only page 0 is
2988 * implemented, a request for page 3F may return Sense Key
2989 * 5: Illegal Request, Sense Code 24: Invalid field in
2993 res
= sd_do_mode_sense(sdkp
, 0, 0, buffer
, 4, &data
, NULL
);
2996 * Third attempt: ask 255 bytes, as we did earlier.
2999 res
= sd_do_mode_sense(sdkp
, 0, 0x3F, buffer
, 255,
3004 sd_first_printk(KERN_WARNING
, sdkp
,
3005 "Test WP failed, assume Write Enabled\n");
3007 sdkp
->write_prot
= ((data
.device_specific
& 0x80) != 0);
3008 set_disk_ro(sdkp
->disk
, sdkp
->write_prot
);
3009 if (sdkp
->first_scan
|| old_wp
!= sdkp
->write_prot
) {
3010 sd_printk(KERN_NOTICE
, sdkp
, "Write Protect is %s\n",
3011 sdkp
->write_prot
? "on" : "off");
3012 sd_printk(KERN_DEBUG
, sdkp
, "Mode Sense: %4ph\n", buffer
);
3018 * sd_read_cache_type - called only from sd_revalidate_disk()
3019 * called with buffer of length SD_BUF_SIZE
3022 sd_read_cache_type(struct scsi_disk
*sdkp
, unsigned char *buffer
)
3025 struct scsi_device
*sdp
= sdkp
->device
;
3030 struct scsi_mode_data data
;
3031 struct scsi_sense_hdr sshdr
;
3032 int old_wce
= sdkp
->WCE
;
3033 int old_rcd
= sdkp
->RCD
;
3034 int old_dpofua
= sdkp
->DPOFUA
;
3037 if (sdkp
->cache_override
)
3041 if (sdp
->skip_ms_page_8
) {
3042 if (sdp
->type
== TYPE_RBC
)
3045 if (sdp
->skip_ms_page_3f
)
3048 if (sdp
->use_192_bytes_for_3f
)
3052 } else if (sdp
->type
== TYPE_RBC
) {
3060 /* cautiously ask */
3061 res
= sd_do_mode_sense(sdkp
, dbd
, modepage
, buffer
, first_len
,
3067 if (!data
.header_length
) {
3070 sd_first_printk(KERN_ERR
, sdkp
,
3071 "Missing header in MODE_SENSE response\n");
3074 /* that went OK, now ask for the proper length */
3078 * We're only interested in the first three bytes, actually.
3079 * But the data cache page is defined for the first 20.
3083 else if (len
> SD_BUF_SIZE
) {
3084 sd_first_printk(KERN_NOTICE
, sdkp
, "Truncating mode parameter "
3085 "data from %d to %d bytes\n", len
, SD_BUF_SIZE
);
3088 if (modepage
== 0x3F && sdp
->use_192_bytes_for_3f
)
3092 if (len
> first_len
)
3093 res
= sd_do_mode_sense(sdkp
, dbd
, modepage
, buffer
, len
,
3097 int offset
= data
.header_length
+ data
.block_descriptor_length
;
3099 while (offset
< len
) {
3100 u8 page_code
= buffer
[offset
] & 0x3F;
3101 u8 spf
= buffer
[offset
] & 0x40;
3103 if (page_code
== 8 || page_code
== 6) {
3104 /* We're interested only in the first 3 bytes.
3106 if (len
- offset
<= 2) {
3107 sd_first_printk(KERN_ERR
, sdkp
,
3108 "Incomplete mode parameter "
3112 modepage
= page_code
;
3116 /* Go to the next page */
3117 if (spf
&& len
- offset
> 3)
3118 offset
+= 4 + (buffer
[offset
+2] << 8) +
3120 else if (!spf
&& len
- offset
> 1)
3121 offset
+= 2 + buffer
[offset
+1];
3123 sd_first_printk(KERN_ERR
, sdkp
,
3125 "parameter data\n");
3131 sd_first_printk(KERN_WARNING
, sdkp
,
3132 "No Caching mode page found\n");
3136 if (modepage
== 8) {
3137 sdkp
->WCE
= ((buffer
[offset
+ 2] & 0x04) != 0);
3138 sdkp
->RCD
= ((buffer
[offset
+ 2] & 0x01) != 0);
3140 sdkp
->WCE
= ((buffer
[offset
+ 2] & 0x01) == 0);
3144 sdkp
->DPOFUA
= (data
.device_specific
& 0x10) != 0;
3145 if (sdp
->broken_fua
) {
3146 sd_first_printk(KERN_NOTICE
, sdkp
, "Disabling FUA\n");
3148 } else if (sdkp
->DPOFUA
&& !sdkp
->device
->use_10_for_rw
&&
3149 !sdkp
->device
->use_16_for_rw
) {
3150 sd_first_printk(KERN_NOTICE
, sdkp
,
3151 "Uses READ/WRITE(6), disabling FUA\n");
3155 /* No cache flush allowed for write protected devices */
3156 if (sdkp
->WCE
&& sdkp
->write_prot
)
3159 if (sdkp
->first_scan
|| old_wce
!= sdkp
->WCE
||
3160 old_rcd
!= sdkp
->RCD
|| old_dpofua
!= sdkp
->DPOFUA
)
3161 sd_printk(KERN_NOTICE
, sdkp
,
3162 "Write cache: %s, read cache: %s, %s\n",
3163 sdkp
->WCE
? "enabled" : "disabled",
3164 sdkp
->RCD
? "disabled" : "enabled",
3165 sdkp
->DPOFUA
? "supports DPO and FUA"
3166 : "doesn't support DPO or FUA");
3172 if (res
== -EIO
&& scsi_sense_valid(&sshdr
) &&
3173 sshdr
.sense_key
== ILLEGAL_REQUEST
&&
3174 sshdr
.asc
== 0x24 && sshdr
.ascq
== 0x0)
3175 /* Invalid field in CDB */
3176 sd_first_printk(KERN_NOTICE
, sdkp
, "Cache data unavailable\n");
3178 sd_first_printk(KERN_ERR
, sdkp
,
3179 "Asking for cache data failed\n");
3182 if (sdp
->wce_default_on
) {
3183 sd_first_printk(KERN_NOTICE
, sdkp
,
3184 "Assuming drive cache: write back\n");
3187 sd_first_printk(KERN_WARNING
, sdkp
,
3188 "Assuming drive cache: write through\n");
3195 static bool sd_is_perm_stream(struct scsi_disk
*sdkp
, unsigned int stream_id
)
3197 u8 cdb
[16] = { SERVICE_ACTION_IN_16
, SAI_GET_STREAM_STATUS
};
3199 struct scsi_stream_status_header h
;
3200 struct scsi_stream_status s
;
3202 struct scsi_device
*sdev
= sdkp
->device
;
3203 struct scsi_sense_hdr sshdr
;
3204 const struct scsi_exec_args exec_args
= {
3209 put_unaligned_be16(stream_id
, &cdb
[4]);
3210 put_unaligned_be32(sizeof(buf
), &cdb
[10]);
3212 res
= scsi_execute_cmd(sdev
, cdb
, REQ_OP_DRV_IN
, &buf
, sizeof(buf
),
3213 SD_TIMEOUT
, sdkp
->max_retries
, &exec_args
);
3216 if (scsi_status_is_check_condition(res
) && scsi_sense_valid(&sshdr
))
3217 sd_print_sense_hdr(sdkp
, &sshdr
);
3220 if (get_unaligned_be32(&buf
.h
.len
) < sizeof(struct scsi_stream_status
))
3222 return buf
.h
.stream_status
[0].perm
;
3225 static void sd_read_io_hints(struct scsi_disk
*sdkp
, unsigned char *buffer
)
3227 struct scsi_device
*sdp
= sdkp
->device
;
3228 const struct scsi_io_group_descriptor
*desc
, *start
, *end
;
3229 u16 permanent_stream_count_old
;
3230 struct scsi_sense_hdr sshdr
;
3231 struct scsi_mode_data data
;
3234 if (sdp
->sdev_bflags
& BLIST_SKIP_IO_HINTS
)
3237 res
= scsi_mode_sense(sdp
, /*dbd=*/0x8, /*modepage=*/0x0a,
3238 /*subpage=*/0x05, buffer
, SD_BUF_SIZE
, SD_TIMEOUT
,
3239 sdkp
->max_retries
, &data
, &sshdr
);
3242 start
= (void *)buffer
+ data
.header_length
+ 16;
3243 end
= (void *)buffer
+ ALIGN_DOWN(data
.header_length
+ data
.length
,
3246 * From "SBC-5 Constrained Streams with Data Lifetimes": Device severs
3247 * should assign the lowest numbered stream identifiers to permanent
3250 for (desc
= start
; desc
< end
; desc
++)
3251 if (!desc
->st_enble
|| !sd_is_perm_stream(sdkp
, desc
- start
))
3253 permanent_stream_count_old
= sdkp
->permanent_stream_count
;
3254 sdkp
->permanent_stream_count
= desc
- start
;
3255 if (sdkp
->rscs
&& sdkp
->permanent_stream_count
< 2)
3256 sd_printk(KERN_INFO
, sdkp
,
3257 "Unexpected: RSCS has been set and the permanent stream count is %u\n",
3258 sdkp
->permanent_stream_count
);
3259 else if (sdkp
->permanent_stream_count
!= permanent_stream_count_old
)
3260 sd_printk(KERN_INFO
, sdkp
, "permanent stream count = %d\n",
3261 sdkp
->permanent_stream_count
);
3265 * The ATO bit indicates whether the DIF application tag is available
3266 * for use by the operating system.
3268 static void sd_read_app_tag_own(struct scsi_disk
*sdkp
, unsigned char *buffer
)
3271 struct scsi_device
*sdp
= sdkp
->device
;
3272 struct scsi_mode_data data
;
3273 struct scsi_sense_hdr sshdr
;
3275 if (sdp
->type
!= TYPE_DISK
&& sdp
->type
!= TYPE_ZBC
)
3278 if (sdkp
->protection_type
== 0)
3281 res
= scsi_mode_sense(sdp
, 1, 0x0a, 0, buffer
, 36, SD_TIMEOUT
,
3282 sdkp
->max_retries
, &data
, &sshdr
);
3284 if (res
< 0 || !data
.header_length
||
3286 sd_first_printk(KERN_WARNING
, sdkp
,
3287 "getting Control mode page failed, assume no ATO\n");
3289 if (res
== -EIO
&& scsi_sense_valid(&sshdr
))
3290 sd_print_sense_hdr(sdkp
, &sshdr
);
3295 offset
= data
.header_length
+ data
.block_descriptor_length
;
3297 if ((buffer
[offset
] & 0x3f) != 0x0a) {
3298 sd_first_printk(KERN_ERR
, sdkp
, "ATO Got wrong page\n");
3302 if ((buffer
[offset
+ 5] & 0x80) == 0)
3310 static unsigned int sd_discard_mode(struct scsi_disk
*sdkp
)
3315 if (!sdkp
->lbpvpd
) {
3316 /* LBP VPD page not provided */
3317 if (sdkp
->max_unmap_blocks
)
3318 return SD_LBP_UNMAP
;
3322 /* LBP VPD page tells us what to use */
3323 if (sdkp
->lbpu
&& sdkp
->max_unmap_blocks
)
3324 return SD_LBP_UNMAP
;
3329 return SD_LBP_DISABLE
;
3333 * Query disk device for preferred I/O sizes.
3335 static void sd_read_block_limits(struct scsi_disk
*sdkp
,
3336 struct queue_limits
*lim
)
3338 struct scsi_vpd
*vpd
;
3342 vpd
= rcu_dereference(sdkp
->device
->vpd_pgb0
);
3343 if (!vpd
|| vpd
->len
< 16)
3346 sdkp
->min_xfer_blocks
= get_unaligned_be16(&vpd
->data
[6]);
3347 sdkp
->max_xfer_blocks
= get_unaligned_be32(&vpd
->data
[8]);
3348 sdkp
->opt_xfer_blocks
= get_unaligned_be32(&vpd
->data
[12]);
3350 if (vpd
->len
>= 64) {
3351 unsigned int lba_count
, desc_count
;
3353 sdkp
->max_ws_blocks
= (u32
)get_unaligned_be64(&vpd
->data
[36]);
3358 lba_count
= get_unaligned_be32(&vpd
->data
[20]);
3359 desc_count
= get_unaligned_be32(&vpd
->data
[24]);
3361 if (lba_count
&& desc_count
)
3362 sdkp
->max_unmap_blocks
= lba_count
;
3364 sdkp
->unmap_granularity
= get_unaligned_be32(&vpd
->data
[28]);
3366 if (vpd
->data
[32] & 0x80)
3367 sdkp
->unmap_alignment
=
3368 get_unaligned_be32(&vpd
->data
[32]) & ~(1 << 31);
3371 sdkp
->max_atomic
= get_unaligned_be32(&vpd
->data
[44]);
3372 sdkp
->atomic_alignment
= get_unaligned_be32(&vpd
->data
[48]);
3373 sdkp
->atomic_granularity
= get_unaligned_be32(&vpd
->data
[52]);
3374 sdkp
->max_atomic_with_boundary
= get_unaligned_be32(&vpd
->data
[56]);
3375 sdkp
->max_atomic_boundary
= get_unaligned_be32(&vpd
->data
[60]);
3377 sd_config_atomic(sdkp
, lim
);
3384 /* Parse the Block Limits Extension VPD page (0xb7) */
3385 static void sd_read_block_limits_ext(struct scsi_disk
*sdkp
)
3387 struct scsi_vpd
*vpd
;
3390 vpd
= rcu_dereference(sdkp
->device
->vpd_pgb7
);
3391 if (vpd
&& vpd
->len
>= 2)
3392 sdkp
->rscs
= vpd
->data
[5] & 1;
3396 /* Query block device characteristics */
3397 static void sd_read_block_characteristics(struct scsi_disk
*sdkp
,
3398 struct queue_limits
*lim
)
3400 struct scsi_vpd
*vpd
;
3404 vpd
= rcu_dereference(sdkp
->device
->vpd_pgb1
);
3406 if (!vpd
|| vpd
->len
<= 8) {
3411 rot
= get_unaligned_be16(&vpd
->data
[4]);
3412 sdkp
->zoned
= (vpd
->data
[8] >> 4) & 3;
3416 lim
->features
&= ~(BLK_FEAT_ROTATIONAL
| BLK_FEAT_ADD_RANDOM
);
3418 if (!sdkp
->first_scan
)
3421 if (sdkp
->device
->type
== TYPE_ZBC
)
3422 sd_printk(KERN_NOTICE
, sdkp
, "Host-managed zoned block device\n");
3423 else if (sdkp
->zoned
== 1)
3424 sd_printk(KERN_NOTICE
, sdkp
, "Host-aware SMR disk used as regular disk\n");
3425 else if (sdkp
->zoned
== 2)
3426 sd_printk(KERN_NOTICE
, sdkp
, "Drive-managed SMR disk\n");
3430 * sd_read_block_provisioning - Query provisioning VPD page
3431 * @sdkp: disk to query
3433 static void sd_read_block_provisioning(struct scsi_disk
*sdkp
)
3435 struct scsi_vpd
*vpd
;
3437 if (sdkp
->lbpme
== 0)
3441 vpd
= rcu_dereference(sdkp
->device
->vpd_pgb2
);
3443 if (!vpd
|| vpd
->len
< 8) {
3449 sdkp
->lbpu
= (vpd
->data
[5] >> 7) & 1; /* UNMAP */
3450 sdkp
->lbpws
= (vpd
->data
[5] >> 6) & 1; /* WRITE SAME(16) w/ UNMAP */
3451 sdkp
->lbpws10
= (vpd
->data
[5] >> 5) & 1; /* WRITE SAME(10) w/ UNMAP */
3455 static void sd_read_write_same(struct scsi_disk
*sdkp
, unsigned char *buffer
)
3457 struct scsi_device
*sdev
= sdkp
->device
;
3459 if (sdev
->host
->no_write_same
) {
3460 sdev
->no_write_same
= 1;
3465 if (scsi_report_opcode(sdev
, buffer
, SD_BUF_SIZE
, INQUIRY
, 0) < 0) {
3466 struct scsi_vpd
*vpd
;
3468 sdev
->no_report_opcodes
= 1;
3470 /* Disable WRITE SAME if REPORT SUPPORTED OPERATION
3471 * CODES is unsupported and the device has an ATA
3472 * Information VPD page (SAT).
3475 vpd
= rcu_dereference(sdev
->vpd_pg89
);
3477 sdev
->no_write_same
= 1;
3481 if (scsi_report_opcode(sdev
, buffer
, SD_BUF_SIZE
, WRITE_SAME_16
, 0) == 1)
3484 if (scsi_report_opcode(sdev
, buffer
, SD_BUF_SIZE
, WRITE_SAME
, 0) == 1)
3488 static void sd_read_security(struct scsi_disk
*sdkp
, unsigned char *buffer
)
3490 struct scsi_device
*sdev
= sdkp
->device
;
3492 if (!sdev
->security_supported
)
3495 if (scsi_report_opcode(sdev
, buffer
, SD_BUF_SIZE
,
3496 SECURITY_PROTOCOL_IN
, 0) == 1 &&
3497 scsi_report_opcode(sdev
, buffer
, SD_BUF_SIZE
,
3498 SECURITY_PROTOCOL_OUT
, 0) == 1)
3502 static inline sector_t
sd64_to_sectors(struct scsi_disk
*sdkp
, u8
*buf
)
3504 return logical_to_sectors(sdkp
->device
, get_unaligned_be64(buf
));
3508 * sd_read_cpr - Query concurrent positioning ranges
3509 * @sdkp: disk to query
3511 static void sd_read_cpr(struct scsi_disk
*sdkp
)
3513 struct blk_independent_access_ranges
*iars
= NULL
;
3514 unsigned char *buffer
= NULL
;
3515 unsigned int nr_cpr
= 0;
3516 int i
, vpd_len
, buf_len
= SD_BUF_SIZE
;
3520 * We need to have the capacity set first for the block layer to be
3521 * able to check the ranges.
3523 if (sdkp
->first_scan
)
3526 if (!sdkp
->capacity
)
3530 * Concurrent Positioning Ranges VPD: there can be at most 256 ranges,
3531 * leading to a maximum page size of 64 + 256*32 bytes.
3533 buf_len
= 64 + 256*32;
3534 buffer
= kmalloc(buf_len
, GFP_KERNEL
);
3535 if (!buffer
|| scsi_get_vpd_page(sdkp
->device
, 0xb9, buffer
, buf_len
))
3538 /* We must have at least a 64B header and one 32B range descriptor */
3539 vpd_len
= get_unaligned_be16(&buffer
[2]) + 4;
3540 if (vpd_len
> buf_len
|| vpd_len
< 64 + 32 || (vpd_len
& 31)) {
3541 sd_printk(KERN_ERR
, sdkp
,
3542 "Invalid Concurrent Positioning Ranges VPD page\n");
3546 nr_cpr
= (vpd_len
- 64) / 32;
3552 iars
= disk_alloc_independent_access_ranges(sdkp
->disk
, nr_cpr
);
3559 for (i
= 0; i
< nr_cpr
; i
++, desc
+= 32) {
3561 sd_printk(KERN_ERR
, sdkp
,
3562 "Invalid Concurrent Positioning Range number\n");
3567 iars
->ia_range
[i
].sector
= sd64_to_sectors(sdkp
, desc
+ 8);
3568 iars
->ia_range
[i
].nr_sectors
= sd64_to_sectors(sdkp
, desc
+ 16);
3572 disk_set_independent_access_ranges(sdkp
->disk
, iars
);
3573 if (nr_cpr
&& sdkp
->nr_actuators
!= nr_cpr
) {
3574 sd_printk(KERN_NOTICE
, sdkp
,
3575 "%u concurrent positioning ranges\n", nr_cpr
);
3576 sdkp
->nr_actuators
= nr_cpr
;
3582 static bool sd_validate_min_xfer_size(struct scsi_disk
*sdkp
)
3584 struct scsi_device
*sdp
= sdkp
->device
;
3585 unsigned int min_xfer_bytes
=
3586 logical_to_bytes(sdp
, sdkp
->min_xfer_blocks
);
3588 if (sdkp
->min_xfer_blocks
== 0)
3591 if (min_xfer_bytes
& (sdkp
->physical_block_size
- 1)) {
3592 sd_first_printk(KERN_WARNING
, sdkp
,
3593 "Preferred minimum I/O size %u bytes not a " \
3594 "multiple of physical block size (%u bytes)\n",
3595 min_xfer_bytes
, sdkp
->physical_block_size
);
3596 sdkp
->min_xfer_blocks
= 0;
3600 sd_first_printk(KERN_INFO
, sdkp
, "Preferred minimum I/O size %u bytes\n",
3606 * Determine the device's preferred I/O size for reads and writes
3607 * unless the reported value is unreasonably small, large, not a
3608 * multiple of the physical block size, or simply garbage.
3610 static bool sd_validate_opt_xfer_size(struct scsi_disk
*sdkp
,
3611 unsigned int dev_max
)
3613 struct scsi_device
*sdp
= sdkp
->device
;
3614 unsigned int opt_xfer_bytes
=
3615 logical_to_bytes(sdp
, sdkp
->opt_xfer_blocks
);
3616 unsigned int min_xfer_bytes
=
3617 logical_to_bytes(sdp
, sdkp
->min_xfer_blocks
);
3619 if (sdkp
->opt_xfer_blocks
== 0)
3622 if (sdkp
->opt_xfer_blocks
> dev_max
) {
3623 sd_first_printk(KERN_WARNING
, sdkp
,
3624 "Optimal transfer size %u logical blocks " \
3625 "> dev_max (%u logical blocks)\n",
3626 sdkp
->opt_xfer_blocks
, dev_max
);
3630 if (sdkp
->opt_xfer_blocks
> SD_DEF_XFER_BLOCKS
) {
3631 sd_first_printk(KERN_WARNING
, sdkp
,
3632 "Optimal transfer size %u logical blocks " \
3633 "> sd driver limit (%u logical blocks)\n",
3634 sdkp
->opt_xfer_blocks
, SD_DEF_XFER_BLOCKS
);
3638 if (opt_xfer_bytes
< PAGE_SIZE
) {
3639 sd_first_printk(KERN_WARNING
, sdkp
,
3640 "Optimal transfer size %u bytes < " \
3641 "PAGE_SIZE (%u bytes)\n",
3642 opt_xfer_bytes
, (unsigned int)PAGE_SIZE
);
3646 if (min_xfer_bytes
&& opt_xfer_bytes
% min_xfer_bytes
) {
3647 sd_first_printk(KERN_WARNING
, sdkp
,
3648 "Optimal transfer size %u bytes not a " \
3649 "multiple of preferred minimum block " \
3650 "size (%u bytes)\n",
3651 opt_xfer_bytes
, min_xfer_bytes
);
3655 if (opt_xfer_bytes
& (sdkp
->physical_block_size
- 1)) {
3656 sd_first_printk(KERN_WARNING
, sdkp
,
3657 "Optimal transfer size %u bytes not a " \
3658 "multiple of physical block size (%u bytes)\n",
3659 opt_xfer_bytes
, sdkp
->physical_block_size
);
3663 sd_first_printk(KERN_INFO
, sdkp
, "Optimal transfer size %u bytes\n",
3668 static void sd_read_block_zero(struct scsi_disk
*sdkp
)
3670 struct scsi_device
*sdev
= sdkp
->device
;
3671 unsigned int buf_len
= sdev
->sector_size
;
3672 u8
*buffer
, cmd
[16] = { };
3674 buffer
= kmalloc(buf_len
, GFP_KERNEL
);
3678 if (sdev
->use_16_for_rw
) {
3680 put_unaligned_be64(0, &cmd
[2]); /* Logical block address 0 */
3681 put_unaligned_be32(1, &cmd
[10]);/* Transfer 1 logical block */
3684 put_unaligned_be32(0, &cmd
[2]); /* Logical block address 0 */
3685 put_unaligned_be16(1, &cmd
[7]); /* Transfer 1 logical block */
3688 scsi_execute_cmd(sdkp
->device
, cmd
, REQ_OP_DRV_IN
, buffer
, buf_len
,
3689 SD_TIMEOUT
, sdkp
->max_retries
, NULL
);
3694 * sd_revalidate_disk - called the first time a new disk is seen,
3695 * performs disk spin up, read_capacity, etc.
3696 * @disk: struct gendisk we care about
3698 static int sd_revalidate_disk(struct gendisk
*disk
)
3700 struct scsi_disk
*sdkp
= scsi_disk(disk
);
3701 struct scsi_device
*sdp
= sdkp
->device
;
3702 sector_t old_capacity
= sdkp
->capacity
;
3703 struct queue_limits lim
;
3704 unsigned char *buffer
;
3705 unsigned int dev_max
;
3708 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO
, sdkp
,
3709 "sd_revalidate_disk\n"));
3712 * If the device is offline, don't try and read capacity or any
3713 * of the other niceties.
3715 if (!scsi_device_online(sdp
))
3718 buffer
= kmalloc(SD_BUF_SIZE
, GFP_KERNEL
);
3720 sd_printk(KERN_WARNING
, sdkp
, "sd_revalidate_disk: Memory "
3721 "allocation failure.\n");
3725 sd_spinup_disk(sdkp
);
3727 lim
= queue_limits_start_update(sdkp
->disk
->queue
);
3730 * Without media there is no reason to ask; moreover, some devices
3731 * react badly if we do.
3733 if (sdkp
->media_present
) {
3734 sd_read_capacity(sdkp
, &lim
, buffer
);
3736 * Some USB/UAS devices return generic values for mode pages
3737 * until the media has been accessed. Trigger a READ operation
3738 * to force the device to populate mode pages.
3740 if (sdp
->read_before_ms
)
3741 sd_read_block_zero(sdkp
);
3743 * set the default to rotational. All non-rotational devices
3744 * support the block characteristics VPD page, which will
3745 * cause this to be updated correctly and any device which
3746 * doesn't support it should be treated as rotational.
3748 lim
.features
|= (BLK_FEAT_ROTATIONAL
| BLK_FEAT_ADD_RANDOM
);
3750 if (scsi_device_supports_vpd(sdp
)) {
3751 sd_read_block_provisioning(sdkp
);
3752 sd_read_block_limits(sdkp
, &lim
);
3753 sd_read_block_limits_ext(sdkp
);
3754 sd_read_block_characteristics(sdkp
, &lim
);
3755 sd_zbc_read_zones(sdkp
, &lim
, buffer
);
3758 sd_config_discard(sdkp
, &lim
, sd_discard_mode(sdkp
));
3760 sd_print_capacity(sdkp
, old_capacity
);
3762 sd_read_write_protect_flag(sdkp
, buffer
);
3763 sd_read_cache_type(sdkp
, buffer
);
3764 sd_read_io_hints(sdkp
, buffer
);
3765 sd_read_app_tag_own(sdkp
, buffer
);
3766 sd_read_write_same(sdkp
, buffer
);
3767 sd_read_security(sdkp
, buffer
);
3768 sd_config_protection(sdkp
, &lim
);
3772 * We now have all cache related info, determine how we deal
3773 * with flush requests.
3775 sd_set_flush_flag(sdkp
, &lim
);
3777 /* Initial block count limit based on CDB TRANSFER LENGTH field size. */
3778 dev_max
= sdp
->use_16_for_rw
? SD_MAX_XFER_BLOCKS
: SD_DEF_XFER_BLOCKS
;
3780 /* Some devices report a maximum block count for READ/WRITE requests. */
3781 dev_max
= min_not_zero(dev_max
, sdkp
->max_xfer_blocks
);
3782 lim
.max_dev_sectors
= logical_to_sectors(sdp
, dev_max
);
3784 if (sd_validate_min_xfer_size(sdkp
))
3785 lim
.io_min
= logical_to_bytes(sdp
, sdkp
->min_xfer_blocks
);
3790 * Limit default to SCSI host optimal sector limit if set. There may be
3791 * an impact on performance for when the size of a request exceeds this
3794 lim
.io_opt
= sdp
->host
->opt_sectors
<< SECTOR_SHIFT
;
3795 if (sd_validate_opt_xfer_size(sdkp
, dev_max
)) {
3796 lim
.io_opt
= min_not_zero(lim
.io_opt
,
3797 logical_to_bytes(sdp
, sdkp
->opt_xfer_blocks
));
3800 sdkp
->first_scan
= 0;
3802 set_capacity_and_notify(disk
, logical_to_sectors(sdp
, sdkp
->capacity
));
3803 sd_config_write_same(sdkp
, &lim
);
3806 blk_mq_freeze_queue(sdkp
->disk
->queue
);
3807 err
= queue_limits_commit_update(sdkp
->disk
->queue
, &lim
);
3808 blk_mq_unfreeze_queue(sdkp
->disk
->queue
);
3813 * Query concurrent positioning ranges after
3814 * queue_limits_commit_update() unlocked q->limits_lock to avoid
3815 * deadlock with q->sysfs_dir_lock and q->sysfs_lock.
3817 if (sdkp
->media_present
&& scsi_device_supports_vpd(sdp
))
3821 * For a zoned drive, revalidating the zones can be done only once
3822 * the gendisk capacity is set. So if this fails, set back the gendisk
3825 if (sd_zbc_revalidate_zones(sdkp
))
3826 set_capacity_and_notify(disk
, 0);
3833 * sd_unlock_native_capacity - unlock native capacity
3834 * @disk: struct gendisk to set capacity for
3836 * Block layer calls this function if it detects that partitions
3837 * on @disk reach beyond the end of the device. If the SCSI host
3838 * implements ->unlock_native_capacity() method, it's invoked to
3839 * give it a chance to adjust the device capacity.
3842 * Defined by block layer. Might sleep.
3844 static void sd_unlock_native_capacity(struct gendisk
*disk
)
3846 struct scsi_device
*sdev
= scsi_disk(disk
)->device
;
3848 if (sdev
->host
->hostt
->unlock_native_capacity
)
3849 sdev
->host
->hostt
->unlock_native_capacity(sdev
);
3853 * sd_format_disk_name - format disk name
3854 * @prefix: name prefix - ie. "sd" for SCSI disks
3855 * @index: index of the disk to format name for
3856 * @buf: output buffer
3857 * @buflen: length of the output buffer
3859 * SCSI disk names starts at sda. The 26th device is sdz and the
3860 * 27th is sdaa. The last one for two lettered suffix is sdzz
3861 * which is followed by sdaaa.
3863 * This is basically 26 base counting with one extra 'nil' entry
3864 * at the beginning from the second digit on and can be
3865 * determined using similar method as 26 base conversion with the
3866 * index shifted -1 after each digit is computed.
3872 * 0 on success, -errno on failure.
3874 static int sd_format_disk_name(char *prefix
, int index
, char *buf
, int buflen
)
3876 const int base
= 'z' - 'a' + 1;
3877 char *begin
= buf
+ strlen(prefix
);
3878 char *end
= buf
+ buflen
;
3888 *--p
= 'a' + (index
% unit
);
3889 index
= (index
/ unit
) - 1;
3890 } while (index
>= 0);
3892 memmove(begin
, p
, end
- p
);
3893 memcpy(buf
, prefix
, strlen(prefix
));
3899 * sd_probe - called during driver initialization and whenever a
3900 * new scsi device is attached to the system. It is called once
3901 * for each scsi device (not just disks) present.
3902 * @dev: pointer to device object
3904 * Returns 0 if successful (or not interested in this scsi device
3905 * (e.g. scanner)); 1 when there is an error.
3907 * Note: this function is invoked from the scsi mid-level.
3908 * This function sets up the mapping between a given
3909 * <host,channel,id,lun> (found in sdp) and new device name
3910 * (e.g. /dev/sda). More precisely it is the block device major
3911 * and minor number that is chosen here.
3913 * Assume sd_probe is not re-entrant (for time being)
3914 * Also think about sd_probe() and sd_remove() running coincidentally.
3916 static int sd_probe(struct device
*dev
)
3918 struct scsi_device
*sdp
= to_scsi_device(dev
);
3919 struct scsi_disk
*sdkp
;
3924 scsi_autopm_get_device(sdp
);
3926 if (sdp
->type
!= TYPE_DISK
&&
3927 sdp
->type
!= TYPE_ZBC
&&
3928 sdp
->type
!= TYPE_MOD
&&
3929 sdp
->type
!= TYPE_RBC
)
3932 if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED
) && sdp
->type
== TYPE_ZBC
) {
3933 sdev_printk(KERN_WARNING
, sdp
,
3934 "Unsupported ZBC host-managed device.\n");
3938 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO
, sdp
,
3942 sdkp
= kzalloc(sizeof(*sdkp
), GFP_KERNEL
);
3946 gd
= blk_mq_alloc_disk_for_queue(sdp
->request_queue
,
3947 &sd_bio_compl_lkclass
);
3951 index
= ida_alloc(&sd_index_ida
, GFP_KERNEL
);
3953 sdev_printk(KERN_WARNING
, sdp
, "sd_probe: memory exhausted.\n");
3957 error
= sd_format_disk_name("sd", index
, gd
->disk_name
, DISK_NAME_LEN
);
3959 sdev_printk(KERN_WARNING
, sdp
, "SCSI disk (sd) name length exceeded.\n");
3960 goto out_free_index
;
3965 sdkp
->index
= index
;
3966 sdkp
->max_retries
= SD_MAX_RETRIES
;
3967 atomic_set(&sdkp
->openers
, 0);
3968 atomic_set(&sdkp
->device
->ioerr_cnt
, 0);
3970 if (!sdp
->request_queue
->rq_timeout
) {
3971 if (sdp
->type
!= TYPE_MOD
)
3972 blk_queue_rq_timeout(sdp
->request_queue
, SD_TIMEOUT
);
3974 blk_queue_rq_timeout(sdp
->request_queue
,
3978 device_initialize(&sdkp
->disk_dev
);
3979 sdkp
->disk_dev
.parent
= get_device(dev
);
3980 sdkp
->disk_dev
.class = &sd_disk_class
;
3981 dev_set_name(&sdkp
->disk_dev
, "%s", dev_name(dev
));
3983 error
= device_add(&sdkp
->disk_dev
);
3985 put_device(&sdkp
->disk_dev
);
3989 dev_set_drvdata(dev
, sdkp
);
3991 gd
->major
= sd_major((index
& 0xf0) >> 4);
3992 gd
->first_minor
= ((index
& 0xf) << 4) | (index
& 0xfff00);
3993 gd
->minors
= SD_MINORS
;
3995 gd
->fops
= &sd_fops
;
3996 gd
->private_data
= sdkp
;
3998 /* defaults, until the device tells us otherwise */
3999 sdp
->sector_size
= 512;
4001 sdkp
->media_present
= 1;
4002 sdkp
->write_prot
= 0;
4003 sdkp
->cache_override
= 0;
4007 sdkp
->first_scan
= 1;
4008 sdkp
->max_medium_access_timeouts
= SD_MAX_MEDIUM_TIMEOUTS
;
4010 sd_revalidate_disk(gd
);
4012 if (sdp
->removable
) {
4013 gd
->flags
|= GENHD_FL_REMOVABLE
;
4014 gd
->events
|= DISK_EVENT_MEDIA_CHANGE
;
4015 gd
->event_flags
= DISK_EVENT_FLAG_POLL
| DISK_EVENT_FLAG_UEVENT
;
4018 blk_pm_runtime_init(sdp
->request_queue
, dev
);
4019 if (sdp
->rpm_autosuspend
) {
4020 pm_runtime_set_autosuspend_delay(dev
,
4021 sdp
->host
->rpm_autosuspend_delay
);
4024 error
= device_add_disk(dev
, gd
, NULL
);
4026 device_unregister(&sdkp
->disk_dev
);
4031 if (sdkp
->security
) {
4032 sdkp
->opal_dev
= init_opal_dev(sdkp
, &sd_sec_submit
);
4034 sd_printk(KERN_NOTICE
, sdkp
, "supports TCG Opal\n");
4037 sd_printk(KERN_NOTICE
, sdkp
, "Attached SCSI %sdisk\n",
4038 sdp
->removable
? "removable " : "");
4039 scsi_autopm_put_device(sdp
);
4044 ida_free(&sd_index_ida
, index
);
4050 scsi_autopm_put_device(sdp
);
4055 * sd_remove - called whenever a scsi disk (previously recognized by
4056 * sd_probe) is detached from the system. It is called (potentially
4057 * multiple times) during sd module unload.
4058 * @dev: pointer to device object
4060 * Note: this function is invoked from the scsi mid-level.
4061 * This function potentially frees up a device name (e.g. /dev/sdc)
4062 * that could be re-used by a subsequent sd_probe().
4063 * This function is not called when the built-in sd driver is "exit-ed".
4065 static int sd_remove(struct device
*dev
)
4067 struct scsi_disk
*sdkp
= dev_get_drvdata(dev
);
4069 scsi_autopm_get_device(sdkp
->device
);
4071 device_del(&sdkp
->disk_dev
);
4072 del_gendisk(sdkp
->disk
);
4073 if (!sdkp
->suspended
)
4076 put_disk(sdkp
->disk
);
4080 static void scsi_disk_release(struct device
*dev
)
4082 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
4084 ida_free(&sd_index_ida
, sdkp
->index
);
4085 put_device(&sdkp
->device
->sdev_gendev
);
4086 free_opal_dev(sdkp
->opal_dev
);
4091 static int sd_start_stop_device(struct scsi_disk
*sdkp
, int start
)
4093 unsigned char cmd
[6] = { START_STOP
}; /* START_VALID */
4094 struct scsi_sense_hdr sshdr
;
4095 struct scsi_failure failure_defs
[] = {
4097 /* Power on, reset, or bus device reset occurred */
4098 .sense
= UNIT_ATTENTION
,
4101 .result
= SAM_STAT_CHECK_CONDITION
,
4104 /* Power on occurred */
4105 .sense
= UNIT_ATTENTION
,
4108 .result
= SAM_STAT_CHECK_CONDITION
,
4111 /* SCSI bus reset */
4112 .sense
= UNIT_ATTENTION
,
4115 .result
= SAM_STAT_CHECK_CONDITION
,
4119 struct scsi_failures failures
= {
4121 .failure_definitions
= failure_defs
,
4123 const struct scsi_exec_args exec_args
= {
4125 .req_flags
= BLK_MQ_REQ_PM
,
4126 .failures
= &failures
,
4128 struct scsi_device
*sdp
= sdkp
->device
;
4132 cmd
[4] |= 1; /* START */
4134 if (sdp
->start_stop_pwr_cond
)
4135 cmd
[4] |= start
? 1 << 4 : 3 << 4; /* Active or Standby */
4137 if (!scsi_device_online(sdp
))
4140 res
= scsi_execute_cmd(sdp
, cmd
, REQ_OP_DRV_IN
, NULL
, 0, SD_TIMEOUT
,
4141 sdkp
->max_retries
, &exec_args
);
4143 sd_print_result(sdkp
, "Start/Stop Unit failed", res
);
4144 if (res
> 0 && scsi_sense_valid(&sshdr
)) {
4145 sd_print_sense_hdr(sdkp
, &sshdr
);
4146 /* 0x3a is medium not present */
4147 if (sshdr
.asc
== 0x3a)
4152 /* SCSI error codes must not go to the generic layer */
4160 * Send a SYNCHRONIZE CACHE instruction down to the device through
4161 * the normal SCSI command structure. Wait for the command to
4164 static void sd_shutdown(struct device
*dev
)
4166 struct scsi_disk
*sdkp
= dev_get_drvdata(dev
);
4169 return; /* this can happen */
4171 if (pm_runtime_suspended(dev
))
4174 if (sdkp
->WCE
&& sdkp
->media_present
) {
4175 sd_printk(KERN_NOTICE
, sdkp
, "Synchronizing SCSI cache\n");
4176 sd_sync_cache(sdkp
);
4179 if ((system_state
!= SYSTEM_RESTART
&&
4180 sdkp
->device
->manage_system_start_stop
) ||
4181 (system_state
== SYSTEM_POWER_OFF
&&
4182 sdkp
->device
->manage_shutdown
)) {
4183 sd_printk(KERN_NOTICE
, sdkp
, "Stopping disk\n");
4184 sd_start_stop_device(sdkp
, 0);
4188 static inline bool sd_do_start_stop(struct scsi_device
*sdev
, bool runtime
)
4190 return (sdev
->manage_system_start_stop
&& !runtime
) ||
4191 (sdev
->manage_runtime_start_stop
&& runtime
);
4194 static int sd_suspend_common(struct device
*dev
, bool runtime
)
4196 struct scsi_disk
*sdkp
= dev_get_drvdata(dev
);
4199 if (!sdkp
) /* E.g.: runtime suspend following sd_remove() */
4202 if (sdkp
->WCE
&& sdkp
->media_present
) {
4203 if (!sdkp
->device
->silence_suspend
)
4204 sd_printk(KERN_NOTICE
, sdkp
, "Synchronizing SCSI cache\n");
4205 ret
= sd_sync_cache(sdkp
);
4206 /* ignore OFFLINE device */
4214 if (sd_do_start_stop(sdkp
->device
, runtime
)) {
4215 if (!sdkp
->device
->silence_suspend
)
4216 sd_printk(KERN_NOTICE
, sdkp
, "Stopping disk\n");
4217 /* an error is not worth aborting a system sleep */
4218 ret
= sd_start_stop_device(sdkp
, 0);
4224 sdkp
->suspended
= true;
4229 static int sd_suspend_system(struct device
*dev
)
4231 if (pm_runtime_suspended(dev
))
4234 return sd_suspend_common(dev
, false);
4237 static int sd_suspend_runtime(struct device
*dev
)
4239 return sd_suspend_common(dev
, true);
4242 static int sd_resume(struct device
*dev
)
4244 struct scsi_disk
*sdkp
= dev_get_drvdata(dev
);
4246 sd_printk(KERN_NOTICE
, sdkp
, "Starting disk\n");
4248 if (opal_unlock_from_suspend(sdkp
->opal_dev
)) {
4249 sd_printk(KERN_NOTICE
, sdkp
, "OPAL unlock failed\n");
4256 static int sd_resume_common(struct device
*dev
, bool runtime
)
4258 struct scsi_disk
*sdkp
= dev_get_drvdata(dev
);
4261 if (!sdkp
) /* E.g.: runtime resume at the start of sd_probe() */
4264 if (!sd_do_start_stop(sdkp
->device
, runtime
)) {
4265 sdkp
->suspended
= false;
4269 sd_printk(KERN_NOTICE
, sdkp
, "Starting disk\n");
4270 ret
= sd_start_stop_device(sdkp
, 1);
4273 sdkp
->suspended
= false;
4279 static int sd_resume_system(struct device
*dev
)
4281 if (pm_runtime_suspended(dev
)) {
4282 struct scsi_disk
*sdkp
= dev_get_drvdata(dev
);
4283 struct scsi_device
*sdp
= sdkp
? sdkp
->device
: NULL
;
4285 if (sdp
&& sdp
->force_runtime_start_on_system_start
)
4286 pm_request_resume(dev
);
4291 return sd_resume_common(dev
, false);
4294 static int sd_resume_runtime(struct device
*dev
)
4296 struct scsi_disk
*sdkp
= dev_get_drvdata(dev
);
4297 struct scsi_device
*sdp
;
4299 if (!sdkp
) /* E.g.: runtime resume at the start of sd_probe() */
4304 if (sdp
->ignore_media_change
) {
4305 /* clear the device's sense data */
4306 static const u8 cmd
[10] = { REQUEST_SENSE
};
4307 const struct scsi_exec_args exec_args
= {
4308 .req_flags
= BLK_MQ_REQ_PM
,
4311 if (scsi_execute_cmd(sdp
, cmd
, REQ_OP_DRV_IN
, NULL
, 0,
4312 sdp
->request_queue
->rq_timeout
, 1,
4314 sd_printk(KERN_NOTICE
, sdkp
,
4315 "Failed to clear sense data\n");
4318 return sd_resume_common(dev
, true);
4321 static const struct dev_pm_ops sd_pm_ops
= {
4322 .suspend
= sd_suspend_system
,
4323 .resume
= sd_resume_system
,
4324 .poweroff
= sd_suspend_system
,
4325 .restore
= sd_resume_system
,
4326 .runtime_suspend
= sd_suspend_runtime
,
4327 .runtime_resume
= sd_resume_runtime
,
4330 static struct scsi_driver sd_template
= {
4334 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
4335 .remove
= sd_remove
,
4336 .shutdown
= sd_shutdown
,
4339 .rescan
= sd_rescan
,
4340 .resume
= sd_resume
,
4341 .init_command
= sd_init_command
,
4342 .uninit_command
= sd_uninit_command
,
4344 .eh_action
= sd_eh_action
,
4345 .eh_reset
= sd_eh_reset
,
4349 * init_sd - entry point for this driver (both when built in or when
4352 * Note: this function registers this driver with the scsi mid-level.
4354 static int __init
init_sd(void)
4356 int majors
= 0, i
, err
;
4358 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
4360 for (i
= 0; i
< SD_MAJORS
; i
++) {
4361 if (__register_blkdev(sd_major(i
), "sd", sd_default_probe
))
4369 err
= class_register(&sd_disk_class
);
4373 sd_page_pool
= mempool_create_page_pool(SD_MEMPOOL_SIZE
, 0);
4374 if (!sd_page_pool
) {
4375 printk(KERN_ERR
"sd: can't init discard page pool\n");
4380 err
= scsi_register_driver(&sd_template
.gendrv
);
4382 goto err_out_driver
;
4387 mempool_destroy(sd_page_pool
);
4389 class_unregister(&sd_disk_class
);
4391 for (i
= 0; i
< SD_MAJORS
; i
++)
4392 unregister_blkdev(sd_major(i
), "sd");
4397 * exit_sd - exit point for this driver (when it is a module).
4399 * Note: this function unregisters this driver from the scsi mid-level.
4401 static void __exit
exit_sd(void)
4405 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
4407 scsi_unregister_driver(&sd_template
.gendrv
);
4408 mempool_destroy(sd_page_pool
);
4410 class_unregister(&sd_disk_class
);
4412 for (i
= 0; i
< SD_MAJORS
; i
++)
4413 unregister_blkdev(sd_major(i
), "sd");
4416 module_init(init_sd
);
4417 module_exit(exit_sd
);
4419 void sd_print_sense_hdr(struct scsi_disk
*sdkp
, struct scsi_sense_hdr
*sshdr
)
4421 scsi_print_sense_hdr(sdkp
->device
,
4422 sdkp
->disk
? sdkp
->disk
->disk_name
: NULL
, sshdr
);
4425 void sd_print_result(const struct scsi_disk
*sdkp
, const char *msg
, int result
)
4427 const char *hb_string
= scsi_hostbyte_string(result
);
4430 sd_printk(KERN_INFO
, sdkp
,
4431 "%s: Result: hostbyte=%s driverbyte=%s\n", msg
,
4432 hb_string
? hb_string
: "invalid",
4435 sd_printk(KERN_INFO
, sdkp
,
4436 "%s: Result: hostbyte=0x%02x driverbyte=%s\n",
4437 msg
, host_byte(result
), "DRIVER_OK");