From 46321d786961ea5d5a63b12084ee2ed551067c3d Mon Sep 17 00:00:00 2001
From: Weili Qian <qianweili@huawei.com>
Date: Tue, 31 May 2022 17:01:01 +0800
Subject: [PATCH 127/183] uadk: move parameter check into wd_util.c

The code for wd alg init parameter checking is same,
so move it into wd_util.c from wd_<alg>.c.

Signed-off-by: Weili Qian <qianweili@huawei.com>
---
 include/wd_util.h |  9 +++++++++
 wd_aead.c         | 17 +----------------
 wd_cipher.c       | 18 +-----------------
 wd_comp.c         | 12 +++---------
 wd_dh.c           | 20 +++-----------------
 wd_digest.c       | 17 +----------------
 wd_ecc.c          | 20 +++-----------------
 wd_rsa.c          | 20 +++-----------------
 wd_util.c         | 20 ++++++++++++++++++++
 9 files changed, 44 insertions(+), 109 deletions(-)

diff --git a/include/wd_util.h b/include/wd_util.h
index 1243428..89dfe87 100644
--- a/include/wd_util.h
+++ b/include/wd_util.h
@@ -327,6 +327,15 @@ int wd_check_ctx(struct wd_ctx_config_internal *config, __u8 mode, __u32 idx);
  */
 int wd_set_epoll_en(const char *var_name, bool *epoll_en);
 
+/**
+ * wd_init_check() - Check input parameters for wd_<alg>_init.
+ * @config: Ctx configuration input by user.
+ * @sched: Scheduler configuration input by user.
+ *
+ * Return 0 if successful or less than 0 otherwise.
+ */
+int wd_init_param_check(struct wd_ctx_config *config, struct wd_sched *sched);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/wd_aead.c b/wd_aead.c
index b3e7b41..3f47f8b 100644
--- a/wd_aead.c
+++ b/wd_aead.c
@@ -388,27 +388,12 @@ static int aead_param_check(struct wd_aead_sess *sess,
 	return 0;
 }
 
-static int aead_init_check(struct wd_ctx_config *config, struct wd_sched *sched)
-{
-	if (!config || !config->ctxs || !config->ctxs[0].ctx || !sched) {
-		WD_ERR("invalid: wd aead config or sched is NULL!\n");
-		return -WD_EINVAL;
-	}
-
-	if (!wd_is_sva(config->ctxs[0].ctx)) {
-		WD_ERR("err, non sva, please check system!\n");
-		return -WD_EINVAL;
-	}
-
-	return 0;
-}
-
 int wd_aead_init(struct wd_ctx_config *config, struct wd_sched *sched)
 {
 	void *priv;
 	int ret;
 
-	ret = aead_init_check(config, sched);
+	ret = wd_init_param_check(config, sched);
 	if (ret)
 		return ret;
 
diff --git a/wd_cipher.c b/wd_cipher.c
index b074dd2..be8996f 100644
--- a/wd_cipher.c
+++ b/wd_cipher.c
@@ -151,22 +151,6 @@ static int cipher_key_len_check(struct wd_cipher_sess *sess, __u32 length)
 	return ret;
 }
 
-static int cipher_init_check(struct wd_ctx_config *config,
-			     struct wd_sched *sched)
-{
-	if (!config || !config->ctxs || !config->ctxs[0].ctx || !sched) {
-		WD_ERR("invalid: wd cipher config or sched is NULL!\n");
-		return -WD_EINVAL;
-	}
-
-	if (!wd_is_sva(config->ctxs[0].ctx)) {
-		WD_ERR("err, non sva, please check system!\n");
-		return -WD_EINVAL;
-	}
-
-	return 0;
-}
-
 int wd_cipher_set_key(handle_t h_sess, const __u8 *key, __u32 key_len)
 {
 	struct wd_cipher_sess *sess = (struct wd_cipher_sess *)h_sess;
@@ -248,7 +232,7 @@ int wd_cipher_init(struct wd_ctx_config *config, struct wd_sched *sched)
 	void *priv;
 	int ret;
 
-	ret = cipher_init_check(config, sched);
+	ret = wd_init_param_check(config, sched);
 	if (ret)
 		return ret;
 
diff --git a/wd_comp.c b/wd_comp.c
index 502bc23..f2d01b6 100644
--- a/wd_comp.c
+++ b/wd_comp.c
@@ -85,15 +85,9 @@ int wd_comp_init(struct wd_ctx_config *config, struct wd_sched *sched)
 	void *priv;
 	int ret;
 
-	if (!config || !config->ctxs || !config->ctxs[0].ctx || !sched) {
-		WD_ERR("invalid: config or sched is NULL!\n");
-		return -WD_EINVAL;
-	}
-
-	if (!wd_is_sva(config->ctxs[0].ctx)) {
-		WD_ERR("failed to find sva device, please check system!\n");
-		return -WD_EINVAL;
-	}
+	ret = wd_init_param_check(config, sched);
+	if (ret)
+		return ret;
 
 	ret = wd_set_epoll_en("WD_COMP_EPOLL_EN",
 			      &wd_comp_setting.config.epoll_en);
diff --git a/wd_dh.c b/wd_dh.c
index 2b02e3c..dfa9505 100644
--- a/wd_dh.c
+++ b/wd_dh.c
@@ -78,28 +78,14 @@ void wd_dh_set_driver(struct wd_dh_driver *drv)
 	wd_dh_setting.driver = drv;
 }
 
-static int param_check(struct wd_ctx_config *config, struct wd_sched *sched)
-{
-	if (!config || !config->ctxs || !config->ctxs[0].ctx || !sched) {
-		WD_ERR("invalid: config or sched is NULL!\n");
-		return -WD_EINVAL;
-	}
-
-	if (!wd_is_sva(config->ctxs[0].ctx)) {
-		WD_ERR("invalid: the mode is non sva, please check system!\n");
-		return -WD_EINVAL;
-	}
-
-	return 0;
-}
-
 int wd_dh_init(struct wd_ctx_config *config, struct wd_sched *sched)
 {
 	void *priv;
 	int ret;
 
-	if (param_check(config, sched))
-		return -WD_EINVAL;
+	ret = wd_init_param_check(config, sched);
+	if (ret)
+		return ret;
 
 	ret = wd_set_epoll_en("WD_DH_EPOLL_EN",
 			      &wd_dh_setting.config.epoll_en);
diff --git a/wd_digest.c b/wd_digest.c
index 1d4e4cc..46546cb 100644
--- a/wd_digest.c
+++ b/wd_digest.c
@@ -150,27 +150,12 @@ void wd_digest_free_sess(handle_t h_sess)
 	free(sess);
 }
 
-static int digest_init_check(struct wd_ctx_config *config, struct wd_sched *sched)
-{
-	if (!config || !config->ctxs || !config->ctxs[0].ctx || !sched) {
-		WD_ERR("failed to check input param!\n");
-		return -WD_EINVAL;
-	}
-
-	if (!wd_is_sva(config->ctxs[0].ctx)) {
-		WD_ERR("err, non sva, please check system!\n");
-		return -WD_EINVAL;
-	}
-
-	return 0;
-}
-
 int wd_digest_init(struct wd_ctx_config *config, struct wd_sched *sched)
 {
 	void *priv;
 	int ret;
 
-	ret = digest_init_check(config, sched);
+	ret = wd_init_param_check(config, sched);
 	if (ret)
 		return ret;
 
diff --git a/wd_ecc.c b/wd_ecc.c
index c6bd111..c463049 100644
--- a/wd_ecc.c
+++ b/wd_ecc.c
@@ -132,28 +132,14 @@ void wd_ecc_set_driver(struct wd_ecc_driver *drv)
 	wd_ecc_setting.driver = drv;
 }
 
-static int init_param_check(struct wd_ctx_config *config, struct wd_sched *sched)
-{
-	if (!config || !config->ctxs || !config->ctxs[0].ctx || !sched) {
-		WD_ERR("invalid: config or sched is NULL!\n");
-		return -WD_EINVAL;
-	}
-
-	if (!wd_is_sva(config->ctxs[0].ctx)) {
-		WD_ERR("invalid: the mode is non sva, please check system!\n");
-		return -WD_EINVAL;
-	}
-
-	return 0;
-}
-
 int wd_ecc_init(struct wd_ctx_config *config, struct wd_sched *sched)
 {
 	void *priv;
 	int ret;
 
-	if (init_param_check(config, sched))
-		return -WD_EINVAL;
+	ret = wd_init_param_check(config, sched);
+	if (ret)
+		return ret;
 
 	ret = wd_set_epoll_en("WD_ECC_EPOLL_EN",
 			      &wd_ecc_setting.config.epoll_en);
diff --git a/wd_rsa.c b/wd_rsa.c
index b7f250e..836c8ae 100644
--- a/wd_rsa.c
+++ b/wd_rsa.c
@@ -118,28 +118,14 @@ void wd_rsa_set_driver(struct wd_rsa_driver *drv)
 	wd_rsa_setting.driver = drv;
 }
 
-static int param_check(struct wd_ctx_config *config, struct wd_sched *sched)
-{
-	if (!config || !config->ctxs[0].ctx || !sched) {
-		WD_ERR("invalid: config or sched NULL!\n");
-		return -WD_EINVAL;
-	}
-
-	if (!wd_is_sva(config->ctxs[0].ctx)) {
-		WD_ERR("invalid: the mode is non sva, please check system!\n");
-		return -WD_EINVAL;
-	}
-
-	return 0;
-}
-
 int wd_rsa_init(struct wd_ctx_config *config, struct wd_sched *sched)
 {
 	void *priv;
 	int ret;
 
-	if (param_check(config, sched))
-		return -WD_EINVAL;
+	ret = wd_init_param_check(config, sched);
+	if (ret)
+		return ret;
 
 	ret = wd_set_epoll_en("WD_RSA_EPOLL_EN",
 			      &wd_rsa_setting.config.epoll_en);
diff --git a/wd_util.c b/wd_util.c
index 8bda8d7..4d5faf9 100644
--- a/wd_util.c
+++ b/wd_util.c
@@ -1628,3 +1628,23 @@ int wd_set_epoll_en(const char *var_name, bool *epoll_en)
 
 	return 0;
 }
+
+int wd_init_param_check(struct wd_ctx_config *config, struct wd_sched *sched)
+{
+	if (!config || !config->ctxs || !config->ctxs[0].ctx) {
+		WD_ERR("invalid: config or config->ctxs is NULL!\n");
+		return -WD_EINVAL;
+	}
+
+	if (!sched) {
+		WD_ERR("invalid: sched is NULL!\n");
+		return -WD_EINVAL;
+	}
+
+	if (!wd_is_sva(config->ctxs[0].ctx)) {
+		WD_ERR("invalid: the mode is non sva, please check system!\n");
+		return -WD_EINVAL;
+	}
+
+	return 0;
+}
-- 
2.27.0

