xref: /OK3568_Linux_fs/kernel/drivers/crypto/mediatek/mtk-sha.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Cryptographic API.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Driver for EIP97 SHA1/SHA2(HMAC) acceleration.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (c) 2016 Ryder Lee <ryder.lee@mediatek.com>
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Some ideas are from atmel-sha.c and omap-sham.c drivers.
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <crypto/hmac.h>
13*4882a593Smuzhiyun #include <crypto/sha.h>
14*4882a593Smuzhiyun #include "mtk-platform.h"
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #define SHA_ALIGN_MSK		(sizeof(u32) - 1)
17*4882a593Smuzhiyun #define SHA_QUEUE_SIZE		512
18*4882a593Smuzhiyun #define SHA_BUF_SIZE		((u32)PAGE_SIZE)
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #define SHA_OP_UPDATE		1
21*4882a593Smuzhiyun #define SHA_OP_FINAL		2
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #define SHA_DATA_LEN_MSK	cpu_to_le32(GENMASK(16, 0))
24*4882a593Smuzhiyun #define SHA_MAX_DIGEST_BUF_SIZE	32
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun /* SHA command token */
27*4882a593Smuzhiyun #define SHA_CT_SIZE		5
28*4882a593Smuzhiyun #define SHA_CT_CTRL_HDR		cpu_to_le32(0x02220000)
29*4882a593Smuzhiyun #define SHA_CMD0		cpu_to_le32(0x03020000)
30*4882a593Smuzhiyun #define SHA_CMD1		cpu_to_le32(0x21060000)
31*4882a593Smuzhiyun #define SHA_CMD2		cpu_to_le32(0xe0e63802)
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /* SHA transform information */
34*4882a593Smuzhiyun #define SHA_TFM_HASH		cpu_to_le32(0x2 << 0)
35*4882a593Smuzhiyun #define SHA_TFM_SIZE(x)		cpu_to_le32((x) << 8)
36*4882a593Smuzhiyun #define SHA_TFM_START		cpu_to_le32(0x1 << 4)
37*4882a593Smuzhiyun #define SHA_TFM_CONTINUE	cpu_to_le32(0x1 << 5)
38*4882a593Smuzhiyun #define SHA_TFM_HASH_STORE	cpu_to_le32(0x1 << 19)
39*4882a593Smuzhiyun #define SHA_TFM_SHA1		cpu_to_le32(0x2 << 23)
40*4882a593Smuzhiyun #define SHA_TFM_SHA256		cpu_to_le32(0x3 << 23)
41*4882a593Smuzhiyun #define SHA_TFM_SHA224		cpu_to_le32(0x4 << 23)
42*4882a593Smuzhiyun #define SHA_TFM_SHA512		cpu_to_le32(0x5 << 23)
43*4882a593Smuzhiyun #define SHA_TFM_SHA384		cpu_to_le32(0x6 << 23)
44*4882a593Smuzhiyun #define SHA_TFM_DIGEST(x)	cpu_to_le32(((x) & GENMASK(3, 0)) << 24)
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun /* SHA flags */
47*4882a593Smuzhiyun #define SHA_FLAGS_BUSY		BIT(0)
48*4882a593Smuzhiyun #define	SHA_FLAGS_FINAL		BIT(1)
49*4882a593Smuzhiyun #define SHA_FLAGS_FINUP		BIT(2)
50*4882a593Smuzhiyun #define SHA_FLAGS_SG		BIT(3)
51*4882a593Smuzhiyun #define SHA_FLAGS_ALGO_MSK	GENMASK(8, 4)
52*4882a593Smuzhiyun #define SHA_FLAGS_SHA1		BIT(4)
53*4882a593Smuzhiyun #define SHA_FLAGS_SHA224	BIT(5)
54*4882a593Smuzhiyun #define SHA_FLAGS_SHA256	BIT(6)
55*4882a593Smuzhiyun #define SHA_FLAGS_SHA384	BIT(7)
56*4882a593Smuzhiyun #define SHA_FLAGS_SHA512	BIT(8)
57*4882a593Smuzhiyun #define SHA_FLAGS_HMAC		BIT(9)
58*4882a593Smuzhiyun #define SHA_FLAGS_PAD		BIT(10)
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun /**
61*4882a593Smuzhiyun  * mtk_sha_info - hardware information of AES
62*4882a593Smuzhiyun  * @cmd:	command token, hardware instruction
63*4882a593Smuzhiyun  * @tfm:	transform state of cipher algorithm.
64*4882a593Smuzhiyun  * @state:	contains keys and initial vectors.
65*4882a593Smuzhiyun  *
66*4882a593Smuzhiyun  */
67*4882a593Smuzhiyun struct mtk_sha_info {
68*4882a593Smuzhiyun 	__le32 ctrl[2];
69*4882a593Smuzhiyun 	__le32 cmd[3];
70*4882a593Smuzhiyun 	__le32 tfm[2];
71*4882a593Smuzhiyun 	__le32 digest[SHA_MAX_DIGEST_BUF_SIZE];
72*4882a593Smuzhiyun };
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun struct mtk_sha_reqctx {
75*4882a593Smuzhiyun 	struct mtk_sha_info info;
76*4882a593Smuzhiyun 	unsigned long flags;
77*4882a593Smuzhiyun 	unsigned long op;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	u64 digcnt;
80*4882a593Smuzhiyun 	size_t bufcnt;
81*4882a593Smuzhiyun 	dma_addr_t dma_addr;
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	__le32 ct_hdr;
84*4882a593Smuzhiyun 	u32 ct_size;
85*4882a593Smuzhiyun 	dma_addr_t ct_dma;
86*4882a593Smuzhiyun 	dma_addr_t tfm_dma;
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	/* Walk state */
89*4882a593Smuzhiyun 	struct scatterlist *sg;
90*4882a593Smuzhiyun 	u32 offset;	/* Offset in current sg */
91*4882a593Smuzhiyun 	u32 total;	/* Total request */
92*4882a593Smuzhiyun 	size_t ds;
93*4882a593Smuzhiyun 	size_t bs;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	u8 *buffer;
96*4882a593Smuzhiyun };
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun struct mtk_sha_hmac_ctx {
99*4882a593Smuzhiyun 	struct crypto_shash	*shash;
100*4882a593Smuzhiyun 	u8 ipad[SHA512_BLOCK_SIZE] __aligned(sizeof(u32));
101*4882a593Smuzhiyun 	u8 opad[SHA512_BLOCK_SIZE] __aligned(sizeof(u32));
102*4882a593Smuzhiyun };
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun struct mtk_sha_ctx {
105*4882a593Smuzhiyun 	struct mtk_cryp *cryp;
106*4882a593Smuzhiyun 	unsigned long flags;
107*4882a593Smuzhiyun 	u8 id;
108*4882a593Smuzhiyun 	u8 buf[SHA_BUF_SIZE] __aligned(sizeof(u32));
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	struct mtk_sha_hmac_ctx	base[];
111*4882a593Smuzhiyun };
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun struct mtk_sha_drv {
114*4882a593Smuzhiyun 	struct list_head dev_list;
115*4882a593Smuzhiyun 	/* Device list lock */
116*4882a593Smuzhiyun 	spinlock_t lock;
117*4882a593Smuzhiyun };
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun static struct mtk_sha_drv mtk_sha = {
120*4882a593Smuzhiyun 	.dev_list = LIST_HEAD_INIT(mtk_sha.dev_list),
121*4882a593Smuzhiyun 	.lock = __SPIN_LOCK_UNLOCKED(mtk_sha.lock),
122*4882a593Smuzhiyun };
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun static int mtk_sha_handle_queue(struct mtk_cryp *cryp, u8 id,
125*4882a593Smuzhiyun 				struct ahash_request *req);
126*4882a593Smuzhiyun 
mtk_sha_read(struct mtk_cryp * cryp,u32 offset)127*4882a593Smuzhiyun static inline u32 mtk_sha_read(struct mtk_cryp *cryp, u32 offset)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun 	return readl_relaxed(cryp->base + offset);
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun 
mtk_sha_write(struct mtk_cryp * cryp,u32 offset,u32 value)132*4882a593Smuzhiyun static inline void mtk_sha_write(struct mtk_cryp *cryp,
133*4882a593Smuzhiyun 				 u32 offset, u32 value)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun 	writel_relaxed(value, cryp->base + offset);
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun 
mtk_sha_ring_shift(struct mtk_ring * ring,struct mtk_desc ** cmd_curr,struct mtk_desc ** res_curr,int * count)138*4882a593Smuzhiyun static inline void mtk_sha_ring_shift(struct mtk_ring *ring,
139*4882a593Smuzhiyun 				      struct mtk_desc **cmd_curr,
140*4882a593Smuzhiyun 				      struct mtk_desc **res_curr,
141*4882a593Smuzhiyun 				      int *count)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun 	*cmd_curr = ring->cmd_next++;
144*4882a593Smuzhiyun 	*res_curr = ring->res_next++;
145*4882a593Smuzhiyun 	(*count)++;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	if (ring->cmd_next == ring->cmd_base + MTK_DESC_NUM) {
148*4882a593Smuzhiyun 		ring->cmd_next = ring->cmd_base;
149*4882a593Smuzhiyun 		ring->res_next = ring->res_base;
150*4882a593Smuzhiyun 	}
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun 
mtk_sha_find_dev(struct mtk_sha_ctx * tctx)153*4882a593Smuzhiyun static struct mtk_cryp *mtk_sha_find_dev(struct mtk_sha_ctx *tctx)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun 	struct mtk_cryp *cryp = NULL;
156*4882a593Smuzhiyun 	struct mtk_cryp *tmp;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	spin_lock_bh(&mtk_sha.lock);
159*4882a593Smuzhiyun 	if (!tctx->cryp) {
160*4882a593Smuzhiyun 		list_for_each_entry(tmp, &mtk_sha.dev_list, sha_list) {
161*4882a593Smuzhiyun 			cryp = tmp;
162*4882a593Smuzhiyun 			break;
163*4882a593Smuzhiyun 		}
164*4882a593Smuzhiyun 		tctx->cryp = cryp;
165*4882a593Smuzhiyun 	} else {
166*4882a593Smuzhiyun 		cryp = tctx->cryp;
167*4882a593Smuzhiyun 	}
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	/*
170*4882a593Smuzhiyun 	 * Assign record id to tfm in round-robin fashion, and this
171*4882a593Smuzhiyun 	 * will help tfm to bind  to corresponding descriptor rings.
172*4882a593Smuzhiyun 	 */
173*4882a593Smuzhiyun 	tctx->id = cryp->rec;
174*4882a593Smuzhiyun 	cryp->rec = !cryp->rec;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	spin_unlock_bh(&mtk_sha.lock);
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	return cryp;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun 
mtk_sha_append_sg(struct mtk_sha_reqctx * ctx)181*4882a593Smuzhiyun static int mtk_sha_append_sg(struct mtk_sha_reqctx *ctx)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun 	size_t count;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	while ((ctx->bufcnt < SHA_BUF_SIZE) && ctx->total) {
186*4882a593Smuzhiyun 		count = min(ctx->sg->length - ctx->offset, ctx->total);
187*4882a593Smuzhiyun 		count = min(count, SHA_BUF_SIZE - ctx->bufcnt);
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 		if (count <= 0) {
190*4882a593Smuzhiyun 			/*
191*4882a593Smuzhiyun 			 * Check if count <= 0 because the buffer is full or
192*4882a593Smuzhiyun 			 * because the sg length is 0. In the latest case,
193*4882a593Smuzhiyun 			 * check if there is another sg in the list, a 0 length
194*4882a593Smuzhiyun 			 * sg doesn't necessarily mean the end of the sg list.
195*4882a593Smuzhiyun 			 */
196*4882a593Smuzhiyun 			if ((ctx->sg->length == 0) && !sg_is_last(ctx->sg)) {
197*4882a593Smuzhiyun 				ctx->sg = sg_next(ctx->sg);
198*4882a593Smuzhiyun 				continue;
199*4882a593Smuzhiyun 			} else {
200*4882a593Smuzhiyun 				break;
201*4882a593Smuzhiyun 			}
202*4882a593Smuzhiyun 		}
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 		scatterwalk_map_and_copy(ctx->buffer + ctx->bufcnt, ctx->sg,
205*4882a593Smuzhiyun 					 ctx->offset, count, 0);
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 		ctx->bufcnt += count;
208*4882a593Smuzhiyun 		ctx->offset += count;
209*4882a593Smuzhiyun 		ctx->total -= count;
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 		if (ctx->offset == ctx->sg->length) {
212*4882a593Smuzhiyun 			ctx->sg = sg_next(ctx->sg);
213*4882a593Smuzhiyun 			if (ctx->sg)
214*4882a593Smuzhiyun 				ctx->offset = 0;
215*4882a593Smuzhiyun 			else
216*4882a593Smuzhiyun 				ctx->total = 0;
217*4882a593Smuzhiyun 		}
218*4882a593Smuzhiyun 	}
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	return 0;
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun /*
224*4882a593Smuzhiyun  * The purpose of this padding is to ensure that the padded message is a
225*4882a593Smuzhiyun  * multiple of 512 bits (SHA1/SHA224/SHA256) or 1024 bits (SHA384/SHA512).
226*4882a593Smuzhiyun  * The bit "1" is appended at the end of the message followed by
227*4882a593Smuzhiyun  * "padlen-1" zero bits. Then a 64 bits block (SHA1/SHA224/SHA256) or
228*4882a593Smuzhiyun  * 128 bits block (SHA384/SHA512) equals to the message length in bits
229*4882a593Smuzhiyun  * is appended.
230*4882a593Smuzhiyun  *
231*4882a593Smuzhiyun  * For SHA1/SHA224/SHA256, padlen is calculated as followed:
232*4882a593Smuzhiyun  *  - if message length < 56 bytes then padlen = 56 - message length
233*4882a593Smuzhiyun  *  - else padlen = 64 + 56 - message length
234*4882a593Smuzhiyun  *
235*4882a593Smuzhiyun  * For SHA384/SHA512, padlen is calculated as followed:
236*4882a593Smuzhiyun  *  - if message length < 112 bytes then padlen = 112 - message length
237*4882a593Smuzhiyun  *  - else padlen = 128 + 112 - message length
238*4882a593Smuzhiyun  */
mtk_sha_fill_padding(struct mtk_sha_reqctx * ctx,u32 len)239*4882a593Smuzhiyun static void mtk_sha_fill_padding(struct mtk_sha_reqctx *ctx, u32 len)
240*4882a593Smuzhiyun {
241*4882a593Smuzhiyun 	u32 index, padlen;
242*4882a593Smuzhiyun 	__be64 bits[2];
243*4882a593Smuzhiyun 	u64 size = ctx->digcnt;
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	size += ctx->bufcnt;
246*4882a593Smuzhiyun 	size += len;
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	bits[1] = cpu_to_be64(size << 3);
249*4882a593Smuzhiyun 	bits[0] = cpu_to_be64(size >> 61);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	switch (ctx->flags & SHA_FLAGS_ALGO_MSK) {
252*4882a593Smuzhiyun 	case SHA_FLAGS_SHA384:
253*4882a593Smuzhiyun 	case SHA_FLAGS_SHA512:
254*4882a593Smuzhiyun 		index = ctx->bufcnt & 0x7f;
255*4882a593Smuzhiyun 		padlen = (index < 112) ? (112 - index) : ((128 + 112) - index);
256*4882a593Smuzhiyun 		*(ctx->buffer + ctx->bufcnt) = 0x80;
257*4882a593Smuzhiyun 		memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen - 1);
258*4882a593Smuzhiyun 		memcpy(ctx->buffer + ctx->bufcnt + padlen, bits, 16);
259*4882a593Smuzhiyun 		ctx->bufcnt += padlen + 16;
260*4882a593Smuzhiyun 		ctx->flags |= SHA_FLAGS_PAD;
261*4882a593Smuzhiyun 		break;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	default:
264*4882a593Smuzhiyun 		index = ctx->bufcnt & 0x3f;
265*4882a593Smuzhiyun 		padlen = (index < 56) ? (56 - index) : ((64 + 56) - index);
266*4882a593Smuzhiyun 		*(ctx->buffer + ctx->bufcnt) = 0x80;
267*4882a593Smuzhiyun 		memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen - 1);
268*4882a593Smuzhiyun 		memcpy(ctx->buffer + ctx->bufcnt + padlen, &bits[1], 8);
269*4882a593Smuzhiyun 		ctx->bufcnt += padlen + 8;
270*4882a593Smuzhiyun 		ctx->flags |= SHA_FLAGS_PAD;
271*4882a593Smuzhiyun 		break;
272*4882a593Smuzhiyun 	}
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun /* Initialize basic transform information of SHA */
mtk_sha_info_init(struct mtk_sha_reqctx * ctx)276*4882a593Smuzhiyun static void mtk_sha_info_init(struct mtk_sha_reqctx *ctx)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun 	struct mtk_sha_info *info = &ctx->info;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	ctx->ct_hdr = SHA_CT_CTRL_HDR;
281*4882a593Smuzhiyun 	ctx->ct_size = SHA_CT_SIZE;
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	info->tfm[0] = SHA_TFM_HASH | SHA_TFM_SIZE(SIZE_IN_WORDS(ctx->ds));
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	switch (ctx->flags & SHA_FLAGS_ALGO_MSK) {
286*4882a593Smuzhiyun 	case SHA_FLAGS_SHA1:
287*4882a593Smuzhiyun 		info->tfm[0] |= SHA_TFM_SHA1;
288*4882a593Smuzhiyun 		break;
289*4882a593Smuzhiyun 	case SHA_FLAGS_SHA224:
290*4882a593Smuzhiyun 		info->tfm[0] |= SHA_TFM_SHA224;
291*4882a593Smuzhiyun 		break;
292*4882a593Smuzhiyun 	case SHA_FLAGS_SHA256:
293*4882a593Smuzhiyun 		info->tfm[0] |= SHA_TFM_SHA256;
294*4882a593Smuzhiyun 		break;
295*4882a593Smuzhiyun 	case SHA_FLAGS_SHA384:
296*4882a593Smuzhiyun 		info->tfm[0] |= SHA_TFM_SHA384;
297*4882a593Smuzhiyun 		break;
298*4882a593Smuzhiyun 	case SHA_FLAGS_SHA512:
299*4882a593Smuzhiyun 		info->tfm[0] |= SHA_TFM_SHA512;
300*4882a593Smuzhiyun 		break;
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	default:
303*4882a593Smuzhiyun 		/* Should not happen... */
304*4882a593Smuzhiyun 		return;
305*4882a593Smuzhiyun 	}
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	info->tfm[1] = SHA_TFM_HASH_STORE;
308*4882a593Smuzhiyun 	info->ctrl[0] = info->tfm[0] | SHA_TFM_CONTINUE | SHA_TFM_START;
309*4882a593Smuzhiyun 	info->ctrl[1] = info->tfm[1];
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	info->cmd[0] = SHA_CMD0;
312*4882a593Smuzhiyun 	info->cmd[1] = SHA_CMD1;
313*4882a593Smuzhiyun 	info->cmd[2] = SHA_CMD2 | SHA_TFM_DIGEST(SIZE_IN_WORDS(ctx->ds));
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun /*
317*4882a593Smuzhiyun  * Update input data length field of transform information and
318*4882a593Smuzhiyun  * map it to DMA region.
319*4882a593Smuzhiyun  */
mtk_sha_info_update(struct mtk_cryp * cryp,struct mtk_sha_rec * sha,size_t len1,size_t len2)320*4882a593Smuzhiyun static int mtk_sha_info_update(struct mtk_cryp *cryp,
321*4882a593Smuzhiyun 			       struct mtk_sha_rec *sha,
322*4882a593Smuzhiyun 			       size_t len1, size_t len2)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun 	struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req);
325*4882a593Smuzhiyun 	struct mtk_sha_info *info = &ctx->info;
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	ctx->ct_hdr &= ~SHA_DATA_LEN_MSK;
328*4882a593Smuzhiyun 	ctx->ct_hdr |= cpu_to_le32(len1 + len2);
329*4882a593Smuzhiyun 	info->cmd[0] &= ~SHA_DATA_LEN_MSK;
330*4882a593Smuzhiyun 	info->cmd[0] |= cpu_to_le32(len1 + len2);
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 	/* Setting SHA_TFM_START only for the first iteration */
333*4882a593Smuzhiyun 	if (ctx->digcnt)
334*4882a593Smuzhiyun 		info->ctrl[0] &= ~SHA_TFM_START;
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	ctx->digcnt += len1;
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	ctx->ct_dma = dma_map_single(cryp->dev, info, sizeof(*info),
339*4882a593Smuzhiyun 				     DMA_BIDIRECTIONAL);
340*4882a593Smuzhiyun 	if (unlikely(dma_mapping_error(cryp->dev, ctx->ct_dma))) {
341*4882a593Smuzhiyun 		dev_err(cryp->dev, "dma %zu bytes error\n", sizeof(*info));
342*4882a593Smuzhiyun 		return -EINVAL;
343*4882a593Smuzhiyun 	}
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	ctx->tfm_dma = ctx->ct_dma + sizeof(info->ctrl) + sizeof(info->cmd);
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	return 0;
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun /*
351*4882a593Smuzhiyun  * Because of hardware limitation, we must pre-calculate the inner
352*4882a593Smuzhiyun  * and outer digest that need to be processed firstly by engine, then
353*4882a593Smuzhiyun  * apply the result digest to the input message. These complex hashing
354*4882a593Smuzhiyun  * procedures limits HMAC performance, so we use fallback SW encoding.
355*4882a593Smuzhiyun  */
mtk_sha_finish_hmac(struct ahash_request * req)356*4882a593Smuzhiyun static int mtk_sha_finish_hmac(struct ahash_request *req)
357*4882a593Smuzhiyun {
358*4882a593Smuzhiyun 	struct mtk_sha_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
359*4882a593Smuzhiyun 	struct mtk_sha_hmac_ctx *bctx = tctx->base;
360*4882a593Smuzhiyun 	struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	SHASH_DESC_ON_STACK(shash, bctx->shash);
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	shash->tfm = bctx->shash;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	return crypto_shash_init(shash) ?:
367*4882a593Smuzhiyun 	       crypto_shash_update(shash, bctx->opad, ctx->bs) ?:
368*4882a593Smuzhiyun 	       crypto_shash_finup(shash, req->result, ctx->ds, req->result);
369*4882a593Smuzhiyun }
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun /* Initialize request context */
mtk_sha_init(struct ahash_request * req)372*4882a593Smuzhiyun static int mtk_sha_init(struct ahash_request *req)
373*4882a593Smuzhiyun {
374*4882a593Smuzhiyun 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
375*4882a593Smuzhiyun 	struct mtk_sha_ctx *tctx = crypto_ahash_ctx(tfm);
376*4882a593Smuzhiyun 	struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	ctx->flags = 0;
379*4882a593Smuzhiyun 	ctx->ds = crypto_ahash_digestsize(tfm);
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	switch (ctx->ds) {
382*4882a593Smuzhiyun 	case SHA1_DIGEST_SIZE:
383*4882a593Smuzhiyun 		ctx->flags |= SHA_FLAGS_SHA1;
384*4882a593Smuzhiyun 		ctx->bs = SHA1_BLOCK_SIZE;
385*4882a593Smuzhiyun 		break;
386*4882a593Smuzhiyun 	case SHA224_DIGEST_SIZE:
387*4882a593Smuzhiyun 		ctx->flags |= SHA_FLAGS_SHA224;
388*4882a593Smuzhiyun 		ctx->bs = SHA224_BLOCK_SIZE;
389*4882a593Smuzhiyun 		break;
390*4882a593Smuzhiyun 	case SHA256_DIGEST_SIZE:
391*4882a593Smuzhiyun 		ctx->flags |= SHA_FLAGS_SHA256;
392*4882a593Smuzhiyun 		ctx->bs = SHA256_BLOCK_SIZE;
393*4882a593Smuzhiyun 		break;
394*4882a593Smuzhiyun 	case SHA384_DIGEST_SIZE:
395*4882a593Smuzhiyun 		ctx->flags |= SHA_FLAGS_SHA384;
396*4882a593Smuzhiyun 		ctx->bs = SHA384_BLOCK_SIZE;
397*4882a593Smuzhiyun 		break;
398*4882a593Smuzhiyun 	case SHA512_DIGEST_SIZE:
399*4882a593Smuzhiyun 		ctx->flags |= SHA_FLAGS_SHA512;
400*4882a593Smuzhiyun 		ctx->bs = SHA512_BLOCK_SIZE;
401*4882a593Smuzhiyun 		break;
402*4882a593Smuzhiyun 	default:
403*4882a593Smuzhiyun 		return -EINVAL;
404*4882a593Smuzhiyun 	}
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	ctx->bufcnt = 0;
407*4882a593Smuzhiyun 	ctx->digcnt = 0;
408*4882a593Smuzhiyun 	ctx->buffer = tctx->buf;
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	if (tctx->flags & SHA_FLAGS_HMAC) {
411*4882a593Smuzhiyun 		struct mtk_sha_hmac_ctx *bctx = tctx->base;
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 		memcpy(ctx->buffer, bctx->ipad, ctx->bs);
414*4882a593Smuzhiyun 		ctx->bufcnt = ctx->bs;
415*4882a593Smuzhiyun 		ctx->flags |= SHA_FLAGS_HMAC;
416*4882a593Smuzhiyun 	}
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	return 0;
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun 
mtk_sha_xmit(struct mtk_cryp * cryp,struct mtk_sha_rec * sha,dma_addr_t addr1,size_t len1,dma_addr_t addr2,size_t len2)421*4882a593Smuzhiyun static int mtk_sha_xmit(struct mtk_cryp *cryp, struct mtk_sha_rec *sha,
422*4882a593Smuzhiyun 			dma_addr_t addr1, size_t len1,
423*4882a593Smuzhiyun 			dma_addr_t addr2, size_t len2)
424*4882a593Smuzhiyun {
425*4882a593Smuzhiyun 	struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req);
426*4882a593Smuzhiyun 	struct mtk_ring *ring = cryp->ring[sha->id];
427*4882a593Smuzhiyun 	struct mtk_desc *cmd, *res;
428*4882a593Smuzhiyun 	int err, count = 0;
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	err = mtk_sha_info_update(cryp, sha, len1, len2);
431*4882a593Smuzhiyun 	if (err)
432*4882a593Smuzhiyun 		return err;
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	/* Fill in the command/result descriptors */
435*4882a593Smuzhiyun 	mtk_sha_ring_shift(ring, &cmd, &res, &count);
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	res->hdr = MTK_DESC_FIRST | MTK_DESC_BUF_LEN(len1);
438*4882a593Smuzhiyun 	cmd->hdr = MTK_DESC_FIRST | MTK_DESC_BUF_LEN(len1) |
439*4882a593Smuzhiyun 		   MTK_DESC_CT_LEN(ctx->ct_size);
440*4882a593Smuzhiyun 	cmd->buf = cpu_to_le32(addr1);
441*4882a593Smuzhiyun 	cmd->ct = cpu_to_le32(ctx->ct_dma);
442*4882a593Smuzhiyun 	cmd->ct_hdr = ctx->ct_hdr;
443*4882a593Smuzhiyun 	cmd->tfm = cpu_to_le32(ctx->tfm_dma);
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	if (len2) {
446*4882a593Smuzhiyun 		mtk_sha_ring_shift(ring, &cmd, &res, &count);
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 		res->hdr = MTK_DESC_BUF_LEN(len2);
449*4882a593Smuzhiyun 		cmd->hdr = MTK_DESC_BUF_LEN(len2);
450*4882a593Smuzhiyun 		cmd->buf = cpu_to_le32(addr2);
451*4882a593Smuzhiyun 	}
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	cmd->hdr |= MTK_DESC_LAST;
454*4882a593Smuzhiyun 	res->hdr |= MTK_DESC_LAST;
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 	/*
457*4882a593Smuzhiyun 	 * Make sure that all changes to the DMA ring are done before we
458*4882a593Smuzhiyun 	 * start engine.
459*4882a593Smuzhiyun 	 */
460*4882a593Smuzhiyun 	wmb();
461*4882a593Smuzhiyun 	/* Start DMA transfer */
462*4882a593Smuzhiyun 	mtk_sha_write(cryp, RDR_PREP_COUNT(sha->id), MTK_DESC_CNT(count));
463*4882a593Smuzhiyun 	mtk_sha_write(cryp, CDR_PREP_COUNT(sha->id), MTK_DESC_CNT(count));
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	return -EINPROGRESS;
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun 
mtk_sha_dma_map(struct mtk_cryp * cryp,struct mtk_sha_rec * sha,struct mtk_sha_reqctx * ctx,size_t count)468*4882a593Smuzhiyun static int mtk_sha_dma_map(struct mtk_cryp *cryp,
469*4882a593Smuzhiyun 			   struct mtk_sha_rec *sha,
470*4882a593Smuzhiyun 			   struct mtk_sha_reqctx *ctx,
471*4882a593Smuzhiyun 			   size_t count)
472*4882a593Smuzhiyun {
473*4882a593Smuzhiyun 	ctx->dma_addr = dma_map_single(cryp->dev, ctx->buffer,
474*4882a593Smuzhiyun 				       SHA_BUF_SIZE, DMA_TO_DEVICE);
475*4882a593Smuzhiyun 	if (unlikely(dma_mapping_error(cryp->dev, ctx->dma_addr))) {
476*4882a593Smuzhiyun 		dev_err(cryp->dev, "dma map error\n");
477*4882a593Smuzhiyun 		return -EINVAL;
478*4882a593Smuzhiyun 	}
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 	ctx->flags &= ~SHA_FLAGS_SG;
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	return mtk_sha_xmit(cryp, sha, ctx->dma_addr, count, 0, 0);
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun 
mtk_sha_update_slow(struct mtk_cryp * cryp,struct mtk_sha_rec * sha)485*4882a593Smuzhiyun static int mtk_sha_update_slow(struct mtk_cryp *cryp,
486*4882a593Smuzhiyun 			       struct mtk_sha_rec *sha)
487*4882a593Smuzhiyun {
488*4882a593Smuzhiyun 	struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req);
489*4882a593Smuzhiyun 	size_t count;
490*4882a593Smuzhiyun 	u32 final;
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 	mtk_sha_append_sg(ctx);
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun 	dev_dbg(cryp->dev, "slow: bufcnt: %zu\n", ctx->bufcnt);
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 	if (final) {
499*4882a593Smuzhiyun 		sha->flags |= SHA_FLAGS_FINAL;
500*4882a593Smuzhiyun 		mtk_sha_fill_padding(ctx, 0);
501*4882a593Smuzhiyun 	}
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	if (final || (ctx->bufcnt == SHA_BUF_SIZE && ctx->total)) {
504*4882a593Smuzhiyun 		count = ctx->bufcnt;
505*4882a593Smuzhiyun 		ctx->bufcnt = 0;
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 		return mtk_sha_dma_map(cryp, sha, ctx, count);
508*4882a593Smuzhiyun 	}
509*4882a593Smuzhiyun 	return 0;
510*4882a593Smuzhiyun }
511*4882a593Smuzhiyun 
mtk_sha_update_start(struct mtk_cryp * cryp,struct mtk_sha_rec * sha)512*4882a593Smuzhiyun static int mtk_sha_update_start(struct mtk_cryp *cryp,
513*4882a593Smuzhiyun 				struct mtk_sha_rec *sha)
514*4882a593Smuzhiyun {
515*4882a593Smuzhiyun 	struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req);
516*4882a593Smuzhiyun 	u32 len, final, tail;
517*4882a593Smuzhiyun 	struct scatterlist *sg;
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun 	if (!ctx->total)
520*4882a593Smuzhiyun 		return 0;
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 	if (ctx->bufcnt || ctx->offset)
523*4882a593Smuzhiyun 		return mtk_sha_update_slow(cryp, sha);
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 	sg = ctx->sg;
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	if (!IS_ALIGNED(sg->offset, sizeof(u32)))
528*4882a593Smuzhiyun 		return mtk_sha_update_slow(cryp, sha);
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	if (!sg_is_last(sg) && !IS_ALIGNED(sg->length, ctx->bs))
531*4882a593Smuzhiyun 		/* size is not ctx->bs aligned */
532*4882a593Smuzhiyun 		return mtk_sha_update_slow(cryp, sha);
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	len = min(ctx->total, sg->length);
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	if (sg_is_last(sg)) {
537*4882a593Smuzhiyun 		if (!(ctx->flags & SHA_FLAGS_FINUP)) {
538*4882a593Smuzhiyun 			/* not last sg must be ctx->bs aligned */
539*4882a593Smuzhiyun 			tail = len & (ctx->bs - 1);
540*4882a593Smuzhiyun 			len -= tail;
541*4882a593Smuzhiyun 		}
542*4882a593Smuzhiyun 	}
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun 	ctx->total -= len;
545*4882a593Smuzhiyun 	ctx->offset = len; /* offset where to start slow */
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 	final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	/* Add padding */
550*4882a593Smuzhiyun 	if (final) {
551*4882a593Smuzhiyun 		size_t count;
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun 		tail = len & (ctx->bs - 1);
554*4882a593Smuzhiyun 		len -= tail;
555*4882a593Smuzhiyun 		ctx->total += tail;
556*4882a593Smuzhiyun 		ctx->offset = len; /* offset where to start slow */
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun 		sg = ctx->sg;
559*4882a593Smuzhiyun 		mtk_sha_append_sg(ctx);
560*4882a593Smuzhiyun 		mtk_sha_fill_padding(ctx, len);
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 		ctx->dma_addr = dma_map_single(cryp->dev, ctx->buffer,
563*4882a593Smuzhiyun 					       SHA_BUF_SIZE, DMA_TO_DEVICE);
564*4882a593Smuzhiyun 		if (unlikely(dma_mapping_error(cryp->dev, ctx->dma_addr))) {
565*4882a593Smuzhiyun 			dev_err(cryp->dev, "dma map bytes error\n");
566*4882a593Smuzhiyun 			return -EINVAL;
567*4882a593Smuzhiyun 		}
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 		sha->flags |= SHA_FLAGS_FINAL;
570*4882a593Smuzhiyun 		count = ctx->bufcnt;
571*4882a593Smuzhiyun 		ctx->bufcnt = 0;
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 		if (len == 0) {
574*4882a593Smuzhiyun 			ctx->flags &= ~SHA_FLAGS_SG;
575*4882a593Smuzhiyun 			return mtk_sha_xmit(cryp, sha, ctx->dma_addr,
576*4882a593Smuzhiyun 					    count, 0, 0);
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun 		} else {
579*4882a593Smuzhiyun 			ctx->sg = sg;
580*4882a593Smuzhiyun 			if (!dma_map_sg(cryp->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
581*4882a593Smuzhiyun 				dev_err(cryp->dev, "dma_map_sg error\n");
582*4882a593Smuzhiyun 				return -EINVAL;
583*4882a593Smuzhiyun 			}
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 			ctx->flags |= SHA_FLAGS_SG;
586*4882a593Smuzhiyun 			return mtk_sha_xmit(cryp, sha, sg_dma_address(ctx->sg),
587*4882a593Smuzhiyun 					    len, ctx->dma_addr, count);
588*4882a593Smuzhiyun 		}
589*4882a593Smuzhiyun 	}
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun 	if (!dma_map_sg(cryp->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
592*4882a593Smuzhiyun 		dev_err(cryp->dev, "dma_map_sg  error\n");
593*4882a593Smuzhiyun 		return -EINVAL;
594*4882a593Smuzhiyun 	}
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 	ctx->flags |= SHA_FLAGS_SG;
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun 	return mtk_sha_xmit(cryp, sha, sg_dma_address(ctx->sg),
599*4882a593Smuzhiyun 			    len, 0, 0);
600*4882a593Smuzhiyun }
601*4882a593Smuzhiyun 
mtk_sha_final_req(struct mtk_cryp * cryp,struct mtk_sha_rec * sha)602*4882a593Smuzhiyun static int mtk_sha_final_req(struct mtk_cryp *cryp,
603*4882a593Smuzhiyun 			     struct mtk_sha_rec *sha)
604*4882a593Smuzhiyun {
605*4882a593Smuzhiyun 	struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req);
606*4882a593Smuzhiyun 	size_t count;
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	mtk_sha_fill_padding(ctx, 0);
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 	sha->flags |= SHA_FLAGS_FINAL;
611*4882a593Smuzhiyun 	count = ctx->bufcnt;
612*4882a593Smuzhiyun 	ctx->bufcnt = 0;
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 	return mtk_sha_dma_map(cryp, sha, ctx, count);
615*4882a593Smuzhiyun }
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun /* Copy ready hash (+ finalize hmac) */
mtk_sha_finish(struct ahash_request * req)618*4882a593Smuzhiyun static int mtk_sha_finish(struct ahash_request *req)
619*4882a593Smuzhiyun {
620*4882a593Smuzhiyun 	struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
621*4882a593Smuzhiyun 	__le32 *digest = ctx->info.digest;
622*4882a593Smuzhiyun 	u32 *result = (u32 *)req->result;
623*4882a593Smuzhiyun 	int i;
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun 	/* Get the hash from the digest buffer */
626*4882a593Smuzhiyun 	for (i = 0; i < SIZE_IN_WORDS(ctx->ds); i++)
627*4882a593Smuzhiyun 		result[i] = le32_to_cpu(digest[i]);
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	if (ctx->flags & SHA_FLAGS_HMAC)
630*4882a593Smuzhiyun 		return mtk_sha_finish_hmac(req);
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 	return 0;
633*4882a593Smuzhiyun }
634*4882a593Smuzhiyun 
mtk_sha_finish_req(struct mtk_cryp * cryp,struct mtk_sha_rec * sha,int err)635*4882a593Smuzhiyun static void mtk_sha_finish_req(struct mtk_cryp *cryp,
636*4882a593Smuzhiyun 			       struct mtk_sha_rec *sha,
637*4882a593Smuzhiyun 			       int err)
638*4882a593Smuzhiyun {
639*4882a593Smuzhiyun 	if (likely(!err && (SHA_FLAGS_FINAL & sha->flags)))
640*4882a593Smuzhiyun 		err = mtk_sha_finish(sha->req);
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 	sha->flags &= ~(SHA_FLAGS_BUSY | SHA_FLAGS_FINAL);
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	sha->req->base.complete(&sha->req->base, err);
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun 	/* Handle new request */
647*4882a593Smuzhiyun 	tasklet_schedule(&sha->queue_task);
648*4882a593Smuzhiyun }
649*4882a593Smuzhiyun 
mtk_sha_handle_queue(struct mtk_cryp * cryp,u8 id,struct ahash_request * req)650*4882a593Smuzhiyun static int mtk_sha_handle_queue(struct mtk_cryp *cryp, u8 id,
651*4882a593Smuzhiyun 				struct ahash_request *req)
652*4882a593Smuzhiyun {
653*4882a593Smuzhiyun 	struct mtk_sha_rec *sha = cryp->sha[id];
654*4882a593Smuzhiyun 	struct crypto_async_request *async_req, *backlog;
655*4882a593Smuzhiyun 	struct mtk_sha_reqctx *ctx;
656*4882a593Smuzhiyun 	unsigned long flags;
657*4882a593Smuzhiyun 	int err = 0, ret = 0;
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	spin_lock_irqsave(&sha->lock, flags);
660*4882a593Smuzhiyun 	if (req)
661*4882a593Smuzhiyun 		ret = ahash_enqueue_request(&sha->queue, req);
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	if (SHA_FLAGS_BUSY & sha->flags) {
664*4882a593Smuzhiyun 		spin_unlock_irqrestore(&sha->lock, flags);
665*4882a593Smuzhiyun 		return ret;
666*4882a593Smuzhiyun 	}
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	backlog = crypto_get_backlog(&sha->queue);
669*4882a593Smuzhiyun 	async_req = crypto_dequeue_request(&sha->queue);
670*4882a593Smuzhiyun 	if (async_req)
671*4882a593Smuzhiyun 		sha->flags |= SHA_FLAGS_BUSY;
672*4882a593Smuzhiyun 	spin_unlock_irqrestore(&sha->lock, flags);
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 	if (!async_req)
675*4882a593Smuzhiyun 		return ret;
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	if (backlog)
678*4882a593Smuzhiyun 		backlog->complete(backlog, -EINPROGRESS);
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	req = ahash_request_cast(async_req);
681*4882a593Smuzhiyun 	ctx = ahash_request_ctx(req);
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun 	sha->req = req;
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun 	mtk_sha_info_init(ctx);
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 	if (ctx->op == SHA_OP_UPDATE) {
688*4882a593Smuzhiyun 		err = mtk_sha_update_start(cryp, sha);
689*4882a593Smuzhiyun 		if (err != -EINPROGRESS && (ctx->flags & SHA_FLAGS_FINUP))
690*4882a593Smuzhiyun 			/* No final() after finup() */
691*4882a593Smuzhiyun 			err = mtk_sha_final_req(cryp, sha);
692*4882a593Smuzhiyun 	} else if (ctx->op == SHA_OP_FINAL) {
693*4882a593Smuzhiyun 		err = mtk_sha_final_req(cryp, sha);
694*4882a593Smuzhiyun 	}
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	if (unlikely(err != -EINPROGRESS))
697*4882a593Smuzhiyun 		/* Task will not finish it, so do it here */
698*4882a593Smuzhiyun 		mtk_sha_finish_req(cryp, sha, err);
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun 	return ret;
701*4882a593Smuzhiyun }
702*4882a593Smuzhiyun 
mtk_sha_enqueue(struct ahash_request * req,u32 op)703*4882a593Smuzhiyun static int mtk_sha_enqueue(struct ahash_request *req, u32 op)
704*4882a593Smuzhiyun {
705*4882a593Smuzhiyun 	struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
706*4882a593Smuzhiyun 	struct mtk_sha_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun 	ctx->op = op;
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun 	return mtk_sha_handle_queue(tctx->cryp, tctx->id, req);
711*4882a593Smuzhiyun }
712*4882a593Smuzhiyun 
mtk_sha_unmap(struct mtk_cryp * cryp,struct mtk_sha_rec * sha)713*4882a593Smuzhiyun static void mtk_sha_unmap(struct mtk_cryp *cryp, struct mtk_sha_rec *sha)
714*4882a593Smuzhiyun {
715*4882a593Smuzhiyun 	struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req);
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun 	dma_unmap_single(cryp->dev, ctx->ct_dma, sizeof(ctx->info),
718*4882a593Smuzhiyun 			 DMA_BIDIRECTIONAL);
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun 	if (ctx->flags & SHA_FLAGS_SG) {
721*4882a593Smuzhiyun 		dma_unmap_sg(cryp->dev, ctx->sg, 1, DMA_TO_DEVICE);
722*4882a593Smuzhiyun 		if (ctx->sg->length == ctx->offset) {
723*4882a593Smuzhiyun 			ctx->sg = sg_next(ctx->sg);
724*4882a593Smuzhiyun 			if (ctx->sg)
725*4882a593Smuzhiyun 				ctx->offset = 0;
726*4882a593Smuzhiyun 		}
727*4882a593Smuzhiyun 		if (ctx->flags & SHA_FLAGS_PAD) {
728*4882a593Smuzhiyun 			dma_unmap_single(cryp->dev, ctx->dma_addr,
729*4882a593Smuzhiyun 					 SHA_BUF_SIZE, DMA_TO_DEVICE);
730*4882a593Smuzhiyun 		}
731*4882a593Smuzhiyun 	} else
732*4882a593Smuzhiyun 		dma_unmap_single(cryp->dev, ctx->dma_addr,
733*4882a593Smuzhiyun 				 SHA_BUF_SIZE, DMA_TO_DEVICE);
734*4882a593Smuzhiyun }
735*4882a593Smuzhiyun 
mtk_sha_complete(struct mtk_cryp * cryp,struct mtk_sha_rec * sha)736*4882a593Smuzhiyun static void mtk_sha_complete(struct mtk_cryp *cryp,
737*4882a593Smuzhiyun 			     struct mtk_sha_rec *sha)
738*4882a593Smuzhiyun {
739*4882a593Smuzhiyun 	int err = 0;
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	err = mtk_sha_update_start(cryp, sha);
742*4882a593Smuzhiyun 	if (err != -EINPROGRESS)
743*4882a593Smuzhiyun 		mtk_sha_finish_req(cryp, sha, err);
744*4882a593Smuzhiyun }
745*4882a593Smuzhiyun 
mtk_sha_update(struct ahash_request * req)746*4882a593Smuzhiyun static int mtk_sha_update(struct ahash_request *req)
747*4882a593Smuzhiyun {
748*4882a593Smuzhiyun 	struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	ctx->total = req->nbytes;
751*4882a593Smuzhiyun 	ctx->sg = req->src;
752*4882a593Smuzhiyun 	ctx->offset = 0;
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 	if ((ctx->bufcnt + ctx->total < SHA_BUF_SIZE) &&
755*4882a593Smuzhiyun 	    !(ctx->flags & SHA_FLAGS_FINUP))
756*4882a593Smuzhiyun 		return mtk_sha_append_sg(ctx);
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun 	return mtk_sha_enqueue(req, SHA_OP_UPDATE);
759*4882a593Smuzhiyun }
760*4882a593Smuzhiyun 
mtk_sha_final(struct ahash_request * req)761*4882a593Smuzhiyun static int mtk_sha_final(struct ahash_request *req)
762*4882a593Smuzhiyun {
763*4882a593Smuzhiyun 	struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	ctx->flags |= SHA_FLAGS_FINUP;
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 	if (ctx->flags & SHA_FLAGS_PAD)
768*4882a593Smuzhiyun 		return mtk_sha_finish(req);
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 	return mtk_sha_enqueue(req, SHA_OP_FINAL);
771*4882a593Smuzhiyun }
772*4882a593Smuzhiyun 
mtk_sha_finup(struct ahash_request * req)773*4882a593Smuzhiyun static int mtk_sha_finup(struct ahash_request *req)
774*4882a593Smuzhiyun {
775*4882a593Smuzhiyun 	struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
776*4882a593Smuzhiyun 	int err1, err2;
777*4882a593Smuzhiyun 
778*4882a593Smuzhiyun 	ctx->flags |= SHA_FLAGS_FINUP;
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun 	err1 = mtk_sha_update(req);
781*4882a593Smuzhiyun 	if (err1 == -EINPROGRESS ||
782*4882a593Smuzhiyun 	    (err1 == -EBUSY && (ahash_request_flags(req) &
783*4882a593Smuzhiyun 				CRYPTO_TFM_REQ_MAY_BACKLOG)))
784*4882a593Smuzhiyun 		return err1;
785*4882a593Smuzhiyun 	/*
786*4882a593Smuzhiyun 	 * final() has to be always called to cleanup resources
787*4882a593Smuzhiyun 	 * even if update() failed
788*4882a593Smuzhiyun 	 */
789*4882a593Smuzhiyun 	err2 = mtk_sha_final(req);
790*4882a593Smuzhiyun 
791*4882a593Smuzhiyun 	return err1 ?: err2;
792*4882a593Smuzhiyun }
793*4882a593Smuzhiyun 
mtk_sha_digest(struct ahash_request * req)794*4882a593Smuzhiyun static int mtk_sha_digest(struct ahash_request *req)
795*4882a593Smuzhiyun {
796*4882a593Smuzhiyun 	return mtk_sha_init(req) ?: mtk_sha_finup(req);
797*4882a593Smuzhiyun }
798*4882a593Smuzhiyun 
mtk_sha_setkey(struct crypto_ahash * tfm,const u8 * key,u32 keylen)799*4882a593Smuzhiyun static int mtk_sha_setkey(struct crypto_ahash *tfm, const u8 *key,
800*4882a593Smuzhiyun 			  u32 keylen)
801*4882a593Smuzhiyun {
802*4882a593Smuzhiyun 	struct mtk_sha_ctx *tctx = crypto_ahash_ctx(tfm);
803*4882a593Smuzhiyun 	struct mtk_sha_hmac_ctx *bctx = tctx->base;
804*4882a593Smuzhiyun 	size_t bs = crypto_shash_blocksize(bctx->shash);
805*4882a593Smuzhiyun 	size_t ds = crypto_shash_digestsize(bctx->shash);
806*4882a593Smuzhiyun 	int err, i;
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 	if (keylen > bs) {
809*4882a593Smuzhiyun 		err = crypto_shash_tfm_digest(bctx->shash, key, keylen,
810*4882a593Smuzhiyun 					      bctx->ipad);
811*4882a593Smuzhiyun 		if (err)
812*4882a593Smuzhiyun 			return err;
813*4882a593Smuzhiyun 		keylen = ds;
814*4882a593Smuzhiyun 	} else {
815*4882a593Smuzhiyun 		memcpy(bctx->ipad, key, keylen);
816*4882a593Smuzhiyun 	}
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun 	memset(bctx->ipad + keylen, 0, bs - keylen);
819*4882a593Smuzhiyun 	memcpy(bctx->opad, bctx->ipad, bs);
820*4882a593Smuzhiyun 
821*4882a593Smuzhiyun 	for (i = 0; i < bs; i++) {
822*4882a593Smuzhiyun 		bctx->ipad[i] ^= HMAC_IPAD_VALUE;
823*4882a593Smuzhiyun 		bctx->opad[i] ^= HMAC_OPAD_VALUE;
824*4882a593Smuzhiyun 	}
825*4882a593Smuzhiyun 
826*4882a593Smuzhiyun 	return 0;
827*4882a593Smuzhiyun }
828*4882a593Smuzhiyun 
mtk_sha_export(struct ahash_request * req,void * out)829*4882a593Smuzhiyun static int mtk_sha_export(struct ahash_request *req, void *out)
830*4882a593Smuzhiyun {
831*4882a593Smuzhiyun 	const struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun 	memcpy(out, ctx, sizeof(*ctx));
834*4882a593Smuzhiyun 	return 0;
835*4882a593Smuzhiyun }
836*4882a593Smuzhiyun 
mtk_sha_import(struct ahash_request * req,const void * in)837*4882a593Smuzhiyun static int mtk_sha_import(struct ahash_request *req, const void *in)
838*4882a593Smuzhiyun {
839*4882a593Smuzhiyun 	struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	memcpy(ctx, in, sizeof(*ctx));
842*4882a593Smuzhiyun 	return 0;
843*4882a593Smuzhiyun }
844*4882a593Smuzhiyun 
mtk_sha_cra_init_alg(struct crypto_tfm * tfm,const char * alg_base)845*4882a593Smuzhiyun static int mtk_sha_cra_init_alg(struct crypto_tfm *tfm,
846*4882a593Smuzhiyun 				const char *alg_base)
847*4882a593Smuzhiyun {
848*4882a593Smuzhiyun 	struct mtk_sha_ctx *tctx = crypto_tfm_ctx(tfm);
849*4882a593Smuzhiyun 	struct mtk_cryp *cryp = NULL;
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun 	cryp = mtk_sha_find_dev(tctx);
852*4882a593Smuzhiyun 	if (!cryp)
853*4882a593Smuzhiyun 		return -ENODEV;
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
856*4882a593Smuzhiyun 				 sizeof(struct mtk_sha_reqctx));
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun 	if (alg_base) {
859*4882a593Smuzhiyun 		struct mtk_sha_hmac_ctx *bctx = tctx->base;
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun 		tctx->flags |= SHA_FLAGS_HMAC;
862*4882a593Smuzhiyun 		bctx->shash = crypto_alloc_shash(alg_base, 0,
863*4882a593Smuzhiyun 					CRYPTO_ALG_NEED_FALLBACK);
864*4882a593Smuzhiyun 		if (IS_ERR(bctx->shash)) {
865*4882a593Smuzhiyun 			pr_err("base driver %s could not be loaded.\n",
866*4882a593Smuzhiyun 			       alg_base);
867*4882a593Smuzhiyun 
868*4882a593Smuzhiyun 			return PTR_ERR(bctx->shash);
869*4882a593Smuzhiyun 		}
870*4882a593Smuzhiyun 	}
871*4882a593Smuzhiyun 	return 0;
872*4882a593Smuzhiyun }
873*4882a593Smuzhiyun 
mtk_sha_cra_init(struct crypto_tfm * tfm)874*4882a593Smuzhiyun static int mtk_sha_cra_init(struct crypto_tfm *tfm)
875*4882a593Smuzhiyun {
876*4882a593Smuzhiyun 	return mtk_sha_cra_init_alg(tfm, NULL);
877*4882a593Smuzhiyun }
878*4882a593Smuzhiyun 
mtk_sha_cra_sha1_init(struct crypto_tfm * tfm)879*4882a593Smuzhiyun static int mtk_sha_cra_sha1_init(struct crypto_tfm *tfm)
880*4882a593Smuzhiyun {
881*4882a593Smuzhiyun 	return mtk_sha_cra_init_alg(tfm, "sha1");
882*4882a593Smuzhiyun }
883*4882a593Smuzhiyun 
mtk_sha_cra_sha224_init(struct crypto_tfm * tfm)884*4882a593Smuzhiyun static int mtk_sha_cra_sha224_init(struct crypto_tfm *tfm)
885*4882a593Smuzhiyun {
886*4882a593Smuzhiyun 	return mtk_sha_cra_init_alg(tfm, "sha224");
887*4882a593Smuzhiyun }
888*4882a593Smuzhiyun 
mtk_sha_cra_sha256_init(struct crypto_tfm * tfm)889*4882a593Smuzhiyun static int mtk_sha_cra_sha256_init(struct crypto_tfm *tfm)
890*4882a593Smuzhiyun {
891*4882a593Smuzhiyun 	return mtk_sha_cra_init_alg(tfm, "sha256");
892*4882a593Smuzhiyun }
893*4882a593Smuzhiyun 
mtk_sha_cra_sha384_init(struct crypto_tfm * tfm)894*4882a593Smuzhiyun static int mtk_sha_cra_sha384_init(struct crypto_tfm *tfm)
895*4882a593Smuzhiyun {
896*4882a593Smuzhiyun 	return mtk_sha_cra_init_alg(tfm, "sha384");
897*4882a593Smuzhiyun }
898*4882a593Smuzhiyun 
mtk_sha_cra_sha512_init(struct crypto_tfm * tfm)899*4882a593Smuzhiyun static int mtk_sha_cra_sha512_init(struct crypto_tfm *tfm)
900*4882a593Smuzhiyun {
901*4882a593Smuzhiyun 	return mtk_sha_cra_init_alg(tfm, "sha512");
902*4882a593Smuzhiyun }
903*4882a593Smuzhiyun 
mtk_sha_cra_exit(struct crypto_tfm * tfm)904*4882a593Smuzhiyun static void mtk_sha_cra_exit(struct crypto_tfm *tfm)
905*4882a593Smuzhiyun {
906*4882a593Smuzhiyun 	struct mtk_sha_ctx *tctx = crypto_tfm_ctx(tfm);
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 	if (tctx->flags & SHA_FLAGS_HMAC) {
909*4882a593Smuzhiyun 		struct mtk_sha_hmac_ctx *bctx = tctx->base;
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 		crypto_free_shash(bctx->shash);
912*4882a593Smuzhiyun 	}
913*4882a593Smuzhiyun }
914*4882a593Smuzhiyun 
915*4882a593Smuzhiyun static struct ahash_alg algs_sha1_sha224_sha256[] = {
916*4882a593Smuzhiyun {
917*4882a593Smuzhiyun 	.init		= mtk_sha_init,
918*4882a593Smuzhiyun 	.update		= mtk_sha_update,
919*4882a593Smuzhiyun 	.final		= mtk_sha_final,
920*4882a593Smuzhiyun 	.finup		= mtk_sha_finup,
921*4882a593Smuzhiyun 	.digest		= mtk_sha_digest,
922*4882a593Smuzhiyun 	.export		= mtk_sha_export,
923*4882a593Smuzhiyun 	.import		= mtk_sha_import,
924*4882a593Smuzhiyun 	.halg.digestsize	= SHA1_DIGEST_SIZE,
925*4882a593Smuzhiyun 	.halg.statesize = sizeof(struct mtk_sha_reqctx),
926*4882a593Smuzhiyun 	.halg.base	= {
927*4882a593Smuzhiyun 		.cra_name		= "sha1",
928*4882a593Smuzhiyun 		.cra_driver_name	= "mtk-sha1",
929*4882a593Smuzhiyun 		.cra_priority		= 400,
930*4882a593Smuzhiyun 		.cra_flags		= CRYPTO_ALG_ASYNC,
931*4882a593Smuzhiyun 		.cra_blocksize		= SHA1_BLOCK_SIZE,
932*4882a593Smuzhiyun 		.cra_ctxsize		= sizeof(struct mtk_sha_ctx),
933*4882a593Smuzhiyun 		.cra_alignmask		= SHA_ALIGN_MSK,
934*4882a593Smuzhiyun 		.cra_module		= THIS_MODULE,
935*4882a593Smuzhiyun 		.cra_init		= mtk_sha_cra_init,
936*4882a593Smuzhiyun 		.cra_exit		= mtk_sha_cra_exit,
937*4882a593Smuzhiyun 	}
938*4882a593Smuzhiyun },
939*4882a593Smuzhiyun {
940*4882a593Smuzhiyun 	.init		= mtk_sha_init,
941*4882a593Smuzhiyun 	.update		= mtk_sha_update,
942*4882a593Smuzhiyun 	.final		= mtk_sha_final,
943*4882a593Smuzhiyun 	.finup		= mtk_sha_finup,
944*4882a593Smuzhiyun 	.digest		= mtk_sha_digest,
945*4882a593Smuzhiyun 	.export		= mtk_sha_export,
946*4882a593Smuzhiyun 	.import		= mtk_sha_import,
947*4882a593Smuzhiyun 	.halg.digestsize	= SHA224_DIGEST_SIZE,
948*4882a593Smuzhiyun 	.halg.statesize = sizeof(struct mtk_sha_reqctx),
949*4882a593Smuzhiyun 	.halg.base	= {
950*4882a593Smuzhiyun 		.cra_name		= "sha224",
951*4882a593Smuzhiyun 		.cra_driver_name	= "mtk-sha224",
952*4882a593Smuzhiyun 		.cra_priority		= 400,
953*4882a593Smuzhiyun 		.cra_flags		= CRYPTO_ALG_ASYNC,
954*4882a593Smuzhiyun 		.cra_blocksize		= SHA224_BLOCK_SIZE,
955*4882a593Smuzhiyun 		.cra_ctxsize		= sizeof(struct mtk_sha_ctx),
956*4882a593Smuzhiyun 		.cra_alignmask		= SHA_ALIGN_MSK,
957*4882a593Smuzhiyun 		.cra_module		= THIS_MODULE,
958*4882a593Smuzhiyun 		.cra_init		= mtk_sha_cra_init,
959*4882a593Smuzhiyun 		.cra_exit		= mtk_sha_cra_exit,
960*4882a593Smuzhiyun 	}
961*4882a593Smuzhiyun },
962*4882a593Smuzhiyun {
963*4882a593Smuzhiyun 	.init		= mtk_sha_init,
964*4882a593Smuzhiyun 	.update		= mtk_sha_update,
965*4882a593Smuzhiyun 	.final		= mtk_sha_final,
966*4882a593Smuzhiyun 	.finup		= mtk_sha_finup,
967*4882a593Smuzhiyun 	.digest		= mtk_sha_digest,
968*4882a593Smuzhiyun 	.export		= mtk_sha_export,
969*4882a593Smuzhiyun 	.import		= mtk_sha_import,
970*4882a593Smuzhiyun 	.halg.digestsize	= SHA256_DIGEST_SIZE,
971*4882a593Smuzhiyun 	.halg.statesize = sizeof(struct mtk_sha_reqctx),
972*4882a593Smuzhiyun 	.halg.base	= {
973*4882a593Smuzhiyun 		.cra_name		= "sha256",
974*4882a593Smuzhiyun 		.cra_driver_name	= "mtk-sha256",
975*4882a593Smuzhiyun 		.cra_priority		= 400,
976*4882a593Smuzhiyun 		.cra_flags		= CRYPTO_ALG_ASYNC,
977*4882a593Smuzhiyun 		.cra_blocksize		= SHA256_BLOCK_SIZE,
978*4882a593Smuzhiyun 		.cra_ctxsize		= sizeof(struct mtk_sha_ctx),
979*4882a593Smuzhiyun 		.cra_alignmask		= SHA_ALIGN_MSK,
980*4882a593Smuzhiyun 		.cra_module		= THIS_MODULE,
981*4882a593Smuzhiyun 		.cra_init		= mtk_sha_cra_init,
982*4882a593Smuzhiyun 		.cra_exit		= mtk_sha_cra_exit,
983*4882a593Smuzhiyun 	}
984*4882a593Smuzhiyun },
985*4882a593Smuzhiyun {
986*4882a593Smuzhiyun 	.init		= mtk_sha_init,
987*4882a593Smuzhiyun 	.update		= mtk_sha_update,
988*4882a593Smuzhiyun 	.final		= mtk_sha_final,
989*4882a593Smuzhiyun 	.finup		= mtk_sha_finup,
990*4882a593Smuzhiyun 	.digest		= mtk_sha_digest,
991*4882a593Smuzhiyun 	.export		= mtk_sha_export,
992*4882a593Smuzhiyun 	.import		= mtk_sha_import,
993*4882a593Smuzhiyun 	.setkey		= mtk_sha_setkey,
994*4882a593Smuzhiyun 	.halg.digestsize	= SHA1_DIGEST_SIZE,
995*4882a593Smuzhiyun 	.halg.statesize = sizeof(struct mtk_sha_reqctx),
996*4882a593Smuzhiyun 	.halg.base	= {
997*4882a593Smuzhiyun 		.cra_name		= "hmac(sha1)",
998*4882a593Smuzhiyun 		.cra_driver_name	= "mtk-hmac-sha1",
999*4882a593Smuzhiyun 		.cra_priority		= 400,
1000*4882a593Smuzhiyun 		.cra_flags		= CRYPTO_ALG_ASYNC |
1001*4882a593Smuzhiyun 					  CRYPTO_ALG_NEED_FALLBACK,
1002*4882a593Smuzhiyun 		.cra_blocksize		= SHA1_BLOCK_SIZE,
1003*4882a593Smuzhiyun 		.cra_ctxsize		= sizeof(struct mtk_sha_ctx) +
1004*4882a593Smuzhiyun 					sizeof(struct mtk_sha_hmac_ctx),
1005*4882a593Smuzhiyun 		.cra_alignmask		= SHA_ALIGN_MSK,
1006*4882a593Smuzhiyun 		.cra_module		= THIS_MODULE,
1007*4882a593Smuzhiyun 		.cra_init		= mtk_sha_cra_sha1_init,
1008*4882a593Smuzhiyun 		.cra_exit		= mtk_sha_cra_exit,
1009*4882a593Smuzhiyun 	}
1010*4882a593Smuzhiyun },
1011*4882a593Smuzhiyun {
1012*4882a593Smuzhiyun 	.init		= mtk_sha_init,
1013*4882a593Smuzhiyun 	.update		= mtk_sha_update,
1014*4882a593Smuzhiyun 	.final		= mtk_sha_final,
1015*4882a593Smuzhiyun 	.finup		= mtk_sha_finup,
1016*4882a593Smuzhiyun 	.digest		= mtk_sha_digest,
1017*4882a593Smuzhiyun 	.export		= mtk_sha_export,
1018*4882a593Smuzhiyun 	.import		= mtk_sha_import,
1019*4882a593Smuzhiyun 	.setkey		= mtk_sha_setkey,
1020*4882a593Smuzhiyun 	.halg.digestsize	= SHA224_DIGEST_SIZE,
1021*4882a593Smuzhiyun 	.halg.statesize = sizeof(struct mtk_sha_reqctx),
1022*4882a593Smuzhiyun 	.halg.base	= {
1023*4882a593Smuzhiyun 		.cra_name		= "hmac(sha224)",
1024*4882a593Smuzhiyun 		.cra_driver_name	= "mtk-hmac-sha224",
1025*4882a593Smuzhiyun 		.cra_priority		= 400,
1026*4882a593Smuzhiyun 		.cra_flags		= CRYPTO_ALG_ASYNC |
1027*4882a593Smuzhiyun 					  CRYPTO_ALG_NEED_FALLBACK,
1028*4882a593Smuzhiyun 		.cra_blocksize		= SHA224_BLOCK_SIZE,
1029*4882a593Smuzhiyun 		.cra_ctxsize		= sizeof(struct mtk_sha_ctx) +
1030*4882a593Smuzhiyun 					sizeof(struct mtk_sha_hmac_ctx),
1031*4882a593Smuzhiyun 		.cra_alignmask		= SHA_ALIGN_MSK,
1032*4882a593Smuzhiyun 		.cra_module		= THIS_MODULE,
1033*4882a593Smuzhiyun 		.cra_init		= mtk_sha_cra_sha224_init,
1034*4882a593Smuzhiyun 		.cra_exit		= mtk_sha_cra_exit,
1035*4882a593Smuzhiyun 	}
1036*4882a593Smuzhiyun },
1037*4882a593Smuzhiyun {
1038*4882a593Smuzhiyun 	.init		= mtk_sha_init,
1039*4882a593Smuzhiyun 	.update		= mtk_sha_update,
1040*4882a593Smuzhiyun 	.final		= mtk_sha_final,
1041*4882a593Smuzhiyun 	.finup		= mtk_sha_finup,
1042*4882a593Smuzhiyun 	.digest		= mtk_sha_digest,
1043*4882a593Smuzhiyun 	.export		= mtk_sha_export,
1044*4882a593Smuzhiyun 	.import		= mtk_sha_import,
1045*4882a593Smuzhiyun 	.setkey		= mtk_sha_setkey,
1046*4882a593Smuzhiyun 	.halg.digestsize	= SHA256_DIGEST_SIZE,
1047*4882a593Smuzhiyun 	.halg.statesize = sizeof(struct mtk_sha_reqctx),
1048*4882a593Smuzhiyun 	.halg.base	= {
1049*4882a593Smuzhiyun 		.cra_name		= "hmac(sha256)",
1050*4882a593Smuzhiyun 		.cra_driver_name	= "mtk-hmac-sha256",
1051*4882a593Smuzhiyun 		.cra_priority		= 400,
1052*4882a593Smuzhiyun 		.cra_flags		= CRYPTO_ALG_ASYNC |
1053*4882a593Smuzhiyun 					  CRYPTO_ALG_NEED_FALLBACK,
1054*4882a593Smuzhiyun 		.cra_blocksize		= SHA256_BLOCK_SIZE,
1055*4882a593Smuzhiyun 		.cra_ctxsize		= sizeof(struct mtk_sha_ctx) +
1056*4882a593Smuzhiyun 					sizeof(struct mtk_sha_hmac_ctx),
1057*4882a593Smuzhiyun 		.cra_alignmask		= SHA_ALIGN_MSK,
1058*4882a593Smuzhiyun 		.cra_module		= THIS_MODULE,
1059*4882a593Smuzhiyun 		.cra_init		= mtk_sha_cra_sha256_init,
1060*4882a593Smuzhiyun 		.cra_exit		= mtk_sha_cra_exit,
1061*4882a593Smuzhiyun 	}
1062*4882a593Smuzhiyun },
1063*4882a593Smuzhiyun };
1064*4882a593Smuzhiyun 
1065*4882a593Smuzhiyun static struct ahash_alg algs_sha384_sha512[] = {
1066*4882a593Smuzhiyun {
1067*4882a593Smuzhiyun 	.init		= mtk_sha_init,
1068*4882a593Smuzhiyun 	.update		= mtk_sha_update,
1069*4882a593Smuzhiyun 	.final		= mtk_sha_final,
1070*4882a593Smuzhiyun 	.finup		= mtk_sha_finup,
1071*4882a593Smuzhiyun 	.digest		= mtk_sha_digest,
1072*4882a593Smuzhiyun 	.export		= mtk_sha_export,
1073*4882a593Smuzhiyun 	.import		= mtk_sha_import,
1074*4882a593Smuzhiyun 	.halg.digestsize	= SHA384_DIGEST_SIZE,
1075*4882a593Smuzhiyun 	.halg.statesize = sizeof(struct mtk_sha_reqctx),
1076*4882a593Smuzhiyun 	.halg.base	= {
1077*4882a593Smuzhiyun 		.cra_name		= "sha384",
1078*4882a593Smuzhiyun 		.cra_driver_name	= "mtk-sha384",
1079*4882a593Smuzhiyun 		.cra_priority		= 400,
1080*4882a593Smuzhiyun 		.cra_flags		= CRYPTO_ALG_ASYNC,
1081*4882a593Smuzhiyun 		.cra_blocksize		= SHA384_BLOCK_SIZE,
1082*4882a593Smuzhiyun 		.cra_ctxsize		= sizeof(struct mtk_sha_ctx),
1083*4882a593Smuzhiyun 		.cra_alignmask		= SHA_ALIGN_MSK,
1084*4882a593Smuzhiyun 		.cra_module		= THIS_MODULE,
1085*4882a593Smuzhiyun 		.cra_init		= mtk_sha_cra_init,
1086*4882a593Smuzhiyun 		.cra_exit		= mtk_sha_cra_exit,
1087*4882a593Smuzhiyun 	}
1088*4882a593Smuzhiyun },
1089*4882a593Smuzhiyun {
1090*4882a593Smuzhiyun 	.init		= mtk_sha_init,
1091*4882a593Smuzhiyun 	.update		= mtk_sha_update,
1092*4882a593Smuzhiyun 	.final		= mtk_sha_final,
1093*4882a593Smuzhiyun 	.finup		= mtk_sha_finup,
1094*4882a593Smuzhiyun 	.digest		= mtk_sha_digest,
1095*4882a593Smuzhiyun 	.export		= mtk_sha_export,
1096*4882a593Smuzhiyun 	.import		= mtk_sha_import,
1097*4882a593Smuzhiyun 	.halg.digestsize	= SHA512_DIGEST_SIZE,
1098*4882a593Smuzhiyun 	.halg.statesize = sizeof(struct mtk_sha_reqctx),
1099*4882a593Smuzhiyun 	.halg.base	= {
1100*4882a593Smuzhiyun 		.cra_name		= "sha512",
1101*4882a593Smuzhiyun 		.cra_driver_name	= "mtk-sha512",
1102*4882a593Smuzhiyun 		.cra_priority		= 400,
1103*4882a593Smuzhiyun 		.cra_flags		= CRYPTO_ALG_ASYNC,
1104*4882a593Smuzhiyun 		.cra_blocksize		= SHA512_BLOCK_SIZE,
1105*4882a593Smuzhiyun 		.cra_ctxsize		= sizeof(struct mtk_sha_ctx),
1106*4882a593Smuzhiyun 		.cra_alignmask		= SHA_ALIGN_MSK,
1107*4882a593Smuzhiyun 		.cra_module		= THIS_MODULE,
1108*4882a593Smuzhiyun 		.cra_init		= mtk_sha_cra_init,
1109*4882a593Smuzhiyun 		.cra_exit		= mtk_sha_cra_exit,
1110*4882a593Smuzhiyun 	}
1111*4882a593Smuzhiyun },
1112*4882a593Smuzhiyun {
1113*4882a593Smuzhiyun 	.init		= mtk_sha_init,
1114*4882a593Smuzhiyun 	.update		= mtk_sha_update,
1115*4882a593Smuzhiyun 	.final		= mtk_sha_final,
1116*4882a593Smuzhiyun 	.finup		= mtk_sha_finup,
1117*4882a593Smuzhiyun 	.digest		= mtk_sha_digest,
1118*4882a593Smuzhiyun 	.export		= mtk_sha_export,
1119*4882a593Smuzhiyun 	.import		= mtk_sha_import,
1120*4882a593Smuzhiyun 	.setkey		= mtk_sha_setkey,
1121*4882a593Smuzhiyun 	.halg.digestsize	= SHA384_DIGEST_SIZE,
1122*4882a593Smuzhiyun 	.halg.statesize = sizeof(struct mtk_sha_reqctx),
1123*4882a593Smuzhiyun 	.halg.base	= {
1124*4882a593Smuzhiyun 		.cra_name		= "hmac(sha384)",
1125*4882a593Smuzhiyun 		.cra_driver_name	= "mtk-hmac-sha384",
1126*4882a593Smuzhiyun 		.cra_priority		= 400,
1127*4882a593Smuzhiyun 		.cra_flags		= CRYPTO_ALG_ASYNC |
1128*4882a593Smuzhiyun 					  CRYPTO_ALG_NEED_FALLBACK,
1129*4882a593Smuzhiyun 		.cra_blocksize		= SHA384_BLOCK_SIZE,
1130*4882a593Smuzhiyun 		.cra_ctxsize		= sizeof(struct mtk_sha_ctx) +
1131*4882a593Smuzhiyun 					sizeof(struct mtk_sha_hmac_ctx),
1132*4882a593Smuzhiyun 		.cra_alignmask		= SHA_ALIGN_MSK,
1133*4882a593Smuzhiyun 		.cra_module		= THIS_MODULE,
1134*4882a593Smuzhiyun 		.cra_init		= mtk_sha_cra_sha384_init,
1135*4882a593Smuzhiyun 		.cra_exit		= mtk_sha_cra_exit,
1136*4882a593Smuzhiyun 	}
1137*4882a593Smuzhiyun },
1138*4882a593Smuzhiyun {
1139*4882a593Smuzhiyun 	.init		= mtk_sha_init,
1140*4882a593Smuzhiyun 	.update		= mtk_sha_update,
1141*4882a593Smuzhiyun 	.final		= mtk_sha_final,
1142*4882a593Smuzhiyun 	.finup		= mtk_sha_finup,
1143*4882a593Smuzhiyun 	.digest		= mtk_sha_digest,
1144*4882a593Smuzhiyun 	.export		= mtk_sha_export,
1145*4882a593Smuzhiyun 	.import		= mtk_sha_import,
1146*4882a593Smuzhiyun 	.setkey		= mtk_sha_setkey,
1147*4882a593Smuzhiyun 	.halg.digestsize	= SHA512_DIGEST_SIZE,
1148*4882a593Smuzhiyun 	.halg.statesize = sizeof(struct mtk_sha_reqctx),
1149*4882a593Smuzhiyun 	.halg.base	= {
1150*4882a593Smuzhiyun 		.cra_name		= "hmac(sha512)",
1151*4882a593Smuzhiyun 		.cra_driver_name	= "mtk-hmac-sha512",
1152*4882a593Smuzhiyun 		.cra_priority		= 400,
1153*4882a593Smuzhiyun 		.cra_flags		= CRYPTO_ALG_ASYNC |
1154*4882a593Smuzhiyun 					  CRYPTO_ALG_NEED_FALLBACK,
1155*4882a593Smuzhiyun 		.cra_blocksize		= SHA512_BLOCK_SIZE,
1156*4882a593Smuzhiyun 		.cra_ctxsize		= sizeof(struct mtk_sha_ctx) +
1157*4882a593Smuzhiyun 					sizeof(struct mtk_sha_hmac_ctx),
1158*4882a593Smuzhiyun 		.cra_alignmask		= SHA_ALIGN_MSK,
1159*4882a593Smuzhiyun 		.cra_module		= THIS_MODULE,
1160*4882a593Smuzhiyun 		.cra_init		= mtk_sha_cra_sha512_init,
1161*4882a593Smuzhiyun 		.cra_exit		= mtk_sha_cra_exit,
1162*4882a593Smuzhiyun 	}
1163*4882a593Smuzhiyun },
1164*4882a593Smuzhiyun };
1165*4882a593Smuzhiyun 
mtk_sha_queue_task(unsigned long data)1166*4882a593Smuzhiyun static void mtk_sha_queue_task(unsigned long data)
1167*4882a593Smuzhiyun {
1168*4882a593Smuzhiyun 	struct mtk_sha_rec *sha = (struct mtk_sha_rec *)data;
1169*4882a593Smuzhiyun 
1170*4882a593Smuzhiyun 	mtk_sha_handle_queue(sha->cryp, sha->id - MTK_RING2, NULL);
1171*4882a593Smuzhiyun }
1172*4882a593Smuzhiyun 
mtk_sha_done_task(unsigned long data)1173*4882a593Smuzhiyun static void mtk_sha_done_task(unsigned long data)
1174*4882a593Smuzhiyun {
1175*4882a593Smuzhiyun 	struct mtk_sha_rec *sha = (struct mtk_sha_rec *)data;
1176*4882a593Smuzhiyun 	struct mtk_cryp *cryp = sha->cryp;
1177*4882a593Smuzhiyun 
1178*4882a593Smuzhiyun 	mtk_sha_unmap(cryp, sha);
1179*4882a593Smuzhiyun 	mtk_sha_complete(cryp, sha);
1180*4882a593Smuzhiyun }
1181*4882a593Smuzhiyun 
mtk_sha_irq(int irq,void * dev_id)1182*4882a593Smuzhiyun static irqreturn_t mtk_sha_irq(int irq, void *dev_id)
1183*4882a593Smuzhiyun {
1184*4882a593Smuzhiyun 	struct mtk_sha_rec *sha = (struct mtk_sha_rec *)dev_id;
1185*4882a593Smuzhiyun 	struct mtk_cryp *cryp = sha->cryp;
1186*4882a593Smuzhiyun 	u32 val = mtk_sha_read(cryp, RDR_STAT(sha->id));
1187*4882a593Smuzhiyun 
1188*4882a593Smuzhiyun 	mtk_sha_write(cryp, RDR_STAT(sha->id), val);
1189*4882a593Smuzhiyun 
1190*4882a593Smuzhiyun 	if (likely((SHA_FLAGS_BUSY & sha->flags))) {
1191*4882a593Smuzhiyun 		mtk_sha_write(cryp, RDR_PROC_COUNT(sha->id), MTK_CNT_RST);
1192*4882a593Smuzhiyun 		mtk_sha_write(cryp, RDR_THRESH(sha->id),
1193*4882a593Smuzhiyun 			      MTK_RDR_PROC_THRESH | MTK_RDR_PROC_MODE);
1194*4882a593Smuzhiyun 
1195*4882a593Smuzhiyun 		tasklet_schedule(&sha->done_task);
1196*4882a593Smuzhiyun 	} else {
1197*4882a593Smuzhiyun 		dev_warn(cryp->dev, "SHA interrupt when no active requests.\n");
1198*4882a593Smuzhiyun 	}
1199*4882a593Smuzhiyun 	return IRQ_HANDLED;
1200*4882a593Smuzhiyun }
1201*4882a593Smuzhiyun 
1202*4882a593Smuzhiyun /*
1203*4882a593Smuzhiyun  * The purpose of two SHA records is used to get extra performance.
1204*4882a593Smuzhiyun  * It is similar to mtk_aes_record_init().
1205*4882a593Smuzhiyun  */
mtk_sha_record_init(struct mtk_cryp * cryp)1206*4882a593Smuzhiyun static int mtk_sha_record_init(struct mtk_cryp *cryp)
1207*4882a593Smuzhiyun {
1208*4882a593Smuzhiyun 	struct mtk_sha_rec **sha = cryp->sha;
1209*4882a593Smuzhiyun 	int i, err = -ENOMEM;
1210*4882a593Smuzhiyun 
1211*4882a593Smuzhiyun 	for (i = 0; i < MTK_REC_NUM; i++) {
1212*4882a593Smuzhiyun 		sha[i] = kzalloc(sizeof(**sha), GFP_KERNEL);
1213*4882a593Smuzhiyun 		if (!sha[i])
1214*4882a593Smuzhiyun 			goto err_cleanup;
1215*4882a593Smuzhiyun 
1216*4882a593Smuzhiyun 		sha[i]->cryp = cryp;
1217*4882a593Smuzhiyun 
1218*4882a593Smuzhiyun 		spin_lock_init(&sha[i]->lock);
1219*4882a593Smuzhiyun 		crypto_init_queue(&sha[i]->queue, SHA_QUEUE_SIZE);
1220*4882a593Smuzhiyun 
1221*4882a593Smuzhiyun 		tasklet_init(&sha[i]->queue_task, mtk_sha_queue_task,
1222*4882a593Smuzhiyun 			     (unsigned long)sha[i]);
1223*4882a593Smuzhiyun 		tasklet_init(&sha[i]->done_task, mtk_sha_done_task,
1224*4882a593Smuzhiyun 			     (unsigned long)sha[i]);
1225*4882a593Smuzhiyun 	}
1226*4882a593Smuzhiyun 
1227*4882a593Smuzhiyun 	/* Link to ring2 and ring3 respectively */
1228*4882a593Smuzhiyun 	sha[0]->id = MTK_RING2;
1229*4882a593Smuzhiyun 	sha[1]->id = MTK_RING3;
1230*4882a593Smuzhiyun 
1231*4882a593Smuzhiyun 	cryp->rec = 1;
1232*4882a593Smuzhiyun 
1233*4882a593Smuzhiyun 	return 0;
1234*4882a593Smuzhiyun 
1235*4882a593Smuzhiyun err_cleanup:
1236*4882a593Smuzhiyun 	for (; i--; )
1237*4882a593Smuzhiyun 		kfree(sha[i]);
1238*4882a593Smuzhiyun 	return err;
1239*4882a593Smuzhiyun }
1240*4882a593Smuzhiyun 
mtk_sha_record_free(struct mtk_cryp * cryp)1241*4882a593Smuzhiyun static void mtk_sha_record_free(struct mtk_cryp *cryp)
1242*4882a593Smuzhiyun {
1243*4882a593Smuzhiyun 	int i;
1244*4882a593Smuzhiyun 
1245*4882a593Smuzhiyun 	for (i = 0; i < MTK_REC_NUM; i++) {
1246*4882a593Smuzhiyun 		tasklet_kill(&cryp->sha[i]->done_task);
1247*4882a593Smuzhiyun 		tasklet_kill(&cryp->sha[i]->queue_task);
1248*4882a593Smuzhiyun 
1249*4882a593Smuzhiyun 		kfree(cryp->sha[i]);
1250*4882a593Smuzhiyun 	}
1251*4882a593Smuzhiyun }
1252*4882a593Smuzhiyun 
mtk_sha_unregister_algs(void)1253*4882a593Smuzhiyun static void mtk_sha_unregister_algs(void)
1254*4882a593Smuzhiyun {
1255*4882a593Smuzhiyun 	int i;
1256*4882a593Smuzhiyun 
1257*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(algs_sha1_sha224_sha256); i++)
1258*4882a593Smuzhiyun 		crypto_unregister_ahash(&algs_sha1_sha224_sha256[i]);
1259*4882a593Smuzhiyun 
1260*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(algs_sha384_sha512); i++)
1261*4882a593Smuzhiyun 		crypto_unregister_ahash(&algs_sha384_sha512[i]);
1262*4882a593Smuzhiyun }
1263*4882a593Smuzhiyun 
mtk_sha_register_algs(void)1264*4882a593Smuzhiyun static int mtk_sha_register_algs(void)
1265*4882a593Smuzhiyun {
1266*4882a593Smuzhiyun 	int err, i;
1267*4882a593Smuzhiyun 
1268*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(algs_sha1_sha224_sha256); i++) {
1269*4882a593Smuzhiyun 		err = crypto_register_ahash(&algs_sha1_sha224_sha256[i]);
1270*4882a593Smuzhiyun 		if (err)
1271*4882a593Smuzhiyun 			goto err_sha_224_256_algs;
1272*4882a593Smuzhiyun 	}
1273*4882a593Smuzhiyun 
1274*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(algs_sha384_sha512); i++) {
1275*4882a593Smuzhiyun 		err = crypto_register_ahash(&algs_sha384_sha512[i]);
1276*4882a593Smuzhiyun 		if (err)
1277*4882a593Smuzhiyun 			goto err_sha_384_512_algs;
1278*4882a593Smuzhiyun 	}
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun 	return 0;
1281*4882a593Smuzhiyun 
1282*4882a593Smuzhiyun err_sha_384_512_algs:
1283*4882a593Smuzhiyun 	for (; i--; )
1284*4882a593Smuzhiyun 		crypto_unregister_ahash(&algs_sha384_sha512[i]);
1285*4882a593Smuzhiyun 	i = ARRAY_SIZE(algs_sha1_sha224_sha256);
1286*4882a593Smuzhiyun err_sha_224_256_algs:
1287*4882a593Smuzhiyun 	for (; i--; )
1288*4882a593Smuzhiyun 		crypto_unregister_ahash(&algs_sha1_sha224_sha256[i]);
1289*4882a593Smuzhiyun 
1290*4882a593Smuzhiyun 	return err;
1291*4882a593Smuzhiyun }
1292*4882a593Smuzhiyun 
mtk_hash_alg_register(struct mtk_cryp * cryp)1293*4882a593Smuzhiyun int mtk_hash_alg_register(struct mtk_cryp *cryp)
1294*4882a593Smuzhiyun {
1295*4882a593Smuzhiyun 	int err;
1296*4882a593Smuzhiyun 
1297*4882a593Smuzhiyun 	INIT_LIST_HEAD(&cryp->sha_list);
1298*4882a593Smuzhiyun 
1299*4882a593Smuzhiyun 	/* Initialize two hash records */
1300*4882a593Smuzhiyun 	err = mtk_sha_record_init(cryp);
1301*4882a593Smuzhiyun 	if (err)
1302*4882a593Smuzhiyun 		goto err_record;
1303*4882a593Smuzhiyun 
1304*4882a593Smuzhiyun 	err = devm_request_irq(cryp->dev, cryp->irq[MTK_RING2], mtk_sha_irq,
1305*4882a593Smuzhiyun 			       0, "mtk-sha", cryp->sha[0]);
1306*4882a593Smuzhiyun 	if (err) {
1307*4882a593Smuzhiyun 		dev_err(cryp->dev, "unable to request sha irq0.\n");
1308*4882a593Smuzhiyun 		goto err_res;
1309*4882a593Smuzhiyun 	}
1310*4882a593Smuzhiyun 
1311*4882a593Smuzhiyun 	err = devm_request_irq(cryp->dev, cryp->irq[MTK_RING3], mtk_sha_irq,
1312*4882a593Smuzhiyun 			       0, "mtk-sha", cryp->sha[1]);
1313*4882a593Smuzhiyun 	if (err) {
1314*4882a593Smuzhiyun 		dev_err(cryp->dev, "unable to request sha irq1.\n");
1315*4882a593Smuzhiyun 		goto err_res;
1316*4882a593Smuzhiyun 	}
1317*4882a593Smuzhiyun 
1318*4882a593Smuzhiyun 	/* Enable ring2 and ring3 interrupt for hash */
1319*4882a593Smuzhiyun 	mtk_sha_write(cryp, AIC_ENABLE_SET(MTK_RING2), MTK_IRQ_RDR2);
1320*4882a593Smuzhiyun 	mtk_sha_write(cryp, AIC_ENABLE_SET(MTK_RING3), MTK_IRQ_RDR3);
1321*4882a593Smuzhiyun 
1322*4882a593Smuzhiyun 	spin_lock(&mtk_sha.lock);
1323*4882a593Smuzhiyun 	list_add_tail(&cryp->sha_list, &mtk_sha.dev_list);
1324*4882a593Smuzhiyun 	spin_unlock(&mtk_sha.lock);
1325*4882a593Smuzhiyun 
1326*4882a593Smuzhiyun 	err = mtk_sha_register_algs();
1327*4882a593Smuzhiyun 	if (err)
1328*4882a593Smuzhiyun 		goto err_algs;
1329*4882a593Smuzhiyun 
1330*4882a593Smuzhiyun 	return 0;
1331*4882a593Smuzhiyun 
1332*4882a593Smuzhiyun err_algs:
1333*4882a593Smuzhiyun 	spin_lock(&mtk_sha.lock);
1334*4882a593Smuzhiyun 	list_del(&cryp->sha_list);
1335*4882a593Smuzhiyun 	spin_unlock(&mtk_sha.lock);
1336*4882a593Smuzhiyun err_res:
1337*4882a593Smuzhiyun 	mtk_sha_record_free(cryp);
1338*4882a593Smuzhiyun err_record:
1339*4882a593Smuzhiyun 
1340*4882a593Smuzhiyun 	dev_err(cryp->dev, "mtk-sha initialization failed.\n");
1341*4882a593Smuzhiyun 	return err;
1342*4882a593Smuzhiyun }
1343*4882a593Smuzhiyun 
mtk_hash_alg_release(struct mtk_cryp * cryp)1344*4882a593Smuzhiyun void mtk_hash_alg_release(struct mtk_cryp *cryp)
1345*4882a593Smuzhiyun {
1346*4882a593Smuzhiyun 	spin_lock(&mtk_sha.lock);
1347*4882a593Smuzhiyun 	list_del(&cryp->sha_list);
1348*4882a593Smuzhiyun 	spin_unlock(&mtk_sha.lock);
1349*4882a593Smuzhiyun 
1350*4882a593Smuzhiyun 	mtk_sha_unregister_algs();
1351*4882a593Smuzhiyun 	mtk_sha_record_free(cryp);
1352*4882a593Smuzhiyun }
1353