Implement support for handling PAPR DSM commands in papr_scm
module. We advertise support for ND_CMD_CALL for the dimm command mask
and implement necessary scaffolding in the module to handle ND_CMD_CALL
ioctl and DSM commands that we receive.

The layout of the DSM commands as we expect from libnvdimm/libndctl is
defined in 'struct nd_pkg_papr_scm' which contains a 'struct
nd_cmd_pkg' as header. This header is used to communicate the DSM
command via 'nd_pkg_papr_scm->nd_command' and size of payload that
need to be sent/received for servicing the DSM.

The PAPR DSM commands are assigned indexes started from 0x10000 to
prevent them from overlapping ND_CMD_* values and also makes handling
dimm commands in papr_scm_ndctl() easier via a simplified switch-case
block. For this a new function cmd_to_func() is implemented that reads
the args to papr_scm_ndctl() , performs sanity tests on them and
converts them to PAPR DSM commands which can then be handled via the
switch-case block.

Signed-off-by: Vaibhav Jain <vaib...@linux.ibm.com>
---
 arch/powerpc/platforms/pseries/papr_scm.c | 96 +++++++++++++++++++++--
 1 file changed, 89 insertions(+), 7 deletions(-)

diff --git a/arch/powerpc/platforms/pseries/papr_scm.c 
b/arch/powerpc/platforms/pseries/papr_scm.c
index deaece6e4d18..e1a2c0e61077 100644
--- a/arch/powerpc/platforms/pseries/papr_scm.c
+++ b/arch/powerpc/platforms/pseries/papr_scm.c
@@ -20,10 +20,29 @@
 #define PAPR_SCM_DIMM_CMD_MASK \
        ((1ul << ND_CMD_GET_CONFIG_SIZE) | \
         (1ul << ND_CMD_GET_CONFIG_DATA) | \
-        (1ul << ND_CMD_SET_CONFIG_DATA))
+        (1ul << ND_CMD_SET_CONFIG_DATA) | \
+        (1ul << ND_CMD_CALL))
 
 #define PAPR_SCM_MAX_PERF_STAT 4096
 
+/*
+ * Sub commands for ND_CMD_CALL. To prevent overlap from ND_CMD_*, values for
+ * these enums start at 0x10000. These values are then returned from
+ * cmd_to_func() making it easy to implement the switch-case block in
+ * papr_scm_ndctl()
+ */
+enum {
+       DSM_PAPR_MIN =  0x10000,
+       DSM_PAPR_MAX,
+};
+
+/* Payload expected with ND_CMD_CALL ioctl from libnvdimm */
+struct nd_pkg_papr_scm {
+       struct nd_cmd_pkg hdr;          /* Package header containing sub-cmd */
+       uint32_t cmd_status;            /* Out: Sub-cmd status returned back */
+       uint8_t payload[];              /* Out: Sub-cmd data buffer */
+} __packed;
+
 /* Buffer layout returned by phyp when reporting drc perf stats */
 struct papr_scm_perf_stats {
        uint8_t version;                /* Should be 0x01 */
@@ -303,19 +322,74 @@ static int papr_scm_meta_set(struct papr_scm_priv *p,
        return 0;
 }
 
+/*
+ * Validate the input to dimm-control function and return papr_scm specific
+ * commands. This does sanity validation to ND_CMD_CALL sub-command packages.
+ */
+static int cmd_to_func(struct nvdimm *nvdimm, unsigned int cmd, void *buf,
+                      unsigned int buf_len)
+{
+       unsigned long cmd_mask = PAPR_SCM_DIMM_CMD_MASK;
+       struct nd_pkg_papr_scm *pkg = (struct nd_pkg_papr_scm *)buf;
+
+       /* Only dimm-specific calls are supported atm */
+       if (!nvdimm)
+               return -EINVAL;
+
+       if (!test_bit(cmd, &cmd_mask)) {
+
+               pr_debug("%s: Unsupported cmd=%u\n", __func__, cmd);
+               return -EINVAL;
+
+       } else if (cmd != ND_CMD_CALL) {
+
+               return cmd;
+
+       } else if (buf_len < sizeof(struct nd_pkg_papr_scm)) {
+
+               pr_debug("%s: Invalid pkg size=%u\n", __func__, buf_len);
+               return -EINVAL;
+
+       } else if (pkg->hdr.nd_family != NVDIMM_FAMILY_PAPR) {
+
+               pr_debug("%s: Invalid pkg family=0x%llx\n", __func__,
+                        pkg->hdr.nd_family);
+               return -EINVAL;
+
+       } else if (pkg->hdr.nd_command <= DSM_PAPR_MIN ||
+                  pkg->hdr.nd_command >= DSM_PAPR_MAX) {
+
+               /* for unknown subcommands return ND_CMD_CALL */
+               pr_debug("%s: Unknown sub-command=0x%llx\n", __func__,
+                        pkg->hdr.nd_command);
+               return ND_CMD_CALL;
+       }
+
+       /* Return the DSM_PAPR_SCM_* command */
+       return pkg->hdr.nd_command;
+}
+
 int papr_scm_ndctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm 
*nvdimm,
                unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
 {
        struct nd_cmd_get_config_size *get_size_hdr;
        struct papr_scm_priv *p;
+       struct nd_pkg_papr_scm *call_pkg = NULL;
+       int cmd_in, rc;
 
-       /* Only dimm-specific calls are supported atm */
-       if (!nvdimm)
-               return -EINVAL;
+       /* Use a local variable in case cmd_rc pointer is NULL */
+       if (cmd_rc == NULL)
+               cmd_rc = &rc;
+
+       cmd_in = cmd_to_func(nvdimm, cmd, buf, buf_len);
+       if (cmd_in < 0) {
+               pr_debug("%s: Invalid cmd=%u. Err=%d\n", __func__, cmd, cmd_in);
+               return cmd_in;
+       }
 
        p = nvdimm_provider_data(nvdimm);
 
-       switch (cmd) {
+       switch (cmd_in) {
        case ND_CMD_GET_CONFIG_SIZE:
                get_size_hdr = buf;
 
@@ -333,13 +407,21 @@ int papr_scm_ndctl(struct nvdimm_bus_descriptor *nd_desc, 
struct nvdimm *nvdimm,
                *cmd_rc = papr_scm_meta_set(p, buf);
                break;
 
+       case ND_CMD_CALL:
+               /* This happens if subcommand package sanity fails */
+               call_pkg = (struct nd_pkg_papr_scm *) buf;
+               call_pkg->cmd_status = -ENOENT;
+               *cmd_rc = 0;
+               break;
+
        default:
-               return -EINVAL;
+               dev_dbg(&p->pdev->dev, "Unknown command = %d\n", cmd_in);
+               *cmd_rc = -EINVAL;
        }
 
        dev_dbg(&p->pdev->dev, "returned with cmd_rc = %d\n", *cmd_rc);
 
-       return 0;
+       return *cmd_rc;
 }
 
 static inline int papr_scm_node(int node)
-- 
2.24.1

Reply via email to