anitazha / rpms / ndctl

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