1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * caam descriptor construction helper functions
4  *
5  * Copyright 2008-2012 Freescale Semiconductor, Inc.
6  * Copyright 2019 NXP
7  */
8 
9 #ifndef DESC_CONSTR_H
10 #define DESC_CONSTR_H
11 
12 #include "desc.h"
13 #include "regs.h"
14 
15 #define IMMEDIATE (1 << 23)
16 #define CAAM_CMD_SZ sizeof(u32)
17 #define CAAM_PTR_SZ caam_ptr_sz
18 #define CAAM_PTR_SZ_MAX sizeof(dma_addr_t)
19 #define CAAM_PTR_SZ_MIN sizeof(u32)
20 #define CAAM_DESC_BYTES_MAX (CAAM_CMD_SZ * MAX_CAAM_DESCSIZE)
21 #define __DESC_JOB_IO_LEN(n) (CAAM_CMD_SZ * 5 + (n) * 3)
22 #define DESC_JOB_IO_LEN __DESC_JOB_IO_LEN(CAAM_PTR_SZ)
23 #define DESC_JOB_IO_LEN_MAX __DESC_JOB_IO_LEN(CAAM_PTR_SZ_MAX)
24 #define DESC_JOB_IO_LEN_MIN __DESC_JOB_IO_LEN(CAAM_PTR_SZ_MIN)
25 
26 /*
27  * The CAAM QI hardware constructs a job descriptor which points
28  * to shared descriptor (as pointed by context_a of FQ to CAAM).
29  * When the job descriptor is executed by deco, the whole job
30  * descriptor together with shared descriptor gets loaded in
31  * deco buffer which is 64 words long (each 32-bit).
32  *
33  * The job descriptor constructed by QI hardware has layout:
34  *
35  *	HEADER		(1 word)
36  *	Shdesc ptr	(1 or 2 words)
37  *	SEQ_OUT_PTR	(1 word)
38  *	Out ptr		(1 or 2 words)
39  *	Out length	(1 word)
40  *	SEQ_IN_PTR	(1 word)
41  *	In ptr		(1 or 2 words)
42  *	In length	(1 word)
43  *
44  * The shdesc ptr is used to fetch shared descriptor contents
45  * into deco buffer.
46  *
47  * Apart from shdesc contents, the total number of words that
48  * get loaded in deco buffer are '8' or '11'. The remaining words
49  * in deco buffer can be used for storing shared descriptor.
50  */
51 #define MAX_SDLEN	((CAAM_DESC_BYTES_MAX - DESC_JOB_IO_LEN_MIN) / CAAM_CMD_SZ)
52 
53 #ifdef DEBUG
54 #define PRINT_POS do { printk(KERN_DEBUG "%02d: %s\n", desc_len(desc),\
55 			      &__func__[sizeof("append")]); } while (0)
56 #else
57 #define PRINT_POS
58 #endif
59 
60 #define SET_OK_NO_PROP_ERRORS (IMMEDIATE | LDST_CLASS_DECO | \
61 			       LDST_SRCDST_WORD_DECOCTRL | \
62 			       (LDOFF_CHG_SHARE_OK_NO_PROP << \
63 				LDST_OFFSET_SHIFT))
64 #define DISABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \
65 				LDST_SRCDST_WORD_DECOCTRL | \
66 				(LDOFF_DISABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT))
67 #define ENABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \
68 			       LDST_SRCDST_WORD_DECOCTRL | \
69 			       (LDOFF_ENABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT))
70 
71 extern bool caam_little_end;
72 extern size_t caam_ptr_sz;
73 
74 /*
75  * HW fetches 4 S/G table entries at a time, irrespective of how many entries
76  * are in the table. It's SW's responsibility to make sure these accesses
77  * do not have side effects.
78  */
79 static inline int pad_sg_nents(int sg_nents)
80 {
81 	return ALIGN(sg_nents, 4);
82 }
83 
84 static inline int desc_len(u32 * const desc)
85 {
86 	return caam32_to_cpu(*desc) & HDR_DESCLEN_MASK;
87 }
88 
89 static inline int desc_bytes(void * const desc)
90 {
91 	return desc_len(desc) * CAAM_CMD_SZ;
92 }
93 
94 static inline u32 *desc_end(u32 * const desc)
95 {
96 	return desc + desc_len(desc);
97 }
98 
99 static inline void *sh_desc_pdb(u32 * const desc)
100 {
101 	return desc + 1;
102 }
103 
104 static inline void init_desc(u32 * const desc, u32 options)
105 {
106 	*desc = cpu_to_caam32((options | HDR_ONE) + 1);
107 }
108 
109 static inline void init_sh_desc(u32 * const desc, u32 options)
110 {
111 	PRINT_POS;
112 	init_desc(desc, CMD_SHARED_DESC_HDR | options);
113 }
114 
115 static inline void init_sh_desc_pdb(u32 * const desc, u32 options,
116 				    size_t pdb_bytes)
117 {
118 	u32 pdb_len = (pdb_bytes + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
119 
120 	init_sh_desc(desc, (((pdb_len + 1) << HDR_START_IDX_SHIFT) + pdb_len) |
121 		     options);
122 }
123 
124 static inline void init_job_desc(u32 * const desc, u32 options)
125 {
126 	init_desc(desc, CMD_DESC_HDR | options);
127 }
128 
129 static inline void init_job_desc_pdb(u32 * const desc, u32 options,
130 				     size_t pdb_bytes)
131 {
132 	u32 pdb_len = (pdb_bytes + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
133 
134 	init_job_desc(desc, (((pdb_len + 1) << HDR_START_IDX_SHIFT)) | options);
135 }
136 
137 static inline void append_ptr(u32 * const desc, dma_addr_t ptr)
138 {
139 	if (caam_ptr_sz == sizeof(dma_addr_t)) {
140 		dma_addr_t *offset = (dma_addr_t *)desc_end(desc);
141 
142 		*offset = cpu_to_caam_dma(ptr);
143 	} else {
144 		u32 *offset = (u32 *)desc_end(desc);
145 
146 		*offset = cpu_to_caam_dma(ptr);
147 	}
148 
149 	(*desc) = cpu_to_caam32(caam32_to_cpu(*desc) +
150 				CAAM_PTR_SZ / CAAM_CMD_SZ);
151 }
152 
153 static inline void init_job_desc_shared(u32 * const desc, dma_addr_t ptr,
154 					int len, u32 options)
155 {
156 	PRINT_POS;
157 	init_job_desc(desc, HDR_SHARED | options |
158 		      (len << HDR_START_IDX_SHIFT));
159 	append_ptr(desc, ptr);
160 }
161 
162 static inline void append_data(u32 * const desc, const void *data, int len)
163 {
164 	u32 *offset = desc_end(desc);
165 
166 	/* Avoid gcc warning: memcpy with data == NULL */
167 	if (!IS_ENABLED(CONFIG_CRYPTO_DEV_FSL_CAAM_DEBUG) || data)
168 		memcpy(offset, data, len);
169 
170 	(*desc) = cpu_to_caam32(caam32_to_cpu(*desc) +
171 				(len + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ);
172 }
173 
174 static inline void append_cmd(u32 * const desc, u32 command)
175 {
176 	u32 *cmd = desc_end(desc);
177 
178 	*cmd = cpu_to_caam32(command);
179 
180 	(*desc) = cpu_to_caam32(caam32_to_cpu(*desc) + 1);
181 }
182 
183 #define append_u32 append_cmd
184 
185 static inline void append_u64(u32 * const desc, u64 data)
186 {
187 	u32 *offset = desc_end(desc);
188 
189 	/* Only 32-bit alignment is guaranteed in descriptor buffer */
190 	if (caam_little_end) {
191 		*offset = cpu_to_caam32(lower_32_bits(data));
192 		*(++offset) = cpu_to_caam32(upper_32_bits(data));
193 	} else {
194 		*offset = cpu_to_caam32(upper_32_bits(data));
195 		*(++offset) = cpu_to_caam32(lower_32_bits(data));
196 	}
197 
198 	(*desc) = cpu_to_caam32(caam32_to_cpu(*desc) + 2);
199 }
200 
201 /* Write command without affecting header, and return pointer to next word */
202 static inline u32 *write_cmd(u32 * const desc, u32 command)
203 {
204 	*desc = cpu_to_caam32(command);
205 
206 	return desc + 1;
207 }
208 
209 static inline void append_cmd_ptr(u32 * const desc, dma_addr_t ptr, int len,
210 				  u32 command)
211 {
212 	append_cmd(desc, command | len);
213 	append_ptr(desc, ptr);
214 }
215 
216 /* Write length after pointer, rather than inside command */
217 static inline void append_cmd_ptr_extlen(u32 * const desc, dma_addr_t ptr,
218 					 unsigned int len, u32 command)
219 {
220 	append_cmd(desc, command);
221 	if (!(command & (SQIN_RTO | SQIN_PRE)))
222 		append_ptr(desc, ptr);
223 	append_cmd(desc, len);
224 }
225 
226 static inline void append_cmd_data(u32 * const desc, const void *data, int len,
227 				   u32 command)
228 {
229 	append_cmd(desc, command | IMMEDIATE | len);
230 	append_data(desc, data, len);
231 }
232 
233 #define APPEND_CMD_RET(cmd, op) \
234 static inline u32 *append_##cmd(u32 * const desc, u32 options) \
235 { \
236 	u32 *cmd = desc_end(desc); \
237 	PRINT_POS; \
238 	append_cmd(desc, CMD_##op | options); \
239 	return cmd; \
240 }
241 APPEND_CMD_RET(jump, JUMP)
242 APPEND_CMD_RET(move, MOVE)
243 APPEND_CMD_RET(move_len, MOVE_LEN)
244 
245 static inline void set_jump_tgt_here(u32 * const desc, u32 *jump_cmd)
246 {
247 	*jump_cmd = cpu_to_caam32(caam32_to_cpu(*jump_cmd) |
248 				  (desc_len(desc) - (jump_cmd - desc)));
249 }
250 
251 static inline void set_move_tgt_here(u32 * const desc, u32 *move_cmd)
252 {
253 	u32 val = caam32_to_cpu(*move_cmd);
254 
255 	val &= ~MOVE_OFFSET_MASK;
256 	val |= (desc_len(desc) << (MOVE_OFFSET_SHIFT + 2)) & MOVE_OFFSET_MASK;
257 	*move_cmd = cpu_to_caam32(val);
258 }
259 
260 #define APPEND_CMD(cmd, op) \
261 static inline void append_##cmd(u32 * const desc, u32 options) \
262 { \
263 	PRINT_POS; \
264 	append_cmd(desc, CMD_##op | options); \
265 }
266 APPEND_CMD(operation, OPERATION)
267 
268 #define APPEND_CMD_LEN(cmd, op) \
269 static inline void append_##cmd(u32 * const desc, unsigned int len, \
270 				u32 options) \
271 { \
272 	PRINT_POS; \
273 	append_cmd(desc, CMD_##op | len | options); \
274 }
275 
276 APPEND_CMD_LEN(seq_load, SEQ_LOAD)
277 APPEND_CMD_LEN(seq_store, SEQ_STORE)
278 APPEND_CMD_LEN(seq_fifo_load, SEQ_FIFO_LOAD)
279 APPEND_CMD_LEN(seq_fifo_store, SEQ_FIFO_STORE)
280 
281 #define APPEND_CMD_PTR(cmd, op) \
282 static inline void append_##cmd(u32 * const desc, dma_addr_t ptr, \
283 				unsigned int len, u32 options) \
284 { \
285 	PRINT_POS; \
286 	append_cmd_ptr(desc, ptr, len, CMD_##op | options); \
287 }
288 APPEND_CMD_PTR(key, KEY)
289 APPEND_CMD_PTR(load, LOAD)
290 APPEND_CMD_PTR(fifo_load, FIFO_LOAD)
291 APPEND_CMD_PTR(fifo_store, FIFO_STORE)
292 
293 static inline void append_store(u32 * const desc, dma_addr_t ptr,
294 				unsigned int len, u32 options)
295 {
296 	u32 cmd_src;
297 
298 	cmd_src = options & LDST_SRCDST_MASK;
299 
300 	append_cmd(desc, CMD_STORE | options | len);
301 
302 	/* The following options do not require pointer */
303 	if (!(cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED ||
304 	      cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB    ||
305 	      cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB_WE ||
306 	      cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED_WE))
307 		append_ptr(desc, ptr);
308 }
309 
310 #define APPEND_SEQ_PTR_INTLEN(cmd, op) \
311 static inline void append_seq_##cmd##_ptr_intlen(u32 * const desc, \
312 						 dma_addr_t ptr, \
313 						 unsigned int len, \
314 						 u32 options) \
315 { \
316 	PRINT_POS; \
317 	if (options & (SQIN_RTO | SQIN_PRE)) \
318 		append_cmd(desc, CMD_SEQ_##op##_PTR | len | options); \
319 	else \
320 		append_cmd_ptr(desc, ptr, len, CMD_SEQ_##op##_PTR | options); \
321 }
322 APPEND_SEQ_PTR_INTLEN(in, IN)
323 APPEND_SEQ_PTR_INTLEN(out, OUT)
324 
325 #define APPEND_CMD_PTR_TO_IMM(cmd, op) \
326 static inline void append_##cmd##_as_imm(u32 * const desc, const void *data, \
327 					 unsigned int len, u32 options) \
328 { \
329 	PRINT_POS; \
330 	append_cmd_data(desc, data, len, CMD_##op | options); \
331 }
332 APPEND_CMD_PTR_TO_IMM(load, LOAD);
333 APPEND_CMD_PTR_TO_IMM(fifo_load, FIFO_LOAD);
334 
335 #define APPEND_CMD_PTR_EXTLEN(cmd, op) \
336 static inline void append_##cmd##_extlen(u32 * const desc, dma_addr_t ptr, \
337 					 unsigned int len, u32 options) \
338 { \
339 	PRINT_POS; \
340 	append_cmd_ptr_extlen(desc, ptr, len, CMD_##op | SQIN_EXT | options); \
341 }
342 APPEND_CMD_PTR_EXTLEN(seq_in_ptr, SEQ_IN_PTR)
343 APPEND_CMD_PTR_EXTLEN(seq_out_ptr, SEQ_OUT_PTR)
344 
345 /*
346  * Determine whether to store length internally or externally depending on
347  * the size of its type
348  */
349 #define APPEND_CMD_PTR_LEN(cmd, op, type) \
350 static inline void append_##cmd(u32 * const desc, dma_addr_t ptr, \
351 				type len, u32 options) \
352 { \
353 	PRINT_POS; \
354 	if (sizeof(type) > sizeof(u16)) \
355 		append_##cmd##_extlen(desc, ptr, len, options); \
356 	else \
357 		append_##cmd##_intlen(desc, ptr, len, options); \
358 }
359 APPEND_CMD_PTR_LEN(seq_in_ptr, SEQ_IN_PTR, u32)
360 APPEND_CMD_PTR_LEN(seq_out_ptr, SEQ_OUT_PTR, u32)
361 
362 /*
363  * 2nd variant for commands whose specified immediate length differs
364  * from length of immediate data provided, e.g., split keys
365  */
366 #define APPEND_CMD_PTR_TO_IMM2(cmd, op) \
367 static inline void append_##cmd##_as_imm(u32 * const desc, const void *data, \
368 					 unsigned int data_len, \
369 					 unsigned int len, u32 options) \
370 { \
371 	PRINT_POS; \
372 	append_cmd(desc, CMD_##op | IMMEDIATE | len | options); \
373 	append_data(desc, data, data_len); \
374 }
375 APPEND_CMD_PTR_TO_IMM2(key, KEY);
376 
377 #define APPEND_CMD_RAW_IMM(cmd, op, type) \
378 static inline void append_##cmd##_imm_##type(u32 * const desc, type immediate, \
379 					     u32 options) \
380 { \
381 	PRINT_POS; \
382 	if (options & LDST_LEN_MASK) \
383 		append_cmd(desc, CMD_##op | IMMEDIATE | options); \
384 	else \
385 		append_cmd(desc, CMD_##op | IMMEDIATE | options | \
386 			   sizeof(type)); \
387 	append_cmd(desc, immediate); \
388 }
389 APPEND_CMD_RAW_IMM(load, LOAD, u32);
390 
391 /*
392  * ee - endianness
393  * size - size of immediate type in bytes
394  */
395 #define APPEND_CMD_RAW_IMM2(cmd, op, ee, size) \
396 static inline void append_##cmd##_imm_##ee##size(u32 *desc, \
397 						   u##size immediate, \
398 						   u32 options) \
399 { \
400 	__##ee##size data = cpu_to_##ee##size(immediate); \
401 	PRINT_POS; \
402 	append_cmd(desc, CMD_##op | IMMEDIATE | options | sizeof(data)); \
403 	append_data(desc, &data, sizeof(data)); \
404 }
405 
406 APPEND_CMD_RAW_IMM2(load, LOAD, be, 32);
407 
408 /*
409  * Append math command. Only the last part of destination and source need to
410  * be specified
411  */
412 #define APPEND_MATH(op, desc, dest, src_0, src_1, len) \
413 append_cmd(desc, CMD_MATH | MATH_FUN_##op | MATH_DEST_##dest | \
414 	MATH_SRC0_##src_0 | MATH_SRC1_##src_1 | (u32)len);
415 
416 #define append_math_add(desc, dest, src0, src1, len) \
417 	APPEND_MATH(ADD, desc, dest, src0, src1, len)
418 #define append_math_sub(desc, dest, src0, src1, len) \
419 	APPEND_MATH(SUB, desc, dest, src0, src1, len)
420 #define append_math_add_c(desc, dest, src0, src1, len) \
421 	APPEND_MATH(ADDC, desc, dest, src0, src1, len)
422 #define append_math_sub_b(desc, dest, src0, src1, len) \
423 	APPEND_MATH(SUBB, desc, dest, src0, src1, len)
424 #define append_math_and(desc, dest, src0, src1, len) \
425 	APPEND_MATH(AND, desc, dest, src0, src1, len)
426 #define append_math_or(desc, dest, src0, src1, len) \
427 	APPEND_MATH(OR, desc, dest, src0, src1, len)
428 #define append_math_xor(desc, dest, src0, src1, len) \
429 	APPEND_MATH(XOR, desc, dest, src0, src1, len)
430 #define append_math_lshift(desc, dest, src0, src1, len) \
431 	APPEND_MATH(LSHIFT, desc, dest, src0, src1, len)
432 #define append_math_rshift(desc, dest, src0, src1, len) \
433 	APPEND_MATH(RSHIFT, desc, dest, src0, src1, len)
434 #define append_math_ldshift(desc, dest, src0, src1, len) \
435 	APPEND_MATH(SHLD, desc, dest, src0, src1, len)
436 
437 /* Exactly one source is IMM. Data is passed in as u32 value */
438 #define APPEND_MATH_IMM_u32(op, desc, dest, src_0, src_1, data) \
439 do { \
440 	APPEND_MATH(op, desc, dest, src_0, src_1, CAAM_CMD_SZ); \
441 	append_cmd(desc, data); \
442 } while (0)
443 
444 #define append_math_add_imm_u32(desc, dest, src0, src1, data) \
445 	APPEND_MATH_IMM_u32(ADD, desc, dest, src0, src1, data)
446 #define append_math_sub_imm_u32(desc, dest, src0, src1, data) \
447 	APPEND_MATH_IMM_u32(SUB, desc, dest, src0, src1, data)
448 #define append_math_add_c_imm_u32(desc, dest, src0, src1, data) \
449 	APPEND_MATH_IMM_u32(ADDC, desc, dest, src0, src1, data)
450 #define append_math_sub_b_imm_u32(desc, dest, src0, src1, data) \
451 	APPEND_MATH_IMM_u32(SUBB, desc, dest, src0, src1, data)
452 #define append_math_and_imm_u32(desc, dest, src0, src1, data) \
453 	APPEND_MATH_IMM_u32(AND, desc, dest, src0, src1, data)
454 #define append_math_or_imm_u32(desc, dest, src0, src1, data) \
455 	APPEND_MATH_IMM_u32(OR, desc, dest, src0, src1, data)
456 #define append_math_xor_imm_u32(desc, dest, src0, src1, data) \
457 	APPEND_MATH_IMM_u32(XOR, desc, dest, src0, src1, data)
458 #define append_math_lshift_imm_u32(desc, dest, src0, src1, data) \
459 	APPEND_MATH_IMM_u32(LSHIFT, desc, dest, src0, src1, data)
460 #define append_math_rshift_imm_u32(desc, dest, src0, src1, data) \
461 	APPEND_MATH_IMM_u32(RSHIFT, desc, dest, src0, src1, data)
462 
463 /* Exactly one source is IMM. Data is passed in as u64 value */
464 #define APPEND_MATH_IMM_u64(op, desc, dest, src_0, src_1, data) \
465 do { \
466 	u32 upper = (data >> 16) >> 16; \
467 	APPEND_MATH(op, desc, dest, src_0, src_1, CAAM_CMD_SZ * 2 | \
468 		    (upper ? 0 : MATH_IFB)); \
469 	if (upper) \
470 		append_u64(desc, data); \
471 	else \
472 		append_u32(desc, lower_32_bits(data)); \
473 } while (0)
474 
475 #define append_math_add_imm_u64(desc, dest, src0, src1, data) \
476 	APPEND_MATH_IMM_u64(ADD, desc, dest, src0, src1, data)
477 #define append_math_sub_imm_u64(desc, dest, src0, src1, data) \
478 	APPEND_MATH_IMM_u64(SUB, desc, dest, src0, src1, data)
479 #define append_math_add_c_imm_u64(desc, dest, src0, src1, data) \
480 	APPEND_MATH_IMM_u64(ADDC, desc, dest, src0, src1, data)
481 #define append_math_sub_b_imm_u64(desc, dest, src0, src1, data) \
482 	APPEND_MATH_IMM_u64(SUBB, desc, dest, src0, src1, data)
483 #define append_math_and_imm_u64(desc, dest, src0, src1, data) \
484 	APPEND_MATH_IMM_u64(AND, desc, dest, src0, src1, data)
485 #define append_math_or_imm_u64(desc, dest, src0, src1, data) \
486 	APPEND_MATH_IMM_u64(OR, desc, dest, src0, src1, data)
487 #define append_math_xor_imm_u64(desc, dest, src0, src1, data) \
488 	APPEND_MATH_IMM_u64(XOR, desc, dest, src0, src1, data)
489 #define append_math_lshift_imm_u64(desc, dest, src0, src1, data) \
490 	APPEND_MATH_IMM_u64(LSHIFT, desc, dest, src0, src1, data)
491 #define append_math_rshift_imm_u64(desc, dest, src0, src1, data) \
492 	APPEND_MATH_IMM_u64(RSHIFT, desc, dest, src0, src1, data)
493 
494 /**
495  * struct alginfo - Container for algorithm details
496  * @algtype: algorithm selector; for valid values, see documentation of the
497  *           functions where it is used.
498  * @keylen: length of the provided algorithm key, in bytes
499  * @keylen_pad: padded length of the provided algorithm key, in bytes
500  * @key_dma: dma (bus) address where algorithm key resides
501  * @key_virt: virtual address where algorithm key resides
502  * @key_inline: true - key can be inlined in the descriptor; false - key is
503  *              referenced by the descriptor
504  */
505 struct alginfo {
506 	u32 algtype;
507 	unsigned int keylen;
508 	unsigned int keylen_pad;
509 	dma_addr_t key_dma;
510 	const void *key_virt;
511 	bool key_inline;
512 };
513 
514 /**
515  * desc_inline_query() - Provide indications on which data items can be inlined
516  *                       and which shall be referenced in a shared descriptor.
517  * @sd_base_len: Shared descriptor base length - bytes consumed by the commands,
518  *               excluding the data items to be inlined (or corresponding
519  *               pointer if an item is not inlined). Each cnstr_* function that
520  *               generates descriptors should have a define mentioning
521  *               corresponding length.
522  * @jd_len: Maximum length of the job descriptor(s) that will be used
523  *          together with the shared descriptor.
524  * @data_len: Array of lengths of the data items trying to be inlined
525  * @inl_mask: 32bit mask with bit x = 1 if data item x can be inlined, 0
526  *            otherwise.
527  * @count: Number of data items (size of @data_len array); must be <= 32
528  *
529  * Return: 0 if data can be inlined / referenced, negative value if not. If 0,
530  *         check @inl_mask for details.
531  */
532 static inline int desc_inline_query(unsigned int sd_base_len,
533 				    unsigned int jd_len, unsigned int *data_len,
534 				    u32 *inl_mask, unsigned int count)
535 {
536 	int rem_bytes = (int)(CAAM_DESC_BYTES_MAX - sd_base_len - jd_len);
537 	unsigned int i;
538 
539 	*inl_mask = 0;
540 	for (i = 0; (i < count) && (rem_bytes > 0); i++) {
541 		if (rem_bytes - (int)(data_len[i] +
542 			(count - i - 1) * CAAM_PTR_SZ) >= 0) {
543 			rem_bytes -= data_len[i];
544 			*inl_mask |= (1 << i);
545 		} else {
546 			rem_bytes -= CAAM_PTR_SZ;
547 		}
548 	}
549 
550 	return (rem_bytes >= 0) ? 0 : -1;
551 }
552 
553 /**
554  * append_proto_dkp - Derived Key Protocol (DKP): key -> split key
555  * @desc: pointer to buffer used for descriptor construction
556  * @adata: pointer to authentication transform definitions.
557  *         keylen should be the length of initial key, while keylen_pad
558  *         the length of the derived (split) key.
559  *         Valid algorithm values - one of OP_ALG_ALGSEL_{MD5, SHA1, SHA224,
560  *         SHA256, SHA384, SHA512}.
561  */
562 static inline void append_proto_dkp(u32 * const desc, struct alginfo *adata)
563 {
564 	u32 protid;
565 
566 	/*
567 	 * Quick & dirty translation from OP_ALG_ALGSEL_{MD5, SHA*}
568 	 * to OP_PCLID_DKP_{MD5, SHA*}
569 	 */
570 	protid = (adata->algtype & OP_ALG_ALGSEL_SUBMASK) |
571 		 (0x20 << OP_ALG_ALGSEL_SHIFT);
572 
573 	if (adata->key_inline) {
574 		int words;
575 
576 		if (adata->keylen > adata->keylen_pad) {
577 			append_operation(desc, OP_TYPE_UNI_PROTOCOL | protid |
578 					 OP_PCL_DKP_SRC_PTR |
579 					 OP_PCL_DKP_DST_IMM | adata->keylen);
580 			append_ptr(desc, adata->key_dma);
581 
582 			words = (ALIGN(adata->keylen_pad, CAAM_CMD_SZ) -
583 				 CAAM_PTR_SZ) / CAAM_CMD_SZ;
584 		} else {
585 			append_operation(desc, OP_TYPE_UNI_PROTOCOL | protid |
586 					 OP_PCL_DKP_SRC_IMM |
587 					 OP_PCL_DKP_DST_IMM | adata->keylen);
588 			append_data(desc, adata->key_virt, adata->keylen);
589 
590 			words = (ALIGN(adata->keylen_pad, CAAM_CMD_SZ) -
591 				 ALIGN(adata->keylen, CAAM_CMD_SZ)) /
592 				CAAM_CMD_SZ;
593 		}
594 
595 		/* Reserve space in descriptor buffer for the derived key */
596 		if (words)
597 			(*desc) = cpu_to_caam32(caam32_to_cpu(*desc) + words);
598 	} else {
599 		append_operation(desc, OP_TYPE_UNI_PROTOCOL | protid |
600 				 OP_PCL_DKP_SRC_PTR | OP_PCL_DKP_DST_PTR |
601 				 adata->keylen);
602 		append_ptr(desc, adata->key_dma);
603 	}
604 }
605 
606 #endif /* DESC_CONSTR_H */
607