1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 HiSilicon Limited. */
3 #include <crypto/internal/acompress.h>
4 #include <linux/bitfield.h>
5 #include <linux/dma-mapping.h>
6 #include <linux/scatterlist.h>
9 #define HZIP_ZLIB_HEAD_SIZE 2
10 #define HZIP_GZIP_HEAD_SIZE 10
12 #define GZIP_HEAD_FHCRC_BIT BIT(1)
13 #define GZIP_HEAD_FEXTRA_BIT BIT(2)
14 #define GZIP_HEAD_FNAME_BIT BIT(3)
15 #define GZIP_HEAD_FCOMMENT_BIT BIT(4)
17 #define GZIP_HEAD_FLG_SHIFT 3
18 #define GZIP_HEAD_FEXTRA_SHIFT 10
19 #define GZIP_HEAD_FEXTRA_XLEN 2
20 #define GZIP_HEAD_FHCRC_SIZE 2
22 #define HZIP_CTX_Q_NUM 2
23 #define HZIP_GZIP_HEAD_BUF 256
24 #define HZIP_ALG_PRIORITY 300
26 static const u8 zlib_head[HZIP_ZLIB_HEAD_SIZE] = {0x78, 0x9c};
27 static const u8 gzip_head[HZIP_GZIP_HEAD_SIZE] = {0x1f, 0x8b, 0x08, 0x0, 0x0,
28 0x0, 0x0, 0x0, 0x0, 0x03};
29 enum hisi_zip_alg_type {
30 HZIP_ALG_TYPE_COMP = 0,
31 HZIP_ALG_TYPE_DECOMP = 1,
34 #define COMP_NAME_TO_TYPE(alg_name) \
35 (!strcmp((alg_name), "zlib-deflate") ? HZIP_ALG_TYPE_ZLIB : \
36 !strcmp((alg_name), "gzip") ? HZIP_ALG_TYPE_GZIP : 0) \
38 #define TO_HEAD_SIZE(req_type) \
39 (((req_type) == HZIP_ALG_TYPE_ZLIB) ? sizeof(zlib_head) : \
40 ((req_type) == HZIP_ALG_TYPE_GZIP) ? sizeof(gzip_head) : 0) \
42 #define TO_HEAD(req_type) \
43 (((req_type) == HZIP_ALG_TYPE_ZLIB) ? zlib_head : \
44 ((req_type) == HZIP_ALG_TYPE_GZIP) ? gzip_head : 0) \
47 struct acomp_req *req;
48 struct scatterlist *src;
49 struct scatterlist *dst;
52 struct hisi_acc_hw_sgl *hw_src;
53 struct hisi_acc_hw_sgl *hw_dst;
59 struct hisi_zip_req_q {
60 struct hisi_zip_req *q;
61 unsigned long *req_bitmap;
66 struct hisi_zip_qp_ctx {
68 struct hisi_zip_sqe zip_sqe;
69 struct hisi_zip_req_q req_q;
70 struct hisi_acc_sgl_pool sgl_pool;
71 struct hisi_zip *zip_dev;
72 struct hisi_zip_ctx *ctx;
78 struct hisi_zip_qp_ctx qp_ctx[HZIP_CTX_Q_NUM];
81 static void hisi_zip_config_buf_type(struct hisi_zip_sqe *sqe, u8 buf_type)
85 val = (sqe->dw9) & ~HZIP_BUF_TYPE_M;
86 val |= FIELD_PREP(HZIP_BUF_TYPE_M, buf_type);
90 static void hisi_zip_config_tag(struct hisi_zip_sqe *sqe, u32 tag)
95 static void hisi_zip_fill_sqe(struct hisi_zip_sqe *sqe, u8 req_type,
96 dma_addr_t s_addr, dma_addr_t d_addr, u32 slen,
99 memset(sqe, 0, sizeof(struct hisi_zip_sqe));
101 sqe->input_data_length = slen;
102 sqe->dw9 = FIELD_PREP(HZIP_REQ_TYPE_M, req_type);
103 sqe->dest_avail_out = dlen;
104 sqe->source_addr_l = lower_32_bits(s_addr);
105 sqe->source_addr_h = upper_32_bits(s_addr);
106 sqe->dest_addr_l = lower_32_bits(d_addr);
107 sqe->dest_addr_h = upper_32_bits(d_addr);
110 static int hisi_zip_create_qp(struct hisi_qm *qm, struct hisi_zip_qp_ctx *ctx,
111 int alg_type, int req_type)
116 qp = hisi_qm_create_qp(qm, alg_type);
120 qp->req_type = req_type;
124 ret = hisi_qm_start_qp(qp, 0);
131 hisi_qm_release_qp(qp);
135 static void hisi_zip_release_qp(struct hisi_zip_qp_ctx *ctx)
137 hisi_qm_stop_qp(ctx->qp);
138 hisi_qm_release_qp(ctx->qp);
141 static int hisi_zip_ctx_init(struct hisi_zip_ctx *hisi_zip_ctx, u8 req_type)
143 struct hisi_zip *hisi_zip;
147 /* find the proper zip device */
148 hisi_zip = find_zip_device(cpu_to_node(smp_processor_id()));
150 pr_err("Failed to find a proper ZIP device!\n");
155 for (i = 0; i < HZIP_CTX_Q_NUM; i++) {
156 /* alg_type = 0 for compress, 1 for decompress in hw sqe */
157 ret = hisi_zip_create_qp(qm, &hisi_zip_ctx->qp_ctx[i], i,
162 hisi_zip_ctx->qp_ctx[i].zip_dev = hisi_zip;
167 for (j = i - 1; j >= 0; j--)
168 hisi_zip_release_qp(&hisi_zip_ctx->qp_ctx[j]);
173 static void hisi_zip_ctx_exit(struct hisi_zip_ctx *hisi_zip_ctx)
177 for (i = 1; i >= 0; i--)
178 hisi_zip_release_qp(&hisi_zip_ctx->qp_ctx[i]);
181 static u16 get_extra_field_size(const u8 *start)
183 return *((u16 *)start) + GZIP_HEAD_FEXTRA_XLEN;
186 static u32 get_name_field_size(const u8 *start)
188 return strlen(start) + 1;
191 static u32 get_comment_field_size(const u8 *start)
193 return strlen(start) + 1;
196 static u32 __get_gzip_head_size(const u8 *src)
198 u8 head_flg = *(src + GZIP_HEAD_FLG_SHIFT);
199 u32 size = GZIP_HEAD_FEXTRA_SHIFT;
201 if (head_flg & GZIP_HEAD_FEXTRA_BIT)
202 size += get_extra_field_size(src + size);
203 if (head_flg & GZIP_HEAD_FNAME_BIT)
204 size += get_name_field_size(src + size);
205 if (head_flg & GZIP_HEAD_FCOMMENT_BIT)
206 size += get_comment_field_size(src + size);
207 if (head_flg & GZIP_HEAD_FHCRC_BIT)
208 size += GZIP_HEAD_FHCRC_SIZE;
213 static int hisi_zip_create_req_q(struct hisi_zip_ctx *ctx)
215 struct hisi_zip_req_q *req_q;
218 for (i = 0; i < HZIP_CTX_Q_NUM; i++) {
219 req_q = &ctx->qp_ctx[i].req_q;
220 req_q->size = QM_Q_DEPTH;
222 req_q->req_bitmap = kcalloc(BITS_TO_LONGS(req_q->size),
223 sizeof(long), GFP_KERNEL);
224 if (!req_q->req_bitmap) {
231 rwlock_init(&req_q->req_lock);
233 req_q->q = kcalloc(req_q->size, sizeof(struct hisi_zip_req),
238 goto err_free_bitmap;
247 kfree(ctx->qp_ctx[QPC_DECOMP].req_q.req_bitmap);
249 kfree(ctx->qp_ctx[QPC_COMP].req_q.q);
251 kfree(ctx->qp_ctx[QPC_COMP].req_q.req_bitmap);
255 static void hisi_zip_release_req_q(struct hisi_zip_ctx *ctx)
259 for (i = 0; i < HZIP_CTX_Q_NUM; i++) {
260 kfree(ctx->qp_ctx[i].req_q.q);
261 kfree(ctx->qp_ctx[i].req_q.req_bitmap);
265 static int hisi_zip_create_sgl_pool(struct hisi_zip_ctx *ctx)
267 struct hisi_zip_qp_ctx *tmp;
270 for (i = 0; i < HZIP_CTX_Q_NUM; i++) {
271 tmp = &ctx->qp_ctx[i];
272 ret = hisi_acc_create_sgl_pool(&tmp->qp->qm->pdev->dev,
277 goto err_free_sgl_pool0;
285 hisi_acc_free_sgl_pool(&ctx->qp_ctx[QPC_COMP].qp->qm->pdev->dev,
286 &ctx->qp_ctx[QPC_COMP].sgl_pool);
290 static void hisi_zip_release_sgl_pool(struct hisi_zip_ctx *ctx)
294 for (i = 0; i < HZIP_CTX_Q_NUM; i++)
295 hisi_acc_free_sgl_pool(&ctx->qp_ctx[i].qp->qm->pdev->dev,
296 &ctx->qp_ctx[i].sgl_pool);
299 static void hisi_zip_remove_req(struct hisi_zip_qp_ctx *qp_ctx,
300 struct hisi_zip_req *req)
302 struct hisi_zip_req_q *req_q = &qp_ctx->req_q;
304 if (qp_ctx->qp->alg_type == HZIP_ALG_TYPE_COMP)
309 write_lock(&req_q->req_lock);
310 clear_bit(req->req_id, req_q->req_bitmap);
311 memset(req, 0, sizeof(struct hisi_zip_req));
312 write_unlock(&req_q->req_lock);
315 static void hisi_zip_acomp_cb(struct hisi_qp *qp, void *data)
317 struct hisi_zip_sqe *sqe = data;
318 struct hisi_zip_qp_ctx *qp_ctx = qp->qp_ctx;
319 struct hisi_zip_req_q *req_q = &qp_ctx->req_q;
320 struct hisi_zip_req *req = req_q->q + sqe->tag;
321 struct acomp_req *acomp_req = req->req;
322 struct device *dev = &qp->qm->pdev->dev;
323 u32 status, dlen, head_size;
326 status = sqe->dw3 & HZIP_BD_STATUS_M;
328 if (status != 0 && status != HZIP_NC_ERR) {
329 dev_err(dev, "%scompress fail in qp%u: %u, output: %u\n",
330 (qp->alg_type == 0) ? "" : "de", qp->qp_id, status,
334 dlen = sqe->produced;
336 hisi_acc_sg_buf_unmap(dev, req->src, req->hw_src);
337 hisi_acc_sg_buf_unmap(dev, req->dst, req->hw_dst);
339 head_size = (qp->alg_type == 0) ? TO_HEAD_SIZE(qp->req_type) : 0;
340 acomp_req->dlen = dlen + head_size;
342 if (acomp_req->base.complete)
343 acomp_request_complete(acomp_req, err);
345 hisi_zip_remove_req(qp_ctx, req);
348 static void hisi_zip_set_acomp_cb(struct hisi_zip_ctx *ctx,
349 void (*fn)(struct hisi_qp *, void *))
353 for (i = 0; i < HZIP_CTX_Q_NUM; i++)
354 ctx->qp_ctx[i].qp->req_cb = fn;
357 static int hisi_zip_acomp_init(struct crypto_acomp *tfm)
359 const char *alg_name = crypto_tfm_alg_name(&tfm->base);
360 struct hisi_zip_ctx *ctx = crypto_tfm_ctx(&tfm->base);
363 ret = hisi_zip_ctx_init(ctx, COMP_NAME_TO_TYPE(alg_name));
367 ret = hisi_zip_create_req_q(ctx);
371 ret = hisi_zip_create_sgl_pool(ctx);
373 goto err_release_req_q;
375 hisi_zip_set_acomp_cb(ctx, hisi_zip_acomp_cb);
380 hisi_zip_release_req_q(ctx);
382 hisi_zip_ctx_exit(ctx);
386 static void hisi_zip_acomp_exit(struct crypto_acomp *tfm)
388 struct hisi_zip_ctx *ctx = crypto_tfm_ctx(&tfm->base);
390 hisi_zip_set_acomp_cb(ctx, NULL);
391 hisi_zip_release_sgl_pool(ctx);
392 hisi_zip_release_req_q(ctx);
393 hisi_zip_ctx_exit(ctx);
396 static int add_comp_head(struct scatterlist *dst, u8 req_type)
398 int head_size = TO_HEAD_SIZE(req_type);
399 const u8 *head = TO_HEAD(req_type);
402 ret = sg_copy_from_buffer(dst, sg_nents(dst), head, head_size);
403 if (ret != head_size)
409 static size_t get_gzip_head_size(struct scatterlist *sgl)
411 char buf[HZIP_GZIP_HEAD_BUF];
413 sg_copy_to_buffer(sgl, sg_nents(sgl), buf, sizeof(buf));
415 return __get_gzip_head_size(buf);
418 static size_t get_comp_head_size(struct scatterlist *src, u8 req_type)
421 case HZIP_ALG_TYPE_ZLIB:
422 return TO_HEAD_SIZE(HZIP_ALG_TYPE_ZLIB);
423 case HZIP_ALG_TYPE_GZIP:
424 return get_gzip_head_size(src);
426 pr_err("request type does not support!\n");
431 static int get_sg_skip_bytes(struct scatterlist *sgl, size_t bytes,
432 size_t remains, struct scatterlist **out)
435 size_t split_sizes[SPLIT_NUM];
436 int out_mapped_nents[SPLIT_NUM];
438 split_sizes[0] = bytes;
439 split_sizes[1] = remains;
441 return sg_split(sgl, 0, 0, SPLIT_NUM, split_sizes, out,
442 out_mapped_nents, GFP_KERNEL);
445 static struct hisi_zip_req *hisi_zip_create_req(struct acomp_req *req,
446 struct hisi_zip_qp_ctx *qp_ctx,
447 size_t head_size, bool is_comp)
449 struct hisi_zip_req_q *req_q = &qp_ctx->req_q;
450 struct hisi_zip_req *q = req_q->q;
451 struct hisi_zip_req *req_cache;
452 struct scatterlist *out[2];
453 struct scatterlist *sgl;
458 * remove/add zlib/gzip head, as hardware operations do not include
459 * comp head. so split req->src to get sgl without heads in acomp, or
460 * add comp head to req->dst ahead of that hardware output compressed
461 * data in sgl splited from req->dst without comp head.
465 len = req->dlen - head_size;
468 len = req->slen - head_size;
471 ret = get_sg_skip_bytes(sgl, head_size, len, out);
475 /* sgl for comp head is useless, so free it now */
478 write_lock(&req_q->req_lock);
480 req_id = find_first_zero_bit(req_q->req_bitmap, req_q->size);
481 if (req_id >= req_q->size) {
482 write_unlock(&req_q->req_lock);
483 dev_dbg(&qp_ctx->qp->qm->pdev->dev, "req cache is full!\n");
485 return ERR_PTR(-EBUSY);
487 set_bit(req_id, req_q->req_bitmap);
489 req_cache = q + req_id;
490 req_cache->req_id = req_id;
491 req_cache->req = req;
493 req_cache->src = req->src;
494 req_cache->dst = out[1];
495 req_cache->slen = req->slen;
496 req_cache->dlen = req->dlen - head_size;
498 req_cache->src = out[1];
499 req_cache->dst = req->dst;
500 req_cache->slen = req->slen - head_size;
501 req_cache->dlen = req->dlen;
504 write_unlock(&req_q->req_lock);
509 static int hisi_zip_do_work(struct hisi_zip_req *req,
510 struct hisi_zip_qp_ctx *qp_ctx)
512 struct hisi_zip_sqe *zip_sqe = &qp_ctx->zip_sqe;
513 struct hisi_qp *qp = qp_ctx->qp;
514 struct device *dev = &qp->qm->pdev->dev;
515 struct hisi_acc_sgl_pool *pool = &qp_ctx->sgl_pool;
520 if (!req->src || !req->slen || !req->dst || !req->dlen)
523 req->hw_src = hisi_acc_sg_buf_map_to_hw_sgl(dev, req->src, pool,
524 req->req_id << 1, &input);
525 if (IS_ERR(req->hw_src))
526 return PTR_ERR(req->hw_src);
527 req->dma_src = input;
529 req->hw_dst = hisi_acc_sg_buf_map_to_hw_sgl(dev, req->dst, pool,
530 (req->req_id << 1) + 1,
532 if (IS_ERR(req->hw_dst)) {
533 ret = PTR_ERR(req->hw_dst);
534 goto err_unmap_input;
536 req->dma_dst = output;
538 hisi_zip_fill_sqe(zip_sqe, qp->req_type, input, output, req->slen,
540 hisi_zip_config_buf_type(zip_sqe, HZIP_SGL);
541 hisi_zip_config_tag(zip_sqe, req->req_id);
543 /* send command to start a task */
544 ret = hisi_qp_send(qp, zip_sqe);
546 goto err_unmap_output;
551 hisi_acc_sg_buf_unmap(dev, req->dst, req->hw_dst);
553 hisi_acc_sg_buf_unmap(dev, req->src, req->hw_src);
557 static int hisi_zip_acompress(struct acomp_req *acomp_req)
559 struct hisi_zip_ctx *ctx = crypto_tfm_ctx(acomp_req->base.tfm);
560 struct hisi_zip_qp_ctx *qp_ctx = &ctx->qp_ctx[QPC_COMP];
561 struct hisi_zip_req *req;
565 /* let's output compression head now */
566 head_size = add_comp_head(acomp_req->dst, qp_ctx->qp->req_type);
570 req = hisi_zip_create_req(acomp_req, qp_ctx, head_size, true);
574 ret = hisi_zip_do_work(req, qp_ctx);
575 if (ret != -EINPROGRESS)
576 hisi_zip_remove_req(qp_ctx, req);
581 static int hisi_zip_adecompress(struct acomp_req *acomp_req)
583 struct hisi_zip_ctx *ctx = crypto_tfm_ctx(acomp_req->base.tfm);
584 struct hisi_zip_qp_ctx *qp_ctx = &ctx->qp_ctx[QPC_DECOMP];
585 struct hisi_zip_req *req;
589 head_size = get_comp_head_size(acomp_req->src, qp_ctx->qp->req_type);
591 req = hisi_zip_create_req(acomp_req, qp_ctx, head_size, false);
595 ret = hisi_zip_do_work(req, qp_ctx);
596 if (ret != -EINPROGRESS)
597 hisi_zip_remove_req(qp_ctx, req);
602 static struct acomp_alg hisi_zip_acomp_zlib = {
603 .init = hisi_zip_acomp_init,
604 .exit = hisi_zip_acomp_exit,
605 .compress = hisi_zip_acompress,
606 .decompress = hisi_zip_adecompress,
608 .cra_name = "zlib-deflate",
609 .cra_driver_name = "hisi-zlib-acomp",
610 .cra_module = THIS_MODULE,
611 .cra_priority = HZIP_ALG_PRIORITY,
612 .cra_ctxsize = sizeof(struct hisi_zip_ctx),
616 static struct acomp_alg hisi_zip_acomp_gzip = {
617 .init = hisi_zip_acomp_init,
618 .exit = hisi_zip_acomp_exit,
619 .compress = hisi_zip_acompress,
620 .decompress = hisi_zip_adecompress,
623 .cra_driver_name = "hisi-gzip-acomp",
624 .cra_module = THIS_MODULE,
625 .cra_priority = HZIP_ALG_PRIORITY,
626 .cra_ctxsize = sizeof(struct hisi_zip_ctx),
630 int hisi_zip_register_to_crypto(void)
634 ret = crypto_register_acomp(&hisi_zip_acomp_zlib);
636 pr_err("Zlib acomp algorithm registration failed\n");
640 ret = crypto_register_acomp(&hisi_zip_acomp_gzip);
642 pr_err("Gzip acomp algorithm registration failed\n");
643 crypto_unregister_acomp(&hisi_zip_acomp_zlib);
649 void hisi_zip_unregister_from_crypto(void)
651 crypto_unregister_acomp(&hisi_zip_acomp_gzip);
652 crypto_unregister_acomp(&hisi_zip_acomp_zlib);