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/bitmap.h>
6 #include <linux/dma-mapping.h>
7 #include <linux/scatterlist.h>
10 /* hisi_zip_sqe dw3 */
11 #define HZIP_BD_STATUS_M GENMASK(7, 0)
12 /* hisi_zip_sqe dw7 */
13 #define HZIP_IN_SGE_DATA_OFFSET_M GENMASK(23, 0)
14 #define HZIP_SQE_TYPE_M GENMASK(31, 28)
15 /* hisi_zip_sqe dw8 */
16 #define HZIP_OUT_SGE_DATA_OFFSET_M GENMASK(23, 0)
17 /* hisi_zip_sqe dw9 */
18 #define HZIP_REQ_TYPE_M GENMASK(7, 0)
19 #define HZIP_ALG_TYPE_DEFLATE 0x01
20 #define HZIP_BUF_TYPE_M GENMASK(11, 8)
23 #define HZIP_ALG_PRIORITY 300
24 #define HZIP_SGL_SGE_NR 10
26 #define HZIP_ALG_DEFLATE GENMASK(5, 4)
28 static DEFINE_MUTEX(zip_algs_lock
);
29 static unsigned int zip_available_devs
;
31 enum hisi_zip_alg_type
{
32 HZIP_ALG_TYPE_COMP
= 0,
33 HZIP_ALG_TYPE_DECOMP
= 1,
42 #define COMP_NAME_TO_TYPE(alg_name) \
43 (!strcmp((alg_name), "deflate") ? HZIP_ALG_TYPE_DEFLATE : 0)
46 struct acomp_req
*req
;
47 struct hisi_acc_hw_sgl
*hw_src
;
48 struct hisi_acc_hw_sgl
*hw_dst
;
54 struct hisi_zip_req_q
{
55 struct hisi_zip_req
*q
;
56 unsigned long *req_bitmap
;
61 struct hisi_zip_qp_ctx
{
63 struct hisi_zip_req_q req_q
;
64 struct hisi_acc_sgl_pool
*sgl_pool
;
65 struct hisi_zip
*zip_dev
;
66 struct hisi_zip_ctx
*ctx
;
69 struct hisi_zip_sqe_ops
{
71 void (*fill_addr
)(struct hisi_zip_sqe
*sqe
, struct hisi_zip_req
*req
);
72 void (*fill_buf_size
)(struct hisi_zip_sqe
*sqe
, struct hisi_zip_req
*req
);
73 void (*fill_buf_type
)(struct hisi_zip_sqe
*sqe
, u8 buf_type
);
74 void (*fill_req_type
)(struct hisi_zip_sqe
*sqe
, u8 req_type
);
75 void (*fill_tag
)(struct hisi_zip_sqe
*sqe
, struct hisi_zip_req
*req
);
76 void (*fill_sqe_type
)(struct hisi_zip_sqe
*sqe
, u8 sqe_type
);
77 u32 (*get_tag
)(struct hisi_zip_sqe
*sqe
);
78 u32 (*get_status
)(struct hisi_zip_sqe
*sqe
);
79 u32 (*get_dstlen
)(struct hisi_zip_sqe
*sqe
);
83 struct hisi_zip_qp_ctx qp_ctx
[HZIP_CTX_Q_NUM
];
84 const struct hisi_zip_sqe_ops
*ops
;
87 static int sgl_sge_nr_set(const char *val
, const struct kernel_param
*kp
)
95 ret
= kstrtou16(val
, 10, &n
);
96 if (ret
|| n
== 0 || n
> HISI_ACC_SGL_SGE_NR_MAX
)
99 return param_set_ushort(val
, kp
);
102 static const struct kernel_param_ops sgl_sge_nr_ops
= {
103 .set
= sgl_sge_nr_set
,
104 .get
= param_get_ushort
,
107 static u16 sgl_sge_nr
= HZIP_SGL_SGE_NR
;
108 module_param_cb(sgl_sge_nr
, &sgl_sge_nr_ops
, &sgl_sge_nr
, 0444);
109 MODULE_PARM_DESC(sgl_sge_nr
, "Number of sge in sgl(1-255)");
111 static struct hisi_zip_req
*hisi_zip_create_req(struct hisi_zip_qp_ctx
*qp_ctx
,
112 struct acomp_req
*req
)
114 struct hisi_zip_req_q
*req_q
= &qp_ctx
->req_q
;
115 struct hisi_zip_req
*q
= req_q
->q
;
116 struct hisi_zip_req
*req_cache
;
119 spin_lock(&req_q
->req_lock
);
121 req_id
= find_first_zero_bit(req_q
->req_bitmap
, req_q
->size
);
122 if (req_id
>= req_q
->size
) {
123 spin_unlock(&req_q
->req_lock
);
124 dev_dbg(&qp_ctx
->qp
->qm
->pdev
->dev
, "req cache is full!\n");
125 return ERR_PTR(-EAGAIN
);
127 set_bit(req_id
, req_q
->req_bitmap
);
129 spin_unlock(&req_q
->req_lock
);
131 req_cache
= q
+ req_id
;
132 req_cache
->req_id
= req_id
;
133 req_cache
->req
= req
;
138 static void hisi_zip_remove_req(struct hisi_zip_qp_ctx
*qp_ctx
,
139 struct hisi_zip_req
*req
)
141 struct hisi_zip_req_q
*req_q
= &qp_ctx
->req_q
;
143 spin_lock(&req_q
->req_lock
);
144 clear_bit(req
->req_id
, req_q
->req_bitmap
);
145 spin_unlock(&req_q
->req_lock
);
148 static void hisi_zip_fill_addr(struct hisi_zip_sqe
*sqe
, struct hisi_zip_req
*req
)
150 sqe
->source_addr_l
= lower_32_bits(req
->dma_src
);
151 sqe
->source_addr_h
= upper_32_bits(req
->dma_src
);
152 sqe
->dest_addr_l
= lower_32_bits(req
->dma_dst
);
153 sqe
->dest_addr_h
= upper_32_bits(req
->dma_dst
);
156 static void hisi_zip_fill_buf_size(struct hisi_zip_sqe
*sqe
, struct hisi_zip_req
*req
)
158 struct acomp_req
*a_req
= req
->req
;
160 sqe
->input_data_length
= a_req
->slen
;
161 sqe
->dest_avail_out
= a_req
->dlen
;
164 static void hisi_zip_fill_buf_type(struct hisi_zip_sqe
*sqe
, u8 buf_type
)
168 val
= sqe
->dw9
& ~HZIP_BUF_TYPE_M
;
169 val
|= FIELD_PREP(HZIP_BUF_TYPE_M
, buf_type
);
173 static void hisi_zip_fill_req_type(struct hisi_zip_sqe
*sqe
, u8 req_type
)
177 val
= sqe
->dw9
& ~HZIP_REQ_TYPE_M
;
178 val
|= FIELD_PREP(HZIP_REQ_TYPE_M
, req_type
);
182 static void hisi_zip_fill_tag(struct hisi_zip_sqe
*sqe
, struct hisi_zip_req
*req
)
184 sqe
->dw26
= req
->req_id
;
187 static void hisi_zip_fill_sqe_type(struct hisi_zip_sqe
*sqe
, u8 sqe_type
)
191 val
= sqe
->dw7
& ~HZIP_SQE_TYPE_M
;
192 val
|= FIELD_PREP(HZIP_SQE_TYPE_M
, sqe_type
);
196 static void hisi_zip_fill_sqe(struct hisi_zip_ctx
*ctx
, struct hisi_zip_sqe
*sqe
,
197 u8 req_type
, struct hisi_zip_req
*req
)
199 const struct hisi_zip_sqe_ops
*ops
= ctx
->ops
;
201 memset(sqe
, 0, sizeof(struct hisi_zip_sqe
));
203 ops
->fill_addr(sqe
, req
);
204 ops
->fill_buf_size(sqe
, req
);
205 ops
->fill_buf_type(sqe
, HZIP_SGL
);
206 ops
->fill_req_type(sqe
, req_type
);
207 ops
->fill_tag(sqe
, req
);
208 ops
->fill_sqe_type(sqe
, ops
->sqe_type
);
211 static int hisi_zip_do_work(struct hisi_zip_qp_ctx
*qp_ctx
,
212 struct hisi_zip_req
*req
)
214 struct hisi_acc_sgl_pool
*pool
= qp_ctx
->sgl_pool
;
215 struct hisi_zip_dfx
*dfx
= &qp_ctx
->zip_dev
->dfx
;
216 struct hisi_zip_req_q
*req_q
= &qp_ctx
->req_q
;
217 struct acomp_req
*a_req
= req
->req
;
218 struct hisi_qp
*qp
= qp_ctx
->qp
;
219 struct device
*dev
= &qp
->qm
->pdev
->dev
;
220 struct hisi_zip_sqe zip_sqe
;
223 if (unlikely(!a_req
->src
|| !a_req
->slen
|| !a_req
->dst
|| !a_req
->dlen
))
226 req
->hw_src
= hisi_acc_sg_buf_map_to_hw_sgl(dev
, a_req
->src
, pool
,
227 req
->req_id
<< 1, &req
->dma_src
);
228 if (IS_ERR(req
->hw_src
)) {
229 dev_err(dev
, "failed to map the src buffer to hw sgl (%ld)!\n",
230 PTR_ERR(req
->hw_src
));
231 return PTR_ERR(req
->hw_src
);
234 req
->hw_dst
= hisi_acc_sg_buf_map_to_hw_sgl(dev
, a_req
->dst
, pool
,
235 (req
->req_id
<< 1) + 1,
237 if (IS_ERR(req
->hw_dst
)) {
238 ret
= PTR_ERR(req
->hw_dst
);
239 dev_err(dev
, "failed to map the dst buffer to hw slg (%d)!\n",
241 goto err_unmap_input
;
244 hisi_zip_fill_sqe(qp_ctx
->ctx
, &zip_sqe
, qp
->req_type
, req
);
246 /* send command to start a task */
247 atomic64_inc(&dfx
->send_cnt
);
248 spin_lock_bh(&req_q
->req_lock
);
249 ret
= hisi_qp_send(qp
, &zip_sqe
);
250 spin_unlock_bh(&req_q
->req_lock
);
251 if (unlikely(ret
< 0)) {
252 atomic64_inc(&dfx
->send_busy_cnt
);
254 dev_dbg_ratelimited(dev
, "failed to send request!\n");
255 goto err_unmap_output
;
261 hisi_acc_sg_buf_unmap(dev
, a_req
->dst
, req
->hw_dst
);
263 hisi_acc_sg_buf_unmap(dev
, a_req
->src
, req
->hw_src
);
267 static u32
hisi_zip_get_tag(struct hisi_zip_sqe
*sqe
)
272 static u32
hisi_zip_get_status(struct hisi_zip_sqe
*sqe
)
274 return sqe
->dw3
& HZIP_BD_STATUS_M
;
277 static u32
hisi_zip_get_dstlen(struct hisi_zip_sqe
*sqe
)
279 return sqe
->produced
;
282 static void hisi_zip_acomp_cb(struct hisi_qp
*qp
, void *data
)
284 struct hisi_zip_qp_ctx
*qp_ctx
= qp
->qp_ctx
;
285 const struct hisi_zip_sqe_ops
*ops
= qp_ctx
->ctx
->ops
;
286 struct hisi_zip_dfx
*dfx
= &qp_ctx
->zip_dev
->dfx
;
287 struct hisi_zip_req_q
*req_q
= &qp_ctx
->req_q
;
288 struct device
*dev
= &qp
->qm
->pdev
->dev
;
289 struct hisi_zip_sqe
*sqe
= data
;
290 u32 tag
= ops
->get_tag(sqe
);
291 struct hisi_zip_req
*req
= req_q
->q
+ tag
;
292 struct acomp_req
*acomp_req
= req
->req
;
296 atomic64_inc(&dfx
->recv_cnt
);
297 status
= ops
->get_status(sqe
);
298 if (unlikely(status
!= 0 && status
!= HZIP_NC_ERR
)) {
299 dev_err(dev
, "%scompress fail in qp%u: %u, output: %u\n",
300 (qp
->alg_type
== 0) ? "" : "de", qp
->qp_id
, status
,
302 atomic64_inc(&dfx
->err_bd_cnt
);
306 hisi_acc_sg_buf_unmap(dev
, acomp_req
->src
, req
->hw_src
);
307 hisi_acc_sg_buf_unmap(dev
, acomp_req
->dst
, req
->hw_dst
);
309 acomp_req
->dlen
= ops
->get_dstlen(sqe
);
311 if (acomp_req
->base
.complete
)
312 acomp_request_complete(acomp_req
, err
);
314 hisi_zip_remove_req(qp_ctx
, req
);
317 static int hisi_zip_acompress(struct acomp_req
*acomp_req
)
319 struct hisi_zip_ctx
*ctx
= crypto_tfm_ctx(acomp_req
->base
.tfm
);
320 struct hisi_zip_qp_ctx
*qp_ctx
= &ctx
->qp_ctx
[HZIP_QPC_COMP
];
321 struct device
*dev
= &qp_ctx
->qp
->qm
->pdev
->dev
;
322 struct hisi_zip_req
*req
;
325 req
= hisi_zip_create_req(qp_ctx
, acomp_req
);
329 ret
= hisi_zip_do_work(qp_ctx
, req
);
330 if (unlikely(ret
!= -EINPROGRESS
)) {
331 dev_info_ratelimited(dev
, "failed to do compress (%d)!\n", ret
);
332 hisi_zip_remove_req(qp_ctx
, req
);
338 static int hisi_zip_adecompress(struct acomp_req
*acomp_req
)
340 struct hisi_zip_ctx
*ctx
= crypto_tfm_ctx(acomp_req
->base
.tfm
);
341 struct hisi_zip_qp_ctx
*qp_ctx
= &ctx
->qp_ctx
[HZIP_QPC_DECOMP
];
342 struct device
*dev
= &qp_ctx
->qp
->qm
->pdev
->dev
;
343 struct hisi_zip_req
*req
;
346 req
= hisi_zip_create_req(qp_ctx
, acomp_req
);
350 ret
= hisi_zip_do_work(qp_ctx
, req
);
351 if (unlikely(ret
!= -EINPROGRESS
)) {
352 dev_info_ratelimited(dev
, "failed to do decompress (%d)!\n",
354 hisi_zip_remove_req(qp_ctx
, req
);
360 static int hisi_zip_start_qp(struct hisi_qp
*qp
, struct hisi_zip_qp_ctx
*qp_ctx
,
361 int alg_type
, int req_type
)
363 struct device
*dev
= &qp
->qm
->pdev
->dev
;
366 qp
->req_type
= req_type
;
367 qp
->alg_type
= alg_type
;
370 ret
= hisi_qm_start_qp(qp
, 0);
372 dev_err(dev
, "failed to start qp (%d)!\n", ret
);
381 static void hisi_zip_release_qp(struct hisi_zip_qp_ctx
*qp_ctx
)
383 hisi_qm_stop_qp(qp_ctx
->qp
);
384 hisi_qm_free_qps(&qp_ctx
->qp
, 1);
387 static const struct hisi_zip_sqe_ops hisi_zip_ops
= {
389 .fill_addr
= hisi_zip_fill_addr
,
390 .fill_buf_size
= hisi_zip_fill_buf_size
,
391 .fill_buf_type
= hisi_zip_fill_buf_type
,
392 .fill_req_type
= hisi_zip_fill_req_type
,
393 .fill_tag
= hisi_zip_fill_tag
,
394 .fill_sqe_type
= hisi_zip_fill_sqe_type
,
395 .get_tag
= hisi_zip_get_tag
,
396 .get_status
= hisi_zip_get_status
,
397 .get_dstlen
= hisi_zip_get_dstlen
,
400 static int hisi_zip_ctx_init(struct hisi_zip_ctx
*hisi_zip_ctx
, u8 req_type
, int node
)
402 struct hisi_qp
*qps
[HZIP_CTX_Q_NUM
] = { NULL
};
403 struct hisi_zip_qp_ctx
*qp_ctx
;
404 struct hisi_zip
*hisi_zip
;
407 ret
= zip_create_qps(qps
, HZIP_CTX_Q_NUM
, node
);
409 pr_err("failed to create zip qps (%d)!\n", ret
);
413 hisi_zip
= container_of(qps
[0]->qm
, struct hisi_zip
, qm
);
415 for (i
= 0; i
< HZIP_CTX_Q_NUM
; i
++) {
416 /* alg_type = 0 for compress, 1 for decompress in hw sqe */
417 qp_ctx
= &hisi_zip_ctx
->qp_ctx
[i
];
418 qp_ctx
->ctx
= hisi_zip_ctx
;
419 ret
= hisi_zip_start_qp(qps
[i
], qp_ctx
, i
, req_type
);
421 for (j
= i
- 1; j
>= 0; j
--)
422 hisi_qm_stop_qp(hisi_zip_ctx
->qp_ctx
[j
].qp
);
424 hisi_qm_free_qps(qps
, HZIP_CTX_Q_NUM
);
428 qp_ctx
->zip_dev
= hisi_zip
;
431 hisi_zip_ctx
->ops
= &hisi_zip_ops
;
436 static void hisi_zip_ctx_exit(struct hisi_zip_ctx
*hisi_zip_ctx
)
440 for (i
= 0; i
< HZIP_CTX_Q_NUM
; i
++)
441 hisi_zip_release_qp(&hisi_zip_ctx
->qp_ctx
[i
]);
444 static int hisi_zip_create_req_q(struct hisi_zip_ctx
*ctx
)
446 u16 q_depth
= ctx
->qp_ctx
[0].qp
->sq_depth
;
447 struct hisi_zip_req_q
*req_q
;
450 for (i
= 0; i
< HZIP_CTX_Q_NUM
; i
++) {
451 req_q
= &ctx
->qp_ctx
[i
].req_q
;
452 req_q
->size
= q_depth
;
454 req_q
->req_bitmap
= bitmap_zalloc(req_q
->size
, GFP_KERNEL
);
455 if (!req_q
->req_bitmap
) {
460 goto err_free_comp_q
;
462 spin_lock_init(&req_q
->req_lock
);
464 req_q
->q
= kcalloc(req_q
->size
, sizeof(struct hisi_zip_req
),
469 goto err_free_comp_bitmap
;
471 goto err_free_decomp_bitmap
;
477 err_free_decomp_bitmap
:
478 bitmap_free(ctx
->qp_ctx
[HZIP_QPC_DECOMP
].req_q
.req_bitmap
);
480 kfree(ctx
->qp_ctx
[HZIP_QPC_COMP
].req_q
.q
);
481 err_free_comp_bitmap
:
482 bitmap_free(ctx
->qp_ctx
[HZIP_QPC_COMP
].req_q
.req_bitmap
);
486 static void hisi_zip_release_req_q(struct hisi_zip_ctx
*ctx
)
490 for (i
= 0; i
< HZIP_CTX_Q_NUM
; i
++) {
491 kfree(ctx
->qp_ctx
[i
].req_q
.q
);
492 bitmap_free(ctx
->qp_ctx
[i
].req_q
.req_bitmap
);
496 static int hisi_zip_create_sgl_pool(struct hisi_zip_ctx
*ctx
)
498 u16 q_depth
= ctx
->qp_ctx
[0].qp
->sq_depth
;
499 struct hisi_zip_qp_ctx
*tmp
;
503 for (i
= 0; i
< HZIP_CTX_Q_NUM
; i
++) {
504 tmp
= &ctx
->qp_ctx
[i
];
505 dev
= &tmp
->qp
->qm
->pdev
->dev
;
506 tmp
->sgl_pool
= hisi_acc_create_sgl_pool(dev
, q_depth
<< 1,
508 if (IS_ERR(tmp
->sgl_pool
)) {
510 goto err_free_sgl_pool0
;
518 hisi_acc_free_sgl_pool(&ctx
->qp_ctx
[HZIP_QPC_COMP
].qp
->qm
->pdev
->dev
,
519 ctx
->qp_ctx
[HZIP_QPC_COMP
].sgl_pool
);
523 static void hisi_zip_release_sgl_pool(struct hisi_zip_ctx
*ctx
)
527 for (i
= 0; i
< HZIP_CTX_Q_NUM
; i
++)
528 hisi_acc_free_sgl_pool(&ctx
->qp_ctx
[i
].qp
->qm
->pdev
->dev
,
529 ctx
->qp_ctx
[i
].sgl_pool
);
532 static void hisi_zip_set_acomp_cb(struct hisi_zip_ctx
*ctx
,
533 void (*fn
)(struct hisi_qp
*, void *))
537 for (i
= 0; i
< HZIP_CTX_Q_NUM
; i
++)
538 ctx
->qp_ctx
[i
].qp
->req_cb
= fn
;
541 static int hisi_zip_acomp_init(struct crypto_acomp
*tfm
)
543 const char *alg_name
= crypto_tfm_alg_name(&tfm
->base
);
544 struct hisi_zip_ctx
*ctx
= crypto_tfm_ctx(&tfm
->base
);
548 ret
= hisi_zip_ctx_init(ctx
, COMP_NAME_TO_TYPE(alg_name
), tfm
->base
.node
);
550 pr_err("failed to init ctx (%d)!\n", ret
);
554 dev
= &ctx
->qp_ctx
[0].qp
->qm
->pdev
->dev
;
556 ret
= hisi_zip_create_req_q(ctx
);
558 dev_err(dev
, "failed to create request queue (%d)!\n", ret
);
562 ret
= hisi_zip_create_sgl_pool(ctx
);
564 dev_err(dev
, "failed to create sgl pool (%d)!\n", ret
);
565 goto err_release_req_q
;
568 hisi_zip_set_acomp_cb(ctx
, hisi_zip_acomp_cb
);
573 hisi_zip_release_req_q(ctx
);
575 hisi_zip_ctx_exit(ctx
);
579 static void hisi_zip_acomp_exit(struct crypto_acomp
*tfm
)
581 struct hisi_zip_ctx
*ctx
= crypto_tfm_ctx(&tfm
->base
);
583 hisi_zip_set_acomp_cb(ctx
, NULL
);
584 hisi_zip_release_sgl_pool(ctx
);
585 hisi_zip_release_req_q(ctx
);
586 hisi_zip_ctx_exit(ctx
);
589 static struct acomp_alg hisi_zip_acomp_deflate
= {
590 .init
= hisi_zip_acomp_init
,
591 .exit
= hisi_zip_acomp_exit
,
592 .compress
= hisi_zip_acompress
,
593 .decompress
= hisi_zip_adecompress
,
595 .cra_name
= "deflate",
596 .cra_driver_name
= "hisi-deflate-acomp",
597 .cra_flags
= CRYPTO_ALG_ASYNC
,
598 .cra_module
= THIS_MODULE
,
599 .cra_priority
= HZIP_ALG_PRIORITY
,
600 .cra_ctxsize
= sizeof(struct hisi_zip_ctx
),
604 static int hisi_zip_register_deflate(struct hisi_qm
*qm
)
608 if (!hisi_zip_alg_support(qm
, HZIP_ALG_DEFLATE
))
611 ret
= crypto_register_acomp(&hisi_zip_acomp_deflate
);
613 dev_err(&qm
->pdev
->dev
, "failed to register to deflate (%d)!\n", ret
);
618 static void hisi_zip_unregister_deflate(struct hisi_qm
*qm
)
620 if (!hisi_zip_alg_support(qm
, HZIP_ALG_DEFLATE
))
623 crypto_unregister_acomp(&hisi_zip_acomp_deflate
);
626 int hisi_zip_register_to_crypto(struct hisi_qm
*qm
)
630 mutex_lock(&zip_algs_lock
);
631 if (zip_available_devs
++)
634 ret
= hisi_zip_register_deflate(qm
);
636 zip_available_devs
--;
639 mutex_unlock(&zip_algs_lock
);
643 void hisi_zip_unregister_from_crypto(struct hisi_qm
*qm
)
645 mutex_lock(&zip_algs_lock
);
646 if (--zip_available_devs
)
649 hisi_zip_unregister_deflate(qm
);
652 mutex_unlock(&zip_algs_lock
);