| /* |
| * QEMU NVM Express Controller |
| * |
| * Copyright (c) 2012, Intel Corporation |
| * |
| * Written by Keith Busch <keith.busch@intel.com> |
| * |
| * This code is licensed under the GNU GPL v2 or later. |
| */ |
| |
| /** |
| * Reference Specs: http://www.nvmexpress.org, 1.4, 1.3, 1.2, 1.1, 1.0e |
| * |
| * https://nvmexpress.org/developers/nvme-specification/ |
| * |
| * |
| * Notes on coding style |
| * --------------------- |
| * While QEMU coding style prefers lowercase hexadecimals in constants, the |
| * NVMe subsystem use thes format from the NVMe specifications in the comments |
| * (i.e. 'h' suffix instead of '0x' prefix). |
| * |
| * Usage |
| * ----- |
| * See docs/system/nvme.rst for extensive documentation. |
| * |
| * Add options: |
| * -drive file=<file>,if=none,id=<drive_id> |
| * -device nvme-subsys,id=<subsys_id>,nqn=<nqn_id> |
| * -device nvme,serial=<serial>,id=<bus_name>, \ |
| * cmb_size_mb=<cmb_size_mb[optional]>, \ |
| * [pmrdev=<mem_backend_file_id>,] \ |
| * max_ioqpairs=<N[optional]>, \ |
| * aerl=<N[optional]>,aer_max_queued=<N[optional]>, \ |
| * mdts=<N[optional]>,vsl=<N[optional]>, \ |
| * zoned.zasl=<N[optional]>, \ |
| * zoned.auto_transition=<on|off[optional]>, \ |
| * sriov_max_vfs=<N[optional]> \ |
| * sriov_vq_flexible=<N[optional]> \ |
| * sriov_vi_flexible=<N[optional]> \ |
| * sriov_max_vi_per_vf=<N[optional]> \ |
| * sriov_max_vq_per_vf=<N[optional]> \ |
| * subsys=<subsys_id> |
| * -device nvme-ns,drive=<drive_id>,bus=<bus_name>,nsid=<nsid>,\ |
| * zoned=<true|false[optional]>, \ |
| * subsys=<subsys_id>,detached=<true|false[optional]> |
| * |
| * Note cmb_size_mb denotes size of CMB in MB. CMB is assumed to be at |
| * offset 0 in BAR2 and supports only WDS, RDS and SQS for now. By default, the |
| * device will use the "v1.4 CMB scheme" - use the `legacy-cmb` parameter to |
| * always enable the CMBLOC and CMBSZ registers (v1.3 behavior). |
| * |
| * Enabling pmr emulation can be achieved by pointing to memory-backend-file. |
| * For example: |
| * -object memory-backend-file,id=<mem_id>,share=on,mem-path=<file_path>, \ |
| * size=<size> .... -device nvme,...,pmrdev=<mem_id> |
| * |
| * The PMR will use BAR 4/5 exclusively. |
| * |
| * To place controller(s) and namespace(s) to a subsystem, then provide |
| * nvme-subsys device as above. |
| * |
| * nvme subsystem device parameters |
| * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| * - `nqn` |
| * This parameter provides the `<nqn_id>` part of the string |
| * `nqn.2019-08.org.qemu:<nqn_id>` which will be reported in the SUBNQN field |
| * of subsystem controllers. Note that `<nqn_id>` should be unique per |
| * subsystem, but this is not enforced by QEMU. If not specified, it will |
| * default to the value of the `id` parameter (`<subsys_id>`). |
| * |
| * nvme device parameters |
| * ~~~~~~~~~~~~~~~~~~~~~~ |
| * - `subsys` |
| * Specifying this parameter attaches the controller to the subsystem and |
| * the SUBNQN field in the controller will report the NQN of the subsystem |
| * device. This also enables multi controller capability represented in |
| * Identify Controller data structure in CMIC (Controller Multi-path I/O and |
| * Namespace Sharing Capabilities). |
| * |
| * - `aerl` |
| * The Asynchronous Event Request Limit (AERL). Indicates the maximum number |
| * of concurrently outstanding Asynchronous Event Request commands support |
| * by the controller. This is a 0's based value. |
| * |
| * - `aer_max_queued` |
| * This is the maximum number of events that the device will enqueue for |
| * completion when there are no outstanding AERs. When the maximum number of |
| * enqueued events are reached, subsequent events will be dropped. |
| * |
| * - `mdts` |
| * Indicates the maximum data transfer size for a command that transfers data |
| * between host-accessible memory and the controller. The value is specified |
| * as a power of two (2^n) and is in units of the minimum memory page size |
| * (CAP.MPSMIN). The default value is 7 (i.e. 512 KiB). |
| * |
| * - `vsl` |
| * Indicates the maximum data size limit for the Verify command. Like `mdts`, |
| * this value is specified as a power of two (2^n) and is in units of the |
| * minimum memory page size (CAP.MPSMIN). The default value is 7 (i.e. 512 |
| * KiB). |
| * |
| * - `zoned.zasl` |
| * Indicates the maximum data transfer size for the Zone Append command. Like |
| * `mdts`, the value is specified as a power of two (2^n) and is in units of |
| * the minimum memory page size (CAP.MPSMIN). The default value is 0 (i.e. |
| * defaulting to the value of `mdts`). |
| * |
| * - `zoned.auto_transition` |
| * Indicates if zones in zone state implicitly opened can be automatically |
| * transitioned to zone state closed for resource management purposes. |
| * Defaults to 'on'. |
| * |
| * - `sriov_max_vfs` |
| * Indicates the maximum number of PCIe virtual functions supported |
| * by the controller. The default value is 0. Specifying a non-zero value |
| * enables reporting of both SR-IOV and ARI capabilities by the NVMe device. |
| * Virtual function controllers will not report SR-IOV capability. |
| * |
| * NOTE: Single Root I/O Virtualization support is experimental. |
| * All the related parameters may be subject to change. |
| * |
| * - `sriov_vq_flexible` |
| * Indicates the total number of flexible queue resources assignable to all |
| * the secondary controllers. Implicitly sets the number of primary |
| * controller's private resources to `(max_ioqpairs - sriov_vq_flexible)`. |
| * |
| * - `sriov_vi_flexible` |
| * Indicates the total number of flexible interrupt resources assignable to |
| * all the secondary controllers. Implicitly sets the number of primary |
| * controller's private resources to `(msix_qsize - sriov_vi_flexible)`. |
| * |
| * - `sriov_max_vi_per_vf` |
| * Indicates the maximum number of virtual interrupt resources assignable |
| * to a secondary controller. The default 0 resolves to |
| * `(sriov_vi_flexible / sriov_max_vfs)`. |
| * |
| * - `sriov_max_vq_per_vf` |
| * Indicates the maximum number of virtual queue resources assignable to |
| * a secondary controller. The default 0 resolves to |
| * `(sriov_vq_flexible / sriov_max_vfs)`. |
| * |
| * nvme namespace device parameters |
| * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| * - `shared` |
| * When the parent nvme device (as defined explicitly by the 'bus' parameter |
| * or implicitly by the most recently defined NvmeBus) is linked to an |
| * nvme-subsys device, the namespace will be attached to all controllers in |
| * the subsystem. If set to 'off' (the default), the namespace will remain a |
| * private namespace and may only be attached to a single controller at a |
| * time. |
| * |
| * - `detached` |
| * This parameter is only valid together with the `subsys` parameter. If left |
| * at the default value (`false/off`), the namespace will be attached to all |
| * controllers in the NVMe subsystem at boot-up. If set to `true/on`, the |
| * namespace will be available in the subsystem but not attached to any |
| * controllers. |
| * |
| * Setting `zoned` to true selects Zoned Command Set at the namespace. |
| * In this case, the following namespace properties are available to configure |
| * zoned operation: |
| * zoned.zone_size=<zone size in bytes, default: 128MiB> |
| * The number may be followed by K, M, G as in kilo-, mega- or giga-. |
| * |
| * zoned.zone_capacity=<zone capacity in bytes, default: zone size> |
| * The value 0 (default) forces zone capacity to be the same as zone |
| * size. The value of this property may not exceed zone size. |
| * |
| * zoned.descr_ext_size=<zone descriptor extension size, default 0> |
| * This value needs to be specified in 64B units. If it is zero, |
| * namespace(s) will not support zone descriptor extensions. |
| * |
| * zoned.max_active=<Maximum Active Resources (zones), default: 0> |
| * The default value means there is no limit to the number of |
| * concurrently active zones. |
| * |
| * zoned.max_open=<Maximum Open Resources (zones), default: 0> |
| * The default value means there is no limit to the number of |
| * concurrently open zones. |
| * |
| * zoned.cross_read=<enable RAZB, default: false> |
| * Setting this property to true enables Read Across Zone Boundaries. |
| */ |
| |
| #include "qemu/osdep.h" |
| #include "qemu/cutils.h" |
| #include "qemu/error-report.h" |
| #include "qemu/log.h" |
| #include "qemu/units.h" |
| #include "qemu/range.h" |
| #include "qapi/error.h" |
| #include "qapi/visitor.h" |
| #include "sysemu/sysemu.h" |
| #include "sysemu/block-backend.h" |
| #include "sysemu/hostmem.h" |
| #include "hw/pci/msix.h" |
| #include "hw/pci/pcie_sriov.h" |
| #include "migration/vmstate.h" |
| |
| #include "nvme.h" |
| #include "dif.h" |
| #include "trace.h" |
| |
| #define NVME_MAX_IOQPAIRS 0xffff |
| #define NVME_DB_SIZE 4 |
| #define NVME_SPEC_VER 0x00010400 |
| #define NVME_CMB_BIR 2 |
| #define NVME_PMR_BIR 4 |
| #define NVME_TEMPERATURE 0x143 |
| #define NVME_TEMPERATURE_WARNING 0x157 |
| #define NVME_TEMPERATURE_CRITICAL 0x175 |
| #define NVME_NUM_FW_SLOTS 1 |
| #define NVME_DEFAULT_MAX_ZA_SIZE (128 * KiB) |
| #define NVME_MAX_VFS 127 |
| #define NVME_VF_RES_GRANULARITY 1 |
| #define NVME_VF_OFFSET 0x1 |
| #define NVME_VF_STRIDE 1 |
| |
| #define NVME_GUEST_ERR(trace, fmt, ...) \ |
| do { \ |
| (trace_##trace)(__VA_ARGS__); \ |
| qemu_log_mask(LOG_GUEST_ERROR, #trace \ |
| " in %s: " fmt "\n", __func__, ## __VA_ARGS__); \ |
| } while (0) |
| |
| static const bool nvme_feature_support[NVME_FID_MAX] = { |
| [NVME_ARBITRATION] = true, |
| [NVME_POWER_MANAGEMENT] = true, |
| [NVME_TEMPERATURE_THRESHOLD] = true, |
| [NVME_ERROR_RECOVERY] = true, |
| [NVME_VOLATILE_WRITE_CACHE] = true, |
| [NVME_NUMBER_OF_QUEUES] = true, |
| [NVME_INTERRUPT_COALESCING] = true, |
| [NVME_INTERRUPT_VECTOR_CONF] = true, |
| [NVME_WRITE_ATOMICITY] = true, |
| [NVME_ASYNCHRONOUS_EVENT_CONF] = true, |
| [NVME_TIMESTAMP] = true, |
| [NVME_HOST_BEHAVIOR_SUPPORT] = true, |
| [NVME_COMMAND_SET_PROFILE] = true, |
| }; |
| |
| static const uint32_t nvme_feature_cap[NVME_FID_MAX] = { |
| [NVME_TEMPERATURE_THRESHOLD] = NVME_FEAT_CAP_CHANGE, |
| [NVME_ERROR_RECOVERY] = NVME_FEAT_CAP_CHANGE | NVME_FEAT_CAP_NS, |
| [NVME_VOLATILE_WRITE_CACHE] = NVME_FEAT_CAP_CHANGE, |
| [NVME_NUMBER_OF_QUEUES] = NVME_FEAT_CAP_CHANGE, |
| [NVME_ASYNCHRONOUS_EVENT_CONF] = NVME_FEAT_CAP_CHANGE, |
| [NVME_TIMESTAMP] = NVME_FEAT_CAP_CHANGE, |
| [NVME_HOST_BEHAVIOR_SUPPORT] = NVME_FEAT_CAP_CHANGE, |
| [NVME_COMMAND_SET_PROFILE] = NVME_FEAT_CAP_CHANGE, |
| }; |
| |
| static const uint32_t nvme_cse_acs[256] = { |
| [NVME_ADM_CMD_DELETE_SQ] = NVME_CMD_EFF_CSUPP, |
| [NVME_ADM_CMD_CREATE_SQ] = NVME_CMD_EFF_CSUPP, |
| [NVME_ADM_CMD_GET_LOG_PAGE] = NVME_CMD_EFF_CSUPP, |
| [NVME_ADM_CMD_DELETE_CQ] = NVME_CMD_EFF_CSUPP, |
| [NVME_ADM_CMD_CREATE_CQ] = NVME_CMD_EFF_CSUPP, |
| [NVME_ADM_CMD_IDENTIFY] = NVME_CMD_EFF_CSUPP, |
| [NVME_ADM_CMD_ABORT] = NVME_CMD_EFF_CSUPP, |
| [NVME_ADM_CMD_SET_FEATURES] = NVME_CMD_EFF_CSUPP, |
| [NVME_ADM_CMD_GET_FEATURES] = NVME_CMD_EFF_CSUPP, |
| [NVME_ADM_CMD_ASYNC_EV_REQ] = NVME_CMD_EFF_CSUPP, |
| [NVME_ADM_CMD_NS_ATTACHMENT] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_NIC, |
| [NVME_ADM_CMD_VIRT_MNGMT] = NVME_CMD_EFF_CSUPP, |
| [NVME_ADM_CMD_DBBUF_CONFIG] = NVME_CMD_EFF_CSUPP, |
| [NVME_ADM_CMD_FORMAT_NVM] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC, |
| }; |
| |
| static const uint32_t nvme_cse_iocs_none[256]; |
| |
| static const uint32_t nvme_cse_iocs_nvm[256] = { |
| [NVME_CMD_FLUSH] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC, |
| [NVME_CMD_WRITE_ZEROES] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC, |
| [NVME_CMD_WRITE] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC, |
| [NVME_CMD_READ] = NVME_CMD_EFF_CSUPP, |
| [NVME_CMD_DSM] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC, |
| [NVME_CMD_VERIFY] = NVME_CMD_EFF_CSUPP, |
| [NVME_CMD_COPY] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC, |
| [NVME_CMD_COMPARE] = NVME_CMD_EFF_CSUPP, |
| }; |
| |
| static const uint32_t nvme_cse_iocs_zoned[256] = { |
| [NVME_CMD_FLUSH] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC, |
| [NVME_CMD_WRITE_ZEROES] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC, |
| [NVME_CMD_WRITE] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC, |
| [NVME_CMD_READ] = NVME_CMD_EFF_CSUPP, |
| [NVME_CMD_DSM] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC, |
| [NVME_CMD_VERIFY] = NVME_CMD_EFF_CSUPP, |
| [NVME_CMD_COPY] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC, |
| [NVME_CMD_COMPARE] = NVME_CMD_EFF_CSUPP, |
| [NVME_CMD_ZONE_APPEND] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC, |
| [NVME_CMD_ZONE_MGMT_SEND] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC, |
| [NVME_CMD_ZONE_MGMT_RECV] = NVME_CMD_EFF_CSUPP, |
| }; |
| |
| static void nvme_process_sq(void *opaque); |
| static void nvme_ctrl_reset(NvmeCtrl *n, NvmeResetType rst); |
| |
| static uint16_t nvme_sqid(NvmeRequest *req) |
| { |
| return le16_to_cpu(req->sq->sqid); |
| } |
| |
| static void nvme_assign_zone_state(NvmeNamespace *ns, NvmeZone *zone, |
| NvmeZoneState state) |
| { |
| if (QTAILQ_IN_USE(zone, entry)) { |
| switch (nvme_get_zone_state(zone)) { |
| case NVME_ZONE_STATE_EXPLICITLY_OPEN: |
| QTAILQ_REMOVE(&ns->exp_open_zones, zone, entry); |
| break; |
| case NVME_ZONE_STATE_IMPLICITLY_OPEN: |
| QTAILQ_REMOVE(&ns->imp_open_zones, zone, entry); |
| break; |
| case NVME_ZONE_STATE_CLOSED: |
| QTAILQ_REMOVE(&ns->closed_zones, zone, entry); |
| break; |
| case NVME_ZONE_STATE_FULL: |
| QTAILQ_REMOVE(&ns->full_zones, zone, entry); |
| default: |
| ; |
| } |
| } |
| |
| nvme_set_zone_state(zone, state); |
| |
| switch (state) { |
| case NVME_ZONE_STATE_EXPLICITLY_OPEN: |
| QTAILQ_INSERT_TAIL(&ns->exp_open_zones, zone, entry); |
| break; |
| case NVME_ZONE_STATE_IMPLICITLY_OPEN: |
| QTAILQ_INSERT_TAIL(&ns->imp_open_zones, zone, entry); |
| break; |
| case NVME_ZONE_STATE_CLOSED: |
| QTAILQ_INSERT_TAIL(&ns->closed_zones, zone, entry); |
| break; |
| case NVME_ZONE_STATE_FULL: |
| QTAILQ_INSERT_TAIL(&ns->full_zones, zone, entry); |
| case NVME_ZONE_STATE_READ_ONLY: |
| break; |
| default: |
| zone->d.za = 0; |
| } |
| } |
| |
| static uint16_t nvme_zns_check_resources(NvmeNamespace *ns, uint32_t act, |
| uint32_t opn, uint32_t zrwa) |
| { |
| if (ns->params.max_active_zones != 0 && |
| ns->nr_active_zones + act > ns->params.max_active_zones) { |
| trace_pci_nvme_err_insuff_active_res(ns->params.max_active_zones); |
| return NVME_ZONE_TOO_MANY_ACTIVE | NVME_DNR; |
| } |
| |
| if (ns->params.max_open_zones != 0 && |
| ns->nr_open_zones + opn > ns->params.max_open_zones) { |
| trace_pci_nvme_err_insuff_open_res(ns->params.max_open_zones); |
| return NVME_ZONE_TOO_MANY_OPEN | NVME_DNR; |
| } |
| |
| if (zrwa > ns->zns.numzrwa) { |
| return NVME_NOZRWA | NVME_DNR; |
| } |
| |
| return NVME_SUCCESS; |
| } |
| |
| /* |
| * Check if we can open a zone without exceeding open/active limits. |
| * AOR stands for "Active and Open Resources" (see TP 4053 section 2.5). |
| */ |
| static uint16_t nvme_aor_check(NvmeNamespace *ns, uint32_t act, uint32_t opn) |
| { |
| return nvme_zns_check_resources(ns, act, opn, 0); |
| } |
| |
| static bool nvme_addr_is_cmb(NvmeCtrl *n, hwaddr addr) |
| { |
| hwaddr hi, lo; |
| |
| if (!n->cmb.cmse) { |
| return false; |
| } |
| |
| lo = n->params.legacy_cmb ? n->cmb.mem.addr : n->cmb.cba; |
| hi = lo + int128_get64(n->cmb.mem.size); |
| |
| return addr >= lo && addr < hi; |
| } |
| |
| static inline void *nvme_addr_to_cmb(NvmeCtrl *n, hwaddr addr) |
| { |
| hwaddr base = n->params.legacy_cmb ? n->cmb.mem.addr : n->cmb.cba; |
| return &n->cmb.buf[addr - base]; |
| } |
| |
| static bool nvme_addr_is_pmr(NvmeCtrl *n, hwaddr addr) |
| { |
| hwaddr hi; |
| |
| if (!n->pmr.cmse) { |
| return false; |
| } |
| |
| hi = n->pmr.cba + int128_get64(n->pmr.dev->mr.size); |
| |
| return addr >= n->pmr.cba && addr < hi; |
| } |
| |
| static inline void *nvme_addr_to_pmr(NvmeCtrl *n, hwaddr addr) |
| { |
| return memory_region_get_ram_ptr(&n->pmr.dev->mr) + (addr - n->pmr.cba); |
| } |
| |
| static inline bool nvme_addr_is_iomem(NvmeCtrl *n, hwaddr addr) |
| { |
| hwaddr hi, lo; |
| |
| /* |
| * The purpose of this check is to guard against invalid "local" access to |
| * the iomem (i.e. controller registers). Thus, we check against the range |
| * covered by the 'bar0' MemoryRegion since that is currently composed of |
| * two subregions (the NVMe "MBAR" and the MSI-X table/pba). Note, however, |
| * that if the device model is ever changed to allow the CMB to be located |
| * in BAR0 as well, then this must be changed. |
| */ |
| lo = n->bar0.addr; |
| hi = lo + int128_get64(n->bar0.size); |
| |
| return addr >= lo && addr < hi; |
| } |
| |
| static int nvme_addr_read(NvmeCtrl *n, hwaddr addr, void *buf, int size) |
| { |
| hwaddr hi = addr + size - 1; |
| if (hi < addr) { |
| return 1; |
| } |
| |
| if (n->bar.cmbsz && nvme_addr_is_cmb(n, addr) && nvme_addr_is_cmb(n, hi)) { |
| memcpy(buf, nvme_addr_to_cmb(n, addr), size); |
| return 0; |
| } |
| |
| if (nvme_addr_is_pmr(n, addr) && nvme_addr_is_pmr(n, hi)) { |
| memcpy(buf, nvme_addr_to_pmr(n, addr), size); |
| return 0; |
| } |
| |
| return pci_dma_read(&n->parent_obj, addr, buf, size); |
| } |
| |
| static int nvme_addr_write(NvmeCtrl *n, hwaddr addr, const void *buf, int size) |
| { |
| hwaddr hi = addr + size - 1; |
| if (hi < addr) { |
| return 1; |
| } |
| |
| if (n->bar.cmbsz && nvme_addr_is_cmb(n, addr) && nvme_addr_is_cmb(n, hi)) { |
| memcpy(nvme_addr_to_cmb(n, addr), buf, size); |
| return 0; |
| } |
| |
| if (nvme_addr_is_pmr(n, addr) && nvme_addr_is_pmr(n, hi)) { |
| memcpy(nvme_addr_to_pmr(n, addr), buf, size); |
| return 0; |
| } |
| |
| return pci_dma_write(&n->parent_obj, addr, buf, size); |
| } |
| |
| static bool nvme_nsid_valid(NvmeCtrl *n, uint32_t nsid) |
| { |
| return nsid && |
| (nsid == NVME_NSID_BROADCAST || nsid <= NVME_MAX_NAMESPACES); |
| } |
| |
| static int nvme_check_sqid(NvmeCtrl *n, uint16_t sqid) |
| { |
| return sqid < n->conf_ioqpairs + 1 && n->sq[sqid] != NULL ? 0 : -1; |
| } |
| |
| static int nvme_check_cqid(NvmeCtrl *n, uint16_t cqid) |
| { |
| return cqid < n->conf_ioqpairs + 1 && n->cq[cqid] != NULL ? 0 : -1; |
| } |
| |
| static void nvme_inc_cq_tail(NvmeCQueue *cq) |
| { |
| cq->tail++; |
| if (cq->tail >= cq->size) { |
| cq->tail = 0; |
| cq->phase = !cq->phase; |
| } |
| } |
| |
| static void nvme_inc_sq_head(NvmeSQueue *sq) |
| { |
| sq->head = (sq->head + 1) % sq->size; |
| } |
| |
| static uint8_t nvme_cq_full(NvmeCQueue *cq) |
| { |
| return (cq->tail + 1) % cq->size == cq->head; |
| } |
| |
| static uint8_t nvme_sq_empty(NvmeSQueue *sq) |
| { |
| return sq->head == sq->tail; |
| } |
| |
| static void nvme_irq_check(NvmeCtrl *n) |
| { |
| uint32_t intms = ldl_le_p(&n->bar.intms); |
| |
| if (msix_enabled(&(n->parent_obj))) { |
| return; |
| } |
| if (~intms & n->irq_status) { |
| pci_irq_assert(&n->parent_obj); |
| } else { |
| pci_irq_deassert(&n->parent_obj); |
| } |
| } |
| |
| static void nvme_irq_assert(NvmeCtrl *n, NvmeCQueue *cq) |
| { |
| if (cq->irq_enabled) { |
| if (msix_enabled(&(n->parent_obj))) { |
| trace_pci_nvme_irq_msix(cq->vector); |
| msix_notify(&(n->parent_obj), cq->vector); |
| } else { |
| trace_pci_nvme_irq_pin(); |
| assert(cq->vector < 32); |
| n->irq_status |= 1 << cq->vector; |
| nvme_irq_check(n); |
| } |
| } else { |
| trace_pci_nvme_irq_masked(); |
| } |
| } |
| |
| static void nvme_irq_deassert(NvmeCtrl *n, NvmeCQueue *cq) |
| { |
| if (cq->irq_enabled) { |
| if (msix_enabled(&(n->parent_obj))) { |
| return; |
| } else { |
| assert(cq->vector < 32); |
| if (!n->cq_pending) { |
| n->irq_status &= ~(1 << cq->vector); |
| } |
| nvme_irq_check(n); |
| } |
| } |
| } |
| |
| static void nvme_req_clear(NvmeRequest *req) |
| { |
| req->ns = NULL; |
| req->opaque = NULL; |
| req->aiocb = NULL; |
| memset(&req->cqe, 0x0, sizeof(req->cqe)); |
| req->status = NVME_SUCCESS; |
| } |
| |
| static inline void nvme_sg_init(NvmeCtrl *n, NvmeSg *sg, bool dma) |
| { |
| if (dma) { |
| pci_dma_sglist_init(&sg->qsg, &n->parent_obj, 0); |
| sg->flags = NVME_SG_DMA; |
| } else { |
| qemu_iovec_init(&sg->iov, 0); |
| } |
| |
| sg->flags |= NVME_SG_ALLOC; |
| } |
| |
| static inline void nvme_sg_unmap(NvmeSg *sg) |
| { |
| if (!(sg->flags & NVME_SG_ALLOC)) { |
| return; |
| } |
| |
| if (sg->flags & NVME_SG_DMA) { |
| qemu_sglist_destroy(&sg->qsg); |
| } else { |
| qemu_iovec_destroy(&sg->iov); |
| } |
| |
| memset(sg, 0x0, sizeof(*sg)); |
| } |
| |
| /* |
| * When metadata is transfered as extended LBAs, the DPTR mapped into `sg` |
| * holds both data and metadata. This function splits the data and metadata |
| * into two separate QSG/IOVs. |
| */ |
| static void nvme_sg_split(NvmeSg *sg, NvmeNamespace *ns, NvmeSg *data, |
| NvmeSg *mdata) |
| { |
| NvmeSg *dst = data; |
| uint32_t trans_len, count = ns->lbasz; |
| uint64_t offset = 0; |
| bool dma = sg->flags & NVME_SG_DMA; |
| size_t sge_len; |
| size_t sg_len = dma ? sg->qsg.size : sg->iov.size; |
| int sg_idx = 0; |
| |
| assert(sg->flags & NVME_SG_ALLOC); |
| |
| while (sg_len) { |
| sge_len = dma ? sg->qsg.sg[sg_idx].len : sg->iov.iov[sg_idx].iov_len; |
| |
| trans_len = MIN(sg_len, count); |
| trans_len = MIN(trans_len, sge_len - offset); |
| |
| if (dst) { |
| if (dma) { |
| qemu_sglist_add(&dst->qsg, sg->qsg.sg[sg_idx].base + offset, |
| trans_len); |
| } else { |
| qemu_iovec_add(&dst->iov, |
| sg->iov.iov[sg_idx].iov_base + offset, |
| trans_len); |
| } |
| } |
| |
| sg_len -= trans_len; |
| count -= trans_len; |
| offset += trans_len; |
| |
| if (count == 0) { |
| dst = (dst == data) ? mdata : data; |
| count = (dst == data) ? ns->lbasz : ns->lbaf.ms; |
| } |
| |
| if (sge_len == offset) { |
| offset = 0; |
| sg_idx++; |
| } |
| } |
| } |
| |
| static uint16_t nvme_map_addr_cmb(NvmeCtrl *n, QEMUIOVector *iov, hwaddr addr, |
| size_t len) |
| { |
| if (!len) { |
| return NVME_SUCCESS; |
| } |
| |
| trace_pci_nvme_map_addr_cmb(addr, len); |
| |
| if (!nvme_addr_is_cmb(n, addr) || !nvme_addr_is_cmb(n, addr + len - 1)) { |
| return NVME_DATA_TRAS_ERROR; |
| } |
| |
| qemu_iovec_add(iov, nvme_addr_to_cmb(n, addr), len); |
| |
| return NVME_SUCCESS; |
| } |
| |
| static uint16_t nvme_map_addr_pmr(NvmeCtrl *n, QEMUIOVector *iov, hwaddr addr, |
| size_t len) |
| { |
| if (!len) { |
| return NVME_SUCCESS; |
| } |
| |
| if (!nvme_addr_is_pmr(n, addr) || !nvme_addr_is_pmr(n, addr + len - 1)) { |
| return NVME_DATA_TRAS_ERROR; |
| } |
| |
| qemu_iovec_add(iov, nvme_addr_to_pmr(n, addr), len); |
| |
| return NVME_SUCCESS; |
| } |
| |
| static uint16_t nvme_map_addr(NvmeCtrl *n, NvmeSg *sg, hwaddr addr, size_t len) |
| { |
| bool cmb = false, pmr = false; |
| |
| if (!len) { |
| return NVME_SUCCESS; |
| } |
| |
| trace_pci_nvme_map_addr(addr, len); |
| |
| if (nvme_addr_is_iomem(n, addr)) { |
| return NVME_DATA_TRAS_ERROR; |
| } |
| |
| if (nvme_addr_is_cmb(n, addr)) { |
| cmb = true; |
| } else if (nvme_addr_is_pmr(n, addr)) { |
| pmr = true; |
| } |
| |
| if (cmb || pmr) { |
| if (sg->flags & NVME_SG_DMA) { |
| return NVME_INVALID_USE_OF_CMB | NVME_DNR; |
| } |
| |
| if (sg->iov.niov + 1 > IOV_MAX) { |
| goto max_mappings_exceeded; |
| } |
| |
| if (cmb) { |
| return nvme_map_addr_cmb(n, &sg->iov, addr, len); |
| } else { |
| return nvme_map_addr_pmr(n, &sg->iov, addr, len); |
| } |
| } |
| |
| if (!(sg->flags & NVME_SG_DMA)) { |
| return NVME_INVALID_USE_OF_CMB | NVME_DNR; |
| } |
| |
| if (sg->qsg.nsg + 1 > IOV_MAX) { |
| goto max_mappings_exceeded; |
| } |
| |
| qemu_sglist_add(&sg->qsg, addr, len); |
| |
| return NVME_SUCCESS; |
| |
| max_mappings_exceeded: |
| NVME_GUEST_ERR(pci_nvme_ub_too_many_mappings, |
| "number of mappings exceed 1024"); |
| return NVME_INTERNAL_DEV_ERROR | NVME_DNR; |
| } |
| |
| static inline bool nvme_addr_is_dma(NvmeCtrl *n, hwaddr addr) |
| { |
| return !(nvme_addr_is_cmb(n, addr) || nvme_addr_is_pmr(n, addr)); |
| } |
| |
| static uint16_t nvme_map_prp(NvmeCtrl *n, NvmeSg *sg, uint64_t prp1, |
| uint64_t prp2, uint32_t len) |
| { |
| hwaddr trans_len = n->page_size - (prp1 % n->page_size); |
| trans_len = MIN(len, trans_len); |
| int num_prps = (len >> n->page_bits) + 1; |
| uint16_t status; |
| int ret; |
| |
| trace_pci_nvme_map_prp(trans_len, len, prp1, prp2, num_prps); |
| |
| nvme_sg_init(n, sg, nvme_addr_is_dma(n, prp1)); |
| |
| status = nvme_map_addr(n, sg, prp1, trans_len); |
| if (status) { |
| goto unmap; |
| } |
| |
| len -= trans_len; |
| if (len) { |
| if (len > n->page_size) { |
| uint64_t prp_list[n->max_prp_ents]; |
| uint32_t nents, prp_trans; |
| int i = 0; |
| |
| /* |
| * The first PRP list entry, pointed to by PRP2 may contain offset. |
| * Hence, we need to calculate the number of entries in based on |
| * that offset. |
| */ |
| nents = (n->page_size - (prp2 & (n->page_size - 1))) >> 3; |
| prp_trans = MIN(n->max_prp_ents, nents) * sizeof(uint64_t); |
| ret = nvme_addr_read(n, prp2, (void *)prp_list, prp_trans); |
| if (ret) { |
| trace_pci_nvme_err_addr_read(prp2); |
| status = NVME_DATA_TRAS_ERROR; |
| goto unmap; |
| } |
| while (len != 0) { |
| uint64_t prp_ent = le64_to_cpu(prp_list[i]); |
| |
| if (i == nents - 1 && len > n->page_size) { |
| if (unlikely(prp_ent & (n->page_size - 1))) { |
| trace_pci_nvme_err_invalid_prplist_ent(prp_ent); |
| status = NVME_INVALID_PRP_OFFSET | NVME_DNR; |
| goto unmap; |
| } |
| |
| i = 0; |
| nents = (len + n->page_size - 1) >> n->page_bits; |
| nents = MIN(nents, n->max_prp_ents); |
| prp_trans = nents * sizeof(uint64_t); |
| ret = nvme_addr_read(n, prp_ent, (void *)prp_list, |
| prp_trans); |
| if (ret) { |
| trace_pci_nvme_err_addr_read(prp_ent); |
| status = NVME_DATA_TRAS_ERROR; |
| goto unmap; |
| } |
| prp_ent = le64_to_cpu(prp_list[i]); |
| } |
| |
| if (unlikely(prp_ent & (n->page_size - 1))) { |
| trace_pci_nvme_err_invalid_prplist_ent(prp_ent); |
| status = NVME_INVALID_PRP_OFFSET | NVME_DNR; |
| goto unmap; |
| } |
| |
| trans_len = MIN(len, n->page_size); |
| status = nvme_map_addr(n, sg, prp_ent, trans_len); |
| if (status) { |
| goto unmap; |
| } |
| |
| len -= trans_len; |
| i++; |
| } |
| } else { |
| if (unlikely(prp2 & (n->page_size - 1))) { |
| trace_pci_nvme_err_invalid_prp2_align(prp2); |
| status = NVME_INVALID_PRP_OFFSET | NVME_DNR; |
| goto unmap; |
| } |
| status = nvme_map_addr(n, sg, prp2, len); |
| if (status) { |
| goto unmap; |
| } |
| } |
| } |
| |
| return NVME_SUCCESS; |
| |
| unmap: |
| nvme_sg_unmap(sg); |
| return status; |
| } |
| |
| /* |
| * Map 'nsgld' data descriptors from 'segment'. The function will subtract the |
| * number of bytes mapped in len. |
| */ |
| static uint16_t nvme_map_sgl_data(NvmeCtrl *n, NvmeSg *sg, |
| NvmeSglDescriptor *segment, uint64_t nsgld, |
| size_t *len, NvmeCmd *cmd) |
| { |
| dma_addr_t addr, trans_len; |
| uint32_t dlen; |
| uint16_t status; |
| |
| for (int i = 0; i < nsgld; i++) { |
| uint8_t type = NVME_SGL_TYPE(segment[i].type); |
| |
| switch (type) { |
| case NVME_SGL_DESCR_TYPE_DATA_BLOCK: |
| break; |
| case NVME_SGL_DESCR_TYPE_SEGMENT: |
| case NVME_SGL_DESCR_TYPE_LAST_SEGMENT: |
| return NVME_INVALID_NUM_SGL_DESCRS | NVME_DNR; |
| default: |
| return NVME_SGL_DESCR_TYPE_INVALID | NVME_DNR; |
| } |
| |
| dlen = le32_to_cpu(segment[i].len); |
| |
| if (!dlen) { |
| continue; |
| } |
| |
| if (*len == 0) { |
| /* |
| * All data has been mapped, but the SGL contains additional |
| * segments and/or descriptors. The controller might accept |
| * ignoring the rest of the SGL. |
| */ |
| uint32_t sgls = le32_to_cpu(n->id_ctrl.sgls); |
| if (sgls & NVME_CTRL_SGLS_EXCESS_LENGTH) { |
| break; |
| } |
| |
| trace_pci_nvme_err_invalid_sgl_excess_length(dlen); |
| return NVME_DATA_SGL_LEN_INVALID | NVME_DNR; |
| } |
| |
| trans_len = MIN(*len, dlen); |
| |
| addr = le64_to_cpu(segment[i].addr); |
| |
| if (UINT64_MAX - addr < dlen) { |
| return NVME_DATA_SGL_LEN_INVALID | NVME_DNR; |
| } |
| |
| status = nvme_map_addr(n, sg, addr, trans_len); |
| if (status) { |
| return status; |
| } |
| |
| *len -= trans_len; |
| } |
| |
| return NVME_SUCCESS; |
| } |
| |
| static uint16_t nvme_map_sgl(NvmeCtrl *n, NvmeSg *sg, NvmeSglDescriptor sgl, |
| size_t len, NvmeCmd *cmd) |
| { |
| /* |
| * Read the segment in chunks of 256 descriptors (one 4k page) to avoid |
| * dynamically allocating a potentially huge SGL. The spec allows the SGL |
| * to be larger (as in number of bytes required to describe the SGL |
| * descriptors and segment chain) than the command transfer size, so it is |
| * not bounded by MDTS. |
| */ |
| const int SEG_CHUNK_SIZE = 256; |
| |
| NvmeSglDescriptor segment[SEG_CHUNK_SIZE], *sgld, *last_sgld; |
| uint64_t nsgld; |
| uint32_t seg_len; |
| uint16_t status; |
| hwaddr addr; |
| int ret; |
| |
| sgld = &sgl; |
| addr = le64_to_cpu(sgl.addr); |
| |
| trace_pci_nvme_map_sgl(NVME_SGL_TYPE(sgl.type), len); |
| |
| nvme_sg_init(n, sg, nvme_addr_is_dma(n, addr)); |
| |
| /* |
| * If the entire transfer can be described with a single data block it can |
| * be mapped directly. |
| */ |
| if (NVME_SGL_TYPE(sgl.type) == NVME_SGL_DESCR_TYPE_DATA_BLOCK) { |
| status = nvme_map_sgl_data(n, sg, sgld, 1, &len, cmd); |
| if (status) { |
| goto unmap; |
| } |
| |
| goto out; |
| } |
| |
| for (;;) { |
| switch (NVME_SGL_TYPE(sgld->type)) { |
| case NVME_SGL_DESCR_TYPE_SEGMENT: |
| case NVME_SGL_DESCR_TYPE_LAST_SEGMENT: |
| break; |
| default: |
| return NVME_INVALID_SGL_SEG_DESCR | NVME_DNR; |
| } |
| |
| seg_len = le32_to_cpu(sgld->len); |
| |
| /* check the length of the (Last) Segment descriptor */ |
| if (!seg_len || seg_len & 0xf) { |
| return NVME_INVALID_SGL_SEG_DESCR | NVME_DNR; |
| } |
| |
| if (UINT64_MAX - addr < seg_len) { |
| return NVME_DATA_SGL_LEN_INVALID | NVME_DNR; |
| } |
| |
| nsgld = seg_len / sizeof(NvmeSglDescriptor); |
| |
| while (nsgld > SEG_CHUNK_SIZE) { |
| if (nvme_addr_read(n, addr, segment, sizeof(segment))) { |
| trace_pci_nvme_err_addr_read(addr); |
| status = NVME_DATA_TRAS_ERROR; |
| goto unmap; |
| } |
| |
| status = nvme_map_sgl_data(n, sg, segment, SEG_CHUNK_SIZE, |
| &len, cmd); |
| if (status) { |
| goto unmap; |
| } |
| |
| nsgld -= SEG_CHUNK_SIZE; |
| addr += SEG_CHUNK_SIZE * sizeof(NvmeSglDescriptor); |
| } |
| |
| ret = nvme_addr_read(n, addr, segment, nsgld * |
| sizeof(NvmeSglDescriptor)); |
| if (ret) { |
| trace_pci_nvme_err_addr_read(addr); |
| status = NVME_DATA_TRAS_ERROR; |
| goto unmap; |
| } |
| |
| last_sgld = &segment[nsgld - 1]; |
| |
| /* |
| * If the segment ends with a Data Block, then we are done. |
| */ |
| if (NVME_SGL_TYPE(last_sgld->type) == NVME_SGL_DESCR_TYPE_DATA_BLOCK) { |
| status = nvme_map_sgl_data(n, sg, segment, nsgld, &len, cmd); |
| if (status) { |
| goto unmap; |
| } |
| |
| goto out; |
| } |
| |
| /* |
| * If the last descriptor was not a Data Block, then the current |
| * segment must not be a Last Segment. |
| */ |
| if (NVME_SGL_TYPE(sgld->type) == NVME_SGL_DESCR_TYPE_LAST_SEGMENT) { |
| status = NVME_INVALID_SGL_SEG_DESCR | NVME_DNR; |
| goto unmap; |
| } |
| |
| sgld = last_sgld; |
| addr = le64_to_cpu(sgld->addr); |
| |
| /* |
| * Do not map the last descriptor; it will be a Segment or Last Segment |
| * descriptor and is handled by the next iteration. |
| */ |
| status = nvme_map_sgl_data(n, sg, segment, nsgld - 1, &len, cmd); |
| if (status) { |
| goto unmap; |
| } |
| } |
| |
| out: |
| /* if there is any residual left in len, the SGL was too short */ |
| if (len) { |
| status = NVME_DATA_SGL_LEN_INVALID | NVME_DNR; |
| goto unmap; |
| } |
| |
| return NVME_SUCCESS; |
| |
| unmap: |
| nvme_sg_unmap(sg); |
| return status; |
| } |
| |
| uint16_t nvme_map_dptr(NvmeCtrl *n, NvmeSg *sg, size_t len, |
| NvmeCmd *cmd) |
| { |
| uint64_t prp1, prp2; |
| |
| switch (NVME_CMD_FLAGS_PSDT(cmd->flags)) { |
| case NVME_PSDT_PRP: |
| prp1 = le64_to_cpu(cmd->dptr.prp1); |
| prp2 = le64_to_cpu(cmd->dptr.prp2); |
| |
| return nvme_map_prp(n, sg, prp1, prp2, len); |
| case NVME_PSDT_SGL_MPTR_CONTIGUOUS: |
| case NVME_PSDT_SGL_MPTR_SGL: |
| return nvme_map_sgl(n, sg, cmd->dptr.sgl, len, cmd); |
| default: |
| return NVME_INVALID_FIELD; |
| } |
| } |
| |
| static uint16_t nvme_map_mptr(NvmeCtrl *n, NvmeSg *sg, size_t len, |
| NvmeCmd *cmd) |
| { |
| int psdt = NVME_CMD_FLAGS_PSDT(cmd->flags); |
| hwaddr mptr = le64_to_cpu(cmd->mptr); |
| uint16_t status; |
| |
| if (psdt == NVME_PSDT_SGL_MPTR_SGL) { |
| NvmeSglDescriptor sgl; |
| |
| if (nvme_addr_read(n, mptr, &sgl, sizeof(sgl))) { |
| return NVME_DATA_TRAS_ERROR; |
| } |
| |
| status = nvme_map_sgl(n, sg, sgl, len, cmd); |
| if (status && (status & 0x7ff) == NVME_DATA_SGL_LEN_INVALID) { |
| status = NVME_MD_SGL_LEN_INVALID | NVME_DNR; |
| } |
| |
| return status; |
| } |
| |
| nvme_sg_init(n, sg, nvme_addr_is_dma(n, mptr)); |
| status = nvme_map_addr(n, sg, mptr, len); |
| if (status) { |
| nvme_sg_unmap(sg); |
| } |
| |
| return status; |
| } |
| |
| static uint16_t nvme_map_data(NvmeCtrl *n, uint32_t nlb, NvmeRequest *req) |
| { |
| NvmeNamespace *ns = req->ns; |
| NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd; |
| bool pi = !!NVME_ID_NS_DPS_TYPE(ns->id_ns.dps); |
| bool pract = !!(le16_to_cpu(rw->control) & NVME_RW_PRINFO_PRACT); |
| size_t len = nvme_l2b(ns, nlb); |
| uint16_t status; |
| |
| if (nvme_ns_ext(ns) && |
| !(pi && pract && ns->lbaf.ms == nvme_pi_tuple_size(ns))) { |
| NvmeSg sg; |
| |
| len += nvme_m2b(ns, nlb); |
| |
| status = nvme_map_dptr(n, &sg, len, &req->cmd); |
| if (status) { |
| return status; |
| } |
| |
| nvme_sg_init(n, &req->sg, sg.flags & NVME_SG_DMA); |
| nvme_sg_split(&sg, ns, &req->sg, NULL); |
| nvme_sg_unmap(&sg); |
| |
| return NVME_SUCCESS; |
| } |
| |
| return nvme_map_dptr(n, &req->sg, len, &req->cmd); |
| } |
| |
| static uint16_t nvme_map_mdata(NvmeCtrl *n, uint32_t nlb, NvmeRequest *req) |
| { |
| NvmeNamespace *ns = req->ns; |
| size_t len = nvme_m2b(ns, nlb); |
| uint16_t status; |
| |
| if (nvme_ns_ext(ns)) { |
| NvmeSg sg; |
| |
| len += nvme_l2b(ns, nlb); |
| |
| status = nvme_map_dptr(n, &sg, len, &req->cmd); |
| if (status) { |
| return status; |
| } |
| |
| nvme_sg_init(n, &req->sg, sg.flags & NVME_SG_DMA); |
| nvme_sg_split(&sg, ns, NULL, &req->sg); |
| nvme_sg_unmap(&sg); |
| |
| return NVME_SUCCESS; |
| } |
| |
| return nvme_map_mptr(n, &req->sg, len, &req->cmd); |
| } |
| |
| static uint16_t nvme_tx_interleaved(NvmeCtrl *n, NvmeSg *sg, uint8_t *ptr, |
| uint32_t len, uint32_t bytes, |
| int32_t skip_bytes, int64_t offset, |
| NvmeTxDirection dir) |
| { |
| hwaddr addr; |
| uint32_t trans_len, count = bytes; |
| bool dma = sg->flags & NVME_SG_DMA; |
| int64_t sge_len; |
| int sg_idx = 0; |
| int ret; |
| |
| assert(sg->flags & NVME_SG_ALLOC); |
| |
| while (len) { |
| sge_len = dma ? sg->qsg.sg[sg_idx].len : sg->iov.iov[sg_idx].iov_len; |
| |
| if (sge_len - offset < 0) { |
| offset -= sge_len; |
| sg_idx++; |
| continue; |
| } |
| |
| if (sge_len == offset) { |
| offset = 0; |
| sg_idx++; |
| continue; |
| } |
| |
| trans_len = MIN(len, count); |
| trans_len = MIN(trans_len, sge_len - offset); |
| |
| if (dma) { |
| addr = sg->qsg.sg[sg_idx].base + offset; |
| } else { |
| addr = (hwaddr)(uintptr_t)sg->iov.iov[sg_idx].iov_base + offset; |
| } |
| |
| if (dir == NVME_TX_DIRECTION_TO_DEVICE) { |
| ret = nvme_addr_read(n, addr, ptr, trans_len); |
| } else { |
| ret = nvme_addr_write(n, addr, ptr, trans_len); |
| } |
| |
| if (ret) { |
| return NVME_DATA_TRAS_ERROR; |
| } |
| |
| ptr += trans_len; |
| len -= trans_len; |
| count -= trans_len; |
| offset += trans_len; |
| |
| if (count == 0) { |
| count = bytes; |
| offset += skip_bytes; |
| } |
| } |
| |
| return NVME_SUCCESS; |
| } |
| |
| static uint16_t nvme_tx(NvmeCtrl *n, NvmeSg *sg, void *ptr, uint32_t len, |
| NvmeTxDirection dir) |
| { |
| assert(sg->flags & NVME_SG_ALLOC); |
| |
| if (sg->flags & NVME_SG_DMA) { |
| const MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED; |
| dma_addr_t residual; |
| |
| if (dir == NVME_TX_DIRECTION_TO_DEVICE) { |
| dma_buf_write(ptr, len, &residual, &sg->qsg, attrs); |
| } else { |
| dma_buf_read(ptr, len, &residual, &sg->qsg, attrs); |
| } |
| |
| if (unlikely(residual)) { |
| trace_pci_nvme_err_invalid_dma(); |
| return NVME_INVALID_FIELD | NVME_DNR; |
| } |
| } else { |
| size_t bytes; |
| |
| if (dir == NVME_TX_DIRECTION_TO_DEVICE) { |
| bytes = qemu_iovec_to_buf(&sg->iov, 0, ptr, len); |
| } else { |
| bytes = qemu_iovec_from_buf(&sg->iov, 0, ptr, len); |
| } |
| |
| if (unlikely(bytes != len)) { |
| trace_pci_nvme_err_invalid_dma(); |
| return NVME_INVALID_FIELD | NVME_DNR; |
| } |
| } |
| |
| return NVME_SUCCESS; |
| } |
| |
| static inline uint16_t nvme_c2h(NvmeCtrl *n, void *ptr, uint32_t len, |
| NvmeRequest *req) |
| { |
| uint16_t status; |
| |
| status = nvme_map_dptr(n, &req->sg, len, &req->cmd); |
| if (status) { |
| return status; |
| } |
| |
| return nvme_tx(n, &req->sg, ptr, len, NVME_TX_DIRECTION_FROM_DEVICE); |
| } |
| |
| static inline uint16_t nvme_h2c(NvmeCtrl *n, void *ptr, uint32_t len, |
| NvmeRequest *req) |
| { |
| uint16_t status; |
| |
| status = nvme_map_dptr(n, &req->sg, len, &req->cmd); |
| if (status) { |
| return status; |
| } |
| |
| return nvme_tx(n, &req->sg, ptr, len, NVME_TX_DIRECTION_TO_DEVICE); |
| } |
| |
| uint16_t nvme_bounce_data(NvmeCtrl *n, void *ptr, uint32_t len, |
| NvmeTxDirection dir, NvmeRequest *req) |
| { |
| NvmeNamespace *ns = req->ns; |
| NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd; |
| bool pi = !!NVME_ID_NS_DPS_TYPE(ns->id_ns.dps); |
| bool pract = !!(le16_to_cpu(rw->control) & NVME_RW_PRINFO_PRACT); |
| |
| if (nvme_ns_ext(ns) && |
| !(pi && pract && ns->lbaf.ms == nvme_pi_tuple_size(ns))) { |
| return nvme_tx_interleaved(n, &req->sg, ptr, len, ns->lbasz, |
| ns->lbaf.ms, 0, dir); |
| } |
| |
| return nvme_tx(n, &req->sg, ptr, len, dir); |
| } |
| |
| uint16_t nvme_bounce_mdata(NvmeCtrl *n, void *ptr, uint32_t len, |
| NvmeTxDirection dir, NvmeRequest *req) |
| { |
| NvmeNamespace *ns = req->ns; |
| uint16_t status; |
| |
| if (nvme_ns_ext(ns)) { |
| return nvme_tx_interleaved(n, &req->sg, ptr, len, ns->lbaf.ms, |
| ns->lbasz, ns->lbasz, dir); |
| } |
| |
| nvme_sg_unmap(&req->sg); |
| |
| status = nvme_map_mptr(n, &req->sg, len, &req->cmd); |
| if (status) { |
| return status; |
| } |
| |
| return nvme_tx(n, &req->sg, ptr, len, dir); |
| } |
| |
| static inline void nvme_blk_read(BlockBackend *blk, int64_t offset, |
| BlockCompletionFunc *cb, NvmeRequest *req) |
| { |
| assert(req->sg.flags & NVME_SG_ALLOC); |
| |
| if (req->sg.flags & NVME_SG_DMA) { |
| req->aiocb = dma_blk_read(blk, &req->sg.qsg, offset, BDRV_SECTOR_SIZE, |
| cb, req); |
| } else { |
| req->aiocb = blk_aio_preadv(blk, offset, &req->sg.iov, 0, cb, req); |
| } |
| } |
| |
| static inline void nvme_blk_write(BlockBackend *blk, int64_t offset, |
| BlockCompletionFunc *cb, NvmeRequest *req) |
| { |
| assert(req->sg.flags & NVME_SG_ALLOC); |
| |
| if (req->sg.flags & NVME_SG_DMA) { |
| req->aiocb = dma_blk_write(blk, &req->sg.qsg, offset, BDRV_SECTOR_SIZE, |
| cb, req); |
| } else { |
| req->aiocb = blk_aio_pwritev(blk, offset, &req->sg.iov, 0, cb, req); |
| } |
| } |
| |
| static void nvme_update_cq_head(NvmeCQueue *cq) |
| { |
| pci_dma_read(&cq->ctrl->parent_obj, cq->db_addr, &cq->head, |
| sizeof(cq->head)); |
| trace_pci_nvme_shadow_doorbell_cq(cq->cqid, cq->head); |
| } |
| |
| static void nvme_post_cqes(void *opaque) |
| { |
| NvmeCQueue *cq = opaque; |
| NvmeCtrl *n = cq->ctrl; |
| NvmeRequest *req, *next; |
| bool pending = cq->head != cq->tail; |
| int ret; |
| |
| QTAILQ_FOREACH_SAFE(req, &cq->req_list, entry, next) { |
| NvmeSQueue *sq; |
| hwaddr addr; |
| |
| if (n->dbbuf_enabled) { |
| nvme_update_cq_head(cq); |
| } |
| |
| if (nvme_cq_full(cq)) { |
| break; |
| } |
| |
| sq = req->sq; |
| req->cqe.status = cpu_to_le16((req->status << 1) | cq->phase); |
| req->cqe.sq_id = cpu_to_le16(sq->sqid); |
| req->cqe.sq_head = cpu_to_le16(sq->head); |
| addr = cq->dma_addr + cq->tail * n->cqe_size; |
| ret = pci_dma_write(&n->parent_obj, addr, (void *)&req->cqe, |
| sizeof(req->cqe)); |
| if (ret) { |
| trace_pci_nvme_err_addr_write(addr); |
| trace_pci_nvme_err_cfs(); |
| stl_le_p(&n->bar.csts, NVME_CSTS_FAILED); |
| break; |
| } |
| QTAILQ_REMOVE(&cq->req_list, req, entry); |
| nvme_inc_cq_tail(cq); |
| nvme_sg_unmap(&req->sg); |
| QTAILQ_INSERT_TAIL(&sq->req_list, req, entry); |
| } |
| if (cq->tail != cq->head) { |
| if (cq->irq_enabled && !pending) { |
| n->cq_pending++; |
| } |
| |
| nvme_irq_assert(n, cq); |
| } |
| } |
| |
| static void nvme_enqueue_req_completion(NvmeCQueue *cq, NvmeRequest *req) |
| { |
| assert(cq->cqid == req->sq->cqid); |
| trace_pci_nvme_enqueue_req_completion(nvme_cid(req), cq->cqid, |
| le32_to_cpu(req->cqe.result), |
| le32_to_cpu(req->cqe.dw1), |
| req->status); |
| |
| if (req->status) { |
| trace_pci_nvme_err_req_status(nvme_cid(req), nvme_nsid(req->ns), |
| req->status, req->cmd.opcode); |
| } |
| |
| QTAILQ_REMOVE(&req->sq->out_req_list, req, entry); |
| QTAILQ_INSERT_TAIL(&cq->req_list, req, entry); |
| |
| if (req->sq->ioeventfd_enabled) { |
| /* Post CQE directly since we are in main loop thread */ |
| nvme_post_cqes(cq); |
| } else { |
| /* Schedule the timer to post CQE later since we are in vcpu thread */ |
| timer_mod(cq->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 500); |
| } |
| } |
| |
| static void nvme_process_aers(void *opaque) |
| { |
| NvmeCtrl *n = opaque; |
| NvmeAsyncEvent *event, *next; |
| |
| trace_pci_nvme_process_aers(n->aer_queued); |
| |
| QTAILQ_FOREACH_SAFE(event, &n->aer_queue, entry, next) { |
| NvmeRequest *req; |
| NvmeAerResult *result; |
| |
| /* can't post cqe if there is nothing to complete */ |
| if (!n->outstanding_aers) { |
| trace_pci_nvme_no_outstanding_aers(); |
| break; |
| } |
| |
| /* ignore if masked (cqe posted, but event not cleared) */ |
| if (n->aer_mask & (1 << event->result.event_type)) { |
| trace_pci_nvme_aer_masked(event->result.event_type, n->aer_mask); |
| continue; |
| } |
| |
| QTAILQ_REMOVE(&n->aer_queue, event, entry); |
| n->aer_queued--; |
| |
| n->aer_mask |= 1 << event->result.event_type; |
| n->outstanding_aers--; |
| |
| req = n->aer_reqs[n->outstanding_aers]; |
| |
| result = (NvmeAerResult *) &req->cqe.result; |
| result->event_type = event->result.event_type; |
| result->event_info = event->result.event_info; |
| result->log_page = event->result.log_page; |
| g_free(event); |
| |
| trace_pci_nvme_aer_post_cqe(result->event_type, result->event_info, |
| result->log_page); |
| |
| nvme_enqueue_req_completion(&n->admin_cq, req); |
| } |
| } |
| |
| static void nvme_enqueue_event(NvmeCtrl *n, uint8_t event_type, |
| uint8_t event_info, uint8_t log_page) |
| { |
| NvmeAsyncEvent *event; |
| |
| trace_pci_nvme_enqueue_event(event_type, event_info, log_page); |
| |
| if (n->aer_queued == n->params.aer_max_queued) { |
| trace_pci_nvme_enqueue_event_noqueue(n->aer_queued); |
| return; |
| } |
| |
| event = g_new(NvmeAsyncEvent, 1); |
| event->result = (NvmeAerResult) { |
| .event_type = event_type, |
| .event_info = event_info, |
| .log_page = log_page, |
| }; |
| |
| QTAILQ_INSERT_TAIL(&n->aer_queue, event, entry); |
| n->aer_queued++; |
| |
| nvme_process_aers(n); |
| } |
| |
| static void nvme_smart_event(NvmeCtrl *n, uint8_t event) |
| { |
| uint8_t aer_info; |
| |
| /* Ref SPEC <Asynchronous Event Information 0x2013 SMART / Health Status> */ |
| if (!(NVME_AEC_SMART(n->features.async_config) & event)) { |
| return; |
| } |
| |
| switch (event) { |
| case NVME_SMART_SPARE: |
| aer_info = NVME_AER_INFO_SMART_SPARE_THRESH; |
| break; |
| case NVME_SMART_TEMPERATURE: |
| aer_info = NVME_AER_INFO_SMART_TEMP_THRESH; |
| break; |
| case NVME_SMART_RELIABILITY: |
| case NVME_SMART_MEDIA_READ_ONLY: |
| case NVME_SMART_FAILED_VOLATILE_MEDIA: |
| case NVME_SMART_PMR_UNRELIABLE: |
| aer_info = NVME_AER_INFO_SMART_RELIABILITY; |
| break; |
| default: |
| return; |
| } |
| |
| nvme_enqueue_event(n, NVME_AER_TYPE_SMART, aer_info, NVME_LOG_SMART_INFO); |
| } |
| |
| static void nvme_clear_events(NvmeCtrl *n, uint8_t event_type) |
| { |
| n->aer_mask &= ~(1 << event_type); |
| if (!QTAILQ_EMPTY(&n->aer_queue)) { |
| nvme_process_aers(n); |
| } |
| } |
| |
| static inline uint16_t nvme_check_mdts(NvmeCtrl *n, size_t len) |
| { |
| uint8_t mdts = n->params.mdts; |
| |
| if (mdts && len > n->page_size << mdts) { |
| trace_pci_nvme_err_mdts(len); |
| return NVME_INVALID_FIELD | NVME_DNR; |
| } |
| |
| return NVME_SUCCESS; |
| } |
| |
| static inline uint16_t nvme_check_bounds(NvmeNamespace *ns, uint64_t slba, |
| uint32_t nlb) |
| { |
| uint64_t nsze = le64_to_cpu(ns->id_ns.nsze); |
| |
| if (unlikely(UINT64_MAX - slba < nlb || slba + nlb > nsze)) { |
| trace_pci_nvme_err_invalid_lba_range(slba, nlb, nsze); |
| return NVME_LBA_RANGE | NVME_DNR; |
| } |
| |
| return NVME_SUCCESS; |
| } |
| |
| static int nvme_block_status_all(NvmeNamespace *ns, uint64_t slba, |
| uint32_t nlb, int flags) |
| { |
| BlockDriverState *bs = blk_bs(ns->blkconf.blk); |
| |
| int64_t pnum = 0, bytes = nvme_l2b(ns, nlb); |
| int64_t offset = nvme_l2b(ns, slba); |
| int ret; |
| |
| /* |
| * `pnum` holds the number of bytes after offset that shares the same |
| * allocation status as the byte at offset. If `pnum` is different from |
| * `bytes`, we should check the allocation status of the next range and |
| * continue this until all bytes have been checked. |
| */ |
| do { |
| bytes -= pnum; |
| |
| ret = bdrv_block_status(bs, offset, bytes, &pnum, NULL, NULL); |
| if (ret < 0) { |
| return ret; |
| } |
| |
| |
| trace_pci_nvme_block_status(offset, bytes, pnum, ret, |
| !!(ret & BDRV_BLOCK_ZERO)); |
| |
| if (!(ret & flags)) { |
| return 1; |
| } |
| |
| offset += pnum; |
| } while (pnum != bytes); |
| |
| return 0; |
| } |
| |
| static uint16_t nvme_check_dulbe(NvmeNamespace *ns, uint64_t slba, |
| uint32_t nlb) |
| { |
| int ret; |
| Error *err = NULL; |
| |
| ret = nvme_block_status_all(ns, slba, nlb, BDRV_BLOCK_DATA); |
| if (ret) { |
| if (ret < 0) { |
| error_setg_errno(&err, -ret, "unable to get block status"); |
| error_report_err(err); |
| |
| return NVME_INTERNAL_DEV_ERROR; |
| } |
| |
| return NVME_DULB; |
| } |
| |
| return NVME_SUCCESS; |
| } |
| |
| static void nvme_aio_err(NvmeRequest *req, int ret) |
| { |
| uint16_t status = NVME_SUCCESS; |
| Error *local_err = NULL; |
| |
| switch (req->cmd.opcode) { |
| case NVME_CMD_READ: |
| status = NVME_UNRECOVERED_READ; |
| break; |
| case NVME_CMD_FLUSH: |
| case NVME_CMD_WRITE: |
| case NVME_CMD_WRITE_ZEROES: |
| case NVME_CMD_ZONE_APPEND: |
| status = NVME_WRITE_FAULT; |
| break; |
| default: |
| status = NVME_INTERNAL_DEV_ERROR; |
| break; |
| } |
| |
| trace_pci_nvme_err_aio(nvme_cid(req), strerror(-ret), status); |
| |
| error_setg_errno(&local_err, -ret, "aio failed"); |
| error_report_err(local_err); |
| |
| /* |
| * Set the command status code to the first encountered error but allow a |
| * subsequent Internal Device Error to trump it. |
| */ |
| if (req->status && status != NVME_INTERNAL_DEV_ERROR) { |
| return; |
| } |
| |
| req->status = status; |
| } |
| |
| static inline uint32_t nvme_zone_idx(NvmeNamespace *ns, uint64_t slba) |
| { |
| return ns->zone_size_log2 > 0 ? slba >> ns->zone_size_log2 : |
| slba / ns->zone_size; |
| } |
| |
| static inline NvmeZone *nvme_get_zone_by_slba(NvmeNamespace *ns, uint64_t slba) |
| { |
| uint32_t zone_idx = nvme_zone_idx(ns, slba); |
| |
| if (zone_idx >= ns->num_zones) { |
| return NULL; |
| } |
| |
| return &ns->zone_array[zone_idx]; |
| } |
| |
| static uint16_t nvme_check_zone_state_for_write(NvmeZone *zone) |
| { |
| uint64_t zslba = zone->d.zslba; |
| |
| switch (nvme_get_zone_state(zone)) { |
| case NVME_ZONE_STATE_EMPTY: |
| case NVME_ZONE_STATE_IMPLICITLY_OPEN: |
| case NVME_ZONE_STATE_EXPLICITLY_OPEN: |
| case NVME_ZONE_STATE_CLOSED: |
| return NVME_SUCCESS; |
| case NVME_ZONE_STATE_FULL: |
| trace_pci_nvme_err_zone_is_full(zslba); |
| return NVME_ZONE_FULL; |
| case NVME_ZONE_STATE_OFFLINE: |
| trace_pci_nvme_err_zone_is_offline(zslba); |
| return NVME_ZONE_OFFLINE; |
| case NVME_ZONE_STATE_READ_ONLY: |
| trace_pci_nvme_err_zone_is_read_only(zslba); |
| return NVME_ZONE_READ_ONLY; |
| default: |
| assert(false); |
| } |
| |
| return NVME_INTERNAL_DEV_ERROR; |
| } |
| |
| static uint16_t nvme_check_zone_write(NvmeNamespace *ns, NvmeZone *zone, |
| uint64_t slba, uint32_t nlb) |
| { |
| uint64_t zcap = nvme_zone_wr_boundary(zone); |
| uint16_t status; |
| |
| status = nvme_check_zone_state_for_write(zone); |
| if (status) { |
| return status; |
| } |
| |
| if (zone->d.za & NVME_ZA_ZRWA_VALID) { |
| uint64_t ezrwa = zone->w_ptr + 2 * ns->zns.zrwas; |
| |
| if (slba < zone->w_ptr || slba + nlb > ezrwa) { |
| trace_pci_nvme_err_zone_invalid_write(slba, zone->w_ptr); |
| return NVME_ZONE_INVALID_WRITE; |
| } |
| } else { |
| if (unlikely(slba != zone->w_ptr)) { |
| trace_pci_nvme_err_write_not_at_wp(slba, zone->d.zslba, |
| zone->w_ptr); |
| return NVME_ZONE_INVALID_WRITE; |
| } |
| } |
| |
| if (unlikely((slba + nlb) > zcap)) { |
| trace_pci_nvme_err_zone_boundary(slba, nlb, zcap); |
| return NVME_ZONE_BOUNDARY_ERROR; |
| } |
| |
| return NVME_SUCCESS; |
| } |
| |
| static uint16_t nvme_check_zone_state_for_read(NvmeZone *zone) |
| { |
| switch (nvme_get_zone_state(zone)) { |
| case NVME_ZONE_STATE_EMPTY: |
| case NVME_ZONE_STATE_IMPLICITLY_OPEN: |
| case NVME_ZONE_STATE_EXPLICITLY_OPEN: |
| case NVME_ZONE_STATE_FULL: |
| case NVME_ZONE_STATE_CLOSED: |
| case NVME_ZONE_STATE_READ_ONLY: |
| return NVME_SUCCESS; |
| case NVME_ZONE_STATE_OFFLINE: |
| trace_pci_nvme_err_zone_is_offline(zone->d.zslba); |
| return NVME_ZONE_OFFLINE; |
| default: |
| assert(false); |
| } |
| |
| return NVME_INTERNAL_DEV_ERROR; |
| } |
| |
| static uint16_t nvme_check_zone_read(NvmeNamespace *ns, uint64_t slba, |
| uint32_t nlb) |
| { |
| NvmeZone *zone; |
| uint64_t bndry, end; |
| uint16_t status; |
| |
| zone = nvme_get_zone_by_slba(ns, slba); |
| assert(zone); |
| |
| bndry = nvme_zone_rd_boundary(ns, zone); |
| end = slba + nlb; |
| |
| status = nvme_check_zone_state_for_read(zone); |
| if (status) { |
| ; |
| } else if (unlikely(end > bndry)) { |
| if (!ns->params.cross_zone_read) { |
| status = NVME_ZONE_BOUNDARY_ERROR; |
| } else { |
| /* |
| * Read across zone boundary - check that all subsequent |
| * zones that are being read have an appropriate state. |
| */ |
| do { |
| zone++; |
| status = nvme_check_zone_state_for_read(zone); |
| if (status) { |
| break; |
| } |
| } while (end > nvme_zone_rd_boundary(ns, zone)); |
| } |
| } |
| |
| return status; |
| } |
| |
| static uint16_t nvme_zrm_finish(NvmeNamespace *ns, NvmeZone *zone) |
| { |
| switch (nvme_get_zone_state(zone)) { |
| case NVME_ZONE_STATE_FULL: |
| return NVME_SUCCESS; |
| |
| case NVME_ZONE_STATE_IMPLICITLY_OPEN: |
| case NVME_ZONE_STATE_EXPLICITLY_OPEN: |
| nvme_aor_dec_open(ns); |
| /* fallthrough */ |
| case NVME_ZONE_STATE_CLOSED: |
| nvme_aor_dec_active(ns); |
| |
| if (zone->d.za & NVME_ZA_ZRWA_VALID) { |
| zone->d.za &= ~NVME_ZA_ZRWA_VALID; |
| if (ns->params.numzrwa) { |
| ns->zns.numzrwa++; |
| } |
| } |
| |
| /* fallthrough */ |
| case NVME_ZONE_STATE_EMPTY: |
| nvme_assign_zone_state(ns, zone, NVME_ZONE_STATE_FULL); |
| return NVME_SUCCESS; |
| |
| default: |
| return NVME_ZONE_INVAL_TRANSITION; |
| } |
| } |
| |
| static uint16_t nvme_zrm_close(NvmeNamespace *ns, NvmeZone *zone) |
| { |
| switch (nvme_get_zone_state(zone)) { |
| case NVME_ZONE_STATE_EXPLICITLY_OPEN: |
| case NVME_ZONE_STATE_IMPLICITLY_OPEN: |
| nvme_aor_dec_open(ns); |
| nvme_assign_zone_state(ns, zone, NVME_ZONE_STATE_CLOSED); |
| /* fall through */ |
| case NVME_ZONE_STATE_CLOSED: |
| return NVME_SUCCESS; |
| |
| default: |
| return NVME_ZONE_INVAL_TRANSITION; |
| } |
| } |
| |
| static uint16_t nvme_zrm_reset(NvmeNamespace *ns, NvmeZone *zone) |
| { |
| switch (nvme_get_zone_state(zone)) { |
| case NVME_ZONE_STATE_EXPLICITLY_OPEN: |
| case NVME_ZONE_STATE_IMPLICITLY_OPEN: |
| nvme_aor_dec_open(ns); |
| /* fallthrough */ |
| case NVME_ZONE_STATE_CLOSED: |
| nvme_aor_dec_active(ns); |
| |
| if (zone->d.za & NVME_ZA_ZRWA_VALID) { |
| if (ns->params.numzrwa) { |
| ns->zns.numzrwa++; |
| } |
| } |
| |
| /* fallthrough */ |
| case NVME_ZONE_STATE_FULL: |
| zone->w_ptr = zone->d.zslba; |
| zone->d.wp = zone->w_ptr; |
| nvme_assign_zone_state(ns, zone, NVME_ZONE_STATE_EMPTY); |
| /* fallthrough */ |
| case NVME_ZONE_STATE_EMPTY: |
| return NVME_SUCCESS; |
| |
| default: |
| return NVME_ZONE_INVAL_TRANSITION; |
| } |
| } |
| |
| static void nvme_zrm_auto_transition_zone(NvmeNamespace *ns) |
| { |
| NvmeZone *zone; |
| |
| if (ns->params.max_open_zones && |
| ns->nr_open_zones == ns->params.max_open_zones) { |
| zone = QTAILQ_FIRST(&ns->imp_open_zones); |
| if (zone) { |
| /* |
| * Automatically close this implicitly open zone. |
| */ |
| QTAILQ_REMOVE(&ns->imp_open_zones, zone, entry); |
| nvme_zrm_close(ns, zone); |
| } |
| } |
| } |
| |
| enum { |
| NVME_ZRM_AUTO = 1 << 0, |
| NVME_ZRM_ZRWA = 1 << 1, |
| }; |
| |
| static uint16_t nvme_zrm_open_flags(NvmeCtrl *n, NvmeNamespace *ns, |
| NvmeZone *zone, int flags) |
| { |
| int act = 0; |
| uint16_t status; |
| |
| switch (nvme_get_zone_state(zone)) { |
| case NVME_ZONE_STATE_EMPTY: |
| act = 1; |
| |
| /* fallthrough */ |
| |
| case NVME_ZONE_STATE_CLOSED: |
| if (n->params.auto_transition_zones) { |
| nvme_zrm_auto_transition_zone(ns); |
| } |
| status = nvme_zns_check_resources(ns, act, 1, |
| (flags & NVME_ZRM_ZRWA) ? 1 : 0); |
| if (status) { |
| return status; |
| } |
| |
| if (act) { |
| nvme_aor_inc_active(ns); |
| } |
| |
| nvme_aor_inc_open(ns); |
| |
| if (flags & NVME_ZRM_AUTO) { |
| nvme_assign_zone_state(ns, zone, NVME_ZONE_STATE_IMPLICITLY_OPEN); |
| return NVME_SUCCESS; |
| } |
| |
| /* fallthrough */ |
| |
| case NVME_ZONE_STATE_IMPLICITLY_OPEN: |
| if (flags & NVME_ZRM_AUTO) { |
| return NVME_SUCCESS; |
| } |
| |
| nvme_assign_zone_state(ns, zone, NVME_ZONE_STATE_EXPLICITLY_OPEN); |
| |
| /* fallthrough */ |
| |
| case NVME_ZONE_STATE_EXPLICITLY_OPEN: |
| if (flags & NVME_ZRM_ZRWA) { |
| ns->zns.numzrwa--; |
| |
| zone->d.za |= NVME_ZA_ZRWA_VALID; |
| } |
| |
| return NVME_SUCCESS; |
| |
| default: |
| return NVME_ZONE_INVAL_TRANSITION; |
| } |
| } |
| |
| static inline uint16_t nvme_zrm_auto(NvmeCtrl *n, NvmeNamespace *ns, |
| NvmeZone *zone) |
| { |
| return nvme_zrm_open_flags(n, ns, zone, NVME_ZRM_AUTO); |
| } |
| |
| static void nvme_advance_zone_wp(NvmeNamespace *ns, NvmeZone *zone, |
| uint32_t nlb) |
| { |
| zone->d.wp += nlb; |
| |
| if (zone->d.wp == nvme_zone_wr_boundary(zone)) { |
| nvme_zrm_finish(ns, zone); |
| } |
| } |
| |
| static void nvme_zoned_zrwa_implicit_flush(NvmeNamespace *ns, NvmeZone *zone, |
| uint32_t nlbc) |
| { |
| uint16_t nzrwafgs = DIV_ROUND_UP(nlbc, ns->zns.zrwafg); |
| |
| nlbc = nzrwafgs * ns->zns.zrwafg; |
| |
| trace_pci_nvme_zoned_zrwa_implicit_flush(zone->d.zslba, nlbc); |
| |
| zone->w_ptr += nlbc; |
| |
| nvme_advance_zone_wp(ns, zone, nlbc); |
| } |
| |
| static void nvme_finalize_zoned_write(NvmeNamespace *ns, NvmeRequest *req) |
| { |
| NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd; |
| NvmeZone *zone; |
| uint64_t slba; |
| uint32_t nlb; |
| |
| slba = le64_to_cpu(rw->slba); |
| nlb = le16_to_cpu(rw->nlb) + 1; |
| zone = nvme_get_zone_by_slba(ns, slba); |
| assert(zone); |
| |
| if (zone->d.za & NVME_ZA_ZRWA_VALID) { |
| uint64_t ezrwa = zone->w_ptr + ns->zns.zrwas - 1; |
| uint64_t elba = slba + nlb - 1; |
| |
| if (elba > ezrwa) { |
| nvme_zoned_zrwa_implicit_flush(ns, zone, elba - ezrwa); |
| } |
| |
| return; |
| } |
| |
| nvme_advance_zone_wp(ns, zone, nlb); |
| } |
| |
| static inline bool nvme_is_write(NvmeRequest *req) |
| { |
| NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd; |
| |
| return rw->opcode == NVME_CMD_WRITE || |
| rw->opcode == NVME_CMD_ZONE_APPEND || |
| rw->opcode == NVME_CMD_WRITE_ZEROES; |
| } |
| |
| static AioContext *nvme_get_aio_context(BlockAIOCB *acb) |
| { |
| return qemu_get_aio_context(); |
| } |
| |
| static void nvme_misc_cb(void *opaque, int ret) |
| { |
| NvmeRequest *req = opaque; |
| |
| trace_pci_nvme_misc_cb(nvme_cid(req)); |
| |
| if (ret) { |
| nvme_aio_err(req, ret); |
| } |
| |
| nvme_enqueue_req_completion(nvme_cq(req), req); |
| } |
| |
| void nvme_rw_complete_cb(void *opaque, int ret) |
| { |
| NvmeRequest *req = opaque; |
| NvmeNamespace *ns = req->ns; |
| BlockBackend *blk = ns->blkconf.blk; |
| BlockAcctCookie *acct = &req->acct; |
| BlockAcctStats *stats = blk_get_stats(blk); |
| |
| trace_pci_nvme_rw_complete_cb(nvme_cid(req), blk_name(blk)); |
| |
| if (ret) { |
| block_acct_failed(stats, acct); |
| nvme_aio_err(req, ret); |
| } else { |
| block_acct_done(stats, acct); |
| } |
| |
| if (ns->params.zoned && nvme_is_write(req)) { |
| nvme_finalize_zoned_write(ns, req); |
| } |
| |
| nvme_enqueue_req_completion(nvme_cq(req), req); |
| } |
| |
| static void nvme_rw_cb(void *opaque, int ret) |
| { |
| NvmeRequest *req = opaque; |
| NvmeNamespace *ns = req->ns; |
| |
| BlockBackend *blk = ns->blkconf.blk; |
| |
| trace_pci_nvme_rw_cb(nvme_cid(req), blk_name(blk)); |
| |
| if (ret) { |
| goto out; |
| } |
| |
| if (ns->lbaf.ms) { |
| NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd; |
| uint64_t slba = le64_to_cpu(rw->slba); |
| uint32_t nlb = (uint32_t)le16_to_cpu(rw->nlb) + 1; |
| uint64_t offset = nvme_moff(ns, slba); |
| |
| if (req->cmd.opcode == NVME_CMD_WRITE_ZEROES) { |
| size_t mlen = nvme_m2b(ns, nlb); |
| |
| req->aiocb = blk_aio_pwrite_zeroes(blk, offset, mlen, |
| BDRV_REQ_MAY_UNMAP, |
| nvme_rw_complete_cb, req); |
| return; |
| } |
| |
| if (nvme_ns_ext(ns) || req->cmd.mptr) { |
| uint16_t status; |
| |
| nvme_sg_unmap(&req->sg); |
| status = nvme_map_mdata(nvme_ctrl(req), nlb, req); |
| if (status) { |
| ret = -EFAULT; |
| goto out; |
| } |
| |
| if (req->cmd.opcode == NVME_CMD_READ) { |
| return nvme_blk_read(blk, offset, nvme_rw_complete_cb, req); |
| } |
| |
| return nvme_blk_write(blk, offset, nvme_rw_complete_cb, req); |
| } |
| } |
| |
| out: |
| nvme_rw_complete_cb(req, ret); |
| } |
| |
| static void nvme_verify_cb(void *opaque, int ret) |
| { |
| NvmeBounceContext *ctx = opaque; |
| NvmeRequest *req = ctx->req; |
| NvmeNamespace *ns = req->ns; |
| BlockBackend *blk = ns->blkconf.blk; |
| BlockAcctCookie *acct = &req->acct; |
| BlockAcctStats *stats = blk_get_stats(blk); |
| NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd; |
| uint64_t slba = le64_to_cpu(rw->slba); |
| uint8_t prinfo = NVME_RW_PRINFO(le16_to_cpu(rw->control)); |
| uint16_t apptag = le16_to_cpu(rw->apptag); |
| uint16_t appmask = le16_to_cpu(rw->appmask); |
| uint64_t reftag = le32_to_cpu(rw->reftag); |
| uint64_t cdw3 = le32_to_cpu(rw->cdw3); |
| uint16_t status; |
| |
| reftag |= cdw3 << 32; |
| |
| trace_pci_nvme_verify_cb(nvme_cid(req), prinfo, apptag, appmask, reftag); |
| |
| if (ret) { |
| block_acct_failed(stats, acct); |
| nvme_aio_err(req, ret); |
| goto out; |
| } |
| |
| block_acct_done(stats, acct); |
| |
| if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) { |
| status = nvme_dif_mangle_mdata(ns, ctx->mdata.bounce, |
| ctx->mdata.iov.size, slba); |
| if (status) { |
| req->status = status; |
| goto out; |
| } |
| |
| req->status = nvme_dif_check(ns, ctx->data.bounce, ctx->data.iov.size, |
| ctx->mdata.bounce, ctx->mdata.iov.size, |
| prinfo, slba, apptag, appmask, &reftag); |
| } |
| |
| out: |
| qemu_iovec_destroy(&ctx->data.iov); |
| g_free(ctx->data.bounce); |
| |
| qemu_iovec_destroy(&ctx->mdata.iov); |
| g_free(ctx->mdata.bounce); |
| |
| g_free(ctx); |
| |
| nvme_enqueue_req_completion(nvme_cq(req), req); |
| } |
| |
| |
| static void nvme_verify_mdata_in_cb(void *opaque, int ret) |
| { |
| NvmeBounceContext *ctx = opaque; |
| NvmeRequest *req = ctx->req; |
| NvmeNamespace *ns = req->ns; |
| NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd; |
| uint64_t slba = le64_to_cpu(rw->slba); |
| uint32_t nlb = le16_to_cpu(rw->nlb) + 1; |
| size_t mlen = nvme_m2b(ns, nlb); |
| uint64_t offset = nvme_moff(ns, slba); |
| BlockBackend *blk = ns->blkconf.blk; |
| |
| trace_pci_nvme_verify_mdata_in_cb(nvme_cid(req), blk_name(blk)); |
| |
| if (ret) { |
| goto out; |
| } |
| |
| ctx->mdata.bounce = g_malloc(mlen); |
| |
| qemu_iovec_reset(&ctx->mdata.iov); |
| qemu_iovec_add(&ctx->mdata.iov, ctx->mdata.bounce, mlen); |
| |
| req->aiocb = blk_aio_preadv(blk, offset, &ctx->mdata.iov, 0, |
| nvme_verify_cb, ctx); |
| return; |
| |
| out: |
| nvme_verify_cb(ctx, ret); |
| } |
| |
| struct nvme_compare_ctx { |
| struct { |
| QEMUIOVector iov; |
| uint8_t *bounce; |
| } data; |
| |
| struct { |
| QEMUIOVector iov; |
| uint8_t *bounce; |
| } mdata; |
| }; |
| |
| static void nvme_compare_mdata_cb(void *opaque, int ret) |
| { |
| NvmeRequest *req = opaque; |
| NvmeNamespace *ns = req->ns; |
| NvmeCtrl *n = nvme_ctrl(req); |
| NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd; |
| uint8_t prinfo = NVME_RW_PRINFO(le16_to_cpu(rw->control)); |
| uint16_t apptag = le16_to_cpu(rw->apptag); |
| uint16_t appmask = le16_to_cpu(rw->appmask); |
| uint64_t reftag = le32_to_cpu(rw->reftag); |
| uint64_t cdw3 = le32_to_cpu(rw->cdw3); |
| struct nvme_compare_ctx *ctx = req->opaque; |
| g_autofree uint8_t *buf = NULL; |
| BlockBackend *blk = ns->blkconf.blk; |
| BlockAcctCookie *acct = &req->acct; |
| BlockAcctStats *stats = blk_get_stats(blk); |
| uint16_t status = NVME_SUCCESS; |
| |
| reftag |= cdw3 << 32; |
| |
| trace_pci_nvme_compare_mdata_cb(nvme_cid(req)); |
| |
| if (ret) { |
| block_acct_failed(stats, acct); |
| nvme_aio_err(req, ret); |
| goto out; |
| } |
| |
| buf = g_malloc(ctx->mdata.iov.size); |
| |
| status = nvme_bounce_mdata(n, buf, ctx->mdata.iov.size, |
| NVME_TX_DIRECTION_TO_DEVICE, req); |
| if (status) { |
| req->status = status; |
| goto out; |
| } |
| |
| if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) { |
| uint64_t slba = le64_to_cpu(rw->slba); |
| uint8_t *bufp; |
| uint8_t *mbufp = ctx->mdata.bounce; |
| uint8_t *end = mbufp + ctx->mdata.iov.size; |
| int16_t pil = 0; |
| |
| status = nvme_dif_check(ns, ctx->data.bounce, ctx->data.iov.size, |
| ctx->mdata.bounce, ctx->mdata.iov.size, prinfo, |
| slba, apptag, appmask, &reftag); |
| if (status) { |
| req->status = status; |
| goto out; |
| } |
| |
| /* |
| * When formatted with protection information, do not compare the DIF |
| * tuple. |
| */ |
| if (!(ns->id_ns.dps & NVME_ID_NS_DPS_FIRST_EIGHT)) { |
| pil = ns->lbaf.ms - nvme_pi_tuple_size(ns); |
| } |
| |
| for (bufp = buf; mbufp < end; bufp += ns->lbaf.ms, mbufp += ns->lbaf.ms) { |
| if (memcmp(bufp + pil, mbufp + pil, ns->lbaf.ms - pil)) { |
| req->status = NVME_CMP_FAILURE; |
| goto out; |
| } |
| } |
| |
| goto out; |
| } |
| |
| if (memcmp(buf, ctx->mdata.bounce, ctx->mdata.iov.size)) { |
| req->status = NVME_CMP_FAILURE; |
| goto out; |
| } |
| |
| block_acct_done(stats, acct); |
| |
| out: |
| qemu_iovec_destroy(&ctx->data.iov); |
| g_free(ctx->data.bounce); |
| |
| qemu_iovec_destroy(&ctx->mdata.iov); |
| g_free(ctx->mdata.bounce); |
| |
| g_free(ctx); |
| |
| nvme_enqueue_req_completion(nvme_cq(req), req); |
| } |
| |
| static void nvme_compare_data_cb(void *opaque, int ret) |
| { |
| NvmeRequest *req = opaque; |
| NvmeCtrl *n = nvme_ctrl(req); |
| NvmeNamespace *ns = req->ns; |
| BlockBackend *blk = ns->blkconf.blk; |
| BlockAcctCookie *acct = &req->acct; |
| BlockAcctStats *stats = blk_get_stats(blk); |
| |
| struct nvme_compare_ctx *ctx = req->opaque; |
| g_autofree uint8_t *buf = NULL; |
| uint16_t status; |
| |
| trace_pci_nvme_compare_data_cb(nvme_cid(req)); |
| |
| if (ret) { |
| block_acct_failed(stats, acct); |
| nvme_aio_err(req, ret); |
| goto out; |
| } |
| |
| buf = g_malloc(ctx->data.iov.size); |
| |
| status = nvme_bounce_data(n, buf, ctx->data.iov.size, |
| NVME_TX_DIRECTION_TO_DEVICE, req); |
| if (status) { |
| req->status = status; |
| goto out; |
| } |
| |
| if (memcmp(buf, ctx->data.bounce, ctx->data.iov.size)) { |
| req->status = NVME_CMP_FAILURE; |
| goto out; |
| } |
| |
| if (ns->lbaf.ms) { |
| NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd; |
| uint64_t slba = le64_to_cpu(rw->slba); |
| uint32_t nlb = le16_to_cpu(rw->nlb) + 1; |
| size_t mlen = nvme_m2b(ns, nlb); |
| uint64_t offset = nvme_moff(ns, slba); |
| |
| ctx->mdata.bounce = g_malloc(mlen); |
| |
| qemu_iovec_init(&ctx->mdata.iov, 1); |
| qemu_iovec_add(&ctx->mdata.iov, ctx->mdata.bounce, mlen); |
| |
| req->aiocb = blk_aio_preadv(blk, offset, &ctx->mdata.iov, 0, |
| nvme_compare_mdata_cb, req); |
| return; |
| } |
| |
| block_acct_done(stats, acct); |
| |
| out: |
| qemu_iovec_destroy(&ctx->data.iov); |
| g_free(ctx->data.bounce); |
| g_free(ctx); |
| |
| nvme_enqueue_req_completion(nvme_cq(req), req); |
| } |
| |
| typedef struct NvmeDSMAIOCB { |
| BlockAIOCB common; |
| BlockAIOCB *aiocb; |
| NvmeRequest *req; |
| QEMUBH *bh; |
| int ret; |
| |
| NvmeDsmRange *range; |
| unsigned int nr; |
| unsigned int idx; |
| } NvmeDSMAIOCB; |
| |
| static void nvme_dsm_cancel(BlockAIOCB *aiocb) |
| { |
| NvmeDSMAIOCB *iocb = container_of(aiocb, NvmeDSMAIOCB, common); |
| |
| /* break nvme_dsm_cb loop */ |
| iocb->idx = iocb->nr; |
| iocb->ret = -ECANCELED; |
| |
| if (iocb->aiocb) { |
| blk_aio_cancel_async(iocb->aiocb); |
| iocb->aiocb = NULL; |
| } else { |
| /* |
| * We only reach this if nvme_dsm_cancel() has already been called or |
| * the command ran to completion and nvme_dsm_bh is scheduled to run. |
| */ |
| assert(iocb->idx == iocb->nr); |
| } |
| } |
| |
| static const AIOCBInfo nvme_dsm_aiocb_info = { |
| .aiocb_size = sizeof(NvmeDSMAIOCB), |
| .cancel_async = nvme_dsm_cancel, |
| }; |
| |
| static void nvme_dsm_bh(void *opaque) |
| { |
| NvmeDSMAIOCB *iocb = opaque; |
| |
| iocb->common.cb(iocb->common.opaque, iocb->ret); |
| |
| qemu_bh_delete(iocb->bh); |
| iocb->bh = NULL; |
| qemu_aio_unref(iocb); |
| } |
| |
| static void nvme_dsm_cb(void *opaque, int ret); |
| |
| static void nvme_dsm_md_cb(void *opaque, int ret) |
| { |
| NvmeDSMAIOCB *iocb = opaque; |
| NvmeRequest *req = iocb->req; |
| NvmeNamespace *ns = req->ns; |
| NvmeDsmRange *range; |
| uint64_t slba; |
| uint32_t nlb; |
| |
| if (ret < 0) { |
| iocb->ret = ret; |
| goto done; |
| } |
| |
| if (!ns->lbaf.ms) { |
| nvme_dsm_cb(iocb, 0); |
| return; |
| } |
| |
| range = &iocb->range[iocb->idx - 1]; |
| slba = le64_to_cpu(range->slba); |
| nlb = le32_to_cpu(range->nlb); |
| |
| /* |
| * Check that all block were discarded (zeroed); otherwise we do not zero |
| * the metadata. |
| */ |
| |
| ret = nvme_block_status_all(ns, slba, nlb, BDRV_BLOCK_ZERO); |
| if (ret) { |
| if (ret < 0) { |
| iocb->ret = ret; |
| goto done; |
| } |
| |
| nvme_dsm_cb(iocb, 0); |
| return; |
| } |
| |
| iocb->aiocb = blk_aio_pwrite_zeroes(ns->blkconf.blk, nvme_moff(ns, slba), |
| nvme_m2b(ns, nlb), BDRV_REQ_MAY_UNMAP, |
| nvme_dsm_cb, iocb); |
| return; |
| |
| done: |
| iocb->aiocb = NULL; |
| qemu_bh_schedule(iocb->bh); |
| } |
| |
| static void nvme_dsm_cb(void *opaque, int ret) |
| { |
| NvmeDSMAIOCB *iocb = opaque; |
| NvmeRequest *req = iocb->req; |
| NvmeCtrl *n = nvme_ctrl(req); |
| NvmeNamespace *ns = req->ns; |
| NvmeDsmRange *range; |
| uint64_t slba; |
| uint32_t nlb; |
| |
| if (ret < 0) { |
| iocb->ret = ret; |
| goto done; |
| } |
| |
| next: |
| if (iocb->idx == iocb->nr) { |
| goto done; |
| } |
| |
| range = &iocb->range[iocb->idx++]; |
| slba = le64_to_cpu(range->slba); |
| nlb = le32_to_cpu(range->nlb); |
| |
| trace_pci_nvme_dsm_deallocate(slba, nlb); |
| |
| if (nlb > n->dmrsl) { |
| trace_pci_nvme_dsm_single_range_limit_exceeded(nlb, n->dmrsl); |
| goto next; |
| } |
| |
| if (nvme_check_bounds(ns, slba, nlb)) { |
| trace_pci_nvme_err_invalid_lba_range(slba, nlb, |
| ns->id_ns.nsze); |
| goto next; |
| } |
| |
| iocb->aiocb = blk_aio_pdiscard(ns->blkconf.blk, nvme_l2b(ns, slba), |
| nvme_l2b(ns, nlb), |
| nvme_dsm_md_cb, iocb); |
| return; |
| |
| done: |
| iocb->aiocb = NULL; |
| qemu_bh_schedule(iocb->bh); |
| } |
| |
| static uint16_t nvme_dsm(NvmeCtrl *n, NvmeRequest *req) |
| { |
| NvmeNamespace *ns = req->ns; |
| NvmeDsmCmd *dsm = (NvmeDsmCmd *) &req->cmd; |
| uint32_t attr = le32_to_cpu(dsm->attributes); |
| uint32_t nr = (le32_to_cpu(dsm->nr) & 0xff) + 1; |
| uint16_t status = NVME_SUCCESS; |
| |
| trace_pci_nvme_dsm(nr, attr); |
| |
| if (attr & NVME_DSMGMT_AD) { |
| NvmeDSMAIOCB *iocb = blk_aio_get(&nvme_dsm_aiocb_info, ns->blkconf.blk, |
| nvme_misc_cb, req); |
| |
| iocb->req = req; |
| iocb->bh = qemu_bh_new(nvme_dsm_bh, iocb); |
| iocb->ret = 0; |
| iocb->range = g_new(NvmeDsmRange, nr); |
| iocb->nr = nr; |
| iocb->idx = 0; |
| |
| status = nvme_h2c(n, (uint8_t *)iocb->range, sizeof(NvmeDsmRange) * nr, |
| req); |
| if (status) { |
| return status; |
| } |
| |
| req->aiocb = &iocb->common; |
| nvme_dsm_cb(iocb, 0); |
| |
| return NVME_NO_COMPLETE; |
| } |
| |
| return status; |
| } |
| |
| static uint16_t nvme_verify(NvmeCtrl *n, NvmeRequest *req) |
| { |
| NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd; |
| NvmeNamespace *ns = req->ns; |
| BlockBackend *blk = ns->blkconf.blk; |
| uint64_t slba = le64_to_cpu(rw->slba); |
| uint32_t nlb = le16_to_cpu(rw->nlb) + 1; |
| size_t len = nvme_l2b(ns, nlb); |
| int64_t offset = nvme_l2b(ns, slba); |
| uint8_t prinfo = NVME_RW_PRINFO(le16_to_cpu(rw->control)); |
| uint32_t reftag = le32_to_cpu(rw->reftag); |
| NvmeBounceContext *ctx = NULL; |
| uint16_t status; |
| |
| trace_pci_nvme_verify(nvme_cid(req), nvme_nsid(ns), slba, nlb); |
| |
| if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) { |
| status = nvme_check_prinfo(ns, prinfo, slba, reftag); |
| if (status) { |
| return status; |
| } |
| |
| if (prinfo & NVME_PRINFO_PRACT) { |
| return NVME_INVALID_PROT_INFO | NVME_DNR; |
| } |
| } |
| |
| if (len > n->page_size << n->params.vsl) { |
| return NVME_INVALID_FIELD | NVME_DNR; |
| } |
| |
| status = nvme_check_bounds(ns, slba, nlb); |
| if (status) { |
| return status; |
| } |
| |
| if (NVME_ERR_REC_DULBE(ns->features.err_rec)) { |
| status = nvme_check_dulbe(ns, slba, nlb); |
| if (status) { |
| return status; |
| } |
| } |
| |
| ctx = g_new0(NvmeBounceContext, 1); |
| ctx->req = req; |
| |
| ctx->data.bounce = g_malloc(len); |
| |
| qemu_iovec_init(&ctx->data.iov, 1); |
| qemu_iovec_add(&ctx->data.iov, ctx->data.bounce, len); |
| |
| block_acct_start(blk_get_stats(blk), &req->acct, ctx->data.iov.size, |
| BLOCK_ACCT_READ); |
| |
| req->aiocb = blk_aio_preadv(ns->blkconf.blk, offset, &ctx->data.iov, 0, |
| nvme_verify_mdata_in_cb, ctx); |
| return NVME_NO_COMPLETE; |
| } |
| |
| typedef struct NvmeCopyAIOCB { |
| BlockAIOCB common; |
| BlockAIOCB *aiocb; |
| NvmeRequest *req; |
| QEMUBH *bh; |
| int ret; |
| |
| void *ranges; |
| unsigned int format; |
| int nr; |
| int idx; |
| |
| uint8_t *bounce; |
| QEMUIOVector iov; |
| struct { |
| BlockAcctCookie read; |
| BlockAcctCookie write; |
| } acct; |
| |
| uint64_t reftag; |
| uint64_t slba; |
| |
| NvmeZone *zone; |
| } NvmeCopyAIOCB; |
| |
| static void nvme_copy_cancel(BlockAIOCB *aiocb) |
| { |
| NvmeCopyAIOCB *iocb = container_of(aiocb, NvmeCopyAIOCB, common); |
| |
| iocb->ret = -ECANCELED; |
| |
| if (iocb->aiocb) { |
| blk_aio_cancel_async(iocb->aiocb); |
| iocb->aiocb = NULL; |
| } |
| } |
| |
| static const AIOCBInfo nvme_copy_aiocb_info = { |
| .aiocb_size = sizeof(NvmeCopyAIOCB), |
| .cancel_async = nvme_copy_cancel, |
| }; |
| |
| static void nvme_copy_bh(void *opaque) |
| { |
| NvmeCopyAIOCB *iocb = opaque; |
| NvmeRequest *req = iocb->req; |
| NvmeNamespace *ns = req->ns; |
| BlockAcctStats *stats = blk_get_stats(ns->blkconf.blk); |
| |
| if (iocb->idx != iocb->nr) { |
| req->cqe.result = cpu_to_le32(iocb->idx); |
| } |
| |
| qemu_iovec_destroy(&iocb->iov); |
| g_free(iocb->bounce); |
| |
| qemu_bh_delete(iocb->bh); |
| iocb->bh = NULL; |
| |
| if (iocb->ret < 0) { |
| block_acct_failed(stats, &iocb->acct.read); |
| block_acct_failed(stats, &iocb->acct.write); |
| } else { |
| block_acct_done(stats, &iocb->acct.read); |
| block_acct_done(stats, &iocb->acct.write); |
| } |
| |
| iocb->common.cb(iocb->common.opaque, iocb->ret); |
| qemu_aio_unref(iocb); |
| } |
| |
| static void nvme_copy_cb(void *opaque, int ret); |
| |
| static void nvme_copy_source_range_parse_format0(void *ranges, int idx, |
| uint64_t *slba, uint32_t *nlb, |
| uint16_t *apptag, |
| uint16_t *appmask, |
| uint64_t *reftag) |
| { |
| NvmeCopySourceRangeFormat0 *_ranges = ranges; |
| |
| if (slba) { |
| *slba = le64_to_cpu(_ranges[idx].slba); |
| } |
| |
| if (nlb) { |
| *nlb = le16_to_cpu(_ranges[idx].nlb) + 1; |
| } |
| |
| if (apptag) { |
| *apptag = le16_to_cpu(_ranges[idx].apptag); |
| } |
| |
| if (appmask) { |
| *appmask = le16_to_cpu(_ranges[idx].appmask); |
| } |
| |
| if (reftag) { |
| *reftag = le32_to_cpu(_ranges[idx].reftag); |
| } |
| } |
| |
| static void nvme_copy_source_range_parse_format1(void *ranges, int idx, |
| uint64_t *slba, uint32_t *nlb, |
| uint16_t *apptag, |
| uint16_t *appmask, |
| uint64_t *reftag) |
| { |
| NvmeCopySourceRangeFormat1 *_ranges = ranges; |
| |
| if (slba) { |
| *slba = le64_to_cpu(_ranges[idx].slba); |
| } |
| |
| if (nlb) { |
| *nlb = le16_to_cpu(_ranges[idx].nlb) + 1; |
| } |
| |
| if (apptag) { |
| *apptag = le16_to_cpu(_ranges[idx].apptag); |
| } |
| |
| if (appmask) { |
| *appmask = le16_to_cpu(_ranges[idx].appmask); |
| } |
| |
| if (reftag) { |
| *reftag = 0; |
| |
| *reftag |= (uint64_t)_ranges[idx].sr[4] << 40; |
| *reftag |= (uint64_t)_ranges[idx].sr[5] << 32; |
| *reftag |= (uint64_t)_ranges[idx].sr[6] << 24; |
| *reftag |= (uint64_t)_ranges[idx].sr[7] << 16; |
| *reftag |= (uint64_t)_ranges[idx].sr[8] << 8; |
| *reftag |= (uint64_t)_ranges[idx].sr[9]; |
| } |
| } |
| |
| static void nvme_copy_source_range_parse(void *ranges, int idx, uint8_t format, |
| uint64_t *slba, uint32_t *nlb, |
| uint16_t *apptag, uint16_t *appmask, |
| uint64_t *reftag) |
| { |
| switch (format) { |
| case NVME_COPY_FORMAT_0: |
| nvme_copy_source_range_parse_format0(ranges, idx, slba, nlb, apptag, |
| appmask, reftag); |
| break; |
| |
| case NVME_COPY_FORMAT_1: |
| nvme_copy_source_range_parse_format1(ranges, idx, slba, nlb, apptag, |
| appmask, reftag); |
| break; |
| |
| default: |
| abort(); |
| } |
| } |
| |
| static void nvme_copy_out_completed_cb(void *opaque, int ret) |
| { |
| NvmeCopyAIOCB *iocb = opaque; |
| NvmeRequest *req = iocb->req; |
| NvmeNamespace *ns = req->ns; |
| uint32_t nlb; |
| |
| nvme_copy_source_range_parse(iocb->ranges, iocb->idx, iocb->format, NULL, |
| &nlb, NULL, NULL, NULL); |
| |
| if (ret < 0) { |
| iocb->ret = ret; |
| goto out; |
| } else if (iocb->ret < 0) { |
| goto out; |
| } |
| |
| if (ns->params.zoned) { |
| nvme_advance_zone_wp(ns, iocb->zone, nlb); |
| } |
| |
| iocb->idx++; |
| iocb->slba += nlb; |
| out: |
| nvme_copy_cb(iocb, iocb->ret); |
| } |
| |
| static void nvme_copy_out_cb(void *opaque, int ret) |
| { |
| NvmeCopyAIOCB *iocb = opaque; |
| NvmeRequest *req = iocb->req; |
| NvmeNamespace *ns = req->ns; |
| uint32_t nlb; |
| size_t mlen; |
| uint8_t *mbounce; |
| |
| if (ret < 0) { |
| iocb->ret = ret; |
| goto out; |
| } else if (iocb->ret < 0) { |
| goto out; |
| } |
| |
| if (!ns->lbaf.ms) { |
| nvme_copy_out_completed_cb(iocb, 0); |
| return; |
| } |
| |
| nvme_copy_source_range_parse(iocb->ranges, iocb->idx, iocb->format, NULL, |
| &nlb, NULL, NULL, NULL); |
| |
| mlen = nvme_m2b(ns, nlb); |
| mbounce = iocb->bounce + nvme_l2b(ns, nlb); |
| |
| qemu_iovec_reset(&iocb->iov); |
| qemu_iovec_add(&iocb->iov, mbounce, mlen); |
| |
| iocb->aiocb = blk_aio_pwritev(ns->blkconf.blk, nvme_moff(ns, iocb->slba), |
| &iocb->iov, 0, nvme_copy_out_completed_cb, |
| iocb); |
| |
| return; |
| |
| out: |
| nvme_copy_cb(iocb, ret); |
| } |
| |
| static void nvme_copy_in_completed_cb(void *opaque, int ret) |
| { |
| NvmeCopyAIOCB *iocb = opaque; |
| NvmeRequest *req = iocb->req; |
| NvmeNamespace *ns = req->ns; |
| uint32_t nlb; |
| uint64_t slba; |
| uint16_t apptag, appmask; |
| uint64_t reftag; |
| size_t len; |
| uint16_t status; |
| |
| if (ret < 0) { |
| iocb->ret = ret; |
| goto out; |
| } else if (iocb->ret < 0) { |
| goto out; |
| } |
| |
| nvme_copy_source_range_parse(iocb->ranges, iocb->idx, iocb->format, &slba, |
| &nlb, &apptag, &appmask, &reftag); |
| len = nvme_l2b(ns, nlb); |
| |
| trace_pci_nvme_copy_out(iocb->slba, nlb); |
| |
| if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) { |
| NvmeCopyCmd *copy = (NvmeCopyCmd *)&req->cmd; |
| |
| uint16_t prinfor = ((copy->control[0] >> 4) & 0xf); |
| uint16_t prinfow = ((copy->control[2] >> 2) & 0xf); |
| |
| size_t mlen = nvme_m2b(ns, nlb); |
| uint8_t *mbounce = iocb->bounce + nvme_l2b(ns, nlb); |
| |
| status = nvme_dif_mangle_mdata(ns, mbounce, mlen, slba); |
| if (status) { |
| goto invalid; |
| } |
| status = nvme_dif_check(ns, iocb->bounce, len, mbounce, mlen, prinfor, |
| slba, apptag, appmask, &reftag); |
| if (status) { |
| goto invalid; |
| } |
| |
| apptag = le16_to_cpu(copy->apptag); |
| appmask = le16_to_cpu(copy->appmask); |
| |
| if (prinfow & NVME_PRINFO_PRACT) { |
| status = nvme_check_prinfo(ns, prinfow, iocb->slba, iocb->reftag); |
| if (status) { |
| goto invalid; |
| } |
| |
| nvme_dif_pract_generate_dif(ns, iocb->bounce, len, mbounce, mlen, |
| apptag, &iocb->reftag); |
| } else { |
| status = nvme_dif_check(ns, iocb->bounce, len, mbounce, mlen, |
| prinfow, iocb->slba, apptag, appmask, |
| &iocb->reftag); |
| if (status) { |
| goto invalid; |
| } |
| } |
| } |
| |
| status = nvme_check_bounds(ns, iocb->slba, nlb); |
| if (status) { |
| goto invalid; |
| } |
| |
| if (ns->params.zoned) { |
| status = nvme_check_zone_write(ns, iocb->zone, iocb->slba, nlb); |
| if (status) { |
| goto invalid; |
| } |
| |
| if (!(iocb->zone->d.za & NVME_ZA_ZRWA_VALID)) { |
| iocb->zone->w_ptr += nlb; |
| } |
| } |
| |
| qemu_iovec_reset(&iocb->iov); |
| qemu_iovec_add(&iocb->iov, iocb->bounce, len); |
| |
| iocb->aiocb = blk_aio_pwritev(ns->blkconf.blk, nvme_l2b(ns, iocb->slba), |
| &iocb->iov, 0, nvme_copy_out_cb, iocb); |
| |
| return; |
| |
| invalid: |
| req->status = status; |
| iocb->aiocb = NULL; |
| if (iocb->bh) { |
| qemu_bh_schedule(iocb->bh); |
| } |
| |
| return; |
| |
| out: |
| nvme_copy_cb(iocb, ret); |
| } |
| |
| static void nvme_copy_in_cb(void *opaque, int ret) |
| { |
| NvmeCopyAIOCB *iocb = opaque; |
| NvmeRequest *req = iocb->req; |
| NvmeNamespace *ns = req->ns; |
| uint64_t slba; |
| uint32_t nlb; |
| |
| if (ret < 0) { |
| iocb->ret = ret; |
| goto out; |
| } else if (iocb->ret < 0) { |
| goto out; |
| } |
| |
| if (!ns->lbaf.ms) { |
| nvme_copy_in_completed_cb(iocb, 0); |
| return; |
| } |
| |
| nvme_copy_source_range_parse(iocb->ranges, iocb->idx, iocb->format, &slba, |
| &nlb, NULL, NULL, NULL); |
| |
| qemu_iovec_reset(&iocb->iov); |
| qemu_iovec_add(&iocb->iov, iocb->bounce + nvme_l2b(ns, nlb), |
| nvme_m2b(ns, nlb)); |
| |
| iocb->aiocb = blk_aio_preadv(ns->blkconf.blk, nvme_moff(ns, slba), |
| &iocb->iov, 0, nvme_copy_in_completed_cb, |
| iocb); |
| return; |
| |
| out: |
| nvme_copy_cb(iocb, iocb->ret); |
| } |
| |
| static void nvme_copy_cb(void *opaque, int ret) |
| { |
| NvmeCopyAIOCB *iocb = opaque; |
| NvmeRequest *req = iocb->req; |
| NvmeNamespace *ns = req->ns; |
| uint64_t slba; |
| uint32_t nlb; |
| size_t len; |
| uint16_t status; |
| |
| if (ret < 0) { |
| iocb->ret = ret; |
| goto done; |
| } else if (iocb->ret < 0) { |
| goto done; |
| } |
| |
| if (iocb->idx == iocb->nr) { |
| goto done; |
| } |
| |
| nvme_copy_source_range_parse(iocb->ranges, iocb->idx, iocb->format, &slba, |
| &nlb, NULL, NULL, NULL); |
| len = nvme_l2b(ns, nlb); |
| |
| trace_pci_nvme_copy_source_range(slba, nlb); |
| |
| if (nlb > le16_to_cpu(ns->id_ns.mssrl)) { |
| status = NVME_CMD_SIZE_LIMIT | NVME_DNR; |
| goto invalid; |
| } |
| |
| status = nvme_check_bounds(ns, slba, nlb); |
| if (status) { |
| goto invalid
|