Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapter part2 #642

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ uadk_drivers_LTLIBRARIES=libhisi_sec.la libhisi_hpre.la libhisi_zip.la \
libisa_ce.la libisa_sve.la libhisi_dae.la

libwd_la_SOURCES=wd.c wd_mempool.c wd.h wd_alg.c wd_alg.h \
wd_sched.c wd_util.c \
v1/wd.c v1/wd.h v1/wd_adapter.c v1/wd_adapter.h \
v1/wd_rng.c v1/wd_rng.h \
v1/wd_rsa.c v1/wd_rsa.h \
Expand All @@ -64,7 +65,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.c

libwd_dae_la_SOURCES=wd_dae.h wd_agg.h wd_agg_drv.h wd_agg.c \
wd_util.c wd_util.h wd_sched.c wd_sched.h wd.c wd.h
Expand Down
181 changes: 181 additions & 0 deletions adapter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@

/* SPDX-License-Identifier: Apache-2.0 */
/*
* Copyright 2024-2025 Huawei Technologies Co.,Ltd. All rights reserved.
* Copyright 2024-2025 Linaro ltd.
*/

#include "adapter.h"

#define CONFIG_FILE_ENV "UADK_CONF"
#define DRIVER_NAME_KEY "driver_name"
#define MODE_KEY "mode"
#define LOOP_KEY "looptime"
#define WORKERS_NB 8

static int read_value_int(char *conf, const char *key)
{
FILE *fp = fopen(conf, "r");
char line[1024];
int ret = 0;

if (fp == NULL)
return 0;

while (fgets(line, sizeof(line), fp) != NULL) {
char *key_value = strtok(line, "=");
if (key_value && strcmp(key_value, key) == 0) {
char *value = strtok(NULL, "\n");

if (value) {
ret = atoi(value);
goto exit;
}
}
}
exit:
fclose(fp);
return ret;
}

static void read_config_entries(char *conf, struct uadk_adapter *adapter, char *alg_name)
{
struct uadk_adapter_worker *worker;
FILE *fp = fopen(conf, "r");
struct wd_alg_driver *drv;
char *drv_name = NULL;
char line[1024];
int i = 0;

if (fp == NULL)
return;

while (fgets(line, sizeof(line), fp) != NULL) {
char *key_value = strtok(line, "=");

if (key_value && strcmp(key_value, DRIVER_NAME_KEY) == 0)
drv_name = strdup(strtok(NULL, "\n"));

if ((drv_name != NULL) && (alg_name != NULL)) {
drv = wd_find_drv(drv_name, alg_name, 0);
if (!drv)
continue;

worker = &adapter->workers[i];
worker->driver = drv;
worker->idx = i;
adapter->workers_nb++;
if (drv_name) {
free(drv_name);
drv_name = NULL;
}

if (++i >= UADK_MAX_NB_WORKERS)
break;
}
}

if (drv_name)
free(drv_name);

fclose(fp);
}

int uadk_adapter_add_workers(struct uadk_adapter *adapter, char *alg)
{
char *conf = getenv(CONFIG_FILE_ENV);
struct uadk_adapter_worker workers[WORKERS_NB];
struct uadk_adapter_worker worker;
struct wd_alg_driver *drv;
int idx = 0, i, j;

adapter->looptime = UADK_WORKER_LOOPTIME;

if (conf != NULL) {
int looptime = 0;

/* if env UADK_CONF exist, parse config first */
adapter->mode = read_value_int(conf, MODE_KEY);
looptime = read_value_int(conf, LOOP_KEY);
if (looptime != 0)
adapter->looptime = looptime;

read_config_entries(conf, adapter, alg);
if (adapter->workers_nb != 0)
return 0;
}

/* Then parse all system drivers to workers */
do {
drv = wd_find_drv(NULL, alg, idx);
if (!drv)
break;

workers[idx++].driver = drv;

if (idx >= WORKERS_NB)
break;
} while (drv);

/* Sorted as priority */
for (i = 0; i < idx; i++) {
for (j = i; j < idx; j++) {
if (workers[i].driver->priority <
workers[j].driver->priority) {
worker.driver = workers[i].driver;
workers[i].driver = workers[j].driver;
workers[j].driver = worker.driver;
}
}
}

for (i = 0; i < idx; i++) {
adapter->workers[i].driver = workers[i].driver;
adapter->workers[i].idx = i;
adapter->workers_nb++;

if (adapter->workers_nb >= UADK_MAX_NB_WORKERS)
break;
}

return (adapter->workers_nb == 0);
}

struct uadk_adapter_worker *uadk_adapter_choose_worker(
struct uadk_adapter *adapter,
enum alg_task_type type)
{
struct uadk_adapter_worker *worker;

/* use worker[0] for simplicity now */
worker = &adapter->workers[0];
worker->valid = true;

return worker;
}

struct uadk_adapter_worker *uadk_adapter_switch_worker(
struct uadk_adapter *adapter,
struct uadk_adapter_worker *worker,
int para)
{
struct uadk_adapter_worker *new_worker;
int idx = worker->idx;

if (adapter->workers_nb == 1)
return worker;

if (para) {
idx += 1;
} else {
if (idx == 0)
idx = adapter->workers_nb - 1;
else
idx -= 1;
}

new_worker = &adapter->workers[idx];
new_worker->valid = true;

return new_worker;
}
9 changes: 5 additions & 4 deletions drv/hisi_comp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1003,9 +1003,10 @@ static void get_ctx_buf(struct hisi_zip_sqe *sqe,
}
}

static int parse_zip_sqe(struct hisi_qp *qp, struct hisi_zip_sqe *sqe,
struct wd_comp_msg *msg)
static int parse_zip_sqe(struct wd_alg_driver *drv, struct hisi_qp *qp,
struct hisi_zip_sqe *sqe, struct wd_comp_msg *msg)
{
struct hisi_zip_ctx *priv = (struct hisi_zip_ctx *)drv->priv;
__u32 buf_type = (sqe->dw9 & HZ_BUF_TYPE_MASK) >> BUF_TYPE_SHIFT;
__u16 ctx_st = sqe->ctx_dw0 & HZ_CTX_ST_MASK;
__u16 lstblk = sqe->dw3 & HZ_LSTBLK_MASK;
Expand All @@ -1030,7 +1031,7 @@ static int parse_zip_sqe(struct hisi_qp *qp, struct hisi_zip_sqe *sqe,
recv_msg->tag = tag;

if (qp->q_info.qp_mode == CTX_MODE_ASYNC) {
recv_msg = wd_comp_get_msg(qp->q_info.idx, tag);
recv_msg = wd_find_msg_in_pool(priv->config.pool, qp->q_info.idx, tag);
if (unlikely(!recv_msg)) {
WD_ERR("failed to get send msg! idx = %u, tag = %u!\n",
qp->q_info.idx, tag);
Expand Down Expand Up @@ -1091,7 +1092,7 @@ static int hisi_zip_comp_recv(struct wd_alg_driver *drv, handle_t ctx, void *com
if (unlikely(ret < 0))
return ret;

return parse_zip_sqe(qp, &sqe, recv_msg);
return parse_zip_sqe(drv, qp, &sqe, recv_msg);
}

#define GEN_ZIP_ALG_DRIVER(zip_alg_name) \
Expand Down
3 changes: 2 additions & 1 deletion drv/hisi_dae.c
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ static void fill_hashagg_msg_task_err(struct dae_sqe *sqe, struct wd_agg_msg *ms

static int hashagg_recv(struct wd_alg_driver *drv, handle_t ctx, void *hashagg_msg)
{
struct hisi_dae_ctx *priv = (struct hisi_dae_ctx *)drv->priv;
handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx);
struct hisi_qp *qp = (struct hisi_qp *)h_qp;
struct dae_extend_addr *ext_addr = qp->priv;
Expand All @@ -673,7 +674,7 @@ static int hashagg_recv(struct wd_alg_driver *drv, handle_t ctx, void *hashagg_m

msg->tag = sqe.low_tag;
if (qp->q_info.qp_mode == CTX_MODE_ASYNC) {
temp_msg = wd_agg_get_msg(qp->q_info.idx, msg->tag);
temp_msg = wd_find_msg_in_pool(priv->config.pool, qp->q_info.idx, msg->tag);
if (!temp_msg) {
msg->result = WD_AGG_IN_EPARA;
WD_ERR("failed to get send msg! idx = %u, tag = %u.\n",
Expand Down
38 changes: 20 additions & 18 deletions drv/hisi_hpre.c
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ static void hpre_result_check(struct hisi_hpre_sqe *hw_msg,

static int rsa_recv(struct wd_alg_driver *drv, handle_t ctx, void *rsa_msg)
{
struct hisi_hpre_ctx *priv = (struct hisi_hpre_ctx *)drv->priv;
handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx);
struct hisi_qp *qp = (struct hisi_qp *)h_qp;
struct hisi_hpre_sqe hw_msg = {0};
Expand All @@ -677,7 +678,7 @@ static int rsa_recv(struct wd_alg_driver *drv, handle_t ctx, void *rsa_msg)

msg->tag = LW_U16(hw_msg.low_tag);
if (qp->q_info.qp_mode == CTX_MODE_ASYNC) {
temp_msg = wd_rsa_get_msg(qp->q_info.idx, msg->tag);
temp_msg = wd_find_msg_in_pool(priv->config.pool, qp->q_info.idx, msg->tag);
if (!temp_msg) {
WD_ERR("failed to get send msg! idx = %u, tag = %u.\n",
qp->q_info.idx, msg->tag);
Expand Down Expand Up @@ -802,6 +803,7 @@ static int dh_send(struct wd_alg_driver *drv, handle_t ctx, void *dh_msg)

static int dh_recv(struct wd_alg_driver *drv, handle_t ctx, void *dh_msg)
{
struct hisi_hpre_ctx *priv = (struct hisi_hpre_ctx *)drv->priv;
handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx);
struct hisi_qp *qp = (struct hisi_qp *)h_qp;
struct wd_dh_msg *msg = dh_msg;
Expand All @@ -820,7 +822,7 @@ static int dh_recv(struct wd_alg_driver *drv, handle_t ctx, void *dh_msg)

msg->tag = LW_U16(hw_msg.low_tag);
if (qp->q_info.qp_mode == CTX_MODE_ASYNC) {
temp_msg = wd_dh_get_msg(qp->q_info.idx, msg->tag);
temp_msg = wd_find_msg_in_pool(priv->config.pool, qp->q_info.idx, msg->tag);
if (!temp_msg) {
WD_ERR("failed to get send msg! idx = %u, tag = %u.\n",
qp->q_info.idx, msg->tag);
Expand Down Expand Up @@ -2037,7 +2039,6 @@ static int ecc_out_transfer(struct wd_ecc_msg *msg,
return ret;
}


static __u32 get_hash_bytes(__u8 type)
{
__u32 val = 0;
Expand Down Expand Up @@ -2303,15 +2304,16 @@ static int sm2_convert_dec_out(struct wd_ecc_msg *src,
return ret;
}

static int ecc_sqe_parse(struct hisi_qp *qp, struct wd_ecc_msg *msg,
struct hisi_hpre_sqe *hw_msg)
static int ecc_sqe_parse(struct wd_alg_driver *drv, struct hisi_qp *qp,
struct wd_ecc_msg *msg, struct hisi_hpre_sqe *hw_msg)
{
struct hisi_hpre_ctx *priv = (struct hisi_hpre_ctx *)drv->priv;
struct wd_ecc_msg *temp_msg;
int ret;

msg->tag = LW_U16(hw_msg->low_tag);
if (qp->q_info.qp_mode == CTX_MODE_ASYNC) {
temp_msg = wd_ecc_get_msg(qp->q_info.idx, msg->tag);
temp_msg = wd_find_msg_in_pool(priv->config.pool, qp->q_info.idx, msg->tag);
if (!temp_msg) {
WD_ERR("failed to get send msg! idx = %u, tag = %u.\n",
qp->q_info.idx, msg->tag);
Expand Down Expand Up @@ -2342,7 +2344,7 @@ static int ecc_sqe_parse(struct hisi_qp *qp, struct wd_ecc_msg *msg,
return ret;
}

static int parse_second_sqe(handle_t h_qp,
static int parse_second_sqe(struct wd_alg_driver *drv, handle_t h_qp,
struct wd_ecc_msg *msg,
struct wd_ecc_msg **second)
{
Expand Down Expand Up @@ -2371,15 +2373,15 @@ static int parse_second_sqe(handle_t h_qp,
hsz = (hw_msg.task_len1 + 1) * BYTE_BITS;
dst = *(struct wd_ecc_msg **)((uintptr_t)data +
hsz * ECDH_OUT_PARAM_NUM);
ret = ecc_sqe_parse((struct hisi_qp *)h_qp, dst, &hw_msg);
ret = ecc_sqe_parse(drv, (struct hisi_qp *)h_qp, dst, &hw_msg);
msg->result = dst->result;
*second = dst;

return ret;
}

static int sm2_enc_parse(handle_t h_qp, struct wd_ecc_msg *msg,
struct hisi_hpre_sqe *hw_msg)
static int sm2_enc_parse(struct wd_alg_driver *drv, handle_t h_qp,
struct wd_ecc_msg *msg, struct hisi_hpre_sqe *hw_msg)
{
__u16 tag = LW_U16(hw_msg->low_tag);
struct wd_ecc_msg *second = NULL;
Expand All @@ -2397,14 +2399,14 @@ static int sm2_enc_parse(handle_t h_qp, struct wd_ecc_msg *msg,
memcpy(&src, first + 1, sizeof(src));

/* parse first sqe */
ret = ecc_sqe_parse((struct hisi_qp *)h_qp, first, hw_msg);
ret = ecc_sqe_parse(drv, (struct hisi_qp *)h_qp, first, hw_msg);
if (ret) {
WD_ERR("failed to parse first BD, ret = %d!\n", ret);
goto free_first;
}

/* parse second sqe */
ret = parse_second_sqe(h_qp, msg, &second);
ret = parse_second_sqe(drv, h_qp, msg, &second);
if (unlikely(ret)) {
WD_ERR("failed to parse second BD, ret = %d!\n", ret);
goto free_first;
Expand All @@ -2424,8 +2426,8 @@ static int sm2_enc_parse(handle_t h_qp, struct wd_ecc_msg *msg,
return ret;
}

static int sm2_dec_parse(handle_t ctx, struct wd_ecc_msg *msg,
struct hisi_hpre_sqe *hw_msg)
static int sm2_dec_parse(struct wd_alg_driver *drv, handle_t ctx,
struct wd_ecc_msg *msg, struct hisi_hpre_sqe *hw_msg)
{
__u16 tag = LW_U16(hw_msg->low_tag);
struct wd_ecc_msg *dst;
Expand All @@ -2441,7 +2443,7 @@ static int sm2_dec_parse(handle_t ctx, struct wd_ecc_msg *msg,
memcpy(&src, dst + 1, sizeof(src));

/* parse first sqe */
ret = ecc_sqe_parse((struct hisi_qp *)ctx, dst, hw_msg);
ret = ecc_sqe_parse(drv, (struct hisi_qp *)ctx, dst, hw_msg);
if (ret) {
WD_ERR("failed to parse decode BD, ret = %d!\n", ret);
goto fail;
Expand Down Expand Up @@ -2480,12 +2482,12 @@ static int ecc_recv(struct wd_alg_driver *drv, handle_t ctx, void *ecc_msg)

if (hw_msg.alg == HPRE_ALG_ECDH_MULTIPLY &&
hw_msg.sm2_mlen == HPRE_SM2_ENC)
return sm2_enc_parse(h_qp, msg, &hw_msg);
return sm2_enc_parse(drv, h_qp, msg, &hw_msg);
else if (hw_msg.alg == HPRE_ALG_ECDH_MULTIPLY &&
hw_msg.sm2_mlen == HPRE_SM2_DEC)
return sm2_dec_parse(h_qp, msg, &hw_msg);
return sm2_dec_parse(drv, h_qp, msg, &hw_msg);

return ecc_sqe_parse((struct hisi_qp *)h_qp, msg, &hw_msg);
return ecc_sqe_parse(drv, (struct hisi_qp *)h_qp, msg, &hw_msg);
}

static int hpre_get_usage(void *param)
Expand Down
Loading