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

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