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>
40 #include <linux/omap-dma.h>
41 #include <mach/irqs.h>
43 #define SHA_REG_DIGEST(x) (0x00 + ((x) * 0x04))
44 #define SHA_REG_DIN(x) (0x1C + ((x) * 0x04))
46 #define SHA1_MD5_BLOCK_SIZE SHA1_BLOCK_SIZE
47 #define MD5_DIGEST_SIZE 16
49 #define SHA_REG_DIGCNT 0x14
51 #define SHA_REG_CTRL 0x18
52 #define SHA_REG_CTRL_LENGTH (0xFFFFFFFF << 5)
53 #define SHA_REG_CTRL_CLOSE_HASH (1 << 4)
54 #define SHA_REG_CTRL_ALGO_CONST (1 << 3)
55 #define SHA_REG_CTRL_ALGO (1 << 2)
56 #define SHA_REG_CTRL_INPUT_READY (1 << 1)
57 #define SHA_REG_CTRL_OUTPUT_READY (1 << 0)
59 #define SHA_REG_REV 0x5C
60 #define SHA_REG_REV_MAJOR 0xF0
61 #define SHA_REG_REV_MINOR 0x0F
63 #define SHA_REG_MASK 0x60
64 #define SHA_REG_MASK_DMA_EN (1 << 3)
65 #define SHA_REG_MASK_IT_EN (1 << 2)
66 #define SHA_REG_MASK_SOFTRESET (1 << 1)
67 #define SHA_REG_AUTOIDLE (1 << 0)
69 #define SHA_REG_SYSSTATUS 0x64
70 #define SHA_REG_SYSSTATUS_RESETDONE (1 << 0)
72 #define DEFAULT_TIMEOUT_INTERVAL HZ
74 /* mostly device flags */
77 #define FLAGS_DMA_ACTIVE 2
78 #define FLAGS_OUTPUT_READY 3
81 #define FLAGS_DMA_READY 6
83 #define FLAGS_FINUP 16
87 #define FLAGS_ERROR 20
92 #define OMAP_ALIGN_MASK (sizeof(u32)-1)
93 #define OMAP_ALIGNED __attribute__((aligned(sizeof(u32))))
95 #define BUFLEN PAGE_SIZE
99 struct omap_sham_reqctx
{
100 struct omap_sham_dev
*dd
;
104 u8 digest
[SHA1_DIGEST_SIZE
] OMAP_ALIGNED
;
111 struct scatterlist
*sg
;
112 unsigned int offset
; /* offset in current sg */
113 unsigned int total
; /* total request */
115 u8 buffer
[0] OMAP_ALIGNED
;
118 struct omap_sham_hmac_ctx
{
119 struct crypto_shash
*shash
;
120 u8 ipad
[SHA1_MD5_BLOCK_SIZE
];
121 u8 opad
[SHA1_MD5_BLOCK_SIZE
];
124 struct omap_sham_ctx
{
125 struct omap_sham_dev
*dd
;
130 struct crypto_shash
*fallback
;
132 struct omap_sham_hmac_ctx base
[0];
135 #define OMAP_SHAM_QUEUE_LENGTH 1
137 struct omap_sham_dev
{
138 struct list_head list
;
139 unsigned long phys_base
;
141 void __iomem
*io_base
;
148 struct tasklet_struct done_task
;
151 struct crypto_queue queue
;
152 struct ahash_request
*req
;
155 struct omap_sham_drv
{
156 struct list_head dev_list
;
161 static struct omap_sham_drv sham
= {
162 .dev_list
= LIST_HEAD_INIT(sham
.dev_list
),
163 .lock
= __SPIN_LOCK_UNLOCKED(sham
.lock
),
166 static inline u32
omap_sham_read(struct omap_sham_dev
*dd
, u32 offset
)
168 return __raw_readl(dd
->io_base
+ offset
);
171 static inline void omap_sham_write(struct omap_sham_dev
*dd
,
172 u32 offset
, u32 value
)
174 __raw_writel(value
, dd
->io_base
+ offset
);
177 static inline void omap_sham_write_mask(struct omap_sham_dev
*dd
, u32 address
,
182 val
= omap_sham_read(dd
, address
);
185 omap_sham_write(dd
, address
, val
);
188 static inline int omap_sham_wait(struct omap_sham_dev
*dd
, u32 offset
, u32 bit
)
190 unsigned long timeout
= jiffies
+ DEFAULT_TIMEOUT_INTERVAL
;
192 while (!(omap_sham_read(dd
, offset
) & bit
)) {
193 if (time_is_before_jiffies(timeout
))
200 static void omap_sham_copy_hash(struct ahash_request
*req
, int out
)
202 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
203 u32
*hash
= (u32
*)ctx
->digest
;
206 /* MD5 is almost unused. So copy sha1 size to reduce code */
207 for (i
= 0; i
< SHA1_DIGEST_SIZE
/ sizeof(u32
); i
++) {
209 hash
[i
] = omap_sham_read(ctx
->dd
,
212 omap_sham_write(ctx
->dd
,
213 SHA_REG_DIGEST(i
), hash
[i
]);
217 static void omap_sham_copy_ready_hash(struct ahash_request
*req
)
219 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
220 u32
*in
= (u32
*)ctx
->digest
;
221 u32
*hash
= (u32
*)req
->result
;
227 if (likely(ctx
->flags
& BIT(FLAGS_SHA1
))) {
228 /* SHA1 results are in big endian */
229 for (i
= 0; i
< SHA1_DIGEST_SIZE
/ sizeof(u32
); i
++)
230 hash
[i
] = be32_to_cpu(in
[i
]);
232 /* MD5 results are in little endian */
233 for (i
= 0; i
< MD5_DIGEST_SIZE
/ sizeof(u32
); i
++)
234 hash
[i
] = le32_to_cpu(in
[i
]);
238 static int omap_sham_hw_init(struct omap_sham_dev
*dd
)
240 clk_enable(dd
->iclk
);
242 if (!test_bit(FLAGS_INIT
, &dd
->flags
)) {
243 omap_sham_write_mask(dd
, SHA_REG_MASK
,
244 SHA_REG_MASK_SOFTRESET
, SHA_REG_MASK_SOFTRESET
);
246 if (omap_sham_wait(dd
, SHA_REG_SYSSTATUS
,
247 SHA_REG_SYSSTATUS_RESETDONE
))
250 set_bit(FLAGS_INIT
, &dd
->flags
);
257 static void omap_sham_write_ctrl(struct omap_sham_dev
*dd
, size_t length
,
260 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
261 u32 val
= length
<< 5, mask
;
263 if (likely(ctx
->digcnt
))
264 omap_sham_write(dd
, SHA_REG_DIGCNT
, ctx
->digcnt
);
266 omap_sham_write_mask(dd
, SHA_REG_MASK
,
267 SHA_REG_MASK_IT_EN
| (dma
? SHA_REG_MASK_DMA_EN
: 0),
268 SHA_REG_MASK_IT_EN
| SHA_REG_MASK_DMA_EN
);
270 * Setting ALGO_CONST only for the first iteration
271 * and CLOSE_HASH only for the last one.
273 if (ctx
->flags
& BIT(FLAGS_SHA1
))
274 val
|= SHA_REG_CTRL_ALGO
;
276 val
|= SHA_REG_CTRL_ALGO_CONST
;
278 val
|= SHA_REG_CTRL_CLOSE_HASH
;
280 mask
= SHA_REG_CTRL_ALGO_CONST
| SHA_REG_CTRL_CLOSE_HASH
|
281 SHA_REG_CTRL_ALGO
| SHA_REG_CTRL_LENGTH
;
283 omap_sham_write_mask(dd
, SHA_REG_CTRL
, val
, mask
);
286 static int omap_sham_xmit_cpu(struct omap_sham_dev
*dd
, const u8
*buf
,
287 size_t length
, int final
)
289 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
291 const u32
*buffer
= (const u32
*)buf
;
293 dev_dbg(dd
->dev
, "xmit_cpu: digcnt: %d, length: %d, final: %d\n",
294 ctx
->digcnt
, length
, final
);
296 omap_sham_write_ctrl(dd
, length
, final
, 0);
298 /* should be non-zero before next lines to disable clocks later */
299 ctx
->digcnt
+= length
;
301 if (omap_sham_wait(dd
, SHA_REG_CTRL
, SHA_REG_CTRL_INPUT_READY
))
305 set_bit(FLAGS_FINAL
, &dd
->flags
); /* catch last interrupt */
307 set_bit(FLAGS_CPU
, &dd
->flags
);
309 len32
= DIV_ROUND_UP(length
, sizeof(u32
));
311 for (count
= 0; count
< len32
; count
++)
312 omap_sham_write(dd
, SHA_REG_DIN(count
), buffer
[count
]);
317 static int omap_sham_xmit_dma(struct omap_sham_dev
*dd
, dma_addr_t dma_addr
,
318 size_t length
, int final
)
320 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
323 dev_dbg(dd
->dev
, "xmit_dma: digcnt: %d, length: %d, final: %d\n",
324 ctx
->digcnt
, length
, final
);
326 len32
= DIV_ROUND_UP(length
, sizeof(u32
));
328 omap_set_dma_transfer_params(dd
->dma_lch
, OMAP_DMA_DATA_TYPE_S32
, len32
,
329 1, OMAP_DMA_SYNC_PACKET
, dd
->dma
,
330 OMAP_DMA_DST_SYNC_PREFETCH
);
332 omap_set_dma_src_params(dd
->dma_lch
, 0, OMAP_DMA_AMODE_POST_INC
,
335 omap_sham_write_ctrl(dd
, length
, final
, 1);
337 ctx
->digcnt
+= length
;
340 set_bit(FLAGS_FINAL
, &dd
->flags
); /* catch last interrupt */
342 set_bit(FLAGS_DMA_ACTIVE
, &dd
->flags
);
344 omap_start_dma(dd
->dma_lch
);
349 static size_t omap_sham_append_buffer(struct omap_sham_reqctx
*ctx
,
350 const u8
*data
, size_t length
)
352 size_t count
= min(length
, ctx
->buflen
- ctx
->bufcnt
);
354 count
= min(count
, ctx
->total
);
357 memcpy(ctx
->buffer
+ ctx
->bufcnt
, data
, count
);
358 ctx
->bufcnt
+= count
;
363 static size_t omap_sham_append_sg(struct omap_sham_reqctx
*ctx
)
368 count
= omap_sham_append_buffer(ctx
,
369 sg_virt(ctx
->sg
) + ctx
->offset
,
370 ctx
->sg
->length
- ctx
->offset
);
373 ctx
->offset
+= count
;
375 if (ctx
->offset
== ctx
->sg
->length
) {
376 ctx
->sg
= sg_next(ctx
->sg
);
387 static int omap_sham_xmit_dma_map(struct omap_sham_dev
*dd
,
388 struct omap_sham_reqctx
*ctx
,
389 size_t length
, int final
)
391 ctx
->dma_addr
= dma_map_single(dd
->dev
, ctx
->buffer
, ctx
->buflen
,
393 if (dma_mapping_error(dd
->dev
, ctx
->dma_addr
)) {
394 dev_err(dd
->dev
, "dma %u bytes error\n", ctx
->buflen
);
398 ctx
->flags
&= ~BIT(FLAGS_SG
);
400 /* next call does not fail... so no unmap in the case of error */
401 return omap_sham_xmit_dma(dd
, ctx
->dma_addr
, length
, final
);
404 static int omap_sham_update_dma_slow(struct omap_sham_dev
*dd
)
406 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
410 omap_sham_append_sg(ctx
);
412 final
= (ctx
->flags
& BIT(FLAGS_FINUP
)) && !ctx
->total
;
414 dev_dbg(dd
->dev
, "slow: bufcnt: %u, digcnt: %d, final: %d\n",
415 ctx
->bufcnt
, ctx
->digcnt
, final
);
417 if (final
|| (ctx
->bufcnt
== ctx
->buflen
&& ctx
->total
)) {
420 return omap_sham_xmit_dma_map(dd
, ctx
, count
, final
);
426 /* Start address alignment */
427 #define SG_AA(sg) (IS_ALIGNED(sg->offset, sizeof(u32)))
428 /* SHA1 block size alignment */
429 #define SG_SA(sg) (IS_ALIGNED(sg->length, SHA1_MD5_BLOCK_SIZE))
431 static int omap_sham_update_dma_start(struct omap_sham_dev
*dd
)
433 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
434 unsigned int length
, final
, tail
;
435 struct scatterlist
*sg
;
440 if (ctx
->bufcnt
|| ctx
->offset
)
441 return omap_sham_update_dma_slow(dd
);
443 dev_dbg(dd
->dev
, "fast: digcnt: %d, bufcnt: %u, total: %u\n",
444 ctx
->digcnt
, ctx
->bufcnt
, ctx
->total
);
449 return omap_sham_update_dma_slow(dd
);
451 if (!sg_is_last(sg
) && !SG_SA(sg
))
452 /* size is not SHA1_BLOCK_SIZE aligned */
453 return omap_sham_update_dma_slow(dd
);
455 length
= min(ctx
->total
, sg
->length
);
457 if (sg_is_last(sg
)) {
458 if (!(ctx
->flags
& BIT(FLAGS_FINUP
))) {
459 /* not last sg must be SHA1_MD5_BLOCK_SIZE aligned */
460 tail
= length
& (SHA1_MD5_BLOCK_SIZE
- 1);
461 /* without finup() we need one block to close hash */
463 tail
= SHA1_MD5_BLOCK_SIZE
;
468 if (!dma_map_sg(dd
->dev
, ctx
->sg
, 1, DMA_TO_DEVICE
)) {
469 dev_err(dd
->dev
, "dma_map_sg error\n");
473 ctx
->flags
|= BIT(FLAGS_SG
);
475 ctx
->total
-= length
;
476 ctx
->offset
= length
; /* offset where to start slow */
478 final
= (ctx
->flags
& BIT(FLAGS_FINUP
)) && !ctx
->total
;
480 /* next call does not fail... so no unmap in the case of error */
481 return omap_sham_xmit_dma(dd
, sg_dma_address(ctx
->sg
), length
, final
);
484 static int omap_sham_update_cpu(struct omap_sham_dev
*dd
)
486 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
489 omap_sham_append_sg(ctx
);
490 bufcnt
= ctx
->bufcnt
;
493 return omap_sham_xmit_cpu(dd
, ctx
->buffer
, bufcnt
, 1);
496 static int omap_sham_update_dma_stop(struct omap_sham_dev
*dd
)
498 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
500 omap_stop_dma(dd
->dma_lch
);
501 if (ctx
->flags
& BIT(FLAGS_SG
)) {
502 dma_unmap_sg(dd
->dev
, ctx
->sg
, 1, DMA_TO_DEVICE
);
503 if (ctx
->sg
->length
== ctx
->offset
) {
504 ctx
->sg
= sg_next(ctx
->sg
);
509 dma_unmap_single(dd
->dev
, ctx
->dma_addr
, ctx
->buflen
,
516 static int omap_sham_init(struct ahash_request
*req
)
518 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
519 struct omap_sham_ctx
*tctx
= crypto_ahash_ctx(tfm
);
520 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
521 struct omap_sham_dev
*dd
= NULL
, *tmp
;
523 spin_lock_bh(&sham
.lock
);
525 list_for_each_entry(tmp
, &sham
.dev_list
, list
) {
533 spin_unlock_bh(&sham
.lock
);
539 dev_dbg(dd
->dev
, "init: digest size: %d\n",
540 crypto_ahash_digestsize(tfm
));
542 if (crypto_ahash_digestsize(tfm
) == SHA1_DIGEST_SIZE
)
543 ctx
->flags
|= BIT(FLAGS_SHA1
);
547 ctx
->buflen
= BUFLEN
;
549 if (tctx
->flags
& BIT(FLAGS_HMAC
)) {
550 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
552 memcpy(ctx
->buffer
, bctx
->ipad
, SHA1_MD5_BLOCK_SIZE
);
553 ctx
->bufcnt
= SHA1_MD5_BLOCK_SIZE
;
554 ctx
->flags
|= BIT(FLAGS_HMAC
);
561 static int omap_sham_update_req(struct omap_sham_dev
*dd
)
563 struct ahash_request
*req
= dd
->req
;
564 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
567 dev_dbg(dd
->dev
, "update_req: total: %u, digcnt: %d, finup: %d\n",
568 ctx
->total
, ctx
->digcnt
, (ctx
->flags
& BIT(FLAGS_FINUP
)) != 0);
570 if (ctx
->flags
& BIT(FLAGS_CPU
))
571 err
= omap_sham_update_cpu(dd
);
573 err
= omap_sham_update_dma_start(dd
);
575 /* wait for dma completion before can take more data */
576 dev_dbg(dd
->dev
, "update: err: %d, digcnt: %d\n", err
, ctx
->digcnt
);
581 static int omap_sham_final_req(struct omap_sham_dev
*dd
)
583 struct ahash_request
*req
= dd
->req
;
584 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
585 int err
= 0, use_dma
= 1;
587 if (ctx
->bufcnt
<= 64)
588 /* faster to handle last block with cpu */
592 err
= omap_sham_xmit_dma_map(dd
, ctx
, ctx
->bufcnt
, 1);
594 err
= omap_sham_xmit_cpu(dd
, ctx
->buffer
, ctx
->bufcnt
, 1);
598 dev_dbg(dd
->dev
, "final_req: err: %d\n", err
);
603 static int omap_sham_finish_hmac(struct ahash_request
*req
)
605 struct omap_sham_ctx
*tctx
= crypto_tfm_ctx(req
->base
.tfm
);
606 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
607 int bs
= crypto_shash_blocksize(bctx
->shash
);
608 int ds
= crypto_shash_digestsize(bctx
->shash
);
610 struct shash_desc shash
;
611 char ctx
[crypto_shash_descsize(bctx
->shash
)];
614 desc
.shash
.tfm
= bctx
->shash
;
615 desc
.shash
.flags
= 0; /* not CRYPTO_TFM_REQ_MAY_SLEEP */
617 return crypto_shash_init(&desc
.shash
) ?:
618 crypto_shash_update(&desc
.shash
, bctx
->opad
, bs
) ?:
619 crypto_shash_finup(&desc
.shash
, req
->result
, ds
, req
->result
);
622 static int omap_sham_finish(struct ahash_request
*req
)
624 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
625 struct omap_sham_dev
*dd
= ctx
->dd
;
629 omap_sham_copy_ready_hash(req
);
630 if (ctx
->flags
& BIT(FLAGS_HMAC
))
631 err
= omap_sham_finish_hmac(req
);
634 dev_dbg(dd
->dev
, "digcnt: %d, bufcnt: %d\n", ctx
->digcnt
, ctx
->bufcnt
);
639 static void omap_sham_finish_req(struct ahash_request
*req
, int err
)
641 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
642 struct omap_sham_dev
*dd
= ctx
->dd
;
645 omap_sham_copy_hash(req
, 1);
646 if (test_bit(FLAGS_FINAL
, &dd
->flags
))
647 err
= omap_sham_finish(req
);
649 ctx
->flags
|= BIT(FLAGS_ERROR
);
652 /* atomic operation is not needed here */
653 dd
->flags
&= ~(BIT(FLAGS_BUSY
) | BIT(FLAGS_FINAL
) | BIT(FLAGS_CPU
) |
654 BIT(FLAGS_DMA_READY
) | BIT(FLAGS_OUTPUT_READY
));
655 clk_disable(dd
->iclk
);
657 if (req
->base
.complete
)
658 req
->base
.complete(&req
->base
, err
);
660 /* handle new request */
661 tasklet_schedule(&dd
->done_task
);
664 static int omap_sham_handle_queue(struct omap_sham_dev
*dd
,
665 struct ahash_request
*req
)
667 struct crypto_async_request
*async_req
, *backlog
;
668 struct omap_sham_reqctx
*ctx
;
670 int err
= 0, ret
= 0;
672 spin_lock_irqsave(&dd
->lock
, flags
);
674 ret
= ahash_enqueue_request(&dd
->queue
, req
);
675 if (test_bit(FLAGS_BUSY
, &dd
->flags
)) {
676 spin_unlock_irqrestore(&dd
->lock
, flags
);
679 backlog
= crypto_get_backlog(&dd
->queue
);
680 async_req
= crypto_dequeue_request(&dd
->queue
);
682 set_bit(FLAGS_BUSY
, &dd
->flags
);
683 spin_unlock_irqrestore(&dd
->lock
, flags
);
689 backlog
->complete(backlog
, -EINPROGRESS
);
691 req
= ahash_request_cast(async_req
);
693 ctx
= ahash_request_ctx(req
);
695 dev_dbg(dd
->dev
, "handling new req, op: %lu, nbytes: %d\n",
696 ctx
->op
, req
->nbytes
);
698 err
= omap_sham_hw_init(dd
);
702 omap_set_dma_dest_params(dd
->dma_lch
, 0,
703 OMAP_DMA_AMODE_CONSTANT
,
704 dd
->phys_base
+ SHA_REG_DIN(0), 0, 16);
706 omap_set_dma_dest_burst_mode(dd
->dma_lch
,
707 OMAP_DMA_DATA_BURST_16
);
709 omap_set_dma_src_burst_mode(dd
->dma_lch
,
710 OMAP_DMA_DATA_BURST_4
);
713 /* request has changed - restore hash */
714 omap_sham_copy_hash(req
, 0);
716 if (ctx
->op
== OP_UPDATE
) {
717 err
= omap_sham_update_req(dd
);
718 if (err
!= -EINPROGRESS
&& (ctx
->flags
& BIT(FLAGS_FINUP
)))
719 /* no final() after finup() */
720 err
= omap_sham_final_req(dd
);
721 } else if (ctx
->op
== OP_FINAL
) {
722 err
= omap_sham_final_req(dd
);
725 if (err
!= -EINPROGRESS
)
726 /* done_task will not finish it, so do it here */
727 omap_sham_finish_req(req
, err
);
729 dev_dbg(dd
->dev
, "exit, err: %d\n", err
);
734 static int omap_sham_enqueue(struct ahash_request
*req
, unsigned int op
)
736 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
737 struct omap_sham_ctx
*tctx
= crypto_tfm_ctx(req
->base
.tfm
);
738 struct omap_sham_dev
*dd
= tctx
->dd
;
742 return omap_sham_handle_queue(dd
, req
);
745 static int omap_sham_update(struct ahash_request
*req
)
747 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
752 ctx
->total
= req
->nbytes
;
756 if (ctx
->flags
& BIT(FLAGS_FINUP
)) {
757 if ((ctx
->digcnt
+ ctx
->bufcnt
+ ctx
->total
) < 9) {
759 * OMAP HW accel works only with buffers >= 9
760 * will switch to bypass in final()
761 * final has the same request and data
763 omap_sham_append_sg(ctx
);
765 } else if (ctx
->bufcnt
+ ctx
->total
<= SHA1_MD5_BLOCK_SIZE
) {
767 * faster to use CPU for short transfers
769 ctx
->flags
|= BIT(FLAGS_CPU
);
771 } else if (ctx
->bufcnt
+ ctx
->total
< ctx
->buflen
) {
772 omap_sham_append_sg(ctx
);
776 return omap_sham_enqueue(req
, OP_UPDATE
);
779 static int omap_sham_shash_digest(struct crypto_shash
*shash
, u32 flags
,
780 const u8
*data
, unsigned int len
, u8
*out
)
783 struct shash_desc shash
;
784 char ctx
[crypto_shash_descsize(shash
)];
787 desc
.shash
.tfm
= shash
;
788 desc
.shash
.flags
= flags
& CRYPTO_TFM_REQ_MAY_SLEEP
;
790 return crypto_shash_digest(&desc
.shash
, data
, len
, out
);
793 static int omap_sham_final_shash(struct ahash_request
*req
)
795 struct omap_sham_ctx
*tctx
= crypto_tfm_ctx(req
->base
.tfm
);
796 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
798 return omap_sham_shash_digest(tctx
->fallback
, req
->base
.flags
,
799 ctx
->buffer
, ctx
->bufcnt
, req
->result
);
802 static int omap_sham_final(struct ahash_request
*req
)
804 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
806 ctx
->flags
|= BIT(FLAGS_FINUP
);
808 if (ctx
->flags
& BIT(FLAGS_ERROR
))
809 return 0; /* uncompleted hash is not needed */
811 /* OMAP HW accel works only with buffers >= 9 */
812 /* HMAC is always >= 9 because ipad == block size */
813 if ((ctx
->digcnt
+ ctx
->bufcnt
) < 9)
814 return omap_sham_final_shash(req
);
815 else if (ctx
->bufcnt
)
816 return omap_sham_enqueue(req
, OP_FINAL
);
818 /* copy ready hash (+ finalize hmac) */
819 return omap_sham_finish(req
);
822 static int omap_sham_finup(struct ahash_request
*req
)
824 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
827 ctx
->flags
|= BIT(FLAGS_FINUP
);
829 err1
= omap_sham_update(req
);
830 if (err1
== -EINPROGRESS
|| err1
== -EBUSY
)
833 * final() has to be always called to cleanup resources
834 * even if udpate() failed, except EINPROGRESS
836 err2
= omap_sham_final(req
);
841 static int omap_sham_digest(struct ahash_request
*req
)
843 return omap_sham_init(req
) ?: omap_sham_finup(req
);
846 static int omap_sham_setkey(struct crypto_ahash
*tfm
, const u8
*key
,
849 struct omap_sham_ctx
*tctx
= crypto_ahash_ctx(tfm
);
850 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
851 int bs
= crypto_shash_blocksize(bctx
->shash
);
852 int ds
= crypto_shash_digestsize(bctx
->shash
);
854 err
= crypto_shash_setkey(tctx
->fallback
, key
, keylen
);
859 err
= omap_sham_shash_digest(bctx
->shash
,
860 crypto_shash_get_flags(bctx
->shash
),
861 key
, keylen
, bctx
->ipad
);
866 memcpy(bctx
->ipad
, key
, keylen
);
869 memset(bctx
->ipad
+ keylen
, 0, bs
- keylen
);
870 memcpy(bctx
->opad
, bctx
->ipad
, bs
);
872 for (i
= 0; i
< bs
; i
++) {
873 bctx
->ipad
[i
] ^= 0x36;
874 bctx
->opad
[i
] ^= 0x5c;
880 static int omap_sham_cra_init_alg(struct crypto_tfm
*tfm
, const char *alg_base
)
882 struct omap_sham_ctx
*tctx
= crypto_tfm_ctx(tfm
);
883 const char *alg_name
= crypto_tfm_alg_name(tfm
);
885 /* Allocate a fallback and abort if it failed. */
886 tctx
->fallback
= crypto_alloc_shash(alg_name
, 0,
887 CRYPTO_ALG_NEED_FALLBACK
);
888 if (IS_ERR(tctx
->fallback
)) {
889 pr_err("omap-sham: fallback driver '%s' "
890 "could not be loaded.\n", alg_name
);
891 return PTR_ERR(tctx
->fallback
);
894 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm
),
895 sizeof(struct omap_sham_reqctx
) + BUFLEN
);
898 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
899 tctx
->flags
|= BIT(FLAGS_HMAC
);
900 bctx
->shash
= crypto_alloc_shash(alg_base
, 0,
901 CRYPTO_ALG_NEED_FALLBACK
);
902 if (IS_ERR(bctx
->shash
)) {
903 pr_err("omap-sham: base driver '%s' "
904 "could not be loaded.\n", alg_base
);
905 crypto_free_shash(tctx
->fallback
);
906 return PTR_ERR(bctx
->shash
);
914 static int omap_sham_cra_init(struct crypto_tfm
*tfm
)
916 return omap_sham_cra_init_alg(tfm
, NULL
);
919 static int omap_sham_cra_sha1_init(struct crypto_tfm
*tfm
)
921 return omap_sham_cra_init_alg(tfm
, "sha1");
924 static int omap_sham_cra_md5_init(struct crypto_tfm
*tfm
)
926 return omap_sham_cra_init_alg(tfm
, "md5");
929 static void omap_sham_cra_exit(struct crypto_tfm
*tfm
)
931 struct omap_sham_ctx
*tctx
= crypto_tfm_ctx(tfm
);
933 crypto_free_shash(tctx
->fallback
);
934 tctx
->fallback
= NULL
;
936 if (tctx
->flags
& BIT(FLAGS_HMAC
)) {
937 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
938 crypto_free_shash(bctx
->shash
);
942 static struct ahash_alg algs
[] = {
944 .init
= omap_sham_init
,
945 .update
= omap_sham_update
,
946 .final
= omap_sham_final
,
947 .finup
= omap_sham_finup
,
948 .digest
= omap_sham_digest
,
949 .halg
.digestsize
= SHA1_DIGEST_SIZE
,
952 .cra_driver_name
= "omap-sha1",
954 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
|
955 CRYPTO_ALG_KERN_DRIVER_ONLY
|
957 CRYPTO_ALG_NEED_FALLBACK
,
958 .cra_blocksize
= SHA1_BLOCK_SIZE
,
959 .cra_ctxsize
= sizeof(struct omap_sham_ctx
),
961 .cra_module
= THIS_MODULE
,
962 .cra_init
= omap_sham_cra_init
,
963 .cra_exit
= omap_sham_cra_exit
,
967 .init
= omap_sham_init
,
968 .update
= omap_sham_update
,
969 .final
= omap_sham_final
,
970 .finup
= omap_sham_finup
,
971 .digest
= omap_sham_digest
,
972 .halg
.digestsize
= MD5_DIGEST_SIZE
,
975 .cra_driver_name
= "omap-md5",
977 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
|
978 CRYPTO_ALG_KERN_DRIVER_ONLY
|
980 CRYPTO_ALG_NEED_FALLBACK
,
981 .cra_blocksize
= SHA1_BLOCK_SIZE
,
982 .cra_ctxsize
= sizeof(struct omap_sham_ctx
),
983 .cra_alignmask
= OMAP_ALIGN_MASK
,
984 .cra_module
= THIS_MODULE
,
985 .cra_init
= omap_sham_cra_init
,
986 .cra_exit
= omap_sham_cra_exit
,
990 .init
= omap_sham_init
,
991 .update
= omap_sham_update
,
992 .final
= omap_sham_final
,
993 .finup
= omap_sham_finup
,
994 .digest
= omap_sham_digest
,
995 .setkey
= omap_sham_setkey
,
996 .halg
.digestsize
= SHA1_DIGEST_SIZE
,
998 .cra_name
= "hmac(sha1)",
999 .cra_driver_name
= "omap-hmac-sha1",
1000 .cra_priority
= 100,
1001 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
|
1002 CRYPTO_ALG_KERN_DRIVER_ONLY
|
1004 CRYPTO_ALG_NEED_FALLBACK
,
1005 .cra_blocksize
= SHA1_BLOCK_SIZE
,
1006 .cra_ctxsize
= sizeof(struct omap_sham_ctx
) +
1007 sizeof(struct omap_sham_hmac_ctx
),
1008 .cra_alignmask
= OMAP_ALIGN_MASK
,
1009 .cra_module
= THIS_MODULE
,
1010 .cra_init
= omap_sham_cra_sha1_init
,
1011 .cra_exit
= omap_sham_cra_exit
,
1015 .init
= omap_sham_init
,
1016 .update
= omap_sham_update
,
1017 .final
= omap_sham_final
,
1018 .finup
= omap_sham_finup
,
1019 .digest
= omap_sham_digest
,
1020 .setkey
= omap_sham_setkey
,
1021 .halg
.digestsize
= MD5_DIGEST_SIZE
,
1023 .cra_name
= "hmac(md5)",
1024 .cra_driver_name
= "omap-hmac-md5",
1025 .cra_priority
= 100,
1026 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
|
1027 CRYPTO_ALG_KERN_DRIVER_ONLY
|
1029 CRYPTO_ALG_NEED_FALLBACK
,
1030 .cra_blocksize
= SHA1_BLOCK_SIZE
,
1031 .cra_ctxsize
= sizeof(struct omap_sham_ctx
) +
1032 sizeof(struct omap_sham_hmac_ctx
),
1033 .cra_alignmask
= OMAP_ALIGN_MASK
,
1034 .cra_module
= THIS_MODULE
,
1035 .cra_init
= omap_sham_cra_md5_init
,
1036 .cra_exit
= omap_sham_cra_exit
,
1041 static void omap_sham_done_task(unsigned long data
)
1043 struct omap_sham_dev
*dd
= (struct omap_sham_dev
*)data
;
1046 if (!test_bit(FLAGS_BUSY
, &dd
->flags
)) {
1047 omap_sham_handle_queue(dd
, NULL
);
1051 if (test_bit(FLAGS_CPU
, &dd
->flags
)) {
1052 if (test_and_clear_bit(FLAGS_OUTPUT_READY
, &dd
->flags
))
1054 } else if (test_bit(FLAGS_DMA_READY
, &dd
->flags
)) {
1055 if (test_and_clear_bit(FLAGS_DMA_ACTIVE
, &dd
->flags
)) {
1056 omap_sham_update_dma_stop(dd
);
1062 if (test_and_clear_bit(FLAGS_OUTPUT_READY
, &dd
->flags
)) {
1063 /* hash or semi-hash ready */
1064 clear_bit(FLAGS_DMA_READY
, &dd
->flags
);
1065 err
= omap_sham_update_dma_start(dd
);
1066 if (err
!= -EINPROGRESS
)
1074 dev_dbg(dd
->dev
, "update done: err: %d\n", err
);
1075 /* finish curent request */
1076 omap_sham_finish_req(dd
->req
, err
);
1079 static irqreturn_t
omap_sham_irq(int irq
, void *dev_id
)
1081 struct omap_sham_dev
*dd
= dev_id
;
1083 if (unlikely(test_bit(FLAGS_FINAL
, &dd
->flags
)))
1084 /* final -> allow device to go to power-saving mode */
1085 omap_sham_write_mask(dd
, SHA_REG_CTRL
, 0, SHA_REG_CTRL_LENGTH
);
1087 omap_sham_write_mask(dd
, SHA_REG_CTRL
, SHA_REG_CTRL_OUTPUT_READY
,
1088 SHA_REG_CTRL_OUTPUT_READY
);
1089 omap_sham_read(dd
, SHA_REG_CTRL
);
1091 if (!test_bit(FLAGS_BUSY
, &dd
->flags
)) {
1092 dev_warn(dd
->dev
, "Interrupt when no active requests.\n");
1096 set_bit(FLAGS_OUTPUT_READY
, &dd
->flags
);
1097 tasklet_schedule(&dd
->done_task
);
1102 static void omap_sham_dma_callback(int lch
, u16 ch_status
, void *data
)
1104 struct omap_sham_dev
*dd
= data
;
1106 if (ch_status
!= OMAP_DMA_BLOCK_IRQ
) {
1107 pr_err("omap-sham DMA error status: 0x%hx\n", ch_status
);
1109 clear_bit(FLAGS_INIT
, &dd
->flags
);/* request to re-initialize */
1112 set_bit(FLAGS_DMA_READY
, &dd
->flags
);
1113 tasklet_schedule(&dd
->done_task
);
1116 static int omap_sham_dma_init(struct omap_sham_dev
*dd
)
1122 err
= omap_request_dma(dd
->dma
, dev_name(dd
->dev
),
1123 omap_sham_dma_callback
, dd
, &dd
->dma_lch
);
1125 dev_err(dd
->dev
, "Unable to request DMA channel\n");
1132 static void omap_sham_dma_cleanup(struct omap_sham_dev
*dd
)
1134 if (dd
->dma_lch
>= 0) {
1135 omap_free_dma(dd
->dma_lch
);
1140 static int omap_sham_probe(struct platform_device
*pdev
)
1142 struct omap_sham_dev
*dd
;
1143 struct device
*dev
= &pdev
->dev
;
1144 struct resource
*res
;
1147 dd
= kzalloc(sizeof(struct omap_sham_dev
), GFP_KERNEL
);
1149 dev_err(dev
, "unable to alloc data struct.\n");
1154 platform_set_drvdata(pdev
, dd
);
1156 INIT_LIST_HEAD(&dd
->list
);
1157 spin_lock_init(&dd
->lock
);
1158 tasklet_init(&dd
->done_task
, omap_sham_done_task
, (unsigned long)dd
);
1159 crypto_init_queue(&dd
->queue
, OMAP_SHAM_QUEUE_LENGTH
);
1163 /* Get the base address */
1164 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1166 dev_err(dev
, "no MEM resource info\n");
1170 dd
->phys_base
= res
->start
;
1173 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
1175 dev_err(dev
, "no DMA resource info\n");
1179 dd
->dma
= res
->start
;
1182 dd
->irq
= platform_get_irq(pdev
, 0);
1184 dev_err(dev
, "no IRQ resource info\n");
1189 err
= request_irq(dd
->irq
, omap_sham_irq
,
1190 IRQF_TRIGGER_LOW
, dev_name(dev
), dd
);
1192 dev_err(dev
, "unable to request irq.\n");
1196 err
= omap_sham_dma_init(dd
);
1200 /* Initializing the clock */
1201 dd
->iclk
= clk_get(dev
, "ick");
1202 if (IS_ERR(dd
->iclk
)) {
1203 dev_err(dev
, "clock intialization failed.\n");
1204 err
= PTR_ERR(dd
->iclk
);
1208 dd
->io_base
= ioremap(dd
->phys_base
, SZ_4K
);
1210 dev_err(dev
, "can't ioremap\n");
1215 clk_enable(dd
->iclk
);
1216 dev_info(dev
, "hw accel on OMAP rev %u.%u\n",
1217 (omap_sham_read(dd
, SHA_REG_REV
) & SHA_REG_REV_MAJOR
) >> 4,
1218 omap_sham_read(dd
, SHA_REG_REV
) & SHA_REG_REV_MINOR
);
1219 clk_disable(dd
->iclk
);
1221 spin_lock(&sham
.lock
);
1222 list_add_tail(&dd
->list
, &sham
.dev_list
);
1223 spin_unlock(&sham
.lock
);
1225 for (i
= 0; i
< ARRAY_SIZE(algs
); i
++) {
1226 err
= crypto_register_ahash(&algs
[i
]);
1234 for (j
= 0; j
< i
; j
++)
1235 crypto_unregister_ahash(&algs
[j
]);
1236 iounmap(dd
->io_base
);
1240 omap_sham_dma_cleanup(dd
);
1243 free_irq(dd
->irq
, dd
);
1248 dev_err(dev
, "initialization failed.\n");
1253 static int omap_sham_remove(struct platform_device
*pdev
)
1255 static struct omap_sham_dev
*dd
;
1258 dd
= platform_get_drvdata(pdev
);
1261 spin_lock(&sham
.lock
);
1262 list_del(&dd
->list
);
1263 spin_unlock(&sham
.lock
);
1264 for (i
= 0; i
< ARRAY_SIZE(algs
); i
++)
1265 crypto_unregister_ahash(&algs
[i
]);
1266 tasklet_kill(&dd
->done_task
);
1267 iounmap(dd
->io_base
);
1269 omap_sham_dma_cleanup(dd
);
1271 free_irq(dd
->irq
, dd
);
1278 static struct platform_driver omap_sham_driver
= {
1279 .probe
= omap_sham_probe
,
1280 .remove
= omap_sham_remove
,
1282 .name
= "omap-sham",
1283 .owner
= THIS_MODULE
,
1287 static int __init
omap_sham_mod_init(void)
1289 pr_info("loading %s driver\n", "omap-sham");
1291 return platform_driver_register(&omap_sham_driver
);
1294 static void __exit
omap_sham_mod_exit(void)
1296 platform_driver_unregister(&omap_sham_driver
);
1299 module_init(omap_sham_mod_init
);
1300 module_exit(omap_sham_mod_exit
);
1302 MODULE_DESCRIPTION("OMAP SHA1/MD5 hw acceleration support.");
1303 MODULE_LICENSE("GPL v2");
1304 MODULE_AUTHOR("Dmitry Kasatkin");