xref: /illumos-gate/usr/src/uts/common/sys/nvme.h (revision e8d3efa1c56e5f2b5368600a2baeb7b1d54a07f8)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2016 Nexenta Systems, Inc.
14  * Copyright 2020 Joyent, Inc.
15  * Copyright 2019 Western Digital Corporation
16  * Copyright 2026 Oxide Computer Company
17  * Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
18  */
19 
20 #ifndef _SYS_NVME_H
21 #define	_SYS_NVME_H
22 
23 #include <sys/types.h>
24 #include <sys/debug.h>
25 #include <sys/stddef.h>
26 
27 #ifdef _KERNEL
28 #include <sys/types32.h>
29 #else
30 #include <sys/uuid.h>
31 #include <stdint.h>
32 #endif
33 
34 /*
35  * Declarations used for communication between nvme(4D) and libnvme.
36  */
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 /*
43  * NVMe ioctl definitions
44  */
45 
46 #define	NVME_IOC			(('N' << 24) | ('V' << 16) | ('M' << 8))
47 #define	NVME_IOC_CTRL_INFO		(NVME_IOC | 0)
48 #define	NVME_IOC_IDENTIFY		(NVME_IOC | 1)
49 #define	NVME_IOC_GET_LOGPAGE		(NVME_IOC | 2)
50 #define	NVME_IOC_GET_FEATURE		(NVME_IOC | 3)
51 #define	NVME_IOC_FORMAT			(NVME_IOC | 4)
52 #define	NVME_IOC_BD_DETACH		(NVME_IOC | 5)
53 #define	NVME_IOC_BD_ATTACH		(NVME_IOC | 6)
54 #define	NVME_IOC_FIRMWARE_DOWNLOAD	(NVME_IOC | 7)
55 #define	NVME_IOC_FIRMWARE_COMMIT	(NVME_IOC | 8)
56 #define	NVME_IOC_PASSTHRU		(NVME_IOC | 9)
57 #define	NVME_IOC_NS_INFO		(NVME_IOC | 10)
58 #define	NVME_IOC_LOCK			(NVME_IOC | 11)
59 #define	NVME_IOC_UNLOCK			(NVME_IOC | 12)
60 #define	NVME_IOC_CTRL_ATTACH		(NVME_IOC | 13)
61 #define	NVME_IOC_CTRL_DETACH		(NVME_IOC | 14)
62 #define	NVME_IOC_NS_CREATE		(NVME_IOC | 15)
63 #define	NVME_IOC_NS_DELETE		(NVME_IOC | 16)
64 #define	NVME_IOC_SET_FEATURE		(NVME_IOC | 17)
65 #define	NVME_IOC_MAX			NVME_IOC_SET_FEATURE
66 
67 #define	IS_NVME_IOC(x)			((x) > NVME_IOC && (x) <= NVME_IOC_MAX)
68 #define	NVME_IOC_CMD(x)			((x) & 0xff)
69 
70 /*
71  * This represents the set of all possible errors that can be returned from an
72  * ioctl. Our general rule of thumb is that we only will use an errno value to
73  * indicate that certain processing failed: a lack of privileges, bad minor, or
74  * failure to copy in and out the initial ioctl structure. However, if we get
75  * far enough that there is any other failure (including a failure to copy in
76  * and out nested data such as the identify command payload) then we will issue
77  * an error here. Put differently, our basic promise is that there should be a
78  * single straightforward meaning for any errno returned and instead all the
79  * nuance is here. Our goal is that no one should guess what of two dozen things
80  * an EINVAL might have referred to.
81  *
82  * When we are dealing with field parameters, there are three general classes of
83  * errors that we define that are common across all request structures:
84  *
85  *   <REQ>_<FIELD>_RANGE	RANGE class errors indicate that the value
86  *				passed in is outside the range that the device
87  *				supports. The range may vary based on the
88  *				specification. This is used both for issues like
89  *				bad alignment in a value (e.g. not 4-byte
90  *				aligned) or a value that is larger than the
91  *				maximum possible size. Because the namespace ID
92  *				is shared in every request in the controller and
93  *				is part of our standard ioctl handling, we use a
94  *				single set of errors for that.
95  *
96  *   <REQ>_<FIELD>_UNSUP	This indicates that the controller cannot
97  *				support any value in the given field. This is
98  *				either because the field was introduced in an
99  *				NVMe specification later than the controller
100  *				supports or because there is an explicit feature
101  *				bit that indicates whether or not this field is
102  *				valid. Entries here may or may not have a
103  *				namespace unsupported entry due to the fact that
104  *				this is command specific.
105  *
106  *  <REQ>_<FIELD>_UNUSE		This class is perhaps the weirdest. This
107  *				represents a case where a given field cannot be
108  *				set because it is not used based on the
109  *				specifics of the request. For example, if you're
110  *				getting the health log page, you may not set the
111  *				LSP or LSI for that log page, even if you have
112  *				an NVMe 1.4 controller that supports both fields
113  *				because they have no meaning. A similar example
114  *				would be setting a controller ID when it has no
115  *				meaning in a particular identify request.
116  *
117  * While every field will have a RANGE class error, some fields will not have an
118  * UNSUP or UNUSE class error depending on the specifics. A field that has
119  * always been present since NVMe 1.0 and is always valid, such as say the log
120  * page ID field for a get log page request or the length of a firmware download
121  * request, currently are always valid. It is possible that future revisions to
122  * the specification or our logic may change this.
123  */
124 typedef enum {
125 	/*
126 	 * Indicates that the command actually completed successfully.
127 	 */
128 	NVME_IOCTL_E_OK	= 0,
129 	/*
130 	 * Indicates that the controller failed the command and the controller
131 	 * specific (SC/SCT) are available. For all other errors, those fields
132 	 * are reserved.
133 	 */
134 	NVME_IOCTL_E_CTRL_ERROR,
135 	/*
136 	 * Indicates that the controller is considered "dead" by the system and
137 	 * therefore is unusable. Separately, the controller may have been
138 	 * removed from the system due to hotplug or related. In that case, the
139 	 * gone variant is used to distinguish this.
140 	 */
141 	NVME_IOCTL_E_CTRL_DEAD,
142 	NVME_IOCTL_E_CTRL_GONE,
143 	/*
144 	 * Indicates that a bad namespace was requested. This would generally
145 	 * happen when referring to a namespace that is outside of controller's
146 	 * range.
147 	 */
148 	NVME_IOCTL_E_NS_RANGE,
149 	/*
150 	 * Indicates that a namespace is not usable in this context.
151 	 */
152 	NVME_IOCTL_E_NS_UNUSE,
153 	/*
154 	 * Indicates that the requested namespace could not be used because we
155 	 * are operating on a namespace minor and asked to operate on a
156 	 * different namespace.
157 	 */
158 	NVME_IOCTL_E_MINOR_WRONG_NS,
159 	/*
160 	 * Indicates that the requested ioctl can only operate on the controller
161 	 * minor and we were on a namespace minor. This is not used for when a
162 	 * namespace is incorrectly requested otherwise.
163 	 */
164 	NVME_IOCTL_E_NOT_CTRL,
165 	/*
166 	 * Indicates that we were asked to operate on the broadcast namespace
167 	 * either because it was specified or that was how the request was
168 	 * transformed and the broadcast namespace is not supported for this
169 	 * operation.
170 	 */
171 	NVME_IOCTL_E_NO_BCAST_NS,
172 	/*
173 	 * Indicates that the operation failed because the operation requires a
174 	 * controller or namespace write lock and the caller did not have it.
175 	 */
176 	NVME_IOCTL_E_NEED_CTRL_WRLOCK,
177 	NVME_IOCTL_E_NEED_NS_WRLOCK,
178 	/*
179 	 * Indicates that the operation could not proceed because someone else
180 	 * has exclusive access currently to the controller or namespace and
181 	 * therefore this request (which does not require exclusive access)
182 	 * could not proceed.
183 	 */
184 	NVME_IOCTL_E_CTRL_LOCKED,
185 	NVME_IOCTL_E_NS_LOCKED,
186 	/*
187 	 * Indicates that a standard log page was requested that the kernel
188 	 * doesn't know about.
189 	 */
190 	NVME_IOCTL_E_UNKNOWN_LOG_PAGE,
191 	/*
192 	 * Indicates that the controller does not support the requested log
193 	 * page; however, the kernel knows about it.
194 	 */
195 	NVME_IOCTL_E_UNSUP_LOG_PAGE,
196 	/*
197 	 * Indicates that the log page's scope requires operating on something
198 	 * that isn't what was requested. For example, trying to request the
199 	 * firmware information page on a namespace.
200 	 */
201 	NVME_IOCTL_E_BAD_LOG_SCOPE,
202 	/*
203 	 * Log page fields with bad values.
204 	 */
205 	NVME_IOCTL_E_LOG_CSI_RANGE,
206 	NVME_IOCTL_E_LOG_LID_RANGE,
207 	NVME_IOCTL_E_LOG_LSP_RANGE,
208 	NVME_IOCTL_E_LOG_LSI_RANGE,
209 	NVME_IOCTL_E_LOG_RAE_RANGE,
210 	NVME_IOCTL_E_LOG_SIZE_RANGE,
211 	NVME_IOCTL_E_LOG_OFFSET_RANGE,
212 	/*
213 	 * Log page fields that may not be supported.
214 	 */
215 	NVME_IOCTL_E_LOG_CSI_UNSUP,
216 	NVME_IOCTL_E_LOG_LSP_UNSUP,
217 	NVME_IOCTL_E_LOG_LSI_UNSUP,
218 	NVME_IOCTL_E_LOG_RAE_UNSUP,
219 	NVME_IOCTL_E_LOG_OFFSET_UNSUP,
220 	/*
221 	 * Log page fields that may not be usable, depending on context.
222 	 */
223 	NVME_IOCTL_E_LOG_LSP_UNUSE,
224 	NVME_IOCTL_E_LOG_LSI_UNUSE,
225 	NVME_IOCTL_E_LOG_RAE_UNUSE,
226 	/*
227 	 * Indicates that no DMA memory was available for a request.
228 	 */
229 	NVME_IOCTL_E_NO_DMA_MEM,
230 	/*
231 	 * Indicates that there was no kernel memory avilable for the request.
232 	 */
233 	NVME_IOCTL_E_NO_KERN_MEM,
234 	/*
235 	 * Indicates that an error occurred while trying to fill out the DMA PRP
236 	 */
237 	NVME_IOCTL_E_BAD_PRP,
238 	/*
239 	 * Indicates that a pointer to user data to read from or write to was
240 	 * not valid and generated a fault. Specifically this is for items that
241 	 * an ioctl structure points to.
242 	 */
243 	NVME_IOCTL_E_BAD_USER_DATA,
244 	/*
245 	 * Indicates that the kernel does not know about the requested identify
246 	 * command.
247 	 */
248 	NVME_IOCTL_E_UNKNOWN_IDENTIFY,
249 	/*
250 	 * Indicates that the controller does not support the requested identify
251 	 * command.
252 	 */
253 	NVME_IOCTL_E_UNSUP_IDENTIFY,
254 	/*
255 	 * The following errors indicate either a bad value for a given identify
256 	 * argument. This would happen because the value is outside the
257 	 * supported range. There is no CNS or below as those are the
258 	 * higher-level errors right above this.
259 	 */
260 	NVME_IOCTL_E_IDENTIFY_CTRLID_RANGE,
261 	/*
262 	 * Next, we have the unsupported and unusable pieces. The nsid was
263 	 * supported starting in NVMe 1.0, therefore it is never unsupported.
264 	 * However, the controller ID both requires controller support and is
265 	 * not usable in several requests.
266 	 */
267 	NVME_IOCTL_E_IDENTIFY_CTRLID_UNSUP,
268 	NVME_IOCTL_E_IDENTIFY_CTRLID_UNUSE,
269 	/*
270 	 * Indicates that the controller does not support the NVMe spec's
271 	 * general vendor unique command format.
272 	 */
273 	NVME_IOCTL_E_CTRL_VUC_UNSUP,
274 	/*
275 	 * The following indicate bad values for given NVMe vendor unique
276 	 * command fields. All of the cdw1[2-5] fields are not part of this
277 	 * because there is nothing that we can validate.
278 	 */
279 	NVME_IOCTL_E_VUC_TIMEOUT_RANGE,
280 	NVME_IOCTL_E_VUC_OPCODE_RANGE,
281 	NVME_IOCTL_E_VUC_FLAGS_RANGE,
282 	NVME_IOCTL_E_VUC_IMPACT_RANGE,
283 	NVME_IOCTL_E_VUC_NDT_RANGE,
284 	/*
285 	 * These indicate that the VUC data and that the corresponding pair of
286 	 * fields do not agree with each other.
287 	 */
288 	NVME_IOCTL_E_INCONSIST_VUC_FLAGS_NDT,
289 	NVME_IOCTL_E_INCONSIST_VUC_BUF_NDT,
290 	/*
291 	 * Indicates that the operation in question did not succeed because
292 	 * blkdev failed to detach. Most often this happens because the device
293 	 * node is busy. Reasons the device node could be busy include that the
294 	 * device is in a zpool, a file system is mounted, a process has the
295 	 * block device open, etc.
296 	 */
297 	NVME_IOCTL_E_BLKDEV_DETACH,
298 	/*
299 	 * Indicates that the operation in question failed because we were
300 	 * unable to create and online a new blkdev child.
301 	 */
302 	NVME_IOCTL_E_BLKDEV_ATTACH,
303 	/*
304 	 * Indicates that the namespace requested for an attach is not supported
305 	 * by the system. This would happen due to properties of the namespace
306 	 * itself (e.g. utilizing metadata sectors).
307 	 */
308 	NVME_IOCTL_E_UNSUP_ATTACH_NS,
309 	/*
310 	 * Indicates that the format operation is not supported by the
311 	 * controller at all.
312 	 */
313 	NVME_IOCTL_E_CTRL_FORMAT_UNSUP,
314 	/*
315 	 * Indicates that the controller does not support the ability to perform
316 	 * a cryptographic secure erase.
317 	 */
318 	NVME_IOCTL_E_CTRL_CRYPTO_SE_UNSUP,
319 	/*
320 	 * Indicates that a format operation is targeting a namespace, but
321 	 * cannot be performed because it does not support formatting an
322 	 * individual namespace or performing a secure-erase of an individual
323 	 * namespace respectively.
324 	 */
325 	NVME_IOCTL_E_CTRL_NS_FORMAT_UNSUP,
326 	NVME_IOCTL_E_CTRL_NS_SE_UNSUP,
327 	/*
328 	 * The following indicate bad values for a format NVM request.
329 	 */
330 	NVME_IOCTL_E_FORMAT_LBAF_RANGE,
331 	NVME_IOCTL_E_FORMAT_SES_RANGE,
332 	/*
333 	 * Indicates that the requested LBA format is not supported due to its
334 	 * use of metadata.
335 	 */
336 	NVME_IOCTL_E_UNSUP_LBAF_META,
337 	/*
338 	 * Indicates that the firmware commands are not supported by the
339 	 * controller at all.
340 	 */
341 	NVME_IOCTL_E_CTRL_FW_UNSUP,
342 	/*
343 	 * Indicates that the controller has reported a firmware update
344 	 * granularity that exceeds the calculated / driver supported maximum
345 	 * DMA transfer size. As such we cannot perform this operation.
346 	 */
347 	NVME_IOCTL_E_FW_LOAD_IMPOS_GRAN,
348 	/*
349 	 * The following indicate bad values for a firmware load's length and
350 	 * offset.
351 	 */
352 	NVME_IOCTL_E_FW_LOAD_LEN_RANGE,
353 	NVME_IOCTL_E_FW_LOAD_OFFSET_RANGE,
354 	/*
355 	 * The following indicate bad values for a firmware commit's slot and
356 	 * action.
357 	 */
358 	NVME_IOCTL_E_FW_COMMIT_SLOT_RANGE,
359 	NVME_IOCTL_E_FW_COMMIT_ACTION_RANGE,
360 	/*
361 	 * Indicates that an explicit attempt was made to download an image into
362 	 * a read-only slot. Note, some instances of this cannot be caught prior
363 	 * to issuing a command to the controller (commit action 0b11 as it can
364 	 * be used whether there is or isn't a staged image) and will result in
365 	 * a controller error.
366 	 */
367 	NVME_IOCTL_E_RO_FW_SLOT,
368 	/*
369 	 * Indicates that the kernel doesn't know about the NVMe feature in
370 	 * question and therefore cannot proceed.
371 	 */
372 	NVME_IOCTL_E_UNKNOWN_FEATURE,
373 	/*
374 	 * Indicates that while the system knows about the feature in question,
375 	 * it is not supported by the controller.
376 	 */
377 	NVME_IOCTL_E_UNSUP_FEATURE,
378 	/*
379 	 * The following errors indicate a bad value for a given get feature
380 	 * field. This would happen because the value is outside the supported
381 	 * range.
382 	 */
383 	NVME_IOCTL_E_GET_FEAT_SEL_RANGE,
384 	NVME_IOCTL_E_GET_FEAT_CDW11_RANGE,
385 	NVME_IOCTL_E_GET_FEAT_DATA_RANGE,
386 	/*
387 	 * This set of errors indicate that the field is not supported. This can
388 	 * happen because a given get feature command doesn't support setting
389 	 * this value, the field isn't supported in this revision of the
390 	 * controller, or similar issues.
391 	 */
392 	NVME_IOCTL_E_GET_FEAT_SEL_UNSUP,
393 	/*
394 	 * Fields that may be circumstantially unusable.
395 	 */
396 	NVME_IOCTL_E_GET_FEAT_CDW11_UNUSE,
397 	NVME_IOCTL_E_GET_FEAT_DATA_UNUSE,
398 	/*
399 	 * The following errors indicate a bad lock type.
400 	 */
401 	NVME_IOCTL_E_BAD_LOCK_ENTITY,
402 	NVME_IOCTL_E_BAD_LOCK_LEVEL,
403 	NVME_IOCTL_E_BAD_LOCK_FLAGS,
404 	/*
405 	 * Indicates that a namespace open cannot lock or unlock a controller.
406 	 */
407 	NVME_IOCTL_E_NS_CANNOT_LOCK_CTRL,
408 	NVME_IOCTL_E_NS_CANNOT_UNLOCK_CTRL,
409 	/*
410 	 * Indicates that this lock is already held by the caller.
411 	 */
412 	NVME_IOCTL_E_LOCK_ALREADY_HELD,
413 	/*
414 	 * Indicates that we cannot take the controller lock, because the
415 	 * caller already has an active namespace lock.
416 	 */
417 	NVME_IOCTL_E_LOCK_NO_CTRL_WITH_NS,
418 	/*
419 	 * Indicates that we cannot take a namespace lock because a controller
420 	 * write lock already exists.
421 	 */
422 	NVME_IOCTL_LOCK_NO_NS_WITH_CTRL_WRLOCK,
423 	/*
424 	 * Indicates that we cannot take a namespace lock because we already
425 	 * have one.
426 	 */
427 	NVME_IOCTL_E_LOCK_NO_2ND_NS,
428 	/*
429 	 * Indicate that a blocking wait for a lock was interrupted due to a
430 	 * signal.
431 	 */
432 	NVME_IOCTL_E_LOCK_WAIT_SIGNAL,
433 	/*
434 	 * Indicates that the lock could not be acquired because it was already
435 	 * held and we were asked not to block on the lock.
436 	 */
437 	NVME_IOCTL_E_LOCK_WOULD_BLOCK,
438 	/*
439 	 * Indicates that the lock operation could not proceed because the minor
440 	 * is already blocking on another lock operation.
441 	 */
442 	NVME_IOCTL_E_LOCK_PENDING,
443 	/*
444 	 * Indicates that the requested lock could not be unlocked because it is
445 	 * not held. The minor may not hold the lock or it may be blocking for
446 	 * acquisition.
447 	 */
448 	NVME_IOCTL_E_LOCK_NOT_HELD,
449 	/*
450 	 * Indicates that the requested lock could not be unlocked because the
451 	 * namespace requested is not the namespace that is currently locked.
452 	 */
453 	NVME_IOCTL_E_LOCK_WRONG_NS,
454 	/*
455 	 * Indicates that the request could not proceed because a namespace is
456 	 * attached to blkdev. This would block a format operation, a vendor
457 	 * unique command that indicated that it would impact all namespaces,
458 	 * etc.
459 	 */
460 	NVME_IOCTL_E_NS_BLKDEV_ATTACH,
461 	/*
462 	 * Indicates that the blkdev address somehow would have overflowed our
463 	 * internal buffer.
464 	 */
465 	NVME_IOCTL_E_BD_ADDR_OVER,
466 	/*
467 	 * Indicates that Namespace Management commands are not supported by the
468 	 * controller at all.
469 	 */
470 	NVME_IOCTL_E_CTRL_NS_MGMT_UNSUP,
471 	/*
472 	 * Indicates that the request could not proceed because the namespace is
473 	 * currently attached to a controller.
474 	 */
475 	NVME_IOCTL_E_NS_CTRL_ATTACHED,
476 	NVME_IOCTL_E_NS_CTRL_NOT_ATTACHED,
477 	/*
478 	 * This indicates that the namespace ID is valid; however, there is no
479 	 * namespace actually allocated for this ID. For example, when trying to
480 	 * attach or detach a controller to an unallocated namespace.
481 	 *
482 	 * When a namespace ID is invalid, the kernel will generally instead
483 	 * return NVME_IOCTL_E_NS_RANGE.
484 	 */
485 	NVME_IOCTL_E_NS_NO_NS,
486 	/*
487 	 * Namespace Create fields with bad values
488 	 */
489 	NVME_IOCTL_E_NS_CREATE_NSZE_RANGE,
490 	NVME_IOCTL_E_NS_CREATE_NCAP_RANGE,
491 	NVME_IOCTL_E_NS_CREATE_CSI_RANGE,
492 	NVME_IOCTL_E_NS_CREATE_FLBAS_RANGE,
493 	NVME_IOCTL_E_NS_CREATE_NMIC_RANGE,
494 	/*
495 	 * Namespace Create fields with unsupported versions. Currently this can
496 	 * only apply to the CSI. Note, there aren't unusable errors yet;
497 	 * however, that'll change when we support other CSI types.
498 	 */
499 	NVME_IOCTL_E_NS_CREATE_CSI_UNSUP,
500 	/*
501 	 * We may have a valid CSI, but not support it at our end. This error
502 	 * indicates that. Similarly, the device may not support thin
503 	 * provisioning.
504 	 */
505 	NVME_IOCTL_E_DRV_CSI_UNSUP,
506 	NVME_IOCTL_E_CTRL_THIN_PROV_UNSUP,
507 	/*
508 	 * Indicates that while this feature is known to the system, setting it
509 	 * is not supported from userland.
510 	 */
511 	NVME_IOCTL_E_SET_FEAT_USER_UNSUP,
512 	/*
513 	 * The following errors indicate a bad value for a given set feature
514 	 * field. This would happen because the value is outside the supported
515 	 * range.
516 	 */
517 	NVME_IOCTL_E_SET_FEAT_SAVE_RANGE,
518 	NVME_IOCTL_E_SET_FEAT_CDW11_RANGE,
519 	NVME_IOCTL_E_SET_FEAT_CDW12_RANGE,
520 	NVME_IOCTL_E_SET_FEAT_CDW13_RANGE,
521 	NVME_IOCTL_E_SET_FEAT_CDW15_RANGE,
522 	NVME_IOCTL_E_SET_FEAT_DATA_RANGE,
523 	NVME_IOCTL_E_SET_FEAT_IMPACT_RANGE,
524 	/*
525 	 * The following set of errors indicate that the field is not supported.
526 	 * This generally happens because the controller is older than the
527 	 * version of the specification that they were introduced in.
528 	 */
529 	NVME_IOCTL_E_SET_FEAT_SAVE_UNSUP,
530 	NVME_IOCTL_E_SET_FEAT_CDW12_UNSUP,
531 	NVME_IOCTL_E_SET_FEAT_CDW13_UNSUP,
532 	NVME_IOCTL_E_SET_FEAT_CDW15_UNSUP,
533 	/*
534 	 * Fields that may be unusable in certain circumstances.
535 	 */
536 	NVME_IOCTL_E_SET_FEAT_CDW11_UNUSE,
537 	NVME_IOCTL_E_SET_FEAT_CDW12_UNUSE,
538 	NVME_IOCTL_E_SET_FEAT_CDW13_UNUSE,
539 	NVME_IOCTL_E_SET_FEAT_CDW15_UNUSE,
540 	NVME_IOCTL_E_SET_FEAT_DATA_UNUSE
541 } nvme_ioctl_errno_t;
542 
543 /*
544  * This structure is embedded as the first item of every ioctl. It is also used
545  * directly for the following ioctls:
546  *
547  *  - blkdev attach (NVME_IOC_ATTACH)
548  *  - blkdev detach (NVME_IOC_DETACH)
549  *  - controller attach (NVME_IOC_CTRL_ATTACH)
550  *  - controller detach (NVME_IOC_CTRL_DETACH)
551  *  - namespace delete (NVME_IOC_NS_DELETE)
552  */
553 typedef struct {
554 	/*
555 	 * This allows one to specify the namespace ID that the ioctl may
556 	 * target, if it supports it. This field may be left to zero to indicate
557 	 * that the current open device (whether the controller or a namespace)
558 	 * should be targeted. If a namespace is open, a value other than 0 or
559 	 * the current namespace's ID is invalid.
560 	 */
561 	uint32_t nioc_nsid;
562 	/*
563 	 * These next three values represent a possible error that may have
564 	 * occurred. On every ioctl nioc_drv_err is set to a value from the
565 	 * nvme_ioctl_errno_t enumeration. Anything other than NVME_IOCTL_E_OK
566 	 * indicates a failure of some kind. Some error values will put
567 	 * supplemental information in sct and sc. For example,
568 	 * NVME_IOCTL_E_CTRL_ERROR uses that as a way to return the raw error
569 	 * values from the controller for someone to inspect. Others may use
570 	 * this for their own well-defined supplemental information.
571 	 */
572 	uint32_t nioc_drv_err;
573 	uint32_t nioc_ctrl_sct;
574 	uint32_t nioc_ctrl_sc;
575 } nvme_ioctl_common_t;
576 
577 /*
578  * NVMe Identify Command (NVME_IOC_IDENTIFY).
579  */
580 typedef struct {
581 	nvme_ioctl_common_t nid_common;
582 	uint32_t nid_cns;
583 	uint32_t nid_ctrlid;
584 	uintptr_t nid_data;
585 } nvme_ioctl_identify_t;
586 
587 /*
588  * The following constants describe the maximum values that may be used in
589  * various identify requests.
590  */
591 #define	NVME_IDENTIFY_MAX_CTRLID	0xffff
592 #define	NVME_IDENTIFY_MAX_NSID		0xffffffff
593 #define	NVME_IDENTIFY_MAX_CNS_1v2	0xff
594 #define	NVME_IDENTIFY_MAX_CNS_1v1	0x3
595 #define	NVME_IDENTIFY_MAX_CNS		0x1
596 
597 /*
598  * Get a specific feature (NVME_IOC_GET_FEATURE).
599  */
600 typedef struct {
601 	nvme_ioctl_common_t nigf_common;
602 	uint32_t nigf_fid;
603 	uint32_t nigf_sel;
604 	uint32_t nigf_cdw11;
605 	uintptr_t nigf_data;
606 	uint64_t nigf_len;
607 	uint32_t nigf_cdw0;
608 } nvme_ioctl_get_feature_t;
609 
610 /*
611  * Set a specific feature (NVME_IOC_SET_FEATURE).
612  */
613 typedef struct {
614 	nvme_ioctl_common_t nisf_common;
615 	uint32_t nisf_fid;
616 	uint32_t nisf_save;
617 	uint32_t nisf_cdw11;
618 	uint32_t nisf_cdw12;
619 	uint32_t nisf_cdw13;
620 	uint32_t nisf_cdw15;
621 	uint32_t nisf_impact;
622 	uintptr_t nisf_data;
623 	uint64_t nisf_len;
624 	uint32_t nisf_cdw0;
625 } nvme_ioctl_set_feature_t;
626 
627 /*
628  * Feature maximums.
629  */
630 #define	NVME_FEAT_MAX_FID	0xff
631 #define	NVME_FEAT_MAX_SEL	0x3
632 #define	NVME_FEAT_MAX_SAVE	0x1
633 
634 /*
635  * Get a specific log page (NVME_IOC_GET_LOGPAGE). By default, unused fields
636  * should be left at zero. The input data length is specified by nigl_len, in
637  * bytes. The NVMe specification does not provide a way for a controller to
638  * write less bytes than requested for a log page. It is undefined behavior if a
639  * log page read requests more data than is supported. If this is successful,
640  * nigl_len bytes will be copied out.
641  */
642 typedef struct {
643 	nvme_ioctl_common_t nigl_common;
644 	uint32_t nigl_csi;
645 	uint32_t nigl_lid;
646 	uint32_t nigl_lsp;
647 	uint32_t nigl_lsi;
648 	uint32_t nigl_rae;
649 	uint64_t nigl_len;
650 	uint64_t nigl_offset;
651 	uintptr_t nigl_data;
652 } nvme_ioctl_get_logpage_t;
653 
654 /*
655  * The following constants describe the maximum values for fields that used in
656  * the log page request. Note, some of these change with the version. These
657  * values are inclusive. The default max is the lowest common value. Larger
658  * values are included here. While these values are what the command set
659  * maximums are, the device driver may support smaller minimums (e.g. for size).
660  */
661 #define	NVME_LOG_MAX_LID	0xff
662 #define	NVME_LOG_MAX_LSP	0x0f
663 #define	NVME_LOG_MAX_LSP_2v0	0x7f
664 #define	NVME_LOG_MAX_LSI	0xffff
665 #define	NVME_LOG_MAX_UUID	0x7f
666 #define	NVME_LOG_MAX_CSI	0xff
667 #define	NVME_LOG_MAX_RAE	0x1
668 #define	NVME_LOG_MAX_OFFSET	UINT64_MAX
669 
670 /*
671  * These maximum size values are inclusive like the others. The fields are 12
672  * and 32-bits wide respectively, but are zero based. That is accounted for by
673  * the shifts below.
674  */
675 #define	NVME_LOG_MAX_SIZE	((1ULL << 12ULL) * 4ULL)
676 #define	NVME_LOG_MAX_SIZE_1v2	((1ULL << 32ULL) * 4ULL)
677 
678 /*
679  * Inject a vendor-specific admin command (NVME_IOC_PASSTHRU).
680  */
681 typedef struct {
682 	nvme_ioctl_common_t npc_common;	/* NSID and status */
683 	uint32_t npc_opcode;	/* Command opcode. */
684 	uint32_t npc_timeout;	/* Command timeout, in seconds. */
685 	uint32_t npc_flags;	/* Flags for the command. */
686 	uint32_t npc_impact;	/* Impact information */
687 	uint32_t npc_cdw0;	/* Command-specific result DWord 0 */
688 	uint32_t npc_cdw12;	/* Command-specific DWord 12 */
689 	uint32_t npc_cdw13;	/* Command-specific DWord 13 */
690 	uint32_t npc_cdw14;	/* Command-specific DWord 14 */
691 	uint32_t npc_cdw15;	/* Command-specific DWord 15 */
692 	uint64_t npc_buflen;	/* Size of npc_buf. */
693 	uintptr_t npc_buf;	/* I/O source or destination */
694 } nvme_ioctl_passthru_t;
695 
696 /*
697  * Constants for the passthru admin commands. Because the timeout is a kernel
698  * property, we don't include that here.
699  */
700 #define	NVME_PASSTHRU_MIN_ADMIN_OPC	0xc0
701 #define	NVME_PASSTHRU_MAX_ADMIN_OPC	0xff
702 
703 /* Flags for NVMe passthru commands. */
704 #define	NVME_PASSTHRU_READ	0x1 /* Read from device */
705 #define	NVME_PASSTHRU_WRITE	0x2 /* Write to device */
706 
707 /*
708  * Impact information for NVMe passthru commands. The current impact flags are
709  * defined as follows:
710  *
711  * NVME_IMPACT_NS	This implies that one or all of the namespaces may be
712  *			changed. This command will rescan all namespace after
713  *			this occurs and update our state as a result. However,
714  *			this requires that all such namespaces not be attached
715  *			to blkdev to continue.
716  */
717 #define	NVME_IMPACT_NS		0x01
718 
719 
720 /*
721  * Firmware download (NVME_IOC_FIRMWARE_DOWNLOAD).
722  */
723 typedef struct {
724 	nvme_ioctl_common_t fwl_common;
725 	uintptr_t fwl_buf;
726 	uint64_t fwl_len;
727 	uint64_t fwl_off;
728 } nvme_ioctl_fw_load_t;
729 
730 /*
731  * Firmware commit (NVME_IOC_FIRMWARE_COMMIT). This was previously called
732  * firmware activate in earlier specification revisions.
733  */
734 typedef struct {
735 	nvme_ioctl_common_t fwc_common;
736 	uint32_t fwc_slot;
737 	uint32_t fwc_action;
738 } nvme_ioctl_fw_commit_t;
739 
740 /*
741  * Format NVM command (NVME_IOC_FORMAT)
742  */
743 typedef struct {
744 	nvme_ioctl_common_t nif_common;
745 	uint32_t nif_lbaf;
746 	uint32_t nif_ses;
747 } nvme_ioctl_format_t;
748 
749 typedef enum {
750 	NVME_LOCK_E_CTRL = 1,
751 	NVME_LOCK_E_NS
752 } nvme_lock_ent_t;
753 
754 typedef enum {
755 	NVME_LOCK_L_READ	= 1,
756 	NVME_LOCK_L_WRITE
757 } nvme_lock_level_t;
758 
759 typedef enum {
760 	NVME_LOCK_F_DONT_BLOCK	= 1 << 0
761 } nvme_lock_flags_t;
762 
763 /*
764  * Lock structure (NVME_IOC_LOCK).
765  */
766 typedef struct {
767 	nvme_ioctl_common_t nil_common;
768 	nvme_lock_ent_t nil_ent;
769 	nvme_lock_level_t nil_level;
770 	nvme_lock_flags_t nil_flags;
771 } nvme_ioctl_lock_t;
772 
773 /*
774  * Unlock structure (NVME_IOC_UNLOCK).
775  */
776 typedef struct {
777 	nvme_ioctl_common_t niu_common;
778 	nvme_lock_ent_t niu_ent;
779 } nvme_ioctl_unlock_t;
780 
781 /*
782  * Namespace Management related structures and constants. Note, namespace
783  * controller attach, controller detach, and namespace delete all use the common
784  * ioctl structure at this time.
785  */
786 #define	NVME_NS_ATTACH_CTRL_ATTACH	0
787 #define	NVME_NS_ATTACH_CTRL_DETACH	1
788 
789 /*
790  * Constants related to fields here. These represent the specifications maximum
791  * size, even though there are additional constraints placed on it by the driver
792  * (e.g. we only allow creating a namespace with the NVM CSI).
793  */
794 #define	NVME_NS_MGMT_MAX_CSI	0xff
795 #define	NVME_NS_MGMT_MAX_FLBAS	0xf
796 #define	NVME_NS_MGMT_NMIC_MASK	0x1
797 
798 /*
799  * Logical values for namespace multipath I/O and sharing capabilities (NMIC).
800  */
801 typedef enum {
802 	/*
803 	 * Indicates that no NVMe namespace sharing is permitted between
804 	 * controllers.
805 	 */
806 	NVME_NS_NMIC_T_NONE	= 0,
807 	/*
808 	 * Indicates that namespace sharing is allowed between controllers. This
809 	 * is equivalent to the SHRNS bit being set.
810 	 */
811 	NVME_NS_NMIC_T_SHARED,
812 	/*
813 	 * Indicates that this is a dispersed namespace. A dispersed namespace
814 	 * implies a shared namespace and indicates that DISNS and SHRNS are
815 	 * both set.
816 	 */
817 	NVME_NS_NMIC_T_DISPERSED
818 } nvme_ns_nmic_t;
819 
820 /*
821  * Namespace create structure (NVME_IOC_NS_CREATE).
822  */
823 typedef struct {
824 	nvme_ioctl_common_t nnc_common;
825 	uint64_t nnc_nsze;
826 	uint64_t nnc_ncap;
827 	uint32_t nnc_csi;
828 	uint32_t nnc_flbas;
829 	uint32_t nnc_nmic;
830 	uint32_t nnc_nsid;
831 } nvme_ioctl_ns_create_t;
832 
833 /*
834  * 32-bit ioctl structures. These must be packed to be 4 bytes to get the proper
835  * ILP32 sizing.
836  */
837 #if defined(_KERNEL) && defined(_SYSCALL32)
838 #pragma pack(4)
839 typedef struct {
840 	nvme_ioctl_common_t nid_common;
841 	uint32_t nid_cns;
842 	uint32_t nid_ctrlid;
843 	uintptr32_t nid_data;
844 } nvme_ioctl_identify32_t;
845 
846 typedef struct {
847 	nvme_ioctl_common_t nigf_common;
848 	uint32_t nigf_fid;
849 	uint32_t nigf_sel;
850 	uint32_t nigf_cdw11;
851 	uintptr32_t nigf_data;
852 	uint64_t nigf_len;
853 	uint32_t nigf_cdw0;
854 } nvme_ioctl_get_feature32_t;
855 
856 typedef struct {
857 	nvme_ioctl_common_t nisf_common;
858 	uint32_t nisf_fid;
859 	uint32_t nisf_save;
860 	uint32_t nisf_cdw11;
861 	uint32_t nisf_cdw12;
862 	uint32_t nisf_cdw13;
863 	uint32_t nisf_cdw15;
864 	uint32_t nisf_impact;
865 	uintptr32_t nisf_data;
866 	uint64_t nisf_len;
867 	uint32_t nisf_cdw0;
868 } nvme_ioctl_set_feature32_t;
869 
870 typedef struct {
871 	nvme_ioctl_common_t nigl_common;
872 	uint32_t nigl_csi;
873 	uint32_t nigl_lid;
874 	uint32_t nigl_lsp;
875 	uint32_t nigl_lsi;
876 	uint32_t nigl_rae;
877 	uint64_t nigl_len;
878 	uint64_t nigl_offset;
879 	uintptr32_t nigl_data;
880 } nvme_ioctl_get_logpage32_t;
881 
882 typedef struct {
883 	nvme_ioctl_common_t npc_common;	/* NSID and status */
884 	uint32_t npc_opcode;	/* Command opcode. */
885 	uint32_t npc_timeout;	/* Command timeout, in seconds. */
886 	uint32_t npc_flags;	/* Flags for the command. */
887 	uint32_t npc_impact;	/* Impact information */
888 	uint32_t npc_cdw0;	/* Command-specific result DWord 0 */
889 	uint32_t npc_cdw12;	/* Command-specific DWord 12 */
890 	uint32_t npc_cdw13;	/* Command-specific DWord 13 */
891 	uint32_t npc_cdw14;	/* Command-specific DWord 14 */
892 	uint32_t npc_cdw15;	/* Command-specific DWord 15 */
893 	uint64_t npc_buflen;	/* Size of npc_buf. */
894 	uintptr32_t npc_buf;	/* I/O source or destination */
895 } nvme_ioctl_passthru32_t;
896 
897 typedef struct {
898 	nvme_ioctl_common_t fwl_common;
899 	uintptr32_t fwl_buf;
900 	uint64_t fwl_len;
901 	uint64_t fwl_off;
902 } nvme_ioctl_fw_load32_t;
903 #pragma pack()	/* pack(4) */
904 #endif	/* _KERNEL && _SYSCALL32 */
905 
906 /*
907  * NVMe capabilities. This is a set of fields that come from the controller's
908  * PCIe register space.
909  */
910 typedef struct {
911 	uint32_t cap_mpsmax;		/* Memory Page Size Maximum */
912 	uint32_t cap_mpsmin;		/* Memory Page Size Minimum */
913 } nvme_capabilities_t;
914 
915 /*
916  * NVMe version
917  */
918 typedef struct {
919 	uint16_t v_minor;
920 	uint16_t v_major;
921 } nvme_version_t;
922 
923 #define	NVME_VERSION_ATLEAST(v, maj, min) \
924 	(((v)->v_major) > (maj) || \
925 	((v)->v_major == (maj) && (v)->v_minor >= (min)))
926 
927 #define	NVME_VERSION_HIGHER(v, maj, min) \
928 	(((v)->v_major) > (maj) || \
929 	((v)->v_major == (maj) && (v)->v_minor > (min)))
930 
931 /*
932  * NVMe Namespace related constants. The maximum NSID is determined by the
933  * identify controller data structure.
934  */
935 #define	NVME_NSID_MIN	1
936 #define	NVME_NSID_BCAST	0xffffffff
937 
938 #pragma pack(1)
939 
940 typedef struct {
941 	uint64_t lo;
942 	uint64_t hi;
943 } nvme_uint128_t;
944 
945 /*
946  * NVMe Identify data structures
947  */
948 
949 #define	NVME_IDENTIFY_BUFSIZE	4096	/* buffer size for Identify */
950 
951 /* NVMe Identify parameters (cdw10) */
952 #define	NVME_IDENTIFY_NSID		0x0	/* Identify Namespace */
953 #define	NVME_IDENTIFY_CTRL		0x1	/* Identify Controller */
954 /* Added in NVMe 1.1 */
955 #define	NVME_IDENTIFY_NSID_LIST		0x2	/* List Active Namespaces */
956 /* Added in NVMe 1.3 */
957 #define	NVME_IDENTIFY_NSID_DESC		0x3	/* Namespace ID Descriptors */
958 /* Added in NVMe 1.4 */
959 #define	NVME_IDENTIFY_SET_LIST		0x4	/* NVM Set List */
960 /* Added in NVMe 2.0 */
961 #define	NVME_IDENTIFY_IOCS_NSID		0x5	/* IOCS Identify Namespace */
962 #define	NVME_IDENTIFY_IOCS_CTRL		0x6	/* IOCS Identify Controller */
963 #define	NVME_IDENTIFY_IOCS_NSID_LIST	0x7	/* List IOCS Active NSIDs */
964 #define	NVME_IDENTIFY_IOCI_NSID		0x8	/* IOCS Ind. Namespace */
965 /* Added in NVMe 2.1 */
966 #define	NVME_IDENTIFY_FMT_NSID		0x9	/* CSI/Format Id. Namespace */
967 #define	NVME_IDENTIFY_FMT_IOCS_NSID	0xa	/* Format Ind. Namespace */
968 
969 /* Added in NVMe 1.2 */
970 #define	NVME_IDENTIFY_NSID_ALLOC_LIST	0x10	/* List Allocated NSID */
971 #define	NVME_IDENTIFY_NSID_ALLOC	0x11	/* Identify Allocated NSID */
972 #define	NVME_IDENTIFY_NSID_CTRL_LIST	0x12	/* List Controllers on NSID */
973 #define	NVME_IDENTIFY_CTRL_LIST		0x13	/* Controller List */
974 /* Added in NVMe 1.3 */
975 #define	NVME_IDENTIFY_PRIMARY_CAPS	0x14	/* Primary Controller Caps */
976 #define	NVME_IDENTIFY_SEC_CTRL_LIST	0x15	/* Secondary Controller List */
977 /* Added in NVMe 1.4 */
978 #define	NVME_IDENTIFY_NS_GRAN_LIST	0x16	/* Namespace Granularity */
979 #define	NVME_IDENTIFY_UUID_LIST		0x17	/* UUID List */
980 /* Added in NVMe 2.0 */
981 #define	NVME_IDENTIFY_DOMAIN_LIST	0x18	/* Domain List */
982 #define	NVME_IDENTIFY_ENDGRP_LIST	0x19	/* Endurance Group List */
983 #define	NVME_IDENTIFY_IOCS_ALLOC_LIST	0x1a	/* IOCS Allocated NSID List */
984 #define	NVME_IDENTIFY_IOCS_ALLOC_NSID	0x1b	/* IOCS Allocated Id. NSID */
985 #define	NVME_IDENTIFY_IOCS_DS		0x1c	/* IOCS data structure */
986 /* Added in NVMe 2.1 */
987 #define	NVME_IDENTIFY_UN_NSID_LIST	0x1d	/* Underlying Namespace List */
988 #define	NVME_IDENTIFY_PORTS_LIST	0x1e	/* Ports List */
989 #define	NVME_IDENTIFY_IOCI_ALLOC_NSID	0x1f	/* IOCS Ind. Alloc Namespace */
990 #define	NVME_IDENTIFY_SUP_CTRL_ST_FMTS	0x20	/* Sup. Ctrl. State Formats */
991 /* Added in NVMe 2.4 */
992 #define	NVME_IDENTIFY_UN_CTRL_LIST	0x21	/* Underlying Controller List */
993 #define	NVME_IDENTIFY_EXP_SUBSYS_LIST	0x22	/* Exported NVM Subsys List */
994 
995 
996 /* NVMe Queue Entry Size bitfield */
997 typedef struct {
998 	uint8_t qes_min:4;		/* minimum entry size */
999 	uint8_t qes_max:4;		/* maximum entry size */
1000 } nvme_idctl_qes_t;
1001 
1002 /* NVMe Power State Descriptor */
1003 typedef struct {
1004 	uint16_t psd_mp;		/* Maximum Power */
1005 	uint8_t psd_rsvd16;
1006 	uint8_t psd_mps:1;		/* Max Power Scale (1.1) */
1007 	uint8_t psd_nops:1;		/* Non-Operational State (1.1) */
1008 	uint8_t psd_rsvd26:6;
1009 	uint32_t psd_enlat;		/* Entry Latency */
1010 	uint32_t psd_exlat;		/* Exit Latency */
1011 	uint8_t psd_rrt:5;		/* Relative Read Throughput */
1012 	uint8_t psd_rsvd101:3;
1013 	uint8_t psd_rrl:5;		/* Relative Read Latency */
1014 	uint8_t psd_rsvd109:3;
1015 	uint8_t psd_rwt:5;		/* Relative Write Throughput */
1016 	uint8_t	psd_rsvd117:3;
1017 	uint8_t psd_rwl:5;		/* Relative Write Latency */
1018 	uint8_t psd_rsvd125:3;
1019 	uint16_t psd_idlp;		/* Idle Power (1.2) */
1020 	uint8_t psd_rsvd144:6;
1021 	uint8_t psd_ips:2;		/* Idle Power Scale (1.2) */
1022 	uint8_t psd_rsvd152;
1023 	uint16_t psd_actp;		/* Active Power (1.2) */
1024 	uint8_t psd_apw:3;		/* Active Power Workload (1.2) */
1025 	uint8_t psd_rsvd179:3;
1026 	uint8_t psd_aps:2;		/* Active Power Scale */
1027 	/* NVMe 2.1 additions  */
1028 	uint8_t psd_epfrt;		/* Emergency Power Fail Recovery Time */
1029 	uint8_t psd_fqvt;		/* Forced Quiescence Vault Time */
1030 	uint8_t psd_epfvt;		/* Emergency Power Fail Vault Time */
1031 	uint8_t psd_epfrts:4;		/* EPFRT Scale */
1032 	uint8_t psd_fqvts:4;		/* FQVTS Scale */
1033 	uint8_t psd_epfvts:4;		/* EPFVTS Scale */
1034 	uint8_t psd_rsvd216:4;
1035 	uint8_t psd_mbw;		/* Max Bandwidth (2.3) */
1036 	uint8_t psd_mbws:3;		/* Max Bandwidth Scale (2.3) */
1037 	uint8_t psd_rsvd:5;
1038 	uint8_t psd_rsvd240[2];
1039 } nvme_idctl_psd_t;
1040 
1041 CTASSERT(sizeof (nvme_idctl_psd_t) == 32);
1042 
1043 #define	NVME_SERIAL_SZ	20
1044 #define	NVME_MODEL_SZ	40
1045 #define	NVME_FWVER_SZ	8
1046 
1047 /* NVMe Identify Controller Data Structure */
1048 typedef struct {
1049 	/* Controller Capabilities & Features */
1050 	uint16_t id_vid;		/* PCI vendor ID */
1051 	uint16_t id_ssvid;		/* PCI subsystem vendor ID */
1052 	char id_serial[NVME_SERIAL_SZ];	/* Serial Number */
1053 	char id_model[NVME_MODEL_SZ];	/* Model Number */
1054 	char id_fwrev[NVME_FWVER_SZ];	/* Firmware Revision */
1055 	uint8_t id_rab;			/* Recommended Arbitration Burst */
1056 	uint8_t id_oui[3];		/* vendor IEEE OUI */
1057 	struct {			/* Multi-Interface Capabilities */
1058 		uint8_t m_multi_pci:1;	/* HW has multiple PCIe interfaces */
1059 		uint8_t m_multi_ctrl:1; /* HW has multiple controllers (1.1) */
1060 		uint8_t m_sr_iov:1;	/* Controller is SR-IOV virt fn (1.1) */
1061 		uint8_t m_anar_sup:1;	/* ANA Reporting Supported (1.4) */
1062 		uint8_t m_rsvd:4;
1063 	} id_mic;
1064 	uint8_t	id_mdts;		/* Maximum Data Transfer Size */
1065 	uint16_t id_cntlid;		/* Unique Controller Identifier (1.1) */
1066 	/* Added in NVMe 1.2 */
1067 	uint32_t id_ver;		/* Version (1.2) */
1068 	uint32_t id_rtd3r;		/* RTD3 Resume Latency (1.2) */
1069 	uint32_t id_rtd3e;		/* RTD3 Entry Latency (1.2) */
1070 	struct {
1071 		uint32_t oaes_rsvd0:8;
1072 		uint32_t oaes_nsan:1;	/* Namespace Attribute Notices (1.2) */
1073 		uint32_t oaes_fwact:1;	/* Firmware Activation Notices (1.2) */
1074 		uint32_t oaes_rsvd10:1;
1075 		uint32_t oaes_ansacn:1;	/* Asymmetric NS Access Change (1.4) */
1076 		uint32_t oaes_plat:1;	/* Predictable Lat Event Agg. (1.4) */
1077 		uint32_t oaes_lbasi:1;	/* LBA Status Information (1.4) */
1078 		uint32_t oaes_egeal:1;	/* Endurance Group Event Agg. (1.4) */
1079 		uint32_t oaes_nnss:1;	/* Normal NVM Subsys Shutdown (2.0) */
1080 		uint32_t oaes_tthr:1;	/* Temp. Tresh. Hysteresis Rec (2.1) */
1081 		uint32_t oaes_rgcns:1;	/* Reach Group Change Notice (2.1) */
1082 		uint32_t oaes_rsvd18:1;
1083 		uint32_t oaes_ansan:1;	/* Allocated Namespace Attr. (2.1) */
1084 		uint32_t oaes_ccrcn:1;	/* X-Ctrl Reset Compl. Notices (2.3) */
1085 		uint32_t oaes_lhcn:1;	/* Lost Host Comms. Notices (2.3) */
1086 		uint32_t oaes_rsvd22:5;
1087 		uint32_t oaes_zdcn:1;	/* Zone Descriptor Change (2.0) */
1088 		uint32_t oaes_rsvd28:3;
1089 		uint32_t oaes_dlpcn:1;	/* Disc Log Page Change (2.0) */
1090 	} id_oaes;
1091 	struct {
1092 		uint32_t ctrat_hid:1;	/* 128-bit Host Identifier (1.2)  */
1093 		uint32_t ctrat_nops:1;	/* Non-Operational Power State (1.3) */
1094 		uint32_t ctrat_nvmset:1; /* NVMe Sets (1.4) */
1095 		uint32_t ctrat_rrl:1;	/* Read Recovery Levels (1.4) */
1096 		uint32_t ctrat_engrp:1; /* Endurance Groups (1.4) */
1097 		uint32_t ctrat_plm:1;	/* Predictable Latency Mode (1.4) */
1098 		uint32_t ctrat_tbkas:1;	/* Traffic Based Keep Alive (1.4) */
1099 		uint32_t ctrat_nsg:1;	/* Namespace Granularity (1.4) */
1100 		uint32_t ctrat_sqass:1;	/* SQ Associations (1.4) */
1101 		uint32_t ctrat_uuid:1;	/* UUID List (1.4) */
1102 		uint32_t ctrat_mds:1;	/* Multi-Domain Subsys (2.0) */
1103 		uint32_t ctrat_fcm:1;	/* Fixed Cap Management (2.0) */
1104 		uint32_t ctrat_vcm:1;	/* Variable Cap Management (2.0) */
1105 		uint32_t ctrat_deg:1;	/* Delete Endurance Group (2.0) */
1106 		uint32_t ctrat_dnvms:1;	/* Delete NVM Set (2.0) */
1107 		uint32_t ctrat_elbas:1;	/* Ext. LBA Formats (2.0) */
1108 		uint32_t ctrat_mem:1;	/* MDTS and Size exclude Meta (2.1) */
1109 		uint32_t ctrat_hmbr:1;	/* HMB Restrictions (2.1) */
1110 		uint32_t ctrat_rhii:1;	/* Reservations and Host ID (2.1) */
1111 		uint32_t ctrat_fdps:1;	/* Flexible Data Placement (2.1) */
1112 		uint32_t ctrat_pls:1;	/* Power Limit Support (2.3) */
1113 		uint32_t ctrat_pms:1;	/* Power Measurement Support (2.3) */
1114 		uint32_t ctrat_rsvd22:10;
1115 	} id_ctratt;
1116 	uint16_t id_rrls;		/* Read Recovery Levels (1.4) */
1117 	struct {
1118 		uint8_t bpcap_rpmbbpwps:2;	/* RPMB Prot Write (2.1) */
1119 		uint8_t bpcap_sfbpwps:1;	/* Set Feat RPMB (2.1) */
1120 		uint8_t bpcap_rsvd3:5;
1121 	} id_bpcap;
1122 	uint8_t id_rsvd_103;
1123 	uint32_t id_nssl;		/* NVM Subsystem Shutdown Lat. (2.1) */
1124 	uint8_t id_rsvd_108[2];
1125 	struct {
1126 		uint8_t plsi_plsepf:1;	/* PLS Emergency Power Fail (2.1) */
1127 		uint8_t plsi_plsfq:1;	/* PLS Force Quiesce (2.1) */
1128 		uint8_t plsi_rvsd:6;
1129 	} id_plsi;
1130 	uint8_t id_cntrltype;		/* Controller Type (1.4) */
1131 	uint8_t id_frguid[16];		/* FRU GUID (1.3) */
1132 	uint16_t id_crdt1;		/* Command Retry Delay Time 1 (1.4) */
1133 	uint16_t id_crdt2;		/* Command Retry Delay Time 2 (1.4) */
1134 	uint16_t id_crdt3;		/* Command Retry Delay Time 3 (1.4) */
1135 	struct {
1136 		uint8_t crcap_rrsup:1;	/* Reachability Reporting (2.1) */
1137 		uint8_t crcap_rgidc:1;	/* Group ID Changeable (2.1) */
1138 		uint8_t crcap_rsvd2:6;
1139 	} id_crcap;
1140 	uint8_t id_ciu;			/* Controller Inst. Uniquifier (2.3) */
1141 	uint8_t id_cirn[8];		/* Controller Inst. Random Num. (2.3) */
1142 	uint8_t id_rsvd2_cc[240 - 144];
1143 	uint8_t id_rsvd_nvmemi[253 - 240];
1144 	/* NVMe-MI region */
1145 	struct {			/* NVMe Subsystem Report */
1146 		uint8_t nvmsr_nvmesd:1;	/* NVMe Storage Device */
1147 		uint8_t nvmsr_nvmee:1;	/* NVMe Enclosure */
1148 		uint8_t nvmsr_rsvd:6;
1149 	} id_nvmsr;
1150 	struct {			/* VPD Write Cycle Information */
1151 		uint8_t vwci_crem:7;	/* Write Cycles Remaining */
1152 		uint8_t vwci_valid:1;	/* Write Cycles Remaining Valid */
1153 	} id_vpdwc;
1154 	struct {			/* Management Endpoint Capabilities */
1155 		uint8_t mec_smbusme:1;	/* SMBus Port Management Endpoint */
1156 		uint8_t mec_pcieme:1;	/* PCIe Port Management Endpoint */
1157 		uint8_t mec_rsvd:6;
1158 	} id_mec;
1159 
1160 	/* Admin Command Set Attributes */
1161 	struct {			/* Optional Admin Command Support */
1162 		uint16_t oa_security:1;	/* Security Send & Receive */
1163 		uint16_t oa_format:1;	/* Format NVM */
1164 		uint16_t oa_firmware:1;	/* Firmware Activate & Download */
1165 		uint16_t oa_nsmgmt:1;	/* Namespace Management (1.2) */
1166 		uint16_t oa_selftest:1;	/* Self Test (1.3) */
1167 		uint16_t oa_direct:1;	/* Directives (1.3) */
1168 		uint16_t oa_nvmemi:1;	/* MI-Send/Recv (1.3) */
1169 		uint16_t oa_virtmgmt:1;	/* Virtualization Management (1.3) */
1170 		uint16_t oa_doorbell:1;	/* Doorbell Buffer Config (1.3) */
1171 		uint16_t oa_lbastat:1;	/* LBA Status (1.4) */
1172 		uint16_t oa_clfs:1;	/* Command and Feat Lockdown (2.0) */
1173 		uint16_t oa_hmlms:1;	/* Host Managed Live Migration (2.0) */
1174 		uint16_t oa_rsvd12:4;
1175 	} id_oacs;
1176 	uint8_t	id_acl;			/* Abort Command Limit */
1177 	uint8_t id_aerl;		/* Asynchronous Event Request Limit */
1178 	struct {			/* Firmware Updates */
1179 		uint8_t fw_readonly:1;	/* Slot 1 is Read-Only */
1180 		uint8_t	fw_nslot:3;	/* number of firmware slots */
1181 		uint8_t fw_norst:1;	/* Activate w/o reset (1.2) */
1182 		uint8_t fw_smud:1;	/* Support Multiple Update Det. (2.0) */
1183 		uint8_t fw_rsvd6:2;
1184 	} id_frmw;
1185 	struct {			/* Log Page Attributes */
1186 		uint8_t lp_smart:1;	/* SMART/Health information per NS */
1187 		uint8_t lp_cmdeff:1;	/* Command Effects (1.2) */
1188 		uint8_t lp_extsup:1;	/* Extended Get Log Page (1.2) */
1189 		uint8_t lp_telemetry:1;	/* Telemetry Log Pages (1.3) */
1190 		uint8_t lp_persist:1;	/* Persistent Log Page (1.4) */
1191 		uint8_t lp_mlps:1;	/* Misc. Log Page (2.0) */
1192 		uint8_t lp_da4s:1;	/* Data Area 4 Support (2.0) */
1193 		uint8_t lp_rsvd7:1;
1194 	} id_lpa;
1195 	uint8_t id_elpe;		/* Error Log Page Entries */
1196 	uint8_t	id_npss;		/* Number of Power States */
1197 	struct {			/* Admin Vendor Specific Command Conf */
1198 		uint8_t av_spec:1;	/* use format from spec */
1199 		uint8_t av_rsvd:7;
1200 	} id_avscc;
1201 	struct {			/* Autonomous Power State Trans (1.1) */
1202 		uint8_t ap_sup:1;	/* APST supported (1.1) */
1203 		uint8_t ap_rsvd:7;
1204 	} id_apsta;
1205 	uint16_t ap_wctemp;		/* Warning Composite Temp. (1.2) */
1206 	uint16_t ap_cctemp;		/* Critical Composite Temp. (1.2) */
1207 	uint16_t ap_mtfa;		/* Maximum Firmware Activation (1.2) */
1208 	uint32_t ap_hmpre;		/* Host Memory Buf Pref Size (1.2) */
1209 	uint32_t ap_hmmin;		/* Host Memory Buf Min Size (1.2) */
1210 	nvme_uint128_t ap_tnvmcap;	/* Total NVM Capacity in Bytes (1.2) */
1211 	nvme_uint128_t ap_unvmcap;	/* Unallocated NVM Capacity (1.2) */
1212 	struct {			/* Replay Protected Mem. Block (1.2) */
1213 		uint32_t rpmbs_units:3;	/* Number of targets */
1214 		uint32_t rpmbs_auth:3;	/* Auth method */
1215 		uint32_t rpmbs_rsvd:10;
1216 		uint32_t rpmbs_tot:8;	/* Total size in 128KB */
1217 		uint32_t rpmbs_acc:8;	/* Access size in 512B */
1218 	} ap_rpmbs;
1219 	/* Added in NVMe 1.3 */
1220 	uint16_t ap_edstt;		/* Ext. Device Self-test time (1.3) */
1221 	struct {			/* Device Self-test Options */
1222 		uint8_t dsto_sub:1;	/* Subsystem level self-test (1.3) */
1223 		uint8_t dsto_hirs:1;	/* Host-Initiated Refresh (2.1) */
1224 		uint8_t dsto_rsvd:6;
1225 	} ap_dsto;
1226 	uint8_t ap_fwug;		/* Firmware Update Granularity (1.3) */
1227 	uint16_t ap_kas;		/* Keep Alive Support (1.2) */
1228 	struct {			/* Host Thermal Management (1.3) */
1229 		uint16_t hctma_hctm:1;	/* Host Controlled (1.3) */
1230 		uint16_t hctma_rsvd:15;
1231 	} ap_hctma;
1232 	uint16_t ap_mntmt;		/* Minimum Thermal Temperature (1.3) */
1233 	uint16_t ap_mxtmt;		/* Maximum Thermal Temperature (1.3) */
1234 	struct {			/* Sanitize Caps */
1235 		uint32_t san_ces:1;	/* Crypto Erase Support (1.3) */
1236 		uint32_t san_bes:1;	/* Block Erase Support (1.3) */
1237 		uint32_t san_ows:1;	/* Overwite Support (1.3) */
1238 		uint32_t san_vers:1;	/* Verification Support (2.1) */
1239 		uint32_t san_rsvd:25;
1240 		uint32_t san_ndi:1;	/* No-deallocate Inhibited (1.4) */
1241 		uint32_t san_nodmmas:2;	/* No-Deallocate Modifies Media (1.4) */
1242 	} ap_sanitize;
1243 	uint32_t ap_hmminds;		/* Host Mem Buf Min Desc Entry (1.4) */
1244 	uint16_t ap_hmmaxd;		/* How Mem Max Desc Entries (1.4) */
1245 	uint16_t ap_nsetidmax;		/* Max NVMe set identifier (1.4) */
1246 	uint16_t ap_engidmax;		/* Max Endurance Group ID (1.4) */
1247 	uint8_t ap_anatt;		/* ANA Transition Time (1.4) */
1248 	struct {			/* Asymmetric Namespace Access Caps */
1249 		uint8_t anacap_opt:1;	/* Optimized State (1.4) */
1250 		uint8_t anacap_unopt:1;	/* Un-optimized State (1.4) */
1251 		uint8_t anacap_inacc:1;	/* Inaccessible State (1.4) */
1252 		uint8_t anacap_ploss:1;	/* Persistent Loss (1.4) */
1253 		uint8_t anacap_chg:1;	/* Change State (1.4 ) */
1254 		uint8_t anacap_rsvd:1;
1255 		uint8_t anacap_grpns:1;	/* ID Changes with NS Attach (1.4) */
1256 		uint8_t anacap_grpid:1;	/* Supports Group ID (1.4) */
1257 	} ap_anacap;
1258 	uint32_t ap_anagrpmax;		/* ANA Group ID Max (1.4) */
1259 	uint32_t ap_nanagrpid;		/* Number of ANA Group IDs (1.4) */
1260 	uint32_t ap_pels;		/* Persistent Event Log Size (1.4) */
1261 	uint16_t ap_did;		/* Domain ID (2.0) */
1262 	struct {
1263 		uint8_t kpioc_kpios:1;	/* Key Per I/O Sup (2.1) */
1264 		uint8_t kpioc_kpisc:1;	/* Key Per I/O Scope (2.1) */
1265 		uint8_t kpioc_rsvd:6;
1266 	} ap_kpioc;
1267 	uint8_t ap_rsvd359;
1268 	uint16_t ap_mptfawr;		/* Max FW Act Time w/o Reset (2.1) */
1269 	uint8_t ap_rsvd362[368-362];
1270 	nvme_uint128_t ap_megcap;	/* Max Endurance Group Cap (2.1) */
1271 	struct {
1272 		uint8_t tmpthha_tmpthmh:3;	/* Temp Tresh Max Hyst (2.1) */
1273 		uint8_t tmpthha_rsvd3:5;
1274 	} ap_tmpthha;
1275 	struct {
1276 		uint8_t mupa_mups:2;	/* Max. Unlimited Power Scale (2.3) */
1277 		uint8_t mupa_rsvd2:6;
1278 	} ap_mupa;			/* Max. Unlimited Power Attrs. (2.3) */
1279 	uint16_t ap_cqt;		/* Command Quiesce Time (2.1) */
1280 	struct {
1281 		struct {
1282 			uint8_t cdpalg_hs3:1;	/* HMAC-SHA-384 (2.3) */
1283 			uint8_t cdpalg_rsvd:7;
1284 		} cdpa_cdpalg;		/* CDP Authentication Algorithm (2.3) */
1285 		uint8_t cdpa_rsvd8;
1286 	} ap_cdpa;			/* Cfg. Device Personality Attr (2.3) */
1287 	uint16_t ap_mup;		/* Maximum Unlimited Power (2.3) */
1288 	struct {
1289 		uint8_t impsr_srv;	/* Sample Rate Value (2.3) */
1290 		uint8_t impsr_srs;	/* Sample Rate Scale (2.3) */
1291 	} ap_impsr;			/* Interval Power Sample Rate (2.3) */
1292 	uint16_t ap_msmt;		/* Maximum Stop Measure. Time (2.3) */
1293 	uint8_t ap_rsvd_ac[512 - 396];
1294 
1295 	/* NVM Command Set Attributes */
1296 	nvme_idctl_qes_t id_sqes;	/* Submission Queue Entry Size */
1297 	nvme_idctl_qes_t id_cqes;	/* Completion Queue Entry Size */
1298 	uint16_t id_maxcmd;		/* Max Outstanding Commands (1.3) */
1299 	uint32_t id_nn;			/* Number of Namespaces */
1300 	struct {			/* Optional NVM Command Support */
1301 		uint16_t on_compare:1;	/* Compare */
1302 		uint16_t on_wr_unc:1;	/* Write Uncorrectable */
1303 		uint16_t on_dset_mgmt:1; /* Dataset Management */
1304 		uint16_t on_wr_zero:1;	/* Write Zeroes (1.1) */
1305 		uint16_t on_save:1;	/* Save/Select in Get/Set Feat (1.1) */
1306 		uint16_t on_reserve:1;	/* Reservations (1.1) */
1307 		uint16_t on_ts:1;	/* Timestamp (1.3) */
1308 		uint16_t on_verify:1;	/* Verify (1.4) */
1309 		uint16_t on_nvmcpys:1;	/* Copy (2.0) */
1310 		uint16_t on_nvmcsa:1;	/* NVM Copy Single Atomicity (2.1) */
1311 		uint16_t on_nvmafc:1;	/* NVM All Fast Copy (2.1) */
1312 		uint16_t on_maxwzd:1;	/* Max Write Zeroes w/ Dealloc (2.1) */
1313 		uint16_t on_nszs:1;	/* Namespace zeros (2.1) */
1314 		uint16_t on_rsvd13:3;
1315 	} id_oncs;
1316 	struct {			/* Fused Operation Support */
1317 		uint16_t f_cmp_wr:1;	/* Compare and Write */
1318 		uint16_t f_rsvd:15;
1319 	} id_fuses;
1320 	struct {			/* Format NVM Attributes */
1321 		uint8_t fn_format:1;	/* Format applies to all NS */
1322 		uint8_t fn_sec_erase:1;	/* Secure Erase applies to all NS */
1323 		uint8_t fn_crypt_erase:1; /* Cryptographic Erase supported */
1324 		uint8_t fn_fnvmbs:1;	/* Format NVM Broadcast (2.0) */
1325 		uint8_t fn_rsvd:4;
1326 	} id_fna;
1327 	struct {			/* Volatile Write Cache */
1328 		uint8_t vwc_present:1;	/* Volatile Write Cache present */
1329 		uint8_t vwc_nsflush:2;	/* Flush with NS ffffffff (1.4) */
1330 		uint8_t rsvd:5;
1331 	} id_vwc;
1332 	uint16_t id_awun;		/* Atomic Write Unit Normal */
1333 	uint16_t id_awupf;		/* Atomic Write Unit Power Fail */
1334 	struct {			/* NVM Vendor Specific Command Conf */
1335 		uint8_t nv_spec:1;	/* use format from spec */
1336 		uint8_t nv_rsvd:7;
1337 	} id_nvscc;
1338 	struct {			/* Namespace Write Protection Caps */
1339 		uint8_t nwpc_base:1;	/* Base support (1.4) */
1340 		uint8_t nwpc_wpupc:1;	/* Write prot until power cycle (1.4) */
1341 		uint8_t nwpc_permwp:1;	/* Permanent write prot (1.4) */
1342 		uint8_t nwpc_rsvd:5;
1343 	} id_nwpc;
1344 	uint16_t id_acwu;		/* Atomic Compare & Write Unit (1.1) */
1345 	struct {
1346 		uint16_t cdfs_cdf0s:1;	/* Copy Desc Format 0 (2.0) */
1347 		uint16_t cdfs_cdf1s:1;	/* Copy Desc Format 1 (2.0) */
1348 		uint16_t cdfs_cdf2s:1;	/* Copy Desc Format 2 (2.1) */
1349 		uint16_t cdfs_cdf3s:1;	/* Copy Desc Format 3 (2.1) */
1350 		uint16_t cdfs_cdf4s:1;	/* Copy Desc Format 4 (2.1) */
1351 		uint16_t cdfs_rsvd5:11;
1352 	} id_cdfs;
1353 	struct {			/* SGL Support (1.1) */
1354 		uint16_t sgl_sup:2;	/* SGL Supported in NVM cmds (1.3) */
1355 		uint16_t sgl_keyed:1;	/* Keyed SGL Support (1.2) */
1356 		uint16_t sgl_rsvd3:5;
1357 		uint16_t sgl_sdt:8;	/* SGL Desc Threshold (2.0) */
1358 		uint16_t sgl_bucket:1;	/* SGL Bit Bucket supported (1.1) */
1359 		uint16_t sgl_balign:1;	/* SGL Byte Aligned (1.2) */
1360 		uint16_t sgl_sglgtd:1;	/* SGL Length Longer than Data (1.2) */
1361 		uint16_t sgl_mptr:1;	/* SGL MPTR w/ SGL (1.2) */
1362 		uint16_t sgl_offset:1;	/* SGL Address is offset (1.2) */
1363 		uint16_t sgl_tport:1;	/* Transport SGL Data Block (1.4) */
1364 		uint16_t sgl_rsvd22:10;
1365 	} id_sgls;
1366 	uint32_t id_mnan;		/* Maximum Num of Allowed NSes (1.4) */
1367 	nvme_uint128_t id_maxdna;	/* Maximum Domain NS Attach (2.0) */
1368 	uint32_t id_maxcna;		/* Maximum I/O Ctrl NS Attach (2.0) */
1369 	uint32_t id_oaqd;		/* Optimal Agg. Queue Depth (2.1) */
1370 	uint8_t id_rhiri;		/* Host-Init Refresh Ival (2.1) */
1371 	uint8_t id_hirt;		/* Host-Init refresh time (2.1) */
1372 	uint16_t id_cmmrtd;		/* Ctrl. Max Mem Track Desc (2.1) */
1373 	uint16_t id_nmmrtd;		/* NVM Max Mem Track Desc (2.1) */
1374 	uint8_t id_minmrtg;		/* Min Mem Range Track Gran (2.1) */
1375 	uint8_t id_maxmrtg;		/* Max Mem Range Track Gran (2.1) */
1376 	struct {
1377 		uint8_t trattr_thmcs:1;	/* Track Host Memory Changes (2.1) */
1378 		uint8_t trattr_tudcs:1;	/* Track User Data Changes (2.1) */
1379 		uint8_t trattr_mrtll:1;	/* Memory Range Tracking Lim (2.1) */
1380 		uint8_t trattr_rsvd3:5;
1381 	} id_trattr;
1382 	uint8_t id_rsvd577;
1383 	uint16_t id_mcudmq;		/* Max Ctrl User Mig Queues (2.1) */
1384 	uint16_t id_mnsudmq;		/* Max NVM Sys Mig Queues (2.1) */
1385 	uint16_t id_mcmr;		/* Max CDQ Memory Ranges (2.1) */
1386 	uint16_t id_nmcmr;		/* NVM Sub Max CDQ Mem Ranges (2.1) */
1387 	uint16_t id_mcdqpc;		/* Max Ctrl Data Queue PRP (2.1) */
1388 	uint8_t id_rsvd_nc_4[768 - 588];
1389 
1390 	/* I/O Command Set Attributes */
1391 	uint8_t id_subnqn[1024 - 768];	/* Subsystem Qualified Name (1.2.1+) */
1392 	uint8_t id_rsvd_ioc[1792 - 1024];
1393 	uint8_t id_nvmof[2048 - 1792];	/* NVMe over Fabrics */
1394 
1395 	/* Power State Descriptors */
1396 	nvme_idctl_psd_t id_psd[32];
1397 
1398 	/* Vendor Specific */
1399 	uint8_t id_vs[1024];
1400 } nvme_identify_ctrl_t;
1401 
1402 /*
1403  * NVMe Controller Types
1404  */
1405 #define	NVME_CNTRLTYPE_RSVD	0
1406 #define	NVME_CNTRLTYPE_IO	1
1407 #define	NVME_CNTRLTYPE_DISC	2
1408 #define	NVME_CNTRLTYPE_ADMIN	3
1409 
1410 /*
1411  * RPMBS Authentication Types
1412  */
1413 #define	NVME_RPMBS_AUTH_HMAC_SHA256	0
1414 
1415 /*
1416  * NODMMAS Values
1417  */
1418 #define	NVME_NODMMAS_UNDEF	0x00
1419 #define	NVME_NODMMAS_NOMOD	0x01
1420 #define	NVME_NODMMAS_DOMOD	0x02
1421 
1422 /*
1423  * VWC NSID flushes
1424  */
1425 #define	NVME_VWCNS_UNKNOWN	0x00
1426 #define	NVME_VWCNS_UNSUP	0x02
1427 #define	NVME_VWCNS_SUP		0x03
1428 
1429 /*
1430  * SGL Support Values
1431  */
1432 #define	NVME_SGL_UNSUP		0x00
1433 #define	NVME_SGL_SUP_UNALIGN	0x01
1434 #define	NVME_SGL_SUP_ALIGN	0x02
1435 
1436 /* NVMe Identify Namespace LBA Format */
1437 typedef struct {
1438 	uint16_t lbaf_ms;		/* Metadata Size */
1439 	uint8_t lbaf_lbads;		/* LBA Data Size */
1440 	uint8_t lbaf_rp:2;		/* Relative Performance */
1441 	uint8_t lbaf_rsvd1:6;
1442 } nvme_idns_lbaf_t;
1443 
1444 #define	NVME_MAX_LBAF	16
1445 
1446 /* NVMe Identify Namespace Data Structure */
1447 typedef struct {
1448 	uint64_t id_nsize;		/* Namespace Size */
1449 	uint64_t id_ncap;		/* Namespace Capacity */
1450 	uint64_t id_nuse;		/* Namespace Utilization */
1451 	struct {			/* Namespace Features */
1452 		uint8_t f_thin:1;	/* Thin Provisioning */
1453 		uint8_t f_nsabp:1;	/* Namespace atomics (1.2) */
1454 		uint8_t f_dae:1;	/* Deallocated errors supported (1.2) */
1455 		uint8_t f_uidreuse:1;	/* GUID reuse impossible (1.3) */
1456 		uint8_t f_optperf:2;	/* Namespace I/O opt (1.4, N1.1) */
1457 		uint8_t f_mam:1;	/* Multiple Atomicity (N1.1) */
1458 		uint8_t f_optrperf:1;	/* Optional Read Perf (N1.1) */
1459 	} id_nsfeat;
1460 	uint8_t id_nlbaf;		/* Number of LBA formats */
1461 	struct {			/* Formatted LBA size */
1462 		uint8_t lba_format:4;	/* LBA format */
1463 		uint8_t lba_extlba:1;	/* extended LBA (includes metadata) */
1464 		uint8_t lba_fidxu:2;	/* Format Index Upper (N1.0) */
1465 		uint8_t lba_rsvd:1;
1466 	} id_flbas;
1467 	struct {			/* Metadata Capabilities */
1468 		uint8_t mc_extlba:1;	/* extended LBA transfers */
1469 		uint8_t mc_separate:1;	/* separate metadata transfers */
1470 		uint8_t mc_rsvd:6;
1471 	} id_mc;
1472 	struct {			/* Data Protection Capabilities */
1473 		uint8_t dp_type1:1;	/* Protection Information Type 1 */
1474 		uint8_t dp_type2:1;	/* Protection Information Type 2 */
1475 		uint8_t dp_type3:1;	/* Protection Information Type 3 */
1476 		uint8_t dp_first:1;	/* first 8 bytes of metadata */
1477 		uint8_t dp_last:1;	/* last 8 bytes of metadata */
1478 		uint8_t dp_rsvd:3;
1479 	} id_dpc;
1480 	struct {			/* Data Protection Settings */
1481 		uint8_t dp_pinfo:3;	/* Protection Information enabled */
1482 		uint8_t dp_first:1;	/* first 8 bytes of metadata */
1483 		uint8_t dp_rsvd:4;
1484 	} id_dps;
1485 	struct {			/* NS Multi-Path/Sharing Cap (1.1) */
1486 		uint8_t nm_shared:1;	/* NS is shared (1.1) */
1487 		uint8_t nm_disperse:1;	/* NS is dispersed (2.1) */
1488 		uint8_t nm_rsvd:6;
1489 	} id_nmic;
1490 	struct {			/* Reservation Capabilities (1.1) */
1491 		uint8_t rc_persist:1;	/* Persist Through Power Loss (1.1) */
1492 		uint8_t rc_wr_excl:1;	/* Write Exclusive (1.1) */
1493 		uint8_t rc_excl:1;	/* Exclusive Access (1.1) */
1494 		uint8_t rc_wr_excl_r:1;	/* Wr Excl - Registrants Only (1.1) */
1495 		uint8_t rc_excl_r:1;	/* Excl Acc - Registrants Only (1.1) */
1496 		uint8_t rc_wr_excl_a:1;	/* Wr Excl - All Registrants (1.1) */
1497 		uint8_t rc_excl_a:1;	/* Excl Acc - All Registrants (1.1) */
1498 		uint8_t rc_ign_ekey:1;	/* Ignore Existing Key (1.3) */
1499 	} id_rescap;
1500 	struct {			/* Format Progress Indicator (1.2) */
1501 		uint8_t fpi_remp:7;	/* Percent NVM Format Remaining (1.2) */
1502 		uint8_t fpi_sup:1;	/* Supported (1.2) */
1503 	} id_fpi;
1504 	struct {
1505 		uint8_t dlfeat_drb:3;	/* Deallocation Read Behavior (1.3) */
1506 		uint8_t dlfeat_wzds:1;	/* Write Zeroes Deallocation (1.3) */
1507 		uint8_t dlfeat_gds:1;	/* Guard Deallocation Status (1.3) */
1508 		uint8_t dlfeat_rsvd5:3;
1509 	} id_dlfeat;
1510 	uint16_t id_nawun;		/* Atomic Write Unit Normal (1.2) */
1511 	uint16_t id_nawupf;		/* Atomic Write Unit Power Fail (1.2) */
1512 	uint16_t id_nacwu;		/* Atomic Compare & Write Unit (1.2) */
1513 	uint16_t id_nabsn;		/* Atomic Boundary Size Normal (1.2) */
1514 	uint16_t id_nbao;		/* Atomic Boundary Offset (1.2) */
1515 	uint16_t id_nabspf;		/* Atomic Boundary Size Fail (1.2) */
1516 	uint16_t id_noiob;		/* Optimal I/O Boundary (1.3) */
1517 	nvme_uint128_t id_nvmcap;	/* NVM Capacity */
1518 	uint16_t id_npwg;		/* NS Pref. Write Gran. (1.4) */
1519 	uint16_t id_npwa;		/* NS Pref. Write Align. (1.4) */
1520 	uint16_t id_npdg;		/* NS Pref. Deallocate Gran. (1.4) */
1521 	uint16_t id_npda;		/* NS Pref. Deallocate Align. (1.4) */
1522 	uint16_t id_nows;		/* NS. Optimal Write Size (1.4) */
1523 	uint16_t id_mssrl;		/* Max Single Source Range (N1.0) */
1524 	uint32_t id_mcl;		/* Max Copy Length (N1.0) */
1525 	uint8_t id_msrc;		/* Max Source Range (N1.0) */
1526 	struct {
1527 		uint8_t kpios_kpioens:1;	/* Key Per I/O En (N1.1) */
1528 		uint8_t kpios_kpiosns:1;	/* Key Per I/O Sup (N1.1) */
1529 		uint8_t kpios_rsvd2:6;
1530 	} id_kpios;
1531 	uint8_t id_nulbaf;		/* Unique Attr. LBA Formats (N1.1) */
1532 	uint8_t id_rsvd83;
1533 	uint32_t id_kpiodaag;		/* Key Per I/O Access Gran (N1.1) */
1534 	uint8_t id_rsvd1[92 - 88];
1535 	uint32_t id_anagrpid;		/* ANA Group Identifier (1.4) */
1536 	uint8_t id_rsvd2[99 - 96];
1537 	struct {
1538 		uint8_t nsa_wprot:1;	/* Write Protected (1.4) */
1539 		uint8_t nsa_rsvd:7;
1540 	} id_nsattr;
1541 	uint16_t id_nvmsetid;		/* NVM Set Identifier (1.4) */
1542 	uint16_t id_endgid;		/* Endurance Group Identifier (1.4) */
1543 	uint8_t id_nguid[16];		/* Namespace GUID (1.2) */
1544 	uint8_t id_eui64[8];		/* IEEE Extended Unique Id (1.1) */
1545 	nvme_idns_lbaf_t id_lbaf[NVME_MAX_LBAF];	/* LBA Formats */
1546 	/*
1547 	 * This region contains additional LBAF and should be updated as part of
1548 	 * enabling support for additional LBA formats in the stack.
1549 	 */
1550 	uint8_t id_rsvd3[384 - 192];
1551 
1552 	uint8_t id_vs[4096 - 384];	/* Vendor Specific */
1553 } nvme_identify_nsid_t;
1554 
1555 /* NVMe Identify Namespace ID List */
1556 typedef struct {
1557 					/* Ordered list of Namespace IDs */
1558 	uint32_t nl_nsid[NVME_IDENTIFY_BUFSIZE / sizeof (uint32_t)];
1559 } nvme_identify_nsid_list_t;
1560 
1561 /* NVME Identify Controller ID List */
1562 typedef struct {
1563 	uint16_t	cl_nid;		/* Number of controller entries */
1564 					/* unique controller identifiers */
1565 	uint16_t	cl_ctlid[NVME_IDENTIFY_BUFSIZE / sizeof (uint16_t) - 1];
1566 } nvme_identify_ctrl_list_t;
1567 
1568 /* NVMe Identify Namespace Descriptor */
1569 typedef struct {
1570 	uint8_t nd_nidt;		/* Namespace Identifier Type */
1571 	uint8_t nd_nidl;		/* Namespace Identifier Length */
1572 	uint8_t nd_resv[2];
1573 	uint8_t nd_nid[];		/* Namespace Identifier */
1574 } nvme_identify_nsid_desc_t;
1575 
1576 #define	NVME_NSID_DESC_EUI64	1
1577 #define	NVME_NSID_DESC_NGUID	2
1578 #define	NVME_NSID_DESC_NUUID	3
1579 #define	NVME_NSID_DESC_CSI	4	/* 2.1 */
1580 #define	NVME_NSID_DESC_MIN	NVME_NSID_DESC_EUI64
1581 #define	NVME_NSID_DESC_MAX	NVME_NSID_DESC_CSI
1582 
1583 #define	NVME_NSID_DESC_LEN_EUI64	8
1584 #define	NVME_NSID_DESC_LEN_NGUID	16
1585 #define	NVME_NSID_DESC_LEN_NUUID	UUID_LEN
1586 #define	NVME_NSID_DESC_LEN_CSI		1
1587 
1588 /* NVMe Identify Primary Controller Capabilities */
1589 typedef struct {
1590 	uint16_t	nipc_cntlid;	/* Controller ID */
1591 	uint16_t	nipc_portid;	/* Port Identifier */
1592 	uint8_t		nipc_crt;	/* Controller Resource Types */
1593 	uint8_t		nipc_rsvd0[32 - 5];
1594 	uint32_t	nipc_vqfrt;	/* VQ Resources Flexible Total */
1595 	uint32_t	nipc_vqrfa;	/* VQ Resources Flexible Assigned */
1596 	uint16_t	nipc_vqrfap;	/* VQ Resources to Primary */
1597 	uint16_t	nipc_vqprt;	/* VQ Resources Private Total */
1598 	uint16_t	nipc_vqfrsm;	/* VQ Resources Secondary Max */
1599 	uint16_t	nipc_vqgran;	/* VQ Flexible Resource Gran */
1600 	uint8_t		nipc_rvsd1[64 - 48];
1601 	uint32_t	nipc_vifrt;	/* VI Flexible total */
1602 	uint32_t	nipc_virfa;	/* VI Flexible Assigned */
1603 	uint16_t	nipc_virfap;	/* VI Flexible Allocated to Primary */
1604 	uint16_t	nipc_viprt;	/* VI Resources Private Total */
1605 	uint16_t	nipc_vifrsm;	/* VI Resources Secondary Max */
1606 	uint16_t	nipc_vigran;	/* VI Flexible Granularity */
1607 	uint8_t		nipc_rsvd2[4096 - 80];
1608 } nvme_identify_primary_caps_t;
1609 
1610 /*
1611  * NVMe completion queue entry status field
1612  */
1613 typedef struct {
1614 	uint16_t sf_p:1;		/* Phase Tag */
1615 	uint16_t sf_sc:8;		/* Status Code */
1616 	uint16_t sf_sct:3;		/* Status Code Type */
1617 	uint16_t sf_rsvd2:2;
1618 	uint16_t sf_m:1;		/* More */
1619 	uint16_t sf_dnr:1;		/* Do Not Retry */
1620 } nvme_cqe_sf_t;
1621 
1622 
1623 /*
1624  * NVMe Get Log Page
1625  */
1626 #define	NVME_LOGPAGE_SUP	0x00	/* Supported Logs (2.0) */
1627 #define	NVME_LOGPAGE_ERROR	0x01	/* Error Information */
1628 #define	NVME_LOGPAGE_HEALTH	0x02	/* SMART/Health Information */
1629 #define	NVME_LOGPAGE_FWSLOT	0x03	/* Firmware Slot Information */
1630 #define	NVME_LOGPAGE_NSCHANGE	0x04	/* Changed namespace (1.2) */
1631 #define	NVME_LOGPAGE_CMDSUP	0x05	/* Cmds. Supported and Effects (1.3) */
1632 #define	NVME_LOGPAGE_SELFTEST	0x06	/* Device self-test (1.3) */
1633 #define	NVME_LOGPAGE_TELMHOST	0x07	/* Telemetry Host-Initiated */
1634 #define	NVME_LOGPAGE_TELMCTRL	0x08	/* Telemetry Controller-Initiated */
1635 #define	NVME_LOGPAGE_ENDGRP	0x09	/* Endurance Group Information (1.4) */
1636 #define	NVME_LOGPAGE_PLATSET	0x0a	/* Predictable Lat. per NVM Set (1.4) */
1637 #define	NVME_LOGPAGE_PLATAGG	0x0b	/* Predictable Lat. Event Agg (1.4) */
1638 #define	NVME_LOGPAGE_ASYMNS	0x0c	/* Asymmetric Namespace Access (1.4) */
1639 #define	NVME_LOGPAGE_PEV	0x0d	/* Persistent Event Log (1.4) */
1640 #define	NVME_LOGPAGE_LBASTS	0x0e	/* LBA Status Information (1.4) */
1641 #define	NVME_LOGPAGE_ENDAGG	0x0f	/* Endurance Group Event Agg. (1.4) */
1642 #define	NVME_LOGPAGE_MEDIA	0x10	/* Media Unit Status (2.0) */
1643 #define	NVME_LOGPAGE_CAPCFG	0x11	/* Supported Capacity cfg. (2.0) */
1644 #define	NVME_LOGPAGE_SUPFEAT	0x12	/* Supported Features (2.0) */
1645 #define	NVME_LOGPAGE_SUPMI	0x13	/* Supported NVMe-MI Commands (2.0) */
1646 #define	NVME_LOGPAGE_LOCKDOWN	0x14	/* Command and Feature Lockdown (2.0) */
1647 #define	NVME_LOGPAGE_BOOTPART	0x15	/* Boot Partition (2.0) */
1648 #define	NVME_LOGPAGE_ROTMEDIA	0x16	/* Rotational Media (2.0) */
1649 #define	NVME_LOGPAGE_DISPNS	0x17	/* Dispersed Namespace (2.1) */
1650 #define	NVME_LOGPAGE_MGMTADDR	0x18	/* Management Address List (2.1) */
1651 #define	NVME_LOGPAGE_PHYEYE	0x19	/* Physical Interface Eye (PCIe 1.1) */
1652 #define	NVME_LOGPAGE_REACHGRP	0x1a	/* Reachability Groups (2.1) */
1653 #define	NVME_LOGPAGE_REACHASC	0x1b	/* Reachability Associations (2.1) */
1654 #define	NVME_LOGPAGE_NSACHANGE	0x1c	/* Changed allocated namespaces (2.1) */
1655 #define	NVME_LOGPAGE_DEVPERS	0x1d	/* Device Personalities (2.3) */
1656 #define	NVME_LOGPAGE_XCTRLRST	0x1e	/* Cross-Controller Reset (2.3) */
1657 #define	NVME_LOGPAGE_LOSTHOST	0x1f	/* Lost Host Communication (2.3) */
1658 #define	NVME_LOGPAGE_FDPCFG	0x20	/* FDP Configuration (2.1) */
1659 #define	NVME_LOGPAGE_RECLAIM	0x21	/* Reclaim Unit Handle Usage (2.1) */
1660 #define	NVME_LOGPAGE_FDPSTAT	0x22	/* FDP Statistics (2.1) */
1661 #define	NVME_LOGPAGE_FDPEVENT	0x23	/* FDP Events (2.1) */
1662 #define	NVME_LOGPAGE_MFGDEFCFG	0x24	/* Mfg Default Config Status (2.4) */
1663 #define	NVME_LOGPAGE_POWER	0x25	/* Power Measurement (2.3) */
1664 /* 0x26 is reserved */
1665 #define	NVME_LOGPAGE_VOLT	0x27	/* Voltage Measurement (2.4) */
1666 #define	NVME_LOGPAGE_RATELIMIT	0x28	/* Rate Limiting (N1.3) */
1667 
1668 
1669 #define	NVME_LOGPAGE_VEND_MIN	0xc0
1670 #define	NVME_LOGPAGE_VEND_MAX	0xff
1671 
1672 /*
1673  * Supported Log Pages (2.0). There is one entry of an nvme_logsup_t that then
1674  * exists on a per-log basis.
1675  */
1676 
1677 /*
1678  * The NVMe Log Identifier specific parameter field. Currently there is only one
1679  * defined field for the persistent event log (pel).
1680  */
1681 typedef union {
1682 	uint16_t nsl_lidsp;		/* Raw Value */
1683 	struct {			/* Persistent Event Log */
1684 		uint16_t nsl_ec512:1;
1685 		uint16_t nsl_pel_rsvd0p1:15;
1686 	} nsl_pel;
1687 } nvme_suplog_lidsp_t;
1688 
1689 typedef struct {
1690 	uint16_t ns_lsupp:1;
1691 	uint16_t ns_ios:1;
1692 	uint16_t ns_rsvd0p2:14;
1693 	nvme_suplog_lidsp_t ns_lidsp;
1694 } nvme_suplog_t;
1695 
1696 CTASSERT(sizeof (nvme_suplog_lidsp_t) == 2);
1697 CTASSERT(sizeof (nvme_suplog_t) == 4);
1698 
1699 typedef struct {
1700 	nvme_suplog_t	nl_logs[256];
1701 } nvme_suplog_log_t;
1702 
1703 CTASSERT(sizeof (nvme_suplog_log_t) == 1024);
1704 
1705 /*
1706  * SMART / Health information
1707  */
1708 typedef struct {
1709 	uint64_t el_count;		/* Error Count */
1710 	uint16_t el_sqid;		/* Submission Queue ID */
1711 	uint16_t el_cid;		/* Command ID */
1712 	nvme_cqe_sf_t el_sf;		/* Status Field */
1713 	uint8_t	el_byte;		/* Parameter Error Location byte */
1714 	uint8_t	el_bit:3;		/* Parameter Error Location bit */
1715 	uint8_t el_rsvd1:5;
1716 	uint64_t el_lba;		/* Logical Block Address */
1717 	uint32_t el_nsid;		/* Namespace ID */
1718 	uint8_t	el_vendor;		/* Vendor Specific Information avail */
1719 	uint8_t el_trtype;		/* Transport Type (2.0) */
1720 	uint8_t el_csi;			/* Command Set Indicator (2.1) */
1721 	uint8_t el_opc;			/* Opcode (2.1) */
1722 	uint8_t el_csinfo[8];		/* Command Specific Info (1.2) */
1723 	uint16_t el_ttsi;		/* Transport Type Specific Info (1.4) */
1724 	uint8_t el_rsvd2[63 - 42];
1725 	uint8_t el_lpver;		/* Log Page Version (2.1) */
1726 } nvme_error_log_entry_t;
1727 
1728 CTASSERT(sizeof (nvme_error_log_entry_t) == 64);
1729 
1730 typedef struct {
1731 	struct {			/* Critical Warning */
1732 		uint8_t cw_avail:1;	/* available space too low */
1733 		uint8_t cw_temp:1;	/* temperature too high */
1734 		uint8_t cw_reliab:1;	/* degraded reliability */
1735 		uint8_t cw_readonly:1;	/* media is read-only */
1736 		uint8_t cw_volatile:1;	/* volatile memory backup failed */
1737 		uint8_t cw_pmrro:1;	/* Persistent Memory Region R/O (1.4) */
1738 		uint8_t cw_ips:1;	/* Indeterminate Pers. State (2.3) */
1739 		uint8_t cw_rsvd:1;
1740 	} hl_crit_warn;
1741 	uint16_t hl_temp;		/* Temperature */
1742 	uint8_t hl_avail_spare;		/* Available Spare */
1743 	uint8_t hl_avail_spare_thr;	/* Available Spare Threshold */
1744 	uint8_t hl_used;		/* Percentage Used */
1745 	struct {
1746 		uint8_t ecgws_egascbt:1; /* Egrp. Available Spare Low (1.4) */
1747 		uint8_t ecgws_rsvd1:1;
1748 		uint8_t ecgws_egdr:1;	/* Egrp. Degraded Reliability (1.4) */
1749 		uint8_t ecgws_egro:1;	/* Endurance Group Read-Only (1.4) */
1750 		uint8_t ecgws_rsvd:4;
1751 	} hl_ecgcws;
1752 	uint8_t hl_rsvd1[32 - 7];
1753 	nvme_uint128_t hl_data_read;	/* Data Units Read */
1754 	nvme_uint128_t hl_data_write;	/* Data Units Written */
1755 	nvme_uint128_t hl_host_read;	/* Host Read Commands */
1756 	nvme_uint128_t hl_host_write;	/* Host Write Commands */
1757 	nvme_uint128_t hl_ctrl_busy;	/* Controller Busy Time */
1758 	nvme_uint128_t hl_power_cycles;	/* Power Cycles */
1759 	nvme_uint128_t hl_power_on_hours; /* Power On Hours */
1760 	nvme_uint128_t hl_unsafe_shutdn; /* Unsafe Shutdowns */
1761 	nvme_uint128_t hl_media_errors;	/* Media Errors */
1762 	nvme_uint128_t hl_errors_logged; /* Number of errors logged */
1763 	/* Added in NVMe 1.2 */
1764 	uint32_t hl_warn_temp_time;	/* Warning Composite Temp Time */
1765 	uint32_t hl_crit_temp_time;	/* Critical Composite Temp Time */
1766 	uint16_t hl_temp_sensor_1;	/* Temperature Sensor 1 */
1767 	uint16_t hl_temp_sensor_2;	/* Temperature Sensor 2 */
1768 	uint16_t hl_temp_sensor_3;	/* Temperature Sensor 3 */
1769 	uint16_t hl_temp_sensor_4;	/* Temperature Sensor 4 */
1770 	uint16_t hl_temp_sensor_5;	/* Temperature Sensor 5 */
1771 	uint16_t hl_temp_sensor_6;	/* Temperature Sensor 6 */
1772 	uint16_t hl_temp_sensor_7;	/* Temperature Sensor 7 */
1773 	uint16_t hl_temp_sensor_8;	/* Temperature Sensor 8 */
1774 	/* Added in NVMe 1.3 */
1775 	uint32_t hl_tmtemp_1_tc;	/* Thermal Mgmt Temp 1 Transition # */
1776 	uint32_t hl_tmtemp_2_tc;	/* Thermal Mgmt Temp 1 Transition # */
1777 	uint32_t hl_tmtemp_1_time;	/* Time in Thermal Mgmt Temp 1 */
1778 	uint32_t hl_tmtemp_2_time;	/* Time in Thermal Mgmt Temp 2 */
1779 	/* Added in NVMe 2.3 */
1780 	uint64_t hl_olec;		/* Operational Lifetime Energy */
1781 	struct {
1782 		uint16_t ipm_ipmv;	/* Interval Power Measurement */
1783 		uint8_t ipm_ipms:2;	/* Interval Power Measurement Scale */
1784 		uint8_t ipm_rsvd18:2;
1785 		uint8_t ipm_pmt:4;	/* Power Measurement Type */
1786 		uint8_t ipm_rsvd24;
1787 	} hl_ipm;			/* Interval Power Measurement */
1788 	uint8_t hl_rsvd2[512 - 244];
1789 } nvme_health_log_t;
1790 
1791 #ifndef	__CHECKER__
1792 CTASSERT(offsetof(nvme_health_log_t, hl_olec) == 232);
1793 CTASSERT(sizeof (nvme_health_log_t) == 512);
1794 #endif
1795 
1796 /*
1797  * The NVMe spec allows for up to seven firmware slots.
1798  */
1799 #define	NVME_MAX_FWSLOTS	7
1800 
1801 typedef struct {
1802 	/* Active Firmware Slot */
1803 	uint8_t fw_afi:3;
1804 	uint8_t fw_rsvd1:1;
1805 	/* Next Active Firmware Slot */
1806 	uint8_t fw_next:3;
1807 	uint8_t fw_rsvd2:1;
1808 	uint8_t fw_rsvd3[7];
1809 	/* Firmware Revision / Slot */
1810 	char fw_frs[NVME_MAX_FWSLOTS][NVME_FWVER_SZ];
1811 	uint8_t fw_rsvd4[512 - 64];
1812 } nvme_fwslot_log_t;
1813 
1814 /*
1815  * The NVMe spec specifies that the changed namespace list contains up to
1816  * 1024 entries.
1817  */
1818 #define	NVME_NSCHANGE_LIST_SIZE	1024
1819 
1820 typedef struct {
1821 	uint32_t	nscl_ns[NVME_NSCHANGE_LIST_SIZE];
1822 } nvme_nschange_list_t;
1823 
1824 /*
1825  * Commands Supported and Effects log page and information structure. This was
1826  * an optional log page added in NVMe 1.2. NVMe 2.0 added variants of this to
1827  * cover supported features and supported NVMe-MI commands. While these use
1828  * similar structures and information, today we define all three top level items
1829  * as their own structure, but leverage similar information. We reuse the
1830  * nvme_cmdeff_csp_t for the corresponding scope fields for all of them.
1831  */
1832 typedef struct {
1833 	uint8_t cmd_csupp:1;	/* Command supported */
1834 	uint8_t cmd_lbcc:1;	/* Logical block content change */
1835 	uint8_t cmd_ncc:1;	/* Namespace capability change */
1836 	uint8_t cmd_nic:1;	/* Namespace inventory change */
1837 	uint8_t cmd_ccc:1;	/* Controller capability change */
1838 	uint8_t cmd_rsvd5:3;
1839 	uint8_t cmd_rsvd8:6;
1840 	uint8_t cmd_cser:2;	/* Cmd. Submission and Exec Relaxations (2.1) */
1841 	uint16_t cmd_cse:3;	/* Command submission and execution */
1842 	uint16_t cmd_uuid:1;	/* UUID select supported, 1.4 */
1843 	uint16_t cmd_csp:12;	/* Command Scope, 2.0 */
1844 } nvme_cmdeff_t;
1845 
1846 CTASSERT(sizeof (nvme_cmdeff_t) == 4);
1847 
1848 typedef enum {
1849 	NVME_CMDEFF_CSP_NS		= 1 << 0,
1850 	NVME_CMDEFF_CSP_CTRL		= 1 << 1,
1851 	NVME_CMDEFF_CSP_SET		= 1 << 2,
1852 	NVME_CMDEFF_CSP_ENDURANCE	= 1 << 3,
1853 	NVME_CMDEFF_CSP_DOMAIN		= 1 << 4,
1854 	NVME_CMDEFF_CSP_NVM		= 1 << 5,
1855 	/* This is currently only defined for supported features */
1856 	NVME_CMDEFF_CSP_CDQSCP		= 1 << 6
1857 } nvme_cmdeff_csp_t;
1858 
1859 typedef enum {
1860 	NVME_CMDEFF_CSE_NONE	= 0,
1861 	NVME_CMDEFF_CSE_NS,
1862 	NVME_CMDEFF_CSE_CTRL
1863 } nvme_cmdeff_cse_t;
1864 
1865 typedef struct {
1866 	nvme_cmdeff_t	cme_admin[256];
1867 	nvme_cmdeff_t	cme_io[256];
1868 	uint8_t		cme_rsvd2048[2048];
1869 } nvme_cmdeff_log_t;
1870 
1871 CTASSERT(sizeof (nvme_cmdeff_log_t) == 4096);
1872 CTASSERT(offsetof(nvme_cmdeff_log_t, cme_rsvd2048) == 2048);
1873 
1874 typedef struct {
1875 	uint8_t feat_fupp:1;	/* FID supported */
1876 	uint8_t feat_udcc:1;	/* User data content change */
1877 	uint8_t feat_ncc:1;	/* Namespace capability change */
1878 	uint8_t feat_nic:1;	/* Namespace inventory change */
1879 	uint8_t feat_ccc:1;	/* Controller capability change */
1880 	uint8_t feat_rsvd0p5:3;
1881 	uint8_t feat_rsvd1;
1882 	uint16_t feat_rsvd2:3;
1883 	uint16_t feat_uuid:1;	/* UUID select supported */
1884 	uint16_t feat_fsp:12;	/* Command Scope */
1885 } nvme_supfeat_t;
1886 
1887 CTASSERT(sizeof (nvme_supfeat_t) == 4);
1888 
1889 typedef struct {
1890 	nvme_supfeat_t nsl_feats[256];
1891 } nvme_supfeat_log_t;
1892 
1893 CTASSERT(sizeof (nvme_supfeat_log_t) == 1024);
1894 
1895 typedef struct {
1896 	uint8_t smi_csupp:1;	/* Command supported */
1897 	uint8_t smi_lbcc:1;	/* Logical block content change */
1898 	uint8_t smi_ncc:1;	/* Namespace capability change */
1899 	uint8_t smi_nic:1;	/* Namespace inventory change */
1900 	uint8_t smi_ccc:1;	/* Controller capability change */
1901 	uint8_t smi_rsvd0p5:3;
1902 	uint8_t smi_rsvd1;
1903 	uint16_t smi_cse:3;	/* Command submission and execution */
1904 	uint16_t smi_uuid:1;	/* UUID select supported, 1.4 */
1905 	uint16_t smi_csp:12;	/* Command Scope, 2.0 */
1906 } nvme_supmicmd_t;
1907 
1908 CTASSERT(sizeof (nvme_supmicmd_t) == 4);
1909 
1910 typedef struct {
1911 	nvme_supmicmd_t mcl_cmds[256];
1912 	uint8_t mcl_rsvd1024[4096 - 1024];
1913 } nvme_supmicmd_log_t;
1914 
1915 CTASSERT(sizeof (nvme_supmicmd_log_t) == 4096);
1916 
1917 /*
1918  * Persistent Event Log Header. This log was added in NVMe 1.4. It begins with a
1919  * 512 byte header which is defined below. It uses the log specific parameter to
1920  * determine how to access it. Internally the drive contains the notion of a
1921  * context that must be released and accessed.
1922  */
1923 typedef struct {
1924 	uint8_t		pel_lid;	/* Log Identifier */
1925 	uint8_t		pel_rsvd1[3];
1926 	uint32_t	pel_tnev;	/* Total Number of Events */
1927 	uint64_t	pel_tll;	/* Total Log Length */
1928 	uint8_t		pel_lrev;	/* Log Revision */
1929 	uint8_t		pel_rsvd17[1];
1930 	uint16_t	pel_lhl;	/* Log Header Length */
1931 	uint64_t	pel_tstmp;	/* Timestamp */
1932 	nvme_uint128_t	pel_poh;	/* Power on Hours */
1933 	uint64_t	pel_pwrcc;	/* Power Cycle Count */
1934 	uint16_t	pel_vid;	/* PCI Vendor ID */
1935 	uint16_t	pel_ssvid;	/* PCI Subsystem Vendor ID */
1936 	uint8_t		pel_sn[NVME_SERIAL_SZ];	/* Serial Number */
1937 	uint8_t		pel_mn[NVME_MODEL_SZ];	/* Model Number */
1938 	uint8_t		pel_subnqn[372 - 116];	/* NVM Subsystem Qual. Name */
1939 	uint16_t	pel_gnum;	/* Generation Number (2.0) */
1940 	struct {			/* Reporting Context Info (2.0) */
1941 		uint16_t pel_rcpid;	/* Port Identifier */
1942 		uint16_t pel_rcpit:2;	/* Port Identifier Type */
1943 		uint16_t pel_rce:1;	/* Reporting Context Exists */
1944 		uint16_t pel_rsvd19:13;
1945 	} pel_rci;
1946 	uint8_t		pel_rsvd378[480 - 378];
1947 	uint8_t		pel_seb[32];	/* Supported Events Bitmap */
1948 	uint8_t		pel_data[];
1949 } nvme_pev_log_t;
1950 
1951 /*
1952  * This enum represents the bit index for various features in the supported
1953  * events bitmap.
1954  */
1955 typedef enum {
1956 	NVME_SEB_SHLSES	= 1,	/* SMART / Health Log */
1957 	NVME_SEB_FCES = 2,	/* Firmware Commit */
1958 	NVME_SEB_TCES = 3,	/* Timestamp Change */
1959 	NVME_SEB_PRES = 4,	/* Power-on or Reset */
1960 	NVME_SEB_NSHEES = 5,	/* NVM Subsystem Hardware Error */
1961 	NVME_SEB_CNES = 6,	/* Change Namespace */
1962 	NVME_SEB_FNSES = 7,	/* Format NVM Start */
1963 	NVME_SEB_FNCES = 8,	/* Format NVM Completion */
1964 	NVME_SEB_SSES = 9,	/* Sanitize Start */
1965 	NVME_SEB_SCES = 10,	/* Sanitize Completion */
1966 	NVME_SEB_SFES = 11,	/* Set Feature */
1967 	NVME_SEB_TLCES = 12,	/* Telemetry Log Create */
1968 	NVME_SEB_TEES = 13,	/* Thermal Excursion */
1969 	NVME_SEB_SMVES = 14,	/* Sanitize Media Verification (2.1) */
1970 	NVME_SEB_CDPCES = 15,	/* Configurable Device Pers. (2.3) */
1971 	NVME_SEB_VSES = 222,	/* Vendor Specific */
1972 	NVME_SEB_TCG = 223	/* TCG */
1973 } nvme_pev_seb_t;
1974 
1975 /*
1976  * Log specific fields for the persistent event log. These are required by the
1977  * log.
1978  */
1979 typedef enum {
1980 	/*
1981 	 * Read the persistent event log, presumes that a context has already
1982 	 * been established.
1983 	 */
1984 	NVME_PEV_LSP_READ	= 0,
1985 	/*
1986 	 * Establish a new context and then read a portion of the event log. Any
1987 	 * prior existing context must already have been released.
1988 	 */
1989 	NVME_PEV_LSP_EST_CTX_READ,
1990 	/*
1991 	 * Releases the persistent event log context. It is legal for this
1992 	 * context to already have been released.
1993 	 */
1994 	NVME_PEV_LSP_REL_CTX,
1995 	/*
1996 	 * This establishes a context and reads the fixed 512 bytes. The
1997 	 * controller is supposed to ignore any offset and length fields and
1998 	 * always read 512 bytes regardless. This is present starting in NVMe
1999 	 * 2.0.
2000 	 */
2001 	NVME_PEV_LSP_EST_CTX_READ_512
2002 } nvme_pev_log_lsp_t;
2003 
2004 #ifndef __CHECKER__
2005 CTASSERT(sizeof (nvme_pev_log_t) == 512);
2006 CTASSERT(offsetof(nvme_pev_log_t, pel_gnum) == 372);
2007 #endif
2008 
2009 /*
2010  * NVMe Telemetry Header
2011  */
2012 typedef struct {
2013 	uint8_t ntl_lid;
2014 	uint8_t ntl_rsvd1[4];
2015 	uint8_t ntl_ieee[3];
2016 	uint16_t ntl_thda1lb;
2017 	uint16_t ntl_thda2lb;
2018 	uint16_t ntl_thda3lb;
2019 	uint8_t ntl_rsvd14[2];
2020 	uint32_t ntl_thda4lb;
2021 	uint8_t ntl_rsvd20[380 - 20];
2022 	uint8_t ntl_ths;
2023 	uint8_t ntl_thdgn;
2024 	uint8_t ntl_tcda;
2025 	uint8_t ntl_tcdgn;
2026 	uint8_t ntl_rid[512 - 384];
2027 	uint8_t ntl_data[];
2028 } nvme_telemetry_log_t;
2029 
2030 CTASSERT(sizeof (nvme_telemetry_log_t) == 512);
2031 
2032 #define	NVME_TELMCTRL_LSP_CTHID	1
2033 
2034 /*
2035  * Physical Interface Receiver Eye Opening Measurement Log or in a PCIe eye
2036  * diagram. It consists of a header and a variable number of data descriptors.
2037  */
2038 
2039 /*
2040  * Eye Opening Measurement Header
2041  */
2042 typedef struct {
2043 	uint8_t eom_lid;
2044 	uint8_t eom_eomip;
2045 	uint16_t eom_hsize;
2046 	uint32_t eom_rsz;
2047 	uint8_t eom_edgn;
2048 	uint8_t eom_lrev;
2049 	struct {
2050 		uint8_t odp_pefp:1;
2051 		uint8_t odp_edfp:1;
2052 		uint8_t odp_rsvd2:6;
2053 	} eom_odp;
2054 	uint8_t eom_lns;
2055 	uint8_t eom_epl;
2056 	struct {
2057 		uint8_t lspfc_lspfv:7;
2058 		uint8_t lspfc_rsvd1:1;
2059 	} eom_lspfc;
2060 	struct {
2061 		uint8_t linfo_mls:4;
2062 		uint8_t linfo_rsvd4:4;
2063 	} eom_linfo;
2064 	uint8_t eom_rsvd15[18 - 15];
2065 	uint16_t eom_lsic;
2066 	uint32_t eom_ds;
2067 	uint16_t eom_nd;
2068 	uint16_t eom_maxtb;
2069 	uint16_t eom_maxlr;
2070 	uint16_t eom_etgood;
2071 	uint16_t eom_etbetter;
2072 	uint16_t eom_etbest;
2073 	uint8_t eom_rsvd[64 - 36];
2074 } nvme_eom_hdr_t;
2075 
2076 CTASSERT(sizeof (nvme_eom_hdr_t) == 64);
2077 
2078 /*
2079  * Eye Opening Lane Descriptor
2080  */
2081 typedef struct {
2082 	uint8_t eld_rsvd0[1];
2083 	struct {
2084 		uint8_t mstat_mcsc:1;
2085 		uint8_t mstat_rsvd1:7;
2086 	} eld_mstat;
2087 	uint8_t eld_ln;
2088 	uint8_t eld_eye;
2089 	uint16_t eld_top;
2090 	uint16_t eld_btm;
2091 	uint16_t eld_lft;
2092 	uint16_t eld_rgt;
2093 	uint16_t eld_nrows;
2094 	uint16_t eld_ncols;
2095 	/*
2096 	 * In revision 2 (NVMe PCIe 1.1) this field is only a uint16_t. It was
2097 	 * extended to a uint32_t in revision 3 of the log.
2098 	 */
2099 	uint32_t eld_edlen;
2100 	uint8_t eld_rsvd20[32 - 20];
2101 	/*
2102 	 * The Lane Descriptor follows this with three different variable data
2103 	 * regions depending on what bits have been set as present in the
2104 	 * header:
2105 	 *
2106 	 * 1. The first eld_nrows * eld_ncols bytes cover the printable eye.
2107 	 * 2. The next eld_edlen bytes cover any vendor-specific eye data.
2108 	 * 3. The final portion of this is padding to ensure this is dword
2109 	 *    aligned. This is based on the header's eom_ds field.
2110 	 */
2111 	uint8_t eld_data[];
2112 } nvme_eom_lane_desc_t;
2113 
2114 typedef enum {
2115 	NVME_EOM_NOT_STARTED = 0,
2116 	NVME_EOM_IN_PROGRESS,
2117 	NVME_EOM_DONE
2118 } nvme_eom_eomip_t;
2119 
2120 CTASSERT(sizeof (nvme_eom_lane_desc_t) == 32);
2121 
2122 typedef enum {
2123 	NVME_EOM_LSP_MQUAL_GOOD	 = 0,
2124 	NVME_EOM_LSP_MQUAL_BETTER,
2125 	NVME_EOM_LSP_MQUAL_BEST
2126 } nvme_eom_lsp_mqual_t;
2127 
2128 typedef enum {
2129 	/*
2130 	 * Read the current eye opening measurement log. This honors a specific
2131 	 * offset to start reading at. This assumes that a measurement is
2132 	 * already in progress.
2133 	 */
2134 	NVME_EOM_LSP_READ	= 0,
2135 	/*
2136 	 * This causes the controller to abort any in progress measurements,
2137 	 * start a new eye opening measurement, and return the initial requested
2138 	 * data.
2139 	 */
2140 	NVME_EOM_LSP_START	= 1,
2141 	/*
2142 	 * This aborts any in progress measurement and clears the log page.
2143 	 */
2144 	NVME_EOM_LSP_ABORT	= 2
2145 } nvme_eom_lsp_act_t;
2146 
2147 typedef union {
2148 	struct {
2149 		uint8_t nel_mqual:2;
2150 		uint8_t nel_act:2;
2151 	};
2152 	/*
2153 	 * The spec has this fixed as a 7-bit value. We round it up to a uint8_t
2154 	 * here for ABI purposes.
2155 	 */
2156 	uint8_t r;
2157 } nvme_eom_lsp_t;
2158 
2159 /*
2160  * NVMe Format NVM
2161  */
2162 #define	NVME_FRMT_SES_NONE	0
2163 #define	NVME_FRMT_SES_USER	1
2164 #define	NVME_FRMT_SES_CRYPTO	2
2165 #define	NVME_FRMT_MAX_SES	2
2166 
2167 #define	NVME_FRMT_MAX_LBAF	15
2168 
2169 typedef union {
2170 	struct {
2171 		uint32_t fm_lbaf:4;		/* LBA Format */
2172 		uint32_t fm_ms:1;		/* Metadata Settings */
2173 		uint32_t fm_pi:3;		/* Protection Information */
2174 		uint32_t fm_pil:1;		/* Prot. Information Location */
2175 		uint32_t fm_ses:3;		/* Secure Erase Settings */
2176 		uint32_t fm_resvd:20;
2177 	} b;
2178 	uint32_t r;
2179 } nvme_format_nvm_t;
2180 
2181 
2182 /*
2183  * NVMe Get / Set Features
2184  */
2185 #define	NVME_FEAT_ARBITRATION	0x01	/* Command Arbitration */
2186 #define	NVME_FEAT_POWER_MGMT	0x02	/* Power Management */
2187 #define	NVME_FEAT_LBA_RANGE	0x03	/* LBA Range Type */
2188 #define	NVME_FEAT_TEMPERATURE	0x04	/* Temperature Threshold */
2189 #define	NVME_FEAT_ERROR		0x05	/* Error Recovery */
2190 #define	NVME_FEAT_WRITE_CACHE	0x06	/* Volatile Write Cache */
2191 #define	NVME_FEAT_NQUEUES	0x07	/* Number of Queues */
2192 #define	NVME_FEAT_INTR_COAL	0x08	/* Interrupt Coalescing */
2193 #define	NVME_FEAT_INTR_VECT	0x09	/* Interrupt Vector Configuration */
2194 #define	NVME_FEAT_WRITE_ATOM	0x0a	/* Write Atomicity */
2195 #define	NVME_FEAT_ASYNC_EVENT	0x0b	/* Asynchronous Event Configuration */
2196 #define	NVME_FEAT_AUTO_PST	0x0c	/* Autonomous Power State Transition */
2197 					/* (1.1) */
2198 #define	NVME_FEAT_HMB		0x0d	/* Host Memory Buffer (1.2) */
2199 #define	NVME_FEAT_TIMESTAMP	0x0e	/* Timestamp (1.3) */
2200 #define	NVME_FEAT_KEEP_ALIVE	0x0f	/* Keep Alive Timer (1.2) */
2201 #define	NVME_FEAT_HCTM		0x10	/* Host Controlled Thermal Mgmt (1.3) */
2202 #define	NVME_FEAT_NOPSC		0x11	/* Non-op Power State Cfg. (1.3) */
2203 #define	NVME_FEAT_READ_REC	0x12	/* Read Recovery Level Cfg (1.4) */
2204 #define	NVME_FEAT_PLM_CFG	0x13	/* Predictable Lat. Mode Cfg. (1.4) */
2205 #define	NVME_FEAT_PLM_WIN	0x14	/* ^ Window (1.4) */
2206 #define	NVME_FEAT_LBA_STS_ATTR	0x15	/* LBA Status Info Attr (1.4) */
2207 #define	NVME_FEAT_HOST_BEHAVE	0x16	/* Host Behavior (1.4) */
2208 #define	NVME_FEAT_SAN_CFG	0x17	/* Sanitize Config (1.4) */
2209 #define	NVME_FEAT_EGRP_EVENT	0x18	/* Endurance Group Event Config (1.4) */
2210 #define	NVME_FEAT_IO_CMD_SET	0x19	/* I/O Command Set Profile (2.0) */
2211 #define	NVME_FEAT_SPINUP	0x1a	/* Spinup Control (2.0) */
2212 #define	NVME_FEAT_PLS		0x1b	/* Power Loss Signaling (2.1) */
2213 #define	NVME_FEAT_PERF_CHAR	0x1c	/* Perf. Characteristics (N1.1) */
2214 #define	NVME_FEAT_FDP		0x1d	/* Flexible Device Placement (2.1) */
2215 #define	NVME_FEAT_FDP_EVENTS	0x1e	/* ^ Events (2.1) */
2216 #define	NVME_FEAT_NS_LABEL	0x1f	/* Namespace Admin Label (2.1) */
2217 #define	NVME_FEAT_CTRL_DQ	0x21	/* Controller Data Queue (2.1) */
2218 #define	NVME_FEAT_DEV_PERS	0x22	/* Device Personality (2.3) */
2219 #define	NVME_FEAT_POWER_LIMIT	0x23	/* Power Limit (2.3) */
2220 #define	NVME_FEAT_POWER_THRESH	0x24	/* Power Threshold (2.3) */
2221 #define	NVME_FEAT_POWER_MEASURE	0x25	/* Power Measurement (2.3) */
2222 #define	NVME_FEAT_VOLT_THRESH	0x26	/* Voltage Threshold (2.4) */
2223 #define	NVME_FEAT_VOLT_MEASURE	0x27	/* Voltage Measurement (2.4) */
2224 #define	NVME_FEAT_RATE_LIMIT	0x28	/* Rate Limiting (N1.3) */
2225 
2226 #define	NVME_FEAT_ENH_CTRL_META	0x7d	/* Enhanced Controller Metadata (2.0) */
2227 #define	NVME_FEAT_CTRL_META	0x7e	/* Controller Metadata (2.0) */
2228 #define	NVME_FEAT_NS_META	0x7f	/* Namespace Metadata (2.0) */
2229 
2230 #define	NVME_FEAT_PROGRESS	0x80	/* Software Progress Marker */
2231 
2232 #define	NVME_FEAT_VEND_MIN	0xc0
2233 #define	NVME_FEAT_VEND_MAX	0xff
2234 
2235 /*
2236  * This enumeration represents the capabilities in the Get Features select / Set
2237  * Features save options. This was introduced in NVMe 1.1 and the values below
2238  * match the specification. An optional feature in the identify controller data
2239  * structure is set to indicate that this is supported (id_oncs.on_save).
2240  */
2241 typedef enum {
2242 	NVME_FEATURE_SEL_CURRENT	= 0,
2243 	NVME_FEATURE_SEL_DEFAULT,
2244 	NVME_FEATURE_SEL_SAVED,
2245 	NVME_FEATURE_SEL_SUPPORTED
2246 } nvme_feature_sel_t;
2247 
2248 typedef union {
2249 	struct {
2250 		uint32_t gt_fid:8;	/* Feature ID */
2251 		uint32_t gt_sel:3;	/* Select */
2252 		uint32_t gt_rsvd:21;
2253 	} b;
2254 	uint32_t r;
2255 } nvme_get_features_dw10_t;
2256 
2257 typedef union {
2258 	struct {
2259 		uint32_t st_fid:8;	/* Feature ID */
2260 		uint32_t st_rsvd:23;
2261 		uint32_t st_save:1;	/* Select */
2262 	} b;
2263 	uint32_t r;
2264 } nvme_set_features_dw10_t;
2265 
2266 /* Arbitration Feature */
2267 typedef union {
2268 	struct {
2269 		uint8_t arb_ab:3;	/* Arbitration Burst */
2270 		uint8_t arb_rsvd:5;
2271 		uint8_t arb_lpw;	/* Low Priority Weight */
2272 		uint8_t arb_mpw;	/* Medium Priority Weight */
2273 		uint8_t arb_hpw;	/* High Priority Weight */
2274 	} b;
2275 	uint32_t r;
2276 } nvme_arbitration_t;
2277 
2278 /* Power Management Feature */
2279 typedef union {
2280 	struct {
2281 		uint32_t pm_ps:5;	/* Power State */
2282 		uint32_t pm_wh:3;	/* Workload Hint (1.2) */
2283 		uint32_t pm_rsvd:24;
2284 	} b;
2285 	uint32_t r;
2286 } nvme_power_mgmt_t;
2287 
2288 /* LBA Range Type Feature */
2289 typedef union {
2290 	struct {
2291 		uint32_t lr_num:6;	/* Number of LBA ranges */
2292 		uint32_t lr_rsvd:26;
2293 	} b;
2294 	uint32_t r;
2295 } nvme_lba_range_type_t;
2296 
2297 typedef struct {
2298 	uint8_t lr_type;		/* Type */
2299 	struct {			/* Attributes */
2300 		uint8_t lr_write:1;	/* may be overwritten */
2301 		uint8_t lr_hidden:1;	/* hidden from OS/EFI/BIOS */
2302 		uint8_t lr_rsvd1:6;
2303 	} lr_attr;
2304 	uint8_t lr_rsvd2[14];
2305 	uint64_t lr_slba;		/* Starting LBA */
2306 	uint64_t lr_nlb;		/* Number of Logical Blocks */
2307 	uint8_t lr_guid[16];		/* Unique Identifier */
2308 	uint8_t lr_rsvd3[16];
2309 } nvme_lba_range_t;
2310 
2311 #define	NVME_LBA_RANGE_BUFSIZE	4096
2312 
2313 /* Temperature Threshold Feature */
2314 typedef union {
2315 	struct {
2316 		uint16_t tt_tmpth;	/* Temperature Threshold */
2317 		uint16_t tt_tmpsel:4;	/* Temperature Select */
2318 		uint16_t tt_thsel:2;	/* Temperature Type */
2319 		uint16_t tt_tmpthh:3;	/* Threshold Hysteresis (2.1) */
2320 		uint16_t tt_resv:7;
2321 	} b;
2322 	uint32_t r;
2323 } nvme_temp_threshold_t;
2324 
2325 #define	NVME_TEMP_THRESH_MAX_SENSOR	8
2326 #define	NVME_TEMP_THRESH_ALL	0xf
2327 #define	NVME_TEMP_THRESH_OVER	0x00
2328 #define	NVME_TEMP_THRESH_UNDER	0x01
2329 
2330 /* Error Recovery Feature */
2331 typedef union {
2332 	struct {
2333 		uint16_t er_tler;	/* Time-Limited Error Recovery */
2334 		uint16_t er_dulbe:1;	/* Deallocated or Unwritten (1.2) */
2335 		uint16_t er_rsvd:15;
2336 	} b;
2337 	uint32_t r;
2338 } nvme_error_recovery_t;
2339 
2340 /* Volatile Write Cache Feature */
2341 typedef union {
2342 	struct {
2343 		uint32_t wc_wce:1;	/* Volatile Write Cache Enable */
2344 		uint32_t wc_rsvd:31;
2345 	} b;
2346 	uint32_t r;
2347 } nvme_write_cache_t;
2348 
2349 /* Number of Queues Feature */
2350 typedef union {
2351 	struct {
2352 		uint16_t nq_nsq;	/* Number of Submission Queues */
2353 		uint16_t nq_ncq;	/* Number of Completion Queues */
2354 	} b;
2355 	uint32_t r;
2356 } nvme_nqueues_t;
2357 
2358 /* Interrupt Coalescing Feature */
2359 typedef union {
2360 	struct {
2361 		uint8_t ic_thr;		/* Aggregation Threshold */
2362 		uint8_t ic_time;	/* Aggregation Time */
2363 		uint16_t ic_rsvd;
2364 	} b;
2365 	uint32_t r;
2366 } nvme_intr_coal_t;
2367 
2368 /* Interrupt Configuration Features */
2369 typedef union {
2370 	struct {
2371 		uint16_t iv_iv;		/* Interrupt Vector */
2372 		uint16_t iv_cd:1;	/* Coalescing Disable */
2373 		uint16_t iv_rsvd:15;
2374 	} b;
2375 	uint32_t r;
2376 } nvme_intr_vect_t;
2377 
2378 /* Write Atomicity Feature */
2379 typedef union {
2380 	struct {
2381 		uint32_t wa_dn:1;	/* Disable Normal */
2382 		uint32_t wa_rsvd:31;
2383 	} b;
2384 	uint32_t r;
2385 } nvme_write_atomicity_t;
2386 
2387 /* Asynchronous Event Configuration Feature */
2388 typedef union {
2389 	struct {
2390 		uint32_t aec_avail:1;	/* Available space too low */
2391 		uint32_t aec_temp:1;	/* Temperature too high */
2392 		uint32_t aec_reliab:1;	/* Degraded reliability */
2393 		uint32_t aec_readonly:1;	/* Media is read-only */
2394 		uint32_t aec_volatile:1;	/* Volatile mem backup failed */
2395 		uint32_t aec_pmrro:1;	/* Persist Memory Read Only (X.X) */
2396 		uint32_t aec_rsvd1:2;
2397 		uint32_t aec_nsan:1;	/* Namespace attribute notices (1.2) */
2398 		uint32_t aec_fwact:1;	/* Firmware activation notices (1.2) */
2399 		uint32_t aec_telln:1;	/* Telemetry log notices (1.3) */
2400 		uint32_t aec_ansacn:1;	/* Asymm. NS access change (1.4) */
2401 		uint32_t aec_plat:1;	/* Predictable latency ev. agg. (1.4) */
2402 		uint32_t aec_lbasi:1;	/* LBA status information (1.4) */
2403 		uint32_t aec_egeal:1;	/* Endurance group ev. agg. (1.4) */
2404 		uint32_t aec_nnsshdn:1;	/* Normal NVM Subsys Shutdown (2.0) */
2405 		uint32_t aec_tthry:1;	/* Temp Thres Hysteresis (2.1) */
2406 		uint32_t aec_rassn:1;	/* Reachability Association (2.1) */
2407 		uint32_t aec_rgpr0:1;	/* Reachability Group (2.1) */
2408 		uint32_t aec_ansan:1;	/* Allocated Namespace Attr. (2.1) */
2409 		uint32_t aec_ccrun:1;	/* X-Ctrl Reset Compl. Notices (2.3) */
2410 		uint32_t aec_lhcn:1;	/* Lost Host Comms. Notices (2.3) */
2411 		uint32_t aec_rsvd20:5;
2412 		uint32_t aec_zdcn:1;	/* Zone Descriptor Changed (2.0) */
2413 		/* Fabrics Specific */
2414 		uint32_t aec_pmdrlpcn:1;	/* Pull Model Change (2.1) */
2415 		uint32_t aec_adlpcn:1;	/* AVE Discovery Change (2.1) */
2416 		uint32_t aec_hdlpcn:1;	/* Host Discovery Change (2.1) */
2417 		uint32_t aec_dlpcn:1;	/* Discovery Change (2.0) */
2418 	} b;
2419 	uint32_t r;
2420 } nvme_async_event_conf_t;
2421 
2422 CTASSERT(sizeof (nvme_async_event_conf_t) == 4);
2423 
2424 /* Autonomous Power State Transition Feature (1.1) */
2425 typedef union {
2426 	struct {
2427 		uint32_t apst_apste:1;	/* APST enabled */
2428 		uint32_t apst_rsvd:31;
2429 	} b;
2430 	uint32_t r;
2431 } nvme_auto_power_state_trans_t;
2432 
2433 typedef struct {
2434 	uint32_t apst_rsvd1:3;
2435 	uint32_t apst_itps:5;	/* Idle Transition Power State */
2436 	uint32_t apst_itpt:24;	/* Idle Time Prior to Transition */
2437 	uint32_t apst_rsvd2;
2438 } nvme_auto_power_state_t;
2439 
2440 #define	NVME_AUTO_PST_BUFSIZE	256
2441 
2442 /* Host Behavior */
2443 typedef struct {
2444 	uint8_t nhb_acre;	/* Advanced Command Retry (1.4) */
2445 	uint8_t nhb_etdas;	/* Telemetry Area 4 (2.0) */
2446 	uint8_t nhb_lbafee;	/* Extended LBA Formats (2.0) */
2447 	uint8_t nhb_hdisns;	/* Dispersed Namespaces (2.1) */
2448 	uint16_t nhb_cdfe;	/* Copy Descriptor (2.1) */
2449 	uint8_t nhb_rsvd[512 - 6];
2450 } nvme_host_behavior_t;
2451 
2452 CTASSERT(sizeof (nvme_host_behavior_t) == 512);
2453 
2454 /* Software Progress Marker Feature */
2455 typedef union {
2456 	struct {
2457 		uint8_t spm_pbslc;	/* Pre-Boot Software Load Count */
2458 		uint8_t spm_rsvd[3];
2459 	} b;
2460 	uint32_t r;
2461 } nvme_software_progress_marker_t;
2462 
2463 /*
2464  * Firmware Commit - Command Dword 10
2465  */
2466 #define	NVME_FWC_SAVE		0x0	/* Save image only */
2467 #define	NVME_FWC_SAVE_ACTIVATE	0x1	/* Save and activate at next reset */
2468 #define	NVME_FWC_ACTIVATE	0x2	/* Activate slot at next reset */
2469 #define	NVME_FWC_ACTIVATE_IMMED	0x3	/* Activate slot immediately */
2470 
2471 /*
2472  * Firmware slot number is only 3 bits, and zero is not allowed.
2473  * Valid range is 1 to 7.
2474  */
2475 #define	NVME_FW_SLOT_MIN	1U	/* lowest allowable slot number ... */
2476 #define	NVME_FW_SLOT_MAX	7U	/* ... and highest */
2477 
2478 /*
2479  * Some constants to make verification of DWORD variables and arguments easier.
2480  * A DWORD is 4 bytes.
2481  */
2482 #define	NVME_DWORD_SHIFT	2
2483 #define	NVME_DWORD_SIZE		(1 << NVME_DWORD_SHIFT)
2484 #define	NVME_DWORD_MASK		(NVME_DWORD_SIZE - 1)
2485 
2486 /*
2487  * The maximum offset a firmware image segment can be loaded at is the number
2488  * of DWORDS in a 32 bit field. The maximum length of such a segment is the
2489  * same. Expressed in bytes it is:
2490  */
2491 #define	NVME_FW_OFFSETB_MAX	((u_longlong_t)UINT32_MAX << NVME_DWORD_SHIFT)
2492 #define	NVME_FW_LENB_MAX	NVME_FW_OFFSETB_MAX
2493 
2494 typedef union {
2495 	struct {
2496 		uint32_t fc_slot:3;	/* Firmware slot */
2497 		uint32_t fc_action:3;	/* Commit action */
2498 		uint32_t fc_rsvd:26;
2499 	} b;
2500 	uint32_t r;
2501 } nvme_firmware_commit_dw10_t;
2502 
2503 #pragma pack() /* pack(1) */
2504 
2505 /* NVMe completion status code type */
2506 #define	NVME_CQE_SCT_GENERIC	0	/* Generic Command Status */
2507 #define	NVME_CQE_SCT_SPECIFIC	1	/* Command Specific Status */
2508 #define	NVME_CQE_SCT_INTEGRITY	2	/* Media and Data Integrity Errors */
2509 #define	NVME_CQE_SCT_PATH	3	/* Path Related Status (1.4) */
2510 #define	NVME_CQE_SCT_VENDOR	7	/* Vendor Specific */
2511 
2512 /*
2513  * Status code ranges
2514  */
2515 #define	NVME_CQE_SC_GEN_MIN		0x00
2516 #define	NVME_CQE_SC_GEN_MAX		0x7f
2517 #define	NVME_CQE_SC_CSI_MIN		0x80
2518 #define	NVME_CQE_SC_CSI_MAX		0xbf
2519 #define	NVME_CQE_SC_VEND_MIN		0xc0
2520 #define	NVME_CQE_SC_VEND_MAX		0xff
2521 
2522 /* NVMe completion status code (generic) */
2523 #define	NVME_CQE_SC_GEN_SUCCESS		0x0	/* Successful Completion */
2524 #define	NVME_CQE_SC_GEN_INV_OPC		0x1	/* Invalid Command Opcode */
2525 #define	NVME_CQE_SC_GEN_INV_FLD		0x2	/* Invalid Field in Command */
2526 #define	NVME_CQE_SC_GEN_ID_CNFL		0x3	/* Command ID Conflict */
2527 #define	NVME_CQE_SC_GEN_DATA_XFR_ERR	0x4	/* Data Transfer Error */
2528 #define	NVME_CQE_SC_GEN_ABORT_PWRLOSS	0x5	/* Cmds Aborted / Pwr Loss */
2529 #define	NVME_CQE_SC_GEN_INTERNAL_ERR	0x6	/* Internal Error */
2530 #define	NVME_CQE_SC_GEN_ABORT_REQUEST	0x7	/* Command Abort Requested */
2531 #define	NVME_CQE_SC_GEN_ABORT_SQ_DEL	0x8	/* Cmd Aborted / SQ deletion */
2532 #define	NVME_CQE_SC_GEN_ABORT_FUSE_FAIL	0x9	/* Cmd Aborted / Failed Fused */
2533 #define	NVME_CQE_SC_GEN_ABORT_FUSE_MISS	0xa	/* Cmd Aborted / Missing Fusd */
2534 #define	NVME_CQE_SC_GEN_INV_NS		0xb	/* Inval Namespace or Format */
2535 #define	NVME_CQE_SC_GEN_CMD_SEQ_ERR	0xc	/* Command Sequence Error */
2536 #define	NVME_CQE_SC_GEN_INV_SGL_LAST	0xd	/* Inval SGL Last Seg Desc */
2537 #define	NVME_CQE_SC_GEN_INV_SGL_NUM	0xe	/* Inval Number of SGL Desc */
2538 #define	NVME_CQE_SC_GEN_INV_DSGL_LEN	0xf	/* Data SGL Length Invalid */
2539 #define	NVME_CQE_SC_GEN_INV_MSGL_LEN	0x10	/* Metadata SGL Length Inval */
2540 #define	NVME_CQE_SC_GEN_INV_SGL_DESC	0x11	/* SGL Descriptor Type Inval */
2541 /* Added in NVMe 1.2 */
2542 #define	NVME_CQE_SC_GEN_INV_USE_CMB	0x12	/* Inval use of Ctrl Mem Buf */
2543 #define	NVME_CQE_SC_GEN_INV_PRP_OFF	0x13	/* PRP Offset Invalid */
2544 #define	NVME_CQE_SC_GEN_AWU_EXCEEDED	0x14	/* Atomic Write Unit Exceeded */
2545 #define	NVME_CQE_SC_GEN_OP_DENIED	0x15	/* Operation Denied */
2546 #define	NVME_CQE_SC_GEN_INV_SGL_OFF	0x16	/* SGL Offset Invalid */
2547 #define	NVME_CQE_SC_GEN_INV_SGL_ST	0x17	/* SGL Sub type Invalid */
2548 #define	NVME_CQE_SC_GEN_INCON_HOSTID	0x18	/* Host ID Inconsistent fmt */
2549 #define	NVME_CQE_SC_GEN_KA_EXP		0x19	/* Keep Alive Timer Expired */
2550 #define	NVME_CQE_SC_GEN_INV_KA_TO	0x1a	/* Keep Alive Timeout Invalid */
2551 /* Added in NVMe 1.3 */
2552 #define	NVME_CQE_SC_GEN_ABORT_PREEMPT	0x1b	/* Cmd aborted due to preempt */
2553 #define	NVME_CQE_SC_GEN_SANITIZE_FAIL	0x1c	/* Sanitize Failed */
2554 #define	NVME_CQE_SC_GEN_SANITIZING	0x1d	/* Sanitize in Progress */
2555 #define	NVME_CQE_SC_GEN_INV_SGL_GRAN	0x1e	/* SGL Data Block Gran. Inval */
2556 #define	NVME_CQE_SC_GEN_NO_CMD_Q_CMD	0x1f	/* Command not sup for CMB Q */
2557 /* Added in NVMe 1.4 */
2558 #define	NVME_CQE_SC_GEN_NS_RDONLY	0x20	/* Namespace is write prot. */
2559 #define	NVME_CQE_SC_GEN_CMD_INTR	0x21	/* Command Interrupted */
2560 #define	NVME_CQE_SC_GEN_TRANSIENT	0x22	/* Transient Transport Error */
2561 /* Added in NVMe 2.0 */
2562 #define	NVME_CQE_SC_GEN_CMD_LOCK	0x23	/* Command/Feature Lockdown */
2563 #define	NVME_CQE_SC_GEN_ADM_MEDIA_NR	0x24	/* Admin Cmd Media Not Ready */
2564 /* Added in NVMe 2.1 */
2565 #define	NVME_CQE_SC_GEN_INV_KEY		0x25	/* Invalid Key Tag */
2566 #define	NVME_CQE_SC_GEN_HOST_DISPNS_DIS	0x26	/* Host Dispersed NS not en. */
2567 #define	NVME_CQE_SC_GEN_HOSTID_UNINIT	0x27	/* Host Identifier Not Init. */
2568 #define	NVME_CQE_SC_GEN_WRONG_KEY	0x28	/* Incorrect Key */
2569 #define	NVME_CQE_SC_GEN_FDP_DIS		0x29	/* FDP Disabled */
2570 #define	NVME_CQE_SC_GEN_INV_PHL		0x2a	/* Invalid Placement Handle */
2571 /* Added in NVMe 2.3 */
2572 #define	NVME_CQE_SC_GEN_SAN_NS_FAIL	0x2b	/* Sanitize NS failed */
2573 #define	NVME_CQE_SC_GEN_SAN_NS_PROG	0x2c	/* Sanitize NS in progress */
2574 /* Added in NVMe 2.4 */
2575 #define	NVME_CQE_SC_GEN_RSTR_CFG_FAIL	0x2d	/* Failed to restore cfg. */
2576 /* 0x2e was allocated, but has no actual prose in the spec. */
2577 
2578 /* NVMe completion status code (generic NVM commands) */
2579 #define	NVME_CQE_SC_GEN_NVM_LBA_RANGE	0x80	/* LBA Out Of Range */
2580 #define	NVME_CQE_SC_GEN_NVM_CAP_EXC	0x81	/* Capacity Exceeded */
2581 #define	NVME_CQE_SC_GEN_NVM_NS_NOTRDY	0x82	/* Namespace Not Ready */
2582 #define	NVME_CQE_SC_GEN_NVM_RSV_CNFLCT	0x83	/* Reservation Conflict */
2583 #define	NVME_CQE_SC_GEN_NVM_FORMATTING	0x84	/* Format in progress (1.2) */
2584 /* Added in NVMe 2.0 */
2585 #define	NVME_CQE_SC_GEN_KEY_INV_VAL	0x85	/* Invalid value size */
2586 #define	NVME_CQE_SC_GEN_KEY_INV_KEY	0x86	/* Invalid key size */
2587 #define	NVME_CQE_SC_GEN_KEY_ENOENT	0x87	/* KV Key Does Not Exist */
2588 #define	NVME_CQE_SC_GEN_KEY_UNRECOV	0x88	/* Unrecovered Error */
2589 #define	NVME_CQE_SC_GEN_KEY_EXISTS	0x89	/* Key already exists */
2590 
2591 /* NVMe completion status code (command specific) */
2592 #define	NVME_CQE_SC_SPC_INV_CQ		0x0	/* Completion Queue Invalid */
2593 #define	NVME_CQE_SC_SPC_INV_QID		0x1	/* Invalid Queue Identifier */
2594 #define	NVME_CQE_SC_SPC_MAX_QSZ_EXC	0x2	/* Max Queue Size Exceeded */
2595 #define	NVME_CQE_SC_SPC_ABRT_CMD_EXC	0x3	/* Abort Cmd Limit Exceeded */
2596 #define	NVME_CQE_SC_SPC_ASYNC_EVREQ_EXC	0x5	/* Async Event Request Limit */
2597 #define	NVME_CQE_SC_SPC_INV_FW_SLOT	0x6	/* Invalid Firmware Slot */
2598 #define	NVME_CQE_SC_SPC_INV_FW_IMG	0x7	/* Invalid Firmware Image */
2599 #define	NVME_CQE_SC_SPC_INV_INT_VECT	0x8	/* Invalid Interrupt Vector */
2600 #define	NVME_CQE_SC_SPC_INV_LOG_PAGE	0x9	/* Invalid Log Page */
2601 #define	NVME_CQE_SC_SPC_INV_FORMAT	0xa	/* Invalid Format */
2602 #define	NVME_CQE_SC_SPC_FW_RESET	0xb	/* FW Application Reset Reqd */
2603 #define	NVME_CQE_SC_SPC_INV_Q_DEL	0xc	/* Invalid Queue Deletion */
2604 #define	NVME_CQE_SC_SPC_FEAT_SAVE	0xd	/* Feature Id Not Saveable */
2605 #define	NVME_CQE_SC_SPC_FEAT_CHG	0xe	/* Feature Not Changeable */
2606 #define	NVME_CQE_SC_SPC_FEAT_NS_SPEC	0xf	/* Feature Not Namespace Spec */
2607 /* Added in NVMe 1.2 */
2608 #define	NVME_CQE_SC_SPC_FW_NSSR		0x10	/* FW Application NSSR Reqd */
2609 #define	NVME_CQE_SC_SPC_FW_NEXT_RESET	0x11	/* FW Application Next Reqd */
2610 #define	NVME_CQE_SC_SPC_FW_MTFA		0x12	/* FW Application Exceed MTFA */
2611 #define	NVME_CQE_SC_SPC_FW_PROHIBITED	0x13	/* FW Application Prohibited */
2612 #define	NVME_CQE_SC_SPC_FW_OVERLAP	0x14	/* Overlapping FW ranges */
2613 #define	NVME_CQE_SC_SPC_NS_INSUF_CAP	0x15	/* NS Insufficient Capacity */
2614 #define	NVME_CQE_SC_SPC_NS_NO_ID	0x16	/* NS ID Unavailable */
2615 /* 0x17 is reserved */
2616 #define	NVME_CQE_SC_SPC_NS_ATTACHED	0x18	/* NS Already Attached */
2617 #define	NVME_CQE_SC_SPC_NS_PRIV		0x19	/* NS is private */
2618 #define	NVME_CQE_SC_SPC_NS_NOT_ATTACH	0x1a	/* NS Not Attached */
2619 #define	NVME_CQE_SC_SPC_THIN_ENOTSUP	0x1b	/* Thin Provisioning ENOTSUP */
2620 #define	NVME_CQE_SC_SPC_INV_CTRL_LIST	0x1c	/* Controller list invalid */
2621 /* Added in NVMe 1.3 */
2622 #define	NVME_CQE_SC_SPC_SELF_TESTING	0x1d	/* Self-test in progress */
2623 #define	NVME_CQE_SC_SPC_NO_BP_WRITE	0x1e	/* No Boot Partition Write */
2624 #define	NVME_CQE_SC_SPC_INV_CTRL_ID	0x1f	/* Invalid Controller Id */
2625 #define	NVME_CQE_SC_SPC_INV_SEC_CTRL	0x20	/* Invalid Sec. Ctrl state */
2626 #define	NVME_CQE_SC_SPC_INV_CTRL_NRSRC	0x21	/* Inv. # Ctrl Resources */
2627 #define	NVME_CQE_SC_SPC_INV_RSRC_ID	0x22	/* Inv. Resource ID */
2628 /* Added in NVMe 1.4 */
2629 #define	NVME_CQE_SC_SPC_NO_SAN_PMR	0x23	/* Sanitize prohib. w/ pmem */
2630 #define	NVME_CQE_SC_SPC_INV_ANA_GID	0x24	/* Invalid ANA group ID */
2631 #define	NVME_CQE_SC_SPC_ANA_ATTACH	0x25	/* ANA Attach Failed */
2632 /* Added in NVMe 2.0 */
2633 #define	NVME_CQE_SC_SPC_INSUF_CAP	0x26	/* Insufficient Capacity */
2634 #define	NVME_CQE_SC_SPC_NS_ATTACH_LIM	0x27	/* NS Attach Limit Exceeded */
2635 #define	NVME_CQE_SC_SPC_LOCKDOWN_UNSUP	0x28	/* Prohib Cmd Exec Not Sup */
2636 #define	NVME_CQE_SC_SPC_UNSUP_IO_CMD	0x29	/* I/O Command set not sup */
2637 #define	NVME_CQE_SC_SPC_DIS_IO_CMD	0x2a	/* I/O Command set not enab */
2638 #define	NVME_CQE_SC_SPC_INV_CMD_COMBO	0x2b	/* I/O command set combo rej */
2639 #define	NVME_CQE_SC_SPC_INV_IO_CMD	0x2c	/* Invalid I/O command set */
2640 #define	NVME_CQE_SC_SPC_UNAVAIL_ID	0x2d	/* Unavailable ID */
2641 /* Added in NVMe 2.1 */
2642 #define	NVME_CQE_SC_SPC_DISPERSE_NS	0x2e	/* Namespace is dispersed */
2643 #define	NVME_CQE_SC_SPC_INV_DISC	0x2f	/* Invalid Discovery */
2644 #define	NVME_CQE_SC_SPC_ZONE_LOCKED	0x30	/* Zoning Data Struct. Locked */
2645 #define	NVME_CQE_SC_SPC_ZONE_ENOENT	0x31	/* Zoning DS Not Found */
2646 #define	NVME_CQE_SC_SPC_DISC_RSRCS	0x32	/* Insufficient Disc. Rsrcs */
2647 #define	NVME_CQE_SC_SPC_FUNC_DIS	0x33	/* Requested Func. Disabled */
2648 #define	NVME_CQE_SC_SPC_INV_ZGRP_ORIG	0x34	/* ZoneGroup Originator Inv. */
2649 #define	NVME_CQE_SC_SPC_INV_HOST	0x35	/* Invalid Host */
2650 #define	NVME_CQE_SC_SPC_INV_NVM_SYS	0x36	/* Invalid NVM Subsystem */
2651 #define	NVME_CQE_SC_SPC_INV_CTL_DQ	0x37	/* Invalid Controller Data Q. */
2652 #define	NVME_CQE_SC_SPC_ERSRC		0x38	/* Not Enough Resources */
2653 #define	NVME_CQE_SC_SPC_CTL_SUSPEND	0x39	/* Controller Suspended */
2654 #define	NVME_CQE_SC_SPC_CTL_NOT_SUSPEND	0x3a	/* Controller Not Suspended */
2655 #define	NVME_CQE_SC_SPC_CTL_DQ_FULL	0x3b	/* Controller Data Queue Full */
2656 /* Added in NVMe 2.3 */
2657 #define	NVME_CQE_SC_SPC_MAX_NS_SAN	0x3c	/* Req Exceeds Max NS in San */
2658 #define	NVME_CQE_SC_SPC_REQ_DEF_PERS	0x3d	/* Default Personality Req. */
2659 #define	NVME_CQE_SC_SPC_INV_PLIMIT	0x3e	/* Invalid Power Limit */
2660 #define	NVME_CQE_SC_SPC_XCTL_RST_PROG	0x3f	/* X-Ctrl Reset in Progress */
2661 #define	NVME_CQE_SC_SPC_XCTL_RST_LPF	0x40	/* X-Ctrl Log Page Full */
2662 #define	NVME_CQE_SC_SPC_XCTL_RST_LIM	0x41	/* X-Ctrl Reset Limit */
2663 /* Added in NVMe 2.4 */
2664 #define	NVME_CQE_SC_SPC_CTL_ACTIVE	0x42	/* Controller Active */
2665 #define	NVME_CQE_SC_SPC_INV_EXP_ASSOC	0x43	/* Invalid Exported Assoc. */
2666 #define	NVME_CQE_SC_SPC_INV_EXP_CFG	0x44	/* Inv. Exported Cfg. State */
2667 
2668 /* NVMe completion status code (I/O command specific) */
2669 #define	NVME_CQE_SC_SPC_NVM_CNFL_ATTR	0x80	/* Conflicting Attributes */
2670 #define	NVME_CQE_SC_SPC_NVM_INV_PROT	0x81	/* Invalid Protection */
2671 #define	NVME_CQE_SC_SPC_NVM_READONLY	0x82	/* Write to Read Only Range */
2672 /* Added in NVMe 2.0 */
2673 #define	NVME_CQE_SC_SPC_IO_LIMIT	0x83	/* Cmd Size Limit Exceeded */
2674 /* Added in NVMe 2.1 */
2675 #define	NVME_CQE_SC_SPC_INV_CID		0x84	/* Invalid Command ID */
2676 #define	NVME_CQE_SC_SPC_INCOMPAT_NS	0x85	/* Incompatible NS or FMT */
2677 #define	NVME_CQE_SC_SPC_NO_FAST_COPY	0x86	/* Fast Copy Not Possible */
2678 #define	NVME_CQE_SC_SPC_OVLP_IO		0x87	/* Overlapping I/O Range */
2679 #define	NVME_CQE_SC_SPC_NS_UNREACH	0x88	/* Namespace Not reachable */
2680 #define	NVME_CQE_SC_SPC_INSUF_RSRC	0x89	/* Insufficient Resources */
2681 #define	NVME_CQE_SC_SPC_INSUF_PROG	0x8a	/* Insufficient Prog Rsrcs. */
2682 #define	NVME_CQE_SC_SPC_INV_MEM_NS	0x8b	/* Invalid Memory NS */
2683 #define	NVME_CQE_SC_SPC_INV_MEM_RANGE	0x8c	/* Invalid Memory Range Set */
2684 #define	NVME_CQE_SC_SPC_INV_MEM_SETID	0x8d	/* Invalid Memory Range SetId */
2685 #define	NVME_CQE_SC_SPC_INV_PROG_DATA	0x8e	/* Invalid Program Data */
2686 #define	NVME_CQE_SC_SPC_INV_PROG_IDX	0x8f	/* Invalid Program Index */
2687 #define	NVME_CQE_SC_SPC_INV_PROG_TYPE	0x90	/* Invalid Program Type */
2688 #define	NVME_CQE_SC_SPC_MAX_MEM_RANGE	0x91	/* Max. Memory Range Exceeded */
2689 #define	NVME_CQE_SC_SPC_MAX_MEM_SETIDS	0x92	/* Max. Mem Sets Ids Exceeded */
2690 #define	NVME_CQE_SC_SPC_MAX_PROG_ACT	0x93	/* Maximum Programs Activated */
2691 #define	NVME_CQE_SC_SPC_MAX_PROG_BYTES	0x94	/* Max. Prog Bytes Exceeded */
2692 #define	NVME_CQE_SC_SPC_MEM_SET_IN_USE	0x95	/* Memory Range Set In Use */
2693 #define	NVME_CQE_SC_SPC_NO_PROG		0x96	/* No Program */
2694 #define	NVME_CQE_SC_SPC_OVLP_MEM_RANGE	0x97	/* Overlap Memory Ranges */
2695 #define	NVME_CQE_SC_SPC_PROG_NOT_ACT	0x98	/* Program not activated */
2696 #define	NVME_CQE_SC_SPC_PROG_IN_USE	0x99	/* Program in Use */
2697 #define	NVME_CQE_SC_SPC_PROG_IDX_NO_DL	0x9a	/* Program index not dl */
2698 #define	NVME_CQE_SC_SPC_PROG_2BIG	0x9b	/* Program Too Big */
2699 #define	NVME_CQE_SC_SPC_MEDIA_VERIF	0x9c	/* Success. Media Verif Read */
2700 /* 0x9d to 0xb7 are reserved */
2701 #define	NVME_CQE_SC_SPC_ZONE_BDRY_ERR	0xb8	/* Zoned Boundary Error */
2702 #define	NVME_CQE_SC_SPC_ZONE_FULL	0xb9	/* Zone is Full */
2703 #define	NVME_CQE_SC_SPC_ZONE_RDONLY	0xba	/* Zone is Read Only */
2704 #define	NVME_CQE_SC_SPC_ZONE_OFFLINE	0xbb	/* Zone is Offline */
2705 #define	NVME_CQE_SC_SPC_ZONE_INV_WRITE	0xbc	/* Zone Invalid Write */
2706 #define	NVME_CQE_SC_SPC_ZONE_ACT	0xbd	/* Too May Active Zones */
2707 #define	NVME_CQE_SC_SPC_ZONE_OPEN	0xbe	/* Too May Open Zones */
2708 #define	NVME_CQE_SC_SPC_INV_ZONE_TRANS	0xbf	/* Invalid Zone State Trans */
2709 
2710 /* NVMe completion status code (data / metadata integrity) */
2711 #define	NVME_CQE_SC_INT_NVM_WRITE	0x80	/* Write Fault */
2712 #define	NVME_CQE_SC_INT_NVM_READ	0x81	/* Unrecovered Read Error */
2713 #define	NVME_CQE_SC_INT_NVM_GUARD	0x82	/* Guard Check Error */
2714 #define	NVME_CQE_SC_INT_NVM_APPL_TAG	0x83	/* Application Tag Check Err */
2715 #define	NVME_CQE_SC_INT_NVM_REF_TAG	0x84	/* Reference Tag Check Err */
2716 #define	NVME_CQE_SC_INT_NVM_COMPARE	0x85	/* Compare Failure */
2717 #define	NVME_CQE_SC_INT_NVM_ACCESS	0x86	/* Access Denied */
2718 /* Added in 1.2 */
2719 #define	NVME_CQE_SC_INT_NVM_DEALLOC	0x87	/* Dealloc Log Block */
2720 /* Added in 2.0 */
2721 #define	NVME_CQE_SC_INT_NVM_TAG		0x88	/* End-to-End Storage Tag Err */
2722 
2723 /* NVMe completion status code (path related) */
2724 /* Added in NVMe 1.4 */
2725 #define	NVME_CQE_SC_PATH_INT_ERR	0x00	/* Internal Path Error */
2726 #define	NVME_CQE_SC_PATH_AA_PLOSS	0x01	/* Asym Access Pers Loss */
2727 #define	NVME_CQE_SC_PATH_AA_INACC	0x02	/* Asym Access Inaccessible */
2728 #define	NVME_CQE_SC_PATH_AA_TRANS	0x03	/* Asym Access Transition */
2729 #define	NVME_CQE_SC_PATH_CTRL_ERR	0x60	/* Controller Path Error */
2730 #define	NVME_CQE_SC_PATH_HOST_ERR	0x70	/* Host Path Error */
2731 #define	NVME_CQE_SC_PATH_HOST_ABRT	0x71	/* Cmd aborted by host */
2732 
2733 /*
2734  * Controller information (NVME_IOC_CTRL_INFO). This is a consolidation of misc.
2735  * information that we want to know about a controller.
2736  */
2737 typedef struct {
2738 	nvme_ioctl_common_t nci_common;
2739 	nvme_identify_ctrl_t nci_ctrl_id;
2740 	nvme_identify_nsid_t nci_common_ns;
2741 	nvme_version_t nci_vers;
2742 	nvme_capabilities_t nci_caps;
2743 	uint32_t nci_nintrs;
2744 } nvme_ioctl_ctrl_info_t;
2745 
2746 /*
2747  * NVME namespace states.
2748  *
2749  * The values are defined entirely by the driver. Some states correspond to
2750  * namespace states described by the NVMe specification r1.3 section 6.1, others
2751  * are specific to the implementation of this driver. These are present in the
2752  * nvme_ns_kinfo_t that is used with the NVME_IOC_NS_INFO ioctl. Devices that
2753  * support Namespace Management have the ability to transition through these
2754  * states directly. Devices without it may be able to have namespaces in these
2755  * states depending on the version.
2756  *
2757  * The states are as follows:
2758  * - UNALLOCATED: The namespace ID exists, but has no corresponding NVM
2759  *   allocation as per the NVMe spec. It leaves this state with an NVMe
2760  *   Namespace Management NS create command: NVME_IOC_NS_CREATE.
2761  *
2762  * - ALLOCATED: The namespace exists in the controller as per the NVMe spec. It
2763  *   becomes ACTIVE (or IGNORED) by performing a controller attach comand:
2764  *   NVME_IOC_CTRL_ATTACH. It becomes unallocated by performing an NVMe
2765  *   Namespace Management NS delete command: NVME_IOC_NS_DELETE.
2766  *
2767  * - ACTIVE: The namespace exists and is attached to this controller as per the
2768  *   NVMe spec. From the hardware's perspective the namespace is usable.
2769  *
2770  *   Not all namespaces are supported by the kernel. For example, a namespace
2771  *   may use features that the NVMe device driver does not support such as
2772  *   end-to-end data protection features or a different command set.
2773  *
2774  *   When a namespace enters the active state, we will immediately evaluate
2775  *   whether or not we can support a block device (via blkdev(4D)) on this
2776  *   namespace. If we can, then we will immediately advance to the NOT_IGNORED
2777  *   state. Otherwise, to transition to the NOT_IGNORED state, the namespace
2778  *   must be formatted with the FORMAT NVM command with supported settings. The
2779  *   namespace can transition back to the ALLOCATED state by performing a
2780  *   NVME_IOC_CTRL_DETACH ioctl.
2781  *
2782  * - NOT_IGNORED: The namespace is active from the controller perspective and is
2783  *   formatted with settings that would support blkdev(4D) being attached;
2784  *   however, there is no blkdev(4D) instance currently attached. A device
2785  *   transitions from the NOT_IGNORED to the ATTACHED state by actively
2786  *   attaching a blkdev(4D) instance to the namespace through the
2787  *   NVME_IOC_BD_ATTACH ioctl. A namespace can transition back to the ACTIVE
2788  *   state by issuing a FORMAT NVM command with unsupported settings. It can
2789  *   also go to the ALLOCATED state by performing the NVME_IOC_CTRL_DETACH
2790  *   ioctl.
2791  *
2792  * - ATTACHED: the driver has attached a blkdev(4D) instance to this namespace
2793  *   and it is usable as a block device. Certain operations such as a FORMAT NVM
2794  *   or similar are rejected during this state. The device can go back to ACTIVE
2795  *   with the NVME_IOC_BD_DETACH ioctl.
2796  */
2797 typedef enum {
2798 	NVME_NS_STATE_UNALLOCATED = 0,
2799 	NVME_NS_STATE_ALLOCATED,
2800 	NVME_NS_STATE_ACTIVE,
2801 	NVME_NS_STATE_NOT_IGNORED,
2802 	NVME_NS_STATE_ATTACHED
2803 } nvme_ns_state_t;
2804 
2805 #define	NVME_NS_NSTATES	5
2806 
2807 /*
2808  * This is the maximum length of the NVMe namespace's blkdev address. This is
2809  * only valid in the structure with the NVME_NS_STATE_ATTACHED flag is set.
2810  * Otherwise the entry will be all zeros. This is useful when you need to
2811  * determine what the corresponding blkdev instance in libdevinfo for the
2812  * device.
2813  */
2814 #define	NVME_BLKDEV_NAMELEN	128
2815 
2816 /*
2817  * Namespace Information (NVME_IOC_NS_INFO).
2818  */
2819 typedef struct {
2820 	nvme_ioctl_common_t nni_common;
2821 	nvme_ns_state_t	nni_state;
2822 	char nni_addr[NVME_BLKDEV_NAMELEN];
2823 	nvme_identify_nsid_t nni_id;
2824 } nvme_ioctl_ns_info_t;
2825 
2826 /*
2827  * NVMe Command Set Identifiers. This was added in NVMe 2.0, but in all the
2828  * places it was required to be specified, the default value of 0 indicates the
2829  * traditional NVM command set.
2830  */
2831 typedef enum {
2832 	NVME_CSI_NVM	= 0,
2833 	NVME_CSI_KV,
2834 	NVME_CSI_ZNS
2835 } nvme_csi_t;
2836 
2837 #ifdef __cplusplus
2838 }
2839 #endif
2840 
2841 #endif /* _SYS_NVME_H */
2842