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

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