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

Sched 12.25 #604

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
11 changes: 11 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ lib_LTLIBRARIES=libwd.la libwd_comp.la libwd_crypto.la
uadk_driversdir=$(libdir)/uadk
uadk_drivers_LTLIBRARIES=libhisi_sec.la libhisi_hpre.la libhisi_zip.la

if HAVE_ZLIB
uadk_drivers_LTLIBRARIES+=libhisi_zlib.la
endif

libwd_la_SOURCES=wd.c wd_mempool.c wd.h wd_alg.c wd_alg.h \
v1/wd.c v1/wd.h v1/wd_adapter.c v1/wd_adapter.h \
v1/wd_rng.c v1/wd_rng.h \
Expand All @@ -69,6 +73,8 @@ libwd_comp_la_SOURCES=wd_comp.c wd_comp.h wd_comp_drv.h wd_util.c wd_util.h \
libhisi_zip_la_SOURCES=drv/hisi_comp.c hisi_comp.h drv/hisi_qm_udrv.c \
hisi_qm_udrv.h wd_comp_drv.h

libhisi_zlib_la_SOURCES=drv/hisi_zlib.c

libwd_crypto_la_SOURCES=wd_cipher.c wd_cipher.h wd_cipher_drv.h \
wd_aead.c wd_aead.h wd_aead_drv.h \
wd_rsa.c wd_rsa.h wd_rsa_drv.h \
Expand Down Expand Up @@ -96,6 +102,8 @@ libwd_comp_la_DEPENDENCIES = libwd.la

libhisi_zip_la_LIBADD = -ldl

libhisi_zlib_la_LIBADD = -ldl -lz

libwd_crypto_la_LIBADD = $(libwd_la_OBJECTS) -ldl -lnuma
libwd_crypto_la_DEPENDENCIES = libwd.la

Expand Down Expand Up @@ -125,6 +133,9 @@ libhisi_zip_la_LIBADD= -lwd -ldl -lwd_comp
libhisi_zip_la_LDFLAGS=$(UADK_VERSION)
libhisi_zip_la_DEPENDENCIES= libwd.la libwd_comp.la

libhisi_zlib_la_LIBADD= -ldl -lz
libhisi_zlib_la_LDFLAGS=$(UADK_VERSION)

libhisi_sec_la_LIBADD= -lwd -lwd_crypto
libhisi_sec_la_LDFLAGS=$(UADK_VERSION)
libhisi_sec_la_DEPENDENCIES= libwd.la libwd_crypto.la
Expand Down
6 changes: 4 additions & 2 deletions drv/hisi_comp.c
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,8 @@ static void free_hw_sgl(handle_t h_qp, struct hisi_zip_sqe *sqe,

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 op_ctx *op = (struct op_ctx *)ctx;
struct hisi_qp *qp = wd_ctx_get_priv(op->ctx);
struct wd_comp_msg *msg = comp_msg;
handle_t h_qp = (handle_t)qp;
struct hisi_zip_sqe sqe = {0};
Expand Down Expand Up @@ -1077,7 +1078,8 @@ static int parse_zip_sqe(struct hisi_qp *qp, struct hisi_zip_sqe *sqe,

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 op_ctx *op = (struct op_ctx *)ctx;
struct hisi_qp *qp = wd_ctx_get_priv(op->ctx);
struct wd_comp_msg *recv_msg = comp_msg;
handle_t h_qp = (handle_t)qp;
struct hisi_zip_sqe sqe = {0};
Expand Down
36 changes: 24 additions & 12 deletions drv/hisi_sec.c
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,8 @@ static int hisi_sec_aead_recv_v3(struct wd_alg_driver *drv, handle_t ctx, void *

static int cipher_send(struct wd_alg_driver *drv, handle_t ctx, void *msg)
{
handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx);
struct op_ctx *op = (struct op_ctx *)ctx;
handle_t h_qp = (handle_t)wd_ctx_get_priv(op->ctx);
struct hisi_qp *qp = (struct hisi_qp *)h_qp;
struct hisi_qm_queue_info q_info = qp->q_info;

Expand All @@ -560,7 +561,8 @@ static int cipher_send(struct wd_alg_driver *drv, handle_t ctx, void *msg)

static int cipher_recv(struct wd_alg_driver *drv, handle_t ctx, void *msg)
{
handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx);
struct op_ctx *op = (struct op_ctx *)ctx;
handle_t h_qp = (handle_t)wd_ctx_get_priv(op->ctx);
struct hisi_qp *qp = (struct hisi_qp *)h_qp;
struct hisi_qm_queue_info q_info = qp->q_info;

Expand All @@ -571,7 +573,8 @@ static int cipher_recv(struct wd_alg_driver *drv, handle_t ctx, void *msg)

static int digest_send(struct wd_alg_driver *drv, handle_t ctx, void *msg)
{
handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx);
struct op_ctx *op = (struct op_ctx *)ctx;
handle_t h_qp = (handle_t)wd_ctx_get_priv(op->ctx);
struct hisi_qp *qp = (struct hisi_qp *)h_qp;
struct hisi_qm_queue_info q_info = qp->q_info;

Expand All @@ -582,7 +585,8 @@ static int digest_send(struct wd_alg_driver *drv, handle_t ctx, void *msg)

static int digest_recv(struct wd_alg_driver *drv, handle_t ctx, void *msg)
{
handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx);
struct op_ctx *op = (struct op_ctx *)ctx;
handle_t h_qp = (handle_t)wd_ctx_get_priv(op->ctx);
struct hisi_qp *qp = (struct hisi_qp *)h_qp;
struct hisi_qm_queue_info q_info = qp->q_info;

Expand Down Expand Up @@ -1170,7 +1174,8 @@ static int fill_cipher_bd2(struct wd_cipher_msg *msg, struct hisi_sec_sqe *sqe)

static int hisi_sec_cipher_send(struct wd_alg_driver *drv, handle_t ctx, void *wd_msg)
{
handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx);
struct op_ctx *op = (struct op_ctx *)ctx;
handle_t h_qp = (handle_t)wd_ctx_get_priv(op->ctx);
struct wd_cipher_msg *msg = wd_msg;
struct hisi_sec_sqe sqe;
__u16 count = 0;
Expand Down Expand Up @@ -1215,7 +1220,8 @@ static int hisi_sec_cipher_send(struct wd_alg_driver *drv, handle_t ctx, void *w

static int hisi_sec_cipher_recv(struct wd_alg_driver *drv, handle_t ctx, void *wd_msg)
{
handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx);
struct op_ctx *op = (struct op_ctx *)ctx;
handle_t h_qp = (handle_t)wd_ctx_get_priv(op->ctx);
struct wd_cipher_msg *recv_msg = wd_msg;
struct hisi_sec_sqe sqe;
__u16 count = 0;
Expand Down Expand Up @@ -1373,7 +1379,8 @@ static int fill_cipher_bd3(struct wd_cipher_msg *msg, struct hisi_sec_sqe3 *sqe)

static int hisi_sec_cipher_send_v3(struct wd_alg_driver *drv, handle_t ctx, void *wd_msg)
{
handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx);
struct op_ctx *op = (struct op_ctx *)ctx;
handle_t h_qp = (handle_t)wd_ctx_get_priv(op->ctx);
struct wd_cipher_msg *msg = wd_msg;
struct hisi_sec_sqe3 sqe;
__u16 count = 0;
Expand Down Expand Up @@ -1463,7 +1470,8 @@ static void parse_cipher_bd3(struct hisi_qp *qp, struct hisi_sec_sqe3 *sqe,

static int hisi_sec_cipher_recv_v3(struct wd_alg_driver *drv, handle_t ctx, void *wd_msg)
{
handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx);
struct op_ctx *op = (struct op_ctx *)ctx;
handle_t h_qp = (handle_t)wd_ctx_get_priv(op->ctx);
struct wd_cipher_msg *recv_msg = wd_msg;
struct hisi_sec_sqe3 sqe;
__u16 count = 0;
Expand Down Expand Up @@ -1736,7 +1744,8 @@ static int digest_len_check(struct wd_digest_msg *msg, enum sec_bd_type type)

static int hisi_sec_digest_send(struct wd_alg_driver *drv, handle_t ctx, void *wd_msg)
{
handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx);
struct op_ctx *op = (struct op_ctx *)ctx;
handle_t h_qp = (handle_t)wd_ctx_get_priv(op->ctx);
struct wd_digest_msg *msg = wd_msg;
struct hisi_sec_sqe sqe;
__u16 count = 0;
Expand Down Expand Up @@ -1803,7 +1812,8 @@ static int hisi_sec_digest_send(struct wd_alg_driver *drv, handle_t ctx, void *w

static int hisi_sec_digest_recv(struct wd_alg_driver *drv, handle_t ctx, void *wd_msg)
{
handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx);
struct op_ctx *op = (struct op_ctx *)ctx;
handle_t h_qp = (handle_t)wd_ctx_get_priv(op->ctx);
struct wd_digest_msg *recv_msg = wd_msg;
struct hisi_sec_sqe sqe;
__u16 count = 0;
Expand Down Expand Up @@ -1980,7 +1990,8 @@ static void fill_digest_v3_scene(struct hisi_sec_sqe3 *sqe,

static int hisi_sec_digest_send_v3(struct wd_alg_driver *drv, handle_t ctx, void *wd_msg)
{
handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx);
struct op_ctx *op = (struct op_ctx *)ctx;
handle_t h_qp = (handle_t)wd_ctx_get_priv(op->ctx);
struct wd_digest_msg *msg = wd_msg;
struct hisi_sec_sqe3 sqe;
__u16 count = 0;
Expand Down Expand Up @@ -2079,7 +2090,8 @@ static void parse_digest_bd3(struct hisi_qp *qp, struct hisi_sec_sqe3 *sqe,

static int hisi_sec_digest_recv_v3(struct wd_alg_driver *drv, handle_t ctx, void *wd_msg)
{
handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx);
struct op_ctx *op = (struct op_ctx *)ctx;
handle_t h_qp = (handle_t)wd_ctx_get_priv(op->ctx);
struct wd_digest_msg *recv_msg = wd_msg;
struct hisi_sec_sqe3 sqe;
__u16 count = 0;
Expand Down
168 changes: 168 additions & 0 deletions drv/hisi_zlib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/* 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 <zlib.h>

#include "drv/wd_comp_drv.h"

struct hisi_zlib_priv {
int windowbits;
};

static int hisi_zlib_init(struct wd_alg_driver *drv, void *conf)
{
struct hisi_zlib_priv *priv;

priv = malloc(sizeof(struct hisi_zlib_priv));
if (!priv)
return -ENOMEM;

if (strcmp(drv->alg_name, "zlib") == 0)
priv->windowbits = 15;
else if (strcmp(drv->alg_name, "deflate") == 0)
priv->windowbits = -15;
else if (strcmp(drv->alg_name, "gzip") == 0)
priv->windowbits = 15 + 16;

drv->priv = priv;

return 0;
}
static void hisi_zlib_exit(struct wd_alg_driver *drv)
{
struct hisi_zlib_priv *priv = (struct hisi_zlib_priv *)drv->priv;

free(priv);
}

static int hisi_zlib_send(struct wd_alg_driver *drv, handle_t ctx, void *comp_msg)
{
struct hisi_zlib_priv *priv = (struct hisi_zlib_priv *)drv->priv;
struct wd_comp_msg *msg = comp_msg;
z_stream strm;
int ret;

memset(&strm, 0, sizeof(z_stream));

strm.next_in = msg->req.src;
strm.avail_in = msg->req.src_len;
strm.next_out = msg->req.dst;
strm.avail_out = msg->req.dst_len;

if (msg->req.op_type == WD_DIR_COMPRESS) {
/* deflate */

ret = deflateInit2(&strm, Z_BEST_SPEED, Z_DEFLATED, priv->windowbits,
8, Z_DEFAULT_STRATEGY);
if (ret != Z_OK) {
printf("deflateInit2: %d\n", ret);
return -EINVAL;
}

do {
ret = deflate(&strm, Z_FINISH);
if ((ret == Z_STREAM_ERROR) || (ret == Z_BUF_ERROR)) {
printf("defalte error %d - %s\n", ret, strm.msg);
ret = -ENOSR;
break;
} else if (!strm.avail_in) {
if (ret != Z_STREAM_END)
printf("deflate unexpected return: %d\n", ret);
ret = 0;
break;
} else if (!strm.avail_out) {
printf("deflate out of memory\n");
ret = -ENOSPC;
break;
}
} while (ret == Z_OK);

deflateEnd(&strm);

} else {
/* inflate */

/* Window size of 15, +32 for auto-decoding gzip/zlib */
ret = inflateInit2(&strm, 15 + 32);
if (ret != Z_OK) {
printf("zlib inflateInit: %d\n", ret);
return -EINVAL;
}

do {
ret = inflate(&strm, Z_NO_FLUSH);
if ((ret < 0) || (ret == Z_NEED_DICT)) {
printf("zlib error %d - %s\n", ret, strm.msg);
ret = -EINVAL;
break;
}
if (!strm.avail_out) {
if (!strm.avail_in || (ret == Z_STREAM_END)) {
ret = 0;
break;
}
printf("%s: avail_out is empty!\n", __func__);
ret = -EINVAL;
break;
}
} while (strm.avail_in && (ret != Z_STREAM_END));
inflateEnd(&strm);
}

msg->produced = msg->req.dst_len - strm.avail_out;
msg->in_cons = msg->req.src_len;

return ret;
}
static int hisi_zlib_recv(struct wd_alg_driver *drv, handle_t ctx, void *msg)
{
/*
* recv just return since cpu does not support async,
* once send func return, the operation is done
*/
return 0;
}

#define GEN_ZLIB_ALG_DRIVER(zlib_alg_name) \
{\
.drv_name = "hisi_zlib",\
.alg_name = zlib_alg_name,\
.calc_type = UADK_ALG_SOFT,\
.priority = 0,\
.init = hisi_zlib_init,\
.exit = hisi_zlib_exit,\
.send = hisi_zlib_send,\
.recv = hisi_zlib_recv,\
}

static struct wd_alg_driver zlib_alg_driver[] = {
GEN_ZLIB_ALG_DRIVER("zlib"),
GEN_ZLIB_ALG_DRIVER("gzip"),
GEN_ZLIB_ALG_DRIVER("deflate"),
};

static void __attribute__((constructor)) hisi_zlib_probe(void)
{
int alg_num = ARRAY_SIZE(zlib_alg_driver);
int i, ret;

for (i = 0; i < alg_num; i++) {
ret = wd_alg_driver_register(&zlib_alg_driver[i]);
if (ret)
fprintf(stderr, "Error: register zlib %s failed!\n",
zlib_alg_driver[i].alg_name);
}
}

static void __attribute__((destructor)) hisi_zlib_remove(void)
{
int alg_num = ARRAY_SIZE(zlib_alg_driver);
int i;

for (i = 0; i < alg_num; i++)
wd_alg_driver_unregister(&zlib_alg_driver[i]);
}
5 changes: 3 additions & 2 deletions include/wd_alg.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ struct wd_alg_list {
/**
* wd_request_drv() - Apply for an algorithm driver.
* @alg_name: task algorithm name.
* @hw_mask: the flag of shield hardware device drivers.
* @task_type: task_type required from user.
*
* Returns the applied algorithm driver, non means error.
*/
struct wd_alg_driver *wd_request_drv(const char *alg_name, bool hw_mask);
struct wd_alg_driver *wd_request_drv(const char *alg_name, int task_type);
void wd_release_drv(struct wd_alg_driver *drv);

/**
Expand All @@ -150,6 +150,7 @@ void wd_enable_drv(struct wd_alg_driver *drv);
void wd_disable_drv(struct wd_alg_driver *drv);

struct wd_alg_list *wd_get_alg_head(void);
struct wd_alg_driver *wd_find_drv(char *drv_name, char *alg_name, int idx);

#ifdef __cplusplus
}
Expand Down