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/unaligned/access_ok.h>
25 #include <linux/crypto.h>
26 #include <linux/cryptohash.h>
27 #include <crypto/scatterwalk.h>
28 #include <crypto/algapi.h>
29 #include <crypto/hash.h>
30 #include <crypto/internal/hash.h>
32 #include <asm/blackfin.h>
33 #include <asm/bfin_crc.h>
35 #include <asm/portmux.h>
37 #define CRC_CCRYPTO_QUEUE_LENGTH 5
39 #define DRIVER_NAME "bfin-hmac-crc"
40 #define CHKSUM_DIGEST_SIZE 4
41 #define CHKSUM_BLOCK_SIZE 1
43 #define CRC_MAX_DMA_DESC 100
45 #define CRC_CRYPTO_STATE_UPDATE 1
46 #define CRC_CRYPTO_STATE_FINALUPDATE 2
47 #define CRC_CRYPTO_STATE_FINISH 3
49 struct bfin_crypto_crc {
50 struct list_head list;
57 volatile struct crc_register *regs;
59 struct ahash_request *req; /* current request in operation */
60 struct dma_desc_array *sg_cpu; /* virt addr of sg dma descriptors */
61 dma_addr_t sg_dma; /* phy addr of sg dma descriptors */
64 struct tasklet_struct done_task;
65 struct crypto_queue queue; /* waiting requests */
67 u8 busy:1; /* crc device in operation flag */
70 static struct bfin_crypto_crc_list {
71 struct list_head dev_list;
75 struct bfin_crypto_crc_reqctx {
76 struct bfin_crypto_crc *crc;
78 unsigned int total; /* total request bytes */
79 size_t sg_buflen; /* bytes for this update */
80 unsigned int sg_nents;
81 struct scatterlist *sg; /* sg list head for this update*/
82 struct scatterlist bufsl[2]; /* chained sg list */
86 u8 bufnext[CHKSUM_DIGEST_SIZE]; /* extra bytes for next udpate */
87 u8 buflast[CHKSUM_DIGEST_SIZE]; /* extra bytes from last udpate */
92 struct bfin_crypto_crc_ctx {
93 struct bfin_crypto_crc *crc;
99 * derive number of elements in scatterlist
101 static int sg_count(struct scatterlist *sg_list)
103 struct scatterlist *sg = sg_list;
109 while (!sg_is_last(sg)) {
111 sg = scatterwalk_sg_next(sg);
118 * get element in scatter list by given index
120 static struct scatterlist *sg_get(struct scatterlist *sg_list, unsigned int nents,
123 struct scatterlist *sg = NULL;
126 for_each_sg(sg_list, sg, nents, i)
133 static int bfin_crypto_crc_init_hw(struct bfin_crypto_crc *crc, u32 key)
135 crc->regs->datacntrld = 0;
136 crc->regs->control = MODE_CALC_CRC << OPMODE_OFFSET;
137 crc->regs->curresult = key;
139 /* setup CRC interrupts */
140 crc->regs->status = CMPERRI | DCNTEXPI;
141 crc->regs->intrenset = CMPERRI | DCNTEXPI;
147 static int bfin_crypto_crc_init(struct ahash_request *req)
149 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
150 struct bfin_crypto_crc_ctx *crc_ctx = crypto_ahash_ctx(tfm);
151 struct bfin_crypto_crc_reqctx *ctx = ahash_request_ctx(req);
152 struct bfin_crypto_crc *crc;
154 dev_dbg(ctx->crc->dev, "crc_init\n");
155 spin_lock_bh(&crc_list.lock);
156 list_for_each_entry(crc, &crc_list.dev_list, list) {
160 spin_unlock_bh(&crc_list.lock);
162 if (sg_count(req->src) > CRC_MAX_DMA_DESC) {
163 dev_dbg(ctx->crc->dev, "init: requested sg list is too big > %d\n",
169 ctx->bufnext_len = 0;
170 ctx->buflast_len = 0;
175 /* init crc results */
176 put_unaligned_le32(crc_ctx->key, req->result);
178 dev_dbg(ctx->crc->dev, "init: digest size: %d\n",
179 crypto_ahash_digestsize(tfm));
181 return bfin_crypto_crc_init_hw(crc, crc_ctx->key);
184 static void bfin_crypto_crc_config_dma(struct bfin_crypto_crc *crc)
186 struct scatterlist *sg;
187 struct bfin_crypto_crc_reqctx *ctx = ahash_request_ctx(crc->req);
189 unsigned long dma_config;
190 unsigned int dma_count;
191 unsigned int dma_addr;
192 unsigned int mid_dma_count = 0;
195 dma_map_sg(crc->dev, ctx->sg, ctx->sg_nents, DMA_TO_DEVICE);
197 for_each_sg(ctx->sg, sg, ctx->sg_nents, j) {
198 dma_config = DMAFLOW_ARRAY | RESTART | NDSIZE_3 | DMAEN | PSIZE_32;
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-1) << 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 /* chop current sg dma len to multiple of 32 bits */
218 mid_dma_count = dma_count % 4;
221 if (dma_addr % 4 == 0) {
222 dma_config |= WDSIZE_32;
225 } else if (dma_addr % 2 == 0) {
226 dma_config |= WDSIZE_16;
230 dma_config |= WDSIZE_8;
234 crc->sg_cpu[i].start_addr = dma_addr;
235 crc->sg_cpu[i].cfg = dma_config;
236 crc->sg_cpu[i].x_count = dma_count;
237 crc->sg_cpu[i].x_modify = dma_mod;
238 dev_dbg(crc->dev, "%d: crc_dma: start_addr:0x%lx, "
239 "cfg:0x%lx, x_count:0x%lx, x_modify:0x%lx\n",
240 i, crc->sg_cpu[i].start_addr,
241 crc->sg_cpu[i].cfg, crc->sg_cpu[i].x_count,
242 crc->sg_cpu[i].x_modify);
246 /* copy extra bytes to next middle dma buffer */
247 dma_config = DMAFLOW_ARRAY | RESTART | NDSIZE_3 |
248 DMAEN | PSIZE_32 | WDSIZE_32;
249 memcpy(crc->sg_mid_buf + (i << 2),
250 (void *)(dma_addr + (dma_count << 2)),
252 /* setup new dma descriptor for next middle dma */
253 crc->sg_cpu[i].start_addr = dma_map_single(crc->dev,
254 crc->sg_mid_buf + (i << 2),
255 CHKSUM_DIGEST_SIZE, DMA_TO_DEVICE);
256 crc->sg_cpu[i].cfg = dma_config;
257 crc->sg_cpu[i].x_count = 1;
258 crc->sg_cpu[i].x_modify = CHKSUM_DIGEST_SIZE;
259 dev_dbg(crc->dev, "%d: crc_dma: start_addr:0x%lx, "
260 "cfg:0x%lx, x_count:0x%lx, x_modify:0x%lx\n",
261 i, crc->sg_cpu[i].start_addr,
262 crc->sg_cpu[i].cfg, crc->sg_cpu[i].x_count,
263 crc->sg_cpu[i].x_modify);
268 dma_config = DMAFLOW_ARRAY | RESTART | NDSIZE_3 | DMAEN | PSIZE_32 | WDSIZE_32;
269 /* For final update req, append the buffer for next update as well*/
270 if (ctx->bufnext_len && (ctx->flag == CRC_CRYPTO_STATE_FINALUPDATE ||
271 ctx->flag == CRC_CRYPTO_STATE_FINISH)) {
272 crc->sg_cpu[i].start_addr = dma_map_single(crc->dev, ctx->bufnext,
273 CHKSUM_DIGEST_SIZE, DMA_TO_DEVICE);
274 crc->sg_cpu[i].cfg = dma_config;
275 crc->sg_cpu[i].x_count = 1;
276 crc->sg_cpu[i].x_modify = CHKSUM_DIGEST_SIZE;
277 dev_dbg(crc->dev, "%d: crc_dma: start_addr:0x%lx, "
278 "cfg:0x%lx, x_count:0x%lx, x_modify:0x%lx\n",
279 i, crc->sg_cpu[i].start_addr,
280 crc->sg_cpu[i].cfg, crc->sg_cpu[i].x_count,
281 crc->sg_cpu[i].x_modify);
288 flush_dcache_range((unsigned int)crc->sg_cpu,
289 (unsigned int)crc->sg_cpu +
290 i * sizeof(struct dma_desc_array));
292 /* Set the last descriptor to stop mode */
293 crc->sg_cpu[i - 1].cfg &= ~(DMAFLOW | NDSIZE);
294 crc->sg_cpu[i - 1].cfg |= DI_EN;
295 set_dma_curr_desc_addr(crc->dma_ch, (unsigned long *)crc->sg_dma);
296 set_dma_x_count(crc->dma_ch, 0);
297 set_dma_x_modify(crc->dma_ch, 0);
299 set_dma_config(crc->dma_ch, dma_config);
302 static int bfin_crypto_crc_handle_queue(struct bfin_crypto_crc *crc,
303 struct ahash_request *req)
305 struct crypto_async_request *async_req, *backlog;
306 struct bfin_crypto_crc_reqctx *ctx;
307 struct scatterlist *sg;
310 unsigned int nextlen;
313 spin_lock_irqsave(&crc->lock, flags);
315 ret = ahash_enqueue_request(&crc->queue, req);
317 spin_unlock_irqrestore(&crc->lock, flags);
320 backlog = crypto_get_backlog(&crc->queue);
321 async_req = crypto_dequeue_request(&crc->queue);
324 spin_unlock_irqrestore(&crc->lock, flags);
330 backlog->complete(backlog, -EINPROGRESS);
332 req = ahash_request_cast(async_req);
334 ctx = ahash_request_ctx(req);
339 dev_dbg(crc->dev, "handling new req, flag=%u, nbytes: %d\n",
340 ctx->flag, req->nbytes);
342 if (ctx->flag == CRC_CRYPTO_STATE_FINISH) {
343 if (ctx->bufnext_len == 0) {
348 /* Pack last crc update buffer to 32bit */
349 memset(ctx->bufnext + ctx->bufnext_len, 0,
350 CHKSUM_DIGEST_SIZE - ctx->bufnext_len);
352 /* Pack small data which is less than 32bit to buffer for next update. */
353 if (ctx->bufnext_len + req->nbytes < CHKSUM_DIGEST_SIZE) {
354 memcpy(ctx->bufnext + ctx->bufnext_len,
355 sg_virt(req->src), req->nbytes);
356 ctx->bufnext_len += req->nbytes;
357 if (ctx->flag == CRC_CRYPTO_STATE_FINALUPDATE &&
366 if (ctx->bufnext_len) {
367 /* Chain in extra bytes of last update */
368 ctx->buflast_len = ctx->bufnext_len;
369 memcpy(ctx->buflast, ctx->bufnext, ctx->buflast_len);
371 nsg = ctx->sg_buflen ? 2 : 1;
372 sg_init_table(ctx->bufsl, nsg);
373 sg_set_buf(ctx->bufsl, ctx->buflast, ctx->buflast_len);
375 scatterwalk_sg_chain(ctx->bufsl, nsg,
377 ctx->sg = ctx->bufsl;
381 /* Chop crc buffer size to multiple of 32 bit */
382 nsg = ctx->sg_nents = sg_count(ctx->sg);
383 ctx->sg_buflen = ctx->buflast_len + req->nbytes;
384 ctx->bufnext_len = ctx->sg_buflen % 4;
385 ctx->sg_buflen &= ~0x3;
387 if (ctx->bufnext_len) {
388 /* copy extra bytes to buffer for next update */
389 memset(ctx->bufnext, 0, CHKSUM_DIGEST_SIZE);
390 nextlen = ctx->bufnext_len;
391 for (i = nsg - 1; i >= 0; i--) {
392 sg = sg_get(ctx->sg, nsg, i);
393 j = min(nextlen, sg_dma_len(sg));
394 memcpy(ctx->bufnext + nextlen - j,
395 sg_virt(sg) + sg_dma_len(sg) - j, j);
396 if (j == sg_dma_len(sg))
406 if (ctx->bufnext_len && (ctx->flag == CRC_CRYPTO_STATE_FINALUPDATE ||
407 ctx->flag == CRC_CRYPTO_STATE_FINISH))
408 ctx->sg_buflen += CHKSUM_DIGEST_SIZE;
410 /* set CRC data count before start DMA */
411 crc->regs->datacnt = ctx->sg_buflen >> 2;
413 /* setup and enable CRC DMA */
414 bfin_crypto_crc_config_dma(crc);
416 /* finally kick off CRC operation */
417 crc->regs->control |= BLKEN;
423 static int bfin_crypto_crc_update(struct ahash_request *req)
425 struct bfin_crypto_crc_reqctx *ctx = ahash_request_ctx(req);
430 dev_dbg(ctx->crc->dev, "crc_update\n");
431 ctx->total += req->nbytes;
432 ctx->flag = CRC_CRYPTO_STATE_UPDATE;
434 return bfin_crypto_crc_handle_queue(ctx->crc, req);
437 static int bfin_crypto_crc_final(struct ahash_request *req)
439 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
440 struct bfin_crypto_crc_ctx *crc_ctx = crypto_ahash_ctx(tfm);
441 struct bfin_crypto_crc_reqctx *ctx = ahash_request_ctx(req);
443 dev_dbg(ctx->crc->dev, "crc_final\n");
444 ctx->flag = CRC_CRYPTO_STATE_FINISH;
447 return bfin_crypto_crc_handle_queue(ctx->crc, req);
450 static int bfin_crypto_crc_finup(struct ahash_request *req)
452 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
453 struct bfin_crypto_crc_ctx *crc_ctx = crypto_ahash_ctx(tfm);
454 struct bfin_crypto_crc_reqctx *ctx = ahash_request_ctx(req);
456 dev_dbg(ctx->crc->dev, "crc_finishupdate\n");
457 ctx->total += req->nbytes;
458 ctx->flag = CRC_CRYPTO_STATE_FINALUPDATE;
461 return bfin_crypto_crc_handle_queue(ctx->crc, req);
464 static int bfin_crypto_crc_digest(struct ahash_request *req)
468 ret = bfin_crypto_crc_init(req);
472 return bfin_crypto_crc_finup(req);
475 static int bfin_crypto_crc_setkey(struct crypto_ahash *tfm, const u8 *key,
478 struct bfin_crypto_crc_ctx *crc_ctx = crypto_ahash_ctx(tfm);
480 dev_dbg(crc_ctx->crc->dev, "crc_setkey\n");
481 if (keylen != CHKSUM_DIGEST_SIZE) {
482 crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
486 crc_ctx->key = get_unaligned_le32(key);
491 static int bfin_crypto_crc_cra_init(struct crypto_tfm *tfm)
493 struct bfin_crypto_crc_ctx *crc_ctx = crypto_tfm_ctx(tfm);
496 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
497 sizeof(struct bfin_crypto_crc_reqctx));
502 static void bfin_crypto_crc_cra_exit(struct crypto_tfm *tfm)
506 static struct ahash_alg algs = {
507 .init = bfin_crypto_crc_init,
508 .update = bfin_crypto_crc_update,
509 .final = bfin_crypto_crc_final,
510 .finup = bfin_crypto_crc_finup,
511 .digest = bfin_crypto_crc_digest,
512 .setkey = bfin_crypto_crc_setkey,
513 .halg.digestsize = CHKSUM_DIGEST_SIZE,
515 .cra_name = "hmac(crc32)",
516 .cra_driver_name = DRIVER_NAME,
518 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
520 .cra_blocksize = CHKSUM_BLOCK_SIZE,
521 .cra_ctxsize = sizeof(struct bfin_crypto_crc_ctx),
523 .cra_module = THIS_MODULE,
524 .cra_init = bfin_crypto_crc_cra_init,
525 .cra_exit = bfin_crypto_crc_cra_exit,
529 static void bfin_crypto_crc_done_task(unsigned long data)
531 struct bfin_crypto_crc *crc = (struct bfin_crypto_crc *)data;
533 bfin_crypto_crc_handle_queue(crc, NULL);
536 static irqreturn_t bfin_crypto_crc_handler(int irq, void *dev_id)
538 struct bfin_crypto_crc *crc = dev_id;
540 if (crc->regs->status & DCNTEXP) {
541 crc->regs->status = DCNTEXP;
544 /* prepare results */
545 put_unaligned_le32(crc->regs->result, crc->req->result);
547 crc->regs->control &= ~BLKEN;
550 if (crc->req->base.complete)
551 crc->req->base.complete(&crc->req->base, 0);
553 tasklet_schedule(&crc->done_task);
562 * bfin_crypto_crc_suspend - suspend crc device
563 * @pdev: device being suspended
564 * @state: requested suspend state
566 static int bfin_crypto_crc_suspend(struct platform_device *pdev, pm_message_t state)
568 struct bfin_crypto_crc *crc = platform_get_drvdata(pdev);
571 while ((crc->regs->control & BLKEN) && --i)
580 # define bfin_crypto_crc_suspend NULL
583 #define bfin_crypto_crc_resume NULL
586 * bfin_crypto_crc_probe - Initialize module
589 static int bfin_crypto_crc_probe(struct platform_device *pdev)
591 struct device *dev = &pdev->dev;
592 struct resource *res;
593 struct bfin_crypto_crc *crc;
594 unsigned int timeout = 100000;
597 crc = kzalloc(sizeof(*crc), GFP_KERNEL);
599 dev_err(&pdev->dev, "fail to malloc bfin_crypto_crc\n");
605 INIT_LIST_HEAD(&crc->list);
606 spin_lock_init(&crc->lock);
607 tasklet_init(&crc->done_task, bfin_crypto_crc_done_task, (unsigned long)crc);
608 crypto_init_queue(&crc->queue, CRC_CCRYPTO_QUEUE_LENGTH);
610 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
612 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
614 goto out_error_free_mem;
617 crc->regs = ioremap(res->start, resource_size(res));
619 dev_err(&pdev->dev, "Cannot map CRC IO\n");
621 goto out_error_free_mem;
624 crc->irq = platform_get_irq(pdev, 0);
626 dev_err(&pdev->dev, "No CRC DCNTEXP IRQ specified\n");
628 goto out_error_unmap;
631 ret = request_irq(crc->irq, bfin_crypto_crc_handler, IRQF_SHARED, dev_name(dev), crc);
633 dev_err(&pdev->dev, "Unable to request blackfin crc irq\n");
634 goto out_error_unmap;
637 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
639 dev_err(&pdev->dev, "No CRC DMA channel specified\n");
643 crc->dma_ch = res->start;
645 ret = request_dma(crc->dma_ch, dev_name(dev));
647 dev_err(&pdev->dev, "Unable to attach Blackfin CRC DMA channel\n");
651 crc->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE, &crc->sg_dma, GFP_KERNEL);
652 if (crc->sg_cpu == NULL) {
657 * need at most CRC_MAX_DMA_DESC sg + CRC_MAX_DMA_DESC middle +
658 * 1 last + 1 next dma descriptors
660 crc->sg_mid_buf = (u8 *)(crc->sg_cpu + ((CRC_MAX_DMA_DESC + 1) << 1));
662 crc->regs->control = 0;
664 crc->regs->poly = crc->poly = (u32)pdev->dev.platform_data;
667 while (!(crc->regs->status & LUTDONE) && (--timeout) > 0)
671 dev_info(&pdev->dev, "init crc poly timeout\n");
673 spin_lock(&crc_list.lock);
674 list_add(&crc->list, &crc_list.dev_list);
675 spin_unlock(&crc_list.lock);
677 platform_set_drvdata(pdev, crc);
679 ret = crypto_register_ahash(&algs);
681 spin_lock(&crc_list.lock);
682 list_del(&crc->list);
683 spin_unlock(&crc_list.lock);
684 dev_err(&pdev->dev, "Cann't register crypto ahash device\n");
688 dev_info(&pdev->dev, "initialized\n");
694 dma_free_coherent(&pdev->dev, PAGE_SIZE, crc->sg_cpu, crc->sg_dma);
695 free_dma(crc->dma_ch);
697 free_irq(crc->irq, crc);
699 iounmap((void *)crc->regs);
707 * bfin_crypto_crc_remove - Initialize module
710 static int bfin_crypto_crc_remove(struct platform_device *pdev)
712 struct bfin_crypto_crc *crc = platform_get_drvdata(pdev);
717 spin_lock(&crc_list.lock);
718 list_del(&crc->list);
719 spin_unlock(&crc_list.lock);
721 crypto_unregister_ahash(&algs);
722 tasklet_kill(&crc->done_task);
723 free_dma(crc->dma_ch);
725 free_irq(crc->irq, crc);
726 iounmap((void *)crc->regs);
732 static struct platform_driver bfin_crypto_crc_driver = {
733 .probe = bfin_crypto_crc_probe,
734 .remove = bfin_crypto_crc_remove,
735 .suspend = bfin_crypto_crc_suspend,
736 .resume = bfin_crypto_crc_resume,
739 .owner = THIS_MODULE,
744 * bfin_crypto_crc_mod_init - Initialize module
746 * Checks the module params and registers the platform driver.
747 * Real work is in the platform probe function.
749 static int __init bfin_crypto_crc_mod_init(void)
753 pr_info("Blackfin hardware CRC crypto driver\n");
755 INIT_LIST_HEAD(&crc_list.dev_list);
756 spin_lock_init(&crc_list.lock);
758 ret = platform_driver_register(&bfin_crypto_crc_driver);
760 pr_info(KERN_ERR "unable to register driver\n");
768 * bfin_crypto_crc_mod_exit - Deinitialize module
770 static void __exit bfin_crypto_crc_mod_exit(void)
772 platform_driver_unregister(&bfin_crypto_crc_driver);
775 module_init(bfin_crypto_crc_mod_init);
776 module_exit(bfin_crypto_crc_mod_exit);
778 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
779 MODULE_DESCRIPTION("Blackfin CRC hardware crypto driver");
780 MODULE_LICENSE("GPL");