Blame SOURCES/0196-cxl-add-commands-to-enable-disable-destroy-region.patch

e0018b
From 93122376250a8a5cfae635e9729c34dfaa0fd116 Mon Sep 17 00:00:00 2001
e0018b
From: Vishal Verma <vishal.l.verma@intel.com>
e0018b
Date: Mon, 15 Aug 2022 13:22:11 -0600
e0018b
Subject: [PATCH 196/217] cxl: add commands to {enable,disable,destroy}-region
e0018b
e0018b
With a template from cxl-create-region in place, add its friends:
e0018b
e0018b
  cxl enable-region
e0018b
  cxl disable-region
e0018b
  cxl destroy-region
e0018b
e0018b
Link: https://lore.kernel.org/r/20220815192214.545800-9-vishal.l.verma@intel.com
e0018b
Cc: Dan Williams <dan.j.williams@intel.com>
e0018b
Reviewed-by: Dan Williams <dan.j.williams@intel.com>
e0018b
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
e0018b
---
e0018b
 Documentation/cxl/cxl-destroy-region.txt |  41 +++++
e0018b
 Documentation/cxl/cxl-disable-region.txt |  36 +++++
e0018b
 Documentation/cxl/cxl-enable-region.txt  |  36 +++++
e0018b
 Documentation/cxl/decoder-option.txt     |   6 +
e0018b
 Documentation/cxl/meson.build            |   4 +
e0018b
 cxl/builtin.h                            |   3 +
e0018b
 cxl/cxl.c                                |   3 +
e0018b
 cxl/region.c                             | 193 ++++++++++++++++++++++-
e0018b
 8 files changed, 321 insertions(+), 1 deletion(-)
e0018b
 create mode 100644 Documentation/cxl/cxl-destroy-region.txt
e0018b
 create mode 100644 Documentation/cxl/cxl-disable-region.txt
e0018b
 create mode 100644 Documentation/cxl/cxl-enable-region.txt
e0018b
 create mode 100644 Documentation/cxl/decoder-option.txt
e0018b
e0018b
diff --git a/Documentation/cxl/cxl-destroy-region.txt b/Documentation/cxl/cxl-destroy-region.txt
e0018b
new file mode 100644
e0018b
index 0000000..74f4093
e0018b
--- /dev/null
e0018b
+++ b/Documentation/cxl/cxl-destroy-region.txt
e0018b
@@ -0,0 +1,41 @@
e0018b
+// SPDX-License-Identifier: GPL-2.0
e0018b
+
e0018b
+cxl-destroy-region(1)
e0018b
+=====================
e0018b
+
e0018b
+NAME
e0018b
+----
e0018b
+cxl-destroy-region - destroy specified region(s).
e0018b
+
e0018b
+SYNOPSIS
e0018b
+--------
e0018b
+[verse]
e0018b
+'cxl destroy-region <region> [<options>]'
e0018b
+
e0018b
+include::region-description.txt[]
e0018b
+
e0018b
+EXAMPLE
e0018b
+-------
e0018b
+----
e0018b
+# cxl destroy-region all
e0018b
+destroyed 2 regions
e0018b
+----
e0018b
+
e0018b
+OPTIONS
e0018b
+-------
e0018b
+include::bus-option.txt[]
e0018b
+
e0018b
+-f::
e0018b
+--force::
e0018b
+	Force a destroy operation even if the region is active.
e0018b
+	This will attempt to disable the region first.
e0018b
+
e0018b
+include::decoder-option.txt[]
e0018b
+
e0018b
+include::debug-option.txt[]
e0018b
+
e0018b
+include::../copyright.txt[]
e0018b
+
e0018b
+SEE ALSO
e0018b
+--------
e0018b
+linkcxl:cxl-list[1], linkcxl:cxl-create-region[1]
e0018b
diff --git a/Documentation/cxl/cxl-disable-region.txt b/Documentation/cxl/cxl-disable-region.txt
e0018b
new file mode 100644
e0018b
index 0000000..6a39aee
e0018b
--- /dev/null
e0018b
+++ b/Documentation/cxl/cxl-disable-region.txt
e0018b
@@ -0,0 +1,36 @@
e0018b
+// SPDX-License-Identifier: GPL-2.0
e0018b
+
e0018b
+cxl-disable-region(1)
e0018b
+=====================
e0018b
+
e0018b
+NAME
e0018b
+----
e0018b
+cxl-disable-region - disable specified region(s).
e0018b
+
e0018b
+SYNOPSIS
e0018b
+--------
e0018b
+[verse]
e0018b
+'cxl disable-region <region> [<options>]'
e0018b
+
e0018b
+include::region-description.txt[]
e0018b
+
e0018b
+EXAMPLE
e0018b
+-------
e0018b
+----
e0018b
+# cxl disable-region all
e0018b
+disabled 2 regions
e0018b
+----
e0018b
+
e0018b
+OPTIONS
e0018b
+-------
e0018b
+include::bus-option.txt[]
e0018b
+
e0018b
+include::decoder-option.txt[]
e0018b
+
e0018b
+include::debug-option.txt[]
e0018b
+
e0018b
+include::../copyright.txt[]
e0018b
+
e0018b
+SEE ALSO
e0018b
+--------
e0018b
+linkcxl:cxl-list[1], linkcxl:cxl-enable-region[1]
e0018b
diff --git a/Documentation/cxl/cxl-enable-region.txt b/Documentation/cxl/cxl-enable-region.txt
e0018b
new file mode 100644
e0018b
index 0000000..f6ef00f
e0018b
--- /dev/null
e0018b
+++ b/Documentation/cxl/cxl-enable-region.txt
e0018b
@@ -0,0 +1,36 @@
e0018b
+// SPDX-License-Identifier: GPL-2.0
e0018b
+
e0018b
+cxl-enable-region(1)
e0018b
+=====================
e0018b
+
e0018b
+NAME
e0018b
+----
e0018b
+cxl-enable-region - enable specified region(s).
e0018b
+
e0018b
+SYNOPSIS
e0018b
+--------
e0018b
+[verse]
e0018b
+'cxl enable-region <region> [<options>]'
e0018b
+
e0018b
+include::region-description.txt[]
e0018b
+
e0018b
+EXAMPLE
e0018b
+-------
e0018b
+----
e0018b
+# cxl enable-region all
e0018b
+enabled 2 regions
e0018b
+----
e0018b
+
e0018b
+OPTIONS
e0018b
+-------
e0018b
+include::bus-option.txt[]
e0018b
+
e0018b
+include::decoder-option.txt[]
e0018b
+
e0018b
+include::debug-option.txt[]
e0018b
+
e0018b
+include::../copyright.txt[]
e0018b
+
e0018b
+SEE ALSO
e0018b
+--------
e0018b
+linkcxl:cxl-list[1], linkcxl:cxl-disable-region[1]
e0018b
diff --git a/Documentation/cxl/decoder-option.txt b/Documentation/cxl/decoder-option.txt
e0018b
new file mode 100644
e0018b
index 0000000..e638d6e
e0018b
--- /dev/null
e0018b
+++ b/Documentation/cxl/decoder-option.txt
e0018b
@@ -0,0 +1,6 @@
e0018b
+// SPDX-License-Identifier: GPL-2.0
e0018b
+
e0018b
+-d::
e0018b
+--decoder=::
e0018b
+	The root decoder to limit the operation to. Only regions that are
e0018b
+	children of the specified decoder will be acted upon.
e0018b
diff --git a/Documentation/cxl/meson.build b/Documentation/cxl/meson.build
e0018b
index 340cdee..147ea71 100644
e0018b
--- a/Documentation/cxl/meson.build
e0018b
+++ b/Documentation/cxl/meson.build
e0018b
@@ -24,6 +24,7 @@ filedeps = [
e0018b
   'labels-options.txt',
e0018b
   'debug-option.txt',
e0018b
   'region-description.txt',
e0018b
+  'decoder-option.txt',
e0018b
 ]
e0018b
 
e0018b
 cxl_manpages = [
e0018b
@@ -41,6 +42,9 @@ cxl_manpages = [
e0018b
   'cxl-reserve-dpa.txt',
e0018b
   'cxl-free-dpa.txt',
e0018b
   'cxl-create-region.txt',
e0018b
+  'cxl-disable-region.txt',
e0018b
+  'cxl-enable-region.txt',
e0018b
+  'cxl-destroy-region.txt',
e0018b
 ]
e0018b
 
e0018b
 foreach man : cxl_manpages
e0018b
diff --git a/cxl/builtin.h b/cxl/builtin.h
e0018b
index 843bada..b28c221 100644
e0018b
--- a/cxl/builtin.h
e0018b
+++ b/cxl/builtin.h
e0018b
@@ -19,4 +19,7 @@ int cmd_enable_port(int argc, const char **argv, struct cxl_ctx *ctx);
e0018b
 int cmd_set_partition(int argc, const char **argv, struct cxl_ctx *ctx);
e0018b
 int cmd_disable_bus(int argc, const char **argv, struct cxl_ctx *ctx);
e0018b
 int cmd_create_region(int argc, const char **argv, struct cxl_ctx *ctx);
e0018b
+int cmd_enable_region(int argc, const char **argv, struct cxl_ctx *ctx);
e0018b
+int cmd_disable_region(int argc, const char **argv, struct cxl_ctx *ctx);
e0018b
+int cmd_destroy_region(int argc, const char **argv, struct cxl_ctx *ctx);
e0018b
 #endif /* _CXL_BUILTIN_H_ */
e0018b
diff --git a/cxl/cxl.c b/cxl/cxl.c
e0018b
index f0afcfe..dd1be7a 100644
e0018b
--- a/cxl/cxl.c
e0018b
+++ b/cxl/cxl.c
e0018b
@@ -73,6 +73,9 @@ static struct cmd_struct commands[] = {
e0018b
 	{ "set-partition", .c_fn = cmd_set_partition },
e0018b
 	{ "disable-bus", .c_fn = cmd_disable_bus },
e0018b
 	{ "create-region", .c_fn = cmd_create_region },
e0018b
+	{ "enable-region", .c_fn = cmd_enable_region },
e0018b
+	{ "disable-region", .c_fn = cmd_disable_region },
e0018b
+	{ "destroy-region", .c_fn = cmd_destroy_region },
e0018b
 };
e0018b
 
e0018b
 int main(int argc, const char **argv)
e0018b
diff --git a/cxl/region.c b/cxl/region.c
e0018b
index 2791ac9..b22d3c8 100644
e0018b
--- a/cxl/region.c
e0018b
+++ b/cxl/region.c
e0018b
@@ -45,6 +45,9 @@ struct parsed_params {
e0018b
 
e0018b
 enum region_actions {
e0018b
 	ACTION_CREATE,
e0018b
+	ACTION_ENABLE,
e0018b
+	ACTION_DISABLE,
e0018b
+	ACTION_DESTROY,
e0018b
 };
e0018b
 
e0018b
 static struct log_ctx rl;
e0018b
@@ -78,7 +81,22 @@ static const struct option create_options[] = {
e0018b
 	OPT_END(),
e0018b
 };
e0018b
 
e0018b
+static const struct option enable_options[] = {
e0018b
+	BASE_OPTIONS(),
e0018b
+	OPT_END(),
e0018b
+};
e0018b
 
e0018b
+static const struct option disable_options[] = {
e0018b
+	BASE_OPTIONS(),
e0018b
+	OPT_END(),
e0018b
+};
e0018b
+
e0018b
+static const struct option destroy_options[] = {
e0018b
+	BASE_OPTIONS(),
e0018b
+	OPT_BOOLEAN('f', "force", &param.force,
e0018b
+		    "destroy region even if currently active"),
e0018b
+	OPT_END(),
e0018b
+};
e0018b
 
e0018b
 static int parse_create_options(int argc, const char **argv,
e0018b
 				struct parsed_params *p)
e0018b
@@ -519,12 +537,122 @@ err_delete:
e0018b
 	return rc;
e0018b
 }
e0018b
 
e0018b
+static int destroy_region(struct cxl_region *region)
e0018b
+{
e0018b
+	const char *devname = cxl_region_get_devname(region);
e0018b
+	unsigned int ways, i;
e0018b
+	int rc;
e0018b
+
e0018b
+	/* First, unbind/disable the region if needed */
e0018b
+	if (cxl_region_is_enabled(region)) {
e0018b
+		if (param.force) {
e0018b
+			rc = cxl_region_disable(region);
e0018b
+			if (rc) {
e0018b
+				log_err(&rl, "%s: error disabling region: %s\n",
e0018b
+					devname, strerror(-rc));
e0018b
+				return rc;
e0018b
+			}
e0018b
+		} else {
e0018b
+			log_err(&rl, "%s active. Disable it or use --force\n",
e0018b
+				devname);
e0018b
+			return -EBUSY;
e0018b
+		}
e0018b
+	}
e0018b
+
e0018b
+	/* Reset the region decode in preparation for removal */
e0018b
+	rc = cxl_region_decode_reset(region);
e0018b
+	if (rc) {
e0018b
+		log_err(&rl, "%s: failed to reset decode: %s\n", devname,
e0018b
+			strerror(-rc));
e0018b
+		return rc;
e0018b
+	}
e0018b
+
e0018b
+	/* Reset all endpoint decoders and region targets */
e0018b
+	ways = cxl_region_get_interleave_ways(region);
e0018b
+	if (ways == 0 || ways == UINT_MAX) {
e0018b
+		log_err(&rl, "%s: error getting interleave ways\n", devname);
e0018b
+		return -ENXIO;
e0018b
+	}
e0018b
+
e0018b
+	for (i = 0; i < ways; i++) {
e0018b
+		struct cxl_decoder *ep_decoder;
e0018b
+
e0018b
+		ep_decoder = cxl_region_get_target_decoder(region, i);
e0018b
+		if (!ep_decoder)
e0018b
+			return -ENXIO;
e0018b
+
e0018b
+		rc = cxl_region_clear_target(region, i);
e0018b
+		if (rc) {
e0018b
+			log_err(&rl, "%s: clearing target%d failed: %s\n",
e0018b
+				devname, i, strerror(abs(rc)));
e0018b
+			return rc;
e0018b
+		}
e0018b
+
e0018b
+		rc = cxl_decoder_set_dpa_size(ep_decoder, 0);
e0018b
+		if (rc) {
e0018b
+			log_err(&rl, "%s: set_dpa_size failed: %s\n",
e0018b
+				cxl_decoder_get_devname(ep_decoder),
e0018b
+				strerror(abs(rc)));
e0018b
+			return rc;
e0018b
+		}
e0018b
+	}
e0018b
+
e0018b
+	/* Finally, delete the region */
e0018b
+	return cxl_region_delete(region);
e0018b
+}
e0018b
+
e0018b
+static int do_region_xable(struct cxl_region *region, enum region_actions action)
e0018b
+{
e0018b
+	switch (action) {
e0018b
+	case ACTION_ENABLE:
e0018b
+		return cxl_region_enable(region);
e0018b
+	case ACTION_DISABLE:
e0018b
+		return cxl_region_disable(region);
e0018b
+	case ACTION_DESTROY:
e0018b
+		return destroy_region(region);
e0018b
+	default:
e0018b
+		return -EINVAL;
e0018b
+	}
e0018b
+}
e0018b
+
e0018b
+static int decoder_region_action(struct parsed_params *p,
e0018b
+				 struct cxl_decoder *decoder,
e0018b
+				 enum region_actions action, int *count)
e0018b
+{
e0018b
+	struct cxl_region *region, *_r;
e0018b
+	int rc = 0, err_rc = 0;
e0018b
+
e0018b
+	cxl_region_foreach_safe (decoder, region, _r) {
e0018b
+		int i, match = 0;
e0018b
+
e0018b
+		for (i = 0; i < p->num_targets; i++) {
e0018b
+			if (util_cxl_region_filter(region, p->targets[i])) {
e0018b
+				match = 1;
e0018b
+				break;
e0018b
+			}
e0018b
+		}
e0018b
+		if (!match)
e0018b
+			continue;
e0018b
+
e0018b
+		rc = do_region_xable(region, action);
e0018b
+		if (rc == 0) {
e0018b
+			*count += 1;
e0018b
+		} else {
e0018b
+			log_err(&rl, "%s: failed: %s\n",
e0018b
+				cxl_region_get_devname(region), strerror(-rc));
e0018b
+			err_rc = rc;
e0018b
+		}
e0018b
+	}
e0018b
+	return err_rc ? err_rc : rc;
e0018b
+}
e0018b
+
e0018b
 static int region_action(int argc, const char **argv, struct cxl_ctx *ctx,
e0018b
 			 enum region_actions action,
e0018b
 			 const struct option *options, struct parsed_params *p,
e0018b
 			 int *count, const char *u)
e0018b
 {
e0018b
-	int rc = -ENXIO;
e0018b
+	int rc = 0, err_rc = 0;
e0018b
+	struct cxl_bus *bus;
e0018b
 
e0018b
 	log_init(&rl, "cxl region", "CXL_REGION_LOG");
e0018b
 	rc = parse_region_options(argc, argv, ctx, action, options, p, u);
e0018b
@@ -534,6 +662,33 @@ static int region_action(int argc, const char **argv, struct cxl_ctx *ctx,
e0018b
 	if (action == ACTION_CREATE)
e0018b
 		return create_region(ctx, count, p);
e0018b
 
e0018b
+	cxl_bus_foreach(ctx, bus) {
e0018b
+		struct cxl_decoder *decoder;
e0018b
+		struct cxl_port *port;
e0018b
+
e0018b
+		if (!util_cxl_bus_filter(bus, param.bus))
e0018b
+			continue;
e0018b
+
e0018b
+		port = cxl_bus_get_port(bus);
e0018b
+		if (!cxl_port_is_root(port))
e0018b
+			continue;
e0018b
+
e0018b
+		cxl_decoder_foreach (port, decoder) {
e0018b
+			decoder = util_cxl_decoder_filter(decoder,
e0018b
+							  param.root_decoder);
e0018b
+			if (!decoder)
e0018b
+				continue;
e0018b
+			rc = decoder_region_action(p, decoder, action, count);
e0018b
+			if (rc)
e0018b
+				err_rc = rc;
e0018b
+		}
e0018b
+	}
e0018b
+
e0018b
+	if (err_rc) {
e0018b
+		log_err(&rl, "one or more failures, last failure: %s\n",
e0018b
+			strerror(-err_rc));
e0018b
+		return err_rc;
e0018b
+	}
e0018b
 	return rc;
e0018b
 }
e0018b
 
e0018b
@@ -548,3 +703,39 @@ int cmd_create_region(int argc, const char **argv, struct cxl_ctx *ctx)
e0018b
 	log_info(&rl, "created %d region%s\n", count, count == 1 ? "" : "s");
e0018b
 	return rc == 0 ? 0 : EXIT_FAILURE;
e0018b
 }
e0018b
+
e0018b
+int cmd_enable_region(int argc, const char **argv, struct cxl_ctx *ctx)
e0018b
+{
e0018b
+	const char *u = "cxl enable-region <region0> ... [<options>]";
e0018b
+	struct parsed_params p = { 0 };
e0018b
+	int rc, count = 0;
e0018b
+
e0018b
+	rc = region_action(argc, argv, ctx, ACTION_ENABLE, enable_options, &p,
e0018b
+			   &count, u);
e0018b
+	log_info(&rl, "enabled %d region%s\n", count, count == 1 ? "" : "s");
e0018b
+	return rc == 0 ? 0 : EXIT_FAILURE;
e0018b
+}
e0018b
+
e0018b
+int cmd_disable_region(int argc, const char **argv, struct cxl_ctx *ctx)
e0018b
+{
e0018b
+	const char *u = "cxl disable-region <region0> ... [<options>]";
e0018b
+	struct parsed_params p = { 0 };
e0018b
+	int rc, count = 0;
e0018b
+
e0018b
+	rc = region_action(argc, argv, ctx, ACTION_DISABLE, disable_options, &p,
e0018b
+			   &count, u);
e0018b
+	log_info(&rl, "disabled %d region%s\n", count, count == 1 ? "" : "s");
e0018b
+	return rc == 0 ? 0 : EXIT_FAILURE;
e0018b
+}
e0018b
+
e0018b
+int cmd_destroy_region(int argc, const char **argv, struct cxl_ctx *ctx)
e0018b
+{
e0018b
+	const char *u = "cxl destroy-region <region0> ... [<options>]";
e0018b
+	struct parsed_params p = { 0 };
e0018b
+	int rc, count = 0;
e0018b
+
e0018b
+	rc = region_action(argc, argv, ctx, ACTION_DESTROY, destroy_options, &p,
e0018b
+			   &count, u);
e0018b
+	log_info(&rl, "destroyed %d region%s\n", count, count == 1 ? "" : "s");
e0018b
+	return rc == 0 ? 0 : EXIT_FAILURE;
e0018b
+}
e0018b
-- 
e0018b
2.27.0
e0018b