xref: /linux/drivers/crypto/caam/caamhash.c (revision e9b4913a5f944b23d6109c44b6f3fc6e092e30ce)
1618b5dc4SHoria Geantă // SPDX-License-Identifier: GPL-2.0+
2045e3678SYuan Kang /*
3045e3678SYuan Kang  * caam - Freescale FSL CAAM support for ahash functions of crypto API
4045e3678SYuan Kang  *
5045e3678SYuan Kang  * Copyright 2011 Freescale Semiconductor, Inc.
687870cfbSIuliana Prodan  * Copyright 2018-2019 NXP
7045e3678SYuan Kang  *
8045e3678SYuan Kang  * Based on caamalg.c crypto API driver.
9045e3678SYuan Kang  *
10045e3678SYuan Kang  * relationship of digest job descriptor or first job descriptor after init to
11045e3678SYuan Kang  * shared descriptors:
12045e3678SYuan Kang  *
13045e3678SYuan Kang  * ---------------                     ---------------
14045e3678SYuan Kang  * | JobDesc #1  |-------------------->|  ShareDesc  |
15045e3678SYuan Kang  * | *(packet 1) |                     |  (hashKey)  |
16045e3678SYuan Kang  * ---------------                     | (operation) |
17045e3678SYuan Kang  *                                     ---------------
18045e3678SYuan Kang  *
19045e3678SYuan Kang  * relationship of subsequent job descriptors to shared descriptors:
20045e3678SYuan Kang  *
21045e3678SYuan Kang  * ---------------                     ---------------
22045e3678SYuan Kang  * | JobDesc #2  |-------------------->|  ShareDesc  |
23045e3678SYuan Kang  * | *(packet 2) |      |------------->|  (hashKey)  |
24045e3678SYuan Kang  * ---------------      |    |-------->| (operation) |
25045e3678SYuan Kang  *       .              |    |         | (load ctx2) |
26045e3678SYuan Kang  *       .              |    |         ---------------
27045e3678SYuan Kang  * ---------------      |    |
28045e3678SYuan Kang  * | JobDesc #3  |------|    |
29045e3678SYuan Kang  * | *(packet 3) |           |
30045e3678SYuan Kang  * ---------------           |
31045e3678SYuan Kang  *       .                   |
32045e3678SYuan Kang  *       .                   |
33045e3678SYuan Kang  * ---------------           |
34045e3678SYuan Kang  * | JobDesc #4  |------------
35045e3678SYuan Kang  * | *(packet 4) |
36045e3678SYuan Kang  * ---------------
37045e3678SYuan Kang  *
38045e3678SYuan Kang  * The SharedDesc never changes for a connection unless rekeyed, but
39045e3678SYuan Kang  * each packet will likely be in a different place. So all we need
40045e3678SYuan Kang  * to know to process the packet is where the input is, where the
41045e3678SYuan Kang  * output goes, and what context we want to process with. Context is
42045e3678SYuan Kang  * in the SharedDesc, packet references in the JobDesc.
43045e3678SYuan Kang  *
44045e3678SYuan Kang  * So, a job desc looks like:
45045e3678SYuan Kang  *
46045e3678SYuan Kang  * ---------------------
47045e3678SYuan Kang  * | Header            |
48045e3678SYuan Kang  * | ShareDesc Pointer |
49045e3678SYuan Kang  * | SEQ_OUT_PTR       |
50045e3678SYuan Kang  * | (output buffer)   |
51045e3678SYuan Kang  * | (output length)   |
52045e3678SYuan Kang  * | SEQ_IN_PTR        |
53045e3678SYuan Kang  * | (input buffer)    |
54045e3678SYuan Kang  * | (input length)    |
55045e3678SYuan Kang  * ---------------------
56045e3678SYuan Kang  */
57045e3678SYuan Kang 
58045e3678SYuan Kang #include "compat.h"
59045e3678SYuan Kang 
60045e3678SYuan Kang #include "regs.h"
61045e3678SYuan Kang #include "intern.h"
62045e3678SYuan Kang #include "desc_constr.h"
63045e3678SYuan Kang #include "jr.h"
64045e3678SYuan Kang #include "error.h"
65045e3678SYuan Kang #include "sg_sw_sec4.h"
66045e3678SYuan Kang #include "key_gen.h"
670efa7579SHoria Geantă #include "caamhash_desc.h"
68045e3678SYuan Kang 
69045e3678SYuan Kang #define CAAM_CRA_PRIORITY		3000
70045e3678SYuan Kang 
71045e3678SYuan Kang /* max hash key is max split key size */
72045e3678SYuan Kang #define CAAM_MAX_HASH_KEY_SIZE		(SHA512_DIGEST_SIZE * 2)
73045e3678SYuan Kang 
74045e3678SYuan Kang #define CAAM_MAX_HASH_BLOCK_SIZE	SHA512_BLOCK_SIZE
75045e3678SYuan Kang #define CAAM_MAX_HASH_DIGEST_SIZE	SHA512_DIGEST_SIZE
76045e3678SYuan Kang 
77045e3678SYuan Kang #define DESC_HASH_MAX_USED_BYTES	(DESC_AHASH_FINAL_LEN + \
78045e3678SYuan Kang 					 CAAM_MAX_HASH_KEY_SIZE)
79045e3678SYuan Kang #define DESC_HASH_MAX_USED_LEN		(DESC_HASH_MAX_USED_BYTES / CAAM_CMD_SZ)
80045e3678SYuan Kang 
81045e3678SYuan Kang /* caam context sizes for hashes: running digest + 8 */
82045e3678SYuan Kang #define HASH_MSG_LEN			8
83045e3678SYuan Kang #define MAX_CTX_LEN			(HASH_MSG_LEN + SHA512_DIGEST_SIZE)
84045e3678SYuan Kang 
85cfc6f11bSRuchika Gupta static struct list_head hash_list;
86cfc6f11bSRuchika Gupta 
87045e3678SYuan Kang /* ahash per-session context */
88045e3678SYuan Kang struct caam_hash_ctx {
89e11793f5SRussell King 	u32 sh_desc_update[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
90e11793f5SRussell King 	u32 sh_desc_update_first[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
91e11793f5SRussell King 	u32 sh_desc_fin[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
92e11793f5SRussell King 	u32 sh_desc_digest[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
9312b8567fSIuliana Prodan 	u8 key[CAAM_MAX_HASH_KEY_SIZE] ____cacheline_aligned;
94e11793f5SRussell King 	dma_addr_t sh_desc_update_dma ____cacheline_aligned;
95045e3678SYuan Kang 	dma_addr_t sh_desc_update_first_dma;
96045e3678SYuan Kang 	dma_addr_t sh_desc_fin_dma;
97045e3678SYuan Kang 	dma_addr_t sh_desc_digest_dma;
987e0880b9SHoria Geantă 	enum dma_data_direction dir;
99*e9b4913aSHoria Geantă 	enum dma_data_direction key_dir;
100e11793f5SRussell King 	struct device *jrdev;
101045e3678SYuan Kang 	int ctx_len;
102db57656bSHoria Geantă 	struct alginfo adata;
103045e3678SYuan Kang };
104045e3678SYuan Kang 
105045e3678SYuan Kang /* ahash state */
106045e3678SYuan Kang struct caam_hash_state {
107045e3678SYuan Kang 	dma_addr_t buf_dma;
108045e3678SYuan Kang 	dma_addr_t ctx_dma;
10965055e21SFranck LENORMAND 	int ctx_dma_len;
110045e3678SYuan Kang 	u8 buf_0[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
111045e3678SYuan Kang 	int buflen_0;
112045e3678SYuan Kang 	u8 buf_1[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
113045e3678SYuan Kang 	int buflen_1;
114e7472422SVictoria Milhoan 	u8 caam_ctx[MAX_CTX_LEN] ____cacheline_aligned;
115045e3678SYuan Kang 	int (*update)(struct ahash_request *req);
116045e3678SYuan Kang 	int (*final)(struct ahash_request *req);
117045e3678SYuan Kang 	int (*finup)(struct ahash_request *req);
118045e3678SYuan Kang 	int current_buf;
119045e3678SYuan Kang };
120045e3678SYuan Kang 
1215ec90831SRussell King struct caam_export_state {
1225ec90831SRussell King 	u8 buf[CAAM_MAX_HASH_BLOCK_SIZE];
1235ec90831SRussell King 	u8 caam_ctx[MAX_CTX_LEN];
1245ec90831SRussell King 	int buflen;
1255ec90831SRussell King 	int (*update)(struct ahash_request *req);
1265ec90831SRussell King 	int (*final)(struct ahash_request *req);
1275ec90831SRussell King 	int (*finup)(struct ahash_request *req);
1285ec90831SRussell King };
1295ec90831SRussell King 
1300355d23dSHoria Geantă static inline void switch_buf(struct caam_hash_state *state)
1310355d23dSHoria Geantă {
1320355d23dSHoria Geantă 	state->current_buf ^= 1;
1330355d23dSHoria Geantă }
1340355d23dSHoria Geantă 
1350355d23dSHoria Geantă static inline u8 *current_buf(struct caam_hash_state *state)
1360355d23dSHoria Geantă {
1370355d23dSHoria Geantă 	return state->current_buf ? state->buf_1 : state->buf_0;
1380355d23dSHoria Geantă }
1390355d23dSHoria Geantă 
1400355d23dSHoria Geantă static inline u8 *alt_buf(struct caam_hash_state *state)
1410355d23dSHoria Geantă {
1420355d23dSHoria Geantă 	return state->current_buf ? state->buf_0 : state->buf_1;
1430355d23dSHoria Geantă }
1440355d23dSHoria Geantă 
1450355d23dSHoria Geantă static inline int *current_buflen(struct caam_hash_state *state)
1460355d23dSHoria Geantă {
1470355d23dSHoria Geantă 	return state->current_buf ? &state->buflen_1 : &state->buflen_0;
1480355d23dSHoria Geantă }
1490355d23dSHoria Geantă 
1500355d23dSHoria Geantă static inline int *alt_buflen(struct caam_hash_state *state)
1510355d23dSHoria Geantă {
1520355d23dSHoria Geantă 	return state->current_buf ? &state->buflen_0 : &state->buflen_1;
1530355d23dSHoria Geantă }
1540355d23dSHoria Geantă 
15587870cfbSIuliana Prodan static inline bool is_cmac_aes(u32 algtype)
15612b8567fSIuliana Prodan {
15712b8567fSIuliana Prodan 	return (algtype & (OP_ALG_ALGSEL_MASK | OP_ALG_AAI_MASK)) ==
15887870cfbSIuliana Prodan 	       (OP_ALG_ALGSEL_AES | OP_ALG_AAI_CMAC);
15912b8567fSIuliana Prodan }
160045e3678SYuan Kang /* Common job descriptor seq in/out ptr routines */
161045e3678SYuan Kang 
162045e3678SYuan Kang /* Map state->caam_ctx, and append seq_out_ptr command that points to it */
163ce572085SHoria Geanta static inline int map_seq_out_ptr_ctx(u32 *desc, struct device *jrdev,
164045e3678SYuan Kang 				      struct caam_hash_state *state,
165045e3678SYuan Kang 				      int ctx_len)
166045e3678SYuan Kang {
16765055e21SFranck LENORMAND 	state->ctx_dma_len = ctx_len;
168045e3678SYuan Kang 	state->ctx_dma = dma_map_single(jrdev, state->caam_ctx,
169045e3678SYuan Kang 					ctx_len, DMA_FROM_DEVICE);
170ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, state->ctx_dma)) {
171ce572085SHoria Geanta 		dev_err(jrdev, "unable to map ctx\n");
17287ec02e7SHoria Geantă 		state->ctx_dma = 0;
173ce572085SHoria Geanta 		return -ENOMEM;
174ce572085SHoria Geanta 	}
175ce572085SHoria Geanta 
176045e3678SYuan Kang 	append_seq_out_ptr(desc, state->ctx_dma, ctx_len, 0);
177ce572085SHoria Geanta 
178ce572085SHoria Geanta 	return 0;
179045e3678SYuan Kang }
180045e3678SYuan Kang 
181944c3d4dSHoria Geantă /* Map current buffer in state (if length > 0) and put it in link table */
182944c3d4dSHoria Geantă static inline int buf_map_to_sec4_sg(struct device *jrdev,
183045e3678SYuan Kang 				     struct sec4_sg_entry *sec4_sg,
184944c3d4dSHoria Geantă 				     struct caam_hash_state *state)
185045e3678SYuan Kang {
186944c3d4dSHoria Geantă 	int buflen = *current_buflen(state);
187045e3678SYuan Kang 
188944c3d4dSHoria Geantă 	if (!buflen)
189944c3d4dSHoria Geantă 		return 0;
190045e3678SYuan Kang 
191944c3d4dSHoria Geantă 	state->buf_dma = dma_map_single(jrdev, current_buf(state), buflen,
192944c3d4dSHoria Geantă 					DMA_TO_DEVICE);
193944c3d4dSHoria Geantă 	if (dma_mapping_error(jrdev, state->buf_dma)) {
194944c3d4dSHoria Geantă 		dev_err(jrdev, "unable to map buf\n");
195944c3d4dSHoria Geantă 		state->buf_dma = 0;
196944c3d4dSHoria Geantă 		return -ENOMEM;
197045e3678SYuan Kang 	}
198045e3678SYuan Kang 
199944c3d4dSHoria Geantă 	dma_to_sec4_sg_one(sec4_sg, state->buf_dma, buflen, 0);
200045e3678SYuan Kang 
201944c3d4dSHoria Geantă 	return 0;
202045e3678SYuan Kang }
203045e3678SYuan Kang 
204045e3678SYuan Kang /* Map state->caam_ctx, and add it to link table */
205dfcd8393SHoria Geantă static inline int ctx_map_to_sec4_sg(struct device *jrdev,
206ce572085SHoria Geanta 				     struct caam_hash_state *state, int ctx_len,
207ce572085SHoria Geanta 				     struct sec4_sg_entry *sec4_sg, u32 flag)
208045e3678SYuan Kang {
20965055e21SFranck LENORMAND 	state->ctx_dma_len = ctx_len;
210045e3678SYuan Kang 	state->ctx_dma = dma_map_single(jrdev, state->caam_ctx, ctx_len, flag);
211ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, state->ctx_dma)) {
212ce572085SHoria Geanta 		dev_err(jrdev, "unable to map ctx\n");
21387ec02e7SHoria Geantă 		state->ctx_dma = 0;
214ce572085SHoria Geanta 		return -ENOMEM;
215ce572085SHoria Geanta 	}
216ce572085SHoria Geanta 
217045e3678SYuan Kang 	dma_to_sec4_sg_one(sec4_sg, state->ctx_dma, ctx_len, 0);
218ce572085SHoria Geanta 
219ce572085SHoria Geanta 	return 0;
220045e3678SYuan Kang }
221045e3678SYuan Kang 
222045e3678SYuan Kang static int ahash_set_sh_desc(struct crypto_ahash *ahash)
223045e3678SYuan Kang {
224045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
225045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
226045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
2277e0880b9SHoria Geantă 	struct caam_drv_private *ctrlpriv = dev_get_drvdata(jrdev->parent);
228045e3678SYuan Kang 	u32 *desc;
229045e3678SYuan Kang 
2307e0880b9SHoria Geantă 	ctx->adata.key_virt = ctx->key;
2317e0880b9SHoria Geantă 
232045e3678SYuan Kang 	/* ahash_update shared descriptor */
233045e3678SYuan Kang 	desc = ctx->sh_desc_update;
2340efa7579SHoria Geantă 	cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_UPDATE, ctx->ctx_len,
2350efa7579SHoria Geantă 			  ctx->ctx_len, true, ctrlpriv->era);
236bbf22344SHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma,
2377e0880b9SHoria Geantă 				   desc_bytes(desc), ctx->dir);
2386e005503SSascha Hauer 
2396e005503SSascha Hauer 	print_hex_dump_debug("ahash update shdesc@"__stringify(__LINE__)": ",
2406e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
2416e005503SSascha Hauer 			     1);
242045e3678SYuan Kang 
243045e3678SYuan Kang 	/* ahash_update_first shared descriptor */
244045e3678SYuan Kang 	desc = ctx->sh_desc_update_first;
2450efa7579SHoria Geantă 	cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len,
2460efa7579SHoria Geantă 			  ctx->ctx_len, false, ctrlpriv->era);
247bbf22344SHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma,
2487e0880b9SHoria Geantă 				   desc_bytes(desc), ctx->dir);
2496e005503SSascha Hauer 	print_hex_dump_debug("ahash update first shdesc@"__stringify(__LINE__)
2506e005503SSascha Hauer 			     ": ", DUMP_PREFIX_ADDRESS, 16, 4, desc,
2516e005503SSascha Hauer 			     desc_bytes(desc), 1);
252045e3678SYuan Kang 
253045e3678SYuan Kang 	/* ahash_final shared descriptor */
254045e3678SYuan Kang 	desc = ctx->sh_desc_fin;
2550efa7579SHoria Geantă 	cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_FINALIZE, digestsize,
2560efa7579SHoria Geantă 			  ctx->ctx_len, true, ctrlpriv->era);
257bbf22344SHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma,
2587e0880b9SHoria Geantă 				   desc_bytes(desc), ctx->dir);
2596e005503SSascha Hauer 
2606e005503SSascha Hauer 	print_hex_dump_debug("ahash final shdesc@"__stringify(__LINE__)": ",
261045e3678SYuan Kang 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
262045e3678SYuan Kang 			     desc_bytes(desc), 1);
263045e3678SYuan Kang 
264045e3678SYuan Kang 	/* ahash_digest shared descriptor */
265045e3678SYuan Kang 	desc = ctx->sh_desc_digest;
2660efa7579SHoria Geantă 	cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_INITFINAL, digestsize,
2670efa7579SHoria Geantă 			  ctx->ctx_len, false, ctrlpriv->era);
268bbf22344SHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma,
2697e0880b9SHoria Geantă 				   desc_bytes(desc), ctx->dir);
2706e005503SSascha Hauer 
2716e005503SSascha Hauer 	print_hex_dump_debug("ahash digest shdesc@"__stringify(__LINE__)": ",
272045e3678SYuan Kang 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
273045e3678SYuan Kang 			     desc_bytes(desc), 1);
274045e3678SYuan Kang 
275045e3678SYuan Kang 	return 0;
276045e3678SYuan Kang }
277045e3678SYuan Kang 
27812b8567fSIuliana Prodan static int axcbc_set_sh_desc(struct crypto_ahash *ahash)
27912b8567fSIuliana Prodan {
28012b8567fSIuliana Prodan 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
28112b8567fSIuliana Prodan 	int digestsize = crypto_ahash_digestsize(ahash);
28212b8567fSIuliana Prodan 	struct device *jrdev = ctx->jrdev;
28312b8567fSIuliana Prodan 	u32 *desc;
28412b8567fSIuliana Prodan 
28512b8567fSIuliana Prodan 	/* shared descriptor for ahash_update */
28612b8567fSIuliana Prodan 	desc = ctx->sh_desc_update;
28787870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_UPDATE,
288a2fb864cSHoria Geantă 			    ctx->ctx_len, ctx->ctx_len);
28912b8567fSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma,
29012b8567fSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
29112b8567fSIuliana Prodan 	print_hex_dump_debug("axcbc update shdesc@" __stringify(__LINE__)" : ",
29212b8567fSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
29312b8567fSIuliana Prodan 			     1);
29412b8567fSIuliana Prodan 
29512b8567fSIuliana Prodan 	/* shared descriptor for ahash_{final,finup} */
29612b8567fSIuliana Prodan 	desc = ctx->sh_desc_fin;
29787870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_FINALIZE,
298a2fb864cSHoria Geantă 			    digestsize, ctx->ctx_len);
29912b8567fSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma,
30012b8567fSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
30112b8567fSIuliana Prodan 	print_hex_dump_debug("axcbc finup shdesc@" __stringify(__LINE__)" : ",
30212b8567fSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
30312b8567fSIuliana Prodan 			     1);
30412b8567fSIuliana Prodan 
30512b8567fSIuliana Prodan 	/* key is immediate data for INIT and INITFINAL states */
30612b8567fSIuliana Prodan 	ctx->adata.key_virt = ctx->key;
30712b8567fSIuliana Prodan 
30812b8567fSIuliana Prodan 	/* shared descriptor for first invocation of ahash_update */
30912b8567fSIuliana Prodan 	desc = ctx->sh_desc_update_first;
31087870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len,
311a2fb864cSHoria Geantă 			    ctx->ctx_len);
31212b8567fSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma,
31312b8567fSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
3146e005503SSascha Hauer 	print_hex_dump_debug("axcbc update first shdesc@" __stringify(__LINE__)
3156e005503SSascha Hauer 			     " : ", DUMP_PREFIX_ADDRESS, 16, 4, desc,
3166e005503SSascha Hauer 			     desc_bytes(desc), 1);
31712b8567fSIuliana Prodan 
31812b8567fSIuliana Prodan 	/* shared descriptor for ahash_digest */
31912b8567fSIuliana Prodan 	desc = ctx->sh_desc_digest;
32087870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INITFINAL,
321a2fb864cSHoria Geantă 			    digestsize, ctx->ctx_len);
32212b8567fSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma,
32312b8567fSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
32412b8567fSIuliana Prodan 	print_hex_dump_debug("axcbc digest shdesc@" __stringify(__LINE__)" : ",
32512b8567fSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
32612b8567fSIuliana Prodan 			     1);
32787870cfbSIuliana Prodan 	return 0;
32887870cfbSIuliana Prodan }
32987870cfbSIuliana Prodan 
33087870cfbSIuliana Prodan static int acmac_set_sh_desc(struct crypto_ahash *ahash)
33187870cfbSIuliana Prodan {
33287870cfbSIuliana Prodan 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
33387870cfbSIuliana Prodan 	int digestsize = crypto_ahash_digestsize(ahash);
33487870cfbSIuliana Prodan 	struct device *jrdev = ctx->jrdev;
33587870cfbSIuliana Prodan 	u32 *desc;
33687870cfbSIuliana Prodan 
33787870cfbSIuliana Prodan 	/* shared descriptor for ahash_update */
33887870cfbSIuliana Prodan 	desc = ctx->sh_desc_update;
33987870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_UPDATE,
340a2fb864cSHoria Geantă 			    ctx->ctx_len, ctx->ctx_len);
34187870cfbSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma,
34287870cfbSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
34387870cfbSIuliana Prodan 	print_hex_dump_debug("acmac update shdesc@" __stringify(__LINE__)" : ",
34487870cfbSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
34587870cfbSIuliana Prodan 			     desc_bytes(desc), 1);
34687870cfbSIuliana Prodan 
34787870cfbSIuliana Prodan 	/* shared descriptor for ahash_{final,finup} */
34887870cfbSIuliana Prodan 	desc = ctx->sh_desc_fin;
34987870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_FINALIZE,
350a2fb864cSHoria Geantă 			    digestsize, ctx->ctx_len);
35187870cfbSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma,
35287870cfbSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
35387870cfbSIuliana Prodan 	print_hex_dump_debug("acmac finup shdesc@" __stringify(__LINE__)" : ",
35487870cfbSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
35587870cfbSIuliana Prodan 			     desc_bytes(desc), 1);
35687870cfbSIuliana Prodan 
35787870cfbSIuliana Prodan 	/* shared descriptor for first invocation of ahash_update */
35887870cfbSIuliana Prodan 	desc = ctx->sh_desc_update_first;
35987870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len,
360a2fb864cSHoria Geantă 			    ctx->ctx_len);
36187870cfbSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma,
36287870cfbSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
3636e005503SSascha Hauer 	print_hex_dump_debug("acmac update first shdesc@" __stringify(__LINE__)
3646e005503SSascha Hauer 			     " : ", DUMP_PREFIX_ADDRESS, 16, 4, desc,
36587870cfbSIuliana Prodan 			     desc_bytes(desc), 1);
36687870cfbSIuliana Prodan 
36787870cfbSIuliana Prodan 	/* shared descriptor for ahash_digest */
36887870cfbSIuliana Prodan 	desc = ctx->sh_desc_digest;
36987870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INITFINAL,
370a2fb864cSHoria Geantă 			    digestsize, ctx->ctx_len);
37187870cfbSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma,
37287870cfbSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
37387870cfbSIuliana Prodan 	print_hex_dump_debug("acmac digest shdesc@" __stringify(__LINE__)" : ",
37487870cfbSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
37587870cfbSIuliana Prodan 			     desc_bytes(desc), 1);
37612b8567fSIuliana Prodan 
37712b8567fSIuliana Prodan 	return 0;
37812b8567fSIuliana Prodan }
37912b8567fSIuliana Prodan 
380045e3678SYuan Kang /* Digest hash size if it is too large */
38130724445SHoria Geantă static int hash_digest_key(struct caam_hash_ctx *ctx, u32 *keylen, u8 *key,
38230724445SHoria Geantă 			   u32 digestsize)
383045e3678SYuan Kang {
384045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
385045e3678SYuan Kang 	u32 *desc;
386045e3678SYuan Kang 	struct split_key_result result;
38730724445SHoria Geantă 	dma_addr_t key_dma;
3889e6df0fdSMarkus Elfring 	int ret;
389045e3678SYuan Kang 
3909c23b7d3SVakul Garg 	desc = kmalloc(CAAM_CMD_SZ * 8 + CAAM_PTR_SZ * 2, GFP_KERNEL | GFP_DMA);
3912af8f4a2SKim Phillips 	if (!desc) {
3922af8f4a2SKim Phillips 		dev_err(jrdev, "unable to allocate key input memory\n");
3932af8f4a2SKim Phillips 		return -ENOMEM;
3942af8f4a2SKim Phillips 	}
395045e3678SYuan Kang 
396045e3678SYuan Kang 	init_job_desc(desc, 0);
397045e3678SYuan Kang 
39830724445SHoria Geantă 	key_dma = dma_map_single(jrdev, key, *keylen, DMA_BIDIRECTIONAL);
39930724445SHoria Geantă 	if (dma_mapping_error(jrdev, key_dma)) {
40030724445SHoria Geantă 		dev_err(jrdev, "unable to map key memory\n");
401045e3678SYuan Kang 		kfree(desc);
402045e3678SYuan Kang 		return -ENOMEM;
403045e3678SYuan Kang 	}
404045e3678SYuan Kang 
405045e3678SYuan Kang 	/* Job descriptor to perform unkeyed hash on key_in */
406db57656bSHoria Geantă 	append_operation(desc, ctx->adata.algtype | OP_ALG_ENCRYPT |
407045e3678SYuan Kang 			 OP_ALG_AS_INITFINAL);
40830724445SHoria Geantă 	append_seq_in_ptr(desc, key_dma, *keylen, 0);
409045e3678SYuan Kang 	append_seq_fifo_load(desc, *keylen, FIFOLD_CLASS_CLASS2 |
410045e3678SYuan Kang 			     FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_MSG);
41130724445SHoria Geantă 	append_seq_out_ptr(desc, key_dma, digestsize, 0);
412045e3678SYuan Kang 	append_seq_store(desc, digestsize, LDST_CLASS_2_CCB |
413045e3678SYuan Kang 			 LDST_SRCDST_BYTE_CONTEXT);
414045e3678SYuan Kang 
4156e005503SSascha Hauer 	print_hex_dump_debug("key_in@"__stringify(__LINE__)": ",
41630724445SHoria Geantă 			     DUMP_PREFIX_ADDRESS, 16, 4, key, *keylen, 1);
4176e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
4186e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
4196e005503SSascha Hauer 			     1);
420045e3678SYuan Kang 
421045e3678SYuan Kang 	result.err = 0;
422045e3678SYuan Kang 	init_completion(&result.completion);
423045e3678SYuan Kang 
424045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, split_key_done, &result);
425045e3678SYuan Kang 	if (!ret) {
426045e3678SYuan Kang 		/* in progress */
4277459e1d2SHoria Geantă 		wait_for_completion(&result.completion);
428045e3678SYuan Kang 		ret = result.err;
4296e005503SSascha Hauer 
4306e005503SSascha Hauer 		print_hex_dump_debug("digested key@"__stringify(__LINE__)": ",
4316e005503SSascha Hauer 				     DUMP_PREFIX_ADDRESS, 16, 4, key,
4326e005503SSascha Hauer 				     digestsize, 1);
433045e3678SYuan Kang 	}
43430724445SHoria Geantă 	dma_unmap_single(jrdev, key_dma, *keylen, DMA_BIDIRECTIONAL);
435045e3678SYuan Kang 
436e11aa9f1SHoria Geanta 	*keylen = digestsize;
437e11aa9f1SHoria Geanta 
438045e3678SYuan Kang 	kfree(desc);
439045e3678SYuan Kang 
440045e3678SYuan Kang 	return ret;
441045e3678SYuan Kang }
442045e3678SYuan Kang 
443045e3678SYuan Kang static int ahash_setkey(struct crypto_ahash *ahash,
444045e3678SYuan Kang 			const u8 *key, unsigned int keylen)
445045e3678SYuan Kang {
446045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
4476e005503SSascha Hauer 	struct device *jrdev = ctx->jrdev;
448045e3678SYuan Kang 	int blocksize = crypto_tfm_alg_blocksize(&ahash->base);
449045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
4507e0880b9SHoria Geantă 	struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctx->jrdev->parent);
4519e6df0fdSMarkus Elfring 	int ret;
452045e3678SYuan Kang 	u8 *hashed_key = NULL;
453045e3678SYuan Kang 
4546e005503SSascha Hauer 	dev_dbg(jrdev, "keylen %d\n", keylen);
455045e3678SYuan Kang 
456045e3678SYuan Kang 	if (keylen > blocksize) {
45730724445SHoria Geantă 		hashed_key = kmemdup(key, keylen, GFP_KERNEL | GFP_DMA);
458045e3678SYuan Kang 		if (!hashed_key)
459045e3678SYuan Kang 			return -ENOMEM;
46030724445SHoria Geantă 		ret = hash_digest_key(ctx, &keylen, hashed_key, digestsize);
461045e3678SYuan Kang 		if (ret)
462d6e7a7d0SMarkus Elfring 			goto bad_free_key;
463045e3678SYuan Kang 		key = hashed_key;
464045e3678SYuan Kang 	}
465045e3678SYuan Kang 
4667e0880b9SHoria Geantă 	/*
4677e0880b9SHoria Geantă 	 * If DKP is supported, use it in the shared descriptor to generate
4687e0880b9SHoria Geantă 	 * the split key.
4697e0880b9SHoria Geantă 	 */
4707e0880b9SHoria Geantă 	if (ctrlpriv->era >= 6) {
4717e0880b9SHoria Geantă 		ctx->adata.key_inline = true;
4727e0880b9SHoria Geantă 		ctx->adata.keylen = keylen;
4737e0880b9SHoria Geantă 		ctx->adata.keylen_pad = split_key_len(ctx->adata.algtype &
4747e0880b9SHoria Geantă 						      OP_ALG_ALGSEL_MASK);
4757e0880b9SHoria Geantă 
4767e0880b9SHoria Geantă 		if (ctx->adata.keylen_pad > CAAM_MAX_HASH_KEY_SIZE)
477d6e7a7d0SMarkus Elfring 			goto bad_free_key;
478045e3678SYuan Kang 
4797e0880b9SHoria Geantă 		memcpy(ctx->key, key, keylen);
480*e9b4913aSHoria Geantă 
481*e9b4913aSHoria Geantă 		/*
482*e9b4913aSHoria Geantă 		 * In case |user key| > |derived key|, using DKP<imm,imm>
483*e9b4913aSHoria Geantă 		 * would result in invalid opcodes (last bytes of user key) in
484*e9b4913aSHoria Geantă 		 * the resulting descriptor. Use DKP<ptr,imm> instead => both
485*e9b4913aSHoria Geantă 		 * virtual and dma key addresses are needed.
486*e9b4913aSHoria Geantă 		 */
487*e9b4913aSHoria Geantă 		if (keylen > ctx->adata.keylen_pad)
488*e9b4913aSHoria Geantă 			dma_sync_single_for_device(ctx->jrdev,
489*e9b4913aSHoria Geantă 						   ctx->adata.key_dma,
490*e9b4913aSHoria Geantă 						   ctx->adata.keylen_pad,
491*e9b4913aSHoria Geantă 						   DMA_TO_DEVICE);
4927e0880b9SHoria Geantă 	} else {
4937e0880b9SHoria Geantă 		ret = gen_split_key(ctx->jrdev, ctx->key, &ctx->adata, key,
4947e0880b9SHoria Geantă 				    keylen, CAAM_MAX_HASH_KEY_SIZE);
4957e0880b9SHoria Geantă 		if (ret)
4967e0880b9SHoria Geantă 			goto bad_free_key;
4977e0880b9SHoria Geantă 	}
498045e3678SYuan Kang 
499045e3678SYuan Kang 	kfree(hashed_key);
500cfb725f6SHoria Geantă 	return ahash_set_sh_desc(ahash);
501d6e7a7d0SMarkus Elfring  bad_free_key:
502045e3678SYuan Kang 	kfree(hashed_key);
503045e3678SYuan Kang 	crypto_ahash_set_flags(ahash, CRYPTO_TFM_RES_BAD_KEY_LEN);
504045e3678SYuan Kang 	return -EINVAL;
505045e3678SYuan Kang }
506045e3678SYuan Kang 
50712b8567fSIuliana Prodan static int axcbc_setkey(struct crypto_ahash *ahash, const u8 *key,
50812b8567fSIuliana Prodan 			unsigned int keylen)
50912b8567fSIuliana Prodan {
51012b8567fSIuliana Prodan 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
51112b8567fSIuliana Prodan 	struct device *jrdev = ctx->jrdev;
51212b8567fSIuliana Prodan 
513836d8f43SIuliana Prodan 	if (keylen != AES_KEYSIZE_128) {
514836d8f43SIuliana Prodan 		crypto_ahash_set_flags(ahash, CRYPTO_TFM_RES_BAD_KEY_LEN);
515836d8f43SIuliana Prodan 		return -EINVAL;
516836d8f43SIuliana Prodan 	}
517836d8f43SIuliana Prodan 
51812b8567fSIuliana Prodan 	memcpy(ctx->key, key, keylen);
519a2fb864cSHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->adata.key_dma, keylen,
520a2fb864cSHoria Geantă 				   DMA_TO_DEVICE);
52112b8567fSIuliana Prodan 	ctx->adata.keylen = keylen;
52212b8567fSIuliana Prodan 
52312b8567fSIuliana Prodan 	print_hex_dump_debug("axcbc ctx.key@" __stringify(__LINE__)" : ",
52412b8567fSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, ctx->key, keylen, 1);
52512b8567fSIuliana Prodan 
52612b8567fSIuliana Prodan 	return axcbc_set_sh_desc(ahash);
52712b8567fSIuliana Prodan }
52887870cfbSIuliana Prodan 
52987870cfbSIuliana Prodan static int acmac_setkey(struct crypto_ahash *ahash, const u8 *key,
53087870cfbSIuliana Prodan 			unsigned int keylen)
53187870cfbSIuliana Prodan {
53287870cfbSIuliana Prodan 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
533836d8f43SIuliana Prodan 	int err;
534836d8f43SIuliana Prodan 
535836d8f43SIuliana Prodan 	err = aes_check_keylen(keylen);
536836d8f43SIuliana Prodan 	if (err) {
537836d8f43SIuliana Prodan 		crypto_ahash_set_flags(ahash, CRYPTO_TFM_RES_BAD_KEY_LEN);
538836d8f43SIuliana Prodan 		return err;
539836d8f43SIuliana Prodan 	}
54087870cfbSIuliana Prodan 
54187870cfbSIuliana Prodan 	/* key is immediate data for all cmac shared descriptors */
54287870cfbSIuliana Prodan 	ctx->adata.key_virt = key;
54387870cfbSIuliana Prodan 	ctx->adata.keylen = keylen;
54487870cfbSIuliana Prodan 
54587870cfbSIuliana Prodan 	print_hex_dump_debug("acmac ctx.key@" __stringify(__LINE__)" : ",
54687870cfbSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
54787870cfbSIuliana Prodan 
54887870cfbSIuliana Prodan 	return acmac_set_sh_desc(ahash);
54987870cfbSIuliana Prodan }
55087870cfbSIuliana Prodan 
551045e3678SYuan Kang /*
552045e3678SYuan Kang  * ahash_edesc - s/w-extended ahash descriptor
553045e3678SYuan Kang  * @sec4_sg_dma: physical mapped address of h/w link table
554045e3678SYuan Kang  * @src_nents: number of segments in input scatterlist
555045e3678SYuan Kang  * @sec4_sg_bytes: length of dma mapped sec4_sg space
556045e3678SYuan Kang  * @hw_desc: the h/w job descriptor followed by any referenced link tables
557343e44b1SRussell King  * @sec4_sg: h/w link table
558045e3678SYuan Kang  */
559045e3678SYuan Kang struct ahash_edesc {
560045e3678SYuan Kang 	dma_addr_t sec4_sg_dma;
561045e3678SYuan Kang 	int src_nents;
562045e3678SYuan Kang 	int sec4_sg_bytes;
563d7b24ed4SRussell King 	u32 hw_desc[DESC_JOB_IO_LEN / sizeof(u32)] ____cacheline_aligned;
564343e44b1SRussell King 	struct sec4_sg_entry sec4_sg[0];
565045e3678SYuan Kang };
566045e3678SYuan Kang 
567045e3678SYuan Kang static inline void ahash_unmap(struct device *dev,
568045e3678SYuan Kang 			struct ahash_edesc *edesc,
569045e3678SYuan Kang 			struct ahash_request *req, int dst_len)
570045e3678SYuan Kang {
571944c3d4dSHoria Geantă 	struct caam_hash_state *state = ahash_request_ctx(req);
572944c3d4dSHoria Geantă 
573045e3678SYuan Kang 	if (edesc->src_nents)
57413fb8fd7SLABBE Corentin 		dma_unmap_sg(dev, req->src, edesc->src_nents, DMA_TO_DEVICE);
575045e3678SYuan Kang 
576045e3678SYuan Kang 	if (edesc->sec4_sg_bytes)
577045e3678SYuan Kang 		dma_unmap_single(dev, edesc->sec4_sg_dma,
578045e3678SYuan Kang 				 edesc->sec4_sg_bytes, DMA_TO_DEVICE);
579944c3d4dSHoria Geantă 
580944c3d4dSHoria Geantă 	if (state->buf_dma) {
581944c3d4dSHoria Geantă 		dma_unmap_single(dev, state->buf_dma, *current_buflen(state),
582944c3d4dSHoria Geantă 				 DMA_TO_DEVICE);
583944c3d4dSHoria Geantă 		state->buf_dma = 0;
584944c3d4dSHoria Geantă 	}
585045e3678SYuan Kang }
586045e3678SYuan Kang 
587045e3678SYuan Kang static inline void ahash_unmap_ctx(struct device *dev,
588045e3678SYuan Kang 			struct ahash_edesc *edesc,
589045e3678SYuan Kang 			struct ahash_request *req, int dst_len, u32 flag)
590045e3678SYuan Kang {
591045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
592045e3678SYuan Kang 
59387ec02e7SHoria Geantă 	if (state->ctx_dma) {
59465055e21SFranck LENORMAND 		dma_unmap_single(dev, state->ctx_dma, state->ctx_dma_len, flag);
59587ec02e7SHoria Geantă 		state->ctx_dma = 0;
59687ec02e7SHoria Geantă 	}
597045e3678SYuan Kang 	ahash_unmap(dev, edesc, req, dst_len);
598045e3678SYuan Kang }
599045e3678SYuan Kang 
600045e3678SYuan Kang static void ahash_done(struct device *jrdev, u32 *desc, u32 err,
601045e3678SYuan Kang 		       void *context)
602045e3678SYuan Kang {
603045e3678SYuan Kang 	struct ahash_request *req = context;
604045e3678SYuan Kang 	struct ahash_edesc *edesc;
605045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
606045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
607c19650d6SHoria Geantă 	struct caam_hash_state *state = ahash_request_ctx(req);
608045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
6091984aaeeSHoria Geantă 	int ecode = 0;
610045e3678SYuan Kang 
6116e005503SSascha Hauer 	dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
612045e3678SYuan Kang 
6134ca7c7d8SHoria Geantă 	edesc = container_of(desc, struct ahash_edesc, hw_desc[0]);
614fa9659cdSMarek Vasut 	if (err)
6151984aaeeSHoria Geantă 		ecode = caam_jr_strstatus(jrdev, err);
616045e3678SYuan Kang 
617c19650d6SHoria Geantă 	ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
618c19650d6SHoria Geantă 	memcpy(req->result, state->caam_ctx, digestsize);
619045e3678SYuan Kang 	kfree(edesc);
620045e3678SYuan Kang 
6216e005503SSascha Hauer 	print_hex_dump_debug("ctx@"__stringify(__LINE__)": ",
622045e3678SYuan Kang 			     DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
623045e3678SYuan Kang 			     ctx->ctx_len, 1);
624045e3678SYuan Kang 
6251984aaeeSHoria Geantă 	req->base.complete(&req->base, ecode);
626045e3678SYuan Kang }
627045e3678SYuan Kang 
628045e3678SYuan Kang static void ahash_done_bi(struct device *jrdev, u32 *desc, u32 err,
629045e3678SYuan Kang 			    void *context)
630045e3678SYuan Kang {
631045e3678SYuan Kang 	struct ahash_request *req = context;
632045e3678SYuan Kang 	struct ahash_edesc *edesc;
633045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
634045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
635045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
636045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
6371984aaeeSHoria Geantă 	int ecode = 0;
638045e3678SYuan Kang 
6396e005503SSascha Hauer 	dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
640045e3678SYuan Kang 
6414ca7c7d8SHoria Geantă 	edesc = container_of(desc, struct ahash_edesc, hw_desc[0]);
642fa9659cdSMarek Vasut 	if (err)
6431984aaeeSHoria Geantă 		ecode = caam_jr_strstatus(jrdev, err);
644045e3678SYuan Kang 
645045e3678SYuan Kang 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_BIDIRECTIONAL);
646944c3d4dSHoria Geantă 	switch_buf(state);
647045e3678SYuan Kang 	kfree(edesc);
648045e3678SYuan Kang 
6496e005503SSascha Hauer 	print_hex_dump_debug("ctx@"__stringify(__LINE__)": ",
650045e3678SYuan Kang 			     DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
651045e3678SYuan Kang 			     ctx->ctx_len, 1);
652045e3678SYuan Kang 	if (req->result)
6536e005503SSascha Hauer 		print_hex_dump_debug("result@"__stringify(__LINE__)": ",
654045e3678SYuan Kang 				     DUMP_PREFIX_ADDRESS, 16, 4, req->result,
655045e3678SYuan Kang 				     digestsize, 1);
656045e3678SYuan Kang 
6571984aaeeSHoria Geantă 	req->base.complete(&req->base, ecode);
658045e3678SYuan Kang }
659045e3678SYuan Kang 
660045e3678SYuan Kang static void ahash_done_ctx_src(struct device *jrdev, u32 *desc, u32 err,
661045e3678SYuan Kang 			       void *context)
662045e3678SYuan Kang {
663045e3678SYuan Kang 	struct ahash_request *req = context;
664045e3678SYuan Kang 	struct ahash_edesc *edesc;
665045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
666045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
667c19650d6SHoria Geantă 	struct caam_hash_state *state = ahash_request_ctx(req);
668045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
6691984aaeeSHoria Geantă 	int ecode = 0;
670045e3678SYuan Kang 
6716e005503SSascha Hauer 	dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
672045e3678SYuan Kang 
6734ca7c7d8SHoria Geantă 	edesc = container_of(desc, struct ahash_edesc, hw_desc[0]);
674fa9659cdSMarek Vasut 	if (err)
6751984aaeeSHoria Geantă 		ecode = caam_jr_strstatus(jrdev, err);
676045e3678SYuan Kang 
677c19650d6SHoria Geantă 	ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL);
678c19650d6SHoria Geantă 	memcpy(req->result, state->caam_ctx, digestsize);
679045e3678SYuan Kang 	kfree(edesc);
680045e3678SYuan Kang 
6816e005503SSascha Hauer 	print_hex_dump_debug("ctx@"__stringify(__LINE__)": ",
682045e3678SYuan Kang 			     DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
683045e3678SYuan Kang 			     ctx->ctx_len, 1);
684045e3678SYuan Kang 
6851984aaeeSHoria Geantă 	req->base.complete(&req->base, ecode);
686045e3678SYuan Kang }
687045e3678SYuan Kang 
688045e3678SYuan Kang static void ahash_done_ctx_dst(struct device *jrdev, u32 *desc, u32 err,
689045e3678SYuan Kang 			       void *context)
690045e3678SYuan Kang {
691045e3678SYuan Kang 	struct ahash_request *req = context;
692045e3678SYuan Kang 	struct ahash_edesc *edesc;
693045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
694045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
695045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
696045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
6971984aaeeSHoria Geantă 	int ecode = 0;
698045e3678SYuan Kang 
6996e005503SSascha Hauer 	dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
700045e3678SYuan Kang 
7014ca7c7d8SHoria Geantă 	edesc = container_of(desc, struct ahash_edesc, hw_desc[0]);
702fa9659cdSMarek Vasut 	if (err)
7031984aaeeSHoria Geantă 		ecode = caam_jr_strstatus(jrdev, err);
704045e3678SYuan Kang 
705ef62b231SHoria Geanta 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_FROM_DEVICE);
706944c3d4dSHoria Geantă 	switch_buf(state);
707045e3678SYuan Kang 	kfree(edesc);
708045e3678SYuan Kang 
7096e005503SSascha Hauer 	print_hex_dump_debug("ctx@"__stringify(__LINE__)": ",
710045e3678SYuan Kang 			     DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
711045e3678SYuan Kang 			     ctx->ctx_len, 1);
712045e3678SYuan Kang 	if (req->result)
7136e005503SSascha Hauer 		print_hex_dump_debug("result@"__stringify(__LINE__)": ",
714045e3678SYuan Kang 				     DUMP_PREFIX_ADDRESS, 16, 4, req->result,
715045e3678SYuan Kang 				     digestsize, 1);
716045e3678SYuan Kang 
7171984aaeeSHoria Geantă 	req->base.complete(&req->base, ecode);
718045e3678SYuan Kang }
719045e3678SYuan Kang 
7205588d039SRussell King /*
7215588d039SRussell King  * Allocate an enhanced descriptor, which contains the hardware descriptor
7225588d039SRussell King  * and space for hardware scatter table containing sg_num entries.
7235588d039SRussell King  */
7245588d039SRussell King static struct ahash_edesc *ahash_edesc_alloc(struct caam_hash_ctx *ctx,
72530a43b44SRussell King 					     int sg_num, u32 *sh_desc,
72630a43b44SRussell King 					     dma_addr_t sh_desc_dma,
72730a43b44SRussell King 					     gfp_t flags)
7285588d039SRussell King {
7295588d039SRussell King 	struct ahash_edesc *edesc;
7305588d039SRussell King 	unsigned int sg_size = sg_num * sizeof(struct sec4_sg_entry);
7315588d039SRussell King 
7325588d039SRussell King 	edesc = kzalloc(sizeof(*edesc) + sg_size, GFP_DMA | flags);
7335588d039SRussell King 	if (!edesc) {
7345588d039SRussell King 		dev_err(ctx->jrdev, "could not allocate extended descriptor\n");
7355588d039SRussell King 		return NULL;
7365588d039SRussell King 	}
7375588d039SRussell King 
73830a43b44SRussell King 	init_job_desc_shared(edesc->hw_desc, sh_desc_dma, desc_len(sh_desc),
73930a43b44SRussell King 			     HDR_SHARE_DEFER | HDR_REVERSE);
74030a43b44SRussell King 
7415588d039SRussell King 	return edesc;
7425588d039SRussell King }
7435588d039SRussell King 
74465cf164aSRussell King static int ahash_edesc_add_src(struct caam_hash_ctx *ctx,
74565cf164aSRussell King 			       struct ahash_edesc *edesc,
74665cf164aSRussell King 			       struct ahash_request *req, int nents,
74765cf164aSRussell King 			       unsigned int first_sg,
74865cf164aSRussell King 			       unsigned int first_bytes, size_t to_hash)
74965cf164aSRussell King {
75065cf164aSRussell King 	dma_addr_t src_dma;
75165cf164aSRussell King 	u32 options;
75265cf164aSRussell King 
75365cf164aSRussell King 	if (nents > 1 || first_sg) {
75465cf164aSRussell King 		struct sec4_sg_entry *sg = edesc->sec4_sg;
755a5e5c133SHoria Geantă 		unsigned int sgsize = sizeof(*sg) *
756a5e5c133SHoria Geantă 				      pad_sg_nents(first_sg + nents);
75765cf164aSRussell King 
758059d73eeSHoria Geantă 		sg_to_sec4_sg_last(req->src, to_hash, sg + first_sg, 0);
75965cf164aSRussell King 
76065cf164aSRussell King 		src_dma = dma_map_single(ctx->jrdev, sg, sgsize, DMA_TO_DEVICE);
76165cf164aSRussell King 		if (dma_mapping_error(ctx->jrdev, src_dma)) {
76265cf164aSRussell King 			dev_err(ctx->jrdev, "unable to map S/G table\n");
76365cf164aSRussell King 			return -ENOMEM;
76465cf164aSRussell King 		}
76565cf164aSRussell King 
76665cf164aSRussell King 		edesc->sec4_sg_bytes = sgsize;
76765cf164aSRussell King 		edesc->sec4_sg_dma = src_dma;
76865cf164aSRussell King 		options = LDST_SGF;
76965cf164aSRussell King 	} else {
77065cf164aSRussell King 		src_dma = sg_dma_address(req->src);
77165cf164aSRussell King 		options = 0;
77265cf164aSRussell King 	}
77365cf164aSRussell King 
77465cf164aSRussell King 	append_seq_in_ptr(edesc->hw_desc, src_dma, first_bytes + to_hash,
77565cf164aSRussell King 			  options);
77665cf164aSRussell King 
77765cf164aSRussell King 	return 0;
77865cf164aSRussell King }
77965cf164aSRussell King 
780045e3678SYuan Kang /* submit update job descriptor */
781045e3678SYuan Kang static int ahash_update_ctx(struct ahash_request *req)
782045e3678SYuan Kang {
783045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
784045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
785045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
786045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
787019d62dbSHoria Geantă 	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
788019d62dbSHoria Geantă 		       GFP_KERNEL : GFP_ATOMIC;
7890355d23dSHoria Geantă 	u8 *buf = current_buf(state);
7900355d23dSHoria Geantă 	int *buflen = current_buflen(state);
7910355d23dSHoria Geantă 	u8 *next_buf = alt_buf(state);
79212b8567fSIuliana Prodan 	int blocksize = crypto_ahash_blocksize(ahash);
7930355d23dSHoria Geantă 	int *next_buflen = alt_buflen(state), last_buflen;
794045e3678SYuan Kang 	int in_len = *buflen + req->nbytes, to_hash;
79530a43b44SRussell King 	u32 *desc;
796bc13c69eSRussell King 	int src_nents, mapped_nents, sec4_sg_bytes, sec4_sg_src_index;
797045e3678SYuan Kang 	struct ahash_edesc *edesc;
798045e3678SYuan Kang 	int ret = 0;
799045e3678SYuan Kang 
800045e3678SYuan Kang 	last_buflen = *next_buflen;
80112b8567fSIuliana Prodan 	*next_buflen = in_len & (blocksize - 1);
802045e3678SYuan Kang 	to_hash = in_len - *next_buflen;
803045e3678SYuan Kang 
80412b8567fSIuliana Prodan 	/*
80587870cfbSIuliana Prodan 	 * For XCBC and CMAC, if to_hash is multiple of block size,
80612b8567fSIuliana Prodan 	 * keep last block in internal buffer
80712b8567fSIuliana Prodan 	 */
80887870cfbSIuliana Prodan 	if ((is_xcbc_aes(ctx->adata.algtype) ||
80987870cfbSIuliana Prodan 	     is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize &&
81012b8567fSIuliana Prodan 	     (*next_buflen == 0)) {
81112b8567fSIuliana Prodan 		*next_buflen = blocksize;
81212b8567fSIuliana Prodan 		to_hash -= blocksize;
81312b8567fSIuliana Prodan 	}
81412b8567fSIuliana Prodan 
815045e3678SYuan Kang 	if (to_hash) {
816a5e5c133SHoria Geantă 		int pad_nents;
817059d73eeSHoria Geantă 		int src_len = req->nbytes - *next_buflen;
818a5e5c133SHoria Geantă 
819059d73eeSHoria Geantă 		src_nents = sg_nents_for_len(req->src, src_len);
820f9970c28SLABBE Corentin 		if (src_nents < 0) {
821f9970c28SLABBE Corentin 			dev_err(jrdev, "Invalid number of src SG.\n");
822f9970c28SLABBE Corentin 			return src_nents;
823f9970c28SLABBE Corentin 		}
824bc13c69eSRussell King 
825bc13c69eSRussell King 		if (src_nents) {
826bc13c69eSRussell King 			mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
827bc13c69eSRussell King 						  DMA_TO_DEVICE);
828bc13c69eSRussell King 			if (!mapped_nents) {
829bc13c69eSRussell King 				dev_err(jrdev, "unable to DMA map source\n");
830bc13c69eSRussell King 				return -ENOMEM;
831bc13c69eSRussell King 			}
832bc13c69eSRussell King 		} else {
833bc13c69eSRussell King 			mapped_nents = 0;
834bc13c69eSRussell King 		}
835bc13c69eSRussell King 
836045e3678SYuan Kang 		sec4_sg_src_index = 1 + (*buflen ? 1 : 0);
837a5e5c133SHoria Geantă 		pad_nents = pad_sg_nents(sec4_sg_src_index + mapped_nents);
838a5e5c133SHoria Geantă 		sec4_sg_bytes = pad_nents * sizeof(struct sec4_sg_entry);
839045e3678SYuan Kang 
840045e3678SYuan Kang 		/*
841045e3678SYuan Kang 		 * allocate space for base edesc and hw desc commands,
842045e3678SYuan Kang 		 * link tables
843045e3678SYuan Kang 		 */
844a5e5c133SHoria Geantă 		edesc = ahash_edesc_alloc(ctx, pad_nents, ctx->sh_desc_update,
84530a43b44SRussell King 					  ctx->sh_desc_update_dma, flags);
846045e3678SYuan Kang 		if (!edesc) {
847bc13c69eSRussell King 			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
848045e3678SYuan Kang 			return -ENOMEM;
849045e3678SYuan Kang 		}
850045e3678SYuan Kang 
851045e3678SYuan Kang 		edesc->src_nents = src_nents;
852045e3678SYuan Kang 		edesc->sec4_sg_bytes = sec4_sg_bytes;
853045e3678SYuan Kang 
854dfcd8393SHoria Geantă 		ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
855045e3678SYuan Kang 					 edesc->sec4_sg, DMA_BIDIRECTIONAL);
856ce572085SHoria Geanta 		if (ret)
85758b0e5d0SMarkus Elfring 			goto unmap_ctx;
858045e3678SYuan Kang 
859944c3d4dSHoria Geantă 		ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
860944c3d4dSHoria Geantă 		if (ret)
861944c3d4dSHoria Geantă 			goto unmap_ctx;
862045e3678SYuan Kang 
863b4e9e931SIuliana Prodan 		if (mapped_nents)
864059d73eeSHoria Geantă 			sg_to_sec4_sg_last(req->src, src_len,
865bc13c69eSRussell King 					   edesc->sec4_sg + sec4_sg_src_index,
866bc13c69eSRussell King 					   0);
867b4e9e931SIuliana Prodan 		else
868b4e9e931SIuliana Prodan 			sg_to_sec4_set_last(edesc->sec4_sg + sec4_sg_src_index -
869b4e9e931SIuliana Prodan 					    1);
870b4e9e931SIuliana Prodan 
8718af7b0f8SVictoria Milhoan 		if (*next_buflen)
872307fd543SCristian Stoica 			scatterwalk_map_and_copy(next_buf, req->src,
873307fd543SCristian Stoica 						 to_hash - *buflen,
874307fd543SCristian Stoica 						 *next_buflen, 0);
875045e3678SYuan Kang 		desc = edesc->hw_desc;
876045e3678SYuan Kang 
8771da2be33SRuchika Gupta 		edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
8781da2be33SRuchika Gupta 						     sec4_sg_bytes,
8791da2be33SRuchika Gupta 						     DMA_TO_DEVICE);
880ce572085SHoria Geanta 		if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
881ce572085SHoria Geanta 			dev_err(jrdev, "unable to map S/G table\n");
88232686d34SRussell King 			ret = -ENOMEM;
88358b0e5d0SMarkus Elfring 			goto unmap_ctx;
884ce572085SHoria Geanta 		}
8851da2be33SRuchika Gupta 
886045e3678SYuan Kang 		append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len +
887045e3678SYuan Kang 				       to_hash, LDST_SGF);
888045e3678SYuan Kang 
889045e3678SYuan Kang 		append_seq_out_ptr(desc, state->ctx_dma, ctx->ctx_len, 0);
890045e3678SYuan Kang 
8916e005503SSascha Hauer 		print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
892045e3678SYuan Kang 				     DUMP_PREFIX_ADDRESS, 16, 4, desc,
893045e3678SYuan Kang 				     desc_bytes(desc), 1);
894045e3678SYuan Kang 
895045e3678SYuan Kang 		ret = caam_jr_enqueue(jrdev, desc, ahash_done_bi, req);
89632686d34SRussell King 		if (ret)
89758b0e5d0SMarkus Elfring 			goto unmap_ctx;
89832686d34SRussell King 
899045e3678SYuan Kang 		ret = -EINPROGRESS;
900045e3678SYuan Kang 	} else if (*next_buflen) {
901307fd543SCristian Stoica 		scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
902307fd543SCristian Stoica 					 req->nbytes, 0);
903045e3678SYuan Kang 		*buflen = *next_buflen;
904045e3678SYuan Kang 		*next_buflen = last_buflen;
905045e3678SYuan Kang 	}
9066e005503SSascha Hauer 
9076e005503SSascha Hauer 	print_hex_dump_debug("buf@"__stringify(__LINE__)": ",
908045e3678SYuan Kang 			     DUMP_PREFIX_ADDRESS, 16, 4, buf, *buflen, 1);
9096e005503SSascha Hauer 	print_hex_dump_debug("next buf@"__stringify(__LINE__)": ",
910045e3678SYuan Kang 			     DUMP_PREFIX_ADDRESS, 16, 4, next_buf,
911045e3678SYuan Kang 			     *next_buflen, 1);
912045e3678SYuan Kang 
913045e3678SYuan Kang 	return ret;
91458b0e5d0SMarkus Elfring unmap_ctx:
91532686d34SRussell King 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_BIDIRECTIONAL);
91632686d34SRussell King 	kfree(edesc);
91732686d34SRussell King 	return ret;
918045e3678SYuan Kang }
919045e3678SYuan Kang 
920045e3678SYuan Kang static int ahash_final_ctx(struct ahash_request *req)
921045e3678SYuan Kang {
922045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
923045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
924045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
925045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
926019d62dbSHoria Geantă 	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
927019d62dbSHoria Geantă 		       GFP_KERNEL : GFP_ATOMIC;
9280355d23dSHoria Geantă 	int buflen = *current_buflen(state);
92930a43b44SRussell King 	u32 *desc;
930a5e5c133SHoria Geantă 	int sec4_sg_bytes;
931045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
932045e3678SYuan Kang 	struct ahash_edesc *edesc;
9339e6df0fdSMarkus Elfring 	int ret;
934045e3678SYuan Kang 
935a5e5c133SHoria Geantă 	sec4_sg_bytes = pad_sg_nents(1 + (buflen ? 1 : 0)) *
936a5e5c133SHoria Geantă 			sizeof(struct sec4_sg_entry);
937045e3678SYuan Kang 
938045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
939a5e5c133SHoria Geantă 	edesc = ahash_edesc_alloc(ctx, 4, ctx->sh_desc_fin,
940a5e5c133SHoria Geantă 				  ctx->sh_desc_fin_dma, flags);
9415588d039SRussell King 	if (!edesc)
942045e3678SYuan Kang 		return -ENOMEM;
943045e3678SYuan Kang 
944045e3678SYuan Kang 	desc = edesc->hw_desc;
945045e3678SYuan Kang 
946045e3678SYuan Kang 	edesc->sec4_sg_bytes = sec4_sg_bytes;
947045e3678SYuan Kang 
948dfcd8393SHoria Geantă 	ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
949c19650d6SHoria Geantă 				 edesc->sec4_sg, DMA_BIDIRECTIONAL);
950ce572085SHoria Geanta 	if (ret)
95158b0e5d0SMarkus Elfring 		goto unmap_ctx;
952045e3678SYuan Kang 
953944c3d4dSHoria Geantă 	ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
954944c3d4dSHoria Geantă 	if (ret)
955944c3d4dSHoria Geantă 		goto unmap_ctx;
956944c3d4dSHoria Geantă 
957a5e5c133SHoria Geantă 	sg_to_sec4_set_last(edesc->sec4_sg + (buflen ? 1 : 0));
958045e3678SYuan Kang 
9591da2be33SRuchika Gupta 	edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
9601da2be33SRuchika Gupta 					    sec4_sg_bytes, DMA_TO_DEVICE);
961ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
962ce572085SHoria Geanta 		dev_err(jrdev, "unable to map S/G table\n");
96332686d34SRussell King 		ret = -ENOMEM;
96458b0e5d0SMarkus Elfring 		goto unmap_ctx;
965ce572085SHoria Geanta 	}
9661da2be33SRuchika Gupta 
967045e3678SYuan Kang 	append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len + buflen,
968045e3678SYuan Kang 			  LDST_SGF);
969c19650d6SHoria Geantă 	append_seq_out_ptr(desc, state->ctx_dma, digestsize, 0);
970045e3678SYuan Kang 
9716e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
9726e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
9736e005503SSascha Hauer 			     1);
974045e3678SYuan Kang 
975045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_src, req);
97632686d34SRussell King 	if (ret)
97758b0e5d0SMarkus Elfring 		goto unmap_ctx;
97832686d34SRussell King 
97932686d34SRussell King 	return -EINPROGRESS;
98058b0e5d0SMarkus Elfring  unmap_ctx:
981c19650d6SHoria Geantă 	ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL);
982045e3678SYuan Kang 	kfree(edesc);
983045e3678SYuan Kang 	return ret;
984045e3678SYuan Kang }
985045e3678SYuan Kang 
986045e3678SYuan Kang static int ahash_finup_ctx(struct ahash_request *req)
987045e3678SYuan Kang {
988045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
989045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
990045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
991045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
992019d62dbSHoria Geantă 	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
993019d62dbSHoria Geantă 		       GFP_KERNEL : GFP_ATOMIC;
9940355d23dSHoria Geantă 	int buflen = *current_buflen(state);
99530a43b44SRussell King 	u32 *desc;
99665cf164aSRussell King 	int sec4_sg_src_index;
997bc13c69eSRussell King 	int src_nents, mapped_nents;
998045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
999045e3678SYuan Kang 	struct ahash_edesc *edesc;
10009e6df0fdSMarkus Elfring 	int ret;
1001045e3678SYuan Kang 
100213fb8fd7SLABBE Corentin 	src_nents = sg_nents_for_len(req->src, req->nbytes);
1003f9970c28SLABBE Corentin 	if (src_nents < 0) {
1004f9970c28SLABBE Corentin 		dev_err(jrdev, "Invalid number of src SG.\n");
1005f9970c28SLABBE Corentin 		return src_nents;
1006f9970c28SLABBE Corentin 	}
1007bc13c69eSRussell King 
1008bc13c69eSRussell King 	if (src_nents) {
1009bc13c69eSRussell King 		mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1010bc13c69eSRussell King 					  DMA_TO_DEVICE);
1011bc13c69eSRussell King 		if (!mapped_nents) {
1012bc13c69eSRussell King 			dev_err(jrdev, "unable to DMA map source\n");
1013bc13c69eSRussell King 			return -ENOMEM;
1014bc13c69eSRussell King 		}
1015bc13c69eSRussell King 	} else {
1016bc13c69eSRussell King 		mapped_nents = 0;
1017bc13c69eSRussell King 	}
1018bc13c69eSRussell King 
1019045e3678SYuan Kang 	sec4_sg_src_index = 1 + (buflen ? 1 : 0);
1020045e3678SYuan Kang 
1021045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
10225588d039SRussell King 	edesc = ahash_edesc_alloc(ctx, sec4_sg_src_index + mapped_nents,
10239a1a1c08SHoria Geantă 				  ctx->sh_desc_fin, ctx->sh_desc_fin_dma,
10245588d039SRussell King 				  flags);
1025045e3678SYuan Kang 	if (!edesc) {
1026bc13c69eSRussell King 		dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1027045e3678SYuan Kang 		return -ENOMEM;
1028045e3678SYuan Kang 	}
1029045e3678SYuan Kang 
1030045e3678SYuan Kang 	desc = edesc->hw_desc;
1031045e3678SYuan Kang 
1032045e3678SYuan Kang 	edesc->src_nents = src_nents;
1033045e3678SYuan Kang 
1034dfcd8393SHoria Geantă 	ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
1035c19650d6SHoria Geantă 				 edesc->sec4_sg, DMA_BIDIRECTIONAL);
1036ce572085SHoria Geanta 	if (ret)
103758b0e5d0SMarkus Elfring 		goto unmap_ctx;
1038045e3678SYuan Kang 
1039944c3d4dSHoria Geantă 	ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
1040944c3d4dSHoria Geantă 	if (ret)
1041944c3d4dSHoria Geantă 		goto unmap_ctx;
1042045e3678SYuan Kang 
104365cf164aSRussell King 	ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents,
104465cf164aSRussell King 				  sec4_sg_src_index, ctx->ctx_len + buflen,
104565cf164aSRussell King 				  req->nbytes);
104665cf164aSRussell King 	if (ret)
104758b0e5d0SMarkus Elfring 		goto unmap_ctx;
1048045e3678SYuan Kang 
1049c19650d6SHoria Geantă 	append_seq_out_ptr(desc, state->ctx_dma, digestsize, 0);
1050045e3678SYuan Kang 
10516e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
10526e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
10536e005503SSascha Hauer 			     1);
1054045e3678SYuan Kang 
1055045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_src, req);
105632686d34SRussell King 	if (ret)
105758b0e5d0SMarkus Elfring 		goto unmap_ctx;
105832686d34SRussell King 
105932686d34SRussell King 	return -EINPROGRESS;
106058b0e5d0SMarkus Elfring  unmap_ctx:
1061c19650d6SHoria Geantă 	ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL);
1062045e3678SYuan Kang 	kfree(edesc);
1063045e3678SYuan Kang 	return ret;
1064045e3678SYuan Kang }
1065045e3678SYuan Kang 
1066045e3678SYuan Kang static int ahash_digest(struct ahash_request *req)
1067045e3678SYuan Kang {
1068045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1069045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1070944c3d4dSHoria Geantă 	struct caam_hash_state *state = ahash_request_ctx(req);
1071045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
1072019d62dbSHoria Geantă 	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
1073019d62dbSHoria Geantă 		       GFP_KERNEL : GFP_ATOMIC;
107430a43b44SRussell King 	u32 *desc;
1075045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
107665cf164aSRussell King 	int src_nents, mapped_nents;
1077045e3678SYuan Kang 	struct ahash_edesc *edesc;
10789e6df0fdSMarkus Elfring 	int ret;
1079045e3678SYuan Kang 
1080944c3d4dSHoria Geantă 	state->buf_dma = 0;
1081944c3d4dSHoria Geantă 
10823d5a2db6SRussell King 	src_nents = sg_nents_for_len(req->src, req->nbytes);
1083f9970c28SLABBE Corentin 	if (src_nents < 0) {
1084f9970c28SLABBE Corentin 		dev_err(jrdev, "Invalid number of src SG.\n");
1085f9970c28SLABBE Corentin 		return src_nents;
1086f9970c28SLABBE Corentin 	}
1087bc13c69eSRussell King 
1088bc13c69eSRussell King 	if (src_nents) {
1089bc13c69eSRussell King 		mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1090bc13c69eSRussell King 					  DMA_TO_DEVICE);
1091bc13c69eSRussell King 		if (!mapped_nents) {
1092bc13c69eSRussell King 			dev_err(jrdev, "unable to map source for DMA\n");
1093bc13c69eSRussell King 			return -ENOMEM;
1094bc13c69eSRussell King 		}
1095bc13c69eSRussell King 	} else {
1096bc13c69eSRussell King 		mapped_nents = 0;
1097bc13c69eSRussell King 	}
1098bc13c69eSRussell King 
1099045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
11005588d039SRussell King 	edesc = ahash_edesc_alloc(ctx, mapped_nents > 1 ? mapped_nents : 0,
110130a43b44SRussell King 				  ctx->sh_desc_digest, ctx->sh_desc_digest_dma,
11025588d039SRussell King 				  flags);
1103045e3678SYuan Kang 	if (!edesc) {
1104bc13c69eSRussell King 		dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1105045e3678SYuan Kang 		return -ENOMEM;
1106045e3678SYuan Kang 	}
1107343e44b1SRussell King 
1108045e3678SYuan Kang 	edesc->src_nents = src_nents;
1109045e3678SYuan Kang 
111065cf164aSRussell King 	ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 0, 0,
111165cf164aSRussell King 				  req->nbytes);
111265cf164aSRussell King 	if (ret) {
111332686d34SRussell King 		ahash_unmap(jrdev, edesc, req, digestsize);
111432686d34SRussell King 		kfree(edesc);
111565cf164aSRussell King 		return ret;
1116ce572085SHoria Geanta 	}
111765cf164aSRussell King 
111865cf164aSRussell King 	desc = edesc->hw_desc;
1119045e3678SYuan Kang 
1120c19650d6SHoria Geantă 	ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
1121c19650d6SHoria Geantă 	if (ret) {
112232686d34SRussell King 		ahash_unmap(jrdev, edesc, req, digestsize);
112332686d34SRussell King 		kfree(edesc);
1124ce572085SHoria Geanta 		return -ENOMEM;
1125ce572085SHoria Geanta 	}
1126045e3678SYuan Kang 
11276e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
11286e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
11296e005503SSascha Hauer 			     1);
1130045e3678SYuan Kang 
1131045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, ahash_done, req);
1132045e3678SYuan Kang 	if (!ret) {
1133045e3678SYuan Kang 		ret = -EINPROGRESS;
1134045e3678SYuan Kang 	} else {
1135c19650d6SHoria Geantă 		ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
1136045e3678SYuan Kang 		kfree(edesc);
1137045e3678SYuan Kang 	}
1138045e3678SYuan Kang 
1139045e3678SYuan Kang 	return ret;
1140045e3678SYuan Kang }
1141045e3678SYuan Kang 
1142045e3678SYuan Kang /* submit ahash final if it the first job descriptor */
1143045e3678SYuan Kang static int ahash_final_no_ctx(struct ahash_request *req)
1144045e3678SYuan Kang {
1145045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1146045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1147045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1148045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
1149019d62dbSHoria Geantă 	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
1150019d62dbSHoria Geantă 		       GFP_KERNEL : GFP_ATOMIC;
11510355d23dSHoria Geantă 	u8 *buf = current_buf(state);
11520355d23dSHoria Geantă 	int buflen = *current_buflen(state);
115330a43b44SRussell King 	u32 *desc;
1154045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
1155045e3678SYuan Kang 	struct ahash_edesc *edesc;
11569e6df0fdSMarkus Elfring 	int ret;
1157045e3678SYuan Kang 
1158045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
115930a43b44SRussell King 	edesc = ahash_edesc_alloc(ctx, 0, ctx->sh_desc_digest,
116030a43b44SRussell King 				  ctx->sh_desc_digest_dma, flags);
11615588d039SRussell King 	if (!edesc)
1162045e3678SYuan Kang 		return -ENOMEM;
1163045e3678SYuan Kang 
1164045e3678SYuan Kang 	desc = edesc->hw_desc;
1165045e3678SYuan Kang 
116604e6d25cSAymen Sghaier 	if (buflen) {
116704e6d25cSAymen Sghaier 		state->buf_dma = dma_map_single(jrdev, buf, buflen,
116804e6d25cSAymen Sghaier 						DMA_TO_DEVICE);
1169ce572085SHoria Geanta 		if (dma_mapping_error(jrdev, state->buf_dma)) {
1170ce572085SHoria Geanta 			dev_err(jrdev, "unable to map src\n");
117106435f34SMarkus Elfring 			goto unmap;
1172ce572085SHoria Geanta 		}
1173045e3678SYuan Kang 
1174045e3678SYuan Kang 		append_seq_in_ptr(desc, state->buf_dma, buflen, 0);
117504e6d25cSAymen Sghaier 	}
1176045e3678SYuan Kang 
1177c19650d6SHoria Geantă 	ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
1178c19650d6SHoria Geantă 	if (ret)
117906435f34SMarkus Elfring 		goto unmap;
1180045e3678SYuan Kang 
11816e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
11826e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
11836e005503SSascha Hauer 			     1);
1184045e3678SYuan Kang 
1185045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, ahash_done, req);
1186045e3678SYuan Kang 	if (!ret) {
1187045e3678SYuan Kang 		ret = -EINPROGRESS;
1188045e3678SYuan Kang 	} else {
1189c19650d6SHoria Geantă 		ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
1190045e3678SYuan Kang 		kfree(edesc);
1191045e3678SYuan Kang 	}
1192045e3678SYuan Kang 
1193045e3678SYuan Kang 	return ret;
119406435f34SMarkus Elfring  unmap:
119506435f34SMarkus Elfring 	ahash_unmap(jrdev, edesc, req, digestsize);
119606435f34SMarkus Elfring 	kfree(edesc);
119706435f34SMarkus Elfring 	return -ENOMEM;
119806435f34SMarkus Elfring 
1199045e3678SYuan Kang }
1200045e3678SYuan Kang 
1201045e3678SYuan Kang /* submit ahash update if it the first job descriptor after update */
1202045e3678SYuan Kang static int ahash_update_no_ctx(struct ahash_request *req)
1203045e3678SYuan Kang {
1204045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1205045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1206045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1207045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
1208019d62dbSHoria Geantă 	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
1209019d62dbSHoria Geantă 		       GFP_KERNEL : GFP_ATOMIC;
12100355d23dSHoria Geantă 	u8 *buf = current_buf(state);
12110355d23dSHoria Geantă 	int *buflen = current_buflen(state);
121212b8567fSIuliana Prodan 	int blocksize = crypto_ahash_blocksize(ahash);
12130355d23dSHoria Geantă 	u8 *next_buf = alt_buf(state);
12140355d23dSHoria Geantă 	int *next_buflen = alt_buflen(state);
1215045e3678SYuan Kang 	int in_len = *buflen + req->nbytes, to_hash;
1216bc13c69eSRussell King 	int sec4_sg_bytes, src_nents, mapped_nents;
1217045e3678SYuan Kang 	struct ahash_edesc *edesc;
121830a43b44SRussell King 	u32 *desc;
1219045e3678SYuan Kang 	int ret = 0;
1220045e3678SYuan Kang 
122112b8567fSIuliana Prodan 	*next_buflen = in_len & (blocksize - 1);
1222045e3678SYuan Kang 	to_hash = in_len - *next_buflen;
1223045e3678SYuan Kang 
122412b8567fSIuliana Prodan 	/*
122587870cfbSIuliana Prodan 	 * For XCBC and CMAC, if to_hash is multiple of block size,
122612b8567fSIuliana Prodan 	 * keep last block in internal buffer
122712b8567fSIuliana Prodan 	 */
122887870cfbSIuliana Prodan 	if ((is_xcbc_aes(ctx->adata.algtype) ||
122987870cfbSIuliana Prodan 	     is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize &&
123012b8567fSIuliana Prodan 	     (*next_buflen == 0)) {
123112b8567fSIuliana Prodan 		*next_buflen = blocksize;
123212b8567fSIuliana Prodan 		to_hash -= blocksize;
123312b8567fSIuliana Prodan 	}
123412b8567fSIuliana Prodan 
1235045e3678SYuan Kang 	if (to_hash) {
1236a5e5c133SHoria Geantă 		int pad_nents;
1237059d73eeSHoria Geantă 		int src_len = req->nbytes - *next_buflen;
1238a5e5c133SHoria Geantă 
1239059d73eeSHoria Geantă 		src_nents = sg_nents_for_len(req->src, src_len);
1240f9970c28SLABBE Corentin 		if (src_nents < 0) {
1241f9970c28SLABBE Corentin 			dev_err(jrdev, "Invalid number of src SG.\n");
1242f9970c28SLABBE Corentin 			return src_nents;
1243f9970c28SLABBE Corentin 		}
1244bc13c69eSRussell King 
1245bc13c69eSRussell King 		if (src_nents) {
1246bc13c69eSRussell King 			mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1247bc13c69eSRussell King 						  DMA_TO_DEVICE);
1248bc13c69eSRussell King 			if (!mapped_nents) {
1249bc13c69eSRussell King 				dev_err(jrdev, "unable to DMA map source\n");
1250bc13c69eSRussell King 				return -ENOMEM;
1251bc13c69eSRussell King 			}
1252bc13c69eSRussell King 		} else {
1253bc13c69eSRussell King 			mapped_nents = 0;
1254bc13c69eSRussell King 		}
1255bc13c69eSRussell King 
1256a5e5c133SHoria Geantă 		pad_nents = pad_sg_nents(1 + mapped_nents);
1257a5e5c133SHoria Geantă 		sec4_sg_bytes = pad_nents * sizeof(struct sec4_sg_entry);
1258045e3678SYuan Kang 
1259045e3678SYuan Kang 		/*
1260045e3678SYuan Kang 		 * allocate space for base edesc and hw desc commands,
1261045e3678SYuan Kang 		 * link tables
1262045e3678SYuan Kang 		 */
1263a5e5c133SHoria Geantă 		edesc = ahash_edesc_alloc(ctx, pad_nents,
126430a43b44SRussell King 					  ctx->sh_desc_update_first,
126530a43b44SRussell King 					  ctx->sh_desc_update_first_dma,
126630a43b44SRussell King 					  flags);
1267045e3678SYuan Kang 		if (!edesc) {
1268bc13c69eSRussell King 			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1269045e3678SYuan Kang 			return -ENOMEM;
1270045e3678SYuan Kang 		}
1271045e3678SYuan Kang 
1272045e3678SYuan Kang 		edesc->src_nents = src_nents;
1273045e3678SYuan Kang 		edesc->sec4_sg_bytes = sec4_sg_bytes;
1274045e3678SYuan Kang 
1275944c3d4dSHoria Geantă 		ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg, state);
1276944c3d4dSHoria Geantă 		if (ret)
1277944c3d4dSHoria Geantă 			goto unmap_ctx;
1278944c3d4dSHoria Geantă 
1279059d73eeSHoria Geantă 		sg_to_sec4_sg_last(req->src, src_len, edesc->sec4_sg + 1, 0);
1280bc13c69eSRussell King 
1281045e3678SYuan Kang 		if (*next_buflen) {
1282307fd543SCristian Stoica 			scatterwalk_map_and_copy(next_buf, req->src,
1283307fd543SCristian Stoica 						 to_hash - *buflen,
1284307fd543SCristian Stoica 						 *next_buflen, 0);
1285045e3678SYuan Kang 		}
1286045e3678SYuan Kang 
1287045e3678SYuan Kang 		desc = edesc->hw_desc;
1288045e3678SYuan Kang 
12891da2be33SRuchika Gupta 		edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
12901da2be33SRuchika Gupta 						    sec4_sg_bytes,
12911da2be33SRuchika Gupta 						    DMA_TO_DEVICE);
1292ce572085SHoria Geanta 		if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
1293ce572085SHoria Geanta 			dev_err(jrdev, "unable to map S/G table\n");
129432686d34SRussell King 			ret = -ENOMEM;
129558b0e5d0SMarkus Elfring 			goto unmap_ctx;
1296ce572085SHoria Geanta 		}
12971da2be33SRuchika Gupta 
1298045e3678SYuan Kang 		append_seq_in_ptr(desc, edesc->sec4_sg_dma, to_hash, LDST_SGF);
1299045e3678SYuan Kang 
1300ce572085SHoria Geanta 		ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len);
1301ce572085SHoria Geanta 		if (ret)
130258b0e5d0SMarkus Elfring 			goto unmap_ctx;
1303045e3678SYuan Kang 
13046e005503SSascha Hauer 		print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
1305045e3678SYuan Kang 				     DUMP_PREFIX_ADDRESS, 16, 4, desc,
1306045e3678SYuan Kang 				     desc_bytes(desc), 1);
1307045e3678SYuan Kang 
1308045e3678SYuan Kang 		ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_dst, req);
130932686d34SRussell King 		if (ret)
131058b0e5d0SMarkus Elfring 			goto unmap_ctx;
131132686d34SRussell King 
1312045e3678SYuan Kang 		ret = -EINPROGRESS;
1313045e3678SYuan Kang 		state->update = ahash_update_ctx;
1314045e3678SYuan Kang 		state->finup = ahash_finup_ctx;
1315045e3678SYuan Kang 		state->final = ahash_final_ctx;
1316045e3678SYuan Kang 	} else if (*next_buflen) {
1317307fd543SCristian Stoica 		scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
1318307fd543SCristian Stoica 					 req->nbytes, 0);
1319045e3678SYuan Kang 		*buflen = *next_buflen;
1320045e3678SYuan Kang 		*next_buflen = 0;
1321045e3678SYuan Kang 	}
13226e005503SSascha Hauer 
13236e005503SSascha Hauer 	print_hex_dump_debug("buf@"__stringify(__LINE__)": ",
1324045e3678SYuan Kang 			     DUMP_PREFIX_ADDRESS, 16, 4, buf, *buflen, 1);
13256e005503SSascha Hauer 	print_hex_dump_debug("next buf@"__stringify(__LINE__)": ",
13266e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, next_buf, *next_buflen,
13276e005503SSascha Hauer 			     1);
1328045e3678SYuan Kang 
1329045e3678SYuan Kang 	return ret;
133058b0e5d0SMarkus Elfring  unmap_ctx:
133132686d34SRussell King 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE);
133232686d34SRussell King 	kfree(edesc);
133332686d34SRussell King 	return ret;
1334045e3678SYuan Kang }
1335045e3678SYuan Kang 
1336045e3678SYuan Kang /* submit ahash finup if it the first job descriptor after update */
1337045e3678SYuan Kang static int ahash_finup_no_ctx(struct ahash_request *req)
1338045e3678SYuan Kang {
1339045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1340045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1341045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1342045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
1343019d62dbSHoria Geantă 	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
1344019d62dbSHoria Geantă 		       GFP_KERNEL : GFP_ATOMIC;
13450355d23dSHoria Geantă 	int buflen = *current_buflen(state);
134630a43b44SRussell King 	u32 *desc;
1347bc13c69eSRussell King 	int sec4_sg_bytes, sec4_sg_src_index, src_nents, mapped_nents;
1348045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
1349045e3678SYuan Kang 	struct ahash_edesc *edesc;
13509e6df0fdSMarkus Elfring 	int ret;
1351045e3678SYuan Kang 
135213fb8fd7SLABBE Corentin 	src_nents = sg_nents_for_len(req->src, req->nbytes);
1353f9970c28SLABBE Corentin 	if (src_nents < 0) {
1354f9970c28SLABBE Corentin 		dev_err(jrdev, "Invalid number of src SG.\n");
1355f9970c28SLABBE Corentin 		return src_nents;
1356f9970c28SLABBE Corentin 	}
1357bc13c69eSRussell King 
1358bc13c69eSRussell King 	if (src_nents) {
1359bc13c69eSRussell King 		mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1360bc13c69eSRussell King 					  DMA_TO_DEVICE);
1361bc13c69eSRussell King 		if (!mapped_nents) {
1362bc13c69eSRussell King 			dev_err(jrdev, "unable to DMA map source\n");
1363bc13c69eSRussell King 			return -ENOMEM;
1364bc13c69eSRussell King 		}
1365bc13c69eSRussell King 	} else {
1366bc13c69eSRussell King 		mapped_nents = 0;
1367bc13c69eSRussell King 	}
1368bc13c69eSRussell King 
1369045e3678SYuan Kang 	sec4_sg_src_index = 2;
1370bc13c69eSRussell King 	sec4_sg_bytes = (sec4_sg_src_index + mapped_nents) *
1371045e3678SYuan Kang 			 sizeof(struct sec4_sg_entry);
1372045e3678SYuan Kang 
1373045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
137430a43b44SRussell King 	edesc = ahash_edesc_alloc(ctx, sec4_sg_src_index + mapped_nents,
137530a43b44SRussell King 				  ctx->sh_desc_digest, ctx->sh_desc_digest_dma,
137630a43b44SRussell King 				  flags);
1377045e3678SYuan Kang 	if (!edesc) {
1378bc13c69eSRussell King 		dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1379045e3678SYuan Kang 		return -ENOMEM;
1380045e3678SYuan Kang 	}
1381045e3678SYuan Kang 
1382045e3678SYuan Kang 	desc = edesc->hw_desc;
1383045e3678SYuan Kang 
1384045e3678SYuan Kang 	edesc->src_nents = src_nents;
1385045e3678SYuan Kang 	edesc->sec4_sg_bytes = sec4_sg_bytes;
1386045e3678SYuan Kang 
1387944c3d4dSHoria Geantă 	ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg, state);
1388944c3d4dSHoria Geantă 	if (ret)
1389944c3d4dSHoria Geantă 		goto unmap;
1390045e3678SYuan Kang 
139165cf164aSRussell King 	ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 1, buflen,
139265cf164aSRussell King 				  req->nbytes);
139365cf164aSRussell King 	if (ret) {
1394ce572085SHoria Geanta 		dev_err(jrdev, "unable to map S/G table\n");
139506435f34SMarkus Elfring 		goto unmap;
1396ce572085SHoria Geanta 	}
13971da2be33SRuchika Gupta 
1398c19650d6SHoria Geantă 	ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
1399c19650d6SHoria Geantă 	if (ret)
140006435f34SMarkus Elfring 		goto unmap;
1401045e3678SYuan Kang 
14026e005503SSascha Hauer 	print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
14036e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
14046e005503SSascha Hauer 			     1);
1405045e3678SYuan Kang 
1406045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, ahash_done, req);
1407045e3678SYuan Kang 	if (!ret) {
1408045e3678SYuan Kang 		ret = -EINPROGRESS;
1409045e3678SYuan Kang 	} else {
1410c19650d6SHoria Geantă 		ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
1411045e3678SYuan Kang 		kfree(edesc);
1412045e3678SYuan Kang 	}
1413045e3678SYuan Kang 
1414045e3678SYuan Kang 	return ret;
141506435f34SMarkus Elfring  unmap:
141606435f34SMarkus Elfring 	ahash_unmap(jrdev, edesc, req, digestsize);
141706435f34SMarkus Elfring 	kfree(edesc);
141806435f34SMarkus Elfring 	return -ENOMEM;
141906435f34SMarkus Elfring 
1420045e3678SYuan Kang }
1421045e3678SYuan Kang 
1422045e3678SYuan Kang /* submit first update job descriptor after init */
1423045e3678SYuan Kang static int ahash_update_first(struct ahash_request *req)
1424045e3678SYuan Kang {
1425045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1426045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1427045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1428045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
1429019d62dbSHoria Geantă 	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
1430019d62dbSHoria Geantă 		       GFP_KERNEL : GFP_ATOMIC;
1431944c3d4dSHoria Geantă 	u8 *next_buf = alt_buf(state);
1432944c3d4dSHoria Geantă 	int *next_buflen = alt_buflen(state);
1433045e3678SYuan Kang 	int to_hash;
143412b8567fSIuliana Prodan 	int blocksize = crypto_ahash_blocksize(ahash);
143530a43b44SRussell King 	u32 *desc;
143665cf164aSRussell King 	int src_nents, mapped_nents;
1437045e3678SYuan Kang 	struct ahash_edesc *edesc;
1438045e3678SYuan Kang 	int ret = 0;
1439045e3678SYuan Kang 
144012b8567fSIuliana Prodan 	*next_buflen = req->nbytes & (blocksize - 1);
1441045e3678SYuan Kang 	to_hash = req->nbytes - *next_buflen;
1442045e3678SYuan Kang 
144312b8567fSIuliana Prodan 	/*
144487870cfbSIuliana Prodan 	 * For XCBC and CMAC, if to_hash is multiple of block size,
144512b8567fSIuliana Prodan 	 * keep last block in internal buffer
144612b8567fSIuliana Prodan 	 */
144787870cfbSIuliana Prodan 	if ((is_xcbc_aes(ctx->adata.algtype) ||
144887870cfbSIuliana Prodan 	     is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize &&
144912b8567fSIuliana Prodan 	     (*next_buflen == 0)) {
145012b8567fSIuliana Prodan 		*next_buflen = blocksize;
145112b8567fSIuliana Prodan 		to_hash -= blocksize;
145212b8567fSIuliana Prodan 	}
145312b8567fSIuliana Prodan 
1454045e3678SYuan Kang 	if (to_hash) {
14553d5a2db6SRussell King 		src_nents = sg_nents_for_len(req->src,
14563d5a2db6SRussell King 					     req->nbytes - *next_buflen);
1457f9970c28SLABBE Corentin 		if (src_nents < 0) {
1458f9970c28SLABBE Corentin 			dev_err(jrdev, "Invalid number of src SG.\n");
1459f9970c28SLABBE Corentin 			return src_nents;
1460f9970c28SLABBE Corentin 		}
1461bc13c69eSRussell King 
1462bc13c69eSRussell King 		if (src_nents) {
1463bc13c69eSRussell King 			mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1464bc13c69eSRussell King 						  DMA_TO_DEVICE);
1465bc13c69eSRussell King 			if (!mapped_nents) {
1466bc13c69eSRussell King 				dev_err(jrdev, "unable to map source for DMA\n");
1467bc13c69eSRussell King 				return -ENOMEM;
1468bc13c69eSRussell King 			}
1469bc13c69eSRussell King 		} else {
1470bc13c69eSRussell King 			mapped_nents = 0;
1471bc13c69eSRussell King 		}
1472045e3678SYuan Kang 
1473045e3678SYuan Kang 		/*
1474045e3678SYuan Kang 		 * allocate space for base edesc and hw desc commands,
1475045e3678SYuan Kang 		 * link tables
1476045e3678SYuan Kang 		 */
14775588d039SRussell King 		edesc = ahash_edesc_alloc(ctx, mapped_nents > 1 ?
147830a43b44SRussell King 					  mapped_nents : 0,
147930a43b44SRussell King 					  ctx->sh_desc_update_first,
148030a43b44SRussell King 					  ctx->sh_desc_update_first_dma,
148130a43b44SRussell King 					  flags);
1482045e3678SYuan Kang 		if (!edesc) {
1483bc13c69eSRussell King 			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1484045e3678SYuan Kang 			return -ENOMEM;
1485045e3678SYuan Kang 		}
1486045e3678SYuan Kang 
1487045e3678SYuan Kang 		edesc->src_nents = src_nents;
1488045e3678SYuan Kang 
148965cf164aSRussell King 		ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 0, 0,
149065cf164aSRussell King 					  to_hash);
149165cf164aSRussell King 		if (ret)
149258b0e5d0SMarkus Elfring 			goto unmap_ctx;
1493045e3678SYuan Kang 
1494045e3678SYuan Kang 		if (*next_buflen)
1495307fd543SCristian Stoica 			scatterwalk_map_and_copy(next_buf, req->src, to_hash,
1496307fd543SCristian Stoica 						 *next_buflen, 0);
1497045e3678SYuan Kang 
1498045e3678SYuan Kang 		desc = edesc->hw_desc;
1499045e3678SYuan Kang 
1500ce572085SHoria Geanta 		ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len);
1501ce572085SHoria Geanta 		if (ret)
150258b0e5d0SMarkus Elfring 			goto unmap_ctx;
1503045e3678SYuan Kang 
15046e005503SSascha Hauer 		print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
1505045e3678SYuan Kang 				     DUMP_PREFIX_ADDRESS, 16, 4, desc,
1506045e3678SYuan Kang 				     desc_bytes(desc), 1);
1507045e3678SYuan Kang 
150832686d34SRussell King 		ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_dst, req);
150932686d34SRussell King 		if (ret)
151058b0e5d0SMarkus Elfring 			goto unmap_ctx;
151132686d34SRussell King 
1512045e3678SYuan Kang 		ret = -EINPROGRESS;
1513045e3678SYuan Kang 		state->update = ahash_update_ctx;
1514045e3678SYuan Kang 		state->finup = ahash_finup_ctx;
1515045e3678SYuan Kang 		state->final = ahash_final_ctx;
1516045e3678SYuan Kang 	} else if (*next_buflen) {
1517045e3678SYuan Kang 		state->update = ahash_update_no_ctx;
1518045e3678SYuan Kang 		state->finup = ahash_finup_no_ctx;
1519045e3678SYuan Kang 		state->final = ahash_final_no_ctx;
1520307fd543SCristian Stoica 		scatterwalk_map_and_copy(next_buf, req->src, 0,
1521307fd543SCristian Stoica 					 req->nbytes, 0);
1522944c3d4dSHoria Geantă 		switch_buf(state);
1523045e3678SYuan Kang 	}
15246e005503SSascha Hauer 
15256e005503SSascha Hauer 	print_hex_dump_debug("next buf@"__stringify(__LINE__)": ",
15266e005503SSascha Hauer 			     DUMP_PREFIX_ADDRESS, 16, 4, next_buf, *next_buflen,
15276e005503SSascha Hauer 			     1);
1528045e3678SYuan Kang 
1529045e3678SYuan Kang 	return ret;
153058b0e5d0SMarkus Elfring  unmap_ctx:
153132686d34SRussell King 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE);
153232686d34SRussell King 	kfree(edesc);
153332686d34SRussell King 	return ret;
1534045e3678SYuan Kang }
1535045e3678SYuan Kang 
1536045e3678SYuan Kang static int ahash_finup_first(struct ahash_request *req)
1537045e3678SYuan Kang {
1538045e3678SYuan Kang 	return ahash_digest(req);
1539045e3678SYuan Kang }
1540045e3678SYuan Kang 
1541045e3678SYuan Kang static int ahash_init(struct ahash_request *req)
1542045e3678SYuan Kang {
1543045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1544045e3678SYuan Kang 
1545045e3678SYuan Kang 	state->update = ahash_update_first;
1546045e3678SYuan Kang 	state->finup = ahash_finup_first;
1547045e3678SYuan Kang 	state->final = ahash_final_no_ctx;
1548045e3678SYuan Kang 
154987ec02e7SHoria Geantă 	state->ctx_dma = 0;
155065055e21SFranck LENORMAND 	state->ctx_dma_len = 0;
1551045e3678SYuan Kang 	state->current_buf = 0;
1552de0e35ecSHoria Geanta 	state->buf_dma = 0;
15536fd4b156SSteve Cornelius 	state->buflen_0 = 0;
15546fd4b156SSteve Cornelius 	state->buflen_1 = 0;
1555045e3678SYuan Kang 
1556045e3678SYuan Kang 	return 0;
1557045e3678SYuan Kang }
1558045e3678SYuan Kang 
1559045e3678SYuan Kang static int ahash_update(struct ahash_request *req)
1560045e3678SYuan Kang {
1561045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1562045e3678SYuan Kang 
1563045e3678SYuan Kang 	return state->update(req);
1564045e3678SYuan Kang }
1565045e3678SYuan Kang 
1566045e3678SYuan Kang static int ahash_finup(struct ahash_request *req)
1567045e3678SYuan Kang {
1568045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1569045e3678SYuan Kang 
1570045e3678SYuan Kang 	return state->finup(req);
1571045e3678SYuan Kang }
1572045e3678SYuan Kang 
1573045e3678SYuan Kang static int ahash_final(struct ahash_request *req)
1574045e3678SYuan Kang {
1575045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1576045e3678SYuan Kang 
1577045e3678SYuan Kang 	return state->final(req);
1578045e3678SYuan Kang }
1579045e3678SYuan Kang 
1580045e3678SYuan Kang static int ahash_export(struct ahash_request *req, void *out)
1581045e3678SYuan Kang {
1582045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
15835ec90831SRussell King 	struct caam_export_state *export = out;
15845ec90831SRussell King 	int len;
15855ec90831SRussell King 	u8 *buf;
1586045e3678SYuan Kang 
15875ec90831SRussell King 	if (state->current_buf) {
15885ec90831SRussell King 		buf = state->buf_1;
15895ec90831SRussell King 		len = state->buflen_1;
15905ec90831SRussell King 	} else {
15915ec90831SRussell King 		buf = state->buf_0;
1592f456cd2dSFabio Estevam 		len = state->buflen_0;
15935ec90831SRussell King 	}
15945ec90831SRussell King 
15955ec90831SRussell King 	memcpy(export->buf, buf, len);
15965ec90831SRussell King 	memcpy(export->caam_ctx, state->caam_ctx, sizeof(export->caam_ctx));
15975ec90831SRussell King 	export->buflen = len;
15985ec90831SRussell King 	export->update = state->update;
15995ec90831SRussell King 	export->final = state->final;
16005ec90831SRussell King 	export->finup = state->finup;
1601434b4212SRussell King 
1602045e3678SYuan Kang 	return 0;
1603045e3678SYuan Kang }
1604045e3678SYuan Kang 
1605045e3678SYuan Kang static int ahash_import(struct ahash_request *req, const void *in)
1606045e3678SYuan Kang {
1607045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
16085ec90831SRussell King 	const struct caam_export_state *export = in;
1609045e3678SYuan Kang 
16105ec90831SRussell King 	memset(state, 0, sizeof(*state));
16115ec90831SRussell King 	memcpy(state->buf_0, export->buf, export->buflen);
16125ec90831SRussell King 	memcpy(state->caam_ctx, export->caam_ctx, sizeof(state->caam_ctx));
16135ec90831SRussell King 	state->buflen_0 = export->buflen;
16145ec90831SRussell King 	state->update = export->update;
16155ec90831SRussell King 	state->final = export->final;
16165ec90831SRussell King 	state->finup = export->finup;
1617434b4212SRussell King 
1618045e3678SYuan Kang 	return 0;
1619045e3678SYuan Kang }
1620045e3678SYuan Kang 
1621045e3678SYuan Kang struct caam_hash_template {
1622045e3678SYuan Kang 	char name[CRYPTO_MAX_ALG_NAME];
1623045e3678SYuan Kang 	char driver_name[CRYPTO_MAX_ALG_NAME];
1624b0e09baeSYuan Kang 	char hmac_name[CRYPTO_MAX_ALG_NAME];
1625b0e09baeSYuan Kang 	char hmac_driver_name[CRYPTO_MAX_ALG_NAME];
1626045e3678SYuan Kang 	unsigned int blocksize;
1627045e3678SYuan Kang 	struct ahash_alg template_ahash;
1628045e3678SYuan Kang 	u32 alg_type;
1629045e3678SYuan Kang };
1630045e3678SYuan Kang 
1631045e3678SYuan Kang /* ahash descriptors */
1632045e3678SYuan Kang static struct caam_hash_template driver_hash[] = {
1633045e3678SYuan Kang 	{
1634b0e09baeSYuan Kang 		.name = "sha1",
1635b0e09baeSYuan Kang 		.driver_name = "sha1-caam",
1636b0e09baeSYuan Kang 		.hmac_name = "hmac(sha1)",
1637b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha1-caam",
1638045e3678SYuan Kang 		.blocksize = SHA1_BLOCK_SIZE,
1639045e3678SYuan Kang 		.template_ahash = {
1640045e3678SYuan Kang 			.init = ahash_init,
1641045e3678SYuan Kang 			.update = ahash_update,
1642045e3678SYuan Kang 			.final = ahash_final,
1643045e3678SYuan Kang 			.finup = ahash_finup,
1644045e3678SYuan Kang 			.digest = ahash_digest,
1645045e3678SYuan Kang 			.export = ahash_export,
1646045e3678SYuan Kang 			.import = ahash_import,
1647045e3678SYuan Kang 			.setkey = ahash_setkey,
1648045e3678SYuan Kang 			.halg = {
1649045e3678SYuan Kang 				.digestsize = SHA1_DIGEST_SIZE,
16505ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1651045e3678SYuan Kang 			},
1652045e3678SYuan Kang 		},
1653045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA1,
1654045e3678SYuan Kang 	}, {
1655b0e09baeSYuan Kang 		.name = "sha224",
1656b0e09baeSYuan Kang 		.driver_name = "sha224-caam",
1657b0e09baeSYuan Kang 		.hmac_name = "hmac(sha224)",
1658b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha224-caam",
1659045e3678SYuan Kang 		.blocksize = SHA224_BLOCK_SIZE,
1660045e3678SYuan Kang 		.template_ahash = {
1661045e3678SYuan Kang 			.init = ahash_init,
1662045e3678SYuan Kang 			.update = ahash_update,
1663045e3678SYuan Kang 			.final = ahash_final,
1664045e3678SYuan Kang 			.finup = ahash_finup,
1665045e3678SYuan Kang 			.digest = ahash_digest,
1666045e3678SYuan Kang 			.export = ahash_export,
1667045e3678SYuan Kang 			.import = ahash_import,
1668045e3678SYuan Kang 			.setkey = ahash_setkey,
1669045e3678SYuan Kang 			.halg = {
1670045e3678SYuan Kang 				.digestsize = SHA224_DIGEST_SIZE,
16715ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1672045e3678SYuan Kang 			},
1673045e3678SYuan Kang 		},
1674045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA224,
1675045e3678SYuan Kang 	}, {
1676b0e09baeSYuan Kang 		.name = "sha256",
1677b0e09baeSYuan Kang 		.driver_name = "sha256-caam",
1678b0e09baeSYuan Kang 		.hmac_name = "hmac(sha256)",
1679b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha256-caam",
1680045e3678SYuan Kang 		.blocksize = SHA256_BLOCK_SIZE,
1681045e3678SYuan Kang 		.template_ahash = {
1682045e3678SYuan Kang 			.init = ahash_init,
1683045e3678SYuan Kang 			.update = ahash_update,
1684045e3678SYuan Kang 			.final = ahash_final,
1685045e3678SYuan Kang 			.finup = ahash_finup,
1686045e3678SYuan Kang 			.digest = ahash_digest,
1687045e3678SYuan Kang 			.export = ahash_export,
1688045e3678SYuan Kang 			.import = ahash_import,
1689045e3678SYuan Kang 			.setkey = ahash_setkey,
1690045e3678SYuan Kang 			.halg = {
1691045e3678SYuan Kang 				.digestsize = SHA256_DIGEST_SIZE,
16925ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1693045e3678SYuan Kang 			},
1694045e3678SYuan Kang 		},
1695045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA256,
1696045e3678SYuan Kang 	}, {
1697b0e09baeSYuan Kang 		.name = "sha384",
1698b0e09baeSYuan Kang 		.driver_name = "sha384-caam",
1699b0e09baeSYuan Kang 		.hmac_name = "hmac(sha384)",
1700b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha384-caam",
1701045e3678SYuan Kang 		.blocksize = SHA384_BLOCK_SIZE,
1702045e3678SYuan Kang 		.template_ahash = {
1703045e3678SYuan Kang 			.init = ahash_init,
1704045e3678SYuan Kang 			.update = ahash_update,
1705045e3678SYuan Kang 			.final = ahash_final,
1706045e3678SYuan Kang 			.finup = ahash_finup,
1707045e3678SYuan Kang 			.digest = ahash_digest,
1708045e3678SYuan Kang 			.export = ahash_export,
1709045e3678SYuan Kang 			.import = ahash_import,
1710045e3678SYuan Kang 			.setkey = ahash_setkey,
1711045e3678SYuan Kang 			.halg = {
1712045e3678SYuan Kang 				.digestsize = SHA384_DIGEST_SIZE,
17135ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1714045e3678SYuan Kang 			},
1715045e3678SYuan Kang 		},
1716045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA384,
1717045e3678SYuan Kang 	}, {
1718b0e09baeSYuan Kang 		.name = "sha512",
1719b0e09baeSYuan Kang 		.driver_name = "sha512-caam",
1720b0e09baeSYuan Kang 		.hmac_name = "hmac(sha512)",
1721b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha512-caam",
1722045e3678SYuan Kang 		.blocksize = SHA512_BLOCK_SIZE,
1723045e3678SYuan Kang 		.template_ahash = {
1724045e3678SYuan Kang 			.init = ahash_init,
1725045e3678SYuan Kang 			.update = ahash_update,
1726045e3678SYuan Kang 			.final = ahash_final,
1727045e3678SYuan Kang 			.finup = ahash_finup,
1728045e3678SYuan Kang 			.digest = ahash_digest,
1729045e3678SYuan Kang 			.export = ahash_export,
1730045e3678SYuan Kang 			.import = ahash_import,
1731045e3678SYuan Kang 			.setkey = ahash_setkey,
1732045e3678SYuan Kang 			.halg = {
1733045e3678SYuan Kang 				.digestsize = SHA512_DIGEST_SIZE,
17345ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1735045e3678SYuan Kang 			},
1736045e3678SYuan Kang 		},
1737045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA512,
1738045e3678SYuan Kang 	}, {
1739b0e09baeSYuan Kang 		.name = "md5",
1740b0e09baeSYuan Kang 		.driver_name = "md5-caam",
1741b0e09baeSYuan Kang 		.hmac_name = "hmac(md5)",
1742b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-md5-caam",
1743045e3678SYuan Kang 		.blocksize = MD5_BLOCK_WORDS * 4,
1744045e3678SYuan Kang 		.template_ahash = {
1745045e3678SYuan Kang 			.init = ahash_init,
1746045e3678SYuan Kang 			.update = ahash_update,
1747045e3678SYuan Kang 			.final = ahash_final,
1748045e3678SYuan Kang 			.finup = ahash_finup,
1749045e3678SYuan Kang 			.digest = ahash_digest,
1750045e3678SYuan Kang 			.export = ahash_export,
1751045e3678SYuan Kang 			.import = ahash_import,
1752045e3678SYuan Kang 			.setkey = ahash_setkey,
1753045e3678SYuan Kang 			.halg = {
1754045e3678SYuan Kang 				.digestsize = MD5_DIGEST_SIZE,
17555ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1756045e3678SYuan Kang 			},
1757045e3678SYuan Kang 		},
1758045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_MD5,
175912b8567fSIuliana Prodan 	}, {
176012b8567fSIuliana Prodan 		.hmac_name = "xcbc(aes)",
176112b8567fSIuliana Prodan 		.hmac_driver_name = "xcbc-aes-caam",
176212b8567fSIuliana Prodan 		.blocksize = AES_BLOCK_SIZE,
176312b8567fSIuliana Prodan 		.template_ahash = {
176412b8567fSIuliana Prodan 			.init = ahash_init,
176512b8567fSIuliana Prodan 			.update = ahash_update,
176612b8567fSIuliana Prodan 			.final = ahash_final,
176712b8567fSIuliana Prodan 			.finup = ahash_finup,
176812b8567fSIuliana Prodan 			.digest = ahash_digest,
176912b8567fSIuliana Prodan 			.export = ahash_export,
177012b8567fSIuliana Prodan 			.import = ahash_import,
177112b8567fSIuliana Prodan 			.setkey = axcbc_setkey,
177212b8567fSIuliana Prodan 			.halg = {
177312b8567fSIuliana Prodan 				.digestsize = AES_BLOCK_SIZE,
177412b8567fSIuliana Prodan 				.statesize = sizeof(struct caam_export_state),
177512b8567fSIuliana Prodan 			},
177612b8567fSIuliana Prodan 		 },
177712b8567fSIuliana Prodan 		.alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_XCBC_MAC,
177887870cfbSIuliana Prodan 	}, {
177987870cfbSIuliana Prodan 		.hmac_name = "cmac(aes)",
178087870cfbSIuliana Prodan 		.hmac_driver_name = "cmac-aes-caam",
178187870cfbSIuliana Prodan 		.blocksize = AES_BLOCK_SIZE,
178287870cfbSIuliana Prodan 		.template_ahash = {
178387870cfbSIuliana Prodan 			.init = ahash_init,
178487870cfbSIuliana Prodan 			.update = ahash_update,
178587870cfbSIuliana Prodan 			.final = ahash_final,
178687870cfbSIuliana Prodan 			.finup = ahash_finup,
178787870cfbSIuliana Prodan 			.digest = ahash_digest,
178887870cfbSIuliana Prodan 			.export = ahash_export,
178987870cfbSIuliana Prodan 			.import = ahash_import,
179087870cfbSIuliana Prodan 			.setkey = acmac_setkey,
179187870cfbSIuliana Prodan 			.halg = {
179287870cfbSIuliana Prodan 				.digestsize = AES_BLOCK_SIZE,
179387870cfbSIuliana Prodan 				.statesize = sizeof(struct caam_export_state),
179487870cfbSIuliana Prodan 			},
179587870cfbSIuliana Prodan 		 },
179687870cfbSIuliana Prodan 		.alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CMAC,
1797045e3678SYuan Kang 	},
1798045e3678SYuan Kang };
1799045e3678SYuan Kang 
1800045e3678SYuan Kang struct caam_hash_alg {
1801045e3678SYuan Kang 	struct list_head entry;
1802045e3678SYuan Kang 	int alg_type;
1803045e3678SYuan Kang 	struct ahash_alg ahash_alg;
1804045e3678SYuan Kang };
1805045e3678SYuan Kang 
1806045e3678SYuan Kang static int caam_hash_cra_init(struct crypto_tfm *tfm)
1807045e3678SYuan Kang {
1808045e3678SYuan Kang 	struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
1809045e3678SYuan Kang 	struct crypto_alg *base = tfm->__crt_alg;
1810045e3678SYuan Kang 	struct hash_alg_common *halg =
1811045e3678SYuan Kang 		 container_of(base, struct hash_alg_common, base);
1812045e3678SYuan Kang 	struct ahash_alg *alg =
1813045e3678SYuan Kang 		 container_of(halg, struct ahash_alg, halg);
1814045e3678SYuan Kang 	struct caam_hash_alg *caam_hash =
1815045e3678SYuan Kang 		 container_of(alg, struct caam_hash_alg, ahash_alg);
1816045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm);
1817045e3678SYuan Kang 	/* Sizes for MDHA running digests: MD5, SHA1, 224, 256, 384, 512 */
1818045e3678SYuan Kang 	static const u8 runninglen[] = { HASH_MSG_LEN + MD5_DIGEST_SIZE,
1819045e3678SYuan Kang 					 HASH_MSG_LEN + SHA1_DIGEST_SIZE,
1820045e3678SYuan Kang 					 HASH_MSG_LEN + 32,
1821045e3678SYuan Kang 					 HASH_MSG_LEN + SHA256_DIGEST_SIZE,
1822045e3678SYuan Kang 					 HASH_MSG_LEN + 64,
1823045e3678SYuan Kang 					 HASH_MSG_LEN + SHA512_DIGEST_SIZE };
1824bbf22344SHoria Geantă 	dma_addr_t dma_addr;
18257e0880b9SHoria Geantă 	struct caam_drv_private *priv;
1826045e3678SYuan Kang 
1827045e3678SYuan Kang 	/*
1828cfc6f11bSRuchika Gupta 	 * Get a Job ring from Job Ring driver to ensure in-order
1829045e3678SYuan Kang 	 * crypto request processing per tfm
1830045e3678SYuan Kang 	 */
1831cfc6f11bSRuchika Gupta 	ctx->jrdev = caam_jr_alloc();
1832cfc6f11bSRuchika Gupta 	if (IS_ERR(ctx->jrdev)) {
1833cfc6f11bSRuchika Gupta 		pr_err("Job Ring Device allocation for transform failed\n");
1834cfc6f11bSRuchika Gupta 		return PTR_ERR(ctx->jrdev);
1835cfc6f11bSRuchika Gupta 	}
1836bbf22344SHoria Geantă 
18377e0880b9SHoria Geantă 	priv = dev_get_drvdata(ctx->jrdev->parent);
183812b8567fSIuliana Prodan 
183912b8567fSIuliana Prodan 	if (is_xcbc_aes(caam_hash->alg_type)) {
184012b8567fSIuliana Prodan 		ctx->dir = DMA_TO_DEVICE;
1841*e9b4913aSHoria Geantă 		ctx->key_dir = DMA_BIDIRECTIONAL;
184212b8567fSIuliana Prodan 		ctx->adata.algtype = OP_TYPE_CLASS1_ALG | caam_hash->alg_type;
184312b8567fSIuliana Prodan 		ctx->ctx_len = 48;
1844*e9b4913aSHoria Geantă 	} else if (is_cmac_aes(caam_hash->alg_type)) {
1845*e9b4913aSHoria Geantă 		ctx->dir = DMA_TO_DEVICE;
1846*e9b4913aSHoria Geantă 		ctx->key_dir = DMA_NONE;
1847*e9b4913aSHoria Geantă 		ctx->adata.algtype = OP_TYPE_CLASS1_ALG | caam_hash->alg_type;
1848*e9b4913aSHoria Geantă 		ctx->ctx_len = 32;
1849*e9b4913aSHoria Geantă 	} else {
1850*e9b4913aSHoria Geantă 		if (priv->era >= 6) {
1851*e9b4913aSHoria Geantă 			ctx->dir = DMA_BIDIRECTIONAL;
1852*e9b4913aSHoria Geantă 			ctx->key_dir = alg->setkey ? DMA_TO_DEVICE : DMA_NONE;
1853*e9b4913aSHoria Geantă 		} else {
1854*e9b4913aSHoria Geantă 			ctx->dir = DMA_TO_DEVICE;
1855*e9b4913aSHoria Geantă 			ctx->key_dir = DMA_NONE;
1856*e9b4913aSHoria Geantă 		}
1857*e9b4913aSHoria Geantă 		ctx->adata.algtype = OP_TYPE_CLASS2_ALG | caam_hash->alg_type;
1858*e9b4913aSHoria Geantă 		ctx->ctx_len = runninglen[(ctx->adata.algtype &
1859*e9b4913aSHoria Geantă 					   OP_ALG_ALGSEL_SUBMASK) >>
1860*e9b4913aSHoria Geantă 					  OP_ALG_ALGSEL_SHIFT];
1861*e9b4913aSHoria Geantă 	}
186212b8567fSIuliana Prodan 
1863*e9b4913aSHoria Geantă 	if (ctx->key_dir != DMA_NONE) {
1864a2fb864cSHoria Geantă 		ctx->adata.key_dma = dma_map_single_attrs(ctx->jrdev, ctx->key,
186512b8567fSIuliana Prodan 							  ARRAY_SIZE(ctx->key),
1866*e9b4913aSHoria Geantă 							  ctx->key_dir,
186712b8567fSIuliana Prodan 							  DMA_ATTR_SKIP_CPU_SYNC);
1868a2fb864cSHoria Geantă 		if (dma_mapping_error(ctx->jrdev, ctx->adata.key_dma)) {
186912b8567fSIuliana Prodan 			dev_err(ctx->jrdev, "unable to map key\n");
187012b8567fSIuliana Prodan 			caam_jr_free(ctx->jrdev);
187112b8567fSIuliana Prodan 			return -ENOMEM;
187212b8567fSIuliana Prodan 		}
187312b8567fSIuliana Prodan 	}
18747e0880b9SHoria Geantă 
1875bbf22344SHoria Geantă 	dma_addr = dma_map_single_attrs(ctx->jrdev, ctx->sh_desc_update,
18768e731ee5SHoria Geantă 					offsetof(struct caam_hash_ctx, key),
18777e0880b9SHoria Geantă 					ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
1878bbf22344SHoria Geantă 	if (dma_mapping_error(ctx->jrdev, dma_addr)) {
1879bbf22344SHoria Geantă 		dev_err(ctx->jrdev, "unable to map shared descriptors\n");
188012b8567fSIuliana Prodan 
1881*e9b4913aSHoria Geantă 		if (ctx->key_dir != DMA_NONE)
1882a2fb864cSHoria Geantă 			dma_unmap_single_attrs(ctx->jrdev, ctx->adata.key_dma,
188312b8567fSIuliana Prodan 					       ARRAY_SIZE(ctx->key),
1884*e9b4913aSHoria Geantă 					       ctx->key_dir,
188512b8567fSIuliana Prodan 					       DMA_ATTR_SKIP_CPU_SYNC);
188612b8567fSIuliana Prodan 
1887bbf22344SHoria Geantă 		caam_jr_free(ctx->jrdev);
1888bbf22344SHoria Geantă 		return -ENOMEM;
1889bbf22344SHoria Geantă 	}
1890bbf22344SHoria Geantă 
1891bbf22344SHoria Geantă 	ctx->sh_desc_update_dma = dma_addr;
1892bbf22344SHoria Geantă 	ctx->sh_desc_update_first_dma = dma_addr +
1893bbf22344SHoria Geantă 					offsetof(struct caam_hash_ctx,
1894bbf22344SHoria Geantă 						 sh_desc_update_first);
1895bbf22344SHoria Geantă 	ctx->sh_desc_fin_dma = dma_addr + offsetof(struct caam_hash_ctx,
1896bbf22344SHoria Geantă 						   sh_desc_fin);
1897bbf22344SHoria Geantă 	ctx->sh_desc_digest_dma = dma_addr + offsetof(struct caam_hash_ctx,
1898bbf22344SHoria Geantă 						      sh_desc_digest);
1899bbf22344SHoria Geantă 
1900045e3678SYuan Kang 	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
1901045e3678SYuan Kang 				 sizeof(struct caam_hash_state));
19029a2537d0SIuliana Prodan 
19039a2537d0SIuliana Prodan 	/*
19049a2537d0SIuliana Prodan 	 * For keyed hash algorithms shared descriptors
19059a2537d0SIuliana Prodan 	 * will be created later in setkey() callback
19069a2537d0SIuliana Prodan 	 */
19079a2537d0SIuliana Prodan 	return alg->setkey ? 0 : ahash_set_sh_desc(ahash);
1908045e3678SYuan Kang }
1909045e3678SYuan Kang 
1910045e3678SYuan Kang static void caam_hash_cra_exit(struct crypto_tfm *tfm)
1911045e3678SYuan Kang {
1912045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm);
1913045e3678SYuan Kang 
1914bbf22344SHoria Geantă 	dma_unmap_single_attrs(ctx->jrdev, ctx->sh_desc_update_dma,
191512b8567fSIuliana Prodan 			       offsetof(struct caam_hash_ctx, key),
19167e0880b9SHoria Geantă 			       ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
1917*e9b4913aSHoria Geantă 	if (ctx->key_dir != DMA_NONE)
1918a2fb864cSHoria Geantă 		dma_unmap_single_attrs(ctx->jrdev, ctx->adata.key_dma,
1919*e9b4913aSHoria Geantă 				       ARRAY_SIZE(ctx->key), ctx->key_dir,
192012b8567fSIuliana Prodan 				       DMA_ATTR_SKIP_CPU_SYNC);
1921cfc6f11bSRuchika Gupta 	caam_jr_free(ctx->jrdev);
1922045e3678SYuan Kang }
1923045e3678SYuan Kang 
19241b46c90cSHoria Geantă void caam_algapi_hash_exit(void)
1925045e3678SYuan Kang {
1926045e3678SYuan Kang 	struct caam_hash_alg *t_alg, *n;
1927045e3678SYuan Kang 
1928cfc6f11bSRuchika Gupta 	if (!hash_list.next)
1929045e3678SYuan Kang 		return;
1930045e3678SYuan Kang 
1931cfc6f11bSRuchika Gupta 	list_for_each_entry_safe(t_alg, n, &hash_list, entry) {
1932045e3678SYuan Kang 		crypto_unregister_ahash(&t_alg->ahash_alg);
1933045e3678SYuan Kang 		list_del(&t_alg->entry);
1934045e3678SYuan Kang 		kfree(t_alg);
1935045e3678SYuan Kang 	}
1936045e3678SYuan Kang }
1937045e3678SYuan Kang 
1938045e3678SYuan Kang static struct caam_hash_alg *
1939cfc6f11bSRuchika Gupta caam_hash_alloc(struct caam_hash_template *template,
1940b0e09baeSYuan Kang 		bool keyed)
1941045e3678SYuan Kang {
1942045e3678SYuan Kang 	struct caam_hash_alg *t_alg;
1943045e3678SYuan Kang 	struct ahash_alg *halg;
1944045e3678SYuan Kang 	struct crypto_alg *alg;
1945045e3678SYuan Kang 
19469c4f9733SFabio Estevam 	t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL);
1947045e3678SYuan Kang 	if (!t_alg) {
1948cfc6f11bSRuchika Gupta 		pr_err("failed to allocate t_alg\n");
1949045e3678SYuan Kang 		return ERR_PTR(-ENOMEM);
1950045e3678SYuan Kang 	}
1951045e3678SYuan Kang 
1952045e3678SYuan Kang 	t_alg->ahash_alg = template->template_ahash;
1953045e3678SYuan Kang 	halg = &t_alg->ahash_alg;
1954045e3678SYuan Kang 	alg = &halg->halg.base;
1955045e3678SYuan Kang 
1956b0e09baeSYuan Kang 	if (keyed) {
1957b0e09baeSYuan Kang 		snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
1958b0e09baeSYuan Kang 			 template->hmac_name);
1959b0e09baeSYuan Kang 		snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
1960b0e09baeSYuan Kang 			 template->hmac_driver_name);
1961b0e09baeSYuan Kang 	} else {
1962b0e09baeSYuan Kang 		snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
1963b0e09baeSYuan Kang 			 template->name);
1964045e3678SYuan Kang 		snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
1965045e3678SYuan Kang 			 template->driver_name);
1966a0118c8bSRussell King 		t_alg->ahash_alg.setkey = NULL;
1967b0e09baeSYuan Kang 	}
1968045e3678SYuan Kang 	alg->cra_module = THIS_MODULE;
1969045e3678SYuan Kang 	alg->cra_init = caam_hash_cra_init;
1970045e3678SYuan Kang 	alg->cra_exit = caam_hash_cra_exit;
1971045e3678SYuan Kang 	alg->cra_ctxsize = sizeof(struct caam_hash_ctx);
1972045e3678SYuan Kang 	alg->cra_priority = CAAM_CRA_PRIORITY;
1973045e3678SYuan Kang 	alg->cra_blocksize = template->blocksize;
1974045e3678SYuan Kang 	alg->cra_alignmask = 0;
19756a38f622SEric Biggers 	alg->cra_flags = CRYPTO_ALG_ASYNC;
1976045e3678SYuan Kang 
1977045e3678SYuan Kang 	t_alg->alg_type = template->alg_type;
1978045e3678SYuan Kang 
1979045e3678SYuan Kang 	return t_alg;
1980045e3678SYuan Kang }
1981045e3678SYuan Kang 
19821b46c90cSHoria Geantă int caam_algapi_hash_init(struct device *ctrldev)
1983045e3678SYuan Kang {
1984045e3678SYuan Kang 	int i = 0, err = 0;
19851b46c90cSHoria Geantă 	struct caam_drv_private *priv = dev_get_drvdata(ctrldev);
1986bf83490eSVictoria Milhoan 	unsigned int md_limit = SHA512_DIGEST_SIZE;
1987d239b10dSHoria Geantă 	u32 md_inst, md_vid;
1988045e3678SYuan Kang 
1989bf83490eSVictoria Milhoan 	/*
1990bf83490eSVictoria Milhoan 	 * Register crypto algorithms the device supports.  First, identify
1991bf83490eSVictoria Milhoan 	 * presence and attributes of MD block.
1992bf83490eSVictoria Milhoan 	 */
1993d239b10dSHoria Geantă 	if (priv->era < 10) {
1994d239b10dSHoria Geantă 		md_vid = (rd_reg32(&priv->ctrl->perfmon.cha_id_ls) &
1995d239b10dSHoria Geantă 			  CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;
1996d239b10dSHoria Geantă 		md_inst = (rd_reg32(&priv->ctrl->perfmon.cha_num_ls) &
1997d239b10dSHoria Geantă 			   CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;
1998d239b10dSHoria Geantă 	} else {
1999d239b10dSHoria Geantă 		u32 mdha = rd_reg32(&priv->ctrl->vreg.mdha);
2000d239b10dSHoria Geantă 
2001d239b10dSHoria Geantă 		md_vid = (mdha & CHA_VER_VID_MASK) >> CHA_VER_VID_SHIFT;
2002d239b10dSHoria Geantă 		md_inst = mdha & CHA_VER_NUM_MASK;
2003d239b10dSHoria Geantă 	}
2004bf83490eSVictoria Milhoan 
2005bf83490eSVictoria Milhoan 	/*
2006bf83490eSVictoria Milhoan 	 * Skip registration of any hashing algorithms if MD block
2007bf83490eSVictoria Milhoan 	 * is not present.
2008bf83490eSVictoria Milhoan 	 */
20091b46c90cSHoria Geantă 	if (!md_inst)
20101b46c90cSHoria Geantă 		return -ENODEV;
2011bf83490eSVictoria Milhoan 
2012bf83490eSVictoria Milhoan 	/* Limit digest size based on LP256 */
2013d239b10dSHoria Geantă 	if (md_vid == CHA_VER_VID_MD_LP256)
2014bf83490eSVictoria Milhoan 		md_limit = SHA256_DIGEST_SIZE;
2015bf83490eSVictoria Milhoan 
2016cfc6f11bSRuchika Gupta 	INIT_LIST_HEAD(&hash_list);
2017045e3678SYuan Kang 
2018045e3678SYuan Kang 	/* register crypto algorithms the device supports */
2019045e3678SYuan Kang 	for (i = 0; i < ARRAY_SIZE(driver_hash); i++) {
2020045e3678SYuan Kang 		struct caam_hash_alg *t_alg;
2021bf83490eSVictoria Milhoan 		struct caam_hash_template *alg = driver_hash + i;
2022bf83490eSVictoria Milhoan 
2023bf83490eSVictoria Milhoan 		/* If MD size is not supported by device, skip registration */
202412b8567fSIuliana Prodan 		if (is_mdha(alg->alg_type) &&
202512b8567fSIuliana Prodan 		    alg->template_ahash.halg.digestsize > md_limit)
2026bf83490eSVictoria Milhoan 			continue;
2027045e3678SYuan Kang 
2028b0e09baeSYuan Kang 		/* register hmac version */
2029bf83490eSVictoria Milhoan 		t_alg = caam_hash_alloc(alg, true);
2030b0e09baeSYuan Kang 		if (IS_ERR(t_alg)) {
2031b0e09baeSYuan Kang 			err = PTR_ERR(t_alg);
20320f103b37SIuliana Prodan 			pr_warn("%s alg allocation failed\n",
20330f103b37SIuliana Prodan 				alg->hmac_driver_name);
2034b0e09baeSYuan Kang 			continue;
2035b0e09baeSYuan Kang 		}
2036b0e09baeSYuan Kang 
2037b0e09baeSYuan Kang 		err = crypto_register_ahash(&t_alg->ahash_alg);
2038b0e09baeSYuan Kang 		if (err) {
20396ea30f0aSRussell King 			pr_warn("%s alg registration failed: %d\n",
20406ea30f0aSRussell King 				t_alg->ahash_alg.halg.base.cra_driver_name,
20416ea30f0aSRussell King 				err);
2042b0e09baeSYuan Kang 			kfree(t_alg);
2043b0e09baeSYuan Kang 		} else
2044cfc6f11bSRuchika Gupta 			list_add_tail(&t_alg->entry, &hash_list);
2045b0e09baeSYuan Kang 
204612b8567fSIuliana Prodan 		if ((alg->alg_type & OP_ALG_ALGSEL_MASK) == OP_ALG_ALGSEL_AES)
204712b8567fSIuliana Prodan 			continue;
204812b8567fSIuliana Prodan 
2049b0e09baeSYuan Kang 		/* register unkeyed version */
2050bf83490eSVictoria Milhoan 		t_alg = caam_hash_alloc(alg, false);
2051045e3678SYuan Kang 		if (IS_ERR(t_alg)) {
2052045e3678SYuan Kang 			err = PTR_ERR(t_alg);
2053bf83490eSVictoria Milhoan 			pr_warn("%s alg allocation failed\n", alg->driver_name);
2054045e3678SYuan Kang 			continue;
2055045e3678SYuan Kang 		}
2056045e3678SYuan Kang 
2057045e3678SYuan Kang 		err = crypto_register_ahash(&t_alg->ahash_alg);
2058045e3678SYuan Kang 		if (err) {
20596ea30f0aSRussell King 			pr_warn("%s alg registration failed: %d\n",
20606ea30f0aSRussell King 				t_alg->ahash_alg.halg.base.cra_driver_name,
20616ea30f0aSRussell King 				err);
2062045e3678SYuan Kang 			kfree(t_alg);
2063045e3678SYuan Kang 		} else
2064cfc6f11bSRuchika Gupta 			list_add_tail(&t_alg->entry, &hash_list);
2065045e3678SYuan Kang 	}
2066045e3678SYuan Kang 
2067045e3678SYuan Kang 	return err;
2068045e3678SYuan Kang }
2069