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]>, \
39 * -device nvme-ns,drive=<drive_id>,bus=<bus_name>,nsid=<nsid>,\
40 * zoned=<true|false[optional]>, \
41 * subsys=<subsys_id>,detached=<true|false[optional]>
43 * Note cmb_size_mb denotes size of CMB in MB. CMB is assumed to be at
44 * offset 0 in BAR2 and supports only WDS, RDS and SQS for now. By default, the
45 * device will use the "v1.4 CMB scheme" - use the `legacy-cmb` parameter to
46 * always enable the CMBLOC and CMBSZ registers (v1.3 behavior).
48 * Enabling pmr emulation can be achieved by pointing to memory-backend-file.
50 * -object memory-backend-file,id=<mem_id>,share=on,mem-path=<file_path>, \
51 * size=<size> .... -device nvme,...,pmrdev=<mem_id>
53 * The PMR will use BAR 4/5 exclusively.
55 * To place controller(s) and namespace(s) to a subsystem, then provide
56 * nvme-subsys device as above.
58 * nvme subsystem device parameters
59 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
61 * This parameter provides the `<nqn_id>` part of the string
62 * `nqn.2019-08.org.qemu:<nqn_id>` which will be reported in the SUBNQN field
63 * of subsystem controllers. Note that `<nqn_id>` should be unique per
64 * subsystem, but this is not enforced by QEMU. If not specified, it will
65 * default to the value of the `id` parameter (`<subsys_id>`).
67 * nvme device parameters
68 * ~~~~~~~~~~~~~~~~~~~~~~
70 * Specifying this parameter attaches the controller to the subsystem and
71 * the SUBNQN field in the controller will report the NQN of the subsystem
72 * device. This also enables multi controller capability represented in
73 * Identify Controller data structure in CMIC (Controller Multi-path I/O and
74 * Namesapce Sharing Capabilities).
77 * The Asynchronous Event Request Limit (AERL). Indicates the maximum number
78 * of concurrently outstanding Asynchronous Event Request commands support
79 * by the controller. This is a 0's based value.
82 * This is the maximum number of events that the device will enqueue for
83 * completion when there are no outstanding AERs. When the maximum number of
84 * enqueued events are reached, subsequent events will be dropped.
87 * Indicates the maximum data transfer size for a command that transfers data
88 * between host-accessible memory and the controller. The value is specified
89 * as a power of two (2^n) and is in units of the minimum memory page size
90 * (CAP.MPSMIN). The default value is 7 (i.e. 512 KiB).
93 * Indicates the maximum data size limit for the Verify command. Like `mdts`,
94 * this value is specified as a power of two (2^n) and is in units of the
95 * minimum memory page size (CAP.MPSMIN). The default value is 7 (i.e. 512
99 * Indicates the maximum data transfer size for the Zone Append command. Like
100 * `mdts`, the value is specified as a power of two (2^n) and is in units of
101 * the minimum memory page size (CAP.MPSMIN). The default value is 0 (i.e.
102 * defaulting to the value of `mdts`).
104 * - `zoned.auto_transition`
105 * Indicates if zones in zone state implicitly opened can be automatically
106 * transitioned to zone state closed for resource management purposes.
109 * nvme namespace device parameters
110 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112 * When the parent nvme device (as defined explicitly by the 'bus' parameter
113 * or implicitly by the most recently defined NvmeBus) is linked to an
114 * nvme-subsys device, the namespace will be attached to all controllers in
115 * the subsystem. If set to 'off' (the default), the namespace will remain a
116 * private namespace and may only be attached to a single controller at a
120 * This parameter is only valid together with the `subsys` parameter. If left
121 * at the default value (`false/off`), the namespace will be attached to all
122 * controllers in the NVMe subsystem at boot-up. If set to `true/on`, the
123 * namespace will be available in the subsystem but not attached to any
126 * Setting `zoned` to true selects Zoned Command Set at the namespace.
127 * In this case, the following namespace properties are available to configure
129 * zoned.zone_size=<zone size in bytes, default: 128MiB>
130 * The number may be followed by K, M, G as in kilo-, mega- or giga-.
132 * zoned.zone_capacity=<zone capacity in bytes, default: zone size>
133 * The value 0 (default) forces zone capacity to be the same as zone
134 * size. The value of this property may not exceed zone size.
136 * zoned.descr_ext_size=<zone descriptor extension size, default 0>
137 * This value needs to be specified in 64B units. If it is zero,
138 * namespace(s) will not support zone descriptor extensions.
140 * zoned.max_active=<Maximum Active Resources (zones), default: 0>
141 * The default value means there is no limit to the number of
142 * concurrently active zones.
144 * zoned.max_open=<Maximum Open Resources (zones), default: 0>
145 * The default value means there is no limit to the number of
146 * concurrently open zones.
148 * zoned.cross_read=<enable RAZB, default: false>
149 * Setting this property to true enables Read Across Zone Boundaries.
152 #include "qemu/osdep.h"
153 #include "qemu/cutils.h"
154 #include "qemu/error-report.h"
155 #include "qemu/log.h"
156 #include "qemu/units.h"
157 #include "qapi/error.h"
158 #include "qapi/visitor.h"
159 #include "sysemu/sysemu.h"
160 #include "sysemu/block-backend.h"
161 #include "sysemu/hostmem.h"
162 #include "hw/pci/msix.h"
163 #include "migration/vmstate.h"
168 #define NVME_MAX_IOQPAIRS 0xffff
169 #define NVME_DB_SIZE 4
170 #define NVME_SPEC_VER 0x00010400
171 #define NVME_CMB_BIR 2
172 #define NVME_PMR_BIR 4
173 #define NVME_TEMPERATURE 0x143
174 #define NVME_TEMPERATURE_WARNING 0x157
175 #define NVME_TEMPERATURE_CRITICAL 0x175
176 #define NVME_NUM_FW_SLOTS 1
177 #define NVME_DEFAULT_MAX_ZA_SIZE (128 * KiB)
179 #define NVME_GUEST_ERR(trace, fmt, ...) \
181 (trace_##trace)(__VA_ARGS__); \
182 qemu_log_mask(LOG_GUEST_ERROR, #trace \
183 " in %s: " fmt "\n", __func__, ## __VA_ARGS__); \
186 static const bool nvme_feature_support
[NVME_FID_MAX
] = {
187 [NVME_ARBITRATION
] = true,
188 [NVME_POWER_MANAGEMENT
] = true,
189 [NVME_TEMPERATURE_THRESHOLD
] = true,
190 [NVME_ERROR_RECOVERY
] = true,
191 [NVME_VOLATILE_WRITE_CACHE
] = true,
192 [NVME_NUMBER_OF_QUEUES
] = true,
193 [NVME_INTERRUPT_COALESCING
] = true,
194 [NVME_INTERRUPT_VECTOR_CONF
] = true,
195 [NVME_WRITE_ATOMICITY
] = true,
196 [NVME_ASYNCHRONOUS_EVENT_CONF
] = true,
197 [NVME_TIMESTAMP
] = true,
198 [NVME_COMMAND_SET_PROFILE
] = true,
201 static const uint32_t nvme_feature_cap
[NVME_FID_MAX
] = {
202 [NVME_TEMPERATURE_THRESHOLD
] = NVME_FEAT_CAP_CHANGE
,
203 [NVME_ERROR_RECOVERY
] = NVME_FEAT_CAP_CHANGE
| NVME_FEAT_CAP_NS
,
204 [NVME_VOLATILE_WRITE_CACHE
] = NVME_FEAT_CAP_CHANGE
,
205 [NVME_NUMBER_OF_QUEUES
] = NVME_FEAT_CAP_CHANGE
,
206 [NVME_ASYNCHRONOUS_EVENT_CONF
] = NVME_FEAT_CAP_CHANGE
,
207 [NVME_TIMESTAMP
] = NVME_FEAT_CAP_CHANGE
,
208 [NVME_COMMAND_SET_PROFILE
] = NVME_FEAT_CAP_CHANGE
,
211 static const uint32_t nvme_cse_acs
[256] = {
212 [NVME_ADM_CMD_DELETE_SQ
] = NVME_CMD_EFF_CSUPP
,
213 [NVME_ADM_CMD_CREATE_SQ
] = NVME_CMD_EFF_CSUPP
,
214 [NVME_ADM_CMD_GET_LOG_PAGE
] = NVME_CMD_EFF_CSUPP
,
215 [NVME_ADM_CMD_DELETE_CQ
] = NVME_CMD_EFF_CSUPP
,
216 [NVME_ADM_CMD_CREATE_CQ
] = NVME_CMD_EFF_CSUPP
,
217 [NVME_ADM_CMD_IDENTIFY
] = NVME_CMD_EFF_CSUPP
,
218 [NVME_ADM_CMD_ABORT
] = NVME_CMD_EFF_CSUPP
,
219 [NVME_ADM_CMD_SET_FEATURES
] = NVME_CMD_EFF_CSUPP
,
220 [NVME_ADM_CMD_GET_FEATURES
] = NVME_CMD_EFF_CSUPP
,
221 [NVME_ADM_CMD_ASYNC_EV_REQ
] = NVME_CMD_EFF_CSUPP
,
222 [NVME_ADM_CMD_NS_ATTACHMENT
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_NIC
,
223 [NVME_ADM_CMD_FORMAT_NVM
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
226 static const uint32_t nvme_cse_iocs_none
[256];
228 static const uint32_t nvme_cse_iocs_nvm
[256] = {
229 [NVME_CMD_FLUSH
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
230 [NVME_CMD_WRITE_ZEROES
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
231 [NVME_CMD_WRITE
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
232 [NVME_CMD_READ
] = NVME_CMD_EFF_CSUPP
,
233 [NVME_CMD_DSM
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
234 [NVME_CMD_VERIFY
] = NVME_CMD_EFF_CSUPP
,
235 [NVME_CMD_COPY
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
236 [NVME_CMD_COMPARE
] = NVME_CMD_EFF_CSUPP
,
239 static const uint32_t nvme_cse_iocs_zoned
[256] = {
240 [NVME_CMD_FLUSH
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
241 [NVME_CMD_WRITE_ZEROES
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
242 [NVME_CMD_WRITE
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
243 [NVME_CMD_READ
] = NVME_CMD_EFF_CSUPP
,
244 [NVME_CMD_DSM
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
245 [NVME_CMD_VERIFY
] = NVME_CMD_EFF_CSUPP
,
246 [NVME_CMD_COPY
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
247 [NVME_CMD_COMPARE
] = NVME_CMD_EFF_CSUPP
,
248 [NVME_CMD_ZONE_APPEND
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
249 [NVME_CMD_ZONE_MGMT_SEND
] = NVME_CMD_EFF_CSUPP
| NVME_CMD_EFF_LBCC
,
250 [NVME_CMD_ZONE_MGMT_RECV
] = NVME_CMD_EFF_CSUPP
,
253 static void nvme_process_sq(void *opaque
);
255 static uint16_t nvme_sqid(NvmeRequest
*req
)
257 return le16_to_cpu(req
->sq
->sqid
);
260 static void nvme_assign_zone_state(NvmeNamespace
*ns
, NvmeZone
*zone
,
263 if (QTAILQ_IN_USE(zone
, entry
)) {
264 switch (nvme_get_zone_state(zone
)) {
265 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
266 QTAILQ_REMOVE(&ns
->exp_open_zones
, zone
, entry
);
268 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
269 QTAILQ_REMOVE(&ns
->imp_open_zones
, zone
, entry
);
271 case NVME_ZONE_STATE_CLOSED
:
272 QTAILQ_REMOVE(&ns
->closed_zones
, zone
, entry
);
274 case NVME_ZONE_STATE_FULL
:
275 QTAILQ_REMOVE(&ns
->full_zones
, zone
, entry
);
281 nvme_set_zone_state(zone
, state
);
284 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
285 QTAILQ_INSERT_TAIL(&ns
->exp_open_zones
, zone
, entry
);
287 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
288 QTAILQ_INSERT_TAIL(&ns
->imp_open_zones
, zone
, entry
);
290 case NVME_ZONE_STATE_CLOSED
:
291 QTAILQ_INSERT_TAIL(&ns
->closed_zones
, zone
, entry
);
293 case NVME_ZONE_STATE_FULL
:
294 QTAILQ_INSERT_TAIL(&ns
->full_zones
, zone
, entry
);
295 case NVME_ZONE_STATE_READ_ONLY
:
303 * Check if we can open a zone without exceeding open/active limits.
304 * AOR stands for "Active and Open Resources" (see TP 4053 section 2.5).
306 static int nvme_aor_check(NvmeNamespace
*ns
, uint32_t act
, uint32_t opn
)
308 if (ns
->params
.max_active_zones
!= 0 &&
309 ns
->nr_active_zones
+ act
> ns
->params
.max_active_zones
) {
310 trace_pci_nvme_err_insuff_active_res(ns
->params
.max_active_zones
);
311 return NVME_ZONE_TOO_MANY_ACTIVE
| NVME_DNR
;
313 if (ns
->params
.max_open_zones
!= 0 &&
314 ns
->nr_open_zones
+ opn
> ns
->params
.max_open_zones
) {
315 trace_pci_nvme_err_insuff_open_res(ns
->params
.max_open_zones
);
316 return NVME_ZONE_TOO_MANY_OPEN
| NVME_DNR
;
322 static bool nvme_addr_is_cmb(NvmeCtrl
*n
, hwaddr addr
)
330 lo
= n
->params
.legacy_cmb
? n
->cmb
.mem
.addr
: n
->cmb
.cba
;
331 hi
= lo
+ int128_get64(n
->cmb
.mem
.size
);
333 return addr
>= lo
&& addr
< hi
;
336 static inline void *nvme_addr_to_cmb(NvmeCtrl
*n
, hwaddr addr
)
338 hwaddr base
= n
->params
.legacy_cmb
? n
->cmb
.mem
.addr
: n
->cmb
.cba
;
339 return &n
->cmb
.buf
[addr
- base
];
342 static bool nvme_addr_is_pmr(NvmeCtrl
*n
, hwaddr addr
)
350 hi
= n
->pmr
.cba
+ int128_get64(n
->pmr
.dev
->mr
.size
);
352 return addr
>= n
->pmr
.cba
&& addr
< hi
;
355 static inline void *nvme_addr_to_pmr(NvmeCtrl
*n
, hwaddr addr
)
357 return memory_region_get_ram_ptr(&n
->pmr
.dev
->mr
) + (addr
- n
->pmr
.cba
);
360 static int nvme_addr_read(NvmeCtrl
*n
, hwaddr addr
, void *buf
, int size
)
362 hwaddr hi
= addr
+ size
- 1;
367 if (n
->bar
.cmbsz
&& nvme_addr_is_cmb(n
, addr
) && nvme_addr_is_cmb(n
, hi
)) {
368 memcpy(buf
, nvme_addr_to_cmb(n
, addr
), size
);
372 if (nvme_addr_is_pmr(n
, addr
) && nvme_addr_is_pmr(n
, hi
)) {
373 memcpy(buf
, nvme_addr_to_pmr(n
, addr
), size
);
377 return pci_dma_read(&n
->parent_obj
, addr
, buf
, size
);
380 static int nvme_addr_write(NvmeCtrl
*n
, hwaddr addr
, void *buf
, int size
)
382 hwaddr hi
= addr
+ size
- 1;
387 if (n
->bar
.cmbsz
&& nvme_addr_is_cmb(n
, addr
) && nvme_addr_is_cmb(n
, hi
)) {
388 memcpy(nvme_addr_to_cmb(n
, addr
), buf
, size
);
392 if (nvme_addr_is_pmr(n
, addr
) && nvme_addr_is_pmr(n
, hi
)) {
393 memcpy(nvme_addr_to_pmr(n
, addr
), buf
, size
);
397 return pci_dma_write(&n
->parent_obj
, addr
, buf
, size
);
400 static bool nvme_nsid_valid(NvmeCtrl
*n
, uint32_t nsid
)
403 (nsid
== NVME_NSID_BROADCAST
|| nsid
<= NVME_MAX_NAMESPACES
);
406 static int nvme_check_sqid(NvmeCtrl
*n
, uint16_t sqid
)
408 return sqid
< n
->params
.max_ioqpairs
+ 1 && n
->sq
[sqid
] != NULL
? 0 : -1;
411 static int nvme_check_cqid(NvmeCtrl
*n
, uint16_t cqid
)
413 return cqid
< n
->params
.max_ioqpairs
+ 1 && n
->cq
[cqid
] != NULL
? 0 : -1;
416 static void nvme_inc_cq_tail(NvmeCQueue
*cq
)
419 if (cq
->tail
>= cq
->size
) {
421 cq
->phase
= !cq
->phase
;
425 static void nvme_inc_sq_head(NvmeSQueue
*sq
)
427 sq
->head
= (sq
->head
+ 1) % sq
->size
;
430 static uint8_t nvme_cq_full(NvmeCQueue
*cq
)
432 return (cq
->tail
+ 1) % cq
->size
== cq
->head
;
435 static uint8_t nvme_sq_empty(NvmeSQueue
*sq
)
437 return sq
->head
== sq
->tail
;
440 static void nvme_irq_check(NvmeCtrl
*n
)
442 uint32_t intms
= ldl_le_p(&n
->bar
.intms
);
444 if (msix_enabled(&(n
->parent_obj
))) {
447 if (~intms
& n
->irq_status
) {
448 pci_irq_assert(&n
->parent_obj
);
450 pci_irq_deassert(&n
->parent_obj
);
454 static void nvme_irq_assert(NvmeCtrl
*n
, NvmeCQueue
*cq
)
456 if (cq
->irq_enabled
) {
457 if (msix_enabled(&(n
->parent_obj
))) {
458 trace_pci_nvme_irq_msix(cq
->vector
);
459 msix_notify(&(n
->parent_obj
), cq
->vector
);
461 trace_pci_nvme_irq_pin();
462 assert(cq
->vector
< 32);
463 n
->irq_status
|= 1 << cq
->vector
;
467 trace_pci_nvme_irq_masked();
471 static void nvme_irq_deassert(NvmeCtrl
*n
, NvmeCQueue
*cq
)
473 if (cq
->irq_enabled
) {
474 if (msix_enabled(&(n
->parent_obj
))) {
477 assert(cq
->vector
< 32);
478 if (!n
->cq_pending
) {
479 n
->irq_status
&= ~(1 << cq
->vector
);
486 static void nvme_req_clear(NvmeRequest
*req
)
491 memset(&req
->cqe
, 0x0, sizeof(req
->cqe
));
492 req
->status
= NVME_SUCCESS
;
495 static inline void nvme_sg_init(NvmeCtrl
*n
, NvmeSg
*sg
, bool dma
)
498 pci_dma_sglist_init(&sg
->qsg
, &n
->parent_obj
, 0);
499 sg
->flags
= NVME_SG_DMA
;
501 qemu_iovec_init(&sg
->iov
, 0);
504 sg
->flags
|= NVME_SG_ALLOC
;
507 static inline void nvme_sg_unmap(NvmeSg
*sg
)
509 if (!(sg
->flags
& NVME_SG_ALLOC
)) {
513 if (sg
->flags
& NVME_SG_DMA
) {
514 qemu_sglist_destroy(&sg
->qsg
);
516 qemu_iovec_destroy(&sg
->iov
);
519 memset(sg
, 0x0, sizeof(*sg
));
523 * When metadata is transfered as extended LBAs, the DPTR mapped into `sg`
524 * holds both data and metadata. This function splits the data and metadata
525 * into two separate QSG/IOVs.
527 static void nvme_sg_split(NvmeSg
*sg
, NvmeNamespace
*ns
, NvmeSg
*data
,
531 uint32_t trans_len
, count
= ns
->lbasz
;
533 bool dma
= sg
->flags
& NVME_SG_DMA
;
535 size_t sg_len
= dma
? sg
->qsg
.size
: sg
->iov
.size
;
538 assert(sg
->flags
& NVME_SG_ALLOC
);
541 sge_len
= dma
? sg
->qsg
.sg
[sg_idx
].len
: sg
->iov
.iov
[sg_idx
].iov_len
;
543 trans_len
= MIN(sg_len
, count
);
544 trans_len
= MIN(trans_len
, sge_len
- offset
);
548 qemu_sglist_add(&dst
->qsg
, sg
->qsg
.sg
[sg_idx
].base
+ offset
,
551 qemu_iovec_add(&dst
->iov
,
552 sg
->iov
.iov
[sg_idx
].iov_base
+ offset
,
562 dst
= (dst
== data
) ? mdata
: data
;
563 count
= (dst
== data
) ? ns
->lbasz
: ns
->lbaf
.ms
;
566 if (sge_len
== offset
) {
573 static uint16_t nvme_map_addr_cmb(NvmeCtrl
*n
, QEMUIOVector
*iov
, hwaddr addr
,
580 trace_pci_nvme_map_addr_cmb(addr
, len
);
582 if (!nvme_addr_is_cmb(n
, addr
) || !nvme_addr_is_cmb(n
, addr
+ len
- 1)) {
583 return NVME_DATA_TRAS_ERROR
;
586 qemu_iovec_add(iov
, nvme_addr_to_cmb(n
, addr
), len
);
591 static uint16_t nvme_map_addr_pmr(NvmeCtrl
*n
, QEMUIOVector
*iov
, hwaddr addr
,
598 if (!nvme_addr_is_pmr(n
, addr
) || !nvme_addr_is_pmr(n
, addr
+ len
- 1)) {
599 return NVME_DATA_TRAS_ERROR
;
602 qemu_iovec_add(iov
, nvme_addr_to_pmr(n
, addr
), len
);
607 static uint16_t nvme_map_addr(NvmeCtrl
*n
, NvmeSg
*sg
, hwaddr addr
, size_t len
)
609 bool cmb
= false, pmr
= false;
615 trace_pci_nvme_map_addr(addr
, len
);
617 if (nvme_addr_is_cmb(n
, addr
)) {
619 } else if (nvme_addr_is_pmr(n
, addr
)) {
624 if (sg
->flags
& NVME_SG_DMA
) {
625 return NVME_INVALID_USE_OF_CMB
| NVME_DNR
;
628 if (sg
->iov
.niov
+ 1 > IOV_MAX
) {
629 goto max_mappings_exceeded
;
633 return nvme_map_addr_cmb(n
, &sg
->iov
, addr
, len
);
635 return nvme_map_addr_pmr(n
, &sg
->iov
, addr
, len
);
639 if (!(sg
->flags
& NVME_SG_DMA
)) {
640 return NVME_INVALID_USE_OF_CMB
| NVME_DNR
;
643 if (sg
->qsg
.nsg
+ 1 > IOV_MAX
) {
644 goto max_mappings_exceeded
;
647 qemu_sglist_add(&sg
->qsg
, addr
, len
);
651 max_mappings_exceeded
:
652 NVME_GUEST_ERR(pci_nvme_ub_too_many_mappings
,
653 "number of mappings exceed 1024");
654 return NVME_INTERNAL_DEV_ERROR
| NVME_DNR
;
657 static inline bool nvme_addr_is_dma(NvmeCtrl
*n
, hwaddr addr
)
659 return !(nvme_addr_is_cmb(n
, addr
) || nvme_addr_is_pmr(n
, addr
));
662 static uint16_t nvme_map_prp(NvmeCtrl
*n
, NvmeSg
*sg
, uint64_t prp1
,
663 uint64_t prp2
, uint32_t len
)
665 hwaddr trans_len
= n
->page_size
- (prp1
% n
->page_size
);
666 trans_len
= MIN(len
, trans_len
);
667 int num_prps
= (len
>> n
->page_bits
) + 1;
671 trace_pci_nvme_map_prp(trans_len
, len
, prp1
, prp2
, num_prps
);
673 nvme_sg_init(n
, sg
, nvme_addr_is_dma(n
, prp1
));
675 status
= nvme_map_addr(n
, sg
, prp1
, trans_len
);
682 if (len
> n
->page_size
) {
683 uint64_t prp_list
[n
->max_prp_ents
];
684 uint32_t nents
, prp_trans
;
688 * The first PRP list entry, pointed to by PRP2 may contain offset.
689 * Hence, we need to calculate the number of entries in based on
692 nents
= (n
->page_size
- (prp2
& (n
->page_size
- 1))) >> 3;
693 prp_trans
= MIN(n
->max_prp_ents
, nents
) * sizeof(uint64_t);
694 ret
= nvme_addr_read(n
, prp2
, (void *)prp_list
, prp_trans
);
696 trace_pci_nvme_err_addr_read(prp2
);
697 status
= NVME_DATA_TRAS_ERROR
;
701 uint64_t prp_ent
= le64_to_cpu(prp_list
[i
]);
703 if (i
== nents
- 1 && len
> n
->page_size
) {
704 if (unlikely(prp_ent
& (n
->page_size
- 1))) {
705 trace_pci_nvme_err_invalid_prplist_ent(prp_ent
);
706 status
= NVME_INVALID_PRP_OFFSET
| NVME_DNR
;
711 nents
= (len
+ n
->page_size
- 1) >> n
->page_bits
;
712 nents
= MIN(nents
, n
->max_prp_ents
);
713 prp_trans
= nents
* sizeof(uint64_t);
714 ret
= nvme_addr_read(n
, prp_ent
, (void *)prp_list
,
717 trace_pci_nvme_err_addr_read(prp_ent
);
718 status
= NVME_DATA_TRAS_ERROR
;
721 prp_ent
= le64_to_cpu(prp_list
[i
]);
724 if (unlikely(prp_ent
& (n
->page_size
- 1))) {
725 trace_pci_nvme_err_invalid_prplist_ent(prp_ent
);
726 status
= NVME_INVALID_PRP_OFFSET
| NVME_DNR
;
730 trans_len
= MIN(len
, n
->page_size
);
731 status
= nvme_map_addr(n
, sg
, prp_ent
, trans_len
);
740 if (unlikely(prp2
& (n
->page_size
- 1))) {
741 trace_pci_nvme_err_invalid_prp2_align(prp2
);
742 status
= NVME_INVALID_PRP_OFFSET
| NVME_DNR
;
745 status
= nvme_map_addr(n
, sg
, prp2
, len
);
760 * Map 'nsgld' data descriptors from 'segment'. The function will subtract the
761 * number of bytes mapped in len.
763 static uint16_t nvme_map_sgl_data(NvmeCtrl
*n
, NvmeSg
*sg
,
764 NvmeSglDescriptor
*segment
, uint64_t nsgld
,
765 size_t *len
, NvmeCmd
*cmd
)
767 dma_addr_t addr
, trans_len
;
771 for (int i
= 0; i
< nsgld
; i
++) {
772 uint8_t type
= NVME_SGL_TYPE(segment
[i
].type
);
775 case NVME_SGL_DESCR_TYPE_BIT_BUCKET
:
776 if (cmd
->opcode
== NVME_CMD_WRITE
) {
779 case NVME_SGL_DESCR_TYPE_DATA_BLOCK
:
781 case NVME_SGL_DESCR_TYPE_SEGMENT
:
782 case NVME_SGL_DESCR_TYPE_LAST_SEGMENT
:
783 return NVME_INVALID_NUM_SGL_DESCRS
| NVME_DNR
;
785 return NVME_SGL_DESCR_TYPE_INVALID
| NVME_DNR
;
788 dlen
= le32_to_cpu(segment
[i
].len
);
796 * All data has been mapped, but the SGL contains additional
797 * segments and/or descriptors. The controller might accept
798 * ignoring the rest of the SGL.
800 uint32_t sgls
= le32_to_cpu(n
->id_ctrl
.sgls
);
801 if (sgls
& NVME_CTRL_SGLS_EXCESS_LENGTH
) {
805 trace_pci_nvme_err_invalid_sgl_excess_length(dlen
);
806 return NVME_DATA_SGL_LEN_INVALID
| NVME_DNR
;
809 trans_len
= MIN(*len
, dlen
);
811 if (type
== NVME_SGL_DESCR_TYPE_BIT_BUCKET
) {
815 addr
= le64_to_cpu(segment
[i
].addr
);
817 if (UINT64_MAX
- addr
< dlen
) {
818 return NVME_DATA_SGL_LEN_INVALID
| NVME_DNR
;
821 status
= nvme_map_addr(n
, sg
, addr
, trans_len
);
833 static uint16_t nvme_map_sgl(NvmeCtrl
*n
, NvmeSg
*sg
, NvmeSglDescriptor sgl
,
834 size_t len
, NvmeCmd
*cmd
)
837 * Read the segment in chunks of 256 descriptors (one 4k page) to avoid
838 * dynamically allocating a potentially huge SGL. The spec allows the SGL
839 * to be larger (as in number of bytes required to describe the SGL
840 * descriptors and segment chain) than the command transfer size, so it is
841 * not bounded by MDTS.
843 const int SEG_CHUNK_SIZE
= 256;
845 NvmeSglDescriptor segment
[SEG_CHUNK_SIZE
], *sgld
, *last_sgld
;
853 addr
= le64_to_cpu(sgl
.addr
);
855 trace_pci_nvme_map_sgl(NVME_SGL_TYPE(sgl
.type
), len
);
857 nvme_sg_init(n
, sg
, nvme_addr_is_dma(n
, addr
));
860 * If the entire transfer can be described with a single data block it can
861 * be mapped directly.
863 if (NVME_SGL_TYPE(sgl
.type
) == NVME_SGL_DESCR_TYPE_DATA_BLOCK
) {
864 status
= nvme_map_sgl_data(n
, sg
, sgld
, 1, &len
, cmd
);
873 switch (NVME_SGL_TYPE(sgld
->type
)) {
874 case NVME_SGL_DESCR_TYPE_SEGMENT
:
875 case NVME_SGL_DESCR_TYPE_LAST_SEGMENT
:
878 return NVME_INVALID_SGL_SEG_DESCR
| NVME_DNR
;
881 seg_len
= le32_to_cpu(sgld
->len
);
883 /* check the length of the (Last) Segment descriptor */
884 if ((!seg_len
|| seg_len
& 0xf) &&
885 (NVME_SGL_TYPE(sgld
->type
) != NVME_SGL_DESCR_TYPE_BIT_BUCKET
)) {
886 return NVME_INVALID_SGL_SEG_DESCR
| NVME_DNR
;
889 if (UINT64_MAX
- addr
< seg_len
) {
890 return NVME_DATA_SGL_LEN_INVALID
| NVME_DNR
;
893 nsgld
= seg_len
/ sizeof(NvmeSglDescriptor
);
895 while (nsgld
> SEG_CHUNK_SIZE
) {
896 if (nvme_addr_read(n
, addr
, segment
, sizeof(segment
))) {
897 trace_pci_nvme_err_addr_read(addr
);
898 status
= NVME_DATA_TRAS_ERROR
;
902 status
= nvme_map_sgl_data(n
, sg
, segment
, SEG_CHUNK_SIZE
,
908 nsgld
-= SEG_CHUNK_SIZE
;
909 addr
+= SEG_CHUNK_SIZE
* sizeof(NvmeSglDescriptor
);
912 ret
= nvme_addr_read(n
, addr
, segment
, nsgld
*
913 sizeof(NvmeSglDescriptor
));
915 trace_pci_nvme_err_addr_read(addr
);
916 status
= NVME_DATA_TRAS_ERROR
;
920 last_sgld
= &segment
[nsgld
- 1];
923 * If the segment ends with a Data Block or Bit Bucket Descriptor Type,
926 switch (NVME_SGL_TYPE(last_sgld
->type
)) {
927 case NVME_SGL_DESCR_TYPE_DATA_BLOCK
:
928 case NVME_SGL_DESCR_TYPE_BIT_BUCKET
:
929 status
= nvme_map_sgl_data(n
, sg
, segment
, nsgld
, &len
, cmd
);
941 * If the last descriptor was not a Data Block or Bit Bucket, then the
942 * current segment must not be a Last Segment.
944 if (NVME_SGL_TYPE(sgld
->type
) == NVME_SGL_DESCR_TYPE_LAST_SEGMENT
) {
945 status
= NVME_INVALID_SGL_SEG_DESCR
| NVME_DNR
;
950 addr
= le64_to_cpu(sgld
->addr
);
953 * Do not map the last descriptor; it will be a Segment or Last Segment
954 * descriptor and is handled by the next iteration.
956 status
= nvme_map_sgl_data(n
, sg
, segment
, nsgld
- 1, &len
, cmd
);
963 /* if there is any residual left in len, the SGL was too short */
965 status
= NVME_DATA_SGL_LEN_INVALID
| NVME_DNR
;
976 uint16_t nvme_map_dptr(NvmeCtrl
*n
, NvmeSg
*sg
, size_t len
,
981 switch (NVME_CMD_FLAGS_PSDT(cmd
->flags
)) {
983 prp1
= le64_to_cpu(cmd
->dptr
.prp1
);
984 prp2
= le64_to_cpu(cmd
->dptr
.prp2
);
986 return nvme_map_prp(n
, sg
, prp1
, prp2
, len
);
987 case NVME_PSDT_SGL_MPTR_CONTIGUOUS
:
988 case NVME_PSDT_SGL_MPTR_SGL
:
989 return nvme_map_sgl(n
, sg
, cmd
->dptr
.sgl
, len
, cmd
);
991 return NVME_INVALID_FIELD
;
995 static uint16_t nvme_map_mptr(NvmeCtrl
*n
, NvmeSg
*sg
, size_t len
,
998 int psdt
= NVME_CMD_FLAGS_PSDT(cmd
->flags
);
999 hwaddr mptr
= le64_to_cpu(cmd
->mptr
);
1002 if (psdt
== NVME_PSDT_SGL_MPTR_SGL
) {
1003 NvmeSglDescriptor sgl
;
1005 if (nvme_addr_read(n
, mptr
, &sgl
, sizeof(sgl
))) {
1006 return NVME_DATA_TRAS_ERROR
;
1009 status
= nvme_map_sgl(n
, sg
, sgl
, len
, cmd
);
1010 if (status
&& (status
& 0x7ff) == NVME_DATA_SGL_LEN_INVALID
) {
1011 status
= NVME_MD_SGL_LEN_INVALID
| NVME_DNR
;
1017 nvme_sg_init(n
, sg
, nvme_addr_is_dma(n
, mptr
));
1018 status
= nvme_map_addr(n
, sg
, mptr
, len
);
1026 static uint16_t nvme_map_data(NvmeCtrl
*n
, uint32_t nlb
, NvmeRequest
*req
)
1028 NvmeNamespace
*ns
= req
->ns
;
1029 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
1030 bool pi
= !!NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
);
1031 bool pract
= !!(le16_to_cpu(rw
->control
) & NVME_RW_PRINFO_PRACT
);
1032 size_t len
= nvme_l2b(ns
, nlb
);
1035 if (nvme_ns_ext(ns
) && !(pi
&& pract
&& ns
->lbaf
.ms
== 8)) {
1038 len
+= nvme_m2b(ns
, nlb
);
1040 status
= nvme_map_dptr(n
, &sg
, len
, &req
->cmd
);
1045 nvme_sg_init(n
, &req
->sg
, sg
.flags
& NVME_SG_DMA
);
1046 nvme_sg_split(&sg
, ns
, &req
->sg
, NULL
);
1049 return NVME_SUCCESS
;
1052 return nvme_map_dptr(n
, &req
->sg
, len
, &req
->cmd
);
1055 static uint16_t nvme_map_mdata(NvmeCtrl
*n
, uint32_t nlb
, NvmeRequest
*req
)
1057 NvmeNamespace
*ns
= req
->ns
;
1058 size_t len
= nvme_m2b(ns
, nlb
);
1061 if (nvme_ns_ext(ns
)) {
1064 len
+= nvme_l2b(ns
, nlb
);
1066 status
= nvme_map_dptr(n
, &sg
, len
, &req
->cmd
);
1071 nvme_sg_init(n
, &req
->sg
, sg
.flags
& NVME_SG_DMA
);
1072 nvme_sg_split(&sg
, ns
, NULL
, &req
->sg
);
1075 return NVME_SUCCESS
;
1078 return nvme_map_mptr(n
, &req
->sg
, len
, &req
->cmd
);
1081 static uint16_t nvme_tx_interleaved(NvmeCtrl
*n
, NvmeSg
*sg
, uint8_t *ptr
,
1082 uint32_t len
, uint32_t bytes
,
1083 int32_t skip_bytes
, int64_t offset
,
1084 NvmeTxDirection dir
)
1087 uint32_t trans_len
, count
= bytes
;
1088 bool dma
= sg
->flags
& NVME_SG_DMA
;
1093 assert(sg
->flags
& NVME_SG_ALLOC
);
1096 sge_len
= dma
? sg
->qsg
.sg
[sg_idx
].len
: sg
->iov
.iov
[sg_idx
].iov_len
;
1098 if (sge_len
- offset
< 0) {
1104 if (sge_len
== offset
) {
1110 trans_len
= MIN(len
, count
);
1111 trans_len
= MIN(trans_len
, sge_len
- offset
);
1114 addr
= sg
->qsg
.sg
[sg_idx
].base
+ offset
;
1116 addr
= (hwaddr
)(uintptr_t)sg
->iov
.iov
[sg_idx
].iov_base
+ offset
;
1119 if (dir
== NVME_TX_DIRECTION_TO_DEVICE
) {
1120 ret
= nvme_addr_read(n
, addr
, ptr
, trans_len
);
1122 ret
= nvme_addr_write(n
, addr
, ptr
, trans_len
);
1126 return NVME_DATA_TRAS_ERROR
;
1132 offset
+= trans_len
;
1136 offset
+= skip_bytes
;
1140 return NVME_SUCCESS
;
1143 static uint16_t nvme_tx(NvmeCtrl
*n
, NvmeSg
*sg
, uint8_t *ptr
, uint32_t len
,
1144 NvmeTxDirection dir
)
1146 assert(sg
->flags
& NVME_SG_ALLOC
);
1148 if (sg
->flags
& NVME_SG_DMA
) {
1149 const MemTxAttrs attrs
= MEMTXATTRS_UNSPECIFIED
;
1152 if (dir
== NVME_TX_DIRECTION_TO_DEVICE
) {
1153 residual
= dma_buf_write(ptr
, len
, &sg
->qsg
, attrs
);
1155 residual
= dma_buf_read(ptr
, len
, &sg
->qsg
, attrs
);
1158 if (unlikely(residual
)) {
1159 trace_pci_nvme_err_invalid_dma();
1160 return NVME_INVALID_FIELD
| NVME_DNR
;
1165 if (dir
== NVME_TX_DIRECTION_TO_DEVICE
) {
1166 bytes
= qemu_iovec_to_buf(&sg
->iov
, 0, ptr
, len
);
1168 bytes
= qemu_iovec_from_buf(&sg
->iov
, 0, ptr
, len
);
1171 if (unlikely(bytes
!= len
)) {
1172 trace_pci_nvme_err_invalid_dma();
1173 return NVME_INVALID_FIELD
| NVME_DNR
;
1177 return NVME_SUCCESS
;
1180 static inline uint16_t nvme_c2h(NvmeCtrl
*n
, uint8_t *ptr
, uint32_t len
,
1185 status
= nvme_map_dptr(n
, &req
->sg
, len
, &req
->cmd
);
1190 return nvme_tx(n
, &req
->sg
, ptr
, len
, NVME_TX_DIRECTION_FROM_DEVICE
);
1193 static inline uint16_t nvme_h2c(NvmeCtrl
*n
, uint8_t *ptr
, uint32_t len
,
1198 status
= nvme_map_dptr(n
, &req
->sg
, len
, &req
->cmd
);
1203 return nvme_tx(n
, &req
->sg
, ptr
, len
, NVME_TX_DIRECTION_TO_DEVICE
);
1206 uint16_t nvme_bounce_data(NvmeCtrl
*n
, uint8_t *ptr
, uint32_t len
,
1207 NvmeTxDirection dir
, NvmeRequest
*req
)
1209 NvmeNamespace
*ns
= req
->ns
;
1210 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
1211 bool pi
= !!NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
);
1212 bool pract
= !!(le16_to_cpu(rw
->control
) & NVME_RW_PRINFO_PRACT
);
1214 if (nvme_ns_ext(ns
) && !(pi
&& pract
&& ns
->lbaf
.ms
== 8)) {
1215 return nvme_tx_interleaved(n
, &req
->sg
, ptr
, len
, ns
->lbasz
,
1216 ns
->lbaf
.ms
, 0, dir
);
1219 return nvme_tx(n
, &req
->sg
, ptr
, len
, dir
);
1222 uint16_t nvme_bounce_mdata(NvmeCtrl
*n
, uint8_t *ptr
, uint32_t len
,
1223 NvmeTxDirection dir
, NvmeRequest
*req
)
1225 NvmeNamespace
*ns
= req
->ns
;
1228 if (nvme_ns_ext(ns
)) {
1229 return nvme_tx_interleaved(n
, &req
->sg
, ptr
, len
, ns
->lbaf
.ms
,
1230 ns
->lbasz
, ns
->lbasz
, dir
);
1233 nvme_sg_unmap(&req
->sg
);
1235 status
= nvme_map_mptr(n
, &req
->sg
, len
, &req
->cmd
);
1240 return nvme_tx(n
, &req
->sg
, ptr
, len
, dir
);
1243 static inline void nvme_blk_read(BlockBackend
*blk
, int64_t offset
,
1244 BlockCompletionFunc
*cb
, NvmeRequest
*req
)
1246 assert(req
->sg
.flags
& NVME_SG_ALLOC
);
1248 if (req
->sg
.flags
& NVME_SG_DMA
) {
1249 req
->aiocb
= dma_blk_read(blk
, &req
->sg
.qsg
, offset
, BDRV_SECTOR_SIZE
,
1252 req
->aiocb
= blk_aio_preadv(blk
, offset
, &req
->sg
.iov
, 0, cb
, req
);
1256 static inline void nvme_blk_write(BlockBackend
*blk
, int64_t offset
,
1257 BlockCompletionFunc
*cb
, NvmeRequest
*req
)
1259 assert(req
->sg
.flags
& NVME_SG_ALLOC
);
1261 if (req
->sg
.flags
& NVME_SG_DMA
) {
1262 req
->aiocb
= dma_blk_write(blk
, &req
->sg
.qsg
, offset
, BDRV_SECTOR_SIZE
,
1265 req
->aiocb
= blk_aio_pwritev(blk
, offset
, &req
->sg
.iov
, 0, cb
, req
);
1269 static void nvme_post_cqes(void *opaque
)
1271 NvmeCQueue
*cq
= opaque
;
1272 NvmeCtrl
*n
= cq
->ctrl
;
1273 NvmeRequest
*req
, *next
;
1274 bool pending
= cq
->head
!= cq
->tail
;
1277 QTAILQ_FOREACH_SAFE(req
, &cq
->req_list
, entry
, next
) {
1281 if (nvme_cq_full(cq
)) {
1286 req
->cqe
.status
= cpu_to_le16((req
->status
<< 1) | cq
->phase
);
1287 req
->cqe
.sq_id
= cpu_to_le16(sq
->sqid
);
1288 req
->cqe
.sq_head
= cpu_to_le16(sq
->head
);
1289 addr
= cq
->dma_addr
+ cq
->tail
* n
->cqe_size
;
1290 ret
= pci_dma_write(&n
->parent_obj
, addr
, (void *)&req
->cqe
,
1293 trace_pci_nvme_err_addr_write(addr
);
1294 trace_pci_nvme_err_cfs();
1295 stl_le_p(&n
->bar
.csts
, NVME_CSTS_FAILED
);
1298 QTAILQ_REMOVE(&cq
->req_list
, req
, entry
);
1299 nvme_inc_cq_tail(cq
);
1300 nvme_sg_unmap(&req
->sg
);
1301 QTAILQ_INSERT_TAIL(&sq
->req_list
, req
, entry
);
1303 if (cq
->tail
!= cq
->head
) {
1304 if (cq
->irq_enabled
&& !pending
) {
1308 nvme_irq_assert(n
, cq
);
1312 static void nvme_enqueue_req_completion(NvmeCQueue
*cq
, NvmeRequest
*req
)
1314 assert(cq
->cqid
== req
->sq
->cqid
);
1315 trace_pci_nvme_enqueue_req_completion(nvme_cid(req
), cq
->cqid
,
1316 le32_to_cpu(req
->cqe
.result
),
1317 le32_to_cpu(req
->cqe
.dw1
),
1321 trace_pci_nvme_err_req_status(nvme_cid(req
), nvme_nsid(req
->ns
),
1322 req
->status
, req
->cmd
.opcode
);
1325 QTAILQ_REMOVE(&req
->sq
->out_req_list
, req
, entry
);
1326 QTAILQ_INSERT_TAIL(&cq
->req_list
, req
, entry
);
1327 timer_mod(cq
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 500);
1330 static void nvme_process_aers(void *opaque
)
1332 NvmeCtrl
*n
= opaque
;
1333 NvmeAsyncEvent
*event
, *next
;
1335 trace_pci_nvme_process_aers(n
->aer_queued
);
1337 QTAILQ_FOREACH_SAFE(event
, &n
->aer_queue
, entry
, next
) {
1339 NvmeAerResult
*result
;
1341 /* can't post cqe if there is nothing to complete */
1342 if (!n
->outstanding_aers
) {
1343 trace_pci_nvme_no_outstanding_aers();
1347 /* ignore if masked (cqe posted, but event not cleared) */
1348 if (n
->aer_mask
& (1 << event
->result
.event_type
)) {
1349 trace_pci_nvme_aer_masked(event
->result
.event_type
, n
->aer_mask
);
1353 QTAILQ_REMOVE(&n
->aer_queue
, event
, entry
);
1356 n
->aer_mask
|= 1 << event
->result
.event_type
;
1357 n
->outstanding_aers
--;
1359 req
= n
->aer_reqs
[n
->outstanding_aers
];
1361 result
= (NvmeAerResult
*) &req
->cqe
.result
;
1362 result
->event_type
= event
->result
.event_type
;
1363 result
->event_info
= event
->result
.event_info
;
1364 result
->log_page
= event
->result
.log_page
;
1367 trace_pci_nvme_aer_post_cqe(result
->event_type
, result
->event_info
,
1370 nvme_enqueue_req_completion(&n
->admin_cq
, req
);
1374 static void nvme_enqueue_event(NvmeCtrl
*n
, uint8_t event_type
,
1375 uint8_t event_info
, uint8_t log_page
)
1377 NvmeAsyncEvent
*event
;
1379 trace_pci_nvme_enqueue_event(event_type
, event_info
, log_page
);
1381 if (n
->aer_queued
== n
->params
.aer_max_queued
) {
1382 trace_pci_nvme_enqueue_event_noqueue(n
->aer_queued
);
1386 event
= g_new(NvmeAsyncEvent
, 1);
1387 event
->result
= (NvmeAerResult
) {
1388 .event_type
= event_type
,
1389 .event_info
= event_info
,
1390 .log_page
= log_page
,
1393 QTAILQ_INSERT_TAIL(&n
->aer_queue
, event
, entry
);
1396 nvme_process_aers(n
);
1399 static void nvme_smart_event(NvmeCtrl
*n
, uint8_t event
)
1403 /* Ref SPEC <Asynchronous Event Information 0x2013 SMART / Health Status> */
1404 if (!(NVME_AEC_SMART(n
->features
.async_config
) & event
)) {
1409 case NVME_SMART_SPARE
:
1410 aer_info
= NVME_AER_INFO_SMART_SPARE_THRESH
;
1412 case NVME_SMART_TEMPERATURE
:
1413 aer_info
= NVME_AER_INFO_SMART_TEMP_THRESH
;
1415 case NVME_SMART_RELIABILITY
:
1416 case NVME_SMART_MEDIA_READ_ONLY
:
1417 case NVME_SMART_FAILED_VOLATILE_MEDIA
:
1418 case NVME_SMART_PMR_UNRELIABLE
:
1419 aer_info
= NVME_AER_INFO_SMART_RELIABILITY
;
1425 nvme_enqueue_event(n
, NVME_AER_TYPE_SMART
, aer_info
, NVME_LOG_SMART_INFO
);
1428 static void nvme_clear_events(NvmeCtrl
*n
, uint8_t event_type
)
1430 n
->aer_mask
&= ~(1 << event_type
);
1431 if (!QTAILQ_EMPTY(&n
->aer_queue
)) {
1432 nvme_process_aers(n
);
1436 static inline uint16_t nvme_check_mdts(NvmeCtrl
*n
, size_t len
)
1438 uint8_t mdts
= n
->params
.mdts
;
1440 if (mdts
&& len
> n
->page_size
<< mdts
) {
1441 trace_pci_nvme_err_mdts(len
);
1442 return NVME_INVALID_FIELD
| NVME_DNR
;
1445 return NVME_SUCCESS
;
1448 static inline uint16_t nvme_check_bounds(NvmeNamespace
*ns
, uint64_t slba
,
1451 uint64_t nsze
= le64_to_cpu(ns
->id_ns
.nsze
);
1453 if (unlikely(UINT64_MAX
- slba
< nlb
|| slba
+ nlb
> nsze
)) {
1454 trace_pci_nvme_err_invalid_lba_range(slba
, nlb
, nsze
);
1455 return NVME_LBA_RANGE
| NVME_DNR
;
1458 return NVME_SUCCESS
;
1461 static int nvme_block_status_all(NvmeNamespace
*ns
, uint64_t slba
,
1462 uint32_t nlb
, int flags
)
1464 BlockDriverState
*bs
= blk_bs(ns
->blkconf
.blk
);
1466 int64_t pnum
= 0, bytes
= nvme_l2b(ns
, nlb
);
1467 int64_t offset
= nvme_l2b(ns
, slba
);
1471 * `pnum` holds the number of bytes after offset that shares the same
1472 * allocation status as the byte at offset. If `pnum` is different from
1473 * `bytes`, we should check the allocation status of the next range and
1474 * continue this until all bytes have been checked.
1479 ret
= bdrv_block_status(bs
, offset
, bytes
, &pnum
, NULL
, NULL
);
1485 trace_pci_nvme_block_status(offset
, bytes
, pnum
, ret
,
1486 !!(ret
& BDRV_BLOCK_ZERO
));
1488 if (!(ret
& flags
)) {
1493 } while (pnum
!= bytes
);
1498 static uint16_t nvme_check_dulbe(NvmeNamespace
*ns
, uint64_t slba
,
1504 ret
= nvme_block_status_all(ns
, slba
, nlb
, BDRV_BLOCK_DATA
);
1507 error_setg_errno(&err
, -ret
, "unable to get block status");
1508 error_report_err(err
);
1510 return NVME_INTERNAL_DEV_ERROR
;
1516 return NVME_SUCCESS
;
1519 static void nvme_aio_err(NvmeRequest
*req
, int ret
)
1521 uint16_t status
= NVME_SUCCESS
;
1522 Error
*local_err
= NULL
;
1524 switch (req
->cmd
.opcode
) {
1526 status
= NVME_UNRECOVERED_READ
;
1528 case NVME_CMD_FLUSH
:
1529 case NVME_CMD_WRITE
:
1530 case NVME_CMD_WRITE_ZEROES
:
1531 case NVME_CMD_ZONE_APPEND
:
1532 status
= NVME_WRITE_FAULT
;
1535 status
= NVME_INTERNAL_DEV_ERROR
;
1539 trace_pci_nvme_err_aio(nvme_cid(req
), strerror(-ret
), status
);
1541 error_setg_errno(&local_err
, -ret
, "aio failed");
1542 error_report_err(local_err
);
1545 * Set the command status code to the first encountered error but allow a
1546 * subsequent Internal Device Error to trump it.
1548 if (req
->status
&& status
!= NVME_INTERNAL_DEV_ERROR
) {
1552 req
->status
= status
;
1555 static inline uint32_t nvme_zone_idx(NvmeNamespace
*ns
, uint64_t slba
)
1557 return ns
->zone_size_log2
> 0 ? slba
>> ns
->zone_size_log2
:
1558 slba
/ ns
->zone_size
;
1561 static inline NvmeZone
*nvme_get_zone_by_slba(NvmeNamespace
*ns
, uint64_t slba
)
1563 uint32_t zone_idx
= nvme_zone_idx(ns
, slba
);
1565 if (zone_idx
>= ns
->num_zones
) {
1569 return &ns
->zone_array
[zone_idx
];
1572 static uint16_t nvme_check_zone_state_for_write(NvmeZone
*zone
)
1574 uint64_t zslba
= zone
->d
.zslba
;
1576 switch (nvme_get_zone_state(zone
)) {
1577 case NVME_ZONE_STATE_EMPTY
:
1578 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
1579 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
1580 case NVME_ZONE_STATE_CLOSED
:
1581 return NVME_SUCCESS
;
1582 case NVME_ZONE_STATE_FULL
:
1583 trace_pci_nvme_err_zone_is_full(zslba
);
1584 return NVME_ZONE_FULL
;
1585 case NVME_ZONE_STATE_OFFLINE
:
1586 trace_pci_nvme_err_zone_is_offline(zslba
);
1587 return NVME_ZONE_OFFLINE
;
1588 case NVME_ZONE_STATE_READ_ONLY
:
1589 trace_pci_nvme_err_zone_is_read_only(zslba
);
1590 return NVME_ZONE_READ_ONLY
;
1595 return NVME_INTERNAL_DEV_ERROR
;
1598 static uint16_t nvme_check_zone_write(NvmeNamespace
*ns
, NvmeZone
*zone
,
1599 uint64_t slba
, uint32_t nlb
)
1601 uint64_t zcap
= nvme_zone_wr_boundary(zone
);
1604 status
= nvme_check_zone_state_for_write(zone
);
1609 if (unlikely(slba
!= zone
->w_ptr
)) {
1610 trace_pci_nvme_err_write_not_at_wp(slba
, zone
->d
.zslba
, zone
->w_ptr
);
1611 return NVME_ZONE_INVALID_WRITE
;
1614 if (unlikely((slba
+ nlb
) > zcap
)) {
1615 trace_pci_nvme_err_zone_boundary(slba
, nlb
, zcap
);
1616 return NVME_ZONE_BOUNDARY_ERROR
;
1619 return NVME_SUCCESS
;
1622 static uint16_t nvme_check_zone_state_for_read(NvmeZone
*zone
)
1624 switch (nvme_get_zone_state(zone
)) {
1625 case NVME_ZONE_STATE_EMPTY
:
1626 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
1627 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
1628 case NVME_ZONE_STATE_FULL
:
1629 case NVME_ZONE_STATE_CLOSED
:
1630 case NVME_ZONE_STATE_READ_ONLY
:
1631 return NVME_SUCCESS
;
1632 case NVME_ZONE_STATE_OFFLINE
:
1633 trace_pci_nvme_err_zone_is_offline(zone
->d
.zslba
);
1634 return NVME_ZONE_OFFLINE
;
1639 return NVME_INTERNAL_DEV_ERROR
;
1642 static uint16_t nvme_check_zone_read(NvmeNamespace
*ns
, uint64_t slba
,
1646 uint64_t bndry
, end
;
1649 zone
= nvme_get_zone_by_slba(ns
, slba
);
1652 bndry
= nvme_zone_rd_boundary(ns
, zone
);
1655 status
= nvme_check_zone_state_for_read(zone
);
1658 } else if (unlikely(end
> bndry
)) {
1659 if (!ns
->params
.cross_zone_read
) {
1660 status
= NVME_ZONE_BOUNDARY_ERROR
;
1663 * Read across zone boundary - check that all subsequent
1664 * zones that are being read have an appropriate state.
1668 status
= nvme_check_zone_state_for_read(zone
);
1672 } while (end
> nvme_zone_rd_boundary(ns
, zone
));
1679 static uint16_t nvme_zrm_finish(NvmeNamespace
*ns
, NvmeZone
*zone
)
1681 switch (nvme_get_zone_state(zone
)) {
1682 case NVME_ZONE_STATE_FULL
:
1683 return NVME_SUCCESS
;
1685 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
1686 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
1687 nvme_aor_dec_open(ns
);
1689 case NVME_ZONE_STATE_CLOSED
:
1690 nvme_aor_dec_active(ns
);
1692 case NVME_ZONE_STATE_EMPTY
:
1693 nvme_assign_zone_state(ns
, zone
, NVME_ZONE_STATE_FULL
);
1694 return NVME_SUCCESS
;
1697 return NVME_ZONE_INVAL_TRANSITION
;
1701 static uint16_t nvme_zrm_close(NvmeNamespace
*ns
, NvmeZone
*zone
)
1703 switch (nvme_get_zone_state(zone
)) {
1704 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
1705 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
1706 nvme_aor_dec_open(ns
);
1707 nvme_assign_zone_state(ns
, zone
, NVME_ZONE_STATE_CLOSED
);
1709 case NVME_ZONE_STATE_CLOSED
:
1710 return NVME_SUCCESS
;
1713 return NVME_ZONE_INVAL_TRANSITION
;
1717 static uint16_t nvme_zrm_reset(NvmeNamespace
*ns
, NvmeZone
*zone
)
1719 switch (nvme_get_zone_state(zone
)) {
1720 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
1721 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
1722 nvme_aor_dec_open(ns
);
1724 case NVME_ZONE_STATE_CLOSED
:
1725 nvme_aor_dec_active(ns
);
1727 case NVME_ZONE_STATE_FULL
:
1728 zone
->w_ptr
= zone
->d
.zslba
;
1729 zone
->d
.wp
= zone
->w_ptr
;
1730 nvme_assign_zone_state(ns
, zone
, NVME_ZONE_STATE_EMPTY
);
1732 case NVME_ZONE_STATE_EMPTY
:
1733 return NVME_SUCCESS
;
1736 return NVME_ZONE_INVAL_TRANSITION
;
1740 static void nvme_zrm_auto_transition_zone(NvmeNamespace
*ns
)
1744 if (ns
->params
.max_open_zones
&&
1745 ns
->nr_open_zones
== ns
->params
.max_open_zones
) {
1746 zone
= QTAILQ_FIRST(&ns
->imp_open_zones
);
1749 * Automatically close this implicitly open zone.
1751 QTAILQ_REMOVE(&ns
->imp_open_zones
, zone
, entry
);
1752 nvme_zrm_close(ns
, zone
);
1758 NVME_ZRM_AUTO
= 1 << 0,
1761 static uint16_t nvme_zrm_open_flags(NvmeCtrl
*n
, NvmeNamespace
*ns
,
1762 NvmeZone
*zone
, int flags
)
1767 switch (nvme_get_zone_state(zone
)) {
1768 case NVME_ZONE_STATE_EMPTY
:
1773 case NVME_ZONE_STATE_CLOSED
:
1774 if (n
->params
.auto_transition_zones
) {
1775 nvme_zrm_auto_transition_zone(ns
);
1777 status
= nvme_aor_check(ns
, act
, 1);
1783 nvme_aor_inc_active(ns
);
1786 nvme_aor_inc_open(ns
);
1788 if (flags
& NVME_ZRM_AUTO
) {
1789 nvme_assign_zone_state(ns
, zone
, NVME_ZONE_STATE_IMPLICITLY_OPEN
);
1790 return NVME_SUCCESS
;
1795 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
1796 if (flags
& NVME_ZRM_AUTO
) {
1797 return NVME_SUCCESS
;
1800 nvme_assign_zone_state(ns
, zone
, NVME_ZONE_STATE_EXPLICITLY_OPEN
);
1804 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
1805 return NVME_SUCCESS
;
1808 return NVME_ZONE_INVAL_TRANSITION
;
1812 static inline uint16_t nvme_zrm_auto(NvmeCtrl
*n
, NvmeNamespace
*ns
,
1815 return nvme_zrm_open_flags(n
, ns
, zone
, NVME_ZRM_AUTO
);
1818 static inline uint16_t nvme_zrm_open(NvmeCtrl
*n
, NvmeNamespace
*ns
,
1821 return nvme_zrm_open_flags(n
, ns
, zone
, 0);
1824 static void nvme_advance_zone_wp(NvmeNamespace
*ns
, NvmeZone
*zone
,
1829 if (zone
->d
.wp
== nvme_zone_wr_boundary(zone
)) {
1830 nvme_zrm_finish(ns
, zone
);
1834 static void nvme_finalize_zoned_write(NvmeNamespace
*ns
, NvmeRequest
*req
)
1836 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
1841 slba
= le64_to_cpu(rw
->slba
);
1842 nlb
= le16_to_cpu(rw
->nlb
) + 1;
1843 zone
= nvme_get_zone_by_slba(ns
, slba
);
1846 nvme_advance_zone_wp(ns
, zone
, nlb
);
1849 static inline bool nvme_is_write(NvmeRequest
*req
)
1851 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
1853 return rw
->opcode
== NVME_CMD_WRITE
||
1854 rw
->opcode
== NVME_CMD_ZONE_APPEND
||
1855 rw
->opcode
== NVME_CMD_WRITE_ZEROES
;
1858 static AioContext
*nvme_get_aio_context(BlockAIOCB
*acb
)
1860 return qemu_get_aio_context();
1863 static void nvme_misc_cb(void *opaque
, int ret
)
1865 NvmeRequest
*req
= opaque
;
1867 trace_pci_nvme_misc_cb(nvme_cid(req
));
1870 nvme_aio_err(req
, ret
);
1873 nvme_enqueue_req_completion(nvme_cq(req
), req
);
1876 void nvme_rw_complete_cb(void *opaque
, int ret
)
1878 NvmeRequest
*req
= opaque
;
1879 NvmeNamespace
*ns
= req
->ns
;
1880 BlockBackend
*blk
= ns
->blkconf
.blk
;
1881 BlockAcctCookie
*acct
= &req
->acct
;
1882 BlockAcctStats
*stats
= blk_get_stats(blk
);
1884 trace_pci_nvme_rw_complete_cb(nvme_cid(req
), blk_name(blk
));
1887 block_acct_failed(stats
, acct
);
1888 nvme_aio_err(req
, ret
);
1890 block_acct_done(stats
, acct
);
1893 if (ns
->params
.zoned
&& nvme_is_write(req
)) {
1894 nvme_finalize_zoned_write(ns
, req
);
1897 nvme_enqueue_req_completion(nvme_cq(req
), req
);
1900 static void nvme_rw_cb(void *opaque
, int ret
)
1902 NvmeRequest
*req
= opaque
;
1903 NvmeNamespace
*ns
= req
->ns
;
1905 BlockBackend
*blk
= ns
->blkconf
.blk
;
1907 trace_pci_nvme_rw_cb(nvme_cid(req
), blk_name(blk
));
1914 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
1915 uint64_t slba
= le64_to_cpu(rw
->slba
);
1916 uint32_t nlb
= (uint32_t)le16_to_cpu(rw
->nlb
) + 1;
1917 uint64_t offset
= nvme_moff(ns
, slba
);
1919 if (req
->cmd
.opcode
== NVME_CMD_WRITE_ZEROES
) {
1920 size_t mlen
= nvme_m2b(ns
, nlb
);
1922 req
->aiocb
= blk_aio_pwrite_zeroes(blk
, offset
, mlen
,
1924 nvme_rw_complete_cb
, req
);
1928 if (nvme_ns_ext(ns
) || req
->cmd
.mptr
) {
1931 nvme_sg_unmap(&req
->sg
);
1932 status
= nvme_map_mdata(nvme_ctrl(req
), nlb
, req
);
1938 if (req
->cmd
.opcode
== NVME_CMD_READ
) {
1939 return nvme_blk_read(blk
, offset
, nvme_rw_complete_cb
, req
);
1942 return nvme_blk_write(blk
, offset
, nvme_rw_complete_cb
, req
);
1947 nvme_rw_complete_cb(req
, ret
);
1950 static void nvme_verify_cb(void *opaque
, int ret
)
1952 NvmeBounceContext
*ctx
= opaque
;
1953 NvmeRequest
*req
= ctx
->req
;
1954 NvmeNamespace
*ns
= req
->ns
;
1955 BlockBackend
*blk
= ns
->blkconf
.blk
;
1956 BlockAcctCookie
*acct
= &req
->acct
;
1957 BlockAcctStats
*stats
= blk_get_stats(blk
);
1958 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
1959 uint64_t slba
= le64_to_cpu(rw
->slba
);
1960 uint8_t prinfo
= NVME_RW_PRINFO(le16_to_cpu(rw
->control
));
1961 uint16_t apptag
= le16_to_cpu(rw
->apptag
);
1962 uint16_t appmask
= le16_to_cpu(rw
->appmask
);
1963 uint32_t reftag
= le32_to_cpu(rw
->reftag
);
1966 trace_pci_nvme_verify_cb(nvme_cid(req
), prinfo
, apptag
, appmask
, reftag
);
1969 block_acct_failed(stats
, acct
);
1970 nvme_aio_err(req
, ret
);
1974 block_acct_done(stats
, acct
);
1976 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
1977 status
= nvme_dif_mangle_mdata(ns
, ctx
->mdata
.bounce
,
1978 ctx
->mdata
.iov
.size
, slba
);
1980 req
->status
= status
;
1984 req
->status
= nvme_dif_check(ns
, ctx
->data
.bounce
, ctx
->data
.iov
.size
,
1985 ctx
->mdata
.bounce
, ctx
->mdata
.iov
.size
,
1986 prinfo
, slba
, apptag
, appmask
, &reftag
);
1990 qemu_iovec_destroy(&ctx
->data
.iov
);
1991 g_free(ctx
->data
.bounce
);
1993 qemu_iovec_destroy(&ctx
->mdata
.iov
);
1994 g_free(ctx
->mdata
.bounce
);
1998 nvme_enqueue_req_completion(nvme_cq(req
), req
);
2002 static void nvme_verify_mdata_in_cb(void *opaque
, int ret
)
2004 NvmeBounceContext
*ctx
= opaque
;
2005 NvmeRequest
*req
= ctx
->req
;
2006 NvmeNamespace
*ns
= req
->ns
;
2007 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
2008 uint64_t slba
= le64_to_cpu(rw
->slba
);
2009 uint32_t nlb
= le16_to_cpu(rw
->nlb
) + 1;
2010 size_t mlen
= nvme_m2b(ns
, nlb
);
2011 uint64_t offset
= nvme_moff(ns
, slba
);
2012 BlockBackend
*blk
= ns
->blkconf
.blk
;
2014 trace_pci_nvme_verify_mdata_in_cb(nvme_cid(req
), blk_name(blk
));
2020 ctx
->mdata
.bounce
= g_malloc(mlen
);
2022 qemu_iovec_reset(&ctx
->mdata
.iov
);
2023 qemu_iovec_add(&ctx
->mdata
.iov
, ctx
->mdata
.bounce
, mlen
);
2025 req
->aiocb
= blk_aio_preadv(blk
, offset
, &ctx
->mdata
.iov
, 0,
2026 nvme_verify_cb
, ctx
);
2030 nvme_verify_cb(ctx
, ret
);
2033 struct nvme_compare_ctx
{
2045 static void nvme_compare_mdata_cb(void *opaque
, int ret
)
2047 NvmeRequest
*req
= opaque
;
2048 NvmeNamespace
*ns
= req
->ns
;
2049 NvmeCtrl
*n
= nvme_ctrl(req
);
2050 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
2051 uint8_t prinfo
= NVME_RW_PRINFO(le16_to_cpu(rw
->control
));
2052 uint16_t apptag
= le16_to_cpu(rw
->apptag
);
2053 uint16_t appmask
= le16_to_cpu(rw
->appmask
);
2054 uint32_t reftag
= le32_to_cpu(rw
->reftag
);
2055 struct nvme_compare_ctx
*ctx
= req
->opaque
;
2056 g_autofree
uint8_t *buf
= NULL
;
2057 BlockBackend
*blk
= ns
->blkconf
.blk
;
2058 BlockAcctCookie
*acct
= &req
->acct
;
2059 BlockAcctStats
*stats
= blk_get_stats(blk
);
2060 uint16_t status
= NVME_SUCCESS
;
2062 trace_pci_nvme_compare_mdata_cb(nvme_cid(req
));
2065 block_acct_failed(stats
, acct
);
2066 nvme_aio_err(req
, ret
);
2070 buf
= g_malloc(ctx
->mdata
.iov
.size
);
2072 status
= nvme_bounce_mdata(n
, buf
, ctx
->mdata
.iov
.size
,
2073 NVME_TX_DIRECTION_TO_DEVICE
, req
);
2075 req
->status
= status
;
2079 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
2080 uint64_t slba
= le64_to_cpu(rw
->slba
);
2082 uint8_t *mbufp
= ctx
->mdata
.bounce
;
2083 uint8_t *end
= mbufp
+ ctx
->mdata
.iov
.size
;
2086 status
= nvme_dif_check(ns
, ctx
->data
.bounce
, ctx
->data
.iov
.size
,
2087 ctx
->mdata
.bounce
, ctx
->mdata
.iov
.size
, prinfo
,
2088 slba
, apptag
, appmask
, &reftag
);
2090 req
->status
= status
;
2095 * When formatted with protection information, do not compare the DIF
2098 if (!(ns
->id_ns
.dps
& NVME_ID_NS_DPS_FIRST_EIGHT
)) {
2099 pil
= ns
->lbaf
.ms
- sizeof(NvmeDifTuple
);
2102 for (bufp
= buf
; mbufp
< end
; bufp
+= ns
->lbaf
.ms
, mbufp
+= ns
->lbaf
.ms
) {
2103 if (memcmp(bufp
+ pil
, mbufp
+ pil
, ns
->lbaf
.ms
- pil
)) {
2104 req
->status
= NVME_CMP_FAILURE
;
2112 if (memcmp(buf
, ctx
->mdata
.bounce
, ctx
->mdata
.iov
.size
)) {
2113 req
->status
= NVME_CMP_FAILURE
;
2117 block_acct_done(stats
, acct
);
2120 qemu_iovec_destroy(&ctx
->data
.iov
);
2121 g_free(ctx
->data
.bounce
);
2123 qemu_iovec_destroy(&ctx
->mdata
.iov
);
2124 g_free(ctx
->mdata
.bounce
);
2128 nvme_enqueue_req_completion(nvme_cq(req
), req
);
2131 static void nvme_compare_data_cb(void *opaque
, int ret
)
2133 NvmeRequest
*req
= opaque
;
2134 NvmeCtrl
*n
= nvme_ctrl(req
);
2135 NvmeNamespace
*ns
= req
->ns
;
2136 BlockBackend
*blk
= ns
->blkconf
.blk
;
2137 BlockAcctCookie
*acct
= &req
->acct
;
2138 BlockAcctStats
*stats
= blk_get_stats(blk
);
2140 struct nvme_compare_ctx
*ctx
= req
->opaque
;
2141 g_autofree
uint8_t *buf
= NULL
;
2144 trace_pci_nvme_compare_data_cb(nvme_cid(req
));
2147 block_acct_failed(stats
, acct
);
2148 nvme_aio_err(req
, ret
);
2152 buf
= g_malloc(ctx
->data
.iov
.size
);
2154 status
= nvme_bounce_data(n
, buf
, ctx
->data
.iov
.size
,
2155 NVME_TX_DIRECTION_TO_DEVICE
, req
);
2157 req
->status
= status
;
2161 if (memcmp(buf
, ctx
->data
.bounce
, ctx
->data
.iov
.size
)) {
2162 req
->status
= NVME_CMP_FAILURE
;
2167 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
2168 uint64_t slba
= le64_to_cpu(rw
->slba
);
2169 uint32_t nlb
= le16_to_cpu(rw
->nlb
) + 1;
2170 size_t mlen
= nvme_m2b(ns
, nlb
);
2171 uint64_t offset
= nvme_moff(ns
, slba
);
2173 ctx
->mdata
.bounce
= g_malloc(mlen
);
2175 qemu_iovec_init(&ctx
->mdata
.iov
, 1);
2176 qemu_iovec_add(&ctx
->mdata
.iov
, ctx
->mdata
.bounce
, mlen
);
2178 req
->aiocb
= blk_aio_preadv(blk
, offset
, &ctx
->mdata
.iov
, 0,
2179 nvme_compare_mdata_cb
, req
);
2183 block_acct_done(stats
, acct
);
2186 qemu_iovec_destroy(&ctx
->data
.iov
);
2187 g_free(ctx
->data
.bounce
);
2190 nvme_enqueue_req_completion(nvme_cq(req
), req
);
2193 typedef struct NvmeDSMAIOCB
{
2200 NvmeDsmRange
*range
;
2205 static void nvme_dsm_cancel(BlockAIOCB
*aiocb
)
2207 NvmeDSMAIOCB
*iocb
= container_of(aiocb
, NvmeDSMAIOCB
, common
);
2209 /* break nvme_dsm_cb loop */
2210 iocb
->idx
= iocb
->nr
;
2211 iocb
->ret
= -ECANCELED
;
2214 blk_aio_cancel_async(iocb
->aiocb
);
2218 * We only reach this if nvme_dsm_cancel() has already been called or
2219 * the command ran to completion and nvme_dsm_bh is scheduled to run.
2221 assert(iocb
->idx
== iocb
->nr
);
2225 static const AIOCBInfo nvme_dsm_aiocb_info
= {
2226 .aiocb_size
= sizeof(NvmeDSMAIOCB
),
2227 .cancel_async
= nvme_dsm_cancel
,
2230 static void nvme_dsm_bh(void *opaque
)
2232 NvmeDSMAIOCB
*iocb
= opaque
;
2234 iocb
->common
.cb(iocb
->common
.opaque
, iocb
->ret
);
2236 qemu_bh_delete(iocb
->bh
);
2238 qemu_aio_unref(iocb
);
2241 static void nvme_dsm_cb(void *opaque
, int ret
);
2243 static void nvme_dsm_md_cb(void *opaque
, int ret
)
2245 NvmeDSMAIOCB
*iocb
= opaque
;
2246 NvmeRequest
*req
= iocb
->req
;
2247 NvmeNamespace
*ns
= req
->ns
;
2248 NvmeDsmRange
*range
;
2258 nvme_dsm_cb(iocb
, 0);
2262 range
= &iocb
->range
[iocb
->idx
- 1];
2263 slba
= le64_to_cpu(range
->slba
);
2264 nlb
= le32_to_cpu(range
->nlb
);
2267 * Check that all block were discarded (zeroed); otherwise we do not zero
2271 ret
= nvme_block_status_all(ns
, slba
, nlb
, BDRV_BLOCK_ZERO
);
2278 nvme_dsm_cb(iocb
, 0);
2281 iocb
->aiocb
= blk_aio_pwrite_zeroes(ns
->blkconf
.blk
, nvme_moff(ns
, slba
),
2282 nvme_m2b(ns
, nlb
), BDRV_REQ_MAY_UNMAP
,
2288 qemu_bh_schedule(iocb
->bh
);
2291 static void nvme_dsm_cb(void *opaque
, int ret
)
2293 NvmeDSMAIOCB
*iocb
= opaque
;
2294 NvmeRequest
*req
= iocb
->req
;
2295 NvmeCtrl
*n
= nvme_ctrl(req
);
2296 NvmeNamespace
*ns
= req
->ns
;
2297 NvmeDsmRange
*range
;
2307 if (iocb
->idx
== iocb
->nr
) {
2311 range
= &iocb
->range
[iocb
->idx
++];
2312 slba
= le64_to_cpu(range
->slba
);
2313 nlb
= le32_to_cpu(range
->nlb
);
2315 trace_pci_nvme_dsm_deallocate(slba
, nlb
);
2317 if (nlb
> n
->dmrsl
) {
2318 trace_pci_nvme_dsm_single_range_limit_exceeded(nlb
, n
->dmrsl
);
2322 if (nvme_check_bounds(ns
, slba
, nlb
)) {
2323 trace_pci_nvme_err_invalid_lba_range(slba
, nlb
,
2328 iocb
->aiocb
= blk_aio_pdiscard(ns
->blkconf
.blk
, nvme_l2b(ns
, slba
),
2330 nvme_dsm_md_cb
, iocb
);
2335 qemu_bh_schedule(iocb
->bh
);
2338 static uint16_t nvme_dsm(NvmeCtrl
*n
, NvmeRequest
*req
)
2340 NvmeNamespace
*ns
= req
->ns
;
2341 NvmeDsmCmd
*dsm
= (NvmeDsmCmd
*) &req
->cmd
;
2342 uint32_t attr
= le32_to_cpu(dsm
->attributes
);
2343 uint32_t nr
= (le32_to_cpu(dsm
->nr
) & 0xff) + 1;
2344 uint16_t status
= NVME_SUCCESS
;
2346 trace_pci_nvme_dsm(nr
, attr
);
2348 if (attr
& NVME_DSMGMT_AD
) {
2349 NvmeDSMAIOCB
*iocb
= blk_aio_get(&nvme_dsm_aiocb_info
, ns
->blkconf
.blk
,
2353 iocb
->bh
= qemu_bh_new(nvme_dsm_bh
, iocb
);
2355 iocb
->range
= g_new(NvmeDsmRange
, nr
);
2359 status
= nvme_h2c(n
, (uint8_t *)iocb
->range
, sizeof(NvmeDsmRange
) * nr
,
2365 req
->aiocb
= &iocb
->common
;
2366 nvme_dsm_cb(iocb
, 0);
2368 return NVME_NO_COMPLETE
;
2374 static uint16_t nvme_verify(NvmeCtrl
*n
, NvmeRequest
*req
)
2376 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
2377 NvmeNamespace
*ns
= req
->ns
;
2378 BlockBackend
*blk
= ns
->blkconf
.blk
;
2379 uint64_t slba
= le64_to_cpu(rw
->slba
);
2380 uint32_t nlb
= le16_to_cpu(rw
->nlb
) + 1;
2381 size_t len
= nvme_l2b(ns
, nlb
);
2382 int64_t offset
= nvme_l2b(ns
, slba
);
2383 uint8_t prinfo
= NVME_RW_PRINFO(le16_to_cpu(rw
->control
));
2384 uint32_t reftag
= le32_to_cpu(rw
->reftag
);
2385 NvmeBounceContext
*ctx
= NULL
;
2388 trace_pci_nvme_verify(nvme_cid(req
), nvme_nsid(ns
), slba
, nlb
);
2390 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
2391 status
= nvme_check_prinfo(ns
, prinfo
, slba
, reftag
);
2396 if (prinfo
& NVME_PRINFO_PRACT
) {
2397 return NVME_INVALID_PROT_INFO
| NVME_DNR
;
2401 if (len
> n
->page_size
<< n
->params
.vsl
) {
2402 return NVME_INVALID_FIELD
| NVME_DNR
;
2405 status
= nvme_check_bounds(ns
, slba
, nlb
);
2410 if (NVME_ERR_REC_DULBE(ns
->features
.err_rec
)) {
2411 status
= nvme_check_dulbe(ns
, slba
, nlb
);
2417 ctx
= g_new0(NvmeBounceContext
, 1);
2420 ctx
->data
.bounce
= g_malloc(len
);
2422 qemu_iovec_init(&ctx
->data
.iov
, 1);
2423 qemu_iovec_add(&ctx
->data
.iov
, ctx
->data
.bounce
, len
);
2425 block_acct_start(blk_get_stats(blk
), &req
->acct
, ctx
->data
.iov
.size
,
2428 req
->aiocb
= blk_aio_preadv(ns
->blkconf
.blk
, offset
, &ctx
->data
.iov
, 0,
2429 nvme_verify_mdata_in_cb
, ctx
);
2430 return NVME_NO_COMPLETE
;
2433 typedef struct NvmeCopyAIOCB
{
2440 NvmeCopySourceRange
*ranges
;
2447 BlockAcctCookie read
;
2448 BlockAcctCookie write
;
2457 static void nvme_copy_cancel(BlockAIOCB
*aiocb
)
2459 NvmeCopyAIOCB
*iocb
= container_of(aiocb
, NvmeCopyAIOCB
, common
);
2461 iocb
->ret
= -ECANCELED
;
2464 blk_aio_cancel_async(iocb
->aiocb
);
2469 static const AIOCBInfo nvme_copy_aiocb_info
= {
2470 .aiocb_size
= sizeof(NvmeCopyAIOCB
),
2471 .cancel_async
= nvme_copy_cancel
,
2474 static void nvme_copy_bh(void *opaque
)
2476 NvmeCopyAIOCB
*iocb
= opaque
;
2477 NvmeRequest
*req
= iocb
->req
;
2478 NvmeNamespace
*ns
= req
->ns
;
2479 BlockAcctStats
*stats
= blk_get_stats(ns
->blkconf
.blk
);
2481 if (iocb
->idx
!= iocb
->nr
) {
2482 req
->cqe
.result
= cpu_to_le32(iocb
->idx
);
2485 qemu_iovec_destroy(&iocb
->iov
);
2486 g_free(iocb
->bounce
);
2488 qemu_bh_delete(iocb
->bh
);
2491 if (iocb
->ret
< 0) {
2492 block_acct_failed(stats
, &iocb
->acct
.read
);
2493 block_acct_failed(stats
, &iocb
->acct
.write
);
2495 block_acct_done(stats
, &iocb
->acct
.read
);
2496 block_acct_done(stats
, &iocb
->acct
.write
);
2499 iocb
->common
.cb(iocb
->common
.opaque
, iocb
->ret
);
2500 qemu_aio_unref(iocb
);
2503 static void nvme_copy_cb(void *opaque
, int ret
);
2505 static void nvme_copy_out_completed_cb(void *opaque
, int ret
)
2507 NvmeCopyAIOCB
*iocb
= opaque
;
2508 NvmeRequest
*req
= iocb
->req
;
2509 NvmeNamespace
*ns
= req
->ns
;
2510 NvmeCopySourceRange
*range
= &iocb
->ranges
[iocb
->idx
];
2511 uint32_t nlb
= le32_to_cpu(range
->nlb
) + 1;
2516 } else if (iocb
->ret
< 0) {
2520 if (ns
->params
.zoned
) {
2521 nvme_advance_zone_wp(ns
, iocb
->zone
, nlb
);
2527 nvme_copy_cb(iocb
, iocb
->ret
);
2530 static void nvme_copy_out_cb(void *opaque
, int ret
)
2532 NvmeCopyAIOCB
*iocb
= opaque
;
2533 NvmeRequest
*req
= iocb
->req
;
2534 NvmeNamespace
*ns
= req
->ns
;
2535 NvmeCopySourceRange
*range
;
2543 } else if (iocb
->ret
< 0) {
2548 nvme_copy_out_completed_cb(iocb
, 0);
2552 range
= &iocb
->ranges
[iocb
->idx
];
2553 nlb
= le32_to_cpu(range
->nlb
) + 1;
2555 mlen
= nvme_m2b(ns
, nlb
);
2556 mbounce
= iocb
->bounce
+ nvme_l2b(ns
, nlb
);
2558 qemu_iovec_reset(&iocb
->iov
);
2559 qemu_iovec_add(&iocb
->iov
, mbounce
, mlen
);
2561 iocb
->aiocb
= blk_aio_pwritev(ns
->blkconf
.blk
, nvme_moff(ns
, iocb
->slba
),
2562 &iocb
->iov
, 0, nvme_copy_out_completed_cb
,
2568 nvme_copy_cb(iocb
, ret
);
2571 static void nvme_copy_in_completed_cb(void *opaque
, int ret
)
2573 NvmeCopyAIOCB
*iocb
= opaque
;
2574 NvmeRequest
*req
= iocb
->req
;
2575 NvmeNamespace
*ns
= req
->ns
;
2576 NvmeCopySourceRange
*range
;
2584 } else if (iocb
->ret
< 0) {
2588 range
= &iocb
->ranges
[iocb
->idx
];
2589 nlb
= le32_to_cpu(range
->nlb
) + 1;
2590 len
= nvme_l2b(ns
, nlb
);
2592 trace_pci_nvme_copy_out(iocb
->slba
, nlb
);
2594 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
2595 NvmeCopyCmd
*copy
= (NvmeCopyCmd
*)&req
->cmd
;
2597 uint16_t prinfor
= ((copy
->control
[0] >> 4) & 0xf);
2598 uint16_t prinfow
= ((copy
->control
[2] >> 2) & 0xf);
2600 uint16_t apptag
= le16_to_cpu(range
->apptag
);
2601 uint16_t appmask
= le16_to_cpu(range
->appmask
);
2602 uint32_t reftag
= le32_to_cpu(range
->reftag
);
2604 uint64_t slba
= le64_to_cpu(range
->slba
);
2605 size_t mlen
= nvme_m2b(ns
, nlb
);
2606 uint8_t *mbounce
= iocb
->bounce
+ nvme_l2b(ns
, nlb
);
2608 status
= nvme_dif_check(ns
, iocb
->bounce
, len
, mbounce
, mlen
, prinfor
,
2609 slba
, apptag
, appmask
, &reftag
);
2614 apptag
= le16_to_cpu(copy
->apptag
);
2615 appmask
= le16_to_cpu(copy
->appmask
);
2617 if (prinfow
& NVME_PRINFO_PRACT
) {
2618 status
= nvme_check_prinfo(ns
, prinfow
, iocb
->slba
, iocb
->reftag
);
2623 nvme_dif_pract_generate_dif(ns
, iocb
->bounce
, len
, mbounce
, mlen
,
2624 apptag
, &iocb
->reftag
);
2626 status
= nvme_dif_check(ns
, iocb
->bounce
, len
, mbounce
, mlen
,
2627 prinfow
, iocb
->slba
, apptag
, appmask
,
2635 status
= nvme_check_bounds(ns
, iocb
->slba
, nlb
);
2640 if (ns
->params
.zoned
) {
2641 status
= nvme_check_zone_write(ns
, iocb
->zone
, iocb
->slba
, nlb
);
2646 iocb
->zone
->w_ptr
+= nlb
;
2649 qemu_iovec_reset(&iocb
->iov
);
2650 qemu_iovec_add(&iocb
->iov
, iocb
->bounce
, len
);
2652 iocb
->aiocb
= blk_aio_pwritev(ns
->blkconf
.blk
, nvme_l2b(ns
, iocb
->slba
),
2653 &iocb
->iov
, 0, nvme_copy_out_cb
, iocb
);
2658 req
->status
= status
;
2661 qemu_bh_schedule(iocb
->bh
);
2667 nvme_copy_cb(iocb
, ret
);
2670 static void nvme_copy_in_cb(void *opaque
, int ret
)
2672 NvmeCopyAIOCB
*iocb
= opaque
;
2673 NvmeRequest
*req
= iocb
->req
;
2674 NvmeNamespace
*ns
= req
->ns
;
2675 NvmeCopySourceRange
*range
;
2682 } else if (iocb
->ret
< 0) {
2687 nvme_copy_in_completed_cb(iocb
, 0);
2691 range
= &iocb
->ranges
[iocb
->idx
];
2692 slba
= le64_to_cpu(range
->slba
);
2693 nlb
= le32_to_cpu(range
->nlb
) + 1;
2695 qemu_iovec_reset(&iocb
->iov
);
2696 qemu_iovec_add(&iocb
->iov
, iocb
->bounce
+ nvme_l2b(ns
, nlb
),
2699 iocb
->aiocb
= blk_aio_preadv(ns
->blkconf
.blk
, nvme_moff(ns
, slba
),
2700 &iocb
->iov
, 0, nvme_copy_in_completed_cb
,
2705 nvme_copy_cb(iocb
, iocb
->ret
);
2708 static void nvme_copy_cb(void *opaque
, int ret
)
2710 NvmeCopyAIOCB
*iocb
= opaque
;
2711 NvmeRequest
*req
= iocb
->req
;
2712 NvmeNamespace
*ns
= req
->ns
;
2713 NvmeCopySourceRange
*range
;
2722 } else if (iocb
->ret
< 0) {
2726 if (iocb
->idx
== iocb
->nr
) {
2730 range
= &iocb
->ranges
[iocb
->idx
];
2731 slba
= le64_to_cpu(range
->slba
);
2732 nlb
= le32_to_cpu(range
->nlb
) + 1;
2733 len
= nvme_l2b(ns
, nlb
);
2735 trace_pci_nvme_copy_source_range(slba
, nlb
);
2737 if (nlb
> le16_to_cpu(ns
->id_ns
.mssrl
)) {
2738 status
= NVME_CMD_SIZE_LIMIT
| NVME_DNR
;
2742 status
= nvme_check_bounds(ns
, slba
, nlb
);
2747 if (NVME_ERR_REC_DULBE(ns
->features
.err_rec
)) {
2748 status
= nvme_check_dulbe(ns
, slba
, nlb
);
2754 if (ns
->params
.zoned
) {
2755 status
= nvme_check_zone_read(ns
, slba
, nlb
);
2761 qemu_iovec_reset(&iocb
->iov
);
2762 qemu_iovec_add(&iocb
->iov
, iocb
->bounce
, len
);
2764 iocb
->aiocb
= blk_aio_preadv(ns
->blkconf
.blk
, nvme_l2b(ns
, slba
),
2765 &iocb
->iov
, 0, nvme_copy_in_cb
, iocb
);
2769 req
->status
= status
;
2773 qemu_bh_schedule(iocb
->bh
);
2778 static uint16_t nvme_copy(NvmeCtrl
*n
, NvmeRequest
*req
)
2780 NvmeNamespace
*ns
= req
->ns
;
2781 NvmeCopyCmd
*copy
= (NvmeCopyCmd
*)&req
->cmd
;
2782 NvmeCopyAIOCB
*iocb
= blk_aio_get(&nvme_copy_aiocb_info
, ns
->blkconf
.blk
,
2784 uint16_t nr
= copy
->nr
+ 1;
2785 uint8_t format
= copy
->control
[0] & 0xf;
2786 uint16_t prinfor
= ((copy
->control
[0] >> 4) & 0xf);
2787 uint16_t prinfow
= ((copy
->control
[2] >> 2) & 0xf);
2791 trace_pci_nvme_copy(nvme_cid(req
), nvme_nsid(ns
), nr
, format
);
2793 iocb
->ranges
= NULL
;
2796 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
) &&
2797 ((prinfor
& NVME_PRINFO_PRACT
) != (prinfow
& NVME_PRINFO_PRACT
))) {
2798 status
= NVME_INVALID_FIELD
| NVME_DNR
;
2802 if (!(n
->id_ctrl
.ocfs
& (1 << format
))) {
2803 trace_pci_nvme_err_copy_invalid_format(format
);
2804 status
= NVME_INVALID_FIELD
| NVME_DNR
;
2808 if (nr
> ns
->id_ns
.msrc
+ 1) {
2809 status
= NVME_CMD_SIZE_LIMIT
| NVME_DNR
;
2813 iocb
->ranges
= g_new(NvmeCopySourceRange
, nr
);
2815 status
= nvme_h2c(n
, (uint8_t *)iocb
->ranges
,
2816 sizeof(NvmeCopySourceRange
) * nr
, req
);
2821 iocb
->slba
= le64_to_cpu(copy
->sdlba
);
2823 if (ns
->params
.zoned
) {
2824 iocb
->zone
= nvme_get_zone_by_slba(ns
, iocb
->slba
);
2826 status
= NVME_LBA_RANGE
| NVME_DNR
;
2830 status
= nvme_zrm_auto(n
, ns
, iocb
->zone
);
2837 iocb
->bh
= qemu_bh_new(nvme_copy_bh
, iocb
);
2841 iocb
->reftag
= le32_to_cpu(copy
->reftag
);
2842 iocb
->bounce
= g_malloc_n(le16_to_cpu(ns
->id_ns
.mssrl
),
2843 ns
->lbasz
+ ns
->lbaf
.ms
);
2845 qemu_iovec_init(&iocb
->iov
, 1);
2847 block_acct_start(blk_get_stats(ns
->blkconf
.blk
), &iocb
->acct
.read
, 0,
2849 block_acct_start(blk_get_stats(ns
->blkconf
.blk
), &iocb
->acct
.write
, 0,
2852 req
->aiocb
= &iocb
->common
;
2853 nvme_copy_cb(iocb
, 0);
2855 return NVME_NO_COMPLETE
;
2858 g_free(iocb
->ranges
);
2859 qemu_aio_unref(iocb
);
2863 static uint16_t nvme_compare(NvmeCtrl
*n
, NvmeRequest
*req
)
2865 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
2866 NvmeNamespace
*ns
= req
->ns
;
2867 BlockBackend
*blk
= ns
->blkconf
.blk
;
2868 uint64_t slba
= le64_to_cpu(rw
->slba
);
2869 uint32_t nlb
= le16_to_cpu(rw
->nlb
) + 1;
2870 uint8_t prinfo
= NVME_RW_PRINFO(le16_to_cpu(rw
->control
));
2871 size_t data_len
= nvme_l2b(ns
, nlb
);
2872 size_t len
= data_len
;
2873 int64_t offset
= nvme_l2b(ns
, slba
);
2874 struct nvme_compare_ctx
*ctx
= NULL
;
2877 trace_pci_nvme_compare(nvme_cid(req
), nvme_nsid(ns
), slba
, nlb
);
2879 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
) && (prinfo
& NVME_PRINFO_PRACT
)) {
2880 return NVME_INVALID_PROT_INFO
| NVME_DNR
;
2883 if (nvme_ns_ext(ns
)) {
2884 len
+= nvme_m2b(ns
, nlb
);
2887 status
= nvme_check_mdts(n
, len
);
2892 status
= nvme_check_bounds(ns
, slba
, nlb
);
2897 if (NVME_ERR_REC_DULBE(ns
->features
.err_rec
)) {
2898 status
= nvme_check_dulbe(ns
, slba
, nlb
);
2904 status
= nvme_map_dptr(n
, &req
->sg
, len
, &req
->cmd
);
2909 ctx
= g_new(struct nvme_compare_ctx
, 1);
2910 ctx
->data
.bounce
= g_malloc(data_len
);
2914 qemu_iovec_init(&ctx
->data
.iov
, 1);
2915 qemu_iovec_add(&ctx
->data
.iov
, ctx
->data
.bounce
, data_len
);
2917 block_acct_start(blk_get_stats(blk
), &req
->acct
, data_len
,
2919 req
->aiocb
= blk_aio_preadv(blk
, offset
, &ctx
->data
.iov
, 0,
2920 nvme_compare_data_cb
, req
);
2922 return NVME_NO_COMPLETE
;
2925 typedef struct NvmeFlushAIOCB
{
2937 static void nvme_flush_cancel(BlockAIOCB
*acb
)
2939 NvmeFlushAIOCB
*iocb
= container_of(acb
, NvmeFlushAIOCB
, common
);
2941 iocb
->ret
= -ECANCELED
;
2944 blk_aio_cancel_async(iocb
->aiocb
);
2948 static const AIOCBInfo nvme_flush_aiocb_info
= {
2949 .aiocb_size
= sizeof(NvmeFlushAIOCB
),
2950 .cancel_async
= nvme_flush_cancel
,
2951 .get_aio_context
= nvme_get_aio_context
,
2954 static void nvme_flush_ns_cb(void *opaque
, int ret
)
2956 NvmeFlushAIOCB
*iocb
= opaque
;
2957 NvmeNamespace
*ns
= iocb
->ns
;
2962 } else if (iocb
->ret
< 0) {
2967 trace_pci_nvme_flush_ns(iocb
->nsid
);
2970 iocb
->aiocb
= blk_aio_flush(ns
->blkconf
.blk
, nvme_flush_ns_cb
, iocb
);
2976 qemu_bh_schedule(iocb
->bh
);
2979 static void nvme_flush_bh(void *opaque
)
2981 NvmeFlushAIOCB
*iocb
= opaque
;
2982 NvmeRequest
*req
= iocb
->req
;
2983 NvmeCtrl
*n
= nvme_ctrl(req
);
2986 if (iocb
->ret
< 0) {
2990 if (iocb
->broadcast
) {
2991 for (i
= iocb
->nsid
+ 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
2992 iocb
->ns
= nvme_ns(n
, i
);
3004 nvme_flush_ns_cb(iocb
, 0);
3008 qemu_bh_delete(iocb
->bh
);
3011 iocb
->common
.cb(iocb
->common
.opaque
, iocb
->ret
);
3013 qemu_aio_unref(iocb
);
3018 static uint16_t nvme_flush(NvmeCtrl
*n
, NvmeRequest
*req
)
3020 NvmeFlushAIOCB
*iocb
;
3021 uint32_t nsid
= le32_to_cpu(req
->cmd
.nsid
);
3024 iocb
= qemu_aio_get(&nvme_flush_aiocb_info
, NULL
, nvme_misc_cb
, req
);
3027 iocb
->bh
= qemu_bh_new(nvme_flush_bh
, iocb
);
3031 iocb
->broadcast
= (nsid
== NVME_NSID_BROADCAST
);
3033 if (!iocb
->broadcast
) {
3034 if (!nvme_nsid_valid(n
, nsid
)) {
3035 status
= NVME_INVALID_NSID
| NVME_DNR
;
3039 iocb
->ns
= nvme_ns(n
, nsid
);
3041 status
= NVME_INVALID_FIELD
| NVME_DNR
;
3048 req
->aiocb
= &iocb
->common
;
3049 qemu_bh_schedule(iocb
->bh
);
3051 return NVME_NO_COMPLETE
;
3054 qemu_bh_delete(iocb
->bh
);
3056 qemu_aio_unref(iocb
);
3061 static uint16_t nvme_read(NvmeCtrl
*n
, NvmeRequest
*req
)
3063 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
3064 NvmeNamespace
*ns
= req
->ns
;
3065 uint64_t slba
= le64_to_cpu(rw
->slba
);
3066 uint32_t nlb
= (uint32_t)le16_to_cpu(rw
->nlb
) + 1;
3067 uint8_t prinfo
= NVME_RW_PRINFO(le16_to_cpu(rw
->control
));
3068 uint64_t data_size
= nvme_l2b(ns
, nlb
);
3069 uint64_t mapped_size
= data_size
;
3070 uint64_t data_offset
;
3071 BlockBackend
*blk
= ns
->blkconf
.blk
;
3074 if (nvme_ns_ext(ns
)) {
3075 mapped_size
+= nvme_m2b(ns
, nlb
);
3077 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
3078 bool pract
= prinfo
& NVME_PRINFO_PRACT
;
3080 if (pract
&& ns
->lbaf
.ms
== 8) {
3081 mapped_size
= data_size
;
3086 trace_pci_nvme_read(nvme_cid(req
), nvme_nsid(ns
), nlb
, mapped_size
, slba
);
3088 status
= nvme_check_mdts(n
, mapped_size
);
3093 status
= nvme_check_bounds(ns
, slba
, nlb
);
3098 if (ns
->params
.zoned
) {
3099 status
= nvme_check_zone_read(ns
, slba
, nlb
);
3101 trace_pci_nvme_err_zone_read_not_ok(slba
, nlb
, status
);
3106 if (NVME_ERR_REC_DULBE(ns
->features
.err_rec
)) {
3107 status
= nvme_check_dulbe(ns
, slba
, nlb
);
3113 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
3114 return nvme_dif_rw(n
, req
);
3117 status
= nvme_map_data(n
, nlb
, req
);
3122 data_offset
= nvme_l2b(ns
, slba
);
3124 block_acct_start(blk_get_stats(blk
), &req
->acct
, data_size
,
3126 nvme_blk_read(blk
, data_offset
, nvme_rw_cb
, req
);
3127 return NVME_NO_COMPLETE
;
3130 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
3131 return status
| NVME_DNR
;
3134 static uint16_t nvme_do_write(NvmeCtrl
*n
, NvmeRequest
*req
, bool append
,
3137 NvmeRwCmd
*rw
= (NvmeRwCmd
*)&req
->cmd
;
3138 NvmeNamespace
*ns
= req
->ns
;
3139 uint64_t slba
= le64_to_cpu(rw
->slba
);
3140 uint32_t nlb
= (uint32_t)le16_to_cpu(rw
->nlb
) + 1;
3141 uint16_t ctrl
= le16_to_cpu(rw
->control
);
3142 uint8_t prinfo
= NVME_RW_PRINFO(ctrl
);
3143 uint64_t data_size
= nvme_l2b(ns
, nlb
);
3144 uint64_t mapped_size
= data_size
;
3145 uint64_t data_offset
;
3147 NvmeZonedResult
*res
= (NvmeZonedResult
*)&req
->cqe
;
3148 BlockBackend
*blk
= ns
->blkconf
.blk
;
3151 if (nvme_ns_ext(ns
)) {
3152 mapped_size
+= nvme_m2b(ns
, nlb
);
3154 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
3155 bool pract
= prinfo
& NVME_PRINFO_PRACT
;
3157 if (pract
&& ns
->lbaf
.ms
== 8) {
3158 mapped_size
-= nvme_m2b(ns
, nlb
);
3163 trace_pci_nvme_write(nvme_cid(req
), nvme_io_opc_str(rw
->opcode
),
3164 nvme_nsid(ns
), nlb
, mapped_size
, slba
);
3167 status
= nvme_check_mdts(n
, mapped_size
);
3173 status
= nvme_check_bounds(ns
, slba
, nlb
);
3178 if (ns
->params
.zoned
) {
3179 zone
= nvme_get_zone_by_slba(ns
, slba
);
3183 bool piremap
= !!(ctrl
& NVME_RW_PIREMAP
);
3185 if (unlikely(slba
!= zone
->d
.zslba
)) {
3186 trace_pci_nvme_err_append_not_at_start(slba
, zone
->d
.zslba
);
3187 status
= NVME_INVALID_FIELD
;
3191 if (n
->params
.zasl
&&
3192 data_size
> (uint64_t)n
->page_size
<< n
->params
.zasl
) {
3193 trace_pci_nvme_err_zasl(data_size
);
3194 return NVME_INVALID_FIELD
| NVME_DNR
;
3198 rw
->slba
= cpu_to_le64(slba
);
3199 res
->slba
= cpu_to_le64(slba
);
3201 switch (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
3202 case NVME_ID_NS_DPS_TYPE_1
:
3204 return NVME_INVALID_PROT_INFO
| NVME_DNR
;
3209 case NVME_ID_NS_DPS_TYPE_2
:
3211 uint32_t reftag
= le32_to_cpu(rw
->reftag
);
3212 rw
->reftag
= cpu_to_le32(reftag
+ (slba
- zone
->d
.zslba
));
3217 case NVME_ID_NS_DPS_TYPE_3
:
3219 return NVME_INVALID_PROT_INFO
| NVME_DNR
;
3226 status
= nvme_check_zone_write(ns
, zone
, slba
, nlb
);
3231 status
= nvme_zrm_auto(n
, ns
, zone
);
3239 data_offset
= nvme_l2b(ns
, slba
);
3241 if (NVME_ID_NS_DPS_TYPE(ns
->id_ns
.dps
)) {
3242 return nvme_dif_rw(n
, req
);
3246 status
= nvme_map_data(n
, nlb
, req
);
3251 block_acct_start(blk_get_stats(blk
), &req
->acct
, data_size
,
3253 nvme_blk_write(blk
, data_offset
, nvme_rw_cb
, req
);
3255 req
->aiocb
= blk_aio_pwrite_zeroes(blk
, data_offset
, data_size
,
3256 BDRV_REQ_MAY_UNMAP
, nvme_rw_cb
,
3260 return NVME_NO_COMPLETE
;
3263 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
3264 return status
| NVME_DNR
;
3267 static inline uint16_t nvme_write(NvmeCtrl
*n
, NvmeRequest
*req
)
3269 return nvme_do_write(n
, req
, false, false);
3272 static inline uint16_t nvme_write_zeroes(NvmeCtrl
*n
, NvmeRequest
*req
)
3274 return nvme_do_write(n
, req
, false, true);
3277 static inline uint16_t nvme_zone_append(NvmeCtrl
*n
, NvmeRequest
*req
)
3279 return nvme_do_write(n
, req
, true, false);
3282 static uint16_t nvme_get_mgmt_zone_slba_idx(NvmeNamespace
*ns
, NvmeCmd
*c
,
3283 uint64_t *slba
, uint32_t *zone_idx
)
3285 uint32_t dw10
= le32_to_cpu(c
->cdw10
);
3286 uint32_t dw11
= le32_to_cpu(c
->cdw11
);
3288 if (!ns
->params
.zoned
) {
3289 trace_pci_nvme_err_invalid_opc(c
->opcode
);
3290 return NVME_INVALID_OPCODE
| NVME_DNR
;
3293 *slba
= ((uint64_t)dw11
) << 32 | dw10
;
3294 if (unlikely(*slba
>= ns
->id_ns
.nsze
)) {
3295 trace_pci_nvme_err_invalid_lba_range(*slba
, 0, ns
->id_ns
.nsze
);
3297 return NVME_LBA_RANGE
| NVME_DNR
;
3300 *zone_idx
= nvme_zone_idx(ns
, *slba
);
3301 assert(*zone_idx
< ns
->num_zones
);
3303 return NVME_SUCCESS
;
3306 typedef uint16_t (*op_handler_t
)(NvmeNamespace
*, NvmeZone
*, NvmeZoneState
,
3309 enum NvmeZoneProcessingMask
{
3310 NVME_PROC_CURRENT_ZONE
= 0,
3311 NVME_PROC_OPENED_ZONES
= 1 << 0,
3312 NVME_PROC_CLOSED_ZONES
= 1 << 1,
3313 NVME_PROC_READ_ONLY_ZONES
= 1 << 2,
3314 NVME_PROC_FULL_ZONES
= 1 << 3,
3317 static uint16_t nvme_open_zone(NvmeNamespace
*ns
, NvmeZone
*zone
,
3318 NvmeZoneState state
, NvmeRequest
*req
)
3320 return nvme_zrm_open(nvme_ctrl(req
), ns
, zone
);
3323 static uint16_t nvme_close_zone(NvmeNamespace
*ns
, NvmeZone
*zone
,
3324 NvmeZoneState state
, NvmeRequest
*req
)
3326 return nvme_zrm_close(ns
, zone
);
3329 static uint16_t nvme_finish_zone(NvmeNamespace
*ns
, NvmeZone
*zone
,
3330 NvmeZoneState state
, NvmeRequest
*req
)
3332 return nvme_zrm_finish(ns
, zone
);
3335 static uint16_t nvme_offline_zone(NvmeNamespace
*ns
, NvmeZone
*zone
,
3336 NvmeZoneState state
, NvmeRequest
*req
)
3339 case NVME_ZONE_STATE_READ_ONLY
:
3340 nvme_assign_zone_state(ns
, zone
, NVME_ZONE_STATE_OFFLINE
);
3342 case NVME_ZONE_STATE_OFFLINE
:
3343 return NVME_SUCCESS
;
3345 return NVME_ZONE_INVAL_TRANSITION
;
3349 static uint16_t nvme_set_zd_ext(NvmeNamespace
*ns
, NvmeZone
*zone
)
3352 uint8_t state
= nvme_get_zone_state(zone
);
3354 if (state
== NVME_ZONE_STATE_EMPTY
) {
3355 status
= nvme_aor_check(ns
, 1, 0);
3359 nvme_aor_inc_active(ns
);
3360 zone
->d
.za
|= NVME_ZA_ZD_EXT_VALID
;
3361 nvme_assign_zone_state(ns
, zone
, NVME_ZONE_STATE_CLOSED
);
3362 return NVME_SUCCESS
;
3365 return NVME_ZONE_INVAL_TRANSITION
;
3368 static uint16_t nvme_bulk_proc_zone(NvmeNamespace
*ns
, NvmeZone
*zone
,
3369 enum NvmeZoneProcessingMask proc_mask
,
3370 op_handler_t op_hndlr
, NvmeRequest
*req
)
3372 uint16_t status
= NVME_SUCCESS
;
3373 NvmeZoneState zs
= nvme_get_zone_state(zone
);
3377 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
3378 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
3379 proc_zone
= proc_mask
& NVME_PROC_OPENED_ZONES
;
3381 case NVME_ZONE_STATE_CLOSED
:
3382 proc_zone
= proc_mask
& NVME_PROC_CLOSED_ZONES
;
3384 case NVME_ZONE_STATE_READ_ONLY
:
3385 proc_zone
= proc_mask
& NVME_PROC_READ_ONLY_ZONES
;
3387 case NVME_ZONE_STATE_FULL
:
3388 proc_zone
= proc_mask
& NVME_PROC_FULL_ZONES
;
3395 status
= op_hndlr(ns
, zone
, zs
, req
);
3401 static uint16_t nvme_do_zone_op(NvmeNamespace
*ns
, NvmeZone
*zone
,
3402 enum NvmeZoneProcessingMask proc_mask
,
3403 op_handler_t op_hndlr
, NvmeRequest
*req
)
3406 uint16_t status
= NVME_SUCCESS
;
3410 status
= op_hndlr(ns
, zone
, nvme_get_zone_state(zone
), req
);
3412 if (proc_mask
& NVME_PROC_CLOSED_ZONES
) {
3413 QTAILQ_FOREACH_SAFE(zone
, &ns
->closed_zones
, entry
, next
) {
3414 status
= nvme_bulk_proc_zone(ns
, zone
, proc_mask
, op_hndlr
,
3416 if (status
&& status
!= NVME_NO_COMPLETE
) {
3421 if (proc_mask
& NVME_PROC_OPENED_ZONES
) {
3422 QTAILQ_FOREACH_SAFE(zone
, &ns
->imp_open_zones
, entry
, next
) {
3423 status
= nvme_bulk_proc_zone(ns
, zone
, proc_mask
, op_hndlr
,
3425 if (status
&& status
!= NVME_NO_COMPLETE
) {
3430 QTAILQ_FOREACH_SAFE(zone
, &ns
->exp_open_zones
, entry
, next
) {
3431 status
= nvme_bulk_proc_zone(ns
, zone
, proc_mask
, op_hndlr
,
3433 if (status
&& status
!= NVME_NO_COMPLETE
) {
3438 if (proc_mask
& NVME_PROC_FULL_ZONES
) {
3439 QTAILQ_FOREACH_SAFE(zone
, &ns
->full_zones
, entry
, next
) {
3440 status
= nvme_bulk_proc_zone(ns
, zone
, proc_mask
, op_hndlr
,
3442 if (status
&& status
!= NVME_NO_COMPLETE
) {
3448 if (proc_mask
& NVME_PROC_READ_ONLY_ZONES
) {
3449 for (i
= 0; i
< ns
->num_zones
; i
++, zone
++) {
3450 status
= nvme_bulk_proc_zone(ns
, zone
, proc_mask
, op_hndlr
,
3452 if (status
&& status
!= NVME_NO_COMPLETE
) {
3463 typedef struct NvmeZoneResetAIOCB
{
3473 } NvmeZoneResetAIOCB
;
3475 static void nvme_zone_reset_cancel(BlockAIOCB
*aiocb
)
3477 NvmeZoneResetAIOCB
*iocb
= container_of(aiocb
, NvmeZoneResetAIOCB
, common
);
3478 NvmeRequest
*req
= iocb
->req
;
3479 NvmeNamespace
*ns
= req
->ns
;
3481 iocb
->idx
= ns
->num_zones
;
3483 iocb
->ret
= -ECANCELED
;
3486 blk_aio_cancel_async(iocb
->aiocb
);
3491 static const AIOCBInfo nvme_zone_reset_aiocb_info
= {
3492 .aiocb_size
= sizeof(NvmeZoneResetAIOCB
),
3493 .cancel_async
= nvme_zone_reset_cancel
,
3496 static void nvme_zone_reset_bh(void *opaque
)
3498 NvmeZoneResetAIOCB
*iocb
= opaque
;
3500 iocb
->common
.cb(iocb
->common
.opaque
, iocb
->ret
);
3502 qemu_bh_delete(iocb
->bh
);
3504 qemu_aio_unref(iocb
);
3507 static void nvme_zone_reset_cb(void *opaque
, int ret
);
3509 static void nvme_zone_reset_epilogue_cb(void *opaque
, int ret
)
3511 NvmeZoneResetAIOCB
*iocb
= opaque
;
3512 NvmeRequest
*req
= iocb
->req
;
3513 NvmeNamespace
*ns
= req
->ns
;
3518 nvme_zone_reset_cb(iocb
, ret
);
3523 nvme_zone_reset_cb(iocb
, 0);
3527 moff
= nvme_moff(ns
, iocb
->zone
->d
.zslba
);
3528 count
= nvme_m2b(ns
, ns
->zone_size
);
3530 iocb
->aiocb
= blk_aio_pwrite_zeroes(ns
->blkconf
.blk
, moff
, count
,
3532 nvme_zone_reset_cb
, iocb
);
3536 static void nvme_zone_reset_cb(void *opaque
, int ret
)
3538 NvmeZoneResetAIOCB
*iocb
= opaque
;
3539 NvmeRequest
*req
= iocb
->req
;
3540 NvmeNamespace
*ns
= req
->ns
;
3548 nvme_zrm_reset(ns
, iocb
->zone
);
3555 while (iocb
->idx
< ns
->num_zones
) {
3556 NvmeZone
*zone
= &ns
->zone_array
[iocb
->idx
++];
3558 switch (nvme_get_zone_state(zone
)) {
3559 case NVME_ZONE_STATE_EMPTY
:
3566 case NVME_ZONE_STATE_EXPLICITLY_OPEN
:
3567 case NVME_ZONE_STATE_IMPLICITLY_OPEN
:
3568 case NVME_ZONE_STATE_CLOSED
:
3569 case NVME_ZONE_STATE_FULL
:
3577 trace_pci_nvme_zns_zone_reset(zone
->d
.zslba
);
3579 iocb
->aiocb
= blk_aio_pwrite_zeroes(ns
->blkconf
.blk
,
3580 nvme_l2b(ns
, zone
->d
.zslba
),
3581 nvme_l2b(ns
, ns
->zone_size
),
3583 nvme_zone_reset_epilogue_cb
,
3591 qemu_bh_schedule(iocb
->bh
);
3595 static uint16_t nvme_zone_mgmt_send(NvmeCtrl
*n
, NvmeRequest
*req
)
3597 NvmeCmd
*cmd
= (NvmeCmd
*)&req
->cmd
;
3598 NvmeNamespace
*ns
= req
->ns
;
3600 NvmeZoneResetAIOCB
*iocb
;
3602 uint32_t dw13
= le32_to_cpu(cmd
->cdw13
);
3604 uint32_t zone_idx
= 0;
3608 enum NvmeZoneProcessingMask proc_mask
= NVME_PROC_CURRENT_ZONE
;
3610 action
= dw13
& 0xff;
3611 all
= !!(dw13
& 0x100);
3613 req
->status
= NVME_SUCCESS
;
3616 status
= nvme_get_mgmt_zone_slba_idx(ns
, cmd
, &slba
, &zone_idx
);
3622 zone
= &ns
->zone_array
[zone_idx
];
3623 if (slba
!= zone
->d
.zslba
) {
3624 trace_pci_nvme_err_unaligned_zone_cmd(action
, slba
, zone
->d
.zslba
);
3625 return NVME_INVALID_FIELD
| NVME_DNR
;
3630 case NVME_ZONE_ACTION_OPEN
:
3632 proc_mask
= NVME_PROC_CLOSED_ZONES
;
3634 trace_pci_nvme_open_zone(slba
, zone_idx
, all
);
3635 status
= nvme_do_zone_op(ns
, zone
, proc_mask
, nvme_open_zone
, req
);
3638 case NVME_ZONE_ACTION_CLOSE
:
3640 proc_mask
= NVME_PROC_OPENED_ZONES
;
3642 trace_pci_nvme_close_zone(slba
, zone_idx
, all
);
3643 status
= nvme_do_zone_op(ns
, zone
, proc_mask
, nvme_close_zone
, req
);
3646 case NVME_ZONE_ACTION_FINISH
:
3648 proc_mask
= NVME_PROC_OPENED_ZONES
| NVME_PROC_CLOSED_ZONES
;
3650 trace_pci_nvme_finish_zone(slba
, zone_idx
, all
);
3651 status
= nvme_do_zone_op(ns
, zone
, proc_mask
, nvme_finish_zone
, req
);
3654 case NVME_ZONE_ACTION_RESET
:
3655 trace_pci_nvme_reset_zone(slba
, zone_idx
, all
);
3657 iocb
= blk_aio_get(&nvme_zone_reset_aiocb_info
, ns
->blkconf
.blk
,
3661 iocb
->bh
= qemu_bh_new(nvme_zone_reset_bh
, iocb
);
3664 iocb
->idx
= zone_idx
;
3667 req
->aiocb
= &iocb
->common
;
3668 nvme_zone_reset_cb(iocb
, 0);
3670 return NVME_NO_COMPLETE
;
3672 case NVME_ZONE_ACTION_OFFLINE
:
3674 proc_mask
= NVME_PROC_READ_ONLY_ZONES
;
3676 trace_pci_nvme_offline_zone(slba
, zone_idx
, all
);
3677 status
= nvme_do_zone_op(ns
, zone
, proc_mask
, nvme_offline_zone
, req
);
3680 case NVME_ZONE_ACTION_SET_ZD_EXT
:
3681 trace_pci_nvme_set_descriptor_extension(slba
, zone_idx
);
3682 if (all
|| !ns
->params
.zd_extension_size
) {
3683 return NVME_INVALID_FIELD
| NVME_DNR
;
3685 zd_ext
= nvme_get_zd_extension(ns
, zone_idx
);
3686 status
= nvme_h2c(n
, zd_ext
, ns
->params
.zd_extension_size
, req
);
3688 trace_pci_nvme_err_zd_extension_map_error(zone_idx
);
3692 status
= nvme_set_zd_ext(ns
, zone
);
3693 if (status
== NVME_SUCCESS
) {
3694 trace_pci_nvme_zd_extension_set(zone_idx
);
3700 trace_pci_nvme_err_invalid_mgmt_action(action
);
3701 status
= NVME_INVALID_FIELD
;
3704 if (status
== NVME_ZONE_INVAL_TRANSITION
) {
3705 trace_pci_nvme_err_invalid_zone_state_transition(action
, slba
,
3715 static bool nvme_zone_matches_filter(uint32_t zafs
, NvmeZone
*zl
)
3717 NvmeZoneState zs
= nvme_get_zone_state(zl
);
3720 case NVME_ZONE_REPORT_ALL
:
3722 case NVME_ZONE_REPORT_EMPTY
:
3723 return zs
== NVME_ZONE_STATE_EMPTY
;
3724 case NVME_ZONE_REPORT_IMPLICITLY_OPEN
:
3725 return zs
== NVME_ZONE_STATE_IMPLICITLY_OPEN
;
3726 case NVME_ZONE_REPORT_EXPLICITLY_OPEN
:
3727 return zs
== NVME_ZONE_STATE_EXPLICITLY_OPEN
;
3728 case NVME_ZONE_REPORT_CLOSED
:
3729 return zs
== NVME_ZONE_STATE_CLOSED
;
3730 case NVME_ZONE_REPORT_FULL
:
3731 return zs
== NVME_ZONE_STATE_FULL
;
3732 case NVME_ZONE_REPORT_READ_ONLY
:
3733 return zs
== NVME_ZONE_STATE_READ_ONLY
;
3734 case NVME_ZONE_REPORT_OFFLINE
:
3735 return zs
== NVME_ZONE_STATE_OFFLINE
;
3741 static uint16_t nvme_zone_mgmt_recv(NvmeCtrl
*n
, NvmeRequest
*req
)
3743 NvmeCmd
*cmd
= (NvmeCmd
*)&req
->cmd
;
3744 NvmeNamespace
*ns
= req
->ns
;
3745 /* cdw12 is zero-based number of dwords to return. Convert to bytes */
3746 uint32_t data_size
= (le32_to_cpu(cmd
->cdw12
) + 1) << 2;
3747 uint32_t dw13
= le32_to_cpu(cmd
->cdw13
);
3748 uint32_t zone_idx
, zra
, zrasf
, partial
;
3749 uint64_t max_zones
, nr_zones
= 0;
3754 NvmeZoneReportHeader
*header
;
3756 size_t zone_entry_sz
;
3759 req
->status
= NVME_SUCCESS
;
3761 status
= nvme_get_mgmt_zone_slba_idx(ns
, cmd
, &slba
, &zone_idx
);
3767 if (zra
!= NVME_ZONE_REPORT
&& zra
!= NVME_ZONE_REPORT_EXTENDED
) {
3768 return NVME_INVALID_FIELD
| NVME_DNR
;
3770 if (zra
== NVME_ZONE_REPORT_EXTENDED
&& !ns
->params
.zd_extension_size
) {
3771 return NVME_INVALID_FIELD
| NVME_DNR
;
3774 zrasf
= (dw13
>> 8) & 0xff;
3775 if (zrasf
> NVME_ZONE_REPORT_OFFLINE
) {
3776 return NVME_INVALID_FIELD
| NVME_DNR
;
3779 if (data_size
< sizeof(NvmeZoneReportHeader
)) {
3780 return NVME_INVALID_FIELD
| NVME_DNR
;
3783 status
= nvme_check_mdts(n
, data_size
);
3788 partial
= (dw13
>> 16) & 0x01;
3790 zone_entry_sz
= sizeof(NvmeZoneDescr
);
3791 if (zra
== NVME_ZONE_REPORT_EXTENDED
) {
3792 zone_entry_sz
+= ns
->params
.zd_extension_size
;
3795 max_zones
= (data_size
- sizeof(NvmeZoneReportHeader
)) / zone_entry_sz
;
3796 buf
= g_malloc0(data_size
);
3798 zone
= &ns
->zone_array
[zone_idx
];
3799 for (i
= zone_idx
; i
< ns
->num_zones
; i
++) {
3800 if (partial
&& nr_zones
>= max_zones
) {
3803 if (nvme_zone_matches_filter(zrasf
, zone
++)) {
3807 header
= (NvmeZoneReportHeader
*)buf
;
3808 header
->nr_zones
= cpu_to_le64(nr_zones
);
3810 buf_p
= buf
+ sizeof(NvmeZoneReportHeader
);
3811 for (; zone_idx
< ns
->num_zones
&& max_zones
> 0; zone_idx
++) {
3812 zone
= &ns
->zone_array
[zone_idx
];
3813 if (nvme_zone_matches_filter(zrasf
, zone
)) {
3814 z
= (NvmeZoneDescr
*)buf_p
;
3815 buf_p
+= sizeof(NvmeZoneDescr
);
3819 z
->zcap
= cpu_to_le64(zone
->d
.zcap
);
3820 z
->zslba
= cpu_to_le64(zone
->d
.zslba
);
3823 if (nvme_wp_is_valid(zone
)) {
3824 z
->wp
= cpu_to_le64(zone
->d
.wp
);
3826 z
->wp
= cpu_to_le64(~0ULL);
3829 if (zra
== NVME_ZONE_REPORT_EXTENDED
) {
3830 if (zone
->d
.za
& NVME_ZA_ZD_EXT_VALID
) {
3831 memcpy(buf_p
, nvme_get_zd_extension(ns
, zone_idx
),
3832 ns
->params
.zd_extension_size
);
3834 buf_p
+= ns
->params
.zd_extension_size
;
3841 status
= nvme_c2h(n
, (uint8_t *)buf
, data_size
, req
);
3848 static uint16_t nvme_io_cmd(NvmeCtrl
*n
, NvmeRequest
*req
)
3851 uint32_t nsid
= le32_to_cpu(req
->cmd
.nsid
);
3853 trace_pci_nvme_io_cmd(nvme_cid(req
), nsid
, nvme_sqid(req
),
3854 req
->cmd
.opcode
, nvme_io_opc_str(req
->cmd
.opcode
));
3856 if (!nvme_nsid_valid(n
, nsid
)) {
3857 return NVME_INVALID_NSID
| NVME_DNR
;
3861 * In the base NVM command set, Flush may apply to all namespaces
3862 * (indicated by NSID being set to FFFFFFFFh). But if that feature is used
3863 * along with TP 4056 (Namespace Types), it may be pretty screwed up.
3865 * If NSID is indeed set to FFFFFFFFh, we simply cannot associate the
3866 * opcode with a specific command since we cannot determine a unique I/O
3867 * command set. Opcode 0h could have any other meaning than something
3868 * equivalent to flushing and say it DOES have completely different
3869 * semantics in some other command set - does an NSID of FFFFFFFFh then
3870 * mean "for all namespaces, apply whatever command set specific command
3871 * that uses the 0h opcode?" Or does it mean "for all namespaces, apply
3872 * whatever command that uses the 0h opcode if, and only if, it allows NSID
3875 * Anyway (and luckily), for now, we do not care about this since the
3876 * device only supports namespace types that includes the NVM Flush command
3877 * (NVM and Zoned), so always do an NVM Flush.
3879 if (req
->cmd
.opcode
== NVME_CMD_FLUSH
) {
3880 return nvme_flush(n
, req
);
3883 ns
= nvme_ns(n
, nsid
);
3884 if (unlikely(!ns
)) {
3885 return NVME_INVALID_FIELD
| NVME_DNR
;
3888 if (!(ns
->iocs
[req
->cmd
.opcode
] & NVME_CMD_EFF_CSUPP
)) {
3889 trace_pci_nvme_err_invalid_opc(req
->cmd
.opcode
);
3890 return NVME_INVALID_OPCODE
| NVME_DNR
;
3897 if (NVME_CMD_FLAGS_FUSE(req
->cmd
.flags
)) {
3898 return NVME_INVALID_FIELD
;
3903 switch (req
->cmd
.opcode
) {
3904 case NVME_CMD_WRITE_ZEROES
:
3905 return nvme_write_zeroes(n
, req
);
3906 case NVME_CMD_ZONE_APPEND
:
3907 return nvme_zone_append(n
, req
);
3908 case NVME_CMD_WRITE
:
3909 return nvme_write(n
, req
);
3911 return nvme_read(n
, req
);
3912 case NVME_CMD_COMPARE
:
3913 return nvme_compare(n
, req
);
3915 return nvme_dsm(n
, req
);
3916 case NVME_CMD_VERIFY
:
3917 return nvme_verify(n
, req
);
3919 return nvme_copy(n
, req
);
3920 case NVME_CMD_ZONE_MGMT_SEND
:
3921 return nvme_zone_mgmt_send(n
, req
);
3922 case NVME_CMD_ZONE_MGMT_RECV
:
3923 return nvme_zone_mgmt_recv(n
, req
);
3928 return NVME_INVALID_OPCODE
| NVME_DNR
;
3931 static void nvme_free_sq(NvmeSQueue
*sq
, NvmeCtrl
*n
)
3933 n
->sq
[sq
->sqid
] = NULL
;
3934 timer_free(sq
->timer
);
3941 static uint16_t nvme_del_sq(NvmeCtrl
*n
, NvmeRequest
*req
)
3943 NvmeDeleteQ
*c
= (NvmeDeleteQ
*)&req
->cmd
;
3944 NvmeRequest
*r
, *next
;
3947 uint16_t qid
= le16_to_cpu(c
->qid
);
3949 if (unlikely(!qid
|| nvme_check_sqid(n
, qid
))) {
3950 trace_pci_nvme_err_invalid_del_sq(qid
);
3951 return NVME_INVALID_QID
| NVME_DNR
;
3954 trace_pci_nvme_del_sq(qid
);
3957 while (!QTAILQ_EMPTY(&sq
->out_req_list
)) {
3958 r
= QTAILQ_FIRST(&sq
->out_req_list
);
3960 blk_aio_cancel(r
->aiocb
);
3963 assert(QTAILQ_EMPTY(&sq
->out_req_list
));
3965 if (!nvme_check_cqid(n
, sq
->cqid
)) {
3966 cq
= n
->cq
[sq
->cqid
];
3967 QTAILQ_REMOVE(&cq
->sq_list
, sq
, entry
);
3970 QTAILQ_FOREACH_SAFE(r
, &cq
->req_list
, entry
, next
) {
3972 QTAILQ_REMOVE(&cq
->req_list
, r
, entry
);
3973 QTAILQ_INSERT_TAIL(&sq
->req_list
, r
, entry
);
3978 nvme_free_sq(sq
, n
);
3979 return NVME_SUCCESS
;
3982 static void nvme_init_sq(NvmeSQueue
*sq
, NvmeCtrl
*n
, uint64_t dma_addr
,
3983 uint16_t sqid
, uint16_t cqid
, uint16_t size
)
3989 sq
->dma_addr
= dma_addr
;
3993 sq
->head
= sq
->tail
= 0;
3994 sq
->io_req
= g_new0(NvmeRequest
, sq
->size
);
3996 QTAILQ_INIT(&sq
->req_list
);
3997 QTAILQ_INIT(&sq
->out_req_list
);
3998 for (i
= 0; i
< sq
->size
; i
++) {
3999 sq
->io_req
[i
].sq
= sq
;
4000 QTAILQ_INSERT_TAIL(&(sq
->req_list
), &sq
->io_req
[i
], entry
);
4002 sq
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, nvme_process_sq
, sq
);
4004 assert(n
->cq
[cqid
]);
4006 QTAILQ_INSERT_TAIL(&(cq
->sq_list
), sq
, entry
);
4010 static uint16_t nvme_create_sq(NvmeCtrl
*n
, NvmeRequest
*req
)
4013 NvmeCreateSq
*c
= (NvmeCreateSq
*)&req
->cmd
;
4015 uint16_t cqid
= le16_to_cpu(c
->cqid
);
4016 uint16_t sqid
= le16_to_cpu(c
->sqid
);
4017 uint16_t qsize
= le16_to_cpu(c
->qsize
);
4018 uint16_t qflags
= le16_to_cpu(c
->sq_flags
);
4019 uint64_t prp1
= le64_to_cpu(c
->prp1
);
4021 trace_pci_nvme_create_sq(prp1
, sqid
, cqid
, qsize
, qflags
);
4023 if (unlikely(!cqid
|| nvme_check_cqid(n
, cqid
))) {
4024 trace_pci_nvme_err_invalid_create_sq_cqid(cqid
);
4025 return NVME_INVALID_CQID
| NVME_DNR
;
4027 if (unlikely(!sqid
|| sqid
> n
->params
.max_ioqpairs
||
4028 n
->sq
[sqid
] != NULL
)) {
4029 trace_pci_nvme_err_invalid_create_sq_sqid(sqid
);
4030 return NVME_INVALID_QID
| NVME_DNR
;
4032 if (unlikely(!qsize
|| qsize
> NVME_CAP_MQES(ldq_le_p(&n
->bar
.cap
)))) {
4033 trace_pci_nvme_err_invalid_create_sq_size(qsize
);
4034 return NVME_MAX_QSIZE_EXCEEDED
| NVME_DNR
;
4036 if (unlikely(prp1
& (n
->page_size
- 1))) {
4037 trace_pci_nvme_err_invalid_create_sq_addr(prp1
);
4038 return NVME_INVALID_PRP_OFFSET
| NVME_DNR
;
4040 if (unlikely(!(NVME_SQ_FLAGS_PC(qflags
)))) {
4041 trace_pci_nvme_err_invalid_create_sq_qflags(NVME_SQ_FLAGS_PC(qflags
));
4042 return NVME_INVALID_FIELD
| NVME_DNR
;
4044 sq
= g_malloc0(sizeof(*sq
));
4045 nvme_init_sq(sq
, n
, prp1
, sqid
, cqid
, qsize
+ 1);
4046 return NVME_SUCCESS
;
4050 uint64_t units_read
;
4051 uint64_t units_written
;
4052 uint64_t read_commands
;
4053 uint64_t write_commands
;
4056 static void nvme_set_blk_stats(NvmeNamespace
*ns
, struct nvme_stats
*stats
)
4058 BlockAcctStats
*s
= blk_get_stats(ns
->blkconf
.blk
);
4060 stats
->units_read
+= s
->nr_bytes
[BLOCK_ACCT_READ
] >> BDRV_SECTOR_BITS
;
4061 stats
->units_written
+= s
->nr_bytes
[BLOCK_ACCT_WRITE
] >> BDRV_SECTOR_BITS
;
4062 stats
->read_commands
+= s
->nr_ops
[BLOCK_ACCT_READ
];
4063 stats
->write_commands
+= s
->nr_ops
[BLOCK_ACCT_WRITE
];
4066 static uint16_t nvme_smart_info(NvmeCtrl
*n
, uint8_t rae
, uint32_t buf_len
,
4067 uint64_t off
, NvmeRequest
*req
)
4069 uint32_t nsid
= le32_to_cpu(req
->cmd
.nsid
);
4070 struct nvme_stats stats
= { 0 };
4071 NvmeSmartLog smart
= { 0 };
4076 if (off
>= sizeof(smart
)) {
4077 return NVME_INVALID_FIELD
| NVME_DNR
;
4080 if (nsid
!= 0xffffffff) {
4081 ns
= nvme_ns(n
, nsid
);
4083 return NVME_INVALID_NSID
| NVME_DNR
;
4085 nvme_set_blk_stats(ns
, &stats
);
4089 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
4094 nvme_set_blk_stats(ns
, &stats
);
4098 trans_len
= MIN(sizeof(smart
) - off
, buf_len
);
4099 smart
.critical_warning
= n
->smart_critical_warning
;
4101 smart
.data_units_read
[0] = cpu_to_le64(DIV_ROUND_UP(stats
.units_read
,
4103 smart
.data_units_written
[0] = cpu_to_le64(DIV_ROUND_UP(stats
.units_written
,
4105 smart
.host_read_commands
[0] = cpu_to_le64(stats
.read_commands
);
4106 smart
.host_write_commands
[0] = cpu_to_le64(stats
.write_commands
);
4108 smart
.temperature
= cpu_to_le16(n
->temperature
);
4110 if ((n
->temperature
>= n
->features
.temp_thresh_hi
) ||
4111 (n
->temperature
<= n
->features
.temp_thresh_low
)) {
4112 smart
.critical_warning
|= NVME_SMART_TEMPERATURE
;
4115 current_ms
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
);
4116 smart
.power_on_hours
[0] =
4117 cpu_to_le64((((current_ms
- n
->starttime_ms
) / 1000) / 60) / 60);
4120 nvme_clear_events(n
, NVME_AER_TYPE_SMART
);
4123 return nvme_c2h(n
, (uint8_t *) &smart
+ off
, trans_len
, req
);
4126 static uint16_t nvme_fw_log_info(NvmeCtrl
*n
, uint32_t buf_len
, uint64_t off
,
4130 NvmeFwSlotInfoLog fw_log
= {
4134 if (off
>= sizeof(fw_log
)) {
4135 return NVME_INVALID_FIELD
| NVME_DNR
;
4138 strpadcpy((char *)&fw_log
.frs1
, sizeof(fw_log
.frs1
), "1.0", ' ');
4139 trans_len
= MIN(sizeof(fw_log
) - off
, buf_len
);
4141 return nvme_c2h(n
, (uint8_t *) &fw_log
+ off
, trans_len
, req
);
4144 static uint16_t nvme_error_info(NvmeCtrl
*n
, uint8_t rae
, uint32_t buf_len
,
4145 uint64_t off
, NvmeRequest
*req
)
4148 NvmeErrorLog errlog
;
4150 if (off
>= sizeof(errlog
)) {
4151 return NVME_INVALID_FIELD
| NVME_DNR
;
4155 nvme_clear_events(n
, NVME_AER_TYPE_ERROR
);
4158 memset(&errlog
, 0x0, sizeof(errlog
));
4159 trans_len
= MIN(sizeof(errlog
) - off
, buf_len
);
4161 return nvme_c2h(n
, (uint8_t *)&errlog
, trans_len
, req
);
4164 static uint16_t nvme_changed_nslist(NvmeCtrl
*n
, uint8_t rae
, uint32_t buf_len
,
4165 uint64_t off
, NvmeRequest
*req
)
4167 uint32_t nslist
[1024];
4172 if (off
>= sizeof(nslist
)) {
4173 trace_pci_nvme_err_invalid_log_page_offset(off
, sizeof(nslist
));
4174 return NVME_INVALID_FIELD
| NVME_DNR
;
4177 memset(nslist
, 0x0, sizeof(nslist
));
4178 trans_len
= MIN(sizeof(nslist
) - off
, buf_len
);
4180 while ((nsid
= find_first_bit(n
->changed_nsids
, NVME_CHANGED_NSID_SIZE
)) !=
4181 NVME_CHANGED_NSID_SIZE
) {
4183 * If more than 1024 namespaces, the first entry in the log page should
4184 * be set to FFFFFFFFh and the others to 0 as spec.
4186 if (i
== ARRAY_SIZE(nslist
)) {
4187 memset(nslist
, 0x0, sizeof(nslist
));
4188 nslist
[0] = 0xffffffff;
4193 clear_bit(nsid
, n
->changed_nsids
);
4197 * Remove all the remaining list entries in case returns directly due to
4198 * more than 1024 namespaces.
4200 if (nslist
[0] == 0xffffffff) {
4201 bitmap_zero(n
->changed_nsids
, NVME_CHANGED_NSID_SIZE
);
4205 nvme_clear_events(n
, NVME_AER_TYPE_NOTICE
);
4208 return nvme_c2h(n
, ((uint8_t *)nslist
) + off
, trans_len
, req
);
4211 static uint16_t nvme_cmd_effects(NvmeCtrl
*n
, uint8_t csi
, uint32_t buf_len
,
4212 uint64_t off
, NvmeRequest
*req
)
4214 NvmeEffectsLog log
= {};
4215 const uint32_t *src_iocs
= NULL
;
4218 if (off
>= sizeof(log
)) {
4219 trace_pci_nvme_err_invalid_log_page_offset(off
, sizeof(log
));
4220 return NVME_INVALID_FIELD
| NVME_DNR
;
4223 switch (NVME_CC_CSS(ldl_le_p(&n
->bar
.cc
))) {
4224 case NVME_CC_CSS_NVM
:
4225 src_iocs
= nvme_cse_iocs_nvm
;
4227 case NVME_CC_CSS_ADMIN_ONLY
:
4229 case NVME_CC_CSS_CSI
:
4232 src_iocs
= nvme_cse_iocs_nvm
;
4234 case NVME_CSI_ZONED
:
4235 src_iocs
= nvme_cse_iocs_zoned
;
4240 memcpy(log
.acs
, nvme_cse_acs
, sizeof(nvme_cse_acs
));
4243 memcpy(log
.iocs
, src_iocs
, sizeof(log
.iocs
));
4246 trans_len
= MIN(sizeof(log
) - off
, buf_len
);
4248 return nvme_c2h(n
, ((uint8_t *)&log
) + off
, trans_len
, req
);
4251 static uint16_t nvme_get_log(NvmeCtrl
*n
, NvmeRequest
*req
)
4253 NvmeCmd
*cmd
= &req
->cmd
;
4255 uint32_t dw10
= le32_to_cpu(cmd
->cdw10
);
4256 uint32_t dw11
= le32_to_cpu(cmd
->cdw11
);
4257 uint32_t dw12
= le32_to_cpu(cmd
->cdw12
);
4258 uint32_t dw13
= le32_to_cpu(cmd
->cdw13
);
4259 uint8_t lid
= dw10
& 0xff;
4260 uint8_t lsp
= (dw10
>> 8) & 0xf;
4261 uint8_t rae
= (dw10
>> 15) & 0x1;
4262 uint8_t csi
= le32_to_cpu(cmd
->cdw14
) >> 24;
4263 uint32_t numdl
, numdu
;
4264 uint64_t off
, lpol
, lpou
;
4268 numdl
= (dw10
>> 16);
4269 numdu
= (dw11
& 0xffff);
4273 len
= (((numdu
<< 16) | numdl
) + 1) << 2;
4274 off
= (lpou
<< 32ULL) | lpol
;
4277 return NVME_INVALID_FIELD
| NVME_DNR
;
4280 trace_pci_nvme_get_log(nvme_cid(req
), lid
, lsp
, rae
, len
, off
);
4282 status
= nvme_check_mdts(n
, len
);
4288 case NVME_LOG_ERROR_INFO
:
4289 return nvme_error_info(n
, rae
, len
, off
, req
);
4290 case NVME_LOG_SMART_INFO
:
4291 return nvme_smart_info(n
, rae
, len
, off
, req
);
4292 case NVME_LOG_FW_SLOT_INFO
:
4293 return nvme_fw_log_info(n
, len
, off
, req
);
4294 case NVME_LOG_CHANGED_NSLIST
:
4295 return nvme_changed_nslist(n
, rae
, len
, off
, req
);
4296 case NVME_LOG_CMD_EFFECTS
:
4297 return nvme_cmd_effects(n
, csi
, len
, off
, req
);
4299 trace_pci_nvme_err_invalid_log_page(nvme_cid(req
), lid
);
4300 return NVME_INVALID_FIELD
| NVME_DNR
;
4304 static void nvme_free_cq(NvmeCQueue
*cq
, NvmeCtrl
*n
)
4306 n
->cq
[cq
->cqid
] = NULL
;
4307 timer_free(cq
->timer
);
4308 if (msix_enabled(&n
->parent_obj
)) {
4309 msix_vector_unuse(&n
->parent_obj
, cq
->vector
);
4316 static uint16_t nvme_del_cq(NvmeCtrl
*n
, NvmeRequest
*req
)
4318 NvmeDeleteQ
*c
= (NvmeDeleteQ
*)&req
->cmd
;
4320 uint16_t qid
= le16_to_cpu(c
->qid
);
4322 if (unlikely(!qid
|| nvme_check_cqid(n
, qid
))) {
4323 trace_pci_nvme_err_invalid_del_cq_cqid(qid
);
4324 return NVME_INVALID_CQID
| NVME_DNR
;
4328 if (unlikely(!QTAILQ_EMPTY(&cq
->sq_list
))) {
4329 trace_pci_nvme_err_invalid_del_cq_notempty(qid
);
4330 return NVME_INVALID_QUEUE_DEL
;
4333 if (cq
->irq_enabled
&& cq
->tail
!= cq
->head
) {
4337 nvme_irq_deassert(n
, cq
);
4338 trace_pci_nvme_del_cq(qid
);
4339 nvme_free_cq(cq
, n
);
4340 return NVME_SUCCESS
;
4343 static void nvme_init_cq(NvmeCQueue
*cq
, NvmeCtrl
*n
, uint64_t dma_addr
,
4344 uint16_t cqid
, uint16_t vector
, uint16_t size
,
4345 uint16_t irq_enabled
)
4349 if (msix_enabled(&n
->parent_obj
)) {
4350 ret
= msix_vector_use(&n
->parent_obj
, vector
);
4356 cq
->dma_addr
= dma_addr
;
4358 cq
->irq_enabled
= irq_enabled
;
4359 cq
->vector
= vector
;
4360 cq
->head
= cq
->tail
= 0;
4361 QTAILQ_INIT(&cq
->req_list
);
4362 QTAILQ_INIT(&cq
->sq_list
);
4364 cq
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, nvme_post_cqes
, cq
);
4367 static uint16_t nvme_create_cq(NvmeCtrl
*n
, NvmeRequest
*req
)
4370 NvmeCreateCq
*c
= (NvmeCreateCq
*)&req
->cmd
;
4371 uint16_t cqid
= le16_to_cpu(c
->cqid
);
4372 uint16_t vector
= le16_to_cpu(c
->irq_vector
);
4373 uint16_t qsize
= le16_to_cpu(c
->qsize
);
4374 uint16_t qflags
= le16_to_cpu(c
->cq_flags
);
4375 uint64_t prp1
= le64_to_cpu(c
->prp1
);
4377 trace_pci_nvme_create_cq(prp1
, cqid
, vector
, qsize
, qflags
,
4378 NVME_CQ_FLAGS_IEN(qflags
) != 0);
4380 if (unlikely(!cqid
|| cqid
> n
->params
.max_ioqpairs
||
4381 n
->cq
[cqid
] != NULL
)) {
4382 trace_pci_nvme_err_invalid_create_cq_cqid(cqid
);
4383 return NVME_INVALID_QID
| NVME_DNR
;
4385 if (unlikely(!qsize
|| qsize
> NVME_CAP_MQES(ldq_le_p(&n
->bar
.cap
)))) {
4386 trace_pci_nvme_err_invalid_create_cq_size(qsize
);
4387 return NVME_MAX_QSIZE_EXCEEDED
| NVME_DNR
;
4389 if (unlikely(prp1
& (n
->page_size
- 1))) {
4390 trace_pci_nvme_err_invalid_create_cq_addr(prp1
);
4391 return NVME_INVALID_PRP_OFFSET
| NVME_DNR
;
4393 if (unlikely(!msix_enabled(&n
->parent_obj
) && vector
)) {
4394 trace_pci_nvme_err_invalid_create_cq_vector(vector
);
4395 return NVME_INVALID_IRQ_VECTOR
| NVME_DNR
;
4397 if (unlikely(vector
>= n
->params
.msix_qsize
)) {
4398 trace_pci_nvme_err_invalid_create_cq_vector(vector
);
4399 return NVME_INVALID_IRQ_VECTOR
| NVME_DNR
;
4401 if (unlikely(!(NVME_CQ_FLAGS_PC(qflags
)))) {
4402 trace_pci_nvme_err_invalid_create_cq_qflags(NVME_CQ_FLAGS_PC(qflags
));
4403 return NVME_INVALID_FIELD
| NVME_DNR
;
4406 cq
= g_malloc0(sizeof(*cq
));
4407 nvme_init_cq(cq
, n
, prp1
, cqid
, vector
, qsize
+ 1,
4408 NVME_CQ_FLAGS_IEN(qflags
));
4411 * It is only required to set qs_created when creating a completion queue;
4412 * creating a submission queue without a matching completion queue will
4415 n
->qs_created
= true;
4416 return NVME_SUCCESS
;
4419 static uint16_t nvme_rpt_empty_id_struct(NvmeCtrl
*n
, NvmeRequest
*req
)
4421 uint8_t id
[NVME_IDENTIFY_DATA_SIZE
] = {};
4423 return nvme_c2h(n
, id
, sizeof(id
), req
);
4426 static uint16_t nvme_identify_ctrl(NvmeCtrl
*n
, NvmeRequest
*req
)
4428 trace_pci_nvme_identify_ctrl();
4430 return nvme_c2h(n
, (uint8_t *)&n
->id_ctrl
, sizeof(n
->id_ctrl
), req
);
4433 static uint16_t nvme_identify_ctrl_csi(NvmeCtrl
*n
, NvmeRequest
*req
)
4435 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
4436 uint8_t id
[NVME_IDENTIFY_DATA_SIZE
] = {};
4437 NvmeIdCtrlNvm
*id_nvm
= (NvmeIdCtrlNvm
*)&id
;
4439 trace_pci_nvme_identify_ctrl_csi(c
->csi
);
4443 id_nvm
->vsl
= n
->params
.vsl
;
4444 id_nvm
->dmrsl
= cpu_to_le32(n
->dmrsl
);
4447 case NVME_CSI_ZONED
:
4448 ((NvmeIdCtrlZoned
*)&id
)->zasl
= n
->params
.zasl
;
4452 return NVME_INVALID_FIELD
| NVME_DNR
;
4455 return nvme_c2h(n
, id
, sizeof(id
), req
);
4458 static uint16_t nvme_identify_ns(NvmeCtrl
*n
, NvmeRequest
*req
, bool active
)
4461 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
4462 uint32_t nsid
= le32_to_cpu(c
->nsid
);
4464 trace_pci_nvme_identify_ns(nsid
);
4466 if (!nvme_nsid_valid(n
, nsid
) || nsid
== NVME_NSID_BROADCAST
) {
4467 return NVME_INVALID_NSID
| NVME_DNR
;
4470 ns
= nvme_ns(n
, nsid
);
4471 if (unlikely(!ns
)) {
4473 ns
= nvme_subsys_ns(n
->subsys
, nsid
);
4475 return nvme_rpt_empty_id_struct(n
, req
);
4478 return nvme_rpt_empty_id_struct(n
, req
);
4482 if (active
|| ns
->csi
== NVME_CSI_NVM
) {
4483 return nvme_c2h(n
, (uint8_t *)&ns
->id_ns
, sizeof(NvmeIdNs
), req
);
4486 return NVME_INVALID_CMD_SET
| NVME_DNR
;
4489 static uint16_t nvme_identify_ctrl_list(NvmeCtrl
*n
, NvmeRequest
*req
,
4492 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
4493 uint32_t nsid
= le32_to_cpu(c
->nsid
);
4494 uint16_t min_id
= le16_to_cpu(c
->ctrlid
);
4495 uint16_t list
[NVME_CONTROLLER_LIST_SIZE
] = {};
4496 uint16_t *ids
= &list
[1];
4499 int cntlid
, nr_ids
= 0;
4501 trace_pci_nvme_identify_ctrl_list(c
->cns
, min_id
);
4504 return NVME_INVALID_FIELD
| NVME_DNR
;
4508 if (nsid
== NVME_NSID_BROADCAST
) {
4509 return NVME_INVALID_FIELD
| NVME_DNR
;
4512 ns
= nvme_subsys_ns(n
->subsys
, nsid
);
4514 return NVME_INVALID_FIELD
| NVME_DNR
;
4518 for (cntlid
= min_id
; cntlid
< ARRAY_SIZE(n
->subsys
->ctrls
); cntlid
++) {
4519 ctrl
= nvme_subsys_ctrl(n
->subsys
, cntlid
);
4524 if (attached
&& !nvme_ns(ctrl
, nsid
)) {
4528 ids
[nr_ids
++] = cntlid
;
4533 return nvme_c2h(n
, (uint8_t *)list
, sizeof(list
), req
);
4536 static uint16_t nvme_identify_ns_csi(NvmeCtrl
*n
, NvmeRequest
*req
,
4540 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
4541 uint32_t nsid
= le32_to_cpu(c
->nsid
);
4543 trace_pci_nvme_identify_ns_csi(nsid
, c
->csi
);
4545 if (!nvme_nsid_valid(n
, nsid
) || nsid
== NVME_NSID_BROADCAST
) {
4546 return NVME_INVALID_NSID
| NVME_DNR
;
4549 ns
= nvme_ns(n
, nsid
);
4550 if (unlikely(!ns
)) {
4552 ns
= nvme_subsys_ns(n
->subsys
, nsid
);
4554 return nvme_rpt_empty_id_struct(n
, req
);
4557 return nvme_rpt_empty_id_struct(n
, req
);
4561 if (c
->csi
== NVME_CSI_NVM
) {
4562 return nvme_rpt_empty_id_struct(n
, req
);
4563 } else if (c
->csi
== NVME_CSI_ZONED
&& ns
->csi
== NVME_CSI_ZONED
) {
4564 return nvme_c2h(n
, (uint8_t *)ns
->id_ns_zoned
, sizeof(NvmeIdNsZoned
),
4568 return NVME_INVALID_FIELD
| NVME_DNR
;
4571 static uint16_t nvme_identify_nslist(NvmeCtrl
*n
, NvmeRequest
*req
,
4575 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
4576 uint32_t min_nsid
= le32_to_cpu(c
->nsid
);
4577 uint8_t list
[NVME_IDENTIFY_DATA_SIZE
] = {};
4578 static const int data_len
= sizeof(list
);
4579 uint32_t *list_ptr
= (uint32_t *)list
;
4582 trace_pci_nvme_identify_nslist(min_nsid
);
4585 * Both FFFFFFFFh (NVME_NSID_BROADCAST) and FFFFFFFFEh are invalid values
4586 * since the Active Namespace ID List should return namespaces with ids
4587 * *higher* than the NSID specified in the command. This is also specified
4588 * in the spec (NVM Express v1.3d, Section 5.15.4).
4590 if (min_nsid
>= NVME_NSID_BROADCAST
- 1) {
4591 return NVME_INVALID_NSID
| NVME_DNR
;
4594 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
4598 ns
= nvme_subsys_ns(n
->subsys
, i
);
4606 if (ns
->params
.nsid
<= min_nsid
) {
4609 list_ptr
[j
++] = cpu_to_le32(ns
->params
.nsid
);
4610 if (j
== data_len
/ sizeof(uint32_t)) {
4615 return nvme_c2h(n
, list
, data_len
, req
);
4618 static uint16_t nvme_identify_nslist_csi(NvmeCtrl
*n
, NvmeRequest
*req
,
4622 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
4623 uint32_t min_nsid
= le32_to_cpu(c
->nsid
);
4624 uint8_t list
[NVME_IDENTIFY_DATA_SIZE
] = {};
4625 static const int data_len
= sizeof(list
);
4626 uint32_t *list_ptr
= (uint32_t *)list
;
4629 trace_pci_nvme_identify_nslist_csi(min_nsid
, c
->csi
);
4632 * Same as in nvme_identify_nslist(), FFFFFFFFh/FFFFFFFFEh are invalid.
4634 if (min_nsid
>= NVME_NSID_BROADCAST
- 1) {
4635 return NVME_INVALID_NSID
| NVME_DNR
;
4638 if (c
->csi
!= NVME_CSI_NVM
&& c
->csi
!= NVME_CSI_ZONED
) {
4639 return NVME_INVALID_FIELD
| NVME_DNR
;
4642 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
4646 ns
= nvme_subsys_ns(n
->subsys
, i
);
4654 if (ns
->params
.nsid
<= min_nsid
|| c
->csi
!= ns
->csi
) {
4657 list_ptr
[j
++] = cpu_to_le32(ns
->params
.nsid
);
4658 if (j
== data_len
/ sizeof(uint32_t)) {
4663 return nvme_c2h(n
, list
, data_len
, req
);
4666 static uint16_t nvme_identify_ns_descr_list(NvmeCtrl
*n
, NvmeRequest
*req
)
4669 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
4670 uint32_t nsid
= le32_to_cpu(c
->nsid
);
4671 uint8_t list
[NVME_IDENTIFY_DATA_SIZE
] = {};
4672 uint8_t *pos
= list
;
4675 uint8_t v
[NVME_NIDL_UUID
];
4676 } QEMU_PACKED uuid
= {};
4680 } QEMU_PACKED eui64
= {};
4684 } QEMU_PACKED csi
= {};
4686 trace_pci_nvme_identify_ns_descr_list(nsid
);
4688 if (!nvme_nsid_valid(n
, nsid
) || nsid
== NVME_NSID_BROADCAST
) {
4689 return NVME_INVALID_NSID
| NVME_DNR
;
4692 ns
= nvme_ns(n
, nsid
);
4693 if (unlikely(!ns
)) {
4694 return NVME_INVALID_FIELD
| NVME_DNR
;
4698 * If the EUI-64 field is 0 and the NGUID field is 0, the namespace must
4699 * provide a valid Namespace UUID in the Namespace Identification Descriptor
4700 * data structure. QEMU does not yet support setting NGUID.
4702 uuid
.hdr
.nidt
= NVME_NIDT_UUID
;
4703 uuid
.hdr
.nidl
= NVME_NIDL_UUID
;
4704 memcpy(uuid
.v
, ns
->params
.uuid
.data
, NVME_NIDL_UUID
);
4705 memcpy(pos
, &uuid
, sizeof(uuid
));
4706 pos
+= sizeof(uuid
);
4708 if (ns
->params
.eui64
) {
4709 eui64
.hdr
.nidt
= NVME_NIDT_EUI64
;
4710 eui64
.hdr
.nidl
= NVME_NIDL_EUI64
;
4711 eui64
.v
= cpu_to_be64(ns
->params
.eui64
);
4712 memcpy(pos
, &eui64
, sizeof(eui64
));
4713 pos
+= sizeof(eui64
);
4716 csi
.hdr
.nidt
= NVME_NIDT_CSI
;
4717 csi
.hdr
.nidl
= NVME_NIDL_CSI
;
4719 memcpy(pos
, &csi
, sizeof(csi
));
4722 return nvme_c2h(n
, list
, sizeof(list
), req
);
4725 static uint16_t nvme_identify_cmd_set(NvmeCtrl
*n
, NvmeRequest
*req
)
4727 uint8_t list
[NVME_IDENTIFY_DATA_SIZE
] = {};
4728 static const int data_len
= sizeof(list
);
4730 trace_pci_nvme_identify_cmd_set();
4732 NVME_SET_CSI(*list
, NVME_CSI_NVM
);
4733 NVME_SET_CSI(*list
, NVME_CSI_ZONED
);
4735 return nvme_c2h(n
, list
, data_len
, req
);
4738 static uint16_t nvme_identify(NvmeCtrl
*n
, NvmeRequest
*req
)
4740 NvmeIdentify
*c
= (NvmeIdentify
*)&req
->cmd
;
4742 trace_pci_nvme_identify(nvme_cid(req
), c
->cns
, le16_to_cpu(c
->ctrlid
),
4746 case NVME_ID_CNS_NS
:
4747 return nvme_identify_ns(n
, req
, true);
4748 case NVME_ID_CNS_NS_PRESENT
:
4749 return nvme_identify_ns(n
, req
, false);
4750 case NVME_ID_CNS_NS_ATTACHED_CTRL_LIST
:
4751 return nvme_identify_ctrl_list(n
, req
, true);
4752 case NVME_ID_CNS_CTRL_LIST
:
4753 return nvme_identify_ctrl_list(n
, req
, false);
4754 case NVME_ID_CNS_CS_NS
:
4755 return nvme_identify_ns_csi(n
, req
, true);
4756 case NVME_ID_CNS_CS_NS_PRESENT
:
4757 return nvme_identify_ns_csi(n
, req
, false);
4758 case NVME_ID_CNS_CTRL
:
4759 return nvme_identify_ctrl(n
, req
);
4760 case NVME_ID_CNS_CS_CTRL
:
4761 return nvme_identify_ctrl_csi(n
, req
);
4762 case NVME_ID_CNS_NS_ACTIVE_LIST
:
4763 return nvme_identify_nslist(n
, req
, true);
4764 case NVME_ID_CNS_NS_PRESENT_LIST
:
4765 return nvme_identify_nslist(n
, req
, false);
4766 case NVME_ID_CNS_CS_NS_ACTIVE_LIST
:
4767 return nvme_identify_nslist_csi(n
, req
, true);
4768 case NVME_ID_CNS_CS_NS_PRESENT_LIST
:
4769 return nvme_identify_nslist_csi(n
, req
, false);
4770 case NVME_ID_CNS_NS_DESCR_LIST
:
4771 return nvme_identify_ns_descr_list(n
, req
);
4772 case NVME_ID_CNS_IO_COMMAND_SET
:
4773 return nvme_identify_cmd_set(n
, req
);
4775 trace_pci_nvme_err_invalid_identify_cns(le32_to_cpu(c
->cns
));
4776 return NVME_INVALID_FIELD
| NVME_DNR
;
4780 static uint16_t nvme_abort(NvmeCtrl
*n
, NvmeRequest
*req
)
4782 uint16_t sqid
= le32_to_cpu(req
->cmd
.cdw10
) & 0xffff;
4784 req
->cqe
.result
= 1;
4785 if (nvme_check_sqid(n
, sqid
)) {
4786 return NVME_INVALID_FIELD
| NVME_DNR
;
4789 return NVME_SUCCESS
;
4792 static inline void nvme_set_timestamp(NvmeCtrl
*n
, uint64_t ts
)
4794 trace_pci_nvme_setfeat_timestamp(ts
);
4796 n
->host_timestamp
= le64_to_cpu(ts
);
4797 n
->timestamp_set_qemu_clock_ms
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
);
4800 static inline uint64_t nvme_get_timestamp(const NvmeCtrl
*n
)
4802 uint64_t current_time
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
);
4803 uint64_t elapsed_time
= current_time
- n
->timestamp_set_qemu_clock_ms
;
4805 union nvme_timestamp
{
4807 uint64_t timestamp
:48;
4815 union nvme_timestamp ts
;
4817 ts
.timestamp
= n
->host_timestamp
+ elapsed_time
;
4819 /* If the host timestamp is non-zero, set the timestamp origin */
4820 ts
.origin
= n
->host_timestamp
? 0x01 : 0x00;
4822 trace_pci_nvme_getfeat_timestamp(ts
.all
);
4824 return cpu_to_le64(ts
.all
);
4827 static uint16_t nvme_get_feature_timestamp(NvmeCtrl
*n
, NvmeRequest
*req
)
4829 uint64_t timestamp
= nvme_get_timestamp(n
);
4831 return nvme_c2h(n
, (uint8_t *)×tamp
, sizeof(timestamp
), req
);
4834 static uint16_t nvme_get_feature(NvmeCtrl
*n
, NvmeRequest
*req
)
4836 NvmeCmd
*cmd
= &req
->cmd
;
4837 uint32_t dw10
= le32_to_cpu(cmd
->cdw10
);
4838 uint32_t dw11
= le32_to_cpu(cmd
->cdw11
);
4839 uint32_t nsid
= le32_to_cpu(cmd
->nsid
);
4841 uint8_t fid
= NVME_GETSETFEAT_FID(dw10
);
4842 NvmeGetFeatureSelect sel
= NVME_GETFEAT_SELECT(dw10
);
4847 static const uint32_t nvme_feature_default
[NVME_FID_MAX
] = {
4848 [NVME_ARBITRATION
] = NVME_ARB_AB_NOLIMIT
,
4851 trace_pci_nvme_getfeat(nvme_cid(req
), nsid
, fid
, sel
, dw11
);
4853 if (!nvme_feature_support
[fid
]) {
4854 return NVME_INVALID_FIELD
| NVME_DNR
;
4857 if (nvme_feature_cap
[fid
] & NVME_FEAT_CAP_NS
) {
4858 if (!nvme_nsid_valid(n
, nsid
) || nsid
== NVME_NSID_BROADCAST
) {
4860 * The Reservation Notification Mask and Reservation Persistence
4861 * features require a status code of Invalid Field in Command when
4862 * NSID is FFFFFFFFh. Since the device does not support those
4863 * features we can always return Invalid Namespace or Format as we
4864 * should do for all other features.
4866 return NVME_INVALID_NSID
| NVME_DNR
;
4869 if (!nvme_ns(n
, nsid
)) {
4870 return NVME_INVALID_FIELD
| NVME_DNR
;
4875 case NVME_GETFEAT_SELECT_CURRENT
:
4877 case NVME_GETFEAT_SELECT_SAVED
:
4878 /* no features are saveable by the controller; fallthrough */
4879 case NVME_GETFEAT_SELECT_DEFAULT
:
4881 case NVME_GETFEAT_SELECT_CAP
:
4882 result
= nvme_feature_cap
[fid
];
4887 case NVME_TEMPERATURE_THRESHOLD
:
4891 * The controller only implements the Composite Temperature sensor, so
4892 * return 0 for all other sensors.
4894 if (NVME_TEMP_TMPSEL(dw11
) != NVME_TEMP_TMPSEL_COMPOSITE
) {
4898 switch (NVME_TEMP_THSEL(dw11
)) {
4899 case NVME_TEMP_THSEL_OVER
:
4900 result
= n
->features
.temp_thresh_hi
;
4902 case NVME_TEMP_THSEL_UNDER
:
4903 result
= n
->features
.temp_thresh_low
;
4907 return NVME_INVALID_FIELD
| NVME_DNR
;
4908 case NVME_ERROR_RECOVERY
:
4909 if (!nvme_nsid_valid(n
, nsid
)) {
4910 return NVME_INVALID_NSID
| NVME_DNR
;
4913 ns
= nvme_ns(n
, nsid
);
4914 if (unlikely(!ns
)) {
4915 return NVME_INVALID_FIELD
| NVME_DNR
;
4918 result
= ns
->features
.err_rec
;
4920 case NVME_VOLATILE_WRITE_CACHE
:
4922 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
4928 result
= blk_enable_write_cache(ns
->blkconf
.blk
);
4933 trace_pci_nvme_getfeat_vwcache(result
? "enabled" : "disabled");
4935 case NVME_ASYNCHRONOUS_EVENT_CONF
:
4936 result
= n
->features
.async_config
;
4938 case NVME_TIMESTAMP
:
4939 return nvme_get_feature_timestamp(n
, req
);
4946 case NVME_TEMPERATURE_THRESHOLD
:
4949 if (NVME_TEMP_TMPSEL(dw11
) != NVME_TEMP_TMPSEL_COMPOSITE
) {
4953 if (NVME_TEMP_THSEL(dw11
) == NVME_TEMP_THSEL_OVER
) {
4954 result
= NVME_TEMPERATURE_WARNING
;
4958 case NVME_NUMBER_OF_QUEUES
:
4959 result
= (n
->params
.max_ioqpairs
- 1) |
4960 ((n
->params
.max_ioqpairs
- 1) << 16);
4961 trace_pci_nvme_getfeat_numq(result
);
4963 case NVME_INTERRUPT_VECTOR_CONF
:
4965 if (iv
>= n
->params
.max_ioqpairs
+ 1) {
4966 return NVME_INVALID_FIELD
| NVME_DNR
;
4970 if (iv
== n
->admin_cq
.vector
) {
4971 result
|= NVME_INTVC_NOCOALESCING
;
4975 result
= nvme_feature_default
[fid
];
4980 req
->cqe
.result
= cpu_to_le32(result
);
4981 return NVME_SUCCESS
;
4984 static uint16_t nvme_set_feature_timestamp(NvmeCtrl
*n
, NvmeRequest
*req
)
4989 ret
= nvme_h2c(n
, (uint8_t *)×tamp
, sizeof(timestamp
), req
);
4994 nvme_set_timestamp(n
, timestamp
);
4996 return NVME_SUCCESS
;
4999 static uint16_t nvme_set_feature(NvmeCtrl
*n
, NvmeRequest
*req
)
5001 NvmeNamespace
*ns
= NULL
;
5003 NvmeCmd
*cmd
= &req
->cmd
;
5004 uint32_t dw10
= le32_to_cpu(cmd
->cdw10
);
5005 uint32_t dw11
= le32_to_cpu(cmd
->cdw11
);
5006 uint32_t nsid
= le32_to_cpu(cmd
->nsid
);
5007 uint8_t fid
= NVME_GETSETFEAT_FID(dw10
);
5008 uint8_t save
= NVME_SETFEAT_SAVE(dw10
);
5011 trace_pci_nvme_setfeat(nvme_cid(req
), nsid
, fid
, save
, dw11
);
5013 if (save
&& !(nvme_feature_cap
[fid
] & NVME_FEAT_CAP_SAVE
)) {
5014 return NVME_FID_NOT_SAVEABLE
| NVME_DNR
;
5017 if (!nvme_feature_support
[fid
]) {
5018 return NVME_INVALID_FIELD
| NVME_DNR
;
5021 if (nvme_feature_cap
[fid
] & NVME_FEAT_CAP_NS
) {
5022 if (nsid
!= NVME_NSID_BROADCAST
) {
5023 if (!nvme_nsid_valid(n
, nsid
)) {
5024 return NVME_INVALID_NSID
| NVME_DNR
;
5027 ns
= nvme_ns(n
, nsid
);
5028 if (unlikely(!ns
)) {
5029 return NVME_INVALID_FIELD
| NVME_DNR
;
5032 } else if (nsid
&& nsid
!= NVME_NSID_BROADCAST
) {
5033 if (!nvme_nsid_valid(n
, nsid
)) {
5034 return NVME_INVALID_NSID
| NVME_DNR
;
5037 return NVME_FEAT_NOT_NS_SPEC
| NVME_DNR
;
5040 if (!(nvme_feature_cap
[fid
] & NVME_FEAT_CAP_CHANGE
)) {
5041 return NVME_FEAT_NOT_CHANGEABLE
| NVME_DNR
;
5045 case NVME_TEMPERATURE_THRESHOLD
:
5046 if (NVME_TEMP_TMPSEL(dw11
) != NVME_TEMP_TMPSEL_COMPOSITE
) {
5050 switch (NVME_TEMP_THSEL(dw11
)) {
5051 case NVME_TEMP_THSEL_OVER
:
5052 n
->features
.temp_thresh_hi
= NVME_TEMP_TMPTH(dw11
);
5054 case NVME_TEMP_THSEL_UNDER
:
5055 n
->features
.temp_thresh_low
= NVME_TEMP_TMPTH(dw11
);
5058 return NVME_INVALID_FIELD
| NVME_DNR
;
5061 if ((n
->temperature
>= n
->features
.temp_thresh_hi
) ||
5062 (n
->temperature
<= n
->features
.temp_thresh_low
)) {
5063 nvme_smart_event(n
, NVME_AER_INFO_SMART_TEMP_THRESH
);
5067 case NVME_ERROR_RECOVERY
:
5068 if (nsid
== NVME_NSID_BROADCAST
) {
5069 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
5076 if (NVME_ID_NS_NSFEAT_DULBE(ns
->id_ns
.nsfeat
)) {
5077 ns
->features
.err_rec
= dw11
;
5085 if (NVME_ID_NS_NSFEAT_DULBE(ns
->id_ns
.nsfeat
)) {
5086 ns
->features
.err_rec
= dw11
;
5089 case NVME_VOLATILE_WRITE_CACHE
:
5090 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
5096 if (!(dw11
& 0x1) && blk_enable_write_cache(ns
->blkconf
.blk
)) {
5097 blk_flush(ns
->blkconf
.blk
);
5100 blk_set_enable_write_cache(ns
->blkconf
.blk
, dw11
& 1);
5105 case NVME_NUMBER_OF_QUEUES
:
5106 if (n
->qs_created
) {
5107 return NVME_CMD_SEQ_ERROR
| NVME_DNR
;
5111 * NVMe v1.3, Section 5.21.1.7: FFFFh is not an allowed value for NCQR
5114 if ((dw11
& 0xffff) == 0xffff || ((dw11
>> 16) & 0xffff) == 0xffff) {
5115 return NVME_INVALID_FIELD
| NVME_DNR
;
5118 trace_pci_nvme_setfeat_numq((dw11
& 0xffff) + 1,
5119 ((dw11
>> 16) & 0xffff) + 1,
5120 n
->params
.max_ioqpairs
,
5121 n
->params
.max_ioqpairs
);
5122 req
->cqe
.result
= cpu_to_le32((n
->params
.max_ioqpairs
- 1) |
5123 ((n
->params
.max_ioqpairs
- 1) << 16));
5125 case NVME_ASYNCHRONOUS_EVENT_CONF
:
5126 n
->features
.async_config
= dw11
;
5128 case NVME_TIMESTAMP
:
5129 return nvme_set_feature_timestamp(n
, req
);
5130 case NVME_COMMAND_SET_PROFILE
:
5132 trace_pci_nvme_err_invalid_iocsci(dw11
& 0x1ff);
5133 return NVME_CMD_SET_CMB_REJECTED
| NVME_DNR
;
5137 return NVME_FEAT_NOT_CHANGEABLE
| NVME_DNR
;
5139 return NVME_SUCCESS
;
5142 static uint16_t nvme_aer(NvmeCtrl
*n
, NvmeRequest
*req
)
5144 trace_pci_nvme_aer(nvme_cid(req
));
5146 if (n
->outstanding_aers
> n
->params
.aerl
) {
5147 trace_pci_nvme_aer_aerl_exceeded();
5148 return NVME_AER_LIMIT_EXCEEDED
;
5151 n
->aer_reqs
[n
->outstanding_aers
] = req
;
5152 n
->outstanding_aers
++;
5154 if (!QTAILQ_EMPTY(&n
->aer_queue
)) {
5155 nvme_process_aers(n
);
5158 return NVME_NO_COMPLETE
;
5161 static void nvme_update_dmrsl(NvmeCtrl
*n
)
5165 for (nsid
= 1; nsid
<= NVME_MAX_NAMESPACES
; nsid
++) {
5166 NvmeNamespace
*ns
= nvme_ns(n
, nsid
);
5171 n
->dmrsl
= MIN_NON_ZERO(n
->dmrsl
,
5172 BDRV_REQUEST_MAX_BYTES
/ nvme_l2b(ns
, 1));
5176 static void nvme_select_iocs_ns(NvmeCtrl
*n
, NvmeNamespace
*ns
)
5178 uint32_t cc
= ldl_le_p(&n
->bar
.cc
);
5180 ns
->iocs
= nvme_cse_iocs_none
;
5183 if (NVME_CC_CSS(cc
) != NVME_CC_CSS_ADMIN_ONLY
) {
5184 ns
->iocs
= nvme_cse_iocs_nvm
;
5187 case NVME_CSI_ZONED
:
5188 if (NVME_CC_CSS(cc
) == NVME_CC_CSS_CSI
) {
5189 ns
->iocs
= nvme_cse_iocs_zoned
;
5190 } else if (NVME_CC_CSS(cc
) == NVME_CC_CSS_NVM
) {
5191 ns
->iocs
= nvme_cse_iocs_nvm
;
5197 static uint16_t nvme_ns_attachment(NvmeCtrl
*n
, NvmeRequest
*req
)
5201 uint16_t list
[NVME_CONTROLLER_LIST_SIZE
] = {};
5202 uint32_t nsid
= le32_to_cpu(req
->cmd
.nsid
);
5203 uint32_t dw10
= le32_to_cpu(req
->cmd
.cdw10
);
5204 uint8_t sel
= dw10
& 0xf;
5205 uint16_t *nr_ids
= &list
[0];
5206 uint16_t *ids
= &list
[1];
5210 trace_pci_nvme_ns_attachment(nvme_cid(req
), dw10
& 0xf);
5212 if (!nvme_nsid_valid(n
, nsid
)) {
5213 return NVME_INVALID_NSID
| NVME_DNR
;
5216 ns
= nvme_subsys_ns(n
->subsys
, nsid
);
5218 return NVME_INVALID_FIELD
| NVME_DNR
;
5221 ret
= nvme_h2c(n
, (uint8_t *)list
, 4096, req
);
5227 return NVME_NS_CTRL_LIST_INVALID
| NVME_DNR
;
5230 *nr_ids
= MIN(*nr_ids
, NVME_CONTROLLER_LIST_SIZE
- 1);
5231 for (i
= 0; i
< *nr_ids
; i
++) {
5232 ctrl
= nvme_subsys_ctrl(n
->subsys
, ids
[i
]);
5234 return NVME_NS_CTRL_LIST_INVALID
| NVME_DNR
;
5238 case NVME_NS_ATTACHMENT_ATTACH
:
5239 if (nvme_ns(ctrl
, nsid
)) {
5240 return NVME_NS_ALREADY_ATTACHED
| NVME_DNR
;
5243 if (ns
->attached
&& !ns
->params
.shared
) {
5244 return NVME_NS_PRIVATE
| NVME_DNR
;
5247 nvme_attach_ns(ctrl
, ns
);
5248 nvme_select_iocs_ns(ctrl
, ns
);
5252 case NVME_NS_ATTACHMENT_DETACH
:
5253 if (!nvme_ns(ctrl
, nsid
)) {
5254 return NVME_NS_NOT_ATTACHED
| NVME_DNR
;
5257 ctrl
->namespaces
[nsid
] = NULL
;
5260 nvme_update_dmrsl(ctrl
);
5265 return NVME_INVALID_FIELD
| NVME_DNR
;
5269 * Add namespace id to the changed namespace id list for event clearing
5270 * via Get Log Page command.
5272 if (!test_and_set_bit(nsid
, ctrl
->changed_nsids
)) {
5273 nvme_enqueue_event(ctrl
, NVME_AER_TYPE_NOTICE
,
5274 NVME_AER_INFO_NOTICE_NS_ATTR_CHANGED
,
5275 NVME_LOG_CHANGED_NSLIST
);
5279 return NVME_SUCCESS
;
5282 typedef struct NvmeFormatAIOCB
{
5295 static void nvme_format_bh(void *opaque
);
5297 static void nvme_format_cancel(BlockAIOCB
*aiocb
)
5299 NvmeFormatAIOCB
*iocb
= container_of(aiocb
, NvmeFormatAIOCB
, common
);
5302 blk_aio_cancel_async(iocb
->aiocb
);
5306 static const AIOCBInfo nvme_format_aiocb_info
= {
5307 .aiocb_size
= sizeof(NvmeFormatAIOCB
),
5308 .cancel_async
= nvme_format_cancel
,
5309 .get_aio_context
= nvme_get_aio_context
,
5312 static void nvme_format_set(NvmeNamespace
*ns
, NvmeCmd
*cmd
)
5314 uint32_t dw10
= le32_to_cpu(cmd
->cdw10
);
5315 uint8_t lbaf
= dw10
& 0xf;
5316 uint8_t pi
= (dw10
>> 5) & 0x7;
5317 uint8_t mset
= (dw10
>> 4) & 0x1;
5318 uint8_t pil
= (dw10
>> 8) & 0x1;
5320 trace_pci_nvme_format_set(ns
->params
.nsid
, lbaf
, mset
, pi
, pil
);
5322 ns
->id_ns
.dps
= (pil
<< 3) | pi
;
5323 ns
->id_ns
.flbas
= lbaf
| (mset
<< 4);
5325 nvme_ns_init_format(ns
);
5328 static void nvme_format_ns_cb(void *opaque
, int ret
)
5330 NvmeFormatAIOCB
*iocb
= opaque
;
5331 NvmeRequest
*req
= iocb
->req
;
5332 NvmeNamespace
*ns
= iocb
->ns
;
5342 if (iocb
->offset
< ns
->size
) {
5343 bytes
= MIN(BDRV_REQUEST_MAX_BYTES
, ns
->size
- iocb
->offset
);
5345 iocb
->aiocb
= blk_aio_pwrite_zeroes(ns
->blkconf
.blk
, iocb
->offset
,
5346 bytes
, BDRV_REQ_MAY_UNMAP
,
5347 nvme_format_ns_cb
, iocb
);
5349 iocb
->offset
+= bytes
;
5353 nvme_format_set(ns
, &req
->cmd
);
5360 qemu_bh_schedule(iocb
->bh
);
5363 static uint16_t nvme_format_check(NvmeNamespace
*ns
, uint8_t lbaf
, uint8_t pi
)
5365 if (ns
->params
.zoned
) {
5366 return NVME_INVALID_FORMAT
| NVME_DNR
;
5369 if (lbaf
> ns
->id_ns
.nlbaf
) {
5370 return NVME_INVALID_FORMAT
| NVME_DNR
;
5373 if (pi
&& (ns
->id_ns
.lbaf
[lbaf
].ms
< sizeof(NvmeDifTuple
))) {
5374 return NVME_INVALID_FORMAT
| NVME_DNR
;
5377 if (pi
&& pi
> NVME_ID_NS_DPS_TYPE_3
) {
5378 return NVME_INVALID_FIELD
| NVME_DNR
;
5381 return NVME_SUCCESS
;
5384 static void nvme_format_bh(void *opaque
)
5386 NvmeFormatAIOCB
*iocb
= opaque
;
5387 NvmeRequest
*req
= iocb
->req
;
5388 NvmeCtrl
*n
= nvme_ctrl(req
);
5389 uint32_t dw10
= le32_to_cpu(req
->cmd
.cdw10
);
5390 uint8_t lbaf
= dw10
& 0xf;
5391 uint8_t pi
= (dw10
>> 5) & 0x7;
5395 if (iocb
->ret
< 0) {
5399 if (iocb
->broadcast
) {
5400 for (i
= iocb
->nsid
+ 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
5401 iocb
->ns
= nvme_ns(n
, i
);
5413 status
= nvme_format_check(iocb
->ns
, lbaf
, pi
);
5415 req
->status
= status
;
5419 iocb
->ns
->status
= NVME_FORMAT_IN_PROGRESS
;
5420 nvme_format_ns_cb(iocb
, 0);
5424 qemu_bh_delete(iocb
->bh
);
5427 iocb
->common
.cb(iocb
->common
.opaque
, iocb
->ret
);
5429 qemu_aio_unref(iocb
);
5432 static uint16_t nvme_format(NvmeCtrl
*n
, NvmeRequest
*req
)
5434 NvmeFormatAIOCB
*iocb
;
5435 uint32_t nsid
= le32_to_cpu(req
->cmd
.nsid
);
5438 iocb
= qemu_aio_get(&nvme_format_aiocb_info
, NULL
, nvme_misc_cb
, req
);
5441 iocb
->bh
= qemu_bh_new(nvme_format_bh
, iocb
);
5445 iocb
->broadcast
= (nsid
== NVME_NSID_BROADCAST
);
5448 if (!iocb
->broadcast
) {
5449 if (!nvme_nsid_valid(n
, nsid
)) {
5450 status
= NVME_INVALID_NSID
| NVME_DNR
;
5454 iocb
->ns
= nvme_ns(n
, nsid
);
5456 status
= NVME_INVALID_FIELD
| NVME_DNR
;
5461 req
->aiocb
= &iocb
->common
;
5462 qemu_bh_schedule(iocb
->bh
);
5464 return NVME_NO_COMPLETE
;
5467 qemu_bh_delete(iocb
->bh
);
5469 qemu_aio_unref(iocb
);
5473 static uint16_t nvme_admin_cmd(NvmeCtrl
*n
, NvmeRequest
*req
)
5475 trace_pci_nvme_admin_cmd(nvme_cid(req
), nvme_sqid(req
), req
->cmd
.opcode
,
5476 nvme_adm_opc_str(req
->cmd
.opcode
));
5478 if (!(nvme_cse_acs
[req
->cmd
.opcode
] & NVME_CMD_EFF_CSUPP
)) {
5479 trace_pci_nvme_err_invalid_admin_opc(req
->cmd
.opcode
);
5480 return NVME_INVALID_OPCODE
| NVME_DNR
;
5483 /* SGLs shall not be used for Admin commands in NVMe over PCIe */
5484 if (NVME_CMD_FLAGS_PSDT(req
->cmd
.flags
) != NVME_PSDT_PRP
) {
5485 return NVME_INVALID_FIELD
| NVME_DNR
;
5488 if (NVME_CMD_FLAGS_FUSE(req
->cmd
.flags
)) {
5489 return NVME_INVALID_FIELD
;
5492 switch (req
->cmd
.opcode
) {
5493 case NVME_ADM_CMD_DELETE_SQ
:
5494 return nvme_del_sq(n
, req
);
5495 case NVME_ADM_CMD_CREATE_SQ
:
5496 return nvme_create_sq(n
, req
);
5497 case NVME_ADM_CMD_GET_LOG_PAGE
:
5498 return nvme_get_log(n
, req
);
5499 case NVME_ADM_CMD_DELETE_CQ
:
5500 return nvme_del_cq(n
, req
);
5501 case NVME_ADM_CMD_CREATE_CQ
:
5502 return nvme_create_cq(n
, req
);
5503 case NVME_ADM_CMD_IDENTIFY
:
5504 return nvme_identify(n
, req
);
5505 case NVME_ADM_CMD_ABORT
:
5506 return nvme_abort(n
, req
);
5507 case NVME_ADM_CMD_SET_FEATURES
:
5508 return nvme_set_feature(n
, req
);
5509 case NVME_ADM_CMD_GET_FEATURES
:
5510 return nvme_get_feature(n
, req
);
5511 case NVME_ADM_CMD_ASYNC_EV_REQ
:
5512 return nvme_aer(n
, req
);
5513 case NVME_ADM_CMD_NS_ATTACHMENT
:
5514 return nvme_ns_attachment(n
, req
);
5515 case NVME_ADM_CMD_FORMAT_NVM
:
5516 return nvme_format(n
, req
);
5521 return NVME_INVALID_OPCODE
| NVME_DNR
;
5524 static void nvme_process_sq(void *opaque
)
5526 NvmeSQueue
*sq
= opaque
;
5527 NvmeCtrl
*n
= sq
->ctrl
;
5528 NvmeCQueue
*cq
= n
->cq
[sq
->cqid
];
5535 while (!(nvme_sq_empty(sq
) || QTAILQ_EMPTY(&sq
->req_list
))) {
5536 addr
= sq
->dma_addr
+ sq
->head
* n
->sqe_size
;
5537 if (nvme_addr_read(n
, addr
, (void *)&cmd
, sizeof(cmd
))) {
5538 trace_pci_nvme_err_addr_read(addr
);
5539 trace_pci_nvme_err_cfs();
5540 stl_le_p(&n
->bar
.csts
, NVME_CSTS_FAILED
);
5543 nvme_inc_sq_head(sq
);
5545 req
= QTAILQ_FIRST(&sq
->req_list
);
5546 QTAILQ_REMOVE(&sq
->req_list
, req
, entry
);
5547 QTAILQ_INSERT_TAIL(&sq
->out_req_list
, req
, entry
);
5548 nvme_req_clear(req
);
5549 req
->cqe
.cid
= cmd
.cid
;
5550 memcpy(&req
->cmd
, &cmd
, sizeof(NvmeCmd
));
5552 status
= sq
->sqid
? nvme_io_cmd(n
, req
) :
5553 nvme_admin_cmd(n
, req
);
5554 if (status
!= NVME_NO_COMPLETE
) {
5555 req
->status
= status
;
5556 nvme_enqueue_req_completion(cq
, req
);
5561 static void nvme_ctrl_reset(NvmeCtrl
*n
)
5566 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
5575 for (i
= 0; i
< n
->params
.max_ioqpairs
+ 1; i
++) {
5576 if (n
->sq
[i
] != NULL
) {
5577 nvme_free_sq(n
->sq
[i
], n
);
5580 for (i
= 0; i
< n
->params
.max_ioqpairs
+ 1; i
++) {
5581 if (n
->cq
[i
] != NULL
) {
5582 nvme_free_cq(n
->cq
[i
], n
);
5586 while (!QTAILQ_EMPTY(&n
->aer_queue
)) {
5587 NvmeAsyncEvent
*event
= QTAILQ_FIRST(&n
->aer_queue
);
5588 QTAILQ_REMOVE(&n
->aer_queue
, event
, entry
);
5593 n
->outstanding_aers
= 0;
5594 n
->qs_created
= false;
5597 static void nvme_ctrl_shutdown(NvmeCtrl
*n
)
5603 memory_region_msync(&n
->pmr
.dev
->mr
, 0, n
->pmr
.dev
->size
);
5606 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
5612 nvme_ns_shutdown(ns
);
5616 static void nvme_select_iocs(NvmeCtrl
*n
)
5621 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
5627 nvme_select_iocs_ns(n
, ns
);
5631 static int nvme_start_ctrl(NvmeCtrl
*n
)
5633 uint64_t cap
= ldq_le_p(&n
->bar
.cap
);
5634 uint32_t cc
= ldl_le_p(&n
->bar
.cc
);
5635 uint32_t aqa
= ldl_le_p(&n
->bar
.aqa
);
5636 uint64_t asq
= ldq_le_p(&n
->bar
.asq
);
5637 uint64_t acq
= ldq_le_p(&n
->bar
.acq
);
5638 uint32_t page_bits
= NVME_CC_MPS(cc
) + 12;
5639 uint32_t page_size
= 1 << page_bits
;
5641 if (unlikely(n
->cq
[0])) {
5642 trace_pci_nvme_err_startfail_cq();
5645 if (unlikely(n
->sq
[0])) {
5646 trace_pci_nvme_err_startfail_sq();
5649 if (unlikely(asq
& (page_size
- 1))) {
5650 trace_pci_nvme_err_startfail_asq_misaligned(asq
);
5653 if (unlikely(acq
& (page_size
- 1))) {
5654 trace_pci_nvme_err_startfail_acq_misaligned(acq
);
5657 if (unlikely(!(NVME_CAP_CSS(cap
) & (1 << NVME_CC_CSS(cc
))))) {
5658 trace_pci_nvme_err_startfail_css(NVME_CC_CSS(cc
));
5661 if (unlikely(NVME_CC_MPS(cc
) < NVME_CAP_MPSMIN(cap
))) {
5662 trace_pci_nvme_err_startfail_page_too_small(
5664 NVME_CAP_MPSMIN(cap
));
5667 if (unlikely(NVME_CC_MPS(cc
) >
5668 NVME_CAP_MPSMAX(cap
))) {
5669 trace_pci_nvme_err_startfail_page_too_large(
5671 NVME_CAP_MPSMAX(cap
));
5674 if (unlikely(NVME_CC_IOCQES(cc
) <
5675 NVME_CTRL_CQES_MIN(n
->id_ctrl
.cqes
))) {
5676 trace_pci_nvme_err_startfail_cqent_too_small(
5678 NVME_CTRL_CQES_MIN(cap
));
5681 if (unlikely(NVME_CC_IOCQES(cc
) >
5682 NVME_CTRL_CQES_MAX(n
->id_ctrl
.cqes
))) {
5683 trace_pci_nvme_err_startfail_cqent_too_large(
5685 NVME_CTRL_CQES_MAX(cap
));
5688 if (unlikely(NVME_CC_IOSQES(cc
) <
5689 NVME_CTRL_SQES_MIN(n
->id_ctrl
.sqes
))) {
5690 trace_pci_nvme_err_startfail_sqent_too_small(
5692 NVME_CTRL_SQES_MIN(cap
));
5695 if (unlikely(NVME_CC_IOSQES(cc
) >
5696 NVME_CTRL_SQES_MAX(n
->id_ctrl
.sqes
))) {
5697 trace_pci_nvme_err_startfail_sqent_too_large(
5699 NVME_CTRL_SQES_MAX(cap
));
5702 if (unlikely(!NVME_AQA_ASQS(aqa
))) {
5703 trace_pci_nvme_err_startfail_asqent_sz_zero();
5706 if (unlikely(!NVME_AQA_ACQS(aqa
))) {
5707 trace_pci_nvme_err_startfail_acqent_sz_zero();
5711 n
->page_bits
= page_bits
;
5712 n
->page_size
= page_size
;
5713 n
->max_prp_ents
= n
->page_size
/ sizeof(uint64_t);
5714 n
->cqe_size
= 1 << NVME_CC_IOCQES(cc
);
5715 n
->sqe_size
= 1 << NVME_CC_IOSQES(cc
);
5716 nvme_init_cq(&n
->admin_cq
, n
, acq
, 0, 0, NVME_AQA_ACQS(aqa
) + 1, 1);
5717 nvme_init_sq(&n
->admin_sq
, n
, asq
, 0, 0, NVME_AQA_ASQS(aqa
) + 1);
5719 nvme_set_timestamp(n
, 0ULL);
5721 QTAILQ_INIT(&n
->aer_queue
);
5723 nvme_select_iocs(n
);
5728 static void nvme_cmb_enable_regs(NvmeCtrl
*n
)
5730 uint32_t cmbloc
= ldl_le_p(&n
->bar
.cmbloc
);
5731 uint32_t cmbsz
= ldl_le_p(&n
->bar
.cmbsz
);
5733 NVME_CMBLOC_SET_CDPCILS(cmbloc
, 1);
5734 NVME_CMBLOC_SET_CDPMLS(cmbloc
, 1);
5735 NVME_CMBLOC_SET_BIR(cmbloc
, NVME_CMB_BIR
);
5736 stl_le_p(&n
->bar
.cmbloc
, cmbloc
);
5738 NVME_CMBSZ_SET_SQS(cmbsz
, 1);
5739 NVME_CMBSZ_SET_CQS(cmbsz
, 0);
5740 NVME_CMBSZ_SET_LISTS(cmbsz
, 1);
5741 NVME_CMBSZ_SET_RDS(cmbsz
, 1);
5742 NVME_CMBSZ_SET_WDS(cmbsz
, 1);
5743 NVME_CMBSZ_SET_SZU(cmbsz
, 2); /* MBs */
5744 NVME_CMBSZ_SET_SZ(cmbsz
, n
->params
.cmb_size_mb
);
5745 stl_le_p(&n
->bar
.cmbsz
, cmbsz
);
5748 static void nvme_write_bar(NvmeCtrl
*n
, hwaddr offset
, uint64_t data
,
5751 uint64_t cap
= ldq_le_p(&n
->bar
.cap
);
5752 uint32_t cc
= ldl_le_p(&n
->bar
.cc
);
5753 uint32_t intms
= ldl_le_p(&n
->bar
.intms
);
5754 uint32_t csts
= ldl_le_p(&n
->bar
.csts
);
5755 uint32_t pmrsts
= ldl_le_p(&n
->bar
.pmrsts
);
5757 if (unlikely(offset
& (sizeof(uint32_t) - 1))) {
5758 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_misaligned32
,
5759 "MMIO write not 32-bit aligned,"
5760 " offset=0x%"PRIx64
"", offset
);
5761 /* should be ignored, fall through for now */
5764 if (unlikely(size
< sizeof(uint32_t))) {
5765 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_toosmall
,
5766 "MMIO write smaller than 32-bits,"
5767 " offset=0x%"PRIx64
", size=%u",
5769 /* should be ignored, fall through for now */
5773 case NVME_REG_INTMS
:
5774 if (unlikely(msix_enabled(&(n
->parent_obj
)))) {
5775 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_intmask_with_msix
,
5776 "undefined access to interrupt mask set"
5777 " when MSI-X is enabled");
5778 /* should be ignored, fall through for now */
5781 stl_le_p(&n
->bar
.intms
, intms
);
5782 n
->bar
.intmc
= n
->bar
.intms
;
5783 trace_pci_nvme_mmio_intm_set(data
& 0xffffffff, intms
);
5786 case NVME_REG_INTMC
:
5787 if (unlikely(msix_enabled(&(n
->parent_obj
)))) {
5788 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_intmask_with_msix
,
5789 "undefined access to interrupt mask clr"
5790 " when MSI-X is enabled");
5791 /* should be ignored, fall through for now */
5794 stl_le_p(&n
->bar
.intms
, intms
);
5795 n
->bar
.intmc
= n
->bar
.intms
;
5796 trace_pci_nvme_mmio_intm_clr(data
& 0xffffffff, intms
);
5800 trace_pci_nvme_mmio_cfg(data
& 0xffffffff);
5802 /* Windows first sends data, then sends enable bit */
5803 if (!NVME_CC_EN(data
) && !NVME_CC_EN(cc
) &&
5804 !NVME_CC_SHN(data
) && !NVME_CC_SHN(cc
))
5809 if (NVME_CC_EN(data
) && !NVME_CC_EN(cc
)) {
5812 /* flush CC since nvme_start_ctrl() needs the value */
5813 stl_le_p(&n
->bar
.cc
, cc
);
5814 if (unlikely(nvme_start_ctrl(n
))) {
5815 trace_pci_nvme_err_startfail();
5816 csts
= NVME_CSTS_FAILED
;
5818 trace_pci_nvme_mmio_start_success();
5819 csts
= NVME_CSTS_READY
;
5821 } else if (!NVME_CC_EN(data
) && NVME_CC_EN(cc
)) {
5822 trace_pci_nvme_mmio_stopped();
5825 csts
&= ~NVME_CSTS_READY
;
5828 if (NVME_CC_SHN(data
) && !(NVME_CC_SHN(cc
))) {
5829 trace_pci_nvme_mmio_shutdown_set();
5830 nvme_ctrl_shutdown(n
);
5832 csts
|= NVME_CSTS_SHST_COMPLETE
;
5833 } else if (!NVME_CC_SHN(data
) && NVME_CC_SHN(cc
)) {
5834 trace_pci_nvme_mmio_shutdown_cleared();
5835 csts
&= ~NVME_CSTS_SHST_COMPLETE
;
5839 stl_le_p(&n
->bar
.cc
, cc
);
5840 stl_le_p(&n
->bar
.csts
, csts
);
5844 if (data
& (1 << 4)) {
5845 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_ssreset_w1c_unsupported
,
5846 "attempted to W1C CSTS.NSSRO"
5847 " but CAP.NSSRS is zero (not supported)");
5848 } else if (data
!= 0) {
5849 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_ro_csts
,
5850 "attempted to set a read only bit"
5851 " of controller status");
5855 if (data
== 0x4e564d65) {
5856 trace_pci_nvme_ub_mmiowr_ssreset_unsupported();
5858 /* The spec says that writes of other values have no effect */
5863 stl_le_p(&n
->bar
.aqa
, data
);
5864 trace_pci_nvme_mmio_aqattr(data
& 0xffffffff);
5867 stn_le_p(&n
->bar
.asq
, size
, data
);
5868 trace_pci_nvme_mmio_asqaddr(data
);
5870 case NVME_REG_ASQ
+ 4:
5871 stl_le_p((uint8_t *)&n
->bar
.asq
+ 4, data
);
5872 trace_pci_nvme_mmio_asqaddr_hi(data
, ldq_le_p(&n
->bar
.asq
));
5875 trace_pci_nvme_mmio_acqaddr(data
);
5876 stn_le_p(&n
->bar
.acq
, size
, data
);
5878 case NVME_REG_ACQ
+ 4:
5879 stl_le_p((uint8_t *)&n
->bar
.acq
+ 4, data
);
5880 trace_pci_nvme_mmio_acqaddr_hi(data
, ldq_le_p(&n
->bar
.acq
));
5882 case NVME_REG_CMBLOC
:
5883 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_cmbloc_reserved
,
5884 "invalid write to reserved CMBLOC"
5885 " when CMBSZ is zero, ignored");
5887 case NVME_REG_CMBSZ
:
5888 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_cmbsz_readonly
,
5889 "invalid write to read only CMBSZ, ignored");
5891 case NVME_REG_CMBMSC
:
5892 if (!NVME_CAP_CMBS(cap
)) {
5896 stn_le_p(&n
->bar
.cmbmsc
, size
, data
);
5897 n
->cmb
.cmse
= false;
5899 if (NVME_CMBMSC_CRE(data
)) {
5900 nvme_cmb_enable_regs(n
);
5902 if (NVME_CMBMSC_CMSE(data
)) {
5903 uint64_t cmbmsc
= ldq_le_p(&n
->bar
.cmbmsc
);
5904 hwaddr cba
= NVME_CMBMSC_CBA(cmbmsc
) << CMBMSC_CBA_SHIFT
;
5905 if (cba
+ int128_get64(n
->cmb
.mem
.size
) < cba
) {
5906 uint32_t cmbsts
= ldl_le_p(&n
->bar
.cmbsts
);
5907 NVME_CMBSTS_SET_CBAI(cmbsts
, 1);
5908 stl_le_p(&n
->bar
.cmbsts
, cmbsts
);
5921 case NVME_REG_CMBMSC
+ 4:
5922 stl_le_p((uint8_t *)&n
->bar
.cmbmsc
+ 4, data
);
5925 case NVME_REG_PMRCAP
:
5926 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrcap_readonly
,
5927 "invalid write to PMRCAP register, ignored");
5929 case NVME_REG_PMRCTL
:
5930 if (!NVME_CAP_PMRS(cap
)) {
5934 stl_le_p(&n
->bar
.pmrctl
, data
);
5935 if (NVME_PMRCTL_EN(data
)) {
5936 memory_region_set_enabled(&n
->pmr
.dev
->mr
, true);
5939 memory_region_set_enabled(&n
->pmr
.dev
->mr
, false);
5940 NVME_PMRSTS_SET_NRDY(pmrsts
, 1);
5941 n
->pmr
.cmse
= false;
5943 stl_le_p(&n
->bar
.pmrsts
, pmrsts
);
5945 case NVME_REG_PMRSTS
:
5946 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrsts_readonly
,
5947 "invalid write to PMRSTS register, ignored");
5949 case NVME_REG_PMREBS
:
5950 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrebs_readonly
,
5951 "invalid write to PMREBS register, ignored");
5953 case NVME_REG_PMRSWTP
:
5954 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrswtp_readonly
,
5955 "invalid write to PMRSWTP register, ignored");
5957 case NVME_REG_PMRMSCL
:
5958 if (!NVME_CAP_PMRS(cap
)) {
5962 stl_le_p(&n
->bar
.pmrmscl
, data
);
5963 n
->pmr
.cmse
= false;
5965 if (NVME_PMRMSCL_CMSE(data
)) {
5966 uint64_t pmrmscu
= ldl_le_p(&n
->bar
.pmrmscu
);
5967 hwaddr cba
= pmrmscu
<< 32 |
5968 (NVME_PMRMSCL_CBA(data
) << PMRMSCL_CBA_SHIFT
);
5969 if (cba
+ int128_get64(n
->pmr
.dev
->mr
.size
) < cba
) {
5970 NVME_PMRSTS_SET_CBAI(pmrsts
, 1);
5971 stl_le_p(&n
->bar
.pmrsts
, pmrsts
);
5980 case NVME_REG_PMRMSCU
:
5981 if (!NVME_CAP_PMRS(cap
)) {
5985 stl_le_p(&n
->bar
.pmrmscu
, data
);
5988 NVME_GUEST_ERR(pci_nvme_ub_mmiowr_invalid
,
5989 "invalid MMIO write,"
5990 " offset=0x%"PRIx64
", data=%"PRIx64
"",
5996 static uint64_t nvme_mmio_read(void *opaque
, hwaddr addr
, unsigned size
)
5998 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
5999 uint8_t *ptr
= (uint8_t *)&n
->bar
;
6001 trace_pci_nvme_mmio_read(addr
, size
);
6003 if (unlikely(addr
& (sizeof(uint32_t) - 1))) {
6004 NVME_GUEST_ERR(pci_nvme_ub_mmiord_misaligned32
,
6005 "MMIO read not 32-bit aligned,"
6006 " offset=0x%"PRIx64
"", addr
);
6007 /* should RAZ, fall through for now */
6008 } else if (unlikely(size
< sizeof(uint32_t))) {
6009 NVME_GUEST_ERR(pci_nvme_ub_mmiord_toosmall
,
6010 "MMIO read smaller than 32-bits,"
6011 " offset=0x%"PRIx64
"", addr
);
6012 /* should RAZ, fall through for now */
6015 if (addr
> sizeof(n
->bar
) - size
) {
6016 NVME_GUEST_ERR(pci_nvme_ub_mmiord_invalid_ofs
,
6017 "MMIO read beyond last register,"
6018 " offset=0x%"PRIx64
", returning 0", addr
);
6024 * When PMRWBM bit 1 is set then read from
6025 * from PMRSTS should ensure prior writes
6026 * made it to persistent media
6028 if (addr
== NVME_REG_PMRSTS
&&
6029 (NVME_PMRCAP_PMRWBM(ldl_le_p(&n
->bar
.pmrcap
)) & 0x02)) {
6030 memory_region_msync(&n
->pmr
.dev
->mr
, 0, n
->pmr
.dev
->size
);
6033 return ldn_le_p(ptr
+ addr
, size
);
6036 static void nvme_process_db(NvmeCtrl
*n
, hwaddr addr
, int val
)
6040 if (unlikely(addr
& ((1 << 2) - 1))) {
6041 NVME_GUEST_ERR(pci_nvme_ub_db_wr_misaligned
,
6042 "doorbell write not 32-bit aligned,"
6043 " offset=0x%"PRIx64
", ignoring", addr
);
6047 if (((addr
- 0x1000) >> 2) & 1) {
6048 /* Completion queue doorbell write */
6050 uint16_t new_head
= val
& 0xffff;
6054 qid
= (addr
- (0x1000 + (1 << 2))) >> 3;
6055 if (unlikely(nvme_check_cqid(n
, qid
))) {
6056 NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_cq
,
6057 "completion queue doorbell write"
6058 " for nonexistent queue,"
6059 " sqid=%"PRIu32
", ignoring", qid
);
6062 * NVM Express v1.3d, Section 4.1 state: "If host software writes
6063 * an invalid value to the Submission Queue Tail Doorbell or
6064 * Completion Queue Head Doorbell regiter and an Asynchronous Event
6065 * Request command is outstanding, then an asynchronous event is
6066 * posted to the Admin Completion Queue with a status code of
6067 * Invalid Doorbell Write Value."
6069 * Also note that the spec includes the "Invalid Doorbell Register"
6070 * status code, but nowhere does it specify when to use it.
6071 * However, it seems reasonable to use it here in a similar
6074 if (n
->outstanding_aers
) {
6075 nvme_enqueue_event(n
, NVME_AER_TYPE_ERROR
,
6076 NVME_AER_INFO_ERR_INVALID_DB_REGISTER
,
6077 NVME_LOG_ERROR_INFO
);
6084 if (unlikely(new_head
>= cq
->size
)) {
6085 NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_cqhead
,
6086 "completion queue doorbell write value"
6087 " beyond queue size, sqid=%"PRIu32
","
6088 " new_head=%"PRIu16
", ignoring",
6091 if (n
->outstanding_aers
) {
6092 nvme_enqueue_event(n
, NVME_AER_TYPE_ERROR
,
6093 NVME_AER_INFO_ERR_INVALID_DB_VALUE
,
6094 NVME_LOG_ERROR_INFO
);
6100 trace_pci_nvme_mmio_doorbell_cq(cq
->cqid
, new_head
);
6102 start_sqs
= nvme_cq_full(cq
) ? 1 : 0;
6103 cq
->head
= new_head
;
6106 QTAILQ_FOREACH(sq
, &cq
->sq_list
, entry
) {
6107 timer_mod(sq
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 500);
6109 timer_mod(cq
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 500);
6112 if (cq
->tail
== cq
->head
) {
6113 if (cq
->irq_enabled
) {
6117 nvme_irq_deassert(n
, cq
);
6120 /* Submission queue doorbell write */
6122 uint16_t new_tail
= val
& 0xffff;
6125 qid
= (addr
- 0x1000) >> 3;
6126 if (unlikely(nvme_check_sqid(n
, qid
))) {
6127 NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_sq
,
6128 "submission queue doorbell write"
6129 " for nonexistent queue,"
6130 " sqid=%"PRIu32
", ignoring", qid
);
6132 if (n
->outstanding_aers
) {
6133 nvme_enqueue_event(n
, NVME_AER_TYPE_ERROR
,
6134 NVME_AER_INFO_ERR_INVALID_DB_REGISTER
,
6135 NVME_LOG_ERROR_INFO
);
6142 if (unlikely(new_tail
>= sq
->size
)) {
6143 NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_sqtail
,
6144 "submission queue doorbell write value"
6145 " beyond queue size, sqid=%"PRIu32
","
6146 " new_tail=%"PRIu16
", ignoring",
6149 if (n
->outstanding_aers
) {
6150 nvme_enqueue_event(n
, NVME_AER_TYPE_ERROR
,
6151 NVME_AER_INFO_ERR_INVALID_DB_VALUE
,
6152 NVME_LOG_ERROR_INFO
);
6158 trace_pci_nvme_mmio_doorbell_sq(sq
->sqid
, new_tail
);
6160 sq
->tail
= new_tail
;
6161 timer_mod(sq
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 500);
6165 static void nvme_mmio_write(void *opaque
, hwaddr addr
, uint64_t data
,
6168 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
6170 trace_pci_nvme_mmio_write(addr
, data
, size
);
6172 if (addr
< sizeof(n
->bar
)) {
6173 nvme_write_bar(n
, addr
, data
, size
);
6175 nvme_process_db(n
, addr
, data
);
6179 static const MemoryRegionOps nvme_mmio_ops
= {
6180 .read
= nvme_mmio_read
,
6181 .write
= nvme_mmio_write
,
6182 .endianness
= DEVICE_LITTLE_ENDIAN
,
6184 .min_access_size
= 2,
6185 .max_access_size
= 8,
6189 static void nvme_cmb_write(void *opaque
, hwaddr addr
, uint64_t data
,
6192 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
6193 stn_le_p(&n
->cmb
.buf
[addr
], size
, data
);
6196 static uint64_t nvme_cmb_read(void *opaque
, hwaddr addr
, unsigned size
)
6198 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
6199 return ldn_le_p(&n
->cmb
.buf
[addr
], size
);
6202 static const MemoryRegionOps nvme_cmb_ops
= {
6203 .read
= nvme_cmb_read
,
6204 .write
= nvme_cmb_write
,
6205 .endianness
= DEVICE_LITTLE_ENDIAN
,
6207 .min_access_size
= 1,
6208 .max_access_size
= 8,
6212 static void nvme_check_constraints(NvmeCtrl
*n
, Error
**errp
)
6214 NvmeParams
*params
= &n
->params
;
6216 if (params
->num_queues
) {
6217 warn_report("num_queues is deprecated; please use max_ioqpairs "
6220 params
->max_ioqpairs
= params
->num_queues
- 1;
6223 if (n
->namespace.blkconf
.blk
&& n
->subsys
) {
6224 error_setg(errp
, "subsystem support is unavailable with legacy "
6225 "namespace ('drive' property)");
6229 if (params
->max_ioqpairs
< 1 ||
6230 params
->max_ioqpairs
> NVME_MAX_IOQPAIRS
) {
6231 error_setg(errp
, "max_ioqpairs must be between 1 and %d",
6236 if (params
->msix_qsize
< 1 ||
6237 params
->msix_qsize
> PCI_MSIX_FLAGS_QSIZE
+ 1) {
6238 error_setg(errp
, "msix_qsize must be between 1 and %d",
6239 PCI_MSIX_FLAGS_QSIZE
+ 1);
6243 if (!params
->serial
) {
6244 error_setg(errp
, "serial property not set");
6249 if (host_memory_backend_is_mapped(n
->pmr
.dev
)) {
6250 error_setg(errp
, "can't use already busy memdev: %s",
6251 object_get_canonical_path_component(OBJECT(n
->pmr
.dev
)));
6255 if (!is_power_of_2(n
->pmr
.dev
->size
)) {
6256 error_setg(errp
, "pmr backend size needs to be power of 2 in size");
6260 host_memory_backend_set_mapped(n
->pmr
.dev
, true);
6263 if (n
->params
.zasl
> n
->params
.mdts
) {
6264 error_setg(errp
, "zoned.zasl (Zone Append Size Limit) must be less "
6265 "than or equal to mdts (Maximum Data Transfer Size)");
6269 if (!n
->params
.vsl
) {
6270 error_setg(errp
, "vsl must be non-zero");
6275 static void nvme_init_state(NvmeCtrl
*n
)
6277 /* add one to max_ioqpairs to account for the admin queue pair */
6278 n
->reg_size
= pow2ceil(sizeof(NvmeBar
) +
6279 2 * (n
->params
.max_ioqpairs
+ 1) * NVME_DB_SIZE
);
6280 n
->sq
= g_new0(NvmeSQueue
*, n
->params
.max_ioqpairs
+ 1);
6281 n
->cq
= g_new0(NvmeCQueue
*, n
->params
.max_ioqpairs
+ 1);
6282 n
->temperature
= NVME_TEMPERATURE
;
6283 n
->features
.temp_thresh_hi
= NVME_TEMPERATURE_WARNING
;
6284 n
->starttime_ms
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
);
6285 n
->aer_reqs
= g_new0(NvmeRequest
*, n
->params
.aerl
+ 1);
6288 static void nvme_init_cmb(NvmeCtrl
*n
, PCIDevice
*pci_dev
)
6290 uint64_t cmb_size
= n
->params
.cmb_size_mb
* MiB
;
6291 uint64_t cap
= ldq_le_p(&n
->bar
.cap
);
6293 n
->cmb
.buf
= g_malloc0(cmb_size
);
6294 memory_region_init_io(&n
->cmb
.mem
, OBJECT(n
), &nvme_cmb_ops
, n
,
6295 "nvme-cmb", cmb_size
);
6296 pci_register_bar(pci_dev
, NVME_CMB_BIR
,
6297 PCI_BASE_ADDRESS_SPACE_MEMORY
|
6298 PCI_BASE_ADDRESS_MEM_TYPE_64
|
6299 PCI_BASE_ADDRESS_MEM_PREFETCH
, &n
->cmb
.mem
);
6301 NVME_CAP_SET_CMBS(cap
, 1);
6302 stq_le_p(&n
->bar
.cap
, cap
);
6304 if (n
->params
.legacy_cmb
) {
6305 nvme_cmb_enable_regs(n
);
6310 static void nvme_init_pmr(NvmeCtrl
*n
, PCIDevice
*pci_dev
)
6312 uint32_t pmrcap
= ldl_le_p(&n
->bar
.pmrcap
);
6314 NVME_PMRCAP_SET_RDS(pmrcap
, 1);
6315 NVME_PMRCAP_SET_WDS(pmrcap
, 1);
6316 NVME_PMRCAP_SET_BIR(pmrcap
, NVME_PMR_BIR
);
6317 /* Turn on bit 1 support */
6318 NVME_PMRCAP_SET_PMRWBM(pmrcap
, 0x02);
6319 NVME_PMRCAP_SET_CMSS(pmrcap
, 1);
6320 stl_le_p(&n
->bar
.pmrcap
, pmrcap
);
6322 pci_register_bar(pci_dev
, NVME_PMR_BIR
,
6323 PCI_BASE_ADDRESS_SPACE_MEMORY
|
6324 PCI_BASE_ADDRESS_MEM_TYPE_64
|
6325 PCI_BASE_ADDRESS_MEM_PREFETCH
, &n
->pmr
.dev
->mr
);
6327 memory_region_set_enabled(&n
->pmr
.dev
->mr
, false);
6330 static int nvme_init_pci(NvmeCtrl
*n
, PCIDevice
*pci_dev
, Error
**errp
)
6332 uint8_t *pci_conf
= pci_dev
->config
;
6333 uint64_t bar_size
, msix_table_size
, msix_pba_size
;
6334 unsigned msix_table_offset
, msix_pba_offset
;
6339 pci_conf
[PCI_INTERRUPT_PIN
] = 1;
6340 pci_config_set_prog_interface(pci_conf
, 0x2);
6342 if (n
->params
.use_intel_id
) {
6343 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_INTEL
);
6344 pci_config_set_device_id(pci_conf
, 0x5845);
6346 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_REDHAT
);
6347 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_REDHAT_NVME
);
6350 pci_config_set_class(pci_conf
, PCI_CLASS_STORAGE_EXPRESS
);
6351 pcie_endpoint_cap_init(pci_dev
, 0x80);
6353 bar_size
= QEMU_ALIGN_UP(n
->reg_size
, 4 * KiB
);
6354 msix_table_offset
= bar_size
;
6355 msix_table_size
= PCI_MSIX_ENTRY_SIZE
* n
->params
.msix_qsize
;
6357 bar_size
+= msix_table_size
;
6358 bar_size
= QEMU_ALIGN_UP(bar_size
, 4 * KiB
);
6359 msix_pba_offset
= bar_size
;
6360 msix_pba_size
= QEMU_ALIGN_UP(n
->params
.msix_qsize
, 64) / 8;
6362 bar_size
+= msix_pba_size
;
6363 bar_size
= pow2ceil(bar_size
);
6365 memory_region_init(&n
->bar0
, OBJECT(n
), "nvme-bar0", bar_size
);
6366 memory_region_init_io(&n
->iomem
, OBJECT(n
), &nvme_mmio_ops
, n
, "nvme",
6368 memory_region_add_subregion(&n
->bar0
, 0, &n
->iomem
);
6370 pci_register_bar(pci_dev
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
|
6371 PCI_BASE_ADDRESS_MEM_TYPE_64
, &n
->bar0
);
6372 ret
= msix_init(pci_dev
, n
->params
.msix_qsize
,
6373 &n
->bar0
, 0, msix_table_offset
,
6374 &n
->bar0
, 0, msix_pba_offset
, 0, &err
);
6376 if (ret
== -ENOTSUP
) {
6377 warn_report_err(err
);
6379 error_propagate(errp
, err
);
6384 if (n
->params
.cmb_size_mb
) {
6385 nvme_init_cmb(n
, pci_dev
);
6389 nvme_init_pmr(n
, pci_dev
);
6395 static void nvme_init_subnqn(NvmeCtrl
*n
)
6397 NvmeSubsystem
*subsys
= n
->subsys
;
6398 NvmeIdCtrl
*id
= &n
->id_ctrl
;
6401 snprintf((char *)id
->subnqn
, sizeof(id
->subnqn
),
6402 "nqn.2019-08.org.qemu:%s", n
->params
.serial
);
6404 pstrcpy((char *)id
->subnqn
, sizeof(id
->subnqn
), (char*)subsys
->subnqn
);
6408 static void nvme_init_ctrl(NvmeCtrl
*n
, PCIDevice
*pci_dev
)
6410 NvmeIdCtrl
*id
= &n
->id_ctrl
;
6411 uint8_t *pci_conf
= pci_dev
->config
;
6412 uint64_t cap
= ldq_le_p(&n
->bar
.cap
);
6414 id
->vid
= cpu_to_le16(pci_get_word(pci_conf
+ PCI_VENDOR_ID
));
6415 id
->ssvid
= cpu_to_le16(pci_get_word(pci_conf
+ PCI_SUBSYSTEM_VENDOR_ID
));
6416 strpadcpy((char *)id
->mn
, sizeof(id
->mn
), "QEMU NVMe Ctrl", ' ');
6417 strpadcpy((char *)id
->fr
, sizeof(id
->fr
), "1.0", ' ');
6418 strpadcpy((char *)id
->sn
, sizeof(id
->sn
), n
->params
.serial
, ' ');
6420 id
->cntlid
= cpu_to_le16(n
->cntlid
);
6422 id
->oaes
= cpu_to_le32(NVME_OAES_NS_ATTR
);
6426 if (n
->params
.use_intel_id
) {
6436 id
->mdts
= n
->params
.mdts
;
6437 id
->ver
= cpu_to_le32(NVME_SPEC_VER
);
6438 id
->oacs
= cpu_to_le16(NVME_OACS_NS_MGMT
| NVME_OACS_FORMAT
);
6439 id
->cntrltype
= 0x1;
6442 * Because the controller always completes the Abort command immediately,
6443 * there can never be more than one concurrently executing Abort command,
6444 * so this value is never used for anything. Note that there can easily be
6445 * many Abort commands in the queues, but they are not considered
6446 * "executing" until processed by nvme_abort.
6448 * The specification recommends a value of 3 for Abort Command Limit (four
6449 * concurrently outstanding Abort commands), so lets use that though it is
6453 id
->aerl
= n
->params
.aerl
;
6454 id
->frmw
= (NVME_NUM_FW_SLOTS
<< 1) | NVME_FRMW_SLOT1_RO
;
6455 id
->lpa
= NVME_LPA_NS_SMART
| NVME_LPA_CSE
| NVME_LPA_EXTENDED
;
6457 /* recommended default value (~70 C) */
6458 id
->wctemp
= cpu_to_le16(NVME_TEMPERATURE_WARNING
);
6459 id
->cctemp
= cpu_to_le16(NVME_TEMPERATURE_CRITICAL
);
6461 id
->sqes
= (0x6 << 4) | 0x6;
6462 id
->cqes
= (0x4 << 4) | 0x4;
6463 id
->nn
= cpu_to_le32(NVME_MAX_NAMESPACES
);
6464 id
->oncs
= cpu_to_le16(NVME_ONCS_WRITE_ZEROES
| NVME_ONCS_TIMESTAMP
|
6465 NVME_ONCS_FEATURES
| NVME_ONCS_DSM
|
6466 NVME_ONCS_COMPARE
| NVME_ONCS_COPY
);
6469 * NOTE: If this device ever supports a command set that does NOT use 0x0
6470 * as a Flush-equivalent operation, support for the broadcast NSID in Flush
6471 * should probably be removed.
6473 * See comment in nvme_io_cmd.
6475 id
->vwc
= NVME_VWC_NSID_BROADCAST_SUPPORT
| NVME_VWC_PRESENT
;
6477 id
->ocfs
= cpu_to_le16(NVME_OCFS_COPY_FORMAT_0
);
6478 id
->sgls
= cpu_to_le32(NVME_CTRL_SGLS_SUPPORT_NO_ALIGN
|
6479 NVME_CTRL_SGLS_BITBUCKET
);
6481 nvme_init_subnqn(n
);
6483 id
->psd
[0].mp
= cpu_to_le16(0x9c4);
6484 id
->psd
[0].enlat
= cpu_to_le32(0x10);
6485 id
->psd
[0].exlat
= cpu_to_le32(0x4);
6488 id
->cmic
|= NVME_CMIC_MULTI_CTRL
;
6491 NVME_CAP_SET_MQES(cap
, 0x7ff);
6492 NVME_CAP_SET_CQR(cap
, 1);
6493 NVME_CAP_SET_TO(cap
, 0xf);
6494 NVME_CAP_SET_CSS(cap
, NVME_CAP_CSS_NVM
);
6495 NVME_CAP_SET_CSS(cap
, NVME_CAP_CSS_CSI_SUPP
);
6496 NVME_CAP_SET_CSS(cap
, NVME_CAP_CSS_ADMIN_ONLY
);
6497 NVME_CAP_SET_MPSMAX(cap
, 4);
6498 NVME_CAP_SET_CMBS(cap
, n
->params
.cmb_size_mb
? 1 : 0);
6499 NVME_CAP_SET_PMRS(cap
, n
->pmr
.dev
? 1 : 0);
6500 stq_le_p(&n
->bar
.cap
, cap
);
6502 stl_le_p(&n
->bar
.vs
, NVME_SPEC_VER
);
6503 n
->bar
.intmc
= n
->bar
.intms
= 0;
6506 static int nvme_init_subsys(NvmeCtrl
*n
, Error
**errp
)
6514 cntlid
= nvme_subsys_register_ctrl(n
, errp
);
6524 void nvme_attach_ns(NvmeCtrl
*n
, NvmeNamespace
*ns
)
6526 uint32_t nsid
= ns
->params
.nsid
;
6527 assert(nsid
&& nsid
<= NVME_MAX_NAMESPACES
);
6529 n
->namespaces
[nsid
] = ns
;
6532 n
->dmrsl
= MIN_NON_ZERO(n
->dmrsl
,
6533 BDRV_REQUEST_MAX_BYTES
/ nvme_l2b(ns
, 1));
6536 static void nvme_realize(PCIDevice
*pci_dev
, Error
**errp
)
6538 NvmeCtrl
*n
= NVME(pci_dev
);
6540 Error
*local_err
= NULL
;
6542 nvme_check_constraints(n
, &local_err
);
6544 error_propagate(errp
, local_err
);
6548 qbus_init(&n
->bus
, sizeof(NvmeBus
), TYPE_NVME_BUS
,
6549 &pci_dev
->qdev
, n
->parent_obj
.qdev
.id
);
6552 if (nvme_init_pci(n
, pci_dev
, errp
)) {
6556 if (nvme_init_subsys(n
, errp
)) {
6557 error_propagate(errp
, local_err
);
6560 nvme_init_ctrl(n
, pci_dev
);
6562 /* setup a namespace if the controller drive property was given */
6563 if (n
->namespace.blkconf
.blk
) {
6565 ns
->params
.nsid
= 1;
6567 if (nvme_ns_setup(ns
, errp
)) {
6571 nvme_attach_ns(n
, ns
);
6575 static void nvme_exit(PCIDevice
*pci_dev
)
6577 NvmeCtrl
*n
= NVME(pci_dev
);
6584 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
6591 nvme_subsys_unregister_ctrl(n
->subsys
, n
);
6596 g_free(n
->aer_reqs
);
6598 if (n
->params
.cmb_size_mb
) {
6603 host_memory_backend_set_mapped(n
->pmr
.dev
, false);
6605 msix_uninit(pci_dev
, &n
->bar0
, &n
->bar0
);
6606 memory_region_del_subregion(&n
->bar0
, &n
->iomem
);
6609 static Property nvme_props
[] = {
6610 DEFINE_BLOCK_PROPERTIES(NvmeCtrl
, namespace.blkconf
),
6611 DEFINE_PROP_LINK("pmrdev", NvmeCtrl
, pmr
.dev
, TYPE_MEMORY_BACKEND
,
6612 HostMemoryBackend
*),
6613 DEFINE_PROP_LINK("subsys", NvmeCtrl
, subsys
, TYPE_NVME_SUBSYS
,
6615 DEFINE_PROP_STRING("serial", NvmeCtrl
, params
.serial
),
6616 DEFINE_PROP_UINT32("cmb_size_mb", NvmeCtrl
, params
.cmb_size_mb
, 0),
6617 DEFINE_PROP_UINT32("num_queues", NvmeCtrl
, params
.num_queues
, 0),
6618 DEFINE_PROP_UINT32("max_ioqpairs", NvmeCtrl
, params
.max_ioqpairs
, 64),
6619 DEFINE_PROP_UINT16("msix_qsize", NvmeCtrl
, params
.msix_qsize
, 65),
6620 DEFINE_PROP_UINT8("aerl", NvmeCtrl
, params
.aerl
, 3),
6621 DEFINE_PROP_UINT32("aer_max_queued", NvmeCtrl
, params
.aer_max_queued
, 64),
6622 DEFINE_PROP_UINT8("mdts", NvmeCtrl
, params
.mdts
, 7),
6623 DEFINE_PROP_UINT8("vsl", NvmeCtrl
, params
.vsl
, 7),
6624 DEFINE_PROP_BOOL("use-intel-id", NvmeCtrl
, params
.use_intel_id
, false),
6625 DEFINE_PROP_BOOL("legacy-cmb", NvmeCtrl
, params
.legacy_cmb
, false),
6626 DEFINE_PROP_UINT8("zoned.zasl", NvmeCtrl
, params
.zasl
, 0),
6627 DEFINE_PROP_BOOL("zoned.auto_transition", NvmeCtrl
,
6628 params
.auto_transition_zones
, true),
6629 DEFINE_PROP_END_OF_LIST(),
6632 static void nvme_get_smart_warning(Object
*obj
, Visitor
*v
, const char *name
,
6633 void *opaque
, Error
**errp
)
6635 NvmeCtrl
*n
= NVME(obj
);
6636 uint8_t value
= n
->smart_critical_warning
;
6638 visit_type_uint8(v
, name
, &value
, errp
);
6641 static void nvme_set_smart_warning(Object
*obj
, Visitor
*v
, const char *name
,
6642 void *opaque
, Error
**errp
)
6644 NvmeCtrl
*n
= NVME(obj
);
6645 uint8_t value
, old_value
, cap
= 0, index
, event
;
6647 if (!visit_type_uint8(v
, name
, &value
, errp
)) {
6651 cap
= NVME_SMART_SPARE
| NVME_SMART_TEMPERATURE
| NVME_SMART_RELIABILITY
6652 | NVME_SMART_MEDIA_READ_ONLY
| NVME_SMART_FAILED_VOLATILE_MEDIA
;
6653 if (NVME_CAP_PMRS(ldq_le_p(&n
->bar
.cap
))) {
6654 cap
|= NVME_SMART_PMR_UNRELIABLE
;
6657 if ((value
& cap
) != value
) {
6658 error_setg(errp
, "unsupported smart critical warning bits: 0x%x",
6663 old_value
= n
->smart_critical_warning
;
6664 n
->smart_critical_warning
= value
;
6666 /* only inject new bits of smart critical warning */
6667 for (index
= 0; index
< NVME_SMART_WARN_MAX
; index
++) {
6669 if (value
& ~old_value
& event
)
6670 nvme_smart_event(n
, event
);
6674 static const VMStateDescription nvme_vmstate
= {
6679 static void nvme_class_init(ObjectClass
*oc
, void *data
)
6681 DeviceClass
*dc
= DEVICE_CLASS(oc
);
6682 PCIDeviceClass
*pc
= PCI_DEVICE_CLASS(oc
);
6684 pc
->realize
= nvme_realize
;
6685 pc
->exit
= nvme_exit
;
6686 pc
->class_id
= PCI_CLASS_STORAGE_EXPRESS
;
6689 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
6690 dc
->desc
= "Non-Volatile Memory Express";
6691 device_class_set_props(dc
, nvme_props
);
6692 dc
->vmsd
= &nvme_vmstate
;
6695 static void nvme_instance_init(Object
*obj
)
6697 NvmeCtrl
*n
= NVME(obj
);
6699 device_add_bootindex_property(obj
, &n
->namespace.blkconf
.bootindex
,
6700 "bootindex", "/namespace@1,0",
6703 object_property_add(obj
, "smart_critical_warning", "uint8",
6704 nvme_get_smart_warning
,
6705 nvme_set_smart_warning
, NULL
, NULL
);
6708 static const TypeInfo nvme_info
= {
6710 .parent
= TYPE_PCI_DEVICE
,
6711 .instance_size
= sizeof(NvmeCtrl
),
6712 .instance_init
= nvme_instance_init
,
6713 .class_init
= nvme_class_init
,
6714 .interfaces
= (InterfaceInfo
[]) {
6715 { INTERFACE_PCIE_DEVICE
},
6720 static const TypeInfo nvme_bus_info
= {
6721 .name
= TYPE_NVME_BUS
,
6723 .instance_size
= sizeof(NvmeBus
),
6726 static void nvme_register_types(void)
6728 type_register_static(&nvme_info
);
6729 type_register_static(&nvme_bus_info
);
6732 type_init(nvme_register_types
)