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

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