In order to simplify the calling logic of uadk, a new init2 interface is added to the cipher algorithm class, which includes hardware device resource initialization and scheduler initialization.
Longfang Liu (2): uadk/cipher: add the init2 interface for cipher uadk/sec: adapt the sec module to the dynamic loading framework
drv/hisi_sec.c | 102 ++++++++++-- include/drv/wd_aead_drv.h | 2 +- include/drv/wd_cipher_drv.h | 26 --- include/drv/wd_digest_drv.h | 2 +- include/wd_cipher.h | 29 ++++ libwd_crypto.map | 3 + wd_cipher.c | 305 +++++++++++++++++++++++++++--------- 7 files changed, 354 insertions(+), 115 deletions(-)
This set of interfaces puts resource initialization operations into the init2 interface, simplifying the initialization operations when users use the cipher algorithm.
Signed-off-by: Longfang Liu liulongfang@huawei.com --- include/wd_cipher.h | 29 ++++++++++ libwd_crypto.map | 3 + wd_cipher.c | 138 ++++++++++++++++++++++++++++++++++++-------- 3 files changed, 147 insertions(+), 23 deletions(-)
diff --git a/include/wd_cipher.h b/include/wd_cipher.h index 8e69852..301eded 100644 --- a/include/wd_cipher.h +++ b/include/wd_cipher.h @@ -105,6 +105,35 @@ struct wd_cipher_req { int wd_cipher_init(struct wd_ctx_config *config, struct wd_sched *sched); void wd_cipher_uninit(void);
+/** + * wd_cipher_init2_() - A simplify interface to initializate uadk + * encryption and decryption. This interface keeps most functions of + * wd_cipher_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 driver's default. + * Please do not use this interface with wd_cipher_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: Task types, including soft computing, hardware and hybrid computing. + * @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_cipher_init2(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params); + +#define wd_cipher_init2_(alg, sched_type, task_type) \ + wd_cipher_init2(alg, sched_type, task_type, NULL) + +/** + * wd_cipher_uninit2() - Uninitialise ctx configuration and scheduler. + */ +void wd_cipher_uninit2(void); + /** * wd_cipher_alloc_sess() Allocate a wd cipher session * @ setup Parameters to setup this session. diff --git a/libwd_crypto.map b/libwd_crypto.map index 5fadc53..f7d1aa0 100644 --- a/libwd_crypto.map +++ b/libwd_crypto.map @@ -2,6 +2,9 @@ UADK_CRYPTO_2.0 { global: wd_cipher_init; wd_cipher_uninit; + wd_cipher_init2; + wd_cipher_init2_; + wd_cipher_uninit2; wd_cipher_alloc_sess; wd_cipher_free_sess; wd_cipher_set_key; diff --git a/wd_cipher.c b/wd_cipher.c index b627c0d..95f075f 100644 --- a/wd_cipher.c +++ b/wd_cipher.c @@ -67,6 +67,17 @@ struct wd_cipher_sess { };
struct wd_env_config wd_cipher_env_config; +static struct wd_init_attrs wd_cipher_init_attrs; + +static struct wd_ctx_nums wd_cipher_ctx_num[] = { + {1, 1}, {} +}; + +static struct wd_ctx_params wd_cipher_ctx_params = { + .op_type_num = WD_CIPHER_DECRYPTION, + .ctx_set_num = wd_cipher_ctx_num, + .bmp = NULL, +};
#ifdef WD_STATIC_DRV static void wd_cipher_set_static_drv(void) @@ -228,38 +239,25 @@ void wd_cipher_free_sess(handle_t h_sess) free(sess); }
-int wd_cipher_init(struct wd_ctx_config *config, struct wd_sched *sched) +static int wd_cipher_common_init(struct wd_ctx_config *config, + struct wd_sched *sched) { void *priv; - bool flag; int ret;
- flag = wd_alg_try_init(&wd_cipher_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_CIPHER_EPOLL_EN", &wd_cipher_setting.config.epoll_en); if (ret < 0) - goto out_clear_init; + return ret;
ret = wd_init_ctx_config(&wd_cipher_setting.config, config); if (ret < 0) - goto out_clear_init; + return ret;
ret = wd_init_sched(&wd_cipher_setting.sched, sched); if (ret < 0) goto out_clear_ctx_config;
-#ifdef WD_STATIC_DRV - /* set driver */ - wd_cipher_set_static_drv(); -#endif - /* allocate async pool for every ctx */ ret = wd_init_async_request_pool(&wd_cipher_setting.pool, config->ctx_num, WD_POOL_MAX_ENTRIES, @@ -277,12 +275,10 @@ int wd_cipher_init(struct wd_ctx_config *config, struct wd_sched *sched)
ret = wd_cipher_setting.driver->init(&wd_cipher_setting.config, priv); if (ret < 0) { - WD_ERR("failed to do dirver init, ret = %d.\n", ret); + WD_ERR("failed to init cipher dirver!\n"); goto out_free_priv; }
- wd_alg_set_init(&wd_cipher_setting.status); - return 0;
out_free_priv: @@ -294,12 +290,10 @@ out_clear_sched: wd_clear_sched(&wd_cipher_setting.sched); out_clear_ctx_config: wd_clear_ctx_config(&wd_cipher_setting.config); -out_clear_init: - wd_alg_clear_init(&wd_cipher_setting.status); return ret; }
-void wd_cipher_uninit(void) +static void wd_cipher_common_uninit(void) { void *priv = wd_cipher_setting.priv;
@@ -310,9 +304,107 @@ void wd_cipher_uninit(void) wd_cipher_setting.priv = NULL; free(priv);
+ /* uninit async request pool */ wd_uninit_async_request_pool(&wd_cipher_setting.pool); + + /* unset config, sched, driver */ wd_clear_sched(&wd_cipher_setting.sched); wd_clear_ctx_config(&wd_cipher_setting.config); +} + +int wd_cipher_init(struct wd_ctx_config *config, struct wd_sched *sched) +{ + enum wd_status status; + bool flag; + int ret; + + wd_alg_get_init(&wd_cipher_setting.status, &status); + if (status == WD_INIT) { + WD_INFO("UADK cipher has been initialized with wd_cipher_init2()!\n"); + return 0; + } + + flag = wd_alg_try_init(&wd_cipher_setting.status); + if (!flag) + return 0; + + ret = wd_init_param_check(config, sched); + if (ret) + goto out_clear_init; + +#ifdef WD_STATIC_DRV + /* set driver */ + wd_cipher_set_static_drv(); +#endif + + ret = wd_cipher_common_init(config, sched); + if (ret) + goto out_clear_init; + + wd_alg_set_init(&wd_cipher_setting.status); + + return 0; + +out_clear_init: + wd_alg_clear_init(&wd_cipher_setting.status); + return ret; +} + +void wd_cipher_uninit(void) +{ + wd_cipher_common_uninit(); + wd_alg_clear_init(&wd_cipher_setting.status); +} + +int wd_cipher_init2(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params) +{ + enum wd_status status; + int ret = 0; + bool flag; + + wd_alg_get_init(&wd_cipher_setting.status, &status); + if (status == WD_INIT) { + WD_INFO("UADK cipher has been initialized with wd_cipher_init()!\n"); + return 0; + } + + flag = wd_alg_try_init(&wd_cipher_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_uninit; + } + + wd_cipher_init_attrs.alg = alg; + wd_cipher_init_attrs.sched_type = sched_type; + wd_cipher_init_attrs.ctx_params = ctx_params ? ctx_params : &wd_cipher_ctx_params; + wd_cipher_init_attrs.alg_init = wd_cipher_common_init; + wd_cipher_init_attrs.alg_poll_ctx = wd_cipher_poll_ctx; + ret = wd_alg_attrs_init(&wd_cipher_init_attrs); + if (ret) { + WD_ERR("fail to init alg attrs.\n"); + goto out_uninit; + } + + wd_alg_set_init(&wd_cipher_setting.status); + + return 0; + +out_uninit: + wd_alg_clear_init(&wd_cipher_setting.status); + return ret; +} + +void wd_cipher_uninit2(void) +{ + wd_cipher_common_uninit(); + + wd_alg_attrs_uninit(&wd_cipher_init_attrs); + wd_alg_clear_init(&wd_cipher_setting.status); }
After adding the cipher module of the init2 interface, combine it Dynamically load the initialization part, transform HiSilicon sec driven, and implemented using the dynamic loading function Connection between driver and algorithm layer.
Signed-off-by: Longfang Liu liulongfang@huawei.com --- drv/hisi_sec.c | 102 +++++++++++++++--- include/drv/wd_aead_drv.h | 2 +- include/drv/wd_cipher_drv.h | 26 ----- include/drv/wd_digest_drv.h | 2 +- wd_cipher.c | 205 ++++++++++++++++++++++++------------ 5 files changed, 226 insertions(+), 111 deletions(-)
diff --git a/drv/hisi_sec.c b/drv/hisi_sec.c index c30b653..5e7da98 100644 --- a/drv/hisi_sec.c +++ b/drv/hisi_sec.c @@ -80,6 +80,8 @@ #define WD_CIPHER_THEN_DIGEST 0x0 #define WD_DIGEST_THEN_CIPHER 0x1
+#define SEC_CTX_Q_NUM_DEF 1 + enum C_ALG { C_ALG_DES = 0x0, C_ALG_3DES = 0x1, @@ -508,9 +510,54 @@ static __u32 g_sec_hmac_full_len[WD_DIGEST_TYPE_MAX] = { SEC_HMAC_SHA512_MAC_LEN, SEC_HMAC_SHA512_224_MAC_LEN, SEC_HMAC_SHA512_256_MAC_LEN };
-int hisi_sec_init(struct wd_ctx_config_internal *config, void *priv); +int hisi_sec_init(void *conf, void *priv); void hisi_sec_exit(void *priv);
+static int hisi_sec_get_usage(void *param) +{ + return 0; +} + +#define GEN_SEC_ALG_DRIVER(sec_alg_name) \ +{\ + .drv_name = "hisi_sec2",\ + .alg_name = sec_alg_name,\ + .priority = UADK_ALG_HW,\ + .priv_size = sizeof(struct hisi_sec_ctx),\ + .queue_num = SEC_CTX_Q_NUM_DEF,\ + .op_type_num = 1,\ + .fallback = 0,\ + .init = hisi_sec_init,\ + .exit = hisi_sec_exit,\ + .get_usage = hisi_sec_get_usage,\ +} + +static struct wd_alg_driver cipher_alg_driver[] = { + GEN_SEC_ALG_DRIVER("ecb(aes)"), + GEN_SEC_ALG_DRIVER("cbc(aes)"), + GEN_SEC_ALG_DRIVER("xts(aes)"), + GEN_SEC_ALG_DRIVER("ecb(sm4)"), + GEN_SEC_ALG_DRIVER("cbc(sm4)"), + GEN_SEC_ALG_DRIVER("ctr(sm4)"), + GEN_SEC_ALG_DRIVER("xts(sm4)"), + GEN_SEC_ALG_DRIVER("ecb(des)"), + GEN_SEC_ALG_DRIVER("cbc(des)"), + GEN_SEC_ALG_DRIVER("ecb(des3_ede)"), + GEN_SEC_ALG_DRIVER("cbc(des3_ede)"), + + GEN_SEC_ALG_DRIVER("ctr(aes)"), + GEN_SEC_ALG_DRIVER("ofb(aes)"), + GEN_SEC_ALG_DRIVER("cfb(aes)"), + GEN_SEC_ALG_DRIVER("cbc-cs1(aes)"), + GEN_SEC_ALG_DRIVER("cbc-cs2(aes)"), + GEN_SEC_ALG_DRIVER("cbc-cs3(aes)"), + GEN_SEC_ALG_DRIVER("ofb(sm4)"), + GEN_SEC_ALG_DRIVER("cfb(sm4)"), + GEN_SEC_ALG_DRIVER("cbc-cs1(sm4)"), + GEN_SEC_ALG_DRIVER("cbc-cs2(sm4)"), + GEN_SEC_ALG_DRIVER("cbc-cs3(sm4)"), +}; + static void dump_sec_msg(void *msg, const char *alg) { struct wd_cipher_msg *cmsg; @@ -1064,16 +1111,6 @@ int hisi_sec_cipher_recv(handle_t ctx, void *cipher_msg) return 0; }
-static struct wd_cipher_driver hisi_cipher_driver = { - .drv_name = "hisi_sec2", - .alg_name = "cipher", - .drv_ctx_size = sizeof(struct hisi_sec_ctx), - .init = hisi_sec_init, - .exit = hisi_sec_exit, -}; - -WD_CIPHER_SET_DRIVER(hisi_cipher_driver); - static int fill_cipher_bd3_alg(struct wd_cipher_msg *msg, struct hisi_sec_sqe3 *sqe) { @@ -2526,11 +2563,15 @@ int hisi_sec_aead_recv_v3(handle_t ctx, void *aead_msg) static void hisi_sec_driver_adapter(struct hisi_qp *qp) { struct hisi_qm_queue_info q_info = qp->q_info; + int alg_num, i;
if (q_info.hw_type == HISI_QM_API_VER2_BASE) { WD_ERR("hisi sec init Kunpeng920!\n"); - hisi_cipher_driver.cipher_send = hisi_sec_cipher_send; - hisi_cipher_driver.cipher_recv = hisi_sec_cipher_recv; + alg_num = ARRAY_SIZE(cipher_alg_driver); + for (i = 0; i < alg_num; i++) { + cipher_alg_driver[i].send = hisi_sec_cipher_send; + cipher_alg_driver[i].recv = hisi_sec_cipher_recv; + }
hisi_digest_driver.digest_send = hisi_sec_digest_send; hisi_digest_driver.digest_recv = hisi_sec_digest_recv; @@ -2539,8 +2580,11 @@ static void hisi_sec_driver_adapter(struct hisi_qp *qp) hisi_aead_driver.aead_recv = hisi_sec_aead_recv; } else { WD_ERR("hisi sec init Kunpeng930!\n"); - hisi_cipher_driver.cipher_send = hisi_sec_cipher_send_v3; - hisi_cipher_driver.cipher_recv = hisi_sec_cipher_recv_v3; + alg_num = ARRAY_SIZE(cipher_alg_driver); + for (i = 0; i < alg_num; i++) { + cipher_alg_driver[i].send = hisi_sec_cipher_send_v3; + cipher_alg_driver[i].recv = hisi_sec_cipher_recv_v3; + }
hisi_digest_driver.digest_send = hisi_sec_digest_send_v3; hisi_digest_driver.digest_recv = hisi_sec_digest_recv_v3; @@ -2550,8 +2594,9 @@ static void hisi_sec_driver_adapter(struct hisi_qp *qp) } }
-int hisi_sec_init(struct wd_ctx_config_internal *config, void *priv) +int hisi_sec_init(void *conf, void *priv) { + struct wd_ctx_config_internal *config = conf; struct hisi_sec_ctx *sec_ctx = priv; struct hisi_qm_priv qm_priv; handle_t h_qp = 0; @@ -2609,3 +2654,28 @@ void hisi_sec_exit(void *priv) hisi_qm_free_qp(h_qp); } } + +static void __attribute__((constructor)) hisi_sec2_probe(void) +{ + int alg_num = ARRAY_SIZE(cipher_alg_driver); + int i, ret; + + WD_INFO("Info: register SEC alg drivers!\n"); + + for (i = 0; i < alg_num; i++) { + ret = wd_alg_driver_register(&cipher_alg_driver[i]); + if (ret) + WD_ERR("Error: register SEC %s failed!\n", + cipher_alg_driver[i].alg_name); + } +} + +static void __attribute__((destructor)) hisi_sec2_remove(void) +{ + int alg_num = ARRAY_SIZE(cipher_alg_driver); + int i; + + for (i = 0; i < alg_num; i++) + wd_alg_driver_unregister(&cipher_alg_driver[i]); +} + diff --git a/include/drv/wd_aead_drv.h b/include/drv/wd_aead_drv.h index 7c657f6..8446238 100644 --- a/include/drv/wd_aead_drv.h +++ b/include/drv/wd_aead_drv.h @@ -67,7 +67,7 @@ struct wd_aead_driver { const char *drv_name; const char *alg_name; __u32 drv_ctx_size; - int (*init)(struct wd_ctx_config_internal *config, void *priv); + int (*init)(void *conf, void *priv); void (*exit)(void *priv); int (*aead_send)(handle_t ctx, void *aead_msg); int (*aead_recv)(handle_t ctx, void *aead_msg); diff --git a/include/drv/wd_cipher_drv.h b/include/drv/wd_cipher_drv.h index 82fb89a..c6d8ddf 100644 --- a/include/drv/wd_cipher_drv.h +++ b/include/drv/wd_cipher_drv.h @@ -50,34 +50,8 @@ struct wd_cipher_msg { __u8 *out; };
-struct wd_cipher_driver { - const char *drv_name; - const char *alg_name; - __u32 drv_ctx_size; - int (*init)(struct wd_ctx_config_internal *config, void *priv); - void (*exit)(void *priv); - int (*cipher_send)(handle_t ctx, void *cipher_msg); - int (*cipher_recv)(handle_t ctx, void *cipher_msg); -}; - -void wd_cipher_set_driver(struct wd_cipher_driver *drv); -struct wd_cipher_driver *wd_cipher_get_driver(void); struct wd_cipher_msg *wd_cipher_get_msg(__u32 idx, __u32 tag);
-#ifdef WD_STATIC_DRV -#define WD_CIPHER_SET_DRIVER(drv) \ -struct wd_cipher_driver *wd_cipher_get_driver(void) \ -{ \ - return &drv; \ -} -#else -#define WD_CIPHER_SET_DRIVER(drv) \ -static void __attribute__((constructor)) set_cipher_driver(void) \ -{ \ - wd_cipher_set_driver(&(drv)); \ -} -#endif - #ifdef __cplusplus } #endif diff --git a/include/drv/wd_digest_drv.h b/include/drv/wd_digest_drv.h index 586588b..96b32e2 100644 --- a/include/drv/wd_digest_drv.h +++ b/include/drv/wd_digest_drv.h @@ -55,7 +55,7 @@ struct wd_digest_driver { const char *drv_name; const char *alg_name; __u32 drv_ctx_size; - int (*init)(struct wd_ctx_config_internal *config, void *priv); + int (*init)(void *conf, void *priv); void (*exit)(void *priv); int (*digest_send)(handle_t ctx, void *digest_msg); int (*digest_recv)(handle_t ctx, void *digest_msg); diff --git a/wd_cipher.c b/wd_cipher.c index 95f075f..236814e 100644 --- a/wd_cipher.c +++ b/wd_cipher.c @@ -43,15 +43,24 @@ static const unsigned char des_weak_keys[DES_WEAK_KEY_NUM][DES_KEY_SIZE] = { {0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1} };
+static char *wd_cipher_alg_name[WD_CIPHER_ALG_TYPE_MAX][WD_CIPHER_MODE_TYPE_MAX] = { + {"ecb(sm4)", "cbc(sm4)", "ctr(sm4)", "xts(sm4)", "ofb(sm4)", + "cfb(sm4)", "cbc-cs1(sm4)", "cbc-cs2(sm4)", "cbc-cs3(sm4)"}, + {"ecb(aes)", "cbc(aes)", "ctr(aes)", "xts(aes)", "ofb(aes)", + "cfb(aes)", "cbc-cs1(aes)", "cbc-cs2(aes)", "cbc-cs3(aes)"}, + {"cbc(des)", "ecb(des)",}, + {"cbc(des3_ede)", "ecb(des3_ede)",} +}; + struct wd_cipher_setting { enum wd_status status; struct wd_ctx_config_internal config; struct wd_sched sched; - void *sched_ctx; - struct wd_cipher_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_cipher_setting;
struct wd_cipher_sess { @@ -69,41 +78,49 @@ struct wd_cipher_sess { struct wd_env_config wd_cipher_env_config; static struct wd_init_attrs wd_cipher_init_attrs;
-static struct wd_ctx_nums wd_cipher_ctx_num[] = { - {1, 1}, {} -}; - -static struct wd_ctx_params wd_cipher_ctx_params = { - .op_type_num = WD_CIPHER_DECRYPTION, - .ctx_set_num = wd_cipher_ctx_num, - .bmp = NULL, -}; - -#ifdef WD_STATIC_DRV -static void wd_cipher_set_static_drv(void) +static void wd_cipher_close_driver(void) { - wd_cipher_setting.driver = wd_cipher_get_driver(); - if (!wd_cipher_setting.driver) - WD_ERR("failed to get driver!\n"); + if (wd_cipher_setting.dlhandle) { + wd_release_drv(wd_cipher_setting.driver); + dlclose(wd_cipher_setting.dlhandle); + wd_cipher_setting.dlhandle = NULL; + } } -#else -static void __attribute__((constructor)) wd_cipher_open_driver(void) + +static int wd_cipher_open_driver(void) { - wd_cipher_setting.dlhandle = dlopen("libhisi_sec.so", RTLD_NOW); - if (!wd_cipher_setting.dlhandle) + struct wd_alg_driver *driver = NULL; + const char *alg_name = "cbc(aes)"; + char lib_path[PATH_STR_SIZE]; + int ret; + + /* + * Compatible with the normal acquisition of device + * drivers in the init interface + */ + if (wd_cipher_setting.dlh_list) + return 0; + + ret = wd_get_lib_file_path("libhisi_sec.so", lib_path, false); + if (ret) + return ret; + + wd_cipher_setting.dlhandle = dlopen(lib_path, RTLD_NOW); + if (!wd_cipher_setting.dlhandle) { WD_ERR("failed to open libhisi_sec.so, %s\n", dlerror()); -} + return -WD_EINVAL; + }
-static void __attribute__((destructor)) wd_cipher_close_driver(void) -{ - if (wd_cipher_setting.dlhandle) - dlclose(wd_cipher_setting.dlhandle); -} -#endif + driver = wd_request_drv(alg_name, false); + if (!driver) { + wd_cipher_close_driver(); + WD_ERR("failed to get %s driver support\n", alg_name); + return -WD_EINVAL; + }
-void wd_cipher_set_driver(struct wd_cipher_driver *drv) -{ - wd_cipher_setting.driver = drv; + wd_cipher_setting.driver = driver; + + return 0; }
static bool is_des_weak_key(const __u8 *key) @@ -196,6 +213,7 @@ int wd_cipher_set_key(handle_t h_sess, const __u8 *key, __u32 key_len) handle_t wd_cipher_alloc_sess(struct wd_cipher_sess_setup *setup) { struct wd_cipher_sess *sess = NULL; + bool ret;
if (unlikely(!setup)) { WD_ERR("invalid: cipher input setup is NULL!\n"); @@ -209,18 +227,35 @@ handle_t wd_cipher_alloc_sess(struct wd_cipher_sess_setup *setup) } memset(sess, 0, sizeof(struct wd_cipher_sess));
+ if (setup->alg >= WD_CIPHER_ALG_TYPE_MAX || + setup->mode >= WD_CIPHER_MODE_TYPE_MAX) { + WD_ERR("failed to check algorithm!\n"); + return (handle_t)0; + } + sess->alg_name = wd_cipher_alg_name[setup->alg][setup->mode]; sess->alg = setup->alg; sess->mode = setup->mode; + ret = wd_drv_alg_support(sess->alg_name, wd_cipher_setting.driver); + if (!ret) { + WD_ERR("failed to support this algorithm: %s!\n", sess->alg_name); + goto err_sess; + } + /* Some simple scheduler don't need scheduling parameters */ sess->sched_key = (void *)wd_cipher_setting.sched.sched_init( wd_cipher_setting.sched.h_sched_ctx, setup->sched_param); if (WD_IS_ERR(sess->sched_key)) { WD_ERR("failed to init session schedule key!\n"); - free(sess); - return (handle_t)0; + goto err_sess; }
return (handle_t)sess; + +err_sess: + if (sess->sched_key) + free(sess->sched_key); + free(sess); + return (handle_t)0; }
void wd_cipher_free_sess(handle_t h_sess) @@ -242,7 +277,6 @@ void wd_cipher_free_sess(handle_t h_sess) static int wd_cipher_common_init(struct wd_ctx_config *config, struct wd_sched *sched) { - void *priv; int ret;
ret = wd_set_epoll_en("WD_CIPHER_EPOLL_EN", @@ -265,25 +299,14 @@ static int wd_cipher_common_init(struct wd_ctx_config *config, if (ret < 0) goto out_clear_sched;
- /* init ctx related resources in specific driver */ - priv = calloc(1, wd_cipher_setting.driver->drv_ctx_size); - if (!priv) { - ret = -WD_ENOMEM; + ret = wd_alg_init_driver(&wd_cipher_setting.config, + wd_cipher_setting.driver, + &wd_cipher_setting.priv); + if (ret) goto out_clear_pool; - } - wd_cipher_setting.priv = priv; - - ret = wd_cipher_setting.driver->init(&wd_cipher_setting.config, priv); - if (ret < 0) { - WD_ERR("failed to init cipher dirver!\n"); - goto out_free_priv; - }
return 0;
-out_free_priv: - free(priv); - wd_cipher_setting.priv = NULL; out_clear_pool: wd_uninit_async_request_pool(&wd_cipher_setting.pool); out_clear_sched: @@ -300,16 +323,14 @@ static void wd_cipher_common_uninit(void) if (!priv) return;
- wd_cipher_setting.driver->exit(priv); - wd_cipher_setting.priv = NULL; - free(priv); - /* uninit async request pool */ wd_uninit_async_request_pool(&wd_cipher_setting.pool);
/* unset config, sched, driver */ wd_clear_sched(&wd_cipher_setting.sched); - wd_clear_ctx_config(&wd_cipher_setting.config); + + wd_alg_uninit_driver(&wd_cipher_setting.config, + wd_cipher_setting.driver, &priv); }
int wd_cipher_init(struct wd_ctx_config *config, struct wd_sched *sched) @@ -332,19 +353,20 @@ int wd_cipher_init(struct wd_ctx_config *config, struct wd_sched *sched) if (ret) goto out_clear_init;
-#ifdef WD_STATIC_DRV - /* set driver */ - wd_cipher_set_static_drv(); -#endif + ret = wd_cipher_open_driver(); + if (ret) + goto out_clear_init;
ret = wd_cipher_common_init(config, sched); if (ret) - goto out_clear_init; + goto out_close_driver;
wd_alg_set_init(&wd_cipher_setting.status);
return 0;
+out_close_driver: + wd_cipher_close_driver(); out_clear_init: wd_alg_clear_init(&wd_cipher_setting.status); return ret; @@ -353,11 +375,15 @@ out_clear_init: void wd_cipher_uninit(void) { wd_cipher_common_uninit(); + + wd_cipher_close_driver(); wd_alg_clear_init(&wd_cipher_setting.status); }
int wd_cipher_init2(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params) { + struct wd_ctx_nums cipher_ctx_num[WD_CIPHER_DECRYPTION + 1] = {0}; + struct wd_ctx_params cipher_ctx_params = {0}; enum wd_status status; int ret = 0; bool flag; @@ -379,21 +405,64 @@ int wd_cipher_init2(char *alg, __u32 sched_type, int task_type, struct wd_ctx_pa goto out_uninit; }
+ /* + * Driver lib file path could set by env param. + * than open tham by wd_dlopen_drv() + * use NULL means dynamic query path + */ + wd_cipher_setting.dlh_list = wd_dlopen_drv(NULL); + if (!wd_cipher_setting.dlh_list) { + WD_ERR("fail to open driver lib files.\n"); + goto out_uninit; + } + +res_retry: + memset(&wd_cipher_setting.config, 0, sizeof(struct wd_ctx_config_internal)); + + /* Get alg driver and dev name */ + wd_cipher_setting.driver = wd_alg_drv_bind(task_type, alg); + if (!wd_cipher_setting.driver) { + WD_ERR("fail to bind a valid driver.\n"); + goto out_dlopen; + } + + ret = wd_ctx_param_init(&cipher_ctx_params, ctx_params, + cipher_ctx_num, wd_cipher_setting.driver, + WD_CIPHER_DECRYPTION + 1); + if (ret) { + if (ret == -WD_EAGAIN) { + wd_disable_drv(wd_cipher_setting.driver); + wd_alg_drv_unbind(wd_cipher_setting.driver); + goto res_retry; + } + goto out_driver; + } + wd_cipher_init_attrs.alg = alg; wd_cipher_init_attrs.sched_type = sched_type; - wd_cipher_init_attrs.ctx_params = ctx_params ? ctx_params : &wd_cipher_ctx_params; + wd_cipher_init_attrs.driver = wd_cipher_setting.driver; + wd_cipher_init_attrs.ctx_params = &cipher_ctx_params; wd_cipher_init_attrs.alg_init = wd_cipher_common_init; wd_cipher_init_attrs.alg_poll_ctx = wd_cipher_poll_ctx; ret = wd_alg_attrs_init(&wd_cipher_init_attrs); if (ret) { + if (ret == -WD_ENODEV) { + wd_disable_drv(wd_cipher_setting.driver); + wd_alg_drv_unbind(wd_cipher_setting.driver); + goto res_retry; + } WD_ERR("fail to init alg attrs.\n"); - goto out_uninit; + goto out_driver; }
wd_alg_set_init(&wd_cipher_setting.status);
return 0;
+out_driver: + wd_alg_drv_unbind(wd_cipher_setting.driver); +out_dlopen: + wd_dlclose_drv(wd_cipher_setting.dlh_list); out_uninit: wd_alg_clear_init(&wd_cipher_setting.status); return ret; @@ -404,7 +473,9 @@ void wd_cipher_uninit2(void) wd_cipher_common_uninit();
wd_alg_attrs_uninit(&wd_cipher_init_attrs); - + wd_alg_drv_unbind(wd_cipher_setting.driver); + wd_dlclose_drv(wd_cipher_setting.dlh_list); + wd_cipher_setting.dlh_list = NULL; wd_alg_clear_init(&wd_cipher_setting.status); }
@@ -511,8 +582,8 @@ static int send_recv_sync(struct wd_ctx_internal *ctx, struct wd_msg_handle msg_handle; int ret;
- msg_handle.send = wd_cipher_setting.driver->cipher_send; - msg_handle.recv = wd_cipher_setting.driver->cipher_recv; + msg_handle.send = wd_cipher_setting.driver->send; + msg_handle.recv = wd_cipher_setting.driver->recv;
pthread_spin_lock(&ctx->lock); ret = wd_handle_msg_sync(&msg_handle, ctx->ctx, msg, NULL, @@ -591,7 +662,7 @@ int wd_do_cipher_async(handle_t h_sess, struct wd_cipher_req *req) fill_request_msg(msg, req, sess); msg->tag = msg_id;
- ret = wd_cipher_setting.driver->cipher_send(ctx->ctx, msg); + ret = wd_cipher_setting.driver->send(ctx->ctx, msg); if (unlikely(ret < 0)) { if (ret != -WD_EBUSY) WD_ERR("wd cipher async send err!\n"); @@ -639,7 +710,7 @@ int wd_cipher_poll_ctx(__u32 idx, __u32 expt, __u32 *count) ctx = config->ctxs + idx;
do { - ret = wd_cipher_setting.driver->cipher_recv(ctx->ctx, &resp_msg); + ret = wd_cipher_setting.driver->recv(ctx->ctx, &resp_msg); if (ret == -WD_EAGAIN) return ret; else if (ret < 0) {