Add a new macro function to retrieve a signed value such as a temperature.
Modify accessors for signed value to return INT_MAX when error occurs and
set errno to corresponding errno codes.
Fix the error checking value of the temperature accessor in cxl/json.c.

Signed-off-by: Jehoon Park <[email protected]>
---
 cxl/json.c       |  2 +-
 cxl/lib/libcxl.c | 30 +++++++++++++++++++++---------
 2 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/cxl/json.c b/cxl/json.c
index 7678d02..89e5fd0 100644
--- a/cxl/json.c
+++ b/cxl/json.c
@@ -246,7 +246,7 @@ static struct json_object *util_cxl_memdev_health_to_json(
        }
 
        field = cxl_cmd_health_info_get_temperature(cmd);
-       if (field != 0xffff) {
+       if (field != INT_MAX) {
                jobj = json_object_new_int(field);
                if (jobj)
                        json_object_object_add(jhealth, "temperature", jobj);
diff --git a/cxl/lib/libcxl.c b/cxl/lib/libcxl.c
index af4ca44..53c9b25 100644
--- a/cxl/lib/libcxl.c
+++ b/cxl/lib/libcxl.c
@@ -3661,11 +3661,23 @@ 
cxl_cmd_alert_config_get_life_used_prog_warn_threshold(struct cxl_cmd *cmd)
                         life_used_prog_warn_threshold);
 }
 
+#define cmd_get_field_s16(cmd, n, N, field)                            \
+do {                                                                   \
+       struct cxl_cmd_##n *c =                                         \
+               (struct cxl_cmd_##n *)cmd->send_cmd->out.payload;       \
+       int rc = cxl_cmd_validate_status(cmd, CXL_MEM_COMMAND_ID_##N);  \
+       if (rc) {                                                       \
+               errno = -rc;                                            \
+               return INT_MAX;                                         \
+       }                                                               \
+       return (int16_t)le16_to_cpu(c->field);                          \
+} while(0)
+
 CXL_EXPORT int
 cxl_cmd_alert_config_get_dev_over_temperature_crit_alert_threshold(
        struct cxl_cmd *cmd)
 {
-       cmd_get_field_u16(cmd, get_alert_config, GET_ALERT_CONFIG,
+       cmd_get_field_s16(cmd, get_alert_config, GET_ALERT_CONFIG,
                          dev_over_temperature_crit_alert_threshold);
 }
 
@@ -3673,7 +3685,7 @@ CXL_EXPORT int
 cxl_cmd_alert_config_get_dev_under_temperature_crit_alert_threshold(
        struct cxl_cmd *cmd)
 {
-       cmd_get_field_u16(cmd, get_alert_config, GET_ALERT_CONFIG,
+       cmd_get_field_s16(cmd, get_alert_config, GET_ALERT_CONFIG,
                          dev_under_temperature_crit_alert_threshold);
 }
 
@@ -3681,7 +3693,7 @@ CXL_EXPORT int
 cxl_cmd_alert_config_get_dev_over_temperature_prog_warn_threshold(
        struct cxl_cmd *cmd)
 {
-       cmd_get_field_u16(cmd, get_alert_config, GET_ALERT_CONFIG,
+       cmd_get_field_s16(cmd, get_alert_config, GET_ALERT_CONFIG,
                          dev_over_temperature_prog_warn_threshold);
 }
 
@@ -3689,7 +3701,7 @@ CXL_EXPORT int
 cxl_cmd_alert_config_get_dev_under_temperature_prog_warn_threshold(
        struct cxl_cmd *cmd)
 {
-       cmd_get_field_u16(cmd, get_alert_config, GET_ALERT_CONFIG,
+       cmd_get_field_s16(cmd, get_alert_config, GET_ALERT_CONFIG,
                          dev_under_temperature_prog_warn_threshold);
 }
 
@@ -3914,7 +3926,7 @@ CXL_EXPORT int cxl_cmd_health_info_get_life_used(struct 
cxl_cmd *cmd)
 
 static int health_info_get_temperature_raw(struct cxl_cmd *cmd)
 {
-       cmd_get_field_u16(cmd, get_health_info, GET_HEALTH_INFO,
+       cmd_get_field_s16(cmd, get_health_info, GET_HEALTH_INFO,
                                 temperature);
 }
 
@@ -3922,10 +3934,10 @@ CXL_EXPORT int 
cxl_cmd_health_info_get_temperature(struct cxl_cmd *cmd)
 {
        int rc = health_info_get_temperature_raw(cmd);
 
-       if (rc < 0)
-               return rc;
-       if (rc == CXL_CMD_HEALTH_INFO_TEMPERATURE_NOT_IMPL)
-               return -EOPNOTSUPP;
+       if (rc == CXL_CMD_HEALTH_INFO_TEMPERATURE_NOT_IMPL) {
+               errno = EOPNOTSUPP;
+               return INT_MAX;
+       }
        return rc;
 }
 
-- 
2.17.1


Reply via email to