xref: /linux/drivers/crypto/caam/caamhash.c (revision 4bc713a49d7f97f7328e5a595b8f5d231d9ebfca)
1618b5dc4SHoria Geantă // SPDX-License-Identifier: GPL-2.0+
2045e3678SYuan Kang /*
3045e3678SYuan Kang  * caam - Freescale FSL CAAM support for ahash functions of crypto API
4045e3678SYuan Kang  *
5045e3678SYuan Kang  * Copyright 2011 Freescale Semiconductor, Inc.
687870cfbSIuliana Prodan  * Copyright 2018-2019 NXP
7045e3678SYuan Kang  *
8045e3678SYuan Kang  * Based on caamalg.c crypto API driver.
9045e3678SYuan Kang  *
10045e3678SYuan Kang  * relationship of digest job descriptor or first job descriptor after init to
11045e3678SYuan Kang  * shared descriptors:
12045e3678SYuan Kang  *
13045e3678SYuan Kang  * ---------------                     ---------------
14045e3678SYuan Kang  * | JobDesc #1  |-------------------->|  ShareDesc  |
15045e3678SYuan Kang  * | *(packet 1) |                     |  (hashKey)  |
16045e3678SYuan Kang  * ---------------                     | (operation) |
17045e3678SYuan Kang  *                                     ---------------
18045e3678SYuan Kang  *
19045e3678SYuan Kang  * relationship of subsequent job descriptors to shared descriptors:
20045e3678SYuan Kang  *
21045e3678SYuan Kang  * ---------------                     ---------------
22045e3678SYuan Kang  * | JobDesc #2  |-------------------->|  ShareDesc  |
23045e3678SYuan Kang  * | *(packet 2) |      |------------->|  (hashKey)  |
24045e3678SYuan Kang  * ---------------      |    |-------->| (operation) |
25045e3678SYuan Kang  *       .              |    |         | (load ctx2) |
26045e3678SYuan Kang  *       .              |    |         ---------------
27045e3678SYuan Kang  * ---------------      |    |
28045e3678SYuan Kang  * | JobDesc #3  |------|    |
29045e3678SYuan Kang  * | *(packet 3) |           |
30045e3678SYuan Kang  * ---------------           |
31045e3678SYuan Kang  *       .                   |
32045e3678SYuan Kang  *       .                   |
33045e3678SYuan Kang  * ---------------           |
34045e3678SYuan Kang  * | JobDesc #4  |------------
35045e3678SYuan Kang  * | *(packet 4) |
36045e3678SYuan Kang  * ---------------
37045e3678SYuan Kang  *
38045e3678SYuan Kang  * The SharedDesc never changes for a connection unless rekeyed, but
39045e3678SYuan Kang  * each packet will likely be in a different place. So all we need
40045e3678SYuan Kang  * to know to process the packet is where the input is, where the
41045e3678SYuan Kang  * output goes, and what context we want to process with. Context is
42045e3678SYuan Kang  * in the SharedDesc, packet references in the JobDesc.
43045e3678SYuan Kang  *
44045e3678SYuan Kang  * So, a job desc looks like:
45045e3678SYuan Kang  *
46045e3678SYuan Kang  * ---------------------
47045e3678SYuan Kang  * | Header            |
48045e3678SYuan Kang  * | ShareDesc Pointer |
49045e3678SYuan Kang  * | SEQ_OUT_PTR       |
50045e3678SYuan Kang  * | (output buffer)   |
51045e3678SYuan Kang  * | (output length)   |
52045e3678SYuan Kang  * | SEQ_IN_PTR        |
53045e3678SYuan Kang  * | (input buffer)    |
54045e3678SYuan Kang  * | (input length)    |
55045e3678SYuan Kang  * ---------------------
56045e3678SYuan Kang  */
57045e3678SYuan Kang 
58045e3678SYuan Kang #include "compat.h"
59045e3678SYuan Kang 
60045e3678SYuan Kang #include "regs.h"
61045e3678SYuan Kang #include "intern.h"
62045e3678SYuan Kang #include "desc_constr.h"
63045e3678SYuan Kang #include "jr.h"
64045e3678SYuan Kang #include "error.h"
65045e3678SYuan Kang #include "sg_sw_sec4.h"
66045e3678SYuan Kang #include "key_gen.h"
670efa7579SHoria Geantă #include "caamhash_desc.h"
6821b014f0SIuliana Prodan #include <crypto/engine.h>
69199354d7SHerbert Xu #include <linux/dma-mapping.h>
70199354d7SHerbert Xu #include <linux/kernel.h>
71045e3678SYuan Kang 
72045e3678SYuan Kang #define CAAM_CRA_PRIORITY		3000
73045e3678SYuan Kang 
74045e3678SYuan Kang /* max hash key is max split key size */
75045e3678SYuan Kang #define CAAM_MAX_HASH_KEY_SIZE		(SHA512_DIGEST_SIZE * 2)
76045e3678SYuan Kang 
77045e3678SYuan Kang #define CAAM_MAX_HASH_BLOCK_SIZE	SHA512_BLOCK_SIZE
78045e3678SYuan Kang #define CAAM_MAX_HASH_DIGEST_SIZE	SHA512_DIGEST_SIZE
79045e3678SYuan Kang 
80045e3678SYuan Kang #define DESC_HASH_MAX_USED_BYTES	(DESC_AHASH_FINAL_LEN + \
81045e3678SYuan Kang 					 CAAM_MAX_HASH_KEY_SIZE)
82045e3678SYuan Kang #define DESC_HASH_MAX_USED_LEN		(DESC_HASH_MAX_USED_BYTES / CAAM_CMD_SZ)
83045e3678SYuan Kang 
84045e3678SYuan Kang /* caam context sizes for hashes: running digest + 8 */
85045e3678SYuan Kang #define HASH_MSG_LEN			8
86045e3678SYuan Kang #define MAX_CTX_LEN			(HASH_MSG_LEN + SHA512_DIGEST_SIZE)
87045e3678SYuan Kang 
88cfc6f11bSRuchika Gupta static struct list_head hash_list;
89cfc6f11bSRuchika Gupta 
90045e3678SYuan Kang /* ahash per-session context */
91045e3678SYuan Kang struct caam_hash_ctx {
9221b014f0SIuliana Prodan 	struct crypto_engine_ctx enginectx;
93e11793f5SRussell King 	u32 sh_desc_update[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
94e11793f5SRussell King 	u32 sh_desc_update_first[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
95e11793f5SRussell King 	u32 sh_desc_fin[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
96e11793f5SRussell King 	u32 sh_desc_digest[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
9712b8567fSIuliana Prodan 	u8 key[CAAM_MAX_HASH_KEY_SIZE] ____cacheline_aligned;
98e11793f5SRussell King 	dma_addr_t sh_desc_update_dma ____cacheline_aligned;
99045e3678SYuan Kang 	dma_addr_t sh_desc_update_first_dma;
100045e3678SYuan Kang 	dma_addr_t sh_desc_fin_dma;
101045e3678SYuan Kang 	dma_addr_t sh_desc_digest_dma;
1027e0880b9SHoria Geantă 	enum dma_data_direction dir;
103e9b4913aSHoria Geantă 	enum dma_data_direction key_dir;
104e11793f5SRussell King 	struct device *jrdev;
105045e3678SYuan Kang 	int ctx_len;
106db57656bSHoria Geantă 	struct alginfo adata;
107045e3678SYuan Kang };
108045e3678SYuan Kang 
109045e3678SYuan Kang /* ahash state */
110045e3678SYuan Kang struct caam_hash_state {
111045e3678SYuan Kang 	dma_addr_t buf_dma;
112045e3678SYuan Kang 	dma_addr_t ctx_dma;
11365055e21SFranck LENORMAND 	int ctx_dma_len;
11446b49abcSAndrei Botila 	u8 buf[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
11546b49abcSAndrei Botila 	int buflen;
11646b49abcSAndrei Botila 	int next_buflen;
117e7472422SVictoria Milhoan 	u8 caam_ctx[MAX_CTX_LEN] ____cacheline_aligned;
11821b014f0SIuliana Prodan 	int (*update)(struct ahash_request *req) ____cacheline_aligned;
119045e3678SYuan Kang 	int (*final)(struct ahash_request *req);
120045e3678SYuan Kang 	int (*finup)(struct ahash_request *req);
12121b014f0SIuliana Prodan 	struct ahash_edesc *edesc;
12221b014f0SIuliana Prodan 	void (*ahash_op_done)(struct device *jrdev, u32 *desc, u32 err,
12321b014f0SIuliana Prodan 			      void *context);
124045e3678SYuan Kang };
125045e3678SYuan Kang 
1265ec90831SRussell King struct caam_export_state {
1275ec90831SRussell King 	u8 buf[CAAM_MAX_HASH_BLOCK_SIZE];
1285ec90831SRussell King 	u8 caam_ctx[MAX_CTX_LEN];
1295ec90831SRussell King 	int buflen;
1305ec90831SRussell King 	int (*update)(struct ahash_request *req);
1315ec90831SRussell King 	int (*final)(struct ahash_request *req);
1325ec90831SRussell King 	int (*finup)(struct ahash_request *req);
1335ec90831SRussell King };
1345ec90831SRussell King 
13587870cfbSIuliana Prodan static inline bool is_cmac_aes(u32 algtype)
13612b8567fSIuliana Prodan {
13712b8567fSIuliana Prodan 	return (algtype & (OP_ALG_ALGSEL_MASK | OP_ALG_AAI_MASK)) ==
13887870cfbSIuliana Prodan 	       (OP_ALG_ALGSEL_AES | OP_ALG_AAI_CMAC);
13912b8567fSIuliana Prodan }
140045e3678SYuan Kang /* Common job descriptor seq in/out ptr routines */
141045e3678SYuan Kang 
142045e3678SYuan Kang /* Map state->caam_ctx, and append seq_out_ptr command that points to it */
143ce572085SHoria Geanta static inline int map_seq_out_ptr_ctx(u32 *desc, struct device *jrdev,
144045e3678SYuan Kang 				      struct caam_hash_state *state,
145045e3678SYuan Kang 				      int ctx_len)
146045e3678SYuan Kang {
14765055e21SFranck LENORMAND 	state->ctx_dma_len = ctx_len;
148045e3678SYuan Kang 	state->ctx_dma = dma_map_single(jrdev, state->caam_ctx,
149045e3678SYuan Kang 					ctx_len, DMA_FROM_DEVICE);
150ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, state->ctx_dma)) {
151ce572085SHoria Geanta 		dev_err(jrdev, "unable to map ctx\n");
15287ec02e7SHoria Geantă 		state->ctx_dma = 0;
153ce572085SHoria Geanta 		return -ENOMEM;
154ce572085SHoria Geanta 	}
155ce572085SHoria Geanta 
156045e3678SYuan Kang 	append_seq_out_ptr(desc, state->ctx_dma, ctx_len, 0);
157ce572085SHoria Geanta 
158ce572085SHoria Geanta 	return 0;
159045e3678SYuan Kang }
160045e3678SYuan Kang 
161944c3d4dSHoria Geantă /* Map current buffer in state (if length > 0) and put it in link table */
162944c3d4dSHoria Geantă static inline int buf_map_to_sec4_sg(struct device *jrdev,
163045e3678SYuan Kang 				     struct sec4_sg_entry *sec4_sg,
164944c3d4dSHoria Geantă 				     struct caam_hash_state *state)
165045e3678SYuan Kang {
16646b49abcSAndrei Botila 	int buflen = state->buflen;
167045e3678SYuan Kang 
168944c3d4dSHoria Geantă 	if (!buflen)
169944c3d4dSHoria Geantă 		return 0;
170045e3678SYuan Kang 
17146b49abcSAndrei Botila 	state->buf_dma = dma_map_single(jrdev, state->buf, buflen,
172944c3d4dSHoria Geantă 					DMA_TO_DEVICE);
173944c3d4dSHoria Geantă 	if (dma_mapping_error(jrdev, state->buf_dma)) {
174944c3d4dSHoria Geantă 		dev_err(jrdev, "unable to map buf\n");
175944c3d4dSHoria Geantă 		state->buf_dma = 0;
176944c3d4dSHoria Geantă 		return -ENOMEM;
177045e3678SYuan Kang 	}
178045e3678SYuan Kang 
179944c3d4dSHoria Geantă 	dma_to_sec4_sg_one(sec4_sg, state->buf_dma, buflen, 0);
180045e3678SYuan Kang 
181944c3d4dSHoria Geantă 	return 0;
182045e3678SYuan Kang }
183045e3678SYuan Kang 
184045e3678SYuan Kang /* Map state->caam_ctx, and add it to link table */
185dfcd8393SHoria Geantă static inline int ctx_map_to_sec4_sg(struct device *jrdev,
186ce572085SHoria Geanta 				     struct caam_hash_state *state, int ctx_len,
187ce572085SHoria Geanta 				     struct sec4_sg_entry *sec4_sg, u32 flag)
188045e3678SYuan Kang {
18965055e21SFranck LENORMAND 	state->ctx_dma_len = ctx_len;
190045e3678SYuan Kang 	state->ctx_dma = dma_map_single(jrdev, state->caam_ctx, ctx_len, flag);
191ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, state->ctx_dma)) {
192ce572085SHoria Geanta 		dev_err(jrdev, "unable to map ctx\n");
19387ec02e7SHoria Geantă 		state->ctx_dma = 0;
194ce572085SHoria Geanta 		return -ENOMEM;
195ce572085SHoria Geanta 	}
196ce572085SHoria Geanta 
197045e3678SYuan Kang 	dma_to_sec4_sg_one(sec4_sg, state->ctx_dma, ctx_len, 0);
198ce572085SHoria Geanta 
199ce572085SHoria Geanta 	return 0;
200045e3678SYuan Kang }
201045e3678SYuan Kang 
202045e3678SYuan Kang static int ahash_set_sh_desc(struct crypto_ahash *ahash)
203045e3678SYuan Kang {
2044cb4f7c1SHerbert Xu 	struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
205045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
206045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
2077e0880b9SHoria Geantă 	struct caam_drv_private *ctrlpriv = dev_get_drvdata(jrdev->parent);
208045e3678SYuan Kang 	u32 *desc;
209045e3678SYuan Kang 
2107e0880b9SHoria Geantă 	ctx->adata.key_virt = ctx->key;
2117e0880b9SHoria Geantă 
212045e3678SYuan Kang 	/* ahash_update shared descriptor */
213045e3678SYuan Kang 	desc = ctx->sh_desc_update;
2140efa7579SHoria Geantă 	cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_UPDATE, ctx->ctx_len,
2150efa7579SHoria Geantă 			  ctx->ctx_len, true, ctrlpriv->era);
216bbf22344SHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma,
2177e0880b9SHoria Geantă 				   desc_bytes(desc), ctx->dir);
2186e005503SSascha Hauer 
2196e005503SSascha Hauer 	print_hex_dump_debug("ahash update shdesc@"__stringify(__LINE__)": ",
2206e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
2216e005503SSascha Hauer 			     1);
222045e3678SYuan Kang 
223045e3678SYuan Kang 	/* ahash_update_first shared descriptor */
224045e3678SYuan Kang 	desc = ctx->sh_desc_update_first;
2250efa7579SHoria Geantă 	cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len,
2260efa7579SHoria Geantă 			  ctx->ctx_len, false, ctrlpriv->era);
227bbf22344SHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma,
2287e0880b9SHoria Geantă 				   desc_bytes(desc), ctx->dir);
2296e005503SSascha Hauer 	print_hex_dump_debug("ahash update first shdesc@"__stringify(__LINE__)
2306e005503SSascha Hauer 			     ": ", DUMP_PREFIX_ADDRESS, 16, 4, desc,
2316e005503SSascha Hauer 			     desc_bytes(desc), 1);
232045e3678SYuan Kang 
233045e3678SYuan Kang 	/* ahash_final shared descriptor */
234045e3678SYuan Kang 	desc = ctx->sh_desc_fin;
2350efa7579SHoria Geantă 	cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_FINALIZE, digestsize,
2360efa7579SHoria Geantă 			  ctx->ctx_len, true, ctrlpriv->era);
237bbf22344SHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma,
2387e0880b9SHoria Geantă 				   desc_bytes(desc), ctx->dir);
2396e005503SSascha Hauer 
2406e005503SSascha Hauer 	print_hex_dump_debug("ahash final shdesc@"__stringify(__LINE__)": ",
241045e3678SYuan Kang 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
242045e3678SYuan Kang 			     desc_bytes(desc), 1);
243045e3678SYuan Kang 
244045e3678SYuan Kang 	/* ahash_digest shared descriptor */
245045e3678SYuan Kang 	desc = ctx->sh_desc_digest;
2460efa7579SHoria Geantă 	cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_INITFINAL, digestsize,
2470efa7579SHoria Geantă 			  ctx->ctx_len, false, ctrlpriv->era);
248bbf22344SHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma,
2497e0880b9SHoria Geantă 				   desc_bytes(desc), ctx->dir);
2506e005503SSascha Hauer 
2516e005503SSascha Hauer 	print_hex_dump_debug("ahash digest shdesc@"__stringify(__LINE__)": ",
252045e3678SYuan Kang 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
253045e3678SYuan Kang 			     desc_bytes(desc), 1);
254045e3678SYuan Kang 
255045e3678SYuan Kang 	return 0;
256045e3678SYuan Kang }
257045e3678SYuan Kang 
25812b8567fSIuliana Prodan static int axcbc_set_sh_desc(struct crypto_ahash *ahash)
25912b8567fSIuliana Prodan {
2604cb4f7c1SHerbert Xu 	struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
26112b8567fSIuliana Prodan 	int digestsize = crypto_ahash_digestsize(ahash);
26212b8567fSIuliana Prodan 	struct device *jrdev = ctx->jrdev;
26312b8567fSIuliana Prodan 	u32 *desc;
26412b8567fSIuliana Prodan 
26512b8567fSIuliana Prodan 	/* shared descriptor for ahash_update */
26612b8567fSIuliana Prodan 	desc = ctx->sh_desc_update;
26787870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_UPDATE,
268a2fb864cSHoria Geantă 			    ctx->ctx_len, ctx->ctx_len);
26912b8567fSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma,
27012b8567fSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
27112b8567fSIuliana Prodan 	print_hex_dump_debug("axcbc update shdesc@" __stringify(__LINE__)" : ",
27212b8567fSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
27312b8567fSIuliana Prodan 			     1);
27412b8567fSIuliana Prodan 
27512b8567fSIuliana Prodan 	/* shared descriptor for ahash_{final,finup} */
27612b8567fSIuliana Prodan 	desc = ctx->sh_desc_fin;
27787870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_FINALIZE,
278a2fb864cSHoria Geantă 			    digestsize, ctx->ctx_len);
27912b8567fSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma,
28012b8567fSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
28112b8567fSIuliana Prodan 	print_hex_dump_debug("axcbc finup shdesc@" __stringify(__LINE__)" : ",
28212b8567fSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
28312b8567fSIuliana Prodan 			     1);
28412b8567fSIuliana Prodan 
28512b8567fSIuliana Prodan 	/* key is immediate data for INIT and INITFINAL states */
28612b8567fSIuliana Prodan 	ctx->adata.key_virt = ctx->key;
28712b8567fSIuliana Prodan 
28812b8567fSIuliana Prodan 	/* shared descriptor for first invocation of ahash_update */
28912b8567fSIuliana Prodan 	desc = ctx->sh_desc_update_first;
29087870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len,
291a2fb864cSHoria Geantă 			    ctx->ctx_len);
29212b8567fSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma,
29312b8567fSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
2946e005503SSascha Hauer 	print_hex_dump_debug("axcbc update first shdesc@" __stringify(__LINE__)
2956e005503SSascha Hauer 			     " : ", DUMP_PREFIX_ADDRESS, 16, 4, desc,
2966e005503SSascha Hauer 			     desc_bytes(desc), 1);
29712b8567fSIuliana Prodan 
29812b8567fSIuliana Prodan 	/* shared descriptor for ahash_digest */
29912b8567fSIuliana Prodan 	desc = ctx->sh_desc_digest;
30087870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INITFINAL,
301a2fb864cSHoria Geantă 			    digestsize, ctx->ctx_len);
30212b8567fSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma,
30312b8567fSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
30412b8567fSIuliana Prodan 	print_hex_dump_debug("axcbc digest shdesc@" __stringify(__LINE__)" : ",
30512b8567fSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
30612b8567fSIuliana Prodan 			     1);
30787870cfbSIuliana Prodan 	return 0;
30887870cfbSIuliana Prodan }
30987870cfbSIuliana Prodan 
31087870cfbSIuliana Prodan static int acmac_set_sh_desc(struct crypto_ahash *ahash)
31187870cfbSIuliana Prodan {
3124cb4f7c1SHerbert Xu 	struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
31387870cfbSIuliana Prodan 	int digestsize = crypto_ahash_digestsize(ahash);
31487870cfbSIuliana Prodan 	struct device *jrdev = ctx->jrdev;
31587870cfbSIuliana Prodan 	u32 *desc;
31687870cfbSIuliana Prodan 
31787870cfbSIuliana Prodan 	/* shared descriptor for ahash_update */
31887870cfbSIuliana Prodan 	desc = ctx->sh_desc_update;
31987870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_UPDATE,
320a2fb864cSHoria Geantă 			    ctx->ctx_len, ctx->ctx_len);
32187870cfbSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma,
32287870cfbSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
32387870cfbSIuliana Prodan 	print_hex_dump_debug("acmac update shdesc@" __stringify(__LINE__)" : ",
32487870cfbSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
32587870cfbSIuliana Prodan 			     desc_bytes(desc), 1);
32687870cfbSIuliana Prodan 
32787870cfbSIuliana Prodan 	/* shared descriptor for ahash_{final,finup} */
32887870cfbSIuliana Prodan 	desc = ctx->sh_desc_fin;
32987870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_FINALIZE,
330a2fb864cSHoria Geantă 			    digestsize, ctx->ctx_len);
33187870cfbSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma,
33287870cfbSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
33387870cfbSIuliana Prodan 	print_hex_dump_debug("acmac finup shdesc@" __stringify(__LINE__)" : ",
33487870cfbSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
33587870cfbSIuliana Prodan 			     desc_bytes(desc), 1);
33687870cfbSIuliana Prodan 
33787870cfbSIuliana Prodan 	/* shared descriptor for first invocation of ahash_update */
33887870cfbSIuliana Prodan 	desc = ctx->sh_desc_update_first;
33987870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len,
340a2fb864cSHoria Geantă 			    ctx->ctx_len);
34187870cfbSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma,
34287870cfbSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
3436e005503SSascha Hauer 	print_hex_dump_debug("acmac update first shdesc@" __stringify(__LINE__)
3446e005503SSascha Hauer 			     " : ", DUMP_PREFIX_ADDRESS, 16, 4, desc,
34587870cfbSIuliana Prodan 			     desc_bytes(desc), 1);
34687870cfbSIuliana Prodan 
34787870cfbSIuliana Prodan 	/* shared descriptor for ahash_digest */
34887870cfbSIuliana Prodan 	desc = ctx->sh_desc_digest;
34987870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INITFINAL,
350a2fb864cSHoria Geantă 			    digestsize, ctx->ctx_len);
35187870cfbSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma,
35287870cfbSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
35387870cfbSIuliana Prodan 	print_hex_dump_debug("acmac digest shdesc@" __stringify(__LINE__)" : ",
35487870cfbSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
35587870cfbSIuliana Prodan 			     desc_bytes(desc), 1);
35612b8567fSIuliana Prodan 
35712b8567fSIuliana Prodan 	return 0;
35812b8567fSIuliana Prodan }
35912b8567fSIuliana Prodan 
360045e3678SYuan Kang /* Digest hash size if it is too large */
36130724445SHoria Geantă static int hash_digest_key(struct caam_hash_ctx *ctx, u32 *keylen, u8 *key,
36230724445SHoria Geantă 			   u32 digestsize)
363045e3678SYuan Kang {
364045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
365045e3678SYuan Kang 	u32 *desc;
366045e3678SYuan Kang 	struct split_key_result result;
36730724445SHoria Geantă 	dma_addr_t key_dma;
3689e6df0fdSMarkus Elfring 	int ret;
369045e3678SYuan Kang 
370199354d7SHerbert Xu 	desc = kmalloc(CAAM_CMD_SZ * 8 + CAAM_PTR_SZ * 2, GFP_KERNEL);
3712af8f4a2SKim Phillips 	if (!desc) {
3722af8f4a2SKim Phillips 		dev_err(jrdev, "unable to allocate key input memory\n");
3732af8f4a2SKim Phillips 		return -ENOMEM;
3742af8f4a2SKim Phillips 	}
375045e3678SYuan Kang 
376045e3678SYuan Kang 	init_job_desc(desc, 0);
377045e3678SYuan Kang 
37830724445SHoria Geantă 	key_dma = dma_map_single(jrdev, key, *keylen, DMA_BIDIRECTIONAL);
37930724445SHoria Geantă 	if (dma_mapping_error(jrdev, key_dma)) {
38030724445SHoria Geantă 		dev_err(jrdev, "unable to map key memory\n");
381045e3678SYuan Kang 		kfree(desc);
382045e3678SYuan Kang 		return -ENOMEM;
383045e3678SYuan Kang 	}
384045e3678SYuan Kang 
385045e3678SYuan Kang 	/* Job descriptor to perform unkeyed hash on key_in */
386db57656bSHoria Geantă 	append_operation(desc, ctx->adata.algtype | OP_ALG_ENCRYPT |
387045e3678SYuan Kang 			 OP_ALG_AS_INITFINAL);
38830724445SHoria Geantă 	append_seq_in_ptr(desc, key_dma, *keylen, 0);
389045e3678SYuan Kang 	append_seq_fifo_load(desc, *keylen, FIFOLD_CLASS_CLASS2 |
390045e3678SYuan Kang 			     FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_MSG);
39130724445SHoria Geantă 	append_seq_out_ptr(desc, key_dma, digestsize, 0);
392045e3678SYuan Kang 	append_seq_store(desc, digestsize, LDST_CLASS_2_CCB |
393045e3678SYuan Kang 			 LDST_SRCDST_BYTE_CONTEXT);
394045e3678SYuan Kang 
3956e005503SSascha Hauer 	print_hex_dump_debug("key_in@"__stringify(__LINE__)": ",
39630724445SHoria Geantă 			     DUMP_PREFIX_ADDRESS, 16, 4, key, *keylen, 1);
3976e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
3986e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
3996e005503SSascha Hauer 			     1);
400045e3678SYuan Kang 
401045e3678SYuan Kang 	result.err = 0;
402045e3678SYuan Kang 	init_completion(&result.completion);
403045e3678SYuan Kang 
404045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, split_key_done, &result);
4054d370a10SIuliana Prodan 	if (ret == -EINPROGRESS) {
406045e3678SYuan Kang 		/* in progress */
4077459e1d2SHoria Geantă 		wait_for_completion(&result.completion);
408045e3678SYuan Kang 		ret = result.err;
4096e005503SSascha Hauer 
4106e005503SSascha Hauer 		print_hex_dump_debug("digested key@"__stringify(__LINE__)": ",
4116e005503SSascha Hauer 				     DUMP_PREFIX_ADDRESS, 16, 4, key,
4126e005503SSascha Hauer 				     digestsize, 1);
413045e3678SYuan Kang 	}
41430724445SHoria Geantă 	dma_unmap_single(jrdev, key_dma, *keylen, DMA_BIDIRECTIONAL);
415045e3678SYuan Kang 
416e11aa9f1SHoria Geanta 	*keylen = digestsize;
417e11aa9f1SHoria Geanta 
418045e3678SYuan Kang 	kfree(desc);
419045e3678SYuan Kang 
420045e3678SYuan Kang 	return ret;
421045e3678SYuan Kang }
422045e3678SYuan Kang 
423045e3678SYuan Kang static int ahash_setkey(struct crypto_ahash *ahash,
424045e3678SYuan Kang 			const u8 *key, unsigned int keylen)
425045e3678SYuan Kang {
4264cb4f7c1SHerbert Xu 	struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
4276e005503SSascha Hauer 	struct device *jrdev = ctx->jrdev;
428045e3678SYuan Kang 	int blocksize = crypto_tfm_alg_blocksize(&ahash->base);
429045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
4307e0880b9SHoria Geantă 	struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctx->jrdev->parent);
4319e6df0fdSMarkus Elfring 	int ret;
432045e3678SYuan Kang 	u8 *hashed_key = NULL;
433045e3678SYuan Kang 
4346e005503SSascha Hauer 	dev_dbg(jrdev, "keylen %d\n", keylen);
435045e3678SYuan Kang 
436045e3678SYuan Kang 	if (keylen > blocksize) {
437199354d7SHerbert Xu 		unsigned int aligned_len =
438199354d7SHerbert Xu 			ALIGN(keylen, dma_get_cache_alignment());
439199354d7SHerbert Xu 
440199354d7SHerbert Xu 		if (aligned_len < keylen)
441199354d7SHerbert Xu 			return -EOVERFLOW;
442199354d7SHerbert Xu 
443199354d7SHerbert Xu 		hashed_key = kmemdup(key, keylen, GFP_KERNEL);
444045e3678SYuan Kang 		if (!hashed_key)
445045e3678SYuan Kang 			return -ENOMEM;
44630724445SHoria Geantă 		ret = hash_digest_key(ctx, &keylen, hashed_key, digestsize);
447045e3678SYuan Kang 		if (ret)
448d6e7a7d0SMarkus Elfring 			goto bad_free_key;
449045e3678SYuan Kang 		key = hashed_key;
450045e3678SYuan Kang 	}
451045e3678SYuan Kang 
4527e0880b9SHoria Geantă 	/*
4537e0880b9SHoria Geantă 	 * If DKP is supported, use it in the shared descriptor to generate
4547e0880b9SHoria Geantă 	 * the split key.
4557e0880b9SHoria Geantă 	 */
4567e0880b9SHoria Geantă 	if (ctrlpriv->era >= 6) {
4577e0880b9SHoria Geantă 		ctx->adata.key_inline = true;
4587e0880b9SHoria Geantă 		ctx->adata.keylen = keylen;
4597e0880b9SHoria Geantă 		ctx->adata.keylen_pad = split_key_len(ctx->adata.algtype &
4607e0880b9SHoria Geantă 						      OP_ALG_ALGSEL_MASK);
4617e0880b9SHoria Geantă 
4627e0880b9SHoria Geantă 		if (ctx->adata.keylen_pad > CAAM_MAX_HASH_KEY_SIZE)
463d6e7a7d0SMarkus Elfring 			goto bad_free_key;
464045e3678SYuan Kang 
4657e0880b9SHoria Geantă 		memcpy(ctx->key, key, keylen);
466e9b4913aSHoria Geantă 
467e9b4913aSHoria Geantă 		/*
468e9b4913aSHoria Geantă 		 * In case |user key| > |derived key|, using DKP<imm,imm>
469e9b4913aSHoria Geantă 		 * would result in invalid opcodes (last bytes of user key) in
470e9b4913aSHoria Geantă 		 * the resulting descriptor. Use DKP<ptr,imm> instead => both
471e9b4913aSHoria Geantă 		 * virtual and dma key addresses are needed.
472e9b4913aSHoria Geantă 		 */
473e9b4913aSHoria Geantă 		if (keylen > ctx->adata.keylen_pad)
474e9b4913aSHoria Geantă 			dma_sync_single_for_device(ctx->jrdev,
475e9b4913aSHoria Geantă 						   ctx->adata.key_dma,
476e9b4913aSHoria Geantă 						   ctx->adata.keylen_pad,
477e9b4913aSHoria Geantă 						   DMA_TO_DEVICE);
4787e0880b9SHoria Geantă 	} else {
4797e0880b9SHoria Geantă 		ret = gen_split_key(ctx->jrdev, ctx->key, &ctx->adata, key,
4807e0880b9SHoria Geantă 				    keylen, CAAM_MAX_HASH_KEY_SIZE);
4817e0880b9SHoria Geantă 		if (ret)
4827e0880b9SHoria Geantă 			goto bad_free_key;
4837e0880b9SHoria Geantă 	}
484045e3678SYuan Kang 
485045e3678SYuan Kang 	kfree(hashed_key);
486cfb725f6SHoria Geantă 	return ahash_set_sh_desc(ahash);
487d6e7a7d0SMarkus Elfring  bad_free_key:
488045e3678SYuan Kang 	kfree(hashed_key);
489045e3678SYuan Kang 	return -EINVAL;
490045e3678SYuan Kang }
491045e3678SYuan Kang 
49212b8567fSIuliana Prodan static int axcbc_setkey(struct crypto_ahash *ahash, const u8 *key,
49312b8567fSIuliana Prodan 			unsigned int keylen)
49412b8567fSIuliana Prodan {
4954cb4f7c1SHerbert Xu 	struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
49612b8567fSIuliana Prodan 	struct device *jrdev = ctx->jrdev;
49712b8567fSIuliana Prodan 
498674f368aSEric Biggers 	if (keylen != AES_KEYSIZE_128)
499836d8f43SIuliana Prodan 		return -EINVAL;
500836d8f43SIuliana Prodan 
50112b8567fSIuliana Prodan 	memcpy(ctx->key, key, keylen);
502a2fb864cSHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->adata.key_dma, keylen,
503a2fb864cSHoria Geantă 				   DMA_TO_DEVICE);
50412b8567fSIuliana Prodan 	ctx->adata.keylen = keylen;
50512b8567fSIuliana Prodan 
50612b8567fSIuliana Prodan 	print_hex_dump_debug("axcbc ctx.key@" __stringify(__LINE__)" : ",
50712b8567fSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, ctx->key, keylen, 1);
50812b8567fSIuliana Prodan 
50912b8567fSIuliana Prodan 	return axcbc_set_sh_desc(ahash);
51012b8567fSIuliana Prodan }
51187870cfbSIuliana Prodan 
51287870cfbSIuliana Prodan static int acmac_setkey(struct crypto_ahash *ahash, const u8 *key,
51387870cfbSIuliana Prodan 			unsigned int keylen)
51487870cfbSIuliana Prodan {
5154cb4f7c1SHerbert Xu 	struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
516836d8f43SIuliana Prodan 	int err;
517836d8f43SIuliana Prodan 
518836d8f43SIuliana Prodan 	err = aes_check_keylen(keylen);
519674f368aSEric Biggers 	if (err)
520836d8f43SIuliana Prodan 		return err;
52187870cfbSIuliana Prodan 
52287870cfbSIuliana Prodan 	/* key is immediate data for all cmac shared descriptors */
52387870cfbSIuliana Prodan 	ctx->adata.key_virt = key;
52487870cfbSIuliana Prodan 	ctx->adata.keylen = keylen;
52587870cfbSIuliana Prodan 
52687870cfbSIuliana Prodan 	print_hex_dump_debug("acmac ctx.key@" __stringify(__LINE__)" : ",
52787870cfbSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
52887870cfbSIuliana Prodan 
52987870cfbSIuliana Prodan 	return acmac_set_sh_desc(ahash);
53087870cfbSIuliana Prodan }
53187870cfbSIuliana Prodan 
532045e3678SYuan Kang /*
533045e3678SYuan Kang  * ahash_edesc - s/w-extended ahash descriptor
534045e3678SYuan Kang  * @sec4_sg_dma: physical mapped address of h/w link table
535045e3678SYuan Kang  * @src_nents: number of segments in input scatterlist
536045e3678SYuan Kang  * @sec4_sg_bytes: length of dma mapped sec4_sg space
53721b014f0SIuliana Prodan  * @bklog: stored to determine if the request needs backlog
538045e3678SYuan Kang  * @hw_desc: the h/w job descriptor followed by any referenced link tables
539343e44b1SRussell King  * @sec4_sg: h/w link table
540045e3678SYuan Kang  */
541045e3678SYuan Kang struct ahash_edesc {
542045e3678SYuan Kang 	dma_addr_t sec4_sg_dma;
543045e3678SYuan Kang 	int src_nents;
544045e3678SYuan Kang 	int sec4_sg_bytes;
54521b014f0SIuliana Prodan 	bool bklog;
5461a3daadcSAndrey Smirnov 	u32 hw_desc[DESC_JOB_IO_LEN_MAX / sizeof(u32)] ____cacheline_aligned;
5475a8a0765SGustavo A. R. Silva 	struct sec4_sg_entry sec4_sg[];
548045e3678SYuan Kang };
549045e3678SYuan Kang 
550045e3678SYuan Kang static inline void ahash_unmap(struct device *dev,
551045e3678SYuan Kang 			struct ahash_edesc *edesc,
552045e3678SYuan Kang 			struct ahash_request *req, int dst_len)
553045e3678SYuan Kang {
5544cb4f7c1SHerbert Xu 	struct caam_hash_state *state = ahash_request_ctx_dma(req);
555944c3d4dSHoria Geantă 
556045e3678SYuan Kang 	if (edesc->src_nents)
55713fb8fd7SLABBE Corentin 		dma_unmap_sg(dev, req->src, edesc->src_nents, DMA_TO_DEVICE);
558045e3678SYuan Kang 
559045e3678SYuan Kang 	if (edesc->sec4_sg_bytes)
560045e3678SYuan Kang 		dma_unmap_single(dev, edesc->sec4_sg_dma,
561045e3678SYuan Kang 				 edesc->sec4_sg_bytes, DMA_TO_DEVICE);
562944c3d4dSHoria Geantă 
563944c3d4dSHoria Geantă 	if (state->buf_dma) {
56446b49abcSAndrei Botila 		dma_unmap_single(dev, state->buf_dma, state->buflen,
565944c3d4dSHoria Geantă 				 DMA_TO_DEVICE);
566944c3d4dSHoria Geantă 		state->buf_dma = 0;
567944c3d4dSHoria Geantă 	}
568045e3678SYuan Kang }
569045e3678SYuan Kang 
570045e3678SYuan Kang static inline void ahash_unmap_ctx(struct device *dev,
571045e3678SYuan Kang 			struct ahash_edesc *edesc,
572045e3678SYuan Kang 			struct ahash_request *req, int dst_len, u32 flag)
573045e3678SYuan Kang {
5744cb4f7c1SHerbert Xu 	struct caam_hash_state *state = ahash_request_ctx_dma(req);
575045e3678SYuan Kang 
57687ec02e7SHoria Geantă 	if (state->ctx_dma) {
57765055e21SFranck LENORMAND 		dma_unmap_single(dev, state->ctx_dma, state->ctx_dma_len, flag);
57887ec02e7SHoria Geantă 		state->ctx_dma = 0;
57987ec02e7SHoria Geantă 	}
580045e3678SYuan Kang 	ahash_unmap(dev, edesc, req, dst_len);
581045e3678SYuan Kang }
582045e3678SYuan Kang 
583c3f7394eSIuliana Prodan static inline void ahash_done_cpy(struct device *jrdev, u32 *desc, u32 err,
584c3f7394eSIuliana Prodan 				  void *context, enum dma_data_direction dir)
585045e3678SYuan Kang {
586045e3678SYuan Kang 	struct ahash_request *req = context;
58721b014f0SIuliana Prodan 	struct caam_drv_private_jr *jrp = dev_get_drvdata(jrdev);
588045e3678SYuan Kang 	struct ahash_edesc *edesc;
589045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
590045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
5914cb4f7c1SHerbert Xu 	struct caam_hash_state *state = ahash_request_ctx_dma(req);
5924cb4f7c1SHerbert Xu 	struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
5931984aaeeSHoria Geantă 	int ecode = 0;
59463db32e6SIuliana Prodan 	bool has_bklog;
595045e3678SYuan Kang 
5966e005503SSascha Hauer 	dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
597045e3678SYuan Kang 
59821b014f0SIuliana Prodan 	edesc = state->edesc;
59963db32e6SIuliana Prodan 	has_bklog = edesc->bklog;
60021b014f0SIuliana Prodan 
601fa9659cdSMarek Vasut 	if (err)
6021984aaeeSHoria Geantă 		ecode = caam_jr_strstatus(jrdev, err);
603045e3678SYuan Kang 
604c3f7394eSIuliana Prodan 	ahash_unmap_ctx(jrdev, edesc, req, digestsize, dir);
605c19650d6SHoria Geantă 	memcpy(req->result, state->caam_ctx, digestsize);
606045e3678SYuan Kang 	kfree(edesc);
607045e3678SYuan Kang 
6086e005503SSascha Hauer 	print_hex_dump_debug("ctx@"__stringify(__LINE__)": ",
609045e3678SYuan Kang 			     DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
610045e3678SYuan Kang 			     ctx->ctx_len, 1);
611045e3678SYuan Kang 
61221b014f0SIuliana Prodan 	/*
61321b014f0SIuliana Prodan 	 * If no backlog flag, the completion of the request is done
61421b014f0SIuliana Prodan 	 * by CAAM, not crypto engine.
61521b014f0SIuliana Prodan 	 */
61663db32e6SIuliana Prodan 	if (!has_bklog)
617*4bc713a4SHerbert Xu 		ahash_request_complete(req, ecode);
61821b014f0SIuliana Prodan 	else
61921b014f0SIuliana Prodan 		crypto_finalize_hash_request(jrp->engine, req, ecode);
620045e3678SYuan Kang }
621045e3678SYuan Kang 
622c3f7394eSIuliana Prodan static void ahash_done(struct device *jrdev, u32 *desc, u32 err,
623c3f7394eSIuliana Prodan 		       void *context)
624c3f7394eSIuliana Prodan {
625c3f7394eSIuliana Prodan 	ahash_done_cpy(jrdev, desc, err, context, DMA_FROM_DEVICE);
626c3f7394eSIuliana Prodan }
627c3f7394eSIuliana Prodan 
628c3f7394eSIuliana Prodan static void ahash_done_ctx_src(struct device *jrdev, u32 *desc, u32 err,
629c3f7394eSIuliana Prodan 			       void *context)
630c3f7394eSIuliana Prodan {
631c3f7394eSIuliana Prodan 	ahash_done_cpy(jrdev, desc, err, context, DMA_BIDIRECTIONAL);
632c3f7394eSIuliana Prodan }
633c3f7394eSIuliana Prodan 
634c3f7394eSIuliana Prodan static inline void ahash_done_switch(struct device *jrdev, u32 *desc, u32 err,
635c3f7394eSIuliana Prodan 				     void *context, enum dma_data_direction dir)
636c3f7394eSIuliana Prodan {
637c3f7394eSIuliana Prodan 	struct ahash_request *req = context;
63821b014f0SIuliana Prodan 	struct caam_drv_private_jr *jrp = dev_get_drvdata(jrdev);
639c3f7394eSIuliana Prodan 	struct ahash_edesc *edesc;
640c3f7394eSIuliana Prodan 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
6414cb4f7c1SHerbert Xu 	struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
6424cb4f7c1SHerbert Xu 	struct caam_hash_state *state = ahash_request_ctx_dma(req);
643c3f7394eSIuliana Prodan 	int digestsize = crypto_ahash_digestsize(ahash);
644c3f7394eSIuliana Prodan 	int ecode = 0;
64563db32e6SIuliana Prodan 	bool has_bklog;
646c3f7394eSIuliana Prodan 
647c3f7394eSIuliana Prodan 	dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
648c3f7394eSIuliana Prodan 
64921b014f0SIuliana Prodan 	edesc = state->edesc;
65063db32e6SIuliana Prodan 	has_bklog = edesc->bklog;
651c3f7394eSIuliana Prodan 	if (err)
652c3f7394eSIuliana Prodan 		ecode = caam_jr_strstatus(jrdev, err);
653c3f7394eSIuliana Prodan 
654c3f7394eSIuliana Prodan 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, dir);
655c3f7394eSIuliana Prodan 	kfree(edesc);
656c3f7394eSIuliana Prodan 
657c3f7394eSIuliana Prodan 	scatterwalk_map_and_copy(state->buf, req->src,
658c3f7394eSIuliana Prodan 				 req->nbytes - state->next_buflen,
659c3f7394eSIuliana Prodan 				 state->next_buflen, 0);
660c3f7394eSIuliana Prodan 	state->buflen = state->next_buflen;
661c3f7394eSIuliana Prodan 
662c3f7394eSIuliana Prodan 	print_hex_dump_debug("buf@" __stringify(__LINE__)": ",
663c3f7394eSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, state->buf,
664c3f7394eSIuliana Prodan 			     state->buflen, 1);
665c3f7394eSIuliana Prodan 
666c3f7394eSIuliana Prodan 	print_hex_dump_debug("ctx@"__stringify(__LINE__)": ",
667c3f7394eSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
668c3f7394eSIuliana Prodan 			     ctx->ctx_len, 1);
669c3f7394eSIuliana Prodan 	if (req->result)
670c3f7394eSIuliana Prodan 		print_hex_dump_debug("result@"__stringify(__LINE__)": ",
671c3f7394eSIuliana Prodan 				     DUMP_PREFIX_ADDRESS, 16, 4, req->result,
672c3f7394eSIuliana Prodan 				     digestsize, 1);
673c3f7394eSIuliana Prodan 
67421b014f0SIuliana Prodan 	/*
67521b014f0SIuliana Prodan 	 * If no backlog flag, the completion of the request is done
67621b014f0SIuliana Prodan 	 * by CAAM, not crypto engine.
67721b014f0SIuliana Prodan 	 */
67863db32e6SIuliana Prodan 	if (!has_bklog)
679*4bc713a4SHerbert Xu 		ahash_request_complete(req, ecode);
68021b014f0SIuliana Prodan 	else
68121b014f0SIuliana Prodan 		crypto_finalize_hash_request(jrp->engine, req, ecode);
68221b014f0SIuliana Prodan 
683c3f7394eSIuliana Prodan }
684c3f7394eSIuliana Prodan 
685045e3678SYuan Kang static void ahash_done_bi(struct device *jrdev, u32 *desc, u32 err,
686045e3678SYuan Kang 			  void *context)
687045e3678SYuan Kang {
688c3f7394eSIuliana Prodan 	ahash_done_switch(jrdev, desc, err, context, DMA_BIDIRECTIONAL);
689045e3678SYuan Kang }
690045e3678SYuan Kang 
691045e3678SYuan Kang static void ahash_done_ctx_dst(struct device *jrdev, u32 *desc, u32 err,
692045e3678SYuan Kang 			       void *context)
693045e3678SYuan Kang {
694c3f7394eSIuliana Prodan 	ahash_done_switch(jrdev, desc, err, context, DMA_FROM_DEVICE);
695045e3678SYuan Kang }
696045e3678SYuan Kang 
6975588d039SRussell King /*
6985588d039SRussell King  * Allocate an enhanced descriptor, which contains the hardware descriptor
6995588d039SRussell King  * and space for hardware scatter table containing sg_num entries.
7005588d039SRussell King  */
7012ba1e798SIuliana Prodan static struct ahash_edesc *ahash_edesc_alloc(struct ahash_request *req,
70230a43b44SRussell King 					     int sg_num, u32 *sh_desc,
7032ba1e798SIuliana Prodan 					     dma_addr_t sh_desc_dma)
7045588d039SRussell King {
7052ba1e798SIuliana Prodan 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
7064cb4f7c1SHerbert Xu 	struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
7074cb4f7c1SHerbert Xu 	struct caam_hash_state *state = ahash_request_ctx_dma(req);
7082ba1e798SIuliana Prodan 	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
7092ba1e798SIuliana Prodan 		       GFP_KERNEL : GFP_ATOMIC;
7105588d039SRussell King 	struct ahash_edesc *edesc;
7115588d039SRussell King 	unsigned int sg_size = sg_num * sizeof(struct sec4_sg_entry);
7125588d039SRussell King 
713199354d7SHerbert Xu 	edesc = kzalloc(sizeof(*edesc) + sg_size, flags);
7145588d039SRussell King 	if (!edesc) {
7155588d039SRussell King 		dev_err(ctx->jrdev, "could not allocate extended descriptor\n");
7165588d039SRussell King 		return NULL;
7175588d039SRussell King 	}
7185588d039SRussell King 
71921b014f0SIuliana Prodan 	state->edesc = edesc;
72021b014f0SIuliana Prodan 
72130a43b44SRussell King 	init_job_desc_shared(edesc->hw_desc, sh_desc_dma, desc_len(sh_desc),
72230a43b44SRussell King 			     HDR_SHARE_DEFER | HDR_REVERSE);
72330a43b44SRussell King 
7245588d039SRussell King 	return edesc;
7255588d039SRussell King }
7265588d039SRussell King 
72765cf164aSRussell King static int ahash_edesc_add_src(struct caam_hash_ctx *ctx,
72865cf164aSRussell King 			       struct ahash_edesc *edesc,
72965cf164aSRussell King 			       struct ahash_request *req, int nents,
73065cf164aSRussell King 			       unsigned int first_sg,
73165cf164aSRussell King 			       unsigned int first_bytes, size_t to_hash)
73265cf164aSRussell King {
73365cf164aSRussell King 	dma_addr_t src_dma;
73465cf164aSRussell King 	u32 options;
73565cf164aSRussell King 
73665cf164aSRussell King 	if (nents > 1 || first_sg) {
73765cf164aSRussell King 		struct sec4_sg_entry *sg = edesc->sec4_sg;
738a5e5c133SHoria Geantă 		unsigned int sgsize = sizeof(*sg) *
739a5e5c133SHoria Geantă 				      pad_sg_nents(first_sg + nents);
74065cf164aSRussell King 
741059d73eeSHoria Geantă 		sg_to_sec4_sg_last(req->src, to_hash, sg + first_sg, 0);
74265cf164aSRussell King 
74365cf164aSRussell King 		src_dma = dma_map_single(ctx->jrdev, sg, sgsize, DMA_TO_DEVICE);
74465cf164aSRussell King 		if (dma_mapping_error(ctx->jrdev, src_dma)) {
74565cf164aSRussell King 			dev_err(ctx->jrdev, "unable to map S/G table\n");
74665cf164aSRussell King 			return -ENOMEM;
74765cf164aSRussell King 		}
74865cf164aSRussell King 
74965cf164aSRussell King 		edesc->sec4_sg_bytes = sgsize;
75065cf164aSRussell King 		edesc->sec4_sg_dma = src_dma;
75165cf164aSRussell King 		options = LDST_SGF;
75265cf164aSRussell King 	} else {
75365cf164aSRussell King 		src_dma = sg_dma_address(req->src);
75465cf164aSRussell King 		options = 0;
75565cf164aSRussell King 	}
75665cf164aSRussell King 
75765cf164aSRussell King 	append_seq_in_ptr(edesc->hw_desc, src_dma, first_bytes + to_hash,
75865cf164aSRussell King 			  options);
75965cf164aSRussell King 
76065cf164aSRussell King 	return 0;
76165cf164aSRussell King }
76265cf164aSRussell King 
76321b014f0SIuliana Prodan static int ahash_do_one_req(struct crypto_engine *engine, void *areq)
76421b014f0SIuliana Prodan {
76521b014f0SIuliana Prodan 	struct ahash_request *req = ahash_request_cast(areq);
7664cb4f7c1SHerbert Xu 	struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(crypto_ahash_reqtfm(req));
7674cb4f7c1SHerbert Xu 	struct caam_hash_state *state = ahash_request_ctx_dma(req);
76821b014f0SIuliana Prodan 	struct device *jrdev = ctx->jrdev;
76921b014f0SIuliana Prodan 	u32 *desc = state->edesc->hw_desc;
77021b014f0SIuliana Prodan 	int ret;
77121b014f0SIuliana Prodan 
77221b014f0SIuliana Prodan 	state->edesc->bklog = true;
77321b014f0SIuliana Prodan 
77421b014f0SIuliana Prodan 	ret = caam_jr_enqueue(jrdev, desc, state->ahash_op_done, req);
77521b014f0SIuliana Prodan 
776087e1d71SGaurav Jain 	if (ret == -ENOSPC && engine->retry_support)
777087e1d71SGaurav Jain 		return ret;
778087e1d71SGaurav Jain 
77921b014f0SIuliana Prodan 	if (ret != -EINPROGRESS) {
78021b014f0SIuliana Prodan 		ahash_unmap(jrdev, state->edesc, req, 0);
78121b014f0SIuliana Prodan 		kfree(state->edesc);
78221b014f0SIuliana Prodan 	} else {
78321b014f0SIuliana Prodan 		ret = 0;
78421b014f0SIuliana Prodan 	}
78521b014f0SIuliana Prodan 
78621b014f0SIuliana Prodan 	return ret;
78721b014f0SIuliana Prodan }
78821b014f0SIuliana Prodan 
78921b014f0SIuliana Prodan static int ahash_enqueue_req(struct device *jrdev,
79021b014f0SIuliana Prodan 			     void (*cbk)(struct device *jrdev, u32 *desc,
79121b014f0SIuliana Prodan 					 u32 err, void *context),
79221b014f0SIuliana Prodan 			     struct ahash_request *req,
79321b014f0SIuliana Prodan 			     int dst_len, enum dma_data_direction dir)
79421b014f0SIuliana Prodan {
79521b014f0SIuliana Prodan 	struct caam_drv_private_jr *jrpriv = dev_get_drvdata(jrdev);
7964cb4f7c1SHerbert Xu 	struct caam_hash_state *state = ahash_request_ctx_dma(req);
79721b014f0SIuliana Prodan 	struct ahash_edesc *edesc = state->edesc;
79821b014f0SIuliana Prodan 	u32 *desc = edesc->hw_desc;
79921b014f0SIuliana Prodan 	int ret;
80021b014f0SIuliana Prodan 
80121b014f0SIuliana Prodan 	state->ahash_op_done = cbk;
80221b014f0SIuliana Prodan 
80321b014f0SIuliana Prodan 	/*
80421b014f0SIuliana Prodan 	 * Only the backlog request are sent to crypto-engine since the others
80521b014f0SIuliana Prodan 	 * can be handled by CAAM, if free, especially since JR has up to 1024
80621b014f0SIuliana Prodan 	 * entries (more than the 10 entries from crypto-engine).
80721b014f0SIuliana Prodan 	 */
80821b014f0SIuliana Prodan 	if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)
80921b014f0SIuliana Prodan 		ret = crypto_transfer_hash_request_to_engine(jrpriv->engine,
81021b014f0SIuliana Prodan 							     req);
81121b014f0SIuliana Prodan 	else
81221b014f0SIuliana Prodan 		ret = caam_jr_enqueue(jrdev, desc, cbk, req);
81321b014f0SIuliana Prodan 
81421b014f0SIuliana Prodan 	if ((ret != -EINPROGRESS) && (ret != -EBUSY)) {
81521b014f0SIuliana Prodan 		ahash_unmap_ctx(jrdev, edesc, req, dst_len, dir);
81621b014f0SIuliana Prodan 		kfree(edesc);
81721b014f0SIuliana Prodan 	}
81821b014f0SIuliana Prodan 
81921b014f0SIuliana Prodan 	return ret;
82021b014f0SIuliana Prodan }
82121b014f0SIuliana Prodan 
822045e3678SYuan Kang /* submit update job descriptor */
823045e3678SYuan Kang static int ahash_update_ctx(struct ahash_request *req)
824045e3678SYuan Kang {
825045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
8264cb4f7c1SHerbert Xu 	struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
8274cb4f7c1SHerbert Xu 	struct caam_hash_state *state = ahash_request_ctx_dma(req);
828045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
82946b49abcSAndrei Botila 	u8 *buf = state->buf;
83046b49abcSAndrei Botila 	int *buflen = &state->buflen;
83146b49abcSAndrei Botila 	int *next_buflen = &state->next_buflen;
83212b8567fSIuliana Prodan 	int blocksize = crypto_ahash_blocksize(ahash);
833045e3678SYuan Kang 	int in_len = *buflen + req->nbytes, to_hash;
83430a43b44SRussell King 	u32 *desc;
835bc13c69eSRussell King 	int src_nents, mapped_nents, sec4_sg_bytes, sec4_sg_src_index;
836045e3678SYuan Kang 	struct ahash_edesc *edesc;
837045e3678SYuan Kang 	int ret = 0;
838045e3678SYuan Kang 
83912b8567fSIuliana Prodan 	*next_buflen = in_len & (blocksize - 1);
840045e3678SYuan Kang 	to_hash = in_len - *next_buflen;
841045e3678SYuan Kang 
84212b8567fSIuliana Prodan 	/*
84387870cfbSIuliana Prodan 	 * For XCBC and CMAC, if to_hash is multiple of block size,
84412b8567fSIuliana Prodan 	 * keep last block in internal buffer
84512b8567fSIuliana Prodan 	 */
84687870cfbSIuliana Prodan 	if ((is_xcbc_aes(ctx->adata.algtype) ||
84787870cfbSIuliana Prodan 	     is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize &&
84812b8567fSIuliana Prodan 	     (*next_buflen == 0)) {
84912b8567fSIuliana Prodan 		*next_buflen = blocksize;
85012b8567fSIuliana Prodan 		to_hash -= blocksize;
85112b8567fSIuliana Prodan 	}
85212b8567fSIuliana Prodan 
853045e3678SYuan Kang 	if (to_hash) {
854a5e5c133SHoria Geantă 		int pad_nents;
855059d73eeSHoria Geantă 		int src_len = req->nbytes - *next_buflen;
856a5e5c133SHoria Geantă 
857059d73eeSHoria Geantă 		src_nents = sg_nents_for_len(req->src, src_len);
858f9970c28SLABBE Corentin 		if (src_nents < 0) {
859f9970c28SLABBE Corentin 			dev_err(jrdev, "Invalid number of src SG.\n");
860f9970c28SLABBE Corentin 			return src_nents;
861f9970c28SLABBE Corentin 		}
862bc13c69eSRussell King 
863bc13c69eSRussell King 		if (src_nents) {
864bc13c69eSRussell King 			mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
865bc13c69eSRussell King 						  DMA_TO_DEVICE);
866bc13c69eSRussell King 			if (!mapped_nents) {
867bc13c69eSRussell King 				dev_err(jrdev, "unable to DMA map source\n");
868bc13c69eSRussell King 				return -ENOMEM;
869bc13c69eSRussell King 			}
870bc13c69eSRussell King 		} else {
871bc13c69eSRussell King 			mapped_nents = 0;
872bc13c69eSRussell King 		}
873bc13c69eSRussell King 
874045e3678SYuan Kang 		sec4_sg_src_index = 1 + (*buflen ? 1 : 0);
875a5e5c133SHoria Geantă 		pad_nents = pad_sg_nents(sec4_sg_src_index + mapped_nents);
876a5e5c133SHoria Geantă 		sec4_sg_bytes = pad_nents * sizeof(struct sec4_sg_entry);
877045e3678SYuan Kang 
878045e3678SYuan Kang 		/*
879045e3678SYuan Kang 		 * allocate space for base edesc and hw desc commands,
880045e3678SYuan Kang 		 * link tables
881045e3678SYuan Kang 		 */
8822ba1e798SIuliana Prodan 		edesc = ahash_edesc_alloc(req, pad_nents, ctx->sh_desc_update,
8832ba1e798SIuliana Prodan 					  ctx->sh_desc_update_dma);
884045e3678SYuan Kang 		if (!edesc) {
885bc13c69eSRussell King 			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
886045e3678SYuan Kang 			return -ENOMEM;
887045e3678SYuan Kang 		}
888045e3678SYuan Kang 
889045e3678SYuan Kang 		edesc->src_nents = src_nents;
890045e3678SYuan Kang 		edesc->sec4_sg_bytes = sec4_sg_bytes;
891045e3678SYuan Kang 
892dfcd8393SHoria Geantă 		ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
893045e3678SYuan Kang 					 edesc->sec4_sg, DMA_BIDIRECTIONAL);
894ce572085SHoria Geanta 		if (ret)
89558b0e5d0SMarkus Elfring 			goto unmap_ctx;
896045e3678SYuan Kang 
897944c3d4dSHoria Geantă 		ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
898944c3d4dSHoria Geantă 		if (ret)
899944c3d4dSHoria Geantă 			goto unmap_ctx;
900045e3678SYuan Kang 
901b4e9e931SIuliana Prodan 		if (mapped_nents)
902059d73eeSHoria Geantă 			sg_to_sec4_sg_last(req->src, src_len,
903bc13c69eSRussell King 					   edesc->sec4_sg + sec4_sg_src_index,
904bc13c69eSRussell King 					   0);
905b4e9e931SIuliana Prodan 		else
906b4e9e931SIuliana Prodan 			sg_to_sec4_set_last(edesc->sec4_sg + sec4_sg_src_index -
907b4e9e931SIuliana Prodan 					    1);
908b4e9e931SIuliana Prodan 
909045e3678SYuan Kang 		desc = edesc->hw_desc;
910045e3678SYuan Kang 
9111da2be33SRuchika Gupta 		edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
9121da2be33SRuchika Gupta 						     sec4_sg_bytes,
9131da2be33SRuchika Gupta 						     DMA_TO_DEVICE);
914ce572085SHoria Geanta 		if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
915ce572085SHoria Geanta 			dev_err(jrdev, "unable to map S/G table\n");
91632686d34SRussell King 			ret = -ENOMEM;
91758b0e5d0SMarkus Elfring 			goto unmap_ctx;
918ce572085SHoria Geanta 		}
9191da2be33SRuchika Gupta 
920045e3678SYuan Kang 		append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len +
921045e3678SYuan Kang 				       to_hash, LDST_SGF);
922045e3678SYuan Kang 
923045e3678SYuan Kang 		append_seq_out_ptr(desc, state->ctx_dma, ctx->ctx_len, 0);
924045e3678SYuan Kang 
9256e005503SSascha Hauer 		print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
926045e3678SYuan Kang 				     DUMP_PREFIX_ADDRESS, 16, 4, desc,
927045e3678SYuan Kang 				     desc_bytes(desc), 1);
928045e3678SYuan Kang 
92921b014f0SIuliana Prodan 		ret = ahash_enqueue_req(jrdev, ahash_done_bi, req,
93021b014f0SIuliana Prodan 					ctx->ctx_len, DMA_BIDIRECTIONAL);
931045e3678SYuan Kang 	} else if (*next_buflen) {
932307fd543SCristian Stoica 		scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
933307fd543SCristian Stoica 					 req->nbytes, 0);
934045e3678SYuan Kang 		*buflen = *next_buflen;
9356e005503SSascha Hauer 
9366e005503SSascha Hauer 		print_hex_dump_debug("buf@" __stringify(__LINE__)": ",
93746b49abcSAndrei Botila 				     DUMP_PREFIX_ADDRESS, 16, 4, buf,
93846b49abcSAndrei Botila 				     *buflen, 1);
93946b49abcSAndrei Botila 	}
940045e3678SYuan Kang 
941045e3678SYuan Kang 	return ret;
94258b0e5d0SMarkus Elfring unmap_ctx:
94332686d34SRussell King 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_BIDIRECTIONAL);
94432686d34SRussell King 	kfree(edesc);
94532686d34SRussell King 	return ret;
946045e3678SYuan Kang }
947045e3678SYuan Kang 
948045e3678SYuan Kang static int ahash_final_ctx(struct ahash_request *req)
949045e3678SYuan Kang {
950045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
9514cb4f7c1SHerbert Xu 	struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
9524cb4f7c1SHerbert Xu 	struct caam_hash_state *state = ahash_request_ctx_dma(req);
953045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
95446b49abcSAndrei Botila 	int buflen = state->buflen;
95530a43b44SRussell King 	u32 *desc;
956a5e5c133SHoria Geantă 	int sec4_sg_bytes;
957045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
958045e3678SYuan Kang 	struct ahash_edesc *edesc;
9599e6df0fdSMarkus Elfring 	int ret;
960045e3678SYuan Kang 
961a5e5c133SHoria Geantă 	sec4_sg_bytes = pad_sg_nents(1 + (buflen ? 1 : 0)) *
962a5e5c133SHoria Geantă 			sizeof(struct sec4_sg_entry);
963045e3678SYuan Kang 
964045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
9652ba1e798SIuliana Prodan 	edesc = ahash_edesc_alloc(req, 4, ctx->sh_desc_fin,
9662ba1e798SIuliana Prodan 				  ctx->sh_desc_fin_dma);
9675588d039SRussell King 	if (!edesc)
968045e3678SYuan Kang 		return -ENOMEM;
969045e3678SYuan Kang 
970045e3678SYuan Kang 	desc = edesc->hw_desc;
971045e3678SYuan Kang 
972045e3678SYuan Kang 	edesc->sec4_sg_bytes = sec4_sg_bytes;
973045e3678SYuan Kang 
974dfcd8393SHoria Geantă 	ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
975c19650d6SHoria Geantă 				 edesc->sec4_sg, DMA_BIDIRECTIONAL);
976ce572085SHoria Geanta 	if (ret)
97758b0e5d0SMarkus Elfring 		goto unmap_ctx;
978045e3678SYuan Kang 
979944c3d4dSHoria Geantă 	ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
980944c3d4dSHoria Geantă 	if (ret)
981944c3d4dSHoria Geantă 		goto unmap_ctx;
982944c3d4dSHoria Geantă 
983a5e5c133SHoria Geantă 	sg_to_sec4_set_last(edesc->sec4_sg + (buflen ? 1 : 0));
984045e3678SYuan Kang 
9851da2be33SRuchika Gupta 	edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
9861da2be33SRuchika Gupta 					    sec4_sg_bytes, DMA_TO_DEVICE);
987ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
988ce572085SHoria Geanta 		dev_err(jrdev, "unable to map S/G table\n");
98932686d34SRussell King 		ret = -ENOMEM;
99058b0e5d0SMarkus Elfring 		goto unmap_ctx;
991ce572085SHoria Geanta 	}
9921da2be33SRuchika Gupta 
993045e3678SYuan Kang 	append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len + buflen,
994045e3678SYuan Kang 			  LDST_SGF);
995c19650d6SHoria Geantă 	append_seq_out_ptr(desc, state->ctx_dma, digestsize, 0);
996045e3678SYuan Kang 
9976e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
9986e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
9996e005503SSascha Hauer 			     1);
1000045e3678SYuan Kang 
100121b014f0SIuliana Prodan 	return ahash_enqueue_req(jrdev, ahash_done_ctx_src, req,
100221b014f0SIuliana Prodan 				 digestsize, DMA_BIDIRECTIONAL);
100358b0e5d0SMarkus Elfring  unmap_ctx:
1004c19650d6SHoria Geantă 	ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL);
1005045e3678SYuan Kang 	kfree(edesc);
1006045e3678SYuan Kang 	return ret;
1007045e3678SYuan Kang }
1008045e3678SYuan Kang 
1009045e3678SYuan Kang static int ahash_finup_ctx(struct ahash_request *req)
1010045e3678SYuan Kang {
1011045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
10124cb4f7c1SHerbert Xu 	struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
10134cb4f7c1SHerbert Xu 	struct caam_hash_state *state = ahash_request_ctx_dma(req);
1014045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
101546b49abcSAndrei Botila 	int buflen = state->buflen;
101630a43b44SRussell King 	u32 *desc;
101765cf164aSRussell King 	int sec4_sg_src_index;
1018bc13c69eSRussell King 	int src_nents, mapped_nents;
1019045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
1020045e3678SYuan Kang 	struct ahash_edesc *edesc;
10219e6df0fdSMarkus Elfring 	int ret;
1022045e3678SYuan Kang 
102313fb8fd7SLABBE Corentin 	src_nents = sg_nents_for_len(req->src, req->nbytes);
1024f9970c28SLABBE Corentin 	if (src_nents < 0) {
1025f9970c28SLABBE Corentin 		dev_err(jrdev, "Invalid number of src SG.\n");
1026f9970c28SLABBE Corentin 		return src_nents;
1027f9970c28SLABBE Corentin 	}
1028bc13c69eSRussell King 
1029bc13c69eSRussell King 	if (src_nents) {
1030bc13c69eSRussell King 		mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1031bc13c69eSRussell King 					  DMA_TO_DEVICE);
1032bc13c69eSRussell King 		if (!mapped_nents) {
1033bc13c69eSRussell King 			dev_err(jrdev, "unable to DMA map source\n");
1034bc13c69eSRussell King 			return -ENOMEM;
1035bc13c69eSRussell King 		}
1036bc13c69eSRussell King 	} else {
1037bc13c69eSRussell King 		mapped_nents = 0;
1038bc13c69eSRussell King 	}
1039bc13c69eSRussell King 
1040045e3678SYuan Kang 	sec4_sg_src_index = 1 + (buflen ? 1 : 0);
1041045e3678SYuan Kang 
1042045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
10432ba1e798SIuliana Prodan 	edesc = ahash_edesc_alloc(req, sec4_sg_src_index + mapped_nents,
10442ba1e798SIuliana Prodan 				  ctx->sh_desc_fin, ctx->sh_desc_fin_dma);
1045045e3678SYuan Kang 	if (!edesc) {
1046bc13c69eSRussell King 		dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1047045e3678SYuan Kang 		return -ENOMEM;
1048045e3678SYuan Kang 	}
1049045e3678SYuan Kang 
1050045e3678SYuan Kang 	desc = edesc->hw_desc;
1051045e3678SYuan Kang 
1052045e3678SYuan Kang 	edesc->src_nents = src_nents;
1053045e3678SYuan Kang 
1054dfcd8393SHoria Geantă 	ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
1055c19650d6SHoria Geantă 				 edesc->sec4_sg, DMA_BIDIRECTIONAL);
1056ce572085SHoria Geanta 	if (ret)
105758b0e5d0SMarkus Elfring 		goto unmap_ctx;
1058045e3678SYuan Kang 
1059944c3d4dSHoria Geantă 	ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
1060944c3d4dSHoria Geantă 	if (ret)
1061944c3d4dSHoria Geantă 		goto unmap_ctx;
1062045e3678SYuan Kang 
106365cf164aSRussell King 	ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents,
106465cf164aSRussell King 				  sec4_sg_src_index, ctx->ctx_len + buflen,
106565cf164aSRussell King 				  req->nbytes);
106665cf164aSRussell King 	if (ret)
106758b0e5d0SMarkus Elfring 		goto unmap_ctx;
1068045e3678SYuan Kang 
1069c19650d6SHoria Geantă 	append_seq_out_ptr(desc, state->ctx_dma, digestsize, 0);
1070045e3678SYuan Kang 
10716e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
10726e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
10736e005503SSascha Hauer 			     1);
1074045e3678SYuan Kang 
107521b014f0SIuliana Prodan 	return ahash_enqueue_req(jrdev, ahash_done_ctx_src, req,
107621b014f0SIuliana Prodan 				 digestsize, DMA_BIDIRECTIONAL);
107758b0e5d0SMarkus Elfring  unmap_ctx:
1078c19650d6SHoria Geantă 	ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL);
1079045e3678SYuan Kang 	kfree(edesc);
1080045e3678SYuan Kang 	return ret;
1081045e3678SYuan Kang }
1082045e3678SYuan Kang 
1083045e3678SYuan Kang static int ahash_digest(struct ahash_request *req)
1084045e3678SYuan Kang {
1085045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
10864cb4f7c1SHerbert Xu 	struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
10874cb4f7c1SHerbert Xu 	struct caam_hash_state *state = ahash_request_ctx_dma(req);
1088045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
108930a43b44SRussell King 	u32 *desc;
1090045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
109165cf164aSRussell King 	int src_nents, mapped_nents;
1092045e3678SYuan Kang 	struct ahash_edesc *edesc;
10939e6df0fdSMarkus Elfring 	int ret;
1094045e3678SYuan Kang 
1095944c3d4dSHoria Geantă 	state->buf_dma = 0;
1096944c3d4dSHoria Geantă 
10973d5a2db6SRussell King 	src_nents = sg_nents_for_len(req->src, req->nbytes);
1098f9970c28SLABBE Corentin 	if (src_nents < 0) {
1099f9970c28SLABBE Corentin 		dev_err(jrdev, "Invalid number of src SG.\n");
1100f9970c28SLABBE Corentin 		return src_nents;
1101f9970c28SLABBE Corentin 	}
1102bc13c69eSRussell King 
1103bc13c69eSRussell King 	if (src_nents) {
1104bc13c69eSRussell King 		mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1105bc13c69eSRussell King 					  DMA_TO_DEVICE);
1106bc13c69eSRussell King 		if (!mapped_nents) {
1107bc13c69eSRussell King 			dev_err(jrdev, "unable to map source for DMA\n");
1108bc13c69eSRussell King 			return -ENOMEM;
1109bc13c69eSRussell King 		}
1110bc13c69eSRussell King 	} else {
1111bc13c69eSRussell King 		mapped_nents = 0;
1112bc13c69eSRussell King 	}
1113bc13c69eSRussell King 
1114045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
11152ba1e798SIuliana Prodan 	edesc = ahash_edesc_alloc(req, mapped_nents > 1 ? mapped_nents : 0,
11162ba1e798SIuliana Prodan 				  ctx->sh_desc_digest, ctx->sh_desc_digest_dma);
1117045e3678SYuan Kang 	if (!edesc) {
1118bc13c69eSRussell King 		dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1119045e3678SYuan Kang 		return -ENOMEM;
1120045e3678SYuan Kang 	}
1121343e44b1SRussell King 
1122045e3678SYuan Kang 	edesc->src_nents = src_nents;
1123045e3678SYuan Kang 
112465cf164aSRussell King 	ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 0, 0,
112565cf164aSRussell King 				  req->nbytes);
112665cf164aSRussell King 	if (ret) {
112732686d34SRussell King 		ahash_unmap(jrdev, edesc, req, digestsize);
112832686d34SRussell King 		kfree(edesc);
112965cf164aSRussell King 		return ret;
1130ce572085SHoria Geanta 	}
113165cf164aSRussell King 
113265cf164aSRussell King 	desc = edesc->hw_desc;
1133045e3678SYuan Kang 
1134c19650d6SHoria Geantă 	ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
1135c19650d6SHoria Geantă 	if (ret) {
113632686d34SRussell King 		ahash_unmap(jrdev, edesc, req, digestsize);
113732686d34SRussell King 		kfree(edesc);
1138ce572085SHoria Geanta 		return -ENOMEM;
1139ce572085SHoria Geanta 	}
1140045e3678SYuan Kang 
11416e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
11426e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
11436e005503SSascha Hauer 			     1);
1144045e3678SYuan Kang 
114521b014f0SIuliana Prodan 	return ahash_enqueue_req(jrdev, ahash_done, req, digestsize,
114621b014f0SIuliana Prodan 				 DMA_FROM_DEVICE);
1147045e3678SYuan Kang }
1148045e3678SYuan Kang 
1149045e3678SYuan Kang /* submit ahash final if it the first job descriptor */
1150045e3678SYuan Kang static int ahash_final_no_ctx(struct ahash_request *req)
1151045e3678SYuan Kang {
1152045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
11534cb4f7c1SHerbert Xu 	struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
11544cb4f7c1SHerbert Xu 	struct caam_hash_state *state = ahash_request_ctx_dma(req);
1155045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
115646b49abcSAndrei Botila 	u8 *buf = state->buf;
115746b49abcSAndrei Botila 	int buflen = state->buflen;
115830a43b44SRussell King 	u32 *desc;
1159045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
1160045e3678SYuan Kang 	struct ahash_edesc *edesc;
11619e6df0fdSMarkus Elfring 	int ret;
1162045e3678SYuan Kang 
1163045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
11642ba1e798SIuliana Prodan 	edesc = ahash_edesc_alloc(req, 0, ctx->sh_desc_digest,
11652ba1e798SIuliana Prodan 				  ctx->sh_desc_digest_dma);
11665588d039SRussell King 	if (!edesc)
1167045e3678SYuan Kang 		return -ENOMEM;
1168045e3678SYuan Kang 
1169045e3678SYuan Kang 	desc = edesc->hw_desc;
1170045e3678SYuan Kang 
117104e6d25cSAymen Sghaier 	if (buflen) {
117204e6d25cSAymen Sghaier 		state->buf_dma = dma_map_single(jrdev, buf, buflen,
117304e6d25cSAymen Sghaier 						DMA_TO_DEVICE);
1174ce572085SHoria Geanta 		if (dma_mapping_error(jrdev, state->buf_dma)) {
1175ce572085SHoria Geanta 			dev_err(jrdev, "unable to map src\n");
117606435f34SMarkus Elfring 			goto unmap;
1177ce572085SHoria Geanta 		}
1178045e3678SYuan Kang 
1179045e3678SYuan Kang 		append_seq_in_ptr(desc, state->buf_dma, buflen, 0);
118004e6d25cSAymen Sghaier 	}
1181045e3678SYuan Kang 
1182c19650d6SHoria Geantă 	ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
1183c19650d6SHoria Geantă 	if (ret)
118406435f34SMarkus Elfring 		goto unmap;
1185045e3678SYuan Kang 
11866e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
11876e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
11886e005503SSascha Hauer 			     1);
1189045e3678SYuan Kang 
119021b014f0SIuliana Prodan 	return ahash_enqueue_req(jrdev, ahash_done, req,
119121b014f0SIuliana Prodan 				 digestsize, DMA_FROM_DEVICE);
119206435f34SMarkus Elfring  unmap:
119306435f34SMarkus Elfring 	ahash_unmap(jrdev, edesc, req, digestsize);
119406435f34SMarkus Elfring 	kfree(edesc);
119506435f34SMarkus Elfring 	return -ENOMEM;
1196045e3678SYuan Kang }
1197045e3678SYuan Kang 
1198045e3678SYuan Kang /* submit ahash update if it the first job descriptor after update */
1199045e3678SYuan Kang static int ahash_update_no_ctx(struct ahash_request *req)
1200045e3678SYuan Kang {
1201045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
12024cb4f7c1SHerbert Xu 	struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
12034cb4f7c1SHerbert Xu 	struct caam_hash_state *state = ahash_request_ctx_dma(req);
1204045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
120546b49abcSAndrei Botila 	u8 *buf = state->buf;
120646b49abcSAndrei Botila 	int *buflen = &state->buflen;
120746b49abcSAndrei Botila 	int *next_buflen = &state->next_buflen;
120812b8567fSIuliana Prodan 	int blocksize = crypto_ahash_blocksize(ahash);
1209045e3678SYuan Kang 	int in_len = *buflen + req->nbytes, to_hash;
1210bc13c69eSRussell King 	int sec4_sg_bytes, src_nents, mapped_nents;
1211045e3678SYuan Kang 	struct ahash_edesc *edesc;
121230a43b44SRussell King 	u32 *desc;
1213045e3678SYuan Kang 	int ret = 0;
1214045e3678SYuan Kang 
121512b8567fSIuliana Prodan 	*next_buflen = in_len & (blocksize - 1);
1216045e3678SYuan Kang 	to_hash = in_len - *next_buflen;
1217045e3678SYuan Kang 
121812b8567fSIuliana Prodan 	/*
121987870cfbSIuliana Prodan 	 * For XCBC and CMAC, if to_hash is multiple of block size,
122012b8567fSIuliana Prodan 	 * keep last block in internal buffer
122112b8567fSIuliana Prodan 	 */
122287870cfbSIuliana Prodan 	if ((is_xcbc_aes(ctx->adata.algtype) ||
122387870cfbSIuliana Prodan 	     is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize &&
122412b8567fSIuliana Prodan 	     (*next_buflen == 0)) {
122512b8567fSIuliana Prodan 		*next_buflen = blocksize;
122612b8567fSIuliana Prodan 		to_hash -= blocksize;
122712b8567fSIuliana Prodan 	}
122812b8567fSIuliana Prodan 
1229045e3678SYuan Kang 	if (to_hash) {
1230a5e5c133SHoria Geantă 		int pad_nents;
1231059d73eeSHoria Geantă 		int src_len = req->nbytes - *next_buflen;
1232a5e5c133SHoria Geantă 
1233059d73eeSHoria Geantă 		src_nents = sg_nents_for_len(req->src, src_len);
1234f9970c28SLABBE Corentin 		if (src_nents < 0) {
1235f9970c28SLABBE Corentin 			dev_err(jrdev, "Invalid number of src SG.\n");
1236f9970c28SLABBE Corentin 			return src_nents;
1237f9970c28SLABBE Corentin 		}
1238bc13c69eSRussell King 
1239bc13c69eSRussell King 		if (src_nents) {
1240bc13c69eSRussell King 			mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1241bc13c69eSRussell King 						  DMA_TO_DEVICE);
1242bc13c69eSRussell King 			if (!mapped_nents) {
1243bc13c69eSRussell King 				dev_err(jrdev, "unable to DMA map source\n");
1244bc13c69eSRussell King 				return -ENOMEM;
1245bc13c69eSRussell King 			}
1246bc13c69eSRussell King 		} else {
1247bc13c69eSRussell King 			mapped_nents = 0;
1248bc13c69eSRussell King 		}
1249bc13c69eSRussell King 
1250a5e5c133SHoria Geantă 		pad_nents = pad_sg_nents(1 + mapped_nents);
1251a5e5c133SHoria Geantă 		sec4_sg_bytes = pad_nents * sizeof(struct sec4_sg_entry);
1252045e3678SYuan Kang 
1253045e3678SYuan Kang 		/*
1254045e3678SYuan Kang 		 * allocate space for base edesc and hw desc commands,
1255045e3678SYuan Kang 		 * link tables
1256045e3678SYuan Kang 		 */
12572ba1e798SIuliana Prodan 		edesc = ahash_edesc_alloc(req, pad_nents,
125830a43b44SRussell King 					  ctx->sh_desc_update_first,
12592ba1e798SIuliana Prodan 					  ctx->sh_desc_update_first_dma);
1260045e3678SYuan Kang 		if (!edesc) {
1261bc13c69eSRussell King 			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1262045e3678SYuan Kang 			return -ENOMEM;
1263045e3678SYuan Kang 		}
1264045e3678SYuan Kang 
1265045e3678SYuan Kang 		edesc->src_nents = src_nents;
1266045e3678SYuan Kang 		edesc->sec4_sg_bytes = sec4_sg_bytes;
1267045e3678SYuan Kang 
1268944c3d4dSHoria Geantă 		ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg, state);
1269944c3d4dSHoria Geantă 		if (ret)
1270944c3d4dSHoria Geantă 			goto unmap_ctx;
1271944c3d4dSHoria Geantă 
1272059d73eeSHoria Geantă 		sg_to_sec4_sg_last(req->src, src_len, edesc->sec4_sg + 1, 0);
1273bc13c69eSRussell King 
1274045e3678SYuan Kang 		desc = edesc->hw_desc;
1275045e3678SYuan Kang 
12761da2be33SRuchika Gupta 		edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
12771da2be33SRuchika Gupta 						    sec4_sg_bytes,
12781da2be33SRuchika Gupta 						    DMA_TO_DEVICE);
1279ce572085SHoria Geanta 		if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
1280ce572085SHoria Geanta 			dev_err(jrdev, "unable to map S/G table\n");
128132686d34SRussell King 			ret = -ENOMEM;
128258b0e5d0SMarkus Elfring 			goto unmap_ctx;
1283ce572085SHoria Geanta 		}
12841da2be33SRuchika Gupta 
1285045e3678SYuan Kang 		append_seq_in_ptr(desc, edesc->sec4_sg_dma, to_hash, LDST_SGF);
1286045e3678SYuan Kang 
1287ce572085SHoria Geanta 		ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len);
1288ce572085SHoria Geanta 		if (ret)
128958b0e5d0SMarkus Elfring 			goto unmap_ctx;
1290045e3678SYuan Kang 
12916e005503SSascha Hauer 		print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
1292045e3678SYuan Kang 				     DUMP_PREFIX_ADDRESS, 16, 4, desc,
1293045e3678SYuan Kang 				     desc_bytes(desc), 1);
1294045e3678SYuan Kang 
129521b014f0SIuliana Prodan 		ret = ahash_enqueue_req(jrdev, ahash_done_ctx_dst, req,
129621b014f0SIuliana Prodan 					ctx->ctx_len, DMA_TO_DEVICE);
129721b014f0SIuliana Prodan 		if ((ret != -EINPROGRESS) && (ret != -EBUSY))
129821b014f0SIuliana Prodan 			return ret;
1299045e3678SYuan Kang 		state->update = ahash_update_ctx;
1300045e3678SYuan Kang 		state->finup = ahash_finup_ctx;
1301045e3678SYuan Kang 		state->final = ahash_final_ctx;
1302045e3678SYuan Kang 	} else if (*next_buflen) {
1303307fd543SCristian Stoica 		scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
1304307fd543SCristian Stoica 					 req->nbytes, 0);
1305045e3678SYuan Kang 		*buflen = *next_buflen;
13066e005503SSascha Hauer 
13076e005503SSascha Hauer 		print_hex_dump_debug("buf@" __stringify(__LINE__)": ",
130846b49abcSAndrei Botila 				     DUMP_PREFIX_ADDRESS, 16, 4, buf,
130946b49abcSAndrei Botila 				     *buflen, 1);
131046b49abcSAndrei Botila 	}
1311045e3678SYuan Kang 
1312045e3678SYuan Kang 	return ret;
131358b0e5d0SMarkus Elfring  unmap_ctx:
131432686d34SRussell King 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE);
131532686d34SRussell King 	kfree(edesc);
131632686d34SRussell King 	return ret;
1317045e3678SYuan Kang }
1318045e3678SYuan Kang 
1319045e3678SYuan Kang /* submit ahash finup if it the first job descriptor after update */
1320045e3678SYuan Kang static int ahash_finup_no_ctx(struct ahash_request *req)
1321045e3678SYuan Kang {
1322045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
13234cb4f7c1SHerbert Xu 	struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
13244cb4f7c1SHerbert Xu 	struct caam_hash_state *state = ahash_request_ctx_dma(req);
1325045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
132646b49abcSAndrei Botila 	int buflen = state->buflen;
132730a43b44SRussell King 	u32 *desc;
1328bc13c69eSRussell King 	int sec4_sg_bytes, sec4_sg_src_index, src_nents, mapped_nents;
1329045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
1330045e3678SYuan Kang 	struct ahash_edesc *edesc;
13319e6df0fdSMarkus Elfring 	int ret;
1332045e3678SYuan Kang 
133313fb8fd7SLABBE Corentin 	src_nents = sg_nents_for_len(req->src, req->nbytes);
1334f9970c28SLABBE Corentin 	if (src_nents < 0) {
1335f9970c28SLABBE Corentin 		dev_err(jrdev, "Invalid number of src SG.\n");
1336f9970c28SLABBE Corentin 		return src_nents;
1337f9970c28SLABBE Corentin 	}
1338bc13c69eSRussell King 
1339bc13c69eSRussell King 	if (src_nents) {
1340bc13c69eSRussell King 		mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1341bc13c69eSRussell King 					  DMA_TO_DEVICE);
1342bc13c69eSRussell King 		if (!mapped_nents) {
1343bc13c69eSRussell King 			dev_err(jrdev, "unable to DMA map source\n");
1344bc13c69eSRussell King 			return -ENOMEM;
1345bc13c69eSRussell King 		}
1346bc13c69eSRussell King 	} else {
1347bc13c69eSRussell King 		mapped_nents = 0;
1348bc13c69eSRussell King 	}
1349bc13c69eSRussell King 
1350045e3678SYuan Kang 	sec4_sg_src_index = 2;
1351bc13c69eSRussell King 	sec4_sg_bytes = (sec4_sg_src_index + mapped_nents) *
1352045e3678SYuan Kang 			 sizeof(struct sec4_sg_entry);
1353045e3678SYuan Kang 
1354045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
13552ba1e798SIuliana Prodan 	edesc = ahash_edesc_alloc(req, sec4_sg_src_index + mapped_nents,
13562ba1e798SIuliana Prodan 				  ctx->sh_desc_digest, ctx->sh_desc_digest_dma);
1357045e3678SYuan Kang 	if (!edesc) {
1358bc13c69eSRussell King 		dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1359045e3678SYuan Kang 		return -ENOMEM;
1360045e3678SYuan Kang 	}
1361045e3678SYuan Kang 
1362045e3678SYuan Kang 	desc = edesc->hw_desc;
1363045e3678SYuan Kang 
1364045e3678SYuan Kang 	edesc->src_nents = src_nents;
1365045e3678SYuan Kang 	edesc->sec4_sg_bytes = sec4_sg_bytes;
1366045e3678SYuan Kang 
1367944c3d4dSHoria Geantă 	ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg, state);
1368944c3d4dSHoria Geantă 	if (ret)
1369944c3d4dSHoria Geantă 		goto unmap;
1370045e3678SYuan Kang 
137165cf164aSRussell King 	ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 1, buflen,
137265cf164aSRussell King 				  req->nbytes);
137365cf164aSRussell King 	if (ret) {
1374ce572085SHoria Geanta 		dev_err(jrdev, "unable to map S/G table\n");
137506435f34SMarkus Elfring 		goto unmap;
1376ce572085SHoria Geanta 	}
13771da2be33SRuchika Gupta 
1378c19650d6SHoria Geantă 	ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
1379c19650d6SHoria Geantă 	if (ret)
138006435f34SMarkus Elfring 		goto unmap;
1381045e3678SYuan Kang 
13826e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
13836e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
13846e005503SSascha Hauer 			     1);
1385045e3678SYuan Kang 
138621b014f0SIuliana Prodan 	return ahash_enqueue_req(jrdev, ahash_done, req,
138721b014f0SIuliana Prodan 				 digestsize, DMA_FROM_DEVICE);
138806435f34SMarkus Elfring  unmap:
138906435f34SMarkus Elfring 	ahash_unmap(jrdev, edesc, req, digestsize);
139006435f34SMarkus Elfring 	kfree(edesc);
139106435f34SMarkus Elfring 	return -ENOMEM;
139206435f34SMarkus Elfring 
1393045e3678SYuan Kang }
1394045e3678SYuan Kang 
1395045e3678SYuan Kang /* submit first update job descriptor after init */
1396045e3678SYuan Kang static int ahash_update_first(struct ahash_request *req)
1397045e3678SYuan Kang {
1398045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
13994cb4f7c1SHerbert Xu 	struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
14004cb4f7c1SHerbert Xu 	struct caam_hash_state *state = ahash_request_ctx_dma(req);
1401045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
140246b49abcSAndrei Botila 	u8 *buf = state->buf;
140346b49abcSAndrei Botila 	int *buflen = &state->buflen;
140446b49abcSAndrei Botila 	int *next_buflen = &state->next_buflen;
1405045e3678SYuan Kang 	int to_hash;
140612b8567fSIuliana Prodan 	int blocksize = crypto_ahash_blocksize(ahash);
140730a43b44SRussell King 	u32 *desc;
140865cf164aSRussell King 	int src_nents, mapped_nents;
1409045e3678SYuan Kang 	struct ahash_edesc *edesc;
1410045e3678SYuan Kang 	int ret = 0;
1411045e3678SYuan Kang 
141212b8567fSIuliana Prodan 	*next_buflen = req->nbytes & (blocksize - 1);
1413045e3678SYuan Kang 	to_hash = req->nbytes - *next_buflen;
1414045e3678SYuan Kang 
141512b8567fSIuliana Prodan 	/*
141687870cfbSIuliana Prodan 	 * For XCBC and CMAC, if to_hash is multiple of block size,
141712b8567fSIuliana Prodan 	 * keep last block in internal buffer
141812b8567fSIuliana Prodan 	 */
141987870cfbSIuliana Prodan 	if ((is_xcbc_aes(ctx->adata.algtype) ||
142087870cfbSIuliana Prodan 	     is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize &&
142112b8567fSIuliana Prodan 	     (*next_buflen == 0)) {
142212b8567fSIuliana Prodan 		*next_buflen = blocksize;
142312b8567fSIuliana Prodan 		to_hash -= blocksize;
142412b8567fSIuliana Prodan 	}
142512b8567fSIuliana Prodan 
1426045e3678SYuan Kang 	if (to_hash) {
14273d5a2db6SRussell King 		src_nents = sg_nents_for_len(req->src,
14283d5a2db6SRussell King 					     req->nbytes - *next_buflen);
1429f9970c28SLABBE Corentin 		if (src_nents < 0) {
1430f9970c28SLABBE Corentin 			dev_err(jrdev, "Invalid number of src SG.\n");
1431f9970c28SLABBE Corentin 			return src_nents;
1432f9970c28SLABBE Corentin 		}
1433bc13c69eSRussell King 
1434bc13c69eSRussell King 		if (src_nents) {
1435bc13c69eSRussell King 			mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1436bc13c69eSRussell King 						  DMA_TO_DEVICE);
1437bc13c69eSRussell King 			if (!mapped_nents) {
1438bc13c69eSRussell King 				dev_err(jrdev, "unable to map source for DMA\n");
1439bc13c69eSRussell King 				return -ENOMEM;
1440bc13c69eSRussell King 			}
1441bc13c69eSRussell King 		} else {
1442bc13c69eSRussell King 			mapped_nents = 0;
1443bc13c69eSRussell King 		}
1444045e3678SYuan Kang 
1445045e3678SYuan Kang 		/*
1446045e3678SYuan Kang 		 * allocate space for base edesc and hw desc commands,
1447045e3678SYuan Kang 		 * link tables
1448045e3678SYuan Kang 		 */
14492ba1e798SIuliana Prodan 		edesc = ahash_edesc_alloc(req, mapped_nents > 1 ?
145030a43b44SRussell King 					  mapped_nents : 0,
145130a43b44SRussell King 					  ctx->sh_desc_update_first,
14522ba1e798SIuliana Prodan 					  ctx->sh_desc_update_first_dma);
1453045e3678SYuan Kang 		if (!edesc) {
1454bc13c69eSRussell King 			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1455045e3678SYuan Kang 			return -ENOMEM;
1456045e3678SYuan Kang 		}
1457045e3678SYuan Kang 
1458045e3678SYuan Kang 		edesc->src_nents = src_nents;
1459045e3678SYuan Kang 
146065cf164aSRussell King 		ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 0, 0,
146165cf164aSRussell King 					  to_hash);
146265cf164aSRussell King 		if (ret)
146358b0e5d0SMarkus Elfring 			goto unmap_ctx;
1464045e3678SYuan Kang 
1465045e3678SYuan Kang 		desc = edesc->hw_desc;
1466045e3678SYuan Kang 
1467ce572085SHoria Geanta 		ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len);
1468ce572085SHoria Geanta 		if (ret)
146958b0e5d0SMarkus Elfring 			goto unmap_ctx;
1470045e3678SYuan Kang 
14716e005503SSascha Hauer 		print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
1472045e3678SYuan Kang 				     DUMP_PREFIX_ADDRESS, 16, 4, desc,
1473045e3678SYuan Kang 				     desc_bytes(desc), 1);
1474045e3678SYuan Kang 
147521b014f0SIuliana Prodan 		ret = ahash_enqueue_req(jrdev, ahash_done_ctx_dst, req,
147621b014f0SIuliana Prodan 					ctx->ctx_len, DMA_TO_DEVICE);
147721b014f0SIuliana Prodan 		if ((ret != -EINPROGRESS) && (ret != -EBUSY))
147821b014f0SIuliana Prodan 			return ret;
1479045e3678SYuan Kang 		state->update = ahash_update_ctx;
1480045e3678SYuan Kang 		state->finup = ahash_finup_ctx;
1481045e3678SYuan Kang 		state->final = ahash_final_ctx;
1482045e3678SYuan Kang 	} else if (*next_buflen) {
1483045e3678SYuan Kang 		state->update = ahash_update_no_ctx;
1484045e3678SYuan Kang 		state->finup = ahash_finup_no_ctx;
1485045e3678SYuan Kang 		state->final = ahash_final_no_ctx;
148646b49abcSAndrei Botila 		scatterwalk_map_and_copy(buf, req->src, 0,
1487307fd543SCristian Stoica 					 req->nbytes, 0);
148846b49abcSAndrei Botila 		*buflen = *next_buflen;
14896e005503SSascha Hauer 
149046b49abcSAndrei Botila 		print_hex_dump_debug("buf@" __stringify(__LINE__)": ",
149146b49abcSAndrei Botila 				     DUMP_PREFIX_ADDRESS, 16, 4, buf,
149246b49abcSAndrei Botila 				     *buflen, 1);
149346b49abcSAndrei Botila 	}
1494045e3678SYuan Kang 
1495045e3678SYuan Kang 	return ret;
149658b0e5d0SMarkus Elfring  unmap_ctx:
149732686d34SRussell King 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE);
149832686d34SRussell King 	kfree(edesc);
149932686d34SRussell King 	return ret;
1500045e3678SYuan Kang }
1501045e3678SYuan Kang 
1502045e3678SYuan Kang static int ahash_finup_first(struct ahash_request *req)
1503045e3678SYuan Kang {
1504045e3678SYuan Kang 	return ahash_digest(req);
1505045e3678SYuan Kang }
1506045e3678SYuan Kang 
1507045e3678SYuan Kang static int ahash_init(struct ahash_request *req)
1508045e3678SYuan Kang {
15094cb4f7c1SHerbert Xu 	struct caam_hash_state *state = ahash_request_ctx_dma(req);
1510045e3678SYuan Kang 
1511045e3678SYuan Kang 	state->update = ahash_update_first;
1512045e3678SYuan Kang 	state->finup = ahash_finup_first;
1513045e3678SYuan Kang 	state->final = ahash_final_no_ctx;
1514045e3678SYuan Kang 
151587ec02e7SHoria Geantă 	state->ctx_dma = 0;
151665055e21SFranck LENORMAND 	state->ctx_dma_len = 0;
1517de0e35ecSHoria Geanta 	state->buf_dma = 0;
151846b49abcSAndrei Botila 	state->buflen = 0;
151946b49abcSAndrei Botila 	state->next_buflen = 0;
1520045e3678SYuan Kang 
1521045e3678SYuan Kang 	return 0;
1522045e3678SYuan Kang }
1523045e3678SYuan Kang 
1524045e3678SYuan Kang static int ahash_update(struct ahash_request *req)
1525045e3678SYuan Kang {
15264cb4f7c1SHerbert Xu 	struct caam_hash_state *state = ahash_request_ctx_dma(req);
1527045e3678SYuan Kang 
1528045e3678SYuan Kang 	return state->update(req);
1529045e3678SYuan Kang }
1530045e3678SYuan Kang 
1531045e3678SYuan Kang static int ahash_finup(struct ahash_request *req)
1532045e3678SYuan Kang {
15334cb4f7c1SHerbert Xu 	struct caam_hash_state *state = ahash_request_ctx_dma(req);
1534045e3678SYuan Kang 
1535045e3678SYuan Kang 	return state->finup(req);
1536045e3678SYuan Kang }
1537045e3678SYuan Kang 
1538045e3678SYuan Kang static int ahash_final(struct ahash_request *req)
1539045e3678SYuan Kang {
15404cb4f7c1SHerbert Xu 	struct caam_hash_state *state = ahash_request_ctx_dma(req);
1541045e3678SYuan Kang 
1542045e3678SYuan Kang 	return state->final(req);
1543045e3678SYuan Kang }
1544045e3678SYuan Kang 
1545045e3678SYuan Kang static int ahash_export(struct ahash_request *req, void *out)
1546045e3678SYuan Kang {
15474cb4f7c1SHerbert Xu 	struct caam_hash_state *state = ahash_request_ctx_dma(req);
15485ec90831SRussell King 	struct caam_export_state *export = out;
154946b49abcSAndrei Botila 	u8 *buf = state->buf;
155046b49abcSAndrei Botila 	int len = state->buflen;
15515ec90831SRussell King 
15525ec90831SRussell King 	memcpy(export->buf, buf, len);
15535ec90831SRussell King 	memcpy(export->caam_ctx, state->caam_ctx, sizeof(export->caam_ctx));
15545ec90831SRussell King 	export->buflen = len;
15555ec90831SRussell King 	export->update = state->update;
15565ec90831SRussell King 	export->final = state->final;
15575ec90831SRussell King 	export->finup = state->finup;
1558434b4212SRussell King 
1559045e3678SYuan Kang 	return 0;
1560045e3678SYuan Kang }
1561045e3678SYuan Kang 
1562045e3678SYuan Kang static int ahash_import(struct ahash_request *req, const void *in)
1563045e3678SYuan Kang {
15644cb4f7c1SHerbert Xu 	struct caam_hash_state *state = ahash_request_ctx_dma(req);
15655ec90831SRussell King 	const struct caam_export_state *export = in;
1566045e3678SYuan Kang 
15675ec90831SRussell King 	memset(state, 0, sizeof(*state));
156846b49abcSAndrei Botila 	memcpy(state->buf, export->buf, export->buflen);
15695ec90831SRussell King 	memcpy(state->caam_ctx, export->caam_ctx, sizeof(state->caam_ctx));
157046b49abcSAndrei Botila 	state->buflen = export->buflen;
15715ec90831SRussell King 	state->update = export->update;
15725ec90831SRussell King 	state->final = export->final;
15735ec90831SRussell King 	state->finup = export->finup;
1574434b4212SRussell King 
1575045e3678SYuan Kang 	return 0;
1576045e3678SYuan Kang }
1577045e3678SYuan Kang 
1578045e3678SYuan Kang struct caam_hash_template {
1579045e3678SYuan Kang 	char name[CRYPTO_MAX_ALG_NAME];
1580045e3678SYuan Kang 	char driver_name[CRYPTO_MAX_ALG_NAME];
1581b0e09baeSYuan Kang 	char hmac_name[CRYPTO_MAX_ALG_NAME];
1582b0e09baeSYuan Kang 	char hmac_driver_name[CRYPTO_MAX_ALG_NAME];
1583045e3678SYuan Kang 	unsigned int blocksize;
1584045e3678SYuan Kang 	struct ahash_alg template_ahash;
1585045e3678SYuan Kang 	u32 alg_type;
1586045e3678SYuan Kang };
1587045e3678SYuan Kang 
1588045e3678SYuan Kang /* ahash descriptors */
1589045e3678SYuan Kang static struct caam_hash_template driver_hash[] = {
1590045e3678SYuan Kang 	{
1591b0e09baeSYuan Kang 		.name = "sha1",
1592b0e09baeSYuan Kang 		.driver_name = "sha1-caam",
1593b0e09baeSYuan Kang 		.hmac_name = "hmac(sha1)",
1594b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha1-caam",
1595045e3678SYuan Kang 		.blocksize = SHA1_BLOCK_SIZE,
1596045e3678SYuan Kang 		.template_ahash = {
1597045e3678SYuan Kang 			.init = ahash_init,
1598045e3678SYuan Kang 			.update = ahash_update,
1599045e3678SYuan Kang 			.final = ahash_final,
1600045e3678SYuan Kang 			.finup = ahash_finup,
1601045e3678SYuan Kang 			.digest = ahash_digest,
1602045e3678SYuan Kang 			.export = ahash_export,
1603045e3678SYuan Kang 			.import = ahash_import,
1604045e3678SYuan Kang 			.setkey = ahash_setkey,
1605045e3678SYuan Kang 			.halg = {
1606045e3678SYuan Kang 				.digestsize = SHA1_DIGEST_SIZE,
16075ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1608045e3678SYuan Kang 			},
1609045e3678SYuan Kang 		},
1610045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA1,
1611045e3678SYuan Kang 	}, {
1612b0e09baeSYuan Kang 		.name = "sha224",
1613b0e09baeSYuan Kang 		.driver_name = "sha224-caam",
1614b0e09baeSYuan Kang 		.hmac_name = "hmac(sha224)",
1615b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha224-caam",
1616045e3678SYuan Kang 		.blocksize = SHA224_BLOCK_SIZE,
1617045e3678SYuan Kang 		.template_ahash = {
1618045e3678SYuan Kang 			.init = ahash_init,
1619045e3678SYuan Kang 			.update = ahash_update,
1620045e3678SYuan Kang 			.final = ahash_final,
1621045e3678SYuan Kang 			.finup = ahash_finup,
1622045e3678SYuan Kang 			.digest = ahash_digest,
1623045e3678SYuan Kang 			.export = ahash_export,
1624045e3678SYuan Kang 			.import = ahash_import,
1625045e3678SYuan Kang 			.setkey = ahash_setkey,
1626045e3678SYuan Kang 			.halg = {
1627045e3678SYuan Kang 				.digestsize = SHA224_DIGEST_SIZE,
16285ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1629045e3678SYuan Kang 			},
1630045e3678SYuan Kang 		},
1631045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA224,
1632045e3678SYuan Kang 	}, {
1633b0e09baeSYuan Kang 		.name = "sha256",
1634b0e09baeSYuan Kang 		.driver_name = "sha256-caam",
1635b0e09baeSYuan Kang 		.hmac_name = "hmac(sha256)",
1636b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha256-caam",
1637045e3678SYuan Kang 		.blocksize = SHA256_BLOCK_SIZE,
1638045e3678SYuan Kang 		.template_ahash = {
1639045e3678SYuan Kang 			.init = ahash_init,
1640045e3678SYuan Kang 			.update = ahash_update,
1641045e3678SYuan Kang 			.final = ahash_final,
1642045e3678SYuan Kang 			.finup = ahash_finup,
1643045e3678SYuan Kang 			.digest = ahash_digest,
1644045e3678SYuan Kang 			.export = ahash_export,
1645045e3678SYuan Kang 			.import = ahash_import,
1646045e3678SYuan Kang 			.setkey = ahash_setkey,
1647045e3678SYuan Kang 			.halg = {
1648045e3678SYuan Kang 				.digestsize = SHA256_DIGEST_SIZE,
16495ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1650045e3678SYuan Kang 			},
1651045e3678SYuan Kang 		},
1652045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA256,
1653045e3678SYuan Kang 	}, {
1654b0e09baeSYuan Kang 		.name = "sha384",
1655b0e09baeSYuan Kang 		.driver_name = "sha384-caam",
1656b0e09baeSYuan Kang 		.hmac_name = "hmac(sha384)",
1657b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha384-caam",
1658045e3678SYuan Kang 		.blocksize = SHA384_BLOCK_SIZE,
1659045e3678SYuan Kang 		.template_ahash = {
1660045e3678SYuan Kang 			.init = ahash_init,
1661045e3678SYuan Kang 			.update = ahash_update,
1662045e3678SYuan Kang 			.final = ahash_final,
1663045e3678SYuan Kang 			.finup = ahash_finup,
1664045e3678SYuan Kang 			.digest = ahash_digest,
1665045e3678SYuan Kang 			.export = ahash_export,
1666045e3678SYuan Kang 			.import = ahash_import,
1667045e3678SYuan Kang 			.setkey = ahash_setkey,
1668045e3678SYuan Kang 			.halg = {
1669045e3678SYuan Kang 				.digestsize = SHA384_DIGEST_SIZE,
16705ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1671045e3678SYuan Kang 			},
1672045e3678SYuan Kang 		},
1673045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA384,
1674045e3678SYuan Kang 	}, {
1675b0e09baeSYuan Kang 		.name = "sha512",
1676b0e09baeSYuan Kang 		.driver_name = "sha512-caam",
1677b0e09baeSYuan Kang 		.hmac_name = "hmac(sha512)",
1678b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha512-caam",
1679045e3678SYuan Kang 		.blocksize = SHA512_BLOCK_SIZE,
1680045e3678SYuan Kang 		.template_ahash = {
1681045e3678SYuan Kang 			.init = ahash_init,
1682045e3678SYuan Kang 			.update = ahash_update,
1683045e3678SYuan Kang 			.final = ahash_final,
1684045e3678SYuan Kang 			.finup = ahash_finup,
1685045e3678SYuan Kang 			.digest = ahash_digest,
1686045e3678SYuan Kang 			.export = ahash_export,
1687045e3678SYuan Kang 			.import = ahash_import,
1688045e3678SYuan Kang 			.setkey = ahash_setkey,
1689045e3678SYuan Kang 			.halg = {
1690045e3678SYuan Kang 				.digestsize = SHA512_DIGEST_SIZE,
16915ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1692045e3678SYuan Kang 			},
1693045e3678SYuan Kang 		},
1694045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA512,
1695045e3678SYuan Kang 	}, {
1696b0e09baeSYuan Kang 		.name = "md5",
1697b0e09baeSYuan Kang 		.driver_name = "md5-caam",
1698b0e09baeSYuan Kang 		.hmac_name = "hmac(md5)",
1699b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-md5-caam",
1700045e3678SYuan Kang 		.blocksize = MD5_BLOCK_WORDS * 4,
1701045e3678SYuan Kang 		.template_ahash = {
1702045e3678SYuan Kang 			.init = ahash_init,
1703045e3678SYuan Kang 			.update = ahash_update,
1704045e3678SYuan Kang 			.final = ahash_final,
1705045e3678SYuan Kang 			.finup = ahash_finup,
1706045e3678SYuan Kang 			.digest = ahash_digest,
1707045e3678SYuan Kang 			.export = ahash_export,
1708045e3678SYuan Kang 			.import = ahash_import,
1709045e3678SYuan Kang 			.setkey = ahash_setkey,
1710045e3678SYuan Kang 			.halg = {
1711045e3678SYuan Kang 				.digestsize = MD5_DIGEST_SIZE,
17125ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1713045e3678SYuan Kang 			},
1714045e3678SYuan Kang 		},
1715045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_MD5,
171612b8567fSIuliana Prodan 	}, {
171712b8567fSIuliana Prodan 		.hmac_name = "xcbc(aes)",
171812b8567fSIuliana Prodan 		.hmac_driver_name = "xcbc-aes-caam",
171912b8567fSIuliana Prodan 		.blocksize = AES_BLOCK_SIZE,
172012b8567fSIuliana Prodan 		.template_ahash = {
172112b8567fSIuliana Prodan 			.init = ahash_init,
172212b8567fSIuliana Prodan 			.update = ahash_update,
172312b8567fSIuliana Prodan 			.final = ahash_final,
172412b8567fSIuliana Prodan 			.finup = ahash_finup,
172512b8567fSIuliana Prodan 			.digest = ahash_digest,
172612b8567fSIuliana Prodan 			.export = ahash_export,
172712b8567fSIuliana Prodan 			.import = ahash_import,
172812b8567fSIuliana Prodan 			.setkey = axcbc_setkey,
172912b8567fSIuliana Prodan 			.halg = {
173012b8567fSIuliana Prodan 				.digestsize = AES_BLOCK_SIZE,
173112b8567fSIuliana Prodan 				.statesize = sizeof(struct caam_export_state),
173212b8567fSIuliana Prodan 			},
173312b8567fSIuliana Prodan 		 },
173412b8567fSIuliana Prodan 		.alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_XCBC_MAC,
173587870cfbSIuliana Prodan 	}, {
173687870cfbSIuliana Prodan 		.hmac_name = "cmac(aes)",
173787870cfbSIuliana Prodan 		.hmac_driver_name = "cmac-aes-caam",
173887870cfbSIuliana Prodan 		.blocksize = AES_BLOCK_SIZE,
173987870cfbSIuliana Prodan 		.template_ahash = {
174087870cfbSIuliana Prodan 			.init = ahash_init,
174187870cfbSIuliana Prodan 			.update = ahash_update,
174287870cfbSIuliana Prodan 			.final = ahash_final,
174387870cfbSIuliana Prodan 			.finup = ahash_finup,
174487870cfbSIuliana Prodan 			.digest = ahash_digest,
174587870cfbSIuliana Prodan 			.export = ahash_export,
174687870cfbSIuliana Prodan 			.import = ahash_import,
174787870cfbSIuliana Prodan 			.setkey = acmac_setkey,
174887870cfbSIuliana Prodan 			.halg = {
174987870cfbSIuliana Prodan 				.digestsize = AES_BLOCK_SIZE,
175087870cfbSIuliana Prodan 				.statesize = sizeof(struct caam_export_state),
175187870cfbSIuliana Prodan 			},
175287870cfbSIuliana Prodan 		 },
175387870cfbSIuliana Prodan 		.alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CMAC,
1754045e3678SYuan Kang 	},
1755045e3678SYuan Kang };
1756045e3678SYuan Kang 
1757045e3678SYuan Kang struct caam_hash_alg {
1758045e3678SYuan Kang 	struct list_head entry;
1759045e3678SYuan Kang 	int alg_type;
1760045e3678SYuan Kang 	struct ahash_alg ahash_alg;
1761045e3678SYuan Kang };
1762045e3678SYuan Kang 
1763045e3678SYuan Kang static int caam_hash_cra_init(struct crypto_tfm *tfm)
1764045e3678SYuan Kang {
1765045e3678SYuan Kang 	struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
1766045e3678SYuan Kang 	struct crypto_alg *base = tfm->__crt_alg;
1767045e3678SYuan Kang 	struct hash_alg_common *halg =
1768045e3678SYuan Kang 		 container_of(base, struct hash_alg_common, base);
1769045e3678SYuan Kang 	struct ahash_alg *alg =
1770045e3678SYuan Kang 		 container_of(halg, struct ahash_alg, halg);
1771045e3678SYuan Kang 	struct caam_hash_alg *caam_hash =
1772045e3678SYuan Kang 		 container_of(alg, struct caam_hash_alg, ahash_alg);
17734cb4f7c1SHerbert Xu 	struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
1774045e3678SYuan Kang 	/* Sizes for MDHA running digests: MD5, SHA1, 224, 256, 384, 512 */
1775045e3678SYuan Kang 	static const u8 runninglen[] = { HASH_MSG_LEN + MD5_DIGEST_SIZE,
1776045e3678SYuan Kang 					 HASH_MSG_LEN + SHA1_DIGEST_SIZE,
1777045e3678SYuan Kang 					 HASH_MSG_LEN + 32,
1778045e3678SYuan Kang 					 HASH_MSG_LEN + SHA256_DIGEST_SIZE,
1779045e3678SYuan Kang 					 HASH_MSG_LEN + 64,
1780045e3678SYuan Kang 					 HASH_MSG_LEN + SHA512_DIGEST_SIZE };
178121b014f0SIuliana Prodan 	const size_t sh_desc_update_offset = offsetof(struct caam_hash_ctx,
178221b014f0SIuliana Prodan 						      sh_desc_update);
1783bbf22344SHoria Geantă 	dma_addr_t dma_addr;
17847e0880b9SHoria Geantă 	struct caam_drv_private *priv;
1785045e3678SYuan Kang 
1786045e3678SYuan Kang 	/*
1787cfc6f11bSRuchika Gupta 	 * Get a Job ring from Job Ring driver to ensure in-order
1788045e3678SYuan Kang 	 * crypto request processing per tfm
1789045e3678SYuan Kang 	 */
1790cfc6f11bSRuchika Gupta 	ctx->jrdev = caam_jr_alloc();
1791cfc6f11bSRuchika Gupta 	if (IS_ERR(ctx->jrdev)) {
1792cfc6f11bSRuchika Gupta 		pr_err("Job Ring Device allocation for transform failed\n");
1793cfc6f11bSRuchika Gupta 		return PTR_ERR(ctx->jrdev);
1794cfc6f11bSRuchika Gupta 	}
1795bbf22344SHoria Geantă 
17967e0880b9SHoria Geantă 	priv = dev_get_drvdata(ctx->jrdev->parent);
179712b8567fSIuliana Prodan 
179812b8567fSIuliana Prodan 	if (is_xcbc_aes(caam_hash->alg_type)) {
179912b8567fSIuliana Prodan 		ctx->dir = DMA_TO_DEVICE;
1800e9b4913aSHoria Geantă 		ctx->key_dir = DMA_BIDIRECTIONAL;
180112b8567fSIuliana Prodan 		ctx->adata.algtype = OP_TYPE_CLASS1_ALG | caam_hash->alg_type;
180212b8567fSIuliana Prodan 		ctx->ctx_len = 48;
1803e9b4913aSHoria Geantă 	} else if (is_cmac_aes(caam_hash->alg_type)) {
1804e9b4913aSHoria Geantă 		ctx->dir = DMA_TO_DEVICE;
1805e9b4913aSHoria Geantă 		ctx->key_dir = DMA_NONE;
1806e9b4913aSHoria Geantă 		ctx->adata.algtype = OP_TYPE_CLASS1_ALG | caam_hash->alg_type;
1807e9b4913aSHoria Geantă 		ctx->ctx_len = 32;
1808e9b4913aSHoria Geantă 	} else {
1809e9b4913aSHoria Geantă 		if (priv->era >= 6) {
1810e9b4913aSHoria Geantă 			ctx->dir = DMA_BIDIRECTIONAL;
1811e9b4913aSHoria Geantă 			ctx->key_dir = alg->setkey ? DMA_TO_DEVICE : DMA_NONE;
1812e9b4913aSHoria Geantă 		} else {
1813e9b4913aSHoria Geantă 			ctx->dir = DMA_TO_DEVICE;
1814e9b4913aSHoria Geantă 			ctx->key_dir = DMA_NONE;
1815e9b4913aSHoria Geantă 		}
1816e9b4913aSHoria Geantă 		ctx->adata.algtype = OP_TYPE_CLASS2_ALG | caam_hash->alg_type;
1817e9b4913aSHoria Geantă 		ctx->ctx_len = runninglen[(ctx->adata.algtype &
1818e9b4913aSHoria Geantă 					   OP_ALG_ALGSEL_SUBMASK) >>
1819e9b4913aSHoria Geantă 					  OP_ALG_ALGSEL_SHIFT];
1820e9b4913aSHoria Geantă 	}
182112b8567fSIuliana Prodan 
1822e9b4913aSHoria Geantă 	if (ctx->key_dir != DMA_NONE) {
1823a2fb864cSHoria Geantă 		ctx->adata.key_dma = dma_map_single_attrs(ctx->jrdev, ctx->key,
182412b8567fSIuliana Prodan 							  ARRAY_SIZE(ctx->key),
1825e9b4913aSHoria Geantă 							  ctx->key_dir,
182612b8567fSIuliana Prodan 							  DMA_ATTR_SKIP_CPU_SYNC);
1827a2fb864cSHoria Geantă 		if (dma_mapping_error(ctx->jrdev, ctx->adata.key_dma)) {
182812b8567fSIuliana Prodan 			dev_err(ctx->jrdev, "unable to map key\n");
182912b8567fSIuliana Prodan 			caam_jr_free(ctx->jrdev);
183012b8567fSIuliana Prodan 			return -ENOMEM;
183112b8567fSIuliana Prodan 		}
183212b8567fSIuliana Prodan 	}
18337e0880b9SHoria Geantă 
1834bbf22344SHoria Geantă 	dma_addr = dma_map_single_attrs(ctx->jrdev, ctx->sh_desc_update,
183521b014f0SIuliana Prodan 					offsetof(struct caam_hash_ctx, key) -
183621b014f0SIuliana Prodan 					sh_desc_update_offset,
18377e0880b9SHoria Geantă 					ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
1838bbf22344SHoria Geantă 	if (dma_mapping_error(ctx->jrdev, dma_addr)) {
1839bbf22344SHoria Geantă 		dev_err(ctx->jrdev, "unable to map shared descriptors\n");
184012b8567fSIuliana Prodan 
1841e9b4913aSHoria Geantă 		if (ctx->key_dir != DMA_NONE)
1842a2fb864cSHoria Geantă 			dma_unmap_single_attrs(ctx->jrdev, ctx->adata.key_dma,
184312b8567fSIuliana Prodan 					       ARRAY_SIZE(ctx->key),
1844e9b4913aSHoria Geantă 					       ctx->key_dir,
184512b8567fSIuliana Prodan 					       DMA_ATTR_SKIP_CPU_SYNC);
184612b8567fSIuliana Prodan 
1847bbf22344SHoria Geantă 		caam_jr_free(ctx->jrdev);
1848bbf22344SHoria Geantă 		return -ENOMEM;
1849bbf22344SHoria Geantă 	}
1850bbf22344SHoria Geantă 
1851bbf22344SHoria Geantă 	ctx->sh_desc_update_dma = dma_addr;
1852bbf22344SHoria Geantă 	ctx->sh_desc_update_first_dma = dma_addr +
1853bbf22344SHoria Geantă 					offsetof(struct caam_hash_ctx,
185421b014f0SIuliana Prodan 						 sh_desc_update_first) -
185521b014f0SIuliana Prodan 					sh_desc_update_offset;
1856bbf22344SHoria Geantă 	ctx->sh_desc_fin_dma = dma_addr + offsetof(struct caam_hash_ctx,
185721b014f0SIuliana Prodan 						   sh_desc_fin) -
185821b014f0SIuliana Prodan 					sh_desc_update_offset;
1859bbf22344SHoria Geantă 	ctx->sh_desc_digest_dma = dma_addr + offsetof(struct caam_hash_ctx,
186021b014f0SIuliana Prodan 						      sh_desc_digest) -
186121b014f0SIuliana Prodan 					sh_desc_update_offset;
186221b014f0SIuliana Prodan 
186321b014f0SIuliana Prodan 	ctx->enginectx.op.do_one_request = ahash_do_one_req;
1864bbf22344SHoria Geantă 
18654cb4f7c1SHerbert Xu 	crypto_ahash_set_reqsize_dma(ahash, sizeof(struct caam_hash_state));
18669a2537d0SIuliana Prodan 
18679a2537d0SIuliana Prodan 	/*
18689a2537d0SIuliana Prodan 	 * For keyed hash algorithms shared descriptors
18699a2537d0SIuliana Prodan 	 * will be created later in setkey() callback
18709a2537d0SIuliana Prodan 	 */
18719a2537d0SIuliana Prodan 	return alg->setkey ? 0 : ahash_set_sh_desc(ahash);
1872045e3678SYuan Kang }
1873045e3678SYuan Kang 
1874045e3678SYuan Kang static void caam_hash_cra_exit(struct crypto_tfm *tfm)
1875045e3678SYuan Kang {
18764cb4f7c1SHerbert Xu 	struct caam_hash_ctx *ctx = crypto_tfm_ctx_dma(tfm);
1877045e3678SYuan Kang 
1878bbf22344SHoria Geantă 	dma_unmap_single_attrs(ctx->jrdev, ctx->sh_desc_update_dma,
187921b014f0SIuliana Prodan 			       offsetof(struct caam_hash_ctx, key) -
188021b014f0SIuliana Prodan 			       offsetof(struct caam_hash_ctx, sh_desc_update),
18817e0880b9SHoria Geantă 			       ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
1882e9b4913aSHoria Geantă 	if (ctx->key_dir != DMA_NONE)
1883a2fb864cSHoria Geantă 		dma_unmap_single_attrs(ctx->jrdev, ctx->adata.key_dma,
1884e9b4913aSHoria Geantă 				       ARRAY_SIZE(ctx->key), ctx->key_dir,
188512b8567fSIuliana Prodan 				       DMA_ATTR_SKIP_CPU_SYNC);
1886cfc6f11bSRuchika Gupta 	caam_jr_free(ctx->jrdev);
1887045e3678SYuan Kang }
1888045e3678SYuan Kang 
18891b46c90cSHoria Geantă void caam_algapi_hash_exit(void)
1890045e3678SYuan Kang {
1891045e3678SYuan Kang 	struct caam_hash_alg *t_alg, *n;
1892045e3678SYuan Kang 
1893cfc6f11bSRuchika Gupta 	if (!hash_list.next)
1894045e3678SYuan Kang 		return;
1895045e3678SYuan Kang 
1896cfc6f11bSRuchika Gupta 	list_for_each_entry_safe(t_alg, n, &hash_list, entry) {
1897045e3678SYuan Kang 		crypto_unregister_ahash(&t_alg->ahash_alg);
1898045e3678SYuan Kang 		list_del(&t_alg->entry);
1899045e3678SYuan Kang 		kfree(t_alg);
1900045e3678SYuan Kang 	}
1901045e3678SYuan Kang }
1902045e3678SYuan Kang 
1903045e3678SYuan Kang static struct caam_hash_alg *
1904cfc6f11bSRuchika Gupta caam_hash_alloc(struct caam_hash_template *template,
1905b0e09baeSYuan Kang 		bool keyed)
1906045e3678SYuan Kang {
1907045e3678SYuan Kang 	struct caam_hash_alg *t_alg;
1908045e3678SYuan Kang 	struct ahash_alg *halg;
1909045e3678SYuan Kang 	struct crypto_alg *alg;
1910045e3678SYuan Kang 
19119c4f9733SFabio Estevam 	t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL);
1912045e3678SYuan Kang 	if (!t_alg) {
1913cfc6f11bSRuchika Gupta 		pr_err("failed to allocate t_alg\n");
1914045e3678SYuan Kang 		return ERR_PTR(-ENOMEM);
1915045e3678SYuan Kang 	}
1916045e3678SYuan Kang 
1917045e3678SYuan Kang 	t_alg->ahash_alg = template->template_ahash;
1918045e3678SYuan Kang 	halg = &t_alg->ahash_alg;
1919045e3678SYuan Kang 	alg = &halg->halg.base;
1920045e3678SYuan Kang 
1921b0e09baeSYuan Kang 	if (keyed) {
1922b0e09baeSYuan Kang 		snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
1923b0e09baeSYuan Kang 			 template->hmac_name);
1924b0e09baeSYuan Kang 		snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
1925b0e09baeSYuan Kang 			 template->hmac_driver_name);
1926b0e09baeSYuan Kang 	} else {
1927b0e09baeSYuan Kang 		snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
1928b0e09baeSYuan Kang 			 template->name);
1929045e3678SYuan Kang 		snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
1930045e3678SYuan Kang 			 template->driver_name);
1931a0118c8bSRussell King 		t_alg->ahash_alg.setkey = NULL;
1932b0e09baeSYuan Kang 	}
1933045e3678SYuan Kang 	alg->cra_module = THIS_MODULE;
1934045e3678SYuan Kang 	alg->cra_init = caam_hash_cra_init;
1935045e3678SYuan Kang 	alg->cra_exit = caam_hash_cra_exit;
19364cb4f7c1SHerbert Xu 	alg->cra_ctxsize = sizeof(struct caam_hash_ctx) + crypto_dma_padding();
1937045e3678SYuan Kang 	alg->cra_priority = CAAM_CRA_PRIORITY;
1938045e3678SYuan Kang 	alg->cra_blocksize = template->blocksize;
1939045e3678SYuan Kang 	alg->cra_alignmask = 0;
1940b8aa7dc5SMikulas Patocka 	alg->cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY;
1941045e3678SYuan Kang 
1942045e3678SYuan Kang 	t_alg->alg_type = template->alg_type;
1943045e3678SYuan Kang 
1944045e3678SYuan Kang 	return t_alg;
1945045e3678SYuan Kang }
1946045e3678SYuan Kang 
19471b46c90cSHoria Geantă int caam_algapi_hash_init(struct device *ctrldev)
1948045e3678SYuan Kang {
1949045e3678SYuan Kang 	int i = 0, err = 0;
19501b46c90cSHoria Geantă 	struct caam_drv_private *priv = dev_get_drvdata(ctrldev);
1951bf83490eSVictoria Milhoan 	unsigned int md_limit = SHA512_DIGEST_SIZE;
1952d239b10dSHoria Geantă 	u32 md_inst, md_vid;
1953045e3678SYuan Kang 
1954bf83490eSVictoria Milhoan 	/*
1955bf83490eSVictoria Milhoan 	 * Register crypto algorithms the device supports.  First, identify
1956bf83490eSVictoria Milhoan 	 * presence and attributes of MD block.
1957bf83490eSVictoria Milhoan 	 */
1958d239b10dSHoria Geantă 	if (priv->era < 10) {
1959d239b10dSHoria Geantă 		md_vid = (rd_reg32(&priv->ctrl->perfmon.cha_id_ls) &
1960d239b10dSHoria Geantă 			  CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;
1961d239b10dSHoria Geantă 		md_inst = (rd_reg32(&priv->ctrl->perfmon.cha_num_ls) &
1962d239b10dSHoria Geantă 			   CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;
1963d239b10dSHoria Geantă 	} else {
1964d239b10dSHoria Geantă 		u32 mdha = rd_reg32(&priv->ctrl->vreg.mdha);
1965d239b10dSHoria Geantă 
1966d239b10dSHoria Geantă 		md_vid = (mdha & CHA_VER_VID_MASK) >> CHA_VER_VID_SHIFT;
1967d239b10dSHoria Geantă 		md_inst = mdha & CHA_VER_NUM_MASK;
1968d239b10dSHoria Geantă 	}
1969bf83490eSVictoria Milhoan 
1970bf83490eSVictoria Milhoan 	/*
1971bf83490eSVictoria Milhoan 	 * Skip registration of any hashing algorithms if MD block
1972bf83490eSVictoria Milhoan 	 * is not present.
1973bf83490eSVictoria Milhoan 	 */
19741b46c90cSHoria Geantă 	if (!md_inst)
19750435d47eSIuliana Prodan 		return 0;
1976bf83490eSVictoria Milhoan 
1977bf83490eSVictoria Milhoan 	/* Limit digest size based on LP256 */
1978d239b10dSHoria Geantă 	if (md_vid == CHA_VER_VID_MD_LP256)
1979bf83490eSVictoria Milhoan 		md_limit = SHA256_DIGEST_SIZE;
1980bf83490eSVictoria Milhoan 
1981cfc6f11bSRuchika Gupta 	INIT_LIST_HEAD(&hash_list);
1982045e3678SYuan Kang 
1983045e3678SYuan Kang 	/* register crypto algorithms the device supports */
1984045e3678SYuan Kang 	for (i = 0; i < ARRAY_SIZE(driver_hash); i++) {
1985045e3678SYuan Kang 		struct caam_hash_alg *t_alg;
1986bf83490eSVictoria Milhoan 		struct caam_hash_template *alg = driver_hash + i;
1987bf83490eSVictoria Milhoan 
1988bf83490eSVictoria Milhoan 		/* If MD size is not supported by device, skip registration */
198912b8567fSIuliana Prodan 		if (is_mdha(alg->alg_type) &&
199012b8567fSIuliana Prodan 		    alg->template_ahash.halg.digestsize > md_limit)
1991bf83490eSVictoria Milhoan 			continue;
1992045e3678SYuan Kang 
1993b0e09baeSYuan Kang 		/* register hmac version */
1994bf83490eSVictoria Milhoan 		t_alg = caam_hash_alloc(alg, true);
1995b0e09baeSYuan Kang 		if (IS_ERR(t_alg)) {
1996b0e09baeSYuan Kang 			err = PTR_ERR(t_alg);
19970f103b37SIuliana Prodan 			pr_warn("%s alg allocation failed\n",
19980f103b37SIuliana Prodan 				alg->hmac_driver_name);
1999b0e09baeSYuan Kang 			continue;
2000b0e09baeSYuan Kang 		}
2001b0e09baeSYuan Kang 
2002b0e09baeSYuan Kang 		err = crypto_register_ahash(&t_alg->ahash_alg);
2003b0e09baeSYuan Kang 		if (err) {
20046ea30f0aSRussell King 			pr_warn("%s alg registration failed: %d\n",
20056ea30f0aSRussell King 				t_alg->ahash_alg.halg.base.cra_driver_name,
20066ea30f0aSRussell King 				err);
2007b0e09baeSYuan Kang 			kfree(t_alg);
2008b0e09baeSYuan Kang 		} else
2009cfc6f11bSRuchika Gupta 			list_add_tail(&t_alg->entry, &hash_list);
2010b0e09baeSYuan Kang 
201112b8567fSIuliana Prodan 		if ((alg->alg_type & OP_ALG_ALGSEL_MASK) == OP_ALG_ALGSEL_AES)
201212b8567fSIuliana Prodan 			continue;
201312b8567fSIuliana Prodan 
2014b0e09baeSYuan Kang 		/* register unkeyed version */
2015bf83490eSVictoria Milhoan 		t_alg = caam_hash_alloc(alg, false);
2016045e3678SYuan Kang 		if (IS_ERR(t_alg)) {
2017045e3678SYuan Kang 			err = PTR_ERR(t_alg);
2018bf83490eSVictoria Milhoan 			pr_warn("%s alg allocation failed\n", alg->driver_name);
2019045e3678SYuan Kang 			continue;
2020045e3678SYuan Kang 		}
2021045e3678SYuan Kang 
2022045e3678SYuan Kang 		err = crypto_register_ahash(&t_alg->ahash_alg);
2023045e3678SYuan Kang 		if (err) {
20246ea30f0aSRussell King 			pr_warn("%s alg registration failed: %d\n",
20256ea30f0aSRussell King 				t_alg->ahash_alg.halg.base.cra_driver_name,
20266ea30f0aSRussell King 				err);
2027045e3678SYuan Kang 			kfree(t_alg);
2028045e3678SYuan Kang 		} else
2029cfc6f11bSRuchika Gupta 			list_add_tail(&t_alg->entry, &hash_list);
2030045e3678SYuan Kang 	}
2031045e3678SYuan Kang 
2032045e3678SYuan Kang 	return err;
2033045e3678SYuan Kang }
2034