xref: /openbmc/linux/drivers/crypto/omap-aes.c (revision 310b0d55)
1537559a5SDmitry Kasatkin /*
2537559a5SDmitry Kasatkin  * Cryptographic API.
3537559a5SDmitry Kasatkin  *
4537559a5SDmitry Kasatkin  * Support for OMAP AES HW acceleration.
5537559a5SDmitry Kasatkin  *
6537559a5SDmitry Kasatkin  * Copyright (c) 2010 Nokia Corporation
7537559a5SDmitry Kasatkin  * Author: Dmitry Kasatkin <dmitry.kasatkin@nokia.com>
80d35583aSMark A. Greer  * Copyright (c) 2011 Texas Instruments Incorporated
9537559a5SDmitry Kasatkin  *
10537559a5SDmitry Kasatkin  * This program is free software; you can redistribute it and/or modify
11537559a5SDmitry Kasatkin  * it under the terms of the GNU General Public License version 2 as published
12537559a5SDmitry Kasatkin  * by the Free Software Foundation.
13537559a5SDmitry Kasatkin  *
14537559a5SDmitry Kasatkin  */
15537559a5SDmitry Kasatkin 
16016af9b5SJoel Fernandes #define pr_fmt(fmt) "%20s: " fmt, __func__
17016af9b5SJoel Fernandes #define prn(num) pr_debug(#num "=%d\n", num)
18016af9b5SJoel Fernandes #define prx(num) pr_debug(#num "=%x\n", num)
19537559a5SDmitry Kasatkin 
20537559a5SDmitry Kasatkin #include <linux/err.h>
21537559a5SDmitry Kasatkin #include <linux/module.h>
22537559a5SDmitry Kasatkin #include <linux/init.h>
23537559a5SDmitry Kasatkin #include <linux/errno.h>
24537559a5SDmitry Kasatkin #include <linux/kernel.h>
25537559a5SDmitry Kasatkin #include <linux/platform_device.h>
26537559a5SDmitry Kasatkin #include <linux/scatterlist.h>
27537559a5SDmitry Kasatkin #include <linux/dma-mapping.h>
28ebedbf79SMark A. Greer #include <linux/dmaengine.h>
29ebedbf79SMark A. Greer #include <linux/omap-dma.h>
305946c4a5SMark A. Greer #include <linux/pm_runtime.h>
31bc69d124SMark A. Greer #include <linux/of.h>
32bc69d124SMark A. Greer #include <linux/of_device.h>
33bc69d124SMark A. Greer #include <linux/of_address.h>
34537559a5SDmitry Kasatkin #include <linux/io.h>
35537559a5SDmitry Kasatkin #include <linux/crypto.h>
36537559a5SDmitry Kasatkin #include <linux/interrupt.h>
37537559a5SDmitry Kasatkin #include <crypto/scatterwalk.h>
38537559a5SDmitry Kasatkin #include <crypto/aes.h>
39537559a5SDmitry Kasatkin 
40ebedbf79SMark A. Greer #define DST_MAXBURST			4
41ebedbf79SMark A. Greer #define DMA_MIN				(DST_MAXBURST * sizeof(u32))
42537559a5SDmitry Kasatkin 
431bf95ccaSJoel Fernandes #define _calc_walked(inout) (dd->inout##_walk.offset - dd->inout##_sg->offset)
441bf95ccaSJoel Fernandes 
45537559a5SDmitry Kasatkin /* OMAP TRM gives bitfields as start:end, where start is the higher bit
46537559a5SDmitry Kasatkin    number. For example 7:0 */
47537559a5SDmitry Kasatkin #define FLD_MASK(start, end)	(((1 << ((start) - (end) + 1)) - 1) << (end))
48537559a5SDmitry Kasatkin #define FLD_VAL(val, start, end) (((val) << (end)) & FLD_MASK(start, end))
49537559a5SDmitry Kasatkin 
500d35583aSMark A. Greer #define AES_REG_KEY(dd, x)		((dd)->pdata->key_ofs - \
510d35583aSMark A. Greer 						((x ^ 0x01) * 0x04))
520d35583aSMark A. Greer #define AES_REG_IV(dd, x)		((dd)->pdata->iv_ofs + ((x) * 0x04))
53537559a5SDmitry Kasatkin 
540d35583aSMark A. Greer #define AES_REG_CTRL(dd)		((dd)->pdata->ctrl_ofs)
55f9fb69e7SMark A. Greer #define AES_REG_CTRL_CTR_WIDTH_MASK	(3 << 7)
56f9fb69e7SMark A. Greer #define AES_REG_CTRL_CTR_WIDTH_32		(0 << 7)
57f9fb69e7SMark A. Greer #define AES_REG_CTRL_CTR_WIDTH_64		(1 << 7)
58f9fb69e7SMark A. Greer #define AES_REG_CTRL_CTR_WIDTH_96		(2 << 7)
59f9fb69e7SMark A. Greer #define AES_REG_CTRL_CTR_WIDTH_128		(3 << 7)
60537559a5SDmitry Kasatkin #define AES_REG_CTRL_CTR		(1 << 6)
61537559a5SDmitry Kasatkin #define AES_REG_CTRL_CBC		(1 << 5)
62537559a5SDmitry Kasatkin #define AES_REG_CTRL_KEY_SIZE		(3 << 3)
63537559a5SDmitry Kasatkin #define AES_REG_CTRL_DIRECTION		(1 << 2)
64537559a5SDmitry Kasatkin #define AES_REG_CTRL_INPUT_READY	(1 << 1)
65537559a5SDmitry Kasatkin #define AES_REG_CTRL_OUTPUT_READY	(1 << 0)
66537559a5SDmitry Kasatkin 
670d35583aSMark A. Greer #define AES_REG_DATA_N(dd, x)		((dd)->pdata->data_ofs + ((x) * 0x04))
68537559a5SDmitry Kasatkin 
690d35583aSMark A. Greer #define AES_REG_REV(dd)			((dd)->pdata->rev_ofs)
70537559a5SDmitry Kasatkin 
710d35583aSMark A. Greer #define AES_REG_MASK(dd)		((dd)->pdata->mask_ofs)
72537559a5SDmitry Kasatkin #define AES_REG_MASK_SIDLE		(1 << 6)
73537559a5SDmitry Kasatkin #define AES_REG_MASK_START		(1 << 5)
74537559a5SDmitry Kasatkin #define AES_REG_MASK_DMA_OUT_EN		(1 << 3)
75537559a5SDmitry Kasatkin #define AES_REG_MASK_DMA_IN_EN		(1 << 2)
76537559a5SDmitry Kasatkin #define AES_REG_MASK_SOFTRESET		(1 << 1)
77537559a5SDmitry Kasatkin #define AES_REG_AUTOIDLE		(1 << 0)
78537559a5SDmitry Kasatkin 
790d35583aSMark A. Greer #define AES_REG_LENGTH_N(x)		(0x54 + ((x) * 0x04))
80537559a5SDmitry Kasatkin 
8167216756SJoel Fernandes #define AES_REG_IRQ_STATUS(dd)         ((dd)->pdata->irq_status_ofs)
8267216756SJoel Fernandes #define AES_REG_IRQ_ENABLE(dd)         ((dd)->pdata->irq_enable_ofs)
8367216756SJoel Fernandes #define AES_REG_IRQ_DATA_IN            BIT(1)
8467216756SJoel Fernandes #define AES_REG_IRQ_DATA_OUT           BIT(2)
85537559a5SDmitry Kasatkin #define DEFAULT_TIMEOUT		(5*HZ)
86537559a5SDmitry Kasatkin 
87537559a5SDmitry Kasatkin #define FLAGS_MODE_MASK		0x000f
88537559a5SDmitry Kasatkin #define FLAGS_ENCRYPT		BIT(0)
89537559a5SDmitry Kasatkin #define FLAGS_CBC		BIT(1)
90537559a5SDmitry Kasatkin #define FLAGS_GIV		BIT(2)
91f9fb69e7SMark A. Greer #define FLAGS_CTR		BIT(3)
92537559a5SDmitry Kasatkin 
9367a730ceSDmitry Kasatkin #define FLAGS_INIT		BIT(4)
9467a730ceSDmitry Kasatkin #define FLAGS_FAST		BIT(5)
9567a730ceSDmitry Kasatkin #define FLAGS_BUSY		BIT(6)
96537559a5SDmitry Kasatkin 
971bf95ccaSJoel Fernandes #define AES_BLOCK_WORDS		(AES_BLOCK_SIZE >> 2)
981bf95ccaSJoel Fernandes 
99537559a5SDmitry Kasatkin struct omap_aes_ctx {
100537559a5SDmitry Kasatkin 	struct omap_aes_dev *dd;
101537559a5SDmitry Kasatkin 
102537559a5SDmitry Kasatkin 	int		keylen;
103537559a5SDmitry Kasatkin 	u32		key[AES_KEYSIZE_256 / sizeof(u32)];
104537559a5SDmitry Kasatkin 	unsigned long	flags;
105537559a5SDmitry Kasatkin };
106537559a5SDmitry Kasatkin 
107537559a5SDmitry Kasatkin struct omap_aes_reqctx {
108537559a5SDmitry Kasatkin 	unsigned long mode;
109537559a5SDmitry Kasatkin };
110537559a5SDmitry Kasatkin 
111537559a5SDmitry Kasatkin #define OMAP_AES_QUEUE_LENGTH	1
112537559a5SDmitry Kasatkin #define OMAP_AES_CACHE_SIZE	0
113537559a5SDmitry Kasatkin 
114f9fb69e7SMark A. Greer struct omap_aes_algs_info {
115f9fb69e7SMark A. Greer 	struct crypto_alg	*algs_list;
116f9fb69e7SMark A. Greer 	unsigned int		size;
117f9fb69e7SMark A. Greer 	unsigned int		registered;
118f9fb69e7SMark A. Greer };
119f9fb69e7SMark A. Greer 
1200d35583aSMark A. Greer struct omap_aes_pdata {
121f9fb69e7SMark A. Greer 	struct omap_aes_algs_info	*algs_info;
122f9fb69e7SMark A. Greer 	unsigned int	algs_info_size;
123f9fb69e7SMark A. Greer 
1240d35583aSMark A. Greer 	void		(*trigger)(struct omap_aes_dev *dd, int length);
1250d35583aSMark A. Greer 
1260d35583aSMark A. Greer 	u32		key_ofs;
1270d35583aSMark A. Greer 	u32		iv_ofs;
1280d35583aSMark A. Greer 	u32		ctrl_ofs;
1290d35583aSMark A. Greer 	u32		data_ofs;
1300d35583aSMark A. Greer 	u32		rev_ofs;
1310d35583aSMark A. Greer 	u32		mask_ofs;
13267216756SJoel Fernandes 	u32             irq_enable_ofs;
13367216756SJoel Fernandes 	u32             irq_status_ofs;
1340d35583aSMark A. Greer 
1350d35583aSMark A. Greer 	u32		dma_enable_in;
1360d35583aSMark A. Greer 	u32		dma_enable_out;
1370d35583aSMark A. Greer 	u32		dma_start;
1380d35583aSMark A. Greer 
1390d35583aSMark A. Greer 	u32		major_mask;
1400d35583aSMark A. Greer 	u32		major_shift;
1410d35583aSMark A. Greer 	u32		minor_mask;
1420d35583aSMark A. Greer 	u32		minor_shift;
1430d35583aSMark A. Greer };
1440d35583aSMark A. Greer 
145537559a5SDmitry Kasatkin struct omap_aes_dev {
146537559a5SDmitry Kasatkin 	struct list_head	list;
147537559a5SDmitry Kasatkin 	unsigned long		phys_base;
148537559a5SDmitry Kasatkin 	void __iomem		*io_base;
149537559a5SDmitry Kasatkin 	struct omap_aes_ctx	*ctx;
150537559a5SDmitry Kasatkin 	struct device		*dev;
151537559a5SDmitry Kasatkin 	unsigned long		flags;
15221fe9767SDmitry Kasatkin 	int			err;
153537559a5SDmitry Kasatkin 
154537559a5SDmitry Kasatkin 	spinlock_t		lock;
155537559a5SDmitry Kasatkin 	struct crypto_queue	queue;
156537559a5SDmitry Kasatkin 
15721fe9767SDmitry Kasatkin 	struct tasklet_struct	done_task;
15821fe9767SDmitry Kasatkin 	struct tasklet_struct	queue_task;
159537559a5SDmitry Kasatkin 
160537559a5SDmitry Kasatkin 	struct ablkcipher_request	*req;
1616242332fSJoel Fernandes 
1626242332fSJoel Fernandes 	/*
1636242332fSJoel Fernandes 	 * total is used by PIO mode for book keeping so introduce
1646242332fSJoel Fernandes 	 * variable total_save as need it to calc page_order
1656242332fSJoel Fernandes 	 */
166537559a5SDmitry Kasatkin 	size_t				total;
1676242332fSJoel Fernandes 	size_t				total_save;
1686242332fSJoel Fernandes 
169537559a5SDmitry Kasatkin 	struct scatterlist		*in_sg;
170537559a5SDmitry Kasatkin 	struct scatterlist		*out_sg;
1716242332fSJoel Fernandes 
1726242332fSJoel Fernandes 	/* Buffers for copying for unaligned cases */
1736242332fSJoel Fernandes 	struct scatterlist		in_sgl;
1746242332fSJoel Fernandes 	struct scatterlist		out_sgl;
1756242332fSJoel Fernandes 	struct scatterlist		*orig_out;
1766242332fSJoel Fernandes 	int				sgs_copied;
1776242332fSJoel Fernandes 
1781bf95ccaSJoel Fernandes 	struct scatter_walk		in_walk;
1791bf95ccaSJoel Fernandes 	struct scatter_walk		out_walk;
180537559a5SDmitry Kasatkin 	int			dma_in;
181ebedbf79SMark A. Greer 	struct dma_chan		*dma_lch_in;
182537559a5SDmitry Kasatkin 	int			dma_out;
183ebedbf79SMark A. Greer 	struct dma_chan		*dma_lch_out;
184e77c756eSJoel Fernandes 	int			in_sg_len;
185e77c756eSJoel Fernandes 	int			out_sg_len;
18698837abcSJoel Fernandes 	int			pio_only;
1870d35583aSMark A. Greer 	const struct omap_aes_pdata	*pdata;
188537559a5SDmitry Kasatkin };
189537559a5SDmitry Kasatkin 
190537559a5SDmitry Kasatkin /* keep registered devices data here */
191537559a5SDmitry Kasatkin static LIST_HEAD(dev_list);
192537559a5SDmitry Kasatkin static DEFINE_SPINLOCK(list_lock);
193537559a5SDmitry Kasatkin 
194016af9b5SJoel Fernandes #ifdef DEBUG
195016af9b5SJoel Fernandes #define omap_aes_read(dd, offset)				\
196016af9b5SJoel Fernandes ({								\
197016af9b5SJoel Fernandes 	int _read_ret;						\
198016af9b5SJoel Fernandes 	_read_ret = __raw_readl(dd->io_base + offset);		\
199016af9b5SJoel Fernandes 	pr_debug("omap_aes_read(" #offset "=%#x)= %#x\n",	\
200016af9b5SJoel Fernandes 		 offset, _read_ret);				\
201016af9b5SJoel Fernandes 	_read_ret;						\
202016af9b5SJoel Fernandes })
203016af9b5SJoel Fernandes #else
204537559a5SDmitry Kasatkin static inline u32 omap_aes_read(struct omap_aes_dev *dd, u32 offset)
205537559a5SDmitry Kasatkin {
206537559a5SDmitry Kasatkin 	return __raw_readl(dd->io_base + offset);
207537559a5SDmitry Kasatkin }
208016af9b5SJoel Fernandes #endif
209537559a5SDmitry Kasatkin 
210016af9b5SJoel Fernandes #ifdef DEBUG
211016af9b5SJoel Fernandes #define omap_aes_write(dd, offset, value)				\
212016af9b5SJoel Fernandes 	do {								\
213016af9b5SJoel Fernandes 		pr_debug("omap_aes_write(" #offset "=%#x) value=%#x\n",	\
214016af9b5SJoel Fernandes 			 offset, value);				\
215016af9b5SJoel Fernandes 		__raw_writel(value, dd->io_base + offset);		\
216016af9b5SJoel Fernandes 	} while (0)
217016af9b5SJoel Fernandes #else
218537559a5SDmitry Kasatkin static inline void omap_aes_write(struct omap_aes_dev *dd, u32 offset,
219537559a5SDmitry Kasatkin 				  u32 value)
220537559a5SDmitry Kasatkin {
221537559a5SDmitry Kasatkin 	__raw_writel(value, dd->io_base + offset);
222537559a5SDmitry Kasatkin }
223016af9b5SJoel Fernandes #endif
224537559a5SDmitry Kasatkin 
225537559a5SDmitry Kasatkin static inline void omap_aes_write_mask(struct omap_aes_dev *dd, u32 offset,
226537559a5SDmitry Kasatkin 					u32 value, u32 mask)
227537559a5SDmitry Kasatkin {
228537559a5SDmitry Kasatkin 	u32 val;
229537559a5SDmitry Kasatkin 
230537559a5SDmitry Kasatkin 	val = omap_aes_read(dd, offset);
231537559a5SDmitry Kasatkin 	val &= ~mask;
232537559a5SDmitry Kasatkin 	val |= value;
233537559a5SDmitry Kasatkin 	omap_aes_write(dd, offset, val);
234537559a5SDmitry Kasatkin }
235537559a5SDmitry Kasatkin 
236537559a5SDmitry Kasatkin static void omap_aes_write_n(struct omap_aes_dev *dd, u32 offset,
237537559a5SDmitry Kasatkin 					u32 *value, int count)
238537559a5SDmitry Kasatkin {
239537559a5SDmitry Kasatkin 	for (; count--; value++, offset += 4)
240537559a5SDmitry Kasatkin 		omap_aes_write(dd, offset, *value);
241537559a5SDmitry Kasatkin }
242537559a5SDmitry Kasatkin 
243537559a5SDmitry Kasatkin static int omap_aes_hw_init(struct omap_aes_dev *dd)
244537559a5SDmitry Kasatkin {
245537559a5SDmitry Kasatkin 	if (!(dd->flags & FLAGS_INIT)) {
246537559a5SDmitry Kasatkin 		dd->flags |= FLAGS_INIT;
24721fe9767SDmitry Kasatkin 		dd->err = 0;
248537559a5SDmitry Kasatkin 	}
249537559a5SDmitry Kasatkin 
250eeb2b202SDmitry Kasatkin 	return 0;
251537559a5SDmitry Kasatkin }
252537559a5SDmitry Kasatkin 
25321fe9767SDmitry Kasatkin static int omap_aes_write_ctrl(struct omap_aes_dev *dd)
254537559a5SDmitry Kasatkin {
255537559a5SDmitry Kasatkin 	unsigned int key32;
25667a730ceSDmitry Kasatkin 	int i, err;
257f9fb69e7SMark A. Greer 	u32 val, mask = 0;
258537559a5SDmitry Kasatkin 
25921fe9767SDmitry Kasatkin 	err = omap_aes_hw_init(dd);
26021fe9767SDmitry Kasatkin 	if (err)
26121fe9767SDmitry Kasatkin 		return err;
26221fe9767SDmitry Kasatkin 
263537559a5SDmitry Kasatkin 	key32 = dd->ctx->keylen / sizeof(u32);
26467a730ceSDmitry Kasatkin 
26567a730ceSDmitry Kasatkin 	/* it seems a key should always be set even if it has not changed */
266537559a5SDmitry Kasatkin 	for (i = 0; i < key32; i++) {
2670d35583aSMark A. Greer 		omap_aes_write(dd, AES_REG_KEY(dd, i),
268537559a5SDmitry Kasatkin 			__le32_to_cpu(dd->ctx->key[i]));
269537559a5SDmitry Kasatkin 	}
270537559a5SDmitry Kasatkin 
271f9fb69e7SMark A. Greer 	if ((dd->flags & (FLAGS_CBC | FLAGS_CTR)) && dd->req->info)
2720d35583aSMark A. Greer 		omap_aes_write_n(dd, AES_REG_IV(dd, 0), dd->req->info, 4);
27367a730ceSDmitry Kasatkin 
27467a730ceSDmitry Kasatkin 	val = FLD_VAL(((dd->ctx->keylen >> 3) - 1), 4, 3);
27567a730ceSDmitry Kasatkin 	if (dd->flags & FLAGS_CBC)
27667a730ceSDmitry Kasatkin 		val |= AES_REG_CTRL_CBC;
277f9fb69e7SMark A. Greer 	if (dd->flags & FLAGS_CTR) {
2788ed49c76SJoel Fernandes 		val |= AES_REG_CTRL_CTR | AES_REG_CTRL_CTR_WIDTH_128;
279f9fb69e7SMark A. Greer 		mask = AES_REG_CTRL_CTR | AES_REG_CTRL_CTR_WIDTH_MASK;
280f9fb69e7SMark A. Greer 	}
28167a730ceSDmitry Kasatkin 	if (dd->flags & FLAGS_ENCRYPT)
28267a730ceSDmitry Kasatkin 		val |= AES_REG_CTRL_DIRECTION;
283537559a5SDmitry Kasatkin 
284f9fb69e7SMark A. Greer 	mask |= AES_REG_CTRL_CBC | AES_REG_CTRL_DIRECTION |
285537559a5SDmitry Kasatkin 			AES_REG_CTRL_KEY_SIZE;
286537559a5SDmitry Kasatkin 
2870d35583aSMark A. Greer 	omap_aes_write_mask(dd, AES_REG_CTRL(dd), val, mask);
288537559a5SDmitry Kasatkin 
28921fe9767SDmitry Kasatkin 	return 0;
290537559a5SDmitry Kasatkin }
291537559a5SDmitry Kasatkin 
2920d35583aSMark A. Greer static void omap_aes_dma_trigger_omap2(struct omap_aes_dev *dd, int length)
2930d35583aSMark A. Greer {
2940d35583aSMark A. Greer 	u32 mask, val;
2950d35583aSMark A. Greer 
2960d35583aSMark A. Greer 	val = dd->pdata->dma_start;
2970d35583aSMark A. Greer 
2980d35583aSMark A. Greer 	if (dd->dma_lch_out != NULL)
2990d35583aSMark A. Greer 		val |= dd->pdata->dma_enable_out;
3000d35583aSMark A. Greer 	if (dd->dma_lch_in != NULL)
3010d35583aSMark A. Greer 		val |= dd->pdata->dma_enable_in;
3020d35583aSMark A. Greer 
3030d35583aSMark A. Greer 	mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
3040d35583aSMark A. Greer 	       dd->pdata->dma_start;
3050d35583aSMark A. Greer 
3060d35583aSMark A. Greer 	omap_aes_write_mask(dd, AES_REG_MASK(dd), val, mask);
3070d35583aSMark A. Greer 
3080d35583aSMark A. Greer }
3090d35583aSMark A. Greer 
3100d35583aSMark A. Greer static void omap_aes_dma_trigger_omap4(struct omap_aes_dev *dd, int length)
3110d35583aSMark A. Greer {
3120d35583aSMark A. Greer 	omap_aes_write(dd, AES_REG_LENGTH_N(0), length);
3130d35583aSMark A. Greer 	omap_aes_write(dd, AES_REG_LENGTH_N(1), 0);
3140d35583aSMark A. Greer 
3150d35583aSMark A. Greer 	omap_aes_dma_trigger_omap2(dd, length);
3160d35583aSMark A. Greer }
3170d35583aSMark A. Greer 
3180d35583aSMark A. Greer static void omap_aes_dma_stop(struct omap_aes_dev *dd)
3190d35583aSMark A. Greer {
3200d35583aSMark A. Greer 	u32 mask;
3210d35583aSMark A. Greer 
3220d35583aSMark A. Greer 	mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
3230d35583aSMark A. Greer 	       dd->pdata->dma_start;
3240d35583aSMark A. Greer 
3250d35583aSMark A. Greer 	omap_aes_write_mask(dd, AES_REG_MASK(dd), 0, mask);
3260d35583aSMark A. Greer }
3270d35583aSMark A. Greer 
328537559a5SDmitry Kasatkin static struct omap_aes_dev *omap_aes_find_dev(struct omap_aes_ctx *ctx)
329537559a5SDmitry Kasatkin {
330537559a5SDmitry Kasatkin 	struct omap_aes_dev *dd = NULL, *tmp;
331537559a5SDmitry Kasatkin 
332537559a5SDmitry Kasatkin 	spin_lock_bh(&list_lock);
333537559a5SDmitry Kasatkin 	if (!ctx->dd) {
334537559a5SDmitry Kasatkin 		list_for_each_entry(tmp, &dev_list, list) {
335537559a5SDmitry Kasatkin 			/* FIXME: take fist available aes core */
336537559a5SDmitry Kasatkin 			dd = tmp;
337537559a5SDmitry Kasatkin 			break;
338537559a5SDmitry Kasatkin 		}
339537559a5SDmitry Kasatkin 		ctx->dd = dd;
340537559a5SDmitry Kasatkin 	} else {
341537559a5SDmitry Kasatkin 		/* already found before */
342537559a5SDmitry Kasatkin 		dd = ctx->dd;
343537559a5SDmitry Kasatkin 	}
344537559a5SDmitry Kasatkin 	spin_unlock_bh(&list_lock);
345537559a5SDmitry Kasatkin 
346537559a5SDmitry Kasatkin 	return dd;
347537559a5SDmitry Kasatkin }
348537559a5SDmitry Kasatkin 
349ebedbf79SMark A. Greer static void omap_aes_dma_out_callback(void *data)
350ebedbf79SMark A. Greer {
351ebedbf79SMark A. Greer 	struct omap_aes_dev *dd = data;
352ebedbf79SMark A. Greer 
353ebedbf79SMark A. Greer 	/* dma_lch_out - completed */
354ebedbf79SMark A. Greer 	tasklet_schedule(&dd->done_task);
355ebedbf79SMark A. Greer }
356537559a5SDmitry Kasatkin 
357537559a5SDmitry Kasatkin static int omap_aes_dma_init(struct omap_aes_dev *dd)
358537559a5SDmitry Kasatkin {
359537559a5SDmitry Kasatkin 	int err = -ENOMEM;
360ebedbf79SMark A. Greer 	dma_cap_mask_t mask;
361537559a5SDmitry Kasatkin 
362ebedbf79SMark A. Greer 	dd->dma_lch_out = NULL;
363ebedbf79SMark A. Greer 	dd->dma_lch_in = NULL;
364537559a5SDmitry Kasatkin 
365ebedbf79SMark A. Greer 	dma_cap_zero(mask);
366ebedbf79SMark A. Greer 	dma_cap_set(DMA_SLAVE, mask);
367ebedbf79SMark A. Greer 
368b4b87a93SMark A. Greer 	dd->dma_lch_in = dma_request_slave_channel_compat(mask,
369b4b87a93SMark A. Greer 							  omap_dma_filter_fn,
370b4b87a93SMark A. Greer 							  &dd->dma_in,
371b4b87a93SMark A. Greer 							  dd->dev, "rx");
372ebedbf79SMark A. Greer 	if (!dd->dma_lch_in) {
373ebedbf79SMark A. Greer 		dev_err(dd->dev, "Unable to request in DMA channel\n");
374ebedbf79SMark A. Greer 		goto err_dma_in;
375ebedbf79SMark A. Greer 	}
376ebedbf79SMark A. Greer 
377b4b87a93SMark A. Greer 	dd->dma_lch_out = dma_request_slave_channel_compat(mask,
378b4b87a93SMark A. Greer 							   omap_dma_filter_fn,
379b4b87a93SMark A. Greer 							   &dd->dma_out,
380b4b87a93SMark A. Greer 							   dd->dev, "tx");
381ebedbf79SMark A. Greer 	if (!dd->dma_lch_out) {
382ebedbf79SMark A. Greer 		dev_err(dd->dev, "Unable to request out DMA channel\n");
383ebedbf79SMark A. Greer 		goto err_dma_out;
384ebedbf79SMark A. Greer 	}
385537559a5SDmitry Kasatkin 
386537559a5SDmitry Kasatkin 	return 0;
387537559a5SDmitry Kasatkin 
388537559a5SDmitry Kasatkin err_dma_out:
389ebedbf79SMark A. Greer 	dma_release_channel(dd->dma_lch_in);
390537559a5SDmitry Kasatkin err_dma_in:
391537559a5SDmitry Kasatkin 	if (err)
392537559a5SDmitry Kasatkin 		pr_err("error: %d\n", err);
393537559a5SDmitry Kasatkin 	return err;
394537559a5SDmitry Kasatkin }
395537559a5SDmitry Kasatkin 
396537559a5SDmitry Kasatkin static void omap_aes_dma_cleanup(struct omap_aes_dev *dd)
397537559a5SDmitry Kasatkin {
398ebedbf79SMark A. Greer 	dma_release_channel(dd->dma_lch_out);
399ebedbf79SMark A. Greer 	dma_release_channel(dd->dma_lch_in);
400537559a5SDmitry Kasatkin }
401537559a5SDmitry Kasatkin 
402537559a5SDmitry Kasatkin static void sg_copy_buf(void *buf, struct scatterlist *sg,
403537559a5SDmitry Kasatkin 			      unsigned int start, unsigned int nbytes, int out)
404537559a5SDmitry Kasatkin {
405537559a5SDmitry Kasatkin 	struct scatter_walk walk;
406537559a5SDmitry Kasatkin 
407537559a5SDmitry Kasatkin 	if (!nbytes)
408537559a5SDmitry Kasatkin 		return;
409537559a5SDmitry Kasatkin 
410537559a5SDmitry Kasatkin 	scatterwalk_start(&walk, sg);
411537559a5SDmitry Kasatkin 	scatterwalk_advance(&walk, start);
412537559a5SDmitry Kasatkin 	scatterwalk_copychunks(buf, &walk, nbytes, out);
413537559a5SDmitry Kasatkin 	scatterwalk_done(&walk, out, 0);
414537559a5SDmitry Kasatkin }
415537559a5SDmitry Kasatkin 
416ebedbf79SMark A. Greer static int omap_aes_crypt_dma(struct crypto_tfm *tfm,
4174b645c94SJoel Fernandes 		struct scatterlist *in_sg, struct scatterlist *out_sg,
4184b645c94SJoel Fernandes 		int in_sg_len, int out_sg_len)
419537559a5SDmitry Kasatkin {
420537559a5SDmitry Kasatkin 	struct omap_aes_ctx *ctx = crypto_tfm_ctx(tfm);
421537559a5SDmitry Kasatkin 	struct omap_aes_dev *dd = ctx->dd;
422ebedbf79SMark A. Greer 	struct dma_async_tx_descriptor *tx_in, *tx_out;
423ebedbf79SMark A. Greer 	struct dma_slave_config cfg;
4244b645c94SJoel Fernandes 	int ret;
425537559a5SDmitry Kasatkin 
42698837abcSJoel Fernandes 	if (dd->pio_only) {
42798837abcSJoel Fernandes 		scatterwalk_start(&dd->in_walk, dd->in_sg);
42898837abcSJoel Fernandes 		scatterwalk_start(&dd->out_walk, dd->out_sg);
42998837abcSJoel Fernandes 
43098837abcSJoel Fernandes 		/* Enable DATAIN interrupt and let it take
43198837abcSJoel Fernandes 		   care of the rest */
43298837abcSJoel Fernandes 		omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x2);
43398837abcSJoel Fernandes 		return 0;
43498837abcSJoel Fernandes 	}
43598837abcSJoel Fernandes 
4360a641712SJoel Fernandes 	dma_sync_sg_for_device(dd->dev, dd->in_sg, in_sg_len, DMA_TO_DEVICE);
4370a641712SJoel Fernandes 
438ebedbf79SMark A. Greer 	memset(&cfg, 0, sizeof(cfg));
439ebedbf79SMark A. Greer 
4400d35583aSMark A. Greer 	cfg.src_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
4410d35583aSMark A. Greer 	cfg.dst_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
442ebedbf79SMark A. Greer 	cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
443ebedbf79SMark A. Greer 	cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
444ebedbf79SMark A. Greer 	cfg.src_maxburst = DST_MAXBURST;
445ebedbf79SMark A. Greer 	cfg.dst_maxburst = DST_MAXBURST;
446ebedbf79SMark A. Greer 
447ebedbf79SMark A. Greer 	/* IN */
448ebedbf79SMark A. Greer 	ret = dmaengine_slave_config(dd->dma_lch_in, &cfg);
449ebedbf79SMark A. Greer 	if (ret) {
450ebedbf79SMark A. Greer 		dev_err(dd->dev, "can't configure IN dmaengine slave: %d\n",
451ebedbf79SMark A. Greer 			ret);
452ebedbf79SMark A. Greer 		return ret;
453ebedbf79SMark A. Greer 	}
454ebedbf79SMark A. Greer 
4554b645c94SJoel Fernandes 	tx_in = dmaengine_prep_slave_sg(dd->dma_lch_in, in_sg, in_sg_len,
456ebedbf79SMark A. Greer 					DMA_MEM_TO_DEV,
457ebedbf79SMark A. Greer 					DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
458ebedbf79SMark A. Greer 	if (!tx_in) {
459ebedbf79SMark A. Greer 		dev_err(dd->dev, "IN prep_slave_sg() failed\n");
460ebedbf79SMark A. Greer 		return -EINVAL;
461ebedbf79SMark A. Greer 	}
462ebedbf79SMark A. Greer 
463ebedbf79SMark A. Greer 	/* No callback necessary */
464ebedbf79SMark A. Greer 	tx_in->callback_param = dd;
465ebedbf79SMark A. Greer 
466ebedbf79SMark A. Greer 	/* OUT */
467ebedbf79SMark A. Greer 	ret = dmaengine_slave_config(dd->dma_lch_out, &cfg);
468ebedbf79SMark A. Greer 	if (ret) {
469ebedbf79SMark A. Greer 		dev_err(dd->dev, "can't configure OUT dmaengine slave: %d\n",
470ebedbf79SMark A. Greer 			ret);
471ebedbf79SMark A. Greer 		return ret;
472ebedbf79SMark A. Greer 	}
473ebedbf79SMark A. Greer 
4744b645c94SJoel Fernandes 	tx_out = dmaengine_prep_slave_sg(dd->dma_lch_out, out_sg, out_sg_len,
475ebedbf79SMark A. Greer 					DMA_DEV_TO_MEM,
476ebedbf79SMark A. Greer 					DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
477ebedbf79SMark A. Greer 	if (!tx_out) {
478ebedbf79SMark A. Greer 		dev_err(dd->dev, "OUT prep_slave_sg() failed\n");
479ebedbf79SMark A. Greer 		return -EINVAL;
480ebedbf79SMark A. Greer 	}
481ebedbf79SMark A. Greer 
482ebedbf79SMark A. Greer 	tx_out->callback = omap_aes_dma_out_callback;
483ebedbf79SMark A. Greer 	tx_out->callback_param = dd;
484ebedbf79SMark A. Greer 
485ebedbf79SMark A. Greer 	dmaengine_submit(tx_in);
486ebedbf79SMark A. Greer 	dmaengine_submit(tx_out);
487ebedbf79SMark A. Greer 
488ebedbf79SMark A. Greer 	dma_async_issue_pending(dd->dma_lch_in);
489ebedbf79SMark A. Greer 	dma_async_issue_pending(dd->dma_lch_out);
490537559a5SDmitry Kasatkin 
4910d35583aSMark A. Greer 	/* start DMA */
4924b645c94SJoel Fernandes 	dd->pdata->trigger(dd, dd->total);
49383ea7e0fSDmitry Kasatkin 
494537559a5SDmitry Kasatkin 	return 0;
495537559a5SDmitry Kasatkin }
496537559a5SDmitry Kasatkin 
497537559a5SDmitry Kasatkin static int omap_aes_crypt_dma_start(struct omap_aes_dev *dd)
498537559a5SDmitry Kasatkin {
499537559a5SDmitry Kasatkin 	struct crypto_tfm *tfm = crypto_ablkcipher_tfm(
500537559a5SDmitry Kasatkin 					crypto_ablkcipher_reqtfm(dd->req));
5014b645c94SJoel Fernandes 	int err;
502537559a5SDmitry Kasatkin 
503537559a5SDmitry Kasatkin 	pr_debug("total: %d\n", dd->total);
504537559a5SDmitry Kasatkin 
50598837abcSJoel Fernandes 	if (!dd->pio_only) {
50698837abcSJoel Fernandes 		err = dma_map_sg(dd->dev, dd->in_sg, dd->in_sg_len,
50798837abcSJoel Fernandes 				 DMA_TO_DEVICE);
508537559a5SDmitry Kasatkin 		if (!err) {
509537559a5SDmitry Kasatkin 			dev_err(dd->dev, "dma_map_sg() error\n");
510537559a5SDmitry Kasatkin 			return -EINVAL;
511537559a5SDmitry Kasatkin 		}
512537559a5SDmitry Kasatkin 
51398837abcSJoel Fernandes 		err = dma_map_sg(dd->dev, dd->out_sg, dd->out_sg_len,
51498837abcSJoel Fernandes 				 DMA_FROM_DEVICE);
515537559a5SDmitry Kasatkin 		if (!err) {
516537559a5SDmitry Kasatkin 			dev_err(dd->dev, "dma_map_sg() error\n");
517537559a5SDmitry Kasatkin 			return -EINVAL;
518537559a5SDmitry Kasatkin 		}
51998837abcSJoel Fernandes 	}
520537559a5SDmitry Kasatkin 
5214b645c94SJoel Fernandes 	err = omap_aes_crypt_dma(tfm, dd->in_sg, dd->out_sg, dd->in_sg_len,
5224b645c94SJoel Fernandes 				 dd->out_sg_len);
52398837abcSJoel Fernandes 	if (err && !dd->pio_only) {
5244b645c94SJoel Fernandes 		dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
5254b645c94SJoel Fernandes 		dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
5264b645c94SJoel Fernandes 			     DMA_FROM_DEVICE);
52721fe9767SDmitry Kasatkin 	}
528537559a5SDmitry Kasatkin 
529537559a5SDmitry Kasatkin 	return err;
530537559a5SDmitry Kasatkin }
531537559a5SDmitry Kasatkin 
532537559a5SDmitry Kasatkin static void omap_aes_finish_req(struct omap_aes_dev *dd, int err)
533537559a5SDmitry Kasatkin {
53421fe9767SDmitry Kasatkin 	struct ablkcipher_request *req = dd->req;
535537559a5SDmitry Kasatkin 
536537559a5SDmitry Kasatkin 	pr_debug("err: %d\n", err);
537537559a5SDmitry Kasatkin 
538eeb2b202SDmitry Kasatkin 	dd->flags &= ~FLAGS_BUSY;
539eeb2b202SDmitry Kasatkin 
54021fe9767SDmitry Kasatkin 	req->base.complete(&req->base, err);
541537559a5SDmitry Kasatkin }
542537559a5SDmitry Kasatkin 
543537559a5SDmitry Kasatkin static int omap_aes_crypt_dma_stop(struct omap_aes_dev *dd)
544537559a5SDmitry Kasatkin {
545537559a5SDmitry Kasatkin 	int err = 0;
546537559a5SDmitry Kasatkin 
547537559a5SDmitry Kasatkin 	pr_debug("total: %d\n", dd->total);
548537559a5SDmitry Kasatkin 
5490d35583aSMark A. Greer 	omap_aes_dma_stop(dd);
550537559a5SDmitry Kasatkin 
551ebedbf79SMark A. Greer 	dmaengine_terminate_all(dd->dma_lch_in);
552ebedbf79SMark A. Greer 	dmaengine_terminate_all(dd->dma_lch_out);
553537559a5SDmitry Kasatkin 
554537559a5SDmitry Kasatkin 	return err;
555537559a5SDmitry Kasatkin }
556537559a5SDmitry Kasatkin 
5576d7e7e02SVutla, Lokesh static int omap_aes_check_aligned(struct scatterlist *sg, int total)
5586242332fSJoel Fernandes {
5596d7e7e02SVutla, Lokesh 	int len = 0;
5606d7e7e02SVutla, Lokesh 
561310b0d55SVutla, Lokesh 	if (!IS_ALIGNED(total, AES_BLOCK_SIZE))
562310b0d55SVutla, Lokesh 		return -EINVAL;
563310b0d55SVutla, Lokesh 
5646242332fSJoel Fernandes 	while (sg) {
5656242332fSJoel Fernandes 		if (!IS_ALIGNED(sg->offset, 4))
5666242332fSJoel Fernandes 			return -1;
5676242332fSJoel Fernandes 		if (!IS_ALIGNED(sg->length, AES_BLOCK_SIZE))
5686242332fSJoel Fernandes 			return -1;
5696d7e7e02SVutla, Lokesh 
5706d7e7e02SVutla, Lokesh 		len += sg->length;
5716242332fSJoel Fernandes 		sg = sg_next(sg);
5726242332fSJoel Fernandes 	}
5736d7e7e02SVutla, Lokesh 
5746d7e7e02SVutla, Lokesh 	if (len != total)
5756d7e7e02SVutla, Lokesh 		return -1;
5766d7e7e02SVutla, Lokesh 
5776242332fSJoel Fernandes 	return 0;
5786242332fSJoel Fernandes }
5796242332fSJoel Fernandes 
580034568e8SSachin Kamat static int omap_aes_copy_sgs(struct omap_aes_dev *dd)
5816242332fSJoel Fernandes {
5826242332fSJoel Fernandes 	void *buf_in, *buf_out;
583310b0d55SVutla, Lokesh 	int pages, total;
5846242332fSJoel Fernandes 
585310b0d55SVutla, Lokesh 	total = ALIGN(dd->total, AES_BLOCK_SIZE);
586310b0d55SVutla, Lokesh 	pages = get_order(total);
5876242332fSJoel Fernandes 
5886242332fSJoel Fernandes 	buf_in = (void *)__get_free_pages(GFP_ATOMIC, pages);
5896242332fSJoel Fernandes 	buf_out = (void *)__get_free_pages(GFP_ATOMIC, pages);
5906242332fSJoel Fernandes 
5916242332fSJoel Fernandes 	if (!buf_in || !buf_out) {
5926242332fSJoel Fernandes 		pr_err("Couldn't allocated pages for unaligned cases.\n");
5936242332fSJoel Fernandes 		return -1;
5946242332fSJoel Fernandes 	}
5956242332fSJoel Fernandes 
5966242332fSJoel Fernandes 	dd->orig_out = dd->out_sg;
5976242332fSJoel Fernandes 
5986242332fSJoel Fernandes 	sg_copy_buf(buf_in, dd->in_sg, 0, dd->total, 0);
5996242332fSJoel Fernandes 
6006242332fSJoel Fernandes 	sg_init_table(&dd->in_sgl, 1);
601310b0d55SVutla, Lokesh 	sg_set_buf(&dd->in_sgl, buf_in, total);
6026242332fSJoel Fernandes 	dd->in_sg = &dd->in_sgl;
6036242332fSJoel Fernandes 
6046242332fSJoel Fernandes 	sg_init_table(&dd->out_sgl, 1);
605310b0d55SVutla, Lokesh 	sg_set_buf(&dd->out_sgl, buf_out, total);
6066242332fSJoel Fernandes 	dd->out_sg = &dd->out_sgl;
6076242332fSJoel Fernandes 
6086242332fSJoel Fernandes 	return 0;
6096242332fSJoel Fernandes }
6106242332fSJoel Fernandes 
61121fe9767SDmitry Kasatkin static int omap_aes_handle_queue(struct omap_aes_dev *dd,
612eeb2b202SDmitry Kasatkin 			       struct ablkcipher_request *req)
613537559a5SDmitry Kasatkin {
614537559a5SDmitry Kasatkin 	struct crypto_async_request *async_req, *backlog;
615537559a5SDmitry Kasatkin 	struct omap_aes_ctx *ctx;
616537559a5SDmitry Kasatkin 	struct omap_aes_reqctx *rctx;
617537559a5SDmitry Kasatkin 	unsigned long flags;
618310b0d55SVutla, Lokesh 	int err, ret = 0, len;
619537559a5SDmitry Kasatkin 
620537559a5SDmitry Kasatkin 	spin_lock_irqsave(&dd->lock, flags);
621eeb2b202SDmitry Kasatkin 	if (req)
62221fe9767SDmitry Kasatkin 		ret = ablkcipher_enqueue_request(&dd->queue, req);
623eeb2b202SDmitry Kasatkin 	if (dd->flags & FLAGS_BUSY) {
624eeb2b202SDmitry Kasatkin 		spin_unlock_irqrestore(&dd->lock, flags);
62521fe9767SDmitry Kasatkin 		return ret;
626eeb2b202SDmitry Kasatkin 	}
627537559a5SDmitry Kasatkin 	backlog = crypto_get_backlog(&dd->queue);
628537559a5SDmitry Kasatkin 	async_req = crypto_dequeue_request(&dd->queue);
629eeb2b202SDmitry Kasatkin 	if (async_req)
630eeb2b202SDmitry Kasatkin 		dd->flags |= FLAGS_BUSY;
631537559a5SDmitry Kasatkin 	spin_unlock_irqrestore(&dd->lock, flags);
632537559a5SDmitry Kasatkin 
633537559a5SDmitry Kasatkin 	if (!async_req)
63421fe9767SDmitry Kasatkin 		return ret;
635537559a5SDmitry Kasatkin 
636537559a5SDmitry Kasatkin 	if (backlog)
637537559a5SDmitry Kasatkin 		backlog->complete(backlog, -EINPROGRESS);
638537559a5SDmitry Kasatkin 
639537559a5SDmitry Kasatkin 	req = ablkcipher_request_cast(async_req);
640537559a5SDmitry Kasatkin 
641537559a5SDmitry Kasatkin 	/* assign new request to device */
642537559a5SDmitry Kasatkin 	dd->req = req;
643537559a5SDmitry Kasatkin 	dd->total = req->nbytes;
6446242332fSJoel Fernandes 	dd->total_save = req->nbytes;
645537559a5SDmitry Kasatkin 	dd->in_sg = req->src;
646537559a5SDmitry Kasatkin 	dd->out_sg = req->dst;
647537559a5SDmitry Kasatkin 
6486d7e7e02SVutla, Lokesh 	if (omap_aes_check_aligned(dd->in_sg, dd->total) ||
6496d7e7e02SVutla, Lokesh 	    omap_aes_check_aligned(dd->out_sg, dd->total)) {
6506242332fSJoel Fernandes 		if (omap_aes_copy_sgs(dd))
6516242332fSJoel Fernandes 			pr_err("Failed to copy SGs for unaligned cases\n");
6526242332fSJoel Fernandes 		dd->sgs_copied = 1;
6536242332fSJoel Fernandes 	} else {
6546242332fSJoel Fernandes 		dd->sgs_copied = 0;
6556242332fSJoel Fernandes 	}
6566242332fSJoel Fernandes 
657310b0d55SVutla, Lokesh 	len = ALIGN(dd->total, AES_BLOCK_SIZE);
658310b0d55SVutla, Lokesh 	dd->in_sg_len = scatterwalk_bytes_sglen(dd->in_sg, len);
659310b0d55SVutla, Lokesh 	dd->out_sg_len = scatterwalk_bytes_sglen(dd->out_sg, len);
660e77c756eSJoel Fernandes 	BUG_ON(dd->in_sg_len < 0 || dd->out_sg_len < 0);
661e77c756eSJoel Fernandes 
662537559a5SDmitry Kasatkin 	rctx = ablkcipher_request_ctx(req);
663537559a5SDmitry Kasatkin 	ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
664537559a5SDmitry Kasatkin 	rctx->mode &= FLAGS_MODE_MASK;
665537559a5SDmitry Kasatkin 	dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode;
666537559a5SDmitry Kasatkin 
667537559a5SDmitry Kasatkin 	dd->ctx = ctx;
66867a730ceSDmitry Kasatkin 	ctx->dd = dd;
669537559a5SDmitry Kasatkin 
67083ea7e0fSDmitry Kasatkin 	err = omap_aes_write_ctrl(dd);
67183ea7e0fSDmitry Kasatkin 	if (!err)
67221fe9767SDmitry Kasatkin 		err = omap_aes_crypt_dma_start(dd);
67321fe9767SDmitry Kasatkin 	if (err) {
67421fe9767SDmitry Kasatkin 		/* aes_task will not finish it, so do it here */
67521fe9767SDmitry Kasatkin 		omap_aes_finish_req(dd, err);
67621fe9767SDmitry Kasatkin 		tasklet_schedule(&dd->queue_task);
677537559a5SDmitry Kasatkin 	}
678537559a5SDmitry Kasatkin 
67921fe9767SDmitry Kasatkin 	return ret; /* return ret, which is enqueue return value */
68021fe9767SDmitry Kasatkin }
68121fe9767SDmitry Kasatkin 
68221fe9767SDmitry Kasatkin static void omap_aes_done_task(unsigned long data)
683537559a5SDmitry Kasatkin {
684537559a5SDmitry Kasatkin 	struct omap_aes_dev *dd = (struct omap_aes_dev *)data;
6856242332fSJoel Fernandes 	void *buf_in, *buf_out;
686310b0d55SVutla, Lokesh 	int pages, len;
687537559a5SDmitry Kasatkin 
6884b645c94SJoel Fernandes 	pr_debug("enter done_task\n");
689537559a5SDmitry Kasatkin 
69098837abcSJoel Fernandes 	if (!dd->pio_only) {
69198837abcSJoel Fernandes 		dma_sync_sg_for_device(dd->dev, dd->out_sg, dd->out_sg_len,
69298837abcSJoel Fernandes 				       DMA_FROM_DEVICE);
6936242332fSJoel Fernandes 		dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
6946242332fSJoel Fernandes 		dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
6956242332fSJoel Fernandes 			     DMA_FROM_DEVICE);
6964b645c94SJoel Fernandes 		omap_aes_crypt_dma_stop(dd);
69798837abcSJoel Fernandes 	}
6986242332fSJoel Fernandes 
6996242332fSJoel Fernandes 	if (dd->sgs_copied) {
7006242332fSJoel Fernandes 		buf_in = sg_virt(&dd->in_sgl);
7016242332fSJoel Fernandes 		buf_out = sg_virt(&dd->out_sgl);
7026242332fSJoel Fernandes 
7036242332fSJoel Fernandes 		sg_copy_buf(buf_out, dd->orig_out, 0, dd->total_save, 1);
7046242332fSJoel Fernandes 
705310b0d55SVutla, Lokesh 		len = ALIGN(dd->total_save, AES_BLOCK_SIZE);
706310b0d55SVutla, Lokesh 		pages = get_order(len);
7076242332fSJoel Fernandes 		free_pages((unsigned long)buf_in, pages);
7086242332fSJoel Fernandes 		free_pages((unsigned long)buf_out, pages);
7096242332fSJoel Fernandes 	}
7106242332fSJoel Fernandes 
7114b645c94SJoel Fernandes 	omap_aes_finish_req(dd, 0);
71221fe9767SDmitry Kasatkin 	omap_aes_handle_queue(dd, NULL);
713537559a5SDmitry Kasatkin 
714537559a5SDmitry Kasatkin 	pr_debug("exit\n");
715537559a5SDmitry Kasatkin }
716537559a5SDmitry Kasatkin 
71721fe9767SDmitry Kasatkin static void omap_aes_queue_task(unsigned long data)
71821fe9767SDmitry Kasatkin {
71921fe9767SDmitry Kasatkin 	struct omap_aes_dev *dd = (struct omap_aes_dev *)data;
72021fe9767SDmitry Kasatkin 
72121fe9767SDmitry Kasatkin 	omap_aes_handle_queue(dd, NULL);
72221fe9767SDmitry Kasatkin }
72321fe9767SDmitry Kasatkin 
724537559a5SDmitry Kasatkin static int omap_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
725537559a5SDmitry Kasatkin {
726537559a5SDmitry Kasatkin 	struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(
727537559a5SDmitry Kasatkin 			crypto_ablkcipher_reqtfm(req));
728537559a5SDmitry Kasatkin 	struct omap_aes_reqctx *rctx = ablkcipher_request_ctx(req);
729537559a5SDmitry Kasatkin 	struct omap_aes_dev *dd;
730537559a5SDmitry Kasatkin 
731537559a5SDmitry Kasatkin 	pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req->nbytes,
732537559a5SDmitry Kasatkin 		  !!(mode & FLAGS_ENCRYPT),
733537559a5SDmitry Kasatkin 		  !!(mode & FLAGS_CBC));
734537559a5SDmitry Kasatkin 
735537559a5SDmitry Kasatkin 	dd = omap_aes_find_dev(ctx);
736537559a5SDmitry Kasatkin 	if (!dd)
737537559a5SDmitry Kasatkin 		return -ENODEV;
738537559a5SDmitry Kasatkin 
739537559a5SDmitry Kasatkin 	rctx->mode = mode;
740537559a5SDmitry Kasatkin 
74121fe9767SDmitry Kasatkin 	return omap_aes_handle_queue(dd, req);
742537559a5SDmitry Kasatkin }
743537559a5SDmitry Kasatkin 
744537559a5SDmitry Kasatkin /* ********************** ALG API ************************************ */
745537559a5SDmitry Kasatkin 
746537559a5SDmitry Kasatkin static int omap_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
747537559a5SDmitry Kasatkin 			   unsigned int keylen)
748537559a5SDmitry Kasatkin {
749537559a5SDmitry Kasatkin 	struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(tfm);
750537559a5SDmitry Kasatkin 
751537559a5SDmitry Kasatkin 	if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
752537559a5SDmitry Kasatkin 		   keylen != AES_KEYSIZE_256)
753537559a5SDmitry Kasatkin 		return -EINVAL;
754537559a5SDmitry Kasatkin 
755537559a5SDmitry Kasatkin 	pr_debug("enter, keylen: %d\n", keylen);
756537559a5SDmitry Kasatkin 
757537559a5SDmitry Kasatkin 	memcpy(ctx->key, key, keylen);
758537559a5SDmitry Kasatkin 	ctx->keylen = keylen;
759537559a5SDmitry Kasatkin 
760537559a5SDmitry Kasatkin 	return 0;
761537559a5SDmitry Kasatkin }
762537559a5SDmitry Kasatkin 
763537559a5SDmitry Kasatkin static int omap_aes_ecb_encrypt(struct ablkcipher_request *req)
764537559a5SDmitry Kasatkin {
765537559a5SDmitry Kasatkin 	return omap_aes_crypt(req, FLAGS_ENCRYPT);
766537559a5SDmitry Kasatkin }
767537559a5SDmitry Kasatkin 
768537559a5SDmitry Kasatkin static int omap_aes_ecb_decrypt(struct ablkcipher_request *req)
769537559a5SDmitry Kasatkin {
770537559a5SDmitry Kasatkin 	return omap_aes_crypt(req, 0);
771537559a5SDmitry Kasatkin }
772537559a5SDmitry Kasatkin 
773537559a5SDmitry Kasatkin static int omap_aes_cbc_encrypt(struct ablkcipher_request *req)
774537559a5SDmitry Kasatkin {
775537559a5SDmitry Kasatkin 	return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC);
776537559a5SDmitry Kasatkin }
777537559a5SDmitry Kasatkin 
778537559a5SDmitry Kasatkin static int omap_aes_cbc_decrypt(struct ablkcipher_request *req)
779537559a5SDmitry Kasatkin {
780537559a5SDmitry Kasatkin 	return omap_aes_crypt(req, FLAGS_CBC);
781537559a5SDmitry Kasatkin }
782537559a5SDmitry Kasatkin 
783f9fb69e7SMark A. Greer static int omap_aes_ctr_encrypt(struct ablkcipher_request *req)
784f9fb69e7SMark A. Greer {
785f9fb69e7SMark A. Greer 	return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CTR);
786f9fb69e7SMark A. Greer }
787f9fb69e7SMark A. Greer 
788f9fb69e7SMark A. Greer static int omap_aes_ctr_decrypt(struct ablkcipher_request *req)
789f9fb69e7SMark A. Greer {
790f9fb69e7SMark A. Greer 	return omap_aes_crypt(req, FLAGS_CTR);
791f9fb69e7SMark A. Greer }
792f9fb69e7SMark A. Greer 
793537559a5SDmitry Kasatkin static int omap_aes_cra_init(struct crypto_tfm *tfm)
794537559a5SDmitry Kasatkin {
795a3485e68SJoel A Fernandes 	struct omap_aes_dev *dd = NULL;
796f7b2b5ddSNishanth Menon 	int err;
797537559a5SDmitry Kasatkin 
798a3485e68SJoel A Fernandes 	/* Find AES device, currently picks the first device */
799a3485e68SJoel A Fernandes 	spin_lock_bh(&list_lock);
800a3485e68SJoel A Fernandes 	list_for_each_entry(dd, &dev_list, list) {
801a3485e68SJoel A Fernandes 		break;
802a3485e68SJoel A Fernandes 	}
803a3485e68SJoel A Fernandes 	spin_unlock_bh(&list_lock);
804a3485e68SJoel A Fernandes 
805f7b2b5ddSNishanth Menon 	err = pm_runtime_get_sync(dd->dev);
806f7b2b5ddSNishanth Menon 	if (err < 0) {
807f7b2b5ddSNishanth Menon 		dev_err(dd->dev, "%s: failed to get_sync(%d)\n",
808f7b2b5ddSNishanth Menon 			__func__, err);
809f7b2b5ddSNishanth Menon 		return err;
810f7b2b5ddSNishanth Menon 	}
811f7b2b5ddSNishanth Menon 
812537559a5SDmitry Kasatkin 	tfm->crt_ablkcipher.reqsize = sizeof(struct omap_aes_reqctx);
813537559a5SDmitry Kasatkin 
814537559a5SDmitry Kasatkin 	return 0;
815537559a5SDmitry Kasatkin }
816537559a5SDmitry Kasatkin 
817537559a5SDmitry Kasatkin static void omap_aes_cra_exit(struct crypto_tfm *tfm)
818537559a5SDmitry Kasatkin {
819a3485e68SJoel A Fernandes 	struct omap_aes_dev *dd = NULL;
820a3485e68SJoel A Fernandes 
821a3485e68SJoel A Fernandes 	/* Find AES device, currently picks the first device */
822a3485e68SJoel A Fernandes 	spin_lock_bh(&list_lock);
823a3485e68SJoel A Fernandes 	list_for_each_entry(dd, &dev_list, list) {
824a3485e68SJoel A Fernandes 		break;
825a3485e68SJoel A Fernandes 	}
826a3485e68SJoel A Fernandes 	spin_unlock_bh(&list_lock);
827a3485e68SJoel A Fernandes 
828a3485e68SJoel A Fernandes 	pm_runtime_put_sync(dd->dev);
829537559a5SDmitry Kasatkin }
830537559a5SDmitry Kasatkin 
831537559a5SDmitry Kasatkin /* ********************** ALGS ************************************ */
832537559a5SDmitry Kasatkin 
833f9fb69e7SMark A. Greer static struct crypto_alg algs_ecb_cbc[] = {
834537559a5SDmitry Kasatkin {
835537559a5SDmitry Kasatkin 	.cra_name		= "ecb(aes)",
836537559a5SDmitry Kasatkin 	.cra_driver_name	= "ecb-aes-omap",
837537559a5SDmitry Kasatkin 	.cra_priority		= 100,
838d912bb76SNikos Mavrogiannopoulos 	.cra_flags		= CRYPTO_ALG_TYPE_ABLKCIPHER |
839d912bb76SNikos Mavrogiannopoulos 				  CRYPTO_ALG_KERN_DRIVER_ONLY |
840d912bb76SNikos Mavrogiannopoulos 				  CRYPTO_ALG_ASYNC,
841537559a5SDmitry Kasatkin 	.cra_blocksize		= AES_BLOCK_SIZE,
842537559a5SDmitry Kasatkin 	.cra_ctxsize		= sizeof(struct omap_aes_ctx),
843537559a5SDmitry Kasatkin 	.cra_alignmask		= 0,
844537559a5SDmitry Kasatkin 	.cra_type		= &crypto_ablkcipher_type,
845537559a5SDmitry Kasatkin 	.cra_module		= THIS_MODULE,
846537559a5SDmitry Kasatkin 	.cra_init		= omap_aes_cra_init,
847537559a5SDmitry Kasatkin 	.cra_exit		= omap_aes_cra_exit,
848537559a5SDmitry Kasatkin 	.cra_u.ablkcipher = {
849537559a5SDmitry Kasatkin 		.min_keysize	= AES_MIN_KEY_SIZE,
850537559a5SDmitry Kasatkin 		.max_keysize	= AES_MAX_KEY_SIZE,
851537559a5SDmitry Kasatkin 		.setkey		= omap_aes_setkey,
852537559a5SDmitry Kasatkin 		.encrypt	= omap_aes_ecb_encrypt,
853537559a5SDmitry Kasatkin 		.decrypt	= omap_aes_ecb_decrypt,
854537559a5SDmitry Kasatkin 	}
855537559a5SDmitry Kasatkin },
856537559a5SDmitry Kasatkin {
857537559a5SDmitry Kasatkin 	.cra_name		= "cbc(aes)",
858537559a5SDmitry Kasatkin 	.cra_driver_name	= "cbc-aes-omap",
859537559a5SDmitry Kasatkin 	.cra_priority		= 100,
860d912bb76SNikos Mavrogiannopoulos 	.cra_flags		= CRYPTO_ALG_TYPE_ABLKCIPHER |
861d912bb76SNikos Mavrogiannopoulos 				  CRYPTO_ALG_KERN_DRIVER_ONLY |
862d912bb76SNikos Mavrogiannopoulos 				  CRYPTO_ALG_ASYNC,
863537559a5SDmitry Kasatkin 	.cra_blocksize		= AES_BLOCK_SIZE,
864537559a5SDmitry Kasatkin 	.cra_ctxsize		= sizeof(struct omap_aes_ctx),
865537559a5SDmitry Kasatkin 	.cra_alignmask		= 0,
866537559a5SDmitry Kasatkin 	.cra_type		= &crypto_ablkcipher_type,
867537559a5SDmitry Kasatkin 	.cra_module		= THIS_MODULE,
868537559a5SDmitry Kasatkin 	.cra_init		= omap_aes_cra_init,
869537559a5SDmitry Kasatkin 	.cra_exit		= omap_aes_cra_exit,
870537559a5SDmitry Kasatkin 	.cra_u.ablkcipher = {
871537559a5SDmitry Kasatkin 		.min_keysize	= AES_MIN_KEY_SIZE,
872537559a5SDmitry Kasatkin 		.max_keysize	= AES_MAX_KEY_SIZE,
873537559a5SDmitry Kasatkin 		.ivsize		= AES_BLOCK_SIZE,
874537559a5SDmitry Kasatkin 		.setkey		= omap_aes_setkey,
875537559a5SDmitry Kasatkin 		.encrypt	= omap_aes_cbc_encrypt,
876537559a5SDmitry Kasatkin 		.decrypt	= omap_aes_cbc_decrypt,
877537559a5SDmitry Kasatkin 	}
878537559a5SDmitry Kasatkin }
879537559a5SDmitry Kasatkin };
880537559a5SDmitry Kasatkin 
881f9fb69e7SMark A. Greer static struct crypto_alg algs_ctr[] = {
882f9fb69e7SMark A. Greer {
883f9fb69e7SMark A. Greer 	.cra_name		= "ctr(aes)",
884f9fb69e7SMark A. Greer 	.cra_driver_name	= "ctr-aes-omap",
885f9fb69e7SMark A. Greer 	.cra_priority		= 100,
886f9fb69e7SMark A. Greer 	.cra_flags		= CRYPTO_ALG_TYPE_ABLKCIPHER |
887f9fb69e7SMark A. Greer 				  CRYPTO_ALG_KERN_DRIVER_ONLY |
888f9fb69e7SMark A. Greer 				  CRYPTO_ALG_ASYNC,
889f9fb69e7SMark A. Greer 	.cra_blocksize		= AES_BLOCK_SIZE,
890f9fb69e7SMark A. Greer 	.cra_ctxsize		= sizeof(struct omap_aes_ctx),
891f9fb69e7SMark A. Greer 	.cra_alignmask		= 0,
892f9fb69e7SMark A. Greer 	.cra_type		= &crypto_ablkcipher_type,
893f9fb69e7SMark A. Greer 	.cra_module		= THIS_MODULE,
894f9fb69e7SMark A. Greer 	.cra_init		= omap_aes_cra_init,
895f9fb69e7SMark A. Greer 	.cra_exit		= omap_aes_cra_exit,
896f9fb69e7SMark A. Greer 	.cra_u.ablkcipher = {
897f9fb69e7SMark A. Greer 		.min_keysize	= AES_MIN_KEY_SIZE,
898f9fb69e7SMark A. Greer 		.max_keysize	= AES_MAX_KEY_SIZE,
899f9fb69e7SMark A. Greer 		.geniv		= "eseqiv",
900f9fb69e7SMark A. Greer 		.ivsize		= AES_BLOCK_SIZE,
901f9fb69e7SMark A. Greer 		.setkey		= omap_aes_setkey,
902f9fb69e7SMark A. Greer 		.encrypt	= omap_aes_ctr_encrypt,
903f9fb69e7SMark A. Greer 		.decrypt	= omap_aes_ctr_decrypt,
904f9fb69e7SMark A. Greer 	}
905f9fb69e7SMark A. Greer } ,
906f9fb69e7SMark A. Greer };
907f9fb69e7SMark A. Greer 
908f9fb69e7SMark A. Greer static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc[] = {
909f9fb69e7SMark A. Greer 	{
910f9fb69e7SMark A. Greer 		.algs_list	= algs_ecb_cbc,
911f9fb69e7SMark A. Greer 		.size		= ARRAY_SIZE(algs_ecb_cbc),
912f9fb69e7SMark A. Greer 	},
913f9fb69e7SMark A. Greer };
914f9fb69e7SMark A. Greer 
9150d35583aSMark A. Greer static const struct omap_aes_pdata omap_aes_pdata_omap2 = {
916f9fb69e7SMark A. Greer 	.algs_info	= omap_aes_algs_info_ecb_cbc,
917f9fb69e7SMark A. Greer 	.algs_info_size	= ARRAY_SIZE(omap_aes_algs_info_ecb_cbc),
9180d35583aSMark A. Greer 	.trigger	= omap_aes_dma_trigger_omap2,
9190d35583aSMark A. Greer 	.key_ofs	= 0x1c,
9200d35583aSMark A. Greer 	.iv_ofs		= 0x20,
9210d35583aSMark A. Greer 	.ctrl_ofs	= 0x30,
9220d35583aSMark A. Greer 	.data_ofs	= 0x34,
9230d35583aSMark A. Greer 	.rev_ofs	= 0x44,
9240d35583aSMark A. Greer 	.mask_ofs	= 0x48,
9250d35583aSMark A. Greer 	.dma_enable_in	= BIT(2),
9260d35583aSMark A. Greer 	.dma_enable_out	= BIT(3),
9270d35583aSMark A. Greer 	.dma_start	= BIT(5),
9280d35583aSMark A. Greer 	.major_mask	= 0xf0,
9290d35583aSMark A. Greer 	.major_shift	= 4,
9300d35583aSMark A. Greer 	.minor_mask	= 0x0f,
9310d35583aSMark A. Greer 	.minor_shift	= 0,
9320d35583aSMark A. Greer };
9330d35583aSMark A. Greer 
934bc69d124SMark A. Greer #ifdef CONFIG_OF
935f9fb69e7SMark A. Greer static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc_ctr[] = {
936f9fb69e7SMark A. Greer 	{
937f9fb69e7SMark A. Greer 		.algs_list	= algs_ecb_cbc,
938f9fb69e7SMark A. Greer 		.size		= ARRAY_SIZE(algs_ecb_cbc),
939f9fb69e7SMark A. Greer 	},
940f9fb69e7SMark A. Greer 	{
941f9fb69e7SMark A. Greer 		.algs_list	= algs_ctr,
942f9fb69e7SMark A. Greer 		.size		= ARRAY_SIZE(algs_ctr),
943f9fb69e7SMark A. Greer 	},
944f9fb69e7SMark A. Greer };
945f9fb69e7SMark A. Greer 
946f9fb69e7SMark A. Greer static const struct omap_aes_pdata omap_aes_pdata_omap3 = {
947f9fb69e7SMark A. Greer 	.algs_info	= omap_aes_algs_info_ecb_cbc_ctr,
948f9fb69e7SMark A. Greer 	.algs_info_size	= ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
949f9fb69e7SMark A. Greer 	.trigger	= omap_aes_dma_trigger_omap2,
950f9fb69e7SMark A. Greer 	.key_ofs	= 0x1c,
951f9fb69e7SMark A. Greer 	.iv_ofs		= 0x20,
952f9fb69e7SMark A. Greer 	.ctrl_ofs	= 0x30,
953f9fb69e7SMark A. Greer 	.data_ofs	= 0x34,
954f9fb69e7SMark A. Greer 	.rev_ofs	= 0x44,
955f9fb69e7SMark A. Greer 	.mask_ofs	= 0x48,
956f9fb69e7SMark A. Greer 	.dma_enable_in	= BIT(2),
957f9fb69e7SMark A. Greer 	.dma_enable_out	= BIT(3),
958f9fb69e7SMark A. Greer 	.dma_start	= BIT(5),
959f9fb69e7SMark A. Greer 	.major_mask	= 0xf0,
960f9fb69e7SMark A. Greer 	.major_shift	= 4,
961f9fb69e7SMark A. Greer 	.minor_mask	= 0x0f,
962f9fb69e7SMark A. Greer 	.minor_shift	= 0,
963f9fb69e7SMark A. Greer };
964f9fb69e7SMark A. Greer 
9650d35583aSMark A. Greer static const struct omap_aes_pdata omap_aes_pdata_omap4 = {
966f9fb69e7SMark A. Greer 	.algs_info	= omap_aes_algs_info_ecb_cbc_ctr,
967f9fb69e7SMark A. Greer 	.algs_info_size	= ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
9680d35583aSMark A. Greer 	.trigger	= omap_aes_dma_trigger_omap4,
9690d35583aSMark A. Greer 	.key_ofs	= 0x3c,
9700d35583aSMark A. Greer 	.iv_ofs		= 0x40,
9710d35583aSMark A. Greer 	.ctrl_ofs	= 0x50,
9720d35583aSMark A. Greer 	.data_ofs	= 0x60,
9730d35583aSMark A. Greer 	.rev_ofs	= 0x80,
9740d35583aSMark A. Greer 	.mask_ofs	= 0x84,
97567216756SJoel Fernandes 	.irq_status_ofs = 0x8c,
97667216756SJoel Fernandes 	.irq_enable_ofs = 0x90,
9770d35583aSMark A. Greer 	.dma_enable_in	= BIT(5),
9780d35583aSMark A. Greer 	.dma_enable_out	= BIT(6),
9790d35583aSMark A. Greer 	.major_mask	= 0x0700,
9800d35583aSMark A. Greer 	.major_shift	= 8,
9810d35583aSMark A. Greer 	.minor_mask	= 0x003f,
9820d35583aSMark A. Greer 	.minor_shift	= 0,
9830d35583aSMark A. Greer };
9840d35583aSMark A. Greer 
9851bf95ccaSJoel Fernandes static irqreturn_t omap_aes_irq(int irq, void *dev_id)
9861bf95ccaSJoel Fernandes {
9871bf95ccaSJoel Fernandes 	struct omap_aes_dev *dd = dev_id;
9881bf95ccaSJoel Fernandes 	u32 status, i;
9891bf95ccaSJoel Fernandes 	u32 *src, *dst;
9901bf95ccaSJoel Fernandes 
9911bf95ccaSJoel Fernandes 	status = omap_aes_read(dd, AES_REG_IRQ_STATUS(dd));
9921bf95ccaSJoel Fernandes 	if (status & AES_REG_IRQ_DATA_IN) {
9931bf95ccaSJoel Fernandes 		omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x0);
9941bf95ccaSJoel Fernandes 
9951bf95ccaSJoel Fernandes 		BUG_ON(!dd->in_sg);
9961bf95ccaSJoel Fernandes 
9971bf95ccaSJoel Fernandes 		BUG_ON(_calc_walked(in) > dd->in_sg->length);
9981bf95ccaSJoel Fernandes 
9991bf95ccaSJoel Fernandes 		src = sg_virt(dd->in_sg) + _calc_walked(in);
10001bf95ccaSJoel Fernandes 
10011bf95ccaSJoel Fernandes 		for (i = 0; i < AES_BLOCK_WORDS; i++) {
10021bf95ccaSJoel Fernandes 			omap_aes_write(dd, AES_REG_DATA_N(dd, i), *src);
10031bf95ccaSJoel Fernandes 
10041bf95ccaSJoel Fernandes 			scatterwalk_advance(&dd->in_walk, 4);
10051bf95ccaSJoel Fernandes 			if (dd->in_sg->length == _calc_walked(in)) {
10065be4d4c9SCristian Stoica 				dd->in_sg = sg_next(dd->in_sg);
10071bf95ccaSJoel Fernandes 				if (dd->in_sg) {
10081bf95ccaSJoel Fernandes 					scatterwalk_start(&dd->in_walk,
10091bf95ccaSJoel Fernandes 							  dd->in_sg);
10101bf95ccaSJoel Fernandes 					src = sg_virt(dd->in_sg) +
10111bf95ccaSJoel Fernandes 					      _calc_walked(in);
10121bf95ccaSJoel Fernandes 				}
10131bf95ccaSJoel Fernandes 			} else {
10141bf95ccaSJoel Fernandes 				src++;
10151bf95ccaSJoel Fernandes 			}
10161bf95ccaSJoel Fernandes 		}
10171bf95ccaSJoel Fernandes 
10181bf95ccaSJoel Fernandes 		/* Clear IRQ status */
10191bf95ccaSJoel Fernandes 		status &= ~AES_REG_IRQ_DATA_IN;
10201bf95ccaSJoel Fernandes 		omap_aes_write(dd, AES_REG_IRQ_STATUS(dd), status);
10211bf95ccaSJoel Fernandes 
10221bf95ccaSJoel Fernandes 		/* Enable DATA_OUT interrupt */
10231bf95ccaSJoel Fernandes 		omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x4);
10241bf95ccaSJoel Fernandes 
10251bf95ccaSJoel Fernandes 	} else if (status & AES_REG_IRQ_DATA_OUT) {
10261bf95ccaSJoel Fernandes 		omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x0);
10271bf95ccaSJoel Fernandes 
10281bf95ccaSJoel Fernandes 		BUG_ON(!dd->out_sg);
10291bf95ccaSJoel Fernandes 
10301bf95ccaSJoel Fernandes 		BUG_ON(_calc_walked(out) > dd->out_sg->length);
10311bf95ccaSJoel Fernandes 
10321bf95ccaSJoel Fernandes 		dst = sg_virt(dd->out_sg) + _calc_walked(out);
10331bf95ccaSJoel Fernandes 
10341bf95ccaSJoel Fernandes 		for (i = 0; i < AES_BLOCK_WORDS; i++) {
10351bf95ccaSJoel Fernandes 			*dst = omap_aes_read(dd, AES_REG_DATA_N(dd, i));
10361bf95ccaSJoel Fernandes 			scatterwalk_advance(&dd->out_walk, 4);
10371bf95ccaSJoel Fernandes 			if (dd->out_sg->length == _calc_walked(out)) {
10385be4d4c9SCristian Stoica 				dd->out_sg = sg_next(dd->out_sg);
10391bf95ccaSJoel Fernandes 				if (dd->out_sg) {
10401bf95ccaSJoel Fernandes 					scatterwalk_start(&dd->out_walk,
10411bf95ccaSJoel Fernandes 							  dd->out_sg);
10421bf95ccaSJoel Fernandes 					dst = sg_virt(dd->out_sg) +
10431bf95ccaSJoel Fernandes 					      _calc_walked(out);
10441bf95ccaSJoel Fernandes 				}
10451bf95ccaSJoel Fernandes 			} else {
10461bf95ccaSJoel Fernandes 				dst++;
10471bf95ccaSJoel Fernandes 			}
10481bf95ccaSJoel Fernandes 		}
10491bf95ccaSJoel Fernandes 
1050310b0d55SVutla, Lokesh 		dd->total -= min_t(size_t, AES_BLOCK_SIZE, dd->total);
10511bf95ccaSJoel Fernandes 
10521bf95ccaSJoel Fernandes 		/* Clear IRQ status */
10531bf95ccaSJoel Fernandes 		status &= ~AES_REG_IRQ_DATA_OUT;
10541bf95ccaSJoel Fernandes 		omap_aes_write(dd, AES_REG_IRQ_STATUS(dd), status);
10551bf95ccaSJoel Fernandes 
10561bf95ccaSJoel Fernandes 		if (!dd->total)
10571bf95ccaSJoel Fernandes 			/* All bytes read! */
10581bf95ccaSJoel Fernandes 			tasklet_schedule(&dd->done_task);
10591bf95ccaSJoel Fernandes 		else
10601bf95ccaSJoel Fernandes 			/* Enable DATA_IN interrupt for next block */
10611bf95ccaSJoel Fernandes 			omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x2);
10621bf95ccaSJoel Fernandes 	}
10631bf95ccaSJoel Fernandes 
10641bf95ccaSJoel Fernandes 	return IRQ_HANDLED;
10651bf95ccaSJoel Fernandes }
10661bf95ccaSJoel Fernandes 
1067bc69d124SMark A. Greer static const struct of_device_id omap_aes_of_match[] = {
1068bc69d124SMark A. Greer 	{
1069bc69d124SMark A. Greer 		.compatible	= "ti,omap2-aes",
10700d35583aSMark A. Greer 		.data		= &omap_aes_pdata_omap2,
10710d35583aSMark A. Greer 	},
10720d35583aSMark A. Greer 	{
1073f9fb69e7SMark A. Greer 		.compatible	= "ti,omap3-aes",
1074f9fb69e7SMark A. Greer 		.data		= &omap_aes_pdata_omap3,
1075f9fb69e7SMark A. Greer 	},
1076f9fb69e7SMark A. Greer 	{
10770d35583aSMark A. Greer 		.compatible	= "ti,omap4-aes",
10780d35583aSMark A. Greer 		.data		= &omap_aes_pdata_omap4,
1079bc69d124SMark A. Greer 	},
1080bc69d124SMark A. Greer 	{},
1081bc69d124SMark A. Greer };
1082bc69d124SMark A. Greer MODULE_DEVICE_TABLE(of, omap_aes_of_match);
1083bc69d124SMark A. Greer 
1084bc69d124SMark A. Greer static int omap_aes_get_res_of(struct omap_aes_dev *dd,
1085bc69d124SMark A. Greer 		struct device *dev, struct resource *res)
1086bc69d124SMark A. Greer {
1087bc69d124SMark A. Greer 	struct device_node *node = dev->of_node;
1088bc69d124SMark A. Greer 	const struct of_device_id *match;
1089bc69d124SMark A. Greer 	int err = 0;
1090bc69d124SMark A. Greer 
1091bc69d124SMark A. Greer 	match = of_match_device(of_match_ptr(omap_aes_of_match), dev);
1092bc69d124SMark A. Greer 	if (!match) {
1093bc69d124SMark A. Greer 		dev_err(dev, "no compatible OF match\n");
1094bc69d124SMark A. Greer 		err = -EINVAL;
1095bc69d124SMark A. Greer 		goto err;
1096bc69d124SMark A. Greer 	}
1097bc69d124SMark A. Greer 
1098bc69d124SMark A. Greer 	err = of_address_to_resource(node, 0, res);
1099bc69d124SMark A. Greer 	if (err < 0) {
1100bc69d124SMark A. Greer 		dev_err(dev, "can't translate OF node address\n");
1101bc69d124SMark A. Greer 		err = -EINVAL;
1102bc69d124SMark A. Greer 		goto err;
1103bc69d124SMark A. Greer 	}
1104bc69d124SMark A. Greer 
1105bc69d124SMark A. Greer 	dd->dma_out = -1; /* Dummy value that's unused */
1106bc69d124SMark A. Greer 	dd->dma_in = -1; /* Dummy value that's unused */
1107bc69d124SMark A. Greer 
11080d35583aSMark A. Greer 	dd->pdata = match->data;
11090d35583aSMark A. Greer 
1110bc69d124SMark A. Greer err:
1111bc69d124SMark A. Greer 	return err;
1112bc69d124SMark A. Greer }
1113bc69d124SMark A. Greer #else
1114bc69d124SMark A. Greer static const struct of_device_id omap_aes_of_match[] = {
1115bc69d124SMark A. Greer 	{},
1116bc69d124SMark A. Greer };
1117bc69d124SMark A. Greer 
1118bc69d124SMark A. Greer static int omap_aes_get_res_of(struct omap_aes_dev *dd,
1119bc69d124SMark A. Greer 		struct device *dev, struct resource *res)
1120bc69d124SMark A. Greer {
1121bc69d124SMark A. Greer 	return -EINVAL;
1122bc69d124SMark A. Greer }
1123bc69d124SMark A. Greer #endif
1124bc69d124SMark A. Greer 
1125bc69d124SMark A. Greer static int omap_aes_get_res_pdev(struct omap_aes_dev *dd,
1126bc69d124SMark A. Greer 		struct platform_device *pdev, struct resource *res)
1127bc69d124SMark A. Greer {
1128bc69d124SMark A. Greer 	struct device *dev = &pdev->dev;
1129bc69d124SMark A. Greer 	struct resource *r;
1130bc69d124SMark A. Greer 	int err = 0;
1131bc69d124SMark A. Greer 
1132bc69d124SMark A. Greer 	/* Get the base address */
1133bc69d124SMark A. Greer 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1134bc69d124SMark A. Greer 	if (!r) {
1135bc69d124SMark A. Greer 		dev_err(dev, "no MEM resource info\n");
1136bc69d124SMark A. Greer 		err = -ENODEV;
1137bc69d124SMark A. Greer 		goto err;
1138bc69d124SMark A. Greer 	}
1139bc69d124SMark A. Greer 	memcpy(res, r, sizeof(*res));
1140bc69d124SMark A. Greer 
1141bc69d124SMark A. Greer 	/* Get the DMA out channel */
1142bc69d124SMark A. Greer 	r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1143bc69d124SMark A. Greer 	if (!r) {
1144bc69d124SMark A. Greer 		dev_err(dev, "no DMA out resource info\n");
1145bc69d124SMark A. Greer 		err = -ENODEV;
1146bc69d124SMark A. Greer 		goto err;
1147bc69d124SMark A. Greer 	}
1148bc69d124SMark A. Greer 	dd->dma_out = r->start;
1149bc69d124SMark A. Greer 
1150bc69d124SMark A. Greer 	/* Get the DMA in channel */
1151bc69d124SMark A. Greer 	r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1152bc69d124SMark A. Greer 	if (!r) {
1153bc69d124SMark A. Greer 		dev_err(dev, "no DMA in resource info\n");
1154bc69d124SMark A. Greer 		err = -ENODEV;
1155bc69d124SMark A. Greer 		goto err;
1156bc69d124SMark A. Greer 	}
1157bc69d124SMark A. Greer 	dd->dma_in = r->start;
1158bc69d124SMark A. Greer 
11590d35583aSMark A. Greer 	/* Only OMAP2/3 can be non-DT */
11600d35583aSMark A. Greer 	dd->pdata = &omap_aes_pdata_omap2;
11610d35583aSMark A. Greer 
1162bc69d124SMark A. Greer err:
1163bc69d124SMark A. Greer 	return err;
1164bc69d124SMark A. Greer }
1165bc69d124SMark A. Greer 
1166537559a5SDmitry Kasatkin static int omap_aes_probe(struct platform_device *pdev)
1167537559a5SDmitry Kasatkin {
1168537559a5SDmitry Kasatkin 	struct device *dev = &pdev->dev;
1169537559a5SDmitry Kasatkin 	struct omap_aes_dev *dd;
1170f9fb69e7SMark A. Greer 	struct crypto_alg *algp;
1171bc69d124SMark A. Greer 	struct resource res;
11721801ad94SJoel Fernandes 	int err = -ENOMEM, i, j, irq = -1;
1173537559a5SDmitry Kasatkin 	u32 reg;
1174537559a5SDmitry Kasatkin 
117505007c10SJoel Fernandes 	dd = devm_kzalloc(dev, sizeof(struct omap_aes_dev), GFP_KERNEL);
1176537559a5SDmitry Kasatkin 	if (dd == NULL) {
1177537559a5SDmitry Kasatkin 		dev_err(dev, "unable to alloc data struct.\n");
1178537559a5SDmitry Kasatkin 		goto err_data;
1179537559a5SDmitry Kasatkin 	}
1180537559a5SDmitry Kasatkin 	dd->dev = dev;
1181537559a5SDmitry Kasatkin 	platform_set_drvdata(pdev, dd);
1182537559a5SDmitry Kasatkin 
1183537559a5SDmitry Kasatkin 	spin_lock_init(&dd->lock);
1184537559a5SDmitry Kasatkin 	crypto_init_queue(&dd->queue, OMAP_AES_QUEUE_LENGTH);
1185537559a5SDmitry Kasatkin 
1186bc69d124SMark A. Greer 	err = (dev->of_node) ? omap_aes_get_res_of(dd, dev, &res) :
1187bc69d124SMark A. Greer 			       omap_aes_get_res_pdev(dd, pdev, &res);
1188bc69d124SMark A. Greer 	if (err)
1189537559a5SDmitry Kasatkin 		goto err_res;
1190537559a5SDmitry Kasatkin 
119130862281SLaurent Navet 	dd->io_base = devm_ioremap_resource(dev, &res);
119230862281SLaurent Navet 	if (IS_ERR(dd->io_base)) {
119330862281SLaurent Navet 		err = PTR_ERR(dd->io_base);
11945946c4a5SMark A. Greer 		goto err_res;
1195537559a5SDmitry Kasatkin 	}
1196bc69d124SMark A. Greer 	dd->phys_base = res.start;
1197537559a5SDmitry Kasatkin 
11985946c4a5SMark A. Greer 	pm_runtime_enable(dev);
1199f7b2b5ddSNishanth Menon 	err = pm_runtime_get_sync(dev);
1200f7b2b5ddSNishanth Menon 	if (err < 0) {
1201f7b2b5ddSNishanth Menon 		dev_err(dev, "%s: failed to get_sync(%d)\n",
1202f7b2b5ddSNishanth Menon 			__func__, err);
1203f7b2b5ddSNishanth Menon 		goto err_res;
1204f7b2b5ddSNishanth Menon 	}
12055946c4a5SMark A. Greer 
12060d35583aSMark A. Greer 	omap_aes_dma_stop(dd);
12070d35583aSMark A. Greer 
12080d35583aSMark A. Greer 	reg = omap_aes_read(dd, AES_REG_REV(dd));
12095946c4a5SMark A. Greer 
12105946c4a5SMark A. Greer 	pm_runtime_put_sync(dev);
1211537559a5SDmitry Kasatkin 
12120d35583aSMark A. Greer 	dev_info(dev, "OMAP AES hw accel rev: %u.%u\n",
12130d35583aSMark A. Greer 		 (reg & dd->pdata->major_mask) >> dd->pdata->major_shift,
12140d35583aSMark A. Greer 		 (reg & dd->pdata->minor_mask) >> dd->pdata->minor_shift);
12150d35583aSMark A. Greer 
121621fe9767SDmitry Kasatkin 	tasklet_init(&dd->done_task, omap_aes_done_task, (unsigned long)dd);
121721fe9767SDmitry Kasatkin 	tasklet_init(&dd->queue_task, omap_aes_queue_task, (unsigned long)dd);
1218537559a5SDmitry Kasatkin 
1219537559a5SDmitry Kasatkin 	err = omap_aes_dma_init(dd);
12201801ad94SJoel Fernandes 	if (err && AES_REG_IRQ_STATUS(dd) && AES_REG_IRQ_ENABLE(dd)) {
12211801ad94SJoel Fernandes 		dd->pio_only = 1;
12221801ad94SJoel Fernandes 
12231801ad94SJoel Fernandes 		irq = platform_get_irq(pdev, 0);
12241801ad94SJoel Fernandes 		if (irq < 0) {
12251801ad94SJoel Fernandes 			dev_err(dev, "can't get IRQ resource\n");
12261801ad94SJoel Fernandes 			goto err_irq;
12271801ad94SJoel Fernandes 		}
12281801ad94SJoel Fernandes 
1229bce2a228SJoel Fernandes 		err = devm_request_irq(dev, irq, omap_aes_irq, 0,
12301801ad94SJoel Fernandes 				dev_name(dev), dd);
12311801ad94SJoel Fernandes 		if (err) {
12321801ad94SJoel Fernandes 			dev_err(dev, "Unable to grab omap-aes IRQ\n");
12331801ad94SJoel Fernandes 			goto err_irq;
12341801ad94SJoel Fernandes 		}
12351801ad94SJoel Fernandes 	}
12361801ad94SJoel Fernandes 
1237537559a5SDmitry Kasatkin 
1238537559a5SDmitry Kasatkin 	INIT_LIST_HEAD(&dd->list);
1239537559a5SDmitry Kasatkin 	spin_lock(&list_lock);
1240537559a5SDmitry Kasatkin 	list_add_tail(&dd->list, &dev_list);
1241537559a5SDmitry Kasatkin 	spin_unlock(&list_lock);
1242537559a5SDmitry Kasatkin 
1243f9fb69e7SMark A. Greer 	for (i = 0; i < dd->pdata->algs_info_size; i++) {
1244f9fb69e7SMark A. Greer 		for (j = 0; j < dd->pdata->algs_info[i].size; j++) {
1245f9fb69e7SMark A. Greer 			algp = &dd->pdata->algs_info[i].algs_list[j];
1246f9fb69e7SMark A. Greer 
1247f9fb69e7SMark A. Greer 			pr_debug("reg alg: %s\n", algp->cra_name);
1248f9fb69e7SMark A. Greer 			INIT_LIST_HEAD(&algp->cra_list);
1249f9fb69e7SMark A. Greer 
1250f9fb69e7SMark A. Greer 			err = crypto_register_alg(algp);
1251537559a5SDmitry Kasatkin 			if (err)
1252537559a5SDmitry Kasatkin 				goto err_algs;
1253f9fb69e7SMark A. Greer 
1254f9fb69e7SMark A. Greer 			dd->pdata->algs_info[i].registered++;
1255f9fb69e7SMark A. Greer 		}
1256537559a5SDmitry Kasatkin 	}
1257537559a5SDmitry Kasatkin 
1258537559a5SDmitry Kasatkin 	return 0;
1259537559a5SDmitry Kasatkin err_algs:
1260f9fb69e7SMark A. Greer 	for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1261f9fb69e7SMark A. Greer 		for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
1262f9fb69e7SMark A. Greer 			crypto_unregister_alg(
1263f9fb69e7SMark A. Greer 					&dd->pdata->algs_info[i].algs_list[j]);
1264bce2a228SJoel Fernandes 	if (!dd->pio_only)
1265537559a5SDmitry Kasatkin 		omap_aes_dma_cleanup(dd);
12661801ad94SJoel Fernandes err_irq:
126721fe9767SDmitry Kasatkin 	tasklet_kill(&dd->done_task);
126821fe9767SDmitry Kasatkin 	tasklet_kill(&dd->queue_task);
12695946c4a5SMark A. Greer 	pm_runtime_disable(dev);
1270537559a5SDmitry Kasatkin err_res:
1271537559a5SDmitry Kasatkin 	dd = NULL;
1272537559a5SDmitry Kasatkin err_data:
1273537559a5SDmitry Kasatkin 	dev_err(dev, "initialization failed.\n");
1274537559a5SDmitry Kasatkin 	return err;
1275537559a5SDmitry Kasatkin }
1276537559a5SDmitry Kasatkin 
1277537559a5SDmitry Kasatkin static int omap_aes_remove(struct platform_device *pdev)
1278537559a5SDmitry Kasatkin {
1279537559a5SDmitry Kasatkin 	struct omap_aes_dev *dd = platform_get_drvdata(pdev);
1280f9fb69e7SMark A. Greer 	int i, j;
1281537559a5SDmitry Kasatkin 
1282537559a5SDmitry Kasatkin 	if (!dd)
1283537559a5SDmitry Kasatkin 		return -ENODEV;
1284537559a5SDmitry Kasatkin 
1285537559a5SDmitry Kasatkin 	spin_lock(&list_lock);
1286537559a5SDmitry Kasatkin 	list_del(&dd->list);
1287537559a5SDmitry Kasatkin 	spin_unlock(&list_lock);
1288537559a5SDmitry Kasatkin 
1289f9fb69e7SMark A. Greer 	for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1290f9fb69e7SMark A. Greer 		for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
1291f9fb69e7SMark A. Greer 			crypto_unregister_alg(
1292f9fb69e7SMark A. Greer 					&dd->pdata->algs_info[i].algs_list[j]);
1293537559a5SDmitry Kasatkin 
129421fe9767SDmitry Kasatkin 	tasklet_kill(&dd->done_task);
129521fe9767SDmitry Kasatkin 	tasklet_kill(&dd->queue_task);
1296537559a5SDmitry Kasatkin 	omap_aes_dma_cleanup(dd);
12975946c4a5SMark A. Greer 	pm_runtime_disable(dd->dev);
1298537559a5SDmitry Kasatkin 	dd = NULL;
1299537559a5SDmitry Kasatkin 
1300537559a5SDmitry Kasatkin 	return 0;
1301537559a5SDmitry Kasatkin }
1302537559a5SDmitry Kasatkin 
13030635fb3aSMark A. Greer #ifdef CONFIG_PM_SLEEP
13040635fb3aSMark A. Greer static int omap_aes_suspend(struct device *dev)
13050635fb3aSMark A. Greer {
13060635fb3aSMark A. Greer 	pm_runtime_put_sync(dev);
13070635fb3aSMark A. Greer 	return 0;
13080635fb3aSMark A. Greer }
13090635fb3aSMark A. Greer 
13100635fb3aSMark A. Greer static int omap_aes_resume(struct device *dev)
13110635fb3aSMark A. Greer {
13120635fb3aSMark A. Greer 	pm_runtime_get_sync(dev);
13130635fb3aSMark A. Greer 	return 0;
13140635fb3aSMark A. Greer }
13150635fb3aSMark A. Greer #endif
13160635fb3aSMark A. Greer 
1317ea7b2843SJingoo Han static SIMPLE_DEV_PM_OPS(omap_aes_pm_ops, omap_aes_suspend, omap_aes_resume);
13180635fb3aSMark A. Greer 
1319537559a5SDmitry Kasatkin static struct platform_driver omap_aes_driver = {
1320537559a5SDmitry Kasatkin 	.probe	= omap_aes_probe,
1321537559a5SDmitry Kasatkin 	.remove	= omap_aes_remove,
1322537559a5SDmitry Kasatkin 	.driver	= {
1323537559a5SDmitry Kasatkin 		.name	= "omap-aes",
13240635fb3aSMark A. Greer 		.pm	= &omap_aes_pm_ops,
1325bc69d124SMark A. Greer 		.of_match_table	= omap_aes_of_match,
1326537559a5SDmitry Kasatkin 	},
1327537559a5SDmitry Kasatkin };
1328537559a5SDmitry Kasatkin 
132994e51df9SSachin Kamat module_platform_driver(omap_aes_driver);
1330537559a5SDmitry Kasatkin 
1331537559a5SDmitry Kasatkin MODULE_DESCRIPTION("OMAP AES hw acceleration support.");
1332537559a5SDmitry Kasatkin MODULE_LICENSE("GPL v2");
1333537559a5SDmitry Kasatkin MODULE_AUTHOR("Dmitry Kasatkin");
1334537559a5SDmitry Kasatkin 
1335