4 * Support Blackfin CRC HW acceleration.
6 * Copyright 2012 Analog Devices Inc.
8 * Licensed under the GPL-2.
11 #include <linux/err.h>
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/errno.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/irq.h>
20 #include <linux/platform_device.h>
21 #include <linux/scatterlist.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/delay.h>
24 #include <linux/crypto.h>
25 #include <linux/cryptohash.h>
26 #include <crypto/scatterwalk.h>
27 #include <crypto/algapi.h>
28 #include <crypto/hash.h>
29 #include <crypto/internal/hash.h>
30 #include <asm/unaligned.h>
33 #include <asm/portmux.h>
38 #define CRC_CCRYPTO_QUEUE_LENGTH 5
40 #define DRIVER_NAME "bfin-hmac-crc"
41 #define CHKSUM_DIGEST_SIZE 4
42 #define CHKSUM_BLOCK_SIZE 1
44 #define CRC_MAX_DMA_DESC 100
46 #define CRC_CRYPTO_STATE_UPDATE 1
47 #define CRC_CRYPTO_STATE_FINALUPDATE 2
48 #define CRC_CRYPTO_STATE_FINISH 3
50 struct bfin_crypto_crc {
51 struct list_head list;
58 struct crc_register *regs;
60 struct ahash_request *req; /* current request in operation */
61 struct dma_desc_array *sg_cpu; /* virt addr of sg dma descriptors */
62 dma_addr_t sg_dma; /* phy addr of sg dma descriptors */
64 dma_addr_t sg_mid_dma; /* phy addr of sg mid buffer */
66 struct tasklet_struct done_task;
67 struct crypto_queue queue; /* waiting requests */
69 u8 busy:1; /* crc device in operation flag */
72 static struct bfin_crypto_crc_list {
73 struct list_head dev_list;
77 struct bfin_crypto_crc_reqctx {
78 struct bfin_crypto_crc *crc;
80 unsigned int total; /* total request bytes */
81 size_t sg_buflen; /* bytes for this update */
82 unsigned int sg_nents;
83 struct scatterlist *sg; /* sg list head for this update*/
84 struct scatterlist bufsl[2]; /* chained sg list */
88 u8 bufnext[CHKSUM_DIGEST_SIZE]; /* extra bytes for next udpate */
89 u8 buflast[CHKSUM_DIGEST_SIZE]; /* extra bytes from last udpate */
94 struct bfin_crypto_crc_ctx {
95 struct bfin_crypto_crc *crc;
101 * derive number of elements in scatterlist
103 static int sg_count(struct scatterlist *sg_list)
105 struct scatterlist *sg = sg_list;
111 while (!sg_is_last(sg)) {
120 * get element in scatter list by given index
122 static struct scatterlist *sg_get(struct scatterlist *sg_list, unsigned int nents,
125 struct scatterlist *sg = NULL;
128 for_each_sg(sg_list, sg, nents, i)
135 static int bfin_crypto_crc_init_hw(struct bfin_crypto_crc *crc, u32 key)
137 writel(0, &crc->regs->datacntrld);
138 writel(MODE_CALC_CRC << OPMODE_OFFSET, &crc->regs->control);
139 writel(key, &crc->regs->curresult);
141 /* setup CRC interrupts */
142 writel(CMPERRI | DCNTEXPI, &crc->regs->status);
143 writel(CMPERRI | DCNTEXPI, &crc->regs->intrenset);
148 static int bfin_crypto_crc_init(struct ahash_request *req)
150 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
151 struct bfin_crypto_crc_ctx *crc_ctx = crypto_ahash_ctx(tfm);
152 struct bfin_crypto_crc_reqctx *ctx = ahash_request_ctx(req);
153 struct bfin_crypto_crc *crc;
155 dev_dbg(ctx->crc->dev, "crc_init\n");
156 spin_lock_bh(&crc_list.lock);
157 list_for_each_entry(crc, &crc_list.dev_list, list) {
161 spin_unlock_bh(&crc_list.lock);
163 if (sg_count(req->src) > CRC_MAX_DMA_DESC) {
164 dev_dbg(ctx->crc->dev, "init: requested sg list is too big > %d\n",
170 ctx->bufnext_len = 0;
171 ctx->buflast_len = 0;
176 /* init crc results */
177 put_unaligned_le32(crc_ctx->key, req->result);
179 dev_dbg(ctx->crc->dev, "init: digest size: %d\n",
180 crypto_ahash_digestsize(tfm));
182 return bfin_crypto_crc_init_hw(crc, crc_ctx->key);
185 static void bfin_crypto_crc_config_dma(struct bfin_crypto_crc *crc)
187 struct scatterlist *sg;
188 struct bfin_crypto_crc_reqctx *ctx = ahash_request_ctx(crc->req);
190 unsigned long dma_config;
191 unsigned int dma_count;
192 unsigned int dma_addr;
193 unsigned int mid_dma_count = 0;
196 dma_map_sg(crc->dev, ctx->sg, ctx->sg_nents, DMA_TO_DEVICE);
198 for_each_sg(ctx->sg, sg, ctx->sg_nents, j) {
199 dma_addr = sg_dma_address(sg);
200 /* deduce extra bytes in last sg */
202 dma_count = sg_dma_len(sg) - ctx->bufnext_len;
204 dma_count = sg_dma_len(sg);
207 /* Append last middle dma buffer to 4 bytes with first
208 bytes in current sg buffer. Move addr of current
209 sg and deduce the length of current sg.
211 memcpy(crc->sg_mid_buf +(i << 2) + mid_dma_count,
213 CHKSUM_DIGEST_SIZE - mid_dma_count);
214 dma_addr += CHKSUM_DIGEST_SIZE - mid_dma_count;
215 dma_count -= CHKSUM_DIGEST_SIZE - mid_dma_count;
217 dma_config = DMAFLOW_ARRAY | RESTART | NDSIZE_3 |
218 DMAEN | PSIZE_32 | WDSIZE_32;
220 /* setup new dma descriptor for next middle dma */
221 crc->sg_cpu[i].start_addr = crc->sg_mid_dma + (i << 2);
222 crc->sg_cpu[i].cfg = dma_config;
223 crc->sg_cpu[i].x_count = 1;
224 crc->sg_cpu[i].x_modify = CHKSUM_DIGEST_SIZE;
225 dev_dbg(crc->dev, "%d: crc_dma: start_addr:0x%lx, "
226 "cfg:0x%lx, x_count:0x%lx, x_modify:0x%lx\n",
227 i, crc->sg_cpu[i].start_addr,
228 crc->sg_cpu[i].cfg, crc->sg_cpu[i].x_count,
229 crc->sg_cpu[i].x_modify);
233 dma_config = DMAFLOW_ARRAY | RESTART | NDSIZE_3 | DMAEN | PSIZE_32;
234 /* chop current sg dma len to multiple of 32 bits */
235 mid_dma_count = dma_count % 4;
238 if (dma_addr % 4 == 0) {
239 dma_config |= WDSIZE_32;
242 } else if (dma_addr % 2 == 0) {
243 dma_config |= WDSIZE_16;
247 dma_config |= WDSIZE_8;
251 crc->sg_cpu[i].start_addr = dma_addr;
252 crc->sg_cpu[i].cfg = dma_config;
253 crc->sg_cpu[i].x_count = dma_count;
254 crc->sg_cpu[i].x_modify = dma_mod;
255 dev_dbg(crc->dev, "%d: crc_dma: start_addr:0x%lx, "
256 "cfg:0x%lx, x_count:0x%lx, x_modify:0x%lx\n",
257 i, crc->sg_cpu[i].start_addr,
258 crc->sg_cpu[i].cfg, crc->sg_cpu[i].x_count,
259 crc->sg_cpu[i].x_modify);
263 /* copy extra bytes to next middle dma buffer */
264 memcpy(crc->sg_mid_buf + (i << 2),
265 (u8*)sg_virt(sg) + (dma_count << 2),
270 dma_config = DMAFLOW_ARRAY | RESTART | NDSIZE_3 | DMAEN | PSIZE_32 | WDSIZE_32;
271 /* For final update req, append the buffer for next update as well*/
272 if (ctx->bufnext_len && (ctx->flag == CRC_CRYPTO_STATE_FINALUPDATE ||
273 ctx->flag == CRC_CRYPTO_STATE_FINISH)) {
274 crc->sg_cpu[i].start_addr = dma_map_single(crc->dev, ctx->bufnext,
275 CHKSUM_DIGEST_SIZE, DMA_TO_DEVICE);
276 crc->sg_cpu[i].cfg = dma_config;
277 crc->sg_cpu[i].x_count = 1;
278 crc->sg_cpu[i].x_modify = CHKSUM_DIGEST_SIZE;
279 dev_dbg(crc->dev, "%d: crc_dma: start_addr:0x%lx, "
280 "cfg:0x%lx, x_count:0x%lx, x_modify:0x%lx\n",
281 i, crc->sg_cpu[i].start_addr,
282 crc->sg_cpu[i].cfg, crc->sg_cpu[i].x_count,
283 crc->sg_cpu[i].x_modify);
290 /* Set the last descriptor to stop mode */
291 crc->sg_cpu[i - 1].cfg &= ~(DMAFLOW | NDSIZE);
292 crc->sg_cpu[i - 1].cfg |= DI_EN;
293 set_dma_curr_desc_addr(crc->dma_ch, (unsigned long *)crc->sg_dma);
294 set_dma_x_count(crc->dma_ch, 0);
295 set_dma_x_modify(crc->dma_ch, 0);
296 set_dma_config(crc->dma_ch, dma_config);
299 static int bfin_crypto_crc_handle_queue(struct bfin_crypto_crc *crc,
300 struct ahash_request *req)
302 struct crypto_async_request *async_req, *backlog;
303 struct bfin_crypto_crc_reqctx *ctx;
304 struct scatterlist *sg;
307 unsigned int nextlen;
311 spin_lock_irqsave(&crc->lock, flags);
313 ret = ahash_enqueue_request(&crc->queue, req);
315 spin_unlock_irqrestore(&crc->lock, flags);
318 backlog = crypto_get_backlog(&crc->queue);
319 async_req = crypto_dequeue_request(&crc->queue);
322 spin_unlock_irqrestore(&crc->lock, flags);
328 backlog->complete(backlog, -EINPROGRESS);
330 req = ahash_request_cast(async_req);
332 ctx = ahash_request_ctx(req);
337 dev_dbg(crc->dev, "handling new req, flag=%u, nbytes: %d\n",
338 ctx->flag, req->nbytes);
340 if (ctx->flag == CRC_CRYPTO_STATE_FINISH) {
341 if (ctx->bufnext_len == 0) {
346 /* Pack last crc update buffer to 32bit */
347 memset(ctx->bufnext + ctx->bufnext_len, 0,
348 CHKSUM_DIGEST_SIZE - ctx->bufnext_len);
350 /* Pack small data which is less than 32bit to buffer for next update. */
351 if (ctx->bufnext_len + req->nbytes < CHKSUM_DIGEST_SIZE) {
352 memcpy(ctx->bufnext + ctx->bufnext_len,
353 sg_virt(req->src), req->nbytes);
354 ctx->bufnext_len += req->nbytes;
355 if (ctx->flag == CRC_CRYPTO_STATE_FINALUPDATE &&
364 if (ctx->bufnext_len) {
365 /* Chain in extra bytes of last update */
366 ctx->buflast_len = ctx->bufnext_len;
367 memcpy(ctx->buflast, ctx->bufnext, ctx->buflast_len);
369 nsg = ctx->sg_buflen ? 2 : 1;
370 sg_init_table(ctx->bufsl, nsg);
371 sg_set_buf(ctx->bufsl, ctx->buflast, ctx->buflast_len);
373 sg_chain(ctx->bufsl, nsg, req->src);
374 ctx->sg = ctx->bufsl;
378 /* Chop crc buffer size to multiple of 32 bit */
379 nsg = ctx->sg_nents = sg_count(ctx->sg);
380 ctx->sg_buflen = ctx->buflast_len + req->nbytes;
381 ctx->bufnext_len = ctx->sg_buflen % 4;
382 ctx->sg_buflen &= ~0x3;
384 if (ctx->bufnext_len) {
385 /* copy extra bytes to buffer for next update */
386 memset(ctx->bufnext, 0, CHKSUM_DIGEST_SIZE);
387 nextlen = ctx->bufnext_len;
388 for (i = nsg - 1; i >= 0; i--) {
389 sg = sg_get(ctx->sg, nsg, i);
390 j = min(nextlen, sg_dma_len(sg));
391 memcpy(ctx->bufnext + nextlen - j,
392 sg_virt(sg) + sg_dma_len(sg) - j, j);
393 if (j == sg_dma_len(sg))
403 if (ctx->bufnext_len && (ctx->flag == CRC_CRYPTO_STATE_FINALUPDATE ||
404 ctx->flag == CRC_CRYPTO_STATE_FINISH))
405 ctx->sg_buflen += CHKSUM_DIGEST_SIZE;
407 /* set CRC data count before start DMA */
408 writel(ctx->sg_buflen >> 2, &crc->regs->datacnt);
410 /* setup and enable CRC DMA */
411 bfin_crypto_crc_config_dma(crc);
413 /* finally kick off CRC operation */
414 reg = readl(&crc->regs->control);
415 writel(reg | BLKEN, &crc->regs->control);
420 static int bfin_crypto_crc_update(struct ahash_request *req)
422 struct bfin_crypto_crc_reqctx *ctx = ahash_request_ctx(req);
427 dev_dbg(ctx->crc->dev, "crc_update\n");
428 ctx->total += req->nbytes;
429 ctx->flag = CRC_CRYPTO_STATE_UPDATE;
431 return bfin_crypto_crc_handle_queue(ctx->crc, req);
434 static int bfin_crypto_crc_final(struct ahash_request *req)
436 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
437 struct bfin_crypto_crc_ctx *crc_ctx = crypto_ahash_ctx(tfm);
438 struct bfin_crypto_crc_reqctx *ctx = ahash_request_ctx(req);
440 dev_dbg(ctx->crc->dev, "crc_final\n");
441 ctx->flag = CRC_CRYPTO_STATE_FINISH;
444 return bfin_crypto_crc_handle_queue(ctx->crc, req);
447 static int bfin_crypto_crc_finup(struct ahash_request *req)
449 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
450 struct bfin_crypto_crc_ctx *crc_ctx = crypto_ahash_ctx(tfm);
451 struct bfin_crypto_crc_reqctx *ctx = ahash_request_ctx(req);
453 dev_dbg(ctx->crc->dev, "crc_finishupdate\n");
454 ctx->total += req->nbytes;
455 ctx->flag = CRC_CRYPTO_STATE_FINALUPDATE;
458 return bfin_crypto_crc_handle_queue(ctx->crc, req);
461 static int bfin_crypto_crc_digest(struct ahash_request *req)
465 ret = bfin_crypto_crc_init(req);
469 return bfin_crypto_crc_finup(req);
472 static int bfin_crypto_crc_setkey(struct crypto_ahash *tfm, const u8 *key,
475 struct bfin_crypto_crc_ctx *crc_ctx = crypto_ahash_ctx(tfm);
477 dev_dbg(crc_ctx->crc->dev, "crc_setkey\n");
478 if (keylen != CHKSUM_DIGEST_SIZE) {
479 crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
483 crc_ctx->key = get_unaligned_le32(key);
488 static int bfin_crypto_crc_cra_init(struct crypto_tfm *tfm)
490 struct bfin_crypto_crc_ctx *crc_ctx = crypto_tfm_ctx(tfm);
493 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
494 sizeof(struct bfin_crypto_crc_reqctx));
499 static void bfin_crypto_crc_cra_exit(struct crypto_tfm *tfm)
503 static struct ahash_alg algs = {
504 .init = bfin_crypto_crc_init,
505 .update = bfin_crypto_crc_update,
506 .final = bfin_crypto_crc_final,
507 .finup = bfin_crypto_crc_finup,
508 .digest = bfin_crypto_crc_digest,
509 .setkey = bfin_crypto_crc_setkey,
510 .halg.digestsize = CHKSUM_DIGEST_SIZE,
512 .cra_name = "hmac(crc32)",
513 .cra_driver_name = DRIVER_NAME,
515 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
517 .cra_blocksize = CHKSUM_BLOCK_SIZE,
518 .cra_ctxsize = sizeof(struct bfin_crypto_crc_ctx),
520 .cra_module = THIS_MODULE,
521 .cra_init = bfin_crypto_crc_cra_init,
522 .cra_exit = bfin_crypto_crc_cra_exit,
526 static void bfin_crypto_crc_done_task(unsigned long data)
528 struct bfin_crypto_crc *crc = (struct bfin_crypto_crc *)data;
530 bfin_crypto_crc_handle_queue(crc, NULL);
533 static irqreturn_t bfin_crypto_crc_handler(int irq, void *dev_id)
535 struct bfin_crypto_crc *crc = dev_id;
538 if (readl(&crc->regs->status) & DCNTEXP) {
539 writel(DCNTEXP, &crc->regs->status);
541 /* prepare results */
542 put_unaligned_le32(readl(&crc->regs->result),
545 reg = readl(&crc->regs->control);
546 writel(reg & ~BLKEN, &crc->regs->control);
549 if (crc->req->base.complete)
550 crc->req->base.complete(&crc->req->base, 0);
552 tasklet_schedule(&crc->done_task);
561 * bfin_crypto_crc_suspend - suspend crc device
562 * @pdev: device being suspended
563 * @state: requested suspend state
565 static int bfin_crypto_crc_suspend(struct platform_device *pdev, pm_message_t state)
567 struct bfin_crypto_crc *crc = platform_get_drvdata(pdev);
570 while ((readl(&crc->regs->control) & BLKEN) && --i)
579 # define bfin_crypto_crc_suspend NULL
582 #define bfin_crypto_crc_resume NULL
585 * bfin_crypto_crc_probe - Initialize module
588 static int bfin_crypto_crc_probe(struct platform_device *pdev)
590 struct device *dev = &pdev->dev;
591 struct resource *res;
592 struct bfin_crypto_crc *crc;
593 unsigned int timeout = 100000;
596 crc = devm_kzalloc(dev, sizeof(*crc), GFP_KERNEL);
598 dev_err(&pdev->dev, "fail to malloc bfin_crypto_crc\n");
604 INIT_LIST_HEAD(&crc->list);
605 spin_lock_init(&crc->lock);
606 tasklet_init(&crc->done_task, bfin_crypto_crc_done_task, (unsigned long)crc);
607 crypto_init_queue(&crc->queue, CRC_CCRYPTO_QUEUE_LENGTH);
609 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
611 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
615 crc->regs = devm_ioremap_resource(dev, res);
616 if (IS_ERR((void *)crc->regs)) {
617 dev_err(&pdev->dev, "Cannot map CRC IO\n");
618 return PTR_ERR((void *)crc->regs);
621 crc->irq = platform_get_irq(pdev, 0);
623 dev_err(&pdev->dev, "No CRC DCNTEXP IRQ specified\n");
627 ret = devm_request_irq(dev, crc->irq, bfin_crypto_crc_handler,
628 IRQF_SHARED, dev_name(dev), crc);
630 dev_err(&pdev->dev, "Unable to request blackfin crc irq\n");
634 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
636 dev_err(&pdev->dev, "No CRC DMA channel specified\n");
639 crc->dma_ch = res->start;
641 ret = request_dma(crc->dma_ch, dev_name(dev));
643 dev_err(&pdev->dev, "Unable to attach Blackfin CRC DMA channel\n");
647 crc->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE, &crc->sg_dma, GFP_KERNEL);
648 if (crc->sg_cpu == NULL) {
653 * need at most CRC_MAX_DMA_DESC sg + CRC_MAX_DMA_DESC middle +
654 * 1 last + 1 next dma descriptors
656 crc->sg_mid_buf = (u8 *)(crc->sg_cpu + ((CRC_MAX_DMA_DESC + 1) << 1));
657 crc->sg_mid_dma = crc->sg_dma + sizeof(struct dma_desc_array)
658 * ((CRC_MAX_DMA_DESC + 1) << 1);
660 writel(0, &crc->regs->control);
661 crc->poly = (u32)pdev->dev.platform_data;
662 writel(crc->poly, &crc->regs->poly);
664 while (!(readl(&crc->regs->status) & LUTDONE) && (--timeout) > 0)
668 dev_info(&pdev->dev, "init crc poly timeout\n");
670 platform_set_drvdata(pdev, crc);
672 spin_lock(&crc_list.lock);
673 list_add(&crc->list, &crc_list.dev_list);
674 spin_unlock(&crc_list.lock);
676 if (list_is_singular(&crc_list.dev_list)) {
677 ret = crypto_register_ahash(&algs);
680 "Can't register crypto ahash device\n");
685 dev_info(&pdev->dev, "initialized\n");
691 dma_free_coherent(&pdev->dev, PAGE_SIZE, crc->sg_cpu, crc->sg_dma);
692 free_dma(crc->dma_ch);
698 * bfin_crypto_crc_remove - Initialize module
701 static int bfin_crypto_crc_remove(struct platform_device *pdev)
703 struct bfin_crypto_crc *crc = platform_get_drvdata(pdev);
708 spin_lock(&crc_list.lock);
709 list_del(&crc->list);
710 spin_unlock(&crc_list.lock);
712 crypto_unregister_ahash(&algs);
713 tasklet_kill(&crc->done_task);
714 free_dma(crc->dma_ch);
719 static struct platform_driver bfin_crypto_crc_driver = {
720 .probe = bfin_crypto_crc_probe,
721 .remove = bfin_crypto_crc_remove,
722 .suspend = bfin_crypto_crc_suspend,
723 .resume = bfin_crypto_crc_resume,
730 * bfin_crypto_crc_mod_init - Initialize module
732 * Checks the module params and registers the platform driver.
733 * Real work is in the platform probe function.
735 static int __init bfin_crypto_crc_mod_init(void)
739 pr_info("Blackfin hardware CRC crypto driver\n");
741 INIT_LIST_HEAD(&crc_list.dev_list);
742 spin_lock_init(&crc_list.lock);
744 ret = platform_driver_register(&bfin_crypto_crc_driver);
746 pr_err("unable to register driver\n");
754 * bfin_crypto_crc_mod_exit - Deinitialize module
756 static void __exit bfin_crypto_crc_mod_exit(void)
758 platform_driver_unregister(&bfin_crypto_crc_driver);
761 module_init(bfin_crypto_crc_mod_init);
762 module_exit(bfin_crypto_crc_mod_exit);
764 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
765 MODULE_DESCRIPTION("Blackfin CRC hardware crypto driver");
766 MODULE_LICENSE("GPL");