From: Longfang Liu <liulongfang@huawei.com> Unify the software ctx and hardware ctx in uadk and merge them on the scheduler. Realize the function of software and hardware calculation together Signed-off-by: Longfang Liu <liulongfang@huawei.com> --- include/wd.h | 5 - include/wd_alg.h | 35 +- include/wd_alg_common.h | 41 ++- include/wd_internal.h | 23 +- include/wd_sched.h | 1 + include/wd_util.h | 16 +- wd_alg.c | 63 ++-- wd_util.c | 757 ++++++++++++++++++++++++++-------------- 8 files changed, 622 insertions(+), 319 deletions(-) diff --git a/include/wd.h b/include/wd.h index a468047..6329528 100644 --- a/include/wd.h +++ b/include/wd.h @@ -104,11 +104,6 @@ typedef void (*wd_log)(const char *format, ...); #define WD_IS_ERR(h) ((uintptr_t)(h) > \ (uintptr_t)(-1000)) -enum wd_buff_type { - WD_FLAT_BUF, - WD_SGL_BUF, -}; - enum wd_alg_type { WD_CIPHER, WD_DIGEST, diff --git a/include/wd_alg.h b/include/wd_alg.h index 885a5e3..05e022c 100644 --- a/include/wd_alg.h +++ b/include/wd_alg.h @@ -64,14 +64,23 @@ typedef unsigned char __u8; # define HWCAP2_RNG (1 << 16) #endif -enum alg_dev_type { - UADK_ALG_SOFT = 0x0, +enum alg_priority { + UADK_ALG_HW = 0x0, UADK_ALG_CE_INSTR = 0x1, UADK_ALG_SVE_INSTR = 0x2, - UADK_ALG_HW = 0x3 + UADK_ALG_SOFT = 0x3 }; -/* +enum alg_drv_type { + ALG_DRV_HW = 0x0, + ALG_DRV_CE_INS, + ALG_DRV_SVE_INS, + ALG_DRV_SOFT, + ALG_DRV_INS, + ALG_DRV_FB, +}; + +/** * @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 @@ -110,6 +119,7 @@ struct wd_alg_driver { int priv_size; int *drv_data; handle_t fallback; + int init_state; int (*init)(void *conf, void *priv); void (*exit)(void *priv); @@ -134,7 +144,7 @@ struct hisi_dev_usage { int wd_alg_driver_register(struct wd_alg_driver *drv); void wd_alg_driver_unregister(struct wd_alg_driver *drv); -/* +/** * @alg_name: name of the algorithm supported by the driver * @drv_name: name of the current device driver * @available: Indicates whether the current driver still has resources available @@ -158,25 +168,24 @@ struct wd_alg_list { char alg_type[ALG_NAME_SIZE]; }; -/* +/** * wd_request_drv() - Apply for an algorithm driver. * @alg_name: task algorithm name. - * @hw_mask: the flag of shield hardware device drivers. + * @drv_type: the type of shield hardware device drivers. * * Returns the applied algorithm driver, non means error. */ -struct wd_alg_driver *wd_request_drv(const char *alg_name, bool hw_mask); +struct wd_alg_driver *wd_request_drv(const char *alg_name, int drv_type); void wd_release_drv(struct wd_alg_driver *drv); -/* +/** * wd_drv_alg_support() - Check the algorithms supported by the driver. * @alg_name: task algorithm name. - * @drv: a device driver that supports an algorithm. + * @param: a device queue parameters. * * Return check result. */ -bool wd_drv_alg_support(const char *alg_name, - struct wd_alg_driver *drv); +bool wd_drv_alg_support(const char *alg_name, void *param); /* * wd_enable_drv() - Re-enable use of the current device driver. @@ -191,7 +200,7 @@ int wd_get_alg_type(const char *alg_name, char *alg_type); struct wd_alg_list *wd_get_alg_head(void); #ifdef WD_STATIC_DRV -/* +/** * duplicate drivers will be skipped when it register to alg_list */ void hisi_sec2_probe(void); diff --git a/include/wd_alg_common.h b/include/wd_alg_common.h index a294877..0e5523f 100644 --- a/include/wd_alg_common.h +++ b/include/wd_alg_common.h @@ -42,6 +42,13 @@ extern "C" { /* Key size of digest */ #define MAX_HMAC_KEY_SIZE 128U +/* + * The maximum number of queue types + * required for similar algorithms + */ +#define MAX_CTX_OP_TYPE 4U +#define STATUS_ENABLE (void *)0x1 + enum alg_task_type { TASK_MIX = 0x0, TASK_HW, @@ -55,6 +62,11 @@ enum wd_ctx_mode { CTX_MODE_MAX, }; +enum wd_buff_type { + WD_FLAT_BUF, + WD_SGL_BUF, +}; + enum wd_init_type { WD_TYPE_V1, WD_TYPE_V2, @@ -69,7 +81,8 @@ enum wd_mem_type { /* * struct wd_ctx - Define one ctx and related type. - * @ctx: The ctx itself. + * @ctx: The ctx itself, the hardware queue is wd_ctx_h. + * The soft computing queue is wd_soft_ctx. * @op_type: Define the operation type of this specific ctx. * e.g. 0: compression; 1: decompression. * @ctx_mode: Define this ctx is used for synchronization of asynchronization @@ -79,9 +92,10 @@ struct wd_ctx { handle_t ctx; __u8 op_type; __u8 ctx_mode; + __u8 ctx_type; }; -/* +/** * struct wd_cap_config - Capabilities. * @ctx_msg_num: number of asynchronous msg pools that the user wants to allocate. * Optional, user can set ctx_msg_num based on the number of requests @@ -94,7 +108,7 @@ struct wd_cap_config { __u32 resv; }; -/* +/** * struct wd_ctx_config - Define a ctx set and its related attributes, which * will be used in the scope of current process. * @ctx_num: The ctx number in below ctx array. @@ -110,19 +124,34 @@ struct wd_ctx_config { struct wd_cap_config *cap; }; -/* +/* 0x0 mean calloc init value */ +enum wd_ctx_property { + UADK_CTX_HW = 0x0, + UADK_CTX_CE_INS = 0x1, + UADK_CTX_SVE_INS = 0x2, + UADK_CTX_SOFT = 0x3, + UADK_CTX_MAX +}; + +/** * struct wd_ctx_nums - Define the ctx sets numbers. * @sync_ctx_num: The ctx numbers which are used for sync mode for each * ctx sets. * @async_ctx_num: The ctx numbers which are used for async mode for each * ctx sets. + * @ctx_prop: Indicates the properties of the current queue + * @ctx_begin: The encoding starting position of the current device ctx + * @other_ctx: Other types of queues configured */ struct wd_ctx_nums { __u32 sync_ctx_num; __u32 async_ctx_num; + __u8 ctx_prop; + __u16 ctx_begin; + struct wd_ctx_nums *other_ctx; }; -/* +/** * struct wd_ctx_params - Define the ctx sets params which are used for init * algorithms. * @op_type_num: Used for index of ctx_set_num, the order is the same as @@ -140,7 +169,7 @@ struct wd_ctx_params { }; /* - * struct wd_comp_sched - Define a scheduler. + * struct wd_sched - Define a scheduler. * @name: Name of this scheduler. * @sched_policy: Method for scheduler to perform scheduling * @sched_init: inited the scheduler input parameters. diff --git a/include/wd_internal.h b/include/wd_internal.h index d899555..59046ae 100644 --- a/include/wd_internal.h +++ b/include/wd_internal.h @@ -17,6 +17,7 @@ extern "C" { #define DEVICE_REGION_MAX 16 #define DECIMAL_NUMBER 10 #define MAX_FD_NUM 65535 +#define MAX_SOFT_QUEUE_LENGTH 1024U struct wd_ctx_h { int fd; @@ -29,8 +30,24 @@ struct wd_ctx_h { void *priv; }; +struct wd_soft_sqe { + __u8 used; + __u8 result; + __u8 complete; + __u32 id; +}; + +/** + * default queue length set to 1024 + */ struct wd_soft_ctx { int fd; + pthread_spinlock_t slock; + __u32 head; + struct wd_soft_sqe qfifo[MAX_SOFT_QUEUE_LENGTH]; + pthread_spinlock_t rlock; + __u32 tail; + __u32 run_num; void *priv; }; @@ -41,11 +58,15 @@ struct wd_ce_ctx { }; struct wd_ctx_internal { - handle_t ctx; __u8 op_type; __u8 ctx_mode; + __u8 ctx_type; + __u8 ctx_used; + handle_t ctx; // if ctx is first will cause problem __u16 sqn; pthread_spinlock_t lock; + struct wd_alg_driver *drv; + void *drv_priv; }; struct wd_ctx_config_internal { diff --git a/include/wd_sched.h b/include/wd_sched.h index 5baecd3..71949f9 100644 --- a/include/wd_sched.h +++ b/include/wd_sched.h @@ -35,6 +35,7 @@ struct sched_params { __u32 begin; __u32 end; __u32 dev_id; + int ctx_prop; }; typedef int (*user_poll_func)(__u32 pos, __u32 expect, __u32 *count); diff --git a/include/wd_util.h b/include/wd_util.h index 14985e2..3be75d3 100644 --- a/include/wd_util.h +++ b/include/wd_util.h @@ -126,13 +126,14 @@ struct wd_msg_handle { struct wd_init_attrs { __u32 sched_type; + __u32 task_type; char alg[CRYPTO_MAX_ALG_NAME]; - 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; + struct wd_alg_driver *driver; //stub for old code }; /* @@ -173,6 +174,9 @@ void wd_clear_ctx_config(struct wd_ctx_config_internal *in); */ void wd_memset_zero(void *data, __u32 size); +int wd_ctx_drv_config(char *alg_name, struct wd_ctx_config_internal *ctx_config); +void wd_ctx_drv_deconfig(struct wd_ctx_config_internal *ctx_config); + /* * wd_init_async_request_pool() - Init async message pools. * @pool: Pointer of message pool. @@ -452,6 +456,10 @@ static inline void wd_alg_clear_init(enum wd_status *status) * * Return 0 if succeed and other error number if fail. */ +int wd_ctx_param_init_nw(struct wd_ctx_params *ctx_params, + struct wd_ctx_params *user_ctx_params, + char *alg, int task_type, enum wd_type type, + int max_op_type); int wd_ctx_param_init(struct wd_ctx_params *ctx_params, struct wd_ctx_params *user_ctx_params, struct wd_alg_driver *driver, @@ -472,12 +480,12 @@ void wd_alg_attrs_uninit(struct wd_init_attrs *attrs); /** * wd_alg_drv_bind() - Request the ctxs and initialize the sched_domain * with the given devices list, ctxs number and numa mask. - * @task_type: the type of task specified by the current algorithm. + * @ctx_type: the type of ctx specified by the current algorithm. * @alg_name: the name of the algorithm specified by the task. * * Return device driver if succeed and other NULL if fail. */ -struct wd_alg_driver *wd_alg_drv_bind(int task_type, const char *alg_name); +struct wd_alg_driver *wd_alg_drv_bind(__u8 ctx_prop, char *alg_name); void wd_alg_drv_unbind(struct wd_alg_driver *drv); /** @@ -489,6 +497,8 @@ void wd_alg_drv_unbind(struct wd_alg_driver *drv); * * Return 0 if succeed and other error number if fail. */ +int wd_alg_init_driver_nw(struct wd_ctx_config_internal *config); +void wd_alg_uninit_driver_nw(struct wd_ctx_config_internal *config); 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, diff --git a/wd_alg.c b/wd_alg.c index a1a0a79..582a4a7 100644 --- a/wd_alg.c +++ b/wd_alg.c @@ -12,7 +12,7 @@ #include <sys/auxv.h> #include "wd.h" -#include "wd_alg.h" +#include "wd_alg_common.h" #define SYS_CLASS_DIR "/sys/class/uacce" #define SVA_FILE_NAME "flags" @@ -337,21 +337,26 @@ 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) +bool wd_drv_alg_support(const char *alg_name, void *param) { + struct wd_ctx_config_internal *config = param; struct wd_alg_list *head = &alg_list_head; struct wd_alg_list *pnext = head->next; + struct wd_alg_driver *drv; + __u32 i; - if (!alg_name || !drv) + if (!alg_name || !config) return false; - while (pnext) { - if (!strcmp(alg_name, pnext->alg_name) && - !strcmp(drv->drv_name, pnext->drv_name)) { - return true; + for (i = 0; i < config->ctx_num; i++) { + drv = config->ctxs[i].drv; + while (pnext) { + if (!strcmp(alg_name, pnext->alg_name) && + !strcmp(drv->drv_name, pnext->drv_name)) { + return true; + } + pnext = pnext->next; } - pnext = pnext->next; } return false; @@ -398,7 +403,7 @@ void wd_disable_drv(struct wd_alg_driver *drv) pthread_mutex_unlock(&mutex); } -struct wd_alg_driver *wd_request_drv(const char *alg_name, bool hw_mask) +struct wd_alg_driver *wd_request_drv(const char *alg_name, int drv_type) { struct wd_alg_list *head = &alg_list_head; struct wd_alg_list *pnext = head->next; @@ -419,18 +424,32 @@ struct wd_alg_driver *wd_request_drv(const char *alg_name, bool hw_mask) /* 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->calc_type == UADK_ALG_HW) || - (!hw_mask && pnext->drv->calc_type != 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; + if (!strcmp(alg_name, pnext->alg_name) && pnext->available) { + /* HW driver mean to used hardware dev */ + if (drv_type == ALG_DRV_HW && pnext->drv->calc_type == UADK_ALG_HW) + select_node = pnext; + /* CE driver mean to used CE dev */ + else if (drv_type == ALG_DRV_CE_INS && pnext->drv->calc_type == UADK_ALG_CE_INSTR) + select_node = pnext; + /* SVE driver mean to used SVE dev */ + else if (drv_type == ALG_DRV_SVE_INS && pnext->drv->calc_type == UADK_ALG_SVE_INSTR) + select_node = pnext; + /* INS driver mean to used CE and SVE dev */ + else if (drv_type == ALG_DRV_INS && (pnext->drv->calc_type == UADK_ALG_CE_INSTR || + pnext->drv->calc_type == UADK_ALG_SVE_INSTR)) + select_node = pnext; + /* Soft driver mean to used Soft, CE and SVE dev */ + else if (drv_type == ALG_DRV_SOFT && pnext->drv->calc_type != UADK_ALG_HW) + select_node = pnext; + /* Fallback driver mean to used Soft or CE dev */ + else if (drv_type == ALG_DRV_FB && (pnext->drv->calc_type == UADK_ALG_SOFT || + pnext->drv->calc_type == UADK_ALG_CE_INSTR)) + select_node = pnext; + + if (select_node && select_node->drv->priority > tmp_priority) { + drv = select_node->drv; + tmp_priority = select_node->drv->priority; + } } pnext = pnext->next; } diff --git a/wd_util.c b/wd_util.c index 0dc9a67..a23152c 100644 --- a/wd_util.c +++ b/wd_util.c @@ -216,6 +216,7 @@ static void clone_ctx_to_internal(struct wd_ctx *ctx, ctx_in->ctx = ctx->ctx; ctx_in->op_type = ctx->op_type; ctx_in->ctx_mode = ctx->ctx_mode; + ctx_in->ctx_type = ctx->ctx_type; } static int wd_shm_create(struct wd_ctx_config_internal *in) @@ -285,9 +286,8 @@ int wd_init_ctx_config(struct wd_ctx_config_internal *in, for (i = 0; i < cfg->ctx_num; i++) { if (!cfg->ctxs[i].ctx) { - WD_ERR("invalid: ctx is NULL!\n"); - ret = -WD_EINVAL; - goto err_out; + WD_ERR("invalid: ctx<%u> is NULL!\n", i); + break; } clone_ctx_to_internal(cfg->ctxs + i, ctxs + i); ret = pthread_spin_init(&ctxs[i].lock, PTHREAD_PROCESS_SHARED); @@ -1847,7 +1847,7 @@ int wd_check_ctx(struct wd_ctx_config_internal *config, __u8 mode, __u32 idx) } ctx = config->ctxs + idx; - if (ctx->ctx_mode != mode) { + if (ctx->ctx_type == UADK_CTX_HW && ctx->ctx_mode != mode) { WD_ERR("invalid: ctx(%u) mode is %hhu!\n", idx, ctx->ctx_mode); return -WD_EINVAL; } @@ -1929,18 +1929,50 @@ int wd_handle_msg_sync(struct wd_msg_handle *msg_handle, handle_t ctx, int wd_init_param_check(struct wd_ctx_config *config, struct wd_sched *sched) { if (!config || !config->ctxs || !config->ctxs[0].ctx) { - WD_ERR("invalid: config or config->ctxs is NULL!\n"); + WD_ERR("invalid: wd_ctx_config is NULL!\n"); return -WD_EINVAL; } if (!sched) { - WD_ERR("invalid: sched is NULL!\n"); + WD_ERR("invalid: wd_sched is NULL!\n"); return -WD_EINVAL; } return 0; } +int wd_alg_try_init(enum wd_status *status) +{ + enum wd_status expected; + __u32 count = 0; + bool ret; + + /* + * Here is aimed to protect the security of the initialization interface + * in the multi-thread scenario. Only one thread can get the WD_INITING + * status to initialize algorithm. Other thread will wait for the result. + * And the algorithm initialization interfaces is a liner process. + * So the initing thread will return a result to notify other thread go on. + */ + do { + expected = WD_UNINIT; + ret = __atomic_compare_exchange_n(status, &expected, WD_INITING, true, + __ATOMIC_RELAXED, __ATOMIC_RELAXED); + if (expected == WD_INIT) { + WD_ERR("The algorithm has been initialized!\n"); + return -WD_EEXIST; + } + usleep(WD_INIT_SLEEP_UTIME); + + if (US2S(WD_INIT_SLEEP_UTIME * ++count) >= WD_INIT_RETRY_TIMEOUT) { + WD_ERR("The algorithm initialize wait timeout!\n"); + return -WD_ETIMEDOUT; + } + } while (!ret); + + return 0; +} + static int wd_alg_init_fallback(struct wd_alg_driver *fb_driver) { if (!fb_driver->init) { @@ -1948,6 +1980,7 @@ static int wd_alg_init_fallback(struct wd_alg_driver *fb_driver) return -WD_EINVAL; } + WD_ERR("debug: call function: %s!\n", __func__); fb_driver->init(NULL, NULL); return 0; @@ -1963,12 +1996,20 @@ static void wd_alg_uninit_fallback(struct wd_alg_driver *fb_driver) fb_driver->exit(NULL); } -int wd_alg_init_driver(struct wd_ctx_config_internal *config, - struct wd_alg_driver *driver, void **drv_priv) +static int wd_ctx_init_driver(struct wd_ctx_config_internal *config, + struct wd_ctx_internal *ctx_config) { - void *priv; + struct wd_alg_driver *driver = ctx_config->drv; + void *priv = ctx_config->drv_priv; int ret; + if (!driver) + return 0; + + /* Prevent repeated initialization */ + if (driver->init_state) + return 0; + if (!driver->priv_size) { WD_ERR("invalid: driver priv ctx size is zero!\n"); return -WD_EINVAL; @@ -1985,6 +2026,7 @@ int wd_alg_init_driver(struct wd_ctx_config_internal *config, ret = -WD_EINVAL; goto err_alloc; } + driver->init_state = 1; ret = driver->init(config, priv); if (ret < 0) { @@ -2000,21 +2042,30 @@ int wd_alg_init_driver(struct wd_ctx_config_internal *config, WD_ERR("soft alg driver init failed.\n"); } } - *drv_priv = priv; return 0; err_alloc: free(priv); + ctx_config->drv_priv = NULL; return ret; } -void wd_alg_uninit_driver(struct wd_ctx_config_internal *config, - struct wd_alg_driver *driver, void **drv_priv) +static void wd_ctx_uninit_driver(struct wd_ctx_config_internal *config, + struct wd_ctx_internal *ctx_config) { - void *priv = *drv_priv; + struct wd_alg_driver *driver = ctx_config->drv; + void *priv = ctx_config->drv_priv; + + if (!driver) + return; + + /* Prevent repeated uninitialization */ + if (!driver->init_state) + return; driver->exit(priv); + driver->init_state = 0; /* Ctx config just need clear once */ wd_clear_ctx_config(config); @@ -2024,10 +2075,53 @@ void wd_alg_uninit_driver(struct wd_ctx_config_internal *config, if (priv) { free(priv); driver->drv_data = NULL; - *drv_priv = NULL; + ctx_config->drv_priv = NULL; } } +int wd_alg_init_driver(struct wd_ctx_config_internal *config, + struct wd_alg_driver *driver, void **drv_priv) +{ + return 0; +} + +int wd_alg_init_driver_nw(struct wd_ctx_config_internal *config) +{ + __u32 i, j; + int ret; + + WD_ERR("debug: call function: %s!\n", __func__); + for (i = 0; i < config->ctx_num; i++) { + if (!config->ctxs[i].ctx) + continue; + ret = wd_ctx_init_driver(config, &config->ctxs[i]); + if (ret) + goto init_err; + } + + return 0; + +init_err: + for (j = 0; j < i; j++) + wd_ctx_uninit_driver(config, &config->ctxs[j]); + + return ret; +} + +void wd_alg_uninit_driver(struct wd_ctx_config_internal *config, + struct wd_alg_driver *driver, void **drv_priv) +{ +} + +void wd_alg_uninit_driver_nw(struct wd_ctx_config_internal *config) +{ + __u32 i; + + for (i = 0; i < config->ctx_num; i++) + wd_ctx_uninit_driver(config, &config->ctxs[i]); + +} + void wd_dlclose_drv(void *dlh_list) { struct drv_lib_list *dlhead = (struct drv_lib_list *)dlh_list; @@ -2178,6 +2272,61 @@ int wd_ctx_param_init(struct wd_ctx_params *ctx_params, numa_free_nodemask(ctx_params->bmp); return ret; } + } else { + /* environment variable is not set, try to use user_ctx_params first */ + if (user_ctx_params) { + copy_bitmask_to_bitmask(user_ctx_params->bmp, ctx_params->bmp); + if (user_ctx_params->op_type_num > (__u32)max_op_type) { + WD_ERR("fail to check user op type numbers.\n"); + numa_free_nodemask(ctx_params->bmp); + return -WD_EINVAL; + } + ctx_params->cap = user_ctx_params->cap; + ctx_params->ctx_set_num = user_ctx_params->ctx_set_num; + ctx_params->op_type_num = user_ctx_params->op_type_num; + + return 0; + } + } + + /* user_ctx_params is also not set, use defalut queue_num max_op_type */ + numa_bitmask_setall(ctx_params->bmp); + for (i = 0; i < max_op_type; i++) { + ctx_params->ctx_set_num[i].sync_ctx_num = max_op_type; + ctx_params->ctx_set_num[i].async_ctx_num = max_op_type; + } + + ctx_params->op_type_num = max_op_type; + + return 0; +} + +int wd_ctx_param_init_nw(struct wd_ctx_params *ctx_params, + struct wd_ctx_params *user_ctx_params, + char *alg, int task_type, enum wd_type type, + int max_op_type) +{ + const char *env_name = wd_env_name[type]; + const char *var_s; + int i, ret; + + ctx_params->bmp = numa_allocate_nodemask(); + if (!ctx_params->bmp) { + WD_ERR("fail to allocate nodemask.\n"); + return -WD_ENOMEM; + } + + /* Only hw driver support environment variable */ + var_s = secure_getenv(env_name); + if (var_s && strlen(var_s) && task_type <= TASK_HW) { + /* environment variable has the highest priority */ + ret = wd_env_set_ctx_nums(alg, env_name, var_s, + ctx_params, max_op_type); + if (ret) { + WD_ERR("fail to init ctx nums from %s!\n", env_name); + numa_free_nodemask(ctx_params->bmp); + return ret; + } } else { /* environment variable is not set, try to use user_ctx_params first */ if (user_ctx_params) { @@ -2540,48 +2689,57 @@ free_path: return (void *)head; } -struct wd_alg_driver *wd_alg_drv_bind(int task_type, const char *alg_name) +struct wd_alg_driver *wd_alg_drv_bind(__u8 ctx_prop, char *alg_name) { - struct wd_alg_driver *set_driver = NULL; + struct wd_alg_driver *fb_drv; struct wd_alg_driver *drv; - /* Get alg driver and dev name */ - switch (task_type) { - case TASK_INSTR: - drv = wd_request_drv(alg_name, true); + /* Get alg driver from ctx type and alg name */ + switch (ctx_prop) { + case UADK_CTX_HW: + drv = wd_request_drv(alg_name, ALG_DRV_HW); if (!drv) { - WD_ERR("no soft %s driver support\n", alg_name); + WD_ERR("no HW %s driver support\n", alg_name); return NULL; } - set_driver = drv; - set_driver->fallback = 0; + + fb_drv = wd_request_drv(alg_name, ALG_DRV_SOFT); + if (!fb_drv) + drv->fallback = 0; + else + drv->fallback = (handle_t)fb_drv; + break; - case TASK_HW: - case TASK_MIX: - drv = wd_request_drv(alg_name, false); + case UADK_CTX_CE_INS: + drv = wd_request_drv(alg_name, ALG_DRV_CE_INS); if (!drv) { - WD_ERR("no HW %s driver support\n", alg_name); + WD_ERR("no CE instr soft %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"); - } + drv->fallback = 0; + break; + case UADK_CTX_SVE_INS: + drv = wd_request_drv(alg_name, ALG_DRV_SVE_INS); + if (!drv) { + WD_ERR("no SVE instr soft %s driver support\n", alg_name); + return NULL; } + drv->fallback = 0; + break; + case UADK_CTX_SOFT: + drv = wd_request_drv(alg_name, ALG_DRV_SOFT); + if (!drv) { + WD_ERR("no instr soft %s driver support\n", alg_name); + return NULL; + } + drv->fallback = 0; break; default: - WD_ERR("task type error.\n"); - return NULL; + WD_ERR("ctx type error: %d.\n", ctx_prop); + return WD_ERR_PTR(-WD_ENODEV); } - return set_driver; + return drv; } void wd_alg_drv_unbind(struct wd_alg_driver *drv) @@ -2597,45 +2755,100 @@ void wd_alg_drv_unbind(struct wd_alg_driver *drv) wd_release_drv(drv); } -int wd_alg_try_init(enum wd_status *status) +static __u32 wd_ctxs_idx_init(struct wd_init_attrs *attrs, int numa_cnt, int task_type) { - enum wd_status expected; + struct wd_ctx_params *ctx_params = attrs->ctx_params; + int end = ctx_params->op_type_num; + struct wd_ctx_nums *ptr_ctx; __u32 count = 0; - bool ret; + int i, uidx; - do { - expected = WD_UNINIT; - ret = __atomic_compare_exchange_n(status, &expected, WD_INITING, true, - __ATOMIC_RELAXED, __ATOMIC_RELAXED); - if (expected == WD_INIT) { - WD_ERR("The algorithm has been initialized!\n"); - return -WD_EEXIST; - } - usleep(WD_INIT_SLEEP_UTIME); + if (ctx_params->op_type_num > MAX_CTX_OP_TYPE) { + WD_ERR("invalid: max ctx op type<%u> is wrong!\n", ctx_params->op_type_num); + return 0; + } - if (US2S(WD_INIT_SLEEP_UTIME * ++count) >= WD_INIT_RETRY_TIMEOUT) { - WD_ERR("The algorithm initialize wait timeout!\n"); - return -WD_ETIMEDOUT; + for (uidx = 0; uidx < UADK_CTX_MAX; uidx++) { + /* If it is a soft computing task, do not use HW queue */ + if (task_type == TASK_INSTR && uidx == UADK_CTX_HW) + continue; + for (i = 0; i < end; i++) { + ptr_ctx = &ctx_params->ctx_set_num[i]; + while (ptr_ctx) { + if (ptr_ctx->ctx_prop == uidx) { + ptr_ctx->ctx_begin = count; + if (uidx == UADK_CTX_HW) { + count += ptr_ctx->sync_ctx_num * numa_cnt; + count += ptr_ctx->async_ctx_num * numa_cnt; + } else { + count += ptr_ctx->sync_ctx_num; + count += ptr_ctx->async_ctx_num; + } + WD_ERR("optype<%d>, prop<%u>, begin<%u>, synx<%u>, async<%u>\n", + i, ptr_ctx->ctx_prop, ptr_ctx->ctx_begin, ptr_ctx->sync_ctx_num, ptr_ctx->async_ctx_num); + } + ptr_ctx = ptr_ctx->other_ctx; + } } - } while (!ret); + } - return 0; + return count; } -static __u32 wd_get_ctx_numbers(struct wd_ctx_params ctx_params, int end) +static struct wd_ctx_nums *wd_get_ctx_ptr(struct wd_ctx_params *ctx_params, + __u32 op_type, int ctx_prop) { - __u32 count = 0; - int i; + struct wd_ctx_nums *ptr_ctx; + + if (op_type > ctx_params->op_type_num) + return NULL; - for (i = 0; i < end; i++) { - count += ctx_params.ctx_set_num[i].sync_ctx_num; - count += ctx_params.ctx_set_num[i].async_ctx_num; + ptr_ctx = &ctx_params->ctx_set_num[op_type]; + while (ptr_ctx) { + if (ptr_ctx->ctx_prop == ctx_prop) + return ptr_ctx; + ptr_ctx = ptr_ctx->other_ctx; } - return count; + return NULL; +} + +void wd_ctx_drv_deconfig(struct wd_ctx_config_internal *ctx_config) +{ + __u32 i; + + // wd_dlclose_drv after this + for (i = 0; i < ctx_config->ctx_num; i++) + wd_alg_drv_unbind(ctx_config->ctxs[i].drv); + +} + +int wd_ctx_drv_config(char *alg_name, struct wd_ctx_config_internal *ctx_config) +{ + __u32 i, j; + + // wd_dlopen_drv before this + WD_ERR("debug: call function: %s!\n", __func__); + for (i = 0; i < ctx_config->ctx_num; i++) { + ctx_config->ctxs[i].drv = wd_alg_drv_bind(ctx_config->ctxs[i].ctx_type, alg_name); + if (WD_IS_ERR(ctx_config->ctxs[i].drv)) { + continue; + } else if (!ctx_config->ctxs[i].drv) { + WD_ERR("failed to bind %s driver.\n", alg_name); + goto bind_err; + } + } + + return 0; + +bind_err: + for (j = 0; j < i; j++) { + wd_alg_drv_unbind(ctx_config->ctxs[j].drv); + } + return -WD_EINVAL; } -static struct uacce_dev_list *wd_get_usable_list(struct uacce_dev_list *list, struct bitmask *bmp) +struct uacce_dev_list *wd_get_usable_list(struct uacce_dev_list *list, struct bitmask *bmp) { struct uacce_dev_list *p, *node, *result = NULL; struct uacce_dev *dev; @@ -2685,7 +2898,7 @@ out_free_list: return result; } -static int wd_init_ctx_set(struct wd_init_attrs *attrs, struct uacce_dev_list *list, +static int wd_init_hw_ctx_set(struct wd_init_attrs *attrs, struct uacce_dev_list *list, __u32 idx, int numa_id, __u32 op_type) { struct wd_ctx_nums ctx_nums = attrs->ctx_params->ctx_set_num[op_type]; @@ -2729,6 +2942,7 @@ static int wd_init_ctx_set(struct wd_init_attrs *attrs, struct uacce_dev_list *l ctx_config->ctxs[i].ctx_mode = ((i - idx) < ctx_nums.sync_ctx_num) ? CTX_MODE_SYNC : CTX_MODE_ASYNC; + ctx_config->ctxs[i].ctx_type = UADK_CTX_HW; } return 0; @@ -2762,6 +2976,7 @@ static int wd_instance_sched_set(struct wd_init_attrs *attrs, struct wd_ctx_nums sparams.dev_id = dev_id; sparams.mode = i; sparams.begin = idx + ctx_nums.sync_ctx_num * i; + sparams.ctx_prop = UADK_CTX_HW; end = idx - 1 + ctx_nums.sync_ctx_num + ctx_nums.async_ctx_num * i; if (end < 0 || sparams.begin > (__u32)end) continue; @@ -2776,7 +2991,7 @@ out: return ret; } -static int wd_init_ctx_and_sched(struct wd_init_attrs *attrs, struct bitmask *bmp, +static int wd_hw_ctx_and_sched(struct wd_init_attrs *attrs, struct bitmask *bmp, struct uacce_dev_list *list) { struct wd_ctx_params *ctx_params = attrs->ctx_params; @@ -2790,7 +3005,7 @@ static int wd_init_ctx_and_sched(struct wd_init_attrs *attrs, struct bitmask *bm continue; for (j = 0; j < op_type_num; j++) { ctx_nums = ctx_params->ctx_set_num[j]; - ret = wd_init_ctx_set(attrs, list, idx, i, j); + ret = wd_init_hw_ctx_set(attrs, list, idx, i, j); if (ret == -WD_ENOPROC) continue; else if (ret) @@ -2821,30 +3036,118 @@ static void wd_init_device_nodemask(struct uacce_dev_list *list, struct bitmask } } -static int wd_alg_ctx_init(struct wd_init_attrs *attrs) +static int wd_alg_other_ctx_init(struct wd_init_attrs *attrs, int ctx_prop) +{ + struct wd_ctx_config *ctx_config = attrs->ctx_config; + struct wd_ctx_params *ctx_params = attrs->ctx_params; + struct wd_ctx_nums *ptr_ctxs; + struct wd_soft_ctx *sfctx; + struct sched_params sparams; + __u32 begin, end, ctx_num; + int sync_type, ret; + __u32 i, j, k; + + WD_ERR("debug: call function: %s!\n", __func__); + for (i = 0; i < ctx_params->op_type_num; i++) { + ptr_ctxs = wd_get_ctx_ptr(ctx_params, i, ctx_prop); + if (!ptr_ctxs) + continue; + + for (sync_type = CTX_MODE_SYNC; sync_type < CTX_MODE_MAX; sync_type++) { + if (sync_type == CTX_MODE_SYNC) { + ctx_num = ptr_ctxs->sync_ctx_num; + begin = ptr_ctxs->ctx_begin; + } else { + ctx_num = ptr_ctxs->async_ctx_num; + begin = ptr_ctxs->ctx_begin + ptr_ctxs->sync_ctx_num; + } + if (ctx_num == 0) + continue; + + end = begin + ctx_num; + for (j = begin; j < end; j++) { + ctx_config->ctxs[j].op_type = i; + ctx_config->ctxs[j].ctx_mode = sync_type; + ctx_config->ctxs[j].ctx_type = ctx_prop; + sfctx = calloc(1, sizeof(struct wd_soft_ctx)); + if (!sfctx) { + WD_ERR("failed to alloc ctx!\n"); + goto ctx_err; + } + ctx_config->ctxs[j].ctx = (handle_t)sfctx; + pthread_spin_init(&sfctx->slock, PTHREAD_PROCESS_SHARED); + pthread_spin_init(&sfctx->rlock, PTHREAD_PROCESS_SHARED); + } + + memset(&sparams, 0x0, sizeof(struct sched_params)); + sparams.begin = begin; + sparams.end = end - 1; + sparams.mode = sync_type; + sparams.numa_id = 0; + sparams.ctx_prop = ctx_prop; + ret = wd_sched_rr_instance(attrs->sched, &sparams); + if (ret) { + WD_ERR("fail to instance scheduler.\n"); + goto ctx_err; + } + } + } + + return WD_SUCCESS; + +ctx_err: + for (k = j; k >= begin; k--) { + free((struct wd_soft_ctx *)ctx_config->ctxs[k].ctx); + ctx_config->ctxs[k].ctx = 0; + } + + return -WD_ENOMEM; +} + +static int wd_alg_other_init(struct wd_init_attrs *attrs) +{ + struct wd_ctx_config *ctx_config = attrs->ctx_config; + struct wd_ctx_params *ctx_params = attrs->ctx_params; + __u32 ctx_set_num, op_type_num; + + WD_ERR("debug: call function: %s!\n", __func__); + op_type_num = ctx_params->op_type_num; + ctx_set_num = wd_ctxs_idx_init(attrs, 1, attrs->task_type); + if (!ctx_set_num || !op_type_num) { + WD_ERR("invalid: ctx_set_num is %u, op_type_num is %u!\n", + ctx_set_num, op_type_num); + return -WD_EINVAL; + } + + ctx_config->ctx_num = ctx_set_num; + ctx_config->ctxs = calloc(ctx_config->ctx_num, sizeof(struct wd_ctx)); + if (!ctx_config->ctxs) { + WD_ERR("failed to alloc ctxs!\n"); + return -WD_ENOMEM; + } + + return 0; +} + +static int wd_alg_hw_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; struct bitmask *used_bmp = ctx_params->bmp; struct uacce_dev_list *list, *used_list = NULL; + char alg_type[CRYPTO_MAX_ALG_NAME]; __u32 ctx_set_num, op_type_num; int numa_cnt, ret; - list = wd_get_accel_list(attrs->alg); + WD_ERR("debug: call function: %s!\n", __func__); + wd_get_alg_type(attrs->alg, alg_type); + + list = wd_get_accel_list(alg_type); if (!list) { WD_ERR("failed to get devices for alg: %s\n", attrs->alg); return -WD_ENODEV; } - op_type_num = ctx_params->op_type_num; - ctx_set_num = wd_get_ctx_numbers(*ctx_params, op_type_num); - if (!ctx_set_num || !op_type_num) { - WD_ERR("invalid: ctx_set_num is %u, op_type_num is %u!\n", - ctx_set_num, op_type_num); - ret = -WD_EINVAL; - goto out_freelist; - } - /* * Not every numa has a device. Therefore, the first thing is to * filter the devices in the selected numa node, and the second @@ -2858,7 +3161,6 @@ static int wd_alg_ctx_init(struct wd_init_attrs *attrs) } wd_init_device_nodemask(used_list, used_bmp); - numa_cnt = numa_bitmask_weight(used_bmp); if (!numa_cnt) { ret = numa_cnt; @@ -2866,7 +3168,17 @@ static int wd_alg_ctx_init(struct wd_init_attrs *attrs) goto out_freeusedlist; } - ctx_config->ctx_num = ctx_set_num * numa_cnt; + op_type_num = ctx_params->op_type_num; + ctx_set_num = wd_ctxs_idx_init(attrs, numa_cnt, attrs->task_type); + WD_ERR("ctx sum num is: %u, op_type num is: %u!\n", ctx_set_num, op_type_num); + if (!ctx_set_num || !op_type_num) { + WD_ERR("invalid: ctx_set_num is %u, op_type_num is %u!\n", + ctx_set_num, op_type_num); + ret = -WD_EINVAL; + goto out_freelist; + } + + ctx_config->ctx_num = ctx_set_num; ctx_config->ctxs = calloc(ctx_config->ctx_num, sizeof(struct wd_ctx)); if (!ctx_config->ctxs) { ret = -WD_ENOMEM; @@ -2874,7 +3186,7 @@ static int wd_alg_ctx_init(struct wd_init_attrs *attrs) goto out_freeusedlist; } - ret = wd_init_ctx_and_sched(attrs, used_bmp, used_list); + ret = wd_hw_ctx_and_sched(attrs, used_bmp, used_list); if (ret) free(ctx_config->ctxs); @@ -2886,224 +3198,145 @@ out_freelist: return ret; } -static int wd_alg_ce_ctx_init(struct wd_init_attrs *attrs) -{ - struct wd_ctx_config *ctx_config = attrs->ctx_config; - struct wd_ce_ctx *ctx; - - ctx_config->ctx_num = 1; - ctx_config->ctxs = calloc(ctx_config->ctx_num, sizeof(struct wd_ctx)); - if (!ctx_config->ctxs) { - WD_ERR("failed to alloc ctxs!\n"); - return -WD_ENOMEM; - } - - ctx = calloc(1, sizeof(struct wd_ce_ctx)); - if (!ctx) { - free(ctx_config->ctxs); - return -WD_ENOMEM; - } - ctx->fd = -1; - ctx_config->ctxs[0].ctx = (handle_t)ctx; - - return WD_SUCCESS; -} - -static void wd_alg_ce_ctx_uninit(struct wd_ctx_config *ctx_config) +static void wd_alg_ctxs_uninit(struct wd_ctx_config *ctx_config) { __u32 i; for (i = 0; i < ctx_config->ctx_num; i++) { if (ctx_config->ctxs[i].ctx) { - free((struct wd_ce_ctx *)ctx_config->ctxs[i].ctx); + if (ctx_config->ctxs[i].ctx_type == UADK_CTX_HW) + wd_release_ctx(ctx_config->ctxs[i].ctx); + else + free((struct wd_soft_ctx *)ctx_config->ctxs[i].ctx); ctx_config->ctxs[i].ctx = 0; } } - free(ctx_config->ctxs); -} - -static void wd_alg_ctx_uninit(struct wd_ctx_config *ctx_config) -{ - __u32 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; - } + if (ctx_config->ctxs) { + free(ctx_config->ctxs); + ctx_config->ctxs = 0; } - - free(ctx_config->ctxs); -} - -static int wd_alg_init_sve_ctx(struct wd_ctx_config *ctx_config) -{ - struct wd_soft_ctx *ctx_sync, *ctx_async; - - ctx_config->ctx_num = WD_SOFT_CTX_NUM; - ctx_config->ctxs = calloc(ctx_config->ctx_num, sizeof(struct wd_ctx)); - if (!ctx_config->ctxs) - return -WD_ENOMEM; - - ctx_sync = calloc(1, sizeof(struct wd_soft_ctx)); - if (!ctx_sync) - goto free_ctxs; - - ctx_sync->fd = -1; - ctx_config->ctxs[WD_SOFT_SYNC_CTX].op_type = 0; - ctx_config->ctxs[WD_SOFT_SYNC_CTX].ctx_mode = CTX_MODE_SYNC; - ctx_config->ctxs[WD_SOFT_SYNC_CTX].ctx = (handle_t)ctx_sync; - - ctx_async = calloc(1, sizeof(struct wd_soft_ctx)); - if (!ctx_async) - goto free_ctx_sync; - - ctx_async->fd = -1; - ctx_config->ctxs[WD_SOFT_ASYNC_CTX].op_type = 0; - ctx_config->ctxs[WD_SOFT_ASYNC_CTX].ctx_mode = CTX_MODE_ASYNC; - ctx_config->ctxs[WD_SOFT_ASYNC_CTX].ctx = (handle_t)ctx_async; - - return 0; - -free_ctx_sync: - free(ctx_sync); -free_ctxs: - free(ctx_config->ctxs); - return -WD_ENOMEM; -} - -static void wd_alg_uninit_sve_ctx(struct wd_ctx_config *ctx_config) -{ - free((struct wd_soft_ctx *)ctx_config->ctxs[WD_SOFT_ASYNC_CTX].ctx); - free((struct wd_soft_ctx *)ctx_config->ctxs[WD_SOFT_SYNC_CTX].ctx); - free(ctx_config->ctxs); } int wd_alg_attrs_init(struct wd_init_attrs *attrs) { wd_alg_poll_ctx alg_poll_func = attrs->alg_poll_ctx; wd_alg_init alg_init_func = attrs->alg_init; - __u32 sched_type = attrs->sched_type; struct wd_ctx_config *ctx_config = NULL; struct wd_sched *alg_sched = NULL; - char alg_type[CRYPTO_MAX_ALG_NAME]; - int driver_type = UADK_ALG_HW; - const char *alg = attrs->alg; - int ret = -WD_EINVAL; + struct wd_alg_driver *drv = NULL; + char *alg_name = attrs->alg; + __u32 op_type_num; + int ret = 0; if (!attrs->ctx_params) return -WD_EINVAL; - if (attrs->driver) - driver_type = attrs->driver->calc_type; - - switch (driver_type) { - case UADK_ALG_SOFT: - case UADK_ALG_CE_INSTR: - ctx_config = calloc(1, sizeof(*ctx_config)); - if (!ctx_config) { - WD_ERR("fail to alloc ctx config\n"); - return -WD_ENOMEM; - } - attrs->ctx_config = ctx_config; + WD_ERR("debug: call function: %s!\n", __func__); + ctx_config = calloc(1, sizeof(*ctx_config)); + if (!ctx_config) { + WD_ERR("fail to alloc ctx config\n"); + return -WD_ENOMEM; + } + attrs->ctx_config = ctx_config; - /* Use default sched_type to alloc scheduler */ - alg_sched = wd_sched_rr_alloc(SCHED_POLICY_NONE, 1, 1, alg_poll_func); - if (!alg_sched) { - WD_ERR("fail to alloc scheduler\n"); - goto out_ctx_config; - } + /* Get op_type_num */ + op_type_num = attrs->ctx_params->op_type_num; + if (!op_type_num) + goto out_ctx_config; - attrs->sched = alg_sched; + /* Use default sched_type to alloc scheduler */ + if (attrs->sched_type == SCHED_POLICY_DEV) + alg_sched = wd_sched_rr_alloc(attrs->sched_type, attrs->ctx_params->op_type_num, + DEVICE_REGION_MAX, alg_poll_func); + else + alg_sched = wd_sched_rr_alloc(attrs->sched_type, attrs->ctx_params->op_type_num, + numa_max_node() + 1, alg_poll_func); + if (!alg_sched) { + WD_ERR("fail to alloc scheduler\n"); + goto out_ctx_config; + } + attrs->sched = alg_sched; - ret = wd_alg_ce_ctx_init(attrs); + /* Initialize queues according to task type */ + switch (attrs->task_type) { + case TASK_HW: + ret = wd_alg_hw_ctx_init(attrs); if (ret) { - WD_ERR("fail to init ce ctx\n"); + WD_ERR("fail to init HW ctx\n"); goto out_freesched; } - ret = alg_init_func(ctx_config, alg_sched); - if (ret) - goto out_pre_init; - break; - case UADK_ALG_SVE_INSTR: - /* Use default sched_type to alloc scheduler */ - 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; - } - attrs->sched = alg_sched; - - ctx_config = calloc(1, sizeof(*ctx_config)); - if (!ctx_config) { - WD_ERR("fail to alloc ctx config\n"); - goto out_freesched; - } - attrs->ctx_config = ctx_config; - - ret = wd_alg_init_sve_ctx(ctx_config); - if (ret) { - WD_ERR("fail to init sve ctx!\n"); - goto out_freesched; - } - - ctx_config->cap = attrs->ctx_params->cap; - ret = alg_init_func(ctx_config, alg_sched); + case TASK_MIX: + ret = wd_alg_hw_ctx_init(attrs); if (ret) { - wd_alg_uninit_sve_ctx(ctx_config); + WD_ERR("fail to init mix HW ctx\n"); goto out_freesched; } - break; - case UADK_ALG_HW: - if (wd_get_alg_type(alg, alg_type)) - return -WD_EINVAL; - (void)strcpy(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; - } - attrs->ctx_config = ctx_config; - if (sched_type == SCHED_POLICY_DEV) - alg_sched = wd_sched_rr_alloc(sched_type, attrs->ctx_params->op_type_num, - DEVICE_REGION_MAX, alg_poll_func); - else - alg_sched = wd_sched_rr_alloc(sched_type, attrs->ctx_params->op_type_num, - numa_max_node() + 1, alg_poll_func); - if (!alg_sched) { - WD_ERR("fail to instance scheduler\n"); - goto out_ctx_config; + WD_ERR("debug: call function: %s!\n", __func__); + drv = wd_request_drv(alg_name, ALG_DRV_SOFT); + if (drv == NULL) { + WD_ERR("fail to find soft driver.\n"); + break; + } else if (drv->calc_type == UADK_ALG_CE_INSTR) { + ret = wd_alg_other_ctx_init(attrs, UADK_CTX_CE_INS); + if (ret) { + WD_ERR("fail to init ce ctx\n"); + goto out_ctx_init; + } + } else if (drv->calc_type == UADK_ALG_SVE_INSTR) { + ret = wd_alg_other_ctx_init(attrs, UADK_CTX_SVE_INS); + if (ret) { + WD_ERR("fail to init sve ctx\n"); + goto out_ctx_init; + } } - attrs->sched = alg_sched; - ret = wd_alg_ctx_init(attrs); + break; + /* Only pure soft queues */ + case TASK_INSTR: + ret = wd_alg_other_init(attrs); if (ret) { - WD_ERR("fail to init ctx\n"); + WD_ERR("fail to init other ctx.\n"); goto out_freesched; } - ctx_config->cap = attrs->ctx_params->cap; - ret = alg_init_func(ctx_config, alg_sched); - if (ret) - goto out_pre_init; + drv = wd_request_drv(alg_name, ALG_DRV_SOFT); + if (drv == NULL) { + WD_ERR("fail to find soft driver.\n"); + goto out_ctx_init; + } else if (drv->calc_type == UADK_ALG_CE_INSTR) { + ret = wd_alg_other_ctx_init(attrs, UADK_CTX_CE_INS); + if (ret) { + WD_ERR("fail to init ce ctx\n"); + goto out_ctx_init; + } + } else if (drv->calc_type == UADK_ALG_SVE_INSTR) { + ret = wd_alg_other_ctx_init(attrs, UADK_CTX_SVE_INS); + if (ret) { + WD_ERR("fail to init sve ctx\n"); + goto out_ctx_init; + } + } break; default: - WD_ERR("driver type error: %d\n", driver_type); + WD_ERR("driver type error: %d\n", drv->calc_type); return -WD_EINVAL; } + ctx_config->cap = attrs->ctx_params->cap; + ret = alg_init_func(ctx_config, alg_sched); + if (ret) + goto out_ctx_init; + + WD_ERR("---->ctx nums: %u\n", ctx_config->ctx_num); + return 0; -out_pre_init: - if (driver_type == UADK_ALG_CE_INSTR || driver_type == UADK_ALG_SOFT) - wd_alg_ce_ctx_uninit(ctx_config); - else - wd_alg_ctx_uninit(ctx_config); +out_ctx_init: + wd_alg_ctxs_uninit(ctx_config); out_freesched: wd_sched_rr_release(alg_sched); out_ctx_config: @@ -3116,27 +3349,13 @@ 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; - int driver_type = attrs->driver->calc_type; if (!ctx_config) { wd_sched_rr_release(alg_sched); return; } - switch (driver_type) { - case UADK_ALG_SOFT: - case UADK_ALG_CE_INSTR: - wd_alg_ce_ctx_uninit(ctx_config); - break; - case UADK_ALG_SVE_INSTR: - wd_alg_uninit_sve_ctx(ctx_config); - break; - case UADK_ALG_HW: - wd_alg_ctx_uninit(ctx_config); - break; - default: - break; - } + wd_alg_ctxs_uninit(ctx_config); free(ctx_config); wd_sched_rr_release(alg_sched); -- 2.43.0