Blame SOURCES/0105-cxl-list-Add-endpoints.patch

26ccd9
From 7eb06a5293531854e7a28666e955106094d3552b Mon Sep 17 00:00:00 2001
26ccd9
From: Dan Williams <dan.j.williams@intel.com>
26ccd9
Date: Sun, 23 Jan 2022 16:53:29 -0800
26ccd9
Subject: [PATCH 105/217] cxl/list: Add endpoints
26ccd9
26ccd9
Endpoints are port-like objects that represent the HDM decoders at terminal
26ccd9
end of a decode chain. Unlike port decoders that route to downstream ports,
26ccd9
endpoint decoders route to endpoint DPA (Device Physical Address) ranges.
26ccd9
26ccd9
Link: https://lore.kernel.org/r/164298560917.3021641.13753578554905796298.stgit@dwillia2-desk3.amr.corp.intel.com
26ccd9
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
26ccd9
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
26ccd9
---
26ccd9
 .clang-format                    |   1 +
26ccd9
 Documentation/cxl/cxl-list.txt   |  16 ++++
26ccd9
 Documentation/cxl/lib/libcxl.txt |  31 ++++++-
26ccd9
 cxl/filter.c                     | 147 ++++++++++++++++++++++++++++---
26ccd9
 cxl/filter.h                     |   2 +
26ccd9
 cxl/json.c                       |  20 ++++-
26ccd9
 cxl/json.h                       |   2 +
26ccd9
 cxl/lib/libcxl.c                 | 107 ++++++++++++++++++++++
26ccd9
 cxl/lib/libcxl.sym               |   9 ++
26ccd9
 cxl/lib/private.h                |  10 +++
26ccd9
 cxl/libcxl.h                     |  15 ++++
26ccd9
 cxl/list.c                       |  13 ++-
26ccd9
 12 files changed, 355 insertions(+), 18 deletions(-)
26ccd9
26ccd9
diff --git a/.clang-format b/.clang-format
26ccd9
index 391cd34..106bc5e 100644
26ccd9
--- a/.clang-format
26ccd9
+++ b/.clang-format
26ccd9
@@ -80,6 +80,7 @@ ForEachMacros:
26ccd9
   - 'cxl_memdev_foreach'
26ccd9
   - 'cxl_bus_foreach'
26ccd9
   - 'cxl_port_foreach'
26ccd9
+  - 'cxl_endpoint_foreach'
26ccd9
   - 'daxctl_dev_foreach'
26ccd9
   - 'daxctl_mapping_foreach'
26ccd9
   - 'daxctl_region_foreach'
26ccd9
diff --git a/Documentation/cxl/cxl-list.txt b/Documentation/cxl/cxl-list.txt
26ccd9
index 42b6de6..d342da2 100644
26ccd9
--- a/Documentation/cxl/cxl-list.txt
26ccd9
+++ b/Documentation/cxl/cxl-list.txt
26ccd9
@@ -190,6 +190,12 @@ OPTIONS
26ccd9
 ----
26ccd9
 # cxl list -B
26ccd9
 # cxl list -P -p root
26ccd9
+----
26ccd9
+	Additionally, endpoint objects are also ports so the following commands
26ccd9
+	are also equivalent.
26ccd9
+----
26ccd9
+# cxl list -E
26ccd9
+# cxl list -P -p endpoint
26ccd9
 ----
26ccd9
 	By default, only 'switch' ports are listed.
26ccd9
 
26ccd9
@@ -200,6 +206,16 @@ OPTIONS
26ccd9
 	descendants of the individual ports that match the filter. By default
26ccd9
 	all descendant objects are listed.
26ccd9
 
26ccd9
+-E::
26ccd9
+--endpoints::
26ccd9
+	Include endpoint objects (CXL Memory Device decoders) in the
26ccd9
+	listing.
26ccd9
+
26ccd9
+-e::
26ccd9
+--endpoint::
26ccd9
+	Specify CXL endpoint device name(s), or device id(s) to filter
26ccd9
+	the emitted endpoint(s).
26ccd9
+
26ccd9
 --debug::
26ccd9
 	If the cxl tool was built with debug enabled, turn on debug
26ccd9
 	messages.
26ccd9
diff --git a/Documentation/cxl/lib/libcxl.txt b/Documentation/cxl/lib/libcxl.txt
26ccd9
index 804e9ca..eebab37 100644
26ccd9
--- a/Documentation/cxl/lib/libcxl.txt
26ccd9
+++ b/Documentation/cxl/lib/libcxl.txt
26ccd9
@@ -199,12 +199,41 @@ int cxl_port_get_id(struct cxl_port *port);
26ccd9
 int cxl_port_is_enabled(struct cxl_port *port);
26ccd9
 bool cxl_port_is_root(struct cxl_port *port);
26ccd9
 bool cxl_port_is_switch(struct cxl_port *port);
26ccd9
+bool cxl_port_is_endpoint(struct cxl_port *port);
26ccd9
 ----
26ccd9
 The port type is communicated via cxl_port_is_<type>(). An 'enabled' port
26ccd9
 is one that has succeeded in discovering the CXL component registers in
26ccd9
 the host device and has enumerated its downstream ports. In order for a
26ccd9
 memdev to be enabled for CXL memory operation all CXL ports in its
26ccd9
-ancestry must also be enabled.
26ccd9
+ancestry must also be enabled including a root port, an arbitrary number
26ccd9
+of intervening switch ports, and a terminal endpoint port.
26ccd9
+
26ccd9
+ENDPOINTS
26ccd9
+---------
26ccd9
+CXL endpoint objects encapsulate the set of host-managed device-memory
26ccd9
+(HDM) decoders in a physical memory device. The endpoint is the last hop
26ccd9
+in a decoder chain that translate SPA to DPA (system-physical-address to
26ccd9
+device-local-physical-address).
26ccd9
+
26ccd9
+=== ENDPOINT: Enumeration
26ccd9
+----
26ccd9
+struct cxl_endpoint *cxl_endpoint_get_first(struct cxl_port *parent);
26ccd9
+struct cxl_endpoint *cxl_endpoint_get_next(struct cxl_endpoint *endpoint);
26ccd9
+struct cxl_ctx *cxl_endpoint_get_ctx(struct cxl_endpoint *endpoint);
26ccd9
+struct cxl_port *cxl_endpoint_get_parent(struct cxl_endpoint *endpoint);
26ccd9
+struct cxl_port *cxl_endpoint_get_port(struct cxl_endpoint *endpoint);
26ccd9
+
26ccd9
+#define cxl_endpoint_foreach(port, endpoint)                                 \
26ccd9
+       for (endpoint = cxl_endpoint_get_first(port); endpoint != NULL;       \
26ccd9
+            endpoint = cxl_endpoint_get_next(endpoint))
26ccd9
+----
26ccd9
+
26ccd9
+=== ENDPOINT: Attributes
26ccd9
+----
26ccd9
+const char *cxl_endpoint_get_devname(struct cxl_endpoint *endpoint);
26ccd9
+int cxl_endpoint_get_id(struct cxl_endpoint *endpoint);
26ccd9
+int cxl_endpoint_is_enabled(struct cxl_endpoint *endpoint);
26ccd9
+----
26ccd9
 
26ccd9
 include::../../copyright.txt[]
26ccd9
 
26ccd9
diff --git a/cxl/filter.c b/cxl/filter.c
26ccd9
index 32171a4..5d80d1b 100644
26ccd9
--- a/cxl/filter.c
26ccd9
+++ b/cxl/filter.c
26ccd9
@@ -47,8 +47,42 @@ bool cxl_filter_has(const char *__filter, const char *needle)
26ccd9
 	return false;
26ccd9
 }
26ccd9
 
26ccd9
+static struct cxl_endpoint *
26ccd9
+util_cxl_endpoint_filter(struct cxl_endpoint *endpoint, const char *__ident)
26ccd9
+{
26ccd9
+	char *ident, *save;
26ccd9
+	const char *arg;
26ccd9
+	int endpoint_id;
26ccd9
+
26ccd9
+	if (!__ident)
26ccd9
+		return endpoint;
26ccd9
+
26ccd9
+	ident = strdup(__ident);
26ccd9
+	if (!ident)
26ccd9
+		return NULL;
26ccd9
+
26ccd9
+	for (arg = strtok_r(ident, which_sep(__ident), &save); arg;
26ccd9
+	     arg = strtok_r(NULL, which_sep(__ident), &save)) {
26ccd9
+		if (strcmp(arg, "all") == 0)
26ccd9
+			break;
26ccd9
+
26ccd9
+		if ((sscanf(arg, "%d", &endpoint_id) == 1 ||
26ccd9
+		     sscanf(arg, "endpoint%d", &endpoint_id) == 1) &&
26ccd9
+		    cxl_endpoint_get_id(endpoint) == endpoint_id)
26ccd9
+			break;
26ccd9
+
26ccd9
+		if (strcmp(arg, cxl_endpoint_get_devname(endpoint)) == 0)
26ccd9
+			break;
26ccd9
+	}
26ccd9
+
26ccd9
+	free(ident);
26ccd9
+	if (arg)
26ccd9
+		return endpoint;
26ccd9
+	return NULL;
26ccd9
+}
26ccd9
+
26ccd9
 static struct cxl_port *__util_cxl_port_filter(struct cxl_port *port,
26ccd9
-					     const char *__ident)
26ccd9
+					       const char *__ident)
26ccd9
 {
26ccd9
 	char *ident, *save;
26ccd9
 	const char *arg;
26ccd9
@@ -72,6 +106,9 @@ static struct cxl_port *__util_cxl_port_filter(struct cxl_port *port,
26ccd9
 		if (strcmp(arg, "switch") == 0 && cxl_port_is_switch(port))
26ccd9
 			break;
26ccd9
 
26ccd9
+		if (strcmp(arg, "endpoint") == 0 && cxl_port_is_endpoint(port))
26ccd9
+			break;
26ccd9
+
26ccd9
 		if ((sscanf(arg, "%d", &port_id) == 1 ||
26ccd9
 		     sscanf(arg, "port%d", &port_id) == 1) &&
26ccd9
 		    cxl_port_get_id(port) == port_id)
26ccd9
@@ -116,6 +153,24 @@ static struct cxl_port *util_cxl_port_filter(struct cxl_port *port,
26ccd9
 	return NULL;
26ccd9
 }
26ccd9
 
26ccd9
+static struct cxl_endpoint *
26ccd9
+util_cxl_endpoint_filter_by_port(struct cxl_endpoint *endpoint,
26ccd9
+				 const char *ident,
26ccd9
+				 enum cxl_port_filter_mode mode)
26ccd9
+{
26ccd9
+	struct cxl_port *iter = cxl_endpoint_get_port(endpoint);
26ccd9
+
26ccd9
+	if (util_cxl_port_filter(iter, ident, CXL_PF_SINGLE))
26ccd9
+		return endpoint;
26ccd9
+	iter = cxl_port_get_parent(iter);
26ccd9
+	if (!iter)
26ccd9
+		return NULL;
26ccd9
+	if (util_cxl_port_filter(iter, ident, mode))
26ccd9
+		return endpoint;
26ccd9
+
26ccd9
+	return NULL;
26ccd9
+}
26ccd9
+
26ccd9
 static struct cxl_bus *util_cxl_bus_filter(struct cxl_bus *bus,
26ccd9
 					   const char *__ident)
26ccd9
 {
26ccd9
@@ -325,10 +380,34 @@ static struct json_object *pick_array(struct json_object *child,
26ccd9
 	return NULL;
26ccd9
 }
26ccd9
 
26ccd9
+static void walk_endpoints(struct cxl_port *port, struct cxl_filter_params *p,
26ccd9
+			   struct json_object *jeps, unsigned long flags)
26ccd9
+{
26ccd9
+	struct cxl_endpoint *endpoint;
26ccd9
+
26ccd9
+	cxl_endpoint_foreach(port, endpoint) {
26ccd9
+		struct cxl_port *ep_port = cxl_endpoint_get_port(endpoint);
26ccd9
+		struct json_object *jendpoint;
26ccd9
+
26ccd9
+		if (!util_cxl_endpoint_filter(endpoint, p->endpoint_filter))
26ccd9
+			continue;
26ccd9
+		if (!util_cxl_port_filter_by_bus(ep_port, p->bus_filter))
26ccd9
+			continue;
26ccd9
+		if (!util_cxl_endpoint_filter_by_port(endpoint, p->port_filter,
26ccd9
+						      pf_mode(p)))
26ccd9
+			continue;
26ccd9
+		if (!p->idle && !cxl_endpoint_is_enabled(endpoint))
26ccd9
+			continue;
26ccd9
+		jendpoint = util_cxl_endpoint_to_json(endpoint, flags);
26ccd9
+		if (jendpoint)
26ccd9
+			json_object_array_add(jeps, jendpoint);
26ccd9
+	}
26ccd9
+}
26ccd9
+
26ccd9
 static void walk_child_ports(struct cxl_port *parent_port,
26ccd9
 			     struct cxl_filter_params *p,
26ccd9
 			     struct json_object *jports,
26ccd9
-			     unsigned long flags)
26ccd9
+			     struct json_object *jeps, unsigned long flags)
26ccd9
 {
26ccd9
 	struct cxl_port *port;
26ccd9
 
26ccd9
@@ -336,6 +415,7 @@ static void walk_child_ports(struct cxl_port *parent_port,
26ccd9
 		const char *devname = cxl_port_get_devname(port);
26ccd9
 		struct json_object *jport = NULL;
26ccd9
 		struct json_object *jchildports = NULL;
26ccd9
+		struct json_object *jchildendpoints = NULL;
26ccd9
 
26ccd9
 		if (!util_cxl_port_filter(port, p->port_filter, pf_mode(p)))
26ccd9
 			goto walk_children;
26ccd9
@@ -343,21 +423,41 @@ static void walk_child_ports(struct cxl_port *parent_port,
26ccd9
 			goto walk_children;
26ccd9
 		if (!p->idle && !cxl_port_is_enabled(port))
26ccd9
 			continue;
26ccd9
-		if (p->ports)
26ccd9
+		if (p->ports) {
26ccd9
 			jport = util_cxl_port_to_json(port, flags);
26ccd9
-		if (!jport)
26ccd9
-			continue;
26ccd9
-		json_object_array_add(jports, jport);
26ccd9
-		jchildports = json_object_new_array();
26ccd9
-		if (!jchildports) {
26ccd9
-			err(p, "%s: failed to enumerate child ports\n",
26ccd9
-			    devname);
26ccd9
-			continue;
26ccd9
+			if (!jport) {
26ccd9
+				err(p, "%s: failed to list\n", devname);
26ccd9
+				continue;
26ccd9
+			}
26ccd9
+			json_object_array_add(jports, jport);
26ccd9
+			jchildports = json_object_new_array();
26ccd9
+			if (!jchildports) {
26ccd9
+				err(p, "%s: failed to enumerate child ports\n",
26ccd9
+				    devname);
26ccd9
+				continue;
26ccd9
+			}
26ccd9
+		}
26ccd9
+
26ccd9
+		if (p->ports && p->endpoints) {
26ccd9
+			jchildendpoints = json_object_new_array();
26ccd9
+			if (!jchildendpoints) {
26ccd9
+				err(p,
26ccd9
+				    "%s: failed to enumerate child endpoints\n",
26ccd9
+				    devname);
26ccd9
+				continue;
26ccd9
+			}
26ccd9
 		}
26ccd9
+
26ccd9
 walk_children:
26ccd9
+		if (p->endpoints)
26ccd9
+			walk_endpoints(port, p, pick_array(jchildendpoints, jeps),
26ccd9
+				       flags);
26ccd9
+
26ccd9
 		walk_child_ports(port, p, pick_array(jchildports, jports),
26ccd9
-				 flags);
26ccd9
+				 pick_array(jchildendpoints, jeps), flags);
26ccd9
 		cond_add_put_array_suffix(jport, "ports", devname, jchildports);
26ccd9
+		cond_add_put_array_suffix(jport, "endpoints", devname,
26ccd9
+					  jchildendpoints);
26ccd9
 	}
26ccd9
 }
26ccd9
 
26ccd9
@@ -366,6 +466,7 @@ int cxl_filter_walk(struct cxl_ctx *ctx, struct cxl_filter_params *p)
26ccd9
 	struct json_object *jdevs = NULL, *jbuses = NULL, *jports = NULL;
26ccd9
 	struct json_object *jplatform = json_object_new_array();
26ccd9
 	unsigned long flags = params_to_flags(p);
26ccd9
+	struct json_object *jeps = NULL;
26ccd9
 	struct cxl_memdev *memdev;
26ccd9
 	int top_level_objs = 0;
26ccd9
 	struct cxl_bus *bus;
26ccd9
@@ -387,6 +488,10 @@ int cxl_filter_walk(struct cxl_ctx *ctx, struct cxl_filter_params *p)
26ccd9
 	if (!jports)
26ccd9
 		goto err;
26ccd9
 
26ccd9
+	jeps = json_object_new_array();
26ccd9
+	if (!jeps)
26ccd9
+		goto err;
26ccd9
+
26ccd9
 	dbg(p, "walk memdevs\n");
26ccd9
 	cxl_memdev_foreach(ctx, memdev) {
26ccd9
 		struct json_object *jdev;
26ccd9
@@ -408,6 +513,7 @@ int cxl_filter_walk(struct cxl_ctx *ctx, struct cxl_filter_params *p)
26ccd9
 	cxl_bus_foreach(ctx, bus) {
26ccd9
 		struct json_object *jbus = NULL;
26ccd9
 		struct json_object *jchildports = NULL;
26ccd9
+		struct json_object *jchildeps = NULL;
26ccd9
 		struct cxl_port *port = cxl_bus_get_port(bus);
26ccd9
 		const char *devname = cxl_bus_get_devname(bus);
26ccd9
 
26ccd9
@@ -431,12 +537,23 @@ int cxl_filter_walk(struct cxl_ctx *ctx, struct cxl_filter_params *p)
26ccd9
 					continue;
26ccd9
 				}
26ccd9
 			}
26ccd9
+			if (p->endpoints) {
26ccd9
+				jchildeps = json_object_new_array();
26ccd9
+				if (!jchildeps) {
26ccd9
+					err(p,
26ccd9
+					    "%s: failed to enumerate child endpoints\n",
26ccd9
+					    devname);
26ccd9
+					continue;
26ccd9
+				}
26ccd9
+			}
26ccd9
 		}
26ccd9
 walk_children:
26ccd9
 		dbg(p, "walk ports\n");
26ccd9
 		walk_child_ports(port, p, pick_array(jchildports, jports),
26ccd9
-				 flags);
26ccd9
+				 pick_array(jchildeps, jeps), flags);
26ccd9
 		cond_add_put_array_suffix(jbus, "ports", devname, jchildports);
26ccd9
+		cond_add_put_array_suffix(jbus, "endpoints", devname,
26ccd9
+					  jchildeps);
26ccd9
 	}
26ccd9
 
26ccd9
 	if (json_object_array_length(jdevs))
26ccd9
@@ -445,10 +562,13 @@ walk_children:
26ccd9
 		top_level_objs++;
26ccd9
 	if (json_object_array_length(jports))
26ccd9
 		top_level_objs++;
26ccd9
+	if (json_object_array_length(jeps))
26ccd9
+		top_level_objs++;
26ccd9
 
26ccd9
 	splice_array(p, jdevs, jplatform, "anon memdevs", top_level_objs > 1);
26ccd9
 	splice_array(p, jbuses, jplatform, "buses", top_level_objs > 1);
26ccd9
 	splice_array(p, jports, jplatform, "ports", top_level_objs > 1);
26ccd9
+	splice_array(p, jeps, jplatform, "endpoints", top_level_objs > 1);
26ccd9
 
26ccd9
 	util_display_json_array(stdout, jplatform, flags);
26ccd9
 
26ccd9
@@ -457,6 +577,7 @@ err:
26ccd9
 	json_object_put(jdevs);
26ccd9
 	json_object_put(jbuses);
26ccd9
 	json_object_put(jports);
26ccd9
+	json_object_put(jeps);
26ccd9
 	json_object_put(jplatform);
26ccd9
 	return -ENOMEM;
26ccd9
 }
26ccd9
diff --git a/cxl/filter.h b/cxl/filter.h
26ccd9
index 0d83304..bbd341c 100644
26ccd9
--- a/cxl/filter.h
26ccd9
+++ b/cxl/filter.h
26ccd9
@@ -11,7 +11,9 @@ struct cxl_filter_params {
26ccd9
 	const char *serial_filter;
26ccd9
 	const char *bus_filter;
26ccd9
 	const char *port_filter;
26ccd9
+	const char *endpoint_filter;
26ccd9
 	bool single;
26ccd9
+	bool endpoints;
26ccd9
 	bool memdevs;
26ccd9
 	bool ports;
26ccd9
 	bool buses;
26ccd9
diff --git a/cxl/json.c b/cxl/json.c
26ccd9
index d9f864e..08f6192 100644
26ccd9
--- a/cxl/json.c
26ccd9
+++ b/cxl/json.c
26ccd9
@@ -243,8 +243,9 @@ struct json_object *util_cxl_bus_to_json(struct cxl_bus *bus,
26ccd9
 	return jbus;
26ccd9
 }
26ccd9
 
26ccd9
-struct json_object *util_cxl_port_to_json(struct cxl_port *port,
26ccd9
-					  unsigned long flags)
26ccd9
+static struct json_object *__util_cxl_port_to_json(struct cxl_port *port,
26ccd9
+						   const char *name_key,
26ccd9
+						   unsigned long flags)
26ccd9
 {
26ccd9
 	const char *devname = cxl_port_get_devname(port);
26ccd9
 	struct json_object *jport, *jobj;
26ccd9
@@ -255,7 +256,7 @@ struct json_object *util_cxl_port_to_json(struct cxl_port *port,
26ccd9
 
26ccd9
 	jobj = json_object_new_string(devname);
26ccd9
 	if (jobj)
26ccd9
-		json_object_object_add(jport, "port", jobj);
26ccd9
+		json_object_object_add(jport, name_key, jobj);
26ccd9
 
26ccd9
 	if (!cxl_port_is_enabled(port)) {
26ccd9
 		jobj = json_object_new_string("disabled");
26ccd9
@@ -265,3 +266,16 @@ struct json_object *util_cxl_port_to_json(struct cxl_port *port,
26ccd9
 
26ccd9
 	return jport;
26ccd9
 }
26ccd9
+
26ccd9
+struct json_object *util_cxl_port_to_json(struct cxl_port *port,
26ccd9
+					  unsigned long flags)
26ccd9
+{
26ccd9
+	return __util_cxl_port_to_json(port, "port", flags);
26ccd9
+}
26ccd9
+
26ccd9
+struct json_object *util_cxl_endpoint_to_json(struct cxl_endpoint *endpoint,
26ccd9
+					      unsigned long flags)
26ccd9
+{
26ccd9
+	return __util_cxl_port_to_json(cxl_endpoint_get_port(endpoint),
26ccd9
+				       "endpoint", flags);
26ccd9
+}
26ccd9
diff --git a/cxl/json.h b/cxl/json.h
26ccd9
index 36653db..8f45190 100644
26ccd9
--- a/cxl/json.h
26ccd9
+++ b/cxl/json.h
26ccd9
@@ -11,4 +11,6 @@ struct json_object *util_cxl_bus_to_json(struct cxl_bus *bus,
26ccd9
 struct cxl_port;
26ccd9
 struct json_object *util_cxl_port_to_json(struct cxl_port *port,
26ccd9
 					  unsigned long flags);
26ccd9
+struct json_object *util_cxl_endpoint_to_json(struct cxl_endpoint *endpoint,
26ccd9
+					      unsigned long flags);
26ccd9
 #endif /* __CXL_UTIL_JSON_H__ */
26ccd9
diff --git a/cxl/lib/libcxl.c b/cxl/lib/libcxl.c
26ccd9
index 03eff3c..a25e715 100644
26ccd9
--- a/cxl/lib/libcxl.c
26ccd9
+++ b/cxl/lib/libcxl.c
26ccd9
@@ -67,14 +67,18 @@ static void free_memdev(struct cxl_memdev *memdev, struct list_head *head)
26ccd9
 }
26ccd9
 
26ccd9
 static void free_port(struct cxl_port *port, struct list_head *head);
26ccd9
+static void free_endpoint(struct cxl_endpoint *endpoint, struct list_head *head);
26ccd9
 static void __free_port(struct cxl_port *port, struct list_head *head)
26ccd9
 {
26ccd9
 	struct cxl_port *child, *_c;
26ccd9
+	struct cxl_endpoint *endpoint, *_e;
26ccd9
 
26ccd9
 	if (head)
26ccd9
 		list_del_from(head, &port->list);
26ccd9
 	list_for_each_safe(&port->child_ports, child, _c, list)
26ccd9
 		free_port(child, &port->child_ports);
26ccd9
+	list_for_each_safe(&port->endpoints, endpoint, _e, port.list)
26ccd9
+		free_endpoint(endpoint, &port->endpoints);
26ccd9
 	kmod_module_unref(port->module);
26ccd9
 	free(port->dev_buf);
26ccd9
 	free(port->dev_path);
26ccd9
@@ -87,6 +91,12 @@ static void free_port(struct cxl_port *port, struct list_head *head)
26ccd9
 	free(port);
26ccd9
 }
26ccd9
 
26ccd9
+static void free_endpoint(struct cxl_endpoint *endpoint, struct list_head *head)
26ccd9
+{
26ccd9
+	__free_port(&endpoint->port, head);
26ccd9
+	free(endpoint);
26ccd9
+}
26ccd9
+
26ccd9
 static void free_bus(struct cxl_bus *bus, struct list_head *head)
26ccd9
 {
26ccd9
 	__free_port(&bus->port, head);
26ccd9
@@ -500,6 +510,7 @@ static int cxl_port_init(struct cxl_port *port, struct cxl_port *parent_port,
26ccd9
 	port->parent = parent_port;
26ccd9
 
26ccd9
 	list_head_init(&port->child_ports);
26ccd9
+	list_head_init(&port->endpoints);
26ccd9
 
26ccd9
 	port->dev_path = strdup(cxlport_base);
26ccd9
 	if (!port->dev_path)
26ccd9
@@ -529,6 +540,97 @@ err:
26ccd9
 	return -ENOMEM;
26ccd9
 }
26ccd9
 
26ccd9
+static void *add_cxl_endpoint(void *parent, int id, const char *cxlep_base)
26ccd9
+{
26ccd9
+	const char *devname = devpath_to_devname(cxlep_base);
26ccd9
+	struct cxl_endpoint *endpoint, *endpoint_dup;
26ccd9
+	struct cxl_port *port = parent;
26ccd9
+	struct cxl_ctx *ctx = cxl_port_get_ctx(port);
26ccd9
+	int rc;
26ccd9
+
26ccd9
+	dbg(ctx, "%s: base: \'%s\'\n", devname, cxlep_base);
26ccd9
+
26ccd9
+	endpoint = calloc(1, sizeof(*endpoint));
26ccd9
+	if (!endpoint)
26ccd9
+		return NULL;
26ccd9
+
26ccd9
+	rc = cxl_port_init(&endpoint->port, port, CXL_PORT_ENDPOINT, ctx, id,
26ccd9
+			   cxlep_base);
26ccd9
+	if (rc)
26ccd9
+		goto err;
26ccd9
+
26ccd9
+	cxl_endpoint_foreach(port, endpoint_dup)
26ccd9
+		if (endpoint_dup->port.id == endpoint->port.id) {
26ccd9
+			free_endpoint(endpoint, NULL);
26ccd9
+			return endpoint_dup;
26ccd9
+		}
26ccd9
+
26ccd9
+	list_add(&port->endpoints, &endpoint->port.list);
26ccd9
+	return endpoint;
26ccd9
+
26ccd9
+err:
26ccd9
+	free(endpoint);
26ccd9
+	return NULL;
26ccd9
+
26ccd9
+}
26ccd9
+
26ccd9
+static void cxl_endpoints_init(struct cxl_port *port)
26ccd9
+{
26ccd9
+	struct cxl_ctx *ctx = cxl_port_get_ctx(port);
26ccd9
+
26ccd9
+	if (port->endpoints_init)
26ccd9
+		return;
26ccd9
+
26ccd9
+	port->endpoints_init = 1;
26ccd9
+
26ccd9
+	sysfs_device_parse(ctx, port->dev_path, "endpoint", port,
26ccd9
+			   add_cxl_endpoint);
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT struct cxl_ctx *cxl_endpoint_get_ctx(struct cxl_endpoint *endpoint)
26ccd9
+{
26ccd9
+	return endpoint->port.ctx;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT struct cxl_endpoint *cxl_endpoint_get_first(struct cxl_port *port)
26ccd9
+{
26ccd9
+	cxl_endpoints_init(port);
26ccd9
+
26ccd9
+	return list_top(&port->endpoints, struct cxl_endpoint, port.list);
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT struct cxl_endpoint *cxl_endpoint_get_next(struct cxl_endpoint *endpoint)
26ccd9
+{
26ccd9
+	struct cxl_port *port = endpoint->port.parent;
26ccd9
+
26ccd9
+	return list_next(&port->endpoints, endpoint, port.list);
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT const char *cxl_endpoint_get_devname(struct cxl_endpoint *endpoint)
26ccd9
+{
26ccd9
+	return devpath_to_devname(endpoint->port.dev_path);
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT int cxl_endpoint_get_id(struct cxl_endpoint *endpoint)
26ccd9
+{
26ccd9
+	return endpoint->port.id;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT struct cxl_port *cxl_endpoint_get_parent(struct cxl_endpoint *endpoint)
26ccd9
+{
26ccd9
+	return endpoint->port.parent;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT struct cxl_port *cxl_endpoint_get_port(struct cxl_endpoint *endpoint)
26ccd9
+{
26ccd9
+	return &endpoint->port;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT int cxl_endpoint_is_enabled(struct cxl_endpoint *endpoint)
26ccd9
+{
26ccd9
+	return cxl_port_is_enabled(&endpoint->port);
26ccd9
+}
26ccd9
+
26ccd9
 static void *add_cxl_port(void *parent, int id, const char *cxlport_base)
26ccd9
 {
26ccd9
 	const char *devname = devpath_to_devname(cxlport_base);
26ccd9
@@ -619,6 +721,11 @@ CXL_EXPORT bool cxl_port_is_switch(struct cxl_port *port)
26ccd9
 	return port->type == CXL_PORT_SWITCH;
26ccd9
 }
26ccd9
 
26ccd9
+CXL_EXPORT bool cxl_port_is_endpoint(struct cxl_port *port)
26ccd9
+{
26ccd9
+	return port->type == CXL_PORT_ENDPOINT;
26ccd9
+}
26ccd9
+
26ccd9
 CXL_EXPORT struct cxl_bus *cxl_port_get_bus(struct cxl_port *port)
26ccd9
 {
26ccd9
 	struct cxl_bus *bus;
26ccd9
diff --git a/cxl/lib/libcxl.sym b/cxl/lib/libcxl.sym
26ccd9
index a7e923f..7a51a0c 100644
26ccd9
--- a/cxl/lib/libcxl.sym
26ccd9
+++ b/cxl/lib/libcxl.sym
26ccd9
@@ -93,5 +93,14 @@ global:
26ccd9
 	cxl_port_is_root;
26ccd9
 	cxl_port_is_switch;
26ccd9
 	cxl_port_to_bus;
26ccd9
+	cxl_port_is_endpoint;
26ccd9
 	cxl_port_get_bus;
26ccd9
+	cxl_endpoint_get_first;
26ccd9
+	cxl_endpoint_get_next;
26ccd9
+	cxl_endpoint_get_devname;
26ccd9
+	cxl_endpoint_get_id;
26ccd9
+	cxl_endpoint_get_ctx;
26ccd9
+	cxl_endpoint_is_enabled;
26ccd9
+	cxl_endpoint_get_parent;
26ccd9
+	cxl_endpoint_get_port;
26ccd9
 } LIBCXL_1;
26ccd9
diff --git a/cxl/lib/private.h b/cxl/lib/private.h
26ccd9
index 637f90d..cedd2f2 100644
26ccd9
--- a/cxl/lib/private.h
26ccd9
+++ b/cxl/lib/private.h
26ccd9
@@ -17,6 +17,7 @@ struct cxl_pmem {
26ccd9
 	char *dev_path;
26ccd9
 };
26ccd9
 
26ccd9
+struct cxl_endpoint;
26ccd9
 struct cxl_memdev {
26ccd9
 	int id, major, minor;
26ccd9
 	void *dev_buf;
26ccd9
@@ -32,11 +33,13 @@ struct cxl_memdev {
26ccd9
 	struct kmod_module *module;
26ccd9
 	struct cxl_pmem *pmem;
26ccd9
 	unsigned long long serial;
26ccd9
+	struct cxl_endpoint *endpoint;
26ccd9
 };
26ccd9
 
26ccd9
 enum cxl_port_type {
26ccd9
 	CXL_PORT_ROOT,
26ccd9
 	CXL_PORT_SWITCH,
26ccd9
+	CXL_PORT_ENDPOINT,
26ccd9
 };
26ccd9
 
26ccd9
 struct cxl_port {
26ccd9
@@ -46,6 +49,7 @@ struct cxl_port {
26ccd9
 	char *dev_path;
26ccd9
 	char *uport;
26ccd9
 	int ports_init;
26ccd9
+	int endpoints_init;
26ccd9
 	struct cxl_ctx *ctx;
26ccd9
 	struct cxl_bus *bus;
26ccd9
 	enum cxl_port_type type;
26ccd9
@@ -53,12 +57,18 @@ struct cxl_port {
26ccd9
 	struct kmod_module *module;
26ccd9
 	struct list_node list;
26ccd9
 	struct list_head child_ports;
26ccd9
+	struct list_head endpoints;
26ccd9
 };
26ccd9
 
26ccd9
 struct cxl_bus {
26ccd9
 	struct cxl_port port;
26ccd9
 };
26ccd9
 
26ccd9
+struct cxl_endpoint {
26ccd9
+	struct cxl_port port;
26ccd9
+	struct cxl_memdev *memdev;
26ccd9
+};
26ccd9
+
26ccd9
 enum cxl_cmd_query_status {
26ccd9
 	CXL_CMD_QUERY_NOT_RUN = 0,
26ccd9
 	CXL_CMD_QUERY_OK,
26ccd9
diff --git a/cxl/libcxl.h b/cxl/libcxl.h
26ccd9
index efbb397..f6ba9a1 100644
26ccd9
--- a/cxl/libcxl.h
26ccd9
+++ b/cxl/libcxl.h
26ccd9
@@ -81,12 +81,27 @@ struct cxl_port *cxl_port_get_parent(struct cxl_port *port);
26ccd9
 bool cxl_port_is_root(struct cxl_port *port);
26ccd9
 bool cxl_port_is_switch(struct cxl_port *port);
26ccd9
 struct cxl_bus *cxl_port_to_bus(struct cxl_port *port);
26ccd9
+bool cxl_port_is_endpoint(struct cxl_port *port);
26ccd9
 struct cxl_bus *cxl_port_get_bus(struct cxl_port *port);
26ccd9
 
26ccd9
 #define cxl_port_foreach(parent, port)                                         \
26ccd9
 	for (port = cxl_port_get_first(parent); port != NULL;                  \
26ccd9
 	     port = cxl_port_get_next(port))
26ccd9
 
26ccd9
+struct cxl_endpoint;
26ccd9
+struct cxl_endpoint *cxl_endpoint_get_first(struct cxl_port *parent);
26ccd9
+struct cxl_endpoint *cxl_endpoint_get_next(struct cxl_endpoint *endpoint);
26ccd9
+const char *cxl_endpoint_get_devname(struct cxl_endpoint *endpoint);
26ccd9
+int cxl_endpoint_get_id(struct cxl_endpoint *endpoint);
26ccd9
+struct cxl_ctx *cxl_endpoint_get_ctx(struct cxl_endpoint *endpoint);
26ccd9
+int cxl_endpoint_is_enabled(struct cxl_endpoint *endpoint);
26ccd9
+struct cxl_port *cxl_endpoint_get_parent(struct cxl_endpoint *endpoint);
26ccd9
+struct cxl_port *cxl_endpoint_get_port(struct cxl_endpoint *endpoint);
26ccd9
+
26ccd9
+#define cxl_endpoint_foreach(port, endpoint)                                   \
26ccd9
+	for (endpoint = cxl_endpoint_get_first(port); endpoint != NULL;        \
26ccd9
+	     endpoint = cxl_endpoint_get_next(endpoint))
26ccd9
+
26ccd9
 struct cxl_cmd;
26ccd9
 const char *cxl_cmd_get_devname(struct cxl_cmd *cmd);
26ccd9
 struct cxl_cmd *cxl_cmd_new_raw(struct cxl_memdev *memdev, int opcode);
26ccd9
diff --git a/cxl/list.c b/cxl/list.c
26ccd9
index 01ab19b..b15e01c 100644
26ccd9
--- a/cxl/list.c
26ccd9
+++ b/cxl/list.c
26ccd9
@@ -31,6 +31,11 @@ static const struct option options[] = {
26ccd9
 	OPT_BOOLEAN('P', "ports", &param.ports, "include CXL port info"),
26ccd9
 	OPT_BOOLEAN('S', "single", &param.single,
26ccd9
 		    "skip listing descendant objects"),
26ccd9
+	OPT_STRING('e', "endpoint", &param.endpoint_filter,
26ccd9
+		   "endpoint device name",
26ccd9
+		   "filter by CXL endpoint device name(s)"),
26ccd9
+	OPT_BOOLEAN('E', "endpoints", &param.endpoints,
26ccd9
+		    "include CXL endpoint info"),
26ccd9
 	OPT_BOOLEAN('i', "idle", &param.idle, "include disabled devices"),
26ccd9
 	OPT_BOOLEAN('u', "human", &param.human,
26ccd9
 		    "use human friendly number formats "),
26ccd9
@@ -44,7 +49,8 @@ static const struct option options[] = {
26ccd9
 
26ccd9
 static int num_list_flags(void)
26ccd9
 {
26ccd9
-       return !!param.memdevs + !!param.buses + !!param.ports;
26ccd9
+	return !!param.memdevs + !!param.buses + !!param.ports +
26ccd9
+	       !!param.endpoints;
26ccd9
 }
26ccd9
 
26ccd9
 int cmd_list(int argc, const char **argv, struct cxl_ctx *ctx)
26ccd9
@@ -74,6 +80,8 @@ int cmd_list(int argc, const char **argv, struct cxl_ctx *ctx)
26ccd9
 			param.buses = true;
26ccd9
 		if (param.port_filter)
26ccd9
 			param.ports = true;
26ccd9
+		if (param.endpoint_filter)
26ccd9
+			param.endpoints = true;
26ccd9
 		if (num_list_flags() == 0) {
26ccd9
 			/*
26ccd9
 			 * TODO: We likely want to list regions by default if
26ccd9
@@ -96,6 +104,9 @@ int cmd_list(int argc, const char **argv, struct cxl_ctx *ctx)
26ccd9
 	if (cxl_filter_has(param.port_filter, "root") && param.ports)
26ccd9
 		param.buses = true;
26ccd9
 
26ccd9
+	if (cxl_filter_has(param.port_filter, "endpoint") && param.ports)
26ccd9
+		param.endpoints = true;
26ccd9
+
26ccd9
 	dbg(&param, "walk topology\n");
26ccd9
 	return cxl_filter_walk(ctx, ¶m;;
26ccd9
 }
26ccd9
-- 
26ccd9
2.27.0
26ccd9