xref: /illumos-gate/usr/src/uts/common/sys/nvme.h (revision 19f828df)
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 2021 Oxide Computer Company
17  */
18 
19 #ifndef _SYS_NVME_H
20 #define	_SYS_NVME_H
21 
22 #include <sys/types.h>
23 
24 #ifdef _KERNEL
25 #include <sys/types32.h>
26 #else
27 #include <stdint.h>
28 #endif
29 
30 /*
31  * Declarations used for communication between nvmeadm(1M) and nvme(7D)
32  */
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 /*
39  * NVMe ioctl definitions
40  */
41 
42 #define	NVME_IOC			(('N' << 24) | ('V' << 16) | ('M' << 8))
43 #define	NVME_IOC_IDENTIFY_CTRL		(NVME_IOC | 1)
44 #define	NVME_IOC_IDENTIFY_NSID		(NVME_IOC | 2)
45 #define	NVME_IOC_CAPABILITIES		(NVME_IOC | 3)
46 #define	NVME_IOC_GET_LOGPAGE		(NVME_IOC | 4)
47 #define	NVME_IOC_GET_FEATURES		(NVME_IOC | 5)
48 #define	NVME_IOC_INTR_CNT		(NVME_IOC | 6)
49 #define	NVME_IOC_VERSION		(NVME_IOC | 7)
50 #define	NVME_IOC_FORMAT			(NVME_IOC | 8)
51 #define	NVME_IOC_DETACH			(NVME_IOC | 9)
52 #define	NVME_IOC_ATTACH			(NVME_IOC | 10)
53 #define	NVME_IOC_FIRMWARE_DOWNLOAD	(NVME_IOC | 11)
54 #define	NVME_IOC_FIRMWARE_COMMIT	(NVME_IOC | 12)
55 #define	NVME_IOC_PASSTHRU		(NVME_IOC | 13)
56 #define	NVME_IOC_MAX			NVME_IOC_PASSTHRU
57 
58 #define	IS_NVME_IOC(x)			((x) > NVME_IOC && (x) <= NVME_IOC_MAX)
59 #define	NVME_IOC_CMD(x)			((x) & 0xff)
60 
61 typedef struct {
62 	size_t		n_len;
63 	uintptr_t	n_buf;
64 	uint64_t	n_arg;
65 } nvme_ioctl_t;
66 
67 #ifdef _KERNEL
68 typedef struct {
69 	size32_t	n_len;
70 	uintptr32_t	n_buf;
71 	uint64_t	n_arg;
72 } nvme_ioctl32_t;
73 #endif
74 
75 /*
76  * NVMe capabilities
77  */
78 typedef struct {
79 	uint32_t mpsmax;		/* Memory Page Size Maximum */
80 	uint32_t mpsmin;		/* Memory Page Size Minimum */
81 } nvme_capabilities_t;
82 
83 /*
84  * NVMe version
85  */
86 typedef struct {
87 	uint16_t v_minor;
88 	uint16_t v_major;
89 } nvme_version_t;
90 
91 #define	NVME_VERSION_ATLEAST(v, maj, min) \
92 	(((v)->v_major) > (maj) || \
93 	((v)->v_major == (maj) && (v)->v_minor >= (min)))
94 
95 #define	NVME_VERSION_HIGHER(v, maj, min) \
96 	(((v)->v_major) > (maj) || \
97 	((v)->v_major == (maj) && (v)->v_minor > (min)))
98 
99 
100 #pragma pack(1)
101 
102 typedef struct {
103 	uint64_t lo;
104 	uint64_t hi;
105 } nvme_uint128_t;
106 
107 /*
108  * NVMe Identify data structures
109  */
110 
111 #define	NVME_IDENTIFY_BUFSIZE	4096	/* buffer size for Identify */
112 
113 /* NVMe Queue Entry Size bitfield */
114 typedef struct {
115 	uint8_t qes_min:4;		/* minimum entry size */
116 	uint8_t qes_max:4;		/* maximum entry size */
117 } nvme_idctl_qes_t;
118 
119 /* NVMe Power State Descriptor */
120 typedef struct {
121 	uint16_t psd_mp;		/* Maximum Power */
122 	uint8_t psd_rsvd1;
123 	uint8_t psd_mps:1;		/* Max Power Scale (1.1) */
124 	uint8_t psd_nops:1;		/* Non-Operational State (1.1) */
125 	uint8_t psd_rsvd2:6;
126 	uint32_t psd_enlat;		/* Entry Latency */
127 	uint32_t psd_exlat;		/* Exit Latency */
128 	uint8_t psd_rrt:5;		/* Relative Read Throughput */
129 	uint8_t psd_rsvd3:3;
130 	uint8_t psd_rrl:5;		/* Relative Read Latency */
131 	uint8_t psd_rsvd4:3;
132 	uint8_t psd_rwt:5;		/* Relative Write Throughput */
133 	uint8_t	psd_rsvd5:3;
134 	uint8_t psd_rwl:5;		/* Relative Write Latency */
135 	uint8_t psd_rsvd6:3;
136 	uint16_t psd_idlp;		/* Idle Power (1.2) */
137 	uint8_t psd_rsvd7:6;
138 	uint8_t psd_ips:2;		/* Idle Power Scale (1.2) */
139 	uint8_t psd_rsvd8;
140 	uint16_t psd_actp;		/* Active Power (1.2) */
141 	uint8_t psd_apw:3;		/* Active Power Workload (1.2) */
142 	uint8_t psd_rsvd9:3;
143 	uint8_t psd_aps:2;		/* Active Power Scale */
144 	uint8_t psd_rsvd10[9];
145 } nvme_idctl_psd_t;
146 
147 #define	NVME_SERIAL_SZ	20
148 #define	NVME_MODEL_SZ	40
149 
150 /* NVMe Identify Controller Data Structure */
151 typedef struct {
152 	/* Controller Capabilities & Features */
153 	uint16_t id_vid;		/* PCI vendor ID */
154 	uint16_t id_ssvid;		/* PCI subsystem vendor ID */
155 	char id_serial[NVME_SERIAL_SZ];	/* Serial Number */
156 	char id_model[NVME_MODEL_SZ];	/* Model Number */
157 	char id_fwrev[8];		/* Firmware Revision */
158 	uint8_t id_rab;			/* Recommended Arbitration Burst */
159 	uint8_t id_oui[3];		/* vendor IEEE OUI */
160 	struct {			/* Multi-Interface Capabilities */
161 		uint8_t m_multi_pci:1;	/* HW has multiple PCIe interfaces */
162 		uint8_t m_multi_ctrl:1; /* HW has multiple controllers (1.1) */
163 		uint8_t m_sr_iov:1;	/* Controller is SR-IOV virt fn (1.1) */
164 		uint8_t m_anar_sup:1;	/* ANA Reporting Supported (1.4) */
165 		uint8_t m_rsvd:4;
166 	} id_mic;
167 	uint8_t	id_mdts;		/* Maximum Data Transfer Size */
168 	uint16_t id_cntlid;		/* Unique Controller Identifier (1.1) */
169 	/* Added in NVMe 1.2 */
170 	uint32_t id_ver;		/* Version (1.2) */
171 	uint32_t id_rtd3r;		/* RTD3 Resume Latency (1.2) */
172 	uint32_t id_rtd3e;		/* RTD3 Entry Latency (1.2) */
173 	struct {
174 		uint32_t oaes_rsvd0:8;
175 		uint32_t oaes_nsan:1;	/* Namespace Attribute Notices (1.2) */
176 		uint32_t oaes_fwact:1;	/* Firmware Activation Notices (1.2) */
177 		uint32_t oaes_rsvd1:1;
178 		uint32_t oaes_ansacn:1;	/* Asymmetric NS Access Change (1.4) */
179 		uint32_t oaes_plat:1;	/* Predictable Lat Event Agg. (1.4) */
180 		uint32_t oaes_lbasi:1;	/* LBA Status Information (1.4) */
181 		uint32_t oaes_egeal:1;	/* Endurance Group Event Agg. (1.4) */
182 		uint32_t oaes_rsvd2:17;
183 	} id_oaes;
184 	struct {
185 		uint32_t ctrat_hid:1;	/* 128-bit Host Identifier (1.2)  */
186 		uint32_t ctrat_nops:1;	/* Non-Operational Power State (1.3) */
187 		uint32_t ctrat_nvmset:1; /* NVMe Sets (1.4) */
188 		uint32_t ctrat_rrl:1;	/* Read Recovery Levels (1.4) */
189 		uint32_t ctrat_engrp:1; /* Endurance Groups (1.4) */
190 		uint32_t ctrat_plm:1;	/* Predictable Latency Mode (1.4) */
191 		uint32_t ctrat_tbkas:1;	/* Traffic Based Keep Alive (1.4) */
192 		uint32_t ctrat_nsg:1;	/* Namespace Granularity (1.4) */
193 		uint32_t ctrat_sqass:1;	/* SQ Associations (1.4) */
194 		uint32_t ctrat_uuid:1;	/* UUID List (1.4) */
195 		uint32_t ctrat_rsvd:22;
196 	} id_ctratt;
197 	uint16_t id_rrls;		/* Read Recovery Levels (1.4) */
198 	uint8_t id_rsvd_cc[111-102];
199 	uint8_t id_cntrltype;		/* Controller Type (1.4) */
200 	uint8_t id_frguid[16];		/* FRU GUID (1.3) */
201 	uint16_t id_crdt1;		/* Command Retry Delay Time 1 (1.4) */
202 	uint16_t id_crdt2;		/* Command Retry Delay Time 2 (1.4) */
203 	uint16_t id_crdt3;		/* Command Retry Delay Time 3 (1.4) */
204 	uint8_t id_rsvd2_cc[240 - 134];
205 	uint8_t id_rsvd_nvmemi[253 - 240];
206 	/* NVMe-MI region */
207 	struct {			/* NVMe Subsystem Report */
208 		uint8_t nvmsr_nvmesd:1;	/* NVMe Storage Device */
209 		uint8_t nvmsr_nvmee:1;	/* NVMe Enclosure */
210 		uint8_t nvmsr_rsvd:6;
211 	} id_nvmsr;
212 	struct {			/* VPD Write Cycle Information */
213 		uint8_t vwci_crem:7;	/* Write Cycles Remaining */
214 		uint8_t vwci_valid:1;	/* Write Cycles Remaining Valid */
215 	} id_vpdwc;
216 	struct {			/* Management Endpoint Capabilities */
217 		uint8_t mec_smbusme:1;	/* SMBus Port Management Endpoint */
218 		uint8_t mec_pcieme:1;	/* PCIe Port Management Endpoint */
219 		uint8_t mec_rsvd:6;
220 	} id_mec;
221 
222 	/* Admin Command Set Attributes */
223 	struct {			/* Optional Admin Command Support */
224 		uint16_t oa_security:1;	/* Security Send & Receive */
225 		uint16_t oa_format:1;	/* Format NVM */
226 		uint16_t oa_firmware:1;	/* Firmware Activate & Download */
227 		uint16_t oa_nsmgmt:1;	/* Namespace Management (1.2) */
228 		uint16_t oa_selftest:1;	/* Self Test (1.3) */
229 		uint16_t oa_direct:1;	/* Directives (1.3) */
230 		uint16_t oa_nvmemi:1;	/* MI-Send/Recv (1.3) */
231 		uint16_t oa_virtmgmt:1;	/* Virtualization Management (1.3) */
232 		uint16_t oa_doorbell:1;	/* Doorbell Buffer Config (1.3) */
233 		uint16_t oa_lbastat:1;	/* LBA Status (1.4) */
234 		uint16_t oa_rsvd:6;
235 	} id_oacs;
236 	uint8_t	id_acl;			/* Abort Command Limit */
237 	uint8_t id_aerl;		/* Asynchronous Event Request Limit */
238 	struct {			/* Firmware Updates */
239 		uint8_t fw_readonly:1;	/* Slot 1 is Read-Only */
240 		uint8_t	fw_nslot:3;	/* number of firmware slots */
241 		uint8_t fw_norst:1;	/* Activate w/o reset (1.2) */
242 		uint8_t fw_rsvd:3;
243 	} id_frmw;
244 	struct {			/* Log Page Attributes */
245 		uint8_t lp_smart:1;	/* SMART/Health information per NS */
246 		uint8_t lp_cmdeff:1;	/* Command Effects (1.2) */
247 		uint8_t lp_extsup:1;	/* Extended Get Log Page (1.2) */
248 		uint8_t lp_telemetry:1;	/* Telemetry Log Pages (1.3) */
249 		uint8_t lp_persist:1;	/* Persistent Log Page (1.4) */
250 		uint8_t lp_rsvd:3;
251 	} id_lpa;
252 	uint8_t id_elpe;		/* Error Log Page Entries */
253 	uint8_t	id_npss;		/* Number of Power States */
254 	struct {			/* Admin Vendor Specific Command Conf */
255 		uint8_t av_spec:1;	/* use format from spec */
256 		uint8_t av_rsvd:7;
257 	} id_avscc;
258 	struct {			/* Autonomous Power State Trans (1.1) */
259 		uint8_t ap_sup:1;	/* APST supported (1.1) */
260 		uint8_t ap_rsvd:7;
261 	} id_apsta;
262 	uint16_t ap_wctemp;		/* Warning Composite Temp. (1.2) */
263 	uint16_t ap_cctemp;		/* Critical Composite Temp. (1.2) */
264 	uint16_t ap_mtfa;		/* Maximum Firmware Activation (1.2) */
265 	uint32_t ap_hmpre;		/* Host Memory Buf Pref Size (1.2) */
266 	uint32_t ap_hmmin;		/* Host Memory Buf Min Size (1.2) */
267 	nvme_uint128_t ap_tnvmcap;	/* Total NVM Capacity in Bytes (1.2) */
268 	nvme_uint128_t ap_unvmcap;	/* Unallocated NVM Capacity (1.2) */
269 	struct {			/* Replay Protected Mem. Block (1.2) */
270 		uint32_t rpmbs_units:3;	/* Number of targets */
271 		uint32_t rpmbs_auth:3;	/* Auth method */
272 		uint32_t rpmbs_rsvd:10;
273 		uint32_t rpmbs_tot:8;	/* Total size in 128KB */
274 		uint32_t rpmbs_acc:8;	/* Access size in 512B */
275 	} ap_rpmbs;
276 	/* Added in NVMe 1.3 */
277 	uint16_t ap_edstt;		/* Ext. Device Self-test time (1.3) */
278 	struct {			/* Device Self-test Options */
279 		uint8_t dsto_sub:1;	/* Subsystem level self-test (1.3) */
280 		uint8_t dsto_rsvd:7;
281 	} ap_dsto;
282 	uint8_t ap_fwug;		/* Firmware Update Granularity (1.3) */
283 	uint16_t ap_kas;		/* Keep Alive Support (1.2) */
284 	struct {			/* Host Thermal Management (1.3) */
285 		uint16_t hctma_hctm:1;	/* Host Controlled (1.3) */
286 		uint16_t hctma_rsvd:15;
287 	} ap_hctma;
288 	uint16_t ap_mntmt;		/* Minimum Thermal Temperature (1.3) */
289 	uint16_t ap_mxtmt;		/* Maximum Thermal Temperature (1.3) */
290 	struct {			/* Sanitize Caps */
291 		uint32_t san_ces:1;	/* Crypto Erase Support (1.3) */
292 		uint32_t san_bes:1;	/* Block Erase Support (1.3) */
293 		uint32_t san_ows:1;	/* Overwite Support (1.3) */
294 		uint32_t san_rsvd:26;
295 		uint32_t san_ndi:1;	/* No-deallocate Inhibited (1.4) */
296 		uint32_t san_nodmmas:2;	/* No-Deallocate Modifies Media (1.4) */
297 	} ap_sanitize;
298 	uint32_t ap_hmminds;		/* Host Mem Buf Min Desc Entry (1.4) */
299 	uint16_t ap_hmmaxd;		/* How Mem Max Desc Entries (1.4) */
300 	uint16_t ap_nsetidmax;		/* Max NVMe set identifier (1.4) */
301 	uint16_t ap_engidmax;		/* Max Endurance Group ID (1.4) */
302 	uint8_t ap_anatt;		/* ANA Transition Time (1.4) */
303 	struct {			/* Asymmetric Namespace Access Caps */
304 		uint8_t anacap_opt:1;	/* Optimized State (1.4) */
305 		uint8_t anacap_unopt:1;	/* Un-optimized State (1.4) */
306 		uint8_t anacap_inacc:1;	/* Inaccessible State (1.4) */
307 		uint8_t anacap_ploss:1;	/* Persistent Loss (1.4) */
308 		uint8_t anacap_chg:1;	/* Change State (1.4 ) */
309 		uint8_t anacap_rsvd:1;
310 		uint8_t anacap_grpns:1;	/* ID Changes with NS Attach (1.4) */
311 		uint8_t anacap_grpid:1;	/* Supports Group ID (1.4) */
312 	} ap_anacap;
313 	uint32_t ap_anagrpmax;		/* ANA Group ID Max (1.4) */
314 	uint32_t ap_nanagrpid;		/* Number of ANA Group IDs (1.4) */
315 	uint32_t ap_pels;		/* Persistent Event Log Size (1.4) */
316 	uint8_t id_rsvd_ac[512 - 356];
317 
318 	/* NVM Command Set Attributes */
319 	nvme_idctl_qes_t id_sqes;	/* Submission Queue Entry Size */
320 	nvme_idctl_qes_t id_cqes;	/* Completion Queue Entry Size */
321 	uint16_t id_maxcmd;		/* Max Outstanding Commands (1.3) */
322 	uint32_t id_nn;			/* Number of Namespaces */
323 	struct {			/* Optional NVM Command Support */
324 		uint16_t on_compare:1;	/* Compare */
325 		uint16_t on_wr_unc:1;	/* Write Uncorrectable */
326 		uint16_t on_dset_mgmt:1; /* Dataset Management */
327 		uint16_t on_wr_zero:1;	/* Write Zeros (1.1) */
328 		uint16_t on_save:1;	/* Save/Select in Get/Set Feat (1.1) */
329 		uint16_t on_reserve:1;	/* Reservations (1.1) */
330 		uint16_t on_ts:1;	/* Timestamp (1.3) */
331 		uint16_t on_verify:1;	/* Verify (1.4) */
332 		uint16_t on_rsvd:8;
333 	} id_oncs;
334 	struct {			/* Fused Operation Support */
335 		uint16_t f_cmp_wr:1;	/* Compare and Write */
336 		uint16_t f_rsvd:15;
337 	} id_fuses;
338 	struct {			/* Format NVM Attributes */
339 		uint8_t fn_format:1;	/* Format applies to all NS */
340 		uint8_t fn_sec_erase:1;	/* Secure Erase applies to all NS */
341 		uint8_t fn_crypt_erase:1; /* Cryptographic Erase supported */
342 		uint8_t fn_rsvd:5;
343 	} id_fna;
344 	struct {			/* Volatile Write Cache */
345 		uint8_t vwc_present:1;	/* Volatile Write Cache present */
346 		uint8_t vwc_nsflush:2;	/* Flush with NS ffffffff (1.4) */
347 		uint8_t rsvd:5;
348 	} id_vwc;
349 	uint16_t id_awun;		/* Atomic Write Unit Normal */
350 	uint16_t id_awupf;		/* Atomic Write Unit Power Fail */
351 	struct {			/* NVM Vendor Specific Command Conf */
352 		uint8_t nv_spec:1;	/* use format from spec */
353 		uint8_t nv_rsvd:7;
354 	} id_nvscc;
355 	struct {			/* Namespace Write Protection Caps */
356 		uint8_t nwpc_base:1;	/* Base support (1.4) */
357 		uint8_t nwpc_wpupc:1;	/* Write prot until power cycle (1.4) */
358 		uint8_t nwpc_permwp:1;	/* Permanent write prot (1.4) */
359 		uint8_t nwpc_rsvd:5;
360 	} id_nwpc;
361 	uint16_t id_acwu;		/* Atomic Compare & Write Unit (1.1) */
362 	uint16_t id_rsvd_nc_3;
363 	struct {			/* SGL Support (1.1) */
364 		uint16_t sgl_sup:2;	/* SGL Supported in NVM cmds (1.3) */
365 		uint16_t sgl_keyed:1;	/* Keyed SGL Support (1.2) */
366 		uint16_t sgl_rsvd1:13;
367 		uint16_t sgl_bucket:1;	/* SGL Bit Bucket supported (1.1) */
368 		uint16_t sgl_balign:1;	/* SGL Byte Aligned (1.2) */
369 		uint16_t sgl_sglgtd:1;	/* SGL Length Longer than Data (1.2) */
370 		uint16_t sgl_mptr:1;	/* SGL MPTR w/ SGL (1.2) */
371 		uint16_t sgl_offset:1;	/* SGL Address is offset (1.2) */
372 		uint16_t sgl_tport:1;	/* Transport SGL Data Block (1.4) */
373 		uint16_t sgl_rsvd2:10;
374 	} id_sgls;
375 	uint32_t id_mnam;		/* Maximum Number of Allowed NSes */
376 	uint8_t id_rsvd_nc_4[768 - 544];
377 
378 	/* I/O Command Set Attributes */
379 	uint8_t id_subnqn[1024 - 768];	/* Subsystem Qualified Name (1.2.1+) */
380 	uint8_t id_rsvd_ioc[1792 - 1024];
381 	uint8_t id_nvmof[2048 - 1792];	/* NVMe over Fabrics */
382 
383 	/* Power State Descriptors */
384 	nvme_idctl_psd_t id_psd[32];
385 
386 	/* Vendor Specific */
387 	uint8_t id_vs[1024];
388 } nvme_identify_ctrl_t;
389 
390 /*
391  * NVMe Controller Types
392  */
393 #define	NVME_CNTRLTYPE_RSVD	0
394 #define	NVME_CNTRLTYPE_IO	1
395 #define	NVME_CNTRLTYPE_DISC	2
396 #define	NVME_CNTRLTYPE_ADMIN	3
397 
398 /*
399  * RPMBS Authentication Types
400  */
401 #define	NVME_RPMBS_AUTH_HMAC_SHA256	0
402 
403 /*
404  * NODMMAS Values
405  */
406 #define	NVME_NODMMAS_UNDEF	0x00
407 #define	NVME_NODMMAS_NOMOD	0x01
408 #define	NVME_NODMMAS_DOMOD	0x02
409 
410 /*
411  * VWC NSID flushes
412  */
413 #define	NVME_VWCNS_UNKNOWN	0x00
414 #define	NVME_VWCNS_UNSUP	0x02
415 #define	NVME_VWCNS_SUP		0x03
416 
417 /*
418  * SGL Support Values
419  */
420 #define	NVME_SGL_UNSUP		0x00
421 #define	NVME_SGL_SUP_UNALIGN	0x01
422 #define	NVME_SGL_SUP_ALIGN	0x02
423 
424 /* NVMe Identify Namespace LBA Format */
425 typedef struct {
426 	uint16_t lbaf_ms;		/* Metadata Size */
427 	uint8_t lbaf_lbads;		/* LBA Data Size */
428 	uint8_t lbaf_rp:2;		/* Relative Performance */
429 	uint8_t lbaf_rsvd1:6;
430 } nvme_idns_lbaf_t;
431 
432 /* NVMe Identify Namespace Data Structure */
433 typedef struct {
434 	uint64_t id_nsize;		/* Namespace Size */
435 	uint64_t id_ncap;		/* Namespace Capacity */
436 	uint64_t id_nuse;		/* Namespace Utilization */
437 	struct {			/* Namespace Features */
438 		uint8_t f_thin:1;	/* Thin Provisioning */
439 		uint8_t f_nsabp:1;	/* Namespace atomics (1.2) */
440 		uint8_t f_dae:1;	/* Deallocated errors supported (1.2) */
441 		uint8_t f_uidreuse:1;	/* GUID reuse impossible (1.3) */
442 		uint8_t f_optperf:1;	/* Namespace I/O opt (1.4) */
443 		uint8_t f_rsvd:3;
444 	} id_nsfeat;
445 	uint8_t id_nlbaf;		/* Number of LBA formats */
446 	struct {			/* Formatted LBA size */
447 		uint8_t lba_format:4;	/* LBA format */
448 		uint8_t lba_extlba:1;	/* extended LBA (includes metadata) */
449 		uint8_t lba_rsvd:3;
450 	} id_flbas;
451 	struct {			/* Metadata Capabilities */
452 		uint8_t mc_extlba:1;	/* extended LBA transfers */
453 		uint8_t mc_separate:1;	/* separate metadata transfers */
454 		uint8_t mc_rsvd:6;
455 	} id_mc;
456 	struct {			/* Data Protection Capabilities */
457 		uint8_t dp_type1:1;	/* Protection Information Type 1 */
458 		uint8_t dp_type2:1;	/* Protection Information Type 2 */
459 		uint8_t dp_type3:1;	/* Protection Information Type 3 */
460 		uint8_t dp_first:1;	/* first 8 bytes of metadata */
461 		uint8_t dp_last:1;	/* last 8 bytes of metadata */
462 		uint8_t dp_rsvd:3;
463 	} id_dpc;
464 	struct {			/* Data Protection Settings */
465 		uint8_t dp_pinfo:3;	/* Protection Information enabled */
466 		uint8_t dp_first:1;	/* first 8 bytes of metadata */
467 		uint8_t dp_rsvd:4;
468 	} id_dps;
469 	struct {			/* NS Multi-Path/Sharing Cap (1.1) */
470 		uint8_t nm_shared:1;	/* NS is shared (1.1) */
471 		uint8_t nm_rsvd:7;
472 	} id_nmic;
473 	struct {			/* Reservation Capabilities (1.1) */
474 		uint8_t rc_persist:1;	/* Persist Through Power Loss (1.1) */
475 		uint8_t rc_wr_excl:1;	/* Write Exclusive (1.1) */
476 		uint8_t rc_excl:1;	/* Exclusive Access (1.1) */
477 		uint8_t rc_wr_excl_r:1;	/* Wr Excl - Registrants Only (1.1) */
478 		uint8_t rc_excl_r:1;	/* Excl Acc - Registrants Only (1.1) */
479 		uint8_t rc_wr_excl_a:1;	/* Wr Excl - All Registrants (1.1) */
480 		uint8_t rc_excl_a:1;	/* Excl Acc - All Registrants (1.1) */
481 		uint8_t rc_ign_ekey:1;	/* Ignore Existing Key (1.3) */
482 	} id_rescap;
483 	struct {			/* Format Progress Indicator (1.2) */
484 		uint8_t fpi_remp:7;	/* Percent NVM Format Remaining (1.2) */
485 		uint8_t fpi_sup:1;	/* Supported (1.2) */
486 	} id_fpi;
487 	uint8_t id_dfleat;		/* Deallocate Log. Block (1.3) */
488 	uint16_t id_nawun;		/* Atomic Write Unit Normal (1.2) */
489 	uint16_t id_nawupf;		/* Atomic Write Unit Power Fail (1.2) */
490 	uint16_t id_nacwu;		/* Atomic Compare & Write Unit (1.2) */
491 	uint16_t id_nabsn;		/* Atomic Boundary Size Normal (1.2) */
492 	uint16_t id_nbao;		/* Atomic Boundary Offset (1.2) */
493 	uint16_t id_nabspf;		/* Atomic Boundary Size Fail (1.2) */
494 	uint16_t id_noiob;		/* Optimal I/O Bondary (1.3) */
495 	nvme_uint128_t id_nvmcap;	/* NVM Capacity */
496 	uint16_t id_npwg;		/* NS Pref. Write Gran. (1.4) */
497 	uint16_t id_npwa;		/* NS Pref. Write Align. (1.4) */
498 	uint16_t id_npdg;		/* NS Pref. Deallocate Gran. (1.4) */
499 	uint16_t id_npda;		/* NS Pref. Deallocate Align. (1.4) */
500 	uint16_t id_nows;		/* NS. Optimal Write Size (1.4) */
501 	uint8_t id_rsvd1[92 - 74];
502 	uint32_t id_anagrpid;		/* ANA Group Identifier (1.4) */
503 	uint8_t id_rsvd2[99 - 96];
504 	struct {
505 		uint8_t nsa_wprot:1;	/* Write Protected (1.4) */
506 		uint8_t nsa_rsvd:7;
507 	} id_nsattr;
508 	uint16_t id_nvmsetid;		/* NVM Set Identifier (1.4) */
509 	uint16_t id_endgid;		/* Endurance Group Identifier (1.4) */
510 	uint8_t id_nguid[16];		/* Namespace GUID (1.2) */
511 	uint8_t id_eui64[8];		/* IEEE Extended Unique Id (1.1) */
512 	nvme_idns_lbaf_t id_lbaf[16];	/* LBA Formats */
513 
514 	uint8_t id_rsvd3[384 - 192];
515 
516 	uint8_t id_vs[4096 - 384];	/* Vendor Specific */
517 } nvme_identify_nsid_t;
518 
519 /* NVMe Identify Primary Controller Capabilities */
520 typedef struct {
521 	uint16_t	nipc_cntlid;	/* Controller ID */
522 	uint16_t	nipc_portid;	/* Port Identifier */
523 	uint8_t		nipc_crt;	/* Controller Resource Types */
524 	uint8_t		nipc_rsvd0[32 - 5];
525 	uint32_t	nipc_vqfrt;	/* VQ Resources Flexible Total */
526 	uint32_t	nipc_vqrfa;	/* VQ Resources Flexible Assigned */
527 	uint16_t	nipc_vqrfap;	/* VQ Resources to Primary */
528 	uint16_t	nipc_vqprt;	/* VQ Resources Private Total */
529 	uint16_t	nipc_vqfrsm;	/* VQ Resources Secondary Max */
530 	uint16_t	nipc_vqgran;	/* VQ Flexible Resource Gran */
531 	uint8_t		nipc_rvsd1[64 - 48];
532 	uint32_t	nipc_vifrt;	/* VI Flexible total */
533 	uint32_t	nipc_virfa;	/* VI Flexible Assigned */
534 	uint16_t	nipc_virfap;	/* VI Flexible Allocated to Primary */
535 	uint16_t	nipc_viprt;	/* VI Resources Private Total */
536 	uint16_t	nipc_vifrsm;	/* VI Resources Secondary Max */
537 	uint16_t	nipc_vigran;	/* VI Flexible Granularity */
538 	uint8_t		nipc_rsvd2[4096 - 80];
539 } nvme_identify_primary_caps_t;
540 
541 /*
542  * NVMe completion queue entry status field
543  */
544 typedef struct {
545 	uint16_t sf_p:1;		/* Phase Tag */
546 	uint16_t sf_sc:8;		/* Status Code */
547 	uint16_t sf_sct:3;		/* Status Code Type */
548 	uint16_t sf_rsvd2:2;
549 	uint16_t sf_m:1;		/* More */
550 	uint16_t sf_dnr:1;		/* Do Not Retry */
551 } nvme_cqe_sf_t;
552 
553 
554 /*
555  * NVMe Get Log Page
556  */
557 #define	NVME_LOGPAGE_ERROR	0x1	/* Error Information */
558 #define	NVME_LOGPAGE_HEALTH	0x2	/* SMART/Health Information */
559 #define	NVME_LOGPAGE_FWSLOT	0x3	/* Firmware Slot Information */
560 
561 typedef struct {
562 	uint64_t el_count;		/* Error Count */
563 	uint16_t el_sqid;		/* Submission Queue ID */
564 	uint16_t el_cid;		/* Command ID */
565 	nvme_cqe_sf_t el_sf;		/* Status Field */
566 	uint8_t	el_byte;		/* Parameter Error Location byte */
567 	uint8_t	el_bit:3;		/* Parameter Error Location bit */
568 	uint8_t el_rsvd1:5;
569 	uint64_t el_lba;		/* Logical Block Address */
570 	uint32_t el_nsid;		/* Namespace ID */
571 	uint8_t	el_vendor;		/* Vendor Specific Information avail */
572 	uint8_t el_rsvd2[64 - 29];
573 } nvme_error_log_entry_t;
574 
575 typedef struct {
576 	struct {			/* Critical Warning */
577 		uint8_t cw_avail:1;	/* available space too low */
578 		uint8_t cw_temp:1;	/* temperature too high */
579 		uint8_t cw_reliab:1;	/* degraded reliability */
580 		uint8_t cw_readonly:1;	/* media is read-only */
581 		uint8_t cw_volatile:1;	/* volatile memory backup failed */
582 		uint8_t cw_rsvd:3;
583 	} hl_crit_warn;
584 	uint16_t hl_temp;		/* Temperature */
585 	uint8_t hl_avail_spare;		/* Available Spare */
586 	uint8_t hl_avail_spare_thr;	/* Available Spare Threshold */
587 	uint8_t hl_used;		/* Percentage Used */
588 	uint8_t hl_rsvd1[32 - 6];
589 	nvme_uint128_t hl_data_read;	/* Data Units Read */
590 	nvme_uint128_t hl_data_write;	/* Data Units Written */
591 	nvme_uint128_t hl_host_read;	/* Host Read Commands */
592 	nvme_uint128_t hl_host_write;	/* Host Write Commands */
593 	nvme_uint128_t hl_ctrl_busy;	/* Controller Busy Time */
594 	nvme_uint128_t hl_power_cycles;	/* Power Cycles */
595 	nvme_uint128_t hl_power_on_hours; /* Power On Hours */
596 	nvme_uint128_t hl_unsafe_shutdn; /* Unsafe Shutdowns */
597 	nvme_uint128_t hl_media_errors;	/* Media Errors */
598 	nvme_uint128_t hl_errors_logged; /* Number of errors logged */
599 	/* Added in NVMe 1.2 */
600 	uint32_t hl_warn_temp_time;	/* Warning Composite Temp Time */
601 	uint32_t hl_crit_temp_time;	/* Critical Composite Temp Time */
602 	uint16_t hl_temp_sensor_1;	/* Temperature Sensor 1 */
603 	uint16_t hl_temp_sensor_2;	/* Temperature Sensor 2 */
604 	uint16_t hl_temp_sensor_3;	/* Temperature Sensor 3 */
605 	uint16_t hl_temp_sensor_4;	/* Temperature Sensor 4 */
606 	uint16_t hl_temp_sensor_5;	/* Temperature Sensor 5 */
607 	uint16_t hl_temp_sensor_6;	/* Temperature Sensor 6 */
608 	uint16_t hl_temp_sensor_7;	/* Temperature Sensor 7 */
609 	uint16_t hl_temp_sensor_8;	/* Temperature Sensor 8 */
610 	/* Added in NVMe 1.3 */
611 	uint32_t hl_tmtemp_1_tc;	/* Thermal Mgmt Temp 1 Transition # */
612 	uint32_t hl_tmtemp_2_tc;	/* Thermal Mgmt Temp 1 Transition # */
613 	uint32_t hl_tmtemp_1_time;	/* Time in Thermal Mgmt Temp 1 */
614 	uint32_t hl_tmtemp_2_time;	/* Time in Thermal Mgmt Temp 2 */
615 	uint8_t hl_rsvd2[512 - 232];
616 } nvme_health_log_t;
617 
618 /*
619  * The NVMe spec allows for up to seven firmware slots.
620  */
621 #define	NVME_MAX_FWSLOTS	7
622 #define	NVME_FWVER_SZ		8
623 
624 typedef struct {
625 	/* Active Firmware Slot */
626 	uint8_t fw_afi:3;
627 	uint8_t fw_rsvd1:1;
628 	/* Next Active Firmware Slot */
629 	uint8_t fw_next:3;
630 	uint8_t fw_rsvd2:1;
631 	uint8_t fw_rsvd3[7];
632 	/* Firmware Revision / Slot */
633 	char fw_frs[NVME_MAX_FWSLOTS][NVME_FWVER_SZ];
634 	uint8_t fw_rsvd4[512 - 64];
635 } nvme_fwslot_log_t;
636 
637 
638 /*
639  * NVMe Format NVM
640  */
641 #define	NVME_FRMT_SES_NONE	0
642 #define	NVME_FRMT_SES_USER	1
643 #define	NVME_FRMT_SES_CRYPTO	2
644 #define	NVME_FRMT_MAX_SES	2
645 
646 #define	NVME_FRMT_MAX_LBAF	15
647 
648 typedef union {
649 	struct {
650 		uint32_t fm_lbaf:4;		/* LBA Format */
651 		uint32_t fm_ms:1;		/* Metadata Settings */
652 		uint32_t fm_pi:3;		/* Protection Information */
653 		uint32_t fm_pil:1;		/* Prot. Information Location */
654 		uint32_t fm_ses:3;		/* Secure Erase Settings */
655 		uint32_t fm_resvd:20;
656 	} b;
657 	uint32_t r;
658 } nvme_format_nvm_t;
659 
660 
661 /*
662  * NVMe Get / Set Features
663  */
664 #define	NVME_FEAT_ARBITRATION	0x1	/* Command Arbitration */
665 #define	NVME_FEAT_POWER_MGMT	0x2	/* Power Management */
666 #define	NVME_FEAT_LBA_RANGE	0x3	/* LBA Range Type */
667 #define	NVME_FEAT_TEMPERATURE	0x4	/* Temperature Threshold */
668 #define	NVME_FEAT_ERROR		0x5	/* Error Recovery */
669 #define	NVME_FEAT_WRITE_CACHE	0x6	/* Volatile Write Cache */
670 #define	NVME_FEAT_NQUEUES	0x7	/* Number of Queues */
671 #define	NVME_FEAT_INTR_COAL	0x8	/* Interrupt Coalescing */
672 #define	NVME_FEAT_INTR_VECT	0x9	/* Interrupt Vector Configuration */
673 #define	NVME_FEAT_WRITE_ATOM	0xa	/* Write Atomicity */
674 #define	NVME_FEAT_ASYNC_EVENT	0xb	/* Asynchronous Event Configuration */
675 #define	NVME_FEAT_AUTO_PST	0xc	/* Autonomous Power State Transition */
676 					/* (1.1) */
677 
678 #define	NVME_FEAT_PROGRESS	0x80	/* Software Progress Marker */
679 
680 /* Arbitration Feature */
681 typedef union {
682 	struct {
683 		uint8_t arb_ab:3;	/* Arbitration Burst */
684 		uint8_t arb_rsvd:5;
685 		uint8_t arb_lpw;	/* Low Priority Weight */
686 		uint8_t arb_mpw;	/* Medium Priority Weight */
687 		uint8_t arb_hpw;	/* High Priority Weight */
688 	} b;
689 	uint32_t r;
690 } nvme_arbitration_t;
691 
692 /* Power Management Feature */
693 typedef union {
694 	struct {
695 		uint32_t pm_ps:5;	/* Power State */
696 		uint32_t pm_rsvd:27;
697 	} b;
698 	uint32_t r;
699 } nvme_power_mgmt_t;
700 
701 /* LBA Range Type Feature */
702 typedef union {
703 	struct {
704 		uint32_t lr_num:6;	/* Number of LBA ranges */
705 		uint32_t lr_rsvd:26;
706 	} b;
707 	uint32_t r;
708 } nvme_lba_range_type_t;
709 
710 typedef struct {
711 	uint8_t lr_type;		/* Type */
712 	struct {			/* Attributes */
713 		uint8_t lr_write:1;	/* may be overwritten */
714 		uint8_t lr_hidden:1;	/* hidden from OS/EFI/BIOS */
715 		uint8_t lr_rsvd1:6;
716 	} lr_attr;
717 	uint8_t lr_rsvd2[14];
718 	uint64_t lr_slba;		/* Starting LBA */
719 	uint64_t lr_nlb;		/* Number of Logical Blocks */
720 	uint8_t lr_guid[16];		/* Unique Identifier */
721 	uint8_t lr_rsvd3[16];
722 } nvme_lba_range_t;
723 
724 #define	NVME_LBA_RANGE_BUFSIZE	4096
725 
726 /* Temperature Threshold Feature */
727 typedef union {
728 	struct {
729 		uint16_t tt_tmpth;	/* Temperature Threshold */
730 		uint16_t tt_tmpsel:4;	/* Temperature Select */
731 		uint16_t tt_thsel:2;	/* Temperature Type */
732 		uint16_t tt_resv:10;
733 	} b;
734 	uint32_t r;
735 } nvme_temp_threshold_t;
736 
737 #define	NVME_TEMP_THRESH_MAX_SENSOR	8
738 #define	NVME_TEMP_THRESH_ALL	0xf
739 #define	NVME_TEMP_THRESH_OVER	0x00
740 #define	NVME_TEMP_THRESH_UNDER	0x01
741 
742 /* Error Recovery Feature */
743 typedef union {
744 	struct {
745 		uint16_t er_tler;	/* Time-Limited Error Recovery */
746 		uint16_t er_rsvd;
747 	} b;
748 	uint32_t r;
749 } nvme_error_recovery_t;
750 
751 /* Volatile Write Cache Feature */
752 typedef union {
753 	struct {
754 		uint32_t wc_wce:1;	/* Volatile Write Cache Enable */
755 		uint32_t wc_rsvd:31;
756 	} b;
757 	uint32_t r;
758 } nvme_write_cache_t;
759 
760 /* Number of Queues Feature */
761 typedef union {
762 	struct {
763 		uint16_t nq_nsq;	/* Number of Submission Queues */
764 		uint16_t nq_ncq;	/* Number of Completion Queues */
765 	} b;
766 	uint32_t r;
767 } nvme_nqueues_t;
768 
769 /* Interrupt Coalescing Feature */
770 typedef union {
771 	struct {
772 		uint8_t ic_thr;		/* Aggregation Threshold */
773 		uint8_t ic_time;	/* Aggregation Time */
774 		uint16_t ic_rsvd;
775 	} b;
776 	uint32_t r;
777 } nvme_intr_coal_t;
778 
779 /* Interrupt Configuration Features */
780 typedef union {
781 	struct {
782 		uint16_t iv_iv;		/* Interrupt Vector */
783 		uint16_t iv_cd:1;	/* Coalescing Disable */
784 		uint16_t iv_rsvd:15;
785 	} b;
786 	uint32_t r;
787 } nvme_intr_vect_t;
788 
789 /* Write Atomicity Feature */
790 typedef union {
791 	struct {
792 		uint32_t wa_dn:1;	/* Disable Normal */
793 		uint32_t wa_rsvd:31;
794 	} b;
795 	uint32_t r;
796 } nvme_write_atomicity_t;
797 
798 /* Asynchronous Event Configuration Feature */
799 typedef union {
800 	struct {
801 		uint8_t aec_avail:1;	/* available space too low */
802 		uint8_t aec_temp:1;	/* temperature too high */
803 		uint8_t aec_reliab:1;	/* degraded reliability */
804 		uint8_t aec_readonly:1;	/* media is read-only */
805 		uint8_t aec_volatile:1;	/* volatile memory backup failed */
806 		uint8_t aec_rsvd1:3;
807 		uint8_t aec_rsvd2[3];
808 	} b;
809 	uint32_t r;
810 } nvme_async_event_conf_t;
811 
812 /* Autonomous Power State Transition Feature (1.1) */
813 typedef union {
814 	struct {
815 		uint8_t	apst_apste:1;	/* APST enabled */
816 		uint8_t apst_rsvd:7;
817 	} b;
818 	uint8_t r;
819 } nvme_auto_power_state_trans_t;
820 
821 typedef struct {
822 	uint32_t apst_rsvd1:3;
823 	uint32_t apst_itps:5;	/* Idle Transition Power State */
824 	uint32_t apst_itpt:24;	/* Idle Time Prior to Transition */
825 	uint32_t apst_rsvd2;
826 } nvme_auto_power_state_t;
827 
828 #define	NVME_AUTO_PST_BUFSIZE	256
829 
830 /* Software Progress Marker Feature */
831 typedef union {
832 	struct {
833 		uint8_t spm_pbslc;	/* Pre-Boot Software Load Count */
834 		uint8_t spm_rsvd[3];
835 	} b;
836 	uint32_t r;
837 } nvme_software_progress_marker_t;
838 
839 /*
840  * Firmware Commit - Command Dword 10
841  */
842 #define	NVME_FWC_SAVE		0x0	/* Save image only */
843 #define	NVME_FWC_SAVE_ACTIVATE	0x1	/* Save and activate at next reset */
844 #define	NVME_FWC_ACTIVATE	0x2	/* Activate slot at next reset */
845 #define	NVME_FWC_ACTIVATE_IMMED	0x3	/* Activate slot immediately */
846 
847 /*
848  * Firmware slot number is only 3 bits, and zero is not allowed.
849  * Valid range is 1 to 7.
850  */
851 #define	NVME_FW_SLOT_MIN	1	/* lowest allowable slot number ... */
852 #define	NVME_FW_SLOT_MAX	7	/* ... and highest */
853 
854 /*
855  * Some constants to make verification of DWORD variables and arguments easier.
856  * A DWORD is 4 bytes.
857  */
858 #define	NVME_DWORD_SHIFT	2
859 #define	NVME_DWORD_SIZE		(1 << NVME_DWORD_SHIFT)
860 #define	NVME_DWORD_MASK		(NVME_DWORD_SIZE - 1)
861 
862 /*
863  * Maximum offset a firmware image can be load at is the number of
864  * DWORDS in a 32 bit field. Expressed in bytes its is:
865  */
866 #define	NVME_FW_OFFSETB_MAX	((u_longlong_t)UINT32_MAX << NVME_DWORD_SHIFT)
867 
868 typedef union {
869 	struct {
870 		uint32_t fc_slot:3;	/* Firmware slot */
871 		uint32_t fc_action:3;	/* Commit action */
872 		uint32_t fc_rsvd:26;
873 	} b;
874 	uint32_t r;
875 } nvme_firmware_commit_dw10_t;
876 
877 #pragma pack() /* pack(1) */
878 
879 /* NVMe completion status code type */
880 #define	NVME_CQE_SCT_GENERIC	0	/* Generic Command Status */
881 #define	NVME_CQE_SCT_SPECIFIC	1	/* Command Specific Status */
882 #define	NVME_CQE_SCT_INTEGRITY	2	/* Media and Data Integrity Errors */
883 #define	NVME_CQE_SCT_VENDOR	7	/* Vendor Specific */
884 
885 /* NVMe completion status code (generic) */
886 #define	NVME_CQE_SC_GEN_SUCCESS		0x0	/* Successful Completion */
887 #define	NVME_CQE_SC_GEN_INV_OPC		0x1	/* Invalid Command Opcode */
888 #define	NVME_CQE_SC_GEN_INV_FLD		0x2	/* Invalid Field in Command */
889 #define	NVME_CQE_SC_GEN_ID_CNFL		0x3	/* Command ID Conflict */
890 #define	NVME_CQE_SC_GEN_DATA_XFR_ERR	0x4	/* Data Transfer Error */
891 #define	NVME_CQE_SC_GEN_ABORT_PWRLOSS	0x5	/* Cmds Aborted / Pwr Loss */
892 #define	NVME_CQE_SC_GEN_INTERNAL_ERR	0x6	/* Internal Error */
893 #define	NVME_CQE_SC_GEN_ABORT_REQUEST	0x7	/* Command Abort Requested */
894 #define	NVME_CQE_SC_GEN_ABORT_SQ_DEL	0x8	/* Cmd Aborted / SQ deletion */
895 #define	NVME_CQE_SC_GEN_ABORT_FUSE_FAIL	0x9	/* Cmd Aborted / Failed Fused */
896 #define	NVME_CQE_SC_GEN_ABORT_FUSE_MISS	0xa	/* Cmd Aborted / Missing Fusd */
897 #define	NVME_CQE_SC_GEN_INV_NS		0xb	/* Inval Namespace or Format */
898 #define	NVME_CQE_SC_GEN_CMD_SEQ_ERR	0xc	/* Command Sequence Error */
899 #define	NVME_CQE_SC_GEN_INV_SGL_LAST	0xd	/* Inval SGL Last Seg Desc */
900 #define	NVME_CQE_SC_GEN_INV_SGL_NUM	0xe	/* Inval Number of SGL Desc */
901 #define	NVME_CQE_SC_GEN_INV_DSGL_LEN	0xf	/* Data SGL Length Invalid */
902 #define	NVME_CQE_SC_GEN_INV_MSGL_LEN	0x10	/* Metadata SGL Length Inval */
903 #define	NVME_CQE_SC_GEN_INV_SGL_DESC	0x11	/* SGL Descriptor Type Inval */
904 #define	NVME_CQE_SC_GEN_INV_USE_CMB	0x12	/* Inval use of Ctrl Mem Buf */
905 #define	NVME_CQE_SC_GEN_INV_PRP_OFF	0x13	/* PRP Offset Invalid */
906 #define	NVME_CQE_SC_GEN_AWU_EXCEEDED	0x14	/* Atomic Write Unit Exceeded */
907 
908 /* NVMe completion status code (generic NVM commands) */
909 #define	NVME_CQE_SC_GEN_NVM_LBA_RANGE	0x80	/* LBA Out Of Range */
910 #define	NVME_CQE_SC_GEN_NVM_CAP_EXC	0x81	/* Capacity Exceeded */
911 #define	NVME_CQE_SC_GEN_NVM_NS_NOTRDY	0x82	/* Namespace Not Ready */
912 #define	NVME_CQE_SC_GEN_NVM_RSV_CNFLCT	0x83	/* Reservation Conflict */
913 
914 /* NVMe completion status code (command specific) */
915 #define	NVME_CQE_SC_SPC_INV_CQ		0x0	/* Completion Queue Invalid */
916 #define	NVME_CQE_SC_SPC_INV_QID		0x1	/* Invalid Queue Identifier */
917 #define	NVME_CQE_SC_SPC_MAX_QSZ_EXC	0x2	/* Max Queue Size Exceeded */
918 #define	NVME_CQE_SC_SPC_ABRT_CMD_EXC	0x3	/* Abort Cmd Limit Exceeded */
919 #define	NVME_CQE_SC_SPC_ASYNC_EVREQ_EXC	0x5	/* Async Event Request Limit */
920 #define	NVME_CQE_SC_SPC_INV_FW_SLOT	0x6	/* Invalid Firmware Slot */
921 #define	NVME_CQE_SC_SPC_INV_FW_IMG	0x7	/* Invalid Firmware Image */
922 #define	NVME_CQE_SC_SPC_INV_INT_VECT	0x8	/* Invalid Interrupt Vector */
923 #define	NVME_CQE_SC_SPC_INV_LOG_PAGE	0x9	/* Invalid Log Page */
924 #define	NVME_CQE_SC_SPC_INV_FORMAT	0xa	/* Invalid Format */
925 #define	NVME_CQE_SC_SPC_FW_RESET	0xb	/* FW Application Reset Reqd */
926 #define	NVME_CQE_SC_SPC_INV_Q_DEL	0xc	/* Invalid Queue Deletion */
927 #define	NVME_CQE_SC_SPC_FEAT_SAVE	0xd	/* Feature Id Not Saveable */
928 #define	NVME_CQE_SC_SPC_FEAT_CHG	0xe	/* Feature Not Changeable */
929 #define	NVME_CQE_SC_SPC_FEAT_NS_SPEC	0xf	/* Feature Not Namespace Spec */
930 #define	NVME_CQE_SC_SPC_FW_NSSR		0x10	/* FW Application NSSR Reqd */
931 #define	NVME_CQE_SC_SPC_FW_NEXT_RESET	0x11	/* FW Application Next Reqd */
932 #define	NVME_CQE_SC_SPC_FW_MTFA		0x12	/* FW Application Exceed MTFA */
933 #define	NVME_CQE_SC_SPC_FW_PROHIBITED	0x13	/* FW Application Prohibited */
934 #define	NVME_CQE_SC_SPC_FW_OVERLAP	0x14	/* Overlapping FW ranges */
935 
936 /* NVMe completion status code (NVM command specific */
937 #define	NVME_CQE_SC_SPC_NVM_CNFL_ATTR	0x80	/* Conflicting Attributes */
938 #define	NVME_CQE_SC_SPC_NVM_INV_PROT	0x81	/* Invalid Protection */
939 #define	NVME_CQE_SC_SPC_NVM_READONLY	0x82	/* Write to Read Only Range */
940 
941 /* NVMe completion status code (data / metadata integrity) */
942 #define	NVME_CQE_SC_INT_NVM_WRITE	0x80	/* Write Fault */
943 #define	NVME_CQE_SC_INT_NVM_READ	0x81	/* Unrecovered Read Error */
944 #define	NVME_CQE_SC_INT_NVM_GUARD	0x82	/* Guard Check Error */
945 #define	NVME_CQE_SC_INT_NVM_APPL_TAG	0x83	/* Application Tag Check Err */
946 #define	NVME_CQE_SC_INT_NVM_REF_TAG	0x84	/* Reference Tag Check Err */
947 #define	NVME_CQE_SC_INT_NVM_COMPARE	0x85	/* Compare Failure */
948 #define	NVME_CQE_SC_INT_NVM_ACCESS	0x86	/* Access Denied */
949 
950 /* Flags for NVMe passthru commands. */
951 #define	NVME_PASSTHRU_READ	0x1 /* Read from device */
952 #define	NVME_PASSTHRU_WRITE	0x2 /* Write to device */
953 
954 /* Error codes for NVMe passthru command validation. */
955 /* Must be sizeof(nvme_passthru_cmd_t) */
956 #define	NVME_PASSTHRU_ERR_CMD_SIZE	0x01
957 #define	NVME_PASSTHRU_ERR_NOT_SUPPORTED	0x02	/* Not supported on device */
958 #define	NVME_PASSTHRU_ERR_INVALID_OPCODE	0x03
959 #define	NVME_PASSTHRU_ERR_READ_AND_WRITE	0x04	/* Must read ^ write */
960 #define	NVME_PASSTHRU_ERR_INVALID_TIMEOUT	0x05
961 
962 /*
963  * Must be
964  * - multiple of 4 bytes in length
965  * - non-null iff length is non-zero
966  * - null if neither reading nor writing
967  * - non-null if either reading or writing
968  * - <= `nvme_vendor_specific_admin_cmd_size` in length, 16 MiB
969  * - <= UINT32_MAX in length
970  */
971 #define	NVME_PASSTHRU_ERR_INVALID_BUFFER	0x06
972 
973 
974 /* Generic struct for passing through vendor-unique commands to a device. */
975 typedef struct {
976 	uint8_t npc_opcode;	/* Command opcode. */
977 	uint8_t npc_status;	/* Command completion status code. */
978 	uint8_t npc_err;	/* Error-code if validation fails. */
979 	uint8_t npc_rsvd0;	/* Align to 4 bytes */
980 	uint32_t npc_timeout;	/* Command timeout, in seconds. */
981 	uint32_t npc_flags;	/* Flags for the command. */
982 	uint32_t npc_cdw0;	/* Command-specific result DWord 0 */
983 	uint32_t npc_cdw12;	/* Command-specific DWord 12 */
984 	uint32_t npc_cdw13;	/* Command-specific DWord 13 */
985 	uint32_t npc_cdw14;	/* Command-specific DWord 14 */
986 	uint32_t npc_cdw15;	/* Command-specific DWord 15 */
987 	size_t npc_buflen;	/* Size of npc_buf. */
988 	uintptr_t npc_buf;	/* I/O source or destination */
989 } nvme_passthru_cmd_t;
990 
991 #ifdef _KERNEL
992 typedef struct {
993 	uint8_t npc_opcode;	/* Command opcode. */
994 	uint8_t npc_status;	/* Command completion status code. */
995 	uint8_t npc_err;	/* Error-code if validation fails. */
996 	uint8_t npc_rsvd0;	/* Align to 4 bytes */
997 	uint32_t npc_timeout;	/* Command timeout, in seconds. */
998 	uint32_t npc_flags;	/* Flags for the command. */
999 	uint32_t npc_cdw0;	/* Command-specific result DWord 0 */
1000 	uint32_t npc_cdw12;	/* Command-specific DWord 12 */
1001 	uint32_t npc_cdw13;	/* Command-specific DWord 13 */
1002 	uint32_t npc_cdw14;	/* Command-specific DWord 14 */
1003 	uint32_t npc_cdw15;	/* Command-specific DWord 15 */
1004 	size32_t npc_buflen;	/* Size of npc_buf. */
1005 	uintptr32_t npc_buf;	/* I/O source or destination */
1006 } nvme_passthru_cmd32_t;
1007 #endif
1008 
1009 #ifdef __cplusplus
1010 }
1011 #endif
1012 
1013 #endif /* _SYS_NVME_H */
1014