anitazha / rpms / ndctl

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