2013-06-04 19:17:10 +04:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-05-16 22:10:59 +03:00
|
|
|
* Reference Specs: http://www.nvmexpress.org, 1.2, 1.1, 1.0e
|
2013-06-04 19:17:10 +04:00
|
|
|
*
|
2020-06-30 14:04:26 +03:00
|
|
|
* https://nvmexpress.org/developers/nvme-specification/
|
2013-06-04 19:17:10 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Usage: add options:
|
|
|
|
* -drive file=<file>,if=none,id=<drive_id>
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
* -device nvme,serial=<serial>,id=<bus_name>, \
|
2018-06-26 04:44:56 +03:00
|
|
|
* cmb_size_mb=<cmb_size_mb[optional]>, \
|
2020-03-30 19:46:56 +03:00
|
|
|
* [pmrdev=<mem_backend_file_id>,] \
|
2020-07-06 09:12:53 +03:00
|
|
|
* max_ioqpairs=<N[optional]>, \
|
2020-02-23 19:38:22 +03:00
|
|
|
* aerl=<N[optional]>, aer_max_queued=<N[optional]>, \
|
|
|
|
* mdts=<N[optional]>
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
* -device nvme-ns,drive=<drive_id>,bus=bus_name,nsid=<nsid>
|
2017-05-16 22:10:59 +03:00
|
|
|
*
|
|
|
|
* Note cmb_size_mb denotes size of CMB in MB. CMB is assumed to be at
|
2017-06-13 13:08:35 +03:00
|
|
|
* offset 0 in BAR2 and supports only WDS, RDS and SQS for now.
|
2020-03-30 19:46:56 +03:00
|
|
|
*
|
|
|
|
* cmb_size_mb= and pmrdev= options are mutually exclusive due to limitation
|
|
|
|
* in available BAR's. cmb_size_mb= will take precedence over pmrdev= when
|
|
|
|
* both provided.
|
|
|
|
* 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>
|
2020-07-06 09:12:53 +03:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* nvme device parameters
|
|
|
|
* ~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
* - `aerl`
|
|
|
|
* The Asynchronous Event Request Limit (AERL). Indicates the maximum number
|
|
|
|
* of concurrently outstanding Asynchronous Event Request commands suppoert
|
|
|
|
* 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 oustanding AERs. When the maximum number of
|
|
|
|
* enqueued events are reached, subsequent events will be dropped.
|
|
|
|
*
|
2013-06-04 19:17:10 +04:00
|
|
|
*/
|
|
|
|
|
2016-01-18 21:01:42 +03:00
|
|
|
#include "qemu/osdep.h"
|
2018-06-25 15:42:05 +03:00
|
|
|
#include "qemu/units.h"
|
2020-06-09 22:03:19 +03:00
|
|
|
#include "qemu/error-report.h"
|
2016-06-22 20:11:19 +03:00
|
|
|
#include "hw/block/block.h"
|
|
|
|
#include "hw/pci/msix.h"
|
|
|
|
#include "hw/pci/pci.h"
|
2019-08-12 08:23:51 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2019-08-12 08:23:45 +03:00
|
|
|
#include "migration/vmstate.h"
|
2014-10-07 12:00:34 +04:00
|
|
|
#include "sysemu/sysemu.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 11:01:28 +03:00
|
|
|
#include "qapi/error.h"
|
2014-10-07 12:00:34 +04:00
|
|
|
#include "qapi/visitor.h"
|
2020-03-30 19:46:56 +03:00
|
|
|
#include "sysemu/hostmem.h"
|
2014-10-07 15:59:18 +04:00
|
|
|
#include "sysemu/block-backend.h"
|
2020-05-08 09:24:55 +03:00
|
|
|
#include "exec/memory.h"
|
2017-11-03 16:37:53 +03:00
|
|
|
#include "qemu/log.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2018-05-29 02:27:13 +03:00
|
|
|
#include "qemu/cutils.h"
|
2017-11-03 16:37:53 +03:00
|
|
|
#include "trace.h"
|
2013-06-04 19:17:10 +04:00
|
|
|
#include "nvme.h"
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
#include "nvme-ns.h"
|
2013-06-04 19:17:10 +04:00
|
|
|
|
2020-06-09 22:03:32 +03:00
|
|
|
#define NVME_MAX_IOQPAIRS 0xffff
|
2020-06-09 22:03:12 +03:00
|
|
|
#define NVME_DB_SIZE 4
|
2020-07-06 09:13:03 +03:00
|
|
|
#define NVME_SPEC_VER 0x00010300
|
2020-06-09 22:03:27 +03:00
|
|
|
#define NVME_CMB_BIR 2
|
2020-06-09 22:03:28 +03:00
|
|
|
#define NVME_PMR_BIR 2
|
2020-07-06 09:12:50 +03:00
|
|
|
#define NVME_TEMPERATURE 0x143
|
|
|
|
#define NVME_TEMPERATURE_WARNING 0x157
|
|
|
|
#define NVME_TEMPERATURE_CRITICAL 0x175
|
2020-07-06 09:12:51 +03:00
|
|
|
#define NVME_NUM_FW_SLOTS 1
|
2020-06-09 22:03:12 +03:00
|
|
|
|
2017-11-03 16:37:53 +03:00
|
|
|
#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)
|
|
|
|
|
2020-07-06 09:12:56 +03:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2020-07-06 09:12:57 +03:00
|
|
|
static const uint32_t nvme_feature_cap[NVME_FID_MAX] = {
|
|
|
|
[NVME_TEMPERATURE_THRESHOLD] = NVME_FEAT_CAP_CHANGE,
|
|
|
|
[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,
|
|
|
|
};
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
static void nvme_process_sq(void *opaque);
|
|
|
|
|
2020-07-06 09:12:48 +03:00
|
|
|
static uint16_t nvme_cid(NvmeRequest *req)
|
|
|
|
{
|
|
|
|
if (!req) {
|
|
|
|
return 0xffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
return le16_to_cpu(req->cqe.cid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t nvme_sqid(NvmeRequest *req)
|
|
|
|
{
|
|
|
|
return le16_to_cpu(req->sq->sqid);
|
|
|
|
}
|
|
|
|
|
2020-06-09 22:03:17 +03:00
|
|
|
static bool nvme_addr_is_cmb(NvmeCtrl *n, hwaddr addr)
|
|
|
|
{
|
|
|
|
hwaddr low = n->ctrl_mem.addr;
|
|
|
|
hwaddr hi = n->ctrl_mem.addr + int128_get64(n->ctrl_mem.size);
|
|
|
|
|
|
|
|
return addr >= low && addr < hi;
|
|
|
|
}
|
|
|
|
|
2020-02-23 16:21:52 +03:00
|
|
|
static inline void *nvme_addr_to_cmb(NvmeCtrl *n, hwaddr addr)
|
|
|
|
{
|
|
|
|
assert(nvme_addr_is_cmb(n, addr));
|
|
|
|
|
|
|
|
return &n->cmbuf[addr - n->ctrl_mem.addr];
|
|
|
|
}
|
|
|
|
|
2019-10-11 09:32:00 +03:00
|
|
|
static int nvme_addr_read(NvmeCtrl *n, hwaddr addr, void *buf, int size)
|
2017-05-16 22:10:59 +03:00
|
|
|
{
|
2020-03-31 00:23:15 +03:00
|
|
|
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)) {
|
2020-02-23 16:21:52 +03:00
|
|
|
memcpy(buf, nvme_addr_to_cmb(n, addr), size);
|
2019-10-11 09:32:00 +03:00
|
|
|
return 0;
|
2017-05-16 22:10:59 +03:00
|
|
|
}
|
2020-06-09 22:03:17 +03:00
|
|
|
|
2019-10-11 09:32:00 +03:00
|
|
|
return pci_dma_read(&n->parent_obj, addr, buf, size);
|
2017-05-16 22:10:59 +03:00
|
|
|
}
|
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
static bool nvme_nsid_valid(NvmeCtrl *n, uint32_t nsid)
|
|
|
|
{
|
|
|
|
return nsid && (nsid == NVME_NSID_BROADCAST || nsid <= n->num_namespaces);
|
|
|
|
}
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
static int nvme_check_sqid(NvmeCtrl *n, uint16_t sqid)
|
|
|
|
{
|
2020-06-09 22:03:19 +03:00
|
|
|
return sqid < n->params.max_ioqpairs + 1 && n->sq[sqid] != NULL ? 0 : -1;
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int nvme_check_cqid(NvmeCtrl *n, uint16_t cqid)
|
|
|
|
{
|
2020-06-09 22:03:19 +03:00
|
|
|
return cqid < n->params.max_ioqpairs + 1 && n->cq[cqid] != NULL ? 0 : -1;
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-12-18 08:00:43 +03:00
|
|
|
static void nvme_irq_check(NvmeCtrl *n)
|
|
|
|
{
|
|
|
|
if (msix_enabled(&(n->parent_obj))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (~n->bar.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)
|
2013-06-04 19:17:10 +04:00
|
|
|
{
|
|
|
|
if (cq->irq_enabled) {
|
|
|
|
if (msix_enabled(&(n->parent_obj))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_irq_msix(cq->vector);
|
2013-06-04 19:17:10 +04:00
|
|
|
msix_notify(&(n->parent_obj), cq->vector);
|
|
|
|
} else {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_irq_pin();
|
2020-06-09 22:03:18 +03:00
|
|
|
assert(cq->vector < 32);
|
|
|
|
n->irq_status |= 1 << cq->vector;
|
2017-12-18 08:00:43 +03:00
|
|
|
nvme_irq_check(n);
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
2017-11-03 16:37:53 +03:00
|
|
|
} else {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_irq_masked();
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 08:00:43 +03:00
|
|
|
static void nvme_irq_deassert(NvmeCtrl *n, NvmeCQueue *cq)
|
|
|
|
{
|
|
|
|
if (cq->irq_enabled) {
|
|
|
|
if (msix_enabled(&(n->parent_obj))) {
|
|
|
|
return;
|
|
|
|
} else {
|
2020-06-09 22:03:18 +03:00
|
|
|
assert(cq->vector < 32);
|
|
|
|
n->irq_status &= ~(1 << cq->vector);
|
2017-12-18 08:00:43 +03:00
|
|
|
nvme_irq_check(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static void nvme_req_clear(NvmeRequest *req)
|
|
|
|
{
|
|
|
|
req->ns = NULL;
|
|
|
|
memset(&req->cqe, 0x0, sizeof(req->cqe));
|
2020-08-24 14:32:06 +03:00
|
|
|
req->status = NVME_SUCCESS;
|
2020-07-20 13:44:01 +03:00
|
|
|
}
|
|
|
|
|
2020-06-29 11:04:10 +03:00
|
|
|
static void nvme_req_exit(NvmeRequest *req)
|
|
|
|
{
|
|
|
|
if (req->qsg.sg) {
|
|
|
|
qemu_sglist_destroy(&req->qsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req->iov.iov) {
|
|
|
|
qemu_iovec_destroy(&req->iov);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 16:21:52 +03:00
|
|
|
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(NvmeCtrl *n, QEMUSGList *qsg, QEMUIOVector *iov,
|
|
|
|
hwaddr addr, size_t len)
|
|
|
|
{
|
|
|
|
if (!len) {
|
|
|
|
return NVME_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace_pci_nvme_map_addr(addr, len);
|
|
|
|
|
|
|
|
if (nvme_addr_is_cmb(n, addr)) {
|
|
|
|
if (qsg && qsg->sg) {
|
|
|
|
return NVME_INVALID_USE_OF_CMB | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(iov);
|
|
|
|
|
|
|
|
if (!iov->iov) {
|
|
|
|
qemu_iovec_init(iov, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nvme_map_addr_cmb(n, iov, addr, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iov && iov->iov) {
|
|
|
|
return NVME_INVALID_USE_OF_CMB | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(qsg);
|
|
|
|
|
|
|
|
if (!qsg->sg) {
|
|
|
|
pci_dma_sglist_init(qsg, &n->parent_obj, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
qemu_sglist_add(qsg, addr, len);
|
|
|
|
|
|
|
|
return NVME_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-07-29 22:55:37 +03:00
|
|
|
static uint16_t nvme_map_prp(NvmeCtrl *n, uint64_t prp1, uint64_t prp2,
|
|
|
|
uint32_t len, NvmeRequest *req)
|
2013-06-04 19:17:10 +04:00
|
|
|
{
|
|
|
|
hwaddr trans_len = n->page_size - (prp1 % n->page_size);
|
|
|
|
trans_len = MIN(len, trans_len);
|
|
|
|
int num_prps = (len >> n->page_bits) + 1;
|
2020-02-23 16:21:52 +03:00
|
|
|
uint16_t status;
|
2020-02-23 18:12:12 +03:00
|
|
|
bool prp_list_in_cmb = false;
|
2019-10-11 09:32:00 +03:00
|
|
|
int ret;
|
2013-06-04 19:17:10 +04:00
|
|
|
|
2020-07-29 22:55:37 +03:00
|
|
|
QEMUSGList *qsg = &req->qsg;
|
|
|
|
QEMUIOVector *iov = &req->iov;
|
|
|
|
|
2020-07-29 22:18:34 +03:00
|
|
|
trace_pci_nvme_map_prp(trans_len, len, prp1, prp2, num_prps);
|
|
|
|
|
2020-02-23 16:21:52 +03:00
|
|
|
if (nvme_addr_is_cmb(n, prp1)) {
|
2017-06-13 13:08:35 +03:00
|
|
|
qemu_iovec_init(iov, num_prps);
|
|
|
|
} else {
|
|
|
|
pci_dma_sglist_init(qsg, &n->parent_obj, num_prps);
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
2020-02-23 16:21:52 +03:00
|
|
|
|
|
|
|
status = nvme_map_addr(n, qsg, iov, prp1, trans_len);
|
|
|
|
if (status) {
|
2020-06-29 11:04:10 +03:00
|
|
|
return status;
|
2020-02-23 16:21:52 +03:00
|
|
|
}
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
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;
|
|
|
|
|
2020-02-23 18:12:12 +03:00
|
|
|
if (nvme_addr_is_cmb(n, prp2)) {
|
|
|
|
prp_list_in_cmb = true;
|
|
|
|
}
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
nents = (len + n->page_size - 1) >> n->page_bits;
|
|
|
|
prp_trans = MIN(n->max_prp_ents, nents) * sizeof(uint64_t);
|
2019-10-11 09:32:00 +03:00
|
|
|
ret = nvme_addr_read(n, prp2, (void *)prp_list, prp_trans);
|
|
|
|
if (ret) {
|
|
|
|
trace_pci_nvme_err_addr_read(prp2);
|
|
|
|
return NVME_DATA_TRAS_ERROR;
|
|
|
|
}
|
2013-06-04 19:17:10 +04:00
|
|
|
while (len != 0) {
|
|
|
|
uint64_t prp_ent = le64_to_cpu(prp_list[i]);
|
|
|
|
|
|
|
|
if (i == n->max_prp_ents - 1 && len > n->page_size) {
|
2020-10-19 10:11:31 +03:00
|
|
|
if (unlikely(prp_ent & (n->page_size - 1))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_invalid_prplist_ent(prp_ent);
|
2020-10-19 10:11:31 +03:00
|
|
|
return NVME_INVALID_PRP_OFFSET | NVME_DNR;
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
|
|
|
|
2020-02-23 18:12:12 +03:00
|
|
|
if (prp_list_in_cmb != nvme_addr_is_cmb(n, prp_ent)) {
|
2020-06-29 11:04:10 +03:00
|
|
|
return NVME_INVALID_USE_OF_CMB | NVME_DNR;
|
2020-02-23 18:12:12 +03:00
|
|
|
}
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
i = 0;
|
|
|
|
nents = (len + n->page_size - 1) >> n->page_bits;
|
|
|
|
prp_trans = MIN(n->max_prp_ents, nents) * sizeof(uint64_t);
|
2019-10-11 09:32:00 +03:00
|
|
|
ret = nvme_addr_read(n, prp_ent, (void *)prp_list,
|
|
|
|
prp_trans);
|
|
|
|
if (ret) {
|
|
|
|
trace_pci_nvme_err_addr_read(prp_ent);
|
|
|
|
return NVME_DATA_TRAS_ERROR;
|
|
|
|
}
|
2013-06-04 19:17:10 +04:00
|
|
|
prp_ent = le64_to_cpu(prp_list[i]);
|
|
|
|
}
|
|
|
|
|
2020-10-19 10:11:31 +03:00
|
|
|
if (unlikely(prp_ent & (n->page_size - 1))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_invalid_prplist_ent(prp_ent);
|
2020-10-19 10:11:31 +03:00
|
|
|
return NVME_INVALID_PRP_OFFSET | NVME_DNR;
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
trans_len = MIN(len, n->page_size);
|
2020-02-23 16:21:52 +03:00
|
|
|
status = nvme_map_addr(n, qsg, iov, prp_ent, trans_len);
|
|
|
|
if (status) {
|
2020-06-29 11:04:10 +03:00
|
|
|
return status;
|
2017-06-13 13:08:35 +03:00
|
|
|
}
|
2020-02-23 18:12:12 +03:00
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
len -= trans_len;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} else {
|
2017-11-03 16:37:53 +03:00
|
|
|
if (unlikely(prp2 & (n->page_size - 1))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_invalid_prp2_align(prp2);
|
2020-10-19 10:11:31 +03:00
|
|
|
return NVME_INVALID_PRP_OFFSET | NVME_DNR;
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
2020-02-23 16:21:52 +03:00
|
|
|
status = nvme_map_addr(n, qsg, iov, prp2, len);
|
|
|
|
if (status) {
|
2020-06-29 11:04:10 +03:00
|
|
|
return status;
|
2017-06-13 13:08:35 +03:00
|
|
|
}
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 11:04:10 +03:00
|
|
|
return NVME_SUCCESS;
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
|
|
|
|
2019-04-12 21:53:16 +03:00
|
|
|
/*
|
|
|
|
* 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, QEMUSGList *qsg,
|
|
|
|
QEMUIOVector *iov,
|
|
|
|
NvmeSglDescriptor *segment, uint64_t nsgld,
|
|
|
|
size_t *len, NvmeRequest *req)
|
|
|
|
{
|
|
|
|
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) {
|
2020-03-18 11:41:19 +03:00
|
|
|
case NVME_SGL_DESCR_TYPE_BIT_BUCKET:
|
|
|
|
if (req->cmd.opcode == NVME_CMD_WRITE) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-04-12 21:53:16 +03:00
|
|
|
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);
|
2020-03-18 11:41:19 +03:00
|
|
|
|
2019-04-12 21:53:16 +03:00
|
|
|
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.
|
|
|
|
*/
|
2020-11-04 13:22:47 +03:00
|
|
|
uint32_t sgls = le32_to_cpu(n->id_ctrl.sgls);
|
2019-04-12 21:53:16 +03:00
|
|
|
if (sgls & NVME_CTRL_SGLS_EXCESS_LENGTH) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace_pci_nvme_err_invalid_sgl_excess_length(nvme_cid(req));
|
|
|
|
return NVME_DATA_SGL_LEN_INVALID | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
|
|
|
trans_len = MIN(*len, dlen);
|
2020-03-18 11:41:19 +03:00
|
|
|
|
|
|
|
if (type == NVME_SGL_DESCR_TYPE_BIT_BUCKET) {
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
2019-04-12 21:53:16 +03:00
|
|
|
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, qsg, iov, addr, trans_len);
|
|
|
|
if (status) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2020-03-18 11:41:19 +03:00
|
|
|
next:
|
2019-04-12 21:53:16 +03:00
|
|
|
*len -= trans_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NVME_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t nvme_map_sgl(NvmeCtrl *n, QEMUSGList *qsg, QEMUIOVector *iov,
|
|
|
|
NvmeSglDescriptor sgl, size_t len,
|
2020-02-23 20:34:34 +03:00
|
|
|
NvmeRequest *req)
|
2019-04-12 21:53:16 +03:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
bool sgl_in_cmb = false;
|
|
|
|
hwaddr addr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
sgld = &sgl;
|
|
|
|
addr = le64_to_cpu(sgl.addr);
|
|
|
|
|
|
|
|
trace_pci_nvme_map_sgl(nvme_cid(req), NVME_SGL_TYPE(sgl.type), len);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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, qsg, iov, sgld, 1, &len, req);
|
|
|
|
if (status) {
|
|
|
|
goto unmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the segment is located in the CMB, the submission queue of the
|
|
|
|
* request must also reside there.
|
|
|
|
*/
|
|
|
|
if (nvme_addr_is_cmb(n, addr)) {
|
|
|
|
if (!nvme_addr_is_cmb(n, req->sq->dma_addr)) {
|
|
|
|
return NVME_INVALID_USE_OF_CMB | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
|
|
|
sgl_in_cmb = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
2020-03-18 11:41:19 +03:00
|
|
|
if ((!seg_len || seg_len & 0xf) &&
|
|
|
|
(NVME_SGL_TYPE(sgld->type) != NVME_SGL_DESCR_TYPE_BIT_BUCKET)) {
|
2019-04-12 21:53:16 +03:00
|
|
|
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, qsg, iov, segment, SEG_CHUNK_SIZE,
|
|
|
|
&len, req);
|
|
|
|
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];
|
|
|
|
|
2020-03-18 11:41:19 +03:00
|
|
|
/*
|
|
|
|
* If the segment ends with a Data Block or Bit Bucket Descriptor Type,
|
|
|
|
* then we are done.
|
|
|
|
*/
|
|
|
|
switch (NVME_SGL_TYPE(last_sgld->type)) {
|
|
|
|
case NVME_SGL_DESCR_TYPE_DATA_BLOCK:
|
|
|
|
case NVME_SGL_DESCR_TYPE_BIT_BUCKET:
|
2019-04-12 21:53:16 +03:00
|
|
|
status = nvme_map_sgl_data(n, qsg, iov, segment, nsgld, &len, req);
|
|
|
|
if (status) {
|
|
|
|
goto unmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto out;
|
2020-03-18 11:41:19 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2019-04-12 21:53:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-03-18 11:41:19 +03:00
|
|
|
* If the last descriptor was not a Data Block or Bit Bucket, then the
|
|
|
|
* current segment must not be a Last Segment.
|
2019-04-12 21:53:16 +03:00
|
|
|
*/
|
|
|
|
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, qsg, iov, segment, nsgld - 1, &len, req);
|
|
|
|
if (status) {
|
|
|
|
goto unmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the next segment is in the CMB, make sure that the sgl was
|
|
|
|
* already located there.
|
|
|
|
*/
|
|
|
|
if (sgl_in_cmb != nvme_addr_is_cmb(n, addr)) {
|
|
|
|
status = NVME_INVALID_USE_OF_CMB | NVME_DNR;
|
|
|
|
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:
|
|
|
|
if (iov->iov) {
|
|
|
|
qemu_iovec_destroy(iov);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (qsg->sg) {
|
|
|
|
qemu_sglist_destroy(qsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t nvme_map_dptr(NvmeCtrl *n, size_t len, NvmeRequest *req)
|
|
|
|
{
|
|
|
|
uint64_t prp1, prp2;
|
|
|
|
|
|
|
|
switch (NVME_CMD_FLAGS_PSDT(req->cmd.flags)) {
|
|
|
|
case NVME_PSDT_PRP:
|
|
|
|
prp1 = le64_to_cpu(req->cmd.dptr.prp1);
|
|
|
|
prp2 = le64_to_cpu(req->cmd.dptr.prp2);
|
|
|
|
|
|
|
|
return nvme_map_prp(n, prp1, prp2, len, req);
|
|
|
|
case NVME_PSDT_SGL_MPTR_CONTIGUOUS:
|
|
|
|
case NVME_PSDT_SGL_MPTR_SGL:
|
|
|
|
/* SGLs shall not be used for Admin commands in NVMe over PCIe */
|
|
|
|
if (!req->sq->sqid) {
|
|
|
|
return NVME_INVALID_FIELD | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nvme_map_sgl(n, &req->qsg, &req->iov, req->cmd.dptr.sgl, len,
|
|
|
|
req);
|
|
|
|
default:
|
|
|
|
return NVME_INVALID_FIELD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t nvme_dma(NvmeCtrl *n, uint8_t *ptr, uint32_t len,
|
|
|
|
DMADirection dir, NvmeRequest *req)
|
2019-05-20 20:40:30 +03:00
|
|
|
{
|
|
|
|
uint16_t status = NVME_SUCCESS;
|
|
|
|
|
2019-04-12 21:53:16 +03:00
|
|
|
status = nvme_map_dptr(n, len, req);
|
2020-02-23 18:03:34 +03:00
|
|
|
if (status) {
|
|
|
|
return status;
|
2019-05-20 20:40:30 +03:00
|
|
|
}
|
|
|
|
|
2020-02-23 18:03:34 +03:00
|
|
|
/* assert that only one of qsg and iov carries data */
|
2020-02-23 20:34:34 +03:00
|
|
|
assert((req->qsg.nsg > 0) != (req->iov.niov > 0));
|
2017-11-03 16:37:53 +03:00
|
|
|
|
2020-02-23 20:34:34 +03:00
|
|
|
if (req->qsg.nsg > 0) {
|
2020-02-23 18:03:34 +03:00
|
|
|
uint64_t residual;
|
|
|
|
|
|
|
|
if (dir == DMA_DIRECTION_TO_DEVICE) {
|
2020-02-23 20:34:34 +03:00
|
|
|
residual = dma_buf_write(ptr, len, &req->qsg);
|
2020-02-23 18:03:34 +03:00
|
|
|
} else {
|
2020-02-23 20:34:34 +03:00
|
|
|
residual = dma_buf_read(ptr, len, &req->qsg);
|
2020-02-23 18:03:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely(residual)) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_invalid_dma();
|
2017-06-13 13:08:35 +03:00
|
|
|
status = NVME_INVALID_FIELD | NVME_DNR;
|
|
|
|
}
|
|
|
|
} else {
|
2020-02-23 18:03:34 +03:00
|
|
|
size_t bytes;
|
|
|
|
|
|
|
|
if (dir == DMA_DIRECTION_TO_DEVICE) {
|
2020-02-23 20:34:34 +03:00
|
|
|
bytes = qemu_iovec_to_buf(&req->iov, 0, ptr, len);
|
2020-02-23 18:03:34 +03:00
|
|
|
} else {
|
2020-02-23 20:34:34 +03:00
|
|
|
bytes = qemu_iovec_from_buf(&req->iov, 0, ptr, len);
|
2020-02-23 18:03:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely(bytes != len)) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_invalid_dma();
|
2017-06-13 13:08:35 +03:00
|
|
|
status = NVME_INVALID_FIELD | NVME_DNR;
|
|
|
|
}
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
2020-02-23 18:03:34 +03:00
|
|
|
|
2017-06-13 13:08:35 +03:00
|
|
|
return status;
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void nvme_post_cqes(void *opaque)
|
|
|
|
{
|
|
|
|
NvmeCQueue *cq = opaque;
|
|
|
|
NvmeCtrl *n = cq->ctrl;
|
|
|
|
NvmeRequest *req, *next;
|
2019-10-11 09:32:00 +03:00
|
|
|
int ret;
|
2013-06-04 19:17:10 +04:00
|
|
|
|
|
|
|
QTAILQ_FOREACH_SAFE(req, &cq->req_list, entry, next) {
|
|
|
|
NvmeSQueue *sq;
|
|
|
|
hwaddr addr;
|
|
|
|
|
|
|
|
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;
|
2019-10-11 09:32:00 +03:00
|
|
|
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();
|
|
|
|
n->bar.csts = NVME_CSTS_FAILED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
QTAILQ_REMOVE(&cq->req_list, req, entry);
|
2013-06-04 19:17:10 +04:00
|
|
|
nvme_inc_cq_tail(cq);
|
2020-06-29 11:04:10 +03:00
|
|
|
nvme_req_exit(req);
|
2013-06-04 19:17:10 +04:00
|
|
|
QTAILQ_INSERT_TAIL(&sq->req_list, req, entry);
|
|
|
|
}
|
2018-11-26 20:17:45 +03:00
|
|
|
if (cq->tail != cq->head) {
|
|
|
|
nvme_irq_assert(n, cq);
|
|
|
|
}
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void nvme_enqueue_req_completion(NvmeCQueue *cq, NvmeRequest *req)
|
|
|
|
{
|
|
|
|
assert(cq->cqid == req->sq->cqid);
|
2020-07-06 09:12:48 +03:00
|
|
|
trace_pci_nvme_enqueue_req_completion(nvme_cid(req), cq->cqid,
|
|
|
|
req->status);
|
2020-09-30 02:19:05 +03:00
|
|
|
|
|
|
|
if (req->status) {
|
|
|
|
trace_pci_nvme_err_req_status(nvme_cid(req), nvme_nsid(req->ns),
|
|
|
|
req->status, req->cmd.opcode);
|
|
|
|
}
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
QTAILQ_REMOVE(&req->sq->out_req_list, req, entry);
|
|
|
|
QTAILQ_INSERT_TAIL(&cq->req_list, req, entry);
|
2013-08-21 19:03:08 +04:00
|
|
|
timer_mod(cq->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 500);
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
|
|
|
|
2020-07-06 09:12:53 +03:00
|
|
|
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_clear_events(NvmeCtrl *n, uint8_t event_type)
|
|
|
|
{
|
|
|
|
n->aer_mask &= ~(1 << event_type);
|
|
|
|
if (!QTAILQ_EMPTY(&n->aer_queue)) {
|
|
|
|
nvme_process_aers(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 19:38:22 +03:00
|
|
|
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) {
|
|
|
|
return NVME_INVALID_FIELD | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NVME_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-02-23 19:32:25 +03:00
|
|
|
static inline uint16_t nvme_check_bounds(NvmeCtrl *n, 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)) {
|
|
|
|
return NVME_LBA_RANGE | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NVME_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
static void nvme_rw_cb(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
NvmeRequest *req = opaque;
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
NvmeNamespace *ns = req->ns;
|
2013-06-04 19:17:10 +04:00
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
BlockBackend *blk = ns->blkconf.blk;
|
2020-08-24 13:43:38 +03:00
|
|
|
BlockAcctCookie *acct = &req->acct;
|
|
|
|
BlockAcctStats *stats = blk_get_stats(blk);
|
|
|
|
|
|
|
|
Error *local_err = NULL;
|
|
|
|
|
|
|
|
trace_pci_nvme_rw_cb(nvme_cid(req), blk_name(blk));
|
2020-07-06 09:12:48 +03:00
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
if (!ret) {
|
2020-08-24 13:43:38 +03:00
|
|
|
block_acct_done(stats, acct);
|
2013-06-04 19:17:10 +04:00
|
|
|
} else {
|
2020-08-24 13:43:38 +03:00
|
|
|
uint16_t status;
|
|
|
|
|
|
|
|
block_acct_failed(stats, acct);
|
|
|
|
|
|
|
|
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:
|
|
|
|
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);
|
|
|
|
|
|
|
|
req->status = status;
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
2020-07-29 22:08:04 +03:00
|
|
|
|
2020-08-24 13:43:38 +03:00
|
|
|
nvme_enqueue_req_completion(nvme_cq(req), req);
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
|
|
|
|
2020-08-24 13:43:38 +03:00
|
|
|
static uint16_t nvme_flush(NvmeCtrl *n, NvmeRequest *req)
|
|
|
|
{
|
2020-09-30 22:22:27 +03:00
|
|
|
block_acct_start(blk_get_stats(req->ns->blkconf.blk), &req->acct, 0,
|
|
|
|
BLOCK_ACCT_FLUSH);
|
|
|
|
req->aiocb = blk_aio_flush(req->ns->blkconf.blk, nvme_rw_cb, req);
|
|
|
|
return NVME_NO_COMPLETE;
|
2020-08-24 13:43:38 +03:00
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static uint16_t nvme_write_zeroes(NvmeCtrl *n, NvmeRequest *req)
|
2017-05-05 12:58:07 +03:00
|
|
|
{
|
2020-07-20 13:44:01 +03:00
|
|
|
NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
|
|
|
|
NvmeNamespace *ns = req->ns;
|
2017-05-05 12:58:07 +03:00
|
|
|
uint64_t slba = le64_to_cpu(rw->slba);
|
2020-08-24 12:55:46 +03:00
|
|
|
uint32_t nlb = (uint32_t)le16_to_cpu(rw->nlb) + 1;
|
2020-08-24 09:59:41 +03:00
|
|
|
uint64_t offset = nvme_l2b(ns, slba);
|
|
|
|
uint32_t count = nvme_l2b(ns, nlb);
|
2020-02-23 19:32:25 +03:00
|
|
|
uint16_t status;
|
2017-05-05 12:58:07 +03:00
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
trace_pci_nvme_write_zeroes(nvme_cid(req), nvme_nsid(ns), slba, nlb);
|
2020-07-06 09:12:48 +03:00
|
|
|
|
2020-02-23 19:32:25 +03:00
|
|
|
status = nvme_check_bounds(n, ns, slba, nlb);
|
|
|
|
if (status) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_invalid_lba_range(slba, nlb, ns->id_ns.nsze);
|
2020-02-23 19:32:25 +03:00
|
|
|
return status;
|
2017-05-05 12:58:07 +03:00
|
|
|
}
|
|
|
|
|
2020-09-30 22:22:27 +03:00
|
|
|
block_acct_start(blk_get_stats(req->ns->blkconf.blk), &req->acct, 0,
|
|
|
|
BLOCK_ACCT_WRITE);
|
|
|
|
req->aiocb = blk_aio_pwrite_zeroes(req->ns->blkconf.blk, offset, count,
|
|
|
|
BDRV_REQ_MAY_UNMAP, nvme_rw_cb, req);
|
|
|
|
return NVME_NO_COMPLETE;
|
2017-05-05 12:58:07 +03:00
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static uint16_t nvme_rw(NvmeCtrl *n, NvmeRequest *req)
|
2013-06-04 19:17:10 +04:00
|
|
|
{
|
2020-07-20 13:44:01 +03:00
|
|
|
NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
|
|
|
|
NvmeNamespace *ns = req->ns;
|
2020-08-24 12:55:46 +03:00
|
|
|
uint32_t nlb = (uint32_t)le16_to_cpu(rw->nlb) + 1;
|
2013-06-04 19:17:10 +04:00
|
|
|
uint64_t slba = le64_to_cpu(rw->slba);
|
|
|
|
|
2020-08-24 09:59:41 +03:00
|
|
|
uint64_t data_size = nvme_l2b(ns, nlb);
|
|
|
|
uint64_t data_offset = nvme_l2b(ns, slba);
|
2020-08-24 13:43:38 +03:00
|
|
|
enum BlockAcctType acct = req->cmd.opcode == NVME_CMD_WRITE ?
|
|
|
|
BLOCK_ACCT_WRITE : BLOCK_ACCT_READ;
|
2020-09-30 22:22:27 +03:00
|
|
|
BlockBackend *blk = ns->blkconf.blk;
|
2020-02-23 19:32:25 +03:00
|
|
|
uint16_t status;
|
2013-06-04 19:17:10 +04:00
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
trace_pci_nvme_rw(nvme_cid(req), nvme_io_opc_str(rw->opcode),
|
|
|
|
nvme_nsid(ns), nlb, data_size, slba);
|
2017-11-03 16:37:53 +03:00
|
|
|
|
2020-02-23 19:38:22 +03:00
|
|
|
status = nvme_check_mdts(n, data_size);
|
|
|
|
if (status) {
|
|
|
|
trace_pci_nvme_err_mdts(nvme_cid(req), data_size);
|
2020-08-24 09:48:55 +03:00
|
|
|
goto invalid;
|
2020-02-23 19:38:22 +03:00
|
|
|
}
|
|
|
|
|
2020-02-23 19:32:25 +03:00
|
|
|
status = nvme_check_bounds(n, ns, slba, nlb);
|
|
|
|
if (status) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_invalid_lba_range(slba, nlb, ns->id_ns.nsze);
|
2020-08-24 09:48:55 +03:00
|
|
|
goto invalid;
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
2015-10-28 18:33:11 +03:00
|
|
|
|
2020-08-24 09:48:55 +03:00
|
|
|
status = nvme_map_dptr(n, data_size, req);
|
|
|
|
if (status) {
|
|
|
|
goto invalid;
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
2015-10-28 18:33:11 +03:00
|
|
|
|
2020-09-30 22:22:27 +03:00
|
|
|
block_acct_start(blk_get_stats(blk), &req->acct, data_size, acct);
|
|
|
|
if (req->qsg.sg) {
|
|
|
|
if (acct == BLOCK_ACCT_WRITE) {
|
|
|
|
req->aiocb = dma_blk_write(blk, &req->qsg, data_offset,
|
|
|
|
BDRV_SECTOR_SIZE, nvme_rw_cb, req);
|
|
|
|
} else {
|
|
|
|
req->aiocb = dma_blk_read(blk, &req->qsg, data_offset,
|
|
|
|
BDRV_SECTOR_SIZE, nvme_rw_cb, req);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (acct == BLOCK_ACCT_WRITE) {
|
|
|
|
req->aiocb = blk_aio_pwritev(blk, data_offset, &req->iov, 0,
|
|
|
|
nvme_rw_cb, req);
|
|
|
|
} else {
|
|
|
|
req->aiocb = blk_aio_preadv(blk, data_offset, &req->iov, 0,
|
|
|
|
nvme_rw_cb, req);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NVME_NO_COMPLETE;
|
2020-08-24 09:48:55 +03:00
|
|
|
|
|
|
|
invalid:
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
block_acct_invalid(blk_get_stats(ns->blkconf.blk), acct);
|
2020-08-24 09:48:55 +03:00
|
|
|
return status;
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static uint16_t nvme_io_cmd(NvmeCtrl *n, NvmeRequest *req)
|
2013-06-04 19:17:10 +04:00
|
|
|
{
|
2020-07-20 13:44:01 +03:00
|
|
|
uint32_t nsid = le32_to_cpu(req->cmd.nsid);
|
2013-06-04 19:17:10 +04:00
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
trace_pci_nvme_io_cmd(nvme_cid(req), nsid, nvme_sqid(req),
|
2020-08-24 23:11:33 +03:00
|
|
|
req->cmd.opcode, nvme_io_opc_str(req->cmd.opcode));
|
2020-07-06 09:12:48 +03:00
|
|
|
|
2020-09-30 02:19:07 +03:00
|
|
|
if (NVME_CC_CSS(n->bar.cc) == NVME_CC_CSS_ADMIN_ONLY) {
|
|
|
|
return NVME_INVALID_OPCODE | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
if (!nvme_nsid_valid(n, nsid)) {
|
2013-06-04 19:17:10 +04:00
|
|
|
return NVME_INVALID_NSID | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
req->ns = nvme_ns(n, nsid);
|
|
|
|
if (unlikely(!req->ns)) {
|
|
|
|
return NVME_INVALID_FIELD | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
switch (req->cmd.opcode) {
|
2013-06-04 19:17:10 +04:00
|
|
|
case NVME_CMD_FLUSH:
|
2020-07-20 13:44:01 +03:00
|
|
|
return nvme_flush(n, req);
|
2020-03-31 00:10:13 +03:00
|
|
|
case NVME_CMD_WRITE_ZEROES:
|
2020-07-20 13:44:01 +03:00
|
|
|
return nvme_write_zeroes(n, req);
|
2013-06-04 19:17:10 +04:00
|
|
|
case NVME_CMD_WRITE:
|
|
|
|
case NVME_CMD_READ:
|
2020-07-20 13:44:01 +03:00
|
|
|
return nvme_rw(n, req);
|
2013-06-04 19:17:10 +04:00
|
|
|
default:
|
2020-07-20 13:44:01 +03:00
|
|
|
trace_pci_nvme_err_invalid_opc(req->cmd.opcode);
|
2013-06-04 19:17:10 +04:00
|
|
|
return NVME_INVALID_OPCODE | NVME_DNR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nvme_free_sq(NvmeSQueue *sq, NvmeCtrl *n)
|
|
|
|
{
|
|
|
|
n->sq[sq->sqid] = NULL;
|
2013-08-21 19:03:08 +04:00
|
|
|
timer_del(sq->timer);
|
|
|
|
timer_free(sq->timer);
|
2013-06-04 19:17:10 +04:00
|
|
|
g_free(sq->io_req);
|
|
|
|
if (sq->sqid) {
|
|
|
|
g_free(sq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static uint16_t nvme_del_sq(NvmeCtrl *n, NvmeRequest *req)
|
2013-06-04 19:17:10 +04:00
|
|
|
{
|
2020-07-20 13:44:01 +03:00
|
|
|
NvmeDeleteQ *c = (NvmeDeleteQ *)&req->cmd;
|
|
|
|
NvmeRequest *r, *next;
|
2013-06-04 19:17:10 +04:00
|
|
|
NvmeSQueue *sq;
|
|
|
|
NvmeCQueue *cq;
|
|
|
|
uint16_t qid = le16_to_cpu(c->qid);
|
|
|
|
|
2017-11-03 16:37:53 +03:00
|
|
|
if (unlikely(!qid || nvme_check_sqid(n, qid))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_invalid_del_sq(qid);
|
2013-06-04 19:17:10 +04:00
|
|
|
return NVME_INVALID_QID | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_del_sq(qid);
|
2017-11-03 16:37:53 +03:00
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
sq = n->sq[qid];
|
|
|
|
while (!QTAILQ_EMPTY(&sq->out_req_list)) {
|
2020-07-20 13:44:01 +03:00
|
|
|
r = QTAILQ_FIRST(&sq->out_req_list);
|
|
|
|
assert(r->aiocb);
|
|
|
|
blk_aio_cancel(r->aiocb);
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
|
|
|
if (!nvme_check_cqid(n, sq->cqid)) {
|
|
|
|
cq = n->cq[sq->cqid];
|
|
|
|
QTAILQ_REMOVE(&cq->sq_list, sq, entry);
|
|
|
|
|
|
|
|
nvme_post_cqes(cq);
|
2020-07-20 13:44:01 +03:00
|
|
|
QTAILQ_FOREACH_SAFE(r, &cq->req_list, entry, next) {
|
|
|
|
if (r->sq == sq) {
|
|
|
|
QTAILQ_REMOVE(&cq->req_list, r, entry);
|
|
|
|
QTAILQ_INSERT_TAIL(&sq->req_list, r, entry);
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nvme_free_sq(sq, n);
|
|
|
|
return NVME_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nvme_init_sq(NvmeSQueue *sq, NvmeCtrl *n, uint64_t dma_addr,
|
2020-08-24 09:58:56 +03:00
|
|
|
uint16_t sqid, uint16_t cqid, uint16_t size)
|
2013-06-04 19:17:10 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
NvmeCQueue *cq;
|
|
|
|
|
|
|
|
sq->ctrl = n;
|
|
|
|
sq->dma_addr = dma_addr;
|
|
|
|
sq->sqid = sqid;
|
|
|
|
sq->size = size;
|
|
|
|
sq->cqid = cqid;
|
|
|
|
sq->head = sq->tail = 0;
|
2020-02-23 18:37:49 +03:00
|
|
|
sq->io_req = g_new0(NvmeRequest, sq->size);
|
2013-06-04 19:17:10 +04:00
|
|
|
|
|
|
|
QTAILQ_INIT(&sq->req_list);
|
|
|
|
QTAILQ_INIT(&sq->out_req_list);
|
|
|
|
for (i = 0; i < sq->size; i++) {
|
|
|
|
sq->io_req[i].sq = sq;
|
|
|
|
QTAILQ_INSERT_TAIL(&(sq->req_list), &sq->io_req[i], entry);
|
|
|
|
}
|
2013-08-21 19:03:08 +04:00
|
|
|
sq->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, nvme_process_sq, sq);
|
2013-06-04 19:17:10 +04:00
|
|
|
|
|
|
|
assert(n->cq[cqid]);
|
|
|
|
cq = n->cq[cqid];
|
|
|
|
QTAILQ_INSERT_TAIL(&(cq->sq_list), sq, entry);
|
|
|
|
n->sq[sqid] = sq;
|
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static uint16_t nvme_create_sq(NvmeCtrl *n, NvmeRequest *req)
|
2013-06-04 19:17:10 +04:00
|
|
|
{
|
|
|
|
NvmeSQueue *sq;
|
2020-07-20 13:44:01 +03:00
|
|
|
NvmeCreateSq *c = (NvmeCreateSq *)&req->cmd;
|
2013-06-04 19:17:10 +04:00
|
|
|
|
|
|
|
uint16_t cqid = le16_to_cpu(c->cqid);
|
|
|
|
uint16_t sqid = le16_to_cpu(c->sqid);
|
|
|
|
uint16_t qsize = le16_to_cpu(c->qsize);
|
|
|
|
uint16_t qflags = le16_to_cpu(c->sq_flags);
|
|
|
|
uint64_t prp1 = le64_to_cpu(c->prp1);
|
|
|
|
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_create_sq(prp1, sqid, cqid, qsize, qflags);
|
2017-11-03 16:37:53 +03:00
|
|
|
|
|
|
|
if (unlikely(!cqid || nvme_check_cqid(n, cqid))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_invalid_create_sq_cqid(cqid);
|
2013-06-04 19:17:10 +04:00
|
|
|
return NVME_INVALID_CQID | NVME_DNR;
|
|
|
|
}
|
2020-10-22 12:07:08 +03:00
|
|
|
if (unlikely(!sqid || sqid > n->params.max_ioqpairs ||
|
|
|
|
n->sq[sqid] != NULL)) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_invalid_create_sq_sqid(sqid);
|
2013-06-04 19:17:10 +04:00
|
|
|
return NVME_INVALID_QID | NVME_DNR;
|
|
|
|
}
|
2017-11-03 16:37:53 +03:00
|
|
|
if (unlikely(!qsize || qsize > NVME_CAP_MQES(n->bar.cap))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_invalid_create_sq_size(qsize);
|
2013-06-04 19:17:10 +04:00
|
|
|
return NVME_MAX_QSIZE_EXCEEDED | NVME_DNR;
|
|
|
|
}
|
2020-10-22 09:28:46 +03:00
|
|
|
if (unlikely(prp1 & (n->page_size - 1))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_invalid_create_sq_addr(prp1);
|
2020-10-22 09:28:46 +03:00
|
|
|
return NVME_INVALID_PRP_OFFSET | NVME_DNR;
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
2017-11-03 16:37:53 +03:00
|
|
|
if (unlikely(!(NVME_SQ_FLAGS_PC(qflags)))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_invalid_create_sq_qflags(NVME_SQ_FLAGS_PC(qflags));
|
2013-06-04 19:17:10 +04:00
|
|
|
return NVME_INVALID_FIELD | NVME_DNR;
|
|
|
|
}
|
|
|
|
sq = g_malloc0(sizeof(*sq));
|
|
|
|
nvme_init_sq(sq, n, prp1, sqid, cqid, qsize + 1);
|
|
|
|
return NVME_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-09-30 20:15:50 +03:00
|
|
|
struct nvme_stats {
|
|
|
|
uint64_t units_read;
|
|
|
|
uint64_t units_written;
|
|
|
|
uint64_t read_commands;
|
|
|
|
uint64_t write_commands;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void nvme_set_blk_stats(NvmeNamespace *ns, struct nvme_stats *stats)
|
|
|
|
{
|
|
|
|
BlockAcctStats *s = blk_get_stats(ns->blkconf.blk);
|
|
|
|
|
|
|
|
stats->units_read += s->nr_bytes[BLOCK_ACCT_READ] >> BDRV_SECTOR_BITS;
|
|
|
|
stats->units_written += s->nr_bytes[BLOCK_ACCT_WRITE] >> BDRV_SECTOR_BITS;
|
|
|
|
stats->read_commands += s->nr_ops[BLOCK_ACCT_READ];
|
|
|
|
stats->write_commands += s->nr_ops[BLOCK_ACCT_WRITE];
|
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static uint16_t nvme_smart_info(NvmeCtrl *n, uint8_t rae, uint32_t buf_len,
|
|
|
|
uint64_t off, NvmeRequest *req)
|
2020-07-06 09:12:52 +03:00
|
|
|
{
|
2019-04-12 21:53:16 +03:00
|
|
|
uint32_t nsid = le32_to_cpu(req->cmd.nsid);
|
2020-09-30 20:15:50 +03:00
|
|
|
struct nvme_stats stats = { 0 };
|
|
|
|
NvmeSmartLog smart = { 0 };
|
2020-07-06 09:12:52 +03:00
|
|
|
uint32_t trans_len;
|
2020-09-30 20:15:50 +03:00
|
|
|
NvmeNamespace *ns;
|
2020-07-06 09:12:52 +03:00
|
|
|
time_t current_ms;
|
|
|
|
|
2020-09-30 20:01:02 +03:00
|
|
|
if (off >= sizeof(smart)) {
|
|
|
|
return NVME_INVALID_FIELD | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
2020-09-30 20:15:50 +03:00
|
|
|
if (nsid != 0xffffffff) {
|
|
|
|
ns = nvme_ns(n, nsid);
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
if (!ns) {
|
2020-09-30 20:15:50 +03:00
|
|
|
return NVME_INVALID_NSID | NVME_DNR;
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
}
|
2020-09-30 20:15:50 +03:00
|
|
|
nvme_set_blk_stats(ns, &stats);
|
|
|
|
} else {
|
|
|
|
int i;
|
2020-07-06 09:12:52 +03:00
|
|
|
|
2020-09-30 20:15:50 +03:00
|
|
|
for (i = 1; i <= n->num_namespaces; i++) {
|
|
|
|
ns = nvme_ns(n, i);
|
|
|
|
if (!ns) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
nvme_set_blk_stats(ns, &stats);
|
|
|
|
}
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
}
|
2020-07-06 09:12:52 +03:00
|
|
|
|
|
|
|
trans_len = MIN(sizeof(smart) - off, buf_len);
|
|
|
|
|
2020-09-30 20:15:50 +03:00
|
|
|
smart.data_units_read[0] = cpu_to_le64(DIV_ROUND_UP(stats.units_read,
|
|
|
|
1000));
|
|
|
|
smart.data_units_written[0] = cpu_to_le64(DIV_ROUND_UP(stats.units_written,
|
2020-07-06 09:12:52 +03:00
|
|
|
1000));
|
2020-09-30 20:15:50 +03:00
|
|
|
smart.host_read_commands[0] = cpu_to_le64(stats.read_commands);
|
|
|
|
smart.host_write_commands[0] = cpu_to_le64(stats.write_commands);
|
2020-07-06 09:12:52 +03:00
|
|
|
|
|
|
|
smart.temperature = cpu_to_le16(n->temperature);
|
|
|
|
|
|
|
|
if ((n->temperature >= n->features.temp_thresh_hi) ||
|
|
|
|
(n->temperature <= n->features.temp_thresh_low)) {
|
|
|
|
smart.critical_warning |= NVME_SMART_TEMPERATURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
current_ms = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
|
|
|
|
smart.power_on_hours[0] =
|
|
|
|
cpu_to_le64((((current_ms - n->starttime_ms) / 1000) / 60) / 60);
|
|
|
|
|
2020-07-06 09:12:53 +03:00
|
|
|
if (!rae) {
|
|
|
|
nvme_clear_events(n, NVME_AER_TYPE_SMART);
|
|
|
|
}
|
|
|
|
|
2019-04-12 21:53:16 +03:00
|
|
|
return nvme_dma(n, (uint8_t *) &smart + off, trans_len,
|
|
|
|
DMA_DIRECTION_FROM_DEVICE, req);
|
2020-07-06 09:12:52 +03:00
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static uint16_t nvme_fw_log_info(NvmeCtrl *n, uint32_t buf_len, uint64_t off,
|
|
|
|
NvmeRequest *req)
|
2020-07-06 09:12:52 +03:00
|
|
|
{
|
|
|
|
uint32_t trans_len;
|
|
|
|
NvmeFwSlotInfoLog fw_log = {
|
|
|
|
.afi = 0x1,
|
|
|
|
};
|
|
|
|
|
2020-09-30 20:01:02 +03:00
|
|
|
if (off >= sizeof(fw_log)) {
|
2020-07-06 09:12:52 +03:00
|
|
|
return NVME_INVALID_FIELD | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
2020-09-30 20:01:02 +03:00
|
|
|
strpadcpy((char *)&fw_log.frs1, sizeof(fw_log.frs1), "1.0", ' ');
|
2020-07-06 09:12:52 +03:00
|
|
|
trans_len = MIN(sizeof(fw_log) - off, buf_len);
|
|
|
|
|
2019-04-12 21:53:16 +03:00
|
|
|
return nvme_dma(n, (uint8_t *) &fw_log + off, trans_len,
|
|
|
|
DMA_DIRECTION_FROM_DEVICE, req);
|
2020-07-06 09:12:52 +03:00
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static uint16_t nvme_error_info(NvmeCtrl *n, uint8_t rae, uint32_t buf_len,
|
|
|
|
uint64_t off, NvmeRequest *req)
|
2020-07-06 09:12:52 +03:00
|
|
|
{
|
|
|
|
uint32_t trans_len;
|
|
|
|
NvmeErrorLog errlog;
|
|
|
|
|
2020-09-30 20:01:02 +03:00
|
|
|
if (off >= sizeof(errlog)) {
|
|
|
|
return NVME_INVALID_FIELD | NVME_DNR;
|
2020-07-06 09:12:53 +03:00
|
|
|
}
|
|
|
|
|
2020-09-30 20:01:02 +03:00
|
|
|
if (!rae) {
|
|
|
|
nvme_clear_events(n, NVME_AER_TYPE_ERROR);
|
2020-07-06 09:12:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(&errlog, 0x0, sizeof(errlog));
|
|
|
|
trans_len = MIN(sizeof(errlog) - off, buf_len);
|
|
|
|
|
2019-04-12 21:53:16 +03:00
|
|
|
return nvme_dma(n, (uint8_t *)&errlog, trans_len,
|
|
|
|
DMA_DIRECTION_FROM_DEVICE, req);
|
2020-07-06 09:12:52 +03:00
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static uint16_t nvme_get_log(NvmeCtrl *n, NvmeRequest *req)
|
2020-07-06 09:12:52 +03:00
|
|
|
{
|
2020-07-20 13:44:01 +03:00
|
|
|
NvmeCmd *cmd = &req->cmd;
|
|
|
|
|
2020-07-06 09:12:52 +03:00
|
|
|
uint32_t dw10 = le32_to_cpu(cmd->cdw10);
|
|
|
|
uint32_t dw11 = le32_to_cpu(cmd->cdw11);
|
|
|
|
uint32_t dw12 = le32_to_cpu(cmd->cdw12);
|
|
|
|
uint32_t dw13 = le32_to_cpu(cmd->cdw13);
|
|
|
|
uint8_t lid = dw10 & 0xff;
|
|
|
|
uint8_t lsp = (dw10 >> 8) & 0xf;
|
|
|
|
uint8_t rae = (dw10 >> 15) & 0x1;
|
|
|
|
uint32_t numdl, numdu;
|
|
|
|
uint64_t off, lpol, lpou;
|
|
|
|
size_t len;
|
2020-02-23 19:38:22 +03:00
|
|
|
uint16_t status;
|
2020-07-06 09:12:52 +03:00
|
|
|
|
|
|
|
numdl = (dw10 >> 16);
|
|
|
|
numdu = (dw11 & 0xffff);
|
|
|
|
lpol = dw12;
|
|
|
|
lpou = dw13;
|
|
|
|
|
|
|
|
len = (((numdu << 16) | numdl) + 1) << 2;
|
|
|
|
off = (lpou << 32ULL) | lpol;
|
|
|
|
|
|
|
|
if (off & 0x3) {
|
|
|
|
return NVME_INVALID_FIELD | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace_pci_nvme_get_log(nvme_cid(req), lid, lsp, rae, len, off);
|
|
|
|
|
2020-02-23 19:38:22 +03:00
|
|
|
status = nvme_check_mdts(n, len);
|
|
|
|
if (status) {
|
|
|
|
trace_pci_nvme_err_mdts(nvme_cid(req), len);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2020-07-06 09:12:52 +03:00
|
|
|
switch (lid) {
|
|
|
|
case NVME_LOG_ERROR_INFO:
|
2020-07-20 13:44:01 +03:00
|
|
|
return nvme_error_info(n, rae, len, off, req);
|
2020-07-06 09:12:52 +03:00
|
|
|
case NVME_LOG_SMART_INFO:
|
2020-07-20 13:44:01 +03:00
|
|
|
return nvme_smart_info(n, rae, len, off, req);
|
2020-07-06 09:12:52 +03:00
|
|
|
case NVME_LOG_FW_SLOT_INFO:
|
2020-07-20 13:44:01 +03:00
|
|
|
return nvme_fw_log_info(n, len, off, req);
|
2020-07-06 09:12:52 +03:00
|
|
|
default:
|
|
|
|
trace_pci_nvme_err_invalid_log_page(nvme_cid(req), lid);
|
|
|
|
return NVME_INVALID_FIELD | NVME_DNR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
static void nvme_free_cq(NvmeCQueue *cq, NvmeCtrl *n)
|
|
|
|
{
|
|
|
|
n->cq[cq->cqid] = NULL;
|
2013-08-21 19:03:08 +04:00
|
|
|
timer_del(cq->timer);
|
|
|
|
timer_free(cq->timer);
|
2013-06-04 19:17:10 +04:00
|
|
|
msix_vector_unuse(&n->parent_obj, cq->vector);
|
|
|
|
if (cq->cqid) {
|
|
|
|
g_free(cq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static uint16_t nvme_del_cq(NvmeCtrl *n, NvmeRequest *req)
|
2013-06-04 19:17:10 +04:00
|
|
|
{
|
2020-07-20 13:44:01 +03:00
|
|
|
NvmeDeleteQ *c = (NvmeDeleteQ *)&req->cmd;
|
2013-06-04 19:17:10 +04:00
|
|
|
NvmeCQueue *cq;
|
|
|
|
uint16_t qid = le16_to_cpu(c->qid);
|
|
|
|
|
2017-11-03 16:37:53 +03:00
|
|
|
if (unlikely(!qid || nvme_check_cqid(n, qid))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_invalid_del_cq_cqid(qid);
|
2013-06-04 19:17:10 +04:00
|
|
|
return NVME_INVALID_CQID | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
|
|
|
cq = n->cq[qid];
|
2017-11-03 16:37:53 +03:00
|
|
|
if (unlikely(!QTAILQ_EMPTY(&cq->sq_list))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_invalid_del_cq_notempty(qid);
|
2013-06-04 19:17:10 +04:00
|
|
|
return NVME_INVALID_QUEUE_DEL;
|
|
|
|
}
|
2018-11-21 21:10:13 +03:00
|
|
|
nvme_irq_deassert(n, cq);
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_del_cq(qid);
|
2013-06-04 19:17:10 +04:00
|
|
|
nvme_free_cq(cq, n);
|
|
|
|
return NVME_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nvme_init_cq(NvmeCQueue *cq, NvmeCtrl *n, uint64_t dma_addr,
|
2020-08-24 09:58:56 +03:00
|
|
|
uint16_t cqid, uint16_t vector, uint16_t size,
|
|
|
|
uint16_t irq_enabled)
|
2013-06-04 19:17:10 +04:00
|
|
|
{
|
2020-06-09 22:03:31 +03:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = msix_vector_use(&n->parent_obj, vector);
|
|
|
|
assert(ret == 0);
|
2013-06-04 19:17:10 +04:00
|
|
|
cq->ctrl = n;
|
|
|
|
cq->cqid = cqid;
|
|
|
|
cq->size = size;
|
|
|
|
cq->dma_addr = dma_addr;
|
|
|
|
cq->phase = 1;
|
|
|
|
cq->irq_enabled = irq_enabled;
|
|
|
|
cq->vector = vector;
|
|
|
|
cq->head = cq->tail = 0;
|
|
|
|
QTAILQ_INIT(&cq->req_list);
|
|
|
|
QTAILQ_INIT(&cq->sq_list);
|
|
|
|
n->cq[cqid] = cq;
|
2013-08-21 19:03:08 +04:00
|
|
|
cq->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, nvme_post_cqes, cq);
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static uint16_t nvme_create_cq(NvmeCtrl *n, NvmeRequest *req)
|
2013-06-04 19:17:10 +04:00
|
|
|
{
|
|
|
|
NvmeCQueue *cq;
|
2020-07-20 13:44:01 +03:00
|
|
|
NvmeCreateCq *c = (NvmeCreateCq *)&req->cmd;
|
2013-06-04 19:17:10 +04:00
|
|
|
uint16_t cqid = le16_to_cpu(c->cqid);
|
|
|
|
uint16_t vector = le16_to_cpu(c->irq_vector);
|
|
|
|
uint16_t qsize = le16_to_cpu(c->qsize);
|
|
|
|
uint16_t qflags = le16_to_cpu(c->cq_flags);
|
|
|
|
uint64_t prp1 = le64_to_cpu(c->prp1);
|
|
|
|
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_create_cq(prp1, cqid, vector, qsize, qflags,
|
|
|
|
NVME_CQ_FLAGS_IEN(qflags) != 0);
|
2017-11-03 16:37:53 +03:00
|
|
|
|
2020-10-22 12:07:08 +03:00
|
|
|
if (unlikely(!cqid || cqid > n->params.max_ioqpairs ||
|
|
|
|
n->cq[cqid] != NULL)) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_invalid_create_cq_cqid(cqid);
|
2020-10-22 09:28:46 +03:00
|
|
|
return NVME_INVALID_QID | NVME_DNR;
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
2017-11-03 16:37:53 +03:00
|
|
|
if (unlikely(!qsize || qsize > NVME_CAP_MQES(n->bar.cap))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_invalid_create_cq_size(qsize);
|
2013-06-04 19:17:10 +04:00
|
|
|
return NVME_MAX_QSIZE_EXCEEDED | NVME_DNR;
|
|
|
|
}
|
2020-10-22 09:28:46 +03:00
|
|
|
if (unlikely(prp1 & (n->page_size - 1))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_invalid_create_cq_addr(prp1);
|
2020-10-22 09:28:46 +03:00
|
|
|
return NVME_INVALID_PRP_OFFSET | NVME_DNR;
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
2020-06-09 22:03:18 +03:00
|
|
|
if (unlikely(!msix_enabled(&n->parent_obj) && vector)) {
|
|
|
|
trace_pci_nvme_err_invalid_create_cq_vector(vector);
|
|
|
|
return NVME_INVALID_IRQ_VECTOR | NVME_DNR;
|
|
|
|
}
|
2020-06-09 22:03:32 +03:00
|
|
|
if (unlikely(vector >= n->params.msix_qsize)) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_invalid_create_cq_vector(vector);
|
2013-06-04 19:17:10 +04:00
|
|
|
return NVME_INVALID_IRQ_VECTOR | NVME_DNR;
|
|
|
|
}
|
2017-11-03 16:37:53 +03:00
|
|
|
if (unlikely(!(NVME_CQ_FLAGS_PC(qflags)))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_invalid_create_cq_qflags(NVME_CQ_FLAGS_PC(qflags));
|
2013-06-04 19:17:10 +04:00
|
|
|
return NVME_INVALID_FIELD | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
|
|
|
cq = g_malloc0(sizeof(*cq));
|
|
|
|
nvme_init_cq(cq, n, prp1, cqid, vector, qsize + 1,
|
2020-08-24 09:58:56 +03:00
|
|
|
NVME_CQ_FLAGS_IEN(qflags));
|
2020-07-06 09:13:01 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* It is only required to set qs_created when creating a completion queue;
|
|
|
|
* creating a submission queue without a matching completion queue will
|
|
|
|
* fail.
|
|
|
|
*/
|
|
|
|
n->qs_created = true;
|
2013-06-04 19:17:10 +04:00
|
|
|
return NVME_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static uint16_t nvme_identify_ctrl(NvmeCtrl *n, NvmeRequest *req)
|
2016-08-04 22:42:14 +03:00
|
|
|
{
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_identify_ctrl();
|
2017-11-03 16:37:53 +03:00
|
|
|
|
2019-04-12 21:53:16 +03:00
|
|
|
return nvme_dma(n, (uint8_t *)&n->id_ctrl, sizeof(n->id_ctrl),
|
|
|
|
DMA_DIRECTION_FROM_DEVICE, req);
|
2016-08-04 22:42:14 +03:00
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static uint16_t nvme_identify_ns(NvmeCtrl *n, NvmeRequest *req)
|
2013-06-04 19:17:10 +04:00
|
|
|
{
|
|
|
|
NvmeNamespace *ns;
|
2020-07-20 13:44:01 +03:00
|
|
|
NvmeIdentify *c = (NvmeIdentify *)&req->cmd;
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
NvmeIdNs *id_ns, inactive = { 0 };
|
2013-06-04 19:17:10 +04:00
|
|
|
uint32_t nsid = le32_to_cpu(c->nsid);
|
|
|
|
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_identify_ns(nsid);
|
2017-11-03 16:37:53 +03:00
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
if (!nvme_nsid_valid(n, nsid) || nsid == NVME_NSID_BROADCAST) {
|
2013-06-04 19:17:10 +04:00
|
|
|
return NVME_INVALID_NSID | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
ns = nvme_ns(n, nsid);
|
|
|
|
if (unlikely(!ns)) {
|
|
|
|
id_ns = &inactive;
|
|
|
|
} else {
|
|
|
|
id_ns = &ns->id_ns;
|
|
|
|
}
|
2017-11-03 16:37:53 +03:00
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
return nvme_dma(n, (uint8_t *)id_ns, sizeof(NvmeIdNs),
|
2019-04-12 21:53:16 +03:00
|
|
|
DMA_DIRECTION_FROM_DEVICE, req);
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static uint16_t nvme_identify_nslist(NvmeCtrl *n, NvmeRequest *req)
|
2016-08-04 22:42:14 +03:00
|
|
|
{
|
2020-07-20 13:44:01 +03:00
|
|
|
NvmeIdentify *c = (NvmeIdentify *)&req->cmd;
|
2020-06-09 22:03:16 +03:00
|
|
|
static const int data_len = NVME_IDENTIFY_DATA_SIZE;
|
2016-08-04 22:42:14 +03:00
|
|
|
uint32_t min_nsid = le32_to_cpu(c->nsid);
|
|
|
|
uint32_t *list;
|
|
|
|
uint16_t ret;
|
2020-02-24 01:35:20 +03:00
|
|
|
int j = 0;
|
2016-08-04 22:42:14 +03:00
|
|
|
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_identify_nslist(min_nsid);
|
2017-11-03 16:37:53 +03:00
|
|
|
|
2020-07-06 09:13:00 +03:00
|
|
|
/*
|
|
|
|
* Both 0xffffffff (NVME_NSID_BROADCAST) and 0xfffffffe are invalid values
|
|
|
|
* since the Active Namespace ID List should return namespaces with ids
|
|
|
|
* *higher* than the NSID specified in the command. This is also specified
|
|
|
|
* in the spec (NVM Express v1.3d, Section 5.15.4).
|
|
|
|
*/
|
|
|
|
if (min_nsid >= NVME_NSID_BROADCAST - 1) {
|
|
|
|
return NVME_INVALID_NSID | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
2016-08-04 22:42:14 +03:00
|
|
|
list = g_malloc0(data_len);
|
2020-02-24 01:35:20 +03:00
|
|
|
for (int i = 1; i <= n->num_namespaces; i++) {
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
if (i <= min_nsid || !nvme_ns(n, i)) {
|
2016-08-04 22:42:14 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-02-24 01:35:20 +03:00
|
|
|
list[j++] = cpu_to_le32(i);
|
2016-08-04 22:42:14 +03:00
|
|
|
if (j == data_len / sizeof(uint32_t)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-04-12 21:53:16 +03:00
|
|
|
ret = nvme_dma(n, (uint8_t *)list, data_len, DMA_DIRECTION_FROM_DEVICE,
|
|
|
|
req);
|
2016-08-04 22:42:14 +03:00
|
|
|
g_free(list);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static uint16_t nvme_identify_ns_descr_list(NvmeCtrl *n, NvmeRequest *req)
|
2020-07-06 09:12:59 +03:00
|
|
|
{
|
2020-07-20 13:44:01 +03:00
|
|
|
NvmeIdentify *c = (NvmeIdentify *)&req->cmd;
|
2020-07-06 09:12:59 +03:00
|
|
|
uint32_t nsid = le32_to_cpu(c->nsid);
|
|
|
|
uint8_t list[NVME_IDENTIFY_DATA_SIZE];
|
|
|
|
|
|
|
|
struct data {
|
|
|
|
struct {
|
|
|
|
NvmeIdNsDescr hdr;
|
|
|
|
uint8_t v[16];
|
|
|
|
} uuid;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct data *ns_descrs = (struct data *)list;
|
|
|
|
|
|
|
|
trace_pci_nvme_identify_ns_descr_list(nsid);
|
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
if (!nvme_nsid_valid(n, nsid) || nsid == NVME_NSID_BROADCAST) {
|
2020-07-06 09:12:59 +03:00
|
|
|
return NVME_INVALID_NSID | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
if (unlikely(!nvme_ns(n, nsid))) {
|
|
|
|
return NVME_INVALID_FIELD | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
2020-07-06 09:12:59 +03:00
|
|
|
memset(list, 0x0, sizeof(list));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Because the NGUID and EUI64 fields are 0 in the Identify Namespace data
|
|
|
|
* structure, a Namespace UUID (nidt = 0x3) must be reported in the
|
|
|
|
* Namespace Identification Descriptor. Add a very basic Namespace UUID
|
|
|
|
* here.
|
|
|
|
*/
|
|
|
|
ns_descrs->uuid.hdr.nidt = NVME_NIDT_UUID;
|
|
|
|
ns_descrs->uuid.hdr.nidl = NVME_NIDT_UUID_LEN;
|
|
|
|
stl_be_p(&ns_descrs->uuid.v, nsid);
|
|
|
|
|
2019-04-12 21:53:16 +03:00
|
|
|
return nvme_dma(n, list, NVME_IDENTIFY_DATA_SIZE,
|
|
|
|
DMA_DIRECTION_FROM_DEVICE, req);
|
2020-07-06 09:12:59 +03:00
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static uint16_t nvme_identify(NvmeCtrl *n, NvmeRequest *req)
|
2016-08-04 22:42:14 +03:00
|
|
|
{
|
2020-07-20 13:44:01 +03:00
|
|
|
NvmeIdentify *c = (NvmeIdentify *)&req->cmd;
|
2016-08-04 22:42:14 +03:00
|
|
|
|
|
|
|
switch (le32_to_cpu(c->cns)) {
|
2020-06-09 22:03:16 +03:00
|
|
|
case NVME_ID_CNS_NS:
|
2020-07-20 13:44:01 +03:00
|
|
|
return nvme_identify_ns(n, req);
|
2020-06-09 22:03:16 +03:00
|
|
|
case NVME_ID_CNS_CTRL:
|
2020-07-20 13:44:01 +03:00
|
|
|
return nvme_identify_ctrl(n, req);
|
2020-06-09 22:03:16 +03:00
|
|
|
case NVME_ID_CNS_NS_ACTIVE_LIST:
|
2020-07-20 13:44:01 +03:00
|
|
|
return nvme_identify_nslist(n, req);
|
2020-07-06 09:12:59 +03:00
|
|
|
case NVME_ID_CNS_NS_DESCR_LIST:
|
2020-07-20 13:44:01 +03:00
|
|
|
return nvme_identify_ns_descr_list(n, req);
|
2016-08-04 22:42:14 +03:00
|
|
|
default:
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_invalid_identify_cns(le32_to_cpu(c->cns));
|
2016-08-04 22:42:14 +03:00
|
|
|
return NVME_INVALID_FIELD | NVME_DNR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static uint16_t nvme_abort(NvmeCtrl *n, NvmeRequest *req)
|
2020-07-06 09:12:49 +03:00
|
|
|
{
|
2020-07-20 13:44:01 +03:00
|
|
|
uint16_t sqid = le32_to_cpu(req->cmd.cdw10) & 0xffff;
|
2020-07-06 09:12:49 +03:00
|
|
|
|
|
|
|
req->cqe.result = 1;
|
|
|
|
if (nvme_check_sqid(n, sqid)) {
|
|
|
|
return NVME_INVALID_FIELD | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NVME_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-05-20 20:40:30 +03:00
|
|
|
static inline void nvme_set_timestamp(NvmeCtrl *n, uint64_t ts)
|
|
|
|
{
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_setfeat_timestamp(ts);
|
2019-05-20 20:40:30 +03:00
|
|
|
|
|
|
|
n->host_timestamp = le64_to_cpu(ts);
|
|
|
|
n->timestamp_set_qemu_clock_ms = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint64_t nvme_get_timestamp(const NvmeCtrl *n)
|
|
|
|
{
|
|
|
|
uint64_t current_time = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
|
|
|
|
uint64_t elapsed_time = current_time - n->timestamp_set_qemu_clock_ms;
|
|
|
|
|
|
|
|
union nvme_timestamp {
|
|
|
|
struct {
|
|
|
|
uint64_t timestamp:48;
|
|
|
|
uint64_t sync:1;
|
|
|
|
uint64_t origin:3;
|
|
|
|
uint64_t rsvd1:12;
|
|
|
|
};
|
|
|
|
uint64_t all;
|
|
|
|
};
|
|
|
|
|
|
|
|
union nvme_timestamp ts;
|
|
|
|
ts.all = 0;
|
2020-10-02 10:57:16 +03:00
|
|
|
ts.timestamp = n->host_timestamp + elapsed_time;
|
2019-05-20 20:40:30 +03:00
|
|
|
|
|
|
|
/* If the host timestamp is non-zero, set the timestamp origin */
|
|
|
|
ts.origin = n->host_timestamp ? 0x01 : 0x00;
|
|
|
|
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_getfeat_timestamp(ts.all);
|
2019-05-20 20:40:30 +03:00
|
|
|
|
|
|
|
return cpu_to_le64(ts.all);
|
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static uint16_t nvme_get_feature_timestamp(NvmeCtrl *n, NvmeRequest *req)
|
2019-05-20 20:40:30 +03:00
|
|
|
{
|
|
|
|
uint64_t timestamp = nvme_get_timestamp(n);
|
|
|
|
|
2019-04-12 21:53:16 +03:00
|
|
|
return nvme_dma(n, (uint8_t *)×tamp, sizeof(timestamp),
|
|
|
|
DMA_DIRECTION_FROM_DEVICE, req);
|
2019-05-20 20:40:30 +03:00
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static uint16_t nvme_get_feature(NvmeCtrl *n, NvmeRequest *req)
|
2013-06-04 19:17:10 +04:00
|
|
|
{
|
2020-07-20 13:44:01 +03:00
|
|
|
NvmeCmd *cmd = &req->cmd;
|
2013-06-04 19:17:10 +04:00
|
|
|
uint32_t dw10 = le32_to_cpu(cmd->cdw10);
|
2020-07-06 09:12:50 +03:00
|
|
|
uint32_t dw11 = le32_to_cpu(cmd->cdw11);
|
2020-07-06 09:12:57 +03:00
|
|
|
uint32_t nsid = le32_to_cpu(cmd->nsid);
|
2015-06-11 13:01:39 +03:00
|
|
|
uint32_t result;
|
2020-07-06 09:12:56 +03:00
|
|
|
uint8_t fid = NVME_GETSETFEAT_FID(dw10);
|
2020-07-06 09:12:57 +03:00
|
|
|
NvmeGetFeatureSelect sel = NVME_GETFEAT_SELECT(dw10);
|
2020-07-06 09:12:56 +03:00
|
|
|
uint16_t iv;
|
|
|
|
|
|
|
|
static const uint32_t nvme_feature_default[NVME_FID_MAX] = {
|
|
|
|
[NVME_ARBITRATION] = NVME_ARB_AB_NOLIMIT,
|
|
|
|
};
|
|
|
|
|
2020-09-30 02:19:04 +03:00
|
|
|
trace_pci_nvme_getfeat(nvme_cid(req), nsid, fid, sel, dw11);
|
2020-07-06 09:12:56 +03:00
|
|
|
|
|
|
|
if (!nvme_feature_support[fid]) {
|
|
|
|
return NVME_INVALID_FIELD | NVME_DNR;
|
|
|
|
}
|
2013-06-04 19:17:10 +04:00
|
|
|
|
2020-07-06 09:12:57 +03:00
|
|
|
if (nvme_feature_cap[fid] & NVME_FEAT_CAP_NS) {
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
if (!nvme_nsid_valid(n, nsid) || nsid == NVME_NSID_BROADCAST) {
|
2020-07-06 09:12:57 +03:00
|
|
|
/*
|
|
|
|
* The Reservation Notification Mask and Reservation Persistence
|
|
|
|
* features require a status code of Invalid Field in Command when
|
|
|
|
* NSID is 0xFFFFFFFF. Since the device does not support those
|
|
|
|
* features we can always return Invalid Namespace or Format as we
|
|
|
|
* should do for all other features.
|
|
|
|
*/
|
|
|
|
return NVME_INVALID_NSID | NVME_DNR;
|
|
|
|
}
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
|
|
|
|
if (!nvme_ns(n, nsid)) {
|
|
|
|
return NVME_INVALID_FIELD | NVME_DNR;
|
|
|
|
}
|
2020-07-06 09:12:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (sel) {
|
|
|
|
case NVME_GETFEAT_SELECT_CURRENT:
|
|
|
|
break;
|
|
|
|
case NVME_GETFEAT_SELECT_SAVED:
|
|
|
|
/* no features are saveable by the controller; fallthrough */
|
|
|
|
case NVME_GETFEAT_SELECT_DEFAULT:
|
|
|
|
goto defaults;
|
|
|
|
case NVME_GETFEAT_SELECT_CAP:
|
|
|
|
result = nvme_feature_cap[fid];
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2020-07-06 09:12:56 +03:00
|
|
|
switch (fid) {
|
2020-07-06 09:12:50 +03:00
|
|
|
case NVME_TEMPERATURE_THRESHOLD:
|
|
|
|
result = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The controller only implements the Composite Temperature sensor, so
|
|
|
|
* return 0 for all other sensors.
|
|
|
|
*/
|
|
|
|
if (NVME_TEMP_TMPSEL(dw11) != NVME_TEMP_TMPSEL_COMPOSITE) {
|
2020-07-06 09:12:57 +03:00
|
|
|
goto out;
|
2020-07-06 09:12:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (NVME_TEMP_THSEL(dw11)) {
|
|
|
|
case NVME_TEMP_THSEL_OVER:
|
|
|
|
result = n->features.temp_thresh_hi;
|
2020-07-06 09:12:57 +03:00
|
|
|
goto out;
|
2020-07-06 09:12:50 +03:00
|
|
|
case NVME_TEMP_THSEL_UNDER:
|
|
|
|
result = n->features.temp_thresh_low;
|
2020-07-06 09:12:57 +03:00
|
|
|
goto out;
|
2020-07-06 09:12:50 +03:00
|
|
|
}
|
|
|
|
|
2020-07-06 09:12:57 +03:00
|
|
|
return NVME_INVALID_FIELD | NVME_DNR;
|
2015-04-30 12:44:17 +03:00
|
|
|
case NVME_VOLATILE_WRITE_CACHE:
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
result = n->features.vwc;
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_getfeat_vwcache(result ? "enabled" : "disabled");
|
2020-07-06 09:12:57 +03:00
|
|
|
goto out;
|
|
|
|
case NVME_ASYNCHRONOUS_EVENT_CONF:
|
|
|
|
result = n->features.async_config;
|
|
|
|
goto out;
|
|
|
|
case NVME_TIMESTAMP:
|
2020-07-20 13:44:01 +03:00
|
|
|
return nvme_get_feature_timestamp(n, req);
|
2020-07-06 09:12:57 +03:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
defaults:
|
|
|
|
switch (fid) {
|
|
|
|
case NVME_TEMPERATURE_THRESHOLD:
|
|
|
|
result = 0;
|
|
|
|
|
|
|
|
if (NVME_TEMP_TMPSEL(dw11) != NVME_TEMP_TMPSEL_COMPOSITE) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NVME_TEMP_THSEL(dw11) == NVME_TEMP_THSEL_OVER) {
|
|
|
|
result = NVME_TEMPERATURE_WARNING;
|
|
|
|
}
|
|
|
|
|
2015-06-11 13:01:39 +03:00
|
|
|
break;
|
|
|
|
case NVME_NUMBER_OF_QUEUES:
|
2020-07-06 09:12:47 +03:00
|
|
|
result = (n->params.max_ioqpairs - 1) |
|
|
|
|
((n->params.max_ioqpairs - 1) << 16);
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_getfeat_numq(result);
|
2020-07-06 09:12:56 +03:00
|
|
|
break;
|
|
|
|
case NVME_INTERRUPT_VECTOR_CONF:
|
|
|
|
iv = dw11 & 0xffff;
|
|
|
|
if (iv >= n->params.max_ioqpairs + 1) {
|
|
|
|
return NVME_INVALID_FIELD | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = iv;
|
|
|
|
if (iv == n->admin_cq.vector) {
|
|
|
|
result |= NVME_INTVC_NOCOALESCING;
|
|
|
|
}
|
|
|
|
|
2015-04-30 12:44:17 +03:00
|
|
|
break;
|
2013-06-04 19:17:10 +04:00
|
|
|
default:
|
2020-07-06 09:12:56 +03:00
|
|
|
result = nvme_feature_default[fid];
|
|
|
|
break;
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
2015-06-11 13:01:39 +03:00
|
|
|
|
2020-07-06 09:12:57 +03:00
|
|
|
out:
|
2020-07-06 09:12:47 +03:00
|
|
|
req->cqe.result = cpu_to_le32(result);
|
2013-06-04 19:17:10 +04:00
|
|
|
return NVME_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static uint16_t nvme_set_feature_timestamp(NvmeCtrl *n, NvmeRequest *req)
|
2019-05-20 20:40:30 +03:00
|
|
|
{
|
|
|
|
uint16_t ret;
|
|
|
|
uint64_t timestamp;
|
|
|
|
|
2019-04-12 21:53:16 +03:00
|
|
|
ret = nvme_dma(n, (uint8_t *)×tamp, sizeof(timestamp),
|
|
|
|
DMA_DIRECTION_TO_DEVICE, req);
|
2019-05-20 20:40:30 +03:00
|
|
|
if (ret != NVME_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
nvme_set_timestamp(n, timestamp);
|
|
|
|
|
|
|
|
return NVME_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static uint16_t nvme_set_feature(NvmeCtrl *n, NvmeRequest *req)
|
2013-06-04 19:17:10 +04:00
|
|
|
{
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
NvmeNamespace *ns;
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
NvmeCmd *cmd = &req->cmd;
|
2013-06-04 19:17:10 +04:00
|
|
|
uint32_t dw10 = le32_to_cpu(cmd->cdw10);
|
2015-06-11 13:01:39 +03:00
|
|
|
uint32_t dw11 = le32_to_cpu(cmd->cdw11);
|
2020-07-06 09:12:57 +03:00
|
|
|
uint32_t nsid = le32_to_cpu(cmd->nsid);
|
2020-07-06 09:12:56 +03:00
|
|
|
uint8_t fid = NVME_GETSETFEAT_FID(dw10);
|
2020-07-06 09:12:57 +03:00
|
|
|
uint8_t save = NVME_SETFEAT_SAVE(dw10);
|
|
|
|
|
2020-09-30 02:19:04 +03:00
|
|
|
trace_pci_nvme_setfeat(nvme_cid(req), nsid, fid, save, dw11);
|
2013-06-04 19:17:10 +04:00
|
|
|
|
2020-07-06 09:12:57 +03:00
|
|
|
if (save) {
|
|
|
|
return NVME_FID_NOT_SAVEABLE | NVME_DNR;
|
|
|
|
}
|
2020-07-06 09:12:56 +03:00
|
|
|
|
|
|
|
if (!nvme_feature_support[fid]) {
|
|
|
|
return NVME_INVALID_FIELD | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
2020-07-06 09:12:57 +03:00
|
|
|
if (nvme_feature_cap[fid] & NVME_FEAT_CAP_NS) {
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
if (nsid != NVME_NSID_BROADCAST) {
|
|
|
|
if (!nvme_nsid_valid(n, nsid)) {
|
|
|
|
return NVME_INVALID_NSID | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ns = nvme_ns(n, nsid);
|
|
|
|
if (unlikely(!ns)) {
|
|
|
|
return NVME_INVALID_FIELD | NVME_DNR;
|
|
|
|
}
|
2020-07-06 09:12:57 +03:00
|
|
|
}
|
|
|
|
} else if (nsid && nsid != NVME_NSID_BROADCAST) {
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
if (!nvme_nsid_valid(n, nsid)) {
|
2020-07-06 09:12:57 +03:00
|
|
|
return NVME_INVALID_NSID | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NVME_FEAT_NOT_NS_SPEC | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(nvme_feature_cap[fid] & NVME_FEAT_CAP_CHANGE)) {
|
|
|
|
return NVME_FEAT_NOT_CHANGEABLE | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
2020-07-06 09:12:56 +03:00
|
|
|
switch (fid) {
|
2020-07-06 09:12:50 +03:00
|
|
|
case NVME_TEMPERATURE_THRESHOLD:
|
|
|
|
if (NVME_TEMP_TMPSEL(dw11) != NVME_TEMP_TMPSEL_COMPOSITE) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (NVME_TEMP_THSEL(dw11)) {
|
|
|
|
case NVME_TEMP_THSEL_OVER:
|
|
|
|
n->features.temp_thresh_hi = NVME_TEMP_TMPTH(dw11);
|
|
|
|
break;
|
|
|
|
case NVME_TEMP_THSEL_UNDER:
|
|
|
|
n->features.temp_thresh_low = NVME_TEMP_TMPTH(dw11);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NVME_INVALID_FIELD | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
2020-07-06 09:12:53 +03:00
|
|
|
if (((n->temperature >= n->features.temp_thresh_hi) ||
|
2020-08-24 09:58:56 +03:00
|
|
|
(n->temperature <= n->features.temp_thresh_low)) &&
|
2020-07-06 09:12:53 +03:00
|
|
|
NVME_AEC_SMART(n->features.async_config) & NVME_SMART_TEMPERATURE) {
|
|
|
|
nvme_enqueue_event(n, NVME_AER_TYPE_SMART,
|
|
|
|
NVME_AER_INFO_SMART_TEMP_THRESH,
|
|
|
|
NVME_LOG_SMART_INFO);
|
|
|
|
}
|
|
|
|
|
2020-07-06 09:12:50 +03:00
|
|
|
break;
|
2015-06-11 13:01:39 +03:00
|
|
|
case NVME_VOLATILE_WRITE_CACHE:
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
n->features.vwc = dw11 & 0x1;
|
|
|
|
|
|
|
|
for (int i = 1; i <= n->num_namespaces; i++) {
|
|
|
|
ns = nvme_ns(n, i);
|
|
|
|
if (!ns) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(dw11 & 0x1) && blk_enable_write_cache(ns->blkconf.blk)) {
|
|
|
|
blk_flush(ns->blkconf.blk);
|
|
|
|
}
|
|
|
|
|
|
|
|
blk_set_enable_write_cache(ns->blkconf.blk, dw11 & 1);
|
2020-07-06 09:12:55 +03:00
|
|
|
}
|
|
|
|
|
2015-06-11 13:01:39 +03:00
|
|
|
break;
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
case NVME_NUMBER_OF_QUEUES:
|
2020-07-06 09:13:01 +03:00
|
|
|
if (n->qs_created) {
|
|
|
|
return NVME_CMD_SEQ_ERROR | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
2020-07-06 09:12:58 +03:00
|
|
|
/*
|
|
|
|
* NVMe v1.3, Section 5.21.1.7: 0xffff is not an allowed value for NCQR
|
|
|
|
* and NSQR.
|
|
|
|
*/
|
|
|
|
if ((dw11 & 0xffff) == 0xffff || ((dw11 >> 16) & 0xffff) == 0xffff) {
|
|
|
|
return NVME_INVALID_FIELD | NVME_DNR;
|
|
|
|
}
|
|
|
|
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_setfeat_numq((dw11 & 0xFFFF) + 1,
|
|
|
|
((dw11 >> 16) & 0xFFFF) + 1,
|
2020-06-09 22:03:19 +03:00
|
|
|
n->params.max_ioqpairs,
|
|
|
|
n->params.max_ioqpairs);
|
|
|
|
req->cqe.result = cpu_to_le32((n->params.max_ioqpairs - 1) |
|
|
|
|
((n->params.max_ioqpairs - 1) << 16));
|
2013-06-04 19:17:10 +04:00
|
|
|
break;
|
2020-07-06 09:12:53 +03:00
|
|
|
case NVME_ASYNCHRONOUS_EVENT_CONF:
|
|
|
|
n->features.async_config = dw11;
|
|
|
|
break;
|
2019-05-20 20:40:30 +03:00
|
|
|
case NVME_TIMESTAMP:
|
2020-07-20 13:44:01 +03:00
|
|
|
return nvme_set_feature_timestamp(n, req);
|
2013-06-04 19:17:10 +04:00
|
|
|
default:
|
2020-07-06 09:12:56 +03:00
|
|
|
return NVME_FEAT_NOT_CHANGEABLE | NVME_DNR;
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
|
|
|
return NVME_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static uint16_t nvme_aer(NvmeCtrl *n, NvmeRequest *req)
|
2020-07-06 09:12:53 +03:00
|
|
|
{
|
|
|
|
trace_pci_nvme_aer(nvme_cid(req));
|
|
|
|
|
|
|
|
if (n->outstanding_aers > n->params.aerl) {
|
|
|
|
trace_pci_nvme_aer_aerl_exceeded();
|
|
|
|
return NVME_AER_LIMIT_EXCEEDED;
|
|
|
|
}
|
|
|
|
|
|
|
|
n->aer_reqs[n->outstanding_aers] = req;
|
|
|
|
n->outstanding_aers++;
|
|
|
|
|
|
|
|
if (!QTAILQ_EMPTY(&n->aer_queue)) {
|
|
|
|
nvme_process_aers(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NVME_NO_COMPLETE;
|
|
|
|
}
|
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
static uint16_t nvme_admin_cmd(NvmeCtrl *n, NvmeRequest *req)
|
2013-06-04 19:17:10 +04:00
|
|
|
{
|
2020-08-24 23:11:33 +03:00
|
|
|
trace_pci_nvme_admin_cmd(nvme_cid(req), nvme_sqid(req), req->cmd.opcode,
|
|
|
|
nvme_adm_opc_str(req->cmd.opcode));
|
2020-07-06 09:12:48 +03:00
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
switch (req->cmd.opcode) {
|
2013-06-04 19:17:10 +04:00
|
|
|
case NVME_ADM_CMD_DELETE_SQ:
|
2020-07-20 13:44:01 +03:00
|
|
|
return nvme_del_sq(n, req);
|
2013-06-04 19:17:10 +04:00
|
|
|
case NVME_ADM_CMD_CREATE_SQ:
|
2020-07-20 13:44:01 +03:00
|
|
|
return nvme_create_sq(n, req);
|
2020-07-06 09:12:52 +03:00
|
|
|
case NVME_ADM_CMD_GET_LOG_PAGE:
|
2020-07-20 13:44:01 +03:00
|
|
|
return nvme_get_log(n, req);
|
2013-06-04 19:17:10 +04:00
|
|
|
case NVME_ADM_CMD_DELETE_CQ:
|
2020-07-20 13:44:01 +03:00
|
|
|
return nvme_del_cq(n, req);
|
2013-06-04 19:17:10 +04:00
|
|
|
case NVME_ADM_CMD_CREATE_CQ:
|
2020-07-20 13:44:01 +03:00
|
|
|
return nvme_create_cq(n, req);
|
2013-06-04 19:17:10 +04:00
|
|
|
case NVME_ADM_CMD_IDENTIFY:
|
2020-07-20 13:44:01 +03:00
|
|
|
return nvme_identify(n, req);
|
2020-07-06 09:12:49 +03:00
|
|
|
case NVME_ADM_CMD_ABORT:
|
2020-07-20 13:44:01 +03:00
|
|
|
return nvme_abort(n, req);
|
2013-06-04 19:17:10 +04:00
|
|
|
case NVME_ADM_CMD_SET_FEATURES:
|
2020-07-20 13:44:01 +03:00
|
|
|
return nvme_set_feature(n, req);
|
2013-06-04 19:17:10 +04:00
|
|
|
case NVME_ADM_CMD_GET_FEATURES:
|
2020-07-20 13:44:01 +03:00
|
|
|
return nvme_get_feature(n, req);
|
2020-07-06 09:12:53 +03:00
|
|
|
case NVME_ADM_CMD_ASYNC_EV_REQ:
|
2020-07-20 13:44:01 +03:00
|
|
|
return nvme_aer(n, req);
|
2013-06-04 19:17:10 +04:00
|
|
|
default:
|
2020-07-20 13:44:01 +03:00
|
|
|
trace_pci_nvme_err_invalid_admin_opc(req->cmd.opcode);
|
2013-06-04 19:17:10 +04:00
|
|
|
return NVME_INVALID_OPCODE | NVME_DNR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nvme_process_sq(void *opaque)
|
|
|
|
{
|
|
|
|
NvmeSQueue *sq = opaque;
|
|
|
|
NvmeCtrl *n = sq->ctrl;
|
|
|
|
NvmeCQueue *cq = n->cq[sq->cqid];
|
|
|
|
|
|
|
|
uint16_t status;
|
|
|
|
hwaddr addr;
|
|
|
|
NvmeCmd cmd;
|
|
|
|
NvmeRequest *req;
|
|
|
|
|
|
|
|
while (!(nvme_sq_empty(sq) || QTAILQ_EMPTY(&sq->req_list))) {
|
|
|
|
addr = sq->dma_addr + sq->head * n->sqe_size;
|
2019-10-11 09:32:00 +03:00
|
|
|
if (nvme_addr_read(n, addr, (void *)&cmd, sizeof(cmd))) {
|
|
|
|
trace_pci_nvme_err_addr_read(addr);
|
|
|
|
trace_pci_nvme_err_cfs();
|
|
|
|
n->bar.csts = NVME_CSTS_FAILED;
|
|
|
|
break;
|
|
|
|
}
|
2013-06-04 19:17:10 +04:00
|
|
|
nvme_inc_sq_head(sq);
|
|
|
|
|
|
|
|
req = QTAILQ_FIRST(&sq->req_list);
|
|
|
|
QTAILQ_REMOVE(&sq->req_list, req, entry);
|
|
|
|
QTAILQ_INSERT_TAIL(&sq->out_req_list, req, entry);
|
2020-07-20 13:44:01 +03:00
|
|
|
nvme_req_clear(req);
|
2013-06-04 19:17:10 +04:00
|
|
|
req->cqe.cid = cmd.cid;
|
2020-07-20 13:44:01 +03:00
|
|
|
memcpy(&req->cmd, &cmd, sizeof(NvmeCmd));
|
2013-06-04 19:17:10 +04:00
|
|
|
|
2020-07-20 13:44:01 +03:00
|
|
|
status = sq->sqid ? nvme_io_cmd(n, req) :
|
|
|
|
nvme_admin_cmd(n, req);
|
2013-06-04 19:17:10 +04:00
|
|
|
if (status != NVME_NO_COMPLETE) {
|
|
|
|
req->status = status;
|
|
|
|
nvme_enqueue_req_completion(cq, req);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nvme_clear_ctrl(NvmeCtrl *n)
|
|
|
|
{
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
NvmeNamespace *ns;
|
2013-06-04 19:17:10 +04:00
|
|
|
int i;
|
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
for (i = 1; i <= n->num_namespaces; i++) {
|
|
|
|
ns = nvme_ns(n, i);
|
|
|
|
if (!ns) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nvme_ns_drain(ns);
|
|
|
|
}
|
2018-11-06 15:16:55 +03:00
|
|
|
|
2020-06-09 22:03:19 +03:00
|
|
|
for (i = 0; i < n->params.max_ioqpairs + 1; i++) {
|
2013-06-04 19:17:10 +04:00
|
|
|
if (n->sq[i] != NULL) {
|
|
|
|
nvme_free_sq(n->sq[i], n);
|
|
|
|
}
|
|
|
|
}
|
2020-06-09 22:03:19 +03:00
|
|
|
for (i = 0; i < n->params.max_ioqpairs + 1; i++) {
|
2013-06-04 19:17:10 +04:00
|
|
|
if (n->cq[i] != NULL) {
|
|
|
|
nvme_free_cq(n->cq[i], n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 09:12:53 +03:00
|
|
|
while (!QTAILQ_EMPTY(&n->aer_queue)) {
|
|
|
|
NvmeAsyncEvent *event = QTAILQ_FIRST(&n->aer_queue);
|
|
|
|
QTAILQ_REMOVE(&n->aer_queue, event, entry);
|
|
|
|
g_free(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
n->aer_queued = 0;
|
|
|
|
n->outstanding_aers = 0;
|
2020-07-06 09:13:01 +03:00
|
|
|
n->qs_created = false;
|
2020-07-06 09:12:53 +03:00
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
for (i = 1; i <= n->num_namespaces; i++) {
|
|
|
|
ns = nvme_ns(n, i);
|
|
|
|
if (!ns) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nvme_ns_flush(ns);
|
|
|
|
}
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
n->bar.cc = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nvme_start_ctrl(NvmeCtrl *n)
|
|
|
|
{
|
|
|
|
uint32_t page_bits = NVME_CC_MPS(n->bar.cc) + 12;
|
|
|
|
uint32_t page_size = 1 << page_bits;
|
|
|
|
|
2017-11-03 16:37:53 +03:00
|
|
|
if (unlikely(n->cq[0])) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_startfail_cq();
|
2017-11-03 16:37:53 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (unlikely(n->sq[0])) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_startfail_sq();
|
2017-11-03 16:37:53 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (unlikely(!n->bar.asq)) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_startfail_nbarasq();
|
2017-11-03 16:37:53 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (unlikely(!n->bar.acq)) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_startfail_nbaracq();
|
2017-11-03 16:37:53 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (unlikely(n->bar.asq & (page_size - 1))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_startfail_asq_misaligned(n->bar.asq);
|
2017-11-03 16:37:53 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (unlikely(n->bar.acq & (page_size - 1))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_startfail_acq_misaligned(n->bar.acq);
|
2017-11-03 16:37:53 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2020-09-30 20:54:05 +03:00
|
|
|
if (unlikely(!(NVME_CAP_CSS(n->bar.cap) & (1 << NVME_CC_CSS(n->bar.cc))))) {
|
|
|
|
trace_pci_nvme_err_startfail_css(NVME_CC_CSS(n->bar.cc));
|
|
|
|
return -1;
|
|
|
|
}
|
2017-11-03 16:37:53 +03:00
|
|
|
if (unlikely(NVME_CC_MPS(n->bar.cc) <
|
|
|
|
NVME_CAP_MPSMIN(n->bar.cap))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_startfail_page_too_small(
|
2017-11-03 16:37:53 +03:00
|
|
|
NVME_CC_MPS(n->bar.cc),
|
|
|
|
NVME_CAP_MPSMIN(n->bar.cap));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (unlikely(NVME_CC_MPS(n->bar.cc) >
|
|
|
|
NVME_CAP_MPSMAX(n->bar.cap))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_startfail_page_too_large(
|
2017-11-03 16:37:53 +03:00
|
|
|
NVME_CC_MPS(n->bar.cc),
|
|
|
|
NVME_CAP_MPSMAX(n->bar.cap));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (unlikely(NVME_CC_IOCQES(n->bar.cc) <
|
|
|
|
NVME_CTRL_CQES_MIN(n->id_ctrl.cqes))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_startfail_cqent_too_small(
|
2017-11-03 16:37:53 +03:00
|
|
|
NVME_CC_IOCQES(n->bar.cc),
|
|
|
|
NVME_CTRL_CQES_MIN(n->bar.cap));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (unlikely(NVME_CC_IOCQES(n->bar.cc) >
|
|
|
|
NVME_CTRL_CQES_MAX(n->id_ctrl.cqes))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_startfail_cqent_too_large(
|
2017-11-03 16:37:53 +03:00
|
|
|
NVME_CC_IOCQES(n->bar.cc),
|
|
|
|
NVME_CTRL_CQES_MAX(n->bar.cap));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (unlikely(NVME_CC_IOSQES(n->bar.cc) <
|
|
|
|
NVME_CTRL_SQES_MIN(n->id_ctrl.sqes))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_startfail_sqent_too_small(
|
2017-11-03 16:37:53 +03:00
|
|
|
NVME_CC_IOSQES(n->bar.cc),
|
|
|
|
NVME_CTRL_SQES_MIN(n->bar.cap));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (unlikely(NVME_CC_IOSQES(n->bar.cc) >
|
|
|
|
NVME_CTRL_SQES_MAX(n->id_ctrl.sqes))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_startfail_sqent_too_large(
|
2017-11-03 16:37:53 +03:00
|
|
|
NVME_CC_IOSQES(n->bar.cc),
|
|
|
|
NVME_CTRL_SQES_MAX(n->bar.cap));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (unlikely(!NVME_AQA_ASQS(n->bar.aqa))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_startfail_asqent_sz_zero();
|
2017-11-03 16:37:53 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (unlikely(!NVME_AQA_ACQS(n->bar.aqa))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_startfail_acqent_sz_zero();
|
2013-06-04 19:17:10 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
n->page_bits = page_bits;
|
|
|
|
n->page_size = page_size;
|
|
|
|
n->max_prp_ents = n->page_size / sizeof(uint64_t);
|
|
|
|
n->cqe_size = 1 << NVME_CC_IOCQES(n->bar.cc);
|
|
|
|
n->sqe_size = 1 << NVME_CC_IOSQES(n->bar.cc);
|
|
|
|
nvme_init_cq(&n->admin_cq, n, n->bar.acq, 0, 0,
|
2020-08-24 09:58:56 +03:00
|
|
|
NVME_AQA_ACQS(n->bar.aqa) + 1, 1);
|
2013-06-04 19:17:10 +04:00
|
|
|
nvme_init_sq(&n->admin_sq, n, n->bar.asq, 0, 0,
|
2020-08-24 09:58:56 +03:00
|
|
|
NVME_AQA_ASQS(n->bar.aqa) + 1);
|
2013-06-04 19:17:10 +04:00
|
|
|
|
2019-05-20 20:40:30 +03:00
|
|
|
nvme_set_timestamp(n, 0ULL);
|
|
|
|
|
2020-07-06 09:12:53 +03:00
|
|
|
QTAILQ_INIT(&n->aer_queue);
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nvme_write_bar(NvmeCtrl *n, hwaddr offset, uint64_t data,
|
2020-08-24 09:58:56 +03:00
|
|
|
unsigned size)
|
2013-06-04 19:17:10 +04:00
|
|
|
{
|
2017-11-03 16:37:53 +03:00
|
|
|
if (unlikely(offset & (sizeof(uint32_t) - 1))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
NVME_GUEST_ERR(pci_nvme_ub_mmiowr_misaligned32,
|
2017-11-03 16:37:53 +03:00
|
|
|
"MMIO write not 32-bit aligned,"
|
|
|
|
" offset=0x%"PRIx64"", offset);
|
|
|
|
/* should be ignored, fall through for now */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely(size < sizeof(uint32_t))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
NVME_GUEST_ERR(pci_nvme_ub_mmiowr_toosmall,
|
2017-11-03 16:37:53 +03:00
|
|
|
"MMIO write smaller than 32-bits,"
|
|
|
|
" offset=0x%"PRIx64", size=%u",
|
|
|
|
offset, size);
|
|
|
|
/* should be ignored, fall through for now */
|
|
|
|
}
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
switch (offset) {
|
2017-11-03 16:37:53 +03:00
|
|
|
case 0xc: /* INTMS */
|
|
|
|
if (unlikely(msix_enabled(&(n->parent_obj)))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
NVME_GUEST_ERR(pci_nvme_ub_mmiowr_intmask_with_msix,
|
2017-11-03 16:37:53 +03:00
|
|
|
"undefined access to interrupt mask set"
|
|
|
|
" when MSI-X is enabled");
|
|
|
|
/* should be ignored, fall through for now */
|
|
|
|
}
|
2013-06-04 19:17:10 +04:00
|
|
|
n->bar.intms |= data & 0xffffffff;
|
|
|
|
n->bar.intmc = n->bar.intms;
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_mmio_intm_set(data & 0xffffffff, n->bar.intmc);
|
2017-12-18 08:00:43 +03:00
|
|
|
nvme_irq_check(n);
|
2013-06-04 19:17:10 +04:00
|
|
|
break;
|
2017-11-03 16:37:53 +03:00
|
|
|
case 0x10: /* INTMC */
|
|
|
|
if (unlikely(msix_enabled(&(n->parent_obj)))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
NVME_GUEST_ERR(pci_nvme_ub_mmiowr_intmask_with_msix,
|
2017-11-03 16:37:53 +03:00
|
|
|
"undefined access to interrupt mask clr"
|
|
|
|
" when MSI-X is enabled");
|
|
|
|
/* should be ignored, fall through for now */
|
|
|
|
}
|
2013-06-04 19:17:10 +04:00
|
|
|
n->bar.intms &= ~(data & 0xffffffff);
|
|
|
|
n->bar.intmc = n->bar.intms;
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_mmio_intm_clr(data & 0xffffffff, n->bar.intmc);
|
2017-12-18 08:00:43 +03:00
|
|
|
nvme_irq_check(n);
|
2013-06-04 19:17:10 +04:00
|
|
|
break;
|
2017-11-03 16:37:53 +03:00
|
|
|
case 0x14: /* CC */
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_mmio_cfg(data & 0xffffffff);
|
2015-04-24 21:55:42 +03:00
|
|
|
/* Windows first sends data, then sends enable bit */
|
|
|
|
if (!NVME_CC_EN(data) && !NVME_CC_EN(n->bar.cc) &&
|
|
|
|
!NVME_CC_SHN(data) && !NVME_CC_SHN(n->bar.cc))
|
|
|
|
{
|
|
|
|
n->bar.cc = data;
|
|
|
|
}
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
if (NVME_CC_EN(data) && !NVME_CC_EN(n->bar.cc)) {
|
|
|
|
n->bar.cc = data;
|
2017-11-03 16:37:53 +03:00
|
|
|
if (unlikely(nvme_start_ctrl(n))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_err_startfail();
|
2013-06-04 19:17:10 +04:00
|
|
|
n->bar.csts = NVME_CSTS_FAILED;
|
|
|
|
} else {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_mmio_start_success();
|
2013-06-04 19:17:10 +04:00
|
|
|
n->bar.csts = NVME_CSTS_READY;
|
|
|
|
}
|
|
|
|
} else if (!NVME_CC_EN(data) && NVME_CC_EN(n->bar.cc)) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_mmio_stopped();
|
2013-06-04 19:17:10 +04:00
|
|
|
nvme_clear_ctrl(n);
|
|
|
|
n->bar.csts &= ~NVME_CSTS_READY;
|
|
|
|
}
|
|
|
|
if (NVME_CC_SHN(data) && !(NVME_CC_SHN(n->bar.cc))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_mmio_shutdown_set();
|
2017-11-03 16:37:53 +03:00
|
|
|
nvme_clear_ctrl(n);
|
|
|
|
n->bar.cc = data;
|
|
|
|
n->bar.csts |= NVME_CSTS_SHST_COMPLETE;
|
2013-06-04 19:17:10 +04:00
|
|
|
} else if (!NVME_CC_SHN(data) && NVME_CC_SHN(n->bar.cc)) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_mmio_shutdown_cleared();
|
2017-11-03 16:37:53 +03:00
|
|
|
n->bar.csts &= ~NVME_CSTS_SHST_COMPLETE;
|
|
|
|
n->bar.cc = data;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x1C: /* CSTS */
|
|
|
|
if (data & (1 << 4)) {
|
2020-06-09 22:03:13 +03:00
|
|
|
NVME_GUEST_ERR(pci_nvme_ub_mmiowr_ssreset_w1c_unsupported,
|
2017-11-03 16:37:53 +03:00
|
|
|
"attempted to W1C CSTS.NSSRO"
|
|
|
|
" but CAP.NSSRS is zero (not supported)");
|
|
|
|
} else if (data != 0) {
|
2020-06-09 22:03:13 +03:00
|
|
|
NVME_GUEST_ERR(pci_nvme_ub_mmiowr_ro_csts,
|
2017-11-03 16:37:53 +03:00
|
|
|
"attempted to set a read only bit"
|
|
|
|
" of controller status");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x20: /* NSSR */
|
|
|
|
if (data == 0x4E564D65) {
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_ub_mmiowr_ssreset_unsupported();
|
2017-11-03 16:37:53 +03:00
|
|
|
} else {
|
|
|
|
/* The spec says that writes of other values have no effect */
|
|
|
|
return;
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
|
|
|
break;
|
2017-11-03 16:37:53 +03:00
|
|
|
case 0x24: /* AQA */
|
2013-06-04 19:17:10 +04:00
|
|
|
n->bar.aqa = data & 0xffffffff;
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_mmio_aqattr(data & 0xffffffff);
|
2013-06-04 19:17:10 +04:00
|
|
|
break;
|
2017-11-03 16:37:53 +03:00
|
|
|
case 0x28: /* ASQ */
|
2013-06-04 19:17:10 +04:00
|
|
|
n->bar.asq = data;
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_mmio_asqaddr(data);
|
2013-06-04 19:17:10 +04:00
|
|
|
break;
|
2017-11-03 16:37:53 +03:00
|
|
|
case 0x2c: /* ASQ hi */
|
2013-06-04 19:17:10 +04:00
|
|
|
n->bar.asq |= data << 32;
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_mmio_asqaddr_hi(data, n->bar.asq);
|
2013-06-04 19:17:10 +04:00
|
|
|
break;
|
2017-11-03 16:37:53 +03:00
|
|
|
case 0x30: /* ACQ */
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_mmio_acqaddr(data);
|
2013-06-04 19:17:10 +04:00
|
|
|
n->bar.acq = data;
|
|
|
|
break;
|
2017-11-03 16:37:53 +03:00
|
|
|
case 0x34: /* ACQ hi */
|
2013-06-04 19:17:10 +04:00
|
|
|
n->bar.acq |= data << 32;
|
2020-06-09 22:03:13 +03:00
|
|
|
trace_pci_nvme_mmio_acqaddr_hi(data, n->bar.acq);
|
2013-06-04 19:17:10 +04:00
|
|
|
break;
|
2017-11-03 16:37:53 +03:00
|
|
|
case 0x38: /* CMBLOC */
|
2020-06-09 22:03:13 +03:00
|
|
|
NVME_GUEST_ERR(pci_nvme_ub_mmiowr_cmbloc_reserved,
|
2017-11-03 16:37:53 +03:00
|
|
|
"invalid write to reserved CMBLOC"
|
|
|
|
" when CMBSZ is zero, ignored");
|
|
|
|
return;
|
|
|
|
case 0x3C: /* CMBSZ */
|
2020-06-09 22:03:13 +03:00
|
|
|
NVME_GUEST_ERR(pci_nvme_ub_mmiowr_cmbsz_readonly,
|
2017-11-03 16:37:53 +03:00
|
|
|
"invalid write to read only CMBSZ, ignored");
|
|
|
|
return;
|
2020-03-30 19:46:56 +03:00
|
|
|
case 0xE00: /* PMRCAP */
|
2020-06-09 22:03:13 +03:00
|
|
|
NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrcap_readonly,
|
2020-03-30 19:46:56 +03:00
|
|
|
"invalid write to PMRCAP register, ignored");
|
|
|
|
return;
|
|
|
|
case 0xE04: /* TODO PMRCTL */
|
|
|
|
break;
|
|
|
|
case 0xE08: /* PMRSTS */
|
2020-06-09 22:03:13 +03:00
|
|
|
NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrsts_readonly,
|
2020-03-30 19:46:56 +03:00
|
|
|
"invalid write to PMRSTS register, ignored");
|
|
|
|
return;
|
|
|
|
case 0xE0C: /* PMREBS */
|
2020-06-09 22:03:13 +03:00
|
|
|
NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrebs_readonly,
|
2020-03-30 19:46:56 +03:00
|
|
|
"invalid write to PMREBS register, ignored");
|
|
|
|
return;
|
|
|
|
case 0xE10: /* PMRSWTP */
|
2020-06-09 22:03:13 +03:00
|
|
|
NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrswtp_readonly,
|
2020-03-30 19:46:56 +03:00
|
|
|
"invalid write to PMRSWTP register, ignored");
|
|
|
|
return;
|
|
|
|
case 0xE14: /* TODO PMRMSC */
|
2020-08-24 09:58:56 +03:00
|
|
|
break;
|
2013-06-04 19:17:10 +04:00
|
|
|
default:
|
2020-06-09 22:03:13 +03:00
|
|
|
NVME_GUEST_ERR(pci_nvme_ub_mmiowr_invalid,
|
2017-11-03 16:37:53 +03:00
|
|
|
"invalid MMIO write,"
|
|
|
|
" offset=0x%"PRIx64", data=%"PRIx64"",
|
|
|
|
offset, data);
|
2013-06-04 19:17:10 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t nvme_mmio_read(void *opaque, hwaddr addr, unsigned size)
|
|
|
|
{
|
|
|
|
NvmeCtrl *n = (NvmeCtrl *)opaque;
|
|
|
|
uint8_t *ptr = (uint8_t *)&n->bar;
|
|
|
|
uint64_t val = 0;
|
|
|
|
|
2020-07-06 09:12:48 +03:00
|
|
|
trace_pci_nvme_mmio_read(addr);
|
|
|
|
|
2017-11-03 16:37:53 +03:00
|
|
|
if (unlikely(addr & (sizeof(uint32_t) - 1))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
NVME_GUEST_ERR(pci_nvme_ub_mmiord_misaligned32,
|
2017-11-03 16:37:53 +03:00
|
|
|
"MMIO read not 32-bit aligned,"
|
|
|
|
" offset=0x%"PRIx64"", addr);
|
|
|
|
/* should RAZ, fall through for now */
|
|
|
|
} else if (unlikely(size < sizeof(uint32_t))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
NVME_GUEST_ERR(pci_nvme_ub_mmiord_toosmall,
|
2017-11-03 16:37:53 +03:00
|
|
|
"MMIO read smaller than 32-bits,"
|
|
|
|
" offset=0x%"PRIx64"", addr);
|
|
|
|
/* should RAZ, fall through for now */
|
|
|
|
}
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
if (addr < sizeof(n->bar)) {
|
2020-03-30 19:46:56 +03:00
|
|
|
/*
|
|
|
|
* When PMRWBM bit 1 is set then read from
|
|
|
|
* from PMRSTS should ensure prior writes
|
|
|
|
* made it to persistent media
|
|
|
|
*/
|
|
|
|
if (addr == 0xE08 &&
|
|
|
|
(NVME_PMRCAP_PMRWBM(n->bar.pmrcap) & 0x02)) {
|
2020-05-08 09:24:55 +03:00
|
|
|
memory_region_msync(&n->pmrdev->mr, 0, n->pmrdev->size);
|
2020-03-30 19:46:56 +03:00
|
|
|
}
|
2013-06-04 19:17:10 +04:00
|
|
|
memcpy(&val, ptr + addr, size);
|
2017-11-03 16:37:53 +03:00
|
|
|
} else {
|
2020-06-09 22:03:13 +03:00
|
|
|
NVME_GUEST_ERR(pci_nvme_ub_mmiord_invalid_ofs,
|
2017-11-03 16:37:53 +03:00
|
|
|
"MMIO read beyond last register,"
|
|
|
|
" offset=0x%"PRIx64", returning 0", addr);
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
2017-11-03 16:37:53 +03:00
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nvme_process_db(NvmeCtrl *n, hwaddr addr, int val)
|
|
|
|
{
|
|
|
|
uint32_t qid;
|
|
|
|
|
2017-11-03 16:37:53 +03:00
|
|
|
if (unlikely(addr & ((1 << 2) - 1))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
NVME_GUEST_ERR(pci_nvme_ub_db_wr_misaligned,
|
2017-11-03 16:37:53 +03:00
|
|
|
"doorbell write not 32-bit aligned,"
|
|
|
|
" offset=0x%"PRIx64", ignoring", addr);
|
2013-06-04 19:17:10 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (((addr - 0x1000) >> 2) & 1) {
|
2017-11-03 16:37:53 +03:00
|
|
|
/* Completion queue doorbell write */
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
uint16_t new_head = val & 0xffff;
|
|
|
|
int start_sqs;
|
|
|
|
NvmeCQueue *cq;
|
|
|
|
|
|
|
|
qid = (addr - (0x1000 + (1 << 2))) >> 3;
|
2017-11-03 16:37:53 +03:00
|
|
|
if (unlikely(nvme_check_cqid(n, qid))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_cq,
|
2017-11-03 16:37:53 +03:00
|
|
|
"completion queue doorbell write"
|
|
|
|
" for nonexistent queue,"
|
|
|
|
" sqid=%"PRIu32", ignoring", qid);
|
2020-07-06 09:12:53 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* NVM Express v1.3d, Section 4.1 state: "If host software writes
|
|
|
|
* an invalid value to the Submission Queue Tail Doorbell or
|
|
|
|
* Completion Queue Head Doorbell regiter and an Asynchronous Event
|
|
|
|
* Request command is outstanding, then an asynchronous event is
|
|
|
|
* posted to the Admin Completion Queue with a status code of
|
|
|
|
* Invalid Doorbell Write Value."
|
|
|
|
*
|
|
|
|
* Also note that the spec includes the "Invalid Doorbell Register"
|
|
|
|
* status code, but nowhere does it specify when to use it.
|
|
|
|
* However, it seems reasonable to use it here in a similar
|
|
|
|
* fashion.
|
|
|
|
*/
|
|
|
|
if (n->outstanding_aers) {
|
|
|
|
nvme_enqueue_event(n, NVME_AER_TYPE_ERROR,
|
|
|
|
NVME_AER_INFO_ERR_INVALID_DB_REGISTER,
|
|
|
|
NVME_LOG_ERROR_INFO);
|
|
|
|
}
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cq = n->cq[qid];
|
2017-11-03 16:37:53 +03:00
|
|
|
if (unlikely(new_head >= cq->size)) {
|
2020-06-09 22:03:13 +03:00
|
|
|
NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_cqhead,
|
2017-11-03 16:37:53 +03:00
|
|
|
"completion queue doorbell write value"
|
|
|
|
" beyond queue size, sqid=%"PRIu32","
|
|
|
|
" new_head=%"PRIu16", ignoring",
|
|
|
|
qid, new_head);
|
2020-07-06 09:12:53 +03:00
|
|
|
|
|
|
|
if (n->outstanding_aers) {
|
|
|
|
nvme_enqueue_event(n, NVME_AER_TYPE_ERROR,
|
|
|
|
NVME_AER_INFO_ERR_INVALID_DB_VALUE,
|
|
|
|
NVME_LOG_ERROR_INFO);
|
|
|
|
}
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-06 09:12:48 +03:00
|
|
|
trace_pci_nvme_mmio_doorbell_cq(cq->cqid, new_head);
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
start_sqs = nvme_cq_full(cq) ? 1 : 0;
|
|
|
|
cq->head = new_head;
|
|
|
|
if (start_sqs) {
|
|
|
|
NvmeSQueue *sq;
|
|
|
|
QTAILQ_FOREACH(sq, &cq->sq_list, entry) {
|
2013-08-21 19:03:08 +04:00
|
|
|
timer_mod(sq->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 500);
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
2013-08-21 19:03:08 +04:00
|
|
|
timer_mod(cq->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 500);
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
|
|
|
|
2017-12-18 08:00:43 +03:00
|
|
|
if (cq->tail == cq->head) {
|
|
|
|
nvme_irq_deassert(n, cq);
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
|
|
|
} else {
|
2017-11-03 16:37:53 +03:00
|
|
|
/* Submission queue doorbell write */
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
uint16_t new_tail = val & 0xffff;
|
|
|
|
NvmeSQueue *sq;
|
|
|
|
|
|
|
|
qid = (addr - 0x1000) >> 3;
|
2017-11-03 16:37:53 +03:00
|
|
|
if (unlikely(nvme_check_sqid(n, qid))) {
|
2020-06-09 22:03:13 +03:00
|
|
|
NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_sq,
|
2017-11-03 16:37:53 +03:00
|
|
|
"submission queue doorbell write"
|
|
|
|
" for nonexistent queue,"
|
|
|
|
" sqid=%"PRIu32", ignoring", qid);
|
2020-07-06 09:12:53 +03:00
|
|
|
|
|
|
|
if (n->outstanding_aers) {
|
|
|
|
nvme_enqueue_event(n, NVME_AER_TYPE_ERROR,
|
|
|
|
NVME_AER_INFO_ERR_INVALID_DB_REGISTER,
|
|
|
|
NVME_LOG_ERROR_INFO);
|
|
|
|
}
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sq = n->sq[qid];
|
2017-11-03 16:37:53 +03:00
|
|
|
if (unlikely(new_tail >= sq->size)) {
|
2020-06-09 22:03:13 +03:00
|
|
|
NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_sqtail,
|
2017-11-03 16:37:53 +03:00
|
|
|
"submission queue doorbell write value"
|
|
|
|
" beyond queue size, sqid=%"PRIu32","
|
|
|
|
" new_tail=%"PRIu16", ignoring",
|
|
|
|
qid, new_tail);
|
2020-07-06 09:12:53 +03:00
|
|
|
|
|
|
|
if (n->outstanding_aers) {
|
|
|
|
nvme_enqueue_event(n, NVME_AER_TYPE_ERROR,
|
|
|
|
NVME_AER_INFO_ERR_INVALID_DB_VALUE,
|
|
|
|
NVME_LOG_ERROR_INFO);
|
|
|
|
}
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-06 09:12:48 +03:00
|
|
|
trace_pci_nvme_mmio_doorbell_sq(sq->sqid, new_tail);
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
sq->tail = new_tail;
|
2013-08-21 19:03:08 +04:00
|
|
|
timer_mod(sq->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 500);
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nvme_mmio_write(void *opaque, hwaddr addr, uint64_t data,
|
2020-08-24 09:58:56 +03:00
|
|
|
unsigned size)
|
2013-06-04 19:17:10 +04:00
|
|
|
{
|
|
|
|
NvmeCtrl *n = (NvmeCtrl *)opaque;
|
2020-07-06 09:12:48 +03:00
|
|
|
|
|
|
|
trace_pci_nvme_mmio_write(addr, data);
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
if (addr < sizeof(n->bar)) {
|
|
|
|
nvme_write_bar(n, addr, data, size);
|
2020-06-30 14:04:29 +03:00
|
|
|
} else {
|
2013-06-04 19:17:10 +04:00
|
|
|
nvme_process_db(n, addr, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const MemoryRegionOps nvme_mmio_ops = {
|
|
|
|
.read = nvme_mmio_read,
|
|
|
|
.write = nvme_mmio_write,
|
|
|
|
.endianness = DEVICE_LITTLE_ENDIAN,
|
|
|
|
.impl = {
|
|
|
|
.min_access_size = 2,
|
|
|
|
.max_access_size = 8,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-05-16 22:10:59 +03:00
|
|
|
static void nvme_cmb_write(void *opaque, hwaddr addr, uint64_t data,
|
2020-08-24 09:58:56 +03:00
|
|
|
unsigned size)
|
2017-05-16 22:10:59 +03:00
|
|
|
{
|
|
|
|
NvmeCtrl *n = (NvmeCtrl *)opaque;
|
2018-11-22 21:23:35 +03:00
|
|
|
stn_le_p(&n->cmbuf[addr], size, data);
|
2017-05-16 22:10:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t nvme_cmb_read(void *opaque, hwaddr addr, unsigned size)
|
|
|
|
{
|
|
|
|
NvmeCtrl *n = (NvmeCtrl *)opaque;
|
2018-11-22 21:23:35 +03:00
|
|
|
return ldn_le_p(&n->cmbuf[addr], size);
|
2017-05-16 22:10:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static const MemoryRegionOps nvme_cmb_ops = {
|
|
|
|
.read = nvme_cmb_read,
|
|
|
|
.write = nvme_cmb_write,
|
|
|
|
.endianness = DEVICE_LITTLE_ENDIAN,
|
|
|
|
.impl = {
|
2018-11-20 21:41:48 +03:00
|
|
|
.min_access_size = 1,
|
2017-05-16 22:10:59 +03:00
|
|
|
.max_access_size = 8,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-06-09 22:03:21 +03:00
|
|
|
static void nvme_check_constraints(NvmeCtrl *n, Error **errp)
|
2013-06-04 19:17:10 +04:00
|
|
|
{
|
2020-06-09 22:03:21 +03:00
|
|
|
NvmeParams *params = &n->params;
|
2013-06-04 19:17:10 +04:00
|
|
|
|
2020-06-09 22:03:21 +03:00
|
|
|
if (params->num_queues) {
|
2020-06-09 22:03:19 +03:00
|
|
|
warn_report("num_queues is deprecated; please use max_ioqpairs "
|
|
|
|
"instead");
|
|
|
|
|
2020-06-09 22:03:21 +03:00
|
|
|
params->max_ioqpairs = params->num_queues - 1;
|
2020-06-09 22:03:19 +03:00
|
|
|
}
|
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
if (n->conf.blk) {
|
|
|
|
warn_report("drive property is deprecated; "
|
|
|
|
"please use an nvme-ns device instead");
|
|
|
|
}
|
|
|
|
|
2020-06-09 22:03:21 +03:00
|
|
|
if (params->max_ioqpairs < 1 ||
|
2020-06-09 22:03:32 +03:00
|
|
|
params->max_ioqpairs > NVME_MAX_IOQPAIRS) {
|
2020-06-09 22:03:19 +03:00
|
|
|
error_setg(errp, "max_ioqpairs must be between 1 and %d",
|
2020-06-09 22:03:32 +03:00
|
|
|
NVME_MAX_IOQPAIRS);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (params->msix_qsize < 1 ||
|
|
|
|
params->msix_qsize > PCI_MSIX_FLAGS_QSIZE + 1) {
|
|
|
|
error_setg(errp, "msix_qsize must be between 1 and %d",
|
|
|
|
PCI_MSIX_FLAGS_QSIZE + 1);
|
nvme: ensure the num_queues is not zero
When it is zero, it causes segv.
Using following command:
"-drive file=//home/test/test1.img,if=none,id=id0
-device nvme,drive=id0,serial=test,num_queues=0"
causes following Backtrack:
Thread 4 "qemu-system-x86" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffe9735700 (LWP 30952)]
0x0000555555a7a77c in nvme_start_ctrl (n=0x5555577473f0) at hw/block/nvme.c:825
825 if (unlikely(n->cq[0])) {
(gdb) bt
0 0x0000555555a7a77c in nvme_start_ctrl (n=0x5555577473f0)
at hw/block/nvme.c:825
1 0x0000555555a7af7f in nvme_write_bar (n=0x5555577473f0, offset=20,
data=4587521, size=4) at hw/block/nvme.c:969
2 0x0000555555a7b81a in nvme_mmio_write (opaque=0x5555577473f0, addr=20,
data=4587521, size=4) at hw/block/nvme.c:1163
3 0x0000555555869236 in memory_region_write_accessor (mr=0x555557747cd0,
addr=20, value=0x7fffe97320f8, size=4, shift=0, mask=4294967295, attrs=...)
at /home/test/qemu1/qemu/memory.c:502
4 0x0000555555869446 in access_with_adjusted_size (addr=20,
value=0x7fffe97320f8, size=4, access_size_min=2, access_size_max=8,
access_fn=0x55555586914d <memory_region_write_accessor>,
mr=0x555557747cd0, attrs=...) at /home/test/qemu1/qemu/memory.c:568
5 0x000055555586c479 in memory_region_dispatch_write (mr=0x555557747cd0,
addr=20, data=4587521, size=4, attrs=...)
at /home/test/qemu1/qemu/memory.c:1499
6 0x00005555558030af in flatview_write_continue (fv=0x7fffe0061130,
addr=4273930260, attrs=..., buf=0x7ffff7ff0028 "\001", len=4, addr1=20,
l=4, mr=0x555557747cd0) at /home/test/qemu1/qemu/exec.c:3234
7 0x00005555558031f9 in flatview_write (fv=0x7fffe0061130, addr=4273930260,
attrs=..., buf=0x7ffff7ff0028 "\001", len=4)
at /home/test/qemu1/qemu/exec.c:3273
8 0x00005555558034ff in address_space_write (
---Type <return> to continue, or q <return> to quit---
as=0x555556758480 <address_space_memory>, addr=4273930260, attrs=...,
buf=0x7ffff7ff0028 "\001", len=4) at /home/test/qemu1/qemu/exec.c:3363
9 0x0000555555803550 in address_space_rw (
as=0x555556758480 <address_space_memory>, addr=4273930260, attrs=...,
buf=0x7ffff7ff0028 "\001", len=4, is_write=true)
at /home/test/qemu1/qemu/exec.c:3374
10 0x00005555558884a1 in kvm_cpu_exec (cpu=0x555556920e40)
at /home/test/qemu1/qemu/accel/kvm/kvm-all.c:2031
11 0x000055555584cd9d in qemu_kvm_cpu_thread_fn (arg=0x555556920e40)
at /home/test/qemu1/qemu/cpus.c:1281
12 0x0000555555dbaf6d in qemu_thread_start (args=0x5555569438a0)
at util/qemu-thread-posix.c:502
13 0x00007ffff5dc86db in start_thread (arg=0x7fffe9735700)
at pthread_create.c:463
14 0x00007ffff5af188f in clone ()
at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
Signed-off-by: Li Qiang <liq3ea@163.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Message-id: 20190120055558.32984-3-liq3ea@163.com
Signed-off-by: Max Reitz <mreitz@redhat.com>
2019-01-20 08:55:57 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-09 22:03:21 +03:00
|
|
|
if (!params->serial) {
|
2017-11-22 06:08:43 +03:00
|
|
|
error_setg(errp, "serial property not set");
|
|
|
|
return;
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
2020-03-30 19:46:56 +03:00
|
|
|
|
2020-06-09 22:03:15 +03:00
|
|
|
if (!n->params.cmb_size_mb && n->pmrdev) {
|
2020-03-30 19:46:56 +03:00
|
|
|
if (host_memory_backend_is_mapped(n->pmrdev)) {
|
2020-07-14 19:02:00 +03:00
|
|
|
error_setg(errp, "can't use already busy memdev: %s",
|
|
|
|
object_get_canonical_path_component(OBJECT(n->pmrdev)));
|
2020-03-30 19:46:56 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_power_of_2(n->pmrdev->size)) {
|
|
|
|
error_setg(errp, "pmr backend size needs to be power of 2 in size");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
host_memory_backend_set_mapped(n->pmrdev, true);
|
|
|
|
}
|
2020-06-09 22:03:21 +03:00
|
|
|
}
|
|
|
|
|
2020-06-09 22:03:22 +03:00
|
|
|
static void nvme_init_state(NvmeCtrl *n)
|
|
|
|
{
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
n->num_namespaces = NVME_MAX_NAMESPACES;
|
2020-06-09 22:03:22 +03:00
|
|
|
/* add one to max_ioqpairs to account for the admin queue pair */
|
2020-06-30 14:04:29 +03:00
|
|
|
n->reg_size = pow2ceil(sizeof(NvmeBar) +
|
2020-06-09 22:03:22 +03:00
|
|
|
2 * (n->params.max_ioqpairs + 1) * NVME_DB_SIZE);
|
|
|
|
n->sq = g_new0(NvmeSQueue *, n->params.max_ioqpairs + 1);
|
|
|
|
n->cq = g_new0(NvmeCQueue *, n->params.max_ioqpairs + 1);
|
2020-07-06 09:12:52 +03:00
|
|
|
n->temperature = NVME_TEMPERATURE;
|
2020-07-06 09:12:50 +03:00
|
|
|
n->features.temp_thresh_hi = NVME_TEMPERATURE_WARNING;
|
2020-07-06 09:12:52 +03:00
|
|
|
n->starttime_ms = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
|
2020-07-06 09:12:53 +03:00
|
|
|
n->aer_reqs = g_new0(NvmeRequest *, n->params.aerl + 1);
|
2020-06-09 22:03:22 +03:00
|
|
|
}
|
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
int nvme_register_namespace(NvmeCtrl *n, NvmeNamespace *ns, Error **errp)
|
2020-06-09 22:03:23 +03:00
|
|
|
{
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
uint32_t nsid = nvme_nsid(ns);
|
|
|
|
|
|
|
|
if (nsid > NVME_MAX_NAMESPACES) {
|
|
|
|
error_setg(errp, "invalid namespace id (must be between 0 and %d)",
|
|
|
|
NVME_MAX_NAMESPACES);
|
|
|
|
return -1;
|
2020-05-29 01:55:10 +03:00
|
|
|
}
|
2020-06-09 22:03:23 +03:00
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
if (!nsid) {
|
|
|
|
for (int i = 1; i <= n->num_namespaces; i++) {
|
2020-11-04 13:22:46 +03:00
|
|
|
if (!nvme_ns(n, i)) {
|
2020-10-02 00:37:20 +03:00
|
|
|
nsid = ns->params.nsid = i;
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-06-09 22:03:25 +03:00
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
if (!nsid) {
|
|
|
|
error_setg(errp, "no free namespace id");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (n->namespaces[nsid - 1]) {
|
|
|
|
error_setg(errp, "namespace id '%d' is already in use", nsid);
|
|
|
|
return -1;
|
|
|
|
}
|
2020-06-09 22:03:25 +03:00
|
|
|
}
|
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
trace_pci_nvme_register_namespace(nsid);
|
2020-06-09 22:03:25 +03:00
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
n->namespaces[nsid - 1] = ns;
|
2020-06-09 22:03:25 +03:00
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
return 0;
|
2020-06-09 22:03:25 +03:00
|
|
|
}
|
|
|
|
|
2020-06-09 22:03:27 +03:00
|
|
|
static void nvme_init_cmb(NvmeCtrl *n, PCIDevice *pci_dev)
|
|
|
|
{
|
|
|
|
NVME_CMBLOC_SET_BIR(n->bar.cmbloc, NVME_CMB_BIR);
|
|
|
|
NVME_CMBLOC_SET_OFST(n->bar.cmbloc, 0);
|
|
|
|
|
|
|
|
NVME_CMBSZ_SET_SQS(n->bar.cmbsz, 1);
|
|
|
|
NVME_CMBSZ_SET_CQS(n->bar.cmbsz, 0);
|
2020-02-23 18:12:12 +03:00
|
|
|
NVME_CMBSZ_SET_LISTS(n->bar.cmbsz, 1);
|
2020-06-09 22:03:27 +03:00
|
|
|
NVME_CMBSZ_SET_RDS(n->bar.cmbsz, 1);
|
|
|
|
NVME_CMBSZ_SET_WDS(n->bar.cmbsz, 1);
|
|
|
|
NVME_CMBSZ_SET_SZU(n->bar.cmbsz, 2); /* MBs */
|
|
|
|
NVME_CMBSZ_SET_SZ(n->bar.cmbsz, n->params.cmb_size_mb);
|
|
|
|
|
|
|
|
n->cmbuf = g_malloc0(NVME_CMBSZ_GETSIZE(n->bar.cmbsz));
|
|
|
|
memory_region_init_io(&n->ctrl_mem, OBJECT(n), &nvme_cmb_ops, n,
|
|
|
|
"nvme-cmb", NVME_CMBSZ_GETSIZE(n->bar.cmbsz));
|
|
|
|
pci_register_bar(pci_dev, NVME_CMBLOC_BIR(n->bar.cmbloc),
|
|
|
|
PCI_BASE_ADDRESS_SPACE_MEMORY |
|
|
|
|
PCI_BASE_ADDRESS_MEM_TYPE_64 |
|
|
|
|
PCI_BASE_ADDRESS_MEM_PREFETCH, &n->ctrl_mem);
|
|
|
|
}
|
|
|
|
|
2020-06-09 22:03:28 +03:00
|
|
|
static void nvme_init_pmr(NvmeCtrl *n, PCIDevice *pci_dev)
|
|
|
|
{
|
|
|
|
/* Controller Capabilities register */
|
|
|
|
NVME_CAP_SET_PMRS(n->bar.cap, 1);
|
|
|
|
|
|
|
|
/* PMR Capabities register */
|
|
|
|
n->bar.pmrcap = 0;
|
|
|
|
NVME_PMRCAP_SET_RDS(n->bar.pmrcap, 0);
|
|
|
|
NVME_PMRCAP_SET_WDS(n->bar.pmrcap, 0);
|
|
|
|
NVME_PMRCAP_SET_BIR(n->bar.pmrcap, NVME_PMR_BIR);
|
|
|
|
NVME_PMRCAP_SET_PMRTU(n->bar.pmrcap, 0);
|
|
|
|
/* Turn on bit 1 support */
|
|
|
|
NVME_PMRCAP_SET_PMRWBM(n->bar.pmrcap, 0x02);
|
|
|
|
NVME_PMRCAP_SET_PMRTO(n->bar.pmrcap, 0);
|
|
|
|
NVME_PMRCAP_SET_CMSS(n->bar.pmrcap, 0);
|
|
|
|
|
|
|
|
/* PMR Control register */
|
|
|
|
n->bar.pmrctl = 0;
|
|
|
|
NVME_PMRCTL_SET_EN(n->bar.pmrctl, 0);
|
|
|
|
|
|
|
|
/* PMR Status register */
|
|
|
|
n->bar.pmrsts = 0;
|
|
|
|
NVME_PMRSTS_SET_ERR(n->bar.pmrsts, 0);
|
|
|
|
NVME_PMRSTS_SET_NRDY(n->bar.pmrsts, 0);
|
|
|
|
NVME_PMRSTS_SET_HSTS(n->bar.pmrsts, 0);
|
|
|
|
NVME_PMRSTS_SET_CBAI(n->bar.pmrsts, 0);
|
|
|
|
|
|
|
|
/* PMR Elasticity Buffer Size register */
|
|
|
|
n->bar.pmrebs = 0;
|
|
|
|
NVME_PMREBS_SET_PMRSZU(n->bar.pmrebs, 0);
|
|
|
|
NVME_PMREBS_SET_RBB(n->bar.pmrebs, 0);
|
|
|
|
NVME_PMREBS_SET_PMRWBZ(n->bar.pmrebs, 0);
|
|
|
|
|
|
|
|
/* PMR Sustained Write Throughput register */
|
|
|
|
n->bar.pmrswtp = 0;
|
|
|
|
NVME_PMRSWTP_SET_PMRSWTU(n->bar.pmrswtp, 0);
|
|
|
|
NVME_PMRSWTP_SET_PMRSWTV(n->bar.pmrswtp, 0);
|
|
|
|
|
|
|
|
/* PMR Memory Space Control register */
|
|
|
|
n->bar.pmrmsc = 0;
|
|
|
|
NVME_PMRMSC_SET_CMSE(n->bar.pmrmsc, 0);
|
|
|
|
NVME_PMRMSC_SET_CBA(n->bar.pmrmsc, 0);
|
|
|
|
|
|
|
|
pci_register_bar(pci_dev, NVME_PMRCAP_BIR(n->bar.pmrcap),
|
|
|
|
PCI_BASE_ADDRESS_SPACE_MEMORY |
|
|
|
|
PCI_BASE_ADDRESS_MEM_TYPE_64 |
|
|
|
|
PCI_BASE_ADDRESS_MEM_PREFETCH, &n->pmrdev->mr);
|
|
|
|
}
|
|
|
|
|
2020-06-09 22:03:33 +03:00
|
|
|
static void nvme_init_pci(NvmeCtrl *n, PCIDevice *pci_dev, Error **errp)
|
2020-06-09 22:03:26 +03:00
|
|
|
{
|
|
|
|
uint8_t *pci_conf = pci_dev->config;
|
|
|
|
|
|
|
|
pci_conf[PCI_INTERRUPT_PIN] = 1;
|
|
|
|
pci_config_set_prog_interface(pci_conf, 0x2);
|
2019-09-27 12:43:12 +03:00
|
|
|
|
|
|
|
if (n->params.use_intel_id) {
|
|
|
|
pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
|
|
|
|
pci_config_set_device_id(pci_conf, 0x5845);
|
|
|
|
} else {
|
|
|
|
pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_REDHAT);
|
|
|
|
pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_REDHAT_NVME);
|
|
|
|
}
|
|
|
|
|
2020-06-09 22:03:26 +03:00
|
|
|
pci_config_set_class(pci_conf, PCI_CLASS_STORAGE_EXPRESS);
|
|
|
|
pcie_endpoint_cap_init(pci_dev, 0x80);
|
|
|
|
|
|
|
|
memory_region_init_io(&n->iomem, OBJECT(n), &nvme_mmio_ops, n, "nvme",
|
|
|
|
n->reg_size);
|
|
|
|
pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY |
|
|
|
|
PCI_BASE_ADDRESS_MEM_TYPE_64, &n->iomem);
|
2020-06-09 22:03:33 +03:00
|
|
|
if (msix_init_exclusive_bar(pci_dev, n->params.msix_qsize, 4, errp)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-09 22:03:29 +03:00
|
|
|
|
|
|
|
if (n->params.cmb_size_mb) {
|
|
|
|
nvme_init_cmb(n, pci_dev);
|
|
|
|
} else if (n->pmrdev) {
|
|
|
|
nvme_init_pmr(n, pci_dev);
|
|
|
|
}
|
2020-06-09 22:03:26 +03:00
|
|
|
}
|
|
|
|
|
2020-06-09 22:03:30 +03:00
|
|
|
static void nvme_init_ctrl(NvmeCtrl *n, PCIDevice *pci_dev)
|
2020-06-09 22:03:21 +03:00
|
|
|
{
|
|
|
|
NvmeIdCtrl *id = &n->id_ctrl;
|
2020-06-09 22:03:30 +03:00
|
|
|
uint8_t *pci_conf = pci_dev->config;
|
2020-07-06 09:13:02 +03:00
|
|
|
char *subnqn;
|
2013-06-04 19:17:10 +04:00
|
|
|
|
|
|
|
id->vid = cpu_to_le16(pci_get_word(pci_conf + PCI_VENDOR_ID));
|
|
|
|
id->ssvid = cpu_to_le16(pci_get_word(pci_conf + PCI_SUBSYSTEM_VENDOR_ID));
|
|
|
|
strpadcpy((char *)id->mn, sizeof(id->mn), "QEMU NVMe Ctrl", ' ');
|
|
|
|
strpadcpy((char *)id->fr, sizeof(id->fr), "1.0", ' ');
|
2020-06-09 22:03:15 +03:00
|
|
|
strpadcpy((char *)id->sn, sizeof(id->sn), n->params.serial, ' ');
|
2013-06-04 19:17:10 +04:00
|
|
|
id->rab = 6;
|
|
|
|
id->ieee[0] = 0x00;
|
|
|
|
id->ieee[1] = 0x02;
|
|
|
|
id->ieee[2] = 0xb3;
|
2020-02-23 19:38:22 +03:00
|
|
|
id->mdts = n->params.mdts;
|
2020-07-06 09:13:03 +03:00
|
|
|
id->ver = cpu_to_le32(NVME_SPEC_VER);
|
2013-06-04 19:17:10 +04:00
|
|
|
id->oacs = cpu_to_le16(0);
|
2020-07-06 09:12:49 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Because the controller always completes the Abort command immediately,
|
|
|
|
* there can never be more than one concurrently executing Abort command,
|
|
|
|
* so this value is never used for anything. Note that there can easily be
|
|
|
|
* many Abort commands in the queues, but they are not considered
|
|
|
|
* "executing" until processed by nvme_abort.
|
|
|
|
*
|
|
|
|
* The specification recommends a value of 3 for Abort Command Limit (four
|
|
|
|
* concurrently outstanding Abort commands), so lets use that though it is
|
|
|
|
* inconsequential.
|
|
|
|
*/
|
|
|
|
id->acl = 3;
|
2020-07-06 09:12:53 +03:00
|
|
|
id->aerl = n->params.aerl;
|
2020-07-06 09:12:51 +03:00
|
|
|
id->frmw = (NVME_NUM_FW_SLOTS << 1) | NVME_FRMW_SLOT1_RO;
|
2020-09-30 20:15:50 +03:00
|
|
|
id->lpa = NVME_LPA_NS_SMART | NVME_LPA_EXTENDED;
|
2020-07-06 09:12:50 +03:00
|
|
|
|
|
|
|
/* recommended default value (~70 C) */
|
|
|
|
id->wctemp = cpu_to_le16(NVME_TEMPERATURE_WARNING);
|
|
|
|
id->cctemp = cpu_to_le16(NVME_TEMPERATURE_CRITICAL);
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
id->sqes = (0x6 << 4) | 0x6;
|
|
|
|
id->cqes = (0x4 << 4) | 0x4;
|
|
|
|
id->nn = cpu_to_le32(n->num_namespaces);
|
2020-03-31 00:10:13 +03:00
|
|
|
id->oncs = cpu_to_le16(NVME_ONCS_WRITE_ZEROES | NVME_ONCS_TIMESTAMP |
|
2020-07-06 09:12:57 +03:00
|
|
|
NVME_ONCS_FEATURES);
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
|
|
|
|
id->vwc = 0x1;
|
2020-03-18 11:41:19 +03:00
|
|
|
id->sgls = cpu_to_le32(NVME_CTRL_SGLS_SUPPORT_NO_ALIGN |
|
|
|
|
NVME_CTRL_SGLS_BITBUCKET);
|
2020-07-06 09:12:57 +03:00
|
|
|
|
2020-07-06 09:13:02 +03:00
|
|
|
subnqn = g_strdup_printf("nqn.2019-08.org.qemu:%s", n->params.serial);
|
|
|
|
strpadcpy((char *)id->subnqn, sizeof(id->subnqn), subnqn, '\0');
|
|
|
|
g_free(subnqn);
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
id->psd[0].mp = cpu_to_le16(0x9c4);
|
|
|
|
id->psd[0].enlat = cpu_to_le32(0x10);
|
|
|
|
id->psd[0].exlat = cpu_to_le32(0x4);
|
|
|
|
|
|
|
|
n->bar.cap = 0;
|
|
|
|
NVME_CAP_SET_MQES(n->bar.cap, 0x7ff);
|
|
|
|
NVME_CAP_SET_CQR(n->bar.cap, 1);
|
|
|
|
NVME_CAP_SET_TO(n->bar.cap, 0xf);
|
2020-09-30 20:54:05 +03:00
|
|
|
NVME_CAP_SET_CSS(n->bar.cap, NVME_CAP_CSS_NVM);
|
2020-09-30 20:58:03 +03:00
|
|
|
NVME_CAP_SET_CSS(n->bar.cap, NVME_CAP_CSS_ADMIN_ONLY);
|
2014-11-27 06:39:21 +03:00
|
|
|
NVME_CAP_SET_MPSMAX(n->bar.cap, 4);
|
2013-06-04 19:17:10 +04:00
|
|
|
|
2020-07-06 09:13:03 +03:00
|
|
|
n->bar.vs = NVME_SPEC_VER;
|
2013-06-04 19:17:10 +04:00
|
|
|
n->bar.intmc = n->bar.intms = 0;
|
2020-06-09 22:03:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void nvme_realize(PCIDevice *pci_dev, Error **errp)
|
|
|
|
{
|
|
|
|
NvmeCtrl *n = NVME(pci_dev);
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
NvmeNamespace *ns;
|
2020-06-09 22:03:30 +03:00
|
|
|
Error *local_err = NULL;
|
|
|
|
|
|
|
|
nvme_check_constraints(n, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
qbus_create_inplace(&n->bus, sizeof(NvmeBus), TYPE_NVME_BUS,
|
|
|
|
&pci_dev->qdev, n->parent_obj.qdev.id);
|
2020-06-09 22:03:30 +03:00
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
nvme_init_state(n);
|
2020-06-09 22:03:33 +03:00
|
|
|
nvme_init_pci(n, pci_dev, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-09 22:03:30 +03:00
|
|
|
nvme_init_ctrl(n, pci_dev);
|
2013-06-04 19:17:10 +04:00
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
/* setup a namespace if the controller drive property was given */
|
|
|
|
if (n->namespace.blkconf.blk) {
|
|
|
|
ns = &n->namespace;
|
|
|
|
ns->params.nsid = 1;
|
|
|
|
|
|
|
|
if (nvme_ns_setup(n, ns, errp)) {
|
2020-06-09 22:03:25 +03:00
|
|
|
return;
|
|
|
|
}
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nvme_exit(PCIDevice *pci_dev)
|
|
|
|
{
|
|
|
|
NvmeCtrl *n = NVME(pci_dev);
|
|
|
|
|
|
|
|
nvme_clear_ctrl(n);
|
|
|
|
g_free(n->cq);
|
|
|
|
g_free(n->sq);
|
2020-07-06 09:12:53 +03:00
|
|
|
g_free(n->aer_reqs);
|
2017-05-16 22:10:59 +03:00
|
|
|
|
2020-06-09 22:03:15 +03:00
|
|
|
if (n->params.cmb_size_mb) {
|
2018-10-29 09:29:41 +03:00
|
|
|
g_free(n->cmbuf);
|
|
|
|
}
|
2020-03-30 19:46:56 +03:00
|
|
|
|
|
|
|
if (n->pmrdev) {
|
|
|
|
host_memory_backend_set_mapped(n->pmrdev, false);
|
|
|
|
}
|
2013-06-04 19:17:10 +04:00
|
|
|
msix_uninit_exclusive_bar(pci_dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Property nvme_props[] = {
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
DEFINE_BLOCK_PROPERTIES(NvmeCtrl, namespace.blkconf),
|
2020-03-30 19:46:56 +03:00
|
|
|
DEFINE_PROP_LINK("pmrdev", NvmeCtrl, pmrdev, TYPE_MEMORY_BACKEND,
|
|
|
|
HostMemoryBackend *),
|
2020-06-09 22:03:15 +03:00
|
|
|
DEFINE_PROP_STRING("serial", NvmeCtrl, params.serial),
|
|
|
|
DEFINE_PROP_UINT32("cmb_size_mb", NvmeCtrl, params.cmb_size_mb, 0),
|
2020-06-09 22:03:19 +03:00
|
|
|
DEFINE_PROP_UINT32("num_queues", NvmeCtrl, params.num_queues, 0),
|
|
|
|
DEFINE_PROP_UINT32("max_ioqpairs", NvmeCtrl, params.max_ioqpairs, 64),
|
2020-06-09 22:03:32 +03:00
|
|
|
DEFINE_PROP_UINT16("msix_qsize", NvmeCtrl, params.msix_qsize, 65),
|
2020-07-06 09:12:53 +03:00
|
|
|
DEFINE_PROP_UINT8("aerl", NvmeCtrl, params.aerl, 3),
|
|
|
|
DEFINE_PROP_UINT32("aer_max_queued", NvmeCtrl, params.aer_max_queued, 64),
|
2020-02-23 19:38:22 +03:00
|
|
|
DEFINE_PROP_UINT8("mdts", NvmeCtrl, params.mdts, 7),
|
2019-09-27 12:43:12 +03:00
|
|
|
DEFINE_PROP_BOOL("use-intel-id", NvmeCtrl, params.use_intel_id, false),
|
2013-06-04 19:17:10 +04:00
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static const VMStateDescription nvme_vmstate = {
|
|
|
|
.name = "nvme",
|
|
|
|
.unmigratable = 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void nvme_class_init(ObjectClass *oc, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(oc);
|
|
|
|
PCIDeviceClass *pc = PCI_DEVICE_CLASS(oc);
|
|
|
|
|
2017-11-22 06:08:43 +03:00
|
|
|
pc->realize = nvme_realize;
|
2013-06-04 19:17:10 +04:00
|
|
|
pc->exit = nvme_exit;
|
|
|
|
pc->class_id = PCI_CLASS_STORAGE_EXPRESS;
|
2016-08-04 22:42:15 +03:00
|
|
|
pc->revision = 2;
|
2013-06-04 19:17:10 +04:00
|
|
|
|
2013-07-29 18:17:45 +04:00
|
|
|
set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
|
2013-06-04 19:17:10 +04:00
|
|
|
dc->desc = "Non-Volatile Memory Express";
|
2020-01-10 18:30:32 +03:00
|
|
|
device_class_set_props(dc, nvme_props);
|
2013-06-04 19:17:10 +04:00
|
|
|
dc->vmsd = &nvme_vmstate;
|
|
|
|
}
|
|
|
|
|
nvme: generate OpenFirmware device path in the "bootorder" fw_cfg file
Background on QEMU boot indices
-------------------------------
Normally, the "bootindex" property is configured for bootable devices
with:
DEVICE_instance_init()
device_add_bootindex_property(..., "bootindex", ...)
object_property_add(..., device_get_bootindex,
device_set_bootindex, ...)
and when the bootindex is set on the QEMU command line, with
-device DEVICE,...,bootindex=N
the setter that was configured above is invoked:
device_set_bootindex()
/* parse boot index */
visit_type_int32()
/* verify unicity */
check_boot_index()
/* store parsed boot index */
...
/* insert device path to boot order */
add_boot_device_path()
In the last step, add_boot_device_path() ensures that an OpenFirmware
device path will show up in the "bootorder" fw_cfg file, at a position
corresponding to the device's boot index. Thus guest firmware (SeaBIOS and
OVMF) can try to boot off the device with the right priority.
NVMe boot index
---------------
In QEMU commit 33739c712982,
nvma: ide: add bootindex to qom property
the following generic setters / getters:
- device_set_bootindex()
- device_get_bootindex()
were open-coded for NVMe, under the names
- nvme_set_bootindex()
- nvme_get_bootindex()
Plus nvme_instance_init() was added to configure the "bootindex" property
manually, designating the open-coded getter & setter, rather than calling
device_add_bootindex_property().
Crucially, nvme_set_bootindex() avoided the final add_boot_device_path()
call. This fact is spelled out in the message of commit 33739c712982, and
it was presumably the entire reason for all of the code duplication.
Now, Vladislav filed an RFE for OVMF
<https://github.com/tianocore/edk2/issues/48>; OVMF should boot off NVMe
devices. It is simple to build edk2's existent NvmExpressDxe driver into
OVMF, but the boot order matching logic in OVMF can only handle NVMe if
the "bootorder" fw_cfg file includes such devices.
Therefore this patch converts the NVMe device model to
device_set_bootindex() all the way.
Device paths
------------
device_set_bootindex() accepts an optional parameter called "suffix". When
present, it is expected to take the form of an OpenFirmware device path
node, and it gets appended as last node to the otherwise auto-generated
OFW path.
For NVMe, the auto-generated part is
/pci@i0cf8/pci8086,5845@6[,1]
^ ^ ^ ^
| | PCI slot and (present when nonzero)
| | function of the NVMe controller, both hex
| "driver name" component, built from PCI vendor & device IDs
PCI root at system bus port, PIO
to which here we append the suffix
/namespace@1,0
^ ^
| big endian (MSB at lowest address) numeric interpretation
| of the 64-bit IEEE Extended Unique Identifier, aka EUI-64,
| hex
32-bit NVMe namespace identifier, aka NSID, hex
resulting in the OFW device path
/pci@i0cf8/pci8086,5845@6[,1]/namespace@1,0
The reason for including the NSID and the EUI-64 is that an NVMe device
can in theory produce several different namespaces (distinguished by
NSID). Additionally, each of those may (optionally) have an EUI-64 value.
For now, QEMU only provides namespace 1.
Furthermore, QEMU doesn't even represent the EUI-64 as a standalone field;
it is embedded (and left unused) inside the "NvmeIdNs.res30" array, at the
last eight bytes. (Which is fine, since EUI-64 can be left zero-filled if
unsupported by the device.)
Based on the above, we set the "unit address" part of the last
("namespace") node to fixed "1,0".
OVMF will then map the above OFW device path to the following UEFI device
path fragment, for boot order processing:
PciRoot(0x0)/Pci(0x6,0x1)/NVMe(0x1,00-00-00-00-00-00-00-00)
^ ^ ^ ^ ^ ^
| | | | | octets of the EUI-64 in address order
| | | | NSID
| | | NVMe namespace messaging device path node
| PCI slot and function
PCI root bridge
Cc: Keith Busch <keith.busch@intel.com> (supporter:nvme)
Cc: Kevin Wolf <kwolf@redhat.com> (supporter:Block layer core)
Cc: qemu-block@nongnu.org (open list:nvme)
Cc: Gonglei <arei.gonglei@huawei.com>
Cc: Vladislav Vovchenko <vladislav.vovchenko@sk.com>
Cc: Feng Tian <feng.tian@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Cc: Kevin O'Connor <kevin@koconnor.net>
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Acked-by: Gonglei <arei.gonglei@huawei.com>
Acked-by: Keith Busch <keith.busch@intel.com>
Tested-by: Vladislav Vovchenko <vladislav.vovchenko@sk.com>
Message-id: 1453850483-27511-1-git-send-email-lersek@redhat.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2016-01-27 02:21:23 +03:00
|
|
|
static void nvme_instance_init(Object *obj)
|
2014-10-07 12:00:34 +04:00
|
|
|
{
|
|
|
|
NvmeCtrl *s = NVME(obj);
|
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
if (s->namespace.blkconf.blk) {
|
|
|
|
device_add_bootindex_property(obj, &s->namespace.blkconf.bootindex,
|
|
|
|
"bootindex", "/namespace@1,0",
|
|
|
|
DEVICE(obj));
|
|
|
|
}
|
2014-10-07 12:00:34 +04:00
|
|
|
}
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
static const TypeInfo nvme_info = {
|
2019-01-20 08:55:56 +03:00
|
|
|
.name = TYPE_NVME,
|
2013-06-04 19:17:10 +04:00
|
|
|
.parent = TYPE_PCI_DEVICE,
|
|
|
|
.instance_size = sizeof(NvmeCtrl),
|
2014-10-07 12:00:34 +04:00
|
|
|
.instance_init = nvme_instance_init,
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
.class_init = nvme_class_init,
|
2017-09-27 22:56:33 +03:00
|
|
|
.interfaces = (InterfaceInfo[]) {
|
|
|
|
{ INTERFACE_PCIE_DEVICE },
|
|
|
|
{ }
|
|
|
|
},
|
2013-06-04 19:17:10 +04:00
|
|
|
};
|
|
|
|
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
static const TypeInfo nvme_bus_info = {
|
|
|
|
.name = TYPE_NVME_BUS,
|
|
|
|
.parent = TYPE_BUS,
|
|
|
|
.instance_size = sizeof(NvmeBus),
|
|
|
|
};
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
static void nvme_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&nvme_info);
|
hw/block/nvme: support multiple namespaces
This adds support for multiple namespaces by introducing a new 'nvme-ns'
device model. The nvme device creates a bus named from the device name
('id'). The nvme-ns devices then connect to this and registers
themselves with the nvme device.
This changes how an nvme device is created. Example with two namespaces:
-drive file=nvme0n1.img,if=none,id=disk1
-drive file=nvme0n2.img,if=none,id=disk2
-device nvme,serial=deadbeef,id=nvme0
-device nvme-ns,drive=disk1,bus=nvme0,nsid=1
-device nvme-ns,drive=disk2,bus=nvme0,nsid=2
The drive property is kept on the nvme device to keep the change
backward compatible, but the property is now optional. Specifying a
drive for the nvme device will always create the namespace with nsid 1.
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2019-06-26 09:51:06 +03:00
|
|
|
type_register_static(&nvme_bus_info);
|
2013-06-04 19:17:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type_init(nvme_register_types)
|