1.use enum type wd_type 2.optimize judgement
Signed-off-by: Wenkai Lin linwenkai6@hisilicon.com --- include/wd_util.h | 16 ++++++++++--- wd_cipher.c | 4 ++-- wd_comp.c | 3 +-- wd_dh.c | 3 +-- wd_ecc.c | 3 +-- wd_rsa.c | 3 +-- wd_util.c | 57 ++++++++++++++++++++++++++--------------------- 7 files changed, 51 insertions(+), 38 deletions(-)
diff --git a/include/wd_util.h b/include/wd_util.h index ebe6669..ce76852 100644 --- a/include/wd_util.h +++ b/include/wd_util.h @@ -23,6 +23,17 @@ extern "C" { for ((i) = 0, (config_numa) = (config)->config_per_numa; \ (i) < (config)->numa_num; (config_numa)++, (i)++)
+enum wd_type { + WD_COMP_TYPE = 0, + WD_CIPHER_TYPE, + WD_DIGEST_TYPE, + WD_AEAD_TYPE, + WD_RSA_TYPE, + WD_DH_TYPE, + WD_ECC_TYPE, + WD_TYPE_MAX, +}; + enum wd_status { WD_UNINIT, WD_INITING, @@ -423,7 +434,7 @@ static inline void wd_alg_clear_init(enum wd_status *status) * @ctx_params: wd_ctx_params to be initialized. * @user_ctx_params: user input wd_ctx_params. * @driver: device driver for the current algorithm application. - * @env_name: algorithm environment variable. + * @type: algorithm type. * @max_op_type: algorithm max operation type. * * Return 0 if succeed and other error number if fail. @@ -431,8 +442,7 @@ static inline void wd_alg_clear_init(enum wd_status *status) int wd_ctx_param_init(struct wd_ctx_params *ctx_params, struct wd_ctx_params *user_ctx_params, struct wd_alg_driver *driver, - const char *env_name, - int max_op_type); + enum wd_type type, int max_op_type);
/** * wd_alg_attrs_init() - Request the ctxs and initialize the sched_domain diff --git a/wd_cipher.c b/wd_cipher.c index b24fcf9..e7c2007 100644 --- a/wd_cipher.c +++ b/wd_cipher.c @@ -425,8 +425,8 @@ res_retry:
cipher_ctx_params.ctx_set_num = cipher_ctx_num; ret = wd_ctx_param_init(&cipher_ctx_params, ctx_params, - wd_cipher_setting.driver, "WD_CIPHER_CTX_NUM", - WD_CIPHER_DECRYPTION + 1); + wd_cipher_setting.driver, + WD_CIPHER_TYPE, WD_CIPHER_DECRYPTION + 1); if (ret) { if (ret == -WD_EAGAIN) { wd_disable_drv(wd_cipher_setting.driver); diff --git a/wd_comp.c b/wd_comp.c index f38a922..ed1d128 100644 --- a/wd_comp.c +++ b/wd_comp.c @@ -245,8 +245,7 @@ res_retry:
comp_ctx_params.ctx_set_num = comp_ctx_num; ret = wd_ctx_param_init(&comp_ctx_params, ctx_params, - wd_comp_setting.driver, "WD_COMP_CTX_NUM", - WD_DIR_MAX); + wd_comp_setting.driver, WD_COMP_TYPE, WD_DIR_MAX); if (ret) { if (ret == -WD_EAGAIN) { wd_disable_drv(wd_comp_setting.driver); diff --git a/wd_dh.c b/wd_dh.c index 7b32f8a..7c57449 100644 --- a/wd_dh.c +++ b/wd_dh.c @@ -237,8 +237,7 @@ res_retry:
dh_ctx_params.ctx_set_num = dh_ctx_num; ret = wd_ctx_param_init(&dh_ctx_params, ctx_params, - wd_dh_setting.driver, "WD_DH_CTX_NUM", - WD_DH_PHASE2); + wd_dh_setting.driver, WD_DH_TYPE, WD_DH_PHASE2); if (ret) { if (ret == -WD_EAGAIN) { wd_disable_drv(wd_dh_setting.driver); diff --git a/wd_ecc.c b/wd_ecc.c index 0565aea..fbe2d16 100644 --- a/wd_ecc.c +++ b/wd_ecc.c @@ -293,8 +293,7 @@ res_retry:
ecc_ctx_params.ctx_set_num = ecc_ctx_num; ret = wd_ctx_param_init(&ecc_ctx_params, ctx_params, - wd_ecc_setting.driver, "WD_ECC_CTX_NUM", - WD_EC_OP_MAX); + wd_ecc_setting.driver, WD_ECC_TYPE, WD_EC_OP_MAX); if (ret) { if (ret == -WD_EAGAIN) { wd_disable_drv(wd_ecc_setting.driver); diff --git a/wd_rsa.c b/wd_rsa.c index f062b78..64c833b 100644 --- a/wd_rsa.c +++ b/wd_rsa.c @@ -278,8 +278,7 @@ res_retry:
rsa_ctx_params.ctx_set_num = rsa_ctx_num; ret = wd_ctx_param_init(&rsa_ctx_params, ctx_params, - wd_rsa_setting.driver, "WD_RSA_CTX_NUM", - WD_RSA_GENKEY); + wd_rsa_setting.driver, WD_RSA_TYPE, WD_RSA_GENKEY); if (ret) { if (ret == -WD_EAGAIN) { wd_disable_drv(wd_rsa_setting.driver); diff --git a/wd_util.c b/wd_util.c index 8286092..1dae154 100644 --- a/wd_util.c +++ b/wd_util.c @@ -47,6 +47,16 @@ static const char *comp_ctx_type[2][2] = { /* define two ctx mode here for cipher and other alg */ static const char *ctx_type[2][1] = { {"sync:"}, {"async:"} };
+static const char *wd_env_name[WD_TYPE_MAX] = { + "WD_COMP_CTX_NUM", + "WD_CIPHER_CTX_NUM", + "WD_DIGEST_CTX_NUM", + "WD_AEAD_CTX_NUM", + "WD_RSA_CTX_NUM", + "WD_DH_CTX_NUM", + "WD_ECC_CTX_NUM", +}; + struct async_task { __u32 idx; }; @@ -2070,48 +2080,45 @@ out_free_str: int wd_ctx_param_init(struct wd_ctx_params *ctx_params, struct wd_ctx_params *user_ctx_params, struct wd_alg_driver *driver, - const char *env_name, - int max_op_type) + enum wd_type type, int max_op_type) { + const char *env_name = wd_env_name[type]; const char *var_s; - int use_env = 0; int i, ret;
var_s = secure_getenv(env_name); if (var_s && strlen(var_s)) { ret = wd_env_set_ctx_nums(env_name, var_s, ctx_params->ctx_set_num, max_op_type); - if (ret < 0) { + if (ret) { WD_ERR("fail to init ctx nums from %s!\n", env_name); return -WD_EINVAL; } - use_env = 1; - } + } else { + if (user_ctx_params) { + ctx_params->bmp = user_ctx_params->bmp; + ctx_params->ctx_set_num = user_ctx_params->ctx_set_num; + ctx_params->op_type_num = user_ctx_params->op_type_num; + if (ctx_params->op_type_num > max_op_type) { + WD_ERR("fail to check user op type numbers.\n"); + return -WD_EINVAL; + }
- if (use_env || !user_ctx_params) { - ctx_params->bmp = NULL; - ctx_params->op_type_num = driver->op_type_num; - if (ctx_params->op_type_num > max_op_type) { - WD_ERR("fail to check driver op type numbers.\n"); - return -WD_EAGAIN; + return 0; }
for (i = 0; i < ctx_params->op_type_num; i++) { - if (!ctx_params->ctx_set_num[i].sync_ctx_num) - ctx_params->ctx_set_num[i].sync_ctx_num = driver->queue_num; - - if (!ctx_params->ctx_set_num[i].async_ctx_num) - ctx_params->ctx_set_num[i].async_ctx_num = driver->queue_num; - } - } else { - ctx_params->bmp = user_ctx_params->bmp; - ctx_params->ctx_set_num = user_ctx_params->ctx_set_num; - ctx_params->op_type_num = user_ctx_params->op_type_num; - if (ctx_params->op_type_num > max_op_type) { - WD_ERR("fail to check user op type numbers.\n"); - return -WD_EINVAL; + ctx_params->ctx_set_num[i].sync_ctx_num = driver->queue_num; + ctx_params->ctx_set_num[i].async_ctx_num = driver->queue_num; } }
+ ctx_params->bmp = NULL; + ctx_params->op_type_num = driver->op_type_num; + if (ctx_params->op_type_num > max_op_type) { + WD_ERR("fail to check driver op type numbers.\n"); + return -WD_EAGAIN; + } + return 0; }