Blame SOURCES/0123-cxl-list-Filter-dports-and-targets-by-memdevs.patch

26ccd9
From f9ebf984e7eb93a044f48c4089485051751face8 Mon Sep 17 00:00:00 2001
26ccd9
From: Dan Williams <dan.j.williams@intel.com>
26ccd9
Date: Sun, 23 Jan 2022 16:55:06 -0800
26ccd9
Subject: [PATCH 123/217] cxl/list: Filter dports and targets by memdevs
26ccd9
26ccd9
Trim dport / target information by the memdev filter. This is useful when
26ccd9
validating connectivity and decoder programming.
26ccd9
26ccd9
Link: https://lore.kernel.org/r/164298570626.3021641.18034728333613142555.stgit@dwillia2-desk3.amr.corp.intel.com
26ccd9
Reported-by: Ben Widawsky <ben.widawsky@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
 Documentation/cxl/lib/libcxl.txt |  1 +
26ccd9
 cxl/filter.c                     | 51 ++++++++++++++++++++++++++++++++
26ccd9
 cxl/filter.h                     |  6 ++++
26ccd9
 cxl/json.c                       | 51 +++++++++++++++++++++-----------
26ccd9
 cxl/json.h                       |  7 +++++
26ccd9
 cxl/lib/libcxl.c                 |  5 ++++
26ccd9
 cxl/lib/libcxl.sym               |  1 +
26ccd9
 cxl/libcxl.h                     |  1 +
26ccd9
 8 files changed, 105 insertions(+), 18 deletions(-)
26ccd9
26ccd9
diff --git a/Documentation/cxl/lib/libcxl.txt b/Documentation/cxl/lib/libcxl.txt
26ccd9
index 27eb29e..4392b47 100644
26ccd9
--- a/Documentation/cxl/lib/libcxl.txt
26ccd9
+++ b/Documentation/cxl/lib/libcxl.txt
26ccd9
@@ -221,6 +221,7 @@ const char *cxl_port_get_host(struct cxl_port *port);
26ccd9
 struct cxl_port *cxl_decoder_get_port(struct cxl_decoder *decoder);
26ccd9
 struct cxl_port *cxl_port_get_next_all(struct cxl_port *port,
26ccd9
                                        const struct cxl_port *top);
26ccd9
+struct cxl_port *cxl_dport_get_port(struct cxl_dport *dport);
26ccd9
 
26ccd9
 #define cxl_port_foreach(parent, port)                                      \
26ccd9
        for (port = cxl_port_get_first(parent); port != NULL;                \
26ccd9
diff --git a/cxl/filter.c b/cxl/filter.c
26ccd9
index f6a23b7..925bf3a 100644
26ccd9
--- a/cxl/filter.c
26ccd9
+++ b/cxl/filter.c
26ccd9
@@ -435,6 +435,48 @@ util_cxl_decoder_filter_by_memdev(struct cxl_decoder *decoder,
26ccd9
 	return NULL;
26ccd9
 }
26ccd9
 
26ccd9
+struct cxl_target *util_cxl_target_filter_by_memdev(struct cxl_target *target,
26ccd9
+						    const char *ident,
26ccd9
+						    const char *serial)
26ccd9
+{
26ccd9
+	struct cxl_decoder *decoder = cxl_target_get_decoder(target);
26ccd9
+	struct cxl_ctx *ctx = cxl_decoder_get_ctx(decoder);
26ccd9
+	struct cxl_memdev *memdev;
26ccd9
+
26ccd9
+	if (!ident && !serial)
26ccd9
+		return target;
26ccd9
+
26ccd9
+	cxl_memdev_foreach(ctx, memdev) {
26ccd9
+		if (!util_cxl_memdev_filter(memdev, ident, serial))
26ccd9
+			continue;
26ccd9
+		if (cxl_target_maps_memdev(target, memdev))
26ccd9
+			return target;
26ccd9
+	}
26ccd9
+
26ccd9
+	return NULL;
26ccd9
+}
26ccd9
+
26ccd9
+struct cxl_dport *util_cxl_dport_filter_by_memdev(struct cxl_dport *dport,
26ccd9
+						  const char *ident,
26ccd9
+						  const char *serial)
26ccd9
+{
26ccd9
+	struct cxl_port *port = cxl_dport_get_port(dport);
26ccd9
+	struct cxl_ctx *ctx = cxl_port_get_ctx(port);
26ccd9
+	struct cxl_memdev *memdev;
26ccd9
+
26ccd9
+	if (!ident && !serial)
26ccd9
+		return dport;
26ccd9
+
26ccd9
+	cxl_memdev_foreach (ctx, memdev) {
26ccd9
+		if (!util_cxl_memdev_filter(memdev, ident, serial))
26ccd9
+			continue;
26ccd9
+		if (cxl_dport_maps_memdev(dport, memdev))
26ccd9
+			return dport;
26ccd9
+	}
26ccd9
+
26ccd9
+	return NULL;
26ccd9
+}
26ccd9
+
26ccd9
 static bool __memdev_filter_by_decoder(struct cxl_memdev *memdev,
26ccd9
 				       struct cxl_port *port, const char *ident)
26ccd9
 {
26ccd9
@@ -639,6 +681,9 @@ static void walk_decoders(struct cxl_port *port, struct cxl_filter_params *p,
26ccd9
 			dbg(p, "decoder object allocation failure\n");
26ccd9
 			continue;
26ccd9
 		}
26ccd9
+		util_cxl_targets_append_json(jdecoder, decoder,
26ccd9
+					     p->memdev_filter, p->serial_filter,
26ccd9
+					     flags);
26ccd9
 		json_object_array_add(jdecoders, jdecoder);
26ccd9
 	}
26ccd9
 }
26ccd9
@@ -756,6 +801,9 @@ walk_child_ports(struct cxl_port *parent_port, struct cxl_filter_params *p,
26ccd9
 				err(p, "%s: failed to list\n", devname);
26ccd9
 				continue;
26ccd9
 			}
26ccd9
+			util_cxl_dports_append_json(jport, port,
26ccd9
+						    p->memdev_filter,
26ccd9
+						    p->serial_filter, flags);
26ccd9
 			json_object_array_add(jports, jport);
26ccd9
 			jchildports = json_object_new_array();
26ccd9
 			if (!jchildports) {
26ccd9
@@ -914,6 +962,9 @@ int cxl_filter_walk(struct cxl_ctx *ctx, struct cxl_filter_params *p)
26ccd9
 				dbg(p, "bus object allocation failure\n");
26ccd9
 				continue;
26ccd9
 			}
26ccd9
+			util_cxl_dports_append_json(jbus, port,
26ccd9
+						    p->memdev_filter,
26ccd9
+						    p->serial_filter, flags);
26ccd9
 			json_object_array_add(jbuses, jbus);
26ccd9
 			if (p->ports) {
26ccd9
 				jchildports = json_object_new_array();
26ccd9
diff --git a/cxl/filter.h b/cxl/filter.h
26ccd9
index 850df70..5deabb3 100644
26ccd9
--- a/cxl/filter.h
26ccd9
+++ b/cxl/filter.h
26ccd9
@@ -42,6 +42,12 @@ struct cxl_port *util_cxl_port_filter(struct cxl_port *port, const char *ident,
26ccd9
 				      enum cxl_port_filter_mode mode);
26ccd9
 struct cxl_endpoint *util_cxl_endpoint_filter(struct cxl_endpoint *endpoint,
26ccd9
 					      const char *__ident);
26ccd9
+struct cxl_target *util_cxl_target_filter_by_memdev(struct cxl_target *target,
26ccd9
+						    const char *ident,
26ccd9
+						    const char *serial);
26ccd9
+struct cxl_dport *util_cxl_dport_filter_by_memdev(struct cxl_dport *dport,
26ccd9
+						  const char *ident,
26ccd9
+						  const char *serial);
26ccd9
 int cxl_filter_walk(struct cxl_ctx *ctx, struct cxl_filter_params *param);
26ccd9
 bool cxl_filter_has(const char *needle, const char *__filter);
26ccd9
 #endif /* _CXL_UTIL_FILTER_H_ */
26ccd9
diff --git a/cxl/json.c b/cxl/json.c
26ccd9
index 4fb5eec..f3b536e 100644
26ccd9
--- a/cxl/json.c
26ccd9
+++ b/cxl/json.c
26ccd9
@@ -8,6 +8,7 @@
26ccd9
 #include <json-c/printbuf.h>
26ccd9
 #include <ccan/short_types/short_types.h>
26ccd9
 
26ccd9
+#include "filter.h"
26ccd9
 #include "json.h"
26ccd9
 
26ccd9
 static struct json_object *util_cxl_memdev_health_to_json(
26ccd9
@@ -241,9 +242,9 @@ struct json_object *util_cxl_memdev_to_json(struct cxl_memdev *memdev,
26ccd9
 	return jdev;
26ccd9
 }
26ccd9
 
26ccd9
-static struct json_object *util_cxl_dports_to_json(struct json_object *jport,
26ccd9
-						   struct cxl_port *port,
26ccd9
-						   unsigned long flags)
26ccd9
+void util_cxl_dports_append_json(struct json_object *jport,
26ccd9
+				 struct cxl_port *port, const char *ident,
26ccd9
+				 const char *serial, unsigned long flags)
26ccd9
 {
26ccd9
 	struct json_object *jobj, *jdports;
26ccd9
 	struct cxl_dport *dport;
26ccd9
@@ -251,7 +252,7 @@ static struct json_object *util_cxl_dports_to_json(struct json_object *jport,
26ccd9
 
26ccd9
 	val = cxl_port_get_nr_dports(port);
26ccd9
 	if (!val || !(flags & UTIL_JSON_TARGETS))
26ccd9
-		return jport;
26ccd9
+		return;
26ccd9
 
26ccd9
 	jobj = json_object_new_int(val);
26ccd9
 	if (jobj)
26ccd9
@@ -259,12 +260,15 @@ static struct json_object *util_cxl_dports_to_json(struct json_object *jport,
26ccd9
 
26ccd9
 	jdports = json_object_new_array();
26ccd9
 	if (!jdports)
26ccd9
-		return jport;
26ccd9
+		return;
26ccd9
 
26ccd9
 	cxl_dport_foreach(port, dport) {
26ccd9
 		struct json_object *jdport;
26ccd9
 		const char *phys_node;
26ccd9
 
26ccd9
+		if (!util_cxl_dport_filter_by_memdev(dport, ident, serial))
26ccd9
+			continue;
26ccd9
+
26ccd9
 		jdport = json_object_new_object();
26ccd9
 		if (!jdport)
26ccd9
 			continue;
26ccd9
@@ -289,8 +293,6 @@ static struct json_object *util_cxl_dports_to_json(struct json_object *jport,
26ccd9
 	}
26ccd9
 
26ccd9
 	json_object_object_add(jport, "dports", jdports);
26ccd9
-
26ccd9
-	return jport;
26ccd9
 }
26ccd9
 
26ccd9
 struct json_object *util_cxl_bus_to_json(struct cxl_bus *bus,
26ccd9
@@ -311,7 +313,7 @@ struct json_object *util_cxl_bus_to_json(struct cxl_bus *bus,
26ccd9
 	if (jobj)
26ccd9
 		json_object_object_add(jbus, "provider", jobj);
26ccd9
 
26ccd9
-	return util_cxl_dports_to_json(jbus, cxl_bus_get_port(bus), flags);
26ccd9
+	return jbus;
26ccd9
 }
26ccd9
 
26ccd9
 struct json_object *util_cxl_decoder_to_json(struct cxl_decoder *decoder,
26ccd9
@@ -320,8 +322,6 @@ struct json_object *util_cxl_decoder_to_json(struct cxl_decoder *decoder,
26ccd9
 	const char *devname = cxl_decoder_get_devname(decoder);
26ccd9
 	struct cxl_port *port = cxl_decoder_get_port(decoder);
26ccd9
 	struct json_object *jdecoder, *jobj;
26ccd9
-	struct json_object *jtargets;
26ccd9
-	struct cxl_target *target;
26ccd9
 	u64 val;
26ccd9
 
26ccd9
 	jdecoder = json_object_new_object();
26ccd9
@@ -375,9 +375,22 @@ struct json_object *util_cxl_decoder_to_json(struct cxl_decoder *decoder,
26ccd9
 					       jobj);
26ccd9
 	}
26ccd9
 
26ccd9
+	return jdecoder;
26ccd9
+}
26ccd9
+
26ccd9
+void util_cxl_targets_append_json(struct json_object *jdecoder,
26ccd9
+				  struct cxl_decoder *decoder,
26ccd9
+				  const char *ident, const char *serial,
26ccd9
+				  unsigned long flags)
26ccd9
+{
26ccd9
+	struct cxl_port *port = cxl_decoder_get_port(decoder);
26ccd9
+	struct json_object *jobj, *jtargets;
26ccd9
+	struct cxl_target *target;
26ccd9
+	int val;
26ccd9
+
26ccd9
 	/* Endpoints don't have targets, they *are* targets */
26ccd9
 	if (cxl_port_is_endpoint(port))
26ccd9
-		return jdecoder;
26ccd9
+		return;
26ccd9
 
26ccd9
 	val = cxl_decoder_get_nr_targets(decoder);
26ccd9
 	jobj = json_object_new_int(val);
26ccd9
@@ -386,16 +399,21 @@ struct json_object *util_cxl_decoder_to_json(struct cxl_decoder *decoder,
26ccd9
 
26ccd9
 	if (!(flags & UTIL_JSON_TARGETS) ||
26ccd9
 	    !cxl_decoder_get_nr_targets(decoder))
26ccd9
-		return jdecoder;
26ccd9
+		return;
26ccd9
 
26ccd9
 	jtargets = json_object_new_array();
26ccd9
 	if (!jtargets)
26ccd9
-		return jdecoder;
26ccd9
+		return;
26ccd9
 
26ccd9
 	cxl_target_foreach(decoder, target) {
26ccd9
-		struct json_object *jtarget = json_object_new_object();
26ccd9
+		struct json_object *jtarget;
26ccd9
 		const char *phys_node;
26ccd9
+		const char *devname;
26ccd9
+
26ccd9
+		if (!util_cxl_target_filter_by_memdev(target, ident, serial))
26ccd9
+			continue;
26ccd9
 
26ccd9
+		jtarget = json_object_new_object();
26ccd9
 		if (!jtarget)
26ccd9
 			continue;
26ccd9
 
26ccd9
@@ -425,9 +443,6 @@ struct json_object *util_cxl_decoder_to_json(struct cxl_decoder *decoder,
26ccd9
 	}
26ccd9
 
26ccd9
 	json_object_object_add(jdecoder, "targets", jtargets);
26ccd9
-
26ccd9
-	return jdecoder;
26ccd9
-
26ccd9
 }
26ccd9
 
26ccd9
 static struct json_object *__util_cxl_port_to_json(struct cxl_port *port,
26ccd9
@@ -455,7 +470,7 @@ static struct json_object *__util_cxl_port_to_json(struct cxl_port *port,
26ccd9
 			json_object_object_add(jport, "state", jobj);
26ccd9
 	}
26ccd9
 
26ccd9
-	return util_cxl_dports_to_json(jport, port, flags);
26ccd9
+	return jport;
26ccd9
 }
26ccd9
 
26ccd9
 struct json_object *util_cxl_port_to_json(struct cxl_port *port,
26ccd9
diff --git a/cxl/json.h b/cxl/json.h
26ccd9
index fcca2e6..9a5a845 100644
26ccd9
--- a/cxl/json.h
26ccd9
+++ b/cxl/json.h
26ccd9
@@ -15,4 +15,11 @@ struct json_object *util_cxl_endpoint_to_json(struct cxl_endpoint *endpoint,
26ccd9
 					      unsigned long flags);
26ccd9
 struct json_object *util_cxl_decoder_to_json(struct cxl_decoder *decoder,
26ccd9
 					     unsigned long flags);
26ccd9
+void util_cxl_targets_append_json(struct json_object *jdecoder,
26ccd9
+				  struct cxl_decoder *decoder,
26ccd9
+				  const char *ident, const char *serial,
26ccd9
+				  unsigned long flags);
26ccd9
+void util_cxl_dports_append_json(struct json_object *jport,
26ccd9
+				 struct cxl_port *port, const char *ident,
26ccd9
+				 const char *serial, unsigned long flags);
26ccd9
 #endif /* __CXL_UTIL_JSON_H__ */
26ccd9
diff --git a/cxl/lib/libcxl.c b/cxl/lib/libcxl.c
26ccd9
index 1a7dccb..e0b443f 100644
26ccd9
--- a/cxl/lib/libcxl.c
26ccd9
+++ b/cxl/lib/libcxl.c
26ccd9
@@ -1527,6 +1527,11 @@ CXL_EXPORT int cxl_dport_get_id(struct cxl_dport *dport)
26ccd9
 	return dport->id;
26ccd9
 }
26ccd9
 
26ccd9
+CXL_EXPORT struct cxl_port *cxl_dport_get_port(struct cxl_dport *dport)
26ccd9
+{
26ccd9
+	return dport->port;
26ccd9
+}
26ccd9
+
26ccd9
 CXL_EXPORT bool cxl_dport_maps_memdev(struct cxl_dport *dport,
26ccd9
 				      struct cxl_memdev *memdev)
26ccd9
 {
26ccd9
diff --git a/cxl/lib/libcxl.sym b/cxl/lib/libcxl.sym
26ccd9
index 67c7fd5..e56a2bf 100644
26ccd9
--- a/cxl/lib/libcxl.sym
26ccd9
+++ b/cxl/lib/libcxl.sym
26ccd9
@@ -152,6 +152,7 @@ global:
26ccd9
 	cxl_dport_get_devname;
26ccd9
 	cxl_dport_get_physical_node;
26ccd9
 	cxl_dport_get_id;
26ccd9
+	cxl_dport_get_port;
26ccd9
 	cxl_port_get_dport_by_memdev;
26ccd9
 	cxl_dport_maps_memdev;
26ccd9
 } LIBCXL_1;
26ccd9
diff --git a/cxl/libcxl.h b/cxl/libcxl.h
26ccd9
index 1aac396..3b2293b 100644
26ccd9
--- a/cxl/libcxl.h
26ccd9
+++ b/cxl/libcxl.h
26ccd9
@@ -113,6 +113,7 @@ struct cxl_dport *cxl_dport_get_first(struct cxl_port *port);
26ccd9
 struct cxl_dport *cxl_dport_get_next(struct cxl_dport *dport);
26ccd9
 const char *cxl_dport_get_devname(struct cxl_dport *dport);
26ccd9
 const char *cxl_dport_get_physical_node(struct cxl_dport *dport);
26ccd9
+struct cxl_port *cxl_dport_get_port(struct cxl_dport *dport);
26ccd9
 int cxl_dport_get_id(struct cxl_dport *dport);
26ccd9
 bool cxl_dport_maps_memdev(struct cxl_dport *dport, struct cxl_memdev *memdev);
26ccd9
 struct cxl_dport *cxl_port_get_dport_by_memdev(struct cxl_port *port,
26ccd9
-- 
26ccd9
2.27.0
26ccd9