only for example
1. change drv ops api, adding para driver. 2. add func wd_alg_driver_xx to replace driver->ops. 3. drv should malloc/free ctx by itself. drv.init malloc priv ctx and drv.exit free 4. all drivers are wd_alg_driver, need remove all private driver struct like wd_aead/digest_driver 5, add wd_find_drv to find drv with drv_name and alg_name, considering several drivers supporting same alg. ignoring refcont, to make it simple, since only used drivers in para are probed
Signed-off-by: Zhangfei Gao zhangfei.gao@linaro.org --- Makefile.am | 3 +- adapter/adapter.c | 245 +++++++++++++++++++++++++++++++++++ adapter/adapter_private.h | 51 ++++++++ adapter/adapter_roundrobin.c | 90 +++++++++++++ drv/hisi_comp.c | 25 ++-- drv/hisi_hpre.c | 47 ++++--- drv/hisi_sec.c | 74 ++++++----- include/adapter.h | 27 ++++ include/drv/wd_aead_drv.h | 16 +-- include/drv/wd_digest_drv.h | 16 +-- include/wd_alg.h | 31 ++++- include/wd_util.h | 8 +- libwd.map | 7 + wd_aead.c | 66 +++++----- wd_alg.c | 26 ++++ wd_cipher.c | 40 ++++-- wd_comp.c | 45 +++++-- wd_dh.c | 8 +- wd_digest.c | 66 +++++++--- wd_ecc.c | 8 +- wd_rsa.c | 8 +- wd_util.c | 17 ++- 22 files changed, 747 insertions(+), 177 deletions(-) create mode 100644 adapter/adapter.c create mode 100644 adapter/adapter_private.h create mode 100644 adapter/adapter_roundrobin.c create mode 100644 include/adapter.h
diff --git a/Makefile.am b/Makefile.am index bd7b36f..3694f7c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -59,7 +59,8 @@ libwd_la_SOURCES=wd.c wd_mempool.c wd.h wd_alg.c wd_alg.h \ v1/drv/hisi_zip_udrv.c v1/drv/hisi_zip_udrv.h \ v1/drv/hisi_hpre_udrv.c v1/drv/hisi_hpre_udrv.h \ v1/drv/hisi_sec_udrv.c v1/drv/hisi_sec_udrv.h \ - v1/drv/hisi_rng_udrv.c v1/drv/hisi_rng_udrv.h + v1/drv/hisi_rng_udrv.c v1/drv/hisi_rng_udrv.h \ + adapter/adapter.c adapter/adapter_roundrobin.c
libwd_comp_la_SOURCES=wd_comp.c wd_comp.h wd_comp_drv.h wd_util.c wd_util.h \ wd_sched.c wd_sched.h wd.c wd.h wd_zlibwrapper.c wd_zlibwrapper.h diff --git a/adapter/adapter.c b/adapter/adapter.c new file mode 100644 index 0000000..b42384c --- /dev/null +++ b/adapter/adapter.c @@ -0,0 +1,245 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/* + * Copyright 2023-2024 Huawei Technologies Co.,Ltd. All rights reserved. + * Copyright 2023-2024 Linaro ltd. + */ + +#include <stdlib.h> +#include <stdio.h> +#include <dlfcn.h> + +#include "adapter_private.h" +#include "wd.h" + +int uadk_adapter_attach_worker(struct wd_alg_driver *adapter, + struct wd_alg_driver *drv, void *dlhandle) +{ + struct uadk_adapter_ctx *ctx = (struct uadk_adapter_ctx *)adapter->priv; + struct uadk_adapter_worker *worker; + int idx = ctx->workers_nb; + + if (idx >= UADK_MAX_NB_WORKERS) { + fprintf(stderr, "%s too many workers\n", __func__); + return -EINVAL; + } + + worker = &ctx->workers[idx]; + worker->driver = drv; + worker->dlhandle = dlhandle; + ctx->workers_nb++; + + return 0; +} + +/* todo */ +int uadk_adapter_parse(struct wd_alg_driver *adapter, char *lib_path, + char *drv_name, char *alg_name) +{ + struct wd_alg_driver *drv; + void *dlhandle = NULL; + int ret; + + if (lib_path) { + dlhandle = dlopen(lib_path, RTLD_NOW); + if (!dlhandle) { + fprintf(stderr, "%s failed to dlopen %s\n", __func__, dlerror()); + return -EINVAL; + } + } + + drv = wd_find_drv(drv_name, alg_name); + if (!drv) { + fprintf(stderr, "%s failed to find driver\n", __func__); + ret = -EINVAL; + goto fail; + } + + ret = uadk_adapter_attach_worker(adapter, drv, dlhandle); + if (ret) + goto fail; + + // parse cmdline and return + + // parse config + + // parse env + + // attach workers + + return 0; +fail: + if (dlhandle) + dlclose(dlhandle); + return ret; +} + +static int uadk_adapter_load_user_adapter(struct wd_alg_driver *adapter, + struct uadk_user_adapter *user_adapter) +{ + struct uadk_adapter_ctx *ctx = (struct uadk_adapter_ctx *)adapter->priv; + int ret; + + /* load scheduler instance operations functions */ + ctx->ops.init = user_adapter->ops->init; + ctx->ops.exit = user_adapter->ops->exit; + ctx->ops.send = user_adapter->ops->send; + ctx->ops.recv = user_adapter->ops->recv; + + if (ctx->priv) { + free(ctx->priv); + ctx->priv = NULL; + } + + if (ctx->ops.init) { + ret = ctx->ops.init(adapter); + if (ret) + return ret; + } + + ctx->mode = user_adapter->mode; + + return 0; +} + +int uadk_adapter_set_mode(struct wd_alg_driver *adapter, enum uadk_adapter_mode mode) +{ + struct uadk_adapter_ctx *ctx = (struct uadk_adapter_ctx *)adapter->priv; + int ret; + + if (mode == ctx->mode) + return 0; + + switch (mode) { + case UADK_ADAPT_MODE_ROUNDROBIN: + ret = uadk_adapter_load_user_adapter(adapter, uadk_user_adapter_roundrobin); + if (ret) + return ret; + + break; + + default: + fprintf(stderr, "Not yet supported"); + return -ENOTSUP; + } + + return 0; +} + +static int uadk_adapter_init(struct wd_alg_driver *adapter, void *conf) +{ + struct uadk_adapter_ctx *ctx = (struct uadk_adapter_ctx *)adapter->priv; + int ret, i; + + for (i = 0; i < ctx->workers_nb; i++) { + struct uadk_adapter_worker *worker = &ctx->workers[i]; + + if (worker->inited) + continue; + + ret = wd_alg_driver_init(worker->driver, conf); + if (ret) + continue; + worker->inited = true; + } + + return 0; +} + +static void uadk_adapter_exit(struct wd_alg_driver *adapter) +{ + struct uadk_adapter_ctx *ctx = (struct uadk_adapter_ctx *)adapter->priv; + int i; + + for (i = 0; i < ctx->workers_nb; i++) { + struct uadk_adapter_worker *worker = &ctx->workers[i]; + + if (!worker->inited) + continue; + + wd_alg_driver_exit(worker->driver); + worker->inited = false; + + if (worker->dlhandle) { + dlclose(worker->dlhandle); + worker->dlhandle = NULL; + } + } + + if (ctx->ops.exit) + ctx->ops.exit(adapter); +} + +static int uadk_adapter_send(struct wd_alg_driver *adapter, handle_t handle, void *msg) +{ + struct uadk_adapter_ctx *ctx = (struct uadk_adapter_ctx *)adapter->priv; + + if (unlikely(ctx->workers_nb == 0)) { + fprintf(stderr, "%s failed since no worker\n", __func__); + return -EINVAL; + } + + /* Just forward if only one worker */ + if (ctx->workers_nb == 1) + return wd_alg_driver_send(ctx->workers[0].driver, handle, msg); + + /* dispatch according to policy */ + if (ctx->ops.send) + return ctx->ops.send(adapter, handle, msg); + + return -EINVAL; +} + +static int uadk_adapter_recv(struct wd_alg_driver *adapter, handle_t handle, void *msg) +{ + struct uadk_adapter_ctx *ctx = (struct uadk_adapter_ctx *)adapter->priv; + + if (unlikely(ctx->workers_nb == 0)) { + fprintf(stderr, "%s failed since no worker\n", __func__); + return -EINVAL; + } + + /* Just forward if only one worker */ + if (ctx->workers_nb == 1) + return wd_alg_driver_recv(ctx->workers[0].driver, handle, msg); + + /* dispatch according to policy */ + if (ctx->ops.recv) + return ctx->ops.recv(adapter, handle, msg); + + return -EINVAL; +} + +struct wd_alg_driver *uadk_adapter_alloc(void) +{ + struct wd_alg_driver *adapter = malloc(sizeof(*adapter)); + + if (adapter == NULL) + return NULL; + + adapter->priv = malloc(sizeof(struct uadk_adapter_ctx)); + if (adapter->priv == NULL) { + free(adapter); + return NULL; + } + memset(adapter->priv, 0, sizeof(struct uadk_adapter_ctx)); + + adapter->init = uadk_adapter_init; + adapter->exit = uadk_adapter_exit; + adapter->send = uadk_adapter_send; + adapter->recv = uadk_adapter_recv; + + // parse env + // uadk_adapter_set_mode(adapter, mode); + + return adapter; +} + +void uadk_adapter_free(struct wd_alg_driver *adapter) +{ + if (adapter) + free(adapter->priv); + + free(adapter); +} + + diff --git a/adapter/adapter_private.h b/adapter/adapter_private.h new file mode 100644 index 0000000..c2d3112 --- /dev/null +++ b/adapter/adapter_private.h @@ -0,0 +1,51 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/* + * Copyright 2023-2024 Huawei Technologies Co.,Ltd. All rights reserved. + * Copyright 2023-2024 Linaro ltd. + */ + +#include "adapter.h" + +/* Maximum number of bonded drv per adapter */ +#ifndef UADK_MAX_NB_WORKERS +#define UADK_MAX_NB_WORKERS (8) +#endif + +struct uadk_adapter_ops { + // worker_attach; + // worker_detach; + + // scheduler_start; + // scheduler_stop; + int (*init)(struct wd_alg_driver *drv); + void (*exit)(struct wd_alg_driver *drv); + int (*send)(struct wd_alg_driver *drv, handle_t handle, void *drv_msg); + int (*recv)(struct wd_alg_driver *drv, handle_t handle, void *drv_msg); +}; + +struct uadk_user_adapter { + const char *name; /* adapter name */ + const char *description; /* adapter description */ + enum uadk_adapter_mode mode; /* adapter mode */ + struct uadk_adapter_ops *ops; /* adapter operation */ +}; + +struct uadk_adapter_worker { + struct wd_alg_driver *driver; + /* handle of shared library */ + void *dlhandle; + bool inited; +}; + +struct uadk_adapter_ctx { + /* priviate ctx */ + void *priv; + /* worker number */ + unsigned int workers_nb; + enum uadk_adapter_mode mode; + /* workers attached to the adapter */ + struct uadk_adapter_worker workers[UADK_MAX_NB_WORKERS]; + struct uadk_adapter_ops ops; +}; + +extern struct uadk_user_adapter *uadk_user_adapter_roundrobin; diff --git a/adapter/adapter_roundrobin.c b/adapter/adapter_roundrobin.c new file mode 100644 index 0000000..6f78cff --- /dev/null +++ b/adapter/adapter_roundrobin.c @@ -0,0 +1,90 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/* + * Copyright 2023-2024 Huawei Technologies Co.,Ltd. All rights reserved. + * Copyright 2023-2024 Linaro ltd. + */ + +#include <stdlib.h> +#include "adapter_private.h" + +struct rr_adapter_ctx { + unsigned int send_idx; + unsigned int recv_idx; +}; + +static int uadk_adapter_rr_init(struct wd_alg_driver *adapter) +{ + struct uadk_adapter_ctx *ctx = (struct uadk_adapter_ctx *)adapter->priv; + struct rr_adapter_ctx *priv; + + /* init may reenter, free and re-allocate */ + if (ctx->priv) + free(ctx->priv); + + priv = malloc(sizeof(*priv)); + if (!priv) + return -ENOMEM; + + memset(priv, 0, sizeof(*priv)); + ctx->priv = priv; + + return 0; +} + +static void uadk_adapter_rr_exit(struct wd_alg_driver *adapter) +{ + struct uadk_adapter_ctx *ctx = (struct uadk_adapter_ctx *)adapter->priv; + + free(ctx->priv); +} + +/* fixme, how to ensure send and recv are matched in async mode */ +static int uadk_adapter_rr_send(struct wd_alg_driver *adapter, handle_t handle, void *drv_msg) +{ + struct uadk_adapter_ctx *ctx = (struct uadk_adapter_ctx *)adapter->priv; + struct rr_adapter_ctx *priv = (struct rr_adapter_ctx *)ctx->priv; + struct uadk_adapter_worker *worker = &ctx->workers[priv->send_idx]; + int ret; + + ret = worker->driver->send(worker->driver, handle, drv_msg); + if (ret) + return ret; + + priv->send_idx++; + priv->send_idx %= ctx->workers_nb; + + return 0; +} + +static int uadk_adapter_rr_recv(struct wd_alg_driver *adapter, handle_t handle, void *drv_msg) +{ + struct uadk_adapter_ctx *ctx = (struct uadk_adapter_ctx *)adapter->priv; + struct rr_adapter_ctx *priv = (struct rr_adapter_ctx *)ctx->priv; + struct uadk_adapter_worker *worker = &ctx->workers[priv->recv_idx]; + int ret; + + ret = worker->driver->recv(worker->driver, handle, drv_msg); + if (ret) + return ret; + + priv->recv_idx++; + priv->recv_idx %= ctx->workers_nb; + + return 0; +} + +static struct uadk_adapter_ops adapter_rr_ops = { + uadk_adapter_rr_init, + uadk_adapter_rr_exit, + uadk_adapter_rr_send, + uadk_adapter_rr_recv, +}; + +static struct uadk_user_adapter rr_adapter = { + .name = "roundrobin-adapter", + .description = "adapter which will round robin across workers", + .mode = UADK_ADAPT_MODE_ROUNDROBIN, + .ops = &adapter_rr_ops +}; + +struct uadk_user_adapter *uadk_user_adapter_roundrobin = &rr_adapter; diff --git a/drv/hisi_comp.c b/drv/hisi_comp.c index 01e2ad8..76903a9 100644 --- a/drv/hisi_comp.c +++ b/drv/hisi_comp.c @@ -771,21 +771,26 @@ static void hisi_zip_sqe_ops_adapt(handle_t h_qp) } }
-static int hisi_zip_init(void *conf, void *priv) +static int hisi_zip_init(struct wd_alg_driver *drv, void *conf) { struct wd_ctx_config_internal *config = conf; - struct hisi_zip_ctx *zip_ctx = (struct hisi_zip_ctx *)priv; + struct hisi_zip_ctx *priv; struct hisi_qm_priv qm_priv; handle_t h_qp = 0; handle_t h_ctx; int i;
+ priv = malloc(sizeof(struct hisi_zip_ctx)); + if (!priv) + return -WD_EINVAL; + if (!config->ctx_num) { WD_ERR("invalid: zip init config ctx num is 0!\n"); + free(priv); return -WD_EINVAL; }
- memcpy(&zip_ctx->config, config, sizeof(struct wd_ctx_config_internal)); + memcpy(&priv->config, config, sizeof(struct wd_ctx_config_internal)); /* allocate qp for each context */ for (i = 0; i < config->ctx_num; i++) { h_ctx = config->ctxs[i].ctx; @@ -802,6 +807,7 @@ static int hisi_zip_init(void *conf, void *priv) }
hisi_zip_sqe_ops_adapt(h_qp); + drv->priv = priv;
return 0; out: @@ -809,13 +815,14 @@ out: h_qp = (handle_t)wd_ctx_get_priv(config->ctxs[i].ctx); hisi_qm_free_qp(h_qp); } + free(priv); return -WD_EINVAL; }
-static void hisi_zip_exit(void *priv) +static void hisi_zip_exit(struct wd_alg_driver *drv) { - struct hisi_zip_ctx *zip_ctx = (struct hisi_zip_ctx *)priv; - struct wd_ctx_config_internal *config = &zip_ctx->config; + struct hisi_zip_ctx *priv = (struct hisi_zip_ctx *)drv->priv; + struct wd_ctx_config_internal *config = &priv->config; handle_t h_qp; int i;
@@ -823,6 +830,8 @@ static void hisi_zip_exit(void *priv) h_qp = (handle_t)wd_ctx_get_priv(config->ctxs[i].ctx); hisi_qm_free_qp(h_qp); } + + free(priv); }
static int fill_zip_comp_sqe(struct hisi_qp *qp, struct wd_comp_msg *msg, @@ -877,7 +886,7 @@ static int fill_zip_comp_sqe(struct hisi_qp *qp, struct wd_comp_msg *msg, return 0; }
-static int hisi_zip_comp_send(handle_t ctx, void *comp_msg) +static int hisi_zip_comp_send(struct wd_alg_driver *drv, handle_t ctx, void *comp_msg) { struct hisi_qp *qp = wd_ctx_get_priv(ctx); struct wd_comp_msg *msg = comp_msg; @@ -1040,7 +1049,7 @@ static int parse_zip_sqe(struct hisi_qp *qp, struct hisi_zip_sqe *sqe, return 0; }
-static int hisi_zip_comp_recv(handle_t ctx, void *comp_msg) +static int hisi_zip_comp_recv(struct wd_alg_driver *drv, handle_t ctx, void *comp_msg) { struct hisi_qp *qp = wd_ctx_get_priv(ctx); struct wd_comp_msg *recv_msg = comp_msg; diff --git a/drv/hisi_hpre.c b/drv/hisi_hpre.c index 4d21788..67fae63 100644 --- a/drv/hisi_hpre.c +++ b/drv/hisi_hpre.c @@ -496,50 +496,62 @@ out: return -WD_EINVAL; }
-static int hpre_rsa_dh_init(void *conf, void *priv) +static int hpre_rsa_dh_init(struct wd_alg_driver *drv, void *conf) { struct wd_ctx_config_internal *config = (struct wd_ctx_config_internal *)conf; - struct hisi_hpre_ctx *hpre_ctx = (struct hisi_hpre_ctx *)priv; struct hisi_qm_priv qm_priv; int ret;
+ drv->priv = malloc(sizeof(struct hisi_hpre_ctx)); + if (!drv->priv) + return -WD_EINVAL; + if (!config->ctx_num) { WD_ERR("invalid: hpre rsa/dh init config ctx num is 0!\n"); + free(drv->priv); return -WD_EINVAL; }
qm_priv.op_type = HPRE_HW_V2_ALG_TYPE; - ret = hpre_init_qm_priv(config, hpre_ctx, &qm_priv); - if (ret) + ret = hpre_init_qm_priv(config, drv->priv, &qm_priv); + if (ret) { + free(drv->priv); return ret; + }
return 0; }
-static int hpre_ecc_init(void *conf, void *priv) +static int hpre_ecc_init(struct wd_alg_driver *drv, void *conf) { struct wd_ctx_config_internal *config = (struct wd_ctx_config_internal *)conf; - struct hisi_hpre_ctx *hpre_ctx = (struct hisi_hpre_ctx *)priv; struct hisi_qm_priv qm_priv; int ret;
+ drv->priv = malloc(sizeof(struct hisi_hpre_ctx)); + if (!drv->priv) + return -WD_EINVAL; + if (!config->ctx_num) { WD_ERR("invalid: hpre ecc init config ctx num is 0!\n"); + free(drv->priv); return -WD_EINVAL; }
qm_priv.op_type = HPRE_HW_V2_ALG_TYPE; - ret = hpre_init_qm_priv(config, hpre_ctx, &qm_priv); - if (ret) + ret = hpre_init_qm_priv(config, drv->priv, &qm_priv); + if (ret) { + free(drv->priv); return ret; + }
return 0; }
-static void hpre_exit(void *priv) +static void hpre_exit(struct wd_alg_driver *drv) { - struct hisi_hpre_ctx *hpre_ctx = (struct hisi_hpre_ctx *)priv; - struct wd_ctx_config_internal *config = &hpre_ctx->config; + struct hisi_hpre_ctx *priv = (struct hisi_hpre_ctx *)drv->priv; + struct wd_ctx_config_internal *config = &priv->config; handle_t h_qp; int i;
@@ -547,9 +559,10 @@ static void hpre_exit(void *priv) h_qp = (handle_t)wd_ctx_get_priv(config->ctxs[i].ctx); hisi_qm_free_qp(h_qp); } + free(priv); }
-static int rsa_send(handle_t ctx, void *rsa_msg) +static int rsa_send(struct wd_alg_driver *drv, handle_t ctx, void *rsa_msg) { handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx); struct wd_rsa_msg *msg = rsa_msg; @@ -605,7 +618,7 @@ static void hpre_result_check(struct hisi_hpre_sqe *hw_msg, } }
-static int rsa_recv(handle_t ctx, void *rsa_msg) +static int rsa_recv(struct wd_alg_driver *drv, handle_t ctx, void *rsa_msg) { handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx); struct hisi_qp *qp = (struct hisi_qp *)h_qp; @@ -703,7 +716,7 @@ static int dh_out_transfer(struct wd_dh_msg *msg, return WD_SUCCESS; }
-static int dh_send(handle_t ctx, void *dh_msg) +static int dh_send(struct wd_alg_driver *drv, handle_t ctx, void *dh_msg) { handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx); struct wd_dh_msg *msg = dh_msg; @@ -748,7 +761,7 @@ static int dh_send(handle_t ctx, void *dh_msg) return hisi_qm_send(h_qp, &hw_msg, 1, &send_cnt); }
-static int dh_recv(handle_t ctx, void *dh_msg) +static int dh_recv(struct wd_alg_driver *drv, handle_t ctx, void *dh_msg) { handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx); struct hisi_qp *qp = (struct hisi_qp *)h_qp; @@ -1839,7 +1852,7 @@ free_dst: return ret; }
-static int ecc_send(handle_t ctx, void *ecc_msg) +static int ecc_send(struct wd_alg_driver *drv, handle_t ctx, void *ecc_msg) { handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx); struct wd_ecc_msg *msg = ecc_msg; @@ -2411,7 +2424,7 @@ fail: return ret; }
-static int ecc_recv(handle_t ctx, void *ecc_msg) +static int ecc_recv(struct wd_alg_driver *drv, handle_t ctx, void *ecc_msg) { handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx); struct wd_ecc_msg *msg = ecc_msg; diff --git a/drv/hisi_sec.c b/drv/hisi_sec.c index 0527bff..1dcb9af 100644 --- a/drv/hisi_sec.c +++ b/drv/hisi_sec.c @@ -517,8 +517,8 @@ 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(void *conf, void *priv); -void hisi_sec_exit(void *priv); +int hisi_sec_init(struct wd_alg_driver *drv, void *conf); +void hisi_sec_exit(struct wd_alg_driver *drv);
static int hisi_sec_get_usage(void *param) { @@ -1048,7 +1048,7 @@ static int fill_cipher_bd2(struct wd_cipher_msg *msg, struct hisi_sec_sqe *sqe) return 0; }
-int hisi_sec_cipher_send(handle_t ctx, void *cipher_msg) +int hisi_sec_cipher_send(struct wd_alg_driver *drv, handle_t ctx, void *cipher_msg) { handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx); struct wd_cipher_msg *msg = cipher_msg; @@ -1093,7 +1093,7 @@ int hisi_sec_cipher_send(handle_t ctx, void *cipher_msg) return 0; }
-int hisi_sec_cipher_recv(handle_t ctx, void *cipher_msg) +int hisi_sec_cipher_recv(struct wd_alg_driver *drv, handle_t ctx, void *cipher_msg) { handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx); struct wd_cipher_msg *recv_msg = cipher_msg; @@ -1245,7 +1245,7 @@ static int fill_cipher_bd3(struct wd_cipher_msg *msg, struct hisi_sec_sqe3 *sqe) return 0; }
-int hisi_sec_cipher_send_v3(handle_t ctx, void *cipher_msg) +int hisi_sec_cipher_send_v3(struct wd_alg_driver *drv, handle_t ctx, void *cipher_msg) { handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx); struct wd_cipher_msg *msg = cipher_msg; @@ -1335,7 +1335,7 @@ static void parse_cipher_bd3(struct hisi_qp *qp, struct hisi_sec_sqe3 *sqe, dump_sec_msg(temp_msg, "cipher"); }
-int hisi_sec_cipher_recv_v3(handle_t ctx, void *cipher_msg) +int hisi_sec_cipher_recv_v3(struct wd_alg_driver *drv, handle_t ctx, void *cipher_msg) { handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx); struct wd_cipher_msg *recv_msg = cipher_msg; @@ -1606,7 +1606,7 @@ static int digest_len_check(struct wd_digest_msg *msg, enum sec_bd_type type) return 0; }
-int hisi_sec_digest_send(handle_t ctx, void *digest_msg) +int hisi_sec_digest_send(struct wd_alg_driver *drv, handle_t ctx, void *digest_msg) { handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx); struct wd_digest_msg *msg = digest_msg; @@ -1673,7 +1673,7 @@ put_sgl: return ret; }
-int hisi_sec_digest_recv(handle_t ctx, void *digest_msg) +int hisi_sec_digest_recv(struct wd_alg_driver *drv, handle_t ctx, void *digest_msg) { handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx); struct wd_digest_msg *recv_msg = digest_msg; @@ -1698,10 +1698,10 @@ int hisi_sec_digest_recv(handle_t ctx, void *digest_msg) return 0; }
-static struct wd_digest_driver hisi_digest_driver = { +static struct wd_alg_driver hisi_digest_driver = { .drv_name = "hisi_sec2", .alg_name = "digest", - .drv_ctx_size = sizeof(struct hisi_sec_ctx), + // .drv_ctx_size = sizeof(struct hisi_sec_ctx), .init = hisi_sec_init, .exit = hisi_sec_exit, }; @@ -1860,7 +1860,7 @@ static void fill_digest_v3_scene(struct hisi_sec_sqe3 *sqe, sqe->bd_param |= (__u16)(de | scene); }
-int hisi_sec_digest_send_v3(handle_t ctx, void *digest_msg) +int hisi_sec_digest_send_v3(struct wd_alg_driver *drv, handle_t ctx, void *digest_msg) { handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx); struct wd_digest_msg *msg = digest_msg; @@ -1958,7 +1958,7 @@ static void parse_digest_bd3(struct hisi_qp *qp, struct hisi_sec_sqe3 *sqe, dump_sec_msg(temp_msg, "digest"); }
-int hisi_sec_digest_recv_v3(handle_t ctx, void *digest_msg) +int hisi_sec_digest_recv_v3(struct wd_alg_driver *drv, handle_t ctx, void *digest_msg) { handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx); struct wd_digest_msg *recv_msg = digest_msg; @@ -2225,7 +2225,7 @@ static int fill_aead_bd2(struct wd_aead_msg *msg, struct hisi_sec_sqe *sqe) return 0; }
-int hisi_sec_aead_send(handle_t ctx, void *aead_msg) +int hisi_sec_aead_send(struct wd_alg_driver *drv, handle_t ctx, void *aead_msg) { handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx); struct wd_aead_msg *msg = aead_msg; @@ -2318,7 +2318,7 @@ static void parse_aead_bd2(struct hisi_qp *qp, struct hisi_sec_sqe *sqe, dump_sec_msg(temp_msg, "aead"); }
-int hisi_sec_aead_recv(handle_t ctx, void *aead_msg) +int hisi_sec_aead_recv(struct wd_alg_driver *drv, handle_t ctx, void *aead_msg) { handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx); struct wd_aead_msg *recv_msg = aead_msg; @@ -2343,10 +2343,10 @@ int hisi_sec_aead_recv(handle_t ctx, void *aead_msg) return 0; }
-static struct wd_aead_driver hisi_aead_driver = { +static struct wd_alg_driver hisi_aead_driver = { .drv_name = "hisi_sec2", .alg_name = "aead", - .drv_ctx_size = sizeof(struct hisi_sec_ctx), +// .drv_ctx_size = sizeof(struct hisi_sec_ctx), .init = hisi_sec_init, .exit = hisi_sec_exit, }; @@ -2500,7 +2500,7 @@ static int fill_aead_bd3(struct wd_aead_msg *msg, struct hisi_sec_sqe3 *sqe) return 0; }
-int hisi_sec_aead_send_v3(handle_t ctx, void *aead_msg) +int hisi_sec_aead_send_v3(struct wd_alg_driver *drv, handle_t ctx, void *aead_msg) { handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx); struct wd_aead_msg *msg = aead_msg; @@ -2587,7 +2587,7 @@ static void parse_aead_bd3(struct hisi_qp *qp, struct hisi_sec_sqe3 *sqe, dump_sec_msg(temp_msg, "aead"); }
-int hisi_sec_aead_recv_v3(handle_t ctx, void *aead_msg) +int hisi_sec_aead_recv_v3(struct wd_alg_driver *drv, handle_t ctx, void *aead_msg) { handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx); struct wd_aead_msg *recv_msg = aead_msg; @@ -2625,11 +2625,11 @@ static void hisi_sec_driver_adapter(struct hisi_qp *qp) 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; + hisi_digest_driver.send = hisi_sec_digest_send; + hisi_digest_driver.recv = hisi_sec_digest_recv;
- hisi_aead_driver.aead_send = hisi_sec_aead_send; - hisi_aead_driver.aead_recv = hisi_sec_aead_recv; + hisi_aead_driver.send = hisi_sec_aead_send; + hisi_aead_driver.recv = hisi_sec_aead_recv; } else { WD_INFO("hisi sec init HIP09!\n"); alg_num = ARRAY_SIZE(cipher_alg_driver); @@ -2638,25 +2638,30 @@ static void hisi_sec_driver_adapter(struct hisi_qp *qp) 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; + hisi_digest_driver.send = hisi_sec_digest_send_v3; + hisi_digest_driver.recv = hisi_sec_digest_recv_v3;
- hisi_aead_driver.aead_send = hisi_sec_aead_send_v3; - hisi_aead_driver.aead_recv = hisi_sec_aead_recv_v3; + hisi_aead_driver.send = hisi_sec_aead_send_v3; + hisi_aead_driver.recv = hisi_sec_aead_recv_v3; } }
-int hisi_sec_init(void *conf, void *priv) +int hisi_sec_init(struct wd_alg_driver *drv, void *conf) { struct wd_ctx_config_internal *config = conf; - struct hisi_sec_ctx *sec_ctx = priv; struct hisi_qm_priv qm_priv; + struct hisi_sec_ctx *priv; handle_t h_qp = 0; handle_t h_ctx; int i, j;
+ priv = malloc(sizeof(struct hisi_sec_ctx)); + if (!priv) + return -WD_EINVAL; + if (!config->ctx_num) { WD_ERR("invalid: sec init config ctx num is 0!\n"); + free(priv); return -WD_EINVAL; }
@@ -2675,8 +2680,9 @@ int hisi_sec_init(void *conf, void *priv) if (!h_qp) goto out; } - memcpy(&sec_ctx->config, config, sizeof(struct wd_ctx_config_internal)); + memcpy(&priv->config, config, sizeof(struct wd_ctx_config_internal)); hisi_sec_driver_adapter((struct hisi_qp *)h_qp); + drv->priv = priv;
return 0;
@@ -2685,26 +2691,28 @@ out: h_qp = (handle_t)wd_ctx_get_priv(config->ctxs[j].ctx); hisi_qm_free_qp(h_qp); } + free(priv); return -WD_EINVAL; }
-void hisi_sec_exit(void *priv) +void hisi_sec_exit(struct wd_alg_driver *drv) { - struct hisi_sec_ctx *sec_ctx = priv; + struct hisi_sec_ctx *priv = (struct hisi_sec_ctx *)drv->priv; struct wd_ctx_config_internal *config; handle_t h_qp; int i;
- if (!sec_ctx) { + if (!priv) { WD_ERR("hisi sec exit input parameter is err!\n"); return; }
- config = &sec_ctx->config; + config = &priv->config; for (i = 0; i < config->ctx_num; i++) { h_qp = (handle_t)wd_ctx_get_priv(config->ctxs[i].ctx); hisi_qm_free_qp(h_qp); } + free(priv); }
static void __attribute__((constructor)) hisi_sec2_probe(void) diff --git a/include/adapter.h b/include/adapter.h new file mode 100644 index 0000000..ffb3f12 --- /dev/null +++ b/include/adapter.h @@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/* + * Copyright 2023-2024 Huawei Technologies Co.,Ltd. All rights reserved. + * Copyright 2023-2024 Linaro ltd. + */ + +#ifndef __ADAPTER_H +#define __ADAPTER_H + +#include "wd_alg.h" + +enum uadk_adapter_mode { + UADK_ADAPT_MODE_NONE, // no mode + UADK_ADAPT_MODE_ROUNDROBIN, // roundrobin + UADK_ADAPT_MODE_THRESHOLD, // > threshold, accelerator, + // < threshold, cpu + UADK_ADAPT_MODE_FAILOVER, // fail to enqueue (full or fail), switch to backup +}; + +struct wd_alg_driver *uadk_adapter_alloc(void); +void uadk_adapter_free(struct wd_alg_driver *adapter); +int uadk_adapter_set_mode(struct wd_alg_driver *adapter, enum uadk_adapter_mode mode); +int uadk_adapter_attach_worker(struct wd_alg_driver *adapter, + struct wd_alg_driver *drv, void *dlhandle); +int uadk_adapter_parse(struct wd_alg_driver *adapter, char *lib_path, + char *drv_name, char *alg_name); +#endif diff --git a/include/drv/wd_aead_drv.h b/include/drv/wd_aead_drv.h index 8446238..495d31a 100644 --- a/include/drv/wd_aead_drv.h +++ b/include/drv/wd_aead_drv.h @@ -63,23 +63,13 @@ struct wd_aead_msg { __u8 *mac; };
-struct wd_aead_driver { - const char *drv_name; - const char *alg_name; - __u32 drv_ctx_size; - int (*init)(void *conf, void *priv); - void (*exit)(void *priv); - int (*aead_send)(handle_t ctx, void *aead_msg); - int (*aead_recv)(handle_t ctx, void *aead_msg); -}; - -void wd_aead_set_driver(struct wd_aead_driver *drv); -struct wd_aead_driver *wd_aead_get_driver(void); +void wd_aead_set_driver(struct wd_alg_driver *drv); +struct wd_alg_driver *wd_aead_get_driver(void); struct wd_aead_msg *wd_aead_get_msg(__u32 idx, __u32 tag);
#ifdef WD_STATIC_DRV #define WD_AEAD_SET_DRIVER(drv) \ -struct wd_aead_driver *wd_aead_get_driver(void) \ +struct wd_alg_driver *wd_aead_get_driver(void) \ { \ return &drv; \ } diff --git a/include/drv/wd_digest_drv.h b/include/drv/wd_digest_drv.h index 96b32e2..5925a24 100644 --- a/include/drv/wd_digest_drv.h +++ b/include/drv/wd_digest_drv.h @@ -51,23 +51,13 @@ struct wd_digest_msg { __u64 long_data_len; };
-struct wd_digest_driver { - const char *drv_name; - const char *alg_name; - __u32 drv_ctx_size; - int (*init)(void *conf, void *priv); - void (*exit)(void *priv); - int (*digest_send)(handle_t ctx, void *digest_msg); - int (*digest_recv)(handle_t ctx, void *digest_msg); -}; - -void wd_digest_set_driver(struct wd_digest_driver *drv); -struct wd_digest_driver *wd_digest_get_driver(void); +void wd_digest_set_driver(struct wd_alg_driver *drv); +struct wd_alg_driver *wd_digest_get_driver(void); struct wd_digest_msg *wd_digest_get_msg(__u32 idx, __u32 tag);
#ifdef WD_STATIC_DRV #define WD_DIGEST_SET_DRIVER(drv) \ -struct wd_digest_driver *wd_digest_get_driver(void) \ +struct wd_alg_driver *wd_digest_get_driver(void) \ { \ return &drv; \ } diff --git a/include/wd_alg.h b/include/wd_alg.h index e25e191..3aa0f77 100644 --- a/include/wd_alg.h +++ b/include/wd_alg.h @@ -37,6 +37,7 @@ enum alg_priority { * algorithm business and requires queues to be executed separately * @priv_size: parameter memory size passed between the internal * interfaces of the driver + * @priv: pointer of priv ctx * @fallback: soft calculation driver handle when performing soft * calculation supplement * @init: callback interface for initializing device drivers @@ -55,15 +56,36 @@ struct wd_alg_driver { int queue_num; int op_type_num; int priv_size; + void *priv; 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 (*init)(struct wd_alg_driver *drv, void *conf); + void (*exit)(struct wd_alg_driver *drv); + int (*send)(struct wd_alg_driver *drv, handle_t ctx, void *drv_msg); + int (*recv)(struct wd_alg_driver *drv, handle_t ctx, void *drv_msg); int (*get_usage)(void *param); };
+inline int wd_alg_driver_init(struct wd_alg_driver *drv, void *conf) +{ + return drv->init(drv, conf); +} + +inline void wd_alg_driver_exit(struct wd_alg_driver *drv) +{ + drv->exit(drv); +} + +inline int wd_alg_driver_send(struct wd_alg_driver *drv, handle_t ctx, void *msg) +{ + return drv->send(drv, ctx, msg); +} + +inline int wd_alg_driver_recv(struct wd_alg_driver *drv, handle_t ctx, void *msg) +{ + return drv->recv(drv, ctx, msg); +} + int wd_alg_driver_register(struct wd_alg_driver *drv); void wd_alg_driver_unregister(struct wd_alg_driver *drv);
@@ -80,6 +102,7 @@ struct wd_alg_list {
struct wd_alg_driver *wd_request_drv(const char *alg_name, bool hw_mask); void wd_release_drv(struct wd_alg_driver *drv); +struct wd_alg_driver *wd_find_drv(char *drv_name, char *alg_name);
bool wd_drv_alg_support(const char *alg_name, struct wd_alg_driver *drv); diff --git a/include/wd_util.h b/include/wd_util.h index a730f36..4d76ef2 100644 --- a/include/wd_util.h +++ b/include/wd_util.h @@ -110,8 +110,10 @@ struct wd_ctx_attr { };
struct wd_msg_handle { - int (*send)(handle_t sess, void *msg); - int (*recv)(handle_t sess, void *msg); + int (*send)(struct wd_alg_driver *drv, handle_t ctx, void *drv_msg); + int (*recv)(struct wd_alg_driver *drv, handle_t ctx, void *drv_msg); + // int (*send)(handle_t sess, void *msg); + // int (*recv)(handle_t sess, void *msg); };
struct wd_init_attrs { @@ -363,7 +365,7 @@ int wd_set_epoll_en(const char *var_name, bool *epoll_en); * * Return 0 if successful or less than 0 otherwise. */ -int wd_handle_msg_sync(struct wd_msg_handle *msg_handle, handle_t ctx, +int wd_handle_msg_sync(struct wd_alg_driver *drv, struct wd_msg_handle *msg_handle, handle_t ctx, void *msg, __u64 *balance, bool epoll_en);
/** diff --git a/libwd.map b/libwd.map index 5522ec0..cdfdf7e 100644 --- a/libwd.map +++ b/libwd.map @@ -45,9 +45,16 @@ global: wd_alg_driver_unregister; wd_request_drv; wd_release_drv; + wd_find_drv; wd_drv_alg_support; wd_enable_drv; wd_disable_drv; wd_get_alg_head; + + uadk_adapter_alloc; + uadk_adapter_free; + uadk_adapter_set_mode; + uadk_adapter_attach_worker; + uadk_adapter_parse; local: *; }; diff --git a/wd_aead.c b/wd_aead.c index 9b80922..55c2308 100644 --- a/wd_aead.c +++ b/wd_aead.c @@ -6,6 +6,8 @@
#include <stdlib.h> #include <pthread.h> + +#include "adapter.h" #include "include/drv/wd_aead_drv.h" #include "wd_aead.h"
@@ -33,7 +35,7 @@ struct wd_aead_setting { enum wd_status status; struct wd_ctx_config_internal config; struct wd_sched sched; - struct wd_aead_driver *driver; + struct wd_alg_driver *driver; struct wd_async_msg_pool pool; void *sched_ctx; void *priv; @@ -79,7 +81,7 @@ static void __attribute__((destructor)) wd_aead_close_driver(void) } #endif
-void wd_aead_set_driver(struct wd_aead_driver *drv) +void wd_aead_set_driver(struct wd_alg_driver *drv) { wd_aead_setting.driver = drv; } @@ -396,9 +398,26 @@ static void wd_aead_clear_status(void)
int wd_aead_init(struct wd_ctx_config *config, struct wd_sched *sched) { - void *priv; - bool flag; + struct wd_alg_driver *adapter = NULL; + char lib_path[PATH_STR_SIZE]; + char *alg_name = "aead"; + char *drv_name = "hisi_sec2"; int ret; + bool flag; + + ret = wd_get_lib_file_path("libhisi_sec2.so", lib_path, false); + if (ret) + return ret; + adapter = uadk_adapter_alloc(); + if (!adapter) + return -WD_EINVAL; + + ret = uadk_adapter_parse(adapter, lib_path, drv_name, alg_name); + if (ret) { + uadk_adapter_free(adapter); + WD_ERR("failed to parse adapter\n"); + return -WD_EINVAL; + }
pthread_atfork(NULL, NULL, wd_aead_clear_status);
@@ -435,27 +454,18 @@ int wd_aead_init(struct wd_ctx_config *config, struct wd_sched *sched) if (ret < 0) goto out_clear_sched;
- /* init ctx related resources in specific driver */ - priv = calloc(1, wd_aead_setting.driver->drv_ctx_size); - if (!priv) { - ret = -WD_ENOMEM; - goto out_clear_pool; - } - wd_aead_setting.priv = priv; - - ret = wd_aead_setting.driver->init(&wd_aead_setting.config, priv); + // ret = wd_aead_setting.driver->init(&wd_aead_setting.config, priv); + ret = wd_alg_driver_init(adapter, &wd_aead_setting.config); if (ret < 0) { WD_ERR("failed to init aead dirver!\n"); - goto out_free_priv; + goto out_clear_pool; }
wd_alg_set_init(&wd_aead_setting.status); + wd_aead_setting.driver = adapter;
return 0;
-out_free_priv: - free(priv); - wd_aead_setting.priv = NULL; out_clear_pool: wd_uninit_async_request_pool(&wd_aead_setting.pool); out_clear_sched: @@ -469,14 +479,8 @@ out_clear_init:
void wd_aead_uninit(void) { - void *priv = wd_aead_setting.priv; - - if (!priv) - return; - - wd_aead_setting.driver->exit(priv); - wd_aead_setting.priv = NULL; - free(priv); + wd_alg_driver_exit(wd_aead_setting.driver); + uadk_adapter_free(wd_aead_setting.driver);
wd_uninit_async_request_pool(&wd_aead_setting.pool); wd_clear_sched(&wd_aead_setting.sched); @@ -517,11 +521,11 @@ static int send_recv_sync(struct wd_ctx_internal *ctx, struct wd_msg_handle msg_handle; int ret;
- msg_handle.send = wd_aead_setting.driver->aead_send; - msg_handle.recv = wd_aead_setting.driver->aead_recv; + msg_handle.send = wd_aead_setting.driver->send; + msg_handle.recv = wd_aead_setting.driver->recv;
pthread_spin_lock(&ctx->lock); - ret = wd_handle_msg_sync(&msg_handle, ctx->ctx, msg, NULL, + ret = wd_handle_msg_sync(wd_aead_setting.driver, &msg_handle, ctx->ctx, msg, NULL, wd_aead_setting.config.epoll_en); pthread_spin_unlock(&ctx->lock); return ret; @@ -597,7 +601,8 @@ int wd_do_aead_async(handle_t h_sess, struct wd_aead_req *req) fill_request_msg(msg, req, sess); msg->tag = msg_id;
- ret = wd_aead_setting.driver->aead_send(ctx->ctx, msg); + // ret = wd_aead_setting.driver->aead_send(ctx->ctx, msg); + ret = wd_alg_driver_send(wd_aead_setting.driver, ctx->ctx, msg); if (unlikely(ret < 0)) { if (ret != -WD_EBUSY) WD_ERR("failed to send BD, hw is err!\n"); @@ -645,7 +650,8 @@ int wd_aead_poll_ctx(__u32 idx, __u32 expt, __u32 *count) ctx = config->ctxs + idx;
do { - ret = wd_aead_setting.driver->aead_recv(ctx->ctx, &resp_msg); + ret = wd_alg_driver_recv(wd_aead_setting.driver, ctx->ctx, &resp_msg); + // ret = wd_aead_setting.driver->aead_recv(ctx->ctx, &resp_msg); if (ret == -WD_EAGAIN) { return ret; } else if (ret < 0) { diff --git a/wd_alg.c b/wd_alg.c index 5e4edaf..2a9f160 100644 --- a/wd_alg.c +++ b/wd_alg.c @@ -263,3 +263,29 @@ void wd_release_drv(struct wd_alg_driver *drv) pthread_mutex_unlock(&mutex); }
+struct wd_alg_driver *wd_find_drv(char *drv_name, char *alg_name) +{ + struct wd_alg_list *head = &alg_list_head; + struct wd_alg_list *pnext = head->next; + struct wd_alg_driver *drv = NULL; + + if (!pnext || !alg_name) { + WD_ERR("invalid: request alg param is error!\n"); + return NULL; + } + + pthread_mutex_lock(&mutex); + while (pnext) { + if (!strcmp(alg_name, pnext->alg_name) && + !strcmp(drv_name, pnext->drv_name)) { + drv = pnext->drv; + break; + } + pnext = pnext->next; + } + + pthread_mutex_unlock(&mutex); + + return drv; +} + diff --git a/wd_cipher.c b/wd_cipher.c index eca9711..138aad1 100644 --- a/wd_cipher.c +++ b/wd_cipher.c @@ -9,6 +9,7 @@ #include <sched.h> #include "include/drv/wd_cipher_drv.h" #include "wd_cipher.h" +#include "adapter.h"
#define XTS_MODE_KEY_DIVISOR 2 #define SM4_KEY_SIZE 16 @@ -89,8 +90,9 @@ static void wd_cipher_close_driver(void)
static int wd_cipher_open_driver(void) { - struct wd_alg_driver *driver = NULL; - const char *alg_name = "cbc(aes)"; + struct wd_alg_driver *adapter = NULL; + char *alg_name = "cbc(aes)"; + char *drv_name = "hisi_sec2"; char lib_path[PATH_STR_SIZE]; int ret;
@@ -98,13 +100,24 @@ static int wd_cipher_open_driver(void) * Compatible with the normal acquisition of device * drivers in the init interface */ - if (wd_cipher_setting.dlh_list) - return 0; + //if (wd_cipher_setting.dlh_list) + // return 0;
ret = wd_get_lib_file_path("libhisi_sec.so", lib_path, false); if (ret) return ret;
+ adapter = uadk_adapter_alloc(); + if (!adapter) + return -WD_EINVAL; + + ret = uadk_adapter_parse(adapter, lib_path, drv_name, alg_name); + if (ret) { + uadk_adapter_free(adapter); + WD_ERR("failed to parse adapter\n"); + return -WD_EINVAL; + } +/* wd_cipher_setting.dlhandle = dlopen(lib_path, RTLD_NOW); if (!wd_cipher_setting.dlhandle) { WD_ERR("failed to open libhisi_sec.so, %s\n", dlerror()); @@ -117,8 +130,8 @@ static int wd_cipher_open_driver(void) WD_ERR("failed to get %s driver support\n", alg_name); return -WD_EINVAL; } - - wd_cipher_setting.driver = driver; +*/ + wd_cipher_setting.driver = adapter;
return 0; } @@ -304,9 +317,12 @@ static int wd_cipher_common_init(struct wd_ctx_config *config, if (ret < 0) goto out_clear_sched;
+ ret = wd_alg_driver_init(wd_cipher_setting.driver, &wd_cipher_setting.config); + /* ret = wd_alg_init_driver(&wd_cipher_setting.config, wd_cipher_setting.driver, &wd_cipher_setting.priv); + */ if (ret) goto out_clear_pool;
@@ -334,8 +350,12 @@ static void wd_cipher_common_uninit(void) /* unset config, sched, driver */ wd_clear_sched(&wd_cipher_setting.sched);
+ wd_alg_driver_exit(wd_cipher_setting.driver); + uadk_adapter_free(wd_cipher_setting.driver); + /* wd_alg_uninit_driver(&wd_cipher_setting.config, wd_cipher_setting.driver, &priv); + */ }
int wd_cipher_init(struct wd_ctx_config *config, struct wd_sched *sched) @@ -582,7 +602,7 @@ static int send_recv_sync(struct wd_ctx_internal *ctx, msg_handle.recv = wd_cipher_setting.driver->recv;
pthread_spin_lock(&ctx->lock); - ret = wd_handle_msg_sync(&msg_handle, ctx->ctx, msg, NULL, + ret = wd_handle_msg_sync(wd_cipher_setting.driver, &msg_handle, ctx->ctx, msg, NULL, wd_cipher_setting.config.epoll_en); pthread_spin_unlock(&ctx->lock); return ret; @@ -658,7 +678,8 @@ 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->send(ctx->ctx, msg); + ret = wd_alg_driver_send(wd_cipher_setting.driver, 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"); @@ -706,7 +727,8 @@ int wd_cipher_poll_ctx(__u32 idx, __u32 expt, __u32 *count) ctx = config->ctxs + idx;
do { - ret = wd_cipher_setting.driver->recv(ctx->ctx, &resp_msg); + ret = wd_alg_driver_recv(wd_cipher_setting.driver, ctx->ctx, &resp_msg); + // ret = wd_cipher_setting.driver->recv(ctx->ctx, &resp_msg); if (ret == -WD_EAGAIN) return ret; else if (ret < 0) { diff --git a/wd_comp.c b/wd_comp.c index b7e0eb7..03e4ea1 100644 --- a/wd_comp.c +++ b/wd_comp.c @@ -12,6 +12,7 @@ #include <string.h> #include <time.h>
+#include "adapter.h" #include "drv/wd_comp_drv.h" #include "wd_comp.h"
@@ -38,6 +39,7 @@ struct wd_comp_sess { void *sched_key; };
+/* can we remove this? */ struct wd_comp_setting { enum wd_status status; struct wd_ctx_config_internal config; @@ -63,22 +65,34 @@ static void wd_comp_close_driver(void)
static int wd_comp_open_driver(void) { - struct wd_alg_driver *driver = NULL; + struct wd_alg_driver *adapter = NULL; char lib_path[PATH_STR_SIZE]; - const char *alg_name = "zlib"; + char *alg_name = "zlib"; + char *drv_name = "hisi_zip"; int ret;
/* * Compatible with the normal acquisition of device * drivers in the init interface */ - if (wd_comp_setting.dlh_list) - return 0; + //if (wd_comp_setting.dlh_list) + // return 0;
ret = wd_get_lib_file_path("libhisi_zip.so", lib_path, false); if (ret) return ret; + adapter = uadk_adapter_alloc(); + if (!adapter) + return -WD_EINVAL; + + ret = uadk_adapter_parse(adapter, lib_path, drv_name, alg_name); + if (ret) { + uadk_adapter_free(adapter); + WD_ERR("failed to parse adapter\n"); + return -WD_EINVAL; + }
+/* wd_comp_setting.dlhandle = dlopen(lib_path, RTLD_NOW); if (!wd_comp_setting.dlhandle) { WD_ERR("failed to open libhisi_zip.so, %s\n", dlerror()); @@ -91,8 +105,8 @@ static int wd_comp_open_driver(void) WD_ERR("failed to get %s driver support\n", alg_name); return -WD_EINVAL; } - - wd_comp_setting.driver = driver; +*/ + wd_comp_setting.driver = adapter;
return 0; } @@ -126,9 +140,13 @@ static int wd_comp_init_nolock(struct wd_ctx_config *config, struct wd_sched *sc if (ret < 0) goto out_clear_sched;
+ ret = wd_alg_driver_init(wd_comp_setting.driver, &wd_comp_setting.config); + + /* ret = wd_alg_init_driver(&wd_comp_setting.config, wd_comp_setting.driver, &wd_comp_setting.priv); + */ if (ret) goto out_clear_pool;
@@ -156,8 +174,11 @@ static void wd_comp_uninit_nolock(void) /* unset config, sched, driver */ wd_clear_sched(&wd_comp_setting.sched);
- wd_alg_uninit_driver(&wd_comp_setting.config, - wd_comp_setting.driver, &priv); + wd_alg_driver_exit(wd_comp_setting.driver); + uadk_adapter_free(wd_comp_setting.driver); + + // wd_alg_uninit_driver(&wd_comp_setting.config, + // wd_comp_setting.driver, &priv); }
int wd_comp_init(struct wd_ctx_config *config, struct wd_sched *sched) @@ -326,7 +347,8 @@ int wd_comp_poll_ctx(__u32 idx, __u32 expt, __u32 *count) ctx = config->ctxs + idx;
do { - ret = wd_comp_setting.driver->recv(ctx->ctx, &resp_msg); + ret = wd_alg_driver_recv(wd_comp_setting.driver, 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"); @@ -561,7 +583,7 @@ static int wd_comp_sync_job(struct wd_comp_sess *sess, msg_handle.recv = wd_comp_setting.driver->recv;
pthread_spin_lock(&ctx->lock); - ret = wd_handle_msg_sync(&msg_handle, ctx->ctx, msg, + ret = wd_handle_msg_sync(wd_comp_setting.driver, &msg_handle, ctx->ctx, msg, NULL, config->epoll_en); pthread_spin_unlock(&ctx->lock);
@@ -817,7 +839,8 @@ 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->send(ctx->ctx, msg); + ret = wd_alg_driver_send(wd_comp_setting.driver, 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; diff --git a/wd_dh.c b/wd_dh.c index d45ac89..c0b6c55 100644 --- a/wd_dh.c +++ b/wd_dh.c @@ -353,7 +353,7 @@ int wd_do_dh_sync(handle_t sess, struct wd_dh_req *req) msg_handle.recv = wd_dh_setting.driver->recv;
pthread_spin_lock(&ctx->lock); - ret = wd_handle_msg_sync(&msg_handle, ctx->ctx, &msg, &balance, + ret = wd_handle_msg_sync(wd_dh_setting.driver, &msg_handle, ctx->ctx, &msg, &balance, wd_dh_setting.config.epoll_en); pthread_spin_unlock(&ctx->lock); if (unlikely(ret)) @@ -399,7 +399,8 @@ int wd_do_dh_async(handle_t sess, struct wd_dh_req *req) goto fail_with_msg; msg->tag = mid;
- ret = wd_dh_setting.driver->send(ctx->ctx, msg); + ret = wd_alg_driver_send(wd_dh_setting.driver, ctx->ctx, msg); + //ret = wd_dh_setting.driver->send(ctx->ctx, msg); if (unlikely(ret)) { if (ret != -WD_EBUSY) WD_ERR("failed to send dh BD, hw is err!\n"); @@ -449,7 +450,8 @@ int wd_dh_poll_ctx(__u32 idx, __u32 expt, __u32 *count) ctx = config->ctxs + idx;
do { - ret = wd_dh_setting.driver->recv(ctx->ctx, &rcv_msg); + ret = wd_alg_driver_recv(wd_dh_setting.driver, ctx->ctx, &rcv_msg); + // ret = wd_dh_setting.driver->recv(ctx->ctx, &rcv_msg); if (ret == -WD_EAGAIN) { return ret; } else if (unlikely(ret)) { diff --git a/wd_digest.c b/wd_digest.c index 8c01709..cab6f94 100644 --- a/wd_digest.c +++ b/wd_digest.c @@ -8,6 +8,7 @@ #include <pthread.h> #include "include/drv/wd_digest_drv.h" #include "wd_digest.h" +#include "adapter.h"
#define XTS_MODE_KEY_DIVISOR 2 #define SM4_KEY_SIZE 16 @@ -41,7 +42,7 @@ struct wd_digest_setting { enum wd_status status; struct wd_ctx_config_internal config; struct wd_sched sched; - struct wd_digest_driver *driver; + struct wd_alg_driver *driver; struct wd_async_msg_pool pool; void *sched_ctx; void *priv; @@ -90,7 +91,7 @@ static void __attribute__((destructor)) wd_digest_close_driver(void) } #endif
-void wd_digest_set_driver(struct wd_digest_driver *drv) +void wd_digest_set_driver(struct wd_alg_driver *drv) { wd_digest_setting.driver = drv; } @@ -190,9 +191,26 @@ static void wd_digest_clear_status(void)
int wd_digest_init(struct wd_ctx_config *config, struct wd_sched *sched) { - void *priv; - bool flag; + struct wd_alg_driver *adapter = NULL; + char lib_path[PATH_STR_SIZE]; + char *alg_name = "digest"; + char *drv_name = "hisi_sec2"; int ret; + bool flag; + + ret = wd_get_lib_file_path("libhisi_sec2.so", lib_path, false); + if (ret) + return ret; + adapter = uadk_adapter_alloc(); + if (!adapter) + return -WD_EINVAL; + + ret = uadk_adapter_parse(adapter, lib_path, drv_name, alg_name); + if (ret) { + uadk_adapter_free(adapter); + WD_ERR("failed to parse adapter\n"); + return -WD_EINVAL; + }
pthread_atfork(NULL, NULL, wd_digest_clear_status);
@@ -229,28 +247,33 @@ int wd_digest_init(struct wd_ctx_config *config, struct wd_sched *sched) if (ret < 0) goto out_clear_sched;
+ /* drv should alloc priv by itself */ /* init ctx related resources in specific driver */ +/* priv = calloc(1, wd_digest_setting.driver->drv_ctx_size); if (!priv) { ret = -WD_ENOMEM; goto out_clear_pool; } wd_digest_setting.priv = priv; +*/
- ret = wd_digest_setting.driver->init(&wd_digest_setting.config, priv); + ret = wd_alg_driver_init(adapter, &wd_digest_setting.config); + //ret = wd_digest_setting.driver->init(&wd_digest_setting.config, priv); if (ret < 0) { WD_ERR("failed to init digest dirver!\n"); goto out_free_priv; }
+ wd_digest_setting.driver = adapter; wd_alg_set_init(&wd_digest_setting.status);
return 0;
out_free_priv: - free(priv); +// free(priv); wd_digest_setting.priv = NULL; -out_clear_pool: +// out_clear_pool: wd_uninit_async_request_pool(&wd_digest_setting.pool); out_clear_sched: wd_clear_sched(&wd_digest_setting.sched); @@ -263,14 +286,18 @@ out_clear_init:
void wd_digest_uninit(void) { - void *priv = wd_digest_setting.priv; + struct wd_alg_driver *adapter = wd_digest_setting.driver; + //void *priv = wd_digest_setting.priv;
- if (!priv) - return; + //if (!priv) + // return;
- wd_digest_setting.driver->exit(priv); - wd_digest_setting.priv = NULL; - free(priv); + wd_alg_driver_exit(adapter); + // wd_digest_setting.driver->exit(wd_digest_setting.driver); + // wd_digest_setting.priv = NULL; + uadk_adapter_free(adapter); + + // free(priv);
wd_uninit_async_request_pool(&wd_digest_setting.pool);
@@ -404,11 +431,11 @@ static int send_recv_sync(struct wd_ctx_internal *ctx, struct wd_digest_sess *ds struct wd_msg_handle msg_handle; int ret;
- msg_handle.send = wd_digest_setting.driver->digest_send; - msg_handle.recv = wd_digest_setting.driver->digest_recv; + msg_handle.send = wd_digest_setting.driver->send; + msg_handle.recv = wd_digest_setting.driver->recv;
pthread_spin_lock(&ctx->lock); - ret = wd_handle_msg_sync(&msg_handle, ctx->ctx, msg, + ret = wd_handle_msg_sync(wd_digest_setting.driver, &msg_handle, ctx->ctx, msg, NULL, wd_digest_setting.config.epoll_en); if (unlikely(ret)) goto out; @@ -493,7 +520,8 @@ int wd_do_digest_async(handle_t h_sess, struct wd_digest_req *req) fill_request_msg(msg, req, dsess); msg->tag = msg_id;
- ret = wd_digest_setting.driver->digest_send(ctx->ctx, msg); + // ret = wd_digest_setting.driver->send(ctx->ctx, msg); + ret = wd_alg_driver_send(wd_digest_setting.driver, ctx->ctx, msg); if (unlikely(ret < 0)) { if (ret != -WD_EBUSY) WD_ERR("failed to send BD, hw is err!\n"); @@ -541,8 +569,8 @@ int wd_digest_poll_ctx(__u32 idx, __u32 expt, __u32 *count) ctx = config->ctxs + idx;
do { - ret = wd_digest_setting.driver->digest_recv(ctx->ctx, - &recv_msg); + ret = wd_alg_driver_recv(wd_digest_setting.driver, ctx->ctx, &recv_msg); + // ret = wd_digest_setting.driver->digest_recv(ctx->ctx, &recv_msg); if (ret == -WD_EAGAIN) { return ret; } else if (ret < 0) { diff --git a/wd_ecc.c b/wd_ecc.c index 57954e0..d5db5ad 100644 --- a/wd_ecc.c +++ b/wd_ecc.c @@ -1557,7 +1557,7 @@ int wd_do_ecc_sync(handle_t h_sess, struct wd_ecc_req *req) msg_handle.recv = wd_ecc_setting.driver->recv;
pthread_spin_lock(&ctx->lock); - ret = wd_handle_msg_sync(&msg_handle, ctx->ctx, &msg, &balance, + ret = wd_handle_msg_sync(wd_ecc_setting.driver, &msg_handle, ctx->ctx, &msg, &balance, wd_ecc_setting.config.epoll_en); pthread_spin_unlock(&ctx->lock); if (unlikely(ret)) @@ -2237,7 +2237,8 @@ int wd_do_ecc_async(handle_t sess, struct wd_ecc_req *req) goto fail_with_msg; msg->tag = mid;
- ret = wd_ecc_setting.driver->send(ctx->ctx, msg); + // ret = wd_ecc_setting.driver->send(ctx->ctx, msg); + ret = wd_alg_driver_send(wd_ecc_setting.driver, ctx->ctx, msg); if (unlikely(ret)) { if (ret != -WD_EBUSY) WD_ERR("failed to send ecc BD, hw is err!\n"); @@ -2285,7 +2286,8 @@ int wd_ecc_poll_ctx(__u32 idx, __u32 expt, __u32 *count) ctx = config->ctxs + idx;
do { - ret = wd_ecc_setting.driver->recv(ctx->ctx, &recv_msg); + ret = wd_alg_driver_recv(wd_ecc_setting.driver, ctx->ctx, &recv_msg); + // ret = wd_ecc_setting.driver->recv(ctx->ctx, &recv_msg); if (ret == -WD_EAGAIN) { return ret; } else if (ret < 0) { diff --git a/wd_rsa.c b/wd_rsa.c index 77fe5c0..d402ecf 100644 --- a/wd_rsa.c +++ b/wd_rsa.c @@ -415,7 +415,7 @@ int wd_do_rsa_sync(handle_t h_sess, struct wd_rsa_req *req) msg_handle.recv = wd_rsa_setting.driver->recv;
pthread_spin_lock(&ctx->lock); - ret = wd_handle_msg_sync(&msg_handle, ctx->ctx, &msg, &balance, + ret = wd_handle_msg_sync(wd_rsa_setting.driver, &msg_handle, ctx->ctx, &msg, &balance, wd_rsa_setting.config.epoll_en); pthread_spin_unlock(&ctx->lock); if (unlikely(ret)) @@ -461,7 +461,8 @@ int wd_do_rsa_async(handle_t sess, struct wd_rsa_req *req) goto fail_with_msg; msg->tag = mid;
- ret = wd_rsa_setting.driver->send(ctx->ctx, msg); + ret = wd_alg_driver_send(wd_rsa_setting.driver, ctx->ctx, msg); + //ret = wd_rsa_setting.driver->send(ctx->ctx, msg); if (unlikely(ret)) { if (ret != -WD_EBUSY) WD_ERR("failed to send rsa BD, hw is err!\n"); @@ -509,7 +510,8 @@ int wd_rsa_poll_ctx(__u32 idx, __u32 expt, __u32 *count) ctx = config->ctxs + idx;
do { - ret = wd_rsa_setting.driver->recv(ctx->ctx, &recv_msg); + ret = wd_alg_driver_recv(wd_rsa_setting.driver, ctx->ctx, &recv_msg); + //ret = wd_rsa_setting.driver->recv(ctx->ctx, &recv_msg); if (ret == -WD_EAGAIN) { return ret; } else if (ret < 0) { diff --git a/wd_util.c b/wd_util.c index dab4fc8..879612a 100644 --- a/wd_util.c +++ b/wd_util.c @@ -1785,7 +1785,7 @@ int wd_set_epoll_en(const char *var_name, bool *epoll_en) return 0; }
-int wd_handle_msg_sync(struct wd_msg_handle *msg_handle, handle_t ctx, +int wd_handle_msg_sync(struct wd_alg_driver *drv, struct wd_msg_handle *msg_handle, handle_t ctx, void *msg, __u64 *balance, bool epoll_en) { __u64 timeout = WD_RECV_MAX_CNT_NOSLEEP; @@ -1795,7 +1795,7 @@ int wd_handle_msg_sync(struct wd_msg_handle *msg_handle, handle_t ctx, if (balance) timeout = WD_RECV_MAX_CNT_SLEEP;
- ret = msg_handle->send(ctx, msg); + ret = msg_handle->send(drv, ctx, msg); if (unlikely(ret < 0)) { WD_ERR("failed to send msg to hw, ret = %d!\n", ret); return ret; @@ -1808,7 +1808,7 @@ int wd_handle_msg_sync(struct wd_msg_handle *msg_handle, handle_t ctx, WD_ERR("wd ctx wait timeout(%d)!\n", ret); }
- ret = msg_handle->recv(ctx, msg); + ret = msg_handle->recv(drv, ctx, msg); if (ret == -WD_EAGAIN) { if (unlikely(rx_cnt++ >= timeout)) { WD_ERR("failed to recv msg: timeout!\n"); @@ -1915,13 +1915,16 @@ static void wd_alg_uninit_fallback(struct wd_alg_driver *fb_driver) int wd_alg_init_driver(struct wd_ctx_config_internal *config, struct wd_alg_driver *driver, void **drv_priv) { - void *priv; +// void *priv; int ret;
+ /* driver should alloc ctx by itself */ /* Init ctx related resources in specific driver */ +/* priv = calloc(1, driver->priv_size); if (!priv) return -WD_ENOMEM; +*/
if (!driver->init) { driver->fallback = 0; @@ -1930,7 +1933,7 @@ int wd_alg_init_driver(struct wd_ctx_config_internal *config, goto err_alloc; }
- ret = driver->init(config, priv); + ret = driver->init(driver, config); if (ret < 0) { WD_ERR("driver init failed.\n"); goto err_alloc; @@ -1943,12 +1946,12 @@ int wd_alg_init_driver(struct wd_ctx_config_internal *config, WD_ERR("soft alg driver init failed.\n"); } } - *drv_priv = priv; + // *drv_priv = priv;
return 0;
err_alloc: - free(priv); + //free(priv); return ret; }