anitazha / rpms / ndctl

Forked from rpms/ndctl a year ago
Clone

Blame SOURCES/0046-cxl-add-health-information-to-cxl-list.patch

e0018b
From f5d1e2133c54c1f420a0c3cf45fa633f097823be Mon Sep 17 00:00:00 2001
e0018b
From: Vishal Verma <vishal.l.verma@intel.com>
e0018b
Date: Thu, 7 Oct 2021 02:21:39 -0600
e0018b
Subject: [PATCH 046/217] cxl: add health information to cxl-list
e0018b
e0018b
Add JSON output for fields from the 'GET_HEALTH_INFO' mailbox command
e0018b
to memory device listings.
e0018b
e0018b
Cc: Dan Williams <dan.j.williams@intel.com>
e0018b
Reviewed-by: Dan Williams <dan.j.williams@intel.com>
e0018b
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
e0018b
---
e0018b
 Documentation/cxl/cxl-list.txt |  38 +++++++
e0018b
 cxl/list.c                     |   5 +
e0018b
 util/json.c                    | 179 +++++++++++++++++++++++++++++++++
e0018b
 util/json.h                    |   1 +
e0018b
 4 files changed, 223 insertions(+)
e0018b
e0018b
diff --git a/Documentation/cxl/cxl-list.txt b/Documentation/cxl/cxl-list.txt
e0018b
index 370d5b8..c8d10fb 100644
e0018b
--- a/Documentation/cxl/cxl-list.txt
e0018b
+++ b/Documentation/cxl/cxl-list.txt
e0018b
@@ -48,6 +48,44 @@ OPTIONS
e0018b
 --idle::
e0018b
 	Include idle (not enabled / zero-sized) devices in the listing
e0018b
 
e0018b
+-H::
e0018b
+--health::
e0018b
+	Include health information in the memdev listing. Example listing:
e0018b
+----
e0018b
+# cxl list -m mem0 -H
e0018b
+[
e0018b
+  {
e0018b
+    "memdev":"mem0",
e0018b
+    "pmem_size":268435456,
e0018b
+    "ram_size":268435456,
e0018b
+    "health":{
e0018b
+      "maintenance_needed":true,
e0018b
+      "performance_degraded":true,
e0018b
+      "hw_replacement_needed":true,
e0018b
+      "media_normal":false,
e0018b
+      "media_not_ready":false,
e0018b
+      "media_persistence_lost":false,
e0018b
+      "media_data_lost":true,
e0018b
+      "media_powerloss_persistence_loss":false,
e0018b
+      "media_shutdown_persistence_loss":false,
e0018b
+      "media_persistence_loss_imminent":false,
e0018b
+      "media_powerloss_data_loss":false,
e0018b
+      "media_shutdown_data_loss":false,
e0018b
+      "media_data_loss_imminent":false,
e0018b
+      "ext_life_used":"normal",
e0018b
+      "ext_temperature":"critical",
e0018b
+      "ext_corrected_volatile":"warning",
e0018b
+      "ext_corrected_persistent":"normal",
e0018b
+      "life_used_percent":15,
e0018b
+      "temperature":25,
e0018b
+      "dirty_shutdowns":10,
e0018b
+      "volatile_errors":20,
e0018b
+      "pmem_errors":30
e0018b
+    }
e0018b
+  }
e0018b
+]
e0018b
+----
e0018b
+
e0018b
 include::human-option.txt[]
e0018b
 
e0018b
 include::verbose-option.txt[]
e0018b
diff --git a/cxl/list.c b/cxl/list.c
e0018b
index 043d20c..b1468b7 100644
e0018b
--- a/cxl/list.c
e0018b
+++ b/cxl/list.c
e0018b
@@ -16,6 +16,7 @@ static struct {
e0018b
 	bool memdevs;
e0018b
 	bool idle;
e0018b
 	bool human;
e0018b
+	bool health;
e0018b
 } list;
e0018b
 
e0018b
 static unsigned long listopts_to_flags(void)
e0018b
@@ -26,6 +27,8 @@ static unsigned long listopts_to_flags(void)
e0018b
 		flags |= UTIL_JSON_IDLE;
e0018b
 	if (list.human)
e0018b
 		flags |= UTIL_JSON_HUMAN;
e0018b
+	if (list.health)
e0018b
+		flags |= UTIL_JSON_HEALTH;
e0018b
 	return flags;
e0018b
 }
e0018b
 
e0018b
@@ -57,6 +60,8 @@ int cmd_list(int argc, const char **argv, struct cxl_ctx *ctx)
e0018b
 		OPT_BOOLEAN('i', "idle", &list.idle, "include idle devices"),
e0018b
 		OPT_BOOLEAN('u', "human", &list.human,
e0018b
 				"use human friendly number formats "),
e0018b
+		OPT_BOOLEAN('H', "health", &list.health,
e0018b
+				"include memory device health information "),
e0018b
 		OPT_END(),
e0018b
 	};
e0018b
 	const char * const u[] = {
e0018b
diff --git a/util/json.c b/util/json.c
e0018b
index 3be3a92..f97cf07 100644
e0018b
--- a/util/json.c
e0018b
+++ b/util/json.c
e0018b
@@ -1442,6 +1442,180 @@ struct json_object *util_badblock_rec_to_json(u64 block, u64 count,
e0018b
 	return NULL;
e0018b
 }
e0018b
 
e0018b
+static struct json_object *util_cxl_memdev_health_to_json(
e0018b
+		struct cxl_memdev *memdev, unsigned long flags)
e0018b
+{
e0018b
+	struct json_object *jhealth;
e0018b
+	struct json_object *jobj;
e0018b
+	struct cxl_cmd *cmd;
e0018b
+	u32 field;
e0018b
+	int rc;
e0018b
+
e0018b
+	jhealth = json_object_new_object();
e0018b
+	if (!jhealth)
e0018b
+		return NULL;
e0018b
+	if (!memdev)
e0018b
+		goto err_jobj;
e0018b
+
e0018b
+	cmd = cxl_cmd_new_get_health_info(memdev);
e0018b
+	if (!cmd)
e0018b
+		goto err_jobj;
e0018b
+
e0018b
+	rc = cxl_cmd_submit(cmd);
e0018b
+	if (rc < 0)
e0018b
+		goto err_cmd;
e0018b
+	rc = cxl_cmd_get_mbox_status(cmd);
e0018b
+	if (rc != 0)
e0018b
+		goto err_cmd;
e0018b
+
e0018b
+	/* health_status fields */
e0018b
+	rc = cxl_cmd_health_info_get_maintenance_needed(cmd);
e0018b
+	jobj = json_object_new_boolean(rc);
e0018b
+	if (jobj)
e0018b
+		json_object_object_add(jhealth, "maintenance_needed", jobj);
e0018b
+
e0018b
+	rc = cxl_cmd_health_info_get_performance_degraded(cmd);
e0018b
+	jobj = json_object_new_boolean(rc);
e0018b
+	if (jobj)
e0018b
+		json_object_object_add(jhealth, "performance_degraded", jobj);
e0018b
+
e0018b
+	rc = cxl_cmd_health_info_get_hw_replacement_needed(cmd);
e0018b
+	jobj = json_object_new_boolean(rc);
e0018b
+	if (jobj)
e0018b
+		json_object_object_add(jhealth, "hw_replacement_needed", jobj);
e0018b
+
e0018b
+	/* media_status fields */
e0018b
+	rc = cxl_cmd_health_info_get_media_normal(cmd);
e0018b
+	jobj = json_object_new_boolean(rc);
e0018b
+	if (jobj)
e0018b
+		json_object_object_add(jhealth, "media_normal", jobj);
e0018b
+
e0018b
+	rc = cxl_cmd_health_info_get_media_not_ready(cmd);
e0018b
+	jobj = json_object_new_boolean(rc);
e0018b
+	if (jobj)
e0018b
+		json_object_object_add(jhealth, "media_not_ready", jobj);
e0018b
+
e0018b
+	rc = cxl_cmd_health_info_get_media_persistence_lost(cmd);
e0018b
+	jobj = json_object_new_boolean(rc);
e0018b
+	if (jobj)
e0018b
+		json_object_object_add(jhealth, "media_persistence_lost", jobj);
e0018b
+
e0018b
+	rc = cxl_cmd_health_info_get_media_data_lost(cmd);
e0018b
+	jobj = json_object_new_boolean(rc);
e0018b
+	if (jobj)
e0018b
+		json_object_object_add(jhealth, "media_data_lost", jobj);
e0018b
+
e0018b
+	rc = cxl_cmd_health_info_get_media_powerloss_persistence_loss(cmd);
e0018b
+	jobj = json_object_new_boolean(rc);
e0018b
+	if (jobj)
e0018b
+		json_object_object_add(jhealth, "media_powerloss_persistence_loss", jobj);
e0018b
+
e0018b
+	rc = cxl_cmd_health_info_get_media_shutdown_persistence_loss(cmd);
e0018b
+	jobj = json_object_new_boolean(rc);
e0018b
+	if (jobj)
e0018b
+		json_object_object_add(jhealth, "media_shutdown_persistence_loss", jobj);
e0018b
+
e0018b
+	rc = cxl_cmd_health_info_get_media_persistence_loss_imminent(cmd);
e0018b
+	jobj = json_object_new_boolean(rc);
e0018b
+	if (jobj)
e0018b
+		json_object_object_add(jhealth, "media_persistence_loss_imminent", jobj);
e0018b
+
e0018b
+	rc = cxl_cmd_health_info_get_media_powerloss_data_loss(cmd);
e0018b
+	jobj = json_object_new_boolean(rc);
e0018b
+	if (jobj)
e0018b
+		json_object_object_add(jhealth, "media_powerloss_data_loss", jobj);
e0018b
+
e0018b
+	rc = cxl_cmd_health_info_get_media_shutdown_data_loss(cmd);
e0018b
+	jobj = json_object_new_boolean(rc);
e0018b
+	if (jobj)
e0018b
+		json_object_object_add(jhealth, "media_shutdown_data_loss", jobj);
e0018b
+
e0018b
+	rc = cxl_cmd_health_info_get_media_data_loss_imminent(cmd);
e0018b
+	jobj = json_object_new_boolean(rc);
e0018b
+	if (jobj)
e0018b
+		json_object_object_add(jhealth, "media_data_loss_imminent", jobj);
e0018b
+
e0018b
+	/* ext_status fields */
e0018b
+	if (cxl_cmd_health_info_get_ext_life_used_normal(cmd))
e0018b
+		jobj = json_object_new_string("normal");
e0018b
+	else if (cxl_cmd_health_info_get_ext_life_used_warning(cmd))
e0018b
+		jobj = json_object_new_string("warning");
e0018b
+	else if (cxl_cmd_health_info_get_ext_life_used_critical(cmd))
e0018b
+		jobj = json_object_new_string("critical");
e0018b
+	else
e0018b
+		jobj = json_object_new_string("unknown");
e0018b
+	if (jobj)
e0018b
+		json_object_object_add(jhealth, "ext_life_used", jobj);
e0018b
+
e0018b
+	if (cxl_cmd_health_info_get_ext_temperature_normal(cmd))
e0018b
+		jobj = json_object_new_string("normal");
e0018b
+	else if (cxl_cmd_health_info_get_ext_temperature_warning(cmd))
e0018b
+		jobj = json_object_new_string("warning");
e0018b
+	else if (cxl_cmd_health_info_get_ext_temperature_critical(cmd))
e0018b
+		jobj = json_object_new_string("critical");
e0018b
+	else
e0018b
+		jobj = json_object_new_string("unknown");
e0018b
+	if (jobj)
e0018b
+		json_object_object_add(jhealth, "ext_temperature", jobj);
e0018b
+
e0018b
+	if (cxl_cmd_health_info_get_ext_corrected_volatile_normal(cmd))
e0018b
+		jobj = json_object_new_string("normal");
e0018b
+	else if (cxl_cmd_health_info_get_ext_corrected_volatile_warning(cmd))
e0018b
+		jobj = json_object_new_string("warning");
e0018b
+	else
e0018b
+		jobj = json_object_new_string("unknown");
e0018b
+	if (jobj)
e0018b
+		json_object_object_add(jhealth, "ext_corrected_volatile", jobj);
e0018b
+
e0018b
+	if (cxl_cmd_health_info_get_ext_corrected_persistent_normal(cmd))
e0018b
+		jobj = json_object_new_string("normal");
e0018b
+	else if (cxl_cmd_health_info_get_ext_corrected_persistent_warning(cmd))
e0018b
+		jobj = json_object_new_string("warning");
e0018b
+	else
e0018b
+		jobj = json_object_new_string("unknown");
e0018b
+	if (jobj)
e0018b
+		json_object_object_add(jhealth, "ext_corrected_persistent", jobj);
e0018b
+
e0018b
+	/* other fields */
e0018b
+	field = cxl_cmd_health_info_get_life_used(cmd);
e0018b
+	if (field != 0xff) {
e0018b
+		jobj = json_object_new_int(field);
e0018b
+		if (jobj)
e0018b
+			json_object_object_add(jhealth, "life_used_percent", jobj);
e0018b
+	}
e0018b
+
e0018b
+	field = cxl_cmd_health_info_get_temperature(cmd);
e0018b
+	if (field != 0xffff) {
e0018b
+		jobj = json_object_new_int(field);
e0018b
+		if (jobj)
e0018b
+			json_object_object_add(jhealth, "temperature", jobj);
e0018b
+	}
e0018b
+
e0018b
+	field = cxl_cmd_health_info_get_dirty_shutdowns(cmd);
e0018b
+	jobj = json_object_new_int64(field);
e0018b
+	if (jobj)
e0018b
+		json_object_object_add(jhealth, "dirty_shutdowns", jobj);
e0018b
+
e0018b
+	field = cxl_cmd_health_info_get_volatile_errors(cmd);
e0018b
+	jobj = json_object_new_int64(field);
e0018b
+	if (jobj)
e0018b
+		json_object_object_add(jhealth, "volatile_errors", jobj);
e0018b
+
e0018b
+	field = cxl_cmd_health_info_get_pmem_errors(cmd);
e0018b
+	jobj = json_object_new_int64(field);
e0018b
+	if (jobj)
e0018b
+		json_object_object_add(jhealth, "pmem_errors", jobj);
e0018b
+
e0018b
+	cxl_cmd_unref(cmd);
e0018b
+	return jhealth;
e0018b
+
e0018b
+err_cmd:
e0018b
+	cxl_cmd_unref(cmd);
e0018b
+err_jobj:
e0018b
+	json_object_put(jhealth);
e0018b
+	return NULL;
e0018b
+}
e0018b
+
e0018b
 struct json_object *util_cxl_memdev_to_json(struct cxl_memdev *memdev,
e0018b
 		unsigned long flags)
e0018b
 {
e0018b
@@ -1464,5 +1638,10 @@ struct json_object *util_cxl_memdev_to_json(struct cxl_memdev *memdev,
e0018b
 	if (jobj)
e0018b
 		json_object_object_add(jdev, "ram_size", jobj);
e0018b
 
e0018b
+	if (flags & UTIL_JSON_HEALTH) {
e0018b
+		jobj = util_cxl_memdev_health_to_json(memdev, flags);
e0018b
+		if (jobj)
e0018b
+			json_object_object_add(jdev, "health", jobj);
e0018b
+	}
e0018b
 	return jdev;
e0018b
 }
e0018b
diff --git a/util/json.h b/util/json.h
e0018b
index 91918c8..ce575e6 100644
e0018b
--- a/util/json.h
e0018b
+++ b/util/json.h
e0018b
@@ -19,6 +19,7 @@ enum util_json_flags {
e0018b
 	UTIL_JSON_CONFIGURED	= (1 << 7),
e0018b
 	UTIL_JSON_FIRMWARE	= (1 << 8),
e0018b
 	UTIL_JSON_DAX_MAPPINGS	= (1 << 9),
e0018b
+	UTIL_JSON_HEALTH	= (1 << 10),
e0018b
 };
e0018b
 
e0018b
 struct json_object;
e0018b
-- 
e0018b
2.27.0
e0018b