blob: 018a95ce0c9b0d6e6a13bff1522630799bc445b3 [file] [log] [blame]
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +10001/*
2 * Support for Marvell's crypto engine which can be found on some Orion5X
3 * boards.
4 *
5 * Author: Sebastian Andrzej Siewior < sebastian at breakpoint dot cc >
6 * License: GPLv2
7 *
8 */
9#include <crypto/aes.h>
10#include <crypto/algapi.h>
11#include <linux/crypto.h>
12#include <linux/interrupt.h>
13#include <linux/io.h>
14#include <linux/kthread.h>
15#include <linux/platform_device.h>
16#include <linux/scatterlist.h>
17
18#include "mv_cesa.h"
19/*
20 * STM:
21 * /---------------------------------------\
22 * | | request complete
23 * \./ |
24 * IDLE -> new request -> BUSY -> done -> DEQUEUE
25 * /°\ |
26 * | | more scatter entries
27 * \________________/
28 */
29enum engine_status {
30 ENGINE_IDLE,
31 ENGINE_BUSY,
32 ENGINE_W_DEQUEUE,
33};
34
35/**
36 * struct req_progress - used for every crypt request
37 * @src_sg_it: sg iterator for src
38 * @dst_sg_it: sg iterator for dst
39 * @sg_src_left: bytes left in src to process (scatter list)
40 * @src_start: offset to add to src start position (scatter list)
41 * @crypt_len: length of current crypt process
42 * @sg_dst_left: bytes left dst to process in this scatter list
43 * @dst_start: offset to add to dst start position (scatter list)
44 * @total_req_bytes: total number of bytes processed (request).
45 *
46 * sg helper are used to iterate over the scatterlist. Since the size of the
47 * SRAM may be less than the scatter size, this struct struct is used to keep
48 * track of progress within current scatterlist.
49 */
50struct req_progress {
51 struct sg_mapping_iter src_sg_it;
52 struct sg_mapping_iter dst_sg_it;
53
54 /* src mostly */
55 int sg_src_left;
56 int src_start;
57 int crypt_len;
58 /* dst mostly */
59 int sg_dst_left;
60 int dst_start;
61 int total_req_bytes;
62};
63
64struct crypto_priv {
65 void __iomem *reg;
66 void __iomem *sram;
67 int irq;
68 struct task_struct *queue_th;
69
70 /* the lock protects queue and eng_st */
71 spinlock_t lock;
72 struct crypto_queue queue;
73 enum engine_status eng_st;
74 struct ablkcipher_request *cur_req;
75 struct req_progress p;
76 int max_req_size;
77 int sram_size;
78};
79
80static struct crypto_priv *cpg;
81
82struct mv_ctx {
83 u8 aes_enc_key[AES_KEY_LEN];
84 u32 aes_dec_key[8];
85 int key_len;
86 u32 need_calc_aes_dkey;
87};
88
89enum crypto_op {
90 COP_AES_ECB,
91 COP_AES_CBC,
92};
93
94struct mv_req_ctx {
95 enum crypto_op op;
96 int decrypt;
97};
98
99static void compute_aes_dec_key(struct mv_ctx *ctx)
100{
101 struct crypto_aes_ctx gen_aes_key;
102 int key_pos;
103
104 if (!ctx->need_calc_aes_dkey)
105 return;
106
107 crypto_aes_expand_key(&gen_aes_key, ctx->aes_enc_key, ctx->key_len);
108
109 key_pos = ctx->key_len + 24;
110 memcpy(ctx->aes_dec_key, &gen_aes_key.key_enc[key_pos], 4 * 4);
111 switch (ctx->key_len) {
112 case AES_KEYSIZE_256:
113 key_pos -= 2;
114 /* fall */
115 case AES_KEYSIZE_192:
116 key_pos -= 2;
117 memcpy(&ctx->aes_dec_key[4], &gen_aes_key.key_enc[key_pos],
118 4 * 4);
119 break;
120 }
121 ctx->need_calc_aes_dkey = 0;
122}
123
124static int mv_setkey_aes(struct crypto_ablkcipher *cipher, const u8 *key,
125 unsigned int len)
126{
127 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
128 struct mv_ctx *ctx = crypto_tfm_ctx(tfm);
129
130 switch (len) {
131 case AES_KEYSIZE_128:
132 case AES_KEYSIZE_192:
133 case AES_KEYSIZE_256:
134 break;
135 default:
136 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
137 return -EINVAL;
138 }
139 ctx->key_len = len;
140 ctx->need_calc_aes_dkey = 1;
141
142 memcpy(ctx->aes_enc_key, key, AES_KEY_LEN);
143 return 0;
144}
145
146static void setup_data_in(struct ablkcipher_request *req)
147{
148 int ret;
149 void *buf;
150
151 if (!cpg->p.sg_src_left) {
152 ret = sg_miter_next(&cpg->p.src_sg_it);
153 BUG_ON(!ret);
154 cpg->p.sg_src_left = cpg->p.src_sg_it.length;
155 cpg->p.src_start = 0;
156 }
157
158 cpg->p.crypt_len = min(cpg->p.sg_src_left, cpg->max_req_size);
159
160 buf = cpg->p.src_sg_it.addr;
161 buf += cpg->p.src_start;
162
163 memcpy(cpg->sram + SRAM_DATA_IN_START, buf, cpg->p.crypt_len);
164
165 cpg->p.sg_src_left -= cpg->p.crypt_len;
166 cpg->p.src_start += cpg->p.crypt_len;
167}
168
169static void mv_process_current_q(int first_block)
170{
171 struct ablkcipher_request *req = cpg->cur_req;
172 struct mv_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
173 struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
174 struct sec_accel_config op;
175
176 switch (req_ctx->op) {
177 case COP_AES_ECB:
178 op.config = CFG_OP_CRYPT_ONLY | CFG_ENCM_AES | CFG_ENC_MODE_ECB;
179 break;
180 case COP_AES_CBC:
Uri Simchoni6bc6fcd2010-04-08 19:25:56 +0300181 default:
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000182 op.config = CFG_OP_CRYPT_ONLY | CFG_ENCM_AES | CFG_ENC_MODE_CBC;
183 op.enc_iv = ENC_IV_POINT(SRAM_DATA_IV) |
184 ENC_IV_BUF_POINT(SRAM_DATA_IV_BUF);
185 if (first_block)
186 memcpy(cpg->sram + SRAM_DATA_IV, req->info, 16);
187 break;
188 }
189 if (req_ctx->decrypt) {
190 op.config |= CFG_DIR_DEC;
191 memcpy(cpg->sram + SRAM_DATA_KEY_P, ctx->aes_dec_key,
192 AES_KEY_LEN);
193 } else {
194 op.config |= CFG_DIR_ENC;
195 memcpy(cpg->sram + SRAM_DATA_KEY_P, ctx->aes_enc_key,
196 AES_KEY_LEN);
197 }
198
199 switch (ctx->key_len) {
200 case AES_KEYSIZE_128:
201 op.config |= CFG_AES_LEN_128;
202 break;
203 case AES_KEYSIZE_192:
204 op.config |= CFG_AES_LEN_192;
205 break;
206 case AES_KEYSIZE_256:
207 op.config |= CFG_AES_LEN_256;
208 break;
209 }
210 op.enc_p = ENC_P_SRC(SRAM_DATA_IN_START) |
211 ENC_P_DST(SRAM_DATA_OUT_START);
212 op.enc_key_p = SRAM_DATA_KEY_P;
213
214 setup_data_in(req);
215 op.enc_len = cpg->p.crypt_len;
216 memcpy(cpg->sram + SRAM_CONFIG, &op,
217 sizeof(struct sec_accel_config));
218
219 writel(SRAM_CONFIG, cpg->reg + SEC_ACCEL_DESC_P0);
220 /* GO */
221 writel(SEC_CMD_EN_SEC_ACCL0, cpg->reg + SEC_ACCEL_CMD);
222
223 /*
224 * XXX: add timer if the interrupt does not occur for some mystery
225 * reason
226 */
227}
228
229static void mv_crypto_algo_completion(void)
230{
231 struct ablkcipher_request *req = cpg->cur_req;
232 struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
233
234 if (req_ctx->op != COP_AES_CBC)
235 return ;
236
237 memcpy(req->info, cpg->sram + SRAM_DATA_IV_BUF, 16);
238}
239
240static void dequeue_complete_req(void)
241{
242 struct ablkcipher_request *req = cpg->cur_req;
243 void *buf;
244 int ret;
Uri Simchonif565e672010-04-08 19:26:34 +0300245 int need_copy_len = cpg->p.crypt_len;
246 int sram_offset = 0;
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000247
248 cpg->p.total_req_bytes += cpg->p.crypt_len;
249 do {
250 int dst_copy;
251
252 if (!cpg->p.sg_dst_left) {
253 ret = sg_miter_next(&cpg->p.dst_sg_it);
254 BUG_ON(!ret);
255 cpg->p.sg_dst_left = cpg->p.dst_sg_it.length;
256 cpg->p.dst_start = 0;
257 }
258
259 buf = cpg->p.dst_sg_it.addr;
260 buf += cpg->p.dst_start;
261
Uri Simchonif565e672010-04-08 19:26:34 +0300262 dst_copy = min(need_copy_len, cpg->p.sg_dst_left);
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000263
Uri Simchonif565e672010-04-08 19:26:34 +0300264 memcpy(buf,
265 cpg->sram + SRAM_DATA_OUT_START + sram_offset,
266 dst_copy);
267 sram_offset += dst_copy;
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000268 cpg->p.sg_dst_left -= dst_copy;
Uri Simchonif565e672010-04-08 19:26:34 +0300269 need_copy_len -= dst_copy;
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000270 cpg->p.dst_start += dst_copy;
Uri Simchonif565e672010-04-08 19:26:34 +0300271 } while (need_copy_len > 0);
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000272
273 BUG_ON(cpg->eng_st != ENGINE_W_DEQUEUE);
274 if (cpg->p.total_req_bytes < req->nbytes) {
275 /* process next scatter list entry */
276 cpg->eng_st = ENGINE_BUSY;
277 mv_process_current_q(0);
278 } else {
279 sg_miter_stop(&cpg->p.src_sg_it);
280 sg_miter_stop(&cpg->p.dst_sg_it);
281 mv_crypto_algo_completion();
282 cpg->eng_st = ENGINE_IDLE;
Uri Simchoni0328ac22010-04-08 19:25:37 +0300283 local_bh_disable();
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000284 req->base.complete(&req->base, 0);
Uri Simchoni0328ac22010-04-08 19:25:37 +0300285 local_bh_enable();
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000286 }
287}
288
289static int count_sgs(struct scatterlist *sl, unsigned int total_bytes)
290{
291 int i = 0;
292
293 do {
294 total_bytes -= sl[i].length;
295 i++;
296
297 } while (total_bytes > 0);
298
299 return i;
300}
301
302static void mv_enqueue_new_req(struct ablkcipher_request *req)
303{
304 int num_sgs;
305
306 cpg->cur_req = req;
307 memset(&cpg->p, 0, sizeof(struct req_progress));
308
309 num_sgs = count_sgs(req->src, req->nbytes);
310 sg_miter_start(&cpg->p.src_sg_it, req->src, num_sgs, SG_MITER_FROM_SG);
311
312 num_sgs = count_sgs(req->dst, req->nbytes);
313 sg_miter_start(&cpg->p.dst_sg_it, req->dst, num_sgs, SG_MITER_TO_SG);
314 mv_process_current_q(1);
315}
316
317static int queue_manag(void *data)
318{
319 cpg->eng_st = ENGINE_IDLE;
320 do {
321 struct ablkcipher_request *req;
322 struct crypto_async_request *async_req = NULL;
323 struct crypto_async_request *backlog;
324
325 __set_current_state(TASK_INTERRUPTIBLE);
326
327 if (cpg->eng_st == ENGINE_W_DEQUEUE)
328 dequeue_complete_req();
329
330 spin_lock_irq(&cpg->lock);
331 if (cpg->eng_st == ENGINE_IDLE) {
332 backlog = crypto_get_backlog(&cpg->queue);
333 async_req = crypto_dequeue_request(&cpg->queue);
334 if (async_req) {
335 BUG_ON(cpg->eng_st != ENGINE_IDLE);
336 cpg->eng_st = ENGINE_BUSY;
337 }
338 }
339 spin_unlock_irq(&cpg->lock);
340
341 if (backlog) {
342 backlog->complete(backlog, -EINPROGRESS);
343 backlog = NULL;
344 }
345
346 if (async_req) {
347 req = container_of(async_req,
348 struct ablkcipher_request, base);
349 mv_enqueue_new_req(req);
350 async_req = NULL;
351 }
352
353 schedule();
354
355 } while (!kthread_should_stop());
356 return 0;
357}
358
359static int mv_handle_req(struct ablkcipher_request *req)
360{
361 unsigned long flags;
362 int ret;
363
364 spin_lock_irqsave(&cpg->lock, flags);
365 ret = ablkcipher_enqueue_request(&cpg->queue, req);
366 spin_unlock_irqrestore(&cpg->lock, flags);
367 wake_up_process(cpg->queue_th);
368 return ret;
369}
370
371static int mv_enc_aes_ecb(struct ablkcipher_request *req)
372{
373 struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
374
375 req_ctx->op = COP_AES_ECB;
376 req_ctx->decrypt = 0;
377
378 return mv_handle_req(req);
379}
380
381static int mv_dec_aes_ecb(struct ablkcipher_request *req)
382{
383 struct mv_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
384 struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
385
386 req_ctx->op = COP_AES_ECB;
387 req_ctx->decrypt = 1;
388
389 compute_aes_dec_key(ctx);
390 return mv_handle_req(req);
391}
392
393static int mv_enc_aes_cbc(struct ablkcipher_request *req)
394{
395 struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
396
397 req_ctx->op = COP_AES_CBC;
398 req_ctx->decrypt = 0;
399
400 return mv_handle_req(req);
401}
402
403static int mv_dec_aes_cbc(struct ablkcipher_request *req)
404{
405 struct mv_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
406 struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
407
408 req_ctx->op = COP_AES_CBC;
409 req_ctx->decrypt = 1;
410
411 compute_aes_dec_key(ctx);
412 return mv_handle_req(req);
413}
414
415static int mv_cra_init(struct crypto_tfm *tfm)
416{
417 tfm->crt_ablkcipher.reqsize = sizeof(struct mv_req_ctx);
418 return 0;
419}
420
421irqreturn_t crypto_int(int irq, void *priv)
422{
423 u32 val;
424
425 val = readl(cpg->reg + SEC_ACCEL_INT_STATUS);
426 if (!(val & SEC_INT_ACCEL0_DONE))
427 return IRQ_NONE;
428
429 val &= ~SEC_INT_ACCEL0_DONE;
430 writel(val, cpg->reg + FPGA_INT_STATUS);
431 writel(val, cpg->reg + SEC_ACCEL_INT_STATUS);
432 BUG_ON(cpg->eng_st != ENGINE_BUSY);
433 cpg->eng_st = ENGINE_W_DEQUEUE;
434 wake_up_process(cpg->queue_th);
435 return IRQ_HANDLED;
436}
437
438struct crypto_alg mv_aes_alg_ecb = {
439 .cra_name = "ecb(aes)",
440 .cra_driver_name = "mv-ecb-aes",
441 .cra_priority = 300,
442 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
443 .cra_blocksize = 16,
444 .cra_ctxsize = sizeof(struct mv_ctx),
445 .cra_alignmask = 0,
446 .cra_type = &crypto_ablkcipher_type,
447 .cra_module = THIS_MODULE,
448 .cra_init = mv_cra_init,
449 .cra_u = {
450 .ablkcipher = {
451 .min_keysize = AES_MIN_KEY_SIZE,
452 .max_keysize = AES_MAX_KEY_SIZE,
453 .setkey = mv_setkey_aes,
454 .encrypt = mv_enc_aes_ecb,
455 .decrypt = mv_dec_aes_ecb,
456 },
457 },
458};
459
460struct crypto_alg mv_aes_alg_cbc = {
461 .cra_name = "cbc(aes)",
462 .cra_driver_name = "mv-cbc-aes",
463 .cra_priority = 300,
464 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
465 .cra_blocksize = AES_BLOCK_SIZE,
466 .cra_ctxsize = sizeof(struct mv_ctx),
467 .cra_alignmask = 0,
468 .cra_type = &crypto_ablkcipher_type,
469 .cra_module = THIS_MODULE,
470 .cra_init = mv_cra_init,
471 .cra_u = {
472 .ablkcipher = {
473 .ivsize = AES_BLOCK_SIZE,
474 .min_keysize = AES_MIN_KEY_SIZE,
475 .max_keysize = AES_MAX_KEY_SIZE,
476 .setkey = mv_setkey_aes,
477 .encrypt = mv_enc_aes_cbc,
478 .decrypt = mv_dec_aes_cbc,
479 },
480 },
481};
482
483static int mv_probe(struct platform_device *pdev)
484{
485 struct crypto_priv *cp;
486 struct resource *res;
487 int irq;
488 int ret;
489
490 if (cpg) {
491 printk(KERN_ERR "Second crypto dev?\n");
492 return -EEXIST;
493 }
494
495 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
496 if (!res)
497 return -ENXIO;
498
499 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
500 if (!cp)
501 return -ENOMEM;
502
503 spin_lock_init(&cp->lock);
504 crypto_init_queue(&cp->queue, 50);
505 cp->reg = ioremap(res->start, res->end - res->start + 1);
506 if (!cp->reg) {
507 ret = -ENOMEM;
508 goto err;
509 }
510
511 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sram");
512 if (!res) {
513 ret = -ENXIO;
514 goto err_unmap_reg;
515 }
516 cp->sram_size = res->end - res->start + 1;
517 cp->max_req_size = cp->sram_size - SRAM_CFG_SPACE;
518 cp->sram = ioremap(res->start, cp->sram_size);
519 if (!cp->sram) {
520 ret = -ENOMEM;
521 goto err_unmap_reg;
522 }
523
524 irq = platform_get_irq(pdev, 0);
525 if (irq < 0 || irq == NO_IRQ) {
526 ret = irq;
527 goto err_unmap_sram;
528 }
529 cp->irq = irq;
530
531 platform_set_drvdata(pdev, cp);
532 cpg = cp;
533
534 cp->queue_th = kthread_run(queue_manag, cp, "mv_crypto");
535 if (IS_ERR(cp->queue_th)) {
536 ret = PTR_ERR(cp->queue_th);
537 goto err_thread;
538 }
539
540 ret = request_irq(irq, crypto_int, IRQF_DISABLED, dev_name(&pdev->dev),
541 cp);
542 if (ret)
543 goto err_unmap_sram;
544
545 writel(SEC_INT_ACCEL0_DONE, cpg->reg + SEC_ACCEL_INT_MASK);
546 writel(SEC_CFG_STOP_DIG_ERR, cpg->reg + SEC_ACCEL_CFG);
547
548 ret = crypto_register_alg(&mv_aes_alg_ecb);
549 if (ret)
550 goto err_reg;
551
552 ret = crypto_register_alg(&mv_aes_alg_cbc);
553 if (ret)
554 goto err_unreg_ecb;
555 return 0;
556err_unreg_ecb:
557 crypto_unregister_alg(&mv_aes_alg_ecb);
558err_thread:
559 free_irq(irq, cp);
560err_reg:
561 kthread_stop(cp->queue_th);
562err_unmap_sram:
563 iounmap(cp->sram);
564err_unmap_reg:
565 iounmap(cp->reg);
566err:
567 kfree(cp);
568 cpg = NULL;
569 platform_set_drvdata(pdev, NULL);
570 return ret;
571}
572
573static int mv_remove(struct platform_device *pdev)
574{
575 struct crypto_priv *cp = platform_get_drvdata(pdev);
576
577 crypto_unregister_alg(&mv_aes_alg_ecb);
578 crypto_unregister_alg(&mv_aes_alg_cbc);
579 kthread_stop(cp->queue_th);
580 free_irq(cp->irq, cp);
581 memset(cp->sram, 0, cp->sram_size);
582 iounmap(cp->sram);
583 iounmap(cp->reg);
584 kfree(cp);
585 cpg = NULL;
586 return 0;
587}
588
589static struct platform_driver marvell_crypto = {
590 .probe = mv_probe,
591 .remove = mv_remove,
592 .driver = {
593 .owner = THIS_MODULE,
594 .name = "mv_crypto",
595 },
596};
597MODULE_ALIAS("platform:mv_crypto");
598
599static int __init mv_crypto_init(void)
600{
601 return platform_driver_register(&marvell_crypto);
602}
603module_init(mv_crypto_init);
604
605static void __exit mv_crypto_exit(void)
606{
607 platform_driver_unregister(&marvell_crypto);
608}
609module_exit(mv_crypto_exit);
610
611MODULE_AUTHOR("Sebastian Andrzej Siewior <sebastian@breakpoint.cc>");
612MODULE_DESCRIPTION("Support for Marvell's cryptographic engine");
613MODULE_LICENSE("GPL");