Blame SOURCES/0195-cxl-add-a-create-region-command.patch

26ccd9
From 21b089025178442baa7b59823a7fd264b4c075a8 Mon Sep 17 00:00:00 2001
26ccd9
From: Vishal Verma <vishal.l.verma@intel.com>
26ccd9
Date: Mon, 15 Aug 2022 13:22:10 -0600
26ccd9
Subject: [PATCH 195/217] cxl: add a 'create-region' command
26ccd9
26ccd9
Add a 'create-region' command to cxl-cli that walks the platform's CXL
26ccd9
hierarchy to find an appropriate root decoder based on any options
26ccd9
provided, and uses libcxl APIs to create a 'region' that is comprehended
26ccd9
by libnvdimm and ndctl.
26ccd9
26ccd9
Link: https://lore.kernel.org/r/20220815192214.545800-8-vishal.l.verma@intel.com
26ccd9
Cc: Dan Williams <dan.j.williams@intel.com>
26ccd9
Reviewed-by: Dan Williams <dan.j.williams@intel.com>
26ccd9
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
26ccd9
---
26ccd9
 Documentation/cxl/bus-option.txt         |   5 +
26ccd9
 Documentation/cxl/cxl-create-region.txt  | 112 +++++
26ccd9
 Documentation/cxl/meson.build            |   2 +
26ccd9
 Documentation/cxl/region-description.txt |   7 +
26ccd9
 cxl/builtin.h                            |   1 +
26ccd9
 cxl/cxl.c                                |   1 +
26ccd9
 cxl/filter.h                             |   4 +-
26ccd9
 cxl/json.c                               |   9 +
26ccd9
 cxl/meson.build                          |   1 +
26ccd9
 cxl/region.c                             | 550 +++++++++++++++++++++++
26ccd9
 10 files changed, 691 insertions(+), 1 deletion(-)
26ccd9
 create mode 100644 Documentation/cxl/bus-option.txt
26ccd9
 create mode 100644 Documentation/cxl/cxl-create-region.txt
26ccd9
 create mode 100644 Documentation/cxl/region-description.txt
26ccd9
 create mode 100644 cxl/region.c
26ccd9
26ccd9
diff --git a/Documentation/cxl/bus-option.txt b/Documentation/cxl/bus-option.txt
26ccd9
new file mode 100644
26ccd9
index 0000000..02e2f08
26ccd9
--- /dev/null
26ccd9
+++ b/Documentation/cxl/bus-option.txt
26ccd9
@@ -0,0 +1,5 @@
26ccd9
+// SPDX-License-Identifier: GPL-2.0
26ccd9
+
26ccd9
+-b::
26ccd9
+--bus=::
26ccd9
+	Restrict the operation to the specified bus.
26ccd9
diff --git a/Documentation/cxl/cxl-create-region.txt b/Documentation/cxl/cxl-create-region.txt
26ccd9
new file mode 100644
26ccd9
index 0000000..6b740d5
26ccd9
--- /dev/null
26ccd9
+++ b/Documentation/cxl/cxl-create-region.txt
26ccd9
@@ -0,0 +1,112 @@
26ccd9
+// SPDX-License-Identifier: GPL-2.0
26ccd9
+
26ccd9
+cxl-create-region(1)
26ccd9
+====================
26ccd9
+
26ccd9
+NAME
26ccd9
+----
26ccd9
+cxl-create-region - Assemble a CXL region by setting up attributes of its
26ccd9
+constituent CXL memdevs.
26ccd9
+
26ccd9
+SYNOPSIS
26ccd9
+--------
26ccd9
+[verse]
26ccd9
+'cxl create-region [<options>]'
26ccd9
+
26ccd9
+include::region-description.txt[]
26ccd9
+
26ccd9
+For create-region, a size can optionally be specified, but if not, the maximum
26ccd9
+possible size for each memdev will be used up to the available decode capacity
26ccd9
+in the system for the given memory type. For persistent regions a UUID can
26ccd9
+optionally be specified, but if not, one will be generated.
26ccd9
+
26ccd9
+If the region-creation operation is successful, a region object will be
26ccd9
+emitted on stdout in JSON format (see examples). If the specified arguments
26ccd9
+cannot be satisfied with a legal configuration, then an appropriate error will
26ccd9
+be emitted on stderr.
26ccd9
+
26ccd9
+EXAMPLE
26ccd9
+-------
26ccd9
+----
26ccd9
+#cxl create - region - m - d decoder0 .1 - w 2 - g 1024 mem0 mem1
26ccd9
+{
26ccd9
+  "region":"region0",
26ccd9
+  "resource":"0xc90000000",
26ccd9
+  "size":"512.00 MiB (536.87 MB)",
26ccd9
+  "interleave_ways":2,
26ccd9
+  "interleave_granularity":1024,
26ccd9
+  "mappings":[
26ccd9
+    {
26ccd9
+      "position":1,
26ccd9
+      "decoder":"decoder4.0"
26ccd9
+    },
26ccd9
+    {
26ccd9
+      "position":0,
26ccd9
+      "decoder":"decoder3.0"
26ccd9
+    }
26ccd9
+  ]
26ccd9
+}
26ccd9
+created 1 region
26ccd9
+----
26ccd9
+
26ccd9
+OPTIONS
26ccd9
+-------
26ccd9
+<target(s)>::
26ccd9
+The CXL targets that should be used to form the region. The number of
26ccd9
+'target' arguments must match the '--ways' option (if provided). The
26ccd9
+targets are memdev names such as 'mem0', 'mem1' etc.
26ccd9
+
26ccd9
+include::bus-option.txt[]
26ccd9
+
26ccd9
+-m::
26ccd9
+--memdevs::
26ccd9
+	Indicate that the non-option arguments for 'target(s)' refer to memdev
26ccd9
+	names. Currently this is the only option supported, and must be
26ccd9
+	specified.
26ccd9
+
26ccd9
+-s::
26ccd9
+--size=::
26ccd9
+	Specify the total size for the new region. This is optional, and by
26ccd9
+	default, the maximum possible size will be used. The maximum possible
26ccd9
+	size is gated by both the contiguous free HPA space remaining in the
26ccd9
+	root decoder, and the available DPA space in the component memdevs.
26ccd9
+
26ccd9
+-t::
26ccd9
+--type=::
26ccd9
+	Specify the region type - 'pmem' or 'ram'. Defaults to 'pmem'.
26ccd9
+
26ccd9
+-U::
26ccd9
+--uuid=::
26ccd9
+	Specify a UUID for the new region. This shouldn't usually need to be
26ccd9
+	specified, as one will be generated by default.
26ccd9
+
26ccd9
+-w::
26ccd9
+--ways=::
26ccd9
+	The number of interleave ways for the new region's interleave. This
26ccd9
+	should be equal to the number of memdevs specified in --memdevs, if
26ccd9
+	--memdevs is being supplied. If --ways is not specified, it will be
26ccd9
+	determined based on the number of memdev targets provided.
26ccd9
+
26ccd9
+-g::
26ccd9
+--granularity=::
26ccd9
+	The interleave granularity for the new region. Must match the selected
26ccd9
+	root decoder's (if provided) granularity. If the root decoder is
26ccd9
+	interleaved across more than one host-bridge then this value must match
26ccd9
+	that granularity. Otherwise, for non-interleaved decode windows, any
26ccd9
+	granularity can be specified as long as all devices support that setting.
26ccd9
+
26ccd9
+-d::
26ccd9
+--decoder=::
26ccd9
+	The root decoder that the region should be created under. If not
26ccd9
+	supplied, the first cross-host bridge (if available), decoder that
26ccd9
+	supports the largest interleave will be chosen.
26ccd9
+
26ccd9
+include::human-option.txt[]
26ccd9
+
26ccd9
+include::debug-option.txt[]
26ccd9
+
26ccd9
+include::../copyright.txt[]
26ccd9
+
26ccd9
+SEE ALSO
26ccd9
+--------
26ccd9
+linkcxl:cxl-list[1],
26ccd9
diff --git a/Documentation/cxl/meson.build b/Documentation/cxl/meson.build
26ccd9
index 423be90..340cdee 100644
26ccd9
--- a/Documentation/cxl/meson.build
26ccd9
+++ b/Documentation/cxl/meson.build
26ccd9
@@ -23,6 +23,7 @@ filedeps = [
26ccd9
   'memdev-option.txt',
26ccd9
   'labels-options.txt',
26ccd9
   'debug-option.txt',
26ccd9
+  'region-description.txt',
26ccd9
 ]
26ccd9
 
26ccd9
 cxl_manpages = [
26ccd9
@@ -39,6 +40,7 @@ cxl_manpages = [
26ccd9
   'cxl-set-partition.txt',
26ccd9
   'cxl-reserve-dpa.txt',
26ccd9
   'cxl-free-dpa.txt',
26ccd9
+  'cxl-create-region.txt',
26ccd9
 ]
26ccd9
 
26ccd9
 foreach man : cxl_manpages
26ccd9
diff --git a/Documentation/cxl/region-description.txt b/Documentation/cxl/region-description.txt
26ccd9
new file mode 100644
26ccd9
index 0000000..d7e3077
26ccd9
--- /dev/null
26ccd9
+++ b/Documentation/cxl/region-description.txt
26ccd9
@@ -0,0 +1,7 @@
26ccd9
+// SPDX-License-Identifier: GPL-2.0
26ccd9
+
26ccd9
+DESCRIPTION
26ccd9
+-----------
26ccd9
+A CXL region is composed of one or more slices of CXL memdevs, with configurable
26ccd9
+interleave settings - both the number of interleave ways, and the interleave
26ccd9
+granularity.
26ccd9
diff --git a/cxl/builtin.h b/cxl/builtin.h
26ccd9
index 9e6fc62..843bada 100644
26ccd9
--- a/cxl/builtin.h
26ccd9
+++ b/cxl/builtin.h
26ccd9
@@ -18,4 +18,5 @@ int cmd_disable_port(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
 int cmd_enable_port(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
 int cmd_set_partition(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
 int cmd_disable_bus(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
+int cmd_create_region(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
 #endif /* _CXL_BUILTIN_H_ */
26ccd9
diff --git a/cxl/cxl.c b/cxl/cxl.c
26ccd9
index ef4cda9..f0afcfe 100644
26ccd9
--- a/cxl/cxl.c
26ccd9
+++ b/cxl/cxl.c
26ccd9
@@ -72,6 +72,7 @@ static struct cmd_struct commands[] = {
26ccd9
 	{ "enable-port", .c_fn = cmd_enable_port },
26ccd9
 	{ "set-partition", .c_fn = cmd_set_partition },
26ccd9
 	{ "disable-bus", .c_fn = cmd_disable_bus },
26ccd9
+	{ "create-region", .c_fn = cmd_create_region },
26ccd9
 };
26ccd9
 
26ccd9
 int main(int argc, const char **argv)
26ccd9
diff --git a/cxl/filter.h b/cxl/filter.h
26ccd9
index 609433c..d22d8b1 100644
26ccd9
--- a/cxl/filter.h
26ccd9
+++ b/cxl/filter.h
26ccd9
@@ -35,8 +35,10 @@ struct cxl_memdev *util_cxl_memdev_filter(struct cxl_memdev *memdev,
26ccd9
 struct cxl_port *util_cxl_port_filter_by_memdev(struct cxl_port *port,
26ccd9
 						const char *ident,
26ccd9
 						const char *serial);
26ccd9
-struct cxl_region *util_cxl_region_filter(struct cxl_region *region,
26ccd9
+struct cxl_decoder *util_cxl_decoder_filter(struct cxl_decoder *decoder,
26ccd9
 					    const char *__ident);
26ccd9
+struct cxl_region *util_cxl_region_filter(struct cxl_region *region,
26ccd9
+					  const char *__ident);
26ccd9
 
26ccd9
 enum cxl_port_filter_mode {
26ccd9
 	CXL_PF_SINGLE,
26ccd9
diff --git a/cxl/json.c b/cxl/json.c
26ccd9
index 70cf286..9dc99df 100644
26ccd9
--- a/cxl/json.c
26ccd9
+++ b/cxl/json.c
26ccd9
@@ -547,6 +547,7 @@ void util_cxl_mappings_append_json(struct json_object *jregion,
26ccd9
 	cxl_mapping_foreach(region, mapping) {
26ccd9
 		struct json_object *jmapping;
26ccd9
 		struct cxl_decoder *decoder;
26ccd9
+		struct cxl_memdev *memdev;
26ccd9
 
26ccd9
 		jmapping = json_object_new_object();
26ccd9
 		if (!jmapping)
26ccd9
@@ -564,6 +565,14 @@ void util_cxl_mappings_append_json(struct json_object *jregion,
26ccd9
 		if (!decoder)
26ccd9
 			continue;
26ccd9
 
26ccd9
+		memdev = cxl_decoder_get_memdev(decoder);
26ccd9
+		if (memdev) {
26ccd9
+			devname = cxl_memdev_get_devname(memdev);
26ccd9
+			jobj = json_object_new_string(devname);
26ccd9
+			if (jobj)
26ccd9
+				json_object_object_add(jmapping, "memdev", jobj);
26ccd9
+		}
26ccd9
+
26ccd9
 		devname = cxl_decoder_get_devname(decoder);
26ccd9
 		jobj = json_object_new_string(devname);
26ccd9
 		if (jobj)
26ccd9
diff --git a/cxl/meson.build b/cxl/meson.build
26ccd9
index d63dcb1..f2474aa 100644
26ccd9
--- a/cxl/meson.build
26ccd9
+++ b/cxl/meson.build
26ccd9
@@ -3,6 +3,7 @@ cxl_src = [
26ccd9
   'list.c',
26ccd9
   'port.c',
26ccd9
   'bus.c',
26ccd9
+  'region.c',
26ccd9
   'memdev.c',
26ccd9
   'json.c',
26ccd9
   'filter.c',
26ccd9
diff --git a/cxl/region.c b/cxl/region.c
26ccd9
new file mode 100644
26ccd9
index 0000000..2791ac9
26ccd9
--- /dev/null
26ccd9
+++ b/cxl/region.c
26ccd9
@@ -0,0 +1,550 @@
26ccd9
+// SPDX-License-Identifier: GPL-2.0
26ccd9
+/* Copyright (C) 2020-2022 Intel Corporation. All rights reserved. */
26ccd9
+#include <stdio.h>
26ccd9
+#include <errno.h>
26ccd9
+#include <stdlib.h>
26ccd9
+#include <unistd.h>
26ccd9
+#include <limits.h>
26ccd9
+#include <util/log.h>
26ccd9
+#include <uuid/uuid.h>
26ccd9
+#include <util/json.h>
26ccd9
+#include <util/size.h>
26ccd9
+#include <cxl/libcxl.h>
26ccd9
+#include <json-c/json.h>
26ccd9
+#include <util/parse-options.h>
26ccd9
+#include <ccan/minmax/minmax.h>
26ccd9
+#include <ccan/short_types/short_types.h>
26ccd9
+
26ccd9
+#include "filter.h"
26ccd9
+#include "json.h"
26ccd9
+
26ccd9
+static struct region_params {
26ccd9
+	const char *bus;
26ccd9
+	const char *size;
26ccd9
+	const char *ways;
26ccd9
+	const char *granularity;
26ccd9
+	const char *type;
26ccd9
+	const char *root_decoder;
26ccd9
+	const char *region;
26ccd9
+	bool memdevs;
26ccd9
+	bool force;
26ccd9
+	bool human;
26ccd9
+	bool debug;
26ccd9
+} param;
26ccd9
+
26ccd9
+struct parsed_params {
26ccd9
+	u64 size;
26ccd9
+	u64 ep_min_size;
26ccd9
+	unsigned int ways;
26ccd9
+	unsigned int granularity;
26ccd9
+	const char **targets;
26ccd9
+	int num_targets;
26ccd9
+	struct cxl_decoder *root_decoder;
26ccd9
+	enum cxl_decoder_mode mode;
26ccd9
+};
26ccd9
+
26ccd9
+enum region_actions {
26ccd9
+	ACTION_CREATE,
26ccd9
+};
26ccd9
+
26ccd9
+static struct log_ctx rl;
26ccd9
+
26ccd9
+#define BASE_OPTIONS() \
26ccd9
+OPT_STRING('b', "bus", &param.bus, "bus name", \
26ccd9
+	   "Limit operation to the specified bus"), \
26ccd9
+OPT_STRING('d', "decoder", &param.root_decoder, "root decoder name", \
26ccd9
+	   "Limit to / use the specified root decoder"), \
26ccd9
+OPT_BOOLEAN(0, "debug", &param.debug, "turn on debug")
26ccd9
+
26ccd9
+#define CREATE_OPTIONS() \
26ccd9
+OPT_STRING('s', "size", &param.size, \
26ccd9
+	   "size in bytes or with a K/M/G etc. suffix", \
26ccd9
+	   "total size desired for the resulting region."), \
26ccd9
+OPT_STRING('w', "ways", &param.ways, \
26ccd9
+	   "number of interleave ways", \
26ccd9
+	   "number of memdevs participating in the regions interleave set"), \
26ccd9
+OPT_STRING('g', "granularity", \
26ccd9
+	   &param.granularity, "interleave granularity", \
26ccd9
+	   "granularity of the interleave set"), \
26ccd9
+OPT_STRING('t', "type", &param.type, \
26ccd9
+	   "region type", "region type - 'pmem' or 'ram'"), \
26ccd9
+OPT_BOOLEAN('m', "memdevs", &param.memdevs, \
26ccd9
+	    "non-option arguments are memdevs"), \
26ccd9
+OPT_BOOLEAN('u', "human", &param.human, "use human friendly number formats")
26ccd9
+
26ccd9
+static const struct option create_options[] = {
26ccd9
+	BASE_OPTIONS(),
26ccd9
+	CREATE_OPTIONS(),
26ccd9
+	OPT_END(),
26ccd9
+};
26ccd9
+
26ccd9
+
26ccd9
+
26ccd9
+static int parse_create_options(int argc, const char **argv,
26ccd9
+				struct parsed_params *p)
26ccd9
+{
26ccd9
+	int i;
26ccd9
+
26ccd9
+	if (!param.root_decoder) {
26ccd9
+		log_err(&rl, "no root decoder specified\n");
26ccd9
+		return -EINVAL;
26ccd9
+	}
26ccd9
+
26ccd9
+	if (param.type) {
26ccd9
+		p->mode = cxl_decoder_mode_from_ident(param.type);
26ccd9
+		if (p->mode == CXL_DECODER_MODE_NONE) {
26ccd9
+			log_err(&rl, "unsupported type: %s\n", param.type);
26ccd9
+			return -EINVAL;
26ccd9
+		}
26ccd9
+	} else {
26ccd9
+		p->mode = CXL_DECODER_MODE_PMEM;
26ccd9
+	}
26ccd9
+
26ccd9
+	if (param.size) {
26ccd9
+		p->size = parse_size64(param.size);
26ccd9
+		if (p->size == ULLONG_MAX) {
26ccd9
+			log_err(&rl, "Invalid size: %s\n", param.size);
26ccd9
+			return -EINVAL;
26ccd9
+		}
26ccd9
+	}
26ccd9
+
26ccd9
+	if (param.ways) {
26ccd9
+		unsigned long ways = strtoul(param.ways, NULL, 0);
26ccd9
+
26ccd9
+		if (ways == ULONG_MAX || (int)ways <= 0) {
26ccd9
+			log_err(&rl, "Invalid interleave ways: %s\n",
26ccd9
+				param.ways);
26ccd9
+			return -EINVAL;
26ccd9
+		}
26ccd9
+		p->ways = ways;
26ccd9
+	} else if (argc) {
26ccd9
+		p->ways = argc;
26ccd9
+	} else {
26ccd9
+		log_err(&rl,
26ccd9
+			"couldn't determine interleave ways from options or arguments\n");
26ccd9
+		return -EINVAL;
26ccd9
+	}
26ccd9
+
26ccd9
+	if (param.granularity) {
26ccd9
+		unsigned long granularity = strtoul(param.granularity, NULL, 0);
26ccd9
+
26ccd9
+		if (granularity == ULONG_MAX || (int)granularity <= 0) {
26ccd9
+			log_err(&rl, "Invalid interleave granularity: %s\n",
26ccd9
+				param.granularity);
26ccd9
+			return -EINVAL;
26ccd9
+		}
26ccd9
+		p->granularity = granularity;
26ccd9
+	}
26ccd9
+
26ccd9
+
26ccd9
+	if (argc > (int)p->ways) {
26ccd9
+		for (i = p->ways; i < argc; i++)
26ccd9
+			log_err(&rl, "extra argument: %s\n", p->targets[i]);
26ccd9
+		return -EINVAL;
26ccd9
+	}
26ccd9
+
26ccd9
+	if (argc < (int)p->ways) {
26ccd9
+		log_err(&rl,
26ccd9
+			"too few target arguments (%d) for interleave ways (%u)\n",
26ccd9
+			argc, p->ways);
26ccd9
+		return -EINVAL;
26ccd9
+	}
26ccd9
+
26ccd9
+	if (p->size && p->ways) {
26ccd9
+		if (p->size % p->ways) {
26ccd9
+			log_err(&rl,
26ccd9
+				"size (%lu) is not an integral multiple of interleave-ways (%u)\n",
26ccd9
+				p->size, p->ways);
26ccd9
+			return -EINVAL;
26ccd9
+		}
26ccd9
+	}
26ccd9
+
26ccd9
+	/*
26ccd9
+	 * For all practical purposes, -m is the default target type, but
26ccd9
+	 * hold off on actively making that decision until a second target
26ccd9
+	 * option is available.
26ccd9
+	 */
26ccd9
+	if (!param.memdevs) {
26ccd9
+		log_err(&rl,
26ccd9
+			"must specify option for target object types (-m)\n");
26ccd9
+		return -EINVAL;
26ccd9
+	}
26ccd9
+
26ccd9
+	return 0;
26ccd9
+}
26ccd9
+
26ccd9
+static int parse_region_options(int argc, const char **argv,
26ccd9
+				struct cxl_ctx *ctx, enum region_actions action,
26ccd9
+				const struct option *options,
26ccd9
+				struct parsed_params *p, const char *usage)
26ccd9
+{
26ccd9
+	const char * const u[] = {
26ccd9
+		usage,
26ccd9
+		NULL
26ccd9
+	};
26ccd9
+
26ccd9
+	argc = parse_options(argc, argv, options, u, 0);
26ccd9
+	p->targets = argv;
26ccd9
+	p->num_targets = argc;
26ccd9
+
26ccd9
+	if (param.debug) {
26ccd9
+		cxl_set_log_priority(ctx, LOG_DEBUG);
26ccd9
+		rl.log_priority = LOG_DEBUG;
26ccd9
+	} else
26ccd9
+		rl.log_priority = LOG_INFO;
26ccd9
+
26ccd9
+	switch(action) {
26ccd9
+	case ACTION_CREATE:
26ccd9
+		return parse_create_options(argc, argv, p);
26ccd9
+	default:
26ccd9
+		return 0;
26ccd9
+	}
26ccd9
+}
26ccd9
+
26ccd9
+/**
26ccd9
+ * validate_memdev() - match memdev with the target provided,
26ccd9
+ *                     and determine its size contribution
26ccd9
+ * @memdev: cxl_memdev being tested for a match against the named target
26ccd9
+ * @target: target memdev
26ccd9
+ * @p:      params structure
26ccd9
+ *
26ccd9
+ * This is called for each memdev in the system, and only returns 'true' if
26ccd9
+ * the memdev name matches the target argument being tested. Additionally,
26ccd9
+ * it sets an ep_min_size attribute that always contains the size of the
26ccd9
+ * smallest target in the provided list. This is used during the automatic
26ccd9
+ * size determination later, to ensure that all targets contribute equally
26ccd9
+ * to the region in case of unevenly sized memdevs.
26ccd9
+ */
26ccd9
+static bool validate_memdev(struct cxl_memdev *memdev, const char *target,
26ccd9
+			    struct parsed_params *p)
26ccd9
+{
26ccd9
+	const char *devname = cxl_memdev_get_devname(memdev);
26ccd9
+	u64 size;
26ccd9
+
26ccd9
+	if (strcmp(devname, target) != 0)
26ccd9
+		return false;
26ccd9
+
26ccd9
+	size = cxl_memdev_get_pmem_size(memdev);
26ccd9
+	if (!p->ep_min_size)
26ccd9
+		p->ep_min_size = size;
26ccd9
+	else
26ccd9
+		p->ep_min_size = min(p->ep_min_size, size);
26ccd9
+
26ccd9
+	return true;
26ccd9
+}
26ccd9
+
26ccd9
+static int validate_config_memdevs(struct cxl_ctx *ctx, struct parsed_params *p)
26ccd9
+{
26ccd9
+	unsigned int i, matched = 0;
26ccd9
+
26ccd9
+	for (i = 0; i < p->ways; i++) {
26ccd9
+		struct cxl_memdev *memdev;
26ccd9
+
26ccd9
+		cxl_memdev_foreach(ctx, memdev)
26ccd9
+			if (validate_memdev(memdev, p->targets[i], p))
26ccd9
+				matched++;
26ccd9
+	}
26ccd9
+	if (matched != p->ways) {
26ccd9
+		log_err(&rl,
26ccd9
+			"one or more memdevs not found in CXL topology\n");
26ccd9
+		return -ENXIO;
26ccd9
+	}
26ccd9
+
26ccd9
+	return 0;
26ccd9
+}
26ccd9
+
26ccd9
+static int validate_decoder(struct cxl_decoder *decoder,
26ccd9
+			    struct parsed_params *p)
26ccd9
+{
26ccd9
+	const char *devname = cxl_decoder_get_devname(decoder);
26ccd9
+
26ccd9
+	switch(p->mode) {
26ccd9
+	case CXL_DECODER_MODE_RAM:
26ccd9
+		if (!cxl_decoder_is_volatile_capable(decoder)) {
26ccd9
+			log_err(&rl, "%s is not volatile capable\n", devname);
26ccd9
+			return -EINVAL;
26ccd9
+		}
26ccd9
+		break;
26ccd9
+	case CXL_DECODER_MODE_PMEM:
26ccd9
+		if (!cxl_decoder_is_pmem_capable(decoder)) {
26ccd9
+			log_err(&rl, "%s is not pmem capable\n", devname);
26ccd9
+			return -EINVAL;
26ccd9
+		}
26ccd9
+		break;
26ccd9
+	default:
26ccd9
+		log_err(&rl, "unknown type: %s\n", param.type);
26ccd9
+		return -EINVAL;
26ccd9
+	}
26ccd9
+
26ccd9
+	/* TODO check if the interleave config is possible under this decoder */
26ccd9
+
26ccd9
+	return 0;
26ccd9
+}
26ccd9
+
26ccd9
+static int create_region_validate_config(struct cxl_ctx *ctx,
26ccd9
+					 struct parsed_params *p)
26ccd9
+{
26ccd9
+	struct cxl_bus *bus;
26ccd9
+	int rc;
26ccd9
+
26ccd9
+	cxl_bus_foreach(ctx, bus) {
26ccd9
+		struct cxl_decoder *decoder;
26ccd9
+		struct cxl_port *port;
26ccd9
+
26ccd9
+		if (!util_cxl_bus_filter(bus, param.bus))
26ccd9
+			continue;
26ccd9
+
26ccd9
+		port = cxl_bus_get_port(bus);
26ccd9
+		if (!cxl_port_is_root(port))
26ccd9
+			continue;
26ccd9
+
26ccd9
+		cxl_decoder_foreach (port, decoder) {
26ccd9
+			if (util_cxl_decoder_filter(decoder,
26ccd9
+						    param.root_decoder)) {
26ccd9
+				p->root_decoder = decoder;
26ccd9
+				goto found;
26ccd9
+			}
26ccd9
+		}
26ccd9
+	}
26ccd9
+
26ccd9
+found:
26ccd9
+	if (p->root_decoder == NULL) {
26ccd9
+		log_err(&rl, "%s not found in CXL topology\n",
26ccd9
+			param.root_decoder);
26ccd9
+		return -ENXIO;
26ccd9
+	}
26ccd9
+
26ccd9
+	rc = validate_decoder(p->root_decoder, p);
26ccd9
+	if (rc)
26ccd9
+		return rc;
26ccd9
+
26ccd9
+	return validate_config_memdevs(ctx, p);
26ccd9
+}
26ccd9
+
26ccd9
+static struct cxl_decoder *
26ccd9
+cxl_memdev_target_find_decoder(struct cxl_ctx *ctx, const char *memdev_name)
26ccd9
+{
26ccd9
+	struct cxl_endpoint *ep = NULL;
26ccd9
+	struct cxl_decoder *decoder;
26ccd9
+	struct cxl_memdev *memdev;
26ccd9
+	struct cxl_port *port;
26ccd9
+
26ccd9
+	cxl_memdev_foreach(ctx, memdev) {
26ccd9
+		const char *devname = cxl_memdev_get_devname(memdev);
26ccd9
+
26ccd9
+		if (strcmp(devname, memdev_name) != 0)
26ccd9
+			continue;
26ccd9
+
26ccd9
+		ep = cxl_memdev_get_endpoint(memdev);
26ccd9
+	}
26ccd9
+
26ccd9
+	if (!ep) {
26ccd9
+		log_err(&rl, "could not get an endpoint for %s\n",
26ccd9
+			memdev_name);
26ccd9
+		return NULL;
26ccd9
+	}
26ccd9
+
26ccd9
+	port = cxl_endpoint_get_port(ep);
26ccd9
+	if (!port) {
26ccd9
+		log_err(&rl, "could not get a port for %s\n",
26ccd9
+			memdev_name);
26ccd9
+		return NULL;
26ccd9
+	}
26ccd9
+
26ccd9
+	cxl_decoder_foreach(port, decoder)
26ccd9
+		if (cxl_decoder_get_size(decoder) == 0)
26ccd9
+			return decoder;
26ccd9
+
26ccd9
+	log_err(&rl, "could not get a free decoder for %s\n", memdev_name);
26ccd9
+	return NULL;
26ccd9
+}
26ccd9
+
26ccd9
+#define try(prefix, op, dev, p) \
26ccd9
+do { \
26ccd9
+	int __rc = prefix##_##op(dev, p); \
26ccd9
+	if (__rc) { \
26ccd9
+		log_err(&rl, "%s: " #op " failed: %s\n", \
26ccd9
+				prefix##_get_devname(dev), \
26ccd9
+				strerror(abs(__rc))); \
26ccd9
+		rc = __rc; \
26ccd9
+		goto err_delete; \
26ccd9
+	} \
26ccd9
+} while (0)
26ccd9
+
26ccd9
+static int cxl_region_determine_granularity(struct cxl_region *region,
26ccd9
+					    struct parsed_params *p)
26ccd9
+{
26ccd9
+	const char *devname = cxl_region_get_devname(region);
26ccd9
+	unsigned int granularity, ways;
26ccd9
+
26ccd9
+	/* Default granularity will be the root decoder's granularity */
26ccd9
+	granularity = cxl_decoder_get_interleave_granularity(p->root_decoder);
26ccd9
+	if (granularity == 0 || granularity == UINT_MAX) {
26ccd9
+		log_err(&rl, "%s: unable to determine root decoder granularity\n",
26ccd9
+			devname);
26ccd9
+		return -ENXIO;
26ccd9
+	}
26ccd9
+
26ccd9
+	/* If no user-supplied granularity, just use the default */
26ccd9
+	if (!p->granularity)
26ccd9
+		return granularity;
26ccd9
+
26ccd9
+	ways = cxl_decoder_get_interleave_ways(p->root_decoder);
26ccd9
+	if (ways == 0 || ways == UINT_MAX) {
26ccd9
+		log_err(&rl, "%s: unable to determine root decoder ways\n",
26ccd9
+			devname);
26ccd9
+		return -ENXIO;
26ccd9
+	}
26ccd9
+
26ccd9
+	/* For ways == 1, any user-supplied granularity is fine */
26ccd9
+	if (ways == 1)
26ccd9
+		return p->granularity;
26ccd9
+
26ccd9
+	/*
26ccd9
+	 * For ways > 1, only allow the same granularity as the selected
26ccd9
+	 * root decoder
26ccd9
+	 */
26ccd9
+	if (p->granularity == granularity)
26ccd9
+		return granularity;
26ccd9
+
26ccd9
+	log_err(&rl,
26ccd9
+		"%s: For an x%d root, only root decoder granularity (%d) permitted\n",
26ccd9
+		devname, ways, granularity);
26ccd9
+	return -EINVAL;
26ccd9
+}
26ccd9
+
26ccd9
+static int create_region(struct cxl_ctx *ctx, int *count,
26ccd9
+			 struct parsed_params *p)
26ccd9
+{
26ccd9
+	unsigned long flags = UTIL_JSON_TARGETS;
26ccd9
+	struct json_object *jregion;
26ccd9
+	unsigned int i, granularity;
26ccd9
+	struct cxl_region *region;
26ccd9
+	const char *devname;
26ccd9
+	uuid_t uuid;
26ccd9
+	u64 size;
26ccd9
+	int rc;
26ccd9
+
26ccd9
+	rc = create_region_validate_config(ctx, p);
26ccd9
+	if (rc)
26ccd9
+		return rc;
26ccd9
+
26ccd9
+	if (p->size) {
26ccd9
+		size = p->size;
26ccd9
+	} else if (p->ep_min_size) {
26ccd9
+		size = p->ep_min_size * p->ways;
26ccd9
+	} else {
26ccd9
+		log_err(&rl, "%s: unable to determine region size\n", __func__);
26ccd9
+		return -ENXIO;
26ccd9
+	}
26ccd9
+
26ccd9
+	if (p->mode == CXL_DECODER_MODE_PMEM) {
26ccd9
+		region = cxl_decoder_create_pmem_region(p->root_decoder);
26ccd9
+		if (!region) {
26ccd9
+			log_err(&rl, "failed to create region under %s\n",
26ccd9
+				param.root_decoder);
26ccd9
+			return -ENXIO;
26ccd9
+		}
26ccd9
+	} else {
26ccd9
+		log_err(&rl, "region type '%s' not supported yet\n",
26ccd9
+			param.type);
26ccd9
+		return -EOPNOTSUPP;
26ccd9
+	}
26ccd9
+
26ccd9
+	devname = cxl_region_get_devname(region);
26ccd9
+
26ccd9
+	rc = cxl_region_determine_granularity(region, p);
26ccd9
+	if (rc < 0)
26ccd9
+		goto err_delete;
26ccd9
+	granularity = rc;
26ccd9
+
26ccd9
+	uuid_generate(uuid);
26ccd9
+	try(cxl_region, set_interleave_granularity, region, granularity);
26ccd9
+	try(cxl_region, set_interleave_ways, region, p->ways);
26ccd9
+	try(cxl_region, set_uuid, region, uuid);
26ccd9
+	try(cxl_region, set_size, region, size);
26ccd9
+
26ccd9
+	for (i = 0; i < p->ways; i++) {
26ccd9
+		struct cxl_decoder *ep_decoder = NULL;
26ccd9
+
26ccd9
+		ep_decoder = cxl_memdev_target_find_decoder(ctx, p->targets[i]);
26ccd9
+		if (!ep_decoder) {
26ccd9
+			rc = -ENXIO;
26ccd9
+			goto err_delete;
26ccd9
+		}
26ccd9
+		if (cxl_decoder_get_mode(ep_decoder) != p->mode) {
26ccd9
+			/*
26ccd9
+			 * The memdev_target_find_decoder() helper returns a free
26ccd9
+			 * decoder whose size has been checked for 0.
26ccd9
+			 * Thus it is safe to change the mode here if needed.
26ccd9
+			 */
26ccd9
+			try(cxl_decoder, set_dpa_size, ep_decoder, 0);
26ccd9
+			try(cxl_decoder, set_mode, ep_decoder, p->mode);
26ccd9
+		}
26ccd9
+		try(cxl_decoder, set_dpa_size, ep_decoder, size/p->ways);
26ccd9
+		rc = cxl_region_set_target(region, i, ep_decoder);
26ccd9
+		if (rc) {
26ccd9
+			log_err(&rl, "%s: failed to set target%d to %s\n",
26ccd9
+				devname, i, p->targets[i]);
26ccd9
+			goto err_delete;
26ccd9
+		}
26ccd9
+	}
26ccd9
+
26ccd9
+	rc = cxl_region_decode_commit(region);
26ccd9
+	if (rc) {
26ccd9
+		log_err(&rl, "%s: failed to commit decode: %s\n", devname,
26ccd9
+			strerror(-rc));
26ccd9
+		goto err_delete;
26ccd9
+	}
26ccd9
+
26ccd9
+	rc = cxl_region_enable(region);
26ccd9
+	if (rc) {
26ccd9
+		log_err(&rl, "%s: failed to enable: %s\n", devname,
26ccd9
+			strerror(-rc));
26ccd9
+		goto err_delete;
26ccd9
+	}
26ccd9
+	*count = 1;
26ccd9
+
26ccd9
+	if (isatty(1))
26ccd9
+		flags |= UTIL_JSON_HUMAN;
26ccd9
+	jregion = util_cxl_region_to_json(region, flags);
26ccd9
+	if (jregion)
26ccd9
+		printf("%s\n", json_object_to_json_string_ext(jregion,
26ccd9
+					JSON_C_TO_STRING_PRETTY));
26ccd9
+
26ccd9
+	return 0;
26ccd9
+
26ccd9
+err_delete:
26ccd9
+	cxl_region_delete(region);
26ccd9
+	return rc;
26ccd9
+}
26ccd9
+
26ccd9
+static int region_action(int argc, const char **argv, struct cxl_ctx *ctx,
26ccd9
+			 enum region_actions action,
26ccd9
+			 const struct option *options, struct parsed_params *p,
26ccd9
+			 int *count, const char *u)
26ccd9
+{
26ccd9
+	int rc = -ENXIO;
26ccd9
+
26ccd9
+	log_init(&rl, "cxl region", "CXL_REGION_LOG");
26ccd9
+	rc = parse_region_options(argc, argv, ctx, action, options, p, u);
26ccd9
+	if (rc)
26ccd9
+		return rc;
26ccd9
+
26ccd9
+	if (action == ACTION_CREATE)
26ccd9
+		return create_region(ctx, count, p);
26ccd9
+
26ccd9
+	return rc;
26ccd9
+}
26ccd9
+
26ccd9
+int cmd_create_region(int argc, const char **argv, struct cxl_ctx *ctx)
26ccd9
+{
26ccd9
+	const char *u = "cxl create-region <target0> ... [<options>]";
26ccd9
+	struct parsed_params p = { 0 };
26ccd9
+	int rc, count = 0;
26ccd9
+
26ccd9
+	rc = region_action(argc, argv, ctx, ACTION_CREATE, create_options, &p,
26ccd9
+			   &count, u);
26ccd9
+	log_info(&rl, "created %d region%s\n", count, count == 1 ? "" : "s");
26ccd9
+	return rc == 0 ? 0 : EXIT_FAILURE;
26ccd9
+}
26ccd9
-- 
26ccd9
2.27.0
26ccd9