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