anitazha / rpms / ndctl

Forked from rpms/ndctl a year ago
Clone

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

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