blob: 403b293509ba23f40ae7b3d27c2e4a1534e6a5a1 [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) |
40 * | SEQ_IN_PTR |
41 * | (input buffer) |
42 * | LOAD (to DECO) |
43 * ---------------------
44 */
45
46#include "compat.h"
47
48#include "regs.h"
49#include "intern.h"
50#include "desc_constr.h"
51#include "jr.h"
52#include "error.h"
53
54/*
55 * crypto alg
56 */
57#define CAAM_CRA_PRIORITY 3000
58/* max key is sum of AES_MAX_KEY_SIZE, max split key size */
59#define CAAM_MAX_KEY_SIZE (AES_MAX_KEY_SIZE + \
60 SHA512_DIGEST_SIZE * 2)
61/* max IV is max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
62#define CAAM_MAX_IV_LENGTH 16
63
Kim Phillips4427b1b2011-05-14 22:08:17 -050064/* length of descriptors text */
65#define DESC_AEAD_SHARED_TEXT_LEN 4
66#define DESC_AEAD_ENCRYPT_TEXT_LEN 21
67#define DESC_AEAD_DECRYPT_TEXT_LEN 24
68#define DESC_AEAD_GIVENCRYPT_TEXT_LEN 27
69
Kim Phillips8e8ec592011-03-13 16:54:26 +080070#ifdef DEBUG
71/* for print_hex_dumps with line references */
72#define xstr(s) str(s)
73#define str(s) #s
74#define debug(format, arg...) printk(format, arg)
75#else
76#define debug(format, arg...)
77#endif
78
79/*
80 * per-session context
81 */
82struct caam_ctx {
83 struct device *jrdev;
84 u32 *sh_desc;
85 dma_addr_t shared_desc_phys;
86 u32 class1_alg_type;
87 u32 class2_alg_type;
88 u32 alg_op;
89 u8 *key;
Yuan Kang885e9e22011-07-15 11:21:41 +080090 dma_addr_t key_dma;
Kim Phillips8e8ec592011-03-13 16:54:26 +080091 unsigned int enckeylen;
Kim Phillips8e8ec592011-03-13 16:54:26 +080092 unsigned int split_key_len;
93 unsigned int split_key_pad_len;
94 unsigned int authsize;
95};
96
Yuan Kang0e479302011-07-15 11:21:41 +080097static int aead_setauthsize(struct crypto_aead *authenc,
Kim Phillips8e8ec592011-03-13 16:54:26 +080098 unsigned int authsize)
99{
100 struct caam_ctx *ctx = crypto_aead_ctx(authenc);
101
102 ctx->authsize = authsize;
103
104 return 0;
105}
106
107struct split_key_result {
108 struct completion completion;
109 int err;
110};
111
112static void split_key_done(struct device *dev, u32 *desc, u32 err,
113 void *context)
114{
115 struct split_key_result *res = context;
116
117#ifdef DEBUG
118 dev_err(dev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
119#endif
120 if (err) {
Kim Phillipsde2954d2011-05-02 18:29:17 -0500121 char tmp[CAAM_ERROR_STR_MAX];
Kim Phillips8e8ec592011-03-13 16:54:26 +0800122
123 dev_err(dev, "%08x: %s\n", err, caam_jr_strstatus(tmp, err));
124 }
125
126 res->err = err;
127
128 complete(&res->completion);
129}
130
131/*
132get a split ipad/opad key
133
134Split key generation-----------------------------------------------
135
136[00] 0xb0810008 jobdesc: stidx=1 share=never len=8
137[01] 0x04000014 key: class2->keyreg len=20
138 @0xffe01000
139[03] 0x84410014 operation: cls2-op sha1 hmac init dec
140[04] 0x24940000 fifold: class2 msgdata-last2 len=0 imm
141[05] 0xa4000001 jump: class2 local all ->1 [06]
142[06] 0x64260028 fifostr: class2 mdsplit-jdk len=40
143 @0xffe04000
144*/
145static u32 gen_split_key(struct caam_ctx *ctx, const u8 *key_in, u32 authkeylen)
146{
147 struct device *jrdev = ctx->jrdev;
148 u32 *desc;
149 struct split_key_result result;
150 dma_addr_t dma_addr_in, dma_addr_out;
151 int ret = 0;
152
153 desc = kmalloc(CAAM_CMD_SZ * 6 + CAAM_PTR_SZ * 2, GFP_KERNEL | GFP_DMA);
154
155 init_job_desc(desc, 0);
156
157 dma_addr_in = dma_map_single(jrdev, (void *)key_in, authkeylen,
158 DMA_TO_DEVICE);
159 if (dma_mapping_error(jrdev, dma_addr_in)) {
160 dev_err(jrdev, "unable to map key input memory\n");
161 kfree(desc);
162 return -ENOMEM;
163 }
164 append_key(desc, dma_addr_in, authkeylen, CLASS_2 |
165 KEY_DEST_CLASS_REG);
166
167 /* Sets MDHA up into an HMAC-INIT */
168 append_operation(desc, ctx->alg_op | OP_ALG_DECRYPT |
169 OP_ALG_AS_INIT);
170
171 /*
172 * do a FIFO_LOAD of zero, this will trigger the internal key expansion
173 into both pads inside MDHA
174 */
175 append_fifo_load_as_imm(desc, NULL, 0, LDST_CLASS_2_CCB |
176 FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST2);
177
178 /*
179 * FIFO_STORE with the explicit split-key content store
180 * (0x26 output type)
181 */
182 dma_addr_out = dma_map_single(jrdev, ctx->key, ctx->split_key_pad_len,
183 DMA_FROM_DEVICE);
184 if (dma_mapping_error(jrdev, dma_addr_out)) {
185 dev_err(jrdev, "unable to map key output memory\n");
186 kfree(desc);
187 return -ENOMEM;
188 }
189 append_fifo_store(desc, dma_addr_out, ctx->split_key_len,
190 LDST_CLASS_2_CCB | FIFOST_TYPE_SPLIT_KEK);
191
192#ifdef DEBUG
193 print_hex_dump(KERN_ERR, "ctx.key@"xstr(__LINE__)": ",
194 DUMP_PREFIX_ADDRESS, 16, 4, key_in, authkeylen, 1);
195 print_hex_dump(KERN_ERR, "jobdesc@"xstr(__LINE__)": ",
196 DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
197#endif
198
199 result.err = 0;
200 init_completion(&result.completion);
201
202 ret = caam_jr_enqueue(jrdev, desc, split_key_done, &result);
203 if (!ret) {
204 /* in progress */
205 wait_for_completion_interruptible(&result.completion);
206 ret = result.err;
207#ifdef DEBUG
208 print_hex_dump(KERN_ERR, "ctx.key@"xstr(__LINE__)": ",
209 DUMP_PREFIX_ADDRESS, 16, 4, ctx->key,
210 ctx->split_key_pad_len, 1);
211#endif
212 }
213
214 dma_unmap_single(jrdev, dma_addr_out, ctx->split_key_pad_len,
215 DMA_FROM_DEVICE);
216 dma_unmap_single(jrdev, dma_addr_in, authkeylen, DMA_TO_DEVICE);
217
218 kfree(desc);
219
220 return ret;
221}
222
223static int build_sh_desc_ipsec(struct caam_ctx *ctx)
224{
225 struct device *jrdev = ctx->jrdev;
226 u32 *sh_desc;
227 u32 *jump_cmd;
Kim Phillips4427b1b2011-05-14 22:08:17 -0500228 bool keys_fit_inline = 0;
229
230 /*
231 * largest Job Descriptor and its Shared Descriptor
232 * must both fit into the 64-word Descriptor h/w Buffer
233 */
234 if ((DESC_AEAD_GIVENCRYPT_TEXT_LEN +
235 DESC_AEAD_SHARED_TEXT_LEN) * CAAM_CMD_SZ +
236 ctx->split_key_pad_len + ctx->enckeylen <= CAAM_DESC_BYTES_MAX)
237 keys_fit_inline = 1;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800238
239 /* build shared descriptor for this session */
Kim Phillips4427b1b2011-05-14 22:08:17 -0500240 sh_desc = kmalloc(CAAM_CMD_SZ * DESC_AEAD_SHARED_TEXT_LEN +
Kim Phillipsa18b9892011-05-26 13:30:34 +1000241 (keys_fit_inline ?
242 ctx->split_key_pad_len + ctx->enckeylen :
243 CAAM_PTR_SZ * 2), GFP_DMA | GFP_KERNEL);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800244 if (!sh_desc) {
245 dev_err(jrdev, "could not allocate shared descriptor\n");
246 return -ENOMEM;
247 }
248
249 init_sh_desc(sh_desc, HDR_SAVECTX | HDR_SHARE_SERIAL);
250
251 jump_cmd = append_jump(sh_desc, CLASS_BOTH | JUMP_TEST_ALL |
252 JUMP_COND_SHRD | JUMP_COND_SELF);
253
Kim Phillips4427b1b2011-05-14 22:08:17 -0500254 /*
255 * process keys, starting with class 2/authentication.
256 */
257 if (keys_fit_inline) {
258 append_key_as_imm(sh_desc, ctx->key, ctx->split_key_pad_len,
259 ctx->split_key_len,
260 CLASS_2 | KEY_DEST_MDHA_SPLIT | KEY_ENC);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800261
Kim Phillips4427b1b2011-05-14 22:08:17 -0500262 append_key_as_imm(sh_desc, (void *)ctx->key +
263 ctx->split_key_pad_len, ctx->enckeylen,
264 ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
265 } else {
Yuan Kang885e9e22011-07-15 11:21:41 +0800266 append_key(sh_desc, ctx->key_dma, ctx->split_key_len, CLASS_2 |
Kim Phillips4427b1b2011-05-14 22:08:17 -0500267 KEY_DEST_MDHA_SPLIT | KEY_ENC);
Yuan Kang885e9e22011-07-15 11:21:41 +0800268 append_key(sh_desc, ctx->key_dma + ctx->split_key_pad_len,
Kim Phillips4427b1b2011-05-14 22:08:17 -0500269 ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
270 }
Kim Phillips8e8ec592011-03-13 16:54:26 +0800271
272 /* update jump cmd now that we are at the jump target */
273 set_jump_tgt_here(sh_desc, jump_cmd);
274
275 ctx->shared_desc_phys = dma_map_single(jrdev, sh_desc,
276 desc_bytes(sh_desc),
277 DMA_TO_DEVICE);
278 if (dma_mapping_error(jrdev, ctx->shared_desc_phys)) {
279 dev_err(jrdev, "unable to map shared descriptor\n");
280 kfree(sh_desc);
281 return -ENOMEM;
282 }
283
284 ctx->sh_desc = sh_desc;
285
286 return 0;
287}
288
Yuan Kang0e479302011-07-15 11:21:41 +0800289static int aead_setkey(struct crypto_aead *aead,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800290 const u8 *key, unsigned int keylen)
291{
292 /* Sizes for MDHA pads (*not* keys): MD5, SHA1, 224, 256, 384, 512 */
293 static const u8 mdpadlen[] = { 16, 20, 32, 32, 64, 64 };
294 struct caam_ctx *ctx = crypto_aead_ctx(aead);
295 struct device *jrdev = ctx->jrdev;
296 struct rtattr *rta = (void *)key;
297 struct crypto_authenc_key_param *param;
298 unsigned int authkeylen;
299 unsigned int enckeylen;
300 int ret = 0;
301
302 param = RTA_DATA(rta);
303 enckeylen = be32_to_cpu(param->enckeylen);
304
305 key += RTA_ALIGN(rta->rta_len);
306 keylen -= RTA_ALIGN(rta->rta_len);
307
308 if (keylen < enckeylen)
309 goto badkey;
310
311 authkeylen = keylen - enckeylen;
312
313 if (keylen > CAAM_MAX_KEY_SIZE)
314 goto badkey;
315
316 /* Pick class 2 key length from algorithm submask */
317 ctx->split_key_len = mdpadlen[(ctx->alg_op & OP_ALG_ALGSEL_SUBMASK) >>
318 OP_ALG_ALGSEL_SHIFT] * 2;
319 ctx->split_key_pad_len = ALIGN(ctx->split_key_len, 16);
320
321#ifdef DEBUG
322 printk(KERN_ERR "keylen %d enckeylen %d authkeylen %d\n",
323 keylen, enckeylen, authkeylen);
324 printk(KERN_ERR "split_key_len %d split_key_pad_len %d\n",
325 ctx->split_key_len, ctx->split_key_pad_len);
326 print_hex_dump(KERN_ERR, "key in @"xstr(__LINE__)": ",
327 DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
328#endif
329 ctx->key = kmalloc(ctx->split_key_pad_len + enckeylen,
330 GFP_KERNEL | GFP_DMA);
331 if (!ctx->key) {
332 dev_err(jrdev, "could not allocate key output memory\n");
333 return -ENOMEM;
334 }
335
336 ret = gen_split_key(ctx, key, authkeylen);
337 if (ret) {
338 kfree(ctx->key);
339 goto badkey;
340 }
341
342 /* postpend encryption key to auth split key */
343 memcpy(ctx->key + ctx->split_key_pad_len, key + authkeylen, enckeylen);
344
Yuan Kang885e9e22011-07-15 11:21:41 +0800345 ctx->key_dma = dma_map_single(jrdev, ctx->key, ctx->split_key_pad_len +
Kim Phillips8e8ec592011-03-13 16:54:26 +0800346 enckeylen, DMA_TO_DEVICE);
Yuan Kang885e9e22011-07-15 11:21:41 +0800347 if (dma_mapping_error(jrdev, ctx->key_dma)) {
Kim Phillips8e8ec592011-03-13 16:54:26 +0800348 dev_err(jrdev, "unable to map key i/o memory\n");
349 kfree(ctx->key);
350 return -ENOMEM;
351 }
352#ifdef DEBUG
353 print_hex_dump(KERN_ERR, "ctx.key@"xstr(__LINE__)": ",
354 DUMP_PREFIX_ADDRESS, 16, 4, ctx->key,
355 ctx->split_key_pad_len + enckeylen, 1);
356#endif
357
Kim Phillips8e8ec592011-03-13 16:54:26 +0800358 ctx->enckeylen = enckeylen;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800359
360 ret = build_sh_desc_ipsec(ctx);
361 if (ret) {
Yuan Kang885e9e22011-07-15 11:21:41 +0800362 dma_unmap_single(jrdev, ctx->key_dma, ctx->split_key_pad_len +
Kim Phillips8e8ec592011-03-13 16:54:26 +0800363 enckeylen, DMA_TO_DEVICE);
364 kfree(ctx->key);
365 }
366
367 return ret;
368badkey:
369 crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
370 return -EINVAL;
371}
372
373struct link_tbl_entry {
374 u64 ptr;
375 u32 len;
376 u8 reserved;
377 u8 buf_pool_id;
378 u16 offset;
379};
380
381/*
Yuan Kang0e479302011-07-15 11:21:41 +0800382 * aead_edesc - s/w-extended ipsec_esp descriptor
Kim Phillips8e8ec592011-03-13 16:54:26 +0800383 * @src_nents: number of segments in input scatterlist
384 * @dst_nents: number of segments in output scatterlist
385 * @assoc_nents: number of segments in associated data (SPI+Seq) scatterlist
386 * @desc: h/w descriptor (variable length; must not exceed MAX_CAAM_DESCSIZE)
387 * @link_tbl_bytes: length of dma mapped link_tbl space
388 * @link_tbl_dma: bus physical mapped address of h/w link table
389 * @hw_desc: the h/w job descriptor followed by any referenced link tables
390 */
Yuan Kang0e479302011-07-15 11:21:41 +0800391struct aead_edesc {
Kim Phillips8e8ec592011-03-13 16:54:26 +0800392 int assoc_nents;
393 int src_nents;
394 int dst_nents;
395 int link_tbl_bytes;
396 dma_addr_t link_tbl_dma;
397 struct link_tbl_entry *link_tbl;
398 u32 hw_desc[0];
399};
400
Yuan Kang0e479302011-07-15 11:21:41 +0800401static void aead_unmap(struct device *dev,
402 struct aead_edesc *edesc,
403 struct aead_request *req)
Kim Phillips8e8ec592011-03-13 16:54:26 +0800404{
Yuan Kang0e479302011-07-15 11:21:41 +0800405 dma_unmap_sg(dev, req->assoc, edesc->assoc_nents, DMA_TO_DEVICE);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800406
Yuan Kang0e479302011-07-15 11:21:41 +0800407 if (unlikely(req->dst != req->src)) {
408 dma_unmap_sg(dev, req->src, edesc->src_nents,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800409 DMA_TO_DEVICE);
Yuan Kang0e479302011-07-15 11:21:41 +0800410 dma_unmap_sg(dev, req->dst, edesc->dst_nents,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800411 DMA_FROM_DEVICE);
412 } else {
Yuan Kang0e479302011-07-15 11:21:41 +0800413 dma_unmap_sg(dev, req->src, edesc->src_nents,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800414 DMA_BIDIRECTIONAL);
415 }
416
417 if (edesc->link_tbl_bytes)
418 dma_unmap_single(dev, edesc->link_tbl_dma,
419 edesc->link_tbl_bytes,
420 DMA_TO_DEVICE);
421}
422
423/*
424 * ipsec_esp descriptor callbacks
425 */
Yuan Kang0e479302011-07-15 11:21:41 +0800426static void aead_encrypt_done(struct device *jrdev, u32 *desc, u32 err,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800427 void *context)
428{
Yuan Kang0e479302011-07-15 11:21:41 +0800429 struct aead_request *req = context;
430 struct aead_edesc *edesc;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800431#ifdef DEBUG
Yuan Kang0e479302011-07-15 11:21:41 +0800432 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800433 int ivsize = crypto_aead_ivsize(aead);
434 struct caam_ctx *ctx = crypto_aead_ctx(aead);
435
436 dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
437#endif
Yuan Kang0e479302011-07-15 11:21:41 +0800438 edesc = (struct aead_edesc *)((char *)desc -
439 offsetof(struct aead_edesc, hw_desc));
Kim Phillips8e8ec592011-03-13 16:54:26 +0800440
441 if (err) {
Kim Phillipsde2954d2011-05-02 18:29:17 -0500442 char tmp[CAAM_ERROR_STR_MAX];
Kim Phillips8e8ec592011-03-13 16:54:26 +0800443
Kim Phillips8e8ec592011-03-13 16:54:26 +0800444 dev_err(jrdev, "%08x: %s\n", err, caam_jr_strstatus(tmp, err));
445 }
446
Yuan Kang0e479302011-07-15 11:21:41 +0800447 aead_unmap(jrdev, edesc, req);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800448
449#ifdef DEBUG
450 print_hex_dump(KERN_ERR, "assoc @"xstr(__LINE__)": ",
Yuan Kang0e479302011-07-15 11:21:41 +0800451 DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->assoc),
452 req->assoclen , 1);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800453 print_hex_dump(KERN_ERR, "dstiv @"xstr(__LINE__)": ",
Yuan Kang0e479302011-07-15 11:21:41 +0800454 DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src) - ivsize,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800455 edesc->src_nents ? 100 : ivsize, 1);
456 print_hex_dump(KERN_ERR, "dst @"xstr(__LINE__)": ",
Yuan Kang0e479302011-07-15 11:21:41 +0800457 DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
458 edesc->src_nents ? 100 : req->cryptlen +
Kim Phillips8e8ec592011-03-13 16:54:26 +0800459 ctx->authsize + 4, 1);
460#endif
461
462 kfree(edesc);
463
Yuan Kang0e479302011-07-15 11:21:41 +0800464 aead_request_complete(req, err);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800465}
466
Yuan Kang0e479302011-07-15 11:21:41 +0800467static void aead_decrypt_done(struct device *jrdev, u32 *desc, u32 err,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800468 void *context)
469{
Yuan Kang0e479302011-07-15 11:21:41 +0800470 struct aead_request *req = context;
471 struct aead_edesc *edesc;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800472#ifdef DEBUG
Yuan Kang0e479302011-07-15 11:21:41 +0800473 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800474 struct caam_ctx *ctx = crypto_aead_ctx(aead);
475
476 dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
477#endif
Yuan Kang0e479302011-07-15 11:21:41 +0800478 edesc = (struct aead_edesc *)((char *)desc -
479 offsetof(struct aead_edesc, hw_desc));
Kim Phillips8e8ec592011-03-13 16:54:26 +0800480
481 if (err) {
Kim Phillipsde2954d2011-05-02 18:29:17 -0500482 char tmp[CAAM_ERROR_STR_MAX];
Kim Phillips8e8ec592011-03-13 16:54:26 +0800483
484 dev_err(jrdev, "%08x: %s\n", err, caam_jr_strstatus(tmp, err));
485 }
486
Yuan Kang0e479302011-07-15 11:21:41 +0800487 aead_unmap(jrdev, edesc, req);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800488
489 /*
490 * verify hw auth check passed else return -EBADMSG
491 */
492 if ((err & JRSTA_CCBERR_ERRID_MASK) == JRSTA_CCBERR_ERRID_ICVCHK)
493 err = -EBADMSG;
494
495#ifdef DEBUG
496 print_hex_dump(KERN_ERR, "iphdrout@"xstr(__LINE__)": ",
497 DUMP_PREFIX_ADDRESS, 16, 4,
Yuan Kang0e479302011-07-15 11:21:41 +0800498 ((char *)sg_virt(req->assoc) - sizeof(struct iphdr)),
499 sizeof(struct iphdr) + req->assoclen +
500 ((req->cryptlen > 1500) ? 1500 : req->cryptlen) +
Kim Phillips8e8ec592011-03-13 16:54:26 +0800501 ctx->authsize + 36, 1);
502 if (!err && edesc->link_tbl_bytes) {
Yuan Kang0e479302011-07-15 11:21:41 +0800503 struct scatterlist *sg = sg_last(req->src, edesc->src_nents);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800504 print_hex_dump(KERN_ERR, "sglastout@"xstr(__LINE__)": ",
505 DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(sg),
506 sg->length + ctx->authsize + 16, 1);
507 }
508#endif
509 kfree(edesc);
510
Yuan Kang0e479302011-07-15 11:21:41 +0800511 aead_request_complete(req, err);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800512}
513
514/*
515 * convert scatterlist to h/w link table format
516 * scatterlist must have been previously dma mapped
517 */
518static void sg_to_link_tbl(struct scatterlist *sg, int sg_count,
519 struct link_tbl_entry *link_tbl_ptr, u32 offset)
520{
521 while (sg_count) {
522 link_tbl_ptr->ptr = sg_dma_address(sg);
523 link_tbl_ptr->len = sg_dma_len(sg);
524 link_tbl_ptr->reserved = 0;
525 link_tbl_ptr->buf_pool_id = 0;
526 link_tbl_ptr->offset = offset;
527 link_tbl_ptr++;
528 sg = sg_next(sg);
529 sg_count--;
530 }
531
532 /* set Final bit (marks end of link table) */
533 link_tbl_ptr--;
534 link_tbl_ptr->len |= 0x40000000;
535}
536
537/*
538 * fill in and submit ipsec_esp job descriptor
539 */
Yuan Kang0e479302011-07-15 11:21:41 +0800540static int init_aead_job(struct aead_edesc *edesc, struct aead_request *req,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800541 u32 encrypt,
542 void (*callback) (struct device *dev, u32 *desc,
543 u32 err, void *context))
544{
Yuan Kang0e479302011-07-15 11:21:41 +0800545 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800546 struct caam_ctx *ctx = crypto_aead_ctx(aead);
547 struct device *jrdev = ctx->jrdev;
548 u32 *desc = edesc->hw_desc, options;
549 int ret, sg_count, assoc_sg_count;
550 int ivsize = crypto_aead_ivsize(aead);
551 int authsize = ctx->authsize;
552 dma_addr_t ptr, dst_dma, src_dma;
553#ifdef DEBUG
554 u32 *sh_desc = ctx->sh_desc;
555
556 debug("assoclen %d cryptlen %d authsize %d\n",
Yuan Kang0e479302011-07-15 11:21:41 +0800557 req->assoclen, req->cryptlen, authsize);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800558 print_hex_dump(KERN_ERR, "assoc @"xstr(__LINE__)": ",
Yuan Kang0e479302011-07-15 11:21:41 +0800559 DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->assoc),
560 req->assoclen , 1);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800561 print_hex_dump(KERN_ERR, "presciv@"xstr(__LINE__)": ",
Yuan Kang0e479302011-07-15 11:21:41 +0800562 DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src) - ivsize,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800563 edesc->src_nents ? 100 : ivsize, 1);
564 print_hex_dump(KERN_ERR, "src @"xstr(__LINE__)": ",
Yuan Kang0e479302011-07-15 11:21:41 +0800565 DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
566 edesc->src_nents ? 100 : req->cryptlen + authsize, 1);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800567 print_hex_dump(KERN_ERR, "shrdesc@"xstr(__LINE__)": ",
568 DUMP_PREFIX_ADDRESS, 16, 4, sh_desc,
569 desc_bytes(sh_desc), 1);
570#endif
Yuan Kang0e479302011-07-15 11:21:41 +0800571 assoc_sg_count = dma_map_sg(jrdev, req->assoc, edesc->assoc_nents ?: 1,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800572 DMA_TO_DEVICE);
Yuan Kang0e479302011-07-15 11:21:41 +0800573 if (req->src == req->dst)
574 sg_count = dma_map_sg(jrdev, req->src, edesc->src_nents ? : 1,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800575 DMA_BIDIRECTIONAL);
576 else
Yuan Kang0e479302011-07-15 11:21:41 +0800577 sg_count = dma_map_sg(jrdev, req->src, edesc->src_nents ? : 1,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800578 DMA_TO_DEVICE);
579
580 /* start auth operation */
581 append_operation(desc, ctx->class2_alg_type | OP_ALG_AS_INITFINAL |
582 (encrypt ? : OP_ALG_ICV_ON));
583
584 /* Load FIFO with data for Class 2 CHA */
585 options = FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG;
586 if (!edesc->assoc_nents) {
Yuan Kang0e479302011-07-15 11:21:41 +0800587 ptr = sg_dma_address(req->assoc);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800588 } else {
Yuan Kang0e479302011-07-15 11:21:41 +0800589 sg_to_link_tbl(req->assoc, edesc->assoc_nents,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800590 edesc->link_tbl, 0);
591 ptr = edesc->link_tbl_dma;
592 options |= LDST_SGF;
593 }
Yuan Kang0e479302011-07-15 11:21:41 +0800594 append_fifo_load(desc, ptr, req->assoclen, options);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800595
596 /* copy iv from cipher/class1 input context to class2 infifo */
597 append_move(desc, MOVE_SRC_CLASS1CTX | MOVE_DEST_CLASS2INFIFO | ivsize);
598
Kim Phillipsddbb8082011-05-14 22:08:02 -0500599 if (!encrypt) {
600 u32 *jump_cmd, *uncond_jump_cmd;
601
602 /* JUMP if shared */
603 jump_cmd = append_jump(desc, JUMP_TEST_ALL | JUMP_COND_SHRD);
604
605 /* start class 1 (cipher) operation, non-shared version */
606 append_operation(desc, ctx->class1_alg_type |
607 OP_ALG_AS_INITFINAL);
608
609 uncond_jump_cmd = append_jump(desc, 0);
610
611 set_jump_tgt_here(desc, jump_cmd);
612
613 /* start class 1 (cipher) operation, shared version */
614 append_operation(desc, ctx->class1_alg_type |
615 OP_ALG_AS_INITFINAL | OP_ALG_AAI_DK);
616 set_jump_tgt_here(desc, uncond_jump_cmd);
617 } else
618 append_operation(desc, ctx->class1_alg_type |
619 OP_ALG_AS_INITFINAL | encrypt);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800620
621 /* load payload & instruct to class2 to snoop class 1 if encrypting */
622 options = 0;
623 if (!edesc->src_nents) {
Yuan Kang0e479302011-07-15 11:21:41 +0800624 src_dma = sg_dma_address(req->src);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800625 } else {
Yuan Kang0e479302011-07-15 11:21:41 +0800626 sg_to_link_tbl(req->src, edesc->src_nents, edesc->link_tbl +
Kim Phillips8e8ec592011-03-13 16:54:26 +0800627 edesc->assoc_nents, 0);
628 src_dma = edesc->link_tbl_dma + edesc->assoc_nents *
629 sizeof(struct link_tbl_entry);
630 options |= LDST_SGF;
631 }
Yuan Kang0e479302011-07-15 11:21:41 +0800632 append_seq_in_ptr(desc, src_dma, req->cryptlen + authsize, options);
633 append_seq_fifo_load(desc, req->cryptlen, FIFOLD_CLASS_BOTH |
Kim Phillips8e8ec592011-03-13 16:54:26 +0800634 FIFOLD_TYPE_LASTBOTH |
635 (encrypt ? FIFOLD_TYPE_MSG1OUT2
636 : FIFOLD_TYPE_MSG));
637
638 /* specify destination */
Yuan Kang0e479302011-07-15 11:21:41 +0800639 if (req->src == req->dst) {
Kim Phillips8e8ec592011-03-13 16:54:26 +0800640 dst_dma = src_dma;
641 } else {
Yuan Kang0e479302011-07-15 11:21:41 +0800642 sg_count = dma_map_sg(jrdev, req->dst, edesc->dst_nents ? : 1,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800643 DMA_FROM_DEVICE);
644 if (!edesc->dst_nents) {
Yuan Kang0e479302011-07-15 11:21:41 +0800645 dst_dma = sg_dma_address(req->dst);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800646 options = 0;
647 } else {
Yuan Kang0e479302011-07-15 11:21:41 +0800648 sg_to_link_tbl(req->dst, edesc->dst_nents,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800649 edesc->link_tbl + edesc->assoc_nents +
650 edesc->src_nents, 0);
651 dst_dma = edesc->link_tbl_dma + (edesc->assoc_nents +
652 edesc->src_nents) *
653 sizeof(struct link_tbl_entry);
654 options = LDST_SGF;
655 }
656 }
Yuan Kang0e479302011-07-15 11:21:41 +0800657 append_seq_out_ptr(desc, dst_dma, req->cryptlen + authsize, options);
658 append_seq_fifo_store(desc, req->cryptlen, FIFOST_TYPE_MESSAGE_DATA);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800659
660 /* ICV */
661 if (encrypt)
662 append_seq_store(desc, authsize, LDST_CLASS_2_CCB |
663 LDST_SRCDST_BYTE_CONTEXT);
664 else
665 append_seq_fifo_load(desc, authsize, FIFOLD_CLASS_CLASS2 |
666 FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_ICV);
667
668#ifdef DEBUG
669 debug("job_desc_len %d\n", desc_len(desc));
670 print_hex_dump(KERN_ERR, "jobdesc@"xstr(__LINE__)": ",
671 DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc) , 1);
672 print_hex_dump(KERN_ERR, "jdlinkt@"xstr(__LINE__)": ",
673 DUMP_PREFIX_ADDRESS, 16, 4, edesc->link_tbl,
674 edesc->link_tbl_bytes, 1);
675#endif
676
Yuan Kang0e479302011-07-15 11:21:41 +0800677 ret = caam_jr_enqueue(jrdev, desc, callback, req);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800678 if (!ret)
679 ret = -EINPROGRESS;
680 else {
Yuan Kang0e479302011-07-15 11:21:41 +0800681 aead_unmap(jrdev, edesc, req);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800682 kfree(edesc);
683 }
684
685 return ret;
686}
687
688/*
689 * derive number of elements in scatterlist
690 */
691static int sg_count(struct scatterlist *sg_list, int nbytes, int *chained)
692{
693 struct scatterlist *sg = sg_list;
694 int sg_nents = 0;
695
696 *chained = 0;
697 while (nbytes > 0) {
698 sg_nents++;
699 nbytes -= sg->length;
700 if (!sg_is_last(sg) && (sg + 1)->length == 0)
701 *chained = 1;
702 sg = scatterwalk_sg_next(sg);
703 }
704
705 return sg_nents;
706}
707
708/*
709 * allocate and map the ipsec_esp extended descriptor
710 */
Yuan Kang0e479302011-07-15 11:21:41 +0800711static struct aead_edesc *aead_edesc_alloc(struct aead_request *req,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800712 int desc_bytes)
713{
Yuan Kang0e479302011-07-15 11:21:41 +0800714 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800715 struct caam_ctx *ctx = crypto_aead_ctx(aead);
716 struct device *jrdev = ctx->jrdev;
Yuan Kang0e479302011-07-15 11:21:41 +0800717 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
Kim Phillips8e8ec592011-03-13 16:54:26 +0800718 GFP_ATOMIC;
719 int assoc_nents, src_nents, dst_nents = 0, chained, link_tbl_bytes;
Yuan Kang0e479302011-07-15 11:21:41 +0800720 struct aead_edesc *edesc;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800721
Yuan Kang0e479302011-07-15 11:21:41 +0800722 assoc_nents = sg_count(req->assoc, req->assoclen, &chained);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800723 BUG_ON(chained);
724 if (likely(assoc_nents == 1))
725 assoc_nents = 0;
726
Yuan Kang0e479302011-07-15 11:21:41 +0800727 src_nents = sg_count(req->src, req->cryptlen + ctx->authsize,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800728 &chained);
729 BUG_ON(chained);
730 if (src_nents == 1)
731 src_nents = 0;
732
Yuan Kang0e479302011-07-15 11:21:41 +0800733 if (unlikely(req->dst != req->src)) {
734 dst_nents = sg_count(req->dst, req->cryptlen + ctx->authsize,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800735 &chained);
736 BUG_ON(chained);
737 if (dst_nents == 1)
738 dst_nents = 0;
739 }
740
741 link_tbl_bytes = (assoc_nents + src_nents + dst_nents) *
742 sizeof(struct link_tbl_entry);
743 debug("link_tbl_bytes %d\n", link_tbl_bytes);
744
745 /* allocate space for base edesc and hw desc commands, link tables */
Yuan Kang0e479302011-07-15 11:21:41 +0800746 edesc = kmalloc(sizeof(struct aead_edesc) + desc_bytes +
Kim Phillips8e8ec592011-03-13 16:54:26 +0800747 link_tbl_bytes, GFP_DMA | flags);
748 if (!edesc) {
749 dev_err(jrdev, "could not allocate extended descriptor\n");
750 return ERR_PTR(-ENOMEM);
751 }
752
753 edesc->assoc_nents = assoc_nents;
754 edesc->src_nents = src_nents;
755 edesc->dst_nents = dst_nents;
Yuan Kang0e479302011-07-15 11:21:41 +0800756 edesc->link_tbl = (void *)edesc + sizeof(struct aead_edesc) +
Kim Phillips8e8ec592011-03-13 16:54:26 +0800757 desc_bytes;
758 edesc->link_tbl_dma = dma_map_single(jrdev, edesc->link_tbl,
759 link_tbl_bytes, DMA_TO_DEVICE);
760 edesc->link_tbl_bytes = link_tbl_bytes;
761
762 return edesc;
763}
764
Yuan Kang0e479302011-07-15 11:21:41 +0800765static int aead_encrypt(struct aead_request *req)
Kim Phillips8e8ec592011-03-13 16:54:26 +0800766{
Yuan Kang0e479302011-07-15 11:21:41 +0800767 struct aead_edesc *edesc;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800768 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800769 struct caam_ctx *ctx = crypto_aead_ctx(aead);
770 struct device *jrdev = ctx->jrdev;
Yuan Kang0e479302011-07-15 11:21:41 +0800771 int ivsize = crypto_aead_ivsize(aead);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800772 u32 *desc;
773 dma_addr_t iv_dma;
774
Kim Phillips8e8ec592011-03-13 16:54:26 +0800775 /* allocate extended descriptor */
Yuan Kang0e479302011-07-15 11:21:41 +0800776 edesc = aead_edesc_alloc(req, DESC_AEAD_ENCRYPT_TEXT_LEN *
Kim Phillips4427b1b2011-05-14 22:08:17 -0500777 CAAM_CMD_SZ);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800778 if (IS_ERR(edesc))
779 return PTR_ERR(edesc);
780
781 desc = edesc->hw_desc;
782
783 /* insert shared descriptor pointer */
784 init_job_desc_shared(desc, ctx->shared_desc_phys,
785 desc_len(ctx->sh_desc), HDR_SHARE_DEFER);
786
787 iv_dma = dma_map_single(jrdev, req->iv, ivsize, DMA_TO_DEVICE);
788 /* check dma error */
789
790 append_load(desc, iv_dma, ivsize,
791 LDST_CLASS_1_CCB | LDST_SRCDST_BYTE_CONTEXT);
792
Yuan Kang0e479302011-07-15 11:21:41 +0800793 return init_aead_job(edesc, req, OP_ALG_ENCRYPT, aead_encrypt_done);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800794}
795
Yuan Kang0e479302011-07-15 11:21:41 +0800796static int aead_decrypt(struct aead_request *req)
Kim Phillips8e8ec592011-03-13 16:54:26 +0800797{
Yuan Kang0e479302011-07-15 11:21:41 +0800798 struct crypto_aead *aead = crypto_aead_reqtfm(req);
799 int ivsize = crypto_aead_ivsize(aead);
800 struct caam_ctx *ctx = crypto_aead_ctx(aead);
801 struct device *jrdev = ctx->jrdev;
802 struct aead_edesc *edesc;
803 u32 *desc;
804 dma_addr_t iv_dma;
805
806 req->cryptlen -= ctx->authsize;
807
808 /* allocate extended descriptor */
809 edesc = aead_edesc_alloc(req, DESC_AEAD_DECRYPT_TEXT_LEN *
810 CAAM_CMD_SZ);
811 if (IS_ERR(edesc))
812 return PTR_ERR(edesc);
813
814 desc = edesc->hw_desc;
815
816 /* insert shared descriptor pointer */
817 init_job_desc_shared(desc, ctx->shared_desc_phys,
818 desc_len(ctx->sh_desc), HDR_SHARE_DEFER);
819
820 iv_dma = dma_map_single(jrdev, req->iv, ivsize, DMA_TO_DEVICE);
821 /* check dma error */
822
823 append_load(desc, iv_dma, ivsize,
824 LDST_CLASS_1_CCB | LDST_SRCDST_BYTE_CONTEXT);
825
826 return init_aead_job(edesc, req, !OP_ALG_ENCRYPT, aead_decrypt_done);
827}
828
829static int aead_givencrypt(struct aead_givcrypt_request *areq)
830{
831 struct aead_request *req = &areq->areq;
832 struct aead_edesc *edesc;
833 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800834 struct caam_ctx *ctx = crypto_aead_ctx(aead);
835 struct device *jrdev = ctx->jrdev;
836 int ivsize = crypto_aead_ivsize(aead);
837 dma_addr_t iv_dma;
838 u32 *desc;
839
Yuan Kang0e479302011-07-15 11:21:41 +0800840 iv_dma = dma_map_single(jrdev, areq->giv, ivsize, DMA_FROM_DEVICE);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800841
Yuan Kang0e479302011-07-15 11:21:41 +0800842 debug("%s: giv %p\n", __func__, areq->giv);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800843
844 /* allocate extended descriptor */
Yuan Kang0e479302011-07-15 11:21:41 +0800845 edesc = aead_edesc_alloc(req, DESC_AEAD_GIVENCRYPT_TEXT_LEN *
Kim Phillips4427b1b2011-05-14 22:08:17 -0500846 CAAM_CMD_SZ);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800847 if (IS_ERR(edesc))
848 return PTR_ERR(edesc);
849
850 desc = edesc->hw_desc;
851
852 /* insert shared descriptor pointer */
853 init_job_desc_shared(desc, ctx->shared_desc_phys,
854 desc_len(ctx->sh_desc), HDR_SHARE_DEFER);
855
856 /*
857 * LOAD IMM Info FIFO
858 * to DECO, Last, Padding, Random, Message, 16 bytes
859 */
860 append_load_imm_u32(desc, NFIFOENTRY_DEST_DECO | NFIFOENTRY_LC1 |
861 NFIFOENTRY_STYPE_PAD | NFIFOENTRY_DTYPE_MSG |
862 NFIFOENTRY_PTYPE_RND | ivsize,
863 LDST_SRCDST_WORD_INFO_FIFO);
864
865 /*
866 * disable info fifo entries since the above serves as the entry
867 * this way, the MOVE command won't generate an entry.
868 * Note that this isn't required in more recent versions of
869 * SEC as a MOVE that doesn't do info FIFO entries is available.
870 */
871 append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);
872
873 /* MOVE DECO Alignment -> C1 Context 16 bytes */
Kim Phillipsd37d36e2011-04-11 19:15:24 -0500874 append_move(desc, MOVE_SRC_INFIFO | MOVE_DEST_CLASS1CTX | ivsize);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800875
876 /* re-enable info fifo entries */
877 append_cmd(desc, CMD_LOAD | ENABLE_AUTO_INFO_FIFO);
878
879 /* MOVE C1 Context -> OFIFO 16 bytes */
Kim Phillipsd37d36e2011-04-11 19:15:24 -0500880 append_move(desc, MOVE_SRC_CLASS1CTX | MOVE_DEST_OUTFIFO | ivsize);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800881
882 append_fifo_store(desc, iv_dma, ivsize, FIFOST_TYPE_MESSAGE_DATA);
883
Yuan Kang0e479302011-07-15 11:21:41 +0800884 return init_aead_job(edesc, req, OP_ALG_ENCRYPT, aead_encrypt_done);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800885}
886
Yuan Kang885e9e22011-07-15 11:21:41 +0800887#define template_aead template_u.aead
Kim Phillips8e8ec592011-03-13 16:54:26 +0800888struct caam_alg_template {
889 char name[CRYPTO_MAX_ALG_NAME];
890 char driver_name[CRYPTO_MAX_ALG_NAME];
891 unsigned int blocksize;
Yuan Kang885e9e22011-07-15 11:21:41 +0800892 u32 type;
893 union {
894 struct ablkcipher_alg ablkcipher;
895 struct aead_alg aead;
896 struct blkcipher_alg blkcipher;
897 struct cipher_alg cipher;
898 struct compress_alg compress;
899 struct rng_alg rng;
900 } template_u;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800901 u32 class1_alg_type;
902 u32 class2_alg_type;
903 u32 alg_op;
904};
905
906static struct caam_alg_template driver_algs[] = {
907 /* single-pass ipsec_esp descriptor */
908 {
909 .name = "authenc(hmac(sha1),cbc(aes))",
910 .driver_name = "authenc-hmac-sha1-cbc-aes-caam",
911 .blocksize = AES_BLOCK_SIZE,
Yuan Kang885e9e22011-07-15 11:21:41 +0800912 .type = CRYPTO_ALG_TYPE_AEAD,
913 .template_aead = {
Yuan Kang0e479302011-07-15 11:21:41 +0800914 .setkey = aead_setkey,
915 .setauthsize = aead_setauthsize,
916 .encrypt = aead_encrypt,
917 .decrypt = aead_decrypt,
918 .givencrypt = aead_givencrypt,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800919 .geniv = "<built-in>",
920 .ivsize = AES_BLOCK_SIZE,
921 .maxauthsize = SHA1_DIGEST_SIZE,
922 },
923 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
924 .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP,
925 .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
926 },
927 {
928 .name = "authenc(hmac(sha256),cbc(aes))",
929 .driver_name = "authenc-hmac-sha256-cbc-aes-caam",
930 .blocksize = AES_BLOCK_SIZE,
Yuan Kang885e9e22011-07-15 11:21:41 +0800931 .type = CRYPTO_ALG_TYPE_AEAD,
932 .template_aead = {
Yuan Kang0e479302011-07-15 11:21:41 +0800933 .setkey = aead_setkey,
934 .setauthsize = aead_setauthsize,
935 .encrypt = aead_encrypt,
936 .decrypt = aead_decrypt,
937 .givencrypt = aead_givencrypt,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800938 .geniv = "<built-in>",
939 .ivsize = AES_BLOCK_SIZE,
940 .maxauthsize = SHA256_DIGEST_SIZE,
941 },
942 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
943 .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
944 OP_ALG_AAI_HMAC_PRECOMP,
945 .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
946 },
947 {
Kim Phillips4427b1b2011-05-14 22:08:17 -0500948 .name = "authenc(hmac(sha512),cbc(aes))",
949 .driver_name = "authenc-hmac-sha512-cbc-aes-caam",
950 .blocksize = AES_BLOCK_SIZE,
Yuan Kang885e9e22011-07-15 11:21:41 +0800951 .type = CRYPTO_ALG_TYPE_AEAD,
952 .template_aead = {
Yuan Kang0e479302011-07-15 11:21:41 +0800953 .setkey = aead_setkey,
954 .setauthsize = aead_setauthsize,
955 .encrypt = aead_encrypt,
956 .decrypt = aead_decrypt,
957 .givencrypt = aead_givencrypt,
Kim Phillips4427b1b2011-05-14 22:08:17 -0500958 .geniv = "<built-in>",
959 .ivsize = AES_BLOCK_SIZE,
960 .maxauthsize = SHA512_DIGEST_SIZE,
961 },
962 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
963 .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
964 OP_ALG_AAI_HMAC_PRECOMP,
965 .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
966 },
967 {
Kim Phillips8e8ec592011-03-13 16:54:26 +0800968 .name = "authenc(hmac(sha1),cbc(des3_ede))",
969 .driver_name = "authenc-hmac-sha1-cbc-des3_ede-caam",
970 .blocksize = DES3_EDE_BLOCK_SIZE,
Yuan Kang885e9e22011-07-15 11:21:41 +0800971 .type = CRYPTO_ALG_TYPE_AEAD,
972 .template_aead = {
Yuan Kang0e479302011-07-15 11:21:41 +0800973 .setkey = aead_setkey,
974 .setauthsize = aead_setauthsize,
975 .encrypt = aead_encrypt,
976 .decrypt = aead_decrypt,
977 .givencrypt = aead_givencrypt,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800978 .geniv = "<built-in>",
979 .ivsize = DES3_EDE_BLOCK_SIZE,
980 .maxauthsize = SHA1_DIGEST_SIZE,
981 },
982 .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
983 .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP,
984 .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
985 },
986 {
987 .name = "authenc(hmac(sha256),cbc(des3_ede))",
988 .driver_name = "authenc-hmac-sha256-cbc-des3_ede-caam",
989 .blocksize = DES3_EDE_BLOCK_SIZE,
Yuan Kang885e9e22011-07-15 11:21:41 +0800990 .type = CRYPTO_ALG_TYPE_AEAD,
991 .template_aead = {
Yuan Kang0e479302011-07-15 11:21:41 +0800992 .setkey = aead_setkey,
993 .setauthsize = aead_setauthsize,
994 .encrypt = aead_encrypt,
995 .decrypt = aead_decrypt,
996 .givencrypt = aead_givencrypt,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800997 .geniv = "<built-in>",
998 .ivsize = DES3_EDE_BLOCK_SIZE,
999 .maxauthsize = SHA256_DIGEST_SIZE,
1000 },
1001 .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
1002 .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
1003 OP_ALG_AAI_HMAC_PRECOMP,
1004 .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
1005 },
1006 {
Kim Phillips4427b1b2011-05-14 22:08:17 -05001007 .name = "authenc(hmac(sha512),cbc(des3_ede))",
1008 .driver_name = "authenc-hmac-sha512-cbc-des3_ede-caam",
1009 .blocksize = DES3_EDE_BLOCK_SIZE,
Yuan Kang885e9e22011-07-15 11:21:41 +08001010 .type = CRYPTO_ALG_TYPE_AEAD,
1011 .template_aead = {
Yuan Kang0e479302011-07-15 11:21:41 +08001012 .setkey = aead_setkey,
1013 .setauthsize = aead_setauthsize,
1014 .encrypt = aead_encrypt,
1015 .decrypt = aead_decrypt,
1016 .givencrypt = aead_givencrypt,
Kim Phillips4427b1b2011-05-14 22:08:17 -05001017 .geniv = "<built-in>",
1018 .ivsize = DES3_EDE_BLOCK_SIZE,
1019 .maxauthsize = SHA512_DIGEST_SIZE,
1020 },
1021 .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
1022 .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
1023 OP_ALG_AAI_HMAC_PRECOMP,
1024 .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
1025 },
1026 {
Kim Phillips8e8ec592011-03-13 16:54:26 +08001027 .name = "authenc(hmac(sha1),cbc(des))",
1028 .driver_name = "authenc-hmac-sha1-cbc-des-caam",
1029 .blocksize = DES_BLOCK_SIZE,
Yuan Kang885e9e22011-07-15 11:21:41 +08001030 .type = CRYPTO_ALG_TYPE_AEAD,
1031 .template_aead = {
Yuan Kang0e479302011-07-15 11:21:41 +08001032 .setkey = aead_setkey,
1033 .setauthsize = aead_setauthsize,
1034 .encrypt = aead_encrypt,
1035 .decrypt = aead_decrypt,
1036 .givencrypt = aead_givencrypt,
Kim Phillips8e8ec592011-03-13 16:54:26 +08001037 .geniv = "<built-in>",
1038 .ivsize = DES_BLOCK_SIZE,
1039 .maxauthsize = SHA1_DIGEST_SIZE,
1040 },
1041 .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
1042 .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP,
1043 .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
1044 },
1045 {
1046 .name = "authenc(hmac(sha256),cbc(des))",
1047 .driver_name = "authenc-hmac-sha256-cbc-des-caam",
1048 .blocksize = DES_BLOCK_SIZE,
Yuan Kang885e9e22011-07-15 11:21:41 +08001049 .type = CRYPTO_ALG_TYPE_AEAD,
1050 .template_aead = {
Yuan Kang0e479302011-07-15 11:21:41 +08001051 .setkey = aead_setkey,
1052 .setauthsize = aead_setauthsize,
1053 .encrypt = aead_encrypt,
1054 .decrypt = aead_decrypt,
1055 .givencrypt = aead_givencrypt,
Kim Phillips8e8ec592011-03-13 16:54:26 +08001056 .geniv = "<built-in>",
1057 .ivsize = DES_BLOCK_SIZE,
1058 .maxauthsize = SHA256_DIGEST_SIZE,
1059 },
1060 .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
1061 .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
1062 OP_ALG_AAI_HMAC_PRECOMP,
1063 .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
1064 },
Kim Phillips4427b1b2011-05-14 22:08:17 -05001065 {
1066 .name = "authenc(hmac(sha512),cbc(des))",
1067 .driver_name = "authenc-hmac-sha512-cbc-des-caam",
1068 .blocksize = DES_BLOCK_SIZE,
Yuan Kang885e9e22011-07-15 11:21:41 +08001069 .type = CRYPTO_ALG_TYPE_AEAD,
1070 .template_aead = {
Yuan Kang0e479302011-07-15 11:21:41 +08001071 .setkey = aead_setkey,
1072 .setauthsize = aead_setauthsize,
1073 .encrypt = aead_encrypt,
1074 .decrypt = aead_decrypt,
1075 .givencrypt = aead_givencrypt,
Kim Phillips4427b1b2011-05-14 22:08:17 -05001076 .geniv = "<built-in>",
1077 .ivsize = DES_BLOCK_SIZE,
1078 .maxauthsize = SHA512_DIGEST_SIZE,
1079 },
1080 .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
1081 .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
1082 OP_ALG_AAI_HMAC_PRECOMP,
1083 .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
1084 },
Kim Phillips8e8ec592011-03-13 16:54:26 +08001085};
1086
1087struct caam_crypto_alg {
1088 struct list_head entry;
1089 struct device *ctrldev;
1090 int class1_alg_type;
1091 int class2_alg_type;
1092 int alg_op;
1093 struct crypto_alg crypto_alg;
1094};
1095
1096static int caam_cra_init(struct crypto_tfm *tfm)
1097{
1098 struct crypto_alg *alg = tfm->__crt_alg;
1099 struct caam_crypto_alg *caam_alg =
1100 container_of(alg, struct caam_crypto_alg, crypto_alg);
1101 struct caam_ctx *ctx = crypto_tfm_ctx(tfm);
1102 struct caam_drv_private *priv = dev_get_drvdata(caam_alg->ctrldev);
1103 int tgt_jr = atomic_inc_return(&priv->tfm_count);
1104
1105 /*
1106 * distribute tfms across job rings to ensure in-order
1107 * crypto request processing per tfm
1108 */
1109 ctx->jrdev = priv->algapi_jr[(tgt_jr / 2) % priv->num_jrs_for_algapi];
1110
1111 /* copy descriptor header template value */
1112 ctx->class1_alg_type = OP_TYPE_CLASS1_ALG | caam_alg->class1_alg_type;
1113 ctx->class2_alg_type = OP_TYPE_CLASS2_ALG | caam_alg->class2_alg_type;
1114 ctx->alg_op = OP_TYPE_CLASS2_ALG | caam_alg->alg_op;
1115
1116 return 0;
1117}
1118
1119static void caam_cra_exit(struct crypto_tfm *tfm)
1120{
1121 struct caam_ctx *ctx = crypto_tfm_ctx(tfm);
1122
1123 if (!dma_mapping_error(ctx->jrdev, ctx->shared_desc_phys))
1124 dma_unmap_single(ctx->jrdev, ctx->shared_desc_phys,
1125 desc_bytes(ctx->sh_desc), DMA_TO_DEVICE);
1126 kfree(ctx->sh_desc);
Kim Phillips4427b1b2011-05-14 22:08:17 -05001127
Yuan Kang885e9e22011-07-15 11:21:41 +08001128 if (!dma_mapping_error(ctx->jrdev, ctx->key_dma))
1129 dma_unmap_single(ctx->jrdev, ctx->key_dma,
Kim Phillips4427b1b2011-05-14 22:08:17 -05001130 ctx->split_key_pad_len + ctx->enckeylen,
1131 DMA_TO_DEVICE);
1132 kfree(ctx->key);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001133}
1134
1135static void __exit caam_algapi_exit(void)
1136{
1137
1138 struct device_node *dev_node;
1139 struct platform_device *pdev;
1140 struct device *ctrldev;
1141 struct caam_drv_private *priv;
1142 struct caam_crypto_alg *t_alg, *n;
1143 int i, err;
1144
Kim Phillips54e198d2011-03-23 21:15:44 +08001145 dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
Kim Phillips8e8ec592011-03-13 16:54:26 +08001146 if (!dev_node)
1147 return;
1148
1149 pdev = of_find_device_by_node(dev_node);
1150 if (!pdev)
1151 return;
1152
1153 ctrldev = &pdev->dev;
1154 of_node_put(dev_node);
1155 priv = dev_get_drvdata(ctrldev);
1156
1157 if (!priv->alg_list.next)
1158 return;
1159
1160 list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
1161 crypto_unregister_alg(&t_alg->crypto_alg);
1162 list_del(&t_alg->entry);
1163 kfree(t_alg);
1164 }
1165
1166 for (i = 0; i < priv->total_jobrs; i++) {
1167 err = caam_jr_deregister(priv->algapi_jr[i]);
1168 if (err < 0)
1169 break;
1170 }
1171 kfree(priv->algapi_jr);
1172}
1173
1174static struct caam_crypto_alg *caam_alg_alloc(struct device *ctrldev,
1175 struct caam_alg_template
1176 *template)
1177{
1178 struct caam_crypto_alg *t_alg;
1179 struct crypto_alg *alg;
1180
1181 t_alg = kzalloc(sizeof(struct caam_crypto_alg), GFP_KERNEL);
1182 if (!t_alg) {
1183 dev_err(ctrldev, "failed to allocate t_alg\n");
1184 return ERR_PTR(-ENOMEM);
1185 }
1186
1187 alg = &t_alg->crypto_alg;
1188
1189 snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", template->name);
1190 snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
1191 template->driver_name);
1192 alg->cra_module = THIS_MODULE;
1193 alg->cra_init = caam_cra_init;
1194 alg->cra_exit = caam_cra_exit;
1195 alg->cra_priority = CAAM_CRA_PRIORITY;
Kim Phillips8e8ec592011-03-13 16:54:26 +08001196 alg->cra_blocksize = template->blocksize;
1197 alg->cra_alignmask = 0;
Kim Phillips8e8ec592011-03-13 16:54:26 +08001198 alg->cra_ctxsize = sizeof(struct caam_ctx);
Yuan Kang885e9e22011-07-15 11:21:41 +08001199 alg->cra_flags = CRYPTO_ALG_ASYNC | template->type;
1200 switch (template->type) {
1201 case CRYPTO_ALG_TYPE_AEAD:
1202 alg->cra_type = &crypto_aead_type;
1203 alg->cra_aead = template->template_aead;
1204 break;
1205 }
Kim Phillips8e8ec592011-03-13 16:54:26 +08001206
1207 t_alg->class1_alg_type = template->class1_alg_type;
1208 t_alg->class2_alg_type = template->class2_alg_type;
1209 t_alg->alg_op = template->alg_op;
1210 t_alg->ctrldev = ctrldev;
1211
1212 return t_alg;
1213}
1214
1215static int __init caam_algapi_init(void)
1216{
1217 struct device_node *dev_node;
1218 struct platform_device *pdev;
1219 struct device *ctrldev, **jrdev;
1220 struct caam_drv_private *priv;
1221 int i = 0, err = 0;
1222
Kim Phillips54e198d2011-03-23 21:15:44 +08001223 dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
Kim Phillips8e8ec592011-03-13 16:54:26 +08001224 if (!dev_node)
1225 return -ENODEV;
1226
1227 pdev = of_find_device_by_node(dev_node);
1228 if (!pdev)
1229 return -ENODEV;
1230
1231 ctrldev = &pdev->dev;
1232 priv = dev_get_drvdata(ctrldev);
1233 of_node_put(dev_node);
1234
1235 INIT_LIST_HEAD(&priv->alg_list);
1236
1237 jrdev = kmalloc(sizeof(*jrdev) * priv->total_jobrs, GFP_KERNEL);
1238 if (!jrdev)
1239 return -ENOMEM;
1240
1241 for (i = 0; i < priv->total_jobrs; i++) {
1242 err = caam_jr_register(ctrldev, &jrdev[i]);
1243 if (err < 0)
1244 break;
1245 }
1246 if (err < 0 && i == 0) {
1247 dev_err(ctrldev, "algapi error in job ring registration: %d\n",
1248 err);
Julia Lawallb3b7f052011-04-08 20:39:23 +08001249 kfree(jrdev);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001250 return err;
1251 }
1252
1253 priv->num_jrs_for_algapi = i;
1254 priv->algapi_jr = jrdev;
1255 atomic_set(&priv->tfm_count, -1);
1256
1257 /* register crypto algorithms the device supports */
1258 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
1259 /* TODO: check if h/w supports alg */
1260 struct caam_crypto_alg *t_alg;
1261
1262 t_alg = caam_alg_alloc(ctrldev, &driver_algs[i]);
1263 if (IS_ERR(t_alg)) {
1264 err = PTR_ERR(t_alg);
1265 dev_warn(ctrldev, "%s alg allocation failed\n",
Dan Carpentercdc712d2011-03-23 21:20:27 +08001266 driver_algs[i].driver_name);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001267 continue;
1268 }
1269
1270 err = crypto_register_alg(&t_alg->crypto_alg);
1271 if (err) {
1272 dev_warn(ctrldev, "%s alg registration failed\n",
1273 t_alg->crypto_alg.cra_driver_name);
1274 kfree(t_alg);
1275 } else {
1276 list_add_tail(&t_alg->entry, &priv->alg_list);
1277 dev_info(ctrldev, "%s\n",
1278 t_alg->crypto_alg.cra_driver_name);
1279 }
1280 }
1281
1282 return err;
1283}
1284
1285module_init(caam_algapi_init);
1286module_exit(caam_algapi_exit);
1287
1288MODULE_LICENSE("GPL");
1289MODULE_DESCRIPTION("FSL CAAM support for crypto API");
1290MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");