4 * Support for OMAP SHA1/MD5 HW acceleration.
6 * Copyright (c) 2010 Nokia Corporation
7 * Author: Dmitry Kasatkin <dmitry.kasatkin@nokia.com>
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/irq.h>
27 #include <linux/platform_device.h>
28 #include <linux/scatterlist.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/dmaengine.h>
31 #include <linux/omap-dma.h>
32 #include <linux/pm_runtime.h>
33 #include <linux/delay.h>
34 #include <linux/crypto.h>
35 #include <linux/cryptohash.h>
36 #include <crypto/scatterwalk.h>
37 #include <crypto/algapi.h>
38 #include <crypto/sha.h>
39 #include <crypto/hash.h>
40 #include <crypto/internal/hash.h>
42 #define SHA_REG_DIGEST(x) (0x00 + ((x) * 0x04))
43 #define SHA_REG_DIN(x) (0x1C + ((x) * 0x04))
45 #define SHA1_MD5_BLOCK_SIZE SHA1_BLOCK_SIZE
46 #define MD5_DIGEST_SIZE 16
48 #define DST_MAXBURST 16
49 #define DMA_MIN (DST_MAXBURST * sizeof(u32))
51 #define SHA_REG_DIGCNT 0x14
53 #define SHA_REG_CTRL 0x18
54 #define SHA_REG_CTRL_LENGTH (0xFFFFFFFF << 5)
55 #define SHA_REG_CTRL_CLOSE_HASH (1 << 4)
56 #define SHA_REG_CTRL_ALGO_CONST (1 << 3)
57 #define SHA_REG_CTRL_ALGO (1 << 2)
58 #define SHA_REG_CTRL_INPUT_READY (1 << 1)
59 #define SHA_REG_CTRL_OUTPUT_READY (1 << 0)
61 #define SHA_REG_REV 0x5C
62 #define SHA_REG_REV_MAJOR 0xF0
63 #define SHA_REG_REV_MINOR 0x0F
65 #define SHA_REG_MASK 0x60
66 #define SHA_REG_MASK_DMA_EN (1 << 3)
67 #define SHA_REG_MASK_IT_EN (1 << 2)
68 #define SHA_REG_MASK_SOFTRESET (1 << 1)
69 #define SHA_REG_AUTOIDLE (1 << 0)
71 #define SHA_REG_SYSSTATUS 0x64
72 #define SHA_REG_SYSSTATUS_RESETDONE (1 << 0)
74 #define DEFAULT_TIMEOUT_INTERVAL HZ
76 /* mostly device flags */
79 #define FLAGS_DMA_ACTIVE 2
80 #define FLAGS_OUTPUT_READY 3
83 #define FLAGS_DMA_READY 6
85 #define FLAGS_FINUP 16
89 #define FLAGS_ERROR 20
94 #define OMAP_ALIGN_MASK (sizeof(u32)-1)
95 #define OMAP_ALIGNED __attribute__((aligned(sizeof(u32))))
97 #define BUFLEN PAGE_SIZE
101 struct omap_sham_reqctx {
102 struct omap_sham_dev *dd;
106 u8 digest[SHA1_DIGEST_SIZE] OMAP_ALIGNED;
113 struct scatterlist *sg;
114 struct scatterlist sgl;
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;
148 struct dma_chan *dma_lch;
149 struct tasklet_struct done_task;
152 struct crypto_queue queue;
153 struct ahash_request *req;
156 struct omap_sham_drv {
157 struct list_head dev_list;
162 static struct omap_sham_drv sham = {
163 .dev_list = LIST_HEAD_INIT(sham.dev_list),
164 .lock = __SPIN_LOCK_UNLOCKED(sham.lock),
167 static inline u32 omap_sham_read(struct omap_sham_dev *dd, u32 offset)
169 return __raw_readl(dd->io_base + offset);
172 static inline void omap_sham_write(struct omap_sham_dev *dd,
173 u32 offset, u32 value)
175 __raw_writel(value, dd->io_base + offset);
178 static inline void omap_sham_write_mask(struct omap_sham_dev *dd, u32 address,
183 val = omap_sham_read(dd, address);
186 omap_sham_write(dd, address, val);
189 static inline int omap_sham_wait(struct omap_sham_dev *dd, u32 offset, u32 bit)
191 unsigned long timeout = jiffies + DEFAULT_TIMEOUT_INTERVAL;
193 while (!(omap_sham_read(dd, offset) & bit)) {
194 if (time_is_before_jiffies(timeout))
201 static void omap_sham_copy_hash(struct ahash_request *req, int out)
203 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
204 u32 *hash = (u32 *)ctx->digest;
207 /* MD5 is almost unused. So copy sha1 size to reduce code */
208 for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(u32); i++) {
210 hash[i] = omap_sham_read(ctx->dd,
213 omap_sham_write(ctx->dd,
214 SHA_REG_DIGEST(i), hash[i]);
218 static void omap_sham_copy_ready_hash(struct ahash_request *req)
220 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
221 u32 *in = (u32 *)ctx->digest;
222 u32 *hash = (u32 *)req->result;
228 if (likely(ctx->flags & BIT(FLAGS_SHA1))) {
229 /* SHA1 results are in big endian */
230 for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(u32); i++)
231 hash[i] = be32_to_cpu(in[i]);
233 /* MD5 results are in little endian */
234 for (i = 0; i < MD5_DIGEST_SIZE / sizeof(u32); i++)
235 hash[i] = le32_to_cpu(in[i]);
239 static int omap_sham_hw_init(struct omap_sham_dev *dd)
241 pm_runtime_get_sync(dd->dev);
243 if (!test_bit(FLAGS_INIT, &dd->flags)) {
244 omap_sham_write_mask(dd, SHA_REG_MASK,
245 SHA_REG_MASK_SOFTRESET, SHA_REG_MASK_SOFTRESET);
247 if (omap_sham_wait(dd, SHA_REG_SYSSTATUS,
248 SHA_REG_SYSSTATUS_RESETDONE))
251 set_bit(FLAGS_INIT, &dd->flags);
258 static void omap_sham_write_ctrl(struct omap_sham_dev *dd, size_t length,
261 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
262 u32 val = length << 5, mask;
264 if (likely(ctx->digcnt))
265 omap_sham_write(dd, SHA_REG_DIGCNT, ctx->digcnt);
267 omap_sham_write_mask(dd, SHA_REG_MASK,
268 SHA_REG_MASK_IT_EN | (dma ? SHA_REG_MASK_DMA_EN : 0),
269 SHA_REG_MASK_IT_EN | SHA_REG_MASK_DMA_EN);
271 * Setting ALGO_CONST only for the first iteration
272 * and CLOSE_HASH only for the last one.
274 if (ctx->flags & BIT(FLAGS_SHA1))
275 val |= SHA_REG_CTRL_ALGO;
277 val |= SHA_REG_CTRL_ALGO_CONST;
279 val |= SHA_REG_CTRL_CLOSE_HASH;
281 mask = SHA_REG_CTRL_ALGO_CONST | SHA_REG_CTRL_CLOSE_HASH |
282 SHA_REG_CTRL_ALGO | SHA_REG_CTRL_LENGTH;
284 omap_sham_write_mask(dd, SHA_REG_CTRL, val, mask);
287 static int omap_sham_xmit_cpu(struct omap_sham_dev *dd, const u8 *buf,
288 size_t length, int final)
290 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
292 const u32 *buffer = (const u32 *)buf;
294 dev_dbg(dd->dev, "xmit_cpu: digcnt: %d, length: %d, final: %d\n",
295 ctx->digcnt, length, final);
297 omap_sham_write_ctrl(dd, length, final, 0);
299 /* should be non-zero before next lines to disable clocks later */
300 ctx->digcnt += length;
302 if (omap_sham_wait(dd, SHA_REG_CTRL, SHA_REG_CTRL_INPUT_READY))
306 set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */
308 set_bit(FLAGS_CPU, &dd->flags);
310 len32 = DIV_ROUND_UP(length, sizeof(u32));
312 for (count = 0; count < len32; count++)
313 omap_sham_write(dd, SHA_REG_DIN(count), buffer[count]);
318 static void omap_sham_dma_callback(void *param)
320 struct omap_sham_dev *dd = param;
322 set_bit(FLAGS_DMA_READY, &dd->flags);
323 tasklet_schedule(&dd->done_task);
326 static int omap_sham_xmit_dma(struct omap_sham_dev *dd, dma_addr_t dma_addr,
327 size_t length, int final, int is_sg)
329 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
330 struct dma_async_tx_descriptor *tx;
331 struct dma_slave_config cfg;
334 dev_dbg(dd->dev, "xmit_dma: digcnt: %d, length: %d, final: %d\n",
335 ctx->digcnt, length, final);
337 memset(&cfg, 0, sizeof(cfg));
339 cfg.dst_addr = dd->phys_base + SHA_REG_DIN(0);
340 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
341 cfg.dst_maxburst = DST_MAXBURST;
343 ret = dmaengine_slave_config(dd->dma_lch, &cfg);
345 pr_err("omap-sham: can't configure dmaengine slave: %d\n", ret);
349 len32 = DIV_ROUND_UP(length, DMA_MIN) * DMA_MIN;
353 * The SG entry passed in may not have the 'length' member
354 * set correctly so use a local SG entry (sgl) with the
355 * proper value for 'length' instead. If this is not done,
356 * the dmaengine may try to DMA the incorrect amount of data.
358 sg_init_table(&ctx->sgl, 1);
359 ctx->sgl.page_link = ctx->sg->page_link;
360 ctx->sgl.offset = ctx->sg->offset;
361 sg_dma_len(&ctx->sgl) = len32;
362 sg_dma_address(&ctx->sgl) = sg_dma_address(ctx->sg);
364 tx = dmaengine_prep_slave_sg(dd->dma_lch, &ctx->sgl, 1,
365 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
367 tx = dmaengine_prep_slave_single(dd->dma_lch, dma_addr, len32,
368 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
372 dev_err(dd->dev, "prep_slave_sg/single() failed\n");
376 tx->callback = omap_sham_dma_callback;
377 tx->callback_param = dd;
379 omap_sham_write_ctrl(dd, length, final, 1);
381 ctx->digcnt += length;
384 set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */
386 set_bit(FLAGS_DMA_ACTIVE, &dd->flags);
388 dmaengine_submit(tx);
389 dma_async_issue_pending(dd->dma_lch);
394 static size_t omap_sham_append_buffer(struct omap_sham_reqctx *ctx,
395 const u8 *data, size_t length)
397 size_t count = min(length, ctx->buflen - ctx->bufcnt);
399 count = min(count, ctx->total);
402 memcpy(ctx->buffer + ctx->bufcnt, data, count);
403 ctx->bufcnt += count;
408 static size_t omap_sham_append_sg(struct omap_sham_reqctx *ctx)
413 count = omap_sham_append_buffer(ctx,
414 sg_virt(ctx->sg) + ctx->offset,
415 ctx->sg->length - ctx->offset);
418 ctx->offset += count;
420 if (ctx->offset == ctx->sg->length) {
421 ctx->sg = sg_next(ctx->sg);
432 static int omap_sham_xmit_dma_map(struct omap_sham_dev *dd,
433 struct omap_sham_reqctx *ctx,
434 size_t length, int final)
438 ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer, ctx->buflen,
440 if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
441 dev_err(dd->dev, "dma %u bytes error\n", ctx->buflen);
445 ctx->flags &= ~BIT(FLAGS_SG);
447 ret = omap_sham_xmit_dma(dd, ctx->dma_addr, length, final, 0);
449 dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen,
455 static int omap_sham_update_dma_slow(struct omap_sham_dev *dd)
457 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
461 omap_sham_append_sg(ctx);
463 final = (ctx->flags & BIT(FLAGS_FINUP)) && !ctx->total;
465 dev_dbg(dd->dev, "slow: bufcnt: %u, digcnt: %d, final: %d\n",
466 ctx->bufcnt, ctx->digcnt, final);
468 if (final || (ctx->bufcnt == ctx->buflen && ctx->total)) {
471 return omap_sham_xmit_dma_map(dd, ctx, count, final);
477 /* Start address alignment */
478 #define SG_AA(sg) (IS_ALIGNED(sg->offset, sizeof(u32)))
479 /* SHA1 block size alignment */
480 #define SG_SA(sg) (IS_ALIGNED(sg->length, SHA1_MD5_BLOCK_SIZE))
482 static int omap_sham_update_dma_start(struct omap_sham_dev *dd)
484 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
485 unsigned int length, final, tail;
486 struct scatterlist *sg;
492 if (ctx->bufcnt || ctx->offset)
493 return omap_sham_update_dma_slow(dd);
496 * Don't use the sg interface when the transfer size is less
497 * than the number of elements in a DMA frame. Otherwise,
498 * the dmaengine infrastructure will calculate that it needs
499 * to transfer 0 frames which ultimately fails.
501 if (ctx->total < (DST_MAXBURST * sizeof(u32)))
502 return omap_sham_update_dma_slow(dd);
504 dev_dbg(dd->dev, "fast: digcnt: %d, bufcnt: %u, total: %u\n",
505 ctx->digcnt, ctx->bufcnt, ctx->total);
510 return omap_sham_update_dma_slow(dd);
512 if (!sg_is_last(sg) && !SG_SA(sg))
513 /* size is not SHA1_BLOCK_SIZE aligned */
514 return omap_sham_update_dma_slow(dd);
516 length = min(ctx->total, sg->length);
518 if (sg_is_last(sg)) {
519 if (!(ctx->flags & BIT(FLAGS_FINUP))) {
520 /* not last sg must be SHA1_MD5_BLOCK_SIZE aligned */
521 tail = length & (SHA1_MD5_BLOCK_SIZE - 1);
522 /* without finup() we need one block to close hash */
524 tail = SHA1_MD5_BLOCK_SIZE;
529 if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
530 dev_err(dd->dev, "dma_map_sg error\n");
534 ctx->flags |= BIT(FLAGS_SG);
536 ctx->total -= length;
537 ctx->offset = length; /* offset where to start slow */
539 final = (ctx->flags & BIT(FLAGS_FINUP)) && !ctx->total;
541 ret = omap_sham_xmit_dma(dd, sg_dma_address(ctx->sg), length, final, 1);
543 dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE);
548 static int omap_sham_update_cpu(struct omap_sham_dev *dd)
550 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
553 omap_sham_append_sg(ctx);
554 bufcnt = ctx->bufcnt;
557 return omap_sham_xmit_cpu(dd, ctx->buffer, bufcnt, 1);
560 static int omap_sham_update_dma_stop(struct omap_sham_dev *dd)
562 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
564 dmaengine_terminate_all(dd->dma_lch);
566 if (ctx->flags & BIT(FLAGS_SG)) {
567 dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE);
568 if (ctx->sg->length == ctx->offset) {
569 ctx->sg = sg_next(ctx->sg);
574 dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen,
581 static int omap_sham_init(struct ahash_request *req)
583 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
584 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
585 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
586 struct omap_sham_dev *dd = NULL, *tmp;
588 spin_lock_bh(&sham.lock);
590 list_for_each_entry(tmp, &sham.dev_list, list) {
598 spin_unlock_bh(&sham.lock);
604 dev_dbg(dd->dev, "init: digest size: %d\n",
605 crypto_ahash_digestsize(tfm));
607 if (crypto_ahash_digestsize(tfm) == SHA1_DIGEST_SIZE)
608 ctx->flags |= BIT(FLAGS_SHA1);
612 ctx->buflen = BUFLEN;
614 if (tctx->flags & BIT(FLAGS_HMAC)) {
615 struct omap_sham_hmac_ctx *bctx = tctx->base;
617 memcpy(ctx->buffer, bctx->ipad, SHA1_MD5_BLOCK_SIZE);
618 ctx->bufcnt = SHA1_MD5_BLOCK_SIZE;
619 ctx->flags |= BIT(FLAGS_HMAC);
626 static int omap_sham_update_req(struct omap_sham_dev *dd)
628 struct ahash_request *req = dd->req;
629 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
632 dev_dbg(dd->dev, "update_req: total: %u, digcnt: %d, finup: %d\n",
633 ctx->total, ctx->digcnt, (ctx->flags & BIT(FLAGS_FINUP)) != 0);
635 if (ctx->flags & BIT(FLAGS_CPU))
636 err = omap_sham_update_cpu(dd);
638 err = omap_sham_update_dma_start(dd);
640 /* wait for dma completion before can take more data */
641 dev_dbg(dd->dev, "update: err: %d, digcnt: %d\n", err, ctx->digcnt);
646 static int omap_sham_final_req(struct omap_sham_dev *dd)
648 struct ahash_request *req = dd->req;
649 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
650 int err = 0, use_dma = 1;
652 if (ctx->bufcnt <= DMA_MIN)
653 /* faster to handle last block with cpu */
657 err = omap_sham_xmit_dma_map(dd, ctx, ctx->bufcnt, 1);
659 err = omap_sham_xmit_cpu(dd, ctx->buffer, ctx->bufcnt, 1);
663 dev_dbg(dd->dev, "final_req: err: %d\n", err);
668 static int omap_sham_finish_hmac(struct ahash_request *req)
670 struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
671 struct omap_sham_hmac_ctx *bctx = tctx->base;
672 int bs = crypto_shash_blocksize(bctx->shash);
673 int ds = crypto_shash_digestsize(bctx->shash);
675 struct shash_desc shash;
676 char ctx[crypto_shash_descsize(bctx->shash)];
679 desc.shash.tfm = bctx->shash;
680 desc.shash.flags = 0; /* not CRYPTO_TFM_REQ_MAY_SLEEP */
682 return crypto_shash_init(&desc.shash) ?:
683 crypto_shash_update(&desc.shash, bctx->opad, bs) ?:
684 crypto_shash_finup(&desc.shash, req->result, ds, req->result);
687 static int omap_sham_finish(struct ahash_request *req)
689 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
690 struct omap_sham_dev *dd = ctx->dd;
694 omap_sham_copy_ready_hash(req);
695 if (ctx->flags & BIT(FLAGS_HMAC))
696 err = omap_sham_finish_hmac(req);
699 dev_dbg(dd->dev, "digcnt: %d, bufcnt: %d\n", ctx->digcnt, ctx->bufcnt);
704 static void omap_sham_finish_req(struct ahash_request *req, int err)
706 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
707 struct omap_sham_dev *dd = ctx->dd;
710 omap_sham_copy_hash(req, 1);
711 if (test_bit(FLAGS_FINAL, &dd->flags))
712 err = omap_sham_finish(req);
714 ctx->flags |= BIT(FLAGS_ERROR);
717 /* atomic operation is not needed here */
718 dd->flags &= ~(BIT(FLAGS_BUSY) | BIT(FLAGS_FINAL) | BIT(FLAGS_CPU) |
719 BIT(FLAGS_DMA_READY) | BIT(FLAGS_OUTPUT_READY));
721 pm_runtime_put_sync(dd->dev);
723 if (req->base.complete)
724 req->base.complete(&req->base, err);
726 /* handle new request */
727 tasklet_schedule(&dd->done_task);
730 static int omap_sham_handle_queue(struct omap_sham_dev *dd,
731 struct ahash_request *req)
733 struct crypto_async_request *async_req, *backlog;
734 struct omap_sham_reqctx *ctx;
736 int err = 0, ret = 0;
738 spin_lock_irqsave(&dd->lock, flags);
740 ret = ahash_enqueue_request(&dd->queue, req);
741 if (test_bit(FLAGS_BUSY, &dd->flags)) {
742 spin_unlock_irqrestore(&dd->lock, flags);
745 backlog = crypto_get_backlog(&dd->queue);
746 async_req = crypto_dequeue_request(&dd->queue);
748 set_bit(FLAGS_BUSY, &dd->flags);
749 spin_unlock_irqrestore(&dd->lock, flags);
755 backlog->complete(backlog, -EINPROGRESS);
757 req = ahash_request_cast(async_req);
759 ctx = ahash_request_ctx(req);
761 dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n",
762 ctx->op, req->nbytes);
764 err = omap_sham_hw_init(dd);
769 /* request has changed - restore hash */
770 omap_sham_copy_hash(req, 0);
772 if (ctx->op == OP_UPDATE) {
773 err = omap_sham_update_req(dd);
774 if (err != -EINPROGRESS && (ctx->flags & BIT(FLAGS_FINUP)))
775 /* no final() after finup() */
776 err = omap_sham_final_req(dd);
777 } else if (ctx->op == OP_FINAL) {
778 err = omap_sham_final_req(dd);
781 if (err != -EINPROGRESS)
782 /* done_task will not finish it, so do it here */
783 omap_sham_finish_req(req, err);
785 dev_dbg(dd->dev, "exit, err: %d\n", err);
790 static int omap_sham_enqueue(struct ahash_request *req, unsigned int op)
792 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
793 struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
794 struct omap_sham_dev *dd = tctx->dd;
798 return omap_sham_handle_queue(dd, req);
801 static int omap_sham_update(struct ahash_request *req)
803 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
808 ctx->total = req->nbytes;
812 if (ctx->flags & BIT(FLAGS_FINUP)) {
813 if ((ctx->digcnt + ctx->bufcnt + ctx->total) < 9) {
815 * OMAP HW accel works only with buffers >= 9
816 * will switch to bypass in final()
817 * final has the same request and data
819 omap_sham_append_sg(ctx);
821 } else if (ctx->bufcnt + ctx->total <= SHA1_MD5_BLOCK_SIZE) {
823 * faster to use CPU for short transfers
825 ctx->flags |= BIT(FLAGS_CPU);
827 } else if (ctx->bufcnt + ctx->total < ctx->buflen) {
828 omap_sham_append_sg(ctx);
832 return omap_sham_enqueue(req, OP_UPDATE);
835 static int omap_sham_shash_digest(struct crypto_shash *shash, u32 flags,
836 const u8 *data, unsigned int len, u8 *out)
839 struct shash_desc shash;
840 char ctx[crypto_shash_descsize(shash)];
843 desc.shash.tfm = shash;
844 desc.shash.flags = flags & CRYPTO_TFM_REQ_MAY_SLEEP;
846 return crypto_shash_digest(&desc.shash, data, len, out);
849 static int omap_sham_final_shash(struct ahash_request *req)
851 struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
852 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
854 return omap_sham_shash_digest(tctx->fallback, req->base.flags,
855 ctx->buffer, ctx->bufcnt, req->result);
858 static int omap_sham_final(struct ahash_request *req)
860 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
862 ctx->flags |= BIT(FLAGS_FINUP);
864 if (ctx->flags & BIT(FLAGS_ERROR))
865 return 0; /* uncompleted hash is not needed */
867 /* OMAP HW accel works only with buffers >= 9 */
868 /* HMAC is always >= 9 because ipad == block size */
869 if ((ctx->digcnt + ctx->bufcnt) < 9)
870 return omap_sham_final_shash(req);
871 else if (ctx->bufcnt)
872 return omap_sham_enqueue(req, OP_FINAL);
874 /* copy ready hash (+ finalize hmac) */
875 return omap_sham_finish(req);
878 static int omap_sham_finup(struct ahash_request *req)
880 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
883 ctx->flags |= BIT(FLAGS_FINUP);
885 err1 = omap_sham_update(req);
886 if (err1 == -EINPROGRESS || err1 == -EBUSY)
889 * final() has to be always called to cleanup resources
890 * even if udpate() failed, except EINPROGRESS
892 err2 = omap_sham_final(req);
897 static int omap_sham_digest(struct ahash_request *req)
899 return omap_sham_init(req) ?: omap_sham_finup(req);
902 static int omap_sham_setkey(struct crypto_ahash *tfm, const u8 *key,
905 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
906 struct omap_sham_hmac_ctx *bctx = tctx->base;
907 int bs = crypto_shash_blocksize(bctx->shash);
908 int ds = crypto_shash_digestsize(bctx->shash);
910 err = crypto_shash_setkey(tctx->fallback, key, keylen);
915 err = omap_sham_shash_digest(bctx->shash,
916 crypto_shash_get_flags(bctx->shash),
917 key, keylen, bctx->ipad);
922 memcpy(bctx->ipad, key, keylen);
925 memset(bctx->ipad + keylen, 0, bs - keylen);
926 memcpy(bctx->opad, bctx->ipad, bs);
928 for (i = 0; i < bs; i++) {
929 bctx->ipad[i] ^= 0x36;
930 bctx->opad[i] ^= 0x5c;
936 static int omap_sham_cra_init_alg(struct crypto_tfm *tfm, const char *alg_base)
938 struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm);
939 const char *alg_name = crypto_tfm_alg_name(tfm);
941 /* Allocate a fallback and abort if it failed. */
942 tctx->fallback = crypto_alloc_shash(alg_name, 0,
943 CRYPTO_ALG_NEED_FALLBACK);
944 if (IS_ERR(tctx->fallback)) {
945 pr_err("omap-sham: fallback driver '%s' "
946 "could not be loaded.\n", alg_name);
947 return PTR_ERR(tctx->fallback);
950 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
951 sizeof(struct omap_sham_reqctx) + BUFLEN);
954 struct omap_sham_hmac_ctx *bctx = tctx->base;
955 tctx->flags |= BIT(FLAGS_HMAC);
956 bctx->shash = crypto_alloc_shash(alg_base, 0,
957 CRYPTO_ALG_NEED_FALLBACK);
958 if (IS_ERR(bctx->shash)) {
959 pr_err("omap-sham: base driver '%s' "
960 "could not be loaded.\n", alg_base);
961 crypto_free_shash(tctx->fallback);
962 return PTR_ERR(bctx->shash);
970 static int omap_sham_cra_init(struct crypto_tfm *tfm)
972 return omap_sham_cra_init_alg(tfm, NULL);
975 static int omap_sham_cra_sha1_init(struct crypto_tfm *tfm)
977 return omap_sham_cra_init_alg(tfm, "sha1");
980 static int omap_sham_cra_md5_init(struct crypto_tfm *tfm)
982 return omap_sham_cra_init_alg(tfm, "md5");
985 static void omap_sham_cra_exit(struct crypto_tfm *tfm)
987 struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm);
989 crypto_free_shash(tctx->fallback);
990 tctx->fallback = NULL;
992 if (tctx->flags & BIT(FLAGS_HMAC)) {
993 struct omap_sham_hmac_ctx *bctx = tctx->base;
994 crypto_free_shash(bctx->shash);
998 static struct ahash_alg algs[] = {
1000 .init = omap_sham_init,
1001 .update = omap_sham_update,
1002 .final = omap_sham_final,
1003 .finup = omap_sham_finup,
1004 .digest = omap_sham_digest,
1005 .halg.digestsize = SHA1_DIGEST_SIZE,
1008 .cra_driver_name = "omap-sha1",
1009 .cra_priority = 100,
1010 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1011 CRYPTO_ALG_KERN_DRIVER_ONLY |
1013 CRYPTO_ALG_NEED_FALLBACK,
1014 .cra_blocksize = SHA1_BLOCK_SIZE,
1015 .cra_ctxsize = sizeof(struct omap_sham_ctx),
1017 .cra_module = THIS_MODULE,
1018 .cra_init = omap_sham_cra_init,
1019 .cra_exit = omap_sham_cra_exit,
1023 .init = omap_sham_init,
1024 .update = omap_sham_update,
1025 .final = omap_sham_final,
1026 .finup = omap_sham_finup,
1027 .digest = omap_sham_digest,
1028 .halg.digestsize = MD5_DIGEST_SIZE,
1031 .cra_driver_name = "omap-md5",
1032 .cra_priority = 100,
1033 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1034 CRYPTO_ALG_KERN_DRIVER_ONLY |
1036 CRYPTO_ALG_NEED_FALLBACK,
1037 .cra_blocksize = SHA1_BLOCK_SIZE,
1038 .cra_ctxsize = sizeof(struct omap_sham_ctx),
1039 .cra_alignmask = OMAP_ALIGN_MASK,
1040 .cra_module = THIS_MODULE,
1041 .cra_init = omap_sham_cra_init,
1042 .cra_exit = omap_sham_cra_exit,
1046 .init = omap_sham_init,
1047 .update = omap_sham_update,
1048 .final = omap_sham_final,
1049 .finup = omap_sham_finup,
1050 .digest = omap_sham_digest,
1051 .setkey = omap_sham_setkey,
1052 .halg.digestsize = SHA1_DIGEST_SIZE,
1054 .cra_name = "hmac(sha1)",
1055 .cra_driver_name = "omap-hmac-sha1",
1056 .cra_priority = 100,
1057 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1058 CRYPTO_ALG_KERN_DRIVER_ONLY |
1060 CRYPTO_ALG_NEED_FALLBACK,
1061 .cra_blocksize = SHA1_BLOCK_SIZE,
1062 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1063 sizeof(struct omap_sham_hmac_ctx),
1064 .cra_alignmask = OMAP_ALIGN_MASK,
1065 .cra_module = THIS_MODULE,
1066 .cra_init = omap_sham_cra_sha1_init,
1067 .cra_exit = omap_sham_cra_exit,
1071 .init = omap_sham_init,
1072 .update = omap_sham_update,
1073 .final = omap_sham_final,
1074 .finup = omap_sham_finup,
1075 .digest = omap_sham_digest,
1076 .setkey = omap_sham_setkey,
1077 .halg.digestsize = MD5_DIGEST_SIZE,
1079 .cra_name = "hmac(md5)",
1080 .cra_driver_name = "omap-hmac-md5",
1081 .cra_priority = 100,
1082 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1083 CRYPTO_ALG_KERN_DRIVER_ONLY |
1085 CRYPTO_ALG_NEED_FALLBACK,
1086 .cra_blocksize = SHA1_BLOCK_SIZE,
1087 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1088 sizeof(struct omap_sham_hmac_ctx),
1089 .cra_alignmask = OMAP_ALIGN_MASK,
1090 .cra_module = THIS_MODULE,
1091 .cra_init = omap_sham_cra_md5_init,
1092 .cra_exit = omap_sham_cra_exit,
1097 static void omap_sham_done_task(unsigned long data)
1099 struct omap_sham_dev *dd = (struct omap_sham_dev *)data;
1102 if (!test_bit(FLAGS_BUSY, &dd->flags)) {
1103 omap_sham_handle_queue(dd, NULL);
1107 if (test_bit(FLAGS_CPU, &dd->flags)) {
1108 if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags))
1110 } else if (test_bit(FLAGS_DMA_READY, &dd->flags)) {
1111 if (test_and_clear_bit(FLAGS_DMA_ACTIVE, &dd->flags)) {
1112 omap_sham_update_dma_stop(dd);
1118 if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags)) {
1119 /* hash or semi-hash ready */
1120 clear_bit(FLAGS_DMA_READY, &dd->flags);
1121 err = omap_sham_update_dma_start(dd);
1122 if (err != -EINPROGRESS)
1130 dev_dbg(dd->dev, "update done: err: %d\n", err);
1131 /* finish curent request */
1132 omap_sham_finish_req(dd->req, err);
1135 static irqreturn_t omap_sham_irq(int irq, void *dev_id)
1137 struct omap_sham_dev *dd = dev_id;
1139 if (unlikely(test_bit(FLAGS_FINAL, &dd->flags)))
1140 /* final -> allow device to go to power-saving mode */
1141 omap_sham_write_mask(dd, SHA_REG_CTRL, 0, SHA_REG_CTRL_LENGTH);
1143 omap_sham_write_mask(dd, SHA_REG_CTRL, SHA_REG_CTRL_OUTPUT_READY,
1144 SHA_REG_CTRL_OUTPUT_READY);
1145 omap_sham_read(dd, SHA_REG_CTRL);
1147 if (!test_bit(FLAGS_BUSY, &dd->flags)) {
1148 dev_warn(dd->dev, "Interrupt when no active requests.\n");
1152 set_bit(FLAGS_OUTPUT_READY, &dd->flags);
1153 tasklet_schedule(&dd->done_task);
1158 static int __devinit omap_sham_probe(struct platform_device *pdev)
1160 struct omap_sham_dev *dd;
1161 struct device *dev = &pdev->dev;
1162 struct resource *res;
1163 dma_cap_mask_t mask;
1167 dd = kzalloc(sizeof(struct omap_sham_dev), GFP_KERNEL);
1169 dev_err(dev, "unable to alloc data struct.\n");
1174 platform_set_drvdata(pdev, dd);
1176 INIT_LIST_HEAD(&dd->list);
1177 spin_lock_init(&dd->lock);
1178 tasklet_init(&dd->done_task, omap_sham_done_task, (unsigned long)dd);
1179 crypto_init_queue(&dd->queue, OMAP_SHAM_QUEUE_LENGTH);
1183 /* Get the base address */
1184 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1186 dev_err(dev, "no MEM resource info\n");
1190 dd->phys_base = res->start;
1193 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1195 dev_err(dev, "no DMA resource info\n");
1199 dma_chan = res->start;
1202 dd->irq = platform_get_irq(pdev, 0);
1204 dev_err(dev, "no IRQ resource info\n");
1209 err = request_irq(dd->irq, omap_sham_irq,
1210 IRQF_TRIGGER_LOW, dev_name(dev), dd);
1212 dev_err(dev, "unable to request irq.\n");
1217 dma_cap_set(DMA_SLAVE, mask);
1219 dd->dma_lch = dma_request_channel(mask, omap_dma_filter_fn, &dma_chan);
1221 dev_err(dev, "unable to obtain RX DMA engine channel %u\n",
1227 dd->io_base = ioremap(dd->phys_base, SZ_4K);
1229 dev_err(dev, "can't ioremap\n");
1234 pm_runtime_enable(dev);
1235 pm_runtime_get_sync(dev);
1237 dev_info(dev, "hw accel on OMAP rev %u.%u\n",
1238 (omap_sham_read(dd, SHA_REG_REV) & SHA_REG_REV_MAJOR) >> 4,
1239 omap_sham_read(dd, SHA_REG_REV) & SHA_REG_REV_MINOR);
1241 pm_runtime_put_sync(&pdev->dev);
1243 spin_lock(&sham.lock);
1244 list_add_tail(&dd->list, &sham.dev_list);
1245 spin_unlock(&sham.lock);
1247 for (i = 0; i < ARRAY_SIZE(algs); i++) {
1248 err = crypto_register_ahash(&algs[i]);
1256 for (j = 0; j < i; j++)
1257 crypto_unregister_ahash(&algs[j]);
1258 iounmap(dd->io_base);
1259 pm_runtime_disable(dev);
1261 dma_release_channel(dd->dma_lch);
1264 free_irq(dd->irq, dd);
1269 dev_err(dev, "initialization failed.\n");
1274 static int __devexit omap_sham_remove(struct platform_device *pdev)
1276 static struct omap_sham_dev *dd;
1279 dd = platform_get_drvdata(pdev);
1282 spin_lock(&sham.lock);
1283 list_del(&dd->list);
1284 spin_unlock(&sham.lock);
1285 for (i = 0; i < ARRAY_SIZE(algs); i++)
1286 crypto_unregister_ahash(&algs[i]);
1287 tasklet_kill(&dd->done_task);
1288 iounmap(dd->io_base);
1289 pm_runtime_disable(&pdev->dev);
1290 dma_release_channel(dd->dma_lch);
1292 free_irq(dd->irq, dd);
1299 #ifdef CONFIG_PM_SLEEP
1300 static int omap_sham_suspend(struct device *dev)
1302 pm_runtime_put_sync(dev);
1306 static int omap_sham_resume(struct device *dev)
1308 pm_runtime_get_sync(dev);
1313 static const struct dev_pm_ops omap_sham_pm_ops = {
1314 SET_SYSTEM_SLEEP_PM_OPS(omap_sham_suspend, omap_sham_resume)
1317 static struct platform_driver omap_sham_driver = {
1318 .probe = omap_sham_probe,
1319 .remove = omap_sham_remove,
1321 .name = "omap-sham",
1322 .owner = THIS_MODULE,
1323 .pm = &omap_sham_pm_ops,
1327 static int __init omap_sham_mod_init(void)
1329 return platform_driver_register(&omap_sham_driver);
1332 static void __exit omap_sham_mod_exit(void)
1334 platform_driver_unregister(&omap_sham_driver);
1337 module_init(omap_sham_mod_init);
1338 module_exit(omap_sham_mod_exit);
1340 MODULE_DESCRIPTION("OMAP SHA1/MD5 hw acceleration support.");
1341 MODULE_LICENSE("GPL v2");
1342 MODULE_AUTHOR("Dmitry Kasatkin");