[Accel-config] [PATCH 1/4] accel-config: Add "enable" option to load-config command

Ramesh Thomas <ramesh.thomas at intel.com> Tue, 07 Dec 2021 19:51:49 -0500
Newsgroups dev.linux.lists.accel-config
Message-ID <[email protected]>
Add an option to enable devices and wqs while configuring devices using
the load-config command. This is a convenience feature so user does not
need to manually enable the newly configured devices and wqs. The option
is -e, enable=true

Signed-off-by: Ramesh Thomas <ramesh.thomas(a)intel.com>
---
 accfg/config.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)

diff --git a/accfg/config.c b/accfg/config.c
index b0ab701..91a71e3 100644
--- a/accfg/config.c
+++ b/accfg/config.c
@@ -21,8 +21,16 @@
 #include <accfg.h>
 
 static bool verbose;
+static bool enable;
 static struct util_filter_params util_param;
 
+static LIST_HEAD(activate_dev_list);
+static LIST_HEAD(activate_wq_list);
+struct activate_dev {
+	void *dev;
+	struct list_node list;
+};
+
 static struct config {
 	bool devices;
 	bool groups;
@@ -417,6 +425,57 @@ static int engine_json_set_val(struct accfg_engine *engine,
 	return -ENOENT;
 }
 
+/*
+ * Add configured devices and wqs to activation list
+ */
+static int add_to_activation_list(struct list_head *activate_list, void *dev)
+{
+	struct activate_dev *act_dev;
+
+	act_dev = calloc(1, sizeof(struct activate_dev));
+	if (!act_dev) {
+		fprintf(stderr, "Error allocating memory for activation list\n");
+		return -ENOMEM;
+	}
+	act_dev->dev = dev;
+	list_add(activate_list, &act_dev->list);
+
+	return 0;
+}
+
+/*
+ * Enable devices in activation list
+ */
+static int activate_devices(void)
+{
+	struct activate_dev *iter, *next;
+	int rc;
+
+	list_for_each_safe(&activate_dev_list, iter, next, list) {
+		printf("Enabling device %s\n",
+				accfg_device_get_devname(iter->dev));
+		rc = accfg_device_enable(iter->dev);
+		if (rc) {
+			fprintf(stderr, "Error enabling device\n");
+			return rc;
+		}
+
+		free(iter);
+	}
+
+	list_for_each_safe(&activate_wq_list, iter, next, list) {
+		printf("Enabling wq %s\n", accfg_wq_get_devname(iter->dev));
+		rc = accfg_wq_enable(iter->dev);
+		if (rc) {
+			fprintf(stderr, "Error enabling wq\n");
+			return rc;
+		}
+		free(iter);
+	}
+
+	return 0;
+}
+
 /*
  * Configuring the value corresponding to integer and strings
  */
@@ -471,6 +530,12 @@ static int configure_json_value(struct accfg_ctx *ctx,
 				wq = NULL;
 				engine = NULL;
 				group = NULL;
+
+				if (enable) {
+					rc = add_to_activation_list(&activate_dev_list, dev);
+					if (rc)
+						return rc;
+				}
 				break;
 			}
 			accel_type = accfg_basenames[i];
@@ -500,6 +565,12 @@ static int configure_json_value(struct accfg_ctx *ctx,
 			dev = NULL;
 			engine = NULL;
 			group = NULL;
+
+			if (enable) {
+				rc = add_to_activation_list(&activate_wq_list, wq);
+				if (rc)
+					return rc;
+			}
 		}
 
 		if (strstr(parsed_string, "engine") != NULL) {
@@ -1004,6 +1075,8 @@ int cmd_config(int argc, const char **argv, void *ctx)
 			     "override the default config"),
 		OPT_BOOLEAN('v', "verbose", &verbose,
 				"emit extra debug messages to stderr"),
+		OPT_BOOLEAN('e', "enable", &enable,
+				"enable configured devices and wqs"),
 		OPT_END(),
 	};
 	const char *const u[] = {
@@ -1049,5 +1122,9 @@ int cmd_config(int argc, const char **argv, void *ctx)
 		fprintf(stderr, "Parse json and set device fail: %d\n", rc);
 
 	free_containers(&cfa);
+
+	if (enable && !rc)
+		rc = activate_devices();
+
 	return rc;
 }
-- 
2.26.3