1 // SPDX-License-Identifier: GPL-2.0-only
5 * Support for OMAP SHA1/MD5 HW acceleration.
7 * Copyright (c) 2010 Nokia Corporation
8 * Author: Dmitry Kasatkin <dmitry.kasatkin@nokia.com>
9 * Copyright (c) 2011 Texas Instruments Incorporated
11 * Some ideas are from old omap-sha1-md5.c driver.
14 #define pr_fmt(fmt) "%s: " fmt, __func__
16 #include <linux/err.h>
17 #include <linux/device.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/errno.h>
21 #include <linux/interrupt.h>
22 #include <linux/kernel.h>
23 #include <linux/irq.h>
25 #include <linux/platform_device.h>
26 #include <linux/scatterlist.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/dmaengine.h>
29 #include <linux/pm_runtime.h>
31 #include <linux/of_device.h>
32 #include <linux/of_address.h>
33 #include <linux/of_irq.h>
34 #include <linux/delay.h>
35 #include <linux/crypto.h>
36 #include <linux/cryptohash.h>
37 #include <crypto/scatterwalk.h>
38 #include <crypto/algapi.h>
39 #include <crypto/sha.h>
40 #include <crypto/hash.h>
41 #include <crypto/hmac.h>
42 #include <crypto/internal/hash.h>
44 #define MD5_DIGEST_SIZE 16
46 #define SHA_REG_IDIGEST(dd, x) ((dd)->pdata->idigest_ofs + ((x)*0x04))
47 #define SHA_REG_DIN(dd, x) ((dd)->pdata->din_ofs + ((x) * 0x04))
48 #define SHA_REG_DIGCNT(dd) ((dd)->pdata->digcnt_ofs)
50 #define SHA_REG_ODIGEST(dd, x) ((dd)->pdata->odigest_ofs + (x * 0x04))
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(dd) ((dd)->pdata->rev_ofs)
62 #define SHA_REG_MASK(dd) ((dd)->pdata->mask_ofs)
63 #define SHA_REG_MASK_DMA_EN (1 << 3)
64 #define SHA_REG_MASK_IT_EN (1 << 2)
65 #define SHA_REG_MASK_SOFTRESET (1 << 1)
66 #define SHA_REG_AUTOIDLE (1 << 0)
68 #define SHA_REG_SYSSTATUS(dd) ((dd)->pdata->sysstatus_ofs)
69 #define SHA_REG_SYSSTATUS_RESETDONE (1 << 0)
71 #define SHA_REG_MODE(dd) ((dd)->pdata->mode_ofs)
72 #define SHA_REG_MODE_HMAC_OUTER_HASH (1 << 7)
73 #define SHA_REG_MODE_HMAC_KEY_PROC (1 << 5)
74 #define SHA_REG_MODE_CLOSE_HASH (1 << 4)
75 #define SHA_REG_MODE_ALGO_CONSTANT (1 << 3)
77 #define SHA_REG_MODE_ALGO_MASK (7 << 0)
78 #define SHA_REG_MODE_ALGO_MD5_128 (0 << 1)
79 #define SHA_REG_MODE_ALGO_SHA1_160 (1 << 1)
80 #define SHA_REG_MODE_ALGO_SHA2_224 (2 << 1)
81 #define SHA_REG_MODE_ALGO_SHA2_256 (3 << 1)
82 #define SHA_REG_MODE_ALGO_SHA2_384 (1 << 0)
83 #define SHA_REG_MODE_ALGO_SHA2_512 (3 << 0)
85 #define SHA_REG_LENGTH(dd) ((dd)->pdata->length_ofs)
87 #define SHA_REG_IRQSTATUS 0x118
88 #define SHA_REG_IRQSTATUS_CTX_RDY (1 << 3)
89 #define SHA_REG_IRQSTATUS_PARTHASH_RDY (1 << 2)
90 #define SHA_REG_IRQSTATUS_INPUT_RDY (1 << 1)
91 #define SHA_REG_IRQSTATUS_OUTPUT_RDY (1 << 0)
93 #define SHA_REG_IRQENA 0x11C
94 #define SHA_REG_IRQENA_CTX_RDY (1 << 3)
95 #define SHA_REG_IRQENA_PARTHASH_RDY (1 << 2)
96 #define SHA_REG_IRQENA_INPUT_RDY (1 << 1)
97 #define SHA_REG_IRQENA_OUTPUT_RDY (1 << 0)
99 #define DEFAULT_TIMEOUT_INTERVAL HZ
101 #define DEFAULT_AUTOSUSPEND_DELAY 1000
103 /* mostly device flags */
105 #define FLAGS_FINAL 1
106 #define FLAGS_DMA_ACTIVE 2
107 #define FLAGS_OUTPUT_READY 3
110 #define FLAGS_DMA_READY 6
111 #define FLAGS_AUTO_XOR 7
112 #define FLAGS_BE32_SHA1 8
113 #define FLAGS_SGS_COPIED 9
114 #define FLAGS_SGS_ALLOCED 10
115 #define FLAGS_HUGE 11
118 #define FLAGS_FINUP 16
120 #define FLAGS_MODE_SHIFT 18
121 #define FLAGS_MODE_MASK (SHA_REG_MODE_ALGO_MASK << FLAGS_MODE_SHIFT)
122 #define FLAGS_MODE_MD5 (SHA_REG_MODE_ALGO_MD5_128 << FLAGS_MODE_SHIFT)
123 #define FLAGS_MODE_SHA1 (SHA_REG_MODE_ALGO_SHA1_160 << FLAGS_MODE_SHIFT)
124 #define FLAGS_MODE_SHA224 (SHA_REG_MODE_ALGO_SHA2_224 << FLAGS_MODE_SHIFT)
125 #define FLAGS_MODE_SHA256 (SHA_REG_MODE_ALGO_SHA2_256 << FLAGS_MODE_SHIFT)
126 #define FLAGS_MODE_SHA384 (SHA_REG_MODE_ALGO_SHA2_384 << FLAGS_MODE_SHIFT)
127 #define FLAGS_MODE_SHA512 (SHA_REG_MODE_ALGO_SHA2_512 << FLAGS_MODE_SHIFT)
129 #define FLAGS_HMAC 21
130 #define FLAGS_ERROR 22
135 #define OMAP_ALIGN_MASK (sizeof(u32)-1)
136 #define OMAP_ALIGNED __attribute__((aligned(sizeof(u32))))
138 #define BUFLEN SHA512_BLOCK_SIZE
139 #define OMAP_SHA_DMA_THRESHOLD 256
141 #define OMAP_SHA_MAX_DMA_LEN (1024 * 2048)
143 struct omap_sham_dev
;
145 struct omap_sham_reqctx
{
146 struct omap_sham_dev
*dd
;
150 u8 digest
[SHA512_DIGEST_SIZE
] OMAP_ALIGNED
;
156 struct scatterlist
*sg
;
157 struct scatterlist sgl
[2];
158 int offset
; /* offset in current sg */
160 unsigned int total
; /* total request */
162 u8 buffer
[] OMAP_ALIGNED
;
165 struct omap_sham_hmac_ctx
{
166 struct crypto_shash
*shash
;
167 u8 ipad
[SHA512_BLOCK_SIZE
] OMAP_ALIGNED
;
168 u8 opad
[SHA512_BLOCK_SIZE
] OMAP_ALIGNED
;
171 struct omap_sham_ctx
{
175 struct crypto_shash
*fallback
;
177 struct omap_sham_hmac_ctx base
[];
180 #define OMAP_SHAM_QUEUE_LENGTH 10
182 struct omap_sham_algs_info
{
183 struct ahash_alg
*algs_list
;
185 unsigned int registered
;
188 struct omap_sham_pdata
{
189 struct omap_sham_algs_info
*algs_info
;
190 unsigned int algs_info_size
;
194 void (*copy_hash
)(struct ahash_request
*req
, int out
);
195 void (*write_ctrl
)(struct omap_sham_dev
*dd
, size_t length
,
197 void (*trigger
)(struct omap_sham_dev
*dd
, size_t length
);
198 int (*poll_irq
)(struct omap_sham_dev
*dd
);
199 irqreturn_t (*intr_hdlr
)(int irq
, void *dev_id
);
217 struct omap_sham_dev
{
218 struct list_head list
;
219 unsigned long phys_base
;
221 void __iomem
*io_base
;
225 struct dma_chan
*dma_lch
;
226 struct tasklet_struct done_task
;
228 u8 xmit_buf
[BUFLEN
] OMAP_ALIGNED
;
232 struct crypto_queue queue
;
233 struct ahash_request
*req
;
235 const struct omap_sham_pdata
*pdata
;
238 struct omap_sham_drv
{
239 struct list_head dev_list
;
244 static struct omap_sham_drv sham
= {
245 .dev_list
= LIST_HEAD_INIT(sham
.dev_list
),
246 .lock
= __SPIN_LOCK_UNLOCKED(sham
.lock
),
249 static inline u32
omap_sham_read(struct omap_sham_dev
*dd
, u32 offset
)
251 return __raw_readl(dd
->io_base
+ offset
);
254 static inline void omap_sham_write(struct omap_sham_dev
*dd
,
255 u32 offset
, u32 value
)
257 __raw_writel(value
, dd
->io_base
+ offset
);
260 static inline void omap_sham_write_mask(struct omap_sham_dev
*dd
, u32 address
,
265 val
= omap_sham_read(dd
, address
);
268 omap_sham_write(dd
, address
, val
);
271 static inline int omap_sham_wait(struct omap_sham_dev
*dd
, u32 offset
, u32 bit
)
273 unsigned long timeout
= jiffies
+ DEFAULT_TIMEOUT_INTERVAL
;
275 while (!(omap_sham_read(dd
, offset
) & bit
)) {
276 if (time_is_before_jiffies(timeout
))
283 static void omap_sham_copy_hash_omap2(struct ahash_request
*req
, int out
)
285 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
286 struct omap_sham_dev
*dd
= ctx
->dd
;
287 u32
*hash
= (u32
*)ctx
->digest
;
290 for (i
= 0; i
< dd
->pdata
->digest_size
/ sizeof(u32
); i
++) {
292 hash
[i
] = omap_sham_read(dd
, SHA_REG_IDIGEST(dd
, i
));
294 omap_sham_write(dd
, SHA_REG_IDIGEST(dd
, i
), hash
[i
]);
298 static void omap_sham_copy_hash_omap4(struct ahash_request
*req
, int out
)
300 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
301 struct omap_sham_dev
*dd
= ctx
->dd
;
304 if (ctx
->flags
& BIT(FLAGS_HMAC
)) {
305 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(dd
->req
);
306 struct omap_sham_ctx
*tctx
= crypto_ahash_ctx(tfm
);
307 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
308 u32
*opad
= (u32
*)bctx
->opad
;
310 for (i
= 0; i
< dd
->pdata
->digest_size
/ sizeof(u32
); i
++) {
312 opad
[i
] = omap_sham_read(dd
,
313 SHA_REG_ODIGEST(dd
, i
));
315 omap_sham_write(dd
, SHA_REG_ODIGEST(dd
, i
),
320 omap_sham_copy_hash_omap2(req
, out
);
323 static void omap_sham_copy_ready_hash(struct ahash_request
*req
)
325 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
326 u32
*in
= (u32
*)ctx
->digest
;
327 u32
*hash
= (u32
*)req
->result
;
328 int i
, d
, big_endian
= 0;
333 switch (ctx
->flags
& FLAGS_MODE_MASK
) {
335 d
= MD5_DIGEST_SIZE
/ sizeof(u32
);
337 case FLAGS_MODE_SHA1
:
338 /* OMAP2 SHA1 is big endian */
339 if (test_bit(FLAGS_BE32_SHA1
, &ctx
->dd
->flags
))
341 d
= SHA1_DIGEST_SIZE
/ sizeof(u32
);
343 case FLAGS_MODE_SHA224
:
344 d
= SHA224_DIGEST_SIZE
/ sizeof(u32
);
346 case FLAGS_MODE_SHA256
:
347 d
= SHA256_DIGEST_SIZE
/ sizeof(u32
);
349 case FLAGS_MODE_SHA384
:
350 d
= SHA384_DIGEST_SIZE
/ sizeof(u32
);
352 case FLAGS_MODE_SHA512
:
353 d
= SHA512_DIGEST_SIZE
/ sizeof(u32
);
360 for (i
= 0; i
< d
; i
++)
361 hash
[i
] = be32_to_cpu(in
[i
]);
363 for (i
= 0; i
< d
; i
++)
364 hash
[i
] = le32_to_cpu(in
[i
]);
367 static int omap_sham_hw_init(struct omap_sham_dev
*dd
)
371 err
= pm_runtime_get_sync(dd
->dev
);
373 dev_err(dd
->dev
, "failed to get sync: %d\n", err
);
377 if (!test_bit(FLAGS_INIT
, &dd
->flags
)) {
378 set_bit(FLAGS_INIT
, &dd
->flags
);
385 static void omap_sham_write_ctrl_omap2(struct omap_sham_dev
*dd
, size_t length
,
388 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
389 u32 val
= length
<< 5, mask
;
391 if (likely(ctx
->digcnt
))
392 omap_sham_write(dd
, SHA_REG_DIGCNT(dd
), ctx
->digcnt
);
394 omap_sham_write_mask(dd
, SHA_REG_MASK(dd
),
395 SHA_REG_MASK_IT_EN
| (dma
? SHA_REG_MASK_DMA_EN
: 0),
396 SHA_REG_MASK_IT_EN
| SHA_REG_MASK_DMA_EN
);
398 * Setting ALGO_CONST only for the first iteration
399 * and CLOSE_HASH only for the last one.
401 if ((ctx
->flags
& FLAGS_MODE_MASK
) == FLAGS_MODE_SHA1
)
402 val
|= SHA_REG_CTRL_ALGO
;
404 val
|= SHA_REG_CTRL_ALGO_CONST
;
406 val
|= SHA_REG_CTRL_CLOSE_HASH
;
408 mask
= SHA_REG_CTRL_ALGO_CONST
| SHA_REG_CTRL_CLOSE_HASH
|
409 SHA_REG_CTRL_ALGO
| SHA_REG_CTRL_LENGTH
;
411 omap_sham_write_mask(dd
, SHA_REG_CTRL
, val
, mask
);
414 static void omap_sham_trigger_omap2(struct omap_sham_dev
*dd
, size_t length
)
418 static int omap_sham_poll_irq_omap2(struct omap_sham_dev
*dd
)
420 return omap_sham_wait(dd
, SHA_REG_CTRL
, SHA_REG_CTRL_INPUT_READY
);
423 static int get_block_size(struct omap_sham_reqctx
*ctx
)
427 switch (ctx
->flags
& FLAGS_MODE_MASK
) {
429 case FLAGS_MODE_SHA1
:
432 case FLAGS_MODE_SHA224
:
433 case FLAGS_MODE_SHA256
:
434 d
= SHA256_BLOCK_SIZE
;
436 case FLAGS_MODE_SHA384
:
437 case FLAGS_MODE_SHA512
:
438 d
= SHA512_BLOCK_SIZE
;
447 static void omap_sham_write_n(struct omap_sham_dev
*dd
, u32 offset
,
448 u32
*value
, int count
)
450 for (; count
--; value
++, offset
+= 4)
451 omap_sham_write(dd
, offset
, *value
);
454 static void omap_sham_write_ctrl_omap4(struct omap_sham_dev
*dd
, size_t length
,
457 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
461 * Setting ALGO_CONST only for the first iteration and
462 * CLOSE_HASH only for the last one. Note that flags mode bits
463 * correspond to algorithm encoding in mode register.
465 val
= (ctx
->flags
& FLAGS_MODE_MASK
) >> (FLAGS_MODE_SHIFT
);
467 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(dd
->req
);
468 struct omap_sham_ctx
*tctx
= crypto_ahash_ctx(tfm
);
469 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
472 val
|= SHA_REG_MODE_ALGO_CONSTANT
;
474 if (ctx
->flags
& BIT(FLAGS_HMAC
)) {
475 bs
= get_block_size(ctx
);
476 nr_dr
= bs
/ (2 * sizeof(u32
));
477 val
|= SHA_REG_MODE_HMAC_KEY_PROC
;
478 omap_sham_write_n(dd
, SHA_REG_ODIGEST(dd
, 0),
479 (u32
*)bctx
->ipad
, nr_dr
);
480 omap_sham_write_n(dd
, SHA_REG_IDIGEST(dd
, 0),
481 (u32
*)bctx
->ipad
+ nr_dr
, nr_dr
);
487 val
|= SHA_REG_MODE_CLOSE_HASH
;
489 if (ctx
->flags
& BIT(FLAGS_HMAC
))
490 val
|= SHA_REG_MODE_HMAC_OUTER_HASH
;
493 mask
= SHA_REG_MODE_ALGO_CONSTANT
| SHA_REG_MODE_CLOSE_HASH
|
494 SHA_REG_MODE_ALGO_MASK
| SHA_REG_MODE_HMAC_OUTER_HASH
|
495 SHA_REG_MODE_HMAC_KEY_PROC
;
497 dev_dbg(dd
->dev
, "ctrl: %08x, flags: %08lx\n", val
, ctx
->flags
);
498 omap_sham_write_mask(dd
, SHA_REG_MODE(dd
), val
, mask
);
499 omap_sham_write(dd
, SHA_REG_IRQENA
, SHA_REG_IRQENA_OUTPUT_RDY
);
500 omap_sham_write_mask(dd
, SHA_REG_MASK(dd
),
502 (dma
? SHA_REG_MASK_DMA_EN
: 0),
503 SHA_REG_MASK_IT_EN
| SHA_REG_MASK_DMA_EN
);
506 static void omap_sham_trigger_omap4(struct omap_sham_dev
*dd
, size_t length
)
508 omap_sham_write(dd
, SHA_REG_LENGTH(dd
), length
);
511 static int omap_sham_poll_irq_omap4(struct omap_sham_dev
*dd
)
513 return omap_sham_wait(dd
, SHA_REG_IRQSTATUS
,
514 SHA_REG_IRQSTATUS_INPUT_RDY
);
517 static int omap_sham_xmit_cpu(struct omap_sham_dev
*dd
, size_t length
,
520 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
521 int count
, len32
, bs32
, offset
= 0;
524 struct sg_mapping_iter mi
;
526 dev_dbg(dd
->dev
, "xmit_cpu: digcnt: %d, length: %d, final: %d\n",
527 ctx
->digcnt
, length
, final
);
529 dd
->pdata
->write_ctrl(dd
, length
, final
, 0);
530 dd
->pdata
->trigger(dd
, length
);
532 /* should be non-zero before next lines to disable clocks later */
533 ctx
->digcnt
+= length
;
534 ctx
->total
-= length
;
537 set_bit(FLAGS_FINAL
, &dd
->flags
); /* catch last interrupt */
539 set_bit(FLAGS_CPU
, &dd
->flags
);
541 len32
= DIV_ROUND_UP(length
, sizeof(u32
));
542 bs32
= get_block_size(ctx
) / sizeof(u32
);
544 sg_miter_start(&mi
, ctx
->sg
, ctx
->sg_len
,
545 SG_MITER_FROM_SG
| SG_MITER_ATOMIC
);
550 if (dd
->pdata
->poll_irq(dd
))
553 for (count
= 0; count
< min(len32
, bs32
); count
++, offset
++) {
558 pr_err("sg miter failure.\n");
564 omap_sham_write(dd
, SHA_REG_DIN(dd
, count
),
568 len32
-= min(len32
, bs32
);
576 static void omap_sham_dma_callback(void *param
)
578 struct omap_sham_dev
*dd
= param
;
580 set_bit(FLAGS_DMA_READY
, &dd
->flags
);
581 tasklet_schedule(&dd
->done_task
);
584 static int omap_sham_xmit_dma(struct omap_sham_dev
*dd
, size_t length
,
587 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
588 struct dma_async_tx_descriptor
*tx
;
589 struct dma_slave_config cfg
;
592 dev_dbg(dd
->dev
, "xmit_dma: digcnt: %d, length: %d, final: %d\n",
593 ctx
->digcnt
, length
, final
);
595 if (!dma_map_sg(dd
->dev
, ctx
->sg
, ctx
->sg_len
, DMA_TO_DEVICE
)) {
596 dev_err(dd
->dev
, "dma_map_sg error\n");
600 memset(&cfg
, 0, sizeof(cfg
));
602 cfg
.dst_addr
= dd
->phys_base
+ SHA_REG_DIN(dd
, 0);
603 cfg
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
604 cfg
.dst_maxburst
= get_block_size(ctx
) / DMA_SLAVE_BUSWIDTH_4_BYTES
;
606 ret
= dmaengine_slave_config(dd
->dma_lch
, &cfg
);
608 pr_err("omap-sham: can't configure dmaengine slave: %d\n", ret
);
612 tx
= dmaengine_prep_slave_sg(dd
->dma_lch
, ctx
->sg
, ctx
->sg_len
,
614 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
617 dev_err(dd
->dev
, "prep_slave_sg failed\n");
621 tx
->callback
= omap_sham_dma_callback
;
622 tx
->callback_param
= dd
;
624 dd
->pdata
->write_ctrl(dd
, length
, final
, 1);
626 ctx
->digcnt
+= length
;
627 ctx
->total
-= length
;
630 set_bit(FLAGS_FINAL
, &dd
->flags
); /* catch last interrupt */
632 set_bit(FLAGS_DMA_ACTIVE
, &dd
->flags
);
634 dmaengine_submit(tx
);
635 dma_async_issue_pending(dd
->dma_lch
);
637 dd
->pdata
->trigger(dd
, length
);
642 static int omap_sham_copy_sg_lists(struct omap_sham_reqctx
*ctx
,
643 struct scatterlist
*sg
, int bs
, int new_len
)
645 int n
= sg_nents(sg
);
646 struct scatterlist
*tmp
;
647 int offset
= ctx
->offset
;
649 ctx
->total
= new_len
;
654 ctx
->sg
= kmalloc_array(n
, sizeof(*sg
), GFP_KERNEL
);
658 sg_init_table(ctx
->sg
, n
);
665 sg_set_buf(tmp
, ctx
->dd
->xmit_buf
, ctx
->bufcnt
);
668 new_len
-= ctx
->bufcnt
;
671 while (sg
&& new_len
) {
672 int len
= sg
->length
- offset
;
675 offset
-= sg
->length
;
685 sg_set_page(tmp
, sg_page(sg
), len
, sg
->offset
+ offset
);
700 set_bit(FLAGS_SGS_ALLOCED
, &ctx
->dd
->flags
);
702 ctx
->offset
+= new_len
- ctx
->bufcnt
;
708 static int omap_sham_copy_sgs(struct omap_sham_reqctx
*ctx
,
709 struct scatterlist
*sg
, int bs
,
710 unsigned int new_len
)
715 pages
= get_order(new_len
);
717 buf
= (void *)__get_free_pages(GFP_ATOMIC
, pages
);
719 pr_err("Couldn't allocate pages for unaligned cases.\n");
724 memcpy(buf
, ctx
->dd
->xmit_buf
, ctx
->bufcnt
);
726 scatterwalk_map_and_copy(buf
+ ctx
->bufcnt
, sg
, ctx
->offset
,
727 min(new_len
, ctx
->total
) - ctx
->bufcnt
, 0);
728 sg_init_table(ctx
->sgl
, 1);
729 sg_set_buf(ctx
->sgl
, buf
, new_len
);
731 set_bit(FLAGS_SGS_COPIED
, &ctx
->dd
->flags
);
733 ctx
->offset
+= new_len
- ctx
->bufcnt
;
735 ctx
->total
= new_len
;
740 static int omap_sham_align_sgs(struct scatterlist
*sg
,
741 int nbytes
, int bs
, bool final
,
742 struct omap_sham_reqctx
*rctx
)
747 struct scatterlist
*sg_tmp
= sg
;
749 int offset
= rctx
->offset
;
750 int bufcnt
= rctx
->bufcnt
;
752 if (!sg
|| !sg
->length
|| !nbytes
) {
754 sg_init_table(rctx
->sgl
, 1);
755 sg_set_buf(rctx
->sgl
, rctx
->dd
->xmit_buf
, bufcnt
);
756 rctx
->sg
= rctx
->sgl
;
768 new_len
= DIV_ROUND_UP(new_len
, bs
) * bs
;
770 new_len
= (new_len
- 1) / bs
* bs
;
775 if (nbytes
!= new_len
)
778 while (nbytes
> 0 && sg_tmp
) {
782 if (!IS_ALIGNED(bufcnt
, bs
)) {
794 #ifdef CONFIG_ZONE_DMA
795 if (page_zonenum(sg_page(sg_tmp
)) != ZONE_DMA
) {
801 if (offset
< sg_tmp
->length
) {
802 if (!IS_ALIGNED(offset
+ sg_tmp
->offset
, 4)) {
807 if (!IS_ALIGNED(sg_tmp
->length
- offset
, bs
)) {
814 offset
-= sg_tmp
->length
;
820 nbytes
-= sg_tmp
->length
;
823 sg_tmp
= sg_next(sg_tmp
);
831 if (new_len
> OMAP_SHA_MAX_DMA_LEN
) {
832 new_len
= OMAP_SHA_MAX_DMA_LEN
;
837 return omap_sham_copy_sgs(rctx
, sg
, bs
, new_len
);
839 return omap_sham_copy_sg_lists(rctx
, sg
, bs
, new_len
);
841 rctx
->total
= new_len
;
842 rctx
->offset
+= new_len
;
845 sg_init_table(rctx
->sgl
, 2);
846 sg_set_buf(rctx
->sgl
, rctx
->dd
->xmit_buf
, rctx
->bufcnt
);
847 sg_chain(rctx
->sgl
, 2, sg
);
848 rctx
->sg
= rctx
->sgl
;
856 static int omap_sham_prepare_request(struct ahash_request
*req
, bool update
)
858 struct omap_sham_reqctx
*rctx
= ahash_request_ctx(req
);
862 bool final
= rctx
->flags
& BIT(FLAGS_FINUP
);
865 bs
= get_block_size(rctx
);
867 nbytes
= rctx
->bufcnt
;
870 nbytes
+= req
->nbytes
- rctx
->offset
;
872 dev_dbg(rctx
->dd
->dev
,
873 "%s: nbytes=%d, bs=%d, total=%d, offset=%d, bufcnt=%d\n",
874 __func__
, nbytes
, bs
, rctx
->total
, rctx
->offset
,
880 rctx
->total
= nbytes
;
882 if (update
&& req
->nbytes
&& (!IS_ALIGNED(rctx
->bufcnt
, bs
))) {
883 int len
= bs
- rctx
->bufcnt
% bs
;
885 if (len
> req
->nbytes
)
887 scatterwalk_map_and_copy(rctx
->buffer
+ rctx
->bufcnt
, req
->src
,
894 memcpy(rctx
->dd
->xmit_buf
, rctx
->buffer
, rctx
->bufcnt
);
896 ret
= omap_sham_align_sgs(req
->src
, nbytes
, bs
, final
, rctx
);
900 hash_later
= nbytes
- rctx
->total
;
904 if (hash_later
&& hash_later
<= rctx
->buflen
) {
905 scatterwalk_map_and_copy(rctx
->buffer
,
907 req
->nbytes
- hash_later
,
910 rctx
->bufcnt
= hash_later
;
915 if (hash_later
> rctx
->buflen
)
916 set_bit(FLAGS_HUGE
, &rctx
->dd
->flags
);
918 rctx
->total
= min(nbytes
, rctx
->total
);
923 static int omap_sham_update_dma_stop(struct omap_sham_dev
*dd
)
925 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
927 dma_unmap_sg(dd
->dev
, ctx
->sg
, ctx
->sg_len
, DMA_TO_DEVICE
);
929 clear_bit(FLAGS_DMA_ACTIVE
, &dd
->flags
);
934 struct omap_sham_dev
*omap_sham_find_dev(struct omap_sham_reqctx
*ctx
)
936 struct omap_sham_dev
*dd
;
941 spin_lock_bh(&sham
.lock
);
942 dd
= list_first_entry(&sham
.dev_list
, struct omap_sham_dev
, list
);
943 list_move_tail(&dd
->list
, &sham
.dev_list
);
945 spin_unlock_bh(&sham
.lock
);
950 static int omap_sham_init(struct ahash_request
*req
)
952 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
953 struct omap_sham_ctx
*tctx
= crypto_ahash_ctx(tfm
);
954 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
955 struct omap_sham_dev
*dd
;
960 dd
= omap_sham_find_dev(ctx
);
966 dev_dbg(dd
->dev
, "init: digest size: %d\n",
967 crypto_ahash_digestsize(tfm
));
969 switch (crypto_ahash_digestsize(tfm
)) {
970 case MD5_DIGEST_SIZE
:
971 ctx
->flags
|= FLAGS_MODE_MD5
;
972 bs
= SHA1_BLOCK_SIZE
;
974 case SHA1_DIGEST_SIZE
:
975 ctx
->flags
|= FLAGS_MODE_SHA1
;
976 bs
= SHA1_BLOCK_SIZE
;
978 case SHA224_DIGEST_SIZE
:
979 ctx
->flags
|= FLAGS_MODE_SHA224
;
980 bs
= SHA224_BLOCK_SIZE
;
982 case SHA256_DIGEST_SIZE
:
983 ctx
->flags
|= FLAGS_MODE_SHA256
;
984 bs
= SHA256_BLOCK_SIZE
;
986 case SHA384_DIGEST_SIZE
:
987 ctx
->flags
|= FLAGS_MODE_SHA384
;
988 bs
= SHA384_BLOCK_SIZE
;
990 case SHA512_DIGEST_SIZE
:
991 ctx
->flags
|= FLAGS_MODE_SHA512
;
992 bs
= SHA512_BLOCK_SIZE
;
1000 ctx
->buflen
= BUFLEN
;
1002 if (tctx
->flags
& BIT(FLAGS_HMAC
)) {
1003 if (!test_bit(FLAGS_AUTO_XOR
, &dd
->flags
)) {
1004 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
1006 memcpy(ctx
->buffer
, bctx
->ipad
, bs
);
1010 ctx
->flags
|= BIT(FLAGS_HMAC
);
1017 static int omap_sham_update_req(struct omap_sham_dev
*dd
)
1019 struct ahash_request
*req
= dd
->req
;
1020 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
1022 bool final
= (ctx
->flags
& BIT(FLAGS_FINUP
)) &&
1023 !(dd
->flags
& BIT(FLAGS_HUGE
));
1025 dev_dbg(dd
->dev
, "update_req: total: %u, digcnt: %d, final: %d",
1026 ctx
->total
, ctx
->digcnt
, final
);
1028 if (ctx
->total
< get_block_size(ctx
) ||
1029 ctx
->total
< dd
->fallback_sz
)
1030 ctx
->flags
|= BIT(FLAGS_CPU
);
1032 if (ctx
->flags
& BIT(FLAGS_CPU
))
1033 err
= omap_sham_xmit_cpu(dd
, ctx
->total
, final
);
1035 err
= omap_sham_xmit_dma(dd
, ctx
->total
, final
);
1037 /* wait for dma completion before can take more data */
1038 dev_dbg(dd
->dev
, "update: err: %d, digcnt: %d\n", err
, ctx
->digcnt
);
1043 static int omap_sham_final_req(struct omap_sham_dev
*dd
)
1045 struct ahash_request
*req
= dd
->req
;
1046 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
1047 int err
= 0, use_dma
= 1;
1049 if (dd
->flags
& BIT(FLAGS_HUGE
))
1052 if ((ctx
->total
<= get_block_size(ctx
)) || dd
->polling_mode
)
1054 * faster to handle last block with cpu or
1055 * use cpu when dma is not present.
1060 err
= omap_sham_xmit_dma(dd
, ctx
->total
, 1);
1062 err
= omap_sham_xmit_cpu(dd
, ctx
->total
, 1);
1066 dev_dbg(dd
->dev
, "final_req: err: %d\n", err
);
1071 static int omap_sham_finish_hmac(struct ahash_request
*req
)
1073 struct omap_sham_ctx
*tctx
= crypto_tfm_ctx(req
->base
.tfm
);
1074 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
1075 int bs
= crypto_shash_blocksize(bctx
->shash
);
1076 int ds
= crypto_shash_digestsize(bctx
->shash
);
1077 SHASH_DESC_ON_STACK(shash
, bctx
->shash
);
1079 shash
->tfm
= bctx
->shash
;
1081 return crypto_shash_init(shash
) ?:
1082 crypto_shash_update(shash
, bctx
->opad
, bs
) ?:
1083 crypto_shash_finup(shash
, req
->result
, ds
, req
->result
);
1086 static int omap_sham_finish(struct ahash_request
*req
)
1088 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
1089 struct omap_sham_dev
*dd
= ctx
->dd
;
1093 omap_sham_copy_ready_hash(req
);
1094 if ((ctx
->flags
& BIT(FLAGS_HMAC
)) &&
1095 !test_bit(FLAGS_AUTO_XOR
, &dd
->flags
))
1096 err
= omap_sham_finish_hmac(req
);
1099 dev_dbg(dd
->dev
, "digcnt: %d, bufcnt: %d\n", ctx
->digcnt
, ctx
->bufcnt
);
1104 static void omap_sham_finish_req(struct ahash_request
*req
, int err
)
1106 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
1107 struct omap_sham_dev
*dd
= ctx
->dd
;
1109 if (test_bit(FLAGS_SGS_COPIED
, &dd
->flags
))
1110 free_pages((unsigned long)sg_virt(ctx
->sg
),
1111 get_order(ctx
->sg
->length
));
1113 if (test_bit(FLAGS_SGS_ALLOCED
, &dd
->flags
))
1118 dd
->flags
&= ~(BIT(FLAGS_SGS_ALLOCED
) | BIT(FLAGS_SGS_COPIED
));
1120 if (dd
->flags
& BIT(FLAGS_HUGE
)) {
1121 dd
->flags
&= ~(BIT(FLAGS_CPU
) | BIT(FLAGS_DMA_READY
) |
1122 BIT(FLAGS_OUTPUT_READY
) | BIT(FLAGS_HUGE
));
1123 omap_sham_prepare_request(req
, ctx
->op
== OP_UPDATE
);
1124 if (ctx
->op
== OP_UPDATE
|| (dd
->flags
& BIT(FLAGS_HUGE
))) {
1125 err
= omap_sham_update_req(dd
);
1126 if (err
!= -EINPROGRESS
&&
1127 (ctx
->flags
& BIT(FLAGS_FINUP
)))
1128 err
= omap_sham_final_req(dd
);
1129 } else if (ctx
->op
== OP_FINAL
) {
1130 omap_sham_final_req(dd
);
1136 dd
->pdata
->copy_hash(req
, 1);
1137 if (test_bit(FLAGS_FINAL
, &dd
->flags
))
1138 err
= omap_sham_finish(req
);
1140 ctx
->flags
|= BIT(FLAGS_ERROR
);
1143 /* atomic operation is not needed here */
1144 dd
->flags
&= ~(BIT(FLAGS_BUSY
) | BIT(FLAGS_FINAL
) | BIT(FLAGS_CPU
) |
1145 BIT(FLAGS_DMA_READY
) | BIT(FLAGS_OUTPUT_READY
));
1147 pm_runtime_mark_last_busy(dd
->dev
);
1148 pm_runtime_put_autosuspend(dd
->dev
);
1152 if (req
->base
.complete
)
1153 req
->base
.complete(&req
->base
, err
);
1156 static int omap_sham_handle_queue(struct omap_sham_dev
*dd
,
1157 struct ahash_request
*req
)
1159 struct crypto_async_request
*async_req
, *backlog
;
1160 struct omap_sham_reqctx
*ctx
;
1161 unsigned long flags
;
1162 int err
= 0, ret
= 0;
1165 spin_lock_irqsave(&dd
->lock
, flags
);
1167 ret
= ahash_enqueue_request(&dd
->queue
, req
);
1168 if (test_bit(FLAGS_BUSY
, &dd
->flags
)) {
1169 spin_unlock_irqrestore(&dd
->lock
, flags
);
1172 backlog
= crypto_get_backlog(&dd
->queue
);
1173 async_req
= crypto_dequeue_request(&dd
->queue
);
1175 set_bit(FLAGS_BUSY
, &dd
->flags
);
1176 spin_unlock_irqrestore(&dd
->lock
, flags
);
1182 backlog
->complete(backlog
, -EINPROGRESS
);
1184 req
= ahash_request_cast(async_req
);
1186 ctx
= ahash_request_ctx(req
);
1188 err
= omap_sham_prepare_request(req
, ctx
->op
== OP_UPDATE
);
1189 if (err
|| !ctx
->total
)
1192 dev_dbg(dd
->dev
, "handling new req, op: %lu, nbytes: %d\n",
1193 ctx
->op
, req
->nbytes
);
1195 err
= omap_sham_hw_init(dd
);
1200 /* request has changed - restore hash */
1201 dd
->pdata
->copy_hash(req
, 0);
1203 if (ctx
->op
== OP_UPDATE
|| (dd
->flags
& BIT(FLAGS_HUGE
))) {
1204 err
= omap_sham_update_req(dd
);
1205 if (err
!= -EINPROGRESS
&& (ctx
->flags
& BIT(FLAGS_FINUP
)))
1206 /* no final() after finup() */
1207 err
= omap_sham_final_req(dd
);
1208 } else if (ctx
->op
== OP_FINAL
) {
1209 err
= omap_sham_final_req(dd
);
1212 dev_dbg(dd
->dev
, "exit, err: %d\n", err
);
1214 if (err
!= -EINPROGRESS
) {
1215 /* done_task will not finish it, so do it here */
1216 omap_sham_finish_req(req
, err
);
1220 * Execute next request immediately if there is anything
1229 static int omap_sham_enqueue(struct ahash_request
*req
, unsigned int op
)
1231 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
1232 struct omap_sham_dev
*dd
= ctx
->dd
;
1236 return omap_sham_handle_queue(dd
, req
);
1239 static int omap_sham_update(struct ahash_request
*req
)
1241 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
1242 struct omap_sham_dev
*dd
= omap_sham_find_dev(ctx
);
1247 if (ctx
->bufcnt
+ req
->nbytes
<= ctx
->buflen
) {
1248 scatterwalk_map_and_copy(ctx
->buffer
+ ctx
->bufcnt
, req
->src
,
1250 ctx
->bufcnt
+= req
->nbytes
;
1254 if (dd
->polling_mode
)
1255 ctx
->flags
|= BIT(FLAGS_CPU
);
1257 return omap_sham_enqueue(req
, OP_UPDATE
);
1260 static int omap_sham_shash_digest(struct crypto_shash
*tfm
, u32 flags
,
1261 const u8
*data
, unsigned int len
, u8
*out
)
1263 SHASH_DESC_ON_STACK(shash
, tfm
);
1267 return crypto_shash_digest(shash
, data
, len
, out
);
1270 static int omap_sham_final_shash(struct ahash_request
*req
)
1272 struct omap_sham_ctx
*tctx
= crypto_tfm_ctx(req
->base
.tfm
);
1273 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
1277 * If we are running HMAC on limited hardware support, skip
1278 * the ipad in the beginning of the buffer if we are going for
1279 * software fallback algorithm.
1281 if (test_bit(FLAGS_HMAC
, &ctx
->flags
) &&
1282 !test_bit(FLAGS_AUTO_XOR
, &ctx
->dd
->flags
))
1283 offset
= get_block_size(ctx
);
1285 return omap_sham_shash_digest(tctx
->fallback
, req
->base
.flags
,
1286 ctx
->buffer
+ offset
,
1287 ctx
->bufcnt
- offset
, req
->result
);
1290 static int omap_sham_final(struct ahash_request
*req
)
1292 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
1294 ctx
->flags
|= BIT(FLAGS_FINUP
);
1296 if (ctx
->flags
& BIT(FLAGS_ERROR
))
1297 return 0; /* uncompleted hash is not needed */
1300 * OMAP HW accel works only with buffers >= 9.
1301 * HMAC is always >= 9 because ipad == block size.
1302 * If buffersize is less than fallback_sz, we use fallback
1303 * SW encoding, as using DMA + HW in this case doesn't provide
1306 if (!ctx
->digcnt
&& ctx
->bufcnt
< ctx
->dd
->fallback_sz
)
1307 return omap_sham_final_shash(req
);
1308 else if (ctx
->bufcnt
)
1309 return omap_sham_enqueue(req
, OP_FINAL
);
1311 /* copy ready hash (+ finalize hmac) */
1312 return omap_sham_finish(req
);
1315 static int omap_sham_finup(struct ahash_request
*req
)
1317 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
1320 ctx
->flags
|= BIT(FLAGS_FINUP
);
1322 err1
= omap_sham_update(req
);
1323 if (err1
== -EINPROGRESS
|| err1
== -EBUSY
)
1326 * final() has to be always called to cleanup resources
1327 * even if udpate() failed, except EINPROGRESS
1329 err2
= omap_sham_final(req
);
1331 return err1
?: err2
;
1334 static int omap_sham_digest(struct ahash_request
*req
)
1336 return omap_sham_init(req
) ?: omap_sham_finup(req
);
1339 static int omap_sham_setkey(struct crypto_ahash
*tfm
, const u8
*key
,
1340 unsigned int keylen
)
1342 struct omap_sham_ctx
*tctx
= crypto_ahash_ctx(tfm
);
1343 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
1344 int bs
= crypto_shash_blocksize(bctx
->shash
);
1345 int ds
= crypto_shash_digestsize(bctx
->shash
);
1348 err
= crypto_shash_setkey(tctx
->fallback
, key
, keylen
);
1353 err
= omap_sham_shash_digest(bctx
->shash
,
1354 crypto_shash_get_flags(bctx
->shash
),
1355 key
, keylen
, bctx
->ipad
);
1360 memcpy(bctx
->ipad
, key
, keylen
);
1363 memset(bctx
->ipad
+ keylen
, 0, bs
- keylen
);
1365 if (!test_bit(FLAGS_AUTO_XOR
, &sham
.flags
)) {
1366 memcpy(bctx
->opad
, bctx
->ipad
, bs
);
1368 for (i
= 0; i
< bs
; i
++) {
1369 bctx
->ipad
[i
] ^= HMAC_IPAD_VALUE
;
1370 bctx
->opad
[i
] ^= HMAC_OPAD_VALUE
;
1377 static int omap_sham_cra_init_alg(struct crypto_tfm
*tfm
, const char *alg_base
)
1379 struct omap_sham_ctx
*tctx
= crypto_tfm_ctx(tfm
);
1380 const char *alg_name
= crypto_tfm_alg_name(tfm
);
1382 /* Allocate a fallback and abort if it failed. */
1383 tctx
->fallback
= crypto_alloc_shash(alg_name
, 0,
1384 CRYPTO_ALG_NEED_FALLBACK
);
1385 if (IS_ERR(tctx
->fallback
)) {
1386 pr_err("omap-sham: fallback driver '%s' "
1387 "could not be loaded.\n", alg_name
);
1388 return PTR_ERR(tctx
->fallback
);
1391 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm
),
1392 sizeof(struct omap_sham_reqctx
) + BUFLEN
);
1395 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
1396 tctx
->flags
|= BIT(FLAGS_HMAC
);
1397 bctx
->shash
= crypto_alloc_shash(alg_base
, 0,
1398 CRYPTO_ALG_NEED_FALLBACK
);
1399 if (IS_ERR(bctx
->shash
)) {
1400 pr_err("omap-sham: base driver '%s' "
1401 "could not be loaded.\n", alg_base
);
1402 crypto_free_shash(tctx
->fallback
);
1403 return PTR_ERR(bctx
->shash
);
1411 static int omap_sham_cra_init(struct crypto_tfm
*tfm
)
1413 return omap_sham_cra_init_alg(tfm
, NULL
);
1416 static int omap_sham_cra_sha1_init(struct crypto_tfm
*tfm
)
1418 return omap_sham_cra_init_alg(tfm
, "sha1");
1421 static int omap_sham_cra_sha224_init(struct crypto_tfm
*tfm
)
1423 return omap_sham_cra_init_alg(tfm
, "sha224");
1426 static int omap_sham_cra_sha256_init(struct crypto_tfm
*tfm
)
1428 return omap_sham_cra_init_alg(tfm
, "sha256");
1431 static int omap_sham_cra_md5_init(struct crypto_tfm
*tfm
)
1433 return omap_sham_cra_init_alg(tfm
, "md5");
1436 static int omap_sham_cra_sha384_init(struct crypto_tfm
*tfm
)
1438 return omap_sham_cra_init_alg(tfm
, "sha384");
1441 static int omap_sham_cra_sha512_init(struct crypto_tfm
*tfm
)
1443 return omap_sham_cra_init_alg(tfm
, "sha512");
1446 static void omap_sham_cra_exit(struct crypto_tfm
*tfm
)
1448 struct omap_sham_ctx
*tctx
= crypto_tfm_ctx(tfm
);
1450 crypto_free_shash(tctx
->fallback
);
1451 tctx
->fallback
= NULL
;
1453 if (tctx
->flags
& BIT(FLAGS_HMAC
)) {
1454 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
1455 crypto_free_shash(bctx
->shash
);
1459 static int omap_sham_export(struct ahash_request
*req
, void *out
)
1461 struct omap_sham_reqctx
*rctx
= ahash_request_ctx(req
);
1463 memcpy(out
, rctx
, sizeof(*rctx
) + rctx
->bufcnt
);
1468 static int omap_sham_import(struct ahash_request
*req
, const void *in
)
1470 struct omap_sham_reqctx
*rctx
= ahash_request_ctx(req
);
1471 const struct omap_sham_reqctx
*ctx_in
= in
;
1473 memcpy(rctx
, in
, sizeof(*rctx
) + ctx_in
->bufcnt
);
1478 static struct ahash_alg algs_sha1_md5
[] = {
1480 .init
= omap_sham_init
,
1481 .update
= omap_sham_update
,
1482 .final
= omap_sham_final
,
1483 .finup
= omap_sham_finup
,
1484 .digest
= omap_sham_digest
,
1485 .halg
.digestsize
= SHA1_DIGEST_SIZE
,
1488 .cra_driver_name
= "omap-sha1",
1489 .cra_priority
= 400,
1490 .cra_flags
= CRYPTO_ALG_KERN_DRIVER_ONLY
|
1492 CRYPTO_ALG_NEED_FALLBACK
,
1493 .cra_blocksize
= SHA1_BLOCK_SIZE
,
1494 .cra_ctxsize
= sizeof(struct omap_sham_ctx
),
1495 .cra_alignmask
= OMAP_ALIGN_MASK
,
1496 .cra_module
= THIS_MODULE
,
1497 .cra_init
= omap_sham_cra_init
,
1498 .cra_exit
= omap_sham_cra_exit
,
1502 .init
= omap_sham_init
,
1503 .update
= omap_sham_update
,
1504 .final
= omap_sham_final
,
1505 .finup
= omap_sham_finup
,
1506 .digest
= omap_sham_digest
,
1507 .halg
.digestsize
= MD5_DIGEST_SIZE
,
1510 .cra_driver_name
= "omap-md5",
1511 .cra_priority
= 400,
1512 .cra_flags
= CRYPTO_ALG_KERN_DRIVER_ONLY
|
1514 CRYPTO_ALG_NEED_FALLBACK
,
1515 .cra_blocksize
= SHA1_BLOCK_SIZE
,
1516 .cra_ctxsize
= sizeof(struct omap_sham_ctx
),
1517 .cra_alignmask
= OMAP_ALIGN_MASK
,
1518 .cra_module
= THIS_MODULE
,
1519 .cra_init
= omap_sham_cra_init
,
1520 .cra_exit
= omap_sham_cra_exit
,
1524 .init
= omap_sham_init
,
1525 .update
= omap_sham_update
,
1526 .final
= omap_sham_final
,
1527 .finup
= omap_sham_finup
,
1528 .digest
= omap_sham_digest
,
1529 .setkey
= omap_sham_setkey
,
1530 .halg
.digestsize
= SHA1_DIGEST_SIZE
,
1532 .cra_name
= "hmac(sha1)",
1533 .cra_driver_name
= "omap-hmac-sha1",
1534 .cra_priority
= 400,
1535 .cra_flags
= CRYPTO_ALG_KERN_DRIVER_ONLY
|
1537 CRYPTO_ALG_NEED_FALLBACK
,
1538 .cra_blocksize
= SHA1_BLOCK_SIZE
,
1539 .cra_ctxsize
= sizeof(struct omap_sham_ctx
) +
1540 sizeof(struct omap_sham_hmac_ctx
),
1541 .cra_alignmask
= OMAP_ALIGN_MASK
,
1542 .cra_module
= THIS_MODULE
,
1543 .cra_init
= omap_sham_cra_sha1_init
,
1544 .cra_exit
= omap_sham_cra_exit
,
1548 .init
= omap_sham_init
,
1549 .update
= omap_sham_update
,
1550 .final
= omap_sham_final
,
1551 .finup
= omap_sham_finup
,
1552 .digest
= omap_sham_digest
,
1553 .setkey
= omap_sham_setkey
,
1554 .halg
.digestsize
= MD5_DIGEST_SIZE
,
1556 .cra_name
= "hmac(md5)",
1557 .cra_driver_name
= "omap-hmac-md5",
1558 .cra_priority
= 400,
1559 .cra_flags
= CRYPTO_ALG_KERN_DRIVER_ONLY
|
1561 CRYPTO_ALG_NEED_FALLBACK
,
1562 .cra_blocksize
= SHA1_BLOCK_SIZE
,
1563 .cra_ctxsize
= sizeof(struct omap_sham_ctx
) +
1564 sizeof(struct omap_sham_hmac_ctx
),
1565 .cra_alignmask
= OMAP_ALIGN_MASK
,
1566 .cra_module
= THIS_MODULE
,
1567 .cra_init
= omap_sham_cra_md5_init
,
1568 .cra_exit
= omap_sham_cra_exit
,
1573 /* OMAP4 has some algs in addition to what OMAP2 has */
1574 static struct ahash_alg algs_sha224_sha256
[] = {
1576 .init
= omap_sham_init
,
1577 .update
= omap_sham_update
,
1578 .final
= omap_sham_final
,
1579 .finup
= omap_sham_finup
,
1580 .digest
= omap_sham_digest
,
1581 .halg
.digestsize
= SHA224_DIGEST_SIZE
,
1583 .cra_name
= "sha224",
1584 .cra_driver_name
= "omap-sha224",
1585 .cra_priority
= 400,
1586 .cra_flags
= CRYPTO_ALG_ASYNC
|
1587 CRYPTO_ALG_NEED_FALLBACK
,
1588 .cra_blocksize
= SHA224_BLOCK_SIZE
,
1589 .cra_ctxsize
= sizeof(struct omap_sham_ctx
),
1590 .cra_alignmask
= OMAP_ALIGN_MASK
,
1591 .cra_module
= THIS_MODULE
,
1592 .cra_init
= omap_sham_cra_init
,
1593 .cra_exit
= omap_sham_cra_exit
,
1597 .init
= omap_sham_init
,
1598 .update
= omap_sham_update
,
1599 .final
= omap_sham_final
,
1600 .finup
= omap_sham_finup
,
1601 .digest
= omap_sham_digest
,
1602 .halg
.digestsize
= SHA256_DIGEST_SIZE
,
1604 .cra_name
= "sha256",
1605 .cra_driver_name
= "omap-sha256",
1606 .cra_priority
= 400,
1607 .cra_flags
= CRYPTO_ALG_ASYNC
|
1608 CRYPTO_ALG_NEED_FALLBACK
,
1609 .cra_blocksize
= SHA256_BLOCK_SIZE
,
1610 .cra_ctxsize
= sizeof(struct omap_sham_ctx
),
1611 .cra_alignmask
= OMAP_ALIGN_MASK
,
1612 .cra_module
= THIS_MODULE
,
1613 .cra_init
= omap_sham_cra_init
,
1614 .cra_exit
= omap_sham_cra_exit
,
1618 .init
= omap_sham_init
,
1619 .update
= omap_sham_update
,
1620 .final
= omap_sham_final
,
1621 .finup
= omap_sham_finup
,
1622 .digest
= omap_sham_digest
,
1623 .setkey
= omap_sham_setkey
,
1624 .halg
.digestsize
= SHA224_DIGEST_SIZE
,
1626 .cra_name
= "hmac(sha224)",
1627 .cra_driver_name
= "omap-hmac-sha224",
1628 .cra_priority
= 400,
1629 .cra_flags
= CRYPTO_ALG_ASYNC
|
1630 CRYPTO_ALG_NEED_FALLBACK
,
1631 .cra_blocksize
= SHA224_BLOCK_SIZE
,
1632 .cra_ctxsize
= sizeof(struct omap_sham_ctx
) +
1633 sizeof(struct omap_sham_hmac_ctx
),
1634 .cra_alignmask
= OMAP_ALIGN_MASK
,
1635 .cra_module
= THIS_MODULE
,
1636 .cra_init
= omap_sham_cra_sha224_init
,
1637 .cra_exit
= omap_sham_cra_exit
,
1641 .init
= omap_sham_init
,
1642 .update
= omap_sham_update
,
1643 .final
= omap_sham_final
,
1644 .finup
= omap_sham_finup
,
1645 .digest
= omap_sham_digest
,
1646 .setkey
= omap_sham_setkey
,
1647 .halg
.digestsize
= SHA256_DIGEST_SIZE
,
1649 .cra_name
= "hmac(sha256)",
1650 .cra_driver_name
= "omap-hmac-sha256",
1651 .cra_priority
= 400,
1652 .cra_flags
= CRYPTO_ALG_ASYNC
|
1653 CRYPTO_ALG_NEED_FALLBACK
,
1654 .cra_blocksize
= SHA256_BLOCK_SIZE
,
1655 .cra_ctxsize
= sizeof(struct omap_sham_ctx
) +
1656 sizeof(struct omap_sham_hmac_ctx
),
1657 .cra_alignmask
= OMAP_ALIGN_MASK
,
1658 .cra_module
= THIS_MODULE
,
1659 .cra_init
= omap_sham_cra_sha256_init
,
1660 .cra_exit
= omap_sham_cra_exit
,
1665 static struct ahash_alg algs_sha384_sha512
[] = {
1667 .init
= omap_sham_init
,
1668 .update
= omap_sham_update
,
1669 .final
= omap_sham_final
,
1670 .finup
= omap_sham_finup
,
1671 .digest
= omap_sham_digest
,
1672 .halg
.digestsize
= SHA384_DIGEST_SIZE
,
1674 .cra_name
= "sha384",
1675 .cra_driver_name
= "omap-sha384",
1676 .cra_priority
= 400,
1677 .cra_flags
= CRYPTO_ALG_ASYNC
|
1678 CRYPTO_ALG_NEED_FALLBACK
,
1679 .cra_blocksize
= SHA384_BLOCK_SIZE
,
1680 .cra_ctxsize
= sizeof(struct omap_sham_ctx
),
1681 .cra_alignmask
= OMAP_ALIGN_MASK
,
1682 .cra_module
= THIS_MODULE
,
1683 .cra_init
= omap_sham_cra_init
,
1684 .cra_exit
= omap_sham_cra_exit
,
1688 .init
= omap_sham_init
,
1689 .update
= omap_sham_update
,
1690 .final
= omap_sham_final
,
1691 .finup
= omap_sham_finup
,
1692 .digest
= omap_sham_digest
,
1693 .halg
.digestsize
= SHA512_DIGEST_SIZE
,
1695 .cra_name
= "sha512",
1696 .cra_driver_name
= "omap-sha512",
1697 .cra_priority
= 400,
1698 .cra_flags
= CRYPTO_ALG_ASYNC
|
1699 CRYPTO_ALG_NEED_FALLBACK
,
1700 .cra_blocksize
= SHA512_BLOCK_SIZE
,
1701 .cra_ctxsize
= sizeof(struct omap_sham_ctx
),
1702 .cra_alignmask
= OMAP_ALIGN_MASK
,
1703 .cra_module
= THIS_MODULE
,
1704 .cra_init
= omap_sham_cra_init
,
1705 .cra_exit
= omap_sham_cra_exit
,
1709 .init
= omap_sham_init
,
1710 .update
= omap_sham_update
,
1711 .final
= omap_sham_final
,
1712 .finup
= omap_sham_finup
,
1713 .digest
= omap_sham_digest
,
1714 .setkey
= omap_sham_setkey
,
1715 .halg
.digestsize
= SHA384_DIGEST_SIZE
,
1717 .cra_name
= "hmac(sha384)",
1718 .cra_driver_name
= "omap-hmac-sha384",
1719 .cra_priority
= 400,
1720 .cra_flags
= CRYPTO_ALG_ASYNC
|
1721 CRYPTO_ALG_NEED_FALLBACK
,
1722 .cra_blocksize
= SHA384_BLOCK_SIZE
,
1723 .cra_ctxsize
= sizeof(struct omap_sham_ctx
) +
1724 sizeof(struct omap_sham_hmac_ctx
),
1725 .cra_alignmask
= OMAP_ALIGN_MASK
,
1726 .cra_module
= THIS_MODULE
,
1727 .cra_init
= omap_sham_cra_sha384_init
,
1728 .cra_exit
= omap_sham_cra_exit
,
1732 .init
= omap_sham_init
,
1733 .update
= omap_sham_update
,
1734 .final
= omap_sham_final
,
1735 .finup
= omap_sham_finup
,
1736 .digest
= omap_sham_digest
,
1737 .setkey
= omap_sham_setkey
,
1738 .halg
.digestsize
= SHA512_DIGEST_SIZE
,
1740 .cra_name
= "hmac(sha512)",
1741 .cra_driver_name
= "omap-hmac-sha512",
1742 .cra_priority
= 400,
1743 .cra_flags
= CRYPTO_ALG_ASYNC
|
1744 CRYPTO_ALG_NEED_FALLBACK
,
1745 .cra_blocksize
= SHA512_BLOCK_SIZE
,
1746 .cra_ctxsize
= sizeof(struct omap_sham_ctx
) +
1747 sizeof(struct omap_sham_hmac_ctx
),
1748 .cra_alignmask
= OMAP_ALIGN_MASK
,
1749 .cra_module
= THIS_MODULE
,
1750 .cra_init
= omap_sham_cra_sha512_init
,
1751 .cra_exit
= omap_sham_cra_exit
,
1756 static void omap_sham_done_task(unsigned long data
)
1758 struct omap_sham_dev
*dd
= (struct omap_sham_dev
*)data
;
1761 dev_dbg(dd
->dev
, "%s: flags=%lx\n", __func__
, dd
->flags
);
1763 if (!test_bit(FLAGS_BUSY
, &dd
->flags
)) {
1764 omap_sham_handle_queue(dd
, NULL
);
1768 if (test_bit(FLAGS_CPU
, &dd
->flags
)) {
1769 if (test_and_clear_bit(FLAGS_OUTPUT_READY
, &dd
->flags
))
1771 } else if (test_bit(FLAGS_DMA_READY
, &dd
->flags
)) {
1772 if (test_and_clear_bit(FLAGS_DMA_ACTIVE
, &dd
->flags
)) {
1773 omap_sham_update_dma_stop(dd
);
1779 if (test_and_clear_bit(FLAGS_OUTPUT_READY
, &dd
->flags
)) {
1780 /* hash or semi-hash ready */
1781 clear_bit(FLAGS_DMA_READY
, &dd
->flags
);
1789 dev_dbg(dd
->dev
, "update done: err: %d\n", err
);
1790 /* finish curent request */
1791 omap_sham_finish_req(dd
->req
, err
);
1793 /* If we are not busy, process next req */
1794 if (!test_bit(FLAGS_BUSY
, &dd
->flags
))
1795 omap_sham_handle_queue(dd
, NULL
);
1798 static irqreturn_t
omap_sham_irq_common(struct omap_sham_dev
*dd
)
1800 if (!test_bit(FLAGS_BUSY
, &dd
->flags
)) {
1801 dev_warn(dd
->dev
, "Interrupt when no active requests.\n");
1803 set_bit(FLAGS_OUTPUT_READY
, &dd
->flags
);
1804 tasklet_schedule(&dd
->done_task
);
1810 static irqreturn_t
omap_sham_irq_omap2(int irq
, void *dev_id
)
1812 struct omap_sham_dev
*dd
= dev_id
;
1814 if (unlikely(test_bit(FLAGS_FINAL
, &dd
->flags
)))
1815 /* final -> allow device to go to power-saving mode */
1816 omap_sham_write_mask(dd
, SHA_REG_CTRL
, 0, SHA_REG_CTRL_LENGTH
);
1818 omap_sham_write_mask(dd
, SHA_REG_CTRL
, SHA_REG_CTRL_OUTPUT_READY
,
1819 SHA_REG_CTRL_OUTPUT_READY
);
1820 omap_sham_read(dd
, SHA_REG_CTRL
);
1822 return omap_sham_irq_common(dd
);
1825 static irqreturn_t
omap_sham_irq_omap4(int irq
, void *dev_id
)
1827 struct omap_sham_dev
*dd
= dev_id
;
1829 omap_sham_write_mask(dd
, SHA_REG_MASK(dd
), 0, SHA_REG_MASK_IT_EN
);
1831 return omap_sham_irq_common(dd
);
1834 static struct omap_sham_algs_info omap_sham_algs_info_omap2
[] = {
1836 .algs_list
= algs_sha1_md5
,
1837 .size
= ARRAY_SIZE(algs_sha1_md5
),
1841 static const struct omap_sham_pdata omap_sham_pdata_omap2
= {
1842 .algs_info
= omap_sham_algs_info_omap2
,
1843 .algs_info_size
= ARRAY_SIZE(omap_sham_algs_info_omap2
),
1844 .flags
= BIT(FLAGS_BE32_SHA1
),
1845 .digest_size
= SHA1_DIGEST_SIZE
,
1846 .copy_hash
= omap_sham_copy_hash_omap2
,
1847 .write_ctrl
= omap_sham_write_ctrl_omap2
,
1848 .trigger
= omap_sham_trigger_omap2
,
1849 .poll_irq
= omap_sham_poll_irq_omap2
,
1850 .intr_hdlr
= omap_sham_irq_omap2
,
1851 .idigest_ofs
= 0x00,
1856 .sysstatus_ofs
= 0x64,
1864 static struct omap_sham_algs_info omap_sham_algs_info_omap4
[] = {
1866 .algs_list
= algs_sha1_md5
,
1867 .size
= ARRAY_SIZE(algs_sha1_md5
),
1870 .algs_list
= algs_sha224_sha256
,
1871 .size
= ARRAY_SIZE(algs_sha224_sha256
),
1875 static const struct omap_sham_pdata omap_sham_pdata_omap4
= {
1876 .algs_info
= omap_sham_algs_info_omap4
,
1877 .algs_info_size
= ARRAY_SIZE(omap_sham_algs_info_omap4
),
1878 .flags
= BIT(FLAGS_AUTO_XOR
),
1879 .digest_size
= SHA256_DIGEST_SIZE
,
1880 .copy_hash
= omap_sham_copy_hash_omap4
,
1881 .write_ctrl
= omap_sham_write_ctrl_omap4
,
1882 .trigger
= omap_sham_trigger_omap4
,
1883 .poll_irq
= omap_sham_poll_irq_omap4
,
1884 .intr_hdlr
= omap_sham_irq_omap4
,
1885 .idigest_ofs
= 0x020,
1888 .digcnt_ofs
= 0x040,
1891 .sysstatus_ofs
= 0x114,
1894 .major_mask
= 0x0700,
1896 .minor_mask
= 0x003f,
1900 static struct omap_sham_algs_info omap_sham_algs_info_omap5
[] = {
1902 .algs_list
= algs_sha1_md5
,
1903 .size
= ARRAY_SIZE(algs_sha1_md5
),
1906 .algs_list
= algs_sha224_sha256
,
1907 .size
= ARRAY_SIZE(algs_sha224_sha256
),
1910 .algs_list
= algs_sha384_sha512
,
1911 .size
= ARRAY_SIZE(algs_sha384_sha512
),
1915 static const struct omap_sham_pdata omap_sham_pdata_omap5
= {
1916 .algs_info
= omap_sham_algs_info_omap5
,
1917 .algs_info_size
= ARRAY_SIZE(omap_sham_algs_info_omap5
),
1918 .flags
= BIT(FLAGS_AUTO_XOR
),
1919 .digest_size
= SHA512_DIGEST_SIZE
,
1920 .copy_hash
= omap_sham_copy_hash_omap4
,
1921 .write_ctrl
= omap_sham_write_ctrl_omap4
,
1922 .trigger
= omap_sham_trigger_omap4
,
1923 .poll_irq
= omap_sham_poll_irq_omap4
,
1924 .intr_hdlr
= omap_sham_irq_omap4
,
1925 .idigest_ofs
= 0x240,
1926 .odigest_ofs
= 0x200,
1928 .digcnt_ofs
= 0x280,
1931 .sysstatus_ofs
= 0x114,
1933 .length_ofs
= 0x288,
1934 .major_mask
= 0x0700,
1936 .minor_mask
= 0x003f,
1940 static const struct of_device_id omap_sham_of_match
[] = {
1942 .compatible
= "ti,omap2-sham",
1943 .data
= &omap_sham_pdata_omap2
,
1946 .compatible
= "ti,omap3-sham",
1947 .data
= &omap_sham_pdata_omap2
,
1950 .compatible
= "ti,omap4-sham",
1951 .data
= &omap_sham_pdata_omap4
,
1954 .compatible
= "ti,omap5-sham",
1955 .data
= &omap_sham_pdata_omap5
,
1959 MODULE_DEVICE_TABLE(of
, omap_sham_of_match
);
1961 static int omap_sham_get_res_of(struct omap_sham_dev
*dd
,
1962 struct device
*dev
, struct resource
*res
)
1964 struct device_node
*node
= dev
->of_node
;
1967 dd
->pdata
= of_device_get_match_data(dev
);
1969 dev_err(dev
, "no compatible OF match\n");
1974 err
= of_address_to_resource(node
, 0, res
);
1976 dev_err(dev
, "can't translate OF node address\n");
1981 dd
->irq
= irq_of_parse_and_map(node
, 0);
1983 dev_err(dev
, "can't translate OF irq value\n");
1992 static const struct of_device_id omap_sham_of_match
[] = {
1996 static int omap_sham_get_res_of(struct omap_sham_dev
*dd
,
1997 struct device
*dev
, struct resource
*res
)
2003 static int omap_sham_get_res_pdev(struct omap_sham_dev
*dd
,
2004 struct platform_device
*pdev
, struct resource
*res
)
2006 struct device
*dev
= &pdev
->dev
;
2010 /* Get the base address */
2011 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2013 dev_err(dev
, "no MEM resource info\n");
2017 memcpy(res
, r
, sizeof(*res
));
2020 dd
->irq
= platform_get_irq(pdev
, 0);
2026 /* Only OMAP2/3 can be non-DT */
2027 dd
->pdata
= &omap_sham_pdata_omap2
;
2033 static ssize_t
fallback_show(struct device
*dev
, struct device_attribute
*attr
,
2036 struct omap_sham_dev
*dd
= dev_get_drvdata(dev
);
2038 return sprintf(buf
, "%d\n", dd
->fallback_sz
);
2041 static ssize_t
fallback_store(struct device
*dev
, struct device_attribute
*attr
,
2042 const char *buf
, size_t size
)
2044 struct omap_sham_dev
*dd
= dev_get_drvdata(dev
);
2048 status
= kstrtol(buf
, 0, &value
);
2052 /* HW accelerator only works with buffers > 9 */
2054 dev_err(dev
, "minimum fallback size 9\n");
2058 dd
->fallback_sz
= value
;
2063 static ssize_t
queue_len_show(struct device
*dev
, struct device_attribute
*attr
,
2066 struct omap_sham_dev
*dd
= dev_get_drvdata(dev
);
2068 return sprintf(buf
, "%d\n", dd
->queue
.max_qlen
);
2071 static ssize_t
queue_len_store(struct device
*dev
,
2072 struct device_attribute
*attr
, const char *buf
,
2075 struct omap_sham_dev
*dd
= dev_get_drvdata(dev
);
2078 unsigned long flags
;
2080 status
= kstrtol(buf
, 0, &value
);
2088 * Changing the queue size in fly is safe, if size becomes smaller
2089 * than current size, it will just not accept new entries until
2090 * it has shrank enough.
2092 spin_lock_irqsave(&dd
->lock
, flags
);
2093 dd
->queue
.max_qlen
= value
;
2094 spin_unlock_irqrestore(&dd
->lock
, flags
);
2099 static DEVICE_ATTR_RW(queue_len
);
2100 static DEVICE_ATTR_RW(fallback
);
2102 static struct attribute
*omap_sham_attrs
[] = {
2103 &dev_attr_queue_len
.attr
,
2104 &dev_attr_fallback
.attr
,
2108 static struct attribute_group omap_sham_attr_group
= {
2109 .attrs
= omap_sham_attrs
,
2112 static int omap_sham_probe(struct platform_device
*pdev
)
2114 struct omap_sham_dev
*dd
;
2115 struct device
*dev
= &pdev
->dev
;
2116 struct resource res
;
2117 dma_cap_mask_t mask
;
2121 dd
= devm_kzalloc(dev
, sizeof(struct omap_sham_dev
), GFP_KERNEL
);
2123 dev_err(dev
, "unable to alloc data struct.\n");
2128 platform_set_drvdata(pdev
, dd
);
2130 INIT_LIST_HEAD(&dd
->list
);
2131 spin_lock_init(&dd
->lock
);
2132 tasklet_init(&dd
->done_task
, omap_sham_done_task
, (unsigned long)dd
);
2133 crypto_init_queue(&dd
->queue
, OMAP_SHAM_QUEUE_LENGTH
);
2135 err
= (dev
->of_node
) ? omap_sham_get_res_of(dd
, dev
, &res
) :
2136 omap_sham_get_res_pdev(dd
, pdev
, &res
);
2140 dd
->io_base
= devm_ioremap_resource(dev
, &res
);
2141 if (IS_ERR(dd
->io_base
)) {
2142 err
= PTR_ERR(dd
->io_base
);
2145 dd
->phys_base
= res
.start
;
2147 err
= devm_request_irq(dev
, dd
->irq
, dd
->pdata
->intr_hdlr
,
2148 IRQF_TRIGGER_NONE
, dev_name(dev
), dd
);
2150 dev_err(dev
, "unable to request irq %d, err = %d\n",
2156 dma_cap_set(DMA_SLAVE
, mask
);
2158 dd
->dma_lch
= dma_request_chan(dev
, "rx");
2159 if (IS_ERR(dd
->dma_lch
)) {
2160 err
= PTR_ERR(dd
->dma_lch
);
2161 if (err
== -EPROBE_DEFER
)
2164 dd
->polling_mode
= 1;
2165 dev_dbg(dev
, "using polling mode instead of dma\n");
2168 dd
->flags
|= dd
->pdata
->flags
;
2169 sham
.flags
|= dd
->pdata
->flags
;
2171 pm_runtime_use_autosuspend(dev
);
2172 pm_runtime_set_autosuspend_delay(dev
, DEFAULT_AUTOSUSPEND_DELAY
);
2174 dd
->fallback_sz
= OMAP_SHA_DMA_THRESHOLD
;
2176 pm_runtime_enable(dev
);
2177 pm_runtime_irq_safe(dev
);
2179 err
= pm_runtime_get_sync(dev
);
2181 dev_err(dev
, "failed to get sync: %d\n", err
);
2185 rev
= omap_sham_read(dd
, SHA_REG_REV(dd
));
2186 pm_runtime_put_sync(&pdev
->dev
);
2188 dev_info(dev
, "hw accel on OMAP rev %u.%u\n",
2189 (rev
& dd
->pdata
->major_mask
) >> dd
->pdata
->major_shift
,
2190 (rev
& dd
->pdata
->minor_mask
) >> dd
->pdata
->minor_shift
);
2192 spin_lock(&sham
.lock
);
2193 list_add_tail(&dd
->list
, &sham
.dev_list
);
2194 spin_unlock(&sham
.lock
);
2196 for (i
= 0; i
< dd
->pdata
->algs_info_size
; i
++) {
2197 if (dd
->pdata
->algs_info
[i
].registered
)
2200 for (j
= 0; j
< dd
->pdata
->algs_info
[i
].size
; j
++) {
2201 struct ahash_alg
*alg
;
2203 alg
= &dd
->pdata
->algs_info
[i
].algs_list
[j
];
2204 alg
->export
= omap_sham_export
;
2205 alg
->import
= omap_sham_import
;
2206 alg
->halg
.statesize
= sizeof(struct omap_sham_reqctx
) +
2208 err
= crypto_register_ahash(alg
);
2212 dd
->pdata
->algs_info
[i
].registered
++;
2216 err
= sysfs_create_group(&dev
->kobj
, &omap_sham_attr_group
);
2218 dev_err(dev
, "could not create sysfs device attrs\n");
2225 for (i
= dd
->pdata
->algs_info_size
- 1; i
>= 0; i
--)
2226 for (j
= dd
->pdata
->algs_info
[i
].registered
- 1; j
>= 0; j
--)
2227 crypto_unregister_ahash(
2228 &dd
->pdata
->algs_info
[i
].algs_list
[j
]);
2230 pm_runtime_disable(dev
);
2231 if (!dd
->polling_mode
)
2232 dma_release_channel(dd
->dma_lch
);
2234 dev_err(dev
, "initialization failed.\n");
2239 static int omap_sham_remove(struct platform_device
*pdev
)
2241 struct omap_sham_dev
*dd
;
2244 dd
= platform_get_drvdata(pdev
);
2247 spin_lock(&sham
.lock
);
2248 list_del(&dd
->list
);
2249 spin_unlock(&sham
.lock
);
2250 for (i
= dd
->pdata
->algs_info_size
- 1; i
>= 0; i
--)
2251 for (j
= dd
->pdata
->algs_info
[i
].registered
- 1; j
>= 0; j
--) {
2252 crypto_unregister_ahash(
2253 &dd
->pdata
->algs_info
[i
].algs_list
[j
]);
2254 dd
->pdata
->algs_info
[i
].registered
--;
2256 tasklet_kill(&dd
->done_task
);
2257 pm_runtime_disable(&pdev
->dev
);
2259 if (!dd
->polling_mode
)
2260 dma_release_channel(dd
->dma_lch
);
2262 sysfs_remove_group(&dd
->dev
->kobj
, &omap_sham_attr_group
);
2267 #ifdef CONFIG_PM_SLEEP
2268 static int omap_sham_suspend(struct device
*dev
)
2270 pm_runtime_put_sync(dev
);
2274 static int omap_sham_resume(struct device
*dev
)
2276 int err
= pm_runtime_get_sync(dev
);
2278 dev_err(dev
, "failed to get sync: %d\n", err
);
2285 static SIMPLE_DEV_PM_OPS(omap_sham_pm_ops
, omap_sham_suspend
, omap_sham_resume
);
2287 static struct platform_driver omap_sham_driver
= {
2288 .probe
= omap_sham_probe
,
2289 .remove
= omap_sham_remove
,
2291 .name
= "omap-sham",
2292 .pm
= &omap_sham_pm_ops
,
2293 .of_match_table
= omap_sham_of_match
,
2297 module_platform_driver(omap_sham_driver
);
2299 MODULE_DESCRIPTION("OMAP SHA1/MD5 hw acceleration support.");
2300 MODULE_LICENSE("GPL v2");
2301 MODULE_AUTHOR("Dmitry Kasatkin");
2302 MODULE_ALIAS("platform:omap-sham");