xref: /linux/drivers/crypto/caam/caamhash.c (revision b8aa7dc5c7535f9abfca4bceb0ade9ee10cf5f54)
1618b5dc4SHoria Geantă // SPDX-License-Identifier: GPL-2.0+
2045e3678SYuan Kang /*
3045e3678SYuan Kang  * caam - Freescale FSL CAAM support for ahash functions of crypto API
4045e3678SYuan Kang  *
5045e3678SYuan Kang  * Copyright 2011 Freescale Semiconductor, Inc.
687870cfbSIuliana Prodan  * Copyright 2018-2019 NXP
7045e3678SYuan Kang  *
8045e3678SYuan Kang  * Based on caamalg.c crypto API driver.
9045e3678SYuan Kang  *
10045e3678SYuan Kang  * relationship of digest job descriptor or first job descriptor after init to
11045e3678SYuan Kang  * shared descriptors:
12045e3678SYuan Kang  *
13045e3678SYuan Kang  * ---------------                     ---------------
14045e3678SYuan Kang  * | JobDesc #1  |-------------------->|  ShareDesc  |
15045e3678SYuan Kang  * | *(packet 1) |                     |  (hashKey)  |
16045e3678SYuan Kang  * ---------------                     | (operation) |
17045e3678SYuan Kang  *                                     ---------------
18045e3678SYuan Kang  *
19045e3678SYuan Kang  * relationship of subsequent job descriptors to shared descriptors:
20045e3678SYuan Kang  *
21045e3678SYuan Kang  * ---------------                     ---------------
22045e3678SYuan Kang  * | JobDesc #2  |-------------------->|  ShareDesc  |
23045e3678SYuan Kang  * | *(packet 2) |      |------------->|  (hashKey)  |
24045e3678SYuan Kang  * ---------------      |    |-------->| (operation) |
25045e3678SYuan Kang  *       .              |    |         | (load ctx2) |
26045e3678SYuan Kang  *       .              |    |         ---------------
27045e3678SYuan Kang  * ---------------      |    |
28045e3678SYuan Kang  * | JobDesc #3  |------|    |
29045e3678SYuan Kang  * | *(packet 3) |           |
30045e3678SYuan Kang  * ---------------           |
31045e3678SYuan Kang  *       .                   |
32045e3678SYuan Kang  *       .                   |
33045e3678SYuan Kang  * ---------------           |
34045e3678SYuan Kang  * | JobDesc #4  |------------
35045e3678SYuan Kang  * | *(packet 4) |
36045e3678SYuan Kang  * ---------------
37045e3678SYuan Kang  *
38045e3678SYuan Kang  * The SharedDesc never changes for a connection unless rekeyed, but
39045e3678SYuan Kang  * each packet will likely be in a different place. So all we need
40045e3678SYuan Kang  * to know to process the packet is where the input is, where the
41045e3678SYuan Kang  * output goes, and what context we want to process with. Context is
42045e3678SYuan Kang  * in the SharedDesc, packet references in the JobDesc.
43045e3678SYuan Kang  *
44045e3678SYuan Kang  * So, a job desc looks like:
45045e3678SYuan Kang  *
46045e3678SYuan Kang  * ---------------------
47045e3678SYuan Kang  * | Header            |
48045e3678SYuan Kang  * | ShareDesc Pointer |
49045e3678SYuan Kang  * | SEQ_OUT_PTR       |
50045e3678SYuan Kang  * | (output buffer)   |
51045e3678SYuan Kang  * | (output length)   |
52045e3678SYuan Kang  * | SEQ_IN_PTR        |
53045e3678SYuan Kang  * | (input buffer)    |
54045e3678SYuan Kang  * | (input length)    |
55045e3678SYuan Kang  * ---------------------
56045e3678SYuan Kang  */
57045e3678SYuan Kang 
58045e3678SYuan Kang #include "compat.h"
59045e3678SYuan Kang 
60045e3678SYuan Kang #include "regs.h"
61045e3678SYuan Kang #include "intern.h"
62045e3678SYuan Kang #include "desc_constr.h"
63045e3678SYuan Kang #include "jr.h"
64045e3678SYuan Kang #include "error.h"
65045e3678SYuan Kang #include "sg_sw_sec4.h"
66045e3678SYuan Kang #include "key_gen.h"
670efa7579SHoria Geantă #include "caamhash_desc.h"
6821b014f0SIuliana Prodan #include <crypto/engine.h>
69045e3678SYuan Kang 
70045e3678SYuan Kang #define CAAM_CRA_PRIORITY		3000
71045e3678SYuan Kang 
72045e3678SYuan Kang /* max hash key is max split key size */
73045e3678SYuan Kang #define CAAM_MAX_HASH_KEY_SIZE		(SHA512_DIGEST_SIZE * 2)
74045e3678SYuan Kang 
75045e3678SYuan Kang #define CAAM_MAX_HASH_BLOCK_SIZE	SHA512_BLOCK_SIZE
76045e3678SYuan Kang #define CAAM_MAX_HASH_DIGEST_SIZE	SHA512_DIGEST_SIZE
77045e3678SYuan Kang 
78045e3678SYuan Kang #define DESC_HASH_MAX_USED_BYTES	(DESC_AHASH_FINAL_LEN + \
79045e3678SYuan Kang 					 CAAM_MAX_HASH_KEY_SIZE)
80045e3678SYuan Kang #define DESC_HASH_MAX_USED_LEN		(DESC_HASH_MAX_USED_BYTES / CAAM_CMD_SZ)
81045e3678SYuan Kang 
82045e3678SYuan Kang /* caam context sizes for hashes: running digest + 8 */
83045e3678SYuan Kang #define HASH_MSG_LEN			8
84045e3678SYuan Kang #define MAX_CTX_LEN			(HASH_MSG_LEN + SHA512_DIGEST_SIZE)
85045e3678SYuan Kang 
86cfc6f11bSRuchika Gupta static struct list_head hash_list;
87cfc6f11bSRuchika Gupta 
88045e3678SYuan Kang /* ahash per-session context */
89045e3678SYuan Kang struct caam_hash_ctx {
9021b014f0SIuliana Prodan 	struct crypto_engine_ctx enginectx;
91e11793f5SRussell King 	u32 sh_desc_update[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
92e11793f5SRussell King 	u32 sh_desc_update_first[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
93e11793f5SRussell King 	u32 sh_desc_fin[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
94e11793f5SRussell King 	u32 sh_desc_digest[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
9512b8567fSIuliana Prodan 	u8 key[CAAM_MAX_HASH_KEY_SIZE] ____cacheline_aligned;
96e11793f5SRussell King 	dma_addr_t sh_desc_update_dma ____cacheline_aligned;
97045e3678SYuan Kang 	dma_addr_t sh_desc_update_first_dma;
98045e3678SYuan Kang 	dma_addr_t sh_desc_fin_dma;
99045e3678SYuan Kang 	dma_addr_t sh_desc_digest_dma;
1007e0880b9SHoria Geantă 	enum dma_data_direction dir;
101e9b4913aSHoria Geantă 	enum dma_data_direction key_dir;
102e11793f5SRussell King 	struct device *jrdev;
103045e3678SYuan Kang 	int ctx_len;
104db57656bSHoria Geantă 	struct alginfo adata;
105045e3678SYuan Kang };
106045e3678SYuan Kang 
107045e3678SYuan Kang /* ahash state */
108045e3678SYuan Kang struct caam_hash_state {
109045e3678SYuan Kang 	dma_addr_t buf_dma;
110045e3678SYuan Kang 	dma_addr_t ctx_dma;
11165055e21SFranck LENORMAND 	int ctx_dma_len;
11246b49abcSAndrei Botila 	u8 buf[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
11346b49abcSAndrei Botila 	int buflen;
11446b49abcSAndrei Botila 	int next_buflen;
115e7472422SVictoria Milhoan 	u8 caam_ctx[MAX_CTX_LEN] ____cacheline_aligned;
11621b014f0SIuliana Prodan 	int (*update)(struct ahash_request *req) ____cacheline_aligned;
117045e3678SYuan Kang 	int (*final)(struct ahash_request *req);
118045e3678SYuan Kang 	int (*finup)(struct ahash_request *req);
11921b014f0SIuliana Prodan 	struct ahash_edesc *edesc;
12021b014f0SIuliana Prodan 	void (*ahash_op_done)(struct device *jrdev, u32 *desc, u32 err,
12121b014f0SIuliana Prodan 			      void *context);
122045e3678SYuan Kang };
123045e3678SYuan Kang 
1245ec90831SRussell King struct caam_export_state {
1255ec90831SRussell King 	u8 buf[CAAM_MAX_HASH_BLOCK_SIZE];
1265ec90831SRussell King 	u8 caam_ctx[MAX_CTX_LEN];
1275ec90831SRussell King 	int buflen;
1285ec90831SRussell King 	int (*update)(struct ahash_request *req);
1295ec90831SRussell King 	int (*final)(struct ahash_request *req);
1305ec90831SRussell King 	int (*finup)(struct ahash_request *req);
1315ec90831SRussell King };
1325ec90831SRussell King 
13387870cfbSIuliana Prodan static inline bool is_cmac_aes(u32 algtype)
13412b8567fSIuliana Prodan {
13512b8567fSIuliana Prodan 	return (algtype & (OP_ALG_ALGSEL_MASK | OP_ALG_AAI_MASK)) ==
13687870cfbSIuliana Prodan 	       (OP_ALG_ALGSEL_AES | OP_ALG_AAI_CMAC);
13712b8567fSIuliana Prodan }
138045e3678SYuan Kang /* Common job descriptor seq in/out ptr routines */
139045e3678SYuan Kang 
140045e3678SYuan Kang /* Map state->caam_ctx, and append seq_out_ptr command that points to it */
141ce572085SHoria Geanta static inline int map_seq_out_ptr_ctx(u32 *desc, struct device *jrdev,
142045e3678SYuan Kang 				      struct caam_hash_state *state,
143045e3678SYuan Kang 				      int ctx_len)
144045e3678SYuan Kang {
14565055e21SFranck LENORMAND 	state->ctx_dma_len = ctx_len;
146045e3678SYuan Kang 	state->ctx_dma = dma_map_single(jrdev, state->caam_ctx,
147045e3678SYuan Kang 					ctx_len, DMA_FROM_DEVICE);
148ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, state->ctx_dma)) {
149ce572085SHoria Geanta 		dev_err(jrdev, "unable to map ctx\n");
15087ec02e7SHoria Geantă 		state->ctx_dma = 0;
151ce572085SHoria Geanta 		return -ENOMEM;
152ce572085SHoria Geanta 	}
153ce572085SHoria Geanta 
154045e3678SYuan Kang 	append_seq_out_ptr(desc, state->ctx_dma, ctx_len, 0);
155ce572085SHoria Geanta 
156ce572085SHoria Geanta 	return 0;
157045e3678SYuan Kang }
158045e3678SYuan Kang 
159944c3d4dSHoria Geantă /* Map current buffer in state (if length > 0) and put it in link table */
160944c3d4dSHoria Geantă static inline int buf_map_to_sec4_sg(struct device *jrdev,
161045e3678SYuan Kang 				     struct sec4_sg_entry *sec4_sg,
162944c3d4dSHoria Geantă 				     struct caam_hash_state *state)
163045e3678SYuan Kang {
16446b49abcSAndrei Botila 	int buflen = state->buflen;
165045e3678SYuan Kang 
166944c3d4dSHoria Geantă 	if (!buflen)
167944c3d4dSHoria Geantă 		return 0;
168045e3678SYuan Kang 
16946b49abcSAndrei Botila 	state->buf_dma = dma_map_single(jrdev, state->buf, buflen,
170944c3d4dSHoria Geantă 					DMA_TO_DEVICE);
171944c3d4dSHoria Geantă 	if (dma_mapping_error(jrdev, state->buf_dma)) {
172944c3d4dSHoria Geantă 		dev_err(jrdev, "unable to map buf\n");
173944c3d4dSHoria Geantă 		state->buf_dma = 0;
174944c3d4dSHoria Geantă 		return -ENOMEM;
175045e3678SYuan Kang 	}
176045e3678SYuan Kang 
177944c3d4dSHoria Geantă 	dma_to_sec4_sg_one(sec4_sg, state->buf_dma, buflen, 0);
178045e3678SYuan Kang 
179944c3d4dSHoria Geantă 	return 0;
180045e3678SYuan Kang }
181045e3678SYuan Kang 
182045e3678SYuan Kang /* Map state->caam_ctx, and add it to link table */
183dfcd8393SHoria Geantă static inline int ctx_map_to_sec4_sg(struct device *jrdev,
184ce572085SHoria Geanta 				     struct caam_hash_state *state, int ctx_len,
185ce572085SHoria Geanta 				     struct sec4_sg_entry *sec4_sg, u32 flag)
186045e3678SYuan Kang {
18765055e21SFranck LENORMAND 	state->ctx_dma_len = ctx_len;
188045e3678SYuan Kang 	state->ctx_dma = dma_map_single(jrdev, state->caam_ctx, ctx_len, flag);
189ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, state->ctx_dma)) {
190ce572085SHoria Geanta 		dev_err(jrdev, "unable to map ctx\n");
19187ec02e7SHoria Geantă 		state->ctx_dma = 0;
192ce572085SHoria Geanta 		return -ENOMEM;
193ce572085SHoria Geanta 	}
194ce572085SHoria Geanta 
195045e3678SYuan Kang 	dma_to_sec4_sg_one(sec4_sg, state->ctx_dma, ctx_len, 0);
196ce572085SHoria Geanta 
197ce572085SHoria Geanta 	return 0;
198045e3678SYuan Kang }
199045e3678SYuan Kang 
200045e3678SYuan Kang static int ahash_set_sh_desc(struct crypto_ahash *ahash)
201045e3678SYuan Kang {
202045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
203045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
204045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
2057e0880b9SHoria Geantă 	struct caam_drv_private *ctrlpriv = dev_get_drvdata(jrdev->parent);
206045e3678SYuan Kang 	u32 *desc;
207045e3678SYuan Kang 
2087e0880b9SHoria Geantă 	ctx->adata.key_virt = ctx->key;
2097e0880b9SHoria Geantă 
210045e3678SYuan Kang 	/* ahash_update shared descriptor */
211045e3678SYuan Kang 	desc = ctx->sh_desc_update;
2120efa7579SHoria Geantă 	cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_UPDATE, ctx->ctx_len,
2130efa7579SHoria Geantă 			  ctx->ctx_len, true, ctrlpriv->era);
214bbf22344SHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma,
2157e0880b9SHoria Geantă 				   desc_bytes(desc), ctx->dir);
2166e005503SSascha Hauer 
2176e005503SSascha Hauer 	print_hex_dump_debug("ahash update shdesc@"__stringify(__LINE__)": ",
2186e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
2196e005503SSascha Hauer 			     1);
220045e3678SYuan Kang 
221045e3678SYuan Kang 	/* ahash_update_first shared descriptor */
222045e3678SYuan Kang 	desc = ctx->sh_desc_update_first;
2230efa7579SHoria Geantă 	cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len,
2240efa7579SHoria Geantă 			  ctx->ctx_len, false, ctrlpriv->era);
225bbf22344SHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma,
2267e0880b9SHoria Geantă 				   desc_bytes(desc), ctx->dir);
2276e005503SSascha Hauer 	print_hex_dump_debug("ahash update first shdesc@"__stringify(__LINE__)
2286e005503SSascha Hauer 			     ": ", DUMP_PREFIX_ADDRESS, 16, 4, desc,
2296e005503SSascha Hauer 			     desc_bytes(desc), 1);
230045e3678SYuan Kang 
231045e3678SYuan Kang 	/* ahash_final shared descriptor */
232045e3678SYuan Kang 	desc = ctx->sh_desc_fin;
2330efa7579SHoria Geantă 	cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_FINALIZE, digestsize,
2340efa7579SHoria Geantă 			  ctx->ctx_len, true, ctrlpriv->era);
235bbf22344SHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma,
2367e0880b9SHoria Geantă 				   desc_bytes(desc), ctx->dir);
2376e005503SSascha Hauer 
2386e005503SSascha Hauer 	print_hex_dump_debug("ahash final shdesc@"__stringify(__LINE__)": ",
239045e3678SYuan Kang 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
240045e3678SYuan Kang 			     desc_bytes(desc), 1);
241045e3678SYuan Kang 
242045e3678SYuan Kang 	/* ahash_digest shared descriptor */
243045e3678SYuan Kang 	desc = ctx->sh_desc_digest;
2440efa7579SHoria Geantă 	cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_INITFINAL, digestsize,
2450efa7579SHoria Geantă 			  ctx->ctx_len, false, ctrlpriv->era);
246bbf22344SHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma,
2477e0880b9SHoria Geantă 				   desc_bytes(desc), ctx->dir);
2486e005503SSascha Hauer 
2496e005503SSascha Hauer 	print_hex_dump_debug("ahash digest shdesc@"__stringify(__LINE__)": ",
250045e3678SYuan Kang 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
251045e3678SYuan Kang 			     desc_bytes(desc), 1);
252045e3678SYuan Kang 
253045e3678SYuan Kang 	return 0;
254045e3678SYuan Kang }
255045e3678SYuan Kang 
25612b8567fSIuliana Prodan static int axcbc_set_sh_desc(struct crypto_ahash *ahash)
25712b8567fSIuliana Prodan {
25812b8567fSIuliana Prodan 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
25912b8567fSIuliana Prodan 	int digestsize = crypto_ahash_digestsize(ahash);
26012b8567fSIuliana Prodan 	struct device *jrdev = ctx->jrdev;
26112b8567fSIuliana Prodan 	u32 *desc;
26212b8567fSIuliana Prodan 
26312b8567fSIuliana Prodan 	/* shared descriptor for ahash_update */
26412b8567fSIuliana Prodan 	desc = ctx->sh_desc_update;
26587870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_UPDATE,
266a2fb864cSHoria Geantă 			    ctx->ctx_len, ctx->ctx_len);
26712b8567fSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma,
26812b8567fSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
26912b8567fSIuliana Prodan 	print_hex_dump_debug("axcbc update shdesc@" __stringify(__LINE__)" : ",
27012b8567fSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
27112b8567fSIuliana Prodan 			     1);
27212b8567fSIuliana Prodan 
27312b8567fSIuliana Prodan 	/* shared descriptor for ahash_{final,finup} */
27412b8567fSIuliana Prodan 	desc = ctx->sh_desc_fin;
27587870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_FINALIZE,
276a2fb864cSHoria Geantă 			    digestsize, ctx->ctx_len);
27712b8567fSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma,
27812b8567fSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
27912b8567fSIuliana Prodan 	print_hex_dump_debug("axcbc finup shdesc@" __stringify(__LINE__)" : ",
28012b8567fSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
28112b8567fSIuliana Prodan 			     1);
28212b8567fSIuliana Prodan 
28312b8567fSIuliana Prodan 	/* key is immediate data for INIT and INITFINAL states */
28412b8567fSIuliana Prodan 	ctx->adata.key_virt = ctx->key;
28512b8567fSIuliana Prodan 
28612b8567fSIuliana Prodan 	/* shared descriptor for first invocation of ahash_update */
28712b8567fSIuliana Prodan 	desc = ctx->sh_desc_update_first;
28887870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len,
289a2fb864cSHoria Geantă 			    ctx->ctx_len);
29012b8567fSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma,
29112b8567fSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
2926e005503SSascha Hauer 	print_hex_dump_debug("axcbc update first shdesc@" __stringify(__LINE__)
2936e005503SSascha Hauer 			     " : ", DUMP_PREFIX_ADDRESS, 16, 4, desc,
2946e005503SSascha Hauer 			     desc_bytes(desc), 1);
29512b8567fSIuliana Prodan 
29612b8567fSIuliana Prodan 	/* shared descriptor for ahash_digest */
29712b8567fSIuliana Prodan 	desc = ctx->sh_desc_digest;
29887870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INITFINAL,
299a2fb864cSHoria Geantă 			    digestsize, ctx->ctx_len);
30012b8567fSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma,
30112b8567fSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
30212b8567fSIuliana Prodan 	print_hex_dump_debug("axcbc digest shdesc@" __stringify(__LINE__)" : ",
30312b8567fSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
30412b8567fSIuliana Prodan 			     1);
30587870cfbSIuliana Prodan 	return 0;
30687870cfbSIuliana Prodan }
30787870cfbSIuliana Prodan 
30887870cfbSIuliana Prodan static int acmac_set_sh_desc(struct crypto_ahash *ahash)
30987870cfbSIuliana Prodan {
31087870cfbSIuliana Prodan 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
31187870cfbSIuliana Prodan 	int digestsize = crypto_ahash_digestsize(ahash);
31287870cfbSIuliana Prodan 	struct device *jrdev = ctx->jrdev;
31387870cfbSIuliana Prodan 	u32 *desc;
31487870cfbSIuliana Prodan 
31587870cfbSIuliana Prodan 	/* shared descriptor for ahash_update */
31687870cfbSIuliana Prodan 	desc = ctx->sh_desc_update;
31787870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_UPDATE,
318a2fb864cSHoria Geantă 			    ctx->ctx_len, ctx->ctx_len);
31987870cfbSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma,
32087870cfbSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
32187870cfbSIuliana Prodan 	print_hex_dump_debug("acmac update shdesc@" __stringify(__LINE__)" : ",
32287870cfbSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
32387870cfbSIuliana Prodan 			     desc_bytes(desc), 1);
32487870cfbSIuliana Prodan 
32587870cfbSIuliana Prodan 	/* shared descriptor for ahash_{final,finup} */
32687870cfbSIuliana Prodan 	desc = ctx->sh_desc_fin;
32787870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_FINALIZE,
328a2fb864cSHoria Geantă 			    digestsize, ctx->ctx_len);
32987870cfbSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma,
33087870cfbSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
33187870cfbSIuliana Prodan 	print_hex_dump_debug("acmac finup shdesc@" __stringify(__LINE__)" : ",
33287870cfbSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
33387870cfbSIuliana Prodan 			     desc_bytes(desc), 1);
33487870cfbSIuliana Prodan 
33587870cfbSIuliana Prodan 	/* shared descriptor for first invocation of ahash_update */
33687870cfbSIuliana Prodan 	desc = ctx->sh_desc_update_first;
33787870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len,
338a2fb864cSHoria Geantă 			    ctx->ctx_len);
33987870cfbSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma,
34087870cfbSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
3416e005503SSascha Hauer 	print_hex_dump_debug("acmac update first shdesc@" __stringify(__LINE__)
3426e005503SSascha Hauer 			     " : ", DUMP_PREFIX_ADDRESS, 16, 4, desc,
34387870cfbSIuliana Prodan 			     desc_bytes(desc), 1);
34487870cfbSIuliana Prodan 
34587870cfbSIuliana Prodan 	/* shared descriptor for ahash_digest */
34687870cfbSIuliana Prodan 	desc = ctx->sh_desc_digest;
34787870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INITFINAL,
348a2fb864cSHoria Geantă 			    digestsize, ctx->ctx_len);
34987870cfbSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma,
35087870cfbSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
35187870cfbSIuliana Prodan 	print_hex_dump_debug("acmac digest shdesc@" __stringify(__LINE__)" : ",
35287870cfbSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
35387870cfbSIuliana Prodan 			     desc_bytes(desc), 1);
35412b8567fSIuliana Prodan 
35512b8567fSIuliana Prodan 	return 0;
35612b8567fSIuliana Prodan }
35712b8567fSIuliana Prodan 
358045e3678SYuan Kang /* Digest hash size if it is too large */
35930724445SHoria Geantă static int hash_digest_key(struct caam_hash_ctx *ctx, u32 *keylen, u8 *key,
36030724445SHoria Geantă 			   u32 digestsize)
361045e3678SYuan Kang {
362045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
363045e3678SYuan Kang 	u32 *desc;
364045e3678SYuan Kang 	struct split_key_result result;
36530724445SHoria Geantă 	dma_addr_t key_dma;
3669e6df0fdSMarkus Elfring 	int ret;
367045e3678SYuan Kang 
3689c23b7d3SVakul Garg 	desc = kmalloc(CAAM_CMD_SZ * 8 + CAAM_PTR_SZ * 2, GFP_KERNEL | GFP_DMA);
3692af8f4a2SKim Phillips 	if (!desc) {
3702af8f4a2SKim Phillips 		dev_err(jrdev, "unable to allocate key input memory\n");
3712af8f4a2SKim Phillips 		return -ENOMEM;
3722af8f4a2SKim Phillips 	}
373045e3678SYuan Kang 
374045e3678SYuan Kang 	init_job_desc(desc, 0);
375045e3678SYuan Kang 
37630724445SHoria Geantă 	key_dma = dma_map_single(jrdev, key, *keylen, DMA_BIDIRECTIONAL);
37730724445SHoria Geantă 	if (dma_mapping_error(jrdev, key_dma)) {
37830724445SHoria Geantă 		dev_err(jrdev, "unable to map key memory\n");
379045e3678SYuan Kang 		kfree(desc);
380045e3678SYuan Kang 		return -ENOMEM;
381045e3678SYuan Kang 	}
382045e3678SYuan Kang 
383045e3678SYuan Kang 	/* Job descriptor to perform unkeyed hash on key_in */
384db57656bSHoria Geantă 	append_operation(desc, ctx->adata.algtype | OP_ALG_ENCRYPT |
385045e3678SYuan Kang 			 OP_ALG_AS_INITFINAL);
38630724445SHoria Geantă 	append_seq_in_ptr(desc, key_dma, *keylen, 0);
387045e3678SYuan Kang 	append_seq_fifo_load(desc, *keylen, FIFOLD_CLASS_CLASS2 |
388045e3678SYuan Kang 			     FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_MSG);
38930724445SHoria Geantă 	append_seq_out_ptr(desc, key_dma, digestsize, 0);
390045e3678SYuan Kang 	append_seq_store(desc, digestsize, LDST_CLASS_2_CCB |
391045e3678SYuan Kang 			 LDST_SRCDST_BYTE_CONTEXT);
392045e3678SYuan Kang 
3936e005503SSascha Hauer 	print_hex_dump_debug("key_in@"__stringify(__LINE__)": ",
39430724445SHoria Geantă 			     DUMP_PREFIX_ADDRESS, 16, 4, key, *keylen, 1);
3956e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
3966e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
3976e005503SSascha Hauer 			     1);
398045e3678SYuan Kang 
399045e3678SYuan Kang 	result.err = 0;
400045e3678SYuan Kang 	init_completion(&result.completion);
401045e3678SYuan Kang 
402045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, split_key_done, &result);
4034d370a10SIuliana Prodan 	if (ret == -EINPROGRESS) {
404045e3678SYuan Kang 		/* in progress */
4057459e1d2SHoria Geantă 		wait_for_completion(&result.completion);
406045e3678SYuan Kang 		ret = result.err;
4076e005503SSascha Hauer 
4086e005503SSascha Hauer 		print_hex_dump_debug("digested key@"__stringify(__LINE__)": ",
4096e005503SSascha Hauer 				     DUMP_PREFIX_ADDRESS, 16, 4, key,
4106e005503SSascha Hauer 				     digestsize, 1);
411045e3678SYuan Kang 	}
41230724445SHoria Geantă 	dma_unmap_single(jrdev, key_dma, *keylen, DMA_BIDIRECTIONAL);
413045e3678SYuan Kang 
414e11aa9f1SHoria Geanta 	*keylen = digestsize;
415e11aa9f1SHoria Geanta 
416045e3678SYuan Kang 	kfree(desc);
417045e3678SYuan Kang 
418045e3678SYuan Kang 	return ret;
419045e3678SYuan Kang }
420045e3678SYuan Kang 
421045e3678SYuan Kang static int ahash_setkey(struct crypto_ahash *ahash,
422045e3678SYuan Kang 			const u8 *key, unsigned int keylen)
423045e3678SYuan Kang {
424045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
4256e005503SSascha Hauer 	struct device *jrdev = ctx->jrdev;
426045e3678SYuan Kang 	int blocksize = crypto_tfm_alg_blocksize(&ahash->base);
427045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
4287e0880b9SHoria Geantă 	struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctx->jrdev->parent);
4299e6df0fdSMarkus Elfring 	int ret;
430045e3678SYuan Kang 	u8 *hashed_key = NULL;
431045e3678SYuan Kang 
4326e005503SSascha Hauer 	dev_dbg(jrdev, "keylen %d\n", keylen);
433045e3678SYuan Kang 
434045e3678SYuan Kang 	if (keylen > blocksize) {
43530724445SHoria Geantă 		hashed_key = kmemdup(key, keylen, GFP_KERNEL | GFP_DMA);
436045e3678SYuan Kang 		if (!hashed_key)
437045e3678SYuan Kang 			return -ENOMEM;
43830724445SHoria Geantă 		ret = hash_digest_key(ctx, &keylen, hashed_key, digestsize);
439045e3678SYuan Kang 		if (ret)
440d6e7a7d0SMarkus Elfring 			goto bad_free_key;
441045e3678SYuan Kang 		key = hashed_key;
442045e3678SYuan Kang 	}
443045e3678SYuan Kang 
4447e0880b9SHoria Geantă 	/*
4457e0880b9SHoria Geantă 	 * If DKP is supported, use it in the shared descriptor to generate
4467e0880b9SHoria Geantă 	 * the split key.
4477e0880b9SHoria Geantă 	 */
4487e0880b9SHoria Geantă 	if (ctrlpriv->era >= 6) {
4497e0880b9SHoria Geantă 		ctx->adata.key_inline = true;
4507e0880b9SHoria Geantă 		ctx->adata.keylen = keylen;
4517e0880b9SHoria Geantă 		ctx->adata.keylen_pad = split_key_len(ctx->adata.algtype &
4527e0880b9SHoria Geantă 						      OP_ALG_ALGSEL_MASK);
4537e0880b9SHoria Geantă 
4547e0880b9SHoria Geantă 		if (ctx->adata.keylen_pad > CAAM_MAX_HASH_KEY_SIZE)
455d6e7a7d0SMarkus Elfring 			goto bad_free_key;
456045e3678SYuan Kang 
4577e0880b9SHoria Geantă 		memcpy(ctx->key, key, keylen);
458e9b4913aSHoria Geantă 
459e9b4913aSHoria Geantă 		/*
460e9b4913aSHoria Geantă 		 * In case |user key| > |derived key|, using DKP<imm,imm>
461e9b4913aSHoria Geantă 		 * would result in invalid opcodes (last bytes of user key) in
462e9b4913aSHoria Geantă 		 * the resulting descriptor. Use DKP<ptr,imm> instead => both
463e9b4913aSHoria Geantă 		 * virtual and dma key addresses are needed.
464e9b4913aSHoria Geantă 		 */
465e9b4913aSHoria Geantă 		if (keylen > ctx->adata.keylen_pad)
466e9b4913aSHoria Geantă 			dma_sync_single_for_device(ctx->jrdev,
467e9b4913aSHoria Geantă 						   ctx->adata.key_dma,
468e9b4913aSHoria Geantă 						   ctx->adata.keylen_pad,
469e9b4913aSHoria Geantă 						   DMA_TO_DEVICE);
4707e0880b9SHoria Geantă 	} else {
4717e0880b9SHoria Geantă 		ret = gen_split_key(ctx->jrdev, ctx->key, &ctx->adata, key,
4727e0880b9SHoria Geantă 				    keylen, CAAM_MAX_HASH_KEY_SIZE);
4737e0880b9SHoria Geantă 		if (ret)
4747e0880b9SHoria Geantă 			goto bad_free_key;
4757e0880b9SHoria Geantă 	}
476045e3678SYuan Kang 
477045e3678SYuan Kang 	kfree(hashed_key);
478cfb725f6SHoria Geantă 	return ahash_set_sh_desc(ahash);
479d6e7a7d0SMarkus Elfring  bad_free_key:
480045e3678SYuan Kang 	kfree(hashed_key);
481045e3678SYuan Kang 	return -EINVAL;
482045e3678SYuan Kang }
483045e3678SYuan Kang 
48412b8567fSIuliana Prodan static int axcbc_setkey(struct crypto_ahash *ahash, const u8 *key,
48512b8567fSIuliana Prodan 			unsigned int keylen)
48612b8567fSIuliana Prodan {
48712b8567fSIuliana Prodan 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
48812b8567fSIuliana Prodan 	struct device *jrdev = ctx->jrdev;
48912b8567fSIuliana Prodan 
490674f368aSEric Biggers 	if (keylen != AES_KEYSIZE_128)
491836d8f43SIuliana Prodan 		return -EINVAL;
492836d8f43SIuliana Prodan 
49312b8567fSIuliana Prodan 	memcpy(ctx->key, key, keylen);
494a2fb864cSHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->adata.key_dma, keylen,
495a2fb864cSHoria Geantă 				   DMA_TO_DEVICE);
49612b8567fSIuliana Prodan 	ctx->adata.keylen = keylen;
49712b8567fSIuliana Prodan 
49812b8567fSIuliana Prodan 	print_hex_dump_debug("axcbc ctx.key@" __stringify(__LINE__)" : ",
49912b8567fSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, ctx->key, keylen, 1);
50012b8567fSIuliana Prodan 
50112b8567fSIuliana Prodan 	return axcbc_set_sh_desc(ahash);
50212b8567fSIuliana Prodan }
50387870cfbSIuliana Prodan 
50487870cfbSIuliana Prodan static int acmac_setkey(struct crypto_ahash *ahash, const u8 *key,
50587870cfbSIuliana Prodan 			unsigned int keylen)
50687870cfbSIuliana Prodan {
50787870cfbSIuliana Prodan 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
508836d8f43SIuliana Prodan 	int err;
509836d8f43SIuliana Prodan 
510836d8f43SIuliana Prodan 	err = aes_check_keylen(keylen);
511674f368aSEric Biggers 	if (err)
512836d8f43SIuliana Prodan 		return err;
51387870cfbSIuliana Prodan 
51487870cfbSIuliana Prodan 	/* key is immediate data for all cmac shared descriptors */
51587870cfbSIuliana Prodan 	ctx->adata.key_virt = key;
51687870cfbSIuliana Prodan 	ctx->adata.keylen = keylen;
51787870cfbSIuliana Prodan 
51887870cfbSIuliana Prodan 	print_hex_dump_debug("acmac ctx.key@" __stringify(__LINE__)" : ",
51987870cfbSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
52087870cfbSIuliana Prodan 
52187870cfbSIuliana Prodan 	return acmac_set_sh_desc(ahash);
52287870cfbSIuliana Prodan }
52387870cfbSIuliana Prodan 
524045e3678SYuan Kang /*
525045e3678SYuan Kang  * ahash_edesc - s/w-extended ahash descriptor
526045e3678SYuan Kang  * @sec4_sg_dma: physical mapped address of h/w link table
527045e3678SYuan Kang  * @src_nents: number of segments in input scatterlist
528045e3678SYuan Kang  * @sec4_sg_bytes: length of dma mapped sec4_sg space
52921b014f0SIuliana Prodan  * @bklog: stored to determine if the request needs backlog
530045e3678SYuan Kang  * @hw_desc: the h/w job descriptor followed by any referenced link tables
531343e44b1SRussell King  * @sec4_sg: h/w link table
532045e3678SYuan Kang  */
533045e3678SYuan Kang struct ahash_edesc {
534045e3678SYuan Kang 	dma_addr_t sec4_sg_dma;
535045e3678SYuan Kang 	int src_nents;
536045e3678SYuan Kang 	int sec4_sg_bytes;
53721b014f0SIuliana Prodan 	bool bklog;
5381a3daadcSAndrey Smirnov 	u32 hw_desc[DESC_JOB_IO_LEN_MAX / sizeof(u32)] ____cacheline_aligned;
5395a8a0765SGustavo A. R. Silva 	struct sec4_sg_entry sec4_sg[];
540045e3678SYuan Kang };
541045e3678SYuan Kang 
542045e3678SYuan Kang static inline void ahash_unmap(struct device *dev,
543045e3678SYuan Kang 			struct ahash_edesc *edesc,
544045e3678SYuan Kang 			struct ahash_request *req, int dst_len)
545045e3678SYuan Kang {
546944c3d4dSHoria Geantă 	struct caam_hash_state *state = ahash_request_ctx(req);
547944c3d4dSHoria Geantă 
548045e3678SYuan Kang 	if (edesc->src_nents)
54913fb8fd7SLABBE Corentin 		dma_unmap_sg(dev, req->src, edesc->src_nents, DMA_TO_DEVICE);
550045e3678SYuan Kang 
551045e3678SYuan Kang 	if (edesc->sec4_sg_bytes)
552045e3678SYuan Kang 		dma_unmap_single(dev, edesc->sec4_sg_dma,
553045e3678SYuan Kang 				 edesc->sec4_sg_bytes, DMA_TO_DEVICE);
554944c3d4dSHoria Geantă 
555944c3d4dSHoria Geantă 	if (state->buf_dma) {
55646b49abcSAndrei Botila 		dma_unmap_single(dev, state->buf_dma, state->buflen,
557944c3d4dSHoria Geantă 				 DMA_TO_DEVICE);
558944c3d4dSHoria Geantă 		state->buf_dma = 0;
559944c3d4dSHoria Geantă 	}
560045e3678SYuan Kang }
561045e3678SYuan Kang 
562045e3678SYuan Kang static inline void ahash_unmap_ctx(struct device *dev,
563045e3678SYuan Kang 			struct ahash_edesc *edesc,
564045e3678SYuan Kang 			struct ahash_request *req, int dst_len, u32 flag)
565045e3678SYuan Kang {
566045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
567045e3678SYuan Kang 
56887ec02e7SHoria Geantă 	if (state->ctx_dma) {
56965055e21SFranck LENORMAND 		dma_unmap_single(dev, state->ctx_dma, state->ctx_dma_len, flag);
57087ec02e7SHoria Geantă 		state->ctx_dma = 0;
57187ec02e7SHoria Geantă 	}
572045e3678SYuan Kang 	ahash_unmap(dev, edesc, req, dst_len);
573045e3678SYuan Kang }
574045e3678SYuan Kang 
575c3f7394eSIuliana Prodan static inline void ahash_done_cpy(struct device *jrdev, u32 *desc, u32 err,
576c3f7394eSIuliana Prodan 				  void *context, enum dma_data_direction dir)
577045e3678SYuan Kang {
578045e3678SYuan Kang 	struct ahash_request *req = context;
57921b014f0SIuliana Prodan 	struct caam_drv_private_jr *jrp = dev_get_drvdata(jrdev);
580045e3678SYuan Kang 	struct ahash_edesc *edesc;
581045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
582045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
583c19650d6SHoria Geantă 	struct caam_hash_state *state = ahash_request_ctx(req);
584045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
5851984aaeeSHoria Geantă 	int ecode = 0;
58663db32e6SIuliana Prodan 	bool has_bklog;
587045e3678SYuan Kang 
5886e005503SSascha Hauer 	dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
589045e3678SYuan Kang 
59021b014f0SIuliana Prodan 	edesc = state->edesc;
59163db32e6SIuliana Prodan 	has_bklog = edesc->bklog;
59221b014f0SIuliana Prodan 
593fa9659cdSMarek Vasut 	if (err)
5941984aaeeSHoria Geantă 		ecode = caam_jr_strstatus(jrdev, err);
595045e3678SYuan Kang 
596c3f7394eSIuliana Prodan 	ahash_unmap_ctx(jrdev, edesc, req, digestsize, dir);
597c19650d6SHoria Geantă 	memcpy(req->result, state->caam_ctx, digestsize);
598045e3678SYuan Kang 	kfree(edesc);
599045e3678SYuan Kang 
6006e005503SSascha Hauer 	print_hex_dump_debug("ctx@"__stringify(__LINE__)": ",
601045e3678SYuan Kang 			     DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
602045e3678SYuan Kang 			     ctx->ctx_len, 1);
603045e3678SYuan Kang 
60421b014f0SIuliana Prodan 	/*
60521b014f0SIuliana Prodan 	 * If no backlog flag, the completion of the request is done
60621b014f0SIuliana Prodan 	 * by CAAM, not crypto engine.
60721b014f0SIuliana Prodan 	 */
60863db32e6SIuliana Prodan 	if (!has_bklog)
6091984aaeeSHoria Geantă 		req->base.complete(&req->base, ecode);
61021b014f0SIuliana Prodan 	else
61121b014f0SIuliana Prodan 		crypto_finalize_hash_request(jrp->engine, req, ecode);
612045e3678SYuan Kang }
613045e3678SYuan Kang 
614c3f7394eSIuliana Prodan static void ahash_done(struct device *jrdev, u32 *desc, u32 err,
615c3f7394eSIuliana Prodan 		       void *context)
616c3f7394eSIuliana Prodan {
617c3f7394eSIuliana Prodan 	ahash_done_cpy(jrdev, desc, err, context, DMA_FROM_DEVICE);
618c3f7394eSIuliana Prodan }
619c3f7394eSIuliana Prodan 
620c3f7394eSIuliana Prodan static void ahash_done_ctx_src(struct device *jrdev, u32 *desc, u32 err,
621c3f7394eSIuliana Prodan 			       void *context)
622c3f7394eSIuliana Prodan {
623c3f7394eSIuliana Prodan 	ahash_done_cpy(jrdev, desc, err, context, DMA_BIDIRECTIONAL);
624c3f7394eSIuliana Prodan }
625c3f7394eSIuliana Prodan 
626c3f7394eSIuliana Prodan static inline void ahash_done_switch(struct device *jrdev, u32 *desc, u32 err,
627c3f7394eSIuliana Prodan 				     void *context, enum dma_data_direction dir)
628c3f7394eSIuliana Prodan {
629c3f7394eSIuliana Prodan 	struct ahash_request *req = context;
63021b014f0SIuliana Prodan 	struct caam_drv_private_jr *jrp = dev_get_drvdata(jrdev);
631c3f7394eSIuliana Prodan 	struct ahash_edesc *edesc;
632c3f7394eSIuliana Prodan 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
633c3f7394eSIuliana Prodan 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
634c3f7394eSIuliana Prodan 	struct caam_hash_state *state = ahash_request_ctx(req);
635c3f7394eSIuliana Prodan 	int digestsize = crypto_ahash_digestsize(ahash);
636c3f7394eSIuliana Prodan 	int ecode = 0;
63763db32e6SIuliana Prodan 	bool has_bklog;
638c3f7394eSIuliana Prodan 
639c3f7394eSIuliana Prodan 	dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
640c3f7394eSIuliana Prodan 
64121b014f0SIuliana Prodan 	edesc = state->edesc;
64263db32e6SIuliana Prodan 	has_bklog = edesc->bklog;
643c3f7394eSIuliana Prodan 	if (err)
644c3f7394eSIuliana Prodan 		ecode = caam_jr_strstatus(jrdev, err);
645c3f7394eSIuliana Prodan 
646c3f7394eSIuliana Prodan 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, dir);
647c3f7394eSIuliana Prodan 	kfree(edesc);
648c3f7394eSIuliana Prodan 
649c3f7394eSIuliana Prodan 	scatterwalk_map_and_copy(state->buf, req->src,
650c3f7394eSIuliana Prodan 				 req->nbytes - state->next_buflen,
651c3f7394eSIuliana Prodan 				 state->next_buflen, 0);
652c3f7394eSIuliana Prodan 	state->buflen = state->next_buflen;
653c3f7394eSIuliana Prodan 
654c3f7394eSIuliana Prodan 	print_hex_dump_debug("buf@" __stringify(__LINE__)": ",
655c3f7394eSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, state->buf,
656c3f7394eSIuliana Prodan 			     state->buflen, 1);
657c3f7394eSIuliana Prodan 
658c3f7394eSIuliana Prodan 	print_hex_dump_debug("ctx@"__stringify(__LINE__)": ",
659c3f7394eSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
660c3f7394eSIuliana Prodan 			     ctx->ctx_len, 1);
661c3f7394eSIuliana Prodan 	if (req->result)
662c3f7394eSIuliana Prodan 		print_hex_dump_debug("result@"__stringify(__LINE__)": ",
663c3f7394eSIuliana Prodan 				     DUMP_PREFIX_ADDRESS, 16, 4, req->result,
664c3f7394eSIuliana Prodan 				     digestsize, 1);
665c3f7394eSIuliana Prodan 
66621b014f0SIuliana Prodan 	/*
66721b014f0SIuliana Prodan 	 * If no backlog flag, the completion of the request is done
66821b014f0SIuliana Prodan 	 * by CAAM, not crypto engine.
66921b014f0SIuliana Prodan 	 */
67063db32e6SIuliana Prodan 	if (!has_bklog)
671c3f7394eSIuliana Prodan 		req->base.complete(&req->base, ecode);
67221b014f0SIuliana Prodan 	else
67321b014f0SIuliana Prodan 		crypto_finalize_hash_request(jrp->engine, req, ecode);
67421b014f0SIuliana Prodan 
675c3f7394eSIuliana Prodan }
676c3f7394eSIuliana Prodan 
677045e3678SYuan Kang static void ahash_done_bi(struct device *jrdev, u32 *desc, u32 err,
678045e3678SYuan Kang 			  void *context)
679045e3678SYuan Kang {
680c3f7394eSIuliana Prodan 	ahash_done_switch(jrdev, desc, err, context, DMA_BIDIRECTIONAL);
681045e3678SYuan Kang }
682045e3678SYuan Kang 
683045e3678SYuan Kang static void ahash_done_ctx_dst(struct device *jrdev, u32 *desc, u32 err,
684045e3678SYuan Kang 			       void *context)
685045e3678SYuan Kang {
686c3f7394eSIuliana Prodan 	ahash_done_switch(jrdev, desc, err, context, DMA_FROM_DEVICE);
687045e3678SYuan Kang }
688045e3678SYuan Kang 
6895588d039SRussell King /*
6905588d039SRussell King  * Allocate an enhanced descriptor, which contains the hardware descriptor
6915588d039SRussell King  * and space for hardware scatter table containing sg_num entries.
6925588d039SRussell King  */
6932ba1e798SIuliana Prodan static struct ahash_edesc *ahash_edesc_alloc(struct ahash_request *req,
69430a43b44SRussell King 					     int sg_num, u32 *sh_desc,
6952ba1e798SIuliana Prodan 					     dma_addr_t sh_desc_dma)
6965588d039SRussell King {
6972ba1e798SIuliana Prodan 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
6982ba1e798SIuliana Prodan 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
69921b014f0SIuliana Prodan 	struct caam_hash_state *state = ahash_request_ctx(req);
7002ba1e798SIuliana Prodan 	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
7012ba1e798SIuliana Prodan 		       GFP_KERNEL : GFP_ATOMIC;
7025588d039SRussell King 	struct ahash_edesc *edesc;
7035588d039SRussell King 	unsigned int sg_size = sg_num * sizeof(struct sec4_sg_entry);
7045588d039SRussell King 
7055588d039SRussell King 	edesc = kzalloc(sizeof(*edesc) + sg_size, GFP_DMA | flags);
7065588d039SRussell King 	if (!edesc) {
7075588d039SRussell King 		dev_err(ctx->jrdev, "could not allocate extended descriptor\n");
7085588d039SRussell King 		return NULL;
7095588d039SRussell King 	}
7105588d039SRussell King 
71121b014f0SIuliana Prodan 	state->edesc = edesc;
71221b014f0SIuliana Prodan 
71330a43b44SRussell King 	init_job_desc_shared(edesc->hw_desc, sh_desc_dma, desc_len(sh_desc),
71430a43b44SRussell King 			     HDR_SHARE_DEFER | HDR_REVERSE);
71530a43b44SRussell King 
7165588d039SRussell King 	return edesc;
7175588d039SRussell King }
7185588d039SRussell King 
71965cf164aSRussell King static int ahash_edesc_add_src(struct caam_hash_ctx *ctx,
72065cf164aSRussell King 			       struct ahash_edesc *edesc,
72165cf164aSRussell King 			       struct ahash_request *req, int nents,
72265cf164aSRussell King 			       unsigned int first_sg,
72365cf164aSRussell King 			       unsigned int first_bytes, size_t to_hash)
72465cf164aSRussell King {
72565cf164aSRussell King 	dma_addr_t src_dma;
72665cf164aSRussell King 	u32 options;
72765cf164aSRussell King 
72865cf164aSRussell King 	if (nents > 1 || first_sg) {
72965cf164aSRussell King 		struct sec4_sg_entry *sg = edesc->sec4_sg;
730a5e5c133SHoria Geantă 		unsigned int sgsize = sizeof(*sg) *
731a5e5c133SHoria Geantă 				      pad_sg_nents(first_sg + nents);
73265cf164aSRussell King 
733059d73eeSHoria Geantă 		sg_to_sec4_sg_last(req->src, to_hash, sg + first_sg, 0);
73465cf164aSRussell King 
73565cf164aSRussell King 		src_dma = dma_map_single(ctx->jrdev, sg, sgsize, DMA_TO_DEVICE);
73665cf164aSRussell King 		if (dma_mapping_error(ctx->jrdev, src_dma)) {
73765cf164aSRussell King 			dev_err(ctx->jrdev, "unable to map S/G table\n");
73865cf164aSRussell King 			return -ENOMEM;
73965cf164aSRussell King 		}
74065cf164aSRussell King 
74165cf164aSRussell King 		edesc->sec4_sg_bytes = sgsize;
74265cf164aSRussell King 		edesc->sec4_sg_dma = src_dma;
74365cf164aSRussell King 		options = LDST_SGF;
74465cf164aSRussell King 	} else {
74565cf164aSRussell King 		src_dma = sg_dma_address(req->src);
74665cf164aSRussell King 		options = 0;
74765cf164aSRussell King 	}
74865cf164aSRussell King 
74965cf164aSRussell King 	append_seq_in_ptr(edesc->hw_desc, src_dma, first_bytes + to_hash,
75065cf164aSRussell King 			  options);
75165cf164aSRussell King 
75265cf164aSRussell King 	return 0;
75365cf164aSRussell King }
75465cf164aSRussell King 
75521b014f0SIuliana Prodan static int ahash_do_one_req(struct crypto_engine *engine, void *areq)
75621b014f0SIuliana Prodan {
75721b014f0SIuliana Prodan 	struct ahash_request *req = ahash_request_cast(areq);
75821b014f0SIuliana Prodan 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
75921b014f0SIuliana Prodan 	struct caam_hash_state *state = ahash_request_ctx(req);
76021b014f0SIuliana Prodan 	struct device *jrdev = ctx->jrdev;
76121b014f0SIuliana Prodan 	u32 *desc = state->edesc->hw_desc;
76221b014f0SIuliana Prodan 	int ret;
76321b014f0SIuliana Prodan 
76421b014f0SIuliana Prodan 	state->edesc->bklog = true;
76521b014f0SIuliana Prodan 
76621b014f0SIuliana Prodan 	ret = caam_jr_enqueue(jrdev, desc, state->ahash_op_done, req);
76721b014f0SIuliana Prodan 
76821b014f0SIuliana Prodan 	if (ret != -EINPROGRESS) {
76921b014f0SIuliana Prodan 		ahash_unmap(jrdev, state->edesc, req, 0);
77021b014f0SIuliana Prodan 		kfree(state->edesc);
77121b014f0SIuliana Prodan 	} else {
77221b014f0SIuliana Prodan 		ret = 0;
77321b014f0SIuliana Prodan 	}
77421b014f0SIuliana Prodan 
77521b014f0SIuliana Prodan 	return ret;
77621b014f0SIuliana Prodan }
77721b014f0SIuliana Prodan 
77821b014f0SIuliana Prodan static int ahash_enqueue_req(struct device *jrdev,
77921b014f0SIuliana Prodan 			     void (*cbk)(struct device *jrdev, u32 *desc,
78021b014f0SIuliana Prodan 					 u32 err, void *context),
78121b014f0SIuliana Prodan 			     struct ahash_request *req,
78221b014f0SIuliana Prodan 			     int dst_len, enum dma_data_direction dir)
78321b014f0SIuliana Prodan {
78421b014f0SIuliana Prodan 	struct caam_drv_private_jr *jrpriv = dev_get_drvdata(jrdev);
78521b014f0SIuliana Prodan 	struct caam_hash_state *state = ahash_request_ctx(req);
78621b014f0SIuliana Prodan 	struct ahash_edesc *edesc = state->edesc;
78721b014f0SIuliana Prodan 	u32 *desc = edesc->hw_desc;
78821b014f0SIuliana Prodan 	int ret;
78921b014f0SIuliana Prodan 
79021b014f0SIuliana Prodan 	state->ahash_op_done = cbk;
79121b014f0SIuliana Prodan 
79221b014f0SIuliana Prodan 	/*
79321b014f0SIuliana Prodan 	 * Only the backlog request are sent to crypto-engine since the others
79421b014f0SIuliana Prodan 	 * can be handled by CAAM, if free, especially since JR has up to 1024
79521b014f0SIuliana Prodan 	 * entries (more than the 10 entries from crypto-engine).
79621b014f0SIuliana Prodan 	 */
79721b014f0SIuliana Prodan 	if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)
79821b014f0SIuliana Prodan 		ret = crypto_transfer_hash_request_to_engine(jrpriv->engine,
79921b014f0SIuliana Prodan 							     req);
80021b014f0SIuliana Prodan 	else
80121b014f0SIuliana Prodan 		ret = caam_jr_enqueue(jrdev, desc, cbk, req);
80221b014f0SIuliana Prodan 
80321b014f0SIuliana Prodan 	if ((ret != -EINPROGRESS) && (ret != -EBUSY)) {
80421b014f0SIuliana Prodan 		ahash_unmap_ctx(jrdev, edesc, req, dst_len, dir);
80521b014f0SIuliana Prodan 		kfree(edesc);
80621b014f0SIuliana Prodan 	}
80721b014f0SIuliana Prodan 
80821b014f0SIuliana Prodan 	return ret;
80921b014f0SIuliana Prodan }
81021b014f0SIuliana Prodan 
811045e3678SYuan Kang /* submit update job descriptor */
812045e3678SYuan Kang static int ahash_update_ctx(struct ahash_request *req)
813045e3678SYuan Kang {
814045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
815045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
816045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
817045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
81846b49abcSAndrei Botila 	u8 *buf = state->buf;
81946b49abcSAndrei Botila 	int *buflen = &state->buflen;
82046b49abcSAndrei Botila 	int *next_buflen = &state->next_buflen;
82112b8567fSIuliana Prodan 	int blocksize = crypto_ahash_blocksize(ahash);
822045e3678SYuan Kang 	int in_len = *buflen + req->nbytes, to_hash;
82330a43b44SRussell King 	u32 *desc;
824bc13c69eSRussell King 	int src_nents, mapped_nents, sec4_sg_bytes, sec4_sg_src_index;
825045e3678SYuan Kang 	struct ahash_edesc *edesc;
826045e3678SYuan Kang 	int ret = 0;
827045e3678SYuan Kang 
82812b8567fSIuliana Prodan 	*next_buflen = in_len & (blocksize - 1);
829045e3678SYuan Kang 	to_hash = in_len - *next_buflen;
830045e3678SYuan Kang 
83112b8567fSIuliana Prodan 	/*
83287870cfbSIuliana Prodan 	 * For XCBC and CMAC, if to_hash is multiple of block size,
83312b8567fSIuliana Prodan 	 * keep last block in internal buffer
83412b8567fSIuliana Prodan 	 */
83587870cfbSIuliana Prodan 	if ((is_xcbc_aes(ctx->adata.algtype) ||
83687870cfbSIuliana Prodan 	     is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize &&
83712b8567fSIuliana Prodan 	     (*next_buflen == 0)) {
83812b8567fSIuliana Prodan 		*next_buflen = blocksize;
83912b8567fSIuliana Prodan 		to_hash -= blocksize;
84012b8567fSIuliana Prodan 	}
84112b8567fSIuliana Prodan 
842045e3678SYuan Kang 	if (to_hash) {
843a5e5c133SHoria Geantă 		int pad_nents;
844059d73eeSHoria Geantă 		int src_len = req->nbytes - *next_buflen;
845a5e5c133SHoria Geantă 
846059d73eeSHoria Geantă 		src_nents = sg_nents_for_len(req->src, src_len);
847f9970c28SLABBE Corentin 		if (src_nents < 0) {
848f9970c28SLABBE Corentin 			dev_err(jrdev, "Invalid number of src SG.\n");
849f9970c28SLABBE Corentin 			return src_nents;
850f9970c28SLABBE Corentin 		}
851bc13c69eSRussell King 
852bc13c69eSRussell King 		if (src_nents) {
853bc13c69eSRussell King 			mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
854bc13c69eSRussell King 						  DMA_TO_DEVICE);
855bc13c69eSRussell King 			if (!mapped_nents) {
856bc13c69eSRussell King 				dev_err(jrdev, "unable to DMA map source\n");
857bc13c69eSRussell King 				return -ENOMEM;
858bc13c69eSRussell King 			}
859bc13c69eSRussell King 		} else {
860bc13c69eSRussell King 			mapped_nents = 0;
861bc13c69eSRussell King 		}
862bc13c69eSRussell King 
863045e3678SYuan Kang 		sec4_sg_src_index = 1 + (*buflen ? 1 : 0);
864a5e5c133SHoria Geantă 		pad_nents = pad_sg_nents(sec4_sg_src_index + mapped_nents);
865a5e5c133SHoria Geantă 		sec4_sg_bytes = pad_nents * sizeof(struct sec4_sg_entry);
866045e3678SYuan Kang 
867045e3678SYuan Kang 		/*
868045e3678SYuan Kang 		 * allocate space for base edesc and hw desc commands,
869045e3678SYuan Kang 		 * link tables
870045e3678SYuan Kang 		 */
8712ba1e798SIuliana Prodan 		edesc = ahash_edesc_alloc(req, pad_nents, ctx->sh_desc_update,
8722ba1e798SIuliana Prodan 					  ctx->sh_desc_update_dma);
873045e3678SYuan Kang 		if (!edesc) {
874bc13c69eSRussell King 			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
875045e3678SYuan Kang 			return -ENOMEM;
876045e3678SYuan Kang 		}
877045e3678SYuan Kang 
878045e3678SYuan Kang 		edesc->src_nents = src_nents;
879045e3678SYuan Kang 		edesc->sec4_sg_bytes = sec4_sg_bytes;
880045e3678SYuan Kang 
881dfcd8393SHoria Geantă 		ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
882045e3678SYuan Kang 					 edesc->sec4_sg, DMA_BIDIRECTIONAL);
883ce572085SHoria Geanta 		if (ret)
88458b0e5d0SMarkus Elfring 			goto unmap_ctx;
885045e3678SYuan Kang 
886944c3d4dSHoria Geantă 		ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
887944c3d4dSHoria Geantă 		if (ret)
888944c3d4dSHoria Geantă 			goto unmap_ctx;
889045e3678SYuan Kang 
890b4e9e931SIuliana Prodan 		if (mapped_nents)
891059d73eeSHoria Geantă 			sg_to_sec4_sg_last(req->src, src_len,
892bc13c69eSRussell King 					   edesc->sec4_sg + sec4_sg_src_index,
893bc13c69eSRussell King 					   0);
894b4e9e931SIuliana Prodan 		else
895b4e9e931SIuliana Prodan 			sg_to_sec4_set_last(edesc->sec4_sg + sec4_sg_src_index -
896b4e9e931SIuliana Prodan 					    1);
897b4e9e931SIuliana Prodan 
898045e3678SYuan Kang 		desc = edesc->hw_desc;
899045e3678SYuan Kang 
9001da2be33SRuchika Gupta 		edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
9011da2be33SRuchika Gupta 						     sec4_sg_bytes,
9021da2be33SRuchika Gupta 						     DMA_TO_DEVICE);
903ce572085SHoria Geanta 		if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
904ce572085SHoria Geanta 			dev_err(jrdev, "unable to map S/G table\n");
90532686d34SRussell King 			ret = -ENOMEM;
90658b0e5d0SMarkus Elfring 			goto unmap_ctx;
907ce572085SHoria Geanta 		}
9081da2be33SRuchika Gupta 
909045e3678SYuan Kang 		append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len +
910045e3678SYuan Kang 				       to_hash, LDST_SGF);
911045e3678SYuan Kang 
912045e3678SYuan Kang 		append_seq_out_ptr(desc, state->ctx_dma, ctx->ctx_len, 0);
913045e3678SYuan Kang 
9146e005503SSascha Hauer 		print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
915045e3678SYuan Kang 				     DUMP_PREFIX_ADDRESS, 16, 4, desc,
916045e3678SYuan Kang 				     desc_bytes(desc), 1);
917045e3678SYuan Kang 
91821b014f0SIuliana Prodan 		ret = ahash_enqueue_req(jrdev, ahash_done_bi, req,
91921b014f0SIuliana Prodan 					ctx->ctx_len, DMA_BIDIRECTIONAL);
920045e3678SYuan Kang 	} else if (*next_buflen) {
921307fd543SCristian Stoica 		scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
922307fd543SCristian Stoica 					 req->nbytes, 0);
923045e3678SYuan Kang 		*buflen = *next_buflen;
9246e005503SSascha Hauer 
9256e005503SSascha Hauer 		print_hex_dump_debug("buf@" __stringify(__LINE__)": ",
92646b49abcSAndrei Botila 				     DUMP_PREFIX_ADDRESS, 16, 4, buf,
92746b49abcSAndrei Botila 				     *buflen, 1);
92846b49abcSAndrei Botila 	}
929045e3678SYuan Kang 
930045e3678SYuan Kang 	return ret;
93158b0e5d0SMarkus Elfring unmap_ctx:
93232686d34SRussell King 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_BIDIRECTIONAL);
93332686d34SRussell King 	kfree(edesc);
93432686d34SRussell King 	return ret;
935045e3678SYuan Kang }
936045e3678SYuan Kang 
937045e3678SYuan Kang static int ahash_final_ctx(struct ahash_request *req)
938045e3678SYuan Kang {
939045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
940045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
941045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
942045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
94346b49abcSAndrei Botila 	int buflen = state->buflen;
94430a43b44SRussell King 	u32 *desc;
945a5e5c133SHoria Geantă 	int sec4_sg_bytes;
946045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
947045e3678SYuan Kang 	struct ahash_edesc *edesc;
9489e6df0fdSMarkus Elfring 	int ret;
949045e3678SYuan Kang 
950a5e5c133SHoria Geantă 	sec4_sg_bytes = pad_sg_nents(1 + (buflen ? 1 : 0)) *
951a5e5c133SHoria Geantă 			sizeof(struct sec4_sg_entry);
952045e3678SYuan Kang 
953045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
9542ba1e798SIuliana Prodan 	edesc = ahash_edesc_alloc(req, 4, ctx->sh_desc_fin,
9552ba1e798SIuliana Prodan 				  ctx->sh_desc_fin_dma);
9565588d039SRussell King 	if (!edesc)
957045e3678SYuan Kang 		return -ENOMEM;
958045e3678SYuan Kang 
959045e3678SYuan Kang 	desc = edesc->hw_desc;
960045e3678SYuan Kang 
961045e3678SYuan Kang 	edesc->sec4_sg_bytes = sec4_sg_bytes;
962045e3678SYuan Kang 
963dfcd8393SHoria Geantă 	ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
964c19650d6SHoria Geantă 				 edesc->sec4_sg, DMA_BIDIRECTIONAL);
965ce572085SHoria Geanta 	if (ret)
96658b0e5d0SMarkus Elfring 		goto unmap_ctx;
967045e3678SYuan Kang 
968944c3d4dSHoria Geantă 	ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
969944c3d4dSHoria Geantă 	if (ret)
970944c3d4dSHoria Geantă 		goto unmap_ctx;
971944c3d4dSHoria Geantă 
972a5e5c133SHoria Geantă 	sg_to_sec4_set_last(edesc->sec4_sg + (buflen ? 1 : 0));
973045e3678SYuan Kang 
9741da2be33SRuchika Gupta 	edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
9751da2be33SRuchika Gupta 					    sec4_sg_bytes, DMA_TO_DEVICE);
976ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
977ce572085SHoria Geanta 		dev_err(jrdev, "unable to map S/G table\n");
97832686d34SRussell King 		ret = -ENOMEM;
97958b0e5d0SMarkus Elfring 		goto unmap_ctx;
980ce572085SHoria Geanta 	}
9811da2be33SRuchika Gupta 
982045e3678SYuan Kang 	append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len + buflen,
983045e3678SYuan Kang 			  LDST_SGF);
984c19650d6SHoria Geantă 	append_seq_out_ptr(desc, state->ctx_dma, digestsize, 0);
985045e3678SYuan Kang 
9866e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
9876e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
9886e005503SSascha Hauer 			     1);
989045e3678SYuan Kang 
99021b014f0SIuliana Prodan 	return ahash_enqueue_req(jrdev, ahash_done_ctx_src, req,
99121b014f0SIuliana Prodan 				 digestsize, DMA_BIDIRECTIONAL);
99258b0e5d0SMarkus Elfring  unmap_ctx:
993c19650d6SHoria Geantă 	ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL);
994045e3678SYuan Kang 	kfree(edesc);
995045e3678SYuan Kang 	return ret;
996045e3678SYuan Kang }
997045e3678SYuan Kang 
998045e3678SYuan Kang static int ahash_finup_ctx(struct ahash_request *req)
999045e3678SYuan Kang {
1000045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1001045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1002045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1003045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
100446b49abcSAndrei Botila 	int buflen = state->buflen;
100530a43b44SRussell King 	u32 *desc;
100665cf164aSRussell King 	int sec4_sg_src_index;
1007bc13c69eSRussell King 	int src_nents, mapped_nents;
1008045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
1009045e3678SYuan Kang 	struct ahash_edesc *edesc;
10109e6df0fdSMarkus Elfring 	int ret;
1011045e3678SYuan Kang 
101213fb8fd7SLABBE Corentin 	src_nents = sg_nents_for_len(req->src, req->nbytes);
1013f9970c28SLABBE Corentin 	if (src_nents < 0) {
1014f9970c28SLABBE Corentin 		dev_err(jrdev, "Invalid number of src SG.\n");
1015f9970c28SLABBE Corentin 		return src_nents;
1016f9970c28SLABBE Corentin 	}
1017bc13c69eSRussell King 
1018bc13c69eSRussell King 	if (src_nents) {
1019bc13c69eSRussell King 		mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1020bc13c69eSRussell King 					  DMA_TO_DEVICE);
1021bc13c69eSRussell King 		if (!mapped_nents) {
1022bc13c69eSRussell King 			dev_err(jrdev, "unable to DMA map source\n");
1023bc13c69eSRussell King 			return -ENOMEM;
1024bc13c69eSRussell King 		}
1025bc13c69eSRussell King 	} else {
1026bc13c69eSRussell King 		mapped_nents = 0;
1027bc13c69eSRussell King 	}
1028bc13c69eSRussell King 
1029045e3678SYuan Kang 	sec4_sg_src_index = 1 + (buflen ? 1 : 0);
1030045e3678SYuan Kang 
1031045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
10322ba1e798SIuliana Prodan 	edesc = ahash_edesc_alloc(req, sec4_sg_src_index + mapped_nents,
10332ba1e798SIuliana Prodan 				  ctx->sh_desc_fin, ctx->sh_desc_fin_dma);
1034045e3678SYuan Kang 	if (!edesc) {
1035bc13c69eSRussell King 		dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1036045e3678SYuan Kang 		return -ENOMEM;
1037045e3678SYuan Kang 	}
1038045e3678SYuan Kang 
1039045e3678SYuan Kang 	desc = edesc->hw_desc;
1040045e3678SYuan Kang 
1041045e3678SYuan Kang 	edesc->src_nents = src_nents;
1042045e3678SYuan Kang 
1043dfcd8393SHoria Geantă 	ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
1044c19650d6SHoria Geantă 				 edesc->sec4_sg, DMA_BIDIRECTIONAL);
1045ce572085SHoria Geanta 	if (ret)
104658b0e5d0SMarkus Elfring 		goto unmap_ctx;
1047045e3678SYuan Kang 
1048944c3d4dSHoria Geantă 	ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
1049944c3d4dSHoria Geantă 	if (ret)
1050944c3d4dSHoria Geantă 		goto unmap_ctx;
1051045e3678SYuan Kang 
105265cf164aSRussell King 	ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents,
105365cf164aSRussell King 				  sec4_sg_src_index, ctx->ctx_len + buflen,
105465cf164aSRussell King 				  req->nbytes);
105565cf164aSRussell King 	if (ret)
105658b0e5d0SMarkus Elfring 		goto unmap_ctx;
1057045e3678SYuan Kang 
1058c19650d6SHoria Geantă 	append_seq_out_ptr(desc, state->ctx_dma, digestsize, 0);
1059045e3678SYuan Kang 
10606e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
10616e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
10626e005503SSascha Hauer 			     1);
1063045e3678SYuan Kang 
106421b014f0SIuliana Prodan 	return ahash_enqueue_req(jrdev, ahash_done_ctx_src, req,
106521b014f0SIuliana Prodan 				 digestsize, DMA_BIDIRECTIONAL);
106658b0e5d0SMarkus Elfring  unmap_ctx:
1067c19650d6SHoria Geantă 	ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL);
1068045e3678SYuan Kang 	kfree(edesc);
1069045e3678SYuan Kang 	return ret;
1070045e3678SYuan Kang }
1071045e3678SYuan Kang 
1072045e3678SYuan Kang static int ahash_digest(struct ahash_request *req)
1073045e3678SYuan Kang {
1074045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1075045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1076944c3d4dSHoria Geantă 	struct caam_hash_state *state = ahash_request_ctx(req);
1077045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
107830a43b44SRussell King 	u32 *desc;
1079045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
108065cf164aSRussell King 	int src_nents, mapped_nents;
1081045e3678SYuan Kang 	struct ahash_edesc *edesc;
10829e6df0fdSMarkus Elfring 	int ret;
1083045e3678SYuan Kang 
1084944c3d4dSHoria Geantă 	state->buf_dma = 0;
1085944c3d4dSHoria Geantă 
10863d5a2db6SRussell King 	src_nents = sg_nents_for_len(req->src, req->nbytes);
1087f9970c28SLABBE Corentin 	if (src_nents < 0) {
1088f9970c28SLABBE Corentin 		dev_err(jrdev, "Invalid number of src SG.\n");
1089f9970c28SLABBE Corentin 		return src_nents;
1090f9970c28SLABBE Corentin 	}
1091bc13c69eSRussell King 
1092bc13c69eSRussell King 	if (src_nents) {
1093bc13c69eSRussell King 		mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1094bc13c69eSRussell King 					  DMA_TO_DEVICE);
1095bc13c69eSRussell King 		if (!mapped_nents) {
1096bc13c69eSRussell King 			dev_err(jrdev, "unable to map source for DMA\n");
1097bc13c69eSRussell King 			return -ENOMEM;
1098bc13c69eSRussell King 		}
1099bc13c69eSRussell King 	} else {
1100bc13c69eSRussell King 		mapped_nents = 0;
1101bc13c69eSRussell King 	}
1102bc13c69eSRussell King 
1103045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
11042ba1e798SIuliana Prodan 	edesc = ahash_edesc_alloc(req, mapped_nents > 1 ? mapped_nents : 0,
11052ba1e798SIuliana Prodan 				  ctx->sh_desc_digest, ctx->sh_desc_digest_dma);
1106045e3678SYuan Kang 	if (!edesc) {
1107bc13c69eSRussell King 		dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1108045e3678SYuan Kang 		return -ENOMEM;
1109045e3678SYuan Kang 	}
1110343e44b1SRussell King 
1111045e3678SYuan Kang 	edesc->src_nents = src_nents;
1112045e3678SYuan Kang 
111365cf164aSRussell King 	ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 0, 0,
111465cf164aSRussell King 				  req->nbytes);
111565cf164aSRussell King 	if (ret) {
111632686d34SRussell King 		ahash_unmap(jrdev, edesc, req, digestsize);
111732686d34SRussell King 		kfree(edesc);
111865cf164aSRussell King 		return ret;
1119ce572085SHoria Geanta 	}
112065cf164aSRussell King 
112165cf164aSRussell King 	desc = edesc->hw_desc;
1122045e3678SYuan Kang 
1123c19650d6SHoria Geantă 	ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
1124c19650d6SHoria Geantă 	if (ret) {
112532686d34SRussell King 		ahash_unmap(jrdev, edesc, req, digestsize);
112632686d34SRussell King 		kfree(edesc);
1127ce572085SHoria Geanta 		return -ENOMEM;
1128ce572085SHoria Geanta 	}
1129045e3678SYuan Kang 
11306e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
11316e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
11326e005503SSascha Hauer 			     1);
1133045e3678SYuan Kang 
113421b014f0SIuliana Prodan 	return ahash_enqueue_req(jrdev, ahash_done, req, digestsize,
113521b014f0SIuliana Prodan 				 DMA_FROM_DEVICE);
1136045e3678SYuan Kang }
1137045e3678SYuan Kang 
1138045e3678SYuan Kang /* submit ahash final if it the first job descriptor */
1139045e3678SYuan Kang static int ahash_final_no_ctx(struct ahash_request *req)
1140045e3678SYuan Kang {
1141045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1142045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1143045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1144045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
114546b49abcSAndrei Botila 	u8 *buf = state->buf;
114646b49abcSAndrei Botila 	int buflen = state->buflen;
114730a43b44SRussell King 	u32 *desc;
1148045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
1149045e3678SYuan Kang 	struct ahash_edesc *edesc;
11509e6df0fdSMarkus Elfring 	int ret;
1151045e3678SYuan Kang 
1152045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
11532ba1e798SIuliana Prodan 	edesc = ahash_edesc_alloc(req, 0, ctx->sh_desc_digest,
11542ba1e798SIuliana Prodan 				  ctx->sh_desc_digest_dma);
11555588d039SRussell King 	if (!edesc)
1156045e3678SYuan Kang 		return -ENOMEM;
1157045e3678SYuan Kang 
1158045e3678SYuan Kang 	desc = edesc->hw_desc;
1159045e3678SYuan Kang 
116004e6d25cSAymen Sghaier 	if (buflen) {
116104e6d25cSAymen Sghaier 		state->buf_dma = dma_map_single(jrdev, buf, buflen,
116204e6d25cSAymen Sghaier 						DMA_TO_DEVICE);
1163ce572085SHoria Geanta 		if (dma_mapping_error(jrdev, state->buf_dma)) {
1164ce572085SHoria Geanta 			dev_err(jrdev, "unable to map src\n");
116506435f34SMarkus Elfring 			goto unmap;
1166ce572085SHoria Geanta 		}
1167045e3678SYuan Kang 
1168045e3678SYuan Kang 		append_seq_in_ptr(desc, state->buf_dma, buflen, 0);
116904e6d25cSAymen Sghaier 	}
1170045e3678SYuan Kang 
1171c19650d6SHoria Geantă 	ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
1172c19650d6SHoria Geantă 	if (ret)
117306435f34SMarkus Elfring 		goto unmap;
1174045e3678SYuan Kang 
11756e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
11766e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
11776e005503SSascha Hauer 			     1);
1178045e3678SYuan Kang 
117921b014f0SIuliana Prodan 	return ahash_enqueue_req(jrdev, ahash_done, req,
118021b014f0SIuliana Prodan 				 digestsize, DMA_FROM_DEVICE);
118106435f34SMarkus Elfring  unmap:
118206435f34SMarkus Elfring 	ahash_unmap(jrdev, edesc, req, digestsize);
118306435f34SMarkus Elfring 	kfree(edesc);
118406435f34SMarkus Elfring 	return -ENOMEM;
1185045e3678SYuan Kang }
1186045e3678SYuan Kang 
1187045e3678SYuan Kang /* submit ahash update if it the first job descriptor after update */
1188045e3678SYuan Kang static int ahash_update_no_ctx(struct ahash_request *req)
1189045e3678SYuan Kang {
1190045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1191045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1192045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1193045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
119446b49abcSAndrei Botila 	u8 *buf = state->buf;
119546b49abcSAndrei Botila 	int *buflen = &state->buflen;
119646b49abcSAndrei Botila 	int *next_buflen = &state->next_buflen;
119712b8567fSIuliana Prodan 	int blocksize = crypto_ahash_blocksize(ahash);
1198045e3678SYuan Kang 	int in_len = *buflen + req->nbytes, to_hash;
1199bc13c69eSRussell King 	int sec4_sg_bytes, src_nents, mapped_nents;
1200045e3678SYuan Kang 	struct ahash_edesc *edesc;
120130a43b44SRussell King 	u32 *desc;
1202045e3678SYuan Kang 	int ret = 0;
1203045e3678SYuan Kang 
120412b8567fSIuliana Prodan 	*next_buflen = in_len & (blocksize - 1);
1205045e3678SYuan Kang 	to_hash = in_len - *next_buflen;
1206045e3678SYuan Kang 
120712b8567fSIuliana Prodan 	/*
120887870cfbSIuliana Prodan 	 * For XCBC and CMAC, if to_hash is multiple of block size,
120912b8567fSIuliana Prodan 	 * keep last block in internal buffer
121012b8567fSIuliana Prodan 	 */
121187870cfbSIuliana Prodan 	if ((is_xcbc_aes(ctx->adata.algtype) ||
121287870cfbSIuliana Prodan 	     is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize &&
121312b8567fSIuliana Prodan 	     (*next_buflen == 0)) {
121412b8567fSIuliana Prodan 		*next_buflen = blocksize;
121512b8567fSIuliana Prodan 		to_hash -= blocksize;
121612b8567fSIuliana Prodan 	}
121712b8567fSIuliana Prodan 
1218045e3678SYuan Kang 	if (to_hash) {
1219a5e5c133SHoria Geantă 		int pad_nents;
1220059d73eeSHoria Geantă 		int src_len = req->nbytes - *next_buflen;
1221a5e5c133SHoria Geantă 
1222059d73eeSHoria Geantă 		src_nents = sg_nents_for_len(req->src, src_len);
1223f9970c28SLABBE Corentin 		if (src_nents < 0) {
1224f9970c28SLABBE Corentin 			dev_err(jrdev, "Invalid number of src SG.\n");
1225f9970c28SLABBE Corentin 			return src_nents;
1226f9970c28SLABBE Corentin 		}
1227bc13c69eSRussell King 
1228bc13c69eSRussell King 		if (src_nents) {
1229bc13c69eSRussell King 			mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1230bc13c69eSRussell King 						  DMA_TO_DEVICE);
1231bc13c69eSRussell King 			if (!mapped_nents) {
1232bc13c69eSRussell King 				dev_err(jrdev, "unable to DMA map source\n");
1233bc13c69eSRussell King 				return -ENOMEM;
1234bc13c69eSRussell King 			}
1235bc13c69eSRussell King 		} else {
1236bc13c69eSRussell King 			mapped_nents = 0;
1237bc13c69eSRussell King 		}
1238bc13c69eSRussell King 
1239a5e5c133SHoria Geantă 		pad_nents = pad_sg_nents(1 + mapped_nents);
1240a5e5c133SHoria Geantă 		sec4_sg_bytes = pad_nents * sizeof(struct sec4_sg_entry);
1241045e3678SYuan Kang 
1242045e3678SYuan Kang 		/*
1243045e3678SYuan Kang 		 * allocate space for base edesc and hw desc commands,
1244045e3678SYuan Kang 		 * link tables
1245045e3678SYuan Kang 		 */
12462ba1e798SIuliana Prodan 		edesc = ahash_edesc_alloc(req, pad_nents,
124730a43b44SRussell King 					  ctx->sh_desc_update_first,
12482ba1e798SIuliana Prodan 					  ctx->sh_desc_update_first_dma);
1249045e3678SYuan Kang 		if (!edesc) {
1250bc13c69eSRussell King 			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1251045e3678SYuan Kang 			return -ENOMEM;
1252045e3678SYuan Kang 		}
1253045e3678SYuan Kang 
1254045e3678SYuan Kang 		edesc->src_nents = src_nents;
1255045e3678SYuan Kang 		edesc->sec4_sg_bytes = sec4_sg_bytes;
1256045e3678SYuan Kang 
1257944c3d4dSHoria Geantă 		ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg, state);
1258944c3d4dSHoria Geantă 		if (ret)
1259944c3d4dSHoria Geantă 			goto unmap_ctx;
1260944c3d4dSHoria Geantă 
1261059d73eeSHoria Geantă 		sg_to_sec4_sg_last(req->src, src_len, edesc->sec4_sg + 1, 0);
1262bc13c69eSRussell King 
1263045e3678SYuan Kang 		desc = edesc->hw_desc;
1264045e3678SYuan Kang 
12651da2be33SRuchika Gupta 		edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
12661da2be33SRuchika Gupta 						    sec4_sg_bytes,
12671da2be33SRuchika Gupta 						    DMA_TO_DEVICE);
1268ce572085SHoria Geanta 		if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
1269ce572085SHoria Geanta 			dev_err(jrdev, "unable to map S/G table\n");
127032686d34SRussell King 			ret = -ENOMEM;
127158b0e5d0SMarkus Elfring 			goto unmap_ctx;
1272ce572085SHoria Geanta 		}
12731da2be33SRuchika Gupta 
1274045e3678SYuan Kang 		append_seq_in_ptr(desc, edesc->sec4_sg_dma, to_hash, LDST_SGF);
1275045e3678SYuan Kang 
1276ce572085SHoria Geanta 		ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len);
1277ce572085SHoria Geanta 		if (ret)
127858b0e5d0SMarkus Elfring 			goto unmap_ctx;
1279045e3678SYuan Kang 
12806e005503SSascha Hauer 		print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
1281045e3678SYuan Kang 				     DUMP_PREFIX_ADDRESS, 16, 4, desc,
1282045e3678SYuan Kang 				     desc_bytes(desc), 1);
1283045e3678SYuan Kang 
128421b014f0SIuliana Prodan 		ret = ahash_enqueue_req(jrdev, ahash_done_ctx_dst, req,
128521b014f0SIuliana Prodan 					ctx->ctx_len, DMA_TO_DEVICE);
128621b014f0SIuliana Prodan 		if ((ret != -EINPROGRESS) && (ret != -EBUSY))
128721b014f0SIuliana Prodan 			return ret;
1288045e3678SYuan Kang 		state->update = ahash_update_ctx;
1289045e3678SYuan Kang 		state->finup = ahash_finup_ctx;
1290045e3678SYuan Kang 		state->final = ahash_final_ctx;
1291045e3678SYuan Kang 	} else if (*next_buflen) {
1292307fd543SCristian Stoica 		scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
1293307fd543SCristian Stoica 					 req->nbytes, 0);
1294045e3678SYuan Kang 		*buflen = *next_buflen;
12956e005503SSascha Hauer 
12966e005503SSascha Hauer 		print_hex_dump_debug("buf@" __stringify(__LINE__)": ",
129746b49abcSAndrei Botila 				     DUMP_PREFIX_ADDRESS, 16, 4, buf,
129846b49abcSAndrei Botila 				     *buflen, 1);
129946b49abcSAndrei Botila 	}
1300045e3678SYuan Kang 
1301045e3678SYuan Kang 	return ret;
130258b0e5d0SMarkus Elfring  unmap_ctx:
130332686d34SRussell King 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE);
130432686d34SRussell King 	kfree(edesc);
130532686d34SRussell King 	return ret;
1306045e3678SYuan Kang }
1307045e3678SYuan Kang 
1308045e3678SYuan Kang /* submit ahash finup if it the first job descriptor after update */
1309045e3678SYuan Kang static int ahash_finup_no_ctx(struct ahash_request *req)
1310045e3678SYuan Kang {
1311045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1312045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1313045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1314045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
131546b49abcSAndrei Botila 	int buflen = state->buflen;
131630a43b44SRussell King 	u32 *desc;
1317bc13c69eSRussell King 	int sec4_sg_bytes, sec4_sg_src_index, src_nents, mapped_nents;
1318045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
1319045e3678SYuan Kang 	struct ahash_edesc *edesc;
13209e6df0fdSMarkus Elfring 	int ret;
1321045e3678SYuan Kang 
132213fb8fd7SLABBE Corentin 	src_nents = sg_nents_for_len(req->src, req->nbytes);
1323f9970c28SLABBE Corentin 	if (src_nents < 0) {
1324f9970c28SLABBE Corentin 		dev_err(jrdev, "Invalid number of src SG.\n");
1325f9970c28SLABBE Corentin 		return src_nents;
1326f9970c28SLABBE Corentin 	}
1327bc13c69eSRussell King 
1328bc13c69eSRussell King 	if (src_nents) {
1329bc13c69eSRussell King 		mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1330bc13c69eSRussell King 					  DMA_TO_DEVICE);
1331bc13c69eSRussell King 		if (!mapped_nents) {
1332bc13c69eSRussell King 			dev_err(jrdev, "unable to DMA map source\n");
1333bc13c69eSRussell King 			return -ENOMEM;
1334bc13c69eSRussell King 		}
1335bc13c69eSRussell King 	} else {
1336bc13c69eSRussell King 		mapped_nents = 0;
1337bc13c69eSRussell King 	}
1338bc13c69eSRussell King 
1339045e3678SYuan Kang 	sec4_sg_src_index = 2;
1340bc13c69eSRussell King 	sec4_sg_bytes = (sec4_sg_src_index + mapped_nents) *
1341045e3678SYuan Kang 			 sizeof(struct sec4_sg_entry);
1342045e3678SYuan Kang 
1343045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
13442ba1e798SIuliana Prodan 	edesc = ahash_edesc_alloc(req, sec4_sg_src_index + mapped_nents,
13452ba1e798SIuliana Prodan 				  ctx->sh_desc_digest, ctx->sh_desc_digest_dma);
1346045e3678SYuan Kang 	if (!edesc) {
1347bc13c69eSRussell King 		dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1348045e3678SYuan Kang 		return -ENOMEM;
1349045e3678SYuan Kang 	}
1350045e3678SYuan Kang 
1351045e3678SYuan Kang 	desc = edesc->hw_desc;
1352045e3678SYuan Kang 
1353045e3678SYuan Kang 	edesc->src_nents = src_nents;
1354045e3678SYuan Kang 	edesc->sec4_sg_bytes = sec4_sg_bytes;
1355045e3678SYuan Kang 
1356944c3d4dSHoria Geantă 	ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg, state);
1357944c3d4dSHoria Geantă 	if (ret)
1358944c3d4dSHoria Geantă 		goto unmap;
1359045e3678SYuan Kang 
136065cf164aSRussell King 	ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 1, buflen,
136165cf164aSRussell King 				  req->nbytes);
136265cf164aSRussell King 	if (ret) {
1363ce572085SHoria Geanta 		dev_err(jrdev, "unable to map S/G table\n");
136406435f34SMarkus Elfring 		goto unmap;
1365ce572085SHoria Geanta 	}
13661da2be33SRuchika Gupta 
1367c19650d6SHoria Geantă 	ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
1368c19650d6SHoria Geantă 	if (ret)
136906435f34SMarkus Elfring 		goto unmap;
1370045e3678SYuan Kang 
13716e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
13726e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
13736e005503SSascha Hauer 			     1);
1374045e3678SYuan Kang 
137521b014f0SIuliana Prodan 	return ahash_enqueue_req(jrdev, ahash_done, req,
137621b014f0SIuliana Prodan 				 digestsize, DMA_FROM_DEVICE);
137706435f34SMarkus Elfring  unmap:
137806435f34SMarkus Elfring 	ahash_unmap(jrdev, edesc, req, digestsize);
137906435f34SMarkus Elfring 	kfree(edesc);
138006435f34SMarkus Elfring 	return -ENOMEM;
138106435f34SMarkus Elfring 
1382045e3678SYuan Kang }
1383045e3678SYuan Kang 
1384045e3678SYuan Kang /* submit first update job descriptor after init */
1385045e3678SYuan Kang static int ahash_update_first(struct ahash_request *req)
1386045e3678SYuan Kang {
1387045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1388045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1389045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1390045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
139146b49abcSAndrei Botila 	u8 *buf = state->buf;
139246b49abcSAndrei Botila 	int *buflen = &state->buflen;
139346b49abcSAndrei Botila 	int *next_buflen = &state->next_buflen;
1394045e3678SYuan Kang 	int to_hash;
139512b8567fSIuliana Prodan 	int blocksize = crypto_ahash_blocksize(ahash);
139630a43b44SRussell King 	u32 *desc;
139765cf164aSRussell King 	int src_nents, mapped_nents;
1398045e3678SYuan Kang 	struct ahash_edesc *edesc;
1399045e3678SYuan Kang 	int ret = 0;
1400045e3678SYuan Kang 
140112b8567fSIuliana Prodan 	*next_buflen = req->nbytes & (blocksize - 1);
1402045e3678SYuan Kang 	to_hash = req->nbytes - *next_buflen;
1403045e3678SYuan Kang 
140412b8567fSIuliana Prodan 	/*
140587870cfbSIuliana Prodan 	 * For XCBC and CMAC, if to_hash is multiple of block size,
140612b8567fSIuliana Prodan 	 * keep last block in internal buffer
140712b8567fSIuliana Prodan 	 */
140887870cfbSIuliana Prodan 	if ((is_xcbc_aes(ctx->adata.algtype) ||
140987870cfbSIuliana Prodan 	     is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize &&
141012b8567fSIuliana Prodan 	     (*next_buflen == 0)) {
141112b8567fSIuliana Prodan 		*next_buflen = blocksize;
141212b8567fSIuliana Prodan 		to_hash -= blocksize;
141312b8567fSIuliana Prodan 	}
141412b8567fSIuliana Prodan 
1415045e3678SYuan Kang 	if (to_hash) {
14163d5a2db6SRussell King 		src_nents = sg_nents_for_len(req->src,
14173d5a2db6SRussell King 					     req->nbytes - *next_buflen);
1418f9970c28SLABBE Corentin 		if (src_nents < 0) {
1419f9970c28SLABBE Corentin 			dev_err(jrdev, "Invalid number of src SG.\n");
1420f9970c28SLABBE Corentin 			return src_nents;
1421f9970c28SLABBE Corentin 		}
1422bc13c69eSRussell King 
1423bc13c69eSRussell King 		if (src_nents) {
1424bc13c69eSRussell King 			mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1425bc13c69eSRussell King 						  DMA_TO_DEVICE);
1426bc13c69eSRussell King 			if (!mapped_nents) {
1427bc13c69eSRussell King 				dev_err(jrdev, "unable to map source for DMA\n");
1428bc13c69eSRussell King 				return -ENOMEM;
1429bc13c69eSRussell King 			}
1430bc13c69eSRussell King 		} else {
1431bc13c69eSRussell King 			mapped_nents = 0;
1432bc13c69eSRussell King 		}
1433045e3678SYuan Kang 
1434045e3678SYuan Kang 		/*
1435045e3678SYuan Kang 		 * allocate space for base edesc and hw desc commands,
1436045e3678SYuan Kang 		 * link tables
1437045e3678SYuan Kang 		 */
14382ba1e798SIuliana Prodan 		edesc = ahash_edesc_alloc(req, mapped_nents > 1 ?
143930a43b44SRussell King 					  mapped_nents : 0,
144030a43b44SRussell King 					  ctx->sh_desc_update_first,
14412ba1e798SIuliana Prodan 					  ctx->sh_desc_update_first_dma);
1442045e3678SYuan Kang 		if (!edesc) {
1443bc13c69eSRussell King 			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1444045e3678SYuan Kang 			return -ENOMEM;
1445045e3678SYuan Kang 		}
1446045e3678SYuan Kang 
1447045e3678SYuan Kang 		edesc->src_nents = src_nents;
1448045e3678SYuan Kang 
144965cf164aSRussell King 		ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 0, 0,
145065cf164aSRussell King 					  to_hash);
145165cf164aSRussell King 		if (ret)
145258b0e5d0SMarkus Elfring 			goto unmap_ctx;
1453045e3678SYuan Kang 
1454045e3678SYuan Kang 		desc = edesc->hw_desc;
1455045e3678SYuan Kang 
1456ce572085SHoria Geanta 		ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len);
1457ce572085SHoria Geanta 		if (ret)
145858b0e5d0SMarkus Elfring 			goto unmap_ctx;
1459045e3678SYuan Kang 
14606e005503SSascha Hauer 		print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
1461045e3678SYuan Kang 				     DUMP_PREFIX_ADDRESS, 16, 4, desc,
1462045e3678SYuan Kang 				     desc_bytes(desc), 1);
1463045e3678SYuan Kang 
146421b014f0SIuliana Prodan 		ret = ahash_enqueue_req(jrdev, ahash_done_ctx_dst, req,
146521b014f0SIuliana Prodan 					ctx->ctx_len, DMA_TO_DEVICE);
146621b014f0SIuliana Prodan 		if ((ret != -EINPROGRESS) && (ret != -EBUSY))
146721b014f0SIuliana Prodan 			return ret;
1468045e3678SYuan Kang 		state->update = ahash_update_ctx;
1469045e3678SYuan Kang 		state->finup = ahash_finup_ctx;
1470045e3678SYuan Kang 		state->final = ahash_final_ctx;
1471045e3678SYuan Kang 	} else if (*next_buflen) {
1472045e3678SYuan Kang 		state->update = ahash_update_no_ctx;
1473045e3678SYuan Kang 		state->finup = ahash_finup_no_ctx;
1474045e3678SYuan Kang 		state->final = ahash_final_no_ctx;
147546b49abcSAndrei Botila 		scatterwalk_map_and_copy(buf, req->src, 0,
1476307fd543SCristian Stoica 					 req->nbytes, 0);
147746b49abcSAndrei Botila 		*buflen = *next_buflen;
14786e005503SSascha Hauer 
147946b49abcSAndrei Botila 		print_hex_dump_debug("buf@" __stringify(__LINE__)": ",
148046b49abcSAndrei Botila 				     DUMP_PREFIX_ADDRESS, 16, 4, buf,
148146b49abcSAndrei Botila 				     *buflen, 1);
148246b49abcSAndrei Botila 	}
1483045e3678SYuan Kang 
1484045e3678SYuan Kang 	return ret;
148558b0e5d0SMarkus Elfring  unmap_ctx:
148632686d34SRussell King 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE);
148732686d34SRussell King 	kfree(edesc);
148832686d34SRussell King 	return ret;
1489045e3678SYuan Kang }
1490045e3678SYuan Kang 
1491045e3678SYuan Kang static int ahash_finup_first(struct ahash_request *req)
1492045e3678SYuan Kang {
1493045e3678SYuan Kang 	return ahash_digest(req);
1494045e3678SYuan Kang }
1495045e3678SYuan Kang 
1496045e3678SYuan Kang static int ahash_init(struct ahash_request *req)
1497045e3678SYuan Kang {
1498045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1499045e3678SYuan Kang 
1500045e3678SYuan Kang 	state->update = ahash_update_first;
1501045e3678SYuan Kang 	state->finup = ahash_finup_first;
1502045e3678SYuan Kang 	state->final = ahash_final_no_ctx;
1503045e3678SYuan Kang 
150487ec02e7SHoria Geantă 	state->ctx_dma = 0;
150565055e21SFranck LENORMAND 	state->ctx_dma_len = 0;
1506de0e35ecSHoria Geanta 	state->buf_dma = 0;
150746b49abcSAndrei Botila 	state->buflen = 0;
150846b49abcSAndrei Botila 	state->next_buflen = 0;
1509045e3678SYuan Kang 
1510045e3678SYuan Kang 	return 0;
1511045e3678SYuan Kang }
1512045e3678SYuan Kang 
1513045e3678SYuan Kang static int ahash_update(struct ahash_request *req)
1514045e3678SYuan Kang {
1515045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1516045e3678SYuan Kang 
1517045e3678SYuan Kang 	return state->update(req);
1518045e3678SYuan Kang }
1519045e3678SYuan Kang 
1520045e3678SYuan Kang static int ahash_finup(struct ahash_request *req)
1521045e3678SYuan Kang {
1522045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1523045e3678SYuan Kang 
1524045e3678SYuan Kang 	return state->finup(req);
1525045e3678SYuan Kang }
1526045e3678SYuan Kang 
1527045e3678SYuan Kang static int ahash_final(struct ahash_request *req)
1528045e3678SYuan Kang {
1529045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1530045e3678SYuan Kang 
1531045e3678SYuan Kang 	return state->final(req);
1532045e3678SYuan Kang }
1533045e3678SYuan Kang 
1534045e3678SYuan Kang static int ahash_export(struct ahash_request *req, void *out)
1535045e3678SYuan Kang {
1536045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
15375ec90831SRussell King 	struct caam_export_state *export = out;
153846b49abcSAndrei Botila 	u8 *buf = state->buf;
153946b49abcSAndrei Botila 	int len = state->buflen;
15405ec90831SRussell King 
15415ec90831SRussell King 	memcpy(export->buf, buf, len);
15425ec90831SRussell King 	memcpy(export->caam_ctx, state->caam_ctx, sizeof(export->caam_ctx));
15435ec90831SRussell King 	export->buflen = len;
15445ec90831SRussell King 	export->update = state->update;
15455ec90831SRussell King 	export->final = state->final;
15465ec90831SRussell King 	export->finup = state->finup;
1547434b4212SRussell King 
1548045e3678SYuan Kang 	return 0;
1549045e3678SYuan Kang }
1550045e3678SYuan Kang 
1551045e3678SYuan Kang static int ahash_import(struct ahash_request *req, const void *in)
1552045e3678SYuan Kang {
1553045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
15545ec90831SRussell King 	const struct caam_export_state *export = in;
1555045e3678SYuan Kang 
15565ec90831SRussell King 	memset(state, 0, sizeof(*state));
155746b49abcSAndrei Botila 	memcpy(state->buf, export->buf, export->buflen);
15585ec90831SRussell King 	memcpy(state->caam_ctx, export->caam_ctx, sizeof(state->caam_ctx));
155946b49abcSAndrei Botila 	state->buflen = export->buflen;
15605ec90831SRussell King 	state->update = export->update;
15615ec90831SRussell King 	state->final = export->final;
15625ec90831SRussell King 	state->finup = export->finup;
1563434b4212SRussell King 
1564045e3678SYuan Kang 	return 0;
1565045e3678SYuan Kang }
1566045e3678SYuan Kang 
1567045e3678SYuan Kang struct caam_hash_template {
1568045e3678SYuan Kang 	char name[CRYPTO_MAX_ALG_NAME];
1569045e3678SYuan Kang 	char driver_name[CRYPTO_MAX_ALG_NAME];
1570b0e09baeSYuan Kang 	char hmac_name[CRYPTO_MAX_ALG_NAME];
1571b0e09baeSYuan Kang 	char hmac_driver_name[CRYPTO_MAX_ALG_NAME];
1572045e3678SYuan Kang 	unsigned int blocksize;
1573045e3678SYuan Kang 	struct ahash_alg template_ahash;
1574045e3678SYuan Kang 	u32 alg_type;
1575045e3678SYuan Kang };
1576045e3678SYuan Kang 
1577045e3678SYuan Kang /* ahash descriptors */
1578045e3678SYuan Kang static struct caam_hash_template driver_hash[] = {
1579045e3678SYuan Kang 	{
1580b0e09baeSYuan Kang 		.name = "sha1",
1581b0e09baeSYuan Kang 		.driver_name = "sha1-caam",
1582b0e09baeSYuan Kang 		.hmac_name = "hmac(sha1)",
1583b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha1-caam",
1584045e3678SYuan Kang 		.blocksize = SHA1_BLOCK_SIZE,
1585045e3678SYuan Kang 		.template_ahash = {
1586045e3678SYuan Kang 			.init = ahash_init,
1587045e3678SYuan Kang 			.update = ahash_update,
1588045e3678SYuan Kang 			.final = ahash_final,
1589045e3678SYuan Kang 			.finup = ahash_finup,
1590045e3678SYuan Kang 			.digest = ahash_digest,
1591045e3678SYuan Kang 			.export = ahash_export,
1592045e3678SYuan Kang 			.import = ahash_import,
1593045e3678SYuan Kang 			.setkey = ahash_setkey,
1594045e3678SYuan Kang 			.halg = {
1595045e3678SYuan Kang 				.digestsize = SHA1_DIGEST_SIZE,
15965ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1597045e3678SYuan Kang 			},
1598045e3678SYuan Kang 		},
1599045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA1,
1600045e3678SYuan Kang 	}, {
1601b0e09baeSYuan Kang 		.name = "sha224",
1602b0e09baeSYuan Kang 		.driver_name = "sha224-caam",
1603b0e09baeSYuan Kang 		.hmac_name = "hmac(sha224)",
1604b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha224-caam",
1605045e3678SYuan Kang 		.blocksize = SHA224_BLOCK_SIZE,
1606045e3678SYuan Kang 		.template_ahash = {
1607045e3678SYuan Kang 			.init = ahash_init,
1608045e3678SYuan Kang 			.update = ahash_update,
1609045e3678SYuan Kang 			.final = ahash_final,
1610045e3678SYuan Kang 			.finup = ahash_finup,
1611045e3678SYuan Kang 			.digest = ahash_digest,
1612045e3678SYuan Kang 			.export = ahash_export,
1613045e3678SYuan Kang 			.import = ahash_import,
1614045e3678SYuan Kang 			.setkey = ahash_setkey,
1615045e3678SYuan Kang 			.halg = {
1616045e3678SYuan Kang 				.digestsize = SHA224_DIGEST_SIZE,
16175ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1618045e3678SYuan Kang 			},
1619045e3678SYuan Kang 		},
1620045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA224,
1621045e3678SYuan Kang 	}, {
1622b0e09baeSYuan Kang 		.name = "sha256",
1623b0e09baeSYuan Kang 		.driver_name = "sha256-caam",
1624b0e09baeSYuan Kang 		.hmac_name = "hmac(sha256)",
1625b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha256-caam",
1626045e3678SYuan Kang 		.blocksize = SHA256_BLOCK_SIZE,
1627045e3678SYuan Kang 		.template_ahash = {
1628045e3678SYuan Kang 			.init = ahash_init,
1629045e3678SYuan Kang 			.update = ahash_update,
1630045e3678SYuan Kang 			.final = ahash_final,
1631045e3678SYuan Kang 			.finup = ahash_finup,
1632045e3678SYuan Kang 			.digest = ahash_digest,
1633045e3678SYuan Kang 			.export = ahash_export,
1634045e3678SYuan Kang 			.import = ahash_import,
1635045e3678SYuan Kang 			.setkey = ahash_setkey,
1636045e3678SYuan Kang 			.halg = {
1637045e3678SYuan Kang 				.digestsize = SHA256_DIGEST_SIZE,
16385ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1639045e3678SYuan Kang 			},
1640045e3678SYuan Kang 		},
1641045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA256,
1642045e3678SYuan Kang 	}, {
1643b0e09baeSYuan Kang 		.name = "sha384",
1644b0e09baeSYuan Kang 		.driver_name = "sha384-caam",
1645b0e09baeSYuan Kang 		.hmac_name = "hmac(sha384)",
1646b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha384-caam",
1647045e3678SYuan Kang 		.blocksize = SHA384_BLOCK_SIZE,
1648045e3678SYuan Kang 		.template_ahash = {
1649045e3678SYuan Kang 			.init = ahash_init,
1650045e3678SYuan Kang 			.update = ahash_update,
1651045e3678SYuan Kang 			.final = ahash_final,
1652045e3678SYuan Kang 			.finup = ahash_finup,
1653045e3678SYuan Kang 			.digest = ahash_digest,
1654045e3678SYuan Kang 			.export = ahash_export,
1655045e3678SYuan Kang 			.import = ahash_import,
1656045e3678SYuan Kang 			.setkey = ahash_setkey,
1657045e3678SYuan Kang 			.halg = {
1658045e3678SYuan Kang 				.digestsize = SHA384_DIGEST_SIZE,
16595ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1660045e3678SYuan Kang 			},
1661045e3678SYuan Kang 		},
1662045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA384,
1663045e3678SYuan Kang 	}, {
1664b0e09baeSYuan Kang 		.name = "sha512",
1665b0e09baeSYuan Kang 		.driver_name = "sha512-caam",
1666b0e09baeSYuan Kang 		.hmac_name = "hmac(sha512)",
1667b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha512-caam",
1668045e3678SYuan Kang 		.blocksize = SHA512_BLOCK_SIZE,
1669045e3678SYuan Kang 		.template_ahash = {
1670045e3678SYuan Kang 			.init = ahash_init,
1671045e3678SYuan Kang 			.update = ahash_update,
1672045e3678SYuan Kang 			.final = ahash_final,
1673045e3678SYuan Kang 			.finup = ahash_finup,
1674045e3678SYuan Kang 			.digest = ahash_digest,
1675045e3678SYuan Kang 			.export = ahash_export,
1676045e3678SYuan Kang 			.import = ahash_import,
1677045e3678SYuan Kang 			.setkey = ahash_setkey,
1678045e3678SYuan Kang 			.halg = {
1679045e3678SYuan Kang 				.digestsize = SHA512_DIGEST_SIZE,
16805ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1681045e3678SYuan Kang 			},
1682045e3678SYuan Kang 		},
1683045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA512,
1684045e3678SYuan Kang 	}, {
1685b0e09baeSYuan Kang 		.name = "md5",
1686b0e09baeSYuan Kang 		.driver_name = "md5-caam",
1687b0e09baeSYuan Kang 		.hmac_name = "hmac(md5)",
1688b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-md5-caam",
1689045e3678SYuan Kang 		.blocksize = MD5_BLOCK_WORDS * 4,
1690045e3678SYuan Kang 		.template_ahash = {
1691045e3678SYuan Kang 			.init = ahash_init,
1692045e3678SYuan Kang 			.update = ahash_update,
1693045e3678SYuan Kang 			.final = ahash_final,
1694045e3678SYuan Kang 			.finup = ahash_finup,
1695045e3678SYuan Kang 			.digest = ahash_digest,
1696045e3678SYuan Kang 			.export = ahash_export,
1697045e3678SYuan Kang 			.import = ahash_import,
1698045e3678SYuan Kang 			.setkey = ahash_setkey,
1699045e3678SYuan Kang 			.halg = {
1700045e3678SYuan Kang 				.digestsize = MD5_DIGEST_SIZE,
17015ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1702045e3678SYuan Kang 			},
1703045e3678SYuan Kang 		},
1704045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_MD5,
170512b8567fSIuliana Prodan 	}, {
170612b8567fSIuliana Prodan 		.hmac_name = "xcbc(aes)",
170712b8567fSIuliana Prodan 		.hmac_driver_name = "xcbc-aes-caam",
170812b8567fSIuliana Prodan 		.blocksize = AES_BLOCK_SIZE,
170912b8567fSIuliana Prodan 		.template_ahash = {
171012b8567fSIuliana Prodan 			.init = ahash_init,
171112b8567fSIuliana Prodan 			.update = ahash_update,
171212b8567fSIuliana Prodan 			.final = ahash_final,
171312b8567fSIuliana Prodan 			.finup = ahash_finup,
171412b8567fSIuliana Prodan 			.digest = ahash_digest,
171512b8567fSIuliana Prodan 			.export = ahash_export,
171612b8567fSIuliana Prodan 			.import = ahash_import,
171712b8567fSIuliana Prodan 			.setkey = axcbc_setkey,
171812b8567fSIuliana Prodan 			.halg = {
171912b8567fSIuliana Prodan 				.digestsize = AES_BLOCK_SIZE,
172012b8567fSIuliana Prodan 				.statesize = sizeof(struct caam_export_state),
172112b8567fSIuliana Prodan 			},
172212b8567fSIuliana Prodan 		 },
172312b8567fSIuliana Prodan 		.alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_XCBC_MAC,
172487870cfbSIuliana Prodan 	}, {
172587870cfbSIuliana Prodan 		.hmac_name = "cmac(aes)",
172687870cfbSIuliana Prodan 		.hmac_driver_name = "cmac-aes-caam",
172787870cfbSIuliana Prodan 		.blocksize = AES_BLOCK_SIZE,
172887870cfbSIuliana Prodan 		.template_ahash = {
172987870cfbSIuliana Prodan 			.init = ahash_init,
173087870cfbSIuliana Prodan 			.update = ahash_update,
173187870cfbSIuliana Prodan 			.final = ahash_final,
173287870cfbSIuliana Prodan 			.finup = ahash_finup,
173387870cfbSIuliana Prodan 			.digest = ahash_digest,
173487870cfbSIuliana Prodan 			.export = ahash_export,
173587870cfbSIuliana Prodan 			.import = ahash_import,
173687870cfbSIuliana Prodan 			.setkey = acmac_setkey,
173787870cfbSIuliana Prodan 			.halg = {
173887870cfbSIuliana Prodan 				.digestsize = AES_BLOCK_SIZE,
173987870cfbSIuliana Prodan 				.statesize = sizeof(struct caam_export_state),
174087870cfbSIuliana Prodan 			},
174187870cfbSIuliana Prodan 		 },
174287870cfbSIuliana Prodan 		.alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CMAC,
1743045e3678SYuan Kang 	},
1744045e3678SYuan Kang };
1745045e3678SYuan Kang 
1746045e3678SYuan Kang struct caam_hash_alg {
1747045e3678SYuan Kang 	struct list_head entry;
1748045e3678SYuan Kang 	int alg_type;
1749045e3678SYuan Kang 	struct ahash_alg ahash_alg;
1750045e3678SYuan Kang };
1751045e3678SYuan Kang 
1752045e3678SYuan Kang static int caam_hash_cra_init(struct crypto_tfm *tfm)
1753045e3678SYuan Kang {
1754045e3678SYuan Kang 	struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
1755045e3678SYuan Kang 	struct crypto_alg *base = tfm->__crt_alg;
1756045e3678SYuan Kang 	struct hash_alg_common *halg =
1757045e3678SYuan Kang 		 container_of(base, struct hash_alg_common, base);
1758045e3678SYuan Kang 	struct ahash_alg *alg =
1759045e3678SYuan Kang 		 container_of(halg, struct ahash_alg, halg);
1760045e3678SYuan Kang 	struct caam_hash_alg *caam_hash =
1761045e3678SYuan Kang 		 container_of(alg, struct caam_hash_alg, ahash_alg);
1762045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm);
1763045e3678SYuan Kang 	/* Sizes for MDHA running digests: MD5, SHA1, 224, 256, 384, 512 */
1764045e3678SYuan Kang 	static const u8 runninglen[] = { HASH_MSG_LEN + MD5_DIGEST_SIZE,
1765045e3678SYuan Kang 					 HASH_MSG_LEN + SHA1_DIGEST_SIZE,
1766045e3678SYuan Kang 					 HASH_MSG_LEN + 32,
1767045e3678SYuan Kang 					 HASH_MSG_LEN + SHA256_DIGEST_SIZE,
1768045e3678SYuan Kang 					 HASH_MSG_LEN + 64,
1769045e3678SYuan Kang 					 HASH_MSG_LEN + SHA512_DIGEST_SIZE };
177021b014f0SIuliana Prodan 	const size_t sh_desc_update_offset = offsetof(struct caam_hash_ctx,
177121b014f0SIuliana Prodan 						      sh_desc_update);
1772bbf22344SHoria Geantă 	dma_addr_t dma_addr;
17737e0880b9SHoria Geantă 	struct caam_drv_private *priv;
1774045e3678SYuan Kang 
1775045e3678SYuan Kang 	/*
1776cfc6f11bSRuchika Gupta 	 * Get a Job ring from Job Ring driver to ensure in-order
1777045e3678SYuan Kang 	 * crypto request processing per tfm
1778045e3678SYuan Kang 	 */
1779cfc6f11bSRuchika Gupta 	ctx->jrdev = caam_jr_alloc();
1780cfc6f11bSRuchika Gupta 	if (IS_ERR(ctx->jrdev)) {
1781cfc6f11bSRuchika Gupta 		pr_err("Job Ring Device allocation for transform failed\n");
1782cfc6f11bSRuchika Gupta 		return PTR_ERR(ctx->jrdev);
1783cfc6f11bSRuchika Gupta 	}
1784bbf22344SHoria Geantă 
17857e0880b9SHoria Geantă 	priv = dev_get_drvdata(ctx->jrdev->parent);
178612b8567fSIuliana Prodan 
178712b8567fSIuliana Prodan 	if (is_xcbc_aes(caam_hash->alg_type)) {
178812b8567fSIuliana Prodan 		ctx->dir = DMA_TO_DEVICE;
1789e9b4913aSHoria Geantă 		ctx->key_dir = DMA_BIDIRECTIONAL;
179012b8567fSIuliana Prodan 		ctx->adata.algtype = OP_TYPE_CLASS1_ALG | caam_hash->alg_type;
179112b8567fSIuliana Prodan 		ctx->ctx_len = 48;
1792e9b4913aSHoria Geantă 	} else if (is_cmac_aes(caam_hash->alg_type)) {
1793e9b4913aSHoria Geantă 		ctx->dir = DMA_TO_DEVICE;
1794e9b4913aSHoria Geantă 		ctx->key_dir = DMA_NONE;
1795e9b4913aSHoria Geantă 		ctx->adata.algtype = OP_TYPE_CLASS1_ALG | caam_hash->alg_type;
1796e9b4913aSHoria Geantă 		ctx->ctx_len = 32;
1797e9b4913aSHoria Geantă 	} else {
1798e9b4913aSHoria Geantă 		if (priv->era >= 6) {
1799e9b4913aSHoria Geantă 			ctx->dir = DMA_BIDIRECTIONAL;
1800e9b4913aSHoria Geantă 			ctx->key_dir = alg->setkey ? DMA_TO_DEVICE : DMA_NONE;
1801e9b4913aSHoria Geantă 		} else {
1802e9b4913aSHoria Geantă 			ctx->dir = DMA_TO_DEVICE;
1803e9b4913aSHoria Geantă 			ctx->key_dir = DMA_NONE;
1804e9b4913aSHoria Geantă 		}
1805e9b4913aSHoria Geantă 		ctx->adata.algtype = OP_TYPE_CLASS2_ALG | caam_hash->alg_type;
1806e9b4913aSHoria Geantă 		ctx->ctx_len = runninglen[(ctx->adata.algtype &
1807e9b4913aSHoria Geantă 					   OP_ALG_ALGSEL_SUBMASK) >>
1808e9b4913aSHoria Geantă 					  OP_ALG_ALGSEL_SHIFT];
1809e9b4913aSHoria Geantă 	}
181012b8567fSIuliana Prodan 
1811e9b4913aSHoria Geantă 	if (ctx->key_dir != DMA_NONE) {
1812a2fb864cSHoria Geantă 		ctx->adata.key_dma = dma_map_single_attrs(ctx->jrdev, ctx->key,
181312b8567fSIuliana Prodan 							  ARRAY_SIZE(ctx->key),
1814e9b4913aSHoria Geantă 							  ctx->key_dir,
181512b8567fSIuliana Prodan 							  DMA_ATTR_SKIP_CPU_SYNC);
1816a2fb864cSHoria Geantă 		if (dma_mapping_error(ctx->jrdev, ctx->adata.key_dma)) {
181712b8567fSIuliana Prodan 			dev_err(ctx->jrdev, "unable to map key\n");
181812b8567fSIuliana Prodan 			caam_jr_free(ctx->jrdev);
181912b8567fSIuliana Prodan 			return -ENOMEM;
182012b8567fSIuliana Prodan 		}
182112b8567fSIuliana Prodan 	}
18227e0880b9SHoria Geantă 
1823bbf22344SHoria Geantă 	dma_addr = dma_map_single_attrs(ctx->jrdev, ctx->sh_desc_update,
182421b014f0SIuliana Prodan 					offsetof(struct caam_hash_ctx, key) -
182521b014f0SIuliana Prodan 					sh_desc_update_offset,
18267e0880b9SHoria Geantă 					ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
1827bbf22344SHoria Geantă 	if (dma_mapping_error(ctx->jrdev, dma_addr)) {
1828bbf22344SHoria Geantă 		dev_err(ctx->jrdev, "unable to map shared descriptors\n");
182912b8567fSIuliana Prodan 
1830e9b4913aSHoria Geantă 		if (ctx->key_dir != DMA_NONE)
1831a2fb864cSHoria Geantă 			dma_unmap_single_attrs(ctx->jrdev, ctx->adata.key_dma,
183212b8567fSIuliana Prodan 					       ARRAY_SIZE(ctx->key),
1833e9b4913aSHoria Geantă 					       ctx->key_dir,
183412b8567fSIuliana Prodan 					       DMA_ATTR_SKIP_CPU_SYNC);
183512b8567fSIuliana Prodan 
1836bbf22344SHoria Geantă 		caam_jr_free(ctx->jrdev);
1837bbf22344SHoria Geantă 		return -ENOMEM;
1838bbf22344SHoria Geantă 	}
1839bbf22344SHoria Geantă 
1840bbf22344SHoria Geantă 	ctx->sh_desc_update_dma = dma_addr;
1841bbf22344SHoria Geantă 	ctx->sh_desc_update_first_dma = dma_addr +
1842bbf22344SHoria Geantă 					offsetof(struct caam_hash_ctx,
184321b014f0SIuliana Prodan 						 sh_desc_update_first) -
184421b014f0SIuliana Prodan 					sh_desc_update_offset;
1845bbf22344SHoria Geantă 	ctx->sh_desc_fin_dma = dma_addr + offsetof(struct caam_hash_ctx,
184621b014f0SIuliana Prodan 						   sh_desc_fin) -
184721b014f0SIuliana Prodan 					sh_desc_update_offset;
1848bbf22344SHoria Geantă 	ctx->sh_desc_digest_dma = dma_addr + offsetof(struct caam_hash_ctx,
184921b014f0SIuliana Prodan 						      sh_desc_digest) -
185021b014f0SIuliana Prodan 					sh_desc_update_offset;
185121b014f0SIuliana Prodan 
185221b014f0SIuliana Prodan 	ctx->enginectx.op.do_one_request = ahash_do_one_req;
1853bbf22344SHoria Geantă 
1854045e3678SYuan Kang 	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
1855045e3678SYuan Kang 				 sizeof(struct caam_hash_state));
18569a2537d0SIuliana Prodan 
18579a2537d0SIuliana Prodan 	/*
18589a2537d0SIuliana Prodan 	 * For keyed hash algorithms shared descriptors
18599a2537d0SIuliana Prodan 	 * will be created later in setkey() callback
18609a2537d0SIuliana Prodan 	 */
18619a2537d0SIuliana Prodan 	return alg->setkey ? 0 : ahash_set_sh_desc(ahash);
1862045e3678SYuan Kang }
1863045e3678SYuan Kang 
1864045e3678SYuan Kang static void caam_hash_cra_exit(struct crypto_tfm *tfm)
1865045e3678SYuan Kang {
1866045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm);
1867045e3678SYuan Kang 
1868bbf22344SHoria Geantă 	dma_unmap_single_attrs(ctx->jrdev, ctx->sh_desc_update_dma,
186921b014f0SIuliana Prodan 			       offsetof(struct caam_hash_ctx, key) -
187021b014f0SIuliana Prodan 			       offsetof(struct caam_hash_ctx, sh_desc_update),
18717e0880b9SHoria Geantă 			       ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
1872e9b4913aSHoria Geantă 	if (ctx->key_dir != DMA_NONE)
1873a2fb864cSHoria Geantă 		dma_unmap_single_attrs(ctx->jrdev, ctx->adata.key_dma,
1874e9b4913aSHoria Geantă 				       ARRAY_SIZE(ctx->key), ctx->key_dir,
187512b8567fSIuliana Prodan 				       DMA_ATTR_SKIP_CPU_SYNC);
1876cfc6f11bSRuchika Gupta 	caam_jr_free(ctx->jrdev);
1877045e3678SYuan Kang }
1878045e3678SYuan Kang 
18791b46c90cSHoria Geantă void caam_algapi_hash_exit(void)
1880045e3678SYuan Kang {
1881045e3678SYuan Kang 	struct caam_hash_alg *t_alg, *n;
1882045e3678SYuan Kang 
1883cfc6f11bSRuchika Gupta 	if (!hash_list.next)
1884045e3678SYuan Kang 		return;
1885045e3678SYuan Kang 
1886cfc6f11bSRuchika Gupta 	list_for_each_entry_safe(t_alg, n, &hash_list, entry) {
1887045e3678SYuan Kang 		crypto_unregister_ahash(&t_alg->ahash_alg);
1888045e3678SYuan Kang 		list_del(&t_alg->entry);
1889045e3678SYuan Kang 		kfree(t_alg);
1890045e3678SYuan Kang 	}
1891045e3678SYuan Kang }
1892045e3678SYuan Kang 
1893045e3678SYuan Kang static struct caam_hash_alg *
1894cfc6f11bSRuchika Gupta caam_hash_alloc(struct caam_hash_template *template,
1895b0e09baeSYuan Kang 		bool keyed)
1896045e3678SYuan Kang {
1897045e3678SYuan Kang 	struct caam_hash_alg *t_alg;
1898045e3678SYuan Kang 	struct ahash_alg *halg;
1899045e3678SYuan Kang 	struct crypto_alg *alg;
1900045e3678SYuan Kang 
19019c4f9733SFabio Estevam 	t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL);
1902045e3678SYuan Kang 	if (!t_alg) {
1903cfc6f11bSRuchika Gupta 		pr_err("failed to allocate t_alg\n");
1904045e3678SYuan Kang 		return ERR_PTR(-ENOMEM);
1905045e3678SYuan Kang 	}
1906045e3678SYuan Kang 
1907045e3678SYuan Kang 	t_alg->ahash_alg = template->template_ahash;
1908045e3678SYuan Kang 	halg = &t_alg->ahash_alg;
1909045e3678SYuan Kang 	alg = &halg->halg.base;
1910045e3678SYuan Kang 
1911b0e09baeSYuan Kang 	if (keyed) {
1912b0e09baeSYuan Kang 		snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
1913b0e09baeSYuan Kang 			 template->hmac_name);
1914b0e09baeSYuan Kang 		snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
1915b0e09baeSYuan Kang 			 template->hmac_driver_name);
1916b0e09baeSYuan Kang 	} else {
1917b0e09baeSYuan Kang 		snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
1918b0e09baeSYuan Kang 			 template->name);
1919045e3678SYuan Kang 		snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
1920045e3678SYuan Kang 			 template->driver_name);
1921a0118c8bSRussell King 		t_alg->ahash_alg.setkey = NULL;
1922b0e09baeSYuan Kang 	}
1923045e3678SYuan Kang 	alg->cra_module = THIS_MODULE;
1924045e3678SYuan Kang 	alg->cra_init = caam_hash_cra_init;
1925045e3678SYuan Kang 	alg->cra_exit = caam_hash_cra_exit;
1926045e3678SYuan Kang 	alg->cra_ctxsize = sizeof(struct caam_hash_ctx);
1927045e3678SYuan Kang 	alg->cra_priority = CAAM_CRA_PRIORITY;
1928045e3678SYuan Kang 	alg->cra_blocksize = template->blocksize;
1929045e3678SYuan Kang 	alg->cra_alignmask = 0;
1930*b8aa7dc5SMikulas Patocka 	alg->cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY;
1931045e3678SYuan Kang 
1932045e3678SYuan Kang 	t_alg->alg_type = template->alg_type;
1933045e3678SYuan Kang 
1934045e3678SYuan Kang 	return t_alg;
1935045e3678SYuan Kang }
1936045e3678SYuan Kang 
19371b46c90cSHoria Geantă int caam_algapi_hash_init(struct device *ctrldev)
1938045e3678SYuan Kang {
1939045e3678SYuan Kang 	int i = 0, err = 0;
19401b46c90cSHoria Geantă 	struct caam_drv_private *priv = dev_get_drvdata(ctrldev);
1941bf83490eSVictoria Milhoan 	unsigned int md_limit = SHA512_DIGEST_SIZE;
1942d239b10dSHoria Geantă 	u32 md_inst, md_vid;
1943045e3678SYuan Kang 
1944bf83490eSVictoria Milhoan 	/*
1945bf83490eSVictoria Milhoan 	 * Register crypto algorithms the device supports.  First, identify
1946bf83490eSVictoria Milhoan 	 * presence and attributes of MD block.
1947bf83490eSVictoria Milhoan 	 */
1948d239b10dSHoria Geantă 	if (priv->era < 10) {
1949d239b10dSHoria Geantă 		md_vid = (rd_reg32(&priv->ctrl->perfmon.cha_id_ls) &
1950d239b10dSHoria Geantă 			  CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;
1951d239b10dSHoria Geantă 		md_inst = (rd_reg32(&priv->ctrl->perfmon.cha_num_ls) &
1952d239b10dSHoria Geantă 			   CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;
1953d239b10dSHoria Geantă 	} else {
1954d239b10dSHoria Geantă 		u32 mdha = rd_reg32(&priv->ctrl->vreg.mdha);
1955d239b10dSHoria Geantă 
1956d239b10dSHoria Geantă 		md_vid = (mdha & CHA_VER_VID_MASK) >> CHA_VER_VID_SHIFT;
1957d239b10dSHoria Geantă 		md_inst = mdha & CHA_VER_NUM_MASK;
1958d239b10dSHoria Geantă 	}
1959bf83490eSVictoria Milhoan 
1960bf83490eSVictoria Milhoan 	/*
1961bf83490eSVictoria Milhoan 	 * Skip registration of any hashing algorithms if MD block
1962bf83490eSVictoria Milhoan 	 * is not present.
1963bf83490eSVictoria Milhoan 	 */
19641b46c90cSHoria Geantă 	if (!md_inst)
19650435d47eSIuliana Prodan 		return 0;
1966bf83490eSVictoria Milhoan 
1967bf83490eSVictoria Milhoan 	/* Limit digest size based on LP256 */
1968d239b10dSHoria Geantă 	if (md_vid == CHA_VER_VID_MD_LP256)
1969bf83490eSVictoria Milhoan 		md_limit = SHA256_DIGEST_SIZE;
1970bf83490eSVictoria Milhoan 
1971cfc6f11bSRuchika Gupta 	INIT_LIST_HEAD(&hash_list);
1972045e3678SYuan Kang 
1973045e3678SYuan Kang 	/* register crypto algorithms the device supports */
1974045e3678SYuan Kang 	for (i = 0; i < ARRAY_SIZE(driver_hash); i++) {
1975045e3678SYuan Kang 		struct caam_hash_alg *t_alg;
1976bf83490eSVictoria Milhoan 		struct caam_hash_template *alg = driver_hash + i;
1977bf83490eSVictoria Milhoan 
1978bf83490eSVictoria Milhoan 		/* If MD size is not supported by device, skip registration */
197912b8567fSIuliana Prodan 		if (is_mdha(alg->alg_type) &&
198012b8567fSIuliana Prodan 		    alg->template_ahash.halg.digestsize > md_limit)
1981bf83490eSVictoria Milhoan 			continue;
1982045e3678SYuan Kang 
1983b0e09baeSYuan Kang 		/* register hmac version */
1984bf83490eSVictoria Milhoan 		t_alg = caam_hash_alloc(alg, true);
1985b0e09baeSYuan Kang 		if (IS_ERR(t_alg)) {
1986b0e09baeSYuan Kang 			err = PTR_ERR(t_alg);
19870f103b37SIuliana Prodan 			pr_warn("%s alg allocation failed\n",
19880f103b37SIuliana Prodan 				alg->hmac_driver_name);
1989b0e09baeSYuan Kang 			continue;
1990b0e09baeSYuan Kang 		}
1991b0e09baeSYuan Kang 
1992b0e09baeSYuan Kang 		err = crypto_register_ahash(&t_alg->ahash_alg);
1993b0e09baeSYuan Kang 		if (err) {
19946ea30f0aSRussell King 			pr_warn("%s alg registration failed: %d\n",
19956ea30f0aSRussell King 				t_alg->ahash_alg.halg.base.cra_driver_name,
19966ea30f0aSRussell King 				err);
1997b0e09baeSYuan Kang 			kfree(t_alg);
1998b0e09baeSYuan Kang 		} else
1999cfc6f11bSRuchika Gupta 			list_add_tail(&t_alg->entry, &hash_list);
2000b0e09baeSYuan Kang 
200112b8567fSIuliana Prodan 		if ((alg->alg_type & OP_ALG_ALGSEL_MASK) == OP_ALG_ALGSEL_AES)
200212b8567fSIuliana Prodan 			continue;
200312b8567fSIuliana Prodan 
2004b0e09baeSYuan Kang 		/* register unkeyed version */
2005bf83490eSVictoria Milhoan 		t_alg = caam_hash_alloc(alg, false);
2006045e3678SYuan Kang 		if (IS_ERR(t_alg)) {
2007045e3678SYuan Kang 			err = PTR_ERR(t_alg);
2008bf83490eSVictoria Milhoan 			pr_warn("%s alg allocation failed\n", alg->driver_name);
2009045e3678SYuan Kang 			continue;
2010045e3678SYuan Kang 		}
2011045e3678SYuan Kang 
2012045e3678SYuan Kang 		err = crypto_register_ahash(&t_alg->ahash_alg);
2013045e3678SYuan Kang 		if (err) {
20146ea30f0aSRussell King 			pr_warn("%s alg registration failed: %d\n",
20156ea30f0aSRussell King 				t_alg->ahash_alg.halg.base.cra_driver_name,
20166ea30f0aSRussell King 				err);
2017045e3678SYuan Kang 			kfree(t_alg);
2018045e3678SYuan Kang 		} else
2019cfc6f11bSRuchika Gupta 			list_add_tail(&t_alg->entry, &hash_list);
2020045e3678SYuan Kang 	}
2021045e3678SYuan Kang 
2022045e3678SYuan Kang 	return err;
2023045e3678SYuan Kang }
2024