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

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