Skip to content

Commit

Permalink
driver alloc and free resources by itself
Browse files Browse the repository at this point in the history
A driver should alloc and free resources by himself instead of the
caller, considering multi-driver exists, the caller can not hold ctx
for all drivers, so drv.init malloc private ctx and drv.exit free.

Add driver as para of alg ops, not only for getting private ctx, but
easier for multi-driver support.

Signed-off-by: Zhangfei Gao <zhangfei.gao@linaro.org>
  • Loading branch information
zhangfeigao committed Jun 12, 2023
1 parent d3f6429 commit 3c7307b
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 173 deletions.
35 changes: 26 additions & 9 deletions drv/hisi_comp.c
Original file line number Diff line number Diff line change
Expand Up @@ -771,21 +771,30 @@ 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 hisi_zip_ctx *priv = (struct hisi_zip_ctx *)drv->priv;
struct wd_ctx_config_internal *config = conf;
struct hisi_zip_ctx *zip_ctx = (struct hisi_zip_ctx *)priv;
struct hisi_qm_priv qm_priv;
handle_t h_qp = 0;
handle_t h_ctx;
__u32 i, j;

if (priv) {
/* return if already inited */
return 0;
}

if (!config->ctx_num) {
WD_ERR("invalid: zip init config ctx num is 0!\n");
return -WD_EINVAL;
}

memcpy(&zip_ctx->config, config, sizeof(struct wd_ctx_config_internal));
priv = malloc(sizeof(struct hisi_zip_ctx));
if (!priv)
return -WD_EINVAL;

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;
Expand All @@ -803,27 +812,36 @@ static int hisi_zip_init(void *conf, void *priv)
}

hisi_zip_sqe_ops_adapt(h_qp);
drv->priv = priv;

return 0;
out:
for (j = 0; j < i; j++) {
h_qp = (handle_t)wd_ctx_get_priv(config->ctxs[j].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;
__u32 i;

if (!priv) {
/* return if already exit */
return;
}

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);
drv->priv = NULL;
}

static int fill_zip_comp_sqe(struct hisi_qp *qp, struct wd_comp_msg *msg,
Expand Down Expand Up @@ -878,7 +896,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;
Expand Down Expand Up @@ -1041,7 +1059,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;
Expand All @@ -1063,7 +1081,6 @@ static int hisi_zip_comp_recv(handle_t ctx, void *comp_msg)
.alg_name = (zip_alg_name),\
.calc_type = UADK_ALG_HW,\
.priority = 100,\
.priv_size = sizeof(struct hisi_zip_ctx),\
.queue_num = ZIP_CTX_Q_NUM_DEF,\
.op_type_num = 2,\
.fallback = 0,\
Expand Down
67 changes: 47 additions & 20 deletions drv/hisi_hpre.c
Original file line number Diff line number Diff line change
Expand Up @@ -497,60 +497,90 @@ static int hpre_init_qm_priv(struct wd_ctx_config_internal *config,
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_hpre_ctx *priv = (struct hisi_hpre_ctx *)drv->priv;
struct hisi_qm_priv qm_priv;
int ret;

if (priv) {
/* return if already inited */
return 0;
}

if (!config->ctx_num) {
WD_ERR("invalid: hpre rsa/dh init config ctx num is 0!\n");
return -WD_EINVAL;
}

drv->priv = malloc(sizeof(struct hisi_hpre_ctx));
if (!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_hpre_ctx *priv = (struct hisi_hpre_ctx *)drv->priv;
struct hisi_qm_priv qm_priv;
int ret;

if (priv) {
/* return if already inited */
return 0;
}

if (!config->ctx_num) {
WD_ERR("invalid: hpre ecc init config ctx num is 0!\n");
return -WD_EINVAL;
}

drv->priv = malloc(sizeof(struct hisi_hpre_ctx));
if (!drv->priv)
return -WD_EINVAL;

qm_priv.op_type = HPRE_HW_V3_ECC_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;
__u32 i;

if (!priv) {
/* return if already exit */
return;
}

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);
drv->priv = NULL;
}

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;
Expand Down Expand Up @@ -606,7 +636,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;
Expand Down Expand Up @@ -704,7 +734,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;
Expand Down Expand Up @@ -749,7 +779,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;
Expand Down Expand Up @@ -1840,7 +1870,7 @@ static int sm2_dec_send(handle_t ctx, struct wd_ecc_msg *msg)
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;
Expand Down Expand Up @@ -2412,7 +2442,7 @@ static int sm2_dec_parse(handle_t ctx, struct wd_ecc_msg *msg,
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;
Expand Down Expand Up @@ -2449,7 +2479,6 @@ static int hpre_get_usage(void *param)
.alg_name = (hpre_alg_name),\
.calc_type = UADK_ALG_HW,\
.priority = 100,\
.priv_size = sizeof(struct hisi_hpre_ctx),\
.queue_num = HPRE_CTX_Q_NUM_DEF,\
.op_type_num = 1,\
.fallback = 0,\
Expand All @@ -2473,7 +2502,6 @@ static struct wd_alg_driver hpre_rsa_driver = {
.alg_name = "rsa",
.calc_type = UADK_ALG_HW,
.priority = 100,
.priv_size = sizeof(struct hisi_hpre_ctx),
.queue_num = HPRE_CTX_Q_NUM_DEF,
.op_type_num = 1,
.fallback = 0,
Expand All @@ -2489,7 +2517,6 @@ static struct wd_alg_driver hpre_dh_driver = {
.alg_name = "dh",
.calc_type = UADK_ALG_HW,
.priority = 100,
.priv_size = sizeof(struct hisi_hpre_ctx),
.queue_num = HPRE_CTX_Q_NUM_DEF,
.op_type_num = 1,
.fallback = 0,
Expand Down
Loading

0 comments on commit 3c7307b

Please sign in to comment.