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>
7 #include "zip.h"
8 
9 #define HZIP_ZLIB_HEAD_SIZE			2
10 #define HZIP_GZIP_HEAD_SIZE			10
11 
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)
16 
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
21 
22 #define HZIP_CTX_Q_NUM				2
23 #define HZIP_GZIP_HEAD_BUF			256
24 #define HZIP_ALG_PRIORITY			300
25 #define HZIP_SGL_SGE_NR				10
26 
27 static const u8 zlib_head[HZIP_ZLIB_HEAD_SIZE] = {0x78, 0x9c};
28 static const u8 gzip_head[HZIP_GZIP_HEAD_SIZE] = {0x1f, 0x8b, 0x08, 0x0, 0x0,
29 						  0x0, 0x0, 0x0, 0x0, 0x03};
30 enum hisi_zip_alg_type {
31 	HZIP_ALG_TYPE_COMP = 0,
32 	HZIP_ALG_TYPE_DECOMP = 1,
33 };
34 
35 #define COMP_NAME_TO_TYPE(alg_name)					\
36 	(!strcmp((alg_name), "zlib-deflate") ? HZIP_ALG_TYPE_ZLIB :	\
37 	 !strcmp((alg_name), "gzip") ? HZIP_ALG_TYPE_GZIP : 0)		\
38 
39 #define TO_HEAD_SIZE(req_type)						\
40 	(((req_type) == HZIP_ALG_TYPE_ZLIB) ? sizeof(zlib_head) :	\
41 	 ((req_type) == HZIP_ALG_TYPE_GZIP) ? sizeof(gzip_head) : 0)	\
42 
43 #define TO_HEAD(req_type)						\
44 	(((req_type) == HZIP_ALG_TYPE_ZLIB) ? zlib_head :		\
45 	 ((req_type) == HZIP_ALG_TYPE_GZIP) ? gzip_head : NULL)		\
46 
47 struct hisi_zip_req {
48 	struct acomp_req *req;
49 	int sskip;
50 	int dskip;
51 	struct hisi_acc_hw_sgl *hw_src;
52 	struct hisi_acc_hw_sgl *hw_dst;
53 	dma_addr_t dma_src;
54 	dma_addr_t dma_dst;
55 	int req_id;
56 };
57 
58 struct hisi_zip_req_q {
59 	struct hisi_zip_req *q;
60 	unsigned long *req_bitmap;
61 	rwlock_t req_lock;
62 	u16 size;
63 };
64 
65 struct hisi_zip_qp_ctx {
66 	struct hisi_qp *qp;
67 	struct hisi_zip_req_q req_q;
68 	struct hisi_acc_sgl_pool *sgl_pool;
69 	struct hisi_zip *zip_dev;
70 	struct hisi_zip_ctx *ctx;
71 };
72 
73 struct hisi_zip_ctx {
74 #define QPC_COMP	0
75 #define QPC_DECOMP	1
76 	struct hisi_zip_qp_ctx qp_ctx[HZIP_CTX_Q_NUM];
77 };
78 
79 static int sgl_sge_nr_set(const char *val, const struct kernel_param *kp)
80 {
81 	int ret;
82 	u16 n;
83 
84 	if (!val)
85 		return -EINVAL;
86 
87 	ret = kstrtou16(val, 10, &n);
88 	if (ret || n == 0 || n > HISI_ACC_SGL_SGE_NR_MAX)
89 		return -EINVAL;
90 
91 	return param_set_int(val, kp);
92 }
93 
94 static const struct kernel_param_ops sgl_sge_nr_ops = {
95 	.set = sgl_sge_nr_set,
96 	.get = param_get_int,
97 };
98 
99 static u16 sgl_sge_nr = HZIP_SGL_SGE_NR;
100 module_param_cb(sgl_sge_nr, &sgl_sge_nr_ops, &sgl_sge_nr, 0444);
101 MODULE_PARM_DESC(sgl_sge_nr, "Number of sge in sgl(1-255)");
102 
103 static void hisi_zip_config_buf_type(struct hisi_zip_sqe *sqe, u8 buf_type)
104 {
105 	u32 val;
106 
107 	val = (sqe->dw9) & ~HZIP_BUF_TYPE_M;
108 	val |= FIELD_PREP(HZIP_BUF_TYPE_M, buf_type);
109 	sqe->dw9 = val;
110 }
111 
112 static void hisi_zip_config_tag(struct hisi_zip_sqe *sqe, u32 tag)
113 {
114 	sqe->tag = tag;
115 }
116 
117 static void hisi_zip_fill_sqe(struct hisi_zip_sqe *sqe, u8 req_type,
118 			      dma_addr_t s_addr, dma_addr_t d_addr, u32 slen,
119 			      u32 dlen, int sskip, int dskip)
120 {
121 	memset(sqe, 0, sizeof(struct hisi_zip_sqe));
122 
123 	sqe->input_data_length = slen - sskip;
124 	sqe->dw7 = FIELD_PREP(HZIP_IN_SGE_DATA_OFFSET_M, sskip);
125 	sqe->dw8 = FIELD_PREP(HZIP_OUT_SGE_DATA_OFFSET_M, dskip);
126 	sqe->dw9 = FIELD_PREP(HZIP_REQ_TYPE_M, req_type);
127 	sqe->dest_avail_out = dlen - dskip;
128 	sqe->source_addr_l = lower_32_bits(s_addr);
129 	sqe->source_addr_h = upper_32_bits(s_addr);
130 	sqe->dest_addr_l = lower_32_bits(d_addr);
131 	sqe->dest_addr_h = upper_32_bits(d_addr);
132 }
133 
134 static int hisi_zip_start_qp(struct hisi_qp *qp, struct hisi_zip_qp_ctx *ctx,
135 			     int alg_type, int req_type)
136 {
137 	struct device *dev = &qp->qm->pdev->dev;
138 	int ret;
139 
140 	qp->req_type = req_type;
141 	qp->alg_type = alg_type;
142 	qp->qp_ctx = ctx;
143 
144 	ret = hisi_qm_start_qp(qp, 0);
145 	if (ret < 0) {
146 		dev_err(dev, "start qp failed!\n");
147 		return ret;
148 	}
149 
150 	ctx->qp = qp;
151 
152 	return 0;
153 }
154 
155 static void hisi_zip_release_qp(struct hisi_zip_qp_ctx *ctx)
156 {
157 	hisi_qm_stop_qp(ctx->qp);
158 	hisi_qm_release_qp(ctx->qp);
159 }
160 
161 static int hisi_zip_ctx_init(struct hisi_zip_ctx *hisi_zip_ctx, u8 req_type)
162 {
163 	struct hisi_qp *qps[HZIP_CTX_Q_NUM] = { NULL };
164 	struct hisi_zip *hisi_zip;
165 	int ret, i, j;
166 
167 	ret = zip_create_qps(qps, HZIP_CTX_Q_NUM);
168 	if (ret) {
169 		pr_err("Can not create zip qps!\n");
170 		return -ENODEV;
171 	}
172 
173 	hisi_zip = container_of(qps[0]->qm, struct hisi_zip, qm);
174 
175 	for (i = 0; i < HZIP_CTX_Q_NUM; i++) {
176 		/* alg_type = 0 for compress, 1 for decompress in hw sqe */
177 		ret = hisi_zip_start_qp(qps[i], &hisi_zip_ctx->qp_ctx[i], i,
178 					req_type);
179 		if (ret) {
180 			for (j = i - 1; j >= 0; j--)
181 				hisi_qm_stop_qp(hisi_zip_ctx->qp_ctx[j].qp);
182 
183 			hisi_qm_free_qps(qps, HZIP_CTX_Q_NUM);
184 			return ret;
185 		}
186 
187 		hisi_zip_ctx->qp_ctx[i].zip_dev = hisi_zip;
188 	}
189 
190 	return 0;
191 }
192 
193 static void hisi_zip_ctx_exit(struct hisi_zip_ctx *hisi_zip_ctx)
194 {
195 	int i;
196 
197 	for (i = 1; i >= 0; i--)
198 		hisi_zip_release_qp(&hisi_zip_ctx->qp_ctx[i]);
199 }
200 
201 static u16 get_extra_field_size(const u8 *start)
202 {
203 	return *((u16 *)start) + GZIP_HEAD_FEXTRA_XLEN;
204 }
205 
206 static u32 get_name_field_size(const u8 *start)
207 {
208 	return strlen(start) + 1;
209 }
210 
211 static u32 get_comment_field_size(const u8 *start)
212 {
213 	return strlen(start) + 1;
214 }
215 
216 static u32 __get_gzip_head_size(const u8 *src)
217 {
218 	u8 head_flg = *(src + GZIP_HEAD_FLG_SHIFT);
219 	u32 size = GZIP_HEAD_FEXTRA_SHIFT;
220 
221 	if (head_flg & GZIP_HEAD_FEXTRA_BIT)
222 		size += get_extra_field_size(src + size);
223 	if (head_flg & GZIP_HEAD_FNAME_BIT)
224 		size += get_name_field_size(src + size);
225 	if (head_flg & GZIP_HEAD_FCOMMENT_BIT)
226 		size += get_comment_field_size(src + size);
227 	if (head_flg & GZIP_HEAD_FHCRC_BIT)
228 		size += GZIP_HEAD_FHCRC_SIZE;
229 
230 	return size;
231 }
232 
233 static int hisi_zip_create_req_q(struct hisi_zip_ctx *ctx)
234 {
235 	struct hisi_zip_req_q *req_q;
236 	int i, ret;
237 
238 	for (i = 0; i < HZIP_CTX_Q_NUM; i++) {
239 		req_q = &ctx->qp_ctx[i].req_q;
240 		req_q->size = QM_Q_DEPTH;
241 
242 		req_q->req_bitmap = kcalloc(BITS_TO_LONGS(req_q->size),
243 					    sizeof(long), GFP_KERNEL);
244 		if (!req_q->req_bitmap) {
245 			ret = -ENOMEM;
246 			if (i == 0)
247 				return ret;
248 
249 			goto err_free_loop0;
250 		}
251 		rwlock_init(&req_q->req_lock);
252 
253 		req_q->q = kcalloc(req_q->size, sizeof(struct hisi_zip_req),
254 				   GFP_KERNEL);
255 		if (!req_q->q) {
256 			ret = -ENOMEM;
257 			if (i == 0)
258 				goto err_free_bitmap;
259 			else
260 				goto err_free_loop1;
261 		}
262 	}
263 
264 	return 0;
265 
266 err_free_loop1:
267 	kfree(ctx->qp_ctx[QPC_DECOMP].req_q.req_bitmap);
268 err_free_loop0:
269 	kfree(ctx->qp_ctx[QPC_COMP].req_q.q);
270 err_free_bitmap:
271 	kfree(ctx->qp_ctx[QPC_COMP].req_q.req_bitmap);
272 	return ret;
273 }
274 
275 static void hisi_zip_release_req_q(struct hisi_zip_ctx *ctx)
276 {
277 	int i;
278 
279 	for (i = 0; i < HZIP_CTX_Q_NUM; i++) {
280 		kfree(ctx->qp_ctx[i].req_q.q);
281 		kfree(ctx->qp_ctx[i].req_q.req_bitmap);
282 	}
283 }
284 
285 static int hisi_zip_create_sgl_pool(struct hisi_zip_ctx *ctx)
286 {
287 	struct hisi_zip_qp_ctx *tmp;
288 	struct device *dev;
289 	int i;
290 
291 	for (i = 0; i < HZIP_CTX_Q_NUM; i++) {
292 		tmp = &ctx->qp_ctx[i];
293 		dev = &tmp->qp->qm->pdev->dev;
294 		tmp->sgl_pool = hisi_acc_create_sgl_pool(dev, QM_Q_DEPTH << 1,
295 							 sgl_sge_nr);
296 		if (IS_ERR(tmp->sgl_pool)) {
297 			if (i == 1)
298 				goto err_free_sgl_pool0;
299 			return -ENOMEM;
300 		}
301 	}
302 
303 	return 0;
304 
305 err_free_sgl_pool0:
306 	hisi_acc_free_sgl_pool(&ctx->qp_ctx[QPC_COMP].qp->qm->pdev->dev,
307 			       ctx->qp_ctx[QPC_COMP].sgl_pool);
308 	return -ENOMEM;
309 }
310 
311 static void hisi_zip_release_sgl_pool(struct hisi_zip_ctx *ctx)
312 {
313 	int i;
314 
315 	for (i = 0; i < HZIP_CTX_Q_NUM; i++)
316 		hisi_acc_free_sgl_pool(&ctx->qp_ctx[i].qp->qm->pdev->dev,
317 				       ctx->qp_ctx[i].sgl_pool);
318 }
319 
320 static void hisi_zip_remove_req(struct hisi_zip_qp_ctx *qp_ctx,
321 				struct hisi_zip_req *req)
322 {
323 	struct hisi_zip_req_q *req_q = &qp_ctx->req_q;
324 
325 	write_lock(&req_q->req_lock);
326 	clear_bit(req->req_id, req_q->req_bitmap);
327 	memset(req, 0, sizeof(struct hisi_zip_req));
328 	write_unlock(&req_q->req_lock);
329 }
330 
331 static void hisi_zip_acomp_cb(struct hisi_qp *qp, void *data)
332 {
333 	struct hisi_zip_sqe *sqe = data;
334 	struct hisi_zip_qp_ctx *qp_ctx = qp->qp_ctx;
335 	struct hisi_zip_dfx *dfx = &qp_ctx->zip_dev->dfx;
336 	struct hisi_zip_req_q *req_q = &qp_ctx->req_q;
337 	struct hisi_zip_req *req = req_q->q + sqe->tag;
338 	struct acomp_req *acomp_req = req->req;
339 	struct device *dev = &qp->qm->pdev->dev;
340 	u32 status, dlen, head_size;
341 	int err = 0;
342 
343 	atomic64_inc(&dfx->recv_cnt);
344 	status = sqe->dw3 & HZIP_BD_STATUS_M;
345 
346 	if (status != 0 && status != HZIP_NC_ERR) {
347 		dev_err(dev, "%scompress fail in qp%u: %u, output: %u\n",
348 			(qp->alg_type == 0) ? "" : "de", qp->qp_id, status,
349 			sqe->produced);
350 		atomic64_inc(&dfx->err_bd_cnt);
351 		err = -EIO;
352 	}
353 	dlen = sqe->produced;
354 
355 	hisi_acc_sg_buf_unmap(dev, acomp_req->src, req->hw_src);
356 	hisi_acc_sg_buf_unmap(dev, acomp_req->dst, req->hw_dst);
357 
358 	head_size = (qp->alg_type == 0) ? TO_HEAD_SIZE(qp->req_type) : 0;
359 	acomp_req->dlen = dlen + head_size;
360 
361 	if (acomp_req->base.complete)
362 		acomp_request_complete(acomp_req, err);
363 
364 	hisi_zip_remove_req(qp_ctx, req);
365 }
366 
367 static void hisi_zip_set_acomp_cb(struct hisi_zip_ctx *ctx,
368 				  void (*fn)(struct hisi_qp *, void *))
369 {
370 	int i;
371 
372 	for (i = 0; i < HZIP_CTX_Q_NUM; i++)
373 		ctx->qp_ctx[i].qp->req_cb = fn;
374 }
375 
376 static int hisi_zip_acomp_init(struct crypto_acomp *tfm)
377 {
378 	const char *alg_name = crypto_tfm_alg_name(&tfm->base);
379 	struct hisi_zip_ctx *ctx = crypto_tfm_ctx(&tfm->base);
380 	int ret;
381 
382 	ret = hisi_zip_ctx_init(ctx, COMP_NAME_TO_TYPE(alg_name));
383 	if (ret)
384 		return ret;
385 
386 	ret = hisi_zip_create_req_q(ctx);
387 	if (ret)
388 		goto err_ctx_exit;
389 
390 	ret = hisi_zip_create_sgl_pool(ctx);
391 	if (ret)
392 		goto err_release_req_q;
393 
394 	hisi_zip_set_acomp_cb(ctx, hisi_zip_acomp_cb);
395 
396 	return 0;
397 
398 err_release_req_q:
399 	hisi_zip_release_req_q(ctx);
400 err_ctx_exit:
401 	hisi_zip_ctx_exit(ctx);
402 	return ret;
403 }
404 
405 static void hisi_zip_acomp_exit(struct crypto_acomp *tfm)
406 {
407 	struct hisi_zip_ctx *ctx = crypto_tfm_ctx(&tfm->base);
408 
409 	hisi_zip_set_acomp_cb(ctx, NULL);
410 	hisi_zip_release_sgl_pool(ctx);
411 	hisi_zip_release_req_q(ctx);
412 	hisi_zip_ctx_exit(ctx);
413 }
414 
415 static int add_comp_head(struct scatterlist *dst, u8 req_type)
416 {
417 	int head_size = TO_HEAD_SIZE(req_type);
418 	const u8 *head = TO_HEAD(req_type);
419 	int ret;
420 
421 	ret = sg_copy_from_buffer(dst, sg_nents(dst), head, head_size);
422 	if (ret != head_size)
423 		return -ENOMEM;
424 
425 	return head_size;
426 }
427 
428 static size_t get_gzip_head_size(struct scatterlist *sgl)
429 {
430 	char buf[HZIP_GZIP_HEAD_BUF];
431 
432 	sg_copy_to_buffer(sgl, sg_nents(sgl), buf, sizeof(buf));
433 
434 	return __get_gzip_head_size(buf);
435 }
436 
437 static size_t get_comp_head_size(struct scatterlist *src, u8 req_type)
438 {
439 	switch (req_type) {
440 	case HZIP_ALG_TYPE_ZLIB:
441 		return TO_HEAD_SIZE(HZIP_ALG_TYPE_ZLIB);
442 	case HZIP_ALG_TYPE_GZIP:
443 		return get_gzip_head_size(src);
444 	default:
445 		pr_err("request type does not support!\n");
446 		return -EINVAL;
447 	}
448 }
449 
450 static struct hisi_zip_req *hisi_zip_create_req(struct acomp_req *req,
451 						struct hisi_zip_qp_ctx *qp_ctx,
452 						size_t head_size, bool is_comp)
453 {
454 	struct hisi_zip_req_q *req_q = &qp_ctx->req_q;
455 	struct hisi_zip_req *q = req_q->q;
456 	struct hisi_zip_req *req_cache;
457 	int req_id;
458 
459 	write_lock(&req_q->req_lock);
460 
461 	req_id = find_first_zero_bit(req_q->req_bitmap, req_q->size);
462 	if (req_id >= req_q->size) {
463 		write_unlock(&req_q->req_lock);
464 		dev_dbg(&qp_ctx->qp->qm->pdev->dev, "req cache is full!\n");
465 		return ERR_PTR(-EBUSY);
466 	}
467 	set_bit(req_id, req_q->req_bitmap);
468 
469 	req_cache = q + req_id;
470 	req_cache->req_id = req_id;
471 	req_cache->req = req;
472 
473 	if (is_comp) {
474 		req_cache->sskip = 0;
475 		req_cache->dskip = head_size;
476 	} else {
477 		req_cache->sskip = head_size;
478 		req_cache->dskip = 0;
479 	}
480 
481 	write_unlock(&req_q->req_lock);
482 
483 	return req_cache;
484 }
485 
486 static int hisi_zip_do_work(struct hisi_zip_req *req,
487 			    struct hisi_zip_qp_ctx *qp_ctx)
488 {
489 	struct acomp_req *a_req = req->req;
490 	struct hisi_qp *qp = qp_ctx->qp;
491 	struct device *dev = &qp->qm->pdev->dev;
492 	struct hisi_acc_sgl_pool *pool = qp_ctx->sgl_pool;
493 	struct hisi_zip_dfx *dfx = &qp_ctx->zip_dev->dfx;
494 	struct hisi_zip_sqe zip_sqe;
495 	dma_addr_t input;
496 	dma_addr_t output;
497 	int ret;
498 
499 	if (!a_req->src || !a_req->slen || !a_req->dst || !a_req->dlen)
500 		return -EINVAL;
501 
502 	req->hw_src = hisi_acc_sg_buf_map_to_hw_sgl(dev, a_req->src, pool,
503 						    req->req_id << 1, &input);
504 	if (IS_ERR(req->hw_src))
505 		return PTR_ERR(req->hw_src);
506 	req->dma_src = input;
507 
508 	req->hw_dst = hisi_acc_sg_buf_map_to_hw_sgl(dev, a_req->dst, pool,
509 						    (req->req_id << 1) + 1,
510 						    &output);
511 	if (IS_ERR(req->hw_dst)) {
512 		ret = PTR_ERR(req->hw_dst);
513 		goto err_unmap_input;
514 	}
515 	req->dma_dst = output;
516 
517 	hisi_zip_fill_sqe(&zip_sqe, qp->req_type, input, output, a_req->slen,
518 			  a_req->dlen, req->sskip, req->dskip);
519 	hisi_zip_config_buf_type(&zip_sqe, HZIP_SGL);
520 	hisi_zip_config_tag(&zip_sqe, req->req_id);
521 
522 	/* send command to start a task */
523 	atomic64_inc(&dfx->send_cnt);
524 	ret = hisi_qp_send(qp, &zip_sqe);
525 	if (ret < 0) {
526 		atomic64_inc(&dfx->send_busy_cnt);
527 		goto err_unmap_output;
528 	}
529 
530 	return -EINPROGRESS;
531 
532 err_unmap_output:
533 	hisi_acc_sg_buf_unmap(dev, a_req->dst, req->hw_dst);
534 err_unmap_input:
535 	hisi_acc_sg_buf_unmap(dev, a_req->src, req->hw_src);
536 	return ret;
537 }
538 
539 static int hisi_zip_acompress(struct acomp_req *acomp_req)
540 {
541 	struct hisi_zip_ctx *ctx = crypto_tfm_ctx(acomp_req->base.tfm);
542 	struct hisi_zip_qp_ctx *qp_ctx = &ctx->qp_ctx[QPC_COMP];
543 	struct hisi_zip_req *req;
544 	int head_size;
545 	int ret;
546 
547 	/* let's output compression head now */
548 	head_size = add_comp_head(acomp_req->dst, qp_ctx->qp->req_type);
549 	if (head_size < 0)
550 		return -ENOMEM;
551 
552 	req = hisi_zip_create_req(acomp_req, qp_ctx, (size_t)head_size, true);
553 	if (IS_ERR(req))
554 		return PTR_ERR(req);
555 
556 	ret = hisi_zip_do_work(req, qp_ctx);
557 	if (ret != -EINPROGRESS)
558 		hisi_zip_remove_req(qp_ctx, req);
559 
560 	return ret;
561 }
562 
563 static int hisi_zip_adecompress(struct acomp_req *acomp_req)
564 {
565 	struct hisi_zip_ctx *ctx = crypto_tfm_ctx(acomp_req->base.tfm);
566 	struct hisi_zip_qp_ctx *qp_ctx = &ctx->qp_ctx[QPC_DECOMP];
567 	struct hisi_zip_req *req;
568 	size_t head_size;
569 	int ret;
570 
571 	head_size = get_comp_head_size(acomp_req->src, qp_ctx->qp->req_type);
572 
573 	req = hisi_zip_create_req(acomp_req, qp_ctx, head_size, false);
574 	if (IS_ERR(req))
575 		return PTR_ERR(req);
576 
577 	ret = hisi_zip_do_work(req, qp_ctx);
578 	if (ret != -EINPROGRESS)
579 		hisi_zip_remove_req(qp_ctx, req);
580 
581 	return ret;
582 }
583 
584 static struct acomp_alg hisi_zip_acomp_zlib = {
585 	.init			= hisi_zip_acomp_init,
586 	.exit			= hisi_zip_acomp_exit,
587 	.compress		= hisi_zip_acompress,
588 	.decompress		= hisi_zip_adecompress,
589 	.base			= {
590 		.cra_name		= "zlib-deflate",
591 		.cra_driver_name	= "hisi-zlib-acomp",
592 		.cra_module		= THIS_MODULE,
593 		.cra_priority           = HZIP_ALG_PRIORITY,
594 		.cra_ctxsize		= sizeof(struct hisi_zip_ctx),
595 	}
596 };
597 
598 static struct acomp_alg hisi_zip_acomp_gzip = {
599 	.init			= hisi_zip_acomp_init,
600 	.exit			= hisi_zip_acomp_exit,
601 	.compress		= hisi_zip_acompress,
602 	.decompress		= hisi_zip_adecompress,
603 	.base			= {
604 		.cra_name		= "gzip",
605 		.cra_driver_name	= "hisi-gzip-acomp",
606 		.cra_module		= THIS_MODULE,
607 		.cra_priority           = HZIP_ALG_PRIORITY,
608 		.cra_ctxsize		= sizeof(struct hisi_zip_ctx),
609 	}
610 };
611 
612 int hisi_zip_register_to_crypto(void)
613 {
614 	int ret = 0;
615 
616 	ret = crypto_register_acomp(&hisi_zip_acomp_zlib);
617 	if (ret) {
618 		pr_err("Zlib acomp algorithm registration failed\n");
619 		return ret;
620 	}
621 
622 	ret = crypto_register_acomp(&hisi_zip_acomp_gzip);
623 	if (ret) {
624 		pr_err("Gzip acomp algorithm registration failed\n");
625 		crypto_unregister_acomp(&hisi_zip_acomp_zlib);
626 	}
627 
628 	return ret;
629 }
630 
631 void hisi_zip_unregister_from_crypto(void)
632 {
633 	crypto_unregister_acomp(&hisi_zip_acomp_gzip);
634 	crypto_unregister_acomp(&hisi_zip_acomp_zlib);
635 }
636