xref: /linux/drivers/crypto/caam/caamhash.c (revision 2ba1e79831aa0388d0d08ad649ecb933f317b61d)
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"
68045e3678SYuan Kang 
69045e3678SYuan Kang #define CAAM_CRA_PRIORITY		3000
70045e3678SYuan Kang 
71045e3678SYuan Kang /* max hash key is max split key size */
72045e3678SYuan Kang #define CAAM_MAX_HASH_KEY_SIZE		(SHA512_DIGEST_SIZE * 2)
73045e3678SYuan Kang 
74045e3678SYuan Kang #define CAAM_MAX_HASH_BLOCK_SIZE	SHA512_BLOCK_SIZE
75045e3678SYuan Kang #define CAAM_MAX_HASH_DIGEST_SIZE	SHA512_DIGEST_SIZE
76045e3678SYuan Kang 
77045e3678SYuan Kang #define DESC_HASH_MAX_USED_BYTES	(DESC_AHASH_FINAL_LEN + \
78045e3678SYuan Kang 					 CAAM_MAX_HASH_KEY_SIZE)
79045e3678SYuan Kang #define DESC_HASH_MAX_USED_LEN		(DESC_HASH_MAX_USED_BYTES / CAAM_CMD_SZ)
80045e3678SYuan Kang 
81045e3678SYuan Kang /* caam context sizes for hashes: running digest + 8 */
82045e3678SYuan Kang #define HASH_MSG_LEN			8
83045e3678SYuan Kang #define MAX_CTX_LEN			(HASH_MSG_LEN + SHA512_DIGEST_SIZE)
84045e3678SYuan Kang 
85cfc6f11bSRuchika Gupta static struct list_head hash_list;
86cfc6f11bSRuchika Gupta 
87045e3678SYuan Kang /* ahash per-session context */
88045e3678SYuan Kang struct caam_hash_ctx {
89e11793f5SRussell King 	u32 sh_desc_update[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
90e11793f5SRussell King 	u32 sh_desc_update_first[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
91e11793f5SRussell King 	u32 sh_desc_fin[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
92e11793f5SRussell King 	u32 sh_desc_digest[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
9312b8567fSIuliana Prodan 	u8 key[CAAM_MAX_HASH_KEY_SIZE] ____cacheline_aligned;
94e11793f5SRussell King 	dma_addr_t sh_desc_update_dma ____cacheline_aligned;
95045e3678SYuan Kang 	dma_addr_t sh_desc_update_first_dma;
96045e3678SYuan Kang 	dma_addr_t sh_desc_fin_dma;
97045e3678SYuan Kang 	dma_addr_t sh_desc_digest_dma;
987e0880b9SHoria Geantă 	enum dma_data_direction dir;
99e9b4913aSHoria Geantă 	enum dma_data_direction key_dir;
100e11793f5SRussell King 	struct device *jrdev;
101045e3678SYuan Kang 	int ctx_len;
102db57656bSHoria Geantă 	struct alginfo adata;
103045e3678SYuan Kang };
104045e3678SYuan Kang 
105045e3678SYuan Kang /* ahash state */
106045e3678SYuan Kang struct caam_hash_state {
107045e3678SYuan Kang 	dma_addr_t buf_dma;
108045e3678SYuan Kang 	dma_addr_t ctx_dma;
10965055e21SFranck LENORMAND 	int ctx_dma_len;
11046b49abcSAndrei Botila 	u8 buf[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
11146b49abcSAndrei Botila 	int buflen;
11246b49abcSAndrei Botila 	int next_buflen;
113e7472422SVictoria Milhoan 	u8 caam_ctx[MAX_CTX_LEN] ____cacheline_aligned;
114045e3678SYuan Kang 	int (*update)(struct ahash_request *req);
115045e3678SYuan Kang 	int (*final)(struct ahash_request *req);
116045e3678SYuan Kang 	int (*finup)(struct ahash_request *req);
117045e3678SYuan Kang };
118045e3678SYuan Kang 
1195ec90831SRussell King struct caam_export_state {
1205ec90831SRussell King 	u8 buf[CAAM_MAX_HASH_BLOCK_SIZE];
1215ec90831SRussell King 	u8 caam_ctx[MAX_CTX_LEN];
1225ec90831SRussell King 	int buflen;
1235ec90831SRussell King 	int (*update)(struct ahash_request *req);
1245ec90831SRussell King 	int (*final)(struct ahash_request *req);
1255ec90831SRussell King 	int (*finup)(struct ahash_request *req);
1265ec90831SRussell King };
1275ec90831SRussell King 
12887870cfbSIuliana Prodan static inline bool is_cmac_aes(u32 algtype)
12912b8567fSIuliana Prodan {
13012b8567fSIuliana Prodan 	return (algtype & (OP_ALG_ALGSEL_MASK | OP_ALG_AAI_MASK)) ==
13187870cfbSIuliana Prodan 	       (OP_ALG_ALGSEL_AES | OP_ALG_AAI_CMAC);
13212b8567fSIuliana Prodan }
133045e3678SYuan Kang /* Common job descriptor seq in/out ptr routines */
134045e3678SYuan Kang 
135045e3678SYuan Kang /* Map state->caam_ctx, and append seq_out_ptr command that points to it */
136ce572085SHoria Geanta static inline int map_seq_out_ptr_ctx(u32 *desc, struct device *jrdev,
137045e3678SYuan Kang 				      struct caam_hash_state *state,
138045e3678SYuan Kang 				      int ctx_len)
139045e3678SYuan Kang {
14065055e21SFranck LENORMAND 	state->ctx_dma_len = ctx_len;
141045e3678SYuan Kang 	state->ctx_dma = dma_map_single(jrdev, state->caam_ctx,
142045e3678SYuan Kang 					ctx_len, DMA_FROM_DEVICE);
143ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, state->ctx_dma)) {
144ce572085SHoria Geanta 		dev_err(jrdev, "unable to map ctx\n");
14587ec02e7SHoria Geantă 		state->ctx_dma = 0;
146ce572085SHoria Geanta 		return -ENOMEM;
147ce572085SHoria Geanta 	}
148ce572085SHoria Geanta 
149045e3678SYuan Kang 	append_seq_out_ptr(desc, state->ctx_dma, ctx_len, 0);
150ce572085SHoria Geanta 
151ce572085SHoria Geanta 	return 0;
152045e3678SYuan Kang }
153045e3678SYuan Kang 
154944c3d4dSHoria Geantă /* Map current buffer in state (if length > 0) and put it in link table */
155944c3d4dSHoria Geantă static inline int buf_map_to_sec4_sg(struct device *jrdev,
156045e3678SYuan Kang 				     struct sec4_sg_entry *sec4_sg,
157944c3d4dSHoria Geantă 				     struct caam_hash_state *state)
158045e3678SYuan Kang {
15946b49abcSAndrei Botila 	int buflen = state->buflen;
160045e3678SYuan Kang 
161944c3d4dSHoria Geantă 	if (!buflen)
162944c3d4dSHoria Geantă 		return 0;
163045e3678SYuan Kang 
16446b49abcSAndrei Botila 	state->buf_dma = dma_map_single(jrdev, state->buf, buflen,
165944c3d4dSHoria Geantă 					DMA_TO_DEVICE);
166944c3d4dSHoria Geantă 	if (dma_mapping_error(jrdev, state->buf_dma)) {
167944c3d4dSHoria Geantă 		dev_err(jrdev, "unable to map buf\n");
168944c3d4dSHoria Geantă 		state->buf_dma = 0;
169944c3d4dSHoria Geantă 		return -ENOMEM;
170045e3678SYuan Kang 	}
171045e3678SYuan Kang 
172944c3d4dSHoria Geantă 	dma_to_sec4_sg_one(sec4_sg, state->buf_dma, buflen, 0);
173045e3678SYuan Kang 
174944c3d4dSHoria Geantă 	return 0;
175045e3678SYuan Kang }
176045e3678SYuan Kang 
177045e3678SYuan Kang /* Map state->caam_ctx, and add it to link table */
178dfcd8393SHoria Geantă static inline int ctx_map_to_sec4_sg(struct device *jrdev,
179ce572085SHoria Geanta 				     struct caam_hash_state *state, int ctx_len,
180ce572085SHoria Geanta 				     struct sec4_sg_entry *sec4_sg, u32 flag)
181045e3678SYuan Kang {
18265055e21SFranck LENORMAND 	state->ctx_dma_len = ctx_len;
183045e3678SYuan Kang 	state->ctx_dma = dma_map_single(jrdev, state->caam_ctx, ctx_len, flag);
184ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, state->ctx_dma)) {
185ce572085SHoria Geanta 		dev_err(jrdev, "unable to map ctx\n");
18687ec02e7SHoria Geantă 		state->ctx_dma = 0;
187ce572085SHoria Geanta 		return -ENOMEM;
188ce572085SHoria Geanta 	}
189ce572085SHoria Geanta 
190045e3678SYuan Kang 	dma_to_sec4_sg_one(sec4_sg, state->ctx_dma, ctx_len, 0);
191ce572085SHoria Geanta 
192ce572085SHoria Geanta 	return 0;
193045e3678SYuan Kang }
194045e3678SYuan Kang 
195045e3678SYuan Kang static int ahash_set_sh_desc(struct crypto_ahash *ahash)
196045e3678SYuan Kang {
197045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
198045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
199045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
2007e0880b9SHoria Geantă 	struct caam_drv_private *ctrlpriv = dev_get_drvdata(jrdev->parent);
201045e3678SYuan Kang 	u32 *desc;
202045e3678SYuan Kang 
2037e0880b9SHoria Geantă 	ctx->adata.key_virt = ctx->key;
2047e0880b9SHoria Geantă 
205045e3678SYuan Kang 	/* ahash_update shared descriptor */
206045e3678SYuan Kang 	desc = ctx->sh_desc_update;
2070efa7579SHoria Geantă 	cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_UPDATE, ctx->ctx_len,
2080efa7579SHoria Geantă 			  ctx->ctx_len, true, ctrlpriv->era);
209bbf22344SHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma,
2107e0880b9SHoria Geantă 				   desc_bytes(desc), ctx->dir);
2116e005503SSascha Hauer 
2126e005503SSascha Hauer 	print_hex_dump_debug("ahash update shdesc@"__stringify(__LINE__)": ",
2136e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
2146e005503SSascha Hauer 			     1);
215045e3678SYuan Kang 
216045e3678SYuan Kang 	/* ahash_update_first shared descriptor */
217045e3678SYuan Kang 	desc = ctx->sh_desc_update_first;
2180efa7579SHoria Geantă 	cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len,
2190efa7579SHoria Geantă 			  ctx->ctx_len, false, ctrlpriv->era);
220bbf22344SHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma,
2217e0880b9SHoria Geantă 				   desc_bytes(desc), ctx->dir);
2226e005503SSascha Hauer 	print_hex_dump_debug("ahash update first shdesc@"__stringify(__LINE__)
2236e005503SSascha Hauer 			     ": ", DUMP_PREFIX_ADDRESS, 16, 4, desc,
2246e005503SSascha Hauer 			     desc_bytes(desc), 1);
225045e3678SYuan Kang 
226045e3678SYuan Kang 	/* ahash_final shared descriptor */
227045e3678SYuan Kang 	desc = ctx->sh_desc_fin;
2280efa7579SHoria Geantă 	cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_FINALIZE, digestsize,
2290efa7579SHoria Geantă 			  ctx->ctx_len, true, ctrlpriv->era);
230bbf22344SHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma,
2317e0880b9SHoria Geantă 				   desc_bytes(desc), ctx->dir);
2326e005503SSascha Hauer 
2336e005503SSascha Hauer 	print_hex_dump_debug("ahash final shdesc@"__stringify(__LINE__)": ",
234045e3678SYuan Kang 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
235045e3678SYuan Kang 			     desc_bytes(desc), 1);
236045e3678SYuan Kang 
237045e3678SYuan Kang 	/* ahash_digest shared descriptor */
238045e3678SYuan Kang 	desc = ctx->sh_desc_digest;
2390efa7579SHoria Geantă 	cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_INITFINAL, digestsize,
2400efa7579SHoria Geantă 			  ctx->ctx_len, false, ctrlpriv->era);
241bbf22344SHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma,
2427e0880b9SHoria Geantă 				   desc_bytes(desc), ctx->dir);
2436e005503SSascha Hauer 
2446e005503SSascha Hauer 	print_hex_dump_debug("ahash digest shdesc@"__stringify(__LINE__)": ",
245045e3678SYuan Kang 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
246045e3678SYuan Kang 			     desc_bytes(desc), 1);
247045e3678SYuan Kang 
248045e3678SYuan Kang 	return 0;
249045e3678SYuan Kang }
250045e3678SYuan Kang 
25112b8567fSIuliana Prodan static int axcbc_set_sh_desc(struct crypto_ahash *ahash)
25212b8567fSIuliana Prodan {
25312b8567fSIuliana Prodan 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
25412b8567fSIuliana Prodan 	int digestsize = crypto_ahash_digestsize(ahash);
25512b8567fSIuliana Prodan 	struct device *jrdev = ctx->jrdev;
25612b8567fSIuliana Prodan 	u32 *desc;
25712b8567fSIuliana Prodan 
25812b8567fSIuliana Prodan 	/* shared descriptor for ahash_update */
25912b8567fSIuliana Prodan 	desc = ctx->sh_desc_update;
26087870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_UPDATE,
261a2fb864cSHoria Geantă 			    ctx->ctx_len, ctx->ctx_len);
26212b8567fSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma,
26312b8567fSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
26412b8567fSIuliana Prodan 	print_hex_dump_debug("axcbc update shdesc@" __stringify(__LINE__)" : ",
26512b8567fSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
26612b8567fSIuliana Prodan 			     1);
26712b8567fSIuliana Prodan 
26812b8567fSIuliana Prodan 	/* shared descriptor for ahash_{final,finup} */
26912b8567fSIuliana Prodan 	desc = ctx->sh_desc_fin;
27087870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_FINALIZE,
271a2fb864cSHoria Geantă 			    digestsize, ctx->ctx_len);
27212b8567fSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma,
27312b8567fSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
27412b8567fSIuliana Prodan 	print_hex_dump_debug("axcbc finup shdesc@" __stringify(__LINE__)" : ",
27512b8567fSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
27612b8567fSIuliana Prodan 			     1);
27712b8567fSIuliana Prodan 
27812b8567fSIuliana Prodan 	/* key is immediate data for INIT and INITFINAL states */
27912b8567fSIuliana Prodan 	ctx->adata.key_virt = ctx->key;
28012b8567fSIuliana Prodan 
28112b8567fSIuliana Prodan 	/* shared descriptor for first invocation of ahash_update */
28212b8567fSIuliana Prodan 	desc = ctx->sh_desc_update_first;
28387870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len,
284a2fb864cSHoria Geantă 			    ctx->ctx_len);
28512b8567fSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma,
28612b8567fSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
2876e005503SSascha Hauer 	print_hex_dump_debug("axcbc update first shdesc@" __stringify(__LINE__)
2886e005503SSascha Hauer 			     " : ", DUMP_PREFIX_ADDRESS, 16, 4, desc,
2896e005503SSascha Hauer 			     desc_bytes(desc), 1);
29012b8567fSIuliana Prodan 
29112b8567fSIuliana Prodan 	/* shared descriptor for ahash_digest */
29212b8567fSIuliana Prodan 	desc = ctx->sh_desc_digest;
29387870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INITFINAL,
294a2fb864cSHoria Geantă 			    digestsize, ctx->ctx_len);
29512b8567fSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma,
29612b8567fSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
29712b8567fSIuliana Prodan 	print_hex_dump_debug("axcbc digest shdesc@" __stringify(__LINE__)" : ",
29812b8567fSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
29912b8567fSIuliana Prodan 			     1);
30087870cfbSIuliana Prodan 	return 0;
30187870cfbSIuliana Prodan }
30287870cfbSIuliana Prodan 
30387870cfbSIuliana Prodan static int acmac_set_sh_desc(struct crypto_ahash *ahash)
30487870cfbSIuliana Prodan {
30587870cfbSIuliana Prodan 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
30687870cfbSIuliana Prodan 	int digestsize = crypto_ahash_digestsize(ahash);
30787870cfbSIuliana Prodan 	struct device *jrdev = ctx->jrdev;
30887870cfbSIuliana Prodan 	u32 *desc;
30987870cfbSIuliana Prodan 
31087870cfbSIuliana Prodan 	/* shared descriptor for ahash_update */
31187870cfbSIuliana Prodan 	desc = ctx->sh_desc_update;
31287870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_UPDATE,
313a2fb864cSHoria Geantă 			    ctx->ctx_len, ctx->ctx_len);
31487870cfbSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma,
31587870cfbSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
31687870cfbSIuliana Prodan 	print_hex_dump_debug("acmac update shdesc@" __stringify(__LINE__)" : ",
31787870cfbSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
31887870cfbSIuliana Prodan 			     desc_bytes(desc), 1);
31987870cfbSIuliana Prodan 
32087870cfbSIuliana Prodan 	/* shared descriptor for ahash_{final,finup} */
32187870cfbSIuliana Prodan 	desc = ctx->sh_desc_fin;
32287870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_FINALIZE,
323a2fb864cSHoria Geantă 			    digestsize, ctx->ctx_len);
32487870cfbSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma,
32587870cfbSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
32687870cfbSIuliana Prodan 	print_hex_dump_debug("acmac finup shdesc@" __stringify(__LINE__)" : ",
32787870cfbSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
32887870cfbSIuliana Prodan 			     desc_bytes(desc), 1);
32987870cfbSIuliana Prodan 
33087870cfbSIuliana Prodan 	/* shared descriptor for first invocation of ahash_update */
33187870cfbSIuliana Prodan 	desc = ctx->sh_desc_update_first;
33287870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len,
333a2fb864cSHoria Geantă 			    ctx->ctx_len);
33487870cfbSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma,
33587870cfbSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
3366e005503SSascha Hauer 	print_hex_dump_debug("acmac update first shdesc@" __stringify(__LINE__)
3376e005503SSascha Hauer 			     " : ", DUMP_PREFIX_ADDRESS, 16, 4, desc,
33887870cfbSIuliana Prodan 			     desc_bytes(desc), 1);
33987870cfbSIuliana Prodan 
34087870cfbSIuliana Prodan 	/* shared descriptor for ahash_digest */
34187870cfbSIuliana Prodan 	desc = ctx->sh_desc_digest;
34287870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INITFINAL,
343a2fb864cSHoria Geantă 			    digestsize, ctx->ctx_len);
34487870cfbSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma,
34587870cfbSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
34687870cfbSIuliana Prodan 	print_hex_dump_debug("acmac digest shdesc@" __stringify(__LINE__)" : ",
34787870cfbSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
34887870cfbSIuliana Prodan 			     desc_bytes(desc), 1);
34912b8567fSIuliana Prodan 
35012b8567fSIuliana Prodan 	return 0;
35112b8567fSIuliana Prodan }
35212b8567fSIuliana Prodan 
353045e3678SYuan Kang /* Digest hash size if it is too large */
35430724445SHoria Geantă static int hash_digest_key(struct caam_hash_ctx *ctx, u32 *keylen, u8 *key,
35530724445SHoria Geantă 			   u32 digestsize)
356045e3678SYuan Kang {
357045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
358045e3678SYuan Kang 	u32 *desc;
359045e3678SYuan Kang 	struct split_key_result result;
36030724445SHoria Geantă 	dma_addr_t key_dma;
3619e6df0fdSMarkus Elfring 	int ret;
362045e3678SYuan Kang 
3639c23b7d3SVakul Garg 	desc = kmalloc(CAAM_CMD_SZ * 8 + CAAM_PTR_SZ * 2, GFP_KERNEL | GFP_DMA);
3642af8f4a2SKim Phillips 	if (!desc) {
3652af8f4a2SKim Phillips 		dev_err(jrdev, "unable to allocate key input memory\n");
3662af8f4a2SKim Phillips 		return -ENOMEM;
3672af8f4a2SKim Phillips 	}
368045e3678SYuan Kang 
369045e3678SYuan Kang 	init_job_desc(desc, 0);
370045e3678SYuan Kang 
37130724445SHoria Geantă 	key_dma = dma_map_single(jrdev, key, *keylen, DMA_BIDIRECTIONAL);
37230724445SHoria Geantă 	if (dma_mapping_error(jrdev, key_dma)) {
37330724445SHoria Geantă 		dev_err(jrdev, "unable to map key memory\n");
374045e3678SYuan Kang 		kfree(desc);
375045e3678SYuan Kang 		return -ENOMEM;
376045e3678SYuan Kang 	}
377045e3678SYuan Kang 
378045e3678SYuan Kang 	/* Job descriptor to perform unkeyed hash on key_in */
379db57656bSHoria Geantă 	append_operation(desc, ctx->adata.algtype | OP_ALG_ENCRYPT |
380045e3678SYuan Kang 			 OP_ALG_AS_INITFINAL);
38130724445SHoria Geantă 	append_seq_in_ptr(desc, key_dma, *keylen, 0);
382045e3678SYuan Kang 	append_seq_fifo_load(desc, *keylen, FIFOLD_CLASS_CLASS2 |
383045e3678SYuan Kang 			     FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_MSG);
38430724445SHoria Geantă 	append_seq_out_ptr(desc, key_dma, digestsize, 0);
385045e3678SYuan Kang 	append_seq_store(desc, digestsize, LDST_CLASS_2_CCB |
386045e3678SYuan Kang 			 LDST_SRCDST_BYTE_CONTEXT);
387045e3678SYuan Kang 
3886e005503SSascha Hauer 	print_hex_dump_debug("key_in@"__stringify(__LINE__)": ",
38930724445SHoria Geantă 			     DUMP_PREFIX_ADDRESS, 16, 4, key, *keylen, 1);
3906e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
3916e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
3926e005503SSascha Hauer 			     1);
393045e3678SYuan Kang 
394045e3678SYuan Kang 	result.err = 0;
395045e3678SYuan Kang 	init_completion(&result.completion);
396045e3678SYuan Kang 
397045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, split_key_done, &result);
398045e3678SYuan Kang 	if (!ret) {
399045e3678SYuan Kang 		/* in progress */
4007459e1d2SHoria Geantă 		wait_for_completion(&result.completion);
401045e3678SYuan Kang 		ret = result.err;
4026e005503SSascha Hauer 
4036e005503SSascha Hauer 		print_hex_dump_debug("digested key@"__stringify(__LINE__)": ",
4046e005503SSascha Hauer 				     DUMP_PREFIX_ADDRESS, 16, 4, key,
4056e005503SSascha Hauer 				     digestsize, 1);
406045e3678SYuan Kang 	}
40730724445SHoria Geantă 	dma_unmap_single(jrdev, key_dma, *keylen, DMA_BIDIRECTIONAL);
408045e3678SYuan Kang 
409e11aa9f1SHoria Geanta 	*keylen = digestsize;
410e11aa9f1SHoria Geanta 
411045e3678SYuan Kang 	kfree(desc);
412045e3678SYuan Kang 
413045e3678SYuan Kang 	return ret;
414045e3678SYuan Kang }
415045e3678SYuan Kang 
416045e3678SYuan Kang static int ahash_setkey(struct crypto_ahash *ahash,
417045e3678SYuan Kang 			const u8 *key, unsigned int keylen)
418045e3678SYuan Kang {
419045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
4206e005503SSascha Hauer 	struct device *jrdev = ctx->jrdev;
421045e3678SYuan Kang 	int blocksize = crypto_tfm_alg_blocksize(&ahash->base);
422045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
4237e0880b9SHoria Geantă 	struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctx->jrdev->parent);
4249e6df0fdSMarkus Elfring 	int ret;
425045e3678SYuan Kang 	u8 *hashed_key = NULL;
426045e3678SYuan Kang 
4276e005503SSascha Hauer 	dev_dbg(jrdev, "keylen %d\n", keylen);
428045e3678SYuan Kang 
429045e3678SYuan Kang 	if (keylen > blocksize) {
43030724445SHoria Geantă 		hashed_key = kmemdup(key, keylen, GFP_KERNEL | GFP_DMA);
431045e3678SYuan Kang 		if (!hashed_key)
432045e3678SYuan Kang 			return -ENOMEM;
43330724445SHoria Geantă 		ret = hash_digest_key(ctx, &keylen, hashed_key, digestsize);
434045e3678SYuan Kang 		if (ret)
435d6e7a7d0SMarkus Elfring 			goto bad_free_key;
436045e3678SYuan Kang 		key = hashed_key;
437045e3678SYuan Kang 	}
438045e3678SYuan Kang 
4397e0880b9SHoria Geantă 	/*
4407e0880b9SHoria Geantă 	 * If DKP is supported, use it in the shared descriptor to generate
4417e0880b9SHoria Geantă 	 * the split key.
4427e0880b9SHoria Geantă 	 */
4437e0880b9SHoria Geantă 	if (ctrlpriv->era >= 6) {
4447e0880b9SHoria Geantă 		ctx->adata.key_inline = true;
4457e0880b9SHoria Geantă 		ctx->adata.keylen = keylen;
4467e0880b9SHoria Geantă 		ctx->adata.keylen_pad = split_key_len(ctx->adata.algtype &
4477e0880b9SHoria Geantă 						      OP_ALG_ALGSEL_MASK);
4487e0880b9SHoria Geantă 
4497e0880b9SHoria Geantă 		if (ctx->adata.keylen_pad > CAAM_MAX_HASH_KEY_SIZE)
450d6e7a7d0SMarkus Elfring 			goto bad_free_key;
451045e3678SYuan Kang 
4527e0880b9SHoria Geantă 		memcpy(ctx->key, key, keylen);
453e9b4913aSHoria Geantă 
454e9b4913aSHoria Geantă 		/*
455e9b4913aSHoria Geantă 		 * In case |user key| > |derived key|, using DKP<imm,imm>
456e9b4913aSHoria Geantă 		 * would result in invalid opcodes (last bytes of user key) in
457e9b4913aSHoria Geantă 		 * the resulting descriptor. Use DKP<ptr,imm> instead => both
458e9b4913aSHoria Geantă 		 * virtual and dma key addresses are needed.
459e9b4913aSHoria Geantă 		 */
460e9b4913aSHoria Geantă 		if (keylen > ctx->adata.keylen_pad)
461e9b4913aSHoria Geantă 			dma_sync_single_for_device(ctx->jrdev,
462e9b4913aSHoria Geantă 						   ctx->adata.key_dma,
463e9b4913aSHoria Geantă 						   ctx->adata.keylen_pad,
464e9b4913aSHoria Geantă 						   DMA_TO_DEVICE);
4657e0880b9SHoria Geantă 	} else {
4667e0880b9SHoria Geantă 		ret = gen_split_key(ctx->jrdev, ctx->key, &ctx->adata, key,
4677e0880b9SHoria Geantă 				    keylen, CAAM_MAX_HASH_KEY_SIZE);
4687e0880b9SHoria Geantă 		if (ret)
4697e0880b9SHoria Geantă 			goto bad_free_key;
4707e0880b9SHoria Geantă 	}
471045e3678SYuan Kang 
472045e3678SYuan Kang 	kfree(hashed_key);
473cfb725f6SHoria Geantă 	return ahash_set_sh_desc(ahash);
474d6e7a7d0SMarkus Elfring  bad_free_key:
475045e3678SYuan Kang 	kfree(hashed_key);
476045e3678SYuan Kang 	return -EINVAL;
477045e3678SYuan Kang }
478045e3678SYuan Kang 
47912b8567fSIuliana Prodan static int axcbc_setkey(struct crypto_ahash *ahash, const u8 *key,
48012b8567fSIuliana Prodan 			unsigned int keylen)
48112b8567fSIuliana Prodan {
48212b8567fSIuliana Prodan 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
48312b8567fSIuliana Prodan 	struct device *jrdev = ctx->jrdev;
48412b8567fSIuliana Prodan 
485674f368aSEric Biggers 	if (keylen != AES_KEYSIZE_128)
486836d8f43SIuliana Prodan 		return -EINVAL;
487836d8f43SIuliana Prodan 
48812b8567fSIuliana Prodan 	memcpy(ctx->key, key, keylen);
489a2fb864cSHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->adata.key_dma, keylen,
490a2fb864cSHoria Geantă 				   DMA_TO_DEVICE);
49112b8567fSIuliana Prodan 	ctx->adata.keylen = keylen;
49212b8567fSIuliana Prodan 
49312b8567fSIuliana Prodan 	print_hex_dump_debug("axcbc ctx.key@" __stringify(__LINE__)" : ",
49412b8567fSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, ctx->key, keylen, 1);
49512b8567fSIuliana Prodan 
49612b8567fSIuliana Prodan 	return axcbc_set_sh_desc(ahash);
49712b8567fSIuliana Prodan }
49887870cfbSIuliana Prodan 
49987870cfbSIuliana Prodan static int acmac_setkey(struct crypto_ahash *ahash, const u8 *key,
50087870cfbSIuliana Prodan 			unsigned int keylen)
50187870cfbSIuliana Prodan {
50287870cfbSIuliana Prodan 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
503836d8f43SIuliana Prodan 	int err;
504836d8f43SIuliana Prodan 
505836d8f43SIuliana Prodan 	err = aes_check_keylen(keylen);
506674f368aSEric Biggers 	if (err)
507836d8f43SIuliana Prodan 		return err;
50887870cfbSIuliana Prodan 
50987870cfbSIuliana Prodan 	/* key is immediate data for all cmac shared descriptors */
51087870cfbSIuliana Prodan 	ctx->adata.key_virt = key;
51187870cfbSIuliana Prodan 	ctx->adata.keylen = keylen;
51287870cfbSIuliana Prodan 
51387870cfbSIuliana Prodan 	print_hex_dump_debug("acmac ctx.key@" __stringify(__LINE__)" : ",
51487870cfbSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
51587870cfbSIuliana Prodan 
51687870cfbSIuliana Prodan 	return acmac_set_sh_desc(ahash);
51787870cfbSIuliana Prodan }
51887870cfbSIuliana Prodan 
519045e3678SYuan Kang /*
520045e3678SYuan Kang  * ahash_edesc - s/w-extended ahash descriptor
521045e3678SYuan Kang  * @sec4_sg_dma: physical mapped address of h/w link table
522045e3678SYuan Kang  * @src_nents: number of segments in input scatterlist
523045e3678SYuan Kang  * @sec4_sg_bytes: length of dma mapped sec4_sg space
524045e3678SYuan Kang  * @hw_desc: the h/w job descriptor followed by any referenced link tables
525343e44b1SRussell King  * @sec4_sg: h/w link table
526045e3678SYuan Kang  */
527045e3678SYuan Kang struct ahash_edesc {
528045e3678SYuan Kang 	dma_addr_t sec4_sg_dma;
529045e3678SYuan Kang 	int src_nents;
530045e3678SYuan Kang 	int sec4_sg_bytes;
5311a3daadcSAndrey Smirnov 	u32 hw_desc[DESC_JOB_IO_LEN_MAX / sizeof(u32)] ____cacheline_aligned;
532343e44b1SRussell King 	struct sec4_sg_entry sec4_sg[0];
533045e3678SYuan Kang };
534045e3678SYuan Kang 
535045e3678SYuan Kang static inline void ahash_unmap(struct device *dev,
536045e3678SYuan Kang 			struct ahash_edesc *edesc,
537045e3678SYuan Kang 			struct ahash_request *req, int dst_len)
538045e3678SYuan Kang {
539944c3d4dSHoria Geantă 	struct caam_hash_state *state = ahash_request_ctx(req);
540944c3d4dSHoria Geantă 
541045e3678SYuan Kang 	if (edesc->src_nents)
54213fb8fd7SLABBE Corentin 		dma_unmap_sg(dev, req->src, edesc->src_nents, DMA_TO_DEVICE);
543045e3678SYuan Kang 
544045e3678SYuan Kang 	if (edesc->sec4_sg_bytes)
545045e3678SYuan Kang 		dma_unmap_single(dev, edesc->sec4_sg_dma,
546045e3678SYuan Kang 				 edesc->sec4_sg_bytes, DMA_TO_DEVICE);
547944c3d4dSHoria Geantă 
548944c3d4dSHoria Geantă 	if (state->buf_dma) {
54946b49abcSAndrei Botila 		dma_unmap_single(dev, state->buf_dma, state->buflen,
550944c3d4dSHoria Geantă 				 DMA_TO_DEVICE);
551944c3d4dSHoria Geantă 		state->buf_dma = 0;
552944c3d4dSHoria Geantă 	}
553045e3678SYuan Kang }
554045e3678SYuan Kang 
555045e3678SYuan Kang static inline void ahash_unmap_ctx(struct device *dev,
556045e3678SYuan Kang 			struct ahash_edesc *edesc,
557045e3678SYuan Kang 			struct ahash_request *req, int dst_len, u32 flag)
558045e3678SYuan Kang {
559045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
560045e3678SYuan Kang 
56187ec02e7SHoria Geantă 	if (state->ctx_dma) {
56265055e21SFranck LENORMAND 		dma_unmap_single(dev, state->ctx_dma, state->ctx_dma_len, flag);
56387ec02e7SHoria Geantă 		state->ctx_dma = 0;
56487ec02e7SHoria Geantă 	}
565045e3678SYuan Kang 	ahash_unmap(dev, edesc, req, dst_len);
566045e3678SYuan Kang }
567045e3678SYuan Kang 
568c3f7394eSIuliana Prodan static inline void ahash_done_cpy(struct device *jrdev, u32 *desc, u32 err,
569c3f7394eSIuliana Prodan 				  void *context, enum dma_data_direction dir)
570045e3678SYuan Kang {
571045e3678SYuan Kang 	struct ahash_request *req = context;
572045e3678SYuan Kang 	struct ahash_edesc *edesc;
573045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
574045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
575c19650d6SHoria Geantă 	struct caam_hash_state *state = ahash_request_ctx(req);
576045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
5771984aaeeSHoria Geantă 	int ecode = 0;
578045e3678SYuan Kang 
5796e005503SSascha Hauer 	dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
580045e3678SYuan Kang 
5814ca7c7d8SHoria Geantă 	edesc = container_of(desc, struct ahash_edesc, hw_desc[0]);
582fa9659cdSMarek Vasut 	if (err)
5831984aaeeSHoria Geantă 		ecode = caam_jr_strstatus(jrdev, err);
584045e3678SYuan Kang 
585c3f7394eSIuliana Prodan 	ahash_unmap_ctx(jrdev, edesc, req, digestsize, dir);
586c19650d6SHoria Geantă 	memcpy(req->result, state->caam_ctx, digestsize);
587045e3678SYuan Kang 	kfree(edesc);
588045e3678SYuan Kang 
5896e005503SSascha Hauer 	print_hex_dump_debug("ctx@"__stringify(__LINE__)": ",
590045e3678SYuan Kang 			     DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
591045e3678SYuan Kang 			     ctx->ctx_len, 1);
592045e3678SYuan Kang 
5931984aaeeSHoria Geantă 	req->base.complete(&req->base, ecode);
594045e3678SYuan Kang }
595045e3678SYuan Kang 
596c3f7394eSIuliana Prodan static void ahash_done(struct device *jrdev, u32 *desc, u32 err,
597c3f7394eSIuliana Prodan 		       void *context)
598c3f7394eSIuliana Prodan {
599c3f7394eSIuliana Prodan 	ahash_done_cpy(jrdev, desc, err, context, DMA_FROM_DEVICE);
600c3f7394eSIuliana Prodan }
601c3f7394eSIuliana Prodan 
602c3f7394eSIuliana Prodan static void ahash_done_ctx_src(struct device *jrdev, u32 *desc, u32 err,
603c3f7394eSIuliana Prodan 			       void *context)
604c3f7394eSIuliana Prodan {
605c3f7394eSIuliana Prodan 	ahash_done_cpy(jrdev, desc, err, context, DMA_BIDIRECTIONAL);
606c3f7394eSIuliana Prodan }
607c3f7394eSIuliana Prodan 
608c3f7394eSIuliana Prodan static inline void ahash_done_switch(struct device *jrdev, u32 *desc, u32 err,
609c3f7394eSIuliana Prodan 				     void *context, enum dma_data_direction dir)
610c3f7394eSIuliana Prodan {
611c3f7394eSIuliana Prodan 	struct ahash_request *req = context;
612c3f7394eSIuliana Prodan 	struct ahash_edesc *edesc;
613c3f7394eSIuliana Prodan 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
614c3f7394eSIuliana Prodan 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
615c3f7394eSIuliana Prodan 	struct caam_hash_state *state = ahash_request_ctx(req);
616c3f7394eSIuliana Prodan 	int digestsize = crypto_ahash_digestsize(ahash);
617c3f7394eSIuliana Prodan 	int ecode = 0;
618c3f7394eSIuliana Prodan 
619c3f7394eSIuliana Prodan 	dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
620c3f7394eSIuliana Prodan 
621c3f7394eSIuliana Prodan 	edesc = container_of(desc, struct ahash_edesc, hw_desc[0]);
622c3f7394eSIuliana Prodan 	if (err)
623c3f7394eSIuliana Prodan 		ecode = caam_jr_strstatus(jrdev, err);
624c3f7394eSIuliana Prodan 
625c3f7394eSIuliana Prodan 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, dir);
626c3f7394eSIuliana Prodan 	kfree(edesc);
627c3f7394eSIuliana Prodan 
628c3f7394eSIuliana Prodan 	scatterwalk_map_and_copy(state->buf, req->src,
629c3f7394eSIuliana Prodan 				 req->nbytes - state->next_buflen,
630c3f7394eSIuliana Prodan 				 state->next_buflen, 0);
631c3f7394eSIuliana Prodan 	state->buflen = state->next_buflen;
632c3f7394eSIuliana Prodan 
633c3f7394eSIuliana Prodan 	print_hex_dump_debug("buf@" __stringify(__LINE__)": ",
634c3f7394eSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, state->buf,
635c3f7394eSIuliana Prodan 			     state->buflen, 1);
636c3f7394eSIuliana Prodan 
637c3f7394eSIuliana Prodan 	print_hex_dump_debug("ctx@"__stringify(__LINE__)": ",
638c3f7394eSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
639c3f7394eSIuliana Prodan 			     ctx->ctx_len, 1);
640c3f7394eSIuliana Prodan 	if (req->result)
641c3f7394eSIuliana Prodan 		print_hex_dump_debug("result@"__stringify(__LINE__)": ",
642c3f7394eSIuliana Prodan 				     DUMP_PREFIX_ADDRESS, 16, 4, req->result,
643c3f7394eSIuliana Prodan 				     digestsize, 1);
644c3f7394eSIuliana Prodan 
645c3f7394eSIuliana Prodan 	req->base.complete(&req->base, ecode);
646c3f7394eSIuliana Prodan }
647c3f7394eSIuliana Prodan 
648045e3678SYuan Kang static void ahash_done_bi(struct device *jrdev, u32 *desc, u32 err,
649045e3678SYuan Kang 			  void *context)
650045e3678SYuan Kang {
651c3f7394eSIuliana Prodan 	ahash_done_switch(jrdev, desc, err, context, DMA_BIDIRECTIONAL);
652045e3678SYuan Kang }
653045e3678SYuan Kang 
654045e3678SYuan Kang static void ahash_done_ctx_dst(struct device *jrdev, u32 *desc, u32 err,
655045e3678SYuan Kang 			       void *context)
656045e3678SYuan Kang {
657c3f7394eSIuliana Prodan 	ahash_done_switch(jrdev, desc, err, context, DMA_FROM_DEVICE);
658045e3678SYuan Kang }
659045e3678SYuan Kang 
6605588d039SRussell King /*
6615588d039SRussell King  * Allocate an enhanced descriptor, which contains the hardware descriptor
6625588d039SRussell King  * and space for hardware scatter table containing sg_num entries.
6635588d039SRussell King  */
664*2ba1e798SIuliana Prodan static struct ahash_edesc *ahash_edesc_alloc(struct ahash_request *req,
66530a43b44SRussell King 					     int sg_num, u32 *sh_desc,
666*2ba1e798SIuliana Prodan 					     dma_addr_t sh_desc_dma)
6675588d039SRussell King {
668*2ba1e798SIuliana Prodan 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
669*2ba1e798SIuliana Prodan 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
670*2ba1e798SIuliana Prodan 	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
671*2ba1e798SIuliana Prodan 		       GFP_KERNEL : GFP_ATOMIC;
6725588d039SRussell King 	struct ahash_edesc *edesc;
6735588d039SRussell King 	unsigned int sg_size = sg_num * sizeof(struct sec4_sg_entry);
6745588d039SRussell King 
6755588d039SRussell King 	edesc = kzalloc(sizeof(*edesc) + sg_size, GFP_DMA | flags);
6765588d039SRussell King 	if (!edesc) {
6775588d039SRussell King 		dev_err(ctx->jrdev, "could not allocate extended descriptor\n");
6785588d039SRussell King 		return NULL;
6795588d039SRussell King 	}
6805588d039SRussell King 
68130a43b44SRussell King 	init_job_desc_shared(edesc->hw_desc, sh_desc_dma, desc_len(sh_desc),
68230a43b44SRussell King 			     HDR_SHARE_DEFER | HDR_REVERSE);
68330a43b44SRussell King 
6845588d039SRussell King 	return edesc;
6855588d039SRussell King }
6865588d039SRussell King 
68765cf164aSRussell King static int ahash_edesc_add_src(struct caam_hash_ctx *ctx,
68865cf164aSRussell King 			       struct ahash_edesc *edesc,
68965cf164aSRussell King 			       struct ahash_request *req, int nents,
69065cf164aSRussell King 			       unsigned int first_sg,
69165cf164aSRussell King 			       unsigned int first_bytes, size_t to_hash)
69265cf164aSRussell King {
69365cf164aSRussell King 	dma_addr_t src_dma;
69465cf164aSRussell King 	u32 options;
69565cf164aSRussell King 
69665cf164aSRussell King 	if (nents > 1 || first_sg) {
69765cf164aSRussell King 		struct sec4_sg_entry *sg = edesc->sec4_sg;
698a5e5c133SHoria Geantă 		unsigned int sgsize = sizeof(*sg) *
699a5e5c133SHoria Geantă 				      pad_sg_nents(first_sg + nents);
70065cf164aSRussell King 
701059d73eeSHoria Geantă 		sg_to_sec4_sg_last(req->src, to_hash, sg + first_sg, 0);
70265cf164aSRussell King 
70365cf164aSRussell King 		src_dma = dma_map_single(ctx->jrdev, sg, sgsize, DMA_TO_DEVICE);
70465cf164aSRussell King 		if (dma_mapping_error(ctx->jrdev, src_dma)) {
70565cf164aSRussell King 			dev_err(ctx->jrdev, "unable to map S/G table\n");
70665cf164aSRussell King 			return -ENOMEM;
70765cf164aSRussell King 		}
70865cf164aSRussell King 
70965cf164aSRussell King 		edesc->sec4_sg_bytes = sgsize;
71065cf164aSRussell King 		edesc->sec4_sg_dma = src_dma;
71165cf164aSRussell King 		options = LDST_SGF;
71265cf164aSRussell King 	} else {
71365cf164aSRussell King 		src_dma = sg_dma_address(req->src);
71465cf164aSRussell King 		options = 0;
71565cf164aSRussell King 	}
71665cf164aSRussell King 
71765cf164aSRussell King 	append_seq_in_ptr(edesc->hw_desc, src_dma, first_bytes + to_hash,
71865cf164aSRussell King 			  options);
71965cf164aSRussell King 
72065cf164aSRussell King 	return 0;
72165cf164aSRussell King }
72265cf164aSRussell King 
723045e3678SYuan Kang /* submit update job descriptor */
724045e3678SYuan Kang static int ahash_update_ctx(struct ahash_request *req)
725045e3678SYuan Kang {
726045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
727045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
728045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
729045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
73046b49abcSAndrei Botila 	u8 *buf = state->buf;
73146b49abcSAndrei Botila 	int *buflen = &state->buflen;
73246b49abcSAndrei Botila 	int *next_buflen = &state->next_buflen;
73312b8567fSIuliana Prodan 	int blocksize = crypto_ahash_blocksize(ahash);
734045e3678SYuan Kang 	int in_len = *buflen + req->nbytes, to_hash;
73530a43b44SRussell King 	u32 *desc;
736bc13c69eSRussell King 	int src_nents, mapped_nents, sec4_sg_bytes, sec4_sg_src_index;
737045e3678SYuan Kang 	struct ahash_edesc *edesc;
738045e3678SYuan Kang 	int ret = 0;
739045e3678SYuan Kang 
74012b8567fSIuliana Prodan 	*next_buflen = in_len & (blocksize - 1);
741045e3678SYuan Kang 	to_hash = in_len - *next_buflen;
742045e3678SYuan Kang 
74312b8567fSIuliana Prodan 	/*
74487870cfbSIuliana Prodan 	 * For XCBC and CMAC, if to_hash is multiple of block size,
74512b8567fSIuliana Prodan 	 * keep last block in internal buffer
74612b8567fSIuliana Prodan 	 */
74787870cfbSIuliana Prodan 	if ((is_xcbc_aes(ctx->adata.algtype) ||
74887870cfbSIuliana Prodan 	     is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize &&
74912b8567fSIuliana Prodan 	     (*next_buflen == 0)) {
75012b8567fSIuliana Prodan 		*next_buflen = blocksize;
75112b8567fSIuliana Prodan 		to_hash -= blocksize;
75212b8567fSIuliana Prodan 	}
75312b8567fSIuliana Prodan 
754045e3678SYuan Kang 	if (to_hash) {
755a5e5c133SHoria Geantă 		int pad_nents;
756059d73eeSHoria Geantă 		int src_len = req->nbytes - *next_buflen;
757a5e5c133SHoria Geantă 
758059d73eeSHoria Geantă 		src_nents = sg_nents_for_len(req->src, src_len);
759f9970c28SLABBE Corentin 		if (src_nents < 0) {
760f9970c28SLABBE Corentin 			dev_err(jrdev, "Invalid number of src SG.\n");
761f9970c28SLABBE Corentin 			return src_nents;
762f9970c28SLABBE Corentin 		}
763bc13c69eSRussell King 
764bc13c69eSRussell King 		if (src_nents) {
765bc13c69eSRussell King 			mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
766bc13c69eSRussell King 						  DMA_TO_DEVICE);
767bc13c69eSRussell King 			if (!mapped_nents) {
768bc13c69eSRussell King 				dev_err(jrdev, "unable to DMA map source\n");
769bc13c69eSRussell King 				return -ENOMEM;
770bc13c69eSRussell King 			}
771bc13c69eSRussell King 		} else {
772bc13c69eSRussell King 			mapped_nents = 0;
773bc13c69eSRussell King 		}
774bc13c69eSRussell King 
775045e3678SYuan Kang 		sec4_sg_src_index = 1 + (*buflen ? 1 : 0);
776a5e5c133SHoria Geantă 		pad_nents = pad_sg_nents(sec4_sg_src_index + mapped_nents);
777a5e5c133SHoria Geantă 		sec4_sg_bytes = pad_nents * sizeof(struct sec4_sg_entry);
778045e3678SYuan Kang 
779045e3678SYuan Kang 		/*
780045e3678SYuan Kang 		 * allocate space for base edesc and hw desc commands,
781045e3678SYuan Kang 		 * link tables
782045e3678SYuan Kang 		 */
783*2ba1e798SIuliana Prodan 		edesc = ahash_edesc_alloc(req, pad_nents, ctx->sh_desc_update,
784*2ba1e798SIuliana Prodan 					  ctx->sh_desc_update_dma);
785045e3678SYuan Kang 		if (!edesc) {
786bc13c69eSRussell King 			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
787045e3678SYuan Kang 			return -ENOMEM;
788045e3678SYuan Kang 		}
789045e3678SYuan Kang 
790045e3678SYuan Kang 		edesc->src_nents = src_nents;
791045e3678SYuan Kang 		edesc->sec4_sg_bytes = sec4_sg_bytes;
792045e3678SYuan Kang 
793dfcd8393SHoria Geantă 		ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
794045e3678SYuan Kang 					 edesc->sec4_sg, DMA_BIDIRECTIONAL);
795ce572085SHoria Geanta 		if (ret)
79658b0e5d0SMarkus Elfring 			goto unmap_ctx;
797045e3678SYuan Kang 
798944c3d4dSHoria Geantă 		ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
799944c3d4dSHoria Geantă 		if (ret)
800944c3d4dSHoria Geantă 			goto unmap_ctx;
801045e3678SYuan Kang 
802b4e9e931SIuliana Prodan 		if (mapped_nents)
803059d73eeSHoria Geantă 			sg_to_sec4_sg_last(req->src, src_len,
804bc13c69eSRussell King 					   edesc->sec4_sg + sec4_sg_src_index,
805bc13c69eSRussell King 					   0);
806b4e9e931SIuliana Prodan 		else
807b4e9e931SIuliana Prodan 			sg_to_sec4_set_last(edesc->sec4_sg + sec4_sg_src_index -
808b4e9e931SIuliana Prodan 					    1);
809b4e9e931SIuliana Prodan 
810045e3678SYuan Kang 		desc = edesc->hw_desc;
811045e3678SYuan Kang 
8121da2be33SRuchika Gupta 		edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
8131da2be33SRuchika Gupta 						     sec4_sg_bytes,
8141da2be33SRuchika Gupta 						     DMA_TO_DEVICE);
815ce572085SHoria Geanta 		if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
816ce572085SHoria Geanta 			dev_err(jrdev, "unable to map S/G table\n");
81732686d34SRussell King 			ret = -ENOMEM;
81858b0e5d0SMarkus Elfring 			goto unmap_ctx;
819ce572085SHoria Geanta 		}
8201da2be33SRuchika Gupta 
821045e3678SYuan Kang 		append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len +
822045e3678SYuan Kang 				       to_hash, LDST_SGF);
823045e3678SYuan Kang 
824045e3678SYuan Kang 		append_seq_out_ptr(desc, state->ctx_dma, ctx->ctx_len, 0);
825045e3678SYuan Kang 
8266e005503SSascha Hauer 		print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
827045e3678SYuan Kang 				     DUMP_PREFIX_ADDRESS, 16, 4, desc,
828045e3678SYuan Kang 				     desc_bytes(desc), 1);
829045e3678SYuan Kang 
830045e3678SYuan Kang 		ret = caam_jr_enqueue(jrdev, desc, ahash_done_bi, req);
83132686d34SRussell King 		if (ret)
83258b0e5d0SMarkus Elfring 			goto unmap_ctx;
83332686d34SRussell King 
834045e3678SYuan Kang 		ret = -EINPROGRESS;
835045e3678SYuan Kang 	} else if (*next_buflen) {
836307fd543SCristian Stoica 		scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
837307fd543SCristian Stoica 					 req->nbytes, 0);
838045e3678SYuan Kang 		*buflen = *next_buflen;
8396e005503SSascha Hauer 
8406e005503SSascha Hauer 		print_hex_dump_debug("buf@" __stringify(__LINE__)": ",
84146b49abcSAndrei Botila 				     DUMP_PREFIX_ADDRESS, 16, 4, buf,
84246b49abcSAndrei Botila 				     *buflen, 1);
84346b49abcSAndrei Botila 	}
844045e3678SYuan Kang 
845045e3678SYuan Kang 	return ret;
84658b0e5d0SMarkus Elfring unmap_ctx:
84732686d34SRussell King 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_BIDIRECTIONAL);
84832686d34SRussell King 	kfree(edesc);
84932686d34SRussell King 	return ret;
850045e3678SYuan Kang }
851045e3678SYuan Kang 
852045e3678SYuan Kang static int ahash_final_ctx(struct ahash_request *req)
853045e3678SYuan Kang {
854045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
855045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
856045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
857045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
85846b49abcSAndrei Botila 	int buflen = state->buflen;
85930a43b44SRussell King 	u32 *desc;
860a5e5c133SHoria Geantă 	int sec4_sg_bytes;
861045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
862045e3678SYuan Kang 	struct ahash_edesc *edesc;
8639e6df0fdSMarkus Elfring 	int ret;
864045e3678SYuan Kang 
865a5e5c133SHoria Geantă 	sec4_sg_bytes = pad_sg_nents(1 + (buflen ? 1 : 0)) *
866a5e5c133SHoria Geantă 			sizeof(struct sec4_sg_entry);
867045e3678SYuan Kang 
868045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
869*2ba1e798SIuliana Prodan 	edesc = ahash_edesc_alloc(req, 4, ctx->sh_desc_fin,
870*2ba1e798SIuliana Prodan 				  ctx->sh_desc_fin_dma);
8715588d039SRussell King 	if (!edesc)
872045e3678SYuan Kang 		return -ENOMEM;
873045e3678SYuan Kang 
874045e3678SYuan Kang 	desc = edesc->hw_desc;
875045e3678SYuan Kang 
876045e3678SYuan Kang 	edesc->sec4_sg_bytes = sec4_sg_bytes;
877045e3678SYuan Kang 
878dfcd8393SHoria Geantă 	ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
879c19650d6SHoria Geantă 				 edesc->sec4_sg, DMA_BIDIRECTIONAL);
880ce572085SHoria Geanta 	if (ret)
88158b0e5d0SMarkus Elfring 		goto unmap_ctx;
882045e3678SYuan Kang 
883944c3d4dSHoria Geantă 	ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
884944c3d4dSHoria Geantă 	if (ret)
885944c3d4dSHoria Geantă 		goto unmap_ctx;
886944c3d4dSHoria Geantă 
887a5e5c133SHoria Geantă 	sg_to_sec4_set_last(edesc->sec4_sg + (buflen ? 1 : 0));
888045e3678SYuan Kang 
8891da2be33SRuchika Gupta 	edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
8901da2be33SRuchika Gupta 					    sec4_sg_bytes, DMA_TO_DEVICE);
891ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
892ce572085SHoria Geanta 		dev_err(jrdev, "unable to map S/G table\n");
89332686d34SRussell King 		ret = -ENOMEM;
89458b0e5d0SMarkus Elfring 		goto unmap_ctx;
895ce572085SHoria Geanta 	}
8961da2be33SRuchika Gupta 
897045e3678SYuan Kang 	append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len + buflen,
898045e3678SYuan Kang 			  LDST_SGF);
899c19650d6SHoria Geantă 	append_seq_out_ptr(desc, state->ctx_dma, digestsize, 0);
900045e3678SYuan Kang 
9016e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
9026e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
9036e005503SSascha Hauer 			     1);
904045e3678SYuan Kang 
905045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_src, req);
90632686d34SRussell King 	if (ret)
90758b0e5d0SMarkus Elfring 		goto unmap_ctx;
90832686d34SRussell King 
90932686d34SRussell King 	return -EINPROGRESS;
91058b0e5d0SMarkus Elfring  unmap_ctx:
911c19650d6SHoria Geantă 	ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL);
912045e3678SYuan Kang 	kfree(edesc);
913045e3678SYuan Kang 	return ret;
914045e3678SYuan Kang }
915045e3678SYuan Kang 
916045e3678SYuan Kang static int ahash_finup_ctx(struct ahash_request *req)
917045e3678SYuan Kang {
918045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
919045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
920045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
921045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
92246b49abcSAndrei Botila 	int buflen = state->buflen;
92330a43b44SRussell King 	u32 *desc;
92465cf164aSRussell King 	int sec4_sg_src_index;
925bc13c69eSRussell King 	int src_nents, mapped_nents;
926045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
927045e3678SYuan Kang 	struct ahash_edesc *edesc;
9289e6df0fdSMarkus Elfring 	int ret;
929045e3678SYuan Kang 
93013fb8fd7SLABBE Corentin 	src_nents = sg_nents_for_len(req->src, req->nbytes);
931f9970c28SLABBE Corentin 	if (src_nents < 0) {
932f9970c28SLABBE Corentin 		dev_err(jrdev, "Invalid number of src SG.\n");
933f9970c28SLABBE Corentin 		return src_nents;
934f9970c28SLABBE Corentin 	}
935bc13c69eSRussell King 
936bc13c69eSRussell King 	if (src_nents) {
937bc13c69eSRussell King 		mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
938bc13c69eSRussell King 					  DMA_TO_DEVICE);
939bc13c69eSRussell King 		if (!mapped_nents) {
940bc13c69eSRussell King 			dev_err(jrdev, "unable to DMA map source\n");
941bc13c69eSRussell King 			return -ENOMEM;
942bc13c69eSRussell King 		}
943bc13c69eSRussell King 	} else {
944bc13c69eSRussell King 		mapped_nents = 0;
945bc13c69eSRussell King 	}
946bc13c69eSRussell King 
947045e3678SYuan Kang 	sec4_sg_src_index = 1 + (buflen ? 1 : 0);
948045e3678SYuan Kang 
949045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
950*2ba1e798SIuliana Prodan 	edesc = ahash_edesc_alloc(req, sec4_sg_src_index + mapped_nents,
951*2ba1e798SIuliana Prodan 				  ctx->sh_desc_fin, ctx->sh_desc_fin_dma);
952045e3678SYuan Kang 	if (!edesc) {
953bc13c69eSRussell King 		dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
954045e3678SYuan Kang 		return -ENOMEM;
955045e3678SYuan Kang 	}
956045e3678SYuan Kang 
957045e3678SYuan Kang 	desc = edesc->hw_desc;
958045e3678SYuan Kang 
959045e3678SYuan Kang 	edesc->src_nents = src_nents;
960045e3678SYuan Kang 
961dfcd8393SHoria Geantă 	ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
962c19650d6SHoria Geantă 				 edesc->sec4_sg, DMA_BIDIRECTIONAL);
963ce572085SHoria Geanta 	if (ret)
96458b0e5d0SMarkus Elfring 		goto unmap_ctx;
965045e3678SYuan Kang 
966944c3d4dSHoria Geantă 	ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
967944c3d4dSHoria Geantă 	if (ret)
968944c3d4dSHoria Geantă 		goto unmap_ctx;
969045e3678SYuan Kang 
97065cf164aSRussell King 	ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents,
97165cf164aSRussell King 				  sec4_sg_src_index, ctx->ctx_len + buflen,
97265cf164aSRussell King 				  req->nbytes);
97365cf164aSRussell King 	if (ret)
97458b0e5d0SMarkus Elfring 		goto unmap_ctx;
975045e3678SYuan Kang 
976c19650d6SHoria Geantă 	append_seq_out_ptr(desc, state->ctx_dma, digestsize, 0);
977045e3678SYuan Kang 
9786e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
9796e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
9806e005503SSascha Hauer 			     1);
981045e3678SYuan Kang 
982045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_src, req);
98332686d34SRussell King 	if (ret)
98458b0e5d0SMarkus Elfring 		goto unmap_ctx;
98532686d34SRussell King 
98632686d34SRussell King 	return -EINPROGRESS;
98758b0e5d0SMarkus Elfring  unmap_ctx:
988c19650d6SHoria Geantă 	ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL);
989045e3678SYuan Kang 	kfree(edesc);
990045e3678SYuan Kang 	return ret;
991045e3678SYuan Kang }
992045e3678SYuan Kang 
993045e3678SYuan Kang static int ahash_digest(struct ahash_request *req)
994045e3678SYuan Kang {
995045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
996045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
997944c3d4dSHoria Geantă 	struct caam_hash_state *state = ahash_request_ctx(req);
998045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
99930a43b44SRussell King 	u32 *desc;
1000045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
100165cf164aSRussell King 	int src_nents, mapped_nents;
1002045e3678SYuan Kang 	struct ahash_edesc *edesc;
10039e6df0fdSMarkus Elfring 	int ret;
1004045e3678SYuan Kang 
1005944c3d4dSHoria Geantă 	state->buf_dma = 0;
1006944c3d4dSHoria Geantă 
10073d5a2db6SRussell King 	src_nents = sg_nents_for_len(req->src, req->nbytes);
1008f9970c28SLABBE Corentin 	if (src_nents < 0) {
1009f9970c28SLABBE Corentin 		dev_err(jrdev, "Invalid number of src SG.\n");
1010f9970c28SLABBE Corentin 		return src_nents;
1011f9970c28SLABBE Corentin 	}
1012bc13c69eSRussell King 
1013bc13c69eSRussell King 	if (src_nents) {
1014bc13c69eSRussell King 		mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1015bc13c69eSRussell King 					  DMA_TO_DEVICE);
1016bc13c69eSRussell King 		if (!mapped_nents) {
1017bc13c69eSRussell King 			dev_err(jrdev, "unable to map source for DMA\n");
1018bc13c69eSRussell King 			return -ENOMEM;
1019bc13c69eSRussell King 		}
1020bc13c69eSRussell King 	} else {
1021bc13c69eSRussell King 		mapped_nents = 0;
1022bc13c69eSRussell King 	}
1023bc13c69eSRussell King 
1024045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
1025*2ba1e798SIuliana Prodan 	edesc = ahash_edesc_alloc(req, mapped_nents > 1 ? mapped_nents : 0,
1026*2ba1e798SIuliana Prodan 				  ctx->sh_desc_digest, ctx->sh_desc_digest_dma);
1027045e3678SYuan Kang 	if (!edesc) {
1028bc13c69eSRussell King 		dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1029045e3678SYuan Kang 		return -ENOMEM;
1030045e3678SYuan Kang 	}
1031343e44b1SRussell King 
1032045e3678SYuan Kang 	edesc->src_nents = src_nents;
1033045e3678SYuan Kang 
103465cf164aSRussell King 	ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 0, 0,
103565cf164aSRussell King 				  req->nbytes);
103665cf164aSRussell King 	if (ret) {
103732686d34SRussell King 		ahash_unmap(jrdev, edesc, req, digestsize);
103832686d34SRussell King 		kfree(edesc);
103965cf164aSRussell King 		return ret;
1040ce572085SHoria Geanta 	}
104165cf164aSRussell King 
104265cf164aSRussell King 	desc = edesc->hw_desc;
1043045e3678SYuan Kang 
1044c19650d6SHoria Geantă 	ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
1045c19650d6SHoria Geantă 	if (ret) {
104632686d34SRussell King 		ahash_unmap(jrdev, edesc, req, digestsize);
104732686d34SRussell King 		kfree(edesc);
1048ce572085SHoria Geanta 		return -ENOMEM;
1049ce572085SHoria Geanta 	}
1050045e3678SYuan Kang 
10516e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
10526e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
10536e005503SSascha Hauer 			     1);
1054045e3678SYuan Kang 
1055045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, ahash_done, req);
1056045e3678SYuan Kang 	if (!ret) {
1057045e3678SYuan Kang 		ret = -EINPROGRESS;
1058045e3678SYuan Kang 	} else {
1059c19650d6SHoria Geantă 		ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
1060045e3678SYuan Kang 		kfree(edesc);
1061045e3678SYuan Kang 	}
1062045e3678SYuan Kang 
1063045e3678SYuan Kang 	return ret;
1064045e3678SYuan Kang }
1065045e3678SYuan Kang 
1066045e3678SYuan Kang /* submit ahash final if it the first job descriptor */
1067045e3678SYuan Kang static int ahash_final_no_ctx(struct ahash_request *req)
1068045e3678SYuan Kang {
1069045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1070045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1071045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1072045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
107346b49abcSAndrei Botila 	u8 *buf = state->buf;
107446b49abcSAndrei Botila 	int buflen = state->buflen;
107530a43b44SRussell King 	u32 *desc;
1076045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
1077045e3678SYuan Kang 	struct ahash_edesc *edesc;
10789e6df0fdSMarkus Elfring 	int ret;
1079045e3678SYuan Kang 
1080045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
1081*2ba1e798SIuliana Prodan 	edesc = ahash_edesc_alloc(req, 0, ctx->sh_desc_digest,
1082*2ba1e798SIuliana Prodan 				  ctx->sh_desc_digest_dma);
10835588d039SRussell King 	if (!edesc)
1084045e3678SYuan Kang 		return -ENOMEM;
1085045e3678SYuan Kang 
1086045e3678SYuan Kang 	desc = edesc->hw_desc;
1087045e3678SYuan Kang 
108804e6d25cSAymen Sghaier 	if (buflen) {
108904e6d25cSAymen Sghaier 		state->buf_dma = dma_map_single(jrdev, buf, buflen,
109004e6d25cSAymen Sghaier 						DMA_TO_DEVICE);
1091ce572085SHoria Geanta 		if (dma_mapping_error(jrdev, state->buf_dma)) {
1092ce572085SHoria Geanta 			dev_err(jrdev, "unable to map src\n");
109306435f34SMarkus Elfring 			goto unmap;
1094ce572085SHoria Geanta 		}
1095045e3678SYuan Kang 
1096045e3678SYuan Kang 		append_seq_in_ptr(desc, state->buf_dma, buflen, 0);
109704e6d25cSAymen Sghaier 	}
1098045e3678SYuan Kang 
1099c19650d6SHoria Geantă 	ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
1100c19650d6SHoria Geantă 	if (ret)
110106435f34SMarkus Elfring 		goto unmap;
1102045e3678SYuan Kang 
11036e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
11046e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
11056e005503SSascha Hauer 			     1);
1106045e3678SYuan Kang 
1107045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, ahash_done, req);
1108045e3678SYuan Kang 	if (!ret) {
1109045e3678SYuan Kang 		ret = -EINPROGRESS;
1110045e3678SYuan Kang 	} else {
1111c19650d6SHoria Geantă 		ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
1112045e3678SYuan Kang 		kfree(edesc);
1113045e3678SYuan Kang 	}
1114045e3678SYuan Kang 
1115045e3678SYuan Kang 	return ret;
111606435f34SMarkus Elfring  unmap:
111706435f34SMarkus Elfring 	ahash_unmap(jrdev, edesc, req, digestsize);
111806435f34SMarkus Elfring 	kfree(edesc);
111906435f34SMarkus Elfring 	return -ENOMEM;
112006435f34SMarkus Elfring 
1121045e3678SYuan Kang }
1122045e3678SYuan Kang 
1123045e3678SYuan Kang /* submit ahash update if it the first job descriptor after update */
1124045e3678SYuan Kang static int ahash_update_no_ctx(struct ahash_request *req)
1125045e3678SYuan Kang {
1126045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1127045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1128045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1129045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
113046b49abcSAndrei Botila 	u8 *buf = state->buf;
113146b49abcSAndrei Botila 	int *buflen = &state->buflen;
113246b49abcSAndrei Botila 	int *next_buflen = &state->next_buflen;
113312b8567fSIuliana Prodan 	int blocksize = crypto_ahash_blocksize(ahash);
1134045e3678SYuan Kang 	int in_len = *buflen + req->nbytes, to_hash;
1135bc13c69eSRussell King 	int sec4_sg_bytes, src_nents, mapped_nents;
1136045e3678SYuan Kang 	struct ahash_edesc *edesc;
113730a43b44SRussell King 	u32 *desc;
1138045e3678SYuan Kang 	int ret = 0;
1139045e3678SYuan Kang 
114012b8567fSIuliana Prodan 	*next_buflen = in_len & (blocksize - 1);
1141045e3678SYuan Kang 	to_hash = in_len - *next_buflen;
1142045e3678SYuan Kang 
114312b8567fSIuliana Prodan 	/*
114487870cfbSIuliana Prodan 	 * For XCBC and CMAC, if to_hash is multiple of block size,
114512b8567fSIuliana Prodan 	 * keep last block in internal buffer
114612b8567fSIuliana Prodan 	 */
114787870cfbSIuliana Prodan 	if ((is_xcbc_aes(ctx->adata.algtype) ||
114887870cfbSIuliana Prodan 	     is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize &&
114912b8567fSIuliana Prodan 	     (*next_buflen == 0)) {
115012b8567fSIuliana Prodan 		*next_buflen = blocksize;
115112b8567fSIuliana Prodan 		to_hash -= blocksize;
115212b8567fSIuliana Prodan 	}
115312b8567fSIuliana Prodan 
1154045e3678SYuan Kang 	if (to_hash) {
1155a5e5c133SHoria Geantă 		int pad_nents;
1156059d73eeSHoria Geantă 		int src_len = req->nbytes - *next_buflen;
1157a5e5c133SHoria Geantă 
1158059d73eeSHoria Geantă 		src_nents = sg_nents_for_len(req->src, src_len);
1159f9970c28SLABBE Corentin 		if (src_nents < 0) {
1160f9970c28SLABBE Corentin 			dev_err(jrdev, "Invalid number of src SG.\n");
1161f9970c28SLABBE Corentin 			return src_nents;
1162f9970c28SLABBE Corentin 		}
1163bc13c69eSRussell King 
1164bc13c69eSRussell King 		if (src_nents) {
1165bc13c69eSRussell King 			mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1166bc13c69eSRussell King 						  DMA_TO_DEVICE);
1167bc13c69eSRussell King 			if (!mapped_nents) {
1168bc13c69eSRussell King 				dev_err(jrdev, "unable to DMA map source\n");
1169bc13c69eSRussell King 				return -ENOMEM;
1170bc13c69eSRussell King 			}
1171bc13c69eSRussell King 		} else {
1172bc13c69eSRussell King 			mapped_nents = 0;
1173bc13c69eSRussell King 		}
1174bc13c69eSRussell King 
1175a5e5c133SHoria Geantă 		pad_nents = pad_sg_nents(1 + mapped_nents);
1176a5e5c133SHoria Geantă 		sec4_sg_bytes = pad_nents * sizeof(struct sec4_sg_entry);
1177045e3678SYuan Kang 
1178045e3678SYuan Kang 		/*
1179045e3678SYuan Kang 		 * allocate space for base edesc and hw desc commands,
1180045e3678SYuan Kang 		 * link tables
1181045e3678SYuan Kang 		 */
1182*2ba1e798SIuliana Prodan 		edesc = ahash_edesc_alloc(req, pad_nents,
118330a43b44SRussell King 					  ctx->sh_desc_update_first,
1184*2ba1e798SIuliana Prodan 					  ctx->sh_desc_update_first_dma);
1185045e3678SYuan Kang 		if (!edesc) {
1186bc13c69eSRussell King 			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1187045e3678SYuan Kang 			return -ENOMEM;
1188045e3678SYuan Kang 		}
1189045e3678SYuan Kang 
1190045e3678SYuan Kang 		edesc->src_nents = src_nents;
1191045e3678SYuan Kang 		edesc->sec4_sg_bytes = sec4_sg_bytes;
1192045e3678SYuan Kang 
1193944c3d4dSHoria Geantă 		ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg, state);
1194944c3d4dSHoria Geantă 		if (ret)
1195944c3d4dSHoria Geantă 			goto unmap_ctx;
1196944c3d4dSHoria Geantă 
1197059d73eeSHoria Geantă 		sg_to_sec4_sg_last(req->src, src_len, edesc->sec4_sg + 1, 0);
1198bc13c69eSRussell King 
1199045e3678SYuan Kang 		desc = edesc->hw_desc;
1200045e3678SYuan Kang 
12011da2be33SRuchika Gupta 		edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
12021da2be33SRuchika Gupta 						    sec4_sg_bytes,
12031da2be33SRuchika Gupta 						    DMA_TO_DEVICE);
1204ce572085SHoria Geanta 		if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
1205ce572085SHoria Geanta 			dev_err(jrdev, "unable to map S/G table\n");
120632686d34SRussell King 			ret = -ENOMEM;
120758b0e5d0SMarkus Elfring 			goto unmap_ctx;
1208ce572085SHoria Geanta 		}
12091da2be33SRuchika Gupta 
1210045e3678SYuan Kang 		append_seq_in_ptr(desc, edesc->sec4_sg_dma, to_hash, LDST_SGF);
1211045e3678SYuan Kang 
1212ce572085SHoria Geanta 		ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len);
1213ce572085SHoria Geanta 		if (ret)
121458b0e5d0SMarkus Elfring 			goto unmap_ctx;
1215045e3678SYuan Kang 
12166e005503SSascha Hauer 		print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
1217045e3678SYuan Kang 				     DUMP_PREFIX_ADDRESS, 16, 4, desc,
1218045e3678SYuan Kang 				     desc_bytes(desc), 1);
1219045e3678SYuan Kang 
1220045e3678SYuan Kang 		ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_dst, req);
122132686d34SRussell King 		if (ret)
122258b0e5d0SMarkus Elfring 			goto unmap_ctx;
122332686d34SRussell King 
1224045e3678SYuan Kang 		ret = -EINPROGRESS;
1225045e3678SYuan Kang 		state->update = ahash_update_ctx;
1226045e3678SYuan Kang 		state->finup = ahash_finup_ctx;
1227045e3678SYuan Kang 		state->final = ahash_final_ctx;
1228045e3678SYuan Kang 	} else if (*next_buflen) {
1229307fd543SCristian Stoica 		scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
1230307fd543SCristian Stoica 					 req->nbytes, 0);
1231045e3678SYuan Kang 		*buflen = *next_buflen;
12326e005503SSascha Hauer 
12336e005503SSascha Hauer 		print_hex_dump_debug("buf@" __stringify(__LINE__)": ",
123446b49abcSAndrei Botila 				     DUMP_PREFIX_ADDRESS, 16, 4, buf,
123546b49abcSAndrei Botila 				     *buflen, 1);
123646b49abcSAndrei Botila 	}
1237045e3678SYuan Kang 
1238045e3678SYuan Kang 	return ret;
123958b0e5d0SMarkus Elfring  unmap_ctx:
124032686d34SRussell King 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE);
124132686d34SRussell King 	kfree(edesc);
124232686d34SRussell King 	return ret;
1243045e3678SYuan Kang }
1244045e3678SYuan Kang 
1245045e3678SYuan Kang /* submit ahash finup if it the first job descriptor after update */
1246045e3678SYuan Kang static int ahash_finup_no_ctx(struct ahash_request *req)
1247045e3678SYuan Kang {
1248045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1249045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1250045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1251045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
125246b49abcSAndrei Botila 	int buflen = state->buflen;
125330a43b44SRussell King 	u32 *desc;
1254bc13c69eSRussell King 	int sec4_sg_bytes, sec4_sg_src_index, src_nents, mapped_nents;
1255045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
1256045e3678SYuan Kang 	struct ahash_edesc *edesc;
12579e6df0fdSMarkus Elfring 	int ret;
1258045e3678SYuan Kang 
125913fb8fd7SLABBE Corentin 	src_nents = sg_nents_for_len(req->src, req->nbytes);
1260f9970c28SLABBE Corentin 	if (src_nents < 0) {
1261f9970c28SLABBE Corentin 		dev_err(jrdev, "Invalid number of src SG.\n");
1262f9970c28SLABBE Corentin 		return src_nents;
1263f9970c28SLABBE Corentin 	}
1264bc13c69eSRussell King 
1265bc13c69eSRussell King 	if (src_nents) {
1266bc13c69eSRussell King 		mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1267bc13c69eSRussell King 					  DMA_TO_DEVICE);
1268bc13c69eSRussell King 		if (!mapped_nents) {
1269bc13c69eSRussell King 			dev_err(jrdev, "unable to DMA map source\n");
1270bc13c69eSRussell King 			return -ENOMEM;
1271bc13c69eSRussell King 		}
1272bc13c69eSRussell King 	} else {
1273bc13c69eSRussell King 		mapped_nents = 0;
1274bc13c69eSRussell King 	}
1275bc13c69eSRussell King 
1276045e3678SYuan Kang 	sec4_sg_src_index = 2;
1277bc13c69eSRussell King 	sec4_sg_bytes = (sec4_sg_src_index + mapped_nents) *
1278045e3678SYuan Kang 			 sizeof(struct sec4_sg_entry);
1279045e3678SYuan Kang 
1280045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
1281*2ba1e798SIuliana Prodan 	edesc = ahash_edesc_alloc(req, sec4_sg_src_index + mapped_nents,
1282*2ba1e798SIuliana Prodan 				  ctx->sh_desc_digest, ctx->sh_desc_digest_dma);
1283045e3678SYuan Kang 	if (!edesc) {
1284bc13c69eSRussell King 		dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1285045e3678SYuan Kang 		return -ENOMEM;
1286045e3678SYuan Kang 	}
1287045e3678SYuan Kang 
1288045e3678SYuan Kang 	desc = edesc->hw_desc;
1289045e3678SYuan Kang 
1290045e3678SYuan Kang 	edesc->src_nents = src_nents;
1291045e3678SYuan Kang 	edesc->sec4_sg_bytes = sec4_sg_bytes;
1292045e3678SYuan Kang 
1293944c3d4dSHoria Geantă 	ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg, state);
1294944c3d4dSHoria Geantă 	if (ret)
1295944c3d4dSHoria Geantă 		goto unmap;
1296045e3678SYuan Kang 
129765cf164aSRussell King 	ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 1, buflen,
129865cf164aSRussell King 				  req->nbytes);
129965cf164aSRussell King 	if (ret) {
1300ce572085SHoria Geanta 		dev_err(jrdev, "unable to map S/G table\n");
130106435f34SMarkus Elfring 		goto unmap;
1302ce572085SHoria Geanta 	}
13031da2be33SRuchika Gupta 
1304c19650d6SHoria Geantă 	ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
1305c19650d6SHoria Geantă 	if (ret)
130606435f34SMarkus Elfring 		goto unmap;
1307045e3678SYuan Kang 
13086e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
13096e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
13106e005503SSascha Hauer 			     1);
1311045e3678SYuan Kang 
1312045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, ahash_done, req);
1313045e3678SYuan Kang 	if (!ret) {
1314045e3678SYuan Kang 		ret = -EINPROGRESS;
1315045e3678SYuan Kang 	} else {
1316c19650d6SHoria Geantă 		ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
1317045e3678SYuan Kang 		kfree(edesc);
1318045e3678SYuan Kang 	}
1319045e3678SYuan Kang 
1320045e3678SYuan Kang 	return ret;
132106435f34SMarkus Elfring  unmap:
132206435f34SMarkus Elfring 	ahash_unmap(jrdev, edesc, req, digestsize);
132306435f34SMarkus Elfring 	kfree(edesc);
132406435f34SMarkus Elfring 	return -ENOMEM;
132506435f34SMarkus Elfring 
1326045e3678SYuan Kang }
1327045e3678SYuan Kang 
1328045e3678SYuan Kang /* submit first update job descriptor after init */
1329045e3678SYuan Kang static int ahash_update_first(struct ahash_request *req)
1330045e3678SYuan Kang {
1331045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1332045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1333045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1334045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
133546b49abcSAndrei Botila 	u8 *buf = state->buf;
133646b49abcSAndrei Botila 	int *buflen = &state->buflen;
133746b49abcSAndrei Botila 	int *next_buflen = &state->next_buflen;
1338045e3678SYuan Kang 	int to_hash;
133912b8567fSIuliana Prodan 	int blocksize = crypto_ahash_blocksize(ahash);
134030a43b44SRussell King 	u32 *desc;
134165cf164aSRussell King 	int src_nents, mapped_nents;
1342045e3678SYuan Kang 	struct ahash_edesc *edesc;
1343045e3678SYuan Kang 	int ret = 0;
1344045e3678SYuan Kang 
134512b8567fSIuliana Prodan 	*next_buflen = req->nbytes & (blocksize - 1);
1346045e3678SYuan Kang 	to_hash = req->nbytes - *next_buflen;
1347045e3678SYuan Kang 
134812b8567fSIuliana Prodan 	/*
134987870cfbSIuliana Prodan 	 * For XCBC and CMAC, if to_hash is multiple of block size,
135012b8567fSIuliana Prodan 	 * keep last block in internal buffer
135112b8567fSIuliana Prodan 	 */
135287870cfbSIuliana Prodan 	if ((is_xcbc_aes(ctx->adata.algtype) ||
135387870cfbSIuliana Prodan 	     is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize &&
135412b8567fSIuliana Prodan 	     (*next_buflen == 0)) {
135512b8567fSIuliana Prodan 		*next_buflen = blocksize;
135612b8567fSIuliana Prodan 		to_hash -= blocksize;
135712b8567fSIuliana Prodan 	}
135812b8567fSIuliana Prodan 
1359045e3678SYuan Kang 	if (to_hash) {
13603d5a2db6SRussell King 		src_nents = sg_nents_for_len(req->src,
13613d5a2db6SRussell King 					     req->nbytes - *next_buflen);
1362f9970c28SLABBE Corentin 		if (src_nents < 0) {
1363f9970c28SLABBE Corentin 			dev_err(jrdev, "Invalid number of src SG.\n");
1364f9970c28SLABBE Corentin 			return src_nents;
1365f9970c28SLABBE Corentin 		}
1366bc13c69eSRussell King 
1367bc13c69eSRussell King 		if (src_nents) {
1368bc13c69eSRussell King 			mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1369bc13c69eSRussell King 						  DMA_TO_DEVICE);
1370bc13c69eSRussell King 			if (!mapped_nents) {
1371bc13c69eSRussell King 				dev_err(jrdev, "unable to map source for DMA\n");
1372bc13c69eSRussell King 				return -ENOMEM;
1373bc13c69eSRussell King 			}
1374bc13c69eSRussell King 		} else {
1375bc13c69eSRussell King 			mapped_nents = 0;
1376bc13c69eSRussell King 		}
1377045e3678SYuan Kang 
1378045e3678SYuan Kang 		/*
1379045e3678SYuan Kang 		 * allocate space for base edesc and hw desc commands,
1380045e3678SYuan Kang 		 * link tables
1381045e3678SYuan Kang 		 */
1382*2ba1e798SIuliana Prodan 		edesc = ahash_edesc_alloc(req, mapped_nents > 1 ?
138330a43b44SRussell King 					  mapped_nents : 0,
138430a43b44SRussell King 					  ctx->sh_desc_update_first,
1385*2ba1e798SIuliana Prodan 					  ctx->sh_desc_update_first_dma);
1386045e3678SYuan Kang 		if (!edesc) {
1387bc13c69eSRussell King 			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1388045e3678SYuan Kang 			return -ENOMEM;
1389045e3678SYuan Kang 		}
1390045e3678SYuan Kang 
1391045e3678SYuan Kang 		edesc->src_nents = src_nents;
1392045e3678SYuan Kang 
139365cf164aSRussell King 		ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 0, 0,
139465cf164aSRussell King 					  to_hash);
139565cf164aSRussell King 		if (ret)
139658b0e5d0SMarkus Elfring 			goto unmap_ctx;
1397045e3678SYuan Kang 
1398045e3678SYuan Kang 		desc = edesc->hw_desc;
1399045e3678SYuan Kang 
1400ce572085SHoria Geanta 		ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len);
1401ce572085SHoria Geanta 		if (ret)
140258b0e5d0SMarkus Elfring 			goto unmap_ctx;
1403045e3678SYuan Kang 
14046e005503SSascha Hauer 		print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
1405045e3678SYuan Kang 				     DUMP_PREFIX_ADDRESS, 16, 4, desc,
1406045e3678SYuan Kang 				     desc_bytes(desc), 1);
1407045e3678SYuan Kang 
140832686d34SRussell King 		ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_dst, req);
140932686d34SRussell King 		if (ret)
141058b0e5d0SMarkus Elfring 			goto unmap_ctx;
141132686d34SRussell King 
1412045e3678SYuan Kang 		ret = -EINPROGRESS;
1413045e3678SYuan Kang 		state->update = ahash_update_ctx;
1414045e3678SYuan Kang 		state->finup = ahash_finup_ctx;
1415045e3678SYuan Kang 		state->final = ahash_final_ctx;
1416045e3678SYuan Kang 	} else if (*next_buflen) {
1417045e3678SYuan Kang 		state->update = ahash_update_no_ctx;
1418045e3678SYuan Kang 		state->finup = ahash_finup_no_ctx;
1419045e3678SYuan Kang 		state->final = ahash_final_no_ctx;
142046b49abcSAndrei Botila 		scatterwalk_map_and_copy(buf, req->src, 0,
1421307fd543SCristian Stoica 					 req->nbytes, 0);
142246b49abcSAndrei Botila 		*buflen = *next_buflen;
14236e005503SSascha Hauer 
142446b49abcSAndrei Botila 		print_hex_dump_debug("buf@" __stringify(__LINE__)": ",
142546b49abcSAndrei Botila 				     DUMP_PREFIX_ADDRESS, 16, 4, buf,
142646b49abcSAndrei Botila 				     *buflen, 1);
142746b49abcSAndrei Botila 	}
1428045e3678SYuan Kang 
1429045e3678SYuan Kang 	return ret;
143058b0e5d0SMarkus Elfring  unmap_ctx:
143132686d34SRussell King 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE);
143232686d34SRussell King 	kfree(edesc);
143332686d34SRussell King 	return ret;
1434045e3678SYuan Kang }
1435045e3678SYuan Kang 
1436045e3678SYuan Kang static int ahash_finup_first(struct ahash_request *req)
1437045e3678SYuan Kang {
1438045e3678SYuan Kang 	return ahash_digest(req);
1439045e3678SYuan Kang }
1440045e3678SYuan Kang 
1441045e3678SYuan Kang static int ahash_init(struct ahash_request *req)
1442045e3678SYuan Kang {
1443045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1444045e3678SYuan Kang 
1445045e3678SYuan Kang 	state->update = ahash_update_first;
1446045e3678SYuan Kang 	state->finup = ahash_finup_first;
1447045e3678SYuan Kang 	state->final = ahash_final_no_ctx;
1448045e3678SYuan Kang 
144987ec02e7SHoria Geantă 	state->ctx_dma = 0;
145065055e21SFranck LENORMAND 	state->ctx_dma_len = 0;
1451de0e35ecSHoria Geanta 	state->buf_dma = 0;
145246b49abcSAndrei Botila 	state->buflen = 0;
145346b49abcSAndrei Botila 	state->next_buflen = 0;
1454045e3678SYuan Kang 
1455045e3678SYuan Kang 	return 0;
1456045e3678SYuan Kang }
1457045e3678SYuan Kang 
1458045e3678SYuan Kang static int ahash_update(struct ahash_request *req)
1459045e3678SYuan Kang {
1460045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1461045e3678SYuan Kang 
1462045e3678SYuan Kang 	return state->update(req);
1463045e3678SYuan Kang }
1464045e3678SYuan Kang 
1465045e3678SYuan Kang static int ahash_finup(struct ahash_request *req)
1466045e3678SYuan Kang {
1467045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1468045e3678SYuan Kang 
1469045e3678SYuan Kang 	return state->finup(req);
1470045e3678SYuan Kang }
1471045e3678SYuan Kang 
1472045e3678SYuan Kang static int ahash_final(struct ahash_request *req)
1473045e3678SYuan Kang {
1474045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1475045e3678SYuan Kang 
1476045e3678SYuan Kang 	return state->final(req);
1477045e3678SYuan Kang }
1478045e3678SYuan Kang 
1479045e3678SYuan Kang static int ahash_export(struct ahash_request *req, void *out)
1480045e3678SYuan Kang {
1481045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
14825ec90831SRussell King 	struct caam_export_state *export = out;
148346b49abcSAndrei Botila 	u8 *buf = state->buf;
148446b49abcSAndrei Botila 	int len = state->buflen;
14855ec90831SRussell King 
14865ec90831SRussell King 	memcpy(export->buf, buf, len);
14875ec90831SRussell King 	memcpy(export->caam_ctx, state->caam_ctx, sizeof(export->caam_ctx));
14885ec90831SRussell King 	export->buflen = len;
14895ec90831SRussell King 	export->update = state->update;
14905ec90831SRussell King 	export->final = state->final;
14915ec90831SRussell King 	export->finup = state->finup;
1492434b4212SRussell King 
1493045e3678SYuan Kang 	return 0;
1494045e3678SYuan Kang }
1495045e3678SYuan Kang 
1496045e3678SYuan Kang static int ahash_import(struct ahash_request *req, const void *in)
1497045e3678SYuan Kang {
1498045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
14995ec90831SRussell King 	const struct caam_export_state *export = in;
1500045e3678SYuan Kang 
15015ec90831SRussell King 	memset(state, 0, sizeof(*state));
150246b49abcSAndrei Botila 	memcpy(state->buf, export->buf, export->buflen);
15035ec90831SRussell King 	memcpy(state->caam_ctx, export->caam_ctx, sizeof(state->caam_ctx));
150446b49abcSAndrei Botila 	state->buflen = export->buflen;
15055ec90831SRussell King 	state->update = export->update;
15065ec90831SRussell King 	state->final = export->final;
15075ec90831SRussell King 	state->finup = export->finup;
1508434b4212SRussell King 
1509045e3678SYuan Kang 	return 0;
1510045e3678SYuan Kang }
1511045e3678SYuan Kang 
1512045e3678SYuan Kang struct caam_hash_template {
1513045e3678SYuan Kang 	char name[CRYPTO_MAX_ALG_NAME];
1514045e3678SYuan Kang 	char driver_name[CRYPTO_MAX_ALG_NAME];
1515b0e09baeSYuan Kang 	char hmac_name[CRYPTO_MAX_ALG_NAME];
1516b0e09baeSYuan Kang 	char hmac_driver_name[CRYPTO_MAX_ALG_NAME];
1517045e3678SYuan Kang 	unsigned int blocksize;
1518045e3678SYuan Kang 	struct ahash_alg template_ahash;
1519045e3678SYuan Kang 	u32 alg_type;
1520045e3678SYuan Kang };
1521045e3678SYuan Kang 
1522045e3678SYuan Kang /* ahash descriptors */
1523045e3678SYuan Kang static struct caam_hash_template driver_hash[] = {
1524045e3678SYuan Kang 	{
1525b0e09baeSYuan Kang 		.name = "sha1",
1526b0e09baeSYuan Kang 		.driver_name = "sha1-caam",
1527b0e09baeSYuan Kang 		.hmac_name = "hmac(sha1)",
1528b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha1-caam",
1529045e3678SYuan Kang 		.blocksize = SHA1_BLOCK_SIZE,
1530045e3678SYuan Kang 		.template_ahash = {
1531045e3678SYuan Kang 			.init = ahash_init,
1532045e3678SYuan Kang 			.update = ahash_update,
1533045e3678SYuan Kang 			.final = ahash_final,
1534045e3678SYuan Kang 			.finup = ahash_finup,
1535045e3678SYuan Kang 			.digest = ahash_digest,
1536045e3678SYuan Kang 			.export = ahash_export,
1537045e3678SYuan Kang 			.import = ahash_import,
1538045e3678SYuan Kang 			.setkey = ahash_setkey,
1539045e3678SYuan Kang 			.halg = {
1540045e3678SYuan Kang 				.digestsize = SHA1_DIGEST_SIZE,
15415ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1542045e3678SYuan Kang 			},
1543045e3678SYuan Kang 		},
1544045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA1,
1545045e3678SYuan Kang 	}, {
1546b0e09baeSYuan Kang 		.name = "sha224",
1547b0e09baeSYuan Kang 		.driver_name = "sha224-caam",
1548b0e09baeSYuan Kang 		.hmac_name = "hmac(sha224)",
1549b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha224-caam",
1550045e3678SYuan Kang 		.blocksize = SHA224_BLOCK_SIZE,
1551045e3678SYuan Kang 		.template_ahash = {
1552045e3678SYuan Kang 			.init = ahash_init,
1553045e3678SYuan Kang 			.update = ahash_update,
1554045e3678SYuan Kang 			.final = ahash_final,
1555045e3678SYuan Kang 			.finup = ahash_finup,
1556045e3678SYuan Kang 			.digest = ahash_digest,
1557045e3678SYuan Kang 			.export = ahash_export,
1558045e3678SYuan Kang 			.import = ahash_import,
1559045e3678SYuan Kang 			.setkey = ahash_setkey,
1560045e3678SYuan Kang 			.halg = {
1561045e3678SYuan Kang 				.digestsize = SHA224_DIGEST_SIZE,
15625ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1563045e3678SYuan Kang 			},
1564045e3678SYuan Kang 		},
1565045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA224,
1566045e3678SYuan Kang 	}, {
1567b0e09baeSYuan Kang 		.name = "sha256",
1568b0e09baeSYuan Kang 		.driver_name = "sha256-caam",
1569b0e09baeSYuan Kang 		.hmac_name = "hmac(sha256)",
1570b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha256-caam",
1571045e3678SYuan Kang 		.blocksize = SHA256_BLOCK_SIZE,
1572045e3678SYuan Kang 		.template_ahash = {
1573045e3678SYuan Kang 			.init = ahash_init,
1574045e3678SYuan Kang 			.update = ahash_update,
1575045e3678SYuan Kang 			.final = ahash_final,
1576045e3678SYuan Kang 			.finup = ahash_finup,
1577045e3678SYuan Kang 			.digest = ahash_digest,
1578045e3678SYuan Kang 			.export = ahash_export,
1579045e3678SYuan Kang 			.import = ahash_import,
1580045e3678SYuan Kang 			.setkey = ahash_setkey,
1581045e3678SYuan Kang 			.halg = {
1582045e3678SYuan Kang 				.digestsize = SHA256_DIGEST_SIZE,
15835ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1584045e3678SYuan Kang 			},
1585045e3678SYuan Kang 		},
1586045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA256,
1587045e3678SYuan Kang 	}, {
1588b0e09baeSYuan Kang 		.name = "sha384",
1589b0e09baeSYuan Kang 		.driver_name = "sha384-caam",
1590b0e09baeSYuan Kang 		.hmac_name = "hmac(sha384)",
1591b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha384-caam",
1592045e3678SYuan Kang 		.blocksize = SHA384_BLOCK_SIZE,
1593045e3678SYuan Kang 		.template_ahash = {
1594045e3678SYuan Kang 			.init = ahash_init,
1595045e3678SYuan Kang 			.update = ahash_update,
1596045e3678SYuan Kang 			.final = ahash_final,
1597045e3678SYuan Kang 			.finup = ahash_finup,
1598045e3678SYuan Kang 			.digest = ahash_digest,
1599045e3678SYuan Kang 			.export = ahash_export,
1600045e3678SYuan Kang 			.import = ahash_import,
1601045e3678SYuan Kang 			.setkey = ahash_setkey,
1602045e3678SYuan Kang 			.halg = {
1603045e3678SYuan Kang 				.digestsize = SHA384_DIGEST_SIZE,
16045ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1605045e3678SYuan Kang 			},
1606045e3678SYuan Kang 		},
1607045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA384,
1608045e3678SYuan Kang 	}, {
1609b0e09baeSYuan Kang 		.name = "sha512",
1610b0e09baeSYuan Kang 		.driver_name = "sha512-caam",
1611b0e09baeSYuan Kang 		.hmac_name = "hmac(sha512)",
1612b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha512-caam",
1613045e3678SYuan Kang 		.blocksize = SHA512_BLOCK_SIZE,
1614045e3678SYuan Kang 		.template_ahash = {
1615045e3678SYuan Kang 			.init = ahash_init,
1616045e3678SYuan Kang 			.update = ahash_update,
1617045e3678SYuan Kang 			.final = ahash_final,
1618045e3678SYuan Kang 			.finup = ahash_finup,
1619045e3678SYuan Kang 			.digest = ahash_digest,
1620045e3678SYuan Kang 			.export = ahash_export,
1621045e3678SYuan Kang 			.import = ahash_import,
1622045e3678SYuan Kang 			.setkey = ahash_setkey,
1623045e3678SYuan Kang 			.halg = {
1624045e3678SYuan Kang 				.digestsize = SHA512_DIGEST_SIZE,
16255ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1626045e3678SYuan Kang 			},
1627045e3678SYuan Kang 		},
1628045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA512,
1629045e3678SYuan Kang 	}, {
1630b0e09baeSYuan Kang 		.name = "md5",
1631b0e09baeSYuan Kang 		.driver_name = "md5-caam",
1632b0e09baeSYuan Kang 		.hmac_name = "hmac(md5)",
1633b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-md5-caam",
1634045e3678SYuan Kang 		.blocksize = MD5_BLOCK_WORDS * 4,
1635045e3678SYuan Kang 		.template_ahash = {
1636045e3678SYuan Kang 			.init = ahash_init,
1637045e3678SYuan Kang 			.update = ahash_update,
1638045e3678SYuan Kang 			.final = ahash_final,
1639045e3678SYuan Kang 			.finup = ahash_finup,
1640045e3678SYuan Kang 			.digest = ahash_digest,
1641045e3678SYuan Kang 			.export = ahash_export,
1642045e3678SYuan Kang 			.import = ahash_import,
1643045e3678SYuan Kang 			.setkey = ahash_setkey,
1644045e3678SYuan Kang 			.halg = {
1645045e3678SYuan Kang 				.digestsize = MD5_DIGEST_SIZE,
16465ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1647045e3678SYuan Kang 			},
1648045e3678SYuan Kang 		},
1649045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_MD5,
165012b8567fSIuliana Prodan 	}, {
165112b8567fSIuliana Prodan 		.hmac_name = "xcbc(aes)",
165212b8567fSIuliana Prodan 		.hmac_driver_name = "xcbc-aes-caam",
165312b8567fSIuliana Prodan 		.blocksize = AES_BLOCK_SIZE,
165412b8567fSIuliana Prodan 		.template_ahash = {
165512b8567fSIuliana Prodan 			.init = ahash_init,
165612b8567fSIuliana Prodan 			.update = ahash_update,
165712b8567fSIuliana Prodan 			.final = ahash_final,
165812b8567fSIuliana Prodan 			.finup = ahash_finup,
165912b8567fSIuliana Prodan 			.digest = ahash_digest,
166012b8567fSIuliana Prodan 			.export = ahash_export,
166112b8567fSIuliana Prodan 			.import = ahash_import,
166212b8567fSIuliana Prodan 			.setkey = axcbc_setkey,
166312b8567fSIuliana Prodan 			.halg = {
166412b8567fSIuliana Prodan 				.digestsize = AES_BLOCK_SIZE,
166512b8567fSIuliana Prodan 				.statesize = sizeof(struct caam_export_state),
166612b8567fSIuliana Prodan 			},
166712b8567fSIuliana Prodan 		 },
166812b8567fSIuliana Prodan 		.alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_XCBC_MAC,
166987870cfbSIuliana Prodan 	}, {
167087870cfbSIuliana Prodan 		.hmac_name = "cmac(aes)",
167187870cfbSIuliana Prodan 		.hmac_driver_name = "cmac-aes-caam",
167287870cfbSIuliana Prodan 		.blocksize = AES_BLOCK_SIZE,
167387870cfbSIuliana Prodan 		.template_ahash = {
167487870cfbSIuliana Prodan 			.init = ahash_init,
167587870cfbSIuliana Prodan 			.update = ahash_update,
167687870cfbSIuliana Prodan 			.final = ahash_final,
167787870cfbSIuliana Prodan 			.finup = ahash_finup,
167887870cfbSIuliana Prodan 			.digest = ahash_digest,
167987870cfbSIuliana Prodan 			.export = ahash_export,
168087870cfbSIuliana Prodan 			.import = ahash_import,
168187870cfbSIuliana Prodan 			.setkey = acmac_setkey,
168287870cfbSIuliana Prodan 			.halg = {
168387870cfbSIuliana Prodan 				.digestsize = AES_BLOCK_SIZE,
168487870cfbSIuliana Prodan 				.statesize = sizeof(struct caam_export_state),
168587870cfbSIuliana Prodan 			},
168687870cfbSIuliana Prodan 		 },
168787870cfbSIuliana Prodan 		.alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CMAC,
1688045e3678SYuan Kang 	},
1689045e3678SYuan Kang };
1690045e3678SYuan Kang 
1691045e3678SYuan Kang struct caam_hash_alg {
1692045e3678SYuan Kang 	struct list_head entry;
1693045e3678SYuan Kang 	int alg_type;
1694045e3678SYuan Kang 	struct ahash_alg ahash_alg;
1695045e3678SYuan Kang };
1696045e3678SYuan Kang 
1697045e3678SYuan Kang static int caam_hash_cra_init(struct crypto_tfm *tfm)
1698045e3678SYuan Kang {
1699045e3678SYuan Kang 	struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
1700045e3678SYuan Kang 	struct crypto_alg *base = tfm->__crt_alg;
1701045e3678SYuan Kang 	struct hash_alg_common *halg =
1702045e3678SYuan Kang 		 container_of(base, struct hash_alg_common, base);
1703045e3678SYuan Kang 	struct ahash_alg *alg =
1704045e3678SYuan Kang 		 container_of(halg, struct ahash_alg, halg);
1705045e3678SYuan Kang 	struct caam_hash_alg *caam_hash =
1706045e3678SYuan Kang 		 container_of(alg, struct caam_hash_alg, ahash_alg);
1707045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm);
1708045e3678SYuan Kang 	/* Sizes for MDHA running digests: MD5, SHA1, 224, 256, 384, 512 */
1709045e3678SYuan Kang 	static const u8 runninglen[] = { HASH_MSG_LEN + MD5_DIGEST_SIZE,
1710045e3678SYuan Kang 					 HASH_MSG_LEN + SHA1_DIGEST_SIZE,
1711045e3678SYuan Kang 					 HASH_MSG_LEN + 32,
1712045e3678SYuan Kang 					 HASH_MSG_LEN + SHA256_DIGEST_SIZE,
1713045e3678SYuan Kang 					 HASH_MSG_LEN + 64,
1714045e3678SYuan Kang 					 HASH_MSG_LEN + SHA512_DIGEST_SIZE };
1715bbf22344SHoria Geantă 	dma_addr_t dma_addr;
17167e0880b9SHoria Geantă 	struct caam_drv_private *priv;
1717045e3678SYuan Kang 
1718045e3678SYuan Kang 	/*
1719cfc6f11bSRuchika Gupta 	 * Get a Job ring from Job Ring driver to ensure in-order
1720045e3678SYuan Kang 	 * crypto request processing per tfm
1721045e3678SYuan Kang 	 */
1722cfc6f11bSRuchika Gupta 	ctx->jrdev = caam_jr_alloc();
1723cfc6f11bSRuchika Gupta 	if (IS_ERR(ctx->jrdev)) {
1724cfc6f11bSRuchika Gupta 		pr_err("Job Ring Device allocation for transform failed\n");
1725cfc6f11bSRuchika Gupta 		return PTR_ERR(ctx->jrdev);
1726cfc6f11bSRuchika Gupta 	}
1727bbf22344SHoria Geantă 
17287e0880b9SHoria Geantă 	priv = dev_get_drvdata(ctx->jrdev->parent);
172912b8567fSIuliana Prodan 
173012b8567fSIuliana Prodan 	if (is_xcbc_aes(caam_hash->alg_type)) {
173112b8567fSIuliana Prodan 		ctx->dir = DMA_TO_DEVICE;
1732e9b4913aSHoria Geantă 		ctx->key_dir = DMA_BIDIRECTIONAL;
173312b8567fSIuliana Prodan 		ctx->adata.algtype = OP_TYPE_CLASS1_ALG | caam_hash->alg_type;
173412b8567fSIuliana Prodan 		ctx->ctx_len = 48;
1735e9b4913aSHoria Geantă 	} else if (is_cmac_aes(caam_hash->alg_type)) {
1736e9b4913aSHoria Geantă 		ctx->dir = DMA_TO_DEVICE;
1737e9b4913aSHoria Geantă 		ctx->key_dir = DMA_NONE;
1738e9b4913aSHoria Geantă 		ctx->adata.algtype = OP_TYPE_CLASS1_ALG | caam_hash->alg_type;
1739e9b4913aSHoria Geantă 		ctx->ctx_len = 32;
1740e9b4913aSHoria Geantă 	} else {
1741e9b4913aSHoria Geantă 		if (priv->era >= 6) {
1742e9b4913aSHoria Geantă 			ctx->dir = DMA_BIDIRECTIONAL;
1743e9b4913aSHoria Geantă 			ctx->key_dir = alg->setkey ? DMA_TO_DEVICE : DMA_NONE;
1744e9b4913aSHoria Geantă 		} else {
1745e9b4913aSHoria Geantă 			ctx->dir = DMA_TO_DEVICE;
1746e9b4913aSHoria Geantă 			ctx->key_dir = DMA_NONE;
1747e9b4913aSHoria Geantă 		}
1748e9b4913aSHoria Geantă 		ctx->adata.algtype = OP_TYPE_CLASS2_ALG | caam_hash->alg_type;
1749e9b4913aSHoria Geantă 		ctx->ctx_len = runninglen[(ctx->adata.algtype &
1750e9b4913aSHoria Geantă 					   OP_ALG_ALGSEL_SUBMASK) >>
1751e9b4913aSHoria Geantă 					  OP_ALG_ALGSEL_SHIFT];
1752e9b4913aSHoria Geantă 	}
175312b8567fSIuliana Prodan 
1754e9b4913aSHoria Geantă 	if (ctx->key_dir != DMA_NONE) {
1755a2fb864cSHoria Geantă 		ctx->adata.key_dma = dma_map_single_attrs(ctx->jrdev, ctx->key,
175612b8567fSIuliana Prodan 							  ARRAY_SIZE(ctx->key),
1757e9b4913aSHoria Geantă 							  ctx->key_dir,
175812b8567fSIuliana Prodan 							  DMA_ATTR_SKIP_CPU_SYNC);
1759a2fb864cSHoria Geantă 		if (dma_mapping_error(ctx->jrdev, ctx->adata.key_dma)) {
176012b8567fSIuliana Prodan 			dev_err(ctx->jrdev, "unable to map key\n");
176112b8567fSIuliana Prodan 			caam_jr_free(ctx->jrdev);
176212b8567fSIuliana Prodan 			return -ENOMEM;
176312b8567fSIuliana Prodan 		}
176412b8567fSIuliana Prodan 	}
17657e0880b9SHoria Geantă 
1766bbf22344SHoria Geantă 	dma_addr = dma_map_single_attrs(ctx->jrdev, ctx->sh_desc_update,
17678e731ee5SHoria Geantă 					offsetof(struct caam_hash_ctx, key),
17687e0880b9SHoria Geantă 					ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
1769bbf22344SHoria Geantă 	if (dma_mapping_error(ctx->jrdev, dma_addr)) {
1770bbf22344SHoria Geantă 		dev_err(ctx->jrdev, "unable to map shared descriptors\n");
177112b8567fSIuliana Prodan 
1772e9b4913aSHoria Geantă 		if (ctx->key_dir != DMA_NONE)
1773a2fb864cSHoria Geantă 			dma_unmap_single_attrs(ctx->jrdev, ctx->adata.key_dma,
177412b8567fSIuliana Prodan 					       ARRAY_SIZE(ctx->key),
1775e9b4913aSHoria Geantă 					       ctx->key_dir,
177612b8567fSIuliana Prodan 					       DMA_ATTR_SKIP_CPU_SYNC);
177712b8567fSIuliana Prodan 
1778bbf22344SHoria Geantă 		caam_jr_free(ctx->jrdev);
1779bbf22344SHoria Geantă 		return -ENOMEM;
1780bbf22344SHoria Geantă 	}
1781bbf22344SHoria Geantă 
1782bbf22344SHoria Geantă 	ctx->sh_desc_update_dma = dma_addr;
1783bbf22344SHoria Geantă 	ctx->sh_desc_update_first_dma = dma_addr +
1784bbf22344SHoria Geantă 					offsetof(struct caam_hash_ctx,
1785bbf22344SHoria Geantă 						 sh_desc_update_first);
1786bbf22344SHoria Geantă 	ctx->sh_desc_fin_dma = dma_addr + offsetof(struct caam_hash_ctx,
1787bbf22344SHoria Geantă 						   sh_desc_fin);
1788bbf22344SHoria Geantă 	ctx->sh_desc_digest_dma = dma_addr + offsetof(struct caam_hash_ctx,
1789bbf22344SHoria Geantă 						      sh_desc_digest);
1790bbf22344SHoria Geantă 
1791045e3678SYuan Kang 	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
1792045e3678SYuan Kang 				 sizeof(struct caam_hash_state));
17939a2537d0SIuliana Prodan 
17949a2537d0SIuliana Prodan 	/*
17959a2537d0SIuliana Prodan 	 * For keyed hash algorithms shared descriptors
17969a2537d0SIuliana Prodan 	 * will be created later in setkey() callback
17979a2537d0SIuliana Prodan 	 */
17989a2537d0SIuliana Prodan 	return alg->setkey ? 0 : ahash_set_sh_desc(ahash);
1799045e3678SYuan Kang }
1800045e3678SYuan Kang 
1801045e3678SYuan Kang static void caam_hash_cra_exit(struct crypto_tfm *tfm)
1802045e3678SYuan Kang {
1803045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm);
1804045e3678SYuan Kang 
1805bbf22344SHoria Geantă 	dma_unmap_single_attrs(ctx->jrdev, ctx->sh_desc_update_dma,
180612b8567fSIuliana Prodan 			       offsetof(struct caam_hash_ctx, key),
18077e0880b9SHoria Geantă 			       ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
1808e9b4913aSHoria Geantă 	if (ctx->key_dir != DMA_NONE)
1809a2fb864cSHoria Geantă 		dma_unmap_single_attrs(ctx->jrdev, ctx->adata.key_dma,
1810e9b4913aSHoria Geantă 				       ARRAY_SIZE(ctx->key), ctx->key_dir,
181112b8567fSIuliana Prodan 				       DMA_ATTR_SKIP_CPU_SYNC);
1812cfc6f11bSRuchika Gupta 	caam_jr_free(ctx->jrdev);
1813045e3678SYuan Kang }
1814045e3678SYuan Kang 
18151b46c90cSHoria Geantă void caam_algapi_hash_exit(void)
1816045e3678SYuan Kang {
1817045e3678SYuan Kang 	struct caam_hash_alg *t_alg, *n;
1818045e3678SYuan Kang 
1819cfc6f11bSRuchika Gupta 	if (!hash_list.next)
1820045e3678SYuan Kang 		return;
1821045e3678SYuan Kang 
1822cfc6f11bSRuchika Gupta 	list_for_each_entry_safe(t_alg, n, &hash_list, entry) {
1823045e3678SYuan Kang 		crypto_unregister_ahash(&t_alg->ahash_alg);
1824045e3678SYuan Kang 		list_del(&t_alg->entry);
1825045e3678SYuan Kang 		kfree(t_alg);
1826045e3678SYuan Kang 	}
1827045e3678SYuan Kang }
1828045e3678SYuan Kang 
1829045e3678SYuan Kang static struct caam_hash_alg *
1830cfc6f11bSRuchika Gupta caam_hash_alloc(struct caam_hash_template *template,
1831b0e09baeSYuan Kang 		bool keyed)
1832045e3678SYuan Kang {
1833045e3678SYuan Kang 	struct caam_hash_alg *t_alg;
1834045e3678SYuan Kang 	struct ahash_alg *halg;
1835045e3678SYuan Kang 	struct crypto_alg *alg;
1836045e3678SYuan Kang 
18379c4f9733SFabio Estevam 	t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL);
1838045e3678SYuan Kang 	if (!t_alg) {
1839cfc6f11bSRuchika Gupta 		pr_err("failed to allocate t_alg\n");
1840045e3678SYuan Kang 		return ERR_PTR(-ENOMEM);
1841045e3678SYuan Kang 	}
1842045e3678SYuan Kang 
1843045e3678SYuan Kang 	t_alg->ahash_alg = template->template_ahash;
1844045e3678SYuan Kang 	halg = &t_alg->ahash_alg;
1845045e3678SYuan Kang 	alg = &halg->halg.base;
1846045e3678SYuan Kang 
1847b0e09baeSYuan Kang 	if (keyed) {
1848b0e09baeSYuan Kang 		snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
1849b0e09baeSYuan Kang 			 template->hmac_name);
1850b0e09baeSYuan Kang 		snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
1851b0e09baeSYuan Kang 			 template->hmac_driver_name);
1852b0e09baeSYuan Kang 	} else {
1853b0e09baeSYuan Kang 		snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
1854b0e09baeSYuan Kang 			 template->name);
1855045e3678SYuan Kang 		snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
1856045e3678SYuan Kang 			 template->driver_name);
1857a0118c8bSRussell King 		t_alg->ahash_alg.setkey = NULL;
1858b0e09baeSYuan Kang 	}
1859045e3678SYuan Kang 	alg->cra_module = THIS_MODULE;
1860045e3678SYuan Kang 	alg->cra_init = caam_hash_cra_init;
1861045e3678SYuan Kang 	alg->cra_exit = caam_hash_cra_exit;
1862045e3678SYuan Kang 	alg->cra_ctxsize = sizeof(struct caam_hash_ctx);
1863045e3678SYuan Kang 	alg->cra_priority = CAAM_CRA_PRIORITY;
1864045e3678SYuan Kang 	alg->cra_blocksize = template->blocksize;
1865045e3678SYuan Kang 	alg->cra_alignmask = 0;
18666a38f622SEric Biggers 	alg->cra_flags = CRYPTO_ALG_ASYNC;
1867045e3678SYuan Kang 
1868045e3678SYuan Kang 	t_alg->alg_type = template->alg_type;
1869045e3678SYuan Kang 
1870045e3678SYuan Kang 	return t_alg;
1871045e3678SYuan Kang }
1872045e3678SYuan Kang 
18731b46c90cSHoria Geantă int caam_algapi_hash_init(struct device *ctrldev)
1874045e3678SYuan Kang {
1875045e3678SYuan Kang 	int i = 0, err = 0;
18761b46c90cSHoria Geantă 	struct caam_drv_private *priv = dev_get_drvdata(ctrldev);
1877bf83490eSVictoria Milhoan 	unsigned int md_limit = SHA512_DIGEST_SIZE;
1878d239b10dSHoria Geantă 	u32 md_inst, md_vid;
1879045e3678SYuan Kang 
1880bf83490eSVictoria Milhoan 	/*
1881bf83490eSVictoria Milhoan 	 * Register crypto algorithms the device supports.  First, identify
1882bf83490eSVictoria Milhoan 	 * presence and attributes of MD block.
1883bf83490eSVictoria Milhoan 	 */
1884d239b10dSHoria Geantă 	if (priv->era < 10) {
1885d239b10dSHoria Geantă 		md_vid = (rd_reg32(&priv->ctrl->perfmon.cha_id_ls) &
1886d239b10dSHoria Geantă 			  CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;
1887d239b10dSHoria Geantă 		md_inst = (rd_reg32(&priv->ctrl->perfmon.cha_num_ls) &
1888d239b10dSHoria Geantă 			   CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;
1889d239b10dSHoria Geantă 	} else {
1890d239b10dSHoria Geantă 		u32 mdha = rd_reg32(&priv->ctrl->vreg.mdha);
1891d239b10dSHoria Geantă 
1892d239b10dSHoria Geantă 		md_vid = (mdha & CHA_VER_VID_MASK) >> CHA_VER_VID_SHIFT;
1893d239b10dSHoria Geantă 		md_inst = mdha & CHA_VER_NUM_MASK;
1894d239b10dSHoria Geantă 	}
1895bf83490eSVictoria Milhoan 
1896bf83490eSVictoria Milhoan 	/*
1897bf83490eSVictoria Milhoan 	 * Skip registration of any hashing algorithms if MD block
1898bf83490eSVictoria Milhoan 	 * is not present.
1899bf83490eSVictoria Milhoan 	 */
19001b46c90cSHoria Geantă 	if (!md_inst)
19010435d47eSIuliana Prodan 		return 0;
1902bf83490eSVictoria Milhoan 
1903bf83490eSVictoria Milhoan 	/* Limit digest size based on LP256 */
1904d239b10dSHoria Geantă 	if (md_vid == CHA_VER_VID_MD_LP256)
1905bf83490eSVictoria Milhoan 		md_limit = SHA256_DIGEST_SIZE;
1906bf83490eSVictoria Milhoan 
1907cfc6f11bSRuchika Gupta 	INIT_LIST_HEAD(&hash_list);
1908045e3678SYuan Kang 
1909045e3678SYuan Kang 	/* register crypto algorithms the device supports */
1910045e3678SYuan Kang 	for (i = 0; i < ARRAY_SIZE(driver_hash); i++) {
1911045e3678SYuan Kang 		struct caam_hash_alg *t_alg;
1912bf83490eSVictoria Milhoan 		struct caam_hash_template *alg = driver_hash + i;
1913bf83490eSVictoria Milhoan 
1914bf83490eSVictoria Milhoan 		/* If MD size is not supported by device, skip registration */
191512b8567fSIuliana Prodan 		if (is_mdha(alg->alg_type) &&
191612b8567fSIuliana Prodan 		    alg->template_ahash.halg.digestsize > md_limit)
1917bf83490eSVictoria Milhoan 			continue;
1918045e3678SYuan Kang 
1919b0e09baeSYuan Kang 		/* register hmac version */
1920bf83490eSVictoria Milhoan 		t_alg = caam_hash_alloc(alg, true);
1921b0e09baeSYuan Kang 		if (IS_ERR(t_alg)) {
1922b0e09baeSYuan Kang 			err = PTR_ERR(t_alg);
19230f103b37SIuliana Prodan 			pr_warn("%s alg allocation failed\n",
19240f103b37SIuliana Prodan 				alg->hmac_driver_name);
1925b0e09baeSYuan Kang 			continue;
1926b0e09baeSYuan Kang 		}
1927b0e09baeSYuan Kang 
1928b0e09baeSYuan Kang 		err = crypto_register_ahash(&t_alg->ahash_alg);
1929b0e09baeSYuan Kang 		if (err) {
19306ea30f0aSRussell King 			pr_warn("%s alg registration failed: %d\n",
19316ea30f0aSRussell King 				t_alg->ahash_alg.halg.base.cra_driver_name,
19326ea30f0aSRussell King 				err);
1933b0e09baeSYuan Kang 			kfree(t_alg);
1934b0e09baeSYuan Kang 		} else
1935cfc6f11bSRuchika Gupta 			list_add_tail(&t_alg->entry, &hash_list);
1936b0e09baeSYuan Kang 
193712b8567fSIuliana Prodan 		if ((alg->alg_type & OP_ALG_ALGSEL_MASK) == OP_ALG_ALGSEL_AES)
193812b8567fSIuliana Prodan 			continue;
193912b8567fSIuliana Prodan 
1940b0e09baeSYuan Kang 		/* register unkeyed version */
1941bf83490eSVictoria Milhoan 		t_alg = caam_hash_alloc(alg, false);
1942045e3678SYuan Kang 		if (IS_ERR(t_alg)) {
1943045e3678SYuan Kang 			err = PTR_ERR(t_alg);
1944bf83490eSVictoria Milhoan 			pr_warn("%s alg allocation failed\n", alg->driver_name);
1945045e3678SYuan Kang 			continue;
1946045e3678SYuan Kang 		}
1947045e3678SYuan Kang 
1948045e3678SYuan Kang 		err = crypto_register_ahash(&t_alg->ahash_alg);
1949045e3678SYuan Kang 		if (err) {
19506ea30f0aSRussell King 			pr_warn("%s alg registration failed: %d\n",
19516ea30f0aSRussell King 				t_alg->ahash_alg.halg.base.cra_driver_name,
19526ea30f0aSRussell King 				err);
1953045e3678SYuan Kang 			kfree(t_alg);
1954045e3678SYuan Kang 		} else
1955cfc6f11bSRuchika Gupta 			list_add_tail(&t_alg->entry, &hash_list);
1956045e3678SYuan Kang 	}
1957045e3678SYuan Kang 
1958045e3678SYuan Kang 	return err;
1959045e3678SYuan Kang }
1960