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