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

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