From f1779e9ab21a895f263478e1265fae0cf6ba0839 Mon Sep 17 00:00:00 2001 From: Zhangfei Gao Date: Sat, 4 Jan 2025 06:51:52 +0000 Subject: [PATCH] uadk: simulate async mode for driver only support sync There may driver does not support async mode. The operation is finished when send is over. So we can simulate async mode just return the correct number Signed-off-by: Zhangfei Gao --- adapter.c | 9 +++++++++ drv/isa_ce_sm3.c | 1 + drv/isa_ce_sm4.c | 1 + include/adapter.h | 5 +++++ include/wd_alg.h | 6 ++++++ libwd.map | 1 + wd_cipher.c | 23 +++++++++++++++++++++++ wd_comp.c | 27 ++++++++++++++++++++++----- wd_digest.c | 23 +++++++++++++++++++++++ 9 files changed, 91 insertions(+), 5 deletions(-) diff --git a/adapter.c b/adapter.c index 34ebf219..1bd02e6f 100644 --- a/adapter.c +++ b/adapter.c @@ -64,6 +64,7 @@ static void read_config_entries(char *conf, struct uadk_adapter *adapter, char * worker = &adapter->workers[i]; worker->driver = drv; worker->idx = i; + pthread_mutex_init(&worker->mutex, NULL); adapter->workers_nb++; if (drv_name) { free(drv_name); @@ -133,6 +134,7 @@ int uadk_adapter_add_workers(struct uadk_adapter *adapter, char *alg) adapter->workers[i].driver = workers[i].driver; adapter->workers[i].idx = i; adapter->workers_nb++; + pthread_mutex_init(&adapter->workers[i].mutex, NULL); if (adapter->workers_nb >= UADK_MAX_NB_WORKERS) break; @@ -179,3 +181,10 @@ struct uadk_adapter_worker *uadk_adapter_switch_worker( return new_worker; } + +void uadk_adapter_free(struct uadk_adapter *adapter) +{ + for (int i = 0; i < adapter->workers_nb; i++) + pthread_mutex_destroy(&adapter->workers[i].mutex); + free(adapter); +} diff --git a/drv/isa_ce_sm3.c b/drv/isa_ce_sm3.c index 54c2a9e7..88532297 100644 --- a/drv/isa_ce_sm3.c +++ b/drv/isa_ce_sm3.c @@ -32,6 +32,7 @@ static struct wd_alg_driver sm3_ce_alg_driver = { .drv_name = "isa_ce_sm3", .alg_name = "sm3", .calc_type = UADK_ALG_CE_INSTR, + .mode = UADK_DRV_SYNCONLY, .priority = 200, .queue_num = 1, .op_type_num = 1, diff --git a/drv/isa_ce_sm4.c b/drv/isa_ce_sm4.c index 5e448fa0..60a09f29 100644 --- a/drv/isa_ce_sm4.c +++ b/drv/isa_ce_sm4.c @@ -419,6 +419,7 @@ static int cipher_recv(struct wd_alg_driver *drv, handle_t ctx, void *msg) .drv_name = "isa_ce_sm4",\ .alg_name = (ce_alg_name),\ .calc_type = UADK_ALG_CE_INSTR,\ + .mode = UADK_DRV_SYNCONLY,\ .priority = 200,\ .op_type_num = 1,\ .fallback = 0,\ diff --git a/include/adapter.h b/include/adapter.h index b206bf75..025658bc 100644 --- a/include/adapter.h +++ b/include/adapter.h @@ -26,6 +26,9 @@ struct uadk_adapter_worker { struct wd_async_msg_pool pool; bool valid; int idx; + __u64 async_recv; + __u64 poll_recv; + pthread_mutex_t mutex; }; struct uadk_adapter { @@ -47,4 +50,6 @@ struct uadk_adapter_worker *uadk_adapter_switch_worker( struct uadk_adapter_worker *worker, int para ); + +void uadk_adapter_free(struct uadk_adapter *adapter); #endif diff --git a/include/wd_alg.h b/include/wd_alg.h index 307ff095..01cb1048 100644 --- a/include/wd_alg.h +++ b/include/wd_alg.h @@ -69,6 +69,11 @@ enum alg_dev_type { UADK_ALG_HW = 0x3 }; +enum alg_drv_mode { + UADK_DRV_NORMAL = 0x0, + UADK_DRV_SYNCONLY = 0x1, +}; + /* * @drv_name: name of the current device driver * @alg_name: name of the algorithm supported by the driver @@ -102,6 +107,7 @@ struct wd_alg_driver { const char *alg_name; int priority; int calc_type; + int mode; int queue_num; int op_type_num; void *priv; diff --git a/libwd.map b/libwd.map index c6e1dbde..f9d55207 100644 --- a/libwd.map +++ b/libwd.map @@ -59,5 +59,6 @@ global: uadk_adapter_add_workers; uadk_adapter_choose_worker; uadk_adapter_switch_worker; + uadk_adapter_free; local: *; }; diff --git a/wd_cipher.c b/wd_cipher.c index 413734c2..c16e7efc 100644 --- a/wd_cipher.c +++ b/wd_cipher.c @@ -791,6 +791,17 @@ int wd_do_cipher_async(handle_t h_sess, struct wd_cipher_req *req) worker = sess->worker; pthread_spin_unlock(&sess->worker_lock); + if (worker->driver->mode == UADK_DRV_SYNCONLY) { + ret = wd_do_cipher_sync(h_sess, req); + if (!ret) { + pthread_mutex_lock(&worker->mutex); + worker->async_recv++; + pthread_mutex_unlock(&worker->mutex); + req->cb(req, req->cb_param); + } + return ret; + } + idx = worker->sched->pick_next_ctx( worker->sched->h_sched_ctx, sess->sched_key[worker->idx], CTX_MODE_ASYNC); @@ -861,6 +872,18 @@ int wd_cipher_poll_ctx_(struct wd_sched *sched, __u32 idx, __u32 expt, __u32 *co *count = 0; + if (worker->driver->mode == UADK_DRV_SYNCONLY) { + pthread_mutex_lock(&worker->mutex); + recv_count = worker->async_recv - worker->poll_recv; + if (recv_count >= expt) + *count = expt; + else + *count = recv_count; + worker->poll_recv += *count; + pthread_mutex_unlock(&worker->mutex); + return 0; + } + ret = wd_check_ctx(&worker->config, CTX_MODE_ASYNC, idx); if (ret) return ret; diff --git a/wd_comp.c b/wd_comp.c index 826fc2ac..22428ea0 100644 --- a/wd_comp.c +++ b/wd_comp.c @@ -382,6 +382,18 @@ int wd_comp_poll_ctx_(struct wd_sched *sched, __u32 idx, __u32 expt, __u32 *coun *count = 0; + if (worker->driver->mode == UADK_DRV_SYNCONLY) { + pthread_mutex_lock(&worker->mutex); + recv_count = worker->async_recv - worker->poll_recv; + if (recv_count >= expt) + *count = expt; + else + *count = recv_count; + worker->poll_recv += *count; + pthread_mutex_unlock(&worker->mutex); + return 0; + } + ret = wd_check_ctx(&worker->config, CTX_MODE_ASYNC, idx); if (unlikely(ret)) return ret; @@ -612,11 +624,6 @@ static int wd_comp_check_params(struct wd_comp_sess *sess, return -WD_EINVAL; } - if (unlikely(mode == CTX_MODE_SYNC && req->cb)) { - WD_ERR("invalid: sync comp cb should be NULL!\n"); - return -WD_EINVAL; - } - return 0; } @@ -902,6 +909,16 @@ int wd_do_comp_async(handle_t h_sess, struct wd_comp_req *req) worker = sess->worker; pthread_spin_unlock(&sess->worker_lock); + if (worker->driver->mode == UADK_DRV_SYNCONLY) { + ret = wd_do_comp_sync(h_sess, req); + if (!ret) { + pthread_mutex_lock(&worker->mutex); + worker->async_recv++; + pthread_mutex_unlock(&worker->mutex); + req->cb(req, req->cb_param); + } + return ret; + } idx = worker->sched->pick_next_ctx( worker->sched->h_sched_ctx, sess->sched_key[worker->idx], CTX_MODE_ASYNC); diff --git a/wd_digest.c b/wd_digest.c index 7d45e41b..a04c41b7 100644 --- a/wd_digest.c +++ b/wd_digest.c @@ -745,6 +745,17 @@ int wd_do_digest_async(handle_t h_sess, struct wd_digest_req *req) worker = dsess->worker; pthread_spin_unlock(&dsess->worker_lock); + if (worker->driver->mode == UADK_DRV_SYNCONLY) { + ret = wd_do_digest_sync(h_sess, req); + if (!ret) { + pthread_mutex_lock(&worker->mutex); + worker->async_recv++; + pthread_mutex_unlock(&worker->mutex); + req->cb(req); + } + return ret; + } + idx = worker->sched->pick_next_ctx( worker->sched->h_sched_ctx, dsess->sched_key[worker->idx], CTX_MODE_ASYNC); @@ -816,6 +827,18 @@ int wd_digest_poll_ctx_(struct wd_sched *sched, __u32 idx, __u32 expt, __u32 *co *count = 0; + if (worker->driver->mode == UADK_DRV_SYNCONLY) { + pthread_mutex_lock(&worker->mutex); + recv_cnt = worker->async_recv - worker->poll_recv; + if (recv_cnt >= expt) + *count = expt; + else + *count = recv_cnt; + worker->poll_recv += *count; + pthread_mutex_unlock(&worker->mutex); + return 0; + } + ret = wd_check_ctx(&worker->config, CTX_MODE_ASYNC, idx); if (ret) return ret;