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>,shared=<true|false[optional]>, \
47 * detached=<true|false[optional]>, \
48 * zoned.zone_size=<N[optional]>, \
49 * zoned.zone_capacity=<N[optional]>, \
50 * zoned.descr_ext_size=<N[optional]>, \
51 * zoned.max_active=<N[optional]>, \
52 * zoned.max_open=<N[optional]>, \
53 * zoned.cross_read=<true|false[optional]>
55 * Note cmb_size_mb denotes size of CMB in MB. CMB is assumed to be at
56 * offset 0 in BAR2 and supports only WDS, RDS and SQS for now. By default, the
57 * device will use the "v1.4 CMB scheme" - use the `legacy-cmb` parameter to
58 * always enable the CMBLOC and CMBSZ registers (v1.3 behavior).
60 * Enabling pmr emulation can be achieved by pointing to memory-backend-file.
62 * -object memory-backend-file,id=<mem_id>,share=on,mem-path=<file_path>, \
63 * size=<size> .... -device nvme,...,pmrdev=<mem_id>
65 * The PMR will use BAR 4/5 exclusively.
67 * To place controller(s) and namespace(s) to a subsystem, then provide
68 * nvme-subsys device as above.
70 * nvme subsystem device parameters
71 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73 * This parameter provides the `<nqn_id>` part of the string
74 * `nqn.2019-08.org.qemu:<nqn_id>` which will be reported in the SUBNQN field
75 * of subsystem controllers. Note that `<nqn_id>` should be unique per
76 * subsystem, but this is not enforced by QEMU. If not specified, it will
77 * default to the value of the `id` parameter (`<subsys_id>`).
79 * nvme device parameters
80 * ~~~~~~~~~~~~~~~~~~~~~~
82 * Specifying this parameter attaches the controller to the subsystem and
83 * the SUBNQN field in the controller will report the NQN of the subsystem
84 * device. This also enables multi controller capability represented in
85 * Identify Controller data structure in CMIC (Controller Multi-path I/O and
86 * Namespace Sharing Capabilities).
89 * The Asynchronous Event Request Limit (AERL). Indicates the maximum number
90 * of concurrently outstanding Asynchronous Event Request commands support
91 * by the controller. This is a 0's based value.
94 * This is the maximum number of events that the device will enqueue for
95 * completion when there are no outstanding AERs. When the maximum number of
96 * enqueued events are reached, subsequent events will be dropped.
99 * Indicates the maximum data transfer size for a command that transfers data
100 * between host-accessible memory and the controller. The value is specified
101 * as a power of two (2^n) and is in units of the minimum memory page size
102 * (CAP.MPSMIN). The default value is 7 (i.e. 512 KiB).
105 * Indicates the maximum data size limit for the Verify command. Like `mdts`,
106 * this value is specified as a power of two (2^n) and is in units of the
107 * minimum memory page size (CAP.MPSMIN). The default value is 7 (i.e. 512
111 * Indicates the maximum data transfer size for the Zone Append command. Like
112 * `mdts`, the value is specified as a power of two (2^n) and is in units of
113 * the minimum memory page size (CAP.MPSMIN). The default value is 0 (i.e.
114 * defaulting to the value of `mdts`).
116 * - `zoned.auto_transition`
117 * Indicates if zones in zone state implicitly opened can be automatically
118 * transitioned to zone state closed for resource management purposes.
122 * Indicates the maximum number of PCIe virtual functions supported
123 * by the controller. The default value is 0. Specifying a non-zero value
124 * enables reporting of both SR-IOV and ARI capabilities by the NVMe device.
125 * Virtual function controllers will not report SR-IOV capability.
127 * NOTE: Single Root I/O Virtualization support is experimental.
128 * All the related parameters may be subject to change.
130 * - `sriov_vq_flexible`
131 * Indicates the total number of flexible queue resources assignable to all
132 * the secondary controllers. Implicitly sets the number of primary
133 * controller's private resources to `(max_ioqpairs - sriov_vq_flexible)`.
135 * - `sriov_vi_flexible`
136 * Indicates the total number of flexible interrupt resources assignable to
137 * all the secondary controllers. Implicitly sets the number of primary
138 * controller's private resources to `(msix_qsize - sriov_vi_flexible)`.
140 * - `sriov_max_vi_per_vf`
141 * Indicates the maximum number of virtual interrupt resources assignable
142 * to a secondary controller. The default 0 resolves to
143 * `(sriov_vi_flexible / sriov_max_vfs)`.
145 * - `sriov_max_vq_per_vf`
146 * Indicates the maximum number of virtual queue resources assignable to
147 * a secondary controller. The default 0 resolves to
148 * `(sriov_vq_flexible / sriov_max_vfs)`.
150 * nvme namespace device parameters
151 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
153 * When the parent nvme device (as defined explicitly by the 'bus' parameter
154 * or implicitly by the most recently defined NvmeBus) is linked to an
155 * nvme-subsys device, the namespace will be attached to all controllers in
156 * the subsystem. If set to 'off' (the default), the namespace will remain a
157 * private namespace and may only be attached to a single controller at a
161 * This parameter is only valid together with the `subsys` parameter. If left
162 * at the default value (`false/off`), the namespace will be attached to all
163 * controllers in the NVMe subsystem at boot-up. If set to `true/on`, the
164 * namespace will be available in the subsystem but not attached to any
167 * Setting `zoned` to true selects Zoned Command Set at the namespace.
168 * In this case, the following namespace properties are available to configure
170 * zoned.zone_size=<zone size in bytes, default: 128MiB>
171 * The number may be followed by K, M, G as in kilo-, mega- or giga-.
173 * zoned.zone_capacity=<zone capacity in bytes, default: zone size>
174 * The value 0 (default) forces zone capacity to be the same as zone
175 * size. The value of this property may not exceed zone size.
177 * zoned.descr_ext_size=<zone descriptor extension size, default 0>
178 * This value needs to be specified in 64B units. If it is zero,
179 * namespace(s) will not support zone descriptor extensions.
181 * zoned.max_active=<Maximum Active Resources (zones), default: 0>
182 * The default value means there is no limit to the number of
183 * concurrently active zones.
185 * zoned.max_open=<Maximum Open Resources (zones), default: 0>
186 * The default value means there is no limit to the number of
187 * concurrently open zones.
189 * zoned.cross_read=<enable RAZB, default: false>
190 * Setting this property to true enables Read Across Zone Boundaries.
193 #include "qemu/osdep.h"
194 #include "qemu/cutils.h"
195 #include "qemu/error-report.h"
196 #include "qemu/log.h"
197 #include "qemu/units.h"
198 #include "qemu/range.h"
199 #include "qapi/error.h"
200 #include "qapi/visitor.h"
201 #include "sysemu/sysemu.h"
202 #include "sysemu/block-backend.h"
203 #include "sysemu/hostmem.h"
204 #include "hw/pci/msix.h"
205 #include "hw/pci/pcie_sriov.h"
206 #include "migration/vmstate.h"
212 #define NVME_MAX_IOQPAIRS 0xffff
213 #define NVME_DB_SIZE 4
214 #define NVME_SPEC_VER 0x00010400
215 #define NVME_CMB_BIR 2
216 #define NVME_PMR_BIR 4
217 #define NVME_TEMPERATURE 0x143
218 #define NVME_TEMPERATURE_WARNING 0x157
219 #define NVME_TEMPERATURE_CRITICAL 0x175
220 #define NVME_NUM_FW_SLOTS 1
221 #define NVME_DEFAULT_MAX_ZA_SIZE (128 * KiB)
222 #define NVME_MAX_VFS 127
223 #define NVME_VF_RES_GRANULARITY 1
224 #define NVME_VF_OFFSET 0x1
225 #define NVME_VF_STRIDE 1
227 #define NVME_GUEST_ERR(trace, fmt, ...) \
229 (trace_##trace)(__VA_ARGS__); \
230 qemu_log_mask(LOG_GUEST_ERROR, #trace \
231 " in %s: " fmt "\n", __func__, ## __VA_ARGS__); \
234 static const bool nvme_feature_support
[NVME_FID_MAX
] = {
235 [NVME_ARBITRATION
] = true,
236 [NVME_POWER_MANAGEMENT
] = true,
237 [NVME_TEMPERATURE_THRESHOLD
] = true,
238 [NVME_ERROR_RECOVERY
] = true,
239 [NVME_VOLATILE_WRITE_CACHE
] = true,
240 [NVME_NUMBER_OF_QUEUES
] = true,
241 [NVME_INTERRUPT_COALESCING
] = true,
242 [NVME_INTERRUPT_VECTOR_CONF
] = true,
243 [NVME_WRITE_ATOMICITY
] = true,
244 [NVME_ASYNCHRONOUS_EVENT_CONF
] = true,
245 [NVME_TIMESTAMP
] = true,
246 [NVME_HOST_BEHAVIOR_SUPPORT
] = true,
247 [NVME_COMMAND_SET_PROFILE
] = true,
248 [NVME_FDP_MODE
] = true,
249 [NVME_FDP_EVENTS
] = true,
252 static const uint32_t nvme_feature_cap
[NVME_FID_MAX
] = {
253 [NVME_TEMPERATURE_THRESHOLD
] = NVME_FEAT_CAP_CHANGE
,
254 [NVME_ERROR_RECOVERY
] = NVME_FEAT_CAP_CHANGE
| NVME_FEAT_CAP_NS
,
255 [NVME_VOLATILE_WRITE_CACHE
] = NVME_FEAT_CAP_CHANGE
,
256 [NVME_NUMBER_OF_QUEUES
] = NVME_FEAT_CAP_CHANGE
,
257 [NVME_ASYNCHRONOUS_EVENT_CONF
] = NVME_FEAT_CAP_CHANGE
,
258 [NVME_TIMESTAMP
] = NVME_FEAT_CAP_CHANGE
,
259 [NVME_HOST_BEHAVIOR_SUPPORT
] = NVME_FEAT_CAP_CHANGE
,
260 [NVME_COMMAND_SET_PROFILE
] = NVME_FEAT_CAP_CHANGE
,
261 [NVME_FDP_MODE
] = NVME_FEAT_CAP_CHANGE
,
262 [NVME_FDP_EVENTS
] = NVME_FEAT_CAP_CHANGE
| NVME_FEAT_CAP_NS
,
265 static const uint32_t nvme_cse_acs
[256] = {
266 [NVME_ADM_CMD_DELETE_SQ
] = NVME_CMD_EFF_CSUPP
,
267 [NVME_ADM_CMD_CREATE_SQ
] = NVME_CMD_EFF_CSUPP
,
268 [NVME_ADM_CMD_GET_LOG_PAGE
] = NVME_CMD_EFF_CSUPP
,
269 [NVME_ADM_CMD_DELETE_CQ
] = NVME_CMD_EFF_CSUPP
,
270 [NVME_ADM_CMD_CREATE_CQ
] = NVME_CMD_EFF_CSUPP
,
271 [NVME_ADM_CMD_IDENTIFY
] = NVME_CMD_EFF_CSUPP
,
272 [NVME_ADM_CMD_ABORT
] = NVME_CMD_EFF_CSUPP
,
273 [NVME_ADM_CMD_SET_FEATURES
] = NVME_CMD_EFF_CSUPP
,
274 [NVME_ADM_CMD_GET_FEATURES
] = NVME_CMD_EFF_CSUPP
,
275 [NVME_ADM_CMD_ASYNC_EV_REQ
] = NVME_CMD_EFF_CSUPP
,
276 [NVME_ADM_CMD_NS_ATTACHMENT
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_NIC
,
277 [NVME_ADM_CMD_VIRT_MNGMT
] = NVME_CMD_EFF_CSUPP
,
278 [NVME_ADM_CMD_DBBUF_CONFIG
] = NVME_CMD_EFF_CSUPP
,
279 [NVME_ADM_CMD_FORMAT_NVM
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
280 [NVME_ADM_CMD_DIRECTIVE_RECV
] = NVME_CMD_EFF_CSUPP
,
281 [NVME_ADM_CMD_DIRECTIVE_SEND
] = NVME_CMD_EFF_CSUPP
,
284 static const uint32_t nvme_cse_iocs_none
[256];
286 static const uint32_t nvme_cse_iocs_nvm
[256] = {
287 [NVME_CMD_FLUSH
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
288 [NVME_CMD_WRITE_ZEROES
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
289 [NVME_CMD_WRITE
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
290 [NVME_CMD_READ
] = NVME_CMD_EFF_CSUPP
,
291 [NVME_CMD_DSM
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
292 [NVME_CMD_VERIFY
] = NVME_CMD_EFF_CSUPP
,
293 [NVME_CMD_COPY
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
294 [NVME_CMD_COMPARE
] = NVME_CMD_EFF_CSUPP
,
295 [NVME_CMD_IO_MGMT_RECV
] = NVME_CMD_EFF_CSUPP
,
296 [NVME_CMD_IO_MGMT_SEND
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
299 static const uint32_t nvme_cse_iocs_zoned
[256] = {
300 [NVME_CMD_FLUSH
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
301 [NVME_CMD_WRITE_ZEROES
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
302 [NVME_CMD_WRITE
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
303 [NVME_CMD_READ
] = NVME_CMD_EFF_CSUPP
,
304 [NVME_CMD_DSM
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
305 [NVME_CMD_VERIFY
] = NVME_CMD_EFF_CSUPP
,
306 [NVME_CMD_COPY
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
307 [NVME_CMD_COMPARE
] = NVME_CMD_EFF_CSUPP
,
308 [NVME_CMD_ZONE_APPEND
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
309 [NVME_CMD_ZONE_MGMT_SEND
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
310 [NVME_CMD_ZONE_MGMT_RECV
] = NVME_CMD_EFF_CSUPP
,
313 static void nvme_process_sq(void *opaque
);
314 static void nvme_ctrl_reset(NvmeCtrl
*n
, NvmeResetType rst
);
315 static inline uint64_t nvme_get_timestamp(const NvmeCtrl
*n
);
317 static uint16_t nvme_sqid(NvmeRequest
*req
)
319 return le16_to_cpu(req
->sq
->sqid
);
322 static inline uint16_t nvme_make_pid(NvmeNamespace
*ns
, uint16_t rg
,
325 uint16_t rgif
= ns
->endgrp
->fdp
.rgif
;
331 return (rg
<< (16 - rgif
)) | ph
;
334 static inline bool nvme_ph_valid(NvmeNamespace
*ns
, uint16_t ph
)
336 return ph
< ns
->fdp
.nphs
;
339 static inline bool nvme_rg_valid(NvmeEnduranceGroup
*endgrp
, uint16_t rg
)
341 return rg
< endgrp
->fdp
.nrg
;
344 static inline uint16_t nvme_pid2ph(NvmeNamespace
*ns
, uint16_t pid
)
346 uint16_t rgif
= ns
->endgrp
->fdp
.rgif
;
352 return pid
& ((1 << (15 - rgif
)) - 1);
355 static inline uint16_t nvme_pid2rg(NvmeNamespace
*ns
, uint16_t pid
)
357 uint16_t rgif
= ns
->endgrp
->fdp
.rgif
;
363 return pid
>> (16 - rgif
);
366 static inline bool nvme_parse_pid(NvmeNamespace
*ns
, uint16_t pid
,
367 uint16_t *ph
, uint16_t *rg
)
369 *rg
= nvme_pid2rg(ns
, pid
);
370 *ph
= nvme_pid2ph(ns
, pid
);
372 return nvme_ph_valid(ns
, *ph
) && nvme_rg_valid(ns
->endgrp
, *rg
);
375 static void nvme_assign_zone_state(NvmeNamespace
*ns
, NvmeZone
*zone
,
378 if (QTAILQ_IN_USE(zone
, entry
)) {
379 switch (nvme_get_zone_state(zone
)) {
380 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
381 QTAILQ_REMOVE(&ns
->exp_open_zones
, zone
, entry
);
383 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
384 QTAILQ_REMOVE(&ns
->imp_open_zones
, zone
, entry
);
386 case NVME_ZONE_STATE_CLOSED
:
387 QTAILQ_REMOVE(&ns
->closed_zones
, zone
, entry
);
389 case NVME_ZONE_STATE_FULL
:
390 QTAILQ_REMOVE(&ns
->full_zones
, zone
, entry
);
396 nvme_set_zone_state(zone
, state
);
399 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
400 QTAILQ_INSERT_TAIL(&ns
->exp_open_zones
, zone
, entry
);
402 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
403 QTAILQ_INSERT_TAIL(&ns
->imp_open_zones
, zone
, entry
);
405 case NVME_ZONE_STATE_CLOSED
:
406 QTAILQ_INSERT_TAIL(&ns
->closed_zones
, zone
, entry
);
408 case NVME_ZONE_STATE_FULL
:
409 QTAILQ_INSERT_TAIL(&ns
->full_zones
, zone
, entry
);
410 case NVME_ZONE_STATE_READ_ONLY
:
417 static uint16_t nvme_zns_check_resources(NvmeNamespace
*ns
, uint32_t act
,
418 uint32_t opn
, uint32_t zrwa
)
420 if (ns
->params
.max_active_zones
!= 0 &&
421 ns
->nr_active_zones
+ act
> ns
->params
.max_active_zones
) {
422 trace_pci_nvme_err_insuff_active_res(ns
->params
.max_active_zones
);
423 return NVME_ZONE_TOO_MANY_ACTIVE
| NVME_DNR
;
426 if (ns
->params
.max_open_zones
!= 0 &&
427 ns
->nr_open_zones
+ opn
> ns
->params
.max_open_zones
) {
428 trace_pci_nvme_err_insuff_open_res(ns
->params
.max_open_zones
);
429 return NVME_ZONE_TOO_MANY_OPEN
| NVME_DNR
;
432 if (zrwa
> ns
->zns
.numzrwa
) {
433 return NVME_NOZRWA
| NVME_DNR
;
440 * Check if we can open a zone without exceeding open/active limits.
441 * AOR stands for "Active and Open Resources" (see TP 4053 section 2.5).
443 static uint16_t nvme_aor_check(NvmeNamespace
*ns
, uint32_t act
, uint32_t opn
)
445 return nvme_zns_check_resources(ns
, act
, opn
, 0);
448 static NvmeFdpEvent
*nvme_fdp_alloc_event(NvmeCtrl
*n
, NvmeFdpEventBuffer
*ebuf
)
450 NvmeFdpEvent
*ret
= NULL
;
451 bool is_full
= ebuf
->next
== ebuf
->start
&& ebuf
->nelems
;
453 ret
= &ebuf
->events
[ebuf
->next
++];
454 if (unlikely(ebuf
->next
== NVME_FDP_MAX_EVENTS
)) {
458 ebuf
->start
= ebuf
->next
;
463 memset(ret
, 0, sizeof(NvmeFdpEvent
));
464 ret
->timestamp
= nvme_get_timestamp(n
);
469 static inline int log_event(NvmeRuHandle
*ruh
, uint8_t event_type
)
471 return (ruh
->event_filter
>> nvme_fdp_evf_shifts
[event_type
]) & 0x1;
474 static bool nvme_update_ruh(NvmeCtrl
*n
, NvmeNamespace
*ns
, uint16_t pid
)
476 NvmeEnduranceGroup
*endgrp
= ns
->endgrp
;
479 NvmeFdpEvent
*e
= NULL
;
480 uint16_t ph
, rg
, ruhid
;
482 if (!nvme_parse_pid(ns
, pid
, &ph
, &rg
)) {
486 ruhid
= ns
->fdp
.phs
[ph
];
488 ruh
= &endgrp
->fdp
.ruhs
[ruhid
];
492 if (log_event(ruh
, FDP_EVT_RU_NOT_FULLY_WRITTEN
)) {
493 e
= nvme_fdp_alloc_event(n
, &endgrp
->fdp
.host_events
);
494 e
->type
= FDP_EVT_RU_NOT_FULLY_WRITTEN
;
495 e
->flags
= FDPEF_PIV
| FDPEF_NSIDV
| FDPEF_LV
;
496 e
->pid
= cpu_to_le16(pid
);
497 e
->nsid
= cpu_to_le32(ns
->params
.nsid
);
498 e
->rgid
= cpu_to_le16(rg
);
499 e
->ruhid
= cpu_to_le16(ruhid
);
502 /* log (eventual) GC overhead of prematurely swapping the RU */
503 nvme_fdp_stat_inc(&endgrp
->fdp
.mbmw
, nvme_l2b(ns
, ru
->ruamw
));
506 ru
->ruamw
= ruh
->ruamw
;
511 static bool nvme_addr_is_cmb(NvmeCtrl
*n
, hwaddr addr
)
519 lo
= n
->params
.legacy_cmb
? n
->cmb
.mem
.addr
: n
->cmb
.cba
;
520 hi
= lo
+ int128_get64(n
->cmb
.mem
.size
);
522 return addr
>= lo
&& addr
< hi
;
525 static inline void *nvme_addr_to_cmb(NvmeCtrl
*n
, hwaddr addr
)
527 hwaddr base
= n
->params
.legacy_cmb
? n
->cmb
.mem
.addr
: n
->cmb
.cba
;
528 return &n
->cmb
.buf
[addr
- base
];
531 static bool nvme_addr_is_pmr(NvmeCtrl
*n
, hwaddr addr
)
539 hi
= n
->pmr
.cba
+ int128_get64(n
->pmr
.dev
->mr
.size
);
541 return addr
>= n
->pmr
.cba
&& addr
< hi
;
544 static inline void *nvme_addr_to_pmr(NvmeCtrl
*n
, hwaddr addr
)
546 return memory_region_get_ram_ptr(&n
->pmr
.dev
->mr
) + (addr
- n
->pmr
.cba
);
549 static inline bool nvme_addr_is_iomem(NvmeCtrl
*n
, hwaddr addr
)
554 * The purpose of this check is to guard against invalid "local" access to
555 * the iomem (i.e. controller registers). Thus, we check against the range
556 * covered by the 'bar0' MemoryRegion since that is currently composed of
557 * two subregions (the NVMe "MBAR" and the MSI-X table/pba). Note, however,
558 * that if the device model is ever changed to allow the CMB to be located
559 * in BAR0 as well, then this must be changed.
562 hi
= lo
+ int128_get64(n
->bar0
.size
);
564 return addr
>= lo
&& addr
< hi
;
567 static int nvme_addr_read(NvmeCtrl
*n
, hwaddr addr
, void *buf
, int size
)
569 hwaddr hi
= addr
+ size
- 1;
574 if (n
->bar
.cmbsz
&& nvme_addr_is_cmb(n
, addr
) && nvme_addr_is_cmb(n
, hi
)) {
575 memcpy(buf
, nvme_addr_to_cmb(n
, addr
), size
);
579 if (nvme_addr_is_pmr(n
, addr
) && nvme_addr_is_pmr(n
, hi
)) {
580 memcpy(buf
, nvme_addr_to_pmr(n
, addr
), size
);
584 return pci_dma_read(PCI_DEVICE(n
), addr
, buf
, size
);
587 static int nvme_addr_write(NvmeCtrl
*n
, hwaddr addr
, const void *buf
, int size
)
589 hwaddr hi
= addr
+ size
- 1;
594 if (n
->bar
.cmbsz
&& nvme_addr_is_cmb(n
, addr
) && nvme_addr_is_cmb(n
, hi
)) {
595 memcpy(nvme_addr_to_cmb(n
, addr
), buf
, size
);
599 if (nvme_addr_is_pmr(n
, addr
) && nvme_addr_is_pmr(n
, hi
)) {
600 memcpy(nvme_addr_to_pmr(n
, addr
), buf
, size
);
604 return pci_dma_write(PCI_DEVICE(n
), addr
, buf
, size
);
607 static bool nvme_nsid_valid(NvmeCtrl
*n
, uint32_t nsid
)
610 (nsid
== NVME_NSID_BROADCAST
|| nsid
<= NVME_MAX_NAMESPACES
);
613 static int nvme_check_sqid(NvmeCtrl
*n
, uint16_t sqid
)
615 return sqid
< n
->conf_ioqpairs
+ 1 && n
->sq
[sqid
] != NULL
? 0 : -1;
618 static int nvme_check_cqid(NvmeCtrl
*n
, uint16_t cqid
)
620 return cqid
< n
->conf_ioqpairs
+ 1 && n
->cq
[cqid
] != NULL
? 0 : -1;
623 static void nvme_inc_cq_tail(NvmeCQueue
*cq
)
626 if (cq
->tail
>= cq
->size
) {
628 cq
->phase
= !cq
->phase
;
632 static void nvme_inc_sq_head(NvmeSQueue
*sq
)
634 sq
->head
= (sq
->head
+ 1) % sq
->size
;
637 static uint8_t nvme_cq_full(NvmeCQueue
*cq
)
639 return (cq
->tail
+ 1) % cq
->size
== cq
->head
;
642 static uint8_t nvme_sq_empty(NvmeSQueue
*sq
)
644 return sq
->head
== sq
->tail
;
647 static void nvme_irq_check(NvmeCtrl
*n
)
649 PCIDevice
*pci
= PCI_DEVICE(n
);
650 uint32_t intms
= ldl_le_p(&n
->bar
.intms
);
652 if (msix_enabled(pci
)) {
655 if (~intms
& n
->irq_status
) {
658 pci_irq_deassert(pci
);
662 static void nvme_irq_assert(NvmeCtrl
*n
, NvmeCQueue
*cq
)
664 PCIDevice
*pci
= PCI_DEVICE(n
);
666 if (cq
->irq_enabled
) {
667 if (msix_enabled(pci
)) {
668 trace_pci_nvme_irq_msix(cq
->vector
);
669 msix_notify(pci
, cq
->vector
);
671 trace_pci_nvme_irq_pin();
672 assert(cq
->vector
< 32);
673 n
->irq_status
|= 1 << cq
->vector
;
677 trace_pci_nvme_irq_masked();
681 static void nvme_irq_deassert(NvmeCtrl
*n
, NvmeCQueue
*cq
)
683 if (cq
->irq_enabled
) {
684 if (msix_enabled(PCI_DEVICE(n
))) {
687 assert(cq
->vector
< 32);
688 if (!n
->cq_pending
) {
689 n
->irq_status
&= ~(1 << cq
->vector
);
696 static void nvme_req_clear(NvmeRequest
*req
)
701 memset(&req
->cqe
, 0x0, sizeof(req
->cqe
));
702 req
->status
= NVME_SUCCESS
;
705 static inline void nvme_sg_init(NvmeCtrl
*n
, NvmeSg
*sg
, bool dma
)
708 pci_dma_sglist_init(&sg
->qsg
, PCI_DEVICE(n
), 0);
709 sg
->flags
= NVME_SG_DMA
;
711 qemu_iovec_init(&sg
->iov
, 0);
714 sg
->flags
|= NVME_SG_ALLOC
;
717 static inline void nvme_sg_unmap(NvmeSg
*sg
)
719 if (!(sg
->flags
& NVME_SG_ALLOC
)) {
723 if (sg
->flags
& NVME_SG_DMA
) {
724 qemu_sglist_destroy(&sg
->qsg
);
726 qemu_iovec_destroy(&sg
->iov
);
729 memset(sg
, 0x0, sizeof(*sg
));
733 * When metadata is transfered as extended LBAs, the DPTR mapped into `sg`
734 * holds both data and metadata. This function splits the data and metadata
735 * into two separate QSG/IOVs.
737 static void nvme_sg_split(NvmeSg
*sg
, NvmeNamespace
*ns
, NvmeSg
*data
,
741 uint32_t trans_len
, count
= ns
->lbasz
;
743 bool dma
= sg
->flags
& NVME_SG_DMA
;
745 size_t sg_len
= dma
? sg
->qsg
.size
: sg
->iov
.size
;
748 assert(sg
->flags
& NVME_SG_ALLOC
);
751 sge_len
= dma
? sg
->qsg
.sg
[sg_idx
].len
: sg
->iov
.iov
[sg_idx
].iov_len
;
753 trans_len
= MIN(sg_len
, count
);
754 trans_len
= MIN(trans_len
, sge_len
- offset
);
758 qemu_sglist_add(&dst
->qsg
, sg
->qsg
.sg
[sg_idx
].base
+ offset
,
761 qemu_iovec_add(&dst
->iov
,
762 sg
->iov
.iov
[sg_idx
].iov_base
+ offset
,
772 dst
= (dst
== data
) ? mdata
: data
;
773 count
= (dst
== data
) ? ns
->lbasz
: ns
->lbaf
.ms
;
776 if (sge_len
== offset
) {
783 static uint16_t nvme_map_addr_cmb(NvmeCtrl
*n
, QEMUIOVector
*iov
, hwaddr addr
,
790 trace_pci_nvme_map_addr_cmb(addr
, len
);
792 if (!nvme_addr_is_cmb(n
, addr
) || !nvme_addr_is_cmb(n
, addr
+ len
- 1)) {
793 return NVME_DATA_TRAS_ERROR
;
796 qemu_iovec_add(iov
, nvme_addr_to_cmb(n
, addr
), len
);
801 static uint16_t nvme_map_addr_pmr(NvmeCtrl
*n
, QEMUIOVector
*iov
, hwaddr addr
,
808 if (!nvme_addr_is_pmr(n
, addr
) || !nvme_addr_is_pmr(n
, addr
+ len
- 1)) {
809 return NVME_DATA_TRAS_ERROR
;
812 qemu_iovec_add(iov
, nvme_addr_to_pmr(n
, addr
), len
);
817 static uint16_t nvme_map_addr(NvmeCtrl
*n
, NvmeSg
*sg
, hwaddr addr
, size_t len
)
819 bool cmb
= false, pmr
= false;
825 trace_pci_nvme_map_addr(addr
, len
);
827 if (nvme_addr_is_iomem(n
, addr
)) {
828 return NVME_DATA_TRAS_ERROR
;
831 if (nvme_addr_is_cmb(n
, addr
)) {
833 } else if (nvme_addr_is_pmr(n
, addr
)) {
838 if (sg
->flags
& NVME_SG_DMA
) {
839 return NVME_INVALID_USE_OF_CMB
| NVME_DNR
;
842 if (sg
->iov
.niov
+ 1 > IOV_MAX
) {
843 goto max_mappings_exceeded
;
847 return nvme_map_addr_cmb(n
, &sg
->iov
, addr
, len
);
849 return nvme_map_addr_pmr(n
, &sg
->iov
, addr
, len
);
853 if (!(sg
->flags
& NVME_SG_DMA
)) {
854 return NVME_INVALID_USE_OF_CMB
| NVME_DNR
;
857 if (sg
->qsg
.nsg
+ 1 > IOV_MAX
) {
858 goto max_mappings_exceeded
;
861 qemu_sglist_add(&sg
->qsg
, addr
, len
);
865 max_mappings_exceeded
:
866 NVME_GUEST_ERR(pci_nvme_ub_too_many_mappings
,
867 "number of mappings exceed 1024");
868 return NVME_INTERNAL_DEV_ERROR
| NVME_DNR
;
871 static inline bool nvme_addr_is_dma(NvmeCtrl
*n
, hwaddr addr
)
873 return !(nvme_addr_is_cmb(n
, addr
) || nvme_addr_is_pmr(n
, addr
));
876 static uint16_t nvme_map_prp(NvmeCtrl
*n
, NvmeSg
*sg
, uint64_t prp1
,
877 uint64_t prp2
, uint32_t len
)
879 hwaddr trans_len
= n
->page_size
- (prp1
% n
->page_size
);
880 trans_len
= MIN(len
, trans_len
);
881 int num_prps
= (len
>> n
->page_bits
) + 1;
885 trace_pci_nvme_map_prp(trans_len
, len
, prp1
, prp2
, num_prps
);
887 nvme_sg_init(n
, sg
, nvme_addr_is_dma(n
, prp1
));
889 status
= nvme_map_addr(n
, sg
, prp1
, trans_len
);
896 if (len
> n
->page_size
) {
897 g_autofree
uint64_t *prp_list
= g_new(uint64_t, n
->max_prp_ents
);
898 uint32_t nents
, prp_trans
;
902 * The first PRP list entry, pointed to by PRP2 may contain offset.
903 * Hence, we need to calculate the number of entries in based on
906 nents
= (n
->page_size
- (prp2
& (n
->page_size
- 1))) >> 3;
907 prp_trans
= MIN(n
->max_prp_ents
, nents
) * sizeof(uint64_t);
908 ret
= nvme_addr_read(n
, prp2
, (void *)prp_list
, prp_trans
);
910 trace_pci_nvme_err_addr_read(prp2
);
911 status
= NVME_DATA_TRAS_ERROR
;
915 uint64_t prp_ent
= le64_to_cpu(prp_list
[i
]);
917 if (i
== nents
- 1 && len
> n
->page_size
) {
918 if (unlikely(prp_ent
& (n
->page_size
- 1))) {
919 trace_pci_nvme_err_invalid_prplist_ent(prp_ent
);
920 status
= NVME_INVALID_PRP_OFFSET
| NVME_DNR
;
925 nents
= (len
+ n
->page_size
- 1) >> n
->page_bits
;
926 nents
= MIN(nents
, n
->max_prp_ents
);
927 prp_trans
= nents
* sizeof(uint64_t);
928 ret
= nvme_addr_read(n
, prp_ent
, (void *)prp_list
,
931 trace_pci_nvme_err_addr_read(prp_ent
);
932 status
= NVME_DATA_TRAS_ERROR
;
935 prp_ent
= le64_to_cpu(prp_list
[i
]);
938 if (unlikely(prp_ent
& (n
->page_size
- 1))) {
939 trace_pci_nvme_err_invalid_prplist_ent(prp_ent
);
940 status
= NVME_INVALID_PRP_OFFSET
| NVME_DNR
;
944 trans_len
= MIN(len
, n
->page_size
);
945 status
= nvme_map_addr(n
, sg
, prp_ent
, trans_len
);
954 if (unlikely(prp2
& (n
->page_size
- 1))) {
955 trace_pci_nvme_err_invalid_prp2_align(prp2
);
956 status
= NVME_INVALID_PRP_OFFSET
| NVME_DNR
;
959 status
= nvme_map_addr(n
, sg
, prp2
, len
);
974 * Map 'nsgld' data descriptors from 'segment'. The function will subtract the
975 * number of bytes mapped in len.
977 static uint16_t nvme_map_sgl_data(NvmeCtrl
*n
, NvmeSg
*sg
,
978 NvmeSglDescriptor
*segment
, uint64_t nsgld
,
979 size_t *len
, NvmeCmd
*cmd
)
981 dma_addr_t addr
, trans_len
;
985 for (int i
= 0; i
< nsgld
; i
++) {
986 uint8_t type
= NVME_SGL_TYPE(segment
[i
].type
);
989 case NVME_SGL_DESCR_TYPE_DATA_BLOCK
:
991 case NVME_SGL_DESCR_TYPE_SEGMENT
:
992 case NVME_SGL_DESCR_TYPE_LAST_SEGMENT
:
993 return NVME_INVALID_NUM_SGL_DESCRS
| NVME_DNR
;
995 return NVME_SGL_DESCR_TYPE_INVALID
| NVME_DNR
;
998 dlen
= le32_to_cpu(segment
[i
].len
);
1006 * All data has been mapped, but the SGL contains additional
1007 * segments and/or descriptors. The controller might accept
1008 * ignoring the rest of the SGL.
1010 uint32_t sgls
= le32_to_cpu(n
->id_ctrl
.sgls
);
1011 if (sgls
& NVME_CTRL_SGLS_EXCESS_LENGTH
) {
1015 trace_pci_nvme_err_invalid_sgl_excess_length(dlen
);
1016 return NVME_DATA_SGL_LEN_INVALID
| NVME_DNR
;
1019 trans_len
= MIN(*len
, dlen
);
1021 addr
= le64_to_cpu(segment
[i
].addr
);
1023 if (UINT64_MAX
- addr
< dlen
) {
1024 return NVME_DATA_SGL_LEN_INVALID
| NVME_DNR
;
1027 status
= nvme_map_addr(n
, sg
, addr
, trans_len
);
1035 return NVME_SUCCESS
;
1038 static uint16_t nvme_map_sgl(NvmeCtrl
*n
, NvmeSg
*sg
, NvmeSglDescriptor sgl
,
1039 size_t len
, NvmeCmd
*cmd
)
1042 * Read the segment in chunks of 256 descriptors (one 4k page) to avoid
1043 * dynamically allocating a potentially huge SGL. The spec allows the SGL
1044 * to be larger (as in number of bytes required to describe the SGL
1045 * descriptors and segment chain) than the command transfer size, so it is
1046 * not bounded by MDTS.
1048 #define SEG_CHUNK_SIZE 256
1050 NvmeSglDescriptor segment
[SEG_CHUNK_SIZE
], *sgld
, *last_sgld
;
1058 addr
= le64_to_cpu(sgl
.addr
);
1060 trace_pci_nvme_map_sgl(NVME_SGL_TYPE(sgl
.type
), len
);
1062 nvme_sg_init(n
, sg
, nvme_addr_is_dma(n
, addr
));
1065 * If the entire transfer can be described with a single data block it can
1066 * be mapped directly.
1068 if (NVME_SGL_TYPE(sgl
.type
) == NVME_SGL_DESCR_TYPE_DATA_BLOCK
) {
1069 status
= nvme_map_sgl_data(n
, sg
, sgld
, 1, &len
, cmd
);
1078 switch (NVME_SGL_TYPE(sgld
->type
)) {
1079 case NVME_SGL_DESCR_TYPE_SEGMENT
:
1080 case NVME_SGL_DESCR_TYPE_LAST_SEGMENT
:
1083 return NVME_INVALID_SGL_SEG_DESCR
| NVME_DNR
;
1086 seg_len
= le32_to_cpu(sgld
->len
);
1088 /* check the length of the (Last) Segment descriptor */
1089 if (!seg_len
|| seg_len
& 0xf) {
1090 return NVME_INVALID_SGL_SEG_DESCR
| NVME_DNR
;
1093 if (UINT64_MAX
- addr
< seg_len
) {
1094 return NVME_DATA_SGL_LEN_INVALID
| NVME_DNR
;
1097 nsgld
= seg_len
/ sizeof(NvmeSglDescriptor
);
1099 while (nsgld
> SEG_CHUNK_SIZE
) {
1100 if (nvme_addr_read(n
, addr
, segment
, sizeof(segment
))) {
1101 trace_pci_nvme_err_addr_read(addr
);
1102 status
= NVME_DATA_TRAS_ERROR
;
1106 status
= nvme_map_sgl_data(n
, sg
, segment
, SEG_CHUNK_SIZE
,
1112 nsgld
-= SEG_CHUNK_SIZE
;
1113 addr
+= SEG_CHUNK_SIZE
* sizeof(NvmeSglDescriptor
);
1116 ret
= nvme_addr_read(n
, addr
, segment
, nsgld
*
1117 sizeof(NvmeSglDescriptor
));
1119 trace_pci_nvme_err_addr_read(addr
);
1120 status
= NVME_DATA_TRAS_ERROR
;
1124 last_sgld
= &segment
[nsgld
- 1];
1127 * If the segment ends with a Data Block, then we are done.
1129 if (NVME_SGL_TYPE(last_sgld
->type
) == NVME_SGL_DESCR_TYPE_DATA_BLOCK
) {
1130 status
= nvme_map_sgl_data(n
, sg
, segment
, nsgld
, &len
, cmd
);
1139 * If the last descriptor was not a Data Block, then the current
1140 * segment must not be a Last Segment.
1142 if (NVME_SGL_TYPE(sgld
->type
) == NVME_SGL_DESCR_TYPE_LAST_SEGMENT
) {
1143 status
= NVME_INVALID_SGL_SEG_DESCR
| NVME_DNR
;
1148 addr
= le64_to_cpu(sgld
->addr
);
1151 * Do not map the last descriptor; it will be a Segment or Last Segment
1152 * descriptor and is handled by the next iteration.
1154 status
= nvme_map_sgl_data(n
, sg
, segment
, nsgld
- 1, &len
, cmd
);
1161 /* if there is any residual left in len, the SGL was too short */
1163 status
= NVME_DATA_SGL_LEN_INVALID
| NVME_DNR
;
1167 return NVME_SUCCESS
;
1174 uint16_t nvme_map_dptr(NvmeCtrl
*n
, NvmeSg
*sg
, size_t len
,
1177 uint64_t prp1
, prp2
;
1179 switch (NVME_CMD_FLAGS_PSDT(cmd
->flags
)) {
1181 prp1
= le64_to_cpu(cmd
->dptr
.prp1
);
1182 prp2
= le64_to_cpu(cmd
->dptr
.prp2
);
1184 return nvme_map_prp(n
, sg
, prp1
, prp2
, len
);
1185 case NVME_PSDT_SGL_MPTR_CONTIGUOUS
:
1186 case NVME_PSDT_SGL_MPTR_SGL
:
1187 return nvme_map_sgl(n
, sg
, cmd
->dptr
.sgl
, len
, cmd
);
1189 return NVME_INVALID_FIELD
;
1193 static uint16_t nvme_map_mptr(NvmeCtrl
*n
, NvmeSg
*sg
, size_t len
,
1196 int psdt
= NVME_CMD_FLAGS_PSDT(cmd
->flags
);
1197 hwaddr mptr
= le64_to_cpu(cmd
->mptr
);
1200 if (psdt
== NVME_PSDT_SGL_MPTR_SGL
) {
1201 NvmeSglDescriptor sgl
;
1203 if (nvme_addr_read(n
, mptr
, &sgl
, sizeof(sgl
))) {
1204 return NVME_DATA_TRAS_ERROR
;
1207 status
= nvme_map_sgl(n
, sg
, sgl
, len
, cmd
);
1208 if (status
&& (status
& 0x7ff) == NVME_DATA_SGL_LEN_INVALID
) {
1209 status
= NVME_MD_SGL_LEN_INVALID
| NVME_DNR
;
1215 nvme_sg_init(n
, sg
, nvme_addr_is_dma(n
, mptr
));
1216 status
= nvme_map_addr(n
, sg
, mptr
, len
);
1224 static uint16_t nvme_map_data(NvmeCtrl
*n
, uint32_t nlb
, NvmeRequest
*req
)
1226 NvmeNamespace
*ns
= req
->ns
;
1227 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
1228 bool pi
= !!NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
);
1229 bool pract
= !!(le16_to_cpu(rw
->control
) & NVME_RW_PRINFO_PRACT
);
1230 size_t len
= nvme_l2b(ns
, nlb
);
1233 if (nvme_ns_ext(ns
) &&
1234 !(pi
&& pract
&& ns
->lbaf
.ms
== nvme_pi_tuple_size(ns
))) {
1237 len
+= nvme_m2b(ns
, nlb
);
1239 status
= nvme_map_dptr(n
, &sg
, len
, &req
->cmd
);
1244 nvme_sg_init(n
, &req
->sg
, sg
.flags
& NVME_SG_DMA
);
1245 nvme_sg_split(&sg
, ns
, &req
->sg
, NULL
);
1248 return NVME_SUCCESS
;
1251 return nvme_map_dptr(n
, &req
->sg
, len
, &req
->cmd
);
1254 static uint16_t nvme_map_mdata(NvmeCtrl
*n
, uint32_t nlb
, NvmeRequest
*req
)
1256 NvmeNamespace
*ns
= req
->ns
;
1257 size_t len
= nvme_m2b(ns
, nlb
);
1260 if (nvme_ns_ext(ns
)) {
1263 len
+= nvme_l2b(ns
, nlb
);
1265 status
= nvme_map_dptr(n
, &sg
, len
, &req
->cmd
);
1270 nvme_sg_init(n
, &req
->sg
, sg
.flags
& NVME_SG_DMA
);
1271 nvme_sg_split(&sg
, ns
, NULL
, &req
->sg
);
1274 return NVME_SUCCESS
;
1277 return nvme_map_mptr(n
, &req
->sg
, len
, &req
->cmd
);
1280 static uint16_t nvme_tx_interleaved(NvmeCtrl
*n
, NvmeSg
*sg
, uint8_t *ptr
,
1281 uint32_t len
, uint32_t bytes
,
1282 int32_t skip_bytes
, int64_t offset
,
1283 NvmeTxDirection dir
)
1286 uint32_t trans_len
, count
= bytes
;
1287 bool dma
= sg
->flags
& NVME_SG_DMA
;
1292 assert(sg
->flags
& NVME_SG_ALLOC
);
1295 sge_len
= dma
? sg
->qsg
.sg
[sg_idx
].len
: sg
->iov
.iov
[sg_idx
].iov_len
;
1297 if (sge_len
- offset
< 0) {
1303 if (sge_len
== offset
) {
1309 trans_len
= MIN(len
, count
);
1310 trans_len
= MIN(trans_len
, sge_len
- offset
);
1313 addr
= sg
->qsg
.sg
[sg_idx
].base
+ offset
;
1315 addr
= (hwaddr
)(uintptr_t)sg
->iov
.iov
[sg_idx
].iov_base
+ offset
;
1318 if (dir
== NVME_TX_DIRECTION_TO_DEVICE
) {
1319 ret
= nvme_addr_read(n
, addr
, ptr
, trans_len
);
1321 ret
= nvme_addr_write(n
, addr
, ptr
, trans_len
);
1325 return NVME_DATA_TRAS_ERROR
;
1331 offset
+= trans_len
;
1335 offset
+= skip_bytes
;
1339 return NVME_SUCCESS
;
1342 static uint16_t nvme_tx(NvmeCtrl
*n
, NvmeSg
*sg
, void *ptr
, uint32_t len
,
1343 NvmeTxDirection dir
)
1345 assert(sg
->flags
& NVME_SG_ALLOC
);
1347 if (sg
->flags
& NVME_SG_DMA
) {
1348 const MemTxAttrs attrs
= MEMTXATTRS_UNSPECIFIED
;
1349 dma_addr_t residual
;
1351 if (dir
== NVME_TX_DIRECTION_TO_DEVICE
) {
1352 dma_buf_write(ptr
, len
, &residual
, &sg
->qsg
, attrs
);
1354 dma_buf_read(ptr
, len
, &residual
, &sg
->qsg
, attrs
);
1357 if (unlikely(residual
)) {
1358 trace_pci_nvme_err_invalid_dma();
1359 return NVME_INVALID_FIELD
| NVME_DNR
;
1364 if (dir
== NVME_TX_DIRECTION_TO_DEVICE
) {
1365 bytes
= qemu_iovec_to_buf(&sg
->iov
, 0, ptr
, len
);
1367 bytes
= qemu_iovec_from_buf(&sg
->iov
, 0, ptr
, len
);
1370 if (unlikely(bytes
!= len
)) {
1371 trace_pci_nvme_err_invalid_dma();
1372 return NVME_INVALID_FIELD
| NVME_DNR
;
1376 return NVME_SUCCESS
;
1379 static inline uint16_t nvme_c2h(NvmeCtrl
*n
, void *ptr
, uint32_t len
,
1384 status
= nvme_map_dptr(n
, &req
->sg
, len
, &req
->cmd
);
1389 return nvme_tx(n
, &req
->sg
, ptr
, len
, NVME_TX_DIRECTION_FROM_DEVICE
);
1392 static inline uint16_t nvme_h2c(NvmeCtrl
*n
, void *ptr
, uint32_t len
,
1397 status
= nvme_map_dptr(n
, &req
->sg
, len
, &req
->cmd
);
1402 return nvme_tx(n
, &req
->sg
, ptr
, len
, NVME_TX_DIRECTION_TO_DEVICE
);
1405 uint16_t nvme_bounce_data(NvmeCtrl
*n
, void *ptr
, uint32_t len
,
1406 NvmeTxDirection dir
, NvmeRequest
*req
)
1408 NvmeNamespace
*ns
= req
->ns
;
1409 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
1410 bool pi
= !!NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
);
1411 bool pract
= !!(le16_to_cpu(rw
->control
) & NVME_RW_PRINFO_PRACT
);
1413 if (nvme_ns_ext(ns
) &&
1414 !(pi
&& pract
&& ns
->lbaf
.ms
== nvme_pi_tuple_size(ns
))) {
1415 return nvme_tx_interleaved(n
, &req
->sg
, ptr
, len
, ns
->lbasz
,
1416 ns
->lbaf
.ms
, 0, dir
);
1419 return nvme_tx(n
, &req
->sg
, ptr
, len
, dir
);
1422 uint16_t nvme_bounce_mdata(NvmeCtrl
*n
, void *ptr
, uint32_t len
,
1423 NvmeTxDirection dir
, NvmeRequest
*req
)
1425 NvmeNamespace
*ns
= req
->ns
;
1428 if (nvme_ns_ext(ns
)) {
1429 return nvme_tx_interleaved(n
, &req
->sg
, ptr
, len
, ns
->lbaf
.ms
,
1430 ns
->lbasz
, ns
->lbasz
, dir
);
1433 nvme_sg_unmap(&req
->sg
);
1435 status
= nvme_map_mptr(n
, &req
->sg
, len
, &req
->cmd
);
1440 return nvme_tx(n
, &req
->sg
, ptr
, len
, dir
);
1443 static inline void nvme_blk_read(BlockBackend
*blk
, int64_t offset
,
1444 uint32_t align
, BlockCompletionFunc
*cb
,
1447 assert(req
->sg
.flags
& NVME_SG_ALLOC
);
1449 if (req
->sg
.flags
& NVME_SG_DMA
) {
1450 req
->aiocb
= dma_blk_read(blk
, &req
->sg
.qsg
, offset
, align
, cb
, req
);
1452 req
->aiocb
= blk_aio_preadv(blk
, offset
, &req
->sg
.iov
, 0, cb
, req
);
1456 static inline void nvme_blk_write(BlockBackend
*blk
, int64_t offset
,
1457 uint32_t align
, BlockCompletionFunc
*cb
,
1460 assert(req
->sg
.flags
& NVME_SG_ALLOC
);
1462 if (req
->sg
.flags
& NVME_SG_DMA
) {
1463 req
->aiocb
= dma_blk_write(blk
, &req
->sg
.qsg
, offset
, align
, cb
, req
);
1465 req
->aiocb
= blk_aio_pwritev(blk
, offset
, &req
->sg
.iov
, 0, cb
, req
);
1469 static void nvme_update_cq_eventidx(const NvmeCQueue
*cq
)
1471 trace_pci_nvme_update_cq_eventidx(cq
->cqid
, cq
->head
);
1473 stl_le_pci_dma(PCI_DEVICE(cq
->ctrl
), cq
->ei_addr
, cq
->head
,
1474 MEMTXATTRS_UNSPECIFIED
);
1477 static void nvme_update_cq_head(NvmeCQueue
*cq
)
1479 ldl_le_pci_dma(PCI_DEVICE(cq
->ctrl
), cq
->db_addr
, &cq
->head
,
1480 MEMTXATTRS_UNSPECIFIED
);
1482 trace_pci_nvme_update_cq_head(cq
->cqid
, cq
->head
);
1485 static void nvme_post_cqes(void *opaque
)
1487 NvmeCQueue
*cq
= opaque
;
1488 NvmeCtrl
*n
= cq
->ctrl
;
1489 NvmeRequest
*req
, *next
;
1490 bool pending
= cq
->head
!= cq
->tail
;
1493 QTAILQ_FOREACH_SAFE(req
, &cq
->req_list
, entry
, next
) {
1497 if (n
->dbbuf_enabled
) {
1498 nvme_update_cq_eventidx(cq
);
1499 nvme_update_cq_head(cq
);
1502 if (nvme_cq_full(cq
)) {
1507 req
->cqe
.status
= cpu_to_le16((req
->status
<< 1) | cq
->phase
);
1508 req
->cqe
.sq_id
= cpu_to_le16(sq
->sqid
);
1509 req
->cqe
.sq_head
= cpu_to_le16(sq
->head
);
1510 addr
= cq
->dma_addr
+ (cq
->tail
<< NVME_CQES
);
1511 ret
= pci_dma_write(PCI_DEVICE(n
), addr
, (void *)&req
->cqe
,
1514 trace_pci_nvme_err_addr_write(addr
);
1515 trace_pci_nvme_err_cfs();
1516 stl_le_p(&n
->bar
.csts
, NVME_CSTS_FAILED
);
1519 QTAILQ_REMOVE(&cq
->req_list
, req
, entry
);
1520 nvme_inc_cq_tail(cq
);
1521 nvme_sg_unmap(&req
->sg
);
1522 QTAILQ_INSERT_TAIL(&sq
->req_list
, req
, entry
);
1524 if (cq
->tail
!= cq
->head
) {
1525 if (cq
->irq_enabled
&& !pending
) {
1529 nvme_irq_assert(n
, cq
);
1533 static void nvme_enqueue_req_completion(NvmeCQueue
*cq
, NvmeRequest
*req
)
1535 assert(cq
->cqid
== req
->sq
->cqid
);
1536 trace_pci_nvme_enqueue_req_completion(nvme_cid(req
), cq
->cqid
,
1537 le32_to_cpu(req
->cqe
.result
),
1538 le32_to_cpu(req
->cqe
.dw1
),
1542 trace_pci_nvme_err_req_status(nvme_cid(req
), nvme_nsid(req
->ns
),
1543 req
->status
, req
->cmd
.opcode
);
1546 QTAILQ_REMOVE(&req
->sq
->out_req_list
, req
, entry
);
1547 QTAILQ_INSERT_TAIL(&cq
->req_list
, req
, entry
);
1549 qemu_bh_schedule(cq
->bh
);
1552 static void nvme_process_aers(void *opaque
)
1554 NvmeCtrl
*n
= opaque
;
1555 NvmeAsyncEvent
*event
, *next
;
1557 trace_pci_nvme_process_aers(n
->aer_queued
);
1559 QTAILQ_FOREACH_SAFE(event
, &n
->aer_queue
, entry
, next
) {
1561 NvmeAerResult
*result
;
1563 /* can't post cqe if there is nothing to complete */
1564 if (!n
->outstanding_aers
) {
1565 trace_pci_nvme_no_outstanding_aers();
1569 /* ignore if masked (cqe posted, but event not cleared) */
1570 if (n
->aer_mask
& (1 << event
->result
.event_type
)) {
1571 trace_pci_nvme_aer_masked(event
->result
.event_type
, n
->aer_mask
);
1575 QTAILQ_REMOVE(&n
->aer_queue
, event
, entry
);
1578 n
->aer_mask
|= 1 << event
->result
.event_type
;
1579 n
->outstanding_aers
--;
1581 req
= n
->aer_reqs
[n
->outstanding_aers
];
1583 result
= (NvmeAerResult
*) &req
->cqe
.result
;
1584 result
->event_type
= event
->result
.event_type
;
1585 result
->event_info
= event
->result
.event_info
;
1586 result
->log_page
= event
->result
.log_page
;
1589 trace_pci_nvme_aer_post_cqe(result
->event_type
, result
->event_info
,
1592 nvme_enqueue_req_completion(&n
->admin_cq
, req
);
1596 static void nvme_enqueue_event(NvmeCtrl
*n
, uint8_t event_type
,
1597 uint8_t event_info
, uint8_t log_page
)
1599 NvmeAsyncEvent
*event
;
1601 trace_pci_nvme_enqueue_event(event_type
, event_info
, log_page
);
1603 if (n
->aer_queued
== n
->params
.aer_max_queued
) {
1604 trace_pci_nvme_enqueue_event_noqueue(n
->aer_queued
);
1608 event
= g_new(NvmeAsyncEvent
, 1);
1609 event
->result
= (NvmeAerResult
) {
1610 .event_type
= event_type
,
1611 .event_info
= event_info
,
1612 .log_page
= log_page
,
1615 QTAILQ_INSERT_TAIL(&n
->aer_queue
, event
, entry
);
1618 nvme_process_aers(n
);
1621 static void nvme_smart_event(NvmeCtrl
*n
, uint8_t event
)
1625 /* Ref SPEC <Asynchronous Event Information 0x2013 SMART / Health Status> */
1626 if (!(NVME_AEC_SMART(n
->features
.async_config
) & event
)) {
1631 case NVME_SMART_SPARE
:
1632 aer_info
= NVME_AER_INFO_SMART_SPARE_THRESH
;
1634 case NVME_SMART_TEMPERATURE
:
1635 aer_info
= NVME_AER_INFO_SMART_TEMP_THRESH
;
1637 case NVME_SMART_RELIABILITY
:
1638 case NVME_SMART_MEDIA_READ_ONLY
:
1639 case NVME_SMART_FAILED_VOLATILE_MEDIA
:
1640 case NVME_SMART_PMR_UNRELIABLE
:
1641 aer_info
= NVME_AER_INFO_SMART_RELIABILITY
;
1647 nvme_enqueue_event(n
, NVME_AER_TYPE_SMART
, aer_info
, NVME_LOG_SMART_INFO
);
1650 static void nvme_clear_events(NvmeCtrl
*n
, uint8_t event_type
)
1652 n
->aer_mask
&= ~(1 << event_type
);
1653 if (!QTAILQ_EMPTY(&n
->aer_queue
)) {
1654 nvme_process_aers(n
);
1658 static inline uint16_t nvme_check_mdts(NvmeCtrl
*n
, size_t len
)
1660 uint8_t mdts
= n
->params
.mdts
;
1662 if (mdts
&& len
> n
->page_size
<< mdts
) {
1663 trace_pci_nvme_err_mdts(len
);
1664 return NVME_INVALID_FIELD
| NVME_DNR
;
1667 return NVME_SUCCESS
;
1670 static inline uint16_t nvme_check_bounds(NvmeNamespace
*ns
, uint64_t slba
,
1673 uint64_t nsze
= le64_to_cpu(ns
->id_ns
.nsze
);
1675 if (unlikely(UINT64_MAX
- slba
< nlb
|| slba
+ nlb
> nsze
)) {
1676 trace_pci_nvme_err_invalid_lba_range(slba
, nlb
, nsze
);
1677 return NVME_LBA_RANGE
| NVME_DNR
;
1680 return NVME_SUCCESS
;
1683 static int nvme_block_status_all(NvmeNamespace
*ns
, uint64_t slba
,
1684 uint32_t nlb
, int flags
)
1686 BlockDriverState
*bs
= blk_bs(ns
->blkconf
.blk
);
1688 int64_t pnum
= 0, bytes
= nvme_l2b(ns
, nlb
);
1689 int64_t offset
= nvme_l2b(ns
, slba
);
1693 * `pnum` holds the number of bytes after offset that shares the same
1694 * allocation status as the byte at offset. If `pnum` is different from
1695 * `bytes`, we should check the allocation status of the next range and
1696 * continue this until all bytes have been checked.
1701 ret
= bdrv_block_status(bs
, offset
, bytes
, &pnum
, NULL
, NULL
);
1707 trace_pci_nvme_block_status(offset
, bytes
, pnum
, ret
,
1708 !!(ret
& BDRV_BLOCK_ZERO
));
1710 if (!(ret
& flags
)) {
1715 } while (pnum
!= bytes
);
1720 static uint16_t nvme_check_dulbe(NvmeNamespace
*ns
, uint64_t slba
,
1726 ret
= nvme_block_status_all(ns
, slba
, nlb
, BDRV_BLOCK_DATA
);
1729 error_setg_errno(&err
, -ret
, "unable to get block status");
1730 error_report_err(err
);
1732 return NVME_INTERNAL_DEV_ERROR
;
1738 return NVME_SUCCESS
;
1741 static void nvme_aio_err(NvmeRequest
*req
, int ret
)
1743 uint16_t status
= NVME_SUCCESS
;
1744 Error
*local_err
= NULL
;
1746 switch (req
->cmd
.opcode
) {
1748 status
= NVME_UNRECOVERED_READ
;
1750 case NVME_CMD_FLUSH
:
1751 case NVME_CMD_WRITE
:
1752 case NVME_CMD_WRITE_ZEROES
:
1753 case NVME_CMD_ZONE_APPEND
:
1755 status
= NVME_WRITE_FAULT
;
1758 status
= NVME_INTERNAL_DEV_ERROR
;
1762 trace_pci_nvme_err_aio(nvme_cid(req
), strerror(-ret
), status
);
1764 error_setg_errno(&local_err
, -ret
, "aio failed");
1765 error_report_err(local_err
);
1768 * Set the command status code to the first encountered error but allow a
1769 * subsequent Internal Device Error to trump it.
1771 if (req
->status
&& status
!= NVME_INTERNAL_DEV_ERROR
) {
1775 req
->status
= status
;
1778 static inline uint32_t nvme_zone_idx(NvmeNamespace
*ns
, uint64_t slba
)
1780 return ns
->zone_size_log2
> 0 ? slba
>> ns
->zone_size_log2
:
1781 slba
/ ns
->zone_size
;
1784 static inline NvmeZone
*nvme_get_zone_by_slba(NvmeNamespace
*ns
, uint64_t slba
)
1786 uint32_t zone_idx
= nvme_zone_idx(ns
, slba
);
1788 if (zone_idx
>= ns
->num_zones
) {
1792 return &ns
->zone_array
[zone_idx
];
1795 static uint16_t nvme_check_zone_state_for_write(NvmeZone
*zone
)
1797 uint64_t zslba
= zone
->d
.zslba
;
1799 switch (nvme_get_zone_state(zone
)) {
1800 case NVME_ZONE_STATE_EMPTY
:
1801 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
1802 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
1803 case NVME_ZONE_STATE_CLOSED
:
1804 return NVME_SUCCESS
;
1805 case NVME_ZONE_STATE_FULL
:
1806 trace_pci_nvme_err_zone_is_full(zslba
);
1807 return NVME_ZONE_FULL
;
1808 case NVME_ZONE_STATE_OFFLINE
:
1809 trace_pci_nvme_err_zone_is_offline(zslba
);
1810 return NVME_ZONE_OFFLINE
;
1811 case NVME_ZONE_STATE_READ_ONLY
:
1812 trace_pci_nvme_err_zone_is_read_only(zslba
);
1813 return NVME_ZONE_READ_ONLY
;
1818 return NVME_INTERNAL_DEV_ERROR
;
1821 static uint16_t nvme_check_zone_write(NvmeNamespace
*ns
, NvmeZone
*zone
,
1822 uint64_t slba
, uint32_t nlb
)
1824 uint64_t zcap
= nvme_zone_wr_boundary(zone
);
1827 status
= nvme_check_zone_state_for_write(zone
);
1832 if (zone
->d
.za
& NVME_ZA_ZRWA_VALID
) {
1833 uint64_t ezrwa
= zone
->w_ptr
+ 2 * ns
->zns
.zrwas
;
1835 if (slba
< zone
->w_ptr
|| slba
+ nlb
> ezrwa
) {
1836 trace_pci_nvme_err_zone_invalid_write(slba
, zone
->w_ptr
);
1837 return NVME_ZONE_INVALID_WRITE
;
1840 if (unlikely(slba
!= zone
->w_ptr
)) {
1841 trace_pci_nvme_err_write_not_at_wp(slba
, zone
->d
.zslba
,
1843 return NVME_ZONE_INVALID_WRITE
;
1847 if (unlikely((slba
+ nlb
) > zcap
)) {
1848 trace_pci_nvme_err_zone_boundary(slba
, nlb
, zcap
);
1849 return NVME_ZONE_BOUNDARY_ERROR
;
1852 return NVME_SUCCESS
;
1855 static uint16_t nvme_check_zone_state_for_read(NvmeZone
*zone
)
1857 switch (nvme_get_zone_state(zone
)) {
1858 case NVME_ZONE_STATE_EMPTY
:
1859 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
1860 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
1861 case NVME_ZONE_STATE_FULL
:
1862 case NVME_ZONE_STATE_CLOSED
:
1863 case NVME_ZONE_STATE_READ_ONLY
:
1864 return NVME_SUCCESS
;
1865 case NVME_ZONE_STATE_OFFLINE
:
1866 trace_pci_nvme_err_zone_is_offline(zone
->d
.zslba
);
1867 return NVME_ZONE_OFFLINE
;
1872 return NVME_INTERNAL_DEV_ERROR
;
1875 static uint16_t nvme_check_zone_read(NvmeNamespace
*ns
, uint64_t slba
,
1879 uint64_t bndry
, end
;
1882 zone
= nvme_get_zone_by_slba(ns
, slba
);
1885 bndry
= nvme_zone_rd_boundary(ns
, zone
);
1888 status
= nvme_check_zone_state_for_read(zone
);
1891 } else if (unlikely(end
> bndry
)) {
1892 if (!ns
->params
.cross_zone_read
) {
1893 status
= NVME_ZONE_BOUNDARY_ERROR
;
1896 * Read across zone boundary - check that all subsequent
1897 * zones that are being read have an appropriate state.
1901 status
= nvme_check_zone_state_for_read(zone
);
1905 } while (end
> nvme_zone_rd_boundary(ns
, zone
));
1912 static uint16_t nvme_zrm_finish(NvmeNamespace
*ns
, NvmeZone
*zone
)
1914 switch (nvme_get_zone_state(zone
)) {
1915 case NVME_ZONE_STATE_FULL
:
1916 return NVME_SUCCESS
;
1918 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
1919 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
1920 nvme_aor_dec_open(ns
);
1922 case NVME_ZONE_STATE_CLOSED
:
1923 nvme_aor_dec_active(ns
);
1925 if (zone
->d
.za
& NVME_ZA_ZRWA_VALID
) {
1926 zone
->d
.za
&= ~NVME_ZA_ZRWA_VALID
;
1927 if (ns
->params
.numzrwa
) {
1933 case NVME_ZONE_STATE_EMPTY
:
1934 nvme_assign_zone_state(ns
, zone
, NVME_ZONE_STATE_FULL
);
1935 return NVME_SUCCESS
;
1938 return NVME_ZONE_INVAL_TRANSITION
;
1942 static uint16_t nvme_zrm_close(NvmeNamespace
*ns
, NvmeZone
*zone
)
1944 switch (nvme_get_zone_state(zone
)) {
1945 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
1946 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
1947 nvme_aor_dec_open(ns
);
1948 nvme_assign_zone_state(ns
, zone
, NVME_ZONE_STATE_CLOSED
);
1950 case NVME_ZONE_STATE_CLOSED
:
1951 return NVME_SUCCESS
;
1954 return NVME_ZONE_INVAL_TRANSITION
;
1958 static uint16_t nvme_zrm_reset(NvmeNamespace
*ns
, NvmeZone
*zone
)
1960 switch (nvme_get_zone_state(zone
)) {
1961 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
1962 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
1963 nvme_aor_dec_open(ns
);
1965 case NVME_ZONE_STATE_CLOSED
:
1966 nvme_aor_dec_active(ns
);
1968 if (zone
->d
.za
& NVME_ZA_ZRWA_VALID
) {
1969 if (ns
->params
.numzrwa
) {
1975 case NVME_ZONE_STATE_FULL
:
1976 zone
->w_ptr
= zone
->d
.zslba
;
1977 zone
->d
.wp
= zone
->w_ptr
;
1978 nvme_assign_zone_state(ns
, zone
, NVME_ZONE_STATE_EMPTY
);
1980 case NVME_ZONE_STATE_EMPTY
:
1981 return NVME_SUCCESS
;
1984 return NVME_ZONE_INVAL_TRANSITION
;
1988 static void nvme_zrm_auto_transition_zone(NvmeNamespace
*ns
)
1992 if (ns
->params
.max_open_zones
&&
1993 ns
->nr_open_zones
== ns
->params
.max_open_zones
) {
1994 zone
= QTAILQ_FIRST(&ns
->imp_open_zones
);
1997 * Automatically close this implicitly open zone.
1999 QTAILQ_REMOVE(&ns
->imp_open_zones
, zone
, entry
);
2000 nvme_zrm_close(ns
, zone
);
2006 NVME_ZRM_AUTO
= 1 << 0,
2007 NVME_ZRM_ZRWA
= 1 << 1,
2010 static uint16_t nvme_zrm_open_flags(NvmeCtrl
*n
, NvmeNamespace
*ns
,
2011 NvmeZone
*zone
, int flags
)
2016 switch (nvme_get_zone_state(zone
)) {
2017 case NVME_ZONE_STATE_EMPTY
:
2022 case NVME_ZONE_STATE_CLOSED
:
2023 if (n
->params
.auto_transition_zones
) {
2024 nvme_zrm_auto_transition_zone(ns
);
2026 status
= nvme_zns_check_resources(ns
, act
, 1,
2027 (flags
& NVME_ZRM_ZRWA
) ? 1 : 0);
2033 nvme_aor_inc_active(ns
);
2036 nvme_aor_inc_open(ns
);
2038 if (flags
& NVME_ZRM_AUTO
) {
2039 nvme_assign_zone_state(ns
, zone
, NVME_ZONE_STATE_IMPLICITLY_OPEN
);
2040 return NVME_SUCCESS
;
2045 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
2046 if (flags
& NVME_ZRM_AUTO
) {
2047 return NVME_SUCCESS
;
2050 nvme_assign_zone_state(ns
, zone
, NVME_ZONE_STATE_EXPLICITLY_OPEN
);
2054 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
2055 if (flags
& NVME_ZRM_ZRWA
) {
2058 zone
->d
.za
|= NVME_ZA_ZRWA_VALID
;
2061 return NVME_SUCCESS
;
2064 return NVME_ZONE_INVAL_TRANSITION
;
2068 static inline uint16_t nvme_zrm_auto(NvmeCtrl
*n
, NvmeNamespace
*ns
,
2071 return nvme_zrm_open_flags(n
, ns
, zone
, NVME_ZRM_AUTO
);
2074 static void nvme_advance_zone_wp(NvmeNamespace
*ns
, NvmeZone
*zone
,
2079 if (zone
->d
.wp
== nvme_zone_wr_boundary(zone
)) {
2080 nvme_zrm_finish(ns
, zone
);
2084 static void nvme_zoned_zrwa_implicit_flush(NvmeNamespace
*ns
, NvmeZone
*zone
,
2087 uint16_t nzrwafgs
= DIV_ROUND_UP(nlbc
, ns
->zns
.zrwafg
);
2089 nlbc
= nzrwafgs
* ns
->zns
.zrwafg
;
2091 trace_pci_nvme_zoned_zrwa_implicit_flush(zone
->d
.zslba
, nlbc
);
2093 zone
->w_ptr
+= nlbc
;
2095 nvme_advance_zone_wp(ns
, zone
, nlbc
);
2098 static void nvme_finalize_zoned_write(NvmeNamespace
*ns
, NvmeRequest
*req
)
2100 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
2105 slba
= le64_to_cpu(rw
->slba
);
2106 nlb
= le16_to_cpu(rw
->nlb
) + 1;
2107 zone
= nvme_get_zone_by_slba(ns
, slba
);
2110 if (zone
->d
.za
& NVME_ZA_ZRWA_VALID
) {
2111 uint64_t ezrwa
= zone
->w_ptr
+ ns
->zns
.zrwas
- 1;
2112 uint64_t elba
= slba
+ nlb
- 1;
2115 nvme_zoned_zrwa_implicit_flush(ns
, zone
, elba
- ezrwa
);
2121 nvme_advance_zone_wp(ns
, zone
, nlb
);
2124 static inline bool nvme_is_write(NvmeRequest
*req
)
2126 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
2128 return rw
->opcode
== NVME_CMD_WRITE
||
2129 rw
->opcode
== NVME_CMD_ZONE_APPEND
||
2130 rw
->opcode
== NVME_CMD_WRITE_ZEROES
;
2133 static AioContext
*nvme_get_aio_context(BlockAIOCB
*acb
)
2135 return qemu_get_aio_context();
2138 static void nvme_misc_cb(void *opaque
, int ret
)
2140 NvmeRequest
*req
= opaque
;
2142 trace_pci_nvme_misc_cb(nvme_cid(req
));
2145 nvme_aio_err(req
, ret
);
2148 nvme_enqueue_req_completion(nvme_cq(req
), req
);
2151 void nvme_rw_complete_cb(void *opaque
, int ret
)
2153 NvmeRequest
*req
= opaque
;
2154 NvmeNamespace
*ns
= req
->ns
;
2155 BlockBackend
*blk
= ns
->blkconf
.blk
;
2156 BlockAcctCookie
*acct
= &req
->acct
;
2157 BlockAcctStats
*stats
= blk_get_stats(blk
);
2159 trace_pci_nvme_rw_complete_cb(nvme_cid(req
), blk_name(blk
));
2162 block_acct_failed(stats
, acct
);
2163 nvme_aio_err(req
, ret
);
2165 block_acct_done(stats
, acct
);
2168 if (ns
->params
.zoned
&& nvme_is_write(req
)) {
2169 nvme_finalize_zoned_write(ns
, req
);
2172 nvme_enqueue_req_completion(nvme_cq(req
), req
);
2175 static void nvme_rw_cb(void *opaque
, int ret
)
2177 NvmeRequest
*req
= opaque
;
2178 NvmeNamespace
*ns
= req
->ns
;
2180 BlockBackend
*blk
= ns
->blkconf
.blk
;
2182 trace_pci_nvme_rw_cb(nvme_cid(req
), blk_name(blk
));
2189 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
2190 uint64_t slba
= le64_to_cpu(rw
->slba
);
2191 uint32_t nlb
= (uint32_t)le16_to_cpu(rw
->nlb
) + 1;
2192 uint64_t offset
= nvme_moff(ns
, slba
);
2194 if (req
->cmd
.opcode
== NVME_CMD_WRITE_ZEROES
) {
2195 size_t mlen
= nvme_m2b(ns
, nlb
);
2197 req
->aiocb
= blk_aio_pwrite_zeroes(blk
, offset
, mlen
,
2199 nvme_rw_complete_cb
, req
);
2203 if (nvme_ns_ext(ns
) || req
->cmd
.mptr
) {
2206 nvme_sg_unmap(&req
->sg
);
2207 status
= nvme_map_mdata(nvme_ctrl(req
), nlb
, req
);
2213 if (req
->cmd
.opcode
== NVME_CMD_READ
) {
2214 return nvme_blk_read(blk
, offset
, 1, nvme_rw_complete_cb
, req
);
2217 return nvme_blk_write(blk
, offset
, 1, nvme_rw_complete_cb
, req
);
2222 nvme_rw_complete_cb(req
, ret
);
2225 static void nvme_verify_cb(void *opaque
, int ret
)
2227 NvmeBounceContext
*ctx
= opaque
;
2228 NvmeRequest
*req
= ctx
->req
;
2229 NvmeNamespace
*ns
= req
->ns
;
2230 BlockBackend
*blk
= ns
->blkconf
.blk
;
2231 BlockAcctCookie
*acct
= &req
->acct
;
2232 BlockAcctStats
*stats
= blk_get_stats(blk
);
2233 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
2234 uint64_t slba
= le64_to_cpu(rw
->slba
);
2235 uint8_t prinfo
= NVME_RW_PRINFO(le16_to_cpu(rw
->control
));
2236 uint16_t apptag
= le16_to_cpu(rw
->apptag
);
2237 uint16_t appmask
= le16_to_cpu(rw
->appmask
);
2238 uint64_t reftag
= le32_to_cpu(rw
->reftag
);
2239 uint64_t cdw3
= le32_to_cpu(rw
->cdw3
);
2242 reftag
|= cdw3
<< 32;
2244 trace_pci_nvme_verify_cb(nvme_cid(req
), prinfo
, apptag
, appmask
, reftag
);
2247 block_acct_failed(stats
, acct
);
2248 nvme_aio_err(req
, ret
);
2252 block_acct_done(stats
, acct
);
2254 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
2255 status
= nvme_dif_mangle_mdata(ns
, ctx
->mdata
.bounce
,
2256 ctx
->mdata
.iov
.size
, slba
);
2258 req
->status
= status
;
2262 req
->status
= nvme_dif_check(ns
, ctx
->data
.bounce
, ctx
->data
.iov
.size
,
2263 ctx
->mdata
.bounce
, ctx
->mdata
.iov
.size
,
2264 prinfo
, slba
, apptag
, appmask
, &reftag
);
2268 qemu_iovec_destroy(&ctx
->data
.iov
);
2269 g_free(ctx
->data
.bounce
);
2271 qemu_iovec_destroy(&ctx
->mdata
.iov
);
2272 g_free(ctx
->mdata
.bounce
);
2276 nvme_enqueue_req_completion(nvme_cq(req
), req
);
2280 static void nvme_verify_mdata_in_cb(void *opaque
, int ret
)
2282 NvmeBounceContext
*ctx
= opaque
;
2283 NvmeRequest
*req
= ctx
->req
;
2284 NvmeNamespace
*ns
= req
->ns
;
2285 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
2286 uint64_t slba
= le64_to_cpu(rw
->slba
);
2287 uint32_t nlb
= le16_to_cpu(rw
->nlb
) + 1;
2288 size_t mlen
= nvme_m2b(ns
, nlb
);
2289 uint64_t offset
= nvme_moff(ns
, slba
);
2290 BlockBackend
*blk
= ns
->blkconf
.blk
;
2292 trace_pci_nvme_verify_mdata_in_cb(nvme_cid(req
), blk_name(blk
));
2298 ctx
->mdata
.bounce
= g_malloc(mlen
);
2300 qemu_iovec_reset(&ctx
->mdata
.iov
);
2301 qemu_iovec_add(&ctx
->mdata
.iov
, ctx
->mdata
.bounce
, mlen
);
2303 req
->aiocb
= blk_aio_preadv(blk
, offset
, &ctx
->mdata
.iov
, 0,
2304 nvme_verify_cb
, ctx
);
2308 nvme_verify_cb(ctx
, ret
);
2311 struct nvme_compare_ctx
{
2323 static void nvme_compare_mdata_cb(void *opaque
, int ret
)
2325 NvmeRequest
*req
= opaque
;
2326 NvmeNamespace
*ns
= req
->ns
;
2327 NvmeCtrl
*n
= nvme_ctrl(req
);
2328 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
2329 uint8_t prinfo
= NVME_RW_PRINFO(le16_to_cpu(rw
->control
));
2330 uint16_t apptag
= le16_to_cpu(rw
->apptag
);
2331 uint16_t appmask
= le16_to_cpu(rw
->appmask
);
2332 uint64_t reftag
= le32_to_cpu(rw
->reftag
);
2333 uint64_t cdw3
= le32_to_cpu(rw
->cdw3
);
2334 struct nvme_compare_ctx
*ctx
= req
->opaque
;
2335 g_autofree
uint8_t *buf
= NULL
;
2336 BlockBackend
*blk
= ns
->blkconf
.blk
;
2337 BlockAcctCookie
*acct
= &req
->acct
;
2338 BlockAcctStats
*stats
= blk_get_stats(blk
);
2339 uint16_t status
= NVME_SUCCESS
;
2341 reftag
|= cdw3
<< 32;
2343 trace_pci_nvme_compare_mdata_cb(nvme_cid(req
));
2346 block_acct_failed(stats
, acct
);
2347 nvme_aio_err(req
, ret
);
2351 buf
= g_malloc(ctx
->mdata
.iov
.size
);
2353 status
= nvme_bounce_mdata(n
, buf
, ctx
->mdata
.iov
.size
,
2354 NVME_TX_DIRECTION_TO_DEVICE
, req
);
2356 req
->status
= status
;
2360 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
2361 uint64_t slba
= le64_to_cpu(rw
->slba
);
2363 uint8_t *mbufp
= ctx
->mdata
.bounce
;
2364 uint8_t *end
= mbufp
+ ctx
->mdata
.iov
.size
;
2367 status
= nvme_dif_check(ns
, ctx
->data
.bounce
, ctx
->data
.iov
.size
,
2368 ctx
->mdata
.bounce
, ctx
->mdata
.iov
.size
, prinfo
,
2369 slba
, apptag
, appmask
, &reftag
);
2371 req
->status
= status
;
2376 * When formatted with protection information, do not compare the DIF
2379 if (!(ns
->id_ns
.dps
& NVME_ID_NS_DPS_FIRST_EIGHT
)) {
2380 pil
= ns
->lbaf
.ms
- nvme_pi_tuple_size(ns
);
2383 for (bufp
= buf
; mbufp
< end
; bufp
+= ns
->lbaf
.ms
, mbufp
+= ns
->lbaf
.ms
) {
2384 if (memcmp(bufp
+ pil
, mbufp
+ pil
, ns
->lbaf
.ms
- pil
)) {
2385 req
->status
= NVME_CMP_FAILURE
| NVME_DNR
;
2393 if (memcmp(buf
, ctx
->mdata
.bounce
, ctx
->mdata
.iov
.size
)) {
2394 req
->status
= NVME_CMP_FAILURE
| NVME_DNR
;
2398 block_acct_done(stats
, acct
);
2401 qemu_iovec_destroy(&ctx
->data
.iov
);
2402 g_free(ctx
->data
.bounce
);
2404 qemu_iovec_destroy(&ctx
->mdata
.iov
);
2405 g_free(ctx
->mdata
.bounce
);
2409 nvme_enqueue_req_completion(nvme_cq(req
), req
);
2412 static void nvme_compare_data_cb(void *opaque
, int ret
)
2414 NvmeRequest
*req
= opaque
;
2415 NvmeCtrl
*n
= nvme_ctrl(req
);
2416 NvmeNamespace
*ns
= req
->ns
;
2417 BlockBackend
*blk
= ns
->blkconf
.blk
;
2418 BlockAcctCookie
*acct
= &req
->acct
;
2419 BlockAcctStats
*stats
= blk_get_stats(blk
);
2421 struct nvme_compare_ctx
*ctx
= req
->opaque
;
2422 g_autofree
uint8_t *buf
= NULL
;
2425 trace_pci_nvme_compare_data_cb(nvme_cid(req
));
2428 block_acct_failed(stats
, acct
);
2429 nvme_aio_err(req
, ret
);
2433 buf
= g_malloc(ctx
->data
.iov
.size
);
2435 status
= nvme_bounce_data(n
, buf
, ctx
->data
.iov
.size
,
2436 NVME_TX_DIRECTION_TO_DEVICE
, req
);
2438 req
->status
= status
;
2442 if (memcmp(buf
, ctx
->data
.bounce
, ctx
->data
.iov
.size
)) {
2443 req
->status
= NVME_CMP_FAILURE
| NVME_DNR
;
2448 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
2449 uint64_t slba
= le64_to_cpu(rw
->slba
);
2450 uint32_t nlb
= le16_to_cpu(rw
->nlb
) + 1;
2451 size_t mlen
= nvme_m2b(ns
, nlb
);
2452 uint64_t offset
= nvme_moff(ns
, slba
);
2454 ctx
->mdata
.bounce
= g_malloc(mlen
);
2456 qemu_iovec_init(&ctx
->mdata
.iov
, 1);
2457 qemu_iovec_add(&ctx
->mdata
.iov
, ctx
->mdata
.bounce
, mlen
);
2459 req
->aiocb
= blk_aio_preadv(blk
, offset
, &ctx
->mdata
.iov
, 0,
2460 nvme_compare_mdata_cb
, req
);
2464 block_acct_done(stats
, acct
);
2467 qemu_iovec_destroy(&ctx
->data
.iov
);
2468 g_free(ctx
->data
.bounce
);
2471 nvme_enqueue_req_completion(nvme_cq(req
), req
);
2474 typedef struct NvmeDSMAIOCB
{
2480 NvmeDsmRange
*range
;
2485 static void nvme_dsm_cancel(BlockAIOCB
*aiocb
)
2487 NvmeDSMAIOCB
*iocb
= container_of(aiocb
, NvmeDSMAIOCB
, common
);
2489 /* break nvme_dsm_cb loop */
2490 iocb
->idx
= iocb
->nr
;
2491 iocb
->ret
= -ECANCELED
;
2494 blk_aio_cancel_async(iocb
->aiocb
);
2498 * We only reach this if nvme_dsm_cancel() has already been called or
2499 * the command ran to completion.
2501 assert(iocb
->idx
== iocb
->nr
);
2505 static const AIOCBInfo nvme_dsm_aiocb_info
= {
2506 .aiocb_size
= sizeof(NvmeDSMAIOCB
),
2507 .cancel_async
= nvme_dsm_cancel
,
2510 static void nvme_dsm_cb(void *opaque
, int ret
);
2512 static void nvme_dsm_md_cb(void *opaque
, int ret
)
2514 NvmeDSMAIOCB
*iocb
= opaque
;
2515 NvmeRequest
*req
= iocb
->req
;
2516 NvmeNamespace
*ns
= req
->ns
;
2517 NvmeDsmRange
*range
;
2521 if (ret
< 0 || iocb
->ret
< 0 || !ns
->lbaf
.ms
) {
2525 range
= &iocb
->range
[iocb
->idx
- 1];
2526 slba
= le64_to_cpu(range
->slba
);
2527 nlb
= le32_to_cpu(range
->nlb
);
2530 * Check that all block were discarded (zeroed); otherwise we do not zero
2534 ret
= nvme_block_status_all(ns
, slba
, nlb
, BDRV_BLOCK_ZERO
);
2540 nvme_dsm_cb(iocb
, 0);
2544 iocb
->aiocb
= blk_aio_pwrite_zeroes(ns
->blkconf
.blk
, nvme_moff(ns
, slba
),
2545 nvme_m2b(ns
, nlb
), BDRV_REQ_MAY_UNMAP
,
2550 nvme_dsm_cb(iocb
, ret
);
2553 static void nvme_dsm_cb(void *opaque
, int ret
)
2555 NvmeDSMAIOCB
*iocb
= opaque
;
2556 NvmeRequest
*req
= iocb
->req
;
2557 NvmeCtrl
*n
= nvme_ctrl(req
);
2558 NvmeNamespace
*ns
= req
->ns
;
2559 NvmeDsmRange
*range
;
2563 if (iocb
->ret
< 0) {
2565 } else if (ret
< 0) {
2571 if (iocb
->idx
== iocb
->nr
) {
2575 range
= &iocb
->range
[iocb
->idx
++];
2576 slba
= le64_to_cpu(range
->slba
);
2577 nlb
= le32_to_cpu(range
->nlb
);
2579 trace_pci_nvme_dsm_deallocate(slba
, nlb
);
2581 if (nlb
> n
->dmrsl
) {
2582 trace_pci_nvme_dsm_single_range_limit_exceeded(nlb
, n
->dmrsl
);
2586 if (nvme_check_bounds(ns
, slba
, nlb
)) {
2587 trace_pci_nvme_err_invalid_lba_range(slba
, nlb
,
2592 iocb
->aiocb
= blk_aio_pdiscard(ns
->blkconf
.blk
, nvme_l2b(ns
, slba
),
2594 nvme_dsm_md_cb
, iocb
);
2599 iocb
->common
.cb(iocb
->common
.opaque
, iocb
->ret
);
2600 qemu_aio_unref(iocb
);
2603 static uint16_t nvme_dsm(NvmeCtrl
*n
, NvmeRequest
*req
)
2605 NvmeNamespace
*ns
= req
->ns
;
2606 NvmeDsmCmd
*dsm
= (NvmeDsmCmd
*) &req
->cmd
;
2607 uint32_t attr
= le32_to_cpu(dsm
->attributes
);
2608 uint32_t nr
= (le32_to_cpu(dsm
->nr
) & 0xff) + 1;
2609 uint16_t status
= NVME_SUCCESS
;
2611 trace_pci_nvme_dsm(nr
, attr
);
2613 if (attr
& NVME_DSMGMT_AD
) {
2614 NvmeDSMAIOCB
*iocb
= blk_aio_get(&nvme_dsm_aiocb_info
, ns
->blkconf
.blk
,
2619 iocb
->range
= g_new(NvmeDsmRange
, nr
);
2623 status
= nvme_h2c(n
, (uint8_t *)iocb
->range
, sizeof(NvmeDsmRange
) * nr
,
2626 g_free(iocb
->range
);
2627 qemu_aio_unref(iocb
);
2632 req
->aiocb
= &iocb
->common
;
2633 nvme_dsm_cb(iocb
, 0);
2635 return NVME_NO_COMPLETE
;
2641 static uint16_t nvme_verify(NvmeCtrl
*n
, NvmeRequest
*req
)
2643 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
2644 NvmeNamespace
*ns
= req
->ns
;
2645 BlockBackend
*blk
= ns
->blkconf
.blk
;
2646 uint64_t slba
= le64_to_cpu(rw
->slba
);
2647 uint32_t nlb
= le16_to_cpu(rw
->nlb
) + 1;
2648 size_t len
= nvme_l2b(ns
, nlb
);
2649 int64_t offset
= nvme_l2b(ns
, slba
);
2650 uint8_t prinfo
= NVME_RW_PRINFO(le16_to_cpu(rw
->control
));
2651 uint32_t reftag
= le32_to_cpu(rw
->reftag
);
2652 NvmeBounceContext
*ctx
= NULL
;
2655 trace_pci_nvme_verify(nvme_cid(req
), nvme_nsid(ns
), slba
, nlb
);
2657 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
2658 status
= nvme_check_prinfo(ns
, prinfo
, slba
, reftag
);
2663 if (prinfo
& NVME_PRINFO_PRACT
) {
2664 return NVME_INVALID_PROT_INFO
| NVME_DNR
;
2668 if (len
> n
->page_size
<< n
->params
.vsl
) {
2669 return NVME_INVALID_FIELD
| NVME_DNR
;
2672 status
= nvme_check_bounds(ns
, slba
, nlb
);
2677 if (NVME_ERR_REC_DULBE(ns
->features
.err_rec
)) {
2678 status
= nvme_check_dulbe(ns
, slba
, nlb
);
2684 ctx
= g_new0(NvmeBounceContext
, 1);
2687 ctx
->data
.bounce
= g_malloc(len
);
2689 qemu_iovec_init(&ctx
->data
.iov
, 1);
2690 qemu_iovec_add(&ctx
->data
.iov
, ctx
->data
.bounce
, len
);
2692 block_acct_start(blk_get_stats(blk
), &req
->acct
, ctx
->data
.iov
.size
,
2695 req
->aiocb
= blk_aio_preadv(ns
->blkconf
.blk
, offset
, &ctx
->data
.iov
, 0,
2696 nvme_verify_mdata_in_cb
, ctx
);
2697 return NVME_NO_COMPLETE
;
2700 typedef struct NvmeCopyAIOCB
{
2707 unsigned int format
;
2714 BlockAcctCookie read
;
2715 BlockAcctCookie write
;
2724 static void nvme_copy_cancel(BlockAIOCB
*aiocb
)
2726 NvmeCopyAIOCB
*iocb
= container_of(aiocb
, NvmeCopyAIOCB
, common
);
2728 iocb
->ret
= -ECANCELED
;
2731 blk_aio_cancel_async(iocb
->aiocb
);
2736 static const AIOCBInfo nvme_copy_aiocb_info
= {
2737 .aiocb_size
= sizeof(NvmeCopyAIOCB
),
2738 .cancel_async
= nvme_copy_cancel
,
2741 static void nvme_copy_done(NvmeCopyAIOCB
*iocb
)
2743 NvmeRequest
*req
= iocb
->req
;
2744 NvmeNamespace
*ns
= req
->ns
;
2745 BlockAcctStats
*stats
= blk_get_stats(ns
->blkconf
.blk
);
2747 if (iocb
->idx
!= iocb
->nr
) {
2748 req
->cqe
.result
= cpu_to_le32(iocb
->idx
);
2751 qemu_iovec_destroy(&iocb
->iov
);
2752 g_free(iocb
->bounce
);
2754 if (iocb
->ret
< 0) {
2755 block_acct_failed(stats
, &iocb
->acct
.read
);
2756 block_acct_failed(stats
, &iocb
->acct
.write
);
2758 block_acct_done(stats
, &iocb
->acct
.read
);
2759 block_acct_done(stats
, &iocb
->acct
.write
);
2762 iocb
->common
.cb(iocb
->common
.opaque
, iocb
->ret
);
2763 qemu_aio_unref(iocb
);
2766 static void nvme_do_copy(NvmeCopyAIOCB
*iocb
);
2768 static void nvme_copy_source_range_parse_format0(void *ranges
, int idx
,
2769 uint64_t *slba
, uint32_t *nlb
,
2774 NvmeCopySourceRangeFormat0
*_ranges
= ranges
;
2777 *slba
= le64_to_cpu(_ranges
[idx
].slba
);
2781 *nlb
= le16_to_cpu(_ranges
[idx
].nlb
) + 1;
2785 *apptag
= le16_to_cpu(_ranges
[idx
].apptag
);
2789 *appmask
= le16_to_cpu(_ranges
[idx
].appmask
);
2793 *reftag
= le32_to_cpu(_ranges
[idx
].reftag
);
2797 static void nvme_copy_source_range_parse_format1(void *ranges
, int idx
,
2798 uint64_t *slba
, uint32_t *nlb
,
2803 NvmeCopySourceRangeFormat1
*_ranges
= ranges
;
2806 *slba
= le64_to_cpu(_ranges
[idx
].slba
);
2810 *nlb
= le16_to_cpu(_ranges
[idx
].nlb
) + 1;
2814 *apptag
= le16_to_cpu(_ranges
[idx
].apptag
);
2818 *appmask
= le16_to_cpu(_ranges
[idx
].appmask
);
2824 *reftag
|= (uint64_t)_ranges
[idx
].sr
[4] << 40;
2825 *reftag
|= (uint64_t)_ranges
[idx
].sr
[5] << 32;
2826 *reftag
|= (uint64_t)_ranges
[idx
].sr
[6] << 24;
2827 *reftag
|= (uint64_t)_ranges
[idx
].sr
[7] << 16;
2828 *reftag
|= (uint64_t)_ranges
[idx
].sr
[8] << 8;
2829 *reftag
|= (uint64_t)_ranges
[idx
].sr
[9];
2833 static void nvme_copy_source_range_parse(void *ranges
, int idx
, uint8_t format
,
2834 uint64_t *slba
, uint32_t *nlb
,
2835 uint16_t *apptag
, uint16_t *appmask
,
2839 case NVME_COPY_FORMAT_0
:
2840 nvme_copy_source_range_parse_format0(ranges
, idx
, slba
, nlb
, apptag
,
2844 case NVME_COPY_FORMAT_1
:
2845 nvme_copy_source_range_parse_format1(ranges
, idx
, slba
, nlb
, apptag
,
2854 static inline uint16_t nvme_check_copy_mcl(NvmeNamespace
*ns
,
2855 NvmeCopyAIOCB
*iocb
, uint16_t nr
)
2857 uint32_t copy_len
= 0;
2859 for (int idx
= 0; idx
< nr
; idx
++) {
2861 nvme_copy_source_range_parse(iocb
->ranges
, idx
, iocb
->format
, NULL
,
2862 &nlb
, NULL
, NULL
, NULL
);
2863 copy_len
+= nlb
+ 1;
2866 if (copy_len
> ns
->id_ns
.mcl
) {
2867 return NVME_CMD_SIZE_LIMIT
| NVME_DNR
;
2870 return NVME_SUCCESS
;
2873 static void nvme_copy_out_completed_cb(void *opaque
, int ret
)
2875 NvmeCopyAIOCB
*iocb
= opaque
;
2876 NvmeRequest
*req
= iocb
->req
;
2877 NvmeNamespace
*ns
= req
->ns
;
2880 nvme_copy_source_range_parse(iocb
->ranges
, iocb
->idx
, iocb
->format
, NULL
,
2881 &nlb
, NULL
, NULL
, NULL
);
2886 } else if (iocb
->ret
< 0) {
2890 if (ns
->params
.zoned
) {
2891 nvme_advance_zone_wp(ns
, iocb
->zone
, nlb
);
2900 static void nvme_copy_out_cb(void *opaque
, int ret
)
2902 NvmeCopyAIOCB
*iocb
= opaque
;
2903 NvmeRequest
*req
= iocb
->req
;
2904 NvmeNamespace
*ns
= req
->ns
;
2909 if (ret
< 0 || iocb
->ret
< 0 || !ns
->lbaf
.ms
) {
2913 nvme_copy_source_range_parse(iocb
->ranges
, iocb
->idx
, iocb
->format
, NULL
,
2914 &nlb
, NULL
, NULL
, NULL
);
2916 mlen
= nvme_m2b(ns
, nlb
);
2917 mbounce
= iocb
->bounce
+ nvme_l2b(ns
, nlb
);
2919 qemu_iovec_reset(&iocb
->iov
);
2920 qemu_iovec_add(&iocb
->iov
, mbounce
, mlen
);
2922 iocb
->aiocb
= blk_aio_pwritev(ns
->blkconf
.blk
, nvme_moff(ns
, iocb
->slba
),
2923 &iocb
->iov
, 0, nvme_copy_out_completed_cb
,
2929 nvme_copy_out_completed_cb(iocb
, ret
);
2932 static void nvme_copy_in_completed_cb(void *opaque
, int ret
)
2934 NvmeCopyAIOCB
*iocb
= opaque
;
2935 NvmeRequest
*req
= iocb
->req
;
2936 NvmeNamespace
*ns
= req
->ns
;
2939 uint16_t apptag
, appmask
;
2947 } else if (iocb
->ret
< 0) {
2951 nvme_copy_source_range_parse(iocb
->ranges
, iocb
->idx
, iocb
->format
, &slba
,
2952 &nlb
, &apptag
, &appmask
, &reftag
);
2953 len
= nvme_l2b(ns
, nlb
);
2955 trace_pci_nvme_copy_out(iocb
->slba
, nlb
);
2957 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
2958 NvmeCopyCmd
*copy
= (NvmeCopyCmd
*)&req
->cmd
;
2960 uint16_t prinfor
= ((copy
->control
[0] >> 4) & 0xf);
2961 uint16_t prinfow
= ((copy
->control
[2] >> 2) & 0xf);
2963 size_t mlen
= nvme_m2b(ns
, nlb
);
2964 uint8_t *mbounce
= iocb
->bounce
+ nvme_l2b(ns
, nlb
);
2966 status
= nvme_dif_mangle_mdata(ns
, mbounce
, mlen
, slba
);
2970 status
= nvme_dif_check(ns
, iocb
->bounce
, len
, mbounce
, mlen
, prinfor
,
2971 slba
, apptag
, appmask
, &reftag
);
2976 apptag
= le16_to_cpu(copy
->apptag
);
2977 appmask
= le16_to_cpu(copy
->appmask
);
2979 if (prinfow
& NVME_PRINFO_PRACT
) {
2980 status
= nvme_check_prinfo(ns
, prinfow
, iocb
->slba
, iocb
->reftag
);
2985 nvme_dif_pract_generate_dif(ns
, iocb
->bounce
, len
, mbounce
, mlen
,
2986 apptag
, &iocb
->reftag
);
2988 status
= nvme_dif_check(ns
, iocb
->bounce
, len
, mbounce
, mlen
,
2989 prinfow
, iocb
->slba
, apptag
, appmask
,
2997 status
= nvme_check_bounds(ns
, iocb
->slba
, nlb
);
3002 if (ns
->params
.zoned
) {
3003 status
= nvme_check_zone_write(ns
, iocb
->zone
, iocb
->slba
, nlb
);
3008 if (!(iocb
->zone
->d
.za
& NVME_ZA_ZRWA_VALID
)) {
3009 iocb
->zone
->w_ptr
+= nlb
;
3013 qemu_iovec_reset(&iocb
->iov
);
3014 qemu_iovec_add(&iocb
->iov
, iocb
->bounce
, len
);
3016 iocb
->aiocb
= blk_aio_pwritev(ns
->blkconf
.blk
, nvme_l2b(ns
, iocb
->slba
),
3017 &iocb
->iov
, 0, nvme_copy_out_cb
, iocb
);
3022 req
->status
= status
;
3028 static void nvme_copy_in_cb(void *opaque
, int ret
)
3030 NvmeCopyAIOCB
*iocb
= opaque
;
3031 NvmeRequest
*req
= iocb
->req
;
3032 NvmeNamespace
*ns
= req
->ns
;
3036 if (ret
< 0 || iocb
->ret
< 0 || !ns
->lbaf
.ms
) {
3040 nvme_copy_source_range_parse(iocb
->ranges
, iocb
->idx
, iocb
->format
, &slba
,
3041 &nlb
, NULL
, NULL
, NULL
);
3043 qemu_iovec_reset(&iocb
->iov
);
3044 qemu_iovec_add(&iocb
->iov
, iocb
->bounce
+ nvme_l2b(ns
, nlb
),
3047 iocb
->aiocb
= blk_aio_preadv(ns
->blkconf
.blk
, nvme_moff(ns
, slba
),
3048 &iocb
->iov
, 0, nvme_copy_in_completed_cb
,
3053 nvme_copy_in_completed_cb(iocb
, ret
);
3056 static void nvme_do_copy(NvmeCopyAIOCB
*iocb
)
3058 NvmeRequest
*req
= iocb
->req
;
3059 NvmeNamespace
*ns
= req
->ns
;
3065 if (iocb
->ret
< 0) {
3069 if (iocb
->idx
== iocb
->nr
) {
3073 nvme_copy_source_range_parse(iocb
->ranges
, iocb
->idx
, iocb
->format
, &slba
,
3074 &nlb
, NULL
, NULL
, NULL
);
3075 len
= nvme_l2b(ns
, nlb
);
3077 trace_pci_nvme_copy_source_range(slba
, nlb
);
3079 if (nlb
> le16_to_cpu(ns
->id_ns
.mssrl
)) {
3080 status
= NVME_CMD_SIZE_LIMIT
| NVME_DNR
;
3084 status
= nvme_check_bounds(ns
, slba
, nlb
);
3089 if (NVME_ERR_REC_DULBE(ns
->features
.err_rec
)) {
3090 status
= nvme_check_dulbe(ns
, slba
, nlb
);
3096 if (ns
->params
.zoned
) {
3097 status
= nvme_check_zone_read(ns
, slba
, nlb
);
3103 qemu_iovec_reset(&iocb
->iov
);
3104 qemu_iovec_add(&iocb
->iov
, iocb
->bounce
, len
);
3106 iocb
->aiocb
= blk_aio_preadv(ns
->blkconf
.blk
, nvme_l2b(ns
, slba
),
3107 &iocb
->iov
, 0, nvme_copy_in_cb
, iocb
);
3111 req
->status
= status
;
3114 nvme_copy_done(iocb
);
3117 static uint16_t nvme_copy(NvmeCtrl
*n
, NvmeRequest
*req
)
3119 NvmeNamespace
*ns
= req
->ns
;
3120 NvmeCopyCmd
*copy
= (NvmeCopyCmd
*)&req
->cmd
;
3121 NvmeCopyAIOCB
*iocb
= blk_aio_get(&nvme_copy_aiocb_info
, ns
->blkconf
.blk
,
3123 uint16_t nr
= copy
->nr
+ 1;
3124 uint8_t format
= copy
->control
[0] & 0xf;
3125 uint16_t prinfor
= ((copy
->control
[0] >> 4) & 0xf);
3126 uint16_t prinfow
= ((copy
->control
[2] >> 2) & 0xf);
3127 size_t len
= sizeof(NvmeCopySourceRangeFormat0
);
3131 trace_pci_nvme_copy(nvme_cid(req
), nvme_nsid(ns
), nr
, format
);
3133 iocb
->ranges
= NULL
;
3136 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
) &&
3137 ((prinfor
& NVME_PRINFO_PRACT
) != (prinfow
& NVME_PRINFO_PRACT
))) {
3138 status
= NVME_INVALID_FIELD
| NVME_DNR
;
3142 if (!(n
->id_ctrl
.ocfs
& (1 << format
))) {
3143 trace_pci_nvme_err_copy_invalid_format(format
);
3144 status
= NVME_INVALID_FIELD
| NVME_DNR
;
3148 if (nr
> ns
->id_ns
.msrc
+ 1) {
3149 status
= NVME_CMD_SIZE_LIMIT
| NVME_DNR
;
3153 if ((ns
->pif
== 0x0 && format
!= 0x0) ||
3154 (ns
->pif
!= 0x0 && format
!= 0x1)) {
3155 status
= NVME_INVALID_FORMAT
| NVME_DNR
;
3160 len
= sizeof(NvmeCopySourceRangeFormat1
);
3163 iocb
->format
= format
;
3164 iocb
->ranges
= g_malloc_n(nr
, len
);
3165 status
= nvme_h2c(n
, (uint8_t *)iocb
->ranges
, len
* nr
, req
);
3170 iocb
->slba
= le64_to_cpu(copy
->sdlba
);
3172 if (ns
->params
.zoned
) {
3173 iocb
->zone
= nvme_get_zone_by_slba(ns
, iocb
->slba
);
3175 status
= NVME_LBA_RANGE
| NVME_DNR
;
3179 status
= nvme_zrm_auto(n
, ns
, iocb
->zone
);
3185 status
= nvme_check_copy_mcl(ns
, iocb
, nr
);
3194 iocb
->reftag
= le32_to_cpu(copy
->reftag
);
3195 iocb
->reftag
|= (uint64_t)le32_to_cpu(copy
->cdw3
) << 32;
3196 iocb
->bounce
= g_malloc_n(le16_to_cpu(ns
->id_ns
.mssrl
),
3197 ns
->lbasz
+ ns
->lbaf
.ms
);
3199 qemu_iovec_init(&iocb
->iov
, 1);
3201 block_acct_start(blk_get_stats(ns
->blkconf
.blk
), &iocb
->acct
.read
, 0,
3203 block_acct_start(blk_get_stats(ns
->blkconf
.blk
), &iocb
->acct
.write
, 0,
3206 req
->aiocb
= &iocb
->common
;
3209 return NVME_NO_COMPLETE
;
3212 g_free(iocb
->ranges
);
3213 qemu_aio_unref(iocb
);
3217 static uint16_t nvme_compare(NvmeCtrl
*n
, NvmeRequest
*req
)
3219 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
3220 NvmeNamespace
*ns
= req
->ns
;
3221 BlockBackend
*blk
= ns
->blkconf
.blk
;
3222 uint64_t slba
= le64_to_cpu(rw
->slba
);
3223 uint32_t nlb
= le16_to_cpu(rw
->nlb
) + 1;
3224 uint8_t prinfo
= NVME_RW_PRINFO(le16_to_cpu(rw
->control
));
3225 size_t data_len
= nvme_l2b(ns
, nlb
);
3226 size_t len
= data_len
;
3227 int64_t offset
= nvme_l2b(ns
, slba
);
3228 struct nvme_compare_ctx
*ctx
= NULL
;
3231 trace_pci_nvme_compare(nvme_cid(req
), nvme_nsid(ns
), slba
, nlb
);
3233 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
) && (prinfo
& NVME_PRINFO_PRACT
)) {
3234 return NVME_INVALID_PROT_INFO
| NVME_DNR
;
3237 if (nvme_ns_ext(ns
)) {
3238 len
+= nvme_m2b(ns
, nlb
);
3241 status
= nvme_check_mdts(n
, len
);
3246 status
= nvme_check_bounds(ns
, slba
, nlb
);
3251 if (NVME_ERR_REC_DULBE(ns
->features
.err_rec
)) {
3252 status
= nvme_check_dulbe(ns
, slba
, nlb
);
3258 status
= nvme_map_dptr(n
, &req
->sg
, len
, &req
->cmd
);
3263 ctx
= g_new(struct nvme_compare_ctx
, 1);
3264 ctx
->data
.bounce
= g_malloc(data_len
);
3268 qemu_iovec_init(&ctx
->data
.iov
, 1);
3269 qemu_iovec_add(&ctx
->data
.iov
, ctx
->data
.bounce
, data_len
);
3271 block_acct_start(blk_get_stats(blk
), &req
->acct
, data_len
,
3273 req
->aiocb
= blk_aio_preadv(blk
, offset
, &ctx
->data
.iov
, 0,
3274 nvme_compare_data_cb
, req
);
3276 return NVME_NO_COMPLETE
;
3279 typedef struct NvmeFlushAIOCB
{
3290 static void nvme_flush_cancel(BlockAIOCB
*acb
)
3292 NvmeFlushAIOCB
*iocb
= container_of(acb
, NvmeFlushAIOCB
, common
);
3294 iocb
->ret
= -ECANCELED
;
3297 blk_aio_cancel_async(iocb
->aiocb
);
3302 static const AIOCBInfo nvme_flush_aiocb_info
= {
3303 .aiocb_size
= sizeof(NvmeFlushAIOCB
),
3304 .cancel_async
= nvme_flush_cancel
,
3305 .get_aio_context
= nvme_get_aio_context
,
3308 static void nvme_do_flush(NvmeFlushAIOCB
*iocb
);
3310 static void nvme_flush_ns_cb(void *opaque
, int ret
)
3312 NvmeFlushAIOCB
*iocb
= opaque
;
3313 NvmeNamespace
*ns
= iocb
->ns
;
3318 } else if (iocb
->ret
< 0) {
3323 trace_pci_nvme_flush_ns(iocb
->nsid
);
3326 iocb
->aiocb
= blk_aio_flush(ns
->blkconf
.blk
, nvme_flush_ns_cb
, iocb
);
3331 nvme_do_flush(iocb
);
3334 static void nvme_do_flush(NvmeFlushAIOCB
*iocb
)
3336 NvmeRequest
*req
= iocb
->req
;
3337 NvmeCtrl
*n
= nvme_ctrl(req
);
3340 if (iocb
->ret
< 0) {
3344 if (iocb
->broadcast
) {
3345 for (i
= iocb
->nsid
+ 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
3346 iocb
->ns
= nvme_ns(n
, i
);
3358 nvme_flush_ns_cb(iocb
, 0);
3362 iocb
->common
.cb(iocb
->common
.opaque
, iocb
->ret
);
3363 qemu_aio_unref(iocb
);
3366 static uint16_t nvme_flush(NvmeCtrl
*n
, NvmeRequest
*req
)
3368 NvmeFlushAIOCB
*iocb
;
3369 uint32_t nsid
= le32_to_cpu(req
->cmd
.nsid
);
3372 iocb
= qemu_aio_get(&nvme_flush_aiocb_info
, NULL
, nvme_misc_cb
, req
);
3378 iocb
->broadcast
= (nsid
== NVME_NSID_BROADCAST
);
3380 if (!iocb
->broadcast
) {
3381 if (!nvme_nsid_valid(n
, nsid
)) {
3382 status
= NVME_INVALID_NSID
| NVME_DNR
;
3386 iocb
->ns
= nvme_ns(n
, nsid
);
3388 status
= NVME_INVALID_FIELD
| NVME_DNR
;
3395 req
->aiocb
= &iocb
->common
;
3396 nvme_do_flush(iocb
);
3398 return NVME_NO_COMPLETE
;
3401 qemu_aio_unref(iocb
);
3406 static uint16_t nvme_read(NvmeCtrl
*n
, NvmeRequest
*req
)
3408 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
3409 NvmeNamespace
*ns
= req
->ns
;
3410 uint64_t slba
= le64_to_cpu(rw
->slba
);
3411 uint32_t nlb
= (uint32_t)le16_to_cpu(rw
->nlb
) + 1;
3412 uint8_t prinfo
= NVME_RW_PRINFO(le16_to_cpu(rw
->control
));
3413 uint64_t data_size
= nvme_l2b(ns
, nlb
);
3414 uint64_t mapped_size
= data_size
;
3415 uint64_t data_offset
;
3416 BlockBackend
*blk
= ns
->blkconf
.blk
;
3419 if (nvme_ns_ext(ns
)) {
3420 mapped_size
+= nvme_m2b(ns
, nlb
);
3422 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
3423 bool pract
= prinfo
& NVME_PRINFO_PRACT
;
3425 if (pract
&& ns
->lbaf
.ms
== nvme_pi_tuple_size(ns
)) {
3426 mapped_size
= data_size
;
3431 trace_pci_nvme_read(nvme_cid(req
), nvme_nsid(ns
), nlb
, mapped_size
, slba
);
3433 status
= nvme_check_mdts(n
, mapped_size
);
3438 status
= nvme_check_bounds(ns
, slba
, nlb
);
3443 if (ns
->params
.zoned
) {
3444 status
= nvme_check_zone_read(ns
, slba
, nlb
);
3446 trace_pci_nvme_err_zone_read_not_ok(slba
, nlb
, status
);
3451 if (NVME_ERR_REC_DULBE(ns
->features
.err_rec
)) {
3452 status
= nvme_check_dulbe(ns
, slba
, nlb
);
3458 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
3459 return nvme_dif_rw(n
, req
);
3462 status
= nvme_map_data(n
, nlb
, req
);
3467 data_offset
= nvme_l2b(ns
, slba
);
3469 block_acct_start(blk_get_stats(blk
), &req
->acct
, data_size
,
3471 nvme_blk_read(blk
, data_offset
, BDRV_SECTOR_SIZE
, nvme_rw_cb
, req
);
3472 return NVME_NO_COMPLETE
;
3475 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
3476 return status
| NVME_DNR
;
3479 static void nvme_do_write_fdp(NvmeCtrl
*n
, NvmeRequest
*req
, uint64_t slba
,
3482 NvmeNamespace
*ns
= req
->ns
;
3483 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
3484 uint64_t data_size
= nvme_l2b(ns
, nlb
);
3485 uint32_t dw12
= le32_to_cpu(req
->cmd
.cdw12
);
3486 uint8_t dtype
= (dw12
>> 20) & 0xf;
3487 uint16_t pid
= le16_to_cpu(rw
->dspec
);
3488 uint16_t ph
, rg
, ruhid
;
3489 NvmeReclaimUnit
*ru
;
3491 if (dtype
!= NVME_DIRECTIVE_DATA_PLACEMENT
||
3492 !nvme_parse_pid(ns
, pid
, &ph
, &rg
)) {
3497 ruhid
= ns
->fdp
.phs
[ph
];
3498 ru
= &ns
->endgrp
->fdp
.ruhs
[ruhid
].rus
[rg
];
3500 nvme_fdp_stat_inc(&ns
->endgrp
->fdp
.hbmw
, data_size
);
3501 nvme_fdp_stat_inc(&ns
->endgrp
->fdp
.mbmw
, data_size
);
3504 if (nlb
< ru
->ruamw
) {
3510 nvme_update_ruh(n
, ns
, pid
);
3514 static uint16_t nvme_do_write(NvmeCtrl
*n
, NvmeRequest
*req
, bool append
,
3517 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
3518 NvmeNamespace
*ns
= req
->ns
;
3519 uint64_t slba
= le64_to_cpu(rw
->slba
);
3520 uint32_t nlb
= (uint32_t)le16_to_cpu(rw
->nlb
) + 1;
3521 uint16_t ctrl
= le16_to_cpu(rw
->control
);
3522 uint8_t prinfo
= NVME_RW_PRINFO(ctrl
);
3523 uint64_t data_size
= nvme_l2b(ns
, nlb
);
3524 uint64_t mapped_size
= data_size
;
3525 uint64_t data_offset
;
3527 NvmeZonedResult
*res
= (NvmeZonedResult
*)&req
->cqe
;
3528 BlockBackend
*blk
= ns
->blkconf
.blk
;
3531 if (nvme_ns_ext(ns
)) {
3532 mapped_size
+= nvme_m2b(ns
, nlb
);
3534 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
3535 bool pract
= prinfo
& NVME_PRINFO_PRACT
;
3537 if (pract
&& ns
->lbaf
.ms
== nvme_pi_tuple_size(ns
)) {
3538 mapped_size
-= nvme_m2b(ns
, nlb
);
3543 trace_pci_nvme_write(nvme_cid(req
), nvme_io_opc_str(rw
->opcode
),
3544 nvme_nsid(ns
), nlb
, mapped_size
, slba
);
3547 status
= nvme_check_mdts(n
, mapped_size
);
3553 status
= nvme_check_bounds(ns
, slba
, nlb
);
3558 if (ns
->params
.zoned
) {
3559 zone
= nvme_get_zone_by_slba(ns
, slba
);
3563 bool piremap
= !!(ctrl
& NVME_RW_PIREMAP
);
3565 if (unlikely(zone
->d
.za
& NVME_ZA_ZRWA_VALID
)) {
3566 return NVME_INVALID_ZONE_OP
| NVME_DNR
;
3569 if (unlikely(slba
!= zone
->d
.zslba
)) {
3570 trace_pci_nvme_err_append_not_at_start(slba
, zone
->d
.zslba
);
3571 status
= NVME_INVALID_FIELD
;
3575 if (n
->params
.zasl
&&
3576 data_size
> (uint64_t)n
->page_size
<< n
->params
.zasl
) {
3577 trace_pci_nvme_err_zasl(data_size
);
3578 return NVME_INVALID_FIELD
| NVME_DNR
;
3582 rw
->slba
= cpu_to_le64(slba
);
3583 res
->slba
= cpu_to_le64(slba
);
3585 switch (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
3586 case NVME_ID_NS_DPS_TYPE_1
:
3588 return NVME_INVALID_PROT_INFO
| NVME_DNR
;
3593 case NVME_ID_NS_DPS_TYPE_2
:
3595 uint32_t reftag
= le32_to_cpu(rw
->reftag
);
3596 rw
->reftag
= cpu_to_le32(reftag
+ (slba
- zone
->d
.zslba
));
3601 case NVME_ID_NS_DPS_TYPE_3
:
3603 return NVME_INVALID_PROT_INFO
| NVME_DNR
;
3610 status
= nvme_check_zone_write(ns
, zone
, slba
, nlb
);
3615 status
= nvme_zrm_auto(n
, ns
, zone
);
3620 if (!(zone
->d
.za
& NVME_ZA_ZRWA_VALID
)) {
3623 } else if (ns
->endgrp
&& ns
->endgrp
->fdp
.enabled
) {
3624 nvme_do_write_fdp(n
, req
, slba
, nlb
);
3627 data_offset
= nvme_l2b(ns
, slba
);
3629 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
3630 return nvme_dif_rw(n
, req
);
3634 status
= nvme_map_data(n
, nlb
, req
);
3639 block_acct_start(blk_get_stats(blk
), &req
->acct
, data_size
,
3641 nvme_blk_write(blk
, data_offset
, BDRV_SECTOR_SIZE
, nvme_rw_cb
, req
);
3643 req
->aiocb
= blk_aio_pwrite_zeroes(blk
, data_offset
, data_size
,
3644 BDRV_REQ_MAY_UNMAP
, nvme_rw_cb
,
3648 return NVME_NO_COMPLETE
;
3651 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
3652 return status
| NVME_DNR
;
3655 static inline uint16_t nvme_write(NvmeCtrl
*n
, NvmeRequest
*req
)
3657 return nvme_do_write(n
, req
, false, false);
3660 static inline uint16_t nvme_write_zeroes(NvmeCtrl
*n
, NvmeRequest
*req
)
3662 return nvme_do_write(n
, req
, false, true);
3665 static inline uint16_t nvme_zone_append(NvmeCtrl
*n
, NvmeRequest
*req
)
3667 return nvme_do_write(n
, req
, true, false);
3670 static uint16_t nvme_get_mgmt_zone_slba_idx(NvmeNamespace
*ns
, NvmeCmd
*c
,
3671 uint64_t *slba
, uint32_t *zone_idx
)
3673 uint32_t dw10
= le32_to_cpu(c
->cdw10
);
3674 uint32_t dw11
= le32_to_cpu(c
->cdw11
);
3676 if (!ns
->params
.zoned
) {
3677 trace_pci_nvme_err_invalid_opc(c
->opcode
);
3678 return NVME_INVALID_OPCODE
| NVME_DNR
;
3681 *slba
= ((uint64_t)dw11
) << 32 | dw10
;
3682 if (unlikely(*slba
>= ns
->id_ns
.nsze
)) {
3683 trace_pci_nvme_err_invalid_lba_range(*slba
, 0, ns
->id_ns
.nsze
);
3685 return NVME_LBA_RANGE
| NVME_DNR
;
3688 *zone_idx
= nvme_zone_idx(ns
, *slba
);
3689 assert(*zone_idx
< ns
->num_zones
);
3691 return NVME_SUCCESS
;
3694 typedef uint16_t (*op_handler_t
)(NvmeNamespace
*, NvmeZone
*, NvmeZoneState
,
3697 enum NvmeZoneProcessingMask
{
3698 NVME_PROC_CURRENT_ZONE
= 0,
3699 NVME_PROC_OPENED_ZONES
= 1 << 0,
3700 NVME_PROC_CLOSED_ZONES
= 1 << 1,
3701 NVME_PROC_READ_ONLY_ZONES
= 1 << 2,
3702 NVME_PROC_FULL_ZONES
= 1 << 3,
3705 static uint16_t nvme_open_zone(NvmeNamespace
*ns
, NvmeZone
*zone
,
3706 NvmeZoneState state
, NvmeRequest
*req
)
3708 NvmeZoneSendCmd
*cmd
= (NvmeZoneSendCmd
*)&req
->cmd
;
3711 if (cmd
->zsflags
& NVME_ZSFLAG_ZRWA_ALLOC
) {
3712 uint16_t ozcs
= le16_to_cpu(ns
->id_ns_zoned
->ozcs
);
3714 if (!(ozcs
& NVME_ID_NS_ZONED_OZCS_ZRWASUP
)) {
3715 return NVME_INVALID_ZONE_OP
| NVME_DNR
;
3718 if (zone
->w_ptr
% ns
->zns
.zrwafg
) {
3719 return NVME_NOZRWA
| NVME_DNR
;
3722 flags
= NVME_ZRM_ZRWA
;
3725 return nvme_zrm_open_flags(nvme_ctrl(req
), ns
, zone
, flags
);
3728 static uint16_t nvme_close_zone(NvmeNamespace
*ns
, NvmeZone
*zone
,
3729 NvmeZoneState state
, NvmeRequest
*req
)
3731 return nvme_zrm_close(ns
, zone
);
3734 static uint16_t nvme_finish_zone(NvmeNamespace
*ns
, NvmeZone
*zone
,
3735 NvmeZoneState state
, NvmeRequest
*req
)
3737 return nvme_zrm_finish(ns
, zone
);
3740 static uint16_t nvme_offline_zone(NvmeNamespace
*ns
, NvmeZone
*zone
,
3741 NvmeZoneState state
, NvmeRequest
*req
)
3744 case NVME_ZONE_STATE_READ_ONLY
:
3745 nvme_assign_zone_state(ns
, zone
, NVME_ZONE_STATE_OFFLINE
);
3747 case NVME_ZONE_STATE_OFFLINE
:
3748 return NVME_SUCCESS
;
3750 return NVME_ZONE_INVAL_TRANSITION
;
3754 static uint16_t nvme_set_zd_ext(NvmeNamespace
*ns
, NvmeZone
*zone
)
3757 uint8_t state
= nvme_get_zone_state(zone
);
3759 if (state
== NVME_ZONE_STATE_EMPTY
) {
3760 status
= nvme_aor_check(ns
, 1, 0);
3764 nvme_aor_inc_active(ns
);
3765 zone
->d
.za
|= NVME_ZA_ZD_EXT_VALID
;
3766 nvme_assign_zone_state(ns
, zone
, NVME_ZONE_STATE_CLOSED
);
3767 return NVME_SUCCESS
;
3770 return NVME_ZONE_INVAL_TRANSITION
;
3773 static uint16_t nvme_bulk_proc_zone(NvmeNamespace
*ns
, NvmeZone
*zone
,
3774 enum NvmeZoneProcessingMask proc_mask
,
3775 op_handler_t op_hndlr
, NvmeRequest
*req
)
3777 uint16_t status
= NVME_SUCCESS
;
3778 NvmeZoneState zs
= nvme_get_zone_state(zone
);
3782 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
3783 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
3784 proc_zone
= proc_mask
& NVME_PROC_OPENED_ZONES
;
3786 case NVME_ZONE_STATE_CLOSED
:
3787 proc_zone
= proc_mask
& NVME_PROC_CLOSED_ZONES
;
3789 case NVME_ZONE_STATE_READ_ONLY
:
3790 proc_zone
= proc_mask
& NVME_PROC_READ_ONLY_ZONES
;
3792 case NVME_ZONE_STATE_FULL
:
3793 proc_zone
= proc_mask
& NVME_PROC_FULL_ZONES
;
3800 status
= op_hndlr(ns
, zone
, zs
, req
);
3806 static uint16_t nvme_do_zone_op(NvmeNamespace
*ns
, NvmeZone
*zone
,
3807 enum NvmeZoneProcessingMask proc_mask
,
3808 op_handler_t op_hndlr
, NvmeRequest
*req
)
3811 uint16_t status
= NVME_SUCCESS
;
3815 status
= op_hndlr(ns
, zone
, nvme_get_zone_state(zone
), req
);
3817 if (proc_mask
& NVME_PROC_CLOSED_ZONES
) {
3818 QTAILQ_FOREACH_SAFE(zone
, &ns
->closed_zones
, entry
, next
) {
3819 status
= nvme_bulk_proc_zone(ns
, zone
, proc_mask
, op_hndlr
,
3821 if (status
&& status
!= NVME_NO_COMPLETE
) {
3826 if (proc_mask
& NVME_PROC_OPENED_ZONES
) {
3827 QTAILQ_FOREACH_SAFE(zone
, &ns
->imp_open_zones
, entry
, next
) {
3828 status
= nvme_bulk_proc_zone(ns
, zone
, proc_mask
, op_hndlr
,
3830 if (status
&& status
!= NVME_NO_COMPLETE
) {
3835 QTAILQ_FOREACH_SAFE(zone
, &ns
->exp_open_zones
, entry
, next
) {
3836 status
= nvme_bulk_proc_zone(ns
, zone
, proc_mask
, op_hndlr
,
3838 if (status
&& status
!= NVME_NO_COMPLETE
) {
3843 if (proc_mask
& NVME_PROC_FULL_ZONES
) {
3844 QTAILQ_FOREACH_SAFE(zone
, &ns
->full_zones
, entry
, next
) {
3845 status
= nvme_bulk_proc_zone(ns
, zone
, proc_mask
, op_hndlr
,
3847 if (status
&& status
!= NVME_NO_COMPLETE
) {
3853 if (proc_mask
& NVME_PROC_READ_ONLY_ZONES
) {
3854 for (i
= 0; i
< ns
->num_zones
; i
++, zone
++) {
3855 status
= nvme_bulk_proc_zone(ns
, zone
, proc_mask
, op_hndlr
,
3857 if (status
&& status
!= NVME_NO_COMPLETE
) {
3868 typedef struct NvmeZoneResetAIOCB
{
3877 } NvmeZoneResetAIOCB
;
3879 static void nvme_zone_reset_cancel(BlockAIOCB
*aiocb
)
3881 NvmeZoneResetAIOCB
*iocb
= container_of(aiocb
, NvmeZoneResetAIOCB
, common
);
3882 NvmeRequest
*req
= iocb
->req
;
3883 NvmeNamespace
*ns
= req
->ns
;
3885 iocb
->idx
= ns
->num_zones
;
3887 iocb
->ret
= -ECANCELED
;
3890 blk_aio_cancel_async(iocb
->aiocb
);
3895 static const AIOCBInfo nvme_zone_reset_aiocb_info
= {
3896 .aiocb_size
= sizeof(NvmeZoneResetAIOCB
),
3897 .cancel_async
= nvme_zone_reset_cancel
,
3900 static void nvme_zone_reset_cb(void *opaque
, int ret
);
3902 static void nvme_zone_reset_epilogue_cb(void *opaque
, int ret
)
3904 NvmeZoneResetAIOCB
*iocb
= opaque
;
3905 NvmeRequest
*req
= iocb
->req
;
3906 NvmeNamespace
*ns
= req
->ns
;
3910 if (ret
< 0 || iocb
->ret
< 0 || !ns
->lbaf
.ms
) {
3914 moff
= nvme_moff(ns
, iocb
->zone
->d
.zslba
);
3915 count
= nvme_m2b(ns
, ns
->zone_size
);
3917 iocb
->aiocb
= blk_aio_pwrite_zeroes(ns
->blkconf
.blk
, moff
, count
,
3919 nvme_zone_reset_cb
, iocb
);
3923 nvme_zone_reset_cb(iocb
, ret
);
3926 static void nvme_zone_reset_cb(void *opaque
, int ret
)
3928 NvmeZoneResetAIOCB
*iocb
= opaque
;
3929 NvmeRequest
*req
= iocb
->req
;
3930 NvmeNamespace
*ns
= req
->ns
;
3932 if (iocb
->ret
< 0) {
3934 } else if (ret
< 0) {
3940 nvme_zrm_reset(ns
, iocb
->zone
);
3947 while (iocb
->idx
< ns
->num_zones
) {
3948 NvmeZone
*zone
= &ns
->zone_array
[iocb
->idx
++];
3950 switch (nvme_get_zone_state(zone
)) {
3951 case NVME_ZONE_STATE_EMPTY
:
3958 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
3959 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
3960 case NVME_ZONE_STATE_CLOSED
:
3961 case NVME_ZONE_STATE_FULL
:
3969 trace_pci_nvme_zns_zone_reset(zone
->d
.zslba
);
3971 iocb
->aiocb
= blk_aio_pwrite_zeroes(ns
->blkconf
.blk
,
3972 nvme_l2b(ns
, zone
->d
.zslba
),
3973 nvme_l2b(ns
, ns
->zone_size
),
3975 nvme_zone_reset_epilogue_cb
,
3983 iocb
->common
.cb(iocb
->common
.opaque
, iocb
->ret
);
3984 qemu_aio_unref(iocb
);
3987 static uint16_t nvme_zone_mgmt_send_zrwa_flush(NvmeCtrl
*n
, NvmeZone
*zone
,
3988 uint64_t elba
, NvmeRequest
*req
)
3990 NvmeNamespace
*ns
= req
->ns
;
3991 uint16_t ozcs
= le16_to_cpu(ns
->id_ns_zoned
->ozcs
);
3992 uint64_t wp
= zone
->d
.wp
;
3993 uint32_t nlb
= elba
- wp
+ 1;
3997 if (!(ozcs
& NVME_ID_NS_ZONED_OZCS_ZRWASUP
)) {
3998 return NVME_INVALID_ZONE_OP
| NVME_DNR
;
4001 if (!(zone
->d
.za
& NVME_ZA_ZRWA_VALID
)) {
4002 return NVME_INVALID_FIELD
| NVME_DNR
;
4005 if (elba
< wp
|| elba
> wp
+ ns
->zns
.zrwas
) {
4006 return NVME_ZONE_BOUNDARY_ERROR
| NVME_DNR
;
4009 if (nlb
% ns
->zns
.zrwafg
) {
4010 return NVME_INVALID_FIELD
| NVME_DNR
;
4013 status
= nvme_zrm_auto(n
, ns
, zone
);
4020 nvme_advance_zone_wp(ns
, zone
, nlb
);
4022 return NVME_SUCCESS
;
4025 static uint16_t nvme_zone_mgmt_send(NvmeCtrl
*n
, NvmeRequest
*req
)
4027 NvmeZoneSendCmd
*cmd
= (NvmeZoneSendCmd
*)&req
->cmd
;
4028 NvmeNamespace
*ns
= req
->ns
;
4030 NvmeZoneResetAIOCB
*iocb
;
4033 uint32_t zone_idx
= 0;
4035 uint8_t action
= cmd
->zsa
;
4037 enum NvmeZoneProcessingMask proc_mask
= NVME_PROC_CURRENT_ZONE
;
4039 all
= cmd
->zsflags
& NVME_ZSFLAG_SELECT_ALL
;
4041 req
->status
= NVME_SUCCESS
;
4044 status
= nvme_get_mgmt_zone_slba_idx(ns
, &req
->cmd
, &slba
, &zone_idx
);
4050 zone
= &ns
->zone_array
[zone_idx
];
4051 if (slba
!= zone
->d
.zslba
&& action
!= NVME_ZONE_ACTION_ZRWA_FLUSH
) {
4052 trace_pci_nvme_err_unaligned_zone_cmd(action
, slba
, zone
->d
.zslba
);
4053 return NVME_INVALID_FIELD
| NVME_DNR
;
4058 case NVME_ZONE_ACTION_OPEN
:
4060 proc_mask
= NVME_PROC_CLOSED_ZONES
;
4062 trace_pci_nvme_open_zone(slba
, zone_idx
, all
);
4063 status
= nvme_do_zone_op(ns
, zone
, proc_mask
, nvme_open_zone
, req
);
4066 case NVME_ZONE_ACTION_CLOSE
:
4068 proc_mask
= NVME_PROC_OPENED_ZONES
;
4070 trace_pci_nvme_close_zone(slba
, zone_idx
, all
);
4071 status
= nvme_do_zone_op(ns
, zone
, proc_mask
, nvme_close_zone
, req
);
4074 case NVME_ZONE_ACTION_FINISH
:
4076 proc_mask
= NVME_PROC_OPENED_ZONES
| NVME_PROC_CLOSED_ZONES
;
4078 trace_pci_nvme_finish_zone(slba
, zone_idx
, all
);
4079 status
= nvme_do_zone_op(ns
, zone
, proc_mask
, nvme_finish_zone
, req
);
4082 case NVME_ZONE_ACTION_RESET
:
4083 trace_pci_nvme_reset_zone(slba
, zone_idx
, all
);
4085 iocb
= blk_aio_get(&nvme_zone_reset_aiocb_info
, ns
->blkconf
.blk
,
4091 iocb
->idx
= zone_idx
;
4094 req
->aiocb
= &iocb
->common
;
4095 nvme_zone_reset_cb(iocb
, 0);
4097 return NVME_NO_COMPLETE
;
4099 case NVME_ZONE_ACTION_OFFLINE
:
4101 proc_mask
= NVME_PROC_READ_ONLY_ZONES
;
4103 trace_pci_nvme_offline_zone(slba
, zone_idx
, all
);
4104 status
= nvme_do_zone_op(ns
, zone
, proc_mask
, nvme_offline_zone
, req
);
4107 case NVME_ZONE_ACTION_SET_ZD_EXT
:
4108 trace_pci_nvme_set_descriptor_extension(slba
, zone_idx
);
4109 if (all
|| !ns
->params
.zd_extension_size
) {
4110 return NVME_INVALID_FIELD
| NVME_DNR
;
4112 zd_ext
= nvme_get_zd_extension(ns
, zone_idx
);
4113 status
= nvme_h2c(n
, zd_ext
, ns
->params
.zd_extension_size
, req
);
4115 trace_pci_nvme_err_zd_extension_map_error(zone_idx
);
4119 status
= nvme_set_zd_ext(ns
, zone
);
4120 if (status
== NVME_SUCCESS
) {
4121 trace_pci_nvme_zd_extension_set(zone_idx
);
4126 case NVME_ZONE_ACTION_ZRWA_FLUSH
:
4128 return NVME_INVALID_FIELD
| NVME_DNR
;
4131 return nvme_zone_mgmt_send_zrwa_flush(n
, zone
, slba
, req
);
4134 trace_pci_nvme_err_invalid_mgmt_action(action
);
4135 status
= NVME_INVALID_FIELD
;
4138 if (status
== NVME_ZONE_INVAL_TRANSITION
) {
4139 trace_pci_nvme_err_invalid_zone_state_transition(action
, slba
,
4149 static bool nvme_zone_matches_filter(uint32_t zafs
, NvmeZone
*zl
)
4151 NvmeZoneState zs
= nvme_get_zone_state(zl
);
4154 case NVME_ZONE_REPORT_ALL
:
4156 case NVME_ZONE_REPORT_EMPTY
:
4157 return zs
== NVME_ZONE_STATE_EMPTY
;
4158 case NVME_ZONE_REPORT_IMPLICITLY_OPEN
:
4159 return zs
== NVME_ZONE_STATE_IMPLICITLY_OPEN
;
4160 case NVME_ZONE_REPORT_EXPLICITLY_OPEN
:
4161 return zs
== NVME_ZONE_STATE_EXPLICITLY_OPEN
;
4162 case NVME_ZONE_REPORT_CLOSED
:
4163 return zs
== NVME_ZONE_STATE_CLOSED
;
4164 case NVME_ZONE_REPORT_FULL
:
4165 return zs
== NVME_ZONE_STATE_FULL
;
4166 case NVME_ZONE_REPORT_READ_ONLY
:
4167 return zs
== NVME_ZONE_STATE_READ_ONLY
;
4168 case NVME_ZONE_REPORT_OFFLINE
:
4169 return zs
== NVME_ZONE_STATE_OFFLINE
;
4175 static uint16_t nvme_zone_mgmt_recv(NvmeCtrl
*n
, NvmeRequest
*req
)
4177 NvmeCmd
*cmd
= (NvmeCmd
*)&req
->cmd
;
4178 NvmeNamespace
*ns
= req
->ns
;
4179 /* cdw12 is zero-based number of dwords to return. Convert to bytes */
4180 uint32_t data_size
= (le32_to_cpu(cmd
->cdw12
) + 1) << 2;
4181 uint32_t dw13
= le32_to_cpu(cmd
->cdw13
);
4182 uint32_t zone_idx
, zra
, zrasf
, partial
;
4183 uint64_t max_zones
, nr_zones
= 0;
4188 NvmeZoneReportHeader
*header
;
4190 size_t zone_entry_sz
;
4193 req
->status
= NVME_SUCCESS
;
4195 status
= nvme_get_mgmt_zone_slba_idx(ns
, cmd
, &slba
, &zone_idx
);
4201 if (zra
!= NVME_ZONE_REPORT
&& zra
!= NVME_ZONE_REPORT_EXTENDED
) {
4202 return NVME_INVALID_FIELD
| NVME_DNR
;
4204 if (zra
== NVME_ZONE_REPORT_EXTENDED
&& !ns
->params
.zd_extension_size
) {
4205 return NVME_INVALID_FIELD
| NVME_DNR
;
4208 zrasf
= (dw13
>> 8) & 0xff;
4209 if (zrasf
> NVME_ZONE_REPORT_OFFLINE
) {
4210 return NVME_INVALID_FIELD
| NVME_DNR
;
4213 if (data_size
< sizeof(NvmeZoneReportHeader
)) {
4214 return NVME_INVALID_FIELD
| NVME_DNR
;
4217 status
= nvme_check_mdts(n
, data_size
);
4222 partial
= (dw13
>> 16) & 0x01;
4224 zone_entry_sz
= sizeof(NvmeZoneDescr
);
4225 if (zra
== NVME_ZONE_REPORT_EXTENDED
) {
4226 zone_entry_sz
+= ns
->params
.zd_extension_size
;
4229 max_zones
= (data_size
- sizeof(NvmeZoneReportHeader
)) / zone_entry_sz
;
4230 buf
= g_malloc0(data_size
);
4232 zone
= &ns
->zone_array
[zone_idx
];
4233 for (i
= zone_idx
; i
< ns
->num_zones
; i
++) {
4234 if (partial
&& nr_zones
>= max_zones
) {
4237 if (nvme_zone_matches_filter(zrasf
, zone
++)) {
4242 header
->nr_zones
= cpu_to_le64(nr_zones
);
4244 buf_p
= buf
+ sizeof(NvmeZoneReportHeader
);
4245 for (; zone_idx
< ns
->num_zones
&& max_zones
> 0; zone_idx
++) {
4246 zone
= &ns
->zone_array
[zone_idx
];
4247 if (nvme_zone_matches_filter(zrasf
, zone
)) {
4249 buf_p
+= sizeof(NvmeZoneDescr
);
4253 z
->zcap
= cpu_to_le64(zone
->d
.zcap
);
4254 z
->zslba
= cpu_to_le64(zone
->d
.zslba
);
4257 if (nvme_wp_is_valid(zone
)) {
4258 z
->wp
= cpu_to_le64(zone
->d
.wp
);
4260 z
->wp
= cpu_to_le64(~0ULL);
4263 if (zra
== NVME_ZONE_REPORT_EXTENDED
) {
4264 if (zone
->d
.za
& NVME_ZA_ZD_EXT_VALID
) {
4265 memcpy(buf_p
, nvme_get_zd_extension(ns
, zone_idx
),
4266 ns
->params
.zd_extension_size
);
4268 buf_p
+= ns
->params
.zd_extension_size
;
4275 status
= nvme_c2h(n
, (uint8_t *)buf
, data_size
, req
);
4282 static uint16_t nvme_io_mgmt_recv_ruhs(NvmeCtrl
*n
, NvmeRequest
*req
,
4285 NvmeNamespace
*ns
= req
->ns
;
4286 NvmeEnduranceGroup
*endgrp
;
4288 NvmeRuhStatusDescr
*ruhsd
;
4289 unsigned int nruhsd
;
4290 uint16_t rg
, ph
, *ruhid
;
4292 g_autofree
uint8_t *buf
= NULL
;
4295 return NVME_INVALID_FIELD
| NVME_DNR
;
4298 if (ns
->params
.nsid
== 0 || ns
->params
.nsid
== 0xffffffff) {
4299 return NVME_INVALID_NSID
| NVME_DNR
;
4302 if (!n
->subsys
->endgrp
.fdp
.enabled
) {
4303 return NVME_FDP_DISABLED
| NVME_DNR
;
4306 endgrp
= ns
->endgrp
;
4308 nruhsd
= ns
->fdp
.nphs
* endgrp
->fdp
.nrg
;
4309 trans_len
= sizeof(NvmeRuhStatus
) + nruhsd
* sizeof(NvmeRuhStatusDescr
);
4310 buf
= g_malloc(trans_len
);
4312 trans_len
= MIN(trans_len
, len
);
4314 hdr
= (NvmeRuhStatus
*)buf
;
4315 ruhsd
= (NvmeRuhStatusDescr
*)(buf
+ sizeof(NvmeRuhStatus
));
4317 hdr
->nruhsd
= cpu_to_le16(nruhsd
);
4319 ruhid
= ns
->fdp
.phs
;
4321 for (ph
= 0; ph
< ns
->fdp
.nphs
; ph
++, ruhid
++) {
4322 NvmeRuHandle
*ruh
= &endgrp
->fdp
.ruhs
[*ruhid
];
4324 for (rg
= 0; rg
< endgrp
->fdp
.nrg
; rg
++, ruhsd
++) {
4325 uint16_t pid
= nvme_make_pid(ns
, rg
, ph
);
4327 ruhsd
->pid
= cpu_to_le16(pid
);
4328 ruhsd
->ruhid
= *ruhid
;
4330 ruhsd
->ruamw
= cpu_to_le64(ruh
->rus
[rg
].ruamw
);
4334 return nvme_c2h(n
, buf
, trans_len
, req
);
4337 static uint16_t nvme_io_mgmt_recv(NvmeCtrl
*n
, NvmeRequest
*req
)
4339 NvmeCmd
*cmd
= &req
->cmd
;
4340 uint32_t cdw10
= le32_to_cpu(cmd
->cdw10
);
4341 uint32_t numd
= le32_to_cpu(cmd
->cdw11
);
4342 uint8_t mo
= (cdw10
& 0xff);
4343 size_t len
= (numd
+ 1) << 2;
4346 case NVME_IOMR_MO_NOP
:
4348 case NVME_IOMR_MO_RUH_STATUS
:
4349 return nvme_io_mgmt_recv_ruhs(n
, req
, len
);
4351 return NVME_INVALID_FIELD
| NVME_DNR
;
4355 static uint16_t nvme_io_mgmt_send_ruh_update(NvmeCtrl
*n
, NvmeRequest
*req
)
4357 NvmeCmd
*cmd
= &req
->cmd
;
4358 NvmeNamespace
*ns
= req
->ns
;
4359 uint32_t cdw10
= le32_to_cpu(cmd
->cdw10
);
4360 uint16_t ret
= NVME_SUCCESS
;
4361 uint32_t npid
= (cdw10
>> 1) + 1;
4363 g_autofree
uint16_t *pids
= NULL
;
4366 if (!ns
->endgrp
|| !ns
->endgrp
->fdp
.enabled
) {
4367 return NVME_FDP_DISABLED
| NVME_DNR
;
4370 maxnpid
= n
->subsys
->endgrp
.fdp
.nrg
* n
->subsys
->endgrp
.fdp
.nruh
;
4372 if (unlikely(npid
>= MIN(NVME_FDP_MAXPIDS
, maxnpid
))) {
4373 return NVME_INVALID_FIELD
| NVME_DNR
;
4376 pids
= g_new(uint16_t, npid
);
4378 ret
= nvme_h2c(n
, pids
, npid
* sizeof(uint16_t), req
);
4383 for (; i
< npid
; i
++) {
4384 if (!nvme_update_ruh(n
, ns
, pids
[i
])) {
4385 return NVME_INVALID_FIELD
| NVME_DNR
;
4392 static uint16_t nvme_io_mgmt_send(NvmeCtrl
*n
, NvmeRequest
*req
)
4394 NvmeCmd
*cmd
= &req
->cmd
;
4395 uint32_t cdw10
= le32_to_cpu(cmd
->cdw10
);
4396 uint8_t mo
= (cdw10
& 0xff);
4399 case NVME_IOMS_MO_NOP
:
4401 case NVME_IOMS_MO_RUH_UPDATE
:
4402 return nvme_io_mgmt_send_ruh_update(n
, req
);
4404 return NVME_INVALID_FIELD
| NVME_DNR
;
4408 static uint16_t nvme_io_cmd(NvmeCtrl
*n
, NvmeRequest
*req
)
4411 uint32_t nsid
= le32_to_cpu(req
->cmd
.nsid
);
4413 trace_pci_nvme_io_cmd(nvme_cid(req
), nsid
, nvme_sqid(req
),
4414 req
->cmd
.opcode
, nvme_io_opc_str(req
->cmd
.opcode
));
4416 if (!nvme_nsid_valid(n
, nsid
)) {
4417 return NVME_INVALID_NSID
| NVME_DNR
;
4421 * In the base NVM command set, Flush may apply to all namespaces
4422 * (indicated by NSID being set to FFFFFFFFh). But if that feature is used
4423 * along with TP 4056 (Namespace Types), it may be pretty screwed up.
4425 * If NSID is indeed set to FFFFFFFFh, we simply cannot associate the
4426 * opcode with a specific command since we cannot determine a unique I/O
4427 * command set. Opcode 0h could have any other meaning than something
4428 * equivalent to flushing and say it DOES have completely different
4429 * semantics in some other command set - does an NSID of FFFFFFFFh then
4430 * mean "for all namespaces, apply whatever command set specific command
4431 * that uses the 0h opcode?" Or does it mean "for all namespaces, apply
4432 * whatever command that uses the 0h opcode if, and only if, it allows NSID
4435 * Anyway (and luckily), for now, we do not care about this since the
4436 * device only supports namespace types that includes the NVM Flush command
4437 * (NVM and Zoned), so always do an NVM Flush.
4439 if (req
->cmd
.opcode
== NVME_CMD_FLUSH
) {
4440 return nvme_flush(n
, req
);
4443 ns
= nvme_ns(n
, nsid
);
4444 if (unlikely(!ns
)) {
4445 return NVME_INVALID_FIELD
| NVME_DNR
;
4448 if (!(ns
->iocs
[req
->cmd
.opcode
] & NVME_CMD_EFF_CSUPP
)) {
4449 trace_pci_nvme_err_invalid_opc(req
->cmd
.opcode
);
4450 return NVME_INVALID_OPCODE
| NVME_DNR
;
4457 if (NVME_CMD_FLAGS_FUSE(req
->cmd
.flags
)) {
4458 return NVME_INVALID_FIELD
;
4463 switch (req
->cmd
.opcode
) {
4464 case NVME_CMD_WRITE_ZEROES
:
4465 return nvme_write_zeroes(n
, req
);
4466 case NVME_CMD_ZONE_APPEND
:
4467 return nvme_zone_append(n
, req
);
4468 case NVME_CMD_WRITE
:
4469 return nvme_write(n
, req
);
4471 return nvme_read(n
, req
);
4472 case NVME_CMD_COMPARE
:
4473 return nvme_compare(n
, req
);
4475 return nvme_dsm(n
, req
);
4476 case NVME_CMD_VERIFY
:
4477 return nvme_verify(n
, req
);
4479 return nvme_copy(n
, req
);
4480 case NVME_CMD_ZONE_MGMT_SEND
:
4481 return nvme_zone_mgmt_send(n
, req
);
4482 case NVME_CMD_ZONE_MGMT_RECV
:
4483 return nvme_zone_mgmt_recv(n
, req
);
4484 case NVME_CMD_IO_MGMT_RECV
:
4485 return nvme_io_mgmt_recv(n
, req
);
4486 case NVME_CMD_IO_MGMT_SEND
:
4487 return nvme_io_mgmt_send(n
, req
);
4492 return NVME_INVALID_OPCODE
| NVME_DNR
;
4495 static void nvme_cq_notifier(EventNotifier
*e
)
4497 NvmeCQueue
*cq
= container_of(e
, NvmeCQueue
, notifier
);
4498 NvmeCtrl
*n
= cq
->ctrl
;
4500 if (!event_notifier_test_and_clear(e
)) {
4504 nvme_update_cq_head(cq
);
4506 if (cq
->tail
== cq
->head
) {
4507 if (cq
->irq_enabled
) {
4511 nvme_irq_deassert(n
, cq
);
4514 qemu_bh_schedule(cq
->bh
);
4517 static int nvme_init_cq_ioeventfd(NvmeCQueue
*cq
)
4519 NvmeCtrl
*n
= cq
->ctrl
;
4520 uint16_t offset
= (cq
->cqid
<< 3) + (1 << 2);
4523 ret
= event_notifier_init(&cq
->notifier
, 0);
4528 event_notifier_set_handler(&cq
->notifier
, nvme_cq_notifier
);
4529 memory_region_add_eventfd(&n
->iomem
,
4530 0x1000 + offset
, 4, false, 0, &cq
->notifier
);
4535 static void nvme_sq_notifier(EventNotifier
*e
)
4537 NvmeSQueue
*sq
= container_of(e
, NvmeSQueue
, notifier
);
4539 if (!event_notifier_test_and_clear(e
)) {
4543 nvme_process_sq(sq
);
4546 static int nvme_init_sq_ioeventfd(NvmeSQueue
*sq
)
4548 NvmeCtrl
*n
= sq
->ctrl
;
4549 uint16_t offset
= sq
->sqid
<< 3;
4552 ret
= event_notifier_init(&sq
->notifier
, 0);
4557 event_notifier_set_handler(&sq
->notifier
, nvme_sq_notifier
);
4558 memory_region_add_eventfd(&n
->iomem
,
4559 0x1000 + offset
, 4, false, 0, &sq
->notifier
);
4564 static void nvme_free_sq(NvmeSQueue
*sq
, NvmeCtrl
*n
)
4566 uint16_t offset
= sq
->sqid
<< 3;
4568 n
->sq
[sq
->sqid
] = NULL
;
4569 qemu_bh_delete(sq
->bh
);
4570 if (sq
->ioeventfd_enabled
) {
4571 memory_region_del_eventfd(&n
->iomem
,
4572 0x1000 + offset
, 4, false, 0, &sq
->notifier
);
4573 event_notifier_set_handler(&sq
->notifier
, NULL
);
4574 event_notifier_cleanup(&sq
->notifier
);
4582 static uint16_t nvme_del_sq(NvmeCtrl
*n
, NvmeRequest
*req
)
4584 NvmeDeleteQ
*c
= (NvmeDeleteQ
*)&req
->cmd
;
4585 NvmeRequest
*r
, *next
;
4588 uint16_t qid
= le16_to_cpu(c
->qid
);
4590 if (unlikely(!qid
|| nvme_check_sqid(n
, qid
))) {
4591 trace_pci_nvme_err_invalid_del_sq(qid
);
4592 return NVME_INVALID_QID
| NVME_DNR
;
4595 trace_pci_nvme_del_sq(qid
);
4598 while (!QTAILQ_EMPTY(&sq
->out_req_list
)) {
4599 r
= QTAILQ_FIRST(&sq
->out_req_list
);
4601 blk_aio_cancel(r
->aiocb
);
4604 assert(QTAILQ_EMPTY(&sq
->out_req_list
));
4606 if (!nvme_check_cqid(n
, sq
->cqid
)) {
4607 cq
= n
->cq
[sq
->cqid
];
4608 QTAILQ_REMOVE(&cq
->sq_list
, sq
, entry
);
4611 QTAILQ_FOREACH_SAFE(r
, &cq
->req_list
, entry
, next
) {
4613 QTAILQ_REMOVE(&cq
->req_list
, r
, entry
);
4614 QTAILQ_INSERT_TAIL(&sq
->req_list
, r
, entry
);
4619 nvme_free_sq(sq
, n
);
4620 return NVME_SUCCESS
;
4623 static void nvme_init_sq(NvmeSQueue
*sq
, NvmeCtrl
*n
, uint64_t dma_addr
,
4624 uint16_t sqid
, uint16_t cqid
, uint16_t size
)
4630 sq
->dma_addr
= dma_addr
;
4634 sq
->head
= sq
->tail
= 0;
4635 sq
->io_req
= g_new0(NvmeRequest
, sq
->size
);
4637 QTAILQ_INIT(&sq
->req_list
);
4638 QTAILQ_INIT(&sq
->out_req_list
);
4639 for (i
= 0; i
< sq
->size
; i
++) {
4640 sq
->io_req
[i
].sq
= sq
;
4641 QTAILQ_INSERT_TAIL(&(sq
->req_list
), &sq
->io_req
[i
], entry
);
4644 sq
->bh
= qemu_bh_new_guarded(nvme_process_sq
, sq
,
4645 &DEVICE(sq
->ctrl
)->mem_reentrancy_guard
);
4647 if (n
->dbbuf_enabled
) {
4648 sq
->db_addr
= n
->dbbuf_dbs
+ (sqid
<< 3);
4649 sq
->ei_addr
= n
->dbbuf_eis
+ (sqid
<< 3);
4651 if (n
->params
.ioeventfd
&& sq
->sqid
!= 0) {
4652 if (!nvme_init_sq_ioeventfd(sq
)) {
4653 sq
->ioeventfd_enabled
= true;
4658 assert(n
->cq
[cqid
]);
4660 QTAILQ_INSERT_TAIL(&(cq
->sq_list
), sq
, entry
);
4664 static uint16_t nvme_create_sq(NvmeCtrl
*n
, NvmeRequest
*req
)
4667 NvmeCreateSq
*c
= (NvmeCreateSq
*)&req
->cmd
;
4669 uint16_t cqid
= le16_to_cpu(c
->cqid
);
4670 uint16_t sqid
= le16_to_cpu(c
->sqid
);
4671 uint16_t qsize
= le16_to_cpu(c
->qsize
);
4672 uint16_t qflags
= le16_to_cpu(c
->sq_flags
);
4673 uint64_t prp1
= le64_to_cpu(c
->prp1
);
4675 trace_pci_nvme_create_sq(prp1
, sqid
, cqid
, qsize
, qflags
);
4677 if (unlikely(!cqid
|| nvme_check_cqid(n
, cqid
))) {
4678 trace_pci_nvme_err_invalid_create_sq_cqid(cqid
);
4679 return NVME_INVALID_CQID
| NVME_DNR
;
4681 if (unlikely(!sqid
|| sqid
> n
->conf_ioqpairs
|| n
->sq
[sqid
] != NULL
)) {
4682 trace_pci_nvme_err_invalid_create_sq_sqid(sqid
);
4683 return NVME_INVALID_QID
| NVME_DNR
;
4685 if (unlikely(!qsize
|| qsize
> NVME_CAP_MQES(ldq_le_p(&n
->bar
.cap
)))) {
4686 trace_pci_nvme_err_invalid_create_sq_size(qsize
);
4687 return NVME_MAX_QSIZE_EXCEEDED
| NVME_DNR
;
4689 if (unlikely(prp1
& (n
->page_size
- 1))) {
4690 trace_pci_nvme_err_invalid_create_sq_addr(prp1
);
4691 return NVME_INVALID_PRP_OFFSET
| NVME_DNR
;
4693 if (unlikely(!(NVME_SQ_FLAGS_PC(qflags
)))) {
4694 trace_pci_nvme_err_invalid_create_sq_qflags(NVME_SQ_FLAGS_PC(qflags
));
4695 return NVME_INVALID_FIELD
| NVME_DNR
;
4697 sq
= g_malloc0(sizeof(*sq
));
4698 nvme_init_sq(sq
, n
, prp1
, sqid
, cqid
, qsize
+ 1);
4699 return NVME_SUCCESS
;
4703 uint64_t units_read
;
4704 uint64_t units_written
;
4705 uint64_t read_commands
;
4706 uint64_t write_commands
;
4709 static void nvme_set_blk_stats(NvmeNamespace
*ns
, struct nvme_stats
*stats
)
4711 BlockAcctStats
*s
= blk_get_stats(ns
->blkconf
.blk
);
4713 stats
->units_read
+= s
->nr_bytes
[BLOCK_ACCT_READ
];
4714 stats
->units_written
+= s
->nr_bytes
[BLOCK_ACCT_WRITE
];
4715 stats
->read_commands
+= s
->nr_ops
[BLOCK_ACCT_READ
];
4716 stats
->write_commands
+= s
->nr_ops
[BLOCK_ACCT_WRITE
];
4719 static uint16_t nvme_smart_info(NvmeCtrl
*n
, uint8_t rae
, uint32_t buf_len
,
4720 uint64_t off
, NvmeRequest
*req
)
4722 uint32_t nsid
= le32_to_cpu(req
->cmd
.nsid
);
4723 struct nvme_stats stats
= { 0 };
4724 NvmeSmartLog smart
= { 0 };
4728 uint64_t u_read
, u_written
;
4730 if (off
>= sizeof(smart
)) {
4731 return NVME_INVALID_FIELD
| NVME_DNR
;
4734 if (nsid
!= 0xffffffff) {
4735 ns
= nvme_ns(n
, nsid
);
4737 return NVME_INVALID_NSID
| NVME_DNR
;
4739 nvme_set_blk_stats(ns
, &stats
);
4743 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
4748 nvme_set_blk_stats(ns
, &stats
);
4752 trans_len
= MIN(sizeof(smart
) - off
, buf_len
);
4753 smart
.critical_warning
= n
->smart_critical_warning
;
4755 u_read
= DIV_ROUND_UP(stats
.units_read
>> BDRV_SECTOR_BITS
, 1000);
4756 u_written
= DIV_ROUND_UP(stats
.units_written
>> BDRV_SECTOR_BITS
, 1000);
4758 smart
.data_units_read
[0] = cpu_to_le64(u_read
);
4759 smart
.data_units_written
[0] = cpu_to_le64(u_written
);
4760 smart
.host_read_commands
[0] = cpu_to_le64(stats
.read_commands
);
4761 smart
.host_write_commands
[0] = cpu_to_le64(stats
.write_commands
);
4763 smart
.temperature
= cpu_to_le16(n
->temperature
);
4765 if ((n
->temperature
>= n
->features
.temp_thresh_hi
) ||
4766 (n
->temperature
<= n
->features
.temp_thresh_low
)) {
4767 smart
.critical_warning
|= NVME_SMART_TEMPERATURE
;
4770 current_ms
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
);
4771 smart
.power_on_hours
[0] =
4772 cpu_to_le64((((current_ms
- n
->starttime_ms
) / 1000) / 60) / 60);
4775 nvme_clear_events(n
, NVME_AER_TYPE_SMART
);
4778 return nvme_c2h(n
, (uint8_t *) &smart
+ off
, trans_len
, req
);
4781 static uint16_t nvme_endgrp_info(NvmeCtrl
*n
, uint8_t rae
, uint32_t buf_len
,
4782 uint64_t off
, NvmeRequest
*req
)
4784 uint32_t dw11
= le32_to_cpu(req
->cmd
.cdw11
);
4785 uint16_t endgrpid
= (dw11
>> 16) & 0xffff;
4786 struct nvme_stats stats
= {};
4787 NvmeEndGrpLog info
= {};
4790 if (!n
->subsys
|| endgrpid
!= 0x1) {
4791 return NVME_INVALID_FIELD
| NVME_DNR
;
4794 if (off
>= sizeof(info
)) {
4795 return NVME_INVALID_FIELD
| NVME_DNR
;
4798 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
4799 NvmeNamespace
*ns
= nvme_subsys_ns(n
->subsys
, i
);
4804 nvme_set_blk_stats(ns
, &stats
);
4807 info
.data_units_read
[0] =
4808 cpu_to_le64(DIV_ROUND_UP(stats
.units_read
/ 1000000000, 1000000000));
4809 info
.data_units_written
[0] =
4810 cpu_to_le64(DIV_ROUND_UP(stats
.units_written
/ 1000000000, 1000000000));
4811 info
.media_units_written
[0] =
4812 cpu_to_le64(DIV_ROUND_UP(stats
.units_written
/ 1000000000, 1000000000));
4814 info
.host_read_commands
[0] = cpu_to_le64(stats
.read_commands
);
4815 info
.host_write_commands
[0] = cpu_to_le64(stats
.write_commands
);
4817 buf_len
= MIN(sizeof(info
) - off
, buf_len
);
4819 return nvme_c2h(n
, (uint8_t *)&info
+ off
, buf_len
, req
);
4823 static uint16_t nvme_fw_log_info(NvmeCtrl
*n
, uint32_t buf_len
, uint64_t off
,
4827 NvmeFwSlotInfoLog fw_log
= {
4831 if (off
>= sizeof(fw_log
)) {
4832 return NVME_INVALID_FIELD
| NVME_DNR
;
4835 strpadcpy((char *)&fw_log
.frs1
, sizeof(fw_log
.frs1
), "1.0", ' ');
4836 trans_len
= MIN(sizeof(fw_log
) - off
, buf_len
);
4838 return nvme_c2h(n
, (uint8_t *) &fw_log
+ off
, trans_len
, req
);
4841 static uint16_t nvme_error_info(NvmeCtrl
*n
, uint8_t rae
, uint32_t buf_len
,
4842 uint64_t off
, NvmeRequest
*req
)
4845 NvmeErrorLog errlog
;
4847 if (off
>= sizeof(errlog
)) {
4848 return NVME_INVALID_FIELD
| NVME_DNR
;
4852 nvme_clear_events(n
, NVME_AER_TYPE_ERROR
);
4855 memset(&errlog
, 0x0, sizeof(errlog
));
4856 trans_len
= MIN(sizeof(errlog
) - off
, buf_len
);
4858 return nvme_c2h(n
, (uint8_t *)&errlog
, trans_len
, req
);
4861 static uint16_t nvme_changed_nslist(NvmeCtrl
*n
, uint8_t rae
, uint32_t buf_len
,
4862 uint64_t off
, NvmeRequest
*req
)
4864 uint32_t nslist
[1024];
4869 if (off
>= sizeof(nslist
)) {
4870 trace_pci_nvme_err_invalid_log_page_offset(off
, sizeof(nslist
));
4871 return NVME_INVALID_FIELD
| NVME_DNR
;
4874 memset(nslist
, 0x0, sizeof(nslist
));
4875 trans_len
= MIN(sizeof(nslist
) - off
, buf_len
);
4877 while ((nsid
= find_first_bit(n
->changed_nsids
, NVME_CHANGED_NSID_SIZE
)) !=
4878 NVME_CHANGED_NSID_SIZE
) {
4880 * If more than 1024 namespaces, the first entry in the log page should
4881 * be set to FFFFFFFFh and the others to 0 as spec.
4883 if (i
== ARRAY_SIZE(nslist
)) {
4884 memset(nslist
, 0x0, sizeof(nslist
));
4885 nslist
[0] = 0xffffffff;
4890 clear_bit(nsid
, n
->changed_nsids
);
4894 * Remove all the remaining list entries in case returns directly due to
4895 * more than 1024 namespaces.
4897 if (nslist
[0] == 0xffffffff) {
4898 bitmap_zero(n
->changed_nsids
, NVME_CHANGED_NSID_SIZE
);
4902 nvme_clear_events(n
, NVME_AER_TYPE_NOTICE
);
4905 return nvme_c2h(n
, ((uint8_t *)nslist
) + off
, trans_len
, req
);
4908 static uint16_t nvme_cmd_effects(NvmeCtrl
*n
, uint8_t csi
, uint32_t buf_len
,
4909 uint64_t off
, NvmeRequest
*req
)
4911 NvmeEffectsLog log
= {};
4912 const uint32_t *src_iocs
= NULL
;
4915 if (off
>= sizeof(log
)) {
4916 trace_pci_nvme_err_invalid_log_page_offset(off
, sizeof(log
));
4917 return NVME_INVALID_FIELD
| NVME_DNR
;
4920 switch (NVME_CC_CSS(ldl_le_p(&n
->bar
.cc
))) {
4921 case NVME_CC_CSS_NVM
:
4922 src_iocs
= nvme_cse_iocs_nvm
;
4924 case NVME_CC_CSS_ADMIN_ONLY
:
4926 case NVME_CC_CSS_CSI
:
4929 src_iocs
= nvme_cse_iocs_nvm
;
4931 case NVME_CSI_ZONED
:
4932 src_iocs
= nvme_cse_iocs_zoned
;
4937 memcpy(log
.acs
, nvme_cse_acs
, sizeof(nvme_cse_acs
));
4940 memcpy(log
.iocs
, src_iocs
, sizeof(log
.iocs
));
4943 trans_len
= MIN(sizeof(log
) - off
, buf_len
);
4945 return nvme_c2h(n
, ((uint8_t *)&log
) + off
, trans_len
, req
);
4948 static size_t sizeof_fdp_conf_descr(size_t nruh
, size_t vss
)
4950 size_t entry_siz
= sizeof(NvmeFdpDescrHdr
) + nruh
* sizeof(NvmeRuhDescr
)
4952 return ROUND_UP(entry_siz
, 8);
4955 static uint16_t nvme_fdp_confs(NvmeCtrl
*n
, uint32_t endgrpid
, uint32_t buf_len
,
4956 uint64_t off
, NvmeRequest
*req
)
4958 uint32_t log_size
, trans_len
;
4959 g_autofree
uint8_t *buf
= NULL
;
4960 NvmeFdpDescrHdr
*hdr
;
4962 NvmeEnduranceGroup
*endgrp
;
4963 NvmeFdpConfsHdr
*log
;
4964 size_t nruh
, fdp_descr_size
;
4967 if (endgrpid
!= 1 || !n
->subsys
) {
4968 return NVME_INVALID_FIELD
| NVME_DNR
;
4971 endgrp
= &n
->subsys
->endgrp
;
4973 if (endgrp
->fdp
.enabled
) {
4974 nruh
= endgrp
->fdp
.nruh
;
4979 fdp_descr_size
= sizeof_fdp_conf_descr(nruh
, FDPVSS
);
4980 log_size
= sizeof(NvmeFdpConfsHdr
) + fdp_descr_size
;
4982 if (off
>= log_size
) {
4983 return NVME_INVALID_FIELD
| NVME_DNR
;
4986 trans_len
= MIN(log_size
- off
, buf_len
);
4988 buf
= g_malloc0(log_size
);
4989 log
= (NvmeFdpConfsHdr
*)buf
;
4990 hdr
= (NvmeFdpDescrHdr
*)(log
+ 1);
4991 ruhd
= (NvmeRuhDescr
*)(buf
+ sizeof(*log
) + sizeof(*hdr
));
4993 log
->num_confs
= cpu_to_le16(0);
4994 log
->size
= cpu_to_le32(log_size
);
4996 hdr
->descr_size
= cpu_to_le16(fdp_descr_size
);
4997 if (endgrp
->fdp
.enabled
) {
4998 hdr
->fdpa
= FIELD_DP8(hdr
->fdpa
, FDPA
, VALID
, 1);
4999 hdr
->fdpa
= FIELD_DP8(hdr
->fdpa
, FDPA
, RGIF
, endgrp
->fdp
.rgif
);
5000 hdr
->nrg
= cpu_to_le16(endgrp
->fdp
.nrg
);
5001 hdr
->nruh
= cpu_to_le16(endgrp
->fdp
.nruh
);
5002 hdr
->maxpids
= cpu_to_le16(NVME_FDP_MAXPIDS
- 1);
5003 hdr
->nnss
= cpu_to_le32(NVME_MAX_NAMESPACES
);
5004 hdr
->runs
= cpu_to_le64(endgrp
->fdp
.runs
);
5006 for (i
= 0; i
< nruh
; i
++) {
5007 ruhd
->ruht
= NVME_RUHT_INITIALLY_ISOLATED
;
5011 /* 1 bit for RUH in PIF -> 2 RUHs max. */
5012 hdr
->nrg
= cpu_to_le16(1);
5013 hdr
->nruh
= cpu_to_le16(1);
5014 hdr
->maxpids
= cpu_to_le16(NVME_FDP_MAXPIDS
- 1);
5015 hdr
->nnss
= cpu_to_le32(1);
5016 hdr
->runs
= cpu_to_le64(96 * MiB
);
5018 ruhd
->ruht
= NVME_RUHT_INITIALLY_ISOLATED
;
5021 return nvme_c2h(n
, (uint8_t *)buf
+ off
, trans_len
, req
);
5024 static uint16_t nvme_fdp_ruh_usage(NvmeCtrl
*n
, uint32_t endgrpid
,
5025 uint32_t dw10
, uint32_t dw12
,
5026 uint32_t buf_len
, uint64_t off
,
5031 NvmeRuhuDescr
*ruhud
;
5032 NvmeEnduranceGroup
*endgrp
;
5033 g_autofree
uint8_t *buf
= NULL
;
5034 uint32_t log_size
, trans_len
;
5037 if (endgrpid
!= 1 || !n
->subsys
) {
5038 return NVME_INVALID_FIELD
| NVME_DNR
;
5041 endgrp
= &n
->subsys
->endgrp
;
5043 if (!endgrp
->fdp
.enabled
) {
5044 return NVME_FDP_DISABLED
| NVME_DNR
;
5047 log_size
= sizeof(NvmeRuhuLog
) + endgrp
->fdp
.nruh
* sizeof(NvmeRuhuDescr
);
5049 if (off
>= log_size
) {
5050 return NVME_INVALID_FIELD
| NVME_DNR
;
5053 trans_len
= MIN(log_size
- off
, buf_len
);
5055 buf
= g_malloc0(log_size
);
5056 hdr
= (NvmeRuhuLog
*)buf
;
5057 ruhud
= (NvmeRuhuDescr
*)(hdr
+ 1);
5059 ruh
= endgrp
->fdp
.ruhs
;
5060 hdr
->nruh
= cpu_to_le16(endgrp
->fdp
.nruh
);
5062 for (i
= 0; i
< endgrp
->fdp
.nruh
; i
++, ruhud
++, ruh
++) {
5063 ruhud
->ruha
= ruh
->ruha
;
5066 return nvme_c2h(n
, (uint8_t *)buf
+ off
, trans_len
, req
);
5069 static uint16_t nvme_fdp_stats(NvmeCtrl
*n
, uint32_t endgrpid
, uint32_t buf_len
,
5070 uint64_t off
, NvmeRequest
*req
)
5072 NvmeEnduranceGroup
*endgrp
;
5073 NvmeFdpStatsLog log
= {};
5076 if (off
>= sizeof(NvmeFdpStatsLog
)) {
5077 return NVME_INVALID_FIELD
| NVME_DNR
;
5080 if (endgrpid
!= 1 || !n
->subsys
) {
5081 return NVME_INVALID_FIELD
| NVME_DNR
;
5084 if (!n
->subsys
->endgrp
.fdp
.enabled
) {
5085 return NVME_FDP_DISABLED
| NVME_DNR
;
5088 endgrp
= &n
->subsys
->endgrp
;
5090 trans_len
= MIN(sizeof(log
) - off
, buf_len
);
5092 /* spec value is 128 bit, we only use 64 bit */
5093 log
.hbmw
[0] = cpu_to_le64(endgrp
->fdp
.hbmw
);
5094 log
.mbmw
[0] = cpu_to_le64(endgrp
->fdp
.mbmw
);
5095 log
.mbe
[0] = cpu_to_le64(endgrp
->fdp
.mbe
);
5097 return nvme_c2h(n
, (uint8_t *)&log
+ off
, trans_len
, req
);
5100 static uint16_t nvme_fdp_events(NvmeCtrl
*n
, uint32_t endgrpid
,
5101 uint32_t buf_len
, uint64_t off
,
5104 NvmeEnduranceGroup
*endgrp
;
5105 NvmeCmd
*cmd
= &req
->cmd
;
5106 bool host_events
= (cmd
->cdw10
>> 8) & 0x1;
5107 uint32_t log_size
, trans_len
;
5108 NvmeFdpEventBuffer
*ebuf
;
5109 g_autofree NvmeFdpEventsLog
*elog
= NULL
;
5110 NvmeFdpEvent
*event
;
5112 if (endgrpid
!= 1 || !n
->subsys
) {
5113 return NVME_INVALID_FIELD
| NVME_DNR
;
5116 endgrp
= &n
->subsys
->endgrp
;
5118 if (!endgrp
->fdp
.enabled
) {
5119 return NVME_FDP_DISABLED
| NVME_DNR
;
5123 ebuf
= &endgrp
->fdp
.host_events
;
5125 ebuf
= &endgrp
->fdp
.ctrl_events
;
5128 log_size
= sizeof(NvmeFdpEventsLog
) + ebuf
->nelems
* sizeof(NvmeFdpEvent
);
5130 if (off
>= log_size
) {
5131 return NVME_INVALID_FIELD
| NVME_DNR
;
5134 trans_len
= MIN(log_size
- off
, buf_len
);
5135 elog
= g_malloc0(log_size
);
5136 elog
->num_events
= cpu_to_le32(ebuf
->nelems
);
5137 event
= (NvmeFdpEvent
*)(elog
+ 1);
5139 if (ebuf
->nelems
&& ebuf
->start
== ebuf
->next
) {
5140 unsigned int nelems
= (NVME_FDP_MAX_EVENTS
- ebuf
->start
);
5141 /* wrap over, copy [start;NVME_FDP_MAX_EVENTS[ and [0; next[ */
5142 memcpy(event
, &ebuf
->events
[ebuf
->start
],
5143 sizeof(NvmeFdpEvent
) * nelems
);
5144 memcpy(event
+ nelems
, ebuf
->events
,
5145 sizeof(NvmeFdpEvent
) * ebuf
->next
);
5146 } else if (ebuf
->start
< ebuf
->next
) {
5147 memcpy(event
, &ebuf
->events
[ebuf
->start
],
5148 sizeof(NvmeFdpEvent
) * (ebuf
->next
- ebuf
->start
));
5151 return nvme_c2h(n
, (uint8_t *)elog
+ off
, trans_len
, req
);
5154 static uint16_t nvme_get_log(NvmeCtrl
*n
, NvmeRequest
*req
)
5156 NvmeCmd
*cmd
= &req
->cmd
;
5158 uint32_t dw10
= le32_to_cpu(cmd
->cdw10
);
5159 uint32_t dw11
= le32_to_cpu(cmd
->cdw11
);
5160 uint32_t dw12
= le32_to_cpu(cmd
->cdw12
);
5161 uint32_t dw13
= le32_to_cpu(cmd
->cdw13
);
5162 uint8_t lid
= dw10
& 0xff;
5163 uint8_t lsp
= (dw10
>> 8) & 0xf;
5164 uint8_t rae
= (dw10
>> 15) & 0x1;
5165 uint8_t csi
= le32_to_cpu(cmd
->cdw14
) >> 24;
5166 uint32_t numdl
, numdu
, lspi
;
5167 uint64_t off
, lpol
, lpou
;
5171 numdl
= (dw10
>> 16);
5172 numdu
= (dw11
& 0xffff);
5173 lspi
= (dw11
>> 16);
5177 len
= (((numdu
<< 16) | numdl
) + 1) << 2;
5178 off
= (lpou
<< 32ULL) | lpol
;
5181 return NVME_INVALID_FIELD
| NVME_DNR
;
5184 trace_pci_nvme_get_log(nvme_cid(req
), lid
, lsp
, rae
, len
, off
);
5186 status
= nvme_check_mdts(n
, len
);
5192 case NVME_LOG_ERROR_INFO
:
5193 return nvme_error_info(n
, rae
, len
, off
, req
);
5194 case NVME_LOG_SMART_INFO
:
5195 return nvme_smart_info(n
, rae
, len
, off
, req
);
5196 case NVME_LOG_FW_SLOT_INFO
:
5197 return nvme_fw_log_info(n
, len
, off
, req
);
5198 case NVME_LOG_CHANGED_NSLIST
:
5199 return nvme_changed_nslist(n
, rae
, len
, off
, req
);
5200 case NVME_LOG_CMD_EFFECTS
:
5201 return nvme_cmd_effects(n
, csi
, len
, off
, req
);
5202 case NVME_LOG_ENDGRP
:
5203 return nvme_endgrp_info(n
, rae
, len
, off
, req
);
5204 case NVME_LOG_FDP_CONFS
:
5205 return nvme_fdp_confs(n
, lspi
, len
, off
, req
);
5206 case NVME_LOG_FDP_RUH_USAGE
:
5207 return nvme_fdp_ruh_usage(n
, lspi
, dw10
, dw12
, len
, off
, req
);
5208 case NVME_LOG_FDP_STATS
:
5209 return nvme_fdp_stats(n
, lspi
, len
, off
, req
);
5210 case NVME_LOG_FDP_EVENTS
:
5211 return nvme_fdp_events(n
, lspi
, len
, off
, req
);
5213 trace_pci_nvme_err_invalid_log_page(nvme_cid(req
), lid
);
5214 return NVME_INVALID_FIELD
| NVME_DNR
;
5218 static void nvme_free_cq(NvmeCQueue
*cq
, NvmeCtrl
*n
)
5220 PCIDevice
*pci
= PCI_DEVICE(n
);
5221 uint16_t offset
= (cq
->cqid
<< 3) + (1 << 2);
5223 n
->cq
[cq
->cqid
] = NULL
;
5224 qemu_bh_delete(cq
->bh
);
5225 if (cq
->ioeventfd_enabled
) {
5226 memory_region_del_eventfd(&n
->iomem
,
5227 0x1000 + offset
, 4, false, 0, &cq
->notifier
);
5228 event_notifier_set_handler(&cq
->notifier
, NULL
);
5229 event_notifier_cleanup(&cq
->notifier
);
5231 if (msix_enabled(pci
)) {
5232 msix_vector_unuse(pci
, cq
->vector
);
5239 static uint16_t nvme_del_cq(NvmeCtrl
*n
, NvmeRequest
*req
)
5241 NvmeDeleteQ
*c
= (NvmeDeleteQ
*)&req
->cmd
;
5243 uint16_t qid
= le16_to_cpu(c
->qid
);
5245 if (unlikely(!qid
|| nvme_check_cqid(n
, qid
))) {
5246 trace_pci_nvme_err_invalid_del_cq_cqid(qid
);
5247 return NVME_INVALID_CQID
| NVME_DNR
;
5251 if (unlikely(!QTAILQ_EMPTY(&cq
->sq_list
))) {
5252 trace_pci_nvme_err_invalid_del_cq_notempty(qid
);
5253 return NVME_INVALID_QUEUE_DEL
;
5256 if (cq
->irq_enabled
&& cq
->tail
!= cq
->head
) {
5260 nvme_irq_deassert(n
, cq
);
5261 trace_pci_nvme_del_cq(qid
);
5262 nvme_free_cq(cq
, n
);
5263 return NVME_SUCCESS
;
5266 static void nvme_init_cq(NvmeCQueue
*cq
, NvmeCtrl
*n
, uint64_t dma_addr
,
5267 uint16_t cqid
, uint16_t vector
, uint16_t size
,
5268 uint16_t irq_enabled
)
5270 PCIDevice
*pci
= PCI_DEVICE(n
);
5272 if (msix_enabled(pci
)) {
5273 msix_vector_use(pci
, vector
);
5278 cq
->dma_addr
= dma_addr
;
5280 cq
->irq_enabled
= irq_enabled
;
5281 cq
->vector
= vector
;
5282 cq
->head
= cq
->tail
= 0;
5283 QTAILQ_INIT(&cq
->req_list
);
5284 QTAILQ_INIT(&cq
->sq_list
);
5285 if (n
->dbbuf_enabled
) {
5286 cq
->db_addr
= n
->dbbuf_dbs
+ (cqid
<< 3) + (1 << 2);
5287 cq
->ei_addr
= n
->dbbuf_eis
+ (cqid
<< 3) + (1 << 2);
5289 if (n
->params
.ioeventfd
&& cqid
!= 0) {
5290 if (!nvme_init_cq_ioeventfd(cq
)) {
5291 cq
->ioeventfd_enabled
= true;
5296 cq
->bh
= qemu_bh_new_guarded(nvme_post_cqes
, cq
,
5297 &DEVICE(cq
->ctrl
)->mem_reentrancy_guard
);
5300 static uint16_t nvme_create_cq(NvmeCtrl
*n
, NvmeRequest
*req
)
5303 NvmeCreateCq
*c
= (NvmeCreateCq
*)&req
->cmd
;
5304 uint16_t cqid
= le16_to_cpu(c
->cqid
);
5305 uint16_t vector
= le16_to_cpu(c
->irq_vector
);
5306 uint16_t qsize
= le16_to_cpu(c
->qsize
);
5307 uint16_t qflags
= le16_to_cpu(c
->cq_flags
);
5308 uint64_t prp1
= le64_to_cpu(c
->prp1
);
5309 uint32_t cc
= ldq_le_p(&n
->bar
.cc
);
5310 uint8_t iocqes
= NVME_CC_IOCQES(cc
);
5311 uint8_t iosqes
= NVME_CC_IOSQES(cc
);
5313 trace_pci_nvme_create_cq(prp1
, cqid
, vector
, qsize
, qflags
,
5314 NVME_CQ_FLAGS_IEN(qflags
) != 0);
5316 if (iosqes
!= NVME_SQES
|| iocqes
!= NVME_CQES
) {
5317 trace_pci_nvme_err_invalid_create_cq_entry_size(iosqes
, iocqes
);
5318 return NVME_MAX_QSIZE_EXCEEDED
| NVME_DNR
;
5321 if (unlikely(!cqid
|| cqid
> n
->conf_ioqpairs
|| n
->cq
[cqid
] != NULL
)) {
5322 trace_pci_nvme_err_invalid_create_cq_cqid(cqid
);
5323 return NVME_INVALID_QID
| NVME_DNR
;
5325 if (unlikely(!qsize
|| qsize
> NVME_CAP_MQES(ldq_le_p(&n
->bar
.cap
)))) {
5326 trace_pci_nvme_err_invalid_create_cq_size(qsize
);
5327 return NVME_MAX_QSIZE_EXCEEDED
| NVME_DNR
;
5329 if (unlikely(prp1
& (n
->page_size
- 1))) {
5330 trace_pci_nvme_err_invalid_create_cq_addr(prp1
);
5331 return NVME_INVALID_PRP_OFFSET
| NVME_DNR
;
5333 if (unlikely(!msix_enabled(PCI_DEVICE(n
)) && vector
)) {
5334 trace_pci_nvme_err_invalid_create_cq_vector(vector
);
5335 return NVME_INVALID_IRQ_VECTOR
| NVME_DNR
;
5337 if (unlikely(vector
>= n
->conf_msix_qsize
)) {
5338 trace_pci_nvme_err_invalid_create_cq_vector(vector
);
5339 return NVME_INVALID_IRQ_VECTOR
| NVME_DNR
;
5341 if (unlikely(!(NVME_CQ_FLAGS_PC(qflags
)))) {
5342 trace_pci_nvme_err_invalid_create_cq_qflags(NVME_CQ_FLAGS_PC(qflags
));
5343 return NVME_INVALID_FIELD
| NVME_DNR
;
5346 cq
= g_malloc0(sizeof(*cq
));
5347 nvme_init_cq(cq
, n
, prp1
, cqid
, vector
, qsize
+ 1,
5348 NVME_CQ_FLAGS_IEN(qflags
));
5351 * It is only required to set qs_created when creating a completion queue;
5352 * creating a submission queue without a matching completion queue will
5355 n
->qs_created
= true;
5356 return NVME_SUCCESS
;
5359 static uint16_t nvme_rpt_empty_id_struct(NvmeCtrl
*n
, NvmeRequest
*req
)
5361 uint8_t id
[NVME_IDENTIFY_DATA_SIZE
] = {};
5363 return nvme_c2h(n
, id
, sizeof(id
), req
);
5366 static uint16_t nvme_identify_ctrl(NvmeCtrl
*n
, NvmeRequest
*req
)
5368 trace_pci_nvme_identify_ctrl();
5370 return nvme_c2h(n
, (uint8_t *)&n
->id_ctrl
, sizeof(n
->id_ctrl
), req
);
5373 static uint16_t nvme_identify_ctrl_csi(NvmeCtrl
*n
, NvmeRequest
*req
)
5375 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
5376 uint8_t id
[NVME_IDENTIFY_DATA_SIZE
] = {};
5377 NvmeIdCtrlNvm
*id_nvm
= (NvmeIdCtrlNvm
*)&id
;
5379 trace_pci_nvme_identify_ctrl_csi(c
->csi
);
5383 id_nvm
->vsl
= n
->params
.vsl
;
5384 id_nvm
->dmrsl
= cpu_to_le32(n
->dmrsl
);
5387 case NVME_CSI_ZONED
:
5388 ((NvmeIdCtrlZoned
*)&id
)->zasl
= n
->params
.zasl
;
5392 return NVME_INVALID_FIELD
| NVME_DNR
;
5395 return nvme_c2h(n
, id
, sizeof(id
), req
);
5398 static uint16_t nvme_identify_ns(NvmeCtrl
*n
, NvmeRequest
*req
, bool active
)
5401 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
5402 uint32_t nsid
= le32_to_cpu(c
->nsid
);
5404 trace_pci_nvme_identify_ns(nsid
);
5406 if (!nvme_nsid_valid(n
, nsid
) || nsid
== NVME_NSID_BROADCAST
) {
5407 return NVME_INVALID_NSID
| NVME_DNR
;
5410 ns
= nvme_ns(n
, nsid
);
5411 if (unlikely(!ns
)) {
5413 ns
= nvme_subsys_ns(n
->subsys
, nsid
);
5415 return nvme_rpt_empty_id_struct(n
, req
);
5418 return nvme_rpt_empty_id_struct(n
, req
);
5422 if (active
|| ns
->csi
== NVME_CSI_NVM
) {
5423 return nvme_c2h(n
, (uint8_t *)&ns
->id_ns
, sizeof(NvmeIdNs
), req
);
5426 return NVME_INVALID_CMD_SET
| NVME_DNR
;
5429 static uint16_t nvme_identify_ctrl_list(NvmeCtrl
*n
, NvmeRequest
*req
,
5432 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
5433 uint32_t nsid
= le32_to_cpu(c
->nsid
);
5434 uint16_t min_id
= le16_to_cpu(c
->ctrlid
);
5435 uint16_t list
[NVME_CONTROLLER_LIST_SIZE
] = {};
5436 uint16_t *ids
= &list
[1];
5439 int cntlid
, nr_ids
= 0;
5441 trace_pci_nvme_identify_ctrl_list(c
->cns
, min_id
);
5444 return NVME_INVALID_FIELD
| NVME_DNR
;
5448 if (nsid
== NVME_NSID_BROADCAST
) {
5449 return NVME_INVALID_FIELD
| NVME_DNR
;
5452 ns
= nvme_subsys_ns(n
->subsys
, nsid
);
5454 return NVME_INVALID_FIELD
| NVME_DNR
;
5458 for (cntlid
= min_id
; cntlid
< ARRAY_SIZE(n
->subsys
->ctrls
); cntlid
++) {
5459 ctrl
= nvme_subsys_ctrl(n
->subsys
, cntlid
);
5464 if (attached
&& !nvme_ns(ctrl
, nsid
)) {
5468 ids
[nr_ids
++] = cntlid
;
5473 return nvme_c2h(n
, (uint8_t *)list
, sizeof(list
), req
);
5476 static uint16_t nvme_identify_pri_ctrl_cap(NvmeCtrl
*n
, NvmeRequest
*req
)
5478 trace_pci_nvme_identify_pri_ctrl_cap(le16_to_cpu(n
->pri_ctrl_cap
.cntlid
));
5480 return nvme_c2h(n
, (uint8_t *)&n
->pri_ctrl_cap
,
5481 sizeof(NvmePriCtrlCap
), req
);
5484 static uint16_t nvme_identify_sec_ctrl_list(NvmeCtrl
*n
, NvmeRequest
*req
)
5486 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
5487 uint16_t pri_ctrl_id
= le16_to_cpu(n
->pri_ctrl_cap
.cntlid
);
5488 uint16_t min_id
= le16_to_cpu(c
->ctrlid
);
5489 uint8_t num_sec_ctrl
= n
->sec_ctrl_list
.numcntl
;
5490 NvmeSecCtrlList list
= {0};
5493 for (i
= 0; i
< num_sec_ctrl
; i
++) {
5494 if (n
->sec_ctrl_list
.sec
[i
].scid
>= min_id
) {
5495 list
.numcntl
= num_sec_ctrl
- i
;
5496 memcpy(&list
.sec
, n
->sec_ctrl_list
.sec
+ i
,
5497 list
.numcntl
* sizeof(NvmeSecCtrlEntry
));
5502 trace_pci_nvme_identify_sec_ctrl_list(pri_ctrl_id
, list
.numcntl
);
5504 return nvme_c2h(n
, (uint8_t *)&list
, sizeof(list
), req
);
5507 static uint16_t nvme_identify_ns_csi(NvmeCtrl
*n
, NvmeRequest
*req
,
5511 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
5512 uint32_t nsid
= le32_to_cpu(c
->nsid
);
5514 trace_pci_nvme_identify_ns_csi(nsid
, c
->csi
);
5516 if (!nvme_nsid_valid(n
, nsid
) || nsid
== NVME_NSID_BROADCAST
) {
5517 return NVME_INVALID_NSID
| NVME_DNR
;
5520 ns
= nvme_ns(n
, nsid
);
5521 if (unlikely(!ns
)) {
5523 ns
= nvme_subsys_ns(n
->subsys
, nsid
);
5525 return nvme_rpt_empty_id_struct(n
, req
);
5528 return nvme_rpt_empty_id_struct(n
, req
);
5532 if (c
->csi
== NVME_CSI_NVM
) {
5533 return nvme_c2h(n
, (uint8_t *)&ns
->id_ns_nvm
, sizeof(NvmeIdNsNvm
),
5535 } else if (c
->csi
== NVME_CSI_ZONED
&& ns
->csi
== NVME_CSI_ZONED
) {
5536 return nvme_c2h(n
, (uint8_t *)ns
->id_ns_zoned
, sizeof(NvmeIdNsZoned
),
5540 return NVME_INVALID_FIELD
| NVME_DNR
;
5543 static uint16_t nvme_identify_nslist(NvmeCtrl
*n
, NvmeRequest
*req
,
5547 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
5548 uint32_t min_nsid
= le32_to_cpu(c
->nsid
);
5549 uint8_t list
[NVME_IDENTIFY_DATA_SIZE
] = {};
5550 static const int data_len
= sizeof(list
);
5551 uint32_t *list_ptr
= (uint32_t *)list
;
5554 trace_pci_nvme_identify_nslist(min_nsid
);
5557 * Both FFFFFFFFh (NVME_NSID_BROADCAST) and FFFFFFFFEh are invalid values
5558 * since the Active Namespace ID List should return namespaces with ids
5559 * *higher* than the NSID specified in the command. This is also specified
5560 * in the spec (NVM Express v1.3d, Section 5.15.4).
5562 if (min_nsid
>= NVME_NSID_BROADCAST
- 1) {
5563 return NVME_INVALID_NSID
| NVME_DNR
;
5566 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
5570 ns
= nvme_subsys_ns(n
->subsys
, i
);
5578 if (ns
->params
.nsid
<= min_nsid
) {
5581 list_ptr
[j
++] = cpu_to_le32(ns
->params
.nsid
);
5582 if (j
== data_len
/ sizeof(uint32_t)) {
5587 return nvme_c2h(n
, list
, data_len
, req
);
5590 static uint16_t nvme_identify_nslist_csi(NvmeCtrl
*n
, NvmeRequest
*req
,
5594 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
5595 uint32_t min_nsid
= le32_to_cpu(c
->nsid
);
5596 uint8_t list
[NVME_IDENTIFY_DATA_SIZE
] = {};
5597 static const int data_len
= sizeof(list
);
5598 uint32_t *list_ptr
= (uint32_t *)list
;
5601 trace_pci_nvme_identify_nslist_csi(min_nsid
, c
->csi
);
5604 * Same as in nvme_identify_nslist(), FFFFFFFFh/FFFFFFFFEh are invalid.
5606 if (min_nsid
>= NVME_NSID_BROADCAST
- 1) {
5607 return NVME_INVALID_NSID
| NVME_DNR
;
5610 if (c
->csi
!= NVME_CSI_NVM
&& c
->csi
!= NVME_CSI_ZONED
) {
5611 return NVME_INVALID_FIELD
| NVME_DNR
;
5614 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
5618 ns
= nvme_subsys_ns(n
->subsys
, i
);
5626 if (ns
->params
.nsid
<= min_nsid
|| c
->csi
!= ns
->csi
) {
5629 list_ptr
[j
++] = cpu_to_le32(ns
->params
.nsid
);
5630 if (j
== data_len
/ sizeof(uint32_t)) {
5635 return nvme_c2h(n
, list
, data_len
, req
);
5638 static uint16_t nvme_identify_ns_descr_list(NvmeCtrl
*n
, NvmeRequest
*req
)
5641 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
5642 uint32_t nsid
= le32_to_cpu(c
->nsid
);
5643 uint8_t list
[NVME_IDENTIFY_DATA_SIZE
] = {};
5644 uint8_t *pos
= list
;
5647 uint8_t v
[NVME_NIDL_UUID
];
5648 } QEMU_PACKED uuid
= {};
5652 } QEMU_PACKED eui64
= {};
5656 } QEMU_PACKED csi
= {};
5658 trace_pci_nvme_identify_ns_descr_list(nsid
);
5660 if (!nvme_nsid_valid(n
, nsid
) || nsid
== NVME_NSID_BROADCAST
) {
5661 return NVME_INVALID_NSID
| NVME_DNR
;
5664 ns
= nvme_ns(n
, nsid
);
5665 if (unlikely(!ns
)) {
5666 return NVME_INVALID_FIELD
| NVME_DNR
;
5669 if (!qemu_uuid_is_null(&ns
->params
.uuid
)) {
5670 uuid
.hdr
.nidt
= NVME_NIDT_UUID
;
5671 uuid
.hdr
.nidl
= NVME_NIDL_UUID
;
5672 memcpy(uuid
.v
, ns
->params
.uuid
.data
, NVME_NIDL_UUID
);
5673 memcpy(pos
, &uuid
, sizeof(uuid
));
5674 pos
+= sizeof(uuid
);
5677 if (ns
->params
.eui64
) {
5678 eui64
.hdr
.nidt
= NVME_NIDT_EUI64
;
5679 eui64
.hdr
.nidl
= NVME_NIDL_EUI64
;
5680 eui64
.v
= cpu_to_be64(ns
->params
.eui64
);
5681 memcpy(pos
, &eui64
, sizeof(eui64
));
5682 pos
+= sizeof(eui64
);
5685 csi
.hdr
.nidt
= NVME_NIDT_CSI
;
5686 csi
.hdr
.nidl
= NVME_NIDL_CSI
;
5688 memcpy(pos
, &csi
, sizeof(csi
));
5691 return nvme_c2h(n
, list
, sizeof(list
), req
);
5694 static uint16_t nvme_identify_cmd_set(NvmeCtrl
*n
, NvmeRequest
*req
)
5696 uint8_t list
[NVME_IDENTIFY_DATA_SIZE
] = {};
5697 static const int data_len
= sizeof(list
);
5699 trace_pci_nvme_identify_cmd_set();
5701 NVME_SET_CSI(*list
, NVME_CSI_NVM
);
5702 NVME_SET_CSI(*list
, NVME_CSI_ZONED
);
5704 return nvme_c2h(n
, list
, data_len
, req
);
5707 static uint16_t nvme_identify(NvmeCtrl
*n
, NvmeRequest
*req
)
5709 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
5711 trace_pci_nvme_identify(nvme_cid(req
), c
->cns
, le16_to_cpu(c
->ctrlid
),
5715 case NVME_ID_CNS_NS
:
5716 return nvme_identify_ns(n
, req
, true);
5717 case NVME_ID_CNS_NS_PRESENT
:
5718 return nvme_identify_ns(n
, req
, false);
5719 case NVME_ID_CNS_NS_ATTACHED_CTRL_LIST
:
5720 return nvme_identify_ctrl_list(n
, req
, true);
5721 case NVME_ID_CNS_CTRL_LIST
:
5722 return nvme_identify_ctrl_list(n
, req
, false);
5723 case NVME_ID_CNS_PRIMARY_CTRL_CAP
:
5724 return nvme_identify_pri_ctrl_cap(n
, req
);
5725 case NVME_ID_CNS_SECONDARY_CTRL_LIST
:
5726 return nvme_identify_sec_ctrl_list(n
, req
);
5727 case NVME_ID_CNS_CS_NS
:
5728 return nvme_identify_ns_csi(n
, req
, true);
5729 case NVME_ID_CNS_CS_NS_PRESENT
:
5730 return nvme_identify_ns_csi(n
, req
, false);
5731 case NVME_ID_CNS_CTRL
:
5732 return nvme_identify_ctrl(n
, req
);
5733 case NVME_ID_CNS_CS_CTRL
:
5734 return nvme_identify_ctrl_csi(n
, req
);
5735 case NVME_ID_CNS_NS_ACTIVE_LIST
:
5736 return nvme_identify_nslist(n
, req
, true);
5737 case NVME_ID_CNS_NS_PRESENT_LIST
:
5738 return nvme_identify_nslist(n
, req
, false);
5739 case NVME_ID_CNS_CS_NS_ACTIVE_LIST
:
5740 return nvme_identify_nslist_csi(n
, req
, true);
5741 case NVME_ID_CNS_CS_NS_PRESENT_LIST
:
5742 return nvme_identify_nslist_csi(n
, req
, false);
5743 case NVME_ID_CNS_NS_DESCR_LIST
:
5744 return nvme_identify_ns_descr_list(n
, req
);
5745 case NVME_ID_CNS_IO_COMMAND_SET
:
5746 return nvme_identify_cmd_set(n
, req
);
5748 trace_pci_nvme_err_invalid_identify_cns(le32_to_cpu(c
->cns
));
5749 return NVME_INVALID_FIELD
| NVME_DNR
;
5753 static uint16_t nvme_abort(NvmeCtrl
*n
, NvmeRequest
*req
)
5755 uint16_t sqid
= le32_to_cpu(req
->cmd
.cdw10
) & 0xffff;
5757 req
->cqe
.result
= 1;
5758 if (nvme_check_sqid(n
, sqid
)) {
5759 return NVME_INVALID_FIELD
| NVME_DNR
;
5762 return NVME_SUCCESS
;
5765 static inline void nvme_set_timestamp(NvmeCtrl
*n
, uint64_t ts
)
5767 trace_pci_nvme_setfeat_timestamp(ts
);
5769 n
->host_timestamp
= le64_to_cpu(ts
);
5770 n
->timestamp_set_qemu_clock_ms
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
);
5773 static inline uint64_t nvme_get_timestamp(const NvmeCtrl
*n
)
5775 uint64_t current_time
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
);
5776 uint64_t elapsed_time
= current_time
- n
->timestamp_set_qemu_clock_ms
;
5778 union nvme_timestamp
{
5780 uint64_t timestamp
:48;
5788 union nvme_timestamp ts
;
5790 ts
.timestamp
= n
->host_timestamp
+ elapsed_time
;
5792 /* If the host timestamp is non-zero, set the timestamp origin */
5793 ts
.origin
= n
->host_timestamp
? 0x01 : 0x00;
5795 trace_pci_nvme_getfeat_timestamp(ts
.all
);
5797 return cpu_to_le64(ts
.all
);
5800 static uint16_t nvme_get_feature_timestamp(NvmeCtrl
*n
, NvmeRequest
*req
)
5802 uint64_t timestamp
= nvme_get_timestamp(n
);
5804 return nvme_c2h(n
, (uint8_t *)×tamp
, sizeof(timestamp
), req
);
5807 static int nvme_get_feature_fdp(NvmeCtrl
*n
, uint32_t endgrpid
,
5812 if (!n
->subsys
|| !n
->subsys
->endgrp
.fdp
.enabled
) {
5813 return NVME_INVALID_FIELD
| NVME_DNR
;
5816 *result
= FIELD_DP16(0, FEAT_FDP
, FDPE
, 1);
5817 *result
= FIELD_DP16(*result
, FEAT_FDP
, CONF_NDX
, 0);
5819 return NVME_SUCCESS
;
5822 static uint16_t nvme_get_feature_fdp_events(NvmeCtrl
*n
, NvmeNamespace
*ns
,
5823 NvmeRequest
*req
, uint32_t *result
)
5825 NvmeCmd
*cmd
= &req
->cmd
;
5826 uint32_t cdw11
= le32_to_cpu(cmd
->cdw11
);
5827 uint16_t ph
= cdw11
& 0xffff;
5828 uint8_t noet
= (cdw11
>> 16) & 0xff;
5829 uint16_t ruhid
, ret
;
5830 uint32_t nentries
= 0;
5831 uint8_t s_events_ndx
= 0;
5832 size_t s_events_siz
= sizeof(NvmeFdpEventDescr
) * noet
;
5833 g_autofree NvmeFdpEventDescr
*s_events
= g_malloc0(s_events_siz
);
5835 NvmeFdpEventDescr
*s_event
;
5837 if (!n
->subsys
|| !n
->subsys
->endgrp
.fdp
.enabled
) {
5838 return NVME_FDP_DISABLED
| NVME_DNR
;
5841 if (!nvme_ph_valid(ns
, ph
)) {
5842 return NVME_INVALID_FIELD
| NVME_DNR
;
5845 ruhid
= ns
->fdp
.phs
[ph
];
5846 ruh
= &n
->subsys
->endgrp
.fdp
.ruhs
[ruhid
];
5850 if (unlikely(noet
== 0)) {
5851 return NVME_INVALID_FIELD
| NVME_DNR
;
5854 for (uint8_t event_type
= 0; event_type
< FDP_EVT_MAX
; event_type
++) {
5855 uint8_t shift
= nvme_fdp_evf_shifts
[event_type
];
5856 if (!shift
&& event_type
) {
5858 * only first entry (event_type == 0) has a shift value of 0
5859 * other entries are simply unpopulated.
5866 s_event
= &s_events
[s_events_ndx
];
5867 s_event
->evt
= event_type
;
5868 s_event
->evta
= (ruh
->event_filter
>> shift
) & 0x1;
5870 /* break if all `noet` entries are filled */
5871 if ((++s_events_ndx
) == noet
) {
5876 ret
= nvme_c2h(n
, s_events
, s_events_siz
, req
);
5882 return NVME_SUCCESS
;
5885 static uint16_t nvme_get_feature(NvmeCtrl
*n
, NvmeRequest
*req
)
5887 NvmeCmd
*cmd
= &req
->cmd
;
5888 uint32_t dw10
= le32_to_cpu(cmd
->cdw10
);
5889 uint32_t dw11
= le32_to_cpu(cmd
->cdw11
);
5890 uint32_t nsid
= le32_to_cpu(cmd
->nsid
);
5892 uint8_t fid
= NVME_GETSETFEAT_FID(dw10
);
5893 NvmeGetFeatureSelect sel
= NVME_GETFEAT_SELECT(dw10
);
5897 uint16_t endgrpid
= 0, ret
= NVME_SUCCESS
;
5899 static const uint32_t nvme_feature_default
[NVME_FID_MAX
] = {
5900 [NVME_ARBITRATION
] = NVME_ARB_AB_NOLIMIT
,
5903 trace_pci_nvme_getfeat(nvme_cid(req
), nsid
, fid
, sel
, dw11
);
5905 if (!nvme_feature_support
[fid
]) {
5906 return NVME_INVALID_FIELD
| NVME_DNR
;
5909 if (nvme_feature_cap
[fid
] & NVME_FEAT_CAP_NS
) {
5910 if (!nvme_nsid_valid(n
, nsid
) || nsid
== NVME_NSID_BROADCAST
) {
5912 * The Reservation Notification Mask and Reservation Persistence
5913 * features require a status code of Invalid Field in Command when
5914 * NSID is FFFFFFFFh. Since the device does not support those
5915 * features we can always return Invalid Namespace or Format as we
5916 * should do for all other features.
5918 return NVME_INVALID_NSID
| NVME_DNR
;
5921 if (!nvme_ns(n
, nsid
)) {
5922 return NVME_INVALID_FIELD
| NVME_DNR
;
5927 case NVME_GETFEAT_SELECT_CURRENT
:
5929 case NVME_GETFEAT_SELECT_SAVED
:
5930 /* no features are saveable by the controller; fallthrough */
5931 case NVME_GETFEAT_SELECT_DEFAULT
:
5933 case NVME_GETFEAT_SELECT_CAP
:
5934 result
= nvme_feature_cap
[fid
];
5939 case NVME_TEMPERATURE_THRESHOLD
:
5943 * The controller only implements the Composite Temperature sensor, so
5944 * return 0 for all other sensors.
5946 if (NVME_TEMP_TMPSEL(dw11
) != NVME_TEMP_TMPSEL_COMPOSITE
) {
5950 switch (NVME_TEMP_THSEL(dw11
)) {
5951 case NVME_TEMP_THSEL_OVER
:
5952 result
= n
->features
.temp_thresh_hi
;
5954 case NVME_TEMP_THSEL_UNDER
:
5955 result
= n
->features
.temp_thresh_low
;
5959 return NVME_INVALID_FIELD
| NVME_DNR
;
5960 case NVME_ERROR_RECOVERY
:
5961 if (!nvme_nsid_valid(n
, nsid
)) {
5962 return NVME_INVALID_NSID
| NVME_DNR
;
5965 ns
= nvme_ns(n
, nsid
);
5966 if (unlikely(!ns
)) {
5967 return NVME_INVALID_FIELD
| NVME_DNR
;
5970 result
= ns
->features
.err_rec
;
5972 case NVME_VOLATILE_WRITE_CACHE
:
5974 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
5980 result
= blk_enable_write_cache(ns
->blkconf
.blk
);
5985 trace_pci_nvme_getfeat_vwcache(result
? "enabled" : "disabled");
5987 case NVME_ASYNCHRONOUS_EVENT_CONF
:
5988 result
= n
->features
.async_config
;
5990 case NVME_TIMESTAMP
:
5991 return nvme_get_feature_timestamp(n
, req
);
5992 case NVME_HOST_BEHAVIOR_SUPPORT
:
5993 return nvme_c2h(n
, (uint8_t *)&n
->features
.hbs
,
5994 sizeof(n
->features
.hbs
), req
);
5996 endgrpid
= dw11
& 0xff;
5998 if (endgrpid
!= 0x1) {
5999 return NVME_INVALID_FIELD
| NVME_DNR
;
6002 ret
= nvme_get_feature_fdp(n
, endgrpid
, &result
);
6007 case NVME_FDP_EVENTS
:
6008 if (!nvme_nsid_valid(n
, nsid
)) {
6009 return NVME_INVALID_NSID
| NVME_DNR
;
6012 ns
= nvme_ns(n
, nsid
);
6013 if (unlikely(!ns
)) {
6014 return NVME_INVALID_FIELD
| NVME_DNR
;
6017 ret
= nvme_get_feature_fdp_events(n
, ns
, req
, &result
);
6028 case NVME_TEMPERATURE_THRESHOLD
:
6031 if (NVME_TEMP_TMPSEL(dw11
) != NVME_TEMP_TMPSEL_COMPOSITE
) {
6035 if (NVME_TEMP_THSEL(dw11
) == NVME_TEMP_THSEL_OVER
) {
6036 result
= NVME_TEMPERATURE_WARNING
;
6040 case NVME_NUMBER_OF_QUEUES
:
6041 result
= (n
->conf_ioqpairs
- 1) | ((n
->conf_ioqpairs
- 1) << 16);
6042 trace_pci_nvme_getfeat_numq(result
);
6044 case NVME_INTERRUPT_VECTOR_CONF
:
6046 if (iv
>= n
->conf_ioqpairs
+ 1) {
6047 return NVME_INVALID_FIELD
| NVME_DNR
;
6051 if (iv
== n
->admin_cq
.vector
) {
6052 result
|= NVME_INTVC_NOCOALESCING
;
6056 endgrpid
= dw11
& 0xff;
6058 if (endgrpid
!= 0x1) {
6059 return NVME_INVALID_FIELD
| NVME_DNR
;
6062 ret
= nvme_get_feature_fdp(n
, endgrpid
, &result
);
6070 result
= nvme_feature_default
[fid
];
6075 req
->cqe
.result
= cpu_to_le32(result
);
6079 static uint16_t nvme_set_feature_timestamp(NvmeCtrl
*n
, NvmeRequest
*req
)
6084 ret
= nvme_h2c(n
, (uint8_t *)×tamp
, sizeof(timestamp
), req
);
6089 nvme_set_timestamp(n
, timestamp
);
6091 return NVME_SUCCESS
;
6094 static uint16_t nvme_set_feature_fdp_events(NvmeCtrl
*n
, NvmeNamespace
*ns
,
6097 NvmeCmd
*cmd
= &req
->cmd
;
6098 uint32_t cdw11
= le32_to_cpu(cmd
->cdw11
);
6099 uint16_t ph
= cdw11
& 0xffff;
6100 uint8_t noet
= (cdw11
>> 16) & 0xff;
6101 uint16_t ret
, ruhid
;
6102 uint8_t enable
= le32_to_cpu(cmd
->cdw12
) & 0x1;
6103 uint8_t event_mask
= 0;
6105 g_autofree
uint8_t *events
= g_malloc0(noet
);
6106 NvmeRuHandle
*ruh
= NULL
;
6110 if (!n
->subsys
|| !n
->subsys
->endgrp
.fdp
.enabled
) {
6111 return NVME_FDP_DISABLED
| NVME_DNR
;
6114 if (!nvme_ph_valid(ns
, ph
)) {
6115 return NVME_INVALID_FIELD
| NVME_DNR
;
6118 ruhid
= ns
->fdp
.phs
[ph
];
6119 ruh
= &n
->subsys
->endgrp
.fdp
.ruhs
[ruhid
];
6121 ret
= nvme_h2c(n
, events
, noet
, req
);
6126 for (i
= 0; i
< noet
; i
++) {
6127 event_mask
|= (1 << nvme_fdp_evf_shifts
[events
[i
]]);
6131 ruh
->event_filter
|= event_mask
;
6133 ruh
->event_filter
= ruh
->event_filter
& ~event_mask
;
6136 return NVME_SUCCESS
;
6139 static uint16_t nvme_set_feature(NvmeCtrl
*n
, NvmeRequest
*req
)
6141 NvmeNamespace
*ns
= NULL
;
6143 NvmeCmd
*cmd
= &req
->cmd
;
6144 uint32_t dw10
= le32_to_cpu(cmd
->cdw10
);
6145 uint32_t dw11
= le32_to_cpu(cmd
->cdw11
);
6146 uint32_t nsid
= le32_to_cpu(cmd
->nsid
);
6147 uint8_t fid
= NVME_GETSETFEAT_FID(dw10
);
6148 uint8_t save
= NVME_SETFEAT_SAVE(dw10
);
6152 trace_pci_nvme_setfeat(nvme_cid(req
), nsid
, fid
, save
, dw11
);
6154 if (save
&& !(nvme_feature_cap
[fid
] & NVME_FEAT_CAP_SAVE
)) {
6155 return NVME_FID_NOT_SAVEABLE
| NVME_DNR
;
6158 if (!nvme_feature_support
[fid
]) {
6159 return NVME_INVALID_FIELD
| NVME_DNR
;
6162 if (nvme_feature_cap
[fid
] & NVME_FEAT_CAP_NS
) {
6163 if (nsid
!= NVME_NSID_BROADCAST
) {
6164 if (!nvme_nsid_valid(n
, nsid
)) {
6165 return NVME_INVALID_NSID
| NVME_DNR
;
6168 ns
= nvme_ns(n
, nsid
);
6169 if (unlikely(!ns
)) {
6170 return NVME_INVALID_FIELD
| NVME_DNR
;
6173 } else if (nsid
&& nsid
!= NVME_NSID_BROADCAST
) {
6174 if (!nvme_nsid_valid(n
, nsid
)) {
6175 return NVME_INVALID_NSID
| NVME_DNR
;
6178 return NVME_FEAT_NOT_NS_SPEC
| NVME_DNR
;
6181 if (!(nvme_feature_cap
[fid
] & NVME_FEAT_CAP_CHANGE
)) {
6182 return NVME_FEAT_NOT_CHANGEABLE
| NVME_DNR
;
6186 case NVME_TEMPERATURE_THRESHOLD
:
6187 if (NVME_TEMP_TMPSEL(dw11
) != NVME_TEMP_TMPSEL_COMPOSITE
) {
6191 switch (NVME_TEMP_THSEL(dw11
)) {
6192 case NVME_TEMP_THSEL_OVER
:
6193 n
->features
.temp_thresh_hi
= NVME_TEMP_TMPTH(dw11
);
6195 case NVME_TEMP_THSEL_UNDER
:
6196 n
->features
.temp_thresh_low
= NVME_TEMP_TMPTH(dw11
);
6199 return NVME_INVALID_FIELD
| NVME_DNR
;
6202 if ((n
->temperature
>= n
->features
.temp_thresh_hi
) ||
6203 (n
->temperature
<= n
->features
.temp_thresh_low
)) {
6204 nvme_smart_event(n
, NVME_SMART_TEMPERATURE
);
6208 case NVME_ERROR_RECOVERY
:
6209 if (nsid
== NVME_NSID_BROADCAST
) {
6210 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
6217 if (NVME_ID_NS_NSFEAT_DULBE(ns
->id_ns
.nsfeat
)) {
6218 ns
->features
.err_rec
= dw11
;
6226 if (NVME_ID_NS_NSFEAT_DULBE(ns
->id_ns
.nsfeat
)) {
6227 ns
->features
.err_rec
= dw11
;
6230 case NVME_VOLATILE_WRITE_CACHE
:
6231 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
6237 if (!(dw11
& 0x1) && blk_enable_write_cache(ns
->blkconf
.blk
)) {
6238 blk_flush(ns
->blkconf
.blk
);
6241 blk_set_enable_write_cache(ns
->blkconf
.blk
, dw11
& 1);
6246 case NVME_NUMBER_OF_QUEUES
:
6247 if (n
->qs_created
) {
6248 return NVME_CMD_SEQ_ERROR
| NVME_DNR
;
6252 * NVMe v1.3, Section 5.21.1.7: FFFFh is not an allowed value for NCQR
6255 if ((dw11
& 0xffff) == 0xffff || ((dw11
>> 16) & 0xffff) == 0xffff) {
6256 return NVME_INVALID_FIELD
| NVME_DNR
;
6259 trace_pci_nvme_setfeat_numq((dw11
& 0xffff) + 1,
6260 ((dw11
>> 16) & 0xffff) + 1,
6263 req
->cqe
.result
= cpu_to_le32((n
->conf_ioqpairs
- 1) |
6264 ((n
->conf_ioqpairs
- 1) << 16));
6266 case NVME_ASYNCHRONOUS_EVENT_CONF
:
6267 n
->features
.async_config
= dw11
;
6269 case NVME_TIMESTAMP
:
6270 return nvme_set_feature_timestamp(n
, req
);
6271 case NVME_HOST_BEHAVIOR_SUPPORT
:
6272 status
= nvme_h2c(n
, (uint8_t *)&n
->features
.hbs
,
6273 sizeof(n
->features
.hbs
), req
);
6278 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
6285 ns
->id_ns
.nlbaf
= ns
->nlbaf
- 1;
6286 if (!n
->features
.hbs
.lbafee
) {
6287 ns
->id_ns
.nlbaf
= MIN(ns
->id_ns
.nlbaf
, 15);
6292 case NVME_COMMAND_SET_PROFILE
:
6294 trace_pci_nvme_err_invalid_iocsci(dw11
& 0x1ff);
6295 return NVME_CMD_SET_CMB_REJECTED
| NVME_DNR
;
6299 /* spec: abort with cmd seq err if there's one or more NS' in endgrp */
6300 return NVME_CMD_SEQ_ERROR
| NVME_DNR
;
6301 case NVME_FDP_EVENTS
:
6302 return nvme_set_feature_fdp_events(n
, ns
, req
);
6304 return NVME_FEAT_NOT_CHANGEABLE
| NVME_DNR
;
6306 return NVME_SUCCESS
;
6309 static uint16_t nvme_aer(NvmeCtrl
*n
, NvmeRequest
*req
)
6311 trace_pci_nvme_aer(nvme_cid(req
));
6313 if (n
->outstanding_aers
> n
->params
.aerl
) {
6314 trace_pci_nvme_aer_aerl_exceeded();
6315 return NVME_AER_LIMIT_EXCEEDED
;
6318 n
->aer_reqs
[n
->outstanding_aers
] = req
;
6319 n
->outstanding_aers
++;
6321 if (!QTAILQ_EMPTY(&n
->aer_queue
)) {
6322 nvme_process_aers(n
);
6325 return NVME_NO_COMPLETE
;
6328 static void nvme_update_dmrsl(NvmeCtrl
*n
)
6332 for (nsid
= 1; nsid
<= NVME_MAX_NAMESPACES
; nsid
++) {
6333 NvmeNamespace
*ns
= nvme_ns(n
, nsid
);
6338 n
->dmrsl
= MIN_NON_ZERO(n
->dmrsl
,
6339 BDRV_REQUEST_MAX_BYTES
/ nvme_l2b(ns
, 1));
6343 static void nvme_select_iocs_ns(NvmeCtrl
*n
, NvmeNamespace
*ns
)
6345 uint32_t cc
= ldl_le_p(&n
->bar
.cc
);
6347 ns
->iocs
= nvme_cse_iocs_none
;
6350 if (NVME_CC_CSS(cc
) != NVME_CC_CSS_ADMIN_ONLY
) {
6351 ns
->iocs
= nvme_cse_iocs_nvm
;
6354 case NVME_CSI_ZONED
:
6355 if (NVME_CC_CSS(cc
) == NVME_CC_CSS_CSI
) {
6356 ns
->iocs
= nvme_cse_iocs_zoned
;
6357 } else if (NVME_CC_CSS(cc
) == NVME_CC_CSS_NVM
) {
6358 ns
->iocs
= nvme_cse_iocs_nvm
;
6364 static uint16_t nvme_ns_attachment(NvmeCtrl
*n
, NvmeRequest
*req
)
6368 uint16_t list
[NVME_CONTROLLER_LIST_SIZE
] = {};
6369 uint32_t nsid
= le32_to_cpu(req
->cmd
.nsid
);
6370 uint32_t dw10
= le32_to_cpu(req
->cmd
.cdw10
);
6371 uint8_t sel
= dw10
& 0xf;
6372 uint16_t *nr_ids
= &list
[0];
6373 uint16_t *ids
= &list
[1];
6377 trace_pci_nvme_ns_attachment(nvme_cid(req
), dw10
& 0xf);
6379 if (!nvme_nsid_valid(n
, nsid
)) {
6380 return NVME_INVALID_NSID
| NVME_DNR
;
6383 ns
= nvme_subsys_ns(n
->subsys
, nsid
);
6385 return NVME_INVALID_FIELD
| NVME_DNR
;
6388 ret
= nvme_h2c(n
, (uint8_t *)list
, 4096, req
);
6394 return NVME_NS_CTRL_LIST_INVALID
| NVME_DNR
;
6397 *nr_ids
= MIN(*nr_ids
, NVME_CONTROLLER_LIST_SIZE
- 1);
6398 for (i
= 0; i
< *nr_ids
; i
++) {
6399 ctrl
= nvme_subsys_ctrl(n
->subsys
, ids
[i
]);
6401 return NVME_NS_CTRL_LIST_INVALID
| NVME_DNR
;
6405 case NVME_NS_ATTACHMENT_ATTACH
:
6406 if (nvme_ns(ctrl
, nsid
)) {
6407 return NVME_NS_ALREADY_ATTACHED
| NVME_DNR
;
6410 if (ns
->attached
&& !ns
->params
.shared
) {
6411 return NVME_NS_PRIVATE
| NVME_DNR
;
6414 nvme_attach_ns(ctrl
, ns
);
6415 nvme_select_iocs_ns(ctrl
, ns
);
6419 case NVME_NS_ATTACHMENT_DETACH
:
6420 if (!nvme_ns(ctrl
, nsid
)) {
6421 return NVME_NS_NOT_ATTACHED
| NVME_DNR
;
6424 ctrl
->namespaces
[nsid
] = NULL
;
6427 nvme_update_dmrsl(ctrl
);
6432 return NVME_INVALID_FIELD
| NVME_DNR
;
6436 * Add namespace id to the changed namespace id list for event clearing
6437 * via Get Log Page command.
6439 if (!test_and_set_bit(nsid
, ctrl
->changed_nsids
)) {
6440 nvme_enqueue_event(ctrl
, NVME_AER_TYPE_NOTICE
,
6441 NVME_AER_INFO_NOTICE_NS_ATTR_CHANGED
,
6442 NVME_LOG_CHANGED_NSLIST
);
6446 return NVME_SUCCESS
;
6449 typedef struct NvmeFormatAIOCB
{
6466 static void nvme_format_cancel(BlockAIOCB
*aiocb
)
6468 NvmeFormatAIOCB
*iocb
= container_of(aiocb
, NvmeFormatAIOCB
, common
);
6470 iocb
->ret
= -ECANCELED
;
6473 blk_aio_cancel_async(iocb
->aiocb
);
6478 static const AIOCBInfo nvme_format_aiocb_info
= {
6479 .aiocb_size
= sizeof(NvmeFormatAIOCB
),
6480 .cancel_async
= nvme_format_cancel
,
6481 .get_aio_context
= nvme_get_aio_context
,
6484 static void nvme_format_set(NvmeNamespace
*ns
, uint8_t lbaf
, uint8_t mset
,
6485 uint8_t pi
, uint8_t pil
)
6487 uint8_t lbafl
= lbaf
& 0xf;
6488 uint8_t lbafu
= lbaf
>> 4;
6490 trace_pci_nvme_format_set(ns
->params
.nsid
, lbaf
, mset
, pi
, pil
);
6492 ns
->id_ns
.dps
= (pil
<< 3) | pi
;
6493 ns
->id_ns
.flbas
= (lbafu
<< 5) | (mset
<< 4) | lbafl
;
6495 nvme_ns_init_format(ns
);
6498 static void nvme_do_format(NvmeFormatAIOCB
*iocb
);
6500 static void nvme_format_ns_cb(void *opaque
, int ret
)
6502 NvmeFormatAIOCB
*iocb
= opaque
;
6503 NvmeNamespace
*ns
= iocb
->ns
;
6506 if (iocb
->ret
< 0) {
6508 } else if (ret
< 0) {
6515 if (iocb
->offset
< ns
->size
) {
6516 bytes
= MIN(BDRV_REQUEST_MAX_BYTES
, ns
->size
- iocb
->offset
);
6518 iocb
->aiocb
= blk_aio_pwrite_zeroes(ns
->blkconf
.blk
, iocb
->offset
,
6519 bytes
, BDRV_REQ_MAY_UNMAP
,
6520 nvme_format_ns_cb
, iocb
);
6522 iocb
->offset
+= bytes
;
6526 nvme_format_set(ns
, iocb
->lbaf
, iocb
->mset
, iocb
->pi
, iocb
->pil
);
6532 nvme_do_format(iocb
);
6535 static uint16_t nvme_format_check(NvmeNamespace
*ns
, uint8_t lbaf
, uint8_t pi
)
6537 if (ns
->params
.zoned
) {
6538 return NVME_INVALID_FORMAT
| NVME_DNR
;
6541 if (lbaf
> ns
->id_ns
.nlbaf
) {
6542 return NVME_INVALID_FORMAT
| NVME_DNR
;
6545 if (pi
&& (ns
->id_ns
.lbaf
[lbaf
].ms
< nvme_pi_tuple_size(ns
))) {
6546 return NVME_INVALID_FORMAT
| NVME_DNR
;
6549 if (pi
&& pi
> NVME_ID_NS_DPS_TYPE_3
) {
6550 return NVME_INVALID_FIELD
| NVME_DNR
;
6553 return NVME_SUCCESS
;
6556 static void nvme_do_format(NvmeFormatAIOCB
*iocb
)
6558 NvmeRequest
*req
= iocb
->req
;
6559 NvmeCtrl
*n
= nvme_ctrl(req
);
6560 uint32_t dw10
= le32_to_cpu(req
->cmd
.cdw10
);
6561 uint8_t lbaf
= dw10
& 0xf;
6562 uint8_t pi
= (dw10
>> 5) & 0x7;
6566 if (iocb
->ret
< 0) {
6570 if (iocb
->broadcast
) {
6571 for (i
= iocb
->nsid
+ 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
6572 iocb
->ns
= nvme_ns(n
, i
);
6584 status
= nvme_format_check(iocb
->ns
, lbaf
, pi
);
6586 req
->status
= status
;
6590 iocb
->ns
->status
= NVME_FORMAT_IN_PROGRESS
;
6591 nvme_format_ns_cb(iocb
, 0);
6595 iocb
->common
.cb(iocb
->common
.opaque
, iocb
->ret
);
6596 qemu_aio_unref(iocb
);
6599 static uint16_t nvme_format(NvmeCtrl
*n
, NvmeRequest
*req
)
6601 NvmeFormatAIOCB
*iocb
;
6602 uint32_t nsid
= le32_to_cpu(req
->cmd
.nsid
);
6603 uint32_t dw10
= le32_to_cpu(req
->cmd
.cdw10
);
6604 uint8_t lbaf
= dw10
& 0xf;
6605 uint8_t mset
= (dw10
>> 4) & 0x1;
6606 uint8_t pi
= (dw10
>> 5) & 0x7;
6607 uint8_t pil
= (dw10
>> 8) & 0x1;
6608 uint8_t lbafu
= (dw10
>> 12) & 0x3;
6611 iocb
= qemu_aio_get(&nvme_format_aiocb_info
, NULL
, nvme_misc_cb
, req
);
6621 iocb
->broadcast
= (nsid
== NVME_NSID_BROADCAST
);
6624 if (n
->features
.hbs
.lbafee
) {
6625 iocb
->lbaf
|= lbafu
<< 4;
6628 if (!iocb
->broadcast
) {
6629 if (!nvme_nsid_valid(n
, nsid
)) {
6630 status
= NVME_INVALID_NSID
| NVME_DNR
;
6634 iocb
->ns
= nvme_ns(n
, nsid
);
6636 status
= NVME_INVALID_FIELD
| NVME_DNR
;
6641 req
->aiocb
= &iocb
->common
;
6642 nvme_do_format(iocb
);
6644 return NVME_NO_COMPLETE
;
6647 qemu_aio_unref(iocb
);
6652 static void nvme_get_virt_res_num(NvmeCtrl
*n
, uint8_t rt
, int *num_total
,
6653 int *num_prim
, int *num_sec
)
6655 *num_total
= le32_to_cpu(rt
?
6656 n
->pri_ctrl_cap
.vifrt
: n
->pri_ctrl_cap
.vqfrt
);
6657 *num_prim
= le16_to_cpu(rt
?
6658 n
->pri_ctrl_cap
.virfap
: n
->pri_ctrl_cap
.vqrfap
);
6659 *num_sec
= le16_to_cpu(rt
? n
->pri_ctrl_cap
.virfa
: n
->pri_ctrl_cap
.vqrfa
);
6662 static uint16_t nvme_assign_virt_res_to_prim(NvmeCtrl
*n
, NvmeRequest
*req
,
6663 uint16_t cntlid
, uint8_t rt
,
6666 int num_total
, num_prim
, num_sec
;
6668 if (cntlid
!= n
->cntlid
) {
6669 return NVME_INVALID_CTRL_ID
| NVME_DNR
;
6672 nvme_get_virt_res_num(n
, rt
, &num_total
, &num_prim
, &num_sec
);
6674 if (nr
> num_total
) {
6675 return NVME_INVALID_NUM_RESOURCES
| NVME_DNR
;
6678 if (nr
> num_total
- num_sec
) {
6679 return NVME_INVALID_RESOURCE_ID
| NVME_DNR
;
6683 n
->next_pri_ctrl_cap
.virfap
= cpu_to_le16(nr
);
6685 n
->next_pri_ctrl_cap
.vqrfap
= cpu_to_le16(nr
);
6688 req
->cqe
.result
= cpu_to_le32(nr
);
6692 static void nvme_update_virt_res(NvmeCtrl
*n
, NvmeSecCtrlEntry
*sctrl
,
6695 int prev_nr
, prev_total
;
6698 prev_nr
= le16_to_cpu(sctrl
->nvi
);
6699 prev_total
= le32_to_cpu(n
->pri_ctrl_cap
.virfa
);
6700 sctrl
->nvi
= cpu_to_le16(nr
);
6701 n
->pri_ctrl_cap
.virfa
= cpu_to_le32(prev_total
+ nr
- prev_nr
);
6703 prev_nr
= le16_to_cpu(sctrl
->nvq
);
6704 prev_total
= le32_to_cpu(n
->pri_ctrl_cap
.vqrfa
);
6705 sctrl
->nvq
= cpu_to_le16(nr
);
6706 n
->pri_ctrl_cap
.vqrfa
= cpu_to_le32(prev_total
+ nr
- prev_nr
);
6710 static uint16_t nvme_assign_virt_res_to_sec(NvmeCtrl
*n
, NvmeRequest
*req
,
6711 uint16_t cntlid
, uint8_t rt
, int nr
)
6713 int num_total
, num_prim
, num_sec
, num_free
, diff
, limit
;
6714 NvmeSecCtrlEntry
*sctrl
;
6716 sctrl
= nvme_sctrl_for_cntlid(n
, cntlid
);
6718 return NVME_INVALID_CTRL_ID
| NVME_DNR
;
6722 return NVME_INVALID_SEC_CTRL_STATE
| NVME_DNR
;
6725 limit
= le16_to_cpu(rt
? n
->pri_ctrl_cap
.vifrsm
: n
->pri_ctrl_cap
.vqfrsm
);
6727 return NVME_INVALID_NUM_RESOURCES
| NVME_DNR
;
6730 nvme_get_virt_res_num(n
, rt
, &num_total
, &num_prim
, &num_sec
);
6731 num_free
= num_total
- num_prim
- num_sec
;
6732 diff
= nr
- le16_to_cpu(rt
? sctrl
->nvi
: sctrl
->nvq
);
6734 if (diff
> num_free
) {
6735 return NVME_INVALID_RESOURCE_ID
| NVME_DNR
;
6738 nvme_update_virt_res(n
, sctrl
, rt
, nr
);
6739 req
->cqe
.result
= cpu_to_le32(nr
);
6744 static uint16_t nvme_virt_set_state(NvmeCtrl
*n
, uint16_t cntlid
, bool online
)
6746 PCIDevice
*pci
= PCI_DEVICE(n
);
6747 NvmeCtrl
*sn
= NULL
;
6748 NvmeSecCtrlEntry
*sctrl
;
6751 sctrl
= nvme_sctrl_for_cntlid(n
, cntlid
);
6753 return NVME_INVALID_CTRL_ID
| NVME_DNR
;
6756 if (!pci_is_vf(pci
)) {
6757 vf_index
= le16_to_cpu(sctrl
->vfn
) - 1;
6758 sn
= NVME(pcie_sriov_get_vf_at_index(pci
, vf_index
));
6762 if (!sctrl
->nvi
|| (le16_to_cpu(sctrl
->nvq
) < 2) || !sn
) {
6763 return NVME_INVALID_SEC_CTRL_STATE
| NVME_DNR
;
6768 nvme_ctrl_reset(sn
, NVME_RESET_FUNCTION
);
6771 nvme_update_virt_res(n
, sctrl
, NVME_VIRT_RES_INTERRUPT
, 0);
6772 nvme_update_virt_res(n
, sctrl
, NVME_VIRT_RES_QUEUE
, 0);
6777 nvme_ctrl_reset(sn
, NVME_RESET_FUNCTION
);
6782 return NVME_SUCCESS
;
6785 static uint16_t nvme_virt_mngmt(NvmeCtrl
*n
, NvmeRequest
*req
)
6787 uint32_t dw10
= le32_to_cpu(req
->cmd
.cdw10
);
6788 uint32_t dw11
= le32_to_cpu(req
->cmd
.cdw11
);
6789 uint8_t act
= dw10
& 0xf;
6790 uint8_t rt
= (dw10
>> 8) & 0x7;
6791 uint16_t cntlid
= (dw10
>> 16) & 0xffff;
6792 int nr
= dw11
& 0xffff;
6794 trace_pci_nvme_virt_mngmt(nvme_cid(req
), act
, cntlid
, rt
? "VI" : "VQ", nr
);
6796 if (rt
!= NVME_VIRT_RES_QUEUE
&& rt
!= NVME_VIRT_RES_INTERRUPT
) {
6797 return NVME_INVALID_RESOURCE_ID
| NVME_DNR
;
6801 case NVME_VIRT_MNGMT_ACTION_SEC_ASSIGN
:
6802 return nvme_assign_virt_res_to_sec(n
, req
, cntlid
, rt
, nr
);
6803 case NVME_VIRT_MNGMT_ACTION_PRM_ALLOC
:
6804 return nvme_assign_virt_res_to_prim(n
, req
, cntlid
, rt
, nr
);
6805 case NVME_VIRT_MNGMT_ACTION_SEC_ONLINE
:
6806 return nvme_virt_set_state(n
, cntlid
, true);
6807 case NVME_VIRT_MNGMT_ACTION_SEC_OFFLINE
:
6808 return nvme_virt_set_state(n
, cntlid
, false);
6810 return NVME_INVALID_FIELD
| NVME_DNR
;
6814 static uint16_t nvme_dbbuf_config(NvmeCtrl
*n
, const NvmeRequest
*req
)
6816 PCIDevice
*pci
= PCI_DEVICE(n
);
6817 uint64_t dbs_addr
= le64_to_cpu(req
->cmd
.dptr
.prp1
);
6818 uint64_t eis_addr
= le64_to_cpu(req
->cmd
.dptr
.prp2
);
6821 /* Address should be page aligned */
6822 if (dbs_addr
& (n
->page_size
- 1) || eis_addr
& (n
->page_size
- 1)) {
6823 return NVME_INVALID_FIELD
| NVME_DNR
;
6826 /* Save shadow buffer base addr for use during queue creation */
6827 n
->dbbuf_dbs
= dbs_addr
;
6828 n
->dbbuf_eis
= eis_addr
;
6829 n
->dbbuf_enabled
= true;
6831 for (i
= 0; i
< n
->params
.max_ioqpairs
+ 1; i
++) {
6832 NvmeSQueue
*sq
= n
->sq
[i
];
6833 NvmeCQueue
*cq
= n
->cq
[i
];
6837 * CAP.DSTRD is 0, so offset of ith sq db_addr is (i<<3)
6838 * nvme_process_db() uses this hard-coded way to calculate
6839 * doorbell offsets. Be consistent with that here.
6841 sq
->db_addr
= dbs_addr
+ (i
<< 3);
6842 sq
->ei_addr
= eis_addr
+ (i
<< 3);
6843 stl_le_pci_dma(pci
, sq
->db_addr
, sq
->tail
, MEMTXATTRS_UNSPECIFIED
);
6845 if (n
->params
.ioeventfd
&& sq
->sqid
!= 0) {
6846 if (!nvme_init_sq_ioeventfd(sq
)) {
6847 sq
->ioeventfd_enabled
= true;
6853 /* CAP.DSTRD is 0, so offset of ith cq db_addr is (i<<3)+(1<<2) */
6854 cq
->db_addr
= dbs_addr
+ (i
<< 3) + (1 << 2);
6855 cq
->ei_addr
= eis_addr
+ (i
<< 3) + (1 << 2);
6856 stl_le_pci_dma(pci
, cq
->db_addr
, cq
->head
, MEMTXATTRS_UNSPECIFIED
);
6858 if (n
->params
.ioeventfd
&& cq
->cqid
!= 0) {
6859 if (!nvme_init_cq_ioeventfd(cq
)) {
6860 cq
->ioeventfd_enabled
= true;
6866 trace_pci_nvme_dbbuf_config(dbs_addr
, eis_addr
);
6868 return NVME_SUCCESS
;
6871 static uint16_t nvme_directive_send(NvmeCtrl
*n
, NvmeRequest
*req
)
6873 return NVME_INVALID_FIELD
| NVME_DNR
;
6876 static uint16_t nvme_directive_receive(NvmeCtrl
*n
, NvmeRequest
*req
)
6879 uint32_t dw10
= le32_to_cpu(req
->cmd
.cdw10
);
6880 uint32_t dw11
= le32_to_cpu(req
->cmd
.cdw11
);
6881 uint32_t nsid
= le32_to_cpu(req
->cmd
.nsid
);
6882 uint8_t doper
, dtype
;
6883 uint32_t numd
, trans_len
;
6884 NvmeDirectiveIdentify id
= {
6885 .supported
= 1 << NVME_DIRECTIVE_IDENTIFY
,
6886 .enabled
= 1 << NVME_DIRECTIVE_IDENTIFY
,
6890 doper
= dw11
& 0xff;
6891 dtype
= (dw11
>> 8) & 0xff;
6893 trans_len
= MIN(sizeof(NvmeDirectiveIdentify
), numd
<< 2);
6895 if (nsid
== NVME_NSID_BROADCAST
|| dtype
!= NVME_DIRECTIVE_IDENTIFY
||
6896 doper
!= NVME_DIRECTIVE_RETURN_PARAMS
) {
6897 return NVME_INVALID_FIELD
| NVME_DNR
;
6900 ns
= nvme_ns(n
, nsid
);
6902 return NVME_INVALID_FIELD
| NVME_DNR
;
6906 case NVME_DIRECTIVE_IDENTIFY
:
6908 case NVME_DIRECTIVE_RETURN_PARAMS
:
6909 if (ns
->endgrp
&& ns
->endgrp
->fdp
.enabled
) {
6910 id
.supported
|= 1 << NVME_DIRECTIVE_DATA_PLACEMENT
;
6911 id
.enabled
|= 1 << NVME_DIRECTIVE_DATA_PLACEMENT
;
6912 id
.persistent
|= 1 << NVME_DIRECTIVE_DATA_PLACEMENT
;
6915 return nvme_c2h(n
, (uint8_t *)&id
, trans_len
, req
);
6918 return NVME_INVALID_FIELD
| NVME_DNR
;
6922 return NVME_INVALID_FIELD
;
6926 static uint16_t nvme_admin_cmd(NvmeCtrl
*n
, NvmeRequest
*req
)
6928 trace_pci_nvme_admin_cmd(nvme_cid(req
), nvme_sqid(req
), req
->cmd
.opcode
,
6929 nvme_adm_opc_str(req
->cmd
.opcode
));
6931 if (!(nvme_cse_acs
[req
->cmd
.opcode
] & NVME_CMD_EFF_CSUPP
)) {
6932 trace_pci_nvme_err_invalid_admin_opc(req
->cmd
.opcode
);
6933 return NVME_INVALID_OPCODE
| NVME_DNR
;
6936 /* SGLs shall not be used for Admin commands in NVMe over PCIe */
6937 if (NVME_CMD_FLAGS_PSDT(req
->cmd
.flags
) != NVME_PSDT_PRP
) {
6938 return NVME_INVALID_FIELD
| NVME_DNR
;
6941 if (NVME_CMD_FLAGS_FUSE(req
->cmd
.flags
)) {
6942 return NVME_INVALID_FIELD
;
6945 switch (req
->cmd
.opcode
) {
6946 case NVME_ADM_CMD_DELETE_SQ
:
6947 return nvme_del_sq(n
, req
);
6948 case NVME_ADM_CMD_CREATE_SQ
:
6949 return nvme_create_sq(n
, req
);
6950 case NVME_ADM_CMD_GET_LOG_PAGE
:
6951 return nvme_get_log(n
, req
);
6952 case NVME_ADM_CMD_DELETE_CQ
:
6953 return nvme_del_cq(n
, req
);
6954 case NVME_ADM_CMD_CREATE_CQ
:
6955 return nvme_create_cq(n
, req
);
6956 case NVME_ADM_CMD_IDENTIFY
:
6957 return nvme_identify(n
, req
);
6958 case NVME_ADM_CMD_ABORT
:
6959 return nvme_abort(n
, req
);
6960 case NVME_ADM_CMD_SET_FEATURES
:
6961 return nvme_set_feature(n
, req
);
6962 case NVME_ADM_CMD_GET_FEATURES
:
6963 return nvme_get_feature(n
, req
);
6964 case NVME_ADM_CMD_ASYNC_EV_REQ
:
6965 return nvme_aer(n
, req
);
6966 case NVME_ADM_CMD_NS_ATTACHMENT
:
6967 return nvme_ns_attachment(n
, req
);
6968 case NVME_ADM_CMD_VIRT_MNGMT
:
6969 return nvme_virt_mngmt(n
, req
);
6970 case NVME_ADM_CMD_DBBUF_CONFIG
:
6971 return nvme_dbbuf_config(n
, req
);
6972 case NVME_ADM_CMD_FORMAT_NVM
:
6973 return nvme_format(n
, req
);
6974 case NVME_ADM_CMD_DIRECTIVE_SEND
:
6975 return nvme_directive_send(n
, req
);
6976 case NVME_ADM_CMD_DIRECTIVE_RECV
:
6977 return nvme_directive_receive(n
, req
);
6982 return NVME_INVALID_OPCODE
| NVME_DNR
;
6985 static void nvme_update_sq_eventidx(const NvmeSQueue
*sq
)
6987 trace_pci_nvme_update_sq_eventidx(sq
->sqid
, sq
->tail
);
6989 stl_le_pci_dma(PCI_DEVICE(sq
->ctrl
), sq
->ei_addr
, sq
->tail
,
6990 MEMTXATTRS_UNSPECIFIED
);
6993 static void nvme_update_sq_tail(NvmeSQueue
*sq
)
6995 ldl_le_pci_dma(PCI_DEVICE(sq
->ctrl
), sq
->db_addr
, &sq
->tail
,
6996 MEMTXATTRS_UNSPECIFIED
);
6998 trace_pci_nvme_update_sq_tail(sq
->sqid
, sq
->tail
);
7001 static void nvme_process_sq(void *opaque
)
7003 NvmeSQueue
*sq
= opaque
;
7004 NvmeCtrl
*n
= sq
->ctrl
;
7005 NvmeCQueue
*cq
= n
->cq
[sq
->cqid
];
7012 if (n
->dbbuf_enabled
) {
7013 nvme_update_sq_tail(sq
);
7016 while (!(nvme_sq_empty(sq
) || QTAILQ_EMPTY(&sq
->req_list
))) {
7017 addr
= sq
->dma_addr
+ (sq
->head
<< NVME_SQES
);
7018 if (nvme_addr_read(n
, addr
, (void *)&cmd
, sizeof(cmd
))) {
7019 trace_pci_nvme_err_addr_read(addr
);
7020 trace_pci_nvme_err_cfs();
7021 stl_le_p(&n
->bar
.csts
, NVME_CSTS_FAILED
);
7024 nvme_inc_sq_head(sq
);
7026 req
= QTAILQ_FIRST(&sq
->req_list
);
7027 QTAILQ_REMOVE(&sq
->req_list
, req
, entry
);
7028 QTAILQ_INSERT_TAIL(&sq
->out_req_list
, req
, entry
);
7029 nvme_req_clear(req
);
7030 req
->cqe
.cid
= cmd
.cid
;
7031 memcpy(&req
->cmd
, &cmd
, sizeof(NvmeCmd
));
7033 status
= sq
->sqid
? nvme_io_cmd(n
, req
) :
7034 nvme_admin_cmd(n
, req
);
7035 if (status
!= NVME_NO_COMPLETE
) {
7036 req
->status
= status
;
7037 nvme_enqueue_req_completion(cq
, req
);
7040 if (n
->dbbuf_enabled
) {
7041 nvme_update_sq_eventidx(sq
);
7042 nvme_update_sq_tail(sq
);
7047 static void nvme_update_msixcap_ts(PCIDevice
*pci_dev
, uint32_t table_size
)
7051 if (!msix_present(pci_dev
)) {
7055 assert(table_size
> 0 && table_size
<= pci_dev
->msix_entries_nr
);
7057 config
= pci_dev
->config
+ pci_dev
->msix_cap
;
7058 pci_set_word_by_mask(config
+ PCI_MSIX_FLAGS
, PCI_MSIX_FLAGS_QSIZE
,
7062 static void nvme_activate_virt_res(NvmeCtrl
*n
)
7064 PCIDevice
*pci_dev
= PCI_DEVICE(n
);
7065 NvmePriCtrlCap
*cap
= &n
->pri_ctrl_cap
;
7066 NvmeSecCtrlEntry
*sctrl
;
7068 /* -1 to account for the admin queue */
7069 if (pci_is_vf(pci_dev
)) {
7070 sctrl
= nvme_sctrl(n
);
7071 cap
->vqprt
= sctrl
->nvq
;
7072 cap
->viprt
= sctrl
->nvi
;
7073 n
->conf_ioqpairs
= sctrl
->nvq
? le16_to_cpu(sctrl
->nvq
) - 1 : 0;
7074 n
->conf_msix_qsize
= sctrl
->nvi
? le16_to_cpu(sctrl
->nvi
) : 1;
7076 cap
->vqrfap
= n
->next_pri_ctrl_cap
.vqrfap
;
7077 cap
->virfap
= n
->next_pri_ctrl_cap
.virfap
;
7078 n
->conf_ioqpairs
= le16_to_cpu(cap
->vqprt
) +
7079 le16_to_cpu(cap
->vqrfap
) - 1;
7080 n
->conf_msix_qsize
= le16_to_cpu(cap
->viprt
) +
7081 le16_to_cpu(cap
->virfap
);
7085 static void nvme_ctrl_reset(NvmeCtrl
*n
, NvmeResetType rst
)
7087 PCIDevice
*pci_dev
= PCI_DEVICE(n
);
7088 NvmeSecCtrlEntry
*sctrl
;
7092 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
7101 for (i
= 0; i
< n
->params
.max_ioqpairs
+ 1; i
++) {
7102 if (n
->sq
[i
] != NULL
) {
7103 nvme_free_sq(n
->sq
[i
], n
);
7106 for (i
= 0; i
< n
->params
.max_ioqpairs
+ 1; i
++) {
7107 if (n
->cq
[i
] != NULL
) {
7108 nvme_free_cq(n
->cq
[i
], n
);
7112 while (!QTAILQ_EMPTY(&n
->aer_queue
)) {
7113 NvmeAsyncEvent
*event
= QTAILQ_FIRST(&n
->aer_queue
);
7114 QTAILQ_REMOVE(&n
->aer_queue
, event
, entry
);
7118 if (n
->params
.sriov_max_vfs
) {
7119 if (!pci_is_vf(pci_dev
)) {
7120 for (i
= 0; i
< n
->sec_ctrl_list
.numcntl
; i
++) {
7121 sctrl
= &n
->sec_ctrl_list
.sec
[i
];
7122 nvme_virt_set_state(n
, le16_to_cpu(sctrl
->scid
), false);
7125 if (rst
!= NVME_RESET_CONTROLLER
) {
7126 pcie_sriov_pf_disable_vfs(pci_dev
);
7130 if (rst
!= NVME_RESET_CONTROLLER
) {
7131 nvme_activate_virt_res(n
);
7137 n
->outstanding_aers
= 0;
7138 n
->qs_created
= false;
7140 nvme_update_msixcap_ts(pci_dev
, n
->conf_msix_qsize
);
7142 if (pci_is_vf(pci_dev
)) {
7143 sctrl
= nvme_sctrl(n
);
7145 stl_le_p(&n
->bar
.csts
, sctrl
->scs
? 0 : NVME_CSTS_FAILED
);
7147 stl_le_p(&n
->bar
.csts
, 0);
7150 stl_le_p(&n
->bar
.intms
, 0);
7151 stl_le_p(&n
->bar
.intmc
, 0);
7152 stl_le_p(&n
->bar
.cc
, 0);
7156 n
->dbbuf_enabled
= false;
7159 static void nvme_ctrl_shutdown(NvmeCtrl
*n
)
7165 memory_region_msync(&n
->pmr
.dev
->mr
, 0, n
->pmr
.dev
->size
);
7168 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
7174 nvme_ns_shutdown(ns
);
7178 static void nvme_select_iocs(NvmeCtrl
*n
)
7183 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
7189 nvme_select_iocs_ns(n
, ns
);
7193 static int nvme_start_ctrl(NvmeCtrl
*n
)
7195 uint64_t cap
= ldq_le_p(&n
->bar
.cap
);
7196 uint32_t cc
= ldl_le_p(&n
->bar
.cc
);
7197 uint32_t aqa
= ldl_le_p(&n
->bar
.aqa
);
7198 uint64_t asq
= ldq_le_p(&n
->bar
.asq
);
7199 uint64_t acq
= ldq_le_p(&n
->bar
.acq
);
7200 uint32_t page_bits
= NVME_CC_MPS(cc
) + 12;
7201 uint32_t page_size
= 1 << page_bits
;
7202 NvmeSecCtrlEntry
*sctrl
= nvme_sctrl(n
);
7204 if (pci_is_vf(PCI_DEVICE(n
)) && !sctrl
->scs
) {
7205 trace_pci_nvme_err_startfail_virt_state(le16_to_cpu(sctrl
->nvi
),
7206 le16_to_cpu(sctrl
->nvq
));
7209 if (unlikely(n
->cq
[0])) {
7210 trace_pci_nvme_err_startfail_cq();
7213 if (unlikely(n
->sq
[0])) {
7214 trace_pci_nvme_err_startfail_sq();
7217 if (unlikely(asq
& (page_size
- 1))) {
7218 trace_pci_nvme_err_startfail_asq_misaligned(asq
);
7221 if (unlikely(acq
& (page_size
- 1))) {
7222 trace_pci_nvme_err_startfail_acq_misaligned(acq
);
7225 if (unlikely(!(NVME_CAP_CSS(cap
) & (1 << NVME_CC_CSS(cc
))))) {
7226 trace_pci_nvme_err_startfail_css(NVME_CC_CSS(cc
));
7229 if (unlikely(NVME_CC_MPS(cc
) < NVME_CAP_MPSMIN(cap
))) {
7230 trace_pci_nvme_err_startfail_page_too_small(
7232 NVME_CAP_MPSMIN(cap
));
7235 if (unlikely(NVME_CC_MPS(cc
) >
7236 NVME_CAP_MPSMAX(cap
))) {
7237 trace_pci_nvme_err_startfail_page_too_large(
7239 NVME_CAP_MPSMAX(cap
));
7242 if (unlikely(!NVME_AQA_ASQS(aqa
))) {
7243 trace_pci_nvme_err_startfail_asqent_sz_zero();
7246 if (unlikely(!NVME_AQA_ACQS(aqa
))) {
7247 trace_pci_nvme_err_startfail_acqent_sz_zero();
7251 n
->page_bits
= page_bits
;
7252 n
->page_size
= page_size
;
7253 n
->max_prp_ents
= n
->page_size
/ sizeof(uint64_t);
7254 nvme_init_cq(&n
->admin_cq
, n
, acq
, 0, 0, NVME_AQA_ACQS(aqa
) + 1, 1);
7255 nvme_init_sq(&n
->admin_sq
, n
, asq
, 0, 0, NVME_AQA_ASQS(aqa
) + 1);
7257 nvme_set_timestamp(n
, 0ULL);
7259 nvme_select_iocs(n
);
7264 static void nvme_cmb_enable_regs(NvmeCtrl
*n
)
7266 uint32_t cmbloc
= ldl_le_p(&n
->bar
.cmbloc
);
7267 uint32_t cmbsz
= ldl_le_p(&n
->bar
.cmbsz
);
7269 NVME_CMBLOC_SET_CDPCILS(cmbloc
, 1);
7270 NVME_CMBLOC_SET_CDPMLS(cmbloc
, 1);
7271 NVME_CMBLOC_SET_BIR(cmbloc
, NVME_CMB_BIR
);
7272 stl_le_p(&n
->bar
.cmbloc
, cmbloc
);
7274 NVME_CMBSZ_SET_SQS(cmbsz
, 1);
7275 NVME_CMBSZ_SET_CQS(cmbsz
, 0);
7276 NVME_CMBSZ_SET_LISTS(cmbsz
, 1);
7277 NVME_CMBSZ_SET_RDS(cmbsz
, 1);
7278 NVME_CMBSZ_SET_WDS(cmbsz
, 1);
7279 NVME_CMBSZ_SET_SZU(cmbsz
, 2); /* MBs */
7280 NVME_CMBSZ_SET_SZ(cmbsz
, n
->params
.cmb_size_mb
);
7281 stl_le_p(&n
->bar
.cmbsz
, cmbsz
);
7284 static void nvme_write_bar(NvmeCtrl
*n
, hwaddr offset
, uint64_t data
,
7287 PCIDevice
*pci
= PCI_DEVICE(n
);
7288 uint64_t cap
= ldq_le_p(&n
->bar
.cap
);
7289 uint32_t cc
= ldl_le_p(&n
->bar
.cc
);
7290 uint32_t intms
= ldl_le_p(&n
->bar
.intms
);
7291 uint32_t csts
= ldl_le_p(&n
->bar
.csts
);
7292 uint32_t pmrsts
= ldl_le_p(&n
->bar
.pmrsts
);
7294 if (unlikely(offset
& (sizeof(uint32_t) - 1))) {
7295 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_misaligned32
,
7296 "MMIO write not 32-bit aligned,"
7297 " offset=0x%"PRIx64
"", offset
);
7298 /* should be ignored, fall through for now */
7301 if (unlikely(size
< sizeof(uint32_t))) {
7302 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_toosmall
,
7303 "MMIO write smaller than 32-bits,"
7304 " offset=0x%"PRIx64
", size=%u",
7306 /* should be ignored, fall through for now */
7310 case NVME_REG_INTMS
:
7311 if (unlikely(msix_enabled(pci
))) {
7312 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_intmask_with_msix
,
7313 "undefined access to interrupt mask set"
7314 " when MSI-X is enabled");
7315 /* should be ignored, fall through for now */
7318 stl_le_p(&n
->bar
.intms
, intms
);
7319 n
->bar
.intmc
= n
->bar
.intms
;
7320 trace_pci_nvme_mmio_intm_set(data
& 0xffffffff, intms
);
7323 case NVME_REG_INTMC
:
7324 if (unlikely(msix_enabled(pci
))) {
7325 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_intmask_with_msix
,
7326 "undefined access to interrupt mask clr"
7327 " when MSI-X is enabled");
7328 /* should be ignored, fall through for now */
7331 stl_le_p(&n
->bar
.intms
, intms
);
7332 n
->bar
.intmc
= n
->bar
.intms
;
7333 trace_pci_nvme_mmio_intm_clr(data
& 0xffffffff, intms
);
7337 stl_le_p(&n
->bar
.cc
, data
);
7339 trace_pci_nvme_mmio_cfg(data
& 0xffffffff);
7341 if (NVME_CC_SHN(data
) && !(NVME_CC_SHN(cc
))) {
7342 trace_pci_nvme_mmio_shutdown_set();
7343 nvme_ctrl_shutdown(n
);
7344 csts
&= ~(CSTS_SHST_MASK
<< CSTS_SHST_SHIFT
);
7345 csts
|= NVME_CSTS_SHST_COMPLETE
;
7346 } else if (!NVME_CC_SHN(data
) && NVME_CC_SHN(cc
)) {
7347 trace_pci_nvme_mmio_shutdown_cleared();
7348 csts
&= ~(CSTS_SHST_MASK
<< CSTS_SHST_SHIFT
);
7351 if (NVME_CC_EN(data
) && !NVME_CC_EN(cc
)) {
7352 if (unlikely(nvme_start_ctrl(n
))) {
7353 trace_pci_nvme_err_startfail();
7354 csts
= NVME_CSTS_FAILED
;
7356 trace_pci_nvme_mmio_start_success();
7357 csts
= NVME_CSTS_READY
;
7359 } else if (!NVME_CC_EN(data
) && NVME_CC_EN(cc
)) {
7360 trace_pci_nvme_mmio_stopped();
7361 nvme_ctrl_reset(n
, NVME_RESET_CONTROLLER
);
7366 stl_le_p(&n
->bar
.csts
, csts
);
7370 if (data
& (1 << 4)) {
7371 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_ssreset_w1c_unsupported
,
7372 "attempted to W1C CSTS.NSSRO"
7373 " but CAP.NSSRS is zero (not supported)");
7374 } else if (data
!= 0) {
7375 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_ro_csts
,
7376 "attempted to set a read only bit"
7377 " of controller status");
7381 if (data
== 0x4e564d65) {
7382 trace_pci_nvme_ub_mmiowr_ssreset_unsupported();
7384 /* The spec says that writes of other values have no effect */
7389 stl_le_p(&n
->bar
.aqa
, data
);
7390 trace_pci_nvme_mmio_aqattr(data
& 0xffffffff);
7393 stn_le_p(&n
->bar
.asq
, size
, data
);
7394 trace_pci_nvme_mmio_asqaddr(data
);
7396 case NVME_REG_ASQ
+ 4:
7397 stl_le_p((uint8_t *)&n
->bar
.asq
+ 4, data
);
7398 trace_pci_nvme_mmio_asqaddr_hi(data
, ldq_le_p(&n
->bar
.asq
));
7401 trace_pci_nvme_mmio_acqaddr(data
);
7402 stn_le_p(&n
->bar
.acq
, size
, data
);
7404 case NVME_REG_ACQ
+ 4:
7405 stl_le_p((uint8_t *)&n
->bar
.acq
+ 4, data
);
7406 trace_pci_nvme_mmio_acqaddr_hi(data
, ldq_le_p(&n
->bar
.acq
));
7408 case NVME_REG_CMBLOC
:
7409 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_cmbloc_reserved
,
7410 "invalid write to reserved CMBLOC"
7411 " when CMBSZ is zero, ignored");
7413 case NVME_REG_CMBSZ
:
7414 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_cmbsz_readonly
,
7415 "invalid write to read only CMBSZ, ignored");
7417 case NVME_REG_CMBMSC
:
7418 if (!NVME_CAP_CMBS(cap
)) {
7422 stn_le_p(&n
->bar
.cmbmsc
, size
, data
);
7423 n
->cmb
.cmse
= false;
7425 if (NVME_CMBMSC_CRE(data
)) {
7426 nvme_cmb_enable_regs(n
);
7428 if (NVME_CMBMSC_CMSE(data
)) {
7429 uint64_t cmbmsc
= ldq_le_p(&n
->bar
.cmbmsc
);
7430 hwaddr cba
= NVME_CMBMSC_CBA(cmbmsc
) << CMBMSC_CBA_SHIFT
;
7431 if (cba
+ int128_get64(n
->cmb
.mem
.size
) < cba
) {
7432 uint32_t cmbsts
= ldl_le_p(&n
->bar
.cmbsts
);
7433 NVME_CMBSTS_SET_CBAI(cmbsts
, 1);
7434 stl_le_p(&n
->bar
.cmbsts
, cmbsts
);
7447 case NVME_REG_CMBMSC
+ 4:
7448 stl_le_p((uint8_t *)&n
->bar
.cmbmsc
+ 4, data
);
7451 case NVME_REG_PMRCAP
:
7452 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrcap_readonly
,
7453 "invalid write to PMRCAP register, ignored");
7455 case NVME_REG_PMRCTL
:
7456 if (!NVME_CAP_PMRS(cap
)) {
7460 stl_le_p(&n
->bar
.pmrctl
, data
);
7461 if (NVME_PMRCTL_EN(data
)) {
7462 memory_region_set_enabled(&n
->pmr
.dev
->mr
, true);
7465 memory_region_set_enabled(&n
->pmr
.dev
->mr
, false);
7466 NVME_PMRSTS_SET_NRDY(pmrsts
, 1);
7467 n
->pmr
.cmse
= false;
7469 stl_le_p(&n
->bar
.pmrsts
, pmrsts
);
7471 case NVME_REG_PMRSTS
:
7472 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrsts_readonly
,
7473 "invalid write to PMRSTS register, ignored");
7475 case NVME_REG_PMREBS
:
7476 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrebs_readonly
,
7477 "invalid write to PMREBS register, ignored");
7479 case NVME_REG_PMRSWTP
:
7480 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrswtp_readonly
,
7481 "invalid write to PMRSWTP register, ignored");
7483 case NVME_REG_PMRMSCL
:
7484 if (!NVME_CAP_PMRS(cap
)) {
7488 stl_le_p(&n
->bar
.pmrmscl
, data
);
7489 n
->pmr
.cmse
= false;
7491 if (NVME_PMRMSCL_CMSE(data
)) {
7492 uint64_t pmrmscu
= ldl_le_p(&n
->bar
.pmrmscu
);
7493 hwaddr cba
= pmrmscu
<< 32 |
7494 (NVME_PMRMSCL_CBA(data
) << PMRMSCL_CBA_SHIFT
);
7495 if (cba
+ int128_get64(n
->pmr
.dev
->mr
.size
) < cba
) {
7496 NVME_PMRSTS_SET_CBAI(pmrsts
, 1);
7497 stl_le_p(&n
->bar
.pmrsts
, pmrsts
);
7506 case NVME_REG_PMRMSCU
:
7507 if (!NVME_CAP_PMRS(cap
)) {
7511 stl_le_p(&n
->bar
.pmrmscu
, data
);
7514 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_invalid
,
7515 "invalid MMIO write,"
7516 " offset=0x%"PRIx64
", data=%"PRIx64
"",
7522 static uint64_t nvme_mmio_read(void *opaque
, hwaddr addr
, unsigned size
)
7524 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
7525 uint8_t *ptr
= (uint8_t *)&n
->bar
;
7527 trace_pci_nvme_mmio_read(addr
, size
);
7529 if (unlikely(addr
& (sizeof(uint32_t) - 1))) {
7530 NVME_GUEST_ERR(pci_nvme_ub_mmiord_misaligned32
,
7531 "MMIO read not 32-bit aligned,"
7532 " offset=0x%"PRIx64
"", addr
);
7533 /* should RAZ, fall through for now */
7534 } else if (unlikely(size
< sizeof(uint32_t))) {
7535 NVME_GUEST_ERR(pci_nvme_ub_mmiord_toosmall
,
7536 "MMIO read smaller than 32-bits,"
7537 " offset=0x%"PRIx64
"", addr
);
7538 /* should RAZ, fall through for now */
7541 if (addr
> sizeof(n
->bar
) - size
) {
7542 NVME_GUEST_ERR(pci_nvme_ub_mmiord_invalid_ofs
,
7543 "MMIO read beyond last register,"
7544 " offset=0x%"PRIx64
", returning 0", addr
);
7549 if (pci_is_vf(PCI_DEVICE(n
)) && !nvme_sctrl(n
)->scs
&&
7550 addr
!= NVME_REG_CSTS
) {
7551 trace_pci_nvme_err_ignored_mmio_vf_offline(addr
, size
);
7556 * When PMRWBM bit 1 is set then read from
7557 * from PMRSTS should ensure prior writes
7558 * made it to persistent media
7560 if (addr
== NVME_REG_PMRSTS
&&
7561 (NVME_PMRCAP_PMRWBM(ldl_le_p(&n
->bar
.pmrcap
)) & 0x02)) {
7562 memory_region_msync(&n
->pmr
.dev
->mr
, 0, n
->pmr
.dev
->size
);
7565 return ldn_le_p(ptr
+ addr
, size
);
7568 static void nvme_process_db(NvmeCtrl
*n
, hwaddr addr
, int val
)
7570 PCIDevice
*pci
= PCI_DEVICE(n
);
7573 if (unlikely(addr
& ((1 << 2) - 1))) {
7574 NVME_GUEST_ERR(pci_nvme_ub_db_wr_misaligned
,
7575 "doorbell write not 32-bit aligned,"
7576 " offset=0x%"PRIx64
", ignoring", addr
);
7580 if (((addr
- 0x1000) >> 2) & 1) {
7581 /* Completion queue doorbell write */
7583 uint16_t new_head
= val
& 0xffff;
7587 qid
= (addr
- (0x1000 + (1 << 2))) >> 3;
7588 if (unlikely(nvme_check_cqid(n
, qid
))) {
7589 NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_cq
,
7590 "completion queue doorbell write"
7591 " for nonexistent queue,"
7592 " sqid=%"PRIu32
", ignoring", qid
);
7595 * NVM Express v1.3d, Section 4.1 state: "If host software writes
7596 * an invalid value to the Submission Queue Tail Doorbell or
7597 * Completion Queue Head Doorbell regiter and an Asynchronous Event
7598 * Request command is outstanding, then an asynchronous event is
7599 * posted to the Admin Completion Queue with a status code of
7600 * Invalid Doorbell Write Value."
7602 * Also note that the spec includes the "Invalid Doorbell Register"
7603 * status code, but nowhere does it specify when to use it.
7604 * However, it seems reasonable to use it here in a similar
7607 if (n
->outstanding_aers
) {
7608 nvme_enqueue_event(n
, NVME_AER_TYPE_ERROR
,
7609 NVME_AER_INFO_ERR_INVALID_DB_REGISTER
,
7610 NVME_LOG_ERROR_INFO
);
7617 if (unlikely(new_head
>= cq
->size
)) {
7618 NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_cqhead
,
7619 "completion queue doorbell write value"
7620 " beyond queue size, sqid=%"PRIu32
","
7621 " new_head=%"PRIu16
", ignoring",
7624 if (n
->outstanding_aers
) {
7625 nvme_enqueue_event(n
, NVME_AER_TYPE_ERROR
,
7626 NVME_AER_INFO_ERR_INVALID_DB_VALUE
,
7627 NVME_LOG_ERROR_INFO
);
7633 trace_pci_nvme_mmio_doorbell_cq(cq
->cqid
, new_head
);
7635 start_sqs
= nvme_cq_full(cq
) ? 1 : 0;
7636 cq
->head
= new_head
;
7637 if (!qid
&& n
->dbbuf_enabled
) {
7638 stl_le_pci_dma(pci
, cq
->db_addr
, cq
->head
, MEMTXATTRS_UNSPECIFIED
);
7642 QTAILQ_FOREACH(sq
, &cq
->sq_list
, entry
) {
7643 qemu_bh_schedule(sq
->bh
);
7645 qemu_bh_schedule(cq
->bh
);
7648 if (cq
->tail
== cq
->head
) {
7649 if (cq
->irq_enabled
) {
7653 nvme_irq_deassert(n
, cq
);
7656 /* Submission queue doorbell write */
7658 uint16_t new_tail
= val
& 0xffff;
7661 qid
= (addr
- 0x1000) >> 3;
7662 if (unlikely(nvme_check_sqid(n
, qid
))) {
7663 NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_sq
,
7664 "submission queue doorbell write"
7665 " for nonexistent queue,"
7666 " sqid=%"PRIu32
", ignoring", qid
);
7668 if (n
->outstanding_aers
) {
7669 nvme_enqueue_event(n
, NVME_AER_TYPE_ERROR
,
7670 NVME_AER_INFO_ERR_INVALID_DB_REGISTER
,
7671 NVME_LOG_ERROR_INFO
);
7678 if (unlikely(new_tail
>= sq
->size
)) {
7679 NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_sqtail
,
7680 "submission queue doorbell write value"
7681 " beyond queue size, sqid=%"PRIu32
","
7682 " new_tail=%"PRIu16
", ignoring",
7685 if (n
->outstanding_aers
) {
7686 nvme_enqueue_event(n
, NVME_AER_TYPE_ERROR
,
7687 NVME_AER_INFO_ERR_INVALID_DB_VALUE
,
7688 NVME_LOG_ERROR_INFO
);
7694 trace_pci_nvme_mmio_doorbell_sq(sq
->sqid
, new_tail
);
7696 sq
->tail
= new_tail
;
7697 if (!qid
&& n
->dbbuf_enabled
) {
7699 * The spec states "the host shall also update the controller's
7700 * corresponding doorbell property to match the value of that entry
7701 * in the Shadow Doorbell buffer."
7703 * Since this context is currently a VM trap, we can safely enforce
7704 * the requirement from the device side in case the host is
7707 * Note, we shouldn't have to do this, but various drivers
7708 * including ones that run on Linux, are not updating Admin Queues,
7709 * so we can't trust reading it for an appropriate sq tail.
7711 stl_le_pci_dma(pci
, sq
->db_addr
, sq
->tail
, MEMTXATTRS_UNSPECIFIED
);
7714 qemu_bh_schedule(sq
->bh
);
7718 static void nvme_mmio_write(void *opaque
, hwaddr addr
, uint64_t data
,
7721 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
7723 trace_pci_nvme_mmio_write(addr
, data
, size
);
7725 if (pci_is_vf(PCI_DEVICE(n
)) && !nvme_sctrl(n
)->scs
&&
7726 addr
!= NVME_REG_CSTS
) {
7727 trace_pci_nvme_err_ignored_mmio_vf_offline(addr
, size
);
7731 if (addr
< sizeof(n
->bar
)) {
7732 nvme_write_bar(n
, addr
, data
, size
);
7734 nvme_process_db(n
, addr
, data
);
7738 static const MemoryRegionOps nvme_mmio_ops
= {
7739 .read
= nvme_mmio_read
,
7740 .write
= nvme_mmio_write
,
7741 .endianness
= DEVICE_LITTLE_ENDIAN
,
7743 .min_access_size
= 2,
7744 .max_access_size
= 8,
7748 static void nvme_cmb_write(void *opaque
, hwaddr addr
, uint64_t data
,
7751 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
7752 stn_le_p(&n
->cmb
.buf
[addr
], size
, data
);
7755 static uint64_t nvme_cmb_read(void *opaque
, hwaddr addr
, unsigned size
)
7757 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
7758 return ldn_le_p(&n
->cmb
.buf
[addr
], size
);
7761 static const MemoryRegionOps nvme_cmb_ops
= {
7762 .read
= nvme_cmb_read
,
7763 .write
= nvme_cmb_write
,
7764 .endianness
= DEVICE_LITTLE_ENDIAN
,
7766 .min_access_size
= 1,
7767 .max_access_size
= 8,
7771 static bool nvme_check_params(NvmeCtrl
*n
, Error
**errp
)
7773 NvmeParams
*params
= &n
->params
;
7775 if (params
->num_queues
) {
7776 warn_report("num_queues is deprecated; please use max_ioqpairs "
7779 params
->max_ioqpairs
= params
->num_queues
- 1;
7782 if (n
->namespace.blkconf
.blk
&& n
->subsys
) {
7783 error_setg(errp
, "subsystem support is unavailable with legacy "
7784 "namespace ('drive' property)");
7788 if (params
->max_ioqpairs
< 1 ||
7789 params
->max_ioqpairs
> NVME_MAX_IOQPAIRS
) {
7790 error_setg(errp
, "max_ioqpairs must be between 1 and %d",
7795 if (params
->msix_qsize
< 1 ||
7796 params
->msix_qsize
> PCI_MSIX_FLAGS_QSIZE
+ 1) {
7797 error_setg(errp
, "msix_qsize must be between 1 and %d",
7798 PCI_MSIX_FLAGS_QSIZE
+ 1);
7802 if (!params
->serial
) {
7803 error_setg(errp
, "serial property not set");
7808 if (host_memory_backend_is_mapped(n
->pmr
.dev
)) {
7809 error_setg(errp
, "can't use already busy memdev: %s",
7810 object_get_canonical_path_component(OBJECT(n
->pmr
.dev
)));
7814 if (!is_power_of_2(n
->pmr
.dev
->size
)) {
7815 error_setg(errp
, "pmr backend size needs to be power of 2 in size");
7819 host_memory_backend_set_mapped(n
->pmr
.dev
, true);
7822 if (n
->params
.zasl
> n
->params
.mdts
) {
7823 error_setg(errp
, "zoned.zasl (Zone Append Size Limit) must be less "
7824 "than or equal to mdts (Maximum Data Transfer Size)");
7828 if (!n
->params
.vsl
) {
7829 error_setg(errp
, "vsl must be non-zero");
7833 if (params
->sriov_max_vfs
) {
7835 error_setg(errp
, "subsystem is required for the use of SR-IOV");
7839 if (params
->sriov_max_vfs
> NVME_MAX_VFS
) {
7840 error_setg(errp
, "sriov_max_vfs must be between 0 and %d",
7845 if (params
->cmb_size_mb
) {
7846 error_setg(errp
, "CMB is not supported with SR-IOV");
7851 error_setg(errp
, "PMR is not supported with SR-IOV");
7855 if (!params
->sriov_vq_flexible
|| !params
->sriov_vi_flexible
) {
7856 error_setg(errp
, "both sriov_vq_flexible and sriov_vi_flexible"
7857 " must be set for the use of SR-IOV");
7861 if (params
->sriov_vq_flexible
< params
->sriov_max_vfs
* 2) {
7862 error_setg(errp
, "sriov_vq_flexible must be greater than or equal"
7863 " to %d (sriov_max_vfs * 2)", params
->sriov_max_vfs
* 2);
7867 if (params
->max_ioqpairs
< params
->sriov_vq_flexible
+ 2) {
7868 error_setg(errp
, "(max_ioqpairs - sriov_vq_flexible) must be"
7869 " greater than or equal to 2");
7873 if (params
->sriov_vi_flexible
< params
->sriov_max_vfs
) {
7874 error_setg(errp
, "sriov_vi_flexible must be greater than or equal"
7875 " to %d (sriov_max_vfs)", params
->sriov_max_vfs
);
7879 if (params
->msix_qsize
< params
->sriov_vi_flexible
+ 1) {
7880 error_setg(errp
, "(msix_qsize - sriov_vi_flexible) must be"
7881 " greater than or equal to 1");
7885 if (params
->sriov_max_vi_per_vf
&&
7886 (params
->sriov_max_vi_per_vf
- 1) % NVME_VF_RES_GRANULARITY
) {
7887 error_setg(errp
, "sriov_max_vi_per_vf must meet:"
7888 " (sriov_max_vi_per_vf - 1) %% %d == 0 and"
7889 " sriov_max_vi_per_vf >= 1", NVME_VF_RES_GRANULARITY
);
7893 if (params
->sriov_max_vq_per_vf
&&
7894 (params
->sriov_max_vq_per_vf
< 2 ||
7895 (params
->sriov_max_vq_per_vf
- 1) % NVME_VF_RES_GRANULARITY
)) {
7896 error_setg(errp
, "sriov_max_vq_per_vf must meet:"
7897 " (sriov_max_vq_per_vf - 1) %% %d == 0 and"
7898 " sriov_max_vq_per_vf >= 2", NVME_VF_RES_GRANULARITY
);
7906 static void nvme_init_state(NvmeCtrl
*n
)
7908 NvmePriCtrlCap
*cap
= &n
->pri_ctrl_cap
;
7909 NvmeSecCtrlList
*list
= &n
->sec_ctrl_list
;
7910 NvmeSecCtrlEntry
*sctrl
;
7911 PCIDevice
*pci
= PCI_DEVICE(n
);
7915 if (pci_is_vf(pci
)) {
7916 sctrl
= nvme_sctrl(n
);
7918 n
->conf_ioqpairs
= sctrl
->nvq
? le16_to_cpu(sctrl
->nvq
) - 1 : 0;
7919 n
->conf_msix_qsize
= sctrl
->nvi
? le16_to_cpu(sctrl
->nvi
) : 1;
7921 max_vfs
= n
->params
.sriov_max_vfs
;
7922 n
->conf_ioqpairs
= n
->params
.max_ioqpairs
;
7923 n
->conf_msix_qsize
= n
->params
.msix_qsize
;
7926 n
->sq
= g_new0(NvmeSQueue
*, n
->params
.max_ioqpairs
+ 1);
7927 n
->cq
= g_new0(NvmeCQueue
*, n
->params
.max_ioqpairs
+ 1);
7928 n
->temperature
= NVME_TEMPERATURE
;
7929 n
->features
.temp_thresh_hi
= NVME_TEMPERATURE_WARNING
;
7930 n
->starttime_ms
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
);
7931 n
->aer_reqs
= g_new0(NvmeRequest
*, n
->params
.aerl
+ 1);
7932 QTAILQ_INIT(&n
->aer_queue
);
7934 list
->numcntl
= cpu_to_le16(max_vfs
);
7935 for (i
= 0; i
< max_vfs
; i
++) {
7936 sctrl
= &list
->sec
[i
];
7937 sctrl
->pcid
= cpu_to_le16(n
->cntlid
);
7938 sctrl
->vfn
= cpu_to_le16(i
+ 1);
7941 cap
->cntlid
= cpu_to_le16(n
->cntlid
);
7942 cap
->crt
= NVME_CRT_VQ
| NVME_CRT_VI
;
7944 if (pci_is_vf(pci
)) {
7945 cap
->vqprt
= cpu_to_le16(1 + n
->conf_ioqpairs
);
7947 cap
->vqprt
= cpu_to_le16(1 + n
->params
.max_ioqpairs
-
7948 n
->params
.sriov_vq_flexible
);
7949 cap
->vqfrt
= cpu_to_le32(n
->params
.sriov_vq_flexible
);
7950 cap
->vqrfap
= cap
->vqfrt
;
7951 cap
->vqgran
= cpu_to_le16(NVME_VF_RES_GRANULARITY
);
7952 cap
->vqfrsm
= n
->params
.sriov_max_vq_per_vf
?
7953 cpu_to_le16(n
->params
.sriov_max_vq_per_vf
) :
7954 cap
->vqfrt
/ MAX(max_vfs
, 1);
7957 if (pci_is_vf(pci
)) {
7958 cap
->viprt
= cpu_to_le16(n
->conf_msix_qsize
);
7960 cap
->viprt
= cpu_to_le16(n
->params
.msix_qsize
-
7961 n
->params
.sriov_vi_flexible
);
7962 cap
->vifrt
= cpu_to_le32(n
->params
.sriov_vi_flexible
);
7963 cap
->virfap
= cap
->vifrt
;
7964 cap
->vigran
= cpu_to_le16(NVME_VF_RES_GRANULARITY
);
7965 cap
->vifrsm
= n
->params
.sriov_max_vi_per_vf
?
7966 cpu_to_le16(n
->params
.sriov_max_vi_per_vf
) :
7967 cap
->vifrt
/ MAX(max_vfs
, 1);
7971 static void nvme_init_cmb(NvmeCtrl
*n
, PCIDevice
*pci_dev
)
7973 uint64_t cmb_size
= n
->params
.cmb_size_mb
* MiB
;
7974 uint64_t cap
= ldq_le_p(&n
->bar
.cap
);
7976 n
->cmb
.buf
= g_malloc0(cmb_size
);
7977 memory_region_init_io(&n
->cmb
.mem
, OBJECT(n
), &nvme_cmb_ops
, n
,
7978 "nvme-cmb", cmb_size
);
7979 pci_register_bar(pci_dev
, NVME_CMB_BIR
,
7980 PCI_BASE_ADDRESS_SPACE_MEMORY
|
7981 PCI_BASE_ADDRESS_MEM_TYPE_64
|
7982 PCI_BASE_ADDRESS_MEM_PREFETCH
, &n
->cmb
.mem
);
7984 NVME_CAP_SET_CMBS(cap
, 1);
7985 stq_le_p(&n
->bar
.cap
, cap
);
7987 if (n
->params
.legacy_cmb
) {
7988 nvme_cmb_enable_regs(n
);
7993 static void nvme_init_pmr(NvmeCtrl
*n
, PCIDevice
*pci_dev
)
7995 uint32_t pmrcap
= ldl_le_p(&n
->bar
.pmrcap
);
7997 NVME_PMRCAP_SET_RDS(pmrcap
, 1);
7998 NVME_PMRCAP_SET_WDS(pmrcap
, 1);
7999 NVME_PMRCAP_SET_BIR(pmrcap
, NVME_PMR_BIR
);
8000 /* Turn on bit 1 support */
8001 NVME_PMRCAP_SET_PMRWBM(pmrcap
, 0x02);
8002 NVME_PMRCAP_SET_CMSS(pmrcap
, 1);
8003 stl_le_p(&n
->bar
.pmrcap
, pmrcap
);
8005 pci_register_bar(pci_dev
, NVME_PMR_BIR
,
8006 PCI_BASE_ADDRESS_SPACE_MEMORY
|
8007 PCI_BASE_ADDRESS_MEM_TYPE_64
|
8008 PCI_BASE_ADDRESS_MEM_PREFETCH
, &n
->pmr
.dev
->mr
);
8010 memory_region_set_enabled(&n
->pmr
.dev
->mr
, false);
8013 static uint64_t nvme_bar_size(unsigned total_queues
, unsigned total_irqs
,
8014 unsigned *msix_table_offset
,
8015 unsigned *msix_pba_offset
)
8017 uint64_t bar_size
, msix_table_size
, msix_pba_size
;
8019 bar_size
= sizeof(NvmeBar
) + 2 * total_queues
* NVME_DB_SIZE
;
8020 bar_size
= QEMU_ALIGN_UP(bar_size
, 4 * KiB
);
8022 if (msix_table_offset
) {
8023 *msix_table_offset
= bar_size
;
8026 msix_table_size
= PCI_MSIX_ENTRY_SIZE
* total_irqs
;
8027 bar_size
+= msix_table_size
;
8028 bar_size
= QEMU_ALIGN_UP(bar_size
, 4 * KiB
);
8030 if (msix_pba_offset
) {
8031 *msix_pba_offset
= bar_size
;
8034 msix_pba_size
= QEMU_ALIGN_UP(total_irqs
, 64) / 8;
8035 bar_size
+= msix_pba_size
;
8037 bar_size
= pow2ceil(bar_size
);
8041 static void nvme_init_sriov(NvmeCtrl
*n
, PCIDevice
*pci_dev
, uint16_t offset
)
8043 uint16_t vf_dev_id
= n
->params
.use_intel_id
?
8044 PCI_DEVICE_ID_INTEL_NVME
: PCI_DEVICE_ID_REDHAT_NVME
;
8045 NvmePriCtrlCap
*cap
= &n
->pri_ctrl_cap
;
8046 uint64_t bar_size
= nvme_bar_size(le16_to_cpu(cap
->vqfrsm
),
8047 le16_to_cpu(cap
->vifrsm
),
8050 pcie_sriov_pf_init(pci_dev
, offset
, "nvme", vf_dev_id
,
8051 n
->params
.sriov_max_vfs
, n
->params
.sriov_max_vfs
,
8052 NVME_VF_OFFSET
, NVME_VF_STRIDE
);
8054 pcie_sriov_pf_init_vf_bar(pci_dev
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
|
8055 PCI_BASE_ADDRESS_MEM_TYPE_64
, bar_size
);
8058 static int nvme_add_pm_capability(PCIDevice
*pci_dev
, uint8_t offset
)
8063 ret
= pci_add_capability(pci_dev
, PCI_CAP_ID_PM
, offset
,
8064 PCI_PM_SIZEOF
, &err
);
8066 error_report_err(err
);
8070 pci_set_word(pci_dev
->config
+ offset
+ PCI_PM_PMC
,
8071 PCI_PM_CAP_VER_1_2
);
8072 pci_set_word(pci_dev
->config
+ offset
+ PCI_PM_CTRL
,
8073 PCI_PM_CTRL_NO_SOFT_RESET
);
8074 pci_set_word(pci_dev
->wmask
+ offset
+ PCI_PM_CTRL
,
8075 PCI_PM_CTRL_STATE_MASK
);
8080 static bool nvme_init_pci(NvmeCtrl
*n
, PCIDevice
*pci_dev
, Error
**errp
)
8083 uint8_t *pci_conf
= pci_dev
->config
;
8085 unsigned msix_table_offset
, msix_pba_offset
;
8088 pci_conf
[PCI_INTERRUPT_PIN
] = 1;
8089 pci_config_set_prog_interface(pci_conf
, 0x2);
8091 if (n
->params
.use_intel_id
) {
8092 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_INTEL
);
8093 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_INTEL_NVME
);
8095 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_REDHAT
);
8096 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_REDHAT_NVME
);
8099 pci_config_set_class(pci_conf
, PCI_CLASS_STORAGE_EXPRESS
);
8100 nvme_add_pm_capability(pci_dev
, 0x60);
8101 pcie_endpoint_cap_init(pci_dev
, 0x80);
8102 pcie_cap_flr_init(pci_dev
);
8103 if (n
->params
.sriov_max_vfs
) {
8104 pcie_ari_init(pci_dev
, 0x100);
8107 /* add one to max_ioqpairs to account for the admin queue pair */
8108 bar_size
= nvme_bar_size(n
->params
.max_ioqpairs
+ 1, n
->params
.msix_qsize
,
8109 &msix_table_offset
, &msix_pba_offset
);
8111 memory_region_init(&n
->bar0
, OBJECT(n
), "nvme-bar0", bar_size
);
8112 memory_region_init_io(&n
->iomem
, OBJECT(n
), &nvme_mmio_ops
, n
, "nvme",
8114 memory_region_add_subregion(&n
->bar0
, 0, &n
->iomem
);
8116 if (pci_is_vf(pci_dev
)) {
8117 pcie_sriov_vf_register_bar(pci_dev
, 0, &n
->bar0
);
8119 pci_register_bar(pci_dev
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
|
8120 PCI_BASE_ADDRESS_MEM_TYPE_64
, &n
->bar0
);
8122 ret
= msix_init(pci_dev
, n
->params
.msix_qsize
,
8123 &n
->bar0
, 0, msix_table_offset
,
8124 &n
->bar0
, 0, msix_pba_offset
, 0, errp
);
8125 if (ret
== -ENOTSUP
) {
8126 /* report that msix is not supported, but do not error out */
8127 warn_report_err(*errp
);
8129 } else if (ret
< 0) {
8130 /* propagate error to caller */
8134 nvme_update_msixcap_ts(pci_dev
, n
->conf_msix_qsize
);
8136 if (n
->params
.cmb_size_mb
) {
8137 nvme_init_cmb(n
, pci_dev
);
8141 nvme_init_pmr(n
, pci_dev
);
8144 if (!pci_is_vf(pci_dev
) && n
->params
.sriov_max_vfs
) {
8145 nvme_init_sriov(n
, pci_dev
, 0x120);
8151 static void nvme_init_subnqn(NvmeCtrl
*n
)
8153 NvmeSubsystem
*subsys
= n
->subsys
;
8154 NvmeIdCtrl
*id
= &n
->id_ctrl
;
8157 snprintf((char *)id
->subnqn
, sizeof(id
->subnqn
),
8158 "nqn.2019-08.org.qemu:%s", n
->params
.serial
);
8160 pstrcpy((char *)id
->subnqn
, sizeof(id
->subnqn
), (char*)subsys
->subnqn
);
8164 static void nvme_init_ctrl(NvmeCtrl
*n
, PCIDevice
*pci_dev
)
8166 NvmeIdCtrl
*id
= &n
->id_ctrl
;
8167 uint8_t *pci_conf
= pci_dev
->config
;
8168 uint64_t cap
= ldq_le_p(&n
->bar
.cap
);
8169 NvmeSecCtrlEntry
*sctrl
= nvme_sctrl(n
);
8172 id
->vid
= cpu_to_le16(pci_get_word(pci_conf
+ PCI_VENDOR_ID
));
8173 id
->ssvid
= cpu_to_le16(pci_get_word(pci_conf
+ PCI_SUBSYSTEM_VENDOR_ID
));
8174 strpadcpy((char *)id
->mn
, sizeof(id
->mn
), "QEMU NVMe Ctrl", ' ');
8175 strpadcpy((char *)id
->fr
, sizeof(id
->fr
), QEMU_VERSION
, ' ');
8176 strpadcpy((char *)id
->sn
, sizeof(id
->sn
), n
->params
.serial
, ' ');
8178 id
->cntlid
= cpu_to_le16(n
->cntlid
);
8180 id
->oaes
= cpu_to_le32(NVME_OAES_NS_ATTR
);
8181 ctratt
= NVME_CTRATT_ELBAS
;
8185 if (n
->params
.use_intel_id
) {
8195 id
->mdts
= n
->params
.mdts
;
8196 id
->ver
= cpu_to_le32(NVME_SPEC_VER
);
8198 cpu_to_le16(NVME_OACS_NS_MGMT
| NVME_OACS_FORMAT
| NVME_OACS_DBBUF
|
8199 NVME_OACS_DIRECTIVES
);
8200 id
->cntrltype
= 0x1;
8203 * Because the controller always completes the Abort command immediately,
8204 * there can never be more than one concurrently executing Abort command,
8205 * so this value is never used for anything. Note that there can easily be
8206 * many Abort commands in the queues, but they are not considered
8207 * "executing" until processed by nvme_abort.
8209 * The specification recommends a value of 3 for Abort Command Limit (four
8210 * concurrently outstanding Abort commands), so lets use that though it is
8214 id
->aerl
= n
->params
.aerl
;
8215 id
->frmw
= (NVME_NUM_FW_SLOTS
<< 1) | NVME_FRMW_SLOT1_RO
;
8216 id
->lpa
= NVME_LPA_NS_SMART
| NVME_LPA_CSE
| NVME_LPA_EXTENDED
;
8218 /* recommended default value (~70 C) */
8219 id
->wctemp
= cpu_to_le16(NVME_TEMPERATURE_WARNING
);
8220 id
->cctemp
= cpu_to_le16(NVME_TEMPERATURE_CRITICAL
);
8222 id
->sqes
= (NVME_SQES
<< 4) | NVME_SQES
;
8223 id
->cqes
= (NVME_CQES
<< 4) | NVME_CQES
;
8224 id
->nn
= cpu_to_le32(NVME_MAX_NAMESPACES
);
8225 id
->oncs
= cpu_to_le16(NVME_ONCS_WRITE_ZEROES
| NVME_ONCS_TIMESTAMP
|
8226 NVME_ONCS_FEATURES
| NVME_ONCS_DSM
|
8227 NVME_ONCS_COMPARE
| NVME_ONCS_COPY
);
8230 * NOTE: If this device ever supports a command set that does NOT use 0x0
8231 * as a Flush-equivalent operation, support for the broadcast NSID in Flush
8232 * should probably be removed.
8234 * See comment in nvme_io_cmd.
8236 id
->vwc
= NVME_VWC_NSID_BROADCAST_SUPPORT
| NVME_VWC_PRESENT
;
8238 id
->ocfs
= cpu_to_le16(NVME_OCFS_COPY_FORMAT_0
| NVME_OCFS_COPY_FORMAT_1
);
8239 id
->sgls
= cpu_to_le32(NVME_CTRL_SGLS_SUPPORT_NO_ALIGN
);
8241 nvme_init_subnqn(n
);
8243 id
->psd
[0].mp
= cpu_to_le16(0x9c4);
8244 id
->psd
[0].enlat
= cpu_to_le32(0x10);
8245 id
->psd
[0].exlat
= cpu_to_le32(0x4);
8248 id
->cmic
|= NVME_CMIC_MULTI_CTRL
;
8249 ctratt
|= NVME_CTRATT_ENDGRPS
;
8251 id
->endgidmax
= cpu_to_le16(0x1);
8253 if (n
->subsys
->endgrp
.fdp
.enabled
) {
8254 ctratt
|= NVME_CTRATT_FDPS
;
8258 id
->ctratt
= cpu_to_le32(ctratt
);
8260 NVME_CAP_SET_MQES(cap
, 0x7ff);
8261 NVME_CAP_SET_CQR(cap
, 1);
8262 NVME_CAP_SET_TO(cap
, 0xf);
8263 NVME_CAP_SET_CSS(cap
, NVME_CAP_CSS_NVM
);
8264 NVME_CAP_SET_CSS(cap
, NVME_CAP_CSS_CSI_SUPP
);
8265 NVME_CAP_SET_CSS(cap
, NVME_CAP_CSS_ADMIN_ONLY
);
8266 NVME_CAP_SET_MPSMAX(cap
, 4);
8267 NVME_CAP_SET_CMBS(cap
, n
->params
.cmb_size_mb
? 1 : 0);
8268 NVME_CAP_SET_PMRS(cap
, n
->pmr
.dev
? 1 : 0);
8269 stq_le_p(&n
->bar
.cap
, cap
);
8271 stl_le_p(&n
->bar
.vs
, NVME_SPEC_VER
);
8272 n
->bar
.intmc
= n
->bar
.intms
= 0;
8274 if (pci_is_vf(pci_dev
) && !sctrl
->scs
) {
8275 stl_le_p(&n
->bar
.csts
, NVME_CSTS_FAILED
);
8279 static int nvme_init_subsys(NvmeCtrl
*n
, Error
**errp
)
8287 cntlid
= nvme_subsys_register_ctrl(n
, errp
);
8297 void nvme_attach_ns(NvmeCtrl
*n
, NvmeNamespace
*ns
)
8299 uint32_t nsid
= ns
->params
.nsid
;
8300 assert(nsid
&& nsid
<= NVME_MAX_NAMESPACES
);
8302 n
->namespaces
[nsid
] = ns
;
8305 n
->dmrsl
= MIN_NON_ZERO(n
->dmrsl
,
8306 BDRV_REQUEST_MAX_BYTES
/ nvme_l2b(ns
, 1));
8309 static void nvme_realize(PCIDevice
*pci_dev
, Error
**errp
)
8311 NvmeCtrl
*n
= NVME(pci_dev
);
8312 DeviceState
*dev
= DEVICE(pci_dev
);
8314 NvmeCtrl
*pn
= NVME(pcie_sriov_get_pf(pci_dev
));
8316 if (pci_is_vf(pci_dev
)) {
8318 * VFs derive settings from the parent. PF's lifespan exceeds
8319 * that of VF's, so it's safe to share params.serial.
8321 memcpy(&n
->params
, &pn
->params
, sizeof(NvmeParams
));
8322 n
->subsys
= pn
->subsys
;
8325 if (!nvme_check_params(n
, errp
)) {
8329 qbus_init(&n
->bus
, sizeof(NvmeBus
), TYPE_NVME_BUS
, dev
, dev
->id
);
8331 if (nvme_init_subsys(n
, errp
)) {
8335 if (!nvme_init_pci(n
, pci_dev
, errp
)) {
8338 nvme_init_ctrl(n
, pci_dev
);
8340 /* setup a namespace if the controller drive property was given */
8341 if (n
->namespace.blkconf
.blk
) {
8343 ns
->params
.nsid
= 1;
8345 if (nvme_ns_setup(ns
, errp
)) {
8349 nvme_attach_ns(n
, ns
);
8353 static void nvme_exit(PCIDevice
*pci_dev
)
8355 NvmeCtrl
*n
= NVME(pci_dev
);
8359 nvme_ctrl_reset(n
, NVME_RESET_FUNCTION
);
8362 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
8369 nvme_subsys_unregister_ctrl(n
->subsys
, n
);
8374 g_free(n
->aer_reqs
);
8376 if (n
->params
.cmb_size_mb
) {
8381 host_memory_backend_set_mapped(n
->pmr
.dev
, false);
8384 if (!pci_is_vf(pci_dev
) && n
->params
.sriov_max_vfs
) {
8385 pcie_sriov_pf_exit(pci_dev
);
8388 msix_uninit(pci_dev
, &n
->bar0
, &n
->bar0
);
8389 memory_region_del_subregion(&n
->bar0
, &n
->iomem
);
8392 static Property nvme_props
[] = {
8393 DEFINE_BLOCK_PROPERTIES(NvmeCtrl
, namespace.blkconf
),
8394 DEFINE_PROP_LINK("pmrdev", NvmeCtrl
, pmr
.dev
, TYPE_MEMORY_BACKEND
,
8395 HostMemoryBackend
*),
8396 DEFINE_PROP_LINK("subsys", NvmeCtrl
, subsys
, TYPE_NVME_SUBSYS
,
8398 DEFINE_PROP_STRING("serial", NvmeCtrl
, params
.serial
),
8399 DEFINE_PROP_UINT32("cmb_size_mb", NvmeCtrl
, params
.cmb_size_mb
, 0),
8400 DEFINE_PROP_UINT32("num_queues", NvmeCtrl
, params
.num_queues
, 0),
8401 DEFINE_PROP_UINT32("max_ioqpairs", NvmeCtrl
, params
.max_ioqpairs
, 64),
8402 DEFINE_PROP_UINT16("msix_qsize", NvmeCtrl
, params
.msix_qsize
, 65),
8403 DEFINE_PROP_UINT8("aerl", NvmeCtrl
, params
.aerl
, 3),
8404 DEFINE_PROP_UINT32("aer_max_queued", NvmeCtrl
, params
.aer_max_queued
, 64),
8405 DEFINE_PROP_UINT8("mdts", NvmeCtrl
, params
.mdts
, 7),
8406 DEFINE_PROP_UINT8("vsl", NvmeCtrl
, params
.vsl
, 7),
8407 DEFINE_PROP_BOOL("use-intel-id", NvmeCtrl
, params
.use_intel_id
, false),
8408 DEFINE_PROP_BOOL("legacy-cmb", NvmeCtrl
, params
.legacy_cmb
, false),
8409 DEFINE_PROP_BOOL("ioeventfd", NvmeCtrl
, params
.ioeventfd
, false),
8410 DEFINE_PROP_UINT8("zoned.zasl", NvmeCtrl
, params
.zasl
, 0),
8411 DEFINE_PROP_BOOL("zoned.auto_transition", NvmeCtrl
,
8412 params
.auto_transition_zones
, true),
8413 DEFINE_PROP_UINT8("sriov_max_vfs", NvmeCtrl
, params
.sriov_max_vfs
, 0),
8414 DEFINE_PROP_UINT16("sriov_vq_flexible", NvmeCtrl
,
8415 params
.sriov_vq_flexible
, 0),
8416 DEFINE_PROP_UINT16("sriov_vi_flexible", NvmeCtrl
,
8417 params
.sriov_vi_flexible
, 0),
8418 DEFINE_PROP_UINT8("sriov_max_vi_per_vf", NvmeCtrl
,
8419 params
.sriov_max_vi_per_vf
, 0),
8420 DEFINE_PROP_UINT8("sriov_max_vq_per_vf", NvmeCtrl
,
8421 params
.sriov_max_vq_per_vf
, 0),
8422 DEFINE_PROP_END_OF_LIST(),
8425 static void nvme_get_smart_warning(Object
*obj
, Visitor
*v
, const char *name
,
8426 void *opaque
, Error
**errp
)
8428 NvmeCtrl
*n
= NVME(obj
);
8429 uint8_t value
= n
->smart_critical_warning
;
8431 visit_type_uint8(v
, name
, &value
, errp
);
8434 static void nvme_set_smart_warning(Object
*obj
, Visitor
*v
, const char *name
,
8435 void *opaque
, Error
**errp
)
8437 NvmeCtrl
*n
= NVME(obj
);
8438 uint8_t value
, old_value
, cap
= 0, index
, event
;
8440 if (!visit_type_uint8(v
, name
, &value
, errp
)) {
8444 cap
= NVME_SMART_SPARE
| NVME_SMART_TEMPERATURE
| NVME_SMART_RELIABILITY
8445 | NVME_SMART_MEDIA_READ_ONLY
| NVME_SMART_FAILED_VOLATILE_MEDIA
;
8446 if (NVME_CAP_PMRS(ldq_le_p(&n
->bar
.cap
))) {
8447 cap
|= NVME_SMART_PMR_UNRELIABLE
;
8450 if ((value
& cap
) != value
) {
8451 error_setg(errp
, "unsupported smart critical warning bits: 0x%x",
8456 old_value
= n
->smart_critical_warning
;
8457 n
->smart_critical_warning
= value
;
8459 /* only inject new bits of smart critical warning */
8460 for (index
= 0; index
< NVME_SMART_WARN_MAX
; index
++) {
8462 if (value
& ~old_value
& event
)
8463 nvme_smart_event(n
, event
);
8467 static void nvme_pci_reset(DeviceState
*qdev
)
8469 PCIDevice
*pci_dev
= PCI_DEVICE(qdev
);
8470 NvmeCtrl
*n
= NVME(pci_dev
);
8472 trace_pci_nvme_pci_reset();
8473 nvme_ctrl_reset(n
, NVME_RESET_FUNCTION
);
8476 static void nvme_sriov_pre_write_ctrl(PCIDevice
*dev
, uint32_t address
,
8477 uint32_t val
, int len
)
8479 NvmeCtrl
*n
= NVME(dev
);
8480 NvmeSecCtrlEntry
*sctrl
;
8481 uint16_t sriov_cap
= dev
->exp
.sriov_cap
;
8482 uint32_t off
= address
- sriov_cap
;
8489 if (range_covers_byte(off
, len
, PCI_SRIOV_CTRL
)) {
8490 if (!(val
& PCI_SRIOV_CTRL_VFE
)) {
8491 num_vfs
= pci_get_word(dev
->config
+ sriov_cap
+ PCI_SRIOV_NUM_VF
);
8492 for (i
= 0; i
< num_vfs
; i
++) {
8493 sctrl
= &n
->sec_ctrl_list
.sec
[i
];
8494 nvme_virt_set_state(n
, le16_to_cpu(sctrl
->scid
), false);
8500 static void nvme_pci_write_config(PCIDevice
*dev
, uint32_t address
,
8501 uint32_t val
, int len
)
8503 nvme_sriov_pre_write_ctrl(dev
, address
, val
, len
);
8504 pci_default_write_config(dev
, address
, val
, len
);
8505 pcie_cap_flr_write_config(dev
, address
, val
, len
);
8508 static const VMStateDescription nvme_vmstate
= {
8513 static void nvme_class_init(ObjectClass
*oc
, void *data
)
8515 DeviceClass
*dc
= DEVICE_CLASS(oc
);
8516 PCIDeviceClass
*pc
= PCI_DEVICE_CLASS(oc
);
8518 pc
->realize
= nvme_realize
;
8519 pc
->config_write
= nvme_pci_write_config
;
8520 pc
->exit
= nvme_exit
;
8521 pc
->class_id
= PCI_CLASS_STORAGE_EXPRESS
;
8524 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
8525 dc
->desc
= "Non-Volatile Memory Express";
8526 device_class_set_props(dc
, nvme_props
);
8527 dc
->vmsd
= &nvme_vmstate
;
8528 dc
->reset
= nvme_pci_reset
;
8531 static void nvme_instance_init(Object
*obj
)
8533 NvmeCtrl
*n
= NVME(obj
);
8535 device_add_bootindex_property(obj
, &n
->namespace.blkconf
.bootindex
,
8536 "bootindex", "/namespace@1,0",
8539 object_property_add(obj
, "smart_critical_warning", "uint8",
8540 nvme_get_smart_warning
,
8541 nvme_set_smart_warning
, NULL
, NULL
);
8544 static const TypeInfo nvme_info
= {
8546 .parent
= TYPE_PCI_DEVICE
,
8547 .instance_size
= sizeof(NvmeCtrl
),
8548 .instance_init
= nvme_instance_init
,
8549 .class_init
= nvme_class_init
,
8550 .interfaces
= (InterfaceInfo
[]) {
8551 { INTERFACE_PCIE_DEVICE
},
8556 static const TypeInfo nvme_bus_info
= {
8557 .name
= TYPE_NVME_BUS
,
8559 .instance_size
= sizeof(NvmeBus
),
8562 static void nvme_register_types(void)
8564 type_register_static(&nvme_info
);
8565 type_register_static(&nvme_bus_info
);
8568 type_init(nvme_register_types
)