blob: 339277c7acb9b6b623c17b7fa46ad551ab145721 [file] [log] [blame]
Kim Phillips8e8ec592011-03-13 16:54:26 +08001/*
2 * caam - Freescale FSL CAAM support for crypto API
3 *
4 * Copyright 2008-2011 Freescale Semiconductor, Inc.
5 *
6 * Based on talitos crypto API driver.
7 *
8 * relationship of job descriptors to shared descriptors (SteveC Dec 10 2008):
9 *
10 * --------------- ---------------
11 * | JobDesc #1 |-------------------->| ShareDesc |
12 * | *(packet 1) | | (PDB) |
13 * --------------- |------------->| (hashKey) |
14 * . | | (cipherKey) |
15 * . | |-------->| (operation) |
16 * --------------- | | ---------------
17 * | JobDesc #2 |------| |
18 * | *(packet 2) | |
19 * --------------- |
20 * . |
21 * . |
22 * --------------- |
23 * | JobDesc #3 |------------
24 * | *(packet 3) |
25 * ---------------
26 *
27 * The SharedDesc never changes for a connection unless rekeyed, but
28 * each packet will likely be in a different place. So all we need
29 * to know to process the packet is where the input is, where the
30 * output goes, and what context we want to process with. Context is
31 * in the SharedDesc, packet references in the JobDesc.
32 *
33 * So, a job desc looks like:
34 *
35 * ---------------------
36 * | Header |
37 * | ShareDesc Pointer |
38 * | SEQ_OUT_PTR |
39 * | (output buffer) |
Yuan Kang6ec47332012-06-22 19:48:43 -050040 * | (output length) |
Kim Phillips8e8ec592011-03-13 16:54:26 +080041 * | SEQ_IN_PTR |
42 * | (input buffer) |
Yuan Kang6ec47332012-06-22 19:48:43 -050043 * | (input length) |
Kim Phillips8e8ec592011-03-13 16:54:26 +080044 * ---------------------
45 */
46
47#include "compat.h"
48
49#include "regs.h"
50#include "intern.h"
51#include "desc_constr.h"
52#include "jr.h"
53#include "error.h"
Yuan Kanga299c832012-06-22 19:48:46 -050054#include "sg_sw_sec4.h"
Yuan Kang4c1ec1f2012-06-22 19:48:45 -050055#include "key_gen.h"
Kim Phillips8e8ec592011-03-13 16:54:26 +080056
57/*
58 * crypto alg
59 */
60#define CAAM_CRA_PRIORITY 3000
61/* max key is sum of AES_MAX_KEY_SIZE, max split key size */
62#define CAAM_MAX_KEY_SIZE (AES_MAX_KEY_SIZE + \
63 SHA512_DIGEST_SIZE * 2)
64/* max IV is max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
65#define CAAM_MAX_IV_LENGTH 16
66
Kim Phillips4427b1b2011-05-14 22:08:17 -050067/* length of descriptors text */
Yuan Kang1acebad32011-07-15 11:21:42 +080068#define DESC_AEAD_BASE (4 * CAAM_CMD_SZ)
Horia Geanta4464a7d2014-03-14 17:46:49 +020069#define DESC_AEAD_ENC_LEN (DESC_AEAD_BASE + 15 * CAAM_CMD_SZ)
70#define DESC_AEAD_DEC_LEN (DESC_AEAD_BASE + 18 * CAAM_CMD_SZ)
Yuan Kang1acebad32011-07-15 11:21:42 +080071#define DESC_AEAD_GIVENC_LEN (DESC_AEAD_ENC_LEN + 7 * CAAM_CMD_SZ)
72
Horia Geantaae4a8252014-03-14 17:46:52 +020073#define DESC_AEAD_NULL_BASE (3 * CAAM_CMD_SZ)
74#define DESC_AEAD_NULL_ENC_LEN (DESC_AEAD_NULL_BASE + 14 * CAAM_CMD_SZ)
75#define DESC_AEAD_NULL_DEC_LEN (DESC_AEAD_NULL_BASE + 17 * CAAM_CMD_SZ)
76
Yuan Kangacdca312011-07-15 11:21:42 +080077#define DESC_ABLKCIPHER_BASE (3 * CAAM_CMD_SZ)
78#define DESC_ABLKCIPHER_ENC_LEN (DESC_ABLKCIPHER_BASE + \
79 20 * CAAM_CMD_SZ)
80#define DESC_ABLKCIPHER_DEC_LEN (DESC_ABLKCIPHER_BASE + \
81 15 * CAAM_CMD_SZ)
82
Yuan Kang1acebad32011-07-15 11:21:42 +080083#define DESC_MAX_USED_BYTES (DESC_AEAD_GIVENC_LEN + \
84 CAAM_MAX_KEY_SIZE)
85#define DESC_MAX_USED_LEN (DESC_MAX_USED_BYTES / CAAM_CMD_SZ)
Kim Phillips4427b1b2011-05-14 22:08:17 -050086
Kim Phillips8e8ec592011-03-13 16:54:26 +080087#ifdef DEBUG
88/* for print_hex_dumps with line references */
Kim Phillips8e8ec592011-03-13 16:54:26 +080089#define debug(format, arg...) printk(format, arg)
90#else
91#define debug(format, arg...)
92#endif
Ruchika Guptacfc6f112013-10-25 12:01:03 +053093static struct list_head alg_list;
Kim Phillips8e8ec592011-03-13 16:54:26 +080094
Yuan Kang1acebad32011-07-15 11:21:42 +080095/* Set DK bit in class 1 operation if shared */
96static inline void append_dec_op1(u32 *desc, u32 type)
97{
98 u32 *jump_cmd, *uncond_jump_cmd;
99
100 jump_cmd = append_jump(desc, JUMP_TEST_ALL | JUMP_COND_SHRD);
101 append_operation(desc, type | OP_ALG_AS_INITFINAL |
102 OP_ALG_DECRYPT);
103 uncond_jump_cmd = append_jump(desc, JUMP_TEST_ALL);
104 set_jump_tgt_here(desc, jump_cmd);
105 append_operation(desc, type | OP_ALG_AS_INITFINAL |
106 OP_ALG_DECRYPT | OP_ALG_AAI_DK);
107 set_jump_tgt_here(desc, uncond_jump_cmd);
108}
109
110/*
Yuan Kang1acebad32011-07-15 11:21:42 +0800111 * For aead functions, read payload and write payload,
112 * both of which are specified in req->src and req->dst
113 */
114static inline void aead_append_src_dst(u32 *desc, u32 msg_type)
115{
Horia Geantaae4a8252014-03-14 17:46:52 +0200116 append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | KEY_VLF);
Yuan Kang1acebad32011-07-15 11:21:42 +0800117 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_BOTH |
118 KEY_VLF | msg_type | FIFOLD_TYPE_LASTBOTH);
Yuan Kang1acebad32011-07-15 11:21:42 +0800119}
120
121/*
122 * For aead encrypt and decrypt, read iv for both classes
123 */
124static inline void aead_append_ld_iv(u32 *desc, int ivsize)
125{
126 append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT |
127 LDST_CLASS_1_CCB | ivsize);
128 append_move(desc, MOVE_SRC_CLASS1CTX | MOVE_DEST_CLASS2INFIFO | ivsize);
129}
130
131/*
Yuan Kangacdca312011-07-15 11:21:42 +0800132 * For ablkcipher encrypt and decrypt, read from req->src and
133 * write to req->dst
134 */
135static inline void ablkcipher_append_src_dst(u32 *desc)
136{
Kim Phillips70d793c2012-06-22 19:42:35 -0500137 append_math_add(desc, VARSEQOUTLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
138 append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
139 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 |
140 KEY_VLF | FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST1);
141 append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | KEY_VLF);
Yuan Kangacdca312011-07-15 11:21:42 +0800142}
143
144/*
Yuan Kang1acebad32011-07-15 11:21:42 +0800145 * If all data, including src (with assoc and iv) or dst (with iv only) are
146 * contiguous
147 */
148#define GIV_SRC_CONTIG 1
149#define GIV_DST_CONTIG (1 << 1)
150
Kim Phillips8e8ec592011-03-13 16:54:26 +0800151/*
152 * per-session context
153 */
154struct caam_ctx {
155 struct device *jrdev;
Yuan Kang1acebad32011-07-15 11:21:42 +0800156 u32 sh_desc_enc[DESC_MAX_USED_LEN];
157 u32 sh_desc_dec[DESC_MAX_USED_LEN];
158 u32 sh_desc_givenc[DESC_MAX_USED_LEN];
159 dma_addr_t sh_desc_enc_dma;
160 dma_addr_t sh_desc_dec_dma;
161 dma_addr_t sh_desc_givenc_dma;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800162 u32 class1_alg_type;
163 u32 class2_alg_type;
164 u32 alg_op;
Yuan Kang1acebad32011-07-15 11:21:42 +0800165 u8 key[CAAM_MAX_KEY_SIZE];
Yuan Kang885e9e22011-07-15 11:21:41 +0800166 dma_addr_t key_dma;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800167 unsigned int enckeylen;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800168 unsigned int split_key_len;
169 unsigned int split_key_pad_len;
170 unsigned int authsize;
171};
172
Yuan Kang1acebad32011-07-15 11:21:42 +0800173static void append_key_aead(u32 *desc, struct caam_ctx *ctx,
174 int keys_fit_inline)
175{
176 if (keys_fit_inline) {
177 append_key_as_imm(desc, ctx->key, ctx->split_key_pad_len,
178 ctx->split_key_len, CLASS_2 |
179 KEY_DEST_MDHA_SPLIT | KEY_ENC);
180 append_key_as_imm(desc, (void *)ctx->key +
181 ctx->split_key_pad_len, ctx->enckeylen,
182 ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
183 } else {
184 append_key(desc, ctx->key_dma, ctx->split_key_len, CLASS_2 |
185 KEY_DEST_MDHA_SPLIT | KEY_ENC);
186 append_key(desc, ctx->key_dma + ctx->split_key_pad_len,
187 ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
188 }
189}
190
191static void init_sh_desc_key_aead(u32 *desc, struct caam_ctx *ctx,
192 int keys_fit_inline)
193{
194 u32 *key_jump_cmd;
195
Kim Phillips61bb86b2012-07-13 17:49:28 -0500196 init_sh_desc(desc, HDR_SHARE_SERIAL);
Yuan Kang1acebad32011-07-15 11:21:42 +0800197
198 /* Skip if already shared */
199 key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
200 JUMP_COND_SHRD);
201
202 append_key_aead(desc, ctx, keys_fit_inline);
203
204 set_jump_tgt_here(desc, key_jump_cmd);
Yuan Kang1acebad32011-07-15 11:21:42 +0800205}
206
Horia Geantaae4a8252014-03-14 17:46:52 +0200207static int aead_null_set_sh_desc(struct crypto_aead *aead)
208{
209 struct aead_tfm *tfm = &aead->base.crt_aead;
210 struct caam_ctx *ctx = crypto_aead_ctx(aead);
211 struct device *jrdev = ctx->jrdev;
212 bool keys_fit_inline = false;
213 u32 *key_jump_cmd, *jump_cmd, *read_move_cmd, *write_move_cmd;
214 u32 *desc;
215
216 /*
217 * Job Descriptor and Shared Descriptors
218 * must all fit into the 64-word Descriptor h/w Buffer
219 */
220 if (DESC_AEAD_NULL_ENC_LEN + DESC_JOB_IO_LEN +
221 ctx->split_key_pad_len <= CAAM_DESC_BYTES_MAX)
222 keys_fit_inline = true;
223
224 /* aead_encrypt shared descriptor */
225 desc = ctx->sh_desc_enc;
226
227 init_sh_desc(desc, HDR_SHARE_SERIAL);
228
229 /* Skip if already shared */
230 key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
231 JUMP_COND_SHRD);
232 if (keys_fit_inline)
233 append_key_as_imm(desc, ctx->key, ctx->split_key_pad_len,
234 ctx->split_key_len, CLASS_2 |
235 KEY_DEST_MDHA_SPLIT | KEY_ENC);
236 else
237 append_key(desc, ctx->key_dma, ctx->split_key_len, CLASS_2 |
238 KEY_DEST_MDHA_SPLIT | KEY_ENC);
239 set_jump_tgt_here(desc, key_jump_cmd);
240
241 /* cryptlen = seqoutlen - authsize */
242 append_math_sub_imm_u32(desc, REG3, SEQOUTLEN, IMM, ctx->authsize);
243
244 /*
245 * NULL encryption; IV is zero
246 * assoclen = (assoclen + cryptlen) - cryptlen
247 */
248 append_math_sub(desc, VARSEQINLEN, SEQINLEN, REG3, CAAM_CMD_SZ);
249
250 /* read assoc before reading payload */
251 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG |
252 KEY_VLF);
253
254 /* Prepare to read and write cryptlen bytes */
255 append_math_add(desc, VARSEQINLEN, ZERO, REG3, CAAM_CMD_SZ);
256 append_math_add(desc, VARSEQOUTLEN, ZERO, REG3, CAAM_CMD_SZ);
257
258 /*
259 * MOVE_LEN opcode is not available in all SEC HW revisions,
260 * thus need to do some magic, i.e. self-patch the descriptor
261 * buffer.
262 */
263 read_move_cmd = append_move(desc, MOVE_SRC_DESCBUF |
264 MOVE_DEST_MATH3 |
265 (0x6 << MOVE_LEN_SHIFT));
266 write_move_cmd = append_move(desc, MOVE_SRC_MATH3 |
267 MOVE_DEST_DESCBUF |
268 MOVE_WAITCOMP |
269 (0x8 << MOVE_LEN_SHIFT));
270
271 /* Class 2 operation */
272 append_operation(desc, ctx->class2_alg_type |
273 OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
274
275 /* Read and write cryptlen bytes */
276 aead_append_src_dst(desc, FIFOLD_TYPE_MSG | FIFOLD_TYPE_FLUSH1);
277
278 set_move_tgt_here(desc, read_move_cmd);
279 set_move_tgt_here(desc, write_move_cmd);
280 append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);
281 append_move(desc, MOVE_SRC_INFIFO_CL | MOVE_DEST_OUTFIFO |
282 MOVE_AUX_LS);
283
284 /* Write ICV */
285 append_seq_store(desc, ctx->authsize, LDST_CLASS_2_CCB |
286 LDST_SRCDST_BYTE_CONTEXT);
287
288 ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc,
289 desc_bytes(desc),
290 DMA_TO_DEVICE);
291 if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
292 dev_err(jrdev, "unable to map shared descriptor\n");
293 return -ENOMEM;
294 }
295#ifdef DEBUG
296 print_hex_dump(KERN_ERR,
297 "aead null enc shdesc@"__stringify(__LINE__)": ",
298 DUMP_PREFIX_ADDRESS, 16, 4, desc,
299 desc_bytes(desc), 1);
300#endif
301
302 /*
303 * Job Descriptor and Shared Descriptors
304 * must all fit into the 64-word Descriptor h/w Buffer
305 */
306 if (DESC_AEAD_NULL_DEC_LEN + DESC_JOB_IO_LEN +
307 ctx->split_key_pad_len <= CAAM_DESC_BYTES_MAX)
308 keys_fit_inline = true;
309
310 desc = ctx->sh_desc_dec;
311
312 /* aead_decrypt shared descriptor */
313 init_sh_desc(desc, HDR_SHARE_SERIAL);
314
315 /* Skip if already shared */
316 key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
317 JUMP_COND_SHRD);
318 if (keys_fit_inline)
319 append_key_as_imm(desc, ctx->key, ctx->split_key_pad_len,
320 ctx->split_key_len, CLASS_2 |
321 KEY_DEST_MDHA_SPLIT | KEY_ENC);
322 else
323 append_key(desc, ctx->key_dma, ctx->split_key_len, CLASS_2 |
324 KEY_DEST_MDHA_SPLIT | KEY_ENC);
325 set_jump_tgt_here(desc, key_jump_cmd);
326
327 /* Class 2 operation */
328 append_operation(desc, ctx->class2_alg_type |
329 OP_ALG_AS_INITFINAL | OP_ALG_DECRYPT | OP_ALG_ICV_ON);
330
331 /* assoclen + cryptlen = seqinlen - ivsize - authsize */
332 append_math_sub_imm_u32(desc, REG3, SEQINLEN, IMM,
333 ctx->authsize + tfm->ivsize);
334 /* assoclen = (assoclen + cryptlen) - cryptlen */
335 append_math_sub(desc, REG2, SEQOUTLEN, REG0, CAAM_CMD_SZ);
336 append_math_sub(desc, VARSEQINLEN, REG3, REG2, CAAM_CMD_SZ);
337
338 /* read assoc before reading payload */
339 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG |
340 KEY_VLF);
341
342 /* Prepare to read and write cryptlen bytes */
343 append_math_add(desc, VARSEQINLEN, ZERO, REG2, CAAM_CMD_SZ);
344 append_math_add(desc, VARSEQOUTLEN, ZERO, REG2, CAAM_CMD_SZ);
345
346 /*
347 * MOVE_LEN opcode is not available in all SEC HW revisions,
348 * thus need to do some magic, i.e. self-patch the descriptor
349 * buffer.
350 */
351 read_move_cmd = append_move(desc, MOVE_SRC_DESCBUF |
352 MOVE_DEST_MATH2 |
353 (0x6 << MOVE_LEN_SHIFT));
354 write_move_cmd = append_move(desc, MOVE_SRC_MATH2 |
355 MOVE_DEST_DESCBUF |
356 MOVE_WAITCOMP |
357 (0x8 << MOVE_LEN_SHIFT));
358
359 /* Read and write cryptlen bytes */
360 aead_append_src_dst(desc, FIFOLD_TYPE_MSG | FIFOLD_TYPE_FLUSH1);
361
362 /*
363 * Insert a NOP here, since we need at least 4 instructions between
364 * code patching the descriptor buffer and the location being patched.
365 */
366 jump_cmd = append_jump(desc, JUMP_TEST_ALL);
367 set_jump_tgt_here(desc, jump_cmd);
368
369 set_move_tgt_here(desc, read_move_cmd);
370 set_move_tgt_here(desc, write_move_cmd);
371 append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);
372 append_move(desc, MOVE_SRC_INFIFO_CL | MOVE_DEST_OUTFIFO |
373 MOVE_AUX_LS);
374 append_cmd(desc, CMD_LOAD | ENABLE_AUTO_INFO_FIFO);
375
376 /* Load ICV */
377 append_seq_fifo_load(desc, ctx->authsize, FIFOLD_CLASS_CLASS2 |
378 FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_ICV);
379
380 ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc,
381 desc_bytes(desc),
382 DMA_TO_DEVICE);
383 if (dma_mapping_error(jrdev, ctx->sh_desc_dec_dma)) {
384 dev_err(jrdev, "unable to map shared descriptor\n");
385 return -ENOMEM;
386 }
387#ifdef DEBUG
388 print_hex_dump(KERN_ERR,
389 "aead null dec shdesc@"__stringify(__LINE__)": ",
390 DUMP_PREFIX_ADDRESS, 16, 4, desc,
391 desc_bytes(desc), 1);
392#endif
393
394 return 0;
395}
396
Yuan Kang1acebad32011-07-15 11:21:42 +0800397static int aead_set_sh_desc(struct crypto_aead *aead)
398{
399 struct aead_tfm *tfm = &aead->base.crt_aead;
400 struct caam_ctx *ctx = crypto_aead_ctx(aead);
401 struct device *jrdev = ctx->jrdev;
Kim Phillips2af8f4a2012-09-07 04:17:03 +0800402 bool keys_fit_inline = false;
Yuan Kang1acebad32011-07-15 11:21:42 +0800403 u32 geniv, moveiv;
404 u32 *desc;
405
Horia Geantaae4a8252014-03-14 17:46:52 +0200406 if (!ctx->authsize)
Yuan Kang1acebad32011-07-15 11:21:42 +0800407 return 0;
408
Horia Geantaae4a8252014-03-14 17:46:52 +0200409 /* NULL encryption / decryption */
410 if (!ctx->enckeylen)
411 return aead_null_set_sh_desc(aead);
412
Yuan Kang1acebad32011-07-15 11:21:42 +0800413 /*
414 * Job Descriptor and Shared Descriptors
415 * must all fit into the 64-word Descriptor h/w Buffer
416 */
417 if (DESC_AEAD_ENC_LEN + DESC_JOB_IO_LEN +
418 ctx->split_key_pad_len + ctx->enckeylen <=
419 CAAM_DESC_BYTES_MAX)
Kim Phillips2af8f4a2012-09-07 04:17:03 +0800420 keys_fit_inline = true;
Yuan Kang1acebad32011-07-15 11:21:42 +0800421
422 /* aead_encrypt shared descriptor */
423 desc = ctx->sh_desc_enc;
424
425 init_sh_desc_key_aead(desc, ctx, keys_fit_inline);
426
427 /* Class 2 operation */
428 append_operation(desc, ctx->class2_alg_type |
429 OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
430
431 /* cryptlen = seqoutlen - authsize */
432 append_math_sub_imm_u32(desc, REG3, SEQOUTLEN, IMM, ctx->authsize);
433
434 /* assoclen + cryptlen = seqinlen - ivsize */
435 append_math_sub_imm_u32(desc, REG2, SEQINLEN, IMM, tfm->ivsize);
436
Horia Geanta4464a7d2014-03-14 17:46:49 +0200437 /* assoclen = (assoclen + cryptlen) - cryptlen */
Yuan Kang1acebad32011-07-15 11:21:42 +0800438 append_math_sub(desc, VARSEQINLEN, REG2, REG3, CAAM_CMD_SZ);
439
440 /* read assoc before reading payload */
441 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG |
442 KEY_VLF);
443 aead_append_ld_iv(desc, tfm->ivsize);
444
445 /* Class 1 operation */
446 append_operation(desc, ctx->class1_alg_type |
447 OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
448
449 /* Read and write cryptlen bytes */
450 append_math_add(desc, VARSEQINLEN, ZERO, REG3, CAAM_CMD_SZ);
451 append_math_add(desc, VARSEQOUTLEN, ZERO, REG3, CAAM_CMD_SZ);
452 aead_append_src_dst(desc, FIFOLD_TYPE_MSG1OUT2);
453
454 /* Write ICV */
455 append_seq_store(desc, ctx->authsize, LDST_CLASS_2_CCB |
456 LDST_SRCDST_BYTE_CONTEXT);
457
458 ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc,
459 desc_bytes(desc),
460 DMA_TO_DEVICE);
461 if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
462 dev_err(jrdev, "unable to map shared descriptor\n");
463 return -ENOMEM;
464 }
465#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +0300466 print_hex_dump(KERN_ERR, "aead enc shdesc@"__stringify(__LINE__)": ",
Yuan Kang1acebad32011-07-15 11:21:42 +0800467 DUMP_PREFIX_ADDRESS, 16, 4, desc,
468 desc_bytes(desc), 1);
469#endif
470
471 /*
472 * Job Descriptor and Shared Descriptors
473 * must all fit into the 64-word Descriptor h/w Buffer
474 */
475 if (DESC_AEAD_DEC_LEN + DESC_JOB_IO_LEN +
476 ctx->split_key_pad_len + ctx->enckeylen <=
477 CAAM_DESC_BYTES_MAX)
Kim Phillips2af8f4a2012-09-07 04:17:03 +0800478 keys_fit_inline = true;
Yuan Kang1acebad32011-07-15 11:21:42 +0800479
Horia Geanta4464a7d2014-03-14 17:46:49 +0200480 /* aead_decrypt shared descriptor */
Yuan Kang1acebad32011-07-15 11:21:42 +0800481 desc = ctx->sh_desc_dec;
482
Horia Geanta4464a7d2014-03-14 17:46:49 +0200483 init_sh_desc_key_aead(desc, ctx, keys_fit_inline);
Yuan Kang1acebad32011-07-15 11:21:42 +0800484
485 /* Class 2 operation */
486 append_operation(desc, ctx->class2_alg_type |
487 OP_ALG_AS_INITFINAL | OP_ALG_DECRYPT | OP_ALG_ICV_ON);
488
Horia Geanta4464a7d2014-03-14 17:46:49 +0200489 /* assoclen + cryptlen = seqinlen - ivsize - authsize */
Yuan Kang1acebad32011-07-15 11:21:42 +0800490 append_math_sub_imm_u32(desc, REG3, SEQINLEN, IMM,
Horia Geantaae4a8252014-03-14 17:46:52 +0200491 ctx->authsize + tfm->ivsize);
Yuan Kang1acebad32011-07-15 11:21:42 +0800492 /* assoclen = (assoclen + cryptlen) - cryptlen */
493 append_math_sub(desc, REG2, SEQOUTLEN, REG0, CAAM_CMD_SZ);
494 append_math_sub(desc, VARSEQINLEN, REG3, REG2, CAAM_CMD_SZ);
495
496 /* read assoc before reading payload */
497 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG |
498 KEY_VLF);
499
500 aead_append_ld_iv(desc, tfm->ivsize);
501
502 append_dec_op1(desc, ctx->class1_alg_type);
503
504 /* Read and write cryptlen bytes */
505 append_math_add(desc, VARSEQINLEN, ZERO, REG2, CAAM_CMD_SZ);
506 append_math_add(desc, VARSEQOUTLEN, ZERO, REG2, CAAM_CMD_SZ);
507 aead_append_src_dst(desc, FIFOLD_TYPE_MSG);
508
509 /* Load ICV */
510 append_seq_fifo_load(desc, ctx->authsize, FIFOLD_CLASS_CLASS2 |
511 FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_ICV);
Yuan Kang1acebad32011-07-15 11:21:42 +0800512
513 ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc,
514 desc_bytes(desc),
515 DMA_TO_DEVICE);
516 if (dma_mapping_error(jrdev, ctx->sh_desc_dec_dma)) {
517 dev_err(jrdev, "unable to map shared descriptor\n");
518 return -ENOMEM;
519 }
520#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +0300521 print_hex_dump(KERN_ERR, "aead dec shdesc@"__stringify(__LINE__)": ",
Yuan Kang1acebad32011-07-15 11:21:42 +0800522 DUMP_PREFIX_ADDRESS, 16, 4, desc,
523 desc_bytes(desc), 1);
524#endif
525
526 /*
527 * Job Descriptor and Shared Descriptors
528 * must all fit into the 64-word Descriptor h/w Buffer
529 */
530 if (DESC_AEAD_GIVENC_LEN + DESC_JOB_IO_LEN +
531 ctx->split_key_pad_len + ctx->enckeylen <=
532 CAAM_DESC_BYTES_MAX)
Kim Phillips2af8f4a2012-09-07 04:17:03 +0800533 keys_fit_inline = true;
Yuan Kang1acebad32011-07-15 11:21:42 +0800534
535 /* aead_givencrypt shared descriptor */
536 desc = ctx->sh_desc_givenc;
537
538 init_sh_desc_key_aead(desc, ctx, keys_fit_inline);
539
540 /* Generate IV */
541 geniv = NFIFOENTRY_STYPE_PAD | NFIFOENTRY_DEST_DECO |
542 NFIFOENTRY_DTYPE_MSG | NFIFOENTRY_LC1 |
543 NFIFOENTRY_PTYPE_RND | (tfm->ivsize << NFIFOENTRY_DLEN_SHIFT);
544 append_load_imm_u32(desc, geniv, LDST_CLASS_IND_CCB |
545 LDST_SRCDST_WORD_INFO_FIFO | LDST_IMM);
546 append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);
547 append_move(desc, MOVE_SRC_INFIFO |
548 MOVE_DEST_CLASS1CTX | (tfm->ivsize << MOVE_LEN_SHIFT));
549 append_cmd(desc, CMD_LOAD | ENABLE_AUTO_INFO_FIFO);
550
551 /* Copy IV to class 1 context */
552 append_move(desc, MOVE_SRC_CLASS1CTX |
553 MOVE_DEST_OUTFIFO | (tfm->ivsize << MOVE_LEN_SHIFT));
554
555 /* Return to encryption */
556 append_operation(desc, ctx->class2_alg_type |
557 OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
558
559 /* ivsize + cryptlen = seqoutlen - authsize */
560 append_math_sub_imm_u32(desc, REG3, SEQOUTLEN, IMM, ctx->authsize);
561
562 /* assoclen = seqinlen - (ivsize + cryptlen) */
563 append_math_sub(desc, VARSEQINLEN, SEQINLEN, REG3, CAAM_CMD_SZ);
564
565 /* read assoc before reading payload */
566 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG |
567 KEY_VLF);
568
569 /* Copy iv from class 1 ctx to class 2 fifo*/
570 moveiv = NFIFOENTRY_STYPE_OFIFO | NFIFOENTRY_DEST_CLASS2 |
571 NFIFOENTRY_DTYPE_MSG | (tfm->ivsize << NFIFOENTRY_DLEN_SHIFT);
572 append_load_imm_u32(desc, moveiv, LDST_CLASS_IND_CCB |
573 LDST_SRCDST_WORD_INFO_FIFO | LDST_IMM);
574 append_load_imm_u32(desc, tfm->ivsize, LDST_CLASS_2_CCB |
575 LDST_SRCDST_WORD_DATASZ_REG | LDST_IMM);
576
577 /* Class 1 operation */
578 append_operation(desc, ctx->class1_alg_type |
579 OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
580
581 /* Will write ivsize + cryptlen */
582 append_math_add(desc, VARSEQOUTLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
583
584 /* Not need to reload iv */
585 append_seq_fifo_load(desc, tfm->ivsize,
586 FIFOLD_CLASS_SKIP);
587
588 /* Will read cryptlen */
589 append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
590 aead_append_src_dst(desc, FIFOLD_TYPE_MSG1OUT2);
591
592 /* Write ICV */
593 append_seq_store(desc, ctx->authsize, LDST_CLASS_2_CCB |
594 LDST_SRCDST_BYTE_CONTEXT);
595
596 ctx->sh_desc_givenc_dma = dma_map_single(jrdev, desc,
597 desc_bytes(desc),
598 DMA_TO_DEVICE);
599 if (dma_mapping_error(jrdev, ctx->sh_desc_givenc_dma)) {
600 dev_err(jrdev, "unable to map shared descriptor\n");
601 return -ENOMEM;
602 }
603#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +0300604 print_hex_dump(KERN_ERR, "aead givenc shdesc@"__stringify(__LINE__)": ",
Yuan Kang1acebad32011-07-15 11:21:42 +0800605 DUMP_PREFIX_ADDRESS, 16, 4, desc,
606 desc_bytes(desc), 1);
607#endif
608
609 return 0;
610}
611
Yuan Kang0e479302011-07-15 11:21:41 +0800612static int aead_setauthsize(struct crypto_aead *authenc,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800613 unsigned int authsize)
614{
615 struct caam_ctx *ctx = crypto_aead_ctx(authenc);
616
617 ctx->authsize = authsize;
Yuan Kang1acebad32011-07-15 11:21:42 +0800618 aead_set_sh_desc(authenc);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800619
620 return 0;
621}
622
Yuan Kang4c1ec1f2012-06-22 19:48:45 -0500623static u32 gen_split_aead_key(struct caam_ctx *ctx, const u8 *key_in,
624 u32 authkeylen)
Kim Phillips8e8ec592011-03-13 16:54:26 +0800625{
Yuan Kang4c1ec1f2012-06-22 19:48:45 -0500626 return gen_split_key(ctx->jrdev, ctx->key, ctx->split_key_len,
627 ctx->split_key_pad_len, key_in, authkeylen,
628 ctx->alg_op);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800629}
630
Yuan Kang0e479302011-07-15 11:21:41 +0800631static int aead_setkey(struct crypto_aead *aead,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800632 const u8 *key, unsigned int keylen)
633{
634 /* Sizes for MDHA pads (*not* keys): MD5, SHA1, 224, 256, 384, 512 */
635 static const u8 mdpadlen[] = { 16, 20, 32, 32, 64, 64 };
636 struct caam_ctx *ctx = crypto_aead_ctx(aead);
637 struct device *jrdev = ctx->jrdev;
Horia Geanta4e6e0b22013-12-19 17:27:35 +0200638 struct crypto_authenc_keys keys;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800639 int ret = 0;
640
Horia Geanta4e6e0b22013-12-19 17:27:35 +0200641 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
Kim Phillips8e8ec592011-03-13 16:54:26 +0800642 goto badkey;
643
644 /* Pick class 2 key length from algorithm submask */
645 ctx->split_key_len = mdpadlen[(ctx->alg_op & OP_ALG_ALGSEL_SUBMASK) >>
646 OP_ALG_ALGSEL_SHIFT] * 2;
647 ctx->split_key_pad_len = ALIGN(ctx->split_key_len, 16);
648
Horia Geanta4e6e0b22013-12-19 17:27:35 +0200649 if (ctx->split_key_pad_len + keys.enckeylen > CAAM_MAX_KEY_SIZE)
650 goto badkey;
651
Kim Phillips8e8ec592011-03-13 16:54:26 +0800652#ifdef DEBUG
653 printk(KERN_ERR "keylen %d enckeylen %d authkeylen %d\n",
Horia Geanta4e6e0b22013-12-19 17:27:35 +0200654 keys.authkeylen + keys.enckeylen, keys.enckeylen,
655 keys.authkeylen);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800656 printk(KERN_ERR "split_key_len %d split_key_pad_len %d\n",
657 ctx->split_key_len, ctx->split_key_pad_len);
Alex Porosanu514df282013-08-14 18:56:45 +0300658 print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
Kim Phillips8e8ec592011-03-13 16:54:26 +0800659 DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
660#endif
Kim Phillips8e8ec592011-03-13 16:54:26 +0800661
Horia Geanta4e6e0b22013-12-19 17:27:35 +0200662 ret = gen_split_aead_key(ctx, keys.authkey, keys.authkeylen);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800663 if (ret) {
Kim Phillips8e8ec592011-03-13 16:54:26 +0800664 goto badkey;
665 }
666
667 /* postpend encryption key to auth split key */
Horia Geanta4e6e0b22013-12-19 17:27:35 +0200668 memcpy(ctx->key + ctx->split_key_pad_len, keys.enckey, keys.enckeylen);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800669
Yuan Kang885e9e22011-07-15 11:21:41 +0800670 ctx->key_dma = dma_map_single(jrdev, ctx->key, ctx->split_key_pad_len +
Horia Geanta4e6e0b22013-12-19 17:27:35 +0200671 keys.enckeylen, DMA_TO_DEVICE);
Yuan Kang885e9e22011-07-15 11:21:41 +0800672 if (dma_mapping_error(jrdev, ctx->key_dma)) {
Kim Phillips8e8ec592011-03-13 16:54:26 +0800673 dev_err(jrdev, "unable to map key i/o memory\n");
Kim Phillips8e8ec592011-03-13 16:54:26 +0800674 return -ENOMEM;
675 }
676#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +0300677 print_hex_dump(KERN_ERR, "ctx.key@"__stringify(__LINE__)": ",
Kim Phillips8e8ec592011-03-13 16:54:26 +0800678 DUMP_PREFIX_ADDRESS, 16, 4, ctx->key,
Horia Geanta4e6e0b22013-12-19 17:27:35 +0200679 ctx->split_key_pad_len + keys.enckeylen, 1);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800680#endif
681
Horia Geanta4e6e0b22013-12-19 17:27:35 +0200682 ctx->enckeylen = keys.enckeylen;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800683
Yuan Kang1acebad32011-07-15 11:21:42 +0800684 ret = aead_set_sh_desc(aead);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800685 if (ret) {
Yuan Kang885e9e22011-07-15 11:21:41 +0800686 dma_unmap_single(jrdev, ctx->key_dma, ctx->split_key_pad_len +
Horia Geanta4e6e0b22013-12-19 17:27:35 +0200687 keys.enckeylen, DMA_TO_DEVICE);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800688 }
689
690 return ret;
691badkey:
692 crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
693 return -EINVAL;
694}
695
Yuan Kangacdca312011-07-15 11:21:42 +0800696static int ablkcipher_setkey(struct crypto_ablkcipher *ablkcipher,
697 const u8 *key, unsigned int keylen)
698{
699 struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
700 struct ablkcipher_tfm *tfm = &ablkcipher->base.crt_ablkcipher;
701 struct device *jrdev = ctx->jrdev;
702 int ret = 0;
Horia Geanta4464a7d2014-03-14 17:46:49 +0200703 u32 *key_jump_cmd;
Yuan Kangacdca312011-07-15 11:21:42 +0800704 u32 *desc;
705
706#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +0300707 print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
Yuan Kangacdca312011-07-15 11:21:42 +0800708 DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
709#endif
710
711 memcpy(ctx->key, key, keylen);
712 ctx->key_dma = dma_map_single(jrdev, ctx->key, keylen,
713 DMA_TO_DEVICE);
714 if (dma_mapping_error(jrdev, ctx->key_dma)) {
715 dev_err(jrdev, "unable to map key i/o memory\n");
716 return -ENOMEM;
717 }
718 ctx->enckeylen = keylen;
719
720 /* ablkcipher_encrypt shared descriptor */
721 desc = ctx->sh_desc_enc;
Kim Phillips61bb86b2012-07-13 17:49:28 -0500722 init_sh_desc(desc, HDR_SHARE_SERIAL);
Yuan Kangacdca312011-07-15 11:21:42 +0800723 /* Skip if already shared */
724 key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
725 JUMP_COND_SHRD);
726
727 /* Load class1 key only */
728 append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
729 ctx->enckeylen, CLASS_1 |
730 KEY_DEST_CLASS_REG);
731
732 set_jump_tgt_here(desc, key_jump_cmd);
733
Yuan Kangacdca312011-07-15 11:21:42 +0800734 /* Load iv */
735 append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT |
736 LDST_CLASS_1_CCB | tfm->ivsize);
737
738 /* Load operation */
739 append_operation(desc, ctx->class1_alg_type |
740 OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
741
742 /* Perform operation */
743 ablkcipher_append_src_dst(desc);
744
745 ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc,
746 desc_bytes(desc),
747 DMA_TO_DEVICE);
748 if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
749 dev_err(jrdev, "unable to map shared descriptor\n");
750 return -ENOMEM;
751 }
752#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +0300753 print_hex_dump(KERN_ERR,
754 "ablkcipher enc shdesc@"__stringify(__LINE__)": ",
Yuan Kangacdca312011-07-15 11:21:42 +0800755 DUMP_PREFIX_ADDRESS, 16, 4, desc,
756 desc_bytes(desc), 1);
757#endif
758 /* ablkcipher_decrypt shared descriptor */
759 desc = ctx->sh_desc_dec;
760
Kim Phillips61bb86b2012-07-13 17:49:28 -0500761 init_sh_desc(desc, HDR_SHARE_SERIAL);
Yuan Kangacdca312011-07-15 11:21:42 +0800762 /* Skip if already shared */
763 key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
764 JUMP_COND_SHRD);
765
766 /* Load class1 key only */
767 append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
768 ctx->enckeylen, CLASS_1 |
769 KEY_DEST_CLASS_REG);
770
Yuan Kangacdca312011-07-15 11:21:42 +0800771 set_jump_tgt_here(desc, key_jump_cmd);
Yuan Kangacdca312011-07-15 11:21:42 +0800772
773 /* load IV */
774 append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT |
775 LDST_CLASS_1_CCB | tfm->ivsize);
776
777 /* Choose operation */
778 append_dec_op1(desc, ctx->class1_alg_type);
779
780 /* Perform operation */
781 ablkcipher_append_src_dst(desc);
782
Yuan Kangacdca312011-07-15 11:21:42 +0800783 ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc,
784 desc_bytes(desc),
785 DMA_TO_DEVICE);
786 if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
787 dev_err(jrdev, "unable to map shared descriptor\n");
788 return -ENOMEM;
789 }
790
791#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +0300792 print_hex_dump(KERN_ERR,
793 "ablkcipher dec shdesc@"__stringify(__LINE__)": ",
Yuan Kangacdca312011-07-15 11:21:42 +0800794 DUMP_PREFIX_ADDRESS, 16, 4, desc,
795 desc_bytes(desc), 1);
796#endif
797
798 return ret;
799}
800
Kim Phillips8e8ec592011-03-13 16:54:26 +0800801/*
Yuan Kang1acebad32011-07-15 11:21:42 +0800802 * aead_edesc - s/w-extended aead descriptor
803 * @assoc_nents: number of segments in associated data (SPI+Seq) scatterlist
Yuan Kang643b39b2012-06-22 19:48:49 -0500804 * @assoc_chained: if source is chained
Kim Phillips8e8ec592011-03-13 16:54:26 +0800805 * @src_nents: number of segments in input scatterlist
Yuan Kang643b39b2012-06-22 19:48:49 -0500806 * @src_chained: if source is chained
Kim Phillips8e8ec592011-03-13 16:54:26 +0800807 * @dst_nents: number of segments in output scatterlist
Yuan Kang643b39b2012-06-22 19:48:49 -0500808 * @dst_chained: if destination is chained
Yuan Kang1acebad32011-07-15 11:21:42 +0800809 * @iv_dma: dma address of iv for checking continuity and link table
Kim Phillips8e8ec592011-03-13 16:54:26 +0800810 * @desc: h/w descriptor (variable length; must not exceed MAX_CAAM_DESCSIZE)
Yuan Kanga299c832012-06-22 19:48:46 -0500811 * @sec4_sg_bytes: length of dma mapped sec4_sg space
812 * @sec4_sg_dma: bus physical mapped address of h/w link table
Kim Phillips8e8ec592011-03-13 16:54:26 +0800813 * @hw_desc: the h/w job descriptor followed by any referenced link tables
814 */
Yuan Kang0e479302011-07-15 11:21:41 +0800815struct aead_edesc {
Kim Phillips8e8ec592011-03-13 16:54:26 +0800816 int assoc_nents;
Yuan Kang643b39b2012-06-22 19:48:49 -0500817 bool assoc_chained;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800818 int src_nents;
Yuan Kang643b39b2012-06-22 19:48:49 -0500819 bool src_chained;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800820 int dst_nents;
Yuan Kang643b39b2012-06-22 19:48:49 -0500821 bool dst_chained;
Yuan Kang1acebad32011-07-15 11:21:42 +0800822 dma_addr_t iv_dma;
Yuan Kanga299c832012-06-22 19:48:46 -0500823 int sec4_sg_bytes;
824 dma_addr_t sec4_sg_dma;
825 struct sec4_sg_entry *sec4_sg;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800826 u32 hw_desc[0];
827};
828
Yuan Kangacdca312011-07-15 11:21:42 +0800829/*
830 * ablkcipher_edesc - s/w-extended ablkcipher descriptor
831 * @src_nents: number of segments in input scatterlist
Yuan Kang643b39b2012-06-22 19:48:49 -0500832 * @src_chained: if source is chained
Yuan Kangacdca312011-07-15 11:21:42 +0800833 * @dst_nents: number of segments in output scatterlist
Yuan Kang643b39b2012-06-22 19:48:49 -0500834 * @dst_chained: if destination is chained
Yuan Kangacdca312011-07-15 11:21:42 +0800835 * @iv_dma: dma address of iv for checking continuity and link table
836 * @desc: h/w descriptor (variable length; must not exceed MAX_CAAM_DESCSIZE)
Yuan Kanga299c832012-06-22 19:48:46 -0500837 * @sec4_sg_bytes: length of dma mapped sec4_sg space
838 * @sec4_sg_dma: bus physical mapped address of h/w link table
Yuan Kangacdca312011-07-15 11:21:42 +0800839 * @hw_desc: the h/w job descriptor followed by any referenced link tables
840 */
841struct ablkcipher_edesc {
842 int src_nents;
Yuan Kang643b39b2012-06-22 19:48:49 -0500843 bool src_chained;
Yuan Kangacdca312011-07-15 11:21:42 +0800844 int dst_nents;
Yuan Kang643b39b2012-06-22 19:48:49 -0500845 bool dst_chained;
Yuan Kangacdca312011-07-15 11:21:42 +0800846 dma_addr_t iv_dma;
Yuan Kanga299c832012-06-22 19:48:46 -0500847 int sec4_sg_bytes;
848 dma_addr_t sec4_sg_dma;
849 struct sec4_sg_entry *sec4_sg;
Yuan Kangacdca312011-07-15 11:21:42 +0800850 u32 hw_desc[0];
851};
852
Yuan Kang1acebad32011-07-15 11:21:42 +0800853static void caam_unmap(struct device *dev, struct scatterlist *src,
Yuan Kang643b39b2012-06-22 19:48:49 -0500854 struct scatterlist *dst, int src_nents,
855 bool src_chained, int dst_nents, bool dst_chained,
Yuan Kanga299c832012-06-22 19:48:46 -0500856 dma_addr_t iv_dma, int ivsize, dma_addr_t sec4_sg_dma,
857 int sec4_sg_bytes)
Kim Phillips8e8ec592011-03-13 16:54:26 +0800858{
Yuan Kang643b39b2012-06-22 19:48:49 -0500859 if (dst != src) {
860 dma_unmap_sg_chained(dev, src, src_nents ? : 1, DMA_TO_DEVICE,
861 src_chained);
862 dma_unmap_sg_chained(dev, dst, dst_nents ? : 1, DMA_FROM_DEVICE,
863 dst_chained);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800864 } else {
Yuan Kang643b39b2012-06-22 19:48:49 -0500865 dma_unmap_sg_chained(dev, src, src_nents ? : 1,
866 DMA_BIDIRECTIONAL, src_chained);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800867 }
868
Yuan Kang1acebad32011-07-15 11:21:42 +0800869 if (iv_dma)
870 dma_unmap_single(dev, iv_dma, ivsize, DMA_TO_DEVICE);
Yuan Kanga299c832012-06-22 19:48:46 -0500871 if (sec4_sg_bytes)
872 dma_unmap_single(dev, sec4_sg_dma, sec4_sg_bytes,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800873 DMA_TO_DEVICE);
874}
875
Yuan Kang1acebad32011-07-15 11:21:42 +0800876static void aead_unmap(struct device *dev,
877 struct aead_edesc *edesc,
878 struct aead_request *req)
879{
880 struct crypto_aead *aead = crypto_aead_reqtfm(req);
881 int ivsize = crypto_aead_ivsize(aead);
882
Yuan Kang643b39b2012-06-22 19:48:49 -0500883 dma_unmap_sg_chained(dev, req->assoc, edesc->assoc_nents,
884 DMA_TO_DEVICE, edesc->assoc_chained);
Yuan Kang1acebad32011-07-15 11:21:42 +0800885
886 caam_unmap(dev, req->src, req->dst,
Yuan Kang643b39b2012-06-22 19:48:49 -0500887 edesc->src_nents, edesc->src_chained, edesc->dst_nents,
888 edesc->dst_chained, edesc->iv_dma, ivsize,
889 edesc->sec4_sg_dma, edesc->sec4_sg_bytes);
Yuan Kang1acebad32011-07-15 11:21:42 +0800890}
891
Yuan Kangacdca312011-07-15 11:21:42 +0800892static void ablkcipher_unmap(struct device *dev,
893 struct ablkcipher_edesc *edesc,
894 struct ablkcipher_request *req)
895{
896 struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
897 int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
898
899 caam_unmap(dev, req->src, req->dst,
Yuan Kang643b39b2012-06-22 19:48:49 -0500900 edesc->src_nents, edesc->src_chained, edesc->dst_nents,
901 edesc->dst_chained, edesc->iv_dma, ivsize,
902 edesc->sec4_sg_dma, edesc->sec4_sg_bytes);
Yuan Kangacdca312011-07-15 11:21:42 +0800903}
904
Yuan Kang0e479302011-07-15 11:21:41 +0800905static void aead_encrypt_done(struct device *jrdev, u32 *desc, u32 err,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800906 void *context)
907{
Yuan Kang0e479302011-07-15 11:21:41 +0800908 struct aead_request *req = context;
909 struct aead_edesc *edesc;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800910#ifdef DEBUG
Yuan Kang0e479302011-07-15 11:21:41 +0800911 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800912 struct caam_ctx *ctx = crypto_aead_ctx(aead);
Yuan Kang1acebad32011-07-15 11:21:42 +0800913 int ivsize = crypto_aead_ivsize(aead);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800914
915 dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
916#endif
Yuan Kang1acebad32011-07-15 11:21:42 +0800917
Yuan Kang0e479302011-07-15 11:21:41 +0800918 edesc = (struct aead_edesc *)((char *)desc -
919 offsetof(struct aead_edesc, hw_desc));
Kim Phillips8e8ec592011-03-13 16:54:26 +0800920
Marek Vasutfa9659c2014-04-24 20:05:12 +0200921 if (err)
922 caam_jr_strstatus(jrdev, err);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800923
Yuan Kang0e479302011-07-15 11:21:41 +0800924 aead_unmap(jrdev, edesc, req);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800925
926#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +0300927 print_hex_dump(KERN_ERR, "assoc @"__stringify(__LINE__)": ",
Yuan Kang0e479302011-07-15 11:21:41 +0800928 DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->assoc),
929 req->assoclen , 1);
Alex Porosanu514df282013-08-14 18:56:45 +0300930 print_hex_dump(KERN_ERR, "dstiv @"__stringify(__LINE__)": ",
Yuan Kang0e479302011-07-15 11:21:41 +0800931 DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src) - ivsize,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800932 edesc->src_nents ? 100 : ivsize, 1);
Alex Porosanu514df282013-08-14 18:56:45 +0300933 print_hex_dump(KERN_ERR, "dst @"__stringify(__LINE__)": ",
Yuan Kang0e479302011-07-15 11:21:41 +0800934 DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
935 edesc->src_nents ? 100 : req->cryptlen +
Kim Phillips8e8ec592011-03-13 16:54:26 +0800936 ctx->authsize + 4, 1);
937#endif
938
939 kfree(edesc);
940
Yuan Kang0e479302011-07-15 11:21:41 +0800941 aead_request_complete(req, err);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800942}
943
Yuan Kang0e479302011-07-15 11:21:41 +0800944static void aead_decrypt_done(struct device *jrdev, u32 *desc, u32 err,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800945 void *context)
946{
Yuan Kang0e479302011-07-15 11:21:41 +0800947 struct aead_request *req = context;
948 struct aead_edesc *edesc;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800949#ifdef DEBUG
Yuan Kang0e479302011-07-15 11:21:41 +0800950 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800951 struct caam_ctx *ctx = crypto_aead_ctx(aead);
Yuan Kang1acebad32011-07-15 11:21:42 +0800952 int ivsize = crypto_aead_ivsize(aead);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800953
954 dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
955#endif
Yuan Kang1acebad32011-07-15 11:21:42 +0800956
Yuan Kang0e479302011-07-15 11:21:41 +0800957 edesc = (struct aead_edesc *)((char *)desc -
958 offsetof(struct aead_edesc, hw_desc));
Kim Phillips8e8ec592011-03-13 16:54:26 +0800959
Yuan Kang1acebad32011-07-15 11:21:42 +0800960#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +0300961 print_hex_dump(KERN_ERR, "dstiv @"__stringify(__LINE__)": ",
Yuan Kang1acebad32011-07-15 11:21:42 +0800962 DUMP_PREFIX_ADDRESS, 16, 4, req->iv,
963 ivsize, 1);
Alex Porosanu514df282013-08-14 18:56:45 +0300964 print_hex_dump(KERN_ERR, "dst @"__stringify(__LINE__)": ",
Yuan Kang1acebad32011-07-15 11:21:42 +0800965 DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->dst),
Horia Geantabbf9c892013-11-28 15:11:16 +0200966 req->cryptlen - ctx->authsize, 1);
Yuan Kang1acebad32011-07-15 11:21:42 +0800967#endif
968
Marek Vasutfa9659c2014-04-24 20:05:12 +0200969 if (err)
970 caam_jr_strstatus(jrdev, err);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800971
Yuan Kang0e479302011-07-15 11:21:41 +0800972 aead_unmap(jrdev, edesc, req);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800973
974 /*
975 * verify hw auth check passed else return -EBADMSG
976 */
977 if ((err & JRSTA_CCBERR_ERRID_MASK) == JRSTA_CCBERR_ERRID_ICVCHK)
978 err = -EBADMSG;
979
980#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +0300981 print_hex_dump(KERN_ERR, "iphdrout@"__stringify(__LINE__)": ",
Kim Phillips8e8ec592011-03-13 16:54:26 +0800982 DUMP_PREFIX_ADDRESS, 16, 4,
Yuan Kang0e479302011-07-15 11:21:41 +0800983 ((char *)sg_virt(req->assoc) - sizeof(struct iphdr)),
984 sizeof(struct iphdr) + req->assoclen +
985 ((req->cryptlen > 1500) ? 1500 : req->cryptlen) +
Kim Phillips8e8ec592011-03-13 16:54:26 +0800986 ctx->authsize + 36, 1);
Yuan Kanga299c832012-06-22 19:48:46 -0500987 if (!err && edesc->sec4_sg_bytes) {
Yuan Kang0e479302011-07-15 11:21:41 +0800988 struct scatterlist *sg = sg_last(req->src, edesc->src_nents);
Alex Porosanu514df282013-08-14 18:56:45 +0300989 print_hex_dump(KERN_ERR, "sglastout@"__stringify(__LINE__)": ",
Kim Phillips8e8ec592011-03-13 16:54:26 +0800990 DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(sg),
991 sg->length + ctx->authsize + 16, 1);
992 }
993#endif
Yuan Kang1acebad32011-07-15 11:21:42 +0800994
Kim Phillips8e8ec592011-03-13 16:54:26 +0800995 kfree(edesc);
996
Yuan Kang0e479302011-07-15 11:21:41 +0800997 aead_request_complete(req, err);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800998}
999
Yuan Kangacdca312011-07-15 11:21:42 +08001000static void ablkcipher_encrypt_done(struct device *jrdev, u32 *desc, u32 err,
1001 void *context)
1002{
1003 struct ablkcipher_request *req = context;
1004 struct ablkcipher_edesc *edesc;
1005#ifdef DEBUG
1006 struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
1007 int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
1008
1009 dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
1010#endif
1011
1012 edesc = (struct ablkcipher_edesc *)((char *)desc -
1013 offsetof(struct ablkcipher_edesc, hw_desc));
1014
Marek Vasutfa9659c2014-04-24 20:05:12 +02001015 if (err)
1016 caam_jr_strstatus(jrdev, err);
Yuan Kangacdca312011-07-15 11:21:42 +08001017
1018#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +03001019 print_hex_dump(KERN_ERR, "dstiv @"__stringify(__LINE__)": ",
Yuan Kangacdca312011-07-15 11:21:42 +08001020 DUMP_PREFIX_ADDRESS, 16, 4, req->info,
1021 edesc->src_nents > 1 ? 100 : ivsize, 1);
Alex Porosanu514df282013-08-14 18:56:45 +03001022 print_hex_dump(KERN_ERR, "dst @"__stringify(__LINE__)": ",
Yuan Kangacdca312011-07-15 11:21:42 +08001023 DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
1024 edesc->dst_nents > 1 ? 100 : req->nbytes, 1);
1025#endif
1026
1027 ablkcipher_unmap(jrdev, edesc, req);
1028 kfree(edesc);
1029
1030 ablkcipher_request_complete(req, err);
1031}
1032
1033static void ablkcipher_decrypt_done(struct device *jrdev, u32 *desc, u32 err,
1034 void *context)
1035{
1036 struct ablkcipher_request *req = context;
1037 struct ablkcipher_edesc *edesc;
1038#ifdef DEBUG
1039 struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
1040 int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
1041
1042 dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
1043#endif
1044
1045 edesc = (struct ablkcipher_edesc *)((char *)desc -
1046 offsetof(struct ablkcipher_edesc, hw_desc));
Marek Vasutfa9659c2014-04-24 20:05:12 +02001047 if (err)
1048 caam_jr_strstatus(jrdev, err);
Yuan Kangacdca312011-07-15 11:21:42 +08001049
1050#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +03001051 print_hex_dump(KERN_ERR, "dstiv @"__stringify(__LINE__)": ",
Yuan Kangacdca312011-07-15 11:21:42 +08001052 DUMP_PREFIX_ADDRESS, 16, 4, req->info,
1053 ivsize, 1);
Alex Porosanu514df282013-08-14 18:56:45 +03001054 print_hex_dump(KERN_ERR, "dst @"__stringify(__LINE__)": ",
Yuan Kangacdca312011-07-15 11:21:42 +08001055 DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
1056 edesc->dst_nents > 1 ? 100 : req->nbytes, 1);
1057#endif
1058
1059 ablkcipher_unmap(jrdev, edesc, req);
1060 kfree(edesc);
1061
1062 ablkcipher_request_complete(req, err);
1063}
1064
Kim Phillips8e8ec592011-03-13 16:54:26 +08001065/*
Yuan Kang1acebad32011-07-15 11:21:42 +08001066 * Fill in aead job descriptor
Kim Phillips8e8ec592011-03-13 16:54:26 +08001067 */
Yuan Kang1acebad32011-07-15 11:21:42 +08001068static void init_aead_job(u32 *sh_desc, dma_addr_t ptr,
1069 struct aead_edesc *edesc,
1070 struct aead_request *req,
1071 bool all_contig, bool encrypt)
Kim Phillips8e8ec592011-03-13 16:54:26 +08001072{
Yuan Kang0e479302011-07-15 11:21:41 +08001073 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001074 struct caam_ctx *ctx = crypto_aead_ctx(aead);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001075 int ivsize = crypto_aead_ivsize(aead);
1076 int authsize = ctx->authsize;
Yuan Kang1acebad32011-07-15 11:21:42 +08001077 u32 *desc = edesc->hw_desc;
1078 u32 out_options = 0, in_options;
1079 dma_addr_t dst_dma, src_dma;
Yuan Kanga299c832012-06-22 19:48:46 -05001080 int len, sec4_sg_index = 0;
Kim Phillips8e8ec592011-03-13 16:54:26 +08001081
Yuan Kang1acebad32011-07-15 11:21:42 +08001082#ifdef DEBUG
Kim Phillips8e8ec592011-03-13 16:54:26 +08001083 debug("assoclen %d cryptlen %d authsize %d\n",
Yuan Kang0e479302011-07-15 11:21:41 +08001084 req->assoclen, req->cryptlen, authsize);
Alex Porosanu514df282013-08-14 18:56:45 +03001085 print_hex_dump(KERN_ERR, "assoc @"__stringify(__LINE__)": ",
Yuan Kang0e479302011-07-15 11:21:41 +08001086 DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->assoc),
1087 req->assoclen , 1);
Alex Porosanu514df282013-08-14 18:56:45 +03001088 print_hex_dump(KERN_ERR, "presciv@"__stringify(__LINE__)": ",
Yuan Kang1acebad32011-07-15 11:21:42 +08001089 DUMP_PREFIX_ADDRESS, 16, 4, req->iv,
Kim Phillips8e8ec592011-03-13 16:54:26 +08001090 edesc->src_nents ? 100 : ivsize, 1);
Alex Porosanu514df282013-08-14 18:56:45 +03001091 print_hex_dump(KERN_ERR, "src @"__stringify(__LINE__)": ",
Yuan Kang0e479302011-07-15 11:21:41 +08001092 DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
Yuan Kang1acebad32011-07-15 11:21:42 +08001093 edesc->src_nents ? 100 : req->cryptlen, 1);
Alex Porosanu514df282013-08-14 18:56:45 +03001094 print_hex_dump(KERN_ERR, "shrdesc@"__stringify(__LINE__)": ",
Kim Phillips8e8ec592011-03-13 16:54:26 +08001095 DUMP_PREFIX_ADDRESS, 16, 4, sh_desc,
1096 desc_bytes(sh_desc), 1);
1097#endif
Yuan Kang1acebad32011-07-15 11:21:42 +08001098
1099 len = desc_len(sh_desc);
1100 init_job_desc_shared(desc, ptr, len, HDR_SHARE_DEFER | HDR_REVERSE);
1101
1102 if (all_contig) {
1103 src_dma = sg_dma_address(req->assoc);
1104 in_options = 0;
1105 } else {
Yuan Kanga299c832012-06-22 19:48:46 -05001106 src_dma = edesc->sec4_sg_dma;
1107 sec4_sg_index += (edesc->assoc_nents ? : 1) + 1 +
1108 (edesc->src_nents ? : 1);
Yuan Kang1acebad32011-07-15 11:21:42 +08001109 in_options = LDST_SGF;
1110 }
Horia Geantabbf9c892013-11-28 15:11:16 +02001111
1112 append_seq_in_ptr(desc, src_dma, req->assoclen + ivsize + req->cryptlen,
1113 in_options);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001114
Yuan Kang1acebad32011-07-15 11:21:42 +08001115 if (likely(req->src == req->dst)) {
1116 if (all_contig) {
1117 dst_dma = sg_dma_address(req->src);
1118 } else {
Yuan Kanga299c832012-06-22 19:48:46 -05001119 dst_dma = src_dma + sizeof(struct sec4_sg_entry) *
Yuan Kang1acebad32011-07-15 11:21:42 +08001120 ((edesc->assoc_nents ? : 1) + 1);
1121 out_options = LDST_SGF;
1122 }
Kim Phillips8e8ec592011-03-13 16:54:26 +08001123 } else {
Kim Phillips8e8ec592011-03-13 16:54:26 +08001124 if (!edesc->dst_nents) {
Yuan Kang0e479302011-07-15 11:21:41 +08001125 dst_dma = sg_dma_address(req->dst);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001126 } else {
Yuan Kanga299c832012-06-22 19:48:46 -05001127 dst_dma = edesc->sec4_sg_dma +
1128 sec4_sg_index *
1129 sizeof(struct sec4_sg_entry);
Yuan Kang1acebad32011-07-15 11:21:42 +08001130 out_options = LDST_SGF;
Kim Phillips8e8ec592011-03-13 16:54:26 +08001131 }
1132 }
Kim Phillips8e8ec592011-03-13 16:54:26 +08001133 if (encrypt)
Horia Geantabbf9c892013-11-28 15:11:16 +02001134 append_seq_out_ptr(desc, dst_dma, req->cryptlen + authsize,
1135 out_options);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001136 else
Yuan Kang1acebad32011-07-15 11:21:42 +08001137 append_seq_out_ptr(desc, dst_dma, req->cryptlen - authsize,
1138 out_options);
1139}
1140
1141/*
1142 * Fill in aead givencrypt job descriptor
1143 */
1144static void init_aead_giv_job(u32 *sh_desc, dma_addr_t ptr,
1145 struct aead_edesc *edesc,
1146 struct aead_request *req,
1147 int contig)
1148{
1149 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1150 struct caam_ctx *ctx = crypto_aead_ctx(aead);
1151 int ivsize = crypto_aead_ivsize(aead);
1152 int authsize = ctx->authsize;
1153 u32 *desc = edesc->hw_desc;
1154 u32 out_options = 0, in_options;
1155 dma_addr_t dst_dma, src_dma;
Yuan Kanga299c832012-06-22 19:48:46 -05001156 int len, sec4_sg_index = 0;
Kim Phillips8e8ec592011-03-13 16:54:26 +08001157
1158#ifdef DEBUG
Yuan Kang1acebad32011-07-15 11:21:42 +08001159 debug("assoclen %d cryptlen %d authsize %d\n",
1160 req->assoclen, req->cryptlen, authsize);
Alex Porosanu514df282013-08-14 18:56:45 +03001161 print_hex_dump(KERN_ERR, "assoc @"__stringify(__LINE__)": ",
Yuan Kang1acebad32011-07-15 11:21:42 +08001162 DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->assoc),
1163 req->assoclen , 1);
Alex Porosanu514df282013-08-14 18:56:45 +03001164 print_hex_dump(KERN_ERR, "presciv@"__stringify(__LINE__)": ",
Yuan Kang1acebad32011-07-15 11:21:42 +08001165 DUMP_PREFIX_ADDRESS, 16, 4, req->iv, ivsize, 1);
Alex Porosanu514df282013-08-14 18:56:45 +03001166 print_hex_dump(KERN_ERR, "src @"__stringify(__LINE__)": ",
Yuan Kang1acebad32011-07-15 11:21:42 +08001167 DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
1168 edesc->src_nents > 1 ? 100 : req->cryptlen, 1);
Alex Porosanu514df282013-08-14 18:56:45 +03001169 print_hex_dump(KERN_ERR, "shrdesc@"__stringify(__LINE__)": ",
Yuan Kang1acebad32011-07-15 11:21:42 +08001170 DUMP_PREFIX_ADDRESS, 16, 4, sh_desc,
1171 desc_bytes(sh_desc), 1);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001172#endif
1173
Yuan Kang1acebad32011-07-15 11:21:42 +08001174 len = desc_len(sh_desc);
1175 init_job_desc_shared(desc, ptr, len, HDR_SHARE_DEFER | HDR_REVERSE);
1176
1177 if (contig & GIV_SRC_CONTIG) {
1178 src_dma = sg_dma_address(req->assoc);
1179 in_options = 0;
1180 } else {
Yuan Kanga299c832012-06-22 19:48:46 -05001181 src_dma = edesc->sec4_sg_dma;
1182 sec4_sg_index += edesc->assoc_nents + 1 + edesc->src_nents;
Yuan Kang1acebad32011-07-15 11:21:42 +08001183 in_options = LDST_SGF;
1184 }
Horia Geantabbf9c892013-11-28 15:11:16 +02001185 append_seq_in_ptr(desc, src_dma, req->assoclen + ivsize + req->cryptlen,
1186 in_options);
Yuan Kang1acebad32011-07-15 11:21:42 +08001187
1188 if (contig & GIV_DST_CONTIG) {
1189 dst_dma = edesc->iv_dma;
1190 } else {
1191 if (likely(req->src == req->dst)) {
Yuan Kanga299c832012-06-22 19:48:46 -05001192 dst_dma = src_dma + sizeof(struct sec4_sg_entry) *
Yuan Kang1acebad32011-07-15 11:21:42 +08001193 edesc->assoc_nents;
1194 out_options = LDST_SGF;
1195 } else {
Yuan Kanga299c832012-06-22 19:48:46 -05001196 dst_dma = edesc->sec4_sg_dma +
1197 sec4_sg_index *
1198 sizeof(struct sec4_sg_entry);
Yuan Kang1acebad32011-07-15 11:21:42 +08001199 out_options = LDST_SGF;
1200 }
Kim Phillips8e8ec592011-03-13 16:54:26 +08001201 }
1202
Horia Geantabbf9c892013-11-28 15:11:16 +02001203 append_seq_out_ptr(desc, dst_dma, ivsize + req->cryptlen + authsize,
1204 out_options);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001205}
1206
1207/*
Yuan Kangacdca312011-07-15 11:21:42 +08001208 * Fill in ablkcipher job descriptor
1209 */
1210static void init_ablkcipher_job(u32 *sh_desc, dma_addr_t ptr,
1211 struct ablkcipher_edesc *edesc,
1212 struct ablkcipher_request *req,
1213 bool iv_contig)
1214{
1215 struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
1216 int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
1217 u32 *desc = edesc->hw_desc;
1218 u32 out_options = 0, in_options;
1219 dma_addr_t dst_dma, src_dma;
Yuan Kanga299c832012-06-22 19:48:46 -05001220 int len, sec4_sg_index = 0;
Yuan Kangacdca312011-07-15 11:21:42 +08001221
1222#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +03001223 print_hex_dump(KERN_ERR, "presciv@"__stringify(__LINE__)": ",
Yuan Kangacdca312011-07-15 11:21:42 +08001224 DUMP_PREFIX_ADDRESS, 16, 4, req->info,
1225 ivsize, 1);
Alex Porosanu514df282013-08-14 18:56:45 +03001226 print_hex_dump(KERN_ERR, "src @"__stringify(__LINE__)": ",
Yuan Kangacdca312011-07-15 11:21:42 +08001227 DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
1228 edesc->src_nents ? 100 : req->nbytes, 1);
1229#endif
1230
1231 len = desc_len(sh_desc);
1232 init_job_desc_shared(desc, ptr, len, HDR_SHARE_DEFER | HDR_REVERSE);
1233
1234 if (iv_contig) {
1235 src_dma = edesc->iv_dma;
1236 in_options = 0;
1237 } else {
Yuan Kanga299c832012-06-22 19:48:46 -05001238 src_dma = edesc->sec4_sg_dma;
1239 sec4_sg_index += (iv_contig ? 0 : 1) + edesc->src_nents;
Yuan Kangacdca312011-07-15 11:21:42 +08001240 in_options = LDST_SGF;
1241 }
1242 append_seq_in_ptr(desc, src_dma, req->nbytes + ivsize, in_options);
1243
1244 if (likely(req->src == req->dst)) {
1245 if (!edesc->src_nents && iv_contig) {
1246 dst_dma = sg_dma_address(req->src);
1247 } else {
Yuan Kanga299c832012-06-22 19:48:46 -05001248 dst_dma = edesc->sec4_sg_dma +
1249 sizeof(struct sec4_sg_entry);
Yuan Kangacdca312011-07-15 11:21:42 +08001250 out_options = LDST_SGF;
1251 }
1252 } else {
1253 if (!edesc->dst_nents) {
1254 dst_dma = sg_dma_address(req->dst);
1255 } else {
Yuan Kanga299c832012-06-22 19:48:46 -05001256 dst_dma = edesc->sec4_sg_dma +
1257 sec4_sg_index * sizeof(struct sec4_sg_entry);
Yuan Kangacdca312011-07-15 11:21:42 +08001258 out_options = LDST_SGF;
1259 }
1260 }
1261 append_seq_out_ptr(desc, dst_dma, req->nbytes, out_options);
1262}
1263
1264/*
Yuan Kang1acebad32011-07-15 11:21:42 +08001265 * allocate and map the aead extended descriptor
Kim Phillips8e8ec592011-03-13 16:54:26 +08001266 */
Yuan Kang0e479302011-07-15 11:21:41 +08001267static struct aead_edesc *aead_edesc_alloc(struct aead_request *req,
Horia Geantabbf9c892013-11-28 15:11:16 +02001268 int desc_bytes, bool *all_contig_ptr,
1269 bool encrypt)
Kim Phillips8e8ec592011-03-13 16:54:26 +08001270{
Yuan Kang0e479302011-07-15 11:21:41 +08001271 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001272 struct caam_ctx *ctx = crypto_aead_ctx(aead);
1273 struct device *jrdev = ctx->jrdev;
Yuan Kang1acebad32011-07-15 11:21:42 +08001274 gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
1275 CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
1276 int assoc_nents, src_nents, dst_nents = 0;
Yuan Kang0e479302011-07-15 11:21:41 +08001277 struct aead_edesc *edesc;
Yuan Kang1acebad32011-07-15 11:21:42 +08001278 dma_addr_t iv_dma = 0;
1279 int sgc;
1280 bool all_contig = true;
Yuan Kang643b39b2012-06-22 19:48:49 -05001281 bool assoc_chained = false, src_chained = false, dst_chained = false;
Yuan Kang1acebad32011-07-15 11:21:42 +08001282 int ivsize = crypto_aead_ivsize(aead);
Yuan Kanga299c832012-06-22 19:48:46 -05001283 int sec4_sg_index, sec4_sg_len = 0, sec4_sg_bytes;
Horia Geantabbf9c892013-11-28 15:11:16 +02001284 unsigned int authsize = ctx->authsize;
Kim Phillips8e8ec592011-03-13 16:54:26 +08001285
Yuan Kang643b39b2012-06-22 19:48:49 -05001286 assoc_nents = sg_count(req->assoc, req->assoclen, &assoc_chained);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001287
Horia Geantabbf9c892013-11-28 15:11:16 +02001288 if (unlikely(req->dst != req->src)) {
1289 src_nents = sg_count(req->src, req->cryptlen, &src_chained);
1290 dst_nents = sg_count(req->dst,
1291 req->cryptlen +
1292 (encrypt ? authsize : (-authsize)),
1293 &dst_chained);
1294 } else {
1295 src_nents = sg_count(req->src,
1296 req->cryptlen +
1297 (encrypt ? authsize : 0),
1298 &src_chained);
1299 }
Kim Phillips8e8ec592011-03-13 16:54:26 +08001300
Yuan Kang643b39b2012-06-22 19:48:49 -05001301 sgc = dma_map_sg_chained(jrdev, req->assoc, assoc_nents ? : 1,
Horia Geanta286233e2013-05-10 15:08:39 +03001302 DMA_TO_DEVICE, assoc_chained);
Yuan Kang1acebad32011-07-15 11:21:42 +08001303 if (likely(req->src == req->dst)) {
Yuan Kang643b39b2012-06-22 19:48:49 -05001304 sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1,
1305 DMA_BIDIRECTIONAL, src_chained);
Yuan Kang1acebad32011-07-15 11:21:42 +08001306 } else {
Yuan Kang643b39b2012-06-22 19:48:49 -05001307 sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1,
1308 DMA_TO_DEVICE, src_chained);
1309 sgc = dma_map_sg_chained(jrdev, req->dst, dst_nents ? : 1,
1310 DMA_FROM_DEVICE, dst_chained);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001311 }
1312
Yuan Kang1acebad32011-07-15 11:21:42 +08001313 /* Check if data are contiguous */
1314 iv_dma = dma_map_single(jrdev, req->iv, ivsize, DMA_TO_DEVICE);
1315 if (assoc_nents || sg_dma_address(req->assoc) + req->assoclen !=
1316 iv_dma || src_nents || iv_dma + ivsize !=
1317 sg_dma_address(req->src)) {
1318 all_contig = false;
1319 assoc_nents = assoc_nents ? : 1;
1320 src_nents = src_nents ? : 1;
Yuan Kanga299c832012-06-22 19:48:46 -05001321 sec4_sg_len = assoc_nents + 1 + src_nents;
Yuan Kang1acebad32011-07-15 11:21:42 +08001322 }
Yuan Kanga299c832012-06-22 19:48:46 -05001323 sec4_sg_len += dst_nents;
Yuan Kang1acebad32011-07-15 11:21:42 +08001324
Yuan Kanga299c832012-06-22 19:48:46 -05001325 sec4_sg_bytes = sec4_sg_len * sizeof(struct sec4_sg_entry);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001326
1327 /* allocate space for base edesc and hw desc commands, link tables */
Yuan Kang0e479302011-07-15 11:21:41 +08001328 edesc = kmalloc(sizeof(struct aead_edesc) + desc_bytes +
Yuan Kanga299c832012-06-22 19:48:46 -05001329 sec4_sg_bytes, GFP_DMA | flags);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001330 if (!edesc) {
1331 dev_err(jrdev, "could not allocate extended descriptor\n");
1332 return ERR_PTR(-ENOMEM);
1333 }
1334
1335 edesc->assoc_nents = assoc_nents;
Yuan Kang643b39b2012-06-22 19:48:49 -05001336 edesc->assoc_chained = assoc_chained;
Kim Phillips8e8ec592011-03-13 16:54:26 +08001337 edesc->src_nents = src_nents;
Yuan Kang643b39b2012-06-22 19:48:49 -05001338 edesc->src_chained = src_chained;
Kim Phillips8e8ec592011-03-13 16:54:26 +08001339 edesc->dst_nents = dst_nents;
Yuan Kang643b39b2012-06-22 19:48:49 -05001340 edesc->dst_chained = dst_chained;
Yuan Kang1acebad32011-07-15 11:21:42 +08001341 edesc->iv_dma = iv_dma;
Yuan Kanga299c832012-06-22 19:48:46 -05001342 edesc->sec4_sg_bytes = sec4_sg_bytes;
1343 edesc->sec4_sg = (void *)edesc + sizeof(struct aead_edesc) +
1344 desc_bytes;
1345 edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
1346 sec4_sg_bytes, DMA_TO_DEVICE);
Yuan Kang1acebad32011-07-15 11:21:42 +08001347 *all_contig_ptr = all_contig;
1348
Yuan Kanga299c832012-06-22 19:48:46 -05001349 sec4_sg_index = 0;
Yuan Kang1acebad32011-07-15 11:21:42 +08001350 if (!all_contig) {
Yuan Kanga299c832012-06-22 19:48:46 -05001351 sg_to_sec4_sg(req->assoc,
1352 (assoc_nents ? : 1),
1353 edesc->sec4_sg +
1354 sec4_sg_index, 0);
1355 sec4_sg_index += assoc_nents ? : 1;
1356 dma_to_sec4_sg_one(edesc->sec4_sg + sec4_sg_index,
Yuan Kang1acebad32011-07-15 11:21:42 +08001357 iv_dma, ivsize, 0);
Yuan Kanga299c832012-06-22 19:48:46 -05001358 sec4_sg_index += 1;
1359 sg_to_sec4_sg_last(req->src,
1360 (src_nents ? : 1),
1361 edesc->sec4_sg +
1362 sec4_sg_index, 0);
1363 sec4_sg_index += src_nents ? : 1;
Yuan Kang1acebad32011-07-15 11:21:42 +08001364 }
1365 if (dst_nents) {
Yuan Kanga299c832012-06-22 19:48:46 -05001366 sg_to_sec4_sg_last(req->dst, dst_nents,
1367 edesc->sec4_sg + sec4_sg_index, 0);
Yuan Kang1acebad32011-07-15 11:21:42 +08001368 }
Kim Phillips8e8ec592011-03-13 16:54:26 +08001369
1370 return edesc;
1371}
1372
Yuan Kang0e479302011-07-15 11:21:41 +08001373static int aead_encrypt(struct aead_request *req)
Kim Phillips8e8ec592011-03-13 16:54:26 +08001374{
Yuan Kang0e479302011-07-15 11:21:41 +08001375 struct aead_edesc *edesc;
Kim Phillips8e8ec592011-03-13 16:54:26 +08001376 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001377 struct caam_ctx *ctx = crypto_aead_ctx(aead);
1378 struct device *jrdev = ctx->jrdev;
Yuan Kang1acebad32011-07-15 11:21:42 +08001379 bool all_contig;
Kim Phillips8e8ec592011-03-13 16:54:26 +08001380 u32 *desc;
Yuan Kang1acebad32011-07-15 11:21:42 +08001381 int ret = 0;
1382
Kim Phillips8e8ec592011-03-13 16:54:26 +08001383 /* allocate extended descriptor */
Yuan Kang1acebad32011-07-15 11:21:42 +08001384 edesc = aead_edesc_alloc(req, DESC_JOB_IO_LEN *
Horia Geantabbf9c892013-11-28 15:11:16 +02001385 CAAM_CMD_SZ, &all_contig, true);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001386 if (IS_ERR(edesc))
1387 return PTR_ERR(edesc);
1388
Yuan Kang1acebad32011-07-15 11:21:42 +08001389 /* Create and submit job descriptor */
1390 init_aead_job(ctx->sh_desc_enc, ctx->sh_desc_enc_dma, edesc, req,
1391 all_contig, true);
1392#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +03001393 print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ",
Yuan Kang1acebad32011-07-15 11:21:42 +08001394 DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
1395 desc_bytes(edesc->hw_desc), 1);
1396#endif
1397
Kim Phillips8e8ec592011-03-13 16:54:26 +08001398 desc = edesc->hw_desc;
Yuan Kang1acebad32011-07-15 11:21:42 +08001399 ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req);
1400 if (!ret) {
1401 ret = -EINPROGRESS;
1402 } else {
1403 aead_unmap(jrdev, edesc, req);
1404 kfree(edesc);
1405 }
Kim Phillips8e8ec592011-03-13 16:54:26 +08001406
Yuan Kang1acebad32011-07-15 11:21:42 +08001407 return ret;
Kim Phillips8e8ec592011-03-13 16:54:26 +08001408}
1409
Yuan Kang0e479302011-07-15 11:21:41 +08001410static int aead_decrypt(struct aead_request *req)
Kim Phillips8e8ec592011-03-13 16:54:26 +08001411{
Yuan Kang1acebad32011-07-15 11:21:42 +08001412 struct aead_edesc *edesc;
Yuan Kang0e479302011-07-15 11:21:41 +08001413 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Yuan Kang0e479302011-07-15 11:21:41 +08001414 struct caam_ctx *ctx = crypto_aead_ctx(aead);
1415 struct device *jrdev = ctx->jrdev;
Yuan Kang1acebad32011-07-15 11:21:42 +08001416 bool all_contig;
Yuan Kang0e479302011-07-15 11:21:41 +08001417 u32 *desc;
Yuan Kang1acebad32011-07-15 11:21:42 +08001418 int ret = 0;
Yuan Kang0e479302011-07-15 11:21:41 +08001419
1420 /* allocate extended descriptor */
Yuan Kang1acebad32011-07-15 11:21:42 +08001421 edesc = aead_edesc_alloc(req, DESC_JOB_IO_LEN *
Horia Geantabbf9c892013-11-28 15:11:16 +02001422 CAAM_CMD_SZ, &all_contig, false);
Yuan Kang0e479302011-07-15 11:21:41 +08001423 if (IS_ERR(edesc))
1424 return PTR_ERR(edesc);
1425
Yuan Kang1acebad32011-07-15 11:21:42 +08001426#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +03001427 print_hex_dump(KERN_ERR, "dec src@"__stringify(__LINE__)": ",
Yuan Kang1acebad32011-07-15 11:21:42 +08001428 DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
1429 req->cryptlen, 1);
1430#endif
1431
1432 /* Create and submit job descriptor*/
1433 init_aead_job(ctx->sh_desc_dec,
1434 ctx->sh_desc_dec_dma, edesc, req, all_contig, false);
1435#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +03001436 print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ",
Yuan Kang1acebad32011-07-15 11:21:42 +08001437 DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
1438 desc_bytes(edesc->hw_desc), 1);
1439#endif
1440
Yuan Kang0e479302011-07-15 11:21:41 +08001441 desc = edesc->hw_desc;
Yuan Kang1acebad32011-07-15 11:21:42 +08001442 ret = caam_jr_enqueue(jrdev, desc, aead_decrypt_done, req);
1443 if (!ret) {
1444 ret = -EINPROGRESS;
1445 } else {
1446 aead_unmap(jrdev, edesc, req);
1447 kfree(edesc);
1448 }
Yuan Kang0e479302011-07-15 11:21:41 +08001449
Yuan Kang1acebad32011-07-15 11:21:42 +08001450 return ret;
1451}
Yuan Kang0e479302011-07-15 11:21:41 +08001452
Yuan Kang1acebad32011-07-15 11:21:42 +08001453/*
1454 * allocate and map the aead extended descriptor for aead givencrypt
1455 */
1456static struct aead_edesc *aead_giv_edesc_alloc(struct aead_givcrypt_request
1457 *greq, int desc_bytes,
1458 u32 *contig_ptr)
1459{
1460 struct aead_request *req = &greq->areq;
1461 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1462 struct caam_ctx *ctx = crypto_aead_ctx(aead);
1463 struct device *jrdev = ctx->jrdev;
1464 gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
1465 CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
1466 int assoc_nents, src_nents, dst_nents = 0;
1467 struct aead_edesc *edesc;
1468 dma_addr_t iv_dma = 0;
1469 int sgc;
1470 u32 contig = GIV_SRC_CONTIG | GIV_DST_CONTIG;
1471 int ivsize = crypto_aead_ivsize(aead);
Yuan Kang643b39b2012-06-22 19:48:49 -05001472 bool assoc_chained = false, src_chained = false, dst_chained = false;
Yuan Kanga299c832012-06-22 19:48:46 -05001473 int sec4_sg_index, sec4_sg_len = 0, sec4_sg_bytes;
Yuan Kang0e479302011-07-15 11:21:41 +08001474
Yuan Kang643b39b2012-06-22 19:48:49 -05001475 assoc_nents = sg_count(req->assoc, req->assoclen, &assoc_chained);
1476 src_nents = sg_count(req->src, req->cryptlen, &src_chained);
Yuan Kang0e479302011-07-15 11:21:41 +08001477
Yuan Kang1acebad32011-07-15 11:21:42 +08001478 if (unlikely(req->dst != req->src))
Horia Geantabbf9c892013-11-28 15:11:16 +02001479 dst_nents = sg_count(req->dst, req->cryptlen + ctx->authsize,
1480 &dst_chained);
Yuan Kang1acebad32011-07-15 11:21:42 +08001481
Yuan Kang643b39b2012-06-22 19:48:49 -05001482 sgc = dma_map_sg_chained(jrdev, req->assoc, assoc_nents ? : 1,
Horia Geanta286233e2013-05-10 15:08:39 +03001483 DMA_TO_DEVICE, assoc_chained);
Yuan Kang1acebad32011-07-15 11:21:42 +08001484 if (likely(req->src == req->dst)) {
Yuan Kang643b39b2012-06-22 19:48:49 -05001485 sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1,
1486 DMA_BIDIRECTIONAL, src_chained);
Yuan Kang1acebad32011-07-15 11:21:42 +08001487 } else {
Yuan Kang643b39b2012-06-22 19:48:49 -05001488 sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1,
1489 DMA_TO_DEVICE, src_chained);
1490 sgc = dma_map_sg_chained(jrdev, req->dst, dst_nents ? : 1,
1491 DMA_FROM_DEVICE, dst_chained);
Yuan Kang1acebad32011-07-15 11:21:42 +08001492 }
1493
1494 /* Check if data are contiguous */
1495 iv_dma = dma_map_single(jrdev, greq->giv, ivsize, DMA_TO_DEVICE);
1496 if (assoc_nents || sg_dma_address(req->assoc) + req->assoclen !=
1497 iv_dma || src_nents || iv_dma + ivsize != sg_dma_address(req->src))
1498 contig &= ~GIV_SRC_CONTIG;
1499 if (dst_nents || iv_dma + ivsize != sg_dma_address(req->dst))
1500 contig &= ~GIV_DST_CONTIG;
Kim Phillips2af8f4a2012-09-07 04:17:03 +08001501 if (unlikely(req->src != req->dst)) {
1502 dst_nents = dst_nents ? : 1;
1503 sec4_sg_len += 1;
1504 }
Yuan Kang1acebad32011-07-15 11:21:42 +08001505 if (!(contig & GIV_SRC_CONTIG)) {
1506 assoc_nents = assoc_nents ? : 1;
1507 src_nents = src_nents ? : 1;
Yuan Kanga299c832012-06-22 19:48:46 -05001508 sec4_sg_len += assoc_nents + 1 + src_nents;
Yuan Kang1acebad32011-07-15 11:21:42 +08001509 if (likely(req->src == req->dst))
1510 contig &= ~GIV_DST_CONTIG;
1511 }
Yuan Kanga299c832012-06-22 19:48:46 -05001512 sec4_sg_len += dst_nents;
Yuan Kang1acebad32011-07-15 11:21:42 +08001513
Yuan Kanga299c832012-06-22 19:48:46 -05001514 sec4_sg_bytes = sec4_sg_len * sizeof(struct sec4_sg_entry);
Yuan Kang1acebad32011-07-15 11:21:42 +08001515
1516 /* allocate space for base edesc and hw desc commands, link tables */
1517 edesc = kmalloc(sizeof(struct aead_edesc) + desc_bytes +
Yuan Kanga299c832012-06-22 19:48:46 -05001518 sec4_sg_bytes, GFP_DMA | flags);
Yuan Kang1acebad32011-07-15 11:21:42 +08001519 if (!edesc) {
1520 dev_err(jrdev, "could not allocate extended descriptor\n");
1521 return ERR_PTR(-ENOMEM);
1522 }
1523
1524 edesc->assoc_nents = assoc_nents;
Yuan Kang643b39b2012-06-22 19:48:49 -05001525 edesc->assoc_chained = assoc_chained;
Yuan Kang1acebad32011-07-15 11:21:42 +08001526 edesc->src_nents = src_nents;
Yuan Kang643b39b2012-06-22 19:48:49 -05001527 edesc->src_chained = src_chained;
Yuan Kang1acebad32011-07-15 11:21:42 +08001528 edesc->dst_nents = dst_nents;
Yuan Kang643b39b2012-06-22 19:48:49 -05001529 edesc->dst_chained = dst_chained;
Yuan Kang1acebad32011-07-15 11:21:42 +08001530 edesc->iv_dma = iv_dma;
Yuan Kanga299c832012-06-22 19:48:46 -05001531 edesc->sec4_sg_bytes = sec4_sg_bytes;
1532 edesc->sec4_sg = (void *)edesc + sizeof(struct aead_edesc) +
1533 desc_bytes;
1534 edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
1535 sec4_sg_bytes, DMA_TO_DEVICE);
Yuan Kang1acebad32011-07-15 11:21:42 +08001536 *contig_ptr = contig;
1537
Yuan Kanga299c832012-06-22 19:48:46 -05001538 sec4_sg_index = 0;
Yuan Kang1acebad32011-07-15 11:21:42 +08001539 if (!(contig & GIV_SRC_CONTIG)) {
Yuan Kanga299c832012-06-22 19:48:46 -05001540 sg_to_sec4_sg(req->assoc, assoc_nents,
1541 edesc->sec4_sg +
1542 sec4_sg_index, 0);
1543 sec4_sg_index += assoc_nents;
1544 dma_to_sec4_sg_one(edesc->sec4_sg + sec4_sg_index,
Yuan Kang1acebad32011-07-15 11:21:42 +08001545 iv_dma, ivsize, 0);
Yuan Kanga299c832012-06-22 19:48:46 -05001546 sec4_sg_index += 1;
1547 sg_to_sec4_sg_last(req->src, src_nents,
1548 edesc->sec4_sg +
1549 sec4_sg_index, 0);
1550 sec4_sg_index += src_nents;
Yuan Kang1acebad32011-07-15 11:21:42 +08001551 }
1552 if (unlikely(req->src != req->dst && !(contig & GIV_DST_CONTIG))) {
Yuan Kanga299c832012-06-22 19:48:46 -05001553 dma_to_sec4_sg_one(edesc->sec4_sg + sec4_sg_index,
Yuan Kang1acebad32011-07-15 11:21:42 +08001554 iv_dma, ivsize, 0);
Yuan Kanga299c832012-06-22 19:48:46 -05001555 sec4_sg_index += 1;
1556 sg_to_sec4_sg_last(req->dst, dst_nents,
1557 edesc->sec4_sg + sec4_sg_index, 0);
Yuan Kang1acebad32011-07-15 11:21:42 +08001558 }
1559
1560 return edesc;
Yuan Kang0e479302011-07-15 11:21:41 +08001561}
1562
1563static int aead_givencrypt(struct aead_givcrypt_request *areq)
1564{
1565 struct aead_request *req = &areq->areq;
1566 struct aead_edesc *edesc;
1567 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001568 struct caam_ctx *ctx = crypto_aead_ctx(aead);
1569 struct device *jrdev = ctx->jrdev;
Yuan Kang1acebad32011-07-15 11:21:42 +08001570 u32 contig;
Kim Phillips8e8ec592011-03-13 16:54:26 +08001571 u32 *desc;
Yuan Kang1acebad32011-07-15 11:21:42 +08001572 int ret = 0;
Kim Phillips8e8ec592011-03-13 16:54:26 +08001573
Kim Phillips8e8ec592011-03-13 16:54:26 +08001574 /* allocate extended descriptor */
Yuan Kang1acebad32011-07-15 11:21:42 +08001575 edesc = aead_giv_edesc_alloc(areq, DESC_JOB_IO_LEN *
1576 CAAM_CMD_SZ, &contig);
1577
Kim Phillips8e8ec592011-03-13 16:54:26 +08001578 if (IS_ERR(edesc))
1579 return PTR_ERR(edesc);
1580
Yuan Kang1acebad32011-07-15 11:21:42 +08001581#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +03001582 print_hex_dump(KERN_ERR, "giv src@"__stringify(__LINE__)": ",
Yuan Kang1acebad32011-07-15 11:21:42 +08001583 DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
1584 req->cryptlen, 1);
1585#endif
1586
1587 /* Create and submit job descriptor*/
1588 init_aead_giv_job(ctx->sh_desc_givenc,
1589 ctx->sh_desc_givenc_dma, edesc, req, contig);
1590#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +03001591 print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ",
Yuan Kang1acebad32011-07-15 11:21:42 +08001592 DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
1593 desc_bytes(edesc->hw_desc), 1);
1594#endif
1595
Kim Phillips8e8ec592011-03-13 16:54:26 +08001596 desc = edesc->hw_desc;
Yuan Kang1acebad32011-07-15 11:21:42 +08001597 ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req);
1598 if (!ret) {
1599 ret = -EINPROGRESS;
1600 } else {
1601 aead_unmap(jrdev, edesc, req);
1602 kfree(edesc);
1603 }
Kim Phillips8e8ec592011-03-13 16:54:26 +08001604
Yuan Kang1acebad32011-07-15 11:21:42 +08001605 return ret;
Kim Phillips8e8ec592011-03-13 16:54:26 +08001606}
1607
Horia Geantaae4a8252014-03-14 17:46:52 +02001608static int aead_null_givencrypt(struct aead_givcrypt_request *areq)
1609{
1610 return aead_encrypt(&areq->areq);
1611}
1612
Yuan Kangacdca312011-07-15 11:21:42 +08001613/*
1614 * allocate and map the ablkcipher extended descriptor for ablkcipher
1615 */
1616static struct ablkcipher_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request
1617 *req, int desc_bytes,
1618 bool *iv_contig_out)
1619{
1620 struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
1621 struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
1622 struct device *jrdev = ctx->jrdev;
1623 gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
1624 CRYPTO_TFM_REQ_MAY_SLEEP)) ?
1625 GFP_KERNEL : GFP_ATOMIC;
Yuan Kanga299c832012-06-22 19:48:46 -05001626 int src_nents, dst_nents = 0, sec4_sg_bytes;
Yuan Kangacdca312011-07-15 11:21:42 +08001627 struct ablkcipher_edesc *edesc;
1628 dma_addr_t iv_dma = 0;
1629 bool iv_contig = false;
1630 int sgc;
1631 int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
Yuan Kang643b39b2012-06-22 19:48:49 -05001632 bool src_chained = false, dst_chained = false;
Yuan Kanga299c832012-06-22 19:48:46 -05001633 int sec4_sg_index;
Yuan Kangacdca312011-07-15 11:21:42 +08001634
Yuan Kang643b39b2012-06-22 19:48:49 -05001635 src_nents = sg_count(req->src, req->nbytes, &src_chained);
Yuan Kangacdca312011-07-15 11:21:42 +08001636
Yuan Kang643b39b2012-06-22 19:48:49 -05001637 if (req->dst != req->src)
1638 dst_nents = sg_count(req->dst, req->nbytes, &dst_chained);
Yuan Kangacdca312011-07-15 11:21:42 +08001639
1640 if (likely(req->src == req->dst)) {
Yuan Kang643b39b2012-06-22 19:48:49 -05001641 sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1,
1642 DMA_BIDIRECTIONAL, src_chained);
Yuan Kangacdca312011-07-15 11:21:42 +08001643 } else {
Yuan Kang643b39b2012-06-22 19:48:49 -05001644 sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1,
1645 DMA_TO_DEVICE, src_chained);
1646 sgc = dma_map_sg_chained(jrdev, req->dst, dst_nents ? : 1,
1647 DMA_FROM_DEVICE, dst_chained);
Yuan Kangacdca312011-07-15 11:21:42 +08001648 }
1649
1650 /*
1651 * Check if iv can be contiguous with source and destination.
1652 * If so, include it. If not, create scatterlist.
1653 */
1654 iv_dma = dma_map_single(jrdev, req->info, ivsize, DMA_TO_DEVICE);
1655 if (!src_nents && iv_dma + ivsize == sg_dma_address(req->src))
1656 iv_contig = true;
1657 else
1658 src_nents = src_nents ? : 1;
Yuan Kanga299c832012-06-22 19:48:46 -05001659 sec4_sg_bytes = ((iv_contig ? 0 : 1) + src_nents + dst_nents) *
1660 sizeof(struct sec4_sg_entry);
Yuan Kangacdca312011-07-15 11:21:42 +08001661
1662 /* allocate space for base edesc and hw desc commands, link tables */
1663 edesc = kmalloc(sizeof(struct ablkcipher_edesc) + desc_bytes +
Yuan Kanga299c832012-06-22 19:48:46 -05001664 sec4_sg_bytes, GFP_DMA | flags);
Yuan Kangacdca312011-07-15 11:21:42 +08001665 if (!edesc) {
1666 dev_err(jrdev, "could not allocate extended descriptor\n");
1667 return ERR_PTR(-ENOMEM);
1668 }
1669
1670 edesc->src_nents = src_nents;
Yuan Kang643b39b2012-06-22 19:48:49 -05001671 edesc->src_chained = src_chained;
Yuan Kangacdca312011-07-15 11:21:42 +08001672 edesc->dst_nents = dst_nents;
Yuan Kang643b39b2012-06-22 19:48:49 -05001673 edesc->dst_chained = dst_chained;
Yuan Kanga299c832012-06-22 19:48:46 -05001674 edesc->sec4_sg_bytes = sec4_sg_bytes;
1675 edesc->sec4_sg = (void *)edesc + sizeof(struct ablkcipher_edesc) +
1676 desc_bytes;
Yuan Kangacdca312011-07-15 11:21:42 +08001677
Yuan Kanga299c832012-06-22 19:48:46 -05001678 sec4_sg_index = 0;
Yuan Kangacdca312011-07-15 11:21:42 +08001679 if (!iv_contig) {
Yuan Kanga299c832012-06-22 19:48:46 -05001680 dma_to_sec4_sg_one(edesc->sec4_sg, iv_dma, ivsize, 0);
1681 sg_to_sec4_sg_last(req->src, src_nents,
1682 edesc->sec4_sg + 1, 0);
1683 sec4_sg_index += 1 + src_nents;
Yuan Kangacdca312011-07-15 11:21:42 +08001684 }
1685
Yuan Kang643b39b2012-06-22 19:48:49 -05001686 if (dst_nents) {
Yuan Kanga299c832012-06-22 19:48:46 -05001687 sg_to_sec4_sg_last(req->dst, dst_nents,
1688 edesc->sec4_sg + sec4_sg_index, 0);
Yuan Kangacdca312011-07-15 11:21:42 +08001689 }
1690
Yuan Kanga299c832012-06-22 19:48:46 -05001691 edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
1692 sec4_sg_bytes, DMA_TO_DEVICE);
Yuan Kangacdca312011-07-15 11:21:42 +08001693 edesc->iv_dma = iv_dma;
1694
1695#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +03001696 print_hex_dump(KERN_ERR, "ablkcipher sec4_sg@"__stringify(__LINE__)": ",
Yuan Kanga299c832012-06-22 19:48:46 -05001697 DUMP_PREFIX_ADDRESS, 16, 4, edesc->sec4_sg,
1698 sec4_sg_bytes, 1);
Yuan Kangacdca312011-07-15 11:21:42 +08001699#endif
1700
1701 *iv_contig_out = iv_contig;
1702 return edesc;
1703}
1704
1705static int ablkcipher_encrypt(struct ablkcipher_request *req)
1706{
1707 struct ablkcipher_edesc *edesc;
1708 struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
1709 struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
1710 struct device *jrdev = ctx->jrdev;
1711 bool iv_contig;
1712 u32 *desc;
1713 int ret = 0;
1714
1715 /* allocate extended descriptor */
1716 edesc = ablkcipher_edesc_alloc(req, DESC_JOB_IO_LEN *
1717 CAAM_CMD_SZ, &iv_contig);
1718 if (IS_ERR(edesc))
1719 return PTR_ERR(edesc);
1720
1721 /* Create and submit job descriptor*/
1722 init_ablkcipher_job(ctx->sh_desc_enc,
1723 ctx->sh_desc_enc_dma, edesc, req, iv_contig);
1724#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +03001725 print_hex_dump(KERN_ERR, "ablkcipher jobdesc@"__stringify(__LINE__)": ",
Yuan Kangacdca312011-07-15 11:21:42 +08001726 DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
1727 desc_bytes(edesc->hw_desc), 1);
1728#endif
1729 desc = edesc->hw_desc;
1730 ret = caam_jr_enqueue(jrdev, desc, ablkcipher_encrypt_done, req);
1731
1732 if (!ret) {
1733 ret = -EINPROGRESS;
1734 } else {
1735 ablkcipher_unmap(jrdev, edesc, req);
1736 kfree(edesc);
1737 }
1738
1739 return ret;
1740}
1741
1742static int ablkcipher_decrypt(struct ablkcipher_request *req)
1743{
1744 struct ablkcipher_edesc *edesc;
1745 struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
1746 struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
1747 struct device *jrdev = ctx->jrdev;
1748 bool iv_contig;
1749 u32 *desc;
1750 int ret = 0;
1751
1752 /* allocate extended descriptor */
1753 edesc = ablkcipher_edesc_alloc(req, DESC_JOB_IO_LEN *
1754 CAAM_CMD_SZ, &iv_contig);
1755 if (IS_ERR(edesc))
1756 return PTR_ERR(edesc);
1757
1758 /* Create and submit job descriptor*/
1759 init_ablkcipher_job(ctx->sh_desc_dec,
1760 ctx->sh_desc_dec_dma, edesc, req, iv_contig);
1761 desc = edesc->hw_desc;
1762#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +03001763 print_hex_dump(KERN_ERR, "ablkcipher jobdesc@"__stringify(__LINE__)": ",
Yuan Kangacdca312011-07-15 11:21:42 +08001764 DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
1765 desc_bytes(edesc->hw_desc), 1);
1766#endif
1767
1768 ret = caam_jr_enqueue(jrdev, desc, ablkcipher_decrypt_done, req);
1769 if (!ret) {
1770 ret = -EINPROGRESS;
1771 } else {
1772 ablkcipher_unmap(jrdev, edesc, req);
1773 kfree(edesc);
1774 }
1775
1776 return ret;
1777}
1778
Yuan Kang885e9e22011-07-15 11:21:41 +08001779#define template_aead template_u.aead
Yuan Kangacdca312011-07-15 11:21:42 +08001780#define template_ablkcipher template_u.ablkcipher
Kim Phillips8e8ec592011-03-13 16:54:26 +08001781struct caam_alg_template {
1782 char name[CRYPTO_MAX_ALG_NAME];
1783 char driver_name[CRYPTO_MAX_ALG_NAME];
1784 unsigned int blocksize;
Yuan Kang885e9e22011-07-15 11:21:41 +08001785 u32 type;
1786 union {
1787 struct ablkcipher_alg ablkcipher;
1788 struct aead_alg aead;
1789 struct blkcipher_alg blkcipher;
1790 struct cipher_alg cipher;
1791 struct compress_alg compress;
1792 struct rng_alg rng;
1793 } template_u;
Kim Phillips8e8ec592011-03-13 16:54:26 +08001794 u32 class1_alg_type;
1795 u32 class2_alg_type;
1796 u32 alg_op;
1797};
1798
1799static struct caam_alg_template driver_algs[] = {
Horia Geanta246bbed2013-03-20 16:31:58 +02001800 /* single-pass ipsec_esp descriptor */
Kim Phillips8e8ec592011-03-13 16:54:26 +08001801 {
Horia Geantaae4a8252014-03-14 17:46:52 +02001802 .name = "authenc(hmac(md5),ecb(cipher_null))",
1803 .driver_name = "authenc-hmac-md5-ecb-cipher_null-caam",
1804 .blocksize = NULL_BLOCK_SIZE,
1805 .type = CRYPTO_ALG_TYPE_AEAD,
1806 .template_aead = {
1807 .setkey = aead_setkey,
1808 .setauthsize = aead_setauthsize,
1809 .encrypt = aead_encrypt,
1810 .decrypt = aead_decrypt,
1811 .givencrypt = aead_null_givencrypt,
1812 .geniv = "<built-in>",
1813 .ivsize = NULL_IV_SIZE,
1814 .maxauthsize = MD5_DIGEST_SIZE,
1815 },
1816 .class1_alg_type = 0,
1817 .class2_alg_type = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC_PRECOMP,
1818 .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
1819 },
1820 {
1821 .name = "authenc(hmac(sha1),ecb(cipher_null))",
1822 .driver_name = "authenc-hmac-sha1-ecb-cipher_null-caam",
1823 .blocksize = NULL_BLOCK_SIZE,
1824 .type = CRYPTO_ALG_TYPE_AEAD,
1825 .template_aead = {
1826 .setkey = aead_setkey,
1827 .setauthsize = aead_setauthsize,
1828 .encrypt = aead_encrypt,
1829 .decrypt = aead_decrypt,
1830 .givencrypt = aead_null_givencrypt,
1831 .geniv = "<built-in>",
1832 .ivsize = NULL_IV_SIZE,
1833 .maxauthsize = SHA1_DIGEST_SIZE,
1834 },
1835 .class1_alg_type = 0,
1836 .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP,
1837 .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
1838 },
1839 {
1840 .name = "authenc(hmac(sha224),ecb(cipher_null))",
1841 .driver_name = "authenc-hmac-sha224-ecb-cipher_null-caam",
1842 .blocksize = NULL_BLOCK_SIZE,
1843 .type = CRYPTO_ALG_TYPE_AEAD,
1844 .template_aead = {
1845 .setkey = aead_setkey,
1846 .setauthsize = aead_setauthsize,
1847 .encrypt = aead_encrypt,
1848 .decrypt = aead_decrypt,
1849 .givencrypt = aead_null_givencrypt,
1850 .geniv = "<built-in>",
1851 .ivsize = NULL_IV_SIZE,
1852 .maxauthsize = SHA224_DIGEST_SIZE,
1853 },
1854 .class1_alg_type = 0,
1855 .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
1856 OP_ALG_AAI_HMAC_PRECOMP,
1857 .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
1858 },
1859 {
1860 .name = "authenc(hmac(sha256),ecb(cipher_null))",
1861 .driver_name = "authenc-hmac-sha256-ecb-cipher_null-caam",
1862 .blocksize = NULL_BLOCK_SIZE,
1863 .type = CRYPTO_ALG_TYPE_AEAD,
1864 .template_aead = {
1865 .setkey = aead_setkey,
1866 .setauthsize = aead_setauthsize,
1867 .encrypt = aead_encrypt,
1868 .decrypt = aead_decrypt,
1869 .givencrypt = aead_null_givencrypt,
1870 .geniv = "<built-in>",
1871 .ivsize = NULL_IV_SIZE,
1872 .maxauthsize = SHA256_DIGEST_SIZE,
1873 },
1874 .class1_alg_type = 0,
1875 .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
1876 OP_ALG_AAI_HMAC_PRECOMP,
1877 .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
1878 },
1879 {
1880 .name = "authenc(hmac(sha384),ecb(cipher_null))",
1881 .driver_name = "authenc-hmac-sha384-ecb-cipher_null-caam",
1882 .blocksize = NULL_BLOCK_SIZE,
1883 .type = CRYPTO_ALG_TYPE_AEAD,
1884 .template_aead = {
1885 .setkey = aead_setkey,
1886 .setauthsize = aead_setauthsize,
1887 .encrypt = aead_encrypt,
1888 .decrypt = aead_decrypt,
1889 .givencrypt = aead_null_givencrypt,
1890 .geniv = "<built-in>",
1891 .ivsize = NULL_IV_SIZE,
1892 .maxauthsize = SHA384_DIGEST_SIZE,
1893 },
1894 .class1_alg_type = 0,
1895 .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
1896 OP_ALG_AAI_HMAC_PRECOMP,
1897 .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
1898 },
1899 {
1900 .name = "authenc(hmac(sha512),ecb(cipher_null))",
1901 .driver_name = "authenc-hmac-sha512-ecb-cipher_null-caam",
1902 .blocksize = NULL_BLOCK_SIZE,
1903 .type = CRYPTO_ALG_TYPE_AEAD,
1904 .template_aead = {
1905 .setkey = aead_setkey,
1906 .setauthsize = aead_setauthsize,
1907 .encrypt = aead_encrypt,
1908 .decrypt = aead_decrypt,
1909 .givencrypt = aead_null_givencrypt,
1910 .geniv = "<built-in>",
1911 .ivsize = NULL_IV_SIZE,
1912 .maxauthsize = SHA512_DIGEST_SIZE,
1913 },
1914 .class1_alg_type = 0,
1915 .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
1916 OP_ALG_AAI_HMAC_PRECOMP,
1917 .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
1918 },
1919 {
Kim Phillips8b4d43a2011-11-21 16:13:27 +08001920 .name = "authenc(hmac(md5),cbc(aes))",
1921 .driver_name = "authenc-hmac-md5-cbc-aes-caam",
1922 .blocksize = AES_BLOCK_SIZE,
1923 .type = CRYPTO_ALG_TYPE_AEAD,
1924 .template_aead = {
1925 .setkey = aead_setkey,
1926 .setauthsize = aead_setauthsize,
1927 .encrypt = aead_encrypt,
1928 .decrypt = aead_decrypt,
1929 .givencrypt = aead_givencrypt,
1930 .geniv = "<built-in>",
1931 .ivsize = AES_BLOCK_SIZE,
1932 .maxauthsize = MD5_DIGEST_SIZE,
1933 },
1934 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
1935 .class2_alg_type = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC_PRECOMP,
1936 .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
1937 },
1938 {
Kim Phillips8e8ec592011-03-13 16:54:26 +08001939 .name = "authenc(hmac(sha1),cbc(aes))",
1940 .driver_name = "authenc-hmac-sha1-cbc-aes-caam",
1941 .blocksize = AES_BLOCK_SIZE,
Yuan Kang885e9e22011-07-15 11:21:41 +08001942 .type = CRYPTO_ALG_TYPE_AEAD,
1943 .template_aead = {
Yuan Kang0e479302011-07-15 11:21:41 +08001944 .setkey = aead_setkey,
1945 .setauthsize = aead_setauthsize,
1946 .encrypt = aead_encrypt,
1947 .decrypt = aead_decrypt,
1948 .givencrypt = aead_givencrypt,
Kim Phillips8e8ec592011-03-13 16:54:26 +08001949 .geniv = "<built-in>",
1950 .ivsize = AES_BLOCK_SIZE,
1951 .maxauthsize = SHA1_DIGEST_SIZE,
1952 },
1953 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
1954 .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP,
1955 .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
1956 },
1957 {
Hemant Agrawale863f9cc2012-01-09 18:26:44 -06001958 .name = "authenc(hmac(sha224),cbc(aes))",
1959 .driver_name = "authenc-hmac-sha224-cbc-aes-caam",
1960 .blocksize = AES_BLOCK_SIZE,
Vakul Gargcb7d5662013-03-12 14:09:24 +05301961 .type = CRYPTO_ALG_TYPE_AEAD,
Hemant Agrawale863f9cc2012-01-09 18:26:44 -06001962 .template_aead = {
1963 .setkey = aead_setkey,
1964 .setauthsize = aead_setauthsize,
1965 .encrypt = aead_encrypt,
1966 .decrypt = aead_decrypt,
1967 .givencrypt = aead_givencrypt,
1968 .geniv = "<built-in>",
1969 .ivsize = AES_BLOCK_SIZE,
1970 .maxauthsize = SHA224_DIGEST_SIZE,
1971 },
1972 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
1973 .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
1974 OP_ALG_AAI_HMAC_PRECOMP,
1975 .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
1976 },
1977 {
Kim Phillips8e8ec592011-03-13 16:54:26 +08001978 .name = "authenc(hmac(sha256),cbc(aes))",
1979 .driver_name = "authenc-hmac-sha256-cbc-aes-caam",
1980 .blocksize = AES_BLOCK_SIZE,
Yuan Kang885e9e22011-07-15 11:21:41 +08001981 .type = CRYPTO_ALG_TYPE_AEAD,
1982 .template_aead = {
Yuan Kang0e479302011-07-15 11:21:41 +08001983 .setkey = aead_setkey,
1984 .setauthsize = aead_setauthsize,
1985 .encrypt = aead_encrypt,
1986 .decrypt = aead_decrypt,
1987 .givencrypt = aead_givencrypt,
Kim Phillips8e8ec592011-03-13 16:54:26 +08001988 .geniv = "<built-in>",
1989 .ivsize = AES_BLOCK_SIZE,
1990 .maxauthsize = SHA256_DIGEST_SIZE,
1991 },
1992 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
1993 .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
1994 OP_ALG_AAI_HMAC_PRECOMP,
1995 .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
1996 },
1997 {
Hemant Agrawale863f9cc2012-01-09 18:26:44 -06001998 .name = "authenc(hmac(sha384),cbc(aes))",
1999 .driver_name = "authenc-hmac-sha384-cbc-aes-caam",
2000 .blocksize = AES_BLOCK_SIZE,
Vakul Gargcb7d5662013-03-12 14:09:24 +05302001 .type = CRYPTO_ALG_TYPE_AEAD,
Hemant Agrawale863f9cc2012-01-09 18:26:44 -06002002 .template_aead = {
2003 .setkey = aead_setkey,
2004 .setauthsize = aead_setauthsize,
2005 .encrypt = aead_encrypt,
2006 .decrypt = aead_decrypt,
2007 .givencrypt = aead_givencrypt,
2008 .geniv = "<built-in>",
2009 .ivsize = AES_BLOCK_SIZE,
2010 .maxauthsize = SHA384_DIGEST_SIZE,
2011 },
2012 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
2013 .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
2014 OP_ALG_AAI_HMAC_PRECOMP,
2015 .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
2016 },
2017
2018 {
Kim Phillips4427b1b2011-05-14 22:08:17 -05002019 .name = "authenc(hmac(sha512),cbc(aes))",
2020 .driver_name = "authenc-hmac-sha512-cbc-aes-caam",
2021 .blocksize = AES_BLOCK_SIZE,
Yuan Kang885e9e22011-07-15 11:21:41 +08002022 .type = CRYPTO_ALG_TYPE_AEAD,
2023 .template_aead = {
Yuan Kang0e479302011-07-15 11:21:41 +08002024 .setkey = aead_setkey,
2025 .setauthsize = aead_setauthsize,
2026 .encrypt = aead_encrypt,
2027 .decrypt = aead_decrypt,
2028 .givencrypt = aead_givencrypt,
Kim Phillips4427b1b2011-05-14 22:08:17 -05002029 .geniv = "<built-in>",
2030 .ivsize = AES_BLOCK_SIZE,
2031 .maxauthsize = SHA512_DIGEST_SIZE,
2032 },
2033 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
2034 .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
2035 OP_ALG_AAI_HMAC_PRECOMP,
2036 .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
2037 },
2038 {
Kim Phillips8b4d43a2011-11-21 16:13:27 +08002039 .name = "authenc(hmac(md5),cbc(des3_ede))",
2040 .driver_name = "authenc-hmac-md5-cbc-des3_ede-caam",
2041 .blocksize = DES3_EDE_BLOCK_SIZE,
2042 .type = CRYPTO_ALG_TYPE_AEAD,
2043 .template_aead = {
2044 .setkey = aead_setkey,
2045 .setauthsize = aead_setauthsize,
2046 .encrypt = aead_encrypt,
2047 .decrypt = aead_decrypt,
2048 .givencrypt = aead_givencrypt,
2049 .geniv = "<built-in>",
2050 .ivsize = DES3_EDE_BLOCK_SIZE,
2051 .maxauthsize = MD5_DIGEST_SIZE,
2052 },
2053 .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
2054 .class2_alg_type = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC_PRECOMP,
2055 .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
2056 },
2057 {
Kim Phillips8e8ec592011-03-13 16:54:26 +08002058 .name = "authenc(hmac(sha1),cbc(des3_ede))",
2059 .driver_name = "authenc-hmac-sha1-cbc-des3_ede-caam",
2060 .blocksize = DES3_EDE_BLOCK_SIZE,
Yuan Kang885e9e22011-07-15 11:21:41 +08002061 .type = CRYPTO_ALG_TYPE_AEAD,
2062 .template_aead = {
Yuan Kang0e479302011-07-15 11:21:41 +08002063 .setkey = aead_setkey,
2064 .setauthsize = aead_setauthsize,
2065 .encrypt = aead_encrypt,
2066 .decrypt = aead_decrypt,
2067 .givencrypt = aead_givencrypt,
Kim Phillips8e8ec592011-03-13 16:54:26 +08002068 .geniv = "<built-in>",
2069 .ivsize = DES3_EDE_BLOCK_SIZE,
2070 .maxauthsize = SHA1_DIGEST_SIZE,
2071 },
2072 .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
2073 .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP,
2074 .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
2075 },
2076 {
Hemant Agrawale863f9cc2012-01-09 18:26:44 -06002077 .name = "authenc(hmac(sha224),cbc(des3_ede))",
2078 .driver_name = "authenc-hmac-sha224-cbc-des3_ede-caam",
2079 .blocksize = DES3_EDE_BLOCK_SIZE,
Vakul Gargcb7d5662013-03-12 14:09:24 +05302080 .type = CRYPTO_ALG_TYPE_AEAD,
Hemant Agrawale863f9cc2012-01-09 18:26:44 -06002081 .template_aead = {
2082 .setkey = aead_setkey,
2083 .setauthsize = aead_setauthsize,
2084 .encrypt = aead_encrypt,
2085 .decrypt = aead_decrypt,
2086 .givencrypt = aead_givencrypt,
2087 .geniv = "<built-in>",
2088 .ivsize = DES3_EDE_BLOCK_SIZE,
2089 .maxauthsize = SHA224_DIGEST_SIZE,
2090 },
2091 .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
2092 .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
2093 OP_ALG_AAI_HMAC_PRECOMP,
2094 .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
2095 },
2096 {
Kim Phillips8e8ec592011-03-13 16:54:26 +08002097 .name = "authenc(hmac(sha256),cbc(des3_ede))",
2098 .driver_name = "authenc-hmac-sha256-cbc-des3_ede-caam",
2099 .blocksize = DES3_EDE_BLOCK_SIZE,
Yuan Kang885e9e22011-07-15 11:21:41 +08002100 .type = CRYPTO_ALG_TYPE_AEAD,
2101 .template_aead = {
Yuan Kang0e479302011-07-15 11:21:41 +08002102 .setkey = aead_setkey,
2103 .setauthsize = aead_setauthsize,
2104 .encrypt = aead_encrypt,
2105 .decrypt = aead_decrypt,
2106 .givencrypt = aead_givencrypt,
Kim Phillips8e8ec592011-03-13 16:54:26 +08002107 .geniv = "<built-in>",
2108 .ivsize = DES3_EDE_BLOCK_SIZE,
2109 .maxauthsize = SHA256_DIGEST_SIZE,
2110 },
2111 .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
2112 .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
2113 OP_ALG_AAI_HMAC_PRECOMP,
2114 .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
2115 },
2116 {
Hemant Agrawale863f9cc2012-01-09 18:26:44 -06002117 .name = "authenc(hmac(sha384),cbc(des3_ede))",
2118 .driver_name = "authenc-hmac-sha384-cbc-des3_ede-caam",
2119 .blocksize = DES3_EDE_BLOCK_SIZE,
Vakul Gargcb7d5662013-03-12 14:09:24 +05302120 .type = CRYPTO_ALG_TYPE_AEAD,
Hemant Agrawale863f9cc2012-01-09 18:26:44 -06002121 .template_aead = {
2122 .setkey = aead_setkey,
2123 .setauthsize = aead_setauthsize,
2124 .encrypt = aead_encrypt,
2125 .decrypt = aead_decrypt,
2126 .givencrypt = aead_givencrypt,
2127 .geniv = "<built-in>",
2128 .ivsize = DES3_EDE_BLOCK_SIZE,
2129 .maxauthsize = SHA384_DIGEST_SIZE,
2130 },
2131 .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
2132 .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
2133 OP_ALG_AAI_HMAC_PRECOMP,
2134 .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
2135 },
2136 {
Kim Phillips4427b1b2011-05-14 22:08:17 -05002137 .name = "authenc(hmac(sha512),cbc(des3_ede))",
2138 .driver_name = "authenc-hmac-sha512-cbc-des3_ede-caam",
2139 .blocksize = DES3_EDE_BLOCK_SIZE,
Yuan Kang885e9e22011-07-15 11:21:41 +08002140 .type = CRYPTO_ALG_TYPE_AEAD,
2141 .template_aead = {
Yuan Kang0e479302011-07-15 11:21:41 +08002142 .setkey = aead_setkey,
2143 .setauthsize = aead_setauthsize,
2144 .encrypt = aead_encrypt,
2145 .decrypt = aead_decrypt,
2146 .givencrypt = aead_givencrypt,
Kim Phillips4427b1b2011-05-14 22:08:17 -05002147 .geniv = "<built-in>",
2148 .ivsize = DES3_EDE_BLOCK_SIZE,
2149 .maxauthsize = SHA512_DIGEST_SIZE,
2150 },
2151 .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
2152 .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
2153 OP_ALG_AAI_HMAC_PRECOMP,
2154 .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
2155 },
2156 {
Kim Phillips8b4d43a2011-11-21 16:13:27 +08002157 .name = "authenc(hmac(md5),cbc(des))",
2158 .driver_name = "authenc-hmac-md5-cbc-des-caam",
2159 .blocksize = DES_BLOCK_SIZE,
2160 .type = CRYPTO_ALG_TYPE_AEAD,
2161 .template_aead = {
2162 .setkey = aead_setkey,
2163 .setauthsize = aead_setauthsize,
2164 .encrypt = aead_encrypt,
2165 .decrypt = aead_decrypt,
2166 .givencrypt = aead_givencrypt,
2167 .geniv = "<built-in>",
2168 .ivsize = DES_BLOCK_SIZE,
2169 .maxauthsize = MD5_DIGEST_SIZE,
2170 },
2171 .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
2172 .class2_alg_type = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC_PRECOMP,
2173 .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
2174 },
2175 {
Kim Phillips8e8ec592011-03-13 16:54:26 +08002176 .name = "authenc(hmac(sha1),cbc(des))",
2177 .driver_name = "authenc-hmac-sha1-cbc-des-caam",
2178 .blocksize = DES_BLOCK_SIZE,
Yuan Kang885e9e22011-07-15 11:21:41 +08002179 .type = CRYPTO_ALG_TYPE_AEAD,
2180 .template_aead = {
Yuan Kang0e479302011-07-15 11:21:41 +08002181 .setkey = aead_setkey,
2182 .setauthsize = aead_setauthsize,
2183 .encrypt = aead_encrypt,
2184 .decrypt = aead_decrypt,
2185 .givencrypt = aead_givencrypt,
Kim Phillips8e8ec592011-03-13 16:54:26 +08002186 .geniv = "<built-in>",
2187 .ivsize = DES_BLOCK_SIZE,
2188 .maxauthsize = SHA1_DIGEST_SIZE,
2189 },
2190 .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
2191 .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP,
2192 .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
2193 },
2194 {
Hemant Agrawale863f9cc2012-01-09 18:26:44 -06002195 .name = "authenc(hmac(sha224),cbc(des))",
2196 .driver_name = "authenc-hmac-sha224-cbc-des-caam",
2197 .blocksize = DES_BLOCK_SIZE,
Vakul Gargcb7d5662013-03-12 14:09:24 +05302198 .type = CRYPTO_ALG_TYPE_AEAD,
Hemant Agrawale863f9cc2012-01-09 18:26:44 -06002199 .template_aead = {
2200 .setkey = aead_setkey,
2201 .setauthsize = aead_setauthsize,
2202 .encrypt = aead_encrypt,
2203 .decrypt = aead_decrypt,
2204 .givencrypt = aead_givencrypt,
2205 .geniv = "<built-in>",
2206 .ivsize = DES_BLOCK_SIZE,
2207 .maxauthsize = SHA224_DIGEST_SIZE,
2208 },
2209 .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
2210 .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
2211 OP_ALG_AAI_HMAC_PRECOMP,
2212 .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
2213 },
2214 {
Kim Phillips8e8ec592011-03-13 16:54:26 +08002215 .name = "authenc(hmac(sha256),cbc(des))",
2216 .driver_name = "authenc-hmac-sha256-cbc-des-caam",
2217 .blocksize = DES_BLOCK_SIZE,
Yuan Kang885e9e22011-07-15 11:21:41 +08002218 .type = CRYPTO_ALG_TYPE_AEAD,
2219 .template_aead = {
Yuan Kang0e479302011-07-15 11:21:41 +08002220 .setkey = aead_setkey,
2221 .setauthsize = aead_setauthsize,
2222 .encrypt = aead_encrypt,
2223 .decrypt = aead_decrypt,
2224 .givencrypt = aead_givencrypt,
Kim Phillips8e8ec592011-03-13 16:54:26 +08002225 .geniv = "<built-in>",
2226 .ivsize = DES_BLOCK_SIZE,
2227 .maxauthsize = SHA256_DIGEST_SIZE,
2228 },
2229 .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
2230 .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
2231 OP_ALG_AAI_HMAC_PRECOMP,
2232 .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
2233 },
Kim Phillips4427b1b2011-05-14 22:08:17 -05002234 {
Hemant Agrawale863f9cc2012-01-09 18:26:44 -06002235 .name = "authenc(hmac(sha384),cbc(des))",
2236 .driver_name = "authenc-hmac-sha384-cbc-des-caam",
2237 .blocksize = DES_BLOCK_SIZE,
Vakul Gargcb7d5662013-03-12 14:09:24 +05302238 .type = CRYPTO_ALG_TYPE_AEAD,
Hemant Agrawale863f9cc2012-01-09 18:26:44 -06002239 .template_aead = {
2240 .setkey = aead_setkey,
2241 .setauthsize = aead_setauthsize,
2242 .encrypt = aead_encrypt,
2243 .decrypt = aead_decrypt,
2244 .givencrypt = aead_givencrypt,
2245 .geniv = "<built-in>",
2246 .ivsize = DES_BLOCK_SIZE,
2247 .maxauthsize = SHA384_DIGEST_SIZE,
2248 },
2249 .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
2250 .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
2251 OP_ALG_AAI_HMAC_PRECOMP,
2252 .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
2253 },
2254 {
Kim Phillips4427b1b2011-05-14 22:08:17 -05002255 .name = "authenc(hmac(sha512),cbc(des))",
2256 .driver_name = "authenc-hmac-sha512-cbc-des-caam",
2257 .blocksize = DES_BLOCK_SIZE,
Yuan Kang885e9e22011-07-15 11:21:41 +08002258 .type = CRYPTO_ALG_TYPE_AEAD,
2259 .template_aead = {
Yuan Kang0e479302011-07-15 11:21:41 +08002260 .setkey = aead_setkey,
2261 .setauthsize = aead_setauthsize,
2262 .encrypt = aead_encrypt,
2263 .decrypt = aead_decrypt,
2264 .givencrypt = aead_givencrypt,
Kim Phillips4427b1b2011-05-14 22:08:17 -05002265 .geniv = "<built-in>",
2266 .ivsize = DES_BLOCK_SIZE,
2267 .maxauthsize = SHA512_DIGEST_SIZE,
2268 },
2269 .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
2270 .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
2271 OP_ALG_AAI_HMAC_PRECOMP,
2272 .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
2273 },
Yuan Kangacdca312011-07-15 11:21:42 +08002274 /* ablkcipher descriptor */
2275 {
2276 .name = "cbc(aes)",
2277 .driver_name = "cbc-aes-caam",
2278 .blocksize = AES_BLOCK_SIZE,
2279 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2280 .template_ablkcipher = {
2281 .setkey = ablkcipher_setkey,
2282 .encrypt = ablkcipher_encrypt,
2283 .decrypt = ablkcipher_decrypt,
2284 .geniv = "eseqiv",
2285 .min_keysize = AES_MIN_KEY_SIZE,
2286 .max_keysize = AES_MAX_KEY_SIZE,
2287 .ivsize = AES_BLOCK_SIZE,
2288 },
2289 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
2290 },
2291 {
2292 .name = "cbc(des3_ede)",
2293 .driver_name = "cbc-3des-caam",
2294 .blocksize = DES3_EDE_BLOCK_SIZE,
2295 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2296 .template_ablkcipher = {
2297 .setkey = ablkcipher_setkey,
2298 .encrypt = ablkcipher_encrypt,
2299 .decrypt = ablkcipher_decrypt,
2300 .geniv = "eseqiv",
2301 .min_keysize = DES3_EDE_KEY_SIZE,
2302 .max_keysize = DES3_EDE_KEY_SIZE,
2303 .ivsize = DES3_EDE_BLOCK_SIZE,
2304 },
2305 .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
2306 },
2307 {
2308 .name = "cbc(des)",
2309 .driver_name = "cbc-des-caam",
2310 .blocksize = DES_BLOCK_SIZE,
2311 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2312 .template_ablkcipher = {
2313 .setkey = ablkcipher_setkey,
2314 .encrypt = ablkcipher_encrypt,
2315 .decrypt = ablkcipher_decrypt,
2316 .geniv = "eseqiv",
2317 .min_keysize = DES_KEY_SIZE,
2318 .max_keysize = DES_KEY_SIZE,
2319 .ivsize = DES_BLOCK_SIZE,
2320 },
2321 .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
2322 }
Kim Phillips8e8ec592011-03-13 16:54:26 +08002323};
2324
2325struct caam_crypto_alg {
2326 struct list_head entry;
Kim Phillips8e8ec592011-03-13 16:54:26 +08002327 int class1_alg_type;
2328 int class2_alg_type;
2329 int alg_op;
2330 struct crypto_alg crypto_alg;
2331};
2332
2333static int caam_cra_init(struct crypto_tfm *tfm)
2334{
2335 struct crypto_alg *alg = tfm->__crt_alg;
2336 struct caam_crypto_alg *caam_alg =
2337 container_of(alg, struct caam_crypto_alg, crypto_alg);
2338 struct caam_ctx *ctx = crypto_tfm_ctx(tfm);
Kim Phillips8e8ec592011-03-13 16:54:26 +08002339
Ruchika Guptacfc6f112013-10-25 12:01:03 +05302340 ctx->jrdev = caam_jr_alloc();
2341 if (IS_ERR(ctx->jrdev)) {
2342 pr_err("Job Ring Device allocation for transform failed\n");
2343 return PTR_ERR(ctx->jrdev);
2344 }
Kim Phillips8e8ec592011-03-13 16:54:26 +08002345
2346 /* copy descriptor header template value */
2347 ctx->class1_alg_type = OP_TYPE_CLASS1_ALG | caam_alg->class1_alg_type;
2348 ctx->class2_alg_type = OP_TYPE_CLASS2_ALG | caam_alg->class2_alg_type;
2349 ctx->alg_op = OP_TYPE_CLASS2_ALG | caam_alg->alg_op;
2350
2351 return 0;
2352}
2353
2354static void caam_cra_exit(struct crypto_tfm *tfm)
2355{
2356 struct caam_ctx *ctx = crypto_tfm_ctx(tfm);
2357
Yuan Kang1acebad32011-07-15 11:21:42 +08002358 if (ctx->sh_desc_enc_dma &&
2359 !dma_mapping_error(ctx->jrdev, ctx->sh_desc_enc_dma))
2360 dma_unmap_single(ctx->jrdev, ctx->sh_desc_enc_dma,
2361 desc_bytes(ctx->sh_desc_enc), DMA_TO_DEVICE);
2362 if (ctx->sh_desc_dec_dma &&
2363 !dma_mapping_error(ctx->jrdev, ctx->sh_desc_dec_dma))
2364 dma_unmap_single(ctx->jrdev, ctx->sh_desc_dec_dma,
2365 desc_bytes(ctx->sh_desc_dec), DMA_TO_DEVICE);
2366 if (ctx->sh_desc_givenc_dma &&
2367 !dma_mapping_error(ctx->jrdev, ctx->sh_desc_givenc_dma))
2368 dma_unmap_single(ctx->jrdev, ctx->sh_desc_givenc_dma,
2369 desc_bytes(ctx->sh_desc_givenc),
Kim Phillips4427b1b2011-05-14 22:08:17 -05002370 DMA_TO_DEVICE);
Horia Geantaec31eed2014-03-14 17:48:30 +02002371 if (ctx->key_dma &&
2372 !dma_mapping_error(ctx->jrdev, ctx->key_dma))
2373 dma_unmap_single(ctx->jrdev, ctx->key_dma,
2374 ctx->enckeylen + ctx->split_key_pad_len,
2375 DMA_TO_DEVICE);
Ruchika Guptacfc6f112013-10-25 12:01:03 +05302376
2377 caam_jr_free(ctx->jrdev);
Kim Phillips8e8ec592011-03-13 16:54:26 +08002378}
2379
2380static void __exit caam_algapi_exit(void)
2381{
2382
Kim Phillips8e8ec592011-03-13 16:54:26 +08002383 struct caam_crypto_alg *t_alg, *n;
Kim Phillips8e8ec592011-03-13 16:54:26 +08002384
Ruchika Guptacfc6f112013-10-25 12:01:03 +05302385 if (!alg_list.next)
Kim Phillips8e8ec592011-03-13 16:54:26 +08002386 return;
2387
Ruchika Guptacfc6f112013-10-25 12:01:03 +05302388 list_for_each_entry_safe(t_alg, n, &alg_list, entry) {
Kim Phillips8e8ec592011-03-13 16:54:26 +08002389 crypto_unregister_alg(&t_alg->crypto_alg);
2390 list_del(&t_alg->entry);
2391 kfree(t_alg);
2392 }
Kim Phillips8e8ec592011-03-13 16:54:26 +08002393}
2394
Ruchika Guptacfc6f112013-10-25 12:01:03 +05302395static struct caam_crypto_alg *caam_alg_alloc(struct caam_alg_template
Kim Phillips8e8ec592011-03-13 16:54:26 +08002396 *template)
2397{
2398 struct caam_crypto_alg *t_alg;
2399 struct crypto_alg *alg;
2400
2401 t_alg = kzalloc(sizeof(struct caam_crypto_alg), GFP_KERNEL);
2402 if (!t_alg) {
Ruchika Guptacfc6f112013-10-25 12:01:03 +05302403 pr_err("failed to allocate t_alg\n");
Kim Phillips8e8ec592011-03-13 16:54:26 +08002404 return ERR_PTR(-ENOMEM);
2405 }
2406
2407 alg = &t_alg->crypto_alg;
2408
2409 snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", template->name);
2410 snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
2411 template->driver_name);
2412 alg->cra_module = THIS_MODULE;
2413 alg->cra_init = caam_cra_init;
2414 alg->cra_exit = caam_cra_exit;
2415 alg->cra_priority = CAAM_CRA_PRIORITY;
Kim Phillips8e8ec592011-03-13 16:54:26 +08002416 alg->cra_blocksize = template->blocksize;
2417 alg->cra_alignmask = 0;
Kim Phillips8e8ec592011-03-13 16:54:26 +08002418 alg->cra_ctxsize = sizeof(struct caam_ctx);
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +01002419 alg->cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY |
2420 template->type;
Yuan Kang885e9e22011-07-15 11:21:41 +08002421 switch (template->type) {
Yuan Kangacdca312011-07-15 11:21:42 +08002422 case CRYPTO_ALG_TYPE_ABLKCIPHER:
2423 alg->cra_type = &crypto_ablkcipher_type;
2424 alg->cra_ablkcipher = template->template_ablkcipher;
2425 break;
Yuan Kang885e9e22011-07-15 11:21:41 +08002426 case CRYPTO_ALG_TYPE_AEAD:
2427 alg->cra_type = &crypto_aead_type;
2428 alg->cra_aead = template->template_aead;
2429 break;
2430 }
Kim Phillips8e8ec592011-03-13 16:54:26 +08002431
2432 t_alg->class1_alg_type = template->class1_alg_type;
2433 t_alg->class2_alg_type = template->class2_alg_type;
2434 t_alg->alg_op = template->alg_op;
Kim Phillips8e8ec592011-03-13 16:54:26 +08002435
2436 return t_alg;
2437}
2438
2439static int __init caam_algapi_init(void)
2440{
Kim Phillips8e8ec592011-03-13 16:54:26 +08002441 int i = 0, err = 0;
2442
Ruchika Guptacfc6f112013-10-25 12:01:03 +05302443 INIT_LIST_HEAD(&alg_list);
Kim Phillips8e8ec592011-03-13 16:54:26 +08002444
2445 /* register crypto algorithms the device supports */
2446 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
2447 /* TODO: check if h/w supports alg */
2448 struct caam_crypto_alg *t_alg;
2449
Ruchika Guptacfc6f112013-10-25 12:01:03 +05302450 t_alg = caam_alg_alloc(&driver_algs[i]);
Kim Phillips8e8ec592011-03-13 16:54:26 +08002451 if (IS_ERR(t_alg)) {
2452 err = PTR_ERR(t_alg);
Ruchika Guptacfc6f112013-10-25 12:01:03 +05302453 pr_warn("%s alg allocation failed\n",
2454 driver_algs[i].driver_name);
Kim Phillips8e8ec592011-03-13 16:54:26 +08002455 continue;
2456 }
2457
2458 err = crypto_register_alg(&t_alg->crypto_alg);
2459 if (err) {
Ruchika Guptacfc6f112013-10-25 12:01:03 +05302460 pr_warn("%s alg registration failed\n",
Kim Phillips8e8ec592011-03-13 16:54:26 +08002461 t_alg->crypto_alg.cra_driver_name);
2462 kfree(t_alg);
Horia Geanta246bbed2013-03-20 16:31:58 +02002463 } else
Ruchika Guptacfc6f112013-10-25 12:01:03 +05302464 list_add_tail(&t_alg->entry, &alg_list);
Kim Phillips8e8ec592011-03-13 16:54:26 +08002465 }
Ruchika Guptacfc6f112013-10-25 12:01:03 +05302466 if (!list_empty(&alg_list))
2467 pr_info("caam algorithms registered in /proc/crypto\n");
Kim Phillips8e8ec592011-03-13 16:54:26 +08002468
2469 return err;
2470}
2471
2472module_init(caam_algapi_init);
2473module_exit(caam_algapi_exit);
2474
2475MODULE_LICENSE("GPL");
2476MODULE_DESCRIPTION("FSL CAAM support for crypto API");
2477MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");