1. Separate the advanced properties of the device from the driver class properties. 2. Fix some internal implementation bugs. 3. Update some comments.
Signed-off-by: Longfang Liu liulongfang@huawei.com --- include/wd.h | 6 +++++ include/wd_alg.h | 47 ++++++++++++++++++++++++++++++++++++- include/wd_util.h | 8 ++++--- wd.c | 19 ++++++++------- wd_alg.c | 59 +++++++++++++++++++++++++++++------------------ wd_sched.c | 2 ++ wd_util.c | 33 +++++++++++++++++--------- 7 files changed, 126 insertions(+), 48 deletions(-)
diff --git a/include/wd.h b/include/wd.h index 21af7ff..301bf1b 100644 --- a/include/wd.h +++ b/include/wd.h @@ -582,11 +582,17 @@ bool wd_need_info(void); struct wd_capability { char alg_name[CRYPTO_MAX_ALG_NAME]; char drv_name[CRYPTO_MAX_ALG_NAME]; + bool available; int priority; + int calc_type;
struct wd_capability *next; };
+/** + * wd_get_alg_cap() - Get the algorithm information supported + * in the current system. + */ struct wd_capability *wd_get_alg_cap(void); void wd_release_alg_cap(struct wd_capability *head);
diff --git a/include/wd_alg.h b/include/wd_alg.h index e25e191..a1b4c4f 100644 --- a/include/wd_alg.h +++ b/include/wd_alg.h @@ -31,6 +31,13 @@ enum alg_priority { * @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 + * the larger the value of priority, the higher the priority of the driver, + * it will be used first when selecting a driver. + * soft calculation can be defined as 0. + * hard calculation can be defined as a value above 100. + * instruction acceleration can define a higher value according to + * the performance situation, such as 400. + * @calc_type: the calculation method of algorithm supported by the driver * @queue_num: number of device queues required by the device to * execute the algorithm task * @op_type_num: number of modes in which the device executes the @@ -52,6 +59,7 @@ struct wd_alg_driver { const char *drv_name; const char *alg_name; int priority; + int calc_type; int queue_num; int op_type_num; int priv_size; @@ -64,25 +72,62 @@ struct wd_alg_driver { int (*get_usage)(void *param); };
+/** + * wd_alg_driver_register() - Register a device driver. + * @wd_alg_driver: a device driver that supports an algorithm. + * + * Return the execution result, non-zero means error code. + */ 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 + * @priority: priority of the type of algorithm supported by the driver + * @calc_type: the calculation method of algorithm supported by the driver + * @refcnt: the number of times the algorithm driver is being cited by the task + * + * @drv: device Drivers Supporting Algorithms + * @next: pointer to the next node of the algorithm linked list + */ struct wd_alg_list { const char *alg_name; const char *drv_name; - bool available; + bool available; int priority; + int calc_type; int refcnt;
struct wd_alg_driver *drv; struct wd_alg_list *next; };
+/** + * wd_request_drv() - Apply for an algorithm driver. + * @alg_name: task algorithm name. + * @hw_mask: the flag 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); 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. + * + * Return check result. + */ bool wd_drv_alg_support(const char *alg_name, struct wd_alg_driver *drv); + +/** + * wd_enable_drv() - Re-enable use of the current device driver. + * @drv: a device driver that supports an algorithm. + */ void wd_enable_drv(struct wd_alg_driver *drv); void wd_disable_drv(struct wd_alg_driver *drv);
diff --git a/include/wd_util.h b/include/wd_util.h index a730f36..b20466f 100644 --- a/include/wd_util.h +++ b/include/wd_util.h @@ -420,9 +420,11 @@ static inline void wd_alg_clear_init(enum wd_status *status) /** * wd_ctx_param_init() - Initialize the current device driver according * to the obtained queue resource and the applied driver. - * @config: device resources requested by the current algorithm. - * @driver: device driver for the current algorithm application. - * @drv_priv: the parameter pointer of the current device driver. + * @ctx_params: queue configuration parameters required for device initialization. + * @user_ctx_params: user-configured queue context parameters. + * @ctx_set_num: the number of user-configured queue contexts. + * @driver: device drivers that support current business algorithms. + * @max_op_type: number of device queue types that support algorithmic services. * * Return 0 if succeed and other error number if fail. */ diff --git a/wd.c b/wd.c index b6286b5..a049035 100644 --- a/wd.c +++ b/wd.c @@ -886,7 +886,7 @@ char *wd_ctx_get_dev_name(handle_t h_ctx) void wd_release_alg_cap(struct wd_capability *head) { struct wd_capability *cap_pnext = head; - struct wd_capability *cap_node = NULL; + struct wd_capability *cap_node;
while (cap_pnext) { cap_node = cap_pnext; @@ -903,30 +903,30 @@ 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; + struct wd_capability *cap_node;
while (pnext) { cap_node = calloc(1, sizeof(struct wd_capability)); - if (!cap_head) { + if (!cap_node) { 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->available = pnext->available; cap_node->priority = pnext->priority; + cap_node->calc_type = pnext->calc_type; cap_node->next = NULL;
- cap_pnext->next = cap_node; - cap_pnext = cap_node; pnext = pnext->next; - if (i == 0) { + if (!cap_pnext) { cap_head = cap_node; - cap_pnext = cap_head; + cap_pnext = cap_node; } - i++; + cap_pnext->next = cap_node; + cap_pnext = cap_node; }
return cap_head; @@ -935,4 +935,3 @@ alloc_err: wd_release_alg_cap(cap_head); return NULL; } - diff --git a/wd_alg.c b/wd_alg.c index 5e4edaf..a802f60 100644 --- a/wd_alg.c +++ b/wd_alg.c @@ -47,6 +47,28 @@ static bool wd_check_accel_dev(const char *dev_name) return false; }
+static bool wd_alg_check_available(int calc_type, const char *dev_name) +{ + bool ret = false; + + switch (calc_type) { + case UADK_ALG_SOFT: + break; + /* Should find the CPU if not support CE */ + case UADK_ALG_CE_INSTR: + break; + /* Should find the CPU if not support SVE */ + case UADK_ALG_SVE_INSTR: + break; + /* Check if the current driver has device support */ + case UADK_ALG_HW: + ret = wd_check_accel_dev(dev_name); + break; + } + + return ret; +} + int wd_alg_driver_register(struct wd_alg_driver *drv) { struct wd_alg_list *new_alg; @@ -65,21 +87,16 @@ int wd_alg_driver_register(struct wd_alg_driver *drv) new_alg->alg_name = drv->alg_name; new_alg->drv_name = drv->drv_name; new_alg->priority = drv->priority; + new_alg->calc_type = drv->calc_type; 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("failed 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; + new_alg->available = wd_alg_check_available(drv->calc_type, drv->drv_name); + if (!new_alg->available) { + free(new_alg); + WD_ERR("failed to find alg driver's device!\n"); + return -WD_ENODEV; }
pthread_mutex_lock(&mutex); @@ -103,7 +120,8 @@ void wd_alg_driver_unregister(struct wd_alg_driver *drv) while (pnext) { if (!strcmp(drv->alg_name, pnext->alg_name) && !strcmp(drv->drv_name, pnext->drv_name) && - drv->priority == pnext->priority) { + drv->priority == pnext->priority && + drv->calc_type == pnext->calc_type) { break; } npre = pnext; @@ -162,19 +180,14 @@ void wd_enable_drv(struct wd_alg_driver *drv) while (pnext) { if (!strcmp(drv->alg_name, pnext->alg_name) && !strcmp(drv->drv_name, pnext->drv_name) && - drv->priority == pnext->priority) { + drv->priority == pnext->priority && + drv->calc_type == pnext->calc_type) { 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; - } + pnext->available = wd_alg_check_available(drv->calc_type, drv->drv_name); pthread_mutex_unlock(&mutex); }
@@ -190,7 +203,8 @@ void wd_disable_drv(struct wd_alg_driver *drv) while (pnext) { if (!strcmp(drv->alg_name, pnext->alg_name) && !strcmp(drv->drv_name, pnext->drv_name) && - drv->priority == pnext->priority) { + drv->priority == pnext->priority && + drv->calc_type == pnext->calc_type) { break; } pnext = pnext->next; @@ -217,7 +231,7 @@ struct wd_alg_driver *wd_request_drv(const char *alg_name, bool hw_mask) pthread_mutex_lock(&mutex); while (pnext) { /* hw_mask true mean not to used hardware dev */ - if (hw_mask && pnext->drv->priority == UADK_ALG_HW) { + if (hw_mask && pnext->drv->calc_type == UADK_ALG_HW) { pnext = pnext->next; continue; } @@ -262,4 +276,3 @@ void wd_release_drv(struct wd_alg_driver *drv) select_node->refcnt--; pthread_mutex_unlock(&mutex); } - diff --git a/wd_sched.c b/wd_sched.c index 9a52c4d..79f57fb 100644 --- a/wd_sched.c +++ b/wd_sched.c @@ -368,6 +368,7 @@ static int sched_none_poll_policy(handle_t h_sched_ctx,
while (loop_times > 0) { /* Default use ctx 0 */ + loop_times--; ret = sched_ctx->poll_func(0, 1, &poll_num); if ((ret < 0) && (ret != -EAGAIN)) return ret; @@ -409,6 +410,7 @@ static int sched_single_poll_policy(handle_t h_sched_ctx,
while (loop_times > 0) { /* Default async mode use ctx 0 */ + loop_times--; ret = sched_ctx->poll_func(0, 1, &poll_num); if ((ret < 0) && (ret != -EAGAIN)) return ret; diff --git a/wd_util.c b/wd_util.c index dab4fc8..f990eb5 100644 --- a/wd_util.c +++ b/wd_util.c @@ -79,7 +79,7 @@ struct acc_alg_item { };
static struct acc_alg_item alg_options[] = { - {"zlib", "zlib-deflate"}, + {"zlib", "zlib"}, {"gzip", "gzip"}, {"deflate", "deflate"}, {"lz77_zstd", "lz77_zstd"}, @@ -131,6 +131,10 @@ static struct acc_alg_item alg_options[] = { {"sha512", "digest"}, {"sha512-224", "digest"}, {"sha512-256", "digest"}, + {"cmac(aes)", "digest"}, + {"gmac(aes)", "digest"}, + {"xcbc-mac-96(aes)", "digest"}, + {"xcbc-prf-128(aes)", "digest"}, {"", ""} };
@@ -1851,9 +1855,10 @@ int wd_init_param_check(struct wd_ctx_config *config, struct wd_sched *sched)
static void wd_get_alg_type(const char *alg_name, char *alg_type) { + int alg_nums = ARRAY_SIZE(alg_options); int i;
- for (i = 0; i < ARRAY_SIZE(alg_options); i++) { + for (i = 0; i < alg_nums; i++) { if (strcmp(alg_name, alg_options[i].name) == 0) { (void)strcpy(alg_type, alg_options[i].algtype); break; @@ -1893,7 +1898,7 @@ static int file_check_valid(char *lib_file) 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"); + WD_ERR("soft acc driver have no init interface.\n"); return -WD_EINVAL; }
@@ -1905,7 +1910,7 @@ static int wd_alg_init_fallback(struct wd_alg_driver *fb_driver) 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"); + WD_ERR("soft acc driver have no exit interface.\n"); return; }
@@ -1959,7 +1964,7 @@ void wd_alg_uninit_driver(struct wd_ctx_config_internal *config,
driver->exit(priv); /* Ctx config just need clear once */ - if (driver->priority == UADK_ALG_HW) + if (driver->calc_type == UADK_ALG_HW) wd_clear_ctx_config(config);
if (driver->fallback) @@ -1967,7 +1972,7 @@ void wd_alg_uninit_driver(struct wd_ctx_config_internal *config,
if (priv) { free(priv); - priv = NULL; + *drv_priv = NULL; } }
@@ -2052,7 +2057,7 @@ int wd_get_lib_file_path(char *lib_file, char *lib_path, bool is_dir) len = strlen(file_path) - 1; for (i = len; i >= 0; i--) { if (file_path[i] == '/') { - memset(&file_path[i], 0, PATH_STR_SIZE - i + 1); + memset(&file_path[i], 0, PATH_STR_SIZE - i); break; } } @@ -2116,7 +2121,7 @@ void *wd_dlopen_drv(const char *cust_lib_dir) if (ret < 0) goto free_node;
- node->dlhandle = dlopen(lib_path, RTLD_NOW); + node->dlhandle = dlopen(lib_path, RTLD_GLOBAL | RTLD_NOW); if (!node->dlhandle) { free(node); /* there are many other files need to skip */ @@ -2183,6 +2188,9 @@ struct wd_alg_driver *wd_alg_drv_bind(int task_type, char *alg_name) } } break; + default: + WD_ERR("task type error.\n"); + return NULL; }
return set_driver; @@ -2400,7 +2408,7 @@ static int wd_alg_ctx_init(struct wd_init_attrs *attrs) 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 %d, op_type_num is %d!\n", + 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; @@ -2486,7 +2494,7 @@ int wd_alg_attrs_init(struct wd_init_attrs *attrs) return -WD_EINVAL;
if (attrs->driver) - driver_type = attrs->driver->priority; + driver_type = attrs->driver->calc_type;
switch (driver_type) { case UADK_ALG_SOFT: @@ -2555,6 +2563,10 @@ int wd_alg_attrs_init(struct wd_init_attrs *attrs) ret = alg_init_func(ctx_config, alg_sched); if (ret) goto out_pre_init; + break; + default: + WD_ERR("driver type error: %d\n", driver_type); + return -WD_EINVAL; }
return 0; @@ -2580,4 +2592,3 @@ void wd_alg_attrs_uninit(struct wd_init_attrs *attrs) } wd_sched_rr_release(alg_sched); } -