]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/crypto/marvell/cesa.c
crypto: marvell - Add a complete operation for async requests
[karo-tx-linux.git] / drivers / crypto / marvell / cesa.c
1 /*
2  * Support for Marvell's Cryptographic Engine and Security Accelerator (CESA)
3  * that can be found on the following platform: Orion, Kirkwood, Armada. This
4  * driver supports the TDMA engine on platforms on which it is available.
5  *
6  * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
7  * Author: Arnaud Ebalard <arno@natisbad.org>
8  *
9  * This work is based on an initial version written by
10  * Sebastian Andrzej Siewior < sebastian at breakpoint dot cc >
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License version 2 as published
14  * by the Free Software Foundation.
15  */
16
17 #include <linux/delay.h>
18 #include <linux/genalloc.h>
19 #include <linux/interrupt.h>
20 #include <linux/io.h>
21 #include <linux/kthread.h>
22 #include <linux/mbus.h>
23 #include <linux/platform_device.h>
24 #include <linux/scatterlist.h>
25 #include <linux/slab.h>
26 #include <linux/module.h>
27 #include <linux/clk.h>
28 #include <linux/of.h>
29 #include <linux/of_platform.h>
30 #include <linux/of_irq.h>
31
32 #include "cesa.h"
33
34 /* Limit of the crypto queue before reaching the backlog */
35 #define CESA_CRYPTO_DEFAULT_MAX_QLEN 50
36
37 static int allhwsupport = !IS_ENABLED(CONFIG_CRYPTO_DEV_MV_CESA);
38 module_param_named(allhwsupport, allhwsupport, int, 0444);
39 MODULE_PARM_DESC(allhwsupport, "Enable support for all hardware (even it if overlaps with the mv_cesa driver)");
40
41 struct mv_cesa_dev *cesa_dev;
42
43 static void mv_cesa_dequeue_req_unlocked(struct mv_cesa_engine *engine)
44 {
45         struct crypto_async_request *req, *backlog;
46         struct mv_cesa_ctx *ctx;
47
48         spin_lock_bh(&cesa_dev->lock);
49         backlog = crypto_get_backlog(&cesa_dev->queue);
50         req = crypto_dequeue_request(&cesa_dev->queue);
51         engine->req = req;
52         spin_unlock_bh(&cesa_dev->lock);
53
54         if (!req)
55                 return;
56
57         if (backlog)
58                 backlog->complete(backlog, -EINPROGRESS);
59
60         ctx = crypto_tfm_ctx(req->tfm);
61         ctx->ops->prepare(req, engine);
62         ctx->ops->step(req);
63 }
64
65 static irqreturn_t mv_cesa_int(int irq, void *priv)
66 {
67         struct mv_cesa_engine *engine = priv;
68         struct crypto_async_request *req;
69         struct mv_cesa_ctx *ctx;
70         u32 status, mask;
71         irqreturn_t ret = IRQ_NONE;
72
73         while (true) {
74                 int res;
75
76                 mask = mv_cesa_get_int_mask(engine);
77                 status = readl(engine->regs + CESA_SA_INT_STATUS);
78
79                 if (!(status & mask))
80                         break;
81
82                 /*
83                  * TODO: avoid clearing the FPGA_INT_STATUS if this not
84                  * relevant on some platforms.
85                  */
86                 writel(~status, engine->regs + CESA_SA_FPGA_INT_STATUS);
87                 writel(~status, engine->regs + CESA_SA_INT_STATUS);
88
89                 ret = IRQ_HANDLED;
90                 spin_lock_bh(&engine->lock);
91                 req = engine->req;
92                 spin_unlock_bh(&engine->lock);
93                 if (req) {
94                         ctx = crypto_tfm_ctx(req->tfm);
95                         res = ctx->ops->process(req, status & mask);
96                         if (res != -EINPROGRESS) {
97                                 spin_lock_bh(&engine->lock);
98                                 engine->req = NULL;
99                                 mv_cesa_dequeue_req_unlocked(engine);
100                                 spin_unlock_bh(&engine->lock);
101                                 ctx->ops->complete(req);
102                                 ctx->ops->cleanup(req);
103                                 local_bh_disable();
104                                 req->complete(req, res);
105                                 local_bh_enable();
106                         } else {
107                                 ctx->ops->step(req);
108                         }
109                 }
110         }
111
112         return ret;
113 }
114
115 int mv_cesa_queue_req(struct crypto_async_request *req,
116                       struct mv_cesa_req *creq)
117 {
118         int ret;
119         int i;
120
121         spin_lock_bh(&cesa_dev->lock);
122         ret = crypto_enqueue_request(&cesa_dev->queue, req);
123         spin_unlock_bh(&cesa_dev->lock);
124
125         if (ret != -EINPROGRESS)
126                 return ret;
127
128         for (i = 0; i < cesa_dev->caps->nengines; i++) {
129                 spin_lock_bh(&cesa_dev->engines[i].lock);
130                 if (!cesa_dev->engines[i].req)
131                         mv_cesa_dequeue_req_unlocked(&cesa_dev->engines[i]);
132                 spin_unlock_bh(&cesa_dev->engines[i].lock);
133         }
134
135         return -EINPROGRESS;
136 }
137
138 static int mv_cesa_add_algs(struct mv_cesa_dev *cesa)
139 {
140         int ret;
141         int i, j;
142
143         for (i = 0; i < cesa->caps->ncipher_algs; i++) {
144                 ret = crypto_register_alg(cesa->caps->cipher_algs[i]);
145                 if (ret)
146                         goto err_unregister_crypto;
147         }
148
149         for (i = 0; i < cesa->caps->nahash_algs; i++) {
150                 ret = crypto_register_ahash(cesa->caps->ahash_algs[i]);
151                 if (ret)
152                         goto err_unregister_ahash;
153         }
154
155         return 0;
156
157 err_unregister_ahash:
158         for (j = 0; j < i; j++)
159                 crypto_unregister_ahash(cesa->caps->ahash_algs[j]);
160         i = cesa->caps->ncipher_algs;
161
162 err_unregister_crypto:
163         for (j = 0; j < i; j++)
164                 crypto_unregister_alg(cesa->caps->cipher_algs[j]);
165
166         return ret;
167 }
168
169 static void mv_cesa_remove_algs(struct mv_cesa_dev *cesa)
170 {
171         int i;
172
173         for (i = 0; i < cesa->caps->nahash_algs; i++)
174                 crypto_unregister_ahash(cesa->caps->ahash_algs[i]);
175
176         for (i = 0; i < cesa->caps->ncipher_algs; i++)
177                 crypto_unregister_alg(cesa->caps->cipher_algs[i]);
178 }
179
180 static struct crypto_alg *orion_cipher_algs[] = {
181         &mv_cesa_ecb_des_alg,
182         &mv_cesa_cbc_des_alg,
183         &mv_cesa_ecb_des3_ede_alg,
184         &mv_cesa_cbc_des3_ede_alg,
185         &mv_cesa_ecb_aes_alg,
186         &mv_cesa_cbc_aes_alg,
187 };
188
189 static struct ahash_alg *orion_ahash_algs[] = {
190         &mv_md5_alg,
191         &mv_sha1_alg,
192         &mv_ahmac_md5_alg,
193         &mv_ahmac_sha1_alg,
194 };
195
196 static struct crypto_alg *armada_370_cipher_algs[] = {
197         &mv_cesa_ecb_des_alg,
198         &mv_cesa_cbc_des_alg,
199         &mv_cesa_ecb_des3_ede_alg,
200         &mv_cesa_cbc_des3_ede_alg,
201         &mv_cesa_ecb_aes_alg,
202         &mv_cesa_cbc_aes_alg,
203 };
204
205 static struct ahash_alg *armada_370_ahash_algs[] = {
206         &mv_md5_alg,
207         &mv_sha1_alg,
208         &mv_sha256_alg,
209         &mv_ahmac_md5_alg,
210         &mv_ahmac_sha1_alg,
211         &mv_ahmac_sha256_alg,
212 };
213
214 static const struct mv_cesa_caps orion_caps = {
215         .nengines = 1,
216         .cipher_algs = orion_cipher_algs,
217         .ncipher_algs = ARRAY_SIZE(orion_cipher_algs),
218         .ahash_algs = orion_ahash_algs,
219         .nahash_algs = ARRAY_SIZE(orion_ahash_algs),
220         .has_tdma = false,
221 };
222
223 static const struct mv_cesa_caps kirkwood_caps = {
224         .nengines = 1,
225         .cipher_algs = orion_cipher_algs,
226         .ncipher_algs = ARRAY_SIZE(orion_cipher_algs),
227         .ahash_algs = orion_ahash_algs,
228         .nahash_algs = ARRAY_SIZE(orion_ahash_algs),
229         .has_tdma = true,
230 };
231
232 static const struct mv_cesa_caps armada_370_caps = {
233         .nengines = 1,
234         .cipher_algs = armada_370_cipher_algs,
235         .ncipher_algs = ARRAY_SIZE(armada_370_cipher_algs),
236         .ahash_algs = armada_370_ahash_algs,
237         .nahash_algs = ARRAY_SIZE(armada_370_ahash_algs),
238         .has_tdma = true,
239 };
240
241 static const struct mv_cesa_caps armada_xp_caps = {
242         .nengines = 2,
243         .cipher_algs = armada_370_cipher_algs,
244         .ncipher_algs = ARRAY_SIZE(armada_370_cipher_algs),
245         .ahash_algs = armada_370_ahash_algs,
246         .nahash_algs = ARRAY_SIZE(armada_370_ahash_algs),
247         .has_tdma = true,
248 };
249
250 static const struct of_device_id mv_cesa_of_match_table[] = {
251         { .compatible = "marvell,orion-crypto", .data = &orion_caps },
252         { .compatible = "marvell,kirkwood-crypto", .data = &kirkwood_caps },
253         { .compatible = "marvell,dove-crypto", .data = &kirkwood_caps },
254         { .compatible = "marvell,armada-370-crypto", .data = &armada_370_caps },
255         { .compatible = "marvell,armada-xp-crypto", .data = &armada_xp_caps },
256         { .compatible = "marvell,armada-375-crypto", .data = &armada_xp_caps },
257         { .compatible = "marvell,armada-38x-crypto", .data = &armada_xp_caps },
258         {}
259 };
260 MODULE_DEVICE_TABLE(of, mv_cesa_of_match_table);
261
262 static void
263 mv_cesa_conf_mbus_windows(struct mv_cesa_engine *engine,
264                           const struct mbus_dram_target_info *dram)
265 {
266         void __iomem *iobase = engine->regs;
267         int i;
268
269         for (i = 0; i < 4; i++) {
270                 writel(0, iobase + CESA_TDMA_WINDOW_CTRL(i));
271                 writel(0, iobase + CESA_TDMA_WINDOW_BASE(i));
272         }
273
274         for (i = 0; i < dram->num_cs; i++) {
275                 const struct mbus_dram_window *cs = dram->cs + i;
276
277                 writel(((cs->size - 1) & 0xffff0000) |
278                        (cs->mbus_attr << 8) |
279                        (dram->mbus_dram_target_id << 4) | 1,
280                        iobase + CESA_TDMA_WINDOW_CTRL(i));
281                 writel(cs->base, iobase + CESA_TDMA_WINDOW_BASE(i));
282         }
283 }
284
285 static int mv_cesa_dev_dma_init(struct mv_cesa_dev *cesa)
286 {
287         struct device *dev = cesa->dev;
288         struct mv_cesa_dev_dma *dma;
289
290         if (!cesa->caps->has_tdma)
291                 return 0;
292
293         dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
294         if (!dma)
295                 return -ENOMEM;
296
297         dma->tdma_desc_pool = dmam_pool_create("tdma_desc", dev,
298                                         sizeof(struct mv_cesa_tdma_desc),
299                                         16, 0);
300         if (!dma->tdma_desc_pool)
301                 return -ENOMEM;
302
303         dma->op_pool = dmam_pool_create("cesa_op", dev,
304                                         sizeof(struct mv_cesa_op_ctx), 16, 0);
305         if (!dma->op_pool)
306                 return -ENOMEM;
307
308         dma->cache_pool = dmam_pool_create("cesa_cache", dev,
309                                            CESA_MAX_HASH_BLOCK_SIZE, 1, 0);
310         if (!dma->cache_pool)
311                 return -ENOMEM;
312
313         dma->padding_pool = dmam_pool_create("cesa_padding", dev, 72, 1, 0);
314         if (!dma->padding_pool)
315                 return -ENOMEM;
316
317         dma->iv_pool = dmam_pool_create("cesa_iv", dev, 16, 1, 0);
318         if (!dma->iv_pool)
319                 return -ENOMEM;
320
321         cesa->dma = dma;
322
323         return 0;
324 }
325
326 static int mv_cesa_get_sram(struct platform_device *pdev, int idx)
327 {
328         struct mv_cesa_dev *cesa = platform_get_drvdata(pdev);
329         struct mv_cesa_engine *engine = &cesa->engines[idx];
330         const char *res_name = "sram";
331         struct resource *res;
332
333         engine->pool = of_gen_pool_get(cesa->dev->of_node,
334                                        "marvell,crypto-srams", idx);
335         if (engine->pool) {
336                 engine->sram = gen_pool_dma_alloc(engine->pool,
337                                                   cesa->sram_size,
338                                                   &engine->sram_dma);
339                 if (engine->sram)
340                         return 0;
341
342                 engine->pool = NULL;
343                 return -ENOMEM;
344         }
345
346         if (cesa->caps->nengines > 1) {
347                 if (!idx)
348                         res_name = "sram0";
349                 else
350                         res_name = "sram1";
351         }
352
353         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
354                                            res_name);
355         if (!res || resource_size(res) < cesa->sram_size)
356                 return -EINVAL;
357
358         engine->sram = devm_ioremap_resource(cesa->dev, res);
359         if (IS_ERR(engine->sram))
360                 return PTR_ERR(engine->sram);
361
362         engine->sram_dma = phys_to_dma(cesa->dev,
363                                        (phys_addr_t)res->start);
364
365         return 0;
366 }
367
368 static void mv_cesa_put_sram(struct platform_device *pdev, int idx)
369 {
370         struct mv_cesa_dev *cesa = platform_get_drvdata(pdev);
371         struct mv_cesa_engine *engine = &cesa->engines[idx];
372
373         if (!engine->pool)
374                 return;
375
376         gen_pool_free(engine->pool, (unsigned long)engine->sram,
377                       cesa->sram_size);
378 }
379
380 static int mv_cesa_probe(struct platform_device *pdev)
381 {
382         const struct mv_cesa_caps *caps = &orion_caps;
383         const struct mbus_dram_target_info *dram;
384         const struct of_device_id *match;
385         struct device *dev = &pdev->dev;
386         struct mv_cesa_dev *cesa;
387         struct mv_cesa_engine *engines;
388         struct resource *res;
389         int irq, ret, i;
390         u32 sram_size;
391
392         if (cesa_dev) {
393                 dev_err(&pdev->dev, "Only one CESA device authorized\n");
394                 return -EEXIST;
395         }
396
397         if (dev->of_node) {
398                 match = of_match_node(mv_cesa_of_match_table, dev->of_node);
399                 if (!match || !match->data)
400                         return -ENOTSUPP;
401
402                 caps = match->data;
403         }
404
405         if ((caps == &orion_caps || caps == &kirkwood_caps) && !allhwsupport)
406                 return -ENOTSUPP;
407
408         cesa = devm_kzalloc(dev, sizeof(*cesa), GFP_KERNEL);
409         if (!cesa)
410                 return -ENOMEM;
411
412         cesa->caps = caps;
413         cesa->dev = dev;
414
415         sram_size = CESA_SA_DEFAULT_SRAM_SIZE;
416         of_property_read_u32(cesa->dev->of_node, "marvell,crypto-sram-size",
417                              &sram_size);
418         if (sram_size < CESA_SA_MIN_SRAM_SIZE)
419                 sram_size = CESA_SA_MIN_SRAM_SIZE;
420
421         cesa->sram_size = sram_size;
422         cesa->engines = devm_kzalloc(dev, caps->nengines * sizeof(*engines),
423                                      GFP_KERNEL);
424         if (!cesa->engines)
425                 return -ENOMEM;
426
427         spin_lock_init(&cesa->lock);
428         crypto_init_queue(&cesa->queue, CESA_CRYPTO_DEFAULT_MAX_QLEN);
429         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
430         cesa->regs = devm_ioremap_resource(dev, res);
431         if (IS_ERR(cesa->regs))
432                 return PTR_ERR(cesa->regs);
433
434         ret = mv_cesa_dev_dma_init(cesa);
435         if (ret)
436                 return ret;
437
438         dram = mv_mbus_dram_info_nooverlap();
439
440         platform_set_drvdata(pdev, cesa);
441
442         for (i = 0; i < caps->nengines; i++) {
443                 struct mv_cesa_engine *engine = &cesa->engines[i];
444                 char res_name[7];
445
446                 engine->id = i;
447                 spin_lock_init(&engine->lock);
448
449                 ret = mv_cesa_get_sram(pdev, i);
450                 if (ret)
451                         goto err_cleanup;
452
453                 irq = platform_get_irq(pdev, i);
454                 if (irq < 0) {
455                         ret = irq;
456                         goto err_cleanup;
457                 }
458
459                 /*
460                  * Not all platforms can gate the CESA clocks: do not complain
461                  * if the clock does not exist.
462                  */
463                 snprintf(res_name, sizeof(res_name), "cesa%d", i);
464                 engine->clk = devm_clk_get(dev, res_name);
465                 if (IS_ERR(engine->clk)) {
466                         engine->clk = devm_clk_get(dev, NULL);
467                         if (IS_ERR(engine->clk))
468                                 engine->clk = NULL;
469                 }
470
471                 snprintf(res_name, sizeof(res_name), "cesaz%d", i);
472                 engine->zclk = devm_clk_get(dev, res_name);
473                 if (IS_ERR(engine->zclk))
474                         engine->zclk = NULL;
475
476                 ret = clk_prepare_enable(engine->clk);
477                 if (ret)
478                         goto err_cleanup;
479
480                 ret = clk_prepare_enable(engine->zclk);
481                 if (ret)
482                         goto err_cleanup;
483
484                 engine->regs = cesa->regs + CESA_ENGINE_OFF(i);
485
486                 if (dram && cesa->caps->has_tdma)
487                         mv_cesa_conf_mbus_windows(engine, dram);
488
489                 writel(0, engine->regs + CESA_SA_INT_STATUS);
490                 writel(CESA_SA_CFG_STOP_DIG_ERR,
491                        engine->regs + CESA_SA_CFG);
492                 writel(engine->sram_dma & CESA_SA_SRAM_MSK,
493                        engine->regs + CESA_SA_DESC_P0);
494
495                 ret = devm_request_threaded_irq(dev, irq, NULL, mv_cesa_int,
496                                                 IRQF_ONESHOT,
497                                                 dev_name(&pdev->dev),
498                                                 engine);
499                 if (ret)
500                         goto err_cleanup;
501         }
502
503         cesa_dev = cesa;
504
505         ret = mv_cesa_add_algs(cesa);
506         if (ret) {
507                 cesa_dev = NULL;
508                 goto err_cleanup;
509         }
510
511         dev_info(dev, "CESA device successfully registered\n");
512
513         return 0;
514
515 err_cleanup:
516         for (i = 0; i < caps->nengines; i++) {
517                 clk_disable_unprepare(cesa->engines[i].zclk);
518                 clk_disable_unprepare(cesa->engines[i].clk);
519                 mv_cesa_put_sram(pdev, i);
520         }
521
522         return ret;
523 }
524
525 static int mv_cesa_remove(struct platform_device *pdev)
526 {
527         struct mv_cesa_dev *cesa = platform_get_drvdata(pdev);
528         int i;
529
530         mv_cesa_remove_algs(cesa);
531
532         for (i = 0; i < cesa->caps->nengines; i++) {
533                 clk_disable_unprepare(cesa->engines[i].zclk);
534                 clk_disable_unprepare(cesa->engines[i].clk);
535                 mv_cesa_put_sram(pdev, i);
536         }
537
538         return 0;
539 }
540
541 static struct platform_driver marvell_cesa = {
542         .probe          = mv_cesa_probe,
543         .remove         = mv_cesa_remove,
544         .driver         = {
545                 .name   = "marvell-cesa",
546                 .of_match_table = mv_cesa_of_match_table,
547         },
548 };
549 module_platform_driver(marvell_cesa);
550
551 MODULE_ALIAS("platform:mv_crypto");
552 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
553 MODULE_AUTHOR("Arnaud Ebalard <arno@natisbad.org>");
554 MODULE_DESCRIPTION("Support for Marvell's cryptographic engine");
555 MODULE_LICENSE("GPL v2");