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
{
172 struct omap_sham_dev
*dd
;
177 struct crypto_shash
*fallback
;
179 struct omap_sham_hmac_ctx base
[];
182 #define OMAP_SHAM_QUEUE_LENGTH 10
184 struct omap_sham_algs_info
{
185 struct ahash_alg
*algs_list
;
187 unsigned int registered
;
190 struct omap_sham_pdata
{
191 struct omap_sham_algs_info
*algs_info
;
192 unsigned int algs_info_size
;
196 void (*copy_hash
)(struct ahash_request
*req
, int out
);
197 void (*write_ctrl
)(struct omap_sham_dev
*dd
, size_t length
,
199 void (*trigger
)(struct omap_sham_dev
*dd
, size_t length
);
200 int (*poll_irq
)(struct omap_sham_dev
*dd
);
201 irqreturn_t (*intr_hdlr
)(int irq
, void *dev_id
);
219 struct omap_sham_dev
{
220 struct list_head list
;
221 unsigned long phys_base
;
223 void __iomem
*io_base
;
227 struct dma_chan
*dma_lch
;
228 struct tasklet_struct done_task
;
230 u8 xmit_buf
[BUFLEN
] OMAP_ALIGNED
;
234 struct crypto_queue queue
;
235 struct ahash_request
*req
;
237 const struct omap_sham_pdata
*pdata
;
240 struct omap_sham_drv
{
241 struct list_head dev_list
;
246 static struct omap_sham_drv sham
= {
247 .dev_list
= LIST_HEAD_INIT(sham
.dev_list
),
248 .lock
= __SPIN_LOCK_UNLOCKED(sham
.lock
),
251 static inline u32
omap_sham_read(struct omap_sham_dev
*dd
, u32 offset
)
253 return __raw_readl(dd
->io_base
+ offset
);
256 static inline void omap_sham_write(struct omap_sham_dev
*dd
,
257 u32 offset
, u32 value
)
259 __raw_writel(value
, dd
->io_base
+ offset
);
262 static inline void omap_sham_write_mask(struct omap_sham_dev
*dd
, u32 address
,
267 val
= omap_sham_read(dd
, address
);
270 omap_sham_write(dd
, address
, val
);
273 static inline int omap_sham_wait(struct omap_sham_dev
*dd
, u32 offset
, u32 bit
)
275 unsigned long timeout
= jiffies
+ DEFAULT_TIMEOUT_INTERVAL
;
277 while (!(omap_sham_read(dd
, offset
) & bit
)) {
278 if (time_is_before_jiffies(timeout
))
285 static void omap_sham_copy_hash_omap2(struct ahash_request
*req
, int out
)
287 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
288 struct omap_sham_dev
*dd
= ctx
->dd
;
289 u32
*hash
= (u32
*)ctx
->digest
;
292 for (i
= 0; i
< dd
->pdata
->digest_size
/ sizeof(u32
); i
++) {
294 hash
[i
] = omap_sham_read(dd
, SHA_REG_IDIGEST(dd
, i
));
296 omap_sham_write(dd
, SHA_REG_IDIGEST(dd
, i
), hash
[i
]);
300 static void omap_sham_copy_hash_omap4(struct ahash_request
*req
, int out
)
302 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
303 struct omap_sham_dev
*dd
= ctx
->dd
;
306 if (ctx
->flags
& BIT(FLAGS_HMAC
)) {
307 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(dd
->req
);
308 struct omap_sham_ctx
*tctx
= crypto_ahash_ctx(tfm
);
309 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
310 u32
*opad
= (u32
*)bctx
->opad
;
312 for (i
= 0; i
< dd
->pdata
->digest_size
/ sizeof(u32
); i
++) {
314 opad
[i
] = omap_sham_read(dd
,
315 SHA_REG_ODIGEST(dd
, i
));
317 omap_sham_write(dd
, SHA_REG_ODIGEST(dd
, i
),
322 omap_sham_copy_hash_omap2(req
, out
);
325 static void omap_sham_copy_ready_hash(struct ahash_request
*req
)
327 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
328 u32
*in
= (u32
*)ctx
->digest
;
329 u32
*hash
= (u32
*)req
->result
;
330 int i
, d
, big_endian
= 0;
335 switch (ctx
->flags
& FLAGS_MODE_MASK
) {
337 d
= MD5_DIGEST_SIZE
/ sizeof(u32
);
339 case FLAGS_MODE_SHA1
:
340 /* OMAP2 SHA1 is big endian */
341 if (test_bit(FLAGS_BE32_SHA1
, &ctx
->dd
->flags
))
343 d
= SHA1_DIGEST_SIZE
/ sizeof(u32
);
345 case FLAGS_MODE_SHA224
:
346 d
= SHA224_DIGEST_SIZE
/ sizeof(u32
);
348 case FLAGS_MODE_SHA256
:
349 d
= SHA256_DIGEST_SIZE
/ sizeof(u32
);
351 case FLAGS_MODE_SHA384
:
352 d
= SHA384_DIGEST_SIZE
/ sizeof(u32
);
354 case FLAGS_MODE_SHA512
:
355 d
= SHA512_DIGEST_SIZE
/ sizeof(u32
);
362 for (i
= 0; i
< d
; i
++)
363 hash
[i
] = be32_to_cpu(in
[i
]);
365 for (i
= 0; i
< d
; i
++)
366 hash
[i
] = le32_to_cpu(in
[i
]);
369 static int omap_sham_hw_init(struct omap_sham_dev
*dd
)
373 err
= pm_runtime_get_sync(dd
->dev
);
375 dev_err(dd
->dev
, "failed to get sync: %d\n", err
);
379 if (!test_bit(FLAGS_INIT
, &dd
->flags
)) {
380 set_bit(FLAGS_INIT
, &dd
->flags
);
387 static void omap_sham_write_ctrl_omap2(struct omap_sham_dev
*dd
, size_t length
,
390 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
391 u32 val
= length
<< 5, mask
;
393 if (likely(ctx
->digcnt
))
394 omap_sham_write(dd
, SHA_REG_DIGCNT(dd
), ctx
->digcnt
);
396 omap_sham_write_mask(dd
, SHA_REG_MASK(dd
),
397 SHA_REG_MASK_IT_EN
| (dma
? SHA_REG_MASK_DMA_EN
: 0),
398 SHA_REG_MASK_IT_EN
| SHA_REG_MASK_DMA_EN
);
400 * Setting ALGO_CONST only for the first iteration
401 * and CLOSE_HASH only for the last one.
403 if ((ctx
->flags
& FLAGS_MODE_MASK
) == FLAGS_MODE_SHA1
)
404 val
|= SHA_REG_CTRL_ALGO
;
406 val
|= SHA_REG_CTRL_ALGO_CONST
;
408 val
|= SHA_REG_CTRL_CLOSE_HASH
;
410 mask
= SHA_REG_CTRL_ALGO_CONST
| SHA_REG_CTRL_CLOSE_HASH
|
411 SHA_REG_CTRL_ALGO
| SHA_REG_CTRL_LENGTH
;
413 omap_sham_write_mask(dd
, SHA_REG_CTRL
, val
, mask
);
416 static void omap_sham_trigger_omap2(struct omap_sham_dev
*dd
, size_t length
)
420 static int omap_sham_poll_irq_omap2(struct omap_sham_dev
*dd
)
422 return omap_sham_wait(dd
, SHA_REG_CTRL
, SHA_REG_CTRL_INPUT_READY
);
425 static int get_block_size(struct omap_sham_reqctx
*ctx
)
429 switch (ctx
->flags
& FLAGS_MODE_MASK
) {
431 case FLAGS_MODE_SHA1
:
434 case FLAGS_MODE_SHA224
:
435 case FLAGS_MODE_SHA256
:
436 d
= SHA256_BLOCK_SIZE
;
438 case FLAGS_MODE_SHA384
:
439 case FLAGS_MODE_SHA512
:
440 d
= SHA512_BLOCK_SIZE
;
449 static void omap_sham_write_n(struct omap_sham_dev
*dd
, u32 offset
,
450 u32
*value
, int count
)
452 for (; count
--; value
++, offset
+= 4)
453 omap_sham_write(dd
, offset
, *value
);
456 static void omap_sham_write_ctrl_omap4(struct omap_sham_dev
*dd
, size_t length
,
459 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
463 * Setting ALGO_CONST only for the first iteration and
464 * CLOSE_HASH only for the last one. Note that flags mode bits
465 * correspond to algorithm encoding in mode register.
467 val
= (ctx
->flags
& FLAGS_MODE_MASK
) >> (FLAGS_MODE_SHIFT
);
469 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(dd
->req
);
470 struct omap_sham_ctx
*tctx
= crypto_ahash_ctx(tfm
);
471 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
474 val
|= SHA_REG_MODE_ALGO_CONSTANT
;
476 if (ctx
->flags
& BIT(FLAGS_HMAC
)) {
477 bs
= get_block_size(ctx
);
478 nr_dr
= bs
/ (2 * sizeof(u32
));
479 val
|= SHA_REG_MODE_HMAC_KEY_PROC
;
480 omap_sham_write_n(dd
, SHA_REG_ODIGEST(dd
, 0),
481 (u32
*)bctx
->ipad
, nr_dr
);
482 omap_sham_write_n(dd
, SHA_REG_IDIGEST(dd
, 0),
483 (u32
*)bctx
->ipad
+ nr_dr
, nr_dr
);
489 val
|= SHA_REG_MODE_CLOSE_HASH
;
491 if (ctx
->flags
& BIT(FLAGS_HMAC
))
492 val
|= SHA_REG_MODE_HMAC_OUTER_HASH
;
495 mask
= SHA_REG_MODE_ALGO_CONSTANT
| SHA_REG_MODE_CLOSE_HASH
|
496 SHA_REG_MODE_ALGO_MASK
| SHA_REG_MODE_HMAC_OUTER_HASH
|
497 SHA_REG_MODE_HMAC_KEY_PROC
;
499 dev_dbg(dd
->dev
, "ctrl: %08x, flags: %08lx\n", val
, ctx
->flags
);
500 omap_sham_write_mask(dd
, SHA_REG_MODE(dd
), val
, mask
);
501 omap_sham_write(dd
, SHA_REG_IRQENA
, SHA_REG_IRQENA_OUTPUT_RDY
);
502 omap_sham_write_mask(dd
, SHA_REG_MASK(dd
),
504 (dma
? SHA_REG_MASK_DMA_EN
: 0),
505 SHA_REG_MASK_IT_EN
| SHA_REG_MASK_DMA_EN
);
508 static void omap_sham_trigger_omap4(struct omap_sham_dev
*dd
, size_t length
)
510 omap_sham_write(dd
, SHA_REG_LENGTH(dd
), length
);
513 static int omap_sham_poll_irq_omap4(struct omap_sham_dev
*dd
)
515 return omap_sham_wait(dd
, SHA_REG_IRQSTATUS
,
516 SHA_REG_IRQSTATUS_INPUT_RDY
);
519 static int omap_sham_xmit_cpu(struct omap_sham_dev
*dd
, size_t length
,
522 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
523 int count
, len32
, bs32
, offset
= 0;
526 struct sg_mapping_iter mi
;
528 dev_dbg(dd
->dev
, "xmit_cpu: digcnt: %d, length: %d, final: %d\n",
529 ctx
->digcnt
, length
, final
);
531 dd
->pdata
->write_ctrl(dd
, length
, final
, 0);
532 dd
->pdata
->trigger(dd
, length
);
534 /* should be non-zero before next lines to disable clocks later */
535 ctx
->digcnt
+= length
;
536 ctx
->total
-= length
;
539 set_bit(FLAGS_FINAL
, &dd
->flags
); /* catch last interrupt */
541 set_bit(FLAGS_CPU
, &dd
->flags
);
543 len32
= DIV_ROUND_UP(length
, sizeof(u32
));
544 bs32
= get_block_size(ctx
) / sizeof(u32
);
546 sg_miter_start(&mi
, ctx
->sg
, ctx
->sg_len
,
547 SG_MITER_FROM_SG
| SG_MITER_ATOMIC
);
552 if (dd
->pdata
->poll_irq(dd
))
555 for (count
= 0; count
< min(len32
, bs32
); count
++, offset
++) {
560 pr_err("sg miter failure.\n");
566 omap_sham_write(dd
, SHA_REG_DIN(dd
, count
),
570 len32
-= min(len32
, bs32
);
578 static void omap_sham_dma_callback(void *param
)
580 struct omap_sham_dev
*dd
= param
;
582 set_bit(FLAGS_DMA_READY
, &dd
->flags
);
583 tasklet_schedule(&dd
->done_task
);
586 static int omap_sham_xmit_dma(struct omap_sham_dev
*dd
, size_t length
,
589 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
590 struct dma_async_tx_descriptor
*tx
;
591 struct dma_slave_config cfg
;
594 dev_dbg(dd
->dev
, "xmit_dma: digcnt: %d, length: %d, final: %d\n",
595 ctx
->digcnt
, length
, final
);
597 if (!dma_map_sg(dd
->dev
, ctx
->sg
, ctx
->sg_len
, DMA_TO_DEVICE
)) {
598 dev_err(dd
->dev
, "dma_map_sg error\n");
602 memset(&cfg
, 0, sizeof(cfg
));
604 cfg
.dst_addr
= dd
->phys_base
+ SHA_REG_DIN(dd
, 0);
605 cfg
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
606 cfg
.dst_maxburst
= get_block_size(ctx
) / DMA_SLAVE_BUSWIDTH_4_BYTES
;
608 ret
= dmaengine_slave_config(dd
->dma_lch
, &cfg
);
610 pr_err("omap-sham: can't configure dmaengine slave: %d\n", ret
);
614 tx
= dmaengine_prep_slave_sg(dd
->dma_lch
, ctx
->sg
, ctx
->sg_len
,
616 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
619 dev_err(dd
->dev
, "prep_slave_sg failed\n");
623 tx
->callback
= omap_sham_dma_callback
;
624 tx
->callback_param
= dd
;
626 dd
->pdata
->write_ctrl(dd
, length
, final
, 1);
628 ctx
->digcnt
+= length
;
629 ctx
->total
-= length
;
632 set_bit(FLAGS_FINAL
, &dd
->flags
); /* catch last interrupt */
634 set_bit(FLAGS_DMA_ACTIVE
, &dd
->flags
);
636 dmaengine_submit(tx
);
637 dma_async_issue_pending(dd
->dma_lch
);
639 dd
->pdata
->trigger(dd
, length
);
644 static int omap_sham_copy_sg_lists(struct omap_sham_reqctx
*ctx
,
645 struct scatterlist
*sg
, int bs
, int new_len
)
647 int n
= sg_nents(sg
);
648 struct scatterlist
*tmp
;
649 int offset
= ctx
->offset
;
651 ctx
->total
= new_len
;
656 ctx
->sg
= kmalloc_array(n
, sizeof(*sg
), GFP_KERNEL
);
660 sg_init_table(ctx
->sg
, n
);
667 sg_set_buf(tmp
, ctx
->dd
->xmit_buf
, ctx
->bufcnt
);
670 new_len
-= ctx
->bufcnt
;
673 while (sg
&& new_len
) {
674 int len
= sg
->length
- offset
;
677 offset
-= sg
->length
;
687 sg_set_page(tmp
, sg_page(sg
), len
, sg
->offset
+ offset
);
702 set_bit(FLAGS_SGS_ALLOCED
, &ctx
->dd
->flags
);
704 ctx
->offset
+= new_len
- ctx
->bufcnt
;
710 static int omap_sham_copy_sgs(struct omap_sham_reqctx
*ctx
,
711 struct scatterlist
*sg
, int bs
,
712 unsigned int new_len
)
717 pages
= get_order(new_len
);
719 buf
= (void *)__get_free_pages(GFP_ATOMIC
, pages
);
721 pr_err("Couldn't allocate pages for unaligned cases.\n");
726 memcpy(buf
, ctx
->dd
->xmit_buf
, ctx
->bufcnt
);
728 scatterwalk_map_and_copy(buf
+ ctx
->bufcnt
, sg
, ctx
->offset
,
729 min(new_len
, ctx
->total
) - ctx
->bufcnt
, 0);
730 sg_init_table(ctx
->sgl
, 1);
731 sg_set_buf(ctx
->sgl
, buf
, new_len
);
733 set_bit(FLAGS_SGS_COPIED
, &ctx
->dd
->flags
);
735 ctx
->offset
+= new_len
- ctx
->bufcnt
;
737 ctx
->total
= new_len
;
742 static int omap_sham_align_sgs(struct scatterlist
*sg
,
743 int nbytes
, int bs
, bool final
,
744 struct omap_sham_reqctx
*rctx
)
749 struct scatterlist
*sg_tmp
= sg
;
751 int offset
= rctx
->offset
;
752 int bufcnt
= rctx
->bufcnt
;
754 if (!sg
|| !sg
->length
|| !nbytes
)
763 new_len
= DIV_ROUND_UP(new_len
, bs
) * bs
;
765 new_len
= (new_len
- 1) / bs
* bs
;
770 if (nbytes
!= new_len
)
773 while (nbytes
> 0 && sg_tmp
) {
777 if (!IS_ALIGNED(bufcnt
, bs
)) {
789 #ifdef CONFIG_ZONE_DMA
790 if (page_zonenum(sg_page(sg_tmp
)) != ZONE_DMA
) {
796 if (offset
< sg_tmp
->length
) {
797 if (!IS_ALIGNED(offset
+ sg_tmp
->offset
, 4)) {
802 if (!IS_ALIGNED(sg_tmp
->length
- offset
, bs
)) {
809 offset
-= sg_tmp
->length
;
815 nbytes
-= sg_tmp
->length
;
818 sg_tmp
= sg_next(sg_tmp
);
826 if (new_len
> OMAP_SHA_MAX_DMA_LEN
) {
827 new_len
= OMAP_SHA_MAX_DMA_LEN
;
832 return omap_sham_copy_sgs(rctx
, sg
, bs
, new_len
);
834 return omap_sham_copy_sg_lists(rctx
, sg
, bs
, new_len
);
836 rctx
->total
= new_len
;
837 rctx
->offset
+= new_len
;
840 sg_init_table(rctx
->sgl
, 2);
841 sg_set_buf(rctx
->sgl
, rctx
->dd
->xmit_buf
, rctx
->bufcnt
);
842 sg_chain(rctx
->sgl
, 2, sg
);
843 rctx
->sg
= rctx
->sgl
;
851 static int omap_sham_prepare_request(struct ahash_request
*req
, bool update
)
853 struct omap_sham_reqctx
*rctx
= ahash_request_ctx(req
);
857 bool final
= rctx
->flags
& BIT(FLAGS_FINUP
);
860 bs
= get_block_size(rctx
);
862 nbytes
= rctx
->bufcnt
;
865 nbytes
+= req
->nbytes
- rctx
->offset
;
867 dev_dbg(rctx
->dd
->dev
,
868 "%s: nbytes=%d, bs=%d, total=%d, offset=%d, bufcnt=%d\n",
869 __func__
, nbytes
, bs
, rctx
->total
, rctx
->offset
,
875 rctx
->total
= nbytes
;
877 if (update
&& req
->nbytes
&& (!IS_ALIGNED(rctx
->bufcnt
, bs
))) {
878 int len
= bs
- rctx
->bufcnt
% bs
;
880 if (len
> req
->nbytes
)
882 scatterwalk_map_and_copy(rctx
->buffer
+ rctx
->bufcnt
, req
->src
,
889 memcpy(rctx
->dd
->xmit_buf
, rctx
->buffer
, rctx
->bufcnt
);
891 ret
= omap_sham_align_sgs(req
->src
, nbytes
, bs
, final
, rctx
);
895 hash_later
= nbytes
- rctx
->total
;
900 scatterwalk_map_and_copy(rctx
->buffer
,
902 req
->nbytes
- hash_later
,
905 rctx
->bufcnt
= hash_later
;
910 if (hash_later
> rctx
->buflen
)
911 set_bit(FLAGS_HUGE
, &rctx
->dd
->flags
);
913 rctx
->total
= min(nbytes
, rctx
->total
);
918 static int omap_sham_update_dma_stop(struct omap_sham_dev
*dd
)
920 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
922 dma_unmap_sg(dd
->dev
, ctx
->sg
, ctx
->sg_len
, DMA_TO_DEVICE
);
924 clear_bit(FLAGS_DMA_ACTIVE
, &dd
->flags
);
929 static int omap_sham_init(struct ahash_request
*req
)
931 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
932 struct omap_sham_ctx
*tctx
= crypto_ahash_ctx(tfm
);
933 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
934 struct omap_sham_dev
*dd
= NULL
, *tmp
;
937 spin_lock_bh(&sham
.lock
);
939 list_for_each_entry(tmp
, &sham
.dev_list
, list
) {
947 spin_unlock_bh(&sham
.lock
);
953 dev_dbg(dd
->dev
, "init: digest size: %d\n",
954 crypto_ahash_digestsize(tfm
));
956 switch (crypto_ahash_digestsize(tfm
)) {
957 case MD5_DIGEST_SIZE
:
958 ctx
->flags
|= FLAGS_MODE_MD5
;
959 bs
= SHA1_BLOCK_SIZE
;
961 case SHA1_DIGEST_SIZE
:
962 ctx
->flags
|= FLAGS_MODE_SHA1
;
963 bs
= SHA1_BLOCK_SIZE
;
965 case SHA224_DIGEST_SIZE
:
966 ctx
->flags
|= FLAGS_MODE_SHA224
;
967 bs
= SHA224_BLOCK_SIZE
;
969 case SHA256_DIGEST_SIZE
:
970 ctx
->flags
|= FLAGS_MODE_SHA256
;
971 bs
= SHA256_BLOCK_SIZE
;
973 case SHA384_DIGEST_SIZE
:
974 ctx
->flags
|= FLAGS_MODE_SHA384
;
975 bs
= SHA384_BLOCK_SIZE
;
977 case SHA512_DIGEST_SIZE
:
978 ctx
->flags
|= FLAGS_MODE_SHA512
;
979 bs
= SHA512_BLOCK_SIZE
;
987 ctx
->buflen
= BUFLEN
;
989 if (tctx
->flags
& BIT(FLAGS_HMAC
)) {
990 if (!test_bit(FLAGS_AUTO_XOR
, &dd
->flags
)) {
991 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
993 memcpy(ctx
->buffer
, bctx
->ipad
, bs
);
997 ctx
->flags
|= BIT(FLAGS_HMAC
);
1004 static int omap_sham_update_req(struct omap_sham_dev
*dd
)
1006 struct ahash_request
*req
= dd
->req
;
1007 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
1009 bool final
= (ctx
->flags
& BIT(FLAGS_FINUP
)) &&
1010 !(dd
->flags
& BIT(FLAGS_HUGE
));
1012 dev_dbg(dd
->dev
, "update_req: total: %u, digcnt: %d, final: %d",
1013 ctx
->total
, ctx
->digcnt
, final
);
1015 if (ctx
->total
< get_block_size(ctx
) ||
1016 ctx
->total
< dd
->fallback_sz
)
1017 ctx
->flags
|= BIT(FLAGS_CPU
);
1019 if (ctx
->flags
& BIT(FLAGS_CPU
))
1020 err
= omap_sham_xmit_cpu(dd
, ctx
->total
, final
);
1022 err
= omap_sham_xmit_dma(dd
, ctx
->total
, final
);
1024 /* wait for dma completion before can take more data */
1025 dev_dbg(dd
->dev
, "update: err: %d, digcnt: %d\n", err
, ctx
->digcnt
);
1030 static int omap_sham_final_req(struct omap_sham_dev
*dd
)
1032 struct ahash_request
*req
= dd
->req
;
1033 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
1034 int err
= 0, use_dma
= 1;
1036 if (dd
->flags
& BIT(FLAGS_HUGE
))
1039 if ((ctx
->total
<= get_block_size(ctx
)) || dd
->polling_mode
)
1041 * faster to handle last block with cpu or
1042 * use cpu when dma is not present.
1047 err
= omap_sham_xmit_dma(dd
, ctx
->total
, 1);
1049 err
= omap_sham_xmit_cpu(dd
, ctx
->total
, 1);
1053 dev_dbg(dd
->dev
, "final_req: err: %d\n", err
);
1058 static int omap_sham_finish_hmac(struct ahash_request
*req
)
1060 struct omap_sham_ctx
*tctx
= crypto_tfm_ctx(req
->base
.tfm
);
1061 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
1062 int bs
= crypto_shash_blocksize(bctx
->shash
);
1063 int ds
= crypto_shash_digestsize(bctx
->shash
);
1064 SHASH_DESC_ON_STACK(shash
, bctx
->shash
);
1066 shash
->tfm
= bctx
->shash
;
1068 return crypto_shash_init(shash
) ?:
1069 crypto_shash_update(shash
, bctx
->opad
, bs
) ?:
1070 crypto_shash_finup(shash
, req
->result
, ds
, req
->result
);
1073 static int omap_sham_finish(struct ahash_request
*req
)
1075 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
1076 struct omap_sham_dev
*dd
= ctx
->dd
;
1080 omap_sham_copy_ready_hash(req
);
1081 if ((ctx
->flags
& BIT(FLAGS_HMAC
)) &&
1082 !test_bit(FLAGS_AUTO_XOR
, &dd
->flags
))
1083 err
= omap_sham_finish_hmac(req
);
1086 dev_dbg(dd
->dev
, "digcnt: %d, bufcnt: %d\n", ctx
->digcnt
, ctx
->bufcnt
);
1091 static void omap_sham_finish_req(struct ahash_request
*req
, int err
)
1093 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
1094 struct omap_sham_dev
*dd
= ctx
->dd
;
1096 if (test_bit(FLAGS_SGS_COPIED
, &dd
->flags
))
1097 free_pages((unsigned long)sg_virt(ctx
->sg
),
1098 get_order(ctx
->sg
->length
));
1100 if (test_bit(FLAGS_SGS_ALLOCED
, &dd
->flags
))
1105 dd
->flags
&= ~(BIT(FLAGS_SGS_ALLOCED
) | BIT(FLAGS_SGS_COPIED
));
1107 if (dd
->flags
& BIT(FLAGS_HUGE
)) {
1108 dd
->flags
&= ~(BIT(FLAGS_CPU
) | BIT(FLAGS_DMA_READY
) |
1109 BIT(FLAGS_OUTPUT_READY
) | BIT(FLAGS_HUGE
));
1110 omap_sham_prepare_request(req
, ctx
->op
== OP_UPDATE
);
1111 if (ctx
->op
== OP_UPDATE
|| (dd
->flags
& BIT(FLAGS_HUGE
))) {
1112 err
= omap_sham_update_req(dd
);
1113 if (err
!= -EINPROGRESS
&&
1114 (ctx
->flags
& BIT(FLAGS_FINUP
)))
1115 err
= omap_sham_final_req(dd
);
1116 } else if (ctx
->op
== OP_FINAL
) {
1117 omap_sham_final_req(dd
);
1123 dd
->pdata
->copy_hash(req
, 1);
1124 if (test_bit(FLAGS_FINAL
, &dd
->flags
))
1125 err
= omap_sham_finish(req
);
1127 ctx
->flags
|= BIT(FLAGS_ERROR
);
1130 /* atomic operation is not needed here */
1131 dd
->flags
&= ~(BIT(FLAGS_BUSY
) | BIT(FLAGS_FINAL
) | BIT(FLAGS_CPU
) |
1132 BIT(FLAGS_DMA_READY
) | BIT(FLAGS_OUTPUT_READY
));
1134 pm_runtime_mark_last_busy(dd
->dev
);
1135 pm_runtime_put_autosuspend(dd
->dev
);
1139 if (req
->base
.complete
)
1140 req
->base
.complete(&req
->base
, err
);
1143 static int omap_sham_handle_queue(struct omap_sham_dev
*dd
,
1144 struct ahash_request
*req
)
1146 struct crypto_async_request
*async_req
, *backlog
;
1147 struct omap_sham_reqctx
*ctx
;
1148 unsigned long flags
;
1149 int err
= 0, ret
= 0;
1152 spin_lock_irqsave(&dd
->lock
, flags
);
1154 ret
= ahash_enqueue_request(&dd
->queue
, req
);
1155 if (test_bit(FLAGS_BUSY
, &dd
->flags
)) {
1156 spin_unlock_irqrestore(&dd
->lock
, flags
);
1159 backlog
= crypto_get_backlog(&dd
->queue
);
1160 async_req
= crypto_dequeue_request(&dd
->queue
);
1162 set_bit(FLAGS_BUSY
, &dd
->flags
);
1163 spin_unlock_irqrestore(&dd
->lock
, flags
);
1169 backlog
->complete(backlog
, -EINPROGRESS
);
1171 req
= ahash_request_cast(async_req
);
1173 ctx
= ahash_request_ctx(req
);
1175 err
= omap_sham_prepare_request(req
, ctx
->op
== OP_UPDATE
);
1176 if (err
|| !ctx
->total
)
1179 dev_dbg(dd
->dev
, "handling new req, op: %lu, nbytes: %d\n",
1180 ctx
->op
, req
->nbytes
);
1182 err
= omap_sham_hw_init(dd
);
1187 /* request has changed - restore hash */
1188 dd
->pdata
->copy_hash(req
, 0);
1190 if (ctx
->op
== OP_UPDATE
|| (dd
->flags
& BIT(FLAGS_HUGE
))) {
1191 err
= omap_sham_update_req(dd
);
1192 if (err
!= -EINPROGRESS
&& (ctx
->flags
& BIT(FLAGS_FINUP
)))
1193 /* no final() after finup() */
1194 err
= omap_sham_final_req(dd
);
1195 } else if (ctx
->op
== OP_FINAL
) {
1196 err
= omap_sham_final_req(dd
);
1199 dev_dbg(dd
->dev
, "exit, err: %d\n", err
);
1201 if (err
!= -EINPROGRESS
) {
1202 /* done_task will not finish it, so do it here */
1203 omap_sham_finish_req(req
, err
);
1207 * Execute next request immediately if there is anything
1216 static int omap_sham_enqueue(struct ahash_request
*req
, unsigned int op
)
1218 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
1219 struct omap_sham_ctx
*tctx
= crypto_tfm_ctx(req
->base
.tfm
);
1220 struct omap_sham_dev
*dd
= tctx
->dd
;
1224 return omap_sham_handle_queue(dd
, req
);
1227 static int omap_sham_update(struct ahash_request
*req
)
1229 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
1230 struct omap_sham_dev
*dd
= ctx
->dd
;
1235 if (ctx
->bufcnt
+ req
->nbytes
<= ctx
->buflen
) {
1236 scatterwalk_map_and_copy(ctx
->buffer
+ ctx
->bufcnt
, req
->src
,
1238 ctx
->bufcnt
+= req
->nbytes
;
1242 if (dd
->polling_mode
)
1243 ctx
->flags
|= BIT(FLAGS_CPU
);
1245 return omap_sham_enqueue(req
, OP_UPDATE
);
1248 static int omap_sham_shash_digest(struct crypto_shash
*tfm
, u32 flags
,
1249 const u8
*data
, unsigned int len
, u8
*out
)
1251 SHASH_DESC_ON_STACK(shash
, tfm
);
1255 return crypto_shash_digest(shash
, data
, len
, out
);
1258 static int omap_sham_final_shash(struct ahash_request
*req
)
1260 struct omap_sham_ctx
*tctx
= crypto_tfm_ctx(req
->base
.tfm
);
1261 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
1265 * If we are running HMAC on limited hardware support, skip
1266 * the ipad in the beginning of the buffer if we are going for
1267 * software fallback algorithm.
1269 if (test_bit(FLAGS_HMAC
, &ctx
->flags
) &&
1270 !test_bit(FLAGS_AUTO_XOR
, &ctx
->dd
->flags
))
1271 offset
= get_block_size(ctx
);
1273 return omap_sham_shash_digest(tctx
->fallback
, req
->base
.flags
,
1274 ctx
->buffer
+ offset
,
1275 ctx
->bufcnt
- offset
, req
->result
);
1278 static int omap_sham_final(struct ahash_request
*req
)
1280 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
1282 ctx
->flags
|= BIT(FLAGS_FINUP
);
1284 if (ctx
->flags
& BIT(FLAGS_ERROR
))
1285 return 0; /* uncompleted hash is not needed */
1288 * OMAP HW accel works only with buffers >= 9.
1289 * HMAC is always >= 9 because ipad == block size.
1290 * If buffersize is less than fallback_sz, we use fallback
1291 * SW encoding, as using DMA + HW in this case doesn't provide
1294 if (!ctx
->digcnt
&& ctx
->bufcnt
< ctx
->dd
->fallback_sz
)
1295 return omap_sham_final_shash(req
);
1296 else if (ctx
->bufcnt
)
1297 return omap_sham_enqueue(req
, OP_FINAL
);
1299 /* copy ready hash (+ finalize hmac) */
1300 return omap_sham_finish(req
);
1303 static int omap_sham_finup(struct ahash_request
*req
)
1305 struct omap_sham_reqctx
*ctx
= ahash_request_ctx(req
);
1308 ctx
->flags
|= BIT(FLAGS_FINUP
);
1310 err1
= omap_sham_update(req
);
1311 if (err1
== -EINPROGRESS
|| err1
== -EBUSY
)
1314 * final() has to be always called to cleanup resources
1315 * even if udpate() failed, except EINPROGRESS
1317 err2
= omap_sham_final(req
);
1319 return err1
?: err2
;
1322 static int omap_sham_digest(struct ahash_request
*req
)
1324 return omap_sham_init(req
) ?: omap_sham_finup(req
);
1327 static int omap_sham_setkey(struct crypto_ahash
*tfm
, const u8
*key
,
1328 unsigned int keylen
)
1330 struct omap_sham_ctx
*tctx
= crypto_ahash_ctx(tfm
);
1331 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
1332 int bs
= crypto_shash_blocksize(bctx
->shash
);
1333 int ds
= crypto_shash_digestsize(bctx
->shash
);
1334 struct omap_sham_dev
*dd
= NULL
, *tmp
;
1337 spin_lock_bh(&sham
.lock
);
1339 list_for_each_entry(tmp
, &sham
.dev_list
, list
) {
1347 spin_unlock_bh(&sham
.lock
);
1349 err
= crypto_shash_setkey(tctx
->fallback
, key
, keylen
);
1354 err
= omap_sham_shash_digest(bctx
->shash
,
1355 crypto_shash_get_flags(bctx
->shash
),
1356 key
, keylen
, bctx
->ipad
);
1361 memcpy(bctx
->ipad
, key
, keylen
);
1364 memset(bctx
->ipad
+ keylen
, 0, bs
- keylen
);
1366 if (!test_bit(FLAGS_AUTO_XOR
, &dd
->flags
)) {
1367 memcpy(bctx
->opad
, bctx
->ipad
, bs
);
1369 for (i
= 0; i
< bs
; i
++) {
1370 bctx
->ipad
[i
] ^= HMAC_IPAD_VALUE
;
1371 bctx
->opad
[i
] ^= HMAC_OPAD_VALUE
;
1378 static int omap_sham_cra_init_alg(struct crypto_tfm
*tfm
, const char *alg_base
)
1380 struct omap_sham_ctx
*tctx
= crypto_tfm_ctx(tfm
);
1381 const char *alg_name
= crypto_tfm_alg_name(tfm
);
1383 /* Allocate a fallback and abort if it failed. */
1384 tctx
->fallback
= crypto_alloc_shash(alg_name
, 0,
1385 CRYPTO_ALG_NEED_FALLBACK
);
1386 if (IS_ERR(tctx
->fallback
)) {
1387 pr_err("omap-sham: fallback driver '%s' "
1388 "could not be loaded.\n", alg_name
);
1389 return PTR_ERR(tctx
->fallback
);
1392 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm
),
1393 sizeof(struct omap_sham_reqctx
) + BUFLEN
);
1396 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
1397 tctx
->flags
|= BIT(FLAGS_HMAC
);
1398 bctx
->shash
= crypto_alloc_shash(alg_base
, 0,
1399 CRYPTO_ALG_NEED_FALLBACK
);
1400 if (IS_ERR(bctx
->shash
)) {
1401 pr_err("omap-sham: base driver '%s' "
1402 "could not be loaded.\n", alg_base
);
1403 crypto_free_shash(tctx
->fallback
);
1404 return PTR_ERR(bctx
->shash
);
1412 static int omap_sham_cra_init(struct crypto_tfm
*tfm
)
1414 return omap_sham_cra_init_alg(tfm
, NULL
);
1417 static int omap_sham_cra_sha1_init(struct crypto_tfm
*tfm
)
1419 return omap_sham_cra_init_alg(tfm
, "sha1");
1422 static int omap_sham_cra_sha224_init(struct crypto_tfm
*tfm
)
1424 return omap_sham_cra_init_alg(tfm
, "sha224");
1427 static int omap_sham_cra_sha256_init(struct crypto_tfm
*tfm
)
1429 return omap_sham_cra_init_alg(tfm
, "sha256");
1432 static int omap_sham_cra_md5_init(struct crypto_tfm
*tfm
)
1434 return omap_sham_cra_init_alg(tfm
, "md5");
1437 static int omap_sham_cra_sha384_init(struct crypto_tfm
*tfm
)
1439 return omap_sham_cra_init_alg(tfm
, "sha384");
1442 static int omap_sham_cra_sha512_init(struct crypto_tfm
*tfm
)
1444 return omap_sham_cra_init_alg(tfm
, "sha512");
1447 static void omap_sham_cra_exit(struct crypto_tfm
*tfm
)
1449 struct omap_sham_ctx
*tctx
= crypto_tfm_ctx(tfm
);
1451 crypto_free_shash(tctx
->fallback
);
1452 tctx
->fallback
= NULL
;
1454 if (tctx
->flags
& BIT(FLAGS_HMAC
)) {
1455 struct omap_sham_hmac_ctx
*bctx
= tctx
->base
;
1456 crypto_free_shash(bctx
->shash
);
1460 static int omap_sham_export(struct ahash_request
*req
, void *out
)
1462 struct omap_sham_reqctx
*rctx
= ahash_request_ctx(req
);
1464 memcpy(out
, rctx
, sizeof(*rctx
) + rctx
->bufcnt
);
1469 static int omap_sham_import(struct ahash_request
*req
, const void *in
)
1471 struct omap_sham_reqctx
*rctx
= ahash_request_ctx(req
);
1472 const struct omap_sham_reqctx
*ctx_in
= in
;
1474 memcpy(rctx
, in
, sizeof(*rctx
) + ctx_in
->bufcnt
);
1479 static struct ahash_alg algs_sha1_md5
[] = {
1481 .init
= omap_sham_init
,
1482 .update
= omap_sham_update
,
1483 .final
= omap_sham_final
,
1484 .finup
= omap_sham_finup
,
1485 .digest
= omap_sham_digest
,
1486 .halg
.digestsize
= SHA1_DIGEST_SIZE
,
1489 .cra_driver_name
= "omap-sha1",
1490 .cra_priority
= 400,
1491 .cra_flags
= CRYPTO_ALG_KERN_DRIVER_ONLY
|
1493 CRYPTO_ALG_NEED_FALLBACK
,
1494 .cra_blocksize
= SHA1_BLOCK_SIZE
,
1495 .cra_ctxsize
= sizeof(struct omap_sham_ctx
),
1496 .cra_alignmask
= OMAP_ALIGN_MASK
,
1497 .cra_module
= THIS_MODULE
,
1498 .cra_init
= omap_sham_cra_init
,
1499 .cra_exit
= omap_sham_cra_exit
,
1503 .init
= omap_sham_init
,
1504 .update
= omap_sham_update
,
1505 .final
= omap_sham_final
,
1506 .finup
= omap_sham_finup
,
1507 .digest
= omap_sham_digest
,
1508 .halg
.digestsize
= MD5_DIGEST_SIZE
,
1511 .cra_driver_name
= "omap-md5",
1512 .cra_priority
= 400,
1513 .cra_flags
= CRYPTO_ALG_KERN_DRIVER_ONLY
|
1515 CRYPTO_ALG_NEED_FALLBACK
,
1516 .cra_blocksize
= SHA1_BLOCK_SIZE
,
1517 .cra_ctxsize
= sizeof(struct omap_sham_ctx
),
1518 .cra_alignmask
= OMAP_ALIGN_MASK
,
1519 .cra_module
= THIS_MODULE
,
1520 .cra_init
= omap_sham_cra_init
,
1521 .cra_exit
= omap_sham_cra_exit
,
1525 .init
= omap_sham_init
,
1526 .update
= omap_sham_update
,
1527 .final
= omap_sham_final
,
1528 .finup
= omap_sham_finup
,
1529 .digest
= omap_sham_digest
,
1530 .setkey
= omap_sham_setkey
,
1531 .halg
.digestsize
= SHA1_DIGEST_SIZE
,
1533 .cra_name
= "hmac(sha1)",
1534 .cra_driver_name
= "omap-hmac-sha1",
1535 .cra_priority
= 400,
1536 .cra_flags
= CRYPTO_ALG_KERN_DRIVER_ONLY
|
1538 CRYPTO_ALG_NEED_FALLBACK
,
1539 .cra_blocksize
= SHA1_BLOCK_SIZE
,
1540 .cra_ctxsize
= sizeof(struct omap_sham_ctx
) +
1541 sizeof(struct omap_sham_hmac_ctx
),
1542 .cra_alignmask
= OMAP_ALIGN_MASK
,
1543 .cra_module
= THIS_MODULE
,
1544 .cra_init
= omap_sham_cra_sha1_init
,
1545 .cra_exit
= omap_sham_cra_exit
,
1549 .init
= omap_sham_init
,
1550 .update
= omap_sham_update
,
1551 .final
= omap_sham_final
,
1552 .finup
= omap_sham_finup
,
1553 .digest
= omap_sham_digest
,
1554 .setkey
= omap_sham_setkey
,
1555 .halg
.digestsize
= MD5_DIGEST_SIZE
,
1557 .cra_name
= "hmac(md5)",
1558 .cra_driver_name
= "omap-hmac-md5",
1559 .cra_priority
= 400,
1560 .cra_flags
= CRYPTO_ALG_KERN_DRIVER_ONLY
|
1562 CRYPTO_ALG_NEED_FALLBACK
,
1563 .cra_blocksize
= SHA1_BLOCK_SIZE
,
1564 .cra_ctxsize
= sizeof(struct omap_sham_ctx
) +
1565 sizeof(struct omap_sham_hmac_ctx
),
1566 .cra_alignmask
= OMAP_ALIGN_MASK
,
1567 .cra_module
= THIS_MODULE
,
1568 .cra_init
= omap_sham_cra_md5_init
,
1569 .cra_exit
= omap_sham_cra_exit
,
1574 /* OMAP4 has some algs in addition to what OMAP2 has */
1575 static struct ahash_alg algs_sha224_sha256
[] = {
1577 .init
= omap_sham_init
,
1578 .update
= omap_sham_update
,
1579 .final
= omap_sham_final
,
1580 .finup
= omap_sham_finup
,
1581 .digest
= omap_sham_digest
,
1582 .halg
.digestsize
= SHA224_DIGEST_SIZE
,
1584 .cra_name
= "sha224",
1585 .cra_driver_name
= "omap-sha224",
1586 .cra_priority
= 400,
1587 .cra_flags
= CRYPTO_ALG_ASYNC
|
1588 CRYPTO_ALG_NEED_FALLBACK
,
1589 .cra_blocksize
= SHA224_BLOCK_SIZE
,
1590 .cra_ctxsize
= sizeof(struct omap_sham_ctx
),
1591 .cra_alignmask
= OMAP_ALIGN_MASK
,
1592 .cra_module
= THIS_MODULE
,
1593 .cra_init
= omap_sham_cra_init
,
1594 .cra_exit
= omap_sham_cra_exit
,
1598 .init
= omap_sham_init
,
1599 .update
= omap_sham_update
,
1600 .final
= omap_sham_final
,
1601 .finup
= omap_sham_finup
,
1602 .digest
= omap_sham_digest
,
1603 .halg
.digestsize
= SHA256_DIGEST_SIZE
,
1605 .cra_name
= "sha256",
1606 .cra_driver_name
= "omap-sha256",
1607 .cra_priority
= 400,
1608 .cra_flags
= CRYPTO_ALG_ASYNC
|
1609 CRYPTO_ALG_NEED_FALLBACK
,
1610 .cra_blocksize
= SHA256_BLOCK_SIZE
,
1611 .cra_ctxsize
= sizeof(struct omap_sham_ctx
),
1612 .cra_alignmask
= OMAP_ALIGN_MASK
,
1613 .cra_module
= THIS_MODULE
,
1614 .cra_init
= omap_sham_cra_init
,
1615 .cra_exit
= omap_sham_cra_exit
,
1619 .init
= omap_sham_init
,
1620 .update
= omap_sham_update
,
1621 .final
= omap_sham_final
,
1622 .finup
= omap_sham_finup
,
1623 .digest
= omap_sham_digest
,
1624 .setkey
= omap_sham_setkey
,
1625 .halg
.digestsize
= SHA224_DIGEST_SIZE
,
1627 .cra_name
= "hmac(sha224)",
1628 .cra_driver_name
= "omap-hmac-sha224",
1629 .cra_priority
= 400,
1630 .cra_flags
= CRYPTO_ALG_ASYNC
|
1631 CRYPTO_ALG_NEED_FALLBACK
,
1632 .cra_blocksize
= SHA224_BLOCK_SIZE
,
1633 .cra_ctxsize
= sizeof(struct omap_sham_ctx
) +
1634 sizeof(struct omap_sham_hmac_ctx
),
1635 .cra_alignmask
= OMAP_ALIGN_MASK
,
1636 .cra_module
= THIS_MODULE
,
1637 .cra_init
= omap_sham_cra_sha224_init
,
1638 .cra_exit
= omap_sham_cra_exit
,
1642 .init
= omap_sham_init
,
1643 .update
= omap_sham_update
,
1644 .final
= omap_sham_final
,
1645 .finup
= omap_sham_finup
,
1646 .digest
= omap_sham_digest
,
1647 .setkey
= omap_sham_setkey
,
1648 .halg
.digestsize
= SHA256_DIGEST_SIZE
,
1650 .cra_name
= "hmac(sha256)",
1651 .cra_driver_name
= "omap-hmac-sha256",
1652 .cra_priority
= 400,
1653 .cra_flags
= CRYPTO_ALG_ASYNC
|
1654 CRYPTO_ALG_NEED_FALLBACK
,
1655 .cra_blocksize
= SHA256_BLOCK_SIZE
,
1656 .cra_ctxsize
= sizeof(struct omap_sham_ctx
) +
1657 sizeof(struct omap_sham_hmac_ctx
),
1658 .cra_alignmask
= OMAP_ALIGN_MASK
,
1659 .cra_module
= THIS_MODULE
,
1660 .cra_init
= omap_sham_cra_sha256_init
,
1661 .cra_exit
= omap_sham_cra_exit
,
1666 static struct ahash_alg algs_sha384_sha512
[] = {
1668 .init
= omap_sham_init
,
1669 .update
= omap_sham_update
,
1670 .final
= omap_sham_final
,
1671 .finup
= omap_sham_finup
,
1672 .digest
= omap_sham_digest
,
1673 .halg
.digestsize
= SHA384_DIGEST_SIZE
,
1675 .cra_name
= "sha384",
1676 .cra_driver_name
= "omap-sha384",
1677 .cra_priority
= 400,
1678 .cra_flags
= CRYPTO_ALG_ASYNC
|
1679 CRYPTO_ALG_NEED_FALLBACK
,
1680 .cra_blocksize
= SHA384_BLOCK_SIZE
,
1681 .cra_ctxsize
= sizeof(struct omap_sham_ctx
),
1682 .cra_alignmask
= OMAP_ALIGN_MASK
,
1683 .cra_module
= THIS_MODULE
,
1684 .cra_init
= omap_sham_cra_init
,
1685 .cra_exit
= omap_sham_cra_exit
,
1689 .init
= omap_sham_init
,
1690 .update
= omap_sham_update
,
1691 .final
= omap_sham_final
,
1692 .finup
= omap_sham_finup
,
1693 .digest
= omap_sham_digest
,
1694 .halg
.digestsize
= SHA512_DIGEST_SIZE
,
1696 .cra_name
= "sha512",
1697 .cra_driver_name
= "omap-sha512",
1698 .cra_priority
= 400,
1699 .cra_flags
= CRYPTO_ALG_ASYNC
|
1700 CRYPTO_ALG_NEED_FALLBACK
,
1701 .cra_blocksize
= SHA512_BLOCK_SIZE
,
1702 .cra_ctxsize
= sizeof(struct omap_sham_ctx
),
1703 .cra_alignmask
= OMAP_ALIGN_MASK
,
1704 .cra_module
= THIS_MODULE
,
1705 .cra_init
= omap_sham_cra_init
,
1706 .cra_exit
= omap_sham_cra_exit
,
1710 .init
= omap_sham_init
,
1711 .update
= omap_sham_update
,
1712 .final
= omap_sham_final
,
1713 .finup
= omap_sham_finup
,
1714 .digest
= omap_sham_digest
,
1715 .setkey
= omap_sham_setkey
,
1716 .halg
.digestsize
= SHA384_DIGEST_SIZE
,
1718 .cra_name
= "hmac(sha384)",
1719 .cra_driver_name
= "omap-hmac-sha384",
1720 .cra_priority
= 400,
1721 .cra_flags
= CRYPTO_ALG_ASYNC
|
1722 CRYPTO_ALG_NEED_FALLBACK
,
1723 .cra_blocksize
= SHA384_BLOCK_SIZE
,
1724 .cra_ctxsize
= sizeof(struct omap_sham_ctx
) +
1725 sizeof(struct omap_sham_hmac_ctx
),
1726 .cra_alignmask
= OMAP_ALIGN_MASK
,
1727 .cra_module
= THIS_MODULE
,
1728 .cra_init
= omap_sham_cra_sha384_init
,
1729 .cra_exit
= omap_sham_cra_exit
,
1733 .init
= omap_sham_init
,
1734 .update
= omap_sham_update
,
1735 .final
= omap_sham_final
,
1736 .finup
= omap_sham_finup
,
1737 .digest
= omap_sham_digest
,
1738 .setkey
= omap_sham_setkey
,
1739 .halg
.digestsize
= SHA512_DIGEST_SIZE
,
1741 .cra_name
= "hmac(sha512)",
1742 .cra_driver_name
= "omap-hmac-sha512",
1743 .cra_priority
= 400,
1744 .cra_flags
= CRYPTO_ALG_ASYNC
|
1745 CRYPTO_ALG_NEED_FALLBACK
,
1746 .cra_blocksize
= SHA512_BLOCK_SIZE
,
1747 .cra_ctxsize
= sizeof(struct omap_sham_ctx
) +
1748 sizeof(struct omap_sham_hmac_ctx
),
1749 .cra_alignmask
= OMAP_ALIGN_MASK
,
1750 .cra_module
= THIS_MODULE
,
1751 .cra_init
= omap_sham_cra_sha512_init
,
1752 .cra_exit
= omap_sham_cra_exit
,
1757 static void omap_sham_done_task(unsigned long data
)
1759 struct omap_sham_dev
*dd
= (struct omap_sham_dev
*)data
;
1762 dev_dbg(dd
->dev
, "%s: flags=%lx\n", __func__
, dd
->flags
);
1764 if (!test_bit(FLAGS_BUSY
, &dd
->flags
)) {
1765 omap_sham_handle_queue(dd
, NULL
);
1769 if (test_bit(FLAGS_CPU
, &dd
->flags
)) {
1770 if (test_and_clear_bit(FLAGS_OUTPUT_READY
, &dd
->flags
))
1772 } else if (test_bit(FLAGS_DMA_READY
, &dd
->flags
)) {
1773 if (test_and_clear_bit(FLAGS_DMA_ACTIVE
, &dd
->flags
)) {
1774 omap_sham_update_dma_stop(dd
);
1780 if (test_and_clear_bit(FLAGS_OUTPUT_READY
, &dd
->flags
)) {
1781 /* hash or semi-hash ready */
1782 clear_bit(FLAGS_DMA_READY
, &dd
->flags
);
1790 dev_dbg(dd
->dev
, "update done: err: %d\n", err
);
1791 /* finish curent request */
1792 omap_sham_finish_req(dd
->req
, err
);
1794 /* If we are not busy, process next req */
1795 if (!test_bit(FLAGS_BUSY
, &dd
->flags
))
1796 omap_sham_handle_queue(dd
, NULL
);
1799 static irqreturn_t
omap_sham_irq_common(struct omap_sham_dev
*dd
)
1801 if (!test_bit(FLAGS_BUSY
, &dd
->flags
)) {
1802 dev_warn(dd
->dev
, "Interrupt when no active requests.\n");
1804 set_bit(FLAGS_OUTPUT_READY
, &dd
->flags
);
1805 tasklet_schedule(&dd
->done_task
);
1811 static irqreturn_t
omap_sham_irq_omap2(int irq
, void *dev_id
)
1813 struct omap_sham_dev
*dd
= dev_id
;
1815 if (unlikely(test_bit(FLAGS_FINAL
, &dd
->flags
)))
1816 /* final -> allow device to go to power-saving mode */
1817 omap_sham_write_mask(dd
, SHA_REG_CTRL
, 0, SHA_REG_CTRL_LENGTH
);
1819 omap_sham_write_mask(dd
, SHA_REG_CTRL
, SHA_REG_CTRL_OUTPUT_READY
,
1820 SHA_REG_CTRL_OUTPUT_READY
);
1821 omap_sham_read(dd
, SHA_REG_CTRL
);
1823 return omap_sham_irq_common(dd
);
1826 static irqreturn_t
omap_sham_irq_omap4(int irq
, void *dev_id
)
1828 struct omap_sham_dev
*dd
= dev_id
;
1830 omap_sham_write_mask(dd
, SHA_REG_MASK(dd
), 0, SHA_REG_MASK_IT_EN
);
1832 return omap_sham_irq_common(dd
);
1835 static struct omap_sham_algs_info omap_sham_algs_info_omap2
[] = {
1837 .algs_list
= algs_sha1_md5
,
1838 .size
= ARRAY_SIZE(algs_sha1_md5
),
1842 static const struct omap_sham_pdata omap_sham_pdata_omap2
= {
1843 .algs_info
= omap_sham_algs_info_omap2
,
1844 .algs_info_size
= ARRAY_SIZE(omap_sham_algs_info_omap2
),
1845 .flags
= BIT(FLAGS_BE32_SHA1
),
1846 .digest_size
= SHA1_DIGEST_SIZE
,
1847 .copy_hash
= omap_sham_copy_hash_omap2
,
1848 .write_ctrl
= omap_sham_write_ctrl_omap2
,
1849 .trigger
= omap_sham_trigger_omap2
,
1850 .poll_irq
= omap_sham_poll_irq_omap2
,
1851 .intr_hdlr
= omap_sham_irq_omap2
,
1852 .idigest_ofs
= 0x00,
1857 .sysstatus_ofs
= 0x64,
1865 static struct omap_sham_algs_info omap_sham_algs_info_omap4
[] = {
1867 .algs_list
= algs_sha1_md5
,
1868 .size
= ARRAY_SIZE(algs_sha1_md5
),
1871 .algs_list
= algs_sha224_sha256
,
1872 .size
= ARRAY_SIZE(algs_sha224_sha256
),
1876 static const struct omap_sham_pdata omap_sham_pdata_omap4
= {
1877 .algs_info
= omap_sham_algs_info_omap4
,
1878 .algs_info_size
= ARRAY_SIZE(omap_sham_algs_info_omap4
),
1879 .flags
= BIT(FLAGS_AUTO_XOR
),
1880 .digest_size
= SHA256_DIGEST_SIZE
,
1881 .copy_hash
= omap_sham_copy_hash_omap4
,
1882 .write_ctrl
= omap_sham_write_ctrl_omap4
,
1883 .trigger
= omap_sham_trigger_omap4
,
1884 .poll_irq
= omap_sham_poll_irq_omap4
,
1885 .intr_hdlr
= omap_sham_irq_omap4
,
1886 .idigest_ofs
= 0x020,
1889 .digcnt_ofs
= 0x040,
1892 .sysstatus_ofs
= 0x114,
1895 .major_mask
= 0x0700,
1897 .minor_mask
= 0x003f,
1901 static struct omap_sham_algs_info omap_sham_algs_info_omap5
[] = {
1903 .algs_list
= algs_sha1_md5
,
1904 .size
= ARRAY_SIZE(algs_sha1_md5
),
1907 .algs_list
= algs_sha224_sha256
,
1908 .size
= ARRAY_SIZE(algs_sha224_sha256
),
1911 .algs_list
= algs_sha384_sha512
,
1912 .size
= ARRAY_SIZE(algs_sha384_sha512
),
1916 static const struct omap_sham_pdata omap_sham_pdata_omap5
= {
1917 .algs_info
= omap_sham_algs_info_omap5
,
1918 .algs_info_size
= ARRAY_SIZE(omap_sham_algs_info_omap5
),
1919 .flags
= BIT(FLAGS_AUTO_XOR
),
1920 .digest_size
= SHA512_DIGEST_SIZE
,
1921 .copy_hash
= omap_sham_copy_hash_omap4
,
1922 .write_ctrl
= omap_sham_write_ctrl_omap4
,
1923 .trigger
= omap_sham_trigger_omap4
,
1924 .poll_irq
= omap_sham_poll_irq_omap4
,
1925 .intr_hdlr
= omap_sham_irq_omap4
,
1926 .idigest_ofs
= 0x240,
1927 .odigest_ofs
= 0x200,
1929 .digcnt_ofs
= 0x280,
1932 .sysstatus_ofs
= 0x114,
1934 .length_ofs
= 0x288,
1935 .major_mask
= 0x0700,
1937 .minor_mask
= 0x003f,
1941 static const struct of_device_id omap_sham_of_match
[] = {
1943 .compatible
= "ti,omap2-sham",
1944 .data
= &omap_sham_pdata_omap2
,
1947 .compatible
= "ti,omap3-sham",
1948 .data
= &omap_sham_pdata_omap2
,
1951 .compatible
= "ti,omap4-sham",
1952 .data
= &omap_sham_pdata_omap4
,
1955 .compatible
= "ti,omap5-sham",
1956 .data
= &omap_sham_pdata_omap5
,
1960 MODULE_DEVICE_TABLE(of
, omap_sham_of_match
);
1962 static int omap_sham_get_res_of(struct omap_sham_dev
*dd
,
1963 struct device
*dev
, struct resource
*res
)
1965 struct device_node
*node
= dev
->of_node
;
1968 dd
->pdata
= of_device_get_match_data(dev
);
1970 dev_err(dev
, "no compatible OF match\n");
1975 err
= of_address_to_resource(node
, 0, res
);
1977 dev_err(dev
, "can't translate OF node address\n");
1982 dd
->irq
= irq_of_parse_and_map(node
, 0);
1984 dev_err(dev
, "can't translate OF irq value\n");
1993 static const struct of_device_id omap_sham_of_match
[] = {
1997 static int omap_sham_get_res_of(struct omap_sham_dev
*dd
,
1998 struct device
*dev
, struct resource
*res
)
2004 static int omap_sham_get_res_pdev(struct omap_sham_dev
*dd
,
2005 struct platform_device
*pdev
, struct resource
*res
)
2007 struct device
*dev
= &pdev
->dev
;
2011 /* Get the base address */
2012 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2014 dev_err(dev
, "no MEM resource info\n");
2018 memcpy(res
, r
, sizeof(*res
));
2021 dd
->irq
= platform_get_irq(pdev
, 0);
2027 /* Only OMAP2/3 can be non-DT */
2028 dd
->pdata
= &omap_sham_pdata_omap2
;
2034 static ssize_t
fallback_show(struct device
*dev
, struct device_attribute
*attr
,
2037 struct omap_sham_dev
*dd
= dev_get_drvdata(dev
);
2039 return sprintf(buf
, "%d\n", dd
->fallback_sz
);
2042 static ssize_t
fallback_store(struct device
*dev
, struct device_attribute
*attr
,
2043 const char *buf
, size_t size
)
2045 struct omap_sham_dev
*dd
= dev_get_drvdata(dev
);
2049 status
= kstrtol(buf
, 0, &value
);
2053 /* HW accelerator only works with buffers > 9 */
2055 dev_err(dev
, "minimum fallback size 9\n");
2059 dd
->fallback_sz
= value
;
2064 static ssize_t
queue_len_show(struct device
*dev
, struct device_attribute
*attr
,
2067 struct omap_sham_dev
*dd
= dev_get_drvdata(dev
);
2069 return sprintf(buf
, "%d\n", dd
->queue
.max_qlen
);
2072 static ssize_t
queue_len_store(struct device
*dev
,
2073 struct device_attribute
*attr
, const char *buf
,
2076 struct omap_sham_dev
*dd
= dev_get_drvdata(dev
);
2079 unsigned long flags
;
2081 status
= kstrtol(buf
, 0, &value
);
2089 * Changing the queue size in fly is safe, if size becomes smaller
2090 * than current size, it will just not accept new entries until
2091 * it has shrank enough.
2093 spin_lock_irqsave(&dd
->lock
, flags
);
2094 dd
->queue
.max_qlen
= value
;
2095 spin_unlock_irqrestore(&dd
->lock
, flags
);
2100 static DEVICE_ATTR_RW(queue_len
);
2101 static DEVICE_ATTR_RW(fallback
);
2103 static struct attribute
*omap_sham_attrs
[] = {
2104 &dev_attr_queue_len
.attr
,
2105 &dev_attr_fallback
.attr
,
2109 static struct attribute_group omap_sham_attr_group
= {
2110 .attrs
= omap_sham_attrs
,
2113 static int omap_sham_probe(struct platform_device
*pdev
)
2115 struct omap_sham_dev
*dd
;
2116 struct device
*dev
= &pdev
->dev
;
2117 struct resource res
;
2118 dma_cap_mask_t mask
;
2122 dd
= devm_kzalloc(dev
, sizeof(struct omap_sham_dev
), GFP_KERNEL
);
2124 dev_err(dev
, "unable to alloc data struct.\n");
2129 platform_set_drvdata(pdev
, dd
);
2131 INIT_LIST_HEAD(&dd
->list
);
2132 spin_lock_init(&dd
->lock
);
2133 tasklet_init(&dd
->done_task
, omap_sham_done_task
, (unsigned long)dd
);
2134 crypto_init_queue(&dd
->queue
, OMAP_SHAM_QUEUE_LENGTH
);
2136 err
= (dev
->of_node
) ? omap_sham_get_res_of(dd
, dev
, &res
) :
2137 omap_sham_get_res_pdev(dd
, pdev
, &res
);
2141 dd
->io_base
= devm_ioremap_resource(dev
, &res
);
2142 if (IS_ERR(dd
->io_base
)) {
2143 err
= PTR_ERR(dd
->io_base
);
2146 dd
->phys_base
= res
.start
;
2148 err
= devm_request_irq(dev
, dd
->irq
, dd
->pdata
->intr_hdlr
,
2149 IRQF_TRIGGER_NONE
, dev_name(dev
), dd
);
2151 dev_err(dev
, "unable to request irq %d, err = %d\n",
2157 dma_cap_set(DMA_SLAVE
, mask
);
2159 dd
->dma_lch
= dma_request_chan(dev
, "rx");
2160 if (IS_ERR(dd
->dma_lch
)) {
2161 err
= PTR_ERR(dd
->dma_lch
);
2162 if (err
== -EPROBE_DEFER
)
2165 dd
->polling_mode
= 1;
2166 dev_dbg(dev
, "using polling mode instead of dma\n");
2169 dd
->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 for (j
= 0; j
< dd
->pdata
->algs_info
[i
].size
; j
++) {
2198 struct ahash_alg
*alg
;
2200 alg
= &dd
->pdata
->algs_info
[i
].algs_list
[j
];
2201 alg
->export
= omap_sham_export
;
2202 alg
->import
= omap_sham_import
;
2203 alg
->halg
.statesize
= sizeof(struct omap_sham_reqctx
) +
2205 err
= crypto_register_ahash(alg
);
2209 dd
->pdata
->algs_info
[i
].registered
++;
2213 err
= sysfs_create_group(&dev
->kobj
, &omap_sham_attr_group
);
2215 dev_err(dev
, "could not create sysfs device attrs\n");
2222 for (i
= dd
->pdata
->algs_info_size
- 1; i
>= 0; i
--)
2223 for (j
= dd
->pdata
->algs_info
[i
].registered
- 1; j
>= 0; j
--)
2224 crypto_unregister_ahash(
2225 &dd
->pdata
->algs_info
[i
].algs_list
[j
]);
2227 pm_runtime_disable(dev
);
2228 if (!dd
->polling_mode
)
2229 dma_release_channel(dd
->dma_lch
);
2231 dev_err(dev
, "initialization failed.\n");
2236 static int omap_sham_remove(struct platform_device
*pdev
)
2238 struct omap_sham_dev
*dd
;
2241 dd
= platform_get_drvdata(pdev
);
2244 spin_lock(&sham
.lock
);
2245 list_del(&dd
->list
);
2246 spin_unlock(&sham
.lock
);
2247 for (i
= dd
->pdata
->algs_info_size
- 1; i
>= 0; i
--)
2248 for (j
= dd
->pdata
->algs_info
[i
].registered
- 1; j
>= 0; j
--)
2249 crypto_unregister_ahash(
2250 &dd
->pdata
->algs_info
[i
].algs_list
[j
]);
2251 tasklet_kill(&dd
->done_task
);
2252 pm_runtime_disable(&pdev
->dev
);
2254 if (!dd
->polling_mode
)
2255 dma_release_channel(dd
->dma_lch
);
2257 sysfs_remove_group(&dd
->dev
->kobj
, &omap_sham_attr_group
);
2262 #ifdef CONFIG_PM_SLEEP
2263 static int omap_sham_suspend(struct device
*dev
)
2265 pm_runtime_put_sync(dev
);
2269 static int omap_sham_resume(struct device
*dev
)
2271 int err
= pm_runtime_get_sync(dev
);
2273 dev_err(dev
, "failed to get sync: %d\n", err
);
2280 static SIMPLE_DEV_PM_OPS(omap_sham_pm_ops
, omap_sham_suspend
, omap_sham_resume
);
2282 static struct platform_driver omap_sham_driver
= {
2283 .probe
= omap_sham_probe
,
2284 .remove
= omap_sham_remove
,
2286 .name
= "omap-sham",
2287 .pm
= &omap_sham_pm_ops
,
2288 .of_match_table
= omap_sham_of_match
,
2292 module_platform_driver(omap_sham_driver
);
2294 MODULE_DESCRIPTION("OMAP SHA1/MD5 hw acceleration support.");
2295 MODULE_LICENSE("GPL v2");
2296 MODULE_AUTHOR("Dmitry Kasatkin");
2297 MODULE_ALIAS("platform:omap-sham");