Blame SOURCES/0121-cxl-list-Support-filtering-memdevs-by-ports.patch

2eb93d
From 15cae420681c5e8efad2b4cbaf0470960e2eba52 Mon Sep 17 00:00:00 2001
2eb93d
From: Dan Williams <dan.j.williams@intel.com>
2eb93d
Date: Sun, 23 Jan 2022 16:54:55 -0800
2eb93d
Subject: [PATCH 121/217] cxl/list: Support filtering memdevs by ports
2eb93d
2eb93d
The ability to filter memdevs by decoders falls short when the decoder does
2eb93d
not have its target list programmed. So, introduce a by port filter to show
2eb93d
the potential memdevs that can be targeted by the decoder.
2eb93d
2eb93d
Link: https://lore.kernel.org/r/164298569568.3021641.888802471376117408.stgit@dwillia2-desk3.amr.corp.intel.com
2eb93d
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
2eb93d
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
2eb93d
---
2eb93d
 Documentation/cxl/cxl-list.txt   |  3 +-
2eb93d
 Documentation/cxl/lib/libcxl.txt |  7 ++++-
2eb93d
 cxl/filter.c                     | 50 ++++++++++++++++++++++++++++++++
2eb93d
 cxl/lib/libcxl.c                 | 23 +++++++++++++++
2eb93d
 cxl/lib/libcxl.sym               |  2 ++
2eb93d
 cxl/libcxl.h                     |  3 ++
2eb93d
 6 files changed, 86 insertions(+), 2 deletions(-)
2eb93d
2eb93d
diff --git a/Documentation/cxl/cxl-list.txt b/Documentation/cxl/cxl-list.txt
2eb93d
index 04e831e..90e6d9f 100644
2eb93d
--- a/Documentation/cxl/cxl-list.txt
2eb93d
+++ b/Documentation/cxl/cxl-list.txt
2eb93d
@@ -63,7 +63,8 @@ one or more memdevs. For example:
2eb93d
 ----
2eb93d
 Additionally, when provisioning new interleave configurations it is
2eb93d
 useful to know which memdevs can be referenced by a given decoder like a
2eb93d
-root decoder:
2eb93d
+root decoder, or mapped by a given port if the decoders are not
2eb93d
+configured.
2eb93d
 ----
2eb93d
 # cxl list -Mu -d decoder0.0
2eb93d
 {
2eb93d
diff --git a/Documentation/cxl/lib/libcxl.txt b/Documentation/cxl/lib/libcxl.txt
2eb93d
index 5ad3027..a0fcee9 100644
2eb93d
--- a/Documentation/cxl/lib/libcxl.txt
2eb93d
+++ b/Documentation/cxl/lib/libcxl.txt
2eb93d
@@ -276,11 +276,12 @@ CXL / PCIe host bridge.
2eb93d
 ----
2eb93d
 struct cxl_dport *cxl_dport_get_first(struct cxl_port *port);
2eb93d
 struct cxl_dport *cxl_dport_get_next(struct cxl_dport *dport);
2eb93d
+struct cxl_dport *cxl_port_get_dport_by_memdev(struct cxl_port *port,
2eb93d
+                                               struct cxl_memdev *memdev);
2eb93d
 
2eb93d
 #define cxl_dport_foreach(port, dport)                                     \
2eb93d
        for (dport = cxl_dport_get_first(port); dport != NULL;              \
2eb93d
             dport = cxl_dport_get_next(dport))
2eb93d
-
2eb93d
 ----
2eb93d
 
2eb93d
 ===== DPORT: Attributes
2eb93d
@@ -288,12 +289,16 @@ struct cxl_dport *cxl_dport_get_next(struct cxl_dport *dport);
2eb93d
 const char *cxl_dport_get_devname(struct cxl_dport *dport);
2eb93d
 const char *cxl_dport_get_physical_node(struct cxl_dport *dport);
2eb93d
 int cxl_dport_get_id(struct cxl_dport *dport);
2eb93d
+bool cxl_dport_maps_memdev(struct cxl_dport *dport, struct cxl_memdev *memdev);
2eb93d
 ----
2eb93d
 The id of a dport is the hardware idenfifier used by an upstream port to
2eb93d
 reference a downstream port. The physical node of a dport is only
2eb93d
 available for platform firmware defined downstream ports and alias the
2eb93d
 companion object, like a PCI host bridge, in the PCI device hierarchy.
2eb93d
 
2eb93d
+The cxl_dport_maps_memdev() helper checks if a dport is an ancestor of a
2eb93d
+given memdev.
2eb93d
+
2eb93d
 ENDPOINTS
2eb93d
 ---------
2eb93d
 CXL endpoint objects encapsulate the set of host-managed device-memory
2eb93d
diff --git a/cxl/filter.c b/cxl/filter.c
2eb93d
index c972545..c691edf 100644
2eb93d
--- a/cxl/filter.c
2eb93d
+++ b/cxl/filter.c
2eb93d
@@ -486,6 +486,53 @@ util_cxl_memdev_filter_by_decoder(struct cxl_memdev *memdev, const char *ident)
2eb93d
 	return NULL;
2eb93d
 }
2eb93d
 
2eb93d
+static bool __memdev_filter_by_port(struct cxl_memdev *memdev,
2eb93d
+				    struct cxl_port *port,
2eb93d
+				    const char *port_ident)
2eb93d
+{
2eb93d
+	struct cxl_endpoint *endpoint;
2eb93d
+
2eb93d
+	if (util_cxl_port_filter(port, port_ident, CXL_PF_SINGLE) &&
2eb93d
+	    cxl_port_get_dport_by_memdev(port, memdev))
2eb93d
+		return true;
2eb93d
+
2eb93d
+	cxl_endpoint_foreach(port, endpoint)
2eb93d
+		if (__memdev_filter_by_port(memdev,
2eb93d
+					    cxl_endpoint_get_port(endpoint),
2eb93d
+					    port_ident))
2eb93d
+			return true;
2eb93d
+	return false;
2eb93d
+}
2eb93d
+
2eb93d
+static struct cxl_memdev *
2eb93d
+util_cxl_memdev_filter_by_port(struct cxl_memdev *memdev, const char *bus_ident,
2eb93d
+			       const char *port_ident)
2eb93d
+{
2eb93d
+	struct cxl_ctx *ctx = cxl_memdev_get_ctx(memdev);
2eb93d
+	struct cxl_bus *bus;
2eb93d
+
2eb93d
+	if (!bus_ident && !port_ident)
2eb93d
+		return memdev;
2eb93d
+
2eb93d
+	cxl_bus_foreach(ctx, bus) {
2eb93d
+		struct cxl_port *port, *top;
2eb93d
+
2eb93d
+		port = cxl_bus_get_port(bus);
2eb93d
+		if (util_cxl_bus_filter(bus, bus_ident))
2eb93d
+			if (__memdev_filter_by_port(memdev, port,
2eb93d
+						    cxl_bus_get_devname(bus)))
2eb93d
+				return memdev;
2eb93d
+		if (__memdev_filter_by_port(memdev, port, port_ident))
2eb93d
+				return memdev;
2eb93d
+		top = port;
2eb93d
+		cxl_port_foreach_all(top, port)
2eb93d
+			if (__memdev_filter_by_port(memdev, port, port_ident))
2eb93d
+				return memdev;
2eb93d
+	}
2eb93d
+
2eb93d
+	return NULL;
2eb93d
+}
2eb93d
+
2eb93d
 static unsigned long params_to_flags(struct cxl_filter_params *param)
2eb93d
 {
2eb93d
 	unsigned long flags = 0;
2eb93d
@@ -647,6 +694,9 @@ static void walk_endpoints(struct cxl_port *port, struct cxl_filter_params *p,
2eb93d
 			if (!util_cxl_memdev_filter_by_decoder(
2eb93d
 				    memdev, p->decoder_filter))
2eb93d
 				continue;
2eb93d
+			if (!util_cxl_memdev_filter_by_port(
2eb93d
+				    memdev, p->bus_filter, p->port_filter))
2eb93d
+				continue;
2eb93d
 			if (!p->idle && !cxl_memdev_is_enabled(memdev))
2eb93d
 				continue;
2eb93d
 			jobj = util_cxl_memdev_to_json(memdev, flags);
2eb93d
diff --git a/cxl/lib/libcxl.c b/cxl/lib/libcxl.c
2eb93d
index 4ebb8b9..dcfc826 100644
2eb93d
--- a/cxl/lib/libcxl.c
2eb93d
+++ b/cxl/lib/libcxl.c
2eb93d
@@ -1452,6 +1452,29 @@ CXL_EXPORT int cxl_dport_get_id(struct cxl_dport *dport)
2eb93d
 	return dport->id;
2eb93d
 }
2eb93d
 
2eb93d
+CXL_EXPORT bool cxl_dport_maps_memdev(struct cxl_dport *dport,
2eb93d
+				      struct cxl_memdev *memdev)
2eb93d
+{
2eb93d
+	struct cxl_ctx *ctx = cxl_memdev_get_ctx(memdev);
2eb93d
+
2eb93d
+	dbg(ctx, "memdev: %s dport: %s\n", memdev->host_path, dport->dev_path);
2eb93d
+
2eb93d
+	if (dport->phys_path)
2eb93d
+		return !!strstr(memdev->host_path, dport->phys_path);
2eb93d
+	return !!strstr(memdev->host_path, dport->dev_path);
2eb93d
+}
2eb93d
+
2eb93d
+CXL_EXPORT struct cxl_dport *
2eb93d
+cxl_port_get_dport_by_memdev(struct cxl_port *port, struct cxl_memdev *memdev)
2eb93d
+{
2eb93d
+	struct cxl_dport *dport;
2eb93d
+
2eb93d
+	cxl_dport_foreach(port, dport)
2eb93d
+		if (cxl_dport_maps_memdev(dport, memdev))
2eb93d
+			return dport;
2eb93d
+	return NULL;
2eb93d
+}
2eb93d
+
2eb93d
 static void *add_cxl_bus(void *parent, int id, const char *cxlbus_base)
2eb93d
 {
2eb93d
 	const char *devname = devpath_to_devname(cxlbus_base);
2eb93d
diff --git a/cxl/lib/libcxl.sym b/cxl/lib/libcxl.sym
2eb93d
index 0190b13..2c8358e 100644
2eb93d
--- a/cxl/lib/libcxl.sym
2eb93d
+++ b/cxl/lib/libcxl.sym
2eb93d
@@ -149,4 +149,6 @@ global:
2eb93d
 	cxl_dport_get_devname;
2eb93d
 	cxl_dport_get_physical_node;
2eb93d
 	cxl_dport_get_id;
2eb93d
+	cxl_port_get_dport_by_memdev;
2eb93d
+	cxl_dport_maps_memdev;
2eb93d
 } LIBCXL_1;
2eb93d
diff --git a/cxl/libcxl.h b/cxl/libcxl.h
2eb93d
index 874c381..c8d07bb 100644
2eb93d
--- a/cxl/libcxl.h
2eb93d
+++ b/cxl/libcxl.h
2eb93d
@@ -111,6 +111,9 @@ struct cxl_dport *cxl_dport_get_next(struct cxl_dport *dport);
2eb93d
 const char *cxl_dport_get_devname(struct cxl_dport *dport);
2eb93d
 const char *cxl_dport_get_physical_node(struct cxl_dport *dport);
2eb93d
 int cxl_dport_get_id(struct cxl_dport *dport);
2eb93d
+bool cxl_dport_maps_memdev(struct cxl_dport *dport, struct cxl_memdev *memdev);
2eb93d
+struct cxl_dport *cxl_port_get_dport_by_memdev(struct cxl_port *port,
2eb93d
+					       struct cxl_memdev *memdev);
2eb93d
 
2eb93d
 #define cxl_dport_foreach(port, dport)                                         \
2eb93d
 	for (dport = cxl_dport_get_first(port); dport != NULL;                 \
2eb93d
-- 
2eb93d
2.27.0
2eb93d