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

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