Blame SOURCES/e086106-libndctl-papr-Fix-probe-for-papr-scm-compatible-nvdimms.patch

0dc130
libndctl/papr: Fix probe for papr-scm compatible nvdimms
0dc130
0dc130
BZ: 
0dc130
Brew: 
0dc130
0dc130
commit e086106b4d81a2079141c848db7695451c04e877
0dc130
Author: Vaibhav Jain <vaibhav@linux.ibm.com>
0dc130
Date:   Mon May 17 21:18:24 2021 +0530
0dc130
0dc130
    libndctl/papr: Fix probe for papr-scm compatible nvdimms
0dc130
    
0dc130
    With recent changes introduced for unification of PAPR and NFIT
0dc130
    families the probe for papr-scm nvdimms is broken since they don't
0dc130
    expose 'handle' or 'phys_id' sysfs attributes. These attributes are
0dc130
    only exposed by NFIT and 'nvdimm_test' nvdimms. Since 'unable to read'
0dc130
    these sysfs attributes is a non-recoverable error hence this prevents
0dc130
    probing of 'PAPR-SCM' nvdimms and ndctl reports following error:
0dc130
    
0dc130
    $ sudo NDCTL_LOG=debug ndctl list -DH
0dc130
    libndctl: ndctl_new: ctx 0x10015342c70 created
0dc130
    libndctl: add_dimm: nmem1: probe failed: Operation not permitted
0dc130
    libndctl: __sysfs_device_parse: nmem1: add_dev() failed
0dc130
    libndctl: add_dimm: nmem0: probe failed: Operation not permitted
0dc130
    libndctl: __sysfs_device_parse: nmem0: add_dev() failed
0dc130
    
0dc130
    Fixing this bug is complicated by the fact these attributes are needed
0dc130
    for by the 'nvdimm_test' nvdimms which also uses the
0dc130
    NVDIMM_FAMILY_PAPR. Adding a two way comparison for these two
0dc130
    attributes in populate_dimm_attributes() to distinguish between
0dc130
    'nvdimm_test' and papr-scm nvdimms will be clunky and make future
0dc130
    updates to populate_dimm_attributes() error prone.
0dc130
    
0dc130
    So, this patch proposes to fix the issue by re-introducing
0dc130
    add_papr_dimm() to probe both papr-scm and 'nvdimm_test' nvdimms. The
0dc130
    'compatible' sysfs attribute associated with the PAPR device is used
0dc130
    to distinguish between the two nvdimm types and in case an
0dc130
    'nvdimm_test' device is detected then forward its probe to
0dc130
    populate_dimm_attributes().
0dc130
    
0dc130
    families")
0dc130
    
0dc130
    Link: https://lore.kernel.org/r/20210517154824.142237-1-vaibhav@linux.ibm.com
0dc130
    Fixes: daef3a386a9c("libndctl: Unify adding dimms for papr and nfit
0dc130
    Signed-off-by: Vaibhav Jain <vaibhav@linux.ibm.com>
0dc130
    Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
0dc130
0dc130
diff -up ndctl-71.1/ndctl/lib/libndctl.c.orig ndctl-71.1/ndctl/lib/libndctl.c
0dc130
--- ndctl-71.1/ndctl/lib/libndctl.c.orig	2022-06-06 17:16:20.703762581 -0400
0dc130
+++ ndctl-71.1/ndctl/lib/libndctl.c	2022-06-06 17:17:34.932019990 -0400
0dc130
@@ -1757,6 +1757,58 @@ static int populate_dimm_attributes(stru
0dc130
 	return rc;
0dc130
 }
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
+	/* Check the compatibility of the probed nvdimm */
0dc130
+	sprintf(path, "%s/../of_node/compatible", dimm_base);
0dc130
+	if (sysfs_read_attr(ctx, path, buf) < 0) {
0dc130
+		dbg(ctx, "%s: Unable to read compatible field\n", devname);
0dc130
+		rc =  -ENODEV;
0dc130
+		goto out;
0dc130
+	}
0dc130
+
0dc130
+	dbg(ctx, "%s:Compatible of_pmem = '%s'\n", devname, buf);
0dc130
+
0dc130
+	/* Probe for papr-scm memory */
0dc130
+	if (strcmp(buf, "ibm,pmemory") == 0) {
0dc130
+		/* Read the dimm flags file */
0dc130
+		sprintf(path, "%s/papr/flags", dimm_base);
0dc130
+		if (sysfs_read_attr(ctx, path, buf) < 0) {
0dc130
+			rc = -errno;
0dc130
+			err(ctx, "%s: Unable to read dimm-flags\n", devname);
0dc130
+			goto out;
0dc130
+		}
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
+	} else if (strcmp(buf, "nvdimm_test") == 0) {
0dc130
+		/* probe via common populate_dimm_attributes() */
0dc130
+		rc = populate_dimm_attributes(dimm, dimm_base, "papr");
0dc130
+	}
0dc130
+out:
0dc130
+	free(path);
0dc130
+	return rc;
0dc130
+}
0dc130
+
0dc130
 static void *add_dimm(void *parent, int id, const char *dimm_base)
0dc130
 {
0dc130
 	int formats, i, rc = -ENODEV;
0dc130
@@ -1848,8 +1900,9 @@ static void *add_dimm(void *parent, int
0dc130
 	/* Check if the given dimm supports nfit */
0dc130
 	if (ndctl_bus_has_nfit(bus)) {
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
+	} else if (ndctl_bus_has_of_node(bus)) {
0dc130
+		rc = add_papr_dimm(dimm, dimm_base);
0dc130
+	}
0dc130
 
0dc130
 	if (rc == -ENODEV) {
0dc130
 		/* Unprobed dimm with no family */