1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (C) 2012-2013 Intel Corporation 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #ifndef __NVME_H__ 32 #define __NVME_H__ 33 34 #ifdef _KERNEL 35 #include <sys/types.h> 36 #endif 37 38 #include <sys/param.h> 39 #include <sys/endian.h> 40 41 #define NVME_PASSTHROUGH_CMD _IOWR('n', 0, struct nvme_pt_command) 42 #define NVME_RESET_CONTROLLER _IO('n', 1) 43 #define NVME_GET_NSID _IOR('n', 2, struct nvme_get_nsid) 44 #define NVME_GET_MAX_XFER_SIZE _IOR('n', 3, uint64_t) 45 46 #define NVME_IO_TEST _IOWR('n', 100, struct nvme_io_test) 47 #define NVME_BIO_TEST _IOWR('n', 101, struct nvme_io_test) 48 49 /* 50 * Macros to deal with NVME revisions, as defined VS register 51 */ 52 #define NVME_REV(x, y) (((x) << 16) | ((y) << 8)) 53 #define NVME_MAJOR(r) (((r) >> 16) & 0xffff) 54 #define NVME_MINOR(r) (((r) >> 8) & 0xff) 55 56 /* 57 * Use to mark a command to apply to all namespaces, or to retrieve global 58 * log pages. 59 */ 60 #define NVME_GLOBAL_NAMESPACE_TAG ((uint32_t)0xFFFFFFFF) 61 62 /* Cap nvme to 1MB transfers driver explodes with larger sizes */ 63 #define NVME_MAX_XFER_SIZE (MAXPHYS < (1<<20) ? MAXPHYS : (1<<20)) 64 65 /* Register field definitions */ 66 #define NVME_CAP_LO_REG_MQES_SHIFT (0) 67 #define NVME_CAP_LO_REG_MQES_MASK (0xFFFF) 68 #define NVME_CAP_LO_REG_CQR_SHIFT (16) 69 #define NVME_CAP_LO_REG_CQR_MASK (0x1) 70 #define NVME_CAP_LO_REG_AMS_SHIFT (17) 71 #define NVME_CAP_LO_REG_AMS_MASK (0x3) 72 #define NVME_CAP_LO_REG_TO_SHIFT (24) 73 #define NVME_CAP_LO_REG_TO_MASK (0xFF) 74 #define NVME_CAP_LO_MQES(x) \ 75 (((x) >> NVME_CAP_LO_REG_MQES_SHIFT) & NVME_CAP_LO_REG_MQES_MASK) 76 #define NVME_CAP_LO_CQR(x) \ 77 (((x) >> NVME_CAP_LO_REG_CQR_SHIFT) & NVME_CAP_LO_REG_CQR_MASK) 78 #define NVME_CAP_LO_AMS(x) \ 79 (((x) >> NVME_CAP_LO_REG_AMS_SHIFT) & NVME_CAP_LO_REG_AMS_MASK) 80 #define NVME_CAP_LO_TO(x) \ 81 (((x) >> NVME_CAP_LO_REG_TO_SHIFT) & NVME_CAP_LO_REG_TO_MASK) 82 83 #define NVME_CAP_HI_REG_DSTRD_SHIFT (0) 84 #define NVME_CAP_HI_REG_DSTRD_MASK (0xF) 85 #define NVME_CAP_HI_REG_NSSRS_SHIFT (4) 86 #define NVME_CAP_HI_REG_NSSRS_MASK (0x1) 87 #define NVME_CAP_HI_REG_CSS_SHIFT (5) 88 #define NVME_CAP_HI_REG_CSS_MASK (0xff) 89 #define NVME_CAP_HI_REG_CSS_NVM_SHIFT (5) 90 #define NVME_CAP_HI_REG_CSS_NVM_MASK (0x1) 91 #define NVME_CAP_HI_REG_BPS_SHIFT (13) 92 #define NVME_CAP_HI_REG_BPS_MASK (0x1) 93 #define NVME_CAP_HI_REG_MPSMIN_SHIFT (16) 94 #define NVME_CAP_HI_REG_MPSMIN_MASK (0xF) 95 #define NVME_CAP_HI_REG_MPSMAX_SHIFT (20) 96 #define NVME_CAP_HI_REG_MPSMAX_MASK (0xF) 97 #define NVME_CAP_HI_REG_PMRS_SHIFT (24) 98 #define NVME_CAP_HI_REG_PMRS_MASK (0x1) 99 #define NVME_CAP_HI_REG_CMBS_SHIFT (25) 100 #define NVME_CAP_HI_REG_CMBS_MASK (0x1) 101 #define NVME_CAP_HI_DSTRD(x) \ 102 (((x) >> NVME_CAP_HI_REG_DSTRD_SHIFT) & NVME_CAP_HI_REG_DSTRD_MASK) 103 #define NVME_CAP_HI_NSSRS(x) \ 104 (((x) >> NVME_CAP_HI_REG_NSSRS_SHIFT) & NVME_CAP_HI_REG_NSSRS_MASK) 105 #define NVME_CAP_HI_CSS(x) \ 106 (((x) >> NVME_CAP_HI_REG_CSS_SHIFT) & NVME_CAP_HI_REG_CSS_MASK) 107 #define NVME_CAP_HI_CSS_NVM(x) \ 108 (((x) >> NVME_CAP_HI_REG_CSS_NVM_SHIFT) & NVME_CAP_HI_REG_CSS_NVM_MASK) 109 #define NVME_CAP_HI_BPS(x) \ 110 (((x) >> NVME_CAP_HI_REG_BPS_SHIFT) & NVME_CAP_HI_REG_BPS_MASK) 111 #define NVME_CAP_HI_MPSMIN(x) \ 112 (((x) >> NVME_CAP_HI_REG_MPSMIN_SHIFT) & NVME_CAP_HI_REG_MPSMIN_MASK) 113 #define NVME_CAP_HI_MPSMAX(x) \ 114 (((x) >> NVME_CAP_HI_REG_MPSMAX_SHIFT) & NVME_CAP_HI_REG_MPSMAX_MASK) 115 #define NVME_CAP_HI_PMRS(x) \ 116 (((x) >> NVME_CAP_HI_REG_PMRS_SHIFT) & NVME_CAP_HI_REG_PMRS_MASK) 117 #define NVME_CAP_HI_CMBS(x) \ 118 (((x) >> NVME_CAP_HI_REG_CMBS_SHIFT) & NVME_CAP_HI_REG_CMBS_MASK) 119 120 #define NVME_CC_REG_EN_SHIFT (0) 121 #define NVME_CC_REG_EN_MASK (0x1) 122 #define NVME_CC_REG_CSS_SHIFT (4) 123 #define NVME_CC_REG_CSS_MASK (0x7) 124 #define NVME_CC_REG_MPS_SHIFT (7) 125 #define NVME_CC_REG_MPS_MASK (0xF) 126 #define NVME_CC_REG_AMS_SHIFT (11) 127 #define NVME_CC_REG_AMS_MASK (0x7) 128 #define NVME_CC_REG_SHN_SHIFT (14) 129 #define NVME_CC_REG_SHN_MASK (0x3) 130 #define NVME_CC_REG_IOSQES_SHIFT (16) 131 #define NVME_CC_REG_IOSQES_MASK (0xF) 132 #define NVME_CC_REG_IOCQES_SHIFT (20) 133 #define NVME_CC_REG_IOCQES_MASK (0xF) 134 135 #define NVME_CSTS_REG_RDY_SHIFT (0) 136 #define NVME_CSTS_REG_RDY_MASK (0x1) 137 #define NVME_CSTS_REG_CFS_SHIFT (1) 138 #define NVME_CSTS_REG_CFS_MASK (0x1) 139 #define NVME_CSTS_REG_SHST_SHIFT (2) 140 #define NVME_CSTS_REG_SHST_MASK (0x3) 141 #define NVME_CSTS_REG_NVSRO_SHIFT (4) 142 #define NVME_CSTS_REG_NVSRO_MASK (0x1) 143 #define NVME_CSTS_REG_PP_SHIFT (5) 144 #define NVME_CSTS_REG_PP_MASK (0x1) 145 146 #define NVME_CSTS_GET_SHST(csts) (((csts) >> NVME_CSTS_REG_SHST_SHIFT) & NVME_CSTS_REG_SHST_MASK) 147 148 #define NVME_AQA_REG_ASQS_SHIFT (0) 149 #define NVME_AQA_REG_ASQS_MASK (0xFFF) 150 #define NVME_AQA_REG_ACQS_SHIFT (16) 151 #define NVME_AQA_REG_ACQS_MASK (0xFFF) 152 153 /* Command field definitions */ 154 155 #define NVME_CMD_FUSE_SHIFT (8) 156 #define NVME_CMD_FUSE_MASK (0x3) 157 158 #define NVME_STATUS_P_SHIFT (0) 159 #define NVME_STATUS_P_MASK (0x1) 160 #define NVME_STATUS_SC_SHIFT (1) 161 #define NVME_STATUS_SC_MASK (0xFF) 162 #define NVME_STATUS_SCT_SHIFT (9) 163 #define NVME_STATUS_SCT_MASK (0x7) 164 #define NVME_STATUS_CRD_SHIFT (12) 165 #define NVME_STATUS_CRD_MASK (0x3) 166 #define NVME_STATUS_M_SHIFT (14) 167 #define NVME_STATUS_M_MASK (0x1) 168 #define NVME_STATUS_DNR_SHIFT (15) 169 #define NVME_STATUS_DNR_MASK (0x1) 170 171 #define NVME_STATUS_GET_P(st) (((st) >> NVME_STATUS_P_SHIFT) & NVME_STATUS_P_MASK) 172 #define NVME_STATUS_GET_SC(st) (((st) >> NVME_STATUS_SC_SHIFT) & NVME_STATUS_SC_MASK) 173 #define NVME_STATUS_GET_SCT(st) (((st) >> NVME_STATUS_SCT_SHIFT) & NVME_STATUS_SCT_MASK) 174 #define NVME_STATUS_GET_M(st) (((st) >> NVME_STATUS_M_SHIFT) & NVME_STATUS_M_MASK) 175 #define NVME_STATUS_GET_DNR(st) (((st) >> NVME_STATUS_DNR_SHIFT) & NVME_STATUS_DNR_MASK) 176 177 #define NVME_PWR_ST_MPS_SHIFT (0) 178 #define NVME_PWR_ST_MPS_MASK (0x1) 179 #define NVME_PWR_ST_NOPS_SHIFT (1) 180 #define NVME_PWR_ST_NOPS_MASK (0x1) 181 #define NVME_PWR_ST_RRT_SHIFT (0) 182 #define NVME_PWR_ST_RRT_MASK (0x1F) 183 #define NVME_PWR_ST_RRL_SHIFT (0) 184 #define NVME_PWR_ST_RRL_MASK (0x1F) 185 #define NVME_PWR_ST_RWT_SHIFT (0) 186 #define NVME_PWR_ST_RWT_MASK (0x1F) 187 #define NVME_PWR_ST_RWL_SHIFT (0) 188 #define NVME_PWR_ST_RWL_MASK (0x1F) 189 #define NVME_PWR_ST_IPS_SHIFT (6) 190 #define NVME_PWR_ST_IPS_MASK (0x3) 191 #define NVME_PWR_ST_APW_SHIFT (0) 192 #define NVME_PWR_ST_APW_MASK (0x7) 193 #define NVME_PWR_ST_APS_SHIFT (6) 194 #define NVME_PWR_ST_APS_MASK (0x3) 195 196 /** Controller Multi-path I/O and Namespace Sharing Capabilities */ 197 /* More then one port */ 198 #define NVME_CTRLR_DATA_MIC_MPORTS_SHIFT (0) 199 #define NVME_CTRLR_DATA_MIC_MPORTS_MASK (0x1) 200 /* More then one controller */ 201 #define NVME_CTRLR_DATA_MIC_MCTRLRS_SHIFT (1) 202 #define NVME_CTRLR_DATA_MIC_MCTRLRS_MASK (0x1) 203 /* SR-IOV Virtual Function */ 204 #define NVME_CTRLR_DATA_MIC_SRIOVVF_SHIFT (2) 205 #define NVME_CTRLR_DATA_MIC_SRIOVVF_MASK (0x1) 206 /* Asymmetric Namespace Access Reporting */ 207 #define NVME_CTRLR_DATA_MIC_ANAR_SHIFT (3) 208 #define NVME_CTRLR_DATA_MIC_ANAR_MASK (0x1) 209 210 /** OACS - optional admin command support */ 211 /* supports security send/receive commands */ 212 #define NVME_CTRLR_DATA_OACS_SECURITY_SHIFT (0) 213 #define NVME_CTRLR_DATA_OACS_SECURITY_MASK (0x1) 214 /* supports format nvm command */ 215 #define NVME_CTRLR_DATA_OACS_FORMAT_SHIFT (1) 216 #define NVME_CTRLR_DATA_OACS_FORMAT_MASK (0x1) 217 /* supports firmware activate/download commands */ 218 #define NVME_CTRLR_DATA_OACS_FIRMWARE_SHIFT (2) 219 #define NVME_CTRLR_DATA_OACS_FIRMWARE_MASK (0x1) 220 /* supports namespace management commands */ 221 #define NVME_CTRLR_DATA_OACS_NSMGMT_SHIFT (3) 222 #define NVME_CTRLR_DATA_OACS_NSMGMT_MASK (0x1) 223 /* supports Device Self-test command */ 224 #define NVME_CTRLR_DATA_OACS_SELFTEST_SHIFT (4) 225 #define NVME_CTRLR_DATA_OACS_SELFTEST_MASK (0x1) 226 /* supports Directives */ 227 #define NVME_CTRLR_DATA_OACS_DIRECTIVES_SHIFT (5) 228 #define NVME_CTRLR_DATA_OACS_DIRECTIVES_MASK (0x1) 229 /* supports NVMe-MI Send/Receive */ 230 #define NVME_CTRLR_DATA_OACS_NVMEMI_SHIFT (6) 231 #define NVME_CTRLR_DATA_OACS_NVMEMI_MASK (0x1) 232 /* supports Virtualization Management */ 233 #define NVME_CTRLR_DATA_OACS_VM_SHIFT (7) 234 #define NVME_CTRLR_DATA_OACS_VM_MASK (0x1) 235 /* supports Doorbell Buffer Config */ 236 #define NVME_CTRLR_DATA_OACS_DBBUFFER_SHIFT (8) 237 #define NVME_CTRLR_DATA_OACS_DBBUFFER_MASK (0x1) 238 /* supports Get LBA Status */ 239 #define NVME_CTRLR_DATA_OACS_GETLBA_SHIFT (9) 240 #define NVME_CTRLR_DATA_OACS_GETLBA_MASK (0x1) 241 242 /** firmware updates */ 243 /* first slot is read-only */ 244 #define NVME_CTRLR_DATA_FRMW_SLOT1_RO_SHIFT (0) 245 #define NVME_CTRLR_DATA_FRMW_SLOT1_RO_MASK (0x1) 246 /* number of firmware slots */ 247 #define NVME_CTRLR_DATA_FRMW_NUM_SLOTS_SHIFT (1) 248 #define NVME_CTRLR_DATA_FRMW_NUM_SLOTS_MASK (0x7) 249 /* firmware activation without reset */ 250 #define NVME_CTRLR_DATA_FRMW_ACT_WO_RESET_SHIFT (4) 251 #define NVME_CTRLR_DATA_FRMW_ACT_WO_RESET_MASK (0x1) 252 253 /** log page attributes */ 254 /* per namespace smart/health log page */ 255 #define NVME_CTRLR_DATA_LPA_NS_SMART_SHIFT (0) 256 #define NVME_CTRLR_DATA_LPA_NS_SMART_MASK (0x1) 257 258 /** AVSCC - admin vendor specific command configuration */ 259 /* admin vendor specific commands use spec format */ 260 #define NVME_CTRLR_DATA_AVSCC_SPEC_FORMAT_SHIFT (0) 261 #define NVME_CTRLR_DATA_AVSCC_SPEC_FORMAT_MASK (0x1) 262 263 /** Autonomous Power State Transition Attributes */ 264 /* Autonomous Power State Transitions supported */ 265 #define NVME_CTRLR_DATA_APSTA_APST_SUPP_SHIFT (0) 266 #define NVME_CTRLR_DATA_APSTA_APST_SUPP_MASK (0x1) 267 268 /** Sanitize Capabilities */ 269 /* Crypto Erase Support */ 270 #define NVME_CTRLR_DATA_SANICAP_CES_SHIFT (0) 271 #define NVME_CTRLR_DATA_SANICAP_CES_MASK (0x1) 272 /* Block Erase Support */ 273 #define NVME_CTRLR_DATA_SANICAP_BES_SHIFT (1) 274 #define NVME_CTRLR_DATA_SANICAP_BES_MASK (0x1) 275 /* Overwrite Support */ 276 #define NVME_CTRLR_DATA_SANICAP_OWS_SHIFT (2) 277 #define NVME_CTRLR_DATA_SANICAP_OWS_MASK (0x1) 278 /* No-Deallocate Inhibited */ 279 #define NVME_CTRLR_DATA_SANICAP_NDI_SHIFT (29) 280 #define NVME_CTRLR_DATA_SANICAP_NDI_MASK (0x1) 281 /* No-Deallocate Modifies Media After Sanitize */ 282 #define NVME_CTRLR_DATA_SANICAP_NODMMAS_SHIFT (30) 283 #define NVME_CTRLR_DATA_SANICAP_NODMMAS_MASK (0x3) 284 #define NVME_CTRLR_DATA_SANICAP_NODMMAS_UNDEF (0) 285 #define NVME_CTRLR_DATA_SANICAP_NODMMAS_NO (1) 286 #define NVME_CTRLR_DATA_SANICAP_NODMMAS_YES (2) 287 288 /** submission queue entry size */ 289 #define NVME_CTRLR_DATA_SQES_MIN_SHIFT (0) 290 #define NVME_CTRLR_DATA_SQES_MIN_MASK (0xF) 291 #define NVME_CTRLR_DATA_SQES_MAX_SHIFT (4) 292 #define NVME_CTRLR_DATA_SQES_MAX_MASK (0xF) 293 294 /** completion queue entry size */ 295 #define NVME_CTRLR_DATA_CQES_MIN_SHIFT (0) 296 #define NVME_CTRLR_DATA_CQES_MIN_MASK (0xF) 297 #define NVME_CTRLR_DATA_CQES_MAX_SHIFT (4) 298 #define NVME_CTRLR_DATA_CQES_MAX_MASK (0xF) 299 300 /** optional nvm command support */ 301 #define NVME_CTRLR_DATA_ONCS_COMPARE_SHIFT (0) 302 #define NVME_CTRLR_DATA_ONCS_COMPARE_MASK (0x1) 303 #define NVME_CTRLR_DATA_ONCS_WRITE_UNC_SHIFT (1) 304 #define NVME_CTRLR_DATA_ONCS_WRITE_UNC_MASK (0x1) 305 #define NVME_CTRLR_DATA_ONCS_DSM_SHIFT (2) 306 #define NVME_CTRLR_DATA_ONCS_DSM_MASK (0x1) 307 #define NVME_CTRLR_DATA_ONCS_WRZERO_SHIFT (3) 308 #define NVME_CTRLR_DATA_ONCS_WRZERO_MASK (0x1) 309 #define NVME_CTRLR_DATA_ONCS_SAVEFEAT_SHIFT (4) 310 #define NVME_CTRLR_DATA_ONCS_SAVEFEAT_MASK (0x1) 311 #define NVME_CTRLR_DATA_ONCS_RESERV_SHIFT (5) 312 #define NVME_CTRLR_DATA_ONCS_RESERV_MASK (0x1) 313 #define NVME_CTRLR_DATA_ONCS_TIMESTAMP_SHIFT (6) 314 #define NVME_CTRLR_DATA_ONCS_TIMESTAMP_MASK (0x1) 315 #define NVME_CTRLR_DATA_ONCS_VERIFY_SHIFT (7) 316 #define NVME_CTRLR_DATA_ONCS_VERIFY_MASK (0x1) 317 318 /** Fused Operation Support */ 319 #define NVME_CTRLR_DATA_FUSES_CNW_SHIFT (0) 320 #define NVME_CTRLR_DATA_FUSES_CNW_MASK (0x1) 321 322 /** Format NVM Attributes */ 323 #define NVME_CTRLR_DATA_FNA_FORMAT_ALL_SHIFT (0) 324 #define NVME_CTRLR_DATA_FNA_FORMAT_ALL_MASK (0x1) 325 #define NVME_CTRLR_DATA_FNA_ERASE_ALL_SHIFT (1) 326 #define NVME_CTRLR_DATA_FNA_ERASE_ALL_MASK (0x1) 327 #define NVME_CTRLR_DATA_FNA_CRYPTO_ERASE_SHIFT (2) 328 #define NVME_CTRLR_DATA_FNA_CRYPTO_ERASE_MASK (0x1) 329 330 /** volatile write cache */ 331 /* volatile write cache present */ 332 #define NVME_CTRLR_DATA_VWC_PRESENT_SHIFT (0) 333 #define NVME_CTRLR_DATA_VWC_PRESENT_MASK (0x1) 334 /* flush all namespaces supported */ 335 #define NVME_CTRLR_DATA_VWC_ALL_SHIFT (1) 336 #define NVME_CTRLR_DATA_VWC_ALL_MASK (0x3) 337 #define NVME_CTRLR_DATA_VWC_ALL_UNKNOWN (0) 338 #define NVME_CTRLR_DATA_VWC_ALL_NO (2) 339 #define NVME_CTRLR_DATA_VWC_ALL_YES (3) 340 341 /** namespace features */ 342 /* thin provisioning */ 343 #define NVME_NS_DATA_NSFEAT_THIN_PROV_SHIFT (0) 344 #define NVME_NS_DATA_NSFEAT_THIN_PROV_MASK (0x1) 345 /* NAWUN, NAWUPF, and NACWU fields are valid */ 346 #define NVME_NS_DATA_NSFEAT_NA_FIELDS_SHIFT (1) 347 #define NVME_NS_DATA_NSFEAT_NA_FIELDS_MASK (0x1) 348 /* Deallocated or Unwritten Logical Block errors supported */ 349 #define NVME_NS_DATA_NSFEAT_DEALLOC_SHIFT (2) 350 #define NVME_NS_DATA_NSFEAT_DEALLOC_MASK (0x1) 351 /* NGUID and EUI64 fields are not reusable */ 352 #define NVME_NS_DATA_NSFEAT_NO_ID_REUSE_SHIFT (3) 353 #define NVME_NS_DATA_NSFEAT_NO_ID_REUSE_MASK (0x1) 354 /* NPWG, NPWA, NPDG, NPDA, and NOWS are valid */ 355 #define NVME_NS_DATA_NSFEAT_NPVALID_SHIFT (4) 356 #define NVME_NS_DATA_NSFEAT_NPVALID_MASK (0x1) 357 358 /** formatted lba size */ 359 #define NVME_NS_DATA_FLBAS_FORMAT_SHIFT (0) 360 #define NVME_NS_DATA_FLBAS_FORMAT_MASK (0xF) 361 #define NVME_NS_DATA_FLBAS_EXTENDED_SHIFT (4) 362 #define NVME_NS_DATA_FLBAS_EXTENDED_MASK (0x1) 363 364 /** metadata capabilities */ 365 /* metadata can be transferred as part of data prp list */ 366 #define NVME_NS_DATA_MC_EXTENDED_SHIFT (0) 367 #define NVME_NS_DATA_MC_EXTENDED_MASK (0x1) 368 /* metadata can be transferred with separate metadata pointer */ 369 #define NVME_NS_DATA_MC_POINTER_SHIFT (1) 370 #define NVME_NS_DATA_MC_POINTER_MASK (0x1) 371 372 /** end-to-end data protection capabilities */ 373 /* protection information type 1 */ 374 #define NVME_NS_DATA_DPC_PIT1_SHIFT (0) 375 #define NVME_NS_DATA_DPC_PIT1_MASK (0x1) 376 /* protection information type 2 */ 377 #define NVME_NS_DATA_DPC_PIT2_SHIFT (1) 378 #define NVME_NS_DATA_DPC_PIT2_MASK (0x1) 379 /* protection information type 3 */ 380 #define NVME_NS_DATA_DPC_PIT3_SHIFT (2) 381 #define NVME_NS_DATA_DPC_PIT3_MASK (0x1) 382 /* first eight bytes of metadata */ 383 #define NVME_NS_DATA_DPC_MD_START_SHIFT (3) 384 #define NVME_NS_DATA_DPC_MD_START_MASK (0x1) 385 /* last eight bytes of metadata */ 386 #define NVME_NS_DATA_DPC_MD_END_SHIFT (4) 387 #define NVME_NS_DATA_DPC_MD_END_MASK (0x1) 388 389 /** end-to-end data protection type settings */ 390 /* protection information type */ 391 #define NVME_NS_DATA_DPS_PIT_SHIFT (0) 392 #define NVME_NS_DATA_DPS_PIT_MASK (0x7) 393 /* 1 == protection info transferred at start of metadata */ 394 /* 0 == protection info transferred at end of metadata */ 395 #define NVME_NS_DATA_DPS_MD_START_SHIFT (3) 396 #define NVME_NS_DATA_DPS_MD_START_MASK (0x1) 397 398 /** Namespace Multi-path I/O and Namespace Sharing Capabilities */ 399 /* the namespace may be attached to two or more controllers */ 400 #define NVME_NS_DATA_NMIC_MAY_BE_SHARED_SHIFT (0) 401 #define NVME_NS_DATA_NMIC_MAY_BE_SHARED_MASK (0x1) 402 403 /** Reservation Capabilities */ 404 /* Persist Through Power Loss */ 405 #define NVME_NS_DATA_RESCAP_PTPL_SHIFT (0) 406 #define NVME_NS_DATA_RESCAP_PTPL_MASK (0x1) 407 /* supports the Write Exclusive */ 408 #define NVME_NS_DATA_RESCAP_WR_EX_SHIFT (1) 409 #define NVME_NS_DATA_RESCAP_WR_EX_MASK (0x1) 410 /* supports the Exclusive Access */ 411 #define NVME_NS_DATA_RESCAP_EX_AC_SHIFT (2) 412 #define NVME_NS_DATA_RESCAP_EX_AC_MASK (0x1) 413 /* supports the Write Exclusive – Registrants Only */ 414 #define NVME_NS_DATA_RESCAP_WR_EX_RO_SHIFT (3) 415 #define NVME_NS_DATA_RESCAP_WR_EX_RO_MASK (0x1) 416 /* supports the Exclusive Access - Registrants Only */ 417 #define NVME_NS_DATA_RESCAP_EX_AC_RO_SHIFT (4) 418 #define NVME_NS_DATA_RESCAP_EX_AC_RO_MASK (0x1) 419 /* supports the Write Exclusive – All Registrants */ 420 #define NVME_NS_DATA_RESCAP_WR_EX_AR_SHIFT (5) 421 #define NVME_NS_DATA_RESCAP_WR_EX_AR_MASK (0x1) 422 /* supports the Exclusive Access - All Registrants */ 423 #define NVME_NS_DATA_RESCAP_EX_AC_AR_SHIFT (6) 424 #define NVME_NS_DATA_RESCAP_EX_AC_AR_MASK (0x1) 425 /* Ignore Existing Key is used as defined in revision 1.3 or later */ 426 #define NVME_NS_DATA_RESCAP_IEKEY13_SHIFT (7) 427 #define NVME_NS_DATA_RESCAP_IEKEY13_MASK (0x1) 428 429 /** Format Progress Indicator */ 430 /* percentage of the Format NVM command that remains to be completed */ 431 #define NVME_NS_DATA_FPI_PERC_SHIFT (0) 432 #define NVME_NS_DATA_FPI_PERC_MASK (0x7f) 433 /* namespace supports the Format Progress Indicator */ 434 #define NVME_NS_DATA_FPI_SUPP_SHIFT (7) 435 #define NVME_NS_DATA_FPI_SUPP_MASK (0x1) 436 437 /** Deallocate Logical Block Features */ 438 /* deallocated logical block read behavior */ 439 #define NVME_NS_DATA_DLFEAT_READ_SHIFT (0) 440 #define NVME_NS_DATA_DLFEAT_READ_MASK (0x07) 441 #define NVME_NS_DATA_DLFEAT_READ_NR (0x00) 442 #define NVME_NS_DATA_DLFEAT_READ_00 (0x01) 443 #define NVME_NS_DATA_DLFEAT_READ_FF (0x02) 444 /* supports the Deallocate bit in the Write Zeroes */ 445 #define NVME_NS_DATA_DLFEAT_DWZ_SHIFT (3) 446 #define NVME_NS_DATA_DLFEAT_DWZ_MASK (0x01) 447 /* Guard field for deallocated logical blocks is set to the CRC */ 448 #define NVME_NS_DATA_DLFEAT_GCRC_SHIFT (4) 449 #define NVME_NS_DATA_DLFEAT_GCRC_MASK (0x01) 450 451 /** lba format support */ 452 /* metadata size */ 453 #define NVME_NS_DATA_LBAF_MS_SHIFT (0) 454 #define NVME_NS_DATA_LBAF_MS_MASK (0xFFFF) 455 /* lba data size */ 456 #define NVME_NS_DATA_LBAF_LBADS_SHIFT (16) 457 #define NVME_NS_DATA_LBAF_LBADS_MASK (0xFF) 458 /* relative performance */ 459 #define NVME_NS_DATA_LBAF_RP_SHIFT (24) 460 #define NVME_NS_DATA_LBAF_RP_MASK (0x3) 461 462 enum nvme_critical_warning_state { 463 NVME_CRIT_WARN_ST_AVAILABLE_SPARE = 0x1, 464 NVME_CRIT_WARN_ST_TEMPERATURE = 0x2, 465 NVME_CRIT_WARN_ST_DEVICE_RELIABILITY = 0x4, 466 NVME_CRIT_WARN_ST_READ_ONLY = 0x8, 467 NVME_CRIT_WARN_ST_VOLATILE_MEMORY_BACKUP = 0x10, 468 }; 469 #define NVME_CRIT_WARN_ST_RESERVED_MASK (0xE0) 470 #define NVME_ASYNC_EVENT_NS_ATTRIBUTE (0x100) 471 #define NVME_ASYNC_EVENT_FW_ACTIVATE (0x200) 472 473 /* slot for current FW */ 474 #define NVME_FIRMWARE_PAGE_AFI_SLOT_SHIFT (0) 475 #define NVME_FIRMWARE_PAGE_AFI_SLOT_MASK (0x7) 476 477 /* Commands Supported and Effects */ 478 #define NVME_CE_PAGE_CSUP_SHIFT (0) 479 #define NVME_CE_PAGE_CSUP_MASK (0x1) 480 #define NVME_CE_PAGE_LBCC_SHIFT (1) 481 #define NVME_CE_PAGE_LBCC_MASK (0x1) 482 #define NVME_CE_PAGE_NCC_SHIFT (2) 483 #define NVME_CE_PAGE_NCC_MASK (0x1) 484 #define NVME_CE_PAGE_NIC_SHIFT (3) 485 #define NVME_CE_PAGE_NIC_MASK (0x1) 486 #define NVME_CE_PAGE_CCC_SHIFT (4) 487 #define NVME_CE_PAGE_CCC_MASK (0x1) 488 #define NVME_CE_PAGE_CSE_SHIFT (16) 489 #define NVME_CE_PAGE_CSE_MASK (0x7) 490 #define NVME_CE_PAGE_UUID_SHIFT (19) 491 #define NVME_CE_PAGE_UUID_MASK (0x1) 492 493 /* Sanitize Status */ 494 #define NVME_SS_PAGE_SSTAT_STATUS_SHIFT (0) 495 #define NVME_SS_PAGE_SSTAT_STATUS_MASK (0x7) 496 #define NVME_SS_PAGE_SSTAT_STATUS_NEVER (0) 497 #define NVME_SS_PAGE_SSTAT_STATUS_COMPLETED (1) 498 #define NVME_SS_PAGE_SSTAT_STATUS_INPROG (2) 499 #define NVME_SS_PAGE_SSTAT_STATUS_FAILED (3) 500 #define NVME_SS_PAGE_SSTAT_STATUS_COMPLETEDWD (4) 501 #define NVME_SS_PAGE_SSTAT_PASSES_SHIFT (3) 502 #define NVME_SS_PAGE_SSTAT_PASSES_MASK (0x1f) 503 #define NVME_SS_PAGE_SSTAT_GDE_SHIFT (8) 504 #define NVME_SS_PAGE_SSTAT_GDE_MASK (0x1) 505 506 /* CC register SHN field values */ 507 enum shn_value { 508 NVME_SHN_NORMAL = 0x1, 509 NVME_SHN_ABRUPT = 0x2, 510 }; 511 512 /* CSTS register SHST field values */ 513 enum shst_value { 514 NVME_SHST_NORMAL = 0x0, 515 NVME_SHST_OCCURRING = 0x1, 516 NVME_SHST_COMPLETE = 0x2, 517 }; 518 519 struct nvme_registers 520 { 521 uint32_t cap_lo; /* controller capabilities */ 522 uint32_t cap_hi; 523 uint32_t vs; /* version */ 524 uint32_t intms; /* interrupt mask set */ 525 uint32_t intmc; /* interrupt mask clear */ 526 uint32_t cc; /* controller configuration */ 527 uint32_t reserved1; 528 uint32_t csts; /* controller status */ 529 uint32_t nssr; /* NVM Subsystem Reset */ 530 uint32_t aqa; /* admin queue attributes */ 531 uint64_t asq; /* admin submission queue base addr */ 532 uint64_t acq; /* admin completion queue base addr */ 533 uint32_t cmbloc; /* Controller Memory Buffer Location */ 534 uint32_t cmbsz; /* Controller Memory Buffer Size */ 535 uint32_t bpinfo; /* Boot Partition Information */ 536 uint32_t bprsel; /* Boot Partition Read Select */ 537 uint64_t bpmbl; /* Boot Partition Memory Buffer Location */ 538 uint64_t cmbmsc; /* Controller Memory Buffer Memory Space Control */ 539 uint32_t cmbsts; /* Controller Memory Buffer Status */ 540 uint8_t reserved3[3492]; /* 5Ch - DFFh */ 541 uint32_t pmrcap; /* Persistent Memory Capabilities */ 542 uint32_t pmrctl; /* Persistent Memory Region Control */ 543 uint32_t pmrsts; /* Persistent Memory Region Status */ 544 uint32_t pmrebs; /* Persistent Memory Region Elasticity Buffer Size */ 545 uint32_t pmrswtp; /* Persistent Memory Region Sustained Write Throughput */ 546 uint32_t pmrmsc_lo; /* Persistent Memory Region Controller Memory Space Control */ 547 uint32_t pmrmsc_hi; 548 uint8_t reserved4[484]; /* E1Ch - FFFh */ 549 struct { 550 uint32_t sq_tdbl; /* submission queue tail doorbell */ 551 uint32_t cq_hdbl; /* completion queue head doorbell */ 552 } doorbell[1] __packed; 553 } __packed; 554 555 _Static_assert(sizeof(struct nvme_registers) == 0x1008, "bad size for nvme_registers"); 556 557 struct nvme_command 558 { 559 /* dword 0 */ 560 uint8_t opc; /* opcode */ 561 uint8_t fuse; /* fused operation */ 562 uint16_t cid; /* command identifier */ 563 564 /* dword 1 */ 565 uint32_t nsid; /* namespace identifier */ 566 567 /* dword 2-3 */ 568 uint32_t rsvd2; 569 uint32_t rsvd3; 570 571 /* dword 4-5 */ 572 uint64_t mptr; /* metadata pointer */ 573 574 /* dword 6-7 */ 575 uint64_t prp1; /* prp entry 1 */ 576 577 /* dword 8-9 */ 578 uint64_t prp2; /* prp entry 2 */ 579 580 /* dword 10-15 */ 581 uint32_t cdw10; /* command-specific */ 582 uint32_t cdw11; /* command-specific */ 583 uint32_t cdw12; /* command-specific */ 584 uint32_t cdw13; /* command-specific */ 585 uint32_t cdw14; /* command-specific */ 586 uint32_t cdw15; /* command-specific */ 587 } __packed; 588 589 _Static_assert(sizeof(struct nvme_command) == 16 * 4, "bad size for nvme_command"); 590 591 struct nvme_completion { 592 /* dword 0 */ 593 uint32_t cdw0; /* command-specific */ 594 595 /* dword 1 */ 596 uint32_t rsvd1; 597 598 /* dword 2 */ 599 uint16_t sqhd; /* submission queue head pointer */ 600 uint16_t sqid; /* submission queue identifier */ 601 602 /* dword 3 */ 603 uint16_t cid; /* command identifier */ 604 uint16_t status; 605 } __packed; 606 607 _Static_assert(sizeof(struct nvme_completion) == 4 * 4, "bad size for nvme_completion"); 608 609 struct nvme_dsm_range { 610 uint32_t attributes; 611 uint32_t length; 612 uint64_t starting_lba; 613 } __packed; 614 615 /* Largest DSM Trim that can be done */ 616 #define NVME_MAX_DSM_TRIM 4096 617 618 _Static_assert(sizeof(struct nvme_dsm_range) == 16, "bad size for nvme_dsm_ranage"); 619 620 /* status code types */ 621 enum nvme_status_code_type { 622 NVME_SCT_GENERIC = 0x0, 623 NVME_SCT_COMMAND_SPECIFIC = 0x1, 624 NVME_SCT_MEDIA_ERROR = 0x2, 625 NVME_SCT_PATH_RELATED = 0x3, 626 /* 0x3-0x6 - reserved */ 627 NVME_SCT_VENDOR_SPECIFIC = 0x7, 628 }; 629 630 /* generic command status codes */ 631 enum nvme_generic_command_status_code { 632 NVME_SC_SUCCESS = 0x00, 633 NVME_SC_INVALID_OPCODE = 0x01, 634 NVME_SC_INVALID_FIELD = 0x02, 635 NVME_SC_COMMAND_ID_CONFLICT = 0x03, 636 NVME_SC_DATA_TRANSFER_ERROR = 0x04, 637 NVME_SC_ABORTED_POWER_LOSS = 0x05, 638 NVME_SC_INTERNAL_DEVICE_ERROR = 0x06, 639 NVME_SC_ABORTED_BY_REQUEST = 0x07, 640 NVME_SC_ABORTED_SQ_DELETION = 0x08, 641 NVME_SC_ABORTED_FAILED_FUSED = 0x09, 642 NVME_SC_ABORTED_MISSING_FUSED = 0x0a, 643 NVME_SC_INVALID_NAMESPACE_OR_FORMAT = 0x0b, 644 NVME_SC_COMMAND_SEQUENCE_ERROR = 0x0c, 645 NVME_SC_INVALID_SGL_SEGMENT_DESCR = 0x0d, 646 NVME_SC_INVALID_NUMBER_OF_SGL_DESCR = 0x0e, 647 NVME_SC_DATA_SGL_LENGTH_INVALID = 0x0f, 648 NVME_SC_METADATA_SGL_LENGTH_INVALID = 0x10, 649 NVME_SC_SGL_DESCRIPTOR_TYPE_INVALID = 0x11, 650 NVME_SC_INVALID_USE_OF_CMB = 0x12, 651 NVME_SC_PRP_OFFET_INVALID = 0x13, 652 NVME_SC_ATOMIC_WRITE_UNIT_EXCEEDED = 0x14, 653 NVME_SC_OPERATION_DENIED = 0x15, 654 NVME_SC_SGL_OFFSET_INVALID = 0x16, 655 /* 0x17 - reserved */ 656 NVME_SC_HOST_ID_INCONSISTENT_FORMAT = 0x18, 657 NVME_SC_KEEP_ALIVE_TIMEOUT_EXPIRED = 0x19, 658 NVME_SC_KEEP_ALIVE_TIMEOUT_INVALID = 0x1a, 659 NVME_SC_ABORTED_DUE_TO_PREEMPT = 0x1b, 660 NVME_SC_SANITIZE_FAILED = 0x1c, 661 NVME_SC_SANITIZE_IN_PROGRESS = 0x1d, 662 NVME_SC_SGL_DATA_BLOCK_GRAN_INVALID = 0x1e, 663 NVME_SC_NOT_SUPPORTED_IN_CMB = 0x1f, 664 NVME_SC_NAMESPACE_IS_WRITE_PROTECTED = 0x20, 665 NVME_SC_COMMAND_INTERRUPTED = 0x21, 666 NVME_SC_TRANSIENT_TRANSPORT_ERROR = 0x22, 667 668 NVME_SC_LBA_OUT_OF_RANGE = 0x80, 669 NVME_SC_CAPACITY_EXCEEDED = 0x81, 670 NVME_SC_NAMESPACE_NOT_READY = 0x82, 671 NVME_SC_RESERVATION_CONFLICT = 0x83, 672 NVME_SC_FORMAT_IN_PROGRESS = 0x84, 673 }; 674 675 /* command specific status codes */ 676 enum nvme_command_specific_status_code { 677 NVME_SC_COMPLETION_QUEUE_INVALID = 0x00, 678 NVME_SC_INVALID_QUEUE_IDENTIFIER = 0x01, 679 NVME_SC_MAXIMUM_QUEUE_SIZE_EXCEEDED = 0x02, 680 NVME_SC_ABORT_COMMAND_LIMIT_EXCEEDED = 0x03, 681 /* 0x04 - reserved */ 682 NVME_SC_ASYNC_EVENT_REQUEST_LIMIT_EXCEEDED = 0x05, 683 NVME_SC_INVALID_FIRMWARE_SLOT = 0x06, 684 NVME_SC_INVALID_FIRMWARE_IMAGE = 0x07, 685 NVME_SC_INVALID_INTERRUPT_VECTOR = 0x08, 686 NVME_SC_INVALID_LOG_PAGE = 0x09, 687 NVME_SC_INVALID_FORMAT = 0x0a, 688 NVME_SC_FIRMWARE_REQUIRES_RESET = 0x0b, 689 NVME_SC_INVALID_QUEUE_DELETION = 0x0c, 690 NVME_SC_FEATURE_NOT_SAVEABLE = 0x0d, 691 NVME_SC_FEATURE_NOT_CHANGEABLE = 0x0e, 692 NVME_SC_FEATURE_NOT_NS_SPECIFIC = 0x0f, 693 NVME_SC_FW_ACT_REQUIRES_NVMS_RESET = 0x10, 694 NVME_SC_FW_ACT_REQUIRES_RESET = 0x11, 695 NVME_SC_FW_ACT_REQUIRES_TIME = 0x12, 696 NVME_SC_FW_ACT_PROHIBITED = 0x13, 697 NVME_SC_OVERLAPPING_RANGE = 0x14, 698 NVME_SC_NS_INSUFFICIENT_CAPACITY = 0x15, 699 NVME_SC_NS_ID_UNAVAILABLE = 0x16, 700 /* 0x17 - reserved */ 701 NVME_SC_NS_ALREADY_ATTACHED = 0x18, 702 NVME_SC_NS_IS_PRIVATE = 0x19, 703 NVME_SC_NS_NOT_ATTACHED = 0x1a, 704 NVME_SC_THIN_PROV_NOT_SUPPORTED = 0x1b, 705 NVME_SC_CTRLR_LIST_INVALID = 0x1c, 706 NVME_SC_SELT_TEST_IN_PROGRESS = 0x1d, 707 NVME_SC_BOOT_PART_WRITE_PROHIB = 0x1e, 708 NVME_SC_INVALID_CTRLR_ID = 0x1f, 709 NVME_SC_INVALID_SEC_CTRLR_STATE = 0x20, 710 NVME_SC_INVALID_NUM_OF_CTRLR_RESRC = 0x21, 711 NVME_SC_INVALID_RESOURCE_ID = 0x22, 712 NVME_SC_SANITIZE_PROHIBITED_WPMRE = 0x23, 713 NVME_SC_ANA_GROUP_ID_INVALID = 0x24, 714 NVME_SC_ANA_ATTACH_FAILED = 0x25, 715 716 NVME_SC_CONFLICTING_ATTRIBUTES = 0x80, 717 NVME_SC_INVALID_PROTECTION_INFO = 0x81, 718 NVME_SC_ATTEMPTED_WRITE_TO_RO_PAGE = 0x82, 719 }; 720 721 /* media error status codes */ 722 enum nvme_media_error_status_code { 723 NVME_SC_WRITE_FAULTS = 0x80, 724 NVME_SC_UNRECOVERED_READ_ERROR = 0x81, 725 NVME_SC_GUARD_CHECK_ERROR = 0x82, 726 NVME_SC_APPLICATION_TAG_CHECK_ERROR = 0x83, 727 NVME_SC_REFERENCE_TAG_CHECK_ERROR = 0x84, 728 NVME_SC_COMPARE_FAILURE = 0x85, 729 NVME_SC_ACCESS_DENIED = 0x86, 730 NVME_SC_DEALLOCATED_OR_UNWRITTEN = 0x87, 731 }; 732 733 /* path related status codes */ 734 enum nvme_path_related_status_code { 735 NVME_SC_INTERNAL_PATH_ERROR = 0x00, 736 NVME_SC_ASYMMETRIC_ACCESS_PERSISTENT_LOSS = 0x01, 737 NVME_SC_ASYMMETRIC_ACCESS_INACCESSIBLE = 0x02, 738 NVME_SC_ASYMMETRIC_ACCESS_TRANSITION = 0x03, 739 NVME_SC_CONTROLLER_PATHING_ERROR = 0x60, 740 NVME_SC_HOST_PATHING_ERROR = 0x70, 741 NVME_SC_COMMAND_ABOTHED_BY_HOST = 0x71, 742 }; 743 744 /* admin opcodes */ 745 enum nvme_admin_opcode { 746 NVME_OPC_DELETE_IO_SQ = 0x00, 747 NVME_OPC_CREATE_IO_SQ = 0x01, 748 NVME_OPC_GET_LOG_PAGE = 0x02, 749 /* 0x03 - reserved */ 750 NVME_OPC_DELETE_IO_CQ = 0x04, 751 NVME_OPC_CREATE_IO_CQ = 0x05, 752 NVME_OPC_IDENTIFY = 0x06, 753 /* 0x07 - reserved */ 754 NVME_OPC_ABORT = 0x08, 755 NVME_OPC_SET_FEATURES = 0x09, 756 NVME_OPC_GET_FEATURES = 0x0a, 757 /* 0x0b - reserved */ 758 NVME_OPC_ASYNC_EVENT_REQUEST = 0x0c, 759 NVME_OPC_NAMESPACE_MANAGEMENT = 0x0d, 760 /* 0x0e-0x0f - reserved */ 761 NVME_OPC_FIRMWARE_ACTIVATE = 0x10, 762 NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD = 0x11, 763 /* 0x12-0x13 - reserved */ 764 NVME_OPC_DEVICE_SELF_TEST = 0x14, 765 NVME_OPC_NAMESPACE_ATTACHMENT = 0x15, 766 /* 0x16-0x17 - reserved */ 767 NVME_OPC_KEEP_ALIVE = 0x18, 768 NVME_OPC_DIRECTIVE_SEND = 0x19, 769 NVME_OPC_DIRECTIVE_RECEIVE = 0x1a, 770 /* 0x1b - reserved */ 771 NVME_OPC_VIRTUALIZATION_MANAGEMENT = 0x1c, 772 NVME_OPC_NVME_MI_SEND = 0x1d, 773 NVME_OPC_NVME_MI_RECEIVE = 0x1e, 774 /* 0x1f-0x7b - reserved */ 775 NVME_OPC_DOORBELL_BUFFER_CONFIG = 0x7c, 776 777 NVME_OPC_FORMAT_NVM = 0x80, 778 NVME_OPC_SECURITY_SEND = 0x81, 779 NVME_OPC_SECURITY_RECEIVE = 0x82, 780 /* 0x83 - reserved */ 781 NVME_OPC_SANITIZE = 0x84, 782 /* 0x85 - reserved */ 783 NVME_OPC_GET_LBA_STATUS = 0x86, 784 }; 785 786 /* nvme nvm opcodes */ 787 enum nvme_nvm_opcode { 788 NVME_OPC_FLUSH = 0x00, 789 NVME_OPC_WRITE = 0x01, 790 NVME_OPC_READ = 0x02, 791 /* 0x03 - reserved */ 792 NVME_OPC_WRITE_UNCORRECTABLE = 0x04, 793 NVME_OPC_COMPARE = 0x05, 794 /* 0x06-0x07 - reserved */ 795 NVME_OPC_WRITE_ZEROES = 0x08, 796 NVME_OPC_DATASET_MANAGEMENT = 0x09, 797 /* 0x0a-0x0b - reserved */ 798 NVME_OPC_VERIFY = 0x0c, 799 NVME_OPC_RESERVATION_REGISTER = 0x0d, 800 NVME_OPC_RESERVATION_REPORT = 0x0e, 801 /* 0x0f-0x10 - reserved */ 802 NVME_OPC_RESERVATION_ACQUIRE = 0x11, 803 /* 0x12-0x14 - reserved */ 804 NVME_OPC_RESERVATION_RELEASE = 0x15, 805 }; 806 807 enum nvme_feature { 808 /* 0x00 - reserved */ 809 NVME_FEAT_ARBITRATION = 0x01, 810 NVME_FEAT_POWER_MANAGEMENT = 0x02, 811 NVME_FEAT_LBA_RANGE_TYPE = 0x03, 812 NVME_FEAT_TEMPERATURE_THRESHOLD = 0x04, 813 NVME_FEAT_ERROR_RECOVERY = 0x05, 814 NVME_FEAT_VOLATILE_WRITE_CACHE = 0x06, 815 NVME_FEAT_NUMBER_OF_QUEUES = 0x07, 816 NVME_FEAT_INTERRUPT_COALESCING = 0x08, 817 NVME_FEAT_INTERRUPT_VECTOR_CONFIGURATION = 0x09, 818 NVME_FEAT_WRITE_ATOMICITY = 0x0A, 819 NVME_FEAT_ASYNC_EVENT_CONFIGURATION = 0x0B, 820 NVME_FEAT_AUTONOMOUS_POWER_STATE_TRANSITION = 0x0C, 821 NVME_FEAT_HOST_MEMORY_BUFFER = 0x0D, 822 NVME_FEAT_TIMESTAMP = 0x0E, 823 NVME_FEAT_KEEP_ALIVE_TIMER = 0x0F, 824 NVME_FEAT_HOST_CONTROLLED_THERMAL_MGMT = 0x10, 825 NVME_FEAT_NON_OP_POWER_STATE_CONFIG = 0x11, 826 NVME_FEAT_READ_RECOVERY_LEVEL_CONFIG = 0x12, 827 NVME_FEAT_PREDICTABLE_LATENCY_MODE_CONFIG = 0x13, 828 NVME_FEAT_PREDICTABLE_LATENCY_MODE_WINDOW = 0x14, 829 NVME_FEAT_LBA_STATUS_INFORMATION_ATTRIBUTES = 0x15, 830 NVME_FEAT_HOST_BEHAVIOR_SUPPORT = 0x16, 831 NVME_FEAT_SANITIZE_CONFIG = 0x17, 832 NVME_FEAT_ENDURANCE_GROUP_EVENT_CONFIGURATION = 0x18, 833 /* 0x19-0x77 - reserved */ 834 /* 0x78-0x7f - NVMe Management Interface */ 835 NVME_FEAT_SOFTWARE_PROGRESS_MARKER = 0x80, 836 NVME_FEAT_HOST_IDENTIFIER = 0x81, 837 NVME_FEAT_RESERVATION_NOTIFICATION_MASK = 0x82, 838 NVME_FEAT_RESERVATION_PERSISTENCE = 0x83, 839 NVME_FEAT_NAMESPACE_WRITE_PROTECTION_CONFIG = 0x84, 840 /* 0x85-0xBF - command set specific (reserved) */ 841 /* 0xC0-0xFF - vendor specific */ 842 }; 843 844 enum nvme_dsm_attribute { 845 NVME_DSM_ATTR_INTEGRAL_READ = 0x1, 846 NVME_DSM_ATTR_INTEGRAL_WRITE = 0x2, 847 NVME_DSM_ATTR_DEALLOCATE = 0x4, 848 }; 849 850 enum nvme_activate_action { 851 NVME_AA_REPLACE_NO_ACTIVATE = 0x0, 852 NVME_AA_REPLACE_ACTIVATE = 0x1, 853 NVME_AA_ACTIVATE = 0x2, 854 }; 855 856 struct nvme_power_state { 857 /** Maximum Power */ 858 uint16_t mp; /* Maximum Power */ 859 uint8_t ps_rsvd1; 860 uint8_t mps_nops; /* Max Power Scale, Non-Operational State */ 861 862 uint32_t enlat; /* Entry Latency */ 863 uint32_t exlat; /* Exit Latency */ 864 865 uint8_t rrt; /* Relative Read Throughput */ 866 uint8_t rrl; /* Relative Read Latency */ 867 uint8_t rwt; /* Relative Write Throughput */ 868 uint8_t rwl; /* Relative Write Latency */ 869 870 uint16_t idlp; /* Idle Power */ 871 uint8_t ips; /* Idle Power Scale */ 872 uint8_t ps_rsvd8; 873 874 uint16_t actp; /* Active Power */ 875 uint8_t apw_aps; /* Active Power Workload, Active Power Scale */ 876 uint8_t ps_rsvd10[9]; 877 } __packed; 878 879 _Static_assert(sizeof(struct nvme_power_state) == 32, "bad size for nvme_power_state"); 880 881 #define NVME_SERIAL_NUMBER_LENGTH 20 882 #define NVME_MODEL_NUMBER_LENGTH 40 883 #define NVME_FIRMWARE_REVISION_LENGTH 8 884 885 struct nvme_controller_data { 886 /* bytes 0-255: controller capabilities and features */ 887 888 /** pci vendor id */ 889 uint16_t vid; 890 891 /** pci subsystem vendor id */ 892 uint16_t ssvid; 893 894 /** serial number */ 895 uint8_t sn[NVME_SERIAL_NUMBER_LENGTH]; 896 897 /** model number */ 898 uint8_t mn[NVME_MODEL_NUMBER_LENGTH]; 899 900 /** firmware revision */ 901 uint8_t fr[NVME_FIRMWARE_REVISION_LENGTH]; 902 903 /** recommended arbitration burst */ 904 uint8_t rab; 905 906 /** ieee oui identifier */ 907 uint8_t ieee[3]; 908 909 /** multi-interface capabilities */ 910 uint8_t mic; 911 912 /** maximum data transfer size */ 913 uint8_t mdts; 914 915 /** Controller ID */ 916 uint16_t ctrlr_id; 917 918 /** Version */ 919 uint32_t ver; 920 921 /** RTD3 Resume Latency */ 922 uint32_t rtd3r; 923 924 /** RTD3 Enter Latency */ 925 uint32_t rtd3e; 926 927 /** Optional Asynchronous Events Supported */ 928 uint32_t oaes; /* bitfield really */ 929 930 /** Controller Attributes */ 931 uint32_t ctratt; /* bitfield really */ 932 933 /** Read Recovery Levels Supported */ 934 uint16_t rrls; 935 936 uint8_t reserved1[9]; 937 938 /** Controller Type */ 939 uint8_t cntrltype; 940 941 /** FRU Globally Unique Identifier */ 942 uint8_t fguid[16]; 943 944 /** Command Retry Delay Time 1 */ 945 uint16_t crdt1; 946 947 /** Command Retry Delay Time 2 */ 948 uint16_t crdt2; 949 950 /** Command Retry Delay Time 3 */ 951 uint16_t crdt3; 952 953 uint8_t reserved2[122]; 954 955 /* bytes 256-511: admin command set attributes */ 956 957 /** optional admin command support */ 958 uint16_t oacs; 959 960 /** abort command limit */ 961 uint8_t acl; 962 963 /** asynchronous event request limit */ 964 uint8_t aerl; 965 966 /** firmware updates */ 967 uint8_t frmw; 968 969 /** log page attributes */ 970 uint8_t lpa; 971 972 /** error log page entries */ 973 uint8_t elpe; 974 975 /** number of power states supported */ 976 uint8_t npss; 977 978 /** admin vendor specific command configuration */ 979 uint8_t avscc; 980 981 /** Autonomous Power State Transition Attributes */ 982 uint8_t apsta; 983 984 /** Warning Composite Temperature Threshold */ 985 uint16_t wctemp; 986 987 /** Critical Composite Temperature Threshold */ 988 uint16_t cctemp; 989 990 /** Maximum Time for Firmware Activation */ 991 uint16_t mtfa; 992 993 /** Host Memory Buffer Preferred Size */ 994 uint32_t hmpre; 995 996 /** Host Memory Buffer Minimum Size */ 997 uint32_t hmmin; 998 999 /** Name space capabilities */ 1000 struct { 1001 /* if nsmgmt, report tnvmcap and unvmcap */ 1002 uint8_t tnvmcap[16]; 1003 uint8_t unvmcap[16]; 1004 } __packed untncap; 1005 1006 /** Replay Protected Memory Block Support */ 1007 uint32_t rpmbs; /* Really a bitfield */ 1008 1009 /** Extended Device Self-test Time */ 1010 uint16_t edstt; 1011 1012 /** Device Self-test Options */ 1013 uint8_t dsto; /* Really a bitfield */ 1014 1015 /** Firmware Update Granularity */ 1016 uint8_t fwug; 1017 1018 /** Keep Alive Support */ 1019 uint16_t kas; 1020 1021 /** Host Controlled Thermal Management Attributes */ 1022 uint16_t hctma; /* Really a bitfield */ 1023 1024 /** Minimum Thermal Management Temperature */ 1025 uint16_t mntmt; 1026 1027 /** Maximum Thermal Management Temperature */ 1028 uint16_t mxtmt; 1029 1030 /** Sanitize Capabilities */ 1031 uint32_t sanicap; /* Really a bitfield */ 1032 1033 /** Host Memory Buffer Minimum Descriptor Entry Size */ 1034 uint32_t hmminds; 1035 1036 /** Host Memory Maximum Descriptors Entries */ 1037 uint16_t hmmaxd; 1038 1039 /** NVM Set Identifier Maximum */ 1040 uint16_t nsetidmax; 1041 1042 /** Endurance Group Identifier Maximum */ 1043 uint16_t endgidmax; 1044 1045 /** ANA Transition Time */ 1046 uint8_t anatt; 1047 1048 /** Asymmetric Namespace Access Capabilities */ 1049 uint8_t anacap; 1050 1051 /** ANA Group Identifier Maximum */ 1052 uint32_t anagrpmax; 1053 1054 /** Number of ANA Group Identifiers */ 1055 uint32_t nanagrpid; 1056 1057 /** Persistent Event Log Size */ 1058 uint32_t pels; 1059 1060 uint8_t reserved3[156]; 1061 /* bytes 512-703: nvm command set attributes */ 1062 1063 /** submission queue entry size */ 1064 uint8_t sqes; 1065 1066 /** completion queue entry size */ 1067 uint8_t cqes; 1068 1069 /** Maximum Outstanding Commands */ 1070 uint16_t maxcmd; 1071 1072 /** number of namespaces */ 1073 uint32_t nn; 1074 1075 /** optional nvm command support */ 1076 uint16_t oncs; 1077 1078 /** fused operation support */ 1079 uint16_t fuses; 1080 1081 /** format nvm attributes */ 1082 uint8_t fna; 1083 1084 /** volatile write cache */ 1085 uint8_t vwc; 1086 1087 /** Atomic Write Unit Normal */ 1088 uint16_t awun; 1089 1090 /** Atomic Write Unit Power Fail */ 1091 uint16_t awupf; 1092 1093 /** NVM Vendor Specific Command Configuration */ 1094 uint8_t nvscc; 1095 1096 /** Namespace Write Protection Capabilities */ 1097 uint8_t nwpc; 1098 1099 /** Atomic Compare & Write Unit */ 1100 uint16_t acwu; 1101 uint16_t reserved6; 1102 1103 /** SGL Support */ 1104 uint32_t sgls; 1105 1106 /** Maximum Number of Allowed Namespaces */ 1107 uint32_t mnan; 1108 1109 /* bytes 540-767: Reserved */ 1110 uint8_t reserved7[224]; 1111 1112 /** NVM Subsystem NVMe Qualified Name */ 1113 uint8_t subnqn[256]; 1114 1115 /* bytes 1024-1791: Reserved */ 1116 uint8_t reserved8[768]; 1117 1118 /* bytes 1792-2047: NVMe over Fabrics specification */ 1119 uint8_t reserved9[256]; 1120 1121 /* bytes 2048-3071: power state descriptors */ 1122 struct nvme_power_state power_state[32]; 1123 1124 /* bytes 3072-4095: vendor specific */ 1125 uint8_t vs[1024]; 1126 } __packed __aligned(4); 1127 1128 _Static_assert(sizeof(struct nvme_controller_data) == 4096, "bad size for nvme_controller_data"); 1129 1130 struct nvme_namespace_data { 1131 /** namespace size */ 1132 uint64_t nsze; 1133 1134 /** namespace capacity */ 1135 uint64_t ncap; 1136 1137 /** namespace utilization */ 1138 uint64_t nuse; 1139 1140 /** namespace features */ 1141 uint8_t nsfeat; 1142 1143 /** number of lba formats */ 1144 uint8_t nlbaf; 1145 1146 /** formatted lba size */ 1147 uint8_t flbas; 1148 1149 /** metadata capabilities */ 1150 uint8_t mc; 1151 1152 /** end-to-end data protection capabilities */ 1153 uint8_t dpc; 1154 1155 /** end-to-end data protection type settings */ 1156 uint8_t dps; 1157 1158 /** Namespace Multi-path I/O and Namespace Sharing Capabilities */ 1159 uint8_t nmic; 1160 1161 /** Reservation Capabilities */ 1162 uint8_t rescap; 1163 1164 /** Format Progress Indicator */ 1165 uint8_t fpi; 1166 1167 /** Deallocate Logical Block Features */ 1168 uint8_t dlfeat; 1169 1170 /** Namespace Atomic Write Unit Normal */ 1171 uint16_t nawun; 1172 1173 /** Namespace Atomic Write Unit Power Fail */ 1174 uint16_t nawupf; 1175 1176 /** Namespace Atomic Compare & Write Unit */ 1177 uint16_t nacwu; 1178 1179 /** Namespace Atomic Boundary Size Normal */ 1180 uint16_t nabsn; 1181 1182 /** Namespace Atomic Boundary Offset */ 1183 uint16_t nabo; 1184 1185 /** Namespace Atomic Boundary Size Power Fail */ 1186 uint16_t nabspf; 1187 1188 /** Namespace Optimal IO Boundary */ 1189 uint16_t noiob; 1190 1191 /** NVM Capacity */ 1192 uint8_t nvmcap[16]; 1193 1194 /** Namespace Preferred Write Granularity */ 1195 uint16_t npwg; 1196 1197 /** Namespace Preferred Write Alignment */ 1198 uint16_t npwa; 1199 1200 /** Namespace Preferred Deallocate Granularity */ 1201 uint16_t npdg; 1202 1203 /** Namespace Preferred Deallocate Alignment */ 1204 uint16_t npda; 1205 1206 /** Namespace Optimal Write Size */ 1207 uint16_t nows; 1208 1209 /* bytes 74-91: Reserved */ 1210 uint8_t reserved5[18]; 1211 1212 /** ANA Group Identifier */ 1213 uint32_t anagrpid; 1214 1215 /* bytes 96-98: Reserved */ 1216 uint8_t reserved6[3]; 1217 1218 /** Namespace Attributes */ 1219 uint8_t nsattr; 1220 1221 /** NVM Set Identifier */ 1222 uint16_t nvmsetid; 1223 1224 /** Endurance Group Identifier */ 1225 uint16_t endgid; 1226 1227 /** Namespace Globally Unique Identifier */ 1228 uint8_t nguid[16]; 1229 1230 /** IEEE Extended Unique Identifier */ 1231 uint8_t eui64[8]; 1232 1233 /** lba format support */ 1234 uint32_t lbaf[16]; 1235 1236 uint8_t reserved7[192]; 1237 1238 uint8_t vendor_specific[3712]; 1239 } __packed __aligned(4); 1240 1241 _Static_assert(sizeof(struct nvme_namespace_data) == 4096, "bad size for nvme_namepsace_data"); 1242 1243 enum nvme_log_page { 1244 /* 0x00 - reserved */ 1245 NVME_LOG_ERROR = 0x01, 1246 NVME_LOG_HEALTH_INFORMATION = 0x02, 1247 NVME_LOG_FIRMWARE_SLOT = 0x03, 1248 NVME_LOG_CHANGED_NAMESPACE = 0x04, 1249 NVME_LOG_COMMAND_EFFECT = 0x05, 1250 NVME_LOG_DEVICE_SELF_TEST = 0x06, 1251 NVME_LOG_TELEMETRY_HOST_INITIATED = 0x07, 1252 NVME_LOG_TELEMETRY_CONTROLLER_INITIATED = 0x08, 1253 NVME_LOG_ENDURANCE_GROUP_INFORMATION = 0x09, 1254 NVME_LOG_PREDICTABLE_LATENCY_PER_NVM_SET = 0x0a, 1255 NVME_LOG_PREDICTABLE_LATENCY_EVENT_AGGREGATE = 0x0b, 1256 NVME_LOG_ASYMMETRIC_NAMESPAVE_ACCESS = 0x0c, 1257 NVME_LOG_PERSISTENT_EVENT_LOG = 0x0d, 1258 NVME_LOG_LBA_STATUS_INFORMATION = 0x0e, 1259 NVME_LOG_ENDURANCE_GROUP_EVENT_AGGREGATE = 0x0f, 1260 /* 0x06-0x7F - reserved */ 1261 /* 0x80-0xBF - I/O command set specific */ 1262 NVME_LOG_RES_NOTIFICATION = 0x80, 1263 NVME_LOG_SANITIZE_STATUS = 0x81, 1264 /* 0x82-0xBF - reserved */ 1265 /* 0xC0-0xFF - vendor specific */ 1266 1267 /* 1268 * The following are Intel Specific log pages, but they seem 1269 * to be widely implemented. 1270 */ 1271 INTEL_LOG_READ_LAT_LOG = 0xc1, 1272 INTEL_LOG_WRITE_LAT_LOG = 0xc2, 1273 INTEL_LOG_TEMP_STATS = 0xc5, 1274 INTEL_LOG_ADD_SMART = 0xca, 1275 INTEL_LOG_DRIVE_MKT_NAME = 0xdd, 1276 1277 /* 1278 * HGST log page, with lots ofs sub pages. 1279 */ 1280 HGST_INFO_LOG = 0xc1, 1281 }; 1282 1283 struct nvme_error_information_entry { 1284 uint64_t error_count; 1285 uint16_t sqid; 1286 uint16_t cid; 1287 uint16_t status; 1288 uint16_t error_location; 1289 uint64_t lba; 1290 uint32_t nsid; 1291 uint8_t vendor_specific; 1292 uint8_t trtype; 1293 uint16_t reserved30; 1294 uint64_t csi; 1295 uint16_t ttsi; 1296 uint8_t reserved[22]; 1297 } __packed __aligned(4); 1298 1299 _Static_assert(sizeof(struct nvme_error_information_entry) == 64, "bad size for nvme_error_information_entry"); 1300 1301 struct nvme_health_information_page { 1302 uint8_t critical_warning; 1303 uint16_t temperature; 1304 uint8_t available_spare; 1305 uint8_t available_spare_threshold; 1306 uint8_t percentage_used; 1307 1308 uint8_t reserved[26]; 1309 1310 /* 1311 * Note that the following are 128-bit values, but are 1312 * defined as an array of 2 64-bit values. 1313 */ 1314 /* Data Units Read is always in 512-byte units. */ 1315 uint64_t data_units_read[2]; 1316 /* Data Units Written is always in 512-byte units. */ 1317 uint64_t data_units_written[2]; 1318 /* For NVM command set, this includes Compare commands. */ 1319 uint64_t host_read_commands[2]; 1320 uint64_t host_write_commands[2]; 1321 /* Controller Busy Time is reported in minutes. */ 1322 uint64_t controller_busy_time[2]; 1323 uint64_t power_cycles[2]; 1324 uint64_t power_on_hours[2]; 1325 uint64_t unsafe_shutdowns[2]; 1326 uint64_t media_errors[2]; 1327 uint64_t num_error_info_log_entries[2]; 1328 uint32_t warning_temp_time; 1329 uint32_t error_temp_time; 1330 uint16_t temp_sensor[8]; 1331 /* Thermal Management Temperature 1 Transition Count */ 1332 uint32_t tmt1tc; 1333 /* Thermal Management Temperature 2 Transition Count */ 1334 uint32_t tmt2tc; 1335 /* Total Time For Thermal Management Temperature 1 */ 1336 uint32_t ttftmt1; 1337 /* Total Time For Thermal Management Temperature 2 */ 1338 uint32_t ttftmt2; 1339 1340 uint8_t reserved2[280]; 1341 } __packed __aligned(4); 1342 1343 _Static_assert(sizeof(struct nvme_health_information_page) == 512, "bad size for nvme_health_information_page"); 1344 1345 struct nvme_firmware_page { 1346 uint8_t afi; 1347 uint8_t reserved[7]; 1348 uint64_t revision[7]; /* revisions for 7 slots */ 1349 uint8_t reserved2[448]; 1350 } __packed __aligned(4); 1351 1352 _Static_assert(sizeof(struct nvme_firmware_page) == 512, "bad size for nvme_firmware_page"); 1353 1354 struct nvme_ns_list { 1355 uint32_t ns[1024]; 1356 } __packed __aligned(4); 1357 1358 _Static_assert(sizeof(struct nvme_ns_list) == 4096, "bad size for nvme_ns_list"); 1359 1360 struct nvme_command_effects_page { 1361 uint32_t acs[256]; 1362 uint32_t iocs[256]; 1363 uint8_t reserved[2048]; 1364 } __packed __aligned(4); 1365 1366 _Static_assert(sizeof(struct nvme_command_effects_page) == 4096, 1367 "bad size for nvme_command_effects_page"); 1368 1369 struct nvme_res_notification_page { 1370 uint64_t log_page_count; 1371 uint8_t log_page_type; 1372 uint8_t available_log_pages; 1373 uint8_t reserved2; 1374 uint32_t nsid; 1375 uint8_t reserved[48]; 1376 } __packed __aligned(4); 1377 1378 _Static_assert(sizeof(struct nvme_res_notification_page) == 64, 1379 "bad size for nvme_res_notification_page"); 1380 1381 struct nvme_sanitize_status_page { 1382 uint16_t sprog; 1383 uint16_t sstat; 1384 uint32_t scdw10; 1385 uint32_t etfo; 1386 uint32_t etfbe; 1387 uint32_t etfce; 1388 uint32_t etfownd; 1389 uint32_t etfbewnd; 1390 uint32_t etfcewnd; 1391 uint8_t reserved[480]; 1392 } __packed __aligned(4); 1393 1394 _Static_assert(sizeof(struct nvme_sanitize_status_page) == 512, 1395 "bad size for nvme_sanitize_status_page"); 1396 1397 struct intel_log_temp_stats 1398 { 1399 uint64_t current; 1400 uint64_t overtemp_flag_last; 1401 uint64_t overtemp_flag_life; 1402 uint64_t max_temp; 1403 uint64_t min_temp; 1404 uint64_t _rsvd[5]; 1405 uint64_t max_oper_temp; 1406 uint64_t min_oper_temp; 1407 uint64_t est_offset; 1408 } __packed __aligned(4); 1409 1410 _Static_assert(sizeof(struct intel_log_temp_stats) == 13 * 8, "bad size for intel_log_temp_stats"); 1411 1412 struct nvme_resv_reg_ctrlr 1413 { 1414 uint16_t ctrlr_id; /* Controller ID */ 1415 uint8_t rcsts; /* Reservation Status */ 1416 uint8_t reserved3[5]; 1417 uint64_t hostid; /* Host Identifier */ 1418 uint64_t rkey; /* Reservation Key */ 1419 } __packed __aligned(4); 1420 1421 _Static_assert(sizeof(struct nvme_resv_reg_ctrlr) == 24, "bad size for nvme_resv_reg_ctrlr"); 1422 1423 struct nvme_resv_reg_ctrlr_ext 1424 { 1425 uint16_t ctrlr_id; /* Controller ID */ 1426 uint8_t rcsts; /* Reservation Status */ 1427 uint8_t reserved3[5]; 1428 uint64_t rkey; /* Reservation Key */ 1429 uint64_t hostid[2]; /* Host Identifier */ 1430 uint8_t reserved32[32]; 1431 } __packed __aligned(4); 1432 1433 _Static_assert(sizeof(struct nvme_resv_reg_ctrlr_ext) == 64, "bad size for nvme_resv_reg_ctrlr_ext"); 1434 1435 struct nvme_resv_status 1436 { 1437 uint32_t gen; /* Generation */ 1438 uint8_t rtype; /* Reservation Type */ 1439 uint8_t regctl[2]; /* Number of Registered Controllers */ 1440 uint8_t reserved7[2]; 1441 uint8_t ptpls; /* Persist Through Power Loss State */ 1442 uint8_t reserved10[14]; 1443 struct nvme_resv_reg_ctrlr ctrlr[0]; 1444 } __packed __aligned(4); 1445 1446 _Static_assert(sizeof(struct nvme_resv_status) == 24, "bad size for nvme_resv_status"); 1447 1448 struct nvme_resv_status_ext 1449 { 1450 uint32_t gen; /* Generation */ 1451 uint8_t rtype; /* Reservation Type */ 1452 uint8_t regctl[2]; /* Number of Registered Controllers */ 1453 uint8_t reserved7[2]; 1454 uint8_t ptpls; /* Persist Through Power Loss State */ 1455 uint8_t reserved10[14]; 1456 uint8_t reserved24[40]; 1457 struct nvme_resv_reg_ctrlr_ext ctrlr[0]; 1458 } __packed __aligned(4); 1459 1460 _Static_assert(sizeof(struct nvme_resv_status_ext) == 64, "bad size for nvme_resv_status_ext"); 1461 1462 #define NVME_TEST_MAX_THREADS 128 1463 1464 struct nvme_io_test { 1465 enum nvme_nvm_opcode opc; 1466 uint32_t size; 1467 uint32_t time; /* in seconds */ 1468 uint32_t num_threads; 1469 uint32_t flags; 1470 uint64_t io_completed[NVME_TEST_MAX_THREADS]; 1471 }; 1472 1473 enum nvme_io_test_flags { 1474 /* 1475 * Specifies whether dev_refthread/dev_relthread should be 1476 * called during NVME_BIO_TEST. Ignored for other test 1477 * types. 1478 */ 1479 NVME_TEST_FLAG_REFTHREAD = 0x1, 1480 }; 1481 1482 struct nvme_pt_command { 1483 /* 1484 * cmd is used to specify a passthrough command to a controller or 1485 * namespace. 1486 * 1487 * The following fields from cmd may be specified by the caller: 1488 * * opc (opcode) 1489 * * nsid (namespace id) - for admin commands only 1490 * * cdw10-cdw15 1491 * 1492 * Remaining fields must be set to 0 by the caller. 1493 */ 1494 struct nvme_command cmd; 1495 1496 /* 1497 * cpl returns completion status for the passthrough command 1498 * specified by cmd. 1499 * 1500 * The following fields will be filled out by the driver, for 1501 * consumption by the caller: 1502 * * cdw0 1503 * * status (except for phase) 1504 * 1505 * Remaining fields will be set to 0 by the driver. 1506 */ 1507 struct nvme_completion cpl; 1508 1509 /* buf is the data buffer associated with this passthrough command. */ 1510 void * buf; 1511 1512 /* 1513 * len is the length of the data buffer associated with this 1514 * passthrough command. 1515 */ 1516 uint32_t len; 1517 1518 /* 1519 * is_read = 1 if the passthrough command will read data into the 1520 * supplied buffer from the controller. 1521 * 1522 * is_read = 0 if the passthrough command will write data from the 1523 * supplied buffer to the controller. 1524 */ 1525 uint32_t is_read; 1526 1527 /* 1528 * driver_lock is used by the driver only. It must be set to 0 1529 * by the caller. 1530 */ 1531 struct mtx * driver_lock; 1532 }; 1533 1534 struct nvme_get_nsid { 1535 char cdev[SPECNAMELEN + 1]; 1536 uint32_t nsid; 1537 }; 1538 1539 struct nvme_hmb_desc { 1540 uint64_t addr; 1541 uint32_t size; 1542 uint32_t reserved; 1543 }; 1544 1545 #define nvme_completion_is_error(cpl) \ 1546 (NVME_STATUS_GET_SC((cpl)->status) != 0 || NVME_STATUS_GET_SCT((cpl)->status) != 0) 1547 1548 void nvme_strvis(uint8_t *dst, const uint8_t *src, int dstlen, int srclen); 1549 1550 #ifdef _KERNEL 1551 1552 struct bio; 1553 struct thread; 1554 1555 struct nvme_namespace; 1556 struct nvme_controller; 1557 struct nvme_consumer; 1558 1559 typedef void (*nvme_cb_fn_t)(void *, const struct nvme_completion *); 1560 1561 typedef void *(*nvme_cons_ns_fn_t)(struct nvme_namespace *, void *); 1562 typedef void *(*nvme_cons_ctrlr_fn_t)(struct nvme_controller *); 1563 typedef void (*nvme_cons_async_fn_t)(void *, const struct nvme_completion *, 1564 uint32_t, void *, uint32_t); 1565 typedef void (*nvme_cons_fail_fn_t)(void *); 1566 1567 enum nvme_namespace_flags { 1568 NVME_NS_DEALLOCATE_SUPPORTED = 0x1, 1569 NVME_NS_FLUSH_SUPPORTED = 0x2, 1570 }; 1571 1572 int nvme_ctrlr_passthrough_cmd(struct nvme_controller *ctrlr, 1573 struct nvme_pt_command *pt, 1574 uint32_t nsid, int is_user_buffer, 1575 int is_admin_cmd); 1576 1577 /* Admin functions */ 1578 void nvme_ctrlr_cmd_set_feature(struct nvme_controller *ctrlr, 1579 uint8_t feature, uint32_t cdw11, 1580 uint32_t cdw12, uint32_t cdw13, 1581 uint32_t cdw14, uint32_t cdw15, 1582 void *payload, uint32_t payload_size, 1583 nvme_cb_fn_t cb_fn, void *cb_arg); 1584 void nvme_ctrlr_cmd_get_feature(struct nvme_controller *ctrlr, 1585 uint8_t feature, uint32_t cdw11, 1586 void *payload, uint32_t payload_size, 1587 nvme_cb_fn_t cb_fn, void *cb_arg); 1588 void nvme_ctrlr_cmd_get_log_page(struct nvme_controller *ctrlr, 1589 uint8_t log_page, uint32_t nsid, 1590 void *payload, uint32_t payload_size, 1591 nvme_cb_fn_t cb_fn, void *cb_arg); 1592 1593 /* NVM I/O functions */ 1594 int nvme_ns_cmd_write(struct nvme_namespace *ns, void *payload, 1595 uint64_t lba, uint32_t lba_count, nvme_cb_fn_t cb_fn, 1596 void *cb_arg); 1597 int nvme_ns_cmd_write_bio(struct nvme_namespace *ns, struct bio *bp, 1598 nvme_cb_fn_t cb_fn, void *cb_arg); 1599 int nvme_ns_cmd_read(struct nvme_namespace *ns, void *payload, 1600 uint64_t lba, uint32_t lba_count, nvme_cb_fn_t cb_fn, 1601 void *cb_arg); 1602 int nvme_ns_cmd_read_bio(struct nvme_namespace *ns, struct bio *bp, 1603 nvme_cb_fn_t cb_fn, void *cb_arg); 1604 int nvme_ns_cmd_deallocate(struct nvme_namespace *ns, void *payload, 1605 uint8_t num_ranges, nvme_cb_fn_t cb_fn, 1606 void *cb_arg); 1607 int nvme_ns_cmd_flush(struct nvme_namespace *ns, nvme_cb_fn_t cb_fn, 1608 void *cb_arg); 1609 int nvme_ns_dump(struct nvme_namespace *ns, void *virt, off_t offset, 1610 size_t len); 1611 1612 /* Registration functions */ 1613 struct nvme_consumer * nvme_register_consumer(nvme_cons_ns_fn_t ns_fn, 1614 nvme_cons_ctrlr_fn_t ctrlr_fn, 1615 nvme_cons_async_fn_t async_fn, 1616 nvme_cons_fail_fn_t fail_fn); 1617 void nvme_unregister_consumer(struct nvme_consumer *consumer); 1618 1619 /* Controller helper functions */ 1620 device_t nvme_ctrlr_get_device(struct nvme_controller *ctrlr); 1621 const struct nvme_controller_data * 1622 nvme_ctrlr_get_data(struct nvme_controller *ctrlr); 1623 static inline bool 1624 nvme_ctrlr_has_dataset_mgmt(const struct nvme_controller_data *cd) 1625 { 1626 /* Assumes cd was byte swapped by nvme_controller_data_swapbytes() */ 1627 return ((cd->oncs >> NVME_CTRLR_DATA_ONCS_DSM_SHIFT) & 1628 NVME_CTRLR_DATA_ONCS_DSM_MASK); 1629 } 1630 1631 /* Namespace helper functions */ 1632 uint32_t nvme_ns_get_max_io_xfer_size(struct nvme_namespace *ns); 1633 uint32_t nvme_ns_get_sector_size(struct nvme_namespace *ns); 1634 uint64_t nvme_ns_get_num_sectors(struct nvme_namespace *ns); 1635 uint64_t nvme_ns_get_size(struct nvme_namespace *ns); 1636 uint32_t nvme_ns_get_flags(struct nvme_namespace *ns); 1637 const char * nvme_ns_get_serial_number(struct nvme_namespace *ns); 1638 const char * nvme_ns_get_model_number(struct nvme_namespace *ns); 1639 const struct nvme_namespace_data * 1640 nvme_ns_get_data(struct nvme_namespace *ns); 1641 uint32_t nvme_ns_get_stripesize(struct nvme_namespace *ns); 1642 1643 int nvme_ns_bio_process(struct nvme_namespace *ns, struct bio *bp, 1644 nvme_cb_fn_t cb_fn); 1645 int nvme_ns_ioctl_process(struct nvme_namespace *ns, u_long cmd, 1646 caddr_t arg, int flag, struct thread *td); 1647 1648 /* 1649 * Command building helper functions -- shared with CAM 1650 * These functions assume allocator zeros out cmd structure 1651 * CAM's xpt_get_ccb and the request allocator for nvme both 1652 * do zero'd allocations. 1653 */ 1654 static inline 1655 void nvme_ns_flush_cmd(struct nvme_command *cmd, uint32_t nsid) 1656 { 1657 1658 cmd->opc = NVME_OPC_FLUSH; 1659 cmd->nsid = htole32(nsid); 1660 } 1661 1662 static inline 1663 void nvme_ns_rw_cmd(struct nvme_command *cmd, uint32_t rwcmd, uint32_t nsid, 1664 uint64_t lba, uint32_t count) 1665 { 1666 cmd->opc = rwcmd; 1667 cmd->nsid = htole32(nsid); 1668 cmd->cdw10 = htole32(lba & 0xffffffffu); 1669 cmd->cdw11 = htole32(lba >> 32); 1670 cmd->cdw12 = htole32(count-1); 1671 } 1672 1673 static inline 1674 void nvme_ns_write_cmd(struct nvme_command *cmd, uint32_t nsid, 1675 uint64_t lba, uint32_t count) 1676 { 1677 nvme_ns_rw_cmd(cmd, NVME_OPC_WRITE, nsid, lba, count); 1678 } 1679 1680 static inline 1681 void nvme_ns_read_cmd(struct nvme_command *cmd, uint32_t nsid, 1682 uint64_t lba, uint32_t count) 1683 { 1684 nvme_ns_rw_cmd(cmd, NVME_OPC_READ, nsid, lba, count); 1685 } 1686 1687 static inline 1688 void nvme_ns_trim_cmd(struct nvme_command *cmd, uint32_t nsid, 1689 uint32_t num_ranges) 1690 { 1691 cmd->opc = NVME_OPC_DATASET_MANAGEMENT; 1692 cmd->nsid = htole32(nsid); 1693 cmd->cdw10 = htole32(num_ranges - 1); 1694 cmd->cdw11 = htole32(NVME_DSM_ATTR_DEALLOCATE); 1695 } 1696 1697 extern int nvme_use_nvd; 1698 1699 #endif /* _KERNEL */ 1700 1701 /* Endianess conversion functions for NVMe structs */ 1702 static inline 1703 void nvme_completion_swapbytes(struct nvme_completion *s) 1704 { 1705 1706 s->cdw0 = le32toh(s->cdw0); 1707 /* omit rsvd1 */ 1708 s->sqhd = le16toh(s->sqhd); 1709 s->sqid = le16toh(s->sqid); 1710 /* omit cid */ 1711 s->status = le16toh(s->status); 1712 } 1713 1714 static inline 1715 void nvme_power_state_swapbytes(struct nvme_power_state *s) 1716 { 1717 1718 s->mp = le16toh(s->mp); 1719 s->enlat = le32toh(s->enlat); 1720 s->exlat = le32toh(s->exlat); 1721 s->idlp = le16toh(s->idlp); 1722 s->actp = le16toh(s->actp); 1723 } 1724 1725 static inline 1726 void nvme_controller_data_swapbytes(struct nvme_controller_data *s) 1727 { 1728 int i; 1729 1730 s->vid = le16toh(s->vid); 1731 s->ssvid = le16toh(s->ssvid); 1732 s->ctrlr_id = le16toh(s->ctrlr_id); 1733 s->ver = le32toh(s->ver); 1734 s->rtd3r = le32toh(s->rtd3r); 1735 s->rtd3e = le32toh(s->rtd3e); 1736 s->oaes = le32toh(s->oaes); 1737 s->ctratt = le32toh(s->ctratt); 1738 s->rrls = le16toh(s->rrls); 1739 s->crdt1 = le16toh(s->crdt1); 1740 s->crdt2 = le16toh(s->crdt2); 1741 s->crdt3 = le16toh(s->crdt3); 1742 s->oacs = le16toh(s->oacs); 1743 s->wctemp = le16toh(s->wctemp); 1744 s->cctemp = le16toh(s->cctemp); 1745 s->mtfa = le16toh(s->mtfa); 1746 s->hmpre = le32toh(s->hmpre); 1747 s->hmmin = le32toh(s->hmmin); 1748 s->rpmbs = le32toh(s->rpmbs); 1749 s->edstt = le16toh(s->edstt); 1750 s->kas = le16toh(s->kas); 1751 s->hctma = le16toh(s->hctma); 1752 s->mntmt = le16toh(s->mntmt); 1753 s->mxtmt = le16toh(s->mxtmt); 1754 s->sanicap = le32toh(s->sanicap); 1755 s->hmminds = le32toh(s->hmminds); 1756 s->hmmaxd = le16toh(s->hmmaxd); 1757 s->nsetidmax = le16toh(s->nsetidmax); 1758 s->endgidmax = le16toh(s->endgidmax); 1759 s->anagrpmax = le32toh(s->anagrpmax); 1760 s->nanagrpid = le32toh(s->nanagrpid); 1761 s->pels = le32toh(s->pels); 1762 s->maxcmd = le16toh(s->maxcmd); 1763 s->nn = le32toh(s->nn); 1764 s->oncs = le16toh(s->oncs); 1765 s->fuses = le16toh(s->fuses); 1766 s->awun = le16toh(s->awun); 1767 s->awupf = le16toh(s->awupf); 1768 s->acwu = le16toh(s->acwu); 1769 s->sgls = le32toh(s->sgls); 1770 s->mnan = le32toh(s->mnan); 1771 for (i = 0; i < 32; i++) 1772 nvme_power_state_swapbytes(&s->power_state[i]); 1773 } 1774 1775 static inline 1776 void nvme_namespace_data_swapbytes(struct nvme_namespace_data *s) 1777 { 1778 int i; 1779 1780 s->nsze = le64toh(s->nsze); 1781 s->ncap = le64toh(s->ncap); 1782 s->nuse = le64toh(s->nuse); 1783 s->nawun = le16toh(s->nawun); 1784 s->nawupf = le16toh(s->nawupf); 1785 s->nacwu = le16toh(s->nacwu); 1786 s->nabsn = le16toh(s->nabsn); 1787 s->nabo = le16toh(s->nabo); 1788 s->nabspf = le16toh(s->nabspf); 1789 s->noiob = le16toh(s->noiob); 1790 s->npwg = le16toh(s->npwg); 1791 s->npwa = le16toh(s->npwa); 1792 s->npdg = le16toh(s->npdg); 1793 s->npda = le16toh(s->npda); 1794 s->nows = le16toh(s->nows); 1795 s->anagrpid = le32toh(s->anagrpid); 1796 s->nvmsetid = le16toh(s->nvmsetid); 1797 s->endgid = le16toh(s->endgid); 1798 for (i = 0; i < 16; i++) 1799 s->lbaf[i] = le32toh(s->lbaf[i]); 1800 } 1801 1802 static inline 1803 void nvme_error_information_entry_swapbytes(struct nvme_error_information_entry *s) 1804 { 1805 1806 s->error_count = le64toh(s->error_count); 1807 s->sqid = le16toh(s->sqid); 1808 s->cid = le16toh(s->cid); 1809 s->status = le16toh(s->status); 1810 s->error_location = le16toh(s->error_location); 1811 s->lba = le64toh(s->lba); 1812 s->nsid = le32toh(s->nsid); 1813 s->csi = le64toh(s->csi); 1814 s->ttsi = le16toh(s->ttsi); 1815 } 1816 1817 static inline 1818 void nvme_le128toh(void *p) 1819 { 1820 #if _BYTE_ORDER != _LITTLE_ENDIAN 1821 /* Swap 16 bytes in place */ 1822 char *tmp = (char*)p; 1823 char b; 1824 int i; 1825 for (i = 0; i < 8; i++) { 1826 b = tmp[i]; 1827 tmp[i] = tmp[15-i]; 1828 tmp[15-i] = b; 1829 } 1830 #else 1831 (void)p; 1832 #endif 1833 } 1834 1835 static inline 1836 void nvme_health_information_page_swapbytes(struct nvme_health_information_page *s) 1837 { 1838 int i; 1839 1840 s->temperature = le16toh(s->temperature); 1841 nvme_le128toh((void *)s->data_units_read); 1842 nvme_le128toh((void *)s->data_units_written); 1843 nvme_le128toh((void *)s->host_read_commands); 1844 nvme_le128toh((void *)s->host_write_commands); 1845 nvme_le128toh((void *)s->controller_busy_time); 1846 nvme_le128toh((void *)s->power_cycles); 1847 nvme_le128toh((void *)s->power_on_hours); 1848 nvme_le128toh((void *)s->unsafe_shutdowns); 1849 nvme_le128toh((void *)s->media_errors); 1850 nvme_le128toh((void *)s->num_error_info_log_entries); 1851 s->warning_temp_time = le32toh(s->warning_temp_time); 1852 s->error_temp_time = le32toh(s->error_temp_time); 1853 for (i = 0; i < 8; i++) 1854 s->temp_sensor[i] = le16toh(s->temp_sensor[i]); 1855 s->tmt1tc = le32toh(s->tmt1tc); 1856 s->tmt2tc = le32toh(s->tmt2tc); 1857 s->ttftmt1 = le32toh(s->ttftmt1); 1858 s->ttftmt2 = le32toh(s->ttftmt2); 1859 } 1860 1861 static inline 1862 void nvme_firmware_page_swapbytes(struct nvme_firmware_page *s) 1863 { 1864 int i; 1865 1866 for (i = 0; i < 7; i++) 1867 s->revision[i] = le64toh(s->revision[i]); 1868 } 1869 1870 static inline 1871 void nvme_ns_list_swapbytes(struct nvme_ns_list *s) 1872 { 1873 int i; 1874 1875 for (i = 0; i < 1024; i++) 1876 s->ns[i] = le32toh(s->ns[i]); 1877 } 1878 1879 static inline 1880 void nvme_command_effects_page_swapbytes(struct nvme_command_effects_page *s) 1881 { 1882 int i; 1883 1884 for (i = 0; i < 256; i++) 1885 s->acs[i] = le32toh(s->acs[i]); 1886 for (i = 0; i < 256; i++) 1887 s->iocs[i] = le32toh(s->iocs[i]); 1888 } 1889 1890 static inline 1891 void nvme_res_notification_page_swapbytes(struct nvme_res_notification_page *s) 1892 { 1893 s->log_page_count = le64toh(s->log_page_count); 1894 s->nsid = le32toh(s->nsid); 1895 } 1896 1897 static inline 1898 void nvme_sanitize_status_page_swapbytes(struct nvme_sanitize_status_page *s) 1899 { 1900 s->sprog = le16toh(s->sprog); 1901 s->sstat = le16toh(s->sstat); 1902 s->scdw10 = le32toh(s->scdw10); 1903 s->etfo = le32toh(s->etfo); 1904 s->etfbe = le32toh(s->etfbe); 1905 s->etfce = le32toh(s->etfce); 1906 s->etfownd = le32toh(s->etfownd); 1907 s->etfbewnd = le32toh(s->etfbewnd); 1908 s->etfcewnd = le32toh(s->etfcewnd); 1909 } 1910 1911 static inline 1912 void intel_log_temp_stats_swapbytes(struct intel_log_temp_stats *s) 1913 { 1914 1915 s->current = le64toh(s->current); 1916 s->overtemp_flag_last = le64toh(s->overtemp_flag_last); 1917 s->overtemp_flag_life = le64toh(s->overtemp_flag_life); 1918 s->max_temp = le64toh(s->max_temp); 1919 s->min_temp = le64toh(s->min_temp); 1920 /* omit _rsvd[] */ 1921 s->max_oper_temp = le64toh(s->max_oper_temp); 1922 s->min_oper_temp = le64toh(s->min_oper_temp); 1923 s->est_offset = le64toh(s->est_offset); 1924 } 1925 1926 static inline 1927 void nvme_resv_status_swapbytes(struct nvme_resv_status *s, size_t size) 1928 { 1929 u_int i, n; 1930 1931 s->gen = le32toh(s->gen); 1932 n = (s->regctl[1] << 8) | s->regctl[0]; 1933 n = MIN(n, (size - sizeof(s)) / sizeof(s->ctrlr[0])); 1934 for (i = 0; i < n; i++) { 1935 s->ctrlr[i].ctrlr_id = le16toh(s->ctrlr[i].ctrlr_id); 1936 s->ctrlr[i].hostid = le64toh(s->ctrlr[i].hostid); 1937 s->ctrlr[i].rkey = le64toh(s->ctrlr[i].rkey); 1938 } 1939 } 1940 1941 static inline 1942 void nvme_resv_status_ext_swapbytes(struct nvme_resv_status_ext *s, size_t size) 1943 { 1944 u_int i, n; 1945 1946 s->gen = le32toh(s->gen); 1947 n = (s->regctl[1] << 8) | s->regctl[0]; 1948 n = MIN(n, (size - sizeof(s)) / sizeof(s->ctrlr[0])); 1949 for (i = 0; i < n; i++) { 1950 s->ctrlr[i].ctrlr_id = le16toh(s->ctrlr[i].ctrlr_id); 1951 s->ctrlr[i].rkey = le64toh(s->ctrlr[i].rkey); 1952 nvme_le128toh((void *)s->ctrlr[i].hostid); 1953 } 1954 } 1955 1956 #endif /* __NVME_H__ */ 1957