452 lines
12 KiB
Diff
452 lines
12 KiB
Diff
From 35e7cb7a5d3477ab7191ac601ddb89dddb80c271 Mon Sep 17 00:00:00 2001
|
|
From: Weili Qian <qianweili@huawei.com>
|
|
Date: Sat, 11 Feb 2023 15:19:33 +0800
|
|
Subject: [PATCH 15/28] uadk/rsa: adapt the rsa module to the dynamic loading
|
|
framework
|
|
|
|
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);
|
|
}
|
|
|
|
--
|
|
2.25.1
|
|
|