Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * sd.c Copyright (C) 1992 Drew Eckhardt |
| 3 | * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale |
| 4 | * |
| 5 | * Linux scsi disk driver |
| 6 | * Initial versions: Drew Eckhardt |
| 7 | * Subsequent revisions: Eric Youngdale |
| 8 | * Modification history: |
| 9 | * - Drew Eckhardt <drew@colorado.edu> original |
| 10 | * - Eric Youngdale <eric@andante.org> add scatter-gather, multiple |
| 11 | * outstanding request, and other enhancements. |
| 12 | * Support loadable low-level scsi drivers. |
| 13 | * - Jirka Hanika <geo@ff.cuni.cz> support more scsi disks using |
| 14 | * eight major numbers. |
| 15 | * - Richard Gooch <rgooch@atnf.csiro.au> support devfs. |
| 16 | * - Torben Mathiasen <tmm@image.dk> Resource allocation fixes in |
| 17 | * sd_init and cleanups. |
| 18 | * - Alex Davis <letmein@erols.com> Fix problem where partition info |
| 19 | * not being read in sd_open. Fix problem where removable media |
| 20 | * could be ejected after sd_open. |
| 21 | * - Douglas Gilbert <dgilbert@interlog.com> cleanup for lk 2.5.x |
| 22 | * - Badari Pulavarty <pbadari@us.ibm.com>, Matthew Wilcox |
| 23 | * <willy@debian.org>, Kurt Garloff <garloff@suse.de>: |
| 24 | * Support 32k/1M disks. |
| 25 | * |
| 26 | * Logging policy (needs CONFIG_SCSI_LOGGING defined): |
| 27 | * - setting up transfer: SCSI_LOG_HLQUEUE levels 1 and 2 |
| 28 | * - end of transfer (bh + scsi_lib): SCSI_LOG_HLCOMPLETE level 1 |
| 29 | * - entering sd_ioctl: SCSI_LOG_IOCTL level 1 |
| 30 | * - entering other commands: SCSI_LOG_HLQUEUE level 3 |
| 31 | * Note: when the logging level is set by the user, it must be greater |
| 32 | * than the level indicated above to trigger output. |
| 33 | */ |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/module.h> |
| 36 | #include <linux/fs.h> |
| 37 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/mm.h> |
| 39 | #include <linux/bio.h> |
| 40 | #include <linux/genhd.h> |
| 41 | #include <linux/hdreg.h> |
| 42 | #include <linux/errno.h> |
| 43 | #include <linux/idr.h> |
| 44 | #include <linux/interrupt.h> |
| 45 | #include <linux/init.h> |
| 46 | #include <linux/blkdev.h> |
| 47 | #include <linux/blkpg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <linux/delay.h> |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 49 | #include <linux/mutex.h> |
James Bottomley | 7404ad3b | 2008-08-31 10:41:52 -0500 | [diff] [blame] | 50 | #include <linux/string_helpers.h> |
Arjan van de Ven | 4ace92f | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 51 | #include <linux/async.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 52 | #include <linux/slab.h> |
Christoph Hellwig | d80210f | 2017-06-19 14:26:46 +0200 | [diff] [blame] | 53 | #include <linux/sed-opal.h> |
Lin Ming | 54f57588 | 2011-12-05 09:20:26 +0800 | [diff] [blame] | 54 | #include <linux/pm_runtime.h> |
Christoph Hellwig | 924d55b | 2015-10-15 14:10:49 +0200 | [diff] [blame] | 55 | #include <linux/pr.h> |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 56 | #include <linux/t10-pi.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 57 | #include <linux/uaccess.h> |
Dave Hansen | 8f76d15 | 2009-04-21 16:43:27 -0700 | [diff] [blame] | 58 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | #include <scsi/scsi.h> |
| 61 | #include <scsi/scsi_cmnd.h> |
| 62 | #include <scsi/scsi_dbg.h> |
| 63 | #include <scsi/scsi_device.h> |
| 64 | #include <scsi/scsi_driver.h> |
| 65 | #include <scsi/scsi_eh.h> |
| 66 | #include <scsi/scsi_host.h> |
| 67 | #include <scsi/scsi_ioctl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #include <scsi/scsicam.h> |
| 69 | |
Martin K. Petersen | aa91696 | 2008-06-17 12:47:32 -0400 | [diff] [blame] | 70 | #include "sd.h" |
Dan Williams | a7a20d1 | 2012-03-22 17:05:11 -0700 | [diff] [blame] | 71 | #include "scsi_priv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #include "scsi_logging.h" |
| 73 | |
Rene Herman | f018fa5 | 2006-03-08 00:14:20 -0800 | [diff] [blame] | 74 | MODULE_AUTHOR("Eric Youngdale"); |
| 75 | MODULE_DESCRIPTION("SCSI disk (sd) driver"); |
| 76 | MODULE_LICENSE("GPL"); |
| 77 | |
| 78 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR); |
| 79 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR); |
| 80 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR); |
| 81 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR); |
| 82 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR); |
| 83 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR); |
| 84 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR); |
| 85 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR); |
| 86 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR); |
| 87 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR); |
| 88 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR); |
| 89 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR); |
| 90 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR); |
| 91 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR); |
| 92 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR); |
| 93 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR); |
Michael Tokarev | d7b8bcb | 2006-10-27 16:02:37 +0400 | [diff] [blame] | 94 | MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK); |
| 95 | MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD); |
| 96 | MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 97 | MODULE_ALIAS_SCSI_DEVICE(TYPE_ZBC); |
Rene Herman | f018fa5 | 2006-03-08 00:14:20 -0800 | [diff] [blame] | 98 | |
Tejun Heo | 870d665 | 2008-08-25 19:47:25 +0900 | [diff] [blame] | 99 | #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT) |
Tejun Heo | f615b48 | 2008-08-25 19:47:24 +0900 | [diff] [blame] | 100 | #define SD_MINORS 16 |
Tejun Heo | 870d665 | 2008-08-25 19:47:25 +0900 | [diff] [blame] | 101 | #else |
Tejun Heo | 3e1a7ff | 2008-08-25 19:56:17 +0900 | [diff] [blame] | 102 | #define SD_MINORS 0 |
Tejun Heo | 870d665 | 2008-08-25 19:47:25 +0900 | [diff] [blame] | 103 | #endif |
| 104 | |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 105 | static void sd_config_discard(struct scsi_disk *, unsigned int); |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 106 | static void sd_config_write_same(struct scsi_disk *); |
Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 107 | static int sd_revalidate_disk(struct gendisk *); |
Tejun Heo | 72ec24b | 2010-05-15 20:09:32 +0200 | [diff] [blame] | 108 | static void sd_unlock_native_capacity(struct gendisk *disk); |
Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 109 | static int sd_probe(struct device *); |
| 110 | static int sd_remove(struct device *); |
| 111 | static void sd_shutdown(struct device *); |
Oliver Neukum | 9589791 | 2013-09-16 13:28:15 +0200 | [diff] [blame] | 112 | static int sd_suspend_system(struct device *); |
| 113 | static int sd_suspend_runtime(struct device *); |
Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 114 | static int sd_resume(struct device *); |
| 115 | static void sd_rescan(struct device *); |
Christoph Hellwig | a1b73fc | 2014-05-01 16:51:04 +0200 | [diff] [blame] | 116 | static int sd_init_command(struct scsi_cmnd *SCpnt); |
| 117 | static void sd_uninit_command(struct scsi_cmnd *SCpnt); |
Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 118 | static int sd_done(struct scsi_cmnd *); |
Hannes Reinecke | 7a38dc0 | 2017-04-06 15:36:29 +0200 | [diff] [blame] | 119 | static void sd_eh_reset(struct scsi_cmnd *); |
James Bottomley | 2451079 | 2013-11-11 13:44:53 +0100 | [diff] [blame] | 120 | static int sd_eh_action(struct scsi_cmnd *, int); |
Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 121 | static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 122 | static void scsi_disk_release(struct device *cdev); |
Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 123 | static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *); |
Hannes Reinecke | ef61329 | 2014-10-24 14:27:00 +0200 | [diff] [blame] | 124 | static void sd_print_result(const struct scsi_disk *, const char *, int); |
Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 125 | |
Tejun Heo | f27bac2 | 2008-07-14 14:59:30 +0900 | [diff] [blame] | 126 | static DEFINE_IDA(sd_index_ida); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | /* This semaphore is used to mediate the 0->1 reference get in the |
| 129 | * face of object destruction (i.e. we can't allow a get on an |
| 130 | * object after last put) */ |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 131 | static DEFINE_MUTEX(sd_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
H Hartley Sweeten | 439d77f | 2010-08-10 18:01:20 -0700 | [diff] [blame] | 133 | static struct kmem_cache *sd_cdb_cache; |
| 134 | static mempool_t *sd_cdb_pool; |
Jens Axboe | 024d515 | 2018-12-12 06:46:55 -0700 | [diff] [blame] | 135 | static mempool_t *sd_page_pool; |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 136 | |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 137 | static const char *sd_cache_types[] = { |
| 138 | "write through", "none", "write back", |
| 139 | "write back, no read (daft)" |
| 140 | }; |
| 141 | |
Vaughan Cao | cb2fb68 | 2014-06-03 17:37:30 +0800 | [diff] [blame] | 142 | static void sd_set_flush_flag(struct scsi_disk *sdkp) |
| 143 | { |
Jens Axboe | eb310e2 | 2016-03-30 10:06:11 -0600 | [diff] [blame] | 144 | bool wc = false, fua = false; |
Vaughan Cao | cb2fb68 | 2014-06-03 17:37:30 +0800 | [diff] [blame] | 145 | |
| 146 | if (sdkp->WCE) { |
Jens Axboe | eb310e2 | 2016-03-30 10:06:11 -0600 | [diff] [blame] | 147 | wc = true; |
Vaughan Cao | cb2fb68 | 2014-06-03 17:37:30 +0800 | [diff] [blame] | 148 | if (sdkp->DPOFUA) |
Jens Axboe | eb310e2 | 2016-03-30 10:06:11 -0600 | [diff] [blame] | 149 | fua = true; |
Vaughan Cao | cb2fb68 | 2014-06-03 17:37:30 +0800 | [diff] [blame] | 150 | } |
| 151 | |
Jens Axboe | eb310e2 | 2016-03-30 10:06:11 -0600 | [diff] [blame] | 152 | blk_queue_write_cache(sdkp->disk->queue, wc, fua); |
Vaughan Cao | cb2fb68 | 2014-06-03 17:37:30 +0800 | [diff] [blame] | 153 | } |
| 154 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 155 | static ssize_t |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 156 | cache_type_store(struct device *dev, struct device_attribute *attr, |
| 157 | const char *buf, size_t count) |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 158 | { |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 159 | int ct, rcd, wce, sp; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 160 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 161 | struct scsi_device *sdp = sdkp->device; |
| 162 | char buffer[64]; |
| 163 | char *buffer_data; |
| 164 | struct scsi_mode_data data; |
| 165 | struct scsi_sense_hdr sshdr; |
Ben Hutchings | 2ee3e26 | 2013-05-27 19:07:19 +0100 | [diff] [blame] | 166 | static const char temp[] = "temporary "; |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 167 | int len; |
| 168 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 169 | if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC) |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 170 | /* no cache control on RBC devices; theoretically they |
| 171 | * can do it, but there's probably so many exceptions |
| 172 | * it's not worth the risk */ |
| 173 | return -EINVAL; |
| 174 | |
James Bottomley | 39c60a0 | 2013-04-24 14:02:53 -0700 | [diff] [blame] | 175 | if (strncmp(buf, temp, sizeof(temp) - 1) == 0) { |
| 176 | buf += sizeof(temp) - 1; |
| 177 | sdkp->cache_override = 1; |
| 178 | } else { |
| 179 | sdkp->cache_override = 0; |
| 180 | } |
| 181 | |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 182 | ct = sysfs_match_string(sd_cache_types, buf); |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 183 | if (ct < 0) |
| 184 | return -EINVAL; |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 185 | |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 186 | rcd = ct & 0x01 ? 1 : 0; |
Sujit Reddy Thumma | 2eefd57 | 2014-08-11 15:40:37 +0300 | [diff] [blame] | 187 | wce = (ct & 0x02) && !sdkp->write_prot ? 1 : 0; |
James Bottomley | 39c60a0 | 2013-04-24 14:02:53 -0700 | [diff] [blame] | 188 | |
| 189 | if (sdkp->cache_override) { |
| 190 | sdkp->WCE = wce; |
| 191 | sdkp->RCD = rcd; |
Vaughan Cao | cb2fb68 | 2014-06-03 17:37:30 +0800 | [diff] [blame] | 192 | sd_set_flush_flag(sdkp); |
James Bottomley | 39c60a0 | 2013-04-24 14:02:53 -0700 | [diff] [blame] | 193 | return count; |
| 194 | } |
| 195 | |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 196 | if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT, |
| 197 | SD_MAX_RETRIES, &data, NULL)) |
| 198 | return -EINVAL; |
Andrew Morton | a9312fb | 2006-03-25 03:08:30 -0800 | [diff] [blame] | 199 | len = min_t(size_t, sizeof(buffer), data.length - data.header_length - |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 200 | data.block_descriptor_length); |
| 201 | buffer_data = buffer + data.header_length + |
| 202 | data.block_descriptor_length; |
| 203 | buffer_data[2] &= ~0x05; |
| 204 | buffer_data[2] |= wce << 2 | rcd; |
| 205 | sp = buffer_data[0] & 0x80 ? 1 : 0; |
Gabriel Krisman Bertazi | 2c5d16d | 2015-10-30 16:04:43 -0200 | [diff] [blame] | 206 | buffer_data[0] &= ~0x80; |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 207 | |
Ivan Mironov | 9d37f4a | 2018-12-23 12:41:58 +0500 | [diff] [blame] | 208 | /* |
| 209 | * Ensure WP, DPOFUA, and RESERVED fields are cleared in |
| 210 | * received mode parameter buffer before doing MODE SELECT. |
| 211 | */ |
| 212 | data.device_specific = 0; |
| 213 | |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 214 | if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT, |
| 215 | SD_MAX_RETRIES, &data, &sshdr)) { |
| 216 | if (scsi_sense_valid(&sshdr)) |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 217 | sd_print_sense_hdr(sdkp, &sshdr); |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 218 | return -EINVAL; |
| 219 | } |
Andrew Patterson | f98a8ca | 2008-09-04 14:27:35 -0600 | [diff] [blame] | 220 | revalidate_disk(sdkp->disk); |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 221 | return count; |
| 222 | } |
| 223 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 224 | static ssize_t |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 225 | manage_start_stop_show(struct device *dev, struct device_attribute *attr, |
| 226 | char *buf) |
| 227 | { |
| 228 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
| 229 | struct scsi_device *sdp = sdkp->device; |
| 230 | |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 231 | return sprintf(buf, "%u\n", sdp->manage_start_stop); |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static ssize_t |
| 235 | manage_start_stop_store(struct device *dev, struct device_attribute *attr, |
| 236 | const char *buf, size_t count) |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 237 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 238 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 239 | struct scsi_device *sdp = sdkp->device; |
weiping zhang | 623401e | 2017-10-12 14:57:06 +0800 | [diff] [blame] | 240 | bool v; |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 241 | |
| 242 | if (!capable(CAP_SYS_ADMIN)) |
| 243 | return -EACCES; |
| 244 | |
weiping zhang | 623401e | 2017-10-12 14:57:06 +0800 | [diff] [blame] | 245 | if (kstrtobool(buf, &v)) |
| 246 | return -EINVAL; |
| 247 | |
| 248 | sdp->manage_start_stop = v; |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 249 | |
| 250 | return count; |
| 251 | } |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 252 | static DEVICE_ATTR_RW(manage_start_stop); |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 253 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 254 | static ssize_t |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 255 | allow_restart_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 256 | { |
| 257 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
| 258 | |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 259 | return sprintf(buf, "%u\n", sdkp->device->allow_restart); |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | static ssize_t |
| 263 | allow_restart_store(struct device *dev, struct device_attribute *attr, |
| 264 | const char *buf, size_t count) |
Brian King | a144c5a | 2006-06-27 11:10:31 -0500 | [diff] [blame] | 265 | { |
weiping zhang | 658e9a6 | 2017-10-12 14:56:44 +0800 | [diff] [blame] | 266 | bool v; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 267 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
Brian King | a144c5a | 2006-06-27 11:10:31 -0500 | [diff] [blame] | 268 | struct scsi_device *sdp = sdkp->device; |
| 269 | |
| 270 | if (!capable(CAP_SYS_ADMIN)) |
| 271 | return -EACCES; |
| 272 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 273 | if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC) |
Brian King | a144c5a | 2006-06-27 11:10:31 -0500 | [diff] [blame] | 274 | return -EINVAL; |
| 275 | |
weiping zhang | 658e9a6 | 2017-10-12 14:56:44 +0800 | [diff] [blame] | 276 | if (kstrtobool(buf, &v)) |
| 277 | return -EINVAL; |
| 278 | |
| 279 | sdp->allow_restart = v; |
Brian King | a144c5a | 2006-06-27 11:10:31 -0500 | [diff] [blame] | 280 | |
| 281 | return count; |
| 282 | } |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 283 | static DEVICE_ATTR_RW(allow_restart); |
Brian King | a144c5a | 2006-06-27 11:10:31 -0500 | [diff] [blame] | 284 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 285 | static ssize_t |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 286 | cache_type_show(struct device *dev, struct device_attribute *attr, char *buf) |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 287 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 288 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 289 | int ct = sdkp->RCD + 2*sdkp->WCE; |
| 290 | |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 291 | return sprintf(buf, "%s\n", sd_cache_types[ct]); |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 292 | } |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 293 | static DEVICE_ATTR_RW(cache_type); |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 294 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 295 | static ssize_t |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 296 | FUA_show(struct device *dev, struct device_attribute *attr, char *buf) |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 297 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 298 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 299 | |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 300 | return sprintf(buf, "%u\n", sdkp->DPOFUA); |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 301 | } |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 302 | static DEVICE_ATTR_RO(FUA); |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 303 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 304 | static ssize_t |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 305 | protection_type_show(struct device *dev, struct device_attribute *attr, |
| 306 | char *buf) |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 307 | { |
| 308 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
| 309 | |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 310 | return sprintf(buf, "%u\n", sdkp->protection_type); |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static ssize_t |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 314 | protection_type_store(struct device *dev, struct device_attribute *attr, |
| 315 | const char *buf, size_t count) |
Martin K. Petersen | 8172499 | 2012-08-28 14:29:34 -0400 | [diff] [blame] | 316 | { |
| 317 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
| 318 | unsigned int val; |
| 319 | int err; |
| 320 | |
| 321 | if (!capable(CAP_SYS_ADMIN)) |
| 322 | return -EACCES; |
| 323 | |
| 324 | err = kstrtouint(buf, 10, &val); |
| 325 | |
| 326 | if (err) |
| 327 | return err; |
| 328 | |
Bart Van Assche | 830cc35 | 2017-08-25 13:46:35 -0700 | [diff] [blame] | 329 | if (val <= T10_PI_TYPE3_PROTECTION) |
Martin K. Petersen | 8172499 | 2012-08-28 14:29:34 -0400 | [diff] [blame] | 330 | sdkp->protection_type = val; |
| 331 | |
| 332 | return count; |
| 333 | } |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 334 | static DEVICE_ATTR_RW(protection_type); |
Martin K. Petersen | 8172499 | 2012-08-28 14:29:34 -0400 | [diff] [blame] | 335 | |
| 336 | static ssize_t |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 337 | protection_mode_show(struct device *dev, struct device_attribute *attr, |
| 338 | char *buf) |
Martin K. Petersen | 518fa8e | 2010-10-08 01:36:24 -0400 | [diff] [blame] | 339 | { |
| 340 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
| 341 | struct scsi_device *sdp = sdkp->device; |
| 342 | unsigned int dif, dix; |
| 343 | |
| 344 | dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type); |
| 345 | dix = scsi_host_dix_capable(sdp->host, sdkp->protection_type); |
| 346 | |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 347 | if (!dix && scsi_host_dix_capable(sdp->host, T10_PI_TYPE0_PROTECTION)) { |
Martin K. Petersen | 518fa8e | 2010-10-08 01:36:24 -0400 | [diff] [blame] | 348 | dif = 0; |
| 349 | dix = 1; |
| 350 | } |
| 351 | |
| 352 | if (!dif && !dix) |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 353 | return sprintf(buf, "none\n"); |
Martin K. Petersen | 518fa8e | 2010-10-08 01:36:24 -0400 | [diff] [blame] | 354 | |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 355 | return sprintf(buf, "%s%u\n", dix ? "dix" : "dif", dif); |
Martin K. Petersen | 518fa8e | 2010-10-08 01:36:24 -0400 | [diff] [blame] | 356 | } |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 357 | static DEVICE_ATTR_RO(protection_mode); |
Martin K. Petersen | 518fa8e | 2010-10-08 01:36:24 -0400 | [diff] [blame] | 358 | |
| 359 | static ssize_t |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 360 | app_tag_own_show(struct device *dev, struct device_attribute *attr, char *buf) |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 361 | { |
| 362 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
| 363 | |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 364 | return sprintf(buf, "%u\n", sdkp->ATO); |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 365 | } |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 366 | static DEVICE_ATTR_RO(app_tag_own); |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 367 | |
Martin K. Petersen | e339c1a | 2009-11-26 12:00:40 -0500 | [diff] [blame] | 368 | static ssize_t |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 369 | thin_provisioning_show(struct device *dev, struct device_attribute *attr, |
| 370 | char *buf) |
Martin K. Petersen | e339c1a | 2009-11-26 12:00:40 -0500 | [diff] [blame] | 371 | { |
| 372 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
| 373 | |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 374 | return sprintf(buf, "%u\n", sdkp->lbpme); |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 375 | } |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 376 | static DEVICE_ATTR_RO(thin_provisioning); |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 377 | |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 378 | /* sysfs_match_string() requires dense arrays */ |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 379 | static const char *lbp_mode[] = { |
| 380 | [SD_LBP_FULL] = "full", |
| 381 | [SD_LBP_UNMAP] = "unmap", |
| 382 | [SD_LBP_WS16] = "writesame_16", |
| 383 | [SD_LBP_WS10] = "writesame_10", |
| 384 | [SD_LBP_ZERO] = "writesame_zero", |
| 385 | [SD_LBP_DISABLE] = "disabled", |
| 386 | }; |
| 387 | |
| 388 | static ssize_t |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 389 | provisioning_mode_show(struct device *dev, struct device_attribute *attr, |
| 390 | char *buf) |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 391 | { |
| 392 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
| 393 | |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 394 | return sprintf(buf, "%s\n", lbp_mode[sdkp->provisioning_mode]); |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | static ssize_t |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 398 | provisioning_mode_store(struct device *dev, struct device_attribute *attr, |
| 399 | const char *buf, size_t count) |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 400 | { |
| 401 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
| 402 | struct scsi_device *sdp = sdkp->device; |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 403 | int mode; |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 404 | |
| 405 | if (!capable(CAP_SYS_ADMIN)) |
| 406 | return -EACCES; |
| 407 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 408 | if (sd_is_zoned(sdkp)) { |
| 409 | sd_config_discard(sdkp, SD_LBP_DISABLE); |
| 410 | return count; |
| 411 | } |
| 412 | |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 413 | if (sdp->type != TYPE_DISK) |
| 414 | return -EINVAL; |
| 415 | |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 416 | mode = sysfs_match_string(lbp_mode, buf); |
| 417 | if (mode < 0) |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 418 | return -EINVAL; |
| 419 | |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 420 | sd_config_discard(sdkp, mode); |
| 421 | |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 422 | return count; |
Martin K. Petersen | e339c1a | 2009-11-26 12:00:40 -0500 | [diff] [blame] | 423 | } |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 424 | static DEVICE_ATTR_RW(provisioning_mode); |
Martin K. Petersen | e339c1a | 2009-11-26 12:00:40 -0500 | [diff] [blame] | 425 | |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 426 | /* sysfs_match_string() requires dense arrays */ |
Martin K. Petersen | e6bd931 | 2017-04-05 19:21:24 +0200 | [diff] [blame] | 427 | static const char *zeroing_mode[] = { |
| 428 | [SD_ZERO_WRITE] = "write", |
| 429 | [SD_ZERO_WS] = "writesame", |
| 430 | [SD_ZERO_WS16_UNMAP] = "writesame_16_unmap", |
| 431 | [SD_ZERO_WS10_UNMAP] = "writesame_10_unmap", |
| 432 | }; |
| 433 | |
| 434 | static ssize_t |
| 435 | zeroing_mode_show(struct device *dev, struct device_attribute *attr, |
| 436 | char *buf) |
| 437 | { |
| 438 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
| 439 | |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 440 | return sprintf(buf, "%s\n", zeroing_mode[sdkp->zeroing_mode]); |
Martin K. Petersen | e6bd931 | 2017-04-05 19:21:24 +0200 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | static ssize_t |
| 444 | zeroing_mode_store(struct device *dev, struct device_attribute *attr, |
| 445 | const char *buf, size_t count) |
| 446 | { |
| 447 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 448 | int mode; |
Martin K. Petersen | e6bd931 | 2017-04-05 19:21:24 +0200 | [diff] [blame] | 449 | |
| 450 | if (!capable(CAP_SYS_ADMIN)) |
| 451 | return -EACCES; |
| 452 | |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 453 | mode = sysfs_match_string(zeroing_mode, buf); |
| 454 | if (mode < 0) |
Martin K. Petersen | e6bd931 | 2017-04-05 19:21:24 +0200 | [diff] [blame] | 455 | return -EINVAL; |
| 456 | |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 457 | sdkp->zeroing_mode = mode; |
| 458 | |
Martin K. Petersen | e6bd931 | 2017-04-05 19:21:24 +0200 | [diff] [blame] | 459 | return count; |
| 460 | } |
| 461 | static DEVICE_ATTR_RW(zeroing_mode); |
| 462 | |
Martin K. Petersen | 18a4d0a | 2012-02-09 13:48:53 -0500 | [diff] [blame] | 463 | static ssize_t |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 464 | max_medium_access_timeouts_show(struct device *dev, |
| 465 | struct device_attribute *attr, char *buf) |
Martin K. Petersen | 18a4d0a | 2012-02-09 13:48:53 -0500 | [diff] [blame] | 466 | { |
| 467 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
| 468 | |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 469 | return sprintf(buf, "%u\n", sdkp->max_medium_access_timeouts); |
Martin K. Petersen | 18a4d0a | 2012-02-09 13:48:53 -0500 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | static ssize_t |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 473 | max_medium_access_timeouts_store(struct device *dev, |
| 474 | struct device_attribute *attr, const char *buf, |
| 475 | size_t count) |
Martin K. Petersen | 18a4d0a | 2012-02-09 13:48:53 -0500 | [diff] [blame] | 476 | { |
| 477 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
| 478 | int err; |
| 479 | |
| 480 | if (!capable(CAP_SYS_ADMIN)) |
| 481 | return -EACCES; |
| 482 | |
| 483 | err = kstrtouint(buf, 10, &sdkp->max_medium_access_timeouts); |
| 484 | |
| 485 | return err ? err : count; |
| 486 | } |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 487 | static DEVICE_ATTR_RW(max_medium_access_timeouts); |
Martin K. Petersen | 18a4d0a | 2012-02-09 13:48:53 -0500 | [diff] [blame] | 488 | |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 489 | static ssize_t |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 490 | max_write_same_blocks_show(struct device *dev, struct device_attribute *attr, |
| 491 | char *buf) |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 492 | { |
| 493 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
| 494 | |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 495 | return sprintf(buf, "%u\n", sdkp->max_ws_blocks); |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | static ssize_t |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 499 | max_write_same_blocks_store(struct device *dev, struct device_attribute *attr, |
| 500 | const char *buf, size_t count) |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 501 | { |
| 502 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
| 503 | struct scsi_device *sdp = sdkp->device; |
| 504 | unsigned long max; |
| 505 | int err; |
| 506 | |
| 507 | if (!capable(CAP_SYS_ADMIN)) |
| 508 | return -EACCES; |
| 509 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 510 | if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC) |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 511 | return -EINVAL; |
| 512 | |
| 513 | err = kstrtoul(buf, 10, &max); |
| 514 | |
| 515 | if (err) |
| 516 | return err; |
| 517 | |
| 518 | if (max == 0) |
| 519 | sdp->no_write_same = 1; |
Martin K. Petersen | 66c28f9 | 2013-06-06 22:15:55 -0400 | [diff] [blame] | 520 | else if (max <= SD_MAX_WS16_BLOCKS) { |
| 521 | sdp->no_write_same = 0; |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 522 | sdkp->max_ws_blocks = max; |
Martin K. Petersen | 66c28f9 | 2013-06-06 22:15:55 -0400 | [diff] [blame] | 523 | } |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 524 | |
| 525 | sd_config_write_same(sdkp); |
| 526 | |
| 527 | return count; |
| 528 | } |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 529 | static DEVICE_ATTR_RW(max_write_same_blocks); |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 530 | |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 531 | static struct attribute *sd_disk_attrs[] = { |
| 532 | &dev_attr_cache_type.attr, |
| 533 | &dev_attr_FUA.attr, |
| 534 | &dev_attr_allow_restart.attr, |
| 535 | &dev_attr_manage_start_stop.attr, |
| 536 | &dev_attr_protection_type.attr, |
| 537 | &dev_attr_protection_mode.attr, |
| 538 | &dev_attr_app_tag_own.attr, |
| 539 | &dev_attr_thin_provisioning.attr, |
| 540 | &dev_attr_provisioning_mode.attr, |
Martin K. Petersen | e6bd931 | 2017-04-05 19:21:24 +0200 | [diff] [blame] | 541 | &dev_attr_zeroing_mode.attr, |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 542 | &dev_attr_max_write_same_blocks.attr, |
| 543 | &dev_attr_max_medium_access_timeouts.attr, |
| 544 | NULL, |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 545 | }; |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 546 | ATTRIBUTE_GROUPS(sd_disk); |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 547 | |
| 548 | static struct class sd_disk_class = { |
| 549 | .name = "scsi_disk", |
| 550 | .owner = THIS_MODULE, |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 551 | .dev_release = scsi_disk_release, |
Greg Kroah-Hartman | e1ea235 | 2013-07-24 15:05:28 -0700 | [diff] [blame] | 552 | .dev_groups = sd_disk_groups, |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 553 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | |
Aaron Lu | 691e3d3 | 2012-11-09 15:27:55 +0800 | [diff] [blame] | 555 | static const struct dev_pm_ops sd_pm_ops = { |
Oliver Neukum | 9589791 | 2013-09-16 13:28:15 +0200 | [diff] [blame] | 556 | .suspend = sd_suspend_system, |
Aaron Lu | 691e3d3 | 2012-11-09 15:27:55 +0800 | [diff] [blame] | 557 | .resume = sd_resume, |
Oliver Neukum | 9589791 | 2013-09-16 13:28:15 +0200 | [diff] [blame] | 558 | .poweroff = sd_suspend_system, |
Aaron Lu | 691e3d3 | 2012-11-09 15:27:55 +0800 | [diff] [blame] | 559 | .restore = sd_resume, |
Oliver Neukum | 9589791 | 2013-09-16 13:28:15 +0200 | [diff] [blame] | 560 | .runtime_suspend = sd_suspend_runtime, |
Aaron Lu | 691e3d3 | 2012-11-09 15:27:55 +0800 | [diff] [blame] | 561 | .runtime_resume = sd_resume, |
| 562 | }; |
| 563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | static struct scsi_driver sd_template = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | .gendrv = { |
| 566 | .name = "sd", |
Christoph Hellwig | 3af6b35 | 2014-11-12 18:34:51 +0100 | [diff] [blame] | 567 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | .probe = sd_probe, |
| 569 | .remove = sd_remove, |
| 570 | .shutdown = sd_shutdown, |
Aaron Lu | 691e3d3 | 2012-11-09 15:27:55 +0800 | [diff] [blame] | 571 | .pm = &sd_pm_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | }, |
| 573 | .rescan = sd_rescan, |
Christoph Hellwig | a1b73fc | 2014-05-01 16:51:04 +0200 | [diff] [blame] | 574 | .init_command = sd_init_command, |
| 575 | .uninit_command = sd_uninit_command, |
Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 576 | .done = sd_done, |
Martin K. Petersen | 18a4d0a | 2012-02-09 13:48:53 -0500 | [diff] [blame] | 577 | .eh_action = sd_eh_action, |
Hannes Reinecke | 7a38dc0 | 2017-04-06 15:36:29 +0200 | [diff] [blame] | 578 | .eh_reset = sd_eh_reset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | }; |
| 580 | |
| 581 | /* |
Hannes Reinecke | 0761df9 | 2013-05-10 11:06:16 +0200 | [diff] [blame] | 582 | * Dummy kobj_map->probe function. |
| 583 | * The default ->probe function will call modprobe, which is |
| 584 | * pointless as this module is already loaded. |
| 585 | */ |
| 586 | static struct kobject *sd_default_probe(dev_t devt, int *partno, void *data) |
| 587 | { |
| 588 | return NULL; |
| 589 | } |
| 590 | |
| 591 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | * Device no to disk mapping: |
| 593 | * |
| 594 | * major disc2 disc p1 |
| 595 | * |............|.............|....|....| <- dev_t |
| 596 | * 31 20 19 8 7 4 3 0 |
| 597 | * |
| 598 | * Inside a major, we have 16k disks, however mapped non- |
| 599 | * contiguously. The first 16 disks are for major0, the next |
| 600 | * ones with major1, ... Disk 256 is for major0 again, disk 272 |
| 601 | * for major1, ... |
| 602 | * As we stay compatible with our numbering scheme, we can reuse |
| 603 | * the well-know SCSI majors 8, 65--71, 136--143. |
| 604 | */ |
| 605 | static int sd_major(int major_idx) |
| 606 | { |
| 607 | switch (major_idx) { |
| 608 | case 0: |
| 609 | return SCSI_DISK0_MAJOR; |
| 610 | case 1 ... 7: |
| 611 | return SCSI_DISK1_MAJOR + major_idx - 1; |
| 612 | case 8 ... 15: |
| 613 | return SCSI_DISK8_MAJOR + major_idx - 8; |
| 614 | default: |
| 615 | BUG(); |
| 616 | return 0; /* shut up gcc */ |
| 617 | } |
| 618 | } |
| 619 | |
Christoph Hellwig | 3d9a1f5 | 2015-02-02 14:01:25 +0100 | [diff] [blame] | 620 | static struct scsi_disk *scsi_disk_get(struct gendisk *disk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | { |
| 622 | struct scsi_disk *sdkp = NULL; |
| 623 | |
Christoph Hellwig | 3d9a1f5 | 2015-02-02 14:01:25 +0100 | [diff] [blame] | 624 | mutex_lock(&sd_ref_mutex); |
| 625 | |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 626 | if (disk->private_data) { |
| 627 | sdkp = scsi_disk(disk); |
| 628 | if (scsi_device_get(sdkp->device) == 0) |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 629 | get_device(&sdkp->dev); |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 630 | else |
| 631 | sdkp = NULL; |
| 632 | } |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 633 | mutex_unlock(&sd_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | return sdkp; |
| 635 | } |
| 636 | |
| 637 | static void scsi_disk_put(struct scsi_disk *sdkp) |
| 638 | { |
| 639 | struct scsi_device *sdev = sdkp->device; |
| 640 | |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 641 | mutex_lock(&sd_ref_mutex); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 642 | put_device(&sdkp->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | scsi_device_put(sdev); |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 644 | mutex_unlock(&sd_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Christoph Hellwig | d80210f | 2017-06-19 14:26:46 +0200 | [diff] [blame] | 647 | #ifdef CONFIG_BLK_SED_OPAL |
| 648 | static int sd_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, |
| 649 | size_t len, bool send) |
| 650 | { |
| 651 | struct scsi_device *sdev = data; |
| 652 | u8 cdb[12] = { 0, }; |
| 653 | int ret; |
| 654 | |
| 655 | cdb[0] = send ? SECURITY_PROTOCOL_OUT : SECURITY_PROTOCOL_IN; |
| 656 | cdb[1] = secp; |
| 657 | put_unaligned_be16(spsp, &cdb[2]); |
| 658 | put_unaligned_be32(len, &cdb[6]); |
| 659 | |
| 660 | ret = scsi_execute_req(sdev, cdb, |
| 661 | send ? DMA_TO_DEVICE : DMA_FROM_DEVICE, |
| 662 | buffer, len, NULL, SD_TIMEOUT, SD_MAX_RETRIES, NULL); |
| 663 | return ret <= 0 ? ret : -EIO; |
| 664 | } |
| 665 | #endif /* CONFIG_BLK_SED_OPAL */ |
| 666 | |
Martin K. Petersen | c611529 | 2014-09-26 19:20:08 -0400 | [diff] [blame] | 667 | static unsigned char sd_setup_protect_cmnd(struct scsi_cmnd *scmd, |
| 668 | unsigned int dix, unsigned int dif) |
| 669 | { |
| 670 | struct bio *bio = scmd->request->bio; |
| 671 | unsigned int prot_op = sd_prot_op(rq_data_dir(scmd->request), dix, dif); |
| 672 | unsigned int protect = 0; |
| 673 | |
| 674 | if (dix) { /* DIX Type 0, 1, 2, 3 */ |
| 675 | if (bio_integrity_flagged(bio, BIP_IP_CHECKSUM)) |
| 676 | scmd->prot_flags |= SCSI_PROT_IP_CHECKSUM; |
| 677 | |
| 678 | if (bio_integrity_flagged(bio, BIP_CTRL_NOCHECK) == false) |
| 679 | scmd->prot_flags |= SCSI_PROT_GUARD_CHECK; |
| 680 | } |
| 681 | |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 682 | if (dif != T10_PI_TYPE3_PROTECTION) { /* DIX/DIF Type 0, 1, 2 */ |
Martin K. Petersen | c611529 | 2014-09-26 19:20:08 -0400 | [diff] [blame] | 683 | scmd->prot_flags |= SCSI_PROT_REF_INCREMENT; |
| 684 | |
| 685 | if (bio_integrity_flagged(bio, BIP_CTRL_NOCHECK) == false) |
| 686 | scmd->prot_flags |= SCSI_PROT_REF_CHECK; |
| 687 | } |
| 688 | |
| 689 | if (dif) { /* DIX/DIF Type 1, 2, 3 */ |
| 690 | scmd->prot_flags |= SCSI_PROT_TRANSFER_PI; |
| 691 | |
| 692 | if (bio_integrity_flagged(bio, BIP_DISK_NOCHECK)) |
| 693 | protect = 3 << 5; /* Disable target PI checking */ |
| 694 | else |
| 695 | protect = 1 << 5; /* Enable target PI checking */ |
Martin K. Petersen | 35e1a5d | 2009-09-18 17:33:00 -0400 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | scsi_set_prot_op(scmd, prot_op); |
| 699 | scsi_set_prot_type(scmd, dif); |
Martin K. Petersen | c611529 | 2014-09-26 19:20:08 -0400 | [diff] [blame] | 700 | scmd->prot_flags &= sd_prot_flag_mask(prot_op); |
| 701 | |
| 702 | return protect; |
Martin K. Petersen | 35e1a5d | 2009-09-18 17:33:00 -0400 | [diff] [blame] | 703 | } |
| 704 | |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 705 | static void sd_config_discard(struct scsi_disk *sdkp, unsigned int mode) |
| 706 | { |
| 707 | struct request_queue *q = sdkp->disk->queue; |
| 708 | unsigned int logical_block_size = sdkp->device->sector_size; |
| 709 | unsigned int max_blocks = 0; |
| 710 | |
Martin K. Petersen | bcd069b | 2017-04-05 19:21:25 +0200 | [diff] [blame] | 711 | q->limits.discard_alignment = |
| 712 | sdkp->unmap_alignment * logical_block_size; |
| 713 | q->limits.discard_granularity = |
| 714 | max(sdkp->physical_block_size, |
| 715 | sdkp->unmap_granularity * logical_block_size); |
Martin K. Petersen | 8973039 | 2012-02-13 15:39:00 -0500 | [diff] [blame] | 716 | sdkp->provisioning_mode = mode; |
| 717 | |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 718 | switch (mode) { |
| 719 | |
Martin K. Petersen | 4c11712 | 2017-05-25 09:34:30 -0400 | [diff] [blame] | 720 | case SD_LBP_FULL: |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 721 | case SD_LBP_DISABLE: |
Jens Axboe | 2bb4cd5 | 2015-07-14 08:15:12 -0600 | [diff] [blame] | 722 | blk_queue_max_discard_sectors(q, 0); |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 723 | blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q); |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 724 | return; |
| 725 | |
| 726 | case SD_LBP_UNMAP: |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 727 | max_blocks = min_not_zero(sdkp->max_unmap_blocks, |
| 728 | (u32)SD_MAX_WS16_BLOCKS); |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 729 | break; |
| 730 | |
| 731 | case SD_LBP_WS16: |
Martin K. Petersen | 28a0bc4 | 2017-09-27 21:35:12 -0400 | [diff] [blame] | 732 | if (sdkp->device->unmap_limit_for_ws) |
| 733 | max_blocks = sdkp->max_unmap_blocks; |
| 734 | else |
| 735 | max_blocks = sdkp->max_ws_blocks; |
| 736 | |
| 737 | max_blocks = min_not_zero(max_blocks, (u32)SD_MAX_WS16_BLOCKS); |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 738 | break; |
| 739 | |
| 740 | case SD_LBP_WS10: |
Martin K. Petersen | 28a0bc4 | 2017-09-27 21:35:12 -0400 | [diff] [blame] | 741 | if (sdkp->device->unmap_limit_for_ws) |
| 742 | max_blocks = sdkp->max_unmap_blocks; |
| 743 | else |
| 744 | max_blocks = sdkp->max_ws_blocks; |
| 745 | |
| 746 | max_blocks = min_not_zero(max_blocks, (u32)SD_MAX_WS10_BLOCKS); |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 747 | break; |
| 748 | |
| 749 | case SD_LBP_ZERO: |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 750 | max_blocks = min_not_zero(sdkp->max_ws_blocks, |
| 751 | (u32)SD_MAX_WS10_BLOCKS); |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 752 | break; |
| 753 | } |
| 754 | |
Jens Axboe | 2bb4cd5 | 2015-07-14 08:15:12 -0600 | [diff] [blame] | 755 | blk_queue_max_discard_sectors(q, max_blocks * (logical_block_size >> 9)); |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 756 | blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 757 | } |
| 758 | |
Christoph Hellwig | 81d926e | 2017-04-05 19:20:59 +0200 | [diff] [blame] | 759 | static int sd_setup_unmap_cmnd(struct scsi_cmnd *cmd) |
Martin K. Petersen | e339c1a | 2009-11-26 12:00:40 -0500 | [diff] [blame] | 760 | { |
Christoph Hellwig | 6a7b439 | 2014-06-28 12:35:13 +0200 | [diff] [blame] | 761 | struct scsi_device *sdp = cmd->device; |
Christoph Hellwig | 81d926e | 2017-04-05 19:20:59 +0200 | [diff] [blame] | 762 | struct request *rq = cmd->request; |
| 763 | u64 sector = blk_rq_pos(rq) >> (ilog2(sdp->sector_size) - 9); |
| 764 | u32 nr_sectors = blk_rq_sectors(rq) >> (ilog2(sdp->sector_size) - 9); |
| 765 | unsigned int data_len = 24; |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 766 | char *buf; |
Martin K. Petersen | e339c1a | 2009-11-26 12:00:40 -0500 | [diff] [blame] | 767 | |
Jens Axboe | 024d515 | 2018-12-12 06:46:55 -0700 | [diff] [blame] | 768 | rq->special_vec.bv_page = mempool_alloc(sd_page_pool, GFP_ATOMIC); |
Christoph Hellwig | 81d926e | 2017-04-05 19:20:59 +0200 | [diff] [blame] | 769 | if (!rq->special_vec.bv_page) |
Christoph Hellwig | 66ac028 | 2010-06-18 16:59:42 +0200 | [diff] [blame] | 770 | return BLKPREP_DEFER; |
Jens Axboe | 024d515 | 2018-12-12 06:46:55 -0700 | [diff] [blame] | 771 | clear_highpage(rq->special_vec.bv_page); |
Christoph Hellwig | f9d03f9 | 2016-12-08 15:20:32 -0700 | [diff] [blame] | 772 | rq->special_vec.bv_offset = 0; |
Christoph Hellwig | 81d926e | 2017-04-05 19:20:59 +0200 | [diff] [blame] | 773 | rq->special_vec.bv_len = data_len; |
Christoph Hellwig | f9d03f9 | 2016-12-08 15:20:32 -0700 | [diff] [blame] | 774 | rq->rq_flags |= RQF_SPECIAL_PAYLOAD; |
Christoph Hellwig | f9d03f9 | 2016-12-08 15:20:32 -0700 | [diff] [blame] | 775 | |
Christoph Hellwig | 81d926e | 2017-04-05 19:20:59 +0200 | [diff] [blame] | 776 | cmd->cmd_len = 10; |
| 777 | cmd->cmnd[0] = UNMAP; |
| 778 | cmd->cmnd[8] = 24; |
| 779 | |
| 780 | buf = page_address(rq->special_vec.bv_page); |
| 781 | put_unaligned_be16(6 + 16, &buf[0]); |
| 782 | put_unaligned_be16(16, &buf[2]); |
| 783 | put_unaligned_be64(sector, &buf[8]); |
| 784 | put_unaligned_be32(nr_sectors, &buf[16]); |
| 785 | |
| 786 | cmd->allowed = SD_MAX_RETRIES; |
| 787 | cmd->transfersize = data_len; |
| 788 | rq->timeout = SD_TIMEOUT; |
| 789 | scsi_req(rq)->resid_len = data_len; |
| 790 | |
| 791 | return scsi_init_io(cmd); |
| 792 | } |
| 793 | |
Christoph Hellwig | 02d2610 | 2017-04-05 19:21:02 +0200 | [diff] [blame] | 794 | static int sd_setup_write_same16_cmnd(struct scsi_cmnd *cmd, bool unmap) |
Christoph Hellwig | 81d926e | 2017-04-05 19:20:59 +0200 | [diff] [blame] | 795 | { |
| 796 | struct scsi_device *sdp = cmd->device; |
| 797 | struct request *rq = cmd->request; |
| 798 | u64 sector = blk_rq_pos(rq) >> (ilog2(sdp->sector_size) - 9); |
| 799 | u32 nr_sectors = blk_rq_sectors(rq) >> (ilog2(sdp->sector_size) - 9); |
| 800 | u32 data_len = sdp->sector_size; |
| 801 | |
Jens Axboe | 024d515 | 2018-12-12 06:46:55 -0700 | [diff] [blame] | 802 | rq->special_vec.bv_page = mempool_alloc(sd_page_pool, GFP_ATOMIC); |
Christoph Hellwig | 81d926e | 2017-04-05 19:20:59 +0200 | [diff] [blame] | 803 | if (!rq->special_vec.bv_page) |
| 804 | return BLKPREP_DEFER; |
Jens Axboe | 024d515 | 2018-12-12 06:46:55 -0700 | [diff] [blame] | 805 | clear_highpage(rq->special_vec.bv_page); |
Christoph Hellwig | 81d926e | 2017-04-05 19:20:59 +0200 | [diff] [blame] | 806 | rq->special_vec.bv_offset = 0; |
| 807 | rq->special_vec.bv_len = data_len; |
| 808 | rq->rq_flags |= RQF_SPECIAL_PAYLOAD; |
| 809 | |
| 810 | cmd->cmd_len = 16; |
| 811 | cmd->cmnd[0] = WRITE_SAME_16; |
Christoph Hellwig | 02d2610 | 2017-04-05 19:21:02 +0200 | [diff] [blame] | 812 | if (unmap) |
| 813 | cmd->cmnd[1] = 0x8; /* UNMAP */ |
Christoph Hellwig | 81d926e | 2017-04-05 19:20:59 +0200 | [diff] [blame] | 814 | put_unaligned_be64(sector, &cmd->cmnd[2]); |
| 815 | put_unaligned_be32(nr_sectors, &cmd->cmnd[10]); |
| 816 | |
| 817 | cmd->allowed = SD_MAX_RETRIES; |
| 818 | cmd->transfersize = data_len; |
Christoph Hellwig | 02d2610 | 2017-04-05 19:21:02 +0200 | [diff] [blame] | 819 | rq->timeout = unmap ? SD_TIMEOUT : SD_WRITE_SAME_TIMEOUT; |
Christoph Hellwig | 81d926e | 2017-04-05 19:20:59 +0200 | [diff] [blame] | 820 | scsi_req(rq)->resid_len = data_len; |
| 821 | |
| 822 | return scsi_init_io(cmd); |
| 823 | } |
| 824 | |
| 825 | static int sd_setup_write_same10_cmnd(struct scsi_cmnd *cmd, bool unmap) |
| 826 | { |
| 827 | struct scsi_device *sdp = cmd->device; |
| 828 | struct request *rq = cmd->request; |
| 829 | u64 sector = blk_rq_pos(rq) >> (ilog2(sdp->sector_size) - 9); |
| 830 | u32 nr_sectors = blk_rq_sectors(rq) >> (ilog2(sdp->sector_size) - 9); |
| 831 | u32 data_len = sdp->sector_size; |
| 832 | |
Jens Axboe | 024d515 | 2018-12-12 06:46:55 -0700 | [diff] [blame] | 833 | rq->special_vec.bv_page = mempool_alloc(sd_page_pool, GFP_ATOMIC); |
Christoph Hellwig | 81d926e | 2017-04-05 19:20:59 +0200 | [diff] [blame] | 834 | if (!rq->special_vec.bv_page) |
| 835 | return BLKPREP_DEFER; |
Jens Axboe | 024d515 | 2018-12-12 06:46:55 -0700 | [diff] [blame] | 836 | clear_highpage(rq->special_vec.bv_page); |
Christoph Hellwig | 81d926e | 2017-04-05 19:20:59 +0200 | [diff] [blame] | 837 | rq->special_vec.bv_offset = 0; |
| 838 | rq->special_vec.bv_len = data_len; |
| 839 | rq->rq_flags |= RQF_SPECIAL_PAYLOAD; |
| 840 | |
| 841 | cmd->cmd_len = 10; |
| 842 | cmd->cmnd[0] = WRITE_SAME; |
| 843 | if (unmap) |
| 844 | cmd->cmnd[1] = 0x8; /* UNMAP */ |
| 845 | put_unaligned_be32(sector, &cmd->cmnd[2]); |
| 846 | put_unaligned_be16(nr_sectors, &cmd->cmnd[7]); |
| 847 | |
| 848 | cmd->allowed = SD_MAX_RETRIES; |
| 849 | cmd->transfersize = data_len; |
Christoph Hellwig | 02d2610 | 2017-04-05 19:21:02 +0200 | [diff] [blame] | 850 | rq->timeout = unmap ? SD_TIMEOUT : SD_WRITE_SAME_TIMEOUT; |
Christoph Hellwig | 81d926e | 2017-04-05 19:20:59 +0200 | [diff] [blame] | 851 | scsi_req(rq)->resid_len = data_len; |
| 852 | |
| 853 | return scsi_init_io(cmd); |
FUJITA Tomonori | f1126e9 | 2010-07-01 19:49:18 +0900 | [diff] [blame] | 854 | } |
| 855 | |
Christoph Hellwig | 02d2610 | 2017-04-05 19:21:02 +0200 | [diff] [blame] | 856 | static int sd_setup_write_zeroes_cmnd(struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | { |
| 858 | struct request *rq = cmd->request; |
| 859 | struct scsi_device *sdp = cmd->device; |
| 860 | struct scsi_disk *sdkp = scsi_disk(rq->rq_disk); |
Christoph Hellwig | 02d2610 | 2017-04-05 19:21:02 +0200 | [diff] [blame] | 861 | u64 sector = blk_rq_pos(rq) >> (ilog2(sdp->sector_size) - 9); |
| 862 | u32 nr_sectors = blk_rq_sectors(rq) >> (ilog2(sdp->sector_size) - 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | |
Christoph Hellwig | e4b8783 | 2017-04-05 19:21:12 +0200 | [diff] [blame] | 864 | if (!(rq->cmd_flags & REQ_NOUNMAP)) { |
Martin K. Petersen | e6bd931 | 2017-04-05 19:21:24 +0200 | [diff] [blame] | 865 | switch (sdkp->zeroing_mode) { |
| 866 | case SD_ZERO_WS16_UNMAP: |
Damien Le Moal | 39051dd | 2017-12-21 15:43:44 +0900 | [diff] [blame] | 867 | return sd_setup_write_same16_cmnd(cmd, true); |
Martin K. Petersen | e6bd931 | 2017-04-05 19:21:24 +0200 | [diff] [blame] | 868 | case SD_ZERO_WS10_UNMAP: |
Damien Le Moal | 39051dd | 2017-12-21 15:43:44 +0900 | [diff] [blame] | 869 | return sd_setup_write_same10_cmnd(cmd, true); |
Christoph Hellwig | e4b8783 | 2017-04-05 19:21:12 +0200 | [diff] [blame] | 870 | } |
Martin K. Petersen | e339c1a | 2009-11-26 12:00:40 -0500 | [diff] [blame] | 871 | } |
| 872 | |
Christoph Hellwig | 02d2610 | 2017-04-05 19:21:02 +0200 | [diff] [blame] | 873 | if (sdp->no_write_same) |
| 874 | return BLKPREP_INVALID; |
Damien Le Moal | ed44fd7 | 2017-05-08 15:48:19 +0900 | [diff] [blame] | 875 | |
Christoph Hellwig | 02d2610 | 2017-04-05 19:21:02 +0200 | [diff] [blame] | 876 | if (sdkp->ws16 || sector > 0xffffffff || nr_sectors > 0xffff) |
Damien Le Moal | 39051dd | 2017-12-21 15:43:44 +0900 | [diff] [blame] | 877 | return sd_setup_write_same16_cmnd(cmd, false); |
Damien Le Moal | ed44fd7 | 2017-05-08 15:48:19 +0900 | [diff] [blame] | 878 | |
Damien Le Moal | 39051dd | 2017-12-21 15:43:44 +0900 | [diff] [blame] | 879 | return sd_setup_write_same10_cmnd(cmd, false); |
FUJITA Tomonori | f1126e9 | 2010-07-01 19:49:18 +0900 | [diff] [blame] | 880 | } |
| 881 | |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 882 | static void sd_config_write_same(struct scsi_disk *sdkp) |
| 883 | { |
| 884 | struct request_queue *q = sdkp->disk->queue; |
| 885 | unsigned int logical_block_size = sdkp->device->sector_size; |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 886 | |
| 887 | if (sdkp->device->no_write_same) { |
| 888 | sdkp->max_ws_blocks = 0; |
| 889 | goto out; |
| 890 | } |
| 891 | |
| 892 | /* Some devices can not handle block counts above 0xffff despite |
| 893 | * supporting WRITE SAME(16). Consequently we default to 64k |
| 894 | * blocks per I/O unless the device explicitly advertises a |
| 895 | * bigger limit. |
| 896 | */ |
Martin K. Petersen | 66c28f9 | 2013-06-06 22:15:55 -0400 | [diff] [blame] | 897 | if (sdkp->max_ws_blocks > SD_MAX_WS10_BLOCKS) |
| 898 | sdkp->max_ws_blocks = min_not_zero(sdkp->max_ws_blocks, |
| 899 | (u32)SD_MAX_WS16_BLOCKS); |
| 900 | else if (sdkp->ws16 || sdkp->ws10 || sdkp->device->no_report_opcodes) |
| 901 | sdkp->max_ws_blocks = min_not_zero(sdkp->max_ws_blocks, |
| 902 | (u32)SD_MAX_WS10_BLOCKS); |
| 903 | else { |
| 904 | sdkp->device->no_write_same = 1; |
| 905 | sdkp->max_ws_blocks = 0; |
| 906 | } |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 907 | |
Martin K. Petersen | e6bd931 | 2017-04-05 19:21:24 +0200 | [diff] [blame] | 908 | if (sdkp->lbprz && sdkp->lbpws) |
| 909 | sdkp->zeroing_mode = SD_ZERO_WS16_UNMAP; |
| 910 | else if (sdkp->lbprz && sdkp->lbpws10) |
| 911 | sdkp->zeroing_mode = SD_ZERO_WS10_UNMAP; |
| 912 | else if (sdkp->max_ws_blocks) |
| 913 | sdkp->zeroing_mode = SD_ZERO_WS; |
| 914 | else |
| 915 | sdkp->zeroing_mode = SD_ZERO_WRITE; |
| 916 | |
Damien Le Moal | b7af62a | 2017-09-05 20:55:35 +0900 | [diff] [blame] | 917 | if (sdkp->max_ws_blocks && |
| 918 | sdkp->physical_block_size > logical_block_size) { |
| 919 | /* |
| 920 | * Reporting a maximum number of blocks that is not aligned |
| 921 | * on the device physical size would cause a large write same |
| 922 | * request to be split into physically unaligned chunks by |
| 923 | * __blkdev_issue_write_zeroes() and __blkdev_issue_write_same() |
| 924 | * even if the caller of these functions took care to align the |
| 925 | * large request. So make sure the maximum reported is aligned |
| 926 | * to the device physical block size. This is only an optional |
| 927 | * optimization for regular disks, but this is mandatory to |
| 928 | * avoid failure of large write same requests directed at |
| 929 | * sequential write required zones of host-managed ZBC disks. |
| 930 | */ |
| 931 | sdkp->max_ws_blocks = |
| 932 | round_down(sdkp->max_ws_blocks, |
| 933 | bytes_to_logical(sdkp->device, |
| 934 | sdkp->physical_block_size)); |
| 935 | } |
| 936 | |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 937 | out: |
Martin K. Petersen | 66c28f9 | 2013-06-06 22:15:55 -0400 | [diff] [blame] | 938 | blk_queue_max_write_same_sectors(q, sdkp->max_ws_blocks * |
| 939 | (logical_block_size >> 9)); |
Christoph Hellwig | 02d2610 | 2017-04-05 19:21:02 +0200 | [diff] [blame] | 940 | blk_queue_max_write_zeroes_sectors(q, sdkp->max_ws_blocks * |
| 941 | (logical_block_size >> 9)); |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | /** |
| 945 | * sd_setup_write_same_cmnd - write the same data to multiple blocks |
Christoph Hellwig | 59b1134 | 2014-06-28 12:22:22 +0200 | [diff] [blame] | 946 | * @cmd: command to prepare |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 947 | * |
Damien Le Moal | 7529fbb | 2017-04-24 16:51:09 +0900 | [diff] [blame] | 948 | * Will set up either WRITE SAME(10) or WRITE SAME(16) depending on |
| 949 | * the preference indicated by the target device. |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 950 | **/ |
Christoph Hellwig | 59b1134 | 2014-06-28 12:22:22 +0200 | [diff] [blame] | 951 | static int sd_setup_write_same_cmnd(struct scsi_cmnd *cmd) |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 952 | { |
Christoph Hellwig | 59b1134 | 2014-06-28 12:22:22 +0200 | [diff] [blame] | 953 | struct request *rq = cmd->request; |
| 954 | struct scsi_device *sdp = cmd->device; |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 955 | struct scsi_disk *sdkp = scsi_disk(rq->rq_disk); |
| 956 | struct bio *bio = rq->bio; |
| 957 | sector_t sector = blk_rq_pos(rq); |
| 958 | unsigned int nr_sectors = blk_rq_sectors(rq); |
Bart Van Assche | 08965c2 | 2017-01-25 13:43:56 -0800 | [diff] [blame] | 959 | unsigned int nr_bytes = blk_rq_bytes(rq); |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 960 | int ret; |
| 961 | |
| 962 | if (sdkp->device->no_write_same) |
Martin K. Petersen | 0fb5b1f | 2016-02-04 00:52:12 -0500 | [diff] [blame] | 963 | return BLKPREP_INVALID; |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 964 | |
Kent Overstreet | a4ad39b1 | 2013-08-07 14:24:32 -0700 | [diff] [blame] | 965 | BUG_ON(bio_offset(bio) || bio_iovec(bio).bv_len != sdp->sector_size); |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 966 | |
| 967 | sector >>= ilog2(sdp->sector_size) - 9; |
| 968 | nr_sectors >>= ilog2(sdp->sector_size) - 9; |
| 969 | |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 970 | rq->timeout = SD_WRITE_SAME_TIMEOUT; |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 971 | |
| 972 | if (sdkp->ws16 || sector > 0xffffffff || nr_sectors > 0xffff) { |
Christoph Hellwig | 59b1134 | 2014-06-28 12:22:22 +0200 | [diff] [blame] | 973 | cmd->cmd_len = 16; |
| 974 | cmd->cmnd[0] = WRITE_SAME_16; |
| 975 | put_unaligned_be64(sector, &cmd->cmnd[2]); |
| 976 | put_unaligned_be32(nr_sectors, &cmd->cmnd[10]); |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 977 | } else { |
Christoph Hellwig | 59b1134 | 2014-06-28 12:22:22 +0200 | [diff] [blame] | 978 | cmd->cmd_len = 10; |
| 979 | cmd->cmnd[0] = WRITE_SAME; |
| 980 | put_unaligned_be32(sector, &cmd->cmnd[2]); |
| 981 | put_unaligned_be16(nr_sectors, &cmd->cmnd[7]); |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 982 | } |
| 983 | |
Christoph Hellwig | 59b1134 | 2014-06-28 12:22:22 +0200 | [diff] [blame] | 984 | cmd->transfersize = sdp->sector_size; |
Christoph Hellwig | a25ee54 | 2014-06-28 12:29:19 +0200 | [diff] [blame] | 985 | cmd->allowed = SD_MAX_RETRIES; |
Bart Van Assche | 08965c2 | 2017-01-25 13:43:56 -0800 | [diff] [blame] | 986 | |
| 987 | /* |
| 988 | * For WRITE SAME the data transferred via the DATA OUT buffer is |
| 989 | * different from the amount of data actually written to the target. |
| 990 | * |
| 991 | * We set up __data_len to the amount of data transferred via the |
| 992 | * DATA OUT buffer so that blk_rq_map_sg sets up the proper S/G list |
| 993 | * to transfer a single sector of data first, but then reset it to |
| 994 | * the amount of data to be written right after so that the I/O path |
| 995 | * knows how much to actually write. |
| 996 | */ |
| 997 | rq->__data_len = sdp->sector_size; |
| 998 | ret = scsi_init_io(cmd); |
| 999 | rq->__data_len = nr_bytes; |
Damien Le Moal | 29f6ca6 | 2017-05-08 14:59:02 +0900 | [diff] [blame] | 1000 | |
Bart Van Assche | 08965c2 | 2017-01-25 13:43:56 -0800 | [diff] [blame] | 1001 | return ret; |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 1002 | } |
| 1003 | |
Christoph Hellwig | a118c6c | 2014-06-28 12:08:05 +0200 | [diff] [blame] | 1004 | static int sd_setup_flush_cmnd(struct scsi_cmnd *cmd) |
FUJITA Tomonori | 90467c2 | 2010-07-03 17:45:34 +0900 | [diff] [blame] | 1005 | { |
Christoph Hellwig | a118c6c | 2014-06-28 12:08:05 +0200 | [diff] [blame] | 1006 | struct request *rq = cmd->request; |
FUJITA Tomonori | 90467c2 | 2010-07-03 17:45:34 +0900 | [diff] [blame] | 1007 | |
Christoph Hellwig | a118c6c | 2014-06-28 12:08:05 +0200 | [diff] [blame] | 1008 | /* flush requests don't perform I/O, zero the S/G table */ |
| 1009 | memset(&cmd->sdb, 0, sizeof(cmd->sdb)); |
| 1010 | |
| 1011 | cmd->cmnd[0] = SYNCHRONIZE_CACHE; |
| 1012 | cmd->cmd_len = 10; |
| 1013 | cmd->transfersize = 0; |
| 1014 | cmd->allowed = SD_MAX_RETRIES; |
| 1015 | |
K. Y. Srinivasan | 26b9fd8 | 2014-07-18 17:11:27 +0200 | [diff] [blame] | 1016 | rq->timeout = rq->q->rq_timeout * SD_FLUSH_TIMEOUT_MULTIPLIER; |
Christoph Hellwig | a118c6c | 2014-06-28 12:08:05 +0200 | [diff] [blame] | 1017 | return BLKPREP_OK; |
FUJITA Tomonori | 90467c2 | 2010-07-03 17:45:34 +0900 | [diff] [blame] | 1018 | } |
| 1019 | |
Christoph Hellwig | 87949ee | 2014-06-28 12:40:18 +0200 | [diff] [blame] | 1020 | static int sd_setup_read_write_cmnd(struct scsi_cmnd *SCpnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | { |
Christoph Hellwig | a1b73fc | 2014-05-01 16:51:04 +0200 | [diff] [blame] | 1022 | struct request *rq = SCpnt->request; |
| 1023 | struct scsi_device *sdp = SCpnt->device; |
Christoph Hellwig | 776b23a | 2006-01-06 18:34:07 +0100 | [diff] [blame] | 1024 | struct gendisk *disk = rq->rq_disk; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 1025 | struct scsi_disk *sdkp = scsi_disk(disk); |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 1026 | sector_t block = blk_rq_pos(rq); |
Linus Torvalds | 1835107 | 2008-08-05 21:42:21 -0700 | [diff] [blame] | 1027 | sector_t threshold; |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 1028 | unsigned int this_count = blk_rq_sectors(rq); |
Martin K. Petersen | c611529 | 2014-09-26 19:20:08 -0400 | [diff] [blame] | 1029 | unsigned int dif, dix; |
| 1030 | int ret; |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 1031 | unsigned char protect; |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 1032 | |
Christoph Hellwig | 3c356bd | 2014-09-05 18:20:23 -0700 | [diff] [blame] | 1033 | ret = scsi_init_io(SCpnt); |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 1034 | if (ret != BLKPREP_OK) |
Damien Le Moal | 39051dd | 2017-12-21 15:43:44 +0900 | [diff] [blame] | 1035 | return ret; |
Bart Van Assche | 0624cbb | 2017-08-25 13:46:33 -0700 | [diff] [blame] | 1036 | WARN_ON_ONCE(SCpnt != rq->special); |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 1037 | |
| 1038 | /* from here on until we're complete, any goto out |
| 1039 | * is used for a killable error condition */ |
| 1040 | ret = BLKPREP_KILL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | |
Christoph Hellwig | a1b73fc | 2014-05-01 16:51:04 +0200 | [diff] [blame] | 1042 | SCSI_LOG_HLQUEUE(1, |
| 1043 | scmd_printk(KERN_INFO, SCpnt, |
| 1044 | "%s: block=%llu, count=%d\n", |
| 1045 | __func__, (unsigned long long)block, this_count)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | |
| 1047 | if (!sdp || !scsi_device_online(sdp) || |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 1048 | block + blk_rq_sectors(rq) > get_capacity(disk)) { |
Martin K. Petersen | fa0d34b | 2007-02-27 22:41:19 -0500 | [diff] [blame] | 1049 | SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 1050 | "Finishing %u sectors\n", |
| 1051 | blk_rq_sectors(rq))); |
Martin K. Petersen | fa0d34b | 2007-02-27 22:41:19 -0500 | [diff] [blame] | 1052 | SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, |
| 1053 | "Retry with 0x%p\n", SCpnt)); |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 1054 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | if (sdp->changed) { |
| 1058 | /* |
| 1059 | * quietly refuse to do anything to a changed disc until |
| 1060 | * the changed bit has been reset |
| 1061 | */ |
Alan Stern | 3ff5588 | 2010-09-07 11:44:01 -0400 | [diff] [blame] | 1062 | /* printk("SCSI disk has been changed or is not present. Prohibiting further I/O.\n"); */ |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 1063 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | } |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 1065 | |
Hans de Goede | a0899d4 | 2008-01-20 11:12:26 +0100 | [diff] [blame] | 1066 | /* |
Linus Torvalds | 1835107 | 2008-08-05 21:42:21 -0700 | [diff] [blame] | 1067 | * Some SD card readers can't handle multi-sector accesses which touch |
| 1068 | * the last one or two hardware sectors. Split accesses as needed. |
Hans de Goede | a0899d4 | 2008-01-20 11:12:26 +0100 | [diff] [blame] | 1069 | */ |
Linus Torvalds | 1835107 | 2008-08-05 21:42:21 -0700 | [diff] [blame] | 1070 | threshold = get_capacity(disk) - SD_LAST_BUGGY_SECTORS * |
| 1071 | (sdp->sector_size / 512); |
| 1072 | |
| 1073 | if (unlikely(sdp->last_sector_bug && block + this_count > threshold)) { |
| 1074 | if (block < threshold) { |
| 1075 | /* Access up to the threshold but not beyond */ |
| 1076 | this_count = threshold - block; |
| 1077 | } else { |
| 1078 | /* Access only a single hardware sector */ |
| 1079 | this_count = sdp->sector_size / 512; |
| 1080 | } |
| 1081 | } |
Hans de Goede | a0899d4 | 2008-01-20 11:12:26 +0100 | [diff] [blame] | 1082 | |
Martin K. Petersen | fa0d34b | 2007-02-27 22:41:19 -0500 | [diff] [blame] | 1083 | SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n", |
| 1084 | (unsigned long long)block)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | |
| 1086 | /* |
| 1087 | * If we have a 1K hardware sectorsize, prevent access to single |
| 1088 | * 512 byte sectors. In theory we could handle this - in fact |
| 1089 | * the scsi cdrom driver must be able to handle this because |
| 1090 | * we typically use 1K blocksizes, and cdroms typically have |
| 1091 | * 2K hardware sectorsizes. Of course, things are simpler |
| 1092 | * with the cdrom, since it is read-only. For performance |
| 1093 | * reasons, the filesystems should be able to handle this |
| 1094 | * and not force the scsi disk driver to use bounce buffers |
| 1095 | * for this. |
| 1096 | */ |
| 1097 | if (sdp->sector_size == 1024) { |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 1098 | if ((block & 1) || (blk_rq_sectors(rq) & 1)) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1099 | scmd_printk(KERN_ERR, SCpnt, |
| 1100 | "Bad block number requested\n"); |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 1101 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | } else { |
| 1103 | block = block >> 1; |
| 1104 | this_count = this_count >> 1; |
| 1105 | } |
| 1106 | } |
| 1107 | if (sdp->sector_size == 2048) { |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 1108 | if ((block & 3) || (blk_rq_sectors(rq) & 3)) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1109 | scmd_printk(KERN_ERR, SCpnt, |
| 1110 | "Bad block number requested\n"); |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 1111 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | } else { |
| 1113 | block = block >> 2; |
| 1114 | this_count = this_count >> 2; |
| 1115 | } |
| 1116 | } |
| 1117 | if (sdp->sector_size == 4096) { |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 1118 | if ((block & 7) || (blk_rq_sectors(rq) & 7)) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1119 | scmd_printk(KERN_ERR, SCpnt, |
| 1120 | "Bad block number requested\n"); |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 1121 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | } else { |
| 1123 | block = block >> 3; |
| 1124 | this_count = this_count >> 3; |
| 1125 | } |
| 1126 | } |
| 1127 | if (rq_data_dir(rq) == WRITE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | SCpnt->cmnd[0] = WRITE_6; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 1129 | |
Martin K. Petersen | 8c579ab | 2012-08-28 14:29:33 -0400 | [diff] [blame] | 1130 | if (blk_integrity_rq(rq)) |
Max Gurtovoy | 10c41dd | 2018-07-30 00:15:32 +0300 | [diff] [blame] | 1131 | t10_pi_prepare(SCpnt->request, sdkp->protection_type); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 1132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | } else if (rq_data_dir(rq) == READ) { |
| 1134 | SCpnt->cmnd[0] = READ_6; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | } else { |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 1136 | scmd_printk(KERN_ERR, SCpnt, "Unknown command %d\n", req_op(rq)); |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 1137 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
Martin K. Petersen | fa0d34b | 2007-02-27 22:41:19 -0500 | [diff] [blame] | 1140 | SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 1141 | "%s %d/%u 512 byte blocks.\n", |
Martin K. Petersen | fa0d34b | 2007-02-27 22:41:19 -0500 | [diff] [blame] | 1142 | (rq_data_dir(rq) == WRITE) ? |
| 1143 | "writing" : "reading", this_count, |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 1144 | blk_rq_sectors(rq))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | |
Martin K. Petersen | c611529 | 2014-09-26 19:20:08 -0400 | [diff] [blame] | 1146 | dix = scsi_prot_sg_count(SCpnt); |
| 1147 | dif = scsi_host_dif_capable(SCpnt->device->host, sdkp->protection_type); |
| 1148 | |
| 1149 | if (dif || dix) |
| 1150 | protect = sd_setup_protect_cmnd(SCpnt, dix, dif); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 1151 | else |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 1152 | protect = 0; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 1153 | |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 1154 | if (protect && sdkp->protection_type == T10_PI_TYPE2_PROTECTION) { |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 1155 | SCpnt->cmnd = mempool_alloc(sd_cdb_pool, GFP_ATOMIC); |
| 1156 | |
| 1157 | if (unlikely(SCpnt->cmnd == NULL)) { |
| 1158 | ret = BLKPREP_DEFER; |
| 1159 | goto out; |
| 1160 | } |
| 1161 | |
| 1162 | SCpnt->cmd_len = SD_EXT_CDB_SIZE; |
| 1163 | memset(SCpnt->cmnd, 0, SCpnt->cmd_len); |
| 1164 | SCpnt->cmnd[0] = VARIABLE_LENGTH_CMD; |
| 1165 | SCpnt->cmnd[7] = 0x18; |
| 1166 | SCpnt->cmnd[9] = (rq_data_dir(rq) == READ) ? READ_32 : WRITE_32; |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 1167 | SCpnt->cmnd[10] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0); |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 1168 | |
| 1169 | /* LBA */ |
| 1170 | SCpnt->cmnd[12] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0; |
| 1171 | SCpnt->cmnd[13] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0; |
| 1172 | SCpnt->cmnd[14] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0; |
| 1173 | SCpnt->cmnd[15] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0; |
| 1174 | SCpnt->cmnd[16] = (unsigned char) (block >> 24) & 0xff; |
| 1175 | SCpnt->cmnd[17] = (unsigned char) (block >> 16) & 0xff; |
| 1176 | SCpnt->cmnd[18] = (unsigned char) (block >> 8) & 0xff; |
| 1177 | SCpnt->cmnd[19] = (unsigned char) block & 0xff; |
| 1178 | |
| 1179 | /* Expected Indirect LBA */ |
| 1180 | SCpnt->cmnd[20] = (unsigned char) (block >> 24) & 0xff; |
| 1181 | SCpnt->cmnd[21] = (unsigned char) (block >> 16) & 0xff; |
| 1182 | SCpnt->cmnd[22] = (unsigned char) (block >> 8) & 0xff; |
| 1183 | SCpnt->cmnd[23] = (unsigned char) block & 0xff; |
| 1184 | |
| 1185 | /* Transfer length */ |
| 1186 | SCpnt->cmnd[28] = (unsigned char) (this_count >> 24) & 0xff; |
| 1187 | SCpnt->cmnd[29] = (unsigned char) (this_count >> 16) & 0xff; |
| 1188 | SCpnt->cmnd[30] = (unsigned char) (this_count >> 8) & 0xff; |
| 1189 | SCpnt->cmnd[31] = (unsigned char) this_count & 0xff; |
Akinobu Mita | e430cbc | 2014-06-02 22:56:47 +0900 | [diff] [blame] | 1190 | } else if (sdp->use_16_for_rw || (this_count > 0xffff)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | SCpnt->cmnd[0] += READ_16 - READ_6; |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 1192 | SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0; |
| 1194 | SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0; |
| 1195 | SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0; |
| 1196 | SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0; |
| 1197 | SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff; |
| 1198 | SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff; |
| 1199 | SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff; |
| 1200 | SCpnt->cmnd[9] = (unsigned char) block & 0xff; |
| 1201 | SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff; |
| 1202 | SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff; |
| 1203 | SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff; |
| 1204 | SCpnt->cmnd[13] = (unsigned char) this_count & 0xff; |
| 1205 | SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0; |
| 1206 | } else if ((this_count > 0xff) || (block > 0x1fffff) || |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 1207 | scsi_device_protection(SCpnt->device) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | SCpnt->device->use_10_for_rw) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | SCpnt->cmnd[0] += READ_10 - READ_6; |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 1210 | SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff; |
| 1212 | SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff; |
| 1213 | SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff; |
| 1214 | SCpnt->cmnd[5] = (unsigned char) block & 0xff; |
| 1215 | SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0; |
| 1216 | SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff; |
| 1217 | SCpnt->cmnd[8] = (unsigned char) this_count & 0xff; |
| 1218 | } else { |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 1219 | if (unlikely(rq->cmd_flags & REQ_FUA)) { |
Tejun Heo | 007365a | 2006-01-06 09:53:52 +0100 | [diff] [blame] | 1220 | /* |
| 1221 | * This happens only if this drive failed |
| 1222 | * 10byte rw command with ILLEGAL_REQUEST |
| 1223 | * during operation and thus turned off |
| 1224 | * use_10_for_rw. |
| 1225 | */ |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1226 | scmd_printk(KERN_ERR, SCpnt, |
| 1227 | "FUA write on READ/WRITE(6) drive\n"); |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 1228 | goto out; |
Tejun Heo | 007365a | 2006-01-06 09:53:52 +0100 | [diff] [blame] | 1229 | } |
| 1230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f); |
| 1232 | SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff); |
| 1233 | SCpnt->cmnd[3] = (unsigned char) block & 0xff; |
| 1234 | SCpnt->cmnd[4] = (unsigned char) this_count; |
| 1235 | SCpnt->cmnd[5] = 0; |
| 1236 | } |
Boaz Harrosh | 30b0c37 | 2007-12-13 13:47:40 +0200 | [diff] [blame] | 1237 | SCpnt->sdb.length = this_count * sdp->sector_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | |
| 1239 | /* |
| 1240 | * We shouldn't disconnect in the middle of a sector, so with a dumb |
| 1241 | * host adapter, it's safe to assume that we can at least transfer |
| 1242 | * this many bytes between each connect / disconnect. |
| 1243 | */ |
| 1244 | SCpnt->transfersize = sdp->sector_size; |
| 1245 | SCpnt->underflow = this_count << 9; |
| 1246 | SCpnt->allowed = SD_MAX_RETRIES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | |
| 1248 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | * This indicates that the command is ready from our end to be |
| 1250 | * queued. |
| 1251 | */ |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 1252 | ret = BLKPREP_OK; |
| 1253 | out: |
Christoph Hellwig | a1b73fc | 2014-05-01 16:51:04 +0200 | [diff] [blame] | 1254 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | } |
| 1256 | |
Christoph Hellwig | 87949ee | 2014-06-28 12:40:18 +0200 | [diff] [blame] | 1257 | static int sd_init_command(struct scsi_cmnd *cmd) |
| 1258 | { |
| 1259 | struct request *rq = cmd->request; |
| 1260 | |
Mike Christie | c2df40d | 2016-06-05 14:32:17 -0500 | [diff] [blame] | 1261 | switch (req_op(rq)) { |
| 1262 | case REQ_OP_DISCARD: |
Christoph Hellwig | 81d926e | 2017-04-05 19:20:59 +0200 | [diff] [blame] | 1263 | switch (scsi_disk(rq->rq_disk)->provisioning_mode) { |
| 1264 | case SD_LBP_UNMAP: |
| 1265 | return sd_setup_unmap_cmnd(cmd); |
| 1266 | case SD_LBP_WS16: |
Christoph Hellwig | 02d2610 | 2017-04-05 19:21:02 +0200 | [diff] [blame] | 1267 | return sd_setup_write_same16_cmnd(cmd, true); |
Christoph Hellwig | 81d926e | 2017-04-05 19:20:59 +0200 | [diff] [blame] | 1268 | case SD_LBP_WS10: |
| 1269 | return sd_setup_write_same10_cmnd(cmd, true); |
| 1270 | case SD_LBP_ZERO: |
| 1271 | return sd_setup_write_same10_cmnd(cmd, false); |
| 1272 | default: |
| 1273 | return BLKPREP_INVALID; |
| 1274 | } |
Christoph Hellwig | 02d2610 | 2017-04-05 19:21:02 +0200 | [diff] [blame] | 1275 | case REQ_OP_WRITE_ZEROES: |
| 1276 | return sd_setup_write_zeroes_cmnd(cmd); |
Mike Christie | c2df40d | 2016-06-05 14:32:17 -0500 | [diff] [blame] | 1277 | case REQ_OP_WRITE_SAME: |
Christoph Hellwig | 87949ee | 2014-06-28 12:40:18 +0200 | [diff] [blame] | 1278 | return sd_setup_write_same_cmnd(cmd); |
Mike Christie | 3a5e02c | 2016-06-05 14:32:23 -0500 | [diff] [blame] | 1279 | case REQ_OP_FLUSH: |
Christoph Hellwig | 87949ee | 2014-06-28 12:40:18 +0200 | [diff] [blame] | 1280 | return sd_setup_flush_cmnd(cmd); |
Mike Christie | c2df40d | 2016-06-05 14:32:17 -0500 | [diff] [blame] | 1281 | case REQ_OP_READ: |
| 1282 | case REQ_OP_WRITE: |
Christoph Hellwig | 87949ee | 2014-06-28 12:40:18 +0200 | [diff] [blame] | 1283 | return sd_setup_read_write_cmnd(cmd); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 1284 | case REQ_OP_ZONE_REPORT: |
| 1285 | return sd_zbc_setup_report_cmnd(cmd); |
| 1286 | case REQ_OP_ZONE_RESET: |
| 1287 | return sd_zbc_setup_reset_cmnd(cmd); |
Mike Christie | c2df40d | 2016-06-05 14:32:17 -0500 | [diff] [blame] | 1288 | default: |
Johannes Thumshirn | f1f1fad | 2018-09-21 09:01:01 +0200 | [diff] [blame] | 1289 | WARN_ON_ONCE(1); |
| 1290 | return BLKPREP_KILL; |
Mike Christie | c2df40d | 2016-06-05 14:32:17 -0500 | [diff] [blame] | 1291 | } |
Christoph Hellwig | 87949ee | 2014-06-28 12:40:18 +0200 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | static void sd_uninit_command(struct scsi_cmnd *SCpnt) |
| 1295 | { |
| 1296 | struct request *rq = SCpnt->request; |
Bart Van Assche | 14e3062 | 2017-12-05 16:57:51 -0800 | [diff] [blame] | 1297 | u8 *cmnd; |
Christoph Hellwig | 87949ee | 2014-06-28 12:40:18 +0200 | [diff] [blame] | 1298 | |
Christoph Hellwig | f9d03f9 | 2016-12-08 15:20:32 -0700 | [diff] [blame] | 1299 | if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) |
Jens Axboe | 024d515 | 2018-12-12 06:46:55 -0700 | [diff] [blame] | 1300 | mempool_free(rq->special_vec.bv_page, sd_page_pool); |
Christoph Hellwig | 87949ee | 2014-06-28 12:40:18 +0200 | [diff] [blame] | 1301 | |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 1302 | if (SCpnt->cmnd != scsi_req(rq)->cmd) { |
Bart Van Assche | 14e3062 | 2017-12-05 16:57:51 -0800 | [diff] [blame] | 1303 | cmnd = SCpnt->cmnd; |
Christoph Hellwig | 87949ee | 2014-06-28 12:40:18 +0200 | [diff] [blame] | 1304 | SCpnt->cmnd = NULL; |
| 1305 | SCpnt->cmd_len = 0; |
Bart Van Assche | 14e3062 | 2017-12-05 16:57:51 -0800 | [diff] [blame] | 1306 | mempool_free(cmnd, sd_cdb_pool); |
Christoph Hellwig | 87949ee | 2014-06-28 12:40:18 +0200 | [diff] [blame] | 1307 | } |
| 1308 | } |
| 1309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | /** |
| 1311 | * sd_open - open a scsi disk device |
Damien Le Moal | 7529fbb | 2017-04-24 16:51:09 +0900 | [diff] [blame] | 1312 | * @bdev: Block device of the scsi disk to open |
| 1313 | * @mode: FMODE_* mask |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | * |
| 1315 | * Returns 0 if successful. Returns a negated errno value in case |
| 1316 | * of error. |
| 1317 | * |
| 1318 | * Note: This can be called from a user context (e.g. fsck(1) ) |
| 1319 | * or from within the kernel (e.g. as a result of a mount(1) ). |
| 1320 | * In the latter case @inode and @filp carry an abridged amount |
| 1321 | * of information as noted above. |
Arnd Bergmann | 409f349 | 2010-07-07 16:51:29 +0200 | [diff] [blame] | 1322 | * |
| 1323 | * Locking: called with bdev->bd_mutex held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | **/ |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 1325 | static int sd_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | { |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 1327 | struct scsi_disk *sdkp = scsi_disk_get(bdev->bd_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | struct scsi_device *sdev; |
| 1329 | int retval; |
| 1330 | |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 1331 | if (!sdkp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | return -ENXIO; |
| 1333 | |
Martin K. Petersen | fa0d34b | 2007-02-27 22:41:19 -0500 | [diff] [blame] | 1334 | SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | |
| 1336 | sdev = sdkp->device; |
| 1337 | |
| 1338 | /* |
| 1339 | * If the device is in error recovery, wait until it is done. |
| 1340 | * If the device is offline, then disallow any access to it. |
| 1341 | */ |
| 1342 | retval = -ENXIO; |
| 1343 | if (!scsi_block_when_processing_errors(sdev)) |
| 1344 | goto error_out; |
| 1345 | |
| 1346 | if (sdev->removable || sdkp->write_prot) |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 1347 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | |
| 1349 | /* |
| 1350 | * If the drive is empty, just let the open fail. |
| 1351 | */ |
| 1352 | retval = -ENOMEDIUM; |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 1353 | if (sdev->removable && !sdkp->media_present && !(mode & FMODE_NDELAY)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | goto error_out; |
| 1355 | |
| 1356 | /* |
| 1357 | * If the device has the write protect tab set, have the open fail |
| 1358 | * if the user expects to be able to write to the thing. |
| 1359 | */ |
| 1360 | retval = -EROFS; |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 1361 | if (sdkp->write_prot && (mode & FMODE_WRITE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | goto error_out; |
| 1363 | |
| 1364 | /* |
| 1365 | * It is possible that the disk changing stuff resulted in |
| 1366 | * the device being taken offline. If this is the case, |
| 1367 | * report this to the user, and don't pretend that the |
| 1368 | * open actually succeeded. |
| 1369 | */ |
| 1370 | retval = -ENXIO; |
| 1371 | if (!scsi_device_online(sdev)) |
| 1372 | goto error_out; |
| 1373 | |
Arnd Bergmann | 409f349 | 2010-07-07 16:51:29 +0200 | [diff] [blame] | 1374 | if ((atomic_inc_return(&sdkp->openers) == 1) && sdev->removable) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | if (scsi_block_when_processing_errors(sdev)) |
| 1376 | scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT); |
| 1377 | } |
| 1378 | |
| 1379 | return 0; |
| 1380 | |
| 1381 | error_out: |
| 1382 | scsi_disk_put(sdkp); |
| 1383 | return retval; |
| 1384 | } |
| 1385 | |
| 1386 | /** |
| 1387 | * sd_release - invoked when the (last) close(2) is called on this |
| 1388 | * scsi disk. |
Damien Le Moal | 7529fbb | 2017-04-24 16:51:09 +0900 | [diff] [blame] | 1389 | * @disk: disk to release |
| 1390 | * @mode: FMODE_* mask |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | * |
| 1392 | * Returns 0. |
| 1393 | * |
| 1394 | * Note: may block (uninterruptible) if error recovery is underway |
| 1395 | * on this disk. |
Arnd Bergmann | 409f349 | 2010-07-07 16:51:29 +0200 | [diff] [blame] | 1396 | * |
| 1397 | * Locking: called with bdev->bd_mutex held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | **/ |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 1399 | static void sd_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | struct scsi_disk *sdkp = scsi_disk(disk); |
| 1402 | struct scsi_device *sdev = sdkp->device; |
| 1403 | |
James Bottomley | 56937f7 | 2007-03-11 12:25:33 -0500 | [diff] [blame] | 1404 | SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | |
Alan Stern | 7e44331 | 2010-09-07 11:27:52 -0400 | [diff] [blame] | 1406 | if (atomic_dec_return(&sdkp->openers) == 0 && sdev->removable) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | if (scsi_block_when_processing_errors(sdev)) |
| 1408 | scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW); |
| 1409 | } |
| 1410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | scsi_disk_put(sdkp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | } |
| 1413 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 1414 | static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | { |
| 1416 | struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk); |
| 1417 | struct scsi_device *sdp = sdkp->device; |
| 1418 | struct Scsi_Host *host = sdp->host; |
Martin K. Petersen | f08bb1e | 2016-03-28 21:18:56 -0400 | [diff] [blame] | 1419 | sector_t capacity = logical_to_sectors(sdp, sdkp->capacity); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | int diskinfo[4]; |
| 1421 | |
| 1422 | /* default to most commonly used values */ |
Martin K. Petersen | f08bb1e | 2016-03-28 21:18:56 -0400 | [diff] [blame] | 1423 | diskinfo[0] = 0x40; /* 1 << 6 */ |
| 1424 | diskinfo[1] = 0x20; /* 1 << 5 */ |
| 1425 | diskinfo[2] = capacity >> 11; |
| 1426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | /* override with calculated, extended default, or driver values */ |
| 1428 | if (host->hostt->bios_param) |
Martin K. Petersen | f08bb1e | 2016-03-28 21:18:56 -0400 | [diff] [blame] | 1429 | host->hostt->bios_param(sdp, bdev, capacity, diskinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | else |
Martin K. Petersen | f08bb1e | 2016-03-28 21:18:56 -0400 | [diff] [blame] | 1431 | scsicam_bios_param(bdev, capacity, diskinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 1433 | geo->heads = diskinfo[0]; |
| 1434 | geo->sectors = diskinfo[1]; |
| 1435 | geo->cylinders = diskinfo[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | return 0; |
| 1437 | } |
| 1438 | |
| 1439 | /** |
| 1440 | * sd_ioctl - process an ioctl |
Damien Le Moal | 7529fbb | 2017-04-24 16:51:09 +0900 | [diff] [blame] | 1441 | * @bdev: target block device |
| 1442 | * @mode: FMODE_* mask |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | * @cmd: ioctl command number |
| 1444 | * @arg: this is third argument given to ioctl(2) system call. |
| 1445 | * Often contains a pointer. |
| 1446 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1447 | * Returns 0 if successful (some ioctls return positive numbers on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | * success as well). Returns a negated errno value in case of error. |
| 1449 | * |
| 1450 | * Note: most ioctls are forward onto the block subsystem or further |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 1451 | * down in the scsi subsystem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | **/ |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 1453 | static int sd_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | unsigned int cmd, unsigned long arg) |
| 1455 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | struct gendisk *disk = bdev->bd_disk; |
Nao Nishijima | fe2d185 | 2011-08-25 18:04:14 +0900 | [diff] [blame] | 1457 | struct scsi_disk *sdkp = scsi_disk(disk); |
| 1458 | struct scsi_device *sdp = sdkp->device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | void __user *p = (void __user *)arg; |
| 1460 | int error; |
| 1461 | |
Nao Nishijima | fe2d185 | 2011-08-25 18:04:14 +0900 | [diff] [blame] | 1462 | SCSI_LOG_IOCTL(1, sd_printk(KERN_INFO, sdkp, "sd_ioctl: disk=%s, " |
| 1463 | "cmd=0x%x\n", disk->disk_name, cmd)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | |
Paolo Bonzini | 0bfc96c | 2012-01-12 16:01:28 +0100 | [diff] [blame] | 1465 | error = scsi_verify_blk_ioctl(bdev, cmd); |
| 1466 | if (error < 0) |
| 1467 | return error; |
| 1468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | /* |
| 1470 | * If we are in the middle of error recovery, don't let anyone |
| 1471 | * else try and use this device. Also, if error recovery fails, it |
| 1472 | * may try and take the device offline, in which case all further |
| 1473 | * access to the device is prohibited. |
| 1474 | */ |
Christoph Hellwig | 906d15f | 2014-10-11 16:25:31 +0200 | [diff] [blame] | 1475 | error = scsi_ioctl_block_when_processing_errors(sdp, cmd, |
| 1476 | (mode & FMODE_NDELAY) != 0); |
| 1477 | if (error) |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 1478 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | |
Christoph Hellwig | d80210f | 2017-06-19 14:26:46 +0200 | [diff] [blame] | 1480 | if (is_sed_ioctl(cmd)) |
| 1481 | return sed_ioctl(sdkp->opal_dev, cmd, p); |
| 1482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | /* |
| 1484 | * Send SCSI addressing ioctls directly to mid level, send other |
| 1485 | * ioctls to block level and then onto mid level if they can't be |
| 1486 | * resolved. |
| 1487 | */ |
| 1488 | switch (cmd) { |
| 1489 | case SCSI_IOCTL_GET_IDLUN: |
| 1490 | case SCSI_IOCTL_GET_BUS_NUMBER: |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 1491 | error = scsi_ioctl(sdp, cmd, p); |
| 1492 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | default: |
Paolo Bonzini | 577ebb3 | 2012-01-12 16:01:27 +0100 | [diff] [blame] | 1494 | error = scsi_cmd_blk_ioctl(bdev, mode, cmd, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | if (error != -ENOTTY) |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 1496 | break; |
| 1497 | error = scsi_ioctl(sdp, cmd, p); |
| 1498 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | } |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 1500 | out: |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 1501 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | static void set_media_not_present(struct scsi_disk *sdkp) |
| 1505 | { |
Tejun Heo | 2bae009 | 2010-12-18 18:42:23 +0100 | [diff] [blame] | 1506 | if (sdkp->media_present) |
| 1507 | sdkp->device->changed = 1; |
| 1508 | |
| 1509 | if (sdkp->device->removable) { |
| 1510 | sdkp->media_present = 0; |
| 1511 | sdkp->capacity = 0; |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | static int media_not_present(struct scsi_disk *sdkp, |
| 1516 | struct scsi_sense_hdr *sshdr) |
| 1517 | { |
| 1518 | if (!scsi_sense_valid(sshdr)) |
| 1519 | return 0; |
| 1520 | |
| 1521 | /* not invoked for commands that could return deferred errors */ |
| 1522 | switch (sshdr->sense_key) { |
| 1523 | case UNIT_ATTENTION: |
| 1524 | case NOT_READY: |
| 1525 | /* medium not present */ |
| 1526 | if (sshdr->asc == 0x3A) { |
| 1527 | set_media_not_present(sdkp); |
| 1528 | return 1; |
| 1529 | } |
| 1530 | } |
| 1531 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | /** |
Tejun Heo | 2bae009 | 2010-12-18 18:42:23 +0100 | [diff] [blame] | 1535 | * sd_check_events - check media events |
| 1536 | * @disk: kernel device descriptor |
| 1537 | * @clearing: disk events currently being cleared |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | * |
Tejun Heo | 2bae009 | 2010-12-18 18:42:23 +0100 | [diff] [blame] | 1539 | * Returns mask of DISK_EVENT_*. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | * |
| 1541 | * Note: this function is invoked from the block subsystem. |
| 1542 | **/ |
Tejun Heo | 2bae009 | 2010-12-18 18:42:23 +0100 | [diff] [blame] | 1543 | static unsigned int sd_check_events(struct gendisk *disk, unsigned int clearing) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | { |
Hannes Reinecke | eb72d0b | 2016-04-26 08:06:58 +0200 | [diff] [blame] | 1545 | struct scsi_disk *sdkp = scsi_disk_get(disk); |
| 1546 | struct scsi_device *sdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | int retval; |
| 1548 | |
Hannes Reinecke | eb72d0b | 2016-04-26 08:06:58 +0200 | [diff] [blame] | 1549 | if (!sdkp) |
| 1550 | return 0; |
| 1551 | |
| 1552 | sdp = sdkp->device; |
Tejun Heo | 2bae009 | 2010-12-18 18:42:23 +0100 | [diff] [blame] | 1553 | SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_check_events\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | |
| 1555 | /* |
| 1556 | * If the device is offline, don't send any commands - just pretend as |
| 1557 | * if the command failed. If the device ever comes back online, we |
| 1558 | * can deal with it then. It is only because of unrecoverable errors |
| 1559 | * that we would ever take a device offline in the first place. |
| 1560 | */ |
Kay Sievers | 285e967 | 2007-08-14 14:10:39 +0200 | [diff] [blame] | 1561 | if (!scsi_device_online(sdp)) { |
| 1562 | set_media_not_present(sdkp); |
Kay Sievers | 285e967 | 2007-08-14 14:10:39 +0200 | [diff] [blame] | 1563 | goto out; |
| 1564 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | |
| 1566 | /* |
| 1567 | * Using TEST_UNIT_READY enables differentiation between drive with |
| 1568 | * no cartridge loaded - NOT READY, drive with changed cartridge - |
| 1569 | * UNIT ATTENTION, or with same cartridge - GOOD STATUS. |
| 1570 | * |
| 1571 | * Drives that auto spin down. eg iomega jaz 1G, will be started |
| 1572 | * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever |
| 1573 | * sd_revalidate() is called. |
| 1574 | */ |
James Bottomley | 001aac2 | 2007-12-02 19:10:40 +0200 | [diff] [blame] | 1575 | if (scsi_block_when_processing_errors(sdp)) { |
Christoph Hellwig | 6fa2b8f | 2017-02-14 20:15:56 +0100 | [diff] [blame] | 1576 | struct scsi_sense_hdr sshdr = { 0, }; |
| 1577 | |
James Bottomley | 001aac2 | 2007-12-02 19:10:40 +0200 | [diff] [blame] | 1578 | retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES, |
Christoph Hellwig | 6fa2b8f | 2017-02-14 20:15:56 +0100 | [diff] [blame] | 1579 | &sshdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | |
Christoph Hellwig | 6fa2b8f | 2017-02-14 20:15:56 +0100 | [diff] [blame] | 1581 | /* failed to execute TUR, assume media not present */ |
| 1582 | if (host_byte(retval)) { |
| 1583 | set_media_not_present(sdkp); |
| 1584 | goto out; |
| 1585 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | |
Christoph Hellwig | 6fa2b8f | 2017-02-14 20:15:56 +0100 | [diff] [blame] | 1587 | if (media_not_present(sdkp, &sshdr)) |
| 1588 | goto out; |
| 1589 | } |
Tejun Heo | 2bae009 | 2010-12-18 18:42:23 +0100 | [diff] [blame] | 1590 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | /* |
| 1592 | * For removable scsi disk we have to recognise the presence |
Tejun Heo | 2bae009 | 2010-12-18 18:42:23 +0100 | [diff] [blame] | 1593 | * of a disk in the drive. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | */ |
Tejun Heo | 2bae009 | 2010-12-18 18:42:23 +0100 | [diff] [blame] | 1595 | if (!sdkp->media_present) |
| 1596 | sdp->changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | sdkp->media_present = 1; |
Kay Sievers | 285e967 | 2007-08-14 14:10:39 +0200 | [diff] [blame] | 1598 | out: |
Alan Stern | 3ff5588 | 2010-09-07 11:44:01 -0400 | [diff] [blame] | 1599 | /* |
Tejun Heo | 2bae009 | 2010-12-18 18:42:23 +0100 | [diff] [blame] | 1600 | * sdp->changed is set under the following conditions: |
Alan Stern | 3ff5588 | 2010-09-07 11:44:01 -0400 | [diff] [blame] | 1601 | * |
Tejun Heo | 2bae009 | 2010-12-18 18:42:23 +0100 | [diff] [blame] | 1602 | * Medium present state has changed in either direction. |
| 1603 | * Device has indicated UNIT_ATTENTION. |
Alan Stern | 3ff5588 | 2010-09-07 11:44:01 -0400 | [diff] [blame] | 1604 | */ |
Tejun Heo | 2bae009 | 2010-12-18 18:42:23 +0100 | [diff] [blame] | 1605 | retval = sdp->changed ? DISK_EVENT_MEDIA_CHANGE : 0; |
| 1606 | sdp->changed = 0; |
Hannes Reinecke | eb72d0b | 2016-04-26 08:06:58 +0200 | [diff] [blame] | 1607 | scsi_disk_put(sdkp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | } |
| 1610 | |
Derek Basehore | 4fa8324 | 2017-05-11 14:34:24 +0200 | [diff] [blame] | 1611 | static int sd_sync_cache(struct scsi_disk *sdkp, struct scsi_sense_hdr *sshdr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | int retries, res; |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1614 | struct scsi_device *sdp = sdkp->device; |
James Bottomley | 7e66010 | 2013-10-04 21:42:24 +0000 | [diff] [blame] | 1615 | const int timeout = sdp->request_queue->rq_timeout |
| 1616 | * SD_FLUSH_TIMEOUT_MULTIPLIER; |
Derek Basehore | 4fa8324 | 2017-05-11 14:34:24 +0200 | [diff] [blame] | 1617 | struct scsi_sense_hdr my_sshdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | |
| 1619 | if (!scsi_device_online(sdp)) |
| 1620 | return -ENODEV; |
| 1621 | |
Derek Basehore | 4fa8324 | 2017-05-11 14:34:24 +0200 | [diff] [blame] | 1622 | /* caller might not be interested in sense, but we need it */ |
| 1623 | if (!sshdr) |
| 1624 | sshdr = &my_sshdr; |
| 1625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | for (retries = 3; retries > 0; --retries) { |
| 1627 | unsigned char cmd[10] = { 0 }; |
| 1628 | |
| 1629 | cmd[0] = SYNCHRONIZE_CACHE; |
| 1630 | /* |
| 1631 | * Leave the rest of the command zero to indicate |
| 1632 | * flush everything. |
| 1633 | */ |
Derek Basehore | 4fa8324 | 2017-05-11 14:34:24 +0200 | [diff] [blame] | 1634 | res = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, sshdr, |
Christoph Hellwig | fcbfffe | 2017-02-23 16:02:37 +0100 | [diff] [blame] | 1635 | timeout, SD_MAX_RETRIES, 0, RQF_PM, NULL); |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1636 | if (res == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | break; |
| 1638 | } |
| 1639 | |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1640 | if (res) { |
Hannes Reinecke | ef61329 | 2014-10-24 14:27:00 +0200 | [diff] [blame] | 1641 | sd_print_result(sdkp, "Synchronize Cache(10) failed", res); |
Oliver Neukum | 9589791 | 2013-09-16 13:28:15 +0200 | [diff] [blame] | 1642 | |
Johannes Thumshirn | c65be1a | 2018-06-25 13:20:58 +0200 | [diff] [blame] | 1643 | if (driver_byte(res) == DRIVER_SENSE) |
Derek Basehore | 4fa8324 | 2017-05-11 14:34:24 +0200 | [diff] [blame] | 1644 | sd_print_sense_hdr(sdkp, sshdr); |
| 1645 | |
Oliver Neukum | 9589791 | 2013-09-16 13:28:15 +0200 | [diff] [blame] | 1646 | /* we need to evaluate the error return */ |
Derek Basehore | 4fa8324 | 2017-05-11 14:34:24 +0200 | [diff] [blame] | 1647 | if (scsi_sense_valid(sshdr) && |
| 1648 | (sshdr->asc == 0x3a || /* medium not present */ |
| 1649 | sshdr->asc == 0x20)) /* invalid command */ |
Oliver Neukum | 9589791 | 2013-09-16 13:28:15 +0200 | [diff] [blame] | 1650 | /* this is no error here */ |
| 1651 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | |
Oliver Neukum | 9589791 | 2013-09-16 13:28:15 +0200 | [diff] [blame] | 1653 | switch (host_byte(res)) { |
| 1654 | /* ignore errors due to racing a disconnection */ |
| 1655 | case DID_BAD_TARGET: |
| 1656 | case DID_NO_CONNECT: |
| 1657 | return 0; |
| 1658 | /* signal the upper layer it might try again */ |
| 1659 | case DID_BUS_BUSY: |
| 1660 | case DID_IMM_RETRY: |
| 1661 | case DID_REQUEUE: |
| 1662 | case DID_SOFT_ERROR: |
| 1663 | return -EBUSY; |
| 1664 | default: |
| 1665 | return -EIO; |
| 1666 | } |
| 1667 | } |
Tejun Heo | 3721050 | 2007-03-21 00:07:18 +0900 | [diff] [blame] | 1668 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | } |
| 1670 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | static void sd_rescan(struct device *dev) |
| 1672 | { |
Christoph Hellwig | 3d9a1f5 | 2015-02-02 14:01:25 +0100 | [diff] [blame] | 1673 | struct scsi_disk *sdkp = dev_get_drvdata(dev); |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 1674 | |
Christoph Hellwig | 3d9a1f5 | 2015-02-02 14:01:25 +0100 | [diff] [blame] | 1675 | revalidate_disk(sdkp->disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | |
| 1679 | #ifdef CONFIG_COMPAT |
| 1680 | /* |
| 1681 | * This gets directly called from VFS. When the ioctl |
| 1682 | * is not recognized we go back to the other translation paths. |
| 1683 | */ |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 1684 | static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 1685 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | { |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 1687 | struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device; |
Christoph Hellwig | 21a9d4c | 2014-10-24 18:39:37 +0200 | [diff] [blame] | 1688 | int error; |
Paolo Bonzini | 0bfc96c | 2012-01-12 16:01:28 +0100 | [diff] [blame] | 1689 | |
Christoph Hellwig | 21a9d4c | 2014-10-24 18:39:37 +0200 | [diff] [blame] | 1690 | error = scsi_ioctl_block_when_processing_errors(sdev, cmd, |
| 1691 | (mode & FMODE_NDELAY) != 0); |
| 1692 | if (error) |
| 1693 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | /* |
| 1696 | * Let the static ioctl translation table take care of it. |
| 1697 | */ |
Christoph Hellwig | 21a9d4c | 2014-10-24 18:39:37 +0200 | [diff] [blame] | 1698 | if (!sdev->host->hostt->compat_ioctl) |
| 1699 | return -ENOIOCTLCMD; |
| 1700 | return sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | } |
| 1702 | #endif |
| 1703 | |
Christoph Hellwig | 924d55b | 2015-10-15 14:10:49 +0200 | [diff] [blame] | 1704 | static char sd_pr_type(enum pr_type type) |
| 1705 | { |
| 1706 | switch (type) { |
| 1707 | case PR_WRITE_EXCLUSIVE: |
| 1708 | return 0x01; |
| 1709 | case PR_EXCLUSIVE_ACCESS: |
| 1710 | return 0x03; |
| 1711 | case PR_WRITE_EXCLUSIVE_REG_ONLY: |
| 1712 | return 0x05; |
| 1713 | case PR_EXCLUSIVE_ACCESS_REG_ONLY: |
| 1714 | return 0x06; |
| 1715 | case PR_WRITE_EXCLUSIVE_ALL_REGS: |
| 1716 | return 0x07; |
| 1717 | case PR_EXCLUSIVE_ACCESS_ALL_REGS: |
| 1718 | return 0x08; |
| 1719 | default: |
| 1720 | return 0; |
| 1721 | } |
| 1722 | }; |
| 1723 | |
| 1724 | static int sd_pr_command(struct block_device *bdev, u8 sa, |
| 1725 | u64 key, u64 sa_key, u8 type, u8 flags) |
| 1726 | { |
| 1727 | struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device; |
| 1728 | struct scsi_sense_hdr sshdr; |
| 1729 | int result; |
| 1730 | u8 cmd[16] = { 0, }; |
| 1731 | u8 data[24] = { 0, }; |
| 1732 | |
| 1733 | cmd[0] = PERSISTENT_RESERVE_OUT; |
| 1734 | cmd[1] = sa; |
| 1735 | cmd[2] = type; |
| 1736 | put_unaligned_be32(sizeof(data), &cmd[5]); |
| 1737 | |
| 1738 | put_unaligned_be64(key, &data[0]); |
| 1739 | put_unaligned_be64(sa_key, &data[8]); |
| 1740 | data[20] = flags; |
| 1741 | |
| 1742 | result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, &data, sizeof(data), |
| 1743 | &sshdr, SD_TIMEOUT, SD_MAX_RETRIES, NULL); |
| 1744 | |
Johannes Thumshirn | c65be1a | 2018-06-25 13:20:58 +0200 | [diff] [blame] | 1745 | if (driver_byte(result) == DRIVER_SENSE && |
| 1746 | scsi_sense_valid(&sshdr)) { |
Christoph Hellwig | 924d55b | 2015-10-15 14:10:49 +0200 | [diff] [blame] | 1747 | sdev_printk(KERN_INFO, sdev, "PR command failed: %d\n", result); |
| 1748 | scsi_print_sense_hdr(sdev, NULL, &sshdr); |
| 1749 | } |
| 1750 | |
| 1751 | return result; |
| 1752 | } |
| 1753 | |
| 1754 | static int sd_pr_register(struct block_device *bdev, u64 old_key, u64 new_key, |
| 1755 | u32 flags) |
| 1756 | { |
| 1757 | if (flags & ~PR_FL_IGNORE_KEY) |
| 1758 | return -EOPNOTSUPP; |
| 1759 | return sd_pr_command(bdev, (flags & PR_FL_IGNORE_KEY) ? 0x06 : 0x00, |
| 1760 | old_key, new_key, 0, |
Christoph Hellwig | 01f90dd | 2016-07-08 21:23:50 +0900 | [diff] [blame] | 1761 | (1 << 0) /* APTPL */); |
Christoph Hellwig | 924d55b | 2015-10-15 14:10:49 +0200 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | static int sd_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type, |
| 1765 | u32 flags) |
| 1766 | { |
| 1767 | if (flags) |
| 1768 | return -EOPNOTSUPP; |
| 1769 | return sd_pr_command(bdev, 0x01, key, 0, sd_pr_type(type), 0); |
| 1770 | } |
| 1771 | |
| 1772 | static int sd_pr_release(struct block_device *bdev, u64 key, enum pr_type type) |
| 1773 | { |
| 1774 | return sd_pr_command(bdev, 0x02, key, 0, sd_pr_type(type), 0); |
| 1775 | } |
| 1776 | |
| 1777 | static int sd_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key, |
| 1778 | enum pr_type type, bool abort) |
| 1779 | { |
| 1780 | return sd_pr_command(bdev, abort ? 0x05 : 0x04, old_key, new_key, |
| 1781 | sd_pr_type(type), 0); |
| 1782 | } |
| 1783 | |
| 1784 | static int sd_pr_clear(struct block_device *bdev, u64 key) |
| 1785 | { |
| 1786 | return sd_pr_command(bdev, 0x03, key, 0, 0, 0); |
| 1787 | } |
| 1788 | |
| 1789 | static const struct pr_ops sd_pr_ops = { |
| 1790 | .pr_register = sd_pr_register, |
| 1791 | .pr_reserve = sd_pr_reserve, |
| 1792 | .pr_release = sd_pr_release, |
| 1793 | .pr_preempt = sd_pr_preempt, |
| 1794 | .pr_clear = sd_pr_clear, |
| 1795 | }; |
| 1796 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 1797 | static const struct block_device_operations sd_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | .owner = THIS_MODULE, |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 1799 | .open = sd_open, |
| 1800 | .release = sd_release, |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 1801 | .ioctl = sd_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 1802 | .getgeo = sd_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | #ifdef CONFIG_COMPAT |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 1804 | .compat_ioctl = sd_compat_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | #endif |
Tejun Heo | 2bae009 | 2010-12-18 18:42:23 +0100 | [diff] [blame] | 1806 | .check_events = sd_check_events, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | .revalidate_disk = sd_revalidate_disk, |
Tejun Heo | 72ec24b | 2010-05-15 20:09:32 +0200 | [diff] [blame] | 1808 | .unlock_native_capacity = sd_unlock_native_capacity, |
Christoph Hellwig | 924d55b | 2015-10-15 14:10:49 +0200 | [diff] [blame] | 1809 | .pr_ops = &sd_pr_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | }; |
| 1811 | |
Martin K. Petersen | 18a4d0a | 2012-02-09 13:48:53 -0500 | [diff] [blame] | 1812 | /** |
Hannes Reinecke | 7a38dc0 | 2017-04-06 15:36:29 +0200 | [diff] [blame] | 1813 | * sd_eh_reset - reset error handling callback |
| 1814 | * @scmd: sd-issued command that has failed |
| 1815 | * |
| 1816 | * This function is called by the SCSI midlayer before starting |
| 1817 | * SCSI EH. When counting medium access failures we have to be |
| 1818 | * careful to register it only only once per device and SCSI EH run; |
| 1819 | * there might be several timed out commands which will cause the |
| 1820 | * 'max_medium_access_timeouts' counter to trigger after the first |
| 1821 | * SCSI EH run already and set the device to offline. |
| 1822 | * So this function resets the internal counter before starting SCSI EH. |
| 1823 | **/ |
| 1824 | static void sd_eh_reset(struct scsi_cmnd *scmd) |
| 1825 | { |
| 1826 | struct scsi_disk *sdkp = scsi_disk(scmd->request->rq_disk); |
| 1827 | |
| 1828 | /* New SCSI EH run, reset gate variable */ |
| 1829 | sdkp->ignore_medium_access_errors = false; |
| 1830 | } |
| 1831 | |
| 1832 | /** |
Martin K. Petersen | 18a4d0a | 2012-02-09 13:48:53 -0500 | [diff] [blame] | 1833 | * sd_eh_action - error handling callback |
| 1834 | * @scmd: sd-issued command that has failed |
Martin K. Petersen | 18a4d0a | 2012-02-09 13:48:53 -0500 | [diff] [blame] | 1835 | * @eh_disp: The recovery disposition suggested by the midlayer |
| 1836 | * |
James Bottomley | 2451079 | 2013-11-11 13:44:53 +0100 | [diff] [blame] | 1837 | * This function is called by the SCSI midlayer upon completion of an |
| 1838 | * error test command (currently TEST UNIT READY). The result of sending |
| 1839 | * the eh command is passed in eh_disp. We're looking for devices that |
| 1840 | * fail medium access commands but are OK with non access commands like |
| 1841 | * test unit ready (so wrongly see the device as having a successful |
| 1842 | * recovery) |
Martin K. Petersen | 18a4d0a | 2012-02-09 13:48:53 -0500 | [diff] [blame] | 1843 | **/ |
James Bottomley | 2451079 | 2013-11-11 13:44:53 +0100 | [diff] [blame] | 1844 | static int sd_eh_action(struct scsi_cmnd *scmd, int eh_disp) |
Martin K. Petersen | 18a4d0a | 2012-02-09 13:48:53 -0500 | [diff] [blame] | 1845 | { |
| 1846 | struct scsi_disk *sdkp = scsi_disk(scmd->request->rq_disk); |
Bart Van Assche | 0db6ca8 | 2017-06-02 14:21:55 -0700 | [diff] [blame] | 1847 | struct scsi_device *sdev = scmd->device; |
Martin K. Petersen | 18a4d0a | 2012-02-09 13:48:53 -0500 | [diff] [blame] | 1848 | |
Bart Van Assche | 0db6ca8 | 2017-06-02 14:21:55 -0700 | [diff] [blame] | 1849 | if (!scsi_device_online(sdev) || |
James Bottomley | 2451079 | 2013-11-11 13:44:53 +0100 | [diff] [blame] | 1850 | !scsi_medium_access_command(scmd) || |
| 1851 | host_byte(scmd->result) != DID_TIME_OUT || |
| 1852 | eh_disp != SUCCESS) |
Martin K. Petersen | 18a4d0a | 2012-02-09 13:48:53 -0500 | [diff] [blame] | 1853 | return eh_disp; |
| 1854 | |
| 1855 | /* |
| 1856 | * The device has timed out executing a medium access command. |
| 1857 | * However, the TEST UNIT READY command sent during error |
| 1858 | * handling completed successfully. Either the device is in the |
| 1859 | * process of recovering or has it suffered an internal failure |
| 1860 | * that prevents access to the storage medium. |
| 1861 | */ |
Hannes Reinecke | 7a38dc0 | 2017-04-06 15:36:29 +0200 | [diff] [blame] | 1862 | if (!sdkp->ignore_medium_access_errors) { |
| 1863 | sdkp->medium_access_timed_out++; |
| 1864 | sdkp->ignore_medium_access_errors = true; |
| 1865 | } |
Martin K. Petersen | 18a4d0a | 2012-02-09 13:48:53 -0500 | [diff] [blame] | 1866 | |
| 1867 | /* |
| 1868 | * If the device keeps failing read/write commands but TEST UNIT |
| 1869 | * READY always completes successfully we assume that medium |
| 1870 | * access is no longer possible and take the device offline. |
| 1871 | */ |
| 1872 | if (sdkp->medium_access_timed_out >= sdkp->max_medium_access_timeouts) { |
| 1873 | scmd_printk(KERN_ERR, scmd, |
| 1874 | "Medium access timeout failure. Offlining disk!\n"); |
Bart Van Assche | 0db6ca8 | 2017-06-02 14:21:55 -0700 | [diff] [blame] | 1875 | mutex_lock(&sdev->state_mutex); |
| 1876 | scsi_device_set_state(sdev, SDEV_OFFLINE); |
| 1877 | mutex_unlock(&sdev->state_mutex); |
Martin K. Petersen | 18a4d0a | 2012-02-09 13:48:53 -0500 | [diff] [blame] | 1878 | |
Hannes Reinecke | e8f8d50 | 2017-04-06 15:36:30 +0200 | [diff] [blame] | 1879 | return SUCCESS; |
Martin K. Petersen | 18a4d0a | 2012-02-09 13:48:53 -0500 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | return eh_disp; |
| 1883 | } |
| 1884 | |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 1885 | static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd) |
| 1886 | { |
Damien Le Moal | 6eadc61 | 2017-04-24 16:51:10 +0900 | [diff] [blame] | 1887 | struct request *req = scmd->request; |
| 1888 | struct scsi_device *sdev = scmd->device; |
| 1889 | unsigned int transferred, good_bytes; |
| 1890 | u64 start_lba, end_lba, bad_lba; |
| 1891 | |
| 1892 | /* |
| 1893 | * Some commands have a payload smaller than the device logical |
| 1894 | * block size (e.g. INQUIRY on a 4K disk). |
| 1895 | */ |
| 1896 | if (scsi_bufflen(scmd) <= sdev->sector_size) |
| 1897 | return 0; |
| 1898 | |
| 1899 | /* Check if we have a 'bad_lba' information */ |
| 1900 | if (!scsi_get_sense_info_fld(scmd->sense_buffer, |
| 1901 | SCSI_SENSE_BUFFERSIZE, |
| 1902 | &bad_lba)) |
| 1903 | return 0; |
| 1904 | |
| 1905 | /* |
| 1906 | * If the bad lba was reported incorrectly, we have no idea where |
| 1907 | * the error is. |
| 1908 | */ |
| 1909 | start_lba = sectors_to_logical(sdev, blk_rq_pos(req)); |
| 1910 | end_lba = start_lba + bytes_to_logical(sdev, scsi_bufflen(scmd)); |
| 1911 | if (bad_lba < start_lba || bad_lba >= end_lba) |
| 1912 | return 0; |
| 1913 | |
James Bottomley | a8733c7 | 2010-12-17 15:36:34 -0500 | [diff] [blame] | 1914 | /* |
| 1915 | * resid is optional but mostly filled in. When it's unused, |
| 1916 | * its value is zero, so we assume the whole buffer transferred |
| 1917 | */ |
Damien Le Moal | 6eadc61 | 2017-04-24 16:51:10 +0900 | [diff] [blame] | 1918 | transferred = scsi_bufflen(scmd) - scsi_get_resid(scmd); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 1919 | |
Damien Le Moal | 6eadc61 | 2017-04-24 16:51:10 +0900 | [diff] [blame] | 1920 | /* This computation should always be done in terms of the |
| 1921 | * resolution of the device's medium. |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 1922 | */ |
Damien Le Moal | 6eadc61 | 2017-04-24 16:51:10 +0900 | [diff] [blame] | 1923 | good_bytes = logical_to_bytes(sdev, bad_lba - start_lba); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 1924 | |
James Bottomley | a8733c7 | 2010-12-17 15:36:34 -0500 | [diff] [blame] | 1925 | return min(good_bytes, transferred); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 1926 | } |
| 1927 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | /** |
Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 1929 | * sd_done - bottom half handler: called when the lower level |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | * driver has completed (successfully or otherwise) a scsi command. |
| 1931 | * @SCpnt: mid-level's per command structure. |
| 1932 | * |
| 1933 | * Note: potentially run from within an ISR. Must not block. |
| 1934 | **/ |
Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 1935 | static int sd_done(struct scsi_cmnd *SCpnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | { |
| 1937 | int result = SCpnt->result; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 1938 | unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt); |
Damien Le Moal | c46f091 | 2017-03-01 17:27:00 +0900 | [diff] [blame] | 1939 | unsigned int sector_size = SCpnt->device->sector_size; |
| 1940 | unsigned int resid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | struct scsi_sense_hdr sshdr; |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 1942 | struct scsi_disk *sdkp = scsi_disk(SCpnt->request->rq_disk); |
Martin K. Petersen | 26e85fc | 2012-09-18 12:19:31 -0400 | [diff] [blame] | 1943 | struct request *req = SCpnt->request; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | int sense_valid = 0; |
| 1945 | int sense_deferred = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 1947 | switch (req_op(req)) { |
| 1948 | case REQ_OP_DISCARD: |
Christoph Hellwig | 02d2610 | 2017-04-05 19:21:02 +0200 | [diff] [blame] | 1949 | case REQ_OP_WRITE_ZEROES: |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 1950 | case REQ_OP_WRITE_SAME: |
| 1951 | case REQ_OP_ZONE_RESET: |
Martin K. Petersen | 26e85fc | 2012-09-18 12:19:31 -0400 | [diff] [blame] | 1952 | if (!result) { |
| 1953 | good_bytes = blk_rq_bytes(req); |
| 1954 | scsi_set_resid(SCpnt, 0); |
| 1955 | } else { |
| 1956 | good_bytes = 0; |
| 1957 | scsi_set_resid(SCpnt, blk_rq_bytes(req)); |
| 1958 | } |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 1959 | break; |
| 1960 | case REQ_OP_ZONE_REPORT: |
| 1961 | if (!result) { |
| 1962 | good_bytes = scsi_bufflen(SCpnt) |
| 1963 | - scsi_get_resid(SCpnt); |
| 1964 | scsi_set_resid(SCpnt, 0); |
| 1965 | } else { |
| 1966 | good_bytes = 0; |
| 1967 | scsi_set_resid(SCpnt, blk_rq_bytes(req)); |
| 1968 | } |
| 1969 | break; |
Damien Le Moal | c46f091 | 2017-03-01 17:27:00 +0900 | [diff] [blame] | 1970 | default: |
| 1971 | /* |
| 1972 | * In case of bogus fw or device, we could end up having |
| 1973 | * an unaligned partial completion. Check this here and force |
| 1974 | * alignment. |
| 1975 | */ |
| 1976 | resid = scsi_get_resid(SCpnt); |
| 1977 | if (resid & (sector_size - 1)) { |
| 1978 | sd_printk(KERN_INFO, sdkp, |
| 1979 | "Unaligned partial completion (resid=%u, sector_sz=%u)\n", |
| 1980 | resid, sector_size); |
| 1981 | resid = min(scsi_bufflen(SCpnt), |
| 1982 | round_up(resid, sector_size)); |
| 1983 | scsi_set_resid(SCpnt, resid); |
| 1984 | } |
Martin K. Petersen | 26e85fc | 2012-09-18 12:19:31 -0400 | [diff] [blame] | 1985 | } |
FUJITA Tomonori | 6a32a8a | 2010-07-21 10:29:37 +0900 | [diff] [blame] | 1986 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | if (result) { |
| 1988 | sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr); |
| 1989 | if (sense_valid) |
| 1990 | sense_deferred = scsi_sense_is_deferred(&sshdr); |
| 1991 | } |
David Jeffery | 2a863ba | 2014-04-10 11:08:30 -0400 | [diff] [blame] | 1992 | sdkp->medium_access_timed_out = 0; |
| 1993 | |
Luben Tuikov | 03aba2f | 2006-06-23 09:39:09 -0700 | [diff] [blame] | 1994 | if (driver_byte(result) != DRIVER_SENSE && |
| 1995 | (!sense_valid || sense_deferred)) |
| 1996 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | |
Luben Tuikov | 03aba2f | 2006-06-23 09:39:09 -0700 | [diff] [blame] | 1998 | switch (sshdr.sense_key) { |
| 1999 | case HARDWARE_ERROR: |
| 2000 | case MEDIUM_ERROR: |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 2001 | good_bytes = sd_completed_bytes(SCpnt); |
Luben Tuikov | 03aba2f | 2006-06-23 09:39:09 -0700 | [diff] [blame] | 2002 | break; |
| 2003 | case RECOVERED_ERROR: |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 2004 | good_bytes = scsi_bufflen(SCpnt); |
| 2005 | break; |
Jamie Wellnitz | 10dab22 | 2008-09-11 21:39:36 -0400 | [diff] [blame] | 2006 | case NO_SENSE: |
| 2007 | /* This indicates a false check condition, so ignore it. An |
| 2008 | * unknown amount of data was transferred so treat it as an |
| 2009 | * error. |
| 2010 | */ |
Jamie Wellnitz | 10dab22 | 2008-09-11 21:39:36 -0400 | [diff] [blame] | 2011 | SCpnt->result = 0; |
| 2012 | memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE); |
| 2013 | break; |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 2014 | case ABORTED_COMMAND: |
| 2015 | if (sshdr.asc == 0x10) /* DIF: Target detected corruption */ |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 2016 | good_bytes = sd_completed_bytes(SCpnt); |
Luben Tuikov | 03aba2f | 2006-06-23 09:39:09 -0700 | [diff] [blame] | 2017 | break; |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 2018 | case ILLEGAL_REQUEST: |
Christoph Hellwig | d227ec2 | 2017-04-24 19:05:12 -0400 | [diff] [blame] | 2019 | switch (sshdr.asc) { |
| 2020 | case 0x10: /* DIX: Host detected corruption */ |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 2021 | good_bytes = sd_completed_bytes(SCpnt); |
Christoph Hellwig | d227ec2 | 2017-04-24 19:05:12 -0400 | [diff] [blame] | 2022 | break; |
| 2023 | case 0x20: /* INVALID COMMAND OPCODE */ |
| 2024 | case 0x24: /* INVALID FIELD IN CDB */ |
| 2025 | switch (SCpnt->cmnd[0]) { |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 2026 | case UNMAP: |
| 2027 | sd_config_discard(sdkp, SD_LBP_DISABLE); |
| 2028 | break; |
| 2029 | case WRITE_SAME_16: |
| 2030 | case WRITE_SAME: |
Christoph Hellwig | d227ec2 | 2017-04-24 19:05:12 -0400 | [diff] [blame] | 2031 | if (SCpnt->cmnd[1] & 8) { /* UNMAP */ |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 2032 | sd_config_discard(sdkp, SD_LBP_DISABLE); |
Christoph Hellwig | d227ec2 | 2017-04-24 19:05:12 -0400 | [diff] [blame] | 2033 | } else { |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 2034 | sdkp->device->no_write_same = 1; |
| 2035 | sd_config_write_same(sdkp); |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 2036 | req->rq_flags |= RQF_QUIET; |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 2037 | } |
Christoph Hellwig | d227ec2 | 2017-04-24 19:05:12 -0400 | [diff] [blame] | 2038 | break; |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 2039 | } |
| 2040 | } |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 2041 | break; |
Luben Tuikov | 03aba2f | 2006-06-23 09:39:09 -0700 | [diff] [blame] | 2042 | default: |
| 2043 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | } |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 2045 | |
Luben Tuikov | 03aba2f | 2006-06-23 09:39:09 -0700 | [diff] [blame] | 2046 | out: |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 2047 | if (sd_is_zoned(sdkp)) |
| 2048 | sd_zbc_complete(SCpnt, good_bytes, &sshdr); |
| 2049 | |
Hannes Reinecke | ef61329 | 2014-10-24 14:27:00 +0200 | [diff] [blame] | 2050 | SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt, |
| 2051 | "sd_done: completed %d of %d bytes\n", |
| 2052 | good_bytes, scsi_bufflen(SCpnt))); |
| 2053 | |
Max Gurtovoy | 10c41dd | 2018-07-30 00:15:32 +0300 | [diff] [blame] | 2054 | if (rq_data_dir(SCpnt->request) == READ && scsi_prot_sg_count(SCpnt) && |
| 2055 | good_bytes) |
| 2056 | t10_pi_complete(SCpnt->request, sdkp->protection_type, |
| 2057 | good_bytes / scsi_prot_interval(SCpnt)); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 2058 | |
Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 2059 | return good_bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | } |
| 2061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | /* |
| 2063 | * spinup disk - called only in sd_revalidate_disk() |
| 2064 | */ |
| 2065 | static void |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 2066 | sd_spinup_disk(struct scsi_disk *sdkp) |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2067 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | unsigned char cmd[10]; |
Alan Stern | 4451e47 | 2005-07-12 10:45:17 -0400 | [diff] [blame] | 2069 | unsigned long spintime_expire = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | int retries, spintime; |
| 2071 | unsigned int the_result; |
| 2072 | struct scsi_sense_hdr sshdr; |
| 2073 | int sense_valid = 0; |
| 2074 | |
| 2075 | spintime = 0; |
| 2076 | |
| 2077 | /* Spin up drives, as required. Only do this at boot time */ |
| 2078 | /* Spinup needs to be done for module loads too. */ |
| 2079 | do { |
| 2080 | retries = 0; |
| 2081 | |
| 2082 | do { |
| 2083 | cmd[0] = TEST_UNIT_READY; |
| 2084 | memset((void *) &cmd[1], 0, 9); |
| 2085 | |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2086 | the_result = scsi_execute_req(sdkp->device, cmd, |
| 2087 | DMA_NONE, NULL, 0, |
| 2088 | &sshdr, SD_TIMEOUT, |
FUJITA Tomonori | f4f4e47 | 2008-12-04 14:24:39 +0900 | [diff] [blame] | 2089 | SD_MAX_RETRIES, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | |
Alan Stern | b4d38e3 | 2006-10-11 16:48:28 -0400 | [diff] [blame] | 2091 | /* |
| 2092 | * If the drive has indicated to us that it |
| 2093 | * doesn't have any media in it, don't bother |
| 2094 | * with any more polling. |
| 2095 | */ |
| 2096 | if (media_not_present(sdkp, &sshdr)) |
| 2097 | return; |
| 2098 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | if (the_result) |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2100 | sense_valid = scsi_sense_valid(&sshdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | retries++; |
| 2102 | } while (retries < 3 && |
| 2103 | (!scsi_status_is_good(the_result) || |
Johannes Thumshirn | c65be1a | 2018-06-25 13:20:58 +0200 | [diff] [blame] | 2104 | ((driver_byte(the_result) == DRIVER_SENSE) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | sense_valid && sshdr.sense_key == UNIT_ATTENTION))); |
| 2106 | |
Johannes Thumshirn | c65be1a | 2018-06-25 13:20:58 +0200 | [diff] [blame] | 2107 | if (driver_byte(the_result) != DRIVER_SENSE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | /* no sense, TUR either succeeded or failed |
| 2109 | * with a status error */ |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 2110 | if(!spintime && !scsi_status_is_good(the_result)) { |
Hannes Reinecke | ef61329 | 2014-10-24 14:27:00 +0200 | [diff] [blame] | 2111 | sd_print_result(sdkp, "Test Unit Ready failed", |
| 2112 | the_result); |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 2113 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | break; |
| 2115 | } |
Hannes Reinecke | ef61329 | 2014-10-24 14:27:00 +0200 | [diff] [blame] | 2116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | /* |
| 2118 | * The device does not want the automatic start to be issued. |
| 2119 | */ |
Matthew Wilcox | 33dd6f9 | 2009-02-20 06:53:48 -0700 | [diff] [blame] | 2120 | if (sdkp->device->no_start_on_add) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | |
Matthew Wilcox | 33dd6f9 | 2009-02-20 06:53:48 -0700 | [diff] [blame] | 2123 | if (sense_valid && sshdr.sense_key == NOT_READY) { |
| 2124 | if (sshdr.asc == 4 && sshdr.ascq == 3) |
| 2125 | break; /* manual intervention required */ |
| 2126 | if (sshdr.asc == 4 && sshdr.ascq == 0xb) |
| 2127 | break; /* standby */ |
| 2128 | if (sshdr.asc == 4 && sshdr.ascq == 0xc) |
| 2129 | break; /* unavailable */ |
Mahesh Rajashekhara | 505aa4b | 2018-04-17 17:03:12 +0530 | [diff] [blame] | 2130 | if (sshdr.asc == 4 && sshdr.ascq == 0x1b) |
| 2131 | break; /* sanitize in progress */ |
Matthew Wilcox | 33dd6f9 | 2009-02-20 06:53:48 -0700 | [diff] [blame] | 2132 | /* |
| 2133 | * Issue command to spin up drive when not ready |
| 2134 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | if (!spintime) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 2136 | sd_printk(KERN_NOTICE, sdkp, "Spinning up disk..."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | cmd[0] = START_STOP; |
| 2138 | cmd[1] = 1; /* Return immediately */ |
| 2139 | memset((void *) &cmd[2], 0, 8); |
| 2140 | cmd[4] = 1; /* Start spin cycle */ |
Stefan Richter | d2886ea | 2008-05-11 00:34:07 +0200 | [diff] [blame] | 2141 | if (sdkp->device->start_stop_pwr_cond) |
| 2142 | cmd[4] |= 1 << 4; |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2143 | scsi_execute_req(sdkp->device, cmd, DMA_NONE, |
| 2144 | NULL, 0, &sshdr, |
FUJITA Tomonori | f4f4e47 | 2008-12-04 14:24:39 +0900 | [diff] [blame] | 2145 | SD_TIMEOUT, SD_MAX_RETRIES, |
| 2146 | NULL); |
Alan Stern | 4451e47 | 2005-07-12 10:45:17 -0400 | [diff] [blame] | 2147 | spintime_expire = jiffies + 100 * HZ; |
| 2148 | spintime = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | /* Wait 1 second for next try */ |
| 2151 | msleep(1000); |
Michał Mirosław | 3a1d078 | 2017-11-24 18:02:35 +0100 | [diff] [blame] | 2152 | printk(KERN_CONT "."); |
Alan Stern | 4451e47 | 2005-07-12 10:45:17 -0400 | [diff] [blame] | 2153 | |
| 2154 | /* |
| 2155 | * Wait for USB flash devices with slow firmware. |
| 2156 | * Yes, this sense key/ASC combination shouldn't |
| 2157 | * occur here. It's characteristic of these devices. |
| 2158 | */ |
| 2159 | } else if (sense_valid && |
| 2160 | sshdr.sense_key == UNIT_ATTENTION && |
| 2161 | sshdr.asc == 0x28) { |
| 2162 | if (!spintime) { |
| 2163 | spintime_expire = jiffies + 5 * HZ; |
| 2164 | spintime = 1; |
| 2165 | } |
| 2166 | /* Wait 1 second for next try */ |
| 2167 | msleep(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | } else { |
| 2169 | /* we don't understand the sense code, so it's |
| 2170 | * probably pointless to loop */ |
| 2171 | if(!spintime) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 2172 | sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n"); |
| 2173 | sd_print_sense_hdr(sdkp, &sshdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | } |
| 2175 | break; |
| 2176 | } |
| 2177 | |
Alan Stern | 4451e47 | 2005-07-12 10:45:17 -0400 | [diff] [blame] | 2178 | } while (spintime && time_before_eq(jiffies, spintime_expire)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | |
| 2180 | if (spintime) { |
| 2181 | if (scsi_status_is_good(the_result)) |
Michał Mirosław | 3a1d078 | 2017-11-24 18:02:35 +0100 | [diff] [blame] | 2182 | printk(KERN_CONT "ready\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | else |
Michał Mirosław | 3a1d078 | 2017-11-24 18:02:35 +0100 | [diff] [blame] | 2184 | printk(KERN_CONT "not responding...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | } |
| 2186 | } |
| 2187 | |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 2188 | /* |
| 2189 | * Determine whether disk supports Data Integrity Field. |
| 2190 | */ |
Martin K. Petersen | fe54239 | 2012-09-21 12:44:12 -0400 | [diff] [blame] | 2191 | static int sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer) |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 2192 | { |
| 2193 | struct scsi_device *sdp = sdkp->device; |
| 2194 | u8 type; |
Martin K. Petersen | fe54239 | 2012-09-21 12:44:12 -0400 | [diff] [blame] | 2195 | int ret = 0; |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 2196 | |
| 2197 | if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0) |
Martin K. Petersen | fe54239 | 2012-09-21 12:44:12 -0400 | [diff] [blame] | 2198 | return ret; |
Martin K. Petersen | 35e1a5d | 2009-09-18 17:33:00 -0400 | [diff] [blame] | 2199 | |
| 2200 | type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */ |
| 2201 | |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 2202 | if (type > T10_PI_TYPE3_PROTECTION) |
Martin K. Petersen | fe54239 | 2012-09-21 12:44:12 -0400 | [diff] [blame] | 2203 | ret = -ENODEV; |
| 2204 | else if (scsi_host_dif_capable(sdp->host, type)) |
| 2205 | ret = 1; |
| 2206 | |
| 2207 | if (sdkp->first_scan || type != sdkp->protection_type) |
| 2208 | switch (ret) { |
| 2209 | case -ENODEV: |
| 2210 | sd_printk(KERN_ERR, sdkp, "formatted with unsupported" \ |
| 2211 | " protection type %u. Disabling disk!\n", |
| 2212 | type); |
| 2213 | break; |
| 2214 | case 1: |
| 2215 | sd_printk(KERN_NOTICE, sdkp, |
| 2216 | "Enabling DIF Type %u protection\n", type); |
| 2217 | break; |
| 2218 | case 0: |
| 2219 | sd_printk(KERN_NOTICE, sdkp, |
| 2220 | "Disabling DIF Type %u protection\n", type); |
| 2221 | break; |
| 2222 | } |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 2223 | |
Martin K. Petersen | be922f4 | 2008-09-19 18:47:20 -0400 | [diff] [blame] | 2224 | sdkp->protection_type = type; |
| 2225 | |
Martin K. Petersen | fe54239 | 2012-09-21 12:44:12 -0400 | [diff] [blame] | 2226 | return ret; |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 2227 | } |
| 2228 | |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2229 | static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp, |
| 2230 | struct scsi_sense_hdr *sshdr, int sense_valid, |
| 2231 | int the_result) |
| 2232 | { |
Johannes Thumshirn | c65be1a | 2018-06-25 13:20:58 +0200 | [diff] [blame] | 2233 | if (driver_byte(the_result) == DRIVER_SENSE) |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2234 | sd_print_sense_hdr(sdkp, sshdr); |
| 2235 | else |
| 2236 | sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n"); |
| 2237 | |
| 2238 | /* |
| 2239 | * Set dirty bit for removable devices if not ready - |
| 2240 | * sometimes drives will not report this properly. |
| 2241 | */ |
| 2242 | if (sdp->removable && |
| 2243 | sense_valid && sshdr->sense_key == NOT_READY) |
Tejun Heo | 2bae009 | 2010-12-18 18:42:23 +0100 | [diff] [blame] | 2244 | set_media_not_present(sdkp); |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2245 | |
| 2246 | /* |
| 2247 | * We used to set media_present to 0 here to indicate no media |
| 2248 | * in the drive, but some drives fail read capacity even with |
| 2249 | * media present, so we can't do that. |
| 2250 | */ |
| 2251 | sdkp->capacity = 0; /* unknown mapped to zero - as usual */ |
| 2252 | } |
| 2253 | |
| 2254 | #define RC16_LEN 32 |
| 2255 | #if RC16_LEN > SD_BUF_SIZE |
| 2256 | #error RC16_LEN must not be more than SD_BUF_SIZE |
| 2257 | #endif |
| 2258 | |
James Bottomley | 3233ac1 | 2010-04-01 10:30:01 -0400 | [diff] [blame] | 2259 | #define READ_CAPACITY_RETRIES_ON_RESET 10 |
| 2260 | |
Martin K. Petersen | 7c85615 | 2017-04-04 10:42:30 -0400 | [diff] [blame] | 2261 | /* |
| 2262 | * Ensure that we don't overflow sector_t when CONFIG_LBDAF is not set |
| 2263 | * and the reported logical block size is bigger than 512 bytes. Note |
| 2264 | * that last_sector is a u64 and therefore logical_to_sectors() is not |
| 2265 | * applicable. |
| 2266 | */ |
| 2267 | static bool sd_addressable_capacity(u64 lba, unsigned int sector_size) |
| 2268 | { |
| 2269 | u64 last_sector = (lba + 1ULL) << (ilog2(sector_size) - 9); |
| 2270 | |
| 2271 | if (sizeof(sector_t) == 4 && last_sector > U32_MAX) |
| 2272 | return false; |
| 2273 | |
| 2274 | return true; |
| 2275 | } |
| 2276 | |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2277 | static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp, |
| 2278 | unsigned char *buffer) |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2279 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | unsigned char cmd[16]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | struct scsi_sense_hdr sshdr; |
| 2282 | int sense_valid = 0; |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2283 | int the_result; |
James Bottomley | 3233ac1 | 2010-04-01 10:30:01 -0400 | [diff] [blame] | 2284 | int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET; |
Martin K. Petersen | ea09bcc | 2009-05-23 11:43:37 -0400 | [diff] [blame] | 2285 | unsigned int alignment; |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2286 | unsigned long long lba; |
| 2287 | unsigned sector_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | |
Hans de Goede | 5ce524b | 2010-10-01 14:20:10 -0700 | [diff] [blame] | 2289 | if (sdp->no_read_capacity_16) |
| 2290 | return -EINVAL; |
| 2291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | do { |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2293 | memset(cmd, 0, 16); |
Hannes Reinecke | eb846d9 | 2014-11-17 14:25:19 +0100 | [diff] [blame] | 2294 | cmd[0] = SERVICE_ACTION_IN_16; |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2295 | cmd[1] = SAI_READ_CAPACITY_16; |
| 2296 | cmd[13] = RC16_LEN; |
| 2297 | memset(buffer, 0, RC16_LEN); |
| 2298 | |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2299 | the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE, |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2300 | buffer, RC16_LEN, &sshdr, |
| 2301 | SD_TIMEOUT, SD_MAX_RETRIES, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2303 | if (media_not_present(sdkp, &sshdr)) |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2304 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | |
Matthew Wilcox | 2b30130 | 2009-03-12 14:20:30 -0400 | [diff] [blame] | 2306 | if (the_result) { |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2307 | sense_valid = scsi_sense_valid(&sshdr); |
Matthew Wilcox | 2b30130 | 2009-03-12 14:20:30 -0400 | [diff] [blame] | 2308 | if (sense_valid && |
| 2309 | sshdr.sense_key == ILLEGAL_REQUEST && |
| 2310 | (sshdr.asc == 0x20 || sshdr.asc == 0x24) && |
| 2311 | sshdr.ascq == 0x00) |
| 2312 | /* Invalid Command Operation Code or |
| 2313 | * Invalid Field in CDB, just retry |
| 2314 | * silently with RC10 */ |
| 2315 | return -EINVAL; |
James Bottomley | 3233ac1 | 2010-04-01 10:30:01 -0400 | [diff] [blame] | 2316 | if (sense_valid && |
| 2317 | sshdr.sense_key == UNIT_ATTENTION && |
| 2318 | sshdr.asc == 0x29 && sshdr.ascq == 0x00) |
| 2319 | /* Device reset might occur several times, |
| 2320 | * give it one more chance */ |
| 2321 | if (--reset_retries > 0) |
| 2322 | continue; |
Matthew Wilcox | 2b30130 | 2009-03-12 14:20:30 -0400 | [diff] [blame] | 2323 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | retries--; |
| 2325 | |
| 2326 | } while (the_result && retries); |
| 2327 | |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2328 | if (the_result) { |
Hannes Reinecke | ef61329 | 2014-10-24 14:27:00 +0200 | [diff] [blame] | 2329 | sd_print_result(sdkp, "Read Capacity(16) failed", the_result); |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2330 | read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result); |
| 2331 | return -EINVAL; |
| 2332 | } |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 2333 | |
Dave Hansen | 8f76d15 | 2009-04-21 16:43:27 -0700 | [diff] [blame] | 2334 | sector_size = get_unaligned_be32(&buffer[8]); |
| 2335 | lba = get_unaligned_be64(&buffer[0]); |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2336 | |
Martin K. Petersen | fe54239 | 2012-09-21 12:44:12 -0400 | [diff] [blame] | 2337 | if (sd_read_protection_type(sdkp, buffer) < 0) { |
| 2338 | sdkp->capacity = 0; |
| 2339 | return -ENODEV; |
| 2340 | } |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2341 | |
Martin K. Petersen | 7c85615 | 2017-04-04 10:42:30 -0400 | [diff] [blame] | 2342 | if (!sd_addressable_capacity(lba, sector_size)) { |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2343 | sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a " |
| 2344 | "kernel compiled with support for large block " |
| 2345 | "devices.\n"); |
| 2346 | sdkp->capacity = 0; |
| 2347 | return -EOVERFLOW; |
| 2348 | } |
| 2349 | |
Martin K. Petersen | ea09bcc | 2009-05-23 11:43:37 -0400 | [diff] [blame] | 2350 | /* Logical blocks per physical block exponent */ |
Martin K. Petersen | 526f7c7 | 2010-09-28 14:48:47 -0400 | [diff] [blame] | 2351 | sdkp->physical_block_size = (1 << (buffer[13] & 0xf)) * sector_size; |
Martin K. Petersen | ea09bcc | 2009-05-23 11:43:37 -0400 | [diff] [blame] | 2352 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 2353 | /* RC basis */ |
| 2354 | sdkp->rc_basis = (buffer[12] >> 4) & 0x3; |
| 2355 | |
Martin K. Petersen | ea09bcc | 2009-05-23 11:43:37 -0400 | [diff] [blame] | 2356 | /* Lowest aligned logical block */ |
| 2357 | alignment = ((buffer[14] & 0x3f) << 8 | buffer[15]) * sector_size; |
| 2358 | blk_queue_alignment_offset(sdp->request_queue, alignment); |
| 2359 | if (alignment && sdkp->first_scan) |
| 2360 | sd_printk(KERN_NOTICE, sdkp, |
| 2361 | "physical block alignment offset: %u\n", alignment); |
| 2362 | |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 2363 | if (buffer[14] & 0x80) { /* LBPME */ |
| 2364 | sdkp->lbpme = 1; |
Martin K. Petersen | e339c1a | 2009-11-26 12:00:40 -0500 | [diff] [blame] | 2365 | |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 2366 | if (buffer[14] & 0x40) /* LBPRZ */ |
| 2367 | sdkp->lbprz = 1; |
Martin K. Petersen | e339c1a | 2009-11-26 12:00:40 -0500 | [diff] [blame] | 2368 | |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 2369 | sd_config_discard(sdkp, SD_LBP_WS16); |
Martin K. Petersen | e339c1a | 2009-11-26 12:00:40 -0500 | [diff] [blame] | 2370 | } |
| 2371 | |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2372 | sdkp->capacity = lba + 1; |
| 2373 | return sector_size; |
| 2374 | } |
| 2375 | |
| 2376 | static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp, |
| 2377 | unsigned char *buffer) |
| 2378 | { |
| 2379 | unsigned char cmd[16]; |
| 2380 | struct scsi_sense_hdr sshdr; |
| 2381 | int sense_valid = 0; |
| 2382 | int the_result; |
James Bottomley | 3233ac1 | 2010-04-01 10:30:01 -0400 | [diff] [blame] | 2383 | int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET; |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2384 | sector_t lba; |
| 2385 | unsigned sector_size; |
| 2386 | |
| 2387 | do { |
| 2388 | cmd[0] = READ_CAPACITY; |
| 2389 | memset(&cmd[1], 0, 9); |
| 2390 | memset(buffer, 0, 8); |
| 2391 | |
| 2392 | the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE, |
| 2393 | buffer, 8, &sshdr, |
| 2394 | SD_TIMEOUT, SD_MAX_RETRIES, NULL); |
| 2395 | |
| 2396 | if (media_not_present(sdkp, &sshdr)) |
| 2397 | return -ENODEV; |
| 2398 | |
James Bottomley | 3233ac1 | 2010-04-01 10:30:01 -0400 | [diff] [blame] | 2399 | if (the_result) { |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2400 | sense_valid = scsi_sense_valid(&sshdr); |
James Bottomley | 3233ac1 | 2010-04-01 10:30:01 -0400 | [diff] [blame] | 2401 | if (sense_valid && |
| 2402 | sshdr.sense_key == UNIT_ATTENTION && |
| 2403 | sshdr.asc == 0x29 && sshdr.ascq == 0x00) |
| 2404 | /* Device reset might occur several times, |
| 2405 | * give it one more chance */ |
| 2406 | if (--reset_retries > 0) |
| 2407 | continue; |
| 2408 | } |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2409 | retries--; |
| 2410 | |
| 2411 | } while (the_result && retries); |
| 2412 | |
| 2413 | if (the_result) { |
Hannes Reinecke | ef61329 | 2014-10-24 14:27:00 +0200 | [diff] [blame] | 2414 | sd_print_result(sdkp, "Read Capacity(10) failed", the_result); |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2415 | read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result); |
| 2416 | return -EINVAL; |
| 2417 | } |
| 2418 | |
Dave Hansen | 8f76d15 | 2009-04-21 16:43:27 -0700 | [diff] [blame] | 2419 | sector_size = get_unaligned_be32(&buffer[4]); |
| 2420 | lba = get_unaligned_be32(&buffer[0]); |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2421 | |
Hans de Goede | 5ce524b | 2010-10-01 14:20:10 -0700 | [diff] [blame] | 2422 | if (sdp->no_read_capacity_16 && (lba == 0xffffffff)) { |
| 2423 | /* Some buggy (usb cardreader) devices return an lba of |
| 2424 | 0xffffffff when the want to report a size of 0 (with |
| 2425 | which they really mean no media is present) */ |
| 2426 | sdkp->capacity = 0; |
Linus Torvalds | 5cc1035 | 2010-10-22 20:30:48 -0700 | [diff] [blame] | 2427 | sdkp->physical_block_size = sector_size; |
Hans de Goede | 5ce524b | 2010-10-01 14:20:10 -0700 | [diff] [blame] | 2428 | return sector_size; |
| 2429 | } |
| 2430 | |
Martin K. Petersen | 7c85615 | 2017-04-04 10:42:30 -0400 | [diff] [blame] | 2431 | if (!sd_addressable_capacity(lba, sector_size)) { |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2432 | sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a " |
| 2433 | "kernel compiled with support for large block " |
| 2434 | "devices.\n"); |
| 2435 | sdkp->capacity = 0; |
| 2436 | return -EOVERFLOW; |
| 2437 | } |
| 2438 | |
| 2439 | sdkp->capacity = lba + 1; |
Martin K. Petersen | 526f7c7 | 2010-09-28 14:48:47 -0400 | [diff] [blame] | 2440 | sdkp->physical_block_size = sector_size; |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2441 | return sector_size; |
| 2442 | } |
| 2443 | |
Matthew Wilcox | 2b30130 | 2009-03-12 14:20:30 -0400 | [diff] [blame] | 2444 | static int sd_try_rc16_first(struct scsi_device *sdp) |
| 2445 | { |
Hannes Reinecke | f87146b | 2010-03-29 09:29:24 +0200 | [diff] [blame] | 2446 | if (sdp->host->max_cmd_len < 16) |
| 2447 | return 0; |
Alan Stern | 6a0bdff | 2012-06-20 16:04:19 -0400 | [diff] [blame] | 2448 | if (sdp->try_rc_10_first) |
| 2449 | return 0; |
Matthew Wilcox | 2b30130 | 2009-03-12 14:20:30 -0400 | [diff] [blame] | 2450 | if (sdp->scsi_level > SCSI_SPC_2) |
| 2451 | return 1; |
| 2452 | if (scsi_device_protection(sdp)) |
| 2453 | return 1; |
| 2454 | return 0; |
| 2455 | } |
| 2456 | |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2457 | /* |
| 2458 | * read disk capacity |
| 2459 | */ |
| 2460 | static void |
| 2461 | sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer) |
| 2462 | { |
| 2463 | int sector_size; |
| 2464 | struct scsi_device *sdp = sdkp->device; |
| 2465 | |
Matthew Wilcox | 2b30130 | 2009-03-12 14:20:30 -0400 | [diff] [blame] | 2466 | if (sd_try_rc16_first(sdp)) { |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2467 | sector_size = read_capacity_16(sdkp, sdp, buffer); |
| 2468 | if (sector_size == -EOVERFLOW) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2469 | goto got_data; |
Matthew Wilcox | 2b30130 | 2009-03-12 14:20:30 -0400 | [diff] [blame] | 2470 | if (sector_size == -ENODEV) |
| 2471 | return; |
| 2472 | if (sector_size < 0) |
| 2473 | sector_size = read_capacity_10(sdkp, sdp, buffer); |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2474 | if (sector_size < 0) |
| 2475 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2476 | } else { |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2477 | sector_size = read_capacity_10(sdkp, sdp, buffer); |
| 2478 | if (sector_size == -EOVERFLOW) |
| 2479 | goto got_data; |
| 2480 | if (sector_size < 0) |
| 2481 | return; |
| 2482 | if ((sizeof(sdkp->capacity) > 4) && |
| 2483 | (sdkp->capacity > 0xffffffffULL)) { |
| 2484 | int old_sector_size = sector_size; |
| 2485 | sd_printk(KERN_NOTICE, sdkp, "Very big device. " |
| 2486 | "Trying to use READ CAPACITY(16).\n"); |
| 2487 | sector_size = read_capacity_16(sdkp, sdp, buffer); |
| 2488 | if (sector_size < 0) { |
| 2489 | sd_printk(KERN_NOTICE, sdkp, |
| 2490 | "Using 0xffffffff as device size\n"); |
| 2491 | sdkp->capacity = 1 + (sector_t) 0xffffffff; |
| 2492 | sector_size = old_sector_size; |
| 2493 | goto got_data; |
| 2494 | } |
Martin K. Petersen | 597d740 | 2018-03-14 12:15:56 -0400 | [diff] [blame] | 2495 | /* Remember that READ CAPACITY(16) succeeded */ |
| 2496 | sdp->try_rc_10_first = 0; |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 2497 | } |
| 2498 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2499 | |
Alan Stern | 5c211ca | 2009-02-18 10:54:44 -0500 | [diff] [blame] | 2500 | /* Some devices are known to return the total number of blocks, |
| 2501 | * not the highest block number. Some devices have versions |
| 2502 | * which do this and others which do not. Some devices we might |
| 2503 | * suspect of doing this but we don't know for certain. |
| 2504 | * |
| 2505 | * If we know the reported capacity is wrong, decrement it. If |
| 2506 | * we can only guess, then assume the number of blocks is even |
| 2507 | * (usually true but not always) and err on the side of lowering |
| 2508 | * the capacity. |
| 2509 | */ |
| 2510 | if (sdp->fix_capacity || |
| 2511 | (sdp->guess_capacity && (sdkp->capacity & 0x01))) { |
| 2512 | sd_printk(KERN_INFO, sdkp, "Adjusting the sector count " |
| 2513 | "from its reported value: %llu\n", |
| 2514 | (unsigned long long) sdkp->capacity); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2515 | --sdkp->capacity; |
Oliver Neukum | 61bf54b | 2007-02-08 09:04:48 +0100 | [diff] [blame] | 2516 | } |
| 2517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2518 | got_data: |
| 2519 | if (sector_size == 0) { |
| 2520 | sector_size = 512; |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 2521 | sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, " |
| 2522 | "assuming 512.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2523 | } |
| 2524 | |
| 2525 | if (sector_size != 512 && |
| 2526 | sector_size != 1024 && |
| 2527 | sector_size != 2048 && |
Mark Hounschell | 74856fb | 2015-05-13 10:49:09 +0200 | [diff] [blame] | 2528 | sector_size != 4096) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 2529 | sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n", |
| 2530 | sector_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2531 | /* |
| 2532 | * The user might want to re-format the drive with |
| 2533 | * a supported sectorsize. Once this happens, it |
| 2534 | * would be relatively trivial to set the thing up. |
| 2535 | * For this reason, we leave the thing in the table. |
| 2536 | */ |
| 2537 | sdkp->capacity = 0; |
| 2538 | /* |
| 2539 | * set a bogus sector size so the normal read/write |
| 2540 | * logic in the block layer will eventually refuse any |
| 2541 | * request on this device without tripping over power |
| 2542 | * of two sector size assumptions |
| 2543 | */ |
| 2544 | sector_size = 512; |
| 2545 | } |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 2546 | blk_queue_logical_block_size(sdp->request_queue, sector_size); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 2547 | blk_queue_physical_block_size(sdp->request_queue, |
| 2548 | sdkp->physical_block_size); |
| 2549 | sdkp->device->sector_size = sector_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | |
Martin K. Petersen | ca369d5 | 2015-11-13 16:46:48 -0500 | [diff] [blame] | 2551 | if (sdkp->capacity > 0xffffffff) |
Martin K. Petersen | bcdb247 | 2014-06-03 18:45:51 -0400 | [diff] [blame] | 2552 | sdp->use_16_for_rw = 1; |
Jason J. Herne | 53ad570 | 2012-11-14 17:03:22 -0500 | [diff] [blame] | 2553 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | /* |
| 2557 | * Print disk capacity |
| 2558 | */ |
| 2559 | static void |
| 2560 | sd_print_capacity(struct scsi_disk *sdkp, |
| 2561 | sector_t old_capacity) |
| 2562 | { |
| 2563 | int sector_size = sdkp->device->sector_size; |
| 2564 | char cap_str_2[10], cap_str_10[10]; |
| 2565 | |
| 2566 | string_get_size(sdkp->capacity, sector_size, |
| 2567 | STRING_UNITS_2, cap_str_2, sizeof(cap_str_2)); |
| 2568 | string_get_size(sdkp->capacity, sector_size, |
| 2569 | STRING_UNITS_10, cap_str_10, |
| 2570 | sizeof(cap_str_10)); |
| 2571 | |
| 2572 | if (sdkp->first_scan || old_capacity != sdkp->capacity) { |
| 2573 | sd_printk(KERN_NOTICE, sdkp, |
| 2574 | "%llu %d-byte logical blocks: (%s/%s)\n", |
| 2575 | (unsigned long long)sdkp->capacity, |
| 2576 | sector_size, cap_str_10, cap_str_2); |
| 2577 | |
| 2578 | if (sdkp->physical_block_size != sector_size) |
| 2579 | sd_printk(KERN_NOTICE, sdkp, |
| 2580 | "%u-byte physical blocks\n", |
| 2581 | sdkp->physical_block_size); |
| 2582 | |
| 2583 | sd_zbc_print_zones(sdkp); |
| 2584 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | /* called with buffer of length 512 */ |
| 2588 | static inline int |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2589 | sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage, |
| 2590 | unsigned char *buffer, int len, struct scsi_mode_data *data, |
| 2591 | struct scsi_sense_hdr *sshdr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | { |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2593 | return scsi_mode_sense(sdp, dbd, modepage, buffer, len, |
James Bottomley | 1cf7269 | 2005-08-28 11:27:01 -0500 | [diff] [blame] | 2594 | SD_TIMEOUT, SD_MAX_RETRIES, data, |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2595 | sshdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2596 | } |
| 2597 | |
| 2598 | /* |
| 2599 | * read write protect setting, if possible - called only in sd_revalidate_disk() |
Al Viro | 4897080 | 2006-02-26 08:34:10 -0600 | [diff] [blame] | 2600 | * called with buffer of length SD_BUF_SIZE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2601 | */ |
| 2602 | static void |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 2603 | sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer) |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2604 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2605 | int res; |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2606 | struct scsi_device *sdp = sdkp->device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2607 | struct scsi_mode_data data; |
Jeremy Cline | 20bd1d0 | 2018-03-06 21:47:32 +0000 | [diff] [blame] | 2608 | int disk_ro = get_disk_ro(sdkp->disk); |
Martin K. Petersen | 70a9b87 | 2009-03-09 11:33:31 -0400 | [diff] [blame] | 2609 | int old_wp = sdkp->write_prot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2610 | |
| 2611 | set_disk_ro(sdkp->disk, 0); |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2612 | if (sdp->skip_ms_page_3f) { |
Martin K. Petersen | b2bff6c | 2014-01-03 18:19:26 -0500 | [diff] [blame] | 2613 | sd_first_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2614 | return; |
| 2615 | } |
| 2616 | |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2617 | if (sdp->use_192_bytes_for_3f) { |
| 2618 | res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2619 | } else { |
| 2620 | /* |
| 2621 | * First attempt: ask for all pages (0x3F), but only 4 bytes. |
| 2622 | * We have to start carefully: some devices hang if we ask |
| 2623 | * for more than is available. |
| 2624 | */ |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2625 | res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2626 | |
| 2627 | /* |
| 2628 | * Second attempt: ask for page 0 When only page 0 is |
| 2629 | * implemented, a request for page 3F may return Sense Key |
| 2630 | * 5: Illegal Request, Sense Code 24: Invalid field in |
| 2631 | * CDB. |
| 2632 | */ |
| 2633 | if (!scsi_status_is_good(res)) |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2634 | res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2635 | |
| 2636 | /* |
| 2637 | * Third attempt: ask 255 bytes, as we did earlier. |
| 2638 | */ |
| 2639 | if (!scsi_status_is_good(res)) |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2640 | res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255, |
| 2641 | &data, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | if (!scsi_status_is_good(res)) { |
Martin K. Petersen | b2bff6c | 2014-01-03 18:19:26 -0500 | [diff] [blame] | 2645 | sd_first_printk(KERN_WARNING, sdkp, |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 2646 | "Test WP failed, assume Write Enabled\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | } else { |
| 2648 | sdkp->write_prot = ((data.device_specific & 0x80) != 0); |
Jeremy Cline | 20bd1d0 | 2018-03-06 21:47:32 +0000 | [diff] [blame] | 2649 | set_disk_ro(sdkp->disk, sdkp->write_prot || disk_ro); |
Martin K. Petersen | 70a9b87 | 2009-03-09 11:33:31 -0400 | [diff] [blame] | 2650 | if (sdkp->first_scan || old_wp != sdkp->write_prot) { |
| 2651 | sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n", |
| 2652 | sdkp->write_prot ? "on" : "off"); |
Andy Shevchenko | df441cc | 2016-10-22 20:32:30 +0300 | [diff] [blame] | 2653 | sd_printk(KERN_DEBUG, sdkp, "Mode Sense: %4ph\n", buffer); |
Martin K. Petersen | 70a9b87 | 2009-03-09 11:33:31 -0400 | [diff] [blame] | 2654 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2655 | } |
| 2656 | } |
| 2657 | |
| 2658 | /* |
| 2659 | * sd_read_cache_type - called only from sd_revalidate_disk() |
Al Viro | 4897080 | 2006-02-26 08:34:10 -0600 | [diff] [blame] | 2660 | * called with buffer of length SD_BUF_SIZE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2661 | */ |
| 2662 | static void |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 2663 | sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer) |
Al Viro | 631e8a1 | 2005-05-16 01:59:55 +0100 | [diff] [blame] | 2664 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2665 | int len = 0, res; |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2666 | struct scsi_device *sdp = sdkp->device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2667 | |
Al Viro | 631e8a1 | 2005-05-16 01:59:55 +0100 | [diff] [blame] | 2668 | int dbd; |
| 2669 | int modepage; |
Luben Tuikov | 0bcaa11 | 2011-05-19 00:00:58 -0700 | [diff] [blame] | 2670 | int first_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2671 | struct scsi_mode_data data; |
| 2672 | struct scsi_sense_hdr sshdr; |
Martin K. Petersen | 70a9b87 | 2009-03-09 11:33:31 -0400 | [diff] [blame] | 2673 | int old_wce = sdkp->WCE; |
| 2674 | int old_rcd = sdkp->RCD; |
| 2675 | int old_dpofua = sdkp->DPOFUA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2676 | |
James Bottomley | 39c60a0 | 2013-04-24 14:02:53 -0700 | [diff] [blame] | 2677 | |
| 2678 | if (sdkp->cache_override) |
| 2679 | return; |
| 2680 | |
Luben Tuikov | 0bcaa11 | 2011-05-19 00:00:58 -0700 | [diff] [blame] | 2681 | first_len = 4; |
| 2682 | if (sdp->skip_ms_page_8) { |
| 2683 | if (sdp->type == TYPE_RBC) |
| 2684 | goto defaults; |
| 2685 | else { |
| 2686 | if (sdp->skip_ms_page_3f) |
| 2687 | goto defaults; |
| 2688 | modepage = 0x3F; |
| 2689 | if (sdp->use_192_bytes_for_3f) |
| 2690 | first_len = 192; |
| 2691 | dbd = 0; |
| 2692 | } |
| 2693 | } else if (sdp->type == TYPE_RBC) { |
Al Viro | 631e8a1 | 2005-05-16 01:59:55 +0100 | [diff] [blame] | 2694 | modepage = 6; |
| 2695 | dbd = 8; |
| 2696 | } else { |
| 2697 | modepage = 8; |
| 2698 | dbd = 0; |
| 2699 | } |
| 2700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2701 | /* cautiously ask */ |
Luben Tuikov | 0bcaa11 | 2011-05-19 00:00:58 -0700 | [diff] [blame] | 2702 | res = sd_do_mode_sense(sdp, dbd, modepage, buffer, first_len, |
| 2703 | &data, &sshdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2704 | |
| 2705 | if (!scsi_status_is_good(res)) |
| 2706 | goto bad_sense; |
| 2707 | |
Al Viro | 6d73c85 | 2006-02-23 02:03:16 +0100 | [diff] [blame] | 2708 | if (!data.header_length) { |
| 2709 | modepage = 6; |
Luben Tuikov | 0bcaa11 | 2011-05-19 00:00:58 -0700 | [diff] [blame] | 2710 | first_len = 0; |
Martin K. Petersen | b2bff6c | 2014-01-03 18:19:26 -0500 | [diff] [blame] | 2711 | sd_first_printk(KERN_ERR, sdkp, |
| 2712 | "Missing header in MODE_SENSE response\n"); |
Al Viro | 6d73c85 | 2006-02-23 02:03:16 +0100 | [diff] [blame] | 2713 | } |
| 2714 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2715 | /* that went OK, now ask for the proper length */ |
| 2716 | len = data.length; |
| 2717 | |
| 2718 | /* |
| 2719 | * We're only interested in the first three bytes, actually. |
| 2720 | * But the data cache page is defined for the first 20. |
| 2721 | */ |
| 2722 | if (len < 3) |
| 2723 | goto bad_sense; |
Luben Tuikov | 0bcaa11 | 2011-05-19 00:00:58 -0700 | [diff] [blame] | 2724 | else if (len > SD_BUF_SIZE) { |
Martin K. Petersen | b2bff6c | 2014-01-03 18:19:26 -0500 | [diff] [blame] | 2725 | sd_first_printk(KERN_NOTICE, sdkp, "Truncating mode parameter " |
Luben Tuikov | 0bcaa11 | 2011-05-19 00:00:58 -0700 | [diff] [blame] | 2726 | "data from %d to %d bytes\n", len, SD_BUF_SIZE); |
| 2727 | len = SD_BUF_SIZE; |
| 2728 | } |
| 2729 | if (modepage == 0x3F && sdp->use_192_bytes_for_3f) |
| 2730 | len = 192; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2731 | |
| 2732 | /* Get the data */ |
Luben Tuikov | 0bcaa11 | 2011-05-19 00:00:58 -0700 | [diff] [blame] | 2733 | if (len > first_len) |
| 2734 | res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, |
| 2735 | &data, &sshdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2736 | |
| 2737 | if (scsi_status_is_good(res)) { |
Al Viro | 631e8a1 | 2005-05-16 01:59:55 +0100 | [diff] [blame] | 2738 | int offset = data.header_length + data.block_descriptor_length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2739 | |
Luben Tuikov | 0bcaa11 | 2011-05-19 00:00:58 -0700 | [diff] [blame] | 2740 | while (offset < len) { |
| 2741 | u8 page_code = buffer[offset] & 0x3F; |
| 2742 | u8 spf = buffer[offset] & 0x40; |
| 2743 | |
| 2744 | if (page_code == 8 || page_code == 6) { |
| 2745 | /* We're interested only in the first 3 bytes. |
| 2746 | */ |
| 2747 | if (len - offset <= 2) { |
Martin K. Petersen | b2bff6c | 2014-01-03 18:19:26 -0500 | [diff] [blame] | 2748 | sd_first_printk(KERN_ERR, sdkp, |
| 2749 | "Incomplete mode parameter " |
| 2750 | "data\n"); |
Luben Tuikov | 0bcaa11 | 2011-05-19 00:00:58 -0700 | [diff] [blame] | 2751 | goto defaults; |
| 2752 | } else { |
| 2753 | modepage = page_code; |
| 2754 | goto Page_found; |
| 2755 | } |
| 2756 | } else { |
| 2757 | /* Go to the next page */ |
| 2758 | if (spf && len - offset > 3) |
| 2759 | offset += 4 + (buffer[offset+2] << 8) + |
| 2760 | buffer[offset+3]; |
| 2761 | else if (!spf && len - offset > 1) |
| 2762 | offset += 2 + buffer[offset+1]; |
| 2763 | else { |
Martin K. Petersen | b2bff6c | 2014-01-03 18:19:26 -0500 | [diff] [blame] | 2764 | sd_first_printk(KERN_ERR, sdkp, |
| 2765 | "Incomplete mode " |
| 2766 | "parameter data\n"); |
Luben Tuikov | 0bcaa11 | 2011-05-19 00:00:58 -0700 | [diff] [blame] | 2767 | goto defaults; |
| 2768 | } |
| 2769 | } |
Al Viro | 4897080 | 2006-02-26 08:34:10 -0600 | [diff] [blame] | 2770 | } |
| 2771 | |
Martin K. Petersen | b2bff6c | 2014-01-03 18:19:26 -0500 | [diff] [blame] | 2772 | sd_first_printk(KERN_ERR, sdkp, "No Caching mode page found\n"); |
Alan Stern | 984f173 | 2013-09-06 11:49:51 -0400 | [diff] [blame] | 2773 | goto defaults; |
| 2774 | |
Luben Tuikov | 0bcaa11 | 2011-05-19 00:00:58 -0700 | [diff] [blame] | 2775 | Page_found: |
Al Viro | 631e8a1 | 2005-05-16 01:59:55 +0100 | [diff] [blame] | 2776 | if (modepage == 8) { |
| 2777 | sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0); |
| 2778 | sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0); |
| 2779 | } else { |
| 2780 | sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0); |
| 2781 | sdkp->RCD = 0; |
| 2782 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2783 | |
Tejun Heo | 007365a | 2006-01-06 09:53:52 +0100 | [diff] [blame] | 2784 | sdkp->DPOFUA = (data.device_specific & 0x10) != 0; |
Alan Stern | b14bf2d | 2014-06-30 11:04:21 -0400 | [diff] [blame] | 2785 | if (sdp->broken_fua) { |
| 2786 | sd_first_printk(KERN_NOTICE, sdkp, "Disabling FUA\n"); |
| 2787 | sdkp->DPOFUA = 0; |
Damien Le Moal | 26f2819 | 2017-01-12 15:25:10 +0900 | [diff] [blame] | 2788 | } else if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw && |
| 2789 | !sdkp->device->use_16_for_rw) { |
Martin K. Petersen | b2bff6c | 2014-01-03 18:19:26 -0500 | [diff] [blame] | 2790 | sd_first_printk(KERN_NOTICE, sdkp, |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 2791 | "Uses READ/WRITE(6), disabling FUA\n"); |
Tejun Heo | 007365a | 2006-01-06 09:53:52 +0100 | [diff] [blame] | 2792 | sdkp->DPOFUA = 0; |
| 2793 | } |
| 2794 | |
Sujit Reddy Thumma | 2eefd57 | 2014-08-11 15:40:37 +0300 | [diff] [blame] | 2795 | /* No cache flush allowed for write protected devices */ |
| 2796 | if (sdkp->WCE && sdkp->write_prot) |
| 2797 | sdkp->WCE = 0; |
| 2798 | |
Martin K. Petersen | 70a9b87 | 2009-03-09 11:33:31 -0400 | [diff] [blame] | 2799 | if (sdkp->first_scan || old_wce != sdkp->WCE || |
| 2800 | old_rcd != sdkp->RCD || old_dpofua != sdkp->DPOFUA) |
| 2801 | sd_printk(KERN_NOTICE, sdkp, |
| 2802 | "Write cache: %s, read cache: %s, %s\n", |
| 2803 | sdkp->WCE ? "enabled" : "disabled", |
| 2804 | sdkp->RCD ? "disabled" : "enabled", |
| 2805 | sdkp->DPOFUA ? "supports DPO and FUA" |
| 2806 | : "doesn't support DPO or FUA"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2807 | |
| 2808 | return; |
| 2809 | } |
| 2810 | |
| 2811 | bad_sense: |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2812 | if (scsi_sense_valid(&sshdr) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | sshdr.sense_key == ILLEGAL_REQUEST && |
| 2814 | sshdr.asc == 0x24 && sshdr.ascq == 0x0) |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 2815 | /* Invalid field in CDB */ |
Martin K. Petersen | b2bff6c | 2014-01-03 18:19:26 -0500 | [diff] [blame] | 2816 | sd_first_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2817 | else |
Martin K. Petersen | b2bff6c | 2014-01-03 18:19:26 -0500 | [diff] [blame] | 2818 | sd_first_printk(KERN_ERR, sdkp, |
| 2819 | "Asking for cache data failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2820 | |
| 2821 | defaults: |
Namjae Jeon | b81478d | 2012-07-07 23:05:08 -0400 | [diff] [blame] | 2822 | if (sdp->wce_default_on) { |
Martin K. Petersen | b2bff6c | 2014-01-03 18:19:26 -0500 | [diff] [blame] | 2823 | sd_first_printk(KERN_NOTICE, sdkp, |
| 2824 | "Assuming drive cache: write back\n"); |
Namjae Jeon | b81478d | 2012-07-07 23:05:08 -0400 | [diff] [blame] | 2825 | sdkp->WCE = 1; |
| 2826 | } else { |
Martin K. Petersen | b2bff6c | 2014-01-03 18:19:26 -0500 | [diff] [blame] | 2827 | sd_first_printk(KERN_ERR, sdkp, |
| 2828 | "Assuming drive cache: write through\n"); |
Namjae Jeon | b81478d | 2012-07-07 23:05:08 -0400 | [diff] [blame] | 2829 | sdkp->WCE = 0; |
| 2830 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2831 | sdkp->RCD = 0; |
Al Viro | 4897080 | 2006-02-26 08:34:10 -0600 | [diff] [blame] | 2832 | sdkp->DPOFUA = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2833 | } |
| 2834 | |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 2835 | /* |
| 2836 | * The ATO bit indicates whether the DIF application tag is available |
| 2837 | * for use by the operating system. |
| 2838 | */ |
H Hartley Sweeten | 439d77f | 2010-08-10 18:01:20 -0700 | [diff] [blame] | 2839 | static void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer) |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 2840 | { |
| 2841 | int res, offset; |
| 2842 | struct scsi_device *sdp = sdkp->device; |
| 2843 | struct scsi_mode_data data; |
| 2844 | struct scsi_sense_hdr sshdr; |
| 2845 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 2846 | if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC) |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 2847 | return; |
| 2848 | |
| 2849 | if (sdkp->protection_type == 0) |
| 2850 | return; |
| 2851 | |
| 2852 | res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT, |
| 2853 | SD_MAX_RETRIES, &data, &sshdr); |
| 2854 | |
| 2855 | if (!scsi_status_is_good(res) || !data.header_length || |
| 2856 | data.length < 6) { |
Martin K. Petersen | b2bff6c | 2014-01-03 18:19:26 -0500 | [diff] [blame] | 2857 | sd_first_printk(KERN_WARNING, sdkp, |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 2858 | "getting Control mode page failed, assume no ATO\n"); |
| 2859 | |
| 2860 | if (scsi_sense_valid(&sshdr)) |
| 2861 | sd_print_sense_hdr(sdkp, &sshdr); |
| 2862 | |
| 2863 | return; |
| 2864 | } |
| 2865 | |
| 2866 | offset = data.header_length + data.block_descriptor_length; |
| 2867 | |
| 2868 | if ((buffer[offset] & 0x3f) != 0x0a) { |
Martin K. Petersen | b2bff6c | 2014-01-03 18:19:26 -0500 | [diff] [blame] | 2869 | sd_first_printk(KERN_ERR, sdkp, "ATO Got wrong page\n"); |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 2870 | return; |
| 2871 | } |
| 2872 | |
| 2873 | if ((buffer[offset + 5] & 0x80) == 0) |
| 2874 | return; |
| 2875 | |
| 2876 | sdkp->ATO = 1; |
| 2877 | |
| 2878 | return; |
| 2879 | } |
| 2880 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2881 | /** |
Martin K. Petersen | d11b691 | 2009-05-23 11:43:39 -0400 | [diff] [blame] | 2882 | * sd_read_block_limits - Query disk device for preferred I/O sizes. |
Damien Le Moal | 7529fbb | 2017-04-24 16:51:09 +0900 | [diff] [blame] | 2883 | * @sdkp: disk to query |
Martin K. Petersen | d11b691 | 2009-05-23 11:43:39 -0400 | [diff] [blame] | 2884 | */ |
| 2885 | static void sd_read_block_limits(struct scsi_disk *sdkp) |
| 2886 | { |
| 2887 | unsigned int sector_sz = sdkp->device->sector_size; |
Martin K. Petersen | bb2d3de | 2010-03-02 08:44:34 -0500 | [diff] [blame] | 2888 | const int vpd_len = 64; |
James Bottomley | e3deec0 | 2009-11-03 12:33:07 -0600 | [diff] [blame] | 2889 | unsigned char *buffer = kmalloc(vpd_len, GFP_KERNEL); |
Martin K. Petersen | d11b691 | 2009-05-23 11:43:39 -0400 | [diff] [blame] | 2890 | |
James Bottomley | e3deec0 | 2009-11-03 12:33:07 -0600 | [diff] [blame] | 2891 | if (!buffer || |
| 2892 | /* Block Limits VPD */ |
| 2893 | scsi_get_vpd_page(sdkp->device, 0xb0, buffer, vpd_len)) |
| 2894 | goto out; |
Martin K. Petersen | d11b691 | 2009-05-23 11:43:39 -0400 | [diff] [blame] | 2895 | |
| 2896 | blk_queue_io_min(sdkp->disk->queue, |
| 2897 | get_unaligned_be16(&buffer[6]) * sector_sz); |
Martin K. Petersen | ca369d5 | 2015-11-13 16:46:48 -0500 | [diff] [blame] | 2898 | |
| 2899 | sdkp->max_xfer_blocks = get_unaligned_be32(&buffer[8]); |
| 2900 | sdkp->opt_xfer_blocks = get_unaligned_be32(&buffer[12]); |
Martin K. Petersen | d11b691 | 2009-05-23 11:43:39 -0400 | [diff] [blame] | 2901 | |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 2902 | if (buffer[3] == 0x3c) { |
| 2903 | unsigned int lba_count, desc_count; |
| 2904 | |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 2905 | sdkp->max_ws_blocks = (u32)get_unaligned_be64(&buffer[36]); |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 2906 | |
| 2907 | if (!sdkp->lbpme) |
| 2908 | goto out; |
Martin K. Petersen | e339c1a | 2009-11-26 12:00:40 -0500 | [diff] [blame] | 2909 | |
| 2910 | lba_count = get_unaligned_be32(&buffer[20]); |
| 2911 | desc_count = get_unaligned_be32(&buffer[24]); |
| 2912 | |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 2913 | if (lba_count && desc_count) |
| 2914 | sdkp->max_unmap_blocks = lba_count; |
Martin K. Petersen | e339c1a | 2009-11-26 12:00:40 -0500 | [diff] [blame] | 2915 | |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 2916 | sdkp->unmap_granularity = get_unaligned_be32(&buffer[28]); |
Martin K. Petersen | e339c1a | 2009-11-26 12:00:40 -0500 | [diff] [blame] | 2917 | |
| 2918 | if (buffer[32] & 0x80) |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 2919 | sdkp->unmap_alignment = |
Martin K. Petersen | e339c1a | 2009-11-26 12:00:40 -0500 | [diff] [blame] | 2920 | get_unaligned_be32(&buffer[32]) & ~(1 << 31); |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 2921 | |
| 2922 | if (!sdkp->lbpvpd) { /* LBP VPD page not provided */ |
| 2923 | |
| 2924 | if (sdkp->max_unmap_blocks) |
| 2925 | sd_config_discard(sdkp, SD_LBP_UNMAP); |
| 2926 | else |
| 2927 | sd_config_discard(sdkp, SD_LBP_WS16); |
| 2928 | |
| 2929 | } else { /* LBP VPD page tells us what to use */ |
Martin K. Petersen | bcd069b | 2017-04-05 19:21:25 +0200 | [diff] [blame] | 2930 | if (sdkp->lbpu && sdkp->max_unmap_blocks) |
Martin K. Petersen | e461338 | 2014-12-05 08:58:03 -0500 | [diff] [blame] | 2931 | sd_config_discard(sdkp, SD_LBP_UNMAP); |
| 2932 | else if (sdkp->lbpws) |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 2933 | sd_config_discard(sdkp, SD_LBP_WS16); |
| 2934 | else if (sdkp->lbpws10) |
| 2935 | sd_config_discard(sdkp, SD_LBP_WS10); |
| 2936 | else |
| 2937 | sd_config_discard(sdkp, SD_LBP_DISABLE); |
| 2938 | } |
Martin K. Petersen | e339c1a | 2009-11-26 12:00:40 -0500 | [diff] [blame] | 2939 | } |
| 2940 | |
James Bottomley | e3deec0 | 2009-11-03 12:33:07 -0600 | [diff] [blame] | 2941 | out: |
Martin K. Petersen | d11b691 | 2009-05-23 11:43:39 -0400 | [diff] [blame] | 2942 | kfree(buffer); |
| 2943 | } |
| 2944 | |
| 2945 | /** |
Martin K. Petersen | 3821d76 | 2009-05-23 11:43:38 -0400 | [diff] [blame] | 2946 | * sd_read_block_characteristics - Query block dev. characteristics |
Damien Le Moal | 7529fbb | 2017-04-24 16:51:09 +0900 | [diff] [blame] | 2947 | * @sdkp: disk to query |
Martin K. Petersen | 3821d76 | 2009-05-23 11:43:38 -0400 | [diff] [blame] | 2948 | */ |
| 2949 | static void sd_read_block_characteristics(struct scsi_disk *sdkp) |
| 2950 | { |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 2951 | struct request_queue *q = sdkp->disk->queue; |
James Bottomley | e3deec0 | 2009-11-03 12:33:07 -0600 | [diff] [blame] | 2952 | unsigned char *buffer; |
Martin K. Petersen | 3821d76 | 2009-05-23 11:43:38 -0400 | [diff] [blame] | 2953 | u16 rot; |
Martin K. Petersen | bb2d3de | 2010-03-02 08:44:34 -0500 | [diff] [blame] | 2954 | const int vpd_len = 64; |
Martin K. Petersen | 3821d76 | 2009-05-23 11:43:38 -0400 | [diff] [blame] | 2955 | |
James Bottomley | e3deec0 | 2009-11-03 12:33:07 -0600 | [diff] [blame] | 2956 | buffer = kmalloc(vpd_len, GFP_KERNEL); |
Martin K. Petersen | 3821d76 | 2009-05-23 11:43:38 -0400 | [diff] [blame] | 2957 | |
James Bottomley | e3deec0 | 2009-11-03 12:33:07 -0600 | [diff] [blame] | 2958 | if (!buffer || |
| 2959 | /* Block Device Characteristics VPD */ |
| 2960 | scsi_get_vpd_page(sdkp->device, 0xb1, buffer, vpd_len)) |
| 2961 | goto out; |
Martin K. Petersen | 3821d76 | 2009-05-23 11:43:38 -0400 | [diff] [blame] | 2962 | |
| 2963 | rot = get_unaligned_be16(&buffer[4]); |
| 2964 | |
Mike Snitzer | b277da0 | 2014-10-04 10:55:32 -0600 | [diff] [blame] | 2965 | if (rot == 1) { |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 2966 | blk_queue_flag_set(QUEUE_FLAG_NONROT, q); |
| 2967 | blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, q); |
Mike Snitzer | b277da0 | 2014-10-04 10:55:32 -0600 | [diff] [blame] | 2968 | } |
Martin K. Petersen | 3821d76 | 2009-05-23 11:43:38 -0400 | [diff] [blame] | 2969 | |
Damien Le Moal | 68af412 | 2017-01-12 15:25:11 +0900 | [diff] [blame] | 2970 | if (sdkp->device->type == TYPE_ZBC) { |
| 2971 | /* Host-managed */ |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 2972 | q->limits.zoned = BLK_ZONED_HM; |
Damien Le Moal | 68af412 | 2017-01-12 15:25:11 +0900 | [diff] [blame] | 2973 | } else { |
| 2974 | sdkp->zoned = (buffer[8] >> 4) & 3; |
| 2975 | if (sdkp->zoned == 1) |
| 2976 | /* Host-aware */ |
| 2977 | q->limits.zoned = BLK_ZONED_HA; |
| 2978 | else |
| 2979 | /* |
| 2980 | * Treat drive-managed devices as |
| 2981 | * regular block devices. |
| 2982 | */ |
| 2983 | q->limits.zoned = BLK_ZONED_NONE; |
| 2984 | } |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 2985 | if (blk_queue_is_zoned(q) && sdkp->first_scan) |
| 2986 | sd_printk(KERN_NOTICE, sdkp, "Host-%s zoned block device\n", |
| 2987 | q->limits.zoned == BLK_ZONED_HM ? "managed" : "aware"); |
| 2988 | |
James Bottomley | e3deec0 | 2009-11-03 12:33:07 -0600 | [diff] [blame] | 2989 | out: |
Martin K. Petersen | 3821d76 | 2009-05-23 11:43:38 -0400 | [diff] [blame] | 2990 | kfree(buffer); |
| 2991 | } |
| 2992 | |
Martin K. Petersen | 045d3fe | 2010-09-10 01:22:07 -0400 | [diff] [blame] | 2993 | /** |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 2994 | * sd_read_block_provisioning - Query provisioning VPD page |
Damien Le Moal | 7529fbb | 2017-04-24 16:51:09 +0900 | [diff] [blame] | 2995 | * @sdkp: disk to query |
Martin K. Petersen | 045d3fe | 2010-09-10 01:22:07 -0400 | [diff] [blame] | 2996 | */ |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 2997 | static void sd_read_block_provisioning(struct scsi_disk *sdkp) |
Martin K. Petersen | 045d3fe | 2010-09-10 01:22:07 -0400 | [diff] [blame] | 2998 | { |
| 2999 | unsigned char *buffer; |
| 3000 | const int vpd_len = 8; |
| 3001 | |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 3002 | if (sdkp->lbpme == 0) |
Martin K. Petersen | 045d3fe | 2010-09-10 01:22:07 -0400 | [diff] [blame] | 3003 | return; |
| 3004 | |
| 3005 | buffer = kmalloc(vpd_len, GFP_KERNEL); |
| 3006 | |
| 3007 | if (!buffer || scsi_get_vpd_page(sdkp->device, 0xb2, buffer, vpd_len)) |
| 3008 | goto out; |
| 3009 | |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 3010 | sdkp->lbpvpd = 1; |
| 3011 | sdkp->lbpu = (buffer[5] >> 7) & 1; /* UNMAP */ |
| 3012 | sdkp->lbpws = (buffer[5] >> 6) & 1; /* WRITE SAME(16) with UNMAP */ |
| 3013 | sdkp->lbpws10 = (buffer[5] >> 5) & 1; /* WRITE SAME(10) with UNMAP */ |
Martin K. Petersen | 045d3fe | 2010-09-10 01:22:07 -0400 | [diff] [blame] | 3014 | |
| 3015 | out: |
| 3016 | kfree(buffer); |
| 3017 | } |
| 3018 | |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 3019 | static void sd_read_write_same(struct scsi_disk *sdkp, unsigned char *buffer) |
| 3020 | { |
Martin K. Petersen | 66c28f9 | 2013-06-06 22:15:55 -0400 | [diff] [blame] | 3021 | struct scsi_device *sdev = sdkp->device; |
| 3022 | |
Martin K. Petersen | 54b2b50 | 2013-10-23 06:25:40 -0400 | [diff] [blame] | 3023 | if (sdev->host->no_write_same) { |
| 3024 | sdev->no_write_same = 1; |
| 3025 | |
| 3026 | return; |
| 3027 | } |
| 3028 | |
Martin K. Petersen | 66c28f9 | 2013-06-06 22:15:55 -0400 | [diff] [blame] | 3029 | if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, INQUIRY) < 0) { |
Bernd Schubert | af73623 | 2013-09-23 14:47:32 +0200 | [diff] [blame] | 3030 | /* too large values might cause issues with arcmsr */ |
| 3031 | int vpd_buf_len = 64; |
| 3032 | |
Martin K. Petersen | 66c28f9 | 2013-06-06 22:15:55 -0400 | [diff] [blame] | 3033 | sdev->no_report_opcodes = 1; |
| 3034 | |
| 3035 | /* Disable WRITE SAME if REPORT SUPPORTED OPERATION |
| 3036 | * CODES is unsupported and the device has an ATA |
| 3037 | * Information VPD page (SAT). |
| 3038 | */ |
Bernd Schubert | af73623 | 2013-09-23 14:47:32 +0200 | [diff] [blame] | 3039 | if (!scsi_get_vpd_page(sdev, 0x89, buffer, vpd_buf_len)) |
Martin K. Petersen | 66c28f9 | 2013-06-06 22:15:55 -0400 | [diff] [blame] | 3040 | sdev->no_write_same = 1; |
| 3041 | } |
| 3042 | |
| 3043 | if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, WRITE_SAME_16) == 1) |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 3044 | sdkp->ws16 = 1; |
Martin K. Petersen | 66c28f9 | 2013-06-06 22:15:55 -0400 | [diff] [blame] | 3045 | |
| 3046 | if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, WRITE_SAME) == 1) |
| 3047 | sdkp->ws10 = 1; |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 3048 | } |
| 3049 | |
Christoph Hellwig | d80210f | 2017-06-19 14:26:46 +0200 | [diff] [blame] | 3050 | static void sd_read_security(struct scsi_disk *sdkp, unsigned char *buffer) |
| 3051 | { |
| 3052 | struct scsi_device *sdev = sdkp->device; |
| 3053 | |
| 3054 | if (!sdev->security_supported) |
| 3055 | return; |
| 3056 | |
| 3057 | if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, |
| 3058 | SECURITY_PROTOCOL_IN) == 1 && |
| 3059 | scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, |
| 3060 | SECURITY_PROTOCOL_OUT) == 1) |
| 3061 | sdkp->security = 1; |
| 3062 | } |
| 3063 | |
Martin K. Petersen | 852a4ab | 2019-02-12 16:21:05 -0500 | [diff] [blame] | 3064 | /* |
| 3065 | * Determine the device's preferred I/O size for reads and writes |
| 3066 | * unless the reported value is unreasonably small, large, not a |
| 3067 | * multiple of the physical block size, or simply garbage. |
| 3068 | */ |
| 3069 | static bool sd_validate_opt_xfer_size(struct scsi_disk *sdkp, |
| 3070 | unsigned int dev_max) |
| 3071 | { |
| 3072 | struct scsi_device *sdp = sdkp->device; |
| 3073 | unsigned int opt_xfer_bytes = |
| 3074 | logical_to_bytes(sdp, sdkp->opt_xfer_blocks); |
| 3075 | |
Martin K. Petersen | a52eb22 | 2019-03-27 12:11:52 -0400 | [diff] [blame^] | 3076 | if (sdkp->opt_xfer_blocks == 0) |
| 3077 | return false; |
| 3078 | |
Martin K. Petersen | 852a4ab | 2019-02-12 16:21:05 -0500 | [diff] [blame] | 3079 | if (sdkp->opt_xfer_blocks > dev_max) { |
| 3080 | sd_first_printk(KERN_WARNING, sdkp, |
| 3081 | "Optimal transfer size %u logical blocks " \ |
| 3082 | "> dev_max (%u logical blocks)\n", |
| 3083 | sdkp->opt_xfer_blocks, dev_max); |
| 3084 | return false; |
| 3085 | } |
| 3086 | |
| 3087 | if (sdkp->opt_xfer_blocks > SD_DEF_XFER_BLOCKS) { |
| 3088 | sd_first_printk(KERN_WARNING, sdkp, |
| 3089 | "Optimal transfer size %u logical blocks " \ |
| 3090 | "> sd driver limit (%u logical blocks)\n", |
| 3091 | sdkp->opt_xfer_blocks, SD_DEF_XFER_BLOCKS); |
| 3092 | return false; |
| 3093 | } |
| 3094 | |
| 3095 | if (opt_xfer_bytes < PAGE_SIZE) { |
| 3096 | sd_first_printk(KERN_WARNING, sdkp, |
| 3097 | "Optimal transfer size %u bytes < " \ |
| 3098 | "PAGE_SIZE (%u bytes)\n", |
| 3099 | opt_xfer_bytes, (unsigned int)PAGE_SIZE); |
| 3100 | return false; |
| 3101 | } |
| 3102 | |
| 3103 | if (opt_xfer_bytes & (sdkp->physical_block_size - 1)) { |
| 3104 | sd_first_printk(KERN_WARNING, sdkp, |
| 3105 | "Optimal transfer size %u bytes not a " \ |
| 3106 | "multiple of physical block size (%u bytes)\n", |
| 3107 | opt_xfer_bytes, sdkp->physical_block_size); |
| 3108 | return false; |
| 3109 | } |
| 3110 | |
| 3111 | sd_first_printk(KERN_INFO, sdkp, "Optimal transfer size %u bytes\n", |
| 3112 | opt_xfer_bytes); |
| 3113 | return true; |
| 3114 | } |
| 3115 | |
Martin K. Petersen | 3821d76 | 2009-05-23 11:43:38 -0400 | [diff] [blame] | 3116 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3117 | * sd_revalidate_disk - called the first time a new disk is seen, |
| 3118 | * performs disk spin up, read_capacity, etc. |
| 3119 | * @disk: struct gendisk we care about |
| 3120 | **/ |
| 3121 | static int sd_revalidate_disk(struct gendisk *disk) |
| 3122 | { |
| 3123 | struct scsi_disk *sdkp = scsi_disk(disk); |
| 3124 | struct scsi_device *sdp = sdkp->device; |
Martin K. Petersen | ca369d5 | 2015-11-13 16:46:48 -0500 | [diff] [blame] | 3125 | struct request_queue *q = sdkp->disk->queue; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 3126 | sector_t old_capacity = sdkp->capacity; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3127 | unsigned char *buffer; |
Martin K. Petersen | ca369d5 | 2015-11-13 16:46:48 -0500 | [diff] [blame] | 3128 | unsigned int dev_max, rw_max; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3129 | |
Martin K. Petersen | fa0d34b | 2007-02-27 22:41:19 -0500 | [diff] [blame] | 3130 | SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, |
| 3131 | "sd_revalidate_disk\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3132 | |
| 3133 | /* |
| 3134 | * If the device is offline, don't try and read capacity or any |
| 3135 | * of the other niceties. |
| 3136 | */ |
| 3137 | if (!scsi_device_online(sdp)) |
| 3138 | goto out; |
| 3139 | |
Bernhard Walle | a6123f1 | 2007-05-21 17:15:26 +0200 | [diff] [blame] | 3140 | buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3141 | if (!buffer) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 3142 | sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory " |
| 3143 | "allocation failure.\n"); |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 3144 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3145 | } |
| 3146 | |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 3147 | sd_spinup_disk(sdkp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3148 | |
| 3149 | /* |
| 3150 | * Without media there is no reason to ask; moreover, some devices |
| 3151 | * react badly if we do. |
| 3152 | */ |
| 3153 | if (sdkp->media_present) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 3154 | sd_read_capacity(sdkp, buffer); |
Martin K. Petersen | ffd4bc2 | 2009-07-29 14:06:53 -0400 | [diff] [blame] | 3155 | |
James Bottomley | 0396cf5 | 2019-02-12 08:05:25 -0800 | [diff] [blame] | 3156 | /* |
| 3157 | * set the default to rotational. All non-rotational devices |
| 3158 | * support the block characteristics VPD page, which will |
| 3159 | * cause this to be updated correctly and any device which |
| 3160 | * doesn't support it should be treated as rotational. |
| 3161 | */ |
| 3162 | blk_queue_flag_clear(QUEUE_FLAG_NONROT, q); |
| 3163 | blk_queue_flag_set(QUEUE_FLAG_ADD_RANDOM, q); |
| 3164 | |
Hannes Reinecke | 5ddfe08 | 2016-04-01 08:57:36 +0200 | [diff] [blame] | 3165 | if (scsi_device_supports_vpd(sdp)) { |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 3166 | sd_read_block_provisioning(sdkp); |
Martin K. Petersen | ffd4bc2 | 2009-07-29 14:06:53 -0400 | [diff] [blame] | 3167 | sd_read_block_limits(sdkp); |
| 3168 | sd_read_block_characteristics(sdkp); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 3169 | sd_zbc_read_zones(sdkp, buffer); |
Martin K. Petersen | ffd4bc2 | 2009-07-29 14:06:53 -0400 | [diff] [blame] | 3170 | } |
| 3171 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 3172 | sd_print_capacity(sdkp, old_capacity); |
| 3173 | |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 3174 | sd_read_write_protect_flag(sdkp, buffer); |
| 3175 | sd_read_cache_type(sdkp, buffer); |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 3176 | sd_read_app_tag_own(sdkp, buffer); |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 3177 | sd_read_write_same(sdkp, buffer); |
Christoph Hellwig | d80210f | 2017-06-19 14:26:46 +0200 | [diff] [blame] | 3178 | sd_read_security(sdkp, buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3179 | } |
Tejun Heo | 461d4e9 | 2006-01-06 09:52:55 +0100 | [diff] [blame] | 3180 | |
| 3181 | /* |
| 3182 | * We now have all cache related info, determine how we deal |
Tejun Heo | 4913efe | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 3183 | * with flush requests. |
Tejun Heo | 461d4e9 | 2006-01-06 09:52:55 +0100 | [diff] [blame] | 3184 | */ |
Vaughan Cao | cb2fb68 | 2014-06-03 17:37:30 +0800 | [diff] [blame] | 3185 | sd_set_flush_flag(sdkp); |
Tejun Heo | 461d4e9 | 2006-01-06 09:52:55 +0100 | [diff] [blame] | 3186 | |
Martin K. Petersen | ca369d5 | 2015-11-13 16:46:48 -0500 | [diff] [blame] | 3187 | /* Initial block count limit based on CDB TRANSFER LENGTH field size. */ |
| 3188 | dev_max = sdp->use_16_for_rw ? SD_MAX_XFER_BLOCKS : SD_DEF_XFER_BLOCKS; |
Brian King | 3a9794d | 2015-01-29 15:54:40 -0600 | [diff] [blame] | 3189 | |
Martin K. Petersen | ca369d5 | 2015-11-13 16:46:48 -0500 | [diff] [blame] | 3190 | /* Some devices report a maximum block count for READ/WRITE requests. */ |
| 3191 | dev_max = min_not_zero(dev_max, sdkp->max_xfer_blocks); |
| 3192 | q->limits.max_dev_sectors = logical_to_sectors(sdp, dev_max); |
| 3193 | |
Martin K. Petersen | 852a4ab | 2019-02-12 16:21:05 -0500 | [diff] [blame] | 3194 | if (sd_validate_opt_xfer_size(sdkp, dev_max)) { |
Martin K. Petersen | 6b7e9cd | 2016-05-12 22:17:34 -0400 | [diff] [blame] | 3195 | q->limits.io_opt = logical_to_bytes(sdp, sdkp->opt_xfer_blocks); |
| 3196 | rw_max = logical_to_sectors(sdp, sdkp->opt_xfer_blocks); |
| 3197 | } else |
Fam Zheng | 6780414 | 2017-03-28 12:41:26 +0800 | [diff] [blame] | 3198 | rw_max = min_not_zero(logical_to_sectors(sdp, dev_max), |
| 3199 | (sector_t)BLK_DEF_MAX_SECTORS); |
Martin K. Petersen | ca369d5 | 2015-11-13 16:46:48 -0500 | [diff] [blame] | 3200 | |
Martin K. Petersen | 77082ca | 2017-09-27 21:38:59 -0400 | [diff] [blame] | 3201 | /* Do not exceed controller limit */ |
| 3202 | rw_max = min(rw_max, queue_max_hw_sectors(q)); |
| 3203 | |
| 3204 | /* |
| 3205 | * Only update max_sectors if previously unset or if the current value |
| 3206 | * exceeds the capabilities of the hardware. |
| 3207 | */ |
| 3208 | if (sdkp->first_scan || |
| 3209 | q->limits.max_sectors > q->limits.max_dev_sectors || |
| 3210 | q->limits.max_sectors > q->limits.max_hw_sectors) |
| 3211 | q->limits.max_sectors = rw_max; |
| 3212 | |
| 3213 | sdkp->first_scan = 0; |
Martin K. Petersen | 4f258a4 | 2015-06-23 12:13:59 -0400 | [diff] [blame] | 3214 | |
Martin K. Petersen | f08bb1e | 2016-03-28 21:18:56 -0400 | [diff] [blame] | 3215 | set_capacity(disk, logical_to_sectors(sdp, sdkp->capacity)); |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 3216 | sd_config_write_same(sdkp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3217 | kfree(buffer); |
| 3218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3219 | out: |
| 3220 | return 0; |
| 3221 | } |
| 3222 | |
| 3223 | /** |
Tejun Heo | 72ec24b | 2010-05-15 20:09:32 +0200 | [diff] [blame] | 3224 | * sd_unlock_native_capacity - unlock native capacity |
| 3225 | * @disk: struct gendisk to set capacity for |
| 3226 | * |
| 3227 | * Block layer calls this function if it detects that partitions |
| 3228 | * on @disk reach beyond the end of the device. If the SCSI host |
| 3229 | * implements ->unlock_native_capacity() method, it's invoked to |
| 3230 | * give it a chance to adjust the device capacity. |
| 3231 | * |
| 3232 | * CONTEXT: |
| 3233 | * Defined by block layer. Might sleep. |
| 3234 | */ |
| 3235 | static void sd_unlock_native_capacity(struct gendisk *disk) |
| 3236 | { |
| 3237 | struct scsi_device *sdev = scsi_disk(disk)->device; |
| 3238 | |
| 3239 | if (sdev->host->hostt->unlock_native_capacity) |
| 3240 | sdev->host->hostt->unlock_native_capacity(sdev); |
| 3241 | } |
| 3242 | |
| 3243 | /** |
Tejun Heo | 3e1a7ff | 2008-08-25 19:56:17 +0900 | [diff] [blame] | 3244 | * sd_format_disk_name - format disk name |
| 3245 | * @prefix: name prefix - ie. "sd" for SCSI disks |
| 3246 | * @index: index of the disk to format name for |
| 3247 | * @buf: output buffer |
| 3248 | * @buflen: length of the output buffer |
| 3249 | * |
| 3250 | * SCSI disk names starts at sda. The 26th device is sdz and the |
| 3251 | * 27th is sdaa. The last one for two lettered suffix is sdzz |
| 3252 | * which is followed by sdaaa. |
| 3253 | * |
| 3254 | * This is basically 26 base counting with one extra 'nil' entry |
Daniel Mack | 3ad2f3f | 2010-02-03 08:01:28 +0800 | [diff] [blame] | 3255 | * at the beginning from the second digit on and can be |
Tejun Heo | 3e1a7ff | 2008-08-25 19:56:17 +0900 | [diff] [blame] | 3256 | * determined using similar method as 26 base conversion with the |
| 3257 | * index shifted -1 after each digit is computed. |
| 3258 | * |
| 3259 | * CONTEXT: |
| 3260 | * Don't care. |
| 3261 | * |
| 3262 | * RETURNS: |
| 3263 | * 0 on success, -errno on failure. |
| 3264 | */ |
| 3265 | static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen) |
| 3266 | { |
| 3267 | const int base = 'z' - 'a' + 1; |
| 3268 | char *begin = buf + strlen(prefix); |
| 3269 | char *end = buf + buflen; |
| 3270 | char *p; |
| 3271 | int unit; |
| 3272 | |
| 3273 | p = end - 1; |
| 3274 | *p = '\0'; |
| 3275 | unit = base; |
| 3276 | do { |
| 3277 | if (p == begin) |
| 3278 | return -EINVAL; |
| 3279 | *--p = 'a' + (index % unit); |
| 3280 | index = (index / unit) - 1; |
| 3281 | } while (index >= 0); |
| 3282 | |
| 3283 | memmove(begin, p, end - p); |
| 3284 | memcpy(buf, prefix, strlen(prefix)); |
| 3285 | |
| 3286 | return 0; |
| 3287 | } |
| 3288 | |
Arjan van de Ven | 4ace92f | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 3289 | /* |
| 3290 | * The asynchronous part of sd_probe |
| 3291 | */ |
| 3292 | static void sd_probe_async(void *data, async_cookie_t cookie) |
| 3293 | { |
| 3294 | struct scsi_disk *sdkp = data; |
| 3295 | struct scsi_device *sdp; |
| 3296 | struct gendisk *gd; |
| 3297 | u32 index; |
| 3298 | struct device *dev; |
| 3299 | |
| 3300 | sdp = sdkp->device; |
| 3301 | gd = sdkp->disk; |
| 3302 | index = sdkp->index; |
| 3303 | dev = &sdp->sdev_gendev; |
| 3304 | |
Michael Reed | 1a03ae0 | 2010-09-20 11:20:22 -0500 | [diff] [blame] | 3305 | gd->major = sd_major((index & 0xf0) >> 4); |
| 3306 | gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00); |
Michael Reed | 1a03ae0 | 2010-09-20 11:20:22 -0500 | [diff] [blame] | 3307 | |
Arjan van de Ven | 4ace92f | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 3308 | gd->fops = &sd_fops; |
| 3309 | gd->private_data = &sdkp->driver; |
| 3310 | gd->queue = sdkp->device->request_queue; |
| 3311 | |
Martin K. Petersen | 70a9b87 | 2009-03-09 11:33:31 -0400 | [diff] [blame] | 3312 | /* defaults, until the device tells us otherwise */ |
| 3313 | sdp->sector_size = 512; |
| 3314 | sdkp->capacity = 0; |
| 3315 | sdkp->media_present = 1; |
| 3316 | sdkp->write_prot = 0; |
James Bottomley | 39c60a0 | 2013-04-24 14:02:53 -0700 | [diff] [blame] | 3317 | sdkp->cache_override = 0; |
Martin K. Petersen | 70a9b87 | 2009-03-09 11:33:31 -0400 | [diff] [blame] | 3318 | sdkp->WCE = 0; |
| 3319 | sdkp->RCD = 0; |
| 3320 | sdkp->ATO = 0; |
| 3321 | sdkp->first_scan = 1; |
Martin K. Petersen | 18a4d0a | 2012-02-09 13:48:53 -0500 | [diff] [blame] | 3322 | sdkp->max_medium_access_timeouts = SD_MAX_MEDIUM_TIMEOUTS; |
Martin K. Petersen | 70a9b87 | 2009-03-09 11:33:31 -0400 | [diff] [blame] | 3323 | |
Arjan van de Ven | 4ace92f | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 3324 | sd_revalidate_disk(gd); |
| 3325 | |
NeilBrown | 97fedbb | 2010-03-16 08:55:32 +0100 | [diff] [blame] | 3326 | gd->flags = GENHD_FL_EXT_DEVT; |
Tejun Heo | 2bae009 | 2010-12-18 18:42:23 +0100 | [diff] [blame] | 3327 | if (sdp->removable) { |
Arjan van de Ven | 4ace92f | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 3328 | gd->flags |= GENHD_FL_REMOVABLE; |
Tejun Heo | 2bae009 | 2010-12-18 18:42:23 +0100 | [diff] [blame] | 3329 | gd->events |= DISK_EVENT_MEDIA_CHANGE; |
| 3330 | } |
Arjan van de Ven | 4ace92f | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 3331 | |
Aaron Lu | 10c580e | 2013-10-10 13:22:36 +0800 | [diff] [blame] | 3332 | blk_pm_runtime_init(sdp->request_queue, dev); |
Dan Williams | 0d52c756 | 2016-06-15 19:44:20 -0700 | [diff] [blame] | 3333 | device_add_disk(dev, gd); |
Martin K. Petersen | fe54239 | 2012-09-21 12:44:12 -0400 | [diff] [blame] | 3334 | if (sdkp->capacity) |
| 3335 | sd_dif_config_host(sdkp); |
Arjan van de Ven | 4ace92f | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 3336 | |
Martin K. Petersen | 3821d76 | 2009-05-23 11:43:38 -0400 | [diff] [blame] | 3337 | sd_revalidate_disk(gd); |
| 3338 | |
Christoph Hellwig | d80210f | 2017-06-19 14:26:46 +0200 | [diff] [blame] | 3339 | if (sdkp->security) { |
| 3340 | sdkp->opal_dev = init_opal_dev(sdp, &sd_sec_submit); |
| 3341 | if (sdkp->opal_dev) |
| 3342 | sd_printk(KERN_NOTICE, sdkp, "supports TCG Opal\n"); |
| 3343 | } |
| 3344 | |
Arjan van de Ven | 4ace92f | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 3345 | sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n", |
| 3346 | sdp->removable ? "removable " : ""); |
Alan Stern | 478a8a0 | 2010-06-16 14:52:17 -0400 | [diff] [blame] | 3347 | scsi_autopm_put_device(sdp); |
James Bottomley | ea038f6 | 2009-08-21 09:47:54 -0600 | [diff] [blame] | 3348 | put_device(&sdkp->dev); |
Arjan van de Ven | 4ace92f | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 3349 | } |
| 3350 | |
Tejun Heo | 3e1a7ff | 2008-08-25 19:56:17 +0900 | [diff] [blame] | 3351 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3352 | * sd_probe - called during driver initialization and whenever a |
| 3353 | * new scsi device is attached to the system. It is called once |
| 3354 | * for each scsi device (not just disks) present. |
| 3355 | * @dev: pointer to device object |
| 3356 | * |
| 3357 | * Returns 0 if successful (or not interested in this scsi device |
| 3358 | * (e.g. scanner)); 1 when there is an error. |
| 3359 | * |
| 3360 | * Note: this function is invoked from the scsi mid-level. |
| 3361 | * This function sets up the mapping between a given |
| 3362 | * <host,channel,id,lun> (found in sdp) and new device name |
| 3363 | * (e.g. /dev/sda). More precisely it is the block device major |
| 3364 | * and minor number that is chosen here. |
| 3365 | * |
Petr Uzel | 2db93ce | 2012-02-24 16:56:54 +0100 | [diff] [blame] | 3366 | * Assume sd_probe is not re-entrant (for time being) |
| 3367 | * Also think about sd_probe() and sd_remove() running coincidentally. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3368 | **/ |
| 3369 | static int sd_probe(struct device *dev) |
| 3370 | { |
| 3371 | struct scsi_device *sdp = to_scsi_device(dev); |
| 3372 | struct scsi_disk *sdkp; |
| 3373 | struct gendisk *gd; |
H Hartley Sweeten | 439d77f | 2010-08-10 18:01:20 -0700 | [diff] [blame] | 3374 | int index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3375 | int error; |
| 3376 | |
Subhash Jadavani | 6fe8c1d | 2014-09-10 14:54:09 +0300 | [diff] [blame] | 3377 | scsi_autopm_get_device(sdp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3378 | error = -ENODEV; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 3379 | if (sdp->type != TYPE_DISK && |
| 3380 | sdp->type != TYPE_ZBC && |
| 3381 | sdp->type != TYPE_MOD && |
| 3382 | sdp->type != TYPE_RBC) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3383 | goto out; |
| 3384 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 3385 | #ifndef CONFIG_BLK_DEV_ZONED |
| 3386 | if (sdp->type == TYPE_ZBC) |
| 3387 | goto out; |
| 3388 | #endif |
James Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 3389 | SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp, |
Petr Uzel | 2db93ce | 2012-02-24 16:56:54 +0100 | [diff] [blame] | 3390 | "sd_probe\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3391 | |
| 3392 | error = -ENOMEM; |
Jes Sorensen | 24669f75 | 2006-01-16 10:31:18 -0500 | [diff] [blame] | 3393 | sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3394 | if (!sdkp) |
| 3395 | goto out; |
| 3396 | |
Tejun Heo | 689d6fa | 2008-08-25 19:56:16 +0900 | [diff] [blame] | 3397 | gd = alloc_disk(SD_MINORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3398 | if (!gd) |
Jan Kara | c01228d | 2017-03-08 17:48:34 +0100 | [diff] [blame] | 3399 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3400 | |
Matthew Wilcox | 9401508 | 2018-06-11 15:26:27 -0400 | [diff] [blame] | 3401 | index = ida_alloc(&sd_index_ida, GFP_KERNEL); |
| 3402 | if (index < 0) { |
Dave Kleikamp | 21208ae | 2011-10-19 11:49:04 -0500 | [diff] [blame] | 3403 | sdev_printk(KERN_WARNING, sdp, "sd_probe: memory exhausted.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3404 | goto out_put; |
Michael Reed | 1a03ae0 | 2010-09-20 11:20:22 -0500 | [diff] [blame] | 3405 | } |
| 3406 | |
Tejun Heo | 3e1a7ff | 2008-08-25 19:56:17 +0900 | [diff] [blame] | 3407 | error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN); |
Dave Kleikamp | 21208ae | 2011-10-19 11:49:04 -0500 | [diff] [blame] | 3408 | if (error) { |
| 3409 | sdev_printk(KERN_WARNING, sdp, "SCSI disk (sd) name length exceeded.\n"); |
Tejun Heo | f27bac2 | 2008-07-14 14:59:30 +0900 | [diff] [blame] | 3410 | goto out_free_index; |
Dave Kleikamp | 21208ae | 2011-10-19 11:49:04 -0500 | [diff] [blame] | 3411 | } |
Tejun Heo | f27bac2 | 2008-07-14 14:59:30 +0900 | [diff] [blame] | 3412 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3413 | sdkp->device = sdp; |
| 3414 | sdkp->driver = &sd_template; |
| 3415 | sdkp->disk = gd; |
| 3416 | sdkp->index = index; |
Arnd Bergmann | 409f349 | 2010-07-07 16:51:29 +0200 | [diff] [blame] | 3417 | atomic_set(&sdkp->openers, 0); |
Josh Hunt | 9e1a153 | 2012-06-09 07:03:39 -0700 | [diff] [blame] | 3418 | atomic_set(&sdkp->device->ioerr_cnt, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3419 | |
James Bottomley | 601e763 | 2009-05-26 20:35:48 +0000 | [diff] [blame] | 3420 | if (!sdp->request_queue->rq_timeout) { |
| 3421 | if (sdp->type != TYPE_MOD) |
| 3422 | blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT); |
| 3423 | else |
| 3424 | blk_queue_rq_timeout(sdp->request_queue, |
| 3425 | SD_MOD_TIMEOUT); |
| 3426 | } |
| 3427 | |
| 3428 | device_initialize(&sdkp->dev); |
Alan Stern | 478a8a0 | 2010-06-16 14:52:17 -0400 | [diff] [blame] | 3429 | sdkp->dev.parent = dev; |
James Bottomley | 601e763 | 2009-05-26 20:35:48 +0000 | [diff] [blame] | 3430 | sdkp->dev.class = &sd_disk_class; |
Kees Cook | 02aa2a3 | 2013-07-03 15:04:56 -0700 | [diff] [blame] | 3431 | dev_set_name(&sdkp->dev, "%s", dev_name(dev)); |
James Bottomley | 601e763 | 2009-05-26 20:35:48 +0000 | [diff] [blame] | 3432 | |
Dan Carpenter | dee0586 | 2015-01-19 17:41:12 +0300 | [diff] [blame] | 3433 | error = device_add(&sdkp->dev); |
| 3434 | if (error) |
James Bottomley | 601e763 | 2009-05-26 20:35:48 +0000 | [diff] [blame] | 3435 | goto out_free_index; |
| 3436 | |
Alan Stern | 478a8a0 | 2010-06-16 14:52:17 -0400 | [diff] [blame] | 3437 | get_device(dev); |
| 3438 | dev_set_drvdata(dev, sdkp); |
James Bottomley | 601e763 | 2009-05-26 20:35:48 +0000 | [diff] [blame] | 3439 | |
James Bottomley | ea038f6 | 2009-08-21 09:47:54 -0600 | [diff] [blame] | 3440 | get_device(&sdkp->dev); /* prevent release before async_schedule */ |
Dan Williams | a7a20d1 | 2012-03-22 17:05:11 -0700 | [diff] [blame] | 3441 | async_schedule_domain(sd_probe_async, sdkp, &scsi_sd_probe_domain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3442 | |
| 3443 | return 0; |
| 3444 | |
Tejun Heo | f27bac2 | 2008-07-14 14:59:30 +0900 | [diff] [blame] | 3445 | out_free_index: |
Matthew Wilcox | 9401508 | 2018-06-11 15:26:27 -0400 | [diff] [blame] | 3446 | ida_free(&sd_index_ida, index); |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 3447 | out_put: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3448 | put_disk(gd); |
Colin Ian King | f170396 | 2017-02-03 19:38:54 +0000 | [diff] [blame] | 3449 | out_free: |
| 3450 | kfree(sdkp); |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 3451 | out: |
Subhash Jadavani | 6fe8c1d | 2014-09-10 14:54:09 +0300 | [diff] [blame] | 3452 | scsi_autopm_put_device(sdp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3453 | return error; |
| 3454 | } |
| 3455 | |
| 3456 | /** |
| 3457 | * sd_remove - called whenever a scsi disk (previously recognized by |
| 3458 | * sd_probe) is detached from the system. It is called (potentially |
| 3459 | * multiple times) during sd module unload. |
John Pittman | f2a3313 | 2017-01-12 16:17:20 -0500 | [diff] [blame] | 3460 | * @dev: pointer to device object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3461 | * |
| 3462 | * Note: this function is invoked from the scsi mid-level. |
| 3463 | * This function potentially frees up a device name (e.g. /dev/sdc) |
| 3464 | * that could be re-used by a subsequent sd_probe(). |
| 3465 | * This function is not called when the built-in sd driver is "exit-ed". |
| 3466 | **/ |
| 3467 | static int sd_remove(struct device *dev) |
| 3468 | { |
James Bottomley | 601e763 | 2009-05-26 20:35:48 +0000 | [diff] [blame] | 3469 | struct scsi_disk *sdkp; |
Hannes Reinecke | 0761df9 | 2013-05-10 11:06:16 +0200 | [diff] [blame] | 3470 | dev_t devt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3471 | |
James Bottomley | 601e763 | 2009-05-26 20:35:48 +0000 | [diff] [blame] | 3472 | sdkp = dev_get_drvdata(dev); |
Hannes Reinecke | 0761df9 | 2013-05-10 11:06:16 +0200 | [diff] [blame] | 3473 | devt = disk_devt(sdkp->disk); |
Alan Stern | 478a8a0 | 2010-06-16 14:52:17 -0400 | [diff] [blame] | 3474 | scsi_autopm_get_device(sdkp->device); |
| 3475 | |
Dan Williams | 3c31b52 | 2014-04-10 15:30:35 -0700 | [diff] [blame] | 3476 | async_synchronize_full_domain(&scsi_sd_pm_domain); |
Dan Williams | a7a20d1 | 2012-03-22 17:05:11 -0700 | [diff] [blame] | 3477 | async_synchronize_full_domain(&scsi_sd_probe_domain); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 3478 | device_del(&sdkp->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3479 | del_gendisk(sdkp->disk); |
| 3480 | sd_shutdown(dev); |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 3481 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 3482 | sd_zbc_remove(sdkp); |
| 3483 | |
Christoph Hellwig | d80210f | 2017-06-19 14:26:46 +0200 | [diff] [blame] | 3484 | free_opal_dev(sdkp->opal_dev); |
| 3485 | |
Hannes Reinecke | 0761df9 | 2013-05-10 11:06:16 +0200 | [diff] [blame] | 3486 | blk_register_region(devt, SD_MINORS, NULL, |
| 3487 | sd_default_probe, NULL, NULL); |
| 3488 | |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 3489 | mutex_lock(&sd_ref_mutex); |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 3490 | dev_set_drvdata(dev, NULL); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 3491 | put_device(&sdkp->dev); |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 3492 | mutex_unlock(&sd_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3493 | |
| 3494 | return 0; |
| 3495 | } |
| 3496 | |
| 3497 | /** |
| 3498 | * scsi_disk_release - Called to free the scsi_disk structure |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 3499 | * @dev: pointer to embedded class device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3500 | * |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 3501 | * sd_ref_mutex must be held entering this routine. Because it is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3502 | * called on last put, you should always use the scsi_disk_get() |
| 3503 | * scsi_disk_put() helpers which manipulate the semaphore directly |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 3504 | * and never do a direct put_device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3505 | **/ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 3506 | static void scsi_disk_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3507 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 3508 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3509 | struct gendisk *disk = sdkp->disk; |
Bart Van Assche | d726587 | 2019-03-25 10:01:46 -0700 | [diff] [blame] | 3510 | struct request_queue *q = disk->queue; |
| 3511 | |
Matthew Wilcox | 9401508 | 2018-06-11 15:26:27 -0400 | [diff] [blame] | 3512 | ida_free(&sd_index_ida, sdkp->index); |
Jan Kara | c01228d | 2017-03-08 17:48:34 +0100 | [diff] [blame] | 3513 | |
Bart Van Assche | d726587 | 2019-03-25 10:01:46 -0700 | [diff] [blame] | 3514 | /* |
| 3515 | * Wait until all requests that are in progress have completed. |
| 3516 | * This is necessary to avoid that e.g. scsi_end_request() crashes |
| 3517 | * due to clearing the disk->private_data pointer. Wait from inside |
| 3518 | * scsi_disk_release() instead of from sd_release() to avoid that |
| 3519 | * freezing and unfreezing the request queue affects user space I/O |
| 3520 | * in case multiple processes open a /dev/sd... node concurrently. |
| 3521 | */ |
| 3522 | blk_mq_freeze_queue(q); |
| 3523 | blk_mq_unfreeze_queue(q); |
| 3524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3525 | disk->private_data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3526 | put_disk(disk); |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 3527 | put_device(&sdkp->device->sdev_gendev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3528 | |
| 3529 | kfree(sdkp); |
| 3530 | } |
| 3531 | |
James Bottomley | cc5d2c8 | 2007-03-20 12:26:03 -0500 | [diff] [blame] | 3532 | static int sd_start_stop_device(struct scsi_disk *sdkp, int start) |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 3533 | { |
| 3534 | unsigned char cmd[6] = { START_STOP }; /* START_VALID */ |
| 3535 | struct scsi_sense_hdr sshdr; |
James Bottomley | cc5d2c8 | 2007-03-20 12:26:03 -0500 | [diff] [blame] | 3536 | struct scsi_device *sdp = sdkp->device; |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 3537 | int res; |
| 3538 | |
| 3539 | if (start) |
| 3540 | cmd[4] |= 1; /* START */ |
| 3541 | |
Stefan Richter | d2886ea | 2008-05-11 00:34:07 +0200 | [diff] [blame] | 3542 | if (sdp->start_stop_pwr_cond) |
| 3543 | cmd[4] |= start ? 1 << 4 : 3 << 4; /* Active or Standby */ |
| 3544 | |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 3545 | if (!scsi_device_online(sdp)) |
| 3546 | return -ENODEV; |
| 3547 | |
Christoph Hellwig | fcbfffe | 2017-02-23 16:02:37 +0100 | [diff] [blame] | 3548 | res = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr, |
| 3549 | SD_TIMEOUT, SD_MAX_RETRIES, 0, RQF_PM, NULL); |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 3550 | if (res) { |
Hannes Reinecke | ef61329 | 2014-10-24 14:27:00 +0200 | [diff] [blame] | 3551 | sd_print_result(sdkp, "Start/Stop Unit failed", res); |
Johannes Thumshirn | c65be1a | 2018-06-25 13:20:58 +0200 | [diff] [blame] | 3552 | if (driver_byte(res) == DRIVER_SENSE) |
James Bottomley | cc5d2c8 | 2007-03-20 12:26:03 -0500 | [diff] [blame] | 3553 | sd_print_sense_hdr(sdkp, &sshdr); |
Oliver Neukum | 9589791 | 2013-09-16 13:28:15 +0200 | [diff] [blame] | 3554 | if (scsi_sense_valid(&sshdr) && |
| 3555 | /* 0x3a is medium not present */ |
| 3556 | sshdr.asc == 0x3a) |
| 3557 | res = 0; |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 3558 | } |
| 3559 | |
Oliver Neukum | 9589791 | 2013-09-16 13:28:15 +0200 | [diff] [blame] | 3560 | /* SCSI error codes must not go to the generic layer */ |
| 3561 | if (res) |
| 3562 | return -EIO; |
| 3563 | |
| 3564 | return 0; |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 3565 | } |
| 3566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3567 | /* |
| 3568 | * Send a SYNCHRONIZE CACHE instruction down to the device through |
| 3569 | * the normal SCSI command structure. Wait for the command to |
| 3570 | * complete. |
| 3571 | */ |
| 3572 | static void sd_shutdown(struct device *dev) |
| 3573 | { |
Christoph Hellwig | 3d9a1f5 | 2015-02-02 14:01:25 +0100 | [diff] [blame] | 3574 | struct scsi_disk *sdkp = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3575 | |
| 3576 | if (!sdkp) |
| 3577 | return; /* this can happen */ |
| 3578 | |
Lin Ming | 54f57588 | 2011-12-05 09:20:26 +0800 | [diff] [blame] | 3579 | if (pm_runtime_suspended(dev)) |
Christoph Hellwig | 3d9a1f5 | 2015-02-02 14:01:25 +0100 | [diff] [blame] | 3580 | return; |
Lin Ming | 54f57588 | 2011-12-05 09:20:26 +0800 | [diff] [blame] | 3581 | |
Oliver Neukum | 9589791 | 2013-09-16 13:28:15 +0200 | [diff] [blame] | 3582 | if (sdkp->WCE && sdkp->media_present) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 3583 | sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n"); |
Derek Basehore | 4fa8324 | 2017-05-11 14:34:24 +0200 | [diff] [blame] | 3584 | sd_sync_cache(sdkp, NULL); |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 3585 | } |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 3586 | |
James Bottomley | cc5d2c8 | 2007-03-20 12:26:03 -0500 | [diff] [blame] | 3587 | if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) { |
| 3588 | sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n"); |
| 3589 | sd_start_stop_device(sdkp, 0); |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 3590 | } |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 3591 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3592 | |
Oliver Neukum | 9589791 | 2013-09-16 13:28:15 +0200 | [diff] [blame] | 3593 | static int sd_suspend_common(struct device *dev, bool ignore_stop_errors) |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 3594 | { |
Christoph Hellwig | 3d9a1f5 | 2015-02-02 14:01:25 +0100 | [diff] [blame] | 3595 | struct scsi_disk *sdkp = dev_get_drvdata(dev); |
Derek Basehore | 4fa8324 | 2017-05-11 14:34:24 +0200 | [diff] [blame] | 3596 | struct scsi_sense_hdr sshdr; |
Alan Stern | 09ff92f | 2007-05-21 09:55:04 -0400 | [diff] [blame] | 3597 | int ret = 0; |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 3598 | |
Alan Stern | 13b4389 | 2016-01-20 11:26:01 -0500 | [diff] [blame] | 3599 | if (!sdkp) /* E.g.: runtime suspend following sd_remove() */ |
| 3600 | return 0; |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 3601 | |
Oliver Neukum | 9589791 | 2013-09-16 13:28:15 +0200 | [diff] [blame] | 3602 | if (sdkp->WCE && sdkp->media_present) { |
James Bottomley | cc5d2c8 | 2007-03-20 12:26:03 -0500 | [diff] [blame] | 3603 | sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n"); |
Derek Basehore | 4fa8324 | 2017-05-11 14:34:24 +0200 | [diff] [blame] | 3604 | ret = sd_sync_cache(sdkp, &sshdr); |
| 3605 | |
Oliver Neukum | 9589791 | 2013-09-16 13:28:15 +0200 | [diff] [blame] | 3606 | if (ret) { |
| 3607 | /* ignore OFFLINE device */ |
| 3608 | if (ret == -ENODEV) |
Derek Basehore | 4fa8324 | 2017-05-11 14:34:24 +0200 | [diff] [blame] | 3609 | return 0; |
| 3610 | |
| 3611 | if (!scsi_sense_valid(&sshdr) || |
| 3612 | sshdr.sense_key != ILLEGAL_REQUEST) |
| 3613 | return ret; |
| 3614 | |
| 3615 | /* |
| 3616 | * sshdr.sense_key == ILLEGAL_REQUEST means this drive |
| 3617 | * doesn't support sync. There's not much to do and |
| 3618 | * suspend shouldn't fail. |
| 3619 | */ |
Bart Van Assche | ed91f7e | 2017-08-25 13:46:34 -0700 | [diff] [blame] | 3620 | ret = 0; |
Oliver Neukum | 9589791 | 2013-09-16 13:28:15 +0200 | [diff] [blame] | 3621 | } |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 3622 | } |
| 3623 | |
Aaron Lu | 691e3d3 | 2012-11-09 15:27:55 +0800 | [diff] [blame] | 3624 | if (sdkp->device->manage_start_stop) { |
James Bottomley | cc5d2c8 | 2007-03-20 12:26:03 -0500 | [diff] [blame] | 3625 | sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n"); |
Oliver Neukum | 9589791 | 2013-09-16 13:28:15 +0200 | [diff] [blame] | 3626 | /* an error is not worth aborting a system sleep */ |
James Bottomley | cc5d2c8 | 2007-03-20 12:26:03 -0500 | [diff] [blame] | 3627 | ret = sd_start_stop_device(sdkp, 0); |
Oliver Neukum | 9589791 | 2013-09-16 13:28:15 +0200 | [diff] [blame] | 3628 | if (ignore_stop_errors) |
| 3629 | ret = 0; |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 3630 | } |
| 3631 | |
Alan Stern | 09ff92f | 2007-05-21 09:55:04 -0400 | [diff] [blame] | 3632 | return ret; |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 3633 | } |
| 3634 | |
Oliver Neukum | 9589791 | 2013-09-16 13:28:15 +0200 | [diff] [blame] | 3635 | static int sd_suspend_system(struct device *dev) |
| 3636 | { |
| 3637 | return sd_suspend_common(dev, true); |
| 3638 | } |
| 3639 | |
| 3640 | static int sd_suspend_runtime(struct device *dev) |
| 3641 | { |
| 3642 | return sd_suspend_common(dev, false); |
| 3643 | } |
| 3644 | |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 3645 | static int sd_resume(struct device *dev) |
| 3646 | { |
Christoph Hellwig | 3d9a1f5 | 2015-02-02 14:01:25 +0100 | [diff] [blame] | 3647 | struct scsi_disk *sdkp = dev_get_drvdata(dev); |
Christoph Hellwig | d80210f | 2017-06-19 14:26:46 +0200 | [diff] [blame] | 3648 | int ret; |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 3649 | |
Alan Stern | 13b4389 | 2016-01-20 11:26:01 -0500 | [diff] [blame] | 3650 | if (!sdkp) /* E.g.: runtime resume at the start of sd_probe() */ |
| 3651 | return 0; |
| 3652 | |
James Bottomley | cc5d2c8 | 2007-03-20 12:26:03 -0500 | [diff] [blame] | 3653 | if (!sdkp->device->manage_start_stop) |
Christoph Hellwig | 3d9a1f5 | 2015-02-02 14:01:25 +0100 | [diff] [blame] | 3654 | return 0; |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 3655 | |
James Bottomley | cc5d2c8 | 2007-03-20 12:26:03 -0500 | [diff] [blame] | 3656 | sd_printk(KERN_NOTICE, sdkp, "Starting disk\n"); |
Christoph Hellwig | d80210f | 2017-06-19 14:26:46 +0200 | [diff] [blame] | 3657 | ret = sd_start_stop_device(sdkp, 1); |
| 3658 | if (!ret) |
| 3659 | opal_unlock_from_suspend(sdkp->opal_dev); |
| 3660 | return ret; |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 3661 | } |
| 3662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3663 | /** |
| 3664 | * init_sd - entry point for this driver (both when built in or when |
| 3665 | * a module). |
| 3666 | * |
| 3667 | * Note: this function registers this driver with the scsi mid-level. |
| 3668 | **/ |
| 3669 | static int __init init_sd(void) |
| 3670 | { |
Jeff Garzik | 5e4009b | 2006-10-04 05:32:54 -0400 | [diff] [blame] | 3671 | int majors = 0, i, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3672 | |
| 3673 | SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n")); |
| 3674 | |
Hannes Reinecke | 0761df9 | 2013-05-10 11:06:16 +0200 | [diff] [blame] | 3675 | for (i = 0; i < SD_MAJORS; i++) { |
| 3676 | if (register_blkdev(sd_major(i), "sd") != 0) |
| 3677 | continue; |
| 3678 | majors++; |
| 3679 | blk_register_region(sd_major(i), SD_MINORS, NULL, |
| 3680 | sd_default_probe, NULL, NULL); |
| 3681 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3682 | |
| 3683 | if (!majors) |
| 3684 | return -ENODEV; |
| 3685 | |
Jeff Garzik | 5e4009b | 2006-10-04 05:32:54 -0400 | [diff] [blame] | 3686 | err = class_register(&sd_disk_class); |
| 3687 | if (err) |
| 3688 | goto err_out; |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 3689 | |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 3690 | sd_cdb_cache = kmem_cache_create("sd_ext_cdb", SD_EXT_CDB_SIZE, |
| 3691 | 0, 0, NULL); |
| 3692 | if (!sd_cdb_cache) { |
| 3693 | printk(KERN_ERR "sd: can't init extended cdb cache\n"); |
Clément Calmels | 8d96447 | 2014-06-03 23:34:25 +0200 | [diff] [blame] | 3694 | err = -ENOMEM; |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 3695 | goto err_out_class; |
| 3696 | } |
| 3697 | |
| 3698 | sd_cdb_pool = mempool_create_slab_pool(SD_MEMPOOL_SIZE, sd_cdb_cache); |
| 3699 | if (!sd_cdb_pool) { |
| 3700 | printk(KERN_ERR "sd: can't init extended cdb pool\n"); |
Clément Calmels | 8d96447 | 2014-06-03 23:34:25 +0200 | [diff] [blame] | 3701 | err = -ENOMEM; |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 3702 | goto err_out_cache; |
| 3703 | } |
| 3704 | |
Jens Axboe | 024d515 | 2018-12-12 06:46:55 -0700 | [diff] [blame] | 3705 | sd_page_pool = mempool_create_page_pool(SD_MEMPOOL_SIZE, 0); |
| 3706 | if (!sd_page_pool) { |
| 3707 | printk(KERN_ERR "sd: can't init discard page pool\n"); |
| 3708 | err = -ENOMEM; |
| 3709 | goto err_out_ppool; |
| 3710 | } |
| 3711 | |
Joel D. Diaz | afd5e34 | 2012-10-10 10:36:11 +0200 | [diff] [blame] | 3712 | err = scsi_register_driver(&sd_template.gendrv); |
| 3713 | if (err) |
| 3714 | goto err_out_driver; |
| 3715 | |
Jeff Garzik | 5e4009b | 2006-10-04 05:32:54 -0400 | [diff] [blame] | 3716 | return 0; |
| 3717 | |
Joel D. Diaz | afd5e34 | 2012-10-10 10:36:11 +0200 | [diff] [blame] | 3718 | err_out_driver: |
Jens Axboe | 024d515 | 2018-12-12 06:46:55 -0700 | [diff] [blame] | 3719 | mempool_destroy(sd_page_pool); |
| 3720 | |
| 3721 | err_out_ppool: |
Joel D. Diaz | afd5e34 | 2012-10-10 10:36:11 +0200 | [diff] [blame] | 3722 | mempool_destroy(sd_cdb_pool); |
| 3723 | |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 3724 | err_out_cache: |
| 3725 | kmem_cache_destroy(sd_cdb_cache); |
| 3726 | |
Jeff Garzik | 5e4009b | 2006-10-04 05:32:54 -0400 | [diff] [blame] | 3727 | err_out_class: |
| 3728 | class_unregister(&sd_disk_class); |
| 3729 | err_out: |
| 3730 | for (i = 0; i < SD_MAJORS; i++) |
| 3731 | unregister_blkdev(sd_major(i), "sd"); |
| 3732 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3733 | } |
| 3734 | |
| 3735 | /** |
| 3736 | * exit_sd - exit point for this driver (when it is a module). |
| 3737 | * |
| 3738 | * Note: this function unregisters this driver from the scsi mid-level. |
| 3739 | **/ |
| 3740 | static void __exit exit_sd(void) |
| 3741 | { |
| 3742 | int i; |
| 3743 | |
| 3744 | SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n")); |
| 3745 | |
Joel D. Diaz | afd5e34 | 2012-10-10 10:36:11 +0200 | [diff] [blame] | 3746 | scsi_unregister_driver(&sd_template.gendrv); |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 3747 | mempool_destroy(sd_cdb_pool); |
Jens Axboe | 024d515 | 2018-12-12 06:46:55 -0700 | [diff] [blame] | 3748 | mempool_destroy(sd_page_pool); |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 3749 | kmem_cache_destroy(sd_cdb_cache); |
| 3750 | |
Jeff Garzik | 5e4009b | 2006-10-04 05:32:54 -0400 | [diff] [blame] | 3751 | class_unregister(&sd_disk_class); |
| 3752 | |
Hannes Reinecke | 0761df9 | 2013-05-10 11:06:16 +0200 | [diff] [blame] | 3753 | for (i = 0; i < SD_MAJORS; i++) { |
| 3754 | blk_unregister_region(sd_major(i), SD_MINORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3755 | unregister_blkdev(sd_major(i), "sd"); |
Hannes Reinecke | 0761df9 | 2013-05-10 11:06:16 +0200 | [diff] [blame] | 3756 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3757 | } |
| 3758 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3759 | module_init(init_sd); |
| 3760 | module_exit(exit_sd); |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 3761 | |
| 3762 | static void sd_print_sense_hdr(struct scsi_disk *sdkp, |
| 3763 | struct scsi_sense_hdr *sshdr) |
| 3764 | { |
Hannes Reinecke | 2104551 | 2015-01-08 07:43:46 +0100 | [diff] [blame] | 3765 | scsi_print_sense_hdr(sdkp->device, |
| 3766 | sdkp->disk ? sdkp->disk->disk_name : NULL, sshdr); |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 3767 | } |
| 3768 | |
Hannes Reinecke | ef61329 | 2014-10-24 14:27:00 +0200 | [diff] [blame] | 3769 | static void sd_print_result(const struct scsi_disk *sdkp, const char *msg, |
| 3770 | int result) |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 3771 | { |
Hannes Reinecke | ef61329 | 2014-10-24 14:27:00 +0200 | [diff] [blame] | 3772 | const char *hb_string = scsi_hostbyte_string(result); |
| 3773 | const char *db_string = scsi_driverbyte_string(result); |
| 3774 | |
| 3775 | if (hb_string || db_string) |
| 3776 | sd_printk(KERN_INFO, sdkp, |
| 3777 | "%s: Result: hostbyte=%s driverbyte=%s\n", msg, |
| 3778 | hb_string ? hb_string : "invalid", |
| 3779 | db_string ? db_string : "invalid"); |
| 3780 | else |
| 3781 | sd_printk(KERN_INFO, sdkp, |
| 3782 | "%s: Result: hostbyte=0x%02x driverbyte=0x%02x\n", |
| 3783 | msg, host_byte(result), driver_byte(result)); |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 3784 | } |
| 3785 | |