2013-06-04 19:17:10 +04:00
|
|
|
#ifndef HW_NVME_H
|
|
|
|
#define HW_NVME_H
|
2020-06-09 22:03:15 +03:00
|
|
|
|
2018-01-16 09:08:59 +03:00
|
|
|
#include "block/nvme.h"
|
2021-02-04 11:55:48 +03:00
|
|
|
#include "hw/pci/pci.h"
|
hw/block/nvme: support to map controller to a subsystem
nvme controller(nvme) can be mapped to a NVMe subsystem(nvme-subsys).
This patch maps a controller to a subsystem by adding a parameter
'subsys' to the nvme device.
To map a controller to a subsystem, we need to put nvme-subsys first and
then maps the subsystem to the controller:
-device nvme-subsys,id=subsys0
-device nvme,serial=foo,id=nvme0,subsys=subsys0
If 'subsys' property is not given to the nvme controller, then subsystem
NQN will be created with serial (e.g., 'foo' in above example),
Otherwise, it will be based on subsys id (e.g., 'subsys0' in above
example).
Signed-off-by: Minwoo Im <minwoo.im.dev@gmail.com>
Tested-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
2021-01-24 05:54:46 +03:00
|
|
|
#include "nvme-subsys.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"
|
|
|
|
|
|
|
|
#define NVME_MAX_NAMESPACES 256
|
2013-06-04 19:17:10 +04:00
|
|
|
|
2020-12-08 23:04:06 +03:00
|
|
|
#define NVME_DEFAULT_ZONE_SIZE (128 * MiB)
|
|
|
|
#define NVME_DEFAULT_MAX_ZA_SIZE (128 * KiB)
|
|
|
|
|
2021-02-05 20:09:20 +03:00
|
|
|
/*
|
|
|
|
* Subsystem namespace list for allocated namespaces should be larger than
|
|
|
|
* attached namespace list in a controller.
|
|
|
|
*/
|
|
|
|
QEMU_BUILD_BUG_ON(NVME_MAX_NAMESPACES > NVME_SUBSYS_MAX_NAMESPACES);
|
|
|
|
|
2020-06-09 22:03:15 +03:00
|
|
|
typedef struct NvmeParams {
|
|
|
|
char *serial;
|
2020-06-09 22:03:19 +03:00
|
|
|
uint32_t num_queues; /* deprecated since 5.1 */
|
|
|
|
uint32_t max_ioqpairs;
|
2020-06-09 22:03:32 +03:00
|
|
|
uint16_t msix_qsize;
|
2020-06-09 22:03:15 +03:00
|
|
|
uint32_t cmb_size_mb;
|
2020-07-06 09:12:53 +03:00
|
|
|
uint8_t aerl;
|
|
|
|
uint32_t aer_max_queued;
|
2020-02-23 19:38:22 +03:00
|
|
|
uint8_t mdts;
|
2021-02-14 21:09:27 +03:00
|
|
|
uint8_t vsl;
|
2019-09-27 12:43:12 +03:00
|
|
|
bool use_intel_id;
|
hw/block/nvme: align zoned.zasl with mdts
ZASL (Zone Append Size Limit) is defined exactly like MDTS (Maximum Data
Transfer Size), that is, it is a value in units of the minimum memory
page size (CAP.MPSMIN) and is reported as a power of two.
The 'mdts' nvme device parameter is specified as in the spec, but the
'zoned.append_size_limit' parameter is specified in bytes. This is
suboptimal for a number of reasons:
1. It is just plain confusing wrt. the definition of mdts.
2. There is a lot of complexity involved in validating the value; it
must be a power of two, it should be larger than 4k, if it is zero
we set it internally to mdts, but still report it as zero.
3. While "hw/block/nvme: improve invalid zasl value reporting"
slightly improved the handling of the parameter, the validation is
still wrong; it does not depend on CC.MPS, it depends on
CAP.MPSMIN. And we are not even checking that it is actually less
than or equal to MDTS, which is kinda the *one* condition it must
satisfy.
Fix this by defining zasl exactly like mdts and checking the one thing
that it must satisfy (that it is less than or equal to mdts). Also,
change the default value from 128KiB to 0 (aka, whatever mdts is).
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
2021-02-22 21:27:58 +03:00
|
|
|
uint8_t zasl;
|
2020-12-18 02:32:16 +03:00
|
|
|
bool legacy_cmb;
|
2020-06-09 22:03:15 +03:00
|
|
|
} NvmeParams;
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
typedef struct NvmeAsyncEvent {
|
2020-07-06 09:12:53 +03:00
|
|
|
QTAILQ_ENTRY(NvmeAsyncEvent) entry;
|
2013-06-04 19:17:10 +04:00
|
|
|
NvmeAerResult result;
|
|
|
|
} NvmeAsyncEvent;
|
|
|
|
|
2021-02-07 23:06:01 +03:00
|
|
|
enum {
|
|
|
|
NVME_SG_ALLOC = 1 << 0,
|
|
|
|
NVME_SG_DMA = 1 << 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct NvmeSg {
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
union {
|
|
|
|
QEMUSGList qsg;
|
|
|
|
QEMUIOVector iov;
|
|
|
|
};
|
|
|
|
} NvmeSg;
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
typedef struct NvmeRequest {
|
|
|
|
struct NvmeSQueue *sq;
|
2020-07-20 13:44:01 +03:00
|
|
|
struct NvmeNamespace *ns;
|
2014-10-07 15:59:14 +04:00
|
|
|
BlockAIOCB *aiocb;
|
2013-06-04 19:17:10 +04:00
|
|
|
uint16_t status;
|
2020-10-21 15:03:19 +03:00
|
|
|
void *opaque;
|
2013-06-04 19:17:10 +04:00
|
|
|
NvmeCqe cqe;
|
2020-07-20 13:44:01 +03:00
|
|
|
NvmeCmd cmd;
|
2013-06-04 19:17:10 +04:00
|
|
|
BlockAcctCookie acct;
|
2021-02-07 23:06:01 +03:00
|
|
|
NvmeSg sg;
|
2013-06-04 19:17:10 +04:00
|
|
|
QTAILQ_ENTRY(NvmeRequest)entry;
|
|
|
|
} NvmeRequest;
|
|
|
|
|
2021-02-04 11:55:48 +03:00
|
|
|
typedef struct NvmeBounceContext {
|
|
|
|
NvmeRequest *req;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
QEMUIOVector iov;
|
|
|
|
uint8_t *bounce;
|
|
|
|
} data, mdata;
|
|
|
|
} NvmeBounceContext;
|
|
|
|
|
2020-08-24 23:11:33 +03:00
|
|
|
static inline const char *nvme_adm_opc_str(uint8_t opc)
|
|
|
|
{
|
|
|
|
switch (opc) {
|
|
|
|
case NVME_ADM_CMD_DELETE_SQ: return "NVME_ADM_CMD_DELETE_SQ";
|
|
|
|
case NVME_ADM_CMD_CREATE_SQ: return "NVME_ADM_CMD_CREATE_SQ";
|
|
|
|
case NVME_ADM_CMD_GET_LOG_PAGE: return "NVME_ADM_CMD_GET_LOG_PAGE";
|
|
|
|
case NVME_ADM_CMD_DELETE_CQ: return "NVME_ADM_CMD_DELETE_CQ";
|
|
|
|
case NVME_ADM_CMD_CREATE_CQ: return "NVME_ADM_CMD_CREATE_CQ";
|
|
|
|
case NVME_ADM_CMD_IDENTIFY: return "NVME_ADM_CMD_IDENTIFY";
|
|
|
|
case NVME_ADM_CMD_ABORT: return "NVME_ADM_CMD_ABORT";
|
|
|
|
case NVME_ADM_CMD_SET_FEATURES: return "NVME_ADM_CMD_SET_FEATURES";
|
|
|
|
case NVME_ADM_CMD_GET_FEATURES: return "NVME_ADM_CMD_GET_FEATURES";
|
|
|
|
case NVME_ADM_CMD_ASYNC_EV_REQ: return "NVME_ADM_CMD_ASYNC_EV_REQ";
|
2021-03-23 17:10:54 +03:00
|
|
|
case NVME_ADM_CMD_NS_ATTACHMENT: return "NVME_ADM_CMD_NS_ATTACHMENT";
|
2021-02-12 15:11:39 +03:00
|
|
|
case NVME_ADM_CMD_FORMAT_NVM: return "NVME_ADM_CMD_FORMAT_NVM";
|
2020-08-24 23:11:33 +03:00
|
|
|
default: return "NVME_ADM_CMD_UNKNOWN";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline const char *nvme_io_opc_str(uint8_t opc)
|
|
|
|
{
|
|
|
|
switch (opc) {
|
|
|
|
case NVME_CMD_FLUSH: return "NVME_NVM_CMD_FLUSH";
|
|
|
|
case NVME_CMD_WRITE: return "NVME_NVM_CMD_WRITE";
|
|
|
|
case NVME_CMD_READ: return "NVME_NVM_CMD_READ";
|
2020-12-10 01:55:06 +03:00
|
|
|
case NVME_CMD_COMPARE: return "NVME_NVM_CMD_COMPARE";
|
2020-08-24 23:11:33 +03:00
|
|
|
case NVME_CMD_WRITE_ZEROES: return "NVME_NVM_CMD_WRITE_ZEROES";
|
2020-10-21 15:03:19 +03:00
|
|
|
case NVME_CMD_DSM: return "NVME_NVM_CMD_DSM";
|
2021-02-09 20:29:42 +03:00
|
|
|
case NVME_CMD_VERIFY: return "NVME_NVM_CMD_VERIFY";
|
2020-11-06 12:46:01 +03:00
|
|
|
case NVME_CMD_COPY: return "NVME_NVM_CMD_COPY";
|
2020-12-10 01:55:06 +03:00
|
|
|
case NVME_CMD_ZONE_MGMT_SEND: return "NVME_ZONED_CMD_MGMT_SEND";
|
|
|
|
case NVME_CMD_ZONE_MGMT_RECV: return "NVME_ZONED_CMD_MGMT_RECV";
|
|
|
|
case NVME_CMD_ZONE_APPEND: return "NVME_ZONED_CMD_ZONE_APPEND";
|
2020-08-24 23:11:33 +03:00
|
|
|
default: return "NVME_NVM_CMD_UNKNOWN";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
typedef struct NvmeSQueue {
|
|
|
|
struct NvmeCtrl *ctrl;
|
|
|
|
uint16_t sqid;
|
|
|
|
uint16_t cqid;
|
|
|
|
uint32_t head;
|
|
|
|
uint32_t tail;
|
|
|
|
uint32_t size;
|
|
|
|
uint64_t dma_addr;
|
|
|
|
QEMUTimer *timer;
|
|
|
|
NvmeRequest *io_req;
|
2018-12-06 13:58:10 +03:00
|
|
|
QTAILQ_HEAD(, NvmeRequest) req_list;
|
|
|
|
QTAILQ_HEAD(, NvmeRequest) out_req_list;
|
2013-06-04 19:17:10 +04:00
|
|
|
QTAILQ_ENTRY(NvmeSQueue) entry;
|
|
|
|
} NvmeSQueue;
|
|
|
|
|
|
|
|
typedef struct NvmeCQueue {
|
|
|
|
struct NvmeCtrl *ctrl;
|
|
|
|
uint8_t phase;
|
|
|
|
uint16_t cqid;
|
|
|
|
uint16_t irq_enabled;
|
|
|
|
uint32_t head;
|
|
|
|
uint32_t tail;
|
|
|
|
uint32_t vector;
|
|
|
|
uint32_t size;
|
|
|
|
uint64_t dma_addr;
|
|
|
|
QEMUTimer *timer;
|
2018-12-06 13:58:10 +03:00
|
|
|
QTAILQ_HEAD(, NvmeSQueue) sq_list;
|
|
|
|
QTAILQ_HEAD(, NvmeRequest) req_list;
|
2013-06-04 19:17:10 +04:00
|
|
|
} NvmeCQueue;
|
|
|
|
|
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 TYPE_NVME_BUS "nvme-bus"
|
|
|
|
#define NVME_BUS(obj) OBJECT_CHECK(NvmeBus, (obj), TYPE_NVME_BUS)
|
2020-06-09 22:03:24 +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
|
|
|
typedef struct NvmeBus {
|
|
|
|
BusState parent_bus;
|
|
|
|
} NvmeBus;
|
2020-08-24 09:59:41 +03:00
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
#define TYPE_NVME "nvme"
|
|
|
|
#define NVME(obj) \
|
|
|
|
OBJECT_CHECK(NvmeCtrl, (obj), TYPE_NVME)
|
|
|
|
|
2020-07-06 09:12:54 +03:00
|
|
|
typedef struct NvmeFeatureVal {
|
|
|
|
struct {
|
|
|
|
uint16_t temp_thresh_hi;
|
|
|
|
uint16_t temp_thresh_low;
|
|
|
|
};
|
|
|
|
uint32_t async_config;
|
|
|
|
} NvmeFeatureVal;
|
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
typedef struct NvmeCtrl {
|
|
|
|
PCIDevice parent_obj;
|
2020-11-13 11:50:33 +03:00
|
|
|
MemoryRegion bar0;
|
2013-06-04 19:17:10 +04:00
|
|
|
MemoryRegion iomem;
|
|
|
|
NvmeBar bar;
|
2020-06-09 22:03:15 +03:00
|
|
|
NvmeParams params;
|
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
|
|
|
NvmeBus bus;
|
|
|
|
BlockConf conf;
|
2013-06-04 19:17:10 +04:00
|
|
|
|
2021-01-24 05:54:48 +03:00
|
|
|
uint16_t cntlid;
|
2020-07-06 09:13:01 +03:00
|
|
|
bool qs_created;
|
2014-11-27 06:39:21 +03:00
|
|
|
uint32_t page_size;
|
2013-06-04 19:17:10 +04:00
|
|
|
uint16_t page_bits;
|
|
|
|
uint16_t max_prp_ents;
|
|
|
|
uint16_t cqe_size;
|
|
|
|
uint16_t sqe_size;
|
|
|
|
uint32_t reg_size;
|
|
|
|
uint32_t num_namespaces;
|
|
|
|
uint32_t max_q_ents;
|
2020-07-06 09:12:53 +03:00
|
|
|
uint8_t outstanding_aers;
|
2020-06-09 22:03:18 +03:00
|
|
|
uint32_t irq_status;
|
2019-05-20 20:40:30 +03:00
|
|
|
uint64_t host_timestamp; /* Timestamp sent by the host */
|
|
|
|
uint64_t timestamp_set_qemu_clock_ms; /* QEMU clock time */
|
2020-07-06 09:12:52 +03:00
|
|
|
uint64_t starttime_ms;
|
|
|
|
uint16_t temperature;
|
2021-01-15 06:27:01 +03:00
|
|
|
uint8_t smart_critical_warning;
|
2013-06-04 19:17:10 +04:00
|
|
|
|
2020-12-18 02:32:16 +03:00
|
|
|
struct {
|
|
|
|
MemoryRegion mem;
|
|
|
|
uint8_t *buf;
|
|
|
|
bool cmse;
|
|
|
|
hwaddr cba;
|
|
|
|
} cmb;
|
|
|
|
|
2020-11-13 08:30:05 +03:00
|
|
|
struct {
|
|
|
|
HostMemoryBackend *dev;
|
|
|
|
bool cmse;
|
|
|
|
hwaddr cba;
|
|
|
|
} pmr;
|
2020-03-30 19:46:56 +03:00
|
|
|
|
2020-07-06 09:12:53 +03:00
|
|
|
uint8_t aer_mask;
|
|
|
|
NvmeRequest **aer_reqs;
|
|
|
|
QTAILQ_HEAD(, NvmeAsyncEvent) aer_queue;
|
|
|
|
int aer_queued;
|
|
|
|
|
2021-02-21 21:39:36 +03:00
|
|
|
uint32_t dmrsl;
|
|
|
|
|
2021-02-28 11:51:02 +03:00
|
|
|
/* Namespace ID is started with 1 so bitmap should be 1-based */
|
|
|
|
#define NVME_CHANGED_NSID_SIZE (NVME_MAX_NAMESPACES + 1)
|
|
|
|
DECLARE_BITMAP(changed_nsids, NVME_CHANGED_NSID_SIZE);
|
|
|
|
|
hw/block/nvme: support to map controller to a subsystem
nvme controller(nvme) can be mapped to a NVMe subsystem(nvme-subsys).
This patch maps a controller to a subsystem by adding a parameter
'subsys' to the nvme device.
To map a controller to a subsystem, we need to put nvme-subsys first and
then maps the subsystem to the controller:
-device nvme-subsys,id=subsys0
-device nvme,serial=foo,id=nvme0,subsys=subsys0
If 'subsys' property is not given to the nvme controller, then subsystem
NQN will be created with serial (e.g., 'foo' in above example),
Otherwise, it will be based on subsys id (e.g., 'subsys0' in above
example).
Signed-off-by: Minwoo Im <minwoo.im.dev@gmail.com>
Tested-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
2021-01-24 05:54:46 +03:00
|
|
|
NvmeSubsystem *subsys;
|
|
|
|
|
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 namespace;
|
hw/block/nvme: support namespace detach
Given that now we have nvme-subsys device supported, we can manage
namespace allocated, but not attached: detached. This patch introduced
a parameter for nvme-ns device named 'detached'. This parameter
indicates whether the given namespace device is detached from
a entire NVMe subsystem('subsys' given case, shared namespace) or a
controller('bus' given case, private namespace).
- Allocated namespace
1) Shared ns in the subsystem 'subsys0':
-device nvme-ns,id=ns1,drive=blknvme0,nsid=1,subsys=subsys0,detached=true
2) Private ns for the controller 'nvme0' of the subsystem 'subsys0':
-device nvme-subsys,id=subsys0
-device nvme,serial=foo,id=nvme0,subsys=subsys0
-device nvme-ns,id=ns1,drive=blknvme0,nsid=1,bus=nvme0,detached=true
3) (Invalid case) Controller 'nvme0' has no subsystem to manage ns:
-device nvme,serial=foo,id=nvme0
-device nvme-ns,id=ns1,drive=blknvme0,nsid=1,bus=nvme0,detached=true
Signed-off-by: Minwoo Im <minwoo.im.dev@gmail.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
2021-02-05 18:30:10 +03:00
|
|
|
/*
|
|
|
|
* Attached namespaces to this controller. If subsys is not given, all
|
|
|
|
* namespaces in this list will always be attached.
|
|
|
|
*/
|
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 *namespaces[NVME_MAX_NAMESPACES];
|
2013-06-04 19:17:10 +04:00
|
|
|
NvmeSQueue **sq;
|
|
|
|
NvmeCQueue **cq;
|
|
|
|
NvmeSQueue admin_sq;
|
|
|
|
NvmeCQueue admin_cq;
|
|
|
|
NvmeIdCtrl id_ctrl;
|
2020-07-06 09:12:50 +03:00
|
|
|
NvmeFeatureVal features;
|
2013-06-04 19:17:10 +04:00
|
|
|
} NvmeCtrl;
|
|
|
|
|
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 inline NvmeNamespace *nvme_ns(NvmeCtrl *n, uint32_t nsid)
|
2020-06-09 22:03:24 +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 || nsid > n->num_namespaces) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n->namespaces[nsid - 1];
|
2020-06-09 22:03:24 +03:00
|
|
|
}
|
|
|
|
|
hw/block/nvme: support namespace detach
Given that now we have nvme-subsys device supported, we can manage
namespace allocated, but not attached: detached. This patch introduced
a parameter for nvme-ns device named 'detached'. This parameter
indicates whether the given namespace device is detached from
a entire NVMe subsystem('subsys' given case, shared namespace) or a
controller('bus' given case, private namespace).
- Allocated namespace
1) Shared ns in the subsystem 'subsys0':
-device nvme-ns,id=ns1,drive=blknvme0,nsid=1,subsys=subsys0,detached=true
2) Private ns for the controller 'nvme0' of the subsystem 'subsys0':
-device nvme-subsys,id=subsys0
-device nvme,serial=foo,id=nvme0,subsys=subsys0
-device nvme-ns,id=ns1,drive=blknvme0,nsid=1,bus=nvme0,detached=true
3) (Invalid case) Controller 'nvme0' has no subsystem to manage ns:
-device nvme,serial=foo,id=nvme0
-device nvme-ns,id=ns1,drive=blknvme0,nsid=1,bus=nvme0,detached=true
Signed-off-by: Minwoo Im <minwoo.im.dev@gmail.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
2021-02-05 18:30:10 +03:00
|
|
|
static inline bool nvme_ns_is_attached(NvmeCtrl *n, NvmeNamespace *ns)
|
|
|
|
{
|
|
|
|
int nsid;
|
|
|
|
|
|
|
|
for (nsid = 1; nsid <= n->num_namespaces; nsid++) {
|
|
|
|
if (nvme_ns(n, nsid) == ns) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void nvme_ns_attach(NvmeCtrl *n, NvmeNamespace *ns)
|
|
|
|
{
|
2021-03-15 10:41:41 +03:00
|
|
|
uint32_t nsid = nvme_nsid(ns);
|
|
|
|
assert(nsid && nsid <= NVME_MAX_NAMESPACES);
|
|
|
|
|
|
|
|
n->namespaces[nsid - 1] = ns;
|
hw/block/nvme: support namespace detach
Given that now we have nvme-subsys device supported, we can manage
namespace allocated, but not attached: detached. This patch introduced
a parameter for nvme-ns device named 'detached'. This parameter
indicates whether the given namespace device is detached from
a entire NVMe subsystem('subsys' given case, shared namespace) or a
controller('bus' given case, private namespace).
- Allocated namespace
1) Shared ns in the subsystem 'subsys0':
-device nvme-ns,id=ns1,drive=blknvme0,nsid=1,subsys=subsys0,detached=true
2) Private ns for the controller 'nvme0' of the subsystem 'subsys0':
-device nvme-subsys,id=subsys0
-device nvme,serial=foo,id=nvme0,subsys=subsys0
-device nvme-ns,id=ns1,drive=blknvme0,nsid=1,bus=nvme0,detached=true
3) (Invalid case) Controller 'nvme0' has no subsystem to manage ns:
-device nvme,serial=foo,id=nvme0
-device nvme-ns,id=ns1,drive=blknvme0,nsid=1,bus=nvme0,detached=true
Signed-off-by: Minwoo Im <minwoo.im.dev@gmail.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
2021-02-05 18:30:10 +03:00
|
|
|
}
|
|
|
|
|
2021-02-06 06:18:09 +03:00
|
|
|
static inline void nvme_ns_detach(NvmeCtrl *n, NvmeNamespace *ns)
|
|
|
|
{
|
2021-03-15 10:41:41 +03:00
|
|
|
uint32_t nsid = nvme_nsid(ns);
|
|
|
|
assert(nsid && nsid <= NVME_MAX_NAMESPACES);
|
|
|
|
|
|
|
|
n->namespaces[nsid - 1] = NULL;
|
2021-02-06 06:18:09 +03:00
|
|
|
}
|
|
|
|
|
2020-08-24 13:43:38 +03:00
|
|
|
static inline NvmeCQueue *nvme_cq(NvmeRequest *req)
|
|
|
|
{
|
|
|
|
NvmeSQueue *sq = req->sq;
|
|
|
|
NvmeCtrl *n = sq->ctrl;
|
|
|
|
|
|
|
|
return n->cq[sq->cqid];
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline NvmeCtrl *nvme_ctrl(NvmeRequest *req)
|
|
|
|
{
|
|
|
|
NvmeSQueue *sq = req->sq;
|
|
|
|
return sq->ctrl;
|
|
|
|
}
|
|
|
|
|
2021-02-04 11:55:48 +03:00
|
|
|
static inline uint16_t nvme_cid(NvmeRequest *req)
|
|
|
|
{
|
|
|
|
if (!req) {
|
|
|
|
return 0xffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
return le16_to_cpu(req->cqe.cid);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef enum NvmeTxDirection {
|
|
|
|
NVME_TX_DIRECTION_TO_DEVICE = 0,
|
|
|
|
NVME_TX_DIRECTION_FROM_DEVICE = 1,
|
|
|
|
} NvmeTxDirection;
|
|
|
|
|
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);
|
2021-02-04 11:55:48 +03:00
|
|
|
uint16_t nvme_bounce_data(NvmeCtrl *n, uint8_t *ptr, uint32_t len,
|
|
|
|
NvmeTxDirection dir, NvmeRequest *req);
|
|
|
|
uint16_t nvme_bounce_mdata(NvmeCtrl *n, uint8_t *ptr, uint32_t len,
|
|
|
|
NvmeTxDirection dir, NvmeRequest *req);
|
|
|
|
void nvme_rw_complete_cb(void *opaque, int ret);
|
|
|
|
uint16_t nvme_map_dptr(NvmeCtrl *n, NvmeSg *sg, size_t len,
|
|
|
|
NvmeCmd *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
|
|
|
|
2013-06-04 19:17:10 +04:00
|
|
|
#endif /* HW_NVME_H */
|