From: Klaus Jensen <k.jen...@samsung.com> Add a basic user creatable object that models an NVMe NVM subsystem.
Signed-off-by: Klaus Jensen <k.jen...@samsung.com> --- hw/nvme/ctrl.c | 14 ++--- hw/nvme/ns.c | 5 +- hw/nvme/nvme.h | 10 +++- hw/nvme/subsys.c | 144 +++++++++++++++++++++++++++++++++++++++++++++-- qapi/qom.json | 17 ++++++ 5 files changed, 175 insertions(+), 15 deletions(-) diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c index b27bf00f623f..c798aeb095e1 100644 --- a/hw/nvme/ctrl.c +++ b/hw/nvme/ctrl.c @@ -6562,14 +6562,14 @@ static void nvme_realize(PCIDevice *pci_dev, Error **errp) NvmeCtrl *ctrl = NVME_DEVICE(pci_dev); NvmeState *n = NVME_STATE(ctrl); - if (n->subsys_dev) { - n->subsys = &n->subsys_dev->subsys; - } + if (ctrl->subsys_dev) { + if (ctrl->namespace.blkconf.blk) { + error_setg(errp, "subsystem support is unavailable with legacy " + "namespace ('drive' property)"); + return; + } - if (ctrl->namespace.blkconf.blk && n->subsys) { - error_setg(errp, "subsystem support is unavailable with legacy " - "namespace ('drive' property)"); - return; + n->subsys = &ctrl->subsys_dev->subsys; } if (nvme_check_constraints(n, errp)) { diff --git a/hw/nvme/ns.c b/hw/nvme/ns.c index efbe9b3808e4..09556f0ec7c9 100644 --- a/hw/nvme/ns.c +++ b/hw/nvme/ns.c @@ -493,7 +493,8 @@ static void nvme_nsdev_realize(DeviceState *dev, Error **errp) NvmeNamespaceDevice *nsdev = NVME_NAMESPACE_DEVICE(dev); NvmeNamespace *ns = &nsdev->ns; BusState *s = qdev_get_parent_bus(dev); - NvmeState *n = NVME_STATE(s->parent); + NvmeCtrl *ctrl = NVME_DEVICE(s->parent); + NvmeState *n = NVME_STATE(ctrl); NvmeSubsystem *subsys = n->subsys; uint32_t nsid = nsdev->params.nsid; int i; @@ -509,7 +510,7 @@ static void nvme_nsdev_realize(DeviceState *dev, Error **errp) * If this namespace belongs to a subsystem (through a link on the * controller device), reparent the device. */ - if (!qdev_set_parent_bus(dev, &n->subsys_dev->bus.parent_bus, errp)) { + if (!qdev_set_parent_bus(dev, &ctrl->subsys_dev->bus.parent_bus, errp)) { return; } } diff --git a/hw/nvme/nvme.h b/hw/nvme/nvme.h index a0c34a49772f..b67e5900a01d 100644 --- a/hw/nvme/nvme.h +++ b/hw/nvme/nvme.h @@ -45,8 +45,14 @@ typedef struct NvmeBus { BusState parent_bus; } NvmeBus; +#define TYPE_NVME_SUBSYSTEM "x-nvme-subsystem" +OBJECT_DECLARE_SIMPLE_TYPE(NvmeSubsystem, NVME_SUBSYSTEM) + typedef struct NvmeSubsystem { - uint8_t subnqn[256]; + Object parent_obj; + + QemuUUID uuid; + uint8_t subnqn[256]; NvmeState *ctrls[NVME_MAX_CONTROLLERS]; NvmeNamespace *namespaces[NVME_MAX_NAMESPACES + 1]; @@ -467,6 +473,8 @@ typedef struct NvmeCtrl { /* for use with legacy single namespace (-device nvme,drive=...) setups */ NvmeNamespaceDevice namespace; + + NvmeSubsystemDevice *subsys_dev; } NvmeCtrl; static inline NvmeNamespace *nvme_ns(NvmeState *n, uint32_t nsid) diff --git a/hw/nvme/subsys.c b/hw/nvme/subsys.c index 3e1736d846b7..2599b83c348e 100644 --- a/hw/nvme/subsys.c +++ b/hw/nvme/subsys.c @@ -8,10 +8,13 @@ #include "qemu/osdep.h" #include "qapi/error.h" +#include "qapi/qapi-builtin-visit.h" +#include "qom/object_interfaces.h" #include "nvme.h" -int nvme_subsys_register_ctrl(NvmeSubsystem *subsys, NvmeState *n, Error **errp) +int nvme_subsys_register_ctrl(NvmeSubsystem *subsys, NvmeState *n, + Error **errp) { int cntlid, nsid; @@ -44,6 +47,136 @@ void nvme_subsys_unregister_ctrl(NvmeSubsystem *subsys, NvmeState *n) n->cntlid = -1; } +static void get_controllers(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + NvmeSubsystem *subsys = NVME_SUBSYSTEM(obj); + strList *paths = NULL; + strList **tail = &paths; + unsigned cntlid; + + for (cntlid = 0; cntlid < NVME_MAX_CONTROLLERS; cntlid++) { + NvmeState *n = subsys->ctrls[cntlid]; + if (!n) { + continue; + } + + QAPI_LIST_APPEND(tail, object_get_canonical_path(OBJECT(n))); + } + + visit_type_strList(v, name, &paths, errp); + qapi_free_strList(paths); +} + +static void get_namespaces(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + NvmeSubsystem *subsys = NVME_SUBSYSTEM(obj); + strList *paths = NULL; + strList **tail = &paths; + unsigned nsid; + + for (nsid = 1; nsid <= NVME_MAX_NAMESPACES; nsid++) { + NvmeNamespace *ns = subsys->namespaces[nsid]; + if (!ns) { + continue; + } + + QAPI_LIST_APPEND(tail, object_get_canonical_path(OBJECT(ns))); + } + + visit_type_strList(v, name, &paths, errp); + qapi_free_strList(paths); +} + +static char *get_subnqn(Object *obj, Error **errp) +{ + NvmeSubsystem *subsys = NVME_SUBSYSTEM(obj); + return g_strdup((char *)subsys->subnqn); +} + +static void set_subnqn(Object *obj, const char *str, Error **errp) +{ + NvmeSubsystem *subsys = NVME_SUBSYSTEM(obj); + snprintf((char *)subsys->subnqn, sizeof(subsys->subnqn), "%s", str); +} + +static char *get_uuid(Object *obj, Error **errp) +{ + NvmeSubsystem *subsys = NVME_SUBSYSTEM(obj); + char buf[UUID_FMT_LEN + 1]; + + qemu_uuid_unparse(&subsys->uuid, buf); + + return g_strdup(buf); +} + +static void set_uuid(Object *obj, const char *str, Error **errp) +{ + NvmeSubsystem *subsys = NVME_SUBSYSTEM(obj); + + if (!strcmp(str, "auto")) { + qemu_uuid_generate(&subsys->uuid); + } else if (qemu_uuid_parse(str, &subsys->uuid) < 0) { + error_setg(errp, "invalid uuid"); + return; + } +} + +static void nvme_subsys_complete(UserCreatable *uc, Error **errp) +{ + NvmeSubsystem *subsys = NVME_SUBSYSTEM(uc); + + if (!strcmp((char *)subsys->subnqn, "")) { + char buf[UUID_FMT_LEN + 1]; + + qemu_uuid_unparse(&subsys->uuid, buf); + + snprintf((char *)subsys->subnqn, sizeof(subsys->subnqn), + "nqn.2014-08.org.nvmexpress:uuid:%s", buf); + } +} + +static void nvme_subsys_instance_init(Object *obj) +{ + object_property_add(obj, "controllers", "str", get_controllers, NULL, NULL, + NULL); + + object_property_add(obj, "namespaces", "str", get_namespaces, NULL, NULL, + NULL); +} + +static void nvme_subsys_class_init(ObjectClass *oc, void *data) +{ + ObjectProperty *op; + + UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); + ucc->complete = nvme_subsys_complete; + + object_class_property_add_str(oc, "subnqn", get_subnqn, set_subnqn); + object_class_property_set_description(oc, "subnqn", "the NVM Subsystem " + "NVMe Qualified Name; " + "(default: \"nqn.2014-08.org.nvmexpress:uuid:<uuid>\")"); + + op = object_class_property_add_str(oc, "uuid", get_uuid, set_uuid); + object_property_set_default_str(op, "auto"); + object_class_property_set_description(oc, "subnqn", "NVM Subsystem UUID " + "(\"auto\" for random value; " + "default: \"auto\")"); +} + +static const TypeInfo nvme_subsys_info = { + .name = TYPE_NVME_SUBSYSTEM, + .parent = TYPE_OBJECT, + .class_init = nvme_subsys_class_init, + .instance_init = nvme_subsys_instance_init, + .instance_size = sizeof(NvmeSubsystem), + .interfaces = (InterfaceInfo[]) { + { TYPE_USER_CREATABLE }, + { }, + } +}; + static void nvme_subsys_device_setup(NvmeSubsystemDevice *dev) { NvmeSubsystem *subsys = &dev->subsys; @@ -64,7 +197,7 @@ static void nvme_subsys_device_realize(DeviceState *dev, Error **errp) nvme_subsys_device_setup(subsys); } -static Property nvme_subsystem_device_props[] = { +static Property nvme_subsys_device_props[] = { DEFINE_PROP_STRING("nqn", NvmeSubsystemDevice, params.nqn), DEFINE_PROP_END_OF_LIST(), }; @@ -79,7 +212,7 @@ static void nvme_subsys_device_class_init(ObjectClass *oc, void *data) dc->desc = "Virtual NVMe subsystem"; dc->hotpluggable = false; - device_class_set_props(dc, nvme_subsystem_device_props); + device_class_set_props(dc, nvme_subsys_device_props); } static const TypeInfo nvme_subsys_device_info = { @@ -89,9 +222,10 @@ static const TypeInfo nvme_subsys_device_info = { .instance_size = sizeof(NvmeSubsystemDevice), }; -static void nvme_subsys_register_types(void) +static void register_types(void) { + type_register_static(&nvme_subsys_info); type_register_static(&nvme_subsys_device_info); } -type_init(nvme_subsys_register_types) +type_init(register_types) diff --git a/qapi/qom.json b/qapi/qom.json index a25616bc7a7d..d4c211fc38b1 100644 --- a/qapi/qom.json +++ b/qapi/qom.json @@ -647,6 +647,21 @@ '*hugetlbsize': 'size', '*seal': 'bool' } } +## +# @NvmeSubsystemProperties: +# +# Properties for nvme-subsys objects. +# +# @subnqn: the NVM Subsystem NVMe Qualified Name +# +# @uuid: the UUID of the subsystem. Used as default in subnqn. +# +# Since: 6.1 +## +{ 'struct': 'NvmeSubsystemProperties', + 'data': { '*subnqn': 'str', + '*uuid': 'str' } } + ## # @PrManagerHelperProperties: # @@ -797,6 +812,7 @@ { 'name': 'memory-backend-memfd', 'if': 'CONFIG_LINUX' }, 'memory-backend-ram', + 'x-nvme-subsystem', 'pef-guest', 'pr-manager-helper', 'qtest', @@ -855,6 +871,7 @@ 'memory-backend-memfd': { 'type': 'MemoryBackendMemfdProperties', 'if': 'CONFIG_LINUX' }, 'memory-backend-ram': 'MemoryBackendProperties', + 'x-nvme-subsystem': 'NvmeSubsystemProperties', 'pr-manager-helper': 'PrManagerHelperProperties', 'qtest': 'QtestProperties', 'rng-builtin': 'RngProperties', -- 2.33.0