xref: /linux/drivers/crypto/caam/caamhash.c (revision d6e7a7d0c2c5170234f0afb94b2bcdaf93630a72)
1045e3678SYuan Kang /*
2045e3678SYuan Kang  * caam - Freescale FSL CAAM support for ahash functions of crypto API
3045e3678SYuan Kang  *
4045e3678SYuan Kang  * Copyright 2011 Freescale Semiconductor, Inc.
5045e3678SYuan Kang  *
6045e3678SYuan Kang  * Based on caamalg.c crypto API driver.
7045e3678SYuan Kang  *
8045e3678SYuan Kang  * relationship of digest job descriptor or first job descriptor after init to
9045e3678SYuan Kang  * shared descriptors:
10045e3678SYuan Kang  *
11045e3678SYuan Kang  * ---------------                     ---------------
12045e3678SYuan Kang  * | JobDesc #1  |-------------------->|  ShareDesc  |
13045e3678SYuan Kang  * | *(packet 1) |                     |  (hashKey)  |
14045e3678SYuan Kang  * ---------------                     | (operation) |
15045e3678SYuan Kang  *                                     ---------------
16045e3678SYuan Kang  *
17045e3678SYuan Kang  * relationship of subsequent job descriptors to shared descriptors:
18045e3678SYuan Kang  *
19045e3678SYuan Kang  * ---------------                     ---------------
20045e3678SYuan Kang  * | JobDesc #2  |-------------------->|  ShareDesc  |
21045e3678SYuan Kang  * | *(packet 2) |      |------------->|  (hashKey)  |
22045e3678SYuan Kang  * ---------------      |    |-------->| (operation) |
23045e3678SYuan Kang  *       .              |    |         | (load ctx2) |
24045e3678SYuan Kang  *       .              |    |         ---------------
25045e3678SYuan Kang  * ---------------      |    |
26045e3678SYuan Kang  * | JobDesc #3  |------|    |
27045e3678SYuan Kang  * | *(packet 3) |           |
28045e3678SYuan Kang  * ---------------           |
29045e3678SYuan Kang  *       .                   |
30045e3678SYuan Kang  *       .                   |
31045e3678SYuan Kang  * ---------------           |
32045e3678SYuan Kang  * | JobDesc #4  |------------
33045e3678SYuan Kang  * | *(packet 4) |
34045e3678SYuan Kang  * ---------------
35045e3678SYuan Kang  *
36045e3678SYuan Kang  * The SharedDesc never changes for a connection unless rekeyed, but
37045e3678SYuan Kang  * each packet will likely be in a different place. So all we need
38045e3678SYuan Kang  * to know to process the packet is where the input is, where the
39045e3678SYuan Kang  * output goes, and what context we want to process with. Context is
40045e3678SYuan Kang  * in the SharedDesc, packet references in the JobDesc.
41045e3678SYuan Kang  *
42045e3678SYuan Kang  * So, a job desc looks like:
43045e3678SYuan Kang  *
44045e3678SYuan Kang  * ---------------------
45045e3678SYuan Kang  * | Header            |
46045e3678SYuan Kang  * | ShareDesc Pointer |
47045e3678SYuan Kang  * | SEQ_OUT_PTR       |
48045e3678SYuan Kang  * | (output buffer)   |
49045e3678SYuan Kang  * | (output length)   |
50045e3678SYuan Kang  * | SEQ_IN_PTR        |
51045e3678SYuan Kang  * | (input buffer)    |
52045e3678SYuan Kang  * | (input length)    |
53045e3678SYuan Kang  * ---------------------
54045e3678SYuan Kang  */
55045e3678SYuan Kang 
56045e3678SYuan Kang #include "compat.h"
57045e3678SYuan Kang 
58045e3678SYuan Kang #include "regs.h"
59045e3678SYuan Kang #include "intern.h"
60045e3678SYuan Kang #include "desc_constr.h"
61045e3678SYuan Kang #include "jr.h"
62045e3678SYuan Kang #include "error.h"
63045e3678SYuan Kang #include "sg_sw_sec4.h"
64045e3678SYuan Kang #include "key_gen.h"
65045e3678SYuan Kang 
66045e3678SYuan Kang #define CAAM_CRA_PRIORITY		3000
67045e3678SYuan Kang 
68045e3678SYuan Kang /* max hash key is max split key size */
69045e3678SYuan Kang #define CAAM_MAX_HASH_KEY_SIZE		(SHA512_DIGEST_SIZE * 2)
70045e3678SYuan Kang 
71045e3678SYuan Kang #define CAAM_MAX_HASH_BLOCK_SIZE	SHA512_BLOCK_SIZE
72045e3678SYuan Kang #define CAAM_MAX_HASH_DIGEST_SIZE	SHA512_DIGEST_SIZE
73045e3678SYuan Kang 
74045e3678SYuan Kang /* length of descriptors text */
75045e3678SYuan Kang #define DESC_AHASH_BASE			(4 * CAAM_CMD_SZ)
76045e3678SYuan Kang #define DESC_AHASH_UPDATE_LEN		(6 * CAAM_CMD_SZ)
77045e3678SYuan Kang #define DESC_AHASH_UPDATE_FIRST_LEN	(DESC_AHASH_BASE + 4 * CAAM_CMD_SZ)
78045e3678SYuan Kang #define DESC_AHASH_FINAL_LEN		(DESC_AHASH_BASE + 5 * CAAM_CMD_SZ)
79045e3678SYuan Kang #define DESC_AHASH_FINUP_LEN		(DESC_AHASH_BASE + 5 * CAAM_CMD_SZ)
80045e3678SYuan Kang #define DESC_AHASH_DIGEST_LEN		(DESC_AHASH_BASE + 4 * CAAM_CMD_SZ)
81045e3678SYuan Kang 
82045e3678SYuan Kang #define DESC_HASH_MAX_USED_BYTES	(DESC_AHASH_FINAL_LEN + \
83045e3678SYuan Kang 					 CAAM_MAX_HASH_KEY_SIZE)
84045e3678SYuan Kang #define DESC_HASH_MAX_USED_LEN		(DESC_HASH_MAX_USED_BYTES / CAAM_CMD_SZ)
85045e3678SYuan Kang 
86045e3678SYuan Kang /* caam context sizes for hashes: running digest + 8 */
87045e3678SYuan Kang #define HASH_MSG_LEN			8
88045e3678SYuan Kang #define MAX_CTX_LEN			(HASH_MSG_LEN + SHA512_DIGEST_SIZE)
89045e3678SYuan Kang 
90045e3678SYuan Kang #ifdef DEBUG
91045e3678SYuan Kang /* for print_hex_dumps with line references */
92045e3678SYuan Kang #define debug(format, arg...) printk(format, arg)
93045e3678SYuan Kang #else
94045e3678SYuan Kang #define debug(format, arg...)
95045e3678SYuan Kang #endif
96045e3678SYuan Kang 
97cfc6f11bSRuchika Gupta 
98cfc6f11bSRuchika Gupta static struct list_head hash_list;
99cfc6f11bSRuchika Gupta 
100045e3678SYuan Kang /* ahash per-session context */
101045e3678SYuan Kang struct caam_hash_ctx {
102e11793f5SRussell King 	u32 sh_desc_update[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
103e11793f5SRussell King 	u32 sh_desc_update_first[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
104e11793f5SRussell King 	u32 sh_desc_fin[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
105e11793f5SRussell King 	u32 sh_desc_digest[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
106e11793f5SRussell King 	u32 sh_desc_finup[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
107e11793f5SRussell King 	dma_addr_t sh_desc_update_dma ____cacheline_aligned;
108045e3678SYuan Kang 	dma_addr_t sh_desc_update_first_dma;
109045e3678SYuan Kang 	dma_addr_t sh_desc_fin_dma;
110045e3678SYuan Kang 	dma_addr_t sh_desc_digest_dma;
111045e3678SYuan Kang 	dma_addr_t sh_desc_finup_dma;
112e11793f5SRussell King 	struct device *jrdev;
113045e3678SYuan Kang 	u32 alg_type;
114045e3678SYuan Kang 	u32 alg_op;
115045e3678SYuan Kang 	u8 key[CAAM_MAX_HASH_KEY_SIZE];
116045e3678SYuan Kang 	dma_addr_t key_dma;
117045e3678SYuan Kang 	int ctx_len;
118045e3678SYuan Kang 	unsigned int split_key_len;
119045e3678SYuan Kang 	unsigned int split_key_pad_len;
120045e3678SYuan Kang };
121045e3678SYuan Kang 
122045e3678SYuan Kang /* ahash state */
123045e3678SYuan Kang struct caam_hash_state {
124045e3678SYuan Kang 	dma_addr_t buf_dma;
125045e3678SYuan Kang 	dma_addr_t ctx_dma;
126045e3678SYuan Kang 	u8 buf_0[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
127045e3678SYuan Kang 	int buflen_0;
128045e3678SYuan Kang 	u8 buf_1[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
129045e3678SYuan Kang 	int buflen_1;
130e7472422SVictoria Milhoan 	u8 caam_ctx[MAX_CTX_LEN] ____cacheline_aligned;
131045e3678SYuan Kang 	int (*update)(struct ahash_request *req);
132045e3678SYuan Kang 	int (*final)(struct ahash_request *req);
133045e3678SYuan Kang 	int (*finup)(struct ahash_request *req);
134045e3678SYuan Kang 	int current_buf;
135045e3678SYuan Kang };
136045e3678SYuan Kang 
1375ec90831SRussell King struct caam_export_state {
1385ec90831SRussell King 	u8 buf[CAAM_MAX_HASH_BLOCK_SIZE];
1395ec90831SRussell King 	u8 caam_ctx[MAX_CTX_LEN];
1405ec90831SRussell King 	int buflen;
1415ec90831SRussell King 	int (*update)(struct ahash_request *req);
1425ec90831SRussell King 	int (*final)(struct ahash_request *req);
1435ec90831SRussell King 	int (*finup)(struct ahash_request *req);
1445ec90831SRussell King };
1455ec90831SRussell King 
146045e3678SYuan Kang /* Common job descriptor seq in/out ptr routines */
147045e3678SYuan Kang 
148045e3678SYuan Kang /* Map state->caam_ctx, and append seq_out_ptr command that points to it */
149ce572085SHoria Geanta static inline int map_seq_out_ptr_ctx(u32 *desc, struct device *jrdev,
150045e3678SYuan Kang 				      struct caam_hash_state *state,
151045e3678SYuan Kang 				      int ctx_len)
152045e3678SYuan Kang {
153045e3678SYuan Kang 	state->ctx_dma = dma_map_single(jrdev, state->caam_ctx,
154045e3678SYuan Kang 					ctx_len, DMA_FROM_DEVICE);
155ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, state->ctx_dma)) {
156ce572085SHoria Geanta 		dev_err(jrdev, "unable to map ctx\n");
157ce572085SHoria Geanta 		return -ENOMEM;
158ce572085SHoria Geanta 	}
159ce572085SHoria Geanta 
160045e3678SYuan Kang 	append_seq_out_ptr(desc, state->ctx_dma, ctx_len, 0);
161ce572085SHoria Geanta 
162ce572085SHoria Geanta 	return 0;
163045e3678SYuan Kang }
164045e3678SYuan Kang 
165045e3678SYuan Kang /* Map req->result, and append seq_out_ptr command that points to it */
166045e3678SYuan Kang static inline dma_addr_t map_seq_out_ptr_result(u32 *desc, struct device *jrdev,
167045e3678SYuan Kang 						u8 *result, int digestsize)
168045e3678SYuan Kang {
169045e3678SYuan Kang 	dma_addr_t dst_dma;
170045e3678SYuan Kang 
171045e3678SYuan Kang 	dst_dma = dma_map_single(jrdev, result, digestsize, DMA_FROM_DEVICE);
172045e3678SYuan Kang 	append_seq_out_ptr(desc, dst_dma, digestsize, 0);
173045e3678SYuan Kang 
174045e3678SYuan Kang 	return dst_dma;
175045e3678SYuan Kang }
176045e3678SYuan Kang 
177045e3678SYuan Kang /* Map current buffer in state and put it in link table */
178045e3678SYuan Kang static inline dma_addr_t buf_map_to_sec4_sg(struct device *jrdev,
179045e3678SYuan Kang 					    struct sec4_sg_entry *sec4_sg,
180045e3678SYuan Kang 					    u8 *buf, int buflen)
181045e3678SYuan Kang {
182045e3678SYuan Kang 	dma_addr_t buf_dma;
183045e3678SYuan Kang 
184045e3678SYuan Kang 	buf_dma = dma_map_single(jrdev, buf, buflen, DMA_TO_DEVICE);
185045e3678SYuan Kang 	dma_to_sec4_sg_one(sec4_sg, buf_dma, buflen, 0);
186045e3678SYuan Kang 
187045e3678SYuan Kang 	return buf_dma;
188045e3678SYuan Kang }
189045e3678SYuan Kang 
190045e3678SYuan Kang /*
191045e3678SYuan Kang  * Only put buffer in link table if it contains data, which is possible,
192045e3678SYuan Kang  * since a buffer has previously been used, and needs to be unmapped,
193045e3678SYuan Kang  */
194045e3678SYuan Kang static inline dma_addr_t
195045e3678SYuan Kang try_buf_map_to_sec4_sg(struct device *jrdev, struct sec4_sg_entry *sec4_sg,
196045e3678SYuan Kang 		       u8 *buf, dma_addr_t buf_dma, int buflen,
197045e3678SYuan Kang 		       int last_buflen)
198045e3678SYuan Kang {
199045e3678SYuan Kang 	if (buf_dma && !dma_mapping_error(jrdev, buf_dma))
200045e3678SYuan Kang 		dma_unmap_single(jrdev, buf_dma, last_buflen, DMA_TO_DEVICE);
201045e3678SYuan Kang 	if (buflen)
202045e3678SYuan Kang 		buf_dma = buf_map_to_sec4_sg(jrdev, sec4_sg, buf, buflen);
203045e3678SYuan Kang 	else
204045e3678SYuan Kang 		buf_dma = 0;
205045e3678SYuan Kang 
206045e3678SYuan Kang 	return buf_dma;
207045e3678SYuan Kang }
208045e3678SYuan Kang 
209045e3678SYuan Kang /* Map state->caam_ctx, and add it to link table */
210ce572085SHoria Geanta static inline int ctx_map_to_sec4_sg(u32 *desc, struct device *jrdev,
211ce572085SHoria Geanta 				     struct caam_hash_state *state, int ctx_len,
212ce572085SHoria Geanta 				     struct sec4_sg_entry *sec4_sg, u32 flag)
213045e3678SYuan Kang {
214045e3678SYuan Kang 	state->ctx_dma = dma_map_single(jrdev, state->caam_ctx, ctx_len, flag);
215ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, state->ctx_dma)) {
216ce572085SHoria Geanta 		dev_err(jrdev, "unable to map ctx\n");
217ce572085SHoria Geanta 		return -ENOMEM;
218ce572085SHoria Geanta 	}
219ce572085SHoria Geanta 
220045e3678SYuan Kang 	dma_to_sec4_sg_one(sec4_sg, state->ctx_dma, ctx_len, 0);
221ce572085SHoria Geanta 
222ce572085SHoria Geanta 	return 0;
223045e3678SYuan Kang }
224045e3678SYuan Kang 
225045e3678SYuan Kang /* Common shared descriptor commands */
226045e3678SYuan Kang static inline void append_key_ahash(u32 *desc, struct caam_hash_ctx *ctx)
227045e3678SYuan Kang {
228045e3678SYuan Kang 	append_key_as_imm(desc, ctx->key, ctx->split_key_pad_len,
229045e3678SYuan Kang 			  ctx->split_key_len, CLASS_2 |
230045e3678SYuan Kang 			  KEY_DEST_MDHA_SPLIT | KEY_ENC);
231045e3678SYuan Kang }
232045e3678SYuan Kang 
233045e3678SYuan Kang /* Append key if it has been set */
234045e3678SYuan Kang static inline void init_sh_desc_key_ahash(u32 *desc, struct caam_hash_ctx *ctx)
235045e3678SYuan Kang {
236045e3678SYuan Kang 	u32 *key_jump_cmd;
237045e3678SYuan Kang 
23861bb86bbSKim Phillips 	init_sh_desc(desc, HDR_SHARE_SERIAL);
239045e3678SYuan Kang 
240045e3678SYuan Kang 	if (ctx->split_key_len) {
241045e3678SYuan Kang 		/* Skip if already shared */
242045e3678SYuan Kang 		key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
243045e3678SYuan Kang 					   JUMP_COND_SHRD);
244045e3678SYuan Kang 
245045e3678SYuan Kang 		append_key_ahash(desc, ctx);
246045e3678SYuan Kang 
247045e3678SYuan Kang 		set_jump_tgt_here(desc, key_jump_cmd);
248045e3678SYuan Kang 	}
249045e3678SYuan Kang 
250045e3678SYuan Kang 	/* Propagate errors from shared to job descriptor */
251045e3678SYuan Kang 	append_cmd(desc, SET_OK_NO_PROP_ERRORS | CMD_LOAD);
252045e3678SYuan Kang }
253045e3678SYuan Kang 
254045e3678SYuan Kang /*
255045e3678SYuan Kang  * For ahash read data from seqin following state->caam_ctx,
256045e3678SYuan Kang  * and write resulting class2 context to seqout, which may be state->caam_ctx
257045e3678SYuan Kang  * or req->result
258045e3678SYuan Kang  */
259045e3678SYuan Kang static inline void ahash_append_load_str(u32 *desc, int digestsize)
260045e3678SYuan Kang {
261045e3678SYuan Kang 	/* Calculate remaining bytes to read */
262045e3678SYuan Kang 	append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
263045e3678SYuan Kang 
264045e3678SYuan Kang 	/* Read remaining bytes */
265045e3678SYuan Kang 	append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_LAST2 |
266045e3678SYuan Kang 			     FIFOLD_TYPE_MSG | KEY_VLF);
267045e3678SYuan Kang 
268045e3678SYuan Kang 	/* Store class2 context bytes */
269045e3678SYuan Kang 	append_seq_store(desc, digestsize, LDST_CLASS_2_CCB |
270045e3678SYuan Kang 			 LDST_SRCDST_BYTE_CONTEXT);
271045e3678SYuan Kang }
272045e3678SYuan Kang 
273045e3678SYuan Kang /*
274045e3678SYuan Kang  * For ahash update, final and finup, import context, read and write to seqout
275045e3678SYuan Kang  */
276045e3678SYuan Kang static inline void ahash_ctx_data_to_out(u32 *desc, u32 op, u32 state,
277045e3678SYuan Kang 					 int digestsize,
278045e3678SYuan Kang 					 struct caam_hash_ctx *ctx)
279045e3678SYuan Kang {
280045e3678SYuan Kang 	init_sh_desc_key_ahash(desc, ctx);
281045e3678SYuan Kang 
282045e3678SYuan Kang 	/* Import context from software */
283045e3678SYuan Kang 	append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT |
284045e3678SYuan Kang 		   LDST_CLASS_2_CCB | ctx->ctx_len);
285045e3678SYuan Kang 
286045e3678SYuan Kang 	/* Class 2 operation */
287045e3678SYuan Kang 	append_operation(desc, op | state | OP_ALG_ENCRYPT);
288045e3678SYuan Kang 
289045e3678SYuan Kang 	/*
290045e3678SYuan Kang 	 * Load from buf and/or src and write to req->result or state->context
291045e3678SYuan Kang 	 */
292045e3678SYuan Kang 	ahash_append_load_str(desc, digestsize);
293045e3678SYuan Kang }
294045e3678SYuan Kang 
295045e3678SYuan Kang /* For ahash firsts and digest, read and write to seqout */
296045e3678SYuan Kang static inline void ahash_data_to_out(u32 *desc, u32 op, u32 state,
297045e3678SYuan Kang 				     int digestsize, struct caam_hash_ctx *ctx)
298045e3678SYuan Kang {
299045e3678SYuan Kang 	init_sh_desc_key_ahash(desc, ctx);
300045e3678SYuan Kang 
301045e3678SYuan Kang 	/* Class 2 operation */
302045e3678SYuan Kang 	append_operation(desc, op | state | OP_ALG_ENCRYPT);
303045e3678SYuan Kang 
304045e3678SYuan Kang 	/*
305045e3678SYuan Kang 	 * Load from buf and/or src and write to req->result or state->context
306045e3678SYuan Kang 	 */
307045e3678SYuan Kang 	ahash_append_load_str(desc, digestsize);
308045e3678SYuan Kang }
309045e3678SYuan Kang 
310045e3678SYuan Kang static int ahash_set_sh_desc(struct crypto_ahash *ahash)
311045e3678SYuan Kang {
312045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
313045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
314045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
315045e3678SYuan Kang 	u32 have_key = 0;
316045e3678SYuan Kang 	u32 *desc;
317045e3678SYuan Kang 
318045e3678SYuan Kang 	if (ctx->split_key_len)
319045e3678SYuan Kang 		have_key = OP_ALG_AAI_HMAC_PRECOMP;
320045e3678SYuan Kang 
321045e3678SYuan Kang 	/* ahash_update shared descriptor */
322045e3678SYuan Kang 	desc = ctx->sh_desc_update;
323045e3678SYuan Kang 
32461bb86bbSKim Phillips 	init_sh_desc(desc, HDR_SHARE_SERIAL);
325045e3678SYuan Kang 
326045e3678SYuan Kang 	/* Import context from software */
327045e3678SYuan Kang 	append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT |
328045e3678SYuan Kang 		   LDST_CLASS_2_CCB | ctx->ctx_len);
329045e3678SYuan Kang 
330045e3678SYuan Kang 	/* Class 2 operation */
331045e3678SYuan Kang 	append_operation(desc, ctx->alg_type | OP_ALG_AS_UPDATE |
332045e3678SYuan Kang 			 OP_ALG_ENCRYPT);
333045e3678SYuan Kang 
334045e3678SYuan Kang 	/* Load data and write to result or context */
335045e3678SYuan Kang 	ahash_append_load_str(desc, ctx->ctx_len);
336045e3678SYuan Kang 
337045e3678SYuan Kang 	ctx->sh_desc_update_dma = dma_map_single(jrdev, desc, desc_bytes(desc),
338045e3678SYuan Kang 						 DMA_TO_DEVICE);
339045e3678SYuan Kang 	if (dma_mapping_error(jrdev, ctx->sh_desc_update_dma)) {
340045e3678SYuan Kang 		dev_err(jrdev, "unable to map shared descriptor\n");
341045e3678SYuan Kang 		return -ENOMEM;
342045e3678SYuan Kang 	}
343045e3678SYuan Kang #ifdef DEBUG
344514df281SAlex Porosanu 	print_hex_dump(KERN_ERR,
345514df281SAlex Porosanu 		       "ahash update shdesc@"__stringify(__LINE__)": ",
346045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
347045e3678SYuan Kang #endif
348045e3678SYuan Kang 
349045e3678SYuan Kang 	/* ahash_update_first shared descriptor */
350045e3678SYuan Kang 	desc = ctx->sh_desc_update_first;
351045e3678SYuan Kang 
352045e3678SYuan Kang 	ahash_data_to_out(desc, have_key | ctx->alg_type, OP_ALG_AS_INIT,
353045e3678SYuan Kang 			  ctx->ctx_len, ctx);
354045e3678SYuan Kang 
355045e3678SYuan Kang 	ctx->sh_desc_update_first_dma = dma_map_single(jrdev, desc,
356045e3678SYuan Kang 						       desc_bytes(desc),
357045e3678SYuan Kang 						       DMA_TO_DEVICE);
358045e3678SYuan Kang 	if (dma_mapping_error(jrdev, ctx->sh_desc_update_first_dma)) {
359045e3678SYuan Kang 		dev_err(jrdev, "unable to map shared descriptor\n");
360045e3678SYuan Kang 		return -ENOMEM;
361045e3678SYuan Kang 	}
362045e3678SYuan Kang #ifdef DEBUG
363514df281SAlex Porosanu 	print_hex_dump(KERN_ERR,
364514df281SAlex Porosanu 		       "ahash update first shdesc@"__stringify(__LINE__)": ",
365045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
366045e3678SYuan Kang #endif
367045e3678SYuan Kang 
368045e3678SYuan Kang 	/* ahash_final shared descriptor */
369045e3678SYuan Kang 	desc = ctx->sh_desc_fin;
370045e3678SYuan Kang 
371045e3678SYuan Kang 	ahash_ctx_data_to_out(desc, have_key | ctx->alg_type,
372045e3678SYuan Kang 			      OP_ALG_AS_FINALIZE, digestsize, ctx);
373045e3678SYuan Kang 
374045e3678SYuan Kang 	ctx->sh_desc_fin_dma = dma_map_single(jrdev, desc, desc_bytes(desc),
375045e3678SYuan Kang 					      DMA_TO_DEVICE);
376045e3678SYuan Kang 	if (dma_mapping_error(jrdev, ctx->sh_desc_fin_dma)) {
377045e3678SYuan Kang 		dev_err(jrdev, "unable to map shared descriptor\n");
378045e3678SYuan Kang 		return -ENOMEM;
379045e3678SYuan Kang 	}
380045e3678SYuan Kang #ifdef DEBUG
381514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "ahash final shdesc@"__stringify(__LINE__)": ",
382045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, desc,
383045e3678SYuan Kang 		       desc_bytes(desc), 1);
384045e3678SYuan Kang #endif
385045e3678SYuan Kang 
386045e3678SYuan Kang 	/* ahash_finup shared descriptor */
387045e3678SYuan Kang 	desc = ctx->sh_desc_finup;
388045e3678SYuan Kang 
389045e3678SYuan Kang 	ahash_ctx_data_to_out(desc, have_key | ctx->alg_type,
390045e3678SYuan Kang 			      OP_ALG_AS_FINALIZE, digestsize, ctx);
391045e3678SYuan Kang 
392045e3678SYuan Kang 	ctx->sh_desc_finup_dma = dma_map_single(jrdev, desc, desc_bytes(desc),
393045e3678SYuan Kang 						DMA_TO_DEVICE);
394045e3678SYuan Kang 	if (dma_mapping_error(jrdev, ctx->sh_desc_finup_dma)) {
395045e3678SYuan Kang 		dev_err(jrdev, "unable to map shared descriptor\n");
396045e3678SYuan Kang 		return -ENOMEM;
397045e3678SYuan Kang 	}
398045e3678SYuan Kang #ifdef DEBUG
399514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "ahash finup shdesc@"__stringify(__LINE__)": ",
400045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, desc,
401045e3678SYuan Kang 		       desc_bytes(desc), 1);
402045e3678SYuan Kang #endif
403045e3678SYuan Kang 
404045e3678SYuan Kang 	/* ahash_digest shared descriptor */
405045e3678SYuan Kang 	desc = ctx->sh_desc_digest;
406045e3678SYuan Kang 
407045e3678SYuan Kang 	ahash_data_to_out(desc, have_key | ctx->alg_type, OP_ALG_AS_INITFINAL,
408045e3678SYuan Kang 			  digestsize, ctx);
409045e3678SYuan Kang 
410045e3678SYuan Kang 	ctx->sh_desc_digest_dma = dma_map_single(jrdev, desc,
411045e3678SYuan Kang 						 desc_bytes(desc),
412045e3678SYuan Kang 						 DMA_TO_DEVICE);
413045e3678SYuan Kang 	if (dma_mapping_error(jrdev, ctx->sh_desc_digest_dma)) {
414045e3678SYuan Kang 		dev_err(jrdev, "unable to map shared descriptor\n");
415045e3678SYuan Kang 		return -ENOMEM;
416045e3678SYuan Kang 	}
417045e3678SYuan Kang #ifdef DEBUG
418514df281SAlex Porosanu 	print_hex_dump(KERN_ERR,
419514df281SAlex Porosanu 		       "ahash digest shdesc@"__stringify(__LINE__)": ",
420045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, desc,
421045e3678SYuan Kang 		       desc_bytes(desc), 1);
422045e3678SYuan Kang #endif
423045e3678SYuan Kang 
424045e3678SYuan Kang 	return 0;
425045e3678SYuan Kang }
426045e3678SYuan Kang 
42766b3e887SKim Phillips static int gen_split_hash_key(struct caam_hash_ctx *ctx, const u8 *key_in,
428045e3678SYuan Kang 			      u32 keylen)
429045e3678SYuan Kang {
430045e3678SYuan Kang 	return gen_split_key(ctx->jrdev, ctx->key, ctx->split_key_len,
431045e3678SYuan Kang 			       ctx->split_key_pad_len, key_in, keylen,
432045e3678SYuan Kang 			       ctx->alg_op);
433045e3678SYuan Kang }
434045e3678SYuan Kang 
435045e3678SYuan Kang /* Digest hash size if it is too large */
43666b3e887SKim Phillips static int hash_digest_key(struct caam_hash_ctx *ctx, const u8 *key_in,
437045e3678SYuan Kang 			   u32 *keylen, u8 *key_out, u32 digestsize)
438045e3678SYuan Kang {
439045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
440045e3678SYuan Kang 	u32 *desc;
441045e3678SYuan Kang 	struct split_key_result result;
442045e3678SYuan Kang 	dma_addr_t src_dma, dst_dma;
443045e3678SYuan Kang 	int ret = 0;
444045e3678SYuan Kang 
4459c23b7d3SVakul Garg 	desc = kmalloc(CAAM_CMD_SZ * 8 + CAAM_PTR_SZ * 2, GFP_KERNEL | GFP_DMA);
4462af8f4a2SKim Phillips 	if (!desc) {
4472af8f4a2SKim Phillips 		dev_err(jrdev, "unable to allocate key input memory\n");
4482af8f4a2SKim Phillips 		return -ENOMEM;
4492af8f4a2SKim Phillips 	}
450045e3678SYuan Kang 
451045e3678SYuan Kang 	init_job_desc(desc, 0);
452045e3678SYuan Kang 
453045e3678SYuan Kang 	src_dma = dma_map_single(jrdev, (void *)key_in, *keylen,
454045e3678SYuan Kang 				 DMA_TO_DEVICE);
455045e3678SYuan Kang 	if (dma_mapping_error(jrdev, src_dma)) {
456045e3678SYuan Kang 		dev_err(jrdev, "unable to map key input memory\n");
457045e3678SYuan Kang 		kfree(desc);
458045e3678SYuan Kang 		return -ENOMEM;
459045e3678SYuan Kang 	}
460045e3678SYuan Kang 	dst_dma = dma_map_single(jrdev, (void *)key_out, digestsize,
461045e3678SYuan Kang 				 DMA_FROM_DEVICE);
462045e3678SYuan Kang 	if (dma_mapping_error(jrdev, dst_dma)) {
463045e3678SYuan Kang 		dev_err(jrdev, "unable to map key output memory\n");
464045e3678SYuan Kang 		dma_unmap_single(jrdev, src_dma, *keylen, DMA_TO_DEVICE);
465045e3678SYuan Kang 		kfree(desc);
466045e3678SYuan Kang 		return -ENOMEM;
467045e3678SYuan Kang 	}
468045e3678SYuan Kang 
469045e3678SYuan Kang 	/* Job descriptor to perform unkeyed hash on key_in */
470045e3678SYuan Kang 	append_operation(desc, ctx->alg_type | OP_ALG_ENCRYPT |
471045e3678SYuan Kang 			 OP_ALG_AS_INITFINAL);
472045e3678SYuan Kang 	append_seq_in_ptr(desc, src_dma, *keylen, 0);
473045e3678SYuan Kang 	append_seq_fifo_load(desc, *keylen, FIFOLD_CLASS_CLASS2 |
474045e3678SYuan Kang 			     FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_MSG);
475045e3678SYuan Kang 	append_seq_out_ptr(desc, dst_dma, digestsize, 0);
476045e3678SYuan Kang 	append_seq_store(desc, digestsize, LDST_CLASS_2_CCB |
477045e3678SYuan Kang 			 LDST_SRCDST_BYTE_CONTEXT);
478045e3678SYuan Kang 
479045e3678SYuan Kang #ifdef DEBUG
480514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "key_in@"__stringify(__LINE__)": ",
481045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, key_in, *keylen, 1);
482514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
483045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
484045e3678SYuan Kang #endif
485045e3678SYuan Kang 
486045e3678SYuan Kang 	result.err = 0;
487045e3678SYuan Kang 	init_completion(&result.completion);
488045e3678SYuan Kang 
489045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, split_key_done, &result);
490045e3678SYuan Kang 	if (!ret) {
491045e3678SYuan Kang 		/* in progress */
492045e3678SYuan Kang 		wait_for_completion_interruptible(&result.completion);
493045e3678SYuan Kang 		ret = result.err;
494045e3678SYuan Kang #ifdef DEBUG
495514df281SAlex Porosanu 		print_hex_dump(KERN_ERR,
496514df281SAlex Porosanu 			       "digested key@"__stringify(__LINE__)": ",
497045e3678SYuan Kang 			       DUMP_PREFIX_ADDRESS, 16, 4, key_in,
498045e3678SYuan Kang 			       digestsize, 1);
499045e3678SYuan Kang #endif
500045e3678SYuan Kang 	}
501045e3678SYuan Kang 	dma_unmap_single(jrdev, src_dma, *keylen, DMA_TO_DEVICE);
502045e3678SYuan Kang 	dma_unmap_single(jrdev, dst_dma, digestsize, DMA_FROM_DEVICE);
503045e3678SYuan Kang 
504e11aa9f1SHoria Geanta 	*keylen = digestsize;
505e11aa9f1SHoria Geanta 
506045e3678SYuan Kang 	kfree(desc);
507045e3678SYuan Kang 
508045e3678SYuan Kang 	return ret;
509045e3678SYuan Kang }
510045e3678SYuan Kang 
511045e3678SYuan Kang static int ahash_setkey(struct crypto_ahash *ahash,
512045e3678SYuan Kang 			const u8 *key, unsigned int keylen)
513045e3678SYuan Kang {
514045e3678SYuan Kang 	/* Sizes for MDHA pads (*not* keys): MD5, SHA1, 224, 256, 384, 512 */
515045e3678SYuan Kang 	static const u8 mdpadlen[] = { 16, 20, 32, 32, 64, 64 };
516045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
517045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
518045e3678SYuan Kang 	int blocksize = crypto_tfm_alg_blocksize(&ahash->base);
519045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
520045e3678SYuan Kang 	int ret = 0;
521045e3678SYuan Kang 	u8 *hashed_key = NULL;
522045e3678SYuan Kang 
523045e3678SYuan Kang #ifdef DEBUG
524045e3678SYuan Kang 	printk(KERN_ERR "keylen %d\n", keylen);
525045e3678SYuan Kang #endif
526045e3678SYuan Kang 
527045e3678SYuan Kang 	if (keylen > blocksize) {
528e7a33c4dSMarkus Elfring 		hashed_key = kmalloc_array(digestsize,
529e7a33c4dSMarkus Elfring 					   sizeof(*hashed_key),
530e7a33c4dSMarkus Elfring 					   GFP_KERNEL | GFP_DMA);
531045e3678SYuan Kang 		if (!hashed_key)
532045e3678SYuan Kang 			return -ENOMEM;
533045e3678SYuan Kang 		ret = hash_digest_key(ctx, key, &keylen, hashed_key,
534045e3678SYuan Kang 				      digestsize);
535045e3678SYuan Kang 		if (ret)
536*d6e7a7d0SMarkus Elfring 			goto bad_free_key;
537045e3678SYuan Kang 		key = hashed_key;
538045e3678SYuan Kang 	}
539045e3678SYuan Kang 
540045e3678SYuan Kang 	/* Pick class 2 key length from algorithm submask */
541045e3678SYuan Kang 	ctx->split_key_len = mdpadlen[(ctx->alg_op & OP_ALG_ALGSEL_SUBMASK) >>
542045e3678SYuan Kang 				      OP_ALG_ALGSEL_SHIFT] * 2;
543045e3678SYuan Kang 	ctx->split_key_pad_len = ALIGN(ctx->split_key_len, 16);
544045e3678SYuan Kang 
545045e3678SYuan Kang #ifdef DEBUG
546045e3678SYuan Kang 	printk(KERN_ERR "split_key_len %d split_key_pad_len %d\n",
547045e3678SYuan Kang 	       ctx->split_key_len, ctx->split_key_pad_len);
548514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
549045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
550045e3678SYuan Kang #endif
551045e3678SYuan Kang 
552045e3678SYuan Kang 	ret = gen_split_hash_key(ctx, key, keylen);
553045e3678SYuan Kang 	if (ret)
554*d6e7a7d0SMarkus Elfring 		goto bad_free_key;
555045e3678SYuan Kang 
556045e3678SYuan Kang 	ctx->key_dma = dma_map_single(jrdev, ctx->key, ctx->split_key_pad_len,
557045e3678SYuan Kang 				      DMA_TO_DEVICE);
558045e3678SYuan Kang 	if (dma_mapping_error(jrdev, ctx->key_dma)) {
559045e3678SYuan Kang 		dev_err(jrdev, "unable to map key i/o memory\n");
5603d67be27SHoria Geanta 		ret = -ENOMEM;
561*d6e7a7d0SMarkus Elfring 		goto error_free_key;
562045e3678SYuan Kang 	}
563045e3678SYuan Kang #ifdef DEBUG
564514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "ctx.key@"__stringify(__LINE__)": ",
565045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, ctx->key,
566045e3678SYuan Kang 		       ctx->split_key_pad_len, 1);
567045e3678SYuan Kang #endif
568045e3678SYuan Kang 
569045e3678SYuan Kang 	ret = ahash_set_sh_desc(ahash);
570045e3678SYuan Kang 	if (ret) {
571045e3678SYuan Kang 		dma_unmap_single(jrdev, ctx->key_dma, ctx->split_key_pad_len,
572045e3678SYuan Kang 				 DMA_TO_DEVICE);
573045e3678SYuan Kang 	}
574*d6e7a7d0SMarkus Elfring  error_free_key:
575045e3678SYuan Kang 	kfree(hashed_key);
576045e3678SYuan Kang 	return ret;
577*d6e7a7d0SMarkus Elfring  bad_free_key:
578045e3678SYuan Kang 	kfree(hashed_key);
579045e3678SYuan Kang 	crypto_ahash_set_flags(ahash, CRYPTO_TFM_RES_BAD_KEY_LEN);
580045e3678SYuan Kang 	return -EINVAL;
581045e3678SYuan Kang }
582045e3678SYuan Kang 
583045e3678SYuan Kang /*
584045e3678SYuan Kang  * ahash_edesc - s/w-extended ahash descriptor
585045e3678SYuan Kang  * @dst_dma: physical mapped address of req->result
586045e3678SYuan Kang  * @sec4_sg_dma: physical mapped address of h/w link table
587045e3678SYuan Kang  * @src_nents: number of segments in input scatterlist
588045e3678SYuan Kang  * @sec4_sg_bytes: length of dma mapped sec4_sg space
589045e3678SYuan Kang  * @hw_desc: the h/w job descriptor followed by any referenced link tables
590343e44b1SRussell King  * @sec4_sg: h/w link table
591045e3678SYuan Kang  */
592045e3678SYuan Kang struct ahash_edesc {
593045e3678SYuan Kang 	dma_addr_t dst_dma;
594045e3678SYuan Kang 	dma_addr_t sec4_sg_dma;
595045e3678SYuan Kang 	int src_nents;
596045e3678SYuan Kang 	int sec4_sg_bytes;
597d7b24ed4SRussell King 	u32 hw_desc[DESC_JOB_IO_LEN / sizeof(u32)] ____cacheline_aligned;
598343e44b1SRussell King 	struct sec4_sg_entry sec4_sg[0];
599045e3678SYuan Kang };
600045e3678SYuan Kang 
601045e3678SYuan Kang static inline void ahash_unmap(struct device *dev,
602045e3678SYuan Kang 			struct ahash_edesc *edesc,
603045e3678SYuan Kang 			struct ahash_request *req, int dst_len)
604045e3678SYuan Kang {
605045e3678SYuan Kang 	if (edesc->src_nents)
60613fb8fd7SLABBE Corentin 		dma_unmap_sg(dev, req->src, edesc->src_nents, DMA_TO_DEVICE);
607045e3678SYuan Kang 	if (edesc->dst_dma)
608045e3678SYuan Kang 		dma_unmap_single(dev, edesc->dst_dma, dst_len, DMA_FROM_DEVICE);
609045e3678SYuan Kang 
610045e3678SYuan Kang 	if (edesc->sec4_sg_bytes)
611045e3678SYuan Kang 		dma_unmap_single(dev, edesc->sec4_sg_dma,
612045e3678SYuan Kang 				 edesc->sec4_sg_bytes, DMA_TO_DEVICE);
613045e3678SYuan Kang }
614045e3678SYuan Kang 
615045e3678SYuan Kang static inline void ahash_unmap_ctx(struct device *dev,
616045e3678SYuan Kang 			struct ahash_edesc *edesc,
617045e3678SYuan Kang 			struct ahash_request *req, int dst_len, u32 flag)
618045e3678SYuan Kang {
619045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
620045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
621045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
622045e3678SYuan Kang 
623045e3678SYuan Kang 	if (state->ctx_dma)
624045e3678SYuan Kang 		dma_unmap_single(dev, state->ctx_dma, ctx->ctx_len, flag);
625045e3678SYuan Kang 	ahash_unmap(dev, edesc, req, dst_len);
626045e3678SYuan Kang }
627045e3678SYuan Kang 
628045e3678SYuan Kang static void ahash_done(struct device *jrdev, u32 *desc, u32 err,
629045e3678SYuan Kang 		       void *context)
630045e3678SYuan Kang {
631045e3678SYuan Kang 	struct ahash_request *req = context;
632045e3678SYuan Kang 	struct ahash_edesc *edesc;
633045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
634045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
635045e3678SYuan Kang #ifdef DEBUG
636045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
637045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
638045e3678SYuan Kang 
639045e3678SYuan Kang 	dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
640045e3678SYuan Kang #endif
641045e3678SYuan Kang 
642045e3678SYuan Kang 	edesc = (struct ahash_edesc *)((char *)desc -
643045e3678SYuan Kang 		 offsetof(struct ahash_edesc, hw_desc));
644fa9659cdSMarek Vasut 	if (err)
645fa9659cdSMarek Vasut 		caam_jr_strstatus(jrdev, err);
646045e3678SYuan Kang 
647045e3678SYuan Kang 	ahash_unmap(jrdev, edesc, req, digestsize);
648045e3678SYuan Kang 	kfree(edesc);
649045e3678SYuan Kang 
650045e3678SYuan Kang #ifdef DEBUG
651514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ",
652045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
653045e3678SYuan Kang 		       ctx->ctx_len, 1);
654045e3678SYuan Kang 	if (req->result)
655514df281SAlex Porosanu 		print_hex_dump(KERN_ERR, "result@"__stringify(__LINE__)": ",
656045e3678SYuan Kang 			       DUMP_PREFIX_ADDRESS, 16, 4, req->result,
657045e3678SYuan Kang 			       digestsize, 1);
658045e3678SYuan Kang #endif
659045e3678SYuan Kang 
660045e3678SYuan Kang 	req->base.complete(&req->base, err);
661045e3678SYuan Kang }
662045e3678SYuan Kang 
663045e3678SYuan Kang static void ahash_done_bi(struct device *jrdev, u32 *desc, u32 err,
664045e3678SYuan Kang 			    void *context)
665045e3678SYuan Kang {
666045e3678SYuan Kang 	struct ahash_request *req = context;
667045e3678SYuan Kang 	struct ahash_edesc *edesc;
668045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
669045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
670045e3678SYuan Kang #ifdef DEBUG
671045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
672045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
673045e3678SYuan Kang 
674045e3678SYuan Kang 	dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
675045e3678SYuan Kang #endif
676045e3678SYuan Kang 
677045e3678SYuan Kang 	edesc = (struct ahash_edesc *)((char *)desc -
678045e3678SYuan Kang 		 offsetof(struct ahash_edesc, hw_desc));
679fa9659cdSMarek Vasut 	if (err)
680fa9659cdSMarek Vasut 		caam_jr_strstatus(jrdev, err);
681045e3678SYuan Kang 
682045e3678SYuan Kang 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_BIDIRECTIONAL);
683045e3678SYuan Kang 	kfree(edesc);
684045e3678SYuan Kang 
685045e3678SYuan Kang #ifdef DEBUG
686514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ",
687045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
688045e3678SYuan Kang 		       ctx->ctx_len, 1);
689045e3678SYuan Kang 	if (req->result)
690514df281SAlex Porosanu 		print_hex_dump(KERN_ERR, "result@"__stringify(__LINE__)": ",
691045e3678SYuan Kang 			       DUMP_PREFIX_ADDRESS, 16, 4, req->result,
692045e3678SYuan Kang 			       digestsize, 1);
693045e3678SYuan Kang #endif
694045e3678SYuan Kang 
695045e3678SYuan Kang 	req->base.complete(&req->base, err);
696045e3678SYuan Kang }
697045e3678SYuan Kang 
698045e3678SYuan Kang static void ahash_done_ctx_src(struct device *jrdev, u32 *desc, u32 err,
699045e3678SYuan Kang 			       void *context)
700045e3678SYuan Kang {
701045e3678SYuan Kang 	struct ahash_request *req = context;
702045e3678SYuan Kang 	struct ahash_edesc *edesc;
703045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
704045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
705045e3678SYuan Kang #ifdef DEBUG
706045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
707045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
708045e3678SYuan Kang 
709045e3678SYuan Kang 	dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
710045e3678SYuan Kang #endif
711045e3678SYuan Kang 
712045e3678SYuan Kang 	edesc = (struct ahash_edesc *)((char *)desc -
713045e3678SYuan Kang 		 offsetof(struct ahash_edesc, hw_desc));
714fa9659cdSMarek Vasut 	if (err)
715fa9659cdSMarek Vasut 		caam_jr_strstatus(jrdev, err);
716045e3678SYuan Kang 
717bc9e05f9SHoria Geanta 	ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_TO_DEVICE);
718045e3678SYuan Kang 	kfree(edesc);
719045e3678SYuan Kang 
720045e3678SYuan Kang #ifdef DEBUG
721514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ",
722045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
723045e3678SYuan Kang 		       ctx->ctx_len, 1);
724045e3678SYuan Kang 	if (req->result)
725514df281SAlex Porosanu 		print_hex_dump(KERN_ERR, "result@"__stringify(__LINE__)": ",
726045e3678SYuan Kang 			       DUMP_PREFIX_ADDRESS, 16, 4, req->result,
727045e3678SYuan Kang 			       digestsize, 1);
728045e3678SYuan Kang #endif
729045e3678SYuan Kang 
730045e3678SYuan Kang 	req->base.complete(&req->base, err);
731045e3678SYuan Kang }
732045e3678SYuan Kang 
733045e3678SYuan Kang static void ahash_done_ctx_dst(struct device *jrdev, u32 *desc, u32 err,
734045e3678SYuan Kang 			       void *context)
735045e3678SYuan Kang {
736045e3678SYuan Kang 	struct ahash_request *req = context;
737045e3678SYuan Kang 	struct ahash_edesc *edesc;
738045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
739045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
740045e3678SYuan Kang #ifdef DEBUG
741045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
742045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
743045e3678SYuan Kang 
744045e3678SYuan Kang 	dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
745045e3678SYuan Kang #endif
746045e3678SYuan Kang 
747045e3678SYuan Kang 	edesc = (struct ahash_edesc *)((char *)desc -
748045e3678SYuan Kang 		 offsetof(struct ahash_edesc, hw_desc));
749fa9659cdSMarek Vasut 	if (err)
750fa9659cdSMarek Vasut 		caam_jr_strstatus(jrdev, err);
751045e3678SYuan Kang 
752ef62b231SHoria Geanta 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_FROM_DEVICE);
753045e3678SYuan Kang 	kfree(edesc);
754045e3678SYuan Kang 
755045e3678SYuan Kang #ifdef DEBUG
756514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ",
757045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
758045e3678SYuan Kang 		       ctx->ctx_len, 1);
759045e3678SYuan Kang 	if (req->result)
760514df281SAlex Porosanu 		print_hex_dump(KERN_ERR, "result@"__stringify(__LINE__)": ",
761045e3678SYuan Kang 			       DUMP_PREFIX_ADDRESS, 16, 4, req->result,
762045e3678SYuan Kang 			       digestsize, 1);
763045e3678SYuan Kang #endif
764045e3678SYuan Kang 
765045e3678SYuan Kang 	req->base.complete(&req->base, err);
766045e3678SYuan Kang }
767045e3678SYuan Kang 
7685588d039SRussell King /*
7695588d039SRussell King  * Allocate an enhanced descriptor, which contains the hardware descriptor
7705588d039SRussell King  * and space for hardware scatter table containing sg_num entries.
7715588d039SRussell King  */
7725588d039SRussell King static struct ahash_edesc *ahash_edesc_alloc(struct caam_hash_ctx *ctx,
77330a43b44SRussell King 					     int sg_num, u32 *sh_desc,
77430a43b44SRussell King 					     dma_addr_t sh_desc_dma,
77530a43b44SRussell King 					     gfp_t flags)
7765588d039SRussell King {
7775588d039SRussell King 	struct ahash_edesc *edesc;
7785588d039SRussell King 	unsigned int sg_size = sg_num * sizeof(struct sec4_sg_entry);
7795588d039SRussell King 
7805588d039SRussell King 	edesc = kzalloc(sizeof(*edesc) + sg_size, GFP_DMA | flags);
7815588d039SRussell King 	if (!edesc) {
7825588d039SRussell King 		dev_err(ctx->jrdev, "could not allocate extended descriptor\n");
7835588d039SRussell King 		return NULL;
7845588d039SRussell King 	}
7855588d039SRussell King 
78630a43b44SRussell King 	init_job_desc_shared(edesc->hw_desc, sh_desc_dma, desc_len(sh_desc),
78730a43b44SRussell King 			     HDR_SHARE_DEFER | HDR_REVERSE);
78830a43b44SRussell King 
7895588d039SRussell King 	return edesc;
7905588d039SRussell King }
7915588d039SRussell King 
79265cf164aSRussell King static int ahash_edesc_add_src(struct caam_hash_ctx *ctx,
79365cf164aSRussell King 			       struct ahash_edesc *edesc,
79465cf164aSRussell King 			       struct ahash_request *req, int nents,
79565cf164aSRussell King 			       unsigned int first_sg,
79665cf164aSRussell King 			       unsigned int first_bytes, size_t to_hash)
79765cf164aSRussell King {
79865cf164aSRussell King 	dma_addr_t src_dma;
79965cf164aSRussell King 	u32 options;
80065cf164aSRussell King 
80165cf164aSRussell King 	if (nents > 1 || first_sg) {
80265cf164aSRussell King 		struct sec4_sg_entry *sg = edesc->sec4_sg;
80365cf164aSRussell King 		unsigned int sgsize = sizeof(*sg) * (first_sg + nents);
80465cf164aSRussell King 
80565cf164aSRussell King 		sg_to_sec4_sg_last(req->src, nents, sg + first_sg, 0);
80665cf164aSRussell King 
80765cf164aSRussell King 		src_dma = dma_map_single(ctx->jrdev, sg, sgsize, DMA_TO_DEVICE);
80865cf164aSRussell King 		if (dma_mapping_error(ctx->jrdev, src_dma)) {
80965cf164aSRussell King 			dev_err(ctx->jrdev, "unable to map S/G table\n");
81065cf164aSRussell King 			return -ENOMEM;
81165cf164aSRussell King 		}
81265cf164aSRussell King 
81365cf164aSRussell King 		edesc->sec4_sg_bytes = sgsize;
81465cf164aSRussell King 		edesc->sec4_sg_dma = src_dma;
81565cf164aSRussell King 		options = LDST_SGF;
81665cf164aSRussell King 	} else {
81765cf164aSRussell King 		src_dma = sg_dma_address(req->src);
81865cf164aSRussell King 		options = 0;
81965cf164aSRussell King 	}
82065cf164aSRussell King 
82165cf164aSRussell King 	append_seq_in_ptr(edesc->hw_desc, src_dma, first_bytes + to_hash,
82265cf164aSRussell King 			  options);
82365cf164aSRussell King 
82465cf164aSRussell King 	return 0;
82565cf164aSRussell King }
82665cf164aSRussell King 
827045e3678SYuan Kang /* submit update job descriptor */
828045e3678SYuan Kang static int ahash_update_ctx(struct ahash_request *req)
829045e3678SYuan Kang {
830045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
831045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
832045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
833045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
834045e3678SYuan Kang 	gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
835045e3678SYuan Kang 		       CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
836045e3678SYuan Kang 	u8 *buf = state->current_buf ? state->buf_1 : state->buf_0;
837045e3678SYuan Kang 	int *buflen = state->current_buf ? &state->buflen_1 : &state->buflen_0;
838045e3678SYuan Kang 	u8 *next_buf = state->current_buf ? state->buf_0 : state->buf_1;
839045e3678SYuan Kang 	int *next_buflen = state->current_buf ? &state->buflen_0 :
840045e3678SYuan Kang 			   &state->buflen_1, last_buflen;
841045e3678SYuan Kang 	int in_len = *buflen + req->nbytes, to_hash;
84230a43b44SRussell King 	u32 *desc;
843bc13c69eSRussell King 	int src_nents, mapped_nents, sec4_sg_bytes, sec4_sg_src_index;
844045e3678SYuan Kang 	struct ahash_edesc *edesc;
845045e3678SYuan Kang 	int ret = 0;
846045e3678SYuan Kang 
847045e3678SYuan Kang 	last_buflen = *next_buflen;
848045e3678SYuan Kang 	*next_buflen = in_len & (crypto_tfm_alg_blocksize(&ahash->base) - 1);
849045e3678SYuan Kang 	to_hash = in_len - *next_buflen;
850045e3678SYuan Kang 
851045e3678SYuan Kang 	if (to_hash) {
85213fb8fd7SLABBE Corentin 		src_nents = sg_nents_for_len(req->src,
85313fb8fd7SLABBE Corentin 					     req->nbytes - (*next_buflen));
854f9970c28SLABBE Corentin 		if (src_nents < 0) {
855f9970c28SLABBE Corentin 			dev_err(jrdev, "Invalid number of src SG.\n");
856f9970c28SLABBE Corentin 			return src_nents;
857f9970c28SLABBE Corentin 		}
858bc13c69eSRussell King 
859bc13c69eSRussell King 		if (src_nents) {
860bc13c69eSRussell King 			mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
861bc13c69eSRussell King 						  DMA_TO_DEVICE);
862bc13c69eSRussell King 			if (!mapped_nents) {
863bc13c69eSRussell King 				dev_err(jrdev, "unable to DMA map source\n");
864bc13c69eSRussell King 				return -ENOMEM;
865bc13c69eSRussell King 			}
866bc13c69eSRussell King 		} else {
867bc13c69eSRussell King 			mapped_nents = 0;
868bc13c69eSRussell King 		}
869bc13c69eSRussell King 
870045e3678SYuan Kang 		sec4_sg_src_index = 1 + (*buflen ? 1 : 0);
871bc13c69eSRussell King 		sec4_sg_bytes = (sec4_sg_src_index + mapped_nents) *
872045e3678SYuan Kang 				 sizeof(struct sec4_sg_entry);
873045e3678SYuan Kang 
874045e3678SYuan Kang 		/*
875045e3678SYuan Kang 		 * allocate space for base edesc and hw desc commands,
876045e3678SYuan Kang 		 * link tables
877045e3678SYuan Kang 		 */
8785588d039SRussell King 		edesc = ahash_edesc_alloc(ctx, sec4_sg_src_index + mapped_nents,
87930a43b44SRussell King 					  ctx->sh_desc_update,
88030a43b44SRussell King 					  ctx->sh_desc_update_dma, flags);
881045e3678SYuan Kang 		if (!edesc) {
882bc13c69eSRussell King 			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
883045e3678SYuan Kang 			return -ENOMEM;
884045e3678SYuan Kang 		}
885045e3678SYuan Kang 
886045e3678SYuan Kang 		edesc->src_nents = src_nents;
887045e3678SYuan Kang 		edesc->sec4_sg_bytes = sec4_sg_bytes;
888045e3678SYuan Kang 
889ce572085SHoria Geanta 		ret = ctx_map_to_sec4_sg(desc, jrdev, state, ctx->ctx_len,
890045e3678SYuan Kang 					 edesc->sec4_sg, DMA_BIDIRECTIONAL);
891ce572085SHoria Geanta 		if (ret)
89232686d34SRussell King 			goto err;
893045e3678SYuan Kang 
894045e3678SYuan Kang 		state->buf_dma = try_buf_map_to_sec4_sg(jrdev,
895045e3678SYuan Kang 							edesc->sec4_sg + 1,
896045e3678SYuan Kang 							buf, state->buf_dma,
897c7556ff7SRussell King 							*buflen, last_buflen);
898045e3678SYuan Kang 
899bc13c69eSRussell King 		if (mapped_nents) {
900bc13c69eSRussell King 			sg_to_sec4_sg_last(req->src, mapped_nents,
901bc13c69eSRussell King 					   edesc->sec4_sg + sec4_sg_src_index,
902bc13c69eSRussell King 					   0);
9038af7b0f8SVictoria Milhoan 			if (*next_buflen)
904307fd543SCristian Stoica 				scatterwalk_map_and_copy(next_buf, req->src,
905307fd543SCristian Stoica 							 to_hash - *buflen,
906307fd543SCristian Stoica 							 *next_buflen, 0);
907045e3678SYuan Kang 		} else {
908045e3678SYuan Kang 			(edesc->sec4_sg + sec4_sg_src_index - 1)->len |=
909261ea058SHoria Geantă 				cpu_to_caam32(SEC4_SG_LEN_FIN);
910045e3678SYuan Kang 		}
911045e3678SYuan Kang 
9128af7b0f8SVictoria Milhoan 		state->current_buf = !state->current_buf;
9138af7b0f8SVictoria Milhoan 
914045e3678SYuan Kang 		desc = edesc->hw_desc;
915045e3678SYuan Kang 
9161da2be33SRuchika Gupta 		edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
9171da2be33SRuchika Gupta 						     sec4_sg_bytes,
9181da2be33SRuchika Gupta 						     DMA_TO_DEVICE);
919ce572085SHoria Geanta 		if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
920ce572085SHoria Geanta 			dev_err(jrdev, "unable to map S/G table\n");
92132686d34SRussell King 			ret = -ENOMEM;
92232686d34SRussell King 			goto err;
923ce572085SHoria Geanta 		}
9241da2be33SRuchika Gupta 
925045e3678SYuan Kang 		append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len +
926045e3678SYuan Kang 				       to_hash, LDST_SGF);
927045e3678SYuan Kang 
928045e3678SYuan Kang 		append_seq_out_ptr(desc, state->ctx_dma, ctx->ctx_len, 0);
929045e3678SYuan Kang 
930045e3678SYuan Kang #ifdef DEBUG
931514df281SAlex Porosanu 		print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
932045e3678SYuan Kang 			       DUMP_PREFIX_ADDRESS, 16, 4, desc,
933045e3678SYuan Kang 			       desc_bytes(desc), 1);
934045e3678SYuan Kang #endif
935045e3678SYuan Kang 
936045e3678SYuan Kang 		ret = caam_jr_enqueue(jrdev, desc, ahash_done_bi, req);
93732686d34SRussell King 		if (ret)
93832686d34SRussell King 			goto err;
93932686d34SRussell King 
940045e3678SYuan Kang 		ret = -EINPROGRESS;
941045e3678SYuan Kang 	} else if (*next_buflen) {
942307fd543SCristian Stoica 		scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
943307fd543SCristian Stoica 					 req->nbytes, 0);
944045e3678SYuan Kang 		*buflen = *next_buflen;
945045e3678SYuan Kang 		*next_buflen = last_buflen;
946045e3678SYuan Kang 	}
947045e3678SYuan Kang #ifdef DEBUG
948514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "buf@"__stringify(__LINE__)": ",
949045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, buf, *buflen, 1);
950514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "next buf@"__stringify(__LINE__)": ",
951045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, next_buf,
952045e3678SYuan Kang 		       *next_buflen, 1);
953045e3678SYuan Kang #endif
954045e3678SYuan Kang 
955045e3678SYuan Kang 	return ret;
95632686d34SRussell King 
95732686d34SRussell King  err:
95832686d34SRussell King 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_BIDIRECTIONAL);
95932686d34SRussell King 	kfree(edesc);
96032686d34SRussell King 	return ret;
961045e3678SYuan Kang }
962045e3678SYuan Kang 
963045e3678SYuan Kang static int ahash_final_ctx(struct ahash_request *req)
964045e3678SYuan Kang {
965045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
966045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
967045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
968045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
969045e3678SYuan Kang 	gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
970045e3678SYuan Kang 		       CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
971045e3678SYuan Kang 	u8 *buf = state->current_buf ? state->buf_1 : state->buf_0;
972045e3678SYuan Kang 	int buflen = state->current_buf ? state->buflen_1 : state->buflen_0;
973045e3678SYuan Kang 	int last_buflen = state->current_buf ? state->buflen_0 :
974045e3678SYuan Kang 			  state->buflen_1;
97530a43b44SRussell King 	u32 *desc;
976b310c178SHoria Geant? 	int sec4_sg_bytes, sec4_sg_src_index;
977045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
978045e3678SYuan Kang 	struct ahash_edesc *edesc;
979045e3678SYuan Kang 	int ret = 0;
980045e3678SYuan Kang 
981b310c178SHoria Geant? 	sec4_sg_src_index = 1 + (buflen ? 1 : 0);
982b310c178SHoria Geant? 	sec4_sg_bytes = sec4_sg_src_index * sizeof(struct sec4_sg_entry);
983045e3678SYuan Kang 
984045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
98530a43b44SRussell King 	edesc = ahash_edesc_alloc(ctx, sec4_sg_src_index,
98630a43b44SRussell King 				  ctx->sh_desc_fin, ctx->sh_desc_fin_dma,
98730a43b44SRussell King 				  flags);
9885588d039SRussell King 	if (!edesc)
989045e3678SYuan Kang 		return -ENOMEM;
990045e3678SYuan Kang 
991045e3678SYuan Kang 	desc = edesc->hw_desc;
992045e3678SYuan Kang 
993045e3678SYuan Kang 	edesc->sec4_sg_bytes = sec4_sg_bytes;
994045e3678SYuan Kang 	edesc->src_nents = 0;
995045e3678SYuan Kang 
996ce572085SHoria Geanta 	ret = ctx_map_to_sec4_sg(desc, jrdev, state, ctx->ctx_len,
997ce572085SHoria Geanta 				 edesc->sec4_sg, DMA_TO_DEVICE);
998ce572085SHoria Geanta 	if (ret)
99932686d34SRussell King 		goto err;
1000045e3678SYuan Kang 
1001045e3678SYuan Kang 	state->buf_dma = try_buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1,
1002045e3678SYuan Kang 						buf, state->buf_dma, buflen,
1003045e3678SYuan Kang 						last_buflen);
1004261ea058SHoria Geantă 	(edesc->sec4_sg + sec4_sg_src_index - 1)->len |=
1005261ea058SHoria Geantă 		cpu_to_caam32(SEC4_SG_LEN_FIN);
1006045e3678SYuan Kang 
10071da2be33SRuchika Gupta 	edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
10081da2be33SRuchika Gupta 					    sec4_sg_bytes, DMA_TO_DEVICE);
1009ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
1010ce572085SHoria Geanta 		dev_err(jrdev, "unable to map S/G table\n");
101132686d34SRussell King 		ret = -ENOMEM;
101232686d34SRussell King 		goto err;
1013ce572085SHoria Geanta 	}
10141da2be33SRuchika Gupta 
1015045e3678SYuan Kang 	append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len + buflen,
1016045e3678SYuan Kang 			  LDST_SGF);
1017045e3678SYuan Kang 
1018045e3678SYuan Kang 	edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result,
1019045e3678SYuan Kang 						digestsize);
1020ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, edesc->dst_dma)) {
1021ce572085SHoria Geanta 		dev_err(jrdev, "unable to map dst\n");
102232686d34SRussell King 		ret = -ENOMEM;
102332686d34SRussell King 		goto err;
1024ce572085SHoria Geanta 	}
1025045e3678SYuan Kang 
1026045e3678SYuan Kang #ifdef DEBUG
1027514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
1028045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
1029045e3678SYuan Kang #endif
1030045e3678SYuan Kang 
1031045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_src, req);
103232686d34SRussell King 	if (ret)
103332686d34SRussell King 		goto err;
103432686d34SRussell King 
103532686d34SRussell King 	return -EINPROGRESS;
103632686d34SRussell King 
103732686d34SRussell King err:
1038045e3678SYuan Kang 	ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
1039045e3678SYuan Kang 	kfree(edesc);
1040045e3678SYuan Kang 	return ret;
1041045e3678SYuan Kang }
1042045e3678SYuan Kang 
1043045e3678SYuan Kang static int ahash_finup_ctx(struct ahash_request *req)
1044045e3678SYuan Kang {
1045045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1046045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1047045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1048045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
1049045e3678SYuan Kang 	gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
1050045e3678SYuan Kang 		       CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
1051045e3678SYuan Kang 	u8 *buf = state->current_buf ? state->buf_1 : state->buf_0;
1052045e3678SYuan Kang 	int buflen = state->current_buf ? state->buflen_1 : state->buflen_0;
1053045e3678SYuan Kang 	int last_buflen = state->current_buf ? state->buflen_0 :
1054045e3678SYuan Kang 			  state->buflen_1;
105530a43b44SRussell King 	u32 *desc;
105665cf164aSRussell King 	int sec4_sg_src_index;
1057bc13c69eSRussell King 	int src_nents, mapped_nents;
1058045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
1059045e3678SYuan Kang 	struct ahash_edesc *edesc;
1060045e3678SYuan Kang 	int ret = 0;
1061045e3678SYuan Kang 
106213fb8fd7SLABBE Corentin 	src_nents = sg_nents_for_len(req->src, req->nbytes);
1063f9970c28SLABBE Corentin 	if (src_nents < 0) {
1064f9970c28SLABBE Corentin 		dev_err(jrdev, "Invalid number of src SG.\n");
1065f9970c28SLABBE Corentin 		return src_nents;
1066f9970c28SLABBE Corentin 	}
1067bc13c69eSRussell King 
1068bc13c69eSRussell King 	if (src_nents) {
1069bc13c69eSRussell King 		mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1070bc13c69eSRussell King 					  DMA_TO_DEVICE);
1071bc13c69eSRussell King 		if (!mapped_nents) {
1072bc13c69eSRussell King 			dev_err(jrdev, "unable to DMA map source\n");
1073bc13c69eSRussell King 			return -ENOMEM;
1074bc13c69eSRussell King 		}
1075bc13c69eSRussell King 	} else {
1076bc13c69eSRussell King 		mapped_nents = 0;
1077bc13c69eSRussell King 	}
1078bc13c69eSRussell King 
1079045e3678SYuan Kang 	sec4_sg_src_index = 1 + (buflen ? 1 : 0);
1080045e3678SYuan Kang 
1081045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
10825588d039SRussell King 	edesc = ahash_edesc_alloc(ctx, sec4_sg_src_index + mapped_nents,
108330a43b44SRussell King 				  ctx->sh_desc_finup, ctx->sh_desc_finup_dma,
10845588d039SRussell King 				  flags);
1085045e3678SYuan Kang 	if (!edesc) {
1086bc13c69eSRussell King 		dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1087045e3678SYuan Kang 		return -ENOMEM;
1088045e3678SYuan Kang 	}
1089045e3678SYuan Kang 
1090045e3678SYuan Kang 	desc = edesc->hw_desc;
1091045e3678SYuan Kang 
1092045e3678SYuan Kang 	edesc->src_nents = src_nents;
1093045e3678SYuan Kang 
1094ce572085SHoria Geanta 	ret = ctx_map_to_sec4_sg(desc, jrdev, state, ctx->ctx_len,
1095ce572085SHoria Geanta 				 edesc->sec4_sg, DMA_TO_DEVICE);
1096ce572085SHoria Geanta 	if (ret)
109732686d34SRussell King 		goto err;
1098045e3678SYuan Kang 
1099045e3678SYuan Kang 	state->buf_dma = try_buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1,
1100045e3678SYuan Kang 						buf, state->buf_dma, buflen,
1101045e3678SYuan Kang 						last_buflen);
1102045e3678SYuan Kang 
110365cf164aSRussell King 	ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents,
110465cf164aSRussell King 				  sec4_sg_src_index, ctx->ctx_len + buflen,
110565cf164aSRussell King 				  req->nbytes);
110665cf164aSRussell King 	if (ret)
110732686d34SRussell King 		goto err;
1108045e3678SYuan Kang 
1109045e3678SYuan Kang 	edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result,
1110045e3678SYuan Kang 						digestsize);
1111ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, edesc->dst_dma)) {
1112ce572085SHoria Geanta 		dev_err(jrdev, "unable to map dst\n");
111332686d34SRussell King 		ret = -ENOMEM;
111432686d34SRussell King 		goto err;
1115ce572085SHoria Geanta 	}
1116045e3678SYuan Kang 
1117045e3678SYuan Kang #ifdef DEBUG
1118514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
1119045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
1120045e3678SYuan Kang #endif
1121045e3678SYuan Kang 
1122045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_src, req);
112332686d34SRussell King 	if (ret)
112432686d34SRussell King 		goto err;
112532686d34SRussell King 
112632686d34SRussell King 	return -EINPROGRESS;
112732686d34SRussell King 
112832686d34SRussell King err:
1129045e3678SYuan Kang 	ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
1130045e3678SYuan Kang 	kfree(edesc);
1131045e3678SYuan Kang 	return ret;
1132045e3678SYuan Kang }
1133045e3678SYuan Kang 
1134045e3678SYuan Kang static int ahash_digest(struct ahash_request *req)
1135045e3678SYuan Kang {
1136045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1137045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1138045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
1139045e3678SYuan Kang 	gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
1140045e3678SYuan Kang 		       CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
114130a43b44SRussell King 	u32 *desc;
1142045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
114365cf164aSRussell King 	int src_nents, mapped_nents;
1144045e3678SYuan Kang 	struct ahash_edesc *edesc;
1145045e3678SYuan Kang 	int ret = 0;
1146045e3678SYuan Kang 
11473d5a2db6SRussell King 	src_nents = sg_nents_for_len(req->src, req->nbytes);
1148f9970c28SLABBE Corentin 	if (src_nents < 0) {
1149f9970c28SLABBE Corentin 		dev_err(jrdev, "Invalid number of src SG.\n");
1150f9970c28SLABBE Corentin 		return src_nents;
1151f9970c28SLABBE Corentin 	}
1152bc13c69eSRussell King 
1153bc13c69eSRussell King 	if (src_nents) {
1154bc13c69eSRussell King 		mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1155bc13c69eSRussell King 					  DMA_TO_DEVICE);
1156bc13c69eSRussell King 		if (!mapped_nents) {
1157bc13c69eSRussell King 			dev_err(jrdev, "unable to map source for DMA\n");
1158bc13c69eSRussell King 			return -ENOMEM;
1159bc13c69eSRussell King 		}
1160bc13c69eSRussell King 	} else {
1161bc13c69eSRussell King 		mapped_nents = 0;
1162bc13c69eSRussell King 	}
1163bc13c69eSRussell King 
1164045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
11655588d039SRussell King 	edesc = ahash_edesc_alloc(ctx, mapped_nents > 1 ? mapped_nents : 0,
116630a43b44SRussell King 				  ctx->sh_desc_digest, ctx->sh_desc_digest_dma,
11675588d039SRussell King 				  flags);
1168045e3678SYuan Kang 	if (!edesc) {
1169bc13c69eSRussell King 		dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1170045e3678SYuan Kang 		return -ENOMEM;
1171045e3678SYuan Kang 	}
1172343e44b1SRussell King 
1173045e3678SYuan Kang 	edesc->src_nents = src_nents;
1174045e3678SYuan Kang 
117565cf164aSRussell King 	ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 0, 0,
117665cf164aSRussell King 				  req->nbytes);
117765cf164aSRussell King 	if (ret) {
117832686d34SRussell King 		ahash_unmap(jrdev, edesc, req, digestsize);
117932686d34SRussell King 		kfree(edesc);
118065cf164aSRussell King 		return ret;
1181ce572085SHoria Geanta 	}
118265cf164aSRussell King 
118365cf164aSRussell King 	desc = edesc->hw_desc;
1184045e3678SYuan Kang 
1185045e3678SYuan Kang 	edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result,
1186045e3678SYuan Kang 						digestsize);
1187ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, edesc->dst_dma)) {
1188ce572085SHoria Geanta 		dev_err(jrdev, "unable to map dst\n");
118932686d34SRussell King 		ahash_unmap(jrdev, edesc, req, digestsize);
119032686d34SRussell King 		kfree(edesc);
1191ce572085SHoria Geanta 		return -ENOMEM;
1192ce572085SHoria Geanta 	}
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 {
1203045e3678SYuan Kang 		ahash_unmap(jrdev, edesc, req, digestsize);
1204045e3678SYuan Kang 		kfree(edesc);
1205045e3678SYuan Kang 	}
1206045e3678SYuan Kang 
1207045e3678SYuan Kang 	return ret;
1208045e3678SYuan Kang }
1209045e3678SYuan Kang 
1210045e3678SYuan Kang /* submit ahash final if it the first job descriptor */
1211045e3678SYuan Kang static int ahash_final_no_ctx(struct ahash_request *req)
1212045e3678SYuan Kang {
1213045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1214045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1215045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1216045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
1217045e3678SYuan Kang 	gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
1218045e3678SYuan Kang 		       CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
1219045e3678SYuan Kang 	u8 *buf = state->current_buf ? state->buf_1 : state->buf_0;
1220045e3678SYuan Kang 	int buflen = state->current_buf ? state->buflen_1 : state->buflen_0;
122130a43b44SRussell King 	u32 *desc;
1222045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
1223045e3678SYuan Kang 	struct ahash_edesc *edesc;
1224045e3678SYuan Kang 	int ret = 0;
1225045e3678SYuan Kang 
1226045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
122730a43b44SRussell King 	edesc = ahash_edesc_alloc(ctx, 0, ctx->sh_desc_digest,
122830a43b44SRussell King 				  ctx->sh_desc_digest_dma, flags);
12295588d039SRussell King 	if (!edesc)
1230045e3678SYuan Kang 		return -ENOMEM;
1231045e3678SYuan Kang 
1232045e3678SYuan Kang 	desc = edesc->hw_desc;
1233045e3678SYuan Kang 
1234045e3678SYuan Kang 	state->buf_dma = dma_map_single(jrdev, buf, buflen, DMA_TO_DEVICE);
1235ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, state->buf_dma)) {
1236ce572085SHoria Geanta 		dev_err(jrdev, "unable to map src\n");
123732686d34SRussell King 		ahash_unmap(jrdev, edesc, req, digestsize);
123832686d34SRussell King 		kfree(edesc);
1239ce572085SHoria Geanta 		return -ENOMEM;
1240ce572085SHoria Geanta 	}
1241045e3678SYuan Kang 
1242045e3678SYuan Kang 	append_seq_in_ptr(desc, state->buf_dma, buflen, 0);
1243045e3678SYuan Kang 
1244045e3678SYuan Kang 	edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result,
1245045e3678SYuan Kang 						digestsize);
1246ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, edesc->dst_dma)) {
1247ce572085SHoria Geanta 		dev_err(jrdev, "unable to map dst\n");
124832686d34SRussell King 		ahash_unmap(jrdev, edesc, req, digestsize);
124932686d34SRussell King 		kfree(edesc);
1250ce572085SHoria Geanta 		return -ENOMEM;
1251ce572085SHoria Geanta 	}
1252045e3678SYuan Kang 	edesc->src_nents = 0;
1253045e3678SYuan Kang 
1254045e3678SYuan Kang #ifdef DEBUG
1255514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
1256045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
1257045e3678SYuan Kang #endif
1258045e3678SYuan Kang 
1259045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, ahash_done, req);
1260045e3678SYuan Kang 	if (!ret) {
1261045e3678SYuan Kang 		ret = -EINPROGRESS;
1262045e3678SYuan Kang 	} else {
1263045e3678SYuan Kang 		ahash_unmap(jrdev, edesc, req, digestsize);
1264045e3678SYuan Kang 		kfree(edesc);
1265045e3678SYuan Kang 	}
1266045e3678SYuan Kang 
1267045e3678SYuan Kang 	return ret;
1268045e3678SYuan Kang }
1269045e3678SYuan Kang 
1270045e3678SYuan Kang /* submit ahash update if it the first job descriptor after update */
1271045e3678SYuan Kang static int ahash_update_no_ctx(struct ahash_request *req)
1272045e3678SYuan Kang {
1273045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1274045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1275045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1276045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
1277045e3678SYuan Kang 	gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
1278045e3678SYuan Kang 		       CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
1279045e3678SYuan Kang 	u8 *buf = state->current_buf ? state->buf_1 : state->buf_0;
1280045e3678SYuan Kang 	int *buflen = state->current_buf ? &state->buflen_1 : &state->buflen_0;
1281045e3678SYuan Kang 	u8 *next_buf = state->current_buf ? state->buf_0 : state->buf_1;
1282045e3678SYuan Kang 	int *next_buflen = state->current_buf ? &state->buflen_0 :
1283045e3678SYuan Kang 			   &state->buflen_1;
1284045e3678SYuan Kang 	int in_len = *buflen + req->nbytes, to_hash;
1285bc13c69eSRussell King 	int sec4_sg_bytes, src_nents, mapped_nents;
1286045e3678SYuan Kang 	struct ahash_edesc *edesc;
128730a43b44SRussell King 	u32 *desc;
1288045e3678SYuan Kang 	int ret = 0;
1289045e3678SYuan Kang 
1290045e3678SYuan Kang 	*next_buflen = in_len & (crypto_tfm_alg_blocksize(&ahash->base) - 1);
1291045e3678SYuan Kang 	to_hash = in_len - *next_buflen;
1292045e3678SYuan Kang 
1293045e3678SYuan Kang 	if (to_hash) {
129413fb8fd7SLABBE Corentin 		src_nents = sg_nents_for_len(req->src,
12953d5a2db6SRussell King 					     req->nbytes - *next_buflen);
1296f9970c28SLABBE Corentin 		if (src_nents < 0) {
1297f9970c28SLABBE Corentin 			dev_err(jrdev, "Invalid number of src SG.\n");
1298f9970c28SLABBE Corentin 			return src_nents;
1299f9970c28SLABBE Corentin 		}
1300bc13c69eSRussell King 
1301bc13c69eSRussell King 		if (src_nents) {
1302bc13c69eSRussell King 			mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1303bc13c69eSRussell King 						  DMA_TO_DEVICE);
1304bc13c69eSRussell King 			if (!mapped_nents) {
1305bc13c69eSRussell King 				dev_err(jrdev, "unable to DMA map source\n");
1306bc13c69eSRussell King 				return -ENOMEM;
1307bc13c69eSRussell King 			}
1308bc13c69eSRussell King 		} else {
1309bc13c69eSRussell King 			mapped_nents = 0;
1310bc13c69eSRussell King 		}
1311bc13c69eSRussell King 
1312bc13c69eSRussell King 		sec4_sg_bytes = (1 + mapped_nents) *
1313045e3678SYuan Kang 				sizeof(struct sec4_sg_entry);
1314045e3678SYuan Kang 
1315045e3678SYuan Kang 		/*
1316045e3678SYuan Kang 		 * allocate space for base edesc and hw desc commands,
1317045e3678SYuan Kang 		 * link tables
1318045e3678SYuan Kang 		 */
131930a43b44SRussell King 		edesc = ahash_edesc_alloc(ctx, 1 + mapped_nents,
132030a43b44SRussell King 					  ctx->sh_desc_update_first,
132130a43b44SRussell King 					  ctx->sh_desc_update_first_dma,
132230a43b44SRussell King 					  flags);
1323045e3678SYuan Kang 		if (!edesc) {
1324bc13c69eSRussell King 			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1325045e3678SYuan Kang 			return -ENOMEM;
1326045e3678SYuan Kang 		}
1327045e3678SYuan Kang 
1328045e3678SYuan Kang 		edesc->src_nents = src_nents;
1329045e3678SYuan Kang 		edesc->sec4_sg_bytes = sec4_sg_bytes;
133076b99080SHoria Geanta 		edesc->dst_dma = 0;
1331045e3678SYuan Kang 
1332045e3678SYuan Kang 		state->buf_dma = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg,
1333045e3678SYuan Kang 						    buf, *buflen);
1334bc13c69eSRussell King 		sg_to_sec4_sg_last(req->src, mapped_nents,
1335bc13c69eSRussell King 				   edesc->sec4_sg + 1, 0);
1336bc13c69eSRussell King 
1337045e3678SYuan Kang 		if (*next_buflen) {
1338307fd543SCristian Stoica 			scatterwalk_map_and_copy(next_buf, req->src,
1339307fd543SCristian Stoica 						 to_hash - *buflen,
1340307fd543SCristian Stoica 						 *next_buflen, 0);
1341045e3678SYuan Kang 		}
1342045e3678SYuan Kang 
13438af7b0f8SVictoria Milhoan 		state->current_buf = !state->current_buf;
13448af7b0f8SVictoria Milhoan 
1345045e3678SYuan Kang 		desc = edesc->hw_desc;
1346045e3678SYuan Kang 
13471da2be33SRuchika Gupta 		edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
13481da2be33SRuchika Gupta 						    sec4_sg_bytes,
13491da2be33SRuchika Gupta 						    DMA_TO_DEVICE);
1350ce572085SHoria Geanta 		if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
1351ce572085SHoria Geanta 			dev_err(jrdev, "unable to map S/G table\n");
135232686d34SRussell King 			ret = -ENOMEM;
135332686d34SRussell King 			goto err;
1354ce572085SHoria Geanta 		}
13551da2be33SRuchika Gupta 
1356045e3678SYuan Kang 		append_seq_in_ptr(desc, edesc->sec4_sg_dma, to_hash, LDST_SGF);
1357045e3678SYuan Kang 
1358ce572085SHoria Geanta 		ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len);
1359ce572085SHoria Geanta 		if (ret)
136032686d34SRussell King 			goto err;
1361045e3678SYuan Kang 
1362045e3678SYuan Kang #ifdef DEBUG
1363514df281SAlex Porosanu 		print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
1364045e3678SYuan Kang 			       DUMP_PREFIX_ADDRESS, 16, 4, desc,
1365045e3678SYuan Kang 			       desc_bytes(desc), 1);
1366045e3678SYuan Kang #endif
1367045e3678SYuan Kang 
1368045e3678SYuan Kang 		ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_dst, req);
136932686d34SRussell King 		if (ret)
137032686d34SRussell King 			goto err;
137132686d34SRussell King 
1372045e3678SYuan Kang 		ret = -EINPROGRESS;
1373045e3678SYuan Kang 		state->update = ahash_update_ctx;
1374045e3678SYuan Kang 		state->finup = ahash_finup_ctx;
1375045e3678SYuan Kang 		state->final = ahash_final_ctx;
1376045e3678SYuan Kang 	} else if (*next_buflen) {
1377307fd543SCristian Stoica 		scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
1378307fd543SCristian Stoica 					 req->nbytes, 0);
1379045e3678SYuan Kang 		*buflen = *next_buflen;
1380045e3678SYuan Kang 		*next_buflen = 0;
1381045e3678SYuan Kang 	}
1382045e3678SYuan Kang #ifdef DEBUG
1383514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "buf@"__stringify(__LINE__)": ",
1384045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, buf, *buflen, 1);
1385514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "next buf@"__stringify(__LINE__)": ",
1386045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, next_buf,
1387045e3678SYuan Kang 		       *next_buflen, 1);
1388045e3678SYuan Kang #endif
1389045e3678SYuan Kang 
1390045e3678SYuan Kang 	return ret;
139132686d34SRussell King 
139232686d34SRussell King err:
139332686d34SRussell King 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE);
139432686d34SRussell King 	kfree(edesc);
139532686d34SRussell King 	return ret;
1396045e3678SYuan Kang }
1397045e3678SYuan Kang 
1398045e3678SYuan Kang /* submit ahash finup if it the first job descriptor after update */
1399045e3678SYuan Kang static int ahash_finup_no_ctx(struct ahash_request *req)
1400045e3678SYuan Kang {
1401045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1402045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1403045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1404045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
1405045e3678SYuan Kang 	gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
1406045e3678SYuan Kang 		       CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
1407045e3678SYuan Kang 	u8 *buf = state->current_buf ? state->buf_1 : state->buf_0;
1408045e3678SYuan Kang 	int buflen = state->current_buf ? state->buflen_1 : state->buflen_0;
1409045e3678SYuan Kang 	int last_buflen = state->current_buf ? state->buflen_0 :
1410045e3678SYuan Kang 			  state->buflen_1;
141130a43b44SRussell King 	u32 *desc;
1412bc13c69eSRussell King 	int sec4_sg_bytes, sec4_sg_src_index, src_nents, mapped_nents;
1413045e3678SYuan Kang 	int digestsize = crypto_ahash_digestsize(ahash);
1414045e3678SYuan Kang 	struct ahash_edesc *edesc;
1415045e3678SYuan Kang 	int ret = 0;
1416045e3678SYuan Kang 
141713fb8fd7SLABBE Corentin 	src_nents = sg_nents_for_len(req->src, req->nbytes);
1418f9970c28SLABBE Corentin 	if (src_nents < 0) {
1419f9970c28SLABBE Corentin 		dev_err(jrdev, "Invalid number of src SG.\n");
1420f9970c28SLABBE Corentin 		return src_nents;
1421f9970c28SLABBE Corentin 	}
1422bc13c69eSRussell King 
1423bc13c69eSRussell King 	if (src_nents) {
1424bc13c69eSRussell King 		mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1425bc13c69eSRussell King 					  DMA_TO_DEVICE);
1426bc13c69eSRussell King 		if (!mapped_nents) {
1427bc13c69eSRussell King 			dev_err(jrdev, "unable to DMA map source\n");
1428bc13c69eSRussell King 			return -ENOMEM;
1429bc13c69eSRussell King 		}
1430bc13c69eSRussell King 	} else {
1431bc13c69eSRussell King 		mapped_nents = 0;
1432bc13c69eSRussell King 	}
1433bc13c69eSRussell King 
1434045e3678SYuan Kang 	sec4_sg_src_index = 2;
1435bc13c69eSRussell King 	sec4_sg_bytes = (sec4_sg_src_index + mapped_nents) *
1436045e3678SYuan Kang 			 sizeof(struct sec4_sg_entry);
1437045e3678SYuan Kang 
1438045e3678SYuan Kang 	/* allocate space for base edesc and hw desc commands, link tables */
143930a43b44SRussell King 	edesc = ahash_edesc_alloc(ctx, sec4_sg_src_index + mapped_nents,
144030a43b44SRussell King 				  ctx->sh_desc_digest, ctx->sh_desc_digest_dma,
144130a43b44SRussell King 				  flags);
1442045e3678SYuan Kang 	if (!edesc) {
1443bc13c69eSRussell King 		dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1444045e3678SYuan Kang 		return -ENOMEM;
1445045e3678SYuan Kang 	}
1446045e3678SYuan Kang 
1447045e3678SYuan Kang 	desc = edesc->hw_desc;
1448045e3678SYuan Kang 
1449045e3678SYuan Kang 	edesc->src_nents = src_nents;
1450045e3678SYuan Kang 	edesc->sec4_sg_bytes = sec4_sg_bytes;
1451045e3678SYuan Kang 
1452045e3678SYuan Kang 	state->buf_dma = try_buf_map_to_sec4_sg(jrdev, edesc->sec4_sg, buf,
1453045e3678SYuan Kang 						state->buf_dma, buflen,
1454045e3678SYuan Kang 						last_buflen);
1455045e3678SYuan Kang 
145665cf164aSRussell King 	ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 1, buflen,
145765cf164aSRussell King 				  req->nbytes);
145865cf164aSRussell King 	if (ret) {
1459ce572085SHoria Geanta 		dev_err(jrdev, "unable to map S/G table\n");
146032686d34SRussell King 		ahash_unmap(jrdev, edesc, req, digestsize);
146132686d34SRussell King 		kfree(edesc);
1462ce572085SHoria Geanta 		return -ENOMEM;
1463ce572085SHoria Geanta 	}
14641da2be33SRuchika Gupta 
1465045e3678SYuan Kang 	edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result,
1466045e3678SYuan Kang 						digestsize);
1467ce572085SHoria Geanta 	if (dma_mapping_error(jrdev, edesc->dst_dma)) {
1468ce572085SHoria Geanta 		dev_err(jrdev, "unable to map dst\n");
146932686d34SRussell King 		ahash_unmap(jrdev, edesc, req, digestsize);
147032686d34SRussell King 		kfree(edesc);
1471ce572085SHoria Geanta 		return -ENOMEM;
1472ce572085SHoria Geanta 	}
1473045e3678SYuan Kang 
1474045e3678SYuan Kang #ifdef DEBUG
1475514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
1476045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
1477045e3678SYuan Kang #endif
1478045e3678SYuan Kang 
1479045e3678SYuan Kang 	ret = caam_jr_enqueue(jrdev, desc, ahash_done, req);
1480045e3678SYuan Kang 	if (!ret) {
1481045e3678SYuan Kang 		ret = -EINPROGRESS;
1482045e3678SYuan Kang 	} else {
1483045e3678SYuan Kang 		ahash_unmap(jrdev, edesc, req, digestsize);
1484045e3678SYuan Kang 		kfree(edesc);
1485045e3678SYuan Kang 	}
1486045e3678SYuan Kang 
1487045e3678SYuan Kang 	return ret;
1488045e3678SYuan Kang }
1489045e3678SYuan Kang 
1490045e3678SYuan Kang /* submit first update job descriptor after init */
1491045e3678SYuan Kang static int ahash_update_first(struct ahash_request *req)
1492045e3678SYuan Kang {
1493045e3678SYuan Kang 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1494045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
1495045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1496045e3678SYuan Kang 	struct device *jrdev = ctx->jrdev;
1497045e3678SYuan Kang 	gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
1498045e3678SYuan Kang 		       CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
14994451d494SCristian Stoica 	u8 *next_buf = state->current_buf ? state->buf_1 : state->buf_0;
15004451d494SCristian Stoica 	int *next_buflen = state->current_buf ?
15014451d494SCristian Stoica 		&state->buflen_1 : &state->buflen_0;
1502045e3678SYuan Kang 	int to_hash;
150330a43b44SRussell King 	u32 *desc;
150465cf164aSRussell King 	int src_nents, mapped_nents;
1505045e3678SYuan Kang 	struct ahash_edesc *edesc;
1506045e3678SYuan Kang 	int ret = 0;
1507045e3678SYuan Kang 
1508045e3678SYuan Kang 	*next_buflen = req->nbytes & (crypto_tfm_alg_blocksize(&ahash->base) -
1509045e3678SYuan Kang 				      1);
1510045e3678SYuan Kang 	to_hash = req->nbytes - *next_buflen;
1511045e3678SYuan Kang 
1512045e3678SYuan Kang 	if (to_hash) {
15133d5a2db6SRussell King 		src_nents = sg_nents_for_len(req->src,
15143d5a2db6SRussell King 					     req->nbytes - *next_buflen);
1515f9970c28SLABBE Corentin 		if (src_nents < 0) {
1516f9970c28SLABBE Corentin 			dev_err(jrdev, "Invalid number of src SG.\n");
1517f9970c28SLABBE Corentin 			return src_nents;
1518f9970c28SLABBE Corentin 		}
1519bc13c69eSRussell King 
1520bc13c69eSRussell King 		if (src_nents) {
1521bc13c69eSRussell King 			mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
1522bc13c69eSRussell King 						  DMA_TO_DEVICE);
1523bc13c69eSRussell King 			if (!mapped_nents) {
1524bc13c69eSRussell King 				dev_err(jrdev, "unable to map source for DMA\n");
1525bc13c69eSRussell King 				return -ENOMEM;
1526bc13c69eSRussell King 			}
1527bc13c69eSRussell King 		} else {
1528bc13c69eSRussell King 			mapped_nents = 0;
1529bc13c69eSRussell King 		}
1530045e3678SYuan Kang 
1531045e3678SYuan Kang 		/*
1532045e3678SYuan Kang 		 * allocate space for base edesc and hw desc commands,
1533045e3678SYuan Kang 		 * link tables
1534045e3678SYuan Kang 		 */
15355588d039SRussell King 		edesc = ahash_edesc_alloc(ctx, mapped_nents > 1 ?
153630a43b44SRussell King 					  mapped_nents : 0,
153730a43b44SRussell King 					  ctx->sh_desc_update_first,
153830a43b44SRussell King 					  ctx->sh_desc_update_first_dma,
153930a43b44SRussell King 					  flags);
1540045e3678SYuan Kang 		if (!edesc) {
1541bc13c69eSRussell King 			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1542045e3678SYuan Kang 			return -ENOMEM;
1543045e3678SYuan Kang 		}
1544045e3678SYuan Kang 
1545045e3678SYuan Kang 		edesc->src_nents = src_nents;
154676b99080SHoria Geanta 		edesc->dst_dma = 0;
1547045e3678SYuan Kang 
154865cf164aSRussell King 		ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 0, 0,
154965cf164aSRussell King 					  to_hash);
155065cf164aSRussell King 		if (ret)
155132686d34SRussell King 			goto err;
1552045e3678SYuan Kang 
1553045e3678SYuan Kang 		if (*next_buflen)
1554307fd543SCristian Stoica 			scatterwalk_map_and_copy(next_buf, req->src, to_hash,
1555307fd543SCristian Stoica 						 *next_buflen, 0);
1556045e3678SYuan Kang 
1557045e3678SYuan Kang 		desc = edesc->hw_desc;
1558045e3678SYuan Kang 
1559ce572085SHoria Geanta 		ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len);
1560ce572085SHoria Geanta 		if (ret)
156132686d34SRussell King 			goto err;
1562045e3678SYuan Kang 
1563045e3678SYuan Kang #ifdef DEBUG
1564514df281SAlex Porosanu 		print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
1565045e3678SYuan Kang 			       DUMP_PREFIX_ADDRESS, 16, 4, desc,
1566045e3678SYuan Kang 			       desc_bytes(desc), 1);
1567045e3678SYuan Kang #endif
1568045e3678SYuan Kang 
156932686d34SRussell King 		ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_dst, req);
157032686d34SRussell King 		if (ret)
157132686d34SRussell King 			goto err;
157232686d34SRussell King 
1573045e3678SYuan Kang 		ret = -EINPROGRESS;
1574045e3678SYuan Kang 		state->update = ahash_update_ctx;
1575045e3678SYuan Kang 		state->finup = ahash_finup_ctx;
1576045e3678SYuan Kang 		state->final = ahash_final_ctx;
1577045e3678SYuan Kang 	} else if (*next_buflen) {
1578045e3678SYuan Kang 		state->update = ahash_update_no_ctx;
1579045e3678SYuan Kang 		state->finup = ahash_finup_no_ctx;
1580045e3678SYuan Kang 		state->final = ahash_final_no_ctx;
1581307fd543SCristian Stoica 		scatterwalk_map_and_copy(next_buf, req->src, 0,
1582307fd543SCristian Stoica 					 req->nbytes, 0);
1583045e3678SYuan Kang 	}
1584045e3678SYuan Kang #ifdef DEBUG
1585514df281SAlex Porosanu 	print_hex_dump(KERN_ERR, "next buf@"__stringify(__LINE__)": ",
1586045e3678SYuan Kang 		       DUMP_PREFIX_ADDRESS, 16, 4, next_buf,
1587045e3678SYuan Kang 		       *next_buflen, 1);
1588045e3678SYuan Kang #endif
1589045e3678SYuan Kang 
1590045e3678SYuan Kang 	return ret;
159132686d34SRussell King 
159232686d34SRussell King err:
159332686d34SRussell King 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE);
159432686d34SRussell King 	kfree(edesc);
159532686d34SRussell King 	return ret;
1596045e3678SYuan Kang }
1597045e3678SYuan Kang 
1598045e3678SYuan Kang static int ahash_finup_first(struct ahash_request *req)
1599045e3678SYuan Kang {
1600045e3678SYuan Kang 	return ahash_digest(req);
1601045e3678SYuan Kang }
1602045e3678SYuan Kang 
1603045e3678SYuan Kang static int ahash_init(struct ahash_request *req)
1604045e3678SYuan Kang {
1605045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1606045e3678SYuan Kang 
1607045e3678SYuan Kang 	state->update = ahash_update_first;
1608045e3678SYuan Kang 	state->finup = ahash_finup_first;
1609045e3678SYuan Kang 	state->final = ahash_final_no_ctx;
1610045e3678SYuan Kang 
1611045e3678SYuan Kang 	state->current_buf = 0;
1612de0e35ecSHoria Geanta 	state->buf_dma = 0;
16136fd4b156SSteve Cornelius 	state->buflen_0 = 0;
16146fd4b156SSteve Cornelius 	state->buflen_1 = 0;
1615045e3678SYuan Kang 
1616045e3678SYuan Kang 	return 0;
1617045e3678SYuan Kang }
1618045e3678SYuan Kang 
1619045e3678SYuan Kang static int ahash_update(struct ahash_request *req)
1620045e3678SYuan Kang {
1621045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1622045e3678SYuan Kang 
1623045e3678SYuan Kang 	return state->update(req);
1624045e3678SYuan Kang }
1625045e3678SYuan Kang 
1626045e3678SYuan Kang static int ahash_finup(struct ahash_request *req)
1627045e3678SYuan Kang {
1628045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1629045e3678SYuan Kang 
1630045e3678SYuan Kang 	return state->finup(req);
1631045e3678SYuan Kang }
1632045e3678SYuan Kang 
1633045e3678SYuan Kang static int ahash_final(struct ahash_request *req)
1634045e3678SYuan Kang {
1635045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
1636045e3678SYuan Kang 
1637045e3678SYuan Kang 	return state->final(req);
1638045e3678SYuan Kang }
1639045e3678SYuan Kang 
1640045e3678SYuan Kang static int ahash_export(struct ahash_request *req, void *out)
1641045e3678SYuan Kang {
1642045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
16435ec90831SRussell King 	struct caam_export_state *export = out;
16445ec90831SRussell King 	int len;
16455ec90831SRussell King 	u8 *buf;
1646045e3678SYuan Kang 
16475ec90831SRussell King 	if (state->current_buf) {
16485ec90831SRussell King 		buf = state->buf_1;
16495ec90831SRussell King 		len = state->buflen_1;
16505ec90831SRussell King 	} else {
16515ec90831SRussell King 		buf = state->buf_0;
1652f456cd2dSFabio Estevam 		len = state->buflen_0;
16535ec90831SRussell King 	}
16545ec90831SRussell King 
16555ec90831SRussell King 	memcpy(export->buf, buf, len);
16565ec90831SRussell King 	memcpy(export->caam_ctx, state->caam_ctx, sizeof(export->caam_ctx));
16575ec90831SRussell King 	export->buflen = len;
16585ec90831SRussell King 	export->update = state->update;
16595ec90831SRussell King 	export->final = state->final;
16605ec90831SRussell King 	export->finup = state->finup;
1661434b4212SRussell King 
1662045e3678SYuan Kang 	return 0;
1663045e3678SYuan Kang }
1664045e3678SYuan Kang 
1665045e3678SYuan Kang static int ahash_import(struct ahash_request *req, const void *in)
1666045e3678SYuan Kang {
1667045e3678SYuan Kang 	struct caam_hash_state *state = ahash_request_ctx(req);
16685ec90831SRussell King 	const struct caam_export_state *export = in;
1669045e3678SYuan Kang 
16705ec90831SRussell King 	memset(state, 0, sizeof(*state));
16715ec90831SRussell King 	memcpy(state->buf_0, export->buf, export->buflen);
16725ec90831SRussell King 	memcpy(state->caam_ctx, export->caam_ctx, sizeof(state->caam_ctx));
16735ec90831SRussell King 	state->buflen_0 = export->buflen;
16745ec90831SRussell King 	state->update = export->update;
16755ec90831SRussell King 	state->final = export->final;
16765ec90831SRussell King 	state->finup = export->finup;
1677434b4212SRussell King 
1678045e3678SYuan Kang 	return 0;
1679045e3678SYuan Kang }
1680045e3678SYuan Kang 
1681045e3678SYuan Kang struct caam_hash_template {
1682045e3678SYuan Kang 	char name[CRYPTO_MAX_ALG_NAME];
1683045e3678SYuan Kang 	char driver_name[CRYPTO_MAX_ALG_NAME];
1684b0e09baeSYuan Kang 	char hmac_name[CRYPTO_MAX_ALG_NAME];
1685b0e09baeSYuan Kang 	char hmac_driver_name[CRYPTO_MAX_ALG_NAME];
1686045e3678SYuan Kang 	unsigned int blocksize;
1687045e3678SYuan Kang 	struct ahash_alg template_ahash;
1688045e3678SYuan Kang 	u32 alg_type;
1689045e3678SYuan Kang 	u32 alg_op;
1690045e3678SYuan Kang };
1691045e3678SYuan Kang 
1692045e3678SYuan Kang /* ahash descriptors */
1693045e3678SYuan Kang static struct caam_hash_template driver_hash[] = {
1694045e3678SYuan Kang 	{
1695b0e09baeSYuan Kang 		.name = "sha1",
1696b0e09baeSYuan Kang 		.driver_name = "sha1-caam",
1697b0e09baeSYuan Kang 		.hmac_name = "hmac(sha1)",
1698b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha1-caam",
1699045e3678SYuan Kang 		.blocksize = SHA1_BLOCK_SIZE,
1700045e3678SYuan Kang 		.template_ahash = {
1701045e3678SYuan Kang 			.init = ahash_init,
1702045e3678SYuan Kang 			.update = ahash_update,
1703045e3678SYuan Kang 			.final = ahash_final,
1704045e3678SYuan Kang 			.finup = ahash_finup,
1705045e3678SYuan Kang 			.digest = ahash_digest,
1706045e3678SYuan Kang 			.export = ahash_export,
1707045e3678SYuan Kang 			.import = ahash_import,
1708045e3678SYuan Kang 			.setkey = ahash_setkey,
1709045e3678SYuan Kang 			.halg = {
1710045e3678SYuan Kang 				.digestsize = SHA1_DIGEST_SIZE,
17115ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1712045e3678SYuan Kang 			},
1713045e3678SYuan Kang 		},
1714045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA1,
1715045e3678SYuan Kang 		.alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
1716045e3678SYuan Kang 	}, {
1717b0e09baeSYuan Kang 		.name = "sha224",
1718b0e09baeSYuan Kang 		.driver_name = "sha224-caam",
1719b0e09baeSYuan Kang 		.hmac_name = "hmac(sha224)",
1720b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha224-caam",
1721045e3678SYuan Kang 		.blocksize = SHA224_BLOCK_SIZE,
1722045e3678SYuan Kang 		.template_ahash = {
1723045e3678SYuan Kang 			.init = ahash_init,
1724045e3678SYuan Kang 			.update = ahash_update,
1725045e3678SYuan Kang 			.final = ahash_final,
1726045e3678SYuan Kang 			.finup = ahash_finup,
1727045e3678SYuan Kang 			.digest = ahash_digest,
1728045e3678SYuan Kang 			.export = ahash_export,
1729045e3678SYuan Kang 			.import = ahash_import,
1730045e3678SYuan Kang 			.setkey = ahash_setkey,
1731045e3678SYuan Kang 			.halg = {
1732045e3678SYuan Kang 				.digestsize = SHA224_DIGEST_SIZE,
17335ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1734045e3678SYuan Kang 			},
1735045e3678SYuan Kang 		},
1736045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA224,
1737045e3678SYuan Kang 		.alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
1738045e3678SYuan Kang 	}, {
1739b0e09baeSYuan Kang 		.name = "sha256",
1740b0e09baeSYuan Kang 		.driver_name = "sha256-caam",
1741b0e09baeSYuan Kang 		.hmac_name = "hmac(sha256)",
1742b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha256-caam",
1743045e3678SYuan Kang 		.blocksize = SHA256_BLOCK_SIZE,
1744045e3678SYuan Kang 		.template_ahash = {
1745045e3678SYuan Kang 			.init = ahash_init,
1746045e3678SYuan Kang 			.update = ahash_update,
1747045e3678SYuan Kang 			.final = ahash_final,
1748045e3678SYuan Kang 			.finup = ahash_finup,
1749045e3678SYuan Kang 			.digest = ahash_digest,
1750045e3678SYuan Kang 			.export = ahash_export,
1751045e3678SYuan Kang 			.import = ahash_import,
1752045e3678SYuan Kang 			.setkey = ahash_setkey,
1753045e3678SYuan Kang 			.halg = {
1754045e3678SYuan Kang 				.digestsize = SHA256_DIGEST_SIZE,
17555ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1756045e3678SYuan Kang 			},
1757045e3678SYuan Kang 		},
1758045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA256,
1759045e3678SYuan Kang 		.alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
1760045e3678SYuan Kang 	}, {
1761b0e09baeSYuan Kang 		.name = "sha384",
1762b0e09baeSYuan Kang 		.driver_name = "sha384-caam",
1763b0e09baeSYuan Kang 		.hmac_name = "hmac(sha384)",
1764b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha384-caam",
1765045e3678SYuan Kang 		.blocksize = SHA384_BLOCK_SIZE,
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 = SHA384_DIGEST_SIZE,
17775ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1778045e3678SYuan Kang 			},
1779045e3678SYuan Kang 		},
1780045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA384,
1781045e3678SYuan Kang 		.alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
1782045e3678SYuan Kang 	}, {
1783b0e09baeSYuan Kang 		.name = "sha512",
1784b0e09baeSYuan Kang 		.driver_name = "sha512-caam",
1785b0e09baeSYuan Kang 		.hmac_name = "hmac(sha512)",
1786b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-sha512-caam",
1787045e3678SYuan Kang 		.blocksize = SHA512_BLOCK_SIZE,
1788045e3678SYuan Kang 		.template_ahash = {
1789045e3678SYuan Kang 			.init = ahash_init,
1790045e3678SYuan Kang 			.update = ahash_update,
1791045e3678SYuan Kang 			.final = ahash_final,
1792045e3678SYuan Kang 			.finup = ahash_finup,
1793045e3678SYuan Kang 			.digest = ahash_digest,
1794045e3678SYuan Kang 			.export = ahash_export,
1795045e3678SYuan Kang 			.import = ahash_import,
1796045e3678SYuan Kang 			.setkey = ahash_setkey,
1797045e3678SYuan Kang 			.halg = {
1798045e3678SYuan Kang 				.digestsize = SHA512_DIGEST_SIZE,
17995ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1800045e3678SYuan Kang 			},
1801045e3678SYuan Kang 		},
1802045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_SHA512,
1803045e3678SYuan Kang 		.alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
1804045e3678SYuan Kang 	}, {
1805b0e09baeSYuan Kang 		.name = "md5",
1806b0e09baeSYuan Kang 		.driver_name = "md5-caam",
1807b0e09baeSYuan Kang 		.hmac_name = "hmac(md5)",
1808b0e09baeSYuan Kang 		.hmac_driver_name = "hmac-md5-caam",
1809045e3678SYuan Kang 		.blocksize = MD5_BLOCK_WORDS * 4,
1810045e3678SYuan Kang 		.template_ahash = {
1811045e3678SYuan Kang 			.init = ahash_init,
1812045e3678SYuan Kang 			.update = ahash_update,
1813045e3678SYuan Kang 			.final = ahash_final,
1814045e3678SYuan Kang 			.finup = ahash_finup,
1815045e3678SYuan Kang 			.digest = ahash_digest,
1816045e3678SYuan Kang 			.export = ahash_export,
1817045e3678SYuan Kang 			.import = ahash_import,
1818045e3678SYuan Kang 			.setkey = ahash_setkey,
1819045e3678SYuan Kang 			.halg = {
1820045e3678SYuan Kang 				.digestsize = MD5_DIGEST_SIZE,
18215ec90831SRussell King 				.statesize = sizeof(struct caam_export_state),
1822045e3678SYuan Kang 			},
1823045e3678SYuan Kang 		},
1824045e3678SYuan Kang 		.alg_type = OP_ALG_ALGSEL_MD5,
1825045e3678SYuan Kang 		.alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
1826045e3678SYuan Kang 	},
1827045e3678SYuan Kang };
1828045e3678SYuan Kang 
1829045e3678SYuan Kang struct caam_hash_alg {
1830045e3678SYuan Kang 	struct list_head entry;
1831045e3678SYuan Kang 	int alg_type;
1832045e3678SYuan Kang 	int alg_op;
1833045e3678SYuan Kang 	struct ahash_alg ahash_alg;
1834045e3678SYuan Kang };
1835045e3678SYuan Kang 
1836045e3678SYuan Kang static int caam_hash_cra_init(struct crypto_tfm *tfm)
1837045e3678SYuan Kang {
1838045e3678SYuan Kang 	struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
1839045e3678SYuan Kang 	struct crypto_alg *base = tfm->__crt_alg;
1840045e3678SYuan Kang 	struct hash_alg_common *halg =
1841045e3678SYuan Kang 		 container_of(base, struct hash_alg_common, base);
1842045e3678SYuan Kang 	struct ahash_alg *alg =
1843045e3678SYuan Kang 		 container_of(halg, struct ahash_alg, halg);
1844045e3678SYuan Kang 	struct caam_hash_alg *caam_hash =
1845045e3678SYuan Kang 		 container_of(alg, struct caam_hash_alg, ahash_alg);
1846045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm);
1847045e3678SYuan Kang 	/* Sizes for MDHA running digests: MD5, SHA1, 224, 256, 384, 512 */
1848045e3678SYuan Kang 	static const u8 runninglen[] = { HASH_MSG_LEN + MD5_DIGEST_SIZE,
1849045e3678SYuan Kang 					 HASH_MSG_LEN + SHA1_DIGEST_SIZE,
1850045e3678SYuan Kang 					 HASH_MSG_LEN + 32,
1851045e3678SYuan Kang 					 HASH_MSG_LEN + SHA256_DIGEST_SIZE,
1852045e3678SYuan Kang 					 HASH_MSG_LEN + 64,
1853045e3678SYuan Kang 					 HASH_MSG_LEN + SHA512_DIGEST_SIZE };
1854045e3678SYuan Kang 	int ret = 0;
1855045e3678SYuan Kang 
1856045e3678SYuan Kang 	/*
1857cfc6f11bSRuchika Gupta 	 * Get a Job ring from Job Ring driver to ensure in-order
1858045e3678SYuan Kang 	 * crypto request processing per tfm
1859045e3678SYuan Kang 	 */
1860cfc6f11bSRuchika Gupta 	ctx->jrdev = caam_jr_alloc();
1861cfc6f11bSRuchika Gupta 	if (IS_ERR(ctx->jrdev)) {
1862cfc6f11bSRuchika Gupta 		pr_err("Job Ring Device allocation for transform failed\n");
1863cfc6f11bSRuchika Gupta 		return PTR_ERR(ctx->jrdev);
1864cfc6f11bSRuchika Gupta 	}
1865045e3678SYuan Kang 	/* copy descriptor header template value */
1866045e3678SYuan Kang 	ctx->alg_type = OP_TYPE_CLASS2_ALG | caam_hash->alg_type;
1867045e3678SYuan Kang 	ctx->alg_op = OP_TYPE_CLASS2_ALG | caam_hash->alg_op;
1868045e3678SYuan Kang 
1869045e3678SYuan Kang 	ctx->ctx_len = runninglen[(ctx->alg_op & OP_ALG_ALGSEL_SUBMASK) >>
1870045e3678SYuan Kang 				  OP_ALG_ALGSEL_SHIFT];
1871045e3678SYuan Kang 
1872045e3678SYuan Kang 	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
1873045e3678SYuan Kang 				 sizeof(struct caam_hash_state));
1874045e3678SYuan Kang 
1875045e3678SYuan Kang 	ret = ahash_set_sh_desc(ahash);
1876045e3678SYuan Kang 
1877045e3678SYuan Kang 	return ret;
1878045e3678SYuan Kang }
1879045e3678SYuan Kang 
1880045e3678SYuan Kang static void caam_hash_cra_exit(struct crypto_tfm *tfm)
1881045e3678SYuan Kang {
1882045e3678SYuan Kang 	struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm);
1883045e3678SYuan Kang 
1884045e3678SYuan Kang 	if (ctx->sh_desc_update_dma &&
1885045e3678SYuan Kang 	    !dma_mapping_error(ctx->jrdev, ctx->sh_desc_update_dma))
1886045e3678SYuan Kang 		dma_unmap_single(ctx->jrdev, ctx->sh_desc_update_dma,
1887045e3678SYuan Kang 				 desc_bytes(ctx->sh_desc_update),
1888045e3678SYuan Kang 				 DMA_TO_DEVICE);
1889045e3678SYuan Kang 	if (ctx->sh_desc_update_first_dma &&
1890045e3678SYuan Kang 	    !dma_mapping_error(ctx->jrdev, ctx->sh_desc_update_first_dma))
1891045e3678SYuan Kang 		dma_unmap_single(ctx->jrdev, ctx->sh_desc_update_first_dma,
1892045e3678SYuan Kang 				 desc_bytes(ctx->sh_desc_update_first),
1893045e3678SYuan Kang 				 DMA_TO_DEVICE);
1894045e3678SYuan Kang 	if (ctx->sh_desc_fin_dma &&
1895045e3678SYuan Kang 	    !dma_mapping_error(ctx->jrdev, ctx->sh_desc_fin_dma))
1896045e3678SYuan Kang 		dma_unmap_single(ctx->jrdev, ctx->sh_desc_fin_dma,
1897045e3678SYuan Kang 				 desc_bytes(ctx->sh_desc_fin), DMA_TO_DEVICE);
1898045e3678SYuan Kang 	if (ctx->sh_desc_digest_dma &&
1899045e3678SYuan Kang 	    !dma_mapping_error(ctx->jrdev, ctx->sh_desc_digest_dma))
1900045e3678SYuan Kang 		dma_unmap_single(ctx->jrdev, ctx->sh_desc_digest_dma,
1901045e3678SYuan Kang 				 desc_bytes(ctx->sh_desc_digest),
1902045e3678SYuan Kang 				 DMA_TO_DEVICE);
1903045e3678SYuan Kang 	if (ctx->sh_desc_finup_dma &&
1904045e3678SYuan Kang 	    !dma_mapping_error(ctx->jrdev, ctx->sh_desc_finup_dma))
1905045e3678SYuan Kang 		dma_unmap_single(ctx->jrdev, ctx->sh_desc_finup_dma,
1906045e3678SYuan Kang 				 desc_bytes(ctx->sh_desc_finup), DMA_TO_DEVICE);
1907cfc6f11bSRuchika Gupta 
1908cfc6f11bSRuchika Gupta 	caam_jr_free(ctx->jrdev);
1909045e3678SYuan Kang }
1910045e3678SYuan Kang 
1911045e3678SYuan Kang static void __exit caam_algapi_hash_exit(void)
1912045e3678SYuan Kang {
1913045e3678SYuan Kang 	struct caam_hash_alg *t_alg, *n;
1914045e3678SYuan Kang 
1915cfc6f11bSRuchika Gupta 	if (!hash_list.next)
1916045e3678SYuan Kang 		return;
1917045e3678SYuan Kang 
1918cfc6f11bSRuchika Gupta 	list_for_each_entry_safe(t_alg, n, &hash_list, entry) {
1919045e3678SYuan Kang 		crypto_unregister_ahash(&t_alg->ahash_alg);
1920045e3678SYuan Kang 		list_del(&t_alg->entry);
1921045e3678SYuan Kang 		kfree(t_alg);
1922045e3678SYuan Kang 	}
1923045e3678SYuan Kang }
1924045e3678SYuan Kang 
1925045e3678SYuan Kang static struct caam_hash_alg *
1926cfc6f11bSRuchika Gupta caam_hash_alloc(struct caam_hash_template *template,
1927b0e09baeSYuan Kang 		bool keyed)
1928045e3678SYuan Kang {
1929045e3678SYuan Kang 	struct caam_hash_alg *t_alg;
1930045e3678SYuan Kang 	struct ahash_alg *halg;
1931045e3678SYuan Kang 	struct crypto_alg *alg;
1932045e3678SYuan Kang 
19339c4f9733SFabio Estevam 	t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL);
1934045e3678SYuan Kang 	if (!t_alg) {
1935cfc6f11bSRuchika Gupta 		pr_err("failed to allocate t_alg\n");
1936045e3678SYuan Kang 		return ERR_PTR(-ENOMEM);
1937045e3678SYuan Kang 	}
1938045e3678SYuan Kang 
1939045e3678SYuan Kang 	t_alg->ahash_alg = template->template_ahash;
1940045e3678SYuan Kang 	halg = &t_alg->ahash_alg;
1941045e3678SYuan Kang 	alg = &halg->halg.base;
1942045e3678SYuan Kang 
1943b0e09baeSYuan Kang 	if (keyed) {
1944b0e09baeSYuan Kang 		snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
1945b0e09baeSYuan Kang 			 template->hmac_name);
1946b0e09baeSYuan Kang 		snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
1947b0e09baeSYuan Kang 			 template->hmac_driver_name);
1948b0e09baeSYuan Kang 	} else {
1949b0e09baeSYuan Kang 		snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
1950b0e09baeSYuan Kang 			 template->name);
1951045e3678SYuan Kang 		snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
1952045e3678SYuan Kang 			 template->driver_name);
1953a0118c8bSRussell King 		t_alg->ahash_alg.setkey = NULL;
1954b0e09baeSYuan Kang 	}
1955045e3678SYuan Kang 	alg->cra_module = THIS_MODULE;
1956045e3678SYuan Kang 	alg->cra_init = caam_hash_cra_init;
1957045e3678SYuan Kang 	alg->cra_exit = caam_hash_cra_exit;
1958045e3678SYuan Kang 	alg->cra_ctxsize = sizeof(struct caam_hash_ctx);
1959045e3678SYuan Kang 	alg->cra_priority = CAAM_CRA_PRIORITY;
1960045e3678SYuan Kang 	alg->cra_blocksize = template->blocksize;
1961045e3678SYuan Kang 	alg->cra_alignmask = 0;
1962045e3678SYuan Kang 	alg->cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_TYPE_AHASH;
1963045e3678SYuan Kang 	alg->cra_type = &crypto_ahash_type;
1964045e3678SYuan Kang 
1965045e3678SYuan Kang 	t_alg->alg_type = template->alg_type;
1966045e3678SYuan Kang 	t_alg->alg_op = template->alg_op;
1967045e3678SYuan Kang 
1968045e3678SYuan Kang 	return t_alg;
1969045e3678SYuan Kang }
1970045e3678SYuan Kang 
1971045e3678SYuan Kang static int __init caam_algapi_hash_init(void)
1972045e3678SYuan Kang {
197335af6403SRuchika Gupta 	struct device_node *dev_node;
197435af6403SRuchika Gupta 	struct platform_device *pdev;
197535af6403SRuchika Gupta 	struct device *ctrldev;
1976045e3678SYuan Kang 	int i = 0, err = 0;
1977bf83490eSVictoria Milhoan 	struct caam_drv_private *priv;
1978bf83490eSVictoria Milhoan 	unsigned int md_limit = SHA512_DIGEST_SIZE;
1979bf83490eSVictoria Milhoan 	u32 cha_inst, cha_vid;
1980045e3678SYuan Kang 
198135af6403SRuchika Gupta 	dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
198235af6403SRuchika Gupta 	if (!dev_node) {
198335af6403SRuchika Gupta 		dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec4.0");
198435af6403SRuchika Gupta 		if (!dev_node)
198535af6403SRuchika Gupta 			return -ENODEV;
198635af6403SRuchika Gupta 	}
198735af6403SRuchika Gupta 
198835af6403SRuchika Gupta 	pdev = of_find_device_by_node(dev_node);
198935af6403SRuchika Gupta 	if (!pdev) {
199035af6403SRuchika Gupta 		of_node_put(dev_node);
199135af6403SRuchika Gupta 		return -ENODEV;
199235af6403SRuchika Gupta 	}
199335af6403SRuchika Gupta 
199435af6403SRuchika Gupta 	ctrldev = &pdev->dev;
199535af6403SRuchika Gupta 	priv = dev_get_drvdata(ctrldev);
199635af6403SRuchika Gupta 	of_node_put(dev_node);
199735af6403SRuchika Gupta 
199835af6403SRuchika Gupta 	/*
199935af6403SRuchika Gupta 	 * If priv is NULL, it's probably because the caam driver wasn't
200035af6403SRuchika Gupta 	 * properly initialized (e.g. RNG4 init failed). Thus, bail out here.
200135af6403SRuchika Gupta 	 */
200235af6403SRuchika Gupta 	if (!priv)
200335af6403SRuchika Gupta 		return -ENODEV;
200435af6403SRuchika Gupta 
2005bf83490eSVictoria Milhoan 	/*
2006bf83490eSVictoria Milhoan 	 * Register crypto algorithms the device supports.  First, identify
2007bf83490eSVictoria Milhoan 	 * presence and attributes of MD block.
2008bf83490eSVictoria Milhoan 	 */
2009bf83490eSVictoria Milhoan 	cha_vid = rd_reg32(&priv->ctrl->perfmon.cha_id_ls);
2010bf83490eSVictoria Milhoan 	cha_inst = rd_reg32(&priv->ctrl->perfmon.cha_num_ls);
2011bf83490eSVictoria Milhoan 
2012bf83490eSVictoria Milhoan 	/*
2013bf83490eSVictoria Milhoan 	 * Skip registration of any hashing algorithms if MD block
2014bf83490eSVictoria Milhoan 	 * is not present.
2015bf83490eSVictoria Milhoan 	 */
2016bf83490eSVictoria Milhoan 	if (!((cha_inst & CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT))
2017bf83490eSVictoria Milhoan 		return -ENODEV;
2018bf83490eSVictoria Milhoan 
2019bf83490eSVictoria Milhoan 	/* Limit digest size based on LP256 */
2020bf83490eSVictoria Milhoan 	if ((cha_vid & CHA_ID_LS_MD_MASK) == CHA_ID_LS_MD_LP256)
2021bf83490eSVictoria Milhoan 		md_limit = SHA256_DIGEST_SIZE;
2022bf83490eSVictoria Milhoan 
2023cfc6f11bSRuchika Gupta 	INIT_LIST_HEAD(&hash_list);
2024045e3678SYuan Kang 
2025045e3678SYuan Kang 	/* register crypto algorithms the device supports */
2026045e3678SYuan Kang 	for (i = 0; i < ARRAY_SIZE(driver_hash); i++) {
2027045e3678SYuan Kang 		struct caam_hash_alg *t_alg;
2028bf83490eSVictoria Milhoan 		struct caam_hash_template *alg = driver_hash + i;
2029bf83490eSVictoria Milhoan 
2030bf83490eSVictoria Milhoan 		/* If MD size is not supported by device, skip registration */
2031bf83490eSVictoria Milhoan 		if (alg->template_ahash.halg.digestsize > md_limit)
2032bf83490eSVictoria Milhoan 			continue;
2033045e3678SYuan Kang 
2034b0e09baeSYuan Kang 		/* register hmac version */
2035bf83490eSVictoria Milhoan 		t_alg = caam_hash_alloc(alg, true);
2036b0e09baeSYuan Kang 		if (IS_ERR(t_alg)) {
2037b0e09baeSYuan Kang 			err = PTR_ERR(t_alg);
2038bf83490eSVictoria Milhoan 			pr_warn("%s alg allocation failed\n", alg->driver_name);
2039b0e09baeSYuan Kang 			continue;
2040b0e09baeSYuan Kang 		}
2041b0e09baeSYuan Kang 
2042b0e09baeSYuan Kang 		err = crypto_register_ahash(&t_alg->ahash_alg);
2043b0e09baeSYuan Kang 		if (err) {
20446ea30f0aSRussell King 			pr_warn("%s alg registration failed: %d\n",
20456ea30f0aSRussell King 				t_alg->ahash_alg.halg.base.cra_driver_name,
20466ea30f0aSRussell King 				err);
2047b0e09baeSYuan Kang 			kfree(t_alg);
2048b0e09baeSYuan Kang 		} else
2049cfc6f11bSRuchika Gupta 			list_add_tail(&t_alg->entry, &hash_list);
2050b0e09baeSYuan Kang 
2051b0e09baeSYuan Kang 		/* register unkeyed version */
2052bf83490eSVictoria Milhoan 		t_alg = caam_hash_alloc(alg, false);
2053045e3678SYuan Kang 		if (IS_ERR(t_alg)) {
2054045e3678SYuan Kang 			err = PTR_ERR(t_alg);
2055bf83490eSVictoria Milhoan 			pr_warn("%s alg allocation failed\n", alg->driver_name);
2056045e3678SYuan Kang 			continue;
2057045e3678SYuan Kang 		}
2058045e3678SYuan Kang 
2059045e3678SYuan Kang 		err = crypto_register_ahash(&t_alg->ahash_alg);
2060045e3678SYuan Kang 		if (err) {
20616ea30f0aSRussell King 			pr_warn("%s alg registration failed: %d\n",
20626ea30f0aSRussell King 				t_alg->ahash_alg.halg.base.cra_driver_name,
20636ea30f0aSRussell King 				err);
2064045e3678SYuan Kang 			kfree(t_alg);
2065045e3678SYuan Kang 		} else
2066cfc6f11bSRuchika Gupta 			list_add_tail(&t_alg->entry, &hash_list);
2067045e3678SYuan Kang 	}
2068045e3678SYuan Kang 
2069045e3678SYuan Kang 	return err;
2070045e3678SYuan Kang }
2071045e3678SYuan Kang 
2072045e3678SYuan Kang module_init(caam_algapi_hash_init);
2073045e3678SYuan Kang module_exit(caam_algapi_hash_exit);
2074045e3678SYuan Kang 
2075045e3678SYuan Kang MODULE_LICENSE("GPL");
2076045e3678SYuan Kang MODULE_DESCRIPTION("FSL CAAM support for ahash functions of crypto API");
2077045e3678SYuan Kang MODULE_AUTHOR("Freescale Semiconductor - NMG");
2078