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