Blame SOURCES/9ef460e-libndctl-papr-Add-limited-support-for-inject-smart.patch

0dc130
libndctl/papr: Add limited support for inject-smart
0dc130
0dc130
BZ: 
0dc130
Brew: 
0dc130
0dc130
commit 9ef460eb7fd1b1f286955b18db8c18cd1a640e77
0dc130
Author: Vaibhav Jain <vaibhav@linux.ibm.com>
0dc130
Date:   Tue Jan 25 02:08:04 2022 +0530
0dc130
0dc130
    libndctl/papr: Add limited support for inject-smart
0dc130
    
0dc130
    Implements support for ndctl inject-smart command by providing an
0dc130
    implementation of 'smart_inject*' dimm-ops callbacks. Presently only
0dc130
    support for injecting unsafe-shutdown and fatal-health states is
0dc130
    available.
0dc130
    
0dc130
    The patch also introduce various PAPR PDSM structures that are used to
0dc130
    communicate the inject-smart errors to the papr_scm kernel
0dc130
    module. This is done via SMART_INJECT PDSM which sends a payload of
0dc130
    type 'struct nd_papr_pdsm_smart_inject'.
0dc130
    
0dc130
    With the patch following output from ndctl inject-smart command is
0dc130
    expected for PAPR NVDIMMs:
0dc130
    
0dc130
    $ sudo ndctl inject-smart -fU nmem0
0dc130
    [
0dc130
      {
0dc130
        "dev":"nmem0",
0dc130
        "flag_failed_flush":true,
0dc130
        "flag_smart_event":true,
0dc130
        "health":{
0dc130
          "health_state":"fatal",
0dc130
          "shutdown_state":"dirty",
0dc130
          "shutdown_count":0
0dc130
        }
0dc130
      }
0dc130
    ]
0dc130
    
0dc130
    $ sudo ndctl inject-smart -N nmem0
0dc130
    [
0dc130
      {
0dc130
        "dev":"nmem0",
0dc130
        "health":{
0dc130
          "health_state":"ok",
0dc130
          "shutdown_state":"clean",
0dc130
          "shutdown_count":0
0dc130
        }
0dc130
      }
0dc130
    ]
0dc130
    
0dc130
    The patch depends on the kernel PAPR PDSM implementation for
0dc130
    PDSM_SMART_INJECT posted at [1].
0dc130
    
0dc130
    [1] : https://lore.kernel.org/nvdimm/20220124202204.1488346-1-vaibhav@linux.ibm.com/
0dc130
    
0dc130
    Link: https://lore.kernel.org/r/20220124203804.1490254-1-vaibhav@linux.ibm.com
0dc130
    Reviewed-by: Ira Weiny <ira.weiny@intel.com>
0dc130
    Signed-off-by: Shivaprasad G Bhat <sbhat@linux.ibm.com>
0dc130
    Signed-off-by: Vaibhav Jain <vaibhav@linux.ibm.com>
0dc130
    Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
0dc130
0dc130
diff --git a/ndctl/lib/papr.c b/ndctl/lib/papr.c
0dc130
index 46cf9c1..7a1d559 100644
0dc130
--- a/ndctl/lib/papr.c
0dc130
+++ b/ndctl/lib/papr.c
0dc130
@@ -221,6 +221,41 @@ static unsigned int papr_smart_get_shutdown_state(struct ndctl_cmd *cmd)
0dc130
 	return health.dimm_bad_shutdown;
0dc130
 }
0dc130
 
0dc130
+static int papr_smart_inject_supported(struct ndctl_dimm *dimm)
0dc130
+{
0dc130
+	if (!ndctl_dimm_is_cmd_supported(dimm, ND_CMD_CALL))
0dc130
+		return -EOPNOTSUPP;
0dc130
+
0dc130
+	if (!test_dimm_dsm(dimm, PAPR_PDSM_SMART_INJECT))
0dc130
+		return -EIO;
0dc130
+
0dc130
+	return ND_SMART_INJECT_HEALTH_STATE | ND_SMART_INJECT_UNCLEAN_SHUTDOWN;
0dc130
+}
0dc130
+
0dc130
+static int papr_smart_inject_valid(struct ndctl_cmd *cmd)
0dc130
+{
0dc130
+	if (cmd->type != ND_CMD_CALL ||
0dc130
+	    to_pdsm(cmd)->cmd_status != 0 ||
0dc130
+	    to_pdsm_cmd(cmd) != PAPR_PDSM_SMART_INJECT)
0dc130
+		return -EINVAL;
0dc130
+
0dc130
+	return 0;
0dc130
+}
0dc130
+
0dc130
+static struct ndctl_cmd *papr_new_smart_inject(struct ndctl_dimm *dimm)
0dc130
+{
0dc130
+	struct ndctl_cmd *cmd;
0dc130
+
0dc130
+	cmd = allocate_cmd(dimm, PAPR_PDSM_SMART_INJECT,
0dc130
+			sizeof(struct nd_papr_pdsm_smart_inject));
0dc130
+	if (!cmd)
0dc130
+		return NULL;
0dc130
+	/* Set the input payload size */
0dc130
+	to_ndcmd(cmd)->nd_size_in = ND_PDSM_HDR_SIZE +
0dc130
+		sizeof(struct nd_papr_pdsm_smart_inject);
0dc130
+	return cmd;
0dc130
+}
0dc130
+
0dc130
 static unsigned int papr_smart_get_life_used(struct ndctl_cmd *cmd)
0dc130
 {
0dc130
 	struct nd_papr_pdsm_health health;
0dc130
@@ -255,11 +290,37 @@ static unsigned int papr_smart_get_shutdown_count(struct ndctl_cmd *cmd)
0dc130
 
0dc130
 	return (health.extension_flags & PDSM_DIMM_DSC_VALID) ?
0dc130
 		(health.dimm_dsc) : 0;
0dc130
+}
0dc130
+
0dc130
+static int papr_cmd_smart_inject_fatal(struct ndctl_cmd *cmd, bool enable)
0dc130
+{
0dc130
+	if (papr_smart_inject_valid(cmd) < 0)
0dc130
+		return -EINVAL;
0dc130
+
0dc130
+	to_payload(cmd)->inject.flags |= PDSM_SMART_INJECT_HEALTH_FATAL;
0dc130
+	to_payload(cmd)->inject.fatal_enable = enable;
0dc130
 
0dc130
+	return 0;
0dc130
+}
0dc130
+
0dc130
+static int papr_cmd_smart_inject_unsafe_shutdown(struct ndctl_cmd *cmd,
0dc130
+						 bool enable)
0dc130
+{
0dc130
+	if (papr_smart_inject_valid(cmd) < 0)
0dc130
+		return -EINVAL;
0dc130
+
0dc130
+	to_payload(cmd)->inject.flags |= PDSM_SMART_INJECT_BAD_SHUTDOWN;
0dc130
+	to_payload(cmd)->inject.unsafe_shutdown_enable = enable;
0dc130
+
0dc130
+	return 0;
0dc130
 }
0dc130
 
0dc130
 struct ndctl_dimm_ops * const papr_dimm_ops = &(struct ndctl_dimm_ops) {
0dc130
 	.cmd_is_supported = papr_cmd_is_supported,
0dc130
+	.new_smart_inject = papr_new_smart_inject,
0dc130
+	.smart_inject_supported = papr_smart_inject_supported,
0dc130
+	.smart_inject_fatal = papr_cmd_smart_inject_fatal,
0dc130
+	.smart_inject_unsafe_shutdown = papr_cmd_smart_inject_unsafe_shutdown,
0dc130
 	.smart_get_flags = papr_smart_get_flags,
0dc130
 	.get_firmware_status =  papr_get_firmware_status,
0dc130
 	.xlat_firmware_status = papr_xlat_firmware_status,
0dc130
diff --git a/ndctl/lib/papr_pdsm.h b/ndctl/lib/papr_pdsm.h
0dc130
index f45b1e4..20ac20f 100644
0dc130
--- a/ndctl/lib/papr_pdsm.h
0dc130
+++ b/ndctl/lib/papr_pdsm.h
0dc130
@@ -121,12 +121,29 @@ struct nd_papr_pdsm_health {
0dc130
 enum papr_pdsm {
0dc130
 	PAPR_PDSM_MIN = 0x0,
0dc130
 	PAPR_PDSM_HEALTH,
0dc130
+	PAPR_PDSM_SMART_INJECT,
0dc130
 	PAPR_PDSM_MAX,
0dc130
 };
0dc130
+/* Flags for injecting specific smart errors */
0dc130
+#define PDSM_SMART_INJECT_HEALTH_FATAL		(1 << 0)
0dc130
+#define PDSM_SMART_INJECT_BAD_SHUTDOWN		(1 << 1)
0dc130
+
0dc130
+struct nd_papr_pdsm_smart_inject {
0dc130
+	union {
0dc130
+		struct {
0dc130
+			/* One or more of PDSM_SMART_INJECT_ */
0dc130
+			__u32 flags;
0dc130
+			__u8 fatal_enable;
0dc130
+			__u8 unsafe_shutdown_enable;
0dc130
+		};
0dc130
+		__u8 buf[ND_PDSM_PAYLOAD_MAX_SIZE];
0dc130
+	};
0dc130
+};
0dc130
 
0dc130
 /* Maximal union that can hold all possible payload types */
0dc130
 union nd_pdsm_payload {
0dc130
 	struct nd_papr_pdsm_health health;
0dc130
+	struct nd_papr_pdsm_smart_inject inject;
0dc130
 	__u8 buf[ND_PDSM_PAYLOAD_MAX_SIZE];
0dc130
 } __attribute__((packed));
0dc130