blob: 4e38b87c3228467581cddb5739a6f93fcaa52957 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Cryptographic API.
3 *
4 * Support for OMAP SHA1/MD5 HW acceleration.
5 *
6 * Copyright (c) 2010 Nokia Corporation
7 * Author: Dmitry Kasatkin <dmitry.kasatkin@nokia.com>
8 * Copyright (c) 2011 Texas Instruments Incorporated
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
13 *
14 * Some ideas are from old omap-sha1-md5.c driver.
15 */
16
17#define pr_fmt(fmt) "%s: " fmt, __func__
18
19#include <linux/err.h>
20#include <linux/device.h>
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/errno.h>
24#include <linux/interrupt.h>
25#include <linux/kernel.h>
26#include <linux/irq.h>
27#include <linux/io.h>
28#include <linux/platform_device.h>
29#include <linux/scatterlist.h>
30#include <linux/dma-mapping.h>
31#include <linux/dmaengine.h>
32#include <linux/pm_runtime.h>
33#include <linux/of.h>
34#include <linux/of_device.h>
35#include <linux/of_address.h>
36#include <linux/of_irq.h>
37#include <linux/delay.h>
38#include <linux/crypto.h>
39#include <linux/cryptohash.h>
40#include <crypto/scatterwalk.h>
41#include <crypto/algapi.h>
42#include <crypto/sha.h>
43#include <crypto/hash.h>
44#include <crypto/hmac.h>
45#include <crypto/internal/hash.h>
46
47#define MD5_DIGEST_SIZE 16
48
49#define SHA_REG_IDIGEST(dd, x) ((dd)->pdata->idigest_ofs + ((x)*0x04))
50#define SHA_REG_DIN(dd, x) ((dd)->pdata->din_ofs + ((x) * 0x04))
51#define SHA_REG_DIGCNT(dd) ((dd)->pdata->digcnt_ofs)
52
53#define SHA_REG_ODIGEST(dd, x) ((dd)->pdata->odigest_ofs + (x * 0x04))
54
55#define SHA_REG_CTRL 0x18
56#define SHA_REG_CTRL_LENGTH (0xFFFFFFFF << 5)
57#define SHA_REG_CTRL_CLOSE_HASH (1 << 4)
58#define SHA_REG_CTRL_ALGO_CONST (1 << 3)
59#define SHA_REG_CTRL_ALGO (1 << 2)
60#define SHA_REG_CTRL_INPUT_READY (1 << 1)
61#define SHA_REG_CTRL_OUTPUT_READY (1 << 0)
62
63#define SHA_REG_REV(dd) ((dd)->pdata->rev_ofs)
64
65#define SHA_REG_MASK(dd) ((dd)->pdata->mask_ofs)
66#define SHA_REG_MASK_DMA_EN (1 << 3)
67#define SHA_REG_MASK_IT_EN (1 << 2)
68#define SHA_REG_MASK_SOFTRESET (1 << 1)
69#define SHA_REG_AUTOIDLE (1 << 0)
70
71#define SHA_REG_SYSSTATUS(dd) ((dd)->pdata->sysstatus_ofs)
72#define SHA_REG_SYSSTATUS_RESETDONE (1 << 0)
73
74#define SHA_REG_MODE(dd) ((dd)->pdata->mode_ofs)
75#define SHA_REG_MODE_HMAC_OUTER_HASH (1 << 7)
76#define SHA_REG_MODE_HMAC_KEY_PROC (1 << 5)
77#define SHA_REG_MODE_CLOSE_HASH (1 << 4)
78#define SHA_REG_MODE_ALGO_CONSTANT (1 << 3)
79
80#define SHA_REG_MODE_ALGO_MASK (7 << 0)
81#define SHA_REG_MODE_ALGO_MD5_128 (0 << 1)
82#define SHA_REG_MODE_ALGO_SHA1_160 (1 << 1)
83#define SHA_REG_MODE_ALGO_SHA2_224 (2 << 1)
84#define SHA_REG_MODE_ALGO_SHA2_256 (3 << 1)
85#define SHA_REG_MODE_ALGO_SHA2_384 (1 << 0)
86#define SHA_REG_MODE_ALGO_SHA2_512 (3 << 0)
87
88#define SHA_REG_LENGTH(dd) ((dd)->pdata->length_ofs)
89
90#define SHA_REG_IRQSTATUS 0x118
91#define SHA_REG_IRQSTATUS_CTX_RDY (1 << 3)
92#define SHA_REG_IRQSTATUS_PARTHASH_RDY (1 << 2)
93#define SHA_REG_IRQSTATUS_INPUT_RDY (1 << 1)
94#define SHA_REG_IRQSTATUS_OUTPUT_RDY (1 << 0)
95
96#define SHA_REG_IRQENA 0x11C
97#define SHA_REG_IRQENA_CTX_RDY (1 << 3)
98#define SHA_REG_IRQENA_PARTHASH_RDY (1 << 2)
99#define SHA_REG_IRQENA_INPUT_RDY (1 << 1)
100#define SHA_REG_IRQENA_OUTPUT_RDY (1 << 0)
101
102#define DEFAULT_TIMEOUT_INTERVAL HZ
103
104#define DEFAULT_AUTOSUSPEND_DELAY 1000
105
106/* mostly device flags */
107#define FLAGS_BUSY 0
108#define FLAGS_FINAL 1
109#define FLAGS_DMA_ACTIVE 2
110#define FLAGS_OUTPUT_READY 3
111#define FLAGS_INIT 4
112#define FLAGS_CPU 5
113#define FLAGS_DMA_READY 6
114#define FLAGS_AUTO_XOR 7
115#define FLAGS_BE32_SHA1 8
116#define FLAGS_SGS_COPIED 9
117#define FLAGS_SGS_ALLOCED 10
118/* context flags */
119#define FLAGS_FINUP 16
120
121#define FLAGS_MODE_SHIFT 18
122#define FLAGS_MODE_MASK (SHA_REG_MODE_ALGO_MASK << FLAGS_MODE_SHIFT)
123#define FLAGS_MODE_MD5 (SHA_REG_MODE_ALGO_MD5_128 << FLAGS_MODE_SHIFT)
124#define FLAGS_MODE_SHA1 (SHA_REG_MODE_ALGO_SHA1_160 << FLAGS_MODE_SHIFT)
125#define FLAGS_MODE_SHA224 (SHA_REG_MODE_ALGO_SHA2_224 << FLAGS_MODE_SHIFT)
126#define FLAGS_MODE_SHA256 (SHA_REG_MODE_ALGO_SHA2_256 << FLAGS_MODE_SHIFT)
127#define FLAGS_MODE_SHA384 (SHA_REG_MODE_ALGO_SHA2_384 << FLAGS_MODE_SHIFT)
128#define FLAGS_MODE_SHA512 (SHA_REG_MODE_ALGO_SHA2_512 << FLAGS_MODE_SHIFT)
129
130#define FLAGS_HMAC 21
131#define FLAGS_ERROR 22
132
133#define OP_UPDATE 1
134#define OP_FINAL 2
135
136#define OMAP_ALIGN_MASK (sizeof(u32)-1)
137#define OMAP_ALIGNED __attribute__((aligned(sizeof(u32))))
138
139#define BUFLEN SHA512_BLOCK_SIZE
140#define OMAP_SHA_DMA_THRESHOLD 256
141
142struct omap_sham_dev;
143
144struct omap_sham_reqctx {
145 struct omap_sham_dev *dd;
146 unsigned long flags;
147 unsigned long op;
148
149 u8 digest[SHA512_DIGEST_SIZE] OMAP_ALIGNED;
150 size_t digcnt;
151 size_t bufcnt;
152 size_t buflen;
153
154 /* walk state */
155 struct scatterlist *sg;
156 struct scatterlist sgl[2];
157 int offset; /* offset in current sg */
158 int sg_len;
159 unsigned int total; /* total request */
160
161 u8 buffer[0] OMAP_ALIGNED;
162};
163
164struct 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;
168};
169
170struct omap_sham_ctx {
171 unsigned long flags;
172
173 /* fallback stuff */
174 struct crypto_shash *fallback;
175
176 struct omap_sham_hmac_ctx base[0];
177};
178
179#define OMAP_SHAM_QUEUE_LENGTH 10
180
181struct omap_sham_algs_info {
182 struct ahash_alg *algs_list;
183 unsigned int size;
184 unsigned int registered;
185};
186
187struct omap_sham_pdata {
188 struct omap_sham_algs_info *algs_info;
189 unsigned int algs_info_size;
190 unsigned long flags;
191 int digest_size;
192
193 void (*copy_hash)(struct ahash_request *req, int out);
194 void (*write_ctrl)(struct omap_sham_dev *dd, size_t length,
195 int final, int dma);
196 void (*trigger)(struct omap_sham_dev *dd, size_t length);
197 int (*poll_irq)(struct omap_sham_dev *dd);
198 irqreturn_t (*intr_hdlr)(int irq, void *dev_id);
199
200 u32 odigest_ofs;
201 u32 idigest_ofs;
202 u32 din_ofs;
203 u32 digcnt_ofs;
204 u32 rev_ofs;
205 u32 mask_ofs;
206 u32 sysstatus_ofs;
207 u32 mode_ofs;
208 u32 length_ofs;
209
210 u32 major_mask;
211 u32 major_shift;
212 u32 minor_mask;
213 u32 minor_shift;
214};
215
216struct omap_sham_dev {
217 struct list_head list;
218 unsigned long phys_base;
219 struct device *dev;
220 void __iomem *io_base;
221 int irq;
222 spinlock_t lock;
223 int err;
224 struct dma_chan *dma_lch;
225 struct tasklet_struct done_task;
226 u8 polling_mode;
227 u8 xmit_buf[BUFLEN] OMAP_ALIGNED;
228
229 unsigned long flags;
230 struct crypto_queue queue;
231 struct ahash_request *req;
232
233 const struct omap_sham_pdata *pdata;
234};
235
236struct omap_sham_drv {
237 struct list_head dev_list;
238 spinlock_t lock;
239 unsigned long flags;
240};
241
242static struct omap_sham_drv sham = {
243 .dev_list = LIST_HEAD_INIT(sham.dev_list),
244 .lock = __SPIN_LOCK_UNLOCKED(sham.lock),
245};
246
247static inline u32 omap_sham_read(struct omap_sham_dev *dd, u32 offset)
248{
249 return __raw_readl(dd->io_base + offset);
250}
251
252static inline void omap_sham_write(struct omap_sham_dev *dd,
253 u32 offset, u32 value)
254{
255 __raw_writel(value, dd->io_base + offset);
256}
257
258static inline void omap_sham_write_mask(struct omap_sham_dev *dd, u32 address,
259 u32 value, u32 mask)
260{
261 u32 val;
262
263 val = omap_sham_read(dd, address);
264 val &= ~mask;
265 val |= value;
266 omap_sham_write(dd, address, val);
267}
268
269static inline int omap_sham_wait(struct omap_sham_dev *dd, u32 offset, u32 bit)
270{
271 unsigned long timeout = jiffies + DEFAULT_TIMEOUT_INTERVAL;
272
273 while (!(omap_sham_read(dd, offset) & bit)) {
274 if (time_is_before_jiffies(timeout))
275 return -ETIMEDOUT;
276 }
277
278 return 0;
279}
280
281static void omap_sham_copy_hash_omap2(struct ahash_request *req, int out)
282{
283 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
284 struct omap_sham_dev *dd = ctx->dd;
285 u32 *hash = (u32 *)ctx->digest;
286 int i;
287
288 for (i = 0; i < dd->pdata->digest_size / sizeof(u32); i++) {
289 if (out)
290 hash[i] = omap_sham_read(dd, SHA_REG_IDIGEST(dd, i));
291 else
292 omap_sham_write(dd, SHA_REG_IDIGEST(dd, i), hash[i]);
293 }
294}
295
296static void omap_sham_copy_hash_omap4(struct ahash_request *req, int out)
297{
298 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
299 struct omap_sham_dev *dd = ctx->dd;
300 int i;
301
302 if (ctx->flags & BIT(FLAGS_HMAC)) {
303 struct crypto_ahash *tfm = crypto_ahash_reqtfm(dd->req);
304 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
305 struct omap_sham_hmac_ctx *bctx = tctx->base;
306 u32 *opad = (u32 *)bctx->opad;
307
308 for (i = 0; i < dd->pdata->digest_size / sizeof(u32); i++) {
309 if (out)
310 opad[i] = omap_sham_read(dd,
311 SHA_REG_ODIGEST(dd, i));
312 else
313 omap_sham_write(dd, SHA_REG_ODIGEST(dd, i),
314 opad[i]);
315 }
316 }
317
318 omap_sham_copy_hash_omap2(req, out);
319}
320
321static void omap_sham_copy_ready_hash(struct ahash_request *req)
322{
323 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
324 u32 *in = (u32 *)ctx->digest;
325 u32 *hash = (u32 *)req->result;
326 int i, d, big_endian = 0;
327
328 if (!hash)
329 return;
330
331 switch (ctx->flags & FLAGS_MODE_MASK) {
332 case FLAGS_MODE_MD5:
333 d = MD5_DIGEST_SIZE / sizeof(u32);
334 break;
335 case FLAGS_MODE_SHA1:
336 /* OMAP2 SHA1 is big endian */
337 if (test_bit(FLAGS_BE32_SHA1, &ctx->dd->flags))
338 big_endian = 1;
339 d = SHA1_DIGEST_SIZE / sizeof(u32);
340 break;
341 case FLAGS_MODE_SHA224:
342 d = SHA224_DIGEST_SIZE / sizeof(u32);
343 break;
344 case FLAGS_MODE_SHA256:
345 d = SHA256_DIGEST_SIZE / sizeof(u32);
346 break;
347 case FLAGS_MODE_SHA384:
348 d = SHA384_DIGEST_SIZE / sizeof(u32);
349 break;
350 case FLAGS_MODE_SHA512:
351 d = SHA512_DIGEST_SIZE / sizeof(u32);
352 break;
353 default:
354 d = 0;
355 }
356
357 if (big_endian)
358 for (i = 0; i < d; i++)
359 hash[i] = be32_to_cpu(in[i]);
360 else
361 for (i = 0; i < d; i++)
362 hash[i] = le32_to_cpu(in[i]);
363}
364
365static int omap_sham_hw_init(struct omap_sham_dev *dd)
366{
367 int err;
368
369 err = pm_runtime_get_sync(dd->dev);
370 if (err < 0) {
371 dev_err(dd->dev, "failed to get sync: %d\n", err);
372 return err;
373 }
374
375 if (!test_bit(FLAGS_INIT, &dd->flags)) {
376 set_bit(FLAGS_INIT, &dd->flags);
377 dd->err = 0;
378 }
379
380 return 0;
381}
382
383static void omap_sham_write_ctrl_omap2(struct omap_sham_dev *dd, size_t length,
384 int final, int dma)
385{
386 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
387 u32 val = length << 5, mask;
388
389 if (likely(ctx->digcnt))
390 omap_sham_write(dd, SHA_REG_DIGCNT(dd), ctx->digcnt);
391
392 omap_sham_write_mask(dd, SHA_REG_MASK(dd),
393 SHA_REG_MASK_IT_EN | (dma ? SHA_REG_MASK_DMA_EN : 0),
394 SHA_REG_MASK_IT_EN | SHA_REG_MASK_DMA_EN);
395 /*
396 * Setting ALGO_CONST only for the first iteration
397 * and CLOSE_HASH only for the last one.
398 */
399 if ((ctx->flags & FLAGS_MODE_MASK) == FLAGS_MODE_SHA1)
400 val |= SHA_REG_CTRL_ALGO;
401 if (!ctx->digcnt)
402 val |= SHA_REG_CTRL_ALGO_CONST;
403 if (final)
404 val |= SHA_REG_CTRL_CLOSE_HASH;
405
406 mask = SHA_REG_CTRL_ALGO_CONST | SHA_REG_CTRL_CLOSE_HASH |
407 SHA_REG_CTRL_ALGO | SHA_REG_CTRL_LENGTH;
408
409 omap_sham_write_mask(dd, SHA_REG_CTRL, val, mask);
410}
411
412static void omap_sham_trigger_omap2(struct omap_sham_dev *dd, size_t length)
413{
414}
415
416static int omap_sham_poll_irq_omap2(struct omap_sham_dev *dd)
417{
418 return omap_sham_wait(dd, SHA_REG_CTRL, SHA_REG_CTRL_INPUT_READY);
419}
420
421static int get_block_size(struct omap_sham_reqctx *ctx)
422{
423 int d;
424
425 switch (ctx->flags & FLAGS_MODE_MASK) {
426 case FLAGS_MODE_MD5:
427 case FLAGS_MODE_SHA1:
428 d = SHA1_BLOCK_SIZE;
429 break;
430 case FLAGS_MODE_SHA224:
431 case FLAGS_MODE_SHA256:
432 d = SHA256_BLOCK_SIZE;
433 break;
434 case FLAGS_MODE_SHA384:
435 case FLAGS_MODE_SHA512:
436 d = SHA512_BLOCK_SIZE;
437 break;
438 default:
439 d = 0;
440 }
441
442 return d;
443}
444
445static void omap_sham_write_n(struct omap_sham_dev *dd, u32 offset,
446 u32 *value, int count)
447{
448 for (; count--; value++, offset += 4)
449 omap_sham_write(dd, offset, *value);
450}
451
452static void omap_sham_write_ctrl_omap4(struct omap_sham_dev *dd, size_t length,
453 int final, int dma)
454{
455 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
456 u32 val, mask;
457
458 /*
459 * Setting ALGO_CONST only for the first iteration and
460 * CLOSE_HASH only for the last one. Note that flags mode bits
461 * correspond to algorithm encoding in mode register.
462 */
463 val = (ctx->flags & FLAGS_MODE_MASK) >> (FLAGS_MODE_SHIFT);
464 if (!ctx->digcnt) {
465 struct crypto_ahash *tfm = crypto_ahash_reqtfm(dd->req);
466 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
467 struct omap_sham_hmac_ctx *bctx = tctx->base;
468 int bs, nr_dr;
469
470 val |= SHA_REG_MODE_ALGO_CONSTANT;
471
472 if (ctx->flags & BIT(FLAGS_HMAC)) {
473 bs = get_block_size(ctx);
474 nr_dr = bs / (2 * sizeof(u32));
475 val |= SHA_REG_MODE_HMAC_KEY_PROC;
476 omap_sham_write_n(dd, SHA_REG_ODIGEST(dd, 0),
477 (u32 *)bctx->ipad, nr_dr);
478 omap_sham_write_n(dd, SHA_REG_IDIGEST(dd, 0),
479 (u32 *)bctx->ipad + nr_dr, nr_dr);
480 ctx->digcnt += bs;
481 }
482 }
483
484 if (final) {
485 val |= SHA_REG_MODE_CLOSE_HASH;
486
487 if (ctx->flags & BIT(FLAGS_HMAC))
488 val |= SHA_REG_MODE_HMAC_OUTER_HASH;
489 }
490
491 mask = SHA_REG_MODE_ALGO_CONSTANT | SHA_REG_MODE_CLOSE_HASH |
492 SHA_REG_MODE_ALGO_MASK | SHA_REG_MODE_HMAC_OUTER_HASH |
493 SHA_REG_MODE_HMAC_KEY_PROC;
494
495 dev_dbg(dd->dev, "ctrl: %08x, flags: %08lx\n", val, ctx->flags);
496 omap_sham_write_mask(dd, SHA_REG_MODE(dd), val, mask);
497 omap_sham_write(dd, SHA_REG_IRQENA, SHA_REG_IRQENA_OUTPUT_RDY);
498 omap_sham_write_mask(dd, SHA_REG_MASK(dd),
499 SHA_REG_MASK_IT_EN |
500 (dma ? SHA_REG_MASK_DMA_EN : 0),
501 SHA_REG_MASK_IT_EN | SHA_REG_MASK_DMA_EN);
502}
503
504static void omap_sham_trigger_omap4(struct omap_sham_dev *dd, size_t length)
505{
506 omap_sham_write(dd, SHA_REG_LENGTH(dd), length);
507}
508
509static int omap_sham_poll_irq_omap4(struct omap_sham_dev *dd)
510{
511 return omap_sham_wait(dd, SHA_REG_IRQSTATUS,
512 SHA_REG_IRQSTATUS_INPUT_RDY);
513}
514
515static int omap_sham_xmit_cpu(struct omap_sham_dev *dd, size_t length,
516 int final)
517{
518 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
519 int count, len32, bs32, offset = 0;
520 const u32 *buffer;
521 int mlen;
522 struct sg_mapping_iter mi;
523
524 dev_dbg(dd->dev, "xmit_cpu: digcnt: %d, length: %d, final: %d\n",
525 ctx->digcnt, length, final);
526
527 dd->pdata->write_ctrl(dd, length, final, 0);
528 dd->pdata->trigger(dd, length);
529
530 /* should be non-zero before next lines to disable clocks later */
531 ctx->digcnt += length;
532 ctx->total -= length;
533
534 if (final)
535 set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */
536
537 set_bit(FLAGS_CPU, &dd->flags);
538
539 len32 = DIV_ROUND_UP(length, sizeof(u32));
540 bs32 = get_block_size(ctx) / sizeof(u32);
541
542 sg_miter_start(&mi, ctx->sg, ctx->sg_len,
543 SG_MITER_FROM_SG | SG_MITER_ATOMIC);
544
545 mlen = 0;
546
547 while (len32) {
548 if (dd->pdata->poll_irq(dd))
549 return -ETIMEDOUT;
550
551 for (count = 0; count < min(len32, bs32); count++, offset++) {
552 if (!mlen) {
553 sg_miter_next(&mi);
554 mlen = mi.length;
555 if (!mlen) {
556 pr_err("sg miter failure.\n");
557 return -EINVAL;
558 }
559 offset = 0;
560 buffer = mi.addr;
561 }
562 omap_sham_write(dd, SHA_REG_DIN(dd, count),
563 buffer[offset]);
564 mlen -= 4;
565 }
566 len32 -= min(len32, bs32);
567 }
568
569 sg_miter_stop(&mi);
570
571 return -EINPROGRESS;
572}
573
574static void omap_sham_dma_callback(void *param)
575{
576 struct omap_sham_dev *dd = param;
577
578 set_bit(FLAGS_DMA_READY, &dd->flags);
579 tasklet_schedule(&dd->done_task);
580}
581
582static int omap_sham_xmit_dma(struct omap_sham_dev *dd, size_t length,
583 int final)
584{
585 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
586 struct dma_async_tx_descriptor *tx;
587 struct dma_slave_config cfg;
588 int ret;
589
590 dev_dbg(dd->dev, "xmit_dma: digcnt: %d, length: %d, final: %d\n",
591 ctx->digcnt, length, final);
592
593 if (!dma_map_sg(dd->dev, ctx->sg, ctx->sg_len, DMA_TO_DEVICE)) {
594 dev_err(dd->dev, "dma_map_sg error\n");
595 return -EINVAL;
596 }
597
598 memset(&cfg, 0, sizeof(cfg));
599
600 cfg.dst_addr = dd->phys_base + SHA_REG_DIN(dd, 0);
601 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
602 cfg.dst_maxburst = get_block_size(ctx) / DMA_SLAVE_BUSWIDTH_4_BYTES;
603
604 ret = dmaengine_slave_config(dd->dma_lch, &cfg);
605 if (ret) {
606 pr_err("omap-sham: can't configure dmaengine slave: %d\n", ret);
607 return ret;
608 }
609
610 tx = dmaengine_prep_slave_sg(dd->dma_lch, ctx->sg, ctx->sg_len,
611 DMA_MEM_TO_DEV,
612 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
613
614 if (!tx) {
615 dev_err(dd->dev, "prep_slave_sg failed\n");
616 return -EINVAL;
617 }
618
619 tx->callback = omap_sham_dma_callback;
620 tx->callback_param = dd;
621
622 dd->pdata->write_ctrl(dd, length, final, 1);
623
624 ctx->digcnt += length;
625 ctx->total -= length;
626
627 if (final)
628 set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */
629
630 set_bit(FLAGS_DMA_ACTIVE, &dd->flags);
631
632 dmaengine_submit(tx);
633 dma_async_issue_pending(dd->dma_lch);
634
635 dd->pdata->trigger(dd, length);
636
637 return -EINPROGRESS;
638}
639
640static int omap_sham_copy_sg_lists(struct omap_sham_reqctx *ctx,
641 struct scatterlist *sg, int bs, int new_len)
642{
643 int n = sg_nents(sg);
644 struct scatterlist *tmp;
645 int offset = ctx->offset;
646
647 if (ctx->bufcnt)
648 n++;
649
650 ctx->sg = kmalloc_array(n, sizeof(*sg), GFP_KERNEL);
651 if (!ctx->sg)
652 return -ENOMEM;
653
654 sg_init_table(ctx->sg, n);
655
656 tmp = ctx->sg;
657
658 ctx->sg_len = 0;
659
660 if (ctx->bufcnt) {
661 sg_set_buf(tmp, ctx->dd->xmit_buf, ctx->bufcnt);
662 tmp = sg_next(tmp);
663 ctx->sg_len++;
664 }
665
666 while (sg && new_len) {
667 int len = sg->length - offset;
668
669 if (offset) {
670 offset -= sg->length;
671 if (offset < 0)
672 offset = 0;
673 }
674
675 if (new_len < len)
676 len = new_len;
677
678 if (len > 0) {
679 new_len -= len;
680 sg_set_page(tmp, sg_page(sg), len, sg->offset);
681 if (new_len <= 0)
682 sg_mark_end(tmp);
683 tmp = sg_next(tmp);
684 ctx->sg_len++;
685 }
686
687 sg = sg_next(sg);
688 }
689
690 set_bit(FLAGS_SGS_ALLOCED, &ctx->dd->flags);
691
692 ctx->bufcnt = 0;
693
694 return 0;
695}
696
697static int omap_sham_copy_sgs(struct omap_sham_reqctx *ctx,
698 struct scatterlist *sg, int bs, int new_len)
699{
700 int pages;
701 void *buf;
702 int len;
703
704 len = new_len + ctx->bufcnt;
705
706 pages = get_order(ctx->total);
707
708 buf = (void *)__get_free_pages(GFP_ATOMIC, pages);
709 if (!buf) {
710 pr_err("Couldn't allocate pages for unaligned cases.\n");
711 return -ENOMEM;
712 }
713
714 if (ctx->bufcnt)
715 memcpy(buf, ctx->dd->xmit_buf, ctx->bufcnt);
716
717 scatterwalk_map_and_copy(buf + ctx->bufcnt, sg, ctx->offset,
718 ctx->total - ctx->bufcnt, 0);
719 sg_init_table(ctx->sgl, 1);
720 sg_set_buf(ctx->sgl, buf, len);
721 ctx->sg = ctx->sgl;
722 set_bit(FLAGS_SGS_COPIED, &ctx->dd->flags);
723 ctx->sg_len = 1;
724 ctx->bufcnt = 0;
725 ctx->offset = 0;
726
727 return 0;
728}
729
730static int omap_sham_align_sgs(struct scatterlist *sg,
731 int nbytes, int bs, bool final,
732 struct omap_sham_reqctx *rctx)
733{
734 int n = 0;
735 bool aligned = true;
736 bool list_ok = true;
737 struct scatterlist *sg_tmp = sg;
738 int new_len;
739 int offset = rctx->offset;
740
741 if (!sg || !sg->length || !nbytes)
742 return 0;
743
744 new_len = nbytes;
745
746 if (offset)
747 list_ok = false;
748
749 if (final)
750 new_len = DIV_ROUND_UP(new_len, bs) * bs;
751 else
752 new_len = (new_len - 1) / bs * bs;
753
754 if (nbytes != new_len)
755 list_ok = false;
756
757 while (nbytes > 0 && sg_tmp) {
758 n++;
759
760 if (offset < sg_tmp->length) {
761 if (!IS_ALIGNED(offset + sg_tmp->offset, 4)) {
762 aligned = false;
763 break;
764 }
765
766 if (!IS_ALIGNED(sg_tmp->length - offset, bs)) {
767 aligned = false;
768 break;
769 }
770 }
771
772 if (offset) {
773 offset -= sg_tmp->length;
774 if (offset < 0) {
775 nbytes += offset;
776 offset = 0;
777 }
778 } else {
779 nbytes -= sg_tmp->length;
780 }
781
782 sg_tmp = sg_next(sg_tmp);
783
784 if (nbytes < 0) {
785 list_ok = false;
786 break;
787 }
788 }
789
790 if (!aligned)
791 return omap_sham_copy_sgs(rctx, sg, bs, new_len);
792 else if (!list_ok)
793 return omap_sham_copy_sg_lists(rctx, sg, bs, new_len);
794
795 rctx->sg_len = n;
796 rctx->sg = sg;
797
798 return 0;
799}
800
801static int omap_sham_prepare_request(struct ahash_request *req, bool update)
802{
803 struct omap_sham_reqctx *rctx = ahash_request_ctx(req);
804 int bs;
805 int ret;
806 int nbytes;
807 bool final = rctx->flags & BIT(FLAGS_FINUP);
808 int xmit_len, hash_later;
809
810 if (!req)
811 return 0;
812
813 bs = get_block_size(rctx);
814
815 if (update)
816 nbytes = req->nbytes;
817 else
818 nbytes = 0;
819
820 rctx->total = nbytes + rctx->bufcnt;
821
822 if (!rctx->total)
823 return 0;
824
825 if (nbytes && (!IS_ALIGNED(rctx->bufcnt, bs))) {
826 int len = bs - rctx->bufcnt % bs;
827
828 if (len > nbytes)
829 len = nbytes;
830 scatterwalk_map_and_copy(rctx->buffer + rctx->bufcnt, req->src,
831 0, len, 0);
832 rctx->bufcnt += len;
833 nbytes -= len;
834 rctx->offset = len;
835 }
836
837 if (rctx->bufcnt)
838 memcpy(rctx->dd->xmit_buf, rctx->buffer, rctx->bufcnt);
839
840 ret = omap_sham_align_sgs(req->src, nbytes, bs, final, rctx);
841 if (ret)
842 return ret;
843
844 xmit_len = rctx->total;
845
846 if (!IS_ALIGNED(xmit_len, bs)) {
847 if (final)
848 xmit_len = DIV_ROUND_UP(xmit_len, bs) * bs;
849 else
850 xmit_len = xmit_len / bs * bs;
851 } else if (!final) {
852 xmit_len -= bs;
853 }
854
855 hash_later = rctx->total - xmit_len;
856 if (hash_later < 0)
857 hash_later = 0;
858
859 if (rctx->bufcnt && nbytes) {
860 /* have data from previous operation and current */
861 sg_init_table(rctx->sgl, 2);
862 sg_set_buf(rctx->sgl, rctx->dd->xmit_buf, rctx->bufcnt);
863
864 sg_chain(rctx->sgl, 2, req->src);
865
866 rctx->sg = rctx->sgl;
867
868 rctx->sg_len++;
869 } else if (rctx->bufcnt) {
870 /* have buffered data only */
871 sg_init_table(rctx->sgl, 1);
872 sg_set_buf(rctx->sgl, rctx->dd->xmit_buf, xmit_len);
873
874 rctx->sg = rctx->sgl;
875
876 rctx->sg_len = 1;
877 }
878
879 if (hash_later) {
880 int offset = 0;
881
882 if (hash_later > req->nbytes) {
883 memcpy(rctx->buffer, rctx->buffer + xmit_len,
884 hash_later - req->nbytes);
885 offset = hash_later - req->nbytes;
886 }
887
888 if (req->nbytes) {
889 scatterwalk_map_and_copy(rctx->buffer + offset,
890 req->src,
891 offset + req->nbytes -
892 hash_later, hash_later, 0);
893 }
894
895 rctx->bufcnt = hash_later;
896 } else {
897 rctx->bufcnt = 0;
898 }
899
900 if (!final)
901 rctx->total = xmit_len;
902
903 return 0;
904}
905
906static int omap_sham_update_dma_stop(struct omap_sham_dev *dd)
907{
908 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
909
910 dma_unmap_sg(dd->dev, ctx->sg, ctx->sg_len, DMA_TO_DEVICE);
911
912 clear_bit(FLAGS_DMA_ACTIVE, &dd->flags);
913
914 return 0;
915}
916
917struct omap_sham_dev *omap_sham_find_dev(struct omap_sham_reqctx *ctx)
918{
919 struct omap_sham_dev *dd;
920
921 if (ctx->dd)
922 return ctx->dd;
923
924 spin_lock_bh(&sham.lock);
925 dd = list_first_entry(&sham.dev_list, struct omap_sham_dev, list);
926 list_move_tail(&dd->list, &sham.dev_list);
927 ctx->dd = dd;
928 spin_unlock_bh(&sham.lock);
929
930 return dd;
931}
932
933static int omap_sham_init(struct ahash_request *req)
934{
935 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
936 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
937 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
938 struct omap_sham_dev *dd;
939 int bs = 0;
940
941 ctx->dd = NULL;
942
943 dd = omap_sham_find_dev(ctx);
944 if (!dd)
945 return -ENODEV;
946
947 ctx->flags = 0;
948
949 dev_dbg(dd->dev, "init: digest size: %d\n",
950 crypto_ahash_digestsize(tfm));
951
952 switch (crypto_ahash_digestsize(tfm)) {
953 case MD5_DIGEST_SIZE:
954 ctx->flags |= FLAGS_MODE_MD5;
955 bs = SHA1_BLOCK_SIZE;
956 break;
957 case SHA1_DIGEST_SIZE:
958 ctx->flags |= FLAGS_MODE_SHA1;
959 bs = SHA1_BLOCK_SIZE;
960 break;
961 case SHA224_DIGEST_SIZE:
962 ctx->flags |= FLAGS_MODE_SHA224;
963 bs = SHA224_BLOCK_SIZE;
964 break;
965 case SHA256_DIGEST_SIZE:
966 ctx->flags |= FLAGS_MODE_SHA256;
967 bs = SHA256_BLOCK_SIZE;
968 break;
969 case SHA384_DIGEST_SIZE:
970 ctx->flags |= FLAGS_MODE_SHA384;
971 bs = SHA384_BLOCK_SIZE;
972 break;
973 case SHA512_DIGEST_SIZE:
974 ctx->flags |= FLAGS_MODE_SHA512;
975 bs = SHA512_BLOCK_SIZE;
976 break;
977 }
978
979 ctx->bufcnt = 0;
980 ctx->digcnt = 0;
981 ctx->total = 0;
982 ctx->offset = 0;
983 ctx->buflen = BUFLEN;
984
985 if (tctx->flags & BIT(FLAGS_HMAC)) {
986 if (!test_bit(FLAGS_AUTO_XOR, &dd->flags)) {
987 struct omap_sham_hmac_ctx *bctx = tctx->base;
988
989 memcpy(ctx->buffer, bctx->ipad, bs);
990 ctx->bufcnt = bs;
991 }
992
993 ctx->flags |= BIT(FLAGS_HMAC);
994 }
995
996 return 0;
997
998}
999
1000static int omap_sham_update_req(struct omap_sham_dev *dd)
1001{
1002 struct ahash_request *req = dd->req;
1003 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1004 int err;
1005 bool final = ctx->flags & BIT(FLAGS_FINUP);
1006
1007 dev_dbg(dd->dev, "update_req: total: %u, digcnt: %d, finup: %d\n",
1008 ctx->total, ctx->digcnt, (ctx->flags & BIT(FLAGS_FINUP)) != 0);
1009
1010 if (ctx->total < get_block_size(ctx) ||
1011 ctx->total < OMAP_SHA_DMA_THRESHOLD)
1012 ctx->flags |= BIT(FLAGS_CPU);
1013
1014 if (ctx->flags & BIT(FLAGS_CPU))
1015 err = omap_sham_xmit_cpu(dd, ctx->total, final);
1016 else
1017 err = omap_sham_xmit_dma(dd, ctx->total, final);
1018
1019 /* wait for dma completion before can take more data */
1020 dev_dbg(dd->dev, "update: err: %d, digcnt: %d\n", err, ctx->digcnt);
1021
1022 return err;
1023}
1024
1025static int omap_sham_final_req(struct omap_sham_dev *dd)
1026{
1027 struct ahash_request *req = dd->req;
1028 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1029 int err = 0, use_dma = 1;
1030
1031 if ((ctx->total <= get_block_size(ctx)) || dd->polling_mode)
1032 /*
1033 * faster to handle last block with cpu or
1034 * use cpu when dma is not present.
1035 */
1036 use_dma = 0;
1037
1038 if (use_dma)
1039 err = omap_sham_xmit_dma(dd, ctx->total, 1);
1040 else
1041 err = omap_sham_xmit_cpu(dd, ctx->total, 1);
1042
1043 ctx->bufcnt = 0;
1044
1045 dev_dbg(dd->dev, "final_req: err: %d\n", err);
1046
1047 return err;
1048}
1049
1050static int omap_sham_finish_hmac(struct ahash_request *req)
1051{
1052 struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
1053 struct omap_sham_hmac_ctx *bctx = tctx->base;
1054 int bs = crypto_shash_blocksize(bctx->shash);
1055 int ds = crypto_shash_digestsize(bctx->shash);
1056 SHASH_DESC_ON_STACK(shash, bctx->shash);
1057
1058 shash->tfm = bctx->shash;
1059 shash->flags = 0; /* not CRYPTO_TFM_REQ_MAY_SLEEP */
1060
1061 return crypto_shash_init(shash) ?:
1062 crypto_shash_update(shash, bctx->opad, bs) ?:
1063 crypto_shash_finup(shash, req->result, ds, req->result);
1064}
1065
1066static int omap_sham_finish(struct ahash_request *req)
1067{
1068 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1069 struct omap_sham_dev *dd = ctx->dd;
1070 int err = 0;
1071
1072 if (ctx->digcnt) {
1073 omap_sham_copy_ready_hash(req);
1074 if ((ctx->flags & BIT(FLAGS_HMAC)) &&
1075 !test_bit(FLAGS_AUTO_XOR, &dd->flags))
1076 err = omap_sham_finish_hmac(req);
1077 }
1078
1079 dev_dbg(dd->dev, "digcnt: %d, bufcnt: %d\n", ctx->digcnt, ctx->bufcnt);
1080
1081 return err;
1082}
1083
1084static void omap_sham_finish_req(struct ahash_request *req, int err)
1085{
1086 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1087 struct omap_sham_dev *dd = ctx->dd;
1088
1089 if (test_bit(FLAGS_SGS_COPIED, &dd->flags))
1090 free_pages((unsigned long)sg_virt(ctx->sg),
1091 get_order(ctx->sg->length + ctx->bufcnt));
1092
1093 if (test_bit(FLAGS_SGS_ALLOCED, &dd->flags))
1094 kfree(ctx->sg);
1095
1096 ctx->sg = NULL;
1097
1098 dd->flags &= ~(BIT(FLAGS_SGS_ALLOCED) | BIT(FLAGS_SGS_COPIED));
1099
1100 if (!err) {
1101 dd->pdata->copy_hash(req, 1);
1102 if (test_bit(FLAGS_FINAL, &dd->flags))
1103 err = omap_sham_finish(req);
1104 } else {
1105 ctx->flags |= BIT(FLAGS_ERROR);
1106 }
1107
1108 /* atomic operation is not needed here */
1109 dd->flags &= ~(BIT(FLAGS_BUSY) | BIT(FLAGS_FINAL) | BIT(FLAGS_CPU) |
1110 BIT(FLAGS_DMA_READY) | BIT(FLAGS_OUTPUT_READY));
1111
1112 pm_runtime_mark_last_busy(dd->dev);
1113 pm_runtime_put_autosuspend(dd->dev);
1114
1115 if (req->base.complete)
1116 req->base.complete(&req->base, err);
1117}
1118
1119static int omap_sham_handle_queue(struct omap_sham_dev *dd,
1120 struct ahash_request *req)
1121{
1122 struct crypto_async_request *async_req, *backlog;
1123 struct omap_sham_reqctx *ctx;
1124 unsigned long flags;
1125 int err = 0, ret = 0;
1126
1127retry:
1128 spin_lock_irqsave(&dd->lock, flags);
1129 if (req)
1130 ret = ahash_enqueue_request(&dd->queue, req);
1131 if (test_bit(FLAGS_BUSY, &dd->flags)) {
1132 spin_unlock_irqrestore(&dd->lock, flags);
1133 return ret;
1134 }
1135 backlog = crypto_get_backlog(&dd->queue);
1136 async_req = crypto_dequeue_request(&dd->queue);
1137 if (async_req)
1138 set_bit(FLAGS_BUSY, &dd->flags);
1139 spin_unlock_irqrestore(&dd->lock, flags);
1140
1141 if (!async_req)
1142 return ret;
1143
1144 if (backlog)
1145 backlog->complete(backlog, -EINPROGRESS);
1146
1147 req = ahash_request_cast(async_req);
1148 dd->req = req;
1149 ctx = ahash_request_ctx(req);
1150
1151 err = omap_sham_prepare_request(req, ctx->op == OP_UPDATE);
1152 if (err || !ctx->total)
1153 goto err1;
1154
1155 dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n",
1156 ctx->op, req->nbytes);
1157
1158 err = omap_sham_hw_init(dd);
1159 if (err)
1160 goto err1;
1161
1162 if (ctx->digcnt)
1163 /* request has changed - restore hash */
1164 dd->pdata->copy_hash(req, 0);
1165
1166 if (ctx->op == OP_UPDATE) {
1167 err = omap_sham_update_req(dd);
1168 if (err != -EINPROGRESS && (ctx->flags & BIT(FLAGS_FINUP)))
1169 /* no final() after finup() */
1170 err = omap_sham_final_req(dd);
1171 } else if (ctx->op == OP_FINAL) {
1172 err = omap_sham_final_req(dd);
1173 }
1174err1:
1175 dev_dbg(dd->dev, "exit, err: %d\n", err);
1176
1177 if (err != -EINPROGRESS) {
1178 /* done_task will not finish it, so do it here */
1179 omap_sham_finish_req(req, err);
1180 req = NULL;
1181
1182 /*
1183 * Execute next request immediately if there is anything
1184 * in queue.
1185 */
1186 goto retry;
1187 }
1188
1189 return ret;
1190}
1191
1192static int omap_sham_enqueue(struct ahash_request *req, unsigned int op)
1193{
1194 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1195 struct omap_sham_dev *dd = ctx->dd;
1196
1197 ctx->op = op;
1198
1199 return omap_sham_handle_queue(dd, req);
1200}
1201
1202static int omap_sham_update(struct ahash_request *req)
1203{
1204 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1205 struct omap_sham_dev *dd = omap_sham_find_dev(ctx);
1206
1207 if (!req->nbytes)
1208 return 0;
1209
1210 if (ctx->bufcnt + req->nbytes <= ctx->buflen) {
1211 scatterwalk_map_and_copy(ctx->buffer + ctx->bufcnt, req->src,
1212 0, req->nbytes, 0);
1213 ctx->bufcnt += req->nbytes;
1214 return 0;
1215 }
1216
1217 if (dd->polling_mode)
1218 ctx->flags |= BIT(FLAGS_CPU);
1219
1220 return omap_sham_enqueue(req, OP_UPDATE);
1221}
1222
1223static int omap_sham_shash_digest(struct crypto_shash *tfm, u32 flags,
1224 const u8 *data, unsigned int len, u8 *out)
1225{
1226 SHASH_DESC_ON_STACK(shash, tfm);
1227
1228 shash->tfm = tfm;
1229 shash->flags = flags & CRYPTO_TFM_REQ_MAY_SLEEP;
1230
1231 return crypto_shash_digest(shash, data, len, out);
1232}
1233
1234static int omap_sham_final_shash(struct ahash_request *req)
1235{
1236 struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
1237 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1238 int offset = 0;
1239
1240 /*
1241 * If we are running HMAC on limited hardware support, skip
1242 * the ipad in the beginning of the buffer if we are going for
1243 * software fallback algorithm.
1244 */
1245 if (test_bit(FLAGS_HMAC, &ctx->flags) &&
1246 !test_bit(FLAGS_AUTO_XOR, &ctx->dd->flags))
1247 offset = get_block_size(ctx);
1248
1249 return omap_sham_shash_digest(tctx->fallback, req->base.flags,
1250 ctx->buffer + offset,
1251 ctx->bufcnt - offset, req->result);
1252}
1253
1254static int omap_sham_final(struct ahash_request *req)
1255{
1256 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1257
1258 ctx->flags |= BIT(FLAGS_FINUP);
1259
1260 if (ctx->flags & BIT(FLAGS_ERROR))
1261 return 0; /* uncompleted hash is not needed */
1262
1263 /*
1264 * OMAP HW accel works only with buffers >= 9.
1265 * HMAC is always >= 9 because ipad == block size.
1266 * If buffersize is less than DMA_THRESHOLD, we use fallback
1267 * SW encoding, as using DMA + HW in this case doesn't provide
1268 * any benefit.
1269 */
1270 if (!ctx->digcnt && ctx->bufcnt < OMAP_SHA_DMA_THRESHOLD)
1271 return omap_sham_final_shash(req);
1272 else if (ctx->bufcnt)
1273 return omap_sham_enqueue(req, OP_FINAL);
1274
1275 /* copy ready hash (+ finalize hmac) */
1276 return omap_sham_finish(req);
1277}
1278
1279static int omap_sham_finup(struct ahash_request *req)
1280{
1281 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1282 int err1, err2;
1283
1284 ctx->flags |= BIT(FLAGS_FINUP);
1285
1286 err1 = omap_sham_update(req);
1287 if (err1 == -EINPROGRESS || err1 == -EBUSY)
1288 return err1;
1289 /*
1290 * final() has to be always called to cleanup resources
1291 * even if udpate() failed, except EINPROGRESS
1292 */
1293 err2 = omap_sham_final(req);
1294
1295 return err1 ?: err2;
1296}
1297
1298static int omap_sham_digest(struct ahash_request *req)
1299{
1300 return omap_sham_init(req) ?: omap_sham_finup(req);
1301}
1302
1303static int omap_sham_setkey(struct crypto_ahash *tfm, const u8 *key,
1304 unsigned int keylen)
1305{
1306 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
1307 struct omap_sham_hmac_ctx *bctx = tctx->base;
1308 int bs = crypto_shash_blocksize(bctx->shash);
1309 int ds = crypto_shash_digestsize(bctx->shash);
1310 int err, i;
1311
1312 err = crypto_shash_setkey(tctx->fallback, key, keylen);
1313 if (err)
1314 return err;
1315
1316 if (keylen > bs) {
1317 err = omap_sham_shash_digest(bctx->shash,
1318 crypto_shash_get_flags(bctx->shash),
1319 key, keylen, bctx->ipad);
1320 if (err)
1321 return err;
1322 keylen = ds;
1323 } else {
1324 memcpy(bctx->ipad, key, keylen);
1325 }
1326
1327 memset(bctx->ipad + keylen, 0, bs - keylen);
1328
1329 if (!test_bit(FLAGS_AUTO_XOR, &sham.flags)) {
1330 memcpy(bctx->opad, bctx->ipad, bs);
1331
1332 for (i = 0; i < bs; i++) {
1333 bctx->ipad[i] ^= HMAC_IPAD_VALUE;
1334 bctx->opad[i] ^= HMAC_OPAD_VALUE;
1335 }
1336 }
1337
1338 return err;
1339}
1340
1341static int omap_sham_cra_init_alg(struct crypto_tfm *tfm, const char *alg_base)
1342{
1343 struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm);
1344 const char *alg_name = crypto_tfm_alg_name(tfm);
1345
1346 /* Allocate a fallback and abort if it failed. */
1347 tctx->fallback = crypto_alloc_shash(alg_name, 0,
1348 CRYPTO_ALG_NEED_FALLBACK);
1349 if (IS_ERR(tctx->fallback)) {
1350 pr_err("omap-sham: fallback driver '%s' "
1351 "could not be loaded.\n", alg_name);
1352 return PTR_ERR(tctx->fallback);
1353 }
1354
1355 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
1356 sizeof(struct omap_sham_reqctx) + BUFLEN);
1357
1358 if (alg_base) {
1359 struct omap_sham_hmac_ctx *bctx = tctx->base;
1360 tctx->flags |= BIT(FLAGS_HMAC);
1361 bctx->shash = crypto_alloc_shash(alg_base, 0,
1362 CRYPTO_ALG_NEED_FALLBACK);
1363 if (IS_ERR(bctx->shash)) {
1364 pr_err("omap-sham: base driver '%s' "
1365 "could not be loaded.\n", alg_base);
1366 crypto_free_shash(tctx->fallback);
1367 return PTR_ERR(bctx->shash);
1368 }
1369
1370 }
1371
1372 return 0;
1373}
1374
1375static int omap_sham_cra_init(struct crypto_tfm *tfm)
1376{
1377 return omap_sham_cra_init_alg(tfm, NULL);
1378}
1379
1380static int omap_sham_cra_sha1_init(struct crypto_tfm *tfm)
1381{
1382 return omap_sham_cra_init_alg(tfm, "sha1");
1383}
1384
1385static int omap_sham_cra_sha224_init(struct crypto_tfm *tfm)
1386{
1387 return omap_sham_cra_init_alg(tfm, "sha224");
1388}
1389
1390static int omap_sham_cra_sha256_init(struct crypto_tfm *tfm)
1391{
1392 return omap_sham_cra_init_alg(tfm, "sha256");
1393}
1394
1395static int omap_sham_cra_md5_init(struct crypto_tfm *tfm)
1396{
1397 return omap_sham_cra_init_alg(tfm, "md5");
1398}
1399
1400static int omap_sham_cra_sha384_init(struct crypto_tfm *tfm)
1401{
1402 return omap_sham_cra_init_alg(tfm, "sha384");
1403}
1404
1405static int omap_sham_cra_sha512_init(struct crypto_tfm *tfm)
1406{
1407 return omap_sham_cra_init_alg(tfm, "sha512");
1408}
1409
1410static void omap_sham_cra_exit(struct crypto_tfm *tfm)
1411{
1412 struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm);
1413
1414 crypto_free_shash(tctx->fallback);
1415 tctx->fallback = NULL;
1416
1417 if (tctx->flags & BIT(FLAGS_HMAC)) {
1418 struct omap_sham_hmac_ctx *bctx = tctx->base;
1419 crypto_free_shash(bctx->shash);
1420 }
1421}
1422
1423static int omap_sham_export(struct ahash_request *req, void *out)
1424{
1425 struct omap_sham_reqctx *rctx = ahash_request_ctx(req);
1426
1427 memcpy(out, rctx, sizeof(*rctx) + rctx->bufcnt);
1428
1429 return 0;
1430}
1431
1432static int omap_sham_import(struct ahash_request *req, const void *in)
1433{
1434 struct omap_sham_reqctx *rctx = ahash_request_ctx(req);
1435 const struct omap_sham_reqctx *ctx_in = in;
1436
1437 memcpy(rctx, in, sizeof(*rctx) + ctx_in->bufcnt);
1438
1439 return 0;
1440}
1441
1442static struct ahash_alg algs_sha1_md5[] = {
1443{
1444 .init = omap_sham_init,
1445 .update = omap_sham_update,
1446 .final = omap_sham_final,
1447 .finup = omap_sham_finup,
1448 .digest = omap_sham_digest,
1449 .halg.digestsize = SHA1_DIGEST_SIZE,
1450 .halg.base = {
1451 .cra_name = "sha1",
1452 .cra_driver_name = "omap-sha1",
1453 .cra_priority = 400,
1454 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1455 CRYPTO_ALG_KERN_DRIVER_ONLY |
1456 CRYPTO_ALG_ASYNC |
1457 CRYPTO_ALG_NEED_FALLBACK,
1458 .cra_blocksize = SHA1_BLOCK_SIZE,
1459 .cra_ctxsize = sizeof(struct omap_sham_ctx),
1460 .cra_alignmask = OMAP_ALIGN_MASK,
1461 .cra_module = THIS_MODULE,
1462 .cra_init = omap_sham_cra_init,
1463 .cra_exit = omap_sham_cra_exit,
1464 }
1465},
1466{
1467 .init = omap_sham_init,
1468 .update = omap_sham_update,
1469 .final = omap_sham_final,
1470 .finup = omap_sham_finup,
1471 .digest = omap_sham_digest,
1472 .halg.digestsize = MD5_DIGEST_SIZE,
1473 .halg.base = {
1474 .cra_name = "md5",
1475 .cra_driver_name = "omap-md5",
1476 .cra_priority = 400,
1477 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1478 CRYPTO_ALG_KERN_DRIVER_ONLY |
1479 CRYPTO_ALG_ASYNC |
1480 CRYPTO_ALG_NEED_FALLBACK,
1481 .cra_blocksize = SHA1_BLOCK_SIZE,
1482 .cra_ctxsize = sizeof(struct omap_sham_ctx),
1483 .cra_alignmask = OMAP_ALIGN_MASK,
1484 .cra_module = THIS_MODULE,
1485 .cra_init = omap_sham_cra_init,
1486 .cra_exit = omap_sham_cra_exit,
1487 }
1488},
1489{
1490 .init = omap_sham_init,
1491 .update = omap_sham_update,
1492 .final = omap_sham_final,
1493 .finup = omap_sham_finup,
1494 .digest = omap_sham_digest,
1495 .setkey = omap_sham_setkey,
1496 .halg.digestsize = SHA1_DIGEST_SIZE,
1497 .halg.base = {
1498 .cra_name = "hmac(sha1)",
1499 .cra_driver_name = "omap-hmac-sha1",
1500 .cra_priority = 400,
1501 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1502 CRYPTO_ALG_KERN_DRIVER_ONLY |
1503 CRYPTO_ALG_ASYNC |
1504 CRYPTO_ALG_NEED_FALLBACK,
1505 .cra_blocksize = SHA1_BLOCK_SIZE,
1506 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1507 sizeof(struct omap_sham_hmac_ctx),
1508 .cra_alignmask = OMAP_ALIGN_MASK,
1509 .cra_module = THIS_MODULE,
1510 .cra_init = omap_sham_cra_sha1_init,
1511 .cra_exit = omap_sham_cra_exit,
1512 }
1513},
1514{
1515 .init = omap_sham_init,
1516 .update = omap_sham_update,
1517 .final = omap_sham_final,
1518 .finup = omap_sham_finup,
1519 .digest = omap_sham_digest,
1520 .setkey = omap_sham_setkey,
1521 .halg.digestsize = MD5_DIGEST_SIZE,
1522 .halg.base = {
1523 .cra_name = "hmac(md5)",
1524 .cra_driver_name = "omap-hmac-md5",
1525 .cra_priority = 400,
1526 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1527 CRYPTO_ALG_KERN_DRIVER_ONLY |
1528 CRYPTO_ALG_ASYNC |
1529 CRYPTO_ALG_NEED_FALLBACK,
1530 .cra_blocksize = SHA1_BLOCK_SIZE,
1531 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1532 sizeof(struct omap_sham_hmac_ctx),
1533 .cra_alignmask = OMAP_ALIGN_MASK,
1534 .cra_module = THIS_MODULE,
1535 .cra_init = omap_sham_cra_md5_init,
1536 .cra_exit = omap_sham_cra_exit,
1537 }
1538}
1539};
1540
1541/* OMAP4 has some algs in addition to what OMAP2 has */
1542static struct ahash_alg algs_sha224_sha256[] = {
1543{
1544 .init = omap_sham_init,
1545 .update = omap_sham_update,
1546 .final = omap_sham_final,
1547 .finup = omap_sham_finup,
1548 .digest = omap_sham_digest,
1549 .halg.digestsize = SHA224_DIGEST_SIZE,
1550 .halg.base = {
1551 .cra_name = "sha224",
1552 .cra_driver_name = "omap-sha224",
1553 .cra_priority = 400,
1554 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1555 CRYPTO_ALG_ASYNC |
1556 CRYPTO_ALG_NEED_FALLBACK,
1557 .cra_blocksize = SHA224_BLOCK_SIZE,
1558 .cra_ctxsize = sizeof(struct omap_sham_ctx),
1559 .cra_alignmask = OMAP_ALIGN_MASK,
1560 .cra_module = THIS_MODULE,
1561 .cra_init = omap_sham_cra_init,
1562 .cra_exit = omap_sham_cra_exit,
1563 }
1564},
1565{
1566 .init = omap_sham_init,
1567 .update = omap_sham_update,
1568 .final = omap_sham_final,
1569 .finup = omap_sham_finup,
1570 .digest = omap_sham_digest,
1571 .halg.digestsize = SHA256_DIGEST_SIZE,
1572 .halg.base = {
1573 .cra_name = "sha256",
1574 .cra_driver_name = "omap-sha256",
1575 .cra_priority = 400,
1576 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1577 CRYPTO_ALG_ASYNC |
1578 CRYPTO_ALG_NEED_FALLBACK,
1579 .cra_blocksize = SHA256_BLOCK_SIZE,
1580 .cra_ctxsize = sizeof(struct omap_sham_ctx),
1581 .cra_alignmask = OMAP_ALIGN_MASK,
1582 .cra_module = THIS_MODULE,
1583 .cra_init = omap_sham_cra_init,
1584 .cra_exit = omap_sham_cra_exit,
1585 }
1586},
1587{
1588 .init = omap_sham_init,
1589 .update = omap_sham_update,
1590 .final = omap_sham_final,
1591 .finup = omap_sham_finup,
1592 .digest = omap_sham_digest,
1593 .setkey = omap_sham_setkey,
1594 .halg.digestsize = SHA224_DIGEST_SIZE,
1595 .halg.base = {
1596 .cra_name = "hmac(sha224)",
1597 .cra_driver_name = "omap-hmac-sha224",
1598 .cra_priority = 400,
1599 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1600 CRYPTO_ALG_ASYNC |
1601 CRYPTO_ALG_NEED_FALLBACK,
1602 .cra_blocksize = SHA224_BLOCK_SIZE,
1603 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1604 sizeof(struct omap_sham_hmac_ctx),
1605 .cra_alignmask = OMAP_ALIGN_MASK,
1606 .cra_module = THIS_MODULE,
1607 .cra_init = omap_sham_cra_sha224_init,
1608 .cra_exit = omap_sham_cra_exit,
1609 }
1610},
1611{
1612 .init = omap_sham_init,
1613 .update = omap_sham_update,
1614 .final = omap_sham_final,
1615 .finup = omap_sham_finup,
1616 .digest = omap_sham_digest,
1617 .setkey = omap_sham_setkey,
1618 .halg.digestsize = SHA256_DIGEST_SIZE,
1619 .halg.base = {
1620 .cra_name = "hmac(sha256)",
1621 .cra_driver_name = "omap-hmac-sha256",
1622 .cra_priority = 400,
1623 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1624 CRYPTO_ALG_ASYNC |
1625 CRYPTO_ALG_NEED_FALLBACK,
1626 .cra_blocksize = SHA256_BLOCK_SIZE,
1627 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1628 sizeof(struct omap_sham_hmac_ctx),
1629 .cra_alignmask = OMAP_ALIGN_MASK,
1630 .cra_module = THIS_MODULE,
1631 .cra_init = omap_sham_cra_sha256_init,
1632 .cra_exit = omap_sham_cra_exit,
1633 }
1634},
1635};
1636
1637static struct ahash_alg algs_sha384_sha512[] = {
1638{
1639 .init = omap_sham_init,
1640 .update = omap_sham_update,
1641 .final = omap_sham_final,
1642 .finup = omap_sham_finup,
1643 .digest = omap_sham_digest,
1644 .halg.digestsize = SHA384_DIGEST_SIZE,
1645 .halg.base = {
1646 .cra_name = "sha384",
1647 .cra_driver_name = "omap-sha384",
1648 .cra_priority = 400,
1649 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1650 CRYPTO_ALG_ASYNC |
1651 CRYPTO_ALG_NEED_FALLBACK,
1652 .cra_blocksize = SHA384_BLOCK_SIZE,
1653 .cra_ctxsize = sizeof(struct omap_sham_ctx),
1654 .cra_alignmask = OMAP_ALIGN_MASK,
1655 .cra_module = THIS_MODULE,
1656 .cra_init = omap_sham_cra_init,
1657 .cra_exit = omap_sham_cra_exit,
1658 }
1659},
1660{
1661 .init = omap_sham_init,
1662 .update = omap_sham_update,
1663 .final = omap_sham_final,
1664 .finup = omap_sham_finup,
1665 .digest = omap_sham_digest,
1666 .halg.digestsize = SHA512_DIGEST_SIZE,
1667 .halg.base = {
1668 .cra_name = "sha512",
1669 .cra_driver_name = "omap-sha512",
1670 .cra_priority = 400,
1671 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1672 CRYPTO_ALG_ASYNC |
1673 CRYPTO_ALG_NEED_FALLBACK,
1674 .cra_blocksize = SHA512_BLOCK_SIZE,
1675 .cra_ctxsize = sizeof(struct omap_sham_ctx),
1676 .cra_alignmask = OMAP_ALIGN_MASK,
1677 .cra_module = THIS_MODULE,
1678 .cra_init = omap_sham_cra_init,
1679 .cra_exit = omap_sham_cra_exit,
1680 }
1681},
1682{
1683 .init = omap_sham_init,
1684 .update = omap_sham_update,
1685 .final = omap_sham_final,
1686 .finup = omap_sham_finup,
1687 .digest = omap_sham_digest,
1688 .setkey = omap_sham_setkey,
1689 .halg.digestsize = SHA384_DIGEST_SIZE,
1690 .halg.base = {
1691 .cra_name = "hmac(sha384)",
1692 .cra_driver_name = "omap-hmac-sha384",
1693 .cra_priority = 400,
1694 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1695 CRYPTO_ALG_ASYNC |
1696 CRYPTO_ALG_NEED_FALLBACK,
1697 .cra_blocksize = SHA384_BLOCK_SIZE,
1698 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1699 sizeof(struct omap_sham_hmac_ctx),
1700 .cra_alignmask = OMAP_ALIGN_MASK,
1701 .cra_module = THIS_MODULE,
1702 .cra_init = omap_sham_cra_sha384_init,
1703 .cra_exit = omap_sham_cra_exit,
1704 }
1705},
1706{
1707 .init = omap_sham_init,
1708 .update = omap_sham_update,
1709 .final = omap_sham_final,
1710 .finup = omap_sham_finup,
1711 .digest = omap_sham_digest,
1712 .setkey = omap_sham_setkey,
1713 .halg.digestsize = SHA512_DIGEST_SIZE,
1714 .halg.base = {
1715 .cra_name = "hmac(sha512)",
1716 .cra_driver_name = "omap-hmac-sha512",
1717 .cra_priority = 400,
1718 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1719 CRYPTO_ALG_ASYNC |
1720 CRYPTO_ALG_NEED_FALLBACK,
1721 .cra_blocksize = SHA512_BLOCK_SIZE,
1722 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1723 sizeof(struct omap_sham_hmac_ctx),
1724 .cra_alignmask = OMAP_ALIGN_MASK,
1725 .cra_module = THIS_MODULE,
1726 .cra_init = omap_sham_cra_sha512_init,
1727 .cra_exit = omap_sham_cra_exit,
1728 }
1729},
1730};
1731
1732static void omap_sham_done_task(unsigned long data)
1733{
1734 struct omap_sham_dev *dd = (struct omap_sham_dev *)data;
1735 int err = 0;
1736
1737 if (!test_bit(FLAGS_BUSY, &dd->flags)) {
1738 omap_sham_handle_queue(dd, NULL);
1739 return;
1740 }
1741
1742 if (test_bit(FLAGS_CPU, &dd->flags)) {
1743 if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags))
1744 goto finish;
1745 } else if (test_bit(FLAGS_DMA_READY, &dd->flags)) {
1746 if (test_and_clear_bit(FLAGS_DMA_ACTIVE, &dd->flags)) {
1747 omap_sham_update_dma_stop(dd);
1748 if (dd->err) {
1749 err = dd->err;
1750 goto finish;
1751 }
1752 }
1753 if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags)) {
1754 /* hash or semi-hash ready */
1755 clear_bit(FLAGS_DMA_READY, &dd->flags);
1756 goto finish;
1757 }
1758 }
1759
1760 return;
1761
1762finish:
1763 dev_dbg(dd->dev, "update done: err: %d\n", err);
1764 /* finish curent request */
1765 omap_sham_finish_req(dd->req, err);
1766
1767 /* If we are not busy, process next req */
1768 if (!test_bit(FLAGS_BUSY, &dd->flags))
1769 omap_sham_handle_queue(dd, NULL);
1770}
1771
1772static irqreturn_t omap_sham_irq_common(struct omap_sham_dev *dd)
1773{
1774 if (!test_bit(FLAGS_BUSY, &dd->flags)) {
1775 dev_warn(dd->dev, "Interrupt when no active requests.\n");
1776 } else {
1777 set_bit(FLAGS_OUTPUT_READY, &dd->flags);
1778 tasklet_schedule(&dd->done_task);
1779 }
1780
1781 return IRQ_HANDLED;
1782}
1783
1784static irqreturn_t omap_sham_irq_omap2(int irq, void *dev_id)
1785{
1786 struct omap_sham_dev *dd = dev_id;
1787
1788 if (unlikely(test_bit(FLAGS_FINAL, &dd->flags)))
1789 /* final -> allow device to go to power-saving mode */
1790 omap_sham_write_mask(dd, SHA_REG_CTRL, 0, SHA_REG_CTRL_LENGTH);
1791
1792 omap_sham_write_mask(dd, SHA_REG_CTRL, SHA_REG_CTRL_OUTPUT_READY,
1793 SHA_REG_CTRL_OUTPUT_READY);
1794 omap_sham_read(dd, SHA_REG_CTRL);
1795
1796 return omap_sham_irq_common(dd);
1797}
1798
1799static irqreturn_t omap_sham_irq_omap4(int irq, void *dev_id)
1800{
1801 struct omap_sham_dev *dd = dev_id;
1802
1803 omap_sham_write_mask(dd, SHA_REG_MASK(dd), 0, SHA_REG_MASK_IT_EN);
1804
1805 return omap_sham_irq_common(dd);
1806}
1807
1808static struct omap_sham_algs_info omap_sham_algs_info_omap2[] = {
1809 {
1810 .algs_list = algs_sha1_md5,
1811 .size = ARRAY_SIZE(algs_sha1_md5),
1812 },
1813};
1814
1815static const struct omap_sham_pdata omap_sham_pdata_omap2 = {
1816 .algs_info = omap_sham_algs_info_omap2,
1817 .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap2),
1818 .flags = BIT(FLAGS_BE32_SHA1),
1819 .digest_size = SHA1_DIGEST_SIZE,
1820 .copy_hash = omap_sham_copy_hash_omap2,
1821 .write_ctrl = omap_sham_write_ctrl_omap2,
1822 .trigger = omap_sham_trigger_omap2,
1823 .poll_irq = omap_sham_poll_irq_omap2,
1824 .intr_hdlr = omap_sham_irq_omap2,
1825 .idigest_ofs = 0x00,
1826 .din_ofs = 0x1c,
1827 .digcnt_ofs = 0x14,
1828 .rev_ofs = 0x5c,
1829 .mask_ofs = 0x60,
1830 .sysstatus_ofs = 0x64,
1831 .major_mask = 0xf0,
1832 .major_shift = 4,
1833 .minor_mask = 0x0f,
1834 .minor_shift = 0,
1835};
1836
1837#ifdef CONFIG_OF
1838static struct omap_sham_algs_info omap_sham_algs_info_omap4[] = {
1839 {
1840 .algs_list = algs_sha1_md5,
1841 .size = ARRAY_SIZE(algs_sha1_md5),
1842 },
1843 {
1844 .algs_list = algs_sha224_sha256,
1845 .size = ARRAY_SIZE(algs_sha224_sha256),
1846 },
1847};
1848
1849static const struct omap_sham_pdata omap_sham_pdata_omap4 = {
1850 .algs_info = omap_sham_algs_info_omap4,
1851 .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap4),
1852 .flags = BIT(FLAGS_AUTO_XOR),
1853 .digest_size = SHA256_DIGEST_SIZE,
1854 .copy_hash = omap_sham_copy_hash_omap4,
1855 .write_ctrl = omap_sham_write_ctrl_omap4,
1856 .trigger = omap_sham_trigger_omap4,
1857 .poll_irq = omap_sham_poll_irq_omap4,
1858 .intr_hdlr = omap_sham_irq_omap4,
1859 .idigest_ofs = 0x020,
1860 .odigest_ofs = 0x0,
1861 .din_ofs = 0x080,
1862 .digcnt_ofs = 0x040,
1863 .rev_ofs = 0x100,
1864 .mask_ofs = 0x110,
1865 .sysstatus_ofs = 0x114,
1866 .mode_ofs = 0x44,
1867 .length_ofs = 0x48,
1868 .major_mask = 0x0700,
1869 .major_shift = 8,
1870 .minor_mask = 0x003f,
1871 .minor_shift = 0,
1872};
1873
1874static struct omap_sham_algs_info omap_sham_algs_info_omap5[] = {
1875 {
1876 .algs_list = algs_sha1_md5,
1877 .size = ARRAY_SIZE(algs_sha1_md5),
1878 },
1879 {
1880 .algs_list = algs_sha224_sha256,
1881 .size = ARRAY_SIZE(algs_sha224_sha256),
1882 },
1883 {
1884 .algs_list = algs_sha384_sha512,
1885 .size = ARRAY_SIZE(algs_sha384_sha512),
1886 },
1887};
1888
1889static const struct omap_sham_pdata omap_sham_pdata_omap5 = {
1890 .algs_info = omap_sham_algs_info_omap5,
1891 .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap5),
1892 .flags = BIT(FLAGS_AUTO_XOR),
1893 .digest_size = SHA512_DIGEST_SIZE,
1894 .copy_hash = omap_sham_copy_hash_omap4,
1895 .write_ctrl = omap_sham_write_ctrl_omap4,
1896 .trigger = omap_sham_trigger_omap4,
1897 .poll_irq = omap_sham_poll_irq_omap4,
1898 .intr_hdlr = omap_sham_irq_omap4,
1899 .idigest_ofs = 0x240,
1900 .odigest_ofs = 0x200,
1901 .din_ofs = 0x080,
1902 .digcnt_ofs = 0x280,
1903 .rev_ofs = 0x100,
1904 .mask_ofs = 0x110,
1905 .sysstatus_ofs = 0x114,
1906 .mode_ofs = 0x284,
1907 .length_ofs = 0x288,
1908 .major_mask = 0x0700,
1909 .major_shift = 8,
1910 .minor_mask = 0x003f,
1911 .minor_shift = 0,
1912};
1913
1914static const struct of_device_id omap_sham_of_match[] = {
1915 {
1916 .compatible = "ti,omap2-sham",
1917 .data = &omap_sham_pdata_omap2,
1918 },
1919 {
1920 .compatible = "ti,omap3-sham",
1921 .data = &omap_sham_pdata_omap2,
1922 },
1923 {
1924 .compatible = "ti,omap4-sham",
1925 .data = &omap_sham_pdata_omap4,
1926 },
1927 {
1928 .compatible = "ti,omap5-sham",
1929 .data = &omap_sham_pdata_omap5,
1930 },
1931 {},
1932};
1933MODULE_DEVICE_TABLE(of, omap_sham_of_match);
1934
1935static int omap_sham_get_res_of(struct omap_sham_dev *dd,
1936 struct device *dev, struct resource *res)
1937{
1938 struct device_node *node = dev->of_node;
1939 const struct of_device_id *match;
1940 int err = 0;
1941
1942 match = of_match_device(of_match_ptr(omap_sham_of_match), dev);
1943 if (!match) {
1944 dev_err(dev, "no compatible OF match\n");
1945 err = -EINVAL;
1946 goto err;
1947 }
1948
1949 err = of_address_to_resource(node, 0, res);
1950 if (err < 0) {
1951 dev_err(dev, "can't translate OF node address\n");
1952 err = -EINVAL;
1953 goto err;
1954 }
1955
1956 dd->irq = irq_of_parse_and_map(node, 0);
1957 if (!dd->irq) {
1958 dev_err(dev, "can't translate OF irq value\n");
1959 err = -EINVAL;
1960 goto err;
1961 }
1962
1963 dd->pdata = match->data;
1964
1965err:
1966 return err;
1967}
1968#else
1969static const struct of_device_id omap_sham_of_match[] = {
1970 {},
1971};
1972
1973static int omap_sham_get_res_of(struct omap_sham_dev *dd,
1974 struct device *dev, struct resource *res)
1975{
1976 return -EINVAL;
1977}
1978#endif
1979
1980static int omap_sham_get_res_pdev(struct omap_sham_dev *dd,
1981 struct platform_device *pdev, struct resource *res)
1982{
1983 struct device *dev = &pdev->dev;
1984 struct resource *r;
1985 int err = 0;
1986
1987 /* Get the base address */
1988 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1989 if (!r) {
1990 dev_err(dev, "no MEM resource info\n");
1991 err = -ENODEV;
1992 goto err;
1993 }
1994 memcpy(res, r, sizeof(*res));
1995
1996 /* Get the IRQ */
1997 dd->irq = platform_get_irq(pdev, 0);
1998 if (dd->irq < 0) {
1999 dev_err(dev, "no IRQ resource info\n");
2000 err = dd->irq;
2001 goto err;
2002 }
2003
2004 /* Only OMAP2/3 can be non-DT */
2005 dd->pdata = &omap_sham_pdata_omap2;
2006
2007err:
2008 return err;
2009}
2010
2011static int omap_sham_probe(struct platform_device *pdev)
2012{
2013 struct omap_sham_dev *dd;
2014 struct device *dev = &pdev->dev;
2015 struct resource res;
2016 dma_cap_mask_t mask;
2017 int err, i, j;
2018 u32 rev;
2019
2020 dd = devm_kzalloc(dev, sizeof(struct omap_sham_dev), GFP_KERNEL);
2021 if (dd == NULL) {
2022 dev_err(dev, "unable to alloc data struct.\n");
2023 err = -ENOMEM;
2024 goto data_err;
2025 }
2026 dd->dev = dev;
2027 platform_set_drvdata(pdev, dd);
2028
2029 INIT_LIST_HEAD(&dd->list);
2030 spin_lock_init(&dd->lock);
2031 tasklet_init(&dd->done_task, omap_sham_done_task, (unsigned long)dd);
2032 crypto_init_queue(&dd->queue, OMAP_SHAM_QUEUE_LENGTH);
2033
2034 err = (dev->of_node) ? omap_sham_get_res_of(dd, dev, &res) :
2035 omap_sham_get_res_pdev(dd, pdev, &res);
2036 if (err)
2037 goto data_err;
2038
2039 dd->io_base = devm_ioremap_resource(dev, &res);
2040 if (IS_ERR(dd->io_base)) {
2041 err = PTR_ERR(dd->io_base);
2042 goto data_err;
2043 }
2044 dd->phys_base = res.start;
2045
2046 err = devm_request_irq(dev, dd->irq, dd->pdata->intr_hdlr,
2047 IRQF_TRIGGER_NONE, dev_name(dev), dd);
2048 if (err) {
2049 dev_err(dev, "unable to request irq %d, err = %d\n",
2050 dd->irq, err);
2051 goto data_err;
2052 }
2053
2054 dma_cap_zero(mask);
2055 dma_cap_set(DMA_SLAVE, mask);
2056
2057 dd->dma_lch = dma_request_chan(dev, "rx");
2058 if (IS_ERR(dd->dma_lch)) {
2059 err = PTR_ERR(dd->dma_lch);
2060 if (err == -EPROBE_DEFER)
2061 goto data_err;
2062
2063 dd->polling_mode = 1;
2064 dev_dbg(dev, "using polling mode instead of dma\n");
2065 }
2066
2067 dd->flags |= dd->pdata->flags;
2068 sham.flags |= dd->pdata->flags;
2069
2070 pm_runtime_use_autosuspend(dev);
2071 pm_runtime_set_autosuspend_delay(dev, DEFAULT_AUTOSUSPEND_DELAY);
2072
2073 pm_runtime_enable(dev);
2074 pm_runtime_irq_safe(dev);
2075
2076 err = pm_runtime_get_sync(dev);
2077 if (err < 0) {
2078 dev_err(dev, "failed to get sync: %d\n", err);
2079 goto err_pm;
2080 }
2081
2082 rev = omap_sham_read(dd, SHA_REG_REV(dd));
2083 pm_runtime_put_sync(&pdev->dev);
2084
2085 dev_info(dev, "hw accel on OMAP rev %u.%u\n",
2086 (rev & dd->pdata->major_mask) >> dd->pdata->major_shift,
2087 (rev & dd->pdata->minor_mask) >> dd->pdata->minor_shift);
2088
2089 spin_lock(&sham.lock);
2090 list_add_tail(&dd->list, &sham.dev_list);
2091 spin_unlock(&sham.lock);
2092
2093 for (i = 0; i < dd->pdata->algs_info_size; i++) {
2094 if (dd->pdata->algs_info[i].registered)
2095 break;
2096
2097 for (j = 0; j < dd->pdata->algs_info[i].size; j++) {
2098 struct ahash_alg *alg;
2099
2100 alg = &dd->pdata->algs_info[i].algs_list[j];
2101 alg->export = omap_sham_export;
2102 alg->import = omap_sham_import;
2103 alg->halg.statesize = sizeof(struct omap_sham_reqctx) +
2104 BUFLEN;
2105 err = crypto_register_ahash(alg);
2106 if (err)
2107 goto err_algs;
2108
2109 dd->pdata->algs_info[i].registered++;
2110 }
2111 }
2112
2113 return 0;
2114
2115err_algs:
2116 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
2117 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
2118 crypto_unregister_ahash(
2119 &dd->pdata->algs_info[i].algs_list[j]);
2120err_pm:
2121 pm_runtime_disable(dev);
2122 if (!dd->polling_mode)
2123 dma_release_channel(dd->dma_lch);
2124data_err:
2125 dev_err(dev, "initialization failed.\n");
2126
2127 return err;
2128}
2129
2130static int omap_sham_remove(struct platform_device *pdev)
2131{
2132 struct omap_sham_dev *dd;
2133 int i, j;
2134
2135 dd = platform_get_drvdata(pdev);
2136 if (!dd)
2137 return -ENODEV;
2138 spin_lock(&sham.lock);
2139 list_del(&dd->list);
2140 spin_unlock(&sham.lock);
2141 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
2142 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--) {
2143 crypto_unregister_ahash(
2144 &dd->pdata->algs_info[i].algs_list[j]);
2145 dd->pdata->algs_info[i].registered--;
2146 }
2147 tasklet_kill(&dd->done_task);
2148 pm_runtime_disable(&pdev->dev);
2149
2150 if (!dd->polling_mode)
2151 dma_release_channel(dd->dma_lch);
2152
2153 return 0;
2154}
2155
2156#ifdef CONFIG_PM_SLEEP
2157static int omap_sham_suspend(struct device *dev)
2158{
2159 pm_runtime_put_sync(dev);
2160 return 0;
2161}
2162
2163static int omap_sham_resume(struct device *dev)
2164{
2165 int err = pm_runtime_get_sync(dev);
2166 if (err < 0) {
2167 dev_err(dev, "failed to get sync: %d\n", err);
2168 return err;
2169 }
2170 return 0;
2171}
2172#endif
2173
2174static SIMPLE_DEV_PM_OPS(omap_sham_pm_ops, omap_sham_suspend, omap_sham_resume);
2175
2176static struct platform_driver omap_sham_driver = {
2177 .probe = omap_sham_probe,
2178 .remove = omap_sham_remove,
2179 .driver = {
2180 .name = "omap-sham",
2181 .pm = &omap_sham_pm_ops,
2182 .of_match_table = omap_sham_of_match,
2183 },
2184};
2185
2186module_platform_driver(omap_sham_driver);
2187
2188MODULE_DESCRIPTION("OMAP SHA1/MD5 hw acceleration support.");
2189MODULE_LICENSE("GPL v2");
2190MODULE_AUTHOR("Dmitry Kasatkin");
2191MODULE_ALIAS("platform:omap-sham");