xref: /openbmc/qemu/include/block/nvme.h (revision 57b3a7d8)
1 #ifndef BLOCK_NVME_H
2 #define BLOCK_NVME_H
3 
4 typedef struct QEMU_PACKED NvmeBar {
5     uint64_t    cap;
6     uint32_t    vs;
7     uint32_t    intms;
8     uint32_t    intmc;
9     uint32_t    cc;
10     uint32_t    rsvd1;
11     uint32_t    csts;
12     uint32_t    nssrc;
13     uint32_t    aqa;
14     uint64_t    asq;
15     uint64_t    acq;
16     uint32_t    cmbloc;
17     uint32_t    cmbsz;
18     uint8_t     padding[3520]; /* not used by QEMU */
19     uint32_t    pmrcap;
20     uint32_t    pmrctl;
21     uint32_t    pmrsts;
22     uint32_t    pmrebs;
23     uint32_t    pmrswtp;
24     uint64_t    pmrmsc;
25     uint8_t     reserved[484];
26 } NvmeBar;
27 
28 enum NvmeCapShift {
29     CAP_MQES_SHIFT     = 0,
30     CAP_CQR_SHIFT      = 16,
31     CAP_AMS_SHIFT      = 17,
32     CAP_TO_SHIFT       = 24,
33     CAP_DSTRD_SHIFT    = 32,
34     CAP_NSSRS_SHIFT    = 36,
35     CAP_CSS_SHIFT      = 37,
36     CAP_MPSMIN_SHIFT   = 48,
37     CAP_MPSMAX_SHIFT   = 52,
38     CAP_PMR_SHIFT      = 56,
39 };
40 
41 enum NvmeCapMask {
42     CAP_MQES_MASK      = 0xffff,
43     CAP_CQR_MASK       = 0x1,
44     CAP_AMS_MASK       = 0x3,
45     CAP_TO_MASK        = 0xff,
46     CAP_DSTRD_MASK     = 0xf,
47     CAP_NSSRS_MASK     = 0x1,
48     CAP_CSS_MASK       = 0xff,
49     CAP_MPSMIN_MASK    = 0xf,
50     CAP_MPSMAX_MASK    = 0xf,
51     CAP_PMR_MASK       = 0x1,
52 };
53 
54 #define NVME_CAP_MQES(cap)  (((cap) >> CAP_MQES_SHIFT)   & CAP_MQES_MASK)
55 #define NVME_CAP_CQR(cap)   (((cap) >> CAP_CQR_SHIFT)    & CAP_CQR_MASK)
56 #define NVME_CAP_AMS(cap)   (((cap) >> CAP_AMS_SHIFT)    & CAP_AMS_MASK)
57 #define NVME_CAP_TO(cap)    (((cap) >> CAP_TO_SHIFT)     & CAP_TO_MASK)
58 #define NVME_CAP_DSTRD(cap) (((cap) >> CAP_DSTRD_SHIFT)  & CAP_DSTRD_MASK)
59 #define NVME_CAP_NSSRS(cap) (((cap) >> CAP_NSSRS_SHIFT)  & CAP_NSSRS_MASK)
60 #define NVME_CAP_CSS(cap)   (((cap) >> CAP_CSS_SHIFT)    & CAP_CSS_MASK)
61 #define NVME_CAP_MPSMIN(cap)(((cap) >> CAP_MPSMIN_SHIFT) & CAP_MPSMIN_MASK)
62 #define NVME_CAP_MPSMAX(cap)(((cap) >> CAP_MPSMAX_SHIFT) & CAP_MPSMAX_MASK)
63 
64 #define NVME_CAP_SET_MQES(cap, val)   (cap |= (uint64_t)(val & CAP_MQES_MASK)  \
65                                                            << CAP_MQES_SHIFT)
66 #define NVME_CAP_SET_CQR(cap, val)    (cap |= (uint64_t)(val & CAP_CQR_MASK)   \
67                                                            << CAP_CQR_SHIFT)
68 #define NVME_CAP_SET_AMS(cap, val)    (cap |= (uint64_t)(val & CAP_AMS_MASK)   \
69                                                            << CAP_AMS_SHIFT)
70 #define NVME_CAP_SET_TO(cap, val)     (cap |= (uint64_t)(val & CAP_TO_MASK)    \
71                                                            << CAP_TO_SHIFT)
72 #define NVME_CAP_SET_DSTRD(cap, val)  (cap |= (uint64_t)(val & CAP_DSTRD_MASK) \
73                                                            << CAP_DSTRD_SHIFT)
74 #define NVME_CAP_SET_NSSRS(cap, val)  (cap |= (uint64_t)(val & CAP_NSSRS_MASK) \
75                                                            << CAP_NSSRS_SHIFT)
76 #define NVME_CAP_SET_CSS(cap, val)    (cap |= (uint64_t)(val & CAP_CSS_MASK)   \
77                                                            << CAP_CSS_SHIFT)
78 #define NVME_CAP_SET_MPSMIN(cap, val) (cap |= (uint64_t)(val & CAP_MPSMIN_MASK)\
79                                                            << CAP_MPSMIN_SHIFT)
80 #define NVME_CAP_SET_MPSMAX(cap, val) (cap |= (uint64_t)(val & CAP_MPSMAX_MASK)\
81                                                             << CAP_MPSMAX_SHIFT)
82 #define NVME_CAP_SET_PMRS(cap, val) (cap |= (uint64_t)(val & CAP_PMR_MASK)\
83                                                             << CAP_PMR_SHIFT)
84 
85 enum NvmeCapCss {
86     NVME_CAP_CSS_NVM        = 1 << 0,
87     NVME_CAP_CSS_ADMIN_ONLY = 1 << 7,
88 };
89 
90 enum NvmeCcShift {
91     CC_EN_SHIFT     = 0,
92     CC_CSS_SHIFT    = 4,
93     CC_MPS_SHIFT    = 7,
94     CC_AMS_SHIFT    = 11,
95     CC_SHN_SHIFT    = 14,
96     CC_IOSQES_SHIFT = 16,
97     CC_IOCQES_SHIFT = 20,
98 };
99 
100 enum NvmeCcMask {
101     CC_EN_MASK      = 0x1,
102     CC_CSS_MASK     = 0x7,
103     CC_MPS_MASK     = 0xf,
104     CC_AMS_MASK     = 0x7,
105     CC_SHN_MASK     = 0x3,
106     CC_IOSQES_MASK  = 0xf,
107     CC_IOCQES_MASK  = 0xf,
108 };
109 
110 #define NVME_CC_EN(cc)     ((cc >> CC_EN_SHIFT)     & CC_EN_MASK)
111 #define NVME_CC_CSS(cc)    ((cc >> CC_CSS_SHIFT)    & CC_CSS_MASK)
112 #define NVME_CC_MPS(cc)    ((cc >> CC_MPS_SHIFT)    & CC_MPS_MASK)
113 #define NVME_CC_AMS(cc)    ((cc >> CC_AMS_SHIFT)    & CC_AMS_MASK)
114 #define NVME_CC_SHN(cc)    ((cc >> CC_SHN_SHIFT)    & CC_SHN_MASK)
115 #define NVME_CC_IOSQES(cc) ((cc >> CC_IOSQES_SHIFT) & CC_IOSQES_MASK)
116 #define NVME_CC_IOCQES(cc) ((cc >> CC_IOCQES_SHIFT) & CC_IOCQES_MASK)
117 
118 enum NvmeCcCss {
119     NVME_CC_CSS_NVM        = 0x0,
120     NVME_CC_CSS_ADMIN_ONLY = 0x7,
121 };
122 
123 enum NvmeCstsShift {
124     CSTS_RDY_SHIFT      = 0,
125     CSTS_CFS_SHIFT      = 1,
126     CSTS_SHST_SHIFT     = 2,
127     CSTS_NSSRO_SHIFT    = 4,
128 };
129 
130 enum NvmeCstsMask {
131     CSTS_RDY_MASK   = 0x1,
132     CSTS_CFS_MASK   = 0x1,
133     CSTS_SHST_MASK  = 0x3,
134     CSTS_NSSRO_MASK = 0x1,
135 };
136 
137 enum NvmeCsts {
138     NVME_CSTS_READY         = 1 << CSTS_RDY_SHIFT,
139     NVME_CSTS_FAILED        = 1 << CSTS_CFS_SHIFT,
140     NVME_CSTS_SHST_NORMAL   = 0 << CSTS_SHST_SHIFT,
141     NVME_CSTS_SHST_PROGRESS = 1 << CSTS_SHST_SHIFT,
142     NVME_CSTS_SHST_COMPLETE = 2 << CSTS_SHST_SHIFT,
143     NVME_CSTS_NSSRO         = 1 << CSTS_NSSRO_SHIFT,
144 };
145 
146 #define NVME_CSTS_RDY(csts)     ((csts >> CSTS_RDY_SHIFT)   & CSTS_RDY_MASK)
147 #define NVME_CSTS_CFS(csts)     ((csts >> CSTS_CFS_SHIFT)   & CSTS_CFS_MASK)
148 #define NVME_CSTS_SHST(csts)    ((csts >> CSTS_SHST_SHIFT)  & CSTS_SHST_MASK)
149 #define NVME_CSTS_NSSRO(csts)   ((csts >> CSTS_NSSRO_SHIFT) & CSTS_NSSRO_MASK)
150 
151 enum NvmeAqaShift {
152     AQA_ASQS_SHIFT  = 0,
153     AQA_ACQS_SHIFT  = 16,
154 };
155 
156 enum NvmeAqaMask {
157     AQA_ASQS_MASK   = 0xfff,
158     AQA_ACQS_MASK   = 0xfff,
159 };
160 
161 #define NVME_AQA_ASQS(aqa) ((aqa >> AQA_ASQS_SHIFT) & AQA_ASQS_MASK)
162 #define NVME_AQA_ACQS(aqa) ((aqa >> AQA_ACQS_SHIFT) & AQA_ACQS_MASK)
163 
164 enum NvmeCmblocShift {
165     CMBLOC_BIR_SHIFT  = 0,
166     CMBLOC_OFST_SHIFT = 12,
167 };
168 
169 enum NvmeCmblocMask {
170     CMBLOC_BIR_MASK  = 0x7,
171     CMBLOC_OFST_MASK = 0xfffff,
172 };
173 
174 #define NVME_CMBLOC_BIR(cmbloc) ((cmbloc >> CMBLOC_BIR_SHIFT)  & \
175                                  CMBLOC_BIR_MASK)
176 #define NVME_CMBLOC_OFST(cmbloc)((cmbloc >> CMBLOC_OFST_SHIFT) & \
177                                  CMBLOC_OFST_MASK)
178 
179 #define NVME_CMBLOC_SET_BIR(cmbloc, val)  \
180     (cmbloc |= (uint64_t)(val & CMBLOC_BIR_MASK) << CMBLOC_BIR_SHIFT)
181 #define NVME_CMBLOC_SET_OFST(cmbloc, val) \
182     (cmbloc |= (uint64_t)(val & CMBLOC_OFST_MASK) << CMBLOC_OFST_SHIFT)
183 
184 enum NvmeCmbszShift {
185     CMBSZ_SQS_SHIFT   = 0,
186     CMBSZ_CQS_SHIFT   = 1,
187     CMBSZ_LISTS_SHIFT = 2,
188     CMBSZ_RDS_SHIFT   = 3,
189     CMBSZ_WDS_SHIFT   = 4,
190     CMBSZ_SZU_SHIFT   = 8,
191     CMBSZ_SZ_SHIFT    = 12,
192 };
193 
194 enum NvmeCmbszMask {
195     CMBSZ_SQS_MASK   = 0x1,
196     CMBSZ_CQS_MASK   = 0x1,
197     CMBSZ_LISTS_MASK = 0x1,
198     CMBSZ_RDS_MASK   = 0x1,
199     CMBSZ_WDS_MASK   = 0x1,
200     CMBSZ_SZU_MASK   = 0xf,
201     CMBSZ_SZ_MASK    = 0xfffff,
202 };
203 
204 #define NVME_CMBSZ_SQS(cmbsz)  ((cmbsz >> CMBSZ_SQS_SHIFT)   & CMBSZ_SQS_MASK)
205 #define NVME_CMBSZ_CQS(cmbsz)  ((cmbsz >> CMBSZ_CQS_SHIFT)   & CMBSZ_CQS_MASK)
206 #define NVME_CMBSZ_LISTS(cmbsz)((cmbsz >> CMBSZ_LISTS_SHIFT) & CMBSZ_LISTS_MASK)
207 #define NVME_CMBSZ_RDS(cmbsz)  ((cmbsz >> CMBSZ_RDS_SHIFT)   & CMBSZ_RDS_MASK)
208 #define NVME_CMBSZ_WDS(cmbsz)  ((cmbsz >> CMBSZ_WDS_SHIFT)   & CMBSZ_WDS_MASK)
209 #define NVME_CMBSZ_SZU(cmbsz)  ((cmbsz >> CMBSZ_SZU_SHIFT)   & CMBSZ_SZU_MASK)
210 #define NVME_CMBSZ_SZ(cmbsz)   ((cmbsz >> CMBSZ_SZ_SHIFT)    & CMBSZ_SZ_MASK)
211 
212 #define NVME_CMBSZ_SET_SQS(cmbsz, val)   \
213     (cmbsz |= (uint64_t)(val &  CMBSZ_SQS_MASK)  << CMBSZ_SQS_SHIFT)
214 #define NVME_CMBSZ_SET_CQS(cmbsz, val)   \
215     (cmbsz |= (uint64_t)(val & CMBSZ_CQS_MASK) << CMBSZ_CQS_SHIFT)
216 #define NVME_CMBSZ_SET_LISTS(cmbsz, val) \
217     (cmbsz |= (uint64_t)(val & CMBSZ_LISTS_MASK) << CMBSZ_LISTS_SHIFT)
218 #define NVME_CMBSZ_SET_RDS(cmbsz, val)   \
219     (cmbsz |= (uint64_t)(val & CMBSZ_RDS_MASK) << CMBSZ_RDS_SHIFT)
220 #define NVME_CMBSZ_SET_WDS(cmbsz, val)   \
221     (cmbsz |= (uint64_t)(val & CMBSZ_WDS_MASK) << CMBSZ_WDS_SHIFT)
222 #define NVME_CMBSZ_SET_SZU(cmbsz, val)   \
223     (cmbsz |= (uint64_t)(val & CMBSZ_SZU_MASK) << CMBSZ_SZU_SHIFT)
224 #define NVME_CMBSZ_SET_SZ(cmbsz, val)    \
225     (cmbsz |= (uint64_t)(val & CMBSZ_SZ_MASK) << CMBSZ_SZ_SHIFT)
226 
227 #define NVME_CMBSZ_GETSIZE(cmbsz) \
228     (NVME_CMBSZ_SZ(cmbsz) * (1 << (12 + 4 * NVME_CMBSZ_SZU(cmbsz))))
229 
230 enum NvmePmrcapShift {
231     PMRCAP_RDS_SHIFT      = 3,
232     PMRCAP_WDS_SHIFT      = 4,
233     PMRCAP_BIR_SHIFT      = 5,
234     PMRCAP_PMRTU_SHIFT    = 8,
235     PMRCAP_PMRWBM_SHIFT   = 10,
236     PMRCAP_PMRTO_SHIFT    = 16,
237     PMRCAP_CMSS_SHIFT     = 24,
238 };
239 
240 enum NvmePmrcapMask {
241     PMRCAP_RDS_MASK      = 0x1,
242     PMRCAP_WDS_MASK      = 0x1,
243     PMRCAP_BIR_MASK      = 0x7,
244     PMRCAP_PMRTU_MASK    = 0x3,
245     PMRCAP_PMRWBM_MASK   = 0xf,
246     PMRCAP_PMRTO_MASK    = 0xff,
247     PMRCAP_CMSS_MASK     = 0x1,
248 };
249 
250 #define NVME_PMRCAP_RDS(pmrcap)    \
251     ((pmrcap >> PMRCAP_RDS_SHIFT)   & PMRCAP_RDS_MASK)
252 #define NVME_PMRCAP_WDS(pmrcap)    \
253     ((pmrcap >> PMRCAP_WDS_SHIFT)   & PMRCAP_WDS_MASK)
254 #define NVME_PMRCAP_BIR(pmrcap)    \
255     ((pmrcap >> PMRCAP_BIR_SHIFT)   & PMRCAP_BIR_MASK)
256 #define NVME_PMRCAP_PMRTU(pmrcap)    \
257     ((pmrcap >> PMRCAP_PMRTU_SHIFT)   & PMRCAP_PMRTU_MASK)
258 #define NVME_PMRCAP_PMRWBM(pmrcap)    \
259     ((pmrcap >> PMRCAP_PMRWBM_SHIFT)   & PMRCAP_PMRWBM_MASK)
260 #define NVME_PMRCAP_PMRTO(pmrcap)    \
261     ((pmrcap >> PMRCAP_PMRTO_SHIFT)   & PMRCAP_PMRTO_MASK)
262 #define NVME_PMRCAP_CMSS(pmrcap)    \
263     ((pmrcap >> PMRCAP_CMSS_SHIFT)   & PMRCAP_CMSS_MASK)
264 
265 #define NVME_PMRCAP_SET_RDS(pmrcap, val)   \
266     (pmrcap |= (uint64_t)(val & PMRCAP_RDS_MASK) << PMRCAP_RDS_SHIFT)
267 #define NVME_PMRCAP_SET_WDS(pmrcap, val)   \
268     (pmrcap |= (uint64_t)(val & PMRCAP_WDS_MASK) << PMRCAP_WDS_SHIFT)
269 #define NVME_PMRCAP_SET_BIR(pmrcap, val)   \
270     (pmrcap |= (uint64_t)(val & PMRCAP_BIR_MASK) << PMRCAP_BIR_SHIFT)
271 #define NVME_PMRCAP_SET_PMRTU(pmrcap, val)   \
272     (pmrcap |= (uint64_t)(val & PMRCAP_PMRTU_MASK) << PMRCAP_PMRTU_SHIFT)
273 #define NVME_PMRCAP_SET_PMRWBM(pmrcap, val)   \
274     (pmrcap |= (uint64_t)(val & PMRCAP_PMRWBM_MASK) << PMRCAP_PMRWBM_SHIFT)
275 #define NVME_PMRCAP_SET_PMRTO(pmrcap, val)   \
276     (pmrcap |= (uint64_t)(val & PMRCAP_PMRTO_MASK) << PMRCAP_PMRTO_SHIFT)
277 #define NVME_PMRCAP_SET_CMSS(pmrcap, val)   \
278     (pmrcap |= (uint64_t)(val & PMRCAP_CMSS_MASK) << PMRCAP_CMSS_SHIFT)
279 
280 enum NvmePmrctlShift {
281     PMRCTL_EN_SHIFT   = 0,
282 };
283 
284 enum NvmePmrctlMask {
285     PMRCTL_EN_MASK   = 0x1,
286 };
287 
288 #define NVME_PMRCTL_EN(pmrctl)  ((pmrctl >> PMRCTL_EN_SHIFT)   & PMRCTL_EN_MASK)
289 
290 #define NVME_PMRCTL_SET_EN(pmrctl, val)   \
291     (pmrctl |= (uint64_t)(val & PMRCTL_EN_MASK) << PMRCTL_EN_SHIFT)
292 
293 enum NvmePmrstsShift {
294     PMRSTS_ERR_SHIFT    = 0,
295     PMRSTS_NRDY_SHIFT   = 8,
296     PMRSTS_HSTS_SHIFT   = 9,
297     PMRSTS_CBAI_SHIFT   = 12,
298 };
299 
300 enum NvmePmrstsMask {
301     PMRSTS_ERR_MASK    = 0xff,
302     PMRSTS_NRDY_MASK   = 0x1,
303     PMRSTS_HSTS_MASK   = 0x7,
304     PMRSTS_CBAI_MASK   = 0x1,
305 };
306 
307 #define NVME_PMRSTS_ERR(pmrsts)     \
308     ((pmrsts >> PMRSTS_ERR_SHIFT)   & PMRSTS_ERR_MASK)
309 #define NVME_PMRSTS_NRDY(pmrsts)    \
310     ((pmrsts >> PMRSTS_NRDY_SHIFT)   & PMRSTS_NRDY_MASK)
311 #define NVME_PMRSTS_HSTS(pmrsts)    \
312     ((pmrsts >> PMRSTS_HSTS_SHIFT)   & PMRSTS_HSTS_MASK)
313 #define NVME_PMRSTS_CBAI(pmrsts)    \
314     ((pmrsts >> PMRSTS_CBAI_SHIFT)   & PMRSTS_CBAI_MASK)
315 
316 #define NVME_PMRSTS_SET_ERR(pmrsts, val)   \
317     (pmrsts |= (uint64_t)(val & PMRSTS_ERR_MASK) << PMRSTS_ERR_SHIFT)
318 #define NVME_PMRSTS_SET_NRDY(pmrsts, val)   \
319     (pmrsts |= (uint64_t)(val & PMRSTS_NRDY_MASK) << PMRSTS_NRDY_SHIFT)
320 #define NVME_PMRSTS_SET_HSTS(pmrsts, val)   \
321     (pmrsts |= (uint64_t)(val & PMRSTS_HSTS_MASK) << PMRSTS_HSTS_SHIFT)
322 #define NVME_PMRSTS_SET_CBAI(pmrsts, val)   \
323     (pmrsts |= (uint64_t)(val & PMRSTS_CBAI_MASK) << PMRSTS_CBAI_SHIFT)
324 
325 enum NvmePmrebsShift {
326     PMREBS_PMRSZU_SHIFT   = 0,
327     PMREBS_RBB_SHIFT      = 4,
328     PMREBS_PMRWBZ_SHIFT   = 8,
329 };
330 
331 enum NvmePmrebsMask {
332     PMREBS_PMRSZU_MASK   = 0xf,
333     PMREBS_RBB_MASK      = 0x1,
334     PMREBS_PMRWBZ_MASK   = 0xffffff,
335 };
336 
337 #define NVME_PMREBS_PMRSZU(pmrebs)  \
338     ((pmrebs >> PMREBS_PMRSZU_SHIFT)   & PMREBS_PMRSZU_MASK)
339 #define NVME_PMREBS_RBB(pmrebs)     \
340     ((pmrebs >> PMREBS_RBB_SHIFT)   & PMREBS_RBB_MASK)
341 #define NVME_PMREBS_PMRWBZ(pmrebs)  \
342     ((pmrebs >> PMREBS_PMRWBZ_SHIFT)   & PMREBS_PMRWBZ_MASK)
343 
344 #define NVME_PMREBS_SET_PMRSZU(pmrebs, val)   \
345     (pmrebs |= (uint64_t)(val & PMREBS_PMRSZU_MASK) << PMREBS_PMRSZU_SHIFT)
346 #define NVME_PMREBS_SET_RBB(pmrebs, val)   \
347     (pmrebs |= (uint64_t)(val & PMREBS_RBB_MASK) << PMREBS_RBB_SHIFT)
348 #define NVME_PMREBS_SET_PMRWBZ(pmrebs, val)   \
349     (pmrebs |= (uint64_t)(val & PMREBS_PMRWBZ_MASK) << PMREBS_PMRWBZ_SHIFT)
350 
351 enum NvmePmrswtpShift {
352     PMRSWTP_PMRSWTU_SHIFT   = 0,
353     PMRSWTP_PMRSWTV_SHIFT   = 8,
354 };
355 
356 enum NvmePmrswtpMask {
357     PMRSWTP_PMRSWTU_MASK   = 0xf,
358     PMRSWTP_PMRSWTV_MASK   = 0xffffff,
359 };
360 
361 #define NVME_PMRSWTP_PMRSWTU(pmrswtp)   \
362     ((pmrswtp >> PMRSWTP_PMRSWTU_SHIFT)   & PMRSWTP_PMRSWTU_MASK)
363 #define NVME_PMRSWTP_PMRSWTV(pmrswtp)   \
364     ((pmrswtp >> PMRSWTP_PMRSWTV_SHIFT)   & PMRSWTP_PMRSWTV_MASK)
365 
366 #define NVME_PMRSWTP_SET_PMRSWTU(pmrswtp, val)   \
367     (pmrswtp |= (uint64_t)(val & PMRSWTP_PMRSWTU_MASK) << PMRSWTP_PMRSWTU_SHIFT)
368 #define NVME_PMRSWTP_SET_PMRSWTV(pmrswtp, val)   \
369     (pmrswtp |= (uint64_t)(val & PMRSWTP_PMRSWTV_MASK) << PMRSWTP_PMRSWTV_SHIFT)
370 
371 enum NvmePmrmscShift {
372     PMRMSC_CMSE_SHIFT   = 1,
373     PMRMSC_CBA_SHIFT    = 12,
374 };
375 
376 enum NvmePmrmscMask {
377     PMRMSC_CMSE_MASK   = 0x1,
378     PMRMSC_CBA_MASK    = 0xfffffffffffff,
379 };
380 
381 #define NVME_PMRMSC_CMSE(pmrmsc)    \
382     ((pmrmsc >> PMRMSC_CMSE_SHIFT)   & PMRMSC_CMSE_MASK)
383 #define NVME_PMRMSC_CBA(pmrmsc)     \
384     ((pmrmsc >> PMRMSC_CBA_SHIFT)   & PMRMSC_CBA_MASK)
385 
386 #define NVME_PMRMSC_SET_CMSE(pmrmsc, val)   \
387     (pmrmsc |= (uint64_t)(val & PMRMSC_CMSE_MASK) << PMRMSC_CMSE_SHIFT)
388 #define NVME_PMRMSC_SET_CBA(pmrmsc, val)   \
389     (pmrmsc |= (uint64_t)(val & PMRMSC_CBA_MASK) << PMRMSC_CBA_SHIFT)
390 
391 enum NvmeSglDescriptorType {
392     NVME_SGL_DESCR_TYPE_DATA_BLOCK          = 0x0,
393     NVME_SGL_DESCR_TYPE_BIT_BUCKET          = 0x1,
394     NVME_SGL_DESCR_TYPE_SEGMENT             = 0x2,
395     NVME_SGL_DESCR_TYPE_LAST_SEGMENT        = 0x3,
396     NVME_SGL_DESCR_TYPE_KEYED_DATA_BLOCK    = 0x4,
397 
398     NVME_SGL_DESCR_TYPE_VENDOR_SPECIFIC     = 0xf,
399 };
400 
401 enum NvmeSglDescriptorSubtype {
402     NVME_SGL_DESCR_SUBTYPE_ADDRESS = 0x0,
403 };
404 
405 typedef struct QEMU_PACKED NvmeSglDescriptor {
406     uint64_t addr;
407     uint32_t len;
408     uint8_t  rsvd[3];
409     uint8_t  type;
410 } NvmeSglDescriptor;
411 
412 #define NVME_SGL_TYPE(type)     ((type >> 4) & 0xf)
413 #define NVME_SGL_SUBTYPE(type)  (type & 0xf)
414 
415 typedef union NvmeCmdDptr {
416     struct {
417         uint64_t    prp1;
418         uint64_t    prp2;
419     };
420 
421     NvmeSglDescriptor sgl;
422 } NvmeCmdDptr;
423 
424 enum NvmePsdt {
425     NVME_PSDT_PRP                 = 0x0,
426     NVME_PSDT_SGL_MPTR_CONTIGUOUS = 0x1,
427     NVME_PSDT_SGL_MPTR_SGL        = 0x2,
428 };
429 
430 typedef struct QEMU_PACKED NvmeCmd {
431     uint8_t     opcode;
432     uint8_t     flags;
433     uint16_t    cid;
434     uint32_t    nsid;
435     uint64_t    res1;
436     uint64_t    mptr;
437     NvmeCmdDptr dptr;
438     uint32_t    cdw10;
439     uint32_t    cdw11;
440     uint32_t    cdw12;
441     uint32_t    cdw13;
442     uint32_t    cdw14;
443     uint32_t    cdw15;
444 } NvmeCmd;
445 
446 #define NVME_CMD_FLAGS_FUSE(flags) (flags & 0x3)
447 #define NVME_CMD_FLAGS_PSDT(flags) ((flags >> 6) & 0x3)
448 
449 enum NvmeAdminCommands {
450     NVME_ADM_CMD_DELETE_SQ      = 0x00,
451     NVME_ADM_CMD_CREATE_SQ      = 0x01,
452     NVME_ADM_CMD_GET_LOG_PAGE   = 0x02,
453     NVME_ADM_CMD_DELETE_CQ      = 0x04,
454     NVME_ADM_CMD_CREATE_CQ      = 0x05,
455     NVME_ADM_CMD_IDENTIFY       = 0x06,
456     NVME_ADM_CMD_ABORT          = 0x08,
457     NVME_ADM_CMD_SET_FEATURES   = 0x09,
458     NVME_ADM_CMD_GET_FEATURES   = 0x0a,
459     NVME_ADM_CMD_ASYNC_EV_REQ   = 0x0c,
460     NVME_ADM_CMD_ACTIVATE_FW    = 0x10,
461     NVME_ADM_CMD_DOWNLOAD_FW    = 0x11,
462     NVME_ADM_CMD_FORMAT_NVM     = 0x80,
463     NVME_ADM_CMD_SECURITY_SEND  = 0x81,
464     NVME_ADM_CMD_SECURITY_RECV  = 0x82,
465 };
466 
467 enum NvmeIoCommands {
468     NVME_CMD_FLUSH              = 0x00,
469     NVME_CMD_WRITE              = 0x01,
470     NVME_CMD_READ               = 0x02,
471     NVME_CMD_WRITE_UNCOR        = 0x04,
472     NVME_CMD_COMPARE            = 0x05,
473     NVME_CMD_WRITE_ZEROES       = 0x08,
474     NVME_CMD_DSM                = 0x09,
475 };
476 
477 typedef struct QEMU_PACKED NvmeDeleteQ {
478     uint8_t     opcode;
479     uint8_t     flags;
480     uint16_t    cid;
481     uint32_t    rsvd1[9];
482     uint16_t    qid;
483     uint16_t    rsvd10;
484     uint32_t    rsvd11[5];
485 } NvmeDeleteQ;
486 
487 typedef struct QEMU_PACKED NvmeCreateCq {
488     uint8_t     opcode;
489     uint8_t     flags;
490     uint16_t    cid;
491     uint32_t    rsvd1[5];
492     uint64_t    prp1;
493     uint64_t    rsvd8;
494     uint16_t    cqid;
495     uint16_t    qsize;
496     uint16_t    cq_flags;
497     uint16_t    irq_vector;
498     uint32_t    rsvd12[4];
499 } NvmeCreateCq;
500 
501 #define NVME_CQ_FLAGS_PC(cq_flags)  (cq_flags & 0x1)
502 #define NVME_CQ_FLAGS_IEN(cq_flags) ((cq_flags >> 1) & 0x1)
503 
504 typedef struct QEMU_PACKED NvmeCreateSq {
505     uint8_t     opcode;
506     uint8_t     flags;
507     uint16_t    cid;
508     uint32_t    rsvd1[5];
509     uint64_t    prp1;
510     uint64_t    rsvd8;
511     uint16_t    sqid;
512     uint16_t    qsize;
513     uint16_t    sq_flags;
514     uint16_t    cqid;
515     uint32_t    rsvd12[4];
516 } NvmeCreateSq;
517 
518 #define NVME_SQ_FLAGS_PC(sq_flags)      (sq_flags & 0x1)
519 #define NVME_SQ_FLAGS_QPRIO(sq_flags)   ((sq_flags >> 1) & 0x3)
520 
521 enum NvmeQueueFlags {
522     NVME_Q_PC           = 1,
523     NVME_Q_PRIO_URGENT  = 0,
524     NVME_Q_PRIO_HIGH    = 1,
525     NVME_Q_PRIO_NORMAL  = 2,
526     NVME_Q_PRIO_LOW     = 3,
527 };
528 
529 typedef struct QEMU_PACKED NvmeIdentify {
530     uint8_t     opcode;
531     uint8_t     flags;
532     uint16_t    cid;
533     uint32_t    nsid;
534     uint64_t    rsvd2[2];
535     uint64_t    prp1;
536     uint64_t    prp2;
537     uint32_t    cns;
538     uint32_t    rsvd11[5];
539 } NvmeIdentify;
540 
541 typedef struct QEMU_PACKED NvmeRwCmd {
542     uint8_t     opcode;
543     uint8_t     flags;
544     uint16_t    cid;
545     uint32_t    nsid;
546     uint64_t    rsvd2;
547     uint64_t    mptr;
548     NvmeCmdDptr dptr;
549     uint64_t    slba;
550     uint16_t    nlb;
551     uint16_t    control;
552     uint32_t    dsmgmt;
553     uint32_t    reftag;
554     uint16_t    apptag;
555     uint16_t    appmask;
556 } NvmeRwCmd;
557 
558 enum {
559     NVME_RW_LR                  = 1 << 15,
560     NVME_RW_FUA                 = 1 << 14,
561     NVME_RW_DSM_FREQ_UNSPEC     = 0,
562     NVME_RW_DSM_FREQ_TYPICAL    = 1,
563     NVME_RW_DSM_FREQ_RARE       = 2,
564     NVME_RW_DSM_FREQ_READS      = 3,
565     NVME_RW_DSM_FREQ_WRITES     = 4,
566     NVME_RW_DSM_FREQ_RW         = 5,
567     NVME_RW_DSM_FREQ_ONCE       = 6,
568     NVME_RW_DSM_FREQ_PREFETCH   = 7,
569     NVME_RW_DSM_FREQ_TEMP       = 8,
570     NVME_RW_DSM_LATENCY_NONE    = 0 << 4,
571     NVME_RW_DSM_LATENCY_IDLE    = 1 << 4,
572     NVME_RW_DSM_LATENCY_NORM    = 2 << 4,
573     NVME_RW_DSM_LATENCY_LOW     = 3 << 4,
574     NVME_RW_DSM_SEQ_REQ         = 1 << 6,
575     NVME_RW_DSM_COMPRESSED      = 1 << 7,
576     NVME_RW_PRINFO_PRACT        = 1 << 13,
577     NVME_RW_PRINFO_PRCHK_GUARD  = 1 << 12,
578     NVME_RW_PRINFO_PRCHK_APP    = 1 << 11,
579     NVME_RW_PRINFO_PRCHK_REF    = 1 << 10,
580 };
581 
582 typedef struct QEMU_PACKED NvmeDsmCmd {
583     uint8_t     opcode;
584     uint8_t     flags;
585     uint16_t    cid;
586     uint32_t    nsid;
587     uint64_t    rsvd2[2];
588     NvmeCmdDptr dptr;
589     uint32_t    nr;
590     uint32_t    attributes;
591     uint32_t    rsvd12[4];
592 } NvmeDsmCmd;
593 
594 enum {
595     NVME_DSMGMT_IDR = 1 << 0,
596     NVME_DSMGMT_IDW = 1 << 1,
597     NVME_DSMGMT_AD  = 1 << 2,
598 };
599 
600 typedef struct QEMU_PACKED NvmeDsmRange {
601     uint32_t    cattr;
602     uint32_t    nlb;
603     uint64_t    slba;
604 } NvmeDsmRange;
605 
606 enum NvmeAsyncEventRequest {
607     NVME_AER_TYPE_ERROR                     = 0,
608     NVME_AER_TYPE_SMART                     = 1,
609     NVME_AER_TYPE_IO_SPECIFIC               = 6,
610     NVME_AER_TYPE_VENDOR_SPECIFIC           = 7,
611     NVME_AER_INFO_ERR_INVALID_DB_REGISTER   = 0,
612     NVME_AER_INFO_ERR_INVALID_DB_VALUE      = 1,
613     NVME_AER_INFO_ERR_DIAG_FAIL             = 2,
614     NVME_AER_INFO_ERR_PERS_INTERNAL_ERR     = 3,
615     NVME_AER_INFO_ERR_TRANS_INTERNAL_ERR    = 4,
616     NVME_AER_INFO_ERR_FW_IMG_LOAD_ERR       = 5,
617     NVME_AER_INFO_SMART_RELIABILITY         = 0,
618     NVME_AER_INFO_SMART_TEMP_THRESH         = 1,
619     NVME_AER_INFO_SMART_SPARE_THRESH        = 2,
620 };
621 
622 typedef struct QEMU_PACKED NvmeAerResult {
623     uint8_t event_type;
624     uint8_t event_info;
625     uint8_t log_page;
626     uint8_t resv;
627 } NvmeAerResult;
628 
629 typedef struct QEMU_PACKED NvmeCqe {
630     uint32_t    result;
631     uint32_t    rsvd;
632     uint16_t    sq_head;
633     uint16_t    sq_id;
634     uint16_t    cid;
635     uint16_t    status;
636 } NvmeCqe;
637 
638 enum NvmeStatusCodes {
639     NVME_SUCCESS                = 0x0000,
640     NVME_INVALID_OPCODE         = 0x0001,
641     NVME_INVALID_FIELD          = 0x0002,
642     NVME_CID_CONFLICT           = 0x0003,
643     NVME_DATA_TRAS_ERROR        = 0x0004,
644     NVME_POWER_LOSS_ABORT       = 0x0005,
645     NVME_INTERNAL_DEV_ERROR     = 0x0006,
646     NVME_CMD_ABORT_REQ          = 0x0007,
647     NVME_CMD_ABORT_SQ_DEL       = 0x0008,
648     NVME_CMD_ABORT_FAILED_FUSE  = 0x0009,
649     NVME_CMD_ABORT_MISSING_FUSE = 0x000a,
650     NVME_INVALID_NSID           = 0x000b,
651     NVME_CMD_SEQ_ERROR          = 0x000c,
652     NVME_INVALID_SGL_SEG_DESCR  = 0x000d,
653     NVME_INVALID_NUM_SGL_DESCRS = 0x000e,
654     NVME_DATA_SGL_LEN_INVALID   = 0x000f,
655     NVME_MD_SGL_LEN_INVALID     = 0x0010,
656     NVME_SGL_DESCR_TYPE_INVALID = 0x0011,
657     NVME_INVALID_USE_OF_CMB     = 0x0012,
658     NVME_INVALID_PRP_OFFSET     = 0x0013,
659     NVME_LBA_RANGE              = 0x0080,
660     NVME_CAP_EXCEEDED           = 0x0081,
661     NVME_NS_NOT_READY           = 0x0082,
662     NVME_NS_RESV_CONFLICT       = 0x0083,
663     NVME_INVALID_CQID           = 0x0100,
664     NVME_INVALID_QID            = 0x0101,
665     NVME_MAX_QSIZE_EXCEEDED     = 0x0102,
666     NVME_ACL_EXCEEDED           = 0x0103,
667     NVME_RESERVED               = 0x0104,
668     NVME_AER_LIMIT_EXCEEDED     = 0x0105,
669     NVME_INVALID_FW_SLOT        = 0x0106,
670     NVME_INVALID_FW_IMAGE       = 0x0107,
671     NVME_INVALID_IRQ_VECTOR     = 0x0108,
672     NVME_INVALID_LOG_ID         = 0x0109,
673     NVME_INVALID_FORMAT         = 0x010a,
674     NVME_FW_REQ_RESET           = 0x010b,
675     NVME_INVALID_QUEUE_DEL      = 0x010c,
676     NVME_FID_NOT_SAVEABLE       = 0x010d,
677     NVME_FEAT_NOT_CHANGEABLE    = 0x010e,
678     NVME_FEAT_NOT_NS_SPEC       = 0x010f,
679     NVME_FW_REQ_SUSYSTEM_RESET  = 0x0110,
680     NVME_CONFLICTING_ATTRS      = 0x0180,
681     NVME_INVALID_PROT_INFO      = 0x0181,
682     NVME_WRITE_TO_RO            = 0x0182,
683     NVME_WRITE_FAULT            = 0x0280,
684     NVME_UNRECOVERED_READ       = 0x0281,
685     NVME_E2E_GUARD_ERROR        = 0x0282,
686     NVME_E2E_APP_ERROR          = 0x0283,
687     NVME_E2E_REF_ERROR          = 0x0284,
688     NVME_CMP_FAILURE            = 0x0285,
689     NVME_ACCESS_DENIED          = 0x0286,
690     NVME_MORE                   = 0x2000,
691     NVME_DNR                    = 0x4000,
692     NVME_NO_COMPLETE            = 0xffff,
693 };
694 
695 typedef struct QEMU_PACKED NvmeFwSlotInfoLog {
696     uint8_t     afi;
697     uint8_t     reserved1[7];
698     uint8_t     frs1[8];
699     uint8_t     frs2[8];
700     uint8_t     frs3[8];
701     uint8_t     frs4[8];
702     uint8_t     frs5[8];
703     uint8_t     frs6[8];
704     uint8_t     frs7[8];
705     uint8_t     reserved2[448];
706 } NvmeFwSlotInfoLog;
707 
708 typedef struct QEMU_PACKED NvmeErrorLog {
709     uint64_t    error_count;
710     uint16_t    sqid;
711     uint16_t    cid;
712     uint16_t    status_field;
713     uint16_t    param_error_location;
714     uint64_t    lba;
715     uint32_t    nsid;
716     uint8_t     vs;
717     uint8_t     resv[35];
718 } NvmeErrorLog;
719 
720 typedef struct QEMU_PACKED NvmeSmartLog {
721     uint8_t     critical_warning;
722     uint16_t    temperature;
723     uint8_t     available_spare;
724     uint8_t     available_spare_threshold;
725     uint8_t     percentage_used;
726     uint8_t     reserved1[26];
727     uint64_t    data_units_read[2];
728     uint64_t    data_units_written[2];
729     uint64_t    host_read_commands[2];
730     uint64_t    host_write_commands[2];
731     uint64_t    controller_busy_time[2];
732     uint64_t    power_cycles[2];
733     uint64_t    power_on_hours[2];
734     uint64_t    unsafe_shutdowns[2];
735     uint64_t    media_errors[2];
736     uint64_t    number_of_error_log_entries[2];
737     uint8_t     reserved2[320];
738 } NvmeSmartLog;
739 
740 enum NvmeSmartWarn {
741     NVME_SMART_SPARE                  = 1 << 0,
742     NVME_SMART_TEMPERATURE            = 1 << 1,
743     NVME_SMART_RELIABILITY            = 1 << 2,
744     NVME_SMART_MEDIA_READ_ONLY        = 1 << 3,
745     NVME_SMART_FAILED_VOLATILE_MEDIA  = 1 << 4,
746 };
747 
748 enum NvmeLogIdentifier {
749     NVME_LOG_ERROR_INFO     = 0x01,
750     NVME_LOG_SMART_INFO     = 0x02,
751     NVME_LOG_FW_SLOT_INFO   = 0x03,
752 };
753 
754 typedef struct QEMU_PACKED NvmePSD {
755     uint16_t    mp;
756     uint16_t    reserved;
757     uint32_t    enlat;
758     uint32_t    exlat;
759     uint8_t     rrt;
760     uint8_t     rrl;
761     uint8_t     rwt;
762     uint8_t     rwl;
763     uint8_t     resv[16];
764 } NvmePSD;
765 
766 #define NVME_IDENTIFY_DATA_SIZE 4096
767 
768 enum {
769     NVME_ID_CNS_NS             = 0x0,
770     NVME_ID_CNS_CTRL           = 0x1,
771     NVME_ID_CNS_NS_ACTIVE_LIST = 0x2,
772     NVME_ID_CNS_NS_DESCR_LIST  = 0x3,
773 };
774 
775 typedef struct QEMU_PACKED NvmeIdCtrl {
776     uint16_t    vid;
777     uint16_t    ssvid;
778     uint8_t     sn[20];
779     uint8_t     mn[40];
780     uint8_t     fr[8];
781     uint8_t     rab;
782     uint8_t     ieee[3];
783     uint8_t     cmic;
784     uint8_t     mdts;
785     uint16_t    cntlid;
786     uint32_t    ver;
787     uint32_t    rtd3r;
788     uint32_t    rtd3e;
789     uint32_t    oaes;
790     uint32_t    ctratt;
791     uint8_t     rsvd100[12];
792     uint8_t     fguid[16];
793     uint8_t     rsvd128[128];
794     uint16_t    oacs;
795     uint8_t     acl;
796     uint8_t     aerl;
797     uint8_t     frmw;
798     uint8_t     lpa;
799     uint8_t     elpe;
800     uint8_t     npss;
801     uint8_t     avscc;
802     uint8_t     apsta;
803     uint16_t    wctemp;
804     uint16_t    cctemp;
805     uint16_t    mtfa;
806     uint32_t    hmpre;
807     uint32_t    hmmin;
808     uint8_t     tnvmcap[16];
809     uint8_t     unvmcap[16];
810     uint32_t    rpmbs;
811     uint16_t    edstt;
812     uint8_t     dsto;
813     uint8_t     fwug;
814     uint16_t    kas;
815     uint16_t    hctma;
816     uint16_t    mntmt;
817     uint16_t    mxtmt;
818     uint32_t    sanicap;
819     uint8_t     rsvd332[180];
820     uint8_t     sqes;
821     uint8_t     cqes;
822     uint16_t    maxcmd;
823     uint32_t    nn;
824     uint16_t    oncs;
825     uint16_t    fuses;
826     uint8_t     fna;
827     uint8_t     vwc;
828     uint16_t    awun;
829     uint16_t    awupf;
830     uint8_t     nvscc;
831     uint8_t     rsvd531;
832     uint16_t    acwu;
833     uint8_t     rsvd534[2];
834     uint32_t    sgls;
835     uint8_t     rsvd540[228];
836     uint8_t     subnqn[256];
837     uint8_t     rsvd1024[1024];
838     NvmePSD     psd[32];
839     uint8_t     vs[1024];
840 } NvmeIdCtrl;
841 
842 enum NvmeIdCtrlOacs {
843     NVME_OACS_SECURITY  = 1 << 0,
844     NVME_OACS_FORMAT    = 1 << 1,
845     NVME_OACS_FW        = 1 << 2,
846 };
847 
848 enum NvmeIdCtrlOncs {
849     NVME_ONCS_COMPARE       = 1 << 0,
850     NVME_ONCS_WRITE_UNCORR  = 1 << 1,
851     NVME_ONCS_DSM           = 1 << 2,
852     NVME_ONCS_WRITE_ZEROES  = 1 << 3,
853     NVME_ONCS_FEATURES      = 1 << 4,
854     NVME_ONCS_RESRVATIONS   = 1 << 5,
855     NVME_ONCS_TIMESTAMP     = 1 << 6,
856 };
857 
858 enum NvmeIdCtrlFrmw {
859     NVME_FRMW_SLOT1_RO = 1 << 0,
860 };
861 
862 enum NvmeIdCtrlLpa {
863     NVME_LPA_NS_SMART = 1 << 0,
864     NVME_LPA_EXTENDED = 1 << 2,
865 };
866 
867 #define NVME_CTRL_SQES_MIN(sqes) ((sqes) & 0xf)
868 #define NVME_CTRL_SQES_MAX(sqes) (((sqes) >> 4) & 0xf)
869 #define NVME_CTRL_CQES_MIN(cqes) ((cqes) & 0xf)
870 #define NVME_CTRL_CQES_MAX(cqes) (((cqes) >> 4) & 0xf)
871 
872 #define NVME_CTRL_SGLS_SUPPORT_MASK        (0x3 <<  0)
873 #define NVME_CTRL_SGLS_SUPPORT_NO_ALIGN    (0x1 <<  0)
874 #define NVME_CTRL_SGLS_SUPPORT_DWORD_ALIGN (0x1 <<  1)
875 #define NVME_CTRL_SGLS_KEYED               (0x1 <<  2)
876 #define NVME_CTRL_SGLS_BITBUCKET           (0x1 << 16)
877 #define NVME_CTRL_SGLS_MPTR_CONTIGUOUS     (0x1 << 17)
878 #define NVME_CTRL_SGLS_EXCESS_LENGTH       (0x1 << 18)
879 #define NVME_CTRL_SGLS_MPTR_SGL            (0x1 << 19)
880 #define NVME_CTRL_SGLS_ADDR_OFFSET         (0x1 << 20)
881 
882 #define NVME_ARB_AB(arb)    (arb & 0x7)
883 #define NVME_ARB_AB_NOLIMIT 0x7
884 #define NVME_ARB_LPW(arb)   ((arb >> 8) & 0xff)
885 #define NVME_ARB_MPW(arb)   ((arb >> 16) & 0xff)
886 #define NVME_ARB_HPW(arb)   ((arb >> 24) & 0xff)
887 
888 #define NVME_INTC_THR(intc)     (intc & 0xff)
889 #define NVME_INTC_TIME(intc)    ((intc >> 8) & 0xff)
890 
891 #define NVME_INTVC_NOCOALESCING (0x1 << 16)
892 
893 #define NVME_TEMP_THSEL(temp)  ((temp >> 20) & 0x3)
894 #define NVME_TEMP_THSEL_OVER   0x0
895 #define NVME_TEMP_THSEL_UNDER  0x1
896 
897 #define NVME_TEMP_TMPSEL(temp)     ((temp >> 16) & 0xf)
898 #define NVME_TEMP_TMPSEL_COMPOSITE 0x0
899 
900 #define NVME_TEMP_TMPTH(temp) (temp & 0xffff)
901 
902 #define NVME_AEC_SMART(aec)         (aec & 0xff)
903 #define NVME_AEC_NS_ATTR(aec)       ((aec >> 8) & 0x1)
904 #define NVME_AEC_FW_ACTIVATION(aec) ((aec >> 9) & 0x1)
905 
906 enum NvmeFeatureIds {
907     NVME_ARBITRATION                = 0x1,
908     NVME_POWER_MANAGEMENT           = 0x2,
909     NVME_LBA_RANGE_TYPE             = 0x3,
910     NVME_TEMPERATURE_THRESHOLD      = 0x4,
911     NVME_ERROR_RECOVERY             = 0x5,
912     NVME_VOLATILE_WRITE_CACHE       = 0x6,
913     NVME_NUMBER_OF_QUEUES           = 0x7,
914     NVME_INTERRUPT_COALESCING       = 0x8,
915     NVME_INTERRUPT_VECTOR_CONF      = 0x9,
916     NVME_WRITE_ATOMICITY            = 0xa,
917     NVME_ASYNCHRONOUS_EVENT_CONF    = 0xb,
918     NVME_TIMESTAMP                  = 0xe,
919     NVME_SOFTWARE_PROGRESS_MARKER   = 0x80,
920     NVME_FID_MAX                    = 0x100,
921 };
922 
923 typedef enum NvmeFeatureCap {
924     NVME_FEAT_CAP_SAVE      = 1 << 0,
925     NVME_FEAT_CAP_NS        = 1 << 1,
926     NVME_FEAT_CAP_CHANGE    = 1 << 2,
927 } NvmeFeatureCap;
928 
929 typedef enum NvmeGetFeatureSelect {
930     NVME_GETFEAT_SELECT_CURRENT = 0x0,
931     NVME_GETFEAT_SELECT_DEFAULT = 0x1,
932     NVME_GETFEAT_SELECT_SAVED   = 0x2,
933     NVME_GETFEAT_SELECT_CAP     = 0x3,
934 } NvmeGetFeatureSelect;
935 
936 #define NVME_GETSETFEAT_FID_MASK 0xff
937 #define NVME_GETSETFEAT_FID(dw10) (dw10 & NVME_GETSETFEAT_FID_MASK)
938 
939 #define NVME_GETFEAT_SELECT_SHIFT 8
940 #define NVME_GETFEAT_SELECT_MASK  0x7
941 #define NVME_GETFEAT_SELECT(dw10) \
942     ((dw10 >> NVME_GETFEAT_SELECT_SHIFT) & NVME_GETFEAT_SELECT_MASK)
943 
944 #define NVME_SETFEAT_SAVE_SHIFT 31
945 #define NVME_SETFEAT_SAVE_MASK  0x1
946 #define NVME_SETFEAT_SAVE(dw10) \
947     ((dw10 >> NVME_SETFEAT_SAVE_SHIFT) & NVME_SETFEAT_SAVE_MASK)
948 
949 typedef struct QEMU_PACKED NvmeRangeType {
950     uint8_t     type;
951     uint8_t     attributes;
952     uint8_t     rsvd2[14];
953     uint64_t    slba;
954     uint64_t    nlb;
955     uint8_t     guid[16];
956     uint8_t     rsvd48[16];
957 } NvmeRangeType;
958 
959 typedef struct QEMU_PACKED NvmeLBAF {
960     uint16_t    ms;
961     uint8_t     ds;
962     uint8_t     rp;
963 } NvmeLBAF;
964 
965 #define NVME_NSID_BROADCAST 0xffffffff
966 
967 typedef struct QEMU_PACKED NvmeIdNs {
968     uint64_t    nsze;
969     uint64_t    ncap;
970     uint64_t    nuse;
971     uint8_t     nsfeat;
972     uint8_t     nlbaf;
973     uint8_t     flbas;
974     uint8_t     mc;
975     uint8_t     dpc;
976     uint8_t     dps;
977     uint8_t     nmic;
978     uint8_t     rescap;
979     uint8_t     fpi;
980     uint8_t     dlfeat;
981     uint16_t    nawun;
982     uint16_t    nawupf;
983     uint16_t    nacwu;
984     uint16_t    nabsn;
985     uint16_t    nabo;
986     uint16_t    nabspf;
987     uint16_t    noiob;
988     uint8_t     nvmcap[16];
989     uint8_t     rsvd64[40];
990     uint8_t     nguid[16];
991     uint64_t    eui64;
992     NvmeLBAF    lbaf[16];
993     uint8_t     rsvd192[192];
994     uint8_t     vs[3712];
995 } NvmeIdNs;
996 
997 typedef struct QEMU_PACKED NvmeIdNsDescr {
998     uint8_t nidt;
999     uint8_t nidl;
1000     uint8_t rsvd2[2];
1001 } NvmeIdNsDescr;
1002 
1003 enum {
1004     NVME_NIDT_EUI64_LEN =  8,
1005     NVME_NIDT_NGUID_LEN = 16,
1006     NVME_NIDT_UUID_LEN  = 16,
1007 };
1008 
1009 enum NvmeNsIdentifierType {
1010     NVME_NIDT_EUI64 = 0x1,
1011     NVME_NIDT_NGUID = 0x2,
1012     NVME_NIDT_UUID  = 0x3,
1013 };
1014 
1015 /*Deallocate Logical Block Features*/
1016 #define NVME_ID_NS_DLFEAT_GUARD_CRC(dlfeat)       ((dlfeat) & 0x10)
1017 #define NVME_ID_NS_DLFEAT_WRITE_ZEROES(dlfeat)    ((dlfeat) & 0x08)
1018 
1019 #define NVME_ID_NS_DLFEAT_READ_BEHAVIOR(dlfeat)     ((dlfeat) & 0x7)
1020 #define NVME_ID_NS_DLFEAT_READ_BEHAVIOR_UNDEFINED   0
1021 #define NVME_ID_NS_DLFEAT_READ_BEHAVIOR_ZEROES      1
1022 #define NVME_ID_NS_DLFEAT_READ_BEHAVIOR_ONES        2
1023 
1024 
1025 #define NVME_ID_NS_NSFEAT_THIN(nsfeat)      ((nsfeat & 0x1))
1026 #define NVME_ID_NS_FLBAS_EXTENDED(flbas)    ((flbas >> 4) & 0x1)
1027 #define NVME_ID_NS_FLBAS_INDEX(flbas)       ((flbas & 0xf))
1028 #define NVME_ID_NS_MC_SEPARATE(mc)          ((mc >> 1) & 0x1)
1029 #define NVME_ID_NS_MC_EXTENDED(mc)          ((mc & 0x1))
1030 #define NVME_ID_NS_DPC_LAST_EIGHT(dpc)      ((dpc >> 4) & 0x1)
1031 #define NVME_ID_NS_DPC_FIRST_EIGHT(dpc)     ((dpc >> 3) & 0x1)
1032 #define NVME_ID_NS_DPC_TYPE_3(dpc)          ((dpc >> 2) & 0x1)
1033 #define NVME_ID_NS_DPC_TYPE_2(dpc)          ((dpc >> 1) & 0x1)
1034 #define NVME_ID_NS_DPC_TYPE_1(dpc)          ((dpc & 0x1))
1035 #define NVME_ID_NS_DPC_TYPE_MASK            0x7
1036 
1037 enum NvmeIdNsDps {
1038     DPS_TYPE_NONE   = 0,
1039     DPS_TYPE_1      = 1,
1040     DPS_TYPE_2      = 2,
1041     DPS_TYPE_3      = 3,
1042     DPS_TYPE_MASK   = 0x7,
1043     DPS_FIRST_EIGHT = 8,
1044 };
1045 
1046 static inline void _nvme_check_size(void)
1047 {
1048     QEMU_BUILD_BUG_ON(sizeof(NvmeBar) != 4096);
1049     QEMU_BUILD_BUG_ON(sizeof(NvmeAerResult) != 4);
1050     QEMU_BUILD_BUG_ON(sizeof(NvmeCqe) != 16);
1051     QEMU_BUILD_BUG_ON(sizeof(NvmeDsmRange) != 16);
1052     QEMU_BUILD_BUG_ON(sizeof(NvmeCmd) != 64);
1053     QEMU_BUILD_BUG_ON(sizeof(NvmeDeleteQ) != 64);
1054     QEMU_BUILD_BUG_ON(sizeof(NvmeCreateCq) != 64);
1055     QEMU_BUILD_BUG_ON(sizeof(NvmeCreateSq) != 64);
1056     QEMU_BUILD_BUG_ON(sizeof(NvmeIdentify) != 64);
1057     QEMU_BUILD_BUG_ON(sizeof(NvmeRwCmd) != 64);
1058     QEMU_BUILD_BUG_ON(sizeof(NvmeDsmCmd) != 64);
1059     QEMU_BUILD_BUG_ON(sizeof(NvmeRangeType) != 64);
1060     QEMU_BUILD_BUG_ON(sizeof(NvmeErrorLog) != 64);
1061     QEMU_BUILD_BUG_ON(sizeof(NvmeFwSlotInfoLog) != 512);
1062     QEMU_BUILD_BUG_ON(sizeof(NvmeSmartLog) != 512);
1063     QEMU_BUILD_BUG_ON(sizeof(NvmeIdCtrl) != 4096);
1064     QEMU_BUILD_BUG_ON(sizeof(NvmeIdNs) != 4096);
1065     QEMU_BUILD_BUG_ON(sizeof(NvmeSglDescriptor) != 16);
1066     QEMU_BUILD_BUG_ON(sizeof(NvmeIdNsDescr) != 4);
1067 }
1068 #endif
1069