xref: /openbmc/linux/drivers/crypto/omap-aes.c (revision 6e2e3d1d)
1 /*
2  * Cryptographic API.
3  *
4  * Support for OMAP AES 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  */
15 
16 #define pr_fmt(fmt) "%20s: " fmt, __func__
17 #define prn(num) pr_debug(#num "=%d\n", num)
18 #define prx(num) pr_debug(#num "=%x\n", num)
19 
20 #include <linux/err.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/errno.h>
24 #include <linux/kernel.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/omap-dma.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/of.h>
32 #include <linux/of_device.h>
33 #include <linux/of_address.h>
34 #include <linux/io.h>
35 #include <linux/crypto.h>
36 #include <linux/interrupt.h>
37 #include <crypto/scatterwalk.h>
38 #include <crypto/aes.h>
39 
40 #define DST_MAXBURST			4
41 #define DMA_MIN				(DST_MAXBURST * sizeof(u32))
42 
43 #define _calc_walked(inout) (dd->inout##_walk.offset - dd->inout##_sg->offset)
44 
45 /* OMAP TRM gives bitfields as start:end, where start is the higher bit
46    number. For example 7:0 */
47 #define FLD_MASK(start, end)	(((1 << ((start) - (end) + 1)) - 1) << (end))
48 #define FLD_VAL(val, start, end) (((val) << (end)) & FLD_MASK(start, end))
49 
50 #define AES_REG_KEY(dd, x)		((dd)->pdata->key_ofs - \
51 						((x ^ 0x01) * 0x04))
52 #define AES_REG_IV(dd, x)		((dd)->pdata->iv_ofs + ((x) * 0x04))
53 
54 #define AES_REG_CTRL(dd)		((dd)->pdata->ctrl_ofs)
55 #define AES_REG_CTRL_CTR_WIDTH_MASK	(3 << 7)
56 #define AES_REG_CTRL_CTR_WIDTH_32		(0 << 7)
57 #define AES_REG_CTRL_CTR_WIDTH_64		(1 << 7)
58 #define AES_REG_CTRL_CTR_WIDTH_96		(2 << 7)
59 #define AES_REG_CTRL_CTR_WIDTH_128		(3 << 7)
60 #define AES_REG_CTRL_CTR		(1 << 6)
61 #define AES_REG_CTRL_CBC		(1 << 5)
62 #define AES_REG_CTRL_KEY_SIZE		(3 << 3)
63 #define AES_REG_CTRL_DIRECTION		(1 << 2)
64 #define AES_REG_CTRL_INPUT_READY	(1 << 1)
65 #define AES_REG_CTRL_OUTPUT_READY	(1 << 0)
66 
67 #define AES_REG_DATA_N(dd, x)		((dd)->pdata->data_ofs + ((x) * 0x04))
68 
69 #define AES_REG_REV(dd)			((dd)->pdata->rev_ofs)
70 
71 #define AES_REG_MASK(dd)		((dd)->pdata->mask_ofs)
72 #define AES_REG_MASK_SIDLE		(1 << 6)
73 #define AES_REG_MASK_START		(1 << 5)
74 #define AES_REG_MASK_DMA_OUT_EN		(1 << 3)
75 #define AES_REG_MASK_DMA_IN_EN		(1 << 2)
76 #define AES_REG_MASK_SOFTRESET		(1 << 1)
77 #define AES_REG_AUTOIDLE		(1 << 0)
78 
79 #define AES_REG_LENGTH_N(x)		(0x54 + ((x) * 0x04))
80 
81 #define AES_REG_IRQ_STATUS(dd)         ((dd)->pdata->irq_status_ofs)
82 #define AES_REG_IRQ_ENABLE(dd)         ((dd)->pdata->irq_enable_ofs)
83 #define AES_REG_IRQ_DATA_IN            BIT(1)
84 #define AES_REG_IRQ_DATA_OUT           BIT(2)
85 #define DEFAULT_TIMEOUT		(5*HZ)
86 
87 #define FLAGS_MODE_MASK		0x000f
88 #define FLAGS_ENCRYPT		BIT(0)
89 #define FLAGS_CBC		BIT(1)
90 #define FLAGS_GIV		BIT(2)
91 #define FLAGS_CTR		BIT(3)
92 
93 #define FLAGS_INIT		BIT(4)
94 #define FLAGS_FAST		BIT(5)
95 #define FLAGS_BUSY		BIT(6)
96 
97 #define AES_BLOCK_WORDS		(AES_BLOCK_SIZE >> 2)
98 
99 struct omap_aes_ctx {
100 	struct omap_aes_dev *dd;
101 
102 	int		keylen;
103 	u32		key[AES_KEYSIZE_256 / sizeof(u32)];
104 	unsigned long	flags;
105 };
106 
107 struct omap_aes_reqctx {
108 	unsigned long mode;
109 };
110 
111 #define OMAP_AES_QUEUE_LENGTH	1
112 #define OMAP_AES_CACHE_SIZE	0
113 
114 struct omap_aes_algs_info {
115 	struct crypto_alg	*algs_list;
116 	unsigned int		size;
117 	unsigned int		registered;
118 };
119 
120 struct omap_aes_pdata {
121 	struct omap_aes_algs_info	*algs_info;
122 	unsigned int	algs_info_size;
123 
124 	void		(*trigger)(struct omap_aes_dev *dd, int length);
125 
126 	u32		key_ofs;
127 	u32		iv_ofs;
128 	u32		ctrl_ofs;
129 	u32		data_ofs;
130 	u32		rev_ofs;
131 	u32		mask_ofs;
132 	u32             irq_enable_ofs;
133 	u32             irq_status_ofs;
134 
135 	u32		dma_enable_in;
136 	u32		dma_enable_out;
137 	u32		dma_start;
138 
139 	u32		major_mask;
140 	u32		major_shift;
141 	u32		minor_mask;
142 	u32		minor_shift;
143 };
144 
145 struct omap_aes_dev {
146 	struct list_head	list;
147 	unsigned long		phys_base;
148 	void __iomem		*io_base;
149 	struct omap_aes_ctx	*ctx;
150 	struct device		*dev;
151 	unsigned long		flags;
152 	int			err;
153 
154 	spinlock_t		lock;
155 	struct crypto_queue	queue;
156 
157 	struct tasklet_struct	done_task;
158 	struct tasklet_struct	queue_task;
159 
160 	struct ablkcipher_request	*req;
161 
162 	/*
163 	 * total is used by PIO mode for book keeping so introduce
164 	 * variable total_save as need it to calc page_order
165 	 */
166 	size_t				total;
167 	size_t				total_save;
168 
169 	struct scatterlist		*in_sg;
170 	struct scatterlist		*out_sg;
171 
172 	/* Buffers for copying for unaligned cases */
173 	struct scatterlist		in_sgl;
174 	struct scatterlist		out_sgl;
175 	struct scatterlist		*orig_out;
176 	int				sgs_copied;
177 
178 	struct scatter_walk		in_walk;
179 	struct scatter_walk		out_walk;
180 	int			dma_in;
181 	struct dma_chan		*dma_lch_in;
182 	int			dma_out;
183 	struct dma_chan		*dma_lch_out;
184 	int			in_sg_len;
185 	int			out_sg_len;
186 	int			pio_only;
187 	const struct omap_aes_pdata	*pdata;
188 };
189 
190 /* keep registered devices data here */
191 static LIST_HEAD(dev_list);
192 static DEFINE_SPINLOCK(list_lock);
193 
194 #ifdef DEBUG
195 #define omap_aes_read(dd, offset)				\
196 ({								\
197 	int _read_ret;						\
198 	_read_ret = __raw_readl(dd->io_base + offset);		\
199 	pr_debug("omap_aes_read(" #offset "=%#x)= %#x\n",	\
200 		 offset, _read_ret);				\
201 	_read_ret;						\
202 })
203 #else
204 static inline u32 omap_aes_read(struct omap_aes_dev *dd, u32 offset)
205 {
206 	return __raw_readl(dd->io_base + offset);
207 }
208 #endif
209 
210 #ifdef DEBUG
211 #define omap_aes_write(dd, offset, value)				\
212 	do {								\
213 		pr_debug("omap_aes_write(" #offset "=%#x) value=%#x\n",	\
214 			 offset, value);				\
215 		__raw_writel(value, dd->io_base + offset);		\
216 	} while (0)
217 #else
218 static inline void omap_aes_write(struct omap_aes_dev *dd, u32 offset,
219 				  u32 value)
220 {
221 	__raw_writel(value, dd->io_base + offset);
222 }
223 #endif
224 
225 static inline void omap_aes_write_mask(struct omap_aes_dev *dd, u32 offset,
226 					u32 value, u32 mask)
227 {
228 	u32 val;
229 
230 	val = omap_aes_read(dd, offset);
231 	val &= ~mask;
232 	val |= value;
233 	omap_aes_write(dd, offset, val);
234 }
235 
236 static void omap_aes_write_n(struct omap_aes_dev *dd, u32 offset,
237 					u32 *value, int count)
238 {
239 	for (; count--; value++, offset += 4)
240 		omap_aes_write(dd, offset, *value);
241 }
242 
243 static int omap_aes_hw_init(struct omap_aes_dev *dd)
244 {
245 	if (!(dd->flags & FLAGS_INIT)) {
246 		dd->flags |= FLAGS_INIT;
247 		dd->err = 0;
248 	}
249 
250 	return 0;
251 }
252 
253 static int omap_aes_write_ctrl(struct omap_aes_dev *dd)
254 {
255 	unsigned int key32;
256 	int i, err;
257 	u32 val, mask = 0;
258 
259 	err = omap_aes_hw_init(dd);
260 	if (err)
261 		return err;
262 
263 	key32 = dd->ctx->keylen / sizeof(u32);
264 
265 	/* it seems a key should always be set even if it has not changed */
266 	for (i = 0; i < key32; i++) {
267 		omap_aes_write(dd, AES_REG_KEY(dd, i),
268 			__le32_to_cpu(dd->ctx->key[i]));
269 	}
270 
271 	if ((dd->flags & (FLAGS_CBC | FLAGS_CTR)) && dd->req->info)
272 		omap_aes_write_n(dd, AES_REG_IV(dd, 0), dd->req->info, 4);
273 
274 	val = FLD_VAL(((dd->ctx->keylen >> 3) - 1), 4, 3);
275 	if (dd->flags & FLAGS_CBC)
276 		val |= AES_REG_CTRL_CBC;
277 	if (dd->flags & FLAGS_CTR) {
278 		val |= AES_REG_CTRL_CTR | AES_REG_CTRL_CTR_WIDTH_128;
279 		mask = AES_REG_CTRL_CTR | AES_REG_CTRL_CTR_WIDTH_MASK;
280 	}
281 	if (dd->flags & FLAGS_ENCRYPT)
282 		val |= AES_REG_CTRL_DIRECTION;
283 
284 	mask |= AES_REG_CTRL_CBC | AES_REG_CTRL_DIRECTION |
285 			AES_REG_CTRL_KEY_SIZE;
286 
287 	omap_aes_write_mask(dd, AES_REG_CTRL(dd), val, mask);
288 
289 	return 0;
290 }
291 
292 static void omap_aes_dma_trigger_omap2(struct omap_aes_dev *dd, int length)
293 {
294 	u32 mask, val;
295 
296 	val = dd->pdata->dma_start;
297 
298 	if (dd->dma_lch_out != NULL)
299 		val |= dd->pdata->dma_enable_out;
300 	if (dd->dma_lch_in != NULL)
301 		val |= dd->pdata->dma_enable_in;
302 
303 	mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
304 	       dd->pdata->dma_start;
305 
306 	omap_aes_write_mask(dd, AES_REG_MASK(dd), val, mask);
307 
308 }
309 
310 static void omap_aes_dma_trigger_omap4(struct omap_aes_dev *dd, int length)
311 {
312 	omap_aes_write(dd, AES_REG_LENGTH_N(0), length);
313 	omap_aes_write(dd, AES_REG_LENGTH_N(1), 0);
314 
315 	omap_aes_dma_trigger_omap2(dd, length);
316 }
317 
318 static void omap_aes_dma_stop(struct omap_aes_dev *dd)
319 {
320 	u32 mask;
321 
322 	mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
323 	       dd->pdata->dma_start;
324 
325 	omap_aes_write_mask(dd, AES_REG_MASK(dd), 0, mask);
326 }
327 
328 static struct omap_aes_dev *omap_aes_find_dev(struct omap_aes_ctx *ctx)
329 {
330 	struct omap_aes_dev *dd = NULL, *tmp;
331 
332 	spin_lock_bh(&list_lock);
333 	if (!ctx->dd) {
334 		list_for_each_entry(tmp, &dev_list, list) {
335 			/* FIXME: take fist available aes core */
336 			dd = tmp;
337 			break;
338 		}
339 		ctx->dd = dd;
340 	} else {
341 		/* already found before */
342 		dd = ctx->dd;
343 	}
344 	spin_unlock_bh(&list_lock);
345 
346 	return dd;
347 }
348 
349 static void omap_aes_dma_out_callback(void *data)
350 {
351 	struct omap_aes_dev *dd = data;
352 
353 	/* dma_lch_out - completed */
354 	tasklet_schedule(&dd->done_task);
355 }
356 
357 static int omap_aes_dma_init(struct omap_aes_dev *dd)
358 {
359 	int err = -ENOMEM;
360 	dma_cap_mask_t mask;
361 
362 	dd->dma_lch_out = NULL;
363 	dd->dma_lch_in = NULL;
364 
365 	dma_cap_zero(mask);
366 	dma_cap_set(DMA_SLAVE, mask);
367 
368 	dd->dma_lch_in = dma_request_slave_channel_compat(mask,
369 							  omap_dma_filter_fn,
370 							  &dd->dma_in,
371 							  dd->dev, "rx");
372 	if (!dd->dma_lch_in) {
373 		dev_err(dd->dev, "Unable to request in DMA channel\n");
374 		goto err_dma_in;
375 	}
376 
377 	dd->dma_lch_out = dma_request_slave_channel_compat(mask,
378 							   omap_dma_filter_fn,
379 							   &dd->dma_out,
380 							   dd->dev, "tx");
381 	if (!dd->dma_lch_out) {
382 		dev_err(dd->dev, "Unable to request out DMA channel\n");
383 		goto err_dma_out;
384 	}
385 
386 	return 0;
387 
388 err_dma_out:
389 	dma_release_channel(dd->dma_lch_in);
390 err_dma_in:
391 	if (err)
392 		pr_err("error: %d\n", err);
393 	return err;
394 }
395 
396 static void omap_aes_dma_cleanup(struct omap_aes_dev *dd)
397 {
398 	dma_release_channel(dd->dma_lch_out);
399 	dma_release_channel(dd->dma_lch_in);
400 }
401 
402 static void sg_copy_buf(void *buf, struct scatterlist *sg,
403 			      unsigned int start, unsigned int nbytes, int out)
404 {
405 	struct scatter_walk walk;
406 
407 	if (!nbytes)
408 		return;
409 
410 	scatterwalk_start(&walk, sg);
411 	scatterwalk_advance(&walk, start);
412 	scatterwalk_copychunks(buf, &walk, nbytes, out);
413 	scatterwalk_done(&walk, out, 0);
414 }
415 
416 static int omap_aes_crypt_dma(struct crypto_tfm *tfm,
417 		struct scatterlist *in_sg, struct scatterlist *out_sg,
418 		int in_sg_len, int out_sg_len)
419 {
420 	struct omap_aes_ctx *ctx = crypto_tfm_ctx(tfm);
421 	struct omap_aes_dev *dd = ctx->dd;
422 	struct dma_async_tx_descriptor *tx_in, *tx_out;
423 	struct dma_slave_config cfg;
424 	int ret;
425 
426 	if (dd->pio_only) {
427 		scatterwalk_start(&dd->in_walk, dd->in_sg);
428 		scatterwalk_start(&dd->out_walk, dd->out_sg);
429 
430 		/* Enable DATAIN interrupt and let it take
431 		   care of the rest */
432 		omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x2);
433 		return 0;
434 	}
435 
436 	dma_sync_sg_for_device(dd->dev, dd->in_sg, in_sg_len, DMA_TO_DEVICE);
437 
438 	memset(&cfg, 0, sizeof(cfg));
439 
440 	cfg.src_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
441 	cfg.dst_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
442 	cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
443 	cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
444 	cfg.src_maxburst = DST_MAXBURST;
445 	cfg.dst_maxburst = DST_MAXBURST;
446 
447 	/* IN */
448 	ret = dmaengine_slave_config(dd->dma_lch_in, &cfg);
449 	if (ret) {
450 		dev_err(dd->dev, "can't configure IN dmaengine slave: %d\n",
451 			ret);
452 		return ret;
453 	}
454 
455 	tx_in = dmaengine_prep_slave_sg(dd->dma_lch_in, in_sg, in_sg_len,
456 					DMA_MEM_TO_DEV,
457 					DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
458 	if (!tx_in) {
459 		dev_err(dd->dev, "IN prep_slave_sg() failed\n");
460 		return -EINVAL;
461 	}
462 
463 	/* No callback necessary */
464 	tx_in->callback_param = dd;
465 
466 	/* OUT */
467 	ret = dmaengine_slave_config(dd->dma_lch_out, &cfg);
468 	if (ret) {
469 		dev_err(dd->dev, "can't configure OUT dmaengine slave: %d\n",
470 			ret);
471 		return ret;
472 	}
473 
474 	tx_out = dmaengine_prep_slave_sg(dd->dma_lch_out, out_sg, out_sg_len,
475 					DMA_DEV_TO_MEM,
476 					DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
477 	if (!tx_out) {
478 		dev_err(dd->dev, "OUT prep_slave_sg() failed\n");
479 		return -EINVAL;
480 	}
481 
482 	tx_out->callback = omap_aes_dma_out_callback;
483 	tx_out->callback_param = dd;
484 
485 	dmaengine_submit(tx_in);
486 	dmaengine_submit(tx_out);
487 
488 	dma_async_issue_pending(dd->dma_lch_in);
489 	dma_async_issue_pending(dd->dma_lch_out);
490 
491 	/* start DMA */
492 	dd->pdata->trigger(dd, dd->total);
493 
494 	return 0;
495 }
496 
497 static int omap_aes_crypt_dma_start(struct omap_aes_dev *dd)
498 {
499 	struct crypto_tfm *tfm = crypto_ablkcipher_tfm(
500 					crypto_ablkcipher_reqtfm(dd->req));
501 	int err;
502 
503 	pr_debug("total: %d\n", dd->total);
504 
505 	if (!dd->pio_only) {
506 		err = dma_map_sg(dd->dev, dd->in_sg, dd->in_sg_len,
507 				 DMA_TO_DEVICE);
508 		if (!err) {
509 			dev_err(dd->dev, "dma_map_sg() error\n");
510 			return -EINVAL;
511 		}
512 
513 		err = dma_map_sg(dd->dev, dd->out_sg, dd->out_sg_len,
514 				 DMA_FROM_DEVICE);
515 		if (!err) {
516 			dev_err(dd->dev, "dma_map_sg() error\n");
517 			return -EINVAL;
518 		}
519 	}
520 
521 	err = omap_aes_crypt_dma(tfm, dd->in_sg, dd->out_sg, dd->in_sg_len,
522 				 dd->out_sg_len);
523 	if (err && !dd->pio_only) {
524 		dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
525 		dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
526 			     DMA_FROM_DEVICE);
527 	}
528 
529 	return err;
530 }
531 
532 static void omap_aes_finish_req(struct omap_aes_dev *dd, int err)
533 {
534 	struct ablkcipher_request *req = dd->req;
535 
536 	pr_debug("err: %d\n", err);
537 
538 	dd->flags &= ~FLAGS_BUSY;
539 
540 	req->base.complete(&req->base, err);
541 }
542 
543 static int omap_aes_crypt_dma_stop(struct omap_aes_dev *dd)
544 {
545 	int err = 0;
546 
547 	pr_debug("total: %d\n", dd->total);
548 
549 	omap_aes_dma_stop(dd);
550 
551 	dmaengine_terminate_all(dd->dma_lch_in);
552 	dmaengine_terminate_all(dd->dma_lch_out);
553 
554 	return err;
555 }
556 
557 static int omap_aes_check_aligned(struct scatterlist *sg, int total)
558 {
559 	int len = 0;
560 
561 	if (!IS_ALIGNED(total, AES_BLOCK_SIZE))
562 		return -EINVAL;
563 
564 	while (sg) {
565 		if (!IS_ALIGNED(sg->offset, 4))
566 			return -1;
567 		if (!IS_ALIGNED(sg->length, AES_BLOCK_SIZE))
568 			return -1;
569 
570 		len += sg->length;
571 		sg = sg_next(sg);
572 	}
573 
574 	if (len != total)
575 		return -1;
576 
577 	return 0;
578 }
579 
580 static int omap_aes_copy_sgs(struct omap_aes_dev *dd)
581 {
582 	void *buf_in, *buf_out;
583 	int pages, total;
584 
585 	total = ALIGN(dd->total, AES_BLOCK_SIZE);
586 	pages = get_order(total);
587 
588 	buf_in = (void *)__get_free_pages(GFP_ATOMIC, pages);
589 	buf_out = (void *)__get_free_pages(GFP_ATOMIC, pages);
590 
591 	if (!buf_in || !buf_out) {
592 		pr_err("Couldn't allocated pages for unaligned cases.\n");
593 		return -1;
594 	}
595 
596 	dd->orig_out = dd->out_sg;
597 
598 	sg_copy_buf(buf_in, dd->in_sg, 0, dd->total, 0);
599 
600 	sg_init_table(&dd->in_sgl, 1);
601 	sg_set_buf(&dd->in_sgl, buf_in, total);
602 	dd->in_sg = &dd->in_sgl;
603 
604 	sg_init_table(&dd->out_sgl, 1);
605 	sg_set_buf(&dd->out_sgl, buf_out, total);
606 	dd->out_sg = &dd->out_sgl;
607 
608 	return 0;
609 }
610 
611 static int omap_aes_handle_queue(struct omap_aes_dev *dd,
612 			       struct ablkcipher_request *req)
613 {
614 	struct crypto_async_request *async_req, *backlog;
615 	struct omap_aes_ctx *ctx;
616 	struct omap_aes_reqctx *rctx;
617 	unsigned long flags;
618 	int err, ret = 0, len;
619 
620 	spin_lock_irqsave(&dd->lock, flags);
621 	if (req)
622 		ret = ablkcipher_enqueue_request(&dd->queue, req);
623 	if (dd->flags & FLAGS_BUSY) {
624 		spin_unlock_irqrestore(&dd->lock, flags);
625 		return ret;
626 	}
627 	backlog = crypto_get_backlog(&dd->queue);
628 	async_req = crypto_dequeue_request(&dd->queue);
629 	if (async_req)
630 		dd->flags |= FLAGS_BUSY;
631 	spin_unlock_irqrestore(&dd->lock, flags);
632 
633 	if (!async_req)
634 		return ret;
635 
636 	if (backlog)
637 		backlog->complete(backlog, -EINPROGRESS);
638 
639 	req = ablkcipher_request_cast(async_req);
640 
641 	/* assign new request to device */
642 	dd->req = req;
643 	dd->total = req->nbytes;
644 	dd->total_save = req->nbytes;
645 	dd->in_sg = req->src;
646 	dd->out_sg = req->dst;
647 
648 	if (omap_aes_check_aligned(dd->in_sg, dd->total) ||
649 	    omap_aes_check_aligned(dd->out_sg, dd->total)) {
650 		if (omap_aes_copy_sgs(dd))
651 			pr_err("Failed to copy SGs for unaligned cases\n");
652 		dd->sgs_copied = 1;
653 	} else {
654 		dd->sgs_copied = 0;
655 	}
656 
657 	len = ALIGN(dd->total, AES_BLOCK_SIZE);
658 	dd->in_sg_len = scatterwalk_bytes_sglen(dd->in_sg, len);
659 	dd->out_sg_len = scatterwalk_bytes_sglen(dd->out_sg, len);
660 	BUG_ON(dd->in_sg_len < 0 || dd->out_sg_len < 0);
661 
662 	rctx = ablkcipher_request_ctx(req);
663 	ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
664 	rctx->mode &= FLAGS_MODE_MASK;
665 	dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode;
666 
667 	dd->ctx = ctx;
668 	ctx->dd = dd;
669 
670 	err = omap_aes_write_ctrl(dd);
671 	if (!err)
672 		err = omap_aes_crypt_dma_start(dd);
673 	if (err) {
674 		/* aes_task will not finish it, so do it here */
675 		omap_aes_finish_req(dd, err);
676 		tasklet_schedule(&dd->queue_task);
677 	}
678 
679 	return ret; /* return ret, which is enqueue return value */
680 }
681 
682 static void omap_aes_done_task(unsigned long data)
683 {
684 	struct omap_aes_dev *dd = (struct omap_aes_dev *)data;
685 	void *buf_in, *buf_out;
686 	int pages, len;
687 
688 	pr_debug("enter done_task\n");
689 
690 	if (!dd->pio_only) {
691 		dma_sync_sg_for_device(dd->dev, dd->out_sg, dd->out_sg_len,
692 				       DMA_FROM_DEVICE);
693 		dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
694 		dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
695 			     DMA_FROM_DEVICE);
696 		omap_aes_crypt_dma_stop(dd);
697 	}
698 
699 	if (dd->sgs_copied) {
700 		buf_in = sg_virt(&dd->in_sgl);
701 		buf_out = sg_virt(&dd->out_sgl);
702 
703 		sg_copy_buf(buf_out, dd->orig_out, 0, dd->total_save, 1);
704 
705 		len = ALIGN(dd->total_save, AES_BLOCK_SIZE);
706 		pages = get_order(len);
707 		free_pages((unsigned long)buf_in, pages);
708 		free_pages((unsigned long)buf_out, pages);
709 	}
710 
711 	omap_aes_finish_req(dd, 0);
712 	omap_aes_handle_queue(dd, NULL);
713 
714 	pr_debug("exit\n");
715 }
716 
717 static void omap_aes_queue_task(unsigned long data)
718 {
719 	struct omap_aes_dev *dd = (struct omap_aes_dev *)data;
720 
721 	omap_aes_handle_queue(dd, NULL);
722 }
723 
724 static int omap_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
725 {
726 	struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(
727 			crypto_ablkcipher_reqtfm(req));
728 	struct omap_aes_reqctx *rctx = ablkcipher_request_ctx(req);
729 	struct omap_aes_dev *dd;
730 
731 	pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req->nbytes,
732 		  !!(mode & FLAGS_ENCRYPT),
733 		  !!(mode & FLAGS_CBC));
734 
735 	dd = omap_aes_find_dev(ctx);
736 	if (!dd)
737 		return -ENODEV;
738 
739 	rctx->mode = mode;
740 
741 	return omap_aes_handle_queue(dd, req);
742 }
743 
744 /* ********************** ALG API ************************************ */
745 
746 static int omap_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
747 			   unsigned int keylen)
748 {
749 	struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(tfm);
750 
751 	if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
752 		   keylen != AES_KEYSIZE_256)
753 		return -EINVAL;
754 
755 	pr_debug("enter, keylen: %d\n", keylen);
756 
757 	memcpy(ctx->key, key, keylen);
758 	ctx->keylen = keylen;
759 
760 	return 0;
761 }
762 
763 static int omap_aes_ecb_encrypt(struct ablkcipher_request *req)
764 {
765 	return omap_aes_crypt(req, FLAGS_ENCRYPT);
766 }
767 
768 static int omap_aes_ecb_decrypt(struct ablkcipher_request *req)
769 {
770 	return omap_aes_crypt(req, 0);
771 }
772 
773 static int omap_aes_cbc_encrypt(struct ablkcipher_request *req)
774 {
775 	return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC);
776 }
777 
778 static int omap_aes_cbc_decrypt(struct ablkcipher_request *req)
779 {
780 	return omap_aes_crypt(req, FLAGS_CBC);
781 }
782 
783 static int omap_aes_ctr_encrypt(struct ablkcipher_request *req)
784 {
785 	return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CTR);
786 }
787 
788 static int omap_aes_ctr_decrypt(struct ablkcipher_request *req)
789 {
790 	return omap_aes_crypt(req, FLAGS_CTR);
791 }
792 
793 static int omap_aes_cra_init(struct crypto_tfm *tfm)
794 {
795 	struct omap_aes_dev *dd = NULL;
796 	int err;
797 
798 	/* Find AES device, currently picks the first device */
799 	spin_lock_bh(&list_lock);
800 	list_for_each_entry(dd, &dev_list, list) {
801 		break;
802 	}
803 	spin_unlock_bh(&list_lock);
804 
805 	err = pm_runtime_get_sync(dd->dev);
806 	if (err < 0) {
807 		dev_err(dd->dev, "%s: failed to get_sync(%d)\n",
808 			__func__, err);
809 		return err;
810 	}
811 
812 	tfm->crt_ablkcipher.reqsize = sizeof(struct omap_aes_reqctx);
813 
814 	return 0;
815 }
816 
817 static void omap_aes_cra_exit(struct crypto_tfm *tfm)
818 {
819 	struct omap_aes_dev *dd = NULL;
820 
821 	/* Find AES device, currently picks the first device */
822 	spin_lock_bh(&list_lock);
823 	list_for_each_entry(dd, &dev_list, list) {
824 		break;
825 	}
826 	spin_unlock_bh(&list_lock);
827 
828 	pm_runtime_put_sync(dd->dev);
829 }
830 
831 /* ********************** ALGS ************************************ */
832 
833 static struct crypto_alg algs_ecb_cbc[] = {
834 {
835 	.cra_name		= "ecb(aes)",
836 	.cra_driver_name	= "ecb-aes-omap",
837 	.cra_priority		= 300,
838 	.cra_flags		= CRYPTO_ALG_TYPE_ABLKCIPHER |
839 				  CRYPTO_ALG_KERN_DRIVER_ONLY |
840 				  CRYPTO_ALG_ASYNC,
841 	.cra_blocksize		= AES_BLOCK_SIZE,
842 	.cra_ctxsize		= sizeof(struct omap_aes_ctx),
843 	.cra_alignmask		= 0,
844 	.cra_type		= &crypto_ablkcipher_type,
845 	.cra_module		= THIS_MODULE,
846 	.cra_init		= omap_aes_cra_init,
847 	.cra_exit		= omap_aes_cra_exit,
848 	.cra_u.ablkcipher = {
849 		.min_keysize	= AES_MIN_KEY_SIZE,
850 		.max_keysize	= AES_MAX_KEY_SIZE,
851 		.setkey		= omap_aes_setkey,
852 		.encrypt	= omap_aes_ecb_encrypt,
853 		.decrypt	= omap_aes_ecb_decrypt,
854 	}
855 },
856 {
857 	.cra_name		= "cbc(aes)",
858 	.cra_driver_name	= "cbc-aes-omap",
859 	.cra_priority		= 300,
860 	.cra_flags		= CRYPTO_ALG_TYPE_ABLKCIPHER |
861 				  CRYPTO_ALG_KERN_DRIVER_ONLY |
862 				  CRYPTO_ALG_ASYNC,
863 	.cra_blocksize		= AES_BLOCK_SIZE,
864 	.cra_ctxsize		= sizeof(struct omap_aes_ctx),
865 	.cra_alignmask		= 0,
866 	.cra_type		= &crypto_ablkcipher_type,
867 	.cra_module		= THIS_MODULE,
868 	.cra_init		= omap_aes_cra_init,
869 	.cra_exit		= omap_aes_cra_exit,
870 	.cra_u.ablkcipher = {
871 		.min_keysize	= AES_MIN_KEY_SIZE,
872 		.max_keysize	= AES_MAX_KEY_SIZE,
873 		.ivsize		= AES_BLOCK_SIZE,
874 		.setkey		= omap_aes_setkey,
875 		.encrypt	= omap_aes_cbc_encrypt,
876 		.decrypt	= omap_aes_cbc_decrypt,
877 	}
878 }
879 };
880 
881 static struct crypto_alg algs_ctr[] = {
882 {
883 	.cra_name		= "ctr(aes)",
884 	.cra_driver_name	= "ctr-aes-omap",
885 	.cra_priority		= 300,
886 	.cra_flags		= CRYPTO_ALG_TYPE_ABLKCIPHER |
887 				  CRYPTO_ALG_KERN_DRIVER_ONLY |
888 				  CRYPTO_ALG_ASYNC,
889 	.cra_blocksize		= AES_BLOCK_SIZE,
890 	.cra_ctxsize		= sizeof(struct omap_aes_ctx),
891 	.cra_alignmask		= 0,
892 	.cra_type		= &crypto_ablkcipher_type,
893 	.cra_module		= THIS_MODULE,
894 	.cra_init		= omap_aes_cra_init,
895 	.cra_exit		= omap_aes_cra_exit,
896 	.cra_u.ablkcipher = {
897 		.min_keysize	= AES_MIN_KEY_SIZE,
898 		.max_keysize	= AES_MAX_KEY_SIZE,
899 		.geniv		= "eseqiv",
900 		.ivsize		= AES_BLOCK_SIZE,
901 		.setkey		= omap_aes_setkey,
902 		.encrypt	= omap_aes_ctr_encrypt,
903 		.decrypt	= omap_aes_ctr_decrypt,
904 	}
905 } ,
906 };
907 
908 static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc[] = {
909 	{
910 		.algs_list	= algs_ecb_cbc,
911 		.size		= ARRAY_SIZE(algs_ecb_cbc),
912 	},
913 };
914 
915 static const struct omap_aes_pdata omap_aes_pdata_omap2 = {
916 	.algs_info	= omap_aes_algs_info_ecb_cbc,
917 	.algs_info_size	= ARRAY_SIZE(omap_aes_algs_info_ecb_cbc),
918 	.trigger	= omap_aes_dma_trigger_omap2,
919 	.key_ofs	= 0x1c,
920 	.iv_ofs		= 0x20,
921 	.ctrl_ofs	= 0x30,
922 	.data_ofs	= 0x34,
923 	.rev_ofs	= 0x44,
924 	.mask_ofs	= 0x48,
925 	.dma_enable_in	= BIT(2),
926 	.dma_enable_out	= BIT(3),
927 	.dma_start	= BIT(5),
928 	.major_mask	= 0xf0,
929 	.major_shift	= 4,
930 	.minor_mask	= 0x0f,
931 	.minor_shift	= 0,
932 };
933 
934 #ifdef CONFIG_OF
935 static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc_ctr[] = {
936 	{
937 		.algs_list	= algs_ecb_cbc,
938 		.size		= ARRAY_SIZE(algs_ecb_cbc),
939 	},
940 	{
941 		.algs_list	= algs_ctr,
942 		.size		= ARRAY_SIZE(algs_ctr),
943 	},
944 };
945 
946 static const struct omap_aes_pdata omap_aes_pdata_omap3 = {
947 	.algs_info	= omap_aes_algs_info_ecb_cbc_ctr,
948 	.algs_info_size	= ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
949 	.trigger	= omap_aes_dma_trigger_omap2,
950 	.key_ofs	= 0x1c,
951 	.iv_ofs		= 0x20,
952 	.ctrl_ofs	= 0x30,
953 	.data_ofs	= 0x34,
954 	.rev_ofs	= 0x44,
955 	.mask_ofs	= 0x48,
956 	.dma_enable_in	= BIT(2),
957 	.dma_enable_out	= BIT(3),
958 	.dma_start	= BIT(5),
959 	.major_mask	= 0xf0,
960 	.major_shift	= 4,
961 	.minor_mask	= 0x0f,
962 	.minor_shift	= 0,
963 };
964 
965 static const struct omap_aes_pdata omap_aes_pdata_omap4 = {
966 	.algs_info	= omap_aes_algs_info_ecb_cbc_ctr,
967 	.algs_info_size	= ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
968 	.trigger	= omap_aes_dma_trigger_omap4,
969 	.key_ofs	= 0x3c,
970 	.iv_ofs		= 0x40,
971 	.ctrl_ofs	= 0x50,
972 	.data_ofs	= 0x60,
973 	.rev_ofs	= 0x80,
974 	.mask_ofs	= 0x84,
975 	.irq_status_ofs = 0x8c,
976 	.irq_enable_ofs = 0x90,
977 	.dma_enable_in	= BIT(5),
978 	.dma_enable_out	= BIT(6),
979 	.major_mask	= 0x0700,
980 	.major_shift	= 8,
981 	.minor_mask	= 0x003f,
982 	.minor_shift	= 0,
983 };
984 
985 static irqreturn_t omap_aes_irq(int irq, void *dev_id)
986 {
987 	struct omap_aes_dev *dd = dev_id;
988 	u32 status, i;
989 	u32 *src, *dst;
990 
991 	status = omap_aes_read(dd, AES_REG_IRQ_STATUS(dd));
992 	if (status & AES_REG_IRQ_DATA_IN) {
993 		omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x0);
994 
995 		BUG_ON(!dd->in_sg);
996 
997 		BUG_ON(_calc_walked(in) > dd->in_sg->length);
998 
999 		src = sg_virt(dd->in_sg) + _calc_walked(in);
1000 
1001 		for (i = 0; i < AES_BLOCK_WORDS; i++) {
1002 			omap_aes_write(dd, AES_REG_DATA_N(dd, i), *src);
1003 
1004 			scatterwalk_advance(&dd->in_walk, 4);
1005 			if (dd->in_sg->length == _calc_walked(in)) {
1006 				dd->in_sg = sg_next(dd->in_sg);
1007 				if (dd->in_sg) {
1008 					scatterwalk_start(&dd->in_walk,
1009 							  dd->in_sg);
1010 					src = sg_virt(dd->in_sg) +
1011 					      _calc_walked(in);
1012 				}
1013 			} else {
1014 				src++;
1015 			}
1016 		}
1017 
1018 		/* Clear IRQ status */
1019 		status &= ~AES_REG_IRQ_DATA_IN;
1020 		omap_aes_write(dd, AES_REG_IRQ_STATUS(dd), status);
1021 
1022 		/* Enable DATA_OUT interrupt */
1023 		omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x4);
1024 
1025 	} else if (status & AES_REG_IRQ_DATA_OUT) {
1026 		omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x0);
1027 
1028 		BUG_ON(!dd->out_sg);
1029 
1030 		BUG_ON(_calc_walked(out) > dd->out_sg->length);
1031 
1032 		dst = sg_virt(dd->out_sg) + _calc_walked(out);
1033 
1034 		for (i = 0; i < AES_BLOCK_WORDS; i++) {
1035 			*dst = omap_aes_read(dd, AES_REG_DATA_N(dd, i));
1036 			scatterwalk_advance(&dd->out_walk, 4);
1037 			if (dd->out_sg->length == _calc_walked(out)) {
1038 				dd->out_sg = sg_next(dd->out_sg);
1039 				if (dd->out_sg) {
1040 					scatterwalk_start(&dd->out_walk,
1041 							  dd->out_sg);
1042 					dst = sg_virt(dd->out_sg) +
1043 					      _calc_walked(out);
1044 				}
1045 			} else {
1046 				dst++;
1047 			}
1048 		}
1049 
1050 		dd->total -= min_t(size_t, AES_BLOCK_SIZE, dd->total);
1051 
1052 		/* Clear IRQ status */
1053 		status &= ~AES_REG_IRQ_DATA_OUT;
1054 		omap_aes_write(dd, AES_REG_IRQ_STATUS(dd), status);
1055 
1056 		if (!dd->total)
1057 			/* All bytes read! */
1058 			tasklet_schedule(&dd->done_task);
1059 		else
1060 			/* Enable DATA_IN interrupt for next block */
1061 			omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x2);
1062 	}
1063 
1064 	return IRQ_HANDLED;
1065 }
1066 
1067 static const struct of_device_id omap_aes_of_match[] = {
1068 	{
1069 		.compatible	= "ti,omap2-aes",
1070 		.data		= &omap_aes_pdata_omap2,
1071 	},
1072 	{
1073 		.compatible	= "ti,omap3-aes",
1074 		.data		= &omap_aes_pdata_omap3,
1075 	},
1076 	{
1077 		.compatible	= "ti,omap4-aes",
1078 		.data		= &omap_aes_pdata_omap4,
1079 	},
1080 	{},
1081 };
1082 MODULE_DEVICE_TABLE(of, omap_aes_of_match);
1083 
1084 static int omap_aes_get_res_of(struct omap_aes_dev *dd,
1085 		struct device *dev, struct resource *res)
1086 {
1087 	struct device_node *node = dev->of_node;
1088 	const struct of_device_id *match;
1089 	int err = 0;
1090 
1091 	match = of_match_device(of_match_ptr(omap_aes_of_match), dev);
1092 	if (!match) {
1093 		dev_err(dev, "no compatible OF match\n");
1094 		err = -EINVAL;
1095 		goto err;
1096 	}
1097 
1098 	err = of_address_to_resource(node, 0, res);
1099 	if (err < 0) {
1100 		dev_err(dev, "can't translate OF node address\n");
1101 		err = -EINVAL;
1102 		goto err;
1103 	}
1104 
1105 	dd->dma_out = -1; /* Dummy value that's unused */
1106 	dd->dma_in = -1; /* Dummy value that's unused */
1107 
1108 	dd->pdata = match->data;
1109 
1110 err:
1111 	return err;
1112 }
1113 #else
1114 static const struct of_device_id omap_aes_of_match[] = {
1115 	{},
1116 };
1117 
1118 static int omap_aes_get_res_of(struct omap_aes_dev *dd,
1119 		struct device *dev, struct resource *res)
1120 {
1121 	return -EINVAL;
1122 }
1123 #endif
1124 
1125 static int omap_aes_get_res_pdev(struct omap_aes_dev *dd,
1126 		struct platform_device *pdev, struct resource *res)
1127 {
1128 	struct device *dev = &pdev->dev;
1129 	struct resource *r;
1130 	int err = 0;
1131 
1132 	/* Get the base address */
1133 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1134 	if (!r) {
1135 		dev_err(dev, "no MEM resource info\n");
1136 		err = -ENODEV;
1137 		goto err;
1138 	}
1139 	memcpy(res, r, sizeof(*res));
1140 
1141 	/* Get the DMA out channel */
1142 	r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1143 	if (!r) {
1144 		dev_err(dev, "no DMA out resource info\n");
1145 		err = -ENODEV;
1146 		goto err;
1147 	}
1148 	dd->dma_out = r->start;
1149 
1150 	/* Get the DMA in channel */
1151 	r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1152 	if (!r) {
1153 		dev_err(dev, "no DMA in resource info\n");
1154 		err = -ENODEV;
1155 		goto err;
1156 	}
1157 	dd->dma_in = r->start;
1158 
1159 	/* Only OMAP2/3 can be non-DT */
1160 	dd->pdata = &omap_aes_pdata_omap2;
1161 
1162 err:
1163 	return err;
1164 }
1165 
1166 static int omap_aes_probe(struct platform_device *pdev)
1167 {
1168 	struct device *dev = &pdev->dev;
1169 	struct omap_aes_dev *dd;
1170 	struct crypto_alg *algp;
1171 	struct resource res;
1172 	int err = -ENOMEM, i, j, irq = -1;
1173 	u32 reg;
1174 
1175 	dd = devm_kzalloc(dev, sizeof(struct omap_aes_dev), GFP_KERNEL);
1176 	if (dd == NULL) {
1177 		dev_err(dev, "unable to alloc data struct.\n");
1178 		goto err_data;
1179 	}
1180 	dd->dev = dev;
1181 	platform_set_drvdata(pdev, dd);
1182 
1183 	spin_lock_init(&dd->lock);
1184 	crypto_init_queue(&dd->queue, OMAP_AES_QUEUE_LENGTH);
1185 
1186 	err = (dev->of_node) ? omap_aes_get_res_of(dd, dev, &res) :
1187 			       omap_aes_get_res_pdev(dd, pdev, &res);
1188 	if (err)
1189 		goto err_res;
1190 
1191 	dd->io_base = devm_ioremap_resource(dev, &res);
1192 	if (IS_ERR(dd->io_base)) {
1193 		err = PTR_ERR(dd->io_base);
1194 		goto err_res;
1195 	}
1196 	dd->phys_base = res.start;
1197 
1198 	pm_runtime_enable(dev);
1199 	err = pm_runtime_get_sync(dev);
1200 	if (err < 0) {
1201 		dev_err(dev, "%s: failed to get_sync(%d)\n",
1202 			__func__, err);
1203 		goto err_res;
1204 	}
1205 
1206 	omap_aes_dma_stop(dd);
1207 
1208 	reg = omap_aes_read(dd, AES_REG_REV(dd));
1209 
1210 	pm_runtime_put_sync(dev);
1211 
1212 	dev_info(dev, "OMAP AES hw accel rev: %u.%u\n",
1213 		 (reg & dd->pdata->major_mask) >> dd->pdata->major_shift,
1214 		 (reg & dd->pdata->minor_mask) >> dd->pdata->minor_shift);
1215 
1216 	tasklet_init(&dd->done_task, omap_aes_done_task, (unsigned long)dd);
1217 	tasklet_init(&dd->queue_task, omap_aes_queue_task, (unsigned long)dd);
1218 
1219 	err = omap_aes_dma_init(dd);
1220 	if (err && AES_REG_IRQ_STATUS(dd) && AES_REG_IRQ_ENABLE(dd)) {
1221 		dd->pio_only = 1;
1222 
1223 		irq = platform_get_irq(pdev, 0);
1224 		if (irq < 0) {
1225 			dev_err(dev, "can't get IRQ resource\n");
1226 			goto err_irq;
1227 		}
1228 
1229 		err = devm_request_irq(dev, irq, omap_aes_irq, 0,
1230 				dev_name(dev), dd);
1231 		if (err) {
1232 			dev_err(dev, "Unable to grab omap-aes IRQ\n");
1233 			goto err_irq;
1234 		}
1235 	}
1236 
1237 
1238 	INIT_LIST_HEAD(&dd->list);
1239 	spin_lock(&list_lock);
1240 	list_add_tail(&dd->list, &dev_list);
1241 	spin_unlock(&list_lock);
1242 
1243 	for (i = 0; i < dd->pdata->algs_info_size; i++) {
1244 		for (j = 0; j < dd->pdata->algs_info[i].size; j++) {
1245 			algp = &dd->pdata->algs_info[i].algs_list[j];
1246 
1247 			pr_debug("reg alg: %s\n", algp->cra_name);
1248 			INIT_LIST_HEAD(&algp->cra_list);
1249 
1250 			err = crypto_register_alg(algp);
1251 			if (err)
1252 				goto err_algs;
1253 
1254 			dd->pdata->algs_info[i].registered++;
1255 		}
1256 	}
1257 
1258 	return 0;
1259 err_algs:
1260 	for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1261 		for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
1262 			crypto_unregister_alg(
1263 					&dd->pdata->algs_info[i].algs_list[j]);
1264 	if (!dd->pio_only)
1265 		omap_aes_dma_cleanup(dd);
1266 err_irq:
1267 	tasklet_kill(&dd->done_task);
1268 	tasklet_kill(&dd->queue_task);
1269 	pm_runtime_disable(dev);
1270 err_res:
1271 	dd = NULL;
1272 err_data:
1273 	dev_err(dev, "initialization failed.\n");
1274 	return err;
1275 }
1276 
1277 static int omap_aes_remove(struct platform_device *pdev)
1278 {
1279 	struct omap_aes_dev *dd = platform_get_drvdata(pdev);
1280 	int i, j;
1281 
1282 	if (!dd)
1283 		return -ENODEV;
1284 
1285 	spin_lock(&list_lock);
1286 	list_del(&dd->list);
1287 	spin_unlock(&list_lock);
1288 
1289 	for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1290 		for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
1291 			crypto_unregister_alg(
1292 					&dd->pdata->algs_info[i].algs_list[j]);
1293 
1294 	tasklet_kill(&dd->done_task);
1295 	tasklet_kill(&dd->queue_task);
1296 	omap_aes_dma_cleanup(dd);
1297 	pm_runtime_disable(dd->dev);
1298 	dd = NULL;
1299 
1300 	return 0;
1301 }
1302 
1303 #ifdef CONFIG_PM_SLEEP
1304 static int omap_aes_suspend(struct device *dev)
1305 {
1306 	pm_runtime_put_sync(dev);
1307 	return 0;
1308 }
1309 
1310 static int omap_aes_resume(struct device *dev)
1311 {
1312 	pm_runtime_get_sync(dev);
1313 	return 0;
1314 }
1315 #endif
1316 
1317 static SIMPLE_DEV_PM_OPS(omap_aes_pm_ops, omap_aes_suspend, omap_aes_resume);
1318 
1319 static struct platform_driver omap_aes_driver = {
1320 	.probe	= omap_aes_probe,
1321 	.remove	= omap_aes_remove,
1322 	.driver	= {
1323 		.name	= "omap-aes",
1324 		.pm	= &omap_aes_pm_ops,
1325 		.of_match_table	= omap_aes_of_match,
1326 	},
1327 };
1328 
1329 module_platform_driver(omap_aes_driver);
1330 
1331 MODULE_DESCRIPTION("OMAP AES hw acceleration support.");
1332 MODULE_LICENSE("GPL v2");
1333 MODULE_AUTHOR("Dmitry Kasatkin");
1334 
1335