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

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