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