Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * NVM Express device driver |
Matthew Wilcox | 8757ad6 | 2014-04-11 10:37:39 -0400 | [diff] [blame] | 3 | * Copyright (c) 2011-2014, Intel Corporation. |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | /* |
| 16 | * Refer to the SCSI-NVMe Translation spec for details on how |
| 17 | * each command is translated. |
| 18 | */ |
| 19 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 20 | #include <linux/bio.h> |
| 21 | #include <linux/bitops.h> |
| 22 | #include <linux/blkdev.h> |
Keith Busch | 320a382 | 2013-10-23 13:07:34 -0600 | [diff] [blame] | 23 | #include <linux/compat.h> |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 24 | #include <linux/delay.h> |
| 25 | #include <linux/errno.h> |
| 26 | #include <linux/fs.h> |
| 27 | #include <linux/genhd.h> |
| 28 | #include <linux/idr.h> |
| 29 | #include <linux/init.h> |
| 30 | #include <linux/interrupt.h> |
| 31 | #include <linux/io.h> |
| 32 | #include <linux/kdev_t.h> |
| 33 | #include <linux/kthread.h> |
| 34 | #include <linux/kernel.h> |
| 35 | #include <linux/mm.h> |
| 36 | #include <linux/module.h> |
| 37 | #include <linux/moduleparam.h> |
| 38 | #include <linux/pci.h> |
| 39 | #include <linux/poison.h> |
| 40 | #include <linux/sched.h> |
| 41 | #include <linux/slab.h> |
| 42 | #include <linux/types.h> |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 43 | #include <asm/unaligned.h> |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 44 | #include <scsi/sg.h> |
| 45 | #include <scsi/scsi.h> |
| 46 | |
Christoph Hellwig | f11bb3e | 2015-10-03 15:46:41 +0200 | [diff] [blame] | 47 | #include "nvme.h" |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 48 | |
| 49 | static int sg_version_num = 30534; /* 2 digits for each component */ |
| 50 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 51 | /* VPD Page Codes */ |
| 52 | #define VPD_SUPPORTED_PAGES 0x00 |
| 53 | #define VPD_SERIAL_NUMBER 0x80 |
| 54 | #define VPD_DEVICE_IDENTIFIERS 0x83 |
| 55 | #define VPD_EXTENDED_INQUIRY 0x86 |
Keith Busch | 7f749d9 | 2015-04-07 15:34:18 -0600 | [diff] [blame] | 56 | #define VPD_BLOCK_LIMITS 0xB0 |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 57 | #define VPD_BLOCK_DEV_CHARACTERISTICS 0xB1 |
| 58 | |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 59 | /* format unit paramter list offsets */ |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 60 | #define FORMAT_UNIT_SHORT_PARM_LIST_LEN 4 |
| 61 | #define FORMAT_UNIT_LONG_PARM_LIST_LEN 8 |
| 62 | #define FORMAT_UNIT_PROT_INT_OFFSET 3 |
| 63 | #define FORMAT_UNIT_PROT_FIELD_USAGE_OFFSET 0 |
| 64 | #define FORMAT_UNIT_PROT_FIELD_USAGE_MASK 0x07 |
| 65 | |
| 66 | /* Misc. defines */ |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 67 | #define FIXED_SENSE_DATA 0x70 |
| 68 | #define DESC_FORMAT_SENSE_DATA 0x72 |
| 69 | #define FIXED_SENSE_DATA_ADD_LENGTH 10 |
| 70 | #define LUN_ENTRY_SIZE 8 |
| 71 | #define LUN_DATA_HEADER_SIZE 8 |
| 72 | #define ALL_LUNS_RETURNED 0x02 |
| 73 | #define ALL_WELL_KNOWN_LUNS_RETURNED 0x01 |
| 74 | #define RESTRICTED_LUNS_RETURNED 0x00 |
| 75 | #define NVME_POWER_STATE_START_VALID 0x00 |
| 76 | #define NVME_POWER_STATE_ACTIVE 0x01 |
| 77 | #define NVME_POWER_STATE_IDLE 0x02 |
| 78 | #define NVME_POWER_STATE_STANDBY 0x03 |
| 79 | #define NVME_POWER_STATE_LU_CONTROL 0x07 |
| 80 | #define POWER_STATE_0 0 |
| 81 | #define POWER_STATE_1 1 |
| 82 | #define POWER_STATE_2 2 |
| 83 | #define POWER_STATE_3 3 |
| 84 | #define DOWNLOAD_SAVE_ACTIVATE 0x05 |
| 85 | #define DOWNLOAD_SAVE_DEFER_ACTIVATE 0x0E |
| 86 | #define ACTIVATE_DEFERRED_MICROCODE 0x0F |
| 87 | #define FORMAT_UNIT_IMMED_MASK 0x2 |
| 88 | #define FORMAT_UNIT_IMMED_OFFSET 1 |
| 89 | #define KELVIN_TEMP_FACTOR 273 |
| 90 | #define FIXED_FMT_SENSE_DATA_SIZE 18 |
| 91 | #define DESC_FMT_SENSE_DATA_SIZE 8 |
| 92 | |
| 93 | /* SCSI/NVMe defines and bit masks */ |
| 94 | #define INQ_STANDARD_INQUIRY_PAGE 0x00 |
| 95 | #define INQ_SUPPORTED_VPD_PAGES_PAGE 0x00 |
| 96 | #define INQ_UNIT_SERIAL_NUMBER_PAGE 0x80 |
| 97 | #define INQ_DEVICE_IDENTIFICATION_PAGE 0x83 |
| 98 | #define INQ_EXTENDED_INQUIRY_DATA_PAGE 0x86 |
Keith Busch | 7f749d9 | 2015-04-07 15:34:18 -0600 | [diff] [blame] | 99 | #define INQ_BDEV_LIMITS_PAGE 0xB0 |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 100 | #define INQ_BDEV_CHARACTERISTICS_PAGE 0xB1 |
| 101 | #define INQ_SERIAL_NUMBER_LENGTH 0x14 |
Keith Busch | 7f749d9 | 2015-04-07 15:34:18 -0600 | [diff] [blame] | 102 | #define INQ_NUM_SUPPORTED_VPD_PAGES 6 |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 103 | #define VERSION_SPC_4 0x06 |
| 104 | #define ACA_UNSUPPORTED 0 |
| 105 | #define STANDARD_INQUIRY_LENGTH 36 |
| 106 | #define ADDITIONAL_STD_INQ_LENGTH 31 |
| 107 | #define EXTENDED_INQUIRY_DATA_PAGE_LENGTH 0x3C |
| 108 | #define RESERVED_FIELD 0 |
| 109 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 110 | /* Mode Sense/Select defines */ |
| 111 | #define MODE_PAGE_INFO_EXCEP 0x1C |
| 112 | #define MODE_PAGE_CACHING 0x08 |
| 113 | #define MODE_PAGE_CONTROL 0x0A |
| 114 | #define MODE_PAGE_POWER_CONDITION 0x1A |
| 115 | #define MODE_PAGE_RETURN_ALL 0x3F |
| 116 | #define MODE_PAGE_BLK_DES_LEN 0x08 |
| 117 | #define MODE_PAGE_LLBAA_BLK_DES_LEN 0x10 |
| 118 | #define MODE_PAGE_CACHING_LEN 0x14 |
| 119 | #define MODE_PAGE_CONTROL_LEN 0x0C |
| 120 | #define MODE_PAGE_POW_CND_LEN 0x28 |
| 121 | #define MODE_PAGE_INF_EXC_LEN 0x0C |
| 122 | #define MODE_PAGE_ALL_LEN 0x54 |
| 123 | #define MODE_SENSE6_MPH_SIZE 4 |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 124 | #define MODE_SENSE_PAGE_CONTROL_MASK 0xC0 |
| 125 | #define MODE_SENSE_PAGE_CODE_OFFSET 2 |
| 126 | #define MODE_SENSE_PAGE_CODE_MASK 0x3F |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 127 | #define MODE_SENSE_LLBAA_MASK 0x10 |
| 128 | #define MODE_SENSE_LLBAA_SHIFT 4 |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 129 | #define MODE_SENSE_DBD_MASK 8 |
| 130 | #define MODE_SENSE_DBD_SHIFT 3 |
| 131 | #define MODE_SENSE10_MPH_SIZE 8 |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 132 | #define MODE_SELECT_CDB_PAGE_FORMAT_MASK 0x10 |
| 133 | #define MODE_SELECT_CDB_SAVE_PAGES_MASK 0x1 |
| 134 | #define MODE_SELECT_6_BD_OFFSET 3 |
| 135 | #define MODE_SELECT_10_BD_OFFSET 6 |
| 136 | #define MODE_SELECT_10_LLBAA_OFFSET 4 |
| 137 | #define MODE_SELECT_10_LLBAA_MASK 1 |
| 138 | #define MODE_SELECT_6_MPH_SIZE 4 |
| 139 | #define MODE_SELECT_10_MPH_SIZE 8 |
| 140 | #define CACHING_MODE_PAGE_WCE_MASK 0x04 |
| 141 | #define MODE_SENSE_BLK_DESC_ENABLED 0 |
| 142 | #define MODE_SENSE_BLK_DESC_COUNT 1 |
| 143 | #define MODE_SELECT_PAGE_CODE_MASK 0x3F |
| 144 | #define SHORT_DESC_BLOCK 8 |
| 145 | #define LONG_DESC_BLOCK 16 |
| 146 | #define MODE_PAGE_POW_CND_LEN_FIELD 0x26 |
| 147 | #define MODE_PAGE_INF_EXC_LEN_FIELD 0x0A |
| 148 | #define MODE_PAGE_CACHING_LEN_FIELD 0x12 |
| 149 | #define MODE_PAGE_CONTROL_LEN_FIELD 0x0A |
| 150 | #define MODE_SENSE_PC_CURRENT_VALUES 0 |
| 151 | |
| 152 | /* Log Sense defines */ |
| 153 | #define LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE 0x00 |
| 154 | #define LOG_PAGE_SUPPORTED_LOG_PAGES_LENGTH 0x07 |
| 155 | #define LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE 0x2F |
| 156 | #define LOG_PAGE_TEMPERATURE_PAGE 0x0D |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 157 | #define LOG_SENSE_CDB_SP_NOT_ENABLED 0 |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 158 | #define LOG_SENSE_CDB_PC_MASK 0xC0 |
| 159 | #define LOG_SENSE_CDB_PC_SHIFT 6 |
| 160 | #define LOG_SENSE_CDB_PC_CUMULATIVE_VALUES 1 |
| 161 | #define LOG_SENSE_CDB_PAGE_CODE_MASK 0x3F |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 162 | #define REMAINING_INFO_EXCP_PAGE_LENGTH 0x8 |
| 163 | #define LOG_INFO_EXCP_PAGE_LENGTH 0xC |
| 164 | #define REMAINING_TEMP_PAGE_LENGTH 0xC |
| 165 | #define LOG_TEMP_PAGE_LENGTH 0x10 |
| 166 | #define LOG_TEMP_UNKNOWN 0xFF |
| 167 | #define SUPPORTED_LOG_PAGES_PAGE_LENGTH 0x3 |
| 168 | |
| 169 | /* Read Capacity defines */ |
| 170 | #define READ_CAP_10_RESP_SIZE 8 |
| 171 | #define READ_CAP_16_RESP_SIZE 32 |
| 172 | |
| 173 | /* NVMe Namespace and Command Defines */ |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 174 | #define BYTES_TO_DWORDS 4 |
| 175 | #define NVME_MAX_FIRMWARE_SLOT 7 |
| 176 | |
| 177 | /* Report LUNs defines */ |
| 178 | #define REPORT_LUNS_FIRST_LUN_OFFSET 8 |
| 179 | |
| 180 | /* SCSI ADDITIONAL SENSE Codes */ |
| 181 | |
| 182 | #define SCSI_ASC_NO_SENSE 0x00 |
| 183 | #define SCSI_ASC_PERIPHERAL_DEV_WRITE_FAULT 0x03 |
| 184 | #define SCSI_ASC_LUN_NOT_READY 0x04 |
| 185 | #define SCSI_ASC_WARNING 0x0B |
| 186 | #define SCSI_ASC_LOG_BLOCK_GUARD_CHECK_FAILED 0x10 |
| 187 | #define SCSI_ASC_LOG_BLOCK_APPTAG_CHECK_FAILED 0x10 |
| 188 | #define SCSI_ASC_LOG_BLOCK_REFTAG_CHECK_FAILED 0x10 |
| 189 | #define SCSI_ASC_UNRECOVERED_READ_ERROR 0x11 |
| 190 | #define SCSI_ASC_MISCOMPARE_DURING_VERIFY 0x1D |
| 191 | #define SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID 0x20 |
| 192 | #define SCSI_ASC_ILLEGAL_COMMAND 0x20 |
| 193 | #define SCSI_ASC_ILLEGAL_BLOCK 0x21 |
| 194 | #define SCSI_ASC_INVALID_CDB 0x24 |
| 195 | #define SCSI_ASC_INVALID_LUN 0x25 |
| 196 | #define SCSI_ASC_INVALID_PARAMETER 0x26 |
| 197 | #define SCSI_ASC_FORMAT_COMMAND_FAILED 0x31 |
| 198 | #define SCSI_ASC_INTERNAL_TARGET_FAILURE 0x44 |
| 199 | |
| 200 | /* SCSI ADDITIONAL SENSE Code Qualifiers */ |
| 201 | |
| 202 | #define SCSI_ASCQ_CAUSE_NOT_REPORTABLE 0x00 |
| 203 | #define SCSI_ASCQ_FORMAT_COMMAND_FAILED 0x01 |
| 204 | #define SCSI_ASCQ_LOG_BLOCK_GUARD_CHECK_FAILED 0x01 |
| 205 | #define SCSI_ASCQ_LOG_BLOCK_APPTAG_CHECK_FAILED 0x02 |
| 206 | #define SCSI_ASCQ_LOG_BLOCK_REFTAG_CHECK_FAILED 0x03 |
| 207 | #define SCSI_ASCQ_FORMAT_IN_PROGRESS 0x04 |
| 208 | #define SCSI_ASCQ_POWER_LOSS_EXPECTED 0x08 |
| 209 | #define SCSI_ASCQ_INVALID_LUN_ID 0x09 |
| 210 | |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 211 | /* copied from drivers/usb/gadget/function/storage_common.h */ |
| 212 | static inline u32 get_unaligned_be24(u8 *buf) |
| 213 | { |
| 214 | return 0xffffff & (u32) get_unaligned_be32(buf - 1); |
| 215 | } |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 216 | |
| 217 | /* Struct to gather data that needs to be extracted from a SCSI CDB. |
| 218 | Not conforming to any particular CDB variant, but compatible with all. */ |
| 219 | |
| 220 | struct nvme_trans_io_cdb { |
| 221 | u8 fua; |
| 222 | u8 prot_info; |
| 223 | u64 lba; |
| 224 | u32 xfer_len; |
| 225 | }; |
| 226 | |
| 227 | |
| 228 | /* Internal Helper Functions */ |
| 229 | |
| 230 | |
| 231 | /* Copy data to userspace memory */ |
| 232 | |
| 233 | static int nvme_trans_copy_to_user(struct sg_io_hdr *hdr, void *from, |
| 234 | unsigned long n) |
| 235 | { |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 236 | int i; |
| 237 | void *index = from; |
| 238 | size_t remaining = n; |
| 239 | size_t xfer_len; |
| 240 | |
| 241 | if (hdr->iovec_count > 0) { |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 242 | struct sg_iovec sgl; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 243 | |
| 244 | for (i = 0; i < hdr->iovec_count; i++) { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 245 | if (copy_from_user(&sgl, hdr->dxferp + |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 246 | i * sizeof(struct sg_iovec), |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 247 | sizeof(struct sg_iovec))) |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 248 | return -EFAULT; |
| 249 | xfer_len = min(remaining, sgl.iov_len); |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 250 | if (copy_to_user(sgl.iov_base, index, xfer_len)) |
| 251 | return -EFAULT; |
| 252 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 253 | index += xfer_len; |
| 254 | remaining -= xfer_len; |
| 255 | if (remaining == 0) |
| 256 | break; |
| 257 | } |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 258 | return 0; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 259 | } |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 260 | |
| 261 | if (copy_to_user(hdr->dxferp, from, n)) |
| 262 | return -EFAULT; |
| 263 | return 0; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /* Copy data from userspace memory */ |
| 267 | |
| 268 | static int nvme_trans_copy_from_user(struct sg_io_hdr *hdr, void *to, |
| 269 | unsigned long n) |
| 270 | { |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 271 | int i; |
| 272 | void *index = to; |
| 273 | size_t remaining = n; |
| 274 | size_t xfer_len; |
| 275 | |
| 276 | if (hdr->iovec_count > 0) { |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 277 | struct sg_iovec sgl; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 278 | |
| 279 | for (i = 0; i < hdr->iovec_count; i++) { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 280 | if (copy_from_user(&sgl, hdr->dxferp + |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 281 | i * sizeof(struct sg_iovec), |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 282 | sizeof(struct sg_iovec))) |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 283 | return -EFAULT; |
| 284 | xfer_len = min(remaining, sgl.iov_len); |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 285 | if (copy_from_user(index, sgl.iov_base, xfer_len)) |
| 286 | return -EFAULT; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 287 | index += xfer_len; |
| 288 | remaining -= xfer_len; |
| 289 | if (remaining == 0) |
| 290 | break; |
| 291 | } |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 292 | return 0; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 295 | if (copy_from_user(to, hdr->dxferp, n)) |
| 296 | return -EFAULT; |
| 297 | return 0; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /* Status/Sense Buffer Writeback */ |
| 301 | |
| 302 | static int nvme_trans_completion(struct sg_io_hdr *hdr, u8 status, u8 sense_key, |
| 303 | u8 asc, u8 ascq) |
| 304 | { |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 305 | u8 xfer_len; |
| 306 | u8 resp[DESC_FMT_SENSE_DATA_SIZE]; |
| 307 | |
| 308 | if (scsi_status_is_good(status)) { |
| 309 | hdr->status = SAM_STAT_GOOD; |
| 310 | hdr->masked_status = GOOD; |
| 311 | hdr->host_status = DID_OK; |
| 312 | hdr->driver_status = DRIVER_OK; |
| 313 | hdr->sb_len_wr = 0; |
| 314 | } else { |
| 315 | hdr->status = status; |
| 316 | hdr->masked_status = status >> 1; |
| 317 | hdr->host_status = DID_OK; |
| 318 | hdr->driver_status = DRIVER_OK; |
| 319 | |
| 320 | memset(resp, 0, DESC_FMT_SENSE_DATA_SIZE); |
| 321 | resp[0] = DESC_FORMAT_SENSE_DATA; |
| 322 | resp[1] = sense_key; |
| 323 | resp[2] = asc; |
| 324 | resp[3] = ascq; |
| 325 | |
| 326 | xfer_len = min_t(u8, hdr->mx_sb_len, DESC_FMT_SENSE_DATA_SIZE); |
| 327 | hdr->sb_len_wr = xfer_len; |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 328 | if (copy_to_user(hdr->sbp, resp, xfer_len) > 0) |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 329 | return -EFAULT; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 332 | return 0; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 335 | /* |
| 336 | * Take a status code from a lowlevel routine, and if it was a positive NVMe |
| 337 | * error code update the sense data based on it. In either case the passed |
| 338 | * in value is returned again, unless an -EFAULT from copy_to_user overrides |
| 339 | * it. |
| 340 | */ |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 341 | static int nvme_trans_status_code(struct sg_io_hdr *hdr, int nvme_sc) |
| 342 | { |
| 343 | u8 status, sense_key, asc, ascq; |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 344 | int res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 345 | |
| 346 | /* For non-nvme (Linux) errors, simply return the error code */ |
| 347 | if (nvme_sc < 0) |
| 348 | return nvme_sc; |
| 349 | |
| 350 | /* Mask DNR, More, and reserved fields */ |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 351 | switch (nvme_sc & 0x7FF) { |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 352 | /* Generic Command Status */ |
| 353 | case NVME_SC_SUCCESS: |
| 354 | status = SAM_STAT_GOOD; |
| 355 | sense_key = NO_SENSE; |
| 356 | asc = SCSI_ASC_NO_SENSE; |
| 357 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; |
| 358 | break; |
| 359 | case NVME_SC_INVALID_OPCODE: |
| 360 | status = SAM_STAT_CHECK_CONDITION; |
| 361 | sense_key = ILLEGAL_REQUEST; |
| 362 | asc = SCSI_ASC_ILLEGAL_COMMAND; |
| 363 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; |
| 364 | break; |
| 365 | case NVME_SC_INVALID_FIELD: |
| 366 | status = SAM_STAT_CHECK_CONDITION; |
| 367 | sense_key = ILLEGAL_REQUEST; |
| 368 | asc = SCSI_ASC_INVALID_CDB; |
| 369 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; |
| 370 | break; |
| 371 | case NVME_SC_DATA_XFER_ERROR: |
| 372 | status = SAM_STAT_CHECK_CONDITION; |
| 373 | sense_key = MEDIUM_ERROR; |
| 374 | asc = SCSI_ASC_NO_SENSE; |
| 375 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; |
| 376 | break; |
| 377 | case NVME_SC_POWER_LOSS: |
| 378 | status = SAM_STAT_TASK_ABORTED; |
| 379 | sense_key = ABORTED_COMMAND; |
| 380 | asc = SCSI_ASC_WARNING; |
| 381 | ascq = SCSI_ASCQ_POWER_LOSS_EXPECTED; |
| 382 | break; |
| 383 | case NVME_SC_INTERNAL: |
| 384 | status = SAM_STAT_CHECK_CONDITION; |
| 385 | sense_key = HARDWARE_ERROR; |
| 386 | asc = SCSI_ASC_INTERNAL_TARGET_FAILURE; |
| 387 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; |
| 388 | break; |
| 389 | case NVME_SC_ABORT_REQ: |
| 390 | status = SAM_STAT_TASK_ABORTED; |
| 391 | sense_key = ABORTED_COMMAND; |
| 392 | asc = SCSI_ASC_NO_SENSE; |
| 393 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; |
| 394 | break; |
| 395 | case NVME_SC_ABORT_QUEUE: |
| 396 | status = SAM_STAT_TASK_ABORTED; |
| 397 | sense_key = ABORTED_COMMAND; |
| 398 | asc = SCSI_ASC_NO_SENSE; |
| 399 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; |
| 400 | break; |
| 401 | case NVME_SC_FUSED_FAIL: |
| 402 | status = SAM_STAT_TASK_ABORTED; |
| 403 | sense_key = ABORTED_COMMAND; |
| 404 | asc = SCSI_ASC_NO_SENSE; |
| 405 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; |
| 406 | break; |
| 407 | case NVME_SC_FUSED_MISSING: |
| 408 | status = SAM_STAT_TASK_ABORTED; |
| 409 | sense_key = ABORTED_COMMAND; |
| 410 | asc = SCSI_ASC_NO_SENSE; |
| 411 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; |
| 412 | break; |
| 413 | case NVME_SC_INVALID_NS: |
| 414 | status = SAM_STAT_CHECK_CONDITION; |
| 415 | sense_key = ILLEGAL_REQUEST; |
| 416 | asc = SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID; |
| 417 | ascq = SCSI_ASCQ_INVALID_LUN_ID; |
| 418 | break; |
| 419 | case NVME_SC_LBA_RANGE: |
| 420 | status = SAM_STAT_CHECK_CONDITION; |
| 421 | sense_key = ILLEGAL_REQUEST; |
| 422 | asc = SCSI_ASC_ILLEGAL_BLOCK; |
| 423 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; |
| 424 | break; |
| 425 | case NVME_SC_CAP_EXCEEDED: |
| 426 | status = SAM_STAT_CHECK_CONDITION; |
| 427 | sense_key = MEDIUM_ERROR; |
| 428 | asc = SCSI_ASC_NO_SENSE; |
| 429 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; |
| 430 | break; |
| 431 | case NVME_SC_NS_NOT_READY: |
| 432 | status = SAM_STAT_CHECK_CONDITION; |
| 433 | sense_key = NOT_READY; |
| 434 | asc = SCSI_ASC_LUN_NOT_READY; |
| 435 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; |
| 436 | break; |
| 437 | |
| 438 | /* Command Specific Status */ |
| 439 | case NVME_SC_INVALID_FORMAT: |
| 440 | status = SAM_STAT_CHECK_CONDITION; |
| 441 | sense_key = ILLEGAL_REQUEST; |
| 442 | asc = SCSI_ASC_FORMAT_COMMAND_FAILED; |
| 443 | ascq = SCSI_ASCQ_FORMAT_COMMAND_FAILED; |
| 444 | break; |
| 445 | case NVME_SC_BAD_ATTRIBUTES: |
| 446 | status = SAM_STAT_CHECK_CONDITION; |
| 447 | sense_key = ILLEGAL_REQUEST; |
| 448 | asc = SCSI_ASC_INVALID_CDB; |
| 449 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; |
| 450 | break; |
| 451 | |
| 452 | /* Media Errors */ |
| 453 | case NVME_SC_WRITE_FAULT: |
| 454 | status = SAM_STAT_CHECK_CONDITION; |
| 455 | sense_key = MEDIUM_ERROR; |
| 456 | asc = SCSI_ASC_PERIPHERAL_DEV_WRITE_FAULT; |
| 457 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; |
| 458 | break; |
| 459 | case NVME_SC_READ_ERROR: |
| 460 | status = SAM_STAT_CHECK_CONDITION; |
| 461 | sense_key = MEDIUM_ERROR; |
| 462 | asc = SCSI_ASC_UNRECOVERED_READ_ERROR; |
| 463 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; |
| 464 | break; |
| 465 | case NVME_SC_GUARD_CHECK: |
| 466 | status = SAM_STAT_CHECK_CONDITION; |
| 467 | sense_key = MEDIUM_ERROR; |
| 468 | asc = SCSI_ASC_LOG_BLOCK_GUARD_CHECK_FAILED; |
| 469 | ascq = SCSI_ASCQ_LOG_BLOCK_GUARD_CHECK_FAILED; |
| 470 | break; |
| 471 | case NVME_SC_APPTAG_CHECK: |
| 472 | status = SAM_STAT_CHECK_CONDITION; |
| 473 | sense_key = MEDIUM_ERROR; |
| 474 | asc = SCSI_ASC_LOG_BLOCK_APPTAG_CHECK_FAILED; |
| 475 | ascq = SCSI_ASCQ_LOG_BLOCK_APPTAG_CHECK_FAILED; |
| 476 | break; |
| 477 | case NVME_SC_REFTAG_CHECK: |
| 478 | status = SAM_STAT_CHECK_CONDITION; |
| 479 | sense_key = MEDIUM_ERROR; |
| 480 | asc = SCSI_ASC_LOG_BLOCK_REFTAG_CHECK_FAILED; |
| 481 | ascq = SCSI_ASCQ_LOG_BLOCK_REFTAG_CHECK_FAILED; |
| 482 | break; |
| 483 | case NVME_SC_COMPARE_FAILED: |
| 484 | status = SAM_STAT_CHECK_CONDITION; |
| 485 | sense_key = MISCOMPARE; |
| 486 | asc = SCSI_ASC_MISCOMPARE_DURING_VERIFY; |
| 487 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; |
| 488 | break; |
| 489 | case NVME_SC_ACCESS_DENIED: |
| 490 | status = SAM_STAT_CHECK_CONDITION; |
| 491 | sense_key = ILLEGAL_REQUEST; |
| 492 | asc = SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID; |
| 493 | ascq = SCSI_ASCQ_INVALID_LUN_ID; |
| 494 | break; |
| 495 | |
| 496 | /* Unspecified/Default */ |
| 497 | case NVME_SC_CMDID_CONFLICT: |
| 498 | case NVME_SC_CMD_SEQ_ERROR: |
| 499 | case NVME_SC_CQ_INVALID: |
| 500 | case NVME_SC_QID_INVALID: |
| 501 | case NVME_SC_QUEUE_SIZE: |
| 502 | case NVME_SC_ABORT_LIMIT: |
| 503 | case NVME_SC_ABORT_MISSING: |
| 504 | case NVME_SC_ASYNC_LIMIT: |
| 505 | case NVME_SC_FIRMWARE_SLOT: |
| 506 | case NVME_SC_FIRMWARE_IMAGE: |
| 507 | case NVME_SC_INVALID_VECTOR: |
| 508 | case NVME_SC_INVALID_LOG_PAGE: |
| 509 | default: |
| 510 | status = SAM_STAT_CHECK_CONDITION; |
| 511 | sense_key = ILLEGAL_REQUEST; |
| 512 | asc = SCSI_ASC_NO_SENSE; |
| 513 | ascq = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; |
| 514 | break; |
| 515 | } |
| 516 | |
| 517 | res = nvme_trans_completion(hdr, status, sense_key, asc, ascq); |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 518 | return res ? res : nvme_sc; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | /* INQUIRY Helper Functions */ |
| 522 | |
| 523 | static int nvme_trans_standard_inquiry_page(struct nvme_ns *ns, |
| 524 | struct sg_io_hdr *hdr, u8 *inq_response, |
| 525 | int alloc_len) |
| 526 | { |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 527 | struct nvme_ctrl *ctrl = ns->ctrl; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 528 | struct nvme_id_ns *id_ns; |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 529 | int res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 530 | int nvme_sc; |
| 531 | int xfer_len; |
| 532 | u8 resp_data_format = 0x02; |
| 533 | u8 protect; |
| 534 | u8 cmdque = 0x01 << 1; |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 535 | u8 fw_offset = sizeof(ctrl->firmware_rev); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 536 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 537 | /* nvme ns identify - use DPS value for PROTECT field */ |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 538 | nvme_sc = nvme_identify_ns(ctrl, ns->ns_id, &id_ns); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 539 | res = nvme_trans_status_code(hdr, nvme_sc); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 540 | if (res) |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 541 | return res; |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 542 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 543 | if (id_ns->dps) |
| 544 | protect = 0x01; |
| 545 | else |
| 546 | protect = 0; |
| 547 | kfree(id_ns); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 548 | |
| 549 | memset(inq_response, 0, STANDARD_INQUIRY_LENGTH); |
| 550 | inq_response[2] = VERSION_SPC_4; |
| 551 | inq_response[3] = resp_data_format; /*normaca=0 | hisup=0 */ |
| 552 | inq_response[4] = ADDITIONAL_STD_INQ_LENGTH; |
| 553 | inq_response[5] = protect; /* sccs=0 | acc=0 | tpgs=0 | pc3=0 */ |
| 554 | inq_response[7] = cmdque; /* wbus16=0 | sync=0 | vs=0 */ |
| 555 | strncpy(&inq_response[8], "NVMe ", 8); |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 556 | strncpy(&inq_response[16], ctrl->model, 16); |
Keith Busch | dedf4b1 | 2014-04-29 15:52:27 -0600 | [diff] [blame] | 557 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 558 | while (ctrl->firmware_rev[fw_offset - 1] == ' ' && fw_offset > 4) |
Keith Busch | dedf4b1 | 2014-04-29 15:52:27 -0600 | [diff] [blame] | 559 | fw_offset--; |
| 560 | fw_offset -= 4; |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 561 | strncpy(&inq_response[32], ctrl->firmware_rev + fw_offset, 4); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 562 | |
| 563 | xfer_len = min(alloc_len, STANDARD_INQUIRY_LENGTH); |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 564 | return nvme_trans_copy_to_user(hdr, inq_response, xfer_len); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | static int nvme_trans_supported_vpd_pages(struct nvme_ns *ns, |
| 568 | struct sg_io_hdr *hdr, u8 *inq_response, |
| 569 | int alloc_len) |
| 570 | { |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 571 | int xfer_len; |
| 572 | |
| 573 | memset(inq_response, 0, STANDARD_INQUIRY_LENGTH); |
| 574 | inq_response[1] = INQ_SUPPORTED_VPD_PAGES_PAGE; /* Page Code */ |
| 575 | inq_response[3] = INQ_NUM_SUPPORTED_VPD_PAGES; /* Page Length */ |
| 576 | inq_response[4] = INQ_SUPPORTED_VPD_PAGES_PAGE; |
| 577 | inq_response[5] = INQ_UNIT_SERIAL_NUMBER_PAGE; |
| 578 | inq_response[6] = INQ_DEVICE_IDENTIFICATION_PAGE; |
| 579 | inq_response[7] = INQ_EXTENDED_INQUIRY_DATA_PAGE; |
| 580 | inq_response[8] = INQ_BDEV_CHARACTERISTICS_PAGE; |
Keith Busch | 7f749d9 | 2015-04-07 15:34:18 -0600 | [diff] [blame] | 581 | inq_response[9] = INQ_BDEV_LIMITS_PAGE; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 582 | |
| 583 | xfer_len = min(alloc_len, STANDARD_INQUIRY_LENGTH); |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 584 | return nvme_trans_copy_to_user(hdr, inq_response, xfer_len); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | static int nvme_trans_unit_serial_page(struct nvme_ns *ns, |
| 588 | struct sg_io_hdr *hdr, u8 *inq_response, |
| 589 | int alloc_len) |
| 590 | { |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 591 | int xfer_len; |
| 592 | |
| 593 | memset(inq_response, 0, STANDARD_INQUIRY_LENGTH); |
| 594 | inq_response[1] = INQ_UNIT_SERIAL_NUMBER_PAGE; /* Page Code */ |
| 595 | inq_response[3] = INQ_SERIAL_NUMBER_LENGTH; /* Page Length */ |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 596 | strncpy(&inq_response[4], ns->ctrl->serial, INQ_SERIAL_NUMBER_LENGTH); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 597 | |
| 598 | xfer_len = min(alloc_len, STANDARD_INQUIRY_LENGTH); |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 599 | return nvme_trans_copy_to_user(hdr, inq_response, xfer_len); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Christoph Hellwig | bf7d3eb | 2015-11-26 09:55:48 +0100 | [diff] [blame] | 602 | static int nvme_fill_device_id_eui64(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 603 | u8 *inq_response, int alloc_len, u32 vs) |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 604 | { |
Christoph Hellwig | bf7d3eb | 2015-11-26 09:55:48 +0100 | [diff] [blame] | 605 | struct nvme_id_ns *id_ns; |
| 606 | int nvme_sc, res; |
| 607 | size_t len; |
| 608 | void *eui; |
| 609 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 610 | nvme_sc = nvme_identify_ns(ns->ctrl, ns->ns_id, &id_ns); |
Christoph Hellwig | bf7d3eb | 2015-11-26 09:55:48 +0100 | [diff] [blame] | 611 | res = nvme_trans_status_code(hdr, nvme_sc); |
| 612 | if (res) |
| 613 | return res; |
| 614 | |
| 615 | eui = id_ns->eui64; |
| 616 | len = sizeof(id_ns->eui64); |
| 617 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 618 | if (vs >= NVME_VS(1, 2)) { |
Christoph Hellwig | bf7d3eb | 2015-11-26 09:55:48 +0100 | [diff] [blame] | 619 | if (bitmap_empty(eui, len * 8)) { |
| 620 | eui = id_ns->nguid; |
| 621 | len = sizeof(id_ns->nguid); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | if (bitmap_empty(eui, len * 8)) { |
| 626 | res = -EOPNOTSUPP; |
| 627 | goto out_free_id; |
| 628 | } |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 629 | |
Keith Busch | 4f1982b | 2015-02-19 13:42:14 -0700 | [diff] [blame] | 630 | memset(inq_response, 0, alloc_len); |
Christoph Hellwig | bf7d3eb | 2015-11-26 09:55:48 +0100 | [diff] [blame] | 631 | inq_response[1] = INQ_DEVICE_IDENTIFICATION_PAGE; |
| 632 | inq_response[3] = 4 + len; /* Page Length */ |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 633 | |
Christoph Hellwig | bf7d3eb | 2015-11-26 09:55:48 +0100 | [diff] [blame] | 634 | /* Designation Descriptor start */ |
| 635 | inq_response[4] = 0x01; /* Proto ID=0h | Code set=1h */ |
| 636 | inq_response[5] = 0x02; /* PIV=0b | Asso=00b | Designator Type=2h */ |
| 637 | inq_response[6] = 0x00; /* Rsvd */ |
| 638 | inq_response[7] = len; /* Designator Length */ |
| 639 | memcpy(&inq_response[8], eui, len); |
Keith Busch | 4f1982b | 2015-02-19 13:42:14 -0700 | [diff] [blame] | 640 | |
Christoph Hellwig | bf7d3eb | 2015-11-26 09:55:48 +0100 | [diff] [blame] | 641 | res = nvme_trans_copy_to_user(hdr, inq_response, alloc_len); |
| 642 | out_free_id: |
| 643 | kfree(id_ns); |
| 644 | return res; |
| 645 | } |
Keith Busch | 4f1982b | 2015-02-19 13:42:14 -0700 | [diff] [blame] | 646 | |
Christoph Hellwig | bf7d3eb | 2015-11-26 09:55:48 +0100 | [diff] [blame] | 647 | static int nvme_fill_device_id_scsi_string(struct nvme_ns *ns, |
| 648 | struct sg_io_hdr *hdr, u8 *inq_response, int alloc_len) |
| 649 | { |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 650 | struct nvme_ctrl *ctrl = ns->ctrl; |
Christoph Hellwig | 01fec28 | 2015-11-26 09:59:44 +0100 | [diff] [blame] | 651 | struct nvme_id_ctrl *id_ctrl; |
| 652 | int nvme_sc, res; |
Keith Busch | 4f1982b | 2015-02-19 13:42:14 -0700 | [diff] [blame] | 653 | |
Christoph Hellwig | bf7d3eb | 2015-11-26 09:55:48 +0100 | [diff] [blame] | 654 | if (alloc_len < 72) { |
| 655 | return nvme_trans_completion(hdr, |
| 656 | SAM_STAT_CHECK_CONDITION, |
| 657 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, |
| 658 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
Keith Busch | 4f1982b | 2015-02-19 13:42:14 -0700 | [diff] [blame] | 659 | } |
Christoph Hellwig | bf7d3eb | 2015-11-26 09:55:48 +0100 | [diff] [blame] | 660 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 661 | nvme_sc = nvme_identify_ctrl(ctrl, &id_ctrl); |
Christoph Hellwig | 01fec28 | 2015-11-26 09:59:44 +0100 | [diff] [blame] | 662 | res = nvme_trans_status_code(hdr, nvme_sc); |
| 663 | if (res) |
| 664 | return res; |
| 665 | |
Christoph Hellwig | bf7d3eb | 2015-11-26 09:55:48 +0100 | [diff] [blame] | 666 | memset(inq_response, 0, alloc_len); |
| 667 | inq_response[1] = INQ_DEVICE_IDENTIFICATION_PAGE; |
| 668 | inq_response[3] = 0x48; /* Page Length */ |
| 669 | |
| 670 | /* Designation Descriptor start */ |
| 671 | inq_response[4] = 0x03; /* Proto ID=0h | Code set=3h */ |
| 672 | inq_response[5] = 0x08; /* PIV=0b | Asso=00b | Designator Type=8h */ |
| 673 | inq_response[6] = 0x00; /* Rsvd */ |
| 674 | inq_response[7] = 0x44; /* Designator Length */ |
| 675 | |
Christoph Hellwig | 01fec28 | 2015-11-26 09:59:44 +0100 | [diff] [blame] | 676 | sprintf(&inq_response[8], "%04x", le16_to_cpu(id_ctrl->vid)); |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 677 | memcpy(&inq_response[12], ctrl->model, sizeof(ctrl->model)); |
Christoph Hellwig | bf7d3eb | 2015-11-26 09:55:48 +0100 | [diff] [blame] | 678 | sprintf(&inq_response[52], "%04x", cpu_to_be32(ns->ns_id)); |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 679 | memcpy(&inq_response[56], ctrl->serial, sizeof(ctrl->serial)); |
Christoph Hellwig | bf7d3eb | 2015-11-26 09:55:48 +0100 | [diff] [blame] | 680 | |
Christoph Hellwig | 01fec28 | 2015-11-26 09:59:44 +0100 | [diff] [blame] | 681 | res = nvme_trans_copy_to_user(hdr, inq_response, alloc_len); |
| 682 | kfree(id_ctrl); |
| 683 | return res; |
Christoph Hellwig | bf7d3eb | 2015-11-26 09:55:48 +0100 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | static int nvme_trans_device_id_page(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
| 687 | u8 *resp, int alloc_len) |
| 688 | { |
| 689 | int res; |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 690 | u32 vs; |
Christoph Hellwig | bf7d3eb | 2015-11-26 09:55:48 +0100 | [diff] [blame] | 691 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 692 | res = ns->ctrl->ops->reg_read32(ns->ctrl, NVME_REG_VS, &vs); |
| 693 | if (res) |
| 694 | return res; |
| 695 | |
| 696 | if (vs >= NVME_VS(1, 1)) { |
| 697 | res = nvme_fill_device_id_eui64(ns, hdr, resp, alloc_len, vs); |
Christoph Hellwig | bf7d3eb | 2015-11-26 09:55:48 +0100 | [diff] [blame] | 698 | if (res != -EOPNOTSUPP) |
| 699 | return res; |
| 700 | } |
| 701 | |
| 702 | return nvme_fill_device_id_scsi_string(ns, hdr, resp, alloc_len); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | static int nvme_trans_ext_inq_page(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
| 706 | int alloc_len) |
| 707 | { |
| 708 | u8 *inq_response; |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 709 | int res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 710 | int nvme_sc; |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 711 | struct nvme_ctrl *ctrl = ns->ctrl; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 712 | struct nvme_id_ctrl *id_ctrl; |
| 713 | struct nvme_id_ns *id_ns; |
| 714 | int xfer_len; |
| 715 | u8 microcode = 0x80; |
| 716 | u8 spt; |
| 717 | u8 spt_lut[8] = {0, 0, 2, 1, 4, 6, 5, 7}; |
| 718 | u8 grd_chk, app_chk, ref_chk, protect; |
| 719 | u8 uask_sup = 0x20; |
| 720 | u8 v_sup; |
| 721 | u8 luiclr = 0x01; |
| 722 | |
| 723 | inq_response = kmalloc(EXTENDED_INQUIRY_DATA_PAGE_LENGTH, GFP_KERNEL); |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 724 | if (inq_response == NULL) |
| 725 | return -ENOMEM; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 726 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 727 | nvme_sc = nvme_identify_ns(ctrl, ns->ns_id, &id_ns); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 728 | res = nvme_trans_status_code(hdr, nvme_sc); |
| 729 | if (res) |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 730 | goto out_free_inq; |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 731 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 732 | spt = spt_lut[id_ns->dpc & 0x07] << 3; |
| 733 | if (id_ns->dps) |
| 734 | protect = 0x01; |
| 735 | else |
| 736 | protect = 0; |
| 737 | kfree(id_ns); |
| 738 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 739 | grd_chk = protect << 2; |
| 740 | app_chk = protect << 1; |
| 741 | ref_chk = protect; |
| 742 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 743 | nvme_sc = nvme_identify_ctrl(ctrl, &id_ctrl); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 744 | res = nvme_trans_status_code(hdr, nvme_sc); |
| 745 | if (res) |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 746 | goto out_free_inq; |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 747 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 748 | v_sup = id_ctrl->vwc; |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 749 | kfree(id_ctrl); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 750 | |
| 751 | memset(inq_response, 0, EXTENDED_INQUIRY_DATA_PAGE_LENGTH); |
| 752 | inq_response[1] = INQ_EXTENDED_INQUIRY_DATA_PAGE; /* Page Code */ |
| 753 | inq_response[2] = 0x00; /* Page Length MSB */ |
| 754 | inq_response[3] = 0x3C; /* Page Length LSB */ |
| 755 | inq_response[4] = microcode | spt | grd_chk | app_chk | ref_chk; |
| 756 | inq_response[5] = uask_sup; |
| 757 | inq_response[6] = v_sup; |
| 758 | inq_response[7] = luiclr; |
| 759 | inq_response[8] = 0; |
| 760 | inq_response[9] = 0; |
| 761 | |
| 762 | xfer_len = min(alloc_len, EXTENDED_INQUIRY_DATA_PAGE_LENGTH); |
| 763 | res = nvme_trans_copy_to_user(hdr, inq_response, xfer_len); |
| 764 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 765 | out_free_inq: |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 766 | kfree(inq_response); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 767 | return res; |
| 768 | } |
| 769 | |
Keith Busch | 7f749d9 | 2015-04-07 15:34:18 -0600 | [diff] [blame] | 770 | static int nvme_trans_bdev_limits_page(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
| 771 | u8 *inq_response, int alloc_len) |
| 772 | { |
Keith Busch | 9283b42 | 2015-04-23 10:24:45 -0600 | [diff] [blame] | 773 | __be32 max_sectors = cpu_to_be32( |
| 774 | nvme_block_nr(ns, queue_max_hw_sectors(ns->queue))); |
Keith Busch | 7f749d9 | 2015-04-07 15:34:18 -0600 | [diff] [blame] | 775 | __be32 max_discard = cpu_to_be32(ns->queue->limits.max_discard_sectors); |
| 776 | __be32 discard_desc_count = cpu_to_be32(0x100); |
| 777 | |
| 778 | memset(inq_response, 0, STANDARD_INQUIRY_LENGTH); |
| 779 | inq_response[1] = VPD_BLOCK_LIMITS; |
| 780 | inq_response[3] = 0x3c; /* Page Length */ |
| 781 | memcpy(&inq_response[8], &max_sectors, sizeof(u32)); |
| 782 | memcpy(&inq_response[20], &max_discard, sizeof(u32)); |
| 783 | |
| 784 | if (max_discard) |
| 785 | memcpy(&inq_response[24], &discard_desc_count, sizeof(u32)); |
| 786 | |
| 787 | return nvme_trans_copy_to_user(hdr, inq_response, 0x3c); |
| 788 | } |
| 789 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 790 | static int nvme_trans_bdev_char_page(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
| 791 | int alloc_len) |
| 792 | { |
| 793 | u8 *inq_response; |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 794 | int res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 795 | int xfer_len; |
| 796 | |
Tushar Behera | 03ea83e | 2013-06-10 10:20:55 +0530 | [diff] [blame] | 797 | inq_response = kzalloc(EXTENDED_INQUIRY_DATA_PAGE_LENGTH, GFP_KERNEL); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 798 | if (inq_response == NULL) { |
| 799 | res = -ENOMEM; |
| 800 | goto out_mem; |
| 801 | } |
| 802 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 803 | inq_response[1] = INQ_BDEV_CHARACTERISTICS_PAGE; /* Page Code */ |
| 804 | inq_response[2] = 0x00; /* Page Length MSB */ |
| 805 | inq_response[3] = 0x3C; /* Page Length LSB */ |
| 806 | inq_response[4] = 0x00; /* Medium Rotation Rate MSB */ |
| 807 | inq_response[5] = 0x01; /* Medium Rotation Rate LSB */ |
| 808 | inq_response[6] = 0x00; /* Form Factor */ |
| 809 | |
| 810 | xfer_len = min(alloc_len, EXTENDED_INQUIRY_DATA_PAGE_LENGTH); |
| 811 | res = nvme_trans_copy_to_user(hdr, inq_response, xfer_len); |
| 812 | |
| 813 | kfree(inq_response); |
| 814 | out_mem: |
| 815 | return res; |
| 816 | } |
| 817 | |
| 818 | /* LOG SENSE Helper Functions */ |
| 819 | |
| 820 | static int nvme_trans_log_supp_pages(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
| 821 | int alloc_len) |
| 822 | { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 823 | int res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 824 | int xfer_len; |
| 825 | u8 *log_response; |
| 826 | |
Tushar Behera | 03ea83e | 2013-06-10 10:20:55 +0530 | [diff] [blame] | 827 | log_response = kzalloc(LOG_PAGE_SUPPORTED_LOG_PAGES_LENGTH, GFP_KERNEL); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 828 | if (log_response == NULL) { |
| 829 | res = -ENOMEM; |
| 830 | goto out_mem; |
| 831 | } |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 832 | |
| 833 | log_response[0] = LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE; |
| 834 | /* Subpage=0x00, Page Length MSB=0 */ |
| 835 | log_response[3] = SUPPORTED_LOG_PAGES_PAGE_LENGTH; |
| 836 | log_response[4] = LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE; |
| 837 | log_response[5] = LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE; |
| 838 | log_response[6] = LOG_PAGE_TEMPERATURE_PAGE; |
| 839 | |
| 840 | xfer_len = min(alloc_len, LOG_PAGE_SUPPORTED_LOG_PAGES_LENGTH); |
| 841 | res = nvme_trans_copy_to_user(hdr, log_response, xfer_len); |
| 842 | |
| 843 | kfree(log_response); |
| 844 | out_mem: |
| 845 | return res; |
| 846 | } |
| 847 | |
| 848 | static int nvme_trans_log_info_exceptions(struct nvme_ns *ns, |
| 849 | struct sg_io_hdr *hdr, int alloc_len) |
| 850 | { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 851 | int res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 852 | int xfer_len; |
| 853 | u8 *log_response; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 854 | struct nvme_smart_log *smart_log; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 855 | u8 temp_c; |
| 856 | u16 temp_k; |
| 857 | |
Tushar Behera | 03ea83e | 2013-06-10 10:20:55 +0530 | [diff] [blame] | 858 | log_response = kzalloc(LOG_INFO_EXCP_PAGE_LENGTH, GFP_KERNEL); |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 859 | if (log_response == NULL) |
| 860 | return -ENOMEM; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 861 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 862 | res = nvme_get_log_page(ns->ctrl, &smart_log); |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 863 | if (res < 0) |
| 864 | goto out_free_response; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 865 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 866 | if (res != NVME_SC_SUCCESS) { |
| 867 | temp_c = LOG_TEMP_UNKNOWN; |
| 868 | } else { |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 869 | temp_k = (smart_log->temperature[1] << 8) + |
| 870 | (smart_log->temperature[0]); |
| 871 | temp_c = temp_k - KELVIN_TEMP_FACTOR; |
| 872 | } |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 873 | kfree(smart_log); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 874 | |
| 875 | log_response[0] = LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE; |
| 876 | /* Subpage=0x00, Page Length MSB=0 */ |
| 877 | log_response[3] = REMAINING_INFO_EXCP_PAGE_LENGTH; |
| 878 | /* Informational Exceptions Log Parameter 1 Start */ |
| 879 | /* Parameter Code=0x0000 bytes 4,5 */ |
| 880 | log_response[6] = 0x23; /* DU=0, TSD=1, ETC=0, TMC=0, FMT_AND_LNK=11b */ |
| 881 | log_response[7] = 0x04; /* PARAMETER LENGTH */ |
| 882 | /* Add sense Code and qualifier = 0x00 each */ |
| 883 | /* Use Temperature from NVMe Get Log Page, convert to C from K */ |
| 884 | log_response[10] = temp_c; |
| 885 | |
| 886 | xfer_len = min(alloc_len, LOG_INFO_EXCP_PAGE_LENGTH); |
| 887 | res = nvme_trans_copy_to_user(hdr, log_response, xfer_len); |
| 888 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 889 | out_free_response: |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 890 | kfree(log_response); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 891 | return res; |
| 892 | } |
| 893 | |
| 894 | static int nvme_trans_log_temperature(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
| 895 | int alloc_len) |
| 896 | { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 897 | int res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 898 | int xfer_len; |
| 899 | u8 *log_response; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 900 | struct nvme_smart_log *smart_log; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 901 | u32 feature_resp; |
| 902 | u8 temp_c_cur, temp_c_thresh; |
| 903 | u16 temp_k; |
| 904 | |
Tushar Behera | 03ea83e | 2013-06-10 10:20:55 +0530 | [diff] [blame] | 905 | log_response = kzalloc(LOG_TEMP_PAGE_LENGTH, GFP_KERNEL); |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 906 | if (log_response == NULL) |
| 907 | return -ENOMEM; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 908 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 909 | res = nvme_get_log_page(ns->ctrl, &smart_log); |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 910 | if (res < 0) |
| 911 | goto out_free_response; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 912 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 913 | if (res != NVME_SC_SUCCESS) { |
| 914 | temp_c_cur = LOG_TEMP_UNKNOWN; |
| 915 | } else { |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 916 | temp_k = (smart_log->temperature[1] << 8) + |
| 917 | (smart_log->temperature[0]); |
| 918 | temp_c_cur = temp_k - KELVIN_TEMP_FACTOR; |
| 919 | } |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 920 | kfree(smart_log); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 921 | |
| 922 | /* Get Features for Temp Threshold */ |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 923 | res = nvme_get_features(ns->ctrl, NVME_FEAT_TEMP_THRESH, 0, 0, |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 924 | &feature_resp); |
| 925 | if (res != NVME_SC_SUCCESS) |
| 926 | temp_c_thresh = LOG_TEMP_UNKNOWN; |
| 927 | else |
| 928 | temp_c_thresh = (feature_resp & 0xFFFF) - KELVIN_TEMP_FACTOR; |
| 929 | |
| 930 | log_response[0] = LOG_PAGE_TEMPERATURE_PAGE; |
| 931 | /* Subpage=0x00, Page Length MSB=0 */ |
| 932 | log_response[3] = REMAINING_TEMP_PAGE_LENGTH; |
| 933 | /* Temperature Log Parameter 1 (Temperature) Start */ |
| 934 | /* Parameter Code = 0x0000 */ |
| 935 | log_response[6] = 0x01; /* Format and Linking = 01b */ |
| 936 | log_response[7] = 0x02; /* Parameter Length */ |
| 937 | /* Use Temperature from NVMe Get Log Page, convert to C from K */ |
| 938 | log_response[9] = temp_c_cur; |
| 939 | /* Temperature Log Parameter 2 (Reference Temperature) Start */ |
| 940 | log_response[11] = 0x01; /* Parameter Code = 0x0001 */ |
| 941 | log_response[12] = 0x01; /* Format and Linking = 01b */ |
| 942 | log_response[13] = 0x02; /* Parameter Length */ |
| 943 | /* Use Temperature Thresh from NVMe Get Log Page, convert to C from K */ |
| 944 | log_response[15] = temp_c_thresh; |
| 945 | |
| 946 | xfer_len = min(alloc_len, LOG_TEMP_PAGE_LENGTH); |
| 947 | res = nvme_trans_copy_to_user(hdr, log_response, xfer_len); |
| 948 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 949 | out_free_response: |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 950 | kfree(log_response); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 951 | return res; |
| 952 | } |
| 953 | |
| 954 | /* MODE SENSE Helper Functions */ |
| 955 | |
| 956 | static int nvme_trans_fill_mode_parm_hdr(u8 *resp, int len, u8 cdb10, u8 llbaa, |
| 957 | u16 mode_data_length, u16 blk_desc_len) |
| 958 | { |
| 959 | /* Quick check to make sure I don't stomp on my own memory... */ |
| 960 | if ((cdb10 && len < 8) || (!cdb10 && len < 4)) |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 961 | return -EINVAL; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 962 | |
| 963 | if (cdb10) { |
| 964 | resp[0] = (mode_data_length & 0xFF00) >> 8; |
| 965 | resp[1] = (mode_data_length & 0x00FF); |
Christoph Hellwig | 9085176 | 2015-05-22 11:12:44 +0200 | [diff] [blame] | 966 | resp[3] = 0x10 /* DPOFUA */; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 967 | resp[4] = llbaa; |
| 968 | resp[5] = RESERVED_FIELD; |
| 969 | resp[6] = (blk_desc_len & 0xFF00) >> 8; |
| 970 | resp[7] = (blk_desc_len & 0x00FF); |
| 971 | } else { |
| 972 | resp[0] = (mode_data_length & 0x00FF); |
Christoph Hellwig | 9085176 | 2015-05-22 11:12:44 +0200 | [diff] [blame] | 973 | resp[2] = 0x10 /* DPOFUA */; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 974 | resp[3] = (blk_desc_len & 0x00FF); |
| 975 | } |
| 976 | |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 977 | return 0; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | static int nvme_trans_fill_blk_desc(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
| 981 | u8 *resp, int len, u8 llbaa) |
| 982 | { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 983 | int res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 984 | int nvme_sc; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 985 | struct nvme_id_ns *id_ns; |
| 986 | u8 flbas; |
| 987 | u32 lba_length; |
| 988 | |
| 989 | if (llbaa == 0 && len < MODE_PAGE_BLK_DES_LEN) |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 990 | return -EINVAL; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 991 | else if (llbaa > 0 && len < MODE_PAGE_LLBAA_BLK_DES_LEN) |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 992 | return -EINVAL; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 993 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 994 | nvme_sc = nvme_identify_ns(ns->ctrl, ns->ns_id, &id_ns); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 995 | res = nvme_trans_status_code(hdr, nvme_sc); |
| 996 | if (res) |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 997 | return res; |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 998 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 999 | flbas = (id_ns->flbas) & 0x0F; |
| 1000 | lba_length = (1 << (id_ns->lbaf[flbas].ds)); |
| 1001 | |
| 1002 | if (llbaa == 0) { |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 1003 | __be32 tmp_cap = cpu_to_be32(le64_to_cpu(id_ns->ncap)); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1004 | /* Byte 4 is reserved */ |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 1005 | __be32 tmp_len = cpu_to_be32(lba_length & 0x00FFFFFF); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1006 | |
| 1007 | memcpy(resp, &tmp_cap, sizeof(u32)); |
| 1008 | memcpy(&resp[4], &tmp_len, sizeof(u32)); |
| 1009 | } else { |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 1010 | __be64 tmp_cap = cpu_to_be64(le64_to_cpu(id_ns->ncap)); |
| 1011 | __be32 tmp_len = cpu_to_be32(lba_length); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1012 | |
| 1013 | memcpy(resp, &tmp_cap, sizeof(u64)); |
| 1014 | /* Bytes 8, 9, 10, 11 are reserved */ |
| 1015 | memcpy(&resp[12], &tmp_len, sizeof(u32)); |
| 1016 | } |
| 1017 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 1018 | kfree(id_ns); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1019 | return res; |
| 1020 | } |
| 1021 | |
| 1022 | static int nvme_trans_fill_control_page(struct nvme_ns *ns, |
| 1023 | struct sg_io_hdr *hdr, u8 *resp, |
| 1024 | int len) |
| 1025 | { |
| 1026 | if (len < MODE_PAGE_CONTROL_LEN) |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1027 | return -EINVAL; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1028 | |
| 1029 | resp[0] = MODE_PAGE_CONTROL; |
| 1030 | resp[1] = MODE_PAGE_CONTROL_LEN_FIELD; |
| 1031 | resp[2] = 0x0E; /* TST=000b, TMF_ONLY=0, DPICZ=1, |
| 1032 | * D_SENSE=1, GLTSD=1, RLEC=0 */ |
| 1033 | resp[3] = 0x12; /* Q_ALGO_MODIFIER=1h, NUAR=0, QERR=01b */ |
| 1034 | /* Byte 4: VS=0, RAC=0, UA_INT=0, SWP=0 */ |
| 1035 | resp[5] = 0x40; /* ATO=0, TAS=1, ATMPE=0, RWWP=0, AUTOLOAD=0 */ |
| 1036 | /* resp[6] and [7] are obsolete, thus zero */ |
| 1037 | resp[8] = 0xFF; /* Busy timeout period = 0xffff */ |
| 1038 | resp[9] = 0xFF; |
| 1039 | /* Bytes 10,11: Extended selftest completion time = 0x0000 */ |
| 1040 | |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1041 | return 0; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | static int nvme_trans_fill_caching_page(struct nvme_ns *ns, |
| 1045 | struct sg_io_hdr *hdr, |
| 1046 | u8 *resp, int len) |
| 1047 | { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1048 | int res = 0; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1049 | int nvme_sc; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1050 | u32 feature_resp; |
| 1051 | u8 vwc; |
| 1052 | |
| 1053 | if (len < MODE_PAGE_CACHING_LEN) |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1054 | return -EINVAL; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1055 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1056 | nvme_sc = nvme_get_features(ns->ctrl, NVME_FEAT_VOLATILE_WC, 0, 0, |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1057 | &feature_resp); |
| 1058 | res = nvme_trans_status_code(hdr, nvme_sc); |
| 1059 | if (res) |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1060 | return res; |
| 1061 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1062 | vwc = feature_resp & 0x00000001; |
| 1063 | |
| 1064 | resp[0] = MODE_PAGE_CACHING; |
| 1065 | resp[1] = MODE_PAGE_CACHING_LEN_FIELD; |
| 1066 | resp[2] = vwc << 2; |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1067 | return 0; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | static int nvme_trans_fill_pow_cnd_page(struct nvme_ns *ns, |
| 1071 | struct sg_io_hdr *hdr, u8 *resp, |
| 1072 | int len) |
| 1073 | { |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1074 | if (len < MODE_PAGE_POW_CND_LEN) |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1075 | return -EINVAL; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1076 | |
| 1077 | resp[0] = MODE_PAGE_POWER_CONDITION; |
| 1078 | resp[1] = MODE_PAGE_POW_CND_LEN_FIELD; |
| 1079 | /* All other bytes are zero */ |
| 1080 | |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1081 | return 0; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | static int nvme_trans_fill_inf_exc_page(struct nvme_ns *ns, |
| 1085 | struct sg_io_hdr *hdr, u8 *resp, |
| 1086 | int len) |
| 1087 | { |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1088 | if (len < MODE_PAGE_INF_EXC_LEN) |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1089 | return -EINVAL; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1090 | |
| 1091 | resp[0] = MODE_PAGE_INFO_EXCEP; |
| 1092 | resp[1] = MODE_PAGE_INF_EXC_LEN_FIELD; |
| 1093 | resp[2] = 0x88; |
| 1094 | /* All other bytes are zero */ |
| 1095 | |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1096 | return 0; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | static int nvme_trans_fill_all_pages(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
| 1100 | u8 *resp, int len) |
| 1101 | { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1102 | int res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1103 | u16 mode_pages_offset_1 = 0; |
| 1104 | u16 mode_pages_offset_2, mode_pages_offset_3, mode_pages_offset_4; |
| 1105 | |
| 1106 | mode_pages_offset_2 = mode_pages_offset_1 + MODE_PAGE_CACHING_LEN; |
| 1107 | mode_pages_offset_3 = mode_pages_offset_2 + MODE_PAGE_CONTROL_LEN; |
| 1108 | mode_pages_offset_4 = mode_pages_offset_3 + MODE_PAGE_POW_CND_LEN; |
| 1109 | |
| 1110 | res = nvme_trans_fill_caching_page(ns, hdr, &resp[mode_pages_offset_1], |
| 1111 | MODE_PAGE_CACHING_LEN); |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1112 | if (res) |
| 1113 | return res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1114 | res = nvme_trans_fill_control_page(ns, hdr, &resp[mode_pages_offset_2], |
| 1115 | MODE_PAGE_CONTROL_LEN); |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1116 | if (res) |
| 1117 | return res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1118 | res = nvme_trans_fill_pow_cnd_page(ns, hdr, &resp[mode_pages_offset_3], |
| 1119 | MODE_PAGE_POW_CND_LEN); |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1120 | if (res) |
| 1121 | return res; |
| 1122 | return nvme_trans_fill_inf_exc_page(ns, hdr, &resp[mode_pages_offset_4], |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1123 | MODE_PAGE_INF_EXC_LEN); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | static inline int nvme_trans_get_blk_desc_len(u8 dbd, u8 llbaa) |
| 1127 | { |
| 1128 | if (dbd == MODE_SENSE_BLK_DESC_ENABLED) { |
| 1129 | /* SPC-4: len = 8 x Num_of_descriptors if llbaa = 0, 16x if 1 */ |
| 1130 | return 8 * (llbaa + 1) * MODE_SENSE_BLK_DESC_COUNT; |
| 1131 | } else { |
| 1132 | return 0; |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | static int nvme_trans_mode_page_create(struct nvme_ns *ns, |
| 1137 | struct sg_io_hdr *hdr, u8 *cmd, |
| 1138 | u16 alloc_len, u8 cdb10, |
| 1139 | int (*mode_page_fill_func) |
| 1140 | (struct nvme_ns *, |
| 1141 | struct sg_io_hdr *hdr, u8 *, int), |
| 1142 | u16 mode_pages_tot_len) |
| 1143 | { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1144 | int res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1145 | int xfer_len; |
| 1146 | u8 *response; |
| 1147 | u8 dbd, llbaa; |
| 1148 | u16 resp_size; |
| 1149 | int mph_size; |
| 1150 | u16 mode_pages_offset_1; |
| 1151 | u16 blk_desc_len, blk_desc_offset, mode_data_length; |
| 1152 | |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 1153 | dbd = (cmd[1] & MODE_SENSE_DBD_MASK) >> MODE_SENSE_DBD_SHIFT; |
| 1154 | llbaa = (cmd[1] & MODE_SENSE_LLBAA_MASK) >> MODE_SENSE_LLBAA_SHIFT; |
| 1155 | mph_size = cdb10 ? MODE_SENSE10_MPH_SIZE : MODE_SENSE6_MPH_SIZE; |
| 1156 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1157 | blk_desc_len = nvme_trans_get_blk_desc_len(dbd, llbaa); |
| 1158 | |
| 1159 | resp_size = mph_size + blk_desc_len + mode_pages_tot_len; |
| 1160 | /* Refer spc4r34 Table 440 for calculation of Mode data Length field */ |
| 1161 | mode_data_length = 3 + (3 * cdb10) + blk_desc_len + mode_pages_tot_len; |
| 1162 | |
| 1163 | blk_desc_offset = mph_size; |
| 1164 | mode_pages_offset_1 = blk_desc_offset + blk_desc_len; |
| 1165 | |
Tushar Behera | 03ea83e | 2013-06-10 10:20:55 +0530 | [diff] [blame] | 1166 | response = kzalloc(resp_size, GFP_KERNEL); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1167 | if (response == NULL) { |
| 1168 | res = -ENOMEM; |
| 1169 | goto out_mem; |
| 1170 | } |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1171 | |
| 1172 | res = nvme_trans_fill_mode_parm_hdr(&response[0], mph_size, cdb10, |
| 1173 | llbaa, mode_data_length, blk_desc_len); |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1174 | if (res) |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1175 | goto out_free; |
| 1176 | if (blk_desc_len > 0) { |
| 1177 | res = nvme_trans_fill_blk_desc(ns, hdr, |
| 1178 | &response[blk_desc_offset], |
| 1179 | blk_desc_len, llbaa); |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1180 | if (res) |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1181 | goto out_free; |
| 1182 | } |
| 1183 | res = mode_page_fill_func(ns, hdr, &response[mode_pages_offset_1], |
| 1184 | mode_pages_tot_len); |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1185 | if (res) |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1186 | goto out_free; |
| 1187 | |
| 1188 | xfer_len = min(alloc_len, resp_size); |
| 1189 | res = nvme_trans_copy_to_user(hdr, response, xfer_len); |
| 1190 | |
| 1191 | out_free: |
| 1192 | kfree(response); |
| 1193 | out_mem: |
| 1194 | return res; |
| 1195 | } |
| 1196 | |
| 1197 | /* Read Capacity Helper Functions */ |
| 1198 | |
| 1199 | static void nvme_trans_fill_read_cap(u8 *response, struct nvme_id_ns *id_ns, |
| 1200 | u8 cdb16) |
| 1201 | { |
| 1202 | u8 flbas; |
| 1203 | u32 lba_length; |
| 1204 | u64 rlba; |
| 1205 | u8 prot_en; |
| 1206 | u8 p_type_lut[4] = {0, 0, 1, 2}; |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 1207 | __be64 tmp_rlba; |
| 1208 | __be32 tmp_rlba_32; |
| 1209 | __be32 tmp_len; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1210 | |
| 1211 | flbas = (id_ns->flbas) & 0x0F; |
| 1212 | lba_length = (1 << (id_ns->lbaf[flbas].ds)); |
| 1213 | rlba = le64_to_cpup(&id_ns->nsze) - 1; |
| 1214 | (id_ns->dps) ? (prot_en = 0x01) : (prot_en = 0); |
| 1215 | |
| 1216 | if (!cdb16) { |
| 1217 | if (rlba > 0xFFFFFFFF) |
| 1218 | rlba = 0xFFFFFFFF; |
| 1219 | tmp_rlba_32 = cpu_to_be32(rlba); |
| 1220 | tmp_len = cpu_to_be32(lba_length); |
| 1221 | memcpy(response, &tmp_rlba_32, sizeof(u32)); |
| 1222 | memcpy(&response[4], &tmp_len, sizeof(u32)); |
| 1223 | } else { |
| 1224 | tmp_rlba = cpu_to_be64(rlba); |
| 1225 | tmp_len = cpu_to_be32(lba_length); |
| 1226 | memcpy(response, &tmp_rlba, sizeof(u64)); |
| 1227 | memcpy(&response[8], &tmp_len, sizeof(u32)); |
| 1228 | response[12] = (p_type_lut[id_ns->dps & 0x3] << 1) | prot_en; |
| 1229 | /* P_I_Exponent = 0x0 | LBPPBE = 0x0 */ |
| 1230 | /* LBPME = 0 | LBPRZ = 0 | LALBA = 0x00 */ |
| 1231 | /* Bytes 16-31 - Reserved */ |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | /* Start Stop Unit Helper Functions */ |
| 1236 | |
| 1237 | static int nvme_trans_power_state(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
| 1238 | u8 pc, u8 pcmod, u8 start) |
| 1239 | { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1240 | int res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1241 | int nvme_sc; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1242 | struct nvme_id_ctrl *id_ctrl; |
| 1243 | int lowest_pow_st; /* max npss = lowest power consumption */ |
| 1244 | unsigned ps_desired = 0; |
| 1245 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1246 | nvme_sc = nvme_identify_ctrl(ns->ctrl, &id_ctrl); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1247 | res = nvme_trans_status_code(hdr, nvme_sc); |
| 1248 | if (res) |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 1249 | return res; |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1250 | |
Dan McLeran | b8e0808 | 2014-06-06 08:27:27 -0600 | [diff] [blame] | 1251 | lowest_pow_st = max(POWER_STATE_0, (int)(id_ctrl->npss - 1)); |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 1252 | kfree(id_ctrl); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1253 | |
| 1254 | switch (pc) { |
| 1255 | case NVME_POWER_STATE_START_VALID: |
| 1256 | /* Action unspecified if POWER CONDITION MODIFIER != 0 */ |
| 1257 | if (pcmod == 0 && start == 0x1) |
| 1258 | ps_desired = POWER_STATE_0; |
| 1259 | if (pcmod == 0 && start == 0x0) |
| 1260 | ps_desired = lowest_pow_st; |
| 1261 | break; |
| 1262 | case NVME_POWER_STATE_ACTIVE: |
| 1263 | /* Action unspecified if POWER CONDITION MODIFIER != 0 */ |
| 1264 | if (pcmod == 0) |
| 1265 | ps_desired = POWER_STATE_0; |
| 1266 | break; |
| 1267 | case NVME_POWER_STATE_IDLE: |
| 1268 | /* Action unspecified if POWER CONDITION MODIFIER != [0,1,2] */ |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1269 | if (pcmod == 0x0) |
Dan McLeran | b8e0808 | 2014-06-06 08:27:27 -0600 | [diff] [blame] | 1270 | ps_desired = POWER_STATE_1; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1271 | else if (pcmod == 0x1) |
Dan McLeran | b8e0808 | 2014-06-06 08:27:27 -0600 | [diff] [blame] | 1272 | ps_desired = POWER_STATE_2; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1273 | else if (pcmod == 0x2) |
Dan McLeran | b8e0808 | 2014-06-06 08:27:27 -0600 | [diff] [blame] | 1274 | ps_desired = POWER_STATE_3; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1275 | break; |
| 1276 | case NVME_POWER_STATE_STANDBY: |
| 1277 | /* Action unspecified if POWER CONDITION MODIFIER != [0,1] */ |
| 1278 | if (pcmod == 0x0) |
Dan McLeran | b8e0808 | 2014-06-06 08:27:27 -0600 | [diff] [blame] | 1279 | ps_desired = max(POWER_STATE_0, (lowest_pow_st - 2)); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1280 | else if (pcmod == 0x1) |
Dan McLeran | b8e0808 | 2014-06-06 08:27:27 -0600 | [diff] [blame] | 1281 | ps_desired = max(POWER_STATE_0, (lowest_pow_st - 1)); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1282 | break; |
| 1283 | case NVME_POWER_STATE_LU_CONTROL: |
| 1284 | default: |
| 1285 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
| 1286 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, |
| 1287 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 1288 | break; |
| 1289 | } |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1290 | nvme_sc = nvme_set_features(ns->ctrl, NVME_FEAT_POWER_MGMT, ps_desired, 0, |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1291 | NULL); |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 1292 | return nvme_trans_status_code(hdr, nvme_sc); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
Christoph Hellwig | b90c48d | 2015-05-22 11:12:40 +0200 | [diff] [blame] | 1295 | static int nvme_trans_send_activate_fw_cmd(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
| 1296 | u8 buffer_id) |
| 1297 | { |
| 1298 | struct nvme_command c; |
| 1299 | int nvme_sc; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1300 | |
Christoph Hellwig | b90c48d | 2015-05-22 11:12:40 +0200 | [diff] [blame] | 1301 | memset(&c, 0, sizeof(c)); |
| 1302 | c.common.opcode = nvme_admin_activate_fw; |
| 1303 | c.common.cdw10[0] = cpu_to_le32(buffer_id | NVME_FWACT_REPL_ACTV); |
| 1304 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 1305 | nvme_sc = nvme_submit_sync_cmd(ns->queue, &c, NULL, 0); |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1306 | return nvme_trans_status_code(hdr, nvme_sc); |
Christoph Hellwig | b90c48d | 2015-05-22 11:12:40 +0200 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | static int nvme_trans_send_download_fw_cmd(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1310 | u8 opcode, u32 tot_len, u32 offset, |
| 1311 | u8 buffer_id) |
| 1312 | { |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1313 | int nvme_sc; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1314 | struct nvme_command c; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1315 | |
Christoph Hellwig | b90c48d | 2015-05-22 11:12:40 +0200 | [diff] [blame] | 1316 | if (hdr->iovec_count > 0) { |
| 1317 | /* Assuming SGL is not allowed for this command */ |
| 1318 | return nvme_trans_completion(hdr, |
| 1319 | SAM_STAT_CHECK_CONDITION, |
| 1320 | ILLEGAL_REQUEST, |
| 1321 | SCSI_ASC_INVALID_CDB, |
| 1322 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1323 | } |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1324 | |
| 1325 | memset(&c, 0, sizeof(c)); |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 1326 | c.common.opcode = nvme_admin_download_fw; |
Christoph Hellwig | b90c48d | 2015-05-22 11:12:40 +0200 | [diff] [blame] | 1327 | c.dlfw.numd = cpu_to_le32((tot_len/BYTES_TO_DWORDS) - 1); |
| 1328 | c.dlfw.offset = cpu_to_le32(offset/BYTES_TO_DWORDS); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1329 | |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame^] | 1330 | nvme_sc = nvme_submit_user_cmd(ns->ctrl->admin_q, &c, |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 1331 | hdr->dxferp, tot_len, NULL, 0); |
| 1332 | return nvme_trans_status_code(hdr, nvme_sc); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | /* Mode Select Helper Functions */ |
| 1336 | |
| 1337 | static inline void nvme_trans_modesel_get_bd_len(u8 *parm_list, u8 cdb10, |
| 1338 | u16 *bd_len, u8 *llbaa) |
| 1339 | { |
| 1340 | if (cdb10) { |
| 1341 | /* 10 Byte CDB */ |
| 1342 | *bd_len = (parm_list[MODE_SELECT_10_BD_OFFSET] << 8) + |
| 1343 | parm_list[MODE_SELECT_10_BD_OFFSET + 1]; |
Keith Busch | 9ac1693 | 2015-01-09 16:52:08 -0700 | [diff] [blame] | 1344 | *llbaa = parm_list[MODE_SELECT_10_LLBAA_OFFSET] & |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1345 | MODE_SELECT_10_LLBAA_MASK; |
| 1346 | } else { |
| 1347 | /* 6 Byte CDB */ |
| 1348 | *bd_len = parm_list[MODE_SELECT_6_BD_OFFSET]; |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | static void nvme_trans_modesel_save_bd(struct nvme_ns *ns, u8 *parm_list, |
| 1353 | u16 idx, u16 bd_len, u8 llbaa) |
| 1354 | { |
| 1355 | u16 bd_num; |
| 1356 | |
| 1357 | bd_num = bd_len / ((llbaa == 0) ? |
| 1358 | SHORT_DESC_BLOCK : LONG_DESC_BLOCK); |
| 1359 | /* Store block descriptor info if a FORMAT UNIT comes later */ |
| 1360 | /* TODO Saving 1st BD info; what to do if multiple BD received? */ |
| 1361 | if (llbaa == 0) { |
| 1362 | /* Standard Block Descriptor - spc4r34 7.5.5.1 */ |
| 1363 | ns->mode_select_num_blocks = |
| 1364 | (parm_list[idx + 1] << 16) + |
| 1365 | (parm_list[idx + 2] << 8) + |
| 1366 | (parm_list[idx + 3]); |
| 1367 | |
| 1368 | ns->mode_select_block_len = |
| 1369 | (parm_list[idx + 5] << 16) + |
| 1370 | (parm_list[idx + 6] << 8) + |
| 1371 | (parm_list[idx + 7]); |
| 1372 | } else { |
| 1373 | /* Long LBA Block Descriptor - sbc3r27 6.4.2.3 */ |
| 1374 | ns->mode_select_num_blocks = |
| 1375 | (((u64)parm_list[idx + 0]) << 56) + |
| 1376 | (((u64)parm_list[idx + 1]) << 48) + |
| 1377 | (((u64)parm_list[idx + 2]) << 40) + |
| 1378 | (((u64)parm_list[idx + 3]) << 32) + |
| 1379 | (((u64)parm_list[idx + 4]) << 24) + |
| 1380 | (((u64)parm_list[idx + 5]) << 16) + |
| 1381 | (((u64)parm_list[idx + 6]) << 8) + |
| 1382 | ((u64)parm_list[idx + 7]); |
| 1383 | |
| 1384 | ns->mode_select_block_len = |
| 1385 | (parm_list[idx + 12] << 24) + |
| 1386 | (parm_list[idx + 13] << 16) + |
| 1387 | (parm_list[idx + 14] << 8) + |
| 1388 | (parm_list[idx + 15]); |
| 1389 | } |
| 1390 | } |
| 1391 | |
Vishal Verma | 710a143 | 2013-05-13 14:55:18 -0600 | [diff] [blame] | 1392 | static int nvme_trans_modesel_get_mp(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1393 | u8 *mode_page, u8 page_code) |
| 1394 | { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1395 | int res = 0; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1396 | int nvme_sc; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1397 | unsigned dword11; |
| 1398 | |
| 1399 | switch (page_code) { |
| 1400 | case MODE_PAGE_CACHING: |
| 1401 | dword11 = ((mode_page[2] & CACHING_MODE_PAGE_WCE_MASK) ? 1 : 0); |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1402 | nvme_sc = nvme_set_features(ns->ctrl, NVME_FEAT_VOLATILE_WC, |
| 1403 | dword11, 0, NULL); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1404 | res = nvme_trans_status_code(hdr, nvme_sc); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1405 | break; |
| 1406 | case MODE_PAGE_CONTROL: |
| 1407 | break; |
| 1408 | case MODE_PAGE_POWER_CONDITION: |
| 1409 | /* Verify the OS is not trying to set timers */ |
| 1410 | if ((mode_page[2] & 0x01) != 0 || (mode_page[3] & 0x0F) != 0) { |
| 1411 | res = nvme_trans_completion(hdr, |
| 1412 | SAM_STAT_CHECK_CONDITION, |
| 1413 | ILLEGAL_REQUEST, |
| 1414 | SCSI_ASC_INVALID_PARAMETER, |
| 1415 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1416 | break; |
| 1417 | } |
| 1418 | break; |
| 1419 | default: |
| 1420 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
| 1421 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, |
| 1422 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1423 | break; |
| 1424 | } |
| 1425 | |
| 1426 | return res; |
| 1427 | } |
| 1428 | |
| 1429 | static int nvme_trans_modesel_data(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
| 1430 | u8 *cmd, u16 parm_list_len, u8 pf, |
| 1431 | u8 sp, u8 cdb10) |
| 1432 | { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1433 | int res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1434 | u8 *parm_list; |
| 1435 | u16 bd_len; |
| 1436 | u8 llbaa = 0; |
| 1437 | u16 index, saved_index; |
| 1438 | u8 page_code; |
| 1439 | u16 mp_size; |
| 1440 | |
| 1441 | /* Get parm list from data-in/out buffer */ |
| 1442 | parm_list = kmalloc(parm_list_len, GFP_KERNEL); |
| 1443 | if (parm_list == NULL) { |
| 1444 | res = -ENOMEM; |
| 1445 | goto out; |
| 1446 | } |
| 1447 | |
| 1448 | res = nvme_trans_copy_from_user(hdr, parm_list, parm_list_len); |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1449 | if (res) |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1450 | goto out_mem; |
| 1451 | |
| 1452 | nvme_trans_modesel_get_bd_len(parm_list, cdb10, &bd_len, &llbaa); |
| 1453 | index = (cdb10) ? (MODE_SELECT_10_MPH_SIZE) : (MODE_SELECT_6_MPH_SIZE); |
| 1454 | |
| 1455 | if (bd_len != 0) { |
| 1456 | /* Block Descriptors present, parse */ |
| 1457 | nvme_trans_modesel_save_bd(ns, parm_list, index, bd_len, llbaa); |
| 1458 | index += bd_len; |
| 1459 | } |
| 1460 | saved_index = index; |
| 1461 | |
| 1462 | /* Multiple mode pages may be present; iterate through all */ |
| 1463 | /* In 1st Iteration, don't do NVME Command, only check for CDB errors */ |
| 1464 | do { |
| 1465 | page_code = parm_list[index] & MODE_SELECT_PAGE_CODE_MASK; |
| 1466 | mp_size = parm_list[index + 1] + 2; |
| 1467 | if ((page_code != MODE_PAGE_CACHING) && |
| 1468 | (page_code != MODE_PAGE_CONTROL) && |
| 1469 | (page_code != MODE_PAGE_POWER_CONDITION)) { |
| 1470 | res = nvme_trans_completion(hdr, |
| 1471 | SAM_STAT_CHECK_CONDITION, |
| 1472 | ILLEGAL_REQUEST, |
| 1473 | SCSI_ASC_INVALID_CDB, |
| 1474 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 1475 | goto out_mem; |
| 1476 | } |
| 1477 | index += mp_size; |
| 1478 | } while (index < parm_list_len); |
| 1479 | |
| 1480 | /* In 2nd Iteration, do the NVME Commands */ |
| 1481 | index = saved_index; |
| 1482 | do { |
| 1483 | page_code = parm_list[index] & MODE_SELECT_PAGE_CODE_MASK; |
| 1484 | mp_size = parm_list[index + 1] + 2; |
| 1485 | res = nvme_trans_modesel_get_mp(ns, hdr, &parm_list[index], |
| 1486 | page_code); |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1487 | if (res) |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1488 | break; |
| 1489 | index += mp_size; |
| 1490 | } while (index < parm_list_len); |
| 1491 | |
| 1492 | out_mem: |
| 1493 | kfree(parm_list); |
| 1494 | out: |
| 1495 | return res; |
| 1496 | } |
| 1497 | |
| 1498 | /* Format Unit Helper Functions */ |
| 1499 | |
| 1500 | static int nvme_trans_fmt_set_blk_size_count(struct nvme_ns *ns, |
| 1501 | struct sg_io_hdr *hdr) |
| 1502 | { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1503 | int res = 0; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1504 | int nvme_sc; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1505 | u8 flbas; |
| 1506 | |
| 1507 | /* |
| 1508 | * SCSI Expects a MODE SELECT would have been issued prior to |
| 1509 | * a FORMAT UNIT, and the block size and number would be used |
| 1510 | * from the block descriptor in it. If a MODE SELECT had not |
| 1511 | * been issued, FORMAT shall use the current values for both. |
| 1512 | */ |
| 1513 | |
| 1514 | if (ns->mode_select_num_blocks == 0 || ns->mode_select_block_len == 0) { |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 1515 | struct nvme_id_ns *id_ns; |
| 1516 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1517 | nvme_sc = nvme_identify_ns(ns->ctrl, ns->ns_id, &id_ns); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1518 | res = nvme_trans_status_code(hdr, nvme_sc); |
| 1519 | if (res) |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 1520 | return res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1521 | |
| 1522 | if (ns->mode_select_num_blocks == 0) |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 1523 | ns->mode_select_num_blocks = le64_to_cpu(id_ns->ncap); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1524 | if (ns->mode_select_block_len == 0) { |
| 1525 | flbas = (id_ns->flbas) & 0x0F; |
| 1526 | ns->mode_select_block_len = |
| 1527 | (1 << (id_ns->lbaf[flbas].ds)); |
| 1528 | } |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 1529 | |
| 1530 | kfree(id_ns); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1531 | } |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 1532 | |
| 1533 | return 0; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | static int nvme_trans_fmt_get_parm_header(struct sg_io_hdr *hdr, u8 len, |
| 1537 | u8 format_prot_info, u8 *nvme_pf_code) |
| 1538 | { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1539 | int res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1540 | u8 *parm_list; |
| 1541 | u8 pf_usage, pf_code; |
| 1542 | |
| 1543 | parm_list = kmalloc(len, GFP_KERNEL); |
| 1544 | if (parm_list == NULL) { |
| 1545 | res = -ENOMEM; |
| 1546 | goto out; |
| 1547 | } |
| 1548 | res = nvme_trans_copy_from_user(hdr, parm_list, len); |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1549 | if (res) |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1550 | goto out_mem; |
| 1551 | |
| 1552 | if ((parm_list[FORMAT_UNIT_IMMED_OFFSET] & |
| 1553 | FORMAT_UNIT_IMMED_MASK) != 0) { |
| 1554 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
| 1555 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, |
| 1556 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 1557 | goto out_mem; |
| 1558 | } |
| 1559 | |
| 1560 | if (len == FORMAT_UNIT_LONG_PARM_LIST_LEN && |
| 1561 | (parm_list[FORMAT_UNIT_PROT_INT_OFFSET] & 0x0F) != 0) { |
| 1562 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
| 1563 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, |
| 1564 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 1565 | goto out_mem; |
| 1566 | } |
| 1567 | pf_usage = parm_list[FORMAT_UNIT_PROT_FIELD_USAGE_OFFSET] & |
| 1568 | FORMAT_UNIT_PROT_FIELD_USAGE_MASK; |
| 1569 | pf_code = (pf_usage << 2) | format_prot_info; |
| 1570 | switch (pf_code) { |
| 1571 | case 0: |
| 1572 | *nvme_pf_code = 0; |
| 1573 | break; |
| 1574 | case 2: |
| 1575 | *nvme_pf_code = 1; |
| 1576 | break; |
| 1577 | case 3: |
| 1578 | *nvme_pf_code = 2; |
| 1579 | break; |
| 1580 | case 7: |
| 1581 | *nvme_pf_code = 3; |
| 1582 | break; |
| 1583 | default: |
| 1584 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
| 1585 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, |
| 1586 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 1587 | break; |
| 1588 | } |
| 1589 | |
| 1590 | out_mem: |
| 1591 | kfree(parm_list); |
| 1592 | out: |
| 1593 | return res; |
| 1594 | } |
| 1595 | |
| 1596 | static int nvme_trans_fmt_send_cmd(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
| 1597 | u8 prot_info) |
| 1598 | { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1599 | int res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1600 | int nvme_sc; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1601 | struct nvme_id_ns *id_ns; |
| 1602 | u8 i; |
| 1603 | u8 flbas, nlbaf; |
| 1604 | u8 selected_lbaf = 0xFF; |
| 1605 | u32 cdw10 = 0; |
| 1606 | struct nvme_command c; |
| 1607 | |
| 1608 | /* Loop thru LBAF's in id_ns to match reqd lbaf, put in cdw10 */ |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1609 | nvme_sc = nvme_identify_ns(ns->ctrl, ns->ns_id, &id_ns); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1610 | res = nvme_trans_status_code(hdr, nvme_sc); |
| 1611 | if (res) |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 1612 | return res; |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1613 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1614 | flbas = (id_ns->flbas) & 0x0F; |
| 1615 | nlbaf = id_ns->nlbaf; |
| 1616 | |
| 1617 | for (i = 0; i < nlbaf; i++) { |
| 1618 | if (ns->mode_select_block_len == (1 << (id_ns->lbaf[i].ds))) { |
| 1619 | selected_lbaf = i; |
| 1620 | break; |
| 1621 | } |
| 1622 | } |
| 1623 | if (selected_lbaf > 0x0F) { |
| 1624 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
| 1625 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_PARAMETER, |
| 1626 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 1627 | } |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 1628 | if (ns->mode_select_num_blocks != le64_to_cpu(id_ns->ncap)) { |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1629 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
| 1630 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_PARAMETER, |
| 1631 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 1632 | } |
| 1633 | |
| 1634 | cdw10 |= prot_info << 5; |
| 1635 | cdw10 |= selected_lbaf & 0x0F; |
| 1636 | memset(&c, 0, sizeof(c)); |
| 1637 | c.format.opcode = nvme_admin_format_nvm; |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 1638 | c.format.nsid = cpu_to_le32(ns->ns_id); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1639 | c.format.cdw10 = cpu_to_le32(cdw10); |
| 1640 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1641 | nvme_sc = nvme_submit_sync_cmd(ns->ctrl->admin_q, &c, NULL, 0); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1642 | res = nvme_trans_status_code(hdr, nvme_sc); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1643 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 1644 | kfree(id_ns); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1645 | return res; |
| 1646 | } |
| 1647 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1648 | static inline u32 nvme_trans_io_get_num_cmds(struct sg_io_hdr *hdr, |
| 1649 | struct nvme_trans_io_cdb *cdb_info, |
| 1650 | u32 max_blocks) |
| 1651 | { |
| 1652 | /* If using iovecs, send one nvme command per vector */ |
| 1653 | if (hdr->iovec_count > 0) |
| 1654 | return hdr->iovec_count; |
| 1655 | else if (cdb_info->xfer_len > max_blocks) |
| 1656 | return ((cdb_info->xfer_len - 1) / max_blocks) + 1; |
| 1657 | else |
| 1658 | return 1; |
| 1659 | } |
| 1660 | |
| 1661 | static u16 nvme_trans_io_get_control(struct nvme_ns *ns, |
| 1662 | struct nvme_trans_io_cdb *cdb_info) |
| 1663 | { |
| 1664 | u16 control = 0; |
| 1665 | |
| 1666 | /* When Protection information support is added, implement here */ |
| 1667 | |
| 1668 | if (cdb_info->fua > 0) |
| 1669 | control |= NVME_RW_FUA; |
| 1670 | |
| 1671 | return control; |
| 1672 | } |
| 1673 | |
| 1674 | static int nvme_trans_do_nvme_io(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
| 1675 | struct nvme_trans_io_cdb *cdb_info, u8 is_write) |
| 1676 | { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1677 | int nvme_sc = NVME_SC_SUCCESS; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1678 | u32 num_cmds; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1679 | u64 unit_len; |
| 1680 | u64 unit_num_blocks; /* Number of blocks to xfer in each nvme cmd */ |
| 1681 | u32 retcode; |
| 1682 | u32 i = 0; |
| 1683 | u64 nvme_offset = 0; |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 1684 | void __user *next_mapping_addr; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1685 | struct nvme_command c; |
| 1686 | u8 opcode = (is_write ? nvme_cmd_write : nvme_cmd_read); |
| 1687 | u16 control; |
Keith Busch | ddcb776 | 2014-03-24 10:03:56 -0400 | [diff] [blame] | 1688 | u32 max_blocks = queue_max_hw_sectors(ns->queue); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1689 | |
| 1690 | num_cmds = nvme_trans_io_get_num_cmds(hdr, cdb_info, max_blocks); |
| 1691 | |
| 1692 | /* |
| 1693 | * This loop handles two cases. |
| 1694 | * First, when an SGL is used in the form of an iovec list: |
| 1695 | * - Use iov_base as the next mapping address for the nvme command_id |
| 1696 | * - Use iov_len as the data transfer length for the command. |
| 1697 | * Second, when we have a single buffer |
| 1698 | * - If larger than max_blocks, split into chunks, offset |
| 1699 | * each nvme command accordingly. |
| 1700 | */ |
| 1701 | for (i = 0; i < num_cmds; i++) { |
| 1702 | memset(&c, 0, sizeof(c)); |
| 1703 | if (hdr->iovec_count > 0) { |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 1704 | struct sg_iovec sgl; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1705 | |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 1706 | retcode = copy_from_user(&sgl, hdr->dxferp + |
| 1707 | i * sizeof(struct sg_iovec), |
| 1708 | sizeof(struct sg_iovec)); |
| 1709 | if (retcode) |
| 1710 | return -EFAULT; |
| 1711 | unit_len = sgl.iov_len; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1712 | unit_num_blocks = unit_len >> ns->lba_shift; |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 1713 | next_mapping_addr = sgl.iov_base; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1714 | } else { |
| 1715 | unit_num_blocks = min((u64)max_blocks, |
| 1716 | (cdb_info->xfer_len - nvme_offset)); |
| 1717 | unit_len = unit_num_blocks << ns->lba_shift; |
| 1718 | next_mapping_addr = hdr->dxferp + |
| 1719 | ((1 << ns->lba_shift) * nvme_offset); |
| 1720 | } |
| 1721 | |
| 1722 | c.rw.opcode = opcode; |
| 1723 | c.rw.nsid = cpu_to_le32(ns->ns_id); |
| 1724 | c.rw.slba = cpu_to_le64(cdb_info->lba + nvme_offset); |
| 1725 | c.rw.length = cpu_to_le16(unit_num_blocks - 1); |
| 1726 | control = nvme_trans_io_get_control(ns, cdb_info); |
| 1727 | c.rw.control = cpu_to_le16(control); |
| 1728 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 1729 | if (get_capacity(ns->disk) - unit_num_blocks < |
| 1730 | cdb_info->lba + nvme_offset) { |
| 1731 | nvme_sc = NVME_SC_LBA_RANGE; |
| 1732 | break; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1733 | } |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame^] | 1734 | nvme_sc = nvme_submit_user_cmd(ns->queue, &c, |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 1735 | next_mapping_addr, unit_len, NULL, 0); |
| 1736 | if (nvme_sc) |
| 1737 | break; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1738 | |
| 1739 | nvme_offset += unit_num_blocks; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1740 | } |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1741 | |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1742 | return nvme_trans_status_code(hdr, nvme_sc); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | |
| 1746 | /* SCSI Command Translation Functions */ |
| 1747 | |
| 1748 | static int nvme_trans_io(struct nvme_ns *ns, struct sg_io_hdr *hdr, u8 is_write, |
| 1749 | u8 *cmd) |
| 1750 | { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1751 | int res = 0; |
Christoph Hellwig | cbbb7a2 | 2015-05-22 11:12:43 +0200 | [diff] [blame] | 1752 | struct nvme_trans_io_cdb cdb_info = { 0, }; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1753 | u8 opcode = cmd[0]; |
| 1754 | u64 xfer_bytes; |
| 1755 | u64 sum_iov_len = 0; |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 1756 | struct sg_iovec sgl; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1757 | int i; |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 1758 | size_t not_copied; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1759 | |
Christoph Hellwig | cbbb7a2 | 2015-05-22 11:12:43 +0200 | [diff] [blame] | 1760 | /* |
| 1761 | * The FUA and WPROTECT fields are not supported in 6-byte CDBs, |
| 1762 | * but always in the same place for all others. |
| 1763 | */ |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1764 | switch (opcode) { |
| 1765 | case WRITE_6: |
| 1766 | case READ_6: |
Christoph Hellwig | cbbb7a2 | 2015-05-22 11:12:43 +0200 | [diff] [blame] | 1767 | break; |
| 1768 | default: |
| 1769 | cdb_info.fua = cmd[1] & 0x8; |
| 1770 | cdb_info.prot_info = (cmd[1] & 0xe0) >> 5; |
Christoph Hellwig | 772ce43 | 2015-05-22 11:12:45 +0200 | [diff] [blame] | 1771 | if (cdb_info.prot_info && !ns->pi_type) { |
| 1772 | return nvme_trans_completion(hdr, |
| 1773 | SAM_STAT_CHECK_CONDITION, |
| 1774 | ILLEGAL_REQUEST, |
| 1775 | SCSI_ASC_INVALID_CDB, |
| 1776 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 1777 | } |
Christoph Hellwig | cbbb7a2 | 2015-05-22 11:12:43 +0200 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | switch (opcode) { |
| 1781 | case WRITE_6: |
| 1782 | case READ_6: |
| 1783 | cdb_info.lba = get_unaligned_be24(&cmd[1]); |
| 1784 | cdb_info.xfer_len = cmd[4]; |
| 1785 | if (cdb_info.xfer_len == 0) |
| 1786 | cdb_info.xfer_len = 256; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1787 | break; |
| 1788 | case WRITE_10: |
| 1789 | case READ_10: |
Christoph Hellwig | cbbb7a2 | 2015-05-22 11:12:43 +0200 | [diff] [blame] | 1790 | cdb_info.lba = get_unaligned_be32(&cmd[2]); |
| 1791 | cdb_info.xfer_len = get_unaligned_be16(&cmd[7]); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1792 | break; |
| 1793 | case WRITE_12: |
| 1794 | case READ_12: |
Christoph Hellwig | cbbb7a2 | 2015-05-22 11:12:43 +0200 | [diff] [blame] | 1795 | cdb_info.lba = get_unaligned_be32(&cmd[2]); |
| 1796 | cdb_info.xfer_len = get_unaligned_be32(&cmd[6]); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1797 | break; |
| 1798 | case WRITE_16: |
| 1799 | case READ_16: |
Christoph Hellwig | cbbb7a2 | 2015-05-22 11:12:43 +0200 | [diff] [blame] | 1800 | cdb_info.lba = get_unaligned_be64(&cmd[2]); |
| 1801 | cdb_info.xfer_len = get_unaligned_be32(&cmd[10]); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1802 | break; |
| 1803 | default: |
| 1804 | /* Will never really reach here */ |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1805 | res = -EIO; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1806 | goto out; |
| 1807 | } |
| 1808 | |
| 1809 | /* Calculate total length of transfer (in bytes) */ |
| 1810 | if (hdr->iovec_count > 0) { |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1811 | for (i = 0; i < hdr->iovec_count; i++) { |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 1812 | not_copied = copy_from_user(&sgl, hdr->dxferp + |
| 1813 | i * sizeof(struct sg_iovec), |
| 1814 | sizeof(struct sg_iovec)); |
| 1815 | if (not_copied) |
| 1816 | return -EFAULT; |
| 1817 | sum_iov_len += sgl.iov_len; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1818 | /* IO vector sizes should be multiples of block size */ |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 1819 | if (sgl.iov_len % (1 << ns->lba_shift) != 0) { |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1820 | res = nvme_trans_completion(hdr, |
| 1821 | SAM_STAT_CHECK_CONDITION, |
| 1822 | ILLEGAL_REQUEST, |
| 1823 | SCSI_ASC_INVALID_PARAMETER, |
| 1824 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 1825 | goto out; |
| 1826 | } |
| 1827 | } |
| 1828 | } else { |
| 1829 | sum_iov_len = hdr->dxfer_len; |
| 1830 | } |
| 1831 | |
| 1832 | /* As Per sg ioctl howto, if the lengths differ, use the lower one */ |
| 1833 | xfer_bytes = min(((u64)hdr->dxfer_len), sum_iov_len); |
| 1834 | |
| 1835 | /* If block count and actual data buffer size dont match, error out */ |
| 1836 | if (xfer_bytes != (cdb_info.xfer_len << ns->lba_shift)) { |
| 1837 | res = -EINVAL; |
| 1838 | goto out; |
| 1839 | } |
| 1840 | |
| 1841 | /* Check for 0 length transfer - it is not illegal */ |
| 1842 | if (cdb_info.xfer_len == 0) |
| 1843 | goto out; |
| 1844 | |
| 1845 | /* Send NVMe IO Command(s) */ |
| 1846 | res = nvme_trans_do_nvme_io(ns, hdr, &cdb_info, is_write); |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1847 | if (res) |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1848 | goto out; |
| 1849 | |
| 1850 | out: |
| 1851 | return res; |
| 1852 | } |
| 1853 | |
| 1854 | static int nvme_trans_inquiry(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
| 1855 | u8 *cmd) |
| 1856 | { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1857 | int res = 0; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1858 | u8 evpd; |
| 1859 | u8 page_code; |
| 1860 | int alloc_len; |
| 1861 | u8 *inq_response; |
| 1862 | |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 1863 | evpd = cmd[1] & 0x01; |
| 1864 | page_code = cmd[2]; |
| 1865 | alloc_len = get_unaligned_be16(&cmd[3]); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1866 | |
Christoph Hellwig | 3fd61b2 | 2015-05-08 18:00:26 +0200 | [diff] [blame] | 1867 | inq_response = kmalloc(max(alloc_len, STANDARD_INQUIRY_LENGTH), |
| 1868 | GFP_KERNEL); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1869 | if (inq_response == NULL) { |
| 1870 | res = -ENOMEM; |
| 1871 | goto out_mem; |
| 1872 | } |
| 1873 | |
| 1874 | if (evpd == 0) { |
| 1875 | if (page_code == INQ_STANDARD_INQUIRY_PAGE) { |
| 1876 | res = nvme_trans_standard_inquiry_page(ns, hdr, |
| 1877 | inq_response, alloc_len); |
| 1878 | } else { |
| 1879 | res = nvme_trans_completion(hdr, |
| 1880 | SAM_STAT_CHECK_CONDITION, |
| 1881 | ILLEGAL_REQUEST, |
| 1882 | SCSI_ASC_INVALID_CDB, |
| 1883 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 1884 | } |
| 1885 | } else { |
| 1886 | switch (page_code) { |
| 1887 | case VPD_SUPPORTED_PAGES: |
| 1888 | res = nvme_trans_supported_vpd_pages(ns, hdr, |
| 1889 | inq_response, alloc_len); |
| 1890 | break; |
| 1891 | case VPD_SERIAL_NUMBER: |
| 1892 | res = nvme_trans_unit_serial_page(ns, hdr, inq_response, |
| 1893 | alloc_len); |
| 1894 | break; |
| 1895 | case VPD_DEVICE_IDENTIFIERS: |
| 1896 | res = nvme_trans_device_id_page(ns, hdr, inq_response, |
| 1897 | alloc_len); |
| 1898 | break; |
| 1899 | case VPD_EXTENDED_INQUIRY: |
| 1900 | res = nvme_trans_ext_inq_page(ns, hdr, alloc_len); |
| 1901 | break; |
Keith Busch | 7f749d9 | 2015-04-07 15:34:18 -0600 | [diff] [blame] | 1902 | case VPD_BLOCK_LIMITS: |
| 1903 | res = nvme_trans_bdev_limits_page(ns, hdr, inq_response, |
| 1904 | alloc_len); |
| 1905 | break; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1906 | case VPD_BLOCK_DEV_CHARACTERISTICS: |
| 1907 | res = nvme_trans_bdev_char_page(ns, hdr, alloc_len); |
| 1908 | break; |
| 1909 | default: |
| 1910 | res = nvme_trans_completion(hdr, |
| 1911 | SAM_STAT_CHECK_CONDITION, |
| 1912 | ILLEGAL_REQUEST, |
| 1913 | SCSI_ASC_INVALID_CDB, |
| 1914 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 1915 | break; |
| 1916 | } |
| 1917 | } |
| 1918 | kfree(inq_response); |
| 1919 | out_mem: |
| 1920 | return res; |
| 1921 | } |
| 1922 | |
| 1923 | static int nvme_trans_log_sense(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
| 1924 | u8 *cmd) |
| 1925 | { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1926 | int res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1927 | u16 alloc_len; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1928 | u8 pc; |
| 1929 | u8 page_code; |
| 1930 | |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 1931 | if (cmd[1] != LOG_SENSE_CDB_SP_NOT_ENABLED) { |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1932 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
| 1933 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, |
| 1934 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 1935 | goto out; |
| 1936 | } |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 1937 | |
| 1938 | page_code = cmd[2] & LOG_SENSE_CDB_PAGE_CODE_MASK; |
| 1939 | pc = (cmd[2] & LOG_SENSE_CDB_PC_MASK) >> LOG_SENSE_CDB_PC_SHIFT; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1940 | if (pc != LOG_SENSE_CDB_PC_CUMULATIVE_VALUES) { |
| 1941 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
| 1942 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, |
| 1943 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 1944 | goto out; |
| 1945 | } |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 1946 | alloc_len = get_unaligned_be16(&cmd[7]); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1947 | switch (page_code) { |
| 1948 | case LOG_PAGE_SUPPORTED_LOG_PAGES_PAGE: |
| 1949 | res = nvme_trans_log_supp_pages(ns, hdr, alloc_len); |
| 1950 | break; |
| 1951 | case LOG_PAGE_INFORMATIONAL_EXCEPTIONS_PAGE: |
| 1952 | res = nvme_trans_log_info_exceptions(ns, hdr, alloc_len); |
| 1953 | break; |
| 1954 | case LOG_PAGE_TEMPERATURE_PAGE: |
| 1955 | res = nvme_trans_log_temperature(ns, hdr, alloc_len); |
| 1956 | break; |
| 1957 | default: |
| 1958 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
| 1959 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, |
| 1960 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 1961 | break; |
| 1962 | } |
| 1963 | |
| 1964 | out: |
| 1965 | return res; |
| 1966 | } |
| 1967 | |
| 1968 | static int nvme_trans_mode_select(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
| 1969 | u8 *cmd) |
| 1970 | { |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1971 | u8 cdb10 = 0; |
| 1972 | u16 parm_list_len; |
| 1973 | u8 page_format; |
| 1974 | u8 save_pages; |
| 1975 | |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 1976 | page_format = cmd[1] & MODE_SELECT_CDB_PAGE_FORMAT_MASK; |
| 1977 | save_pages = cmd[1] & MODE_SELECT_CDB_SAVE_PAGES_MASK; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1978 | |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 1979 | if (cmd[0] == MODE_SELECT) { |
| 1980 | parm_list_len = cmd[4]; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1981 | } else { |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 1982 | parm_list_len = cmd[7]; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1983 | cdb10 = 1; |
| 1984 | } |
| 1985 | |
| 1986 | if (parm_list_len != 0) { |
| 1987 | /* |
| 1988 | * According to SPC-4 r24, a paramter list length field of 0 |
| 1989 | * shall not be considered an error |
| 1990 | */ |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1991 | return nvme_trans_modesel_data(ns, hdr, cmd, parm_list_len, |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1992 | page_format, save_pages, cdb10); |
| 1993 | } |
| 1994 | |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 1995 | return 0; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1996 | } |
| 1997 | |
| 1998 | static int nvme_trans_mode_sense(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
| 1999 | u8 *cmd) |
| 2000 | { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 2001 | int res = 0; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2002 | u16 alloc_len; |
| 2003 | u8 cdb10 = 0; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2004 | |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 2005 | if (cmd[0] == MODE_SENSE) { |
| 2006 | alloc_len = cmd[4]; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2007 | } else { |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 2008 | alloc_len = get_unaligned_be16(&cmd[7]); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2009 | cdb10 = 1; |
| 2010 | } |
| 2011 | |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 2012 | if ((cmd[2] & MODE_SENSE_PAGE_CONTROL_MASK) != |
| 2013 | MODE_SENSE_PC_CURRENT_VALUES) { |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2014 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
| 2015 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, |
| 2016 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 2017 | goto out; |
| 2018 | } |
| 2019 | |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 2020 | switch (cmd[2] & MODE_SENSE_PAGE_CODE_MASK) { |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2021 | case MODE_PAGE_CACHING: |
| 2022 | res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len, |
| 2023 | cdb10, |
| 2024 | &nvme_trans_fill_caching_page, |
| 2025 | MODE_PAGE_CACHING_LEN); |
| 2026 | break; |
| 2027 | case MODE_PAGE_CONTROL: |
| 2028 | res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len, |
| 2029 | cdb10, |
| 2030 | &nvme_trans_fill_control_page, |
| 2031 | MODE_PAGE_CONTROL_LEN); |
| 2032 | break; |
| 2033 | case MODE_PAGE_POWER_CONDITION: |
| 2034 | res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len, |
| 2035 | cdb10, |
| 2036 | &nvme_trans_fill_pow_cnd_page, |
| 2037 | MODE_PAGE_POW_CND_LEN); |
| 2038 | break; |
| 2039 | case MODE_PAGE_INFO_EXCEP: |
| 2040 | res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len, |
| 2041 | cdb10, |
| 2042 | &nvme_trans_fill_inf_exc_page, |
| 2043 | MODE_PAGE_INF_EXC_LEN); |
| 2044 | break; |
| 2045 | case MODE_PAGE_RETURN_ALL: |
| 2046 | res = nvme_trans_mode_page_create(ns, hdr, cmd, alloc_len, |
| 2047 | cdb10, |
| 2048 | &nvme_trans_fill_all_pages, |
| 2049 | MODE_PAGE_ALL_LEN); |
| 2050 | break; |
| 2051 | default: |
| 2052 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
| 2053 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, |
| 2054 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 2055 | break; |
| 2056 | } |
| 2057 | |
| 2058 | out: |
| 2059 | return res; |
| 2060 | } |
| 2061 | |
| 2062 | static int nvme_trans_read_capacity(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 2063 | u8 *cmd, u8 cdb16) |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2064 | { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 2065 | int res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2066 | int nvme_sc; |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 2067 | u32 alloc_len; |
| 2068 | u32 resp_size; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2069 | u32 xfer_len; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2070 | struct nvme_id_ns *id_ns; |
| 2071 | u8 *response; |
| 2072 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2073 | if (cdb16) { |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 2074 | alloc_len = get_unaligned_be32(&cmd[10]); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2075 | resp_size = READ_CAP_16_RESP_SIZE; |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 2076 | } else { |
| 2077 | alloc_len = READ_CAP_10_RESP_SIZE; |
| 2078 | resp_size = READ_CAP_10_RESP_SIZE; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2079 | } |
| 2080 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 2081 | nvme_sc = nvme_identify_ns(ns->ctrl, ns->ns_id, &id_ns); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2082 | res = nvme_trans_status_code(hdr, nvme_sc); |
| 2083 | if (res) |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 2084 | return res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2085 | |
Tushar Behera | 03ea83e | 2013-06-10 10:20:55 +0530 | [diff] [blame] | 2086 | response = kzalloc(resp_size, GFP_KERNEL); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2087 | if (response == NULL) { |
| 2088 | res = -ENOMEM; |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 2089 | goto out_free_id; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2090 | } |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2091 | nvme_trans_fill_read_cap(response, id_ns, cdb16); |
| 2092 | |
| 2093 | xfer_len = min(alloc_len, resp_size); |
| 2094 | res = nvme_trans_copy_to_user(hdr, response, xfer_len); |
| 2095 | |
| 2096 | kfree(response); |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 2097 | out_free_id: |
| 2098 | kfree(id_ns); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2099 | return res; |
| 2100 | } |
| 2101 | |
| 2102 | static int nvme_trans_report_luns(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
| 2103 | u8 *cmd) |
| 2104 | { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 2105 | int res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2106 | int nvme_sc; |
| 2107 | u32 alloc_len, xfer_len, resp_size; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2108 | u8 *response; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2109 | struct nvme_id_ctrl *id_ctrl; |
| 2110 | u32 ll_length, lun_id; |
| 2111 | u8 lun_id_offset = REPORT_LUNS_FIRST_LUN_OFFSET; |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 2112 | __be32 tmp_len; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2113 | |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 2114 | switch (cmd[2]) { |
| 2115 | default: |
| 2116 | return nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2117 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, |
| 2118 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 2119 | case ALL_LUNS_RETURNED: |
| 2120 | case ALL_WELL_KNOWN_LUNS_RETURNED: |
| 2121 | case RESTRICTED_LUNS_RETURNED: |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 2122 | nvme_sc = nvme_identify_ctrl(ns->ctrl, &id_ctrl); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2123 | res = nvme_trans_status_code(hdr, nvme_sc); |
| 2124 | if (res) |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 2125 | return res; |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 2126 | |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 2127 | ll_length = le32_to_cpu(id_ctrl->nn) * LUN_ENTRY_SIZE; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2128 | resp_size = ll_length + LUN_DATA_HEADER_SIZE; |
| 2129 | |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 2130 | alloc_len = get_unaligned_be32(&cmd[6]); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2131 | if (alloc_len < resp_size) { |
| 2132 | res = nvme_trans_completion(hdr, |
| 2133 | SAM_STAT_CHECK_CONDITION, |
| 2134 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, |
| 2135 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 2136 | goto out_free_id; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2137 | } |
| 2138 | |
Tushar Behera | 03ea83e | 2013-06-10 10:20:55 +0530 | [diff] [blame] | 2139 | response = kzalloc(resp_size, GFP_KERNEL); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2140 | if (response == NULL) { |
| 2141 | res = -ENOMEM; |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 2142 | goto out_free_id; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2143 | } |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2144 | |
| 2145 | /* The first LUN ID will always be 0 per the SAM spec */ |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 2146 | for (lun_id = 0; lun_id < le32_to_cpu(id_ctrl->nn); lun_id++) { |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2147 | /* |
| 2148 | * Set the LUN Id and then increment to the next LUN |
| 2149 | * location in the parameter data. |
| 2150 | */ |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 2151 | __be64 tmp_id = cpu_to_be64(lun_id); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2152 | memcpy(&response[lun_id_offset], &tmp_id, sizeof(u64)); |
| 2153 | lun_id_offset += LUN_ENTRY_SIZE; |
| 2154 | } |
| 2155 | tmp_len = cpu_to_be32(ll_length); |
| 2156 | memcpy(response, &tmp_len, sizeof(u32)); |
| 2157 | } |
| 2158 | |
| 2159 | xfer_len = min(alloc_len, resp_size); |
| 2160 | res = nvme_trans_copy_to_user(hdr, response, xfer_len); |
| 2161 | |
| 2162 | kfree(response); |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 2163 | out_free_id: |
| 2164 | kfree(id_ctrl); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2165 | return res; |
| 2166 | } |
| 2167 | |
| 2168 | static int nvme_trans_request_sense(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
| 2169 | u8 *cmd) |
| 2170 | { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 2171 | int res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2172 | u8 alloc_len, xfer_len, resp_size; |
| 2173 | u8 desc_format; |
| 2174 | u8 *response; |
| 2175 | |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 2176 | desc_format = cmd[1] & 0x01; |
| 2177 | alloc_len = cmd[4]; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2178 | |
| 2179 | resp_size = ((desc_format) ? (DESC_FMT_SENSE_DATA_SIZE) : |
| 2180 | (FIXED_FMT_SENSE_DATA_SIZE)); |
Tushar Behera | 03ea83e | 2013-06-10 10:20:55 +0530 | [diff] [blame] | 2181 | response = kzalloc(resp_size, GFP_KERNEL); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2182 | if (response == NULL) { |
| 2183 | res = -ENOMEM; |
| 2184 | goto out; |
| 2185 | } |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2186 | |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 2187 | if (desc_format) { |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2188 | /* Descriptor Format Sense Data */ |
| 2189 | response[0] = DESC_FORMAT_SENSE_DATA; |
| 2190 | response[1] = NO_SENSE; |
| 2191 | /* TODO How is LOW POWER CONDITION ON handled? (byte 2) */ |
| 2192 | response[2] = SCSI_ASC_NO_SENSE; |
| 2193 | response[3] = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; |
| 2194 | /* SDAT_OVFL = 0 | Additional Sense Length = 0 */ |
| 2195 | } else { |
| 2196 | /* Fixed Format Sense Data */ |
| 2197 | response[0] = FIXED_SENSE_DATA; |
| 2198 | /* Byte 1 = Obsolete */ |
| 2199 | response[2] = NO_SENSE; /* FM, EOM, ILI, SDAT_OVFL = 0 */ |
| 2200 | /* Bytes 3-6 - Information - set to zero */ |
| 2201 | response[7] = FIXED_SENSE_DATA_ADD_LENGTH; |
| 2202 | /* Bytes 8-11 - Cmd Specific Information - set to zero */ |
| 2203 | response[12] = SCSI_ASC_NO_SENSE; |
| 2204 | response[13] = SCSI_ASCQ_CAUSE_NOT_REPORTABLE; |
| 2205 | /* Byte 14 = Field Replaceable Unit Code = 0 */ |
| 2206 | /* Bytes 15-17 - SKSV=0; Sense Key Specific = 0 */ |
| 2207 | } |
| 2208 | |
| 2209 | xfer_len = min(alloc_len, resp_size); |
| 2210 | res = nvme_trans_copy_to_user(hdr, response, xfer_len); |
| 2211 | |
| 2212 | kfree(response); |
| 2213 | out: |
| 2214 | return res; |
| 2215 | } |
| 2216 | |
| 2217 | static int nvme_trans_security_protocol(struct nvme_ns *ns, |
| 2218 | struct sg_io_hdr *hdr, |
| 2219 | u8 *cmd) |
| 2220 | { |
| 2221 | return nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
| 2222 | ILLEGAL_REQUEST, SCSI_ASC_ILLEGAL_COMMAND, |
| 2223 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 2224 | } |
| 2225 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2226 | static int nvme_trans_synchronize_cache(struct nvme_ns *ns, |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 2227 | struct sg_io_hdr *hdr) |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2228 | { |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2229 | int nvme_sc; |
Keith Busch | 14385de | 2013-04-25 14:39:27 -0600 | [diff] [blame] | 2230 | struct nvme_command c; |
Keith Busch | 14385de | 2013-04-25 14:39:27 -0600 | [diff] [blame] | 2231 | |
| 2232 | memset(&c, 0, sizeof(c)); |
| 2233 | c.common.opcode = nvme_cmd_flush; |
| 2234 | c.common.nsid = cpu_to_le32(ns->ns_id); |
| 2235 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 2236 | nvme_sc = nvme_submit_sync_cmd(ns->queue, &c, NULL, 0); |
| 2237 | return nvme_trans_status_code(hdr, nvme_sc); |
| 2238 | } |
Keith Busch | 14385de | 2013-04-25 14:39:27 -0600 | [diff] [blame] | 2239 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2240 | static int nvme_trans_start_stop(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
| 2241 | u8 *cmd) |
| 2242 | { |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2243 | u8 immed, pcmod, pc, no_flush, start; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2244 | |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 2245 | immed = cmd[1] & 0x01; |
| 2246 | pcmod = cmd[3] & 0x0f; |
| 2247 | pc = (cmd[4] & 0xf0) >> 4; |
| 2248 | no_flush = cmd[4] & 0x04; |
| 2249 | start = cmd[4] & 0x01; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2250 | |
| 2251 | if (immed != 0) { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 2252 | return nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2253 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, |
| 2254 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 2255 | } else { |
| 2256 | if (no_flush == 0) { |
| 2257 | /* Issue NVME FLUSH command prior to START STOP UNIT */ |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 2258 | int res = nvme_trans_synchronize_cache(ns, hdr); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2259 | if (res) |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 2260 | return res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2261 | } |
| 2262 | /* Setup the expected power state transition */ |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 2263 | return nvme_trans_power_state(ns, hdr, pc, pcmod, start); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2264 | } |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2265 | } |
| 2266 | |
| 2267 | static int nvme_trans_format_unit(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
| 2268 | u8 *cmd) |
| 2269 | { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 2270 | int res; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2271 | u8 parm_hdr_len = 0; |
| 2272 | u8 nvme_pf_code = 0; |
| 2273 | u8 format_prot_info, long_list, format_data; |
| 2274 | |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 2275 | format_prot_info = (cmd[1] & 0xc0) >> 6; |
| 2276 | long_list = cmd[1] & 0x20; |
| 2277 | format_data = cmd[1] & 0x10; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2278 | |
| 2279 | if (format_data != 0) { |
| 2280 | if (format_prot_info != 0) { |
| 2281 | if (long_list == 0) |
| 2282 | parm_hdr_len = FORMAT_UNIT_SHORT_PARM_LIST_LEN; |
| 2283 | else |
| 2284 | parm_hdr_len = FORMAT_UNIT_LONG_PARM_LIST_LEN; |
| 2285 | } |
| 2286 | } else if (format_data == 0 && format_prot_info != 0) { |
| 2287 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
| 2288 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, |
| 2289 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 2290 | goto out; |
| 2291 | } |
| 2292 | |
| 2293 | /* Get parm header from data-in/out buffer */ |
| 2294 | /* |
| 2295 | * According to the translation spec, the only fields in the parameter |
| 2296 | * list we are concerned with are in the header. So allocate only that. |
| 2297 | */ |
| 2298 | if (parm_hdr_len > 0) { |
| 2299 | res = nvme_trans_fmt_get_parm_header(hdr, parm_hdr_len, |
| 2300 | format_prot_info, &nvme_pf_code); |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 2301 | if (res) |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2302 | goto out; |
| 2303 | } |
| 2304 | |
| 2305 | /* Attempt to activate any previously downloaded firmware image */ |
Christoph Hellwig | b90c48d | 2015-05-22 11:12:40 +0200 | [diff] [blame] | 2306 | res = nvme_trans_send_activate_fw_cmd(ns, hdr, 0); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2307 | |
| 2308 | /* Determine Block size and count and send format command */ |
| 2309 | res = nvme_trans_fmt_set_blk_size_count(ns, hdr); |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 2310 | if (res) |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2311 | goto out; |
| 2312 | |
| 2313 | res = nvme_trans_fmt_send_cmd(ns, hdr, nvme_pf_code); |
| 2314 | |
| 2315 | out: |
| 2316 | return res; |
| 2317 | } |
| 2318 | |
| 2319 | static int nvme_trans_test_unit_ready(struct nvme_ns *ns, |
| 2320 | struct sg_io_hdr *hdr, |
| 2321 | u8 *cmd) |
| 2322 | { |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 2323 | if (nvme_ctrl_ready(ns->ctrl)) |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 2324 | return nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2325 | NOT_READY, SCSI_ASC_LUN_NOT_READY, |
| 2326 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 2327 | else |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 2328 | return nvme_trans_completion(hdr, SAM_STAT_GOOD, NO_SENSE, 0, 0); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2329 | } |
| 2330 | |
| 2331 | static int nvme_trans_write_buffer(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
| 2332 | u8 *cmd) |
| 2333 | { |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 2334 | int res = 0; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2335 | u32 buffer_offset, parm_list_length; |
| 2336 | u8 buffer_id, mode; |
| 2337 | |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 2338 | parm_list_length = get_unaligned_be24(&cmd[6]); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2339 | if (parm_list_length % BYTES_TO_DWORDS != 0) { |
| 2340 | /* NVMe expects Firmware file to be a whole number of DWORDS */ |
| 2341 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
| 2342 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, |
| 2343 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 2344 | goto out; |
| 2345 | } |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 2346 | buffer_id = cmd[2]; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2347 | if (buffer_id > NVME_MAX_FIRMWARE_SLOT) { |
| 2348 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
| 2349 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, |
| 2350 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 2351 | goto out; |
| 2352 | } |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 2353 | mode = cmd[1] & 0x1f; |
| 2354 | buffer_offset = get_unaligned_be24(&cmd[3]); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2355 | |
| 2356 | switch (mode) { |
| 2357 | case DOWNLOAD_SAVE_ACTIVATE: |
Christoph Hellwig | b90c48d | 2015-05-22 11:12:40 +0200 | [diff] [blame] | 2358 | res = nvme_trans_send_download_fw_cmd(ns, hdr, nvme_admin_download_fw, |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2359 | parm_list_length, buffer_offset, |
| 2360 | buffer_id); |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 2361 | if (res) |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2362 | goto out; |
Christoph Hellwig | b90c48d | 2015-05-22 11:12:40 +0200 | [diff] [blame] | 2363 | res = nvme_trans_send_activate_fw_cmd(ns, hdr, buffer_id); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2364 | break; |
| 2365 | case DOWNLOAD_SAVE_DEFER_ACTIVATE: |
Christoph Hellwig | b90c48d | 2015-05-22 11:12:40 +0200 | [diff] [blame] | 2366 | res = nvme_trans_send_download_fw_cmd(ns, hdr, nvme_admin_download_fw, |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2367 | parm_list_length, buffer_offset, |
| 2368 | buffer_id); |
| 2369 | break; |
| 2370 | case ACTIVATE_DEFERRED_MICROCODE: |
Christoph Hellwig | b90c48d | 2015-05-22 11:12:40 +0200 | [diff] [blame] | 2371 | res = nvme_trans_send_activate_fw_cmd(ns, hdr, buffer_id); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2372 | break; |
| 2373 | default: |
| 2374 | res = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
| 2375 | ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB, |
| 2376 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 2377 | break; |
| 2378 | } |
| 2379 | |
| 2380 | out: |
| 2381 | return res; |
| 2382 | } |
| 2383 | |
Keith Busch | ec50373 | 2013-04-24 15:44:24 -0600 | [diff] [blame] | 2384 | struct scsi_unmap_blk_desc { |
| 2385 | __be64 slba; |
| 2386 | __be32 nlb; |
| 2387 | u32 resv; |
| 2388 | }; |
| 2389 | |
| 2390 | struct scsi_unmap_parm_list { |
| 2391 | __be16 unmap_data_len; |
| 2392 | __be16 unmap_blk_desc_data_len; |
| 2393 | u32 resv; |
| 2394 | struct scsi_unmap_blk_desc desc[0]; |
| 2395 | }; |
| 2396 | |
| 2397 | static int nvme_trans_unmap(struct nvme_ns *ns, struct sg_io_hdr *hdr, |
| 2398 | u8 *cmd) |
| 2399 | { |
Keith Busch | ec50373 | 2013-04-24 15:44:24 -0600 | [diff] [blame] | 2400 | struct scsi_unmap_parm_list *plist; |
| 2401 | struct nvme_dsm_range *range; |
Keith Busch | ec50373 | 2013-04-24 15:44:24 -0600 | [diff] [blame] | 2402 | struct nvme_command c; |
Axel Lin | 51ef72b | 2015-06-20 16:29:14 +0800 | [diff] [blame] | 2403 | int i, nvme_sc, res; |
Keith Busch | ec50373 | 2013-04-24 15:44:24 -0600 | [diff] [blame] | 2404 | u16 ndesc, list_len; |
Keith Busch | ec50373 | 2013-04-24 15:44:24 -0600 | [diff] [blame] | 2405 | |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 2406 | list_len = get_unaligned_be16(&cmd[7]); |
Keith Busch | ec50373 | 2013-04-24 15:44:24 -0600 | [diff] [blame] | 2407 | if (!list_len) |
| 2408 | return -EINVAL; |
| 2409 | |
| 2410 | plist = kmalloc(list_len, GFP_KERNEL); |
| 2411 | if (!plist) |
| 2412 | return -ENOMEM; |
| 2413 | |
| 2414 | res = nvme_trans_copy_from_user(hdr, plist, list_len); |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 2415 | if (res) |
Keith Busch | ec50373 | 2013-04-24 15:44:24 -0600 | [diff] [blame] | 2416 | goto out; |
| 2417 | |
| 2418 | ndesc = be16_to_cpu(plist->unmap_blk_desc_data_len) >> 4; |
| 2419 | if (!ndesc || ndesc > 256) { |
| 2420 | res = -EINVAL; |
| 2421 | goto out; |
| 2422 | } |
| 2423 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 2424 | range = kcalloc(ndesc, sizeof(*range), GFP_KERNEL); |
Axel Lin | 51ef72b | 2015-06-20 16:29:14 +0800 | [diff] [blame] | 2425 | if (!range) { |
| 2426 | res = -ENOMEM; |
Keith Busch | ec50373 | 2013-04-24 15:44:24 -0600 | [diff] [blame] | 2427 | goto out; |
Axel Lin | 51ef72b | 2015-06-20 16:29:14 +0800 | [diff] [blame] | 2428 | } |
Keith Busch | ec50373 | 2013-04-24 15:44:24 -0600 | [diff] [blame] | 2429 | |
| 2430 | for (i = 0; i < ndesc; i++) { |
| 2431 | range[i].nlb = cpu_to_le32(be32_to_cpu(plist->desc[i].nlb)); |
| 2432 | range[i].slba = cpu_to_le64(be64_to_cpu(plist->desc[i].slba)); |
| 2433 | range[i].cattr = 0; |
| 2434 | } |
| 2435 | |
| 2436 | memset(&c, 0, sizeof(c)); |
| 2437 | c.dsm.opcode = nvme_cmd_dsm; |
| 2438 | c.dsm.nsid = cpu_to_le32(ns->ns_id); |
Keith Busch | ec50373 | 2013-04-24 15:44:24 -0600 | [diff] [blame] | 2439 | c.dsm.nr = cpu_to_le32(ndesc - 1); |
| 2440 | c.dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD); |
| 2441 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 2442 | nvme_sc = nvme_submit_sync_cmd(ns->queue, &c, range, |
| 2443 | ndesc * sizeof(*range)); |
Keith Busch | ec50373 | 2013-04-24 15:44:24 -0600 | [diff] [blame] | 2444 | res = nvme_trans_status_code(hdr, nvme_sc); |
| 2445 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 2446 | kfree(range); |
Keith Busch | ec50373 | 2013-04-24 15:44:24 -0600 | [diff] [blame] | 2447 | out: |
| 2448 | kfree(plist); |
| 2449 | return res; |
| 2450 | } |
| 2451 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2452 | static int nvme_scsi_translate(struct nvme_ns *ns, struct sg_io_hdr *hdr) |
| 2453 | { |
| 2454 | u8 cmd[BLK_MAX_CDB]; |
| 2455 | int retcode; |
| 2456 | unsigned int opcode; |
| 2457 | |
| 2458 | if (hdr->cmdp == NULL) |
| 2459 | return -EMSGSIZE; |
| 2460 | if (copy_from_user(cmd, hdr->cmdp, hdr->cmd_len)) |
| 2461 | return -EFAULT; |
| 2462 | |
Keith Busch | 695a4fe | 2014-08-27 13:55:39 -0600 | [diff] [blame] | 2463 | /* |
| 2464 | * Prime the hdr with good status for scsi commands that don't require |
| 2465 | * an nvme command for translation. |
| 2466 | */ |
| 2467 | retcode = nvme_trans_status_code(hdr, NVME_SC_SUCCESS); |
| 2468 | if (retcode) |
| 2469 | return retcode; |
| 2470 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2471 | opcode = cmd[0]; |
| 2472 | |
| 2473 | switch (opcode) { |
| 2474 | case READ_6: |
| 2475 | case READ_10: |
| 2476 | case READ_12: |
| 2477 | case READ_16: |
| 2478 | retcode = nvme_trans_io(ns, hdr, 0, cmd); |
| 2479 | break; |
| 2480 | case WRITE_6: |
| 2481 | case WRITE_10: |
| 2482 | case WRITE_12: |
| 2483 | case WRITE_16: |
| 2484 | retcode = nvme_trans_io(ns, hdr, 1, cmd); |
| 2485 | break; |
| 2486 | case INQUIRY: |
| 2487 | retcode = nvme_trans_inquiry(ns, hdr, cmd); |
| 2488 | break; |
| 2489 | case LOG_SENSE: |
| 2490 | retcode = nvme_trans_log_sense(ns, hdr, cmd); |
| 2491 | break; |
| 2492 | case MODE_SELECT: |
| 2493 | case MODE_SELECT_10: |
| 2494 | retcode = nvme_trans_mode_select(ns, hdr, cmd); |
| 2495 | break; |
| 2496 | case MODE_SENSE: |
| 2497 | case MODE_SENSE_10: |
| 2498 | retcode = nvme_trans_mode_sense(ns, hdr, cmd); |
| 2499 | break; |
| 2500 | case READ_CAPACITY: |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 2501 | retcode = nvme_trans_read_capacity(ns, hdr, cmd, 0); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2502 | break; |
Hannes Reinecke | eb846d9 | 2014-11-17 14:25:19 +0100 | [diff] [blame] | 2503 | case SERVICE_ACTION_IN_16: |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 2504 | switch (cmd[1]) { |
| 2505 | case SAI_READ_CAPACITY_16: |
| 2506 | retcode = nvme_trans_read_capacity(ns, hdr, cmd, 1); |
| 2507 | break; |
| 2508 | default: |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2509 | goto out; |
Christoph Hellwig | 3726897 | 2015-05-22 11:12:42 +0200 | [diff] [blame] | 2510 | } |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2511 | break; |
| 2512 | case REPORT_LUNS: |
| 2513 | retcode = nvme_trans_report_luns(ns, hdr, cmd); |
| 2514 | break; |
| 2515 | case REQUEST_SENSE: |
| 2516 | retcode = nvme_trans_request_sense(ns, hdr, cmd); |
| 2517 | break; |
| 2518 | case SECURITY_PROTOCOL_IN: |
| 2519 | case SECURITY_PROTOCOL_OUT: |
| 2520 | retcode = nvme_trans_security_protocol(ns, hdr, cmd); |
| 2521 | break; |
| 2522 | case START_STOP: |
| 2523 | retcode = nvme_trans_start_stop(ns, hdr, cmd); |
| 2524 | break; |
| 2525 | case SYNCHRONIZE_CACHE: |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 2526 | retcode = nvme_trans_synchronize_cache(ns, hdr); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2527 | break; |
| 2528 | case FORMAT_UNIT: |
| 2529 | retcode = nvme_trans_format_unit(ns, hdr, cmd); |
| 2530 | break; |
| 2531 | case TEST_UNIT_READY: |
| 2532 | retcode = nvme_trans_test_unit_ready(ns, hdr, cmd); |
| 2533 | break; |
| 2534 | case WRITE_BUFFER: |
| 2535 | retcode = nvme_trans_write_buffer(ns, hdr, cmd); |
| 2536 | break; |
Keith Busch | ec50373 | 2013-04-24 15:44:24 -0600 | [diff] [blame] | 2537 | case UNMAP: |
| 2538 | retcode = nvme_trans_unmap(ns, hdr, cmd); |
| 2539 | break; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2540 | default: |
| 2541 | out: |
| 2542 | retcode = nvme_trans_completion(hdr, SAM_STAT_CHECK_CONDITION, |
| 2543 | ILLEGAL_REQUEST, SCSI_ASC_ILLEGAL_COMMAND, |
| 2544 | SCSI_ASCQ_CAUSE_NOT_REPORTABLE); |
| 2545 | break; |
| 2546 | } |
| 2547 | return retcode; |
| 2548 | } |
| 2549 | |
| 2550 | int nvme_sg_io(struct nvme_ns *ns, struct sg_io_hdr __user *u_hdr) |
| 2551 | { |
| 2552 | struct sg_io_hdr hdr; |
| 2553 | int retcode; |
| 2554 | |
| 2555 | if (!capable(CAP_SYS_ADMIN)) |
| 2556 | return -EACCES; |
| 2557 | if (copy_from_user(&hdr, u_hdr, sizeof(hdr))) |
| 2558 | return -EFAULT; |
| 2559 | if (hdr.interface_id != 'S') |
| 2560 | return -EINVAL; |
| 2561 | if (hdr.cmd_len > BLK_MAX_CDB) |
| 2562 | return -EINVAL; |
| 2563 | |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 2564 | /* |
| 2565 | * A positive return code means a NVMe status, which has been |
| 2566 | * translated to sense data. |
| 2567 | */ |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2568 | retcode = nvme_scsi_translate(ns, &hdr); |
| 2569 | if (retcode < 0) |
| 2570 | return retcode; |
Vishal Verma | 8741ee4 | 2013-04-04 17:52:27 -0600 | [diff] [blame] | 2571 | if (copy_to_user(u_hdr, &hdr, sizeof(sg_io_hdr_t)) > 0) |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2572 | return -EFAULT; |
Christoph Hellwig | e61b0a8 | 2015-05-22 11:12:41 +0200 | [diff] [blame] | 2573 | return 0; |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2574 | } |
| 2575 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 2576 | int nvme_sg_get_version_num(int __user *ip) |
| 2577 | { |
| 2578 | return put_user(sg_version_num, ip); |
| 2579 | } |