Update the current driver adaptation and usage method, from fixed use of Hisilicon device driver to automatic registration of the driver according to the algorithm.
When the algorithm API layer uses the driver, it is no longer bound to the fixed device driver, but dynamically obtained and stored according to the algorithm query to use.
Update the driver and API layer of the zip and sec module, keep the function of the init interface unchanged, update the implementation of the init2 interface and match the dynamic loading function.
Changes v1 -> v2: - Fixed the compatibility method with the previous library file loading
liulongfang (5): uadk: Add driver dynamic loading function uadk: added ability to query supported algorithms uadk: improve the dynamic loading public framework uadk/zip: Adapt the zip module to the dynamic loading framework uadk/sec: adapt the sec module to the dynamic loading framework
Makefile.am | 4 +- drv/hisi_comp.c | 59 ++++- drv/hisi_sec.c | 125 ++++++++-- include/drv/wd_cipher_drv.h | 26 -- include/drv/wd_comp_drv.h | 27 --- include/wd.h | 12 + include/wd_alg.h | 95 ++++++++ include/wd_alg_common.h | 10 + include/wd_sched.h | 6 +- include/wd_util.h | 19 +- libwd.map | 8 + libwd_crypto.map | 3 + wd.c | 56 ++++- wd_alg.c | 258 ++++++++++++++++++++ wd_cipher.c | 233 ++++++++++++++---- wd_comp.c | 188 ++++++++------- wd_sched.c | 57 +++++ wd_util.c | 463 +++++++++++++++++++++++++++++++++++- 18 files changed, 1419 insertions(+), 230 deletions(-) create mode 100644 include/wd_alg.h create mode 100644 wd_alg.c
According to the logical layering of UADK, the device driver has been updated from the previous fixed binding HiSilicon accelerator to the dynamic registration method through the algorithm linked list method.
After the update, it can support the use of instruction acceleration and third-party device drivers.
Signed-off-by: Longfang Liu liulongfang@huawei.com --- Makefile.am | 4 +- include/wd_alg.h | 95 +++++++++++++++ include/wd_alg_common.h | 1 + libwd.map | 8 ++ wd_alg.c | 258 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 364 insertions(+), 2 deletions(-) create mode 100644 include/wd_alg.h create mode 100644 wd_alg.c
diff --git a/Makefile.am b/Makefile.am index 4ef5c2c..5b45626 100644 --- a/Makefile.am +++ b/Makefile.am @@ -33,7 +33,7 @@ AM_CFLAGS+= -DUADK_RELEASED_TIME=""Released ${MONTH} ${DAY}, ${YEAR}"" pkginclude_HEADERS = include/wd.h include/wd_cipher.h include/wd_aead.h \ include/wd_comp.h include/wd_dh.h include/wd_digest.h \ include/wd_rsa.h include/uacce.h include/wd_alg_common.h \ - include/wd_ecc.h include/wd_sched.h + include/wd_ecc.h include/wd_sched.h include/wd_alg.h
nobase_pkginclude_HEADERS = v1/wd.h v1/wd_cipher.h v1/wd_aead.h v1/uacce.h v1/wd_dh.h \ v1/wd_digest.h v1/wd_rsa.h v1/wd_bmm.h @@ -41,7 +41,7 @@ nobase_pkginclude_HEADERS = v1/wd.h v1/wd_cipher.h v1/wd_aead.h v1/uacce.h v1/wd lib_LTLIBRARIES=libwd.la libwd_comp.la libwd_crypto.la libhisi_zip.la \ libhisi_hpre.la libhisi_sec.la
-libwd_la_SOURCES=wd.c wd_mempool.c wd.h \ +libwd_la_SOURCES=wd.c wd_mempool.c wd.h wd_alg.c wd_alg.h \ v1/wd.c v1/wd.h v1/wd_adapter.c v1/wd_adapter.h \ v1/wd_rng.c v1/wd_rng.h \ v1/wd_rsa.c v1/wd_rsa.h \ diff --git a/include/wd_alg.h b/include/wd_alg.h new file mode 100644 index 0000000..55ac055 --- /dev/null +++ b/include/wd_alg.h @@ -0,0 +1,95 @@ +// SPDX-License-Identifier: Apache-2.0 +/* + * Copyright 2022 Huawei Technologies Co.,Ltd. All rights reserved. + */ + +#ifndef __WD_ALG_H +#define __WD_ALG_H +#include <errno.h> +#include <fcntl.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <syslog.h> +#include <unistd.h> +#include <asm/types.h> + +#ifdef __cplusplus +extern "C" { +#endif + +#define handle_t uintptr_t +enum alg_priority { + UADK_ALG_SOFT = 0x0, + UADK_ALG_CE_INSTR = 0x1, + UADK_ALG_SVE_INSTR = 0x2, + UADK_ALG_HW = 0x3 +}; + +/* + * @drv_name: name of the current device driver + * @alg_name: name of the algorithm supported by the driver + * @priority: priority of the type of algorithm supported by the driver + * @queue_num: number of device queues required by the device to + * execute the algorithm business + * @op_type_num: number of modes in which the device executes the + * algorithm business and requires queues to be executed separately + * @priv_size: parameter memory size passed between the internal + * interfaces of the driver + * @fallback: soft calculation driver handle when performing soft + * calculation supplement + * @init: callback interface for initializing device drivers + * @exit: callback interface for destroying device drivers + * @send: callback interface used to send task packets to + * hardware devices. + * @recv: callback interface used to retrieve the calculation + * result of the task packets from the hardware device. + * @get_usage: callback interface used to obtain the + * utilization rate of hardware devices. + */ +struct wd_alg_driver { + const char *drv_name; + const char *alg_name; + int priority; + int queue_num; + int op_type_num; + int priv_size; + handle_t fallback; + + int (*init)(void *conf, void *priv); + void (*exit)(void *priv); + int (*send)(handle_t ctx, void *drv_msg); + int (*recv)(handle_t ctx, void *drv_msg); + int (*get_usage)(void *param); +}; + +int wd_alg_driver_register(struct wd_alg_driver *drv); +void wd_alg_driver_unregister(struct wd_alg_driver *drv); + +struct wd_alg_list { + const char *alg_name; + const char *drv_name; + int priority; + bool available; + int refcnt; + + struct wd_alg_driver *drv; + struct wd_alg_list *next; +}; + +struct wd_alg_driver *wd_request_drv(const char *alg_name, bool hw_mask); +void wd_release_drv(struct wd_alg_driver *drv); + +bool wd_drv_alg_support(const char *alg_name, + struct wd_alg_driver *drv); +void wd_enable_drv(struct wd_alg_driver *drv); +void wd_disable_drv(struct wd_alg_driver *drv); + +struct wd_alg_list *wd_get_alg_head(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/wd_alg_common.h b/include/wd_alg_common.h index 56539cc..d04b046 100644 --- a/include/wd_alg_common.h +++ b/include/wd_alg_common.h @@ -10,6 +10,7 @@ #include <pthread.h> #include <stdbool.h> #include "wd.h" +#include "wd_alg.h"
#ifdef __cplusplus extern "C" { diff --git a/libwd.map b/libwd.map index 459f9ba..5522ec0 100644 --- a/libwd.map +++ b/libwd.map @@ -41,5 +41,13 @@ global: wd_add_dev_to_list; wd_find_dev_by_numa;
+ wd_alg_driver_register; + wd_alg_driver_unregister; + wd_request_drv; + wd_release_drv; + wd_drv_alg_support; + wd_enable_drv; + wd_disable_drv; + wd_get_alg_head; local: *; }; diff --git a/wd_alg.c b/wd_alg.c new file mode 100644 index 0000000..ea3ef65 --- /dev/null +++ b/wd_alg.c @@ -0,0 +1,258 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/* + * Copyright 2022 Huawei Technologies Co.,Ltd. All rights reserved. + */ + +#define _GNU_SOURCE +#include <ctype.h> +#include <dirent.h> +#include <errno.h> +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> +#include <pthread.h> + +#include "wd.h" +#include "wd_alg.h" + +#define SYS_CLASS_DIR "/sys/class/uacce" +static struct wd_alg_list alg_list_head; +static struct wd_alg_list *alg_list_tail = &alg_list_head; + +static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + +static bool wd_check_accel_dev(const char *drv_name) +{ + struct dirent *dev_dir; + DIR *wd_class; + + wd_class = opendir(SYS_CLASS_DIR); + if (!wd_class) { + WD_ERR("UADK framework isn't enabled in system!\n"); + return NULL; + } + + while ((dev_dir = readdir(wd_class)) != NULL) { + if (!strncmp(dev_dir->d_name, ".", LINUX_CRTDIR_SIZE) || + !strncmp(dev_dir->d_name, "..", LINUX_PRTDIR_SIZE)) + continue; + + if (!strncmp(dev_dir->d_name, drv_name, strlen(drv_name))) { + closedir(wd_class); + return true; + } + } + closedir(wd_class); + + return false; +} + +int wd_alg_driver_register(struct wd_alg_driver *drv) +{ + struct wd_alg_list *new_alg; + + if (!drv) { + WD_ERR("invalid: register drv is NULL!\n"); + return -WD_EINVAL; + } + + new_alg = calloc(1, sizeof(struct wd_alg_list)); + if (!new_alg) { + WD_ERR("faild to alloc alg driver memory!\n"); + return -WD_ENOMEM; + } + + new_alg->alg_name = drv->alg_name; + new_alg->drv_name = drv->drv_name; + new_alg->priority = drv->priority; + new_alg->drv = drv; + new_alg->refcnt = 0; + new_alg->next = NULL; + + if (drv->priority == UADK_ALG_HW) { + /* If not find dev, remove this driver node */ + new_alg->available = wd_check_accel_dev(drv->drv_name); + if (!new_alg->available) { + free(new_alg); + WD_ERR("faild to find alg driver's device!\n"); + return -WD_ENODEV; + } + } else { + /* Should find the CPU if not support SVE or CE */ + new_alg->available = true; + } + + pthread_mutex_lock(&mutex); + alg_list_tail->next = new_alg; + alg_list_tail = new_alg; + pthread_mutex_unlock(&mutex); + + return 0; +} + +void wd_alg_driver_unregister(struct wd_alg_driver *drv) +{ + struct wd_alg_list *npre = &alg_list_head; + struct wd_alg_list *pnext = npre->next; + + /* Alg driver list has no drivers */ + if (!pnext || !drv) + return; + + pthread_mutex_lock(&mutex); + while (pnext) { + if (!strcmp(drv->alg_name, pnext->alg_name) && + !strcmp(drv->drv_name, pnext->drv_name) && + drv->priority == pnext->priority) { + break; + } + npre = pnext; + pnext = pnext->next; + } + + /* It is used to locate the problem and ensure symmetrical use driver */ + if (pnext->refcnt > 0) + WD_ERR("driver<%s> still in used: %d\n", pnext->drv_name, pnext->refcnt); + + if (pnext == alg_list_tail) + alg_list_tail = npre; + + npre->next = pnext->next; + free(pnext); + pthread_mutex_unlock(&mutex); +} + +struct wd_alg_list *wd_get_alg_head(void) +{ + return &alg_list_head; +} + +bool wd_drv_alg_support(const char *alg_name, + struct wd_alg_driver *drv) +{ + struct wd_alg_list *head = &alg_list_head; + struct wd_alg_list *pnext = head->next; + + while (pnext) { + if (!strcmp(alg_name, pnext->alg_name) && + !strcmp(drv->drv_name, pnext->drv_name)) { + return true; + } + pnext = pnext->next; + } + + return false; +} + +void wd_enable_drv(struct wd_alg_driver *drv) +{ + struct wd_alg_list *head = &alg_list_head; + struct wd_alg_list *pnext = head->next; + + if (!pnext || !drv) + return; + + pthread_mutex_lock(&mutex); + while (pnext) { + if (!strcmp(drv->alg_name, pnext->alg_name) && + !strcmp(drv->drv_name, pnext->drv_name) && + drv->priority == pnext->priority) { + break; + } + pnext = pnext->next; + } + + if (drv->priority == UADK_ALG_HW) { + /* If not find dev, remove this driver node */ + pnext->available = wd_check_accel_dev(drv->drv_name); + } else { + /* Should find the CPU if not support SVE or CE */ + pnext->available = true; + } + pthread_mutex_unlock(&mutex); +} + +void wd_disable_drv(struct wd_alg_driver *drv) +{ + struct wd_alg_list *head = &alg_list_head; + struct wd_alg_list *pnext = head->next; + + if (!pnext || !drv) + return; + + pthread_mutex_lock(&mutex); + while (pnext) { + if (!strcmp(drv->alg_name, pnext->alg_name) && + !strcmp(drv->drv_name, pnext->drv_name) && + drv->priority == pnext->priority) { + break; + } + pnext = pnext->next; + } + + pnext->available = false; + pthread_mutex_unlock(&mutex); +} + +struct wd_alg_driver *wd_request_drv(const char *alg_name, bool hw_mask) +{ + struct wd_alg_list *head = &alg_list_head; + struct wd_alg_list *pnext = head->next; + struct wd_alg_list *select_node = NULL; + struct wd_alg_driver *drv = NULL; + int tmp_priority = -1; + + if (!pnext || !alg_name) { + WD_ERR("invalid: request alg param is error!\n"); + return NULL; + } + + /* Check the list to get an best driver */ + pthread_mutex_lock(&mutex); + while (pnext) { + /* hw_mask true mean not to used hardware dev */ + if (hw_mask && pnext->drv->priority == UADK_ALG_HW) { + pnext = pnext->next; + continue; + } + + if (!strcmp(alg_name, pnext->alg_name) && pnext->available && + pnext->drv->priority > tmp_priority) { + tmp_priority = pnext->drv->priority; + select_node = pnext; + drv = pnext->drv; + } + pnext = pnext->next; + } + + if (select_node) + select_node->refcnt++; + pthread_mutex_unlock(&mutex); + + return drv; +} + +void wd_release_drv(struct wd_alg_driver *drv) +{ + struct wd_alg_list *head = &alg_list_head; + struct wd_alg_list *pnext = head->next; + + if (!pnext || !drv) + return; + + pthread_mutex_lock(&mutex); + while (pnext) { + if (!strcmp(drv->alg_name, pnext->alg_name) && + !strcmp(drv->drv_name, pnext->drv_name) && + drv->priority == pnext->priority) { + break; + } + pnext = pnext->next; + } + + if (pnext->refcnt > 0) { + pnext->refcnt--; + } + pthread_mutex_unlock(&mutex); +} +
After the driver dynamic loading function is added, the corresponding function of querying all algorithms supported on the current UADK is added.
Signed-off-by: Longfang Liu liulongfang@huawei.com --- include/wd.h | 12 +++++++++++ wd.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-)
diff --git a/include/wd.h b/include/wd.h index e102fe2..21af7ff 100644 --- a/include/wd.h +++ b/include/wd.h @@ -29,6 +29,7 @@ extern "C" { #define LINUX_PRTDIR_SIZE 2 #define WD_CTX_CNT_NUM 1024 #define WD_IPC_KEY 0x500011 +#define CRYPTO_MAX_ALG_NAME 128
/* Required compiler attributes */ #define likely(x) __builtin_expect(!!(x), 1) @@ -578,6 +579,17 @@ bool wd_need_debug(void); */ bool wd_need_info(void);
+struct wd_capability { + char alg_name[CRYPTO_MAX_ALG_NAME]; + char drv_name[CRYPTO_MAX_ALG_NAME]; + int priority; + + struct wd_capability *next; +}; + +struct wd_capability *wd_get_alg_cap(void); +void wd_release_alg_cap(struct wd_capability *head); + #ifdef __cplusplus } #endif diff --git a/wd.c b/wd.c index 629c0df..c882c1f 100644 --- a/wd.c +++ b/wd.c @@ -19,7 +19,7 @@ #include <sched.h>
#include "wd.h" - +#include "wd_alg.h" #define SYS_CLASS_DIR "/sys/class/uacce"
enum UADK_LOG_LEVEL { @@ -882,3 +882,57 @@ char *wd_ctx_get_dev_name(handle_t h_ctx)
return ctx->dev_name; } + +void wd_release_alg_cap(struct wd_capability *head) +{ + struct wd_capability *cap_pnext = head; + struct wd_capability *cap_node = NULL; + + while (cap_pnext) { + cap_node = cap_pnext; + cap_pnext = cap_pnext->next; + free(cap_node); + } + + if (head) + free(head); +} + +struct wd_capability *wd_get_alg_cap(void) +{ + struct wd_alg_list *head = wd_get_alg_head(); + struct wd_alg_list *pnext = head->next; + struct wd_capability *cap_head = NULL; + struct wd_capability *cap_node = NULL; + struct wd_capability *cap_pnext = NULL; + int i = 0; + + while (pnext) { + cap_node = calloc(1, sizeof(struct wd_capability)); + if (!cap_head) { + WD_ERR("fail to alloc wd capability head\n"); + goto alloc_err; + } + + strcpy(cap_node->alg_name, pnext->alg_name); + strcpy(cap_node->drv_name, pnext->drv_name); + cap_node->priority = pnext->priority; + cap_node->next = NULL; + + cap_pnext->next = cap_node; + cap_pnext = cap_node; + pnext = pnext->next; + if (i == 0) { + cap_head = cap_node; + cap_pnext = cap_head; + } + i++; + } + + return cap_head; + +alloc_err: + wd_release_alg_cap(cap_head); + return NULL; +} +
After the dynamic loading function is added, device resource initialization, driver acquisition, and scheduler initialization functions need to be extracted into the public framework.
so that the algorithm can quickly adapt to the dynamic loading function internally.
Signed-off-by: Longfang Liu liulongfang@huawei.com --- include/wd_alg_common.h | 9 + include/wd_sched.h | 6 +- include/wd_util.h | 19 +- wd_comp.c | 2 +- wd_sched.c | 57 +++++ wd_util.c | 463 +++++++++++++++++++++++++++++++++++++++- 6 files changed, 551 insertions(+), 5 deletions(-)
diff --git a/include/wd_alg_common.h b/include/wd_alg_common.h index d04b046..99e584a 100644 --- a/include/wd_alg_common.h +++ b/include/wd_alg_common.h @@ -28,6 +28,12 @@ extern "C" { #define CTX_TYPE_INVALID 9999 #define POLL_TIME 1000
+enum alg_task_type { + TASK_MIX = 0x0, + TASK_HW, + TASK_INSTR, +}; + enum wd_ctx_mode { CTX_MODE_SYNC = 0, CTX_MODE_ASYNC, @@ -130,6 +136,9 @@ struct wd_sched { handle_t h_sched_ctx; };
+typedef int (*wd_alg_init)(struct wd_ctx_config *config, struct wd_sched *sched); +typedef int (*wd_alg_poll_ctx)(__u32 idx, __u32 expt, __u32 *count); + struct wd_datalist { void *data; __u32 len; diff --git a/include/wd_sched.h b/include/wd_sched.h index 2ae6103..a492d70 100644 --- a/include/wd_sched.h +++ b/include/wd_sched.h @@ -18,7 +18,11 @@ extern "C" { enum sched_policy_type { /* requests will be sent to ctxs one by one */ SCHED_POLICY_RR = 0, - SCHED_POLICY_BUTT + /* requests will no need ctxs */ + SCHED_POLICY_NONE, + /* requests will need a fixed ctx */ + SCHED_POLICY_SINGLE, + SCHED_POLICY_BUTT, };
struct sched_params { diff --git a/include/wd_util.h b/include/wd_util.h index d4b2814..937cca5 100644 --- a/include/wd_util.h +++ b/include/wd_util.h @@ -117,9 +117,12 @@ struct wd_msg_handle { struct wd_init_attrs { __u32 sched_type; char *alg; + struct wd_alg_driver *driver; struct wd_sched *sched; struct wd_ctx_params *ctx_params; struct wd_ctx_config *ctx_config; + wd_alg_init alg_init; + wd_alg_poll_ctx alg_poll_ctx; };
/* @@ -415,13 +418,25 @@ static inline void wd_alg_clear_init(enum wd_status *status) }
/** - * wd_alg_pre_init() - Request the ctxs and initialize the sched_domain + * wd_alg_attrs_init() - Request the ctxs and initialize the sched_domain * with the given devices list, ctxs number and numa mask. * @attrs: the algorithm initialization parameters. * * Return device if succeed and other error number if fail. */ -int wd_alg_pre_init(struct wd_init_attrs *attrs); +int wd_alg_attrs_init(struct wd_init_attrs *alg_init_attrs); +void wd_alg_attrs_uninit(struct wd_init_attrs *attrs); + +struct wd_alg_driver *wd_alg_drv_bind(int task_type, char *alg_name); +void wd_alg_drv_unbind(struct wd_alg_driver *drv); + +int wd_alg_init_driver(struct wd_ctx_config_internal *config, + struct wd_alg_driver *driver, void **drv_priv); +void wd_alg_uninit_driver(struct wd_ctx_config_internal *config, + struct wd_alg_driver *driver, void *drv_priv); + +void *wd_dlopen_drv(const char *cust_lib_dir); +int wd_dlclose_drv(void *dlh_list);
/** * wd_dfx_msg_cnt() - Message counter interface for ctx diff --git a/wd_comp.c b/wd_comp.c index cd938d9..bb142a7 100644 --- a/wd_comp.c +++ b/wd_comp.c @@ -230,7 +230,7 @@ int wd_comp_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_par wd_comp_sched->name = SCHED_RR_NAME; wd_comp_init_attrs.sched = wd_comp_sched;
- ret = wd_alg_pre_init(&wd_comp_init_attrs); + ret = wd_alg_attrs_init(&wd_comp_init_attrs); if (ret) goto out_freesched;
diff --git a/wd_sched.c b/wd_sched.c index 98f4cfd..7adc968 100644 --- a/wd_sched.c +++ b/wd_sched.c @@ -347,8 +347,60 @@ static int session_sched_poll_policy(handle_t h_sched_ctx, __u32 expect, __u32 * return 0; }
+static handle_t sched_none_init(handle_t h_sched_ctx, void *sched_param) +{ + return (handle_t)0; +} + +static __u32 sched_none_pick_next_ctx(handle_t sched_ctx, + void *sched_key, const int sched_mode) +{ + return 0; +} + +static int sched_none_poll_policy(handle_t h_sched_ctx, + __u32 expect, __u32 *count) +{ + return 0; +} + +static handle_t sched_single_init(handle_t h_sched_ctx, void *sched_param) +{ + return (handle_t)0; +} + +static __u32 sched_single_pick_next_ctx(handle_t sched_ctx, + void *sched_key, const int sched_mode) +{ +#define CTX_ASYNC 1 +#define CTX_SYNC 0 + + if (sched_mode) + return CTX_ASYNC; + else + return CTX_SYNC; +} + +static int sched_single_poll_policy(handle_t h_sched_ctx, + __u32 expect, __u32 *count) +{ + return 0; +} + static struct wd_sched sched_table[SCHED_POLICY_BUTT] = { { + .name = "None scheduler", + .sched_policy = SCHED_POLICY_SINGLE, + .sched_init = sched_none_init, + .pick_next_ctx = sched_none_pick_next_ctx, + .poll_policy = sched_none_poll_policy, + },{ + .name = "Single scheduler", + .sched_policy = SCHED_POLICY_SINGLE, + .sched_init = sched_single_init, + .pick_next_ctx = sched_single_pick_next_ctx, + .poll_policy = sched_single_poll_policy, + },{ .name = "RR scheduler", .sched_policy = SCHED_POLICY_RR, .sched_init = session_sched_init, @@ -523,6 +575,10 @@ struct wd_sched *wd_sched_rr_alloc(__u8 sched_type, __u8 type_num, return NULL; }
+ if (sched_type == SCHED_POLICY_NONE || + sched_type == SCHED_POLICY_SINGLE) + goto simple_ok; + sched_ctx = calloc(1, sizeof(struct wd_sched_ctx) + sizeof(struct wd_sched_info) * numa_num); if (!sched_ctx) { @@ -548,6 +604,7 @@ struct wd_sched *wd_sched_rr_alloc(__u8 sched_type, __u8 type_num, sched_ctx->numa_num = numa_num; memset(sched_ctx->numa_map, -1, sizeof(int) * NUMA_NUM_NODES);
+simple_ok: sched->sched_init = sched_table[sched_type].sched_init; sched->pick_next_ctx = sched_table[sched_type].pick_next_ctx; sched->poll_policy = sched_table[sched_type].poll_policy; diff --git a/wd_util.c b/wd_util.c index 433dd56..b08a9d9 100644 --- a/wd_util.c +++ b/wd_util.c @@ -5,6 +5,8 @@ */
#define _GNU_SOURCE +#include <dirent.h> +#include <dlfcn.h> #include <pthread.h> #include <semaphore.h> #include <string.h> @@ -23,6 +25,8 @@ #define WD_INIT_SLEEP_UTIME 1000 #define WD_INIT_RETRY_TIMES 10000
+#define DRV_LIB_DIR "/root/lib" + struct msg_pool { /* message array allocated dynamically */ void *msgs; @@ -64,6 +68,72 @@ struct async_task_queue { int (*alg_poll_ctx)(__u32, __u32, __u32 *); };
+struct drv_lib_list { + void *dlhandle; + struct drv_lib_list *next; +}; + +struct acc_alg_item { + char *name; + char *algtype; +}; + +static struct acc_alg_item alg_options[] = { + {"zlib", "zlib-deflate"}, + {"gzip", "gzip"}, + {"deflate", "deflate"}, + {"lz77_zstd", "lz77_zstd"}, + + {"rsa", "rsa"}, + {"dh", "dh"}, + {"ecdh", "ecdh"}, + {"x25519", "x25519"}, + {"x448", "x448"}, + {"ecdsa", "ecdsa"}, + {"sm2", "sm2"}, + + {"ecb(aes)", "cipher"}, + {"cbc(aes)", "cipher"}, + {"xts(aes)", "cipher"}, + {"ofb(aes)", "cipher"}, + {"cfb(aes)", "cipher"}, + {"ctr(aes)", "cipher"}, + {"cbc-cs1(aes)", "cipher"}, + {"cbc-cs2(aes)", "cipher"}, + {"cbc-cs3(aes)", "cipher"}, + {"ecb(sm4)", "cipher"}, + {"xts(sm4)", "cipher"}, + {"cbc(sm4)", "cipher"}, + {"ofb(sm4)", "cipher"}, + {"cfb(sm4)", "cipher"}, + {"ctr(sm4)", "cipher"}, + {"cbc-cs1(sm4)", "cipher"}, + {"cbc-cs2(sm4)", "cipher"}, + {"cbc-cs3(sm4)", "cipher"}, + {"ecb(des)", "cipher"}, + {"cbc(des)", "cipher"}, + {"ecb(des3_ede)", "cipher"}, + {"cbc(des3_ede)", "cipher"}, + + {"ccm(aes)", "aead"}, + {"gcm(aes)", "aead"}, + {"ccm(sm4)", "aead"}, + {"gcm(sm4)", "aead"}, + {"authenc(hmac(sha256),cbc(aes))", "aead"}, + {"authenc(hmac(sha256),cbc(sm4))", "aead"}, + + {"sm3", "digest"}, + {"md5", "digest"}, + {"sha1", "digest"}, + {"sha256", "digest"}, + {"sha224", "digest"}, + {"sha384", "digest"}, + {"sha512", "digest"}, + {"sha512-224", "digest"}, + {"sha512-256", "digest"}, + {"", ""} +}; + static void clone_ctx_to_internal(struct wd_ctx *ctx, struct wd_ctx_internal *ctx_in) { @@ -1779,6 +1849,272 @@ int wd_init_param_check(struct wd_ctx_config *config, struct wd_sched *sched) return 0; }
+static void wd_get_alg_type(const char *alg_name, char *alg_type) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(alg_options); i++) { + if (strcmp(alg_name, alg_options[i].name) == 0) { + (void)strcpy(alg_type, alg_options[i].algtype); + break; + } + } +} + +/** + * There are many other .so files in this file directory (/root/lib/), + * and it is necessary to screen out valid uadk driver files + * through this function. + */ +static int file_check_valid(char *lib_file) +{ +#define FILE_HEAD_SZ 4 +#define FILE_TAIL_SZ 4 + int file_len = strlen(lib_file); + char file_head[FILE_HEAD_SZ] = "lib"; + char file_tail[FILE_TAIL_SZ] = ".so"; + int i, j; + + /* Lib file name is libxx_xxx.so */ + for (i = 0; i < FILE_HEAD_SZ - 1; i++) { + if (lib_file[i] != file_head[i]) + return -EINVAL; + } + + for (i = file_len - (FILE_TAIL_SZ - 1), j = 0; + i < file_len && j < FILE_TAIL_SZ; i++, j++) { + if (lib_file[i] != file_tail[j]) + return -EINVAL; + } + + return 0; +} + +static int wd_alg_init_fallback(struct wd_alg_driver *fb_driver) +{ + if (!fb_driver->init) { + WD_ERR("soft sec driver have no init interface.\n"); + return -WD_EINVAL; + } + + fb_driver->init(NULL, NULL); + + return 0; +} + +static void wd_alg_uninit_fallback(struct wd_alg_driver *fb_driver) +{ + if (!fb_driver->exit) { + WD_ERR("soft sec driver have no exit interface.\n"); + return; + } + + fb_driver->exit(NULL); +} + +int wd_alg_init_driver(struct wd_ctx_config_internal *config, + struct wd_alg_driver *driver, void **drv_priv) +{ + void *priv; + int ret; + + /* Init ctx related resources in specific driver */ + priv = calloc(1, driver->priv_size); + if (!priv) + return -WD_ENOMEM; + + if (!driver->init) { + driver->fallback = 0; + WD_ERR("driver have no init interface.\n"); + ret = -WD_EINVAL; + goto err_alloc; + } + + ret = driver->init(config, priv); + if (ret < 0) { + WD_ERR("driver init failed.\n"); + goto err_alloc; + } + + if (driver->fallback) { + ret = wd_alg_init_fallback((struct wd_alg_driver *)driver->fallback); + if (ret) { + driver->fallback = 0; + WD_ERR("soft alg driver init failed.\n"); + } + } + *drv_priv = priv; + + return 0; + +err_alloc: + free(priv); + return ret; +} + +void wd_alg_uninit_driver(struct wd_ctx_config_internal *config, + struct wd_alg_driver *driver, void *drv_priv) +{ + driver->exit(drv_priv); + /* Ctx config just need clear once */ + if (driver->priority == UADK_ALG_HW) + wd_clear_ctx_config(config); + + if (driver->fallback) + wd_alg_uninit_fallback((struct wd_alg_driver *)driver->fallback); +} + +int wd_dlclose_drv(void *dlh_list) +{ + struct drv_lib_list *dlhead = (struct drv_lib_list *)dlh_list; + struct drv_lib_list *dlnode; + + if (!dlhead) + return -WD_EINVAL; + + while (dlhead) { + dlnode = dlhead; + dlhead = dlhead->next; + dlclose(dlnode); + free(dlnode); + } + + return 0; +} + +static void add_lib_to_list(struct drv_lib_list *head, + struct drv_lib_list *node) +{ + struct drv_lib_list *tmp = head; + + while (tmp->next) + tmp = tmp->next; + + tmp->next = node; +} + +void *wd_dlopen_drv(const char *cust_lib_dir) +{ + typedef int (*alg_ops)(struct wd_alg_driver *drv); + struct drv_lib_list *node, *head = NULL; + char lib_dir_path[PATH_STR_SIZE]; + char lib_path[PATH_STR_SIZE]; + struct dirent *lib_dir; + alg_ops dl_func = NULL; + DIR *wd_dir; + int ret; + + if (!cust_lib_dir) + strncpy(lib_dir_path, DRV_LIB_DIR, PATH_STR_SIZE - 1); + else + strncpy(lib_dir_path, cust_lib_dir, PATH_STR_SIZE - 1); + + wd_dir = opendir(lib_dir_path); + if (!wd_dir) { + WD_ERR("UADK driver lib file: %s not exist!\n", lib_dir_path); + return NULL; + } + + while ((lib_dir = readdir(wd_dir)) != NULL) { + if (!strncmp(lib_dir->d_name, ".", LINUX_CRTDIR_SIZE) || + !strncmp(lib_dir->d_name, "..", LINUX_PRTDIR_SIZE)) + continue; + + ret = file_check_valid(lib_dir->d_name); + if (ret) + continue; + + node = calloc(1, sizeof(*node)); + if (!node) + goto free_list; + + ret = snprintf(lib_path, PATH_STR_SIZE, "%s/%s", lib_dir_path, lib_dir->d_name); + if (ret < 0) + goto free_list; + + node->dlhandle = dlopen(lib_path, RTLD_NOW); + if (!node->dlhandle) { + free(node); + /* there are many other files need to skip */ + continue; + } + + dl_func = dlsym(node->dlhandle, "wd_alg_driver_register"); + if (dl_func == NULL) { + dlclose(node->dlhandle); + free(node); + continue; + } + + if (!head) + head = node; + else + add_lib_to_list(head, node); + } + closedir(wd_dir); + + return (void *)head; + +free_list: + closedir(wd_dir); + wd_dlclose_drv(head); + return NULL; +} + +struct wd_alg_driver *wd_alg_drv_bind(int task_type, char *alg_name) +{ + struct wd_alg_driver *set_driver = NULL; + struct wd_alg_driver *drv; + + /* Get alg driver and dev name */ + switch (task_type) { + case TASK_INSTR: + drv = wd_request_drv(alg_name, true); + if (!drv) { + WD_ERR("no soft %s driver support\n", alg_name); + return NULL; + } + set_driver = drv; + set_driver->fallback = 0; + break; + case TASK_HW: + case TASK_MIX: + drv = wd_request_drv(alg_name, false); + if (!drv) { + WD_ERR("no HW %s driver support\n", alg_name); + return NULL; + } + set_driver = drv; + set_driver->fallback = 0; + if (task_type == TASK_MIX) { + drv = wd_request_drv(alg_name, true); + if (!drv) { + set_driver->fallback = 0; + WD_ERR("no soft %s driver support\n", alg_name); + } else { + set_driver->fallback = (handle_t)drv; + WD_ERR("successful to get soft driver\n"); + } + } + break; + } + + return set_driver; +} + +void wd_alg_drv_unbind(struct wd_alg_driver *drv) +{ + struct wd_alg_driver *fb_drv = NULL; + + if (!drv) + return; + + fb_drv = (struct wd_alg_driver *)drv->fallback; + if (fb_drv) + wd_release_drv(fb_drv); + wd_release_drv(drv); +} + bool wd_alg_try_init(enum wd_status *status) { enum wd_status expected; @@ -1960,7 +2296,7 @@ free_ctxs: return ret; }
-int wd_alg_pre_init(struct wd_init_attrs *attrs) +static int wd_alg_ctx_init(struct wd_init_attrs *attrs) { struct wd_ctx_config *ctx_config = attrs->ctx_config; struct wd_ctx_params *ctx_params = attrs->ctx_params; @@ -2034,3 +2370,128 @@ out_freelist:
return ret; } + +static void wd_alg_ctx_uninit(struct wd_ctx_config *ctx_config) +{ + int i; + + for (i = 0; i < ctx_config->ctx_num; i++) + if (ctx_config->ctxs[i].ctx) { + wd_release_ctx(ctx_config->ctxs[i].ctx); + ctx_config->ctxs[i].ctx = 0; + } + + free(ctx_config->ctxs); +} + +int wd_alg_attrs_init(struct wd_init_attrs *alg_init_attrs) +{ + wd_alg_poll_ctx alg_poll_func = alg_init_attrs->alg_poll_ctx; + wd_alg_init alg_init_func = alg_init_attrs->alg_init; + struct wd_alg_driver *driver = alg_init_attrs->driver; + __u32 sched_type = alg_init_attrs->sched_type; + char *alg = alg_init_attrs->alg; + struct wd_ctx_config *ctx_config = NULL; + struct wd_sched *alg_sched = NULL; + char alg_type[WD_NAME_SIZE]; + __u32 op_type_num; + int ret; + + if (!alg_init_attrs->ctx_params) + return -WD_EINVAL; + + op_type_num = alg_init_attrs->ctx_params->op_type_num; + + switch (driver->priority) { + case UADK_ALG_SOFT: + case UADK_ALG_CE_INSTR: + /* No need to alloc resource */ + if (sched_type != SCHED_POLICY_NONE) + return -WD_EINVAL; + + alg_sched = wd_sched_rr_alloc(SCHED_POLICY_NONE, 1, 1, alg_poll_func); + if (!alg_sched) { + WD_ERR("fail to alloc scheduler\n"); + return -WD_EINVAL; + } + alg_init_attrs->sched = alg_sched; + + ret = wd_sched_rr_instance(alg_sched, NULL); + if (ret) { + WD_ERR("fail to instance scheduler\n"); + goto out_freesched; + } + break; + case UADK_ALG_SVE_INSTR: + /* Todo lock cpu core */ + if (sched_type != SCHED_POLICY_SINGLE) + return -WD_EINVAL; + + alg_sched = wd_sched_rr_alloc(SCHED_POLICY_SINGLE, 1, 1, alg_poll_func); + if (!alg_sched) { + WD_ERR("fail to alloc scheduler\n"); + return -WD_EINVAL; + } + alg_init_attrs->sched = alg_sched; + + ret = wd_sched_rr_instance(alg_sched, NULL); + if (ret) { + WD_ERR("fail to instance scheduler\n"); + goto out_freesched; + } + break; + case UADK_ALG_HW: + wd_get_alg_type(alg, alg_type); + alg_init_attrs->alg = alg_type; + + ctx_config = calloc(1, sizeof(*ctx_config)); + if (!ctx_config) { + WD_ERR("fail to alloc ctx config\n"); + return -WD_ENOMEM; + } + alg_init_attrs->ctx_config = ctx_config; + + alg_sched = wd_sched_rr_alloc(sched_type, op_type_num, + numa_max_node() + 1, alg_poll_func); + if (!alg_sched) { + WD_ERR("fail to instance scheduler\n"); + ret = -WD_EINVAL; + goto out_ctx_config; + } + alg_init_attrs->sched = alg_sched; + + ret = wd_alg_ctx_init(alg_init_attrs); + if (ret) { + WD_ERR("fail to init ctx\n"); + goto out_freesched; + } + + ret = alg_init_func(ctx_config, alg_sched); + if (ret) + goto out_pre_init; + } + + return 0; + +out_pre_init: + wd_alg_ctx_uninit(ctx_config); +out_freesched: + wd_sched_rr_release(alg_sched); +out_ctx_config: + if (ctx_config) + free(ctx_config); + return ret; +} + +void wd_alg_attrs_uninit(struct wd_init_attrs *attrs) +{ + struct wd_ctx_config *ctx_config = attrs->ctx_config; + struct wd_sched *alg_sched = attrs->sched; + + if (ctx_config) { + wd_alg_ctx_uninit(ctx_config); + free(ctx_config); + } + wd_sched_rr_release(alg_sched); +} +
After adding the zip module of the init2 interface, combine its initialization part with dynamic loading, and transform the HiSilicon driver of zip, and use the dynamic loading function to realize the connection between the driver and the algorithm layer.
Signed-off-by: Longfang Liu liulongfang@huawei.com --- drv/hisi_comp.c | 59 +++++++++--- include/drv/wd_comp_drv.h | 27 ------ wd_comp.c | 186 +++++++++++++++++++------------------- 3 files changed, 143 insertions(+), 129 deletions(-)
diff --git a/drv/hisi_comp.c b/drv/hisi_comp.c index 2eede39..9f595d1 100644 --- a/drv/hisi_comp.c +++ b/drv/hisi_comp.c @@ -13,7 +13,7 @@
#define ZLIB_HEADER "\x78\x9c" #define ZLIB_HEADER_SZ 2 - +#define ZIP_CTX_Q_NUM_DEF 2 /* * We use a extra field for gzip block length. So the fourth byte is \x04. * This is necessary because our software don't know the size of block when @@ -771,8 +771,9 @@ static void hisi_zip_sqe_ops_adapt(handle_t h_qp) } }
-static int hisi_zip_init(struct wd_ctx_config_internal *config, void *priv) +static int hisi_zip_init(void *conf, void *priv) { + struct wd_ctx_config_internal *config = conf; struct hisi_zip_ctx *zip_ctx = (struct hisi_zip_ctx *)priv; struct hisi_qm_priv qm_priv; handle_t h_qp = 0; @@ -1055,14 +1056,50 @@ static int hisi_zip_comp_recv(handle_t ctx, void *comp_msg) return parse_zip_sqe(qp, &sqe, recv_msg); }
-struct wd_comp_driver hisi_zip = { - .drv_name = "hisi_zip", - .alg_name = "zlib\ngzip\ndeflate\nlz77_zstd", - .drv_ctx_size = sizeof(struct hisi_zip_ctx), - .init = hisi_zip_init, - .exit = hisi_zip_exit, - .comp_send = hisi_zip_comp_send, - .comp_recv = hisi_zip_comp_recv, +#define GEN_ZIP_ALG_DRIVER(zip_alg_name) \ +{\ + .drv_name = "hisi_zip",\ + .alg_name = zip_alg_name,\ + .priority = UADK_ALG_HW,\ + .priv_size = sizeof(struct hisi_zip_ctx),\ + .queue_num = ZIP_CTX_Q_NUM_DEF,\ + .op_type_num = 2,\ + .fallback = 0,\ + .init = hisi_zip_init,\ + .exit = hisi_zip_exit,\ + .send = hisi_zip_comp_send,\ + .recv = hisi_zip_comp_recv,\ +} + +static struct wd_alg_driver zip_alg_driver[] = { + GEN_ZIP_ALG_DRIVER("zlib"), + GEN_ZIP_ALG_DRIVER("gzip"), + + GEN_ZIP_ALG_DRIVER("deflate"), + GEN_ZIP_ALG_DRIVER("lz77_zstd"), };
-WD_COMP_SET_DRIVER(hisi_zip); +static void __attribute__((constructor)) hisi_zip_probe(void) +{ + int alg_num = ARRAY_SIZE(zip_alg_driver); + int i, ret; + + WD_ERR("Info: register ZIP alg drivers!\n"); + + for (i = 0; i < alg_num; i++) { + ret = wd_alg_driver_register(&zip_alg_driver[i]); + if (ret) + WD_ERR("Error: register ZIP %s failed!\n", + zip_alg_driver[i].alg_name); + } +} + +static void __attribute__((destructor)) hisi_zip_remove(void) +{ + int alg_num = ARRAY_SIZE(zip_alg_driver); + int i; + + for (i = 0; i < alg_num; i++) + wd_alg_driver_unregister(&zip_alg_driver[i]); +} + diff --git a/include/drv/wd_comp_drv.h b/include/drv/wd_comp_drv.h index 4aeaee4..213cf2d 100644 --- a/include/drv/wd_comp_drv.h +++ b/include/drv/wd_comp_drv.h @@ -55,35 +55,8 @@ struct wd_comp_msg { __u32 tag; };
-struct wd_comp_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 (*comp_send)(handle_t ctx, void *comp_msg); - int (*comp_recv)(handle_t ctx, void *comp_msg); -}; - -void wd_comp_set_driver(struct wd_comp_driver *drv); -struct wd_comp_driver *wd_comp_get_driver(void); - struct wd_comp_msg *wd_comp_get_msg(__u32 idx, __u32 tag);
-#ifdef WD_STATIC_DRV -#define WD_COMP_SET_DRIVER(drv) \ -struct wd_comp_driver *wd_comp_get_driver(void) \ -{ \ - return &drv; \ -} -#else -#define WD_COMP_SET_DRIVER(drv) \ -static void __attribute__((constructor)) set_comp_driver(void) \ -{ \ - wd_comp_set_driver(&(drv)); \ -} -#endif - #ifdef __cplusplus } #endif diff --git a/wd_comp.c b/wd_comp.c index bb142a7..7a667b0 100644 --- a/wd_comp.c +++ b/wd_comp.c @@ -19,8 +19,6 @@ #define HW_CTX_SIZE (64 * 1024) #define STREAM_CHUNK (128 * 1024)
-#define SCHED_RR_NAME "sched_rr" - #define swap_byte(x) \ ((((x) & 0x000000ff) << 24) | \ (((x) & 0x0000ff00) << 8) | \ @@ -45,17 +43,15 @@ struct wd_comp_setting { enum wd_status status2; struct wd_ctx_config_internal config; struct wd_sched sched; - struct wd_comp_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_comp_setting;
struct wd_env_config wd_comp_env_config; - static struct wd_init_attrs wd_comp_init_attrs; -static struct wd_ctx_config wd_comp_ctx; -static struct wd_sched *wd_comp_sched;
static struct wd_ctx_nums wd_comp_ctx_num[] = { {1, 1}, {1, 1}, {} @@ -67,36 +63,45 @@ static struct wd_ctx_params wd_comp_ctx_params = { .bmp = NULL, };
-#ifdef WD_STATIC_DRV -static void wd_comp_set_static_drv(void) +static void wd_comp_close_driver(void) { - wd_comp_setting.driver = wd_comp_get_driver(); - if (!wd_comp_setting.driver) - WD_ERR("failed to get driver!\n"); + if (wd_comp_setting.dlhandle) { + wd_release_drv(wd_comp_setting.driver); + dlclose(wd_comp_setting.dlhandle); + } } -#else -static void __attribute__((constructor)) wd_comp_open_driver(void) + +static int wd_comp_open_driver(void) { + struct wd_alg_driver *driver = NULL; + const char *alg_name = "zlib"; + + /* + * Compatible with the normal acquisition of device + * drivers in the init interface + */ + if (wd_comp_setting.dlh_list) + return 0; + wd_comp_setting.dlhandle = dlopen("libhisi_zip.so", RTLD_NOW); - if (!wd_comp_setting.dlhandle) + if (!wd_comp_setting.dlhandle) { WD_ERR("failed to open libhisi_zip.so, %s\n", dlerror()); -} + return -WD_EINVAL; + }
-static void __attribute__((destructor)) wd_comp_close_driver(void) -{ - if (wd_comp_setting.dlhandle) - dlclose(wd_comp_setting.dlhandle); -} -#endif + driver = wd_request_drv(alg_name, false); + if (!driver) { + WD_ERR("failed to get %s driver support\n", alg_name); + return -WD_EINVAL; + }
-void wd_comp_set_driver(struct wd_comp_driver *drv) -{ - wd_comp_setting.driver = drv; + wd_comp_setting.driver = driver; + + return 0; }
int wd_comp_init(struct wd_ctx_config *config, struct wd_sched *sched) { - void *priv; bool flag; int ret;
@@ -120,19 +125,6 @@ int wd_comp_init(struct wd_ctx_config *config, struct wd_sched *sched) ret = wd_init_sched(&wd_comp_setting.sched, sched); if (ret < 0) goto out_clear_ctx_config; - /* - * Fix me: ctx could be passed into wd_comp_set_static_drv to help to - * choose static compiled vendor driver. For dynamic vendor driver, - * wd_comp_open_driver will be called in the process of opening - * libwd_comp.so to load related driver dynamic library. Vendor driver - * pointer will be passed to wd_comp_setting.driver in the process of - * opening of vendor driver dynamic library. A configure file could be - * introduced to help to define which vendor driver lib should be - * loaded. - */ -#ifdef WD_STATIC_DRV - wd_comp_set_static_drv(); -#endif
/* fix me: sadly find we allocate async pool for every ctx */ ret = wd_init_async_request_pool(&wd_comp_setting.pool, @@ -142,25 +134,24 @@ int wd_comp_init(struct wd_ctx_config *config, struct wd_sched *sched) goto out_clear_sched;
/* init ctx related resources in specific driver */ - priv = calloc(1, wd_comp_setting.driver->drv_ctx_size); - if (!priv) { - ret = -WD_ENOMEM; - goto out_clear_pool; - } - wd_comp_setting.priv = priv; - ret = wd_comp_setting.driver->init(&wd_comp_setting.config, priv); - if (ret < 0) { - WD_ERR("failed to do driver init, ret = %d!\n", ret); - goto out_free_priv; + if (!wd_comp_setting.driver) { + ret = wd_comp_open_driver(); + if (ret) + goto out_clear_pool; }
+ ret = wd_alg_init_driver(&wd_comp_setting.config, + wd_comp_setting.driver, + &wd_comp_setting.priv); + if (ret) + goto out_clear_driver; + wd_alg_set_init(&wd_comp_setting.status);
return 0;
-out_free_priv: - free(priv); - wd_comp_setting.priv = NULL; +out_clear_driver: + wd_comp_close_driver(); out_clear_pool: wd_uninit_async_request_pool(&wd_comp_setting.pool); out_clear_sched: @@ -179,25 +170,26 @@ void wd_comp_uninit(void) if (!priv) return;
- wd_comp_setting.driver->exit(priv); - free(priv); - wd_comp_setting.priv = NULL; - /* uninit async request pool */ wd_uninit_async_request_pool(&wd_comp_setting.pool);
/* unset config, sched, driver */ wd_clear_sched(&wd_comp_setting.sched); - wd_clear_ctx_config(&wd_comp_setting.config); + wd_alg_uninit_driver(&wd_comp_setting.config, + wd_comp_setting.driver, wd_comp_setting.priv); + + wd_alg_drv_unbind(wd_comp_setting.driver); + wd_comp_setting.driver = NULL;
+ wd_comp_close_driver(); wd_alg_clear_init(&wd_comp_setting.status); }
int wd_comp_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params) { enum wd_status status; + int ret = 0; bool flag; - int ret;
wd_alg_get_init(&wd_comp_setting.status, &status); if (status == WD_INIT) { @@ -215,55 +207,67 @@ int wd_comp_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_par goto out_uninit; }
- wd_comp_init_attrs.alg = alg; - wd_comp_init_attrs.sched_type = sched_type; + /* + * 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_comp_setting.dlh_list = wd_dlopen_drv(NULL); + if (!wd_comp_setting.dlh_list) { + WD_ERR("fail to open driver lib files.\n"); + goto out_uninit; + }
- wd_comp_init_attrs.ctx_params = ctx_params ? ctx_params : &wd_comp_ctx_params; - wd_comp_init_attrs.ctx_config = &wd_comp_ctx; +res_retry: + /* Get alg driver and dev name */ + wd_comp_setting.driver = wd_alg_drv_bind(task_type, alg); + if (!wd_comp_setting.driver) + goto out_dlopen;
- wd_comp_sched = wd_sched_rr_alloc(sched_type, wd_comp_init_attrs.ctx_params->op_type_num, - numa_max_node() + 1, wd_comp_poll_ctx); - if (!wd_comp_sched) { - ret = -WD_EINVAL; - goto out_uninit; + if (sched_type < 0 || sched_type > SCHED_POLICY_BUTT) { + WD_ERR("fail to check sched_type: %d\n", sched_type); + goto out_driver; } - wd_comp_sched->name = SCHED_RR_NAME; - wd_comp_init_attrs.sched = wd_comp_sched; + memset(&wd_comp_setting.config, 0, sizeof(struct wd_ctx_config_internal));
+ wd_comp_init_attrs.alg = alg; + wd_comp_init_attrs.sched_type = sched_type; + wd_comp_init_attrs.driver = wd_comp_setting.driver; + wd_comp_init_attrs.ctx_params = ctx_params ? ctx_params : + &wd_comp_ctx_params; + wd_comp_init_attrs.alg_init = wd_comp_init; + wd_comp_init_attrs.alg_poll_ctx = wd_comp_poll_ctx; ret = wd_alg_attrs_init(&wd_comp_init_attrs); - if (ret) - goto out_freesched; - - ret = wd_comp_init(&wd_comp_ctx, wd_comp_sched); - if (ret) - goto out_freesched; + if (ret) { + if (ret == -WD_ENODEV) { + wd_disable_drv(wd_comp_setting.driver); + goto res_retry; + } + WD_ERR("fail to init alg attrs.\n"); + goto out_driver; + }
wd_alg_set_init(&wd_comp_setting.status2);
return 0;
-out_freesched: - wd_sched_rr_release(wd_comp_sched); - +out_driver: + wd_alg_drv_unbind(wd_comp_setting.driver); +out_dlopen: + wd_dlclose_drv(wd_comp_setting.dlh_list); out_uninit: wd_alg_clear_init(&wd_comp_setting.status2); - return ret; }
void wd_comp_uninit2(void) { - int i; - wd_comp_uninit();
- for (i = 0; i < wd_comp_ctx.ctx_num; i++) - if (wd_comp_ctx.ctxs[i].ctx) { - wd_release_ctx(wd_comp_ctx.ctxs[i].ctx); - wd_comp_ctx.ctxs[i].ctx = 0; - } + wd_alg_attrs_uninit(&wd_comp_init_attrs);
- wd_sched_rr_release(wd_comp_sched); + wd_alg_drv_unbind(wd_comp_setting.driver); + wd_dlclose_drv(wd_comp_setting.dlh_list); wd_alg_clear_init(&wd_comp_setting.status2); }
@@ -297,7 +301,7 @@ int wd_comp_poll_ctx(__u32 idx, __u32 expt, __u32 *count) ctx = config->ctxs + idx;
do { - ret = wd_comp_setting.driver->comp_recv(ctx->ctx, &resp_msg); + ret = wd_comp_setting.driver->recv(ctx->ctx, &resp_msg); if (unlikely(ret < 0)) { if (ret == -WD_HW_EACCESS) WD_ERR("wd comp recv hw error!\n"); @@ -513,8 +517,8 @@ static int wd_comp_sync_job(struct wd_comp_sess *sess, wd_dfx_msg_cnt(config->msg_cnt, WD_CTX_CNT_NUM, idx); ctx = config->ctxs + idx;
- msg_handle.send = wd_comp_setting.driver->comp_send; - msg_handle.recv = wd_comp_setting.driver->comp_recv; + msg_handle.send = wd_comp_setting.driver->send; + msg_handle.recv = wd_comp_setting.driver->recv;
pthread_spin_lock(&ctx->lock); ret = wd_handle_msg_sync(&msg_handle, ctx->ctx, msg, @@ -773,7 +777,7 @@ int wd_do_comp_async(handle_t h_sess, struct wd_comp_req *req) msg->tag = tag; msg->stream_mode = WD_COMP_STATELESS;
- ret = wd_comp_setting.driver->comp_send(ctx->ctx, msg); + ret = wd_comp_setting.driver->send(ctx->ctx, msg); if (unlikely(ret < 0)) { WD_ERR("wd comp send error, ret = %d!\n", ret); goto fail_with_msg;
1. Add the init2 interface for the cipher algorithm to simplify the user's use of the cipher algorithm.
2. 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 | 125 ++++++++++++++++--- include/drv/wd_cipher_drv.h | 26 ---- libwd_crypto.map | 3 + wd_cipher.c | 233 ++++++++++++++++++++++++++++-------- 4 files changed, 294 insertions(+), 93 deletions(-)
diff --git a/drv/hisi_sec.c b/drv/hisi_sec.c index c30b653..0ff2e5c 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 2 + enum C_ALG { C_ALG_DES = 0x0, C_ALG_3DES = 0x1, @@ -508,9 +510,77 @@ 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)"), +}; + +#if 0 +static struct wd_alg_driver aead_alg_driver[] = { + GEN_SEC_ALG_DRIVER("ccm(aes)"), + GEN_SEC_ALG_DRIVER("gcm(aes)"), + GEN_SEC_ALG_DRIVER("authenc(hmac(sha256),cbc(aes))"), + GEN_SEC_ALG_DRIVER("ccm(sm4)"), + GEN_SEC_ALG_DRIVER("gcm(sm4)"), + GEN_SEC_ALG_DRIVER("authenc(hmac(sha256),cbc(sm4))"), +}; + +static struct wd_alg_driver alg_driver[] = { + GEN_SEC_ALG_DRIVER("sm3"), + GEN_SEC_ALG_DRIVER("md5"), + GEN_SEC_ALG_DRIVER("sha1"), + GEN_SEC_ALG_DRIVER("sha224"), + GEN_SEC_ALG_DRIVER("sha256"), + GEN_SEC_ALG_DRIVER("sha384"), + GEN_SEC_ALG_DRIVER("sha512"), + GEN_SEC_ALG_DRIVER("sha512-224"), + GEN_SEC_ALG_DRIVER("sha512-256"), +}; +#endif + static void dump_sec_msg(void *msg, const char *alg) { struct wd_cipher_msg *cmsg; @@ -1064,16 +1134,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 +2586,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 +2603,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 +2617,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 +2677,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_ERR("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_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/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..cae9a6c 100644 --- a/wd_cipher.c +++ b/wd_cipher.c @@ -43,15 +43,25 @@ 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; + enum wd_status status2; 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 { @@ -67,32 +77,53 @@ 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}, {} +};
-#ifdef WD_STATIC_DRV -static void wd_cipher_set_static_drv(void) +static struct wd_ctx_params wd_cipher_ctx_params = { + .op_type_num = 1, + .ctx_set_num = wd_cipher_ctx_num, + .bmp = NULL, +}; + +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); + } } -#else -static void __attribute__((constructor)) wd_cipher_open_driver(void) + +static int wd_cipher_open_driver(void) { + struct wd_alg_driver *driver = NULL; + const char *alg_name = "cbc(aes)"; + + /* + * Compatible with the normal acquisition of device + * drivers in the init interface + */ + if (wd_cipher_setting.dlh_list) + return 0; + wd_cipher_setting.dlhandle = dlopen("libhisi_sec.so", RTLD_NOW); - if (!wd_cipher_setting.dlhandle) + 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_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) @@ -185,6 +216,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"); @@ -198,18 +230,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) @@ -230,7 +279,6 @@ void wd_cipher_free_sess(handle_t h_sess)
int wd_cipher_init(struct wd_ctx_config *config, struct wd_sched *sched) { - void *priv; bool flag; int ret;
@@ -255,11 +303,6 @@ int wd_cipher_init(struct wd_ctx_config *config, struct wd_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, @@ -268,26 +311,24 @@ int wd_cipher_init(struct wd_ctx_config *config, struct wd_sched *sched) 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; - goto out_clear_pool; + if (!wd_cipher_setting.driver) { + ret = wd_cipher_open_driver(); + 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 do dirver init, ret = %d.\n", ret); - goto out_free_priv; - } + ret = wd_alg_init_driver(&wd_cipher_setting.config, + wd_cipher_setting.driver, + &wd_cipher_setting.priv); + if (ret) + goto out_close_driver;
wd_alg_set_init(&wd_cipher_setting.status);
return 0;
-out_free_priv: - free(priv); - wd_cipher_setting.priv = NULL; +out_close_driver: + wd_cipher_close_driver(); out_clear_pool: wd_uninit_async_request_pool(&wd_cipher_setting.pool); out_clear_sched: @@ -306,16 +347,106 @@ void wd_cipher_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, wd_cipher_setting.priv); + + wd_alg_drv_unbind(wd_cipher_setting.driver); + wd_cipher_setting.driver = NULL; + + 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) +{ + 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.status2); + if (!flag) + return 0; + + if (!alg) { + WD_ERR("invalid: alg is NULL!\n"); + ret = -WD_EINVAL; + goto out_uninit; + } + + /* + * 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_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: + /* Get alg driver and dev name */ + wd_cipher_setting.driver = wd_alg_drv_bind(task_type, alg); + if (!wd_cipher_setting.driver) + goto out_dlopen; + + if (sched_type < 0 || sched_type > SCHED_POLICY_BUTT) { + WD_ERR("fail to check sched_type: %d\n", sched_type); + goto out_driver; + } + memset(&wd_cipher_setting.config, 0, sizeof(struct wd_ctx_config_internal)); + + wd_cipher_init_attrs.alg = alg; + wd_cipher_init_attrs.sched_type = sched_type; + wd_cipher_init_attrs.driver = wd_cipher_setting.driver; + wd_cipher_init_attrs.ctx_params = ctx_params ? ctx_params : &wd_cipher_ctx_params; + wd_cipher_init_attrs.alg_init = wd_cipher_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); + goto res_retry; + } + WD_ERR("fail to init alg attrs.\n"); + goto out_driver; + } + + wd_alg_set_init(&wd_cipher_setting.status2); + + 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.status2); + return ret; +} + +void wd_cipher_uninit2(void) +{ + wd_cipher_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_alg_clear_init(&wd_cipher_setting.status2); +} + static void fill_request_msg(struct wd_cipher_msg *msg, struct wd_cipher_req *req, struct wd_cipher_sess *sess) @@ -419,8 +550,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, @@ -499,7 +630,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"); @@ -547,7 +678,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) {