qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Klaus Jensen <its@irrelevant.dk>
To: qemu-devel@nongnu.org
Cc: "Fam Zheng" <fam@euphon.net>, "Kevin Wolf" <kwolf@redhat.com>,
	"Daniel P. Berrangé" <berrange@redhat.com>,
	"Eduardo Habkost" <ehabkost@redhat.com>,
	qemu-block@nongnu.org,
	"Philippe Mathieu-Daudé" <philmd@redhat.com>,
	"Markus Armbruster" <armbru@redhat.com>,
	"Klaus Jensen" <its@irrelevant.dk>,
	"Hanna Reitz" <hreitz@redhat.com>,
	"Hannes Reinecke" <hare@suse.de>,
	"Stefan Hajnoczi" <stefanha@redhat.com>,
	"Klaus Jensen" <k.jensen@samsung.com>,
	"Keith Busch" <kbusch@kernel.org>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Eric Blake" <eblake@redhat.com>
Subject: [PATCH RFC v2 11/16] hw/nvme: add experimental object x-nvme-subsystem
Date: Mon, 27 Sep 2021 07:17:54 +0200	[thread overview]
Message-ID: <20210927051759.447305-12-its@irrelevant.dk> (raw)
In-Reply-To: <20210927051759.447305-1-its@irrelevant.dk>

From: Klaus Jensen <k.jensen@samsung.com>

Add a basic user creatable object that models an NVMe NVM subsystem.

Signed-off-by: Klaus Jensen <k.jensen@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



  parent reply	other threads:[~2021-09-27  5:52 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-27  5:17 [PATCH RFC v2 00/16] hw/nvme: experimental user-creatable objects Klaus Jensen
2021-09-27  5:17 ` [PATCH RFC v2 01/16] hw/nvme: reattach subsystem namespaces on hotplug Klaus Jensen
2021-09-27  5:17 ` [PATCH RFC v2 02/16] hw/nvme: change nvme-ns 'shared' default Klaus Jensen
2021-09-27  5:17 ` [PATCH RFC v2 03/16] hw/nvme: move dif/pi prototypes into dif.h Klaus Jensen
2021-09-27  5:17 ` [PATCH RFC v2 04/16] hw/nvme: move zns helpers and types into zns.h Klaus Jensen
2021-09-27  5:17 ` [PATCH RFC v2 05/16] hw/nvme: move zoned namespace members to separate struct Klaus Jensen
2021-09-27  5:17 ` [PATCH RFC v2 06/16] hw/nvme: move nvm " Klaus Jensen
2021-09-27  5:17 ` [PATCH RFC v2 07/16] hw/nvme: move BlockBackend to NvmeNamespaceNvm Klaus Jensen
2021-09-27  5:17 ` [PATCH RFC v2 08/16] hw/nvme: hoist qdev state from namespace Klaus Jensen
2021-09-27  5:17 ` [PATCH RFC v2 09/16] hw/nvme: hoist qdev state from subsystem Klaus Jensen
2021-09-27  5:17 ` [PATCH RFC v2 10/16] hw/nvme: hoist qdev state from controller Klaus Jensen
2021-09-27  5:17 ` Klaus Jensen [this message]
2021-09-27  5:17 ` [PATCH RFC v2 12/16] nvme: add structured type for nguid Klaus Jensen
2021-09-27  5:17 ` [PATCH RFC v2 13/16] hw/nvme: add experimental abstract object x-nvme-ns Klaus Jensen
2021-09-27  5:17 ` [PATCH RFC v2 14/16] hw/nvme: add experimental objects x-nvme-ns-{nvm, zoned} Klaus Jensen
2021-09-27  5:17 ` [PATCH RFC v2 15/16] hw/nvme: add experimental device x-nvme-ctrl Klaus Jensen
2021-09-27  5:17 ` [PATCH RFC v2 16/16] docs: add documentation for experimental nvme emulation Klaus Jensen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210927051759.447305-12-its@irrelevant.dk \
    --to=its@irrelevant.dk \
    --cc=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=eblake@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=fam@euphon.net \
    --cc=hare@suse.de \
    --cc=hreitz@redhat.com \
    --cc=k.jensen@samsung.com \
    --cc=kbusch@kernel.org \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=philmd@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).