2 * QEMU NVM Express Controller
4 * Copyright (c) 2012, Intel Corporation
6 * Written by Keith Busch <keith.busch@intel.com>
8 * This code is licensed under the GNU GPL v2 or later.
12 * Reference Specs: http://www.nvmexpress.org, 1.4, 1.3, 1.2, 1.1, 1.0e
14 * https://nvmexpress.org/developers/nvme-specification/
17 * Notes on coding style
18 * ---------------------
19 * While QEMU coding style prefers lowercase hexadecimals in constants, the
20 * NVMe subsystem use thes format from the NVMe specifications in the comments
21 * (i.e. 'h' suffix instead of '0x' prefix).
25 * See docs/system/nvme.rst for extensive documentation.
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]> \
44 * -device nvme-ns,drive=<drive_id>,bus=<bus_name>,nsid=<nsid>,\
45 * zoned=<true|false[optional]>, \
46 * subsys=<subsys_id>,detached=<true|false[optional]>
48 * Note cmb_size_mb denotes size of CMB in MB. CMB is assumed to be at
49 * offset 0 in BAR2 and supports only WDS, RDS and SQS for now. By default, the
50 * device will use the "v1.4 CMB scheme" - use the `legacy-cmb` parameter to
51 * always enable the CMBLOC and CMBSZ registers (v1.3 behavior).
53 * Enabling pmr emulation can be achieved by pointing to memory-backend-file.
55 * -object memory-backend-file,id=<mem_id>,share=on,mem-path=<file_path>, \
56 * size=<size> .... -device nvme,...,pmrdev=<mem_id>
58 * The PMR will use BAR 4/5 exclusively.
60 * To place controller(s) and namespace(s) to a subsystem, then provide
61 * nvme-subsys device as above.
63 * nvme subsystem device parameters
64 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
66 * This parameter provides the `<nqn_id>` part of the string
67 * `nqn.2019-08.org.qemu:<nqn_id>` which will be reported in the SUBNQN field
68 * of subsystem controllers. Note that `<nqn_id>` should be unique per
69 * subsystem, but this is not enforced by QEMU. If not specified, it will
70 * default to the value of the `id` parameter (`<subsys_id>`).
72 * nvme device parameters
73 * ~~~~~~~~~~~~~~~~~~~~~~
75 * Specifying this parameter attaches the controller to the subsystem and
76 * the SUBNQN field in the controller will report the NQN of the subsystem
77 * device. This also enables multi controller capability represented in
78 * Identify Controller data structure in CMIC (Controller Multi-path I/O and
79 * Namespace Sharing Capabilities).
82 * The Asynchronous Event Request Limit (AERL). Indicates the maximum number
83 * of concurrently outstanding Asynchronous Event Request commands support
84 * by the controller. This is a 0's based value.
87 * This is the maximum number of events that the device will enqueue for
88 * completion when there are no outstanding AERs. When the maximum number of
89 * enqueued events are reached, subsequent events will be dropped.
92 * Indicates the maximum data transfer size for a command that transfers data
93 * between host-accessible memory and the controller. The value is specified
94 * as a power of two (2^n) and is in units of the minimum memory page size
95 * (CAP.MPSMIN). The default value is 7 (i.e. 512 KiB).
98 * Indicates the maximum data size limit for the Verify command. Like `mdts`,
99 * this value is specified as a power of two (2^n) and is in units of the
100 * minimum memory page size (CAP.MPSMIN). The default value is 7 (i.e. 512
104 * Indicates the maximum data transfer size for the Zone Append command. Like
105 * `mdts`, the value is specified as a power of two (2^n) and is in units of
106 * the minimum memory page size (CAP.MPSMIN). The default value is 0 (i.e.
107 * defaulting to the value of `mdts`).
109 * - `zoned.auto_transition`
110 * Indicates if zones in zone state implicitly opened can be automatically
111 * transitioned to zone state closed for resource management purposes.
115 * Indicates the maximum number of PCIe virtual functions supported
116 * by the controller. The default value is 0. Specifying a non-zero value
117 * enables reporting of both SR-IOV and ARI capabilities by the NVMe device.
118 * Virtual function controllers will not report SR-IOV capability.
120 * NOTE: Single Root I/O Virtualization support is experimental.
121 * All the related parameters may be subject to change.
123 * - `sriov_vq_flexible`
124 * Indicates the total number of flexible queue resources assignable to all
125 * the secondary controllers. Implicitly sets the number of primary
126 * controller's private resources to `(max_ioqpairs - sriov_vq_flexible)`.
128 * - `sriov_vi_flexible`
129 * Indicates the total number of flexible interrupt resources assignable to
130 * all the secondary controllers. Implicitly sets the number of primary
131 * controller's private resources to `(msix_qsize - sriov_vi_flexible)`.
133 * - `sriov_max_vi_per_vf`
134 * Indicates the maximum number of virtual interrupt resources assignable
135 * to a secondary controller. The default 0 resolves to
136 * `(sriov_vi_flexible / sriov_max_vfs)`.
138 * - `sriov_max_vq_per_vf`
139 * Indicates the maximum number of virtual queue resources assignable to
140 * a secondary controller. The default 0 resolves to
141 * `(sriov_vq_flexible / sriov_max_vfs)`.
143 * nvme namespace device parameters
144 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
146 * When the parent nvme device (as defined explicitly by the 'bus' parameter
147 * or implicitly by the most recently defined NvmeBus) is linked to an
148 * nvme-subsys device, the namespace will be attached to all controllers in
149 * the subsystem. If set to 'off' (the default), the namespace will remain a
150 * private namespace and may only be attached to a single controller at a
154 * This parameter is only valid together with the `subsys` parameter. If left
155 * at the default value (`false/off`), the namespace will be attached to all
156 * controllers in the NVMe subsystem at boot-up. If set to `true/on`, the
157 * namespace will be available in the subsystem but not attached to any
160 * Setting `zoned` to true selects Zoned Command Set at the namespace.
161 * In this case, the following namespace properties are available to configure
163 * zoned.zone_size=<zone size in bytes, default: 128MiB>
164 * The number may be followed by K, M, G as in kilo-, mega- or giga-.
166 * zoned.zone_capacity=<zone capacity in bytes, default: zone size>
167 * The value 0 (default) forces zone capacity to be the same as zone
168 * size. The value of this property may not exceed zone size.
170 * zoned.descr_ext_size=<zone descriptor extension size, default 0>
171 * This value needs to be specified in 64B units. If it is zero,
172 * namespace(s) will not support zone descriptor extensions.
174 * zoned.max_active=<Maximum Active Resources (zones), default: 0>
175 * The default value means there is no limit to the number of
176 * concurrently active zones.
178 * zoned.max_open=<Maximum Open Resources (zones), default: 0>
179 * The default value means there is no limit to the number of
180 * concurrently open zones.
182 * zoned.cross_read=<enable RAZB, default: false>
183 * Setting this property to true enables Read Across Zone Boundaries.
186 #include "qemu/osdep.h"
187 #include "qemu/cutils.h"
188 #include "qemu/error-report.h"
189 #include "qemu/log.h"
190 #include "qemu/units.h"
191 #include "qemu/range.h"
192 #include "qapi/error.h"
193 #include "qapi/visitor.h"
194 #include "sysemu/sysemu.h"
195 #include "sysemu/block-backend.h"
196 #include "sysemu/hostmem.h"
197 #include "hw/pci/msix.h"
198 #include "hw/pci/pcie_sriov.h"
199 #include "migration/vmstate.h"
205 #define NVME_MAX_IOQPAIRS 0xffff
206 #define NVME_DB_SIZE 4
207 #define NVME_SPEC_VER 0x00010400
208 #define NVME_CMB_BIR 2
209 #define NVME_PMR_BIR 4
210 #define NVME_TEMPERATURE 0x143
211 #define NVME_TEMPERATURE_WARNING 0x157
212 #define NVME_TEMPERATURE_CRITICAL 0x175
213 #define NVME_NUM_FW_SLOTS 1
214 #define NVME_DEFAULT_MAX_ZA_SIZE (128 * KiB)
215 #define NVME_MAX_VFS 127
216 #define NVME_VF_RES_GRANULARITY 1
217 #define NVME_VF_OFFSET 0x1
218 #define NVME_VF_STRIDE 1
220 #define NVME_GUEST_ERR(trace, fmt, ...) \
222 (trace_##trace)(__VA_ARGS__); \
223 qemu_log_mask(LOG_GUEST_ERROR, #trace \
224 " in %s: " fmt "\n", __func__, ## __VA_ARGS__); \
227 static const bool nvme_feature_support
[NVME_FID_MAX
] = {
228 [NVME_ARBITRATION
] = true,
229 [NVME_POWER_MANAGEMENT
] = true,
230 [NVME_TEMPERATURE_THRESHOLD
] = true,
231 [NVME_ERROR_RECOVERY
] = true,
232 [NVME_VOLATILE_WRITE_CACHE
] = true,
233 [NVME_NUMBER_OF_QUEUES
] = true,
234 [NVME_INTERRUPT_COALESCING
] = true,
235 [NVME_INTERRUPT_VECTOR_CONF
] = true,
236 [NVME_WRITE_ATOMICITY
] = true,
237 [NVME_ASYNCHRONOUS_EVENT_CONF
] = true,
238 [NVME_TIMESTAMP
] = true,
239 [NVME_HOST_BEHAVIOR_SUPPORT
] = true,
240 [NVME_COMMAND_SET_PROFILE
] = true,
243 static const uint32_t nvme_feature_cap
[NVME_FID_MAX
] = {
244 [NVME_TEMPERATURE_THRESHOLD
] = NVME_FEAT_CAP_CHANGE
,
245 [NVME_ERROR_RECOVERY
] = NVME_FEAT_CAP_CHANGE
| NVME_FEAT_CAP_NS
,
246 [NVME_VOLATILE_WRITE_CACHE
] = NVME_FEAT_CAP_CHANGE
,
247 [NVME_NUMBER_OF_QUEUES
] = NVME_FEAT_CAP_CHANGE
,
248 [NVME_ASYNCHRONOUS_EVENT_CONF
] = NVME_FEAT_CAP_CHANGE
,
249 [NVME_TIMESTAMP
] = NVME_FEAT_CAP_CHANGE
,
250 [NVME_HOST_BEHAVIOR_SUPPORT
] = NVME_FEAT_CAP_CHANGE
,
251 [NVME_COMMAND_SET_PROFILE
] = NVME_FEAT_CAP_CHANGE
,
254 static const uint32_t nvme_cse_acs
[256] = {
255 [NVME_ADM_CMD_DELETE_SQ
] = NVME_CMD_EFF_CSUPP
,
256 [NVME_ADM_CMD_CREATE_SQ
] = NVME_CMD_EFF_CSUPP
,
257 [NVME_ADM_CMD_GET_LOG_PAGE
] = NVME_CMD_EFF_CSUPP
,
258 [NVME_ADM_CMD_DELETE_CQ
] = NVME_CMD_EFF_CSUPP
,
259 [NVME_ADM_CMD_CREATE_CQ
] = NVME_CMD_EFF_CSUPP
,
260 [NVME_ADM_CMD_IDENTIFY
] = NVME_CMD_EFF_CSUPP
,
261 [NVME_ADM_CMD_ABORT
] = NVME_CMD_EFF_CSUPP
,
262 [NVME_ADM_CMD_SET_FEATURES
] = NVME_CMD_EFF_CSUPP
,
263 [NVME_ADM_CMD_GET_FEATURES
] = NVME_CMD_EFF_CSUPP
,
264 [NVME_ADM_CMD_ASYNC_EV_REQ
] = NVME_CMD_EFF_CSUPP
,
265 [NVME_ADM_CMD_NS_ATTACHMENT
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_NIC
,
266 [NVME_ADM_CMD_VIRT_MNGMT
] = NVME_CMD_EFF_CSUPP
,
267 [NVME_ADM_CMD_DBBUF_CONFIG
] = NVME_CMD_EFF_CSUPP
,
268 [NVME_ADM_CMD_FORMAT_NVM
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
271 static const uint32_t nvme_cse_iocs_none
[256];
273 static const uint32_t nvme_cse_iocs_nvm
[256] = {
274 [NVME_CMD_FLUSH
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
275 [NVME_CMD_WRITE_ZEROES
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
276 [NVME_CMD_WRITE
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
277 [NVME_CMD_READ
] = NVME_CMD_EFF_CSUPP
,
278 [NVME_CMD_DSM
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
279 [NVME_CMD_VERIFY
] = NVME_CMD_EFF_CSUPP
,
280 [NVME_CMD_COPY
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
281 [NVME_CMD_COMPARE
] = NVME_CMD_EFF_CSUPP
,
284 static const uint32_t nvme_cse_iocs_zoned
[256] = {
285 [NVME_CMD_FLUSH
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
286 [NVME_CMD_WRITE_ZEROES
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
287 [NVME_CMD_WRITE
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
288 [NVME_CMD_READ
] = NVME_CMD_EFF_CSUPP
,
289 [NVME_CMD_DSM
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
290 [NVME_CMD_VERIFY
] = NVME_CMD_EFF_CSUPP
,
291 [NVME_CMD_COPY
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
292 [NVME_CMD_COMPARE
] = NVME_CMD_EFF_CSUPP
,
293 [NVME_CMD_ZONE_APPEND
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
294 [NVME_CMD_ZONE_MGMT_SEND
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
295 [NVME_CMD_ZONE_MGMT_RECV
] = NVME_CMD_EFF_CSUPP
,
298 static void nvme_process_sq(void *opaque
);
299 static void nvme_ctrl_reset(NvmeCtrl
*n
, NvmeResetType rst
);
301 static uint16_t nvme_sqid(NvmeRequest
*req
)
303 return le16_to_cpu(req
->sq
->sqid
);
306 static void nvme_assign_zone_state(NvmeNamespace
*ns
, NvmeZone
*zone
,
309 if (QTAILQ_IN_USE(zone
, entry
)) {
310 switch (nvme_get_zone_state(zone
)) {
311 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
312 QTAILQ_REMOVE(&ns
->exp_open_zones
, zone
, entry
);
314 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
315 QTAILQ_REMOVE(&ns
->imp_open_zones
, zone
, entry
);
317 case NVME_ZONE_STATE_CLOSED
:
318 QTAILQ_REMOVE(&ns
->closed_zones
, zone
, entry
);
320 case NVME_ZONE_STATE_FULL
:
321 QTAILQ_REMOVE(&ns
->full_zones
, zone
, entry
);
327 nvme_set_zone_state(zone
, state
);
330 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
331 QTAILQ_INSERT_TAIL(&ns
->exp_open_zones
, zone
, entry
);
333 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
334 QTAILQ_INSERT_TAIL(&ns
->imp_open_zones
, zone
, entry
);
336 case NVME_ZONE_STATE_CLOSED
:
337 QTAILQ_INSERT_TAIL(&ns
->closed_zones
, zone
, entry
);
339 case NVME_ZONE_STATE_FULL
:
340 QTAILQ_INSERT_TAIL(&ns
->full_zones
, zone
, entry
);
341 case NVME_ZONE_STATE_READ_ONLY
:
348 static uint16_t nvme_zns_check_resources(NvmeNamespace
*ns
, uint32_t act
,
349 uint32_t opn
, uint32_t zrwa
)
351 if (ns
->params
.max_active_zones
!= 0 &&
352 ns
->nr_active_zones
+ act
> ns
->params
.max_active_zones
) {
353 trace_pci_nvme_err_insuff_active_res(ns
->params
.max_active_zones
);
354 return NVME_ZONE_TOO_MANY_ACTIVE
| NVME_DNR
;
357 if (ns
->params
.max_open_zones
!= 0 &&
358 ns
->nr_open_zones
+ opn
> ns
->params
.max_open_zones
) {
359 trace_pci_nvme_err_insuff_open_res(ns
->params
.max_open_zones
);
360 return NVME_ZONE_TOO_MANY_OPEN
| NVME_DNR
;
363 if (zrwa
> ns
->zns
.numzrwa
) {
364 return NVME_NOZRWA
| NVME_DNR
;
371 * Check if we can open a zone without exceeding open/active limits.
372 * AOR stands for "Active and Open Resources" (see TP 4053 section 2.5).
374 static uint16_t nvme_aor_check(NvmeNamespace
*ns
, uint32_t act
, uint32_t opn
)
376 return nvme_zns_check_resources(ns
, act
, opn
, 0);
379 static bool nvme_addr_is_cmb(NvmeCtrl
*n
, hwaddr addr
)
387 lo
= n
->params
.legacy_cmb
? n
->cmb
.mem
.addr
: n
->cmb
.cba
;
388 hi
= lo
+ int128_get64(n
->cmb
.mem
.size
);
390 return addr
>= lo
&& addr
< hi
;
393 static inline void *nvme_addr_to_cmb(NvmeCtrl
*n
, hwaddr addr
)
395 hwaddr base
= n
->params
.legacy_cmb
? n
->cmb
.mem
.addr
: n
->cmb
.cba
;
396 return &n
->cmb
.buf
[addr
- base
];
399 static bool nvme_addr_is_pmr(NvmeCtrl
*n
, hwaddr addr
)
407 hi
= n
->pmr
.cba
+ int128_get64(n
->pmr
.dev
->mr
.size
);
409 return addr
>= n
->pmr
.cba
&& addr
< hi
;
412 static inline void *nvme_addr_to_pmr(NvmeCtrl
*n
, hwaddr addr
)
414 return memory_region_get_ram_ptr(&n
->pmr
.dev
->mr
) + (addr
- n
->pmr
.cba
);
417 static inline bool nvme_addr_is_iomem(NvmeCtrl
*n
, hwaddr addr
)
422 * The purpose of this check is to guard against invalid "local" access to
423 * the iomem (i.e. controller registers). Thus, we check against the range
424 * covered by the 'bar0' MemoryRegion since that is currently composed of
425 * two subregions (the NVMe "MBAR" and the MSI-X table/pba). Note, however,
426 * that if the device model is ever changed to allow the CMB to be located
427 * in BAR0 as well, then this must be changed.
430 hi
= lo
+ int128_get64(n
->bar0
.size
);
432 return addr
>= lo
&& addr
< hi
;
435 static int nvme_addr_read(NvmeCtrl
*n
, hwaddr addr
, void *buf
, int size
)
437 hwaddr hi
= addr
+ size
- 1;
442 if (n
->bar
.cmbsz
&& nvme_addr_is_cmb(n
, addr
) && nvme_addr_is_cmb(n
, hi
)) {
443 memcpy(buf
, nvme_addr_to_cmb(n
, addr
), size
);
447 if (nvme_addr_is_pmr(n
, addr
) && nvme_addr_is_pmr(n
, hi
)) {
448 memcpy(buf
, nvme_addr_to_pmr(n
, addr
), size
);
452 return pci_dma_read(PCI_DEVICE(n
), addr
, buf
, size
);
455 static int nvme_addr_write(NvmeCtrl
*n
, hwaddr addr
, const void *buf
, int size
)
457 hwaddr hi
= addr
+ size
- 1;
462 if (n
->bar
.cmbsz
&& nvme_addr_is_cmb(n
, addr
) && nvme_addr_is_cmb(n
, hi
)) {
463 memcpy(nvme_addr_to_cmb(n
, addr
), buf
, size
);
467 if (nvme_addr_is_pmr(n
, addr
) && nvme_addr_is_pmr(n
, hi
)) {
468 memcpy(nvme_addr_to_pmr(n
, addr
), buf
, size
);
472 return pci_dma_write(PCI_DEVICE(n
), addr
, buf
, size
);
475 static bool nvme_nsid_valid(NvmeCtrl
*n
, uint32_t nsid
)
478 (nsid
== NVME_NSID_BROADCAST
|| nsid
<= NVME_MAX_NAMESPACES
);
481 static int nvme_check_sqid(NvmeCtrl
*n
, uint16_t sqid
)
483 return sqid
< n
->conf_ioqpairs
+ 1 && n
->sq
[sqid
] != NULL
? 0 : -1;
486 static int nvme_check_cqid(NvmeCtrl
*n
, uint16_t cqid
)
488 return cqid
< n
->conf_ioqpairs
+ 1 && n
->cq
[cqid
] != NULL
? 0 : -1;
491 static void nvme_inc_cq_tail(NvmeCQueue
*cq
)
494 if (cq
->tail
>= cq
->size
) {
496 cq
->phase
= !cq
->phase
;
500 static void nvme_inc_sq_head(NvmeSQueue
*sq
)
502 sq
->head
= (sq
->head
+ 1) % sq
->size
;
505 static uint8_t nvme_cq_full(NvmeCQueue
*cq
)
507 return (cq
->tail
+ 1) % cq
->size
== cq
->head
;
510 static uint8_t nvme_sq_empty(NvmeSQueue
*sq
)
512 return sq
->head
== sq
->tail
;
515 static void nvme_irq_check(NvmeCtrl
*n
)
517 PCIDevice
*pci
= PCI_DEVICE(n
);
518 uint32_t intms
= ldl_le_p(&n
->bar
.intms
);
520 if (msix_enabled(pci
)) {
523 if (~intms
& n
->irq_status
) {
526 pci_irq_deassert(pci
);
530 static void nvme_irq_assert(NvmeCtrl
*n
, NvmeCQueue
*cq
)
532 PCIDevice
*pci
= PCI_DEVICE(n
);
534 if (cq
->irq_enabled
) {
535 if (msix_enabled(pci
)) {
536 trace_pci_nvme_irq_msix(cq
->vector
);
537 msix_notify(pci
, cq
->vector
);
539 trace_pci_nvme_irq_pin();
540 assert(cq
->vector
< 32);
541 n
->irq_status
|= 1 << cq
->vector
;
545 trace_pci_nvme_irq_masked();
549 static void nvme_irq_deassert(NvmeCtrl
*n
, NvmeCQueue
*cq
)
551 if (cq
->irq_enabled
) {
552 if (msix_enabled(PCI_DEVICE(n
))) {
555 assert(cq
->vector
< 32);
556 if (!n
->cq_pending
) {
557 n
->irq_status
&= ~(1 << cq
->vector
);
564 static void nvme_req_clear(NvmeRequest
*req
)
569 memset(&req
->cqe
, 0x0, sizeof(req
->cqe
));
570 req
->status
= NVME_SUCCESS
;
573 static inline void nvme_sg_init(NvmeCtrl
*n
, NvmeSg
*sg
, bool dma
)
576 pci_dma_sglist_init(&sg
->qsg
, PCI_DEVICE(n
), 0);
577 sg
->flags
= NVME_SG_DMA
;
579 qemu_iovec_init(&sg
->iov
, 0);
582 sg
->flags
|= NVME_SG_ALLOC
;
585 static inline void nvme_sg_unmap(NvmeSg
*sg
)
587 if (!(sg
->flags
& NVME_SG_ALLOC
)) {
591 if (sg
->flags
& NVME_SG_DMA
) {
592 qemu_sglist_destroy(&sg
->qsg
);
594 qemu_iovec_destroy(&sg
->iov
);
597 memset(sg
, 0x0, sizeof(*sg
));
601 * When metadata is transfered as extended LBAs, the DPTR mapped into `sg`
602 * holds both data and metadata. This function splits the data and metadata
603 * into two separate QSG/IOVs.
605 static void nvme_sg_split(NvmeSg
*sg
, NvmeNamespace
*ns
, NvmeSg
*data
,
609 uint32_t trans_len
, count
= ns
->lbasz
;
611 bool dma
= sg
->flags
& NVME_SG_DMA
;
613 size_t sg_len
= dma
? sg
->qsg
.size
: sg
->iov
.size
;
616 assert(sg
->flags
& NVME_SG_ALLOC
);
619 sge_len
= dma
? sg
->qsg
.sg
[sg_idx
].len
: sg
->iov
.iov
[sg_idx
].iov_len
;
621 trans_len
= MIN(sg_len
, count
);
622 trans_len
= MIN(trans_len
, sge_len
- offset
);
626 qemu_sglist_add(&dst
->qsg
, sg
->qsg
.sg
[sg_idx
].base
+ offset
,
629 qemu_iovec_add(&dst
->iov
,
630 sg
->iov
.iov
[sg_idx
].iov_base
+ offset
,
640 dst
= (dst
== data
) ? mdata
: data
;
641 count
= (dst
== data
) ? ns
->lbasz
: ns
->lbaf
.ms
;
644 if (sge_len
== offset
) {
651 static uint16_t nvme_map_addr_cmb(NvmeCtrl
*n
, QEMUIOVector
*iov
, hwaddr addr
,
658 trace_pci_nvme_map_addr_cmb(addr
, len
);
660 if (!nvme_addr_is_cmb(n
, addr
) || !nvme_addr_is_cmb(n
, addr
+ len
- 1)) {
661 return NVME_DATA_TRAS_ERROR
;
664 qemu_iovec_add(iov
, nvme_addr_to_cmb(n
, addr
), len
);
669 static uint16_t nvme_map_addr_pmr(NvmeCtrl
*n
, QEMUIOVector
*iov
, hwaddr addr
,
676 if (!nvme_addr_is_pmr(n
, addr
) || !nvme_addr_is_pmr(n
, addr
+ len
- 1)) {
677 return NVME_DATA_TRAS_ERROR
;
680 qemu_iovec_add(iov
, nvme_addr_to_pmr(n
, addr
), len
);
685 static uint16_t nvme_map_addr(NvmeCtrl
*n
, NvmeSg
*sg
, hwaddr addr
, size_t len
)
687 bool cmb
= false, pmr
= false;
693 trace_pci_nvme_map_addr(addr
, len
);
695 if (nvme_addr_is_iomem(n
, addr
)) {
696 return NVME_DATA_TRAS_ERROR
;
699 if (nvme_addr_is_cmb(n
, addr
)) {
701 } else if (nvme_addr_is_pmr(n
, addr
)) {
706 if (sg
->flags
& NVME_SG_DMA
) {
707 return NVME_INVALID_USE_OF_CMB
| NVME_DNR
;
710 if (sg
->iov
.niov
+ 1 > IOV_MAX
) {
711 goto max_mappings_exceeded
;
715 return nvme_map_addr_cmb(n
, &sg
->iov
, addr
, len
);
717 return nvme_map_addr_pmr(n
, &sg
->iov
, addr
, len
);
721 if (!(sg
->flags
& NVME_SG_DMA
)) {
722 return NVME_INVALID_USE_OF_CMB
| NVME_DNR
;
725 if (sg
->qsg
.nsg
+ 1 > IOV_MAX
) {
726 goto max_mappings_exceeded
;
729 qemu_sglist_add(&sg
->qsg
, addr
, len
);
733 max_mappings_exceeded
:
734 NVME_GUEST_ERR(pci_nvme_ub_too_many_mappings
,
735 "number of mappings exceed 1024");
736 return NVME_INTERNAL_DEV_ERROR
| NVME_DNR
;
739 static inline bool nvme_addr_is_dma(NvmeCtrl
*n
, hwaddr addr
)
741 return !(nvme_addr_is_cmb(n
, addr
) || nvme_addr_is_pmr(n
, addr
));
744 static uint16_t nvme_map_prp(NvmeCtrl
*n
, NvmeSg
*sg
, uint64_t prp1
,
745 uint64_t prp2
, uint32_t len
)
747 hwaddr trans_len
= n
->page_size
- (prp1
% n
->page_size
);
748 trans_len
= MIN(len
, trans_len
);
749 int num_prps
= (len
>> n
->page_bits
) + 1;
753 trace_pci_nvme_map_prp(trans_len
, len
, prp1
, prp2
, num_prps
);
755 nvme_sg_init(n
, sg
, nvme_addr_is_dma(n
, prp1
));
757 status
= nvme_map_addr(n
, sg
, prp1
, trans_len
);
764 if (len
> n
->page_size
) {
765 uint64_t prp_list
[n
->max_prp_ents
];
766 uint32_t nents
, prp_trans
;
770 * The first PRP list entry, pointed to by PRP2 may contain offset.
771 * Hence, we need to calculate the number of entries in based on
774 nents
= (n
->page_size
- (prp2
& (n
->page_size
- 1))) >> 3;
775 prp_trans
= MIN(n
->max_prp_ents
, nents
) * sizeof(uint64_t);
776 ret
= nvme_addr_read(n
, prp2
, (void *)prp_list
, prp_trans
);
778 trace_pci_nvme_err_addr_read(prp2
);
779 status
= NVME_DATA_TRAS_ERROR
;
783 uint64_t prp_ent
= le64_to_cpu(prp_list
[i
]);
785 if (i
== nents
- 1 && len
> n
->page_size
) {
786 if (unlikely(prp_ent
& (n
->page_size
- 1))) {
787 trace_pci_nvme_err_invalid_prplist_ent(prp_ent
);
788 status
= NVME_INVALID_PRP_OFFSET
| NVME_DNR
;
793 nents
= (len
+ n
->page_size
- 1) >> n
->page_bits
;
794 nents
= MIN(nents
, n
->max_prp_ents
);
795 prp_trans
= nents
* sizeof(uint64_t);
796 ret
= nvme_addr_read(n
, prp_ent
, (void *)prp_list
,
799 trace_pci_nvme_err_addr_read(prp_ent
);
800 status
= NVME_DATA_TRAS_ERROR
;
803 prp_ent
= le64_to_cpu(prp_list
[i
]);
806 if (unlikely(prp_ent
& (n
->page_size
- 1))) {
807 trace_pci_nvme_err_invalid_prplist_ent(prp_ent
);
808 status
= NVME_INVALID_PRP_OFFSET
| NVME_DNR
;
812 trans_len
= MIN(len
, n
->page_size
);
813 status
= nvme_map_addr(n
, sg
, prp_ent
, trans_len
);
822 if (unlikely(prp2
& (n
->page_size
- 1))) {
823 trace_pci_nvme_err_invalid_prp2_align(prp2
);
824 status
= NVME_INVALID_PRP_OFFSET
| NVME_DNR
;
827 status
= nvme_map_addr(n
, sg
, prp2
, len
);
842 * Map 'nsgld' data descriptors from 'segment'. The function will subtract the
843 * number of bytes mapped in len.
845 static uint16_t nvme_map_sgl_data(NvmeCtrl
*n
, NvmeSg
*sg
,
846 NvmeSglDescriptor
*segment
, uint64_t nsgld
,
847 size_t *len
, NvmeCmd
*cmd
)
849 dma_addr_t addr
, trans_len
;
853 for (int i
= 0; i
< nsgld
; i
++) {
854 uint8_t type
= NVME_SGL_TYPE(segment
[i
].type
);
857 case NVME_SGL_DESCR_TYPE_DATA_BLOCK
:
859 case NVME_SGL_DESCR_TYPE_SEGMENT
:
860 case NVME_SGL_DESCR_TYPE_LAST_SEGMENT
:
861 return NVME_INVALID_NUM_SGL_DESCRS
| NVME_DNR
;
863 return NVME_SGL_DESCR_TYPE_INVALID
| NVME_DNR
;
866 dlen
= le32_to_cpu(segment
[i
].len
);
874 * All data has been mapped, but the SGL contains additional
875 * segments and/or descriptors. The controller might accept
876 * ignoring the rest of the SGL.
878 uint32_t sgls
= le32_to_cpu(n
->id_ctrl
.sgls
);
879 if (sgls
& NVME_CTRL_SGLS_EXCESS_LENGTH
) {
883 trace_pci_nvme_err_invalid_sgl_excess_length(dlen
);
884 return NVME_DATA_SGL_LEN_INVALID
| NVME_DNR
;
887 trans_len
= MIN(*len
, dlen
);
889 addr
= le64_to_cpu(segment
[i
].addr
);
891 if (UINT64_MAX
- addr
< dlen
) {
892 return NVME_DATA_SGL_LEN_INVALID
| NVME_DNR
;
895 status
= nvme_map_addr(n
, sg
, addr
, trans_len
);
906 static uint16_t nvme_map_sgl(NvmeCtrl
*n
, NvmeSg
*sg
, NvmeSglDescriptor sgl
,
907 size_t len
, NvmeCmd
*cmd
)
910 * Read the segment in chunks of 256 descriptors (one 4k page) to avoid
911 * dynamically allocating a potentially huge SGL. The spec allows the SGL
912 * to be larger (as in number of bytes required to describe the SGL
913 * descriptors and segment chain) than the command transfer size, so it is
914 * not bounded by MDTS.
916 const int SEG_CHUNK_SIZE
= 256;
918 NvmeSglDescriptor segment
[SEG_CHUNK_SIZE
], *sgld
, *last_sgld
;
926 addr
= le64_to_cpu(sgl
.addr
);
928 trace_pci_nvme_map_sgl(NVME_SGL_TYPE(sgl
.type
), len
);
930 nvme_sg_init(n
, sg
, nvme_addr_is_dma(n
, addr
));
933 * If the entire transfer can be described with a single data block it can
934 * be mapped directly.
936 if (NVME_SGL_TYPE(sgl
.type
) == NVME_SGL_DESCR_TYPE_DATA_BLOCK
) {
937 status
= nvme_map_sgl_data(n
, sg
, sgld
, 1, &len
, cmd
);
946 switch (NVME_SGL_TYPE(sgld
->type
)) {
947 case NVME_SGL_DESCR_TYPE_SEGMENT
:
948 case NVME_SGL_DESCR_TYPE_LAST_SEGMENT
:
951 return NVME_INVALID_SGL_SEG_DESCR
| NVME_DNR
;
954 seg_len
= le32_to_cpu(sgld
->len
);
956 /* check the length of the (Last) Segment descriptor */
957 if (!seg_len
|| seg_len
& 0xf) {
958 return NVME_INVALID_SGL_SEG_DESCR
| NVME_DNR
;
961 if (UINT64_MAX
- addr
< seg_len
) {
962 return NVME_DATA_SGL_LEN_INVALID
| NVME_DNR
;
965 nsgld
= seg_len
/ sizeof(NvmeSglDescriptor
);
967 while (nsgld
> SEG_CHUNK_SIZE
) {
968 if (nvme_addr_read(n
, addr
, segment
, sizeof(segment
))) {
969 trace_pci_nvme_err_addr_read(addr
);
970 status
= NVME_DATA_TRAS_ERROR
;
974 status
= nvme_map_sgl_data(n
, sg
, segment
, SEG_CHUNK_SIZE
,
980 nsgld
-= SEG_CHUNK_SIZE
;
981 addr
+= SEG_CHUNK_SIZE
* sizeof(NvmeSglDescriptor
);
984 ret
= nvme_addr_read(n
, addr
, segment
, nsgld
*
985 sizeof(NvmeSglDescriptor
));
987 trace_pci_nvme_err_addr_read(addr
);
988 status
= NVME_DATA_TRAS_ERROR
;
992 last_sgld
= &segment
[nsgld
- 1];
995 * If the segment ends with a Data Block, then we are done.
997 if (NVME_SGL_TYPE(last_sgld
->type
) == NVME_SGL_DESCR_TYPE_DATA_BLOCK
) {
998 status
= nvme_map_sgl_data(n
, sg
, segment
, nsgld
, &len
, cmd
);
1007 * If the last descriptor was not a Data Block, then the current
1008 * segment must not be a Last Segment.
1010 if (NVME_SGL_TYPE(sgld
->type
) == NVME_SGL_DESCR_TYPE_LAST_SEGMENT
) {
1011 status
= NVME_INVALID_SGL_SEG_DESCR
| NVME_DNR
;
1016 addr
= le64_to_cpu(sgld
->addr
);
1019 * Do not map the last descriptor; it will be a Segment or Last Segment
1020 * descriptor and is handled by the next iteration.
1022 status
= nvme_map_sgl_data(n
, sg
, segment
, nsgld
- 1, &len
, cmd
);
1029 /* if there is any residual left in len, the SGL was too short */
1031 status
= NVME_DATA_SGL_LEN_INVALID
| NVME_DNR
;
1035 return NVME_SUCCESS
;
1042 uint16_t nvme_map_dptr(NvmeCtrl
*n
, NvmeSg
*sg
, size_t len
,
1045 uint64_t prp1
, prp2
;
1047 switch (NVME_CMD_FLAGS_PSDT(cmd
->flags
)) {
1049 prp1
= le64_to_cpu(cmd
->dptr
.prp1
);
1050 prp2
= le64_to_cpu(cmd
->dptr
.prp2
);
1052 return nvme_map_prp(n
, sg
, prp1
, prp2
, len
);
1053 case NVME_PSDT_SGL_MPTR_CONTIGUOUS
:
1054 case NVME_PSDT_SGL_MPTR_SGL
:
1055 return nvme_map_sgl(n
, sg
, cmd
->dptr
.sgl
, len
, cmd
);
1057 return NVME_INVALID_FIELD
;
1061 static uint16_t nvme_map_mptr(NvmeCtrl
*n
, NvmeSg
*sg
, size_t len
,
1064 int psdt
= NVME_CMD_FLAGS_PSDT(cmd
->flags
);
1065 hwaddr mptr
= le64_to_cpu(cmd
->mptr
);
1068 if (psdt
== NVME_PSDT_SGL_MPTR_SGL
) {
1069 NvmeSglDescriptor sgl
;
1071 if (nvme_addr_read(n
, mptr
, &sgl
, sizeof(sgl
))) {
1072 return NVME_DATA_TRAS_ERROR
;
1075 status
= nvme_map_sgl(n
, sg
, sgl
, len
, cmd
);
1076 if (status
&& (status
& 0x7ff) == NVME_DATA_SGL_LEN_INVALID
) {
1077 status
= NVME_MD_SGL_LEN_INVALID
| NVME_DNR
;
1083 nvme_sg_init(n
, sg
, nvme_addr_is_dma(n
, mptr
));
1084 status
= nvme_map_addr(n
, sg
, mptr
, len
);
1092 static uint16_t nvme_map_data(NvmeCtrl
*n
, uint32_t nlb
, NvmeRequest
*req
)
1094 NvmeNamespace
*ns
= req
->ns
;
1095 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
1096 bool pi
= !!NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
);
1097 bool pract
= !!(le16_to_cpu(rw
->control
) & NVME_RW_PRINFO_PRACT
);
1098 size_t len
= nvme_l2b(ns
, nlb
);
1101 if (nvme_ns_ext(ns
) &&
1102 !(pi
&& pract
&& ns
->lbaf
.ms
== nvme_pi_tuple_size(ns
))) {
1105 len
+= nvme_m2b(ns
, nlb
);
1107 status
= nvme_map_dptr(n
, &sg
, len
, &req
->cmd
);
1112 nvme_sg_init(n
, &req
->sg
, sg
.flags
& NVME_SG_DMA
);
1113 nvme_sg_split(&sg
, ns
, &req
->sg
, NULL
);
1116 return NVME_SUCCESS
;
1119 return nvme_map_dptr(n
, &req
->sg
, len
, &req
->cmd
);
1122 static uint16_t nvme_map_mdata(NvmeCtrl
*n
, uint32_t nlb
, NvmeRequest
*req
)
1124 NvmeNamespace
*ns
= req
->ns
;
1125 size_t len
= nvme_m2b(ns
, nlb
);
1128 if (nvme_ns_ext(ns
)) {
1131 len
+= nvme_l2b(ns
, nlb
);
1133 status
= nvme_map_dptr(n
, &sg
, len
, &req
->cmd
);
1138 nvme_sg_init(n
, &req
->sg
, sg
.flags
& NVME_SG_DMA
);
1139 nvme_sg_split(&sg
, ns
, NULL
, &req
->sg
);
1142 return NVME_SUCCESS
;
1145 return nvme_map_mptr(n
, &req
->sg
, len
, &req
->cmd
);
1148 static uint16_t nvme_tx_interleaved(NvmeCtrl
*n
, NvmeSg
*sg
, uint8_t *ptr
,
1149 uint32_t len
, uint32_t bytes
,
1150 int32_t skip_bytes
, int64_t offset
,
1151 NvmeTxDirection dir
)
1154 uint32_t trans_len
, count
= bytes
;
1155 bool dma
= sg
->flags
& NVME_SG_DMA
;
1160 assert(sg
->flags
& NVME_SG_ALLOC
);
1163 sge_len
= dma
? sg
->qsg
.sg
[sg_idx
].len
: sg
->iov
.iov
[sg_idx
].iov_len
;
1165 if (sge_len
- offset
< 0) {
1171 if (sge_len
== offset
) {
1177 trans_len
= MIN(len
, count
);
1178 trans_len
= MIN(trans_len
, sge_len
- offset
);
1181 addr
= sg
->qsg
.sg
[sg_idx
].base
+ offset
;
1183 addr
= (hwaddr
)(uintptr_t)sg
->iov
.iov
[sg_idx
].iov_base
+ offset
;
1186 if (dir
== NVME_TX_DIRECTION_TO_DEVICE
) {
1187 ret
= nvme_addr_read(n
, addr
, ptr
, trans_len
);
1189 ret
= nvme_addr_write(n
, addr
, ptr
, trans_len
);
1193 return NVME_DATA_TRAS_ERROR
;
1199 offset
+= trans_len
;
1203 offset
+= skip_bytes
;
1207 return NVME_SUCCESS
;
1210 static uint16_t nvme_tx(NvmeCtrl
*n
, NvmeSg
*sg
, void *ptr
, uint32_t len
,
1211 NvmeTxDirection dir
)
1213 assert(sg
->flags
& NVME_SG_ALLOC
);
1215 if (sg
->flags
& NVME_SG_DMA
) {
1216 const MemTxAttrs attrs
= MEMTXATTRS_UNSPECIFIED
;
1217 dma_addr_t residual
;
1219 if (dir
== NVME_TX_DIRECTION_TO_DEVICE
) {
1220 dma_buf_write(ptr
, len
, &residual
, &sg
->qsg
, attrs
);
1222 dma_buf_read(ptr
, len
, &residual
, &sg
->qsg
, attrs
);
1225 if (unlikely(residual
)) {
1226 trace_pci_nvme_err_invalid_dma();
1227 return NVME_INVALID_FIELD
| NVME_DNR
;
1232 if (dir
== NVME_TX_DIRECTION_TO_DEVICE
) {
1233 bytes
= qemu_iovec_to_buf(&sg
->iov
, 0, ptr
, len
);
1235 bytes
= qemu_iovec_from_buf(&sg
->iov
, 0, ptr
, len
);
1238 if (unlikely(bytes
!= len
)) {
1239 trace_pci_nvme_err_invalid_dma();
1240 return NVME_INVALID_FIELD
| NVME_DNR
;
1244 return NVME_SUCCESS
;
1247 static inline uint16_t nvme_c2h(NvmeCtrl
*n
, void *ptr
, uint32_t len
,
1252 status
= nvme_map_dptr(n
, &req
->sg
, len
, &req
->cmd
);
1257 return nvme_tx(n
, &req
->sg
, ptr
, len
, NVME_TX_DIRECTION_FROM_DEVICE
);
1260 static inline uint16_t nvme_h2c(NvmeCtrl
*n
, void *ptr
, uint32_t len
,
1265 status
= nvme_map_dptr(n
, &req
->sg
, len
, &req
->cmd
);
1270 return nvme_tx(n
, &req
->sg
, ptr
, len
, NVME_TX_DIRECTION_TO_DEVICE
);
1273 uint16_t nvme_bounce_data(NvmeCtrl
*n
, void *ptr
, uint32_t len
,
1274 NvmeTxDirection dir
, NvmeRequest
*req
)
1276 NvmeNamespace
*ns
= req
->ns
;
1277 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
1278 bool pi
= !!NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
);
1279 bool pract
= !!(le16_to_cpu(rw
->control
) & NVME_RW_PRINFO_PRACT
);
1281 if (nvme_ns_ext(ns
) &&
1282 !(pi
&& pract
&& ns
->lbaf
.ms
== nvme_pi_tuple_size(ns
))) {
1283 return nvme_tx_interleaved(n
, &req
->sg
, ptr
, len
, ns
->lbasz
,
1284 ns
->lbaf
.ms
, 0, dir
);
1287 return nvme_tx(n
, &req
->sg
, ptr
, len
, dir
);
1290 uint16_t nvme_bounce_mdata(NvmeCtrl
*n
, void *ptr
, uint32_t len
,
1291 NvmeTxDirection dir
, NvmeRequest
*req
)
1293 NvmeNamespace
*ns
= req
->ns
;
1296 if (nvme_ns_ext(ns
)) {
1297 return nvme_tx_interleaved(n
, &req
->sg
, ptr
, len
, ns
->lbaf
.ms
,
1298 ns
->lbasz
, ns
->lbasz
, dir
);
1301 nvme_sg_unmap(&req
->sg
);
1303 status
= nvme_map_mptr(n
, &req
->sg
, len
, &req
->cmd
);
1308 return nvme_tx(n
, &req
->sg
, ptr
, len
, dir
);
1311 static inline void nvme_blk_read(BlockBackend
*blk
, int64_t offset
,
1312 BlockCompletionFunc
*cb
, NvmeRequest
*req
)
1314 assert(req
->sg
.flags
& NVME_SG_ALLOC
);
1316 if (req
->sg
.flags
& NVME_SG_DMA
) {
1317 req
->aiocb
= dma_blk_read(blk
, &req
->sg
.qsg
, offset
, BDRV_SECTOR_SIZE
,
1320 req
->aiocb
= blk_aio_preadv(blk
, offset
, &req
->sg
.iov
, 0, cb
, req
);
1324 static inline void nvme_blk_write(BlockBackend
*blk
, int64_t offset
,
1325 BlockCompletionFunc
*cb
, NvmeRequest
*req
)
1327 assert(req
->sg
.flags
& NVME_SG_ALLOC
);
1329 if (req
->sg
.flags
& NVME_SG_DMA
) {
1330 req
->aiocb
= dma_blk_write(blk
, &req
->sg
.qsg
, offset
, BDRV_SECTOR_SIZE
,
1333 req
->aiocb
= blk_aio_pwritev(blk
, offset
, &req
->sg
.iov
, 0, cb
, req
);
1337 static void nvme_update_cq_eventidx(const NvmeCQueue
*cq
)
1339 uint32_t v
= cpu_to_le32(cq
->head
);
1341 trace_pci_nvme_update_cq_eventidx(cq
->cqid
, cq
->head
);
1343 pci_dma_write(PCI_DEVICE(cq
->ctrl
), cq
->ei_addr
, &v
, sizeof(v
));
1346 static void nvme_update_cq_head(NvmeCQueue
*cq
)
1350 pci_dma_read(PCI_DEVICE(cq
->ctrl
), cq
->db_addr
, &v
, sizeof(v
));
1352 cq
->head
= le32_to_cpu(v
);
1354 trace_pci_nvme_update_cq_head(cq
->cqid
, cq
->head
);
1357 static void nvme_post_cqes(void *opaque
)
1359 NvmeCQueue
*cq
= opaque
;
1360 NvmeCtrl
*n
= cq
->ctrl
;
1361 NvmeRequest
*req
, *next
;
1362 bool pending
= cq
->head
!= cq
->tail
;
1365 QTAILQ_FOREACH_SAFE(req
, &cq
->req_list
, entry
, next
) {
1369 if (n
->dbbuf_enabled
) {
1370 nvme_update_cq_eventidx(cq
);
1371 nvme_update_cq_head(cq
);
1374 if (nvme_cq_full(cq
)) {
1379 req
->cqe
.status
= cpu_to_le16((req
->status
<< 1) | cq
->phase
);
1380 req
->cqe
.sq_id
= cpu_to_le16(sq
->sqid
);
1381 req
->cqe
.sq_head
= cpu_to_le16(sq
->head
);
1382 addr
= cq
->dma_addr
+ cq
->tail
* n
->cqe_size
;
1383 ret
= pci_dma_write(PCI_DEVICE(n
), addr
, (void *)&req
->cqe
,
1386 trace_pci_nvme_err_addr_write(addr
);
1387 trace_pci_nvme_err_cfs();
1388 stl_le_p(&n
->bar
.csts
, NVME_CSTS_FAILED
);
1391 QTAILQ_REMOVE(&cq
->req_list
, req
, entry
);
1392 nvme_inc_cq_tail(cq
);
1393 nvme_sg_unmap(&req
->sg
);
1394 QTAILQ_INSERT_TAIL(&sq
->req_list
, req
, entry
);
1396 if (cq
->tail
!= cq
->head
) {
1397 if (cq
->irq_enabled
&& !pending
) {
1401 nvme_irq_assert(n
, cq
);
1405 static void nvme_enqueue_req_completion(NvmeCQueue
*cq
, NvmeRequest
*req
)
1407 assert(cq
->cqid
== req
->sq
->cqid
);
1408 trace_pci_nvme_enqueue_req_completion(nvme_cid(req
), cq
->cqid
,
1409 le32_to_cpu(req
->cqe
.result
),
1410 le32_to_cpu(req
->cqe
.dw1
),
1414 trace_pci_nvme_err_req_status(nvme_cid(req
), nvme_nsid(req
->ns
),
1415 req
->status
, req
->cmd
.opcode
);
1418 QTAILQ_REMOVE(&req
->sq
->out_req_list
, req
, entry
);
1419 QTAILQ_INSERT_TAIL(&cq
->req_list
, req
, entry
);
1421 qemu_bh_schedule(cq
->bh
);
1424 static void nvme_process_aers(void *opaque
)
1426 NvmeCtrl
*n
= opaque
;
1427 NvmeAsyncEvent
*event
, *next
;
1429 trace_pci_nvme_process_aers(n
->aer_queued
);
1431 QTAILQ_FOREACH_SAFE(event
, &n
->aer_queue
, entry
, next
) {
1433 NvmeAerResult
*result
;
1435 /* can't post cqe if there is nothing to complete */
1436 if (!n
->outstanding_aers
) {
1437 trace_pci_nvme_no_outstanding_aers();
1441 /* ignore if masked (cqe posted, but event not cleared) */
1442 if (n
->aer_mask
& (1 << event
->result
.event_type
)) {
1443 trace_pci_nvme_aer_masked(event
->result
.event_type
, n
->aer_mask
);
1447 QTAILQ_REMOVE(&n
->aer_queue
, event
, entry
);
1450 n
->aer_mask
|= 1 << event
->result
.event_type
;
1451 n
->outstanding_aers
--;
1453 req
= n
->aer_reqs
[n
->outstanding_aers
];
1455 result
= (NvmeAerResult
*) &req
->cqe
.result
;
1456 result
->event_type
= event
->result
.event_type
;
1457 result
->event_info
= event
->result
.event_info
;
1458 result
->log_page
= event
->result
.log_page
;
1461 trace_pci_nvme_aer_post_cqe(result
->event_type
, result
->event_info
,
1464 nvme_enqueue_req_completion(&n
->admin_cq
, req
);
1468 static void nvme_enqueue_event(NvmeCtrl
*n
, uint8_t event_type
,
1469 uint8_t event_info
, uint8_t log_page
)
1471 NvmeAsyncEvent
*event
;
1473 trace_pci_nvme_enqueue_event(event_type
, event_info
, log_page
);
1475 if (n
->aer_queued
== n
->params
.aer_max_queued
) {
1476 trace_pci_nvme_enqueue_event_noqueue(n
->aer_queued
);
1480 event
= g_new(NvmeAsyncEvent
, 1);
1481 event
->result
= (NvmeAerResult
) {
1482 .event_type
= event_type
,
1483 .event_info
= event_info
,
1484 .log_page
= log_page
,
1487 QTAILQ_INSERT_TAIL(&n
->aer_queue
, event
, entry
);
1490 nvme_process_aers(n
);
1493 static void nvme_smart_event(NvmeCtrl
*n
, uint8_t event
)
1497 /* Ref SPEC <Asynchronous Event Information 0x2013 SMART / Health Status> */
1498 if (!(NVME_AEC_SMART(n
->features
.async_config
) & event
)) {
1503 case NVME_SMART_SPARE
:
1504 aer_info
= NVME_AER_INFO_SMART_SPARE_THRESH
;
1506 case NVME_SMART_TEMPERATURE
:
1507 aer_info
= NVME_AER_INFO_SMART_TEMP_THRESH
;
1509 case NVME_SMART_RELIABILITY
:
1510 case NVME_SMART_MEDIA_READ_ONLY
:
1511 case NVME_SMART_FAILED_VOLATILE_MEDIA
:
1512 case NVME_SMART_PMR_UNRELIABLE
:
1513 aer_info
= NVME_AER_INFO_SMART_RELIABILITY
;
1519 nvme_enqueue_event(n
, NVME_AER_TYPE_SMART
, aer_info
, NVME_LOG_SMART_INFO
);
1522 static void nvme_clear_events(NvmeCtrl
*n
, uint8_t event_type
)
1524 n
->aer_mask
&= ~(1 << event_type
);
1525 if (!QTAILQ_EMPTY(&n
->aer_queue
)) {
1526 nvme_process_aers(n
);
1530 static inline uint16_t nvme_check_mdts(NvmeCtrl
*n
, size_t len
)
1532 uint8_t mdts
= n
->params
.mdts
;
1534 if (mdts
&& len
> n
->page_size
<< mdts
) {
1535 trace_pci_nvme_err_mdts(len
);
1536 return NVME_INVALID_FIELD
| NVME_DNR
;
1539 return NVME_SUCCESS
;
1542 static inline uint16_t nvme_check_bounds(NvmeNamespace
*ns
, uint64_t slba
,
1545 uint64_t nsze
= le64_to_cpu(ns
->id_ns
.nsze
);
1547 if (unlikely(UINT64_MAX
- slba
< nlb
|| slba
+ nlb
> nsze
)) {
1548 trace_pci_nvme_err_invalid_lba_range(slba
, nlb
, nsze
);
1549 return NVME_LBA_RANGE
| NVME_DNR
;
1552 return NVME_SUCCESS
;
1555 static int nvme_block_status_all(NvmeNamespace
*ns
, uint64_t slba
,
1556 uint32_t nlb
, int flags
)
1558 BlockDriverState
*bs
= blk_bs(ns
->blkconf
.blk
);
1560 int64_t pnum
= 0, bytes
= nvme_l2b(ns
, nlb
);
1561 int64_t offset
= nvme_l2b(ns
, slba
);
1565 * `pnum` holds the number of bytes after offset that shares the same
1566 * allocation status as the byte at offset. If `pnum` is different from
1567 * `bytes`, we should check the allocation status of the next range and
1568 * continue this until all bytes have been checked.
1573 ret
= bdrv_block_status(bs
, offset
, bytes
, &pnum
, NULL
, NULL
);
1579 trace_pci_nvme_block_status(offset
, bytes
, pnum
, ret
,
1580 !!(ret
& BDRV_BLOCK_ZERO
));
1582 if (!(ret
& flags
)) {
1587 } while (pnum
!= bytes
);
1592 static uint16_t nvme_check_dulbe(NvmeNamespace
*ns
, uint64_t slba
,
1598 ret
= nvme_block_status_all(ns
, slba
, nlb
, BDRV_BLOCK_DATA
);
1601 error_setg_errno(&err
, -ret
, "unable to get block status");
1602 error_report_err(err
);
1604 return NVME_INTERNAL_DEV_ERROR
;
1610 return NVME_SUCCESS
;
1613 static void nvme_aio_err(NvmeRequest
*req
, int ret
)
1615 uint16_t status
= NVME_SUCCESS
;
1616 Error
*local_err
= NULL
;
1618 switch (req
->cmd
.opcode
) {
1620 status
= NVME_UNRECOVERED_READ
;
1622 case NVME_CMD_FLUSH
:
1623 case NVME_CMD_WRITE
:
1624 case NVME_CMD_WRITE_ZEROES
:
1625 case NVME_CMD_ZONE_APPEND
:
1626 status
= NVME_WRITE_FAULT
;
1629 status
= NVME_INTERNAL_DEV_ERROR
;
1633 trace_pci_nvme_err_aio(nvme_cid(req
), strerror(-ret
), status
);
1635 error_setg_errno(&local_err
, -ret
, "aio failed");
1636 error_report_err(local_err
);
1639 * Set the command status code to the first encountered error but allow a
1640 * subsequent Internal Device Error to trump it.
1642 if (req
->status
&& status
!= NVME_INTERNAL_DEV_ERROR
) {
1646 req
->status
= status
;
1649 static inline uint32_t nvme_zone_idx(NvmeNamespace
*ns
, uint64_t slba
)
1651 return ns
->zone_size_log2
> 0 ? slba
>> ns
->zone_size_log2
:
1652 slba
/ ns
->zone_size
;
1655 static inline NvmeZone
*nvme_get_zone_by_slba(NvmeNamespace
*ns
, uint64_t slba
)
1657 uint32_t zone_idx
= nvme_zone_idx(ns
, slba
);
1659 if (zone_idx
>= ns
->num_zones
) {
1663 return &ns
->zone_array
[zone_idx
];
1666 static uint16_t nvme_check_zone_state_for_write(NvmeZone
*zone
)
1668 uint64_t zslba
= zone
->d
.zslba
;
1670 switch (nvme_get_zone_state(zone
)) {
1671 case NVME_ZONE_STATE_EMPTY
:
1672 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
1673 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
1674 case NVME_ZONE_STATE_CLOSED
:
1675 return NVME_SUCCESS
;
1676 case NVME_ZONE_STATE_FULL
:
1677 trace_pci_nvme_err_zone_is_full(zslba
);
1678 return NVME_ZONE_FULL
;
1679 case NVME_ZONE_STATE_OFFLINE
:
1680 trace_pci_nvme_err_zone_is_offline(zslba
);
1681 return NVME_ZONE_OFFLINE
;
1682 case NVME_ZONE_STATE_READ_ONLY
:
1683 trace_pci_nvme_err_zone_is_read_only(zslba
);
1684 return NVME_ZONE_READ_ONLY
;
1689 return NVME_INTERNAL_DEV_ERROR
;
1692 static uint16_t nvme_check_zone_write(NvmeNamespace
*ns
, NvmeZone
*zone
,
1693 uint64_t slba
, uint32_t nlb
)
1695 uint64_t zcap
= nvme_zone_wr_boundary(zone
);
1698 status
= nvme_check_zone_state_for_write(zone
);
1703 if (zone
->d
.za
& NVME_ZA_ZRWA_VALID
) {
1704 uint64_t ezrwa
= zone
->w_ptr
+ 2 * ns
->zns
.zrwas
;
1706 if (slba
< zone
->w_ptr
|| slba
+ nlb
> ezrwa
) {
1707 trace_pci_nvme_err_zone_invalid_write(slba
, zone
->w_ptr
);
1708 return NVME_ZONE_INVALID_WRITE
;
1711 if (unlikely(slba
!= zone
->w_ptr
)) {
1712 trace_pci_nvme_err_write_not_at_wp(slba
, zone
->d
.zslba
,
1714 return NVME_ZONE_INVALID_WRITE
;
1718 if (unlikely((slba
+ nlb
) > zcap
)) {
1719 trace_pci_nvme_err_zone_boundary(slba
, nlb
, zcap
);
1720 return NVME_ZONE_BOUNDARY_ERROR
;
1723 return NVME_SUCCESS
;
1726 static uint16_t nvme_check_zone_state_for_read(NvmeZone
*zone
)
1728 switch (nvme_get_zone_state(zone
)) {
1729 case NVME_ZONE_STATE_EMPTY
:
1730 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
1731 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
1732 case NVME_ZONE_STATE_FULL
:
1733 case NVME_ZONE_STATE_CLOSED
:
1734 case NVME_ZONE_STATE_READ_ONLY
:
1735 return NVME_SUCCESS
;
1736 case NVME_ZONE_STATE_OFFLINE
:
1737 trace_pci_nvme_err_zone_is_offline(zone
->d
.zslba
);
1738 return NVME_ZONE_OFFLINE
;
1743 return NVME_INTERNAL_DEV_ERROR
;
1746 static uint16_t nvme_check_zone_read(NvmeNamespace
*ns
, uint64_t slba
,
1750 uint64_t bndry
, end
;
1753 zone
= nvme_get_zone_by_slba(ns
, slba
);
1756 bndry
= nvme_zone_rd_boundary(ns
, zone
);
1759 status
= nvme_check_zone_state_for_read(zone
);
1762 } else if (unlikely(end
> bndry
)) {
1763 if (!ns
->params
.cross_zone_read
) {
1764 status
= NVME_ZONE_BOUNDARY_ERROR
;
1767 * Read across zone boundary - check that all subsequent
1768 * zones that are being read have an appropriate state.
1772 status
= nvme_check_zone_state_for_read(zone
);
1776 } while (end
> nvme_zone_rd_boundary(ns
, zone
));
1783 static uint16_t nvme_zrm_finish(NvmeNamespace
*ns
, NvmeZone
*zone
)
1785 switch (nvme_get_zone_state(zone
)) {
1786 case NVME_ZONE_STATE_FULL
:
1787 return NVME_SUCCESS
;
1789 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
1790 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
1791 nvme_aor_dec_open(ns
);
1793 case NVME_ZONE_STATE_CLOSED
:
1794 nvme_aor_dec_active(ns
);
1796 if (zone
->d
.za
& NVME_ZA_ZRWA_VALID
) {
1797 zone
->d
.za
&= ~NVME_ZA_ZRWA_VALID
;
1798 if (ns
->params
.numzrwa
) {
1804 case NVME_ZONE_STATE_EMPTY
:
1805 nvme_assign_zone_state(ns
, zone
, NVME_ZONE_STATE_FULL
);
1806 return NVME_SUCCESS
;
1809 return NVME_ZONE_INVAL_TRANSITION
;
1813 static uint16_t nvme_zrm_close(NvmeNamespace
*ns
, NvmeZone
*zone
)
1815 switch (nvme_get_zone_state(zone
)) {
1816 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
1817 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
1818 nvme_aor_dec_open(ns
);
1819 nvme_assign_zone_state(ns
, zone
, NVME_ZONE_STATE_CLOSED
);
1821 case NVME_ZONE_STATE_CLOSED
:
1822 return NVME_SUCCESS
;
1825 return NVME_ZONE_INVAL_TRANSITION
;
1829 static uint16_t nvme_zrm_reset(NvmeNamespace
*ns
, NvmeZone
*zone
)
1831 switch (nvme_get_zone_state(zone
)) {
1832 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
1833 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
1834 nvme_aor_dec_open(ns
);
1836 case NVME_ZONE_STATE_CLOSED
:
1837 nvme_aor_dec_active(ns
);
1839 if (zone
->d
.za
& NVME_ZA_ZRWA_VALID
) {
1840 if (ns
->params
.numzrwa
) {
1846 case NVME_ZONE_STATE_FULL
:
1847 zone
->w_ptr
= zone
->d
.zslba
;
1848 zone
->d
.wp
= zone
->w_ptr
;
1849 nvme_assign_zone_state(ns
, zone
, NVME_ZONE_STATE_EMPTY
);
1851 case NVME_ZONE_STATE_EMPTY
:
1852 return NVME_SUCCESS
;
1855 return NVME_ZONE_INVAL_TRANSITION
;
1859 static void nvme_zrm_auto_transition_zone(NvmeNamespace
*ns
)
1863 if (ns
->params
.max_open_zones
&&
1864 ns
->nr_open_zones
== ns
->params
.max_open_zones
) {
1865 zone
= QTAILQ_FIRST(&ns
->imp_open_zones
);
1868 * Automatically close this implicitly open zone.
1870 QTAILQ_REMOVE(&ns
->imp_open_zones
, zone
, entry
);
1871 nvme_zrm_close(ns
, zone
);
1877 NVME_ZRM_AUTO
= 1 << 0,
1878 NVME_ZRM_ZRWA
= 1 << 1,
1881 static uint16_t nvme_zrm_open_flags(NvmeCtrl
*n
, NvmeNamespace
*ns
,
1882 NvmeZone
*zone
, int flags
)
1887 switch (nvme_get_zone_state(zone
)) {
1888 case NVME_ZONE_STATE_EMPTY
:
1893 case NVME_ZONE_STATE_CLOSED
:
1894 if (n
->params
.auto_transition_zones
) {
1895 nvme_zrm_auto_transition_zone(ns
);
1897 status
= nvme_zns_check_resources(ns
, act
, 1,
1898 (flags
& NVME_ZRM_ZRWA
) ? 1 : 0);
1904 nvme_aor_inc_active(ns
);
1907 nvme_aor_inc_open(ns
);
1909 if (flags
& NVME_ZRM_AUTO
) {
1910 nvme_assign_zone_state(ns
, zone
, NVME_ZONE_STATE_IMPLICITLY_OPEN
);
1911 return NVME_SUCCESS
;
1916 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
1917 if (flags
& NVME_ZRM_AUTO
) {
1918 return NVME_SUCCESS
;
1921 nvme_assign_zone_state(ns
, zone
, NVME_ZONE_STATE_EXPLICITLY_OPEN
);
1925 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
1926 if (flags
& NVME_ZRM_ZRWA
) {
1929 zone
->d
.za
|= NVME_ZA_ZRWA_VALID
;
1932 return NVME_SUCCESS
;
1935 return NVME_ZONE_INVAL_TRANSITION
;
1939 static inline uint16_t nvme_zrm_auto(NvmeCtrl
*n
, NvmeNamespace
*ns
,
1942 return nvme_zrm_open_flags(n
, ns
, zone
, NVME_ZRM_AUTO
);
1945 static void nvme_advance_zone_wp(NvmeNamespace
*ns
, NvmeZone
*zone
,
1950 if (zone
->d
.wp
== nvme_zone_wr_boundary(zone
)) {
1951 nvme_zrm_finish(ns
, zone
);
1955 static void nvme_zoned_zrwa_implicit_flush(NvmeNamespace
*ns
, NvmeZone
*zone
,
1958 uint16_t nzrwafgs
= DIV_ROUND_UP(nlbc
, ns
->zns
.zrwafg
);
1960 nlbc
= nzrwafgs
* ns
->zns
.zrwafg
;
1962 trace_pci_nvme_zoned_zrwa_implicit_flush(zone
->d
.zslba
, nlbc
);
1964 zone
->w_ptr
+= nlbc
;
1966 nvme_advance_zone_wp(ns
, zone
, nlbc
);
1969 static void nvme_finalize_zoned_write(NvmeNamespace
*ns
, NvmeRequest
*req
)
1971 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
1976 slba
= le64_to_cpu(rw
->slba
);
1977 nlb
= le16_to_cpu(rw
->nlb
) + 1;
1978 zone
= nvme_get_zone_by_slba(ns
, slba
);
1981 if (zone
->d
.za
& NVME_ZA_ZRWA_VALID
) {
1982 uint64_t ezrwa
= zone
->w_ptr
+ ns
->zns
.zrwas
- 1;
1983 uint64_t elba
= slba
+ nlb
- 1;
1986 nvme_zoned_zrwa_implicit_flush(ns
, zone
, elba
- ezrwa
);
1992 nvme_advance_zone_wp(ns
, zone
, nlb
);
1995 static inline bool nvme_is_write(NvmeRequest
*req
)
1997 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
1999 return rw
->opcode
== NVME_CMD_WRITE
||
2000 rw
->opcode
== NVME_CMD_ZONE_APPEND
||
2001 rw
->opcode
== NVME_CMD_WRITE_ZEROES
;
2004 static AioContext
*nvme_get_aio_context(BlockAIOCB
*acb
)
2006 return qemu_get_aio_context();
2009 static void nvme_misc_cb(void *opaque
, int ret
)
2011 NvmeRequest
*req
= opaque
;
2013 trace_pci_nvme_misc_cb(nvme_cid(req
));
2016 nvme_aio_err(req
, ret
);
2019 nvme_enqueue_req_completion(nvme_cq(req
), req
);
2022 void nvme_rw_complete_cb(void *opaque
, int ret
)
2024 NvmeRequest
*req
= opaque
;
2025 NvmeNamespace
*ns
= req
->ns
;
2026 BlockBackend
*blk
= ns
->blkconf
.blk
;
2027 BlockAcctCookie
*acct
= &req
->acct
;
2028 BlockAcctStats
*stats
= blk_get_stats(blk
);
2030 trace_pci_nvme_rw_complete_cb(nvme_cid(req
), blk_name(blk
));
2033 block_acct_failed(stats
, acct
);
2034 nvme_aio_err(req
, ret
);
2036 block_acct_done(stats
, acct
);
2039 if (ns
->params
.zoned
&& nvme_is_write(req
)) {
2040 nvme_finalize_zoned_write(ns
, req
);
2043 nvme_enqueue_req_completion(nvme_cq(req
), req
);
2046 static void nvme_rw_cb(void *opaque
, int ret
)
2048 NvmeRequest
*req
= opaque
;
2049 NvmeNamespace
*ns
= req
->ns
;
2051 BlockBackend
*blk
= ns
->blkconf
.blk
;
2053 trace_pci_nvme_rw_cb(nvme_cid(req
), blk_name(blk
));
2060 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
2061 uint64_t slba
= le64_to_cpu(rw
->slba
);
2062 uint32_t nlb
= (uint32_t)le16_to_cpu(rw
->nlb
) + 1;
2063 uint64_t offset
= nvme_moff(ns
, slba
);
2065 if (req
->cmd
.opcode
== NVME_CMD_WRITE_ZEROES
) {
2066 size_t mlen
= nvme_m2b(ns
, nlb
);
2068 req
->aiocb
= blk_aio_pwrite_zeroes(blk
, offset
, mlen
,
2070 nvme_rw_complete_cb
, req
);
2074 if (nvme_ns_ext(ns
) || req
->cmd
.mptr
) {
2077 nvme_sg_unmap(&req
->sg
);
2078 status
= nvme_map_mdata(nvme_ctrl(req
), nlb
, req
);
2084 if (req
->cmd
.opcode
== NVME_CMD_READ
) {
2085 return nvme_blk_read(blk
, offset
, nvme_rw_complete_cb
, req
);
2088 return nvme_blk_write(blk
, offset
, nvme_rw_complete_cb
, req
);
2093 nvme_rw_complete_cb(req
, ret
);
2096 static void nvme_verify_cb(void *opaque
, int ret
)
2098 NvmeBounceContext
*ctx
= opaque
;
2099 NvmeRequest
*req
= ctx
->req
;
2100 NvmeNamespace
*ns
= req
->ns
;
2101 BlockBackend
*blk
= ns
->blkconf
.blk
;
2102 BlockAcctCookie
*acct
= &req
->acct
;
2103 BlockAcctStats
*stats
= blk_get_stats(blk
);
2104 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
2105 uint64_t slba
= le64_to_cpu(rw
->slba
);
2106 uint8_t prinfo
= NVME_RW_PRINFO(le16_to_cpu(rw
->control
));
2107 uint16_t apptag
= le16_to_cpu(rw
->apptag
);
2108 uint16_t appmask
= le16_to_cpu(rw
->appmask
);
2109 uint64_t reftag
= le32_to_cpu(rw
->reftag
);
2110 uint64_t cdw3
= le32_to_cpu(rw
->cdw3
);
2113 reftag
|= cdw3
<< 32;
2115 trace_pci_nvme_verify_cb(nvme_cid(req
), prinfo
, apptag
, appmask
, reftag
);
2118 block_acct_failed(stats
, acct
);
2119 nvme_aio_err(req
, ret
);
2123 block_acct_done(stats
, acct
);
2125 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
2126 status
= nvme_dif_mangle_mdata(ns
, ctx
->mdata
.bounce
,
2127 ctx
->mdata
.iov
.size
, slba
);
2129 req
->status
= status
;
2133 req
->status
= nvme_dif_check(ns
, ctx
->data
.bounce
, ctx
->data
.iov
.size
,
2134 ctx
->mdata
.bounce
, ctx
->mdata
.iov
.size
,
2135 prinfo
, slba
, apptag
, appmask
, &reftag
);
2139 qemu_iovec_destroy(&ctx
->data
.iov
);
2140 g_free(ctx
->data
.bounce
);
2142 qemu_iovec_destroy(&ctx
->mdata
.iov
);
2143 g_free(ctx
->mdata
.bounce
);
2147 nvme_enqueue_req_completion(nvme_cq(req
), req
);
2151 static void nvme_verify_mdata_in_cb(void *opaque
, int ret
)
2153 NvmeBounceContext
*ctx
= opaque
;
2154 NvmeRequest
*req
= ctx
->req
;
2155 NvmeNamespace
*ns
= req
->ns
;
2156 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
2157 uint64_t slba
= le64_to_cpu(rw
->slba
);
2158 uint32_t nlb
= le16_to_cpu(rw
->nlb
) + 1;
2159 size_t mlen
= nvme_m2b(ns
, nlb
);
2160 uint64_t offset
= nvme_moff(ns
, slba
);
2161 BlockBackend
*blk
= ns
->blkconf
.blk
;
2163 trace_pci_nvme_verify_mdata_in_cb(nvme_cid(req
), blk_name(blk
));
2169 ctx
->mdata
.bounce
= g_malloc(mlen
);
2171 qemu_iovec_reset(&ctx
->mdata
.iov
);
2172 qemu_iovec_add(&ctx
->mdata
.iov
, ctx
->mdata
.bounce
, mlen
);
2174 req
->aiocb
= blk_aio_preadv(blk
, offset
, &ctx
->mdata
.iov
, 0,
2175 nvme_verify_cb
, ctx
);
2179 nvme_verify_cb(ctx
, ret
);
2182 struct nvme_compare_ctx
{
2194 static void nvme_compare_mdata_cb(void *opaque
, int ret
)
2196 NvmeRequest
*req
= opaque
;
2197 NvmeNamespace
*ns
= req
->ns
;
2198 NvmeCtrl
*n
= nvme_ctrl(req
);
2199 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
2200 uint8_t prinfo
= NVME_RW_PRINFO(le16_to_cpu(rw
->control
));
2201 uint16_t apptag
= le16_to_cpu(rw
->apptag
);
2202 uint16_t appmask
= le16_to_cpu(rw
->appmask
);
2203 uint64_t reftag
= le32_to_cpu(rw
->reftag
);
2204 uint64_t cdw3
= le32_to_cpu(rw
->cdw3
);
2205 struct nvme_compare_ctx
*ctx
= req
->opaque
;
2206 g_autofree
uint8_t *buf
= NULL
;
2207 BlockBackend
*blk
= ns
->blkconf
.blk
;
2208 BlockAcctCookie
*acct
= &req
->acct
;
2209 BlockAcctStats
*stats
= blk_get_stats(blk
);
2210 uint16_t status
= NVME_SUCCESS
;
2212 reftag
|= cdw3
<< 32;
2214 trace_pci_nvme_compare_mdata_cb(nvme_cid(req
));
2217 block_acct_failed(stats
, acct
);
2218 nvme_aio_err(req
, ret
);
2222 buf
= g_malloc(ctx
->mdata
.iov
.size
);
2224 status
= nvme_bounce_mdata(n
, buf
, ctx
->mdata
.iov
.size
,
2225 NVME_TX_DIRECTION_TO_DEVICE
, req
);
2227 req
->status
= status
;
2231 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
2232 uint64_t slba
= le64_to_cpu(rw
->slba
);
2234 uint8_t *mbufp
= ctx
->mdata
.bounce
;
2235 uint8_t *end
= mbufp
+ ctx
->mdata
.iov
.size
;
2238 status
= nvme_dif_check(ns
, ctx
->data
.bounce
, ctx
->data
.iov
.size
,
2239 ctx
->mdata
.bounce
, ctx
->mdata
.iov
.size
, prinfo
,
2240 slba
, apptag
, appmask
, &reftag
);
2242 req
->status
= status
;
2247 * When formatted with protection information, do not compare the DIF
2250 if (!(ns
->id_ns
.dps
& NVME_ID_NS_DPS_FIRST_EIGHT
)) {
2251 pil
= ns
->lbaf
.ms
- nvme_pi_tuple_size(ns
);
2254 for (bufp
= buf
; mbufp
< end
; bufp
+= ns
->lbaf
.ms
, mbufp
+= ns
->lbaf
.ms
) {
2255 if (memcmp(bufp
+ pil
, mbufp
+ pil
, ns
->lbaf
.ms
- pil
)) {
2256 req
->status
= NVME_CMP_FAILURE
;
2264 if (memcmp(buf
, ctx
->mdata
.bounce
, ctx
->mdata
.iov
.size
)) {
2265 req
->status
= NVME_CMP_FAILURE
;
2269 block_acct_done(stats
, acct
);
2272 qemu_iovec_destroy(&ctx
->data
.iov
);
2273 g_free(ctx
->data
.bounce
);
2275 qemu_iovec_destroy(&ctx
->mdata
.iov
);
2276 g_free(ctx
->mdata
.bounce
);
2280 nvme_enqueue_req_completion(nvme_cq(req
), req
);
2283 static void nvme_compare_data_cb(void *opaque
, int ret
)
2285 NvmeRequest
*req
= opaque
;
2286 NvmeCtrl
*n
= nvme_ctrl(req
);
2287 NvmeNamespace
*ns
= req
->ns
;
2288 BlockBackend
*blk
= ns
->blkconf
.blk
;
2289 BlockAcctCookie
*acct
= &req
->acct
;
2290 BlockAcctStats
*stats
= blk_get_stats(blk
);
2292 struct nvme_compare_ctx
*ctx
= req
->opaque
;
2293 g_autofree
uint8_t *buf
= NULL
;
2296 trace_pci_nvme_compare_data_cb(nvme_cid(req
));
2299 block_acct_failed(stats
, acct
);
2300 nvme_aio_err(req
, ret
);
2304 buf
= g_malloc(ctx
->data
.iov
.size
);
2306 status
= nvme_bounce_data(n
, buf
, ctx
->data
.iov
.size
,
2307 NVME_TX_DIRECTION_TO_DEVICE
, req
);
2309 req
->status
= status
;
2313 if (memcmp(buf
, ctx
->data
.bounce
, ctx
->data
.iov
.size
)) {
2314 req
->status
= NVME_CMP_FAILURE
;
2319 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
2320 uint64_t slba
= le64_to_cpu(rw
->slba
);
2321 uint32_t nlb
= le16_to_cpu(rw
->nlb
) + 1;
2322 size_t mlen
= nvme_m2b(ns
, nlb
);
2323 uint64_t offset
= nvme_moff(ns
, slba
);
2325 ctx
->mdata
.bounce
= g_malloc(mlen
);
2327 qemu_iovec_init(&ctx
->mdata
.iov
, 1);
2328 qemu_iovec_add(&ctx
->mdata
.iov
, ctx
->mdata
.bounce
, mlen
);
2330 req
->aiocb
= blk_aio_preadv(blk
, offset
, &ctx
->mdata
.iov
, 0,
2331 nvme_compare_mdata_cb
, req
);
2335 block_acct_done(stats
, acct
);
2338 qemu_iovec_destroy(&ctx
->data
.iov
);
2339 g_free(ctx
->data
.bounce
);
2342 nvme_enqueue_req_completion(nvme_cq(req
), req
);
2345 typedef struct NvmeDSMAIOCB
{
2351 NvmeDsmRange
*range
;
2356 static void nvme_dsm_cancel(BlockAIOCB
*aiocb
)
2358 NvmeDSMAIOCB
*iocb
= container_of(aiocb
, NvmeDSMAIOCB
, common
);
2360 /* break nvme_dsm_cb loop */
2361 iocb
->idx
= iocb
->nr
;
2362 iocb
->ret
= -ECANCELED
;
2365 blk_aio_cancel_async(iocb
->aiocb
);
2369 * We only reach this if nvme_dsm_cancel() has already been called or
2370 * the command ran to completion.
2372 assert(iocb
->idx
== iocb
->nr
);
2376 static const AIOCBInfo nvme_dsm_aiocb_info
= {
2377 .aiocb_size
= sizeof(NvmeDSMAIOCB
),
2378 .cancel_async
= nvme_dsm_cancel
,
2381 static void nvme_dsm_cb(void *opaque
, int ret
);
2383 static void nvme_dsm_md_cb(void *opaque
, int ret
)
2385 NvmeDSMAIOCB
*iocb
= opaque
;
2386 NvmeRequest
*req
= iocb
->req
;
2387 NvmeNamespace
*ns
= req
->ns
;
2388 NvmeDsmRange
*range
;
2392 if (ret
< 0 || iocb
->ret
< 0 || !ns
->lbaf
.ms
) {
2396 range
= &iocb
->range
[iocb
->idx
- 1];
2397 slba
= le64_to_cpu(range
->slba
);
2398 nlb
= le32_to_cpu(range
->nlb
);
2401 * Check that all block were discarded (zeroed); otherwise we do not zero
2405 ret
= nvme_block_status_all(ns
, slba
, nlb
, BDRV_BLOCK_ZERO
);
2411 nvme_dsm_cb(iocb
, 0);
2415 iocb
->aiocb
= blk_aio_pwrite_zeroes(ns
->blkconf
.blk
, nvme_moff(ns
, slba
),
2416 nvme_m2b(ns
, nlb
), BDRV_REQ_MAY_UNMAP
,
2421 nvme_dsm_cb(iocb
, ret
);
2424 static void nvme_dsm_cb(void *opaque
, int ret
)
2426 NvmeDSMAIOCB
*iocb
= opaque
;
2427 NvmeRequest
*req
= iocb
->req
;
2428 NvmeCtrl
*n
= nvme_ctrl(req
);
2429 NvmeNamespace
*ns
= req
->ns
;
2430 NvmeDsmRange
*range
;
2434 if (iocb
->ret
< 0) {
2436 } else if (ret
< 0) {
2442 if (iocb
->idx
== iocb
->nr
) {
2446 range
= &iocb
->range
[iocb
->idx
++];
2447 slba
= le64_to_cpu(range
->slba
);
2448 nlb
= le32_to_cpu(range
->nlb
);
2450 trace_pci_nvme_dsm_deallocate(slba
, nlb
);
2452 if (nlb
> n
->dmrsl
) {
2453 trace_pci_nvme_dsm_single_range_limit_exceeded(nlb
, n
->dmrsl
);
2457 if (nvme_check_bounds(ns
, slba
, nlb
)) {
2458 trace_pci_nvme_err_invalid_lba_range(slba
, nlb
,
2463 iocb
->aiocb
= blk_aio_pdiscard(ns
->blkconf
.blk
, nvme_l2b(ns
, slba
),
2465 nvme_dsm_md_cb
, iocb
);
2470 iocb
->common
.cb(iocb
->common
.opaque
, iocb
->ret
);
2471 qemu_aio_unref(iocb
);
2474 static uint16_t nvme_dsm(NvmeCtrl
*n
, NvmeRequest
*req
)
2476 NvmeNamespace
*ns
= req
->ns
;
2477 NvmeDsmCmd
*dsm
= (NvmeDsmCmd
*) &req
->cmd
;
2478 uint32_t attr
= le32_to_cpu(dsm
->attributes
);
2479 uint32_t nr
= (le32_to_cpu(dsm
->nr
) & 0xff) + 1;
2480 uint16_t status
= NVME_SUCCESS
;
2482 trace_pci_nvme_dsm(nr
, attr
);
2484 if (attr
& NVME_DSMGMT_AD
) {
2485 NvmeDSMAIOCB
*iocb
= blk_aio_get(&nvme_dsm_aiocb_info
, ns
->blkconf
.blk
,
2490 iocb
->range
= g_new(NvmeDsmRange
, nr
);
2494 status
= nvme_h2c(n
, (uint8_t *)iocb
->range
, sizeof(NvmeDsmRange
) * nr
,
2500 req
->aiocb
= &iocb
->common
;
2501 nvme_dsm_cb(iocb
, 0);
2503 return NVME_NO_COMPLETE
;
2509 static uint16_t nvme_verify(NvmeCtrl
*n
, NvmeRequest
*req
)
2511 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
2512 NvmeNamespace
*ns
= req
->ns
;
2513 BlockBackend
*blk
= ns
->blkconf
.blk
;
2514 uint64_t slba
= le64_to_cpu(rw
->slba
);
2515 uint32_t nlb
= le16_to_cpu(rw
->nlb
) + 1;
2516 size_t len
= nvme_l2b(ns
, nlb
);
2517 int64_t offset
= nvme_l2b(ns
, slba
);
2518 uint8_t prinfo
= NVME_RW_PRINFO(le16_to_cpu(rw
->control
));
2519 uint32_t reftag
= le32_to_cpu(rw
->reftag
);
2520 NvmeBounceContext
*ctx
= NULL
;
2523 trace_pci_nvme_verify(nvme_cid(req
), nvme_nsid(ns
), slba
, nlb
);
2525 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
2526 status
= nvme_check_prinfo(ns
, prinfo
, slba
, reftag
);
2531 if (prinfo
& NVME_PRINFO_PRACT
) {
2532 return NVME_INVALID_PROT_INFO
| NVME_DNR
;
2536 if (len
> n
->page_size
<< n
->params
.vsl
) {
2537 return NVME_INVALID_FIELD
| NVME_DNR
;
2540 status
= nvme_check_bounds(ns
, slba
, nlb
);
2545 if (NVME_ERR_REC_DULBE(ns
->features
.err_rec
)) {
2546 status
= nvme_check_dulbe(ns
, slba
, nlb
);
2552 ctx
= g_new0(NvmeBounceContext
, 1);
2555 ctx
->data
.bounce
= g_malloc(len
);
2557 qemu_iovec_init(&ctx
->data
.iov
, 1);
2558 qemu_iovec_add(&ctx
->data
.iov
, ctx
->data
.bounce
, len
);
2560 block_acct_start(blk_get_stats(blk
), &req
->acct
, ctx
->data
.iov
.size
,
2563 req
->aiocb
= blk_aio_preadv(ns
->blkconf
.blk
, offset
, &ctx
->data
.iov
, 0,
2564 nvme_verify_mdata_in_cb
, ctx
);
2565 return NVME_NO_COMPLETE
;
2568 typedef struct NvmeCopyAIOCB
{
2575 unsigned int format
;
2582 BlockAcctCookie read
;
2583 BlockAcctCookie write
;
2592 static void nvme_copy_cancel(BlockAIOCB
*aiocb
)
2594 NvmeCopyAIOCB
*iocb
= container_of(aiocb
, NvmeCopyAIOCB
, common
);
2596 iocb
->ret
= -ECANCELED
;
2599 blk_aio_cancel_async(iocb
->aiocb
);
2604 static const AIOCBInfo nvme_copy_aiocb_info
= {
2605 .aiocb_size
= sizeof(NvmeCopyAIOCB
),
2606 .cancel_async
= nvme_copy_cancel
,
2609 static void nvme_copy_done(NvmeCopyAIOCB
*iocb
)
2611 NvmeRequest
*req
= iocb
->req
;
2612 NvmeNamespace
*ns
= req
->ns
;
2613 BlockAcctStats
*stats
= blk_get_stats(ns
->blkconf
.blk
);
2615 if (iocb
->idx
!= iocb
->nr
) {
2616 req
->cqe
.result
= cpu_to_le32(iocb
->idx
);
2619 qemu_iovec_destroy(&iocb
->iov
);
2620 g_free(iocb
->bounce
);
2622 if (iocb
->ret
< 0) {
2623 block_acct_failed(stats
, &iocb
->acct
.read
);
2624 block_acct_failed(stats
, &iocb
->acct
.write
);
2626 block_acct_done(stats
, &iocb
->acct
.read
);
2627 block_acct_done(stats
, &iocb
->acct
.write
);
2630 iocb
->common
.cb(iocb
->common
.opaque
, iocb
->ret
);
2631 qemu_aio_unref(iocb
);
2634 static void nvme_do_copy(NvmeCopyAIOCB
*iocb
);
2636 static void nvme_copy_source_range_parse_format0(void *ranges
, int idx
,
2637 uint64_t *slba
, uint32_t *nlb
,
2642 NvmeCopySourceRangeFormat0
*_ranges
= ranges
;
2645 *slba
= le64_to_cpu(_ranges
[idx
].slba
);
2649 *nlb
= le16_to_cpu(_ranges
[idx
].nlb
) + 1;
2653 *apptag
= le16_to_cpu(_ranges
[idx
].apptag
);
2657 *appmask
= le16_to_cpu(_ranges
[idx
].appmask
);
2661 *reftag
= le32_to_cpu(_ranges
[idx
].reftag
);
2665 static void nvme_copy_source_range_parse_format1(void *ranges
, int idx
,
2666 uint64_t *slba
, uint32_t *nlb
,
2671 NvmeCopySourceRangeFormat1
*_ranges
= ranges
;
2674 *slba
= le64_to_cpu(_ranges
[idx
].slba
);
2678 *nlb
= le16_to_cpu(_ranges
[idx
].nlb
) + 1;
2682 *apptag
= le16_to_cpu(_ranges
[idx
].apptag
);
2686 *appmask
= le16_to_cpu(_ranges
[idx
].appmask
);
2692 *reftag
|= (uint64_t)_ranges
[idx
].sr
[4] << 40;
2693 *reftag
|= (uint64_t)_ranges
[idx
].sr
[5] << 32;
2694 *reftag
|= (uint64_t)_ranges
[idx
].sr
[6] << 24;
2695 *reftag
|= (uint64_t)_ranges
[idx
].sr
[7] << 16;
2696 *reftag
|= (uint64_t)_ranges
[idx
].sr
[8] << 8;
2697 *reftag
|= (uint64_t)_ranges
[idx
].sr
[9];
2701 static void nvme_copy_source_range_parse(void *ranges
, int idx
, uint8_t format
,
2702 uint64_t *slba
, uint32_t *nlb
,
2703 uint16_t *apptag
, uint16_t *appmask
,
2707 case NVME_COPY_FORMAT_0
:
2708 nvme_copy_source_range_parse_format0(ranges
, idx
, slba
, nlb
, apptag
,
2712 case NVME_COPY_FORMAT_1
:
2713 nvme_copy_source_range_parse_format1(ranges
, idx
, slba
, nlb
, apptag
,
2722 static void nvme_copy_out_completed_cb(void *opaque
, int ret
)
2724 NvmeCopyAIOCB
*iocb
= opaque
;
2725 NvmeRequest
*req
= iocb
->req
;
2726 NvmeNamespace
*ns
= req
->ns
;
2729 nvme_copy_source_range_parse(iocb
->ranges
, iocb
->idx
, iocb
->format
, NULL
,
2730 &nlb
, NULL
, NULL
, NULL
);
2735 } else if (iocb
->ret
< 0) {
2739 if (ns
->params
.zoned
) {
2740 nvme_advance_zone_wp(ns
, iocb
->zone
, nlb
);
2749 static void nvme_copy_out_cb(void *opaque
, int ret
)
2751 NvmeCopyAIOCB
*iocb
= opaque
;
2752 NvmeRequest
*req
= iocb
->req
;
2753 NvmeNamespace
*ns
= req
->ns
;
2758 if (ret
< 0 || iocb
->ret
< 0 || !ns
->lbaf
.ms
) {
2762 nvme_copy_source_range_parse(iocb
->ranges
, iocb
->idx
, iocb
->format
, NULL
,
2763 &nlb
, NULL
, NULL
, NULL
);
2765 mlen
= nvme_m2b(ns
, nlb
);
2766 mbounce
= iocb
->bounce
+ nvme_l2b(ns
, nlb
);
2768 qemu_iovec_reset(&iocb
->iov
);
2769 qemu_iovec_add(&iocb
->iov
, mbounce
, mlen
);
2771 iocb
->aiocb
= blk_aio_pwritev(ns
->blkconf
.blk
, nvme_moff(ns
, iocb
->slba
),
2772 &iocb
->iov
, 0, nvme_copy_out_completed_cb
,
2778 nvme_copy_out_completed_cb(iocb
, ret
);
2781 static void nvme_copy_in_completed_cb(void *opaque
, int ret
)
2783 NvmeCopyAIOCB
*iocb
= opaque
;
2784 NvmeRequest
*req
= iocb
->req
;
2785 NvmeNamespace
*ns
= req
->ns
;
2788 uint16_t apptag
, appmask
;
2796 } else if (iocb
->ret
< 0) {
2800 nvme_copy_source_range_parse(iocb
->ranges
, iocb
->idx
, iocb
->format
, &slba
,
2801 &nlb
, &apptag
, &appmask
, &reftag
);
2802 len
= nvme_l2b(ns
, nlb
);
2804 trace_pci_nvme_copy_out(iocb
->slba
, nlb
);
2806 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
2807 NvmeCopyCmd
*copy
= (NvmeCopyCmd
*)&req
->cmd
;
2809 uint16_t prinfor
= ((copy
->control
[0] >> 4) & 0xf);
2810 uint16_t prinfow
= ((copy
->control
[2] >> 2) & 0xf);
2812 size_t mlen
= nvme_m2b(ns
, nlb
);
2813 uint8_t *mbounce
= iocb
->bounce
+ nvme_l2b(ns
, nlb
);
2815 status
= nvme_dif_mangle_mdata(ns
, mbounce
, mlen
, slba
);
2819 status
= nvme_dif_check(ns
, iocb
->bounce
, len
, mbounce
, mlen
, prinfor
,
2820 slba
, apptag
, appmask
, &reftag
);
2825 apptag
= le16_to_cpu(copy
->apptag
);
2826 appmask
= le16_to_cpu(copy
->appmask
);
2828 if (prinfow
& NVME_PRINFO_PRACT
) {
2829 status
= nvme_check_prinfo(ns
, prinfow
, iocb
->slba
, iocb
->reftag
);
2834 nvme_dif_pract_generate_dif(ns
, iocb
->bounce
, len
, mbounce
, mlen
,
2835 apptag
, &iocb
->reftag
);
2837 status
= nvme_dif_check(ns
, iocb
->bounce
, len
, mbounce
, mlen
,
2838 prinfow
, iocb
->slba
, apptag
, appmask
,
2846 status
= nvme_check_bounds(ns
, iocb
->slba
, nlb
);
2851 if (ns
->params
.zoned
) {
2852 status
= nvme_check_zone_write(ns
, iocb
->zone
, iocb
->slba
, nlb
);
2857 if (!(iocb
->zone
->d
.za
& NVME_ZA_ZRWA_VALID
)) {
2858 iocb
->zone
->w_ptr
+= nlb
;
2862 qemu_iovec_reset(&iocb
->iov
);
2863 qemu_iovec_add(&iocb
->iov
, iocb
->bounce
, len
);
2865 iocb
->aiocb
= blk_aio_pwritev(ns
->blkconf
.blk
, nvme_l2b(ns
, iocb
->slba
),
2866 &iocb
->iov
, 0, nvme_copy_out_cb
, iocb
);
2871 req
->status
= status
;
2877 static void nvme_copy_in_cb(void *opaque
, int ret
)
2879 NvmeCopyAIOCB
*iocb
= opaque
;
2880 NvmeRequest
*req
= iocb
->req
;
2881 NvmeNamespace
*ns
= req
->ns
;
2885 if (ret
< 0 || iocb
->ret
< 0 || !ns
->lbaf
.ms
) {
2889 nvme_copy_source_range_parse(iocb
->ranges
, iocb
->idx
, iocb
->format
, &slba
,
2890 &nlb
, NULL
, NULL
, NULL
);
2892 qemu_iovec_reset(&iocb
->iov
);
2893 qemu_iovec_add(&iocb
->iov
, iocb
->bounce
+ nvme_l2b(ns
, nlb
),
2896 iocb
->aiocb
= blk_aio_preadv(ns
->blkconf
.blk
, nvme_moff(ns
, slba
),
2897 &iocb
->iov
, 0, nvme_copy_in_completed_cb
,
2902 nvme_copy_in_completed_cb(iocb
, ret
);
2905 static void nvme_do_copy(NvmeCopyAIOCB
*iocb
)
2907 NvmeRequest
*req
= iocb
->req
;
2908 NvmeNamespace
*ns
= req
->ns
;
2914 if (iocb
->ret
< 0) {
2918 if (iocb
->idx
== iocb
->nr
) {
2922 nvme_copy_source_range_parse(iocb
->ranges
, iocb
->idx
, iocb
->format
, &slba
,
2923 &nlb
, NULL
, NULL
, NULL
);
2924 len
= nvme_l2b(ns
, nlb
);
2926 trace_pci_nvme_copy_source_range(slba
, nlb
);
2928 if (nlb
> le16_to_cpu(ns
->id_ns
.mssrl
)) {
2929 status
= NVME_CMD_SIZE_LIMIT
| NVME_DNR
;
2933 status
= nvme_check_bounds(ns
, slba
, nlb
);
2938 if (NVME_ERR_REC_DULBE(ns
->features
.err_rec
)) {
2939 status
= nvme_check_dulbe(ns
, slba
, nlb
);
2945 if (ns
->params
.zoned
) {
2946 status
= nvme_check_zone_read(ns
, slba
, nlb
);
2952 qemu_iovec_reset(&iocb
->iov
);
2953 qemu_iovec_add(&iocb
->iov
, iocb
->bounce
, len
);
2955 iocb
->aiocb
= blk_aio_preadv(ns
->blkconf
.blk
, nvme_l2b(ns
, slba
),
2956 &iocb
->iov
, 0, nvme_copy_in_cb
, iocb
);
2960 req
->status
= status
;
2963 nvme_copy_done(iocb
);
2966 static uint16_t nvme_copy(NvmeCtrl
*n
, NvmeRequest
*req
)
2968 NvmeNamespace
*ns
= req
->ns
;
2969 NvmeCopyCmd
*copy
= (NvmeCopyCmd
*)&req
->cmd
;
2970 NvmeCopyAIOCB
*iocb
= blk_aio_get(&nvme_copy_aiocb_info
, ns
->blkconf
.blk
,
2972 uint16_t nr
= copy
->nr
+ 1;
2973 uint8_t format
= copy
->control
[0] & 0xf;
2974 uint16_t prinfor
= ((copy
->control
[0] >> 4) & 0xf);
2975 uint16_t prinfow
= ((copy
->control
[2] >> 2) & 0xf);
2976 size_t len
= sizeof(NvmeCopySourceRangeFormat0
);
2980 trace_pci_nvme_copy(nvme_cid(req
), nvme_nsid(ns
), nr
, format
);
2982 iocb
->ranges
= NULL
;
2985 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
) &&
2986 ((prinfor
& NVME_PRINFO_PRACT
) != (prinfow
& NVME_PRINFO_PRACT
))) {
2987 status
= NVME_INVALID_FIELD
| NVME_DNR
;
2991 if (!(n
->id_ctrl
.ocfs
& (1 << format
))) {
2992 trace_pci_nvme_err_copy_invalid_format(format
);
2993 status
= NVME_INVALID_FIELD
| NVME_DNR
;
2997 if (nr
> ns
->id_ns
.msrc
+ 1) {
2998 status
= NVME_CMD_SIZE_LIMIT
| NVME_DNR
;
3002 if ((ns
->pif
== 0x0 && format
!= 0x0) ||
3003 (ns
->pif
!= 0x0 && format
!= 0x1)) {
3004 status
= NVME_INVALID_FORMAT
| NVME_DNR
;
3009 len
= sizeof(NvmeCopySourceRangeFormat1
);
3012 iocb
->format
= format
;
3013 iocb
->ranges
= g_malloc_n(nr
, len
);
3014 status
= nvme_h2c(n
, (uint8_t *)iocb
->ranges
, len
* nr
, req
);
3019 iocb
->slba
= le64_to_cpu(copy
->sdlba
);
3021 if (ns
->params
.zoned
) {
3022 iocb
->zone
= nvme_get_zone_by_slba(ns
, iocb
->slba
);
3024 status
= NVME_LBA_RANGE
| NVME_DNR
;
3028 status
= nvme_zrm_auto(n
, ns
, iocb
->zone
);
3038 iocb
->reftag
= le32_to_cpu(copy
->reftag
);
3039 iocb
->reftag
|= (uint64_t)le32_to_cpu(copy
->cdw3
) << 32;
3040 iocb
->bounce
= g_malloc_n(le16_to_cpu(ns
->id_ns
.mssrl
),
3041 ns
->lbasz
+ ns
->lbaf
.ms
);
3043 qemu_iovec_init(&iocb
->iov
, 1);
3045 block_acct_start(blk_get_stats(ns
->blkconf
.blk
), &iocb
->acct
.read
, 0,
3047 block_acct_start(blk_get_stats(ns
->blkconf
.blk
), &iocb
->acct
.write
, 0,
3050 req
->aiocb
= &iocb
->common
;
3053 return NVME_NO_COMPLETE
;
3056 g_free(iocb
->ranges
);
3057 qemu_aio_unref(iocb
);
3061 static uint16_t nvme_compare(NvmeCtrl
*n
, NvmeRequest
*req
)
3063 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
3064 NvmeNamespace
*ns
= req
->ns
;
3065 BlockBackend
*blk
= ns
->blkconf
.blk
;
3066 uint64_t slba
= le64_to_cpu(rw
->slba
);
3067 uint32_t nlb
= le16_to_cpu(rw
->nlb
) + 1;
3068 uint8_t prinfo
= NVME_RW_PRINFO(le16_to_cpu(rw
->control
));
3069 size_t data_len
= nvme_l2b(ns
, nlb
);
3070 size_t len
= data_len
;
3071 int64_t offset
= nvme_l2b(ns
, slba
);
3072 struct nvme_compare_ctx
*ctx
= NULL
;
3075 trace_pci_nvme_compare(nvme_cid(req
), nvme_nsid(ns
), slba
, nlb
);
3077 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
) && (prinfo
& NVME_PRINFO_PRACT
)) {
3078 return NVME_INVALID_PROT_INFO
| NVME_DNR
;
3081 if (nvme_ns_ext(ns
)) {
3082 len
+= nvme_m2b(ns
, nlb
);
3085 status
= nvme_check_mdts(n
, len
);
3090 status
= nvme_check_bounds(ns
, slba
, nlb
);
3095 if (NVME_ERR_REC_DULBE(ns
->features
.err_rec
)) {
3096 status
= nvme_check_dulbe(ns
, slba
, nlb
);
3102 status
= nvme_map_dptr(n
, &req
->sg
, len
, &req
->cmd
);
3107 ctx
= g_new(struct nvme_compare_ctx
, 1);
3108 ctx
->data
.bounce
= g_malloc(data_len
);
3112 qemu_iovec_init(&ctx
->data
.iov
, 1);
3113 qemu_iovec_add(&ctx
->data
.iov
, ctx
->data
.bounce
, data_len
);
3115 block_acct_start(blk_get_stats(blk
), &req
->acct
, data_len
,
3117 req
->aiocb
= blk_aio_preadv(blk
, offset
, &ctx
->data
.iov
, 0,
3118 nvme_compare_data_cb
, req
);
3120 return NVME_NO_COMPLETE
;
3123 typedef struct NvmeFlushAIOCB
{
3134 static void nvme_flush_cancel(BlockAIOCB
*acb
)
3136 NvmeFlushAIOCB
*iocb
= container_of(acb
, NvmeFlushAIOCB
, common
);
3138 iocb
->ret
= -ECANCELED
;
3141 blk_aio_cancel_async(iocb
->aiocb
);
3146 static const AIOCBInfo nvme_flush_aiocb_info
= {
3147 .aiocb_size
= sizeof(NvmeFlushAIOCB
),
3148 .cancel_async
= nvme_flush_cancel
,
3149 .get_aio_context
= nvme_get_aio_context
,
3152 static void nvme_do_flush(NvmeFlushAIOCB
*iocb
);
3154 static void nvme_flush_ns_cb(void *opaque
, int ret
)
3156 NvmeFlushAIOCB
*iocb
= opaque
;
3157 NvmeNamespace
*ns
= iocb
->ns
;
3162 } else if (iocb
->ret
< 0) {
3167 trace_pci_nvme_flush_ns(iocb
->nsid
);
3170 iocb
->aiocb
= blk_aio_flush(ns
->blkconf
.blk
, nvme_flush_ns_cb
, iocb
);
3175 nvme_do_flush(iocb
);
3178 static void nvme_do_flush(NvmeFlushAIOCB
*iocb
)
3180 NvmeRequest
*req
= iocb
->req
;
3181 NvmeCtrl
*n
= nvme_ctrl(req
);
3184 if (iocb
->ret
< 0) {
3188 if (iocb
->broadcast
) {
3189 for (i
= iocb
->nsid
+ 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
3190 iocb
->ns
= nvme_ns(n
, i
);
3202 nvme_flush_ns_cb(iocb
, 0);
3206 iocb
->common
.cb(iocb
->common
.opaque
, iocb
->ret
);
3207 qemu_aio_unref(iocb
);
3210 static uint16_t nvme_flush(NvmeCtrl
*n
, NvmeRequest
*req
)
3212 NvmeFlushAIOCB
*iocb
;
3213 uint32_t nsid
= le32_to_cpu(req
->cmd
.nsid
);
3216 iocb
= qemu_aio_get(&nvme_flush_aiocb_info
, NULL
, nvme_misc_cb
, req
);
3222 iocb
->broadcast
= (nsid
== NVME_NSID_BROADCAST
);
3224 if (!iocb
->broadcast
) {
3225 if (!nvme_nsid_valid(n
, nsid
)) {
3226 status
= NVME_INVALID_NSID
| NVME_DNR
;
3230 iocb
->ns
= nvme_ns(n
, nsid
);
3232 status
= NVME_INVALID_FIELD
| NVME_DNR
;
3239 req
->aiocb
= &iocb
->common
;
3240 nvme_do_flush(iocb
);
3242 return NVME_NO_COMPLETE
;
3245 qemu_aio_unref(iocb
);
3250 static uint16_t nvme_read(NvmeCtrl
*n
, NvmeRequest
*req
)
3252 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
3253 NvmeNamespace
*ns
= req
->ns
;
3254 uint64_t slba
= le64_to_cpu(rw
->slba
);
3255 uint32_t nlb
= (uint32_t)le16_to_cpu(rw
->nlb
) + 1;
3256 uint8_t prinfo
= NVME_RW_PRINFO(le16_to_cpu(rw
->control
));
3257 uint64_t data_size
= nvme_l2b(ns
, nlb
);
3258 uint64_t mapped_size
= data_size
;
3259 uint64_t data_offset
;
3260 BlockBackend
*blk
= ns
->blkconf
.blk
;
3263 if (nvme_ns_ext(ns
)) {
3264 mapped_size
+= nvme_m2b(ns
, nlb
);
3266 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
3267 bool pract
= prinfo
& NVME_PRINFO_PRACT
;
3269 if (pract
&& ns
->lbaf
.ms
== nvme_pi_tuple_size(ns
)) {
3270 mapped_size
= data_size
;
3275 trace_pci_nvme_read(nvme_cid(req
), nvme_nsid(ns
), nlb
, mapped_size
, slba
);
3277 status
= nvme_check_mdts(n
, mapped_size
);
3282 status
= nvme_check_bounds(ns
, slba
, nlb
);
3287 if (ns
->params
.zoned
) {
3288 status
= nvme_check_zone_read(ns
, slba
, nlb
);
3290 trace_pci_nvme_err_zone_read_not_ok(slba
, nlb
, status
);
3295 if (NVME_ERR_REC_DULBE(ns
->features
.err_rec
)) {
3296 status
= nvme_check_dulbe(ns
, slba
, nlb
);
3302 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
3303 return nvme_dif_rw(n
, req
);
3306 status
= nvme_map_data(n
, nlb
, req
);
3311 data_offset
= nvme_l2b(ns
, slba
);
3313 block_acct_start(blk_get_stats(blk
), &req
->acct
, data_size
,
3315 nvme_blk_read(blk
, data_offset
, nvme_rw_cb
, req
);
3316 return NVME_NO_COMPLETE
;
3319 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
3320 return status
| NVME_DNR
;
3323 static uint16_t nvme_do_write(NvmeCtrl
*n
, NvmeRequest
*req
, bool append
,
3326 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
3327 NvmeNamespace
*ns
= req
->ns
;
3328 uint64_t slba
= le64_to_cpu(rw
->slba
);
3329 uint32_t nlb
= (uint32_t)le16_to_cpu(rw
->nlb
) + 1;
3330 uint16_t ctrl
= le16_to_cpu(rw
->control
);
3331 uint8_t prinfo
= NVME_RW_PRINFO(ctrl
);
3332 uint64_t data_size
= nvme_l2b(ns
, nlb
);
3333 uint64_t mapped_size
= data_size
;
3334 uint64_t data_offset
;
3336 NvmeZonedResult
*res
= (NvmeZonedResult
*)&req
->cqe
;
3337 BlockBackend
*blk
= ns
->blkconf
.blk
;
3340 if (nvme_ns_ext(ns
)) {
3341 mapped_size
+= nvme_m2b(ns
, nlb
);
3343 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
3344 bool pract
= prinfo
& NVME_PRINFO_PRACT
;
3346 if (pract
&& ns
->lbaf
.ms
== nvme_pi_tuple_size(ns
)) {
3347 mapped_size
-= nvme_m2b(ns
, nlb
);
3352 trace_pci_nvme_write(nvme_cid(req
), nvme_io_opc_str(rw
->opcode
),
3353 nvme_nsid(ns
), nlb
, mapped_size
, slba
);
3356 status
= nvme_check_mdts(n
, mapped_size
);
3362 status
= nvme_check_bounds(ns
, slba
, nlb
);
3367 if (ns
->params
.zoned
) {
3368 zone
= nvme_get_zone_by_slba(ns
, slba
);
3372 bool piremap
= !!(ctrl
& NVME_RW_PIREMAP
);
3374 if (unlikely(zone
->d
.za
& NVME_ZA_ZRWA_VALID
)) {
3375 return NVME_INVALID_ZONE_OP
| NVME_DNR
;
3378 if (unlikely(slba
!= zone
->d
.zslba
)) {
3379 trace_pci_nvme_err_append_not_at_start(slba
, zone
->d
.zslba
);
3380 status
= NVME_INVALID_FIELD
;
3384 if (n
->params
.zasl
&&
3385 data_size
> (uint64_t)n
->page_size
<< n
->params
.zasl
) {
3386 trace_pci_nvme_err_zasl(data_size
);
3387 return NVME_INVALID_FIELD
| NVME_DNR
;
3391 rw
->slba
= cpu_to_le64(slba
);
3392 res
->slba
= cpu_to_le64(slba
);
3394 switch (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
3395 case NVME_ID_NS_DPS_TYPE_1
:
3397 return NVME_INVALID_PROT_INFO
| NVME_DNR
;
3402 case NVME_ID_NS_DPS_TYPE_2
:
3404 uint32_t reftag
= le32_to_cpu(rw
->reftag
);
3405 rw
->reftag
= cpu_to_le32(reftag
+ (slba
- zone
->d
.zslba
));
3410 case NVME_ID_NS_DPS_TYPE_3
:
3412 return NVME_INVALID_PROT_INFO
| NVME_DNR
;
3419 status
= nvme_check_zone_write(ns
, zone
, slba
, nlb
);
3424 status
= nvme_zrm_auto(n
, ns
, zone
);
3429 if (!(zone
->d
.za
& NVME_ZA_ZRWA_VALID
)) {
3434 data_offset
= nvme_l2b(ns
, slba
);
3436 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
3437 return nvme_dif_rw(n
, req
);
3441 status
= nvme_map_data(n
, nlb
, req
);
3446 block_acct_start(blk_get_stats(blk
), &req
->acct
, data_size
,
3448 nvme_blk_write(blk
, data_offset
, nvme_rw_cb
, req
);
3450 req
->aiocb
= blk_aio_pwrite_zeroes(blk
, data_offset
, data_size
,
3451 BDRV_REQ_MAY_UNMAP
, nvme_rw_cb
,
3455 return NVME_NO_COMPLETE
;
3458 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
3459 return status
| NVME_DNR
;
3462 static inline uint16_t nvme_write(NvmeCtrl
*n
, NvmeRequest
*req
)
3464 return nvme_do_write(n
, req
, false, false);
3467 static inline uint16_t nvme_write_zeroes(NvmeCtrl
*n
, NvmeRequest
*req
)
3469 return nvme_do_write(n
, req
, false, true);
3472 static inline uint16_t nvme_zone_append(NvmeCtrl
*n
, NvmeRequest
*req
)
3474 return nvme_do_write(n
, req
, true, false);
3477 static uint16_t nvme_get_mgmt_zone_slba_idx(NvmeNamespace
*ns
, NvmeCmd
*c
,
3478 uint64_t *slba
, uint32_t *zone_idx
)
3480 uint32_t dw10
= le32_to_cpu(c
->cdw10
);
3481 uint32_t dw11
= le32_to_cpu(c
->cdw11
);
3483 if (!ns
->params
.zoned
) {
3484 trace_pci_nvme_err_invalid_opc(c
->opcode
);
3485 return NVME_INVALID_OPCODE
| NVME_DNR
;
3488 *slba
= ((uint64_t)dw11
) << 32 | dw10
;
3489 if (unlikely(*slba
>= ns
->id_ns
.nsze
)) {
3490 trace_pci_nvme_err_invalid_lba_range(*slba
, 0, ns
->id_ns
.nsze
);
3492 return NVME_LBA_RANGE
| NVME_DNR
;
3495 *zone_idx
= nvme_zone_idx(ns
, *slba
);
3496 assert(*zone_idx
< ns
->num_zones
);
3498 return NVME_SUCCESS
;
3501 typedef uint16_t (*op_handler_t
)(NvmeNamespace
*, NvmeZone
*, NvmeZoneState
,
3504 enum NvmeZoneProcessingMask
{
3505 NVME_PROC_CURRENT_ZONE
= 0,
3506 NVME_PROC_OPENED_ZONES
= 1 << 0,
3507 NVME_PROC_CLOSED_ZONES
= 1 << 1,
3508 NVME_PROC_READ_ONLY_ZONES
= 1 << 2,
3509 NVME_PROC_FULL_ZONES
= 1 << 3,
3512 static uint16_t nvme_open_zone(NvmeNamespace
*ns
, NvmeZone
*zone
,
3513 NvmeZoneState state
, NvmeRequest
*req
)
3515 NvmeZoneSendCmd
*cmd
= (NvmeZoneSendCmd
*)&req
->cmd
;
3518 if (cmd
->zsflags
& NVME_ZSFLAG_ZRWA_ALLOC
) {
3519 uint16_t ozcs
= le16_to_cpu(ns
->id_ns_zoned
->ozcs
);
3521 if (!(ozcs
& NVME_ID_NS_ZONED_OZCS_ZRWASUP
)) {
3522 return NVME_INVALID_ZONE_OP
| NVME_DNR
;
3525 if (zone
->w_ptr
% ns
->zns
.zrwafg
) {
3526 return NVME_NOZRWA
| NVME_DNR
;
3529 flags
= NVME_ZRM_ZRWA
;
3532 return nvme_zrm_open_flags(nvme_ctrl(req
), ns
, zone
, flags
);
3535 static uint16_t nvme_close_zone(NvmeNamespace
*ns
, NvmeZone
*zone
,
3536 NvmeZoneState state
, NvmeRequest
*req
)
3538 return nvme_zrm_close(ns
, zone
);
3541 static uint16_t nvme_finish_zone(NvmeNamespace
*ns
, NvmeZone
*zone
,
3542 NvmeZoneState state
, NvmeRequest
*req
)
3544 return nvme_zrm_finish(ns
, zone
);
3547 static uint16_t nvme_offline_zone(NvmeNamespace
*ns
, NvmeZone
*zone
,
3548 NvmeZoneState state
, NvmeRequest
*req
)
3551 case NVME_ZONE_STATE_READ_ONLY
:
3552 nvme_assign_zone_state(ns
, zone
, NVME_ZONE_STATE_OFFLINE
);
3554 case NVME_ZONE_STATE_OFFLINE
:
3555 return NVME_SUCCESS
;
3557 return NVME_ZONE_INVAL_TRANSITION
;
3561 static uint16_t nvme_set_zd_ext(NvmeNamespace
*ns
, NvmeZone
*zone
)
3564 uint8_t state
= nvme_get_zone_state(zone
);
3566 if (state
== NVME_ZONE_STATE_EMPTY
) {
3567 status
= nvme_aor_check(ns
, 1, 0);
3571 nvme_aor_inc_active(ns
);
3572 zone
->d
.za
|= NVME_ZA_ZD_EXT_VALID
;
3573 nvme_assign_zone_state(ns
, zone
, NVME_ZONE_STATE_CLOSED
);
3574 return NVME_SUCCESS
;
3577 return NVME_ZONE_INVAL_TRANSITION
;
3580 static uint16_t nvme_bulk_proc_zone(NvmeNamespace
*ns
, NvmeZone
*zone
,
3581 enum NvmeZoneProcessingMask proc_mask
,
3582 op_handler_t op_hndlr
, NvmeRequest
*req
)
3584 uint16_t status
= NVME_SUCCESS
;
3585 NvmeZoneState zs
= nvme_get_zone_state(zone
);
3589 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
3590 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
3591 proc_zone
= proc_mask
& NVME_PROC_OPENED_ZONES
;
3593 case NVME_ZONE_STATE_CLOSED
:
3594 proc_zone
= proc_mask
& NVME_PROC_CLOSED_ZONES
;
3596 case NVME_ZONE_STATE_READ_ONLY
:
3597 proc_zone
= proc_mask
& NVME_PROC_READ_ONLY_ZONES
;
3599 case NVME_ZONE_STATE_FULL
:
3600 proc_zone
= proc_mask
& NVME_PROC_FULL_ZONES
;
3607 status
= op_hndlr(ns
, zone
, zs
, req
);
3613 static uint16_t nvme_do_zone_op(NvmeNamespace
*ns
, NvmeZone
*zone
,
3614 enum NvmeZoneProcessingMask proc_mask
,
3615 op_handler_t op_hndlr
, NvmeRequest
*req
)
3618 uint16_t status
= NVME_SUCCESS
;
3622 status
= op_hndlr(ns
, zone
, nvme_get_zone_state(zone
), req
);
3624 if (proc_mask
& NVME_PROC_CLOSED_ZONES
) {
3625 QTAILQ_FOREACH_SAFE(zone
, &ns
->closed_zones
, entry
, next
) {
3626 status
= nvme_bulk_proc_zone(ns
, zone
, proc_mask
, op_hndlr
,
3628 if (status
&& status
!= NVME_NO_COMPLETE
) {
3633 if (proc_mask
& NVME_PROC_OPENED_ZONES
) {
3634 QTAILQ_FOREACH_SAFE(zone
, &ns
->imp_open_zones
, entry
, next
) {
3635 status
= nvme_bulk_proc_zone(ns
, zone
, proc_mask
, op_hndlr
,
3637 if (status
&& status
!= NVME_NO_COMPLETE
) {
3642 QTAILQ_FOREACH_SAFE(zone
, &ns
->exp_open_zones
, entry
, next
) {
3643 status
= nvme_bulk_proc_zone(ns
, zone
, proc_mask
, op_hndlr
,
3645 if (status
&& status
!= NVME_NO_COMPLETE
) {
3650 if (proc_mask
& NVME_PROC_FULL_ZONES
) {
3651 QTAILQ_FOREACH_SAFE(zone
, &ns
->full_zones
, entry
, next
) {
3652 status
= nvme_bulk_proc_zone(ns
, zone
, proc_mask
, op_hndlr
,
3654 if (status
&& status
!= NVME_NO_COMPLETE
) {
3660 if (proc_mask
& NVME_PROC_READ_ONLY_ZONES
) {
3661 for (i
= 0; i
< ns
->num_zones
; i
++, zone
++) {
3662 status
= nvme_bulk_proc_zone(ns
, zone
, proc_mask
, op_hndlr
,
3664 if (status
&& status
!= NVME_NO_COMPLETE
) {
3675 typedef struct NvmeZoneResetAIOCB
{
3684 } NvmeZoneResetAIOCB
;
3686 static void nvme_zone_reset_cancel(BlockAIOCB
*aiocb
)
3688 NvmeZoneResetAIOCB
*iocb
= container_of(aiocb
, NvmeZoneResetAIOCB
, common
);
3689 NvmeRequest
*req
= iocb
->req
;
3690 NvmeNamespace
*ns
= req
->ns
;
3692 iocb
->idx
= ns
->num_zones
;
3694 iocb
->ret
= -ECANCELED
;
3697 blk_aio_cancel_async(iocb
->aiocb
);
3702 static const AIOCBInfo nvme_zone_reset_aiocb_info
= {
3703 .aiocb_size
= sizeof(NvmeZoneResetAIOCB
),
3704 .cancel_async
= nvme_zone_reset_cancel
,
3707 static void nvme_zone_reset_cb(void *opaque
, int ret
);
3709 static void nvme_zone_reset_epilogue_cb(void *opaque
, int ret
)
3711 NvmeZoneResetAIOCB
*iocb
= opaque
;
3712 NvmeRequest
*req
= iocb
->req
;
3713 NvmeNamespace
*ns
= req
->ns
;
3717 if (ret
< 0 || iocb
->ret
< 0 || !ns
->lbaf
.ms
) {
3721 moff
= nvme_moff(ns
, iocb
->zone
->d
.zslba
);
3722 count
= nvme_m2b(ns
, ns
->zone_size
);
3724 iocb
->aiocb
= blk_aio_pwrite_zeroes(ns
->blkconf
.blk
, moff
, count
,
3726 nvme_zone_reset_cb
, iocb
);
3730 nvme_zone_reset_cb(iocb
, ret
);
3733 static void nvme_zone_reset_cb(void *opaque
, int ret
)
3735 NvmeZoneResetAIOCB
*iocb
= opaque
;
3736 NvmeRequest
*req
= iocb
->req
;
3737 NvmeNamespace
*ns
= req
->ns
;
3739 if (iocb
->ret
< 0) {
3741 } else if (ret
< 0) {
3747 nvme_zrm_reset(ns
, iocb
->zone
);
3754 while (iocb
->idx
< ns
->num_zones
) {
3755 NvmeZone
*zone
= &ns
->zone_array
[iocb
->idx
++];
3757 switch (nvme_get_zone_state(zone
)) {
3758 case NVME_ZONE_STATE_EMPTY
:
3765 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
3766 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
3767 case NVME_ZONE_STATE_CLOSED
:
3768 case NVME_ZONE_STATE_FULL
:
3776 trace_pci_nvme_zns_zone_reset(zone
->d
.zslba
);
3778 iocb
->aiocb
= blk_aio_pwrite_zeroes(ns
->blkconf
.blk
,
3779 nvme_l2b(ns
, zone
->d
.zslba
),
3780 nvme_l2b(ns
, ns
->zone_size
),
3782 nvme_zone_reset_epilogue_cb
,
3790 iocb
->common
.cb(iocb
->common
.opaque
, iocb
->ret
);
3791 qemu_aio_unref(iocb
);
3794 static uint16_t nvme_zone_mgmt_send_zrwa_flush(NvmeCtrl
*n
, NvmeZone
*zone
,
3795 uint64_t elba
, NvmeRequest
*req
)
3797 NvmeNamespace
*ns
= req
->ns
;
3798 uint16_t ozcs
= le16_to_cpu(ns
->id_ns_zoned
->ozcs
);
3799 uint64_t wp
= zone
->d
.wp
;
3800 uint32_t nlb
= elba
- wp
+ 1;
3804 if (!(ozcs
& NVME_ID_NS_ZONED_OZCS_ZRWASUP
)) {
3805 return NVME_INVALID_ZONE_OP
| NVME_DNR
;
3808 if (!(zone
->d
.za
& NVME_ZA_ZRWA_VALID
)) {
3809 return NVME_INVALID_FIELD
| NVME_DNR
;
3812 if (elba
< wp
|| elba
> wp
+ ns
->zns
.zrwas
) {
3813 return NVME_ZONE_BOUNDARY_ERROR
| NVME_DNR
;
3816 if (nlb
% ns
->zns
.zrwafg
) {
3817 return NVME_INVALID_FIELD
| NVME_DNR
;
3820 status
= nvme_zrm_auto(n
, ns
, zone
);
3827 nvme_advance_zone_wp(ns
, zone
, nlb
);
3829 return NVME_SUCCESS
;
3832 static uint16_t nvme_zone_mgmt_send(NvmeCtrl
*n
, NvmeRequest
*req
)
3834 NvmeZoneSendCmd
*cmd
= (NvmeZoneSendCmd
*)&req
->cmd
;
3835 NvmeNamespace
*ns
= req
->ns
;
3837 NvmeZoneResetAIOCB
*iocb
;
3840 uint32_t zone_idx
= 0;
3842 uint8_t action
= cmd
->zsa
;
3844 enum NvmeZoneProcessingMask proc_mask
= NVME_PROC_CURRENT_ZONE
;
3846 all
= cmd
->zsflags
& NVME_ZSFLAG_SELECT_ALL
;
3848 req
->status
= NVME_SUCCESS
;
3851 status
= nvme_get_mgmt_zone_slba_idx(ns
, &req
->cmd
, &slba
, &zone_idx
);
3857 zone
= &ns
->zone_array
[zone_idx
];
3858 if (slba
!= zone
->d
.zslba
&& action
!= NVME_ZONE_ACTION_ZRWA_FLUSH
) {
3859 trace_pci_nvme_err_unaligned_zone_cmd(action
, slba
, zone
->d
.zslba
);
3860 return NVME_INVALID_FIELD
| NVME_DNR
;
3865 case NVME_ZONE_ACTION_OPEN
:
3867 proc_mask
= NVME_PROC_CLOSED_ZONES
;
3869 trace_pci_nvme_open_zone(slba
, zone_idx
, all
);
3870 status
= nvme_do_zone_op(ns
, zone
, proc_mask
, nvme_open_zone
, req
);
3873 case NVME_ZONE_ACTION_CLOSE
:
3875 proc_mask
= NVME_PROC_OPENED_ZONES
;
3877 trace_pci_nvme_close_zone(slba
, zone_idx
, all
);
3878 status
= nvme_do_zone_op(ns
, zone
, proc_mask
, nvme_close_zone
, req
);
3881 case NVME_ZONE_ACTION_FINISH
:
3883 proc_mask
= NVME_PROC_OPENED_ZONES
| NVME_PROC_CLOSED_ZONES
;
3885 trace_pci_nvme_finish_zone(slba
, zone_idx
, all
);
3886 status
= nvme_do_zone_op(ns
, zone
, proc_mask
, nvme_finish_zone
, req
);
3889 case NVME_ZONE_ACTION_RESET
:
3890 trace_pci_nvme_reset_zone(slba
, zone_idx
, all
);
3892 iocb
= blk_aio_get(&nvme_zone_reset_aiocb_info
, ns
->blkconf
.blk
,
3898 iocb
->idx
= zone_idx
;
3901 req
->aiocb
= &iocb
->common
;
3902 nvme_zone_reset_cb(iocb
, 0);
3904 return NVME_NO_COMPLETE
;
3906 case NVME_ZONE_ACTION_OFFLINE
:
3908 proc_mask
= NVME_PROC_READ_ONLY_ZONES
;
3910 trace_pci_nvme_offline_zone(slba
, zone_idx
, all
);
3911 status
= nvme_do_zone_op(ns
, zone
, proc_mask
, nvme_offline_zone
, req
);
3914 case NVME_ZONE_ACTION_SET_ZD_EXT
:
3915 trace_pci_nvme_set_descriptor_extension(slba
, zone_idx
);
3916 if (all
|| !ns
->params
.zd_extension_size
) {
3917 return NVME_INVALID_FIELD
| NVME_DNR
;
3919 zd_ext
= nvme_get_zd_extension(ns
, zone_idx
);
3920 status
= nvme_h2c(n
, zd_ext
, ns
->params
.zd_extension_size
, req
);
3922 trace_pci_nvme_err_zd_extension_map_error(zone_idx
);
3926 status
= nvme_set_zd_ext(ns
, zone
);
3927 if (status
== NVME_SUCCESS
) {
3928 trace_pci_nvme_zd_extension_set(zone_idx
);
3933 case NVME_ZONE_ACTION_ZRWA_FLUSH
:
3935 return NVME_INVALID_FIELD
| NVME_DNR
;
3938 return nvme_zone_mgmt_send_zrwa_flush(n
, zone
, slba
, req
);
3941 trace_pci_nvme_err_invalid_mgmt_action(action
);
3942 status
= NVME_INVALID_FIELD
;
3945 if (status
== NVME_ZONE_INVAL_TRANSITION
) {
3946 trace_pci_nvme_err_invalid_zone_state_transition(action
, slba
,
3956 static bool nvme_zone_matches_filter(uint32_t zafs
, NvmeZone
*zl
)
3958 NvmeZoneState zs
= nvme_get_zone_state(zl
);
3961 case NVME_ZONE_REPORT_ALL
:
3963 case NVME_ZONE_REPORT_EMPTY
:
3964 return zs
== NVME_ZONE_STATE_EMPTY
;
3965 case NVME_ZONE_REPORT_IMPLICITLY_OPEN
:
3966 return zs
== NVME_ZONE_STATE_IMPLICITLY_OPEN
;
3967 case NVME_ZONE_REPORT_EXPLICITLY_OPEN
:
3968 return zs
== NVME_ZONE_STATE_EXPLICITLY_OPEN
;
3969 case NVME_ZONE_REPORT_CLOSED
:
3970 return zs
== NVME_ZONE_STATE_CLOSED
;
3971 case NVME_ZONE_REPORT_FULL
:
3972 return zs
== NVME_ZONE_STATE_FULL
;
3973 case NVME_ZONE_REPORT_READ_ONLY
:
3974 return zs
== NVME_ZONE_STATE_READ_ONLY
;
3975 case NVME_ZONE_REPORT_OFFLINE
:
3976 return zs
== NVME_ZONE_STATE_OFFLINE
;
3982 static uint16_t nvme_zone_mgmt_recv(NvmeCtrl
*n
, NvmeRequest
*req
)
3984 NvmeCmd
*cmd
= (NvmeCmd
*)&req
->cmd
;
3985 NvmeNamespace
*ns
= req
->ns
;
3986 /* cdw12 is zero-based number of dwords to return. Convert to bytes */
3987 uint32_t data_size
= (le32_to_cpu(cmd
->cdw12
) + 1) << 2;
3988 uint32_t dw13
= le32_to_cpu(cmd
->cdw13
);
3989 uint32_t zone_idx
, zra
, zrasf
, partial
;
3990 uint64_t max_zones
, nr_zones
= 0;
3995 NvmeZoneReportHeader
*header
;
3997 size_t zone_entry_sz
;
4000 req
->status
= NVME_SUCCESS
;
4002 status
= nvme_get_mgmt_zone_slba_idx(ns
, cmd
, &slba
, &zone_idx
);
4008 if (zra
!= NVME_ZONE_REPORT
&& zra
!= NVME_ZONE_REPORT_EXTENDED
) {
4009 return NVME_INVALID_FIELD
| NVME_DNR
;
4011 if (zra
== NVME_ZONE_REPORT_EXTENDED
&& !ns
->params
.zd_extension_size
) {
4012 return NVME_INVALID_FIELD
| NVME_DNR
;
4015 zrasf
= (dw13
>> 8) & 0xff;
4016 if (zrasf
> NVME_ZONE_REPORT_OFFLINE
) {
4017 return NVME_INVALID_FIELD
| NVME_DNR
;
4020 if (data_size
< sizeof(NvmeZoneReportHeader
)) {
4021 return NVME_INVALID_FIELD
| NVME_DNR
;
4024 status
= nvme_check_mdts(n
, data_size
);
4029 partial
= (dw13
>> 16) & 0x01;
4031 zone_entry_sz
= sizeof(NvmeZoneDescr
);
4032 if (zra
== NVME_ZONE_REPORT_EXTENDED
) {
4033 zone_entry_sz
+= ns
->params
.zd_extension_size
;
4036 max_zones
= (data_size
- sizeof(NvmeZoneReportHeader
)) / zone_entry_sz
;
4037 buf
= g_malloc0(data_size
);
4039 zone
= &ns
->zone_array
[zone_idx
];
4040 for (i
= zone_idx
; i
< ns
->num_zones
; i
++) {
4041 if (partial
&& nr_zones
>= max_zones
) {
4044 if (nvme_zone_matches_filter(zrasf
, zone
++)) {
4049 header
->nr_zones
= cpu_to_le64(nr_zones
);
4051 buf_p
= buf
+ sizeof(NvmeZoneReportHeader
);
4052 for (; zone_idx
< ns
->num_zones
&& max_zones
> 0; zone_idx
++) {
4053 zone
= &ns
->zone_array
[zone_idx
];
4054 if (nvme_zone_matches_filter(zrasf
, zone
)) {
4056 buf_p
+= sizeof(NvmeZoneDescr
);
4060 z
->zcap
= cpu_to_le64(zone
->d
.zcap
);
4061 z
->zslba
= cpu_to_le64(zone
->d
.zslba
);
4064 if (nvme_wp_is_valid(zone
)) {
4065 z
->wp
= cpu_to_le64(zone
->d
.wp
);
4067 z
->wp
= cpu_to_le64(~0ULL);
4070 if (zra
== NVME_ZONE_REPORT_EXTENDED
) {
4071 if (zone
->d
.za
& NVME_ZA_ZD_EXT_VALID
) {
4072 memcpy(buf_p
, nvme_get_zd_extension(ns
, zone_idx
),
4073 ns
->params
.zd_extension_size
);
4075 buf_p
+= ns
->params
.zd_extension_size
;
4082 status
= nvme_c2h(n
, (uint8_t *)buf
, data_size
, req
);
4089 static uint16_t nvme_io_cmd(NvmeCtrl
*n
, NvmeRequest
*req
)
4092 uint32_t nsid
= le32_to_cpu(req
->cmd
.nsid
);
4094 trace_pci_nvme_io_cmd(nvme_cid(req
), nsid
, nvme_sqid(req
),
4095 req
->cmd
.opcode
, nvme_io_opc_str(req
->cmd
.opcode
));
4097 if (!nvme_nsid_valid(n
, nsid
)) {
4098 return NVME_INVALID_NSID
| NVME_DNR
;
4102 * In the base NVM command set, Flush may apply to all namespaces
4103 * (indicated by NSID being set to FFFFFFFFh). But if that feature is used
4104 * along with TP 4056 (Namespace Types), it may be pretty screwed up.
4106 * If NSID is indeed set to FFFFFFFFh, we simply cannot associate the
4107 * opcode with a specific command since we cannot determine a unique I/O
4108 * command set. Opcode 0h could have any other meaning than something
4109 * equivalent to flushing and say it DOES have completely different
4110 * semantics in some other command set - does an NSID of FFFFFFFFh then
4111 * mean "for all namespaces, apply whatever command set specific command
4112 * that uses the 0h opcode?" Or does it mean "for all namespaces, apply
4113 * whatever command that uses the 0h opcode if, and only if, it allows NSID
4116 * Anyway (and luckily), for now, we do not care about this since the
4117 * device only supports namespace types that includes the NVM Flush command
4118 * (NVM and Zoned), so always do an NVM Flush.
4120 if (req
->cmd
.opcode
== NVME_CMD_FLUSH
) {
4121 return nvme_flush(n
, req
);
4124 ns
= nvme_ns(n
, nsid
);
4125 if (unlikely(!ns
)) {
4126 return NVME_INVALID_FIELD
| NVME_DNR
;
4129 if (!(ns
->iocs
[req
->cmd
.opcode
] & NVME_CMD_EFF_CSUPP
)) {
4130 trace_pci_nvme_err_invalid_opc(req
->cmd
.opcode
);
4131 return NVME_INVALID_OPCODE
| NVME_DNR
;
4138 if (NVME_CMD_FLAGS_FUSE(req
->cmd
.flags
)) {
4139 return NVME_INVALID_FIELD
;
4144 switch (req
->cmd
.opcode
) {
4145 case NVME_CMD_WRITE_ZEROES
:
4146 return nvme_write_zeroes(n
, req
);
4147 case NVME_CMD_ZONE_APPEND
:
4148 return nvme_zone_append(n
, req
);
4149 case NVME_CMD_WRITE
:
4150 return nvme_write(n
, req
);
4152 return nvme_read(n
, req
);
4153 case NVME_CMD_COMPARE
:
4154 return nvme_compare(n
, req
);
4156 return nvme_dsm(n
, req
);
4157 case NVME_CMD_VERIFY
:
4158 return nvme_verify(n
, req
);
4160 return nvme_copy(n
, req
);
4161 case NVME_CMD_ZONE_MGMT_SEND
:
4162 return nvme_zone_mgmt_send(n
, req
);
4163 case NVME_CMD_ZONE_MGMT_RECV
:
4164 return nvme_zone_mgmt_recv(n
, req
);
4169 return NVME_INVALID_OPCODE
| NVME_DNR
;
4172 static void nvme_cq_notifier(EventNotifier
*e
)
4174 NvmeCQueue
*cq
= container_of(e
, NvmeCQueue
, notifier
);
4175 NvmeCtrl
*n
= cq
->ctrl
;
4177 if (!event_notifier_test_and_clear(e
)) {
4181 nvme_update_cq_head(cq
);
4183 if (cq
->tail
== cq
->head
) {
4184 if (cq
->irq_enabled
) {
4188 nvme_irq_deassert(n
, cq
);
4191 qemu_bh_schedule(cq
->bh
);
4194 static int nvme_init_cq_ioeventfd(NvmeCQueue
*cq
)
4196 NvmeCtrl
*n
= cq
->ctrl
;
4197 uint16_t offset
= (cq
->cqid
<< 3) + (1 << 2);
4200 ret
= event_notifier_init(&cq
->notifier
, 0);
4205 event_notifier_set_handler(&cq
->notifier
, nvme_cq_notifier
);
4206 memory_region_add_eventfd(&n
->iomem
,
4207 0x1000 + offset
, 4, false, 0, &cq
->notifier
);
4212 static void nvme_sq_notifier(EventNotifier
*e
)
4214 NvmeSQueue
*sq
= container_of(e
, NvmeSQueue
, notifier
);
4216 if (!event_notifier_test_and_clear(e
)) {
4220 nvme_process_sq(sq
);
4223 static int nvme_init_sq_ioeventfd(NvmeSQueue
*sq
)
4225 NvmeCtrl
*n
= sq
->ctrl
;
4226 uint16_t offset
= sq
->sqid
<< 3;
4229 ret
= event_notifier_init(&sq
->notifier
, 0);
4234 event_notifier_set_handler(&sq
->notifier
, nvme_sq_notifier
);
4235 memory_region_add_eventfd(&n
->iomem
,
4236 0x1000 + offset
, 4, false, 0, &sq
->notifier
);
4241 static void nvme_free_sq(NvmeSQueue
*sq
, NvmeCtrl
*n
)
4243 uint16_t offset
= sq
->sqid
<< 3;
4245 n
->sq
[sq
->sqid
] = NULL
;
4246 qemu_bh_delete(sq
->bh
);
4247 if (sq
->ioeventfd_enabled
) {
4248 memory_region_del_eventfd(&n
->iomem
,
4249 0x1000 + offset
, 4, false, 0, &sq
->notifier
);
4250 event_notifier_set_handler(&sq
->notifier
, NULL
);
4251 event_notifier_cleanup(&sq
->notifier
);
4259 static uint16_t nvme_del_sq(NvmeCtrl
*n
, NvmeRequest
*req
)
4261 NvmeDeleteQ
*c
= (NvmeDeleteQ
*)&req
->cmd
;
4262 NvmeRequest
*r
, *next
;
4265 uint16_t qid
= le16_to_cpu(c
->qid
);
4267 if (unlikely(!qid
|| nvme_check_sqid(n
, qid
))) {
4268 trace_pci_nvme_err_invalid_del_sq(qid
);
4269 return NVME_INVALID_QID
| NVME_DNR
;
4272 trace_pci_nvme_del_sq(qid
);
4275 while (!QTAILQ_EMPTY(&sq
->out_req_list
)) {
4276 r
= QTAILQ_FIRST(&sq
->out_req_list
);
4278 blk_aio_cancel(r
->aiocb
);
4281 assert(QTAILQ_EMPTY(&sq
->out_req_list
));
4283 if (!nvme_check_cqid(n
, sq
->cqid
)) {
4284 cq
= n
->cq
[sq
->cqid
];
4285 QTAILQ_REMOVE(&cq
->sq_list
, sq
, entry
);
4288 QTAILQ_FOREACH_SAFE(r
, &cq
->req_list
, entry
, next
) {
4290 QTAILQ_REMOVE(&cq
->req_list
, r
, entry
);
4291 QTAILQ_INSERT_TAIL(&sq
->req_list
, r
, entry
);
4296 nvme_free_sq(sq
, n
);
4297 return NVME_SUCCESS
;
4300 static void nvme_init_sq(NvmeSQueue
*sq
, NvmeCtrl
*n
, uint64_t dma_addr
,
4301 uint16_t sqid
, uint16_t cqid
, uint16_t size
)
4307 sq
->dma_addr
= dma_addr
;
4311 sq
->head
= sq
->tail
= 0;
4312 sq
->io_req
= g_new0(NvmeRequest
, sq
->size
);
4314 QTAILQ_INIT(&sq
->req_list
);
4315 QTAILQ_INIT(&sq
->out_req_list
);
4316 for (i
= 0; i
< sq
->size
; i
++) {
4317 sq
->io_req
[i
].sq
= sq
;
4318 QTAILQ_INSERT_TAIL(&(sq
->req_list
), &sq
->io_req
[i
], entry
);
4321 sq
->bh
= qemu_bh_new(nvme_process_sq
, sq
);
4323 if (n
->dbbuf_enabled
) {
4324 sq
->db_addr
= n
->dbbuf_dbs
+ (sqid
<< 3);
4325 sq
->ei_addr
= n
->dbbuf_eis
+ (sqid
<< 3);
4327 if (n
->params
.ioeventfd
&& sq
->sqid
!= 0) {
4328 if (!nvme_init_sq_ioeventfd(sq
)) {
4329 sq
->ioeventfd_enabled
= true;
4334 assert(n
->cq
[cqid
]);
4336 QTAILQ_INSERT_TAIL(&(cq
->sq_list
), sq
, entry
);
4340 static uint16_t nvme_create_sq(NvmeCtrl
*n
, NvmeRequest
*req
)
4343 NvmeCreateSq
*c
= (NvmeCreateSq
*)&req
->cmd
;
4345 uint16_t cqid
= le16_to_cpu(c
->cqid
);
4346 uint16_t sqid
= le16_to_cpu(c
->sqid
);
4347 uint16_t qsize
= le16_to_cpu(c
->qsize
);
4348 uint16_t qflags
= le16_to_cpu(c
->sq_flags
);
4349 uint64_t prp1
= le64_to_cpu(c
->prp1
);
4351 trace_pci_nvme_create_sq(prp1
, sqid
, cqid
, qsize
, qflags
);
4353 if (unlikely(!cqid
|| nvme_check_cqid(n
, cqid
))) {
4354 trace_pci_nvme_err_invalid_create_sq_cqid(cqid
);
4355 return NVME_INVALID_CQID
| NVME_DNR
;
4357 if (unlikely(!sqid
|| sqid
> n
->conf_ioqpairs
|| n
->sq
[sqid
] != NULL
)) {
4358 trace_pci_nvme_err_invalid_create_sq_sqid(sqid
);
4359 return NVME_INVALID_QID
| NVME_DNR
;
4361 if (unlikely(!qsize
|| qsize
> NVME_CAP_MQES(ldq_le_p(&n
->bar
.cap
)))) {
4362 trace_pci_nvme_err_invalid_create_sq_size(qsize
);
4363 return NVME_MAX_QSIZE_EXCEEDED
| NVME_DNR
;
4365 if (unlikely(prp1
& (n
->page_size
- 1))) {
4366 trace_pci_nvme_err_invalid_create_sq_addr(prp1
);
4367 return NVME_INVALID_PRP_OFFSET
| NVME_DNR
;
4369 if (unlikely(!(NVME_SQ_FLAGS_PC(qflags
)))) {
4370 trace_pci_nvme_err_invalid_create_sq_qflags(NVME_SQ_FLAGS_PC(qflags
));
4371 return NVME_INVALID_FIELD
| NVME_DNR
;
4373 sq
= g_malloc0(sizeof(*sq
));
4374 nvme_init_sq(sq
, n
, prp1
, sqid
, cqid
, qsize
+ 1);
4375 return NVME_SUCCESS
;
4379 uint64_t units_read
;
4380 uint64_t units_written
;
4381 uint64_t read_commands
;
4382 uint64_t write_commands
;
4385 static void nvme_set_blk_stats(NvmeNamespace
*ns
, struct nvme_stats
*stats
)
4387 BlockAcctStats
*s
= blk_get_stats(ns
->blkconf
.blk
);
4389 stats
->units_read
+= s
->nr_bytes
[BLOCK_ACCT_READ
] >> BDRV_SECTOR_BITS
;
4390 stats
->units_written
+= s
->nr_bytes
[BLOCK_ACCT_WRITE
] >> BDRV_SECTOR_BITS
;
4391 stats
->read_commands
+= s
->nr_ops
[BLOCK_ACCT_READ
];
4392 stats
->write_commands
+= s
->nr_ops
[BLOCK_ACCT_WRITE
];
4395 static uint16_t nvme_smart_info(NvmeCtrl
*n
, uint8_t rae
, uint32_t buf_len
,
4396 uint64_t off
, NvmeRequest
*req
)
4398 uint32_t nsid
= le32_to_cpu(req
->cmd
.nsid
);
4399 struct nvme_stats stats
= { 0 };
4400 NvmeSmartLog smart
= { 0 };
4405 if (off
>= sizeof(smart
)) {
4406 return NVME_INVALID_FIELD
| NVME_DNR
;
4409 if (nsid
!= 0xffffffff) {
4410 ns
= nvme_ns(n
, nsid
);
4412 return NVME_INVALID_NSID
| NVME_DNR
;
4414 nvme_set_blk_stats(ns
, &stats
);
4418 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
4423 nvme_set_blk_stats(ns
, &stats
);
4427 trans_len
= MIN(sizeof(smart
) - off
, buf_len
);
4428 smart
.critical_warning
= n
->smart_critical_warning
;
4430 smart
.data_units_read
[0] = cpu_to_le64(DIV_ROUND_UP(stats
.units_read
,
4432 smart
.data_units_written
[0] = cpu_to_le64(DIV_ROUND_UP(stats
.units_written
,
4434 smart
.host_read_commands
[0] = cpu_to_le64(stats
.read_commands
);
4435 smart
.host_write_commands
[0] = cpu_to_le64(stats
.write_commands
);
4437 smart
.temperature
= cpu_to_le16(n
->temperature
);
4439 if ((n
->temperature
>= n
->features
.temp_thresh_hi
) ||
4440 (n
->temperature
<= n
->features
.temp_thresh_low
)) {
4441 smart
.critical_warning
|= NVME_SMART_TEMPERATURE
;
4444 current_ms
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
);
4445 smart
.power_on_hours
[0] =
4446 cpu_to_le64((((current_ms
- n
->starttime_ms
) / 1000) / 60) / 60);
4449 nvme_clear_events(n
, NVME_AER_TYPE_SMART
);
4452 return nvme_c2h(n
, (uint8_t *) &smart
+ off
, trans_len
, req
);
4455 static uint16_t nvme_fw_log_info(NvmeCtrl
*n
, uint32_t buf_len
, uint64_t off
,
4459 NvmeFwSlotInfoLog fw_log
= {
4463 if (off
>= sizeof(fw_log
)) {
4464 return NVME_INVALID_FIELD
| NVME_DNR
;
4467 strpadcpy((char *)&fw_log
.frs1
, sizeof(fw_log
.frs1
), "1.0", ' ');
4468 trans_len
= MIN(sizeof(fw_log
) - off
, buf_len
);
4470 return nvme_c2h(n
, (uint8_t *) &fw_log
+ off
, trans_len
, req
);
4473 static uint16_t nvme_error_info(NvmeCtrl
*n
, uint8_t rae
, uint32_t buf_len
,
4474 uint64_t off
, NvmeRequest
*req
)
4477 NvmeErrorLog errlog
;
4479 if (off
>= sizeof(errlog
)) {
4480 return NVME_INVALID_FIELD
| NVME_DNR
;
4484 nvme_clear_events(n
, NVME_AER_TYPE_ERROR
);
4487 memset(&errlog
, 0x0, sizeof(errlog
));
4488 trans_len
= MIN(sizeof(errlog
) - off
, buf_len
);
4490 return nvme_c2h(n
, (uint8_t *)&errlog
, trans_len
, req
);
4493 static uint16_t nvme_changed_nslist(NvmeCtrl
*n
, uint8_t rae
, uint32_t buf_len
,
4494 uint64_t off
, NvmeRequest
*req
)
4496 uint32_t nslist
[1024];
4501 if (off
>= sizeof(nslist
)) {
4502 trace_pci_nvme_err_invalid_log_page_offset(off
, sizeof(nslist
));
4503 return NVME_INVALID_FIELD
| NVME_DNR
;
4506 memset(nslist
, 0x0, sizeof(nslist
));
4507 trans_len
= MIN(sizeof(nslist
) - off
, buf_len
);
4509 while ((nsid
= find_first_bit(n
->changed_nsids
, NVME_CHANGED_NSID_SIZE
)) !=
4510 NVME_CHANGED_NSID_SIZE
) {
4512 * If more than 1024 namespaces, the first entry in the log page should
4513 * be set to FFFFFFFFh and the others to 0 as spec.
4515 if (i
== ARRAY_SIZE(nslist
)) {
4516 memset(nslist
, 0x0, sizeof(nslist
));
4517 nslist
[0] = 0xffffffff;
4522 clear_bit(nsid
, n
->changed_nsids
);
4526 * Remove all the remaining list entries in case returns directly due to
4527 * more than 1024 namespaces.
4529 if (nslist
[0] == 0xffffffff) {
4530 bitmap_zero(n
->changed_nsids
, NVME_CHANGED_NSID_SIZE
);
4534 nvme_clear_events(n
, NVME_AER_TYPE_NOTICE
);
4537 return nvme_c2h(n
, ((uint8_t *)nslist
) + off
, trans_len
, req
);
4540 static uint16_t nvme_cmd_effects(NvmeCtrl
*n
, uint8_t csi
, uint32_t buf_len
,
4541 uint64_t off
, NvmeRequest
*req
)
4543 NvmeEffectsLog log
= {};
4544 const uint32_t *src_iocs
= NULL
;
4547 if (off
>= sizeof(log
)) {
4548 trace_pci_nvme_err_invalid_log_page_offset(off
, sizeof(log
));
4549 return NVME_INVALID_FIELD
| NVME_DNR
;
4552 switch (NVME_CC_CSS(ldl_le_p(&n
->bar
.cc
))) {
4553 case NVME_CC_CSS_NVM
:
4554 src_iocs
= nvme_cse_iocs_nvm
;
4556 case NVME_CC_CSS_ADMIN_ONLY
:
4558 case NVME_CC_CSS_CSI
:
4561 src_iocs
= nvme_cse_iocs_nvm
;
4563 case NVME_CSI_ZONED
:
4564 src_iocs
= nvme_cse_iocs_zoned
;
4569 memcpy(log
.acs
, nvme_cse_acs
, sizeof(nvme_cse_acs
));
4572 memcpy(log
.iocs
, src_iocs
, sizeof(log
.iocs
));
4575 trans_len
= MIN(sizeof(log
) - off
, buf_len
);
4577 return nvme_c2h(n
, ((uint8_t *)&log
) + off
, trans_len
, req
);
4580 static uint16_t nvme_get_log(NvmeCtrl
*n
, NvmeRequest
*req
)
4582 NvmeCmd
*cmd
= &req
->cmd
;
4584 uint32_t dw10
= le32_to_cpu(cmd
->cdw10
);
4585 uint32_t dw11
= le32_to_cpu(cmd
->cdw11
);
4586 uint32_t dw12
= le32_to_cpu(cmd
->cdw12
);
4587 uint32_t dw13
= le32_to_cpu(cmd
->cdw13
);
4588 uint8_t lid
= dw10
& 0xff;
4589 uint8_t lsp
= (dw10
>> 8) & 0xf;
4590 uint8_t rae
= (dw10
>> 15) & 0x1;
4591 uint8_t csi
= le32_to_cpu(cmd
->cdw14
) >> 24;
4592 uint32_t numdl
, numdu
;
4593 uint64_t off
, lpol
, lpou
;
4597 numdl
= (dw10
>> 16);
4598 numdu
= (dw11
& 0xffff);
4602 len
= (((numdu
<< 16) | numdl
) + 1) << 2;
4603 off
= (lpou
<< 32ULL) | lpol
;
4606 return NVME_INVALID_FIELD
| NVME_DNR
;
4609 trace_pci_nvme_get_log(nvme_cid(req
), lid
, lsp
, rae
, len
, off
);
4611 status
= nvme_check_mdts(n
, len
);
4617 case NVME_LOG_ERROR_INFO
:
4618 return nvme_error_info(n
, rae
, len
, off
, req
);
4619 case NVME_LOG_SMART_INFO
:
4620 return nvme_smart_info(n
, rae
, len
, off
, req
);
4621 case NVME_LOG_FW_SLOT_INFO
:
4622 return nvme_fw_log_info(n
, len
, off
, req
);
4623 case NVME_LOG_CHANGED_NSLIST
:
4624 return nvme_changed_nslist(n
, rae
, len
, off
, req
);
4625 case NVME_LOG_CMD_EFFECTS
:
4626 return nvme_cmd_effects(n
, csi
, len
, off
, req
);
4628 trace_pci_nvme_err_invalid_log_page(nvme_cid(req
), lid
);
4629 return NVME_INVALID_FIELD
| NVME_DNR
;
4633 static void nvme_free_cq(NvmeCQueue
*cq
, NvmeCtrl
*n
)
4635 PCIDevice
*pci
= PCI_DEVICE(n
);
4636 uint16_t offset
= (cq
->cqid
<< 3) + (1 << 2);
4638 n
->cq
[cq
->cqid
] = NULL
;
4639 qemu_bh_delete(cq
->bh
);
4640 if (cq
->ioeventfd_enabled
) {
4641 memory_region_del_eventfd(&n
->iomem
,
4642 0x1000 + offset
, 4, false, 0, &cq
->notifier
);
4643 event_notifier_set_handler(&cq
->notifier
, NULL
);
4644 event_notifier_cleanup(&cq
->notifier
);
4646 if (msix_enabled(pci
)) {
4647 msix_vector_unuse(pci
, cq
->vector
);
4654 static uint16_t nvme_del_cq(NvmeCtrl
*n
, NvmeRequest
*req
)
4656 NvmeDeleteQ
*c
= (NvmeDeleteQ
*)&req
->cmd
;
4658 uint16_t qid
= le16_to_cpu(c
->qid
);
4660 if (unlikely(!qid
|| nvme_check_cqid(n
, qid
))) {
4661 trace_pci_nvme_err_invalid_del_cq_cqid(qid
);
4662 return NVME_INVALID_CQID
| NVME_DNR
;
4666 if (unlikely(!QTAILQ_EMPTY(&cq
->sq_list
))) {
4667 trace_pci_nvme_err_invalid_del_cq_notempty(qid
);
4668 return NVME_INVALID_QUEUE_DEL
;
4671 if (cq
->irq_enabled
&& cq
->tail
!= cq
->head
) {
4675 nvme_irq_deassert(n
, cq
);
4676 trace_pci_nvme_del_cq(qid
);
4677 nvme_free_cq(cq
, n
);
4678 return NVME_SUCCESS
;
4681 static void nvme_init_cq(NvmeCQueue
*cq
, NvmeCtrl
*n
, uint64_t dma_addr
,
4682 uint16_t cqid
, uint16_t vector
, uint16_t size
,
4683 uint16_t irq_enabled
)
4685 PCIDevice
*pci
= PCI_DEVICE(n
);
4687 if (msix_enabled(pci
)) {
4688 msix_vector_use(pci
, vector
);
4693 cq
->dma_addr
= dma_addr
;
4695 cq
->irq_enabled
= irq_enabled
;
4696 cq
->vector
= vector
;
4697 cq
->head
= cq
->tail
= 0;
4698 QTAILQ_INIT(&cq
->req_list
);
4699 QTAILQ_INIT(&cq
->sq_list
);
4700 if (n
->dbbuf_enabled
) {
4701 cq
->db_addr
= n
->dbbuf_dbs
+ (cqid
<< 3) + (1 << 2);
4702 cq
->ei_addr
= n
->dbbuf_eis
+ (cqid
<< 3) + (1 << 2);
4704 if (n
->params
.ioeventfd
&& cqid
!= 0) {
4705 if (!nvme_init_cq_ioeventfd(cq
)) {
4706 cq
->ioeventfd_enabled
= true;
4711 cq
->bh
= qemu_bh_new(nvme_post_cqes
, cq
);
4714 static uint16_t nvme_create_cq(NvmeCtrl
*n
, NvmeRequest
*req
)
4717 NvmeCreateCq
*c
= (NvmeCreateCq
*)&req
->cmd
;
4718 uint16_t cqid
= le16_to_cpu(c
->cqid
);
4719 uint16_t vector
= le16_to_cpu(c
->irq_vector
);
4720 uint16_t qsize
= le16_to_cpu(c
->qsize
);
4721 uint16_t qflags
= le16_to_cpu(c
->cq_flags
);
4722 uint64_t prp1
= le64_to_cpu(c
->prp1
);
4724 trace_pci_nvme_create_cq(prp1
, cqid
, vector
, qsize
, qflags
,
4725 NVME_CQ_FLAGS_IEN(qflags
) != 0);
4727 if (unlikely(!cqid
|| cqid
> n
->conf_ioqpairs
|| n
->cq
[cqid
] != NULL
)) {
4728 trace_pci_nvme_err_invalid_create_cq_cqid(cqid
);
4729 return NVME_INVALID_QID
| NVME_DNR
;
4731 if (unlikely(!qsize
|| qsize
> NVME_CAP_MQES(ldq_le_p(&n
->bar
.cap
)))) {
4732 trace_pci_nvme_err_invalid_create_cq_size(qsize
);
4733 return NVME_MAX_QSIZE_EXCEEDED
| NVME_DNR
;
4735 if (unlikely(prp1
& (n
->page_size
- 1))) {
4736 trace_pci_nvme_err_invalid_create_cq_addr(prp1
);
4737 return NVME_INVALID_PRP_OFFSET
| NVME_DNR
;
4739 if (unlikely(!msix_enabled(PCI_DEVICE(n
)) && vector
)) {
4740 trace_pci_nvme_err_invalid_create_cq_vector(vector
);
4741 return NVME_INVALID_IRQ_VECTOR
| NVME_DNR
;
4743 if (unlikely(vector
>= n
->conf_msix_qsize
)) {
4744 trace_pci_nvme_err_invalid_create_cq_vector(vector
);
4745 return NVME_INVALID_IRQ_VECTOR
| NVME_DNR
;
4747 if (unlikely(!(NVME_CQ_FLAGS_PC(qflags
)))) {
4748 trace_pci_nvme_err_invalid_create_cq_qflags(NVME_CQ_FLAGS_PC(qflags
));
4749 return NVME_INVALID_FIELD
| NVME_DNR
;
4752 cq
= g_malloc0(sizeof(*cq
));
4753 nvme_init_cq(cq
, n
, prp1
, cqid
, vector
, qsize
+ 1,
4754 NVME_CQ_FLAGS_IEN(qflags
));
4757 * It is only required to set qs_created when creating a completion queue;
4758 * creating a submission queue without a matching completion queue will
4761 n
->qs_created
= true;
4762 return NVME_SUCCESS
;
4765 static uint16_t nvme_rpt_empty_id_struct(NvmeCtrl
*n
, NvmeRequest
*req
)
4767 uint8_t id
[NVME_IDENTIFY_DATA_SIZE
] = {};
4769 return nvme_c2h(n
, id
, sizeof(id
), req
);
4772 static uint16_t nvme_identify_ctrl(NvmeCtrl
*n
, NvmeRequest
*req
)
4774 trace_pci_nvme_identify_ctrl();
4776 return nvme_c2h(n
, (uint8_t *)&n
->id_ctrl
, sizeof(n
->id_ctrl
), req
);
4779 static uint16_t nvme_identify_ctrl_csi(NvmeCtrl
*n
, NvmeRequest
*req
)
4781 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
4782 uint8_t id
[NVME_IDENTIFY_DATA_SIZE
] = {};
4783 NvmeIdCtrlNvm
*id_nvm
= (NvmeIdCtrlNvm
*)&id
;
4785 trace_pci_nvme_identify_ctrl_csi(c
->csi
);
4789 id_nvm
->vsl
= n
->params
.vsl
;
4790 id_nvm
->dmrsl
= cpu_to_le32(n
->dmrsl
);
4793 case NVME_CSI_ZONED
:
4794 ((NvmeIdCtrlZoned
*)&id
)->zasl
= n
->params
.zasl
;
4798 return NVME_INVALID_FIELD
| NVME_DNR
;
4801 return nvme_c2h(n
, id
, sizeof(id
), req
);
4804 static uint16_t nvme_identify_ns(NvmeCtrl
*n
, NvmeRequest
*req
, bool active
)
4807 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
4808 uint32_t nsid
= le32_to_cpu(c
->nsid
);
4810 trace_pci_nvme_identify_ns(nsid
);
4812 if (!nvme_nsid_valid(n
, nsid
) || nsid
== NVME_NSID_BROADCAST
) {
4813 return NVME_INVALID_NSID
| NVME_DNR
;
4816 ns
= nvme_ns(n
, nsid
);
4817 if (unlikely(!ns
)) {
4819 ns
= nvme_subsys_ns(n
->subsys
, nsid
);
4821 return nvme_rpt_empty_id_struct(n
, req
);
4824 return nvme_rpt_empty_id_struct(n
, req
);
4828 if (active
|| ns
->csi
== NVME_CSI_NVM
) {
4829 return nvme_c2h(n
, (uint8_t *)&ns
->id_ns
, sizeof(NvmeIdNs
), req
);
4832 return NVME_INVALID_CMD_SET
| NVME_DNR
;
4835 static uint16_t nvme_identify_ctrl_list(NvmeCtrl
*n
, NvmeRequest
*req
,
4838 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
4839 uint32_t nsid
= le32_to_cpu(c
->nsid
);
4840 uint16_t min_id
= le16_to_cpu(c
->ctrlid
);
4841 uint16_t list
[NVME_CONTROLLER_LIST_SIZE
] = {};
4842 uint16_t *ids
= &list
[1];
4845 int cntlid
, nr_ids
= 0;
4847 trace_pci_nvme_identify_ctrl_list(c
->cns
, min_id
);
4850 return NVME_INVALID_FIELD
| NVME_DNR
;
4854 if (nsid
== NVME_NSID_BROADCAST
) {
4855 return NVME_INVALID_FIELD
| NVME_DNR
;
4858 ns
= nvme_subsys_ns(n
->subsys
, nsid
);
4860 return NVME_INVALID_FIELD
| NVME_DNR
;
4864 for (cntlid
= min_id
; cntlid
< ARRAY_SIZE(n
->subsys
->ctrls
); cntlid
++) {
4865 ctrl
= nvme_subsys_ctrl(n
->subsys
, cntlid
);
4870 if (attached
&& !nvme_ns(ctrl
, nsid
)) {
4874 ids
[nr_ids
++] = cntlid
;
4879 return nvme_c2h(n
, (uint8_t *)list
, sizeof(list
), req
);
4882 static uint16_t nvme_identify_pri_ctrl_cap(NvmeCtrl
*n
, NvmeRequest
*req
)
4884 trace_pci_nvme_identify_pri_ctrl_cap(le16_to_cpu(n
->pri_ctrl_cap
.cntlid
));
4886 return nvme_c2h(n
, (uint8_t *)&n
->pri_ctrl_cap
,
4887 sizeof(NvmePriCtrlCap
), req
);
4890 static uint16_t nvme_identify_sec_ctrl_list(NvmeCtrl
*n
, NvmeRequest
*req
)
4892 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
4893 uint16_t pri_ctrl_id
= le16_to_cpu(n
->pri_ctrl_cap
.cntlid
);
4894 uint16_t min_id
= le16_to_cpu(c
->ctrlid
);
4895 uint8_t num_sec_ctrl
= n
->sec_ctrl_list
.numcntl
;
4896 NvmeSecCtrlList list
= {0};
4899 for (i
= 0; i
< num_sec_ctrl
; i
++) {
4900 if (n
->sec_ctrl_list
.sec
[i
].scid
>= min_id
) {
4901 list
.numcntl
= num_sec_ctrl
- i
;
4902 memcpy(&list
.sec
, n
->sec_ctrl_list
.sec
+ i
,
4903 list
.numcntl
* sizeof(NvmeSecCtrlEntry
));
4908 trace_pci_nvme_identify_sec_ctrl_list(pri_ctrl_id
, list
.numcntl
);
4910 return nvme_c2h(n
, (uint8_t *)&list
, sizeof(list
), req
);
4913 static uint16_t nvme_identify_ns_csi(NvmeCtrl
*n
, NvmeRequest
*req
,
4917 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
4918 uint32_t nsid
= le32_to_cpu(c
->nsid
);
4920 trace_pci_nvme_identify_ns_csi(nsid
, c
->csi
);
4922 if (!nvme_nsid_valid(n
, nsid
) || nsid
== NVME_NSID_BROADCAST
) {
4923 return NVME_INVALID_NSID
| NVME_DNR
;
4926 ns
= nvme_ns(n
, nsid
);
4927 if (unlikely(!ns
)) {
4929 ns
= nvme_subsys_ns(n
->subsys
, nsid
);
4931 return nvme_rpt_empty_id_struct(n
, req
);
4934 return nvme_rpt_empty_id_struct(n
, req
);
4938 if (c
->csi
== NVME_CSI_NVM
) {
4939 return nvme_c2h(n
, (uint8_t *)&ns
->id_ns_nvm
, sizeof(NvmeIdNsNvm
),
4941 } else if (c
->csi
== NVME_CSI_ZONED
&& ns
->csi
== NVME_CSI_ZONED
) {
4942 return nvme_c2h(n
, (uint8_t *)ns
->id_ns_zoned
, sizeof(NvmeIdNsZoned
),
4946 return NVME_INVALID_FIELD
| NVME_DNR
;
4949 static uint16_t nvme_identify_nslist(NvmeCtrl
*n
, NvmeRequest
*req
,
4953 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
4954 uint32_t min_nsid
= le32_to_cpu(c
->nsid
);
4955 uint8_t list
[NVME_IDENTIFY_DATA_SIZE
] = {};
4956 static const int data_len
= sizeof(list
);
4957 uint32_t *list_ptr
= (uint32_t *)list
;
4960 trace_pci_nvme_identify_nslist(min_nsid
);
4963 * Both FFFFFFFFh (NVME_NSID_BROADCAST) and FFFFFFFFEh are invalid values
4964 * since the Active Namespace ID List should return namespaces with ids
4965 * *higher* than the NSID specified in the command. This is also specified
4966 * in the spec (NVM Express v1.3d, Section 5.15.4).
4968 if (min_nsid
>= NVME_NSID_BROADCAST
- 1) {
4969 return NVME_INVALID_NSID
| NVME_DNR
;
4972 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
4976 ns
= nvme_subsys_ns(n
->subsys
, i
);
4984 if (ns
->params
.nsid
<= min_nsid
) {
4987 list_ptr
[j
++] = cpu_to_le32(ns
->params
.nsid
);
4988 if (j
== data_len
/ sizeof(uint32_t)) {
4993 return nvme_c2h(n
, list
, data_len
, req
);
4996 static uint16_t nvme_identify_nslist_csi(NvmeCtrl
*n
, NvmeRequest
*req
,
5000 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
5001 uint32_t min_nsid
= le32_to_cpu(c
->nsid
);
5002 uint8_t list
[NVME_IDENTIFY_DATA_SIZE
] = {};
5003 static const int data_len
= sizeof(list
);
5004 uint32_t *list_ptr
= (uint32_t *)list
;
5007 trace_pci_nvme_identify_nslist_csi(min_nsid
, c
->csi
);
5010 * Same as in nvme_identify_nslist(), FFFFFFFFh/FFFFFFFFEh are invalid.
5012 if (min_nsid
>= NVME_NSID_BROADCAST
- 1) {
5013 return NVME_INVALID_NSID
| NVME_DNR
;
5016 if (c
->csi
!= NVME_CSI_NVM
&& c
->csi
!= NVME_CSI_ZONED
) {
5017 return NVME_INVALID_FIELD
| NVME_DNR
;
5020 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
5024 ns
= nvme_subsys_ns(n
->subsys
, i
);
5032 if (ns
->params
.nsid
<= min_nsid
|| c
->csi
!= ns
->csi
) {
5035 list_ptr
[j
++] = cpu_to_le32(ns
->params
.nsid
);
5036 if (j
== data_len
/ sizeof(uint32_t)) {
5041 return nvme_c2h(n
, list
, data_len
, req
);
5044 static uint16_t nvme_identify_ns_descr_list(NvmeCtrl
*n
, NvmeRequest
*req
)
5047 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
5048 uint32_t nsid
= le32_to_cpu(c
->nsid
);
5049 uint8_t list
[NVME_IDENTIFY_DATA_SIZE
] = {};
5050 uint8_t *pos
= list
;
5053 uint8_t v
[NVME_NIDL_UUID
];
5054 } QEMU_PACKED uuid
= {};
5058 } QEMU_PACKED eui64
= {};
5062 } QEMU_PACKED csi
= {};
5064 trace_pci_nvme_identify_ns_descr_list(nsid
);
5066 if (!nvme_nsid_valid(n
, nsid
) || nsid
== NVME_NSID_BROADCAST
) {
5067 return NVME_INVALID_NSID
| NVME_DNR
;
5070 ns
= nvme_ns(n
, nsid
);
5071 if (unlikely(!ns
)) {
5072 return NVME_INVALID_FIELD
| NVME_DNR
;
5075 if (!qemu_uuid_is_null(&ns
->params
.uuid
)) {
5076 uuid
.hdr
.nidt
= NVME_NIDT_UUID
;
5077 uuid
.hdr
.nidl
= NVME_NIDL_UUID
;
5078 memcpy(uuid
.v
, ns
->params
.uuid
.data
, NVME_NIDL_UUID
);
5079 memcpy(pos
, &uuid
, sizeof(uuid
));
5080 pos
+= sizeof(uuid
);
5083 if (ns
->params
.eui64
) {
5084 eui64
.hdr
.nidt
= NVME_NIDT_EUI64
;
5085 eui64
.hdr
.nidl
= NVME_NIDL_EUI64
;
5086 eui64
.v
= cpu_to_be64(ns
->params
.eui64
);
5087 memcpy(pos
, &eui64
, sizeof(eui64
));
5088 pos
+= sizeof(eui64
);
5091 csi
.hdr
.nidt
= NVME_NIDT_CSI
;
5092 csi
.hdr
.nidl
= NVME_NIDL_CSI
;
5094 memcpy(pos
, &csi
, sizeof(csi
));
5097 return nvme_c2h(n
, list
, sizeof(list
), req
);
5100 static uint16_t nvme_identify_cmd_set(NvmeCtrl
*n
, NvmeRequest
*req
)
5102 uint8_t list
[NVME_IDENTIFY_DATA_SIZE
] = {};
5103 static const int data_len
= sizeof(list
);
5105 trace_pci_nvme_identify_cmd_set();
5107 NVME_SET_CSI(*list
, NVME_CSI_NVM
);
5108 NVME_SET_CSI(*list
, NVME_CSI_ZONED
);
5110 return nvme_c2h(n
, list
, data_len
, req
);
5113 static uint16_t nvme_identify(NvmeCtrl
*n
, NvmeRequest
*req
)
5115 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
5117 trace_pci_nvme_identify(nvme_cid(req
), c
->cns
, le16_to_cpu(c
->ctrlid
),
5121 case NVME_ID_CNS_NS
:
5122 return nvme_identify_ns(n
, req
, true);
5123 case NVME_ID_CNS_NS_PRESENT
:
5124 return nvme_identify_ns(n
, req
, false);
5125 case NVME_ID_CNS_NS_ATTACHED_CTRL_LIST
:
5126 return nvme_identify_ctrl_list(n
, req
, true);
5127 case NVME_ID_CNS_CTRL_LIST
:
5128 return nvme_identify_ctrl_list(n
, req
, false);
5129 case NVME_ID_CNS_PRIMARY_CTRL_CAP
:
5130 return nvme_identify_pri_ctrl_cap(n
, req
);
5131 case NVME_ID_CNS_SECONDARY_CTRL_LIST
:
5132 return nvme_identify_sec_ctrl_list(n
, req
);
5133 case NVME_ID_CNS_CS_NS
:
5134 return nvme_identify_ns_csi(n
, req
, true);
5135 case NVME_ID_CNS_CS_NS_PRESENT
:
5136 return nvme_identify_ns_csi(n
, req
, false);
5137 case NVME_ID_CNS_CTRL
:
5138 return nvme_identify_ctrl(n
, req
);
5139 case NVME_ID_CNS_CS_CTRL
:
5140 return nvme_identify_ctrl_csi(n
, req
);
5141 case NVME_ID_CNS_NS_ACTIVE_LIST
:
5142 return nvme_identify_nslist(n
, req
, true);
5143 case NVME_ID_CNS_NS_PRESENT_LIST
:
5144 return nvme_identify_nslist(n
, req
, false);
5145 case NVME_ID_CNS_CS_NS_ACTIVE_LIST
:
5146 return nvme_identify_nslist_csi(n
, req
, true);
5147 case NVME_ID_CNS_CS_NS_PRESENT_LIST
:
5148 return nvme_identify_nslist_csi(n
, req
, false);
5149 case NVME_ID_CNS_NS_DESCR_LIST
:
5150 return nvme_identify_ns_descr_list(n
, req
);
5151 case NVME_ID_CNS_IO_COMMAND_SET
:
5152 return nvme_identify_cmd_set(n
, req
);
5154 trace_pci_nvme_err_invalid_identify_cns(le32_to_cpu(c
->cns
));
5155 return NVME_INVALID_FIELD
| NVME_DNR
;
5159 static uint16_t nvme_abort(NvmeCtrl
*n
, NvmeRequest
*req
)
5161 uint16_t sqid
= le32_to_cpu(req
->cmd
.cdw10
) & 0xffff;
5163 req
->cqe
.result
= 1;
5164 if (nvme_check_sqid(n
, sqid
)) {
5165 return NVME_INVALID_FIELD
| NVME_DNR
;
5168 return NVME_SUCCESS
;
5171 static inline void nvme_set_timestamp(NvmeCtrl
*n
, uint64_t ts
)
5173 trace_pci_nvme_setfeat_timestamp(ts
);
5175 n
->host_timestamp
= le64_to_cpu(ts
);
5176 n
->timestamp_set_qemu_clock_ms
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
);
5179 static inline uint64_t nvme_get_timestamp(const NvmeCtrl
*n
)
5181 uint64_t current_time
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
);
5182 uint64_t elapsed_time
= current_time
- n
->timestamp_set_qemu_clock_ms
;
5184 union nvme_timestamp
{
5186 uint64_t timestamp
:48;
5194 union nvme_timestamp ts
;
5196 ts
.timestamp
= n
->host_timestamp
+ elapsed_time
;
5198 /* If the host timestamp is non-zero, set the timestamp origin */
5199 ts
.origin
= n
->host_timestamp
? 0x01 : 0x00;
5201 trace_pci_nvme_getfeat_timestamp(ts
.all
);
5203 return cpu_to_le64(ts
.all
);
5206 static uint16_t nvme_get_feature_timestamp(NvmeCtrl
*n
, NvmeRequest
*req
)
5208 uint64_t timestamp
= nvme_get_timestamp(n
);
5210 return nvme_c2h(n
, (uint8_t *)×tamp
, sizeof(timestamp
), req
);
5213 static uint16_t nvme_get_feature(NvmeCtrl
*n
, NvmeRequest
*req
)
5215 NvmeCmd
*cmd
= &req
->cmd
;
5216 uint32_t dw10
= le32_to_cpu(cmd
->cdw10
);
5217 uint32_t dw11
= le32_to_cpu(cmd
->cdw11
);
5218 uint32_t nsid
= le32_to_cpu(cmd
->nsid
);
5220 uint8_t fid
= NVME_GETSETFEAT_FID(dw10
);
5221 NvmeGetFeatureSelect sel
= NVME_GETFEAT_SELECT(dw10
);
5226 static const uint32_t nvme_feature_default
[NVME_FID_MAX
] = {
5227 [NVME_ARBITRATION
] = NVME_ARB_AB_NOLIMIT
,
5230 trace_pci_nvme_getfeat(nvme_cid(req
), nsid
, fid
, sel
, dw11
);
5232 if (!nvme_feature_support
[fid
]) {
5233 return NVME_INVALID_FIELD
| NVME_DNR
;
5236 if (nvme_feature_cap
[fid
] & NVME_FEAT_CAP_NS
) {
5237 if (!nvme_nsid_valid(n
, nsid
) || nsid
== NVME_NSID_BROADCAST
) {
5239 * The Reservation Notification Mask and Reservation Persistence
5240 * features require a status code of Invalid Field in Command when
5241 * NSID is FFFFFFFFh. Since the device does not support those
5242 * features we can always return Invalid Namespace or Format as we
5243 * should do for all other features.
5245 return NVME_INVALID_NSID
| NVME_DNR
;
5248 if (!nvme_ns(n
, nsid
)) {
5249 return NVME_INVALID_FIELD
| NVME_DNR
;
5254 case NVME_GETFEAT_SELECT_CURRENT
:
5256 case NVME_GETFEAT_SELECT_SAVED
:
5257 /* no features are saveable by the controller; fallthrough */
5258 case NVME_GETFEAT_SELECT_DEFAULT
:
5260 case NVME_GETFEAT_SELECT_CAP
:
5261 result
= nvme_feature_cap
[fid
];
5266 case NVME_TEMPERATURE_THRESHOLD
:
5270 * The controller only implements the Composite Temperature sensor, so
5271 * return 0 for all other sensors.
5273 if (NVME_TEMP_TMPSEL(dw11
) != NVME_TEMP_TMPSEL_COMPOSITE
) {
5277 switch (NVME_TEMP_THSEL(dw11
)) {
5278 case NVME_TEMP_THSEL_OVER
:
5279 result
= n
->features
.temp_thresh_hi
;
5281 case NVME_TEMP_THSEL_UNDER
:
5282 result
= n
->features
.temp_thresh_low
;
5286 return NVME_INVALID_FIELD
| NVME_DNR
;
5287 case NVME_ERROR_RECOVERY
:
5288 if (!nvme_nsid_valid(n
, nsid
)) {
5289 return NVME_INVALID_NSID
| NVME_DNR
;
5292 ns
= nvme_ns(n
, nsid
);
5293 if (unlikely(!ns
)) {
5294 return NVME_INVALID_FIELD
| NVME_DNR
;
5297 result
= ns
->features
.err_rec
;
5299 case NVME_VOLATILE_WRITE_CACHE
:
5301 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
5307 result
= blk_enable_write_cache(ns
->blkconf
.blk
);
5312 trace_pci_nvme_getfeat_vwcache(result
? "enabled" : "disabled");
5314 case NVME_ASYNCHRONOUS_EVENT_CONF
:
5315 result
= n
->features
.async_config
;
5317 case NVME_TIMESTAMP
:
5318 return nvme_get_feature_timestamp(n
, req
);
5319 case NVME_HOST_BEHAVIOR_SUPPORT
:
5320 return nvme_c2h(n
, (uint8_t *)&n
->features
.hbs
,
5321 sizeof(n
->features
.hbs
), req
);
5328 case NVME_TEMPERATURE_THRESHOLD
:
5331 if (NVME_TEMP_TMPSEL(dw11
) != NVME_TEMP_TMPSEL_COMPOSITE
) {
5335 if (NVME_TEMP_THSEL(dw11
) == NVME_TEMP_THSEL_OVER
) {
5336 result
= NVME_TEMPERATURE_WARNING
;
5340 case NVME_NUMBER_OF_QUEUES
:
5341 result
= (n
->conf_ioqpairs
- 1) | ((n
->conf_ioqpairs
- 1) << 16);
5342 trace_pci_nvme_getfeat_numq(result
);
5344 case NVME_INTERRUPT_VECTOR_CONF
:
5346 if (iv
>= n
->conf_ioqpairs
+ 1) {
5347 return NVME_INVALID_FIELD
| NVME_DNR
;
5351 if (iv
== n
->admin_cq
.vector
) {
5352 result
|= NVME_INTVC_NOCOALESCING
;
5356 result
= nvme_feature_default
[fid
];
5361 req
->cqe
.result
= cpu_to_le32(result
);
5362 return NVME_SUCCESS
;
5365 static uint16_t nvme_set_feature_timestamp(NvmeCtrl
*n
, NvmeRequest
*req
)
5370 ret
= nvme_h2c(n
, (uint8_t *)×tamp
, sizeof(timestamp
), req
);
5375 nvme_set_timestamp(n
, timestamp
);
5377 return NVME_SUCCESS
;
5380 static uint16_t nvme_set_feature(NvmeCtrl
*n
, NvmeRequest
*req
)
5382 NvmeNamespace
*ns
= NULL
;
5384 NvmeCmd
*cmd
= &req
->cmd
;
5385 uint32_t dw10
= le32_to_cpu(cmd
->cdw10
);
5386 uint32_t dw11
= le32_to_cpu(cmd
->cdw11
);
5387 uint32_t nsid
= le32_to_cpu(cmd
->nsid
);
5388 uint8_t fid
= NVME_GETSETFEAT_FID(dw10
);
5389 uint8_t save
= NVME_SETFEAT_SAVE(dw10
);
5393 trace_pci_nvme_setfeat(nvme_cid(req
), nsid
, fid
, save
, dw11
);
5395 if (save
&& !(nvme_feature_cap
[fid
] & NVME_FEAT_CAP_SAVE
)) {
5396 return NVME_FID_NOT_SAVEABLE
| NVME_DNR
;
5399 if (!nvme_feature_support
[fid
]) {
5400 return NVME_INVALID_FIELD
| NVME_DNR
;
5403 if (nvme_feature_cap
[fid
] & NVME_FEAT_CAP_NS
) {
5404 if (nsid
!= NVME_NSID_BROADCAST
) {
5405 if (!nvme_nsid_valid(n
, nsid
)) {
5406 return NVME_INVALID_NSID
| NVME_DNR
;
5409 ns
= nvme_ns(n
, nsid
);
5410 if (unlikely(!ns
)) {
5411 return NVME_INVALID_FIELD
| NVME_DNR
;
5414 } else if (nsid
&& nsid
!= NVME_NSID_BROADCAST
) {
5415 if (!nvme_nsid_valid(n
, nsid
)) {
5416 return NVME_INVALID_NSID
| NVME_DNR
;
5419 return NVME_FEAT_NOT_NS_SPEC
| NVME_DNR
;
5422 if (!(nvme_feature_cap
[fid
] & NVME_FEAT_CAP_CHANGE
)) {
5423 return NVME_FEAT_NOT_CHANGEABLE
| NVME_DNR
;
5427 case NVME_TEMPERATURE_THRESHOLD
:
5428 if (NVME_TEMP_TMPSEL(dw11
) != NVME_TEMP_TMPSEL_COMPOSITE
) {
5432 switch (NVME_TEMP_THSEL(dw11
)) {
5433 case NVME_TEMP_THSEL_OVER
:
5434 n
->features
.temp_thresh_hi
= NVME_TEMP_TMPTH(dw11
);
5436 case NVME_TEMP_THSEL_UNDER
:
5437 n
->features
.temp_thresh_low
= NVME_TEMP_TMPTH(dw11
);
5440 return NVME_INVALID_FIELD
| NVME_DNR
;
5443 if ((n
->temperature
>= n
->features
.temp_thresh_hi
) ||
5444 (n
->temperature
<= n
->features
.temp_thresh_low
)) {
5445 nvme_smart_event(n
, NVME_SMART_TEMPERATURE
);
5449 case NVME_ERROR_RECOVERY
:
5450 if (nsid
== NVME_NSID_BROADCAST
) {
5451 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
5458 if (NVME_ID_NS_NSFEAT_DULBE(ns
->id_ns
.nsfeat
)) {
5459 ns
->features
.err_rec
= dw11
;
5467 if (NVME_ID_NS_NSFEAT_DULBE(ns
->id_ns
.nsfeat
)) {
5468 ns
->features
.err_rec
= dw11
;
5471 case NVME_VOLATILE_WRITE_CACHE
:
5472 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
5478 if (!(dw11
& 0x1) && blk_enable_write_cache(ns
->blkconf
.blk
)) {
5479 blk_flush(ns
->blkconf
.blk
);
5482 blk_set_enable_write_cache(ns
->blkconf
.blk
, dw11
& 1);
5487 case NVME_NUMBER_OF_QUEUES
:
5488 if (n
->qs_created
) {
5489 return NVME_CMD_SEQ_ERROR
| NVME_DNR
;
5493 * NVMe v1.3, Section 5.21.1.7: FFFFh is not an allowed value for NCQR
5496 if ((dw11
& 0xffff) == 0xffff || ((dw11
>> 16) & 0xffff) == 0xffff) {
5497 return NVME_INVALID_FIELD
| NVME_DNR
;
5500 trace_pci_nvme_setfeat_numq((dw11
& 0xffff) + 1,
5501 ((dw11
>> 16) & 0xffff) + 1,
5504 req
->cqe
.result
= cpu_to_le32((n
->conf_ioqpairs
- 1) |
5505 ((n
->conf_ioqpairs
- 1) << 16));
5507 case NVME_ASYNCHRONOUS_EVENT_CONF
:
5508 n
->features
.async_config
= dw11
;
5510 case NVME_TIMESTAMP
:
5511 return nvme_set_feature_timestamp(n
, req
);
5512 case NVME_HOST_BEHAVIOR_SUPPORT
:
5513 status
= nvme_h2c(n
, (uint8_t *)&n
->features
.hbs
,
5514 sizeof(n
->features
.hbs
), req
);
5519 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
5526 ns
->id_ns
.nlbaf
= ns
->nlbaf
- 1;
5527 if (!n
->features
.hbs
.lbafee
) {
5528 ns
->id_ns
.nlbaf
= MIN(ns
->id_ns
.nlbaf
, 15);
5533 case NVME_COMMAND_SET_PROFILE
:
5535 trace_pci_nvme_err_invalid_iocsci(dw11
& 0x1ff);
5536 return NVME_CMD_SET_CMB_REJECTED
| NVME_DNR
;
5540 return NVME_FEAT_NOT_CHANGEABLE
| NVME_DNR
;
5542 return NVME_SUCCESS
;
5545 static uint16_t nvme_aer(NvmeCtrl
*n
, NvmeRequest
*req
)
5547 trace_pci_nvme_aer(nvme_cid(req
));
5549 if (n
->outstanding_aers
> n
->params
.aerl
) {
5550 trace_pci_nvme_aer_aerl_exceeded();
5551 return NVME_AER_LIMIT_EXCEEDED
;
5554 n
->aer_reqs
[n
->outstanding_aers
] = req
;
5555 n
->outstanding_aers
++;
5557 if (!QTAILQ_EMPTY(&n
->aer_queue
)) {
5558 nvme_process_aers(n
);
5561 return NVME_NO_COMPLETE
;
5564 static void nvme_update_dmrsl(NvmeCtrl
*n
)
5568 for (nsid
= 1; nsid
<= NVME_MAX_NAMESPACES
; nsid
++) {
5569 NvmeNamespace
*ns
= nvme_ns(n
, nsid
);
5574 n
->dmrsl
= MIN_NON_ZERO(n
->dmrsl
,
5575 BDRV_REQUEST_MAX_BYTES
/ nvme_l2b(ns
, 1));
5579 static void nvme_select_iocs_ns(NvmeCtrl
*n
, NvmeNamespace
*ns
)
5581 uint32_t cc
= ldl_le_p(&n
->bar
.cc
);
5583 ns
->iocs
= nvme_cse_iocs_none
;
5586 if (NVME_CC_CSS(cc
) != NVME_CC_CSS_ADMIN_ONLY
) {
5587 ns
->iocs
= nvme_cse_iocs_nvm
;
5590 case NVME_CSI_ZONED
:
5591 if (NVME_CC_CSS(cc
) == NVME_CC_CSS_CSI
) {
5592 ns
->iocs
= nvme_cse_iocs_zoned
;
5593 } else if (NVME_CC_CSS(cc
) == NVME_CC_CSS_NVM
) {
5594 ns
->iocs
= nvme_cse_iocs_nvm
;
5600 static uint16_t nvme_ns_attachment(NvmeCtrl
*n
, NvmeRequest
*req
)
5604 uint16_t list
[NVME_CONTROLLER_LIST_SIZE
] = {};
5605 uint32_t nsid
= le32_to_cpu(req
->cmd
.nsid
);
5606 uint32_t dw10
= le32_to_cpu(req
->cmd
.cdw10
);
5607 uint8_t sel
= dw10
& 0xf;
5608 uint16_t *nr_ids
= &list
[0];
5609 uint16_t *ids
= &list
[1];
5613 trace_pci_nvme_ns_attachment(nvme_cid(req
), dw10
& 0xf);
5615 if (!nvme_nsid_valid(n
, nsid
)) {
5616 return NVME_INVALID_NSID
| NVME_DNR
;
5619 ns
= nvme_subsys_ns(n
->subsys
, nsid
);
5621 return NVME_INVALID_FIELD
| NVME_DNR
;
5624 ret
= nvme_h2c(n
, (uint8_t *)list
, 4096, req
);
5630 return NVME_NS_CTRL_LIST_INVALID
| NVME_DNR
;
5633 *nr_ids
= MIN(*nr_ids
, NVME_CONTROLLER_LIST_SIZE
- 1);
5634 for (i
= 0; i
< *nr_ids
; i
++) {
5635 ctrl
= nvme_subsys_ctrl(n
->subsys
, ids
[i
]);
5637 return NVME_NS_CTRL_LIST_INVALID
| NVME_DNR
;
5641 case NVME_NS_ATTACHMENT_ATTACH
:
5642 if (nvme_ns(ctrl
, nsid
)) {
5643 return NVME_NS_ALREADY_ATTACHED
| NVME_DNR
;
5646 if (ns
->attached
&& !ns
->params
.shared
) {
5647 return NVME_NS_PRIVATE
| NVME_DNR
;
5650 nvme_attach_ns(ctrl
, ns
);
5651 nvme_select_iocs_ns(ctrl
, ns
);
5655 case NVME_NS_ATTACHMENT_DETACH
:
5656 if (!nvme_ns(ctrl
, nsid
)) {
5657 return NVME_NS_NOT_ATTACHED
| NVME_DNR
;
5660 ctrl
->namespaces
[nsid
] = NULL
;
5663 nvme_update_dmrsl(ctrl
);
5668 return NVME_INVALID_FIELD
| NVME_DNR
;
5672 * Add namespace id to the changed namespace id list for event clearing
5673 * via Get Log Page command.
5675 if (!test_and_set_bit(nsid
, ctrl
->changed_nsids
)) {
5676 nvme_enqueue_event(ctrl
, NVME_AER_TYPE_NOTICE
,
5677 NVME_AER_INFO_NOTICE_NS_ATTR_CHANGED
,
5678 NVME_LOG_CHANGED_NSLIST
);
5682 return NVME_SUCCESS
;
5685 typedef struct NvmeFormatAIOCB
{
5702 static void nvme_format_cancel(BlockAIOCB
*aiocb
)
5704 NvmeFormatAIOCB
*iocb
= container_of(aiocb
, NvmeFormatAIOCB
, common
);
5706 iocb
->ret
= -ECANCELED
;
5709 blk_aio_cancel_async(iocb
->aiocb
);
5714 static const AIOCBInfo nvme_format_aiocb_info
= {
5715 .aiocb_size
= sizeof(NvmeFormatAIOCB
),
5716 .cancel_async
= nvme_format_cancel
,
5717 .get_aio_context
= nvme_get_aio_context
,
5720 static void nvme_format_set(NvmeNamespace
*ns
, uint8_t lbaf
, uint8_t mset
,
5721 uint8_t pi
, uint8_t pil
)
5723 uint8_t lbafl
= lbaf
& 0xf;
5724 uint8_t lbafu
= lbaf
>> 4;
5726 trace_pci_nvme_format_set(ns
->params
.nsid
, lbaf
, mset
, pi
, pil
);
5728 ns
->id_ns
.dps
= (pil
<< 3) | pi
;
5729 ns
->id_ns
.flbas
= (lbafu
<< 5) | (mset
<< 4) | lbafl
;
5731 nvme_ns_init_format(ns
);
5734 static void nvme_do_format(NvmeFormatAIOCB
*iocb
);
5736 static void nvme_format_ns_cb(void *opaque
, int ret
)
5738 NvmeFormatAIOCB
*iocb
= opaque
;
5739 NvmeNamespace
*ns
= iocb
->ns
;
5742 if (iocb
->ret
< 0) {
5744 } else if (ret
< 0) {
5751 if (iocb
->offset
< ns
->size
) {
5752 bytes
= MIN(BDRV_REQUEST_MAX_BYTES
, ns
->size
- iocb
->offset
);
5754 iocb
->aiocb
= blk_aio_pwrite_zeroes(ns
->blkconf
.blk
, iocb
->offset
,
5755 bytes
, BDRV_REQ_MAY_UNMAP
,
5756 nvme_format_ns_cb
, iocb
);
5758 iocb
->offset
+= bytes
;
5762 nvme_format_set(ns
, iocb
->lbaf
, iocb
->mset
, iocb
->pi
, iocb
->pil
);
5768 nvme_do_format(iocb
);
5771 static uint16_t nvme_format_check(NvmeNamespace
*ns
, uint8_t lbaf
, uint8_t pi
)
5773 if (ns
->params
.zoned
) {
5774 return NVME_INVALID_FORMAT
| NVME_DNR
;
5777 if (lbaf
> ns
->id_ns
.nlbaf
) {
5778 return NVME_INVALID_FORMAT
| NVME_DNR
;
5781 if (pi
&& (ns
->id_ns
.lbaf
[lbaf
].ms
< nvme_pi_tuple_size(ns
))) {
5782 return NVME_INVALID_FORMAT
| NVME_DNR
;
5785 if (pi
&& pi
> NVME_ID_NS_DPS_TYPE_3
) {
5786 return NVME_INVALID_FIELD
| NVME_DNR
;
5789 return NVME_SUCCESS
;
5792 static void nvme_do_format(NvmeFormatAIOCB
*iocb
)
5794 NvmeRequest
*req
= iocb
->req
;
5795 NvmeCtrl
*n
= nvme_ctrl(req
);
5796 uint32_t dw10
= le32_to_cpu(req
->cmd
.cdw10
);
5797 uint8_t lbaf
= dw10
& 0xf;
5798 uint8_t pi
= (dw10
>> 5) & 0x7;
5802 if (iocb
->ret
< 0) {
5806 if (iocb
->broadcast
) {
5807 for (i
= iocb
->nsid
+ 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
5808 iocb
->ns
= nvme_ns(n
, i
);
5820 status
= nvme_format_check(iocb
->ns
, lbaf
, pi
);
5822 req
->status
= status
;
5826 iocb
->ns
->status
= NVME_FORMAT_IN_PROGRESS
;
5827 nvme_format_ns_cb(iocb
, 0);
5831 iocb
->common
.cb(iocb
->common
.opaque
, iocb
->ret
);
5832 qemu_aio_unref(iocb
);
5835 static uint16_t nvme_format(NvmeCtrl
*n
, NvmeRequest
*req
)
5837 NvmeFormatAIOCB
*iocb
;
5838 uint32_t nsid
= le32_to_cpu(req
->cmd
.nsid
);
5839 uint32_t dw10
= le32_to_cpu(req
->cmd
.cdw10
);
5840 uint8_t lbaf
= dw10
& 0xf;
5841 uint8_t mset
= (dw10
>> 4) & 0x1;
5842 uint8_t pi
= (dw10
>> 5) & 0x7;
5843 uint8_t pil
= (dw10
>> 8) & 0x1;
5844 uint8_t lbafu
= (dw10
>> 12) & 0x3;
5847 iocb
= qemu_aio_get(&nvme_format_aiocb_info
, NULL
, nvme_misc_cb
, req
);
5857 iocb
->broadcast
= (nsid
== NVME_NSID_BROADCAST
);
5860 if (n
->features
.hbs
.lbafee
) {
5861 iocb
->lbaf
|= lbafu
<< 4;
5864 if (!iocb
->broadcast
) {
5865 if (!nvme_nsid_valid(n
, nsid
)) {
5866 status
= NVME_INVALID_NSID
| NVME_DNR
;
5870 iocb
->ns
= nvme_ns(n
, nsid
);
5872 status
= NVME_INVALID_FIELD
| NVME_DNR
;
5877 req
->aiocb
= &iocb
->common
;
5878 nvme_do_format(iocb
);
5880 return NVME_NO_COMPLETE
;
5883 qemu_aio_unref(iocb
);
5888 static void nvme_get_virt_res_num(NvmeCtrl
*n
, uint8_t rt
, int *num_total
,
5889 int *num_prim
, int *num_sec
)
5891 *num_total
= le32_to_cpu(rt
?
5892 n
->pri_ctrl_cap
.vifrt
: n
->pri_ctrl_cap
.vqfrt
);
5893 *num_prim
= le16_to_cpu(rt
?
5894 n
->pri_ctrl_cap
.virfap
: n
->pri_ctrl_cap
.vqrfap
);
5895 *num_sec
= le16_to_cpu(rt
? n
->pri_ctrl_cap
.virfa
: n
->pri_ctrl_cap
.vqrfa
);
5898 static uint16_t nvme_assign_virt_res_to_prim(NvmeCtrl
*n
, NvmeRequest
*req
,
5899 uint16_t cntlid
, uint8_t rt
,
5902 int num_total
, num_prim
, num_sec
;
5904 if (cntlid
!= n
->cntlid
) {
5905 return NVME_INVALID_CTRL_ID
| NVME_DNR
;
5908 nvme_get_virt_res_num(n
, rt
, &num_total
, &num_prim
, &num_sec
);
5910 if (nr
> num_total
) {
5911 return NVME_INVALID_NUM_RESOURCES
| NVME_DNR
;
5914 if (nr
> num_total
- num_sec
) {
5915 return NVME_INVALID_RESOURCE_ID
| NVME_DNR
;
5919 n
->next_pri_ctrl_cap
.virfap
= cpu_to_le16(nr
);
5921 n
->next_pri_ctrl_cap
.vqrfap
= cpu_to_le16(nr
);
5924 req
->cqe
.result
= cpu_to_le32(nr
);
5928 static void nvme_update_virt_res(NvmeCtrl
*n
, NvmeSecCtrlEntry
*sctrl
,
5931 int prev_nr
, prev_total
;
5934 prev_nr
= le16_to_cpu(sctrl
->nvi
);
5935 prev_total
= le32_to_cpu(n
->pri_ctrl_cap
.virfa
);
5936 sctrl
->nvi
= cpu_to_le16(nr
);
5937 n
->pri_ctrl_cap
.virfa
= cpu_to_le32(prev_total
+ nr
- prev_nr
);
5939 prev_nr
= le16_to_cpu(sctrl
->nvq
);
5940 prev_total
= le32_to_cpu(n
->pri_ctrl_cap
.vqrfa
);
5941 sctrl
->nvq
= cpu_to_le16(nr
);
5942 n
->pri_ctrl_cap
.vqrfa
= cpu_to_le32(prev_total
+ nr
- prev_nr
);
5946 static uint16_t nvme_assign_virt_res_to_sec(NvmeCtrl
*n
, NvmeRequest
*req
,
5947 uint16_t cntlid
, uint8_t rt
, int nr
)
5949 int num_total
, num_prim
, num_sec
, num_free
, diff
, limit
;
5950 NvmeSecCtrlEntry
*sctrl
;
5952 sctrl
= nvme_sctrl_for_cntlid(n
, cntlid
);
5954 return NVME_INVALID_CTRL_ID
| NVME_DNR
;
5958 return NVME_INVALID_SEC_CTRL_STATE
| NVME_DNR
;
5961 limit
= le16_to_cpu(rt
? n
->pri_ctrl_cap
.vifrsm
: n
->pri_ctrl_cap
.vqfrsm
);
5963 return NVME_INVALID_NUM_RESOURCES
| NVME_DNR
;
5966 nvme_get_virt_res_num(n
, rt
, &num_total
, &num_prim
, &num_sec
);
5967 num_free
= num_total
- num_prim
- num_sec
;
5968 diff
= nr
- le16_to_cpu(rt
? sctrl
->nvi
: sctrl
->nvq
);
5970 if (diff
> num_free
) {
5971 return NVME_INVALID_RESOURCE_ID
| NVME_DNR
;
5974 nvme_update_virt_res(n
, sctrl
, rt
, nr
);
5975 req
->cqe
.result
= cpu_to_le32(nr
);
5980 static uint16_t nvme_virt_set_state(NvmeCtrl
*n
, uint16_t cntlid
, bool online
)
5982 PCIDevice
*pci
= PCI_DEVICE(n
);
5983 NvmeCtrl
*sn
= NULL
;
5984 NvmeSecCtrlEntry
*sctrl
;
5987 sctrl
= nvme_sctrl_for_cntlid(n
, cntlid
);
5989 return NVME_INVALID_CTRL_ID
| NVME_DNR
;
5992 if (!pci_is_vf(pci
)) {
5993 vf_index
= le16_to_cpu(sctrl
->vfn
) - 1;
5994 sn
= NVME(pcie_sriov_get_vf_at_index(pci
, vf_index
));
5998 if (!sctrl
->nvi
|| (le16_to_cpu(sctrl
->nvq
) < 2) || !sn
) {
5999 return NVME_INVALID_SEC_CTRL_STATE
| NVME_DNR
;
6004 nvme_ctrl_reset(sn
, NVME_RESET_FUNCTION
);
6007 nvme_update_virt_res(n
, sctrl
, NVME_VIRT_RES_INTERRUPT
, 0);
6008 nvme_update_virt_res(n
, sctrl
, NVME_VIRT_RES_QUEUE
, 0);
6013 nvme_ctrl_reset(sn
, NVME_RESET_FUNCTION
);
6018 return NVME_SUCCESS
;
6021 static uint16_t nvme_virt_mngmt(NvmeCtrl
*n
, NvmeRequest
*req
)
6023 uint32_t dw10
= le32_to_cpu(req
->cmd
.cdw10
);
6024 uint32_t dw11
= le32_to_cpu(req
->cmd
.cdw11
);
6025 uint8_t act
= dw10
& 0xf;
6026 uint8_t rt
= (dw10
>> 8) & 0x7;
6027 uint16_t cntlid
= (dw10
>> 16) & 0xffff;
6028 int nr
= dw11
& 0xffff;
6030 trace_pci_nvme_virt_mngmt(nvme_cid(req
), act
, cntlid
, rt
? "VI" : "VQ", nr
);
6032 if (rt
!= NVME_VIRT_RES_QUEUE
&& rt
!= NVME_VIRT_RES_INTERRUPT
) {
6033 return NVME_INVALID_RESOURCE_ID
| NVME_DNR
;
6037 case NVME_VIRT_MNGMT_ACTION_SEC_ASSIGN
:
6038 return nvme_assign_virt_res_to_sec(n
, req
, cntlid
, rt
, nr
);
6039 case NVME_VIRT_MNGMT_ACTION_PRM_ALLOC
:
6040 return nvme_assign_virt_res_to_prim(n
, req
, cntlid
, rt
, nr
);
6041 case NVME_VIRT_MNGMT_ACTION_SEC_ONLINE
:
6042 return nvme_virt_set_state(n
, cntlid
, true);
6043 case NVME_VIRT_MNGMT_ACTION_SEC_OFFLINE
:
6044 return nvme_virt_set_state(n
, cntlid
, false);
6046 return NVME_INVALID_FIELD
| NVME_DNR
;
6050 static uint16_t nvme_dbbuf_config(NvmeCtrl
*n
, const NvmeRequest
*req
)
6052 PCIDevice
*pci
= PCI_DEVICE(n
);
6053 uint64_t dbs_addr
= le64_to_cpu(req
->cmd
.dptr
.prp1
);
6054 uint64_t eis_addr
= le64_to_cpu(req
->cmd
.dptr
.prp2
);
6057 /* Address should be page aligned */
6058 if (dbs_addr
& (n
->page_size
- 1) || eis_addr
& (n
->page_size
- 1)) {
6059 return NVME_INVALID_FIELD
| NVME_DNR
;
6062 /* Save shadow buffer base addr for use during queue creation */
6063 n
->dbbuf_dbs
= dbs_addr
;
6064 n
->dbbuf_eis
= eis_addr
;
6065 n
->dbbuf_enabled
= true;
6067 for (i
= 0; i
< n
->params
.max_ioqpairs
+ 1; i
++) {
6068 NvmeSQueue
*sq
= n
->sq
[i
];
6069 NvmeCQueue
*cq
= n
->cq
[i
];
6073 * CAP.DSTRD is 0, so offset of ith sq db_addr is (i<<3)
6074 * nvme_process_db() uses this hard-coded way to calculate
6075 * doorbell offsets. Be consistent with that here.
6077 sq
->db_addr
= dbs_addr
+ (i
<< 3);
6078 sq
->ei_addr
= eis_addr
+ (i
<< 3);
6079 pci_dma_write(pci
, sq
->db_addr
, &sq
->tail
, sizeof(sq
->tail
));
6081 if (n
->params
.ioeventfd
&& sq
->sqid
!= 0) {
6082 if (!nvme_init_sq_ioeventfd(sq
)) {
6083 sq
->ioeventfd_enabled
= true;
6089 /* CAP.DSTRD is 0, so offset of ith cq db_addr is (i<<3)+(1<<2) */
6090 cq
->db_addr
= dbs_addr
+ (i
<< 3) + (1 << 2);
6091 cq
->ei_addr
= eis_addr
+ (i
<< 3) + (1 << 2);
6092 pci_dma_write(pci
, cq
->db_addr
, &cq
->head
, sizeof(cq
->head
));
6094 if (n
->params
.ioeventfd
&& cq
->cqid
!= 0) {
6095 if (!nvme_init_cq_ioeventfd(cq
)) {
6096 cq
->ioeventfd_enabled
= true;
6102 trace_pci_nvme_dbbuf_config(dbs_addr
, eis_addr
);
6104 return NVME_SUCCESS
;
6107 static uint16_t nvme_admin_cmd(NvmeCtrl
*n
, NvmeRequest
*req
)
6109 trace_pci_nvme_admin_cmd(nvme_cid(req
), nvme_sqid(req
), req
->cmd
.opcode
,
6110 nvme_adm_opc_str(req
->cmd
.opcode
));
6112 if (!(nvme_cse_acs
[req
->cmd
.opcode
] & NVME_CMD_EFF_CSUPP
)) {
6113 trace_pci_nvme_err_invalid_admin_opc(req
->cmd
.opcode
);
6114 return NVME_INVALID_OPCODE
| NVME_DNR
;
6117 /* SGLs shall not be used for Admin commands in NVMe over PCIe */
6118 if (NVME_CMD_FLAGS_PSDT(req
->cmd
.flags
) != NVME_PSDT_PRP
) {
6119 return NVME_INVALID_FIELD
| NVME_DNR
;
6122 if (NVME_CMD_FLAGS_FUSE(req
->cmd
.flags
)) {
6123 return NVME_INVALID_FIELD
;
6126 switch (req
->cmd
.opcode
) {
6127 case NVME_ADM_CMD_DELETE_SQ
:
6128 return nvme_del_sq(n
, req
);
6129 case NVME_ADM_CMD_CREATE_SQ
:
6130 return nvme_create_sq(n
, req
);
6131 case NVME_ADM_CMD_GET_LOG_PAGE
:
6132 return nvme_get_log(n
, req
);
6133 case NVME_ADM_CMD_DELETE_CQ
:
6134 return nvme_del_cq(n
, req
);
6135 case NVME_ADM_CMD_CREATE_CQ
:
6136 return nvme_create_cq(n
, req
);
6137 case NVME_ADM_CMD_IDENTIFY
:
6138 return nvme_identify(n
, req
);
6139 case NVME_ADM_CMD_ABORT
:
6140 return nvme_abort(n
, req
);
6141 case NVME_ADM_CMD_SET_FEATURES
:
6142 return nvme_set_feature(n
, req
);
6143 case NVME_ADM_CMD_GET_FEATURES
:
6144 return nvme_get_feature(n
, req
);
6145 case NVME_ADM_CMD_ASYNC_EV_REQ
:
6146 return nvme_aer(n
, req
);
6147 case NVME_ADM_CMD_NS_ATTACHMENT
:
6148 return nvme_ns_attachment(n
, req
);
6149 case NVME_ADM_CMD_VIRT_MNGMT
:
6150 return nvme_virt_mngmt(n
, req
);
6151 case NVME_ADM_CMD_DBBUF_CONFIG
:
6152 return nvme_dbbuf_config(n
, req
);
6153 case NVME_ADM_CMD_FORMAT_NVM
:
6154 return nvme_format(n
, req
);
6159 return NVME_INVALID_OPCODE
| NVME_DNR
;
6162 static void nvme_update_sq_eventidx(const NvmeSQueue
*sq
)
6164 uint32_t v
= cpu_to_le32(sq
->tail
);
6166 trace_pci_nvme_update_sq_eventidx(sq
->sqid
, sq
->tail
);
6168 pci_dma_write(PCI_DEVICE(sq
->ctrl
), sq
->ei_addr
, &v
, sizeof(v
));
6171 static void nvme_update_sq_tail(NvmeSQueue
*sq
)
6175 pci_dma_read(PCI_DEVICE(sq
->ctrl
), sq
->db_addr
, &v
, sizeof(v
));
6177 sq
->tail
= le32_to_cpu(v
);
6179 trace_pci_nvme_update_sq_tail(sq
->sqid
, sq
->tail
);
6182 static void nvme_process_sq(void *opaque
)
6184 NvmeSQueue
*sq
= opaque
;
6185 NvmeCtrl
*n
= sq
->ctrl
;
6186 NvmeCQueue
*cq
= n
->cq
[sq
->cqid
];
6193 if (n
->dbbuf_enabled
) {
6194 nvme_update_sq_tail(sq
);
6197 while (!(nvme_sq_empty(sq
) || QTAILQ_EMPTY(&sq
->req_list
))) {
6198 addr
= sq
->dma_addr
+ sq
->head
* n
->sqe_size
;
6199 if (nvme_addr_read(n
, addr
, (void *)&cmd
, sizeof(cmd
))) {
6200 trace_pci_nvme_err_addr_read(addr
);
6201 trace_pci_nvme_err_cfs();
6202 stl_le_p(&n
->bar
.csts
, NVME_CSTS_FAILED
);
6205 nvme_inc_sq_head(sq
);
6207 req
= QTAILQ_FIRST(&sq
->req_list
);
6208 QTAILQ_REMOVE(&sq
->req_list
, req
, entry
);
6209 QTAILQ_INSERT_TAIL(&sq
->out_req_list
, req
, entry
);
6210 nvme_req_clear(req
);
6211 req
->cqe
.cid
= cmd
.cid
;
6212 memcpy(&req
->cmd
, &cmd
, sizeof(NvmeCmd
));
6214 status
= sq
->sqid
? nvme_io_cmd(n
, req
) :
6215 nvme_admin_cmd(n
, req
);
6216 if (status
!= NVME_NO_COMPLETE
) {
6217 req
->status
= status
;
6218 nvme_enqueue_req_completion(cq
, req
);
6221 if (n
->dbbuf_enabled
) {
6222 nvme_update_sq_eventidx(sq
);
6223 nvme_update_sq_tail(sq
);
6228 static void nvme_update_msixcap_ts(PCIDevice
*pci_dev
, uint32_t table_size
)
6232 if (!msix_present(pci_dev
)) {
6236 assert(table_size
> 0 && table_size
<= pci_dev
->msix_entries_nr
);
6238 config
= pci_dev
->config
+ pci_dev
->msix_cap
;
6239 pci_set_word_by_mask(config
+ PCI_MSIX_FLAGS
, PCI_MSIX_FLAGS_QSIZE
,
6243 static void nvme_activate_virt_res(NvmeCtrl
*n
)
6245 PCIDevice
*pci_dev
= PCI_DEVICE(n
);
6246 NvmePriCtrlCap
*cap
= &n
->pri_ctrl_cap
;
6247 NvmeSecCtrlEntry
*sctrl
;
6249 /* -1 to account for the admin queue */
6250 if (pci_is_vf(pci_dev
)) {
6251 sctrl
= nvme_sctrl(n
);
6252 cap
->vqprt
= sctrl
->nvq
;
6253 cap
->viprt
= sctrl
->nvi
;
6254 n
->conf_ioqpairs
= sctrl
->nvq
? le16_to_cpu(sctrl
->nvq
) - 1 : 0;
6255 n
->conf_msix_qsize
= sctrl
->nvi
? le16_to_cpu(sctrl
->nvi
) : 1;
6257 cap
->vqrfap
= n
->next_pri_ctrl_cap
.vqrfap
;
6258 cap
->virfap
= n
->next_pri_ctrl_cap
.virfap
;
6259 n
->conf_ioqpairs
= le16_to_cpu(cap
->vqprt
) +
6260 le16_to_cpu(cap
->vqrfap
) - 1;
6261 n
->conf_msix_qsize
= le16_to_cpu(cap
->viprt
) +
6262 le16_to_cpu(cap
->virfap
);
6266 static void nvme_ctrl_reset(NvmeCtrl
*n
, NvmeResetType rst
)
6268 PCIDevice
*pci_dev
= PCI_DEVICE(n
);
6269 NvmeSecCtrlEntry
*sctrl
;
6273 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
6282 for (i
= 0; i
< n
->params
.max_ioqpairs
+ 1; i
++) {
6283 if (n
->sq
[i
] != NULL
) {
6284 nvme_free_sq(n
->sq
[i
], n
);
6287 for (i
= 0; i
< n
->params
.max_ioqpairs
+ 1; i
++) {
6288 if (n
->cq
[i
] != NULL
) {
6289 nvme_free_cq(n
->cq
[i
], n
);
6293 while (!QTAILQ_EMPTY(&n
->aer_queue
)) {
6294 NvmeAsyncEvent
*event
= QTAILQ_FIRST(&n
->aer_queue
);
6295 QTAILQ_REMOVE(&n
->aer_queue
, event
, entry
);
6299 if (n
->params
.sriov_max_vfs
) {
6300 if (!pci_is_vf(pci_dev
)) {
6301 for (i
= 0; i
< n
->sec_ctrl_list
.numcntl
; i
++) {
6302 sctrl
= &n
->sec_ctrl_list
.sec
[i
];
6303 nvme_virt_set_state(n
, le16_to_cpu(sctrl
->scid
), false);
6306 if (rst
!= NVME_RESET_CONTROLLER
) {
6307 pcie_sriov_pf_disable_vfs(pci_dev
);
6311 if (rst
!= NVME_RESET_CONTROLLER
) {
6312 nvme_activate_virt_res(n
);
6318 n
->outstanding_aers
= 0;
6319 n
->qs_created
= false;
6321 nvme_update_msixcap_ts(pci_dev
, n
->conf_msix_qsize
);
6323 if (pci_is_vf(pci_dev
)) {
6324 sctrl
= nvme_sctrl(n
);
6326 stl_le_p(&n
->bar
.csts
, sctrl
->scs
? 0 : NVME_CSTS_FAILED
);
6328 stl_le_p(&n
->bar
.csts
, 0);
6331 stl_le_p(&n
->bar
.intms
, 0);
6332 stl_le_p(&n
->bar
.intmc
, 0);
6333 stl_le_p(&n
->bar
.cc
, 0);
6337 n
->dbbuf_enabled
= false;
6340 static void nvme_ctrl_shutdown(NvmeCtrl
*n
)
6346 memory_region_msync(&n
->pmr
.dev
->mr
, 0, n
->pmr
.dev
->size
);
6349 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
6355 nvme_ns_shutdown(ns
);
6359 static void nvme_select_iocs(NvmeCtrl
*n
)
6364 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
6370 nvme_select_iocs_ns(n
, ns
);
6374 static int nvme_start_ctrl(NvmeCtrl
*n
)
6376 uint64_t cap
= ldq_le_p(&n
->bar
.cap
);
6377 uint32_t cc
= ldl_le_p(&n
->bar
.cc
);
6378 uint32_t aqa
= ldl_le_p(&n
->bar
.aqa
);
6379 uint64_t asq
= ldq_le_p(&n
->bar
.asq
);
6380 uint64_t acq
= ldq_le_p(&n
->bar
.acq
);
6381 uint32_t page_bits
= NVME_CC_MPS(cc
) + 12;
6382 uint32_t page_size
= 1 << page_bits
;
6383 NvmeSecCtrlEntry
*sctrl
= nvme_sctrl(n
);
6385 if (pci_is_vf(PCI_DEVICE(n
)) && !sctrl
->scs
) {
6386 trace_pci_nvme_err_startfail_virt_state(le16_to_cpu(sctrl
->nvi
),
6387 le16_to_cpu(sctrl
->nvq
),
6388 sctrl
->scs
? "ONLINE" :
6392 if (unlikely(n
->cq
[0])) {
6393 trace_pci_nvme_err_startfail_cq();
6396 if (unlikely(n
->sq
[0])) {
6397 trace_pci_nvme_err_startfail_sq();
6400 if (unlikely(asq
& (page_size
- 1))) {
6401 trace_pci_nvme_err_startfail_asq_misaligned(asq
);
6404 if (unlikely(acq
& (page_size
- 1))) {
6405 trace_pci_nvme_err_startfail_acq_misaligned(acq
);
6408 if (unlikely(!(NVME_CAP_CSS(cap
) & (1 << NVME_CC_CSS(cc
))))) {
6409 trace_pci_nvme_err_startfail_css(NVME_CC_CSS(cc
));
6412 if (unlikely(NVME_CC_MPS(cc
) < NVME_CAP_MPSMIN(cap
))) {
6413 trace_pci_nvme_err_startfail_page_too_small(
6415 NVME_CAP_MPSMIN(cap
));
6418 if (unlikely(NVME_CC_MPS(cc
) >
6419 NVME_CAP_MPSMAX(cap
))) {
6420 trace_pci_nvme_err_startfail_page_too_large(
6422 NVME_CAP_MPSMAX(cap
));
6425 if (unlikely(NVME_CC_IOCQES(cc
) <
6426 NVME_CTRL_CQES_MIN(n
->id_ctrl
.cqes
))) {
6427 trace_pci_nvme_err_startfail_cqent_too_small(
6429 NVME_CTRL_CQES_MIN(cap
));
6432 if (unlikely(NVME_CC_IOCQES(cc
) >
6433 NVME_CTRL_CQES_MAX(n
->id_ctrl
.cqes
))) {
6434 trace_pci_nvme_err_startfail_cqent_too_large(
6436 NVME_CTRL_CQES_MAX(cap
));
6439 if (unlikely(NVME_CC_IOSQES(cc
) <
6440 NVME_CTRL_SQES_MIN(n
->id_ctrl
.sqes
))) {
6441 trace_pci_nvme_err_startfail_sqent_too_small(
6443 NVME_CTRL_SQES_MIN(cap
));
6446 if (unlikely(NVME_CC_IOSQES(cc
) >
6447 NVME_CTRL_SQES_MAX(n
->id_ctrl
.sqes
))) {
6448 trace_pci_nvme_err_startfail_sqent_too_large(
6450 NVME_CTRL_SQES_MAX(cap
));
6453 if (unlikely(!NVME_AQA_ASQS(aqa
))) {
6454 trace_pci_nvme_err_startfail_asqent_sz_zero();
6457 if (unlikely(!NVME_AQA_ACQS(aqa
))) {
6458 trace_pci_nvme_err_startfail_acqent_sz_zero();
6462 n
->page_bits
= page_bits
;
6463 n
->page_size
= page_size
;
6464 n
->max_prp_ents
= n
->page_size
/ sizeof(uint64_t);
6465 n
->cqe_size
= 1 << NVME_CC_IOCQES(cc
);
6466 n
->sqe_size
= 1 << NVME_CC_IOSQES(cc
);
6467 nvme_init_cq(&n
->admin_cq
, n
, acq
, 0, 0, NVME_AQA_ACQS(aqa
) + 1, 1);
6468 nvme_init_sq(&n
->admin_sq
, n
, asq
, 0, 0, NVME_AQA_ASQS(aqa
) + 1);
6470 nvme_set_timestamp(n
, 0ULL);
6472 nvme_select_iocs(n
);
6477 static void nvme_cmb_enable_regs(NvmeCtrl
*n
)
6479 uint32_t cmbloc
= ldl_le_p(&n
->bar
.cmbloc
);
6480 uint32_t cmbsz
= ldl_le_p(&n
->bar
.cmbsz
);
6482 NVME_CMBLOC_SET_CDPCILS(cmbloc
, 1);
6483 NVME_CMBLOC_SET_CDPMLS(cmbloc
, 1);
6484 NVME_CMBLOC_SET_BIR(cmbloc
, NVME_CMB_BIR
);
6485 stl_le_p(&n
->bar
.cmbloc
, cmbloc
);
6487 NVME_CMBSZ_SET_SQS(cmbsz
, 1);
6488 NVME_CMBSZ_SET_CQS(cmbsz
, 0);
6489 NVME_CMBSZ_SET_LISTS(cmbsz
, 1);
6490 NVME_CMBSZ_SET_RDS(cmbsz
, 1);
6491 NVME_CMBSZ_SET_WDS(cmbsz
, 1);
6492 NVME_CMBSZ_SET_SZU(cmbsz
, 2); /* MBs */
6493 NVME_CMBSZ_SET_SZ(cmbsz
, n
->params
.cmb_size_mb
);
6494 stl_le_p(&n
->bar
.cmbsz
, cmbsz
);
6497 static void nvme_write_bar(NvmeCtrl
*n
, hwaddr offset
, uint64_t data
,
6500 PCIDevice
*pci
= PCI_DEVICE(n
);
6501 uint64_t cap
= ldq_le_p(&n
->bar
.cap
);
6502 uint32_t cc
= ldl_le_p(&n
->bar
.cc
);
6503 uint32_t intms
= ldl_le_p(&n
->bar
.intms
);
6504 uint32_t csts
= ldl_le_p(&n
->bar
.csts
);
6505 uint32_t pmrsts
= ldl_le_p(&n
->bar
.pmrsts
);
6507 if (unlikely(offset
& (sizeof(uint32_t) - 1))) {
6508 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_misaligned32
,
6509 "MMIO write not 32-bit aligned,"
6510 " offset=0x%"PRIx64
"", offset
);
6511 /* should be ignored, fall through for now */
6514 if (unlikely(size
< sizeof(uint32_t))) {
6515 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_toosmall
,
6516 "MMIO write smaller than 32-bits,"
6517 " offset=0x%"PRIx64
", size=%u",
6519 /* should be ignored, fall through for now */
6523 case NVME_REG_INTMS
:
6524 if (unlikely(msix_enabled(pci
))) {
6525 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_intmask_with_msix
,
6526 "undefined access to interrupt mask set"
6527 " when MSI-X is enabled");
6528 /* should be ignored, fall through for now */
6531 stl_le_p(&n
->bar
.intms
, intms
);
6532 n
->bar
.intmc
= n
->bar
.intms
;
6533 trace_pci_nvme_mmio_intm_set(data
& 0xffffffff, intms
);
6536 case NVME_REG_INTMC
:
6537 if (unlikely(msix_enabled(pci
))) {
6538 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_intmask_with_msix
,
6539 "undefined access to interrupt mask clr"
6540 " when MSI-X is enabled");
6541 /* should be ignored, fall through for now */
6544 stl_le_p(&n
->bar
.intms
, intms
);
6545 n
->bar
.intmc
= n
->bar
.intms
;
6546 trace_pci_nvme_mmio_intm_clr(data
& 0xffffffff, intms
);
6550 stl_le_p(&n
->bar
.cc
, data
);
6552 trace_pci_nvme_mmio_cfg(data
& 0xffffffff);
6554 if (NVME_CC_SHN(data
) && !(NVME_CC_SHN(cc
))) {
6555 trace_pci_nvme_mmio_shutdown_set();
6556 nvme_ctrl_shutdown(n
);
6557 csts
&= ~(CSTS_SHST_MASK
<< CSTS_SHST_SHIFT
);
6558 csts
|= NVME_CSTS_SHST_COMPLETE
;
6559 } else if (!NVME_CC_SHN(data
) && NVME_CC_SHN(cc
)) {
6560 trace_pci_nvme_mmio_shutdown_cleared();
6561 csts
&= ~(CSTS_SHST_MASK
<< CSTS_SHST_SHIFT
);
6564 if (NVME_CC_EN(data
) && !NVME_CC_EN(cc
)) {
6565 if (unlikely(nvme_start_ctrl(n
))) {
6566 trace_pci_nvme_err_startfail();
6567 csts
= NVME_CSTS_FAILED
;
6569 trace_pci_nvme_mmio_start_success();
6570 csts
= NVME_CSTS_READY
;
6572 } else if (!NVME_CC_EN(data
) && NVME_CC_EN(cc
)) {
6573 trace_pci_nvme_mmio_stopped();
6574 nvme_ctrl_reset(n
, NVME_RESET_CONTROLLER
);
6579 stl_le_p(&n
->bar
.csts
, csts
);
6583 if (data
& (1 << 4)) {
6584 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_ssreset_w1c_unsupported
,
6585 "attempted to W1C CSTS.NSSRO"
6586 " but CAP.NSSRS is zero (not supported)");
6587 } else if (data
!= 0) {
6588 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_ro_csts
,
6589 "attempted to set a read only bit"
6590 " of controller status");
6594 if (data
== 0x4e564d65) {
6595 trace_pci_nvme_ub_mmiowr_ssreset_unsupported();
6597 /* The spec says that writes of other values have no effect */
6602 stl_le_p(&n
->bar
.aqa
, data
);
6603 trace_pci_nvme_mmio_aqattr(data
& 0xffffffff);
6606 stn_le_p(&n
->bar
.asq
, size
, data
);
6607 trace_pci_nvme_mmio_asqaddr(data
);
6609 case NVME_REG_ASQ
+ 4:
6610 stl_le_p((uint8_t *)&n
->bar
.asq
+ 4, data
);
6611 trace_pci_nvme_mmio_asqaddr_hi(data
, ldq_le_p(&n
->bar
.asq
));
6614 trace_pci_nvme_mmio_acqaddr(data
);
6615 stn_le_p(&n
->bar
.acq
, size
, data
);
6617 case NVME_REG_ACQ
+ 4:
6618 stl_le_p((uint8_t *)&n
->bar
.acq
+ 4, data
);
6619 trace_pci_nvme_mmio_acqaddr_hi(data
, ldq_le_p(&n
->bar
.acq
));
6621 case NVME_REG_CMBLOC
:
6622 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_cmbloc_reserved
,
6623 "invalid write to reserved CMBLOC"
6624 " when CMBSZ is zero, ignored");
6626 case NVME_REG_CMBSZ
:
6627 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_cmbsz_readonly
,
6628 "invalid write to read only CMBSZ, ignored");
6630 case NVME_REG_CMBMSC
:
6631 if (!NVME_CAP_CMBS(cap
)) {
6635 stn_le_p(&n
->bar
.cmbmsc
, size
, data
);
6636 n
->cmb
.cmse
= false;
6638 if (NVME_CMBMSC_CRE(data
)) {
6639 nvme_cmb_enable_regs(n
);
6641 if (NVME_CMBMSC_CMSE(data
)) {
6642 uint64_t cmbmsc
= ldq_le_p(&n
->bar
.cmbmsc
);
6643 hwaddr cba
= NVME_CMBMSC_CBA(cmbmsc
) << CMBMSC_CBA_SHIFT
;
6644 if (cba
+ int128_get64(n
->cmb
.mem
.size
) < cba
) {
6645 uint32_t cmbsts
= ldl_le_p(&n
->bar
.cmbsts
);
6646 NVME_CMBSTS_SET_CBAI(cmbsts
, 1);
6647 stl_le_p(&n
->bar
.cmbsts
, cmbsts
);
6660 case NVME_REG_CMBMSC
+ 4:
6661 stl_le_p((uint8_t *)&n
->bar
.cmbmsc
+ 4, data
);
6664 case NVME_REG_PMRCAP
:
6665 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrcap_readonly
,
6666 "invalid write to PMRCAP register, ignored");
6668 case NVME_REG_PMRCTL
:
6669 if (!NVME_CAP_PMRS(cap
)) {
6673 stl_le_p(&n
->bar
.pmrctl
, data
);
6674 if (NVME_PMRCTL_EN(data
)) {
6675 memory_region_set_enabled(&n
->pmr
.dev
->mr
, true);
6678 memory_region_set_enabled(&n
->pmr
.dev
->mr
, false);
6679 NVME_PMRSTS_SET_NRDY(pmrsts
, 1);
6680 n
->pmr
.cmse
= false;
6682 stl_le_p(&n
->bar
.pmrsts
, pmrsts
);
6684 case NVME_REG_PMRSTS
:
6685 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrsts_readonly
,
6686 "invalid write to PMRSTS register, ignored");
6688 case NVME_REG_PMREBS
:
6689 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrebs_readonly
,
6690 "invalid write to PMREBS register, ignored");
6692 case NVME_REG_PMRSWTP
:
6693 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrswtp_readonly
,
6694 "invalid write to PMRSWTP register, ignored");
6696 case NVME_REG_PMRMSCL
:
6697 if (!NVME_CAP_PMRS(cap
)) {
6701 stl_le_p(&n
->bar
.pmrmscl
, data
);
6702 n
->pmr
.cmse
= false;
6704 if (NVME_PMRMSCL_CMSE(data
)) {
6705 uint64_t pmrmscu
= ldl_le_p(&n
->bar
.pmrmscu
);
6706 hwaddr cba
= pmrmscu
<< 32 |
6707 (NVME_PMRMSCL_CBA(data
) << PMRMSCL_CBA_SHIFT
);
6708 if (cba
+ int128_get64(n
->pmr
.dev
->mr
.size
) < cba
) {
6709 NVME_PMRSTS_SET_CBAI(pmrsts
, 1);
6710 stl_le_p(&n
->bar
.pmrsts
, pmrsts
);
6719 case NVME_REG_PMRMSCU
:
6720 if (!NVME_CAP_PMRS(cap
)) {
6724 stl_le_p(&n
->bar
.pmrmscu
, data
);
6727 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_invalid
,
6728 "invalid MMIO write,"
6729 " offset=0x%"PRIx64
", data=%"PRIx64
"",
6735 static uint64_t nvme_mmio_read(void *opaque
, hwaddr addr
, unsigned size
)
6737 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
6738 uint8_t *ptr
= (uint8_t *)&n
->bar
;
6740 trace_pci_nvme_mmio_read(addr
, size
);
6742 if (unlikely(addr
& (sizeof(uint32_t) - 1))) {
6743 NVME_GUEST_ERR(pci_nvme_ub_mmiord_misaligned32
,
6744 "MMIO read not 32-bit aligned,"
6745 " offset=0x%"PRIx64
"", addr
);
6746 /* should RAZ, fall through for now */
6747 } else if (unlikely(size
< sizeof(uint32_t))) {
6748 NVME_GUEST_ERR(pci_nvme_ub_mmiord_toosmall
,
6749 "MMIO read smaller than 32-bits,"
6750 " offset=0x%"PRIx64
"", addr
);
6751 /* should RAZ, fall through for now */
6754 if (addr
> sizeof(n
->bar
) - size
) {
6755 NVME_GUEST_ERR(pci_nvme_ub_mmiord_invalid_ofs
,
6756 "MMIO read beyond last register,"
6757 " offset=0x%"PRIx64
", returning 0", addr
);
6762 if (pci_is_vf(PCI_DEVICE(n
)) && !nvme_sctrl(n
)->scs
&&
6763 addr
!= NVME_REG_CSTS
) {
6764 trace_pci_nvme_err_ignored_mmio_vf_offline(addr
, size
);
6769 * When PMRWBM bit 1 is set then read from
6770 * from PMRSTS should ensure prior writes
6771 * made it to persistent media
6773 if (addr
== NVME_REG_PMRSTS
&&
6774 (NVME_PMRCAP_PMRWBM(ldl_le_p(&n
->bar
.pmrcap
)) & 0x02)) {
6775 memory_region_msync(&n
->pmr
.dev
->mr
, 0, n
->pmr
.dev
->size
);
6778 return ldn_le_p(ptr
+ addr
, size
);
6781 static void nvme_process_db(NvmeCtrl
*n
, hwaddr addr
, int val
)
6783 PCIDevice
*pci
= PCI_DEVICE(n
);
6786 if (unlikely(addr
& ((1 << 2) - 1))) {
6787 NVME_GUEST_ERR(pci_nvme_ub_db_wr_misaligned
,
6788 "doorbell write not 32-bit aligned,"
6789 " offset=0x%"PRIx64
", ignoring", addr
);
6793 if (((addr
- 0x1000) >> 2) & 1) {
6794 /* Completion queue doorbell write */
6796 uint16_t new_head
= val
& 0xffff;
6800 qid
= (addr
- (0x1000 + (1 << 2))) >> 3;
6801 if (unlikely(nvme_check_cqid(n
, qid
))) {
6802 NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_cq
,
6803 "completion queue doorbell write"
6804 " for nonexistent queue,"
6805 " sqid=%"PRIu32
", ignoring", qid
);
6808 * NVM Express v1.3d, Section 4.1 state: "If host software writes
6809 * an invalid value to the Submission Queue Tail Doorbell or
6810 * Completion Queue Head Doorbell regiter and an Asynchronous Event
6811 * Request command is outstanding, then an asynchronous event is
6812 * posted to the Admin Completion Queue with a status code of
6813 * Invalid Doorbell Write Value."
6815 * Also note that the spec includes the "Invalid Doorbell Register"
6816 * status code, but nowhere does it specify when to use it.
6817 * However, it seems reasonable to use it here in a similar
6820 if (n
->outstanding_aers
) {
6821 nvme_enqueue_event(n
, NVME_AER_TYPE_ERROR
,
6822 NVME_AER_INFO_ERR_INVALID_DB_REGISTER
,
6823 NVME_LOG_ERROR_INFO
);
6830 if (unlikely(new_head
>= cq
->size
)) {
6831 NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_cqhead
,
6832 "completion queue doorbell write value"
6833 " beyond queue size, sqid=%"PRIu32
","
6834 " new_head=%"PRIu16
", ignoring",
6837 if (n
->outstanding_aers
) {
6838 nvme_enqueue_event(n
, NVME_AER_TYPE_ERROR
,
6839 NVME_AER_INFO_ERR_INVALID_DB_VALUE
,
6840 NVME_LOG_ERROR_INFO
);
6846 trace_pci_nvme_mmio_doorbell_cq(cq
->cqid
, new_head
);
6848 start_sqs
= nvme_cq_full(cq
) ? 1 : 0;
6849 cq
->head
= new_head
;
6850 if (!qid
&& n
->dbbuf_enabled
) {
6851 pci_dma_write(pci
, cq
->db_addr
, &cq
->head
, sizeof(cq
->head
));
6855 QTAILQ_FOREACH(sq
, &cq
->sq_list
, entry
) {
6856 qemu_bh_schedule(sq
->bh
);
6858 qemu_bh_schedule(cq
->bh
);
6861 if (cq
->tail
== cq
->head
) {
6862 if (cq
->irq_enabled
) {
6866 nvme_irq_deassert(n
, cq
);
6869 /* Submission queue doorbell write */
6871 uint16_t new_tail
= val
& 0xffff;
6874 qid
= (addr
- 0x1000) >> 3;
6875 if (unlikely(nvme_check_sqid(n
, qid
))) {
6876 NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_sq
,
6877 "submission queue doorbell write"
6878 " for nonexistent queue,"
6879 " sqid=%"PRIu32
", ignoring", qid
);
6881 if (n
->outstanding_aers
) {
6882 nvme_enqueue_event(n
, NVME_AER_TYPE_ERROR
,
6883 NVME_AER_INFO_ERR_INVALID_DB_REGISTER
,
6884 NVME_LOG_ERROR_INFO
);
6891 if (unlikely(new_tail
>= sq
->size
)) {
6892 NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_sqtail
,
6893 "submission queue doorbell write value"
6894 " beyond queue size, sqid=%"PRIu32
","
6895 " new_tail=%"PRIu16
", ignoring",
6898 if (n
->outstanding_aers
) {
6899 nvme_enqueue_event(n
, NVME_AER_TYPE_ERROR
,
6900 NVME_AER_INFO_ERR_INVALID_DB_VALUE
,
6901 NVME_LOG_ERROR_INFO
);
6907 trace_pci_nvme_mmio_doorbell_sq(sq
->sqid
, new_tail
);
6909 sq
->tail
= new_tail
;
6910 if (!qid
&& n
->dbbuf_enabled
) {
6912 * The spec states "the host shall also update the controller's
6913 * corresponding doorbell property to match the value of that entry
6914 * in the Shadow Doorbell buffer."
6916 * Since this context is currently a VM trap, we can safely enforce
6917 * the requirement from the device side in case the host is
6920 * Note, we shouldn't have to do this, but various drivers
6921 * including ones that run on Linux, are not updating Admin Queues,
6922 * so we can't trust reading it for an appropriate sq tail.
6924 pci_dma_write(pci
, sq
->db_addr
, &sq
->tail
, sizeof(sq
->tail
));
6927 qemu_bh_schedule(sq
->bh
);
6931 static void nvme_mmio_write(void *opaque
, hwaddr addr
, uint64_t data
,
6934 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
6936 trace_pci_nvme_mmio_write(addr
, data
, size
);
6938 if (pci_is_vf(PCI_DEVICE(n
)) && !nvme_sctrl(n
)->scs
&&
6939 addr
!= NVME_REG_CSTS
) {
6940 trace_pci_nvme_err_ignored_mmio_vf_offline(addr
, size
);
6944 if (addr
< sizeof(n
->bar
)) {
6945 nvme_write_bar(n
, addr
, data
, size
);
6947 nvme_process_db(n
, addr
, data
);
6951 static const MemoryRegionOps nvme_mmio_ops
= {
6952 .read
= nvme_mmio_read
,
6953 .write
= nvme_mmio_write
,
6954 .endianness
= DEVICE_LITTLE_ENDIAN
,
6956 .min_access_size
= 2,
6957 .max_access_size
= 8,
6961 static void nvme_cmb_write(void *opaque
, hwaddr addr
, uint64_t data
,
6964 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
6965 stn_le_p(&n
->cmb
.buf
[addr
], size
, data
);
6968 static uint64_t nvme_cmb_read(void *opaque
, hwaddr addr
, unsigned size
)
6970 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
6971 return ldn_le_p(&n
->cmb
.buf
[addr
], size
);
6974 static const MemoryRegionOps nvme_cmb_ops
= {
6975 .read
= nvme_cmb_read
,
6976 .write
= nvme_cmb_write
,
6977 .endianness
= DEVICE_LITTLE_ENDIAN
,
6979 .min_access_size
= 1,
6980 .max_access_size
= 8,
6984 static bool nvme_check_params(NvmeCtrl
*n
, Error
**errp
)
6986 NvmeParams
*params
= &n
->params
;
6988 if (params
->num_queues
) {
6989 warn_report("num_queues is deprecated; please use max_ioqpairs "
6992 params
->max_ioqpairs
= params
->num_queues
- 1;
6995 if (n
->namespace.blkconf
.blk
&& n
->subsys
) {
6996 error_setg(errp
, "subsystem support is unavailable with legacy "
6997 "namespace ('drive' property)");
7001 if (params
->max_ioqpairs
< 1 ||
7002 params
->max_ioqpairs
> NVME_MAX_IOQPAIRS
) {
7003 error_setg(errp
, "max_ioqpairs must be between 1 and %d",
7008 if (params
->msix_qsize
< 1 ||
7009 params
->msix_qsize
> PCI_MSIX_FLAGS_QSIZE
+ 1) {
7010 error_setg(errp
, "msix_qsize must be between 1 and %d",
7011 PCI_MSIX_FLAGS_QSIZE
+ 1);
7015 if (!params
->serial
) {
7016 error_setg(errp
, "serial property not set");
7021 if (host_memory_backend_is_mapped(n
->pmr
.dev
)) {
7022 error_setg(errp
, "can't use already busy memdev: %s",
7023 object_get_canonical_path_component(OBJECT(n
->pmr
.dev
)));
7027 if (!is_power_of_2(n
->pmr
.dev
->size
)) {
7028 error_setg(errp
, "pmr backend size needs to be power of 2 in size");
7032 host_memory_backend_set_mapped(n
->pmr
.dev
, true);
7035 if (n
->params
.zasl
> n
->params
.mdts
) {
7036 error_setg(errp
, "zoned.zasl (Zone Append Size Limit) must be less "
7037 "than or equal to mdts (Maximum Data Transfer Size)");
7041 if (!n
->params
.vsl
) {
7042 error_setg(errp
, "vsl must be non-zero");
7046 if (params
->sriov_max_vfs
) {
7048 error_setg(errp
, "subsystem is required for the use of SR-IOV");
7052 if (params
->sriov_max_vfs
> NVME_MAX_VFS
) {
7053 error_setg(errp
, "sriov_max_vfs must be between 0 and %d",
7058 if (params
->cmb_size_mb
) {
7059 error_setg(errp
, "CMB is not supported with SR-IOV");
7064 error_setg(errp
, "PMR is not supported with SR-IOV");
7068 if (!params
->sriov_vq_flexible
|| !params
->sriov_vi_flexible
) {
7069 error_setg(errp
, "both sriov_vq_flexible and sriov_vi_flexible"
7070 " must be set for the use of SR-IOV");
7074 if (params
->sriov_vq_flexible
< params
->sriov_max_vfs
* 2) {
7075 error_setg(errp
, "sriov_vq_flexible must be greater than or equal"
7076 " to %d (sriov_max_vfs * 2)", params
->sriov_max_vfs
* 2);
7080 if (params
->max_ioqpairs
< params
->sriov_vq_flexible
+ 2) {
7081 error_setg(errp
, "(max_ioqpairs - sriov_vq_flexible) must be"
7082 " greater than or equal to 2");
7086 if (params
->sriov_vi_flexible
< params
->sriov_max_vfs
) {
7087 error_setg(errp
, "sriov_vi_flexible must be greater than or equal"
7088 " to %d (sriov_max_vfs)", params
->sriov_max_vfs
);
7092 if (params
->msix_qsize
< params
->sriov_vi_flexible
+ 1) {
7093 error_setg(errp
, "(msix_qsize - sriov_vi_flexible) must be"
7094 " greater than or equal to 1");
7098 if (params
->sriov_max_vi_per_vf
&&
7099 (params
->sriov_max_vi_per_vf
- 1) % NVME_VF_RES_GRANULARITY
) {
7100 error_setg(errp
, "sriov_max_vi_per_vf must meet:"
7101 " (sriov_max_vi_per_vf - 1) %% %d == 0 and"
7102 " sriov_max_vi_per_vf >= 1", NVME_VF_RES_GRANULARITY
);
7106 if (params
->sriov_max_vq_per_vf
&&
7107 (params
->sriov_max_vq_per_vf
< 2 ||
7108 (params
->sriov_max_vq_per_vf
- 1) % NVME_VF_RES_GRANULARITY
)) {
7109 error_setg(errp
, "sriov_max_vq_per_vf must meet:"
7110 " (sriov_max_vq_per_vf - 1) %% %d == 0 and"
7111 " sriov_max_vq_per_vf >= 2", NVME_VF_RES_GRANULARITY
);
7119 static void nvme_init_state(NvmeCtrl
*n
)
7121 NvmePriCtrlCap
*cap
= &n
->pri_ctrl_cap
;
7122 NvmeSecCtrlList
*list
= &n
->sec_ctrl_list
;
7123 NvmeSecCtrlEntry
*sctrl
;
7124 PCIDevice
*pci
= PCI_DEVICE(n
);
7128 if (pci_is_vf(pci
)) {
7129 sctrl
= nvme_sctrl(n
);
7131 n
->conf_ioqpairs
= sctrl
->nvq
? le16_to_cpu(sctrl
->nvq
) - 1 : 0;
7132 n
->conf_msix_qsize
= sctrl
->nvi
? le16_to_cpu(sctrl
->nvi
) : 1;
7134 max_vfs
= n
->params
.sriov_max_vfs
;
7135 n
->conf_ioqpairs
= n
->params
.max_ioqpairs
;
7136 n
->conf_msix_qsize
= n
->params
.msix_qsize
;
7139 n
->sq
= g_new0(NvmeSQueue
*, n
->params
.max_ioqpairs
+ 1);
7140 n
->cq
= g_new0(NvmeCQueue
*, n
->params
.max_ioqpairs
+ 1);
7141 n
->temperature
= NVME_TEMPERATURE
;
7142 n
->features
.temp_thresh_hi
= NVME_TEMPERATURE_WARNING
;
7143 n
->starttime_ms
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
);
7144 n
->aer_reqs
= g_new0(NvmeRequest
*, n
->params
.aerl
+ 1);
7145 QTAILQ_INIT(&n
->aer_queue
);
7147 list
->numcntl
= cpu_to_le16(max_vfs
);
7148 for (i
= 0; i
< max_vfs
; i
++) {
7149 sctrl
= &list
->sec
[i
];
7150 sctrl
->pcid
= cpu_to_le16(n
->cntlid
);
7151 sctrl
->vfn
= cpu_to_le16(i
+ 1);
7154 cap
->cntlid
= cpu_to_le16(n
->cntlid
);
7155 cap
->crt
= NVME_CRT_VQ
| NVME_CRT_VI
;
7157 if (pci_is_vf(pci
)) {
7158 cap
->vqprt
= cpu_to_le16(1 + n
->conf_ioqpairs
);
7160 cap
->vqprt
= cpu_to_le16(1 + n
->params
.max_ioqpairs
-
7161 n
->params
.sriov_vq_flexible
);
7162 cap
->vqfrt
= cpu_to_le32(n
->params
.sriov_vq_flexible
);
7163 cap
->vqrfap
= cap
->vqfrt
;
7164 cap
->vqgran
= cpu_to_le16(NVME_VF_RES_GRANULARITY
);
7165 cap
->vqfrsm
= n
->params
.sriov_max_vq_per_vf
?
7166 cpu_to_le16(n
->params
.sriov_max_vq_per_vf
) :
7167 cap
->vqfrt
/ MAX(max_vfs
, 1);
7170 if (pci_is_vf(pci
)) {
7171 cap
->viprt
= cpu_to_le16(n
->conf_msix_qsize
);
7173 cap
->viprt
= cpu_to_le16(n
->params
.msix_qsize
-
7174 n
->params
.sriov_vi_flexible
);
7175 cap
->vifrt
= cpu_to_le32(n
->params
.sriov_vi_flexible
);
7176 cap
->virfap
= cap
->vifrt
;
7177 cap
->vigran
= cpu_to_le16(NVME_VF_RES_GRANULARITY
);
7178 cap
->vifrsm
= n
->params
.sriov_max_vi_per_vf
?
7179 cpu_to_le16(n
->params
.sriov_max_vi_per_vf
) :
7180 cap
->vifrt
/ MAX(max_vfs
, 1);
7184 static void nvme_init_cmb(NvmeCtrl
*n
, PCIDevice
*pci_dev
)
7186 uint64_t cmb_size
= n
->params
.cmb_size_mb
* MiB
;
7187 uint64_t cap
= ldq_le_p(&n
->bar
.cap
);
7189 n
->cmb
.buf
= g_malloc0(cmb_size
);
7190 memory_region_init_io(&n
->cmb
.mem
, OBJECT(n
), &nvme_cmb_ops
, n
,
7191 "nvme-cmb", cmb_size
);
7192 pci_register_bar(pci_dev
, NVME_CMB_BIR
,
7193 PCI_BASE_ADDRESS_SPACE_MEMORY
|
7194 PCI_BASE_ADDRESS_MEM_TYPE_64
|
7195 PCI_BASE_ADDRESS_MEM_PREFETCH
, &n
->cmb
.mem
);
7197 NVME_CAP_SET_CMBS(cap
, 1);
7198 stq_le_p(&n
->bar
.cap
, cap
);
7200 if (n
->params
.legacy_cmb
) {
7201 nvme_cmb_enable_regs(n
);
7206 static void nvme_init_pmr(NvmeCtrl
*n
, PCIDevice
*pci_dev
)
7208 uint32_t pmrcap
= ldl_le_p(&n
->bar
.pmrcap
);
7210 NVME_PMRCAP_SET_RDS(pmrcap
, 1);
7211 NVME_PMRCAP_SET_WDS(pmrcap
, 1);
7212 NVME_PMRCAP_SET_BIR(pmrcap
, NVME_PMR_BIR
);
7213 /* Turn on bit 1 support */
7214 NVME_PMRCAP_SET_PMRWBM(pmrcap
, 0x02);
7215 NVME_PMRCAP_SET_CMSS(pmrcap
, 1);
7216 stl_le_p(&n
->bar
.pmrcap
, pmrcap
);
7218 pci_register_bar(pci_dev
, NVME_PMR_BIR
,
7219 PCI_BASE_ADDRESS_SPACE_MEMORY
|
7220 PCI_BASE_ADDRESS_MEM_TYPE_64
|
7221 PCI_BASE_ADDRESS_MEM_PREFETCH
, &n
->pmr
.dev
->mr
);
7223 memory_region_set_enabled(&n
->pmr
.dev
->mr
, false);
7226 static uint64_t nvme_bar_size(unsigned total_queues
, unsigned total_irqs
,
7227 unsigned *msix_table_offset
,
7228 unsigned *msix_pba_offset
)
7230 uint64_t bar_size
, msix_table_size
, msix_pba_size
;
7232 bar_size
= sizeof(NvmeBar
) + 2 * total_queues
* NVME_DB_SIZE
;
7233 bar_size
= QEMU_ALIGN_UP(bar_size
, 4 * KiB
);
7235 if (msix_table_offset
) {
7236 *msix_table_offset
= bar_size
;
7239 msix_table_size
= PCI_MSIX_ENTRY_SIZE
* total_irqs
;
7240 bar_size
+= msix_table_size
;
7241 bar_size
= QEMU_ALIGN_UP(bar_size
, 4 * KiB
);
7243 if (msix_pba_offset
) {
7244 *msix_pba_offset
= bar_size
;
7247 msix_pba_size
= QEMU_ALIGN_UP(total_irqs
, 64) / 8;
7248 bar_size
+= msix_pba_size
;
7250 bar_size
= pow2ceil(bar_size
);
7254 static void nvme_init_sriov(NvmeCtrl
*n
, PCIDevice
*pci_dev
, uint16_t offset
)
7256 uint16_t vf_dev_id
= n
->params
.use_intel_id
?
7257 PCI_DEVICE_ID_INTEL_NVME
: PCI_DEVICE_ID_REDHAT_NVME
;
7258 NvmePriCtrlCap
*cap
= &n
->pri_ctrl_cap
;
7259 uint64_t bar_size
= nvme_bar_size(le16_to_cpu(cap
->vqfrsm
),
7260 le16_to_cpu(cap
->vifrsm
),
7263 pcie_sriov_pf_init(pci_dev
, offset
, "nvme", vf_dev_id
,
7264 n
->params
.sriov_max_vfs
, n
->params
.sriov_max_vfs
,
7265 NVME_VF_OFFSET
, NVME_VF_STRIDE
);
7267 pcie_sriov_pf_init_vf_bar(pci_dev
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
|
7268 PCI_BASE_ADDRESS_MEM_TYPE_64
, bar_size
);
7271 static int nvme_add_pm_capability(PCIDevice
*pci_dev
, uint8_t offset
)
7276 ret
= pci_add_capability(pci_dev
, PCI_CAP_ID_PM
, offset
,
7277 PCI_PM_SIZEOF
, &err
);
7279 error_report_err(err
);
7283 pci_set_word(pci_dev
->config
+ offset
+ PCI_PM_PMC
,
7284 PCI_PM_CAP_VER_1_2
);
7285 pci_set_word(pci_dev
->config
+ offset
+ PCI_PM_CTRL
,
7286 PCI_PM_CTRL_NO_SOFT_RESET
);
7287 pci_set_word(pci_dev
->wmask
+ offset
+ PCI_PM_CTRL
,
7288 PCI_PM_CTRL_STATE_MASK
);
7293 static bool nvme_init_pci(NvmeCtrl
*n
, PCIDevice
*pci_dev
, Error
**errp
)
7296 uint8_t *pci_conf
= pci_dev
->config
;
7298 unsigned msix_table_offset
, msix_pba_offset
;
7301 pci_conf
[PCI_INTERRUPT_PIN
] = 1;
7302 pci_config_set_prog_interface(pci_conf
, 0x2);
7304 if (n
->params
.use_intel_id
) {
7305 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_INTEL
);
7306 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_INTEL_NVME
);
7308 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_REDHAT
);
7309 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_REDHAT_NVME
);
7312 pci_config_set_class(pci_conf
, PCI_CLASS_STORAGE_EXPRESS
);
7313 nvme_add_pm_capability(pci_dev
, 0x60);
7314 pcie_endpoint_cap_init(pci_dev
, 0x80);
7315 pcie_cap_flr_init(pci_dev
);
7316 if (n
->params
.sriov_max_vfs
) {
7317 pcie_ari_init(pci_dev
, 0x100, 1);
7320 /* add one to max_ioqpairs to account for the admin queue pair */
7321 bar_size
= nvme_bar_size(n
->params
.max_ioqpairs
+ 1, n
->params
.msix_qsize
,
7322 &msix_table_offset
, &msix_pba_offset
);
7324 memory_region_init(&n
->bar0
, OBJECT(n
), "nvme-bar0", bar_size
);
7325 memory_region_init_io(&n
->iomem
, OBJECT(n
), &nvme_mmio_ops
, n
, "nvme",
7327 memory_region_add_subregion(&n
->bar0
, 0, &n
->iomem
);
7329 if (pci_is_vf(pci_dev
)) {
7330 pcie_sriov_vf_register_bar(pci_dev
, 0, &n
->bar0
);
7332 pci_register_bar(pci_dev
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
|
7333 PCI_BASE_ADDRESS_MEM_TYPE_64
, &n
->bar0
);
7335 ret
= msix_init(pci_dev
, n
->params
.msix_qsize
,
7336 &n
->bar0
, 0, msix_table_offset
,
7337 &n
->bar0
, 0, msix_pba_offset
, 0, errp
);
7338 if (ret
== -ENOTSUP
) {
7339 /* report that msix is not supported, but do not error out */
7340 warn_report_err(*errp
);
7342 } else if (ret
< 0) {
7343 /* propagate error to caller */
7347 nvme_update_msixcap_ts(pci_dev
, n
->conf_msix_qsize
);
7349 if (n
->params
.cmb_size_mb
) {
7350 nvme_init_cmb(n
, pci_dev
);
7354 nvme_init_pmr(n
, pci_dev
);
7357 if (!pci_is_vf(pci_dev
) && n
->params
.sriov_max_vfs
) {
7358 nvme_init_sriov(n
, pci_dev
, 0x120);
7364 static void nvme_init_subnqn(NvmeCtrl
*n
)
7366 NvmeSubsystem
*subsys
= n
->subsys
;
7367 NvmeIdCtrl
*id
= &n
->id_ctrl
;
7370 snprintf((char *)id
->subnqn
, sizeof(id
->subnqn
),
7371 "nqn.2019-08.org.qemu:%s", n
->params
.serial
);
7373 pstrcpy((char *)id
->subnqn
, sizeof(id
->subnqn
), (char*)subsys
->subnqn
);
7377 static void nvme_init_ctrl(NvmeCtrl
*n
, PCIDevice
*pci_dev
)
7379 NvmeIdCtrl
*id
= &n
->id_ctrl
;
7380 uint8_t *pci_conf
= pci_dev
->config
;
7381 uint64_t cap
= ldq_le_p(&n
->bar
.cap
);
7382 NvmeSecCtrlEntry
*sctrl
= nvme_sctrl(n
);
7384 id
->vid
= cpu_to_le16(pci_get_word(pci_conf
+ PCI_VENDOR_ID
));
7385 id
->ssvid
= cpu_to_le16(pci_get_word(pci_conf
+ PCI_SUBSYSTEM_VENDOR_ID
));
7386 strpadcpy((char *)id
->mn
, sizeof(id
->mn
), "QEMU NVMe Ctrl", ' ');
7387 strpadcpy((char *)id
->fr
, sizeof(id
->fr
), QEMU_VERSION
, ' ');
7388 strpadcpy((char *)id
->sn
, sizeof(id
->sn
), n
->params
.serial
, ' ');
7390 id
->cntlid
= cpu_to_le16(n
->cntlid
);
7392 id
->oaes
= cpu_to_le32(NVME_OAES_NS_ATTR
);
7393 id
->ctratt
|= cpu_to_le32(NVME_CTRATT_ELBAS
);
7397 if (n
->params
.use_intel_id
) {
7407 id
->mdts
= n
->params
.mdts
;
7408 id
->ver
= cpu_to_le32(NVME_SPEC_VER
);
7410 cpu_to_le16(NVME_OACS_NS_MGMT
| NVME_OACS_FORMAT
| NVME_OACS_DBBUF
);
7411 id
->cntrltype
= 0x1;
7414 * Because the controller always completes the Abort command immediately,
7415 * there can never be more than one concurrently executing Abort command,
7416 * so this value is never used for anything. Note that there can easily be
7417 * many Abort commands in the queues, but they are not considered
7418 * "executing" until processed by nvme_abort.
7420 * The specification recommends a value of 3 for Abort Command Limit (four
7421 * concurrently outstanding Abort commands), so lets use that though it is
7425 id
->aerl
= n
->params
.aerl
;
7426 id
->frmw
= (NVME_NUM_FW_SLOTS
<< 1) | NVME_FRMW_SLOT1_RO
;
7427 id
->lpa
= NVME_LPA_NS_SMART
| NVME_LPA_CSE
| NVME_LPA_EXTENDED
;
7429 /* recommended default value (~70 C) */
7430 id
->wctemp
= cpu_to_le16(NVME_TEMPERATURE_WARNING
);
7431 id
->cctemp
= cpu_to_le16(NVME_TEMPERATURE_CRITICAL
);
7433 id
->sqes
= (0x6 << 4) | 0x6;
7434 id
->cqes
= (0x4 << 4) | 0x4;
7435 id
->nn
= cpu_to_le32(NVME_MAX_NAMESPACES
);
7436 id
->oncs
= cpu_to_le16(NVME_ONCS_WRITE_ZEROES
| NVME_ONCS_TIMESTAMP
|
7437 NVME_ONCS_FEATURES
| NVME_ONCS_DSM
|
7438 NVME_ONCS_COMPARE
| NVME_ONCS_COPY
);
7441 * NOTE: If this device ever supports a command set that does NOT use 0x0
7442 * as a Flush-equivalent operation, support for the broadcast NSID in Flush
7443 * should probably be removed.
7445 * See comment in nvme_io_cmd.
7447 id
->vwc
= NVME_VWC_NSID_BROADCAST_SUPPORT
| NVME_VWC_PRESENT
;
7449 id
->ocfs
= cpu_to_le16(NVME_OCFS_COPY_FORMAT_0
| NVME_OCFS_COPY_FORMAT_1
);
7450 id
->sgls
= cpu_to_le32(NVME_CTRL_SGLS_SUPPORT_NO_ALIGN
);
7452 nvme_init_subnqn(n
);
7454 id
->psd
[0].mp
= cpu_to_le16(0x9c4);
7455 id
->psd
[0].enlat
= cpu_to_le32(0x10);
7456 id
->psd
[0].exlat
= cpu_to_le32(0x4);
7459 id
->cmic
|= NVME_CMIC_MULTI_CTRL
;
7462 NVME_CAP_SET_MQES(cap
, 0x7ff);
7463 NVME_CAP_SET_CQR(cap
, 1);
7464 NVME_CAP_SET_TO(cap
, 0xf);
7465 NVME_CAP_SET_CSS(cap
, NVME_CAP_CSS_NVM
);
7466 NVME_CAP_SET_CSS(cap
, NVME_CAP_CSS_CSI_SUPP
);
7467 NVME_CAP_SET_CSS(cap
, NVME_CAP_CSS_ADMIN_ONLY
);
7468 NVME_CAP_SET_MPSMAX(cap
, 4);
7469 NVME_CAP_SET_CMBS(cap
, n
->params
.cmb_size_mb
? 1 : 0);
7470 NVME_CAP_SET_PMRS(cap
, n
->pmr
.dev
? 1 : 0);
7471 stq_le_p(&n
->bar
.cap
, cap
);
7473 stl_le_p(&n
->bar
.vs
, NVME_SPEC_VER
);
7474 n
->bar
.intmc
= n
->bar
.intms
= 0;
7476 if (pci_is_vf(pci_dev
) && !sctrl
->scs
) {
7477 stl_le_p(&n
->bar
.csts
, NVME_CSTS_FAILED
);
7481 static int nvme_init_subsys(NvmeCtrl
*n
, Error
**errp
)
7489 cntlid
= nvme_subsys_register_ctrl(n
, errp
);
7499 void nvme_attach_ns(NvmeCtrl
*n
, NvmeNamespace
*ns
)
7501 uint32_t nsid
= ns
->params
.nsid
;
7502 assert(nsid
&& nsid
<= NVME_MAX_NAMESPACES
);
7504 n
->namespaces
[nsid
] = ns
;
7507 n
->dmrsl
= MIN_NON_ZERO(n
->dmrsl
,
7508 BDRV_REQUEST_MAX_BYTES
/ nvme_l2b(ns
, 1));
7511 static void nvme_realize(PCIDevice
*pci_dev
, Error
**errp
)
7513 NvmeCtrl
*n
= NVME(pci_dev
);
7514 DeviceState
*dev
= DEVICE(pci_dev
);
7516 NvmeCtrl
*pn
= NVME(pcie_sriov_get_pf(pci_dev
));
7518 if (pci_is_vf(pci_dev
)) {
7520 * VFs derive settings from the parent. PF's lifespan exceeds
7521 * that of VF's, so it's safe to share params.serial.
7523 memcpy(&n
->params
, &pn
->params
, sizeof(NvmeParams
));
7524 n
->subsys
= pn
->subsys
;
7527 if (!nvme_check_params(n
, errp
)) {
7531 qbus_init(&n
->bus
, sizeof(NvmeBus
), TYPE_NVME_BUS
, dev
, dev
->id
);
7533 if (nvme_init_subsys(n
, errp
)) {
7537 if (!nvme_init_pci(n
, pci_dev
, errp
)) {
7540 nvme_init_ctrl(n
, pci_dev
);
7542 /* setup a namespace if the controller drive property was given */
7543 if (n
->namespace.blkconf
.blk
) {
7545 ns
->params
.nsid
= 1;
7547 if (nvme_ns_setup(ns
, errp
)) {
7551 nvme_attach_ns(n
, ns
);
7555 static void nvme_exit(PCIDevice
*pci_dev
)
7557 NvmeCtrl
*n
= NVME(pci_dev
);
7561 nvme_ctrl_reset(n
, NVME_RESET_FUNCTION
);
7564 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
7571 nvme_subsys_unregister_ctrl(n
->subsys
, n
);
7576 g_free(n
->aer_reqs
);
7578 if (n
->params
.cmb_size_mb
) {
7583 host_memory_backend_set_mapped(n
->pmr
.dev
, false);
7586 if (!pci_is_vf(pci_dev
) && n
->params
.sriov_max_vfs
) {
7587 pcie_sriov_pf_exit(pci_dev
);
7590 msix_uninit(pci_dev
, &n
->bar0
, &n
->bar0
);
7591 memory_region_del_subregion(&n
->bar0
, &n
->iomem
);
7594 static Property nvme_props
[] = {
7595 DEFINE_BLOCK_PROPERTIES(NvmeCtrl
, namespace.blkconf
),
7596 DEFINE_PROP_LINK("pmrdev", NvmeCtrl
, pmr
.dev
, TYPE_MEMORY_BACKEND
,
7597 HostMemoryBackend
*),
7598 DEFINE_PROP_LINK("subsys", NvmeCtrl
, subsys
, TYPE_NVME_SUBSYS
,
7600 DEFINE_PROP_STRING("serial", NvmeCtrl
, params
.serial
),
7601 DEFINE_PROP_UINT32("cmb_size_mb", NvmeCtrl
, params
.cmb_size_mb
, 0),
7602 DEFINE_PROP_UINT32("num_queues", NvmeCtrl
, params
.num_queues
, 0),
7603 DEFINE_PROP_UINT32("max_ioqpairs", NvmeCtrl
, params
.max_ioqpairs
, 64),
7604 DEFINE_PROP_UINT16("msix_qsize", NvmeCtrl
, params
.msix_qsize
, 65),
7605 DEFINE_PROP_UINT8("aerl", NvmeCtrl
, params
.aerl
, 3),
7606 DEFINE_PROP_UINT32("aer_max_queued", NvmeCtrl
, params
.aer_max_queued
, 64),
7607 DEFINE_PROP_UINT8("mdts", NvmeCtrl
, params
.mdts
, 7),
7608 DEFINE_PROP_UINT8("vsl", NvmeCtrl
, params
.vsl
, 7),
7609 DEFINE_PROP_BOOL("use-intel-id", NvmeCtrl
, params
.use_intel_id
, false),
7610 DEFINE_PROP_BOOL("legacy-cmb", NvmeCtrl
, params
.legacy_cmb
, false),
7611 DEFINE_PROP_BOOL("ioeventfd", NvmeCtrl
, params
.ioeventfd
, false),
7612 DEFINE_PROP_UINT8("zoned.zasl", NvmeCtrl
, params
.zasl
, 0),
7613 DEFINE_PROP_BOOL("zoned.auto_transition", NvmeCtrl
,
7614 params
.auto_transition_zones
, true),
7615 DEFINE_PROP_UINT8("sriov_max_vfs", NvmeCtrl
, params
.sriov_max_vfs
, 0),
7616 DEFINE_PROP_UINT16("sriov_vq_flexible", NvmeCtrl
,
7617 params
.sriov_vq_flexible
, 0),
7618 DEFINE_PROP_UINT16("sriov_vi_flexible", NvmeCtrl
,
7619 params
.sriov_vi_flexible
, 0),
7620 DEFINE_PROP_UINT8("sriov_max_vi_per_vf", NvmeCtrl
,
7621 params
.sriov_max_vi_per_vf
, 0),
7622 DEFINE_PROP_UINT8("sriov_max_vq_per_vf", NvmeCtrl
,
7623 params
.sriov_max_vq_per_vf
, 0),
7624 DEFINE_PROP_END_OF_LIST(),
7627 static void nvme_get_smart_warning(Object
*obj
, Visitor
*v
, const char *name
,
7628 void *opaque
, Error
**errp
)
7630 NvmeCtrl
*n
= NVME(obj
);
7631 uint8_t value
= n
->smart_critical_warning
;
7633 visit_type_uint8(v
, name
, &value
, errp
);
7636 static void nvme_set_smart_warning(Object
*obj
, Visitor
*v
, const char *name
,
7637 void *opaque
, Error
**errp
)
7639 NvmeCtrl
*n
= NVME(obj
);
7640 uint8_t value
, old_value
, cap
= 0, index
, event
;
7642 if (!visit_type_uint8(v
, name
, &value
, errp
)) {
7646 cap
= NVME_SMART_SPARE
| NVME_SMART_TEMPERATURE
| NVME_SMART_RELIABILITY
7647 | NVME_SMART_MEDIA_READ_ONLY
| NVME_SMART_FAILED_VOLATILE_MEDIA
;
7648 if (NVME_CAP_PMRS(ldq_le_p(&n
->bar
.cap
))) {
7649 cap
|= NVME_SMART_PMR_UNRELIABLE
;
7652 if ((value
& cap
) != value
) {
7653 error_setg(errp
, "unsupported smart critical warning bits: 0x%x",
7658 old_value
= n
->smart_critical_warning
;
7659 n
->smart_critical_warning
= value
;
7661 /* only inject new bits of smart critical warning */
7662 for (index
= 0; index
< NVME_SMART_WARN_MAX
; index
++) {
7664 if (value
& ~old_value
& event
)
7665 nvme_smart_event(n
, event
);
7669 static void nvme_pci_reset(DeviceState
*qdev
)
7671 PCIDevice
*pci_dev
= PCI_DEVICE(qdev
);
7672 NvmeCtrl
*n
= NVME(pci_dev
);
7674 trace_pci_nvme_pci_reset();
7675 nvme_ctrl_reset(n
, NVME_RESET_FUNCTION
);
7678 static void nvme_sriov_pre_write_ctrl(PCIDevice
*dev
, uint32_t address
,
7679 uint32_t val
, int len
)
7681 NvmeCtrl
*n
= NVME(dev
);
7682 NvmeSecCtrlEntry
*sctrl
;
7683 uint16_t sriov_cap
= dev
->exp
.sriov_cap
;
7684 uint32_t off
= address
- sriov_cap
;
7691 if (range_covers_byte(off
, len
, PCI_SRIOV_CTRL
)) {
7692 if (!(val
& PCI_SRIOV_CTRL_VFE
)) {
7693 num_vfs
= pci_get_word(dev
->config
+ sriov_cap
+ PCI_SRIOV_NUM_VF
);
7694 for (i
= 0; i
< num_vfs
; i
++) {
7695 sctrl
= &n
->sec_ctrl_list
.sec
[i
];
7696 nvme_virt_set_state(n
, le16_to_cpu(sctrl
->scid
), false);
7702 static void nvme_pci_write_config(PCIDevice
*dev
, uint32_t address
,
7703 uint32_t val
, int len
)
7705 nvme_sriov_pre_write_ctrl(dev
, address
, val
, len
);
7706 pci_default_write_config(dev
, address
, val
, len
);
7707 pcie_cap_flr_write_config(dev
, address
, val
, len
);
7710 static const VMStateDescription nvme_vmstate
= {
7715 static void nvme_class_init(ObjectClass
*oc
, void *data
)
7717 DeviceClass
*dc
= DEVICE_CLASS(oc
);
7718 PCIDeviceClass
*pc
= PCI_DEVICE_CLASS(oc
);
7720 pc
->realize
= nvme_realize
;
7721 pc
->config_write
= nvme_pci_write_config
;
7722 pc
->exit
= nvme_exit
;
7723 pc
->class_id
= PCI_CLASS_STORAGE_EXPRESS
;
7726 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
7727 dc
->desc
= "Non-Volatile Memory Express";
7728 device_class_set_props(dc
, nvme_props
);
7729 dc
->vmsd
= &nvme_vmstate
;
7730 dc
->reset
= nvme_pci_reset
;
7733 static void nvme_instance_init(Object
*obj
)
7735 NvmeCtrl
*n
= NVME(obj
);
7737 device_add_bootindex_property(obj
, &n
->namespace.blkconf
.bootindex
,
7738 "bootindex", "/namespace@1,0",
7741 object_property_add(obj
, "smart_critical_warning", "uint8",
7742 nvme_get_smart_warning
,
7743 nvme_set_smart_warning
, NULL
, NULL
);
7746 static const TypeInfo nvme_info
= {
7748 .parent
= TYPE_PCI_DEVICE
,
7749 .instance_size
= sizeof(NvmeCtrl
),
7750 .instance_init
= nvme_instance_init
,
7751 .class_init
= nvme_class_init
,
7752 .interfaces
= (InterfaceInfo
[]) {
7753 { INTERFACE_PCIE_DEVICE
},
7758 static const TypeInfo nvme_bus_info
= {
7759 .name
= TYPE_NVME_BUS
,
7761 .instance_size
= sizeof(NvmeBus
),
7764 static void nvme_register_types(void)
7766 type_register_static(&nvme_info
);
7767 type_register_static(&nvme_bus_info
);
7770 type_init(nvme_register_types
)