anitazha / rpms / ndctl

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