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