Skip to contentSkip to conversation

libndctl/msft: Cleanup the code

PATCH RESEND In review Alexander Motin 2 participants 5 messages Jan 13, 2023 – Feb 15, 2023 libnvdimm: non-volatile memory device

lore
Share & tools
lore
#0 lore raw

PATCH RESEND v2 1/2 libndctl/msft: Cleanup the code

Clean up the code, making it more uniform with others and allowing more methods to be implemented later:
- remove nonsense NDN_MSFT_CMD_SMART definition, replacing it
with real commands, primarity NDN_MSFT_CMD_NHEALTH;
- allow sending arbitrary commands and add their descriptions;
- add custom cmd_is_supported method to allow monitor mode.

---
 ndctl/lib/msft.c | 58 ++++++++++++++++++++++++++++++++++++++----------
 ndctl/lib/msft.h | 13 ++++-------
 2 files changed, 50 insertions(+), 21 deletions(-)
ndctl/lib/msft.c +46 -12
index 3112799..efc7fe1 100644
@@ -2,6 +2,7 @@
 // Copyright (C) 2016-2017 Dell, Inc.
 // Copyright (C) 2016 Hewlett Packard Enterprise Development LP
 // Copyright (C) 2016-2020, Intel Corporation.
+/* Copyright (C) 2022 iXsystems, Inc. */
 #include <stdlib.h>
 #include <limits.h>
 #include <util/log.h>
@@ -12,12 +13,39 @@
 #define CMD_MSFT(_c) ((_c)->msft)
 #define CMD_MSFT_SMART(_c) (CMD_MSFT(_c)->u.smart.data)
 
+static const char *msft_cmd_desc(int fn)
+{
+	static const char * const descs[] = {
+		[NDN_MSFT_CMD_CHEALTH] = "critical_health",
+		[NDN_MSFT_CMD_NHEALTH] = "nvdimm_health",
+		[NDN_MSFT_CMD_EHEALTH] = "es_health",
+	};
+	const char *desc;
+
+	if (fn >= (int) ARRAY_SIZE(descs))
+		return "unknown";
+	desc = descs[fn];
+	if (!desc)
+		return "unknown";
+	return desc;
+}
+
+static bool msft_cmd_is_supported(struct ndctl_dimm *dimm, int cmd)
+{
+	/* Handle this separately to support monitor mode */
+	if (cmd == ND_CMD_SMART)
+		return true;
+
+	return !!(dimm->cmd_mask & (1ULL << cmd));
+}
+
 static u32 msft_get_firmware_status(struct ndctl_cmd *cmd)
 {
 	return cmd->msft->u.smart.status;
 }
 
-static struct ndctl_cmd *msft_dimm_cmd_new_smart(struct ndctl_dimm *dimm)
+static struct ndctl_cmd *alloc_msft_cmd(struct ndctl_dimm *dimm,
+		unsigned int func, size_t in_size, size_t out_size)
 {
 	struct ndctl_bus *bus = ndctl_dimm_get_bus(dimm);
 	struct ndctl_ctx *ctx = ndctl_bus_get_ctx(bus);
@@ -30,12 +58,12 @@ static struct ndctl_cmd *msft_dimm_cmd_new_smart(struct ndctl_dimm *dimm)
 		return NULL;
 	}
 
-	if (test_dimm_dsm(dimm, NDN_MSFT_CMD_SMART) == DIMM_DSM_UNSUPPORTED) {
+	if (test_dimm_dsm(dimm, func) == DIMM_DSM_UNSUPPORTED) {
 		dbg(ctx, "unsupported function\n");
 		return NULL;
 	}
 
-	size = sizeof(*cmd) + sizeof(struct ndn_pkg_msft);
+	size = sizeof(*cmd) + sizeof(struct nd_cmd_pkg) + in_size + out_size;
 	cmd = calloc(1, size);
 	if (!cmd)
 		return NULL;
@@ -45,25 +73,30 @@ static struct ndctl_cmd *msft_dimm_cmd_new_smart(struct ndctl_dimm *dimm)
 	cmd->type = ND_CMD_CALL;
 	cmd->size = size;
 	cmd->status = 1;
+	cmd->get_firmware_status = msft_get_firmware_status;
 
 	msft = CMD_MSFT(cmd);
 	msft->gen.nd_family = NVDIMM_FAMILY_MSFT;
-	msft->gen.nd_command = NDN_MSFT_CMD_SMART;
+	msft->gen.nd_command = func;
 	msft->gen.nd_fw_size = 0;
-	msft->gen.nd_size_in = offsetof(struct ndn_msft_smart, status);
-	msft->gen.nd_size_out = sizeof(msft->u.smart);
+	msft->gen.nd_size_in = in_size;
+	msft->gen.nd_size_out = out_size;
 	msft->u.smart.status = 0;
-	cmd->get_firmware_status = msft_get_firmware_status;
 
 	return cmd;
 }
 
+static struct ndctl_cmd *msft_dimm_cmd_new_smart(struct ndctl_dimm *dimm)
+{
+	return (alloc_msft_cmd(dimm, NDN_MSFT_CMD_NHEALTH,
+			0, sizeof(struct ndn_msft_smart)));
+}
+
 static int msft_smart_valid(struct ndctl_cmd *cmd)
 {
 	if (cmd->type != ND_CMD_CALL ||
-	    cmd->size != sizeof(*cmd) + sizeof(struct ndn_pkg_msft) ||
 	    CMD_MSFT(cmd)->gen.nd_family != NVDIMM_FAMILY_MSFT ||
-	    CMD_MSFT(cmd)->gen.nd_command != NDN_MSFT_CMD_SMART ||
+	    CMD_MSFT(cmd)->gen.nd_command != NDN_MSFT_CMD_NHEALTH ||
 	    cmd->status != 0)
 		return cmd->status < 0 ? cmd->status : -EINVAL;
 	return 0;
@@ -80,9 +113,8 @@ static unsigned int msft_cmd_smart_get_flags(struct ndctl_cmd *cmd)
 	}
 
 	/* below health data can be retrieved via MSFT _DSM function 11 */
-	return NDN_MSFT_SMART_HEALTH_VALID |
-		NDN_MSFT_SMART_TEMP_VALID |
-		NDN_MSFT_SMART_USED_VALID;
+	return ND_SMART_HEALTH_VALID | ND_SMART_TEMP_VALID |
+	    ND_SMART_USED_VALID;
 }
 
 static unsigned int num_set_bit_health(__u16 num)
@@ -171,6 +203,8 @@ static int msft_cmd_xlat_firmware_status(struct ndctl_cmd *cmd)
 }
 
 struct ndctl_dimm_ops * const msft_dimm_ops = &(struct ndctl_dimm_ops) {
+	.cmd_desc = msft_cmd_desc,
+	.cmd_is_supported = msft_cmd_is_supported,
 	.new_smart = msft_dimm_cmd_new_smart,
 	.smart_get_flags = msft_cmd_smart_get_flags,
 	.smart_get_health = msft_cmd_smart_get_health,
ndctl/lib/msft.h +4 -9
index 978cc11..8d246a5 100644
@@ -2,21 +2,16 @@
 /* Copyright (C) 2016-2017 Dell, Inc. */
 /* Copyright (C) 2016 Hewlett Packard Enterprise Development LP */
 /* Copyright (C) 2014-2020, Intel Corporation. */
+/* Copyright (C) 2022 iXsystems, Inc. */
 #ifndef __NDCTL_MSFT_H__
 #define __NDCTL_MSFT_H__
 
 enum {
-	NDN_MSFT_CMD_QUERY = 0,
-
-	/* non-root commands */
-	NDN_MSFT_CMD_SMART = 11,
+	NDN_MSFT_CMD_CHEALTH = 10,
+	NDN_MSFT_CMD_NHEALTH = 11,
+	NDN_MSFT_CMD_EHEALTH = 12,
 };
 
-/* NDN_MSFT_CMD_SMART */
-#define NDN_MSFT_SMART_HEALTH_VALID	ND_SMART_HEALTH_VALID
-#define NDN_MSFT_SMART_TEMP_VALID	ND_SMART_TEMP_VALID
-#define NDN_MSFT_SMART_USED_VALID	ND_SMART_USED_VALID
-
 /*
  * This is actually function 11 data,
  * This is the closest I can find to match smart
Signature · 1 line
2.30.2
#1 lore raw

PATCH RESEND v2 2/2 libndctl/msft: Improve "smart" state reporting

Previous code reported "smart" state based on number of bits set in the module health field. But actually any single bit set there already means critical failure. Rework the logic according to specification, properly reporting non-critical state in case of warning threshold reached, critical in case of any module health bit set or error threshold reached and fatal if NVDIMM exhausted its life time. In attempt to report the cause of failure in absence of better methods, report reached thresholds as more or less matching alarms.

---
 ndctl/lib/msft.c | 55 ++++++++++++++++++++++++++----------------------
 1 file changed, 30 insertions(+), 25 deletions(-)
ndctl/lib/msft.c +30 -25
index efc7fe1..8f66c97 100644
@@ -114,26 +114,32 @@ static unsigned int msft_cmd_smart_get_flags(struct ndctl_cmd *cmd)
 
 	/* below health data can be retrieved via MSFT _DSM function 11 */
 	return ND_SMART_HEALTH_VALID | ND_SMART_TEMP_VALID |
-	    ND_SMART_USED_VALID;
+	    ND_SMART_USED_VALID | ND_SMART_ALARM_VALID;
 }
 
-static unsigned int num_set_bit_health(__u16 num)
+static unsigned int msft_cmd_smart_get_health(struct ndctl_cmd *cmd)
 {
-	int i;
-	__u16 n = num & 0x7FFF;
-	unsigned int count = 0;
+	unsigned int health = 0;
+	int rc;
 
-	for (i = 0; i < 15; i++)
-		if (!!(n & (1 << i)))
-			count++;
+	rc = msft_smart_valid(cmd);
+	if (rc < 0) {
+		errno = -rc;
+		return UINT_MAX;
+	}
 
-	return count;
+	if (CMD_MSFT_SMART(cmd)->nvm_lifetime == 0)
+		health |= ND_SMART_FATAL_HEALTH;
+	if (CMD_MSFT_SMART(cmd)->health != 0 ||
+	    CMD_MSFT_SMART(cmd)->err_thresh_stat != 0)
+		health |= ND_SMART_CRITICAL_HEALTH;
+	if (CMD_MSFT_SMART(cmd)->warn_thresh_stat != 0)
+		health |= ND_SMART_NON_CRITICAL_HEALTH;
+	return health;
 }
 
-static unsigned int msft_cmd_smart_get_health(struct ndctl_cmd *cmd)
+static unsigned int msft_cmd_smart_get_media_temperature(struct ndctl_cmd *cmd)
 {
-	unsigned int health;
-	unsigned int num;
 	int rc;
 
 	rc = msft_smart_valid(cmd);
@@ -142,21 +148,13 @@ static unsigned int msft_cmd_smart_get_health(struct ndctl_cmd *cmd)
 		return UINT_MAX;
 	}
 
-	num = num_set_bit_health(CMD_MSFT_SMART(cmd)->health);
-	if (num == 0)
-		health = 0;
-	else if (num < 2)
-		health = ND_SMART_NON_CRITICAL_HEALTH;
-	else if (num < 3)
-		health = ND_SMART_CRITICAL_HEALTH;
-	else
-		health = ND_SMART_FATAL_HEALTH;
-
-	return health;
+	return CMD_MSFT_SMART(cmd)->temp * 16;
 }
 
-static unsigned int msft_cmd_smart_get_media_temperature(struct ndctl_cmd *cmd)
+static unsigned int msft_cmd_smart_get_alarm_flags(struct ndctl_cmd *cmd)
 {
+	__u8 stat;
+	unsigned int flags = 0;
 	int rc;
 
 	rc = msft_smart_valid(cmd);
@@ -165,7 +163,13 @@ static unsigned int msft_cmd_smart_get_media_temperature(struct ndctl_cmd *cmd)
 		return UINT_MAX;
 	}
 
-	return CMD_MSFT_SMART(cmd)->temp * 16;
+	stat = CMD_MSFT_SMART(cmd)->err_thresh_stat |
+	    CMD_MSFT_SMART(cmd)->warn_thresh_stat;
+	if (stat & 3) /* NVM_LIFETIME/ES_LIFETIME */
+		flags |= ND_SMART_SPARE_TRIP;
+	if (stat & 4) /* ES_TEMP */
+		flags |= ND_SMART_CTEMP_TRIP;
+	return flags;
 }
 
 static unsigned int msft_cmd_smart_get_life_used(struct ndctl_cmd *cmd)
@@ -209,6 +213,7 @@ struct ndctl_dimm_ops * const msft_dimm_ops = &(struct ndctl_dimm_ops) {
 	.smart_get_flags = msft_cmd_smart_get_flags,
 	.smart_get_health = msft_cmd_smart_get_health,
 	.smart_get_media_temperature = msft_cmd_smart_get_media_temperature,
+	.smart_get_alarm_flags = msft_cmd_smart_get_alarm_flags,
 	.smart_get_life_used = msft_cmd_smart_get_life_used,
 	.xlat_firmware_status = msft_cmd_xlat_firmware_status,
 };
Signature · 1 line
2.30.2
maintainer
#2 lore raw

Re: [ndctl PATCH 2/2 v2 RESEND] libndctl/msft: Improve "smart" state reporting

[Dropping Lijun from CC due to a bounce]

Quoted 9 lines · Alexander Motin

Previous code reported "smart" state based on number of bits set in the module health field.  But actually any single bit set there already means critical failure.  Rework the logic according to specification, properly reporting non-critical state in case of warning threshold reached, critical in case of any module health bit set or error threshold reached and fatal if NVDIMM exhausted its life time.  In attempt to report the cause of failure in absence of better methods, report reached thresholds as more or less matching alarms.

This looks like unnecessarily aggressive wrapping. Commit messages are typically wrapped at 72 chars.

Other than that this looks fine.

Quoted 104 lines

---
 ndctl/lib/msft.c | 55 ++++++++++++++++++++++++++----------------------

 1 file changed, 30 insertions(+), 25 deletions(-)
ndctl/lib/msft.c +0 -0
index efc7fe1..8f66c97 100644
@@ -114,26 +114,32 @@ static unsigned int msft_cmd_smart_get_flags(struct ndctl_cmd *cmd)


        /* below health data can be retrieved via MSFT _DSM function 11 */ return ND_SMART_HEALTH_VALID | ND_SMART_TEMP_VALID |
-           ND_SMART_USED_VALID;
+           ND_SMART_USED_VALID | ND_SMART_ALARM_VALID;
 }

ndctl/lib/msft.c +1 -1
-static unsigned int num_set_bit_health(__u16 num)
+static unsigned int msft_cmd_smart_get_health(struct ndctl_cmd *cmd)

 {
-       int i;
-       __u16 n = num & 0x7FFF;
-       unsigned int count = 0;
+       unsigned int health = 0;
+       int rc;

-       for (i = 0; i < 15; i++)
-               if (!!(n & (1 << i)))
-                       count++;
+       rc = msft_smart_valid(cmd);
+       if (rc < 0) {
+               errno = -rc;
+               return UINT_MAX;
+       }

-       return count;
+       if (CMD_MSFT_SMART(cmd)->nvm_lifetime == 0) +               health |= ND_SMART_FATAL_HEALTH; +       if (CMD_MSFT_SMART(cmd)->health != 0 || +           CMD_MSFT_SMART(cmd)->err_thresh_stat != 0) +               health |= ND_SMART_CRITICAL_HEALTH; +       if (CMD_MSFT_SMART(cmd)->warn_thresh_stat != 0) +               health |= ND_SMART_NON_CRITICAL_HEALTH; +       return health;
 }

ndctl/lib/msft.c +1 -1
-static unsigned int msft_cmd_smart_get_health(struct ndctl_cmd *cmd)
+static unsigned int msft_cmd_smart_get_media_temperature(struct ndctl_cmd *cmd)

 {
-       unsigned int health;
-       unsigned int num;
        int rc;

        rc = msft_smart_valid(cmd);

ndctl/lib/msft.c +0 -0
@@ -142,21 +148,13 @@ static unsigned int msft_cmd_smart_get_health(struct ndctl_cmd *cmd)
                 return UINT_MAX;
         }


-       num = num_set_bit_health(CMD_MSFT_SMART(cmd)->health); -       if (num == 0)
-               health = 0;
-       else if (num < 2)
-               health = ND_SMART_NON_CRITICAL_HEALTH; -       else if (num < 3)
-               health = ND_SMART_CRITICAL_HEALTH;
-       else
-               health = ND_SMART_FATAL_HEALTH;
-
-       return health;
+       return CMD_MSFT_SMART(cmd)->temp * 16;
 }

ndctl/lib/msft.c +1 -1
-static unsigned int msft_cmd_smart_get_media_temperature(struct ndctl_cmd *cmd)
+static unsigned int msft_cmd_smart_get_alarm_flags(struct ndctl_cmd *cmd)

 {
+       __u8 stat;
+       unsigned int flags = 0;
        int rc;

        rc = msft_smart_valid(cmd);

ndctl/lib/msft.c +0 -0
@@ -165,7 +163,13 @@ static unsigned int msft_cmd_smart_get_media_temperature(struct ndctl_cmd *cmd)
                 return UINT_MAX;
         }


-       return CMD_MSFT_SMART(cmd)->temp * 16;
+       stat = CMD_MSFT_SMART(cmd)->err_thresh_stat |
+           CMD_MSFT_SMART(cmd)->warn_thresh_stat;
+       if (stat & 3) /* NVM_LIFETIME/ES_LIFETIME */
+               flags |= ND_SMART_SPARE_TRIP;
+       if (stat & 4) /* ES_TEMP */
+               flags |= ND_SMART_CTEMP_TRIP;
+       return flags;
 }

 static unsigned int msft_cmd_smart_get_life_used(struct ndctl_cmd *cmd)

ndctl/lib/msft.c +1 -0
@@ -209,6 +213,7 @@ struct ndctl_dimm_ops * const msft_dimm_ops = &(struct ndctl_dimm_ops) {
         .smart_get_flags = msft_cmd_smart_get_flags,
         .smart_get_health = msft_cmd_smart_get_health,
         .smart_get_media_temperature = msft_cmd_smart_get_media_temperature,
+       .smart_get_alarm_flags = msft_cmd_smart_get_alarm_flags,
         .smart_get_life_used = msft_cmd_smart_get_life_used,
         .xlat_firmware_status = msft_cmd_xlat_firmware_status,
  };
maintainer
#3 lore raw
In reply to Alexander Motin
Quoted 6 lines · Alexander Motin

Clean up the code, making it more uniform with others and allowing more methods to be implemented later:
 - remove nonsense NDN_MSFT_CMD_SMART definition, replacing it with real commands, primarity NDN_MSFT_CMD_NHEALTH;
 - allow sending arbitrary commands and add their descriptions;
 - add custom cmd_is_supported method to allow monitor mode.

Hi Alexander,

I think I had similar feedback earlier, but these would be reviewable more easily if these three unrelated things are broken up into three separate patches.

A few other minor comments below.

Quoted 84 lines

---
 ndctl/lib/msft.c | 58 ++++++++++++++++++++++++++++++++++++++---------- ndctl/lib/msft.h | 13 ++++-------

 2 files changed, 50 insertions(+), 21 deletions(-)
ndctl/lib/msft.c +1 -0
index 3112799..efc7fe1 100644
@@ -2,6 +2,7 @@
  // Copyright (C) 2016-2017 Dell, Inc.
  // Copyright (C) 2016 Hewlett Packard Enterprise Development LP
  // Copyright (C) 2016-2020, Intel Corporation.
+/* Copyright (C) 2022 iXsystems, Inc. */
  #include <stdlib.h>
  #include <limits.h>
  #include <util/log.h>
@@ -12,12 +13,39 @@
  #define CMD_MSFT(_c) ((_c)->msft)
  #define CMD_MSFT_SMART(_c) (CMD_MSFT(_c)->u.smart.data)

ndctl/lib/msft.c +26 -0
+static const char *msft_cmd_desc(int fn)
+{
+       static const char * const descs[] = {
+               [NDN_MSFT_CMD_CHEALTH] = "critical_health",
+               [NDN_MSFT_CMD_NHEALTH] = "nvdimm_health",
+               [NDN_MSFT_CMD_EHEALTH] = "es_health",
+       };
+       const char *desc;
+
+       if (fn >= (int) ARRAY_SIZE(descs))
+               return "unknown";
+       desc = descs[fn];
+       if (!desc)
+               return "unknown";
+       return desc;
+}
+
+static bool msft_cmd_is_supported(struct ndctl_dimm *dimm, int cmd)
+{
+       /* Handle this separately to support monitor mode */
+       if (cmd == ND_CMD_SMART)
+               return true;
+
+       return !!(dimm->cmd_mask & (1ULL << cmd));
+}
+

 static u32 msft_get_firmware_status(struct ndctl_cmd *cmd) {
        return cmd->msft->u.smart.status;
 }

ndctl/lib/msft.c +2 -1
-static struct ndctl_cmd *msft_dimm_cmd_new_smart(struct ndctl_dimm *dimm)
+static struct ndctl_cmd *alloc_msft_cmd(struct ndctl_dimm *dimm,
+               unsigned int func, size_t in_size, size_t out_size)

 {
        struct ndctl_bus *bus = ndctl_dimm_get_bus(dimm); struct ndctl_ctx *ctx = ndctl_bus_get_ctx(bus);

ndctl/lib/msft.c +0 -0
@@ -30,12 +58,12 @@ static struct ndctl_cmd *msft_dimm_cmd_new_smart(struct ndctl_dimm *dimm)
                 return NULL;
         }


-       if (test_dimm_dsm(dimm, NDN_MSFT_CMD_SMART) == DIMM_DSM_UNSUPPORTED) { +       if (test_dimm_dsm(dimm, func) == DIMM_DSM_UNSUPPORTED) { dbg(ctx, "unsupported function\n");
                return NULL;
        }

-       size = sizeof(*cmd) + sizeof(struct ndn_pkg_msft);
+       size = sizeof(*cmd) + sizeof(struct nd_cmd_pkg) + in_size + out_size; cmd = calloc(1, size);
        if (!cmd)
                return NULL;

ndctl/lib/msft.c +1 -0
@@ -45,25 +73,30 @@ static struct ndctl_cmd *msft_dimm_cmd_new_smart(struct ndctl_dimm *dimm)
         cmd->type = ND_CMD_CALL;
         cmd->size = size;
         cmd->status = 1;
+       cmd->get_firmware_status = msft_get_firmware_status;


        msft = CMD_MSFT(cmd); msft->gen.nd_family = NVDIMM_FAMILY_MSFT; -       msft->gen.nd_command = NDN_MSFT_CMD_SMART;

Quoted 8 lines

+       msft->gen.nd_command = func;
        msft->gen.nd_fw_size = 0;
-       msft->gen.nd_size_in = offsetof(struct ndn_msft_smart, status); -       msft->gen.nd_size_out = sizeof(msft->u.smart);
+       msft->gen.nd_size_in = in_size;
+       msft->gen.nd_size_out = out_size; msft->u.smart.status = 0;
-       cmd->get_firmware_status = msft_get_firmware_status;

Moving this line up seems unnecessary?

ndctl/lib/msft.c static struct ndctl_cmd *msft_dimm_cmd_new_smart(struct ndctl_dimm *dimm) · line 92

Quoted 8 lines


        return cmd;
 }

ndctl/lib/msft.c +4 -0
+static struct ndctl_cmd *msft_dimm_cmd_new_smart(struct ndctl_dimm *dimm)
+{
+       return (alloc_msft_cmd(dimm, NDN_MSFT_CMD_NHEALTH,
+                       0, sizeof(struct ndn_msft_smart)));

The outermost parenthesis here are unnecessary and unconventional for the code base.

Also sizeof(*foo_ptr) is more canonical than sizeof(struct foo). Checkpatch should warn about this.

Quoted 63 lines
ndctl/lib/msft.c +2 -0
+}
+

 static int msft_smart_valid(struct ndctl_cmd *cmd)
 {
        if (cmd->type != ND_CMD_CALL ||
-           cmd->size != sizeof(*cmd) + sizeof(struct ndn_pkg_msft) ||

            CMD_MSFT(cmd)->gen.nd_family != NVDIMM_FAMILY_MSFT ||
-           CMD_MSFT(cmd)->gen.nd_command != NDN_MSFT_CMD_SMART ||

+           CMD_MSFT(cmd)->gen.nd_command != NDN_MSFT_CMD_NHEALTH || cmd->status != 0)
                return cmd->status < 0 ? cmd->status : -EINVAL; return 0;

ndctl/lib/msft.c +0 -0
@@ -80,9 +113,8 @@ static unsigned int msft_cmd_smart_get_flags(struct ndctl_cmd *cmd)
         }


        /* below health data can be retrieved via MSFT _DSM function 11 */ -       return NDN_MSFT_SMART_HEALTH_VALID |
-               NDN_MSFT_SMART_TEMP_VALID |
-               NDN_MSFT_SMART_USED_VALID;
+       return ND_SMART_HEALTH_VALID | ND_SMART_TEMP_VALID |
+           ND_SMART_USED_VALID;
 }

 static unsigned int num_set_bit_health(__u16 num)

ndctl/lib/msft.c +0 -0
@@ -171,6 +203,8 @@ static int msft_cmd_xlat_firmware_status(struct ndctl_cmd *cmd)
  }


 struct ndctl_dimm_ops * const msft_dimm_ops = &(struct ndctl_dimm_ops) { +       .cmd_desc = msft_cmd_desc,
+       .cmd_is_supported = msft_cmd_is_supported,
        .new_smart = msft_dimm_cmd_new_smart, .smart_get_flags = msft_cmd_smart_get_flags, .smart_get_health = msft_cmd_smart_get_health,

ndctl/lib/msft.h +1 -0
index 978cc11..8d246a5 100644
@@ -2,21 +2,16 @@
  /* Copyright (C) 2016-2017 Dell, Inc. */
  /* Copyright (C) 2016 Hewlett Packard Enterprise Development LP */
  /* Copyright (C) 2014-2020, Intel Corporation. */
+/* Copyright (C) 2022 iXsystems, Inc. */
  #ifndef __NDCTL_MSFT_H__
  #define __NDCTL_MSFT_H__


 enum {
-       NDN_MSFT_CMD_QUERY = 0,
-
-       /* non-root commands */
-       NDN_MSFT_CMD_SMART = 11,
+       NDN_MSFT_CMD_CHEALTH = 10,
+       NDN_MSFT_CMD_NHEALTH = 11,
+       NDN_MSFT_CMD_EHEALTH = 12,
 };

ndctl/lib/msft.h +0 -5
-/* NDN_MSFT_CMD_SMART */
-#define NDN_MSFT_SMART_HEALTH_VALID    ND_SMART_HEALTH_VALID
-#define NDN_MSFT_SMART_TEMP_VALID      ND_SMART_TEMP_VALID
-#define NDN_MSFT_SMART_USED_VALID      ND_SMART_USED_VALID
-

 /*
  * This is actually function 11 data,
  * This is the closest I can find to match smart

#4 lore raw
Quoted 8 lines · Vishal L
Quoted 6 lines · Alexander Motin

Clean up the code, making it more uniform with others and allowing more methods to be implemented later:
 - remove nonsense NDN_MSFT_CMD_SMART definition, replacing it
with real commands, primarity NDN_MSFT_CMD_NHEALTH;
 - allow sending arbitrary commands and add their descriptions;
 - add custom cmd_is_supported method to allow monitor mode.

Quoted 5 lines

Hi Alexander,

I think I had similar feedback earlier, but these would be reviewable more easily if these three unrelated things are broken up into three separate patches.

Hi Vishal,

You did ask me to add more details into commit message and possibly split the patch. I've added the details, but about the second I pleaded to save few hours of my time on reshuffling trivial patches. Apparently my plea was not good enough. Apparently you have a long line of people who wish to fix this area. Whatever, doing it now, hopefully to end this and focus on things that actually matter.

Quoted 1 line

A few other minor comments below.

Thanks for looking deeper.

Quoted 81 lines
Quoted 81 lines
---
  ndctl/lib/msft.c | 58 ++++++++++++++++++++++++++++++++++++++----------
  ndctl/lib/msft.h | 13 ++++-------
  2 files changed, 50 insertions(+), 21 deletions(-)
ndctl/lib/msft.c +32 -3
index 3112799..efc7fe1 100644
@@ -2,6 +2,7 @@
  // Copyright (C) 2016-2017 Dell, Inc.
  // Copyright (C) 2016 Hewlett Packard Enterprise Development LP
  // Copyright (C) 2016-2020, Intel Corporation.
+/* Copyright (C) 2022 iXsystems, Inc. */
  #include <stdlib.h>
  #include <limits.h>
  #include <util/log.h>
@@ -12,12 +13,39 @@
  #define CMD_MSFT(_c) ((_c)->msft)
  #define CMD_MSFT_SMART(_c) (CMD_MSFT(_c)->u.smart.data)
 
+static const char *msft_cmd_desc(int fn)
+{
+       static const char * const descs[] = {
+               [NDN_MSFT_CMD_CHEALTH] = "critical_health",
+               [NDN_MSFT_CMD_NHEALTH] = "nvdimm_health",
+               [NDN_MSFT_CMD_EHEALTH] = "es_health",
+       };
+       const char *desc;
+
+       if (fn >= (int) ARRAY_SIZE(descs))
+               return "unknown";
+       desc = descs[fn];
+       if (!desc)
+               return "unknown";
+       return desc;
+}
+
+static bool msft_cmd_is_supported(struct ndctl_dimm *dimm, int cmd)
+{
+       /* Handle this separately to support monitor mode */
+       if (cmd == ND_CMD_SMART)
+               return true;
+
+       return !!(dimm->cmd_mask & (1ULL << cmd));
+}
+
  static u32 msft_get_firmware_status(struct ndctl_cmd *cmd)
  {
         return cmd->msft->u.smart.status;
  }
 
-static struct ndctl_cmd *msft_dimm_cmd_new_smart(struct ndctl_dimm *dimm)
+static struct ndctl_cmd *alloc_msft_cmd(struct ndctl_dimm *dimm,
+               unsigned int func, size_t in_size, size_t out_size)
  {
         struct ndctl_bus *bus = ndctl_dimm_get_bus(dimm);
         struct ndctl_ctx *ctx = ndctl_bus_get_ctx(bus);
@@ -30,12 +58,12 @@ static struct ndctl_cmd *msft_dimm_cmd_new_smart(struct ndctl_dimm *dimm)
                 return NULL;
         }
 
-       if (test_dimm_dsm(dimm, NDN_MSFT_CMD_SMART) == DIMM_DSM_UNSUPPORTED) {
+       if (test_dimm_dsm(dimm, func) == DIMM_DSM_UNSUPPORTED) {
                 dbg(ctx, "unsupported function\n");
                 return NULL;
         }
 
-       size = sizeof(*cmd) + sizeof(struct ndn_pkg_msft);
+       size = sizeof(*cmd) + sizeof(struct nd_cmd_pkg) + in_size + out_size;
         cmd = calloc(1, size);
         if (!cmd)
                 return NULL;
@@ -45,25 +73,30 @@ static struct ndctl_cmd *msft_dimm_cmd_new_smart(struct ndctl_dimm *dimm)
         cmd->type = ND_CMD_CALL;
         cmd->size = size;
         cmd->status = 1;
+       cmd->get_firmware_status = msft_get_firmware_status;
 
Quoted 13 lines

ndctl/lib/msft.c static struct ndctl_cmd *msft_dimm_cmd_new_smart(struct ndctl_dimm *dimm) · line 56

Quoted 11 lines
ndctl/lib/msft.c +3 -4
         msft = CMD_MSFT(cmd);
         msft->gen.nd_family = NVDIMM_FAMILY_MSFT;
-       msft->gen.nd_command = NDN_MSFT_CMD_SMART;
+       msft->gen.nd_command = func;
         msft->gen.nd_fw_size = 0;
-       msft->gen.nd_size_in = offsetof(struct ndn_msft_smart, status);
-       msft->gen.nd_size_out = sizeof(msft->u.smart);
+       msft->gen.nd_size_in = in_size;
+       msft->gen.nd_size_out = out_size;
         msft->u.smart.status = 0;
-       cmd->get_firmware_status = msft_get_firmware_status;

Moving this line up seems unnecessary?

Yes, it is unnecesary, but it looked cleaner and more contained to assign fields of each structure together. But once you say so, returned back.

Quoted 11 lines

ndctl/lib/msft.c static struct ndctl_cmd *msft_dimm_cmd_new_smart(struct ndctl_dimm *dimm) · line 92

Quoted 8 lines
ndctl/lib/msft.c +4 -0
         return cmd;
  }
 
+static struct ndctl_cmd *msft_dimm_cmd_new_smart(struct ndctl_dimm *dimm)
+{
+       return (alloc_msft_cmd(dimm, NDN_MSFT_CMD_NHEALTH,
+                       0, sizeof(struct ndn_msft_smart)));

The outermost parenthesis here are unnecessary and unconventional for the code base.

Thanks. Removed.

Quoted 2 lines

Also sizeof(*foo_ptr) is more canonical than sizeof(struct foo). Checkpatch should warn about this.

Yes, I would totally agree, would I have the variable here. Adding variable just for that purpose is IMO a code obfuscation.

Quoted 64 lines
Quoted 63 lines
ndctl/lib/msft.c +7 -5
+}
+
  static int msft_smart_valid(struct ndctl_cmd *cmd)
  {
         if (cmd->type != ND_CMD_CALL ||
-           cmd->size != sizeof(*cmd) + sizeof(struct ndn_pkg_msft) ||
             CMD_MSFT(cmd)->gen.nd_family != NVDIMM_FAMILY_MSFT ||
-           CMD_MSFT(cmd)->gen.nd_command != NDN_MSFT_CMD_SMART ||
+           CMD_MSFT(cmd)->gen.nd_command != NDN_MSFT_CMD_NHEALTH ||
             cmd->status != 0)
                 return cmd->status < 0 ? cmd->status : -EINVAL;
         return 0;
@@ -80,9 +113,8 @@ static unsigned int msft_cmd_smart_get_flags(struct ndctl_cmd *cmd)
         }
 
         /* below health data can be retrieved via MSFT _DSM function 11 */
-       return NDN_MSFT_SMART_HEALTH_VALID |
-               NDN_MSFT_SMART_TEMP_VALID |
-               NDN_MSFT_SMART_USED_VALID;
+       return ND_SMART_HEALTH_VALID | ND_SMART_TEMP_VALID |
+           ND_SMART_USED_VALID;
  }
 
  static unsigned int num_set_bit_health(__u16 num)
@@ -171,6 +203,8 @@ static int msft_cmd_xlat_firmware_status(struct ndctl_cmd *cmd)
  }
 
  struct ndctl_dimm_ops * const msft_dimm_ops = &(struct ndctl_dimm_ops) {
+       .cmd_desc = msft_cmd_desc,
+       .cmd_is_supported = msft_cmd_is_supported,
         .new_smart = msft_dimm_cmd_new_smart,
         .smart_get_flags = msft_cmd_smart_get_flags,
         .smart_get_health = msft_cmd_smart_get_health,
ndctl/lib/msft.h +4 -9
index 978cc11..8d246a5 100644
@@ -2,21 +2,16 @@
  /* Copyright (C) 2016-2017 Dell, Inc. */
  /* Copyright (C) 2016 Hewlett Packard Enterprise Development LP */
  /* Copyright (C) 2014-2020, Intel Corporation. */
+/* Copyright (C) 2022 iXsystems, Inc. */
  #ifndef __NDCTL_MSFT_H__
  #define __NDCTL_MSFT_H__
 
  enum {
-       NDN_MSFT_CMD_QUERY = 0,
-
-       /* non-root commands */
-       NDN_MSFT_CMD_SMART = 11,
+       NDN_MSFT_CMD_CHEALTH = 10,
+       NDN_MSFT_CMD_NHEALTH = 11,
+       NDN_MSFT_CMD_EHEALTH = 12,
  };
 
-/* NDN_MSFT_CMD_SMART */
-#define NDN_MSFT_SMART_HEALTH_VALID    ND_SMART_HEALTH_VALID
-#define NDN_MSFT_SMART_TEMP_VALID      ND_SMART_TEMP_VALID
-#define NDN_MSFT_SMART_USED_VALID      ND_SMART_USED_VALID
-
  /*
   * This is actually function 11 data,
   * This is the closest I can find to match smart
Signature · 1 line
Alexander Motin