]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/crypto/caam/desc_constr.h
Merge tag 'for-f2fs-4.12' of git://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk...
[mirror_ubuntu-artful-kernel.git] / drivers / crypto / caam / desc_constr.h
1 /*
2 * caam descriptor construction helper functions
3 *
4 * Copyright 2008-2012 Freescale Semiconductor, Inc.
5 */
6
7 #ifndef DESC_CONSTR_H
8 #define DESC_CONSTR_H
9
10 #include "desc.h"
11 #include "regs.h"
12
13 #define IMMEDIATE (1 << 23)
14 #define CAAM_CMD_SZ sizeof(u32)
15 #define CAAM_PTR_SZ sizeof(dma_addr_t)
16 #define CAAM_DESC_BYTES_MAX (CAAM_CMD_SZ * MAX_CAAM_DESCSIZE)
17 #define DESC_JOB_IO_LEN (CAAM_CMD_SZ * 5 + CAAM_PTR_SZ * 3)
18
19 #ifdef DEBUG
20 #define PRINT_POS do { printk(KERN_DEBUG "%02d: %s\n", desc_len(desc),\
21 &__func__[sizeof("append")]); } while (0)
22 #else
23 #define PRINT_POS
24 #endif
25
26 #define SET_OK_NO_PROP_ERRORS (IMMEDIATE | LDST_CLASS_DECO | \
27 LDST_SRCDST_WORD_DECOCTRL | \
28 (LDOFF_CHG_SHARE_OK_NO_PROP << \
29 LDST_OFFSET_SHIFT))
30 #define DISABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \
31 LDST_SRCDST_WORD_DECOCTRL | \
32 (LDOFF_DISABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT))
33 #define ENABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \
34 LDST_SRCDST_WORD_DECOCTRL | \
35 (LDOFF_ENABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT))
36
37 extern bool caam_little_end;
38
39 static inline int desc_len(u32 * const desc)
40 {
41 return caam32_to_cpu(*desc) & HDR_DESCLEN_MASK;
42 }
43
44 static inline int desc_bytes(void * const desc)
45 {
46 return desc_len(desc) * CAAM_CMD_SZ;
47 }
48
49 static inline u32 *desc_end(u32 * const desc)
50 {
51 return desc + desc_len(desc);
52 }
53
54 static inline void *sh_desc_pdb(u32 * const desc)
55 {
56 return desc + 1;
57 }
58
59 static inline void init_desc(u32 * const desc, u32 options)
60 {
61 *desc = cpu_to_caam32((options | HDR_ONE) + 1);
62 }
63
64 static inline void init_sh_desc(u32 * const desc, u32 options)
65 {
66 PRINT_POS;
67 init_desc(desc, CMD_SHARED_DESC_HDR | options);
68 }
69
70 static inline void init_sh_desc_pdb(u32 * const desc, u32 options,
71 size_t pdb_bytes)
72 {
73 u32 pdb_len = (pdb_bytes + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
74
75 init_sh_desc(desc, (((pdb_len + 1) << HDR_START_IDX_SHIFT) + pdb_len) |
76 options);
77 }
78
79 static inline void init_job_desc(u32 * const desc, u32 options)
80 {
81 init_desc(desc, CMD_DESC_HDR | options);
82 }
83
84 static inline void init_job_desc_pdb(u32 * const desc, u32 options,
85 size_t pdb_bytes)
86 {
87 u32 pdb_len = (pdb_bytes + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
88
89 init_job_desc(desc, (((pdb_len + 1) << HDR_START_IDX_SHIFT)) | options);
90 }
91
92 static inline void append_ptr(u32 * const desc, dma_addr_t ptr)
93 {
94 dma_addr_t *offset = (dma_addr_t *)desc_end(desc);
95
96 *offset = cpu_to_caam_dma(ptr);
97
98 (*desc) = cpu_to_caam32(caam32_to_cpu(*desc) +
99 CAAM_PTR_SZ / CAAM_CMD_SZ);
100 }
101
102 static inline void init_job_desc_shared(u32 * const desc, dma_addr_t ptr,
103 int len, u32 options)
104 {
105 PRINT_POS;
106 init_job_desc(desc, HDR_SHARED | options |
107 (len << HDR_START_IDX_SHIFT));
108 append_ptr(desc, ptr);
109 }
110
111 static inline void append_data(u32 * const desc, void *data, int len)
112 {
113 u32 *offset = desc_end(desc);
114
115 if (len) /* avoid sparse warning: memcpy with byte count of 0 */
116 memcpy(offset, data, len);
117
118 (*desc) = cpu_to_caam32(caam32_to_cpu(*desc) +
119 (len + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ);
120 }
121
122 static inline void append_cmd(u32 * const desc, u32 command)
123 {
124 u32 *cmd = desc_end(desc);
125
126 *cmd = cpu_to_caam32(command);
127
128 (*desc) = cpu_to_caam32(caam32_to_cpu(*desc) + 1);
129 }
130
131 #define append_u32 append_cmd
132
133 static inline void append_u64(u32 * const desc, u64 data)
134 {
135 u32 *offset = desc_end(desc);
136
137 /* Only 32-bit alignment is guaranteed in descriptor buffer */
138 if (caam_little_end) {
139 *offset = cpu_to_caam32(lower_32_bits(data));
140 *(++offset) = cpu_to_caam32(upper_32_bits(data));
141 } else {
142 *offset = cpu_to_caam32(upper_32_bits(data));
143 *(++offset) = cpu_to_caam32(lower_32_bits(data));
144 }
145
146 (*desc) = cpu_to_caam32(caam32_to_cpu(*desc) + 2);
147 }
148
149 /* Write command without affecting header, and return pointer to next word */
150 static inline u32 *write_cmd(u32 * const desc, u32 command)
151 {
152 *desc = cpu_to_caam32(command);
153
154 return desc + 1;
155 }
156
157 static inline void append_cmd_ptr(u32 * const desc, dma_addr_t ptr, int len,
158 u32 command)
159 {
160 append_cmd(desc, command | len);
161 append_ptr(desc, ptr);
162 }
163
164 /* Write length after pointer, rather than inside command */
165 static inline void append_cmd_ptr_extlen(u32 * const desc, dma_addr_t ptr,
166 unsigned int len, u32 command)
167 {
168 append_cmd(desc, command);
169 if (!(command & (SQIN_RTO | SQIN_PRE)))
170 append_ptr(desc, ptr);
171 append_cmd(desc, len);
172 }
173
174 static inline void append_cmd_data(u32 * const desc, void *data, int len,
175 u32 command)
176 {
177 append_cmd(desc, command | IMMEDIATE | len);
178 append_data(desc, data, len);
179 }
180
181 #define APPEND_CMD_RET(cmd, op) \
182 static inline u32 *append_##cmd(u32 * const desc, u32 options) \
183 { \
184 u32 *cmd = desc_end(desc); \
185 PRINT_POS; \
186 append_cmd(desc, CMD_##op | options); \
187 return cmd; \
188 }
189 APPEND_CMD_RET(jump, JUMP)
190 APPEND_CMD_RET(move, MOVE)
191
192 static inline void set_jump_tgt_here(u32 * const desc, u32 *jump_cmd)
193 {
194 *jump_cmd = cpu_to_caam32(caam32_to_cpu(*jump_cmd) |
195 (desc_len(desc) - (jump_cmd - desc)));
196 }
197
198 static inline void set_move_tgt_here(u32 * const desc, u32 *move_cmd)
199 {
200 u32 val = caam32_to_cpu(*move_cmd);
201
202 val &= ~MOVE_OFFSET_MASK;
203 val |= (desc_len(desc) << (MOVE_OFFSET_SHIFT + 2)) & MOVE_OFFSET_MASK;
204 *move_cmd = cpu_to_caam32(val);
205 }
206
207 #define APPEND_CMD(cmd, op) \
208 static inline void append_##cmd(u32 * const desc, u32 options) \
209 { \
210 PRINT_POS; \
211 append_cmd(desc, CMD_##op | options); \
212 }
213 APPEND_CMD(operation, OPERATION)
214
215 #define APPEND_CMD_LEN(cmd, op) \
216 static inline void append_##cmd(u32 * const desc, unsigned int len, \
217 u32 options) \
218 { \
219 PRINT_POS; \
220 append_cmd(desc, CMD_##op | len | options); \
221 }
222
223 APPEND_CMD_LEN(seq_load, SEQ_LOAD)
224 APPEND_CMD_LEN(seq_store, SEQ_STORE)
225 APPEND_CMD_LEN(seq_fifo_load, SEQ_FIFO_LOAD)
226 APPEND_CMD_LEN(seq_fifo_store, SEQ_FIFO_STORE)
227
228 #define APPEND_CMD_PTR(cmd, op) \
229 static inline void append_##cmd(u32 * const desc, dma_addr_t ptr, \
230 unsigned int len, u32 options) \
231 { \
232 PRINT_POS; \
233 append_cmd_ptr(desc, ptr, len, CMD_##op | options); \
234 }
235 APPEND_CMD_PTR(key, KEY)
236 APPEND_CMD_PTR(load, LOAD)
237 APPEND_CMD_PTR(fifo_load, FIFO_LOAD)
238 APPEND_CMD_PTR(fifo_store, FIFO_STORE)
239
240 static inline void append_store(u32 * const desc, dma_addr_t ptr,
241 unsigned int len, u32 options)
242 {
243 u32 cmd_src;
244
245 cmd_src = options & LDST_SRCDST_MASK;
246
247 append_cmd(desc, CMD_STORE | options | len);
248
249 /* The following options do not require pointer */
250 if (!(cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED ||
251 cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB ||
252 cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB_WE ||
253 cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED_WE))
254 append_ptr(desc, ptr);
255 }
256
257 #define APPEND_SEQ_PTR_INTLEN(cmd, op) \
258 static inline void append_seq_##cmd##_ptr_intlen(u32 * const desc, \
259 dma_addr_t ptr, \
260 unsigned int len, \
261 u32 options) \
262 { \
263 PRINT_POS; \
264 if (options & (SQIN_RTO | SQIN_PRE)) \
265 append_cmd(desc, CMD_SEQ_##op##_PTR | len | options); \
266 else \
267 append_cmd_ptr(desc, ptr, len, CMD_SEQ_##op##_PTR | options); \
268 }
269 APPEND_SEQ_PTR_INTLEN(in, IN)
270 APPEND_SEQ_PTR_INTLEN(out, OUT)
271
272 #define APPEND_CMD_PTR_TO_IMM(cmd, op) \
273 static inline void append_##cmd##_as_imm(u32 * const desc, void *data, \
274 unsigned int len, u32 options) \
275 { \
276 PRINT_POS; \
277 append_cmd_data(desc, data, len, CMD_##op | options); \
278 }
279 APPEND_CMD_PTR_TO_IMM(load, LOAD);
280 APPEND_CMD_PTR_TO_IMM(fifo_load, FIFO_LOAD);
281
282 #define APPEND_CMD_PTR_EXTLEN(cmd, op) \
283 static inline void append_##cmd##_extlen(u32 * const desc, dma_addr_t ptr, \
284 unsigned int len, u32 options) \
285 { \
286 PRINT_POS; \
287 append_cmd_ptr_extlen(desc, ptr, len, CMD_##op | SQIN_EXT | options); \
288 }
289 APPEND_CMD_PTR_EXTLEN(seq_in_ptr, SEQ_IN_PTR)
290 APPEND_CMD_PTR_EXTLEN(seq_out_ptr, SEQ_OUT_PTR)
291
292 /*
293 * Determine whether to store length internally or externally depending on
294 * the size of its type
295 */
296 #define APPEND_CMD_PTR_LEN(cmd, op, type) \
297 static inline void append_##cmd(u32 * const desc, dma_addr_t ptr, \
298 type len, u32 options) \
299 { \
300 PRINT_POS; \
301 if (sizeof(type) > sizeof(u16)) \
302 append_##cmd##_extlen(desc, ptr, len, options); \
303 else \
304 append_##cmd##_intlen(desc, ptr, len, options); \
305 }
306 APPEND_CMD_PTR_LEN(seq_in_ptr, SEQ_IN_PTR, u32)
307 APPEND_CMD_PTR_LEN(seq_out_ptr, SEQ_OUT_PTR, u32)
308
309 /*
310 * 2nd variant for commands whose specified immediate length differs
311 * from length of immediate data provided, e.g., split keys
312 */
313 #define APPEND_CMD_PTR_TO_IMM2(cmd, op) \
314 static inline void append_##cmd##_as_imm(u32 * const desc, void *data, \
315 unsigned int data_len, \
316 unsigned int len, u32 options) \
317 { \
318 PRINT_POS; \
319 append_cmd(desc, CMD_##op | IMMEDIATE | len | options); \
320 append_data(desc, data, data_len); \
321 }
322 APPEND_CMD_PTR_TO_IMM2(key, KEY);
323
324 #define APPEND_CMD_RAW_IMM(cmd, op, type) \
325 static inline void append_##cmd##_imm_##type(u32 * const desc, type immediate, \
326 u32 options) \
327 { \
328 PRINT_POS; \
329 append_cmd(desc, CMD_##op | IMMEDIATE | options | sizeof(type)); \
330 append_cmd(desc, immediate); \
331 }
332 APPEND_CMD_RAW_IMM(load, LOAD, u32);
333
334 /*
335 * ee - endianness
336 * size - size of immediate type in bytes
337 */
338 #define APPEND_CMD_RAW_IMM2(cmd, op, ee, size) \
339 static inline void append_##cmd##_imm_##ee##size(u32 *desc, \
340 u##size immediate, \
341 u32 options) \
342 { \
343 __##ee##size data = cpu_to_##ee##size(immediate); \
344 PRINT_POS; \
345 append_cmd(desc, CMD_##op | IMMEDIATE | options | sizeof(data)); \
346 append_data(desc, &data, sizeof(data)); \
347 }
348
349 APPEND_CMD_RAW_IMM2(load, LOAD, be, 32);
350
351 /*
352 * Append math command. Only the last part of destination and source need to
353 * be specified
354 */
355 #define APPEND_MATH(op, desc, dest, src_0, src_1, len) \
356 append_cmd(desc, CMD_MATH | MATH_FUN_##op | MATH_DEST_##dest | \
357 MATH_SRC0_##src_0 | MATH_SRC1_##src_1 | (u32)len);
358
359 #define append_math_add(desc, dest, src0, src1, len) \
360 APPEND_MATH(ADD, desc, dest, src0, src1, len)
361 #define append_math_sub(desc, dest, src0, src1, len) \
362 APPEND_MATH(SUB, desc, dest, src0, src1, len)
363 #define append_math_add_c(desc, dest, src0, src1, len) \
364 APPEND_MATH(ADDC, desc, dest, src0, src1, len)
365 #define append_math_sub_b(desc, dest, src0, src1, len) \
366 APPEND_MATH(SUBB, desc, dest, src0, src1, len)
367 #define append_math_and(desc, dest, src0, src1, len) \
368 APPEND_MATH(AND, desc, dest, src0, src1, len)
369 #define append_math_or(desc, dest, src0, src1, len) \
370 APPEND_MATH(OR, desc, dest, src0, src1, len)
371 #define append_math_xor(desc, dest, src0, src1, len) \
372 APPEND_MATH(XOR, desc, dest, src0, src1, len)
373 #define append_math_lshift(desc, dest, src0, src1, len) \
374 APPEND_MATH(LSHIFT, desc, dest, src0, src1, len)
375 #define append_math_rshift(desc, dest, src0, src1, len) \
376 APPEND_MATH(RSHIFT, desc, dest, src0, src1, len)
377 #define append_math_ldshift(desc, dest, src0, src1, len) \
378 APPEND_MATH(SHLD, desc, dest, src0, src1, len)
379
380 /* Exactly one source is IMM. Data is passed in as u32 value */
381 #define APPEND_MATH_IMM_u32(op, desc, dest, src_0, src_1, data) \
382 do { \
383 APPEND_MATH(op, desc, dest, src_0, src_1, CAAM_CMD_SZ); \
384 append_cmd(desc, data); \
385 } while (0)
386
387 #define append_math_add_imm_u32(desc, dest, src0, src1, data) \
388 APPEND_MATH_IMM_u32(ADD, desc, dest, src0, src1, data)
389 #define append_math_sub_imm_u32(desc, dest, src0, src1, data) \
390 APPEND_MATH_IMM_u32(SUB, desc, dest, src0, src1, data)
391 #define append_math_add_c_imm_u32(desc, dest, src0, src1, data) \
392 APPEND_MATH_IMM_u32(ADDC, desc, dest, src0, src1, data)
393 #define append_math_sub_b_imm_u32(desc, dest, src0, src1, data) \
394 APPEND_MATH_IMM_u32(SUBB, desc, dest, src0, src1, data)
395 #define append_math_and_imm_u32(desc, dest, src0, src1, data) \
396 APPEND_MATH_IMM_u32(AND, desc, dest, src0, src1, data)
397 #define append_math_or_imm_u32(desc, dest, src0, src1, data) \
398 APPEND_MATH_IMM_u32(OR, desc, dest, src0, src1, data)
399 #define append_math_xor_imm_u32(desc, dest, src0, src1, data) \
400 APPEND_MATH_IMM_u32(XOR, desc, dest, src0, src1, data)
401 #define append_math_lshift_imm_u32(desc, dest, src0, src1, data) \
402 APPEND_MATH_IMM_u32(LSHIFT, desc, dest, src0, src1, data)
403 #define append_math_rshift_imm_u32(desc, dest, src0, src1, data) \
404 APPEND_MATH_IMM_u32(RSHIFT, desc, dest, src0, src1, data)
405
406 /* Exactly one source is IMM. Data is passed in as u64 value */
407 #define APPEND_MATH_IMM_u64(op, desc, dest, src_0, src_1, data) \
408 do { \
409 u32 upper = (data >> 16) >> 16; \
410 APPEND_MATH(op, desc, dest, src_0, src_1, CAAM_CMD_SZ * 2 | \
411 (upper ? 0 : MATH_IFB)); \
412 if (upper) \
413 append_u64(desc, data); \
414 else \
415 append_u32(desc, lower_32_bits(data)); \
416 } while (0)
417
418 #define append_math_add_imm_u64(desc, dest, src0, src1, data) \
419 APPEND_MATH_IMM_u64(ADD, desc, dest, src0, src1, data)
420 #define append_math_sub_imm_u64(desc, dest, src0, src1, data) \
421 APPEND_MATH_IMM_u64(SUB, desc, dest, src0, src1, data)
422 #define append_math_add_c_imm_u64(desc, dest, src0, src1, data) \
423 APPEND_MATH_IMM_u64(ADDC, desc, dest, src0, src1, data)
424 #define append_math_sub_b_imm_u64(desc, dest, src0, src1, data) \
425 APPEND_MATH_IMM_u64(SUBB, desc, dest, src0, src1, data)
426 #define append_math_and_imm_u64(desc, dest, src0, src1, data) \
427 APPEND_MATH_IMM_u64(AND, desc, dest, src0, src1, data)
428 #define append_math_or_imm_u64(desc, dest, src0, src1, data) \
429 APPEND_MATH_IMM_u64(OR, desc, dest, src0, src1, data)
430 #define append_math_xor_imm_u64(desc, dest, src0, src1, data) \
431 APPEND_MATH_IMM_u64(XOR, desc, dest, src0, src1, data)
432 #define append_math_lshift_imm_u64(desc, dest, src0, src1, data) \
433 APPEND_MATH_IMM_u64(LSHIFT, desc, dest, src0, src1, data)
434 #define append_math_rshift_imm_u64(desc, dest, src0, src1, data) \
435 APPEND_MATH_IMM_u64(RSHIFT, desc, dest, src0, src1, data)
436
437 /**
438 * struct alginfo - Container for algorithm details
439 * @algtype: algorithm selector; for valid values, see documentation of the
440 * functions where it is used.
441 * @keylen: length of the provided algorithm key, in bytes
442 * @keylen_pad: padded length of the provided algorithm key, in bytes
443 * @key: address where algorithm key resides; virtual address if key_inline
444 * is true, dma (bus) address if key_inline is false.
445 * @key_inline: true - key can be inlined in the descriptor; false - key is
446 * referenced by the descriptor
447 */
448 struct alginfo {
449 u32 algtype;
450 unsigned int keylen;
451 unsigned int keylen_pad;
452 union {
453 dma_addr_t key_dma;
454 void *key_virt;
455 };
456 bool key_inline;
457 };
458
459 /**
460 * desc_inline_query() - Provide indications on which data items can be inlined
461 * and which shall be referenced in a shared descriptor.
462 * @sd_base_len: Shared descriptor base length - bytes consumed by the commands,
463 * excluding the data items to be inlined (or corresponding
464 * pointer if an item is not inlined). Each cnstr_* function that
465 * generates descriptors should have a define mentioning
466 * corresponding length.
467 * @jd_len: Maximum length of the job descriptor(s) that will be used
468 * together with the shared descriptor.
469 * @data_len: Array of lengths of the data items trying to be inlined
470 * @inl_mask: 32bit mask with bit x = 1 if data item x can be inlined, 0
471 * otherwise.
472 * @count: Number of data items (size of @data_len array); must be <= 32
473 *
474 * Return: 0 if data can be inlined / referenced, negative value if not. If 0,
475 * check @inl_mask for details.
476 */
477 static inline int desc_inline_query(unsigned int sd_base_len,
478 unsigned int jd_len, unsigned int *data_len,
479 u32 *inl_mask, unsigned int count)
480 {
481 int rem_bytes = (int)(CAAM_DESC_BYTES_MAX - sd_base_len - jd_len);
482 unsigned int i;
483
484 *inl_mask = 0;
485 for (i = 0; (i < count) && (rem_bytes > 0); i++) {
486 if (rem_bytes - (int)(data_len[i] +
487 (count - i - 1) * CAAM_PTR_SZ) >= 0) {
488 rem_bytes -= data_len[i];
489 *inl_mask |= (1 << i);
490 } else {
491 rem_bytes -= CAAM_PTR_SZ;
492 }
493 }
494
495 return (rem_bytes >= 0) ? 0 : -1;
496 }
497
498 #endif /* DESC_CONSTR_H */