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