Skip to content

Commit ea37779

Browse files
Chenghai Huangherbertx
authored andcommitted
crypto: hisilicon/zip - add lz4 algorithm for hisi_zip
Add the "hisi-lz4-acomp" algorithm by the crypto acomp. When the 8th bit of the capability register is 1, the lz4 algorithm will register to crypto acomp, and the window length is configured to 16K by default. Since the "hisi-lz4-acomp" currently only support compression direction, decompression is completed by the soft lz4 algorithm. Signed-off-by: Chenghai Huang <huangchenghai2@huawei.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 9d58d22 commit ea37779

2 files changed

Lines changed: 84 additions & 8 deletions

File tree

drivers/crypto/hisilicon/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ config CRYPTO_DEV_HISI_ZIP
5858
depends on ACPI
5959
select CRYPTO_DEV_HISI_QM
6060
select CRYPTO_DEFLATE
61+
select CRYPTO_LZ4
6162
help
6263
Support for HiSilicon ZIP Driver
6364

drivers/crypto/hisilicon/zip/zip_crypto.c

Lines changed: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@
1717
/* hisi_zip_sqe dw9 */
1818
#define HZIP_REQ_TYPE_M GENMASK(7, 0)
1919
#define HZIP_ALG_TYPE_DEFLATE 0x01
20+
#define HZIP_ALG_TYPE_LZ4 0x04
2021
#define HZIP_BUF_TYPE_M GENMASK(11, 8)
2122
#define HZIP_SGL 0x1
23+
#define HZIP_WIN_SIZE_M GENMASK(15, 12)
24+
#define HZIP_16K_WINSZ 0x2
2225

2326
#define HZIP_ALG_PRIORITY 300
2427
#define HZIP_SGL_SGE_NR 10
2528

2629
#define HZIP_ALG_DEFLATE GENMASK(5, 4)
30+
#define HZIP_ALG_LZ4 BIT(8)
2731

2832
static DEFINE_MUTEX(zip_algs_lock);
2933
static unsigned int zip_available_devs;
@@ -41,7 +45,8 @@ enum {
4145

4246
#define GET_REQ_FROM_SQE(sqe) ((u64)(sqe)->dw26 | (u64)(sqe)->dw27 << 32)
4347
#define COMP_NAME_TO_TYPE(alg_name) \
44-
(!strcmp((alg_name), "deflate") ? HZIP_ALG_TYPE_DEFLATE : 0)
48+
(!strcmp((alg_name), "deflate") ? HZIP_ALG_TYPE_DEFLATE : \
49+
(!strcmp((alg_name), "lz4") ? HZIP_ALG_TYPE_LZ4 : 0))
4550

4651
struct hisi_zip_req {
4752
struct acomp_req *req;
@@ -75,6 +80,7 @@ struct hisi_zip_sqe_ops {
7580
void (*fill_buf_size)(struct hisi_zip_sqe *sqe, struct hisi_zip_req *req);
7681
void (*fill_buf_type)(struct hisi_zip_sqe *sqe, u8 buf_type);
7782
void (*fill_req_type)(struct hisi_zip_sqe *sqe, u8 req_type);
83+
void (*fill_win_size)(struct hisi_zip_sqe *sqe, u8 win_size);
7884
void (*fill_tag)(struct hisi_zip_sqe *sqe, struct hisi_zip_req *req);
7985
void (*fill_sqe_type)(struct hisi_zip_sqe *sqe, u8 sqe_type);
8086
u32 (*get_status)(struct hisi_zip_sqe *sqe);
@@ -201,6 +207,15 @@ static void hisi_zip_fill_req_type(struct hisi_zip_sqe *sqe, u8 req_type)
201207
sqe->dw9 = val;
202208
}
203209

210+
static void hisi_zip_fill_win_size(struct hisi_zip_sqe *sqe, u8 win_size)
211+
{
212+
u32 val;
213+
214+
val = sqe->dw9 & ~HZIP_WIN_SIZE_M;
215+
val |= FIELD_PREP(HZIP_WIN_SIZE_M, win_size);
216+
sqe->dw9 = val;
217+
}
218+
204219
static void hisi_zip_fill_tag(struct hisi_zip_sqe *sqe, struct hisi_zip_req *req)
205220
{
206221
sqe->dw26 = lower_32_bits((u64)req);
@@ -227,6 +242,7 @@ static void hisi_zip_fill_sqe(struct hisi_zip_ctx *ctx, struct hisi_zip_sqe *sqe
227242
ops->fill_buf_size(sqe, req);
228243
ops->fill_buf_type(sqe, HZIP_SGL);
229244
ops->fill_req_type(sqe, req_type);
245+
ops->fill_win_size(sqe, HZIP_16K_WINSZ);
230246
ops->fill_tag(sqe, req);
231247
ops->fill_sqe_type(sqe, ops->sqe_type);
232248
}
@@ -381,12 +397,18 @@ static int hisi_zip_adecompress(struct acomp_req *acomp_req)
381397
return ret;
382398
}
383399

400+
static int hisi_zip_decompress(struct acomp_req *acomp_req)
401+
{
402+
return hisi_zip_fallback_do_work(acomp_req, 1);
403+
}
404+
384405
static const struct hisi_zip_sqe_ops hisi_zip_ops = {
385406
.sqe_type = 0x3,
386407
.fill_addr = hisi_zip_fill_addr,
387408
.fill_buf_size = hisi_zip_fill_buf_size,
388409
.fill_buf_type = hisi_zip_fill_buf_type,
389410
.fill_req_type = hisi_zip_fill_req_type,
411+
.fill_win_size = hisi_zip_fill_win_size,
390412
.fill_tag = hisi_zip_fill_tag,
391413
.fill_sqe_type = hisi_zip_fill_sqe_type,
392414
.get_status = hisi_zip_get_status,
@@ -578,11 +600,12 @@ static void hisi_zip_acomp_exit(struct crypto_acomp *tfm)
578600
{
579601
struct hisi_zip_ctx *ctx = crypto_tfm_ctx(&tfm->base);
580602

581-
if (!ctx->fallback) {
582-
hisi_zip_release_sgl_pool(ctx);
583-
hisi_zip_release_req_q(ctx);
584-
hisi_zip_ctx_exit(ctx);
585-
}
603+
if (ctx->fallback)
604+
return;
605+
606+
hisi_zip_release_sgl_pool(ctx);
607+
hisi_zip_release_req_q(ctx);
608+
hisi_zip_ctx_exit(ctx);
586609
}
587610

588611
static struct acomp_alg hisi_zip_acomp_deflate = {
@@ -623,18 +646,69 @@ static void hisi_zip_unregister_deflate(struct hisi_qm *qm)
623646
crypto_unregister_acomp(&hisi_zip_acomp_deflate);
624647
}
625648

649+
static struct acomp_alg hisi_zip_acomp_lz4 = {
650+
.init = hisi_zip_acomp_init,
651+
.exit = hisi_zip_acomp_exit,
652+
.compress = hisi_zip_acompress,
653+
.decompress = hisi_zip_decompress,
654+
.base = {
655+
.cra_name = "lz4",
656+
.cra_driver_name = "hisi-lz4-acomp",
657+
.cra_flags = CRYPTO_ALG_ASYNC |
658+
CRYPTO_ALG_NEED_FALLBACK,
659+
.cra_module = THIS_MODULE,
660+
.cra_priority = HZIP_ALG_PRIORITY,
661+
.cra_ctxsize = sizeof(struct hisi_zip_ctx),
662+
}
663+
};
664+
665+
static int hisi_zip_register_lz4(struct hisi_qm *qm)
666+
{
667+
int ret;
668+
669+
if (!hisi_zip_alg_support(qm, HZIP_ALG_LZ4))
670+
return 0;
671+
672+
ret = crypto_register_acomp(&hisi_zip_acomp_lz4);
673+
if (ret)
674+
dev_err(&qm->pdev->dev, "failed to register to LZ4 (%d)!\n", ret);
675+
676+
return ret;
677+
}
678+
679+
static void hisi_zip_unregister_lz4(struct hisi_qm *qm)
680+
{
681+
if (!hisi_zip_alg_support(qm, HZIP_ALG_LZ4))
682+
return;
683+
684+
crypto_unregister_acomp(&hisi_zip_acomp_lz4);
685+
}
686+
626687
int hisi_zip_register_to_crypto(struct hisi_qm *qm)
627688
{
628689
int ret = 0;
629690

630691
mutex_lock(&zip_algs_lock);
631-
if (zip_available_devs++)
692+
if (zip_available_devs) {
693+
zip_available_devs++;
632694
goto unlock;
695+
}
633696

634697
ret = hisi_zip_register_deflate(qm);
635698
if (ret)
636-
zip_available_devs--;
699+
goto unlock;
700+
701+
ret = hisi_zip_register_lz4(qm);
702+
if (ret)
703+
goto unreg_deflate;
704+
705+
zip_available_devs++;
706+
mutex_unlock(&zip_algs_lock);
637707

708+
return 0;
709+
710+
unreg_deflate:
711+
hisi_zip_unregister_deflate(qm);
638712
unlock:
639713
mutex_unlock(&zip_algs_lock);
640714
return ret;
@@ -647,6 +721,7 @@ void hisi_zip_unregister_from_crypto(struct hisi_qm *qm)
647721
goto unlock;
648722

649723
hisi_zip_unregister_deflate(qm);
724+
hisi_zip_unregister_lz4(qm);
650725

651726
unlock:
652727
mutex_unlock(&zip_algs_lock);

0 commit comments

Comments
 (0)