crypto: caam - fix printk recursion for long error texts
[linux-2.6.git] / drivers / crypto / caam / desc_constr.h
1 /*
2  * caam descriptor construction helper functions
3  *
4  * Copyright 2008-2011 Freescale Semiconductor, Inc.
5  */
6
7 #include "desc.h"
8
9 #define IMMEDIATE (1 << 23)
10 #define CAAM_CMD_SZ sizeof(u32)
11 #define CAAM_PTR_SZ sizeof(dma_addr_t)
12
13 #ifdef DEBUG
14 #define PRINT_POS do { printk(KERN_DEBUG "%02d: %s\n", desc_len(desc),\
15                               &__func__[sizeof("append")]); } while (0)
16 #else
17 #define PRINT_POS
18 #endif
19
20 #define DISABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \
21                                 LDST_SRCDST_WORD_DECOCTRL | \
22                                 (LDOFF_DISABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT))
23 #define ENABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \
24                                LDST_SRCDST_WORD_DECOCTRL | \
25                                (LDOFF_ENABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT))
26
27 static inline int desc_len(u32 *desc)
28 {
29         return *desc & HDR_DESCLEN_MASK;
30 }
31
32 static inline int desc_bytes(void *desc)
33 {
34         return desc_len(desc) * CAAM_CMD_SZ;
35 }
36
37 static inline u32 *desc_end(u32 *desc)
38 {
39         return desc + desc_len(desc);
40 }
41
42 static inline void *sh_desc_pdb(u32 *desc)
43 {
44         return desc + 1;
45 }
46
47 static inline void init_desc(u32 *desc, u32 options)
48 {
49         *desc = options | HDR_ONE | 1;
50 }
51
52 static inline void init_sh_desc(u32 *desc, u32 options)
53 {
54         PRINT_POS;
55         init_desc(desc, CMD_SHARED_DESC_HDR | options);
56 }
57
58 static inline void init_sh_desc_pdb(u32 *desc, u32 options, size_t pdb_bytes)
59 {
60         u32 pdb_len = pdb_bytes / CAAM_CMD_SZ + 1;
61
62         init_sh_desc(desc, ((pdb_len << HDR_START_IDX_SHIFT) + pdb_len) |
63                      options);
64 }
65
66 static inline void init_job_desc(u32 *desc, u32 options)
67 {
68         init_desc(desc, CMD_DESC_HDR | options);
69 }
70
71 static inline void append_ptr(u32 *desc, dma_addr_t ptr)
72 {
73         dma_addr_t *offset = (dma_addr_t *)desc_end(desc);
74
75         *offset = ptr;
76
77         (*desc) += CAAM_PTR_SZ / CAAM_CMD_SZ;
78 }
79
80 static inline void init_job_desc_shared(u32 *desc, dma_addr_t ptr, int len,
81                                         u32 options)
82 {
83         PRINT_POS;
84         init_job_desc(desc, HDR_SHARED | options |
85                       (len << HDR_START_IDX_SHIFT));
86         append_ptr(desc, ptr);
87 }
88
89 static inline void append_data(u32 *desc, void *data, int len)
90 {
91         u32 *offset = desc_end(desc);
92
93         if (len) /* avoid sparse warning: memcpy with byte count of 0 */
94                 memcpy(offset, data, len);
95
96         (*desc) += (len + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
97 }
98
99 static inline void append_cmd(u32 *desc, u32 command)
100 {
101         u32 *cmd = desc_end(desc);
102
103         *cmd = command;
104
105         (*desc)++;
106 }
107
108 static inline void append_cmd_ptr(u32 *desc, dma_addr_t ptr, int len,
109                                   u32 command)
110 {
111         append_cmd(desc, command | len);
112         append_ptr(desc, ptr);
113 }
114
115 static inline void append_cmd_data(u32 *desc, void *data, int len,
116                                    u32 command)
117 {
118         append_cmd(desc, command | IMMEDIATE | len);
119         append_data(desc, data, len);
120 }
121
122 static inline u32 *append_jump(u32 *desc, u32 options)
123 {
124         u32 *cmd = desc_end(desc);
125
126         PRINT_POS;
127         append_cmd(desc, CMD_JUMP | options);
128
129         return cmd;
130 }
131
132 static inline void set_jump_tgt_here(u32 *desc, u32 *jump_cmd)
133 {
134         *jump_cmd = *jump_cmd | (desc_len(desc) - (jump_cmd - desc));
135 }
136
137 #define APPEND_CMD(cmd, op) \
138 static inline void append_##cmd(u32 *desc, u32 options) \
139 { \
140         PRINT_POS; \
141         append_cmd(desc, CMD_##op | options); \
142 }
143 APPEND_CMD(operation, OPERATION)
144 APPEND_CMD(move, MOVE)
145
146 #define APPEND_CMD_LEN(cmd, op) \
147 static inline void append_##cmd(u32 *desc, unsigned int len, u32 options) \
148 { \
149         PRINT_POS; \
150         append_cmd(desc, CMD_##op | len | options); \
151 }
152 APPEND_CMD_LEN(seq_store, SEQ_STORE)
153 APPEND_CMD_LEN(seq_fifo_load, SEQ_FIFO_LOAD)
154 APPEND_CMD_LEN(seq_fifo_store, SEQ_FIFO_STORE)
155
156 #define APPEND_CMD_PTR(cmd, op) \
157 static inline void append_##cmd(u32 *desc, dma_addr_t ptr, unsigned int len, \
158                                 u32 options) \
159 { \
160         PRINT_POS; \
161         append_cmd_ptr(desc, ptr, len, CMD_##op | options); \
162 }
163 APPEND_CMD_PTR(key, KEY)
164 APPEND_CMD_PTR(seq_in_ptr, SEQ_IN_PTR)
165 APPEND_CMD_PTR(seq_out_ptr, SEQ_OUT_PTR)
166 APPEND_CMD_PTR(load, LOAD)
167 APPEND_CMD_PTR(store, STORE)
168 APPEND_CMD_PTR(fifo_load, FIFO_LOAD)
169 APPEND_CMD_PTR(fifo_store, FIFO_STORE)
170
171 #define APPEND_CMD_PTR_TO_IMM(cmd, op) \
172 static inline void append_##cmd##_as_imm(u32 *desc, void *data, \
173                                          unsigned int len, u32 options) \
174 { \
175         PRINT_POS; \
176         append_cmd_data(desc, data, len, CMD_##op | options); \
177 }
178 APPEND_CMD_PTR_TO_IMM(load, LOAD);
179 APPEND_CMD_PTR_TO_IMM(fifo_load, FIFO_LOAD);
180
181 /*
182  * 2nd variant for commands whose specified immediate length differs
183  * from length of immediate data provided, e.g., split keys
184  */
185 #define APPEND_CMD_PTR_TO_IMM2(cmd, op) \
186 static inline void append_##cmd##_as_imm(u32 *desc, void *data, \
187                                          unsigned int data_len, \
188                                          unsigned int len, u32 options) \
189 { \
190         PRINT_POS; \
191         append_cmd(desc, CMD_##op | IMMEDIATE | len | options); \
192         append_data(desc, data, data_len); \
193 }
194 APPEND_CMD_PTR_TO_IMM2(key, KEY);
195
196 #define APPEND_CMD_RAW_IMM(cmd, op, type) \
197 static inline void append_##cmd##_imm_##type(u32 *desc, type immediate, \
198                                              u32 options) \
199 { \
200         PRINT_POS; \
201         append_cmd(desc, CMD_##op | IMMEDIATE | options | sizeof(type)); \
202         append_cmd(desc, immediate); \
203 }
204 APPEND_CMD_RAW_IMM(load, LOAD, u32);