Blame SOURCES/daef3a3-libndctl-Unify-adding-dimms-for-papr-and-nfit-families.patch

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