Blame SOURCES/0107-cxl-list-Add-host-entries-for-memdevs.patch

26ccd9
From eec8c953a840a1cbdca63352c64cec3e48e86afe Mon Sep 17 00:00:00 2001
26ccd9
From: Dan Williams <dan.j.williams@intel.com>
26ccd9
Date: Sun, 23 Jan 2022 16:53:39 -0800
26ccd9
Subject: [PATCH 107/217] cxl/list: Add 'host' entries for memdevs
26ccd9
26ccd9
For debugging CXL port connectivity issues it will be useful to have the
26ccd9
PCI device name for the memory expander in the 'memdev' listing.
26ccd9
26ccd9
Link: https://lore.kernel.org/r/164298561980.3021641.9636572507721689266.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/cxl-list.txt   |  3 ++-
26ccd9
 Documentation/cxl/lib/libcxl.txt |  4 ++++
26ccd9
 cxl/json.c                       |  5 +++++
26ccd9
 cxl/lib/libcxl.c                 | 24 ++++++++++++++++++++++++
26ccd9
 cxl/lib/libcxl.sym               |  1 +
26ccd9
 cxl/lib/private.h                |  1 +
26ccd9
 cxl/libcxl.h                     |  1 +
26ccd9
 7 files changed, 38 insertions(+), 1 deletion(-)
26ccd9
26ccd9
diff --git a/Documentation/cxl/cxl-list.txt b/Documentation/cxl/cxl-list.txt
26ccd9
index 30b6161..9c21ab7 100644
26ccd9
--- a/Documentation/cxl/cxl-list.txt
26ccd9
+++ b/Documentation/cxl/cxl-list.txt
26ccd9
@@ -43,7 +43,8 @@ EXAMPLE
26ccd9
     "memdev":"mem0",
26ccd9
     "pmem_size":268435456,
26ccd9
     "ram_size":0,
26ccd9
-    "serial":0
26ccd9
+    "serial":0,
26ccd9
+    "host":"0000:35:00.0"
26ccd9
   }
26ccd9
 ]
26ccd9
 
26ccd9
diff --git a/Documentation/cxl/lib/libcxl.txt b/Documentation/cxl/lib/libcxl.txt
26ccd9
index e4b372d..91fd33e 100644
26ccd9
--- a/Documentation/cxl/lib/libcxl.txt
26ccd9
+++ b/Documentation/cxl/lib/libcxl.txt
26ccd9
@@ -40,6 +40,7 @@ kernel, or to send data or trigger kernel operations for a given device.
26ccd9
 struct cxl_memdev *cxl_memdev_get_first(struct cxl_ctx *ctx);
26ccd9
 struct cxl_memdev *cxl_memdev_get_next(struct cxl_memdev *memdev);
26ccd9
 struct cxl_ctx *cxl_memdev_get_ctx(struct cxl_memdev *memdev);
26ccd9
+const char *cxl_memdev_get_host(struct cxl_memdev *memdev)
26ccd9
 
26ccd9
 #define cxl_memdev_foreach(ctx, memdev) \
26ccd9
         for (memdev = cxl_memdev_get_first(ctx); \
26ccd9
@@ -54,6 +55,9 @@ memory device commands, see the port, decoder, and endpoint APIs to
26ccd9
 determine what if any CXL Memory Resources are reachable given a
26ccd9
 specific memdev.
26ccd9
 
26ccd9
+The host of a memdev is the PCIe Endpoint device that registered its CXL
26ccd9
+capabilities with the Linux CXL core.
26ccd9
+
26ccd9
 === MEMDEV: Attributes
26ccd9
 ----
26ccd9
 int cxl_memdev_get_id(struct cxl_memdev *memdev);
26ccd9
diff --git a/cxl/json.c b/cxl/json.c
26ccd9
index af3b4fe..1868686 100644
26ccd9
--- a/cxl/json.c
26ccd9
+++ b/cxl/json.c
26ccd9
@@ -219,6 +219,11 @@ struct json_object *util_cxl_memdev_to_json(struct cxl_memdev *memdev,
26ccd9
 		if (jobj)
26ccd9
 			json_object_object_add(jdev, "serial", jobj);
26ccd9
 	}
26ccd9
+
26ccd9
+	jobj = json_object_new_string(cxl_memdev_get_host(memdev));
26ccd9
+	if (jobj)
26ccd9
+		json_object_object_add(jdev, "host", jobj);
26ccd9
+
26ccd9
 	return jdev;
26ccd9
 }
26ccd9
 
26ccd9
diff --git a/cxl/lib/libcxl.c b/cxl/lib/libcxl.c
26ccd9
index 5f48202..c4ddc7d 100644
26ccd9
--- a/cxl/lib/libcxl.c
26ccd9
+++ b/cxl/lib/libcxl.c
26ccd9
@@ -63,6 +63,7 @@ static void free_memdev(struct cxl_memdev *memdev, struct list_head *head)
26ccd9
 	free(memdev->firmware_version);
26ccd9
 	free(memdev->dev_buf);
26ccd9
 	free(memdev->dev_path);
26ccd9
+	free(memdev->host);
26ccd9
 	free(memdev);
26ccd9
 }
26ccd9
 
26ccd9
@@ -297,6 +298,7 @@ static void *add_cxl_memdev(void *parent, int id, const char *cxlmem_base)
26ccd9
 	char *path = calloc(1, strlen(cxlmem_base) + 100);
26ccd9
 	struct cxl_ctx *ctx = parent;
26ccd9
 	struct cxl_memdev *memdev, *memdev_dup;
26ccd9
+	char *host, *rpath = NULL;
26ccd9
 	char buf[SYSFS_ATTR_SIZE];
26ccd9
 	struct stat st;
26ccd9
 
26ccd9
@@ -350,6 +352,22 @@ static void *add_cxl_memdev(void *parent, int id, const char *cxlmem_base)
26ccd9
 	if (!memdev->dev_path)
26ccd9
 		goto err_read;
26ccd9
 
26ccd9
+	rpath = realpath(cxlmem_base, NULL);
26ccd9
+	if (!rpath)
26ccd9
+		goto err_read;
26ccd9
+	host = strrchr(rpath, '/');
26ccd9
+	if (host) {
26ccd9
+		host[0] = '\0';
26ccd9
+		host = strrchr(rpath, '/');
26ccd9
+	}
26ccd9
+	if (!host)
26ccd9
+		goto err_read;
26ccd9
+	memdev->host = strdup(host + 1);
26ccd9
+	if (!memdev->host)
26ccd9
+		goto err_read;
26ccd9
+	free(rpath);
26ccd9
+	rpath = NULL;
26ccd9
+
26ccd9
 	sprintf(path, "%s/firmware_version", cxlmem_base);
26ccd9
 	if (sysfs_read_attr(ctx, path, buf) < 0)
26ccd9
 		goto err_read;
26ccd9
@@ -381,6 +399,7 @@ static void *add_cxl_memdev(void *parent, int id, const char *cxlmem_base)
26ccd9
 	free(memdev->dev_buf);
26ccd9
 	free(memdev->dev_path);
26ccd9
 	free(memdev);
26ccd9
+	free(rpath);
26ccd9
  err_dev:
26ccd9
 	free(path);
26ccd9
 	return NULL;
26ccd9
@@ -431,6 +450,11 @@ CXL_EXPORT const char *cxl_memdev_get_devname(struct cxl_memdev *memdev)
26ccd9
 	return devpath_to_devname(memdev->dev_path);
26ccd9
 }
26ccd9
 
26ccd9
+CXL_EXPORT const char *cxl_memdev_get_host(struct cxl_memdev *memdev)
26ccd9
+{
26ccd9
+	return memdev->host;
26ccd9
+}
26ccd9
+
26ccd9
 CXL_EXPORT int cxl_memdev_get_major(struct cxl_memdev *memdev)
26ccd9
 {
26ccd9
 	return memdev->major;
26ccd9
diff --git a/cxl/lib/libcxl.sym b/cxl/lib/libcxl.sym
26ccd9
index dc2863e..8f0688a 100644
26ccd9
--- a/cxl/lib/libcxl.sym
26ccd9
+++ b/cxl/lib/libcxl.sym
26ccd9
@@ -77,6 +77,7 @@ local:
26ccd9
 LIBCXL_2 {
26ccd9
 global:
26ccd9
 	cxl_memdev_get_serial;
26ccd9
+	cxl_memdev_get_host;
26ccd9
 	cxl_bus_get_first;
26ccd9
 	cxl_bus_get_next;
26ccd9
 	cxl_bus_get_provider;
26ccd9
diff --git a/cxl/lib/private.h b/cxl/lib/private.h
26ccd9
index cedd2f2..b097bdf 100644
26ccd9
--- a/cxl/lib/private.h
26ccd9
+++ b/cxl/lib/private.h
26ccd9
@@ -22,6 +22,7 @@ struct cxl_memdev {
26ccd9
 	int id, major, minor;
26ccd9
 	void *dev_buf;
26ccd9
 	size_t buf_len;
26ccd9
+	char *host;
26ccd9
 	char *dev_path;
26ccd9
 	char *firmware_version;
26ccd9
 	struct cxl_ctx *ctx;
26ccd9
diff --git a/cxl/libcxl.h b/cxl/libcxl.h
26ccd9
index a60777e..5487b55 100644
26ccd9
--- a/cxl/libcxl.h
26ccd9
+++ b/cxl/libcxl.h
26ccd9
@@ -38,6 +38,7 @@ struct cxl_memdev *cxl_memdev_get_next(struct cxl_memdev *memdev);
26ccd9
 int cxl_memdev_get_id(struct cxl_memdev *memdev);
26ccd9
 unsigned long long cxl_memdev_get_serial(struct cxl_memdev *memdev);
26ccd9
 const char *cxl_memdev_get_devname(struct cxl_memdev *memdev);
26ccd9
+const char *cxl_memdev_get_host(struct cxl_memdev *memdev);
26ccd9
 int cxl_memdev_get_major(struct cxl_memdev *memdev);
26ccd9
 int cxl_memdev_get_minor(struct cxl_memdev *memdev);
26ccd9
 struct cxl_ctx *cxl_memdev_get_ctx(struct cxl_memdev *memdev);
26ccd9
-- 
26ccd9
2.27.0
26ccd9