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 <crypto/scatterwalk.h>
37 #include <crypto/algapi.h>
38 #include <crypto/sha1.h>
39 #include <crypto/sha2.h>
40 #include <crypto/hash.h>
41 #include <crypto/hmac.h>
42 #include <crypto/internal/hash.h>
43 #include <crypto/engine.h>
45 #define MD5_DIGEST_SIZE 16
47 #define SHA_REG_IDIGEST(dd, x) ((dd)->pdata->idigest_ofs + ((x)*0x04))
48 #define SHA_REG_DIN(dd, x) ((dd)->pdata->din_ofs + ((x) * 0x04))
49 #define SHA_REG_DIGCNT(dd) ((dd)->pdata->digcnt_ofs)
51 #define SHA_REG_ODIGEST(dd, x) ((dd)->pdata->odigest_ofs + (x * 0x04))
53 #define SHA_REG_CTRL 0x18
54 #define SHA_REG_CTRL_LENGTH (0xFFFFFFFF << 5)
55 #define SHA_REG_CTRL_CLOSE_HASH (1 << 4)
56 #define SHA_REG_CTRL_ALGO_CONST (1 << 3)
57 #define SHA_REG_CTRL_ALGO (1 << 2)
58 #define SHA_REG_CTRL_INPUT_READY (1 << 1)
59 #define SHA_REG_CTRL_OUTPUT_READY (1 << 0)
61 #define SHA_REG_REV(dd) ((dd)->pdata->rev_ofs)
63 #define SHA_REG_MASK(dd) ((dd)->pdata->mask_ofs)
64 #define SHA_REG_MASK_DMA_EN (1 << 3)
65 #define SHA_REG_MASK_IT_EN (1 << 2)
66 #define SHA_REG_MASK_SOFTRESET (1 << 1)
67 #define SHA_REG_AUTOIDLE (1 << 0)
69 #define SHA_REG_SYSSTATUS(dd) ((dd)->pdata->sysstatus_ofs)
70 #define SHA_REG_SYSSTATUS_RESETDONE (1 << 0)
72 #define SHA_REG_MODE(dd) ((dd)->pdata->mode_ofs)
73 #define SHA_REG_MODE_HMAC_OUTER_HASH (1 << 7)
74 #define SHA_REG_MODE_HMAC_KEY_PROC (1 << 5)
75 #define SHA_REG_MODE_CLOSE_HASH (1 << 4)
76 #define SHA_REG_MODE_ALGO_CONSTANT (1 << 3)
78 #define SHA_REG_MODE_ALGO_MASK (7 << 0)
79 #define SHA_REG_MODE_ALGO_MD5_128 (0 << 1)
80 #define SHA_REG_MODE_ALGO_SHA1_160 (1 << 1)
81 #define SHA_REG_MODE_ALGO_SHA2_224 (2 << 1)
82 #define SHA_REG_MODE_ALGO_SHA2_256 (3 << 1)
83 #define SHA_REG_MODE_ALGO_SHA2_384 (1 << 0)
84 #define SHA_REG_MODE_ALGO_SHA2_512 (3 << 0)
86 #define SHA_REG_LENGTH(dd) ((dd)->pdata->length_ofs)
88 #define SHA_REG_IRQSTATUS 0x118
89 #define SHA_REG_IRQSTATUS_CTX_RDY (1 << 3)
90 #define SHA_REG_IRQSTATUS_PARTHASH_RDY (1 << 2)
91 #define SHA_REG_IRQSTATUS_INPUT_RDY (1 << 1)
92 #define SHA_REG_IRQSTATUS_OUTPUT_RDY (1 << 0)
94 #define SHA_REG_IRQENA 0x11C
95 #define SHA_REG_IRQENA_CTX_RDY (1 << 3)
96 #define SHA_REG_IRQENA_PARTHASH_RDY (1 << 2)
97 #define SHA_REG_IRQENA_INPUT_RDY (1 << 1)
98 #define SHA_REG_IRQENA_OUTPUT_RDY (1 << 0)
100 #define DEFAULT_TIMEOUT_INTERVAL HZ
102 #define DEFAULT_AUTOSUSPEND_DELAY 1000
104 /* mostly device flags */
105 #define FLAGS_FINAL 1
106 #define FLAGS_DMA_ACTIVE 2
107 #define FLAGS_OUTPUT_READY 3
109 #define FLAGS_DMA_READY 6
110 #define FLAGS_AUTO_XOR 7
111 #define FLAGS_BE32_SHA1 8
112 #define FLAGS_SGS_COPIED 9
113 #define FLAGS_SGS_ALLOCED 10
114 #define FLAGS_HUGE 11
117 #define FLAGS_FINUP 16
119 #define FLAGS_MODE_SHIFT 18
120 #define FLAGS_MODE_MASK (SHA_REG_MODE_ALGO_MASK << FLAGS_MODE_SHIFT)
121 #define FLAGS_MODE_MD5 (SHA_REG_MODE_ALGO_MD5_128 << FLAGS_MODE_SHIFT)
122 #define FLAGS_MODE_SHA1 (SHA_REG_MODE_ALGO_SHA1_160 << FLAGS_MODE_SHIFT)
123 #define FLAGS_MODE_SHA224 (SHA_REG_MODE_ALGO_SHA2_224 << FLAGS_MODE_SHIFT)
124 #define FLAGS_MODE_SHA256 (SHA_REG_MODE_ALGO_SHA2_256 << FLAGS_MODE_SHIFT)
125 #define FLAGS_MODE_SHA384 (SHA_REG_MODE_ALGO_SHA2_384 << FLAGS_MODE_SHIFT)
126 #define FLAGS_MODE_SHA512 (SHA_REG_MODE_ALGO_SHA2_512 << FLAGS_MODE_SHIFT)
128 #define FLAGS_HMAC 21
129 #define FLAGS_ERROR 22
134 #define OMAP_ALIGN_MASK (sizeof(u32)-1)
135 #define OMAP_ALIGNED __attribute__((aligned(sizeof(u32))))
137 #define BUFLEN SHA512_BLOCK_SIZE
138 #define OMAP_SHA_DMA_THRESHOLD 256
140 #define OMAP_SHA_MAX_DMA_LEN (1024 * 2048)
142 struct omap_sham_dev;
144 struct omap_sham_reqctx {
145 struct omap_sham_dev *dd;
149 u8 digest[SHA512_DIGEST_SIZE] OMAP_ALIGNED;
155 struct scatterlist *sg;
156 struct scatterlist sgl[2];
157 int offset; /* offset in current sg */
159 unsigned int total; /* total request */
161 u8 buffer[] OMAP_ALIGNED;
164 struct omap_sham_hmac_ctx {
165 struct crypto_shash *shash;
166 u8 ipad[SHA512_BLOCK_SIZE] OMAP_ALIGNED;
167 u8 opad[SHA512_BLOCK_SIZE] OMAP_ALIGNED;
170 struct omap_sham_ctx {
171 struct crypto_engine_ctx enginectx;
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;
224 struct dma_chan *dma_lch;
225 struct tasklet_struct done_task;
227 u8 xmit_buf[BUFLEN] OMAP_ALIGNED;
231 struct crypto_queue queue;
232 struct ahash_request *req;
233 struct crypto_engine *engine;
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 int omap_sham_enqueue(struct ahash_request *req, unsigned int op);
250 static void omap_sham_finish_req(struct ahash_request *req, int err);
252 static inline u32 omap_sham_read(struct omap_sham_dev *dd, u32 offset)
254 return __raw_readl(dd->io_base + offset);
257 static inline void omap_sham_write(struct omap_sham_dev *dd,
258 u32 offset, u32 value)
260 __raw_writel(value, dd->io_base + offset);
263 static inline void omap_sham_write_mask(struct omap_sham_dev *dd, u32 address,
268 val = omap_sham_read(dd, address);
271 omap_sham_write(dd, address, val);
274 static inline int omap_sham_wait(struct omap_sham_dev *dd, u32 offset, u32 bit)
276 unsigned long timeout = jiffies + DEFAULT_TIMEOUT_INTERVAL;
278 while (!(omap_sham_read(dd, offset) & bit)) {
279 if (time_is_before_jiffies(timeout))
286 static void omap_sham_copy_hash_omap2(struct ahash_request *req, int out)
288 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
289 struct omap_sham_dev *dd = ctx->dd;
290 u32 *hash = (u32 *)ctx->digest;
293 for (i = 0; i < dd->pdata->digest_size / sizeof(u32); i++) {
295 hash[i] = omap_sham_read(dd, SHA_REG_IDIGEST(dd, i));
297 omap_sham_write(dd, SHA_REG_IDIGEST(dd, i), hash[i]);
301 static void omap_sham_copy_hash_omap4(struct ahash_request *req, int out)
303 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
304 struct omap_sham_dev *dd = ctx->dd;
307 if (ctx->flags & BIT(FLAGS_HMAC)) {
308 struct crypto_ahash *tfm = crypto_ahash_reqtfm(dd->req);
309 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
310 struct omap_sham_hmac_ctx *bctx = tctx->base;
311 u32 *opad = (u32 *)bctx->opad;
313 for (i = 0; i < dd->pdata->digest_size / sizeof(u32); i++) {
315 opad[i] = omap_sham_read(dd,
316 SHA_REG_ODIGEST(dd, i));
318 omap_sham_write(dd, SHA_REG_ODIGEST(dd, i),
323 omap_sham_copy_hash_omap2(req, out);
326 static void omap_sham_copy_ready_hash(struct ahash_request *req)
328 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
329 u32 *in = (u32 *)ctx->digest;
330 u32 *hash = (u32 *)req->result;
331 int i, d, big_endian = 0;
336 switch (ctx->flags & FLAGS_MODE_MASK) {
338 d = MD5_DIGEST_SIZE / sizeof(u32);
340 case FLAGS_MODE_SHA1:
341 /* OMAP2 SHA1 is big endian */
342 if (test_bit(FLAGS_BE32_SHA1, &ctx->dd->flags))
344 d = SHA1_DIGEST_SIZE / sizeof(u32);
346 case FLAGS_MODE_SHA224:
347 d = SHA224_DIGEST_SIZE / sizeof(u32);
349 case FLAGS_MODE_SHA256:
350 d = SHA256_DIGEST_SIZE / sizeof(u32);
352 case FLAGS_MODE_SHA384:
353 d = SHA384_DIGEST_SIZE / sizeof(u32);
355 case FLAGS_MODE_SHA512:
356 d = SHA512_DIGEST_SIZE / sizeof(u32);
363 for (i = 0; i < d; i++)
364 hash[i] = be32_to_cpup((__be32 *)in + i);
366 for (i = 0; i < d; i++)
367 hash[i] = le32_to_cpup((__le32 *)in + i);
370 static void omap_sham_write_ctrl_omap2(struct omap_sham_dev *dd, size_t length,
373 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
374 u32 val = length << 5, mask;
376 if (likely(ctx->digcnt))
377 omap_sham_write(dd, SHA_REG_DIGCNT(dd), ctx->digcnt);
379 omap_sham_write_mask(dd, SHA_REG_MASK(dd),
380 SHA_REG_MASK_IT_EN | (dma ? SHA_REG_MASK_DMA_EN : 0),
381 SHA_REG_MASK_IT_EN | SHA_REG_MASK_DMA_EN);
383 * Setting ALGO_CONST only for the first iteration
384 * and CLOSE_HASH only for the last one.
386 if ((ctx->flags & FLAGS_MODE_MASK) == FLAGS_MODE_SHA1)
387 val |= SHA_REG_CTRL_ALGO;
389 val |= SHA_REG_CTRL_ALGO_CONST;
391 val |= SHA_REG_CTRL_CLOSE_HASH;
393 mask = SHA_REG_CTRL_ALGO_CONST | SHA_REG_CTRL_CLOSE_HASH |
394 SHA_REG_CTRL_ALGO | SHA_REG_CTRL_LENGTH;
396 omap_sham_write_mask(dd, SHA_REG_CTRL, val, mask);
399 static void omap_sham_trigger_omap2(struct omap_sham_dev *dd, size_t length)
403 static int omap_sham_poll_irq_omap2(struct omap_sham_dev *dd)
405 return omap_sham_wait(dd, SHA_REG_CTRL, SHA_REG_CTRL_INPUT_READY);
408 static int get_block_size(struct omap_sham_reqctx *ctx)
412 switch (ctx->flags & FLAGS_MODE_MASK) {
414 case FLAGS_MODE_SHA1:
417 case FLAGS_MODE_SHA224:
418 case FLAGS_MODE_SHA256:
419 d = SHA256_BLOCK_SIZE;
421 case FLAGS_MODE_SHA384:
422 case FLAGS_MODE_SHA512:
423 d = SHA512_BLOCK_SIZE;
432 static void omap_sham_write_n(struct omap_sham_dev *dd, u32 offset,
433 u32 *value, int count)
435 for (; count--; value++, offset += 4)
436 omap_sham_write(dd, offset, *value);
439 static void omap_sham_write_ctrl_omap4(struct omap_sham_dev *dd, size_t length,
442 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
445 if (likely(ctx->digcnt))
446 omap_sham_write(dd, SHA_REG_DIGCNT(dd), ctx->digcnt);
449 * Setting ALGO_CONST only for the first iteration and
450 * CLOSE_HASH only for the last one. Note that flags mode bits
451 * correspond to algorithm encoding in mode register.
453 val = (ctx->flags & FLAGS_MODE_MASK) >> (FLAGS_MODE_SHIFT);
455 struct crypto_ahash *tfm = crypto_ahash_reqtfm(dd->req);
456 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
457 struct omap_sham_hmac_ctx *bctx = tctx->base;
460 val |= SHA_REG_MODE_ALGO_CONSTANT;
462 if (ctx->flags & BIT(FLAGS_HMAC)) {
463 bs = get_block_size(ctx);
464 nr_dr = bs / (2 * sizeof(u32));
465 val |= SHA_REG_MODE_HMAC_KEY_PROC;
466 omap_sham_write_n(dd, SHA_REG_ODIGEST(dd, 0),
467 (u32 *)bctx->ipad, nr_dr);
468 omap_sham_write_n(dd, SHA_REG_IDIGEST(dd, 0),
469 (u32 *)bctx->ipad + nr_dr, nr_dr);
475 val |= SHA_REG_MODE_CLOSE_HASH;
477 if (ctx->flags & BIT(FLAGS_HMAC))
478 val |= SHA_REG_MODE_HMAC_OUTER_HASH;
481 mask = SHA_REG_MODE_ALGO_CONSTANT | SHA_REG_MODE_CLOSE_HASH |
482 SHA_REG_MODE_ALGO_MASK | SHA_REG_MODE_HMAC_OUTER_HASH |
483 SHA_REG_MODE_HMAC_KEY_PROC;
485 dev_dbg(dd->dev, "ctrl: %08x, flags: %08lx\n", val, ctx->flags);
486 omap_sham_write_mask(dd, SHA_REG_MODE(dd), val, mask);
487 omap_sham_write(dd, SHA_REG_IRQENA, SHA_REG_IRQENA_OUTPUT_RDY);
488 omap_sham_write_mask(dd, SHA_REG_MASK(dd),
490 (dma ? SHA_REG_MASK_DMA_EN : 0),
491 SHA_REG_MASK_IT_EN | SHA_REG_MASK_DMA_EN);
494 static void omap_sham_trigger_omap4(struct omap_sham_dev *dd, size_t length)
496 omap_sham_write(dd, SHA_REG_LENGTH(dd), length);
499 static int omap_sham_poll_irq_omap4(struct omap_sham_dev *dd)
501 return omap_sham_wait(dd, SHA_REG_IRQSTATUS,
502 SHA_REG_IRQSTATUS_INPUT_RDY);
505 static int omap_sham_xmit_cpu(struct omap_sham_dev *dd, size_t length,
508 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
509 int count, len32, bs32, offset = 0;
512 struct sg_mapping_iter mi;
514 dev_dbg(dd->dev, "xmit_cpu: digcnt: %zd, length: %zd, final: %d\n",
515 ctx->digcnt, length, final);
517 dd->pdata->write_ctrl(dd, length, final, 0);
518 dd->pdata->trigger(dd, length);
520 /* should be non-zero before next lines to disable clocks later */
521 ctx->digcnt += length;
522 ctx->total -= length;
525 set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */
527 set_bit(FLAGS_CPU, &dd->flags);
529 len32 = DIV_ROUND_UP(length, sizeof(u32));
530 bs32 = get_block_size(ctx) / sizeof(u32);
532 sg_miter_start(&mi, ctx->sg, ctx->sg_len,
533 SG_MITER_FROM_SG | SG_MITER_ATOMIC);
538 if (dd->pdata->poll_irq(dd))
541 for (count = 0; count < min(len32, bs32); count++, offset++) {
546 pr_err("sg miter failure.\n");
552 omap_sham_write(dd, SHA_REG_DIN(dd, count),
556 len32 -= min(len32, bs32);
564 static void omap_sham_dma_callback(void *param)
566 struct omap_sham_dev *dd = param;
568 set_bit(FLAGS_DMA_READY, &dd->flags);
569 tasklet_schedule(&dd->done_task);
572 static int omap_sham_xmit_dma(struct omap_sham_dev *dd, size_t length,
575 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
576 struct dma_async_tx_descriptor *tx;
577 struct dma_slave_config cfg;
580 dev_dbg(dd->dev, "xmit_dma: digcnt: %zd, length: %zd, final: %d\n",
581 ctx->digcnt, length, final);
583 if (!dma_map_sg(dd->dev, ctx->sg, ctx->sg_len, DMA_TO_DEVICE)) {
584 dev_err(dd->dev, "dma_map_sg error\n");
588 memset(&cfg, 0, sizeof(cfg));
590 cfg.dst_addr = dd->phys_base + SHA_REG_DIN(dd, 0);
591 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
592 cfg.dst_maxburst = get_block_size(ctx) / DMA_SLAVE_BUSWIDTH_4_BYTES;
594 ret = dmaengine_slave_config(dd->dma_lch, &cfg);
596 pr_err("omap-sham: can't configure dmaengine slave: %d\n", ret);
600 tx = dmaengine_prep_slave_sg(dd->dma_lch, ctx->sg, ctx->sg_len,
602 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
605 dev_err(dd->dev, "prep_slave_sg failed\n");
609 tx->callback = omap_sham_dma_callback;
610 tx->callback_param = dd;
612 dd->pdata->write_ctrl(dd, length, final, 1);
614 ctx->digcnt += length;
615 ctx->total -= length;
618 set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */
620 set_bit(FLAGS_DMA_ACTIVE, &dd->flags);
622 dmaengine_submit(tx);
623 dma_async_issue_pending(dd->dma_lch);
625 dd->pdata->trigger(dd, length);
630 static int omap_sham_copy_sg_lists(struct omap_sham_reqctx *ctx,
631 struct scatterlist *sg, int bs, int new_len)
633 int n = sg_nents(sg);
634 struct scatterlist *tmp;
635 int offset = ctx->offset;
637 ctx->total = new_len;
642 ctx->sg = kmalloc_array(n, sizeof(*sg), GFP_KERNEL);
646 sg_init_table(ctx->sg, n);
653 sg_set_buf(tmp, ctx->dd->xmit_buf, ctx->bufcnt);
656 new_len -= ctx->bufcnt;
659 while (sg && new_len) {
660 int len = sg->length - offset;
663 offset -= sg->length;
673 sg_set_page(tmp, sg_page(sg), len, sg->offset + offset);
688 set_bit(FLAGS_SGS_ALLOCED, &ctx->dd->flags);
690 ctx->offset += new_len - ctx->bufcnt;
696 static int omap_sham_copy_sgs(struct omap_sham_reqctx *ctx,
697 struct scatterlist *sg, int bs,
698 unsigned int new_len)
703 pages = get_order(new_len);
705 buf = (void *)__get_free_pages(GFP_ATOMIC, pages);
707 pr_err("Couldn't allocate pages for unaligned cases.\n");
712 memcpy(buf, ctx->dd->xmit_buf, ctx->bufcnt);
714 scatterwalk_map_and_copy(buf + ctx->bufcnt, sg, ctx->offset,
715 min(new_len, ctx->total) - ctx->bufcnt, 0);
716 sg_init_table(ctx->sgl, 1);
717 sg_set_buf(ctx->sgl, buf, new_len);
719 set_bit(FLAGS_SGS_COPIED, &ctx->dd->flags);
721 ctx->offset += new_len - ctx->bufcnt;
723 ctx->total = new_len;
728 static int omap_sham_align_sgs(struct scatterlist *sg,
729 int nbytes, int bs, bool final,
730 struct omap_sham_reqctx *rctx)
735 struct scatterlist *sg_tmp = sg;
737 int offset = rctx->offset;
738 int bufcnt = rctx->bufcnt;
740 if (!sg || !sg->length || !nbytes) {
742 bufcnt = DIV_ROUND_UP(bufcnt, bs) * bs;
743 sg_init_table(rctx->sgl, 1);
744 sg_set_buf(rctx->sgl, rctx->dd->xmit_buf, bufcnt);
745 rctx->sg = rctx->sgl;
758 new_len = DIV_ROUND_UP(new_len, bs) * bs;
760 new_len = (new_len - 1) / bs * bs;
765 if (nbytes != new_len)
768 while (nbytes > 0 && sg_tmp) {
772 if (!IS_ALIGNED(bufcnt, bs)) {
784 #ifdef CONFIG_ZONE_DMA
785 if (page_zonenum(sg_page(sg_tmp)) != ZONE_DMA) {
791 if (offset < sg_tmp->length) {
792 if (!IS_ALIGNED(offset + sg_tmp->offset, 4)) {
797 if (!IS_ALIGNED(sg_tmp->length - offset, bs)) {
804 offset -= sg_tmp->length;
810 nbytes -= sg_tmp->length;
813 sg_tmp = sg_next(sg_tmp);
821 if (new_len > OMAP_SHA_MAX_DMA_LEN) {
822 new_len = OMAP_SHA_MAX_DMA_LEN;
827 return omap_sham_copy_sgs(rctx, sg, bs, new_len);
829 return omap_sham_copy_sg_lists(rctx, sg, bs, new_len);
831 rctx->total = new_len;
832 rctx->offset += new_len;
835 sg_init_table(rctx->sgl, 2);
836 sg_set_buf(rctx->sgl, rctx->dd->xmit_buf, rctx->bufcnt);
837 sg_chain(rctx->sgl, 2, sg);
838 rctx->sg = rctx->sgl;
846 static int omap_sham_prepare_request(struct crypto_engine *engine, void *areq)
848 struct ahash_request *req = container_of(areq, struct ahash_request,
850 struct omap_sham_reqctx *rctx = ahash_request_ctx(req);
854 bool final = rctx->flags & BIT(FLAGS_FINUP);
855 bool update = rctx->op == OP_UPDATE;
858 bs = get_block_size(rctx);
860 nbytes = rctx->bufcnt;
863 nbytes += req->nbytes - rctx->offset;
865 dev_dbg(rctx->dd->dev,
866 "%s: nbytes=%d, bs=%d, total=%d, offset=%d, bufcnt=%zd\n",
867 __func__, nbytes, bs, rctx->total, rctx->offset,
873 rctx->total = nbytes;
875 if (update && req->nbytes && (!IS_ALIGNED(rctx->bufcnt, bs))) {
876 int len = bs - rctx->bufcnt % bs;
878 if (len > req->nbytes)
880 scatterwalk_map_and_copy(rctx->buffer + rctx->bufcnt, req->src,
887 memcpy(rctx->dd->xmit_buf, rctx->buffer, rctx->bufcnt);
889 ret = omap_sham_align_sgs(req->src, nbytes, bs, final, rctx);
893 hash_later = nbytes - rctx->total;
897 if (hash_later && hash_later <= rctx->buflen) {
898 scatterwalk_map_and_copy(rctx->buffer,
900 req->nbytes - hash_later,
903 rctx->bufcnt = hash_later;
908 if (hash_later > rctx->buflen)
909 set_bit(FLAGS_HUGE, &rctx->dd->flags);
911 rctx->total = min(nbytes, rctx->total);
916 static int omap_sham_update_dma_stop(struct omap_sham_dev *dd)
918 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
920 dma_unmap_sg(dd->dev, ctx->sg, ctx->sg_len, DMA_TO_DEVICE);
922 clear_bit(FLAGS_DMA_ACTIVE, &dd->flags);
927 static struct omap_sham_dev *omap_sham_find_dev(struct omap_sham_reqctx *ctx)
929 struct omap_sham_dev *dd;
934 spin_lock_bh(&sham.lock);
935 dd = list_first_entry(&sham.dev_list, struct omap_sham_dev, list);
936 list_move_tail(&dd->list, &sham.dev_list);
938 spin_unlock_bh(&sham.lock);
943 static int omap_sham_init(struct ahash_request *req)
945 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
946 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
947 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
948 struct omap_sham_dev *dd;
953 dd = omap_sham_find_dev(ctx);
959 dev_dbg(dd->dev, "init: digest size: %d\n",
960 crypto_ahash_digestsize(tfm));
962 switch (crypto_ahash_digestsize(tfm)) {
963 case MD5_DIGEST_SIZE:
964 ctx->flags |= FLAGS_MODE_MD5;
965 bs = SHA1_BLOCK_SIZE;
967 case SHA1_DIGEST_SIZE:
968 ctx->flags |= FLAGS_MODE_SHA1;
969 bs = SHA1_BLOCK_SIZE;
971 case SHA224_DIGEST_SIZE:
972 ctx->flags |= FLAGS_MODE_SHA224;
973 bs = SHA224_BLOCK_SIZE;
975 case SHA256_DIGEST_SIZE:
976 ctx->flags |= FLAGS_MODE_SHA256;
977 bs = SHA256_BLOCK_SIZE;
979 case SHA384_DIGEST_SIZE:
980 ctx->flags |= FLAGS_MODE_SHA384;
981 bs = SHA384_BLOCK_SIZE;
983 case SHA512_DIGEST_SIZE:
984 ctx->flags |= FLAGS_MODE_SHA512;
985 bs = SHA512_BLOCK_SIZE;
993 ctx->buflen = BUFLEN;
995 if (tctx->flags & BIT(FLAGS_HMAC)) {
996 if (!test_bit(FLAGS_AUTO_XOR, &dd->flags)) {
997 struct omap_sham_hmac_ctx *bctx = tctx->base;
999 memcpy(ctx->buffer, bctx->ipad, bs);
1003 ctx->flags |= BIT(FLAGS_HMAC);
1010 static int omap_sham_update_req(struct omap_sham_dev *dd)
1012 struct ahash_request *req = dd->req;
1013 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1015 bool final = (ctx->flags & BIT(FLAGS_FINUP)) &&
1016 !(dd->flags & BIT(FLAGS_HUGE));
1018 dev_dbg(dd->dev, "update_req: total: %u, digcnt: %zd, final: %d",
1019 ctx->total, ctx->digcnt, final);
1021 if (ctx->total < get_block_size(ctx) ||
1022 ctx->total < dd->fallback_sz)
1023 ctx->flags |= BIT(FLAGS_CPU);
1025 if (ctx->flags & BIT(FLAGS_CPU))
1026 err = omap_sham_xmit_cpu(dd, ctx->total, final);
1028 err = omap_sham_xmit_dma(dd, ctx->total, final);
1030 /* wait for dma completion before can take more data */
1031 dev_dbg(dd->dev, "update: err: %d, digcnt: %zd\n", err, ctx->digcnt);
1036 static int omap_sham_final_req(struct omap_sham_dev *dd)
1038 struct ahash_request *req = dd->req;
1039 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1040 int err = 0, use_dma = 1;
1042 if (dd->flags & BIT(FLAGS_HUGE))
1045 if ((ctx->total <= get_block_size(ctx)) || dd->polling_mode)
1047 * faster to handle last block with cpu or
1048 * use cpu when dma is not present.
1053 err = omap_sham_xmit_dma(dd, ctx->total, 1);
1055 err = omap_sham_xmit_cpu(dd, ctx->total, 1);
1059 dev_dbg(dd->dev, "final_req: err: %d\n", err);
1064 static int omap_sham_hash_one_req(struct crypto_engine *engine, void *areq)
1066 struct ahash_request *req = container_of(areq, struct ahash_request,
1068 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1069 struct omap_sham_dev *dd = ctx->dd;
1071 bool final = (ctx->flags & BIT(FLAGS_FINUP)) &&
1072 !(dd->flags & BIT(FLAGS_HUGE));
1074 dev_dbg(dd->dev, "hash-one: op: %u, total: %u, digcnt: %zd, final: %d",
1075 ctx->op, ctx->total, ctx->digcnt, final);
1077 err = pm_runtime_resume_and_get(dd->dev);
1079 dev_err(dd->dev, "failed to get sync: %d\n", err);
1087 dd->pdata->copy_hash(req, 0);
1089 if (ctx->op == OP_UPDATE)
1090 err = omap_sham_update_req(dd);
1091 else if (ctx->op == OP_FINAL)
1092 err = omap_sham_final_req(dd);
1094 if (err != -EINPROGRESS)
1095 omap_sham_finish_req(req, err);
1100 static int omap_sham_finish_hmac(struct ahash_request *req)
1102 struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
1103 struct omap_sham_hmac_ctx *bctx = tctx->base;
1104 int bs = crypto_shash_blocksize(bctx->shash);
1105 int ds = crypto_shash_digestsize(bctx->shash);
1106 SHASH_DESC_ON_STACK(shash, bctx->shash);
1108 shash->tfm = bctx->shash;
1110 return crypto_shash_init(shash) ?:
1111 crypto_shash_update(shash, bctx->opad, bs) ?:
1112 crypto_shash_finup(shash, req->result, ds, req->result);
1115 static int omap_sham_finish(struct ahash_request *req)
1117 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1118 struct omap_sham_dev *dd = ctx->dd;
1122 omap_sham_copy_ready_hash(req);
1123 if ((ctx->flags & BIT(FLAGS_HMAC)) &&
1124 !test_bit(FLAGS_AUTO_XOR, &dd->flags))
1125 err = omap_sham_finish_hmac(req);
1128 dev_dbg(dd->dev, "digcnt: %zd, bufcnt: %zd\n", ctx->digcnt, ctx->bufcnt);
1133 static void omap_sham_finish_req(struct ahash_request *req, int err)
1135 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1136 struct omap_sham_dev *dd = ctx->dd;
1138 if (test_bit(FLAGS_SGS_COPIED, &dd->flags))
1139 free_pages((unsigned long)sg_virt(ctx->sg),
1140 get_order(ctx->sg->length));
1142 if (test_bit(FLAGS_SGS_ALLOCED, &dd->flags))
1147 dd->flags &= ~(BIT(FLAGS_SGS_ALLOCED) | BIT(FLAGS_SGS_COPIED) |
1148 BIT(FLAGS_CPU) | BIT(FLAGS_DMA_READY) |
1149 BIT(FLAGS_OUTPUT_READY));
1152 dd->pdata->copy_hash(req, 1);
1154 if (dd->flags & BIT(FLAGS_HUGE)) {
1155 /* Re-enqueue the request */
1156 omap_sham_enqueue(req, ctx->op);
1161 if (test_bit(FLAGS_FINAL, &dd->flags))
1162 err = omap_sham_finish(req);
1164 ctx->flags |= BIT(FLAGS_ERROR);
1167 /* atomic operation is not needed here */
1168 dd->flags &= ~(BIT(FLAGS_FINAL) | BIT(FLAGS_CPU) |
1169 BIT(FLAGS_DMA_READY) | BIT(FLAGS_OUTPUT_READY));
1171 pm_runtime_mark_last_busy(dd->dev);
1172 pm_runtime_put_autosuspend(dd->dev);
1176 crypto_finalize_hash_request(dd->engine, req, err);
1179 static int omap_sham_handle_queue(struct omap_sham_dev *dd,
1180 struct ahash_request *req)
1182 return crypto_transfer_hash_request_to_engine(dd->engine, req);
1185 static int omap_sham_enqueue(struct ahash_request *req, unsigned int op)
1187 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1188 struct omap_sham_dev *dd = ctx->dd;
1192 return omap_sham_handle_queue(dd, req);
1195 static int omap_sham_update(struct ahash_request *req)
1197 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1198 struct omap_sham_dev *dd = omap_sham_find_dev(ctx);
1203 if (ctx->bufcnt + req->nbytes <= ctx->buflen) {
1204 scatterwalk_map_and_copy(ctx->buffer + ctx->bufcnt, req->src,
1206 ctx->bufcnt += req->nbytes;
1210 if (dd->polling_mode)
1211 ctx->flags |= BIT(FLAGS_CPU);
1213 return omap_sham_enqueue(req, OP_UPDATE);
1216 static int omap_sham_final_shash(struct ahash_request *req)
1218 struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
1219 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1223 * If we are running HMAC on limited hardware support, skip
1224 * the ipad in the beginning of the buffer if we are going for
1225 * software fallback algorithm.
1227 if (test_bit(FLAGS_HMAC, &ctx->flags) &&
1228 !test_bit(FLAGS_AUTO_XOR, &ctx->dd->flags))
1229 offset = get_block_size(ctx);
1231 return crypto_shash_tfm_digest(tctx->fallback, ctx->buffer + offset,
1232 ctx->bufcnt - offset, req->result);
1235 static int omap_sham_final(struct ahash_request *req)
1237 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1239 ctx->flags |= BIT(FLAGS_FINUP);
1241 if (ctx->flags & BIT(FLAGS_ERROR))
1242 return 0; /* uncompleted hash is not needed */
1245 * OMAP HW accel works only with buffers >= 9.
1246 * HMAC is always >= 9 because ipad == block size.
1247 * If buffersize is less than fallback_sz, we use fallback
1248 * SW encoding, as using DMA + HW in this case doesn't provide
1251 if (!ctx->digcnt && ctx->bufcnt < ctx->dd->fallback_sz)
1252 return omap_sham_final_shash(req);
1253 else if (ctx->bufcnt)
1254 return omap_sham_enqueue(req, OP_FINAL);
1256 /* copy ready hash (+ finalize hmac) */
1257 return omap_sham_finish(req);
1260 static int omap_sham_finup(struct ahash_request *req)
1262 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1265 ctx->flags |= BIT(FLAGS_FINUP);
1267 err1 = omap_sham_update(req);
1268 if (err1 == -EINPROGRESS || err1 == -EBUSY)
1271 * final() has to be always called to cleanup resources
1272 * even if udpate() failed, except EINPROGRESS
1274 err2 = omap_sham_final(req);
1276 return err1 ?: err2;
1279 static int omap_sham_digest(struct ahash_request *req)
1281 return omap_sham_init(req) ?: omap_sham_finup(req);
1284 static int omap_sham_setkey(struct crypto_ahash *tfm, const u8 *key,
1285 unsigned int keylen)
1287 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
1288 struct omap_sham_hmac_ctx *bctx = tctx->base;
1289 int bs = crypto_shash_blocksize(bctx->shash);
1290 int ds = crypto_shash_digestsize(bctx->shash);
1293 err = crypto_shash_setkey(tctx->fallback, key, keylen);
1298 err = crypto_shash_tfm_digest(bctx->shash, key, keylen,
1304 memcpy(bctx->ipad, key, keylen);
1307 memset(bctx->ipad + keylen, 0, bs - keylen);
1309 if (!test_bit(FLAGS_AUTO_XOR, &sham.flags)) {
1310 memcpy(bctx->opad, bctx->ipad, bs);
1312 for (i = 0; i < bs; i++) {
1313 bctx->ipad[i] ^= HMAC_IPAD_VALUE;
1314 bctx->opad[i] ^= HMAC_OPAD_VALUE;
1321 static int omap_sham_cra_init_alg(struct crypto_tfm *tfm, const char *alg_base)
1323 struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm);
1324 const char *alg_name = crypto_tfm_alg_name(tfm);
1326 /* Allocate a fallback and abort if it failed. */
1327 tctx->fallback = crypto_alloc_shash(alg_name, 0,
1328 CRYPTO_ALG_NEED_FALLBACK);
1329 if (IS_ERR(tctx->fallback)) {
1330 pr_err("omap-sham: fallback driver '%s' "
1331 "could not be loaded.\n", alg_name);
1332 return PTR_ERR(tctx->fallback);
1335 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
1336 sizeof(struct omap_sham_reqctx) + BUFLEN);
1339 struct omap_sham_hmac_ctx *bctx = tctx->base;
1340 tctx->flags |= BIT(FLAGS_HMAC);
1341 bctx->shash = crypto_alloc_shash(alg_base, 0,
1342 CRYPTO_ALG_NEED_FALLBACK);
1343 if (IS_ERR(bctx->shash)) {
1344 pr_err("omap-sham: base driver '%s' "
1345 "could not be loaded.\n", alg_base);
1346 crypto_free_shash(tctx->fallback);
1347 return PTR_ERR(bctx->shash);
1352 tctx->enginectx.op.do_one_request = omap_sham_hash_one_req;
1353 tctx->enginectx.op.prepare_request = omap_sham_prepare_request;
1354 tctx->enginectx.op.unprepare_request = NULL;
1359 static int omap_sham_cra_init(struct crypto_tfm *tfm)
1361 return omap_sham_cra_init_alg(tfm, NULL);
1364 static int omap_sham_cra_sha1_init(struct crypto_tfm *tfm)
1366 return omap_sham_cra_init_alg(tfm, "sha1");
1369 static int omap_sham_cra_sha224_init(struct crypto_tfm *tfm)
1371 return omap_sham_cra_init_alg(tfm, "sha224");
1374 static int omap_sham_cra_sha256_init(struct crypto_tfm *tfm)
1376 return omap_sham_cra_init_alg(tfm, "sha256");
1379 static int omap_sham_cra_md5_init(struct crypto_tfm *tfm)
1381 return omap_sham_cra_init_alg(tfm, "md5");
1384 static int omap_sham_cra_sha384_init(struct crypto_tfm *tfm)
1386 return omap_sham_cra_init_alg(tfm, "sha384");
1389 static int omap_sham_cra_sha512_init(struct crypto_tfm *tfm)
1391 return omap_sham_cra_init_alg(tfm, "sha512");
1394 static void omap_sham_cra_exit(struct crypto_tfm *tfm)
1396 struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm);
1398 crypto_free_shash(tctx->fallback);
1399 tctx->fallback = NULL;
1401 if (tctx->flags & BIT(FLAGS_HMAC)) {
1402 struct omap_sham_hmac_ctx *bctx = tctx->base;
1403 crypto_free_shash(bctx->shash);
1407 static int omap_sham_export(struct ahash_request *req, void *out)
1409 struct omap_sham_reqctx *rctx = ahash_request_ctx(req);
1411 memcpy(out, rctx, sizeof(*rctx) + rctx->bufcnt);
1416 static int omap_sham_import(struct ahash_request *req, const void *in)
1418 struct omap_sham_reqctx *rctx = ahash_request_ctx(req);
1419 const struct omap_sham_reqctx *ctx_in = in;
1421 memcpy(rctx, in, sizeof(*rctx) + ctx_in->bufcnt);
1426 static struct ahash_alg algs_sha1_md5[] = {
1428 .init = omap_sham_init,
1429 .update = omap_sham_update,
1430 .final = omap_sham_final,
1431 .finup = omap_sham_finup,
1432 .digest = omap_sham_digest,
1433 .halg.digestsize = SHA1_DIGEST_SIZE,
1436 .cra_driver_name = "omap-sha1",
1437 .cra_priority = 400,
1438 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
1440 CRYPTO_ALG_NEED_FALLBACK,
1441 .cra_blocksize = SHA1_BLOCK_SIZE,
1442 .cra_ctxsize = sizeof(struct omap_sham_ctx),
1443 .cra_alignmask = OMAP_ALIGN_MASK,
1444 .cra_module = THIS_MODULE,
1445 .cra_init = omap_sham_cra_init,
1446 .cra_exit = omap_sham_cra_exit,
1450 .init = omap_sham_init,
1451 .update = omap_sham_update,
1452 .final = omap_sham_final,
1453 .finup = omap_sham_finup,
1454 .digest = omap_sham_digest,
1455 .halg.digestsize = MD5_DIGEST_SIZE,
1458 .cra_driver_name = "omap-md5",
1459 .cra_priority = 400,
1460 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
1462 CRYPTO_ALG_NEED_FALLBACK,
1463 .cra_blocksize = SHA1_BLOCK_SIZE,
1464 .cra_ctxsize = sizeof(struct omap_sham_ctx),
1465 .cra_alignmask = OMAP_ALIGN_MASK,
1466 .cra_module = THIS_MODULE,
1467 .cra_init = omap_sham_cra_init,
1468 .cra_exit = omap_sham_cra_exit,
1472 .init = omap_sham_init,
1473 .update = omap_sham_update,
1474 .final = omap_sham_final,
1475 .finup = omap_sham_finup,
1476 .digest = omap_sham_digest,
1477 .setkey = omap_sham_setkey,
1478 .halg.digestsize = SHA1_DIGEST_SIZE,
1480 .cra_name = "hmac(sha1)",
1481 .cra_driver_name = "omap-hmac-sha1",
1482 .cra_priority = 400,
1483 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
1485 CRYPTO_ALG_NEED_FALLBACK,
1486 .cra_blocksize = SHA1_BLOCK_SIZE,
1487 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1488 sizeof(struct omap_sham_hmac_ctx),
1489 .cra_alignmask = OMAP_ALIGN_MASK,
1490 .cra_module = THIS_MODULE,
1491 .cra_init = omap_sham_cra_sha1_init,
1492 .cra_exit = omap_sham_cra_exit,
1496 .init = omap_sham_init,
1497 .update = omap_sham_update,
1498 .final = omap_sham_final,
1499 .finup = omap_sham_finup,
1500 .digest = omap_sham_digest,
1501 .setkey = omap_sham_setkey,
1502 .halg.digestsize = MD5_DIGEST_SIZE,
1504 .cra_name = "hmac(md5)",
1505 .cra_driver_name = "omap-hmac-md5",
1506 .cra_priority = 400,
1507 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
1509 CRYPTO_ALG_NEED_FALLBACK,
1510 .cra_blocksize = SHA1_BLOCK_SIZE,
1511 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1512 sizeof(struct omap_sham_hmac_ctx),
1513 .cra_alignmask = OMAP_ALIGN_MASK,
1514 .cra_module = THIS_MODULE,
1515 .cra_init = omap_sham_cra_md5_init,
1516 .cra_exit = omap_sham_cra_exit,
1521 /* OMAP4 has some algs in addition to what OMAP2 has */
1522 static struct ahash_alg algs_sha224_sha256[] = {
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 .halg.digestsize = SHA224_DIGEST_SIZE,
1531 .cra_name = "sha224",
1532 .cra_driver_name = "omap-sha224",
1533 .cra_priority = 400,
1534 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
1536 CRYPTO_ALG_NEED_FALLBACK,
1537 .cra_blocksize = SHA224_BLOCK_SIZE,
1538 .cra_ctxsize = sizeof(struct omap_sham_ctx),
1539 .cra_alignmask = OMAP_ALIGN_MASK,
1540 .cra_module = THIS_MODULE,
1541 .cra_init = omap_sham_cra_init,
1542 .cra_exit = omap_sham_cra_exit,
1546 .init = omap_sham_init,
1547 .update = omap_sham_update,
1548 .final = omap_sham_final,
1549 .finup = omap_sham_finup,
1550 .digest = omap_sham_digest,
1551 .halg.digestsize = SHA256_DIGEST_SIZE,
1553 .cra_name = "sha256",
1554 .cra_driver_name = "omap-sha256",
1555 .cra_priority = 400,
1556 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
1558 CRYPTO_ALG_NEED_FALLBACK,
1559 .cra_blocksize = SHA256_BLOCK_SIZE,
1560 .cra_ctxsize = sizeof(struct omap_sham_ctx),
1561 .cra_alignmask = OMAP_ALIGN_MASK,
1562 .cra_module = THIS_MODULE,
1563 .cra_init = omap_sham_cra_init,
1564 .cra_exit = omap_sham_cra_exit,
1568 .init = omap_sham_init,
1569 .update = omap_sham_update,
1570 .final = omap_sham_final,
1571 .finup = omap_sham_finup,
1572 .digest = omap_sham_digest,
1573 .setkey = omap_sham_setkey,
1574 .halg.digestsize = SHA224_DIGEST_SIZE,
1576 .cra_name = "hmac(sha224)",
1577 .cra_driver_name = "omap-hmac-sha224",
1578 .cra_priority = 400,
1579 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
1581 CRYPTO_ALG_NEED_FALLBACK,
1582 .cra_blocksize = SHA224_BLOCK_SIZE,
1583 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1584 sizeof(struct omap_sham_hmac_ctx),
1585 .cra_alignmask = OMAP_ALIGN_MASK,
1586 .cra_module = THIS_MODULE,
1587 .cra_init = omap_sham_cra_sha224_init,
1588 .cra_exit = omap_sham_cra_exit,
1592 .init = omap_sham_init,
1593 .update = omap_sham_update,
1594 .final = omap_sham_final,
1595 .finup = omap_sham_finup,
1596 .digest = omap_sham_digest,
1597 .setkey = omap_sham_setkey,
1598 .halg.digestsize = SHA256_DIGEST_SIZE,
1600 .cra_name = "hmac(sha256)",
1601 .cra_driver_name = "omap-hmac-sha256",
1602 .cra_priority = 400,
1603 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
1605 CRYPTO_ALG_NEED_FALLBACK,
1606 .cra_blocksize = SHA256_BLOCK_SIZE,
1607 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1608 sizeof(struct omap_sham_hmac_ctx),
1609 .cra_alignmask = OMAP_ALIGN_MASK,
1610 .cra_module = THIS_MODULE,
1611 .cra_init = omap_sham_cra_sha256_init,
1612 .cra_exit = omap_sham_cra_exit,
1617 static struct ahash_alg algs_sha384_sha512[] = {
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 .halg.digestsize = SHA384_DIGEST_SIZE,
1626 .cra_name = "sha384",
1627 .cra_driver_name = "omap-sha384",
1628 .cra_priority = 400,
1629 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
1631 CRYPTO_ALG_NEED_FALLBACK,
1632 .cra_blocksize = SHA384_BLOCK_SIZE,
1633 .cra_ctxsize = sizeof(struct omap_sham_ctx),
1634 .cra_alignmask = OMAP_ALIGN_MASK,
1635 .cra_module = THIS_MODULE,
1636 .cra_init = omap_sham_cra_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 .halg.digestsize = SHA512_DIGEST_SIZE,
1648 .cra_name = "sha512",
1649 .cra_driver_name = "omap-sha512",
1650 .cra_priority = 400,
1651 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
1653 CRYPTO_ALG_NEED_FALLBACK,
1654 .cra_blocksize = SHA512_BLOCK_SIZE,
1655 .cra_ctxsize = sizeof(struct omap_sham_ctx),
1656 .cra_alignmask = OMAP_ALIGN_MASK,
1657 .cra_module = THIS_MODULE,
1658 .cra_init = omap_sham_cra_init,
1659 .cra_exit = omap_sham_cra_exit,
1663 .init = omap_sham_init,
1664 .update = omap_sham_update,
1665 .final = omap_sham_final,
1666 .finup = omap_sham_finup,
1667 .digest = omap_sham_digest,
1668 .setkey = omap_sham_setkey,
1669 .halg.digestsize = SHA384_DIGEST_SIZE,
1671 .cra_name = "hmac(sha384)",
1672 .cra_driver_name = "omap-hmac-sha384",
1673 .cra_priority = 400,
1674 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
1676 CRYPTO_ALG_NEED_FALLBACK,
1677 .cra_blocksize = SHA384_BLOCK_SIZE,
1678 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1679 sizeof(struct omap_sham_hmac_ctx),
1680 .cra_alignmask = OMAP_ALIGN_MASK,
1681 .cra_module = THIS_MODULE,
1682 .cra_init = omap_sham_cra_sha384_init,
1683 .cra_exit = omap_sham_cra_exit,
1687 .init = omap_sham_init,
1688 .update = omap_sham_update,
1689 .final = omap_sham_final,
1690 .finup = omap_sham_finup,
1691 .digest = omap_sham_digest,
1692 .setkey = omap_sham_setkey,
1693 .halg.digestsize = SHA512_DIGEST_SIZE,
1695 .cra_name = "hmac(sha512)",
1696 .cra_driver_name = "omap-hmac-sha512",
1697 .cra_priority = 400,
1698 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
1700 CRYPTO_ALG_NEED_FALLBACK,
1701 .cra_blocksize = SHA512_BLOCK_SIZE,
1702 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1703 sizeof(struct omap_sham_hmac_ctx),
1704 .cra_alignmask = OMAP_ALIGN_MASK,
1705 .cra_module = THIS_MODULE,
1706 .cra_init = omap_sham_cra_sha512_init,
1707 .cra_exit = omap_sham_cra_exit,
1712 static void omap_sham_done_task(unsigned long data)
1714 struct omap_sham_dev *dd = (struct omap_sham_dev *)data;
1717 dev_dbg(dd->dev, "%s: flags=%lx\n", __func__, dd->flags);
1719 if (test_bit(FLAGS_CPU, &dd->flags)) {
1720 if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags))
1722 } else if (test_bit(FLAGS_DMA_READY, &dd->flags)) {
1723 if (test_bit(FLAGS_DMA_ACTIVE, &dd->flags)) {
1724 omap_sham_update_dma_stop(dd);
1730 if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags)) {
1731 /* hash or semi-hash ready */
1732 clear_bit(FLAGS_DMA_READY, &dd->flags);
1740 dev_dbg(dd->dev, "update done: err: %d\n", err);
1741 /* finish curent request */
1742 omap_sham_finish_req(dd->req, err);
1745 static irqreturn_t omap_sham_irq_common(struct omap_sham_dev *dd)
1747 set_bit(FLAGS_OUTPUT_READY, &dd->flags);
1748 tasklet_schedule(&dd->done_task);
1753 static irqreturn_t omap_sham_irq_omap2(int irq, void *dev_id)
1755 struct omap_sham_dev *dd = dev_id;
1757 if (unlikely(test_bit(FLAGS_FINAL, &dd->flags)))
1758 /* final -> allow device to go to power-saving mode */
1759 omap_sham_write_mask(dd, SHA_REG_CTRL, 0, SHA_REG_CTRL_LENGTH);
1761 omap_sham_write_mask(dd, SHA_REG_CTRL, SHA_REG_CTRL_OUTPUT_READY,
1762 SHA_REG_CTRL_OUTPUT_READY);
1763 omap_sham_read(dd, SHA_REG_CTRL);
1765 return omap_sham_irq_common(dd);
1768 static irqreturn_t omap_sham_irq_omap4(int irq, void *dev_id)
1770 struct omap_sham_dev *dd = dev_id;
1772 omap_sham_write_mask(dd, SHA_REG_MASK(dd), 0, SHA_REG_MASK_IT_EN);
1774 return omap_sham_irq_common(dd);
1777 static struct omap_sham_algs_info omap_sham_algs_info_omap2[] = {
1779 .algs_list = algs_sha1_md5,
1780 .size = ARRAY_SIZE(algs_sha1_md5),
1784 static const struct omap_sham_pdata omap_sham_pdata_omap2 = {
1785 .algs_info = omap_sham_algs_info_omap2,
1786 .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap2),
1787 .flags = BIT(FLAGS_BE32_SHA1),
1788 .digest_size = SHA1_DIGEST_SIZE,
1789 .copy_hash = omap_sham_copy_hash_omap2,
1790 .write_ctrl = omap_sham_write_ctrl_omap2,
1791 .trigger = omap_sham_trigger_omap2,
1792 .poll_irq = omap_sham_poll_irq_omap2,
1793 .intr_hdlr = omap_sham_irq_omap2,
1794 .idigest_ofs = 0x00,
1799 .sysstatus_ofs = 0x64,
1807 static struct omap_sham_algs_info omap_sham_algs_info_omap4[] = {
1809 .algs_list = algs_sha1_md5,
1810 .size = ARRAY_SIZE(algs_sha1_md5),
1813 .algs_list = algs_sha224_sha256,
1814 .size = ARRAY_SIZE(algs_sha224_sha256),
1818 static const struct omap_sham_pdata omap_sham_pdata_omap4 = {
1819 .algs_info = omap_sham_algs_info_omap4,
1820 .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap4),
1821 .flags = BIT(FLAGS_AUTO_XOR),
1822 .digest_size = SHA256_DIGEST_SIZE,
1823 .copy_hash = omap_sham_copy_hash_omap4,
1824 .write_ctrl = omap_sham_write_ctrl_omap4,
1825 .trigger = omap_sham_trigger_omap4,
1826 .poll_irq = omap_sham_poll_irq_omap4,
1827 .intr_hdlr = omap_sham_irq_omap4,
1828 .idigest_ofs = 0x020,
1831 .digcnt_ofs = 0x040,
1834 .sysstatus_ofs = 0x114,
1837 .major_mask = 0x0700,
1839 .minor_mask = 0x003f,
1843 static struct omap_sham_algs_info omap_sham_algs_info_omap5[] = {
1845 .algs_list = algs_sha1_md5,
1846 .size = ARRAY_SIZE(algs_sha1_md5),
1849 .algs_list = algs_sha224_sha256,
1850 .size = ARRAY_SIZE(algs_sha224_sha256),
1853 .algs_list = algs_sha384_sha512,
1854 .size = ARRAY_SIZE(algs_sha384_sha512),
1858 static const struct omap_sham_pdata omap_sham_pdata_omap5 = {
1859 .algs_info = omap_sham_algs_info_omap5,
1860 .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap5),
1861 .flags = BIT(FLAGS_AUTO_XOR),
1862 .digest_size = SHA512_DIGEST_SIZE,
1863 .copy_hash = omap_sham_copy_hash_omap4,
1864 .write_ctrl = omap_sham_write_ctrl_omap4,
1865 .trigger = omap_sham_trigger_omap4,
1866 .poll_irq = omap_sham_poll_irq_omap4,
1867 .intr_hdlr = omap_sham_irq_omap4,
1868 .idigest_ofs = 0x240,
1869 .odigest_ofs = 0x200,
1871 .digcnt_ofs = 0x280,
1874 .sysstatus_ofs = 0x114,
1876 .length_ofs = 0x288,
1877 .major_mask = 0x0700,
1879 .minor_mask = 0x003f,
1883 static const struct of_device_id omap_sham_of_match[] = {
1885 .compatible = "ti,omap2-sham",
1886 .data = &omap_sham_pdata_omap2,
1889 .compatible = "ti,omap3-sham",
1890 .data = &omap_sham_pdata_omap2,
1893 .compatible = "ti,omap4-sham",
1894 .data = &omap_sham_pdata_omap4,
1897 .compatible = "ti,omap5-sham",
1898 .data = &omap_sham_pdata_omap5,
1902 MODULE_DEVICE_TABLE(of, omap_sham_of_match);
1904 static int omap_sham_get_res_of(struct omap_sham_dev *dd,
1905 struct device *dev, struct resource *res)
1907 struct device_node *node = dev->of_node;
1910 dd->pdata = of_device_get_match_data(dev);
1912 dev_err(dev, "no compatible OF match\n");
1917 err = of_address_to_resource(node, 0, res);
1919 dev_err(dev, "can't translate OF node address\n");
1924 dd->irq = irq_of_parse_and_map(node, 0);
1926 dev_err(dev, "can't translate OF irq value\n");
1935 static const struct of_device_id omap_sham_of_match[] = {
1939 static int omap_sham_get_res_of(struct omap_sham_dev *dd,
1940 struct device *dev, struct resource *res)
1946 static int omap_sham_get_res_pdev(struct omap_sham_dev *dd,
1947 struct platform_device *pdev, struct resource *res)
1949 struct device *dev = &pdev->dev;
1953 /* Get the base address */
1954 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1956 dev_err(dev, "no MEM resource info\n");
1960 memcpy(res, r, sizeof(*res));
1963 dd->irq = platform_get_irq(pdev, 0);
1969 /* Only OMAP2/3 can be non-DT */
1970 dd->pdata = &omap_sham_pdata_omap2;
1976 static ssize_t fallback_show(struct device *dev, struct device_attribute *attr,
1979 struct omap_sham_dev *dd = dev_get_drvdata(dev);
1981 return sprintf(buf, "%d\n", dd->fallback_sz);
1984 static ssize_t fallback_store(struct device *dev, struct device_attribute *attr,
1985 const char *buf, size_t size)
1987 struct omap_sham_dev *dd = dev_get_drvdata(dev);
1991 status = kstrtol(buf, 0, &value);
1995 /* HW accelerator only works with buffers > 9 */
1997 dev_err(dev, "minimum fallback size 9\n");
2001 dd->fallback_sz = value;
2006 static ssize_t queue_len_show(struct device *dev, struct device_attribute *attr,
2009 struct omap_sham_dev *dd = dev_get_drvdata(dev);
2011 return sprintf(buf, "%d\n", dd->queue.max_qlen);
2014 static ssize_t queue_len_store(struct device *dev,
2015 struct device_attribute *attr, const char *buf,
2018 struct omap_sham_dev *dd = dev_get_drvdata(dev);
2022 status = kstrtol(buf, 0, &value);
2030 * Changing the queue size in fly is safe, if size becomes smaller
2031 * than current size, it will just not accept new entries until
2032 * it has shrank enough.
2034 dd->queue.max_qlen = value;
2039 static DEVICE_ATTR_RW(queue_len);
2040 static DEVICE_ATTR_RW(fallback);
2042 static struct attribute *omap_sham_attrs[] = {
2043 &dev_attr_queue_len.attr,
2044 &dev_attr_fallback.attr,
2048 static struct attribute_group omap_sham_attr_group = {
2049 .attrs = omap_sham_attrs,
2052 static int omap_sham_probe(struct platform_device *pdev)
2054 struct omap_sham_dev *dd;
2055 struct device *dev = &pdev->dev;
2056 struct resource res;
2057 dma_cap_mask_t mask;
2061 dd = devm_kzalloc(dev, sizeof(struct omap_sham_dev), GFP_KERNEL);
2063 dev_err(dev, "unable to alloc data struct.\n");
2068 platform_set_drvdata(pdev, dd);
2070 INIT_LIST_HEAD(&dd->list);
2071 tasklet_init(&dd->done_task, omap_sham_done_task, (unsigned long)dd);
2072 crypto_init_queue(&dd->queue, OMAP_SHAM_QUEUE_LENGTH);
2074 err = (dev->of_node) ? omap_sham_get_res_of(dd, dev, &res) :
2075 omap_sham_get_res_pdev(dd, pdev, &res);
2079 dd->io_base = devm_ioremap_resource(dev, &res);
2080 if (IS_ERR(dd->io_base)) {
2081 err = PTR_ERR(dd->io_base);
2084 dd->phys_base = res.start;
2086 err = devm_request_irq(dev, dd->irq, dd->pdata->intr_hdlr,
2087 IRQF_TRIGGER_NONE, dev_name(dev), dd);
2089 dev_err(dev, "unable to request irq %d, err = %d\n",
2095 dma_cap_set(DMA_SLAVE, mask);
2097 dd->dma_lch = dma_request_chan(dev, "rx");
2098 if (IS_ERR(dd->dma_lch)) {
2099 err = PTR_ERR(dd->dma_lch);
2100 if (err == -EPROBE_DEFER)
2103 dd->polling_mode = 1;
2104 dev_dbg(dev, "using polling mode instead of dma\n");
2107 dd->flags |= dd->pdata->flags;
2108 sham.flags |= dd->pdata->flags;
2110 pm_runtime_use_autosuspend(dev);
2111 pm_runtime_set_autosuspend_delay(dev, DEFAULT_AUTOSUSPEND_DELAY);
2113 dd->fallback_sz = OMAP_SHA_DMA_THRESHOLD;
2115 pm_runtime_enable(dev);
2117 err = pm_runtime_resume_and_get(dev);
2119 dev_err(dev, "failed to get sync: %d\n", err);
2123 rev = omap_sham_read(dd, SHA_REG_REV(dd));
2124 pm_runtime_put_sync(&pdev->dev);
2126 dev_info(dev, "hw accel on OMAP rev %u.%u\n",
2127 (rev & dd->pdata->major_mask) >> dd->pdata->major_shift,
2128 (rev & dd->pdata->minor_mask) >> dd->pdata->minor_shift);
2130 spin_lock_bh(&sham.lock);
2131 list_add_tail(&dd->list, &sham.dev_list);
2132 spin_unlock_bh(&sham.lock);
2134 dd->engine = crypto_engine_alloc_init(dev, 1);
2140 err = crypto_engine_start(dd->engine);
2142 goto err_engine_start;
2144 for (i = 0; i < dd->pdata->algs_info_size; i++) {
2145 if (dd->pdata->algs_info[i].registered)
2148 for (j = 0; j < dd->pdata->algs_info[i].size; j++) {
2149 struct ahash_alg *alg;
2151 alg = &dd->pdata->algs_info[i].algs_list[j];
2152 alg->export = omap_sham_export;
2153 alg->import = omap_sham_import;
2154 alg->halg.statesize = sizeof(struct omap_sham_reqctx) +
2156 err = crypto_register_ahash(alg);
2160 dd->pdata->algs_info[i].registered++;
2164 err = sysfs_create_group(&dev->kobj, &omap_sham_attr_group);
2166 dev_err(dev, "could not create sysfs device attrs\n");
2173 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
2174 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
2175 crypto_unregister_ahash(
2176 &dd->pdata->algs_info[i].algs_list[j]);
2178 crypto_engine_exit(dd->engine);
2180 spin_lock_bh(&sham.lock);
2181 list_del(&dd->list);
2182 spin_unlock_bh(&sham.lock);
2184 pm_runtime_dont_use_autosuspend(dev);
2185 pm_runtime_disable(dev);
2186 if (!dd->polling_mode)
2187 dma_release_channel(dd->dma_lch);
2189 dev_err(dev, "initialization failed.\n");
2194 static int omap_sham_remove(struct platform_device *pdev)
2196 struct omap_sham_dev *dd;
2199 dd = platform_get_drvdata(pdev);
2202 spin_lock_bh(&sham.lock);
2203 list_del(&dd->list);
2204 spin_unlock_bh(&sham.lock);
2205 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
2206 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--) {
2207 crypto_unregister_ahash(
2208 &dd->pdata->algs_info[i].algs_list[j]);
2209 dd->pdata->algs_info[i].registered--;
2211 tasklet_kill(&dd->done_task);
2212 pm_runtime_dont_use_autosuspend(&pdev->dev);
2213 pm_runtime_disable(&pdev->dev);
2215 if (!dd->polling_mode)
2216 dma_release_channel(dd->dma_lch);
2218 sysfs_remove_group(&dd->dev->kobj, &omap_sham_attr_group);
2223 static struct platform_driver omap_sham_driver = {
2224 .probe = omap_sham_probe,
2225 .remove = omap_sham_remove,
2227 .name = "omap-sham",
2228 .of_match_table = omap_sham_of_match,
2232 module_platform_driver(omap_sham_driver);
2234 MODULE_DESCRIPTION("OMAP SHA1/MD5 hw acceleration support.");
2235 MODULE_LICENSE("GPL v2");
2236 MODULE_AUTHOR("Dmitry Kasatkin");
2237 MODULE_ALIAS("platform:omap-sham");