Re: [PATCH V6 6/6] hw/block/nvme: support for shared namespace in subsystem

2021-01-25 Thread Klaus Jensen
On Jan 24 11:54, Minwoo Im wrote:
> nvme-ns device is registered to a nvme controller device during the
> initialization in nvme_register_namespace() in case that 'bus' property
> is given which means it's mapped to a single controller.
> 
> This patch introduced a new property 'subsys' just like the controller
> device instance did to map a namespace to a NVMe subsystem.
> 
> If 'subsys' property is given to the nvme-ns device, it will belong to
> the specified subsystem and will be attached to all controllers in that
> subsystem by enabling shared namespace capability in NMIC(Namespace
> Multi-path I/O and Namespace Capabilities) in Identify Namespace.
> 
> Usage:
> 
>   -device nvme-subsys,id=subsys0
>   -device nvme,serial=foo,id=nvme0,subsys=subsys0
>   -device nvme,serial=bar,id=nvme1,subsys=subsys0
>   -device nvme,serial=baz,id=nvme2,subsys=subsys0
>   -device nvme-ns,id=ns1,drive=,nsid=1,subsys=subsys0  # Shared
>   -device nvme-ns,id=ns2,drive=,nsid=2,bus=nvme2   # Non-shared
> 
>   In the above example, 'ns1' will be shared to 'nvme0' and 'nvme1' in
>   the same subsystem.  On the other hand, 'ns2' will be attached to the
>   'nvme2' only as a private namespace in that subsystem.
> 
> All the namespace with 'subsys' parameter will attach all controllers in
> the subsystem to the namespace by default.
> 
> Signed-off-by: Minwoo Im 
> ---
>  hw/block/nvme-ns.c | 23 ++-
>  hw/block/nvme-ns.h |  7 +++
>  hw/block/nvme-subsys.c | 25 +
>  hw/block/nvme-subsys.h |  3 +++
>  hw/block/nvme.c| 10 +-
>  5 files changed, 62 insertions(+), 6 deletions(-)
> 
> diff --git a/hw/block/nvme-ns.c b/hw/block/nvme-ns.c
> index 62b25cf69bfa..9b493f2ead03 100644
> --- a/hw/block/nvme-ns.c
> +++ b/hw/block/nvme-ns.c
> @@ -63,6 +63,10 @@ static int nvme_ns_init(NvmeNamespace *ns, Error **errp)
>  
>  id_ns->npda = id_ns->npdg = npdg - 1;
>  
> +if (nvme_ns_shared(ns)) {
> +id_ns->nmic |= NVME_NMIC_NS_SHARED;
> +}
> +
>  return 0;
>  }
>  
> @@ -365,16 +369,25 @@ static void nvme_ns_realize(DeviceState *dev, Error 
> **errp)
>  return;
>  }
>  
> -if (nvme_register_namespace(n, ns, errp)) {
> -error_propagate_prepend(errp, local_err,
> -"could not register namespace: ");
> -return;
> +if (ns->subsys) {
> +if (nvme_subsys_register_ns(ns, errp)) {
> +error_propagate_prepend(errp, local_err,
> +"could not setup namespace to subsys: ");
> +return;
> +}
> +} else {
> +if (nvme_register_namespace(n, ns, errp)) {
> +error_propagate_prepend(errp, local_err,
> +"could not register namespace: ");
> +return;
> +}
>  }
> -
>  }
>  
>  static Property nvme_ns_props[] = {
>  DEFINE_BLOCK_PROPERTIES(NvmeNamespace, blkconf),
> +DEFINE_PROP_LINK("subsys", NvmeNamespace, subsys, TYPE_NVME_SUBSYS,
> + NvmeSubsystem *),
>  DEFINE_PROP_UINT32("nsid", NvmeNamespace, params.nsid, 0),
>  DEFINE_PROP_UUID("uuid", NvmeNamespace, params.uuid),
>  DEFINE_PROP_BOOL("zoned", NvmeNamespace, params.zoned, false),
> diff --git a/hw/block/nvme-ns.h b/hw/block/nvme-ns.h
> index 293ac990e3f6..929e78861903 100644
> --- a/hw/block/nvme-ns.h
> +++ b/hw/block/nvme-ns.h
> @@ -47,6 +47,8 @@ typedef struct NvmeNamespace {
>  const uint32_t *iocs;
>  uint8_t  csi;
>  
> +NvmeSubsystem   *subsys;
> +
>  NvmeIdNsZoned   *id_ns_zoned;
>  NvmeZone*zone_array;
>  QTAILQ_HEAD(, NvmeZone) exp_open_zones;
> @@ -77,6 +79,11 @@ static inline uint32_t nvme_nsid(NvmeNamespace *ns)
>  return -1;
>  }
>  
> +static inline bool nvme_ns_shared(NvmeNamespace *ns)
> +{
> +return !!ns->subsys;
> +}
> +
>  static inline NvmeLBAF *nvme_ns_lbaf(NvmeNamespace *ns)
>  {
>  NvmeIdNs *id_ns = >id_ns;
> diff --git a/hw/block/nvme-subsys.c b/hw/block/nvme-subsys.c
> index e9d61c993c90..641de33e99fc 100644
> --- a/hw/block/nvme-subsys.c
> +++ b/hw/block/nvme-subsys.c
> @@ -43,6 +43,31 @@ int nvme_subsys_register_ctrl(NvmeCtrl *n, Error **errp)
>  return cntlid;
>  }
>  
> +int nvme_subsys_register_ns(NvmeNamespace *ns, Error **errp)
> +{
> +NvmeSubsystem *subsys = ns->subsys;
> +NvmeCtrl *n;
> +int i;
> +
> +if (subsys->namespaces[nvme_nsid(ns)]) {
> +error_setg(errp, "namespace %d already registerd to subsy %s",

Small typo.

"registerEd to subsySTEM"

> +   nvme_nsid(ns), subsys->parent_obj.id);
> +return -1;
> +}
> +
> +subsys->namespaces[nvme_nsid(ns)] = ns;
> +
> +for (i = 0; i < ARRAY_SIZE(subsys->ctrls); i++) {
> +n = subsys->ctrls[i];
> +
> +if (n && nvme_register_namespace(n, ns, errp)) {
> +return -1;
> +}
> +}
> +
> +return 0;
> +}
> +
>  static void 

[PATCH V6 6/6] hw/block/nvme: support for shared namespace in subsystem

2021-01-23 Thread Minwoo Im
nvme-ns device is registered to a nvme controller device during the
initialization in nvme_register_namespace() in case that 'bus' property
is given which means it's mapped to a single controller.

This patch introduced a new property 'subsys' just like the controller
device instance did to map a namespace to a NVMe subsystem.

If 'subsys' property is given to the nvme-ns device, it will belong to
the specified subsystem and will be attached to all controllers in that
subsystem by enabling shared namespace capability in NMIC(Namespace
Multi-path I/O and Namespace Capabilities) in Identify Namespace.

Usage:

  -device nvme-subsys,id=subsys0
  -device nvme,serial=foo,id=nvme0,subsys=subsys0
  -device nvme,serial=bar,id=nvme1,subsys=subsys0
  -device nvme,serial=baz,id=nvme2,subsys=subsys0
  -device nvme-ns,id=ns1,drive=,nsid=1,subsys=subsys0  # Shared
  -device nvme-ns,id=ns2,drive=,nsid=2,bus=nvme2   # Non-shared

  In the above example, 'ns1' will be shared to 'nvme0' and 'nvme1' in
  the same subsystem.  On the other hand, 'ns2' will be attached to the
  'nvme2' only as a private namespace in that subsystem.

All the namespace with 'subsys' parameter will attach all controllers in
the subsystem to the namespace by default.

Signed-off-by: Minwoo Im 
---
 hw/block/nvme-ns.c | 23 ++-
 hw/block/nvme-ns.h |  7 +++
 hw/block/nvme-subsys.c | 25 +
 hw/block/nvme-subsys.h |  3 +++
 hw/block/nvme.c| 10 +-
 5 files changed, 62 insertions(+), 6 deletions(-)

diff --git a/hw/block/nvme-ns.c b/hw/block/nvme-ns.c
index 62b25cf69bfa..9b493f2ead03 100644
--- a/hw/block/nvme-ns.c
+++ b/hw/block/nvme-ns.c
@@ -63,6 +63,10 @@ static int nvme_ns_init(NvmeNamespace *ns, Error **errp)
 
 id_ns->npda = id_ns->npdg = npdg - 1;
 
+if (nvme_ns_shared(ns)) {
+id_ns->nmic |= NVME_NMIC_NS_SHARED;
+}
+
 return 0;
 }
 
@@ -365,16 +369,25 @@ static void nvme_ns_realize(DeviceState *dev, Error 
**errp)
 return;
 }
 
-if (nvme_register_namespace(n, ns, errp)) {
-error_propagate_prepend(errp, local_err,
-"could not register namespace: ");
-return;
+if (ns->subsys) {
+if (nvme_subsys_register_ns(ns, errp)) {
+error_propagate_prepend(errp, local_err,
+"could not setup namespace to subsys: ");
+return;
+}
+} else {
+if (nvme_register_namespace(n, ns, errp)) {
+error_propagate_prepend(errp, local_err,
+"could not register namespace: ");
+return;
+}
 }
-
 }
 
 static Property nvme_ns_props[] = {
 DEFINE_BLOCK_PROPERTIES(NvmeNamespace, blkconf),
+DEFINE_PROP_LINK("subsys", NvmeNamespace, subsys, TYPE_NVME_SUBSYS,
+ NvmeSubsystem *),
 DEFINE_PROP_UINT32("nsid", NvmeNamespace, params.nsid, 0),
 DEFINE_PROP_UUID("uuid", NvmeNamespace, params.uuid),
 DEFINE_PROP_BOOL("zoned", NvmeNamespace, params.zoned, false),
diff --git a/hw/block/nvme-ns.h b/hw/block/nvme-ns.h
index 293ac990e3f6..929e78861903 100644
--- a/hw/block/nvme-ns.h
+++ b/hw/block/nvme-ns.h
@@ -47,6 +47,8 @@ typedef struct NvmeNamespace {
 const uint32_t *iocs;
 uint8_t  csi;
 
+NvmeSubsystem   *subsys;
+
 NvmeIdNsZoned   *id_ns_zoned;
 NvmeZone*zone_array;
 QTAILQ_HEAD(, NvmeZone) exp_open_zones;
@@ -77,6 +79,11 @@ static inline uint32_t nvme_nsid(NvmeNamespace *ns)
 return -1;
 }
 
+static inline bool nvme_ns_shared(NvmeNamespace *ns)
+{
+return !!ns->subsys;
+}
+
 static inline NvmeLBAF *nvme_ns_lbaf(NvmeNamespace *ns)
 {
 NvmeIdNs *id_ns = >id_ns;
diff --git a/hw/block/nvme-subsys.c b/hw/block/nvme-subsys.c
index e9d61c993c90..641de33e99fc 100644
--- a/hw/block/nvme-subsys.c
+++ b/hw/block/nvme-subsys.c
@@ -43,6 +43,31 @@ int nvme_subsys_register_ctrl(NvmeCtrl *n, Error **errp)
 return cntlid;
 }
 
+int nvme_subsys_register_ns(NvmeNamespace *ns, Error **errp)
+{
+NvmeSubsystem *subsys = ns->subsys;
+NvmeCtrl *n;
+int i;
+
+if (subsys->namespaces[nvme_nsid(ns)]) {
+error_setg(errp, "namespace %d already registerd to subsy %s",
+   nvme_nsid(ns), subsys->parent_obj.id);
+return -1;
+}
+
+subsys->namespaces[nvme_nsid(ns)] = ns;
+
+for (i = 0; i < ARRAY_SIZE(subsys->ctrls); i++) {
+n = subsys->ctrls[i];
+
+if (n && nvme_register_namespace(n, ns, errp)) {
+return -1;
+}
+}
+
+return 0;
+}
+
 static void nvme_subsys_setup(NvmeSubsystem *subsys)
 {
 snprintf((char *)subsys->subnqn, sizeof(subsys->subnqn),
diff --git a/hw/block/nvme-subsys.h b/hw/block/nvme-subsys.h
index 4eba50d96a1d..ccf6a71398d3 100644
--- a/hw/block/nvme-subsys.h
+++ b/hw/block/nvme-subsys.h
@@ -14,6 +14,7 @@
 OBJECT_CHECK(NvmeSubsystem, (obj), TYPE_NVME_SUBSYS)
 
 #define NVME_SUBSYS_MAX_CTRLS