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