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