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/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>
42 #include <mach/irqs.h>
44 #define SHA_REG_DIGEST(x) (0x00 + ((x) * 0x04))
45 #define SHA_REG_DIN(x) (0x1C + ((x) * 0x04))
47 #define SHA1_MD5_BLOCK_SIZE SHA1_BLOCK_SIZE
48 #define MD5_DIGEST_SIZE 16
50 #define SHA_REG_DIGCNT 0x14
52 #define SHA_REG_CTRL 0x18
53 #define SHA_REG_CTRL_LENGTH (0xFFFFFFFF << 5)
54 #define SHA_REG_CTRL_CLOSE_HASH (1 << 4)
55 #define SHA_REG_CTRL_ALGO_CONST (1 << 3)
56 #define SHA_REG_CTRL_ALGO (1 << 2)
57 #define SHA_REG_CTRL_INPUT_READY (1 << 1)
58 #define SHA_REG_CTRL_OUTPUT_READY (1 << 0)
60 #define SHA_REG_REV 0x5C
61 #define SHA_REG_REV_MAJOR 0xF0
62 #define SHA_REG_REV_MINOR 0x0F
64 #define SHA_REG_MASK 0x60
65 #define SHA_REG_MASK_DMA_EN (1 << 3)
66 #define SHA_REG_MASK_IT_EN (1 << 2)
67 #define SHA_REG_MASK_SOFTRESET (1 << 1)
68 #define SHA_REG_AUTOIDLE (1 << 0)
70 #define SHA_REG_SYSSTATUS 0x64
71 #define SHA_REG_SYSSTATUS_RESETDONE (1 << 0)
73 #define DEFAULT_TIMEOUT_INTERVAL HZ
75 #define FLAGS_FIRST 0x0001
76 #define FLAGS_FINUP 0x0002
77 #define FLAGS_FINAL 0x0004
78 #define FLAGS_FAST 0x0008
79 #define FLAGS_SHA1 0x0010
80 #define FLAGS_DMA_ACTIVE 0x0020
81 #define FLAGS_OUTPUT_READY 0x0040
82 #define FLAGS_CLEAN 0x0080
83 #define FLAGS_INIT 0x0100
84 #define FLAGS_CPU 0x0200
85 #define FLAGS_HMAC 0x0400
95 struct omap_sham_reqctx
{
96 struct omap_sham_dev
*dd
;
100 u8 digest
[SHA1_DIGEST_SIZE
];
108 struct scatterlist
*sg
;
109 unsigned int offset
; /* offset in current sg */
110 unsigned int total
; /* total request */
113 struct omap_sham_hmac_ctx
{
114 struct crypto_shash
*shash
;
115 u8 ipad
[SHA1_MD5_BLOCK_SIZE
];
116 u8 opad
[SHA1_MD5_BLOCK_SIZE
];
119 struct omap_sham_ctx
{
120 struct omap_sham_dev
*dd
;
125 struct crypto_shash
*fallback
;
127 struct omap_sham_hmac_ctx base
[0];
130 #define OMAP_SHAM_QUEUE_LENGTH 1
132 struct omap_sham_dev
{
133 struct list_head list
;
134 unsigned long phys_base
;
136 void __iomem
*io_base
;
142 struct tasklet_struct done_task
;
143 struct tasklet_struct queue_task
;
146 struct crypto_queue queue
;
147 struct ahash_request
*req
;
150 struct omap_sham_drv
{
151 struct list_head dev_list
;
156 static struct omap_sham_drv sham
= {
157 .dev_list
= LIST_HEAD_INIT(sham
.dev_list
),
158 .lock
= __SPIN_LOCK_UNLOCKED(sham
.lock
),
161 static inline u32
omap_sham_read(struct omap_sham_dev
*dd
, u32 offset
)
163 return __raw_readl(dd
->io_base
+ offset
);
166 static inline void omap_sham_write(struct omap_sham_dev
*dd
,
167 u32 offset
, u32 value
)
169 __raw_writel(value
, dd
->io_base
+ offset
);
172 static inline void omap_sham_write_mask(struct omap_sham_dev
*dd
, u32 address
,
177 val
= omap_sham_read(dd
, address
);
180 omap_sham_write(dd
, address
, val
);
183 static inline int omap_sham_wait(struct omap_sham_dev
*dd
, u32 offset
, u32 bit
)
185 unsigned long timeout
= jiffies
+ DEFAULT_TIMEOUT_INTERVAL
;
187 while (!(omap_sham_read(dd
, offset
) & bit
)) {
188 if (time_is_before_jiffies(timeout
))
195 static void omap_sham_copy_hash(struct ahash_request
*req
, int out
)
197 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
198 u32
*hash
= (u32
*)ctx
->digest
;
201 if (likely(ctx
->flags
& FLAGS_SHA1
)) {
202 /* SHA1 results are in big endian */
203 for (i
= 0; i
< SHA1_DIGEST_SIZE
/ sizeof(u32
); i
++)
205 hash
[i
] = be32_to_cpu(omap_sham_read(ctx
->dd
,
208 omap_sham_write(ctx
->dd
, SHA_REG_DIGEST(i
),
209 cpu_to_be32(hash
[i
]));
211 /* MD5 results are in little endian */
212 for (i
= 0; i
< MD5_DIGEST_SIZE
/ sizeof(u32
); i
++)
214 hash
[i
] = le32_to_cpu(omap_sham_read(ctx
->dd
,
217 omap_sham_write(ctx
->dd
, SHA_REG_DIGEST(i
),
218 cpu_to_le32(hash
[i
]));
222 static int omap_sham_write_ctrl(struct omap_sham_dev
*dd
, size_t length
,
225 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
226 u32 val
= length
<< 5, mask
;
228 if (unlikely(!ctx
->digcnt
)) {
230 clk_enable(dd
->iclk
);
232 if (!(dd
->flags
& FLAGS_INIT
)) {
233 omap_sham_write_mask(dd
, SHA_REG_MASK
,
234 SHA_REG_MASK_SOFTRESET
, SHA_REG_MASK_SOFTRESET
);
236 if (omap_sham_wait(dd
, SHA_REG_SYSSTATUS
,
237 SHA_REG_SYSSTATUS_RESETDONE
))
240 dd
->flags
|= FLAGS_INIT
;
243 omap_sham_write(dd
, SHA_REG_DIGCNT
, ctx
->digcnt
);
246 omap_sham_write_mask(dd
, SHA_REG_MASK
,
247 SHA_REG_MASK_IT_EN
| (dma
? SHA_REG_MASK_DMA_EN
: 0),
248 SHA_REG_MASK_IT_EN
| SHA_REG_MASK_DMA_EN
);
250 * Setting ALGO_CONST only for the first iteration
251 * and CLOSE_HASH only for the last one.
253 if (ctx
->flags
& FLAGS_SHA1
)
254 val
|= SHA_REG_CTRL_ALGO
;
256 val
|= SHA_REG_CTRL_ALGO_CONST
;
258 val
|= SHA_REG_CTRL_CLOSE_HASH
;
260 mask
= SHA_REG_CTRL_ALGO_CONST
| SHA_REG_CTRL_CLOSE_HASH
|
261 SHA_REG_CTRL_ALGO
| SHA_REG_CTRL_LENGTH
;
263 omap_sham_write_mask(dd
, SHA_REG_CTRL
, val
, mask
);
268 static int omap_sham_xmit_cpu(struct omap_sham_dev
*dd
, const u8
*buf
,
269 size_t length
, int final
)
271 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
272 int err
, count
, len32
;
273 const u32
*buffer
= (const u32
*)buf
;
275 dev_dbg(dd
->dev
, "xmit_cpu: digcnt: %d, length: %d, final: %d\n",
276 ctx
->digcnt
, length
, final
);
278 err
= omap_sham_write_ctrl(dd
, length
, final
, 0);
282 if (omap_sham_wait(dd
, SHA_REG_CTRL
, SHA_REG_CTRL_INPUT_READY
))
285 ctx
->digcnt
+= length
;
288 ctx
->flags
|= FLAGS_FINAL
; /* catch last interrupt */
290 len32
= DIV_ROUND_UP(length
, sizeof(u32
));
292 for (count
= 0; count
< len32
; count
++)
293 omap_sham_write(dd
, SHA_REG_DIN(count
), buffer
[count
]);
298 static int omap_sham_xmit_dma(struct omap_sham_dev
*dd
, dma_addr_t dma_addr
,
299 size_t length
, int final
)
301 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
304 dev_dbg(dd
->dev
, "xmit_dma: digcnt: %d, length: %d, final: %d\n",
305 ctx
->digcnt
, length
, final
);
307 /* flush cache entries related to our page */
308 if (dma_addr
== ctx
->dma_addr
)
309 dma_sync_single_for_device(dd
->dev
, dma_addr
, length
,
312 len32
= DIV_ROUND_UP(length
, sizeof(u32
));
314 omap_set_dma_transfer_params(dd
->dma_lch
, OMAP_DMA_DATA_TYPE_S32
, len32
,
315 1, OMAP_DMA_SYNC_PACKET
, dd
->dma
,
316 OMAP_DMA_DST_SYNC_PREFETCH
);
318 omap_set_dma_src_params(dd
->dma_lch
, 0, OMAP_DMA_AMODE_POST_INC
,
321 omap_set_dma_dest_params(dd
->dma_lch
, 0,
322 OMAP_DMA_AMODE_CONSTANT
,
323 dd
->phys_base
+ SHA_REG_DIN(0), 0, 16);
325 omap_set_dma_dest_burst_mode(dd
->dma_lch
,
326 OMAP_DMA_DATA_BURST_16
);
328 omap_set_dma_src_burst_mode(dd
->dma_lch
,
329 OMAP_DMA_DATA_BURST_4
);
331 err
= omap_sham_write_ctrl(dd
, length
, final
, 1);
335 ctx
->digcnt
+= length
;
338 ctx
->flags
|= FLAGS_FINAL
; /* catch last interrupt */
340 dd
->flags
|= FLAGS_DMA_ACTIVE
;
342 omap_start_dma(dd
->dma_lch
);
347 static size_t omap_sham_append_buffer(struct omap_sham_reqctx
*ctx
,
348 const u8
*data
, size_t length
)
350 size_t count
= min(length
, ctx
->buflen
- ctx
->bufcnt
);
352 count
= min(count
, ctx
->total
);
355 memcpy(ctx
->buffer
+ ctx
->bufcnt
, data
, count
);
356 ctx
->bufcnt
+= count
;
361 static size_t omap_sham_append_sg(struct omap_sham_reqctx
*ctx
)
366 count
= omap_sham_append_buffer(ctx
,
367 sg_virt(ctx
->sg
) + ctx
->offset
,
368 ctx
->sg
->length
- ctx
->offset
);
371 ctx
->offset
+= count
;
373 if (ctx
->offset
== ctx
->sg
->length
) {
374 ctx
->sg
= sg_next(ctx
->sg
);
385 static int omap_sham_update_dma_slow(struct omap_sham_dev
*dd
)
387 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
394 omap_sham_append_sg(ctx
);
396 final
= (ctx
->flags
& FLAGS_FINUP
) && !ctx
->total
;
398 dev_dbg(dd
->dev
, "slow: bufcnt: %u, digcnt: %d, final: %d\n",
399 ctx
->bufcnt
, ctx
->digcnt
, final
);
401 if (final
|| (ctx
->bufcnt
== ctx
->buflen
&& ctx
->total
)) {
404 return omap_sham_xmit_dma(dd
, ctx
->dma_addr
, count
, final
);
410 static int omap_sham_update_dma_fast(struct omap_sham_dev
*dd
)
412 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
415 ctx
->flags
|= FLAGS_FAST
;
417 length
= min(ctx
->total
, sg_dma_len(ctx
->sg
));
420 if (!dma_map_sg(dd
->dev
, ctx
->sg
, 1, DMA_TO_DEVICE
)) {
421 dev_err(dd
->dev
, "dma_map_sg error\n");
425 ctx
->total
-= length
;
427 return omap_sham_xmit_dma(dd
, sg_dma_address(ctx
->sg
), length
, 1);
430 static int omap_sham_update_cpu(struct omap_sham_dev
*dd
)
432 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
435 omap_sham_append_sg(ctx
);
436 bufcnt
= ctx
->bufcnt
;
439 return omap_sham_xmit_cpu(dd
, ctx
->buffer
, bufcnt
, 1);
442 static int omap_sham_update_dma_stop(struct omap_sham_dev
*dd
)
444 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
446 omap_stop_dma(dd
->dma_lch
);
447 if (ctx
->flags
& FLAGS_FAST
)
448 dma_unmap_sg(dd
->dev
, ctx
->sg
, 1, DMA_TO_DEVICE
);
453 static void omap_sham_cleanup(struct ahash_request
*req
)
455 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
456 struct omap_sham_dev
*dd
= ctx
->dd
;
459 spin_lock_irqsave(&dd
->lock
, flags
);
460 if (ctx
->flags
& FLAGS_CLEAN
) {
461 spin_unlock_irqrestore(&dd
->lock
, flags
);
464 ctx
->flags
|= FLAGS_CLEAN
;
465 spin_unlock_irqrestore(&dd
->lock
, flags
);
468 clk_disable(dd
->iclk
);
469 memcpy(req
->result
, ctx
->digest
, (ctx
->flags
& FLAGS_SHA1
) ?
470 SHA1_DIGEST_SIZE
: MD5_DIGEST_SIZE
);
474 dma_unmap_single(dd
->dev
, ctx
->dma_addr
, ctx
->buflen
,
478 free_page((unsigned long)ctx
->buffer
);
480 dev_dbg(dd
->dev
, "digcnt: %d, bufcnt: %d\n", ctx
->digcnt
, ctx
->bufcnt
);
483 static int omap_sham_init(struct ahash_request
*req
)
485 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
486 struct omap_sham_ctx
*tctx
= crypto_ahash_ctx(tfm
);
487 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
488 struct omap_sham_dev
*dd
= NULL
, *tmp
;
490 spin_lock_bh(&sham
.lock
);
492 list_for_each_entry(tmp
, &sham
.dev_list
, list
) {
500 spin_unlock_bh(&sham
.lock
);
506 ctx
->flags
|= FLAGS_FIRST
;
508 dev_dbg(dd
->dev
, "init: digest size: %d\n",
509 crypto_ahash_digestsize(tfm
));
511 if (crypto_ahash_digestsize(tfm
) == SHA1_DIGEST_SIZE
)
512 ctx
->flags
|= FLAGS_SHA1
;
517 ctx
->buflen
= PAGE_SIZE
;
518 ctx
->buffer
= (void *)__get_free_page(
519 (req
->base
.flags
& CRYPTO_TFM_REQ_MAY_SLEEP
) ?
520 GFP_KERNEL
: GFP_ATOMIC
);
524 ctx
->dma_addr
= dma_map_single(dd
->dev
, ctx
->buffer
, ctx
->buflen
,
526 if (dma_mapping_error(dd
->dev
, ctx
->dma_addr
)) {
527 dev_err(dd
->dev
, "dma %u bytes error\n", ctx
->buflen
);
528 free_page((unsigned long)ctx
->buffer
);
532 if (tctx
->flags
& FLAGS_HMAC
) {
533 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
535 memcpy(ctx
->buffer
, bctx
->ipad
, SHA1_MD5_BLOCK_SIZE
);
536 ctx
->bufcnt
= SHA1_MD5_BLOCK_SIZE
;
537 ctx
->flags
|= FLAGS_HMAC
;
544 static int omap_sham_update_req(struct omap_sham_dev
*dd
)
546 struct ahash_request
*req
= dd
->req
;
547 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
550 dev_dbg(dd
->dev
, "update_req: total: %u, digcnt: %d, finup: %d\n",
551 ctx
->total
, ctx
->digcnt
, (ctx
->flags
& FLAGS_FINUP
) != 0);
553 if (ctx
->flags
& FLAGS_CPU
)
554 err
= omap_sham_update_cpu(dd
);
555 else if (ctx
->flags
& FLAGS_FAST
)
556 err
= omap_sham_update_dma_fast(dd
);
558 err
= omap_sham_update_dma_slow(dd
);
560 /* wait for dma completion before can take more data */
561 dev_dbg(dd
->dev
, "update: err: %d, digcnt: %d\n", err
, ctx
->digcnt
);
566 static int omap_sham_final_req(struct omap_sham_dev
*dd
)
568 struct ahash_request
*req
= dd
->req
;
569 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
570 int err
= 0, use_dma
= 1;
572 if (ctx
->bufcnt
<= 64)
573 /* faster to handle last block with cpu */
577 err
= omap_sham_xmit_dma(dd
, ctx
->dma_addr
, ctx
->bufcnt
, 1);
579 err
= omap_sham_xmit_cpu(dd
, ctx
->buffer
, ctx
->bufcnt
, 1);
583 if (err
!= -EINPROGRESS
)
584 omap_sham_cleanup(req
);
586 dev_dbg(dd
->dev
, "final_req: err: %d\n", err
);
591 static int omap_sham_finish_req_hmac(struct ahash_request
*req
)
593 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
594 struct omap_sham_ctx
*tctx
= crypto_tfm_ctx(req
->base
.tfm
);
595 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
596 int bs
= crypto_shash_blocksize(bctx
->shash
);
597 int ds
= crypto_shash_digestsize(bctx
->shash
);
599 struct shash_desc shash
;
600 char ctx
[crypto_shash_descsize(bctx
->shash
)];
603 desc
.shash
.tfm
= bctx
->shash
;
604 desc
.shash
.flags
= 0; /* not CRYPTO_TFM_REQ_MAY_SLEEP */
606 return crypto_shash_init(&desc
.shash
) ?:
607 crypto_shash_update(&desc
.shash
, bctx
->opad
, bs
) ?:
608 crypto_shash_finup(&desc
.shash
, ctx
->digest
, ds
, ctx
->digest
);
611 static void omap_sham_finish_req(struct ahash_request
*req
, int err
)
613 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
616 omap_sham_copy_hash(ctx
->dd
->req
, 1);
617 if (ctx
->flags
& FLAGS_HMAC
)
618 err
= omap_sham_finish_req_hmac(req
);
621 if (ctx
->flags
& FLAGS_FINAL
)
622 omap_sham_cleanup(req
);
624 clear_bit(FLAGS_BUSY
, &ctx
->dd
->flags
);
626 if (req
->base
.complete
)
627 req
->base
.complete(&req
->base
, err
);
630 static int omap_sham_handle_queue(struct omap_sham_dev
*dd
)
632 struct crypto_async_request
*async_req
, *backlog
;
633 struct omap_sham_reqctx
*ctx
;
634 struct ahash_request
*req
, *prev_req
;
638 if (test_and_set_bit(FLAGS_BUSY
, &dd
->flags
))
641 spin_lock_irqsave(&dd
->lock
, flags
);
642 backlog
= crypto_get_backlog(&dd
->queue
);
643 async_req
= crypto_dequeue_request(&dd
->queue
);
645 clear_bit(FLAGS_BUSY
, &dd
->flags
);
646 spin_unlock_irqrestore(&dd
->lock
, flags
);
652 backlog
->complete(backlog
, -EINPROGRESS
);
654 req
= ahash_request_cast(async_req
);
659 ctx
= ahash_request_ctx(req
);
661 dev_dbg(dd
->dev
, "handling new req, op: %lu, nbytes: %d\n",
662 ctx
->op
, req
->nbytes
);
664 if (req
!= prev_req
&& ctx
->digcnt
)
665 /* request has changed - restore hash */
666 omap_sham_copy_hash(req
, 0);
668 if (ctx
->op
== OP_UPDATE
) {
669 err
= omap_sham_update_req(dd
);
670 if (err
!= -EINPROGRESS
&& (ctx
->flags
& FLAGS_FINUP
))
671 /* no final() after finup() */
672 err
= omap_sham_final_req(dd
);
673 } else if (ctx
->op
== OP_FINAL
) {
674 err
= omap_sham_final_req(dd
);
677 if (err
!= -EINPROGRESS
) {
678 /* done_task will not finish it, so do it here */
679 omap_sham_finish_req(req
, err
);
680 tasklet_schedule(&dd
->queue_task
);
683 dev_dbg(dd
->dev
, "exit, err: %d\n", err
);
688 static int omap_sham_enqueue(struct ahash_request
*req
, unsigned int op
)
690 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
691 struct omap_sham_ctx
*tctx
= crypto_tfm_ctx(req
->base
.tfm
);
692 struct omap_sham_dev
*dd
= tctx
->dd
;
698 spin_lock_irqsave(&dd
->lock
, flags
);
699 err
= ahash_enqueue_request(&dd
->queue
, req
);
700 spin_unlock_irqrestore(&dd
->lock
, flags
);
702 omap_sham_handle_queue(dd
);
707 static int omap_sham_update(struct ahash_request
*req
)
709 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
714 ctx
->total
= req
->nbytes
;
718 if (ctx
->flags
& FLAGS_FINUP
) {
719 if ((ctx
->digcnt
+ ctx
->bufcnt
+ ctx
->total
) < 9) {
721 * OMAP HW accel works only with buffers >= 9
722 * will switch to bypass in final()
723 * final has the same request and data
725 omap_sham_append_sg(ctx
);
727 } else if (ctx
->bufcnt
+ ctx
->total
<= 64) {
728 ctx
->flags
|= FLAGS_CPU
;
729 } else if (!ctx
->bufcnt
&& sg_is_last(ctx
->sg
)) {
730 /* may be can use faster functions */
731 int aligned
= IS_ALIGNED((u32
)ctx
->sg
->offset
,
734 if (aligned
&& (ctx
->flags
& FLAGS_FIRST
))
735 /* digest: first and final */
736 ctx
->flags
|= FLAGS_FAST
;
738 ctx
->flags
&= ~FLAGS_FIRST
;
740 } else if (ctx
->bufcnt
+ ctx
->total
<= ctx
->buflen
) {
741 /* if not finaup -> not fast */
742 omap_sham_append_sg(ctx
);
746 return omap_sham_enqueue(req
, OP_UPDATE
);
749 static int omap_sham_shash_digest(struct crypto_shash
*shash
, u32 flags
,
750 const u8
*data
, unsigned int len
, u8
*out
)
753 struct shash_desc shash
;
754 char ctx
[crypto_shash_descsize(shash
)];
757 desc
.shash
.tfm
= shash
;
758 desc
.shash
.flags
= flags
& CRYPTO_TFM_REQ_MAY_SLEEP
;
760 return crypto_shash_digest(&desc
.shash
, data
, len
, out
);
763 static int omap_sham_final_shash(struct ahash_request
*req
)
765 struct omap_sham_ctx
*tctx
= crypto_tfm_ctx(req
->base
.tfm
);
766 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
768 return omap_sham_shash_digest(tctx
->fallback
, req
->base
.flags
,
769 ctx
->buffer
, ctx
->bufcnt
, req
->result
);
772 static int omap_sham_final(struct ahash_request
*req
)
774 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
777 ctx
->flags
|= FLAGS_FINUP
;
779 /* OMAP HW accel works only with buffers >= 9 */
780 /* HMAC is always >= 9 because of ipad */
781 if ((ctx
->digcnt
+ ctx
->bufcnt
) < 9)
782 err
= omap_sham_final_shash(req
);
783 else if (ctx
->bufcnt
)
784 return omap_sham_enqueue(req
, OP_FINAL
);
786 omap_sham_cleanup(req
);
791 static int omap_sham_finup(struct ahash_request
*req
)
793 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
796 ctx
->flags
|= FLAGS_FINUP
;
798 err1
= omap_sham_update(req
);
799 if (err1
== -EINPROGRESS
)
802 * final() has to be always called to cleanup resources
803 * even if udpate() failed, except EINPROGRESS
805 err2
= omap_sham_final(req
);
810 static int omap_sham_digest(struct ahash_request
*req
)
812 return omap_sham_init(req
) ?: omap_sham_finup(req
);
815 static int omap_sham_setkey(struct crypto_ahash
*tfm
, const u8
*key
,
818 struct omap_sham_ctx
*tctx
= crypto_ahash_ctx(tfm
);
819 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
820 int bs
= crypto_shash_blocksize(bctx
->shash
);
821 int ds
= crypto_shash_digestsize(bctx
->shash
);
823 err
= crypto_shash_setkey(tctx
->fallback
, key
, keylen
);
828 err
= omap_sham_shash_digest(bctx
->shash
,
829 crypto_shash_get_flags(bctx
->shash
),
830 key
, keylen
, bctx
->ipad
);
835 memcpy(bctx
->ipad
, key
, keylen
);
838 memset(bctx
->ipad
+ keylen
, 0, bs
- keylen
);
839 memcpy(bctx
->opad
, bctx
->ipad
, bs
);
841 for (i
= 0; i
< bs
; i
++) {
842 bctx
->ipad
[i
] ^= 0x36;
843 bctx
->opad
[i
] ^= 0x5c;
849 static int omap_sham_cra_init_alg(struct crypto_tfm
*tfm
, const char *alg_base
)
851 struct omap_sham_ctx
*tctx
= crypto_tfm_ctx(tfm
);
852 const char *alg_name
= crypto_tfm_alg_name(tfm
);
854 /* Allocate a fallback and abort if it failed. */
855 tctx
->fallback
= crypto_alloc_shash(alg_name
, 0,
856 CRYPTO_ALG_NEED_FALLBACK
);
857 if (IS_ERR(tctx
->fallback
)) {
858 pr_err("omap-sham: fallback driver '%s' "
859 "could not be loaded.\n", alg_name
);
860 return PTR_ERR(tctx
->fallback
);
863 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm
),
864 sizeof(struct omap_sham_reqctx
));
867 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
868 tctx
->flags
|= FLAGS_HMAC
;
869 bctx
->shash
= crypto_alloc_shash(alg_base
, 0,
870 CRYPTO_ALG_NEED_FALLBACK
);
871 if (IS_ERR(bctx
->shash
)) {
872 pr_err("omap-sham: base driver '%s' "
873 "could not be loaded.\n", alg_base
);
874 crypto_free_shash(tctx
->fallback
);
875 return PTR_ERR(bctx
->shash
);
883 static int omap_sham_cra_init(struct crypto_tfm
*tfm
)
885 return omap_sham_cra_init_alg(tfm
, NULL
);
888 static int omap_sham_cra_sha1_init(struct crypto_tfm
*tfm
)
890 return omap_sham_cra_init_alg(tfm
, "sha1");
893 static int omap_sham_cra_md5_init(struct crypto_tfm
*tfm
)
895 return omap_sham_cra_init_alg(tfm
, "md5");
898 static void omap_sham_cra_exit(struct crypto_tfm
*tfm
)
900 struct omap_sham_ctx
*tctx
= crypto_tfm_ctx(tfm
);
902 crypto_free_shash(tctx
->fallback
);
903 tctx
->fallback
= NULL
;
905 if (tctx
->flags
& FLAGS_HMAC
) {
906 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
907 crypto_free_shash(bctx
->shash
);
911 static struct ahash_alg algs
[] = {
913 .init
= omap_sham_init
,
914 .update
= omap_sham_update
,
915 .final
= omap_sham_final
,
916 .finup
= omap_sham_finup
,
917 .digest
= omap_sham_digest
,
918 .halg
.digestsize
= SHA1_DIGEST_SIZE
,
921 .cra_driver_name
= "omap-sha1",
923 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
|
925 CRYPTO_ALG_NEED_FALLBACK
,
926 .cra_blocksize
= SHA1_BLOCK_SIZE
,
927 .cra_ctxsize
= sizeof(struct omap_sham_ctx
),
929 .cra_module
= THIS_MODULE
,
930 .cra_init
= omap_sham_cra_init
,
931 .cra_exit
= omap_sham_cra_exit
,
935 .init
= omap_sham_init
,
936 .update
= omap_sham_update
,
937 .final
= omap_sham_final
,
938 .finup
= omap_sham_finup
,
939 .digest
= omap_sham_digest
,
940 .halg
.digestsize
= MD5_DIGEST_SIZE
,
943 .cra_driver_name
= "omap-md5",
945 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
|
947 CRYPTO_ALG_NEED_FALLBACK
,
948 .cra_blocksize
= SHA1_BLOCK_SIZE
,
949 .cra_ctxsize
= sizeof(struct omap_sham_ctx
),
951 .cra_module
= THIS_MODULE
,
952 .cra_init
= omap_sham_cra_init
,
953 .cra_exit
= omap_sham_cra_exit
,
957 .init
= omap_sham_init
,
958 .update
= omap_sham_update
,
959 .final
= omap_sham_final
,
960 .finup
= omap_sham_finup
,
961 .digest
= omap_sham_digest
,
962 .setkey
= omap_sham_setkey
,
963 .halg
.digestsize
= SHA1_DIGEST_SIZE
,
965 .cra_name
= "hmac(sha1)",
966 .cra_driver_name
= "omap-hmac-sha1",
968 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
|
970 CRYPTO_ALG_NEED_FALLBACK
,
971 .cra_blocksize
= SHA1_BLOCK_SIZE
,
972 .cra_ctxsize
= sizeof(struct omap_sham_ctx
) +
973 sizeof(struct omap_sham_hmac_ctx
),
975 .cra_module
= THIS_MODULE
,
976 .cra_init
= omap_sham_cra_sha1_init
,
977 .cra_exit
= omap_sham_cra_exit
,
981 .init
= omap_sham_init
,
982 .update
= omap_sham_update
,
983 .final
= omap_sham_final
,
984 .finup
= omap_sham_finup
,
985 .digest
= omap_sham_digest
,
986 .setkey
= omap_sham_setkey
,
987 .halg
.digestsize
= MD5_DIGEST_SIZE
,
989 .cra_name
= "hmac(md5)",
990 .cra_driver_name
= "omap-hmac-md5",
992 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
|
994 CRYPTO_ALG_NEED_FALLBACK
,
995 .cra_blocksize
= SHA1_BLOCK_SIZE
,
996 .cra_ctxsize
= sizeof(struct omap_sham_ctx
) +
997 sizeof(struct omap_sham_hmac_ctx
),
999 .cra_module
= THIS_MODULE
,
1000 .cra_init
= omap_sham_cra_md5_init
,
1001 .cra_exit
= omap_sham_cra_exit
,
1006 static void omap_sham_done_task(unsigned long data
)
1008 struct omap_sham_dev
*dd
= (struct omap_sham_dev
*)data
;
1009 struct ahash_request
*req
= dd
->req
;
1010 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
1013 if (ctx
->flags
& FLAGS_OUTPUT_READY
) {
1014 ctx
->flags
&= ~FLAGS_OUTPUT_READY
;
1018 if (dd
->flags
& FLAGS_DMA_ACTIVE
) {
1019 dd
->flags
&= ~FLAGS_DMA_ACTIVE
;
1020 omap_sham_update_dma_stop(dd
);
1021 omap_sham_update_dma_slow(dd
);
1024 if (ready
&& !(dd
->flags
& FLAGS_DMA_ACTIVE
)) {
1025 dev_dbg(dd
->dev
, "update done\n");
1026 /* finish curent request */
1027 omap_sham_finish_req(req
, 0);
1028 /* start new request */
1029 omap_sham_handle_queue(dd
);
1033 static void omap_sham_queue_task(unsigned long data
)
1035 struct omap_sham_dev
*dd
= (struct omap_sham_dev
*)data
;
1037 omap_sham_handle_queue(dd
);
1040 static irqreturn_t
omap_sham_irq(int irq
, void *dev_id
)
1042 struct omap_sham_dev
*dd
= dev_id
;
1043 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
1046 dev_err(dd
->dev
, "unknown interrupt.\n");
1050 if (unlikely(ctx
->flags
& FLAGS_FINAL
))
1051 /* final -> allow device to go to power-saving mode */
1052 omap_sham_write_mask(dd
, SHA_REG_CTRL
, 0, SHA_REG_CTRL_LENGTH
);
1054 omap_sham_write_mask(dd
, SHA_REG_CTRL
, SHA_REG_CTRL_OUTPUT_READY
,
1055 SHA_REG_CTRL_OUTPUT_READY
);
1056 omap_sham_read(dd
, SHA_REG_CTRL
);
1058 ctx
->flags
|= FLAGS_OUTPUT_READY
;
1059 tasklet_schedule(&dd
->done_task
);
1064 static void omap_sham_dma_callback(int lch
, u16 ch_status
, void *data
)
1066 struct omap_sham_dev
*dd
= data
;
1068 if (likely(lch
== dd
->dma_lch
))
1069 tasklet_schedule(&dd
->done_task
);
1072 static int omap_sham_dma_init(struct omap_sham_dev
*dd
)
1078 err
= omap_request_dma(dd
->dma
, dev_name(dd
->dev
),
1079 omap_sham_dma_callback
, dd
, &dd
->dma_lch
);
1081 dev_err(dd
->dev
, "Unable to request DMA channel\n");
1088 static void omap_sham_dma_cleanup(struct omap_sham_dev
*dd
)
1090 if (dd
->dma_lch
>= 0) {
1091 omap_free_dma(dd
->dma_lch
);
1096 static int __devinit
omap_sham_probe(struct platform_device
*pdev
)
1098 struct omap_sham_dev
*dd
;
1099 struct device
*dev
= &pdev
->dev
;
1100 struct resource
*res
;
1103 dd
= kzalloc(sizeof(struct omap_sham_dev
), GFP_KERNEL
);
1105 dev_err(dev
, "unable to alloc data struct.\n");
1110 platform_set_drvdata(pdev
, dd
);
1112 INIT_LIST_HEAD(&dd
->list
);
1113 spin_lock_init(&dd
->lock
);
1114 tasklet_init(&dd
->done_task
, omap_sham_done_task
, (unsigned long)dd
);
1115 tasklet_init(&dd
->queue_task
, omap_sham_queue_task
, (unsigned long)dd
);
1116 crypto_init_queue(&dd
->queue
, OMAP_SHAM_QUEUE_LENGTH
);
1120 /* Get the base address */
1121 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1123 dev_err(dev
, "no MEM resource info\n");
1127 dd
->phys_base
= res
->start
;
1130 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
1132 dev_err(dev
, "no DMA resource info\n");
1136 dd
->dma
= res
->start
;
1139 dd
->irq
= platform_get_irq(pdev
, 0);
1141 dev_err(dev
, "no IRQ resource info\n");
1146 err
= request_irq(dd
->irq
, omap_sham_irq
,
1147 IRQF_TRIGGER_LOW
, dev_name(dev
), dd
);
1149 dev_err(dev
, "unable to request irq.\n");
1153 err
= omap_sham_dma_init(dd
);
1157 /* Initializing the clock */
1158 dd
->iclk
= clk_get(dev
, "ick");
1160 dev_err(dev
, "clock intialization failed.\n");
1165 dd
->io_base
= ioremap(dd
->phys_base
, SZ_4K
);
1167 dev_err(dev
, "can't ioremap\n");
1172 clk_enable(dd
->iclk
);
1173 dev_info(dev
, "hw accel on OMAP rev %u.%u\n",
1174 (omap_sham_read(dd
, SHA_REG_REV
) & SHA_REG_REV_MAJOR
) >> 4,
1175 omap_sham_read(dd
, SHA_REG_REV
) & SHA_REG_REV_MINOR
);
1176 clk_disable(dd
->iclk
);
1178 spin_lock(&sham
.lock
);
1179 list_add_tail(&dd
->list
, &sham
.dev_list
);
1180 spin_unlock(&sham
.lock
);
1182 for (i
= 0; i
< ARRAY_SIZE(algs
); i
++) {
1183 err
= crypto_register_ahash(&algs
[i
]);
1191 for (j
= 0; j
< i
; j
++)
1192 crypto_unregister_ahash(&algs
[j
]);
1193 iounmap(dd
->io_base
);
1197 omap_sham_dma_cleanup(dd
);
1200 free_irq(dd
->irq
, dd
);
1205 dev_err(dev
, "initialization failed.\n");
1210 static int __devexit
omap_sham_remove(struct platform_device
*pdev
)
1212 static struct omap_sham_dev
*dd
;
1215 dd
= platform_get_drvdata(pdev
);
1218 spin_lock(&sham
.lock
);
1219 list_del(&dd
->list
);
1220 spin_unlock(&sham
.lock
);
1221 for (i
= 0; i
< ARRAY_SIZE(algs
); i
++)
1222 crypto_unregister_ahash(&algs
[i
]);
1223 tasklet_kill(&dd
->done_task
);
1224 tasklet_kill(&dd
->queue_task
);
1225 iounmap(dd
->io_base
);
1227 omap_sham_dma_cleanup(dd
);
1229 free_irq(dd
->irq
, dd
);
1236 static struct platform_driver omap_sham_driver
= {
1237 .probe
= omap_sham_probe
,
1238 .remove
= omap_sham_remove
,
1240 .name
= "omap-sham",
1241 .owner
= THIS_MODULE
,
1245 static int __init
omap_sham_mod_init(void)
1247 pr_info("loading %s driver\n", "omap-sham");
1249 if (!cpu_class_is_omap2() ||
1250 omap_type() != OMAP2_DEVICE_TYPE_SEC
) {
1251 pr_err("Unsupported cpu\n");
1255 return platform_driver_register(&omap_sham_driver
);
1258 static void __exit
omap_sham_mod_exit(void)
1260 platform_driver_unregister(&omap_sham_driver
);
1263 module_init(omap_sham_mod_init
);
1264 module_exit(omap_sham_mod_exit
);
1266 MODULE_DESCRIPTION("OMAP SHA1/MD5 hw acceleration support.");
1267 MODULE_LICENSE("GPL v2");
1268 MODULE_AUTHOR("Dmitry Kasatkin");