2019-08-02 07:57:52 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/* Copyright (c) 2019 HiSilicon Limited. */
|
|
|
|
#include <crypto/internal/acompress.h>
|
|
|
|
#include <linux/bitfield.h>
|
2022-07-21 20:58:53 +00:00
|
|
|
#include <linux/bitmap.h>
|
2019-08-02 07:57:52 +00:00
|
|
|
#include <linux/dma-mapping.h>
|
|
|
|
#include <linux/scatterlist.h>
|
|
|
|
#include "zip.h"
|
|
|
|
|
2020-09-07 08:22:00 +00:00
|
|
|
/* hisi_zip_sqe dw3 */
|
|
|
|
#define HZIP_BD_STATUS_M GENMASK(7, 0)
|
|
|
|
/* hisi_zip_sqe dw7 */
|
|
|
|
#define HZIP_IN_SGE_DATA_OFFSET_M GENMASK(23, 0)
|
2021-03-27 07:28:47 +00:00
|
|
|
#define HZIP_SQE_TYPE_M GENMASK(31, 28)
|
2020-09-07 08:22:00 +00:00
|
|
|
/* hisi_zip_sqe dw8 */
|
|
|
|
#define HZIP_OUT_SGE_DATA_OFFSET_M GENMASK(23, 0)
|
|
|
|
/* hisi_zip_sqe dw9 */
|
|
|
|
#define HZIP_REQ_TYPE_M GENMASK(7, 0)
|
2023-09-14 09:09:07 +00:00
|
|
|
#define HZIP_ALG_TYPE_DEFLATE 0x01
|
2020-09-07 08:22:00 +00:00
|
|
|
#define HZIP_BUF_TYPE_M GENMASK(11, 8)
|
|
|
|
#define HZIP_SGL 0x1
|
|
|
|
|
2019-08-02 07:57:52 +00:00
|
|
|
#define HZIP_ALG_PRIORITY 300
|
2019-09-30 07:08:52 +00:00
|
|
|
#define HZIP_SGL_SGE_NR 10
|
2019-08-02 07:57:52 +00:00
|
|
|
|
2023-09-14 09:09:07 +00:00
|
|
|
#define HZIP_ALG_DEFLATE GENMASK(5, 4)
|
2022-09-09 09:47:02 +00:00
|
|
|
|
2023-09-28 09:21:47 +00:00
|
|
|
static DEFINE_MUTEX(zip_algs_lock);
|
|
|
|
static unsigned int zip_available_devs;
|
|
|
|
|
2019-08-02 07:57:52 +00:00
|
|
|
enum hisi_zip_alg_type {
|
|
|
|
HZIP_ALG_TYPE_COMP = 0,
|
|
|
|
HZIP_ALG_TYPE_DECOMP = 1,
|
|
|
|
};
|
|
|
|
|
2020-09-07 08:21:57 +00:00
|
|
|
enum {
|
|
|
|
HZIP_QPC_COMP,
|
|
|
|
HZIP_QPC_DECOMP,
|
|
|
|
HZIP_CTX_Q_NUM
|
|
|
|
};
|
|
|
|
|
2019-08-02 07:57:52 +00:00
|
|
|
#define COMP_NAME_TO_TYPE(alg_name) \
|
2023-09-14 09:09:08 +00:00
|
|
|
(!strcmp((alg_name), "deflate") ? HZIP_ALG_TYPE_DEFLATE : 0)
|
2019-08-02 07:57:52 +00:00
|
|
|
|
|
|
|
struct hisi_zip_req {
|
|
|
|
struct acomp_req *req;
|
|
|
|
struct hisi_acc_hw_sgl *hw_src;
|
|
|
|
struct hisi_acc_hw_sgl *hw_dst;
|
|
|
|
dma_addr_t dma_src;
|
|
|
|
dma_addr_t dma_dst;
|
2020-09-07 08:21:59 +00:00
|
|
|
u16 req_id;
|
2019-08-02 07:57:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct hisi_zip_req_q {
|
|
|
|
struct hisi_zip_req *q;
|
|
|
|
unsigned long *req_bitmap;
|
|
|
|
rwlock_t req_lock;
|
|
|
|
u16 size;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct hisi_zip_qp_ctx {
|
|
|
|
struct hisi_qp *qp;
|
|
|
|
struct hisi_zip_req_q req_q;
|
2019-09-30 07:08:52 +00:00
|
|
|
struct hisi_acc_sgl_pool *sgl_pool;
|
2019-08-02 07:57:52 +00:00
|
|
|
struct hisi_zip *zip_dev;
|
|
|
|
struct hisi_zip_ctx *ctx;
|
|
|
|
};
|
|
|
|
|
2021-03-27 07:28:47 +00:00
|
|
|
struct hisi_zip_sqe_ops {
|
|
|
|
u8 sqe_type;
|
|
|
|
void (*fill_addr)(struct hisi_zip_sqe *sqe, struct hisi_zip_req *req);
|
|
|
|
void (*fill_buf_size)(struct hisi_zip_sqe *sqe, struct hisi_zip_req *req);
|
|
|
|
void (*fill_buf_type)(struct hisi_zip_sqe *sqe, u8 buf_type);
|
|
|
|
void (*fill_req_type)(struct hisi_zip_sqe *sqe, u8 req_type);
|
|
|
|
void (*fill_tag)(struct hisi_zip_sqe *sqe, struct hisi_zip_req *req);
|
|
|
|
void (*fill_sqe_type)(struct hisi_zip_sqe *sqe, u8 sqe_type);
|
|
|
|
u32 (*get_tag)(struct hisi_zip_sqe *sqe);
|
|
|
|
u32 (*get_status)(struct hisi_zip_sqe *sqe);
|
|
|
|
u32 (*get_dstlen)(struct hisi_zip_sqe *sqe);
|
|
|
|
};
|
|
|
|
|
2019-08-02 07:57:52 +00:00
|
|
|
struct hisi_zip_ctx {
|
|
|
|
struct hisi_zip_qp_ctx qp_ctx[HZIP_CTX_Q_NUM];
|
2021-03-27 07:28:47 +00:00
|
|
|
const struct hisi_zip_sqe_ops *ops;
|
2019-08-02 07:57:52 +00:00
|
|
|
};
|
|
|
|
|
2019-09-30 07:08:53 +00:00
|
|
|
static int sgl_sge_nr_set(const char *val, const struct kernel_param *kp)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
u16 n;
|
|
|
|
|
|
|
|
if (!val)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
ret = kstrtou16(val, 10, &n);
|
|
|
|
if (ret || n == 0 || n > HISI_ACC_SGL_SGE_NR_MAX)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2022-07-28 02:07:58 +00:00
|
|
|
return param_set_ushort(val, kp);
|
2019-09-30 07:08:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct kernel_param_ops sgl_sge_nr_ops = {
|
|
|
|
.set = sgl_sge_nr_set,
|
2022-07-28 02:07:58 +00:00
|
|
|
.get = param_get_ushort,
|
2019-09-30 07:08:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static u16 sgl_sge_nr = HZIP_SGL_SGE_NR;
|
|
|
|
module_param_cb(sgl_sge_nr, &sgl_sge_nr_ops, &sgl_sge_nr, 0444);
|
|
|
|
MODULE_PARM_DESC(sgl_sge_nr, "Number of sge in sgl(1-255)");
|
|
|
|
|
2023-09-14 09:09:08 +00:00
|
|
|
static struct hisi_zip_req *hisi_zip_create_req(struct hisi_zip_qp_ctx *qp_ctx,
|
|
|
|
struct acomp_req *req)
|
2021-03-27 07:28:45 +00:00
|
|
|
{
|
|
|
|
struct hisi_zip_req_q *req_q = &qp_ctx->req_q;
|
|
|
|
struct hisi_zip_req *q = req_q->q;
|
|
|
|
struct hisi_zip_req *req_cache;
|
|
|
|
int req_id;
|
|
|
|
|
|
|
|
write_lock(&req_q->req_lock);
|
|
|
|
|
|
|
|
req_id = find_first_zero_bit(req_q->req_bitmap, req_q->size);
|
|
|
|
if (req_id >= req_q->size) {
|
|
|
|
write_unlock(&req_q->req_lock);
|
|
|
|
dev_dbg(&qp_ctx->qp->qm->pdev->dev, "req cache is full!\n");
|
|
|
|
return ERR_PTR(-EAGAIN);
|
|
|
|
}
|
|
|
|
set_bit(req_id, req_q->req_bitmap);
|
|
|
|
|
2022-08-13 09:57:52 +00:00
|
|
|
write_unlock(&req_q->req_lock);
|
|
|
|
|
2021-03-27 07:28:45 +00:00
|
|
|
req_cache = q + req_id;
|
|
|
|
req_cache->req_id = req_id;
|
|
|
|
req_cache->req = req;
|
|
|
|
|
|
|
|
return req_cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hisi_zip_remove_req(struct hisi_zip_qp_ctx *qp_ctx,
|
|
|
|
struct hisi_zip_req *req)
|
|
|
|
{
|
|
|
|
struct hisi_zip_req_q *req_q = &qp_ctx->req_q;
|
|
|
|
|
|
|
|
write_lock(&req_q->req_lock);
|
|
|
|
clear_bit(req->req_id, req_q->req_bitmap);
|
|
|
|
write_unlock(&req_q->req_lock);
|
|
|
|
}
|
|
|
|
|
2021-03-27 07:28:47 +00:00
|
|
|
static void hisi_zip_fill_addr(struct hisi_zip_sqe *sqe, struct hisi_zip_req *req)
|
|
|
|
{
|
|
|
|
sqe->source_addr_l = lower_32_bits(req->dma_src);
|
|
|
|
sqe->source_addr_h = upper_32_bits(req->dma_src);
|
|
|
|
sqe->dest_addr_l = lower_32_bits(req->dma_dst);
|
|
|
|
sqe->dest_addr_h = upper_32_bits(req->dma_dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hisi_zip_fill_buf_size(struct hisi_zip_sqe *sqe, struct hisi_zip_req *req)
|
|
|
|
{
|
|
|
|
struct acomp_req *a_req = req->req;
|
|
|
|
|
2023-09-14 09:09:08 +00:00
|
|
|
sqe->input_data_length = a_req->slen;
|
|
|
|
sqe->dest_avail_out = a_req->dlen;
|
2021-03-27 07:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void hisi_zip_fill_buf_type(struct hisi_zip_sqe *sqe, u8 buf_type)
|
2019-08-02 07:57:52 +00:00
|
|
|
{
|
|
|
|
u32 val;
|
|
|
|
|
2021-03-27 07:28:47 +00:00
|
|
|
val = sqe->dw9 & ~HZIP_BUF_TYPE_M;
|
2019-08-02 07:57:52 +00:00
|
|
|
val |= FIELD_PREP(HZIP_BUF_TYPE_M, buf_type);
|
|
|
|
sqe->dw9 = val;
|
|
|
|
}
|
|
|
|
|
2021-03-27 07:28:47 +00:00
|
|
|
static void hisi_zip_fill_req_type(struct hisi_zip_sqe *sqe, u8 req_type)
|
2019-08-02 07:57:52 +00:00
|
|
|
{
|
2021-03-27 07:28:47 +00:00
|
|
|
u32 val;
|
|
|
|
|
|
|
|
val = sqe->dw9 & ~HZIP_REQ_TYPE_M;
|
|
|
|
val |= FIELD_PREP(HZIP_REQ_TYPE_M, req_type);
|
|
|
|
sqe->dw9 = val;
|
2019-08-02 07:57:52 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 09:09:08 +00:00
|
|
|
static void hisi_zip_fill_tag(struct hisi_zip_sqe *sqe, struct hisi_zip_req *req)
|
2021-03-27 07:28:48 +00:00
|
|
|
{
|
|
|
|
sqe->dw26 = req->req_id;
|
|
|
|
}
|
|
|
|
|
2021-03-27 07:28:47 +00:00
|
|
|
static void hisi_zip_fill_sqe_type(struct hisi_zip_sqe *sqe, u8 sqe_type)
|
|
|
|
{
|
|
|
|
u32 val;
|
|
|
|
|
|
|
|
val = sqe->dw7 & ~HZIP_SQE_TYPE_M;
|
|
|
|
val |= FIELD_PREP(HZIP_SQE_TYPE_M, sqe_type);
|
|
|
|
sqe->dw7 = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hisi_zip_fill_sqe(struct hisi_zip_ctx *ctx, struct hisi_zip_sqe *sqe,
|
|
|
|
u8 req_type, struct hisi_zip_req *req)
|
|
|
|
{
|
|
|
|
const struct hisi_zip_sqe_ops *ops = ctx->ops;
|
|
|
|
|
2019-08-02 07:57:52 +00:00
|
|
|
memset(sqe, 0, sizeof(struct hisi_zip_sqe));
|
|
|
|
|
2021-03-27 07:28:47 +00:00
|
|
|
ops->fill_addr(sqe, req);
|
|
|
|
ops->fill_buf_size(sqe, req);
|
|
|
|
ops->fill_buf_type(sqe, HZIP_SGL);
|
|
|
|
ops->fill_req_type(sqe, req_type);
|
|
|
|
ops->fill_tag(sqe, req);
|
|
|
|
ops->fill_sqe_type(sqe, ops->sqe_type);
|
2019-08-02 07:57:52 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 09:09:08 +00:00
|
|
|
static int hisi_zip_do_work(struct hisi_zip_qp_ctx *qp_ctx,
|
|
|
|
struct hisi_zip_req *req)
|
2021-03-27 07:28:45 +00:00
|
|
|
{
|
|
|
|
struct hisi_acc_sgl_pool *pool = qp_ctx->sgl_pool;
|
|
|
|
struct hisi_zip_dfx *dfx = &qp_ctx->zip_dev->dfx;
|
|
|
|
struct acomp_req *a_req = req->req;
|
|
|
|
struct hisi_qp *qp = qp_ctx->qp;
|
|
|
|
struct device *dev = &qp->qm->pdev->dev;
|
|
|
|
struct hisi_zip_sqe zip_sqe;
|
|
|
|
int ret;
|
|
|
|
|
2022-08-13 09:57:52 +00:00
|
|
|
if (unlikely(!a_req->src || !a_req->slen || !a_req->dst || !a_req->dlen))
|
2021-03-27 07:28:45 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
req->hw_src = hisi_acc_sg_buf_map_to_hw_sgl(dev, a_req->src, pool,
|
2021-03-27 07:28:47 +00:00
|
|
|
req->req_id << 1, &req->dma_src);
|
2021-03-27 07:28:45 +00:00
|
|
|
if (IS_ERR(req->hw_src)) {
|
|
|
|
dev_err(dev, "failed to map the src buffer to hw sgl (%ld)!\n",
|
|
|
|
PTR_ERR(req->hw_src));
|
|
|
|
return PTR_ERR(req->hw_src);
|
|
|
|
}
|
|
|
|
|
|
|
|
req->hw_dst = hisi_acc_sg_buf_map_to_hw_sgl(dev, a_req->dst, pool,
|
|
|
|
(req->req_id << 1) + 1,
|
2021-03-27 07:28:47 +00:00
|
|
|
&req->dma_dst);
|
2021-03-27 07:28:45 +00:00
|
|
|
if (IS_ERR(req->hw_dst)) {
|
|
|
|
ret = PTR_ERR(req->hw_dst);
|
|
|
|
dev_err(dev, "failed to map the dst buffer to hw slg (%d)!\n",
|
|
|
|
ret);
|
|
|
|
goto err_unmap_input;
|
|
|
|
}
|
|
|
|
|
2021-03-27 07:28:47 +00:00
|
|
|
hisi_zip_fill_sqe(qp_ctx->ctx, &zip_sqe, qp->req_type, req);
|
2021-03-27 07:28:45 +00:00
|
|
|
|
|
|
|
/* send command to start a task */
|
|
|
|
atomic64_inc(&dfx->send_cnt);
|
|
|
|
ret = hisi_qp_send(qp, &zip_sqe);
|
2022-08-13 09:57:52 +00:00
|
|
|
if (unlikely(ret < 0)) {
|
2021-03-27 07:28:45 +00:00
|
|
|
atomic64_inc(&dfx->send_busy_cnt);
|
|
|
|
ret = -EAGAIN;
|
|
|
|
dev_dbg_ratelimited(dev, "failed to send request!\n");
|
|
|
|
goto err_unmap_output;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINPROGRESS;
|
|
|
|
|
|
|
|
err_unmap_output:
|
|
|
|
hisi_acc_sg_buf_unmap(dev, a_req->dst, req->hw_dst);
|
|
|
|
err_unmap_input:
|
|
|
|
hisi_acc_sg_buf_unmap(dev, a_req->src, req->hw_src);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-09-14 09:09:08 +00:00
|
|
|
static u32 hisi_zip_get_tag(struct hisi_zip_sqe *sqe)
|
2021-03-27 07:28:48 +00:00
|
|
|
{
|
|
|
|
return sqe->dw26;
|
|
|
|
}
|
|
|
|
|
2021-03-27 07:28:47 +00:00
|
|
|
static u32 hisi_zip_get_status(struct hisi_zip_sqe *sqe)
|
|
|
|
{
|
|
|
|
return sqe->dw3 & HZIP_BD_STATUS_M;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 hisi_zip_get_dstlen(struct hisi_zip_sqe *sqe)
|
|
|
|
{
|
|
|
|
return sqe->produced;
|
|
|
|
}
|
|
|
|
|
2021-03-27 07:28:45 +00:00
|
|
|
static void hisi_zip_acomp_cb(struct hisi_qp *qp, void *data)
|
|
|
|
{
|
|
|
|
struct hisi_zip_qp_ctx *qp_ctx = qp->qp_ctx;
|
2021-03-27 07:28:47 +00:00
|
|
|
const struct hisi_zip_sqe_ops *ops = qp_ctx->ctx->ops;
|
2021-03-27 07:28:45 +00:00
|
|
|
struct hisi_zip_dfx *dfx = &qp_ctx->zip_dev->dfx;
|
|
|
|
struct hisi_zip_req_q *req_q = &qp_ctx->req_q;
|
|
|
|
struct device *dev = &qp->qm->pdev->dev;
|
|
|
|
struct hisi_zip_sqe *sqe = data;
|
2021-03-27 07:28:47 +00:00
|
|
|
u32 tag = ops->get_tag(sqe);
|
|
|
|
struct hisi_zip_req *req = req_q->q + tag;
|
2021-03-27 07:28:45 +00:00
|
|
|
struct acomp_req *acomp_req = req->req;
|
|
|
|
int err = 0;
|
2023-09-14 09:09:08 +00:00
|
|
|
u32 status;
|
2021-03-27 07:28:45 +00:00
|
|
|
|
|
|
|
atomic64_inc(&dfx->recv_cnt);
|
2021-03-27 07:28:47 +00:00
|
|
|
status = ops->get_status(sqe);
|
2022-08-13 09:57:52 +00:00
|
|
|
if (unlikely(status != 0 && status != HZIP_NC_ERR)) {
|
2021-03-27 07:28:45 +00:00
|
|
|
dev_err(dev, "%scompress fail in qp%u: %u, output: %u\n",
|
|
|
|
(qp->alg_type == 0) ? "" : "de", qp->qp_id, status,
|
|
|
|
sqe->produced);
|
|
|
|
atomic64_inc(&dfx->err_bd_cnt);
|
|
|
|
err = -EIO;
|
|
|
|
}
|
2021-03-27 07:28:47 +00:00
|
|
|
|
2021-03-27 07:28:45 +00:00
|
|
|
hisi_acc_sg_buf_unmap(dev, acomp_req->src, req->hw_src);
|
|
|
|
hisi_acc_sg_buf_unmap(dev, acomp_req->dst, req->hw_dst);
|
|
|
|
|
2023-09-14 09:09:08 +00:00
|
|
|
acomp_req->dlen = ops->get_dstlen(sqe);
|
2021-03-27 07:28:45 +00:00
|
|
|
|
|
|
|
if (acomp_req->base.complete)
|
|
|
|
acomp_request_complete(acomp_req, err);
|
|
|
|
|
|
|
|
hisi_zip_remove_req(qp_ctx, req);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hisi_zip_acompress(struct acomp_req *acomp_req)
|
|
|
|
{
|
|
|
|
struct hisi_zip_ctx *ctx = crypto_tfm_ctx(acomp_req->base.tfm);
|
|
|
|
struct hisi_zip_qp_ctx *qp_ctx = &ctx->qp_ctx[HZIP_QPC_COMP];
|
|
|
|
struct device *dev = &qp_ctx->qp->qm->pdev->dev;
|
|
|
|
struct hisi_zip_req *req;
|
|
|
|
int ret;
|
|
|
|
|
2023-09-14 09:09:08 +00:00
|
|
|
req = hisi_zip_create_req(qp_ctx, acomp_req);
|
2021-03-27 07:28:45 +00:00
|
|
|
if (IS_ERR(req))
|
|
|
|
return PTR_ERR(req);
|
|
|
|
|
2023-09-14 09:09:08 +00:00
|
|
|
ret = hisi_zip_do_work(qp_ctx, req);
|
2022-08-13 09:57:52 +00:00
|
|
|
if (unlikely(ret != -EINPROGRESS)) {
|
2021-03-27 07:28:45 +00:00
|
|
|
dev_info_ratelimited(dev, "failed to do compress (%d)!\n", ret);
|
|
|
|
hisi_zip_remove_req(qp_ctx, req);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hisi_zip_adecompress(struct acomp_req *acomp_req)
|
|
|
|
{
|
|
|
|
struct hisi_zip_ctx *ctx = crypto_tfm_ctx(acomp_req->base.tfm);
|
|
|
|
struct hisi_zip_qp_ctx *qp_ctx = &ctx->qp_ctx[HZIP_QPC_DECOMP];
|
|
|
|
struct device *dev = &qp_ctx->qp->qm->pdev->dev;
|
|
|
|
struct hisi_zip_req *req;
|
2023-09-14 09:09:08 +00:00
|
|
|
int ret;
|
2021-03-27 07:28:45 +00:00
|
|
|
|
2023-09-14 09:09:08 +00:00
|
|
|
req = hisi_zip_create_req(qp_ctx, acomp_req);
|
2021-03-27 07:28:45 +00:00
|
|
|
if (IS_ERR(req))
|
|
|
|
return PTR_ERR(req);
|
|
|
|
|
2023-09-14 09:09:08 +00:00
|
|
|
ret = hisi_zip_do_work(qp_ctx, req);
|
2022-08-13 09:57:52 +00:00
|
|
|
if (unlikely(ret != -EINPROGRESS)) {
|
2021-03-27 07:28:45 +00:00
|
|
|
dev_info_ratelimited(dev, "failed to do decompress (%d)!\n",
|
|
|
|
ret);
|
|
|
|
hisi_zip_remove_req(qp_ctx, req);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-08-13 10:19:39 +00:00
|
|
|
static int hisi_zip_start_qp(struct hisi_qp *qp, struct hisi_zip_qp_ctx *qp_ctx,
|
2020-03-10 08:42:50 +00:00
|
|
|
int alg_type, int req_type)
|
2019-08-02 07:57:52 +00:00
|
|
|
{
|
2020-03-10 08:42:50 +00:00
|
|
|
struct device *dev = &qp->qm->pdev->dev;
|
2019-08-02 07:57:52 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
qp->req_type = req_type;
|
2020-03-10 08:42:50 +00:00
|
|
|
qp->alg_type = alg_type;
|
2022-08-13 10:19:39 +00:00
|
|
|
qp->qp_ctx = qp_ctx;
|
2019-08-02 07:57:52 +00:00
|
|
|
|
|
|
|
ret = hisi_qm_start_qp(qp, 0);
|
2020-03-10 08:42:50 +00:00
|
|
|
if (ret < 0) {
|
2020-09-07 08:21:58 +00:00
|
|
|
dev_err(dev, "failed to start qp (%d)!\n", ret);
|
2020-03-10 08:42:50 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2019-08-02 07:57:52 +00:00
|
|
|
|
2022-08-13 10:19:39 +00:00
|
|
|
qp_ctx->qp = qp;
|
2019-08-02 07:57:52 +00:00
|
|
|
|
2020-03-10 08:42:50 +00:00
|
|
|
return 0;
|
2019-08-02 07:57:52 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 10:19:39 +00:00
|
|
|
static void hisi_zip_release_qp(struct hisi_zip_qp_ctx *qp_ctx)
|
2019-08-02 07:57:52 +00:00
|
|
|
{
|
2022-08-13 10:19:39 +00:00
|
|
|
hisi_qm_stop_qp(qp_ctx->qp);
|
|
|
|
hisi_qm_free_qps(&qp_ctx->qp, 1);
|
2019-08-02 07:57:52 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 09:09:08 +00:00
|
|
|
static const struct hisi_zip_sqe_ops hisi_zip_ops = {
|
2021-03-27 07:28:48 +00:00
|
|
|
.sqe_type = 0x3,
|
|
|
|
.fill_addr = hisi_zip_fill_addr,
|
|
|
|
.fill_buf_size = hisi_zip_fill_buf_size,
|
|
|
|
.fill_buf_type = hisi_zip_fill_buf_type,
|
|
|
|
.fill_req_type = hisi_zip_fill_req_type,
|
2023-09-14 09:09:08 +00:00
|
|
|
.fill_tag = hisi_zip_fill_tag,
|
2021-03-27 07:28:48 +00:00
|
|
|
.fill_sqe_type = hisi_zip_fill_sqe_type,
|
2023-09-14 09:09:08 +00:00
|
|
|
.get_tag = hisi_zip_get_tag,
|
2021-03-27 07:28:48 +00:00
|
|
|
.get_status = hisi_zip_get_status,
|
|
|
|
.get_dstlen = hisi_zip_get_dstlen,
|
|
|
|
};
|
|
|
|
|
2020-07-05 09:18:59 +00:00
|
|
|
static int hisi_zip_ctx_init(struct hisi_zip_ctx *hisi_zip_ctx, u8 req_type, int node)
|
2019-08-02 07:57:52 +00:00
|
|
|
{
|
2020-03-10 08:42:50 +00:00
|
|
|
struct hisi_qp *qps[HZIP_CTX_Q_NUM] = { NULL };
|
2021-03-27 07:28:47 +00:00
|
|
|
struct hisi_zip_qp_ctx *qp_ctx;
|
2019-08-02 07:57:52 +00:00
|
|
|
struct hisi_zip *hisi_zip;
|
|
|
|
int ret, i, j;
|
|
|
|
|
2020-07-05 09:18:59 +00:00
|
|
|
ret = zip_create_qps(qps, HZIP_CTX_Q_NUM, node);
|
2020-03-10 08:42:50 +00:00
|
|
|
if (ret) {
|
2020-09-07 08:21:58 +00:00
|
|
|
pr_err("failed to create zip qps (%d)!\n", ret);
|
2019-08-02 07:57:52 +00:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
2020-03-10 08:42:50 +00:00
|
|
|
|
|
|
|
hisi_zip = container_of(qps[0]->qm, struct hisi_zip, qm);
|
2019-08-02 07:57:52 +00:00
|
|
|
|
|
|
|
for (i = 0; i < HZIP_CTX_Q_NUM; i++) {
|
|
|
|
/* alg_type = 0 for compress, 1 for decompress in hw sqe */
|
2021-03-27 07:28:47 +00:00
|
|
|
qp_ctx = &hisi_zip_ctx->qp_ctx[i];
|
|
|
|
qp_ctx->ctx = hisi_zip_ctx;
|
|
|
|
ret = hisi_zip_start_qp(qps[i], qp_ctx, i, req_type);
|
2020-03-10 08:42:50 +00:00
|
|
|
if (ret) {
|
|
|
|
for (j = i - 1; j >= 0; j--)
|
|
|
|
hisi_qm_stop_qp(hisi_zip_ctx->qp_ctx[j].qp);
|
|
|
|
|
|
|
|
hisi_qm_free_qps(qps, HZIP_CTX_Q_NUM);
|
|
|
|
return ret;
|
|
|
|
}
|
2019-08-02 07:57:52 +00:00
|
|
|
|
2021-03-27 07:28:47 +00:00
|
|
|
qp_ctx->zip_dev = hisi_zip;
|
2019-08-02 07:57:52 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 09:09:08 +00:00
|
|
|
hisi_zip_ctx->ops = &hisi_zip_ops;
|
2021-03-27 07:28:47 +00:00
|
|
|
|
2019-08-02 07:57:52 +00:00
|
|
|
return 0;
|
2021-03-27 07:28:45 +00:00
|
|
|
}
|
2019-08-02 07:57:52 +00:00
|
|
|
|
2021-03-27 07:28:45 +00:00
|
|
|
static void hisi_zip_ctx_exit(struct hisi_zip_ctx *hisi_zip_ctx)
|
|
|
|
{
|
|
|
|
int i;
|
2019-08-02 07:57:52 +00:00
|
|
|
|
2022-08-13 10:19:39 +00:00
|
|
|
for (i = 0; i < HZIP_CTX_Q_NUM; i++)
|
2021-03-27 07:28:45 +00:00
|
|
|
hisi_zip_release_qp(&hisi_zip_ctx->qp_ctx[i]);
|
2019-08-02 07:57:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int hisi_zip_create_req_q(struct hisi_zip_ctx *ctx)
|
|
|
|
{
|
2022-09-09 09:46:56 +00:00
|
|
|
u16 q_depth = ctx->qp_ctx[0].qp->sq_depth;
|
2019-08-02 07:57:52 +00:00
|
|
|
struct hisi_zip_req_q *req_q;
|
|
|
|
int i, ret;
|
|
|
|
|
|
|
|
for (i = 0; i < HZIP_CTX_Q_NUM; i++) {
|
|
|
|
req_q = &ctx->qp_ctx[i].req_q;
|
2022-09-09 09:46:56 +00:00
|
|
|
req_q->size = q_depth;
|
2019-08-02 07:57:52 +00:00
|
|
|
|
2022-07-21 20:58:53 +00:00
|
|
|
req_q->req_bitmap = bitmap_zalloc(req_q->size, GFP_KERNEL);
|
2019-08-02 07:57:52 +00:00
|
|
|
if (!req_q->req_bitmap) {
|
|
|
|
ret = -ENOMEM;
|
2019-08-14 09:28:39 +00:00
|
|
|
if (i == 0)
|
|
|
|
return ret;
|
|
|
|
|
2022-08-13 10:19:39 +00:00
|
|
|
goto err_free_comp_q;
|
2019-08-02 07:57:52 +00:00
|
|
|
}
|
|
|
|
rwlock_init(&req_q->req_lock);
|
|
|
|
|
|
|
|
req_q->q = kcalloc(req_q->size, sizeof(struct hisi_zip_req),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!req_q->q) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
if (i == 0)
|
2022-08-13 10:19:39 +00:00
|
|
|
goto err_free_comp_bitmap;
|
2019-08-02 07:57:52 +00:00
|
|
|
else
|
2022-08-13 10:19:39 +00:00
|
|
|
goto err_free_decomp_bitmap;
|
2019-08-02 07:57:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2022-08-13 10:19:39 +00:00
|
|
|
err_free_decomp_bitmap:
|
2022-07-21 20:58:53 +00:00
|
|
|
bitmap_free(ctx->qp_ctx[HZIP_QPC_DECOMP].req_q.req_bitmap);
|
2022-08-13 10:19:39 +00:00
|
|
|
err_free_comp_q:
|
2020-09-07 08:21:57 +00:00
|
|
|
kfree(ctx->qp_ctx[HZIP_QPC_COMP].req_q.q);
|
2022-08-13 10:19:39 +00:00
|
|
|
err_free_comp_bitmap:
|
2022-07-21 20:58:53 +00:00
|
|
|
bitmap_free(ctx->qp_ctx[HZIP_QPC_COMP].req_q.req_bitmap);
|
2019-08-02 07:57:52 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hisi_zip_release_req_q(struct hisi_zip_ctx *ctx)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < HZIP_CTX_Q_NUM; i++) {
|
|
|
|
kfree(ctx->qp_ctx[i].req_q.q);
|
2022-07-21 20:58:53 +00:00
|
|
|
bitmap_free(ctx->qp_ctx[i].req_q.req_bitmap);
|
2019-08-02 07:57:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hisi_zip_create_sgl_pool(struct hisi_zip_ctx *ctx)
|
|
|
|
{
|
2022-09-09 09:46:56 +00:00
|
|
|
u16 q_depth = ctx->qp_ctx[0].qp->sq_depth;
|
2019-08-02 07:57:52 +00:00
|
|
|
struct hisi_zip_qp_ctx *tmp;
|
2019-09-30 07:08:52 +00:00
|
|
|
struct device *dev;
|
|
|
|
int i;
|
2019-08-02 07:57:52 +00:00
|
|
|
|
|
|
|
for (i = 0; i < HZIP_CTX_Q_NUM; i++) {
|
|
|
|
tmp = &ctx->qp_ctx[i];
|
2019-09-30 07:08:52 +00:00
|
|
|
dev = &tmp->qp->qm->pdev->dev;
|
2022-09-09 09:46:56 +00:00
|
|
|
tmp->sgl_pool = hisi_acc_create_sgl_pool(dev, q_depth << 1,
|
2019-09-30 07:08:53 +00:00
|
|
|
sgl_sge_nr);
|
2019-09-30 07:08:52 +00:00
|
|
|
if (IS_ERR(tmp->sgl_pool)) {
|
2019-08-02 07:57:52 +00:00
|
|
|
if (i == 1)
|
|
|
|
goto err_free_sgl_pool0;
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_free_sgl_pool0:
|
2020-09-07 08:21:57 +00:00
|
|
|
hisi_acc_free_sgl_pool(&ctx->qp_ctx[HZIP_QPC_COMP].qp->qm->pdev->dev,
|
|
|
|
ctx->qp_ctx[HZIP_QPC_COMP].sgl_pool);
|
2019-08-02 07:57:52 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hisi_zip_release_sgl_pool(struct hisi_zip_ctx *ctx)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < HZIP_CTX_Q_NUM; i++)
|
|
|
|
hisi_acc_free_sgl_pool(&ctx->qp_ctx[i].qp->qm->pdev->dev,
|
2019-09-30 07:08:52 +00:00
|
|
|
ctx->qp_ctx[i].sgl_pool);
|
2019-08-02 07:57:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void hisi_zip_set_acomp_cb(struct hisi_zip_ctx *ctx,
|
|
|
|
void (*fn)(struct hisi_qp *, void *))
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < HZIP_CTX_Q_NUM; i++)
|
|
|
|
ctx->qp_ctx[i].qp->req_cb = fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hisi_zip_acomp_init(struct crypto_acomp *tfm)
|
|
|
|
{
|
|
|
|
const char *alg_name = crypto_tfm_alg_name(&tfm->base);
|
|
|
|
struct hisi_zip_ctx *ctx = crypto_tfm_ctx(&tfm->base);
|
2020-09-07 08:21:58 +00:00
|
|
|
struct device *dev;
|
2019-08-02 07:57:52 +00:00
|
|
|
int ret;
|
|
|
|
|
2020-07-05 09:18:59 +00:00
|
|
|
ret = hisi_zip_ctx_init(ctx, COMP_NAME_TO_TYPE(alg_name), tfm->base.node);
|
2020-09-07 08:21:58 +00:00
|
|
|
if (ret) {
|
|
|
|
pr_err("failed to init ctx (%d)!\n", ret);
|
2019-08-02 07:57:52 +00:00
|
|
|
return ret;
|
2020-09-07 08:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dev = &ctx->qp_ctx[0].qp->qm->pdev->dev;
|
2019-08-02 07:57:52 +00:00
|
|
|
|
|
|
|
ret = hisi_zip_create_req_q(ctx);
|
2020-09-07 08:21:58 +00:00
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "failed to create request queue (%d)!\n", ret);
|
2019-08-02 07:57:52 +00:00
|
|
|
goto err_ctx_exit;
|
2020-09-07 08:21:58 +00:00
|
|
|
}
|
2019-08-02 07:57:52 +00:00
|
|
|
|
|
|
|
ret = hisi_zip_create_sgl_pool(ctx);
|
2020-09-07 08:21:58 +00:00
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "failed to create sgl pool (%d)!\n", ret);
|
2019-08-02 07:57:52 +00:00
|
|
|
goto err_release_req_q;
|
2020-09-07 08:21:58 +00:00
|
|
|
}
|
2019-08-02 07:57:52 +00:00
|
|
|
|
|
|
|
hisi_zip_set_acomp_cb(ctx, hisi_zip_acomp_cb);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_release_req_q:
|
|
|
|
hisi_zip_release_req_q(ctx);
|
|
|
|
err_ctx_exit:
|
|
|
|
hisi_zip_ctx_exit(ctx);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hisi_zip_acomp_exit(struct crypto_acomp *tfm)
|
|
|
|
{
|
|
|
|
struct hisi_zip_ctx *ctx = crypto_tfm_ctx(&tfm->base);
|
|
|
|
|
|
|
|
hisi_zip_set_acomp_cb(ctx, NULL);
|
|
|
|
hisi_zip_release_sgl_pool(ctx);
|
|
|
|
hisi_zip_release_req_q(ctx);
|
|
|
|
hisi_zip_ctx_exit(ctx);
|
|
|
|
}
|
|
|
|
|
2023-09-14 09:09:07 +00:00
|
|
|
static struct acomp_alg hisi_zip_acomp_deflate = {
|
|
|
|
.init = hisi_zip_acomp_init,
|
|
|
|
.exit = hisi_zip_acomp_exit,
|
|
|
|
.compress = hisi_zip_acompress,
|
|
|
|
.decompress = hisi_zip_adecompress,
|
|
|
|
.base = {
|
|
|
|
.cra_name = "deflate",
|
|
|
|
.cra_driver_name = "hisi-deflate-acomp",
|
|
|
|
.cra_module = THIS_MODULE,
|
|
|
|
.cra_priority = HZIP_ALG_PRIORITY,
|
|
|
|
.cra_ctxsize = sizeof(struct hisi_zip_ctx),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static int hisi_zip_register_deflate(struct hisi_qm *qm)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!hisi_zip_alg_support(qm, HZIP_ALG_DEFLATE))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = crypto_register_acomp(&hisi_zip_acomp_deflate);
|
|
|
|
if (ret)
|
|
|
|
dev_err(&qm->pdev->dev, "failed to register to deflate (%d)!\n", ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hisi_zip_unregister_deflate(struct hisi_qm *qm)
|
|
|
|
{
|
|
|
|
if (!hisi_zip_alg_support(qm, HZIP_ALG_DEFLATE))
|
|
|
|
return;
|
|
|
|
|
|
|
|
crypto_unregister_acomp(&hisi_zip_acomp_deflate);
|
|
|
|
}
|
|
|
|
|
2022-09-09 09:47:02 +00:00
|
|
|
int hisi_zip_register_to_crypto(struct hisi_qm *qm)
|
|
|
|
{
|
2023-09-28 09:21:47 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
mutex_lock(&zip_algs_lock);
|
|
|
|
if (zip_available_devs++)
|
|
|
|
goto unlock;
|
|
|
|
|
|
|
|
ret = hisi_zip_register_deflate(qm);
|
|
|
|
if (ret)
|
|
|
|
zip_available_devs--;
|
|
|
|
|
|
|
|
unlock:
|
|
|
|
mutex_unlock(&zip_algs_lock);
|
|
|
|
return ret;
|
2022-09-09 09:47:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void hisi_zip_unregister_from_crypto(struct hisi_qm *qm)
|
|
|
|
{
|
2023-09-28 09:21:47 +00:00
|
|
|
mutex_lock(&zip_algs_lock);
|
|
|
|
if (--zip_available_devs)
|
|
|
|
goto unlock;
|
|
|
|
|
2023-09-14 09:09:07 +00:00
|
|
|
hisi_zip_unregister_deflate(qm);
|
2023-09-28 09:21:47 +00:00
|
|
|
|
|
|
|
unlock:
|
|
|
|
mutex_unlock(&zip_algs_lock);
|
2019-08-02 07:57:52 +00:00
|
|
|
}
|