xref: /linux/drivers/crypto/caam/caamhash.c (revision 1b46c90c8e002028dc622bf7bd8cf89efcaab274)
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 
85045e3678SYuan Kang #ifdef DEBUG
86045e3678SYuan Kang /* for print_hex_dumps with line references */
87045e3678SYuan Kang #define debug(format, arg...) printk(format, arg)
88045e3678SYuan Kang #else
89045e3678SYuan Kang #define debug(format, arg...)
90045e3678SYuan Kang #endif
91045e3678SYuan Kang 
92cfc6f11bSRuchika Gupta 
93cfc6f11bSRuchika Gupta static struct list_head hash_list;
94cfc6f11bSRuchika Gupta 
95045e3678SYuan Kang /* ahash per-session context */
96045e3678SYuan Kang struct caam_hash_ctx {
97e11793f5SRussell King 	u32 sh_desc_update[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
98e11793f5SRussell King 	u32 sh_desc_update_first[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
99e11793f5SRussell King 	u32 sh_desc_fin[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
100e11793f5SRussell King 	u32 sh_desc_digest[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
10112b8567fSIuliana Prodan 	u8 key[CAAM_MAX_HASH_KEY_SIZE] ____cacheline_aligned;
102e11793f5SRussell King 	dma_addr_t sh_desc_update_dma ____cacheline_aligned;
103045e3678SYuan Kang 	dma_addr_t sh_desc_update_first_dma;
104045e3678SYuan Kang 	dma_addr_t sh_desc_fin_dma;
105045e3678SYuan Kang 	dma_addr_t sh_desc_digest_dma;
10612b8567fSIuliana Prodan 	dma_addr_t key_dma;
1077e0880b9SHoria Geantă 	enum dma_data_direction dir;
108e11793f5SRussell King 	struct device *jrdev;
109045e3678SYuan Kang 	int ctx_len;
110db57656bSHoria Geantă 	struct alginfo adata;
111045e3678SYuan Kang };
112045e3678SYuan Kang 
113045e3678SYuan Kang /* ahash state */
114045e3678SYuan Kang struct caam_hash_state {
115045e3678SYuan Kang 	dma_addr_t buf_dma;
116045e3678SYuan Kang 	dma_addr_t ctx_dma;
11765055e21SFranck LENORMAND 	int ctx_dma_len;
118045e3678SYuan Kang 	u8 buf_0[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
119045e3678SYuan Kang 	int buflen_0;
120045e3678SYuan Kang 	u8 buf_1[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
121045e3678SYuan Kang 	int buflen_1;
122e7472422SVictoria Milhoan 	u8 caam_ctx[MAX_CTX_LEN] ____cacheline_aligned;
123045e3678SYuan Kang 	int (*update)(struct ahash_request *req);
124045e3678SYuan Kang 	int (*final)(struct ahash_request *req);
125045e3678SYuan Kang 	int (*finup)(struct ahash_request *req);
126045e3678SYuan Kang 	int current_buf;
127045e3678SYuan Kang };
128045e3678SYuan Kang 
1295ec90831SRussell King struct caam_export_state {
1305ec90831SRussell King 	u8 buf[CAAM_MAX_HASH_BLOCK_SIZE];
1315ec90831SRussell King 	u8 caam_ctx[MAX_CTX_LEN];
1325ec90831SRussell King 	int buflen;
1335ec90831SRussell King 	int (*update)(struct ahash_request *req);
1345ec90831SRussell King 	int (*final)(struct ahash_request *req);
1355ec90831SRussell King 	int (*finup)(struct ahash_request *req);
1365ec90831SRussell King };
1375ec90831SRussell King 
1380355d23dSHoria Geantă static inline void switch_buf(struct caam_hash_state *state)
1390355d23dSHoria Geantă {
1400355d23dSHoria Geantă 	state->current_buf ^= 1;
1410355d23dSHoria Geantă }
1420355d23dSHoria Geantă 
1430355d23dSHoria Geantă static inline u8 *current_buf(struct caam_hash_state *state)
1440355d23dSHoria Geantă {
1450355d23dSHoria Geantă 	return state->current_buf ? state->buf_1 : state->buf_0;
1460355d23dSHoria Geantă }
1470355d23dSHoria Geantă 
1480355d23dSHoria Geantă static inline u8 *alt_buf(struct caam_hash_state *state)
1490355d23dSHoria Geantă {
1500355d23dSHoria Geantă 	return state->current_buf ? state->buf_0 : state->buf_1;
1510355d23dSHoria Geantă }
1520355d23dSHoria Geantă 
1530355d23dSHoria Geantă static inline int *current_buflen(struct caam_hash_state *state)
1540355d23dSHoria Geantă {
1550355d23dSHoria Geantă 	return state->current_buf ? &state->buflen_1 : &state->buflen_0;
1560355d23dSHoria Geantă }
1570355d23dSHoria Geantă 
1580355d23dSHoria Geantă static inline int *alt_buflen(struct caam_hash_state *state)
1590355d23dSHoria Geantă {
1600355d23dSHoria Geantă 	return state->current_buf ? &state->buflen_0 : &state->buflen_1;
1610355d23dSHoria Geantă }
1620355d23dSHoria Geantă 
16387870cfbSIuliana Prodan static inline bool is_cmac_aes(u32 algtype)
16412b8567fSIuliana Prodan {
16512b8567fSIuliana Prodan 	return (algtype & (OP_ALG_ALGSEL_MASK | OP_ALG_AAI_MASK)) ==
16687870cfbSIuliana Prodan 	       (OP_ALG_ALGSEL_AES | OP_ALG_AAI_CMAC);
16712b8567fSIuliana Prodan }
168045e3678SYuan Kang /* Common job descriptor seq in/out ptr routines */
169045e3678SYuan Kang 
170045e3678SYuan Kang /* Map state->caam_ctx, and append seq_out_ptr command that points to it */
171ce572085SHoria Geanta static inline int map_seq_out_ptr_ctx(u32 *desc, struct device *jrdev,
172045e3678SYuan Kang 				      struct caam_hash_state *state,
173045e3678SYuan Kang 				      int ctx_len)
174045e3678SYuan Kang {
17565055e21SFranck LENORMAND 	state->ctx_dma_len = ctx_len;
176045e3678SYuan Kang 	state->ctx_dma = dma_map_single(jrdev, state->caam_ctx,
177045e3678SYuan Kang 					ctx_len, DMA_FROM_DEVICE);
178ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, state->ctx_dma)) {
179ce572085SHoria Geanta 		dev_err(jrdev, "unable to map ctx\n");
18087ec02e7SHoria Geantă 		state->ctx_dma = 0;
181ce572085SHoria Geanta 		return -ENOMEM;
182ce572085SHoria Geanta 	}
183ce572085SHoria Geanta 
184045e3678SYuan Kang 	append_seq_out_ptr(desc, state->ctx_dma, ctx_len, 0);
185ce572085SHoria Geanta 
186ce572085SHoria Geanta 	return 0;
187045e3678SYuan Kang }
188045e3678SYuan Kang 
189944c3d4dSHoria Geantă /* Map current buffer in state (if length > 0) and put it in link table */
190944c3d4dSHoria Geantă static inline int buf_map_to_sec4_sg(struct device *jrdev,
191045e3678SYuan Kang 				     struct sec4_sg_entry *sec4_sg,
192944c3d4dSHoria Geantă 				     struct caam_hash_state *state)
193045e3678SYuan Kang {
194944c3d4dSHoria Geantă 	int buflen = *current_buflen(state);
195045e3678SYuan Kang 
196944c3d4dSHoria Geantă 	if (!buflen)
197944c3d4dSHoria Geantă 		return 0;
198045e3678SYuan Kang 
199944c3d4dSHoria Geantă 	state->buf_dma = dma_map_single(jrdev, current_buf(state), buflen,
200944c3d4dSHoria Geantă 					DMA_TO_DEVICE);
201944c3d4dSHoria Geantă 	if (dma_mapping_error(jrdev, state->buf_dma)) {
202944c3d4dSHoria Geantă 		dev_err(jrdev, "unable to map buf\n");
203944c3d4dSHoria Geantă 		state->buf_dma = 0;
204944c3d4dSHoria Geantă 		return -ENOMEM;
205045e3678SYuan Kang 	}
206045e3678SYuan Kang 
207944c3d4dSHoria Geantă 	dma_to_sec4_sg_one(sec4_sg, state->buf_dma, buflen, 0);
208045e3678SYuan Kang 
209944c3d4dSHoria Geantă 	return 0;
210045e3678SYuan Kang }
211045e3678SYuan Kang 
212045e3678SYuan Kang /* Map state->caam_ctx, and add it to link table */
213dfcd8393SHoria Geantă static inline int ctx_map_to_sec4_sg(struct device *jrdev,
214ce572085SHoria Geanta 				     struct caam_hash_state *state, int ctx_len,
215ce572085SHoria Geanta 				     struct sec4_sg_entry *sec4_sg, u32 flag)
216045e3678SYuan Kang {
21765055e21SFranck LENORMAND 	state->ctx_dma_len = ctx_len;
218045e3678SYuan Kang 	state->ctx_dma = dma_map_single(jrdev, state->caam_ctx, ctx_len, flag);
219ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, state->ctx_dma)) {
220ce572085SHoria Geanta 		dev_err(jrdev, "unable to map ctx\n");
22187ec02e7SHoria Geantă 		state->ctx_dma = 0;
222ce572085SHoria Geanta 		return -ENOMEM;
223ce572085SHoria Geanta 	}
224ce572085SHoria Geanta 
225045e3678SYuan Kang 	dma_to_sec4_sg_one(sec4_sg, state->ctx_dma, ctx_len, 0);
226ce572085SHoria Geanta 
227ce572085SHoria Geanta 	return 0;
228045e3678SYuan Kang }
229045e3678SYuan Kang 
230045e3678SYuan Kang static int ahash_set_sh_desc(struct crypto_ahash *ahash)
231045e3678SYuan Kang {
232045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
233045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
234045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
2357e0880b9SHoria Geantă 	struct caam_drv_private *ctrlpriv = dev_get_drvdata(jrdev->parent);
236045e3678SYuan Kang 	u32 *desc;
237045e3678SYuan Kang 
2387e0880b9SHoria Geantă 	ctx->adata.key_virt = ctx->key;
2397e0880b9SHoria Geantă 
240045e3678SYuan Kang 	/* ahash_update shared descriptor */
241045e3678SYuan Kang 	desc = ctx->sh_desc_update;
2420efa7579SHoria Geantă 	cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_UPDATE, ctx->ctx_len,
2430efa7579SHoria Geantă 			  ctx->ctx_len, true, ctrlpriv->era);
244bbf22344SHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma,
2457e0880b9SHoria Geantă 				   desc_bytes(desc), ctx->dir);
246045e3678SYuan Kang #ifdef DEBUG
247514df281SAlex Porosanu 	print_hex_dump(KERN_ERR,
248514df281SAlex Porosanu 		       "ahash update shdesc@"__stringify(__LINE__)": ",
249045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
250045e3678SYuan Kang #endif
251045e3678SYuan Kang 
252045e3678SYuan Kang 	/* ahash_update_first shared descriptor */
253045e3678SYuan Kang 	desc = ctx->sh_desc_update_first;
2540efa7579SHoria Geantă 	cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len,
2550efa7579SHoria Geantă 			  ctx->ctx_len, false, ctrlpriv->era);
256bbf22344SHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma,
2577e0880b9SHoria Geantă 				   desc_bytes(desc), ctx->dir);
258045e3678SYuan Kang #ifdef DEBUG
259514df281SAlex Porosanu 	print_hex_dump(KERN_ERR,
260514df281SAlex Porosanu 		       "ahash update first shdesc@"__stringify(__LINE__)": ",
261045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
262045e3678SYuan Kang #endif
263045e3678SYuan Kang 
264045e3678SYuan Kang 	/* ahash_final shared descriptor */
265045e3678SYuan Kang 	desc = ctx->sh_desc_fin;
2660efa7579SHoria Geantă 	cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_FINALIZE, digestsize,
2670efa7579SHoria Geantă 			  ctx->ctx_len, true, ctrlpriv->era);
268bbf22344SHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma,
2697e0880b9SHoria Geantă 				   desc_bytes(desc), ctx->dir);
270045e3678SYuan Kang #ifdef DEBUG
271514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "ahash final shdesc@"__stringify(__LINE__)": ",
272045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, desc,
273045e3678SYuan Kang 		       desc_bytes(desc), 1);
274045e3678SYuan Kang #endif
275045e3678SYuan Kang 
276045e3678SYuan Kang 	/* ahash_digest shared descriptor */
277045e3678SYuan Kang 	desc = ctx->sh_desc_digest;
2780efa7579SHoria Geantă 	cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_INITFINAL, digestsize,
2790efa7579SHoria Geantă 			  ctx->ctx_len, false, ctrlpriv->era);
280bbf22344SHoria Geantă 	dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma,
2817e0880b9SHoria Geantă 				   desc_bytes(desc), ctx->dir);
282045e3678SYuan Kang #ifdef DEBUG
283514df281SAlex Porosanu 	print_hex_dump(KERN_ERR,
284514df281SAlex Porosanu 		       "ahash digest shdesc@"__stringify(__LINE__)": ",
285045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, desc,
286045e3678SYuan Kang 		       desc_bytes(desc), 1);
287045e3678SYuan Kang #endif
288045e3678SYuan Kang 
289045e3678SYuan Kang 	return 0;
290045e3678SYuan Kang }
291045e3678SYuan Kang 
29212b8567fSIuliana Prodan static int axcbc_set_sh_desc(struct crypto_ahash *ahash)
29312b8567fSIuliana Prodan {
29412b8567fSIuliana Prodan 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
29512b8567fSIuliana Prodan 	int digestsize = crypto_ahash_digestsize(ahash);
29612b8567fSIuliana Prodan 	struct device *jrdev = ctx->jrdev;
29712b8567fSIuliana Prodan 	u32 *desc;
29812b8567fSIuliana Prodan 
29912b8567fSIuliana Prodan 	/* key is loaded from memory for UPDATE and FINALIZE states */
30012b8567fSIuliana Prodan 	ctx->adata.key_dma = ctx->key_dma;
30112b8567fSIuliana Prodan 
30212b8567fSIuliana Prodan 	/* shared descriptor for ahash_update */
30312b8567fSIuliana Prodan 	desc = ctx->sh_desc_update;
30487870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_UPDATE,
30587870cfbSIuliana Prodan 			    ctx->ctx_len, ctx->ctx_len, 0);
30612b8567fSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma,
30712b8567fSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
30812b8567fSIuliana Prodan 	print_hex_dump_debug("axcbc update shdesc@" __stringify(__LINE__)" : ",
30912b8567fSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
31012b8567fSIuliana Prodan 			     1);
31112b8567fSIuliana Prodan 
31212b8567fSIuliana Prodan 	/* shared descriptor for ahash_{final,finup} */
31312b8567fSIuliana Prodan 	desc = ctx->sh_desc_fin;
31487870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_FINALIZE,
31587870cfbSIuliana Prodan 			    digestsize, ctx->ctx_len, 0);
31612b8567fSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma,
31712b8567fSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
31812b8567fSIuliana Prodan 	print_hex_dump_debug("axcbc finup shdesc@" __stringify(__LINE__)" : ",
31912b8567fSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
32012b8567fSIuliana Prodan 			     1);
32112b8567fSIuliana Prodan 
32212b8567fSIuliana Prodan 	/* key is immediate data for INIT and INITFINAL states */
32312b8567fSIuliana Prodan 	ctx->adata.key_virt = ctx->key;
32412b8567fSIuliana Prodan 
32512b8567fSIuliana Prodan 	/* shared descriptor for first invocation of ahash_update */
32612b8567fSIuliana Prodan 	desc = ctx->sh_desc_update_first;
32787870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len,
32812b8567fSIuliana Prodan 			    ctx->ctx_len, ctx->key_dma);
32912b8567fSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma,
33012b8567fSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
33112b8567fSIuliana Prodan 	print_hex_dump_debug("axcbc update first shdesc@" __stringify(__LINE__)" : ",
33212b8567fSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
33312b8567fSIuliana Prodan 			     1);
33412b8567fSIuliana Prodan 
33512b8567fSIuliana Prodan 	/* shared descriptor for ahash_digest */
33612b8567fSIuliana Prodan 	desc = ctx->sh_desc_digest;
33787870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INITFINAL,
33887870cfbSIuliana Prodan 			    digestsize, ctx->ctx_len, 0);
33912b8567fSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma,
34012b8567fSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
34112b8567fSIuliana Prodan 	print_hex_dump_debug("axcbc digest shdesc@" __stringify(__LINE__)" : ",
34212b8567fSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
34312b8567fSIuliana Prodan 			     1);
34487870cfbSIuliana Prodan 	return 0;
34587870cfbSIuliana Prodan }
34687870cfbSIuliana Prodan 
34787870cfbSIuliana Prodan static int acmac_set_sh_desc(struct crypto_ahash *ahash)
34887870cfbSIuliana Prodan {
34987870cfbSIuliana Prodan 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
35087870cfbSIuliana Prodan 	int digestsize = crypto_ahash_digestsize(ahash);
35187870cfbSIuliana Prodan 	struct device *jrdev = ctx->jrdev;
35287870cfbSIuliana Prodan 	u32 *desc;
35387870cfbSIuliana Prodan 
35487870cfbSIuliana Prodan 	/* shared descriptor for ahash_update */
35587870cfbSIuliana Prodan 	desc = ctx->sh_desc_update;
35687870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_UPDATE,
35787870cfbSIuliana Prodan 			    ctx->ctx_len, ctx->ctx_len, 0);
35887870cfbSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma,
35987870cfbSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
36087870cfbSIuliana Prodan 	print_hex_dump_debug("acmac update shdesc@" __stringify(__LINE__)" : ",
36187870cfbSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
36287870cfbSIuliana Prodan 			     desc_bytes(desc), 1);
36387870cfbSIuliana Prodan 
36487870cfbSIuliana Prodan 	/* shared descriptor for ahash_{final,finup} */
36587870cfbSIuliana Prodan 	desc = ctx->sh_desc_fin;
36687870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_FINALIZE,
36787870cfbSIuliana Prodan 			    digestsize, ctx->ctx_len, 0);
36887870cfbSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma,
36987870cfbSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
37087870cfbSIuliana Prodan 	print_hex_dump_debug("acmac finup shdesc@" __stringify(__LINE__)" : ",
37187870cfbSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
37287870cfbSIuliana Prodan 			     desc_bytes(desc), 1);
37387870cfbSIuliana Prodan 
37487870cfbSIuliana Prodan 	/* shared descriptor for first invocation of ahash_update */
37587870cfbSIuliana Prodan 	desc = ctx->sh_desc_update_first;
37687870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len,
37787870cfbSIuliana Prodan 			    ctx->ctx_len, 0);
37887870cfbSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma,
37987870cfbSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
38087870cfbSIuliana Prodan 	print_hex_dump_debug("acmac update first shdesc@" __stringify(__LINE__)" : ",
38187870cfbSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
38287870cfbSIuliana Prodan 			     desc_bytes(desc), 1);
38387870cfbSIuliana Prodan 
38487870cfbSIuliana Prodan 	/* shared descriptor for ahash_digest */
38587870cfbSIuliana Prodan 	desc = ctx->sh_desc_digest;
38687870cfbSIuliana Prodan 	cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INITFINAL,
38787870cfbSIuliana Prodan 			    digestsize, ctx->ctx_len, 0);
38887870cfbSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma,
38987870cfbSIuliana Prodan 				   desc_bytes(desc), ctx->dir);
39087870cfbSIuliana Prodan 	print_hex_dump_debug("acmac digest shdesc@" __stringify(__LINE__)" : ",
39187870cfbSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, desc,
39287870cfbSIuliana Prodan 			     desc_bytes(desc), 1);
39312b8567fSIuliana Prodan 
39412b8567fSIuliana Prodan 	return 0;
39512b8567fSIuliana Prodan }
39612b8567fSIuliana Prodan 
397045e3678SYuan Kang /* Digest hash size if it is too large */
39830724445SHoria Geantă static int hash_digest_key(struct caam_hash_ctx *ctx, u32 *keylen, u8 *key,
39930724445SHoria Geantă 			   u32 digestsize)
400045e3678SYuan Kang {
401045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
402045e3678SYuan Kang 	u32 *desc;
403045e3678SYuan Kang 	struct split_key_result result;
40430724445SHoria Geantă 	dma_addr_t key_dma;
4059e6df0fdSMarkus Elfring 	int ret;
406045e3678SYuan Kang 
4079c23b7d3SVakul Garg 	desc = kmalloc(CAAM_CMD_SZ * 8 + CAAM_PTR_SZ * 2, GFP_KERNEL | GFP_DMA);
4082af8f4a2SKim Phillips 	if (!desc) {
4092af8f4a2SKim Phillips 		dev_err(jrdev, "unable to allocate key input memory\n");
4102af8f4a2SKim Phillips 		return -ENOMEM;
4112af8f4a2SKim Phillips 	}
412045e3678SYuan Kang 
413045e3678SYuan Kang 	init_job_desc(desc, 0);
414045e3678SYuan Kang 
41530724445SHoria Geantă 	key_dma = dma_map_single(jrdev, key, *keylen, DMA_BIDIRECTIONAL);
41630724445SHoria Geantă 	if (dma_mapping_error(jrdev, key_dma)) {
41730724445SHoria Geantă 		dev_err(jrdev, "unable to map key memory\n");
418045e3678SYuan Kang 		kfree(desc);
419045e3678SYuan Kang 		return -ENOMEM;
420045e3678SYuan Kang 	}
421045e3678SYuan Kang 
422045e3678SYuan Kang 	/* Job descriptor to perform unkeyed hash on key_in */
423db57656bSHoria Geantă 	append_operation(desc, ctx->adata.algtype | OP_ALG_ENCRYPT |
424045e3678SYuan Kang 			 OP_ALG_AS_INITFINAL);
42530724445SHoria Geantă 	append_seq_in_ptr(desc, key_dma, *keylen, 0);
426045e3678SYuan Kang 	append_seq_fifo_load(desc, *keylen, FIFOLD_CLASS_CLASS2 |
427045e3678SYuan Kang 			     FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_MSG);
42830724445SHoria Geantă 	append_seq_out_ptr(desc, key_dma, digestsize, 0);
429045e3678SYuan Kang 	append_seq_store(desc, digestsize, LDST_CLASS_2_CCB |
430045e3678SYuan Kang 			 LDST_SRCDST_BYTE_CONTEXT);
431045e3678SYuan Kang 
432045e3678SYuan Kang #ifdef DEBUG
433514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "key_in@"__stringify(__LINE__)": ",
43430724445SHoria Geantă 		       DUMP_PREFIX_ADDRESS, 16, 4, key, *keylen, 1);
435514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
436045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
437045e3678SYuan Kang #endif
438045e3678SYuan Kang 
439045e3678SYuan Kang 	result.err = 0;
440045e3678SYuan Kang 	init_completion(&result.completion);
441045e3678SYuan Kang 
442045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, split_key_done, &result);
443045e3678SYuan Kang 	if (!ret) {
444045e3678SYuan Kang 		/* in progress */
4457459e1d2SHoria Geantă 		wait_for_completion(&result.completion);
446045e3678SYuan Kang 		ret = result.err;
447045e3678SYuan Kang #ifdef DEBUG
448514df281SAlex Porosanu 		print_hex_dump(KERN_ERR,
449514df281SAlex Porosanu 			       "digested key@"__stringify(__LINE__)": ",
45030724445SHoria Geantă 			       DUMP_PREFIX_ADDRESS, 16, 4, key, digestsize, 1);
451045e3678SYuan Kang #endif
452045e3678SYuan Kang 	}
45330724445SHoria Geantă 	dma_unmap_single(jrdev, key_dma, *keylen, DMA_BIDIRECTIONAL);
454045e3678SYuan Kang 
455e11aa9f1SHoria Geanta 	*keylen = digestsize;
456e11aa9f1SHoria Geanta 
457045e3678SYuan Kang 	kfree(desc);
458045e3678SYuan Kang 
459045e3678SYuan Kang 	return ret;
460045e3678SYuan Kang }
461045e3678SYuan Kang 
462045e3678SYuan Kang static int ahash_setkey(struct crypto_ahash *ahash,
463045e3678SYuan Kang 			const u8 *key, unsigned int keylen)
464045e3678SYuan Kang {
465045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
466045e3678SYuan Kang 	int blocksize = crypto_tfm_alg_blocksize(&ahash->base);
467045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
4687e0880b9SHoria Geantă 	struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctx->jrdev->parent);
4699e6df0fdSMarkus Elfring 	int ret;
470045e3678SYuan Kang 	u8 *hashed_key = NULL;
471045e3678SYuan Kang 
472045e3678SYuan Kang #ifdef DEBUG
473045e3678SYuan Kang 	printk(KERN_ERR "keylen %d\n", keylen);
474045e3678SYuan Kang #endif
475045e3678SYuan Kang 
476045e3678SYuan Kang 	if (keylen > blocksize) {
47730724445SHoria Geantă 		hashed_key = kmemdup(key, keylen, GFP_KERNEL | GFP_DMA);
478045e3678SYuan Kang 		if (!hashed_key)
479045e3678SYuan Kang 			return -ENOMEM;
48030724445SHoria Geantă 		ret = hash_digest_key(ctx, &keylen, hashed_key, digestsize);
481045e3678SYuan Kang 		if (ret)
482d6e7a7d0SMarkus Elfring 			goto bad_free_key;
483045e3678SYuan Kang 		key = hashed_key;
484045e3678SYuan Kang 	}
485045e3678SYuan Kang 
4867e0880b9SHoria Geantă 	/*
4877e0880b9SHoria Geantă 	 * If DKP is supported, use it in the shared descriptor to generate
4887e0880b9SHoria Geantă 	 * the split key.
4897e0880b9SHoria Geantă 	 */
4907e0880b9SHoria Geantă 	if (ctrlpriv->era >= 6) {
4917e0880b9SHoria Geantă 		ctx->adata.key_inline = true;
4927e0880b9SHoria Geantă 		ctx->adata.keylen = keylen;
4937e0880b9SHoria Geantă 		ctx->adata.keylen_pad = split_key_len(ctx->adata.algtype &
4947e0880b9SHoria Geantă 						      OP_ALG_ALGSEL_MASK);
4957e0880b9SHoria Geantă 
4967e0880b9SHoria Geantă 		if (ctx->adata.keylen_pad > CAAM_MAX_HASH_KEY_SIZE)
497d6e7a7d0SMarkus Elfring 			goto bad_free_key;
498045e3678SYuan Kang 
4997e0880b9SHoria Geantă 		memcpy(ctx->key, key, keylen);
5007e0880b9SHoria Geantă 	} else {
5017e0880b9SHoria Geantă 		ret = gen_split_key(ctx->jrdev, ctx->key, &ctx->adata, key,
5027e0880b9SHoria Geantă 				    keylen, CAAM_MAX_HASH_KEY_SIZE);
5037e0880b9SHoria Geantă 		if (ret)
5047e0880b9SHoria Geantă 			goto bad_free_key;
5057e0880b9SHoria Geantă 	}
506045e3678SYuan Kang 
507045e3678SYuan Kang 	kfree(hashed_key);
508cfb725f6SHoria Geantă 	return ahash_set_sh_desc(ahash);
509d6e7a7d0SMarkus Elfring  bad_free_key:
510045e3678SYuan Kang 	kfree(hashed_key);
511045e3678SYuan Kang 	crypto_ahash_set_flags(ahash, CRYPTO_TFM_RES_BAD_KEY_LEN);
512045e3678SYuan Kang 	return -EINVAL;
513045e3678SYuan Kang }
514045e3678SYuan Kang 
51512b8567fSIuliana Prodan static int axcbc_setkey(struct crypto_ahash *ahash, const u8 *key,
51612b8567fSIuliana Prodan 			unsigned int keylen)
51712b8567fSIuliana Prodan {
51812b8567fSIuliana Prodan 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
51912b8567fSIuliana Prodan 	struct device *jrdev = ctx->jrdev;
52012b8567fSIuliana Prodan 
52112b8567fSIuliana Prodan 	memcpy(ctx->key, key, keylen);
52212b8567fSIuliana Prodan 	dma_sync_single_for_device(jrdev, ctx->key_dma, keylen, DMA_TO_DEVICE);
52312b8567fSIuliana Prodan 	ctx->adata.keylen = keylen;
52412b8567fSIuliana Prodan 
52512b8567fSIuliana Prodan 	print_hex_dump_debug("axcbc ctx.key@" __stringify(__LINE__)" : ",
52612b8567fSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, ctx->key, keylen, 1);
52712b8567fSIuliana Prodan 
52812b8567fSIuliana Prodan 	return axcbc_set_sh_desc(ahash);
52912b8567fSIuliana Prodan }
53087870cfbSIuliana Prodan 
53187870cfbSIuliana Prodan static int acmac_setkey(struct crypto_ahash *ahash, const u8 *key,
53287870cfbSIuliana Prodan 			unsigned int keylen)
53387870cfbSIuliana Prodan {
53487870cfbSIuliana Prodan 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
53587870cfbSIuliana Prodan 
53687870cfbSIuliana Prodan 	/* key is immediate data for all cmac shared descriptors */
53787870cfbSIuliana Prodan 	ctx->adata.key_virt = key;
53887870cfbSIuliana Prodan 	ctx->adata.keylen = keylen;
53987870cfbSIuliana Prodan 
54087870cfbSIuliana Prodan 	print_hex_dump_debug("acmac ctx.key@" __stringify(__LINE__)" : ",
54187870cfbSIuliana Prodan 			     DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
54287870cfbSIuliana Prodan 
54387870cfbSIuliana Prodan 	return acmac_set_sh_desc(ahash);
54487870cfbSIuliana Prodan }
54587870cfbSIuliana Prodan 
546045e3678SYuan Kang /*
547045e3678SYuan Kang  * ahash_edesc - s/w-extended ahash descriptor
548045e3678SYuan Kang  * @sec4_sg_dma: physical mapped address of h/w link table
549045e3678SYuan Kang  * @src_nents: number of segments in input scatterlist
550045e3678SYuan Kang  * @sec4_sg_bytes: length of dma mapped sec4_sg space
551045e3678SYuan Kang  * @hw_desc: the h/w job descriptor followed by any referenced link tables
552343e44b1SRussell King  * @sec4_sg: h/w link table
553045e3678SYuan Kang  */
554045e3678SYuan Kang struct ahash_edesc {
555045e3678SYuan Kang 	dma_addr_t sec4_sg_dma;
556045e3678SYuan Kang 	int src_nents;
557045e3678SYuan Kang 	int sec4_sg_bytes;
558d7b24ed4SRussell King 	u32 hw_desc[DESC_JOB_IO_LEN / sizeof(u32)] ____cacheline_aligned;
559343e44b1SRussell King 	struct sec4_sg_entry sec4_sg[0];
560045e3678SYuan Kang };
561045e3678SYuan Kang 
562045e3678SYuan Kang static inline void ahash_unmap(struct device *dev,
563045e3678SYuan Kang 			struct ahash_edesc *edesc,
564045e3678SYuan Kang 			struct ahash_request *req, int dst_len)
565045e3678SYuan Kang {
566944c3d4dSHoria Geantă 	struct caam_hash_state *state = ahash_request_ctx(req);
567944c3d4dSHoria Geantă 
568045e3678SYuan Kang 	if (edesc->src_nents)
56913fb8fd7SLABBE Corentin 		dma_unmap_sg(dev, req->src, edesc->src_nents, DMA_TO_DEVICE);
570045e3678SYuan Kang 
571045e3678SYuan Kang 	if (edesc->sec4_sg_bytes)
572045e3678SYuan Kang 		dma_unmap_single(dev, edesc->sec4_sg_dma,
573045e3678SYuan Kang 				 edesc->sec4_sg_bytes, DMA_TO_DEVICE);
574944c3d4dSHoria Geantă 
575944c3d4dSHoria Geantă 	if (state->buf_dma) {
576944c3d4dSHoria Geantă 		dma_unmap_single(dev, state->buf_dma, *current_buflen(state),
577944c3d4dSHoria Geantă 				 DMA_TO_DEVICE);
578944c3d4dSHoria Geantă 		state->buf_dma = 0;
579944c3d4dSHoria Geantă 	}
580045e3678SYuan Kang }
581045e3678SYuan Kang 
582045e3678SYuan Kang static inline void ahash_unmap_ctx(struct device *dev,
583045e3678SYuan Kang 			struct ahash_edesc *edesc,
584045e3678SYuan Kang 			struct ahash_request *req, int dst_len, u32 flag)
585045e3678SYuan Kang {
586045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
587045e3678SYuan Kang 
58887ec02e7SHoria Geantă 	if (state->ctx_dma) {
58965055e21SFranck LENORMAND 		dma_unmap_single(dev, state->ctx_dma, state->ctx_dma_len, flag);
59087ec02e7SHoria Geantă 		state->ctx_dma = 0;
59187ec02e7SHoria Geantă 	}
592045e3678SYuan Kang 	ahash_unmap(dev, edesc, req, dst_len);
593045e3678SYuan Kang }
594045e3678SYuan Kang 
595045e3678SYuan Kang static void ahash_done(struct device *jrdev, u32 *desc, u32 err,
596045e3678SYuan Kang 		       void *context)
597045e3678SYuan Kang {
598045e3678SYuan Kang 	struct ahash_request *req = context;
599045e3678SYuan Kang 	struct ahash_edesc *edesc;
600045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
601045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
602c19650d6SHoria Geantă 	struct caam_hash_state *state = ahash_request_ctx(req);
603045e3678SYuan Kang #ifdef DEBUG
604045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
605045e3678SYuan Kang 
606045e3678SYuan Kang 	dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
607045e3678SYuan Kang #endif
608045e3678SYuan Kang 
6094ca7c7d8SHoria Geantă 	edesc = container_of(desc, struct ahash_edesc, hw_desc[0]);
610fa9659cdSMarek Vasut 	if (err)
611fa9659cdSMarek Vasut 		caam_jr_strstatus(jrdev, err);
612045e3678SYuan Kang 
613c19650d6SHoria Geantă 	ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
614c19650d6SHoria Geantă 	memcpy(req->result, state->caam_ctx, digestsize);
615045e3678SYuan Kang 	kfree(edesc);
616045e3678SYuan Kang 
617045e3678SYuan Kang #ifdef DEBUG
618514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ",
619045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
620045e3678SYuan Kang 		       ctx->ctx_len, 1);
621045e3678SYuan Kang #endif
622045e3678SYuan Kang 
623045e3678SYuan Kang 	req->base.complete(&req->base, err);
624045e3678SYuan Kang }
625045e3678SYuan Kang 
626045e3678SYuan Kang static void ahash_done_bi(struct device *jrdev, u32 *desc, u32 err,
627045e3678SYuan Kang 			    void *context)
628045e3678SYuan Kang {
629045e3678SYuan Kang 	struct ahash_request *req = context;
630045e3678SYuan Kang 	struct ahash_edesc *edesc;
631045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
632045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
633045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
634944c3d4dSHoria Geantă #ifdef DEBUG
635045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
636045e3678SYuan Kang 
637045e3678SYuan Kang 	dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
638045e3678SYuan Kang #endif
639045e3678SYuan Kang 
6404ca7c7d8SHoria Geantă 	edesc = container_of(desc, struct ahash_edesc, hw_desc[0]);
641fa9659cdSMarek Vasut 	if (err)
642fa9659cdSMarek Vasut 		caam_jr_strstatus(jrdev, err);
643045e3678SYuan Kang 
644045e3678SYuan Kang 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_BIDIRECTIONAL);
645944c3d4dSHoria Geantă 	switch_buf(state);
646045e3678SYuan Kang 	kfree(edesc);
647045e3678SYuan Kang 
648045e3678SYuan Kang #ifdef DEBUG
649514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ",
650045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
651045e3678SYuan Kang 		       ctx->ctx_len, 1);
652045e3678SYuan Kang 	if (req->result)
653514df281SAlex Porosanu 		print_hex_dump(KERN_ERR, "result@"__stringify(__LINE__)": ",
654045e3678SYuan Kang 			       DUMP_PREFIX_ADDRESS, 16, 4, req->result,
655045e3678SYuan Kang 			       digestsize, 1);
656045e3678SYuan Kang #endif
657045e3678SYuan Kang 
658045e3678SYuan Kang 	req->base.complete(&req->base, err);
659045e3678SYuan Kang }
660045e3678SYuan Kang 
661045e3678SYuan Kang static void ahash_done_ctx_src(struct device *jrdev, u32 *desc, u32 err,
662045e3678SYuan Kang 			       void *context)
663045e3678SYuan Kang {
664045e3678SYuan Kang 	struct ahash_request *req = context;
665045e3678SYuan Kang 	struct ahash_edesc *edesc;
666045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
667045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
668c19650d6SHoria Geantă 	struct caam_hash_state *state = ahash_request_ctx(req);
669045e3678SYuan Kang #ifdef DEBUG
670045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
671045e3678SYuan Kang 
672045e3678SYuan Kang 	dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
673045e3678SYuan Kang #endif
674045e3678SYuan Kang 
6754ca7c7d8SHoria Geantă 	edesc = container_of(desc, struct ahash_edesc, hw_desc[0]);
676fa9659cdSMarek Vasut 	if (err)
677fa9659cdSMarek Vasut 		caam_jr_strstatus(jrdev, err);
678045e3678SYuan Kang 
679c19650d6SHoria Geantă 	ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL);
680c19650d6SHoria Geantă 	memcpy(req->result, state->caam_ctx, digestsize);
681045e3678SYuan Kang 	kfree(edesc);
682045e3678SYuan Kang 
683045e3678SYuan Kang #ifdef DEBUG
684514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ",
685045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
686045e3678SYuan Kang 		       ctx->ctx_len, 1);
687045e3678SYuan Kang #endif
688045e3678SYuan Kang 
689045e3678SYuan Kang 	req->base.complete(&req->base, err);
690045e3678SYuan Kang }
691045e3678SYuan Kang 
692045e3678SYuan Kang static void ahash_done_ctx_dst(struct device *jrdev, u32 *desc, u32 err,
693045e3678SYuan Kang 			       void *context)
694045e3678SYuan Kang {
695045e3678SYuan Kang 	struct ahash_request *req = context;
696045e3678SYuan Kang 	struct ahash_edesc *edesc;
697045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
698045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
699045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
700944c3d4dSHoria Geantă #ifdef DEBUG
701045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
702045e3678SYuan Kang 
703045e3678SYuan Kang 	dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
704045e3678SYuan Kang #endif
705045e3678SYuan Kang 
7064ca7c7d8SHoria Geantă 	edesc = container_of(desc, struct ahash_edesc, hw_desc[0]);
707fa9659cdSMarek Vasut 	if (err)
708fa9659cdSMarek Vasut 		caam_jr_strstatus(jrdev, err);
709045e3678SYuan Kang 
710ef62b231SHoria Geanta 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_FROM_DEVICE);
711944c3d4dSHoria Geantă 	switch_buf(state);
712045e3678SYuan Kang 	kfree(edesc);
713045e3678SYuan Kang 
714045e3678SYuan Kang #ifdef DEBUG
715514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ",
716045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
717045e3678SYuan Kang 		       ctx->ctx_len, 1);
718045e3678SYuan Kang 	if (req->result)
719514df281SAlex Porosanu 		print_hex_dump(KERN_ERR, "result@"__stringify(__LINE__)": ",
720045e3678SYuan Kang 			       DUMP_PREFIX_ADDRESS, 16, 4, req->result,
721045e3678SYuan Kang 			       digestsize, 1);
722045e3678SYuan Kang #endif
723045e3678SYuan Kang 
724045e3678SYuan Kang 	req->base.complete(&req->base, err);
725045e3678SYuan Kang }
726045e3678SYuan Kang 
7275588d039SRussell King /*
7285588d039SRussell King  * Allocate an enhanced descriptor, which contains the hardware descriptor
7295588d039SRussell King  * and space for hardware scatter table containing sg_num entries.
7305588d039SRussell King  */
7315588d039SRussell King static struct ahash_edesc *ahash_edesc_alloc(struct caam_hash_ctx *ctx,
73230a43b44SRussell King 					     int sg_num, u32 *sh_desc,
73330a43b44SRussell King 					     dma_addr_t sh_desc_dma,
73430a43b44SRussell King 					     gfp_t flags)
7355588d039SRussell King {
7365588d039SRussell King 	struct ahash_edesc *edesc;
7375588d039SRussell King 	unsigned int sg_size = sg_num * sizeof(struct sec4_sg_entry);
7385588d039SRussell King 
7395588d039SRussell King 	edesc = kzalloc(sizeof(*edesc) + sg_size, GFP_DMA | flags);
7405588d039SRussell King 	if (!edesc) {
7415588d039SRussell King 		dev_err(ctx->jrdev, "could not allocate extended descriptor\n");
7425588d039SRussell King 		return NULL;
7435588d039SRussell King 	}
7445588d039SRussell King 
74530a43b44SRussell King 	init_job_desc_shared(edesc->hw_desc, sh_desc_dma, desc_len(sh_desc),
74630a43b44SRussell King 			     HDR_SHARE_DEFER | HDR_REVERSE);
74730a43b44SRussell King 
7485588d039SRussell King 	return edesc;
7495588d039SRussell King }
7505588d039SRussell King 
75165cf164aSRussell King static int ahash_edesc_add_src(struct caam_hash_ctx *ctx,
75265cf164aSRussell King 			       struct ahash_edesc *edesc,
75365cf164aSRussell King 			       struct ahash_request *req, int nents,
75465cf164aSRussell King 			       unsigned int first_sg,
75565cf164aSRussell King 			       unsigned int first_bytes, size_t to_hash)
75665cf164aSRussell King {
75765cf164aSRussell King 	dma_addr_t src_dma;
75865cf164aSRussell King 	u32 options;
75965cf164aSRussell King 
76065cf164aSRussell King 	if (nents > 1 || first_sg) {
76165cf164aSRussell King 		struct sec4_sg_entry *sg = edesc->sec4_sg;
762a5e5c133SHoria Geantă 		unsigned int sgsize = sizeof(*sg) *
763a5e5c133SHoria Geantă 				      pad_sg_nents(first_sg + nents);
76465cf164aSRussell King 
76565cf164aSRussell King 		sg_to_sec4_sg_last(req->src, nents, sg + first_sg, 0);
76665cf164aSRussell King 
76765cf164aSRussell King 		src_dma = dma_map_single(ctx->jrdev, sg, sgsize, DMA_TO_DEVICE);
76865cf164aSRussell King 		if (dma_mapping_error(ctx->jrdev, src_dma)) {
76965cf164aSRussell King 			dev_err(ctx->jrdev, "unable to map S/G table\n");
77065cf164aSRussell King 			return -ENOMEM;
77165cf164aSRussell King 		}
77265cf164aSRussell King 
77365cf164aSRussell King 		edesc->sec4_sg_bytes = sgsize;
77465cf164aSRussell King 		edesc->sec4_sg_dma = src_dma;
77565cf164aSRussell King 		options = LDST_SGF;
77665cf164aSRussell King 	} else {
77765cf164aSRussell King 		src_dma = sg_dma_address(req->src);
77865cf164aSRussell King 		options = 0;
77965cf164aSRussell King 	}
78065cf164aSRussell King 
78165cf164aSRussell King 	append_seq_in_ptr(edesc->hw_desc, src_dma, first_bytes + to_hash,
78265cf164aSRussell King 			  options);
78365cf164aSRussell King 
78465cf164aSRussell King 	return 0;
78565cf164aSRussell King }
78665cf164aSRussell King 
787045e3678SYuan Kang /* submit update job descriptor */
788045e3678SYuan Kang static int ahash_update_ctx(struct ahash_request *req)
789045e3678SYuan Kang {
790045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
791045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
792045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
793045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
794019d62dbSHoria Geantă 	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
795019d62dbSHoria Geantă 		       GFP_KERNEL : GFP_ATOMIC;
7960355d23dSHoria Geantă 	u8 *buf = current_buf(state);
7970355d23dSHoria Geantă 	int *buflen = current_buflen(state);
7980355d23dSHoria Geantă 	u8 *next_buf = alt_buf(state);
79912b8567fSIuliana Prodan 	int blocksize = crypto_ahash_blocksize(ahash);
8000355d23dSHoria Geantă 	int *next_buflen = alt_buflen(state), last_buflen;
801045e3678SYuan Kang 	int in_len = *buflen + req->nbytes, to_hash;
80230a43b44SRussell King 	u32 *desc;
803bc13c69eSRussell King 	int src_nents, mapped_nents, sec4_sg_bytes, sec4_sg_src_index;
804045e3678SYuan Kang 	struct ahash_edesc *edesc;
805045e3678SYuan Kang 	int ret = 0;
806045e3678SYuan Kang 
807045e3678SYuan Kang 	last_buflen = *next_buflen;
80812b8567fSIuliana Prodan 	*next_buflen = in_len & (blocksize - 1);
809045e3678SYuan Kang 	to_hash = in_len - *next_buflen;
810045e3678SYuan Kang 
81112b8567fSIuliana Prodan 	/*
81287870cfbSIuliana Prodan 	 * For XCBC and CMAC, if to_hash is multiple of block size,
81312b8567fSIuliana Prodan 	 * keep last block in internal buffer
81412b8567fSIuliana Prodan 	 */
81587870cfbSIuliana Prodan 	if ((is_xcbc_aes(ctx->adata.algtype) ||
81687870cfbSIuliana Prodan 	     is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize &&
81712b8567fSIuliana Prodan 	     (*next_buflen == 0)) {
81812b8567fSIuliana Prodan 		*next_buflen = blocksize;
81912b8567fSIuliana Prodan 		to_hash -= blocksize;
82012b8567fSIuliana Prodan 	}
82112b8567fSIuliana Prodan 
822045e3678SYuan Kang 	if (to_hash) {
823a5e5c133SHoria Geantă 		int pad_nents;
824a5e5c133SHoria Geantă 
82513fb8fd7SLABBE Corentin 		src_nents = sg_nents_for_len(req->src,
82613fb8fd7SLABBE Corentin 					     req->nbytes - (*next_buflen));
827f9970c28SLABBE Corentin 		if (src_nents < 0) {
828f9970c28SLABBE Corentin 			dev_err(jrdev, "Invalid number of src SG.\n");
829f9970c28SLABBE Corentin 			return src_nents;
830f9970c28SLABBE Corentin 		}
831bc13c69eSRussell King 
832bc13c69eSRussell King 		if (src_nents) {
833bc13c69eSRussell King 			mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
834bc13c69eSRussell King 						  DMA_TO_DEVICE);
835bc13c69eSRussell King 			if (!mapped_nents) {
836bc13c69eSRussell King 				dev_err(jrdev, "unable to DMA map source\n");
837bc13c69eSRussell King 				return -ENOMEM;
838bc13c69eSRussell King 			}
839bc13c69eSRussell King 		} else {
840bc13c69eSRussell King 			mapped_nents = 0;
841bc13c69eSRussell King 		}
842bc13c69eSRussell King 
843045e3678SYuan Kang 		sec4_sg_src_index = 1 + (*buflen ? 1 : 0);
844a5e5c133SHoria Geantă 		pad_nents = pad_sg_nents(sec4_sg_src_index + mapped_nents);
845a5e5c133SHoria Geantă 		sec4_sg_bytes = pad_nents * sizeof(struct sec4_sg_entry);
846045e3678SYuan Kang 
847045e3678SYuan Kang 		/*
848045e3678SYuan Kang 		 * allocate space for base edesc and hw desc commands,
849045e3678SYuan Kang 		 * link tables
850045e3678SYuan Kang 		 */
851a5e5c133SHoria Geantă 		edesc = ahash_edesc_alloc(ctx, pad_nents, ctx->sh_desc_update,
85230a43b44SRussell King 					  ctx->sh_desc_update_dma, flags);
853045e3678SYuan Kang 		if (!edesc) {
854bc13c69eSRussell King 			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
855045e3678SYuan Kang 			return -ENOMEM;
856045e3678SYuan Kang 		}
857045e3678SYuan Kang 
858045e3678SYuan Kang 		edesc->src_nents = src_nents;
859045e3678SYuan Kang 		edesc->sec4_sg_bytes = sec4_sg_bytes;
860045e3678SYuan Kang 
861dfcd8393SHoria Geantă 		ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
862045e3678SYuan Kang 					 edesc->sec4_sg, DMA_BIDIRECTIONAL);
863ce572085SHoria Geanta 		if (ret)
86458b0e5d0SMarkus Elfring 			goto unmap_ctx;
865045e3678SYuan Kang 
866944c3d4dSHoria Geantă 		ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
867944c3d4dSHoria Geantă 		if (ret)
868944c3d4dSHoria Geantă 			goto unmap_ctx;
869045e3678SYuan Kang 
870b4e9e931SIuliana Prodan 		if (mapped_nents)
871bc13c69eSRussell King 			sg_to_sec4_sg_last(req->src, mapped_nents,
872bc13c69eSRussell King 					   edesc->sec4_sg + sec4_sg_src_index,
873bc13c69eSRussell King 					   0);
874b4e9e931SIuliana Prodan 		else
875b4e9e931SIuliana Prodan 			sg_to_sec4_set_last(edesc->sec4_sg + sec4_sg_src_index -
876b4e9e931SIuliana Prodan 					    1);
877b4e9e931SIuliana Prodan 
8788af7b0f8SVictoria Milhoan 		if (*next_buflen)
879307fd543SCristian Stoica 			scatterwalk_map_and_copy(next_buf, req->src,
880307fd543SCristian Stoica 						 to_hash - *buflen,
881307fd543SCristian Stoica 						 *next_buflen, 0);
882045e3678SYuan Kang 		desc = edesc->hw_desc;
883045e3678SYuan Kang 
8841da2be33SRuchika Gupta 		edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
8851da2be33SRuchika Gupta 						     sec4_sg_bytes,
8861da2be33SRuchika Gupta 						     DMA_TO_DEVICE);
887ce572085SHoria Geanta 		if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
888ce572085SHoria Geanta 			dev_err(jrdev, "unable to map S/G table\n");
88932686d34SRussell King 			ret = -ENOMEM;
89058b0e5d0SMarkus Elfring 			goto unmap_ctx;
891ce572085SHoria Geanta 		}
8921da2be33SRuchika Gupta 
893045e3678SYuan Kang 		append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len +
894045e3678SYuan Kang 				       to_hash, LDST_SGF);
895045e3678SYuan Kang 
896045e3678SYuan Kang 		append_seq_out_ptr(desc, state->ctx_dma, ctx->ctx_len, 0);
897045e3678SYuan Kang 
898045e3678SYuan Kang #ifdef DEBUG
899514df281SAlex Porosanu 		print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
900045e3678SYuan Kang 			       DUMP_PREFIX_ADDRESS, 16, 4, desc,
901045e3678SYuan Kang 			       desc_bytes(desc), 1);
902045e3678SYuan Kang #endif
903045e3678SYuan Kang 
904045e3678SYuan Kang 		ret = caam_jr_enqueue(jrdev, desc, ahash_done_bi, req);
90532686d34SRussell King 		if (ret)
90658b0e5d0SMarkus Elfring 			goto unmap_ctx;
90732686d34SRussell King 
908045e3678SYuan Kang 		ret = -EINPROGRESS;
909045e3678SYuan Kang 	} else if (*next_buflen) {
910307fd543SCristian Stoica 		scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
911307fd543SCristian Stoica 					 req->nbytes, 0);
912045e3678SYuan Kang 		*buflen = *next_buflen;
913045e3678SYuan Kang 		*next_buflen = last_buflen;
914045e3678SYuan Kang 	}
915045e3678SYuan Kang #ifdef DEBUG
916514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "buf@"__stringify(__LINE__)": ",
917045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, buf, *buflen, 1);
918514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "next buf@"__stringify(__LINE__)": ",
919045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, next_buf,
920045e3678SYuan Kang 		       *next_buflen, 1);
921045e3678SYuan Kang #endif
922045e3678SYuan Kang 
923045e3678SYuan Kang 	return ret;
92458b0e5d0SMarkus Elfring unmap_ctx:
92532686d34SRussell King 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_BIDIRECTIONAL);
92632686d34SRussell King 	kfree(edesc);
92732686d34SRussell King 	return ret;
928045e3678SYuan Kang }
929045e3678SYuan Kang 
930045e3678SYuan Kang static int ahash_final_ctx(struct ahash_request *req)
931045e3678SYuan Kang {
932045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
933045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
934045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
935045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
936019d62dbSHoria Geantă 	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
937019d62dbSHoria Geantă 		       GFP_KERNEL : GFP_ATOMIC;
9380355d23dSHoria Geantă 	int buflen = *current_buflen(state);
93930a43b44SRussell King 	u32 *desc;
940a5e5c133SHoria Geantă 	int sec4_sg_bytes;
941045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
942045e3678SYuan Kang 	struct ahash_edesc *edesc;
9439e6df0fdSMarkus Elfring 	int ret;
944045e3678SYuan Kang 
945a5e5c133SHoria Geantă 	sec4_sg_bytes = pad_sg_nents(1 + (buflen ? 1 : 0)) *
946a5e5c133SHoria Geantă 			sizeof(struct sec4_sg_entry);
947045e3678SYuan Kang 
948045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
949a5e5c133SHoria Geantă 	edesc = ahash_edesc_alloc(ctx, 4, ctx->sh_desc_fin,
950a5e5c133SHoria Geantă 				  ctx->sh_desc_fin_dma, flags);
9515588d039SRussell King 	if (!edesc)
952045e3678SYuan Kang 		return -ENOMEM;
953045e3678SYuan Kang 
954045e3678SYuan Kang 	desc = edesc->hw_desc;
955045e3678SYuan Kang 
956045e3678SYuan Kang 	edesc->sec4_sg_bytes = sec4_sg_bytes;
957045e3678SYuan Kang 
958dfcd8393SHoria Geantă 	ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
959c19650d6SHoria Geantă 				 edesc->sec4_sg, DMA_BIDIRECTIONAL);
960ce572085SHoria Geanta 	if (ret)
96158b0e5d0SMarkus Elfring 		goto unmap_ctx;
962045e3678SYuan Kang 
963944c3d4dSHoria Geantă 	ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
964944c3d4dSHoria Geantă 	if (ret)
965944c3d4dSHoria Geantă 		goto unmap_ctx;
966944c3d4dSHoria Geantă 
967a5e5c133SHoria Geantă 	sg_to_sec4_set_last(edesc->sec4_sg + (buflen ? 1 : 0));
968045e3678SYuan Kang 
9691da2be33SRuchika Gupta 	edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
9701da2be33SRuchika Gupta 					    sec4_sg_bytes, DMA_TO_DEVICE);
971ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
972ce572085SHoria Geanta 		dev_err(jrdev, "unable to map S/G table\n");
97332686d34SRussell King 		ret = -ENOMEM;
97458b0e5d0SMarkus Elfring 		goto unmap_ctx;
975ce572085SHoria Geanta 	}
9761da2be33SRuchika Gupta 
977045e3678SYuan Kang 	append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len + buflen,
978045e3678SYuan Kang 			  LDST_SGF);
979c19650d6SHoria Geantă 	append_seq_out_ptr(desc, state->ctx_dma, digestsize, 0);
980045e3678SYuan Kang 
981045e3678SYuan Kang #ifdef DEBUG
982514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
983045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
984045e3678SYuan Kang #endif
985045e3678SYuan Kang 
986045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_src, req);
98732686d34SRussell King 	if (ret)
98858b0e5d0SMarkus Elfring 		goto unmap_ctx;
98932686d34SRussell King 
99032686d34SRussell King 	return -EINPROGRESS;
99158b0e5d0SMarkus Elfring  unmap_ctx:
992c19650d6SHoria Geantă 	ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL);
993045e3678SYuan Kang 	kfree(edesc);
994045e3678SYuan Kang 	return ret;
995045e3678SYuan Kang }
996045e3678SYuan Kang 
997045e3678SYuan Kang static int ahash_finup_ctx(struct ahash_request *req)
998045e3678SYuan Kang {
999045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1000045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1001045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1002045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
1003019d62dbSHoria Geantă 	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
1004019d62dbSHoria Geantă 		       GFP_KERNEL : GFP_ATOMIC;
10050355d23dSHoria Geantă 	int buflen = *current_buflen(state);
100630a43b44SRussell King 	u32 *desc;
100765cf164aSRussell King 	int sec4_sg_src_index;
1008bc13c69eSRussell King 	int src_nents, mapped_nents;
1009045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
1010045e3678SYuan Kang 	struct ahash_edesc *edesc;
10119e6df0fdSMarkus Elfring 	int ret;
1012045e3678SYuan Kang 
101313fb8fd7SLABBE Corentin 	src_nents = sg_nents_for_len(req->src, req->nbytes);
1014f9970c28SLABBE Corentin 	if (src_nents < 0) {
1015f9970c28SLABBE Corentin 		dev_err(jrdev, "Invalid number of src SG.\n");
1016f9970c28SLABBE Corentin 		return src_nents;
1017f9970c28SLABBE Corentin 	}
1018bc13c69eSRussell King 
1019bc13c69eSRussell King 	if (src_nents) {
1020bc13c69eSRussell King 		mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1021bc13c69eSRussell King 					  DMA_TO_DEVICE);
1022bc13c69eSRussell King 		if (!mapped_nents) {
1023bc13c69eSRussell King 			dev_err(jrdev, "unable to DMA map source\n");
1024bc13c69eSRussell King 			return -ENOMEM;
1025bc13c69eSRussell King 		}
1026bc13c69eSRussell King 	} else {
1027bc13c69eSRussell King 		mapped_nents = 0;
1028bc13c69eSRussell King 	}
1029bc13c69eSRussell King 
1030045e3678SYuan Kang 	sec4_sg_src_index = 1 + (buflen ? 1 : 0);
1031045e3678SYuan Kang 
1032045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
10335588d039SRussell King 	edesc = ahash_edesc_alloc(ctx, sec4_sg_src_index + mapped_nents,
10349a1a1c08SHoria Geantă 				  ctx->sh_desc_fin, ctx->sh_desc_fin_dma,
10355588d039SRussell King 				  flags);
1036045e3678SYuan Kang 	if (!edesc) {
1037bc13c69eSRussell King 		dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1038045e3678SYuan Kang 		return -ENOMEM;
1039045e3678SYuan Kang 	}
1040045e3678SYuan Kang 
1041045e3678SYuan Kang 	desc = edesc->hw_desc;
1042045e3678SYuan Kang 
1043045e3678SYuan Kang 	edesc->src_nents = src_nents;
1044045e3678SYuan Kang 
1045dfcd8393SHoria Geantă 	ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
1046c19650d6SHoria Geantă 				 edesc->sec4_sg, DMA_BIDIRECTIONAL);
1047ce572085SHoria Geanta 	if (ret)
104858b0e5d0SMarkus Elfring 		goto unmap_ctx;
1049045e3678SYuan Kang 
1050944c3d4dSHoria Geantă 	ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
1051944c3d4dSHoria Geantă 	if (ret)
1052944c3d4dSHoria Geantă 		goto unmap_ctx;
1053045e3678SYuan Kang 
105465cf164aSRussell King 	ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents,
105565cf164aSRussell King 				  sec4_sg_src_index, ctx->ctx_len + buflen,
105665cf164aSRussell King 				  req->nbytes);
105765cf164aSRussell King 	if (ret)
105858b0e5d0SMarkus Elfring 		goto unmap_ctx;
1059045e3678SYuan Kang 
1060c19650d6SHoria Geantă 	append_seq_out_ptr(desc, state->ctx_dma, digestsize, 0);
1061045e3678SYuan Kang 
1062045e3678SYuan Kang #ifdef DEBUG
1063514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
1064045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
1065045e3678SYuan Kang #endif
1066045e3678SYuan Kang 
1067045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_src, req);
106832686d34SRussell King 	if (ret)
106958b0e5d0SMarkus Elfring 		goto unmap_ctx;
107032686d34SRussell King 
107132686d34SRussell King 	return -EINPROGRESS;
107258b0e5d0SMarkus Elfring  unmap_ctx:
1073c19650d6SHoria Geantă 	ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL);
1074045e3678SYuan Kang 	kfree(edesc);
1075045e3678SYuan Kang 	return ret;
1076045e3678SYuan Kang }
1077045e3678SYuan Kang 
1078045e3678SYuan Kang static int ahash_digest(struct ahash_request *req)
1079045e3678SYuan Kang {
1080045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1081045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1082944c3d4dSHoria Geantă 	struct caam_hash_state *state = ahash_request_ctx(req);
1083045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
1084019d62dbSHoria Geantă 	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
1085019d62dbSHoria Geantă 		       GFP_KERNEL : GFP_ATOMIC;
108630a43b44SRussell King 	u32 *desc;
1087045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
108865cf164aSRussell King 	int src_nents, mapped_nents;
1089045e3678SYuan Kang 	struct ahash_edesc *edesc;
10909e6df0fdSMarkus Elfring 	int ret;
1091045e3678SYuan Kang 
1092944c3d4dSHoria Geantă 	state->buf_dma = 0;
1093944c3d4dSHoria Geantă 
10943d5a2db6SRussell King 	src_nents = sg_nents_for_len(req->src, req->nbytes);
1095f9970c28SLABBE Corentin 	if (src_nents < 0) {
1096f9970c28SLABBE Corentin 		dev_err(jrdev, "Invalid number of src SG.\n");
1097f9970c28SLABBE Corentin 		return src_nents;
1098f9970c28SLABBE Corentin 	}
1099bc13c69eSRussell King 
1100bc13c69eSRussell King 	if (src_nents) {
1101bc13c69eSRussell King 		mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1102bc13c69eSRussell King 					  DMA_TO_DEVICE);
1103bc13c69eSRussell King 		if (!mapped_nents) {
1104bc13c69eSRussell King 			dev_err(jrdev, "unable to map source for DMA\n");
1105bc13c69eSRussell King 			return -ENOMEM;
1106bc13c69eSRussell King 		}
1107bc13c69eSRussell King 	} else {
1108bc13c69eSRussell King 		mapped_nents = 0;
1109bc13c69eSRussell King 	}
1110bc13c69eSRussell King 
1111045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
11125588d039SRussell King 	edesc = ahash_edesc_alloc(ctx, mapped_nents > 1 ? mapped_nents : 0,
111330a43b44SRussell King 				  ctx->sh_desc_digest, ctx->sh_desc_digest_dma,
11145588d039SRussell King 				  flags);
1115045e3678SYuan Kang 	if (!edesc) {
1116bc13c69eSRussell King 		dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1117045e3678SYuan Kang 		return -ENOMEM;
1118045e3678SYuan Kang 	}
1119343e44b1SRussell King 
1120045e3678SYuan Kang 	edesc->src_nents = src_nents;
1121045e3678SYuan Kang 
112265cf164aSRussell King 	ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 0, 0,
112365cf164aSRussell King 				  req->nbytes);
112465cf164aSRussell King 	if (ret) {
112532686d34SRussell King 		ahash_unmap(jrdev, edesc, req, digestsize);
112632686d34SRussell King 		kfree(edesc);
112765cf164aSRussell King 		return ret;
1128ce572085SHoria Geanta 	}
112965cf164aSRussell King 
113065cf164aSRussell King 	desc = edesc->hw_desc;
1131045e3678SYuan Kang 
1132c19650d6SHoria Geantă 	ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
1133c19650d6SHoria Geantă 	if (ret) {
113432686d34SRussell King 		ahash_unmap(jrdev, edesc, req, digestsize);
113532686d34SRussell King 		kfree(edesc);
1136ce572085SHoria Geanta 		return -ENOMEM;
1137ce572085SHoria Geanta 	}
1138045e3678SYuan Kang 
1139045e3678SYuan Kang #ifdef DEBUG
1140514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
1141045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
1142045e3678SYuan Kang #endif
1143045e3678SYuan Kang 
1144045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, ahash_done, req);
1145045e3678SYuan Kang 	if (!ret) {
1146045e3678SYuan Kang 		ret = -EINPROGRESS;
1147045e3678SYuan Kang 	} else {
1148c19650d6SHoria Geantă 		ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
1149045e3678SYuan Kang 		kfree(edesc);
1150045e3678SYuan Kang 	}
1151045e3678SYuan Kang 
1152045e3678SYuan Kang 	return ret;
1153045e3678SYuan Kang }
1154045e3678SYuan Kang 
1155045e3678SYuan Kang /* submit ahash final if it the first job descriptor */
1156045e3678SYuan Kang static int ahash_final_no_ctx(struct ahash_request *req)
1157045e3678SYuan Kang {
1158045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1159045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1160045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1161045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
1162019d62dbSHoria Geantă 	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
1163019d62dbSHoria Geantă 		       GFP_KERNEL : GFP_ATOMIC;
11640355d23dSHoria Geantă 	u8 *buf = current_buf(state);
11650355d23dSHoria Geantă 	int buflen = *current_buflen(state);
116630a43b44SRussell King 	u32 *desc;
1167045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
1168045e3678SYuan Kang 	struct ahash_edesc *edesc;
11699e6df0fdSMarkus Elfring 	int ret;
1170045e3678SYuan Kang 
1171045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
117230a43b44SRussell King 	edesc = ahash_edesc_alloc(ctx, 0, ctx->sh_desc_digest,
117330a43b44SRussell King 				  ctx->sh_desc_digest_dma, flags);
11745588d039SRussell King 	if (!edesc)
1175045e3678SYuan Kang 		return -ENOMEM;
1176045e3678SYuan Kang 
1177045e3678SYuan Kang 	desc = edesc->hw_desc;
1178045e3678SYuan Kang 
117904e6d25cSAymen Sghaier 	if (buflen) {
118004e6d25cSAymen Sghaier 		state->buf_dma = dma_map_single(jrdev, buf, buflen,
118104e6d25cSAymen Sghaier 						DMA_TO_DEVICE);
1182ce572085SHoria Geanta 		if (dma_mapping_error(jrdev, state->buf_dma)) {
1183ce572085SHoria Geanta 			dev_err(jrdev, "unable to map src\n");
118406435f34SMarkus Elfring 			goto unmap;
1185ce572085SHoria Geanta 		}
1186045e3678SYuan Kang 
1187045e3678SYuan Kang 		append_seq_in_ptr(desc, state->buf_dma, buflen, 0);
118804e6d25cSAymen Sghaier 	}
1189045e3678SYuan Kang 
1190c19650d6SHoria Geantă 	ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
1191c19650d6SHoria Geantă 	if (ret)
119206435f34SMarkus Elfring 		goto unmap;
1193045e3678SYuan Kang 
1194045e3678SYuan Kang #ifdef DEBUG
1195514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
1196045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
1197045e3678SYuan Kang #endif
1198045e3678SYuan Kang 
1199045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, ahash_done, req);
1200045e3678SYuan Kang 	if (!ret) {
1201045e3678SYuan Kang 		ret = -EINPROGRESS;
1202045e3678SYuan Kang 	} else {
1203c19650d6SHoria Geantă 		ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
1204045e3678SYuan Kang 		kfree(edesc);
1205045e3678SYuan Kang 	}
1206045e3678SYuan Kang 
1207045e3678SYuan Kang 	return ret;
120806435f34SMarkus Elfring  unmap:
120906435f34SMarkus Elfring 	ahash_unmap(jrdev, edesc, req, digestsize);
121006435f34SMarkus Elfring 	kfree(edesc);
121106435f34SMarkus Elfring 	return -ENOMEM;
121206435f34SMarkus Elfring 
1213045e3678SYuan Kang }
1214045e3678SYuan Kang 
1215045e3678SYuan Kang /* submit ahash update if it the first job descriptor after update */
1216045e3678SYuan Kang static int ahash_update_no_ctx(struct ahash_request *req)
1217045e3678SYuan Kang {
1218045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1219045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1220045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1221045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
1222019d62dbSHoria Geantă 	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
1223019d62dbSHoria Geantă 		       GFP_KERNEL : GFP_ATOMIC;
12240355d23dSHoria Geantă 	u8 *buf = current_buf(state);
12250355d23dSHoria Geantă 	int *buflen = current_buflen(state);
122612b8567fSIuliana Prodan 	int blocksize = crypto_ahash_blocksize(ahash);
12270355d23dSHoria Geantă 	u8 *next_buf = alt_buf(state);
12280355d23dSHoria Geantă 	int *next_buflen = alt_buflen(state);
1229045e3678SYuan Kang 	int in_len = *buflen + req->nbytes, to_hash;
1230bc13c69eSRussell King 	int sec4_sg_bytes, src_nents, mapped_nents;
1231045e3678SYuan Kang 	struct ahash_edesc *edesc;
123230a43b44SRussell King 	u32 *desc;
1233045e3678SYuan Kang 	int ret = 0;
1234045e3678SYuan Kang 
123512b8567fSIuliana Prodan 	*next_buflen = in_len & (blocksize - 1);
1236045e3678SYuan Kang 	to_hash = in_len - *next_buflen;
1237045e3678SYuan Kang 
123812b8567fSIuliana Prodan 	/*
123987870cfbSIuliana Prodan 	 * For XCBC and CMAC, if to_hash is multiple of block size,
124012b8567fSIuliana Prodan 	 * keep last block in internal buffer
124112b8567fSIuliana Prodan 	 */
124287870cfbSIuliana Prodan 	if ((is_xcbc_aes(ctx->adata.algtype) ||
124387870cfbSIuliana Prodan 	     is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize &&
124412b8567fSIuliana Prodan 	     (*next_buflen == 0)) {
124512b8567fSIuliana Prodan 		*next_buflen = blocksize;
124612b8567fSIuliana Prodan 		to_hash -= blocksize;
124712b8567fSIuliana Prodan 	}
124812b8567fSIuliana Prodan 
1249045e3678SYuan Kang 	if (to_hash) {
1250a5e5c133SHoria Geantă 		int pad_nents;
1251a5e5c133SHoria Geantă 
125213fb8fd7SLABBE Corentin 		src_nents = sg_nents_for_len(req->src,
12533d5a2db6SRussell King 					     req->nbytes - *next_buflen);
1254f9970c28SLABBE Corentin 		if (src_nents < 0) {
1255f9970c28SLABBE Corentin 			dev_err(jrdev, "Invalid number of src SG.\n");
1256f9970c28SLABBE Corentin 			return src_nents;
1257f9970c28SLABBE Corentin 		}
1258bc13c69eSRussell King 
1259bc13c69eSRussell King 		if (src_nents) {
1260bc13c69eSRussell King 			mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1261bc13c69eSRussell King 						  DMA_TO_DEVICE);
1262bc13c69eSRussell King 			if (!mapped_nents) {
1263bc13c69eSRussell King 				dev_err(jrdev, "unable to DMA map source\n");
1264bc13c69eSRussell King 				return -ENOMEM;
1265bc13c69eSRussell King 			}
1266bc13c69eSRussell King 		} else {
1267bc13c69eSRussell King 			mapped_nents = 0;
1268bc13c69eSRussell King 		}
1269bc13c69eSRussell King 
1270a5e5c133SHoria Geantă 		pad_nents = pad_sg_nents(1 + mapped_nents);
1271a5e5c133SHoria Geantă 		sec4_sg_bytes = pad_nents * sizeof(struct sec4_sg_entry);
1272045e3678SYuan Kang 
1273045e3678SYuan Kang 		/*
1274045e3678SYuan Kang 		 * allocate space for base edesc and hw desc commands,
1275045e3678SYuan Kang 		 * link tables
1276045e3678SYuan Kang 		 */
1277a5e5c133SHoria Geantă 		edesc = ahash_edesc_alloc(ctx, pad_nents,
127830a43b44SRussell King 					  ctx->sh_desc_update_first,
127930a43b44SRussell King 					  ctx->sh_desc_update_first_dma,
128030a43b44SRussell King 					  flags);
1281045e3678SYuan Kang 		if (!edesc) {
1282bc13c69eSRussell King 			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1283045e3678SYuan Kang 			return -ENOMEM;
1284045e3678SYuan Kang 		}
1285045e3678SYuan Kang 
1286045e3678SYuan Kang 		edesc->src_nents = src_nents;
1287045e3678SYuan Kang 		edesc->sec4_sg_bytes = sec4_sg_bytes;
1288045e3678SYuan Kang 
1289944c3d4dSHoria Geantă 		ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg, state);
1290944c3d4dSHoria Geantă 		if (ret)
1291944c3d4dSHoria Geantă 			goto unmap_ctx;
1292944c3d4dSHoria Geantă 
1293bc13c69eSRussell King 		sg_to_sec4_sg_last(req->src, mapped_nents,
1294bc13c69eSRussell King 				   edesc->sec4_sg + 1, 0);
1295bc13c69eSRussell King 
1296045e3678SYuan Kang 		if (*next_buflen) {
1297307fd543SCristian Stoica 			scatterwalk_map_and_copy(next_buf, req->src,
1298307fd543SCristian Stoica 						 to_hash - *buflen,
1299307fd543SCristian Stoica 						 *next_buflen, 0);
1300045e3678SYuan Kang 		}
1301045e3678SYuan Kang 
1302045e3678SYuan Kang 		desc = edesc->hw_desc;
1303045e3678SYuan Kang 
13041da2be33SRuchika Gupta 		edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
13051da2be33SRuchika Gupta 						    sec4_sg_bytes,
13061da2be33SRuchika Gupta 						    DMA_TO_DEVICE);
1307ce572085SHoria Geanta 		if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
1308ce572085SHoria Geanta 			dev_err(jrdev, "unable to map S/G table\n");
130932686d34SRussell King 			ret = -ENOMEM;
131058b0e5d0SMarkus Elfring 			goto unmap_ctx;
1311ce572085SHoria Geanta 		}
13121da2be33SRuchika Gupta 
1313045e3678SYuan Kang 		append_seq_in_ptr(desc, edesc->sec4_sg_dma, to_hash, LDST_SGF);
1314045e3678SYuan Kang 
1315ce572085SHoria Geanta 		ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len);
1316ce572085SHoria Geanta 		if (ret)
131758b0e5d0SMarkus Elfring 			goto unmap_ctx;
1318045e3678SYuan Kang 
1319045e3678SYuan Kang #ifdef DEBUG
1320514df281SAlex Porosanu 		print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
1321045e3678SYuan Kang 			       DUMP_PREFIX_ADDRESS, 16, 4, desc,
1322045e3678SYuan Kang 			       desc_bytes(desc), 1);
1323045e3678SYuan Kang #endif
1324045e3678SYuan Kang 
1325045e3678SYuan Kang 		ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_dst, req);
132632686d34SRussell King 		if (ret)
132758b0e5d0SMarkus Elfring 			goto unmap_ctx;
132832686d34SRussell King 
1329045e3678SYuan Kang 		ret = -EINPROGRESS;
1330045e3678SYuan Kang 		state->update = ahash_update_ctx;
1331045e3678SYuan Kang 		state->finup = ahash_finup_ctx;
1332045e3678SYuan Kang 		state->final = ahash_final_ctx;
1333045e3678SYuan Kang 	} else if (*next_buflen) {
1334307fd543SCristian Stoica 		scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
1335307fd543SCristian Stoica 					 req->nbytes, 0);
1336045e3678SYuan Kang 		*buflen = *next_buflen;
1337045e3678SYuan Kang 		*next_buflen = 0;
1338045e3678SYuan Kang 	}
1339045e3678SYuan Kang #ifdef DEBUG
1340514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "buf@"__stringify(__LINE__)": ",
1341045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, buf, *buflen, 1);
1342514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "next buf@"__stringify(__LINE__)": ",
1343045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, next_buf,
1344045e3678SYuan Kang 		       *next_buflen, 1);
1345045e3678SYuan Kang #endif
1346045e3678SYuan Kang 
1347045e3678SYuan Kang 	return ret;
134858b0e5d0SMarkus Elfring  unmap_ctx:
134932686d34SRussell King 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE);
135032686d34SRussell King 	kfree(edesc);
135132686d34SRussell King 	return ret;
1352045e3678SYuan Kang }
1353045e3678SYuan Kang 
1354045e3678SYuan Kang /* submit ahash finup if it the first job descriptor after update */
1355045e3678SYuan Kang static int ahash_finup_no_ctx(struct ahash_request *req)
1356045e3678SYuan Kang {
1357045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1358045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1359045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1360045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
1361019d62dbSHoria Geantă 	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
1362019d62dbSHoria Geantă 		       GFP_KERNEL : GFP_ATOMIC;
13630355d23dSHoria Geantă 	int buflen = *current_buflen(state);
136430a43b44SRussell King 	u32 *desc;
1365bc13c69eSRussell King 	int sec4_sg_bytes, sec4_sg_src_index, src_nents, mapped_nents;
1366045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
1367045e3678SYuan Kang 	struct ahash_edesc *edesc;
13689e6df0fdSMarkus Elfring 	int ret;
1369045e3678SYuan Kang 
137013fb8fd7SLABBE Corentin 	src_nents = sg_nents_for_len(req->src, req->nbytes);
1371f9970c28SLABBE Corentin 	if (src_nents < 0) {
1372f9970c28SLABBE Corentin 		dev_err(jrdev, "Invalid number of src SG.\n");
1373f9970c28SLABBE Corentin 		return src_nents;
1374f9970c28SLABBE Corentin 	}
1375bc13c69eSRussell King 
1376bc13c69eSRussell King 	if (src_nents) {
1377bc13c69eSRussell King 		mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1378bc13c69eSRussell King 					  DMA_TO_DEVICE);
1379bc13c69eSRussell King 		if (!mapped_nents) {
1380bc13c69eSRussell King 			dev_err(jrdev, "unable to DMA map source\n");
1381bc13c69eSRussell King 			return -ENOMEM;
1382bc13c69eSRussell King 		}
1383bc13c69eSRussell King 	} else {
1384bc13c69eSRussell King 		mapped_nents = 0;
1385bc13c69eSRussell King 	}
1386bc13c69eSRussell King 
1387045e3678SYuan Kang 	sec4_sg_src_index = 2;
1388bc13c69eSRussell King 	sec4_sg_bytes = (sec4_sg_src_index + mapped_nents) *
1389045e3678SYuan Kang 			 sizeof(struct sec4_sg_entry);
1390045e3678SYuan Kang 
1391045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
139230a43b44SRussell King 	edesc = ahash_edesc_alloc(ctx, sec4_sg_src_index + mapped_nents,
139330a43b44SRussell King 				  ctx->sh_desc_digest, ctx->sh_desc_digest_dma,
139430a43b44SRussell King 				  flags);
1395045e3678SYuan Kang 	if (!edesc) {
1396bc13c69eSRussell King 		dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1397045e3678SYuan Kang 		return -ENOMEM;
1398045e3678SYuan Kang 	}
1399045e3678SYuan Kang 
1400045e3678SYuan Kang 	desc = edesc->hw_desc;
1401045e3678SYuan Kang 
1402045e3678SYuan Kang 	edesc->src_nents = src_nents;
1403045e3678SYuan Kang 	edesc->sec4_sg_bytes = sec4_sg_bytes;
1404045e3678SYuan Kang 
1405944c3d4dSHoria Geantă 	ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg, state);
1406944c3d4dSHoria Geantă 	if (ret)
1407944c3d4dSHoria Geantă 		goto unmap;
1408045e3678SYuan Kang 
140965cf164aSRussell King 	ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 1, buflen,
141065cf164aSRussell King 				  req->nbytes);
141165cf164aSRussell King 	if (ret) {
1412ce572085SHoria Geanta 		dev_err(jrdev, "unable to map S/G table\n");
141306435f34SMarkus Elfring 		goto unmap;
1414ce572085SHoria Geanta 	}
14151da2be33SRuchika Gupta 
1416c19650d6SHoria Geantă 	ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
1417c19650d6SHoria Geantă 	if (ret)
141806435f34SMarkus Elfring 		goto unmap;
1419045e3678SYuan Kang 
1420045e3678SYuan Kang #ifdef DEBUG
1421514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
1422045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
1423045e3678SYuan Kang #endif
1424045e3678SYuan Kang 
1425045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, ahash_done, req);
1426045e3678SYuan Kang 	if (!ret) {
1427045e3678SYuan Kang 		ret = -EINPROGRESS;
1428045e3678SYuan Kang 	} else {
1429c19650d6SHoria Geantă 		ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
1430045e3678SYuan Kang 		kfree(edesc);
1431045e3678SYuan Kang 	}
1432045e3678SYuan Kang 
1433045e3678SYuan Kang 	return ret;
143406435f34SMarkus Elfring  unmap:
143506435f34SMarkus Elfring 	ahash_unmap(jrdev, edesc, req, digestsize);
143606435f34SMarkus Elfring 	kfree(edesc);
143706435f34SMarkus Elfring 	return -ENOMEM;
143806435f34SMarkus Elfring 
1439045e3678SYuan Kang }
1440045e3678SYuan Kang 
1441045e3678SYuan Kang /* submit first update job descriptor after init */
1442045e3678SYuan Kang static int ahash_update_first(struct ahash_request *req)
1443045e3678SYuan Kang {
1444045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1445045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1446045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1447045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
1448019d62dbSHoria Geantă 	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
1449019d62dbSHoria Geantă 		       GFP_KERNEL : GFP_ATOMIC;
1450944c3d4dSHoria Geantă 	u8 *next_buf = alt_buf(state);
1451944c3d4dSHoria Geantă 	int *next_buflen = alt_buflen(state);
1452045e3678SYuan Kang 	int to_hash;
145312b8567fSIuliana Prodan 	int blocksize = crypto_ahash_blocksize(ahash);
145430a43b44SRussell King 	u32 *desc;
145565cf164aSRussell King 	int src_nents, mapped_nents;
1456045e3678SYuan Kang 	struct ahash_edesc *edesc;
1457045e3678SYuan Kang 	int ret = 0;
1458045e3678SYuan Kang 
145912b8567fSIuliana Prodan 	*next_buflen = req->nbytes & (blocksize - 1);
1460045e3678SYuan Kang 	to_hash = req->nbytes - *next_buflen;
1461045e3678SYuan Kang 
146212b8567fSIuliana Prodan 	/*
146387870cfbSIuliana Prodan 	 * For XCBC and CMAC, if to_hash is multiple of block size,
146412b8567fSIuliana Prodan 	 * keep last block in internal buffer
146512b8567fSIuliana Prodan 	 */
146687870cfbSIuliana Prodan 	if ((is_xcbc_aes(ctx->adata.algtype) ||
146787870cfbSIuliana Prodan 	     is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize &&
146812b8567fSIuliana Prodan 	     (*next_buflen == 0)) {
146912b8567fSIuliana Prodan 		*next_buflen = blocksize;
147012b8567fSIuliana Prodan 		to_hash -= blocksize;
147112b8567fSIuliana Prodan 	}
147212b8567fSIuliana Prodan 
1473045e3678SYuan Kang 	if (to_hash) {
14743d5a2db6SRussell King 		src_nents = sg_nents_for_len(req->src,
14753d5a2db6SRussell King 					     req->nbytes - *next_buflen);
1476f9970c28SLABBE Corentin 		if (src_nents < 0) {
1477f9970c28SLABBE Corentin 			dev_err(jrdev, "Invalid number of src SG.\n");
1478f9970c28SLABBE Corentin 			return src_nents;
1479f9970c28SLABBE Corentin 		}
1480bc13c69eSRussell King 
1481bc13c69eSRussell King 		if (src_nents) {
1482bc13c69eSRussell King 			mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1483bc13c69eSRussell King 						  DMA_TO_DEVICE);
1484bc13c69eSRussell King 			if (!mapped_nents) {
1485bc13c69eSRussell King 				dev_err(jrdev, "unable to map source for DMA\n");
1486bc13c69eSRussell King 				return -ENOMEM;
1487bc13c69eSRussell King 			}
1488bc13c69eSRussell King 		} else {
1489bc13c69eSRussell King 			mapped_nents = 0;
1490bc13c69eSRussell King 		}
1491045e3678SYuan Kang 
1492045e3678SYuan Kang 		/*
1493045e3678SYuan Kang 		 * allocate space for base edesc and hw desc commands,
1494045e3678SYuan Kang 		 * link tables
1495045e3678SYuan Kang 		 */
14965588d039SRussell King 		edesc = ahash_edesc_alloc(ctx, mapped_nents > 1 ?
149730a43b44SRussell King 					  mapped_nents : 0,
149830a43b44SRussell King 					  ctx->sh_desc_update_first,
149930a43b44SRussell King 					  ctx->sh_desc_update_first_dma,
150030a43b44SRussell King 					  flags);
1501045e3678SYuan Kang 		if (!edesc) {
1502bc13c69eSRussell King 			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1503045e3678SYuan Kang 			return -ENOMEM;
1504045e3678SYuan Kang 		}
1505045e3678SYuan Kang 
1506045e3678SYuan Kang 		edesc->src_nents = src_nents;
1507045e3678SYuan Kang 
150865cf164aSRussell King 		ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 0, 0,
150965cf164aSRussell King 					  to_hash);
151065cf164aSRussell King 		if (ret)
151158b0e5d0SMarkus Elfring 			goto unmap_ctx;
1512045e3678SYuan Kang 
1513045e3678SYuan Kang 		if (*next_buflen)
1514307fd543SCristian Stoica 			scatterwalk_map_and_copy(next_buf, req->src, to_hash,
1515307fd543SCristian Stoica 						 *next_buflen, 0);
1516045e3678SYuan Kang 
1517045e3678SYuan Kang 		desc = edesc->hw_desc;
1518045e3678SYuan Kang 
1519ce572085SHoria Geanta 		ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len);
1520ce572085SHoria Geanta 		if (ret)
152158b0e5d0SMarkus Elfring 			goto unmap_ctx;
1522045e3678SYuan Kang 
1523045e3678SYuan Kang #ifdef DEBUG
1524514df281SAlex Porosanu 		print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
1525045e3678SYuan Kang 			       DUMP_PREFIX_ADDRESS, 16, 4, desc,
1526045e3678SYuan Kang 			       desc_bytes(desc), 1);
1527045e3678SYuan Kang #endif
1528045e3678SYuan Kang 
152932686d34SRussell King 		ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_dst, req);
153032686d34SRussell King 		if (ret)
153158b0e5d0SMarkus Elfring 			goto unmap_ctx;
153232686d34SRussell King 
1533045e3678SYuan Kang 		ret = -EINPROGRESS;
1534045e3678SYuan Kang 		state->update = ahash_update_ctx;
1535045e3678SYuan Kang 		state->finup = ahash_finup_ctx;
1536045e3678SYuan Kang 		state->final = ahash_final_ctx;
1537045e3678SYuan Kang 	} else if (*next_buflen) {
1538045e3678SYuan Kang 		state->update = ahash_update_no_ctx;
1539045e3678SYuan Kang 		state->finup = ahash_finup_no_ctx;
1540045e3678SYuan Kang 		state->final = ahash_final_no_ctx;
1541307fd543SCristian Stoica 		scatterwalk_map_and_copy(next_buf, req->src, 0,
1542307fd543SCristian Stoica 					 req->nbytes, 0);
1543944c3d4dSHoria Geantă 		switch_buf(state);
1544045e3678SYuan Kang 	}
1545045e3678SYuan Kang #ifdef DEBUG
1546514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "next buf@"__stringify(__LINE__)": ",
1547045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, next_buf,
1548045e3678SYuan Kang 		       *next_buflen, 1);
1549045e3678SYuan Kang #endif
1550045e3678SYuan Kang 
1551045e3678SYuan Kang 	return ret;
155258b0e5d0SMarkus Elfring  unmap_ctx:
155332686d34SRussell King 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE);
155432686d34SRussell King 	kfree(edesc);
155532686d34SRussell King 	return ret;
1556045e3678SYuan Kang }
1557045e3678SYuan Kang 
1558045e3678SYuan Kang static int ahash_finup_first(struct ahash_request *req)
1559045e3678SYuan Kang {
1560045e3678SYuan Kang 	return ahash_digest(req);
1561045e3678SYuan Kang }
1562045e3678SYuan Kang 
1563045e3678SYuan Kang static int ahash_init(struct ahash_request *req)
1564045e3678SYuan Kang {
1565045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1566045e3678SYuan Kang 
1567045e3678SYuan Kang 	state->update = ahash_update_first;
1568045e3678SYuan Kang 	state->finup = ahash_finup_first;
1569045e3678SYuan Kang 	state->final = ahash_final_no_ctx;
1570045e3678SYuan Kang 
157187ec02e7SHoria Geantă 	state->ctx_dma = 0;
157265055e21SFranck LENORMAND 	state->ctx_dma_len = 0;
1573045e3678SYuan Kang 	state->current_buf = 0;
1574de0e35ecSHoria Geanta 	state->buf_dma = 0;
15756fd4b156SSteve Cornelius 	state->buflen_0 = 0;
15766fd4b156SSteve Cornelius 	state->buflen_1 = 0;
1577045e3678SYuan Kang 
1578045e3678SYuan Kang 	return 0;
1579045e3678SYuan Kang }
1580045e3678SYuan Kang 
1581045e3678SYuan Kang static int ahash_update(struct ahash_request *req)
1582045e3678SYuan Kang {
1583045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1584045e3678SYuan Kang 
1585045e3678SYuan Kang 	return state->update(req);
1586045e3678SYuan Kang }
1587045e3678SYuan Kang 
1588045e3678SYuan Kang static int ahash_finup(struct ahash_request *req)
1589045e3678SYuan Kang {
1590045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1591045e3678SYuan Kang 
1592045e3678SYuan Kang 	return state->finup(req);
1593045e3678SYuan Kang }
1594045e3678SYuan Kang 
1595045e3678SYuan Kang static int ahash_final(struct ahash_request *req)
1596045e3678SYuan Kang {
1597045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1598045e3678SYuan Kang 
1599045e3678SYuan Kang 	return state->final(req);
1600045e3678SYuan Kang }
1601045e3678SYuan Kang 
1602045e3678SYuan Kang static int ahash_export(struct ahash_request *req, void *out)
1603045e3678SYuan Kang {
1604045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
16055ec90831SRussell King 	struct caam_export_state *export = out;
16065ec90831SRussell King 	int len;
16075ec90831SRussell King 	u8 *buf;
1608045e3678SYuan Kang 
16095ec90831SRussell King 	if (state->current_buf) {
16105ec90831SRussell King 		buf = state->buf_1;
16115ec90831SRussell King 		len = state->buflen_1;
16125ec90831SRussell King 	} else {
16135ec90831SRussell King 		buf = state->buf_0;
1614f456cd2dSFabio Estevam 		len = state->buflen_0;
16155ec90831SRussell King 	}
16165ec90831SRussell King 
16175ec90831SRussell King 	memcpy(export->buf, buf, len);
16185ec90831SRussell King 	memcpy(export->caam_ctx, state->caam_ctx, sizeof(export->caam_ctx));
16195ec90831SRussell King 	export->buflen = len;
16205ec90831SRussell King 	export->update = state->update;
16215ec90831SRussell King 	export->final = state->final;
16225ec90831SRussell King 	export->finup = state->finup;
1623434b4212SRussell King 
1624045e3678SYuan Kang 	return 0;
1625045e3678SYuan Kang }
1626045e3678SYuan Kang 
1627045e3678SYuan Kang static int ahash_import(struct ahash_request *req, const void *in)
1628045e3678SYuan Kang {
1629045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
16305ec90831SRussell King 	const struct caam_export_state *export = in;
1631045e3678SYuan Kang 
16325ec90831SRussell King 	memset(state, 0, sizeof(*state));
16335ec90831SRussell King 	memcpy(state->buf_0, export->buf, export->buflen);
16345ec90831SRussell King 	memcpy(state->caam_ctx, export->caam_ctx, sizeof(state->caam_ctx));
16355ec90831SRussell King 	state->buflen_0 = export->buflen;
16365ec90831SRussell King 	state->update = export->update;
16375ec90831SRussell King 	state->final = export->final;
16385ec90831SRussell King 	state->finup = export->finup;
1639434b4212SRussell King 
1640045e3678SYuan Kang 	return 0;
1641045e3678SYuan Kang }
1642045e3678SYuan Kang 
1643045e3678SYuan Kang struct caam_hash_template {
1644045e3678SYuan Kang 	char name[CRYPTO_MAX_ALG_NAME];
1645045e3678SYuan Kang 	char driver_name[CRYPTO_MAX_ALG_NAME];
1646b0e09baeSYuan Kang 	char hmac_name[CRYPTO_MAX_ALG_NAME];
1647b0e09baeSYuan Kang 	char hmac_driver_name[CRYPTO_MAX_ALG_NAME];
1648045e3678SYuan Kang 	unsigned int blocksize;
1649045e3678SYuan Kang 	struct ahash_alg template_ahash;
1650045e3678SYuan Kang 	u32 alg_type;
1651045e3678SYuan Kang };
1652045e3678SYuan Kang 
1653045e3678SYuan Kang /* ahash descriptors */
1654045e3678SYuan Kang static struct caam_hash_template driver_hash[] = {
1655045e3678SYuan Kang 	{
1656b0e09baeSYuan Kang 		.name = "sha1",
1657b0e09baeSYuan Kang 		.driver_name = "sha1-caam",
1658b0e09baeSYuan Kang 		.hmac_name = "hmac(sha1)",
1659b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha1-caam",
1660045e3678SYuan Kang 		.blocksize = SHA1_BLOCK_SIZE,
1661045e3678SYuan Kang 		.template_ahash = {
1662045e3678SYuan Kang 			.init = ahash_init,
1663045e3678SYuan Kang 			.update = ahash_update,
1664045e3678SYuan Kang 			.final = ahash_final,
1665045e3678SYuan Kang 			.finup = ahash_finup,
1666045e3678SYuan Kang 			.digest = ahash_digest,
1667045e3678SYuan Kang 			.export = ahash_export,
1668045e3678SYuan Kang 			.import = ahash_import,
1669045e3678SYuan Kang 			.setkey = ahash_setkey,
1670045e3678SYuan Kang 			.halg = {
1671045e3678SYuan Kang 				.digestsize = SHA1_DIGEST_SIZE,
16725ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1673045e3678SYuan Kang 			},
1674045e3678SYuan Kang 		},
1675045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA1,
1676045e3678SYuan Kang 	}, {
1677b0e09baeSYuan Kang 		.name = "sha224",
1678b0e09baeSYuan Kang 		.driver_name = "sha224-caam",
1679b0e09baeSYuan Kang 		.hmac_name = "hmac(sha224)",
1680b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha224-caam",
1681045e3678SYuan Kang 		.blocksize = SHA224_BLOCK_SIZE,
1682045e3678SYuan Kang 		.template_ahash = {
1683045e3678SYuan Kang 			.init = ahash_init,
1684045e3678SYuan Kang 			.update = ahash_update,
1685045e3678SYuan Kang 			.final = ahash_final,
1686045e3678SYuan Kang 			.finup = ahash_finup,
1687045e3678SYuan Kang 			.digest = ahash_digest,
1688045e3678SYuan Kang 			.export = ahash_export,
1689045e3678SYuan Kang 			.import = ahash_import,
1690045e3678SYuan Kang 			.setkey = ahash_setkey,
1691045e3678SYuan Kang 			.halg = {
1692045e3678SYuan Kang 				.digestsize = SHA224_DIGEST_SIZE,
16935ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1694045e3678SYuan Kang 			},
1695045e3678SYuan Kang 		},
1696045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA224,
1697045e3678SYuan Kang 	}, {
1698b0e09baeSYuan Kang 		.name = "sha256",
1699b0e09baeSYuan Kang 		.driver_name = "sha256-caam",
1700b0e09baeSYuan Kang 		.hmac_name = "hmac(sha256)",
1701b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha256-caam",
1702045e3678SYuan Kang 		.blocksize = SHA256_BLOCK_SIZE,
1703045e3678SYuan Kang 		.template_ahash = {
1704045e3678SYuan Kang 			.init = ahash_init,
1705045e3678SYuan Kang 			.update = ahash_update,
1706045e3678SYuan Kang 			.final = ahash_final,
1707045e3678SYuan Kang 			.finup = ahash_finup,
1708045e3678SYuan Kang 			.digest = ahash_digest,
1709045e3678SYuan Kang 			.export = ahash_export,
1710045e3678SYuan Kang 			.import = ahash_import,
1711045e3678SYuan Kang 			.setkey = ahash_setkey,
1712045e3678SYuan Kang 			.halg = {
1713045e3678SYuan Kang 				.digestsize = SHA256_DIGEST_SIZE,
17145ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1715045e3678SYuan Kang 			},
1716045e3678SYuan Kang 		},
1717045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA256,
1718045e3678SYuan Kang 	}, {
1719b0e09baeSYuan Kang 		.name = "sha384",
1720b0e09baeSYuan Kang 		.driver_name = "sha384-caam",
1721b0e09baeSYuan Kang 		.hmac_name = "hmac(sha384)",
1722b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha384-caam",
1723045e3678SYuan Kang 		.blocksize = SHA384_BLOCK_SIZE,
1724045e3678SYuan Kang 		.template_ahash = {
1725045e3678SYuan Kang 			.init = ahash_init,
1726045e3678SYuan Kang 			.update = ahash_update,
1727045e3678SYuan Kang 			.final = ahash_final,
1728045e3678SYuan Kang 			.finup = ahash_finup,
1729045e3678SYuan Kang 			.digest = ahash_digest,
1730045e3678SYuan Kang 			.export = ahash_export,
1731045e3678SYuan Kang 			.import = ahash_import,
1732045e3678SYuan Kang 			.setkey = ahash_setkey,
1733045e3678SYuan Kang 			.halg = {
1734045e3678SYuan Kang 				.digestsize = SHA384_DIGEST_SIZE,
17355ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1736045e3678SYuan Kang 			},
1737045e3678SYuan Kang 		},
1738045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA384,
1739045e3678SYuan Kang 	}, {
1740b0e09baeSYuan Kang 		.name = "sha512",
1741b0e09baeSYuan Kang 		.driver_name = "sha512-caam",
1742b0e09baeSYuan Kang 		.hmac_name = "hmac(sha512)",
1743b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha512-caam",
1744045e3678SYuan Kang 		.blocksize = SHA512_BLOCK_SIZE,
1745045e3678SYuan Kang 		.template_ahash = {
1746045e3678SYuan Kang 			.init = ahash_init,
1747045e3678SYuan Kang 			.update = ahash_update,
1748045e3678SYuan Kang 			.final = ahash_final,
1749045e3678SYuan Kang 			.finup = ahash_finup,
1750045e3678SYuan Kang 			.digest = ahash_digest,
1751045e3678SYuan Kang 			.export = ahash_export,
1752045e3678SYuan Kang 			.import = ahash_import,
1753045e3678SYuan Kang 			.setkey = ahash_setkey,
1754045e3678SYuan Kang 			.halg = {
1755045e3678SYuan Kang 				.digestsize = SHA512_DIGEST_SIZE,
17565ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1757045e3678SYuan Kang 			},
1758045e3678SYuan Kang 		},
1759045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA512,
1760045e3678SYuan Kang 	}, {
1761b0e09baeSYuan Kang 		.name = "md5",
1762b0e09baeSYuan Kang 		.driver_name = "md5-caam",
1763b0e09baeSYuan Kang 		.hmac_name = "hmac(md5)",
1764b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-md5-caam",
1765045e3678SYuan Kang 		.blocksize = MD5_BLOCK_WORDS * 4,
1766045e3678SYuan Kang 		.template_ahash = {
1767045e3678SYuan Kang 			.init = ahash_init,
1768045e3678SYuan Kang 			.update = ahash_update,
1769045e3678SYuan Kang 			.final = ahash_final,
1770045e3678SYuan Kang 			.finup = ahash_finup,
1771045e3678SYuan Kang 			.digest = ahash_digest,
1772045e3678SYuan Kang 			.export = ahash_export,
1773045e3678SYuan Kang 			.import = ahash_import,
1774045e3678SYuan Kang 			.setkey = ahash_setkey,
1775045e3678SYuan Kang 			.halg = {
1776045e3678SYuan Kang 				.digestsize = MD5_DIGEST_SIZE,
17775ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1778045e3678SYuan Kang 			},
1779045e3678SYuan Kang 		},
1780045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_MD5,
178112b8567fSIuliana Prodan 	}, {
178212b8567fSIuliana Prodan 		.hmac_name = "xcbc(aes)",
178312b8567fSIuliana Prodan 		.hmac_driver_name = "xcbc-aes-caam",
178412b8567fSIuliana Prodan 		.blocksize = AES_BLOCK_SIZE,
178512b8567fSIuliana Prodan 		.template_ahash = {
178612b8567fSIuliana Prodan 			.init = ahash_init,
178712b8567fSIuliana Prodan 			.update = ahash_update,
178812b8567fSIuliana Prodan 			.final = ahash_final,
178912b8567fSIuliana Prodan 			.finup = ahash_finup,
179012b8567fSIuliana Prodan 			.digest = ahash_digest,
179112b8567fSIuliana Prodan 			.export = ahash_export,
179212b8567fSIuliana Prodan 			.import = ahash_import,
179312b8567fSIuliana Prodan 			.setkey = axcbc_setkey,
179412b8567fSIuliana Prodan 			.halg = {
179512b8567fSIuliana Prodan 				.digestsize = AES_BLOCK_SIZE,
179612b8567fSIuliana Prodan 				.statesize = sizeof(struct caam_export_state),
179712b8567fSIuliana Prodan 			},
179812b8567fSIuliana Prodan 		 },
179912b8567fSIuliana Prodan 		.alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_XCBC_MAC,
180087870cfbSIuliana Prodan 	}, {
180187870cfbSIuliana Prodan 		.hmac_name = "cmac(aes)",
180287870cfbSIuliana Prodan 		.hmac_driver_name = "cmac-aes-caam",
180387870cfbSIuliana Prodan 		.blocksize = AES_BLOCK_SIZE,
180487870cfbSIuliana Prodan 		.template_ahash = {
180587870cfbSIuliana Prodan 			.init = ahash_init,
180687870cfbSIuliana Prodan 			.update = ahash_update,
180787870cfbSIuliana Prodan 			.final = ahash_final,
180887870cfbSIuliana Prodan 			.finup = ahash_finup,
180987870cfbSIuliana Prodan 			.digest = ahash_digest,
181087870cfbSIuliana Prodan 			.export = ahash_export,
181187870cfbSIuliana Prodan 			.import = ahash_import,
181287870cfbSIuliana Prodan 			.setkey = acmac_setkey,
181387870cfbSIuliana Prodan 			.halg = {
181487870cfbSIuliana Prodan 				.digestsize = AES_BLOCK_SIZE,
181587870cfbSIuliana Prodan 				.statesize = sizeof(struct caam_export_state),
181687870cfbSIuliana Prodan 			},
181787870cfbSIuliana Prodan 		 },
181887870cfbSIuliana Prodan 		.alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CMAC,
1819045e3678SYuan Kang 	},
1820045e3678SYuan Kang };
1821045e3678SYuan Kang 
1822045e3678SYuan Kang struct caam_hash_alg {
1823045e3678SYuan Kang 	struct list_head entry;
1824045e3678SYuan Kang 	int alg_type;
1825045e3678SYuan Kang 	struct ahash_alg ahash_alg;
1826045e3678SYuan Kang };
1827045e3678SYuan Kang 
1828045e3678SYuan Kang static int caam_hash_cra_init(struct crypto_tfm *tfm)
1829045e3678SYuan Kang {
1830045e3678SYuan Kang 	struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
1831045e3678SYuan Kang 	struct crypto_alg *base = tfm->__crt_alg;
1832045e3678SYuan Kang 	struct hash_alg_common *halg =
1833045e3678SYuan Kang 		 container_of(base, struct hash_alg_common, base);
1834045e3678SYuan Kang 	struct ahash_alg *alg =
1835045e3678SYuan Kang 		 container_of(halg, struct ahash_alg, halg);
1836045e3678SYuan Kang 	struct caam_hash_alg *caam_hash =
1837045e3678SYuan Kang 		 container_of(alg, struct caam_hash_alg, ahash_alg);
1838045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm);
1839045e3678SYuan Kang 	/* Sizes for MDHA running digests: MD5, SHA1, 224, 256, 384, 512 */
1840045e3678SYuan Kang 	static const u8 runninglen[] = { HASH_MSG_LEN + MD5_DIGEST_SIZE,
1841045e3678SYuan Kang 					 HASH_MSG_LEN + SHA1_DIGEST_SIZE,
1842045e3678SYuan Kang 					 HASH_MSG_LEN + 32,
1843045e3678SYuan Kang 					 HASH_MSG_LEN + SHA256_DIGEST_SIZE,
1844045e3678SYuan Kang 					 HASH_MSG_LEN + 64,
1845045e3678SYuan Kang 					 HASH_MSG_LEN + SHA512_DIGEST_SIZE };
1846bbf22344SHoria Geantă 	dma_addr_t dma_addr;
18477e0880b9SHoria Geantă 	struct caam_drv_private *priv;
1848045e3678SYuan Kang 
1849045e3678SYuan Kang 	/*
1850cfc6f11bSRuchika Gupta 	 * Get a Job ring from Job Ring driver to ensure in-order
1851045e3678SYuan Kang 	 * crypto request processing per tfm
1852045e3678SYuan Kang 	 */
1853cfc6f11bSRuchika Gupta 	ctx->jrdev = caam_jr_alloc();
1854cfc6f11bSRuchika Gupta 	if (IS_ERR(ctx->jrdev)) {
1855cfc6f11bSRuchika Gupta 		pr_err("Job Ring Device allocation for transform failed\n");
1856cfc6f11bSRuchika Gupta 		return PTR_ERR(ctx->jrdev);
1857cfc6f11bSRuchika Gupta 	}
1858bbf22344SHoria Geantă 
18597e0880b9SHoria Geantă 	priv = dev_get_drvdata(ctx->jrdev->parent);
186012b8567fSIuliana Prodan 
186112b8567fSIuliana Prodan 	if (is_xcbc_aes(caam_hash->alg_type)) {
186212b8567fSIuliana Prodan 		ctx->dir = DMA_TO_DEVICE;
186312b8567fSIuliana Prodan 		ctx->adata.algtype = OP_TYPE_CLASS1_ALG | caam_hash->alg_type;
186412b8567fSIuliana Prodan 		ctx->ctx_len = 48;
186512b8567fSIuliana Prodan 
186612b8567fSIuliana Prodan 		ctx->key_dma = dma_map_single_attrs(ctx->jrdev, ctx->key,
186712b8567fSIuliana Prodan 						    ARRAY_SIZE(ctx->key),
186812b8567fSIuliana Prodan 						    DMA_BIDIRECTIONAL,
186912b8567fSIuliana Prodan 						    DMA_ATTR_SKIP_CPU_SYNC);
187012b8567fSIuliana Prodan 		if (dma_mapping_error(ctx->jrdev, ctx->key_dma)) {
187112b8567fSIuliana Prodan 			dev_err(ctx->jrdev, "unable to map key\n");
187212b8567fSIuliana Prodan 			caam_jr_free(ctx->jrdev);
187312b8567fSIuliana Prodan 			return -ENOMEM;
187412b8567fSIuliana Prodan 		}
187587870cfbSIuliana Prodan 	} else if (is_cmac_aes(caam_hash->alg_type)) {
187687870cfbSIuliana Prodan 		ctx->dir = DMA_TO_DEVICE;
187787870cfbSIuliana Prodan 		ctx->adata.algtype = OP_TYPE_CLASS1_ALG | caam_hash->alg_type;
187887870cfbSIuliana Prodan 		ctx->ctx_len = 32;
187912b8567fSIuliana Prodan 	} else {
18807e0880b9SHoria Geantă 		ctx->dir = priv->era >= 6 ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
188112b8567fSIuliana Prodan 		ctx->adata.algtype = OP_TYPE_CLASS2_ALG | caam_hash->alg_type;
188212b8567fSIuliana Prodan 		ctx->ctx_len = runninglen[(ctx->adata.algtype &
188312b8567fSIuliana Prodan 					   OP_ALG_ALGSEL_SUBMASK) >>
188412b8567fSIuliana Prodan 					  OP_ALG_ALGSEL_SHIFT];
188512b8567fSIuliana Prodan 	}
18867e0880b9SHoria Geantă 
1887bbf22344SHoria Geantă 	dma_addr = dma_map_single_attrs(ctx->jrdev, ctx->sh_desc_update,
18888e731ee5SHoria Geantă 					offsetof(struct caam_hash_ctx, key),
18897e0880b9SHoria Geantă 					ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
1890bbf22344SHoria Geantă 	if (dma_mapping_error(ctx->jrdev, dma_addr)) {
1891bbf22344SHoria Geantă 		dev_err(ctx->jrdev, "unable to map shared descriptors\n");
189212b8567fSIuliana Prodan 
189312b8567fSIuliana Prodan 		if (is_xcbc_aes(caam_hash->alg_type))
189412b8567fSIuliana Prodan 			dma_unmap_single_attrs(ctx->jrdev, ctx->key_dma,
189512b8567fSIuliana Prodan 					       ARRAY_SIZE(ctx->key),
189612b8567fSIuliana Prodan 					       DMA_BIDIRECTIONAL,
189712b8567fSIuliana Prodan 					       DMA_ATTR_SKIP_CPU_SYNC);
189812b8567fSIuliana Prodan 
1899bbf22344SHoria Geantă 		caam_jr_free(ctx->jrdev);
1900bbf22344SHoria Geantă 		return -ENOMEM;
1901bbf22344SHoria Geantă 	}
1902bbf22344SHoria Geantă 
1903bbf22344SHoria Geantă 	ctx->sh_desc_update_dma = dma_addr;
1904bbf22344SHoria Geantă 	ctx->sh_desc_update_first_dma = dma_addr +
1905bbf22344SHoria Geantă 					offsetof(struct caam_hash_ctx,
1906bbf22344SHoria Geantă 						 sh_desc_update_first);
1907bbf22344SHoria Geantă 	ctx->sh_desc_fin_dma = dma_addr + offsetof(struct caam_hash_ctx,
1908bbf22344SHoria Geantă 						   sh_desc_fin);
1909bbf22344SHoria Geantă 	ctx->sh_desc_digest_dma = dma_addr + offsetof(struct caam_hash_ctx,
1910bbf22344SHoria Geantă 						      sh_desc_digest);
1911bbf22344SHoria Geantă 
1912045e3678SYuan Kang 	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
1913045e3678SYuan Kang 				 sizeof(struct caam_hash_state));
19149a2537d0SIuliana Prodan 
19159a2537d0SIuliana Prodan 	/*
19169a2537d0SIuliana Prodan 	 * For keyed hash algorithms shared descriptors
19179a2537d0SIuliana Prodan 	 * will be created later in setkey() callback
19189a2537d0SIuliana Prodan 	 */
19199a2537d0SIuliana Prodan 	return alg->setkey ? 0 : ahash_set_sh_desc(ahash);
1920045e3678SYuan Kang }
1921045e3678SYuan Kang 
1922045e3678SYuan Kang static void caam_hash_cra_exit(struct crypto_tfm *tfm)
1923045e3678SYuan Kang {
1924045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm);
1925045e3678SYuan Kang 
1926bbf22344SHoria Geantă 	dma_unmap_single_attrs(ctx->jrdev, ctx->sh_desc_update_dma,
192712b8567fSIuliana Prodan 			       offsetof(struct caam_hash_ctx, key),
19287e0880b9SHoria Geantă 			       ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
192912b8567fSIuliana Prodan 	if (is_xcbc_aes(ctx->adata.algtype))
193012b8567fSIuliana Prodan 		dma_unmap_single_attrs(ctx->jrdev, ctx->key_dma,
193112b8567fSIuliana Prodan 				       ARRAY_SIZE(ctx->key), DMA_BIDIRECTIONAL,
193212b8567fSIuliana Prodan 				       DMA_ATTR_SKIP_CPU_SYNC);
1933cfc6f11bSRuchika Gupta 	caam_jr_free(ctx->jrdev);
1934045e3678SYuan Kang }
1935045e3678SYuan Kang 
1936*1b46c90cSHoria Geantă void caam_algapi_hash_exit(void)
1937045e3678SYuan Kang {
1938045e3678SYuan Kang 	struct caam_hash_alg *t_alg, *n;
1939045e3678SYuan Kang 
1940cfc6f11bSRuchika Gupta 	if (!hash_list.next)
1941045e3678SYuan Kang 		return;
1942045e3678SYuan Kang 
1943cfc6f11bSRuchika Gupta 	list_for_each_entry_safe(t_alg, n, &hash_list, entry) {
1944045e3678SYuan Kang 		crypto_unregister_ahash(&t_alg->ahash_alg);
1945045e3678SYuan Kang 		list_del(&t_alg->entry);
1946045e3678SYuan Kang 		kfree(t_alg);
1947045e3678SYuan Kang 	}
1948045e3678SYuan Kang }
1949045e3678SYuan Kang 
1950045e3678SYuan Kang static struct caam_hash_alg *
1951cfc6f11bSRuchika Gupta caam_hash_alloc(struct caam_hash_template *template,
1952b0e09baeSYuan Kang 		bool keyed)
1953045e3678SYuan Kang {
1954045e3678SYuan Kang 	struct caam_hash_alg *t_alg;
1955045e3678SYuan Kang 	struct ahash_alg *halg;
1956045e3678SYuan Kang 	struct crypto_alg *alg;
1957045e3678SYuan Kang 
19589c4f9733SFabio Estevam 	t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL);
1959045e3678SYuan Kang 	if (!t_alg) {
1960cfc6f11bSRuchika Gupta 		pr_err("failed to allocate t_alg\n");
1961045e3678SYuan Kang 		return ERR_PTR(-ENOMEM);
1962045e3678SYuan Kang 	}
1963045e3678SYuan Kang 
1964045e3678SYuan Kang 	t_alg->ahash_alg = template->template_ahash;
1965045e3678SYuan Kang 	halg = &t_alg->ahash_alg;
1966045e3678SYuan Kang 	alg = &halg->halg.base;
1967045e3678SYuan Kang 
1968b0e09baeSYuan Kang 	if (keyed) {
1969b0e09baeSYuan Kang 		snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
1970b0e09baeSYuan Kang 			 template->hmac_name);
1971b0e09baeSYuan Kang 		snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
1972b0e09baeSYuan Kang 			 template->hmac_driver_name);
1973b0e09baeSYuan Kang 	} else {
1974b0e09baeSYuan Kang 		snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
1975b0e09baeSYuan Kang 			 template->name);
1976045e3678SYuan Kang 		snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
1977045e3678SYuan Kang 			 template->driver_name);
1978a0118c8bSRussell King 		t_alg->ahash_alg.setkey = NULL;
1979b0e09baeSYuan Kang 	}
1980045e3678SYuan Kang 	alg->cra_module = THIS_MODULE;
1981045e3678SYuan Kang 	alg->cra_init = caam_hash_cra_init;
1982045e3678SYuan Kang 	alg->cra_exit = caam_hash_cra_exit;
1983045e3678SYuan Kang 	alg->cra_ctxsize = sizeof(struct caam_hash_ctx);
1984045e3678SYuan Kang 	alg->cra_priority = CAAM_CRA_PRIORITY;
1985045e3678SYuan Kang 	alg->cra_blocksize = template->blocksize;
1986045e3678SYuan Kang 	alg->cra_alignmask = 0;
19876a38f622SEric Biggers 	alg->cra_flags = CRYPTO_ALG_ASYNC;
1988045e3678SYuan Kang 
1989045e3678SYuan Kang 	t_alg->alg_type = template->alg_type;
1990045e3678SYuan Kang 
1991045e3678SYuan Kang 	return t_alg;
1992045e3678SYuan Kang }
1993045e3678SYuan Kang 
1994*1b46c90cSHoria Geantă int caam_algapi_hash_init(struct device *ctrldev)
1995045e3678SYuan Kang {
1996045e3678SYuan Kang 	int i = 0, err = 0;
1997*1b46c90cSHoria Geantă 	struct caam_drv_private *priv = dev_get_drvdata(ctrldev);
1998bf83490eSVictoria Milhoan 	unsigned int md_limit = SHA512_DIGEST_SIZE;
1999d239b10dSHoria Geantă 	u32 md_inst, md_vid;
2000045e3678SYuan Kang 
2001bf83490eSVictoria Milhoan 	/*
2002bf83490eSVictoria Milhoan 	 * Register crypto algorithms the device supports.  First, identify
2003bf83490eSVictoria Milhoan 	 * presence and attributes of MD block.
2004bf83490eSVictoria Milhoan 	 */
2005d239b10dSHoria Geantă 	if (priv->era < 10) {
2006d239b10dSHoria Geantă 		md_vid = (rd_reg32(&priv->ctrl->perfmon.cha_id_ls) &
2007d239b10dSHoria Geantă 			  CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;
2008d239b10dSHoria Geantă 		md_inst = (rd_reg32(&priv->ctrl->perfmon.cha_num_ls) &
2009d239b10dSHoria Geantă 			   CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;
2010d239b10dSHoria Geantă 	} else {
2011d239b10dSHoria Geantă 		u32 mdha = rd_reg32(&priv->ctrl->vreg.mdha);
2012d239b10dSHoria Geantă 
2013d239b10dSHoria Geantă 		md_vid = (mdha & CHA_VER_VID_MASK) >> CHA_VER_VID_SHIFT;
2014d239b10dSHoria Geantă 		md_inst = mdha & CHA_VER_NUM_MASK;
2015d239b10dSHoria Geantă 	}
2016bf83490eSVictoria Milhoan 
2017bf83490eSVictoria Milhoan 	/*
2018bf83490eSVictoria Milhoan 	 * Skip registration of any hashing algorithms if MD block
2019bf83490eSVictoria Milhoan 	 * is not present.
2020bf83490eSVictoria Milhoan 	 */
2021*1b46c90cSHoria Geantă 	if (!md_inst)
2022*1b46c90cSHoria Geantă 		return -ENODEV;
2023bf83490eSVictoria Milhoan 
2024bf83490eSVictoria Milhoan 	/* Limit digest size based on LP256 */
2025d239b10dSHoria Geantă 	if (md_vid == CHA_VER_VID_MD_LP256)
2026bf83490eSVictoria Milhoan 		md_limit = SHA256_DIGEST_SIZE;
2027bf83490eSVictoria Milhoan 
2028cfc6f11bSRuchika Gupta 	INIT_LIST_HEAD(&hash_list);
2029045e3678SYuan Kang 
2030045e3678SYuan Kang 	/* register crypto algorithms the device supports */
2031045e3678SYuan Kang 	for (i = 0; i < ARRAY_SIZE(driver_hash); i++) {
2032045e3678SYuan Kang 		struct caam_hash_alg *t_alg;
2033bf83490eSVictoria Milhoan 		struct caam_hash_template *alg = driver_hash + i;
2034bf83490eSVictoria Milhoan 
2035bf83490eSVictoria Milhoan 		/* If MD size is not supported by device, skip registration */
203612b8567fSIuliana Prodan 		if (is_mdha(alg->alg_type) &&
203712b8567fSIuliana Prodan 		    alg->template_ahash.halg.digestsize > md_limit)
2038bf83490eSVictoria Milhoan 			continue;
2039045e3678SYuan Kang 
2040b0e09baeSYuan Kang 		/* register hmac version */
2041bf83490eSVictoria Milhoan 		t_alg = caam_hash_alloc(alg, true);
2042b0e09baeSYuan Kang 		if (IS_ERR(t_alg)) {
2043b0e09baeSYuan Kang 			err = PTR_ERR(t_alg);
20440f103b37SIuliana Prodan 			pr_warn("%s alg allocation failed\n",
20450f103b37SIuliana Prodan 				alg->hmac_driver_name);
2046b0e09baeSYuan Kang 			continue;
2047b0e09baeSYuan Kang 		}
2048b0e09baeSYuan Kang 
2049b0e09baeSYuan Kang 		err = crypto_register_ahash(&t_alg->ahash_alg);
2050b0e09baeSYuan Kang 		if (err) {
20516ea30f0aSRussell King 			pr_warn("%s alg registration failed: %d\n",
20526ea30f0aSRussell King 				t_alg->ahash_alg.halg.base.cra_driver_name,
20536ea30f0aSRussell King 				err);
2054b0e09baeSYuan Kang 			kfree(t_alg);
2055b0e09baeSYuan Kang 		} else
2056cfc6f11bSRuchika Gupta 			list_add_tail(&t_alg->entry, &hash_list);
2057b0e09baeSYuan Kang 
205812b8567fSIuliana Prodan 		if ((alg->alg_type & OP_ALG_ALGSEL_MASK) == OP_ALG_ALGSEL_AES)
205912b8567fSIuliana Prodan 			continue;
206012b8567fSIuliana Prodan 
2061b0e09baeSYuan Kang 		/* register unkeyed version */
2062bf83490eSVictoria Milhoan 		t_alg = caam_hash_alloc(alg, false);
2063045e3678SYuan Kang 		if (IS_ERR(t_alg)) {
2064045e3678SYuan Kang 			err = PTR_ERR(t_alg);
2065bf83490eSVictoria Milhoan 			pr_warn("%s alg allocation failed\n", alg->driver_name);
2066045e3678SYuan Kang 			continue;
2067045e3678SYuan Kang 		}
2068045e3678SYuan Kang 
2069045e3678SYuan Kang 		err = crypto_register_ahash(&t_alg->ahash_alg);
2070045e3678SYuan Kang 		if (err) {
20716ea30f0aSRussell King 			pr_warn("%s alg registration failed: %d\n",
20726ea30f0aSRussell King 				t_alg->ahash_alg.halg.base.cra_driver_name,
20736ea30f0aSRussell King 				err);
2074045e3678SYuan Kang 			kfree(t_alg);
2075045e3678SYuan Kang 		} else
2076cfc6f11bSRuchika Gupta 			list_add_tail(&t_alg->entry, &hash_list);
2077045e3678SYuan Kang 	}
2078045e3678SYuan Kang 
2079045e3678SYuan Kang 	return err;
2080045e3678SYuan Kang }
2081