xref: /openbmc/qemu/hw/nvme/ctrl.c (revision 159fb790)
1 /*
2  * QEMU NVM Express Controller
3  *
4  * Copyright (c) 2012, Intel Corporation
5  *
6  * Written by Keith Busch <keith.busch@intel.com>
7  *
8  * This code is licensed under the GNU GPL v2 or later.
9  */
10 
11 /**
12  * Reference Specs: http://www.nvmexpress.org, 1.4, 1.3, 1.2, 1.1, 1.0e
13  *
14  *  https://nvmexpress.org/developers/nvme-specification/
15  *
16  *
17  * Notes on coding style
18  * ---------------------
19  * While QEMU coding style prefers lowercase hexadecimals in constants, the
20  * NVMe subsystem use this format from the NVMe specifications in the comments
21  * (i.e. 'h' suffix instead of '0x' prefix).
22  *
23  * Usage
24  * -----
25  * See docs/system/nvme.rst for extensive documentation.
26  *
27  * Add options:
28  *      -drive file=<file>,if=none,id=<drive_id>
29  *      -device nvme-subsys,id=<subsys_id>,nqn=<nqn_id>
30  *      -device nvme,serial=<serial>,id=<bus_name>, \
31  *              cmb_size_mb=<cmb_size_mb[optional]>, \
32  *              [pmrdev=<mem_backend_file_id>,] \
33  *              max_ioqpairs=<N[optional]>, \
34  *              aerl=<N[optional]>,aer_max_queued=<N[optional]>, \
35  *              mdts=<N[optional]>,vsl=<N[optional]>, \
36  *              zoned.zasl=<N[optional]>, \
37  *              zoned.auto_transition=<on|off[optional]>, \
38  *              sriov_max_vfs=<N[optional]> \
39  *              sriov_vq_flexible=<N[optional]> \
40  *              sriov_vi_flexible=<N[optional]> \
41  *              sriov_max_vi_per_vf=<N[optional]> \
42  *              sriov_max_vq_per_vf=<N[optional]> \
43  *              subsys=<subsys_id>
44  *      -device nvme-ns,drive=<drive_id>,bus=<bus_name>,nsid=<nsid>,\
45  *              zoned=<true|false[optional]>, \
46  *              subsys=<subsys_id>,shared=<true|false[optional]>, \
47  *              detached=<true|false[optional]>, \
48  *              zoned.zone_size=<N[optional]>, \
49  *              zoned.zone_capacity=<N[optional]>, \
50  *              zoned.descr_ext_size=<N[optional]>, \
51  *              zoned.max_active=<N[optional]>, \
52  *              zoned.max_open=<N[optional]>, \
53  *              zoned.cross_read=<true|false[optional]>
54  *
55  * Note cmb_size_mb denotes size of CMB in MB. CMB is assumed to be at
56  * offset 0 in BAR2 and supports only WDS, RDS and SQS for now. By default, the
57  * device will use the "v1.4 CMB scheme" - use the `legacy-cmb` parameter to
58  * always enable the CMBLOC and CMBSZ registers (v1.3 behavior).
59  *
60  * Enabling pmr emulation can be achieved by pointing to memory-backend-file.
61  * For example:
62  * -object memory-backend-file,id=<mem_id>,share=on,mem-path=<file_path>, \
63  *  size=<size> .... -device nvme,...,pmrdev=<mem_id>
64  *
65  * The PMR will use BAR 4/5 exclusively.
66  *
67  * To place controller(s) and namespace(s) to a subsystem, then provide
68  * nvme-subsys device as above.
69  *
70  * nvme subsystem device parameters
71  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72  * - `nqn`
73  *   This parameter provides the `<nqn_id>` part of the string
74  *   `nqn.2019-08.org.qemu:<nqn_id>` which will be reported in the SUBNQN field
75  *   of subsystem controllers. Note that `<nqn_id>` should be unique per
76  *   subsystem, but this is not enforced by QEMU. If not specified, it will
77  *   default to the value of the `id` parameter (`<subsys_id>`).
78  *
79  * nvme device parameters
80  * ~~~~~~~~~~~~~~~~~~~~~~
81  * - `subsys`
82  *   Specifying this parameter attaches the controller to the subsystem and
83  *   the SUBNQN field in the controller will report the NQN of the subsystem
84  *   device. This also enables multi controller capability represented in
85  *   Identify Controller data structure in CMIC (Controller Multi-path I/O and
86  *   Namespace Sharing Capabilities).
87  *
88  * - `aerl`
89  *   The Asynchronous Event Request Limit (AERL). Indicates the maximum number
90  *   of concurrently outstanding Asynchronous Event Request commands support
91  *   by the controller. This is a 0's based value.
92  *
93  * - `aer_max_queued`
94  *   This is the maximum number of events that the device will enqueue for
95  *   completion when there are no outstanding AERs. When the maximum number of
96  *   enqueued events are reached, subsequent events will be dropped.
97  *
98  * - `mdts`
99  *   Indicates the maximum data transfer size for a command that transfers data
100  *   between host-accessible memory and the controller. The value is specified
101  *   as a power of two (2^n) and is in units of the minimum memory page size
102  *   (CAP.MPSMIN). The default value is 7 (i.e. 512 KiB).
103  *
104  * - `vsl`
105  *   Indicates the maximum data size limit for the Verify command. Like `mdts`,
106  *   this value is specified as a power of two (2^n) and is in units of the
107  *   minimum memory page size (CAP.MPSMIN). The default value is 7 (i.e. 512
108  *   KiB).
109  *
110  * - `zoned.zasl`
111  *   Indicates the maximum data transfer size for the Zone Append command. Like
112  *   `mdts`, the value is specified as a power of two (2^n) and is in units of
113  *   the minimum memory page size (CAP.MPSMIN). The default value is 0 (i.e.
114  *   defaulting to the value of `mdts`).
115  *
116  * - `zoned.auto_transition`
117  *   Indicates if zones in zone state implicitly opened can be automatically
118  *   transitioned to zone state closed for resource management purposes.
119  *   Defaults to 'on'.
120  *
121  * - `sriov_max_vfs`
122  *   Indicates the maximum number of PCIe virtual functions supported
123  *   by the controller. The default value is 0. Specifying a non-zero value
124  *   enables reporting of both SR-IOV and ARI capabilities by the NVMe device.
125  *   Virtual function controllers will not report SR-IOV capability.
126  *
127  *   NOTE: Single Root I/O Virtualization support is experimental.
128  *   All the related parameters may be subject to change.
129  *
130  * - `sriov_vq_flexible`
131  *   Indicates the total number of flexible queue resources assignable to all
132  *   the secondary controllers. Implicitly sets the number of primary
133  *   controller's private resources to `(max_ioqpairs - sriov_vq_flexible)`.
134  *
135  * - `sriov_vi_flexible`
136  *   Indicates the total number of flexible interrupt resources assignable to
137  *   all the secondary controllers. Implicitly sets the number of primary
138  *   controller's private resources to `(msix_qsize - sriov_vi_flexible)`.
139  *
140  * - `sriov_max_vi_per_vf`
141  *   Indicates the maximum number of virtual interrupt resources assignable
142  *   to a secondary controller. The default 0 resolves to
143  *   `(sriov_vi_flexible / sriov_max_vfs)`.
144  *
145  * - `sriov_max_vq_per_vf`
146  *   Indicates the maximum number of virtual queue resources assignable to
147  *   a secondary controller. The default 0 resolves to
148  *   `(sriov_vq_flexible / sriov_max_vfs)`.
149  *
150  * nvme namespace device parameters
151  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
152  * - `shared`
153  *   When the parent nvme device (as defined explicitly by the 'bus' parameter
154  *   or implicitly by the most recently defined NvmeBus) is linked to an
155  *   nvme-subsys device, the namespace will be attached to all controllers in
156  *   the subsystem. If set to 'off' (the default), the namespace will remain a
157  *   private namespace and may only be attached to a single controller at a
158  *   time.
159  *
160  * - `detached`
161  *   This parameter is only valid together with the `subsys` parameter. If left
162  *   at the default value (`false/off`), the namespace will be attached to all
163  *   controllers in the NVMe subsystem at boot-up. If set to `true/on`, the
164  *   namespace will be available in the subsystem but not attached to any
165  *   controllers.
166  *
167  * Setting `zoned` to true selects Zoned Command Set at the namespace.
168  * In this case, the following namespace properties are available to configure
169  * zoned operation:
170  *     zoned.zone_size=<zone size in bytes, default: 128MiB>
171  *         The number may be followed by K, M, G as in kilo-, mega- or giga-.
172  *
173  *     zoned.zone_capacity=<zone capacity in bytes, default: zone size>
174  *         The value 0 (default) forces zone capacity to be the same as zone
175  *         size. The value of this property may not exceed zone size.
176  *
177  *     zoned.descr_ext_size=<zone descriptor extension size, default 0>
178  *         This value needs to be specified in 64B units. If it is zero,
179  *         namespace(s) will not support zone descriptor extensions.
180  *
181  *     zoned.max_active=<Maximum Active Resources (zones), default: 0>
182  *         The default value means there is no limit to the number of
183  *         concurrently active zones.
184  *
185  *     zoned.max_open=<Maximum Open Resources (zones), default: 0>
186  *         The default value means there is no limit to the number of
187  *         concurrently open zones.
188  *
189  *     zoned.cross_read=<enable RAZB, default: false>
190  *         Setting this property to true enables Read Across Zone Boundaries.
191  */
192 
193 #include "qemu/osdep.h"
194 #include "qemu/cutils.h"
195 #include "qemu/error-report.h"
196 #include "qemu/log.h"
197 #include "qemu/units.h"
198 #include "qemu/range.h"
199 #include "qapi/error.h"
200 #include "qapi/visitor.h"
201 #include "sysemu/sysemu.h"
202 #include "sysemu/block-backend.h"
203 #include "sysemu/hostmem.h"
204 #include "hw/pci/msix.h"
205 #include "hw/pci/pcie_sriov.h"
206 #include "migration/vmstate.h"
207 
208 #include "nvme.h"
209 #include "dif.h"
210 #include "trace.h"
211 
212 #define NVME_MAX_IOQPAIRS 0xffff
213 #define NVME_DB_SIZE  4
214 #define NVME_SPEC_VER 0x00010400
215 #define NVME_CMB_BIR 2
216 #define NVME_PMR_BIR 4
217 #define NVME_TEMPERATURE 0x143
218 #define NVME_TEMPERATURE_WARNING 0x157
219 #define NVME_TEMPERATURE_CRITICAL 0x175
220 #define NVME_NUM_FW_SLOTS 1
221 #define NVME_DEFAULT_MAX_ZA_SIZE (128 * KiB)
222 #define NVME_MAX_VFS 127
223 #define NVME_VF_RES_GRANULARITY 1
224 #define NVME_VF_OFFSET 0x1
225 #define NVME_VF_STRIDE 1
226 
227 #define NVME_GUEST_ERR(trace, fmt, ...) \
228     do { \
229         (trace_##trace)(__VA_ARGS__); \
230         qemu_log_mask(LOG_GUEST_ERROR, #trace \
231             " in %s: " fmt "\n", __func__, ## __VA_ARGS__); \
232     } while (0)
233 
234 static const bool nvme_feature_support[NVME_FID_MAX] = {
235     [NVME_ARBITRATION]              = true,
236     [NVME_POWER_MANAGEMENT]         = true,
237     [NVME_TEMPERATURE_THRESHOLD]    = true,
238     [NVME_ERROR_RECOVERY]           = true,
239     [NVME_VOLATILE_WRITE_CACHE]     = true,
240     [NVME_NUMBER_OF_QUEUES]         = true,
241     [NVME_INTERRUPT_COALESCING]     = true,
242     [NVME_INTERRUPT_VECTOR_CONF]    = true,
243     [NVME_WRITE_ATOMICITY]          = true,
244     [NVME_ASYNCHRONOUS_EVENT_CONF]  = true,
245     [NVME_TIMESTAMP]                = true,
246     [NVME_HOST_BEHAVIOR_SUPPORT]    = true,
247     [NVME_COMMAND_SET_PROFILE]      = true,
248     [NVME_FDP_MODE]                 = true,
249     [NVME_FDP_EVENTS]               = true,
250 };
251 
252 static const uint32_t nvme_feature_cap[NVME_FID_MAX] = {
253     [NVME_TEMPERATURE_THRESHOLD]    = NVME_FEAT_CAP_CHANGE,
254     [NVME_ERROR_RECOVERY]           = NVME_FEAT_CAP_CHANGE | NVME_FEAT_CAP_NS,
255     [NVME_VOLATILE_WRITE_CACHE]     = NVME_FEAT_CAP_CHANGE,
256     [NVME_NUMBER_OF_QUEUES]         = NVME_FEAT_CAP_CHANGE,
257     [NVME_ASYNCHRONOUS_EVENT_CONF]  = NVME_FEAT_CAP_CHANGE,
258     [NVME_TIMESTAMP]                = NVME_FEAT_CAP_CHANGE,
259     [NVME_HOST_BEHAVIOR_SUPPORT]    = NVME_FEAT_CAP_CHANGE,
260     [NVME_COMMAND_SET_PROFILE]      = NVME_FEAT_CAP_CHANGE,
261     [NVME_FDP_MODE]                 = NVME_FEAT_CAP_CHANGE,
262     [NVME_FDP_EVENTS]               = NVME_FEAT_CAP_CHANGE | NVME_FEAT_CAP_NS,
263 };
264 
265 static const uint32_t nvme_cse_acs[256] = {
266     [NVME_ADM_CMD_DELETE_SQ]        = NVME_CMD_EFF_CSUPP,
267     [NVME_ADM_CMD_CREATE_SQ]        = NVME_CMD_EFF_CSUPP,
268     [NVME_ADM_CMD_GET_LOG_PAGE]     = NVME_CMD_EFF_CSUPP,
269     [NVME_ADM_CMD_DELETE_CQ]        = NVME_CMD_EFF_CSUPP,
270     [NVME_ADM_CMD_CREATE_CQ]        = NVME_CMD_EFF_CSUPP,
271     [NVME_ADM_CMD_IDENTIFY]         = NVME_CMD_EFF_CSUPP,
272     [NVME_ADM_CMD_ABORT]            = NVME_CMD_EFF_CSUPP,
273     [NVME_ADM_CMD_SET_FEATURES]     = NVME_CMD_EFF_CSUPP,
274     [NVME_ADM_CMD_GET_FEATURES]     = NVME_CMD_EFF_CSUPP,
275     [NVME_ADM_CMD_ASYNC_EV_REQ]     = NVME_CMD_EFF_CSUPP,
276     [NVME_ADM_CMD_NS_ATTACHMENT]    = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_NIC,
277     [NVME_ADM_CMD_VIRT_MNGMT]       = NVME_CMD_EFF_CSUPP,
278     [NVME_ADM_CMD_DBBUF_CONFIG]     = NVME_CMD_EFF_CSUPP,
279     [NVME_ADM_CMD_FORMAT_NVM]       = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
280     [NVME_ADM_CMD_DIRECTIVE_RECV]   = NVME_CMD_EFF_CSUPP,
281     [NVME_ADM_CMD_DIRECTIVE_SEND]   = NVME_CMD_EFF_CSUPP,
282 };
283 
284 static const uint32_t nvme_cse_iocs_none[256];
285 
286 static const uint32_t nvme_cse_iocs_nvm[256] = {
287     [NVME_CMD_FLUSH]                = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
288     [NVME_CMD_WRITE_ZEROES]         = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
289     [NVME_CMD_WRITE]                = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
290     [NVME_CMD_READ]                 = NVME_CMD_EFF_CSUPP,
291     [NVME_CMD_DSM]                  = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
292     [NVME_CMD_VERIFY]               = NVME_CMD_EFF_CSUPP,
293     [NVME_CMD_COPY]                 = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
294     [NVME_CMD_COMPARE]              = NVME_CMD_EFF_CSUPP,
295     [NVME_CMD_IO_MGMT_RECV]         = NVME_CMD_EFF_CSUPP,
296     [NVME_CMD_IO_MGMT_SEND]         = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
297 };
298 
299 static const uint32_t nvme_cse_iocs_zoned[256] = {
300     [NVME_CMD_FLUSH]                = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
301     [NVME_CMD_WRITE_ZEROES]         = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
302     [NVME_CMD_WRITE]                = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
303     [NVME_CMD_READ]                 = NVME_CMD_EFF_CSUPP,
304     [NVME_CMD_DSM]                  = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
305     [NVME_CMD_VERIFY]               = NVME_CMD_EFF_CSUPP,
306     [NVME_CMD_COPY]                 = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
307     [NVME_CMD_COMPARE]              = NVME_CMD_EFF_CSUPP,
308     [NVME_CMD_ZONE_APPEND]          = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
309     [NVME_CMD_ZONE_MGMT_SEND]       = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
310     [NVME_CMD_ZONE_MGMT_RECV]       = NVME_CMD_EFF_CSUPP,
311 };
312 
313 static void nvme_process_sq(void *opaque);
314 static void nvme_ctrl_reset(NvmeCtrl *n, NvmeResetType rst);
315 static inline uint64_t nvme_get_timestamp(const NvmeCtrl *n);
316 
317 static uint16_t nvme_sqid(NvmeRequest *req)
318 {
319     return le16_to_cpu(req->sq->sqid);
320 }
321 
322 static inline uint16_t nvme_make_pid(NvmeNamespace *ns, uint16_t rg,
323                                      uint16_t ph)
324 {
325     uint16_t rgif = ns->endgrp->fdp.rgif;
326 
327     if (!rgif) {
328         return ph;
329     }
330 
331     return (rg << (16 - rgif)) | ph;
332 }
333 
334 static inline bool nvme_ph_valid(NvmeNamespace *ns, uint16_t ph)
335 {
336     return ph < ns->fdp.nphs;
337 }
338 
339 static inline bool nvme_rg_valid(NvmeEnduranceGroup *endgrp, uint16_t rg)
340 {
341     return rg < endgrp->fdp.nrg;
342 }
343 
344 static inline uint16_t nvme_pid2ph(NvmeNamespace *ns, uint16_t pid)
345 {
346     uint16_t rgif = ns->endgrp->fdp.rgif;
347 
348     if (!rgif) {
349         return pid;
350     }
351 
352     return pid & ((1 << (15 - rgif)) - 1);
353 }
354 
355 static inline uint16_t nvme_pid2rg(NvmeNamespace *ns, uint16_t pid)
356 {
357     uint16_t rgif = ns->endgrp->fdp.rgif;
358 
359     if (!rgif) {
360         return 0;
361     }
362 
363     return pid >> (16 - rgif);
364 }
365 
366 static inline bool nvme_parse_pid(NvmeNamespace *ns, uint16_t pid,
367                                   uint16_t *ph, uint16_t *rg)
368 {
369     *rg = nvme_pid2rg(ns, pid);
370     *ph = nvme_pid2ph(ns, pid);
371 
372     return nvme_ph_valid(ns, *ph) && nvme_rg_valid(ns->endgrp, *rg);
373 }
374 
375 static void nvme_assign_zone_state(NvmeNamespace *ns, NvmeZone *zone,
376                                    NvmeZoneState state)
377 {
378     if (QTAILQ_IN_USE(zone, entry)) {
379         switch (nvme_get_zone_state(zone)) {
380         case NVME_ZONE_STATE_EXPLICITLY_OPEN:
381             QTAILQ_REMOVE(&ns->exp_open_zones, zone, entry);
382             break;
383         case NVME_ZONE_STATE_IMPLICITLY_OPEN:
384             QTAILQ_REMOVE(&ns->imp_open_zones, zone, entry);
385             break;
386         case NVME_ZONE_STATE_CLOSED:
387             QTAILQ_REMOVE(&ns->closed_zones, zone, entry);
388             break;
389         case NVME_ZONE_STATE_FULL:
390             QTAILQ_REMOVE(&ns->full_zones, zone, entry);
391         default:
392             ;
393         }
394     }
395 
396     nvme_set_zone_state(zone, state);
397 
398     switch (state) {
399     case NVME_ZONE_STATE_EXPLICITLY_OPEN:
400         QTAILQ_INSERT_TAIL(&ns->exp_open_zones, zone, entry);
401         break;
402     case NVME_ZONE_STATE_IMPLICITLY_OPEN:
403         QTAILQ_INSERT_TAIL(&ns->imp_open_zones, zone, entry);
404         break;
405     case NVME_ZONE_STATE_CLOSED:
406         QTAILQ_INSERT_TAIL(&ns->closed_zones, zone, entry);
407         break;
408     case NVME_ZONE_STATE_FULL:
409         QTAILQ_INSERT_TAIL(&ns->full_zones, zone, entry);
410     case NVME_ZONE_STATE_READ_ONLY:
411         break;
412     default:
413         zone->d.za = 0;
414     }
415 }
416 
417 static uint16_t nvme_zns_check_resources(NvmeNamespace *ns, uint32_t act,
418                                          uint32_t opn, uint32_t zrwa)
419 {
420     if (ns->params.max_active_zones != 0 &&
421         ns->nr_active_zones + act > ns->params.max_active_zones) {
422         trace_pci_nvme_err_insuff_active_res(ns->params.max_active_zones);
423         return NVME_ZONE_TOO_MANY_ACTIVE | NVME_DNR;
424     }
425 
426     if (ns->params.max_open_zones != 0 &&
427         ns->nr_open_zones + opn > ns->params.max_open_zones) {
428         trace_pci_nvme_err_insuff_open_res(ns->params.max_open_zones);
429         return NVME_ZONE_TOO_MANY_OPEN | NVME_DNR;
430     }
431 
432     if (zrwa > ns->zns.numzrwa) {
433         return NVME_NOZRWA | NVME_DNR;
434     }
435 
436     return NVME_SUCCESS;
437 }
438 
439 /*
440  * Check if we can open a zone without exceeding open/active limits.
441  * AOR stands for "Active and Open Resources" (see TP 4053 section 2.5).
442  */
443 static uint16_t nvme_aor_check(NvmeNamespace *ns, uint32_t act, uint32_t opn)
444 {
445     return nvme_zns_check_resources(ns, act, opn, 0);
446 }
447 
448 static NvmeFdpEvent *nvme_fdp_alloc_event(NvmeCtrl *n, NvmeFdpEventBuffer *ebuf)
449 {
450     NvmeFdpEvent *ret = NULL;
451     bool is_full = ebuf->next == ebuf->start && ebuf->nelems;
452 
453     ret = &ebuf->events[ebuf->next++];
454     if (unlikely(ebuf->next == NVME_FDP_MAX_EVENTS)) {
455         ebuf->next = 0;
456     }
457     if (is_full) {
458         ebuf->start = ebuf->next;
459     } else {
460         ebuf->nelems++;
461     }
462 
463     memset(ret, 0, sizeof(NvmeFdpEvent));
464     ret->timestamp = nvme_get_timestamp(n);
465 
466     return ret;
467 }
468 
469 static inline int log_event(NvmeRuHandle *ruh, uint8_t event_type)
470 {
471     return (ruh->event_filter >> nvme_fdp_evf_shifts[event_type]) & 0x1;
472 }
473 
474 static bool nvme_update_ruh(NvmeCtrl *n, NvmeNamespace *ns, uint16_t pid)
475 {
476     NvmeEnduranceGroup *endgrp = ns->endgrp;
477     NvmeRuHandle *ruh;
478     NvmeReclaimUnit *ru;
479     NvmeFdpEvent *e = NULL;
480     uint16_t ph, rg, ruhid;
481 
482     if (!nvme_parse_pid(ns, pid, &ph, &rg)) {
483         return false;
484     }
485 
486     ruhid = ns->fdp.phs[ph];
487 
488     ruh = &endgrp->fdp.ruhs[ruhid];
489     ru = &ruh->rus[rg];
490 
491     if (ru->ruamw) {
492         if (log_event(ruh, FDP_EVT_RU_NOT_FULLY_WRITTEN)) {
493             e = nvme_fdp_alloc_event(n, &endgrp->fdp.host_events);
494             e->type = FDP_EVT_RU_NOT_FULLY_WRITTEN;
495             e->flags = FDPEF_PIV | FDPEF_NSIDV | FDPEF_LV;
496             e->pid = cpu_to_le16(pid);
497             e->nsid = cpu_to_le32(ns->params.nsid);
498             e->rgid = cpu_to_le16(rg);
499             e->ruhid = cpu_to_le16(ruhid);
500         }
501 
502         /* log (eventual) GC overhead of prematurely swapping the RU */
503         nvme_fdp_stat_inc(&endgrp->fdp.mbmw, nvme_l2b(ns, ru->ruamw));
504     }
505 
506     ru->ruamw = ruh->ruamw;
507 
508     return true;
509 }
510 
511 static bool nvme_addr_is_cmb(NvmeCtrl *n, hwaddr addr)
512 {
513     hwaddr hi, lo;
514 
515     if (!n->cmb.cmse) {
516         return false;
517     }
518 
519     lo = n->params.legacy_cmb ? n->cmb.mem.addr : n->cmb.cba;
520     hi = lo + int128_get64(n->cmb.mem.size);
521 
522     return addr >= lo && addr < hi;
523 }
524 
525 static inline void *nvme_addr_to_cmb(NvmeCtrl *n, hwaddr addr)
526 {
527     hwaddr base = n->params.legacy_cmb ? n->cmb.mem.addr : n->cmb.cba;
528     return &n->cmb.buf[addr - base];
529 }
530 
531 static bool nvme_addr_is_pmr(NvmeCtrl *n, hwaddr addr)
532 {
533     hwaddr hi;
534 
535     if (!n->pmr.cmse) {
536         return false;
537     }
538 
539     hi = n->pmr.cba + int128_get64(n->pmr.dev->mr.size);
540 
541     return addr >= n->pmr.cba && addr < hi;
542 }
543 
544 static inline void *nvme_addr_to_pmr(NvmeCtrl *n, hwaddr addr)
545 {
546     return memory_region_get_ram_ptr(&n->pmr.dev->mr) + (addr - n->pmr.cba);
547 }
548 
549 static inline bool nvme_addr_is_iomem(NvmeCtrl *n, hwaddr addr)
550 {
551     hwaddr hi, lo;
552 
553     /*
554      * The purpose of this check is to guard against invalid "local" access to
555      * the iomem (i.e. controller registers). Thus, we check against the range
556      * covered by the 'bar0' MemoryRegion since that is currently composed of
557      * two subregions (the NVMe "MBAR" and the MSI-X table/pba). Note, however,
558      * that if the device model is ever changed to allow the CMB to be located
559      * in BAR0 as well, then this must be changed.
560      */
561     lo = n->bar0.addr;
562     hi = lo + int128_get64(n->bar0.size);
563 
564     return addr >= lo && addr < hi;
565 }
566 
567 static int nvme_addr_read(NvmeCtrl *n, hwaddr addr, void *buf, int size)
568 {
569     hwaddr hi = addr + size - 1;
570     if (hi < addr) {
571         return 1;
572     }
573 
574     if (n->bar.cmbsz && nvme_addr_is_cmb(n, addr) && nvme_addr_is_cmb(n, hi)) {
575         memcpy(buf, nvme_addr_to_cmb(n, addr), size);
576         return 0;
577     }
578 
579     if (nvme_addr_is_pmr(n, addr) && nvme_addr_is_pmr(n, hi)) {
580         memcpy(buf, nvme_addr_to_pmr(n, addr), size);
581         return 0;
582     }
583 
584     return pci_dma_read(PCI_DEVICE(n), addr, buf, size);
585 }
586 
587 static int nvme_addr_write(NvmeCtrl *n, hwaddr addr, const void *buf, int size)
588 {
589     hwaddr hi = addr + size - 1;
590     if (hi < addr) {
591         return 1;
592     }
593 
594     if (n->bar.cmbsz && nvme_addr_is_cmb(n, addr) && nvme_addr_is_cmb(n, hi)) {
595         memcpy(nvme_addr_to_cmb(n, addr), buf, size);
596         return 0;
597     }
598 
599     if (nvme_addr_is_pmr(n, addr) && nvme_addr_is_pmr(n, hi)) {
600         memcpy(nvme_addr_to_pmr(n, addr), buf, size);
601         return 0;
602     }
603 
604     return pci_dma_write(PCI_DEVICE(n), addr, buf, size);
605 }
606 
607 static bool nvme_nsid_valid(NvmeCtrl *n, uint32_t nsid)
608 {
609     return nsid &&
610         (nsid == NVME_NSID_BROADCAST || nsid <= NVME_MAX_NAMESPACES);
611 }
612 
613 static int nvme_check_sqid(NvmeCtrl *n, uint16_t sqid)
614 {
615     return sqid < n->conf_ioqpairs + 1 && n->sq[sqid] != NULL ? 0 : -1;
616 }
617 
618 static int nvme_check_cqid(NvmeCtrl *n, uint16_t cqid)
619 {
620     return cqid < n->conf_ioqpairs + 1 && n->cq[cqid] != NULL ? 0 : -1;
621 }
622 
623 static void nvme_inc_cq_tail(NvmeCQueue *cq)
624 {
625     cq->tail++;
626     if (cq->tail >= cq->size) {
627         cq->tail = 0;
628         cq->phase = !cq->phase;
629     }
630 }
631 
632 static void nvme_inc_sq_head(NvmeSQueue *sq)
633 {
634     sq->head = (sq->head + 1) % sq->size;
635 }
636 
637 static uint8_t nvme_cq_full(NvmeCQueue *cq)
638 {
639     return (cq->tail + 1) % cq->size == cq->head;
640 }
641 
642 static uint8_t nvme_sq_empty(NvmeSQueue *sq)
643 {
644     return sq->head == sq->tail;
645 }
646 
647 static void nvme_irq_check(NvmeCtrl *n)
648 {
649     PCIDevice *pci = PCI_DEVICE(n);
650     uint32_t intms = ldl_le_p(&n->bar.intms);
651 
652     if (msix_enabled(pci)) {
653         return;
654     }
655     if (~intms & n->irq_status) {
656         pci_irq_assert(pci);
657     } else {
658         pci_irq_deassert(pci);
659     }
660 }
661 
662 static void nvme_irq_assert(NvmeCtrl *n, NvmeCQueue *cq)
663 {
664     PCIDevice *pci = PCI_DEVICE(n);
665 
666     if (cq->irq_enabled) {
667         if (msix_enabled(pci)) {
668             trace_pci_nvme_irq_msix(cq->vector);
669             msix_notify(pci, cq->vector);
670         } else {
671             trace_pci_nvme_irq_pin();
672             assert(cq->vector < 32);
673             n->irq_status |= 1 << cq->vector;
674             nvme_irq_check(n);
675         }
676     } else {
677         trace_pci_nvme_irq_masked();
678     }
679 }
680 
681 static void nvme_irq_deassert(NvmeCtrl *n, NvmeCQueue *cq)
682 {
683     if (cq->irq_enabled) {
684         if (msix_enabled(PCI_DEVICE(n))) {
685             return;
686         } else {
687             assert(cq->vector < 32);
688             if (!n->cq_pending) {
689                 n->irq_status &= ~(1 << cq->vector);
690             }
691             nvme_irq_check(n);
692         }
693     }
694 }
695 
696 static void nvme_req_clear(NvmeRequest *req)
697 {
698     req->ns = NULL;
699     req->opaque = NULL;
700     req->aiocb = NULL;
701     memset(&req->cqe, 0x0, sizeof(req->cqe));
702     req->status = NVME_SUCCESS;
703 }
704 
705 static inline void nvme_sg_init(NvmeCtrl *n, NvmeSg *sg, bool dma)
706 {
707     if (dma) {
708         pci_dma_sglist_init(&sg->qsg, PCI_DEVICE(n), 0);
709         sg->flags = NVME_SG_DMA;
710     } else {
711         qemu_iovec_init(&sg->iov, 0);
712     }
713 
714     sg->flags |= NVME_SG_ALLOC;
715 }
716 
717 static inline void nvme_sg_unmap(NvmeSg *sg)
718 {
719     if (!(sg->flags & NVME_SG_ALLOC)) {
720         return;
721     }
722 
723     if (sg->flags & NVME_SG_DMA) {
724         qemu_sglist_destroy(&sg->qsg);
725     } else {
726         qemu_iovec_destroy(&sg->iov);
727     }
728 
729     memset(sg, 0x0, sizeof(*sg));
730 }
731 
732 /*
733  * When metadata is transferred as extended LBAs, the DPTR mapped into `sg`
734  * holds both data and metadata. This function splits the data and metadata
735  * into two separate QSG/IOVs.
736  */
737 static void nvme_sg_split(NvmeSg *sg, NvmeNamespace *ns, NvmeSg *data,
738                           NvmeSg *mdata)
739 {
740     NvmeSg *dst = data;
741     uint32_t trans_len, count = ns->lbasz;
742     uint64_t offset = 0;
743     bool dma = sg->flags & NVME_SG_DMA;
744     size_t sge_len;
745     size_t sg_len = dma ? sg->qsg.size : sg->iov.size;
746     int sg_idx = 0;
747 
748     assert(sg->flags & NVME_SG_ALLOC);
749 
750     while (sg_len) {
751         sge_len = dma ? sg->qsg.sg[sg_idx].len : sg->iov.iov[sg_idx].iov_len;
752 
753         trans_len = MIN(sg_len, count);
754         trans_len = MIN(trans_len, sge_len - offset);
755 
756         if (dst) {
757             if (dma) {
758                 qemu_sglist_add(&dst->qsg, sg->qsg.sg[sg_idx].base + offset,
759                                 trans_len);
760             } else {
761                 qemu_iovec_add(&dst->iov,
762                                sg->iov.iov[sg_idx].iov_base + offset,
763                                trans_len);
764             }
765         }
766 
767         sg_len -= trans_len;
768         count -= trans_len;
769         offset += trans_len;
770 
771         if (count == 0) {
772             dst = (dst == data) ? mdata : data;
773             count = (dst == data) ? ns->lbasz : ns->lbaf.ms;
774         }
775 
776         if (sge_len == offset) {
777             offset = 0;
778             sg_idx++;
779         }
780     }
781 }
782 
783 static uint16_t nvme_map_addr_cmb(NvmeCtrl *n, QEMUIOVector *iov, hwaddr addr,
784                                   size_t len)
785 {
786     if (!len) {
787         return NVME_SUCCESS;
788     }
789 
790     trace_pci_nvme_map_addr_cmb(addr, len);
791 
792     if (!nvme_addr_is_cmb(n, addr) || !nvme_addr_is_cmb(n, addr + len - 1)) {
793         return NVME_DATA_TRAS_ERROR;
794     }
795 
796     qemu_iovec_add(iov, nvme_addr_to_cmb(n, addr), len);
797 
798     return NVME_SUCCESS;
799 }
800 
801 static uint16_t nvme_map_addr_pmr(NvmeCtrl *n, QEMUIOVector *iov, hwaddr addr,
802                                   size_t len)
803 {
804     if (!len) {
805         return NVME_SUCCESS;
806     }
807 
808     if (!nvme_addr_is_pmr(n, addr) || !nvme_addr_is_pmr(n, addr + len - 1)) {
809         return NVME_DATA_TRAS_ERROR;
810     }
811 
812     qemu_iovec_add(iov, nvme_addr_to_pmr(n, addr), len);
813 
814     return NVME_SUCCESS;
815 }
816 
817 static uint16_t nvme_map_addr(NvmeCtrl *n, NvmeSg *sg, hwaddr addr, size_t len)
818 {
819     bool cmb = false, pmr = false;
820 
821     if (!len) {
822         return NVME_SUCCESS;
823     }
824 
825     trace_pci_nvme_map_addr(addr, len);
826 
827     if (nvme_addr_is_iomem(n, addr)) {
828         return NVME_DATA_TRAS_ERROR;
829     }
830 
831     if (nvme_addr_is_cmb(n, addr)) {
832         cmb = true;
833     } else if (nvme_addr_is_pmr(n, addr)) {
834         pmr = true;
835     }
836 
837     if (cmb || pmr) {
838         if (sg->flags & NVME_SG_DMA) {
839             return NVME_INVALID_USE_OF_CMB | NVME_DNR;
840         }
841 
842         if (sg->iov.niov + 1 > IOV_MAX) {
843             goto max_mappings_exceeded;
844         }
845 
846         if (cmb) {
847             return nvme_map_addr_cmb(n, &sg->iov, addr, len);
848         } else {
849             return nvme_map_addr_pmr(n, &sg->iov, addr, len);
850         }
851     }
852 
853     if (!(sg->flags & NVME_SG_DMA)) {
854         return NVME_INVALID_USE_OF_CMB | NVME_DNR;
855     }
856 
857     if (sg->qsg.nsg + 1 > IOV_MAX) {
858         goto max_mappings_exceeded;
859     }
860 
861     qemu_sglist_add(&sg->qsg, addr, len);
862 
863     return NVME_SUCCESS;
864 
865 max_mappings_exceeded:
866     NVME_GUEST_ERR(pci_nvme_ub_too_many_mappings,
867                    "number of mappings exceed 1024");
868     return NVME_INTERNAL_DEV_ERROR | NVME_DNR;
869 }
870 
871 static inline bool nvme_addr_is_dma(NvmeCtrl *n, hwaddr addr)
872 {
873     return !(nvme_addr_is_cmb(n, addr) || nvme_addr_is_pmr(n, addr));
874 }
875 
876 static uint16_t nvme_map_prp(NvmeCtrl *n, NvmeSg *sg, uint64_t prp1,
877                              uint64_t prp2, uint32_t len)
878 {
879     hwaddr trans_len = n->page_size - (prp1 % n->page_size);
880     trans_len = MIN(len, trans_len);
881     int num_prps = (len >> n->page_bits) + 1;
882     uint16_t status;
883     int ret;
884 
885     trace_pci_nvme_map_prp(trans_len, len, prp1, prp2, num_prps);
886 
887     nvme_sg_init(n, sg, nvme_addr_is_dma(n, prp1));
888 
889     status = nvme_map_addr(n, sg, prp1, trans_len);
890     if (status) {
891         goto unmap;
892     }
893 
894     len -= trans_len;
895     if (len) {
896         if (len > n->page_size) {
897             g_autofree uint64_t *prp_list = g_new(uint64_t, n->max_prp_ents);
898             uint32_t nents, prp_trans;
899             int i = 0;
900 
901             /*
902              * The first PRP list entry, pointed to by PRP2 may contain offset.
903              * Hence, we need to calculate the number of entries in based on
904              * that offset.
905              */
906             nents = (n->page_size - (prp2 & (n->page_size - 1))) >> 3;
907             prp_trans = MIN(n->max_prp_ents, nents) * sizeof(uint64_t);
908             ret = nvme_addr_read(n, prp2, (void *)prp_list, prp_trans);
909             if (ret) {
910                 trace_pci_nvme_err_addr_read(prp2);
911                 status = NVME_DATA_TRAS_ERROR;
912                 goto unmap;
913             }
914             while (len != 0) {
915                 uint64_t prp_ent = le64_to_cpu(prp_list[i]);
916 
917                 if (i == nents - 1 && len > n->page_size) {
918                     if (unlikely(prp_ent & (n->page_size - 1))) {
919                         trace_pci_nvme_err_invalid_prplist_ent(prp_ent);
920                         status = NVME_INVALID_PRP_OFFSET | NVME_DNR;
921                         goto unmap;
922                     }
923 
924                     i = 0;
925                     nents = (len + n->page_size - 1) >> n->page_bits;
926                     nents = MIN(nents, n->max_prp_ents);
927                     prp_trans = nents * sizeof(uint64_t);
928                     ret = nvme_addr_read(n, prp_ent, (void *)prp_list,
929                                          prp_trans);
930                     if (ret) {
931                         trace_pci_nvme_err_addr_read(prp_ent);
932                         status = NVME_DATA_TRAS_ERROR;
933                         goto unmap;
934                     }
935                     prp_ent = le64_to_cpu(prp_list[i]);
936                 }
937 
938                 if (unlikely(prp_ent & (n->page_size - 1))) {
939                     trace_pci_nvme_err_invalid_prplist_ent(prp_ent);
940                     status = NVME_INVALID_PRP_OFFSET | NVME_DNR;
941                     goto unmap;
942                 }
943 
944                 trans_len = MIN(len, n->page_size);
945                 status = nvme_map_addr(n, sg, prp_ent, trans_len);
946                 if (status) {
947                     goto unmap;
948                 }
949 
950                 len -= trans_len;
951                 i++;
952             }
953         } else {
954             if (unlikely(prp2 & (n->page_size - 1))) {
955                 trace_pci_nvme_err_invalid_prp2_align(prp2);
956                 status = NVME_INVALID_PRP_OFFSET | NVME_DNR;
957                 goto unmap;
958             }
959             status = nvme_map_addr(n, sg, prp2, len);
960             if (status) {
961                 goto unmap;
962             }
963         }
964     }
965 
966     return NVME_SUCCESS;
967 
968 unmap:
969     nvme_sg_unmap(sg);
970     return status;
971 }
972 
973 /*
974  * Map 'nsgld' data descriptors from 'segment'. The function will subtract the
975  * number of bytes mapped in len.
976  */
977 static uint16_t nvme_map_sgl_data(NvmeCtrl *n, NvmeSg *sg,
978                                   NvmeSglDescriptor *segment, uint64_t nsgld,
979                                   size_t *len, NvmeCmd *cmd)
980 {
981     dma_addr_t addr, trans_len;
982     uint32_t dlen;
983     uint16_t status;
984 
985     for (int i = 0; i < nsgld; i++) {
986         uint8_t type = NVME_SGL_TYPE(segment[i].type);
987 
988         switch (type) {
989         case NVME_SGL_DESCR_TYPE_DATA_BLOCK:
990             break;
991         case NVME_SGL_DESCR_TYPE_SEGMENT:
992         case NVME_SGL_DESCR_TYPE_LAST_SEGMENT:
993             return NVME_INVALID_NUM_SGL_DESCRS | NVME_DNR;
994         default:
995             return NVME_SGL_DESCR_TYPE_INVALID | NVME_DNR;
996         }
997 
998         dlen = le32_to_cpu(segment[i].len);
999 
1000         if (!dlen) {
1001             continue;
1002         }
1003 
1004         if (*len == 0) {
1005             /*
1006              * All data has been mapped, but the SGL contains additional
1007              * segments and/or descriptors. The controller might accept
1008              * ignoring the rest of the SGL.
1009              */
1010             uint32_t sgls = le32_to_cpu(n->id_ctrl.sgls);
1011             if (sgls & NVME_CTRL_SGLS_EXCESS_LENGTH) {
1012                 break;
1013             }
1014 
1015             trace_pci_nvme_err_invalid_sgl_excess_length(dlen);
1016             return NVME_DATA_SGL_LEN_INVALID | NVME_DNR;
1017         }
1018 
1019         trans_len = MIN(*len, dlen);
1020 
1021         addr = le64_to_cpu(segment[i].addr);
1022 
1023         if (UINT64_MAX - addr < dlen) {
1024             return NVME_DATA_SGL_LEN_INVALID | NVME_DNR;
1025         }
1026 
1027         status = nvme_map_addr(n, sg, addr, trans_len);
1028         if (status) {
1029             return status;
1030         }
1031 
1032         *len -= trans_len;
1033     }
1034 
1035     return NVME_SUCCESS;
1036 }
1037 
1038 static uint16_t nvme_map_sgl(NvmeCtrl *n, NvmeSg *sg, NvmeSglDescriptor sgl,
1039                              size_t len, NvmeCmd *cmd)
1040 {
1041     /*
1042      * Read the segment in chunks of 256 descriptors (one 4k page) to avoid
1043      * dynamically allocating a potentially huge SGL. The spec allows the SGL
1044      * to be larger (as in number of bytes required to describe the SGL
1045      * descriptors and segment chain) than the command transfer size, so it is
1046      * not bounded by MDTS.
1047      */
1048 #define SEG_CHUNK_SIZE 256
1049 
1050     NvmeSglDescriptor segment[SEG_CHUNK_SIZE], *sgld, *last_sgld;
1051     uint64_t nsgld;
1052     uint32_t seg_len;
1053     uint16_t status;
1054     hwaddr addr;
1055     int ret;
1056 
1057     sgld = &sgl;
1058     addr = le64_to_cpu(sgl.addr);
1059 
1060     trace_pci_nvme_map_sgl(NVME_SGL_TYPE(sgl.type), len);
1061 
1062     nvme_sg_init(n, sg, nvme_addr_is_dma(n, addr));
1063 
1064     /*
1065      * If the entire transfer can be described with a single data block it can
1066      * be mapped directly.
1067      */
1068     if (NVME_SGL_TYPE(sgl.type) == NVME_SGL_DESCR_TYPE_DATA_BLOCK) {
1069         status = nvme_map_sgl_data(n, sg, sgld, 1, &len, cmd);
1070         if (status) {
1071             goto unmap;
1072         }
1073 
1074         goto out;
1075     }
1076 
1077     for (;;) {
1078         switch (NVME_SGL_TYPE(sgld->type)) {
1079         case NVME_SGL_DESCR_TYPE_SEGMENT:
1080         case NVME_SGL_DESCR_TYPE_LAST_SEGMENT:
1081             break;
1082         default:
1083             return NVME_INVALID_SGL_SEG_DESCR | NVME_DNR;
1084         }
1085 
1086         seg_len = le32_to_cpu(sgld->len);
1087 
1088         /* check the length of the (Last) Segment descriptor */
1089         if (!seg_len || seg_len & 0xf) {
1090             return NVME_INVALID_SGL_SEG_DESCR | NVME_DNR;
1091         }
1092 
1093         if (UINT64_MAX - addr < seg_len) {
1094             return NVME_DATA_SGL_LEN_INVALID | NVME_DNR;
1095         }
1096 
1097         nsgld = seg_len / sizeof(NvmeSglDescriptor);
1098 
1099         while (nsgld > SEG_CHUNK_SIZE) {
1100             if (nvme_addr_read(n, addr, segment, sizeof(segment))) {
1101                 trace_pci_nvme_err_addr_read(addr);
1102                 status = NVME_DATA_TRAS_ERROR;
1103                 goto unmap;
1104             }
1105 
1106             status = nvme_map_sgl_data(n, sg, segment, SEG_CHUNK_SIZE,
1107                                        &len, cmd);
1108             if (status) {
1109                 goto unmap;
1110             }
1111 
1112             nsgld -= SEG_CHUNK_SIZE;
1113             addr += SEG_CHUNK_SIZE * sizeof(NvmeSglDescriptor);
1114         }
1115 
1116         ret = nvme_addr_read(n, addr, segment, nsgld *
1117                              sizeof(NvmeSglDescriptor));
1118         if (ret) {
1119             trace_pci_nvme_err_addr_read(addr);
1120             status = NVME_DATA_TRAS_ERROR;
1121             goto unmap;
1122         }
1123 
1124         last_sgld = &segment[nsgld - 1];
1125 
1126         /*
1127          * If the segment ends with a Data Block, then we are done.
1128          */
1129         if (NVME_SGL_TYPE(last_sgld->type) == NVME_SGL_DESCR_TYPE_DATA_BLOCK) {
1130             status = nvme_map_sgl_data(n, sg, segment, nsgld, &len, cmd);
1131             if (status) {
1132                 goto unmap;
1133             }
1134 
1135             goto out;
1136         }
1137 
1138         /*
1139          * If the last descriptor was not a Data Block, then the current
1140          * segment must not be a Last Segment.
1141          */
1142         if (NVME_SGL_TYPE(sgld->type) == NVME_SGL_DESCR_TYPE_LAST_SEGMENT) {
1143             status = NVME_INVALID_SGL_SEG_DESCR | NVME_DNR;
1144             goto unmap;
1145         }
1146 
1147         sgld = last_sgld;
1148         addr = le64_to_cpu(sgld->addr);
1149 
1150         /*
1151          * Do not map the last descriptor; it will be a Segment or Last Segment
1152          * descriptor and is handled by the next iteration.
1153          */
1154         status = nvme_map_sgl_data(n, sg, segment, nsgld - 1, &len, cmd);
1155         if (status) {
1156             goto unmap;
1157         }
1158     }
1159 
1160 out:
1161     /* if there is any residual left in len, the SGL was too short */
1162     if (len) {
1163         status = NVME_DATA_SGL_LEN_INVALID | NVME_DNR;
1164         goto unmap;
1165     }
1166 
1167     return NVME_SUCCESS;
1168 
1169 unmap:
1170     nvme_sg_unmap(sg);
1171     return status;
1172 }
1173 
1174 uint16_t nvme_map_dptr(NvmeCtrl *n, NvmeSg *sg, size_t len,
1175                        NvmeCmd *cmd)
1176 {
1177     uint64_t prp1, prp2;
1178 
1179     switch (NVME_CMD_FLAGS_PSDT(cmd->flags)) {
1180     case NVME_PSDT_PRP:
1181         prp1 = le64_to_cpu(cmd->dptr.prp1);
1182         prp2 = le64_to_cpu(cmd->dptr.prp2);
1183 
1184         return nvme_map_prp(n, sg, prp1, prp2, len);
1185     case NVME_PSDT_SGL_MPTR_CONTIGUOUS:
1186     case NVME_PSDT_SGL_MPTR_SGL:
1187         return nvme_map_sgl(n, sg, cmd->dptr.sgl, len, cmd);
1188     default:
1189         return NVME_INVALID_FIELD;
1190     }
1191 }
1192 
1193 static uint16_t nvme_map_mptr(NvmeCtrl *n, NvmeSg *sg, size_t len,
1194                               NvmeCmd *cmd)
1195 {
1196     int psdt = NVME_CMD_FLAGS_PSDT(cmd->flags);
1197     hwaddr mptr = le64_to_cpu(cmd->mptr);
1198     uint16_t status;
1199 
1200     if (psdt == NVME_PSDT_SGL_MPTR_SGL) {
1201         NvmeSglDescriptor sgl;
1202 
1203         if (nvme_addr_read(n, mptr, &sgl, sizeof(sgl))) {
1204             return NVME_DATA_TRAS_ERROR;
1205         }
1206 
1207         status = nvme_map_sgl(n, sg, sgl, len, cmd);
1208         if (status && (status & 0x7ff) == NVME_DATA_SGL_LEN_INVALID) {
1209             status = NVME_MD_SGL_LEN_INVALID | NVME_DNR;
1210         }
1211 
1212         return status;
1213     }
1214 
1215     nvme_sg_init(n, sg, nvme_addr_is_dma(n, mptr));
1216     status = nvme_map_addr(n, sg, mptr, len);
1217     if (status) {
1218         nvme_sg_unmap(sg);
1219     }
1220 
1221     return status;
1222 }
1223 
1224 static uint16_t nvme_map_data(NvmeCtrl *n, uint32_t nlb, NvmeRequest *req)
1225 {
1226     NvmeNamespace *ns = req->ns;
1227     NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
1228     bool pi = !!NVME_ID_NS_DPS_TYPE(ns->id_ns.dps);
1229     bool pract = !!(le16_to_cpu(rw->control) & NVME_RW_PRINFO_PRACT);
1230     size_t len = nvme_l2b(ns, nlb);
1231     uint16_t status;
1232 
1233     if (nvme_ns_ext(ns) &&
1234         !(pi && pract && ns->lbaf.ms == nvme_pi_tuple_size(ns))) {
1235         NvmeSg sg;
1236 
1237         len += nvme_m2b(ns, nlb);
1238 
1239         status = nvme_map_dptr(n, &sg, len, &req->cmd);
1240         if (status) {
1241             return status;
1242         }
1243 
1244         nvme_sg_init(n, &req->sg, sg.flags & NVME_SG_DMA);
1245         nvme_sg_split(&sg, ns, &req->sg, NULL);
1246         nvme_sg_unmap(&sg);
1247 
1248         return NVME_SUCCESS;
1249     }
1250 
1251     return nvme_map_dptr(n, &req->sg, len, &req->cmd);
1252 }
1253 
1254 static uint16_t nvme_map_mdata(NvmeCtrl *n, uint32_t nlb, NvmeRequest *req)
1255 {
1256     NvmeNamespace *ns = req->ns;
1257     size_t len = nvme_m2b(ns, nlb);
1258     uint16_t status;
1259 
1260     if (nvme_ns_ext(ns)) {
1261         NvmeSg sg;
1262 
1263         len += nvme_l2b(ns, nlb);
1264 
1265         status = nvme_map_dptr(n, &sg, len, &req->cmd);
1266         if (status) {
1267             return status;
1268         }
1269 
1270         nvme_sg_init(n, &req->sg, sg.flags & NVME_SG_DMA);
1271         nvme_sg_split(&sg, ns, NULL, &req->sg);
1272         nvme_sg_unmap(&sg);
1273 
1274         return NVME_SUCCESS;
1275     }
1276 
1277     return nvme_map_mptr(n, &req->sg, len, &req->cmd);
1278 }
1279 
1280 static uint16_t nvme_tx_interleaved(NvmeCtrl *n, NvmeSg *sg, uint8_t *ptr,
1281                                     uint32_t len, uint32_t bytes,
1282                                     int32_t skip_bytes, int64_t offset,
1283                                     NvmeTxDirection dir)
1284 {
1285     hwaddr addr;
1286     uint32_t trans_len, count = bytes;
1287     bool dma = sg->flags & NVME_SG_DMA;
1288     int64_t sge_len;
1289     int sg_idx = 0;
1290     int ret;
1291 
1292     assert(sg->flags & NVME_SG_ALLOC);
1293 
1294     while (len) {
1295         sge_len = dma ? sg->qsg.sg[sg_idx].len : sg->iov.iov[sg_idx].iov_len;
1296 
1297         if (sge_len - offset < 0) {
1298             offset -= sge_len;
1299             sg_idx++;
1300             continue;
1301         }
1302 
1303         if (sge_len == offset) {
1304             offset = 0;
1305             sg_idx++;
1306             continue;
1307         }
1308 
1309         trans_len = MIN(len, count);
1310         trans_len = MIN(trans_len, sge_len - offset);
1311 
1312         if (dma) {
1313             addr = sg->qsg.sg[sg_idx].base + offset;
1314         } else {
1315             addr = (hwaddr)(uintptr_t)sg->iov.iov[sg_idx].iov_base + offset;
1316         }
1317 
1318         if (dir == NVME_TX_DIRECTION_TO_DEVICE) {
1319             ret = nvme_addr_read(n, addr, ptr, trans_len);
1320         } else {
1321             ret = nvme_addr_write(n, addr, ptr, trans_len);
1322         }
1323 
1324         if (ret) {
1325             return NVME_DATA_TRAS_ERROR;
1326         }
1327 
1328         ptr += trans_len;
1329         len -= trans_len;
1330         count -= trans_len;
1331         offset += trans_len;
1332 
1333         if (count == 0) {
1334             count = bytes;
1335             offset += skip_bytes;
1336         }
1337     }
1338 
1339     return NVME_SUCCESS;
1340 }
1341 
1342 static uint16_t nvme_tx(NvmeCtrl *n, NvmeSg *sg, void *ptr, uint32_t len,
1343                         NvmeTxDirection dir)
1344 {
1345     assert(sg->flags & NVME_SG_ALLOC);
1346 
1347     if (sg->flags & NVME_SG_DMA) {
1348         const MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED;
1349         dma_addr_t residual;
1350 
1351         if (dir == NVME_TX_DIRECTION_TO_DEVICE) {
1352             dma_buf_write(ptr, len, &residual, &sg->qsg, attrs);
1353         } else {
1354             dma_buf_read(ptr, len, &residual, &sg->qsg, attrs);
1355         }
1356 
1357         if (unlikely(residual)) {
1358             trace_pci_nvme_err_invalid_dma();
1359             return NVME_INVALID_FIELD | NVME_DNR;
1360         }
1361     } else {
1362         size_t bytes;
1363 
1364         if (dir == NVME_TX_DIRECTION_TO_DEVICE) {
1365             bytes = qemu_iovec_to_buf(&sg->iov, 0, ptr, len);
1366         } else {
1367             bytes = qemu_iovec_from_buf(&sg->iov, 0, ptr, len);
1368         }
1369 
1370         if (unlikely(bytes != len)) {
1371             trace_pci_nvme_err_invalid_dma();
1372             return NVME_INVALID_FIELD | NVME_DNR;
1373         }
1374     }
1375 
1376     return NVME_SUCCESS;
1377 }
1378 
1379 static inline uint16_t nvme_c2h(NvmeCtrl *n, void *ptr, uint32_t len,
1380                                 NvmeRequest *req)
1381 {
1382     uint16_t status;
1383 
1384     status = nvme_map_dptr(n, &req->sg, len, &req->cmd);
1385     if (status) {
1386         return status;
1387     }
1388 
1389     return nvme_tx(n, &req->sg, ptr, len, NVME_TX_DIRECTION_FROM_DEVICE);
1390 }
1391 
1392 static inline uint16_t nvme_h2c(NvmeCtrl *n, void *ptr, uint32_t len,
1393                                 NvmeRequest *req)
1394 {
1395     uint16_t status;
1396 
1397     status = nvme_map_dptr(n, &req->sg, len, &req->cmd);
1398     if (status) {
1399         return status;
1400     }
1401 
1402     return nvme_tx(n, &req->sg, ptr, len, NVME_TX_DIRECTION_TO_DEVICE);
1403 }
1404 
1405 uint16_t nvme_bounce_data(NvmeCtrl *n, void *ptr, uint32_t len,
1406                           NvmeTxDirection dir, NvmeRequest *req)
1407 {
1408     NvmeNamespace *ns = req->ns;
1409     NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
1410     bool pi = !!NVME_ID_NS_DPS_TYPE(ns->id_ns.dps);
1411     bool pract = !!(le16_to_cpu(rw->control) & NVME_RW_PRINFO_PRACT);
1412 
1413     if (nvme_ns_ext(ns) &&
1414         !(pi && pract && ns->lbaf.ms == nvme_pi_tuple_size(ns))) {
1415         return nvme_tx_interleaved(n, &req->sg, ptr, len, ns->lbasz,
1416                                    ns->lbaf.ms, 0, dir);
1417     }
1418 
1419     return nvme_tx(n, &req->sg, ptr, len, dir);
1420 }
1421 
1422 uint16_t nvme_bounce_mdata(NvmeCtrl *n, void *ptr, uint32_t len,
1423                            NvmeTxDirection dir, NvmeRequest *req)
1424 {
1425     NvmeNamespace *ns = req->ns;
1426     uint16_t status;
1427 
1428     if (nvme_ns_ext(ns)) {
1429         return nvme_tx_interleaved(n, &req->sg, ptr, len, ns->lbaf.ms,
1430                                    ns->lbasz, ns->lbasz, dir);
1431     }
1432 
1433     nvme_sg_unmap(&req->sg);
1434 
1435     status = nvme_map_mptr(n, &req->sg, len, &req->cmd);
1436     if (status) {
1437         return status;
1438     }
1439 
1440     return nvme_tx(n, &req->sg, ptr, len, dir);
1441 }
1442 
1443 static inline void nvme_blk_read(BlockBackend *blk, int64_t offset,
1444                                  uint32_t align, BlockCompletionFunc *cb,
1445                                  NvmeRequest *req)
1446 {
1447     assert(req->sg.flags & NVME_SG_ALLOC);
1448 
1449     if (req->sg.flags & NVME_SG_DMA) {
1450         req->aiocb = dma_blk_read(blk, &req->sg.qsg, offset, align, cb, req);
1451     } else {
1452         req->aiocb = blk_aio_preadv(blk, offset, &req->sg.iov, 0, cb, req);
1453     }
1454 }
1455 
1456 static inline void nvme_blk_write(BlockBackend *blk, int64_t offset,
1457                                   uint32_t align, BlockCompletionFunc *cb,
1458                                   NvmeRequest *req)
1459 {
1460     assert(req->sg.flags & NVME_SG_ALLOC);
1461 
1462     if (req->sg.flags & NVME_SG_DMA) {
1463         req->aiocb = dma_blk_write(blk, &req->sg.qsg, offset, align, cb, req);
1464     } else {
1465         req->aiocb = blk_aio_pwritev(blk, offset, &req->sg.iov, 0, cb, req);
1466     }
1467 }
1468 
1469 static void nvme_update_cq_eventidx(const NvmeCQueue *cq)
1470 {
1471     trace_pci_nvme_update_cq_eventidx(cq->cqid, cq->head);
1472 
1473     stl_le_pci_dma(PCI_DEVICE(cq->ctrl), cq->ei_addr, cq->head,
1474                    MEMTXATTRS_UNSPECIFIED);
1475 }
1476 
1477 static void nvme_update_cq_head(NvmeCQueue *cq)
1478 {
1479     ldl_le_pci_dma(PCI_DEVICE(cq->ctrl), cq->db_addr, &cq->head,
1480                    MEMTXATTRS_UNSPECIFIED);
1481 
1482     trace_pci_nvme_update_cq_head(cq->cqid, cq->head);
1483 }
1484 
1485 static void nvme_post_cqes(void *opaque)
1486 {
1487     NvmeCQueue *cq = opaque;
1488     NvmeCtrl *n = cq->ctrl;
1489     NvmeRequest *req, *next;
1490     bool pending = cq->head != cq->tail;
1491     int ret;
1492 
1493     QTAILQ_FOREACH_SAFE(req, &cq->req_list, entry, next) {
1494         NvmeSQueue *sq;
1495         hwaddr addr;
1496 
1497         if (n->dbbuf_enabled) {
1498             nvme_update_cq_eventidx(cq);
1499             nvme_update_cq_head(cq);
1500         }
1501 
1502         if (nvme_cq_full(cq)) {
1503             break;
1504         }
1505 
1506         sq = req->sq;
1507         req->cqe.status = cpu_to_le16((req->status << 1) | cq->phase);
1508         req->cqe.sq_id = cpu_to_le16(sq->sqid);
1509         req->cqe.sq_head = cpu_to_le16(sq->head);
1510         addr = cq->dma_addr + (cq->tail << NVME_CQES);
1511         ret = pci_dma_write(PCI_DEVICE(n), addr, (void *)&req->cqe,
1512                             sizeof(req->cqe));
1513         if (ret) {
1514             trace_pci_nvme_err_addr_write(addr);
1515             trace_pci_nvme_err_cfs();
1516             stl_le_p(&n->bar.csts, NVME_CSTS_FAILED);
1517             break;
1518         }
1519         QTAILQ_REMOVE(&cq->req_list, req, entry);
1520         nvme_inc_cq_tail(cq);
1521         nvme_sg_unmap(&req->sg);
1522         QTAILQ_INSERT_TAIL(&sq->req_list, req, entry);
1523     }
1524     if (cq->tail != cq->head) {
1525         if (cq->irq_enabled && !pending) {
1526             n->cq_pending++;
1527         }
1528 
1529         nvme_irq_assert(n, cq);
1530     }
1531 }
1532 
1533 static void nvme_enqueue_req_completion(NvmeCQueue *cq, NvmeRequest *req)
1534 {
1535     assert(cq->cqid == req->sq->cqid);
1536     trace_pci_nvme_enqueue_req_completion(nvme_cid(req), cq->cqid,
1537                                           le32_to_cpu(req->cqe.result),
1538                                           le32_to_cpu(req->cqe.dw1),
1539                                           req->status);
1540 
1541     if (req->status) {
1542         trace_pci_nvme_err_req_status(nvme_cid(req), nvme_nsid(req->ns),
1543                                       req->status, req->cmd.opcode);
1544     }
1545 
1546     QTAILQ_REMOVE(&req->sq->out_req_list, req, entry);
1547     QTAILQ_INSERT_TAIL(&cq->req_list, req, entry);
1548 
1549     qemu_bh_schedule(cq->bh);
1550 }
1551 
1552 static void nvme_process_aers(void *opaque)
1553 {
1554     NvmeCtrl *n = opaque;
1555     NvmeAsyncEvent *event, *next;
1556 
1557     trace_pci_nvme_process_aers(n->aer_queued);
1558 
1559     QTAILQ_FOREACH_SAFE(event, &n->aer_queue, entry, next) {
1560         NvmeRequest *req;
1561         NvmeAerResult *result;
1562 
1563         /* can't post cqe if there is nothing to complete */
1564         if (!n->outstanding_aers) {
1565             trace_pci_nvme_no_outstanding_aers();
1566             break;
1567         }
1568 
1569         /* ignore if masked (cqe posted, but event not cleared) */
1570         if (n->aer_mask & (1 << event->result.event_type)) {
1571             trace_pci_nvme_aer_masked(event->result.event_type, n->aer_mask);
1572             continue;
1573         }
1574 
1575         QTAILQ_REMOVE(&n->aer_queue, event, entry);
1576         n->aer_queued--;
1577 
1578         n->aer_mask |= 1 << event->result.event_type;
1579         n->outstanding_aers--;
1580 
1581         req = n->aer_reqs[n->outstanding_aers];
1582 
1583         result = (NvmeAerResult *) &req->cqe.result;
1584         result->event_type = event->result.event_type;
1585         result->event_info = event->result.event_info;
1586         result->log_page = event->result.log_page;
1587         g_free(event);
1588 
1589         trace_pci_nvme_aer_post_cqe(result->event_type, result->event_info,
1590                                     result->log_page);
1591 
1592         nvme_enqueue_req_completion(&n->admin_cq, req);
1593     }
1594 }
1595 
1596 static void nvme_enqueue_event(NvmeCtrl *n, uint8_t event_type,
1597                                uint8_t event_info, uint8_t log_page)
1598 {
1599     NvmeAsyncEvent *event;
1600 
1601     trace_pci_nvme_enqueue_event(event_type, event_info, log_page);
1602 
1603     if (n->aer_queued == n->params.aer_max_queued) {
1604         trace_pci_nvme_enqueue_event_noqueue(n->aer_queued);
1605         return;
1606     }
1607 
1608     event = g_new(NvmeAsyncEvent, 1);
1609     event->result = (NvmeAerResult) {
1610         .event_type = event_type,
1611         .event_info = event_info,
1612         .log_page   = log_page,
1613     };
1614 
1615     QTAILQ_INSERT_TAIL(&n->aer_queue, event, entry);
1616     n->aer_queued++;
1617 
1618     nvme_process_aers(n);
1619 }
1620 
1621 static void nvme_smart_event(NvmeCtrl *n, uint8_t event)
1622 {
1623     uint8_t aer_info;
1624 
1625     /* Ref SPEC <Asynchronous Event Information 0x2013 SMART / Health Status> */
1626     if (!(NVME_AEC_SMART(n->features.async_config) & event)) {
1627         return;
1628     }
1629 
1630     switch (event) {
1631     case NVME_SMART_SPARE:
1632         aer_info = NVME_AER_INFO_SMART_SPARE_THRESH;
1633         break;
1634     case NVME_SMART_TEMPERATURE:
1635         aer_info = NVME_AER_INFO_SMART_TEMP_THRESH;
1636         break;
1637     case NVME_SMART_RELIABILITY:
1638     case NVME_SMART_MEDIA_READ_ONLY:
1639     case NVME_SMART_FAILED_VOLATILE_MEDIA:
1640     case NVME_SMART_PMR_UNRELIABLE:
1641         aer_info = NVME_AER_INFO_SMART_RELIABILITY;
1642         break;
1643     default:
1644         return;
1645     }
1646 
1647     nvme_enqueue_event(n, NVME_AER_TYPE_SMART, aer_info, NVME_LOG_SMART_INFO);
1648 }
1649 
1650 static void nvme_clear_events(NvmeCtrl *n, uint8_t event_type)
1651 {
1652     n->aer_mask &= ~(1 << event_type);
1653     if (!QTAILQ_EMPTY(&n->aer_queue)) {
1654         nvme_process_aers(n);
1655     }
1656 }
1657 
1658 static inline uint16_t nvme_check_mdts(NvmeCtrl *n, size_t len)
1659 {
1660     uint8_t mdts = n->params.mdts;
1661 
1662     if (mdts && len > n->page_size << mdts) {
1663         trace_pci_nvme_err_mdts(len);
1664         return NVME_INVALID_FIELD | NVME_DNR;
1665     }
1666 
1667     return NVME_SUCCESS;
1668 }
1669 
1670 static inline uint16_t nvme_check_bounds(NvmeNamespace *ns, uint64_t slba,
1671                                          uint32_t nlb)
1672 {
1673     uint64_t nsze = le64_to_cpu(ns->id_ns.nsze);
1674 
1675     if (unlikely(UINT64_MAX - slba < nlb || slba + nlb > nsze)) {
1676         trace_pci_nvme_err_invalid_lba_range(slba, nlb, nsze);
1677         return NVME_LBA_RANGE | NVME_DNR;
1678     }
1679 
1680     return NVME_SUCCESS;
1681 }
1682 
1683 static int nvme_block_status_all(NvmeNamespace *ns, uint64_t slba,
1684                                  uint32_t nlb, int flags)
1685 {
1686     BlockDriverState *bs = blk_bs(ns->blkconf.blk);
1687 
1688     int64_t pnum = 0, bytes = nvme_l2b(ns, nlb);
1689     int64_t offset = nvme_l2b(ns, slba);
1690     int ret;
1691 
1692     /*
1693      * `pnum` holds the number of bytes after offset that shares the same
1694      * allocation status as the byte at offset. If `pnum` is different from
1695      * `bytes`, we should check the allocation status of the next range and
1696      * continue this until all bytes have been checked.
1697      */
1698     do {
1699         bytes -= pnum;
1700 
1701         ret = bdrv_block_status(bs, offset, bytes, &pnum, NULL, NULL);
1702         if (ret < 0) {
1703             return ret;
1704         }
1705 
1706 
1707         trace_pci_nvme_block_status(offset, bytes, pnum, ret,
1708                                     !!(ret & BDRV_BLOCK_ZERO));
1709 
1710         if (!(ret & flags)) {
1711             return 1;
1712         }
1713 
1714         offset += pnum;
1715     } while (pnum != bytes);
1716 
1717     return 0;
1718 }
1719 
1720 static uint16_t nvme_check_dulbe(NvmeNamespace *ns, uint64_t slba,
1721                                  uint32_t nlb)
1722 {
1723     int ret;
1724     Error *err = NULL;
1725 
1726     ret = nvme_block_status_all(ns, slba, nlb, BDRV_BLOCK_DATA);
1727     if (ret) {
1728         if (ret < 0) {
1729             error_setg_errno(&err, -ret, "unable to get block status");
1730             error_report_err(err);
1731 
1732             return NVME_INTERNAL_DEV_ERROR;
1733         }
1734 
1735         return NVME_DULB;
1736     }
1737 
1738     return NVME_SUCCESS;
1739 }
1740 
1741 static void nvme_aio_err(NvmeRequest *req, int ret)
1742 {
1743     uint16_t status = NVME_SUCCESS;
1744     Error *local_err = NULL;
1745 
1746     switch (req->cmd.opcode) {
1747     case NVME_CMD_READ:
1748         status = NVME_UNRECOVERED_READ;
1749         break;
1750     case NVME_CMD_FLUSH:
1751     case NVME_CMD_WRITE:
1752     case NVME_CMD_WRITE_ZEROES:
1753     case NVME_CMD_ZONE_APPEND:
1754     case NVME_CMD_COPY:
1755         status = NVME_WRITE_FAULT;
1756         break;
1757     default:
1758         status = NVME_INTERNAL_DEV_ERROR;
1759         break;
1760     }
1761 
1762     trace_pci_nvme_err_aio(nvme_cid(req), strerror(-ret), status);
1763 
1764     error_setg_errno(&local_err, -ret, "aio failed");
1765     error_report_err(local_err);
1766 
1767     /*
1768      * Set the command status code to the first encountered error but allow a
1769      * subsequent Internal Device Error to trump it.
1770      */
1771     if (req->status && status != NVME_INTERNAL_DEV_ERROR) {
1772         return;
1773     }
1774 
1775     req->status = status;
1776 }
1777 
1778 static inline uint32_t nvme_zone_idx(NvmeNamespace *ns, uint64_t slba)
1779 {
1780     return ns->zone_size_log2 > 0 ? slba >> ns->zone_size_log2 :
1781                                     slba / ns->zone_size;
1782 }
1783 
1784 static inline NvmeZone *nvme_get_zone_by_slba(NvmeNamespace *ns, uint64_t slba)
1785 {
1786     uint32_t zone_idx = nvme_zone_idx(ns, slba);
1787 
1788     if (zone_idx >= ns->num_zones) {
1789         return NULL;
1790     }
1791 
1792     return &ns->zone_array[zone_idx];
1793 }
1794 
1795 static uint16_t nvme_check_zone_state_for_write(NvmeZone *zone)
1796 {
1797     uint64_t zslba = zone->d.zslba;
1798 
1799     switch (nvme_get_zone_state(zone)) {
1800     case NVME_ZONE_STATE_EMPTY:
1801     case NVME_ZONE_STATE_IMPLICITLY_OPEN:
1802     case NVME_ZONE_STATE_EXPLICITLY_OPEN:
1803     case NVME_ZONE_STATE_CLOSED:
1804         return NVME_SUCCESS;
1805     case NVME_ZONE_STATE_FULL:
1806         trace_pci_nvme_err_zone_is_full(zslba);
1807         return NVME_ZONE_FULL;
1808     case NVME_ZONE_STATE_OFFLINE:
1809         trace_pci_nvme_err_zone_is_offline(zslba);
1810         return NVME_ZONE_OFFLINE;
1811     case NVME_ZONE_STATE_READ_ONLY:
1812         trace_pci_nvme_err_zone_is_read_only(zslba);
1813         return NVME_ZONE_READ_ONLY;
1814     default:
1815         assert(false);
1816     }
1817 
1818     return NVME_INTERNAL_DEV_ERROR;
1819 }
1820 
1821 static uint16_t nvme_check_zone_write(NvmeNamespace *ns, NvmeZone *zone,
1822                                       uint64_t slba, uint32_t nlb)
1823 {
1824     uint64_t zcap = nvme_zone_wr_boundary(zone);
1825     uint16_t status;
1826 
1827     status = nvme_check_zone_state_for_write(zone);
1828     if (status) {
1829         return status;
1830     }
1831 
1832     if (zone->d.za & NVME_ZA_ZRWA_VALID) {
1833         uint64_t ezrwa = zone->w_ptr + 2 * ns->zns.zrwas;
1834 
1835         if (slba < zone->w_ptr || slba + nlb > ezrwa) {
1836             trace_pci_nvme_err_zone_invalid_write(slba, zone->w_ptr);
1837             return NVME_ZONE_INVALID_WRITE;
1838         }
1839     } else {
1840         if (unlikely(slba != zone->w_ptr)) {
1841             trace_pci_nvme_err_write_not_at_wp(slba, zone->d.zslba,
1842                                                zone->w_ptr);
1843             return NVME_ZONE_INVALID_WRITE;
1844         }
1845     }
1846 
1847     if (unlikely((slba + nlb) > zcap)) {
1848         trace_pci_nvme_err_zone_boundary(slba, nlb, zcap);
1849         return NVME_ZONE_BOUNDARY_ERROR;
1850     }
1851 
1852     return NVME_SUCCESS;
1853 }
1854 
1855 static uint16_t nvme_check_zone_state_for_read(NvmeZone *zone)
1856 {
1857     switch (nvme_get_zone_state(zone)) {
1858     case NVME_ZONE_STATE_EMPTY:
1859     case NVME_ZONE_STATE_IMPLICITLY_OPEN:
1860     case NVME_ZONE_STATE_EXPLICITLY_OPEN:
1861     case NVME_ZONE_STATE_FULL:
1862     case NVME_ZONE_STATE_CLOSED:
1863     case NVME_ZONE_STATE_READ_ONLY:
1864         return NVME_SUCCESS;
1865     case NVME_ZONE_STATE_OFFLINE:
1866         trace_pci_nvme_err_zone_is_offline(zone->d.zslba);
1867         return NVME_ZONE_OFFLINE;
1868     default:
1869         assert(false);
1870     }
1871 
1872     return NVME_INTERNAL_DEV_ERROR;
1873 }
1874 
1875 static uint16_t nvme_check_zone_read(NvmeNamespace *ns, uint64_t slba,
1876                                      uint32_t nlb)
1877 {
1878     NvmeZone *zone;
1879     uint64_t bndry, end;
1880     uint16_t status;
1881 
1882     zone = nvme_get_zone_by_slba(ns, slba);
1883     assert(zone);
1884 
1885     bndry = nvme_zone_rd_boundary(ns, zone);
1886     end = slba + nlb;
1887 
1888     status = nvme_check_zone_state_for_read(zone);
1889     if (status) {
1890         ;
1891     } else if (unlikely(end > bndry)) {
1892         if (!ns->params.cross_zone_read) {
1893             status = NVME_ZONE_BOUNDARY_ERROR;
1894         } else {
1895             /*
1896              * Read across zone boundary - check that all subsequent
1897              * zones that are being read have an appropriate state.
1898              */
1899             do {
1900                 zone++;
1901                 status = nvme_check_zone_state_for_read(zone);
1902                 if (status) {
1903                     break;
1904                 }
1905             } while (end > nvme_zone_rd_boundary(ns, zone));
1906         }
1907     }
1908 
1909     return status;
1910 }
1911 
1912 static uint16_t nvme_zrm_finish(NvmeNamespace *ns, NvmeZone *zone)
1913 {
1914     switch (nvme_get_zone_state(zone)) {
1915     case NVME_ZONE_STATE_FULL:
1916         return NVME_SUCCESS;
1917 
1918     case NVME_ZONE_STATE_IMPLICITLY_OPEN:
1919     case NVME_ZONE_STATE_EXPLICITLY_OPEN:
1920         nvme_aor_dec_open(ns);
1921         /* fallthrough */
1922     case NVME_ZONE_STATE_CLOSED:
1923         nvme_aor_dec_active(ns);
1924 
1925         if (zone->d.za & NVME_ZA_ZRWA_VALID) {
1926             zone->d.za &= ~NVME_ZA_ZRWA_VALID;
1927             if (ns->params.numzrwa) {
1928                 ns->zns.numzrwa++;
1929             }
1930         }
1931 
1932         /* fallthrough */
1933     case NVME_ZONE_STATE_EMPTY:
1934         nvme_assign_zone_state(ns, zone, NVME_ZONE_STATE_FULL);
1935         return NVME_SUCCESS;
1936 
1937     default:
1938         return NVME_ZONE_INVAL_TRANSITION;
1939     }
1940 }
1941 
1942 static uint16_t nvme_zrm_close(NvmeNamespace *ns, NvmeZone *zone)
1943 {
1944     switch (nvme_get_zone_state(zone)) {
1945     case NVME_ZONE_STATE_EXPLICITLY_OPEN:
1946     case NVME_ZONE_STATE_IMPLICITLY_OPEN:
1947         nvme_aor_dec_open(ns);
1948         nvme_assign_zone_state(ns, zone, NVME_ZONE_STATE_CLOSED);
1949         /* fall through */
1950     case NVME_ZONE_STATE_CLOSED:
1951         return NVME_SUCCESS;
1952 
1953     default:
1954         return NVME_ZONE_INVAL_TRANSITION;
1955     }
1956 }
1957 
1958 static uint16_t nvme_zrm_reset(NvmeNamespace *ns, NvmeZone *zone)
1959 {
1960     switch (nvme_get_zone_state(zone)) {
1961     case NVME_ZONE_STATE_EXPLICITLY_OPEN:
1962     case NVME_ZONE_STATE_IMPLICITLY_OPEN:
1963         nvme_aor_dec_open(ns);
1964         /* fallthrough */
1965     case NVME_ZONE_STATE_CLOSED:
1966         nvme_aor_dec_active(ns);
1967 
1968         if (zone->d.za & NVME_ZA_ZRWA_VALID) {
1969             if (ns->params.numzrwa) {
1970                 ns->zns.numzrwa++;
1971             }
1972         }
1973 
1974         /* fallthrough */
1975     case NVME_ZONE_STATE_FULL:
1976         zone->w_ptr = zone->d.zslba;
1977         zone->d.wp = zone->w_ptr;
1978         nvme_assign_zone_state(ns, zone, NVME_ZONE_STATE_EMPTY);
1979         /* fallthrough */
1980     case NVME_ZONE_STATE_EMPTY:
1981         return NVME_SUCCESS;
1982 
1983     default:
1984         return NVME_ZONE_INVAL_TRANSITION;
1985     }
1986 }
1987 
1988 static void nvme_zrm_auto_transition_zone(NvmeNamespace *ns)
1989 {
1990     NvmeZone *zone;
1991 
1992     if (ns->params.max_open_zones &&
1993         ns->nr_open_zones == ns->params.max_open_zones) {
1994         zone = QTAILQ_FIRST(&ns->imp_open_zones);
1995         if (zone) {
1996             /*
1997              * Automatically close this implicitly open zone.
1998              */
1999             QTAILQ_REMOVE(&ns->imp_open_zones, zone, entry);
2000             nvme_zrm_close(ns, zone);
2001         }
2002     }
2003 }
2004 
2005 enum {
2006     NVME_ZRM_AUTO = 1 << 0,
2007     NVME_ZRM_ZRWA = 1 << 1,
2008 };
2009 
2010 static uint16_t nvme_zrm_open_flags(NvmeCtrl *n, NvmeNamespace *ns,
2011                                     NvmeZone *zone, int flags)
2012 {
2013     int act = 0;
2014     uint16_t status;
2015 
2016     switch (nvme_get_zone_state(zone)) {
2017     case NVME_ZONE_STATE_EMPTY:
2018         act = 1;
2019 
2020         /* fallthrough */
2021 
2022     case NVME_ZONE_STATE_CLOSED:
2023         if (n->params.auto_transition_zones) {
2024             nvme_zrm_auto_transition_zone(ns);
2025         }
2026         status = nvme_zns_check_resources(ns, act, 1,
2027                                           (flags & NVME_ZRM_ZRWA) ? 1 : 0);
2028         if (status) {
2029             return status;
2030         }
2031 
2032         if (act) {
2033             nvme_aor_inc_active(ns);
2034         }
2035 
2036         nvme_aor_inc_open(ns);
2037 
2038         if (flags & NVME_ZRM_AUTO) {
2039             nvme_assign_zone_state(ns, zone, NVME_ZONE_STATE_IMPLICITLY_OPEN);
2040             return NVME_SUCCESS;
2041         }
2042 
2043         /* fallthrough */
2044 
2045     case NVME_ZONE_STATE_IMPLICITLY_OPEN:
2046         if (flags & NVME_ZRM_AUTO) {
2047             return NVME_SUCCESS;
2048         }
2049 
2050         nvme_assign_zone_state(ns, zone, NVME_ZONE_STATE_EXPLICITLY_OPEN);
2051 
2052         /* fallthrough */
2053 
2054     case NVME_ZONE_STATE_EXPLICITLY_OPEN:
2055         if (flags & NVME_ZRM_ZRWA) {
2056             ns->zns.numzrwa--;
2057 
2058             zone->d.za |= NVME_ZA_ZRWA_VALID;
2059         }
2060 
2061         return NVME_SUCCESS;
2062 
2063     default:
2064         return NVME_ZONE_INVAL_TRANSITION;
2065     }
2066 }
2067 
2068 static inline uint16_t nvme_zrm_auto(NvmeCtrl *n, NvmeNamespace *ns,
2069                                      NvmeZone *zone)
2070 {
2071     return nvme_zrm_open_flags(n, ns, zone, NVME_ZRM_AUTO);
2072 }
2073 
2074 static void nvme_advance_zone_wp(NvmeNamespace *ns, NvmeZone *zone,
2075                                  uint32_t nlb)
2076 {
2077     zone->d.wp += nlb;
2078 
2079     if (zone->d.wp == nvme_zone_wr_boundary(zone)) {
2080         nvme_zrm_finish(ns, zone);
2081     }
2082 }
2083 
2084 static void nvme_zoned_zrwa_implicit_flush(NvmeNamespace *ns, NvmeZone *zone,
2085                                            uint32_t nlbc)
2086 {
2087     uint16_t nzrwafgs = DIV_ROUND_UP(nlbc, ns->zns.zrwafg);
2088 
2089     nlbc = nzrwafgs * ns->zns.zrwafg;
2090 
2091     trace_pci_nvme_zoned_zrwa_implicit_flush(zone->d.zslba, nlbc);
2092 
2093     zone->w_ptr += nlbc;
2094 
2095     nvme_advance_zone_wp(ns, zone, nlbc);
2096 }
2097 
2098 static void nvme_finalize_zoned_write(NvmeNamespace *ns, NvmeRequest *req)
2099 {
2100     NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
2101     NvmeZone *zone;
2102     uint64_t slba;
2103     uint32_t nlb;
2104 
2105     slba = le64_to_cpu(rw->slba);
2106     nlb = le16_to_cpu(rw->nlb) + 1;
2107     zone = nvme_get_zone_by_slba(ns, slba);
2108     assert(zone);
2109 
2110     if (zone->d.za & NVME_ZA_ZRWA_VALID) {
2111         uint64_t ezrwa = zone->w_ptr + ns->zns.zrwas - 1;
2112         uint64_t elba = slba + nlb - 1;
2113 
2114         if (elba > ezrwa) {
2115             nvme_zoned_zrwa_implicit_flush(ns, zone, elba - ezrwa);
2116         }
2117 
2118         return;
2119     }
2120 
2121     nvme_advance_zone_wp(ns, zone, nlb);
2122 }
2123 
2124 static inline bool nvme_is_write(NvmeRequest *req)
2125 {
2126     NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
2127 
2128     return rw->opcode == NVME_CMD_WRITE ||
2129            rw->opcode == NVME_CMD_ZONE_APPEND ||
2130            rw->opcode == NVME_CMD_WRITE_ZEROES;
2131 }
2132 
2133 static void nvme_misc_cb(void *opaque, int ret)
2134 {
2135     NvmeRequest *req = opaque;
2136 
2137     trace_pci_nvme_misc_cb(nvme_cid(req));
2138 
2139     if (ret) {
2140         nvme_aio_err(req, ret);
2141     }
2142 
2143     nvme_enqueue_req_completion(nvme_cq(req), req);
2144 }
2145 
2146 void nvme_rw_complete_cb(void *opaque, int ret)
2147 {
2148     NvmeRequest *req = opaque;
2149     NvmeNamespace *ns = req->ns;
2150     BlockBackend *blk = ns->blkconf.blk;
2151     BlockAcctCookie *acct = &req->acct;
2152     BlockAcctStats *stats = blk_get_stats(blk);
2153 
2154     trace_pci_nvme_rw_complete_cb(nvme_cid(req), blk_name(blk));
2155 
2156     if (ret) {
2157         block_acct_failed(stats, acct);
2158         nvme_aio_err(req, ret);
2159     } else {
2160         block_acct_done(stats, acct);
2161     }
2162 
2163     if (ns->params.zoned && nvme_is_write(req)) {
2164         nvme_finalize_zoned_write(ns, req);
2165     }
2166 
2167     nvme_enqueue_req_completion(nvme_cq(req), req);
2168 }
2169 
2170 static void nvme_rw_cb(void *opaque, int ret)
2171 {
2172     NvmeRequest *req = opaque;
2173     NvmeNamespace *ns = req->ns;
2174 
2175     BlockBackend *blk = ns->blkconf.blk;
2176 
2177     trace_pci_nvme_rw_cb(nvme_cid(req), blk_name(blk));
2178 
2179     if (ret) {
2180         goto out;
2181     }
2182 
2183     if (ns->lbaf.ms) {
2184         NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
2185         uint64_t slba = le64_to_cpu(rw->slba);
2186         uint32_t nlb = (uint32_t)le16_to_cpu(rw->nlb) + 1;
2187         uint64_t offset = nvme_moff(ns, slba);
2188 
2189         if (req->cmd.opcode == NVME_CMD_WRITE_ZEROES) {
2190             size_t mlen = nvme_m2b(ns, nlb);
2191 
2192             req->aiocb = blk_aio_pwrite_zeroes(blk, offset, mlen,
2193                                                BDRV_REQ_MAY_UNMAP,
2194                                                nvme_rw_complete_cb, req);
2195             return;
2196         }
2197 
2198         if (nvme_ns_ext(ns) || req->cmd.mptr) {
2199             uint16_t status;
2200 
2201             nvme_sg_unmap(&req->sg);
2202             status = nvme_map_mdata(nvme_ctrl(req), nlb, req);
2203             if (status) {
2204                 ret = -EFAULT;
2205                 goto out;
2206             }
2207 
2208             if (req->cmd.opcode == NVME_CMD_READ) {
2209                 return nvme_blk_read(blk, offset, 1, nvme_rw_complete_cb, req);
2210             }
2211 
2212             return nvme_blk_write(blk, offset, 1, nvme_rw_complete_cb, req);
2213         }
2214     }
2215 
2216 out:
2217     nvme_rw_complete_cb(req, ret);
2218 }
2219 
2220 static void nvme_verify_cb(void *opaque, int ret)
2221 {
2222     NvmeBounceContext *ctx = opaque;
2223     NvmeRequest *req = ctx->req;
2224     NvmeNamespace *ns = req->ns;
2225     BlockBackend *blk = ns->blkconf.blk;
2226     BlockAcctCookie *acct = &req->acct;
2227     BlockAcctStats *stats = blk_get_stats(blk);
2228     NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
2229     uint64_t slba = le64_to_cpu(rw->slba);
2230     uint8_t prinfo = NVME_RW_PRINFO(le16_to_cpu(rw->control));
2231     uint16_t apptag = le16_to_cpu(rw->apptag);
2232     uint16_t appmask = le16_to_cpu(rw->appmask);
2233     uint64_t reftag = le32_to_cpu(rw->reftag);
2234     uint64_t cdw3 = le32_to_cpu(rw->cdw3);
2235     uint16_t status;
2236 
2237     reftag |= cdw3 << 32;
2238 
2239     trace_pci_nvme_verify_cb(nvme_cid(req), prinfo, apptag, appmask, reftag);
2240 
2241     if (ret) {
2242         block_acct_failed(stats, acct);
2243         nvme_aio_err(req, ret);
2244         goto out;
2245     }
2246 
2247     block_acct_done(stats, acct);
2248 
2249     if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
2250         status = nvme_dif_mangle_mdata(ns, ctx->mdata.bounce,
2251                                        ctx->mdata.iov.size, slba);
2252         if (status) {
2253             req->status = status;
2254             goto out;
2255         }
2256 
2257         req->status = nvme_dif_check(ns, ctx->data.bounce, ctx->data.iov.size,
2258                                      ctx->mdata.bounce, ctx->mdata.iov.size,
2259                                      prinfo, slba, apptag, appmask, &reftag);
2260     }
2261 
2262 out:
2263     qemu_iovec_destroy(&ctx->data.iov);
2264     g_free(ctx->data.bounce);
2265 
2266     qemu_iovec_destroy(&ctx->mdata.iov);
2267     g_free(ctx->mdata.bounce);
2268 
2269     g_free(ctx);
2270 
2271     nvme_enqueue_req_completion(nvme_cq(req), req);
2272 }
2273 
2274 
2275 static void nvme_verify_mdata_in_cb(void *opaque, int ret)
2276 {
2277     NvmeBounceContext *ctx = opaque;
2278     NvmeRequest *req = ctx->req;
2279     NvmeNamespace *ns = req->ns;
2280     NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
2281     uint64_t slba = le64_to_cpu(rw->slba);
2282     uint32_t nlb = le16_to_cpu(rw->nlb) + 1;
2283     size_t mlen = nvme_m2b(ns, nlb);
2284     uint64_t offset = nvme_moff(ns, slba);
2285     BlockBackend *blk = ns->blkconf.blk;
2286 
2287     trace_pci_nvme_verify_mdata_in_cb(nvme_cid(req), blk_name(blk));
2288 
2289     if (ret) {
2290         goto out;
2291     }
2292 
2293     ctx->mdata.bounce = g_malloc(mlen);
2294 
2295     qemu_iovec_reset(&ctx->mdata.iov);
2296     qemu_iovec_add(&ctx->mdata.iov, ctx->mdata.bounce, mlen);
2297 
2298     req->aiocb = blk_aio_preadv(blk, offset, &ctx->mdata.iov, 0,
2299                                 nvme_verify_cb, ctx);
2300     return;
2301 
2302 out:
2303     nvme_verify_cb(ctx, ret);
2304 }
2305 
2306 struct nvme_compare_ctx {
2307     struct {
2308         QEMUIOVector iov;
2309         uint8_t *bounce;
2310     } data;
2311 
2312     struct {
2313         QEMUIOVector iov;
2314         uint8_t *bounce;
2315     } mdata;
2316 };
2317 
2318 static void nvme_compare_mdata_cb(void *opaque, int ret)
2319 {
2320     NvmeRequest *req = opaque;
2321     NvmeNamespace *ns = req->ns;
2322     NvmeCtrl *n = nvme_ctrl(req);
2323     NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
2324     uint8_t prinfo = NVME_RW_PRINFO(le16_to_cpu(rw->control));
2325     uint16_t apptag = le16_to_cpu(rw->apptag);
2326     uint16_t appmask = le16_to_cpu(rw->appmask);
2327     uint64_t reftag = le32_to_cpu(rw->reftag);
2328     uint64_t cdw3 = le32_to_cpu(rw->cdw3);
2329     struct nvme_compare_ctx *ctx = req->opaque;
2330     g_autofree uint8_t *buf = NULL;
2331     BlockBackend *blk = ns->blkconf.blk;
2332     BlockAcctCookie *acct = &req->acct;
2333     BlockAcctStats *stats = blk_get_stats(blk);
2334     uint16_t status = NVME_SUCCESS;
2335 
2336     reftag |= cdw3 << 32;
2337 
2338     trace_pci_nvme_compare_mdata_cb(nvme_cid(req));
2339 
2340     if (ret) {
2341         block_acct_failed(stats, acct);
2342         nvme_aio_err(req, ret);
2343         goto out;
2344     }
2345 
2346     buf = g_malloc(ctx->mdata.iov.size);
2347 
2348     status = nvme_bounce_mdata(n, buf, ctx->mdata.iov.size,
2349                                NVME_TX_DIRECTION_TO_DEVICE, req);
2350     if (status) {
2351         req->status = status;
2352         goto out;
2353     }
2354 
2355     if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
2356         uint64_t slba = le64_to_cpu(rw->slba);
2357         uint8_t *bufp;
2358         uint8_t *mbufp = ctx->mdata.bounce;
2359         uint8_t *end = mbufp + ctx->mdata.iov.size;
2360         int16_t pil = 0;
2361 
2362         status = nvme_dif_check(ns, ctx->data.bounce, ctx->data.iov.size,
2363                                 ctx->mdata.bounce, ctx->mdata.iov.size, prinfo,
2364                                 slba, apptag, appmask, &reftag);
2365         if (status) {
2366             req->status = status;
2367             goto out;
2368         }
2369 
2370         /*
2371          * When formatted with protection information, do not compare the DIF
2372          * tuple.
2373          */
2374         if (!(ns->id_ns.dps & NVME_ID_NS_DPS_FIRST_EIGHT)) {
2375             pil = ns->lbaf.ms - nvme_pi_tuple_size(ns);
2376         }
2377 
2378         for (bufp = buf; mbufp < end; bufp += ns->lbaf.ms, mbufp += ns->lbaf.ms) {
2379             if (memcmp(bufp + pil, mbufp + pil, ns->lbaf.ms - pil)) {
2380                 req->status = NVME_CMP_FAILURE | NVME_DNR;
2381                 goto out;
2382             }
2383         }
2384 
2385         goto out;
2386     }
2387 
2388     if (memcmp(buf, ctx->mdata.bounce, ctx->mdata.iov.size)) {
2389         req->status = NVME_CMP_FAILURE | NVME_DNR;
2390         goto out;
2391     }
2392 
2393     block_acct_done(stats, acct);
2394 
2395 out:
2396     qemu_iovec_destroy(&ctx->data.iov);
2397     g_free(ctx->data.bounce);
2398 
2399     qemu_iovec_destroy(&ctx->mdata.iov);
2400     g_free(ctx->mdata.bounce);
2401 
2402     g_free(ctx);
2403 
2404     nvme_enqueue_req_completion(nvme_cq(req), req);
2405 }
2406 
2407 static void nvme_compare_data_cb(void *opaque, int ret)
2408 {
2409     NvmeRequest *req = opaque;
2410     NvmeCtrl *n = nvme_ctrl(req);
2411     NvmeNamespace *ns = req->ns;
2412     BlockBackend *blk = ns->blkconf.blk;
2413     BlockAcctCookie *acct = &req->acct;
2414     BlockAcctStats *stats = blk_get_stats(blk);
2415 
2416     struct nvme_compare_ctx *ctx = req->opaque;
2417     g_autofree uint8_t *buf = NULL;
2418     uint16_t status;
2419 
2420     trace_pci_nvme_compare_data_cb(nvme_cid(req));
2421 
2422     if (ret) {
2423         block_acct_failed(stats, acct);
2424         nvme_aio_err(req, ret);
2425         goto out;
2426     }
2427 
2428     buf = g_malloc(ctx->data.iov.size);
2429 
2430     status = nvme_bounce_data(n, buf, ctx->data.iov.size,
2431                               NVME_TX_DIRECTION_TO_DEVICE, req);
2432     if (status) {
2433         req->status = status;
2434         goto out;
2435     }
2436 
2437     if (memcmp(buf, ctx->data.bounce, ctx->data.iov.size)) {
2438         req->status = NVME_CMP_FAILURE | NVME_DNR;
2439         goto out;
2440     }
2441 
2442     if (ns->lbaf.ms) {
2443         NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
2444         uint64_t slba = le64_to_cpu(rw->slba);
2445         uint32_t nlb = le16_to_cpu(rw->nlb) + 1;
2446         size_t mlen = nvme_m2b(ns, nlb);
2447         uint64_t offset = nvme_moff(ns, slba);
2448 
2449         ctx->mdata.bounce = g_malloc(mlen);
2450 
2451         qemu_iovec_init(&ctx->mdata.iov, 1);
2452         qemu_iovec_add(&ctx->mdata.iov, ctx->mdata.bounce, mlen);
2453 
2454         req->aiocb = blk_aio_preadv(blk, offset, &ctx->mdata.iov, 0,
2455                                     nvme_compare_mdata_cb, req);
2456         return;
2457     }
2458 
2459     block_acct_done(stats, acct);
2460 
2461 out:
2462     qemu_iovec_destroy(&ctx->data.iov);
2463     g_free(ctx->data.bounce);
2464     g_free(ctx);
2465 
2466     nvme_enqueue_req_completion(nvme_cq(req), req);
2467 }
2468 
2469 typedef struct NvmeDSMAIOCB {
2470     BlockAIOCB common;
2471     BlockAIOCB *aiocb;
2472     NvmeRequest *req;
2473     int ret;
2474 
2475     NvmeDsmRange *range;
2476     unsigned int nr;
2477     unsigned int idx;
2478 } NvmeDSMAIOCB;
2479 
2480 static void nvme_dsm_cancel(BlockAIOCB *aiocb)
2481 {
2482     NvmeDSMAIOCB *iocb = container_of(aiocb, NvmeDSMAIOCB, common);
2483 
2484     /* break nvme_dsm_cb loop */
2485     iocb->idx = iocb->nr;
2486     iocb->ret = -ECANCELED;
2487 
2488     if (iocb->aiocb) {
2489         blk_aio_cancel_async(iocb->aiocb);
2490         iocb->aiocb = NULL;
2491     } else {
2492         /*
2493          * We only reach this if nvme_dsm_cancel() has already been called or
2494          * the command ran to completion.
2495          */
2496         assert(iocb->idx == iocb->nr);
2497     }
2498 }
2499 
2500 static const AIOCBInfo nvme_dsm_aiocb_info = {
2501     .aiocb_size   = sizeof(NvmeDSMAIOCB),
2502     .cancel_async = nvme_dsm_cancel,
2503 };
2504 
2505 static void nvme_dsm_cb(void *opaque, int ret);
2506 
2507 static void nvme_dsm_md_cb(void *opaque, int ret)
2508 {
2509     NvmeDSMAIOCB *iocb = opaque;
2510     NvmeRequest *req = iocb->req;
2511     NvmeNamespace *ns = req->ns;
2512     NvmeDsmRange *range;
2513     uint64_t slba;
2514     uint32_t nlb;
2515 
2516     if (ret < 0 || iocb->ret < 0 || !ns->lbaf.ms) {
2517         goto done;
2518     }
2519 
2520     range = &iocb->range[iocb->idx - 1];
2521     slba = le64_to_cpu(range->slba);
2522     nlb = le32_to_cpu(range->nlb);
2523 
2524     /*
2525      * Check that all block were discarded (zeroed); otherwise we do not zero
2526      * the metadata.
2527      */
2528 
2529     ret = nvme_block_status_all(ns, slba, nlb, BDRV_BLOCK_ZERO);
2530     if (ret) {
2531         if (ret < 0) {
2532             goto done;
2533         }
2534 
2535         nvme_dsm_cb(iocb, 0);
2536         return;
2537     }
2538 
2539     iocb->aiocb = blk_aio_pwrite_zeroes(ns->blkconf.blk, nvme_moff(ns, slba),
2540                                         nvme_m2b(ns, nlb), BDRV_REQ_MAY_UNMAP,
2541                                         nvme_dsm_cb, iocb);
2542     return;
2543 
2544 done:
2545     nvme_dsm_cb(iocb, ret);
2546 }
2547 
2548 static void nvme_dsm_cb(void *opaque, int ret)
2549 {
2550     NvmeDSMAIOCB *iocb = opaque;
2551     NvmeRequest *req = iocb->req;
2552     NvmeCtrl *n = nvme_ctrl(req);
2553     NvmeNamespace *ns = req->ns;
2554     NvmeDsmRange *range;
2555     uint64_t slba;
2556     uint32_t nlb;
2557 
2558     if (iocb->ret < 0) {
2559         goto done;
2560     } else if (ret < 0) {
2561         iocb->ret = ret;
2562         goto done;
2563     }
2564 
2565 next:
2566     if (iocb->idx == iocb->nr) {
2567         goto done;
2568     }
2569 
2570     range = &iocb->range[iocb->idx++];
2571     slba = le64_to_cpu(range->slba);
2572     nlb = le32_to_cpu(range->nlb);
2573 
2574     trace_pci_nvme_dsm_deallocate(slba, nlb);
2575 
2576     if (nlb > n->dmrsl) {
2577         trace_pci_nvme_dsm_single_range_limit_exceeded(nlb, n->dmrsl);
2578         goto next;
2579     }
2580 
2581     if (nvme_check_bounds(ns, slba, nlb)) {
2582         trace_pci_nvme_err_invalid_lba_range(slba, nlb,
2583                                              ns->id_ns.nsze);
2584         goto next;
2585     }
2586 
2587     iocb->aiocb = blk_aio_pdiscard(ns->blkconf.blk, nvme_l2b(ns, slba),
2588                                    nvme_l2b(ns, nlb),
2589                                    nvme_dsm_md_cb, iocb);
2590     return;
2591 
2592 done:
2593     iocb->aiocb = NULL;
2594     iocb->common.cb(iocb->common.opaque, iocb->ret);
2595     qemu_aio_unref(iocb);
2596 }
2597 
2598 static uint16_t nvme_dsm(NvmeCtrl *n, NvmeRequest *req)
2599 {
2600     NvmeNamespace *ns = req->ns;
2601     NvmeDsmCmd *dsm = (NvmeDsmCmd *) &req->cmd;
2602     uint32_t attr = le32_to_cpu(dsm->attributes);
2603     uint32_t nr = (le32_to_cpu(dsm->nr) & 0xff) + 1;
2604     uint16_t status = NVME_SUCCESS;
2605 
2606     trace_pci_nvme_dsm(nr, attr);
2607 
2608     if (attr & NVME_DSMGMT_AD) {
2609         NvmeDSMAIOCB *iocb = blk_aio_get(&nvme_dsm_aiocb_info, ns->blkconf.blk,
2610                                          nvme_misc_cb, req);
2611 
2612         iocb->req = req;
2613         iocb->ret = 0;
2614         iocb->range = g_new(NvmeDsmRange, nr);
2615         iocb->nr = nr;
2616         iocb->idx = 0;
2617 
2618         status = nvme_h2c(n, (uint8_t *)iocb->range, sizeof(NvmeDsmRange) * nr,
2619                           req);
2620         if (status) {
2621             g_free(iocb->range);
2622             qemu_aio_unref(iocb);
2623 
2624             return status;
2625         }
2626 
2627         req->aiocb = &iocb->common;
2628         nvme_dsm_cb(iocb, 0);
2629 
2630         return NVME_NO_COMPLETE;
2631     }
2632 
2633     return status;
2634 }
2635 
2636 static uint16_t nvme_verify(NvmeCtrl *n, NvmeRequest *req)
2637 {
2638     NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
2639     NvmeNamespace *ns = req->ns;
2640     BlockBackend *blk = ns->blkconf.blk;
2641     uint64_t slba = le64_to_cpu(rw->slba);
2642     uint32_t nlb = le16_to_cpu(rw->nlb) + 1;
2643     size_t len = nvme_l2b(ns, nlb);
2644     int64_t offset = nvme_l2b(ns, slba);
2645     uint8_t prinfo = NVME_RW_PRINFO(le16_to_cpu(rw->control));
2646     uint32_t reftag = le32_to_cpu(rw->reftag);
2647     NvmeBounceContext *ctx = NULL;
2648     uint16_t status;
2649 
2650     trace_pci_nvme_verify(nvme_cid(req), nvme_nsid(ns), slba, nlb);
2651 
2652     if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
2653         status = nvme_check_prinfo(ns, prinfo, slba, reftag);
2654         if (status) {
2655             return status;
2656         }
2657 
2658         if (prinfo & NVME_PRINFO_PRACT) {
2659             return NVME_INVALID_PROT_INFO | NVME_DNR;
2660         }
2661     }
2662 
2663     if (len > n->page_size << n->params.vsl) {
2664         return NVME_INVALID_FIELD | NVME_DNR;
2665     }
2666 
2667     status = nvme_check_bounds(ns, slba, nlb);
2668     if (status) {
2669         return status;
2670     }
2671 
2672     if (NVME_ERR_REC_DULBE(ns->features.err_rec)) {
2673         status = nvme_check_dulbe(ns, slba, nlb);
2674         if (status) {
2675             return status;
2676         }
2677     }
2678 
2679     ctx = g_new0(NvmeBounceContext, 1);
2680     ctx->req = req;
2681 
2682     ctx->data.bounce = g_malloc(len);
2683 
2684     qemu_iovec_init(&ctx->data.iov, 1);
2685     qemu_iovec_add(&ctx->data.iov, ctx->data.bounce, len);
2686 
2687     block_acct_start(blk_get_stats(blk), &req->acct, ctx->data.iov.size,
2688                      BLOCK_ACCT_READ);
2689 
2690     req->aiocb = blk_aio_preadv(ns->blkconf.blk, offset, &ctx->data.iov, 0,
2691                                 nvme_verify_mdata_in_cb, ctx);
2692     return NVME_NO_COMPLETE;
2693 }
2694 
2695 typedef struct NvmeCopyAIOCB {
2696     BlockAIOCB common;
2697     BlockAIOCB *aiocb;
2698     NvmeRequest *req;
2699     int ret;
2700 
2701     void *ranges;
2702     unsigned int format;
2703     int nr;
2704     int idx;
2705 
2706     uint8_t *bounce;
2707     QEMUIOVector iov;
2708     struct {
2709         BlockAcctCookie read;
2710         BlockAcctCookie write;
2711     } acct;
2712 
2713     uint64_t reftag;
2714     uint64_t slba;
2715 
2716     NvmeZone *zone;
2717 } NvmeCopyAIOCB;
2718 
2719 static void nvme_copy_cancel(BlockAIOCB *aiocb)
2720 {
2721     NvmeCopyAIOCB *iocb = container_of(aiocb, NvmeCopyAIOCB, common);
2722 
2723     iocb->ret = -ECANCELED;
2724 
2725     if (iocb->aiocb) {
2726         blk_aio_cancel_async(iocb->aiocb);
2727         iocb->aiocb = NULL;
2728     }
2729 }
2730 
2731 static const AIOCBInfo nvme_copy_aiocb_info = {
2732     .aiocb_size   = sizeof(NvmeCopyAIOCB),
2733     .cancel_async = nvme_copy_cancel,
2734 };
2735 
2736 static void nvme_copy_done(NvmeCopyAIOCB *iocb)
2737 {
2738     NvmeRequest *req = iocb->req;
2739     NvmeNamespace *ns = req->ns;
2740     BlockAcctStats *stats = blk_get_stats(ns->blkconf.blk);
2741 
2742     if (iocb->idx != iocb->nr) {
2743         req->cqe.result = cpu_to_le32(iocb->idx);
2744     }
2745 
2746     qemu_iovec_destroy(&iocb->iov);
2747     g_free(iocb->bounce);
2748 
2749     if (iocb->ret < 0) {
2750         block_acct_failed(stats, &iocb->acct.read);
2751         block_acct_failed(stats, &iocb->acct.write);
2752     } else {
2753         block_acct_done(stats, &iocb->acct.read);
2754         block_acct_done(stats, &iocb->acct.write);
2755     }
2756 
2757     iocb->common.cb(iocb->common.opaque, iocb->ret);
2758     qemu_aio_unref(iocb);
2759 }
2760 
2761 static void nvme_do_copy(NvmeCopyAIOCB *iocb);
2762 
2763 static void nvme_copy_source_range_parse_format0(void *ranges, int idx,
2764                                                  uint64_t *slba, uint32_t *nlb,
2765                                                  uint16_t *apptag,
2766                                                  uint16_t *appmask,
2767                                                  uint64_t *reftag)
2768 {
2769     NvmeCopySourceRangeFormat0 *_ranges = ranges;
2770 
2771     if (slba) {
2772         *slba = le64_to_cpu(_ranges[idx].slba);
2773     }
2774 
2775     if (nlb) {
2776         *nlb = le16_to_cpu(_ranges[idx].nlb) + 1;
2777     }
2778 
2779     if (apptag) {
2780         *apptag = le16_to_cpu(_ranges[idx].apptag);
2781     }
2782 
2783     if (appmask) {
2784         *appmask = le16_to_cpu(_ranges[idx].appmask);
2785     }
2786 
2787     if (reftag) {
2788         *reftag = le32_to_cpu(_ranges[idx].reftag);
2789     }
2790 }
2791 
2792 static void nvme_copy_source_range_parse_format1(void *ranges, int idx,
2793                                                  uint64_t *slba, uint32_t *nlb,
2794                                                  uint16_t *apptag,
2795                                                  uint16_t *appmask,
2796                                                  uint64_t *reftag)
2797 {
2798     NvmeCopySourceRangeFormat1 *_ranges = ranges;
2799 
2800     if (slba) {
2801         *slba = le64_to_cpu(_ranges[idx].slba);
2802     }
2803 
2804     if (nlb) {
2805         *nlb = le16_to_cpu(_ranges[idx].nlb) + 1;
2806     }
2807 
2808     if (apptag) {
2809         *apptag = le16_to_cpu(_ranges[idx].apptag);
2810     }
2811 
2812     if (appmask) {
2813         *appmask = le16_to_cpu(_ranges[idx].appmask);
2814     }
2815 
2816     if (reftag) {
2817         *reftag = 0;
2818 
2819         *reftag |= (uint64_t)_ranges[idx].sr[4] << 40;
2820         *reftag |= (uint64_t)_ranges[idx].sr[5] << 32;
2821         *reftag |= (uint64_t)_ranges[idx].sr[6] << 24;
2822         *reftag |= (uint64_t)_ranges[idx].sr[7] << 16;
2823         *reftag |= (uint64_t)_ranges[idx].sr[8] << 8;
2824         *reftag |= (uint64_t)_ranges[idx].sr[9];
2825     }
2826 }
2827 
2828 static void nvme_copy_source_range_parse(void *ranges, int idx, uint8_t format,
2829                                          uint64_t *slba, uint32_t *nlb,
2830                                          uint16_t *apptag, uint16_t *appmask,
2831                                          uint64_t *reftag)
2832 {
2833     switch (format) {
2834     case NVME_COPY_FORMAT_0:
2835         nvme_copy_source_range_parse_format0(ranges, idx, slba, nlb, apptag,
2836                                              appmask, reftag);
2837         break;
2838 
2839     case NVME_COPY_FORMAT_1:
2840         nvme_copy_source_range_parse_format1(ranges, idx, slba, nlb, apptag,
2841                                              appmask, reftag);
2842         break;
2843 
2844     default:
2845         abort();
2846     }
2847 }
2848 
2849 static inline uint16_t nvme_check_copy_mcl(NvmeNamespace *ns,
2850                                            NvmeCopyAIOCB *iocb, uint16_t nr)
2851 {
2852     uint32_t copy_len = 0;
2853 
2854     for (int idx = 0; idx < nr; idx++) {
2855         uint32_t nlb;
2856         nvme_copy_source_range_parse(iocb->ranges, idx, iocb->format, NULL,
2857                                      &nlb, NULL, NULL, NULL);
2858         copy_len += nlb;
2859     }
2860 
2861     if (copy_len > ns->id_ns.mcl) {
2862         return NVME_CMD_SIZE_LIMIT | NVME_DNR;
2863     }
2864 
2865     return NVME_SUCCESS;
2866 }
2867 
2868 static void nvme_copy_out_completed_cb(void *opaque, int ret)
2869 {
2870     NvmeCopyAIOCB *iocb = opaque;
2871     NvmeRequest *req = iocb->req;
2872     NvmeNamespace *ns = req->ns;
2873     uint32_t nlb;
2874 
2875     nvme_copy_source_range_parse(iocb->ranges, iocb->idx, iocb->format, NULL,
2876                                  &nlb, NULL, NULL, NULL);
2877 
2878     if (ret < 0) {
2879         iocb->ret = ret;
2880         goto out;
2881     } else if (iocb->ret < 0) {
2882         goto out;
2883     }
2884 
2885     if (ns->params.zoned) {
2886         nvme_advance_zone_wp(ns, iocb->zone, nlb);
2887     }
2888 
2889     iocb->idx++;
2890     iocb->slba += nlb;
2891 out:
2892     nvme_do_copy(iocb);
2893 }
2894 
2895 static void nvme_copy_out_cb(void *opaque, int ret)
2896 {
2897     NvmeCopyAIOCB *iocb = opaque;
2898     NvmeRequest *req = iocb->req;
2899     NvmeNamespace *ns = req->ns;
2900     uint32_t nlb;
2901     size_t mlen;
2902     uint8_t *mbounce;
2903 
2904     if (ret < 0 || iocb->ret < 0 || !ns->lbaf.ms) {
2905         goto out;
2906     }
2907 
2908     nvme_copy_source_range_parse(iocb->ranges, iocb->idx, iocb->format, NULL,
2909                                  &nlb, NULL, NULL, NULL);
2910 
2911     mlen = nvme_m2b(ns, nlb);
2912     mbounce = iocb->bounce + nvme_l2b(ns, nlb);
2913 
2914     qemu_iovec_reset(&iocb->iov);
2915     qemu_iovec_add(&iocb->iov, mbounce, mlen);
2916 
2917     iocb->aiocb = blk_aio_pwritev(ns->blkconf.blk, nvme_moff(ns, iocb->slba),
2918                                   &iocb->iov, 0, nvme_copy_out_completed_cb,
2919                                   iocb);
2920 
2921     return;
2922 
2923 out:
2924     nvme_copy_out_completed_cb(iocb, ret);
2925 }
2926 
2927 static void nvme_copy_in_completed_cb(void *opaque, int ret)
2928 {
2929     NvmeCopyAIOCB *iocb = opaque;
2930     NvmeRequest *req = iocb->req;
2931     NvmeNamespace *ns = req->ns;
2932     uint32_t nlb;
2933     uint64_t slba;
2934     uint16_t apptag, appmask;
2935     uint64_t reftag;
2936     size_t len;
2937     uint16_t status;
2938 
2939     if (ret < 0) {
2940         iocb->ret = ret;
2941         goto out;
2942     } else if (iocb->ret < 0) {
2943         goto out;
2944     }
2945 
2946     nvme_copy_source_range_parse(iocb->ranges, iocb->idx, iocb->format, &slba,
2947                                  &nlb, &apptag, &appmask, &reftag);
2948     len = nvme_l2b(ns, nlb);
2949 
2950     trace_pci_nvme_copy_out(iocb->slba, nlb);
2951 
2952     if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
2953         NvmeCopyCmd *copy = (NvmeCopyCmd *)&req->cmd;
2954 
2955         uint16_t prinfor = ((copy->control[0] >> 4) & 0xf);
2956         uint16_t prinfow = ((copy->control[2] >> 2) & 0xf);
2957 
2958         size_t mlen = nvme_m2b(ns, nlb);
2959         uint8_t *mbounce = iocb->bounce + nvme_l2b(ns, nlb);
2960 
2961         status = nvme_dif_mangle_mdata(ns, mbounce, mlen, slba);
2962         if (status) {
2963             goto invalid;
2964         }
2965         status = nvme_dif_check(ns, iocb->bounce, len, mbounce, mlen, prinfor,
2966                                 slba, apptag, appmask, &reftag);
2967         if (status) {
2968             goto invalid;
2969         }
2970 
2971         apptag = le16_to_cpu(copy->apptag);
2972         appmask = le16_to_cpu(copy->appmask);
2973 
2974         if (prinfow & NVME_PRINFO_PRACT) {
2975             status = nvme_check_prinfo(ns, prinfow, iocb->slba, iocb->reftag);
2976             if (status) {
2977                 goto invalid;
2978             }
2979 
2980             nvme_dif_pract_generate_dif(ns, iocb->bounce, len, mbounce, mlen,
2981                                         apptag, &iocb->reftag);
2982         } else {
2983             status = nvme_dif_check(ns, iocb->bounce, len, mbounce, mlen,
2984                                     prinfow, iocb->slba, apptag, appmask,
2985                                     &iocb->reftag);
2986             if (status) {
2987                 goto invalid;
2988             }
2989         }
2990     }
2991 
2992     status = nvme_check_bounds(ns, iocb->slba, nlb);
2993     if (status) {
2994         goto invalid;
2995     }
2996 
2997     if (ns->params.zoned) {
2998         status = nvme_check_zone_write(ns, iocb->zone, iocb->slba, nlb);
2999         if (status) {
3000             goto invalid;
3001         }
3002 
3003         if (!(iocb->zone->d.za & NVME_ZA_ZRWA_VALID)) {
3004             iocb->zone->w_ptr += nlb;
3005         }
3006     }
3007 
3008     qemu_iovec_reset(&iocb->iov);
3009     qemu_iovec_add(&iocb->iov, iocb->bounce, len);
3010 
3011     iocb->aiocb = blk_aio_pwritev(ns->blkconf.blk, nvme_l2b(ns, iocb->slba),
3012                                   &iocb->iov, 0, nvme_copy_out_cb, iocb);
3013 
3014     return;
3015 
3016 invalid:
3017     req->status = status;
3018     iocb->ret = -1;
3019 out:
3020     nvme_do_copy(iocb);
3021 }
3022 
3023 static void nvme_copy_in_cb(void *opaque, int ret)
3024 {
3025     NvmeCopyAIOCB *iocb = opaque;
3026     NvmeRequest *req = iocb->req;
3027     NvmeNamespace *ns = req->ns;
3028     uint64_t slba;
3029     uint32_t nlb;
3030 
3031     if (ret < 0 || iocb->ret < 0 || !ns->lbaf.ms) {
3032         goto out;
3033     }
3034 
3035     nvme_copy_source_range_parse(iocb->ranges, iocb->idx, iocb->format, &slba,
3036                                  &nlb, NULL, NULL, NULL);
3037 
3038     qemu_iovec_reset(&iocb->iov);
3039     qemu_iovec_add(&iocb->iov, iocb->bounce + nvme_l2b(ns, nlb),
3040                    nvme_m2b(ns, nlb));
3041 
3042     iocb->aiocb = blk_aio_preadv(ns->blkconf.blk, nvme_moff(ns, slba),
3043                                  &iocb->iov, 0, nvme_copy_in_completed_cb,
3044                                  iocb);
3045     return;
3046 
3047 out:
3048     nvme_copy_in_completed_cb(iocb, ret);
3049 }
3050 
3051 static void nvme_do_copy(NvmeCopyAIOCB *iocb)
3052 {
3053     NvmeRequest *req = iocb->req;
3054     NvmeNamespace *ns = req->ns;
3055     uint64_t slba;
3056     uint32_t nlb;
3057     size_t len;
3058     uint16_t status;
3059 
3060     if (iocb->ret < 0) {
3061         goto done;
3062     }
3063 
3064     if (iocb->idx == iocb->nr) {
3065         goto done;
3066     }
3067 
3068     nvme_copy_source_range_parse(iocb->ranges, iocb->idx, iocb->format, &slba,
3069                                  &nlb, NULL, NULL, NULL);
3070     len = nvme_l2b(ns, nlb);
3071 
3072     trace_pci_nvme_copy_source_range(slba, nlb);
3073 
3074     if (nlb > le16_to_cpu(ns->id_ns.mssrl)) {
3075         status = NVME_CMD_SIZE_LIMIT | NVME_DNR;
3076         goto invalid;
3077     }
3078 
3079     status = nvme_check_bounds(ns, slba, nlb);
3080     if (status) {
3081         goto invalid;
3082     }
3083 
3084     if (NVME_ERR_REC_DULBE(ns->features.err_rec)) {
3085         status = nvme_check_dulbe(ns, slba, nlb);
3086         if (status) {
3087             goto invalid;
3088         }
3089     }
3090 
3091     if (ns->params.zoned) {
3092         status = nvme_check_zone_read(ns, slba, nlb);
3093         if (status) {
3094             goto invalid;
3095         }
3096     }
3097 
3098     qemu_iovec_reset(&iocb->iov);
3099     qemu_iovec_add(&iocb->iov, iocb->bounce, len);
3100 
3101     iocb->aiocb = blk_aio_preadv(ns->blkconf.blk, nvme_l2b(ns, slba),
3102                                  &iocb->iov, 0, nvme_copy_in_cb, iocb);
3103     return;
3104 
3105 invalid:
3106     req->status = status;
3107     iocb->ret = -1;
3108 done:
3109     nvme_copy_done(iocb);
3110 }
3111 
3112 static uint16_t nvme_copy(NvmeCtrl *n, NvmeRequest *req)
3113 {
3114     NvmeNamespace *ns = req->ns;
3115     NvmeCopyCmd *copy = (NvmeCopyCmd *)&req->cmd;
3116     NvmeCopyAIOCB *iocb = blk_aio_get(&nvme_copy_aiocb_info, ns->blkconf.blk,
3117                                       nvme_misc_cb, req);
3118     uint16_t nr = copy->nr + 1;
3119     uint8_t format = copy->control[0] & 0xf;
3120     uint16_t prinfor = ((copy->control[0] >> 4) & 0xf);
3121     uint16_t prinfow = ((copy->control[2] >> 2) & 0xf);
3122     size_t len = sizeof(NvmeCopySourceRangeFormat0);
3123 
3124     uint16_t status;
3125 
3126     trace_pci_nvme_copy(nvme_cid(req), nvme_nsid(ns), nr, format);
3127 
3128     iocb->ranges = NULL;
3129     iocb->zone = NULL;
3130 
3131     if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps) &&
3132         ((prinfor & NVME_PRINFO_PRACT) != (prinfow & NVME_PRINFO_PRACT))) {
3133         status = NVME_INVALID_FIELD | NVME_DNR;
3134         goto invalid;
3135     }
3136 
3137     if (!(n->id_ctrl.ocfs & (1 << format))) {
3138         trace_pci_nvme_err_copy_invalid_format(format);
3139         status = NVME_INVALID_FIELD | NVME_DNR;
3140         goto invalid;
3141     }
3142 
3143     if (nr > ns->id_ns.msrc + 1) {
3144         status = NVME_CMD_SIZE_LIMIT | NVME_DNR;
3145         goto invalid;
3146     }
3147 
3148     if ((ns->pif == 0x0 && format != 0x0) ||
3149         (ns->pif != 0x0 && format != 0x1)) {
3150         status = NVME_INVALID_FORMAT | NVME_DNR;
3151         goto invalid;
3152     }
3153 
3154     if (ns->pif) {
3155         len = sizeof(NvmeCopySourceRangeFormat1);
3156     }
3157 
3158     iocb->format = format;
3159     iocb->ranges = g_malloc_n(nr, len);
3160     status = nvme_h2c(n, (uint8_t *)iocb->ranges, len * nr, req);
3161     if (status) {
3162         goto invalid;
3163     }
3164 
3165     iocb->slba = le64_to_cpu(copy->sdlba);
3166 
3167     if (ns->params.zoned) {
3168         iocb->zone = nvme_get_zone_by_slba(ns, iocb->slba);
3169         if (!iocb->zone) {
3170             status = NVME_LBA_RANGE | NVME_DNR;
3171             goto invalid;
3172         }
3173 
3174         status = nvme_zrm_auto(n, ns, iocb->zone);
3175         if (status) {
3176             goto invalid;
3177         }
3178     }
3179 
3180     status = nvme_check_copy_mcl(ns, iocb, nr);
3181     if (status) {
3182         goto invalid;
3183     }
3184 
3185     iocb->req = req;
3186     iocb->ret = 0;
3187     iocb->nr = nr;
3188     iocb->idx = 0;
3189     iocb->reftag = le32_to_cpu(copy->reftag);
3190     iocb->reftag |= (uint64_t)le32_to_cpu(copy->cdw3) << 32;
3191     iocb->bounce = g_malloc_n(le16_to_cpu(ns->id_ns.mssrl),
3192                               ns->lbasz + ns->lbaf.ms);
3193 
3194     qemu_iovec_init(&iocb->iov, 1);
3195 
3196     block_acct_start(blk_get_stats(ns->blkconf.blk), &iocb->acct.read, 0,
3197                      BLOCK_ACCT_READ);
3198     block_acct_start(blk_get_stats(ns->blkconf.blk), &iocb->acct.write, 0,
3199                      BLOCK_ACCT_WRITE);
3200 
3201     req->aiocb = &iocb->common;
3202     nvme_do_copy(iocb);
3203 
3204     return NVME_NO_COMPLETE;
3205 
3206 invalid:
3207     g_free(iocb->ranges);
3208     qemu_aio_unref(iocb);
3209     return status;
3210 }
3211 
3212 static uint16_t nvme_compare(NvmeCtrl *n, NvmeRequest *req)
3213 {
3214     NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
3215     NvmeNamespace *ns = req->ns;
3216     BlockBackend *blk = ns->blkconf.blk;
3217     uint64_t slba = le64_to_cpu(rw->slba);
3218     uint32_t nlb = le16_to_cpu(rw->nlb) + 1;
3219     uint8_t prinfo = NVME_RW_PRINFO(le16_to_cpu(rw->control));
3220     size_t data_len = nvme_l2b(ns, nlb);
3221     size_t len = data_len;
3222     int64_t offset = nvme_l2b(ns, slba);
3223     struct nvme_compare_ctx *ctx = NULL;
3224     uint16_t status;
3225 
3226     trace_pci_nvme_compare(nvme_cid(req), nvme_nsid(ns), slba, nlb);
3227 
3228     if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps) && (prinfo & NVME_PRINFO_PRACT)) {
3229         return NVME_INVALID_PROT_INFO | NVME_DNR;
3230     }
3231 
3232     if (nvme_ns_ext(ns)) {
3233         len += nvme_m2b(ns, nlb);
3234     }
3235 
3236     status = nvme_check_mdts(n, len);
3237     if (status) {
3238         return status;
3239     }
3240 
3241     status = nvme_check_bounds(ns, slba, nlb);
3242     if (status) {
3243         return status;
3244     }
3245 
3246     if (NVME_ERR_REC_DULBE(ns->features.err_rec)) {
3247         status = nvme_check_dulbe(ns, slba, nlb);
3248         if (status) {
3249             return status;
3250         }
3251     }
3252 
3253     status = nvme_map_dptr(n, &req->sg, len, &req->cmd);
3254     if (status) {
3255         return status;
3256     }
3257 
3258     ctx = g_new(struct nvme_compare_ctx, 1);
3259     ctx->data.bounce = g_malloc(data_len);
3260 
3261     req->opaque = ctx;
3262 
3263     qemu_iovec_init(&ctx->data.iov, 1);
3264     qemu_iovec_add(&ctx->data.iov, ctx->data.bounce, data_len);
3265 
3266     block_acct_start(blk_get_stats(blk), &req->acct, data_len,
3267                      BLOCK_ACCT_READ);
3268     req->aiocb = blk_aio_preadv(blk, offset, &ctx->data.iov, 0,
3269                                 nvme_compare_data_cb, req);
3270 
3271     return NVME_NO_COMPLETE;
3272 }
3273 
3274 typedef struct NvmeFlushAIOCB {
3275     BlockAIOCB common;
3276     BlockAIOCB *aiocb;
3277     NvmeRequest *req;
3278     int ret;
3279 
3280     NvmeNamespace *ns;
3281     uint32_t nsid;
3282     bool broadcast;
3283 } NvmeFlushAIOCB;
3284 
3285 static void nvme_flush_cancel(BlockAIOCB *acb)
3286 {
3287     NvmeFlushAIOCB *iocb = container_of(acb, NvmeFlushAIOCB, common);
3288 
3289     iocb->ret = -ECANCELED;
3290 
3291     if (iocb->aiocb) {
3292         blk_aio_cancel_async(iocb->aiocb);
3293         iocb->aiocb = NULL;
3294     }
3295 }
3296 
3297 static const AIOCBInfo nvme_flush_aiocb_info = {
3298     .aiocb_size = sizeof(NvmeFlushAIOCB),
3299     .cancel_async = nvme_flush_cancel,
3300 };
3301 
3302 static void nvme_do_flush(NvmeFlushAIOCB *iocb);
3303 
3304 static void nvme_flush_ns_cb(void *opaque, int ret)
3305 {
3306     NvmeFlushAIOCB *iocb = opaque;
3307     NvmeNamespace *ns = iocb->ns;
3308 
3309     if (ret < 0) {
3310         iocb->ret = ret;
3311         goto out;
3312     } else if (iocb->ret < 0) {
3313         goto out;
3314     }
3315 
3316     if (ns) {
3317         trace_pci_nvme_flush_ns(iocb->nsid);
3318 
3319         iocb->ns = NULL;
3320         iocb->aiocb = blk_aio_flush(ns->blkconf.blk, nvme_flush_ns_cb, iocb);
3321         return;
3322     }
3323 
3324 out:
3325     nvme_do_flush(iocb);
3326 }
3327 
3328 static void nvme_do_flush(NvmeFlushAIOCB *iocb)
3329 {
3330     NvmeRequest *req = iocb->req;
3331     NvmeCtrl *n = nvme_ctrl(req);
3332     int i;
3333 
3334     if (iocb->ret < 0) {
3335         goto done;
3336     }
3337 
3338     if (iocb->broadcast) {
3339         for (i = iocb->nsid + 1; i <= NVME_MAX_NAMESPACES; i++) {
3340             iocb->ns = nvme_ns(n, i);
3341             if (iocb->ns) {
3342                 iocb->nsid = i;
3343                 break;
3344             }
3345         }
3346     }
3347 
3348     if (!iocb->ns) {
3349         goto done;
3350     }
3351 
3352     nvme_flush_ns_cb(iocb, 0);
3353     return;
3354 
3355 done:
3356     iocb->common.cb(iocb->common.opaque, iocb->ret);
3357     qemu_aio_unref(iocb);
3358 }
3359 
3360 static uint16_t nvme_flush(NvmeCtrl *n, NvmeRequest *req)
3361 {
3362     NvmeFlushAIOCB *iocb;
3363     uint32_t nsid = le32_to_cpu(req->cmd.nsid);
3364     uint16_t status;
3365 
3366     iocb = qemu_aio_get(&nvme_flush_aiocb_info, NULL, nvme_misc_cb, req);
3367 
3368     iocb->req = req;
3369     iocb->ret = 0;
3370     iocb->ns = NULL;
3371     iocb->nsid = 0;
3372     iocb->broadcast = (nsid == NVME_NSID_BROADCAST);
3373 
3374     if (!iocb->broadcast) {
3375         if (!nvme_nsid_valid(n, nsid)) {
3376             status = NVME_INVALID_NSID | NVME_DNR;
3377             goto out;
3378         }
3379 
3380         iocb->ns = nvme_ns(n, nsid);
3381         if (!iocb->ns) {
3382             status = NVME_INVALID_FIELD | NVME_DNR;
3383             goto out;
3384         }
3385 
3386         iocb->nsid = nsid;
3387     }
3388 
3389     req->aiocb = &iocb->common;
3390     nvme_do_flush(iocb);
3391 
3392     return NVME_NO_COMPLETE;
3393 
3394 out:
3395     qemu_aio_unref(iocb);
3396 
3397     return status;
3398 }
3399 
3400 static uint16_t nvme_read(NvmeCtrl *n, NvmeRequest *req)
3401 {
3402     NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
3403     NvmeNamespace *ns = req->ns;
3404     uint64_t slba = le64_to_cpu(rw->slba);
3405     uint32_t nlb = (uint32_t)le16_to_cpu(rw->nlb) + 1;
3406     uint8_t prinfo = NVME_RW_PRINFO(le16_to_cpu(rw->control));
3407     uint64_t data_size = nvme_l2b(ns, nlb);
3408     uint64_t mapped_size = data_size;
3409     uint64_t data_offset;
3410     BlockBackend *blk = ns->blkconf.blk;
3411     uint16_t status;
3412 
3413     if (nvme_ns_ext(ns)) {
3414         mapped_size += nvme_m2b(ns, nlb);
3415 
3416         if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
3417             bool pract = prinfo & NVME_PRINFO_PRACT;
3418 
3419             if (pract && ns->lbaf.ms == nvme_pi_tuple_size(ns)) {
3420                 mapped_size = data_size;
3421             }
3422         }
3423     }
3424 
3425     trace_pci_nvme_read(nvme_cid(req), nvme_nsid(ns), nlb, mapped_size, slba);
3426 
3427     status = nvme_check_mdts(n, mapped_size);
3428     if (status) {
3429         goto invalid;
3430     }
3431 
3432     status = nvme_check_bounds(ns, slba, nlb);
3433     if (status) {
3434         goto invalid;
3435     }
3436 
3437     if (ns->params.zoned) {
3438         status = nvme_check_zone_read(ns, slba, nlb);
3439         if (status) {
3440             trace_pci_nvme_err_zone_read_not_ok(slba, nlb, status);
3441             goto invalid;
3442         }
3443     }
3444 
3445     if (NVME_ERR_REC_DULBE(ns->features.err_rec)) {
3446         status = nvme_check_dulbe(ns, slba, nlb);
3447         if (status) {
3448             goto invalid;
3449         }
3450     }
3451 
3452     if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
3453         return nvme_dif_rw(n, req);
3454     }
3455 
3456     status = nvme_map_data(n, nlb, req);
3457     if (status) {
3458         goto invalid;
3459     }
3460 
3461     data_offset = nvme_l2b(ns, slba);
3462 
3463     block_acct_start(blk_get_stats(blk), &req->acct, data_size,
3464                      BLOCK_ACCT_READ);
3465     nvme_blk_read(blk, data_offset, BDRV_SECTOR_SIZE, nvme_rw_cb, req);
3466     return NVME_NO_COMPLETE;
3467 
3468 invalid:
3469     block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
3470     return status | NVME_DNR;
3471 }
3472 
3473 static void nvme_do_write_fdp(NvmeCtrl *n, NvmeRequest *req, uint64_t slba,
3474                               uint32_t nlb)
3475 {
3476     NvmeNamespace *ns = req->ns;
3477     NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
3478     uint64_t data_size = nvme_l2b(ns, nlb);
3479     uint32_t dw12 = le32_to_cpu(req->cmd.cdw12);
3480     uint8_t dtype = (dw12 >> 20) & 0xf;
3481     uint16_t pid = le16_to_cpu(rw->dspec);
3482     uint16_t ph, rg, ruhid;
3483     NvmeReclaimUnit *ru;
3484 
3485     if (dtype != NVME_DIRECTIVE_DATA_PLACEMENT ||
3486         !nvme_parse_pid(ns, pid, &ph, &rg)) {
3487         ph = 0;
3488         rg = 0;
3489     }
3490 
3491     ruhid = ns->fdp.phs[ph];
3492     ru = &ns->endgrp->fdp.ruhs[ruhid].rus[rg];
3493 
3494     nvme_fdp_stat_inc(&ns->endgrp->fdp.hbmw, data_size);
3495     nvme_fdp_stat_inc(&ns->endgrp->fdp.mbmw, data_size);
3496 
3497     while (nlb) {
3498         if (nlb < ru->ruamw) {
3499             ru->ruamw -= nlb;
3500             break;
3501         }
3502 
3503         nlb -= ru->ruamw;
3504         nvme_update_ruh(n, ns, pid);
3505     }
3506 }
3507 
3508 static uint16_t nvme_do_write(NvmeCtrl *n, NvmeRequest *req, bool append,
3509                               bool wrz)
3510 {
3511     NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
3512     NvmeNamespace *ns = req->ns;
3513     uint64_t slba = le64_to_cpu(rw->slba);
3514     uint32_t nlb = (uint32_t)le16_to_cpu(rw->nlb) + 1;
3515     uint16_t ctrl = le16_to_cpu(rw->control);
3516     uint8_t prinfo = NVME_RW_PRINFO(ctrl);
3517     uint64_t data_size = nvme_l2b(ns, nlb);
3518     uint64_t mapped_size = data_size;
3519     uint64_t data_offset;
3520     NvmeZone *zone;
3521     NvmeZonedResult *res = (NvmeZonedResult *)&req->cqe;
3522     BlockBackend *blk = ns->blkconf.blk;
3523     uint16_t status;
3524 
3525     if (nvme_ns_ext(ns)) {
3526         mapped_size += nvme_m2b(ns, nlb);
3527 
3528         if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
3529             bool pract = prinfo & NVME_PRINFO_PRACT;
3530 
3531             if (pract && ns->lbaf.ms == nvme_pi_tuple_size(ns)) {
3532                 mapped_size -= nvme_m2b(ns, nlb);
3533             }
3534         }
3535     }
3536 
3537     trace_pci_nvme_write(nvme_cid(req), nvme_io_opc_str(rw->opcode),
3538                          nvme_nsid(ns), nlb, mapped_size, slba);
3539 
3540     if (!wrz) {
3541         status = nvme_check_mdts(n, mapped_size);
3542         if (status) {
3543             goto invalid;
3544         }
3545     }
3546 
3547     status = nvme_check_bounds(ns, slba, nlb);
3548     if (status) {
3549         goto invalid;
3550     }
3551 
3552     if (ns->params.zoned) {
3553         zone = nvme_get_zone_by_slba(ns, slba);
3554         assert(zone);
3555 
3556         if (append) {
3557             bool piremap = !!(ctrl & NVME_RW_PIREMAP);
3558 
3559             if (unlikely(zone->d.za & NVME_ZA_ZRWA_VALID)) {
3560                 return NVME_INVALID_ZONE_OP | NVME_DNR;
3561             }
3562 
3563             if (unlikely(slba != zone->d.zslba)) {
3564                 trace_pci_nvme_err_append_not_at_start(slba, zone->d.zslba);
3565                 status = NVME_INVALID_FIELD;
3566                 goto invalid;
3567             }
3568 
3569             if (n->params.zasl &&
3570                 data_size > (uint64_t)n->page_size << n->params.zasl) {
3571                 trace_pci_nvme_err_zasl(data_size);
3572                 return NVME_INVALID_FIELD | NVME_DNR;
3573             }
3574 
3575             slba = zone->w_ptr;
3576             rw->slba = cpu_to_le64(slba);
3577             res->slba = cpu_to_le64(slba);
3578 
3579             switch (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
3580             case NVME_ID_NS_DPS_TYPE_1:
3581                 if (!piremap) {
3582                     return NVME_INVALID_PROT_INFO | NVME_DNR;
3583                 }
3584 
3585                 /* fallthrough */
3586 
3587             case NVME_ID_NS_DPS_TYPE_2:
3588                 if (piremap) {
3589                     uint32_t reftag = le32_to_cpu(rw->reftag);
3590                     rw->reftag = cpu_to_le32(reftag + (slba - zone->d.zslba));
3591                 }
3592 
3593                 break;
3594 
3595             case NVME_ID_NS_DPS_TYPE_3:
3596                 if (piremap) {
3597                     return NVME_INVALID_PROT_INFO | NVME_DNR;
3598                 }
3599 
3600                 break;
3601             }
3602         }
3603 
3604         status = nvme_check_zone_write(ns, zone, slba, nlb);
3605         if (status) {
3606             goto invalid;
3607         }
3608 
3609         status = nvme_zrm_auto(n, ns, zone);
3610         if (status) {
3611             goto invalid;
3612         }
3613 
3614         if (!(zone->d.za & NVME_ZA_ZRWA_VALID)) {
3615             zone->w_ptr += nlb;
3616         }
3617     } else if (ns->endgrp && ns->endgrp->fdp.enabled) {
3618         nvme_do_write_fdp(n, req, slba, nlb);
3619     }
3620 
3621     data_offset = nvme_l2b(ns, slba);
3622 
3623     if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
3624         return nvme_dif_rw(n, req);
3625     }
3626 
3627     if (!wrz) {
3628         status = nvme_map_data(n, nlb, req);
3629         if (status) {
3630             goto invalid;
3631         }
3632 
3633         block_acct_start(blk_get_stats(blk), &req->acct, data_size,
3634                          BLOCK_ACCT_WRITE);
3635         nvme_blk_write(blk, data_offset, BDRV_SECTOR_SIZE, nvme_rw_cb, req);
3636     } else {
3637         req->aiocb = blk_aio_pwrite_zeroes(blk, data_offset, data_size,
3638                                            BDRV_REQ_MAY_UNMAP, nvme_rw_cb,
3639                                            req);
3640     }
3641 
3642     return NVME_NO_COMPLETE;
3643 
3644 invalid:
3645     block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
3646     return status | NVME_DNR;
3647 }
3648 
3649 static inline uint16_t nvme_write(NvmeCtrl *n, NvmeRequest *req)
3650 {
3651     return nvme_do_write(n, req, false, false);
3652 }
3653 
3654 static inline uint16_t nvme_write_zeroes(NvmeCtrl *n, NvmeRequest *req)
3655 {
3656     return nvme_do_write(n, req, false, true);
3657 }
3658 
3659 static inline uint16_t nvme_zone_append(NvmeCtrl *n, NvmeRequest *req)
3660 {
3661     return nvme_do_write(n, req, true, false);
3662 }
3663 
3664 static uint16_t nvme_get_mgmt_zone_slba_idx(NvmeNamespace *ns, NvmeCmd *c,
3665                                             uint64_t *slba, uint32_t *zone_idx)
3666 {
3667     uint32_t dw10 = le32_to_cpu(c->cdw10);
3668     uint32_t dw11 = le32_to_cpu(c->cdw11);
3669 
3670     if (!ns->params.zoned) {
3671         trace_pci_nvme_err_invalid_opc(c->opcode);
3672         return NVME_INVALID_OPCODE | NVME_DNR;
3673     }
3674 
3675     *slba = ((uint64_t)dw11) << 32 | dw10;
3676     if (unlikely(*slba >= ns->id_ns.nsze)) {
3677         trace_pci_nvme_err_invalid_lba_range(*slba, 0, ns->id_ns.nsze);
3678         *slba = 0;
3679         return NVME_LBA_RANGE | NVME_DNR;
3680     }
3681 
3682     *zone_idx = nvme_zone_idx(ns, *slba);
3683     assert(*zone_idx < ns->num_zones);
3684 
3685     return NVME_SUCCESS;
3686 }
3687 
3688 typedef uint16_t (*op_handler_t)(NvmeNamespace *, NvmeZone *, NvmeZoneState,
3689                                  NvmeRequest *);
3690 
3691 enum NvmeZoneProcessingMask {
3692     NVME_PROC_CURRENT_ZONE    = 0,
3693     NVME_PROC_OPENED_ZONES    = 1 << 0,
3694     NVME_PROC_CLOSED_ZONES    = 1 << 1,
3695     NVME_PROC_READ_ONLY_ZONES = 1 << 2,
3696     NVME_PROC_FULL_ZONES      = 1 << 3,
3697 };
3698 
3699 static uint16_t nvme_open_zone(NvmeNamespace *ns, NvmeZone *zone,
3700                                NvmeZoneState state, NvmeRequest *req)
3701 {
3702     NvmeZoneSendCmd *cmd = (NvmeZoneSendCmd *)&req->cmd;
3703     int flags = 0;
3704 
3705     if (cmd->zsflags & NVME_ZSFLAG_ZRWA_ALLOC) {
3706         uint16_t ozcs = le16_to_cpu(ns->id_ns_zoned->ozcs);
3707 
3708         if (!(ozcs & NVME_ID_NS_ZONED_OZCS_ZRWASUP)) {
3709             return NVME_INVALID_ZONE_OP | NVME_DNR;
3710         }
3711 
3712         if (zone->w_ptr % ns->zns.zrwafg) {
3713             return NVME_NOZRWA | NVME_DNR;
3714         }
3715 
3716         flags = NVME_ZRM_ZRWA;
3717     }
3718 
3719     return nvme_zrm_open_flags(nvme_ctrl(req), ns, zone, flags);
3720 }
3721 
3722 static uint16_t nvme_close_zone(NvmeNamespace *ns, NvmeZone *zone,
3723                                 NvmeZoneState state, NvmeRequest *req)
3724 {
3725     return nvme_zrm_close(ns, zone);
3726 }
3727 
3728 static uint16_t nvme_finish_zone(NvmeNamespace *ns, NvmeZone *zone,
3729                                  NvmeZoneState state, NvmeRequest *req)
3730 {
3731     return nvme_zrm_finish(ns, zone);
3732 }
3733 
3734 static uint16_t nvme_offline_zone(NvmeNamespace *ns, NvmeZone *zone,
3735                                   NvmeZoneState state, NvmeRequest *req)
3736 {
3737     switch (state) {
3738     case NVME_ZONE_STATE_READ_ONLY:
3739         nvme_assign_zone_state(ns, zone, NVME_ZONE_STATE_OFFLINE);
3740         /* fall through */
3741     case NVME_ZONE_STATE_OFFLINE:
3742         return NVME_SUCCESS;
3743     default:
3744         return NVME_ZONE_INVAL_TRANSITION;
3745     }
3746 }
3747 
3748 static uint16_t nvme_set_zd_ext(NvmeNamespace *ns, NvmeZone *zone)
3749 {
3750     uint16_t status;
3751     uint8_t state = nvme_get_zone_state(zone);
3752 
3753     if (state == NVME_ZONE_STATE_EMPTY) {
3754         status = nvme_aor_check(ns, 1, 0);
3755         if (status) {
3756             return status;
3757         }
3758         nvme_aor_inc_active(ns);
3759         zone->d.za |= NVME_ZA_ZD_EXT_VALID;
3760         nvme_assign_zone_state(ns, zone, NVME_ZONE_STATE_CLOSED);
3761         return NVME_SUCCESS;
3762     }
3763 
3764     return NVME_ZONE_INVAL_TRANSITION;
3765 }
3766 
3767 static uint16_t nvme_bulk_proc_zone(NvmeNamespace *ns, NvmeZone *zone,
3768                                     enum NvmeZoneProcessingMask proc_mask,
3769                                     op_handler_t op_hndlr, NvmeRequest *req)
3770 {
3771     uint16_t status = NVME_SUCCESS;
3772     NvmeZoneState zs = nvme_get_zone_state(zone);
3773     bool proc_zone;
3774 
3775     switch (zs) {
3776     case NVME_ZONE_STATE_IMPLICITLY_OPEN:
3777     case NVME_ZONE_STATE_EXPLICITLY_OPEN:
3778         proc_zone = proc_mask & NVME_PROC_OPENED_ZONES;
3779         break;
3780     case NVME_ZONE_STATE_CLOSED:
3781         proc_zone = proc_mask & NVME_PROC_CLOSED_ZONES;
3782         break;
3783     case NVME_ZONE_STATE_READ_ONLY:
3784         proc_zone = proc_mask & NVME_PROC_READ_ONLY_ZONES;
3785         break;
3786     case NVME_ZONE_STATE_FULL:
3787         proc_zone = proc_mask & NVME_PROC_FULL_ZONES;
3788         break;
3789     default:
3790         proc_zone = false;
3791     }
3792 
3793     if (proc_zone) {
3794         status = op_hndlr(ns, zone, zs, req);
3795     }
3796 
3797     return status;
3798 }
3799 
3800 static uint16_t nvme_do_zone_op(NvmeNamespace *ns, NvmeZone *zone,
3801                                 enum NvmeZoneProcessingMask proc_mask,
3802                                 op_handler_t op_hndlr, NvmeRequest *req)
3803 {
3804     NvmeZone *next;
3805     uint16_t status = NVME_SUCCESS;
3806     int i;
3807 
3808     if (!proc_mask) {
3809         status = op_hndlr(ns, zone, nvme_get_zone_state(zone), req);
3810     } else {
3811         if (proc_mask & NVME_PROC_CLOSED_ZONES) {
3812             QTAILQ_FOREACH_SAFE(zone, &ns->closed_zones, entry, next) {
3813                 status = nvme_bulk_proc_zone(ns, zone, proc_mask, op_hndlr,
3814                                              req);
3815                 if (status && status != NVME_NO_COMPLETE) {
3816                     goto out;
3817                 }
3818             }
3819         }
3820         if (proc_mask & NVME_PROC_OPENED_ZONES) {
3821             QTAILQ_FOREACH_SAFE(zone, &ns->imp_open_zones, entry, next) {
3822                 status = nvme_bulk_proc_zone(ns, zone, proc_mask, op_hndlr,
3823                                              req);
3824                 if (status && status != NVME_NO_COMPLETE) {
3825                     goto out;
3826                 }
3827             }
3828 
3829             QTAILQ_FOREACH_SAFE(zone, &ns->exp_open_zones, entry, next) {
3830                 status = nvme_bulk_proc_zone(ns, zone, proc_mask, op_hndlr,
3831                                              req);
3832                 if (status && status != NVME_NO_COMPLETE) {
3833                     goto out;
3834                 }
3835             }
3836         }
3837         if (proc_mask & NVME_PROC_FULL_ZONES) {
3838             QTAILQ_FOREACH_SAFE(zone, &ns->full_zones, entry, next) {
3839                 status = nvme_bulk_proc_zone(ns, zone, proc_mask, op_hndlr,
3840                                              req);
3841                 if (status && status != NVME_NO_COMPLETE) {
3842                     goto out;
3843                 }
3844             }
3845         }
3846 
3847         if (proc_mask & NVME_PROC_READ_ONLY_ZONES) {
3848             for (i = 0; i < ns->num_zones; i++, zone++) {
3849                 status = nvme_bulk_proc_zone(ns, zone, proc_mask, op_hndlr,
3850                                              req);
3851                 if (status && status != NVME_NO_COMPLETE) {
3852                     goto out;
3853                 }
3854             }
3855         }
3856     }
3857 
3858 out:
3859     return status;
3860 }
3861 
3862 typedef struct NvmeZoneResetAIOCB {
3863     BlockAIOCB common;
3864     BlockAIOCB *aiocb;
3865     NvmeRequest *req;
3866     int ret;
3867 
3868     bool all;
3869     int idx;
3870     NvmeZone *zone;
3871 } NvmeZoneResetAIOCB;
3872 
3873 static void nvme_zone_reset_cancel(BlockAIOCB *aiocb)
3874 {
3875     NvmeZoneResetAIOCB *iocb = container_of(aiocb, NvmeZoneResetAIOCB, common);
3876     NvmeRequest *req = iocb->req;
3877     NvmeNamespace *ns = req->ns;
3878 
3879     iocb->idx = ns->num_zones;
3880 
3881     iocb->ret = -ECANCELED;
3882 
3883     if (iocb->aiocb) {
3884         blk_aio_cancel_async(iocb->aiocb);
3885         iocb->aiocb = NULL;
3886     }
3887 }
3888 
3889 static const AIOCBInfo nvme_zone_reset_aiocb_info = {
3890     .aiocb_size = sizeof(NvmeZoneResetAIOCB),
3891     .cancel_async = nvme_zone_reset_cancel,
3892 };
3893 
3894 static void nvme_zone_reset_cb(void *opaque, int ret);
3895 
3896 static void nvme_zone_reset_epilogue_cb(void *opaque, int ret)
3897 {
3898     NvmeZoneResetAIOCB *iocb = opaque;
3899     NvmeRequest *req = iocb->req;
3900     NvmeNamespace *ns = req->ns;
3901     int64_t moff;
3902     int count;
3903 
3904     if (ret < 0 || iocb->ret < 0 || !ns->lbaf.ms) {
3905         goto out;
3906     }
3907 
3908     moff = nvme_moff(ns, iocb->zone->d.zslba);
3909     count = nvme_m2b(ns, ns->zone_size);
3910 
3911     iocb->aiocb = blk_aio_pwrite_zeroes(ns->blkconf.blk, moff, count,
3912                                         BDRV_REQ_MAY_UNMAP,
3913                                         nvme_zone_reset_cb, iocb);
3914     return;
3915 
3916 out:
3917     nvme_zone_reset_cb(iocb, ret);
3918 }
3919 
3920 static void nvme_zone_reset_cb(void *opaque, int ret)
3921 {
3922     NvmeZoneResetAIOCB *iocb = opaque;
3923     NvmeRequest *req = iocb->req;
3924     NvmeNamespace *ns = req->ns;
3925 
3926     if (iocb->ret < 0) {
3927         goto done;
3928     } else if (ret < 0) {
3929         iocb->ret = ret;
3930         goto done;
3931     }
3932 
3933     if (iocb->zone) {
3934         nvme_zrm_reset(ns, iocb->zone);
3935 
3936         if (!iocb->all) {
3937             goto done;
3938         }
3939     }
3940 
3941     while (iocb->idx < ns->num_zones) {
3942         NvmeZone *zone = &ns->zone_array[iocb->idx++];
3943 
3944         switch (nvme_get_zone_state(zone)) {
3945         case NVME_ZONE_STATE_EMPTY:
3946             if (!iocb->all) {
3947                 goto done;
3948             }
3949 
3950             continue;
3951 
3952         case NVME_ZONE_STATE_EXPLICITLY_OPEN:
3953         case NVME_ZONE_STATE_IMPLICITLY_OPEN:
3954         case NVME_ZONE_STATE_CLOSED:
3955         case NVME_ZONE_STATE_FULL:
3956             iocb->zone = zone;
3957             break;
3958 
3959         default:
3960             continue;
3961         }
3962 
3963         trace_pci_nvme_zns_zone_reset(zone->d.zslba);
3964 
3965         iocb->aiocb = blk_aio_pwrite_zeroes(ns->blkconf.blk,
3966                                             nvme_l2b(ns, zone->d.zslba),
3967                                             nvme_l2b(ns, ns->zone_size),
3968                                             BDRV_REQ_MAY_UNMAP,
3969                                             nvme_zone_reset_epilogue_cb,
3970                                             iocb);
3971         return;
3972     }
3973 
3974 done:
3975     iocb->aiocb = NULL;
3976 
3977     iocb->common.cb(iocb->common.opaque, iocb->ret);
3978     qemu_aio_unref(iocb);
3979 }
3980 
3981 static uint16_t nvme_zone_mgmt_send_zrwa_flush(NvmeCtrl *n, NvmeZone *zone,
3982                                                uint64_t elba, NvmeRequest *req)
3983 {
3984     NvmeNamespace *ns = req->ns;
3985     uint16_t ozcs = le16_to_cpu(ns->id_ns_zoned->ozcs);
3986     uint64_t wp = zone->d.wp;
3987     uint32_t nlb = elba - wp + 1;
3988     uint16_t status;
3989 
3990 
3991     if (!(ozcs & NVME_ID_NS_ZONED_OZCS_ZRWASUP)) {
3992         return NVME_INVALID_ZONE_OP | NVME_DNR;
3993     }
3994 
3995     if (!(zone->d.za & NVME_ZA_ZRWA_VALID)) {
3996         return NVME_INVALID_FIELD | NVME_DNR;
3997     }
3998 
3999     if (elba < wp || elba > wp + ns->zns.zrwas) {
4000         return NVME_ZONE_BOUNDARY_ERROR | NVME_DNR;
4001     }
4002 
4003     if (nlb % ns->zns.zrwafg) {
4004         return NVME_INVALID_FIELD | NVME_DNR;
4005     }
4006 
4007     status = nvme_zrm_auto(n, ns, zone);
4008     if (status) {
4009         return status;
4010     }
4011 
4012     zone->w_ptr += nlb;
4013 
4014     nvme_advance_zone_wp(ns, zone, nlb);
4015 
4016     return NVME_SUCCESS;
4017 }
4018 
4019 static uint16_t nvme_zone_mgmt_send(NvmeCtrl *n, NvmeRequest *req)
4020 {
4021     NvmeZoneSendCmd *cmd = (NvmeZoneSendCmd *)&req->cmd;
4022     NvmeNamespace *ns = req->ns;
4023     NvmeZone *zone;
4024     NvmeZoneResetAIOCB *iocb;
4025     uint8_t *zd_ext;
4026     uint64_t slba = 0;
4027     uint32_t zone_idx = 0;
4028     uint16_t status;
4029     uint8_t action = cmd->zsa;
4030     bool all;
4031     enum NvmeZoneProcessingMask proc_mask = NVME_PROC_CURRENT_ZONE;
4032 
4033     all = cmd->zsflags & NVME_ZSFLAG_SELECT_ALL;
4034 
4035     req->status = NVME_SUCCESS;
4036 
4037     if (!all) {
4038         status = nvme_get_mgmt_zone_slba_idx(ns, &req->cmd, &slba, &zone_idx);
4039         if (status) {
4040             return status;
4041         }
4042     }
4043 
4044     zone = &ns->zone_array[zone_idx];
4045     if (slba != zone->d.zslba && action != NVME_ZONE_ACTION_ZRWA_FLUSH) {
4046         trace_pci_nvme_err_unaligned_zone_cmd(action, slba, zone->d.zslba);
4047         return NVME_INVALID_FIELD | NVME_DNR;
4048     }
4049 
4050     switch (action) {
4051 
4052     case NVME_ZONE_ACTION_OPEN:
4053         if (all) {
4054             proc_mask = NVME_PROC_CLOSED_ZONES;
4055         }
4056         trace_pci_nvme_open_zone(slba, zone_idx, all);
4057         status = nvme_do_zone_op(ns, zone, proc_mask, nvme_open_zone, req);
4058         break;
4059 
4060     case NVME_ZONE_ACTION_CLOSE:
4061         if (all) {
4062             proc_mask = NVME_PROC_OPENED_ZONES;
4063         }
4064         trace_pci_nvme_close_zone(slba, zone_idx, all);
4065         status = nvme_do_zone_op(ns, zone, proc_mask, nvme_close_zone, req);
4066         break;
4067 
4068     case NVME_ZONE_ACTION_FINISH:
4069         if (all) {
4070             proc_mask = NVME_PROC_OPENED_ZONES | NVME_PROC_CLOSED_ZONES;
4071         }
4072         trace_pci_nvme_finish_zone(slba, zone_idx, all);
4073         status = nvme_do_zone_op(ns, zone, proc_mask, nvme_finish_zone, req);
4074         break;
4075 
4076     case NVME_ZONE_ACTION_RESET:
4077         trace_pci_nvme_reset_zone(slba, zone_idx, all);
4078 
4079         iocb = blk_aio_get(&nvme_zone_reset_aiocb_info, ns->blkconf.blk,
4080                            nvme_misc_cb, req);
4081 
4082         iocb->req = req;
4083         iocb->ret = 0;
4084         iocb->all = all;
4085         iocb->idx = zone_idx;
4086         iocb->zone = NULL;
4087 
4088         req->aiocb = &iocb->common;
4089         nvme_zone_reset_cb(iocb, 0);
4090 
4091         return NVME_NO_COMPLETE;
4092 
4093     case NVME_ZONE_ACTION_OFFLINE:
4094         if (all) {
4095             proc_mask = NVME_PROC_READ_ONLY_ZONES;
4096         }
4097         trace_pci_nvme_offline_zone(slba, zone_idx, all);
4098         status = nvme_do_zone_op(ns, zone, proc_mask, nvme_offline_zone, req);
4099         break;
4100 
4101     case NVME_ZONE_ACTION_SET_ZD_EXT:
4102         trace_pci_nvme_set_descriptor_extension(slba, zone_idx);
4103         if (all || !ns->params.zd_extension_size) {
4104             return NVME_INVALID_FIELD | NVME_DNR;
4105         }
4106         zd_ext = nvme_get_zd_extension(ns, zone_idx);
4107         status = nvme_h2c(n, zd_ext, ns->params.zd_extension_size, req);
4108         if (status) {
4109             trace_pci_nvme_err_zd_extension_map_error(zone_idx);
4110             return status;
4111         }
4112 
4113         status = nvme_set_zd_ext(ns, zone);
4114         if (status == NVME_SUCCESS) {
4115             trace_pci_nvme_zd_extension_set(zone_idx);
4116             return status;
4117         }
4118         break;
4119 
4120     case NVME_ZONE_ACTION_ZRWA_FLUSH:
4121         if (all) {
4122             return NVME_INVALID_FIELD | NVME_DNR;
4123         }
4124 
4125         return nvme_zone_mgmt_send_zrwa_flush(n, zone, slba, req);
4126 
4127     default:
4128         trace_pci_nvme_err_invalid_mgmt_action(action);
4129         status = NVME_INVALID_FIELD;
4130     }
4131 
4132     if (status == NVME_ZONE_INVAL_TRANSITION) {
4133         trace_pci_nvme_err_invalid_zone_state_transition(action, slba,
4134                                                          zone->d.za);
4135     }
4136     if (status) {
4137         status |= NVME_DNR;
4138     }
4139 
4140     return status;
4141 }
4142 
4143 static bool nvme_zone_matches_filter(uint32_t zafs, NvmeZone *zl)
4144 {
4145     NvmeZoneState zs = nvme_get_zone_state(zl);
4146 
4147     switch (zafs) {
4148     case NVME_ZONE_REPORT_ALL:
4149         return true;
4150     case NVME_ZONE_REPORT_EMPTY:
4151         return zs == NVME_ZONE_STATE_EMPTY;
4152     case NVME_ZONE_REPORT_IMPLICITLY_OPEN:
4153         return zs == NVME_ZONE_STATE_IMPLICITLY_OPEN;
4154     case NVME_ZONE_REPORT_EXPLICITLY_OPEN:
4155         return zs == NVME_ZONE_STATE_EXPLICITLY_OPEN;
4156     case NVME_ZONE_REPORT_CLOSED:
4157         return zs == NVME_ZONE_STATE_CLOSED;
4158     case NVME_ZONE_REPORT_FULL:
4159         return zs == NVME_ZONE_STATE_FULL;
4160     case NVME_ZONE_REPORT_READ_ONLY:
4161         return zs == NVME_ZONE_STATE_READ_ONLY;
4162     case NVME_ZONE_REPORT_OFFLINE:
4163         return zs == NVME_ZONE_STATE_OFFLINE;
4164     default:
4165         return false;
4166     }
4167 }
4168 
4169 static uint16_t nvme_zone_mgmt_recv(NvmeCtrl *n, NvmeRequest *req)
4170 {
4171     NvmeCmd *cmd = (NvmeCmd *)&req->cmd;
4172     NvmeNamespace *ns = req->ns;
4173     /* cdw12 is zero-based number of dwords to return. Convert to bytes */
4174     uint32_t data_size = (le32_to_cpu(cmd->cdw12) + 1) << 2;
4175     uint32_t dw13 = le32_to_cpu(cmd->cdw13);
4176     uint32_t zone_idx, zra, zrasf, partial;
4177     uint64_t max_zones, nr_zones = 0;
4178     uint16_t status;
4179     uint64_t slba;
4180     NvmeZoneDescr *z;
4181     NvmeZone *zone;
4182     NvmeZoneReportHeader *header;
4183     void *buf, *buf_p;
4184     size_t zone_entry_sz;
4185     int i;
4186 
4187     req->status = NVME_SUCCESS;
4188 
4189     status = nvme_get_mgmt_zone_slba_idx(ns, cmd, &slba, &zone_idx);
4190     if (status) {
4191         return status;
4192     }
4193 
4194     zra = dw13 & 0xff;
4195     if (zra != NVME_ZONE_REPORT && zra != NVME_ZONE_REPORT_EXTENDED) {
4196         return NVME_INVALID_FIELD | NVME_DNR;
4197     }
4198     if (zra == NVME_ZONE_REPORT_EXTENDED && !ns->params.zd_extension_size) {
4199         return NVME_INVALID_FIELD | NVME_DNR;
4200     }
4201 
4202     zrasf = (dw13 >> 8) & 0xff;
4203     if (zrasf > NVME_ZONE_REPORT_OFFLINE) {
4204         return NVME_INVALID_FIELD | NVME_DNR;
4205     }
4206 
4207     if (data_size < sizeof(NvmeZoneReportHeader)) {
4208         return NVME_INVALID_FIELD | NVME_DNR;
4209     }
4210 
4211     status = nvme_check_mdts(n, data_size);
4212     if (status) {
4213         return status;
4214     }
4215 
4216     partial = (dw13 >> 16) & 0x01;
4217 
4218     zone_entry_sz = sizeof(NvmeZoneDescr);
4219     if (zra == NVME_ZONE_REPORT_EXTENDED) {
4220         zone_entry_sz += ns->params.zd_extension_size;
4221     }
4222 
4223     max_zones = (data_size - sizeof(NvmeZoneReportHeader)) / zone_entry_sz;
4224     buf = g_malloc0(data_size);
4225 
4226     zone = &ns->zone_array[zone_idx];
4227     for (i = zone_idx; i < ns->num_zones; i++) {
4228         if (partial && nr_zones >= max_zones) {
4229             break;
4230         }
4231         if (nvme_zone_matches_filter(zrasf, zone++)) {
4232             nr_zones++;
4233         }
4234     }
4235     header = buf;
4236     header->nr_zones = cpu_to_le64(nr_zones);
4237 
4238     buf_p = buf + sizeof(NvmeZoneReportHeader);
4239     for (; zone_idx < ns->num_zones && max_zones > 0; zone_idx++) {
4240         zone = &ns->zone_array[zone_idx];
4241         if (nvme_zone_matches_filter(zrasf, zone)) {
4242             z = buf_p;
4243             buf_p += sizeof(NvmeZoneDescr);
4244 
4245             z->zt = zone->d.zt;
4246             z->zs = zone->d.zs;
4247             z->zcap = cpu_to_le64(zone->d.zcap);
4248             z->zslba = cpu_to_le64(zone->d.zslba);
4249             z->za = zone->d.za;
4250 
4251             if (nvme_wp_is_valid(zone)) {
4252                 z->wp = cpu_to_le64(zone->d.wp);
4253             } else {
4254                 z->wp = cpu_to_le64(~0ULL);
4255             }
4256 
4257             if (zra == NVME_ZONE_REPORT_EXTENDED) {
4258                 if (zone->d.za & NVME_ZA_ZD_EXT_VALID) {
4259                     memcpy(buf_p, nvme_get_zd_extension(ns, zone_idx),
4260                            ns->params.zd_extension_size);
4261                 }
4262                 buf_p += ns->params.zd_extension_size;
4263             }
4264 
4265             max_zones--;
4266         }
4267     }
4268 
4269     status = nvme_c2h(n, (uint8_t *)buf, data_size, req);
4270 
4271     g_free(buf);
4272 
4273     return status;
4274 }
4275 
4276 static uint16_t nvme_io_mgmt_recv_ruhs(NvmeCtrl *n, NvmeRequest *req,
4277                                        size_t len)
4278 {
4279     NvmeNamespace *ns = req->ns;
4280     NvmeEnduranceGroup *endgrp;
4281     NvmeRuhStatus *hdr;
4282     NvmeRuhStatusDescr *ruhsd;
4283     unsigned int nruhsd;
4284     uint16_t rg, ph, *ruhid;
4285     size_t trans_len;
4286     g_autofree uint8_t *buf = NULL;
4287 
4288     if (!n->subsys) {
4289         return NVME_INVALID_FIELD | NVME_DNR;
4290     }
4291 
4292     if (ns->params.nsid == 0 || ns->params.nsid == 0xffffffff) {
4293         return NVME_INVALID_NSID | NVME_DNR;
4294     }
4295 
4296     if (!n->subsys->endgrp.fdp.enabled) {
4297         return NVME_FDP_DISABLED | NVME_DNR;
4298     }
4299 
4300     endgrp = ns->endgrp;
4301 
4302     nruhsd = ns->fdp.nphs * endgrp->fdp.nrg;
4303     trans_len = sizeof(NvmeRuhStatus) + nruhsd * sizeof(NvmeRuhStatusDescr);
4304     buf = g_malloc(trans_len);
4305 
4306     trans_len = MIN(trans_len, len);
4307 
4308     hdr = (NvmeRuhStatus *)buf;
4309     ruhsd = (NvmeRuhStatusDescr *)(buf + sizeof(NvmeRuhStatus));
4310 
4311     hdr->nruhsd = cpu_to_le16(nruhsd);
4312 
4313     ruhid = ns->fdp.phs;
4314 
4315     for (ph = 0; ph < ns->fdp.nphs; ph++, ruhid++) {
4316         NvmeRuHandle *ruh = &endgrp->fdp.ruhs[*ruhid];
4317 
4318         for (rg = 0; rg < endgrp->fdp.nrg; rg++, ruhsd++) {
4319             uint16_t pid = nvme_make_pid(ns, rg, ph);
4320 
4321             ruhsd->pid = cpu_to_le16(pid);
4322             ruhsd->ruhid = *ruhid;
4323             ruhsd->earutr = 0;
4324             ruhsd->ruamw = cpu_to_le64(ruh->rus[rg].ruamw);
4325         }
4326     }
4327 
4328     return nvme_c2h(n, buf, trans_len, req);
4329 }
4330 
4331 static uint16_t nvme_io_mgmt_recv(NvmeCtrl *n, NvmeRequest *req)
4332 {
4333     NvmeCmd *cmd = &req->cmd;
4334     uint32_t cdw10 = le32_to_cpu(cmd->cdw10);
4335     uint32_t numd = le32_to_cpu(cmd->cdw11);
4336     uint8_t mo = (cdw10 & 0xff);
4337     size_t len = (numd + 1) << 2;
4338 
4339     switch (mo) {
4340     case NVME_IOMR_MO_NOP:
4341         return 0;
4342     case NVME_IOMR_MO_RUH_STATUS:
4343         return nvme_io_mgmt_recv_ruhs(n, req, len);
4344     default:
4345         return NVME_INVALID_FIELD | NVME_DNR;
4346     };
4347 }
4348 
4349 static uint16_t nvme_io_mgmt_send_ruh_update(NvmeCtrl *n, NvmeRequest *req)
4350 {
4351     NvmeCmd *cmd = &req->cmd;
4352     NvmeNamespace *ns = req->ns;
4353     uint32_t cdw10 = le32_to_cpu(cmd->cdw10);
4354     uint16_t ret = NVME_SUCCESS;
4355     uint32_t npid = (cdw10 >> 1) + 1;
4356     unsigned int i = 0;
4357     g_autofree uint16_t *pids = NULL;
4358     uint32_t maxnpid;
4359 
4360     if (!ns->endgrp || !ns->endgrp->fdp.enabled) {
4361         return NVME_FDP_DISABLED | NVME_DNR;
4362     }
4363 
4364     maxnpid = n->subsys->endgrp.fdp.nrg * n->subsys->endgrp.fdp.nruh;
4365 
4366     if (unlikely(npid >= MIN(NVME_FDP_MAXPIDS, maxnpid))) {
4367         return NVME_INVALID_FIELD | NVME_DNR;
4368     }
4369 
4370     pids = g_new(uint16_t, npid);
4371 
4372     ret = nvme_h2c(n, pids, npid * sizeof(uint16_t), req);
4373     if (ret) {
4374         return ret;
4375     }
4376 
4377     for (; i < npid; i++) {
4378         if (!nvme_update_ruh(n, ns, pids[i])) {
4379             return NVME_INVALID_FIELD | NVME_DNR;
4380         }
4381     }
4382 
4383     return ret;
4384 }
4385 
4386 static uint16_t nvme_io_mgmt_send(NvmeCtrl *n, NvmeRequest *req)
4387 {
4388     NvmeCmd *cmd = &req->cmd;
4389     uint32_t cdw10 = le32_to_cpu(cmd->cdw10);
4390     uint8_t mo = (cdw10 & 0xff);
4391 
4392     switch (mo) {
4393     case NVME_IOMS_MO_NOP:
4394         return 0;
4395     case NVME_IOMS_MO_RUH_UPDATE:
4396         return nvme_io_mgmt_send_ruh_update(n, req);
4397     default:
4398         return NVME_INVALID_FIELD | NVME_DNR;
4399     };
4400 }
4401 
4402 static uint16_t nvme_io_cmd(NvmeCtrl *n, NvmeRequest *req)
4403 {
4404     NvmeNamespace *ns;
4405     uint32_t nsid = le32_to_cpu(req->cmd.nsid);
4406 
4407     trace_pci_nvme_io_cmd(nvme_cid(req), nsid, nvme_sqid(req),
4408                           req->cmd.opcode, nvme_io_opc_str(req->cmd.opcode));
4409 
4410     if (!nvme_nsid_valid(n, nsid)) {
4411         return NVME_INVALID_NSID | NVME_DNR;
4412     }
4413 
4414     /*
4415      * In the base NVM command set, Flush may apply to all namespaces
4416      * (indicated by NSID being set to FFFFFFFFh). But if that feature is used
4417      * along with TP 4056 (Namespace Types), it may be pretty screwed up.
4418      *
4419      * If NSID is indeed set to FFFFFFFFh, we simply cannot associate the
4420      * opcode with a specific command since we cannot determine a unique I/O
4421      * command set. Opcode 0h could have any other meaning than something
4422      * equivalent to flushing and say it DOES have completely different
4423      * semantics in some other command set - does an NSID of FFFFFFFFh then
4424      * mean "for all namespaces, apply whatever command set specific command
4425      * that uses the 0h opcode?" Or does it mean "for all namespaces, apply
4426      * whatever command that uses the 0h opcode if, and only if, it allows NSID
4427      * to be FFFFFFFFh"?
4428      *
4429      * Anyway (and luckily), for now, we do not care about this since the
4430      * device only supports namespace types that includes the NVM Flush command
4431      * (NVM and Zoned), so always do an NVM Flush.
4432      */
4433     if (req->cmd.opcode == NVME_CMD_FLUSH) {
4434         return nvme_flush(n, req);
4435     }
4436 
4437     ns = nvme_ns(n, nsid);
4438     if (unlikely(!ns)) {
4439         return NVME_INVALID_FIELD | NVME_DNR;
4440     }
4441 
4442     if (!(ns->iocs[req->cmd.opcode] & NVME_CMD_EFF_CSUPP)) {
4443         trace_pci_nvme_err_invalid_opc(req->cmd.opcode);
4444         return NVME_INVALID_OPCODE | NVME_DNR;
4445     }
4446 
4447     if (ns->status) {
4448         return ns->status;
4449     }
4450 
4451     if (NVME_CMD_FLAGS_FUSE(req->cmd.flags)) {
4452         return NVME_INVALID_FIELD;
4453     }
4454 
4455     req->ns = ns;
4456 
4457     switch (req->cmd.opcode) {
4458     case NVME_CMD_WRITE_ZEROES:
4459         return nvme_write_zeroes(n, req);
4460     case NVME_CMD_ZONE_APPEND:
4461         return nvme_zone_append(n, req);
4462     case NVME_CMD_WRITE:
4463         return nvme_write(n, req);
4464     case NVME_CMD_READ:
4465         return nvme_read(n, req);
4466     case NVME_CMD_COMPARE:
4467         return nvme_compare(n, req);
4468     case NVME_CMD_DSM:
4469         return nvme_dsm(n, req);
4470     case NVME_CMD_VERIFY:
4471         return nvme_verify(n, req);
4472     case NVME_CMD_COPY:
4473         return nvme_copy(n, req);
4474     case NVME_CMD_ZONE_MGMT_SEND:
4475         return nvme_zone_mgmt_send(n, req);
4476     case NVME_CMD_ZONE_MGMT_RECV:
4477         return nvme_zone_mgmt_recv(n, req);
4478     case NVME_CMD_IO_MGMT_RECV:
4479         return nvme_io_mgmt_recv(n, req);
4480     case NVME_CMD_IO_MGMT_SEND:
4481         return nvme_io_mgmt_send(n, req);
4482     default:
4483         assert(false);
4484     }
4485 
4486     return NVME_INVALID_OPCODE | NVME_DNR;
4487 }
4488 
4489 static void nvme_cq_notifier(EventNotifier *e)
4490 {
4491     NvmeCQueue *cq = container_of(e, NvmeCQueue, notifier);
4492     NvmeCtrl *n = cq->ctrl;
4493 
4494     if (!event_notifier_test_and_clear(e)) {
4495         return;
4496     }
4497 
4498     nvme_update_cq_head(cq);
4499 
4500     if (cq->tail == cq->head) {
4501         if (cq->irq_enabled) {
4502             n->cq_pending--;
4503         }
4504 
4505         nvme_irq_deassert(n, cq);
4506     }
4507 
4508     qemu_bh_schedule(cq->bh);
4509 }
4510 
4511 static int nvme_init_cq_ioeventfd(NvmeCQueue *cq)
4512 {
4513     NvmeCtrl *n = cq->ctrl;
4514     uint16_t offset = (cq->cqid << 3) + (1 << 2);
4515     int ret;
4516 
4517     ret = event_notifier_init(&cq->notifier, 0);
4518     if (ret < 0) {
4519         return ret;
4520     }
4521 
4522     event_notifier_set_handler(&cq->notifier, nvme_cq_notifier);
4523     memory_region_add_eventfd(&n->iomem,
4524                               0x1000 + offset, 4, false, 0, &cq->notifier);
4525 
4526     return 0;
4527 }
4528 
4529 static void nvme_sq_notifier(EventNotifier *e)
4530 {
4531     NvmeSQueue *sq = container_of(e, NvmeSQueue, notifier);
4532 
4533     if (!event_notifier_test_and_clear(e)) {
4534         return;
4535     }
4536 
4537     nvme_process_sq(sq);
4538 }
4539 
4540 static int nvme_init_sq_ioeventfd(NvmeSQueue *sq)
4541 {
4542     NvmeCtrl *n = sq->ctrl;
4543     uint16_t offset = sq->sqid << 3;
4544     int ret;
4545 
4546     ret = event_notifier_init(&sq->notifier, 0);
4547     if (ret < 0) {
4548         return ret;
4549     }
4550 
4551     event_notifier_set_handler(&sq->notifier, nvme_sq_notifier);
4552     memory_region_add_eventfd(&n->iomem,
4553                               0x1000 + offset, 4, false, 0, &sq->notifier);
4554 
4555     return 0;
4556 }
4557 
4558 static void nvme_free_sq(NvmeSQueue *sq, NvmeCtrl *n)
4559 {
4560     uint16_t offset = sq->sqid << 3;
4561 
4562     n->sq[sq->sqid] = NULL;
4563     qemu_bh_delete(sq->bh);
4564     if (sq->ioeventfd_enabled) {
4565         memory_region_del_eventfd(&n->iomem,
4566                                   0x1000 + offset, 4, false, 0, &sq->notifier);
4567         event_notifier_set_handler(&sq->notifier, NULL);
4568         event_notifier_cleanup(&sq->notifier);
4569     }
4570     g_free(sq->io_req);
4571     if (sq->sqid) {
4572         g_free(sq);
4573     }
4574 }
4575 
4576 static uint16_t nvme_del_sq(NvmeCtrl *n, NvmeRequest *req)
4577 {
4578     NvmeDeleteQ *c = (NvmeDeleteQ *)&req->cmd;
4579     NvmeRequest *r, *next;
4580     NvmeSQueue *sq;
4581     NvmeCQueue *cq;
4582     uint16_t qid = le16_to_cpu(c->qid);
4583 
4584     if (unlikely(!qid || nvme_check_sqid(n, qid))) {
4585         trace_pci_nvme_err_invalid_del_sq(qid);
4586         return NVME_INVALID_QID | NVME_DNR;
4587     }
4588 
4589     trace_pci_nvme_del_sq(qid);
4590 
4591     sq = n->sq[qid];
4592     while (!QTAILQ_EMPTY(&sq->out_req_list)) {
4593         r = QTAILQ_FIRST(&sq->out_req_list);
4594         assert(r->aiocb);
4595         blk_aio_cancel(r->aiocb);
4596     }
4597 
4598     assert(QTAILQ_EMPTY(&sq->out_req_list));
4599 
4600     if (!nvme_check_cqid(n, sq->cqid)) {
4601         cq = n->cq[sq->cqid];
4602         QTAILQ_REMOVE(&cq->sq_list, sq, entry);
4603 
4604         nvme_post_cqes(cq);
4605         QTAILQ_FOREACH_SAFE(r, &cq->req_list, entry, next) {
4606             if (r->sq == sq) {
4607                 QTAILQ_REMOVE(&cq->req_list, r, entry);
4608                 QTAILQ_INSERT_TAIL(&sq->req_list, r, entry);
4609             }
4610         }
4611     }
4612 
4613     nvme_free_sq(sq, n);
4614     return NVME_SUCCESS;
4615 }
4616 
4617 static void nvme_init_sq(NvmeSQueue *sq, NvmeCtrl *n, uint64_t dma_addr,
4618                          uint16_t sqid, uint16_t cqid, uint16_t size)
4619 {
4620     int i;
4621     NvmeCQueue *cq;
4622 
4623     sq->ctrl = n;
4624     sq->dma_addr = dma_addr;
4625     sq->sqid = sqid;
4626     sq->size = size;
4627     sq->cqid = cqid;
4628     sq->head = sq->tail = 0;
4629     sq->io_req = g_new0(NvmeRequest, sq->size);
4630 
4631     QTAILQ_INIT(&sq->req_list);
4632     QTAILQ_INIT(&sq->out_req_list);
4633     for (i = 0; i < sq->size; i++) {
4634         sq->io_req[i].sq = sq;
4635         QTAILQ_INSERT_TAIL(&(sq->req_list), &sq->io_req[i], entry);
4636     }
4637 
4638     sq->bh = qemu_bh_new_guarded(nvme_process_sq, sq,
4639                                  &DEVICE(sq->ctrl)->mem_reentrancy_guard);
4640 
4641     if (n->dbbuf_enabled) {
4642         sq->db_addr = n->dbbuf_dbs + (sqid << 3);
4643         sq->ei_addr = n->dbbuf_eis + (sqid << 3);
4644 
4645         if (n->params.ioeventfd && sq->sqid != 0) {
4646             if (!nvme_init_sq_ioeventfd(sq)) {
4647                 sq->ioeventfd_enabled = true;
4648             }
4649         }
4650     }
4651 
4652     assert(n->cq[cqid]);
4653     cq = n->cq[cqid];
4654     QTAILQ_INSERT_TAIL(&(cq->sq_list), sq, entry);
4655     n->sq[sqid] = sq;
4656 }
4657 
4658 static uint16_t nvme_create_sq(NvmeCtrl *n, NvmeRequest *req)
4659 {
4660     NvmeSQueue *sq;
4661     NvmeCreateSq *c = (NvmeCreateSq *)&req->cmd;
4662 
4663     uint16_t cqid = le16_to_cpu(c->cqid);
4664     uint16_t sqid = le16_to_cpu(c->sqid);
4665     uint16_t qsize = le16_to_cpu(c->qsize);
4666     uint16_t qflags = le16_to_cpu(c->sq_flags);
4667     uint64_t prp1 = le64_to_cpu(c->prp1);
4668 
4669     trace_pci_nvme_create_sq(prp1, sqid, cqid, qsize, qflags);
4670 
4671     if (unlikely(!cqid || nvme_check_cqid(n, cqid))) {
4672         trace_pci_nvme_err_invalid_create_sq_cqid(cqid);
4673         return NVME_INVALID_CQID | NVME_DNR;
4674     }
4675     if (unlikely(!sqid || sqid > n->conf_ioqpairs || n->sq[sqid] != NULL)) {
4676         trace_pci_nvme_err_invalid_create_sq_sqid(sqid);
4677         return NVME_INVALID_QID | NVME_DNR;
4678     }
4679     if (unlikely(!qsize || qsize > NVME_CAP_MQES(ldq_le_p(&n->bar.cap)))) {
4680         trace_pci_nvme_err_invalid_create_sq_size(qsize);
4681         return NVME_MAX_QSIZE_EXCEEDED | NVME_DNR;
4682     }
4683     if (unlikely(prp1 & (n->page_size - 1))) {
4684         trace_pci_nvme_err_invalid_create_sq_addr(prp1);
4685         return NVME_INVALID_PRP_OFFSET | NVME_DNR;
4686     }
4687     if (unlikely(!(NVME_SQ_FLAGS_PC(qflags)))) {
4688         trace_pci_nvme_err_invalid_create_sq_qflags(NVME_SQ_FLAGS_PC(qflags));
4689         return NVME_INVALID_FIELD | NVME_DNR;
4690     }
4691     sq = g_malloc0(sizeof(*sq));
4692     nvme_init_sq(sq, n, prp1, sqid, cqid, qsize + 1);
4693     return NVME_SUCCESS;
4694 }
4695 
4696 struct nvme_stats {
4697     uint64_t units_read;
4698     uint64_t units_written;
4699     uint64_t read_commands;
4700     uint64_t write_commands;
4701 };
4702 
4703 static void nvme_set_blk_stats(NvmeNamespace *ns, struct nvme_stats *stats)
4704 {
4705     BlockAcctStats *s = blk_get_stats(ns->blkconf.blk);
4706 
4707     stats->units_read += s->nr_bytes[BLOCK_ACCT_READ];
4708     stats->units_written += s->nr_bytes[BLOCK_ACCT_WRITE];
4709     stats->read_commands += s->nr_ops[BLOCK_ACCT_READ];
4710     stats->write_commands += s->nr_ops[BLOCK_ACCT_WRITE];
4711 }
4712 
4713 static uint16_t nvme_smart_info(NvmeCtrl *n, uint8_t rae, uint32_t buf_len,
4714                                 uint64_t off, NvmeRequest *req)
4715 {
4716     uint32_t nsid = le32_to_cpu(req->cmd.nsid);
4717     struct nvme_stats stats = { 0 };
4718     NvmeSmartLog smart = { 0 };
4719     uint32_t trans_len;
4720     NvmeNamespace *ns;
4721     time_t current_ms;
4722     uint64_t u_read, u_written;
4723 
4724     if (off >= sizeof(smart)) {
4725         return NVME_INVALID_FIELD | NVME_DNR;
4726     }
4727 
4728     if (nsid != 0xffffffff) {
4729         ns = nvme_ns(n, nsid);
4730         if (!ns) {
4731             return NVME_INVALID_NSID | NVME_DNR;
4732         }
4733         nvme_set_blk_stats(ns, &stats);
4734     } else {
4735         int i;
4736 
4737         for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
4738             ns = nvme_ns(n, i);
4739             if (!ns) {
4740                 continue;
4741             }
4742             nvme_set_blk_stats(ns, &stats);
4743         }
4744     }
4745 
4746     trans_len = MIN(sizeof(smart) - off, buf_len);
4747     smart.critical_warning = n->smart_critical_warning;
4748 
4749     u_read = DIV_ROUND_UP(stats.units_read >> BDRV_SECTOR_BITS, 1000);
4750     u_written = DIV_ROUND_UP(stats.units_written >> BDRV_SECTOR_BITS, 1000);
4751 
4752     smart.data_units_read[0] = cpu_to_le64(u_read);
4753     smart.data_units_written[0] = cpu_to_le64(u_written);
4754     smart.host_read_commands[0] = cpu_to_le64(stats.read_commands);
4755     smart.host_write_commands[0] = cpu_to_le64(stats.write_commands);
4756 
4757     smart.temperature = cpu_to_le16(n->temperature);
4758 
4759     if ((n->temperature >= n->features.temp_thresh_hi) ||
4760         (n->temperature <= n->features.temp_thresh_low)) {
4761         smart.critical_warning |= NVME_SMART_TEMPERATURE;
4762     }
4763 
4764     current_ms = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
4765     smart.power_on_hours[0] =
4766         cpu_to_le64((((current_ms - n->starttime_ms) / 1000) / 60) / 60);
4767 
4768     if (!rae) {
4769         nvme_clear_events(n, NVME_AER_TYPE_SMART);
4770     }
4771 
4772     return nvme_c2h(n, (uint8_t *) &smart + off, trans_len, req);
4773 }
4774 
4775 static uint16_t nvme_endgrp_info(NvmeCtrl *n,  uint8_t rae, uint32_t buf_len,
4776                                  uint64_t off, NvmeRequest *req)
4777 {
4778     uint32_t dw11 = le32_to_cpu(req->cmd.cdw11);
4779     uint16_t endgrpid = (dw11 >> 16) & 0xffff;
4780     struct nvme_stats stats = {};
4781     NvmeEndGrpLog info = {};
4782     int i;
4783 
4784     if (!n->subsys || endgrpid != 0x1) {
4785         return NVME_INVALID_FIELD | NVME_DNR;
4786     }
4787 
4788     if (off >= sizeof(info)) {
4789         return NVME_INVALID_FIELD | NVME_DNR;
4790     }
4791 
4792     for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
4793         NvmeNamespace *ns = nvme_subsys_ns(n->subsys, i);
4794         if (!ns) {
4795             continue;
4796         }
4797 
4798         nvme_set_blk_stats(ns, &stats);
4799     }
4800 
4801     info.data_units_read[0] =
4802         cpu_to_le64(DIV_ROUND_UP(stats.units_read / 1000000000, 1000000000));
4803     info.data_units_written[0] =
4804         cpu_to_le64(DIV_ROUND_UP(stats.units_written / 1000000000, 1000000000));
4805     info.media_units_written[0] =
4806         cpu_to_le64(DIV_ROUND_UP(stats.units_written / 1000000000, 1000000000));
4807 
4808     info.host_read_commands[0] = cpu_to_le64(stats.read_commands);
4809     info.host_write_commands[0] = cpu_to_le64(stats.write_commands);
4810 
4811     buf_len = MIN(sizeof(info) - off, buf_len);
4812 
4813     return nvme_c2h(n, (uint8_t *)&info + off, buf_len, req);
4814 }
4815 
4816 
4817 static uint16_t nvme_fw_log_info(NvmeCtrl *n, uint32_t buf_len, uint64_t off,
4818                                  NvmeRequest *req)
4819 {
4820     uint32_t trans_len;
4821     NvmeFwSlotInfoLog fw_log = {
4822         .afi = 0x1,
4823     };
4824 
4825     if (off >= sizeof(fw_log)) {
4826         return NVME_INVALID_FIELD | NVME_DNR;
4827     }
4828 
4829     strpadcpy((char *)&fw_log.frs1, sizeof(fw_log.frs1), "1.0", ' ');
4830     trans_len = MIN(sizeof(fw_log) - off, buf_len);
4831 
4832     return nvme_c2h(n, (uint8_t *) &fw_log + off, trans_len, req);
4833 }
4834 
4835 static uint16_t nvme_error_info(NvmeCtrl *n, uint8_t rae, uint32_t buf_len,
4836                                 uint64_t off, NvmeRequest *req)
4837 {
4838     uint32_t trans_len;
4839     NvmeErrorLog errlog;
4840 
4841     if (off >= sizeof(errlog)) {
4842         return NVME_INVALID_FIELD | NVME_DNR;
4843     }
4844 
4845     if (!rae) {
4846         nvme_clear_events(n, NVME_AER_TYPE_ERROR);
4847     }
4848 
4849     memset(&errlog, 0x0, sizeof(errlog));
4850     trans_len = MIN(sizeof(errlog) - off, buf_len);
4851 
4852     return nvme_c2h(n, (uint8_t *)&errlog, trans_len, req);
4853 }
4854 
4855 static uint16_t nvme_changed_nslist(NvmeCtrl *n, uint8_t rae, uint32_t buf_len,
4856                                     uint64_t off, NvmeRequest *req)
4857 {
4858     uint32_t nslist[1024];
4859     uint32_t trans_len;
4860     int i = 0;
4861     uint32_t nsid;
4862 
4863     if (off >= sizeof(nslist)) {
4864         trace_pci_nvme_err_invalid_log_page_offset(off, sizeof(nslist));
4865         return NVME_INVALID_FIELD | NVME_DNR;
4866     }
4867 
4868     memset(nslist, 0x0, sizeof(nslist));
4869     trans_len = MIN(sizeof(nslist) - off, buf_len);
4870 
4871     while ((nsid = find_first_bit(n->changed_nsids, NVME_CHANGED_NSID_SIZE)) !=
4872             NVME_CHANGED_NSID_SIZE) {
4873         /*
4874          * If more than 1024 namespaces, the first entry in the log page should
4875          * be set to FFFFFFFFh and the others to 0 as spec.
4876          */
4877         if (i == ARRAY_SIZE(nslist)) {
4878             memset(nslist, 0x0, sizeof(nslist));
4879             nslist[0] = 0xffffffff;
4880             break;
4881         }
4882 
4883         nslist[i++] = nsid;
4884         clear_bit(nsid, n->changed_nsids);
4885     }
4886 
4887     /*
4888      * Remove all the remaining list entries in case returns directly due to
4889      * more than 1024 namespaces.
4890      */
4891     if (nslist[0] == 0xffffffff) {
4892         bitmap_zero(n->changed_nsids, NVME_CHANGED_NSID_SIZE);
4893     }
4894 
4895     if (!rae) {
4896         nvme_clear_events(n, NVME_AER_TYPE_NOTICE);
4897     }
4898 
4899     return nvme_c2h(n, ((uint8_t *)nslist) + off, trans_len, req);
4900 }
4901 
4902 static uint16_t nvme_cmd_effects(NvmeCtrl *n, uint8_t csi, uint32_t buf_len,
4903                                  uint64_t off, NvmeRequest *req)
4904 {
4905     NvmeEffectsLog log = {};
4906     const uint32_t *src_iocs = NULL;
4907     uint32_t trans_len;
4908 
4909     if (off >= sizeof(log)) {
4910         trace_pci_nvme_err_invalid_log_page_offset(off, sizeof(log));
4911         return NVME_INVALID_FIELD | NVME_DNR;
4912     }
4913 
4914     switch (NVME_CC_CSS(ldl_le_p(&n->bar.cc))) {
4915     case NVME_CC_CSS_NVM:
4916         src_iocs = nvme_cse_iocs_nvm;
4917         /* fall through */
4918     case NVME_CC_CSS_ADMIN_ONLY:
4919         break;
4920     case NVME_CC_CSS_CSI:
4921         switch (csi) {
4922         case NVME_CSI_NVM:
4923             src_iocs = nvme_cse_iocs_nvm;
4924             break;
4925         case NVME_CSI_ZONED:
4926             src_iocs = nvme_cse_iocs_zoned;
4927             break;
4928         }
4929     }
4930 
4931     memcpy(log.acs, nvme_cse_acs, sizeof(nvme_cse_acs));
4932 
4933     if (src_iocs) {
4934         memcpy(log.iocs, src_iocs, sizeof(log.iocs));
4935     }
4936 
4937     trans_len = MIN(sizeof(log) - off, buf_len);
4938 
4939     return nvme_c2h(n, ((uint8_t *)&log) + off, trans_len, req);
4940 }
4941 
4942 static size_t sizeof_fdp_conf_descr(size_t nruh, size_t vss)
4943 {
4944     size_t entry_siz = sizeof(NvmeFdpDescrHdr) + nruh * sizeof(NvmeRuhDescr)
4945                        + vss;
4946     return ROUND_UP(entry_siz, 8);
4947 }
4948 
4949 static uint16_t nvme_fdp_confs(NvmeCtrl *n, uint32_t endgrpid, uint32_t buf_len,
4950                                uint64_t off, NvmeRequest *req)
4951 {
4952     uint32_t log_size, trans_len;
4953     g_autofree uint8_t *buf = NULL;
4954     NvmeFdpDescrHdr *hdr;
4955     NvmeRuhDescr *ruhd;
4956     NvmeEnduranceGroup *endgrp;
4957     NvmeFdpConfsHdr *log;
4958     size_t nruh, fdp_descr_size;
4959     int i;
4960 
4961     if (endgrpid != 1 || !n->subsys) {
4962         return NVME_INVALID_FIELD | NVME_DNR;
4963     }
4964 
4965     endgrp = &n->subsys->endgrp;
4966 
4967     if (endgrp->fdp.enabled) {
4968         nruh = endgrp->fdp.nruh;
4969     } else {
4970         nruh = 1;
4971     }
4972 
4973     fdp_descr_size = sizeof_fdp_conf_descr(nruh, FDPVSS);
4974     log_size = sizeof(NvmeFdpConfsHdr) + fdp_descr_size;
4975 
4976     if (off >= log_size) {
4977         return NVME_INVALID_FIELD | NVME_DNR;
4978     }
4979 
4980     trans_len = MIN(log_size - off, buf_len);
4981 
4982     buf = g_malloc0(log_size);
4983     log = (NvmeFdpConfsHdr *)buf;
4984     hdr = (NvmeFdpDescrHdr *)(log + 1);
4985     ruhd = (NvmeRuhDescr *)(buf + sizeof(*log) + sizeof(*hdr));
4986 
4987     log->num_confs = cpu_to_le16(0);
4988     log->size = cpu_to_le32(log_size);
4989 
4990     hdr->descr_size = cpu_to_le16(fdp_descr_size);
4991     if (endgrp->fdp.enabled) {
4992         hdr->fdpa = FIELD_DP8(hdr->fdpa, FDPA, VALID, 1);
4993         hdr->fdpa = FIELD_DP8(hdr->fdpa, FDPA, RGIF, endgrp->fdp.rgif);
4994         hdr->nrg = cpu_to_le16(endgrp->fdp.nrg);
4995         hdr->nruh = cpu_to_le16(endgrp->fdp.nruh);
4996         hdr->maxpids = cpu_to_le16(NVME_FDP_MAXPIDS - 1);
4997         hdr->nnss = cpu_to_le32(NVME_MAX_NAMESPACES);
4998         hdr->runs = cpu_to_le64(endgrp->fdp.runs);
4999 
5000         for (i = 0; i < nruh; i++) {
5001             ruhd->ruht = NVME_RUHT_INITIALLY_ISOLATED;
5002             ruhd++;
5003         }
5004     } else {
5005         /* 1 bit for RUH in PIF -> 2 RUHs max. */
5006         hdr->nrg = cpu_to_le16(1);
5007         hdr->nruh = cpu_to_le16(1);
5008         hdr->maxpids = cpu_to_le16(NVME_FDP_MAXPIDS - 1);
5009         hdr->nnss = cpu_to_le32(1);
5010         hdr->runs = cpu_to_le64(96 * MiB);
5011 
5012         ruhd->ruht = NVME_RUHT_INITIALLY_ISOLATED;
5013     }
5014 
5015     return nvme_c2h(n, (uint8_t *)buf + off, trans_len, req);
5016 }
5017 
5018 static uint16_t nvme_fdp_ruh_usage(NvmeCtrl *n, uint32_t endgrpid,
5019                                    uint32_t dw10, uint32_t dw12,
5020                                    uint32_t buf_len, uint64_t off,
5021                                    NvmeRequest *req)
5022 {
5023     NvmeRuHandle *ruh;
5024     NvmeRuhuLog *hdr;
5025     NvmeRuhuDescr *ruhud;
5026     NvmeEnduranceGroup *endgrp;
5027     g_autofree uint8_t *buf = NULL;
5028     uint32_t log_size, trans_len;
5029     uint16_t i;
5030 
5031     if (endgrpid != 1 || !n->subsys) {
5032         return NVME_INVALID_FIELD | NVME_DNR;
5033     }
5034 
5035     endgrp = &n->subsys->endgrp;
5036 
5037     if (!endgrp->fdp.enabled) {
5038         return NVME_FDP_DISABLED | NVME_DNR;
5039     }
5040 
5041     log_size = sizeof(NvmeRuhuLog) + endgrp->fdp.nruh * sizeof(NvmeRuhuDescr);
5042 
5043     if (off >= log_size) {
5044         return NVME_INVALID_FIELD | NVME_DNR;
5045     }
5046 
5047     trans_len = MIN(log_size - off, buf_len);
5048 
5049     buf = g_malloc0(log_size);
5050     hdr = (NvmeRuhuLog *)buf;
5051     ruhud = (NvmeRuhuDescr *)(hdr + 1);
5052 
5053     ruh = endgrp->fdp.ruhs;
5054     hdr->nruh = cpu_to_le16(endgrp->fdp.nruh);
5055 
5056     for (i = 0; i < endgrp->fdp.nruh; i++, ruhud++, ruh++) {
5057         ruhud->ruha = ruh->ruha;
5058     }
5059 
5060     return nvme_c2h(n, (uint8_t *)buf + off, trans_len, req);
5061 }
5062 
5063 static uint16_t nvme_fdp_stats(NvmeCtrl *n, uint32_t endgrpid, uint32_t buf_len,
5064                                uint64_t off, NvmeRequest *req)
5065 {
5066     NvmeEnduranceGroup *endgrp;
5067     NvmeFdpStatsLog log = {};
5068     uint32_t trans_len;
5069 
5070     if (off >= sizeof(NvmeFdpStatsLog)) {
5071         return NVME_INVALID_FIELD | NVME_DNR;
5072     }
5073 
5074     if (endgrpid != 1 || !n->subsys) {
5075         return NVME_INVALID_FIELD | NVME_DNR;
5076     }
5077 
5078     if (!n->subsys->endgrp.fdp.enabled) {
5079         return NVME_FDP_DISABLED | NVME_DNR;
5080     }
5081 
5082     endgrp = &n->subsys->endgrp;
5083 
5084     trans_len = MIN(sizeof(log) - off, buf_len);
5085 
5086     /* spec value is 128 bit, we only use 64 bit */
5087     log.hbmw[0] = cpu_to_le64(endgrp->fdp.hbmw);
5088     log.mbmw[0] = cpu_to_le64(endgrp->fdp.mbmw);
5089     log.mbe[0] = cpu_to_le64(endgrp->fdp.mbe);
5090 
5091     return nvme_c2h(n, (uint8_t *)&log + off, trans_len, req);
5092 }
5093 
5094 static uint16_t nvme_fdp_events(NvmeCtrl *n, uint32_t endgrpid,
5095                                 uint32_t buf_len, uint64_t off,
5096                                 NvmeRequest *req)
5097 {
5098     NvmeEnduranceGroup *endgrp;
5099     NvmeCmd *cmd = &req->cmd;
5100     bool host_events = (cmd->cdw10 >> 8) & 0x1;
5101     uint32_t log_size, trans_len;
5102     NvmeFdpEventBuffer *ebuf;
5103     g_autofree NvmeFdpEventsLog *elog = NULL;
5104     NvmeFdpEvent *event;
5105 
5106     if (endgrpid != 1 || !n->subsys) {
5107         return NVME_INVALID_FIELD | NVME_DNR;
5108     }
5109 
5110     endgrp = &n->subsys->endgrp;
5111 
5112     if (!endgrp->fdp.enabled) {
5113         return NVME_FDP_DISABLED | NVME_DNR;
5114     }
5115 
5116     if (host_events) {
5117         ebuf = &endgrp->fdp.host_events;
5118     } else {
5119         ebuf = &endgrp->fdp.ctrl_events;
5120     }
5121 
5122     log_size = sizeof(NvmeFdpEventsLog) + ebuf->nelems * sizeof(NvmeFdpEvent);
5123 
5124     if (off >= log_size) {
5125         return NVME_INVALID_FIELD | NVME_DNR;
5126     }
5127 
5128     trans_len = MIN(log_size - off, buf_len);
5129     elog = g_malloc0(log_size);
5130     elog->num_events = cpu_to_le32(ebuf->nelems);
5131     event = (NvmeFdpEvent *)(elog + 1);
5132 
5133     if (ebuf->nelems && ebuf->start == ebuf->next) {
5134         unsigned int nelems = (NVME_FDP_MAX_EVENTS - ebuf->start);
5135         /* wrap over, copy [start;NVME_FDP_MAX_EVENTS[ and [0; next[ */
5136         memcpy(event, &ebuf->events[ebuf->start],
5137                sizeof(NvmeFdpEvent) * nelems);
5138         memcpy(event + nelems, ebuf->events,
5139                sizeof(NvmeFdpEvent) * ebuf->next);
5140     } else if (ebuf->start < ebuf->next) {
5141         memcpy(event, &ebuf->events[ebuf->start],
5142                sizeof(NvmeFdpEvent) * (ebuf->next - ebuf->start));
5143     }
5144 
5145     return nvme_c2h(n, (uint8_t *)elog + off, trans_len, req);
5146 }
5147 
5148 static uint16_t nvme_get_log(NvmeCtrl *n, NvmeRequest *req)
5149 {
5150     NvmeCmd *cmd = &req->cmd;
5151 
5152     uint32_t dw10 = le32_to_cpu(cmd->cdw10);
5153     uint32_t dw11 = le32_to_cpu(cmd->cdw11);
5154     uint32_t dw12 = le32_to_cpu(cmd->cdw12);
5155     uint32_t dw13 = le32_to_cpu(cmd->cdw13);
5156     uint8_t  lid = dw10 & 0xff;
5157     uint8_t  lsp = (dw10 >> 8) & 0xf;
5158     uint8_t  rae = (dw10 >> 15) & 0x1;
5159     uint8_t  csi = le32_to_cpu(cmd->cdw14) >> 24;
5160     uint32_t numdl, numdu, lspi;
5161     uint64_t off, lpol, lpou;
5162     size_t   len;
5163     uint16_t status;
5164 
5165     numdl = (dw10 >> 16);
5166     numdu = (dw11 & 0xffff);
5167     lspi = (dw11 >> 16);
5168     lpol = dw12;
5169     lpou = dw13;
5170 
5171     len = (((numdu << 16) | numdl) + 1) << 2;
5172     off = (lpou << 32ULL) | lpol;
5173 
5174     if (off & 0x3) {
5175         return NVME_INVALID_FIELD | NVME_DNR;
5176     }
5177 
5178     trace_pci_nvme_get_log(nvme_cid(req), lid, lsp, rae, len, off);
5179 
5180     status = nvme_check_mdts(n, len);
5181     if (status) {
5182         return status;
5183     }
5184 
5185     switch (lid) {
5186     case NVME_LOG_ERROR_INFO:
5187         return nvme_error_info(n, rae, len, off, req);
5188     case NVME_LOG_SMART_INFO:
5189         return nvme_smart_info(n, rae, len, off, req);
5190     case NVME_LOG_FW_SLOT_INFO:
5191         return nvme_fw_log_info(n, len, off, req);
5192     case NVME_LOG_CHANGED_NSLIST:
5193         return nvme_changed_nslist(n, rae, len, off, req);
5194     case NVME_LOG_CMD_EFFECTS:
5195         return nvme_cmd_effects(n, csi, len, off, req);
5196     case NVME_LOG_ENDGRP:
5197         return nvme_endgrp_info(n, rae, len, off, req);
5198     case NVME_LOG_FDP_CONFS:
5199         return nvme_fdp_confs(n, lspi, len, off, req);
5200     case NVME_LOG_FDP_RUH_USAGE:
5201         return nvme_fdp_ruh_usage(n, lspi, dw10, dw12, len, off, req);
5202     case NVME_LOG_FDP_STATS:
5203         return nvme_fdp_stats(n, lspi, len, off, req);
5204     case NVME_LOG_FDP_EVENTS:
5205         return nvme_fdp_events(n, lspi, len, off, req);
5206     default:
5207         trace_pci_nvme_err_invalid_log_page(nvme_cid(req), lid);
5208         return NVME_INVALID_FIELD | NVME_DNR;
5209     }
5210 }
5211 
5212 static void nvme_free_cq(NvmeCQueue *cq, NvmeCtrl *n)
5213 {
5214     PCIDevice *pci = PCI_DEVICE(n);
5215     uint16_t offset = (cq->cqid << 3) + (1 << 2);
5216 
5217     n->cq[cq->cqid] = NULL;
5218     qemu_bh_delete(cq->bh);
5219     if (cq->ioeventfd_enabled) {
5220         memory_region_del_eventfd(&n->iomem,
5221                                   0x1000 + offset, 4, false, 0, &cq->notifier);
5222         event_notifier_set_handler(&cq->notifier, NULL);
5223         event_notifier_cleanup(&cq->notifier);
5224     }
5225     if (msix_enabled(pci)) {
5226         msix_vector_unuse(pci, cq->vector);
5227     }
5228     if (cq->cqid) {
5229         g_free(cq);
5230     }
5231 }
5232 
5233 static uint16_t nvme_del_cq(NvmeCtrl *n, NvmeRequest *req)
5234 {
5235     NvmeDeleteQ *c = (NvmeDeleteQ *)&req->cmd;
5236     NvmeCQueue *cq;
5237     uint16_t qid = le16_to_cpu(c->qid);
5238 
5239     if (unlikely(!qid || nvme_check_cqid(n, qid))) {
5240         trace_pci_nvme_err_invalid_del_cq_cqid(qid);
5241         return NVME_INVALID_CQID | NVME_DNR;
5242     }
5243 
5244     cq = n->cq[qid];
5245     if (unlikely(!QTAILQ_EMPTY(&cq->sq_list))) {
5246         trace_pci_nvme_err_invalid_del_cq_notempty(qid);
5247         return NVME_INVALID_QUEUE_DEL;
5248     }
5249 
5250     if (cq->irq_enabled && cq->tail != cq->head) {
5251         n->cq_pending--;
5252     }
5253 
5254     nvme_irq_deassert(n, cq);
5255     trace_pci_nvme_del_cq(qid);
5256     nvme_free_cq(cq, n);
5257     return NVME_SUCCESS;
5258 }
5259 
5260 static void nvme_init_cq(NvmeCQueue *cq, NvmeCtrl *n, uint64_t dma_addr,
5261                          uint16_t cqid, uint16_t vector, uint16_t size,
5262                          uint16_t irq_enabled)
5263 {
5264     PCIDevice *pci = PCI_DEVICE(n);
5265 
5266     if (msix_enabled(pci)) {
5267         msix_vector_use(pci, vector);
5268     }
5269     cq->ctrl = n;
5270     cq->cqid = cqid;
5271     cq->size = size;
5272     cq->dma_addr = dma_addr;
5273     cq->phase = 1;
5274     cq->irq_enabled = irq_enabled;
5275     cq->vector = vector;
5276     cq->head = cq->tail = 0;
5277     QTAILQ_INIT(&cq->req_list);
5278     QTAILQ_INIT(&cq->sq_list);
5279     if (n->dbbuf_enabled) {
5280         cq->db_addr = n->dbbuf_dbs + (cqid << 3) + (1 << 2);
5281         cq->ei_addr = n->dbbuf_eis + (cqid << 3) + (1 << 2);
5282 
5283         if (n->params.ioeventfd && cqid != 0) {
5284             if (!nvme_init_cq_ioeventfd(cq)) {
5285                 cq->ioeventfd_enabled = true;
5286             }
5287         }
5288     }
5289     n->cq[cqid] = cq;
5290     cq->bh = qemu_bh_new_guarded(nvme_post_cqes, cq,
5291                                  &DEVICE(cq->ctrl)->mem_reentrancy_guard);
5292 }
5293 
5294 static uint16_t nvme_create_cq(NvmeCtrl *n, NvmeRequest *req)
5295 {
5296     NvmeCQueue *cq;
5297     NvmeCreateCq *c = (NvmeCreateCq *)&req->cmd;
5298     uint16_t cqid = le16_to_cpu(c->cqid);
5299     uint16_t vector = le16_to_cpu(c->irq_vector);
5300     uint16_t qsize = le16_to_cpu(c->qsize);
5301     uint16_t qflags = le16_to_cpu(c->cq_flags);
5302     uint64_t prp1 = le64_to_cpu(c->prp1);
5303     uint32_t cc = ldq_le_p(&n->bar.cc);
5304     uint8_t iocqes = NVME_CC_IOCQES(cc);
5305     uint8_t iosqes = NVME_CC_IOSQES(cc);
5306 
5307     trace_pci_nvme_create_cq(prp1, cqid, vector, qsize, qflags,
5308                              NVME_CQ_FLAGS_IEN(qflags) != 0);
5309 
5310     if (iosqes != NVME_SQES || iocqes != NVME_CQES) {
5311         trace_pci_nvme_err_invalid_create_cq_entry_size(iosqes, iocqes);
5312         return NVME_MAX_QSIZE_EXCEEDED | NVME_DNR;
5313     }
5314 
5315     if (unlikely(!cqid || cqid > n->conf_ioqpairs || n->cq[cqid] != NULL)) {
5316         trace_pci_nvme_err_invalid_create_cq_cqid(cqid);
5317         return NVME_INVALID_QID | NVME_DNR;
5318     }
5319     if (unlikely(!qsize || qsize > NVME_CAP_MQES(ldq_le_p(&n->bar.cap)))) {
5320         trace_pci_nvme_err_invalid_create_cq_size(qsize);
5321         return NVME_MAX_QSIZE_EXCEEDED | NVME_DNR;
5322     }
5323     if (unlikely(prp1 & (n->page_size - 1))) {
5324         trace_pci_nvme_err_invalid_create_cq_addr(prp1);
5325         return NVME_INVALID_PRP_OFFSET | NVME_DNR;
5326     }
5327     if (unlikely(!msix_enabled(PCI_DEVICE(n)) && vector)) {
5328         trace_pci_nvme_err_invalid_create_cq_vector(vector);
5329         return NVME_INVALID_IRQ_VECTOR | NVME_DNR;
5330     }
5331     if (unlikely(vector >= n->conf_msix_qsize)) {
5332         trace_pci_nvme_err_invalid_create_cq_vector(vector);
5333         return NVME_INVALID_IRQ_VECTOR | NVME_DNR;
5334     }
5335     if (unlikely(!(NVME_CQ_FLAGS_PC(qflags)))) {
5336         trace_pci_nvme_err_invalid_create_cq_qflags(NVME_CQ_FLAGS_PC(qflags));
5337         return NVME_INVALID_FIELD | NVME_DNR;
5338     }
5339 
5340     cq = g_malloc0(sizeof(*cq));
5341     nvme_init_cq(cq, n, prp1, cqid, vector, qsize + 1,
5342                  NVME_CQ_FLAGS_IEN(qflags));
5343 
5344     /*
5345      * It is only required to set qs_created when creating a completion queue;
5346      * creating a submission queue without a matching completion queue will
5347      * fail.
5348      */
5349     n->qs_created = true;
5350     return NVME_SUCCESS;
5351 }
5352 
5353 static uint16_t nvme_rpt_empty_id_struct(NvmeCtrl *n, NvmeRequest *req)
5354 {
5355     uint8_t id[NVME_IDENTIFY_DATA_SIZE] = {};
5356 
5357     return nvme_c2h(n, id, sizeof(id), req);
5358 }
5359 
5360 static uint16_t nvme_identify_ctrl(NvmeCtrl *n, NvmeRequest *req)
5361 {
5362     trace_pci_nvme_identify_ctrl();
5363 
5364     return nvme_c2h(n, (uint8_t *)&n->id_ctrl, sizeof(n->id_ctrl), req);
5365 }
5366 
5367 static uint16_t nvme_identify_ctrl_csi(NvmeCtrl *n, NvmeRequest *req)
5368 {
5369     NvmeIdentify *c = (NvmeIdentify *)&req->cmd;
5370     uint8_t id[NVME_IDENTIFY_DATA_SIZE] = {};
5371     NvmeIdCtrlNvm *id_nvm = (NvmeIdCtrlNvm *)&id;
5372 
5373     trace_pci_nvme_identify_ctrl_csi(c->csi);
5374 
5375     switch (c->csi) {
5376     case NVME_CSI_NVM:
5377         id_nvm->vsl = n->params.vsl;
5378         id_nvm->dmrsl = cpu_to_le32(n->dmrsl);
5379         break;
5380 
5381     case NVME_CSI_ZONED:
5382         ((NvmeIdCtrlZoned *)&id)->zasl = n->params.zasl;
5383         break;
5384 
5385     default:
5386         return NVME_INVALID_FIELD | NVME_DNR;
5387     }
5388 
5389     return nvme_c2h(n, id, sizeof(id), req);
5390 }
5391 
5392 static uint16_t nvme_identify_ns(NvmeCtrl *n, NvmeRequest *req, bool active)
5393 {
5394     NvmeNamespace *ns;
5395     NvmeIdentify *c = (NvmeIdentify *)&req->cmd;
5396     uint32_t nsid = le32_to_cpu(c->nsid);
5397 
5398     trace_pci_nvme_identify_ns(nsid);
5399 
5400     if (!nvme_nsid_valid(n, nsid) || nsid == NVME_NSID_BROADCAST) {
5401         return NVME_INVALID_NSID | NVME_DNR;
5402     }
5403 
5404     ns = nvme_ns(n, nsid);
5405     if (unlikely(!ns)) {
5406         if (!active) {
5407             ns = nvme_subsys_ns(n->subsys, nsid);
5408             if (!ns) {
5409                 return nvme_rpt_empty_id_struct(n, req);
5410             }
5411         } else {
5412             return nvme_rpt_empty_id_struct(n, req);
5413         }
5414     }
5415 
5416     if (active || ns->csi == NVME_CSI_NVM) {
5417         return nvme_c2h(n, (uint8_t *)&ns->id_ns, sizeof(NvmeIdNs), req);
5418     }
5419 
5420     return NVME_INVALID_CMD_SET | NVME_DNR;
5421 }
5422 
5423 static uint16_t nvme_identify_ctrl_list(NvmeCtrl *n, NvmeRequest *req,
5424                                         bool attached)
5425 {
5426     NvmeIdentify *c = (NvmeIdentify *)&req->cmd;
5427     uint32_t nsid = le32_to_cpu(c->nsid);
5428     uint16_t min_id = le16_to_cpu(c->ctrlid);
5429     uint16_t list[NVME_CONTROLLER_LIST_SIZE] = {};
5430     uint16_t *ids = &list[1];
5431     NvmeNamespace *ns;
5432     NvmeCtrl *ctrl;
5433     int cntlid, nr_ids = 0;
5434 
5435     trace_pci_nvme_identify_ctrl_list(c->cns, min_id);
5436 
5437     if (!n->subsys) {
5438         return NVME_INVALID_FIELD | NVME_DNR;
5439     }
5440 
5441     if (attached) {
5442         if (nsid == NVME_NSID_BROADCAST) {
5443             return NVME_INVALID_FIELD | NVME_DNR;
5444         }
5445 
5446         ns = nvme_subsys_ns(n->subsys, nsid);
5447         if (!ns) {
5448             return NVME_INVALID_FIELD | NVME_DNR;
5449         }
5450     }
5451 
5452     for (cntlid = min_id; cntlid < ARRAY_SIZE(n->subsys->ctrls); cntlid++) {
5453         ctrl = nvme_subsys_ctrl(n->subsys, cntlid);
5454         if (!ctrl) {
5455             continue;
5456         }
5457 
5458         if (attached && !nvme_ns(ctrl, nsid)) {
5459             continue;
5460         }
5461 
5462         ids[nr_ids++] = cntlid;
5463     }
5464 
5465     list[0] = nr_ids;
5466 
5467     return nvme_c2h(n, (uint8_t *)list, sizeof(list), req);
5468 }
5469 
5470 static uint16_t nvme_identify_pri_ctrl_cap(NvmeCtrl *n, NvmeRequest *req)
5471 {
5472     trace_pci_nvme_identify_pri_ctrl_cap(le16_to_cpu(n->pri_ctrl_cap.cntlid));
5473 
5474     return nvme_c2h(n, (uint8_t *)&n->pri_ctrl_cap,
5475                     sizeof(NvmePriCtrlCap), req);
5476 }
5477 
5478 static uint16_t nvme_identify_sec_ctrl_list(NvmeCtrl *n, NvmeRequest *req)
5479 {
5480     NvmeIdentify *c = (NvmeIdentify *)&req->cmd;
5481     uint16_t pri_ctrl_id = le16_to_cpu(n->pri_ctrl_cap.cntlid);
5482     uint16_t min_id = le16_to_cpu(c->ctrlid);
5483     uint8_t num_sec_ctrl = n->sec_ctrl_list.numcntl;
5484     NvmeSecCtrlList list = {0};
5485     uint8_t i;
5486 
5487     for (i = 0; i < num_sec_ctrl; i++) {
5488         if (n->sec_ctrl_list.sec[i].scid >= min_id) {
5489             list.numcntl = num_sec_ctrl - i;
5490             memcpy(&list.sec, n->sec_ctrl_list.sec + i,
5491                    list.numcntl * sizeof(NvmeSecCtrlEntry));
5492             break;
5493         }
5494     }
5495 
5496     trace_pci_nvme_identify_sec_ctrl_list(pri_ctrl_id, list.numcntl);
5497 
5498     return nvme_c2h(n, (uint8_t *)&list, sizeof(list), req);
5499 }
5500 
5501 static uint16_t nvme_identify_ns_csi(NvmeCtrl *n, NvmeRequest *req,
5502                                      bool active)
5503 {
5504     NvmeNamespace *ns;
5505     NvmeIdentify *c = (NvmeIdentify *)&req->cmd;
5506     uint32_t nsid = le32_to_cpu(c->nsid);
5507 
5508     trace_pci_nvme_identify_ns_csi(nsid, c->csi);
5509 
5510     if (!nvme_nsid_valid(n, nsid) || nsid == NVME_NSID_BROADCAST) {
5511         return NVME_INVALID_NSID | NVME_DNR;
5512     }
5513 
5514     ns = nvme_ns(n, nsid);
5515     if (unlikely(!ns)) {
5516         if (!active) {
5517             ns = nvme_subsys_ns(n->subsys, nsid);
5518             if (!ns) {
5519                 return nvme_rpt_empty_id_struct(n, req);
5520             }
5521         } else {
5522             return nvme_rpt_empty_id_struct(n, req);
5523         }
5524     }
5525 
5526     if (c->csi == NVME_CSI_NVM) {
5527         return nvme_c2h(n, (uint8_t *)&ns->id_ns_nvm, sizeof(NvmeIdNsNvm),
5528                         req);
5529     } else if (c->csi == NVME_CSI_ZONED && ns->csi == NVME_CSI_ZONED) {
5530         return nvme_c2h(n, (uint8_t *)ns->id_ns_zoned, sizeof(NvmeIdNsZoned),
5531                         req);
5532     }
5533 
5534     return NVME_INVALID_FIELD | NVME_DNR;
5535 }
5536 
5537 static uint16_t nvme_identify_nslist(NvmeCtrl *n, NvmeRequest *req,
5538                                      bool active)
5539 {
5540     NvmeNamespace *ns;
5541     NvmeIdentify *c = (NvmeIdentify *)&req->cmd;
5542     uint32_t min_nsid = le32_to_cpu(c->nsid);
5543     uint8_t list[NVME_IDENTIFY_DATA_SIZE] = {};
5544     static const int data_len = sizeof(list);
5545     uint32_t *list_ptr = (uint32_t *)list;
5546     int i, j = 0;
5547 
5548     trace_pci_nvme_identify_nslist(min_nsid);
5549 
5550     /*
5551      * Both FFFFFFFFh (NVME_NSID_BROADCAST) and FFFFFFFFEh are invalid values
5552      * since the Active Namespace ID List should return namespaces with ids
5553      * *higher* than the NSID specified in the command. This is also specified
5554      * in the spec (NVM Express v1.3d, Section 5.15.4).
5555      */
5556     if (min_nsid >= NVME_NSID_BROADCAST - 1) {
5557         return NVME_INVALID_NSID | NVME_DNR;
5558     }
5559 
5560     for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
5561         ns = nvme_ns(n, i);
5562         if (!ns) {
5563             if (!active) {
5564                 ns = nvme_subsys_ns(n->subsys, i);
5565                 if (!ns) {
5566                     continue;
5567                 }
5568             } else {
5569                 continue;
5570             }
5571         }
5572         if (ns->params.nsid <= min_nsid) {
5573             continue;
5574         }
5575         list_ptr[j++] = cpu_to_le32(ns->params.nsid);
5576         if (j == data_len / sizeof(uint32_t)) {
5577             break;
5578         }
5579     }
5580 
5581     return nvme_c2h(n, list, data_len, req);
5582 }
5583 
5584 static uint16_t nvme_identify_nslist_csi(NvmeCtrl *n, NvmeRequest *req,
5585                                          bool active)
5586 {
5587     NvmeNamespace *ns;
5588     NvmeIdentify *c = (NvmeIdentify *)&req->cmd;
5589     uint32_t min_nsid = le32_to_cpu(c->nsid);
5590     uint8_t list[NVME_IDENTIFY_DATA_SIZE] = {};
5591     static const int data_len = sizeof(list);
5592     uint32_t *list_ptr = (uint32_t *)list;
5593     int i, j = 0;
5594 
5595     trace_pci_nvme_identify_nslist_csi(min_nsid, c->csi);
5596 
5597     /*
5598      * Same as in nvme_identify_nslist(), FFFFFFFFh/FFFFFFFFEh are invalid.
5599      */
5600     if (min_nsid >= NVME_NSID_BROADCAST - 1) {
5601         return NVME_INVALID_NSID | NVME_DNR;
5602     }
5603 
5604     if (c->csi != NVME_CSI_NVM && c->csi != NVME_CSI_ZONED) {
5605         return NVME_INVALID_FIELD | NVME_DNR;
5606     }
5607 
5608     for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
5609         ns = nvme_ns(n, i);
5610         if (!ns) {
5611             if (!active) {
5612                 ns = nvme_subsys_ns(n->subsys, i);
5613                 if (!ns) {
5614                     continue;
5615                 }
5616             } else {
5617                 continue;
5618             }
5619         }
5620         if (ns->params.nsid <= min_nsid || c->csi != ns->csi) {
5621             continue;
5622         }
5623         list_ptr[j++] = cpu_to_le32(ns->params.nsid);
5624         if (j == data_len / sizeof(uint32_t)) {
5625             break;
5626         }
5627     }
5628 
5629     return nvme_c2h(n, list, data_len, req);
5630 }
5631 
5632 static uint16_t nvme_identify_ns_descr_list(NvmeCtrl *n, NvmeRequest *req)
5633 {
5634     NvmeNamespace *ns;
5635     NvmeIdentify *c = (NvmeIdentify *)&req->cmd;
5636     uint32_t nsid = le32_to_cpu(c->nsid);
5637     uint8_t list[NVME_IDENTIFY_DATA_SIZE] = {};
5638     uint8_t *pos = list;
5639     struct {
5640         NvmeIdNsDescr hdr;
5641         uint8_t v[NVME_NIDL_UUID];
5642     } QEMU_PACKED uuid = {};
5643     struct {
5644         NvmeIdNsDescr hdr;
5645         uint8_t v[NVME_NIDL_NGUID];
5646     } QEMU_PACKED nguid = {};
5647     struct {
5648         NvmeIdNsDescr hdr;
5649         uint64_t v;
5650     } QEMU_PACKED eui64 = {};
5651     struct {
5652         NvmeIdNsDescr hdr;
5653         uint8_t v;
5654     } QEMU_PACKED csi = {};
5655 
5656     trace_pci_nvme_identify_ns_descr_list(nsid);
5657 
5658     if (!nvme_nsid_valid(n, nsid) || nsid == NVME_NSID_BROADCAST) {
5659         return NVME_INVALID_NSID | NVME_DNR;
5660     }
5661 
5662     ns = nvme_ns(n, nsid);
5663     if (unlikely(!ns)) {
5664         return NVME_INVALID_FIELD | NVME_DNR;
5665     }
5666 
5667     if (!qemu_uuid_is_null(&ns->params.uuid)) {
5668         uuid.hdr.nidt = NVME_NIDT_UUID;
5669         uuid.hdr.nidl = NVME_NIDL_UUID;
5670         memcpy(uuid.v, ns->params.uuid.data, NVME_NIDL_UUID);
5671         memcpy(pos, &uuid, sizeof(uuid));
5672         pos += sizeof(uuid);
5673     }
5674 
5675     if (!nvme_nguid_is_null(&ns->params.nguid)) {
5676         nguid.hdr.nidt = NVME_NIDT_NGUID;
5677         nguid.hdr.nidl = NVME_NIDL_NGUID;
5678         memcpy(nguid.v, ns->params.nguid.data, NVME_NIDL_NGUID);
5679         memcpy(pos, &nguid, sizeof(nguid));
5680         pos += sizeof(nguid);
5681     }
5682 
5683     if (ns->params.eui64) {
5684         eui64.hdr.nidt = NVME_NIDT_EUI64;
5685         eui64.hdr.nidl = NVME_NIDL_EUI64;
5686         eui64.v = cpu_to_be64(ns->params.eui64);
5687         memcpy(pos, &eui64, sizeof(eui64));
5688         pos += sizeof(eui64);
5689     }
5690 
5691     csi.hdr.nidt = NVME_NIDT_CSI;
5692     csi.hdr.nidl = NVME_NIDL_CSI;
5693     csi.v = ns->csi;
5694     memcpy(pos, &csi, sizeof(csi));
5695     pos += sizeof(csi);
5696 
5697     return nvme_c2h(n, list, sizeof(list), req);
5698 }
5699 
5700 static uint16_t nvme_identify_cmd_set(NvmeCtrl *n, NvmeRequest *req)
5701 {
5702     uint8_t list[NVME_IDENTIFY_DATA_SIZE] = {};
5703     static const int data_len = sizeof(list);
5704 
5705     trace_pci_nvme_identify_cmd_set();
5706 
5707     NVME_SET_CSI(*list, NVME_CSI_NVM);
5708     NVME_SET_CSI(*list, NVME_CSI_ZONED);
5709 
5710     return nvme_c2h(n, list, data_len, req);
5711 }
5712 
5713 static uint16_t nvme_identify(NvmeCtrl *n, NvmeRequest *req)
5714 {
5715     NvmeIdentify *c = (NvmeIdentify *)&req->cmd;
5716 
5717     trace_pci_nvme_identify(nvme_cid(req), c->cns, le16_to_cpu(c->ctrlid),
5718                             c->csi);
5719 
5720     switch (c->cns) {
5721     case NVME_ID_CNS_NS:
5722         return nvme_identify_ns(n, req, true);
5723     case NVME_ID_CNS_NS_PRESENT:
5724         return nvme_identify_ns(n, req, false);
5725     case NVME_ID_CNS_NS_ATTACHED_CTRL_LIST:
5726         return nvme_identify_ctrl_list(n, req, true);
5727     case NVME_ID_CNS_CTRL_LIST:
5728         return nvme_identify_ctrl_list(n, req, false);
5729     case NVME_ID_CNS_PRIMARY_CTRL_CAP:
5730         return nvme_identify_pri_ctrl_cap(n, req);
5731     case NVME_ID_CNS_SECONDARY_CTRL_LIST:
5732         return nvme_identify_sec_ctrl_list(n, req);
5733     case NVME_ID_CNS_CS_NS:
5734         return nvme_identify_ns_csi(n, req, true);
5735     case NVME_ID_CNS_CS_NS_PRESENT:
5736         return nvme_identify_ns_csi(n, req, false);
5737     case NVME_ID_CNS_CTRL:
5738         return nvme_identify_ctrl(n, req);
5739     case NVME_ID_CNS_CS_CTRL:
5740         return nvme_identify_ctrl_csi(n, req);
5741     case NVME_ID_CNS_NS_ACTIVE_LIST:
5742         return nvme_identify_nslist(n, req, true);
5743     case NVME_ID_CNS_NS_PRESENT_LIST:
5744         return nvme_identify_nslist(n, req, false);
5745     case NVME_ID_CNS_CS_NS_ACTIVE_LIST:
5746         return nvme_identify_nslist_csi(n, req, true);
5747     case NVME_ID_CNS_CS_NS_PRESENT_LIST:
5748         return nvme_identify_nslist_csi(n, req, false);
5749     case NVME_ID_CNS_NS_DESCR_LIST:
5750         return nvme_identify_ns_descr_list(n, req);
5751     case NVME_ID_CNS_IO_COMMAND_SET:
5752         return nvme_identify_cmd_set(n, req);
5753     default:
5754         trace_pci_nvme_err_invalid_identify_cns(le32_to_cpu(c->cns));
5755         return NVME_INVALID_FIELD | NVME_DNR;
5756     }
5757 }
5758 
5759 static uint16_t nvme_abort(NvmeCtrl *n, NvmeRequest *req)
5760 {
5761     uint16_t sqid = le32_to_cpu(req->cmd.cdw10) & 0xffff;
5762 
5763     req->cqe.result = 1;
5764     if (nvme_check_sqid(n, sqid)) {
5765         return NVME_INVALID_FIELD | NVME_DNR;
5766     }
5767 
5768     return NVME_SUCCESS;
5769 }
5770 
5771 static inline void nvme_set_timestamp(NvmeCtrl *n, uint64_t ts)
5772 {
5773     trace_pci_nvme_setfeat_timestamp(ts);
5774 
5775     n->host_timestamp = le64_to_cpu(ts);
5776     n->timestamp_set_qemu_clock_ms = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
5777 }
5778 
5779 static inline uint64_t nvme_get_timestamp(const NvmeCtrl *n)
5780 {
5781     uint64_t current_time = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
5782     uint64_t elapsed_time = current_time - n->timestamp_set_qemu_clock_ms;
5783 
5784     union nvme_timestamp {
5785         struct {
5786             uint64_t timestamp:48;
5787             uint64_t sync:1;
5788             uint64_t origin:3;
5789             uint64_t rsvd1:12;
5790         };
5791         uint64_t all;
5792     };
5793 
5794     union nvme_timestamp ts;
5795     ts.all = 0;
5796     ts.timestamp = n->host_timestamp + elapsed_time;
5797 
5798     /* If the host timestamp is non-zero, set the timestamp origin */
5799     ts.origin = n->host_timestamp ? 0x01 : 0x00;
5800 
5801     trace_pci_nvme_getfeat_timestamp(ts.all);
5802 
5803     return cpu_to_le64(ts.all);
5804 }
5805 
5806 static uint16_t nvme_get_feature_timestamp(NvmeCtrl *n, NvmeRequest *req)
5807 {
5808     uint64_t timestamp = nvme_get_timestamp(n);
5809 
5810     return nvme_c2h(n, (uint8_t *)&timestamp, sizeof(timestamp), req);
5811 }
5812 
5813 static int nvme_get_feature_fdp(NvmeCtrl *n, uint32_t endgrpid,
5814                                 uint32_t *result)
5815 {
5816     *result = 0;
5817 
5818     if (!n->subsys || !n->subsys->endgrp.fdp.enabled) {
5819         return NVME_INVALID_FIELD | NVME_DNR;
5820     }
5821 
5822     *result = FIELD_DP16(0, FEAT_FDP, FDPE, 1);
5823     *result = FIELD_DP16(*result, FEAT_FDP, CONF_NDX, 0);
5824 
5825     return NVME_SUCCESS;
5826 }
5827 
5828 static uint16_t nvme_get_feature_fdp_events(NvmeCtrl *n, NvmeNamespace *ns,
5829                                             NvmeRequest *req, uint32_t *result)
5830 {
5831     NvmeCmd *cmd = &req->cmd;
5832     uint32_t cdw11 = le32_to_cpu(cmd->cdw11);
5833     uint16_t ph = cdw11 & 0xffff;
5834     uint8_t noet = (cdw11 >> 16) & 0xff;
5835     uint16_t ruhid, ret;
5836     uint32_t nentries = 0;
5837     uint8_t s_events_ndx = 0;
5838     size_t s_events_siz = sizeof(NvmeFdpEventDescr) * noet;
5839     g_autofree NvmeFdpEventDescr *s_events = g_malloc0(s_events_siz);
5840     NvmeRuHandle *ruh;
5841     NvmeFdpEventDescr *s_event;
5842 
5843     if (!n->subsys || !n->subsys->endgrp.fdp.enabled) {
5844         return NVME_FDP_DISABLED | NVME_DNR;
5845     }
5846 
5847     if (!nvme_ph_valid(ns, ph)) {
5848         return NVME_INVALID_FIELD | NVME_DNR;
5849     }
5850 
5851     ruhid = ns->fdp.phs[ph];
5852     ruh = &n->subsys->endgrp.fdp.ruhs[ruhid];
5853 
5854     assert(ruh);
5855 
5856     if (unlikely(noet == 0)) {
5857         return NVME_INVALID_FIELD | NVME_DNR;
5858     }
5859 
5860     for (uint8_t event_type = 0; event_type < FDP_EVT_MAX; event_type++) {
5861         uint8_t shift = nvme_fdp_evf_shifts[event_type];
5862         if (!shift && event_type) {
5863             /*
5864              * only first entry (event_type == 0) has a shift value of 0
5865              * other entries are simply unpopulated.
5866              */
5867             continue;
5868         }
5869 
5870         nentries++;
5871 
5872         s_event = &s_events[s_events_ndx];
5873         s_event->evt = event_type;
5874         s_event->evta = (ruh->event_filter >> shift) & 0x1;
5875 
5876         /* break if all `noet` entries are filled */
5877         if ((++s_events_ndx) == noet) {
5878             break;
5879         }
5880     }
5881 
5882     ret = nvme_c2h(n, s_events, s_events_siz, req);
5883     if (ret) {
5884         return ret;
5885     }
5886 
5887     *result = nentries;
5888     return NVME_SUCCESS;
5889 }
5890 
5891 static uint16_t nvme_get_feature(NvmeCtrl *n, NvmeRequest *req)
5892 {
5893     NvmeCmd *cmd = &req->cmd;
5894     uint32_t dw10 = le32_to_cpu(cmd->cdw10);
5895     uint32_t dw11 = le32_to_cpu(cmd->cdw11);
5896     uint32_t nsid = le32_to_cpu(cmd->nsid);
5897     uint32_t result = 0;
5898     uint8_t fid = NVME_GETSETFEAT_FID(dw10);
5899     NvmeGetFeatureSelect sel = NVME_GETFEAT_SELECT(dw10);
5900     uint16_t iv;
5901     NvmeNamespace *ns;
5902     int i;
5903     uint16_t endgrpid = 0, ret = NVME_SUCCESS;
5904 
5905     static const uint32_t nvme_feature_default[NVME_FID_MAX] = {
5906         [NVME_ARBITRATION] = NVME_ARB_AB_NOLIMIT,
5907     };
5908 
5909     trace_pci_nvme_getfeat(nvme_cid(req), nsid, fid, sel, dw11);
5910 
5911     if (!nvme_feature_support[fid]) {
5912         return NVME_INVALID_FIELD | NVME_DNR;
5913     }
5914 
5915     if (nvme_feature_cap[fid] & NVME_FEAT_CAP_NS) {
5916         if (!nvme_nsid_valid(n, nsid) || nsid == NVME_NSID_BROADCAST) {
5917             /*
5918              * The Reservation Notification Mask and Reservation Persistence
5919              * features require a status code of Invalid Field in Command when
5920              * NSID is FFFFFFFFh. Since the device does not support those
5921              * features we can always return Invalid Namespace or Format as we
5922              * should do for all other features.
5923              */
5924             return NVME_INVALID_NSID | NVME_DNR;
5925         }
5926 
5927         if (!nvme_ns(n, nsid)) {
5928             return NVME_INVALID_FIELD | NVME_DNR;
5929         }
5930     }
5931 
5932     switch (sel) {
5933     case NVME_GETFEAT_SELECT_CURRENT:
5934         break;
5935     case NVME_GETFEAT_SELECT_SAVED:
5936         /* no features are saveable by the controller; fallthrough */
5937     case NVME_GETFEAT_SELECT_DEFAULT:
5938         goto defaults;
5939     case NVME_GETFEAT_SELECT_CAP:
5940         result = nvme_feature_cap[fid];
5941         goto out;
5942     }
5943 
5944     switch (fid) {
5945     case NVME_TEMPERATURE_THRESHOLD:
5946         result = 0;
5947 
5948         /*
5949          * The controller only implements the Composite Temperature sensor, so
5950          * return 0 for all other sensors.
5951          */
5952         if (NVME_TEMP_TMPSEL(dw11) != NVME_TEMP_TMPSEL_COMPOSITE) {
5953             goto out;
5954         }
5955 
5956         switch (NVME_TEMP_THSEL(dw11)) {
5957         case NVME_TEMP_THSEL_OVER:
5958             result = n->features.temp_thresh_hi;
5959             goto out;
5960         case NVME_TEMP_THSEL_UNDER:
5961             result = n->features.temp_thresh_low;
5962             goto out;
5963         }
5964 
5965         return NVME_INVALID_FIELD | NVME_DNR;
5966     case NVME_ERROR_RECOVERY:
5967         if (!nvme_nsid_valid(n, nsid)) {
5968             return NVME_INVALID_NSID | NVME_DNR;
5969         }
5970 
5971         ns = nvme_ns(n, nsid);
5972         if (unlikely(!ns)) {
5973             return NVME_INVALID_FIELD | NVME_DNR;
5974         }
5975 
5976         result = ns->features.err_rec;
5977         goto out;
5978     case NVME_VOLATILE_WRITE_CACHE:
5979         result = 0;
5980         for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
5981             ns = nvme_ns(n, i);
5982             if (!ns) {
5983                 continue;
5984             }
5985 
5986             result = blk_enable_write_cache(ns->blkconf.blk);
5987             if (result) {
5988                 break;
5989             }
5990         }
5991         trace_pci_nvme_getfeat_vwcache(result ? "enabled" : "disabled");
5992         goto out;
5993     case NVME_ASYNCHRONOUS_EVENT_CONF:
5994         result = n->features.async_config;
5995         goto out;
5996     case NVME_TIMESTAMP:
5997         return nvme_get_feature_timestamp(n, req);
5998     case NVME_HOST_BEHAVIOR_SUPPORT:
5999         return nvme_c2h(n, (uint8_t *)&n->features.hbs,
6000                         sizeof(n->features.hbs), req);
6001     case NVME_FDP_MODE:
6002         endgrpid = dw11 & 0xff;
6003 
6004         if (endgrpid != 0x1) {
6005             return NVME_INVALID_FIELD | NVME_DNR;
6006         }
6007 
6008         ret = nvme_get_feature_fdp(n, endgrpid, &result);
6009         if (ret) {
6010             return ret;
6011         }
6012         goto out;
6013     case NVME_FDP_EVENTS:
6014         if (!nvme_nsid_valid(n, nsid)) {
6015             return NVME_INVALID_NSID | NVME_DNR;
6016         }
6017 
6018         ns = nvme_ns(n, nsid);
6019         if (unlikely(!ns)) {
6020             return NVME_INVALID_FIELD | NVME_DNR;
6021         }
6022 
6023         ret = nvme_get_feature_fdp_events(n, ns, req, &result);
6024         if (ret) {
6025             return ret;
6026         }
6027         goto out;
6028     default:
6029         break;
6030     }
6031 
6032 defaults:
6033     switch (fid) {
6034     case NVME_TEMPERATURE_THRESHOLD:
6035         result = 0;
6036 
6037         if (NVME_TEMP_TMPSEL(dw11) != NVME_TEMP_TMPSEL_COMPOSITE) {
6038             break;
6039         }
6040 
6041         if (NVME_TEMP_THSEL(dw11) == NVME_TEMP_THSEL_OVER) {
6042             result = NVME_TEMPERATURE_WARNING;
6043         }
6044 
6045         break;
6046     case NVME_NUMBER_OF_QUEUES:
6047         result = (n->conf_ioqpairs - 1) | ((n->conf_ioqpairs - 1) << 16);
6048         trace_pci_nvme_getfeat_numq(result);
6049         break;
6050     case NVME_INTERRUPT_VECTOR_CONF:
6051         iv = dw11 & 0xffff;
6052         if (iv >= n->conf_ioqpairs + 1) {
6053             return NVME_INVALID_FIELD | NVME_DNR;
6054         }
6055 
6056         result = iv;
6057         if (iv == n->admin_cq.vector) {
6058             result |= NVME_INTVC_NOCOALESCING;
6059         }
6060         break;
6061     case NVME_FDP_MODE:
6062         endgrpid = dw11 & 0xff;
6063 
6064         if (endgrpid != 0x1) {
6065             return NVME_INVALID_FIELD | NVME_DNR;
6066         }
6067 
6068         ret = nvme_get_feature_fdp(n, endgrpid, &result);
6069         if (ret) {
6070             return ret;
6071         }
6072         goto out;
6073 
6074         break;
6075     default:
6076         result = nvme_feature_default[fid];
6077         break;
6078     }
6079 
6080 out:
6081     req->cqe.result = cpu_to_le32(result);
6082     return ret;
6083 }
6084 
6085 static uint16_t nvme_set_feature_timestamp(NvmeCtrl *n, NvmeRequest *req)
6086 {
6087     uint16_t ret;
6088     uint64_t timestamp;
6089 
6090     ret = nvme_h2c(n, (uint8_t *)&timestamp, sizeof(timestamp), req);
6091     if (ret) {
6092         return ret;
6093     }
6094 
6095     nvme_set_timestamp(n, timestamp);
6096 
6097     return NVME_SUCCESS;
6098 }
6099 
6100 static uint16_t nvme_set_feature_fdp_events(NvmeCtrl *n, NvmeNamespace *ns,
6101                                             NvmeRequest *req)
6102 {
6103     NvmeCmd *cmd = &req->cmd;
6104     uint32_t cdw11 = le32_to_cpu(cmd->cdw11);
6105     uint16_t ph = cdw11 & 0xffff;
6106     uint8_t noet = (cdw11 >> 16) & 0xff;
6107     uint16_t ret, ruhid;
6108     uint8_t enable = le32_to_cpu(cmd->cdw12) & 0x1;
6109     uint8_t event_mask = 0;
6110     unsigned int i;
6111     g_autofree uint8_t *events = g_malloc0(noet);
6112     NvmeRuHandle *ruh = NULL;
6113 
6114     assert(ns);
6115 
6116     if (!n->subsys || !n->subsys->endgrp.fdp.enabled) {
6117         return NVME_FDP_DISABLED | NVME_DNR;
6118     }
6119 
6120     if (!nvme_ph_valid(ns, ph)) {
6121         return NVME_INVALID_FIELD | NVME_DNR;
6122     }
6123 
6124     ruhid = ns->fdp.phs[ph];
6125     ruh = &n->subsys->endgrp.fdp.ruhs[ruhid];
6126 
6127     ret = nvme_h2c(n, events, noet, req);
6128     if (ret) {
6129         return ret;
6130     }
6131 
6132     for (i = 0; i < noet; i++) {
6133         event_mask |= (1 << nvme_fdp_evf_shifts[events[i]]);
6134     }
6135 
6136     if (enable) {
6137         ruh->event_filter |= event_mask;
6138     } else {
6139         ruh->event_filter = ruh->event_filter & ~event_mask;
6140     }
6141 
6142     return NVME_SUCCESS;
6143 }
6144 
6145 static uint16_t nvme_set_feature(NvmeCtrl *n, NvmeRequest *req)
6146 {
6147     NvmeNamespace *ns = NULL;
6148 
6149     NvmeCmd *cmd = &req->cmd;
6150     uint32_t dw10 = le32_to_cpu(cmd->cdw10);
6151     uint32_t dw11 = le32_to_cpu(cmd->cdw11);
6152     uint32_t nsid = le32_to_cpu(cmd->nsid);
6153     uint8_t fid = NVME_GETSETFEAT_FID(dw10);
6154     uint8_t save = NVME_SETFEAT_SAVE(dw10);
6155     uint16_t status;
6156     int i;
6157 
6158     trace_pci_nvme_setfeat(nvme_cid(req), nsid, fid, save, dw11);
6159 
6160     if (save && !(nvme_feature_cap[fid] & NVME_FEAT_CAP_SAVE)) {
6161         return NVME_FID_NOT_SAVEABLE | NVME_DNR;
6162     }
6163 
6164     if (!nvme_feature_support[fid]) {
6165         return NVME_INVALID_FIELD | NVME_DNR;
6166     }
6167 
6168     if (nvme_feature_cap[fid] & NVME_FEAT_CAP_NS) {
6169         if (nsid != NVME_NSID_BROADCAST) {
6170             if (!nvme_nsid_valid(n, nsid)) {
6171                 return NVME_INVALID_NSID | NVME_DNR;
6172             }
6173 
6174             ns = nvme_ns(n, nsid);
6175             if (unlikely(!ns)) {
6176                 return NVME_INVALID_FIELD | NVME_DNR;
6177             }
6178         }
6179     } else if (nsid && nsid != NVME_NSID_BROADCAST) {
6180         if (!nvme_nsid_valid(n, nsid)) {
6181             return NVME_INVALID_NSID | NVME_DNR;
6182         }
6183 
6184         return NVME_FEAT_NOT_NS_SPEC | NVME_DNR;
6185     }
6186 
6187     if (!(nvme_feature_cap[fid] & NVME_FEAT_CAP_CHANGE)) {
6188         return NVME_FEAT_NOT_CHANGEABLE | NVME_DNR;
6189     }
6190 
6191     switch (fid) {
6192     case NVME_TEMPERATURE_THRESHOLD:
6193         if (NVME_TEMP_TMPSEL(dw11) != NVME_TEMP_TMPSEL_COMPOSITE) {
6194             break;
6195         }
6196 
6197         switch (NVME_TEMP_THSEL(dw11)) {
6198         case NVME_TEMP_THSEL_OVER:
6199             n->features.temp_thresh_hi = NVME_TEMP_TMPTH(dw11);
6200             break;
6201         case NVME_TEMP_THSEL_UNDER:
6202             n->features.temp_thresh_low = NVME_TEMP_TMPTH(dw11);
6203             break;
6204         default:
6205             return NVME_INVALID_FIELD | NVME_DNR;
6206         }
6207 
6208         if ((n->temperature >= n->features.temp_thresh_hi) ||
6209             (n->temperature <= n->features.temp_thresh_low)) {
6210             nvme_smart_event(n, NVME_SMART_TEMPERATURE);
6211         }
6212 
6213         break;
6214     case NVME_ERROR_RECOVERY:
6215         if (nsid == NVME_NSID_BROADCAST) {
6216             for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
6217                 ns = nvme_ns(n, i);
6218 
6219                 if (!ns) {
6220                     continue;
6221                 }
6222 
6223                 if (NVME_ID_NS_NSFEAT_DULBE(ns->id_ns.nsfeat)) {
6224                     ns->features.err_rec = dw11;
6225                 }
6226             }
6227 
6228             break;
6229         }
6230 
6231         assert(ns);
6232         if (NVME_ID_NS_NSFEAT_DULBE(ns->id_ns.nsfeat))  {
6233             ns->features.err_rec = dw11;
6234         }
6235         break;
6236     case NVME_VOLATILE_WRITE_CACHE:
6237         for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
6238             ns = nvme_ns(n, i);
6239             if (!ns) {
6240                 continue;
6241             }
6242 
6243             if (!(dw11 & 0x1) && blk_enable_write_cache(ns->blkconf.blk)) {
6244                 blk_flush(ns->blkconf.blk);
6245             }
6246 
6247             blk_set_enable_write_cache(ns->blkconf.blk, dw11 & 1);
6248         }
6249 
6250         break;
6251 
6252     case NVME_NUMBER_OF_QUEUES:
6253         if (n->qs_created) {
6254             return NVME_CMD_SEQ_ERROR | NVME_DNR;
6255         }
6256 
6257         /*
6258          * NVMe v1.3, Section 5.21.1.7: FFFFh is not an allowed value for NCQR
6259          * and NSQR.
6260          */
6261         if ((dw11 & 0xffff) == 0xffff || ((dw11 >> 16) & 0xffff) == 0xffff) {
6262             return NVME_INVALID_FIELD | NVME_DNR;
6263         }
6264 
6265         trace_pci_nvme_setfeat_numq((dw11 & 0xffff) + 1,
6266                                     ((dw11 >> 16) & 0xffff) + 1,
6267                                     n->conf_ioqpairs,
6268                                     n->conf_ioqpairs);
6269         req->cqe.result = cpu_to_le32((n->conf_ioqpairs - 1) |
6270                                       ((n->conf_ioqpairs - 1) << 16));
6271         break;
6272     case NVME_ASYNCHRONOUS_EVENT_CONF:
6273         n->features.async_config = dw11;
6274         break;
6275     case NVME_TIMESTAMP:
6276         return nvme_set_feature_timestamp(n, req);
6277     case NVME_HOST_BEHAVIOR_SUPPORT:
6278         status = nvme_h2c(n, (uint8_t *)&n->features.hbs,
6279                           sizeof(n->features.hbs), req);
6280         if (status) {
6281             return status;
6282         }
6283 
6284         for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
6285             ns = nvme_ns(n, i);
6286 
6287             if (!ns) {
6288                 continue;
6289             }
6290 
6291             ns->id_ns.nlbaf = ns->nlbaf - 1;
6292             if (!n->features.hbs.lbafee) {
6293                 ns->id_ns.nlbaf = MIN(ns->id_ns.nlbaf, 15);
6294             }
6295         }
6296 
6297         return status;
6298     case NVME_COMMAND_SET_PROFILE:
6299         if (dw11 & 0x1ff) {
6300             trace_pci_nvme_err_invalid_iocsci(dw11 & 0x1ff);
6301             return NVME_CMD_SET_CMB_REJECTED | NVME_DNR;
6302         }
6303         break;
6304     case NVME_FDP_MODE:
6305         /* spec: abort with cmd seq err if there's one or more NS' in endgrp */
6306         return NVME_CMD_SEQ_ERROR | NVME_DNR;
6307     case NVME_FDP_EVENTS:
6308         return nvme_set_feature_fdp_events(n, ns, req);
6309     default:
6310         return NVME_FEAT_NOT_CHANGEABLE | NVME_DNR;
6311     }
6312     return NVME_SUCCESS;
6313 }
6314 
6315 static uint16_t nvme_aer(NvmeCtrl *n, NvmeRequest *req)
6316 {
6317     trace_pci_nvme_aer(nvme_cid(req));
6318 
6319     if (n->outstanding_aers > n->params.aerl) {
6320         trace_pci_nvme_aer_aerl_exceeded();
6321         return NVME_AER_LIMIT_EXCEEDED;
6322     }
6323 
6324     n->aer_reqs[n->outstanding_aers] = req;
6325     n->outstanding_aers++;
6326 
6327     if (!QTAILQ_EMPTY(&n->aer_queue)) {
6328         nvme_process_aers(n);
6329     }
6330 
6331     return NVME_NO_COMPLETE;
6332 }
6333 
6334 static void nvme_update_dmrsl(NvmeCtrl *n)
6335 {
6336     int nsid;
6337 
6338     for (nsid = 1; nsid <= NVME_MAX_NAMESPACES; nsid++) {
6339         NvmeNamespace *ns = nvme_ns(n, nsid);
6340         if (!ns) {
6341             continue;
6342         }
6343 
6344         n->dmrsl = MIN_NON_ZERO(n->dmrsl,
6345                                 BDRV_REQUEST_MAX_BYTES / nvme_l2b(ns, 1));
6346     }
6347 }
6348 
6349 static void nvme_select_iocs_ns(NvmeCtrl *n, NvmeNamespace *ns)
6350 {
6351     uint32_t cc = ldl_le_p(&n->bar.cc);
6352 
6353     ns->iocs = nvme_cse_iocs_none;
6354     switch (ns->csi) {
6355     case NVME_CSI_NVM:
6356         if (NVME_CC_CSS(cc) != NVME_CC_CSS_ADMIN_ONLY) {
6357             ns->iocs = nvme_cse_iocs_nvm;
6358         }
6359         break;
6360     case NVME_CSI_ZONED:
6361         if (NVME_CC_CSS(cc) == NVME_CC_CSS_CSI) {
6362             ns->iocs = nvme_cse_iocs_zoned;
6363         } else if (NVME_CC_CSS(cc) == NVME_CC_CSS_NVM) {
6364             ns->iocs = nvme_cse_iocs_nvm;
6365         }
6366         break;
6367     }
6368 }
6369 
6370 static uint16_t nvme_ns_attachment(NvmeCtrl *n, NvmeRequest *req)
6371 {
6372     NvmeNamespace *ns;
6373     NvmeCtrl *ctrl;
6374     uint16_t list[NVME_CONTROLLER_LIST_SIZE] = {};
6375     uint32_t nsid = le32_to_cpu(req->cmd.nsid);
6376     uint32_t dw10 = le32_to_cpu(req->cmd.cdw10);
6377     uint8_t sel = dw10 & 0xf;
6378     uint16_t *nr_ids = &list[0];
6379     uint16_t *ids = &list[1];
6380     uint16_t ret;
6381     int i;
6382 
6383     trace_pci_nvme_ns_attachment(nvme_cid(req), dw10 & 0xf);
6384 
6385     if (!nvme_nsid_valid(n, nsid)) {
6386         return NVME_INVALID_NSID | NVME_DNR;
6387     }
6388 
6389     ns = nvme_subsys_ns(n->subsys, nsid);
6390     if (!ns) {
6391         return NVME_INVALID_FIELD | NVME_DNR;
6392     }
6393 
6394     ret = nvme_h2c(n, (uint8_t *)list, 4096, req);
6395     if (ret) {
6396         return ret;
6397     }
6398 
6399     if (!*nr_ids) {
6400         return NVME_NS_CTRL_LIST_INVALID | NVME_DNR;
6401     }
6402 
6403     *nr_ids = MIN(*nr_ids, NVME_CONTROLLER_LIST_SIZE - 1);
6404     for (i = 0; i < *nr_ids; i++) {
6405         ctrl = nvme_subsys_ctrl(n->subsys, ids[i]);
6406         if (!ctrl) {
6407             return NVME_NS_CTRL_LIST_INVALID | NVME_DNR;
6408         }
6409 
6410         switch (sel) {
6411         case NVME_NS_ATTACHMENT_ATTACH:
6412             if (nvme_ns(ctrl, nsid)) {
6413                 return NVME_NS_ALREADY_ATTACHED | NVME_DNR;
6414             }
6415 
6416             if (ns->attached && !ns->params.shared) {
6417                 return NVME_NS_PRIVATE | NVME_DNR;
6418             }
6419 
6420             nvme_attach_ns(ctrl, ns);
6421             nvme_select_iocs_ns(ctrl, ns);
6422 
6423             break;
6424 
6425         case NVME_NS_ATTACHMENT_DETACH:
6426             if (!nvme_ns(ctrl, nsid)) {
6427                 return NVME_NS_NOT_ATTACHED | NVME_DNR;
6428             }
6429 
6430             ctrl->namespaces[nsid] = NULL;
6431             ns->attached--;
6432 
6433             nvme_update_dmrsl(ctrl);
6434 
6435             break;
6436 
6437         default:
6438             return NVME_INVALID_FIELD | NVME_DNR;
6439         }
6440 
6441         /*
6442          * Add namespace id to the changed namespace id list for event clearing
6443          * via Get Log Page command.
6444          */
6445         if (!test_and_set_bit(nsid, ctrl->changed_nsids)) {
6446             nvme_enqueue_event(ctrl, NVME_AER_TYPE_NOTICE,
6447                                NVME_AER_INFO_NOTICE_NS_ATTR_CHANGED,
6448                                NVME_LOG_CHANGED_NSLIST);
6449         }
6450     }
6451 
6452     return NVME_SUCCESS;
6453 }
6454 
6455 typedef struct NvmeFormatAIOCB {
6456     BlockAIOCB common;
6457     BlockAIOCB *aiocb;
6458     NvmeRequest *req;
6459     int ret;
6460 
6461     NvmeNamespace *ns;
6462     uint32_t nsid;
6463     bool broadcast;
6464     int64_t offset;
6465 
6466     uint8_t lbaf;
6467     uint8_t mset;
6468     uint8_t pi;
6469     uint8_t pil;
6470 } NvmeFormatAIOCB;
6471 
6472 static void nvme_format_cancel(BlockAIOCB *aiocb)
6473 {
6474     NvmeFormatAIOCB *iocb = container_of(aiocb, NvmeFormatAIOCB, common);
6475 
6476     iocb->ret = -ECANCELED;
6477 
6478     if (iocb->aiocb) {
6479         blk_aio_cancel_async(iocb->aiocb);
6480         iocb->aiocb = NULL;
6481     }
6482 }
6483 
6484 static const AIOCBInfo nvme_format_aiocb_info = {
6485     .aiocb_size = sizeof(NvmeFormatAIOCB),
6486     .cancel_async = nvme_format_cancel,
6487 };
6488 
6489 static void nvme_format_set(NvmeNamespace *ns, uint8_t lbaf, uint8_t mset,
6490                             uint8_t pi, uint8_t pil)
6491 {
6492     uint8_t lbafl = lbaf & 0xf;
6493     uint8_t lbafu = lbaf >> 4;
6494 
6495     trace_pci_nvme_format_set(ns->params.nsid, lbaf, mset, pi, pil);
6496 
6497     ns->id_ns.dps = (pil << 3) | pi;
6498     ns->id_ns.flbas = (lbafu << 5) | (mset << 4) | lbafl;
6499 
6500     nvme_ns_init_format(ns);
6501 }
6502 
6503 static void nvme_do_format(NvmeFormatAIOCB *iocb);
6504 
6505 static void nvme_format_ns_cb(void *opaque, int ret)
6506 {
6507     NvmeFormatAIOCB *iocb = opaque;
6508     NvmeNamespace *ns = iocb->ns;
6509     int bytes;
6510 
6511     if (iocb->ret < 0) {
6512         goto done;
6513     } else if (ret < 0) {
6514         iocb->ret = ret;
6515         goto done;
6516     }
6517 
6518     assert(ns);
6519 
6520     if (iocb->offset < ns->size) {
6521         bytes = MIN(BDRV_REQUEST_MAX_BYTES, ns->size - iocb->offset);
6522 
6523         iocb->aiocb = blk_aio_pwrite_zeroes(ns->blkconf.blk, iocb->offset,
6524                                             bytes, BDRV_REQ_MAY_UNMAP,
6525                                             nvme_format_ns_cb, iocb);
6526 
6527         iocb->offset += bytes;
6528         return;
6529     }
6530 
6531     nvme_format_set(ns, iocb->lbaf, iocb->mset, iocb->pi, iocb->pil);
6532     ns->status = 0x0;
6533     iocb->ns = NULL;
6534     iocb->offset = 0;
6535 
6536 done:
6537     nvme_do_format(iocb);
6538 }
6539 
6540 static uint16_t nvme_format_check(NvmeNamespace *ns, uint8_t lbaf, uint8_t pi)
6541 {
6542     if (ns->params.zoned) {
6543         return NVME_INVALID_FORMAT | NVME_DNR;
6544     }
6545 
6546     if (lbaf > ns->id_ns.nlbaf) {
6547         return NVME_INVALID_FORMAT | NVME_DNR;
6548     }
6549 
6550     if (pi && (ns->id_ns.lbaf[lbaf].ms < nvme_pi_tuple_size(ns))) {
6551         return NVME_INVALID_FORMAT | NVME_DNR;
6552     }
6553 
6554     if (pi && pi > NVME_ID_NS_DPS_TYPE_3) {
6555         return NVME_INVALID_FIELD | NVME_DNR;
6556     }
6557 
6558     return NVME_SUCCESS;
6559 }
6560 
6561 static void nvme_do_format(NvmeFormatAIOCB *iocb)
6562 {
6563     NvmeRequest *req = iocb->req;
6564     NvmeCtrl *n = nvme_ctrl(req);
6565     uint32_t dw10 = le32_to_cpu(req->cmd.cdw10);
6566     uint8_t lbaf = dw10 & 0xf;
6567     uint8_t pi = (dw10 >> 5) & 0x7;
6568     uint16_t status;
6569     int i;
6570 
6571     if (iocb->ret < 0) {
6572         goto done;
6573     }
6574 
6575     if (iocb->broadcast) {
6576         for (i = iocb->nsid + 1; i <= NVME_MAX_NAMESPACES; i++) {
6577             iocb->ns = nvme_ns(n, i);
6578             if (iocb->ns) {
6579                 iocb->nsid = i;
6580                 break;
6581             }
6582         }
6583     }
6584 
6585     if (!iocb->ns) {
6586         goto done;
6587     }
6588 
6589     status = nvme_format_check(iocb->ns, lbaf, pi);
6590     if (status) {
6591         req->status = status;
6592         goto done;
6593     }
6594 
6595     iocb->ns->status = NVME_FORMAT_IN_PROGRESS;
6596     nvme_format_ns_cb(iocb, 0);
6597     return;
6598 
6599 done:
6600     iocb->common.cb(iocb->common.opaque, iocb->ret);
6601     qemu_aio_unref(iocb);
6602 }
6603 
6604 static uint16_t nvme_format(NvmeCtrl *n, NvmeRequest *req)
6605 {
6606     NvmeFormatAIOCB *iocb;
6607     uint32_t nsid = le32_to_cpu(req->cmd.nsid);
6608     uint32_t dw10 = le32_to_cpu(req->cmd.cdw10);
6609     uint8_t lbaf = dw10 & 0xf;
6610     uint8_t mset = (dw10 >> 4) & 0x1;
6611     uint8_t pi = (dw10 >> 5) & 0x7;
6612     uint8_t pil = (dw10 >> 8) & 0x1;
6613     uint8_t lbafu = (dw10 >> 12) & 0x3;
6614     uint16_t status;
6615 
6616     iocb = qemu_aio_get(&nvme_format_aiocb_info, NULL, nvme_misc_cb, req);
6617 
6618     iocb->req = req;
6619     iocb->ret = 0;
6620     iocb->ns = NULL;
6621     iocb->nsid = 0;
6622     iocb->lbaf = lbaf;
6623     iocb->mset = mset;
6624     iocb->pi = pi;
6625     iocb->pil = pil;
6626     iocb->broadcast = (nsid == NVME_NSID_BROADCAST);
6627     iocb->offset = 0;
6628 
6629     if (n->features.hbs.lbafee) {
6630         iocb->lbaf |= lbafu << 4;
6631     }
6632 
6633     if (!iocb->broadcast) {
6634         if (!nvme_nsid_valid(n, nsid)) {
6635             status = NVME_INVALID_NSID | NVME_DNR;
6636             goto out;
6637         }
6638 
6639         iocb->ns = nvme_ns(n, nsid);
6640         if (!iocb->ns) {
6641             status = NVME_INVALID_FIELD | NVME_DNR;
6642             goto out;
6643         }
6644     }
6645 
6646     req->aiocb = &iocb->common;
6647     nvme_do_format(iocb);
6648 
6649     return NVME_NO_COMPLETE;
6650 
6651 out:
6652     qemu_aio_unref(iocb);
6653 
6654     return status;
6655 }
6656 
6657 static void nvme_get_virt_res_num(NvmeCtrl *n, uint8_t rt, int *num_total,
6658                                   int *num_prim, int *num_sec)
6659 {
6660     *num_total = le32_to_cpu(rt ?
6661                              n->pri_ctrl_cap.vifrt : n->pri_ctrl_cap.vqfrt);
6662     *num_prim = le16_to_cpu(rt ?
6663                             n->pri_ctrl_cap.virfap : n->pri_ctrl_cap.vqrfap);
6664     *num_sec = le16_to_cpu(rt ? n->pri_ctrl_cap.virfa : n->pri_ctrl_cap.vqrfa);
6665 }
6666 
6667 static uint16_t nvme_assign_virt_res_to_prim(NvmeCtrl *n, NvmeRequest *req,
6668                                              uint16_t cntlid, uint8_t rt,
6669                                              int nr)
6670 {
6671     int num_total, num_prim, num_sec;
6672 
6673     if (cntlid != n->cntlid) {
6674         return NVME_INVALID_CTRL_ID | NVME_DNR;
6675     }
6676 
6677     nvme_get_virt_res_num(n, rt, &num_total, &num_prim, &num_sec);
6678 
6679     if (nr > num_total) {
6680         return NVME_INVALID_NUM_RESOURCES | NVME_DNR;
6681     }
6682 
6683     if (nr > num_total - num_sec) {
6684         return NVME_INVALID_RESOURCE_ID | NVME_DNR;
6685     }
6686 
6687     if (rt) {
6688         n->next_pri_ctrl_cap.virfap = cpu_to_le16(nr);
6689     } else {
6690         n->next_pri_ctrl_cap.vqrfap = cpu_to_le16(nr);
6691     }
6692 
6693     req->cqe.result = cpu_to_le32(nr);
6694     return req->status;
6695 }
6696 
6697 static void nvme_update_virt_res(NvmeCtrl *n, NvmeSecCtrlEntry *sctrl,
6698                                  uint8_t rt, int nr)
6699 {
6700     int prev_nr, prev_total;
6701 
6702     if (rt) {
6703         prev_nr = le16_to_cpu(sctrl->nvi);
6704         prev_total = le32_to_cpu(n->pri_ctrl_cap.virfa);
6705         sctrl->nvi = cpu_to_le16(nr);
6706         n->pri_ctrl_cap.virfa = cpu_to_le32(prev_total + nr - prev_nr);
6707     } else {
6708         prev_nr = le16_to_cpu(sctrl->nvq);
6709         prev_total = le32_to_cpu(n->pri_ctrl_cap.vqrfa);
6710         sctrl->nvq = cpu_to_le16(nr);
6711         n->pri_ctrl_cap.vqrfa = cpu_to_le32(prev_total + nr - prev_nr);
6712     }
6713 }
6714 
6715 static uint16_t nvme_assign_virt_res_to_sec(NvmeCtrl *n, NvmeRequest *req,
6716                                             uint16_t cntlid, uint8_t rt, int nr)
6717 {
6718     int num_total, num_prim, num_sec, num_free, diff, limit;
6719     NvmeSecCtrlEntry *sctrl;
6720 
6721     sctrl = nvme_sctrl_for_cntlid(n, cntlid);
6722     if (!sctrl) {
6723         return NVME_INVALID_CTRL_ID | NVME_DNR;
6724     }
6725 
6726     if (sctrl->scs) {
6727         return NVME_INVALID_SEC_CTRL_STATE | NVME_DNR;
6728     }
6729 
6730     limit = le16_to_cpu(rt ? n->pri_ctrl_cap.vifrsm : n->pri_ctrl_cap.vqfrsm);
6731     if (nr > limit) {
6732         return NVME_INVALID_NUM_RESOURCES | NVME_DNR;
6733     }
6734 
6735     nvme_get_virt_res_num(n, rt, &num_total, &num_prim, &num_sec);
6736     num_free = num_total - num_prim - num_sec;
6737     diff = nr - le16_to_cpu(rt ? sctrl->nvi : sctrl->nvq);
6738 
6739     if (diff > num_free) {
6740         return NVME_INVALID_RESOURCE_ID | NVME_DNR;
6741     }
6742 
6743     nvme_update_virt_res(n, sctrl, rt, nr);
6744     req->cqe.result = cpu_to_le32(nr);
6745 
6746     return req->status;
6747 }
6748 
6749 static uint16_t nvme_virt_set_state(NvmeCtrl *n, uint16_t cntlid, bool online)
6750 {
6751     PCIDevice *pci = PCI_DEVICE(n);
6752     NvmeCtrl *sn = NULL;
6753     NvmeSecCtrlEntry *sctrl;
6754     int vf_index;
6755 
6756     sctrl = nvme_sctrl_for_cntlid(n, cntlid);
6757     if (!sctrl) {
6758         return NVME_INVALID_CTRL_ID | NVME_DNR;
6759     }
6760 
6761     if (!pci_is_vf(pci)) {
6762         vf_index = le16_to_cpu(sctrl->vfn) - 1;
6763         sn = NVME(pcie_sriov_get_vf_at_index(pci, vf_index));
6764     }
6765 
6766     if (online) {
6767         if (!sctrl->nvi || (le16_to_cpu(sctrl->nvq) < 2) || !sn) {
6768             return NVME_INVALID_SEC_CTRL_STATE | NVME_DNR;
6769         }
6770 
6771         if (!sctrl->scs) {
6772             sctrl->scs = 0x1;
6773             nvme_ctrl_reset(sn, NVME_RESET_FUNCTION);
6774         }
6775     } else {
6776         nvme_update_virt_res(n, sctrl, NVME_VIRT_RES_INTERRUPT, 0);
6777         nvme_update_virt_res(n, sctrl, NVME_VIRT_RES_QUEUE, 0);
6778 
6779         if (sctrl->scs) {
6780             sctrl->scs = 0x0;
6781             if (sn) {
6782                 nvme_ctrl_reset(sn, NVME_RESET_FUNCTION);
6783             }
6784         }
6785     }
6786 
6787     return NVME_SUCCESS;
6788 }
6789 
6790 static uint16_t nvme_virt_mngmt(NvmeCtrl *n, NvmeRequest *req)
6791 {
6792     uint32_t dw10 = le32_to_cpu(req->cmd.cdw10);
6793     uint32_t dw11 = le32_to_cpu(req->cmd.cdw11);
6794     uint8_t act = dw10 & 0xf;
6795     uint8_t rt = (dw10 >> 8) & 0x7;
6796     uint16_t cntlid = (dw10 >> 16) & 0xffff;
6797     int nr = dw11 & 0xffff;
6798 
6799     trace_pci_nvme_virt_mngmt(nvme_cid(req), act, cntlid, rt ? "VI" : "VQ", nr);
6800 
6801     if (rt != NVME_VIRT_RES_QUEUE && rt != NVME_VIRT_RES_INTERRUPT) {
6802         return NVME_INVALID_RESOURCE_ID | NVME_DNR;
6803     }
6804 
6805     switch (act) {
6806     case NVME_VIRT_MNGMT_ACTION_SEC_ASSIGN:
6807         return nvme_assign_virt_res_to_sec(n, req, cntlid, rt, nr);
6808     case NVME_VIRT_MNGMT_ACTION_PRM_ALLOC:
6809         return nvme_assign_virt_res_to_prim(n, req, cntlid, rt, nr);
6810     case NVME_VIRT_MNGMT_ACTION_SEC_ONLINE:
6811         return nvme_virt_set_state(n, cntlid, true);
6812     case NVME_VIRT_MNGMT_ACTION_SEC_OFFLINE:
6813         return nvme_virt_set_state(n, cntlid, false);
6814     default:
6815         return NVME_INVALID_FIELD | NVME_DNR;
6816     }
6817 }
6818 
6819 static uint16_t nvme_dbbuf_config(NvmeCtrl *n, const NvmeRequest *req)
6820 {
6821     PCIDevice *pci = PCI_DEVICE(n);
6822     uint64_t dbs_addr = le64_to_cpu(req->cmd.dptr.prp1);
6823     uint64_t eis_addr = le64_to_cpu(req->cmd.dptr.prp2);
6824     int i;
6825 
6826     /* Address should be page aligned */
6827     if (dbs_addr & (n->page_size - 1) || eis_addr & (n->page_size - 1)) {
6828         return NVME_INVALID_FIELD | NVME_DNR;
6829     }
6830 
6831     /* Save shadow buffer base addr for use during queue creation */
6832     n->dbbuf_dbs = dbs_addr;
6833     n->dbbuf_eis = eis_addr;
6834     n->dbbuf_enabled = true;
6835 
6836     for (i = 0; i < n->params.max_ioqpairs + 1; i++) {
6837         NvmeSQueue *sq = n->sq[i];
6838         NvmeCQueue *cq = n->cq[i];
6839 
6840         if (sq) {
6841             /*
6842              * CAP.DSTRD is 0, so offset of ith sq db_addr is (i<<3)
6843              * nvme_process_db() uses this hard-coded way to calculate
6844              * doorbell offsets. Be consistent with that here.
6845              */
6846             sq->db_addr = dbs_addr + (i << 3);
6847             sq->ei_addr = eis_addr + (i << 3);
6848             stl_le_pci_dma(pci, sq->db_addr, sq->tail, MEMTXATTRS_UNSPECIFIED);
6849 
6850             if (n->params.ioeventfd && sq->sqid != 0) {
6851                 if (!nvme_init_sq_ioeventfd(sq)) {
6852                     sq->ioeventfd_enabled = true;
6853                 }
6854             }
6855         }
6856 
6857         if (cq) {
6858             /* CAP.DSTRD is 0, so offset of ith cq db_addr is (i<<3)+(1<<2) */
6859             cq->db_addr = dbs_addr + (i << 3) + (1 << 2);
6860             cq->ei_addr = eis_addr + (i << 3) + (1 << 2);
6861             stl_le_pci_dma(pci, cq->db_addr, cq->head, MEMTXATTRS_UNSPECIFIED);
6862 
6863             if (n->params.ioeventfd && cq->cqid != 0) {
6864                 if (!nvme_init_cq_ioeventfd(cq)) {
6865                     cq->ioeventfd_enabled = true;
6866                 }
6867             }
6868         }
6869     }
6870 
6871     trace_pci_nvme_dbbuf_config(dbs_addr, eis_addr);
6872 
6873     return NVME_SUCCESS;
6874 }
6875 
6876 static uint16_t nvme_directive_send(NvmeCtrl *n, NvmeRequest *req)
6877 {
6878     return NVME_INVALID_FIELD | NVME_DNR;
6879 }
6880 
6881 static uint16_t nvme_directive_receive(NvmeCtrl *n, NvmeRequest *req)
6882 {
6883     NvmeNamespace *ns;
6884     uint32_t dw10 = le32_to_cpu(req->cmd.cdw10);
6885     uint32_t dw11 = le32_to_cpu(req->cmd.cdw11);
6886     uint32_t nsid = le32_to_cpu(req->cmd.nsid);
6887     uint8_t doper, dtype;
6888     uint32_t numd, trans_len;
6889     NvmeDirectiveIdentify id = {
6890         .supported = 1 << NVME_DIRECTIVE_IDENTIFY,
6891         .enabled = 1 << NVME_DIRECTIVE_IDENTIFY,
6892     };
6893 
6894     numd = dw10 + 1;
6895     doper = dw11 & 0xff;
6896     dtype = (dw11 >> 8) & 0xff;
6897 
6898     trans_len = MIN(sizeof(NvmeDirectiveIdentify), numd << 2);
6899 
6900     if (nsid == NVME_NSID_BROADCAST || dtype != NVME_DIRECTIVE_IDENTIFY ||
6901         doper != NVME_DIRECTIVE_RETURN_PARAMS) {
6902         return NVME_INVALID_FIELD | NVME_DNR;
6903     }
6904 
6905     ns = nvme_ns(n, nsid);
6906     if (!ns) {
6907         return NVME_INVALID_FIELD | NVME_DNR;
6908     }
6909 
6910     switch (dtype) {
6911     case NVME_DIRECTIVE_IDENTIFY:
6912         switch (doper) {
6913         case NVME_DIRECTIVE_RETURN_PARAMS:
6914             if (ns->endgrp && ns->endgrp->fdp.enabled) {
6915                 id.supported |= 1 << NVME_DIRECTIVE_DATA_PLACEMENT;
6916                 id.enabled |= 1 << NVME_DIRECTIVE_DATA_PLACEMENT;
6917                 id.persistent |= 1 << NVME_DIRECTIVE_DATA_PLACEMENT;
6918             }
6919 
6920             return nvme_c2h(n, (uint8_t *)&id, trans_len, req);
6921 
6922         default:
6923             return NVME_INVALID_FIELD | NVME_DNR;
6924         }
6925 
6926     default:
6927         return NVME_INVALID_FIELD;
6928     }
6929 }
6930 
6931 static uint16_t nvme_admin_cmd(NvmeCtrl *n, NvmeRequest *req)
6932 {
6933     trace_pci_nvme_admin_cmd(nvme_cid(req), nvme_sqid(req), req->cmd.opcode,
6934                              nvme_adm_opc_str(req->cmd.opcode));
6935 
6936     if (!(nvme_cse_acs[req->cmd.opcode] & NVME_CMD_EFF_CSUPP)) {
6937         trace_pci_nvme_err_invalid_admin_opc(req->cmd.opcode);
6938         return NVME_INVALID_OPCODE | NVME_DNR;
6939     }
6940 
6941     /* SGLs shall not be used for Admin commands in NVMe over PCIe */
6942     if (NVME_CMD_FLAGS_PSDT(req->cmd.flags) != NVME_PSDT_PRP) {
6943         return NVME_INVALID_FIELD | NVME_DNR;
6944     }
6945 
6946     if (NVME_CMD_FLAGS_FUSE(req->cmd.flags)) {
6947         return NVME_INVALID_FIELD;
6948     }
6949 
6950     switch (req->cmd.opcode) {
6951     case NVME_ADM_CMD_DELETE_SQ:
6952         return nvme_del_sq(n, req);
6953     case NVME_ADM_CMD_CREATE_SQ:
6954         return nvme_create_sq(n, req);
6955     case NVME_ADM_CMD_GET_LOG_PAGE:
6956         return nvme_get_log(n, req);
6957     case NVME_ADM_CMD_DELETE_CQ:
6958         return nvme_del_cq(n, req);
6959     case NVME_ADM_CMD_CREATE_CQ:
6960         return nvme_create_cq(n, req);
6961     case NVME_ADM_CMD_IDENTIFY:
6962         return nvme_identify(n, req);
6963     case NVME_ADM_CMD_ABORT:
6964         return nvme_abort(n, req);
6965     case NVME_ADM_CMD_SET_FEATURES:
6966         return nvme_set_feature(n, req);
6967     case NVME_ADM_CMD_GET_FEATURES:
6968         return nvme_get_feature(n, req);
6969     case NVME_ADM_CMD_ASYNC_EV_REQ:
6970         return nvme_aer(n, req);
6971     case NVME_ADM_CMD_NS_ATTACHMENT:
6972         return nvme_ns_attachment(n, req);
6973     case NVME_ADM_CMD_VIRT_MNGMT:
6974         return nvme_virt_mngmt(n, req);
6975     case NVME_ADM_CMD_DBBUF_CONFIG:
6976         return nvme_dbbuf_config(n, req);
6977     case NVME_ADM_CMD_FORMAT_NVM:
6978         return nvme_format(n, req);
6979     case NVME_ADM_CMD_DIRECTIVE_SEND:
6980         return nvme_directive_send(n, req);
6981     case NVME_ADM_CMD_DIRECTIVE_RECV:
6982         return nvme_directive_receive(n, req);
6983     default:
6984         assert(false);
6985     }
6986 
6987     return NVME_INVALID_OPCODE | NVME_DNR;
6988 }
6989 
6990 static void nvme_update_sq_eventidx(const NvmeSQueue *sq)
6991 {
6992     trace_pci_nvme_update_sq_eventidx(sq->sqid, sq->tail);
6993 
6994     stl_le_pci_dma(PCI_DEVICE(sq->ctrl), sq->ei_addr, sq->tail,
6995                    MEMTXATTRS_UNSPECIFIED);
6996 }
6997 
6998 static void nvme_update_sq_tail(NvmeSQueue *sq)
6999 {
7000     ldl_le_pci_dma(PCI_DEVICE(sq->ctrl), sq->db_addr, &sq->tail,
7001                    MEMTXATTRS_UNSPECIFIED);
7002 
7003     trace_pci_nvme_update_sq_tail(sq->sqid, sq->tail);
7004 }
7005 
7006 static void nvme_process_sq(void *opaque)
7007 {
7008     NvmeSQueue *sq = opaque;
7009     NvmeCtrl *n = sq->ctrl;
7010     NvmeCQueue *cq = n->cq[sq->cqid];
7011 
7012     uint16_t status;
7013     hwaddr addr;
7014     NvmeCmd cmd;
7015     NvmeRequest *req;
7016 
7017     if (n->dbbuf_enabled) {
7018         nvme_update_sq_tail(sq);
7019     }
7020 
7021     while (!(nvme_sq_empty(sq) || QTAILQ_EMPTY(&sq->req_list))) {
7022         addr = sq->dma_addr + (sq->head << NVME_SQES);
7023         if (nvme_addr_read(n, addr, (void *)&cmd, sizeof(cmd))) {
7024             trace_pci_nvme_err_addr_read(addr);
7025             trace_pci_nvme_err_cfs();
7026             stl_le_p(&n->bar.csts, NVME_CSTS_FAILED);
7027             break;
7028         }
7029         nvme_inc_sq_head(sq);
7030 
7031         req = QTAILQ_FIRST(&sq->req_list);
7032         QTAILQ_REMOVE(&sq->req_list, req, entry);
7033         QTAILQ_INSERT_TAIL(&sq->out_req_list, req, entry);
7034         nvme_req_clear(req);
7035         req->cqe.cid = cmd.cid;
7036         memcpy(&req->cmd, &cmd, sizeof(NvmeCmd));
7037 
7038         status = sq->sqid ? nvme_io_cmd(n, req) :
7039             nvme_admin_cmd(n, req);
7040         if (status != NVME_NO_COMPLETE) {
7041             req->status = status;
7042             nvme_enqueue_req_completion(cq, req);
7043         }
7044 
7045         if (n->dbbuf_enabled) {
7046             nvme_update_sq_eventidx(sq);
7047             nvme_update_sq_tail(sq);
7048         }
7049     }
7050 }
7051 
7052 static void nvme_update_msixcap_ts(PCIDevice *pci_dev, uint32_t table_size)
7053 {
7054     uint8_t *config;
7055 
7056     if (!msix_present(pci_dev)) {
7057         return;
7058     }
7059 
7060     assert(table_size > 0 && table_size <= pci_dev->msix_entries_nr);
7061 
7062     config = pci_dev->config + pci_dev->msix_cap;
7063     pci_set_word_by_mask(config + PCI_MSIX_FLAGS, PCI_MSIX_FLAGS_QSIZE,
7064                          table_size - 1);
7065 }
7066 
7067 static void nvme_activate_virt_res(NvmeCtrl *n)
7068 {
7069     PCIDevice *pci_dev = PCI_DEVICE(n);
7070     NvmePriCtrlCap *cap = &n->pri_ctrl_cap;
7071     NvmeSecCtrlEntry *sctrl;
7072 
7073     /* -1 to account for the admin queue */
7074     if (pci_is_vf(pci_dev)) {
7075         sctrl = nvme_sctrl(n);
7076         cap->vqprt = sctrl->nvq;
7077         cap->viprt = sctrl->nvi;
7078         n->conf_ioqpairs = sctrl->nvq ? le16_to_cpu(sctrl->nvq) - 1 : 0;
7079         n->conf_msix_qsize = sctrl->nvi ? le16_to_cpu(sctrl->nvi) : 1;
7080     } else {
7081         cap->vqrfap = n->next_pri_ctrl_cap.vqrfap;
7082         cap->virfap = n->next_pri_ctrl_cap.virfap;
7083         n->conf_ioqpairs = le16_to_cpu(cap->vqprt) +
7084                            le16_to_cpu(cap->vqrfap) - 1;
7085         n->conf_msix_qsize = le16_to_cpu(cap->viprt) +
7086                              le16_to_cpu(cap->virfap);
7087     }
7088 }
7089 
7090 static void nvme_ctrl_reset(NvmeCtrl *n, NvmeResetType rst)
7091 {
7092     PCIDevice *pci_dev = PCI_DEVICE(n);
7093     NvmeSecCtrlEntry *sctrl;
7094     NvmeNamespace *ns;
7095     int i;
7096 
7097     for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
7098         ns = nvme_ns(n, i);
7099         if (!ns) {
7100             continue;
7101         }
7102 
7103         nvme_ns_drain(ns);
7104     }
7105 
7106     for (i = 0; i < n->params.max_ioqpairs + 1; i++) {
7107         if (n->sq[i] != NULL) {
7108             nvme_free_sq(n->sq[i], n);
7109         }
7110     }
7111     for (i = 0; i < n->params.max_ioqpairs + 1; i++) {
7112         if (n->cq[i] != NULL) {
7113             nvme_free_cq(n->cq[i], n);
7114         }
7115     }
7116 
7117     while (!QTAILQ_EMPTY(&n->aer_queue)) {
7118         NvmeAsyncEvent *event = QTAILQ_FIRST(&n->aer_queue);
7119         QTAILQ_REMOVE(&n->aer_queue, event, entry);
7120         g_free(event);
7121     }
7122 
7123     if (n->params.sriov_max_vfs) {
7124         if (!pci_is_vf(pci_dev)) {
7125             for (i = 0; i < n->sec_ctrl_list.numcntl; i++) {
7126                 sctrl = &n->sec_ctrl_list.sec[i];
7127                 nvme_virt_set_state(n, le16_to_cpu(sctrl->scid), false);
7128             }
7129         }
7130 
7131         if (rst != NVME_RESET_CONTROLLER) {
7132             nvme_activate_virt_res(n);
7133         }
7134     }
7135 
7136     n->aer_queued = 0;
7137     n->aer_mask = 0;
7138     n->outstanding_aers = 0;
7139     n->qs_created = false;
7140 
7141     nvme_update_msixcap_ts(pci_dev, n->conf_msix_qsize);
7142 
7143     if (pci_is_vf(pci_dev)) {
7144         sctrl = nvme_sctrl(n);
7145 
7146         stl_le_p(&n->bar.csts, sctrl->scs ? 0 : NVME_CSTS_FAILED);
7147     } else {
7148         stl_le_p(&n->bar.csts, 0);
7149     }
7150 
7151     stl_le_p(&n->bar.intms, 0);
7152     stl_le_p(&n->bar.intmc, 0);
7153     stl_le_p(&n->bar.cc, 0);
7154 
7155     n->dbbuf_dbs = 0;
7156     n->dbbuf_eis = 0;
7157     n->dbbuf_enabled = false;
7158 }
7159 
7160 static void nvme_ctrl_shutdown(NvmeCtrl *n)
7161 {
7162     NvmeNamespace *ns;
7163     int i;
7164 
7165     if (n->pmr.dev) {
7166         memory_region_msync(&n->pmr.dev->mr, 0, n->pmr.dev->size);
7167     }
7168 
7169     for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
7170         ns = nvme_ns(n, i);
7171         if (!ns) {
7172             continue;
7173         }
7174 
7175         nvme_ns_shutdown(ns);
7176     }
7177 }
7178 
7179 static void nvme_select_iocs(NvmeCtrl *n)
7180 {
7181     NvmeNamespace *ns;
7182     int i;
7183 
7184     for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
7185         ns = nvme_ns(n, i);
7186         if (!ns) {
7187             continue;
7188         }
7189 
7190         nvme_select_iocs_ns(n, ns);
7191     }
7192 }
7193 
7194 static int nvme_start_ctrl(NvmeCtrl *n)
7195 {
7196     uint64_t cap = ldq_le_p(&n->bar.cap);
7197     uint32_t cc = ldl_le_p(&n->bar.cc);
7198     uint32_t aqa = ldl_le_p(&n->bar.aqa);
7199     uint64_t asq = ldq_le_p(&n->bar.asq);
7200     uint64_t acq = ldq_le_p(&n->bar.acq);
7201     uint32_t page_bits = NVME_CC_MPS(cc) + 12;
7202     uint32_t page_size = 1 << page_bits;
7203     NvmeSecCtrlEntry *sctrl = nvme_sctrl(n);
7204 
7205     if (pci_is_vf(PCI_DEVICE(n)) && !sctrl->scs) {
7206         trace_pci_nvme_err_startfail_virt_state(le16_to_cpu(sctrl->nvi),
7207                                                 le16_to_cpu(sctrl->nvq));
7208         return -1;
7209     }
7210     if (unlikely(n->cq[0])) {
7211         trace_pci_nvme_err_startfail_cq();
7212         return -1;
7213     }
7214     if (unlikely(n->sq[0])) {
7215         trace_pci_nvme_err_startfail_sq();
7216         return -1;
7217     }
7218     if (unlikely(asq & (page_size - 1))) {
7219         trace_pci_nvme_err_startfail_asq_misaligned(asq);
7220         return -1;
7221     }
7222     if (unlikely(acq & (page_size - 1))) {
7223         trace_pci_nvme_err_startfail_acq_misaligned(acq);
7224         return -1;
7225     }
7226     if (unlikely(!(NVME_CAP_CSS(cap) & (1 << NVME_CC_CSS(cc))))) {
7227         trace_pci_nvme_err_startfail_css(NVME_CC_CSS(cc));
7228         return -1;
7229     }
7230     if (unlikely(NVME_CC_MPS(cc) < NVME_CAP_MPSMIN(cap))) {
7231         trace_pci_nvme_err_startfail_page_too_small(
7232                     NVME_CC_MPS(cc),
7233                     NVME_CAP_MPSMIN(cap));
7234         return -1;
7235     }
7236     if (unlikely(NVME_CC_MPS(cc) >
7237                  NVME_CAP_MPSMAX(cap))) {
7238         trace_pci_nvme_err_startfail_page_too_large(
7239                     NVME_CC_MPS(cc),
7240                     NVME_CAP_MPSMAX(cap));
7241         return -1;
7242     }
7243     if (unlikely(!NVME_AQA_ASQS(aqa))) {
7244         trace_pci_nvme_err_startfail_asqent_sz_zero();
7245         return -1;
7246     }
7247     if (unlikely(!NVME_AQA_ACQS(aqa))) {
7248         trace_pci_nvme_err_startfail_acqent_sz_zero();
7249         return -1;
7250     }
7251 
7252     n->page_bits = page_bits;
7253     n->page_size = page_size;
7254     n->max_prp_ents = n->page_size / sizeof(uint64_t);
7255     nvme_init_cq(&n->admin_cq, n, acq, 0, 0, NVME_AQA_ACQS(aqa) + 1, 1);
7256     nvme_init_sq(&n->admin_sq, n, asq, 0, 0, NVME_AQA_ASQS(aqa) + 1);
7257 
7258     nvme_set_timestamp(n, 0ULL);
7259 
7260     nvme_select_iocs(n);
7261 
7262     return 0;
7263 }
7264 
7265 static void nvme_cmb_enable_regs(NvmeCtrl *n)
7266 {
7267     uint32_t cmbloc = ldl_le_p(&n->bar.cmbloc);
7268     uint32_t cmbsz = ldl_le_p(&n->bar.cmbsz);
7269 
7270     NVME_CMBLOC_SET_CDPCILS(cmbloc, 1);
7271     NVME_CMBLOC_SET_CDPMLS(cmbloc, 1);
7272     NVME_CMBLOC_SET_BIR(cmbloc, NVME_CMB_BIR);
7273     stl_le_p(&n->bar.cmbloc, cmbloc);
7274 
7275     NVME_CMBSZ_SET_SQS(cmbsz, 1);
7276     NVME_CMBSZ_SET_CQS(cmbsz, 0);
7277     NVME_CMBSZ_SET_LISTS(cmbsz, 1);
7278     NVME_CMBSZ_SET_RDS(cmbsz, 1);
7279     NVME_CMBSZ_SET_WDS(cmbsz, 1);
7280     NVME_CMBSZ_SET_SZU(cmbsz, 2); /* MBs */
7281     NVME_CMBSZ_SET_SZ(cmbsz, n->params.cmb_size_mb);
7282     stl_le_p(&n->bar.cmbsz, cmbsz);
7283 }
7284 
7285 static void nvme_write_bar(NvmeCtrl *n, hwaddr offset, uint64_t data,
7286                            unsigned size)
7287 {
7288     PCIDevice *pci = PCI_DEVICE(n);
7289     uint64_t cap = ldq_le_p(&n->bar.cap);
7290     uint32_t cc = ldl_le_p(&n->bar.cc);
7291     uint32_t intms = ldl_le_p(&n->bar.intms);
7292     uint32_t csts = ldl_le_p(&n->bar.csts);
7293     uint32_t pmrsts = ldl_le_p(&n->bar.pmrsts);
7294 
7295     if (unlikely(offset & (sizeof(uint32_t) - 1))) {
7296         NVME_GUEST_ERR(pci_nvme_ub_mmiowr_misaligned32,
7297                        "MMIO write not 32-bit aligned,"
7298                        " offset=0x%"PRIx64"", offset);
7299         /* should be ignored, fall through for now */
7300     }
7301 
7302     if (unlikely(size < sizeof(uint32_t))) {
7303         NVME_GUEST_ERR(pci_nvme_ub_mmiowr_toosmall,
7304                        "MMIO write smaller than 32-bits,"
7305                        " offset=0x%"PRIx64", size=%u",
7306                        offset, size);
7307         /* should be ignored, fall through for now */
7308     }
7309 
7310     switch (offset) {
7311     case NVME_REG_INTMS:
7312         if (unlikely(msix_enabled(pci))) {
7313             NVME_GUEST_ERR(pci_nvme_ub_mmiowr_intmask_with_msix,
7314                            "undefined access to interrupt mask set"
7315                            " when MSI-X is enabled");
7316             /* should be ignored, fall through for now */
7317         }
7318         intms |= data;
7319         stl_le_p(&n->bar.intms, intms);
7320         n->bar.intmc = n->bar.intms;
7321         trace_pci_nvme_mmio_intm_set(data & 0xffffffff, intms);
7322         nvme_irq_check(n);
7323         break;
7324     case NVME_REG_INTMC:
7325         if (unlikely(msix_enabled(pci))) {
7326             NVME_GUEST_ERR(pci_nvme_ub_mmiowr_intmask_with_msix,
7327                            "undefined access to interrupt mask clr"
7328                            " when MSI-X is enabled");
7329             /* should be ignored, fall through for now */
7330         }
7331         intms &= ~data;
7332         stl_le_p(&n->bar.intms, intms);
7333         n->bar.intmc = n->bar.intms;
7334         trace_pci_nvme_mmio_intm_clr(data & 0xffffffff, intms);
7335         nvme_irq_check(n);
7336         break;
7337     case NVME_REG_CC:
7338         stl_le_p(&n->bar.cc, data);
7339 
7340         trace_pci_nvme_mmio_cfg(data & 0xffffffff);
7341 
7342         if (NVME_CC_SHN(data) && !(NVME_CC_SHN(cc))) {
7343             trace_pci_nvme_mmio_shutdown_set();
7344             nvme_ctrl_shutdown(n);
7345             csts &= ~(CSTS_SHST_MASK << CSTS_SHST_SHIFT);
7346             csts |= NVME_CSTS_SHST_COMPLETE;
7347         } else if (!NVME_CC_SHN(data) && NVME_CC_SHN(cc)) {
7348             trace_pci_nvme_mmio_shutdown_cleared();
7349             csts &= ~(CSTS_SHST_MASK << CSTS_SHST_SHIFT);
7350         }
7351 
7352         if (NVME_CC_EN(data) && !NVME_CC_EN(cc)) {
7353             if (unlikely(nvme_start_ctrl(n))) {
7354                 trace_pci_nvme_err_startfail();
7355                 csts = NVME_CSTS_FAILED;
7356             } else {
7357                 trace_pci_nvme_mmio_start_success();
7358                 csts = NVME_CSTS_READY;
7359             }
7360         } else if (!NVME_CC_EN(data) && NVME_CC_EN(cc)) {
7361             trace_pci_nvme_mmio_stopped();
7362             nvme_ctrl_reset(n, NVME_RESET_CONTROLLER);
7363 
7364             break;
7365         }
7366 
7367         stl_le_p(&n->bar.csts, csts);
7368 
7369         break;
7370     case NVME_REG_CSTS:
7371         if (data & (1 << 4)) {
7372             NVME_GUEST_ERR(pci_nvme_ub_mmiowr_ssreset_w1c_unsupported,
7373                            "attempted to W1C CSTS.NSSRO"
7374                            " but CAP.NSSRS is zero (not supported)");
7375         } else if (data != 0) {
7376             NVME_GUEST_ERR(pci_nvme_ub_mmiowr_ro_csts,
7377                            "attempted to set a read only bit"
7378                            " of controller status");
7379         }
7380         break;
7381     case NVME_REG_NSSR:
7382         if (data == 0x4e564d65) {
7383             trace_pci_nvme_ub_mmiowr_ssreset_unsupported();
7384         } else {
7385             /* The spec says that writes of other values have no effect */
7386             return;
7387         }
7388         break;
7389     case NVME_REG_AQA:
7390         stl_le_p(&n->bar.aqa, data);
7391         trace_pci_nvme_mmio_aqattr(data & 0xffffffff);
7392         break;
7393     case NVME_REG_ASQ:
7394         stn_le_p(&n->bar.asq, size, data);
7395         trace_pci_nvme_mmio_asqaddr(data);
7396         break;
7397     case NVME_REG_ASQ + 4:
7398         stl_le_p((uint8_t *)&n->bar.asq + 4, data);
7399         trace_pci_nvme_mmio_asqaddr_hi(data, ldq_le_p(&n->bar.asq));
7400         break;
7401     case NVME_REG_ACQ:
7402         trace_pci_nvme_mmio_acqaddr(data);
7403         stn_le_p(&n->bar.acq, size, data);
7404         break;
7405     case NVME_REG_ACQ + 4:
7406         stl_le_p((uint8_t *)&n->bar.acq + 4, data);
7407         trace_pci_nvme_mmio_acqaddr_hi(data, ldq_le_p(&n->bar.acq));
7408         break;
7409     case NVME_REG_CMBLOC:
7410         NVME_GUEST_ERR(pci_nvme_ub_mmiowr_cmbloc_reserved,
7411                        "invalid write to reserved CMBLOC"
7412                        " when CMBSZ is zero, ignored");
7413         return;
7414     case NVME_REG_CMBSZ:
7415         NVME_GUEST_ERR(pci_nvme_ub_mmiowr_cmbsz_readonly,
7416                        "invalid write to read only CMBSZ, ignored");
7417         return;
7418     case NVME_REG_CMBMSC:
7419         if (!NVME_CAP_CMBS(cap)) {
7420             return;
7421         }
7422 
7423         stn_le_p(&n->bar.cmbmsc, size, data);
7424         n->cmb.cmse = false;
7425 
7426         if (NVME_CMBMSC_CRE(data)) {
7427             nvme_cmb_enable_regs(n);
7428 
7429             if (NVME_CMBMSC_CMSE(data)) {
7430                 uint64_t cmbmsc = ldq_le_p(&n->bar.cmbmsc);
7431                 hwaddr cba = NVME_CMBMSC_CBA(cmbmsc) << CMBMSC_CBA_SHIFT;
7432                 if (cba + int128_get64(n->cmb.mem.size) < cba) {
7433                     uint32_t cmbsts = ldl_le_p(&n->bar.cmbsts);
7434                     NVME_CMBSTS_SET_CBAI(cmbsts, 1);
7435                     stl_le_p(&n->bar.cmbsts, cmbsts);
7436                     return;
7437                 }
7438 
7439                 n->cmb.cba = cba;
7440                 n->cmb.cmse = true;
7441             }
7442         } else {
7443             n->bar.cmbsz = 0;
7444             n->bar.cmbloc = 0;
7445         }
7446 
7447         return;
7448     case NVME_REG_CMBMSC + 4:
7449         stl_le_p((uint8_t *)&n->bar.cmbmsc + 4, data);
7450         return;
7451 
7452     case NVME_REG_PMRCAP:
7453         NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrcap_readonly,
7454                        "invalid write to PMRCAP register, ignored");
7455         return;
7456     case NVME_REG_PMRCTL:
7457         if (!NVME_CAP_PMRS(cap)) {
7458             return;
7459         }
7460 
7461         stl_le_p(&n->bar.pmrctl, data);
7462         if (NVME_PMRCTL_EN(data)) {
7463             memory_region_set_enabled(&n->pmr.dev->mr, true);
7464             pmrsts = 0;
7465         } else {
7466             memory_region_set_enabled(&n->pmr.dev->mr, false);
7467             NVME_PMRSTS_SET_NRDY(pmrsts, 1);
7468             n->pmr.cmse = false;
7469         }
7470         stl_le_p(&n->bar.pmrsts, pmrsts);
7471         return;
7472     case NVME_REG_PMRSTS:
7473         NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrsts_readonly,
7474                        "invalid write to PMRSTS register, ignored");
7475         return;
7476     case NVME_REG_PMREBS:
7477         NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrebs_readonly,
7478                        "invalid write to PMREBS register, ignored");
7479         return;
7480     case NVME_REG_PMRSWTP:
7481         NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrswtp_readonly,
7482                        "invalid write to PMRSWTP register, ignored");
7483         return;
7484     case NVME_REG_PMRMSCL:
7485         if (!NVME_CAP_PMRS(cap)) {
7486             return;
7487         }
7488 
7489         stl_le_p(&n->bar.pmrmscl, data);
7490         n->pmr.cmse = false;
7491 
7492         if (NVME_PMRMSCL_CMSE(data)) {
7493             uint64_t pmrmscu = ldl_le_p(&n->bar.pmrmscu);
7494             hwaddr cba = pmrmscu << 32 |
7495                 (NVME_PMRMSCL_CBA(data) << PMRMSCL_CBA_SHIFT);
7496             if (cba + int128_get64(n->pmr.dev->mr.size) < cba) {
7497                 NVME_PMRSTS_SET_CBAI(pmrsts, 1);
7498                 stl_le_p(&n->bar.pmrsts, pmrsts);
7499                 return;
7500             }
7501 
7502             n->pmr.cmse = true;
7503             n->pmr.cba = cba;
7504         }
7505 
7506         return;
7507     case NVME_REG_PMRMSCU:
7508         if (!NVME_CAP_PMRS(cap)) {
7509             return;
7510         }
7511 
7512         stl_le_p(&n->bar.pmrmscu, data);
7513         return;
7514     default:
7515         NVME_GUEST_ERR(pci_nvme_ub_mmiowr_invalid,
7516                        "invalid MMIO write,"
7517                        " offset=0x%"PRIx64", data=%"PRIx64"",
7518                        offset, data);
7519         break;
7520     }
7521 }
7522 
7523 static uint64_t nvme_mmio_read(void *opaque, hwaddr addr, unsigned size)
7524 {
7525     NvmeCtrl *n = (NvmeCtrl *)opaque;
7526     uint8_t *ptr = (uint8_t *)&n->bar;
7527 
7528     trace_pci_nvme_mmio_read(addr, size);
7529 
7530     if (unlikely(addr & (sizeof(uint32_t) - 1))) {
7531         NVME_GUEST_ERR(pci_nvme_ub_mmiord_misaligned32,
7532                        "MMIO read not 32-bit aligned,"
7533                        " offset=0x%"PRIx64"", addr);
7534         /* should RAZ, fall through for now */
7535     } else if (unlikely(size < sizeof(uint32_t))) {
7536         NVME_GUEST_ERR(pci_nvme_ub_mmiord_toosmall,
7537                        "MMIO read smaller than 32-bits,"
7538                        " offset=0x%"PRIx64"", addr);
7539         /* should RAZ, fall through for now */
7540     }
7541 
7542     if (addr > sizeof(n->bar) - size) {
7543         NVME_GUEST_ERR(pci_nvme_ub_mmiord_invalid_ofs,
7544                        "MMIO read beyond last register,"
7545                        " offset=0x%"PRIx64", returning 0", addr);
7546 
7547         return 0;
7548     }
7549 
7550     if (pci_is_vf(PCI_DEVICE(n)) && !nvme_sctrl(n)->scs &&
7551         addr != NVME_REG_CSTS) {
7552         trace_pci_nvme_err_ignored_mmio_vf_offline(addr, size);
7553         return 0;
7554     }
7555 
7556     /*
7557      * When PMRWBM bit 1 is set then read from
7558      * from PMRSTS should ensure prior writes
7559      * made it to persistent media
7560      */
7561     if (addr == NVME_REG_PMRSTS &&
7562         (NVME_PMRCAP_PMRWBM(ldl_le_p(&n->bar.pmrcap)) & 0x02)) {
7563         memory_region_msync(&n->pmr.dev->mr, 0, n->pmr.dev->size);
7564     }
7565 
7566     return ldn_le_p(ptr + addr, size);
7567 }
7568 
7569 static void nvme_process_db(NvmeCtrl *n, hwaddr addr, int val)
7570 {
7571     PCIDevice *pci = PCI_DEVICE(n);
7572     uint32_t qid;
7573 
7574     if (unlikely(addr & ((1 << 2) - 1))) {
7575         NVME_GUEST_ERR(pci_nvme_ub_db_wr_misaligned,
7576                        "doorbell write not 32-bit aligned,"
7577                        " offset=0x%"PRIx64", ignoring", addr);
7578         return;
7579     }
7580 
7581     if (((addr - 0x1000) >> 2) & 1) {
7582         /* Completion queue doorbell write */
7583 
7584         uint16_t new_head = val & 0xffff;
7585         int start_sqs;
7586         NvmeCQueue *cq;
7587 
7588         qid = (addr - (0x1000 + (1 << 2))) >> 3;
7589         if (unlikely(nvme_check_cqid(n, qid))) {
7590             NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_cq,
7591                            "completion queue doorbell write"
7592                            " for nonexistent queue,"
7593                            " sqid=%"PRIu32", ignoring", qid);
7594 
7595             /*
7596              * NVM Express v1.3d, Section 4.1 state: "If host software writes
7597              * an invalid value to the Submission Queue Tail Doorbell or
7598              * Completion Queue Head Doorbell register and an Asynchronous Event
7599              * Request command is outstanding, then an asynchronous event is
7600              * posted to the Admin Completion Queue with a status code of
7601              * Invalid Doorbell Write Value."
7602              *
7603              * Also note that the spec includes the "Invalid Doorbell Register"
7604              * status code, but nowhere does it specify when to use it.
7605              * However, it seems reasonable to use it here in a similar
7606              * fashion.
7607              */
7608             if (n->outstanding_aers) {
7609                 nvme_enqueue_event(n, NVME_AER_TYPE_ERROR,
7610                                    NVME_AER_INFO_ERR_INVALID_DB_REGISTER,
7611                                    NVME_LOG_ERROR_INFO);
7612             }
7613 
7614             return;
7615         }
7616 
7617         cq = n->cq[qid];
7618         if (unlikely(new_head >= cq->size)) {
7619             NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_cqhead,
7620                            "completion queue doorbell write value"
7621                            " beyond queue size, sqid=%"PRIu32","
7622                            " new_head=%"PRIu16", ignoring",
7623                            qid, new_head);
7624 
7625             if (n->outstanding_aers) {
7626                 nvme_enqueue_event(n, NVME_AER_TYPE_ERROR,
7627                                    NVME_AER_INFO_ERR_INVALID_DB_VALUE,
7628                                    NVME_LOG_ERROR_INFO);
7629             }
7630 
7631             return;
7632         }
7633 
7634         trace_pci_nvme_mmio_doorbell_cq(cq->cqid, new_head);
7635 
7636         start_sqs = nvme_cq_full(cq) ? 1 : 0;
7637         cq->head = new_head;
7638         if (!qid && n->dbbuf_enabled) {
7639             stl_le_pci_dma(pci, cq->db_addr, cq->head, MEMTXATTRS_UNSPECIFIED);
7640         }
7641         if (start_sqs) {
7642             NvmeSQueue *sq;
7643             QTAILQ_FOREACH(sq, &cq->sq_list, entry) {
7644                 qemu_bh_schedule(sq->bh);
7645             }
7646             qemu_bh_schedule(cq->bh);
7647         }
7648 
7649         if (cq->tail == cq->head) {
7650             if (cq->irq_enabled) {
7651                 n->cq_pending--;
7652             }
7653 
7654             nvme_irq_deassert(n, cq);
7655         }
7656     } else {
7657         /* Submission queue doorbell write */
7658 
7659         uint16_t new_tail = val & 0xffff;
7660         NvmeSQueue *sq;
7661 
7662         qid = (addr - 0x1000) >> 3;
7663         if (unlikely(nvme_check_sqid(n, qid))) {
7664             NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_sq,
7665                            "submission queue doorbell write"
7666                            " for nonexistent queue,"
7667                            " sqid=%"PRIu32", ignoring", qid);
7668 
7669             if (n->outstanding_aers) {
7670                 nvme_enqueue_event(n, NVME_AER_TYPE_ERROR,
7671                                    NVME_AER_INFO_ERR_INVALID_DB_REGISTER,
7672                                    NVME_LOG_ERROR_INFO);
7673             }
7674 
7675             return;
7676         }
7677 
7678         sq = n->sq[qid];
7679         if (unlikely(new_tail >= sq->size)) {
7680             NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_sqtail,
7681                            "submission queue doorbell write value"
7682                            " beyond queue size, sqid=%"PRIu32","
7683                            " new_tail=%"PRIu16", ignoring",
7684                            qid, new_tail);
7685 
7686             if (n->outstanding_aers) {
7687                 nvme_enqueue_event(n, NVME_AER_TYPE_ERROR,
7688                                    NVME_AER_INFO_ERR_INVALID_DB_VALUE,
7689                                    NVME_LOG_ERROR_INFO);
7690             }
7691 
7692             return;
7693         }
7694 
7695         trace_pci_nvme_mmio_doorbell_sq(sq->sqid, new_tail);
7696 
7697         sq->tail = new_tail;
7698         if (!qid && n->dbbuf_enabled) {
7699             /*
7700              * The spec states "the host shall also update the controller's
7701              * corresponding doorbell property to match the value of that entry
7702              * in the Shadow Doorbell buffer."
7703              *
7704              * Since this context is currently a VM trap, we can safely enforce
7705              * the requirement from the device side in case the host is
7706              * misbehaving.
7707              *
7708              * Note, we shouldn't have to do this, but various drivers
7709              * including ones that run on Linux, are not updating Admin Queues,
7710              * so we can't trust reading it for an appropriate sq tail.
7711              */
7712             stl_le_pci_dma(pci, sq->db_addr, sq->tail, MEMTXATTRS_UNSPECIFIED);
7713         }
7714 
7715         qemu_bh_schedule(sq->bh);
7716     }
7717 }
7718 
7719 static void nvme_mmio_write(void *opaque, hwaddr addr, uint64_t data,
7720                             unsigned size)
7721 {
7722     NvmeCtrl *n = (NvmeCtrl *)opaque;
7723 
7724     trace_pci_nvme_mmio_write(addr, data, size);
7725 
7726     if (pci_is_vf(PCI_DEVICE(n)) && !nvme_sctrl(n)->scs &&
7727         addr != NVME_REG_CSTS) {
7728         trace_pci_nvme_err_ignored_mmio_vf_offline(addr, size);
7729         return;
7730     }
7731 
7732     if (addr < sizeof(n->bar)) {
7733         nvme_write_bar(n, addr, data, size);
7734     } else {
7735         nvme_process_db(n, addr, data);
7736     }
7737 }
7738 
7739 static const MemoryRegionOps nvme_mmio_ops = {
7740     .read = nvme_mmio_read,
7741     .write = nvme_mmio_write,
7742     .endianness = DEVICE_LITTLE_ENDIAN,
7743     .impl = {
7744         .min_access_size = 2,
7745         .max_access_size = 8,
7746     },
7747 };
7748 
7749 static void nvme_cmb_write(void *opaque, hwaddr addr, uint64_t data,
7750                            unsigned size)
7751 {
7752     NvmeCtrl *n = (NvmeCtrl *)opaque;
7753     stn_le_p(&n->cmb.buf[addr], size, data);
7754 }
7755 
7756 static uint64_t nvme_cmb_read(void *opaque, hwaddr addr, unsigned size)
7757 {
7758     NvmeCtrl *n = (NvmeCtrl *)opaque;
7759     return ldn_le_p(&n->cmb.buf[addr], size);
7760 }
7761 
7762 static const MemoryRegionOps nvme_cmb_ops = {
7763     .read = nvme_cmb_read,
7764     .write = nvme_cmb_write,
7765     .endianness = DEVICE_LITTLE_ENDIAN,
7766     .impl = {
7767         .min_access_size = 1,
7768         .max_access_size = 8,
7769     },
7770 };
7771 
7772 static bool nvme_check_params(NvmeCtrl *n, Error **errp)
7773 {
7774     NvmeParams *params = &n->params;
7775 
7776     if (params->num_queues) {
7777         warn_report("num_queues is deprecated; please use max_ioqpairs "
7778                     "instead");
7779 
7780         params->max_ioqpairs = params->num_queues - 1;
7781     }
7782 
7783     if (n->namespace.blkconf.blk && n->subsys) {
7784         error_setg(errp, "subsystem support is unavailable with legacy "
7785                    "namespace ('drive' property)");
7786         return false;
7787     }
7788 
7789     if (params->max_ioqpairs < 1 ||
7790         params->max_ioqpairs > NVME_MAX_IOQPAIRS) {
7791         error_setg(errp, "max_ioqpairs must be between 1 and %d",
7792                    NVME_MAX_IOQPAIRS);
7793         return false;
7794     }
7795 
7796     if (params->msix_qsize < 1 ||
7797         params->msix_qsize > PCI_MSIX_FLAGS_QSIZE + 1) {
7798         error_setg(errp, "msix_qsize must be between 1 and %d",
7799                    PCI_MSIX_FLAGS_QSIZE + 1);
7800         return false;
7801     }
7802 
7803     if (!params->serial) {
7804         error_setg(errp, "serial property not set");
7805         return false;
7806     }
7807 
7808     if (n->pmr.dev) {
7809         if (params->msix_exclusive_bar) {
7810             error_setg(errp, "not enough BARs available to enable PMR");
7811             return false;
7812         }
7813 
7814         if (host_memory_backend_is_mapped(n->pmr.dev)) {
7815             error_setg(errp, "can't use already busy memdev: %s",
7816                        object_get_canonical_path_component(OBJECT(n->pmr.dev)));
7817             return false;
7818         }
7819 
7820         if (!is_power_of_2(n->pmr.dev->size)) {
7821             error_setg(errp, "pmr backend size needs to be power of 2 in size");
7822             return false;
7823         }
7824 
7825         host_memory_backend_set_mapped(n->pmr.dev, true);
7826     }
7827 
7828     if (n->params.zasl > n->params.mdts) {
7829         error_setg(errp, "zoned.zasl (Zone Append Size Limit) must be less "
7830                    "than or equal to mdts (Maximum Data Transfer Size)");
7831         return false;
7832     }
7833 
7834     if (!n->params.vsl) {
7835         error_setg(errp, "vsl must be non-zero");
7836         return false;
7837     }
7838 
7839     if (params->sriov_max_vfs) {
7840         if (!n->subsys) {
7841             error_setg(errp, "subsystem is required for the use of SR-IOV");
7842             return false;
7843         }
7844 
7845         if (params->sriov_max_vfs > NVME_MAX_VFS) {
7846             error_setg(errp, "sriov_max_vfs must be between 0 and %d",
7847                        NVME_MAX_VFS);
7848             return false;
7849         }
7850 
7851         if (params->cmb_size_mb) {
7852             error_setg(errp, "CMB is not supported with SR-IOV");
7853             return false;
7854         }
7855 
7856         if (n->pmr.dev) {
7857             error_setg(errp, "PMR is not supported with SR-IOV");
7858             return false;
7859         }
7860 
7861         if (!params->sriov_vq_flexible || !params->sriov_vi_flexible) {
7862             error_setg(errp, "both sriov_vq_flexible and sriov_vi_flexible"
7863                        " must be set for the use of SR-IOV");
7864             return false;
7865         }
7866 
7867         if (params->sriov_vq_flexible < params->sriov_max_vfs * 2) {
7868             error_setg(errp, "sriov_vq_flexible must be greater than or equal"
7869                        " to %d (sriov_max_vfs * 2)", params->sriov_max_vfs * 2);
7870             return false;
7871         }
7872 
7873         if (params->max_ioqpairs < params->sriov_vq_flexible + 2) {
7874             error_setg(errp, "(max_ioqpairs - sriov_vq_flexible) must be"
7875                        " greater than or equal to 2");
7876             return false;
7877         }
7878 
7879         if (params->sriov_vi_flexible < params->sriov_max_vfs) {
7880             error_setg(errp, "sriov_vi_flexible must be greater than or equal"
7881                        " to %d (sriov_max_vfs)", params->sriov_max_vfs);
7882             return false;
7883         }
7884 
7885         if (params->msix_qsize < params->sriov_vi_flexible + 1) {
7886             error_setg(errp, "(msix_qsize - sriov_vi_flexible) must be"
7887                        " greater than or equal to 1");
7888             return false;
7889         }
7890 
7891         if (params->sriov_max_vi_per_vf &&
7892             (params->sriov_max_vi_per_vf - 1) % NVME_VF_RES_GRANULARITY) {
7893             error_setg(errp, "sriov_max_vi_per_vf must meet:"
7894                        " (sriov_max_vi_per_vf - 1) %% %d == 0 and"
7895                        " sriov_max_vi_per_vf >= 1", NVME_VF_RES_GRANULARITY);
7896             return false;
7897         }
7898 
7899         if (params->sriov_max_vq_per_vf &&
7900             (params->sriov_max_vq_per_vf < 2 ||
7901              (params->sriov_max_vq_per_vf - 1) % NVME_VF_RES_GRANULARITY)) {
7902             error_setg(errp, "sriov_max_vq_per_vf must meet:"
7903                        " (sriov_max_vq_per_vf - 1) %% %d == 0 and"
7904                        " sriov_max_vq_per_vf >= 2", NVME_VF_RES_GRANULARITY);
7905             return false;
7906         }
7907     }
7908 
7909     return true;
7910 }
7911 
7912 static void nvme_init_state(NvmeCtrl *n)
7913 {
7914     NvmePriCtrlCap *cap = &n->pri_ctrl_cap;
7915     NvmeSecCtrlList *list = &n->sec_ctrl_list;
7916     NvmeSecCtrlEntry *sctrl;
7917     PCIDevice *pci = PCI_DEVICE(n);
7918     uint8_t max_vfs;
7919     int i;
7920 
7921     if (pci_is_vf(pci)) {
7922         sctrl = nvme_sctrl(n);
7923         max_vfs = 0;
7924         n->conf_ioqpairs = sctrl->nvq ? le16_to_cpu(sctrl->nvq) - 1 : 0;
7925         n->conf_msix_qsize = sctrl->nvi ? le16_to_cpu(sctrl->nvi) : 1;
7926     } else {
7927         max_vfs = n->params.sriov_max_vfs;
7928         n->conf_ioqpairs = n->params.max_ioqpairs;
7929         n->conf_msix_qsize = n->params.msix_qsize;
7930     }
7931 
7932     n->sq = g_new0(NvmeSQueue *, n->params.max_ioqpairs + 1);
7933     n->cq = g_new0(NvmeCQueue *, n->params.max_ioqpairs + 1);
7934     n->temperature = NVME_TEMPERATURE;
7935     n->features.temp_thresh_hi = NVME_TEMPERATURE_WARNING;
7936     n->starttime_ms = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
7937     n->aer_reqs = g_new0(NvmeRequest *, n->params.aerl + 1);
7938     QTAILQ_INIT(&n->aer_queue);
7939 
7940     list->numcntl = max_vfs;
7941     for (i = 0; i < max_vfs; i++) {
7942         sctrl = &list->sec[i];
7943         sctrl->pcid = cpu_to_le16(n->cntlid);
7944         sctrl->vfn = cpu_to_le16(i + 1);
7945     }
7946 
7947     cap->cntlid = cpu_to_le16(n->cntlid);
7948     cap->crt = NVME_CRT_VQ | NVME_CRT_VI;
7949 
7950     if (pci_is_vf(pci)) {
7951         cap->vqprt = cpu_to_le16(1 + n->conf_ioqpairs);
7952     } else {
7953         cap->vqprt = cpu_to_le16(1 + n->params.max_ioqpairs -
7954                                  n->params.sriov_vq_flexible);
7955         cap->vqfrt = cpu_to_le32(n->params.sriov_vq_flexible);
7956         cap->vqrfap = cap->vqfrt;
7957         cap->vqgran = cpu_to_le16(NVME_VF_RES_GRANULARITY);
7958         cap->vqfrsm = n->params.sriov_max_vq_per_vf ?
7959                         cpu_to_le16(n->params.sriov_max_vq_per_vf) :
7960                         cap->vqfrt / MAX(max_vfs, 1);
7961     }
7962 
7963     if (pci_is_vf(pci)) {
7964         cap->viprt = cpu_to_le16(n->conf_msix_qsize);
7965     } else {
7966         cap->viprt = cpu_to_le16(n->params.msix_qsize -
7967                                  n->params.sriov_vi_flexible);
7968         cap->vifrt = cpu_to_le32(n->params.sriov_vi_flexible);
7969         cap->virfap = cap->vifrt;
7970         cap->vigran = cpu_to_le16(NVME_VF_RES_GRANULARITY);
7971         cap->vifrsm = n->params.sriov_max_vi_per_vf ?
7972                         cpu_to_le16(n->params.sriov_max_vi_per_vf) :
7973                         cap->vifrt / MAX(max_vfs, 1);
7974     }
7975 }
7976 
7977 static void nvme_init_cmb(NvmeCtrl *n, PCIDevice *pci_dev)
7978 {
7979     uint64_t cmb_size = n->params.cmb_size_mb * MiB;
7980     uint64_t cap = ldq_le_p(&n->bar.cap);
7981 
7982     n->cmb.buf = g_malloc0(cmb_size);
7983     memory_region_init_io(&n->cmb.mem, OBJECT(n), &nvme_cmb_ops, n,
7984                           "nvme-cmb", cmb_size);
7985     pci_register_bar(pci_dev, NVME_CMB_BIR,
7986                      PCI_BASE_ADDRESS_SPACE_MEMORY |
7987                      PCI_BASE_ADDRESS_MEM_TYPE_64 |
7988                      PCI_BASE_ADDRESS_MEM_PREFETCH, &n->cmb.mem);
7989 
7990     NVME_CAP_SET_CMBS(cap, 1);
7991     stq_le_p(&n->bar.cap, cap);
7992 
7993     if (n->params.legacy_cmb) {
7994         nvme_cmb_enable_regs(n);
7995         n->cmb.cmse = true;
7996     }
7997 }
7998 
7999 static void nvme_init_pmr(NvmeCtrl *n, PCIDevice *pci_dev)
8000 {
8001     uint32_t pmrcap = ldl_le_p(&n->bar.pmrcap);
8002 
8003     NVME_PMRCAP_SET_RDS(pmrcap, 1);
8004     NVME_PMRCAP_SET_WDS(pmrcap, 1);
8005     NVME_PMRCAP_SET_BIR(pmrcap, NVME_PMR_BIR);
8006     /* Turn on bit 1 support */
8007     NVME_PMRCAP_SET_PMRWBM(pmrcap, 0x02);
8008     NVME_PMRCAP_SET_CMSS(pmrcap, 1);
8009     stl_le_p(&n->bar.pmrcap, pmrcap);
8010 
8011     pci_register_bar(pci_dev, NVME_PMR_BIR,
8012                      PCI_BASE_ADDRESS_SPACE_MEMORY |
8013                      PCI_BASE_ADDRESS_MEM_TYPE_64 |
8014                      PCI_BASE_ADDRESS_MEM_PREFETCH, &n->pmr.dev->mr);
8015 
8016     memory_region_set_enabled(&n->pmr.dev->mr, false);
8017 }
8018 
8019 static uint64_t nvme_mbar_size(unsigned total_queues, unsigned total_irqs,
8020                                unsigned *msix_table_offset,
8021                                unsigned *msix_pba_offset)
8022 {
8023     uint64_t bar_size, msix_table_size;
8024 
8025     bar_size = sizeof(NvmeBar) + 2 * total_queues * NVME_DB_SIZE;
8026 
8027     if (total_irqs == 0) {
8028         goto out;
8029     }
8030 
8031     bar_size = QEMU_ALIGN_UP(bar_size, 4 * KiB);
8032 
8033     if (msix_table_offset) {
8034         *msix_table_offset = bar_size;
8035     }
8036 
8037     msix_table_size = PCI_MSIX_ENTRY_SIZE * total_irqs;
8038     bar_size += msix_table_size;
8039     bar_size = QEMU_ALIGN_UP(bar_size, 4 * KiB);
8040 
8041     if (msix_pba_offset) {
8042         *msix_pba_offset = bar_size;
8043     }
8044 
8045     bar_size += QEMU_ALIGN_UP(total_irqs, 64) / 8;
8046 
8047 out:
8048     return pow2ceil(bar_size);
8049 }
8050 
8051 static void nvme_init_sriov(NvmeCtrl *n, PCIDevice *pci_dev, uint16_t offset)
8052 {
8053     uint16_t vf_dev_id = n->params.use_intel_id ?
8054                          PCI_DEVICE_ID_INTEL_NVME : PCI_DEVICE_ID_REDHAT_NVME;
8055     NvmePriCtrlCap *cap = &n->pri_ctrl_cap;
8056     uint64_t bar_size = nvme_mbar_size(le16_to_cpu(cap->vqfrsm),
8057                                       le16_to_cpu(cap->vifrsm),
8058                                       NULL, NULL);
8059 
8060     pcie_sriov_pf_init(pci_dev, offset, "nvme", vf_dev_id,
8061                        n->params.sriov_max_vfs, n->params.sriov_max_vfs,
8062                        NVME_VF_OFFSET, NVME_VF_STRIDE);
8063 
8064     pcie_sriov_pf_init_vf_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY |
8065                               PCI_BASE_ADDRESS_MEM_TYPE_64, bar_size);
8066 }
8067 
8068 static int nvme_add_pm_capability(PCIDevice *pci_dev, uint8_t offset)
8069 {
8070     Error *err = NULL;
8071     int ret;
8072 
8073     ret = pci_add_capability(pci_dev, PCI_CAP_ID_PM, offset,
8074                              PCI_PM_SIZEOF, &err);
8075     if (err) {
8076         error_report_err(err);
8077         return ret;
8078     }
8079 
8080     pci_set_word(pci_dev->config + offset + PCI_PM_PMC,
8081                  PCI_PM_CAP_VER_1_2);
8082     pci_set_word(pci_dev->config + offset + PCI_PM_CTRL,
8083                  PCI_PM_CTRL_NO_SOFT_RESET);
8084     pci_set_word(pci_dev->wmask + offset + PCI_PM_CTRL,
8085                  PCI_PM_CTRL_STATE_MASK);
8086 
8087     return 0;
8088 }
8089 
8090 static bool nvme_init_pci(NvmeCtrl *n, PCIDevice *pci_dev, Error **errp)
8091 {
8092     ERRP_GUARD();
8093     uint8_t *pci_conf = pci_dev->config;
8094     uint64_t bar_size;
8095     unsigned msix_table_offset = 0, msix_pba_offset = 0;
8096     int ret;
8097 
8098     pci_conf[PCI_INTERRUPT_PIN] = 1;
8099     pci_config_set_prog_interface(pci_conf, 0x2);
8100 
8101     if (n->params.use_intel_id) {
8102         pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
8103         pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_NVME);
8104     } else {
8105         pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_REDHAT);
8106         pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_REDHAT_NVME);
8107     }
8108 
8109     pci_config_set_class(pci_conf, PCI_CLASS_STORAGE_EXPRESS);
8110     nvme_add_pm_capability(pci_dev, 0x60);
8111     pcie_endpoint_cap_init(pci_dev, 0x80);
8112     pcie_cap_flr_init(pci_dev);
8113     if (n->params.sriov_max_vfs) {
8114         pcie_ari_init(pci_dev, 0x100);
8115     }
8116 
8117     if (n->params.msix_exclusive_bar && !pci_is_vf(pci_dev)) {
8118         bar_size = nvme_mbar_size(n->params.max_ioqpairs + 1, 0, NULL, NULL);
8119         memory_region_init_io(&n->iomem, OBJECT(n), &nvme_mmio_ops, n, "nvme",
8120                               bar_size);
8121         pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY |
8122                          PCI_BASE_ADDRESS_MEM_TYPE_64, &n->iomem);
8123         ret = msix_init_exclusive_bar(pci_dev, n->params.msix_qsize, 4, errp);
8124     } else {
8125         assert(n->params.msix_qsize >= 1);
8126 
8127         /* add one to max_ioqpairs to account for the admin queue pair */
8128         bar_size = nvme_mbar_size(n->params.max_ioqpairs + 1,
8129                                   n->params.msix_qsize, &msix_table_offset,
8130                                   &msix_pba_offset);
8131 
8132         memory_region_init(&n->bar0, OBJECT(n), "nvme-bar0", bar_size);
8133         memory_region_init_io(&n->iomem, OBJECT(n), &nvme_mmio_ops, n, "nvme",
8134                               msix_table_offset);
8135         memory_region_add_subregion(&n->bar0, 0, &n->iomem);
8136 
8137         if (pci_is_vf(pci_dev)) {
8138             pcie_sriov_vf_register_bar(pci_dev, 0, &n->bar0);
8139         } else {
8140             pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY |
8141                              PCI_BASE_ADDRESS_MEM_TYPE_64, &n->bar0);
8142         }
8143 
8144         ret = msix_init(pci_dev, n->params.msix_qsize,
8145                         &n->bar0, 0, msix_table_offset,
8146                         &n->bar0, 0, msix_pba_offset, 0, errp);
8147     }
8148 
8149     if (ret == -ENOTSUP) {
8150         /* report that msix is not supported, but do not error out */
8151         warn_report_err(*errp);
8152         *errp = NULL;
8153     } else if (ret < 0) {
8154         /* propagate error to caller */
8155         return false;
8156     }
8157 
8158     nvme_update_msixcap_ts(pci_dev, n->conf_msix_qsize);
8159 
8160     if (n->params.cmb_size_mb) {
8161         nvme_init_cmb(n, pci_dev);
8162     }
8163 
8164     if (n->pmr.dev) {
8165         nvme_init_pmr(n, pci_dev);
8166     }
8167 
8168     if (!pci_is_vf(pci_dev) && n->params.sriov_max_vfs) {
8169         nvme_init_sriov(n, pci_dev, 0x120);
8170     }
8171 
8172     return true;
8173 }
8174 
8175 static void nvme_init_subnqn(NvmeCtrl *n)
8176 {
8177     NvmeSubsystem *subsys = n->subsys;
8178     NvmeIdCtrl *id = &n->id_ctrl;
8179 
8180     if (!subsys) {
8181         snprintf((char *)id->subnqn, sizeof(id->subnqn),
8182                  "nqn.2019-08.org.qemu:%s", n->params.serial);
8183     } else {
8184         pstrcpy((char *)id->subnqn, sizeof(id->subnqn), (char*)subsys->subnqn);
8185     }
8186 }
8187 
8188 static void nvme_init_ctrl(NvmeCtrl *n, PCIDevice *pci_dev)
8189 {
8190     NvmeIdCtrl *id = &n->id_ctrl;
8191     uint8_t *pci_conf = pci_dev->config;
8192     uint64_t cap = ldq_le_p(&n->bar.cap);
8193     NvmeSecCtrlEntry *sctrl = nvme_sctrl(n);
8194     uint32_t ctratt;
8195 
8196     id->vid = cpu_to_le16(pci_get_word(pci_conf + PCI_VENDOR_ID));
8197     id->ssvid = cpu_to_le16(pci_get_word(pci_conf + PCI_SUBSYSTEM_VENDOR_ID));
8198     strpadcpy((char *)id->mn, sizeof(id->mn), "QEMU NVMe Ctrl", ' ');
8199     strpadcpy((char *)id->fr, sizeof(id->fr), QEMU_VERSION, ' ');
8200     strpadcpy((char *)id->sn, sizeof(id->sn), n->params.serial, ' ');
8201 
8202     id->cntlid = cpu_to_le16(n->cntlid);
8203 
8204     id->oaes = cpu_to_le32(NVME_OAES_NS_ATTR);
8205     ctratt = NVME_CTRATT_ELBAS;
8206 
8207     id->rab = 6;
8208 
8209     if (n->params.use_intel_id) {
8210         id->ieee[0] = 0xb3;
8211         id->ieee[1] = 0x02;
8212         id->ieee[2] = 0x00;
8213     } else {
8214         id->ieee[0] = 0x00;
8215         id->ieee[1] = 0x54;
8216         id->ieee[2] = 0x52;
8217     }
8218 
8219     id->mdts = n->params.mdts;
8220     id->ver = cpu_to_le32(NVME_SPEC_VER);
8221     id->oacs =
8222         cpu_to_le16(NVME_OACS_NS_MGMT | NVME_OACS_FORMAT | NVME_OACS_DBBUF |
8223                     NVME_OACS_DIRECTIVES);
8224     id->cntrltype = 0x1;
8225 
8226     /*
8227      * Because the controller always completes the Abort command immediately,
8228      * there can never be more than one concurrently executing Abort command,
8229      * so this value is never used for anything. Note that there can easily be
8230      * many Abort commands in the queues, but they are not considered
8231      * "executing" until processed by nvme_abort.
8232      *
8233      * The specification recommends a value of 3 for Abort Command Limit (four
8234      * concurrently outstanding Abort commands), so lets use that though it is
8235      * inconsequential.
8236      */
8237     id->acl = 3;
8238     id->aerl = n->params.aerl;
8239     id->frmw = (NVME_NUM_FW_SLOTS << 1) | NVME_FRMW_SLOT1_RO;
8240     id->lpa = NVME_LPA_NS_SMART | NVME_LPA_CSE | NVME_LPA_EXTENDED;
8241 
8242     /* recommended default value (~70 C) */
8243     id->wctemp = cpu_to_le16(NVME_TEMPERATURE_WARNING);
8244     id->cctemp = cpu_to_le16(NVME_TEMPERATURE_CRITICAL);
8245 
8246     id->sqes = (NVME_SQES << 4) | NVME_SQES;
8247     id->cqes = (NVME_CQES << 4) | NVME_CQES;
8248     id->nn = cpu_to_le32(NVME_MAX_NAMESPACES);
8249     id->oncs = cpu_to_le16(NVME_ONCS_WRITE_ZEROES | NVME_ONCS_TIMESTAMP |
8250                            NVME_ONCS_FEATURES | NVME_ONCS_DSM |
8251                            NVME_ONCS_COMPARE | NVME_ONCS_COPY);
8252 
8253     /*
8254      * NOTE: If this device ever supports a command set that does NOT use 0x0
8255      * as a Flush-equivalent operation, support for the broadcast NSID in Flush
8256      * should probably be removed.
8257      *
8258      * See comment in nvme_io_cmd.
8259      */
8260     id->vwc = NVME_VWC_NSID_BROADCAST_SUPPORT | NVME_VWC_PRESENT;
8261 
8262     id->ocfs = cpu_to_le16(NVME_OCFS_COPY_FORMAT_0 | NVME_OCFS_COPY_FORMAT_1);
8263     id->sgls = cpu_to_le32(NVME_CTRL_SGLS_SUPPORT_NO_ALIGN);
8264 
8265     nvme_init_subnqn(n);
8266 
8267     id->psd[0].mp = cpu_to_le16(0x9c4);
8268     id->psd[0].enlat = cpu_to_le32(0x10);
8269     id->psd[0].exlat = cpu_to_le32(0x4);
8270 
8271     if (n->subsys) {
8272         id->cmic |= NVME_CMIC_MULTI_CTRL;
8273         ctratt |= NVME_CTRATT_ENDGRPS;
8274 
8275         id->endgidmax = cpu_to_le16(0x1);
8276 
8277         if (n->subsys->endgrp.fdp.enabled) {
8278             ctratt |= NVME_CTRATT_FDPS;
8279         }
8280     }
8281 
8282     id->ctratt = cpu_to_le32(ctratt);
8283 
8284     NVME_CAP_SET_MQES(cap, 0x7ff);
8285     NVME_CAP_SET_CQR(cap, 1);
8286     NVME_CAP_SET_TO(cap, 0xf);
8287     NVME_CAP_SET_CSS(cap, NVME_CAP_CSS_NVM);
8288     NVME_CAP_SET_CSS(cap, NVME_CAP_CSS_CSI_SUPP);
8289     NVME_CAP_SET_CSS(cap, NVME_CAP_CSS_ADMIN_ONLY);
8290     NVME_CAP_SET_MPSMAX(cap, 4);
8291     NVME_CAP_SET_CMBS(cap, n->params.cmb_size_mb ? 1 : 0);
8292     NVME_CAP_SET_PMRS(cap, n->pmr.dev ? 1 : 0);
8293     stq_le_p(&n->bar.cap, cap);
8294 
8295     stl_le_p(&n->bar.vs, NVME_SPEC_VER);
8296     n->bar.intmc = n->bar.intms = 0;
8297 
8298     if (pci_is_vf(pci_dev) && !sctrl->scs) {
8299         stl_le_p(&n->bar.csts, NVME_CSTS_FAILED);
8300     }
8301 }
8302 
8303 static int nvme_init_subsys(NvmeCtrl *n, Error **errp)
8304 {
8305     int cntlid;
8306 
8307     if (!n->subsys) {
8308         return 0;
8309     }
8310 
8311     cntlid = nvme_subsys_register_ctrl(n, errp);
8312     if (cntlid < 0) {
8313         return -1;
8314     }
8315 
8316     n->cntlid = cntlid;
8317 
8318     return 0;
8319 }
8320 
8321 void nvme_attach_ns(NvmeCtrl *n, NvmeNamespace *ns)
8322 {
8323     uint32_t nsid = ns->params.nsid;
8324     assert(nsid && nsid <= NVME_MAX_NAMESPACES);
8325 
8326     n->namespaces[nsid] = ns;
8327     ns->attached++;
8328 
8329     n->dmrsl = MIN_NON_ZERO(n->dmrsl,
8330                             BDRV_REQUEST_MAX_BYTES / nvme_l2b(ns, 1));
8331 }
8332 
8333 static void nvme_realize(PCIDevice *pci_dev, Error **errp)
8334 {
8335     NvmeCtrl *n = NVME(pci_dev);
8336     DeviceState *dev = DEVICE(pci_dev);
8337     NvmeNamespace *ns;
8338     NvmeCtrl *pn = NVME(pcie_sriov_get_pf(pci_dev));
8339 
8340     if (pci_is_vf(pci_dev)) {
8341         /*
8342          * VFs derive settings from the parent. PF's lifespan exceeds
8343          * that of VF's.
8344          */
8345         memcpy(&n->params, &pn->params, sizeof(NvmeParams));
8346 
8347         /*
8348          * Set PF's serial value to a new string memory to prevent 'serial'
8349          * property object release of PF when a VF is removed from the system.
8350          */
8351         n->params.serial = g_strdup(pn->params.serial);
8352         n->subsys = pn->subsys;
8353     }
8354 
8355     if (!nvme_check_params(n, errp)) {
8356         return;
8357     }
8358 
8359     qbus_init(&n->bus, sizeof(NvmeBus), TYPE_NVME_BUS, dev, dev->id);
8360 
8361     if (nvme_init_subsys(n, errp)) {
8362         return;
8363     }
8364     nvme_init_state(n);
8365     if (!nvme_init_pci(n, pci_dev, errp)) {
8366         return;
8367     }
8368     nvme_init_ctrl(n, pci_dev);
8369 
8370     /* setup a namespace if the controller drive property was given */
8371     if (n->namespace.blkconf.blk) {
8372         ns = &n->namespace;
8373         ns->params.nsid = 1;
8374 
8375         if (nvme_ns_setup(ns, errp)) {
8376             return;
8377         }
8378 
8379         nvme_attach_ns(n, ns);
8380     }
8381 }
8382 
8383 static void nvme_exit(PCIDevice *pci_dev)
8384 {
8385     NvmeCtrl *n = NVME(pci_dev);
8386     NvmeNamespace *ns;
8387     int i;
8388 
8389     nvme_ctrl_reset(n, NVME_RESET_FUNCTION);
8390 
8391     if (n->subsys) {
8392         for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
8393             ns = nvme_ns(n, i);
8394             if (ns) {
8395                 ns->attached--;
8396             }
8397         }
8398 
8399         nvme_subsys_unregister_ctrl(n->subsys, n);
8400     }
8401 
8402     g_free(n->cq);
8403     g_free(n->sq);
8404     g_free(n->aer_reqs);
8405 
8406     if (n->params.cmb_size_mb) {
8407         g_free(n->cmb.buf);
8408     }
8409 
8410     if (n->pmr.dev) {
8411         host_memory_backend_set_mapped(n->pmr.dev, false);
8412     }
8413 
8414     if (!pci_is_vf(pci_dev) && n->params.sriov_max_vfs) {
8415         pcie_sriov_pf_exit(pci_dev);
8416     }
8417 
8418     msix_uninit(pci_dev, &n->bar0, &n->bar0);
8419     memory_region_del_subregion(&n->bar0, &n->iomem);
8420 }
8421 
8422 static Property nvme_props[] = {
8423     DEFINE_BLOCK_PROPERTIES(NvmeCtrl, namespace.blkconf),
8424     DEFINE_PROP_LINK("pmrdev", NvmeCtrl, pmr.dev, TYPE_MEMORY_BACKEND,
8425                      HostMemoryBackend *),
8426     DEFINE_PROP_LINK("subsys", NvmeCtrl, subsys, TYPE_NVME_SUBSYS,
8427                      NvmeSubsystem *),
8428     DEFINE_PROP_STRING("serial", NvmeCtrl, params.serial),
8429     DEFINE_PROP_UINT32("cmb_size_mb", NvmeCtrl, params.cmb_size_mb, 0),
8430     DEFINE_PROP_UINT32("num_queues", NvmeCtrl, params.num_queues, 0),
8431     DEFINE_PROP_UINT32("max_ioqpairs", NvmeCtrl, params.max_ioqpairs, 64),
8432     DEFINE_PROP_UINT16("msix_qsize", NvmeCtrl, params.msix_qsize, 65),
8433     DEFINE_PROP_UINT8("aerl", NvmeCtrl, params.aerl, 3),
8434     DEFINE_PROP_UINT32("aer_max_queued", NvmeCtrl, params.aer_max_queued, 64),
8435     DEFINE_PROP_UINT8("mdts", NvmeCtrl, params.mdts, 7),
8436     DEFINE_PROP_UINT8("vsl", NvmeCtrl, params.vsl, 7),
8437     DEFINE_PROP_BOOL("use-intel-id", NvmeCtrl, params.use_intel_id, false),
8438     DEFINE_PROP_BOOL("legacy-cmb", NvmeCtrl, params.legacy_cmb, false),
8439     DEFINE_PROP_BOOL("ioeventfd", NvmeCtrl, params.ioeventfd, false),
8440     DEFINE_PROP_UINT8("zoned.zasl", NvmeCtrl, params.zasl, 0),
8441     DEFINE_PROP_BOOL("zoned.auto_transition", NvmeCtrl,
8442                      params.auto_transition_zones, true),
8443     DEFINE_PROP_UINT8("sriov_max_vfs", NvmeCtrl, params.sriov_max_vfs, 0),
8444     DEFINE_PROP_UINT16("sriov_vq_flexible", NvmeCtrl,
8445                        params.sriov_vq_flexible, 0),
8446     DEFINE_PROP_UINT16("sriov_vi_flexible", NvmeCtrl,
8447                        params.sriov_vi_flexible, 0),
8448     DEFINE_PROP_UINT8("sriov_max_vi_per_vf", NvmeCtrl,
8449                       params.sriov_max_vi_per_vf, 0),
8450     DEFINE_PROP_UINT8("sriov_max_vq_per_vf", NvmeCtrl,
8451                       params.sriov_max_vq_per_vf, 0),
8452     DEFINE_PROP_BOOL("msix-exclusive-bar", NvmeCtrl, params.msix_exclusive_bar,
8453                      false),
8454     DEFINE_PROP_END_OF_LIST(),
8455 };
8456 
8457 static void nvme_get_smart_warning(Object *obj, Visitor *v, const char *name,
8458                                    void *opaque, Error **errp)
8459 {
8460     NvmeCtrl *n = NVME(obj);
8461     uint8_t value = n->smart_critical_warning;
8462 
8463     visit_type_uint8(v, name, &value, errp);
8464 }
8465 
8466 static void nvme_set_smart_warning(Object *obj, Visitor *v, const char *name,
8467                                    void *opaque, Error **errp)
8468 {
8469     NvmeCtrl *n = NVME(obj);
8470     uint8_t value, old_value, cap = 0, index, event;
8471 
8472     if (!visit_type_uint8(v, name, &value, errp)) {
8473         return;
8474     }
8475 
8476     cap = NVME_SMART_SPARE | NVME_SMART_TEMPERATURE | NVME_SMART_RELIABILITY
8477           | NVME_SMART_MEDIA_READ_ONLY | NVME_SMART_FAILED_VOLATILE_MEDIA;
8478     if (NVME_CAP_PMRS(ldq_le_p(&n->bar.cap))) {
8479         cap |= NVME_SMART_PMR_UNRELIABLE;
8480     }
8481 
8482     if ((value & cap) != value) {
8483         error_setg(errp, "unsupported smart critical warning bits: 0x%x",
8484                    value & ~cap);
8485         return;
8486     }
8487 
8488     old_value = n->smart_critical_warning;
8489     n->smart_critical_warning = value;
8490 
8491     /* only inject new bits of smart critical warning */
8492     for (index = 0; index < NVME_SMART_WARN_MAX; index++) {
8493         event = 1 << index;
8494         if (value & ~old_value & event)
8495             nvme_smart_event(n, event);
8496     }
8497 }
8498 
8499 static void nvme_pci_reset(DeviceState *qdev)
8500 {
8501     PCIDevice *pci_dev = PCI_DEVICE(qdev);
8502     NvmeCtrl *n = NVME(pci_dev);
8503 
8504     trace_pci_nvme_pci_reset();
8505     nvme_ctrl_reset(n, NVME_RESET_FUNCTION);
8506 }
8507 
8508 static void nvme_sriov_post_write_config(PCIDevice *dev, uint16_t old_num_vfs)
8509 {
8510     NvmeCtrl *n = NVME(dev);
8511     NvmeSecCtrlEntry *sctrl;
8512     int i;
8513 
8514     for (i = pcie_sriov_num_vfs(dev); i < old_num_vfs; i++) {
8515         sctrl = &n->sec_ctrl_list.sec[i];
8516         nvme_virt_set_state(n, le16_to_cpu(sctrl->scid), false);
8517     }
8518 }
8519 
8520 static void nvme_pci_write_config(PCIDevice *dev, uint32_t address,
8521                                   uint32_t val, int len)
8522 {
8523     uint16_t old_num_vfs = pcie_sriov_num_vfs(dev);
8524 
8525     pci_default_write_config(dev, address, val, len);
8526     pcie_cap_flr_write_config(dev, address, val, len);
8527     nvme_sriov_post_write_config(dev, old_num_vfs);
8528 }
8529 
8530 static const VMStateDescription nvme_vmstate = {
8531     .name = "nvme",
8532     .unmigratable = 1,
8533 };
8534 
8535 static void nvme_class_init(ObjectClass *oc, void *data)
8536 {
8537     DeviceClass *dc = DEVICE_CLASS(oc);
8538     PCIDeviceClass *pc = PCI_DEVICE_CLASS(oc);
8539 
8540     pc->realize = nvme_realize;
8541     pc->config_write = nvme_pci_write_config;
8542     pc->exit = nvme_exit;
8543     pc->class_id = PCI_CLASS_STORAGE_EXPRESS;
8544     pc->revision = 2;
8545 
8546     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
8547     dc->desc = "Non-Volatile Memory Express";
8548     device_class_set_props(dc, nvme_props);
8549     dc->vmsd = &nvme_vmstate;
8550     dc->reset = nvme_pci_reset;
8551 }
8552 
8553 static void nvme_instance_init(Object *obj)
8554 {
8555     NvmeCtrl *n = NVME(obj);
8556 
8557     device_add_bootindex_property(obj, &n->namespace.blkconf.bootindex,
8558                                   "bootindex", "/namespace@1,0",
8559                                   DEVICE(obj));
8560 
8561     object_property_add(obj, "smart_critical_warning", "uint8",
8562                         nvme_get_smart_warning,
8563                         nvme_set_smart_warning, NULL, NULL);
8564 }
8565 
8566 static const TypeInfo nvme_info = {
8567     .name          = TYPE_NVME,
8568     .parent        = TYPE_PCI_DEVICE,
8569     .instance_size = sizeof(NvmeCtrl),
8570     .instance_init = nvme_instance_init,
8571     .class_init    = nvme_class_init,
8572     .interfaces = (InterfaceInfo[]) {
8573         { INTERFACE_PCIE_DEVICE },
8574         { }
8575     },
8576 };
8577 
8578 static const TypeInfo nvme_bus_info = {
8579     .name = TYPE_NVME_BUS,
8580     .parent = TYPE_BUS,
8581     .instance_size = sizeof(NvmeBus),
8582 };
8583 
8584 static void nvme_register_types(void)
8585 {
8586     type_register_static(&nvme_info);
8587     type_register_static(&nvme_bus_info);
8588 }
8589 
8590 type_init(nvme_register_types)
8591