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

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