To simplify the calling logic of uadk, a new init2 interface is added to the ecc/rsa/dh algorithm class, which includes hardware device resource initialization and scheduler initialization.
Weili Qian (6): uadk/ecc: add the init2 interface for ecc uadk/rsa: add the init2 interface for rsa uadk/dh: add the init2 interface for dh uadk/ecc: adapt the ecc module to the dynamic loading framework uadk/rsa: adapt the rsa module to the dynamic loading framework uadk/dh: adapt the dh module to the dynamic loading framework
drv/hisi_hpre.c | 180 +++++++++++++++++++-------- include/drv/wd_dh_drv.h | 27 ---- include/drv/wd_ecc_drv.h | 27 ---- include/drv/wd_rsa_drv.h | 27 ---- include/wd_dh.h | 30 +++++ include/wd_ecc.h | 28 +++++ include/wd_rsa.h | 28 +++++ libwd_crypto.map | 9 ++ wd_dh.c | 256 +++++++++++++++++++++++++++----------- wd_ecc.c | 262 ++++++++++++++++++++++++++++----------- wd_rsa.c | 256 +++++++++++++++++++++++++++----------- 11 files changed, 786 insertions(+), 344 deletions(-)
This set of interfaces puts resource initialization operations into the init2 interface, simplifying the initialization operations when users use the ecc algorithm.
Signed-off-by: Weili Qian qianweili@huawei.com --- include/wd_ecc.h | 28 ++++++++++++ libwd_crypto.map | 3 ++ wd_ecc.c | 109 ++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 121 insertions(+), 19 deletions(-)
diff --git a/include/wd_ecc.h b/include/wd_ecc.h index a92bb4b..6f670e2 100644 --- a/include/wd_ecc.h +++ b/include/wd_ecc.h @@ -432,6 +432,34 @@ int wd_ecc_init(struct wd_ctx_config *config, struct wd_sched *sched); */ void wd_ecc_uninit(void);
+/** + * wd_ecc_init2_() - A simplify interface to initializate ecc. + * This interface keeps most functions of + * wd_ecc_init(). Users just need to descripe the deployment of + * business scenarios. Then the initialization will request appropriate + * resources to support the business scenarios. + * To make the initializate simpler, ctx_params support set NULL. + * And then the function will set them as default. + * Please do not use this interface with wd_ecc_init() together, or + * some resources may be leak. + * + * @alg: The algorithm users want to use. + * @sched_type: The scheduling type users want to use. + * @task_type: Reserved. + * @ctx_params: The ctxs resources users want to use. Include per operation + * type ctx numbers and business process run numa. + * + * Return 0 if succeed and others if fail. + */ +int wd_ecc_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params); + +#define wd_ecc_init2(alg, sched_type, task_type) \ + wd_ecc_init2_(alg, sched_type, task_type, NULL) + +/** + * wd_ecc_uninit2() - Uninitialise ctx configuration and scheduler. + */ +void wd_ecc_uninit2(void);
/** * wd_ecc_alloc_sess() - Allocate a wd ecc session. diff --git a/libwd_crypto.map b/libwd_crypto.map index 5fadc53..2983f20 100644 --- a/libwd_crypto.map +++ b/libwd_crypto.map @@ -154,6 +154,9 @@ global: wd_ecdsa_get_sign_out_params; wd_ecc_init; wd_ecc_uninit; + wd_ecc_init2; + wd_ecc_init2_; + wd_ecc_uninit2; wd_ecc_alloc_sess; wd_ecc_free_sess; wd_ecc_poll; diff --git a/wd_ecc.c b/wd_ecc.c index 99811b5..4cad1e4 100644 --- a/wd_ecc.c +++ b/wd_ecc.c @@ -74,6 +74,17 @@ static struct wd_ecc_setting { } wd_ecc_setting;
struct wd_env_config wd_ecc_env_config; +static struct wd_init_attrs wd_ecc_init_attrs; + +static struct wd_ctx_nums wd_ecc_ctx_num[] = { + {1, 1}, {} +}; + +static struct wd_ctx_params wd_ecc_ctx_params = { + .op_type_num = 1, + .ctx_set_num = wd_ecc_ctx_num, + .bmp = NULL, +};
static const struct wd_ecc_curve_list curve_list[] = { /* parameter 3 is key width */ @@ -135,30 +146,19 @@ static void wd_ecc_clear_status(void) wd_alg_clear_init(&wd_ecc_setting.status); }
-int wd_ecc_init(struct wd_ctx_config *config, struct wd_sched *sched) +static int wd_ecc_common_init(struct wd_ctx_config *config, struct wd_sched *sched) { void *priv; - bool flag; int ret;
- pthread_atfork(NULL, NULL, wd_ecc_clear_status); - - flag = wd_alg_try_init(&wd_ecc_setting.status); - if (!flag) - return 0; - - ret = wd_init_param_check(config, sched); - if (ret) - goto out_clear_init; - ret = wd_set_epoll_en("WD_ECC_EPOLL_EN", &wd_ecc_setting.config.epoll_en); if (ret < 0) - goto out_clear_init; + return ret;
ret = wd_init_ctx_config(&wd_ecc_setting.config, config); if (ret < 0) - goto out_clear_init; + return ret;
ret = wd_init_sched(&wd_ecc_setting.sched, sched); if (ret < 0) @@ -190,8 +190,6 @@ int wd_ecc_init(struct wd_ctx_config *config, struct wd_sched *sched) goto out_free_priv; }
- wd_alg_set_init(&wd_ecc_setting.status); - return 0;
out_free_priv: @@ -203,12 +201,10 @@ out_clear_sched: wd_clear_sched(&wd_ecc_setting.sched); out_clear_ctx_config: wd_clear_ctx_config(&wd_ecc_setting.config); -out_clear_init: - wd_alg_clear_init(&wd_ecc_setting.status); return ret; }
-void wd_ecc_uninit(void) +static void wd_ecc_common_uninit(void) { if (!wd_ecc_setting.priv) { WD_ERR("invalid: repeat uninit ecc!\n"); @@ -226,6 +222,81 @@ void wd_ecc_uninit(void) /* unset config, sched, driver */ wd_clear_sched(&wd_ecc_setting.sched); wd_clear_ctx_config(&wd_ecc_setting.config); +} + +int wd_ecc_init(struct wd_ctx_config *config, struct wd_sched *sched) +{ + bool flag; + int ret; + + pthread_atfork(NULL, NULL, wd_ecc_clear_status); + + flag = wd_alg_try_init(&wd_ecc_setting.status); + if (!flag) + return 0; + + ret = wd_init_param_check(config, sched); + if (ret) + goto out_clear_init; + + ret = wd_ecc_common_init(config, sched); + if (ret) + goto out_clear_init; + + wd_alg_set_init(&wd_ecc_setting.status); + + return 0; + +out_clear_init: + wd_alg_clear_init(&wd_ecc_setting.status); + return ret; +} + +void wd_ecc_uninit(void) +{ + wd_ecc_common_uninit(); + wd_alg_clear_init(&wd_ecc_setting.status); +} + +int wd_ecc_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params) +{ + bool flag; + int ret; + + pthread_atfork(NULL, NULL, wd_ecc_clear_status); + + flag = wd_alg_try_init(&wd_ecc_setting.status); + if (!flag) + return 0; + + if (!alg || sched_type > SCHED_POLICY_BUTT || task_type < 0 || task_type > TASK_MAX_TYPE) { + WD_ERR("invalid: input param is wrong!\n"); + ret = -WD_EINVAL; + goto out_clear_init; + } + + wd_ecc_init_attrs.alg = alg; + wd_ecc_init_attrs.sched_type = sched_type; + wd_ecc_init_attrs.ctx_params = ctx_params ? ctx_params : &wd_ecc_ctx_params; + wd_ecc_init_attrs.alg_init = wd_ecc_common_init; + wd_ecc_init_attrs.alg_poll_ctx = wd_ecc_poll_ctx; + ret = wd_alg_attrs_init(&wd_ecc_init_attrs); + if (ret) + goto out_clear_init; + + wd_alg_set_init(&wd_ecc_setting.status); + + return 0; + +out_clear_init: + wd_alg_clear_init(&wd_ecc_setting.status); + return ret; +} + +void wd_ecc_uninit2(void) +{ + wd_ecc_common_uninit(); + wd_alg_attrs_uninit(&wd_ecc_init_attrs); wd_alg_clear_init(&wd_ecc_setting.status); }
This set of interfaces puts resource initialization operations into the init2 interface, simplifying the initialization operations when users use the rsa algorithm.
Signed-off-by: Weili Qian qianweili@huawei.com --- include/wd_rsa.h | 28 ++++++++++++ libwd_crypto.map | 3 ++ wd_rsa.c | 109 ++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 121 insertions(+), 19 deletions(-)
diff --git a/include/wd_rsa.h b/include/wd_rsa.h index e16171f..733d0b7 100644 --- a/include/wd_rsa.h +++ b/include/wd_rsa.h @@ -120,6 +120,34 @@ int wd_rsa_init(struct wd_ctx_config *config, struct wd_sched *sched); */ void wd_rsa_uninit(void);
+/** + * wd_rsa_init2_() - A simplify interface to initializate rsa. + * This interface keeps most functions of + * wd_rsa_init(). Users just need to descripe the deployment of + * business scenarios. Then the initialization will request appropriate + * resources to support the business scenarios. + * To make the initializate simpler, ctx_params support set NULL. + * And then the function will set them as default. + * Please do not use this interface with wd_rsa_init() together, or + * some resources may be leak. + * + * @alg: The algorithm users want to use. + * @sched_type: The scheduling type users want to use. + * @task_type: Reserved. + * @ctx_params: The ctxs resources users want to use. Include per operation + * type ctx numbers and business process run numa. + * + * Return 0 if succeed and others if fail. + */ +int wd_rsa_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params); + +#define wd_rsa_init2(alg, sched_type, task_type) \ + wd_rsa_init2_(alg, sched_type, task_type, NULL) + +/** + * wd_rsa_uninit2() - Uninitialise ctx configuration and scheduler. + */ +void wd_rsa_uninit2(void);
/** * wd_rsa_alloc_sess() - Allocate a wd rsa session. diff --git a/libwd_crypto.map b/libwd_crypto.map index 2983f20..871ef36 100644 --- a/libwd_crypto.map +++ b/libwd_crypto.map @@ -81,6 +81,9 @@ global: wd_rsa_set_kg_out_psz; wd_rsa_init; wd_rsa_uninit; + wd_rsa_init2; + wd_rsa_init2_; + wd_rsa_uninit2; wd_rsa_alloc_sess; wd_rsa_free_sess; wd_do_rsa_async; diff --git a/wd_rsa.c b/wd_rsa.c index 0b76c48..99bfe48 100644 --- a/wd_rsa.c +++ b/wd_rsa.c @@ -82,6 +82,17 @@ static struct wd_rsa_setting { } wd_rsa_setting;
struct wd_env_config wd_rsa_env_config; +static struct wd_init_attrs wd_rsa_init_attrs; + +static struct wd_ctx_nums wd_rsa_ctx_num[] = { + {1, 1}, {} +}; + +static struct wd_ctx_params wd_rsa_ctx_params = { + .op_type_num = 1, + .ctx_set_num = wd_rsa_ctx_num, + .bmp = NULL, +};
#ifdef WD_STATIC_DRV static void wd_rsa_set_static_drv(void) @@ -120,30 +131,19 @@ static void wd_rsa_clear_status(void) wd_alg_clear_init(&wd_rsa_setting.status); }
-int wd_rsa_init(struct wd_ctx_config *config, struct wd_sched *sched) +static int wd_rsa_common_init(struct wd_ctx_config *config, struct wd_sched *sched) { void *priv; - bool flag; int ret;
- pthread_atfork(NULL, NULL, wd_rsa_clear_status); - - flag = wd_alg_try_init(&wd_rsa_setting.status); - if (!flag) - return 0; - - ret = wd_init_param_check(config, sched); - if (ret) - goto out_clear_init; - ret = wd_set_epoll_en("WD_RSA_EPOLL_EN", &wd_rsa_setting.config.epoll_en); if (ret < 0) - goto out_clear_init; + return ret;
ret = wd_init_ctx_config(&wd_rsa_setting.config, config); if (ret < 0) - goto out_clear_init; + return ret;
ret = wd_init_sched(&wd_rsa_setting.sched, sched); if (ret < 0) @@ -175,8 +175,6 @@ int wd_rsa_init(struct wd_ctx_config *config, struct wd_sched *sched) goto out_free_priv; }
- wd_alg_set_init(&wd_rsa_setting.status); - return 0;
out_free_priv: @@ -188,12 +186,10 @@ out_clear_sched: wd_clear_sched(&wd_rsa_setting.sched); out_clear_ctx_config: wd_clear_ctx_config(&wd_rsa_setting.config); -out_clear_init: - wd_alg_clear_init(&wd_rsa_setting.status); return ret; }
-void wd_rsa_uninit(void) +static void wd_rsa_common_uninit(void) { if (!wd_rsa_setting.priv) { WD_ERR("invalid: repeat uninit rsa!\n"); @@ -211,6 +207,81 @@ void wd_rsa_uninit(void) /* unset config, sched, driver */ wd_clear_sched(&wd_rsa_setting.sched); wd_clear_ctx_config(&wd_rsa_setting.config); +} + +int wd_rsa_init(struct wd_ctx_config *config, struct wd_sched *sched) +{ + bool flag; + int ret; + + pthread_atfork(NULL, NULL, wd_rsa_clear_status); + + flag = wd_alg_try_init(&wd_rsa_setting.status); + if (!flag) + return 0; + + ret = wd_init_param_check(config, sched); + if (ret) + goto out_clear_init; + + ret = wd_rsa_common_init(config, sched); + if (ret) + goto out_clear_init; + + wd_alg_set_init(&wd_rsa_setting.status); + + return 0; + +out_clear_init: + wd_alg_clear_init(&wd_rsa_setting.status); + return ret; +} + +void wd_rsa_uninit(void) +{ + wd_rsa_common_uninit(); + wd_alg_clear_init(&wd_rsa_setting.status); +} + +int wd_rsa_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params) +{ + bool flag; + int ret; + + pthread_atfork(NULL, NULL, wd_rsa_clear_status); + + flag = wd_alg_try_init(&wd_rsa_setting.status); + if (!flag) + return 0; + + if (!alg || sched_type > SCHED_POLICY_BUTT || task_type < 0 || task_type > TASK_MAX_TYPE) { + WD_ERR("invalid: input param is wrong!\n"); + ret = -WD_EINVAL; + goto out_clear_init; + } + + wd_rsa_init_attrs.alg = alg; + wd_rsa_init_attrs.sched_type = sched_type; + wd_rsa_init_attrs.ctx_params = ctx_params ? ctx_params : &wd_rsa_ctx_params; + wd_rsa_init_attrs.alg_init = wd_rsa_common_init; + wd_rsa_init_attrs.alg_poll_ctx = wd_rsa_poll_ctx; + ret = wd_alg_attrs_init(&wd_rsa_init_attrs); + if (ret) + goto out_clear_init; + + wd_alg_set_init(&wd_rsa_setting.status); + + return 0; + +out_clear_init: + wd_alg_clear_init(&wd_rsa_setting.status); + return ret; +} + +void wd_rsa_uninit2(void) +{ + wd_rsa_common_uninit(); + wd_alg_attrs_uninit(&wd_rsa_init_attrs); wd_alg_clear_init(&wd_rsa_setting.status); }
This set of interfaces puts resource initialization operations into the init2 interface, simplifying the initialization operations when users use the dh algorithm.
Signed-off-by: Weili Qian qianweili@huawei.com --- include/wd_dh.h | 30 +++++++++++++ libwd_crypto.map | 3 ++ wd_dh.c | 109 ++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 123 insertions(+), 19 deletions(-)
diff --git a/include/wd_dh.h b/include/wd_dh.h index 3912680..afc2f7c 100644 --- a/include/wd_dh.h +++ b/include/wd_dh.h @@ -61,6 +61,36 @@ int wd_dh_poll_ctx(__u32 idx, __u32 expt, __u32 *count); int wd_dh_poll(__u32 expt, __u32 *count); int wd_dh_init(struct wd_ctx_config *config, struct wd_sched *sched); void wd_dh_uninit(void); + +/** + * wd_dh_init2_() - A simplify interface to initializate dh. + * This interface keeps most functions of + * wd_dh_init(). Users just need to descripe the deployment of + * business scenarios. Then the initialization will request appropriate + * resources to support the business scenarios. + * To make the initializate simpler, ctx_params support set NULL. + * And then the function will set them as default. + * Please do not use this interface with wd_dh_init() together, or + * some resources may be leak. + * + * @alg: The algorithm users want to use. + * @sched_type: The scheduling type users want to use. + * @task_type: Reserved. + * @ctx_params: The ctxs resources users want to use. Include per operation + * type ctx numbers and business process run numa. + * + * Return 0 if succeed and others if fail. + */ +int wd_dh_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params); + +#define wd_dh_init2(alg, sched_type, task_type) \ + wd_dh_init2_(alg, sched_type, task_type, NULL) + +/** + * wd_dh_uninit2() - Uninitialise ctx configuration and scheduler. + */ +void wd_dh_uninit2(void); + int wd_dh_env_init(struct wd_sched *sched); void wd_dh_env_uninit(void); int wd_dh_ctx_num_init(__u32 node, __u32 type, __u32 num, __u8 mode); diff --git a/libwd_crypto.map b/libwd_crypto.map index 871ef36..5c46c44 100644 --- a/libwd_crypto.map +++ b/libwd_crypto.map @@ -112,6 +112,9 @@ global: wd_dh_poll; wd_dh_init; wd_dh_uninit; + wd_dh_init2; + wd_dh_init2_; + wd_dh_uninit2; wd_dh_env_init; wd_dh_env_uninit; wd_dh_ctx_num_init; diff --git a/wd_dh.c b/wd_dh.c index 4cb5c26..d861b34 100644 --- a/wd_dh.c +++ b/wd_dh.c @@ -41,6 +41,17 @@ static struct wd_dh_setting { } wd_dh_setting;
struct wd_env_config wd_dh_env_config; +static struct wd_init_attrs wd_dh_init_attrs; + +static struct wd_ctx_nums wd_dh_ctx_num[] = { + {1, 1}, {} +}; + +static struct wd_ctx_params wd_dh_ctx_params = { + .op_type_num = 1, + .ctx_set_num = wd_dh_ctx_num, + .bmp = NULL, +};
#ifdef WD_STATIC_DRV static void wd_dh_set_static_drv(void) @@ -79,30 +90,19 @@ static void wd_dh_clear_status(void) wd_alg_clear_init(&wd_dh_setting.status); }
-int wd_dh_init(struct wd_ctx_config *config, struct wd_sched *sched) +static int wd_dh_common_init(struct wd_ctx_config *config, struct wd_sched *sched) { void *priv; - bool flag; int ret;
- pthread_atfork(NULL, NULL, wd_dh_clear_status); - - flag = wd_alg_try_init(&wd_dh_setting.status); - if (!flag) - return 0; - - ret = wd_init_param_check(config, sched); - if (ret) - goto out_clear_init; - ret = wd_set_epoll_en("WD_DH_EPOLL_EN", &wd_dh_setting.config.epoll_en); if (ret < 0) - goto out_clear_init; + return ret;
ret = wd_init_ctx_config(&wd_dh_setting.config, config); if (ret) - goto out_clear_init; + return ret;
ret = wd_init_sched(&wd_dh_setting.sched, sched); if (ret) @@ -134,8 +134,6 @@ int wd_dh_init(struct wd_ctx_config *config, struct wd_sched *sched) goto out_free_priv; }
- wd_alg_set_init(&wd_dh_setting.status); - return 0;
out_free_priv: @@ -147,12 +145,10 @@ out_clear_sched: wd_clear_sched(&wd_dh_setting.sched); out_clear_ctx_config: wd_clear_ctx_config(&wd_dh_setting.config); -out_clear_init: - wd_alg_clear_init(&wd_dh_setting.status); return ret; }
-void wd_dh_uninit(void) +static void wd_dh_common_uninit(void) { if (!wd_dh_setting.priv) { WD_ERR("invalid: repeat uninit dh!\n"); @@ -170,6 +166,81 @@ void wd_dh_uninit(void) /* unset config, sched, driver */ wd_clear_sched(&wd_dh_setting.sched); wd_clear_ctx_config(&wd_dh_setting.config); +} + +int wd_dh_init(struct wd_ctx_config *config, struct wd_sched *sched) +{ + bool flag; + int ret; + + pthread_atfork(NULL, NULL, wd_dh_clear_status); + + flag = wd_alg_try_init(&wd_dh_setting.status); + if (!flag) + return 0; + + ret = wd_init_param_check(config, sched); + if (ret) + goto out_clear_init; + + ret = wd_dh_common_init(config, sched); + if (ret) + goto out_clear_init; + + wd_alg_set_init(&wd_dh_setting.status); + + return 0; + +out_clear_init: + wd_alg_clear_init(&wd_dh_setting.status); + return ret; +} + +void wd_dh_uninit(void) +{ + wd_dh_common_uninit(); + wd_alg_clear_init(&wd_dh_setting.status); +} + +int wd_dh_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params) +{ + bool flag; + int ret; + + pthread_atfork(NULL, NULL, wd_dh_clear_status); + + flag = wd_alg_try_init(&wd_dh_setting.status); + if (!flag) + return 0; + + if (!alg || sched_type > SCHED_POLICY_BUTT || task_type < 0 || task_type > TASK_MAX_TYPE) { + WD_ERR("invalid: input param is wrong!\n"); + ret = -WD_EINVAL; + goto out_clear_init; + } + + wd_dh_init_attrs.alg = alg; + wd_dh_init_attrs.sched_type = sched_type; + wd_dh_init_attrs.ctx_params = ctx_params ? ctx_params : &wd_dh_ctx_params; + wd_dh_init_attrs.alg_init = wd_dh_common_init; + wd_dh_init_attrs.alg_poll_ctx = wd_dh_poll_ctx; + ret = wd_alg_attrs_init(&wd_dh_init_attrs); + if (ret) + goto out_clear_init; + + wd_alg_set_init(&wd_dh_setting.status); + + return 0; + +out_clear_init: + wd_alg_clear_init(&wd_dh_setting.status); + return ret; +} + +void wd_dh_uninit2(void) +{ + wd_dh_common_uninit(); + wd_alg_attrs_uninit(&wd_dh_init_attrs); wd_alg_clear_init(&wd_dh_setting.status); }
After adding the ecc module of the init2 interface, combine it dynamically load the initialization part, transform HiSilicon HPRE driven, and implemented using the dynamic loading function Connection between driver and algorithm layer.
Signed-off-by: Weili Qian qianweili@huawei.com --- drv/hisi_hpre.c | 124 ++++++++++++++++++++------- include/drv/wd_ecc_drv.h | 27 ------ wd_ecc.c | 181 +++++++++++++++++++++++++-------------- 3 files changed, 209 insertions(+), 123 deletions(-)
diff --git a/drv/hisi_hpre.c b/drv/hisi_hpre.c index d8b212d..a08e5e6 100644 --- a/drv/hisi_hpre.c +++ b/drv/hisi_hpre.c @@ -32,6 +32,7 @@ #define SM2_PONIT_SIZE 64 #define MAX_HASH_LENS BITS_TO_BYTES(521) #define HW_PLAINTEXT_BYTES_MAX BITS_TO_BYTES(4096) +#define HPRE_CTX_Q_NUM_DEF 1
#define CRT_PARAMS_SZ(key_size) ((5 * (key_size)) >> 1) #define CRT_GEN_PARAMS_SZ(key_size) ((7 * (key_size)) >> 1) @@ -459,37 +460,26 @@ static int rsa_prepare_iot(struct wd_rsa_msg *msg, return ret; }
-static int hpre_init(struct wd_ctx_config_internal *config, void *priv, const char *alg_name) +static int hpre_init_qm_priv(struct wd_ctx_config_internal *config, + struct hisi_hpre_ctx *hpre_ctx, + struct hisi_qm_priv *qm_priv) { - struct hisi_hpre_ctx *hpre_ctx = (struct hisi_hpre_ctx *)priv; - struct hisi_qm_priv qm_priv; handle_t h_ctx, h_qp; int i, j;
- if (!config->ctx_num) { - WD_ERR("invalid: hpre init config ctx num is 0!\n"); - return -WD_EINVAL; - } - memcpy(&hpre_ctx->config, config, sizeof(*config));
/* allocate qp for each context */ - qm_priv.sqe_size = sizeof(struct hisi_hpre_sqe); - - /* DH/RSA: qm sqc_type = 0, ECC: qm sqc_type = 1; */ - if (!strcmp(alg_name, "ecc")) - qm_priv.op_type = HPRE_HW_V3_ECC_ALG_TYPE; - else - qm_priv.op_type = HPRE_HW_V2_ALG_TYPE; + qm_priv->sqe_size = sizeof(struct hisi_hpre_sqe);
for (i = 0; i < config->ctx_num; i++) { h_ctx = config->ctxs[i].ctx; - qm_priv.qp_mode = config->ctxs[i].ctx_mode; + qm_priv->qp_mode = config->ctxs[i].ctx_mode; /* Setting the epoll en to 0 for ASYNC ctx */ - qm_priv.epoll_en = (qm_priv.qp_mode == CTX_MODE_SYNC) ? + qm_priv->epoll_en = (qm_priv->qp_mode == CTX_MODE_SYNC) ? config->epoll_en : 0; - qm_priv.idx = i; - h_qp = hisi_qm_alloc_qp(&qm_priv, h_ctx); + qm_priv->idx = i; + h_qp = hisi_qm_alloc_qp(qm_priv, h_ctx); if (!h_qp) { WD_ERR("failed to alloc qp!\n"); goto out; @@ -506,6 +496,45 @@ out: return -WD_EINVAL; }
+static int hpre_rsa_dh_init(struct wd_ctx_config_internal *config, void *priv, const char *alg_name) +{ + struct hisi_hpre_ctx *hpre_ctx = (struct hisi_hpre_ctx *)priv; + struct hisi_qm_priv qm_priv; + int ret; + + if (!config->ctx_num) { + WD_ERR("invalid: hpre rsa/dh init config ctx num is 0!\n"); + return -WD_EINVAL; + } + + qm_priv.op_type = HPRE_HW_V2_ALG_TYPE; + ret = hpre_init_qm_priv(config, hpre_ctx, &qm_priv); + if (ret) + return ret; + + return 0; +} + +static int hpre_ecc_init(void *conf, void *priv) +{ + struct wd_ctx_config_internal *config = (struct wd_ctx_config_internal *)conf; + struct hisi_hpre_ctx *hpre_ctx = (struct hisi_hpre_ctx *)priv; + struct hisi_qm_priv qm_priv; + int ret; + + if (!config->ctx_num) { + WD_ERR("invalid: hpre ecc init config ctx num is 0!\n"); + return -WD_EINVAL; + } + + qm_priv.op_type = HPRE_HW_V2_ALG_TYPE; + ret = hpre_init_qm_priv(config, hpre_ctx, &qm_priv); + if (ret) + return ret; + + return 0; +} + static void hpre_exit(void *priv) { struct hisi_hpre_ctx *hpre_ctx = (struct hisi_hpre_ctx *)priv; @@ -624,7 +653,7 @@ static struct wd_rsa_driver rsa_hisi_hpre = { .drv_name = "hisi_hpre", .alg_name = "rsa", .drv_ctx_size = sizeof(struct hisi_hpre_ctx), - .init = hpre_init, + .init = hpre_rsa_dh_init, .exit = hpre_exit, .send = rsa_send, .recv = rsa_recv, @@ -777,7 +806,7 @@ static struct wd_dh_driver dh_hisi_hpre = { .drv_name = "hisi_hpre", .alg_name = "dh", .drv_ctx_size = sizeof(struct hisi_hpre_ctx), - .init = hpre_init, + .init = hpre_rsa_dh_init, .exit = hpre_exit, .send = dh_send, .recv = dh_recv, @@ -2427,16 +2456,51 @@ static int ecc_recv(handle_t ctx, void *ecc_msg) return ecc_sqe_parse((struct hisi_qp *)h_qp, msg, &hw_msg); }
-static struct wd_ecc_driver ecc_hisi_hpre = { - .drv_name = "hisi_hpre", - .alg_name = "ecc", - .drv_ctx_size = sizeof(struct hisi_hpre_ctx), - .init = hpre_init, - .exit = hpre_exit, - .send = ecc_send, - .recv = ecc_recv, +#define GEN_HPRE_ALG_DRIVER(hpre_alg_name) \ +{\ + .drv_name = "hisi_hpre",\ + .alg_name = hpre_alg_name,\ + .priority = UADK_ALG_HW,\ + .priv_size = sizeof(struct hisi_hpre_ctx),\ + .queue_num = HPRE_CTX_Q_NUM_DEF,\ + .op_type_num = 1,\ + .fallback = 0,\ + .init = hpre_ecc_init,\ + .exit = hpre_exit,\ + .send = ecc_send,\ + .recv = ecc_recv,\ +} + +static struct wd_alg_driver hpre_alg_driver[] = { + GEN_HPRE_ALG_DRIVER("sm2"), + GEN_HPRE_ALG_DRIVER("ecdh"), + GEN_HPRE_ALG_DRIVER("ecdsa"), + GEN_HPRE_ALG_DRIVER("x25519"), + GEN_HPRE_ALG_DRIVER("x448"), };
+static void __attribute__((constructor)) hisi_hpre_probe(void) +{ + int alg_num = ARRAY_SIZE(hpre_alg_driver); + int i, ret; + + WD_INFO("Info: register HPRE alg drivers!\n"); + + for (i = 0; i < alg_num; i++) { + ret = wd_alg_driver_register(&hpre_alg_driver[i]); + if (ret) + WD_ERR("failed to register HPRE %s driver!\n", hpre_alg_driver[i].alg_name); + } +} + +static void __attribute__((destructor)) hisi_hpre_remove(void) +{ + int alg_num = ARRAY_SIZE(hpre_alg_driver); + int i; + + for (i = 0; i < alg_num; i++) + wd_alg_driver_unregister(&hpre_alg_driver[i]); +} + WD_RSA_SET_DRIVER(rsa_hisi_hpre); WD_DH_SET_DRIVER(dh_hisi_hpre); -WD_ECC_SET_DRIVER(ecc_hisi_hpre); diff --git a/include/drv/wd_ecc_drv.h b/include/drv/wd_ecc_drv.h index 4d27ccb..f5805cd 100644 --- a/include/drv/wd_ecc_drv.h +++ b/include/drv/wd_ecc_drv.h @@ -175,35 +175,8 @@ struct wd_ecc_out { char data[]; };
-struct wd_ecc_driver { - const char *drv_name; - const char *alg_name; - __u32 drv_ctx_size; - int (*init)(struct wd_ctx_config_internal *config, void *priv, - const char *alg_name); - void (*exit)(void *priv); - int (*send)(handle_t sess, void *ecc_msg); - int (*recv)(handle_t sess, void *ecc_msg); -}; - -void wd_ecc_set_driver(struct wd_ecc_driver *drv); -struct wd_ecc_driver *wd_ecc_get_driver(void); struct wd_ecc_msg *wd_ecc_get_msg(__u32 idx, __u32 tag);
-#ifdef WD_STATIC_DRV -#define WD_ECC_SET_DRIVER(drv) \ -struct wd_ecc_driver *wd_ecc_get_driver(void) \ -{ \ - return &drv; \ -} -#else -#define WD_ECC_SET_DRIVER(drv) \ -static void __attribute__((constructor)) set_driver_ecc(void) \ -{ \ - wd_ecc_set_driver(&(drv)); \ -} -#endif - #ifdef __cplusplus } #endif diff --git a/wd_ecc.c b/wd_ecc.c index 4cad1e4..57954e0 100644 --- a/wd_ecc.c +++ b/wd_ecc.c @@ -66,26 +66,16 @@ static struct wd_ecc_setting { enum wd_status status; struct wd_ctx_config_internal config; struct wd_sched sched; - void *sched_ctx; - const struct wd_ecc_driver *driver; + struct wd_async_msg_pool pool; + struct wd_alg_driver *driver; void *priv; void *dlhandle; - struct wd_async_msg_pool pool; + void *dlh_list; } wd_ecc_setting;
struct wd_env_config wd_ecc_env_config; static struct wd_init_attrs wd_ecc_init_attrs;
-static struct wd_ctx_nums wd_ecc_ctx_num[] = { - {1, 1}, {} -}; - -static struct wd_ctx_params wd_ecc_ctx_params = { - .op_type_num = 1, - .ctx_set_num = wd_ecc_ctx_num, - .bmp = NULL, -}; - static const struct wd_ecc_curve_list curve_list[] = { /* parameter 3 is key width */ { WD_X25519, "x25519", 256, X25519_256_PARAM }, @@ -109,36 +99,50 @@ static const struct curve_param_desc curve_pram_list[] = { { ECC_CURVE_G, offsetof(struct wd_ecc_prikey, g), offsetof(struct wd_ecc_pubkey, g) } };
-#ifdef WD_STATIC_DRV -static void wd_ecc_set_static_drv(void) +static void wd_ecc_close_driver(void) { - wd_ecc_setting.driver = wd_ecc_get_driver(); - if (!wd_ecc_setting.driver) - WD_ERR("failed to get ecc driver!\n"); -} -#else -static void __attribute__((constructor)) wd_ecc_open_driver(void) -{ - wd_ecc_setting.dlhandle = dlopen("libhisi_hpre.so", RTLD_NOW); if (!wd_ecc_setting.dlhandle) - WD_ERR("failed to open libhisi_hpre.so, %s\n", dlerror()); -} + return;
-static void __attribute__((destructor)) wd_ecc_close_driver(void) -{ - if (wd_ecc_setting.dlhandle) - dlclose(wd_ecc_setting.dlhandle); + wd_release_drv(wd_ecc_setting.driver); + dlclose(wd_ecc_setting.dlhandle); + wd_ecc_setting.dlhandle = NULL; } -#endif
-void wd_ecc_set_driver(struct wd_ecc_driver *drv) +static int wd_ecc_open_driver(void) { - if (!drv) { - WD_ERR("invalid: ecc drv is NULL!\n"); - return; + struct wd_alg_driver *driver = NULL; + char lib_path[PATH_STR_SIZE]; + const char *alg_name = "sm2"; + int ret; + + /* + * Compatible with the normal acquisition of device + * drivers in the init interface + */ + if (wd_ecc_setting.dlh_list) + return 0; + + ret = wd_get_lib_file_path("libhisi_hpre.so", lib_path, false); + if (ret) + return ret; + + wd_ecc_setting.dlhandle = dlopen(lib_path, RTLD_NOW); + if (!wd_ecc_setting.dlhandle) { + WD_ERR("failed to open libhisi_hpre.so, %s!\n", dlerror()); + return -WD_EINVAL; }
- wd_ecc_setting.driver = drv; + driver = wd_request_drv(alg_name, false); + if (!driver) { + wd_ecc_close_driver(); + WD_ERR("failed to get %s driver support\n", alg_name); + return -WD_EINVAL; + } + + wd_ecc_setting.driver = driver; + + return 0; }
static void wd_ecc_clear_status(void) @@ -148,7 +152,6 @@ static void wd_ecc_clear_status(void)
static int wd_ecc_common_init(struct wd_ctx_config *config, struct wd_sched *sched) { - void *priv; int ret;
ret = wd_set_epoll_en("WD_ECC_EPOLL_EN", @@ -164,10 +167,6 @@ static int wd_ecc_common_init(struct wd_ctx_config *config, struct wd_sched *sch if (ret < 0) goto out_clear_ctx_config;
-#ifdef WD_STATIC_DRV - wd_ecc_set_static_drv(); -#endif - /* fix me: sadly find we allocate async pool for every ctx */ ret = wd_init_async_request_pool(&wd_ecc_setting.pool, config->ctx_num, WD_POOL_MAX_ENTRIES, @@ -175,26 +174,14 @@ static int wd_ecc_common_init(struct wd_ctx_config *config, struct wd_sched *sch if (ret < 0) goto out_clear_sched;
- /* initialize ctx related resources in specific driver */ - priv = calloc(1, wd_ecc_setting.driver->drv_ctx_size); - if (!priv) { - ret = -WD_ENOMEM; + ret = wd_alg_init_driver(&wd_ecc_setting.config, + wd_ecc_setting.driver, + &wd_ecc_setting.priv); + if (ret) goto out_clear_pool; - } - - wd_ecc_setting.priv = priv; - ret = wd_ecc_setting.driver->init(&wd_ecc_setting.config, priv, - wd_ecc_setting.driver->alg_name); - if (ret < 0) { - WD_ERR("failed to init ecc driver, ret = %d!\n", ret); - goto out_free_priv; - }
return 0;
-out_free_priv: - free(priv); - wd_ecc_setting.priv = NULL; out_clear_pool: wd_uninit_async_request_pool(&wd_ecc_setting.pool); out_clear_sched: @@ -211,17 +198,14 @@ static void wd_ecc_common_uninit(void) return; }
- /* driver uninit */ - wd_ecc_setting.driver->exit(wd_ecc_setting.priv); - free(wd_ecc_setting.priv); - wd_ecc_setting.priv = NULL; - /* uninit async request pool */ wd_uninit_async_request_pool(&wd_ecc_setting.pool);
/* unset config, sched, driver */ wd_clear_sched(&wd_ecc_setting.sched); - wd_clear_ctx_config(&wd_ecc_setting.config); + wd_alg_uninit_driver(&wd_ecc_setting.config, + wd_ecc_setting.driver, + &wd_ecc_setting.priv); }
int wd_ecc_init(struct wd_ctx_config *config, struct wd_sched *sched) @@ -239,14 +223,20 @@ int wd_ecc_init(struct wd_ctx_config *config, struct wd_sched *sched) if (ret) goto out_clear_init;
- ret = wd_ecc_common_init(config, sched); + ret = wd_ecc_open_driver(); if (ret) goto out_clear_init;
+ ret = wd_ecc_common_init(config, sched); + if (ret) + goto out_close_driver; + wd_alg_set_init(&wd_ecc_setting.status);
return 0;
+out_close_driver: + wd_ecc_close_driver(); out_clear_init: wd_alg_clear_init(&wd_ecc_setting.status); return ret; @@ -255,11 +245,14 @@ out_clear_init: void wd_ecc_uninit(void) { wd_ecc_common_uninit(); + wd_ecc_close_driver(); wd_alg_clear_init(&wd_ecc_setting.status); }
int wd_ecc_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params) { + struct wd_ctx_nums ecc_ctx_num[WD_EC_OP_MAX] = {0}; + struct wd_ctx_params ecc_ctx_params = {0}; bool flag; int ret;
@@ -275,19 +268,66 @@ int wd_ecc_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_para goto out_clear_init; }
+ /* + * Driver lib file path could set by env param. + * than open tham by wd_dlopen_drv() + * default dir in the /root/lib/xxx.so and then dlopen + */ + wd_ecc_setting.dlh_list = wd_dlopen_drv(NULL); + if (!wd_ecc_setting.dlh_list) { + WD_ERR("failed to open driver lib files!\n"); + ret = -WD_EINVAL; + goto out_clear_init; + } + +res_retry: + memset(&wd_ecc_setting.config, 0, sizeof(struct wd_ctx_config_internal)); + + /* Get alg driver and dev name */ + wd_ecc_setting.driver = wd_alg_drv_bind(task_type, alg); + if (!wd_ecc_setting.driver) { + WD_ERR("failed to bind a valid driver!\n"); + ret = -WD_EINVAL; + goto out_dlopen; + } + + ret = wd_ctx_param_init(&ecc_ctx_params, ctx_params, + ecc_ctx_num, wd_ecc_setting.driver, + WD_EC_OP_MAX); + if (ret) { + if (ret == -WD_EAGAIN) { + wd_disable_drv(wd_ecc_setting.driver); + wd_alg_drv_unbind(wd_ecc_setting.driver); + goto res_retry; + } + goto out_driver; + } + wd_ecc_init_attrs.alg = alg; wd_ecc_init_attrs.sched_type = sched_type; - wd_ecc_init_attrs.ctx_params = ctx_params ? ctx_params : &wd_ecc_ctx_params; + wd_ecc_init_attrs.driver = wd_ecc_setting.driver; + wd_ecc_init_attrs.ctx_params = &ecc_ctx_params; wd_ecc_init_attrs.alg_init = wd_ecc_common_init; wd_ecc_init_attrs.alg_poll_ctx = wd_ecc_poll_ctx; ret = wd_alg_attrs_init(&wd_ecc_init_attrs); - if (ret) - goto out_clear_init; + if (ret) { + if (ret == -WD_ENODEV) { + wd_disable_drv(wd_ecc_setting.driver); + wd_alg_drv_unbind(wd_ecc_setting.driver); + goto res_retry; + } + WD_ERR("failed to init alg attrs!\n"); + goto out_driver; + }
wd_alg_set_init(&wd_ecc_setting.status);
return 0;
+out_driver: + wd_alg_drv_unbind(wd_ecc_setting.driver); +out_dlopen: + wd_dlclose_drv(wd_ecc_setting.dlh_list); out_clear_init: wd_alg_clear_init(&wd_ecc_setting.status); return ret; @@ -297,6 +337,9 @@ void wd_ecc_uninit2(void) { wd_ecc_common_uninit(); wd_alg_attrs_uninit(&wd_ecc_init_attrs); + wd_alg_drv_unbind(wd_ecc_setting.driver); + wd_dlclose_drv(wd_ecc_setting.dlh_list); + wd_ecc_setting.dlh_list = NULL; wd_alg_clear_init(&wd_ecc_setting.status); }
@@ -1098,6 +1141,12 @@ handle_t wd_ecc_alloc_sess(struct wd_ecc_sess_setup *setup) if (setup_param_check(setup)) return (handle_t)0;
+ ret = wd_drv_alg_support(setup->alg, wd_ecc_setting.driver); + if (!ret) { + WD_ERR("failed to support this algorithm: %s!\n", setup->alg); + return (handle_t)0; + } + sess = calloc(1, sizeof(struct wd_ecc_sess)); if (!sess) return (handle_t)0;
在 2023/2/11 15:19, Weili Qian 写道:
After adding the ecc module of the init2 interface, combine it dynamically load the initialization part, transform HiSilicon HPRE driven, and implemented using the dynamic loading function Connection between driver and algorithm layer.
Signed-off-by: Weili Qian qianweili@huawei.com
drv/hisi_hpre.c | 124 ++++++++++++++++++++------- include/drv/wd_ecc_drv.h | 27 ------ wd_ecc.c | 181 +++++++++++++++++++++++++-------------- 3 files changed, 209 insertions(+), 123 deletions(-)
diff --git a/drv/hisi_hpre.c b/drv/hisi_hpre.c index d8b212d..a08e5e6 100644 --- a/drv/hisi_hpre.c +++ b/drv/hisi_hpre.c @@ -32,6 +32,7 @@ #define SM2_PONIT_SIZE 64 #define MAX_HASH_LENS BITS_TO_BYTES(521) #define HW_PLAINTEXT_BYTES_MAX BITS_TO_BYTES(4096) +#define HPRE_CTX_Q_NUM_DEF 1 #define CRT_PARAMS_SZ(key_size) ((5 * (key_size)) >> 1) #define CRT_GEN_PARAMS_SZ(key_size) ((7 * (key_size)) >> 1) @@ -459,37 +460,26 @@ static int rsa_prepare_iot(struct wd_rsa_msg *msg, return ret; } -static int hpre_init(struct wd_ctx_config_internal *config, void *priv, const char *alg_name) +static int hpre_init_qm_priv(struct wd_ctx_config_internal *config,
struct hisi_hpre_ctx *hpre_ctx,
{struct hisi_qm_priv *qm_priv)
- struct hisi_hpre_ctx *hpre_ctx = (struct hisi_hpre_ctx *)priv;
- struct hisi_qm_priv qm_priv; handle_t h_ctx, h_qp; int i, j;
- if (!config->ctx_num) {
WD_ERR("invalid: hpre init config ctx num is 0!\n");
return -WD_EINVAL;
- }
- memcpy(&hpre_ctx->config, config, sizeof(*config));
/* allocate qp for each context */
- qm_priv.sqe_size = sizeof(struct hisi_hpre_sqe);
- /* DH/RSA: qm sqc_type = 0, ECC: qm sqc_type = 1; */
- if (!strcmp(alg_name, "ecc"))
qm_priv.op_type = HPRE_HW_V3_ECC_ALG_TYPE;
- else
qm_priv.op_type = HPRE_HW_V2_ALG_TYPE;
- qm_priv->sqe_size = sizeof(struct hisi_hpre_sqe);
for (i = 0; i < config->ctx_num; i++) { h_ctx = config->ctxs[i].ctx;
qm_priv.qp_mode = config->ctxs[i].ctx_mode;
/* Setting the epoll en to 0 for ASYNC ctx */qm_priv->qp_mode = config->ctxs[i].ctx_mode;
qm_priv.epoll_en = (qm_priv.qp_mode == CTX_MODE_SYNC) ?
qm_priv->epoll_en = (qm_priv->qp_mode == CTX_MODE_SYNC) ? config->epoll_en : 0;
qm_priv.idx = i;
h_qp = hisi_qm_alloc_qp(&qm_priv, h_ctx);
qm_priv->idx = i;
if (!h_qp) { WD_ERR("failed to alloc qp!\n"); goto out;h_qp = hisi_qm_alloc_qp(qm_priv, h_ctx);
@@ -506,6 +496,45 @@ out: return -WD_EINVAL; } +static int hpre_rsa_dh_init(struct wd_ctx_config_internal *config, void *priv, const char *alg_name) +{
- struct hisi_hpre_ctx *hpre_ctx = (struct hisi_hpre_ctx *)priv;
- struct hisi_qm_priv qm_priv;
- int ret;
- if (!config->ctx_num) {
WD_ERR("invalid: hpre rsa/dh init config ctx num is 0!\n");
return -WD_EINVAL;
- }
- qm_priv.op_type = HPRE_HW_V2_ALG_TYPE;
- ret = hpre_init_qm_priv(config, hpre_ctx, &qm_priv);
- if (ret)
return ret;
- return 0;
+}
+static int hpre_ecc_init(void *conf, void *priv) +{
- struct wd_ctx_config_internal *config = (struct wd_ctx_config_internal *)conf;
- struct hisi_hpre_ctx *hpre_ctx = (struct hisi_hpre_ctx *)priv;
- struct hisi_qm_priv qm_priv;
- int ret;
- if (!config->ctx_num) {
WD_ERR("invalid: hpre ecc init config ctx num is 0!\n");
return -WD_EINVAL;
- }
- qm_priv.op_type = HPRE_HW_V2_ALG_TYPE;
- ret = hpre_init_qm_priv(config, hpre_ctx, &qm_priv);
- if (ret)
return ret;
- return 0;
+}
- static void hpre_exit(void *priv) { struct hisi_hpre_ctx *hpre_ctx = (struct hisi_hpre_ctx *)priv;
@@ -624,7 +653,7 @@ static struct wd_rsa_driver rsa_hisi_hpre = { .drv_name = "hisi_hpre", .alg_name = "rsa", .drv_ctx_size = sizeof(struct hisi_hpre_ctx),
- .init = hpre_init,
- .init = hpre_rsa_dh_init, .exit = hpre_exit, .send = rsa_send, .recv = rsa_recv,
@@ -777,7 +806,7 @@ static struct wd_dh_driver dh_hisi_hpre = { .drv_name = "hisi_hpre", .alg_name = "dh", .drv_ctx_size = sizeof(struct hisi_hpre_ctx),
- .init = hpre_init,
- .init = hpre_rsa_dh_init, .exit = hpre_exit, .send = dh_send, .recv = dh_recv,
@@ -2427,16 +2456,51 @@ static int ecc_recv(handle_t ctx, void *ecc_msg) return ecc_sqe_parse((struct hisi_qp *)h_qp, msg, &hw_msg); } -static struct wd_ecc_driver ecc_hisi_hpre = {
- .drv_name = "hisi_hpre",
- .alg_name = "ecc",
- .drv_ctx_size = sizeof(struct hisi_hpre_ctx),
- .init = hpre_init,
- .exit = hpre_exit,
- .send = ecc_send,
- .recv = ecc_recv,
+#define GEN_HPRE_ALG_DRIVER(hpre_alg_name) \ +{\
- .drv_name = "hisi_hpre",\
- .alg_name = hpre_alg_name,\
- .priority = UADK_ALG_HW,\
- .priv_size = sizeof(struct hisi_hpre_ctx),\
- .queue_num = HPRE_CTX_Q_NUM_DEF,\
- .op_type_num = 1,\
- .fallback = 0,\
- .init = hpre_ecc_init,\
- .exit = hpre_exit,\
- .send = ecc_send,\
- .recv = ecc_recv,\
+}
+static struct wd_alg_driver hpre_alg_driver[] = {
- GEN_HPRE_ALG_DRIVER("sm2"),
- GEN_HPRE_ALG_DRIVER("ecdh"),
- GEN_HPRE_ALG_DRIVER("ecdsa"),
- GEN_HPRE_ALG_DRIVER("x25519"),
- GEN_HPRE_ALG_DRIVER("x448"), };
+static void __attribute__((constructor)) hisi_hpre_probe(void) +{
- int alg_num = ARRAY_SIZE(hpre_alg_driver);
- int i, ret;
- WD_INFO("Info: register HPRE alg drivers!\n");
- for (i = 0; i < alg_num; i++) {
ret = wd_alg_driver_register(&hpre_alg_driver[i]);
if (ret)
WD_ERR("failed to register HPRE %s driver!\n", hpre_alg_driver[i].alg_name);
- }
+}
+static void __attribute__((destructor)) hisi_hpre_remove(void) +{
- int alg_num = ARRAY_SIZE(hpre_alg_driver);
- int i;
- for (i = 0; i < alg_num; i++)
wd_alg_driver_unregister(&hpre_alg_driver[i]);
+}
- WD_RSA_SET_DRIVER(rsa_hisi_hpre); WD_DH_SET_DRIVER(dh_hisi_hpre);
-WD_ECC_SET_DRIVER(ecc_hisi_hpre); diff --git a/include/drv/wd_ecc_drv.h b/include/drv/wd_ecc_drv.h index 4d27ccb..f5805cd 100644 --- a/include/drv/wd_ecc_drv.h +++ b/include/drv/wd_ecc_drv.h @@ -175,35 +175,8 @@ struct wd_ecc_out { char data[]; }; -struct wd_ecc_driver {
- const char *drv_name;
- const char *alg_name;
- __u32 drv_ctx_size;
- int (*init)(struct wd_ctx_config_internal *config, void *priv,
const char *alg_name);
- void (*exit)(void *priv);
- int (*send)(handle_t sess, void *ecc_msg);
- int (*recv)(handle_t sess, void *ecc_msg);
-};
-void wd_ecc_set_driver(struct wd_ecc_driver *drv); -struct wd_ecc_driver *wd_ecc_get_driver(void); struct wd_ecc_msg *wd_ecc_get_msg(__u32 idx, __u32 tag); -#ifdef WD_STATIC_DRV -#define WD_ECC_SET_DRIVER(drv) \ -struct wd_ecc_driver *wd_ecc_get_driver(void) \ -{ \
- return &drv; \
-} -#else -#define WD_ECC_SET_DRIVER(drv) \ -static void __attribute__((constructor)) set_driver_ecc(void) \ -{ \
- wd_ecc_set_driver(&(drv)); \
-} -#endif
- #ifdef __cplusplus } #endif
diff --git a/wd_ecc.c b/wd_ecc.c index 4cad1e4..57954e0 100644 --- a/wd_ecc.c +++ b/wd_ecc.c @@ -66,26 +66,16 @@ static struct wd_ecc_setting { enum wd_status status; struct wd_ctx_config_internal config; struct wd_sched sched;
- void *sched_ctx;
- const struct wd_ecc_driver *driver;
- struct wd_async_msg_pool pool;
- struct wd_alg_driver *driver; void *priv; void *dlhandle;
- struct wd_async_msg_pool pool;
- void *dlh_list; } wd_ecc_setting;
struct wd_env_config wd_ecc_env_config; static struct wd_init_attrs wd_ecc_init_attrs; -static struct wd_ctx_nums wd_ecc_ctx_num[] = {
- {1, 1}, {}
-};
-static struct wd_ctx_params wd_ecc_ctx_params = {
- .op_type_num = 1,
- .ctx_set_num = wd_ecc_ctx_num,
- .bmp = NULL,
-};
- static const struct wd_ecc_curve_list curve_list[] = { /* parameter 3 is key width */ { WD_X25519, "x25519", 256, X25519_256_PARAM },
@@ -109,36 +99,50 @@ static const struct curve_param_desc curve_pram_list[] = { { ECC_CURVE_G, offsetof(struct wd_ecc_prikey, g), offsetof(struct wd_ecc_pubkey, g) } }; -#ifdef WD_STATIC_DRV -static void wd_ecc_set_static_drv(void) +static void wd_ecc_close_driver(void) {
- wd_ecc_setting.driver = wd_ecc_get_driver();
- if (!wd_ecc_setting.driver)
WD_ERR("failed to get ecc driver!\n");
-} -#else -static void __attribute__((constructor)) wd_ecc_open_driver(void) -{
- wd_ecc_setting.dlhandle = dlopen("libhisi_hpre.so", RTLD_NOW); if (!wd_ecc_setting.dlhandle)
WD_ERR("failed to open libhisi_hpre.so, %s\n", dlerror());
-}
return;
-static void __attribute__((destructor)) wd_ecc_close_driver(void) -{
- if (wd_ecc_setting.dlhandle)
dlclose(wd_ecc_setting.dlhandle);
- wd_release_drv(wd_ecc_setting.driver);
- dlclose(wd_ecc_setting.dlhandle);
- wd_ecc_setting.dlhandle = NULL; }
-#endif -void wd_ecc_set_driver(struct wd_ecc_driver *drv) +static int wd_ecc_open_driver(void) {
- if (!drv) {
WD_ERR("invalid: ecc drv is NULL!\n");
return;
- struct wd_alg_driver *driver = NULL;
- char lib_path[PATH_STR_SIZE];
- const char *alg_name = "sm2";
- int ret;
- /*
* Compatible with the normal acquisition of device
* drivers in the init interface
*/
- if (wd_ecc_setting.dlh_list)
return 0;
- ret = wd_get_lib_file_path("libhisi_hpre.so", lib_path, false);
Can not to specify a specific vendor so?
- if (ret)
return ret;
- wd_ecc_setting.dlhandle = dlopen(lib_path, RTLD_NOW);
- if (!wd_ecc_setting.dlhandle) {
WD_ERR("failed to open libhisi_hpre.so, %s!\n", dlerror());
}return -WD_EINVAL;
- wd_ecc_setting.driver = drv;
- driver = wd_request_drv(alg_name, false);
- if (!driver) {
wd_ecc_close_driver();
WD_ERR("failed to get %s driver support\n", alg_name);
return -WD_EINVAL;
- }
- wd_ecc_setting.driver = driver;
- return 0; }
static void wd_ecc_clear_status(void) @@ -148,7 +152,6 @@ static void wd_ecc_clear_status(void) static int wd_ecc_common_init(struct wd_ctx_config *config, struct wd_sched *sched) {
- void *priv; int ret;
ret = wd_set_epoll_en("WD_ECC_EPOLL_EN", @@ -164,10 +167,6 @@ static int wd_ecc_common_init(struct wd_ctx_config *config, struct wd_sched *sch if (ret < 0) goto out_clear_ctx_config; -#ifdef WD_STATIC_DRV
- wd_ecc_set_static_drv();
-#endif
- /* fix me: sadly find we allocate async pool for every ctx */ ret = wd_init_async_request_pool(&wd_ecc_setting.pool, config->ctx_num, WD_POOL_MAX_ENTRIES,
@@ -175,26 +174,14 @@ static int wd_ecc_common_init(struct wd_ctx_config *config, struct wd_sched *sch if (ret < 0) goto out_clear_sched;
- /* initialize ctx related resources in specific driver */
- priv = calloc(1, wd_ecc_setting.driver->drv_ctx_size);
- if (!priv) {
ret = -WD_ENOMEM;
- ret = wd_alg_init_driver(&wd_ecc_setting.config,
wd_ecc_setting.driver,
&wd_ecc_setting.priv);
- if (ret) goto out_clear_pool;
- }
- wd_ecc_setting.priv = priv;
- ret = wd_ecc_setting.driver->init(&wd_ecc_setting.config, priv,
wd_ecc_setting.driver->alg_name);
- if (ret < 0) {
WD_ERR("failed to init ecc driver, ret = %d!\n", ret);
goto out_free_priv;
- }
return 0; -out_free_priv:
- free(priv);
- wd_ecc_setting.priv = NULL; out_clear_pool: wd_uninit_async_request_pool(&wd_ecc_setting.pool); out_clear_sched:
@@ -211,17 +198,14 @@ static void wd_ecc_common_uninit(void) return; }
- /* driver uninit */
- wd_ecc_setting.driver->exit(wd_ecc_setting.priv);
- free(wd_ecc_setting.priv);
- wd_ecc_setting.priv = NULL;
- /* uninit async request pool */ wd_uninit_async_request_pool(&wd_ecc_setting.pool);
/* unset config, sched, driver */ wd_clear_sched(&wd_ecc_setting.sched);
- wd_clear_ctx_config(&wd_ecc_setting.config);
- wd_alg_uninit_driver(&wd_ecc_setting.config,
wd_ecc_setting.driver,
}&wd_ecc_setting.priv);
int wd_ecc_init(struct wd_ctx_config *config, struct wd_sched *sched) @@ -239,14 +223,20 @@ int wd_ecc_init(struct wd_ctx_config *config, struct wd_sched *sched) if (ret) goto out_clear_init;
- ret = wd_ecc_common_init(config, sched);
- ret = wd_ecc_open_driver(); if (ret) goto out_clear_init;
- ret = wd_ecc_common_init(config, sched);
- if (ret)
goto out_close_driver;
- wd_alg_set_init(&wd_ecc_setting.status);
return 0; +out_close_driver:
- wd_ecc_close_driver(); out_clear_init: wd_alg_clear_init(&wd_ecc_setting.status); return ret;
@@ -255,11 +245,14 @@ out_clear_init: void wd_ecc_uninit(void) { wd_ecc_common_uninit();
- wd_ecc_close_driver(); wd_alg_clear_init(&wd_ecc_setting.status); }
int wd_ecc_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params) {
- struct wd_ctx_nums ecc_ctx_num[WD_EC_OP_MAX] = {0};
- struct wd_ctx_params ecc_ctx_params = {0}; bool flag; int ret;
@@ -275,19 +268,66 @@ int wd_ecc_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_para goto out_clear_init; }
- /*
* Driver lib file path could set by env param.
* than open tham by wd_dlopen_drv()
* default dir in the /root/lib/xxx.so and then dlopen
*/
- wd_ecc_setting.dlh_list = wd_dlopen_drv(NULL);
- if (!wd_ecc_setting.dlh_list) {
WD_ERR("failed to open driver lib files!\n");
ret = -WD_EINVAL;
goto out_clear_init;
- }
+res_retry:
- memset(&wd_ecc_setting.config, 0, sizeof(struct wd_ctx_config_internal));
- /* Get alg driver and dev name */
- wd_ecc_setting.driver = wd_alg_drv_bind(task_type, alg);
- if (!wd_ecc_setting.driver) {
WD_ERR("failed to bind a valid driver!\n");
ret = -WD_EINVAL;
goto out_dlopen;
- }
- ret = wd_ctx_param_init(&ecc_ctx_params, ctx_params,
ecc_ctx_num, wd_ecc_setting.driver,
WD_EC_OP_MAX);
- if (ret) {
if (ret == -WD_EAGAIN) {
wd_disable_drv(wd_ecc_setting.driver);
wd_alg_drv_unbind(wd_ecc_setting.driver);
goto res_retry;
}
goto out_driver;
- }
- wd_ecc_init_attrs.alg = alg; wd_ecc_init_attrs.sched_type = sched_type;
- wd_ecc_init_attrs.ctx_params = ctx_params ? ctx_params : &wd_ecc_ctx_params;
- wd_ecc_init_attrs.driver = wd_ecc_setting.driver;
- wd_ecc_init_attrs.ctx_params = &ecc_ctx_params; wd_ecc_init_attrs.alg_init = wd_ecc_common_init; wd_ecc_init_attrs.alg_poll_ctx = wd_ecc_poll_ctx; ret = wd_alg_attrs_init(&wd_ecc_init_attrs);
- if (ret)
goto out_clear_init;
- if (ret) {
if (ret == -WD_ENODEV) {
wd_disable_drv(wd_ecc_setting.driver);
wd_alg_drv_unbind(wd_ecc_setting.driver);
goto res_retry;
}
WD_ERR("failed to init alg attrs!\n");
goto out_driver;
- }
wd_alg_set_init(&wd_ecc_setting.status); return 0; +out_driver:
- wd_alg_drv_unbind(wd_ecc_setting.driver);
+out_dlopen:
- wd_dlclose_drv(wd_ecc_setting.dlh_list); out_clear_init: wd_alg_clear_init(&wd_ecc_setting.status); return ret;
@@ -297,6 +337,9 @@ void wd_ecc_uninit2(void) { wd_ecc_common_uninit(); wd_alg_attrs_uninit(&wd_ecc_init_attrs);
- wd_alg_drv_unbind(wd_ecc_setting.driver);
- wd_dlclose_drv(wd_ecc_setting.dlh_list);
- wd_ecc_setting.dlh_list = NULL; wd_alg_clear_init(&wd_ecc_setting.status); }
@@ -1098,6 +1141,12 @@ handle_t wd_ecc_alloc_sess(struct wd_ecc_sess_setup *setup) if (setup_param_check(setup)) return (handle_t)0;
- ret = wd_drv_alg_support(setup->alg, wd_ecc_setting.driver);
- if (!ret) {
WD_ERR("failed to support this algorithm: %s!\n", setup->alg);
return (handle_t)0;
- }
- sess = calloc(1, sizeof(struct wd_ecc_sess)); if (!sess) return (handle_t)0;
On 2023/2/16 21:32, fanghao (A) wrote:
在 2023/2/11 15:19, Weili Qian 写道:
After adding the ecc module of the init2 interface, combine it dynamically load the initialization part, transform HiSilicon HPRE driven, and implemented using the dynamic loading function Connection between driver and algorithm layer.
Signed-off-by: Weili Qian qianweili@huawei.com
drv/hisi_hpre.c | 124 ++++++++++++++++++++------- include/drv/wd_ecc_drv.h | 27 ------ wd_ecc.c | 181 +++++++++++++++++++++++++-------------- 3 files changed, 209 insertions(+), 123 deletions(-)
diff --git a/drv/hisi_hpre.c b/drv/hisi_hpre.c index d8b212d..a08e5e6 100644 --- a/drv/hisi_hpre.c +++ b/drv/hisi_hpre.c @@ -32,6 +32,7 @@ #define SM2_PONIT_SIZE 64 #define MAX_HASH_LENS BITS_TO_BYTES(521) #define HW_PLAINTEXT_BYTES_MAX BITS_TO_BYTES(4096) +#define HPRE_CTX_Q_NUM_DEF 1 #define CRT_PARAMS_SZ(key_size) ((5 * (key_size)) >> 1) #define CRT_GEN_PARAMS_SZ(key_size) ((7 * (key_size)) >> 1) @@ -459,37 +460,26 @@ static int rsa_prepare_iot(struct wd_rsa_msg *msg, return ret; } -static int hpre_init(struct wd_ctx_config_internal *config, void *priv, const char *alg_name) +static int hpre_init_qm_priv(struct wd_ctx_config_internal *config,
struct hisi_hpre_ctx *hpre_ctx,
{struct hisi_qm_priv *qm_priv)
- struct hisi_hpre_ctx *hpre_ctx = (struct hisi_hpre_ctx *)priv;
- struct hisi_qm_priv qm_priv; handle_t h_ctx, h_qp; int i, j;
- if (!config->ctx_num) {
WD_ERR("invalid: hpre init config ctx num is 0!\n");
return -WD_EINVAL;
- }
memcpy(&hpre_ctx->config, config, sizeof(*config)); /* allocate qp for each context */
- qm_priv.sqe_size = sizeof(struct hisi_hpre_sqe);
- /* DH/RSA: qm sqc_type = 0, ECC: qm sqc_type = 1; */
- if (!strcmp(alg_name, "ecc"))
qm_priv.op_type = HPRE_HW_V3_ECC_ALG_TYPE;
- else
qm_priv.op_type = HPRE_HW_V2_ALG_TYPE;
- qm_priv->sqe_size = sizeof(struct hisi_hpre_sqe); for (i = 0; i < config->ctx_num; i++) { h_ctx = config->ctxs[i].ctx;
qm_priv.qp_mode = config->ctxs[i].ctx_mode;
qm_priv->qp_mode = config->ctxs[i].ctx_mode; /* Setting the epoll en to 0 for ASYNC ctx */
qm_priv.epoll_en = (qm_priv.qp_mode == CTX_MODE_SYNC) ?
qm_priv->epoll_en = (qm_priv->qp_mode == CTX_MODE_SYNC) ? config->epoll_en : 0;
qm_priv.idx = i;
h_qp = hisi_qm_alloc_qp(&qm_priv, h_ctx);
qm_priv->idx = i;
h_qp = hisi_qm_alloc_qp(qm_priv, h_ctx); if (!h_qp) { WD_ERR("failed to alloc qp!\n"); goto out;
@@ -506,6 +496,45 @@ out: return -WD_EINVAL; } +static int hpre_rsa_dh_init(struct wd_ctx_config_internal *config, void *priv, const char *alg_name) +{
- struct hisi_hpre_ctx *hpre_ctx = (struct hisi_hpre_ctx *)priv;
- struct hisi_qm_priv qm_priv;
- int ret;
- if (!config->ctx_num) {
WD_ERR("invalid: hpre rsa/dh init config ctx num is 0!\n");
return -WD_EINVAL;
- }
- qm_priv.op_type = HPRE_HW_V2_ALG_TYPE;
- ret = hpre_init_qm_priv(config, hpre_ctx, &qm_priv);
- if (ret)
return ret;
- return 0;
+}
+static int hpre_ecc_init(void *conf, void *priv) +{
- struct wd_ctx_config_internal *config = (struct wd_ctx_config_internal *)conf;
- struct hisi_hpre_ctx *hpre_ctx = (struct hisi_hpre_ctx *)priv;
- struct hisi_qm_priv qm_priv;
- int ret;
- if (!config->ctx_num) {
WD_ERR("invalid: hpre ecc init config ctx num is 0!\n");
return -WD_EINVAL;
- }
- qm_priv.op_type = HPRE_HW_V2_ALG_TYPE;
- ret = hpre_init_qm_priv(config, hpre_ctx, &qm_priv);
- if (ret)
return ret;
- return 0;
+}
- static void hpre_exit(void *priv) { struct hisi_hpre_ctx *hpre_ctx = (struct hisi_hpre_ctx *)priv;
@@ -624,7 +653,7 @@ static struct wd_rsa_driver rsa_hisi_hpre = { .drv_name = "hisi_hpre", .alg_name = "rsa", .drv_ctx_size = sizeof(struct hisi_hpre_ctx),
- .init = hpre_init,
- .init = hpre_rsa_dh_init, .exit = hpre_exit, .send = rsa_send, .recv = rsa_recv,
@@ -777,7 +806,7 @@ static struct wd_dh_driver dh_hisi_hpre = { .drv_name = "hisi_hpre", .alg_name = "dh", .drv_ctx_size = sizeof(struct hisi_hpre_ctx),
- .init = hpre_init,
- .init = hpre_rsa_dh_init, .exit = hpre_exit, .send = dh_send, .recv = dh_recv,
@@ -2427,16 +2456,51 @@ static int ecc_recv(handle_t ctx, void *ecc_msg) return ecc_sqe_parse((struct hisi_qp *)h_qp, msg, &hw_msg); } -static struct wd_ecc_driver ecc_hisi_hpre = {
- .drv_name = "hisi_hpre",
- .alg_name = "ecc",
- .drv_ctx_size = sizeof(struct hisi_hpre_ctx),
- .init = hpre_init,
- .exit = hpre_exit,
- .send = ecc_send,
- .recv = ecc_recv,
+#define GEN_HPRE_ALG_DRIVER(hpre_alg_name) \ +{\
- .drv_name = "hisi_hpre",\
- .alg_name = hpre_alg_name,\
- .priority = UADK_ALG_HW,\
- .priv_size = sizeof(struct hisi_hpre_ctx),\
- .queue_num = HPRE_CTX_Q_NUM_DEF,\
- .op_type_num = 1,\
- .fallback = 0,\
- .init = hpre_ecc_init,\
- .exit = hpre_exit,\
- .send = ecc_send,\
- .recv = ecc_recv,\
+}
+static struct wd_alg_driver hpre_alg_driver[] = {
- GEN_HPRE_ALG_DRIVER("sm2"),
- GEN_HPRE_ALG_DRIVER("ecdh"),
- GEN_HPRE_ALG_DRIVER("ecdsa"),
- GEN_HPRE_ALG_DRIVER("x25519"),
- GEN_HPRE_ALG_DRIVER("x448"), }; +static void __attribute__((constructor)) hisi_hpre_probe(void)
+{
- int alg_num = ARRAY_SIZE(hpre_alg_driver);
- int i, ret;
- WD_INFO("Info: register HPRE alg drivers!\n");
- for (i = 0; i < alg_num; i++) {
ret = wd_alg_driver_register(&hpre_alg_driver[i]);
if (ret)
WD_ERR("failed to register HPRE %s driver!\n", hpre_alg_driver[i].alg_name);
- }
+}
+static void __attribute__((destructor)) hisi_hpre_remove(void) +{
- int alg_num = ARRAY_SIZE(hpre_alg_driver);
- int i;
- for (i = 0; i < alg_num; i++)
wd_alg_driver_unregister(&hpre_alg_driver[i]);
+}
- WD_RSA_SET_DRIVER(rsa_hisi_hpre); WD_DH_SET_DRIVER(dh_hisi_hpre);
-WD_ECC_SET_DRIVER(ecc_hisi_hpre); diff --git a/include/drv/wd_ecc_drv.h b/include/drv/wd_ecc_drv.h index 4d27ccb..f5805cd 100644 --- a/include/drv/wd_ecc_drv.h +++ b/include/drv/wd_ecc_drv.h @@ -175,35 +175,8 @@ struct wd_ecc_out { char data[]; }; -struct wd_ecc_driver {
- const char *drv_name;
- const char *alg_name;
- __u32 drv_ctx_size;
- int (*init)(struct wd_ctx_config_internal *config, void *priv,
const char *alg_name);
- void (*exit)(void *priv);
- int (*send)(handle_t sess, void *ecc_msg);
- int (*recv)(handle_t sess, void *ecc_msg);
-};
-void wd_ecc_set_driver(struct wd_ecc_driver *drv); -struct wd_ecc_driver *wd_ecc_get_driver(void); struct wd_ecc_msg *wd_ecc_get_msg(__u32 idx, __u32 tag); -#ifdef WD_STATIC_DRV -#define WD_ECC_SET_DRIVER(drv) \ -struct wd_ecc_driver *wd_ecc_get_driver(void) \ -{ \
- return &drv; \
-} -#else -#define WD_ECC_SET_DRIVER(drv) \ -static void __attribute__((constructor)) set_driver_ecc(void) \ -{ \
- wd_ecc_set_driver(&(drv)); \
-} -#endif
- #ifdef __cplusplus } #endif
diff --git a/wd_ecc.c b/wd_ecc.c index 4cad1e4..57954e0 100644 --- a/wd_ecc.c +++ b/wd_ecc.c @@ -66,26 +66,16 @@ static struct wd_ecc_setting { enum wd_status status; struct wd_ctx_config_internal config; struct wd_sched sched;
- void *sched_ctx;
- const struct wd_ecc_driver *driver;
- struct wd_async_msg_pool pool;
- struct wd_alg_driver *driver; void *priv; void *dlhandle;
- struct wd_async_msg_pool pool;
- void *dlh_list; } wd_ecc_setting; struct wd_env_config wd_ecc_env_config; static struct wd_init_attrs wd_ecc_init_attrs; -static struct wd_ctx_nums wd_ecc_ctx_num[] = {
- {1, 1}, {}
-};
-static struct wd_ctx_params wd_ecc_ctx_params = {
- .op_type_num = 1,
- .ctx_set_num = wd_ecc_ctx_num,
- .bmp = NULL,
-};
- static const struct wd_ecc_curve_list curve_list[] = { /* parameter 3 is key width */ { WD_X25519, "x25519", 256, X25519_256_PARAM },
@@ -109,36 +99,50 @@ static const struct curve_param_desc curve_pram_list[] = { { ECC_CURVE_G, offsetof(struct wd_ecc_prikey, g), offsetof(struct wd_ecc_pubkey, g) } }; -#ifdef WD_STATIC_DRV -static void wd_ecc_set_static_drv(void) +static void wd_ecc_close_driver(void) {
- wd_ecc_setting.driver = wd_ecc_get_driver();
- if (!wd_ecc_setting.driver)
WD_ERR("failed to get ecc driver!\n");
-} -#else -static void __attribute__((constructor)) wd_ecc_open_driver(void) -{
- wd_ecc_setting.dlhandle = dlopen("libhisi_hpre.so", RTLD_NOW); if (!wd_ecc_setting.dlhandle)
WD_ERR("failed to open libhisi_hpre.so, %s\n", dlerror());
-}
-static void __attribute__((destructor)) wd_ecc_close_driver(void)return;
-{
- if (wd_ecc_setting.dlhandle)
dlclose(wd_ecc_setting.dlhandle);
- wd_release_drv(wd_ecc_setting.driver);
- dlclose(wd_ecc_setting.dlhandle);
- wd_ecc_setting.dlhandle = NULL; }
-#endif -void wd_ecc_set_driver(struct wd_ecc_driver *drv) +static int wd_ecc_open_driver(void) {
- if (!drv) {
WD_ERR("invalid: ecc drv is NULL!\n");
return;
- struct wd_alg_driver *driver = NULL;
- char lib_path[PATH_STR_SIZE];
- const char *alg_name = "sm2";
- int ret;
- /*
* Compatible with the normal acquisition of device
* drivers in the init interface
*/
- if (wd_ecc_setting.dlh_list)
return 0;
- ret = wd_get_lib_file_path("libhisi_hpre.so", lib_path, false);
Can not to specify a specific vendor so?
wd_<alg>_init only supports libhisi_hpre.so, wd_<alg>_init2 can support different .so.
- if (ret)
return ret;
- wd_ecc_setting.dlhandle = dlopen(lib_path, RTLD_NOW);
- if (!wd_ecc_setting.dlhandle) {
WD_ERR("failed to open libhisi_hpre.so, %s!\n", dlerror());
return -WD_EINVAL; }
- wd_ecc_setting.driver = drv;
- driver = wd_request_drv(alg_name, false);
- if (!driver) {
wd_ecc_close_driver();
WD_ERR("failed to get %s driver support\n", alg_name);
return -WD_EINVAL;
- }
- wd_ecc_setting.driver = driver;
- return 0; } static void wd_ecc_clear_status(void)
@@ -148,7 +152,6 @@ static void wd_ecc_clear_status(void) static int wd_ecc_common_init(struct wd_ctx_config *config, struct wd_sched *sched) {
- void *priv; int ret; ret = wd_set_epoll_en("WD_ECC_EPOLL_EN",
@@ -164,10 +167,6 @@ static int wd_ecc_common_init(struct wd_ctx_config *config, struct wd_sched *sch if (ret < 0) goto out_clear_ctx_config; -#ifdef WD_STATIC_DRV
- wd_ecc_set_static_drv();
-#endif
/* fix me: sadly find we allocate async pool for every ctx */ ret = wd_init_async_request_pool(&wd_ecc_setting.pool, config->ctx_num, WD_POOL_MAX_ENTRIES,
@@ -175,26 +174,14 @@ static int wd_ecc_common_init(struct wd_ctx_config *config, struct wd_sched *sch if (ret < 0) goto out_clear_sched;
- /* initialize ctx related resources in specific driver */
- priv = calloc(1, wd_ecc_setting.driver->drv_ctx_size);
- if (!priv) {
ret = -WD_ENOMEM;
- ret = wd_alg_init_driver(&wd_ecc_setting.config,
wd_ecc_setting.driver,
&wd_ecc_setting.priv);
- if (ret) goto out_clear_pool;
- }
- wd_ecc_setting.priv = priv;
- ret = wd_ecc_setting.driver->init(&wd_ecc_setting.config, priv,
wd_ecc_setting.driver->alg_name);
- if (ret < 0) {
WD_ERR("failed to init ecc driver, ret = %d!\n", ret);
goto out_free_priv;
- } return 0; -out_free_priv:
- free(priv);
- wd_ecc_setting.priv = NULL; out_clear_pool: wd_uninit_async_request_pool(&wd_ecc_setting.pool); out_clear_sched:
@@ -211,17 +198,14 @@ static void wd_ecc_common_uninit(void) return; }
- /* driver uninit */
- wd_ecc_setting.driver->exit(wd_ecc_setting.priv);
- free(wd_ecc_setting.priv);
- wd_ecc_setting.priv = NULL;
/* uninit async request pool */ wd_uninit_async_request_pool(&wd_ecc_setting.pool); /* unset config, sched, driver */ wd_clear_sched(&wd_ecc_setting.sched);
- wd_clear_ctx_config(&wd_ecc_setting.config);
- wd_alg_uninit_driver(&wd_ecc_setting.config,
wd_ecc_setting.driver,
} int wd_ecc_init(struct wd_ctx_config *config, struct wd_sched *sched)&wd_ecc_setting.priv);
@@ -239,14 +223,20 @@ int wd_ecc_init(struct wd_ctx_config *config, struct wd_sched *sched) if (ret) goto out_clear_init;
- ret = wd_ecc_common_init(config, sched);
- ret = wd_ecc_open_driver(); if (ret) goto out_clear_init;
- ret = wd_ecc_common_init(config, sched);
- if (ret)
goto out_close_driver;
+out_close_driver:wd_alg_set_init(&wd_ecc_setting.status); return 0;
- wd_ecc_close_driver(); out_clear_init: wd_alg_clear_init(&wd_ecc_setting.status); return ret;
@@ -255,11 +245,14 @@ out_clear_init: void wd_ecc_uninit(void) { wd_ecc_common_uninit();
- wd_ecc_close_driver(); wd_alg_clear_init(&wd_ecc_setting.status); } int wd_ecc_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params) {
- struct wd_ctx_nums ecc_ctx_num[WD_EC_OP_MAX] = {0};
- struct wd_ctx_params ecc_ctx_params = {0}; bool flag; int ret; @@ -275,19 +268,66 @@ int wd_ecc_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_para goto out_clear_init; }
- /*
* Driver lib file path could set by env param.
* than open tham by wd_dlopen_drv()
* default dir in the /root/lib/xxx.so and then dlopen
*/
- wd_ecc_setting.dlh_list = wd_dlopen_drv(NULL);
- if (!wd_ecc_setting.dlh_list) {
WD_ERR("failed to open driver lib files!\n");
ret = -WD_EINVAL;
goto out_clear_init;
- }
+res_retry:
- memset(&wd_ecc_setting.config, 0, sizeof(struct wd_ctx_config_internal));
- /* Get alg driver and dev name */
- wd_ecc_setting.driver = wd_alg_drv_bind(task_type, alg);
- if (!wd_ecc_setting.driver) {
WD_ERR("failed to bind a valid driver!\n");
ret = -WD_EINVAL;
goto out_dlopen;
- }
- ret = wd_ctx_param_init(&ecc_ctx_params, ctx_params,
ecc_ctx_num, wd_ecc_setting.driver,
WD_EC_OP_MAX);
- if (ret) {
if (ret == -WD_EAGAIN) {
wd_disable_drv(wd_ecc_setting.driver);
wd_alg_drv_unbind(wd_ecc_setting.driver);
goto res_retry;
}
goto out_driver;
- }
wd_ecc_init_attrs.alg = alg; wd_ecc_init_attrs.sched_type = sched_type;
- wd_ecc_init_attrs.ctx_params = ctx_params ? ctx_params : &wd_ecc_ctx_params;
- wd_ecc_init_attrs.driver = wd_ecc_setting.driver;
- wd_ecc_init_attrs.ctx_params = &ecc_ctx_params; wd_ecc_init_attrs.alg_init = wd_ecc_common_init; wd_ecc_init_attrs.alg_poll_ctx = wd_ecc_poll_ctx; ret = wd_alg_attrs_init(&wd_ecc_init_attrs);
- if (ret)
goto out_clear_init;
- if (ret) {
if (ret == -WD_ENODEV) {
wd_disable_drv(wd_ecc_setting.driver);
wd_alg_drv_unbind(wd_ecc_setting.driver);
goto res_retry;
}
WD_ERR("failed to init alg attrs!\n");
goto out_driver;
- } wd_alg_set_init(&wd_ecc_setting.status); return 0; +out_driver:
- wd_alg_drv_unbind(wd_ecc_setting.driver);
+out_dlopen:
- wd_dlclose_drv(wd_ecc_setting.dlh_list); out_clear_init: wd_alg_clear_init(&wd_ecc_setting.status); return ret;
@@ -297,6 +337,9 @@ void wd_ecc_uninit2(void) { wd_ecc_common_uninit(); wd_alg_attrs_uninit(&wd_ecc_init_attrs);
- wd_alg_drv_unbind(wd_ecc_setting.driver);
- wd_dlclose_drv(wd_ecc_setting.dlh_list);
- wd_ecc_setting.dlh_list = NULL; wd_alg_clear_init(&wd_ecc_setting.status); } @@ -1098,6 +1141,12 @@ handle_t wd_ecc_alloc_sess(struct wd_ecc_sess_setup *setup) if (setup_param_check(setup)) return (handle_t)0;
- ret = wd_drv_alg_support(setup->alg, wd_ecc_setting.driver);
- if (!ret) {
WD_ERR("failed to support this algorithm: %s!\n", setup->alg);
return (handle_t)0;
- }
sess = calloc(1, sizeof(struct wd_ecc_sess)); if (!sess) return (handle_t)0;
.
After adding the rsa module of the init2 interface, combine it dynamically load the initialization part, transform HiSilicon HPRE driven, and implemented using the dynamic loading function Connection between driver and algorithm layer.
Signed-off-by: Weili Qian qianweili@huawei.com --- drv/hisi_hpre.c | 32 ++++--- include/drv/wd_dh_drv.h | 3 +- include/drv/wd_rsa_drv.h | 27 ------ wd_dh.c | 3 +- wd_rsa.c | 175 ++++++++++++++++++++++++--------------- 5 files changed, 131 insertions(+), 109 deletions(-)
diff --git a/drv/hisi_hpre.c b/drv/hisi_hpre.c index a08e5e6..8ecb950 100644 --- a/drv/hisi_hpre.c +++ b/drv/hisi_hpre.c @@ -496,8 +496,9 @@ out: return -WD_EINVAL; }
-static int hpre_rsa_dh_init(struct wd_ctx_config_internal *config, void *priv, const char *alg_name) +static int hpre_rsa_dh_init(void *conf, void *priv) { + struct wd_ctx_config_internal *config = (struct wd_ctx_config_internal *)conf; struct hisi_hpre_ctx *hpre_ctx = (struct hisi_hpre_ctx *)priv; struct hisi_qm_priv qm_priv; int ret; @@ -649,16 +650,6 @@ static int rsa_recv(handle_t ctx, void *rsa_msg) return 0; }
-static struct wd_rsa_driver rsa_hisi_hpre = { - .drv_name = "hisi_hpre", - .alg_name = "rsa", - .drv_ctx_size = sizeof(struct hisi_hpre_ctx), - .init = hpre_rsa_dh_init, - .exit = hpre_exit, - .send = rsa_send, - .recv = rsa_recv, -}; - static int fill_dh_xp_params(struct wd_dh_msg *msg, struct hisi_hpre_sqe *hw_msg) { @@ -2479,6 +2470,20 @@ static struct wd_alg_driver hpre_alg_driver[] = { GEN_HPRE_ALG_DRIVER("x448"), };
+static struct wd_alg_driver hpre_rsa_driver = { + .drv_name = "hisi_hpre", + .alg_name = "rsa", + .priority = UADK_ALG_HW, + .priv_size = sizeof(struct hisi_hpre_ctx), + .queue_num = HPRE_CTX_Q_NUM_DEF, + .op_type_num = 1, + .fallback = 0, + .init = hpre_rsa_dh_init, + .exit = hpre_exit, + .send = rsa_send, + .recv = rsa_recv, +}; + static void __attribute__((constructor)) hisi_hpre_probe(void) { int alg_num = ARRAY_SIZE(hpre_alg_driver); @@ -2486,6 +2491,9 @@ static void __attribute__((constructor)) hisi_hpre_probe(void)
WD_INFO("Info: register HPRE alg drivers!\n");
+ ret = wd_alg_driver_register(&hpre_rsa_driver); + if (ret) + WD_ERR("failed to register HPRE rsa driver!\n"); for (i = 0; i < alg_num; i++) { ret = wd_alg_driver_register(&hpre_alg_driver[i]); if (ret) @@ -2500,7 +2508,7 @@ static void __attribute__((destructor)) hisi_hpre_remove(void)
for (i = 0; i < alg_num; i++) wd_alg_driver_unregister(&hpre_alg_driver[i]); + wd_alg_driver_unregister(&hpre_rsa_driver); }
-WD_RSA_SET_DRIVER(rsa_hisi_hpre); WD_DH_SET_DRIVER(dh_hisi_hpre); diff --git a/include/drv/wd_dh_drv.h b/include/drv/wd_dh_drv.h index 64144e1..0bf8b06 100644 --- a/include/drv/wd_dh_drv.h +++ b/include/drv/wd_dh_drv.h @@ -28,8 +28,7 @@ struct wd_dh_driver { const char *drv_name; const char *alg_name; __u32 drv_ctx_size; - int (*init)(struct wd_ctx_config_internal *config, void *priv, - const char *alg_name); + int (*init)(void *conf, void *priv); void (*exit)(void *priv); int (*send)(handle_t sess, void *dh_msg); int (*recv)(handle_t sess, void *dh_msg); diff --git a/include/drv/wd_rsa_drv.h b/include/drv/wd_rsa_drv.h index baf112f..d231ecf 100644 --- a/include/drv/wd_rsa_drv.h +++ b/include/drv/wd_rsa_drv.h @@ -49,35 +49,8 @@ struct wd_rsa_msg { __u8 *key; /* Input key VA pointer, should be DMA buffer */ };
-struct wd_rsa_driver { - const char *drv_name; - const char *alg_name; - __u32 drv_ctx_size; - int (*init)(struct wd_ctx_config_internal *config, void *priv, - const char *alg_name); - void (*exit)(void *priv); - int (*send)(handle_t sess, void *rsa_msg); - int (*recv)(handle_t sess, void *rsa_msg); -}; - -void wd_rsa_set_driver(struct wd_rsa_driver *drv); -struct wd_rsa_driver *wd_rsa_get_driver(void); struct wd_rsa_msg *wd_rsa_get_msg(__u32 idx, __u32 tag);
-#ifdef WD_STATIC_DRV -#define WD_RSA_SET_DRIVER(drv) \ -struct wd_rsa_driver *wd_rsa_get_driver(void) \ -{ \ - return &drv; \ -} -#else -#define WD_RSA_SET_DRIVER(drv) \ -static void __attribute__((constructor)) set_driver_rsa(void) \ -{ \ - wd_rsa_set_driver(&(drv)); \ -} -#endif - #ifdef __cplusplus } #endif diff --git a/wd_dh.c b/wd_dh.c index d861b34..dd6c921 100644 --- a/wd_dh.c +++ b/wd_dh.c @@ -127,8 +127,7 @@ static int wd_dh_common_init(struct wd_ctx_config *config, struct wd_sched *sche }
wd_dh_setting.priv = priv; - ret = wd_dh_setting.driver->init(&wd_dh_setting.config, priv, - wd_dh_setting.driver->alg_name); + ret = wd_dh_setting.driver->init(&wd_dh_setting.config, priv); if (ret < 0) { WD_ERR("failed to init dh driver, ret= %d!\n", ret); goto out_free_priv; diff --git a/wd_rsa.c b/wd_rsa.c index 99bfe48..77fe5c0 100644 --- a/wd_rsa.c +++ b/wd_rsa.c @@ -74,56 +74,60 @@ static struct wd_rsa_setting { enum wd_status status; struct wd_ctx_config_internal config; struct wd_sched sched; - void *sched_ctx; - const struct wd_rsa_driver *driver; + struct wd_async_msg_pool pool; + struct wd_alg_driver *driver; void *priv; void *dlhandle; - struct wd_async_msg_pool pool; + void *dlh_list; } wd_rsa_setting;
struct wd_env_config wd_rsa_env_config; static struct wd_init_attrs wd_rsa_init_attrs;
-static struct wd_ctx_nums wd_rsa_ctx_num[] = { - {1, 1}, {} -}; - -static struct wd_ctx_params wd_rsa_ctx_params = { - .op_type_num = 1, - .ctx_set_num = wd_rsa_ctx_num, - .bmp = NULL, -}; - -#ifdef WD_STATIC_DRV -static void wd_rsa_set_static_drv(void) +static void wd_rsa_close_driver(void) { - wd_rsa_setting.driver = wd_rsa_get_driver(); - if (!wd_rsa_setting.driver) - WD_ERR("failed to get rsa driver!\n"); -} -#else -static void __attribute__((constructor)) wd_rsa_open_driver(void) -{ - wd_rsa_setting.dlhandle = dlopen("libhisi_hpre.so", RTLD_NOW); if (!wd_rsa_setting.dlhandle) - WD_ERR("failed to open libhisi_hpre.so, %s\n", dlerror()); -} + return;
-static void __attribute__((destructor)) wd_rsa_close_driver(void) -{ - if (wd_rsa_setting.dlhandle) - dlclose(wd_rsa_setting.dlhandle); + wd_release_drv(wd_rsa_setting.driver); + dlclose(wd_rsa_setting.dlhandle); + wd_rsa_setting.dlhandle = NULL; } -#endif
-void wd_rsa_set_driver(struct wd_rsa_driver *drv) +static int wd_rsa_open_driver(void) { - if (!drv) { - WD_ERR("invalid: rsa drv is NULL!\n"); - return; + struct wd_alg_driver *driver = NULL; + char lib_path[PATH_STR_SIZE]; + const char *alg_name = "rsa"; + int ret; + + /* + * Compatible with the normal acquisition of device + * drivers in the init interface. + */ + if (wd_rsa_setting.dlh_list) + return 0; + + ret = wd_get_lib_file_path("libhisi_hpre.so", lib_path, false); + if (ret) + return ret; + + wd_rsa_setting.dlhandle = dlopen(lib_path, RTLD_NOW); + if (!wd_rsa_setting.dlhandle) { + WD_ERR("failed to open libhisi_hpre.so, %s!\n", dlerror()); + return -WD_EINVAL; }
- wd_rsa_setting.driver = drv; + driver = wd_request_drv(alg_name, false); + if (!driver) { + wd_rsa_close_driver(); + WD_ERR("failed to get %s driver support!\n", alg_name); + return -WD_EINVAL; + } + + wd_rsa_setting.driver = driver; + + return 0; }
static void wd_rsa_clear_status(void) @@ -133,7 +137,6 @@ static void wd_rsa_clear_status(void)
static int wd_rsa_common_init(struct wd_ctx_config *config, struct wd_sched *sched) { - void *priv; int ret;
ret = wd_set_epoll_en("WD_RSA_EPOLL_EN", @@ -149,10 +152,6 @@ static int wd_rsa_common_init(struct wd_ctx_config *config, struct wd_sched *sch if (ret < 0) goto out_clear_ctx_config;
-#ifdef WD_STATIC_DRV - wd_rsa_set_static_drv(); -#endif - /* fix me: sadly find we allocate async pool for every ctx */ ret = wd_init_async_request_pool(&wd_rsa_setting.pool, config->ctx_num, WD_POOL_MAX_ENTRIES, @@ -160,26 +159,14 @@ static int wd_rsa_common_init(struct wd_ctx_config *config, struct wd_sched *sch if (ret < 0) goto out_clear_sched;
- /* initialize ctx related resources in specific driver */ - priv = calloc(1, wd_rsa_setting.driver->drv_ctx_size); - if (!priv) { - ret = -WD_ENOMEM; + ret = wd_alg_init_driver(&wd_rsa_setting.config, + wd_rsa_setting.driver, + &wd_rsa_setting.priv); + if (ret) goto out_clear_pool; - } - - wd_rsa_setting.priv = priv; - ret = wd_rsa_setting.driver->init(&wd_rsa_setting.config, priv, - wd_rsa_setting.driver->alg_name); - if (ret < 0) { - WD_ERR("failed to init rsa driver, ret = %d!\n", ret); - goto out_free_priv; - }
return 0;
-out_free_priv: - free(priv); - wd_rsa_setting.priv = NULL; out_clear_pool: wd_uninit_async_request_pool(&wd_rsa_setting.pool); out_clear_sched: @@ -196,17 +183,14 @@ static void wd_rsa_common_uninit(void) return; }
- /* driver uninit */ - wd_rsa_setting.driver->exit(wd_rsa_setting.priv); - free(wd_rsa_setting.priv); - wd_rsa_setting.priv = NULL; - /* uninit async request pool */ wd_uninit_async_request_pool(&wd_rsa_setting.pool);
/* unset config, sched, driver */ wd_clear_sched(&wd_rsa_setting.sched); - wd_clear_ctx_config(&wd_rsa_setting.config); + wd_alg_uninit_driver(&wd_rsa_setting.config, + wd_rsa_setting.driver, + &wd_rsa_setting.priv); }
int wd_rsa_init(struct wd_ctx_config *config, struct wd_sched *sched) @@ -224,14 +208,20 @@ int wd_rsa_init(struct wd_ctx_config *config, struct wd_sched *sched) if (ret) goto out_clear_init;
- ret = wd_rsa_common_init(config, sched); + ret = wd_rsa_open_driver(); if (ret) goto out_clear_init;
+ ret = wd_rsa_common_init(config, sched); + if (ret) + goto out_close_driver; + wd_alg_set_init(&wd_rsa_setting.status);
return 0;
+out_close_driver: + wd_rsa_close_driver(); out_clear_init: wd_alg_clear_init(&wd_rsa_setting.status); return ret; @@ -240,11 +230,14 @@ out_clear_init: void wd_rsa_uninit(void) { wd_rsa_common_uninit(); + wd_rsa_close_driver(); wd_alg_clear_init(&wd_rsa_setting.status); }
int wd_rsa_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params) { + struct wd_ctx_nums rsa_ctx_num[WD_RSA_GENKEY] = {0}; + struct wd_ctx_params rsa_ctx_params = {0}; bool flag; int ret;
@@ -260,19 +253,66 @@ int wd_rsa_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_para goto out_clear_init; }
+ /* + * Driver lib file path could set by env param. + * than open tham by wd_dlopen_drv() + * default dir in the /root/lib/xxx.so and then dlopen + */ + wd_rsa_setting.dlh_list = wd_dlopen_drv(NULL); + if (!wd_rsa_setting.dlh_list) { + WD_ERR("failed to open driver lib files!\n"); + ret = -WD_EINVAL; + goto out_clear_init; + } + +res_retry: + memset(&wd_rsa_setting.config, 0, sizeof(struct wd_ctx_config_internal)); + + /* Get alg driver and dev name */ + wd_rsa_setting.driver = wd_alg_drv_bind(task_type, alg); + if (!wd_rsa_setting.driver) { + WD_ERR("failed to bind a valid driver!\n"); + ret = -WD_EINVAL; + goto out_dlopen; + } + + ret = wd_ctx_param_init(&rsa_ctx_params, ctx_params, + rsa_ctx_num, wd_rsa_setting.driver, + WD_RSA_GENKEY); + if (ret) { + if (ret == -WD_EAGAIN) { + wd_disable_drv(wd_rsa_setting.driver); + wd_alg_drv_unbind(wd_rsa_setting.driver); + goto res_retry; + } + goto out_driver; + } + wd_rsa_init_attrs.alg = alg; wd_rsa_init_attrs.sched_type = sched_type; - wd_rsa_init_attrs.ctx_params = ctx_params ? ctx_params : &wd_rsa_ctx_params; + wd_rsa_init_attrs.driver = wd_rsa_setting.driver; + wd_rsa_init_attrs.ctx_params = &rsa_ctx_params; wd_rsa_init_attrs.alg_init = wd_rsa_common_init; wd_rsa_init_attrs.alg_poll_ctx = wd_rsa_poll_ctx; ret = wd_alg_attrs_init(&wd_rsa_init_attrs); - if (ret) - goto out_clear_init; + if (ret) { + if (ret == -WD_ENODEV) { + wd_disable_drv(wd_rsa_setting.driver); + wd_alg_drv_unbind(wd_rsa_setting.driver); + goto res_retry; + } + WD_ERR("failed to init alg attrs!\n"); + goto out_driver; + }
wd_alg_set_init(&wd_rsa_setting.status);
return 0;
+out_driver: + wd_alg_drv_unbind(wd_rsa_setting.driver); +out_dlopen: + wd_dlclose_drv(wd_rsa_setting.dlh_list); out_clear_init: wd_alg_clear_init(&wd_rsa_setting.status); return ret; @@ -282,6 +322,9 @@ void wd_rsa_uninit2(void) { wd_rsa_common_uninit(); wd_alg_attrs_uninit(&wd_rsa_init_attrs); + wd_alg_drv_unbind(wd_rsa_setting.driver); + wd_dlclose_drv(wd_rsa_setting.dlh_list); + wd_rsa_setting.dlh_list = NULL; wd_alg_clear_init(&wd_rsa_setting.status); }
After adding the ecc module of the init2 interface, combine it dynamically load the initialization part, transform HiSilicon HPRE driven, and implemented using the dynamic loading function Connection between driver and algorithm layer.
Signed-off-by: Weili Qian qianweili@huawei.com --- drv/hisi_hpre.c | 32 +++++--- include/drv/wd_dh_drv.h | 26 ------ wd_dh.c | 174 +++++++++++++++++++++++++--------------- 3 files changed, 129 insertions(+), 103 deletions(-)
diff --git a/drv/hisi_hpre.c b/drv/hisi_hpre.c index 8ecb950..4d21788 100644 --- a/drv/hisi_hpre.c +++ b/drv/hisi_hpre.c @@ -793,16 +793,6 @@ static int dh_recv(handle_t ctx, void *dh_msg) return 0; }
-static struct wd_dh_driver dh_hisi_hpre = { - .drv_name = "hisi_hpre", - .alg_name = "dh", - .drv_ctx_size = sizeof(struct hisi_hpre_ctx), - .init = hpre_rsa_dh_init, - .exit = hpre_exit, - .send = dh_send, - .recv = dh_recv, -}; - static int ecc_prepare_alg(struct wd_ecc_msg *msg, struct hisi_hpre_sqe *hw_msg) { @@ -2484,6 +2474,19 @@ static struct wd_alg_driver hpre_rsa_driver = { .recv = rsa_recv, };
+static struct wd_alg_driver hpre_dh_driver = { + .drv_name = "hisi_hpre", + .alg_name = "dh", + .priority = UADK_ALG_HW, + .priv_size = sizeof(struct hisi_hpre_ctx), + .queue_num = HPRE_CTX_Q_NUM_DEF, + .op_type_num = 1, + .fallback = 0, + .init = hpre_rsa_dh_init, + .exit = hpre_exit, + .send = dh_send, + .recv = dh_recv, +}; static void __attribute__((constructor)) hisi_hpre_probe(void) { int alg_num = ARRAY_SIZE(hpre_alg_driver); @@ -2494,6 +2497,11 @@ static void __attribute__((constructor)) hisi_hpre_probe(void) ret = wd_alg_driver_register(&hpre_rsa_driver); if (ret) WD_ERR("failed to register HPRE rsa driver!\n"); + + ret = wd_alg_driver_register(&hpre_dh_driver); + if (ret) + WD_ERR("failed to register HPRE dh driver!\n"); + for (i = 0; i < alg_num; i++) { ret = wd_alg_driver_register(&hpre_alg_driver[i]); if (ret) @@ -2508,7 +2516,7 @@ static void __attribute__((destructor)) hisi_hpre_remove(void)
for (i = 0; i < alg_num; i++) wd_alg_driver_unregister(&hpre_alg_driver[i]); + + wd_alg_driver_unregister(&hpre_dh_driver); wd_alg_driver_unregister(&hpre_rsa_driver); } - -WD_DH_SET_DRIVER(dh_hisi_hpre); diff --git a/include/drv/wd_dh_drv.h b/include/drv/wd_dh_drv.h index 0bf8b06..d205dc4 100644 --- a/include/drv/wd_dh_drv.h +++ b/include/drv/wd_dh_drv.h @@ -24,34 +24,8 @@ struct wd_dh_msg { __u8 result; /* Data format, denoted by WD error code */ };
-struct wd_dh_driver { - const char *drv_name; - const char *alg_name; - __u32 drv_ctx_size; - int (*init)(void *conf, void *priv); - void (*exit)(void *priv); - int (*send)(handle_t sess, void *dh_msg); - int (*recv)(handle_t sess, void *dh_msg); -}; - -void wd_dh_set_driver(struct wd_dh_driver *drv); -struct wd_dh_driver *wd_dh_get_driver(void); struct wd_dh_msg *wd_dh_get_msg(__u32 idx, __u32 tag);
-#ifdef WD_STATIC_DRV -#define WD_DH_SET_DRIVER(drv) \ -struct wd_dh_driver *wd_dh_get_driver(void) \ -{ \ - return &drv; \ -} -#else -#define WD_DH_SET_DRIVER(drv) \ -static void __attribute__((constructor)) set_driver_dh(void) \ -{ \ - wd_dh_set_driver(&(drv)); \ -} -#endif - #ifdef __cplusplus } #endif diff --git a/wd_dh.c b/wd_dh.c index dd6c921..d45ac89 100644 --- a/wd_dh.c +++ b/wd_dh.c @@ -33,56 +33,60 @@ static struct wd_dh_setting { enum wd_status status; struct wd_ctx_config_internal config; struct wd_sched sched; - void *sched_ctx; - const struct wd_dh_driver *driver; + struct wd_async_msg_pool pool; + struct wd_alg_driver *driver; void *priv; void *dlhandle; - struct wd_async_msg_pool pool; + void *dlh_list; } wd_dh_setting;
struct wd_env_config wd_dh_env_config; static struct wd_init_attrs wd_dh_init_attrs;
-static struct wd_ctx_nums wd_dh_ctx_num[] = { - {1, 1}, {} -}; - -static struct wd_ctx_params wd_dh_ctx_params = { - .op_type_num = 1, - .ctx_set_num = wd_dh_ctx_num, - .bmp = NULL, -}; - -#ifdef WD_STATIC_DRV -static void wd_dh_set_static_drv(void) +static void wd_dh_close_driver(void) { - wd_dh_setting.driver = wd_dh_get_driver(); - if (!wd_dh_setting.driver) - WD_ERR("failed to get dh driver!\n"); -} -#else -static void __attribute__((constructor)) wd_dh_open_driver(void) -{ - wd_dh_setting.dlhandle = dlopen("libhisi_hpre.so", RTLD_NOW); if (!wd_dh_setting.dlhandle) - WD_ERR("failed to open libhisi_hpre.so, %s\n", dlerror()); -} + return;
-static void __attribute__((destructor)) wd_dh_close_driver(void) -{ - if (wd_dh_setting.dlhandle) - dlclose(wd_dh_setting.dlhandle); + wd_release_drv(wd_dh_setting.driver); + dlclose(wd_dh_setting.dlhandle); + wd_dh_setting.dlhandle = NULL; } -#endif
-void wd_dh_set_driver(struct wd_dh_driver *drv) +static int wd_dh_open_driver(void) { - if (!drv) { - WD_ERR("invalid: dh drv is NULL!\n"); - return; + struct wd_alg_driver *driver = NULL; + char lib_path[PATH_STR_SIZE]; + const char *alg_name = "dh"; + int ret; + + /* + * Compatible with the normal acquisition of device + * drivers in the init interface. + */ + if (wd_dh_setting.dlh_list) + return 0; + + ret = wd_get_lib_file_path("libhisi_hpre.so", lib_path, false); + if (ret) + return ret; + + wd_dh_setting.dlhandle = dlopen(lib_path, RTLD_NOW); + if (!wd_dh_setting.dlhandle) { + WD_ERR("failed to open libhisi_hpre.so, %s!\n", dlerror()); + return -WD_EINVAL; }
- wd_dh_setting.driver = drv; + driver = wd_request_drv(alg_name, false); + if (!driver) { + wd_dh_close_driver(); + WD_ERR("failed to get %s driver support\n", alg_name); + return -WD_EINVAL; + } + + wd_dh_setting.driver = driver; + + return 0; }
static void wd_dh_clear_status(void) @@ -92,7 +96,6 @@ static void wd_dh_clear_status(void)
static int wd_dh_common_init(struct wd_ctx_config *config, struct wd_sched *sched) { - void *priv; int ret;
ret = wd_set_epoll_en("WD_DH_EPOLL_EN", @@ -108,10 +111,6 @@ static int wd_dh_common_init(struct wd_ctx_config *config, struct wd_sched *sche if (ret) goto out_clear_ctx_config;
-#ifdef WD_STATIC_DRV - wd_dh_set_static_drv(); -#endif - /* initialize async request pool */ ret = wd_init_async_request_pool(&wd_dh_setting.pool, config->ctx_num, WD_POOL_MAX_ENTRIES, @@ -119,25 +118,14 @@ static int wd_dh_common_init(struct wd_ctx_config *config, struct wd_sched *sche if (ret) goto out_clear_sched;
- /* initialize ctx related resources in specific driver */ - priv = calloc(1, wd_dh_setting.driver->drv_ctx_size); - if (!priv) { - ret = -WD_ENOMEM; + ret = wd_alg_init_driver(&wd_dh_setting.config, + wd_dh_setting.driver, + &wd_dh_setting.priv); + if (ret) goto out_clear_pool; - } - - wd_dh_setting.priv = priv; - ret = wd_dh_setting.driver->init(&wd_dh_setting.config, priv); - if (ret < 0) { - WD_ERR("failed to init dh driver, ret= %d!\n", ret); - goto out_free_priv; - }
return 0;
-out_free_priv: - free(priv); - wd_dh_setting.priv = NULL; out_clear_pool: wd_uninit_async_request_pool(&wd_dh_setting.pool); out_clear_sched: @@ -154,17 +142,14 @@ static void wd_dh_common_uninit(void) return; }
- /* driver uninit */ - wd_dh_setting.driver->exit(wd_dh_setting.priv); - free(wd_dh_setting.priv); - wd_dh_setting.priv = NULL; - /* uninit async request pool */ wd_uninit_async_request_pool(&wd_dh_setting.pool);
/* unset config, sched, driver */ wd_clear_sched(&wd_dh_setting.sched); - wd_clear_ctx_config(&wd_dh_setting.config); + wd_alg_uninit_driver(&wd_dh_setting.config, + wd_dh_setting.driver, + &wd_dh_setting.priv); }
int wd_dh_init(struct wd_ctx_config *config, struct wd_sched *sched) @@ -182,14 +167,20 @@ int wd_dh_init(struct wd_ctx_config *config, struct wd_sched *sched) if (ret) goto out_clear_init;
- ret = wd_dh_common_init(config, sched); + ret = wd_dh_open_driver(); if (ret) goto out_clear_init;
+ ret = wd_dh_common_init(config, sched); + if (ret) + goto out_close_driver; + wd_alg_set_init(&wd_dh_setting.status);
return 0;
+out_close_driver: + wd_dh_close_driver(); out_clear_init: wd_alg_clear_init(&wd_dh_setting.status); return ret; @@ -198,11 +189,14 @@ out_clear_init: void wd_dh_uninit(void) { wd_dh_common_uninit(); + wd_dh_close_driver(); wd_alg_clear_init(&wd_dh_setting.status); }
int wd_dh_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params) { + struct wd_ctx_nums dh_ctx_num[WD_DH_PHASE2] = {0}; + struct wd_ctx_params dh_ctx_params = {0}; bool flag; int ret;
@@ -218,19 +212,66 @@ int wd_dh_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_param goto out_clear_init; }
+ /* + * Driver lib file path could set by env param. + * than open tham by wd_dlopen_drv() + * default dir in the /root/lib/xxx.so and then dlopen + */ + wd_dh_setting.dlh_list = wd_dlopen_drv(NULL); + if (!wd_dh_setting.dlh_list) { + WD_ERR("failed to open driver lib files!\n"); + ret = -WD_EINVAL; + goto out_clear_init; + } + +res_retry: + memset(&wd_dh_setting.config, 0, sizeof(struct wd_ctx_config_internal)); + + /* Get alg driver and dev name */ + wd_dh_setting.driver = wd_alg_drv_bind(task_type, alg); + if (!wd_dh_setting.driver) { + WD_ERR("fail to bind a valid driver.\n"); + ret = -WD_EINVAL; + goto out_dlopen; + } + + ret = wd_ctx_param_init(&dh_ctx_params, ctx_params, + dh_ctx_num, wd_dh_setting.driver, + WD_DH_PHASE2); + if (ret) { + if (ret == -WD_EAGAIN) { + wd_disable_drv(wd_dh_setting.driver); + wd_alg_drv_unbind(wd_dh_setting.driver); + goto res_retry; + } + goto out_driver; + } + wd_dh_init_attrs.alg = alg; wd_dh_init_attrs.sched_type = sched_type; - wd_dh_init_attrs.ctx_params = ctx_params ? ctx_params : &wd_dh_ctx_params; + wd_dh_init_attrs.driver = wd_dh_setting.driver; + wd_dh_init_attrs.ctx_params = &dh_ctx_params; wd_dh_init_attrs.alg_init = wd_dh_common_init; wd_dh_init_attrs.alg_poll_ctx = wd_dh_poll_ctx; ret = wd_alg_attrs_init(&wd_dh_init_attrs); - if (ret) - goto out_clear_init; + if (ret) { + if (ret == -WD_ENODEV) { + wd_disable_drv(wd_dh_setting.driver); + wd_alg_drv_unbind(wd_dh_setting.driver); + goto res_retry; + } + WD_ERR("failed to init alg attrs!\n"); + goto out_driver; + }
wd_alg_set_init(&wd_dh_setting.status);
return 0;
+out_driver: + wd_alg_drv_unbind(wd_dh_setting.driver); +out_dlopen: + wd_dlclose_drv(wd_dh_setting.dlh_list); out_clear_init: wd_alg_clear_init(&wd_dh_setting.status); return ret; @@ -240,6 +281,9 @@ void wd_dh_uninit2(void) { wd_dh_common_uninit(); wd_alg_attrs_uninit(&wd_dh_init_attrs); + wd_alg_drv_unbind(wd_dh_setting.driver); + wd_dlclose_drv(wd_dh_setting.dlh_list); + wd_dh_setting.dlh_list = NULL; wd_alg_clear_init(&wd_dh_setting.status); }