anitazha / rpms / ndctl

Forked from rpms/ndctl a year ago
Clone

Blame SOURCES/0118-cxl-list-Use-physical_node-for-root-port-attachment-.patch

e0018b
From 5fb1b8a1630115f3aa3cd6bb7bc9ba5122867f66 Mon Sep 17 00:00:00 2001
e0018b
From: Dan Williams <dan.j.williams@intel.com>
e0018b
Date: Sun, 23 Jan 2022 16:54:39 -0800
e0018b
Subject: [PATCH 118/217] cxl/list: Use 'physical_node' for root port
e0018b
 attachment detection
e0018b
e0018b
Platform firmware objects like ACPI0016 link from /sys/bus/acpi to
e0018b
/sys/bus/pci via a 'physical_node' attribute. Consider that link when
e0018b
attempting to link memdevs to root ports.
e0018b
e0018b
Emit it in the the target listing as the 'alias' for the listed target
e0018b
device.
e0018b
e0018b
Link: https://lore.kernel.org/r/164298567943.3021641.12696733268157328279.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/lib/libcxl.txt |  6 ++++++
e0018b
 cxl/json.c                       |  8 ++++++++
e0018b
 cxl/lib/libcxl.c                 | 16 +++++++++++++++-
e0018b
 cxl/lib/libcxl.sym               |  1 +
e0018b
 cxl/lib/private.h                |  1 +
e0018b
 cxl/libcxl.h                     |  1 +
e0018b
 6 files changed, 32 insertions(+), 1 deletion(-)
e0018b
e0018b
diff --git a/Documentation/cxl/lib/libcxl.txt b/Documentation/cxl/lib/libcxl.txt
e0018b
index bd92fef..a68a58b 100644
e0018b
--- a/Documentation/cxl/lib/libcxl.txt
e0018b
+++ b/Documentation/cxl/lib/libcxl.txt
e0018b
@@ -389,6 +389,7 @@ unsigned long cxl_target_get_id(struct cxl_target *target);
e0018b
 const char *cxl_target_get_devname(struct cxl_target *target);
e0018b
 bool cxl_target_maps_memdev(struct cxl_target *target,
e0018b
                            struct cxl_memdev *memdev);
e0018b
+const char *cxl_target_get_physical_node(struct cxl_target *target);
e0018b
 ----
e0018b
 The position of a decoder along with the interleave granularity dictate
e0018b
 which address in the decoder's resource range map to which port.
e0018b
@@ -410,6 +411,11 @@ The cxl_target_maps_memdev() helper is the companion of
e0018b
 cxl_decoder_get_target_by_memdev() to determine which downstream ports /
e0018b
 targets are capable of mapping which memdevs.
e0018b
 
e0018b
+Some platform firmware implementations define an alias / companion
e0018b
+device to represent the root of a PCI device hierarchy. The
e0018b
+cxl_target_get_physical_node() helper returns the device name of that
e0018b
+companion object in the PCI hierarchy.
e0018b
+
e0018b
 include::../../copyright.txt[]
e0018b
 
e0018b
 SEE ALSO
e0018b
diff --git a/cxl/json.c b/cxl/json.c
e0018b
index 3a37909..d81aed8 100644
e0018b
--- a/cxl/json.c
e0018b
+++ b/cxl/json.c
e0018b
@@ -342,6 +342,7 @@ struct json_object *util_cxl_decoder_to_json(struct cxl_decoder *decoder,
e0018b
 
e0018b
 	cxl_target_foreach(decoder, target) {
e0018b
 		struct json_object *jtarget = json_object_new_object();
e0018b
+		const char *phys_node;
e0018b
 
e0018b
 		if (!jtarget)
e0018b
 			continue;
e0018b
@@ -351,6 +352,13 @@ struct json_object *util_cxl_decoder_to_json(struct cxl_decoder *decoder,
e0018b
 		if (jobj)
e0018b
 			json_object_object_add(jtarget, "target", jobj);
e0018b
 
e0018b
+		phys_node = cxl_target_get_physical_node(target);
e0018b
+		if (phys_node) {
e0018b
+			jobj = json_object_new_string(phys_node);
e0018b
+			if (jobj)
e0018b
+				json_object_object_add(jtarget, "alias", jobj);
e0018b
+		}
e0018b
+
e0018b
 		val = cxl_target_get_position(target);
e0018b
 		jobj = json_object_new_int(val);
e0018b
 		if (jobj)
e0018b
diff --git a/cxl/lib/libcxl.c b/cxl/lib/libcxl.c
e0018b
index 877f42c..7bf7949 100644
e0018b
--- a/cxl/lib/libcxl.c
e0018b
+++ b/cxl/lib/libcxl.c
e0018b
@@ -72,6 +72,7 @@ static void free_target(struct cxl_target *target, struct list_head *head)
e0018b
 	if (head)
e0018b
 		list_del_from(head, &target->list);
e0018b
 	free(target->dev_path);
e0018b
+	free(target->phys_path);
e0018b
 	free(target);
e0018b
 }
e0018b
 
e0018b
@@ -970,7 +971,11 @@ static void *add_cxl_decoder(void *parent, int id, const char *cxldecoder_base)
e0018b
 			free(target);
e0018b
 			break;
e0018b
 		}
e0018b
-		dbg(ctx, "%s: target%ld %s\n", devname, i, target->dev_path);
e0018b
+		sprintf(port->dev_buf, "%s/dport%d/physical_node", port->dev_path, did);
e0018b
+		target->phys_path = realpath(port->dev_buf, NULL);
e0018b
+		dbg(ctx, "%s: target%ld %s phys_path: %s\n", devname, i,
e0018b
+		    target->dev_path,
e0018b
+		    target->phys_path ? target->phys_path : "none");
e0018b
 		list_add(&decoder->targets, &target->list);
e0018b
 	}
e0018b
 
e0018b
@@ -1138,9 +1143,18 @@ CXL_EXPORT bool cxl_target_maps_memdev(struct cxl_target *target,
e0018b
 	dbg(ctx, "memdev: %s target: %s\n", memdev->host_path,
e0018b
 	    target->dev_path);
e0018b
 
e0018b
+	if (target->phys_path)
e0018b
+		return !!strstr(memdev->host_path, target->phys_path);
e0018b
 	return !!strstr(memdev->host_path, target->dev_path);
e0018b
 }
e0018b
 
e0018b
+CXL_EXPORT const char *cxl_target_get_physical_node(struct cxl_target *target)
e0018b
+{
e0018b
+	if (!target->phys_path)
e0018b
+		return NULL;
e0018b
+	return devpath_to_devname(target->phys_path);
e0018b
+}
e0018b
+
e0018b
 CXL_EXPORT struct cxl_target *
e0018b
 cxl_decoder_get_target_by_memdev(struct cxl_decoder *decoder,
e0018b
 				 struct cxl_memdev *memdev)
e0018b
diff --git a/cxl/lib/libcxl.sym b/cxl/lib/libcxl.sym
e0018b
index cb33180..ce01298 100644
e0018b
--- a/cxl/lib/libcxl.sym
e0018b
+++ b/cxl/lib/libcxl.sym
e0018b
@@ -141,4 +141,5 @@ global:
e0018b
 	cxl_target_get_id;
e0018b
 	cxl_target_get_devname;
e0018b
 	cxl_target_maps_memdev;
e0018b
+	cxl_target_get_physical_node;
e0018b
 } LIBCXL_1;
e0018b
diff --git a/cxl/lib/private.h b/cxl/lib/private.h
e0018b
index 1743a24..7e7742d 100644
e0018b
--- a/cxl/lib/private.h
e0018b
+++ b/cxl/lib/private.h
e0018b
@@ -77,6 +77,7 @@ struct cxl_target {
e0018b
 	struct list_node list;
e0018b
 	struct cxl_decoder *decoder;
e0018b
 	char *dev_path;
e0018b
+	char *phys_path;
e0018b
 	int id, position;
e0018b
 };
e0018b
 
e0018b
diff --git a/cxl/libcxl.h b/cxl/libcxl.h
e0018b
index abda0e5..0e484cc 100644
e0018b
--- a/cxl/libcxl.h
e0018b
+++ b/cxl/libcxl.h
e0018b
@@ -140,6 +140,7 @@ unsigned long cxl_target_get_id(struct cxl_target *target);
e0018b
 const char *cxl_target_get_devname(struct cxl_target *target);
e0018b
 bool cxl_target_maps_memdev(struct cxl_target *target,
e0018b
 			    struct cxl_memdev *memdev);
e0018b
+const char *cxl_target_get_physical_node(struct cxl_target *target);
e0018b
 
e0018b
 #define cxl_target_foreach(decoder, target)                                    \
e0018b
 	for (target = cxl_target_get_first(decoder); target != NULL;           \
e0018b
-- 
e0018b
2.27.0
e0018b