anitazha / rpms / ndctl

Forked from rpms/ndctl a year ago
Clone
e0018b
From b90fc91e1034668cfde06f0fd8a7293df8b7690d Mon Sep 17 00:00:00 2001
e0018b
From: Dan Williams <dan.j.williams@intel.com>
e0018b
Date: Sun, 23 Jan 2022 16:53:50 -0800
e0018b
Subject: [PATCH 109/217] cxl/list: Filter memdev by ancestry
e0018b
e0018b
Whenever a memdev filter is specified limit output of buses, ports and
e0018b
endpoints to those that are in the memdev's ancestry.
e0018b
e0018b
Link: https://lore.kernel.org/r/164298563039.3021641.5253222797042241091.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-list.txt   | 19 +++++++++
e0018b
 Documentation/cxl/lib/libcxl.txt | 11 +++++
e0018b
 cxl/filter.c                     | 69 ++++++++++++++++++++++++++++++++
e0018b
 cxl/lib/libcxl.c                 | 36 +++++++++++++++++
e0018b
 cxl/lib/libcxl.sym               |  5 +++
e0018b
 cxl/libcxl.h                     |  4 ++
e0018b
 6 files changed, 144 insertions(+)
e0018b
e0018b
diff --git a/Documentation/cxl/cxl-list.txt b/Documentation/cxl/cxl-list.txt
e0018b
index 1751868..bac27c7 100644
e0018b
--- a/Documentation/cxl/cxl-list.txt
e0018b
+++ b/Documentation/cxl/cxl-list.txt
e0018b
@@ -39,6 +39,25 @@ they are combined as an 'AND' filter. So, "-m mem0,mem1,mem2 -p port10"
e0018b
 would only list objects that are beneath port10 AND map mem0, mem1, OR
e0018b
 mem2.
e0018b
 
e0018b
+Given that many topology queries seek to answer questions relative to a
e0018b
+given memdev, buses, ports, and endpoints can be filtered by one or more
e0018b
+memdevs. For example:
e0018b
+----
e0018b
+# cxl list -P -p switch,endpoint -m mem0
e0018b
+[
e0018b
+  {
e0018b
+    "port":"port1",
e0018b
+    "host":"ACPI0016:00",
e0018b
+    "endpoints:port1":[
e0018b
+      {
e0018b
+        "endpoint":"endpoint2",
e0018b
+        "host":"mem0"
e0018b
+      }
e0018b
+    ]
e0018b
+  }
e0018b
+]
e0018b
+----
e0018b
+
e0018b
 The --human option in addition to reformatting some fields to more human
e0018b
 friendly strings also unwraps the array to reduce the number of lines of
e0018b
 output.
e0018b
diff --git a/Documentation/cxl/lib/libcxl.txt b/Documentation/cxl/lib/libcxl.txt
e0018b
index 73b0fb9..b0253d7 100644
e0018b
--- a/Documentation/cxl/lib/libcxl.txt
e0018b
+++ b/Documentation/cxl/lib/libcxl.txt
e0018b
@@ -150,11 +150,18 @@ cxl_bus'.
e0018b
 ----
e0018b
 struct cxl_bus *cxl_bus_get_first(struct cxl_ctx *ctx);
e0018b
 struct cxl_bus *cxl_bus_get_next(struct cxl_bus *bus);
e0018b
+struct cxl_ctx *cxl_bus_get_ctx(struct cxl_bus *bus);
e0018b
+struct cxl_bus *cxl_memdev_get_bus(struct cxl_memdev *memdev);
e0018b
+struct cxl_bus *cxl_endpoint_get_bus(struct cxl_endpoint *endpoint);
e0018b
 
e0018b
 #define cxl_bus_foreach(ctx, bus)                                           \
e0018b
        for (bus = cxl_bus_get_first(ctx); bus != NULL;                      \
e0018b
             bus = cxl_bus_get_next(bus))
e0018b
 ----
e0018b
+When a memdev is active it has established a CXL port hierarchy between
e0018b
+itself and the root of its associated CXL topology. The
e0018b
+cxl_{memdev,endpoint}_get_bus() helpers walk that topology to retrieve
e0018b
+the associated bus object.
e0018b
 
e0018b
 === BUS: Attributes
e0018b
 ----
e0018b
@@ -209,6 +216,7 @@ int cxl_port_is_enabled(struct cxl_port *port);
e0018b
 bool cxl_port_is_root(struct cxl_port *port);
e0018b
 bool cxl_port_is_switch(struct cxl_port *port);
e0018b
 bool cxl_port_is_endpoint(struct cxl_port *port);
e0018b
+bool cxl_port_hosts_memdev(struct cxl_port *port, struct cxl_memdev *memdev);
e0018b
 ----
e0018b
 The port type is communicated via cxl_port_is_<type>(). An 'enabled' port
e0018b
 is one that has succeeded in discovering the CXL component registers in
e0018b
@@ -217,6 +225,9 @@ memdev to be enabled for CXL memory operation all CXL ports in its
e0018b
 ancestry must also be enabled including a root port, an arbitrary number
e0018b
 of intervening switch ports, and a terminal endpoint port.
e0018b
 
e0018b
+cxl_port_hosts_memdev() returns true if the port's host appears in the
e0018b
+memdev host's device topology ancestry.
e0018b
+
e0018b
 ENDPOINTS
e0018b
 ---------
e0018b
 CXL endpoint objects encapsulate the set of host-managed device-memory
e0018b
diff --git a/cxl/filter.c b/cxl/filter.c
e0018b
index 2130816..6dc61a1 100644
e0018b
--- a/cxl/filter.c
e0018b
+++ b/cxl/filter.c
e0018b
@@ -297,6 +297,66 @@ struct cxl_memdev *util_cxl_memdev_filter(struct cxl_memdev *memdev,
e0018b
 	return NULL;
e0018b
 }
e0018b
 
e0018b
+static struct cxl_bus *util_cxl_bus_filter_by_memdev(struct cxl_bus *bus,
e0018b
+						     const char *ident,
e0018b
+						     const char *serial)
e0018b
+{
e0018b
+	struct cxl_ctx *ctx = cxl_bus_get_ctx(bus);
e0018b
+	struct cxl_memdev *memdev;
e0018b
+
e0018b
+	if (!ident && !serial)
e0018b
+		return bus;
e0018b
+
e0018b
+	cxl_memdev_foreach(ctx, memdev) {
e0018b
+		if (!util_cxl_memdev_filter(memdev, ident, serial))
e0018b
+			continue;
e0018b
+		if (cxl_memdev_get_bus(memdev) == bus)
e0018b
+			return bus;
e0018b
+	}
e0018b
+
e0018b
+	return NULL;
e0018b
+}
e0018b
+
e0018b
+static struct cxl_endpoint *
e0018b
+util_cxl_endpoint_filter_by_memdev(struct cxl_endpoint *endpoint,
e0018b
+				   const char *ident, const char *serial)
e0018b
+{
e0018b
+	struct cxl_ctx *ctx = cxl_endpoint_get_ctx(endpoint);
e0018b
+	struct cxl_memdev *memdev;
e0018b
+
e0018b
+	if (!ident && !serial)
e0018b
+		return endpoint;
e0018b
+
e0018b
+	cxl_memdev_foreach(ctx, memdev) {
e0018b
+		if (!util_cxl_memdev_filter(memdev, ident, serial))
e0018b
+			continue;
e0018b
+		if (cxl_memdev_get_endpoint(memdev) == endpoint)
e0018b
+			return endpoint;
e0018b
+	}
e0018b
+
e0018b
+	return NULL;
e0018b
+}
e0018b
+
e0018b
+static struct cxl_port *util_cxl_port_filter_by_memdev(struct cxl_port *port,
e0018b
+						       const char *ident,
e0018b
+						       const char *serial)
e0018b
+{
e0018b
+	struct cxl_ctx *ctx = cxl_port_get_ctx(port);
e0018b
+	struct cxl_memdev *memdev;
e0018b
+
e0018b
+	if (!ident && !serial)
e0018b
+		return port;
e0018b
+
e0018b
+	cxl_memdev_foreach(ctx, memdev) {
e0018b
+		if (!util_cxl_memdev_filter(memdev, ident, serial))
e0018b
+			continue;
e0018b
+		if (cxl_port_hosts_memdev(port, memdev))
e0018b
+			return port;
e0018b
+	}
e0018b
+
e0018b
+	return NULL;
e0018b
+}
e0018b
+
e0018b
 static unsigned long params_to_flags(struct cxl_filter_params *param)
e0018b
 {
e0018b
 	unsigned long flags = 0;
e0018b
@@ -399,6 +459,9 @@ static void walk_endpoints(struct cxl_port *port, struct cxl_filter_params *p,
e0018b
 		if (!util_cxl_endpoint_filter_by_port(endpoint, p->port_filter,
e0018b
 						      pf_mode(p)))
e0018b
 			continue;
e0018b
+		if (!util_cxl_endpoint_filter_by_memdev(
e0018b
+			    endpoint, p->memdev_filter, p->serial_filter))
e0018b
+			continue;
e0018b
 		if (!p->idle && !cxl_endpoint_is_enabled(endpoint))
e0018b
 			continue;
e0018b
 		if (p->endpoints) {
e0018b
@@ -450,6 +513,9 @@ static void walk_child_ports(struct cxl_port *parent_port,
e0018b
 		struct json_object *jchildports = NULL;
e0018b
 		struct json_object *jchildeps = NULL;
e0018b
 
e0018b
+		if (!util_cxl_port_filter_by_memdev(port, p->memdev_filter,
e0018b
+						    p->serial_filter))
e0018b
+			continue;
e0018b
 		if (!util_cxl_port_filter(port, p->port_filter, pf_mode(p)))
e0018b
 			goto walk_children;
e0018b
 		if (!util_cxl_port_filter_by_bus(port, p->bus_filter))
e0018b
@@ -573,6 +639,9 @@ int cxl_filter_walk(struct cxl_ctx *ctx, struct cxl_filter_params *p)
e0018b
 		struct cxl_port *port = cxl_bus_get_port(bus);
e0018b
 		const char *devname = cxl_bus_get_devname(bus);
e0018b
 
e0018b
+		if (!util_cxl_bus_filter_by_memdev(bus, p->memdev_filter,
e0018b
+						   p->serial_filter))
e0018b
+			continue;
e0018b
 		if (!util_cxl_bus_filter(bus, p->bus_filter))
e0018b
 			goto walk_children;
e0018b
 		if (!util_cxl_port_filter(port, p->port_filter, pf_mode(p)))
e0018b
diff --git a/cxl/lib/libcxl.c b/cxl/lib/libcxl.c
e0018b
index 4523ca6..0065f6b 100644
e0018b
--- a/cxl/lib/libcxl.c
e0018b
+++ b/cxl/lib/libcxl.c
e0018b
@@ -455,6 +455,15 @@ CXL_EXPORT const char *cxl_memdev_get_host(struct cxl_memdev *memdev)
e0018b
 	return memdev->host;
e0018b
 }
e0018b
 
e0018b
+CXL_EXPORT struct cxl_bus *cxl_memdev_get_bus(struct cxl_memdev *memdev)
e0018b
+{
e0018b
+	struct cxl_endpoint *endpoint = cxl_memdev_get_endpoint(memdev);
e0018b
+
e0018b
+	if (!endpoint)
e0018b
+		return NULL;
e0018b
+	return cxl_endpoint_get_bus(endpoint);
e0018b
+}
e0018b
+
e0018b
 CXL_EXPORT int cxl_memdev_get_major(struct cxl_memdev *memdev)
e0018b
 {
e0018b
 	return memdev->major;
e0018b
@@ -724,6 +733,13 @@ CXL_EXPORT const char *cxl_endpoint_get_host(struct cxl_endpoint *endpoint)
e0018b
 	return cxl_port_get_host(&endpoint->port);
e0018b
 }
e0018b
 
e0018b
+CXL_EXPORT struct cxl_bus *cxl_endpoint_get_bus(struct cxl_endpoint *endpoint)
e0018b
+{
e0018b
+	struct cxl_port *port = &endpoint->port;
e0018b
+
e0018b
+	return cxl_port_get_bus(port);
e0018b
+}
e0018b
+
e0018b
 CXL_EXPORT int cxl_endpoint_is_enabled(struct cxl_endpoint *endpoint)
e0018b
 {
e0018b
 	return cxl_port_is_enabled(&endpoint->port);
e0018b
@@ -875,6 +891,21 @@ CXL_EXPORT const char *cxl_port_get_host(struct cxl_port *port)
e0018b
 	return devpath_to_devname(port->uport);
e0018b
 }
e0018b
 
e0018b
+CXL_EXPORT bool cxl_port_hosts_memdev(struct cxl_port *port,
e0018b
+				      struct cxl_memdev *memdev)
e0018b
+{
e0018b
+	struct cxl_endpoint *endpoint = cxl_memdev_get_endpoint(memdev);
e0018b
+	struct cxl_port *iter;
e0018b
+
e0018b
+	if (!endpoint)
e0018b
+		return false;
e0018b
+
e0018b
+	iter = cxl_endpoint_get_port(endpoint);
e0018b
+	while (iter && iter != port)
e0018b
+		iter = iter->parent;
e0018b
+	return iter != NULL;
e0018b
+}
e0018b
+
e0018b
 CXL_EXPORT int cxl_port_is_enabled(struct cxl_port *port)
e0018b
 {
e0018b
 	struct cxl_ctx *ctx = cxl_port_get_ctx(port);
e0018b
@@ -985,6 +1016,11 @@ CXL_EXPORT const char *cxl_bus_get_provider(struct cxl_bus *bus)
e0018b
 	return devname;
e0018b
 }
e0018b
 
e0018b
+CXL_EXPORT struct cxl_ctx *cxl_bus_get_ctx(struct cxl_bus *bus)
e0018b
+{
e0018b
+	return cxl_port_get_ctx(&bus->port);
e0018b
+}
e0018b
+
e0018b
 CXL_EXPORT void cxl_cmd_unref(struct cxl_cmd *cmd)
e0018b
 {
e0018b
 	if (!cmd)
e0018b
diff --git a/cxl/lib/libcxl.sym b/cxl/lib/libcxl.sym
e0018b
index 321acac..29f3498 100644
e0018b
--- a/cxl/lib/libcxl.sym
e0018b
+++ b/cxl/lib/libcxl.sym
e0018b
@@ -84,6 +84,7 @@ global:
e0018b
 	cxl_bus_get_devname;
e0018b
 	cxl_bus_get_id;
e0018b
 	cxl_bus_get_port;
e0018b
+	cxl_bus_get_ctx;
e0018b
 	cxl_port_get_first;
e0018b
 	cxl_port_get_next;
e0018b
 	cxl_port_get_devname;
e0018b
@@ -97,6 +98,8 @@ global:
e0018b
 	cxl_port_is_endpoint;
e0018b
 	cxl_port_get_bus;
e0018b
 	cxl_port_get_host;
e0018b
+	cxl_port_get_bus;
e0018b
+	cxl_port_hosts_memdev;
e0018b
 	cxl_endpoint_get_first;
e0018b
 	cxl_endpoint_get_next;
e0018b
 	cxl_endpoint_get_devname;
e0018b
@@ -107,6 +110,8 @@ global:
e0018b
 	cxl_endpoint_get_port;
e0018b
 	cxl_endpoint_get_host;
e0018b
 	cxl_endpoint_get_memdev;
e0018b
+	cxl_endpoint_get_bus;
e0018b
 	cxl_memdev_get_endpoint;
e0018b
 	cxl_memdev_is_enabled;
e0018b
+	cxl_memdev_get_bus;
e0018b
 } LIBCXL_1;
e0018b
diff --git a/cxl/libcxl.h b/cxl/libcxl.h
e0018b
index 790ece8..e7b675e 100644
e0018b
--- a/cxl/libcxl.h
e0018b
+++ b/cxl/libcxl.h
e0018b
@@ -39,6 +39,7 @@ int cxl_memdev_get_id(struct cxl_memdev *memdev);
e0018b
 unsigned long long cxl_memdev_get_serial(struct cxl_memdev *memdev);
e0018b
 const char *cxl_memdev_get_devname(struct cxl_memdev *memdev);
e0018b
 const char *cxl_memdev_get_host(struct cxl_memdev *memdev);
e0018b
+struct cxl_bus *cxl_memdev_get_bus(struct cxl_memdev *memdev);
e0018b
 int cxl_memdev_get_major(struct cxl_memdev *memdev);
e0018b
 int cxl_memdev_get_minor(struct cxl_memdev *memdev);
e0018b
 struct cxl_ctx *cxl_memdev_get_ctx(struct cxl_memdev *memdev);
e0018b
@@ -68,6 +69,7 @@ const char *cxl_bus_get_provider(struct cxl_bus *bus);
e0018b
 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
 
e0018b
 #define cxl_bus_foreach(ctx, bus)                                              \
e0018b
 	for (bus = cxl_bus_get_first(ctx); bus != NULL;                        \
e0018b
@@ -87,6 +89,7 @@ struct cxl_bus *cxl_port_to_bus(struct cxl_port *port);
e0018b
 bool cxl_port_is_endpoint(struct cxl_port *port);
e0018b
 struct cxl_bus *cxl_port_get_bus(struct cxl_port *port);
e0018b
 const char *cxl_port_get_host(struct cxl_port *port);
e0018b
+bool cxl_port_hosts_memdev(struct cxl_port *port, struct cxl_memdev *memdev);
e0018b
 
e0018b
 #define cxl_port_foreach(parent, port)                                         \
e0018b
 	for (port = cxl_port_get_first(parent); port != NULL;                  \
e0018b
@@ -102,6 +105,7 @@ int cxl_endpoint_is_enabled(struct cxl_endpoint *endpoint);
e0018b
 struct cxl_port *cxl_endpoint_get_parent(struct cxl_endpoint *endpoint);
e0018b
 struct cxl_port *cxl_endpoint_get_port(struct cxl_endpoint *endpoint);
e0018b
 const char *cxl_endpoint_get_host(struct cxl_endpoint *endpoint);
e0018b
+struct cxl_bus *cxl_endpoint_get_bus(struct cxl_endpoint *endpoint);
e0018b
 struct cxl_memdev *cxl_endpoint_get_memdev(struct cxl_endpoint *endpoint);
e0018b
 int cxl_memdev_is_enabled(struct cxl_memdev *memdev);
e0018b
 
e0018b
-- 
e0018b
2.27.0
e0018b