2021-04-14 20:10:01 +03:00
|
|
|
/*
|
|
|
|
* QEMU NVM Express
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Intel Corporation
|
|
|
|
* Copyright (c) 2021 Minwoo Im
|
|
|
|
* Copyright (c) 2021 Samsung Electronics Co., Ltd.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Keith Busch <kbusch@kernel.org>
|
|
|
|
* Klaus Jensen <k.jensen@samsung.com>
|
|
|
|
* Gollu Appalanaidu <anaidu.gollu@samsung.com>
|
|
|
|
* Dmitry Fomichev <dmitry.fomichev@wdc.com>
|
|
|
|
* Minwoo Im <minwoo.im.dev@gmail.com>
|
|
|
|
*
|
|
|
|
* This code is licensed under the GNU GPL v2 or later.
|
|
|
|
*/
|
|
|
|
|
2022-05-06 16:49:08 +03:00
|
|
|
#ifndef HW_NVME_NVME_H
|
|
|
|
#define HW_NVME_NVME_H
|
2020-06-09 22:03:15 +03:00
|
|
|
|
2021-04-14 20:10:01 +03:00
|
|
|
#include "qemu/uuid.h"
|
2022-12-22 13:03:28 +03:00
|
|
|
#include "hw/pci/pci_device.h"
|
2021-04-14 20:10:01 +03:00
|
|
|
#include "hw/block/block.h"
|
|
|
|
|
|
|
|
#include "block/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
|
|
|
|
2022-05-09 17:16:09 +03:00
|
|
|
#define NVME_MAX_CONTROLLERS 256
|
2021-04-14 20:10:01 +03:00
|
|
|
#define NVME_MAX_NAMESPACES 256
|
2021-06-14 23:19:01 +03:00
|
|
|
#define NVME_EUI64_DEFAULT ((uint64_t)0x5254000000000000)
|
2023-02-20 14:59:26 +03:00
|
|
|
#define NVME_FDP_MAX_EVENTS 63
|
|
|
|
#define NVME_FDP_MAXPIDS 128
|
2020-12-08 23:04:06 +03:00
|
|
|
|
2023-07-19 21:21:58 +03:00
|
|
|
/*
|
|
|
|
* The controller only supports Submission and Completion Queue Entry Sizes of
|
|
|
|
* 64 and 16 bytes respectively.
|
|
|
|
*/
|
|
|
|
#define NVME_SQES 6
|
|
|
|
#define NVME_CQES 4
|
|
|
|
|
2021-06-17 22:06:47 +03:00
|
|
|
QEMU_BUILD_BUG_ON(NVME_MAX_NAMESPACES > NVME_NSID_BROADCAST - 1);
|
|
|
|
|
2021-04-14 20:10:01 +03:00
|
|
|
typedef struct NvmeCtrl NvmeCtrl;
|
|
|
|
typedef struct NvmeNamespace NvmeNamespace;
|
|
|
|
|
2021-04-23 19:55:11 +03:00
|
|
|
#define TYPE_NVME_BUS "nvme-bus"
|
|
|
|
OBJECT_DECLARE_SIMPLE_TYPE(NvmeBus, NVME_BUS)
|
|
|
|
|
|
|
|
typedef struct NvmeBus {
|
|
|
|
BusState parent_bus;
|
|
|
|
} NvmeBus;
|
|
|
|
|
2021-04-14 20:10:01 +03:00
|
|
|
#define TYPE_NVME_SUBSYS "nvme-subsys"
|
|
|
|
#define NVME_SUBSYS(obj) \
|
|
|
|
OBJECT_CHECK(NvmeSubsystem, (obj), TYPE_NVME_SUBSYS)
|
2022-05-09 17:16:11 +03:00
|
|
|
#define SUBSYS_SLOT_RSVD (void *)0xFFFF
|
2021-04-14 20:10:01 +03:00
|
|
|
|
2023-02-20 14:59:26 +03:00
|
|
|
typedef struct NvmeReclaimUnit {
|
|
|
|
uint64_t ruamw;
|
|
|
|
} NvmeReclaimUnit;
|
|
|
|
|
|
|
|
typedef struct NvmeRuHandle {
|
|
|
|
uint8_t ruht;
|
|
|
|
uint8_t ruha;
|
|
|
|
uint64_t event_filter;
|
|
|
|
uint8_t lbafi;
|
|
|
|
uint64_t ruamw;
|
|
|
|
|
|
|
|
/* reclaim units indexed by reclaim group */
|
|
|
|
NvmeReclaimUnit *rus;
|
|
|
|
} NvmeRuHandle;
|
|
|
|
|
|
|
|
typedef struct NvmeFdpEventBuffer {
|
|
|
|
NvmeFdpEvent events[NVME_FDP_MAX_EVENTS];
|
|
|
|
unsigned int nelems;
|
|
|
|
unsigned int start;
|
|
|
|
unsigned int next;
|
|
|
|
} NvmeFdpEventBuffer;
|
|
|
|
|
2023-02-20 14:59:24 +03:00
|
|
|
typedef struct NvmeEnduranceGroup {
|
|
|
|
uint8_t event_conf;
|
2023-02-20 14:59:26 +03:00
|
|
|
|
|
|
|
struct {
|
|
|
|
NvmeFdpEventBuffer host_events, ctrl_events;
|
|
|
|
|
|
|
|
uint16_t nruh;
|
|
|
|
uint16_t nrg;
|
|
|
|
uint8_t rgif;
|
|
|
|
uint64_t runs;
|
|
|
|
|
|
|
|
uint64_t hbmw;
|
|
|
|
uint64_t mbmw;
|
|
|
|
uint64_t mbe;
|
|
|
|
|
|
|
|
bool enabled;
|
|
|
|
|
|
|
|
NvmeRuHandle *ruhs;
|
|
|
|
} fdp;
|
2023-02-20 14:59:24 +03:00
|
|
|
} NvmeEnduranceGroup;
|
|
|
|
|
2021-04-14 20:10:01 +03:00
|
|
|
typedef struct NvmeSubsystem {
|
|
|
|
DeviceState parent_obj;
|
2021-04-23 19:55:11 +03:00
|
|
|
NvmeBus bus;
|
2021-04-14 20:10:01 +03:00
|
|
|
uint8_t subnqn[256];
|
2022-04-29 11:33:32 +03:00
|
|
|
char *serial;
|
2021-04-14 20:10:01 +03:00
|
|
|
|
2023-02-20 14:59:26 +03:00
|
|
|
NvmeCtrl *ctrls[NVME_MAX_CONTROLLERS];
|
|
|
|
NvmeNamespace *namespaces[NVME_MAX_NAMESPACES + 1];
|
|
|
|
NvmeEnduranceGroup endgrp;
|
2021-04-14 20:10:01 +03:00
|
|
|
|
|
|
|
struct {
|
|
|
|
char *nqn;
|
2023-02-20 14:59:26 +03:00
|
|
|
|
|
|
|
struct {
|
|
|
|
bool enabled;
|
|
|
|
uint64_t runs;
|
|
|
|
uint16_t nruh;
|
|
|
|
uint32_t nrg;
|
|
|
|
} fdp;
|
2021-04-14 20:10:01 +03:00
|
|
|
} params;
|
|
|
|
} NvmeSubsystem;
|
|
|
|
|
|
|
|
int nvme_subsys_register_ctrl(NvmeCtrl *n, Error **errp);
|
2021-07-06 11:51:36 +03:00
|
|
|
void nvme_subsys_unregister_ctrl(NvmeSubsystem *subsys, NvmeCtrl *n);
|
2021-04-14 20:10:01 +03:00
|
|
|
|
|
|
|
static inline NvmeCtrl *nvme_subsys_ctrl(NvmeSubsystem *subsys,
|
|
|
|
uint32_t cntlid)
|
|
|
|
{
|
|
|
|
if (!subsys || cntlid >= NVME_MAX_CONTROLLERS) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-05-09 17:16:11 +03:00
|
|
|
if (subsys->ctrls[cntlid] == SUBSYS_SLOT_RSVD) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-04-14 20:10:01 +03:00
|
|
|
return subsys->ctrls[cntlid];
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline NvmeNamespace *nvme_subsys_ns(NvmeSubsystem *subsys,
|
|
|
|
uint32_t nsid)
|
|
|
|
{
|
|
|
|
if (!subsys || !nsid || nsid > NVME_MAX_NAMESPACES) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return subsys->namespaces[nsid];
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TYPE_NVME_NS "nvme-ns"
|
|
|
|
#define NVME_NS(obj) \
|
|
|
|
OBJECT_CHECK(NvmeNamespace, (obj), TYPE_NVME_NS)
|
|
|
|
|
|
|
|
typedef struct NvmeZone {
|
|
|
|
NvmeZoneDescr d;
|
|
|
|
uint64_t w_ptr;
|
|
|
|
QTAILQ_ENTRY(NvmeZone) entry;
|
|
|
|
} NvmeZone;
|
|
|
|
|
2023-02-20 14:59:26 +03:00
|
|
|
#define FDP_EVT_MAX 0xff
|
|
|
|
#define NVME_FDP_MAX_NS_RUHS 32u
|
|
|
|
#define FDPVSS 0
|
|
|
|
|
|
|
|
static const uint8_t nvme_fdp_evf_shifts[FDP_EVT_MAX] = {
|
|
|
|
/* Host events */
|
|
|
|
[FDP_EVT_RU_NOT_FULLY_WRITTEN] = 0,
|
|
|
|
[FDP_EVT_RU_ATL_EXCEEDED] = 1,
|
|
|
|
[FDP_EVT_CTRL_RESET_RUH] = 2,
|
|
|
|
[FDP_EVT_INVALID_PID] = 3,
|
|
|
|
/* CTRL events */
|
|
|
|
[FDP_EVT_MEDIA_REALLOC] = 32,
|
|
|
|
[FDP_EVT_RUH_IMPLICIT_RU_CHANGE] = 33,
|
|
|
|
};
|
|
|
|
|
2024-02-22 20:50:16 +03:00
|
|
|
#define NGUID_LEN 16
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint8_t data[NGUID_LEN];
|
|
|
|
} NvmeNGUID;
|
|
|
|
|
|
|
|
bool nvme_nguid_is_null(const NvmeNGUID *nguid);
|
|
|
|
|
|
|
|
extern const PropertyInfo qdev_prop_nguid;
|
|
|
|
|
|
|
|
#define DEFINE_PROP_NGUID_NODEFAULT(_name, _state, _field) \
|
|
|
|
DEFINE_PROP(_name, _state, _field, qdev_prop_nguid, NvmeNGUID)
|
|
|
|
|
2021-04-14 20:10:01 +03:00
|
|
|
typedef struct NvmeNamespaceParams {
|
2024-02-22 20:50:16 +03:00
|
|
|
bool detached;
|
|
|
|
bool shared;
|
|
|
|
uint32_t nsid;
|
|
|
|
QemuUUID uuid;
|
|
|
|
NvmeNGUID nguid;
|
|
|
|
uint64_t eui64;
|
|
|
|
bool eui64_default;
|
2021-04-14 20:10:01 +03:00
|
|
|
|
|
|
|
uint16_t ms;
|
|
|
|
uint8_t mset;
|
|
|
|
uint8_t pi;
|
|
|
|
uint8_t pil;
|
2021-11-16 16:26:52 +03:00
|
|
|
uint8_t pif;
|
2021-04-14 20:10:01 +03:00
|
|
|
|
|
|
|
uint16_t mssrl;
|
|
|
|
uint32_t mcl;
|
|
|
|
uint8_t msrc;
|
|
|
|
|
|
|
|
bool zoned;
|
|
|
|
bool cross_zone_read;
|
|
|
|
uint64_t zone_size_bs;
|
|
|
|
uint64_t zone_cap_bs;
|
|
|
|
uint32_t max_active_zones;
|
|
|
|
uint32_t max_open_zones;
|
|
|
|
uint32_t zd_extension_size;
|
2021-03-04 10:40:11 +03:00
|
|
|
|
|
|
|
uint32_t numzrwa;
|
|
|
|
uint64_t zrwas;
|
|
|
|
uint64_t zrwafg;
|
2023-02-20 14:59:26 +03:00
|
|
|
|
|
|
|
struct {
|
|
|
|
char *ruhs;
|
|
|
|
} fdp;
|
2021-04-14 20:10:01 +03:00
|
|
|
} NvmeNamespaceParams;
|
|
|
|
|
2024-09-27 00:24:58 +03:00
|
|
|
typedef struct NvmeAtomic {
|
|
|
|
uint32_t atomic_max_write_size;
|
|
|
|
bool atomic_writes;
|
|
|
|
} NvmeAtomic;
|
|
|
|
|
2021-04-14 20:10:01 +03:00
|
|
|
typedef struct NvmeNamespace {
|
|
|
|
DeviceState parent_obj;
|
|
|
|
BlockConf blkconf;
|
|
|
|
int32_t bootindex;
|
|
|
|
int64_t size;
|
2021-04-13 22:51:30 +03:00
|
|
|
int64_t moff;
|
2021-04-14 20:10:01 +03:00
|
|
|
NvmeIdNs id_ns;
|
2021-11-16 16:26:52 +03:00
|
|
|
NvmeIdNsNvm id_ns_nvm;
|
2021-04-14 22:34:44 +03:00
|
|
|
NvmeLBAF lbaf;
|
2021-10-06 09:53:30 +03:00
|
|
|
unsigned int nlbaf;
|
2021-04-14 22:34:44 +03:00
|
|
|
size_t lbasz;
|
2021-04-14 20:10:01 +03:00
|
|
|
const uint32_t *iocs;
|
|
|
|
uint8_t csi;
|
|
|
|
uint16_t status;
|
|
|
|
int attached;
|
2021-11-16 16:26:52 +03:00
|
|
|
uint8_t pif;
|
2021-04-14 20:10:01 +03:00
|
|
|
|
2021-03-04 10:40:11 +03:00
|
|
|
struct {
|
|
|
|
uint16_t zrwas;
|
|
|
|
uint16_t zrwafg;
|
|
|
|
uint32_t numzrwa;
|
|
|
|
} zns;
|
|
|
|
|
2021-04-14 20:10:01 +03:00
|
|
|
QTAILQ_ENTRY(NvmeNamespace) entry;
|
|
|
|
|
|
|
|
NvmeIdNsZoned *id_ns_zoned;
|
|
|
|
NvmeZone *zone_array;
|
|
|
|
QTAILQ_HEAD(, NvmeZone) exp_open_zones;
|
|
|
|
QTAILQ_HEAD(, NvmeZone) imp_open_zones;
|
|
|
|
QTAILQ_HEAD(, NvmeZone) closed_zones;
|
|
|
|
QTAILQ_HEAD(, NvmeZone) full_zones;
|
|
|
|
uint32_t num_zones;
|
|
|
|
uint64_t zone_size;
|
|
|
|
uint64_t zone_capacity;
|
|
|
|
uint32_t zone_size_log2;
|
|
|
|
uint8_t *zd_extensions;
|
|
|
|
int32_t nr_open_zones;
|
|
|
|
int32_t nr_active_zones;
|
|
|
|
|
|
|
|
NvmeNamespaceParams params;
|
2023-02-20 14:59:23 +03:00
|
|
|
NvmeSubsystem *subsys;
|
2023-02-20 14:59:26 +03:00
|
|
|
NvmeEnduranceGroup *endgrp;
|
2021-04-14 20:10:01 +03:00
|
|
|
|
|
|
|
struct {
|
|
|
|
uint32_t err_rec;
|
|
|
|
} features;
|
2023-02-20 14:59:26 +03:00
|
|
|
|
|
|
|
struct {
|
|
|
|
uint16_t nphs;
|
|
|
|
/* reclaim unit handle identifiers indexed by placement handle */
|
|
|
|
uint16_t *phs;
|
|
|
|
} fdp;
|
2021-04-14 20:10:01 +03:00
|
|
|
} NvmeNamespace;
|
|
|
|
|
|
|
|
static inline uint32_t nvme_nsid(NvmeNamespace *ns)
|
|
|
|
{
|
|
|
|
if (ns) {
|
|
|
|
return ns->params.nsid;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline size_t nvme_l2b(NvmeNamespace *ns, uint64_t lba)
|
|
|
|
{
|
2021-04-14 22:34:44 +03:00
|
|
|
return lba << ns->lbaf.ds;
|
2021-04-14 20:10:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline size_t nvme_m2b(NvmeNamespace *ns, uint64_t lba)
|
|
|
|
{
|
2021-04-14 22:34:44 +03:00
|
|
|
return ns->lbaf.ms * lba;
|
2021-04-14 20:10:01 +03:00
|
|
|
}
|
|
|
|
|
2021-04-13 22:51:30 +03:00
|
|
|
static inline int64_t nvme_moff(NvmeNamespace *ns, uint64_t lba)
|
|
|
|
{
|
|
|
|
return ns->moff + nvme_m2b(ns, lba);
|
|
|
|
}
|
|
|
|
|
2021-04-14 20:10:01 +03:00
|
|
|
static inline bool nvme_ns_ext(NvmeNamespace *ns)
|
|
|
|
{
|
|
|
|
return !!NVME_ID_NS_FLBAS_EXTENDED(ns->id_ns.flbas);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline NvmeZoneState nvme_get_zone_state(NvmeZone *zone)
|
|
|
|
{
|
|
|
|
return zone->d.zs >> 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void nvme_set_zone_state(NvmeZone *zone, NvmeZoneState state)
|
|
|
|
{
|
|
|
|
zone->d.zs = state << 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint64_t nvme_zone_rd_boundary(NvmeNamespace *ns, NvmeZone *zone)
|
|
|
|
{
|
|
|
|
return zone->d.zslba + ns->zone_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint64_t nvme_zone_wr_boundary(NvmeZone *zone)
|
|
|
|
{
|
|
|
|
return zone->d.zslba + zone->d.zcap;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool nvme_wp_is_valid(NvmeZone *zone)
|
|
|
|
{
|
|
|
|
uint8_t st = nvme_get_zone_state(zone);
|
|
|
|
|
|
|
|
return st != NVME_ZONE_STATE_FULL &&
|
|
|
|
st != NVME_ZONE_STATE_READ_ONLY &&
|
|
|
|
st != NVME_ZONE_STATE_OFFLINE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint8_t *nvme_get_zd_extension(NvmeNamespace *ns,
|
|
|
|
uint32_t zone_idx)
|
|
|
|
{
|
|
|
|
return &ns->zd_extensions[zone_idx * ns->params.zd_extension_size];
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void nvme_aor_inc_open(NvmeNamespace *ns)
|
|
|
|
{
|
|
|
|
assert(ns->nr_open_zones >= 0);
|
|
|
|
if (ns->params.max_open_zones) {
|
|
|
|
ns->nr_open_zones++;
|
|
|
|
assert(ns->nr_open_zones <= ns->params.max_open_zones);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void nvme_aor_dec_open(NvmeNamespace *ns)
|
|
|
|
{
|
|
|
|
if (ns->params.max_open_zones) {
|
|
|
|
assert(ns->nr_open_zones > 0);
|
|
|
|
ns->nr_open_zones--;
|
|
|
|
}
|
|
|
|
assert(ns->nr_open_zones >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void nvme_aor_inc_active(NvmeNamespace *ns)
|
|
|
|
{
|
|
|
|
assert(ns->nr_active_zones >= 0);
|
|
|
|
if (ns->params.max_active_zones) {
|
|
|
|
ns->nr_active_zones++;
|
|
|
|
assert(ns->nr_active_zones <= ns->params.max_active_zones);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void nvme_aor_dec_active(NvmeNamespace *ns)
|
|
|
|
{
|
|
|
|
if (ns->params.max_active_zones) {
|
|
|
|
assert(ns->nr_active_zones > 0);
|
|
|
|
ns->nr_active_zones--;
|
|
|
|
assert(ns->nr_active_zones >= ns->nr_open_zones);
|
|
|
|
}
|
|
|
|
assert(ns->nr_active_zones >= 0);
|
|
|
|
}
|
|
|
|
|
2023-02-20 14:59:26 +03:00
|
|
|
static inline void nvme_fdp_stat_inc(uint64_t *a, uint64_t b)
|
|
|
|
{
|
|
|
|
uint64_t ret = *a + b;
|
|
|
|
*a = ret < *a ? UINT64_MAX : ret;
|
|
|
|
}
|
|
|
|
|
2021-04-14 20:10:01 +03:00
|
|
|
void nvme_ns_init_format(NvmeNamespace *ns);
|
2021-07-06 10:10:56 +03:00
|
|
|
int nvme_ns_setup(NvmeNamespace *ns, Error **errp);
|
2021-04-14 20:10:01 +03:00
|
|
|
void nvme_ns_drain(NvmeNamespace *ns);
|
|
|
|
void nvme_ns_shutdown(NvmeNamespace *ns);
|
|
|
|
void nvme_ns_cleanup(NvmeNamespace *ns);
|
2020-06-09 22:03:15 +03:00
|
|
|
|
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;
|
|
|
|
|
2021-04-14 20:10:01 +03:00
|
|
|
typedef enum NvmeTxDirection {
|
|
|
|
NVME_TX_DIRECTION_TO_DEVICE = 0,
|
|
|
|
NVME_TX_DIRECTION_FROM_DEVICE = 1,
|
|
|
|
} NvmeTxDirection;
|
|
|
|
|
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;
|
2024-09-27 00:24:58 +03:00
|
|
|
bool atomic_write;
|
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";
|
2023-02-20 14:59:25 +03:00
|
|
|
case NVME_ADM_CMD_DIRECTIVE_SEND: return "NVME_ADM_CMD_DIRECTIVE_SEND";
|
2022-05-09 17:16:17 +03:00
|
|
|
case NVME_ADM_CMD_VIRT_MNGMT: return "NVME_ADM_CMD_VIRT_MNGMT";
|
2023-02-20 14:59:25 +03:00
|
|
|
case NVME_ADM_CMD_DIRECTIVE_RECV: return "NVME_ADM_CMD_DIRECTIVE_RECV";
|
2022-06-16 15:34:07 +03:00
|
|
|
case NVME_ADM_CMD_DBBUF_CONFIG: return "NVME_ADM_CMD_DBBUF_CONFIG";
|
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;
|
2022-06-16 15:34:07 +03:00
|
|
|
uint64_t db_addr;
|
|
|
|
uint64_t ei_addr;
|
2022-10-19 23:28:02 +03:00
|
|
|
QEMUBH *bh;
|
2022-07-05 17:24:03 +03:00
|
|
|
EventNotifier notifier;
|
|
|
|
bool ioeventfd_enabled;
|
2013-06-04 19:17:10 +04:00
|
|
|
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;
|
2022-06-16 15:34:07 +03:00
|
|
|
uint64_t db_addr;
|
|
|
|
uint64_t ei_addr;
|
2022-10-19 23:28:02 +03:00
|
|
|
QEMUBH *bh;
|
2022-07-05 17:24:03 +03:00
|
|
|
EventNotifier notifier;
|
|
|
|
bool ioeventfd_enabled;
|
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;
|
|
|
|
|
|
|
|
#define TYPE_NVME "nvme"
|
|
|
|
#define NVME(obj) \
|
|
|
|
OBJECT_CHECK(NvmeCtrl, (obj), TYPE_NVME)
|
|
|
|
|
2021-04-14 20:10:01 +03:00
|
|
|
typedef struct NvmeParams {
|
|
|
|
char *serial;
|
|
|
|
uint32_t num_queues; /* deprecated since 5.1 */
|
|
|
|
uint32_t max_ioqpairs;
|
|
|
|
uint16_t msix_qsize;
|
2024-04-04 15:04:18 +03:00
|
|
|
uint16_t mqes;
|
2021-04-14 20:10:01 +03:00
|
|
|
uint32_t cmb_size_mb;
|
|
|
|
uint8_t aerl;
|
|
|
|
uint32_t aer_max_queued;
|
|
|
|
uint8_t mdts;
|
|
|
|
uint8_t vsl;
|
|
|
|
bool use_intel_id;
|
|
|
|
uint8_t zasl;
|
2021-05-28 14:05:07 +03:00
|
|
|
bool auto_transition_zones;
|
2021-04-14 20:10:01 +03:00
|
|
|
bool legacy_cmb;
|
2022-07-05 17:24:03 +03:00
|
|
|
bool ioeventfd;
|
2024-05-29 15:42:33 +03:00
|
|
|
uint16_t sriov_max_vfs;
|
2022-05-09 17:16:16 +03:00
|
|
|
uint16_t sriov_vq_flexible;
|
|
|
|
uint16_t sriov_vi_flexible;
|
2024-05-29 15:42:34 +03:00
|
|
|
uint32_t sriov_max_vq_per_vf;
|
|
|
|
uint32_t sriov_max_vi_per_vf;
|
2024-03-10 13:43:55 +03:00
|
|
|
bool msix_exclusive_bar;
|
2024-09-24 09:35:40 +03:00
|
|
|
|
|
|
|
struct {
|
|
|
|
bool mem;
|
|
|
|
} ctratt;
|
2024-09-27 00:24:58 +03:00
|
|
|
|
|
|
|
uint16_t atomic_awun;
|
|
|
|
uint16_t atomic_awupf;
|
|
|
|
bool atomic_dn;
|
2021-04-14 20:10:01 +03:00
|
|
|
} NvmeParams;
|
2020-07-06 09:12:54 +03:00
|
|
|
|
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;
|
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;
|
|
|
|
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;
|
2021-06-17 21:55:42 +03:00
|
|
|
int cq_pending;
|
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;
|
hw/nvme: Make max_ioqpairs and msix_qsize configurable in runtime
The NVMe device defines two properties: max_ioqpairs, msix_qsize. Having
them as constants is problematic for SR-IOV support.
SR-IOV introduces virtual resources (queues, interrupts) that can be
assigned to PF and its dependent VFs. Each device, following a reset,
should work with the configured number of queues. A single constant is
no longer sufficient to hold the whole state.
This patch tries to solve the problem by introducing additional
variables in NvmeCtrl’s state. The variables for, e.g., managing queues
are therefore organized as:
- n->params.max_ioqpairs – no changes, constant set by the user
- n->(mutable_state) – (not a part of this patch) user-configurable,
specifies number of queues available _after_
reset
- n->conf_ioqpairs - (new) used in all the places instead of the ‘old’
n->params.max_ioqpairs; initialized in realize()
and updated during reset() to reflect user’s
changes to the mutable state
Since the number of available i/o queues and interrupts can change in
runtime, buffers for sq/cqs and the MSIX-related structures are
allocated big enough to handle the limits, to completely avoid the
complicated reallocation. A helper function (nvme_update_msixcap_ts)
updates the corresponding capability register, to signal configuration
changes.
Signed-off-by: Łukasz Gieryk <lukasz.gieryk@linux.intel.com>
Reviewed-by: Klaus Jensen <k.jensen@samsung.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
2022-05-09 17:16:13 +03:00
|
|
|
uint32_t conf_msix_qsize;
|
|
|
|
uint32_t conf_ioqpairs;
|
2022-06-16 15:34:07 +03:00
|
|
|
uint64_t dbbuf_dbs;
|
|
|
|
uint64_t dbbuf_eis;
|
|
|
|
bool dbbuf_enabled;
|
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;
|
2021-04-14 22:40:40 +03:00
|
|
|
NvmeNamespace *namespaces[NVME_MAX_NAMESPACES + 1];
|
2013-06-04 19:17:10 +04:00
|
|
|
NvmeSQueue **sq;
|
|
|
|
NvmeCQueue **cq;
|
|
|
|
NvmeSQueue admin_sq;
|
|
|
|
NvmeCQueue admin_cq;
|
|
|
|
NvmeIdCtrl id_ctrl;
|
2021-04-14 20:10:01 +03:00
|
|
|
|
|
|
|
struct {
|
|
|
|
struct {
|
|
|
|
uint16_t temp_thresh_hi;
|
|
|
|
uint16_t temp_thresh_low;
|
|
|
|
};
|
2021-10-06 09:50:49 +03:00
|
|
|
|
|
|
|
uint32_t async_config;
|
|
|
|
NvmeHostBehaviorSupport hbs;
|
2021-04-14 20:10:01 +03:00
|
|
|
} features;
|
2022-05-09 17:16:10 +03:00
|
|
|
|
|
|
|
NvmePriCtrlCap pri_ctrl_cap;
|
2024-05-29 15:42:32 +03:00
|
|
|
uint32_t nr_sec_ctrls;
|
2024-05-29 15:42:33 +03:00
|
|
|
NvmeSecCtrlEntry *sec_ctrl_list;
|
2022-05-09 17:16:17 +03:00
|
|
|
struct {
|
|
|
|
uint16_t vqrfap;
|
|
|
|
uint16_t virfap;
|
|
|
|
} next_pri_ctrl_cap; /* These override pri_ctrl_cap after reset */
|
2024-09-27 00:24:58 +03:00
|
|
|
uint32_t dn; /* Disable Normal */
|
|
|
|
NvmeAtomic atomic;
|
2013-06-04 19:17:10 +04:00
|
|
|
} NvmeCtrl;
|
|
|
|
|
hw/nvme: Implement the Function Level Reset
This patch implements the Function Level Reset, a feature currently not
implemented for the Nvme device, while listed as a mandatory ("shall")
in the 1.4 spec.
The implementation reuses FLR-related building blocks defined for the
pci-bridge module, and follows the same logic:
- FLR capability is advertised in the PCIE config,
- custom pci_write_config callback detects a write to the trigger
register and performs the PCI reset,
- which, eventually, calls the custom dc->reset handler.
Depending on reset type, parts of the state should (or should not) be
cleared. To distinguish the type of reset, an additional parameter is
passed to the reset function.
This patch also enables advertisement of the Power Management PCI
capability. The main reason behind it is to announce the no_soft_reset=1
bit, to signal SR-IOV support where each VF can be reset individually.
The implementation purposedly ignores writes to the PMCS.PS register,
as even such naïve behavior is enough to correctly handle the D3->D0
transition.
It’s worth to note, that the power state transition back to to D3, with
all the corresponding side effects, wasn't and stil isn't handled
properly.
Signed-off-by: Łukasz Gieryk <lukasz.gieryk@linux.intel.com>
Reviewed-by: Klaus Jensen <k.jensen@samsung.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
2022-05-09 17:16:12 +03:00
|
|
|
typedef enum NvmeResetType {
|
|
|
|
NVME_RESET_FUNCTION = 0,
|
|
|
|
NVME_RESET_CONTROLLER = 1,
|
|
|
|
} NvmeResetType;
|
|
|
|
|
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
|
|
|
{
|
2021-04-14 20:10:01 +03:00
|
|
|
if (!nsid || nsid > NVME_MAX_NAMESPACES) {
|
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 NULL;
|
|
|
|
}
|
|
|
|
|
2021-04-14 22:40:40 +03:00
|
|
|
return n->namespaces[nsid];
|
2020-06-09 22:03:24 +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);
|
|
|
|
}
|
|
|
|
|
2022-05-09 17:16:11 +03:00
|
|
|
static inline NvmeSecCtrlEntry *nvme_sctrl(NvmeCtrl *n)
|
|
|
|
{
|
|
|
|
PCIDevice *pci_dev = &n->parent_obj;
|
|
|
|
NvmeCtrl *pf = NVME(pcie_sriov_get_pf(pci_dev));
|
|
|
|
|
|
|
|
if (pci_is_vf(pci_dev)) {
|
2024-05-29 15:42:32 +03:00
|
|
|
return &pf->sec_ctrl_list[pcie_sriov_vf_number(pci_dev)];
|
2022-05-09 17:16:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-05-09 17:16:17 +03:00
|
|
|
static inline NvmeSecCtrlEntry *nvme_sctrl_for_cntlid(NvmeCtrl *n,
|
|
|
|
uint16_t cntlid)
|
|
|
|
{
|
2024-05-29 15:42:32 +03:00
|
|
|
NvmeSecCtrlEntry *list = n->sec_ctrl_list;
|
2022-05-09 17:16:17 +03:00
|
|
|
uint8_t i;
|
|
|
|
|
2024-05-29 15:42:32 +03:00
|
|
|
for (i = 0; i < n->nr_sec_ctrls; i++) {
|
|
|
|
if (le16_to_cpu(list[i].scid) == cntlid) {
|
|
|
|
return &list[i];
|
2022-05-09 17:16:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
hw/block/nvme: fix handling of private namespaces
Prior to this patch, if a private nvme-ns device (that is, a namespace
that is not linked to a subsystem) is wired up to an nvme-subsys linked
nvme controller device, the device fails to verify that the namespace id
is unique within the subsystem. NVM Express v1.4b, Section 6.1.6 ("NSID
and Namespace Usage") states that because the device supports Namespace
Management, "NSIDs *shall* be unique within the NVM subsystem".
Additionally, prior to this patch, private namespaces are not known to
the subsystem and the namespace is considered exclusive to the
controller with which it is initially wired up to. However, this is not
the definition of a private namespace; per Section 1.6.33 ("private
namespace"), a private namespace is just a namespace that does not
support multipath I/O or namespace sharing, which means "that it is only
able to be attached to one controller at a time".
Fix this by always allocating namespaces in the subsystem (if one is
linked to the controller), regardless of the shared/private status of
the namespace. Whether or not the namespace is shareable is controlled
by a new `shared` nvme-ns parameter.
Finally, this fix allows the nvme-ns `subsys` parameter to be removed,
since the `shared` parameter now serves the purpose of attaching the
namespace to all controllers in the subsystem upon device realization.
It is invalid to have an nvme-ns namespace device with a linked
subsystem without the parent nvme controller device also being linked to
one and since the nvme-ns devices will unconditionally be "attached" (in
QEMU terms that is) to an nvme controller device through an NvmeBus, the
nvme-ns namespace device can always get a reference to the subsystem of
the controller it is explicitly (using 'bus=' parameter) or implicitly
attaching to.
Fixes: e570768566b3 ("hw/block/nvme: support for shared namespace in subsystem")
Cc: Minwoo Im <minwoo.im.dev@gmail.com>
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Gollu Appalanaidu <anaidu.gollu@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Minwoo Im <minwoo.im.dev@gmail.com>
2021-03-23 14:43:24 +03:00
|
|
|
void nvme_attach_ns(NvmeCtrl *n, NvmeNamespace *ns);
|
2021-11-11 18:45:52 +03:00
|
|
|
uint16_t nvme_bounce_data(NvmeCtrl *n, void *ptr, uint32_t len,
|
2021-02-04 11:55:48 +03:00
|
|
|
NvmeTxDirection dir, NvmeRequest *req);
|
2021-11-11 18:45:52 +03:00
|
|
|
uint16_t nvme_bounce_mdata(NvmeCtrl *n, void *ptr, uint32_t len,
|
2021-02-04 11:55:48 +03:00
|
|
|
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
|
|
|
|
2022-05-06 16:49:08 +03:00
|
|
|
#endif /* HW_NVME_NVME_H */
|