anitazha / rpms / ndctl

Forked from rpms/ndctl a year ago
Clone

Blame SOURCES/0018-libndctl-Unify-adding-dimms-for-papr-and-nfit-famili.patch

e0018b
From daef3a386a9c45105a2c045ddee46600e265939f Mon Sep 17 00:00:00 2001
e0018b
From: Santosh Sivaraj <santosh@fossix.org>
e0018b
Date: Thu, 13 May 2021 11:42:15 +0530
e0018b
Subject: [PATCH 018/217] libndctl: Unify adding dimms for papr and nfit
e0018b
 families
e0018b
e0018b
In preparation for enabling tests on non-nfit devices, unify both, already very
e0018b
similar, functions into one. This will help in adding all attributes needed for
e0018b
the unit tests. Since the function doesn't fail if some of the dimm attributes
e0018b
are missing, this will work fine on PAPR platforms though only part of the DIMM
e0018b
attributes are provided (This doesn't mean that all of the DIMM attributes can
e0018b
be missing).
e0018b
e0018b
Link: https://lore.kernel.org/r/20210513061218.760322-1-santosh@fossix.org
e0018b
Signed-off-by: Santosh Sivaraj <santosh@fossix.org>
e0018b
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
e0018b
---
e0018b
 ndctl/lib/libndctl.c | 103 ++++++++++++++++---------------------------
e0018b
 1 file changed, 38 insertions(+), 65 deletions(-)
e0018b
e0018b
diff --git a/ndctl/lib/libndctl.c b/ndctl/lib/libndctl.c
e0018b
index 2f6d806..e45353f 100644
e0018b
--- a/ndctl/lib/libndctl.c
e0018b
+++ b/ndctl/lib/libndctl.c
e0018b
@@ -1646,41 +1646,9 @@ static int ndctl_bind(struct ndctl_ctx *ctx, struct kmod_module *module,
e0018b
 static int ndctl_unbind(struct ndctl_ctx *ctx, const char *devpath);
e0018b
 static struct kmod_module *to_module(struct ndctl_ctx *ctx, const char *alias);
e0018b
 
e0018b
-static int add_papr_dimm(struct ndctl_dimm *dimm, const char *dimm_base)
e0018b
-{
e0018b
-	int rc = -ENODEV;
e0018b
-	char buf[SYSFS_ATTR_SIZE];
e0018b
-	struct ndctl_ctx *ctx = dimm->bus->ctx;
e0018b
-	char *path = calloc(1, strlen(dimm_base) + 100);
e0018b
-	const char * const devname = ndctl_dimm_get_devname(dimm);
e0018b
-
e0018b
-	dbg(ctx, "%s: Probing of_pmem dimm at %s\n", devname, dimm_base);
e0018b
-
e0018b
-	if (!path)
e0018b
-		return -ENOMEM;
e0018b
-
e0018b
-	/* construct path to the papr compatible dimm flags file */
e0018b
-	sprintf(path, "%s/papr/flags", dimm_base);
e0018b
-
e0018b
-	if (ndctl_bus_is_papr_scm(dimm->bus) &&
e0018b
-	    sysfs_read_attr(ctx, path, buf) == 0) {
e0018b
-
e0018b
-		dbg(ctx, "%s: Adding papr-scm dimm flags:\"%s\"\n", devname, buf);
e0018b
-		dimm->cmd_family = NVDIMM_FAMILY_PAPR;
e0018b
-
e0018b
-		/* Parse dimm flags */
e0018b
-		parse_papr_flags(dimm, buf);
e0018b
-
e0018b
-		/* Allocate monitor mode fd */
e0018b
-		dimm->health_eventfd = open(path, O_RDONLY|O_CLOEXEC);
e0018b
-		rc = 0;
e0018b
-	}
e0018b
-
e0018b
-	free(path);
e0018b
-	return rc;
e0018b
-}
e0018b
-
e0018b
-static int add_nfit_dimm(struct ndctl_dimm *dimm, const char *dimm_base)
e0018b
+static int populate_dimm_attributes(struct ndctl_dimm *dimm,
e0018b
+				    const char *dimm_base,
e0018b
+				    const char *bus_prefix)
e0018b
 {
e0018b
 	int i, rc = -1;
e0018b
 	char buf[SYSFS_ATTR_SIZE];
e0018b
@@ -1694,7 +1662,7 @@ static int add_nfit_dimm(struct ndctl_dimm *dimm, const char *dimm_base)
e0018b
 	 * 'unique_id' may not be available on older kernels, so don't
e0018b
 	 * fail if the read fails.
e0018b
 	 */
e0018b
-	sprintf(path, "%s/nfit/id", dimm_base);
e0018b
+	sprintf(path, "%s/%s/id", dimm_base, bus_prefix);
e0018b
 	if (sysfs_read_attr(ctx, path, buf) == 0) {
e0018b
 		unsigned int b[9];
e0018b
 
e0018b
@@ -1709,68 +1677,74 @@ static int add_nfit_dimm(struct ndctl_dimm *dimm, const char *dimm_base)
e0018b
 		}
e0018b
 	}
e0018b
 
e0018b
-	sprintf(path, "%s/nfit/handle", dimm_base);
e0018b
+	sprintf(path, "%s/%s/handle", dimm_base, bus_prefix);
e0018b
 	if (sysfs_read_attr(ctx, path, buf) < 0)
e0018b
 		goto err_read;
e0018b
 	dimm->handle = strtoul(buf, NULL, 0);
e0018b
 
e0018b
-	sprintf(path, "%s/nfit/phys_id", dimm_base);
e0018b
+	sprintf(path, "%s/%s/phys_id", dimm_base, bus_prefix);
e0018b
 	if (sysfs_read_attr(ctx, path, buf) < 0)
e0018b
 		goto err_read;
e0018b
 	dimm->phys_id = strtoul(buf, NULL, 0);
e0018b
 
e0018b
-	sprintf(path, "%s/nfit/serial", dimm_base);
e0018b
+	sprintf(path, "%s/%s/serial", dimm_base, bus_prefix);
e0018b
 	if (sysfs_read_attr(ctx, path, buf) == 0)
e0018b
 		dimm->serial = strtoul(buf, NULL, 0);
e0018b
 
e0018b
-	sprintf(path, "%s/nfit/vendor", dimm_base);
e0018b
+	sprintf(path, "%s/%s/vendor", dimm_base, bus_prefix);
e0018b
 	if (sysfs_read_attr(ctx, path, buf) == 0)
e0018b
 		dimm->vendor_id = strtoul(buf, NULL, 0);
e0018b
 
e0018b
-	sprintf(path, "%s/nfit/device", dimm_base);
e0018b
+	sprintf(path, "%s/%s/device", dimm_base, bus_prefix);
e0018b
 	if (sysfs_read_attr(ctx, path, buf) == 0)
e0018b
 		dimm->device_id = strtoul(buf, NULL, 0);
e0018b
 
e0018b
-	sprintf(path, "%s/nfit/rev_id", dimm_base);
e0018b
+	sprintf(path, "%s/%s/rev_id", dimm_base, bus_prefix);
e0018b
 	if (sysfs_read_attr(ctx, path, buf) == 0)
e0018b
 		dimm->revision_id = strtoul(buf, NULL, 0);
e0018b
 
e0018b
-	sprintf(path, "%s/nfit/dirty_shutdown", dimm_base);
e0018b
+	sprintf(path, "%s/%s/dirty_shutdown", dimm_base, bus_prefix);
e0018b
 	if (sysfs_read_attr(ctx, path, buf) == 0)
e0018b
 		dimm->dirty_shutdown = strtoll(buf, NULL, 0);
e0018b
 
e0018b
-	sprintf(path, "%s/nfit/subsystem_vendor", dimm_base);
e0018b
+	sprintf(path, "%s/%s/subsystem_vendor", dimm_base, bus_prefix);
e0018b
 	if (sysfs_read_attr(ctx, path, buf) == 0)
e0018b
 		dimm->subsystem_vendor_id = strtoul(buf, NULL, 0);
e0018b
 
e0018b
-	sprintf(path, "%s/nfit/subsystem_device", dimm_base);
e0018b
+	sprintf(path, "%s/%s/subsystem_device", dimm_base, bus_prefix);
e0018b
 	if (sysfs_read_attr(ctx, path, buf) == 0)
e0018b
 		dimm->subsystem_device_id = strtoul(buf, NULL, 0);
e0018b
 
e0018b
-	sprintf(path, "%s/nfit/subsystem_rev_id", dimm_base);
e0018b
+	sprintf(path, "%s/%s/subsystem_rev_id", dimm_base, bus_prefix);
e0018b
 	if (sysfs_read_attr(ctx, path, buf) == 0)
e0018b
 		dimm->subsystem_revision_id = strtoul(buf, NULL, 0);
e0018b
 
e0018b
-	sprintf(path, "%s/nfit/family", dimm_base);
e0018b
+	sprintf(path, "%s/%s/family", dimm_base, bus_prefix);
e0018b
 	if (sysfs_read_attr(ctx, path, buf) == 0)
e0018b
 		dimm->cmd_family = strtoul(buf, NULL, 0);
e0018b
 
e0018b
-	sprintf(path, "%s/nfit/dsm_mask", dimm_base);
e0018b
+	sprintf(path, "%s/%s/dsm_mask", dimm_base, bus_prefix);
e0018b
 	if (sysfs_read_attr(ctx, path, buf) == 0)
e0018b
 		dimm->nfit_dsm_mask = strtoul(buf, NULL, 0);
e0018b
 
e0018b
-	sprintf(path, "%s/nfit/format", dimm_base);
e0018b
+	sprintf(path, "%s/%s/format", dimm_base, bus_prefix);
e0018b
 	if (sysfs_read_attr(ctx, path, buf) == 0)
e0018b
 		dimm->format[0] = strtoul(buf, NULL, 0);
e0018b
 	for (i = 1; i < dimm->formats; i++) {
e0018b
-		sprintf(path, "%s/nfit/format%d", dimm_base, i);
e0018b
+		sprintf(path, "%s/%s/format%d", dimm_base, bus_prefix, i);
e0018b
 		if (sysfs_read_attr(ctx, path, buf) == 0)
e0018b
 			dimm->format[i] = strtoul(buf, NULL, 0);
e0018b
 	}
e0018b
 
e0018b
-	sprintf(path, "%s/nfit/flags", dimm_base);
e0018b
-	if (sysfs_read_attr(ctx, path, buf) == 0)
e0018b
-		parse_nfit_mem_flags(dimm, buf);
e0018b
+	sprintf(path, "%s/%s/flags", dimm_base, bus_prefix);
e0018b
+	if (sysfs_read_attr(ctx, path, buf) == 0) {
e0018b
+		if (ndctl_bus_has_nfit(dimm->bus))
e0018b
+			parse_nfit_mem_flags(dimm, buf);
e0018b
+		else if (ndctl_bus_is_papr_scm(dimm->bus)) {
e0018b
+			dimm->cmd_family = NVDIMM_FAMILY_PAPR;
e0018b
+			parse_papr_flags(dimm, buf);
e0018b
+		}
e0018b
+	}
e0018b
 
e0018b
 	dimm->health_eventfd = open(path, O_RDONLY|O_CLOEXEC);
e0018b
 	rc = 0;
e0018b
@@ -1792,7 +1766,8 @@ static void *add_dimm(void *parent, int id, const char *dimm_base)
e0018b
 	if (!path)
e0018b
 		return NULL;
e0018b
 
e0018b
-	sprintf(path, "%s/nfit/formats", dimm_base);
e0018b
+	sprintf(path, "%s/%s/formats", dimm_base,
e0018b
+		ndctl_bus_has_nfit(bus) ? "nfit" : "papr");
e0018b
 	if (sysfs_read_attr(ctx, path, buf) < 0)
e0018b
 		formats = 1;
e0018b
 	else
e0018b
@@ -1866,13 +1841,12 @@ static void *add_dimm(void *parent, int id, const char *dimm_base)
e0018b
 	else
e0018b
 		dimm->fwa_result = fwa_result_to_result(buf);
e0018b
 
e0018b
+	dimm->formats = formats;
e0018b
 	/* Check if the given dimm supports nfit */
e0018b
 	if (ndctl_bus_has_nfit(bus)) {
e0018b
-		dimm->formats = formats;
e0018b
-		rc = add_nfit_dimm(dimm, dimm_base);
e0018b
-	} else if (ndctl_bus_has_of_node(bus)) {
e0018b
-		rc = add_papr_dimm(dimm, dimm_base);
e0018b
-	}
e0018b
+		rc = populate_dimm_attributes(dimm, dimm_base, "nfit");
e0018b
+	} else if (ndctl_bus_has_of_node(bus))
e0018b
+		rc = populate_dimm_attributes(dimm, dimm_base, "papr");
e0018b
 
e0018b
 	if (rc == -ENODEV) {
e0018b
 		/* Unprobed dimm with no family */
e0018b
@@ -2531,13 +2505,12 @@ static void *add_region(void *parent, int id, const char *region_base)
e0018b
 		goto err_read;
e0018b
 	region->num_mappings = strtoul(buf, NULL, 0);
e0018b
 
e0018b
-	sprintf(path, "%s/nfit/range_index", region_base);
e0018b
-	if (ndctl_bus_has_nfit(bus)) {
e0018b
-		if (sysfs_read_attr(ctx, path, buf) < 0)
e0018b
-			goto err_read;
e0018b
-		region->range_index = strtoul(buf, NULL, 0);
e0018b
-	} else
e0018b
+	sprintf(path, "%s/%s/range_index", region_base,
e0018b
+		ndctl_bus_has_nfit(bus) ? "nfit": "papr");
e0018b
+	if (sysfs_read_attr(ctx, path, buf) < 0)
e0018b
 		region->range_index = -1;
e0018b
+	else
e0018b
+		region->range_index = strtoul(buf, NULL, 0);
e0018b
 
e0018b
 	sprintf(path, "%s/read_only", region_base);
e0018b
 	if (sysfs_read_attr(ctx, path, buf) < 0)
e0018b
-- 
e0018b
2.27.0
e0018b