4 * Support for OMAP SHA1/MD5 HW acceleration.
6 * Copyright (c) 2010 Nokia Corporation
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as published
11 * by the Free Software Foundation.
13 * Some ideas are from old omap-sha1-md5.c driver.
16 #define pr_fmt(fmt) "%s: " fmt, __func__
18 #include <linux/err.h>
19 #include <linux/device.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/errno.h>
23 #include <linux/interrupt.h>
24 #include <linux/kernel.h>
25 #include <linux/clk.h>
26 #include <linux/irq.h>
28 #include <linux/platform_device.h>
29 #include <linux/scatterlist.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/delay.h>
32 #include <linux/crypto.h>
33 #include <linux/cryptohash.h>
34 #include <crypto/scatterwalk.h>
35 #include <crypto/algapi.h>
36 #include <crypto/sha.h>
37 #include <crypto/hash.h>
38 #include <crypto/internal/hash.h>
40 #include <linux/omap-dma.h>
42 #ifdef CONFIG_ARCH_OMAP1
43 #include <mach/irqs.h>
46 #define SHA_REG_DIGEST(x) (0x00 + ((x) * 0x04))
47 #define SHA_REG_DIN(x) (0x1C + ((x) * 0x04))
49 #define SHA1_MD5_BLOCK_SIZE SHA1_BLOCK_SIZE
50 #define MD5_DIGEST_SIZE 16
52 #define SHA_REG_DIGCNT 0x14
54 #define SHA_REG_CTRL 0x18
55 #define SHA_REG_CTRL_LENGTH (0xFFFFFFFF << 5)
56 #define SHA_REG_CTRL_CLOSE_HASH (1 << 4)
57 #define SHA_REG_CTRL_ALGO_CONST (1 << 3)
58 #define SHA_REG_CTRL_ALGO (1 << 2)
59 #define SHA_REG_CTRL_INPUT_READY (1 << 1)
60 #define SHA_REG_CTRL_OUTPUT_READY (1 << 0)
62 #define SHA_REG_REV 0x5C
63 #define SHA_REG_REV_MAJOR 0xF0
64 #define SHA_REG_REV_MINOR 0x0F
66 #define SHA_REG_MASK 0x60
67 #define SHA_REG_MASK_DMA_EN (1 << 3)
68 #define SHA_REG_MASK_IT_EN (1 << 2)
69 #define SHA_REG_MASK_SOFTRESET (1 << 1)
70 #define SHA_REG_AUTOIDLE (1 << 0)
72 #define SHA_REG_SYSSTATUS 0x64
73 #define SHA_REG_SYSSTATUS_RESETDONE (1 << 0)
75 #define DEFAULT_TIMEOUT_INTERVAL HZ
77 /* mostly device flags */
80 #define FLAGS_DMA_ACTIVE 2
81 #define FLAGS_OUTPUT_READY 3
84 #define FLAGS_DMA_READY 6
86 #define FLAGS_FINUP 16
90 #define FLAGS_ERROR 20
95 #define OMAP_ALIGN_MASK (sizeof(u32)-1)
96 #define OMAP_ALIGNED __attribute__((aligned(sizeof(u32))))
98 #define BUFLEN PAGE_SIZE
100 struct omap_sham_dev;
102 struct omap_sham_reqctx {
103 struct omap_sham_dev *dd;
107 u8 digest[SHA1_DIGEST_SIZE] OMAP_ALIGNED;
114 struct scatterlist *sg;
115 unsigned int offset; /* offset in current sg */
116 unsigned int total; /* total request */
118 u8 buffer[0] OMAP_ALIGNED;
121 struct omap_sham_hmac_ctx {
122 struct crypto_shash *shash;
123 u8 ipad[SHA1_MD5_BLOCK_SIZE];
124 u8 opad[SHA1_MD5_BLOCK_SIZE];
127 struct omap_sham_ctx {
128 struct omap_sham_dev *dd;
133 struct crypto_shash *fallback;
135 struct omap_sham_hmac_ctx base[0];
138 #define OMAP_SHAM_QUEUE_LENGTH 1
140 struct omap_sham_dev {
141 struct list_head list;
142 unsigned long phys_base;
144 void __iomem *io_base;
151 struct tasklet_struct done_task;
154 struct crypto_queue queue;
155 struct ahash_request *req;
158 struct omap_sham_drv {
159 struct list_head dev_list;
164 static struct omap_sham_drv sham = {
165 .dev_list = LIST_HEAD_INIT(sham.dev_list),
166 .lock = __SPIN_LOCK_UNLOCKED(sham.lock),
169 static inline u32 omap_sham_read(struct omap_sham_dev *dd, u32 offset)
171 return __raw_readl(dd->io_base + offset);
174 static inline void omap_sham_write(struct omap_sham_dev *dd,
175 u32 offset, u32 value)
177 __raw_writel(value, dd->io_base + offset);
180 static inline void omap_sham_write_mask(struct omap_sham_dev *dd, u32 address,
185 val = omap_sham_read(dd, address);
188 omap_sham_write(dd, address, val);
191 static inline int omap_sham_wait(struct omap_sham_dev *dd, u32 offset, u32 bit)
193 unsigned long timeout = jiffies + DEFAULT_TIMEOUT_INTERVAL;
195 while (!(omap_sham_read(dd, offset) & bit)) {
196 if (time_is_before_jiffies(timeout))
203 static void omap_sham_copy_hash(struct ahash_request *req, int out)
205 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
206 u32 *hash = (u32 *)ctx->digest;
209 /* MD5 is almost unused. So copy sha1 size to reduce code */
210 for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(u32); i++) {
212 hash[i] = omap_sham_read(ctx->dd,
215 omap_sham_write(ctx->dd,
216 SHA_REG_DIGEST(i), hash[i]);
220 static void omap_sham_copy_ready_hash(struct ahash_request *req)
222 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
223 u32 *in = (u32 *)ctx->digest;
224 u32 *hash = (u32 *)req->result;
230 if (likely(ctx->flags & BIT(FLAGS_SHA1))) {
231 /* SHA1 results are in big endian */
232 for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(u32); i++)
233 hash[i] = be32_to_cpu(in[i]);
235 /* MD5 results are in little endian */
236 for (i = 0; i < MD5_DIGEST_SIZE / sizeof(u32); i++)
237 hash[i] = le32_to_cpu(in[i]);
241 static int omap_sham_hw_init(struct omap_sham_dev *dd)
243 clk_enable(dd->iclk);
245 if (!test_bit(FLAGS_INIT, &dd->flags)) {
246 omap_sham_write_mask(dd, SHA_REG_MASK,
247 SHA_REG_MASK_SOFTRESET, SHA_REG_MASK_SOFTRESET);
249 if (omap_sham_wait(dd, SHA_REG_SYSSTATUS,
250 SHA_REG_SYSSTATUS_RESETDONE))
253 set_bit(FLAGS_INIT, &dd->flags);
260 static void omap_sham_write_ctrl(struct omap_sham_dev *dd, size_t length,
263 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
264 u32 val = length << 5, mask;
266 if (likely(ctx->digcnt))
267 omap_sham_write(dd, SHA_REG_DIGCNT, ctx->digcnt);
269 omap_sham_write_mask(dd, SHA_REG_MASK,
270 SHA_REG_MASK_IT_EN | (dma ? SHA_REG_MASK_DMA_EN : 0),
271 SHA_REG_MASK_IT_EN | SHA_REG_MASK_DMA_EN);
273 * Setting ALGO_CONST only for the first iteration
274 * and CLOSE_HASH only for the last one.
276 if (ctx->flags & BIT(FLAGS_SHA1))
277 val |= SHA_REG_CTRL_ALGO;
279 val |= SHA_REG_CTRL_ALGO_CONST;
281 val |= SHA_REG_CTRL_CLOSE_HASH;
283 mask = SHA_REG_CTRL_ALGO_CONST | SHA_REG_CTRL_CLOSE_HASH |
284 SHA_REG_CTRL_ALGO | SHA_REG_CTRL_LENGTH;
286 omap_sham_write_mask(dd, SHA_REG_CTRL, val, mask);
289 static int omap_sham_xmit_cpu(struct omap_sham_dev *dd, const u8 *buf,
290 size_t length, int final)
292 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
294 const u32 *buffer = (const u32 *)buf;
296 dev_dbg(dd->dev, "xmit_cpu: digcnt: %d, length: %d, final: %d\n",
297 ctx->digcnt, length, final);
299 omap_sham_write_ctrl(dd, length, final, 0);
301 /* should be non-zero before next lines to disable clocks later */
302 ctx->digcnt += length;
304 if (omap_sham_wait(dd, SHA_REG_CTRL, SHA_REG_CTRL_INPUT_READY))
308 set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */
310 set_bit(FLAGS_CPU, &dd->flags);
312 len32 = DIV_ROUND_UP(length, sizeof(u32));
314 for (count = 0; count < len32; count++)
315 omap_sham_write(dd, SHA_REG_DIN(count), buffer[count]);
320 static int omap_sham_xmit_dma(struct omap_sham_dev *dd, dma_addr_t dma_addr,
321 size_t length, int final)
323 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
326 dev_dbg(dd->dev, "xmit_dma: digcnt: %d, length: %d, final: %d\n",
327 ctx->digcnt, length, final);
329 len32 = DIV_ROUND_UP(length, sizeof(u32));
331 omap_set_dma_transfer_params(dd->dma_lch, OMAP_DMA_DATA_TYPE_S32, len32,
332 1, OMAP_DMA_SYNC_PACKET, dd->dma,
333 OMAP_DMA_DST_SYNC_PREFETCH);
335 omap_set_dma_src_params(dd->dma_lch, 0, OMAP_DMA_AMODE_POST_INC,
338 omap_sham_write_ctrl(dd, length, final, 1);
340 ctx->digcnt += length;
343 set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */
345 set_bit(FLAGS_DMA_ACTIVE, &dd->flags);
347 omap_start_dma(dd->dma_lch);
352 static size_t omap_sham_append_buffer(struct omap_sham_reqctx *ctx,
353 const u8 *data, size_t length)
355 size_t count = min(length, ctx->buflen - ctx->bufcnt);
357 count = min(count, ctx->total);
360 memcpy(ctx->buffer + ctx->bufcnt, data, count);
361 ctx->bufcnt += count;
366 static size_t omap_sham_append_sg(struct omap_sham_reqctx *ctx)
371 count = omap_sham_append_buffer(ctx,
372 sg_virt(ctx->sg) + ctx->offset,
373 ctx->sg->length - ctx->offset);
376 ctx->offset += count;
378 if (ctx->offset == ctx->sg->length) {
379 ctx->sg = sg_next(ctx->sg);
390 static int omap_sham_xmit_dma_map(struct omap_sham_dev *dd,
391 struct omap_sham_reqctx *ctx,
392 size_t length, int final)
394 ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer, ctx->buflen,
396 if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
397 dev_err(dd->dev, "dma %u bytes error\n", ctx->buflen);
401 ctx->flags &= ~BIT(FLAGS_SG);
403 /* next call does not fail... so no unmap in the case of error */
404 return omap_sham_xmit_dma(dd, ctx->dma_addr, length, final);
407 static int omap_sham_update_dma_slow(struct omap_sham_dev *dd)
409 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
413 omap_sham_append_sg(ctx);
415 final = (ctx->flags & BIT(FLAGS_FINUP)) && !ctx->total;
417 dev_dbg(dd->dev, "slow: bufcnt: %u, digcnt: %d, final: %d\n",
418 ctx->bufcnt, ctx->digcnt, final);
420 if (final || (ctx->bufcnt == ctx->buflen && ctx->total)) {
423 return omap_sham_xmit_dma_map(dd, ctx, count, final);
429 /* Start address alignment */
430 #define SG_AA(sg) (IS_ALIGNED(sg->offset, sizeof(u32)))
431 /* SHA1 block size alignment */
432 #define SG_SA(sg) (IS_ALIGNED(sg->length, SHA1_MD5_BLOCK_SIZE))
434 static int omap_sham_update_dma_start(struct omap_sham_dev *dd)
436 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
437 unsigned int length, final, tail;
438 struct scatterlist *sg;
443 if (ctx->bufcnt || ctx->offset)
444 return omap_sham_update_dma_slow(dd);
446 dev_dbg(dd->dev, "fast: digcnt: %d, bufcnt: %u, total: %u\n",
447 ctx->digcnt, ctx->bufcnt, ctx->total);
452 return omap_sham_update_dma_slow(dd);
454 if (!sg_is_last(sg) && !SG_SA(sg))
455 /* size is not SHA1_BLOCK_SIZE aligned */
456 return omap_sham_update_dma_slow(dd);
458 length = min(ctx->total, sg->length);
460 if (sg_is_last(sg)) {
461 if (!(ctx->flags & BIT(FLAGS_FINUP))) {
462 /* not last sg must be SHA1_MD5_BLOCK_SIZE aligned */
463 tail = length & (SHA1_MD5_BLOCK_SIZE - 1);
464 /* without finup() we need one block to close hash */
466 tail = SHA1_MD5_BLOCK_SIZE;
471 if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
472 dev_err(dd->dev, "dma_map_sg error\n");
476 ctx->flags |= BIT(FLAGS_SG);
478 ctx->total -= length;
479 ctx->offset = length; /* offset where to start slow */
481 final = (ctx->flags & BIT(FLAGS_FINUP)) && !ctx->total;
483 /* next call does not fail... so no unmap in the case of error */
484 return omap_sham_xmit_dma(dd, sg_dma_address(ctx->sg), length, final);
487 static int omap_sham_update_cpu(struct omap_sham_dev *dd)
489 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
492 omap_sham_append_sg(ctx);
493 bufcnt = ctx->bufcnt;
496 return omap_sham_xmit_cpu(dd, ctx->buffer, bufcnt, 1);
499 static int omap_sham_update_dma_stop(struct omap_sham_dev *dd)
501 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
503 omap_stop_dma(dd->dma_lch);
504 if (ctx->flags & BIT(FLAGS_SG)) {
505 dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE);
506 if (ctx->sg->length == ctx->offset) {
507 ctx->sg = sg_next(ctx->sg);
512 dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen,
519 static int omap_sham_init(struct ahash_request *req)
521 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
522 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
523 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
524 struct omap_sham_dev *dd = NULL, *tmp;
526 spin_lock_bh(&sham.lock);
528 list_for_each_entry(tmp, &sham.dev_list, list) {
536 spin_unlock_bh(&sham.lock);
542 dev_dbg(dd->dev, "init: digest size: %d\n",
543 crypto_ahash_digestsize(tfm));
545 if (crypto_ahash_digestsize(tfm) == SHA1_DIGEST_SIZE)
546 ctx->flags |= BIT(FLAGS_SHA1);
550 ctx->buflen = BUFLEN;
552 if (tctx->flags & BIT(FLAGS_HMAC)) {
553 struct omap_sham_hmac_ctx *bctx = tctx->base;
555 memcpy(ctx->buffer, bctx->ipad, SHA1_MD5_BLOCK_SIZE);
556 ctx->bufcnt = SHA1_MD5_BLOCK_SIZE;
557 ctx->flags |= BIT(FLAGS_HMAC);
564 static int omap_sham_update_req(struct omap_sham_dev *dd)
566 struct ahash_request *req = dd->req;
567 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
570 dev_dbg(dd->dev, "update_req: total: %u, digcnt: %d, finup: %d\n",
571 ctx->total, ctx->digcnt, (ctx->flags & BIT(FLAGS_FINUP)) != 0);
573 if (ctx->flags & BIT(FLAGS_CPU))
574 err = omap_sham_update_cpu(dd);
576 err = omap_sham_update_dma_start(dd);
578 /* wait for dma completion before can take more data */
579 dev_dbg(dd->dev, "update: err: %d, digcnt: %d\n", err, ctx->digcnt);
584 static int omap_sham_final_req(struct omap_sham_dev *dd)
586 struct ahash_request *req = dd->req;
587 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
588 int err = 0, use_dma = 1;
590 if (ctx->bufcnt <= 64)
591 /* faster to handle last block with cpu */
595 err = omap_sham_xmit_dma_map(dd, ctx, ctx->bufcnt, 1);
597 err = omap_sham_xmit_cpu(dd, ctx->buffer, ctx->bufcnt, 1);
601 dev_dbg(dd->dev, "final_req: err: %d\n", err);
606 static int omap_sham_finish_hmac(struct ahash_request *req)
608 struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
609 struct omap_sham_hmac_ctx *bctx = tctx->base;
610 int bs = crypto_shash_blocksize(bctx->shash);
611 int ds = crypto_shash_digestsize(bctx->shash);
613 struct shash_desc shash;
614 char ctx[crypto_shash_descsize(bctx->shash)];
617 desc.shash.tfm = bctx->shash;
618 desc.shash.flags = 0; /* not CRYPTO_TFM_REQ_MAY_SLEEP */
620 return crypto_shash_init(&desc.shash) ?:
621 crypto_shash_update(&desc.shash, bctx->opad, bs) ?:
622 crypto_shash_finup(&desc.shash, req->result, ds, req->result);
625 static int omap_sham_finish(struct ahash_request *req)
627 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
628 struct omap_sham_dev *dd = ctx->dd;
632 omap_sham_copy_ready_hash(req);
633 if (ctx->flags & BIT(FLAGS_HMAC))
634 err = omap_sham_finish_hmac(req);
637 dev_dbg(dd->dev, "digcnt: %d, bufcnt: %d\n", ctx->digcnt, ctx->bufcnt);
642 static void omap_sham_finish_req(struct ahash_request *req, int err)
644 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
645 struct omap_sham_dev *dd = ctx->dd;
648 omap_sham_copy_hash(req, 1);
649 if (test_bit(FLAGS_FINAL, &dd->flags))
650 err = omap_sham_finish(req);
652 ctx->flags |= BIT(FLAGS_ERROR);
655 /* atomic operation is not needed here */
656 dd->flags &= ~(BIT(FLAGS_BUSY) | BIT(FLAGS_FINAL) | BIT(FLAGS_CPU) |
657 BIT(FLAGS_DMA_READY) | BIT(FLAGS_OUTPUT_READY));
658 clk_disable(dd->iclk);
660 if (req->base.complete)
661 req->base.complete(&req->base, err);
663 /* handle new request */
664 tasklet_schedule(&dd->done_task);
667 static int omap_sham_handle_queue(struct omap_sham_dev *dd,
668 struct ahash_request *req)
670 struct crypto_async_request *async_req, *backlog;
671 struct omap_sham_reqctx *ctx;
673 int err = 0, ret = 0;
675 spin_lock_irqsave(&dd->lock, flags);
677 ret = ahash_enqueue_request(&dd->queue, req);
678 if (test_bit(FLAGS_BUSY, &dd->flags)) {
679 spin_unlock_irqrestore(&dd->lock, flags);
682 backlog = crypto_get_backlog(&dd->queue);
683 async_req = crypto_dequeue_request(&dd->queue);
685 set_bit(FLAGS_BUSY, &dd->flags);
686 spin_unlock_irqrestore(&dd->lock, flags);
692 backlog->complete(backlog, -EINPROGRESS);
694 req = ahash_request_cast(async_req);
696 ctx = ahash_request_ctx(req);
698 dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n",
699 ctx->op, req->nbytes);
701 err = omap_sham_hw_init(dd);
705 omap_set_dma_dest_params(dd->dma_lch, 0,
706 OMAP_DMA_AMODE_CONSTANT,
707 dd->phys_base + SHA_REG_DIN(0), 0, 16);
709 omap_set_dma_dest_burst_mode(dd->dma_lch,
710 OMAP_DMA_DATA_BURST_16);
712 omap_set_dma_src_burst_mode(dd->dma_lch,
713 OMAP_DMA_DATA_BURST_4);
716 /* request has changed - restore hash */
717 omap_sham_copy_hash(req, 0);
719 if (ctx->op == OP_UPDATE) {
720 err = omap_sham_update_req(dd);
721 if (err != -EINPROGRESS && (ctx->flags & BIT(FLAGS_FINUP)))
722 /* no final() after finup() */
723 err = omap_sham_final_req(dd);
724 } else if (ctx->op == OP_FINAL) {
725 err = omap_sham_final_req(dd);
728 if (err != -EINPROGRESS)
729 /* done_task will not finish it, so do it here */
730 omap_sham_finish_req(req, err);
732 dev_dbg(dd->dev, "exit, err: %d\n", err);
737 static int omap_sham_enqueue(struct ahash_request *req, unsigned int op)
739 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
740 struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
741 struct omap_sham_dev *dd = tctx->dd;
745 return omap_sham_handle_queue(dd, req);
748 static int omap_sham_update(struct ahash_request *req)
750 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
755 ctx->total = req->nbytes;
759 if (ctx->flags & BIT(FLAGS_FINUP)) {
760 if ((ctx->digcnt + ctx->bufcnt + ctx->total) < 9) {
762 * OMAP HW accel works only with buffers >= 9
763 * will switch to bypass in final()
764 * final has the same request and data
766 omap_sham_append_sg(ctx);
768 } else if (ctx->bufcnt + ctx->total <= SHA1_MD5_BLOCK_SIZE) {
770 * faster to use CPU for short transfers
772 ctx->flags |= BIT(FLAGS_CPU);
774 } else if (ctx->bufcnt + ctx->total < ctx->buflen) {
775 omap_sham_append_sg(ctx);
779 return omap_sham_enqueue(req, OP_UPDATE);
782 static int omap_sham_shash_digest(struct crypto_shash *shash, u32 flags,
783 const u8 *data, unsigned int len, u8 *out)
786 struct shash_desc shash;
787 char ctx[crypto_shash_descsize(shash)];
790 desc.shash.tfm = shash;
791 desc.shash.flags = flags & CRYPTO_TFM_REQ_MAY_SLEEP;
793 return crypto_shash_digest(&desc.shash, data, len, out);
796 static int omap_sham_final_shash(struct ahash_request *req)
798 struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
799 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
801 return omap_sham_shash_digest(tctx->fallback, req->base.flags,
802 ctx->buffer, ctx->bufcnt, req->result);
805 static int omap_sham_final(struct ahash_request *req)
807 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
809 ctx->flags |= BIT(FLAGS_FINUP);
811 if (ctx->flags & BIT(FLAGS_ERROR))
812 return 0; /* uncompleted hash is not needed */
814 /* OMAP HW accel works only with buffers >= 9 */
815 /* HMAC is always >= 9 because ipad == block size */
816 if ((ctx->digcnt + ctx->bufcnt) < 9)
817 return omap_sham_final_shash(req);
818 else if (ctx->bufcnt)
819 return omap_sham_enqueue(req, OP_FINAL);
821 /* copy ready hash (+ finalize hmac) */
822 return omap_sham_finish(req);
825 static int omap_sham_finup(struct ahash_request *req)
827 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
830 ctx->flags |= BIT(FLAGS_FINUP);
832 err1 = omap_sham_update(req);
833 if (err1 == -EINPROGRESS || err1 == -EBUSY)
836 * final() has to be always called to cleanup resources
837 * even if udpate() failed, except EINPROGRESS
839 err2 = omap_sham_final(req);
844 static int omap_sham_digest(struct ahash_request *req)
846 return omap_sham_init(req) ?: omap_sham_finup(req);
849 static int omap_sham_setkey(struct crypto_ahash *tfm, const u8 *key,
852 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
853 struct omap_sham_hmac_ctx *bctx = tctx->base;
854 int bs = crypto_shash_blocksize(bctx->shash);
855 int ds = crypto_shash_digestsize(bctx->shash);
857 err = crypto_shash_setkey(tctx->fallback, key, keylen);
862 err = omap_sham_shash_digest(bctx->shash,
863 crypto_shash_get_flags(bctx->shash),
864 key, keylen, bctx->ipad);
869 memcpy(bctx->ipad, key, keylen);
872 memset(bctx->ipad + keylen, 0, bs - keylen);
873 memcpy(bctx->opad, bctx->ipad, bs);
875 for (i = 0; i < bs; i++) {
876 bctx->ipad[i] ^= 0x36;
877 bctx->opad[i] ^= 0x5c;
883 static int omap_sham_cra_init_alg(struct crypto_tfm *tfm, const char *alg_base)
885 struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm);
886 const char *alg_name = crypto_tfm_alg_name(tfm);
888 /* Allocate a fallback and abort if it failed. */
889 tctx->fallback = crypto_alloc_shash(alg_name, 0,
890 CRYPTO_ALG_NEED_FALLBACK);
891 if (IS_ERR(tctx->fallback)) {
892 pr_err("omap-sham: fallback driver '%s' "
893 "could not be loaded.\n", alg_name);
894 return PTR_ERR(tctx->fallback);
897 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
898 sizeof(struct omap_sham_reqctx) + BUFLEN);
901 struct omap_sham_hmac_ctx *bctx = tctx->base;
902 tctx->flags |= BIT(FLAGS_HMAC);
903 bctx->shash = crypto_alloc_shash(alg_base, 0,
904 CRYPTO_ALG_NEED_FALLBACK);
905 if (IS_ERR(bctx->shash)) {
906 pr_err("omap-sham: base driver '%s' "
907 "could not be loaded.\n", alg_base);
908 crypto_free_shash(tctx->fallback);
909 return PTR_ERR(bctx->shash);
917 static int omap_sham_cra_init(struct crypto_tfm *tfm)
919 return omap_sham_cra_init_alg(tfm, NULL);
922 static int omap_sham_cra_sha1_init(struct crypto_tfm *tfm)
924 return omap_sham_cra_init_alg(tfm, "sha1");
927 static int omap_sham_cra_md5_init(struct crypto_tfm *tfm)
929 return omap_sham_cra_init_alg(tfm, "md5");
932 static void omap_sham_cra_exit(struct crypto_tfm *tfm)
934 struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm);
936 crypto_free_shash(tctx->fallback);
937 tctx->fallback = NULL;
939 if (tctx->flags & BIT(FLAGS_HMAC)) {
940 struct omap_sham_hmac_ctx *bctx = tctx->base;
941 crypto_free_shash(bctx->shash);
945 static struct ahash_alg algs[] = {
947 .init = omap_sham_init,
948 .update = omap_sham_update,
949 .final = omap_sham_final,
950 .finup = omap_sham_finup,
951 .digest = omap_sham_digest,
952 .halg.digestsize = SHA1_DIGEST_SIZE,
955 .cra_driver_name = "omap-sha1",
957 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
958 CRYPTO_ALG_KERN_DRIVER_ONLY |
960 CRYPTO_ALG_NEED_FALLBACK,
961 .cra_blocksize = SHA1_BLOCK_SIZE,
962 .cra_ctxsize = sizeof(struct omap_sham_ctx),
964 .cra_module = THIS_MODULE,
965 .cra_init = omap_sham_cra_init,
966 .cra_exit = omap_sham_cra_exit,
970 .init = omap_sham_init,
971 .update = omap_sham_update,
972 .final = omap_sham_final,
973 .finup = omap_sham_finup,
974 .digest = omap_sham_digest,
975 .halg.digestsize = MD5_DIGEST_SIZE,
978 .cra_driver_name = "omap-md5",
980 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
981 CRYPTO_ALG_KERN_DRIVER_ONLY |
983 CRYPTO_ALG_NEED_FALLBACK,
984 .cra_blocksize = SHA1_BLOCK_SIZE,
985 .cra_ctxsize = sizeof(struct omap_sham_ctx),
986 .cra_alignmask = OMAP_ALIGN_MASK,
987 .cra_module = THIS_MODULE,
988 .cra_init = omap_sham_cra_init,
989 .cra_exit = omap_sham_cra_exit,
993 .init = omap_sham_init,
994 .update = omap_sham_update,
995 .final = omap_sham_final,
996 .finup = omap_sham_finup,
997 .digest = omap_sham_digest,
998 .setkey = omap_sham_setkey,
999 .halg.digestsize = SHA1_DIGEST_SIZE,
1001 .cra_name = "hmac(sha1)",
1002 .cra_driver_name = "omap-hmac-sha1",
1003 .cra_priority = 100,
1004 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1005 CRYPTO_ALG_KERN_DRIVER_ONLY |
1007 CRYPTO_ALG_NEED_FALLBACK,
1008 .cra_blocksize = SHA1_BLOCK_SIZE,
1009 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1010 sizeof(struct omap_sham_hmac_ctx),
1011 .cra_alignmask = OMAP_ALIGN_MASK,
1012 .cra_module = THIS_MODULE,
1013 .cra_init = omap_sham_cra_sha1_init,
1014 .cra_exit = omap_sham_cra_exit,
1018 .init = omap_sham_init,
1019 .update = omap_sham_update,
1020 .final = omap_sham_final,
1021 .finup = omap_sham_finup,
1022 .digest = omap_sham_digest,
1023 .setkey = omap_sham_setkey,
1024 .halg.digestsize = MD5_DIGEST_SIZE,
1026 .cra_name = "hmac(md5)",
1027 .cra_driver_name = "omap-hmac-md5",
1028 .cra_priority = 100,
1029 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1030 CRYPTO_ALG_KERN_DRIVER_ONLY |
1032 CRYPTO_ALG_NEED_FALLBACK,
1033 .cra_blocksize = SHA1_BLOCK_SIZE,
1034 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1035 sizeof(struct omap_sham_hmac_ctx),
1036 .cra_alignmask = OMAP_ALIGN_MASK,
1037 .cra_module = THIS_MODULE,
1038 .cra_init = omap_sham_cra_md5_init,
1039 .cra_exit = omap_sham_cra_exit,
1044 static void omap_sham_done_task(unsigned long data)
1046 struct omap_sham_dev *dd = (struct omap_sham_dev *)data;
1049 if (!test_bit(FLAGS_BUSY, &dd->flags)) {
1050 omap_sham_handle_queue(dd, NULL);
1054 if (test_bit(FLAGS_CPU, &dd->flags)) {
1055 if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags))
1057 } else if (test_bit(FLAGS_DMA_READY, &dd->flags)) {
1058 if (test_and_clear_bit(FLAGS_DMA_ACTIVE, &dd->flags)) {
1059 omap_sham_update_dma_stop(dd);
1065 if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags)) {
1066 /* hash or semi-hash ready */
1067 clear_bit(FLAGS_DMA_READY, &dd->flags);
1068 err = omap_sham_update_dma_start(dd);
1069 if (err != -EINPROGRESS)
1077 dev_dbg(dd->dev, "update done: err: %d\n", err);
1078 /* finish curent request */
1079 omap_sham_finish_req(dd->req, err);
1082 static irqreturn_t omap_sham_irq(int irq, void *dev_id)
1084 struct omap_sham_dev *dd = dev_id;
1086 if (unlikely(test_bit(FLAGS_FINAL, &dd->flags)))
1087 /* final -> allow device to go to power-saving mode */
1088 omap_sham_write_mask(dd, SHA_REG_CTRL, 0, SHA_REG_CTRL_LENGTH);
1090 omap_sham_write_mask(dd, SHA_REG_CTRL, SHA_REG_CTRL_OUTPUT_READY,
1091 SHA_REG_CTRL_OUTPUT_READY);
1092 omap_sham_read(dd, SHA_REG_CTRL);
1094 if (!test_bit(FLAGS_BUSY, &dd->flags)) {
1095 dev_warn(dd->dev, "Interrupt when no active requests.\n");
1099 set_bit(FLAGS_OUTPUT_READY, &dd->flags);
1100 tasklet_schedule(&dd->done_task);
1105 static void omap_sham_dma_callback(int lch, u16 ch_status, void *data)
1107 struct omap_sham_dev *dd = data;
1109 if (ch_status != OMAP_DMA_BLOCK_IRQ) {
1110 pr_err("omap-sham DMA error status: 0x%hx\n", ch_status);
1112 clear_bit(FLAGS_INIT, &dd->flags);/* request to re-initialize */
1115 set_bit(FLAGS_DMA_READY, &dd->flags);
1116 tasklet_schedule(&dd->done_task);
1119 static int omap_sham_dma_init(struct omap_sham_dev *dd)
1125 err = omap_request_dma(dd->dma, dev_name(dd->dev),
1126 omap_sham_dma_callback, dd, &dd->dma_lch);
1128 dev_err(dd->dev, "Unable to request DMA channel\n");
1135 static void omap_sham_dma_cleanup(struct omap_sham_dev *dd)
1137 if (dd->dma_lch >= 0) {
1138 omap_free_dma(dd->dma_lch);
1143 static int omap_sham_probe(struct platform_device *pdev)
1145 struct omap_sham_dev *dd;
1146 struct device *dev = &pdev->dev;
1147 struct resource *res;
1150 dd = kzalloc(sizeof(struct omap_sham_dev), GFP_KERNEL);
1152 dev_err(dev, "unable to alloc data struct.\n");
1157 platform_set_drvdata(pdev, dd);
1159 INIT_LIST_HEAD(&dd->list);
1160 spin_lock_init(&dd->lock);
1161 tasklet_init(&dd->done_task, omap_sham_done_task, (unsigned long)dd);
1162 crypto_init_queue(&dd->queue, OMAP_SHAM_QUEUE_LENGTH);
1166 /* Get the base address */
1167 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1169 dev_err(dev, "no MEM resource info\n");
1173 dd->phys_base = res->start;
1176 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1178 dev_err(dev, "no DMA resource info\n");
1182 dd->dma = res->start;
1185 dd->irq = platform_get_irq(pdev, 0);
1187 dev_err(dev, "no IRQ resource info\n");
1192 err = request_irq(dd->irq, omap_sham_irq,
1193 IRQF_TRIGGER_LOW, dev_name(dev), dd);
1195 dev_err(dev, "unable to request irq.\n");
1199 err = omap_sham_dma_init(dd);
1203 /* Initializing the clock */
1204 dd->iclk = clk_get(dev, "ick");
1205 if (IS_ERR(dd->iclk)) {
1206 dev_err(dev, "clock intialization failed.\n");
1207 err = PTR_ERR(dd->iclk);
1211 dd->io_base = ioremap(dd->phys_base, SZ_4K);
1213 dev_err(dev, "can't ioremap\n");
1218 clk_enable(dd->iclk);
1219 dev_info(dev, "hw accel on OMAP rev %u.%u\n",
1220 (omap_sham_read(dd, SHA_REG_REV) & SHA_REG_REV_MAJOR) >> 4,
1221 omap_sham_read(dd, SHA_REG_REV) & SHA_REG_REV_MINOR);
1222 clk_disable(dd->iclk);
1224 spin_lock(&sham.lock);
1225 list_add_tail(&dd->list, &sham.dev_list);
1226 spin_unlock(&sham.lock);
1228 for (i = 0; i < ARRAY_SIZE(algs); i++) {
1229 err = crypto_register_ahash(&algs[i]);
1237 for (j = 0; j < i; j++)
1238 crypto_unregister_ahash(&algs[j]);
1239 iounmap(dd->io_base);
1243 omap_sham_dma_cleanup(dd);
1246 free_irq(dd->irq, dd);
1251 dev_err(dev, "initialization failed.\n");
1256 static int omap_sham_remove(struct platform_device *pdev)
1258 static struct omap_sham_dev *dd;
1261 dd = platform_get_drvdata(pdev);
1264 spin_lock(&sham.lock);
1265 list_del(&dd->list);
1266 spin_unlock(&sham.lock);
1267 for (i = 0; i < ARRAY_SIZE(algs); i++)
1268 crypto_unregister_ahash(&algs[i]);
1269 tasklet_kill(&dd->done_task);
1270 iounmap(dd->io_base);
1272 omap_sham_dma_cleanup(dd);
1274 free_irq(dd->irq, dd);
1281 static struct platform_driver omap_sham_driver = {
1282 .probe = omap_sham_probe,
1283 .remove = omap_sham_remove,
1285 .name = "omap-sham",
1286 .owner = THIS_MODULE,
1290 static int __init omap_sham_mod_init(void)
1292 pr_info("loading %s driver\n", "omap-sham");
1294 return platform_driver_register(&omap_sham_driver);
1297 static void __exit omap_sham_mod_exit(void)
1299 platform_driver_unregister(&omap_sham_driver);
1302 module_init(omap_sham_mod_init);
1303 module_exit(omap_sham_mod_exit);
1305 MODULE_DESCRIPTION("OMAP SHA1/MD5 hw acceleration support.");
1306 MODULE_LICENSE("GPL v2");
1307 MODULE_AUTHOR("Dmitry Kasatkin");