diff mbox series

[ndctl,06/16] ndctl/dimm: Prepare to emit dimm json object after firmware update

Message ID 159408965016.2386154.1586833845040694311.stgit@dwillia2-desk3.amr.corp.intel.com (mailing list archive)
State Superseded
Headers show
Series Firmware Activation and Test Updates | expand

Commit Message

Dan Williams July 7, 2020, 2:40 a.m. UTC
Move util_dimm_firmware_to_json() internal to util_dimm_to_json().
Introduce a new UTIL_JSON_FIRMWARE flag to optionally dump firmware info
when listing the dimm from either 'ndctl list', or after 'ndctl
update-firmware'.

Move util_dimm_firmware_to_json() out of ndctl/util/json-firmware.c into
the core util/json.c.

Signed-off-by: Dan Williams <dan.j.williams@intel.com>
---
 ndctl/Makefile.am          |    1 -
 ndctl/list.c               |   10 +----
 ndctl/util/json-firmware.c |   85 --------------------------------------------
 util/json.c                |   84 +++++++++++++++++++++++++++++++++++++++++++
 util/json.h                |   17 +++++----
 5 files changed, 95 insertions(+), 102 deletions(-)
 delete mode 100644 ndctl/util/json-firmware.c
diff mbox series

Patch

diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index 49c6c4ab4498..a63b1e0b8a01 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -25,7 +25,6 @@  ndctl_SOURCES = ndctl.c \
 		../util/json.c \
 		../util/json.h \
 		util/json-smart.c \
-		util/json-firmware.c \
 		util/keys.h \
 		inject-error.c \
 		inject-smart.c \
diff --git a/ndctl/list.c b/ndctl/list.c
index 31fb1b9593a2..1f7cc8ee1deb 100644
--- a/ndctl/list.c
+++ b/ndctl/list.c
@@ -59,6 +59,8 @@  static unsigned long listopts_to_flags(void)
 		flags |= UTIL_JSON_VERBOSE;
 	if (list.capabilities)
 		flags |= UTIL_JSON_CAPABILITIES;
+	if (list.firmware)
+		flags |= UTIL_JSON_FIRMWARE;
 	return flags;
 }
 
@@ -367,14 +369,6 @@  static void filter_dimm(struct ndctl_dimm *dimm, struct util_filter_ctx *ctx)
 		}
 	}
 
-	if (list.firmware) {
-		struct json_object *jfirmware;
-
-		jfirmware = util_dimm_firmware_to_json(dimm, lfa->flags);
-		if (jfirmware)
-			json_object_object_add(jdimm, "firmware", jfirmware);
-	}
-
 	/*
 	 * Without a bus we are collecting dimms anonymously across the
 	 * platform.
diff --git a/ndctl/util/json-firmware.c b/ndctl/util/json-firmware.c
deleted file mode 100644
index 9a9db064d851..000000000000
--- a/ndctl/util/json-firmware.c
+++ /dev/null
@@ -1,85 +0,0 @@ 
-/* SPDX-License-Identifier: GPL-2.0 */
-/* Copyright(c) 2018 Intel Corporation. All rights reserved. */
-#include <limits.h>
-#include <util/json.h>
-#include <uuid/uuid.h>
-#include <json-c/json.h>
-#include <ndctl/libndctl.h>
-#include <ccan/array_size/array_size.h>
-#include <ndctl.h>
-
-struct json_object *util_dimm_firmware_to_json(struct ndctl_dimm *dimm,
-		unsigned long flags)
-{
-	struct json_object *jfirmware = json_object_new_object();
-	struct json_object *jobj;
-	struct ndctl_cmd *cmd;
-	int rc;
-	uint64_t run, next;
-
-	if (!jfirmware)
-		return NULL;
-
-	cmd = ndctl_dimm_cmd_new_fw_get_info(dimm);
-	if (!cmd)
-		goto err;
-
-	rc = ndctl_cmd_submit(cmd);
-	if ((rc < 0) || ndctl_cmd_fw_xlat_firmware_status(cmd) != FW_SUCCESS) {
-		jobj = util_json_object_hex(-1, flags);
-		if (jobj)
-			json_object_object_add(jfirmware, "current_version",
-					jobj);
-		goto out;
-	}
-
-	run = ndctl_cmd_fw_info_get_run_version(cmd);
-	if (run == ULLONG_MAX) {
-		jobj = util_json_object_hex(-1, flags);
-		if (jobj)
-			json_object_object_add(jfirmware, "current_version",
-					jobj);
-		goto out;
-	}
-
-	jobj = util_json_object_hex(run, flags);
-	if (jobj)
-		json_object_object_add(jfirmware, "current_version", jobj);
-
-	rc = ndctl_dimm_fw_update_supported(dimm);
-	jobj = json_object_new_boolean(rc == 0);
-	if (jobj)
-		json_object_object_add(jfirmware, "can_update", jobj);
-
-	next = ndctl_cmd_fw_info_get_updated_version(cmd);
-	if (next == ULLONG_MAX) {
-		jobj = util_json_object_hex(-1, flags);
-		if (jobj)
-			json_object_object_add(jfirmware, "next_version",
-					jobj);
-		goto out;
-	}
-
-	if (next != 0) {
-		jobj = util_json_object_hex(next, flags);
-		if (jobj)
-			json_object_object_add(jfirmware,
-					"next_version", jobj);
-
-		jobj = json_object_new_boolean(true);
-		if (jobj)
-			json_object_object_add(jfirmware,
-					"need_powercycle", jobj);
-	}
-
-	ndctl_cmd_unref(cmd);
-	return jfirmware;
-
-err:
-	json_object_put(jfirmware);
-	jfirmware = NULL;
-out:
-	if (cmd)
-		ndctl_cmd_unref(cmd);
-	return jfirmware;
-}
diff --git a/util/json.c b/util/json.c
index 21ab25674624..59a3d07cc4a6 100644
--- a/util/json.c
+++ b/util/json.c
@@ -156,6 +156,82 @@  struct json_object *util_bus_to_json(struct ndctl_bus *bus)
 	return NULL;
 }
 
+struct json_object *util_dimm_firmware_to_json(struct ndctl_dimm *dimm,
+		unsigned long flags)
+{
+	struct json_object *jfirmware = json_object_new_object();
+	struct json_object *jobj;
+	struct ndctl_cmd *cmd;
+	int rc;
+	uint64_t run, next;
+
+	if (!jfirmware)
+		return NULL;
+
+	cmd = ndctl_dimm_cmd_new_fw_get_info(dimm);
+	if (!cmd)
+		goto err;
+
+	rc = ndctl_cmd_submit(cmd);
+	if ((rc < 0) || ndctl_cmd_fw_xlat_firmware_status(cmd) != FW_SUCCESS) {
+		jobj = util_json_object_hex(-1, flags);
+		if (jobj)
+			json_object_object_add(jfirmware, "current_version",
+					jobj);
+		goto out;
+	}
+
+	run = ndctl_cmd_fw_info_get_run_version(cmd);
+	if (run == ULLONG_MAX) {
+		jobj = util_json_object_hex(-1, flags);
+		if (jobj)
+			json_object_object_add(jfirmware, "current_version",
+					jobj);
+		goto out;
+	}
+
+	jobj = util_json_object_hex(run, flags);
+	if (jobj)
+		json_object_object_add(jfirmware, "current_version", jobj);
+
+	rc = ndctl_dimm_fw_update_supported(dimm);
+	jobj = json_object_new_boolean(rc == 0);
+	if (jobj)
+		json_object_object_add(jfirmware, "can_update", jobj);
+
+	next = ndctl_cmd_fw_info_get_updated_version(cmd);
+	if (next == ULLONG_MAX) {
+		jobj = util_json_object_hex(-1, flags);
+		if (jobj)
+			json_object_object_add(jfirmware, "next_version",
+					jobj);
+		goto out;
+	}
+
+	if (next != 0) {
+		jobj = util_json_object_hex(next, flags);
+		if (jobj)
+			json_object_object_add(jfirmware,
+					"next_version", jobj);
+
+		jobj = json_object_new_boolean(true);
+		if (jobj)
+			json_object_object_add(jfirmware,
+					"need_powercycle", jobj);
+	}
+
+	ndctl_cmd_unref(cmd);
+	return jfirmware;
+
+err:
+	json_object_put(jfirmware);
+	jfirmware = NULL;
+out:
+	if (cmd)
+		ndctl_cmd_unref(cmd);
+	return jfirmware;
+}
+
 struct json_object *util_dimm_to_json(struct ndctl_dimm *dimm,
 		unsigned long flags)
 {
@@ -266,6 +342,14 @@  struct json_object *util_dimm_to_json(struct ndctl_dimm *dimm,
 			json_object_object_add(jdimm, "security_frozen", jobj);
 	}
 
+	if (flags & UTIL_JSON_FIRMWARE) {
+		struct json_object *jfirmware;
+
+		jfirmware = util_dimm_firmware_to_json(dimm, flags);
+		if (jfirmware)
+			json_object_object_add(jdimm, "firmware", jfirmware);
+	}
+
 	return jdimm;
  err:
 	json_object_put(jdimm);
diff --git a/util/json.h b/util/json.h
index 6d39d3aa4693..fc91a8db034f 100644
--- a/util/json.h
+++ b/util/json.h
@@ -18,14 +18,15 @@ 
 #include <ccan/short_types/short_types.h>
 
 enum util_json_flags {
-	UTIL_JSON_IDLE = (1 << 0),
-	UTIL_JSON_MEDIA_ERRORS = (1 << 1),
-	UTIL_JSON_DAX = (1 << 2),
-	UTIL_JSON_DAX_DEVS = (1 << 3),
-	UTIL_JSON_HUMAN = (1 << 4),
-	UTIL_JSON_VERBOSE = (1 << 5),
-	UTIL_JSON_CAPABILITIES = (1 << 6),
-	UTIL_JSON_CONFIGURED = (1 << 7),
+	UTIL_JSON_IDLE		= (1 << 0),
+	UTIL_JSON_MEDIA_ERRORS	= (1 << 1),
+	UTIL_JSON_DAX		= (1 << 2),
+	UTIL_JSON_DAX_DEVS	= (1 << 3),
+	UTIL_JSON_HUMAN		= (1 << 4),
+	UTIL_JSON_VERBOSE	= (1 << 5),
+	UTIL_JSON_CAPABILITIES	= (1 << 6),
+	UTIL_JSON_CONFIGURED	= (1 << 7),
+	UTIL_JSON_FIRMWARE	= (1 << 8),
 };
 
 struct json_object;