anitazha / rpms / ndctl

Forked from rpms/ndctl 2 years ago
Clone

Blame SOURCES/0159-cxl-bus-Add-bus-disable-support.patch

e0018b
From 8a35aa8fd3e1db06228329a0ca900ce246ca329e Mon Sep 17 00:00:00 2001
e0018b
From: Dan Williams <dan.j.williams@intel.com>
e0018b
Date: Thu, 28 Apr 2022 15:10:27 -0700
e0018b
Subject: [PATCH 159/217] cxl/bus: Add bus disable support
e0018b
e0018b
Route requests to disable the root back to unbinding the platform firmware
e0018b
device, ACPI0017 for ACPI.CXL platforms.
e0018b
e0018b
Link: https://lore.kernel.org/r/165118382738.1676208.16851880881648171660.stgit@dwillia2-desk3.amr.corp.intel.com
e0018b
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
e0018b
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
e0018b
---
e0018b
 Documentation/cxl/cxl-disable-bus.txt |  37 ++++++
e0018b
 Documentation/cxl/lib/libcxl.txt      |  12 ++
e0018b
 Documentation/cxl/meson.build         |   1 +
e0018b
 cxl/builtin.h                         |   1 +
e0018b
 cxl/bus.c                             | 159 ++++++++++++++++++++++++++
e0018b
 cxl/cxl.c                             |   1 +
e0018b
 cxl/filter.c                          |   3 +-
e0018b
 cxl/filter.h                          |   1 +
e0018b
 cxl/lib/libcxl.c                      |  15 +++
e0018b
 cxl/lib/libcxl.sym                    |   1 +
e0018b
 cxl/libcxl.h                          |   1 +
e0018b
 cxl/meson.build                       |   1 +
e0018b
 12 files changed, 231 insertions(+), 2 deletions(-)
e0018b
 create mode 100644 Documentation/cxl/cxl-disable-bus.txt
e0018b
 create mode 100644 cxl/bus.c
e0018b
e0018b
diff --git a/Documentation/cxl/cxl-disable-bus.txt b/Documentation/cxl/cxl-disable-bus.txt
e0018b
new file mode 100644
e0018b
index 0000000..65f695c
e0018b
--- /dev/null
e0018b
+++ b/Documentation/cxl/cxl-disable-bus.txt
e0018b
@@ -0,0 +1,37 @@
e0018b
+// SPDX-License-Identifier: GPL-2.0
e0018b
+
e0018b
+cxl-disable-bus(1)
e0018b
+===================
e0018b
+
e0018b
+NAME
e0018b
+----
e0018b
+cxl-disable-bus - Shutdown an entire tree of CXL devices
e0018b
+
e0018b
+SYNOPSIS
e0018b
+--------
e0018b
+[verse]
e0018b
+'cxl disable-bus' <root0> [<root1>..<rootN>] [<options>]
e0018b
+
e0018b
+For test and debug scenarios, disable a CXL bus and any associated
e0018b
+memory devices from CXL.mem operations.
e0018b
+
e0018b
+OPTIONS
e0018b
+-------
e0018b
+-f::
e0018b
+--force::
e0018b
+	DANGEROUS: Override the safety measure that blocks attempts to disable a
e0018b
+	bus if the tool determines a descendent memdev is in active usage.
e0018b
+	Recall that CXL memory ranges might have been established by platform
e0018b
+	firmware and disabling an active device is akin to force removing memory
e0018b
+	from a running system.
e0018b
+
e0018b
+--debug::
e0018b
+	If the cxl tool was built with debug disabled, turn on debug
e0018b
+	messages.
e0018b
+
e0018b
+
e0018b
+include::../copyright.txt[]
e0018b
+
e0018b
+SEE ALSO
e0018b
+--------
e0018b
+linkcxl:cxl-disable-port[1]
e0018b
diff --git a/Documentation/cxl/lib/libcxl.txt b/Documentation/cxl/lib/libcxl.txt
e0018b
index 7b223cb..f8f0e66 100644
e0018b
--- a/Documentation/cxl/lib/libcxl.txt
e0018b
+++ b/Documentation/cxl/lib/libcxl.txt
e0018b
@@ -216,6 +216,18 @@ discovery order. The possible provider names are 'ACPI.CXL' and
e0018b
 the kernel device names that are subject to change based on discovery
e0018b
 order.
e0018b
 
e0018b
+=== BUS: Control
e0018b
+----
e0018b
+int cxl_bus_disable_invalidate(struct cxl_bus *bus);
e0018b
+----
e0018b
+
e0018b
+An entire CXL topology can be torn down with this API. Like other
e0018b
+_invalidate APIs callers must assume that all library objects have been
e0018b
+freed. This one goes one step further and also frees the @bus argument.
e0018b
+This may crash the system and is only useful in kernel driver
e0018b
+development scenarios.
e0018b
+
e0018b
+
e0018b
 PORTS
e0018b
 -----
e0018b
 CXL ports track the PCIe hierarchy between a platform firmware CXL root
e0018b
diff --git a/Documentation/cxl/meson.build b/Documentation/cxl/meson.build
e0018b
index e927644..974a5a4 100644
e0018b
--- a/Documentation/cxl/meson.build
e0018b
+++ b/Documentation/cxl/meson.build
e0018b
@@ -34,6 +34,7 @@ cxl_manpages = [
e0018b
   'cxl-disable-memdev.txt',
e0018b
   'cxl-enable-port.txt',
e0018b
   'cxl-disable-port.txt',
e0018b
+  'cxl-disable-bus.txt',
e0018b
   'cxl-set-partition.txt',
e0018b
 ]
e0018b
 
e0018b
diff --git a/cxl/builtin.h b/cxl/builtin.h
e0018b
index 7bbad98..a437bc3 100644
e0018b
--- a/cxl/builtin.h
e0018b
+++ b/cxl/builtin.h
e0018b
@@ -15,4 +15,5 @@ int cmd_enable_memdev(int argc, const char **argv, struct cxl_ctx *ctx);
e0018b
 int cmd_disable_port(int argc, const char **argv, struct cxl_ctx *ctx);
e0018b
 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
 #endif /* _CXL_BUILTIN_H_ */
e0018b
diff --git a/cxl/bus.c b/cxl/bus.c
e0018b
new file mode 100644
e0018b
index 0000000..3321295
e0018b
--- /dev/null
e0018b
+++ b/cxl/bus.c
e0018b
@@ -0,0 +1,159 @@
e0018b
+// SPDX-License-Identifier: GPL-2.0
e0018b
+/* Copyright (C) 2020-2022 Intel Corporation. All rights reserved. */
e0018b
+#include <stdio.h>
e0018b
+#include <errno.h>
e0018b
+#include <stdlib.h>
e0018b
+#include <unistd.h>
e0018b
+#include <limits.h>
e0018b
+#include <util/log.h>
e0018b
+#include <cxl/libcxl.h>
e0018b
+#include <util/parse-options.h>
e0018b
+#include <ccan/minmax/minmax.h>
e0018b
+#include <ccan/array_size/array_size.h>
e0018b
+
e0018b
+#include "filter.h"
e0018b
+
e0018b
+static struct parameters {
e0018b
+	bool debug;
e0018b
+	bool force;
e0018b
+} param;
e0018b
+
e0018b
+static struct log_ctx bl;
e0018b
+
e0018b
+#define BASE_OPTIONS()                                                 \
e0018b
+OPT_BOOLEAN(0, "debug", &param.debug, "turn on debug")
e0018b
+
e0018b
+#define DISABLE_OPTIONS()                                              \
e0018b
+OPT_BOOLEAN('f', "force", &param.force,                                \
e0018b
+	    "DANGEROUS: override active memdev safety checks")
e0018b
+
e0018b
+static const struct option disable_options[] = {
e0018b
+	BASE_OPTIONS(),
e0018b
+	DISABLE_OPTIONS(),
e0018b
+	OPT_END(),
e0018b
+};
e0018b
+
e0018b
+static int action_disable(struct cxl_bus *bus)
e0018b
+{
e0018b
+	const char *devname = cxl_bus_get_devname(bus);
e0018b
+	struct cxl_ctx *ctx = cxl_bus_get_ctx(bus);
e0018b
+	struct cxl_memdev *memdev;
e0018b
+	int active_memdevs = 0;
e0018b
+
e0018b
+	cxl_memdev_foreach(ctx, memdev)
e0018b
+		if (bus == cxl_memdev_get_bus(memdev))
e0018b
+			active_memdevs++;
e0018b
+
e0018b
+	if (active_memdevs && !param.force) {
e0018b
+		/*
e0018b
+		 * TODO: actually detect rather than assume active just
e0018b
+		 * because the memdev is enabled
e0018b
+		 */
e0018b
+		log_err(&bl,
e0018b
+			"%s hosts %d memdev%s which %s part of an active region\n",
e0018b
+			devname, active_memdevs, active_memdevs > 1 ? "s" : "",
e0018b
+			active_memdevs > 1 ? "are" : "is");
e0018b
+		log_err(&bl,
e0018b
+			"See 'cxl list -M -b %s' to see impacted device%s\n",
e0018b
+			devname, active_memdevs > 1 ? "s" : "");
e0018b
+		return -EBUSY;
e0018b
+	}
e0018b
+
e0018b
+	return cxl_bus_disable_invalidate(bus);
e0018b
+}
e0018b
+
e0018b
+static struct cxl_bus *find_cxl_bus(struct cxl_ctx *ctx, const char *ident)
e0018b
+{
e0018b
+	struct cxl_bus *bus;
e0018b
+
e0018b
+	cxl_bus_foreach(ctx, bus)
e0018b
+		if (util_cxl_bus_filter(bus, ident))
e0018b
+			return bus;
e0018b
+	return NULL;
e0018b
+}
e0018b
+
e0018b
+static int bus_action(int argc, const char **argv, struct cxl_ctx *ctx,
e0018b
+		      int (*action)(struct cxl_bus *bus),
e0018b
+		      const struct option *options, const char *usage)
e0018b
+{
e0018b
+	int i, rc = 0, count = 0, err = 0;
e0018b
+	const char * const u[] = {
e0018b
+		usage,
e0018b
+		NULL
e0018b
+	};
e0018b
+	unsigned long id;
e0018b
+
e0018b
+	log_init(&bl, "cxl bus", "CXL_PORT_LOG");
e0018b
+	argc = parse_options(argc, argv, options, u, 0);
e0018b
+
e0018b
+	if (argc == 0)
e0018b
+		usage_with_options(u, options);
e0018b
+	for (i = 0; i < argc; i++) {
e0018b
+		if (strcmp(argv[i], "all") == 0) {
e0018b
+			argv[0] = "all";
e0018b
+			argc = 1;
e0018b
+			break;
e0018b
+		}
e0018b
+
e0018b
+		if (sscanf(argv[i], "root%lu", &id) == 1)
e0018b
+			continue;
e0018b
+		if (sscanf(argv[i], "%lu", &id) == 1)
e0018b
+			continue;
e0018b
+
e0018b
+		log_err(&bl, "'%s' is not a valid bus identifer\n", argv[i]);
e0018b
+		err++;
e0018b
+	}
e0018b
+
e0018b
+	if (err == argc) {
e0018b
+		usage_with_options(u, options);
e0018b
+		return -EINVAL;
e0018b
+	}
e0018b
+
e0018b
+	if (param.debug) {
e0018b
+		cxl_set_log_priority(ctx, LOG_DEBUG);
e0018b
+		bl.log_priority = LOG_DEBUG;
e0018b
+	} else
e0018b
+		bl.log_priority = LOG_INFO;
e0018b
+
e0018b
+	rc = 0;
e0018b
+	err = 0;
e0018b
+	count = 0;
e0018b
+
e0018b
+	for (i = 0; i < argc; i++) {
e0018b
+		struct cxl_bus *bus;
e0018b
+
e0018b
+		bus = find_cxl_bus(ctx, argv[i]);
e0018b
+		if (!bus) {
e0018b
+			log_dbg(&bl, "bus: %s not found\n", argv[i]);
e0018b
+			continue;
e0018b
+		}
e0018b
+
e0018b
+		log_dbg(&bl, "run action on bus: %s\n",
e0018b
+			cxl_bus_get_devname(bus));
e0018b
+		rc = action(bus);
e0018b
+		if (rc == 0)
e0018b
+			count++;
e0018b
+		else if (rc && !err)
e0018b
+			err = rc;
e0018b
+	}
e0018b
+	rc = err;
e0018b
+
e0018b
+	/*
e0018b
+	 * count if some actions succeeded, 0 if none were attempted,
e0018b
+	 * negative error code otherwise.
e0018b
+	 */
e0018b
+	if (count > 0)
e0018b
+		return count;
e0018b
+	return rc;
e0018b
+}
e0018b
+
e0018b
+ int cmd_disable_bus(int argc, const char **argv, struct cxl_ctx *ctx)
e0018b
+ {
e0018b
+	 int count = bus_action(
e0018b
+		 argc, argv, ctx, action_disable, disable_options,
e0018b
+		 "cxl disable-bus <bus0> [<bus1>..<busN>] [<options>]");
e0018b
+
e0018b
+	 log_info(&bl, "disabled %d bus%s\n", count >= 0 ? count : 0,
e0018b
+		  count > 1 ? "s" : "");
e0018b
+	 return count >= 0 ? 0 : EXIT_FAILURE;
e0018b
+ }
e0018b
diff --git a/cxl/cxl.c b/cxl/cxl.c
e0018b
index ab4bbec..aa4ce61 100644
e0018b
--- a/cxl/cxl.c
e0018b
+++ b/cxl/cxl.c
e0018b
@@ -69,6 +69,7 @@ static struct cmd_struct commands[] = {
e0018b
 	{ "disable-port", .c_fn = cmd_disable_port },
e0018b
 	{ "enable-port", .c_fn = cmd_enable_port },
e0018b
 	{ "set-partition", .c_fn = cmd_set_partition },
e0018b
+	{ "disable-bus", .c_fn = cmd_disable_bus },
e0018b
 };
e0018b
 
e0018b
 int main(int argc, const char **argv)
e0018b
diff --git a/cxl/filter.c b/cxl/filter.c
e0018b
index b339642..c6ab9eb 100644
e0018b
--- a/cxl/filter.c
e0018b
+++ b/cxl/filter.c
e0018b
@@ -176,8 +176,7 @@ util_cxl_decoder_filter_by_port(struct cxl_decoder *decoder, const char *ident,
e0018b
 	return NULL;
e0018b
 }
e0018b
 
e0018b
-static struct cxl_bus *util_cxl_bus_filter(struct cxl_bus *bus,
e0018b
-					   const char *__ident)
e0018b
+struct cxl_bus *util_cxl_bus_filter(struct cxl_bus *bus, const char *__ident)
e0018b
 {
e0018b
 	char *ident, *save;
e0018b
 	const char *arg;
e0018b
diff --git a/cxl/filter.h b/cxl/filter.h
e0018b
index 697b777..9557943 100644
e0018b
--- a/cxl/filter.h
e0018b
+++ b/cxl/filter.h
e0018b
@@ -41,6 +41,7 @@ enum cxl_port_filter_mode {
e0018b
 
e0018b
 struct cxl_port *util_cxl_port_filter(struct cxl_port *port, const char *ident,
e0018b
 				      enum cxl_port_filter_mode mode);
e0018b
+struct cxl_bus *util_cxl_bus_filter(struct cxl_bus *bus, const char *__ident);
e0018b
 struct cxl_endpoint *util_cxl_endpoint_filter(struct cxl_endpoint *endpoint,
e0018b
 					      const char *__ident);
e0018b
 struct cxl_target *util_cxl_target_filter_by_memdev(struct cxl_target *target,
e0018b
diff --git a/cxl/lib/libcxl.c b/cxl/lib/libcxl.c
e0018b
index 59e1644..0e8dd20 100644
e0018b
--- a/cxl/lib/libcxl.c
e0018b
+++ b/cxl/lib/libcxl.c
e0018b
@@ -556,6 +556,21 @@ static void bus_invalidate(struct cxl_bus *bus)
e0018b
 	cxl_flush(ctx);
e0018b
 }
e0018b
 
e0018b
+CXL_EXPORT int cxl_bus_disable_invalidate(struct cxl_bus *bus)
e0018b
+{
e0018b
+	struct cxl_ctx *ctx = cxl_bus_get_ctx(bus);
e0018b
+	struct cxl_port *port = cxl_bus_get_port(bus);
e0018b
+	int rc;
e0018b
+
e0018b
+	rc = util_unbind(port->uport, ctx);
e0018b
+	if (rc)
e0018b
+		return rc;
e0018b
+
e0018b
+	free_bus(bus, &ctx->buses);
e0018b
+	cxl_flush(ctx);
e0018b
+	return 0;
e0018b
+}
e0018b
+
e0018b
 CXL_EXPORT int cxl_memdev_disable_invalidate(struct cxl_memdev *memdev)
e0018b
 {
e0018b
 	struct cxl_ctx *ctx = cxl_memdev_get_ctx(memdev);
e0018b
diff --git a/cxl/lib/libcxl.sym b/cxl/lib/libcxl.sym
e0018b
index aab1112..dffcb60 100644
e0018b
--- a/cxl/lib/libcxl.sym
e0018b
+++ b/cxl/lib/libcxl.sym
e0018b
@@ -86,6 +86,7 @@ global:
e0018b
 	cxl_bus_get_id;
e0018b
 	cxl_bus_get_port;
e0018b
 	cxl_bus_get_ctx;
e0018b
+	cxl_bus_disable_invalidate;
e0018b
 	cxl_port_get_first;
e0018b
 	cxl_port_get_next;
e0018b
 	cxl_port_get_devname;
e0018b
diff --git a/cxl/libcxl.h b/cxl/libcxl.h
e0018b
index 0063d31..0007f4d 100644
e0018b
--- a/cxl/libcxl.h
e0018b
+++ b/cxl/libcxl.h
e0018b
@@ -73,6 +73,7 @@ const char *cxl_bus_get_devname(struct cxl_bus *bus);
e0018b
 int cxl_bus_get_id(struct cxl_bus *bus);
e0018b
 struct cxl_port *cxl_bus_get_port(struct cxl_bus *bus);
e0018b
 struct cxl_ctx *cxl_bus_get_ctx(struct cxl_bus *bus);
e0018b
+int cxl_bus_disable_invalidate(struct cxl_bus *bus);
e0018b
 
e0018b
 #define cxl_bus_foreach(ctx, bus)                                              \
e0018b
 	for (bus = cxl_bus_get_first(ctx); bus != NULL;                        \
e0018b
diff --git a/cxl/meson.build b/cxl/meson.build
e0018b
index 671c8e1..d63dcb1 100644
e0018b
--- a/cxl/meson.build
e0018b
+++ b/cxl/meson.build
e0018b
@@ -2,6 +2,7 @@ cxl_src = [
e0018b
   'cxl.c',
e0018b
   'list.c',
e0018b
   'port.c',
e0018b
+  'bus.c',
e0018b
   'memdev.c',
e0018b
   'json.c',
e0018b
   'filter.c',
e0018b
-- 
e0018b
2.27.0
e0018b