blob: 4ab53a604312668512ff196f03c64e84da153130 [file] [log] [blame]
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001/*
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>
Mark A. Greer0d355832013-01-08 11:57:46 -07008 * Copyright (c) 2011 Texas Instruments Incorporated
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08009 *
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
Joel Fernandes016af9b2013-08-18 00:56:11 -050016#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)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080019
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>
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080025#include <linux/platform_device.h>
26#include <linux/scatterlist.h>
27#include <linux/dma-mapping.h>
Mark A. Greerebedbf72013-01-08 11:57:42 -070028#include <linux/dmaengine.h>
Mark A. Greer5946c4a2013-01-08 11:57:40 -070029#include <linux/pm_runtime.h>
Mark A. Greerbc69d122013-01-08 11:57:44 -070030#include <linux/of.h>
31#include <linux/of_device.h>
32#include <linux/of_address.h>
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080033#include <linux/io.h>
34#include <linux/crypto.h>
35#include <linux/interrupt.h>
36#include <crypto/scatterwalk.h>
37#include <crypto/aes.h>
Baolin Wang05299002016-01-26 20:25:40 +080038#include <crypto/algapi.h>
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080039
Mark A. Greerebedbf72013-01-08 11:57:42 -070040#define DST_MAXBURST 4
41#define DMA_MIN (DST_MAXBURST * sizeof(u32))
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080042
Joel Fernandes1bf95cc2013-08-17 21:42:29 -050043#define _calc_walked(inout) (dd->inout##_walk.offset - dd->inout##_sg->offset)
44
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080045/* 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
Mark A. Greer0d355832013-01-08 11:57:46 -070050#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))
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080053
Mark A. Greer0d355832013-01-08 11:57:46 -070054#define AES_REG_CTRL(dd) ((dd)->pdata->ctrl_ofs)
Vutla, Lokesh340d9d32015-07-07 21:01:46 +053055#define AES_REG_CTRL_CTR_WIDTH_MASK GENMASK(8, 7)
56#define AES_REG_CTRL_CTR_WIDTH_32 0
57#define AES_REG_CTRL_CTR_WIDTH_64 BIT(7)
58#define AES_REG_CTRL_CTR_WIDTH_96 BIT(8)
59#define AES_REG_CTRL_CTR_WIDTH_128 GENMASK(8, 7)
60#define AES_REG_CTRL_CTR BIT(6)
61#define AES_REG_CTRL_CBC BIT(5)
62#define AES_REG_CTRL_KEY_SIZE GENMASK(4, 3)
63#define AES_REG_CTRL_DIRECTION BIT(2)
64#define AES_REG_CTRL_INPUT_READY BIT(1)
65#define AES_REG_CTRL_OUTPUT_READY BIT(0)
Vutla, Lokesh5396c6c2015-07-07 21:01:45 +053066#define AES_REG_CTRL_MASK GENMASK(24, 2)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080067
Mark A. Greer0d355832013-01-08 11:57:46 -070068#define AES_REG_DATA_N(dd, x) ((dd)->pdata->data_ofs + ((x) * 0x04))
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080069
Mark A. Greer0d355832013-01-08 11:57:46 -070070#define AES_REG_REV(dd) ((dd)->pdata->rev_ofs)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080071
Mark A. Greer0d355832013-01-08 11:57:46 -070072#define AES_REG_MASK(dd) ((dd)->pdata->mask_ofs)
Vutla, Lokesh340d9d32015-07-07 21:01:46 +053073#define AES_REG_MASK_SIDLE BIT(6)
74#define AES_REG_MASK_START BIT(5)
75#define AES_REG_MASK_DMA_OUT_EN BIT(3)
76#define AES_REG_MASK_DMA_IN_EN BIT(2)
77#define AES_REG_MASK_SOFTRESET BIT(1)
78#define AES_REG_AUTOIDLE BIT(0)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080079
Mark A. Greer0d355832013-01-08 11:57:46 -070080#define AES_REG_LENGTH_N(x) (0x54 + ((x) * 0x04))
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080081
Joel Fernandes67216752013-08-17 21:42:28 -050082#define AES_REG_IRQ_STATUS(dd) ((dd)->pdata->irq_status_ofs)
83#define AES_REG_IRQ_ENABLE(dd) ((dd)->pdata->irq_enable_ofs)
84#define AES_REG_IRQ_DATA_IN BIT(1)
85#define AES_REG_IRQ_DATA_OUT BIT(2)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080086#define DEFAULT_TIMEOUT (5*HZ)
87
88#define FLAGS_MODE_MASK 0x000f
89#define FLAGS_ENCRYPT BIT(0)
90#define FLAGS_CBC BIT(1)
91#define FLAGS_GIV BIT(2)
Mark A. Greerf9fb69e2013-01-08 11:57:47 -070092#define FLAGS_CTR BIT(3)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080093
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +020094#define FLAGS_INIT BIT(4)
95#define FLAGS_FAST BIT(5)
96#define FLAGS_BUSY BIT(6)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080097
Joel Fernandes1bf95cc2013-08-17 21:42:29 -050098#define AES_BLOCK_WORDS (AES_BLOCK_SIZE >> 2)
99
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800100struct omap_aes_ctx {
101 struct omap_aes_dev *dd;
102
103 int keylen;
104 u32 key[AES_KEYSIZE_256 / sizeof(u32)];
105 unsigned long flags;
106};
107
108struct omap_aes_reqctx {
109 unsigned long mode;
110};
111
112#define OMAP_AES_QUEUE_LENGTH 1
113#define OMAP_AES_CACHE_SIZE 0
114
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700115struct omap_aes_algs_info {
116 struct crypto_alg *algs_list;
117 unsigned int size;
118 unsigned int registered;
119};
120
Mark A. Greer0d355832013-01-08 11:57:46 -0700121struct omap_aes_pdata {
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700122 struct omap_aes_algs_info *algs_info;
123 unsigned int algs_info_size;
124
Mark A. Greer0d355832013-01-08 11:57:46 -0700125 void (*trigger)(struct omap_aes_dev *dd, int length);
126
127 u32 key_ofs;
128 u32 iv_ofs;
129 u32 ctrl_ofs;
130 u32 data_ofs;
131 u32 rev_ofs;
132 u32 mask_ofs;
Joel Fernandes67216752013-08-17 21:42:28 -0500133 u32 irq_enable_ofs;
134 u32 irq_status_ofs;
Mark A. Greer0d355832013-01-08 11:57:46 -0700135
136 u32 dma_enable_in;
137 u32 dma_enable_out;
138 u32 dma_start;
139
140 u32 major_mask;
141 u32 major_shift;
142 u32 minor_mask;
143 u32 minor_shift;
144};
145
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800146struct omap_aes_dev {
147 struct list_head list;
148 unsigned long phys_base;
Dmitry Kasatkinefce41b2010-11-30 10:13:32 +0200149 void __iomem *io_base;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800150 struct omap_aes_ctx *ctx;
151 struct device *dev;
152 unsigned long flags;
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200153 int err;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800154
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200155 struct tasklet_struct done_task;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800156
157 struct ablkcipher_request *req;
Baolin Wang05299002016-01-26 20:25:40 +0800158 struct crypto_engine *engine;
Joel Fernandes62423322013-08-17 21:42:32 -0500159
160 /*
161 * total is used by PIO mode for book keeping so introduce
162 * variable total_save as need it to calc page_order
163 */
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800164 size_t total;
Joel Fernandes62423322013-08-17 21:42:32 -0500165 size_t total_save;
166
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800167 struct scatterlist *in_sg;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800168 struct scatterlist *out_sg;
Joel Fernandes62423322013-08-17 21:42:32 -0500169
170 /* Buffers for copying for unaligned cases */
171 struct scatterlist in_sgl;
172 struct scatterlist out_sgl;
173 struct scatterlist *orig_out;
174 int sgs_copied;
175
Joel Fernandes1bf95cc2013-08-17 21:42:29 -0500176 struct scatter_walk in_walk;
177 struct scatter_walk out_walk;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700178 struct dma_chan *dma_lch_in;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700179 struct dma_chan *dma_lch_out;
Joel Fernandese77c7562013-08-17 21:42:24 -0500180 int in_sg_len;
181 int out_sg_len;
Joel Fernandes98837ab2013-08-17 21:42:30 -0500182 int pio_only;
Mark A. Greer0d355832013-01-08 11:57:46 -0700183 const struct omap_aes_pdata *pdata;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800184};
185
186/* keep registered devices data here */
187static LIST_HEAD(dev_list);
188static DEFINE_SPINLOCK(list_lock);
189
Joel Fernandes016af9b2013-08-18 00:56:11 -0500190#ifdef DEBUG
191#define omap_aes_read(dd, offset) \
192({ \
193 int _read_ret; \
194 _read_ret = __raw_readl(dd->io_base + offset); \
195 pr_debug("omap_aes_read(" #offset "=%#x)= %#x\n", \
196 offset, _read_ret); \
197 _read_ret; \
198})
199#else
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800200static inline u32 omap_aes_read(struct omap_aes_dev *dd, u32 offset)
201{
202 return __raw_readl(dd->io_base + offset);
203}
Joel Fernandes016af9b2013-08-18 00:56:11 -0500204#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800205
Joel Fernandes016af9b2013-08-18 00:56:11 -0500206#ifdef DEBUG
207#define omap_aes_write(dd, offset, value) \
208 do { \
209 pr_debug("omap_aes_write(" #offset "=%#x) value=%#x\n", \
210 offset, value); \
211 __raw_writel(value, dd->io_base + offset); \
212 } while (0)
213#else
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800214static inline void omap_aes_write(struct omap_aes_dev *dd, u32 offset,
215 u32 value)
216{
217 __raw_writel(value, dd->io_base + offset);
218}
Joel Fernandes016af9b2013-08-18 00:56:11 -0500219#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800220
221static inline void omap_aes_write_mask(struct omap_aes_dev *dd, u32 offset,
222 u32 value, u32 mask)
223{
224 u32 val;
225
226 val = omap_aes_read(dd, offset);
227 val &= ~mask;
228 val |= value;
229 omap_aes_write(dd, offset, val);
230}
231
232static void omap_aes_write_n(struct omap_aes_dev *dd, u32 offset,
233 u32 *value, int count)
234{
235 for (; count--; value++, offset += 4)
236 omap_aes_write(dd, offset, *value);
237}
238
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800239static int omap_aes_hw_init(struct omap_aes_dev *dd)
240{
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800241 if (!(dd->flags & FLAGS_INIT)) {
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200242 dd->flags |= FLAGS_INIT;
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200243 dd->err = 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800244 }
245
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200246 return 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800247}
248
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200249static int omap_aes_write_ctrl(struct omap_aes_dev *dd)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800250{
251 unsigned int key32;
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200252 int i, err;
Vutla, Lokesh5396c6c2015-07-07 21:01:45 +0530253 u32 val;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800254
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200255 err = omap_aes_hw_init(dd);
256 if (err)
257 return err;
258
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800259 key32 = dd->ctx->keylen / sizeof(u32);
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200260
261 /* it seems a key should always be set even if it has not changed */
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800262 for (i = 0; i < key32; i++) {
Mark A. Greer0d355832013-01-08 11:57:46 -0700263 omap_aes_write(dd, AES_REG_KEY(dd, i),
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800264 __le32_to_cpu(dd->ctx->key[i]));
265 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800266
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700267 if ((dd->flags & (FLAGS_CBC | FLAGS_CTR)) && dd->req->info)
Mark A. Greer0d355832013-01-08 11:57:46 -0700268 omap_aes_write_n(dd, AES_REG_IV(dd, 0), dd->req->info, 4);
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200269
270 val = FLD_VAL(((dd->ctx->keylen >> 3) - 1), 4, 3);
271 if (dd->flags & FLAGS_CBC)
272 val |= AES_REG_CTRL_CBC;
Vutla, Lokesh5396c6c2015-07-07 21:01:45 +0530273 if (dd->flags & FLAGS_CTR)
Joel Fernandes8ed49c72013-10-29 17:37:38 -0500274 val |= AES_REG_CTRL_CTR | AES_REG_CTRL_CTR_WIDTH_128;
Vutla, Lokesh5396c6c2015-07-07 21:01:45 +0530275
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200276 if (dd->flags & FLAGS_ENCRYPT)
277 val |= AES_REG_CTRL_DIRECTION;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800278
Vutla, Lokesh5396c6c2015-07-07 21:01:45 +0530279 omap_aes_write_mask(dd, AES_REG_CTRL(dd), val, AES_REG_CTRL_MASK);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800280
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200281 return 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800282}
283
Mark A. Greer0d355832013-01-08 11:57:46 -0700284static void omap_aes_dma_trigger_omap2(struct omap_aes_dev *dd, int length)
285{
286 u32 mask, val;
287
288 val = dd->pdata->dma_start;
289
290 if (dd->dma_lch_out != NULL)
291 val |= dd->pdata->dma_enable_out;
292 if (dd->dma_lch_in != NULL)
293 val |= dd->pdata->dma_enable_in;
294
295 mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
296 dd->pdata->dma_start;
297
298 omap_aes_write_mask(dd, AES_REG_MASK(dd), val, mask);
299
300}
301
302static void omap_aes_dma_trigger_omap4(struct omap_aes_dev *dd, int length)
303{
304 omap_aes_write(dd, AES_REG_LENGTH_N(0), length);
305 omap_aes_write(dd, AES_REG_LENGTH_N(1), 0);
306
307 omap_aes_dma_trigger_omap2(dd, length);
308}
309
310static void omap_aes_dma_stop(struct omap_aes_dev *dd)
311{
312 u32 mask;
313
314 mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
315 dd->pdata->dma_start;
316
317 omap_aes_write_mask(dd, AES_REG_MASK(dd), 0, mask);
318}
319
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800320static struct omap_aes_dev *omap_aes_find_dev(struct omap_aes_ctx *ctx)
321{
322 struct omap_aes_dev *dd = NULL, *tmp;
323
324 spin_lock_bh(&list_lock);
325 if (!ctx->dd) {
326 list_for_each_entry(tmp, &dev_list, list) {
327 /* FIXME: take fist available aes core */
328 dd = tmp;
329 break;
330 }
331 ctx->dd = dd;
332 } else {
333 /* already found before */
334 dd = ctx->dd;
335 }
336 spin_unlock_bh(&list_lock);
337
338 return dd;
339}
340
Mark A. Greerebedbf72013-01-08 11:57:42 -0700341static void omap_aes_dma_out_callback(void *data)
342{
343 struct omap_aes_dev *dd = data;
344
345 /* dma_lch_out - completed */
346 tasklet_schedule(&dd->done_task);
347}
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800348
349static int omap_aes_dma_init(struct omap_aes_dev *dd)
350{
Peter Ujfalusida8b29a2016-04-29 16:02:18 +0300351 int err;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800352
Mark A. Greerebedbf72013-01-08 11:57:42 -0700353 dd->dma_lch_out = NULL;
354 dd->dma_lch_in = NULL;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800355
Peter Ujfalusida8b29a2016-04-29 16:02:18 +0300356 dd->dma_lch_in = dma_request_chan(dd->dev, "rx");
357 if (IS_ERR(dd->dma_lch_in)) {
Mark A. Greerebedbf72013-01-08 11:57:42 -0700358 dev_err(dd->dev, "Unable to request in DMA channel\n");
Peter Ujfalusida8b29a2016-04-29 16:02:18 +0300359 return PTR_ERR(dd->dma_lch_in);
Mark A. Greerebedbf72013-01-08 11:57:42 -0700360 }
361
Peter Ujfalusida8b29a2016-04-29 16:02:18 +0300362 dd->dma_lch_out = dma_request_chan(dd->dev, "tx");
363 if (IS_ERR(dd->dma_lch_out)) {
Mark A. Greerebedbf72013-01-08 11:57:42 -0700364 dev_err(dd->dev, "Unable to request out DMA channel\n");
Peter Ujfalusida8b29a2016-04-29 16:02:18 +0300365 err = PTR_ERR(dd->dma_lch_out);
Mark A. Greerebedbf72013-01-08 11:57:42 -0700366 goto err_dma_out;
367 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800368
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800369 return 0;
370
371err_dma_out:
Mark A. Greerebedbf72013-01-08 11:57:42 -0700372 dma_release_channel(dd->dma_lch_in);
Peter Ujfalusida8b29a2016-04-29 16:02:18 +0300373
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800374 return err;
375}
376
377static void omap_aes_dma_cleanup(struct omap_aes_dev *dd)
378{
Peter Ujfalusida8b29a2016-04-29 16:02:18 +0300379 if (dd->pio_only)
380 return;
381
Mark A. Greerebedbf72013-01-08 11:57:42 -0700382 dma_release_channel(dd->dma_lch_out);
383 dma_release_channel(dd->dma_lch_in);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800384}
385
386static void sg_copy_buf(void *buf, struct scatterlist *sg,
387 unsigned int start, unsigned int nbytes, int out)
388{
389 struct scatter_walk walk;
390
391 if (!nbytes)
392 return;
393
394 scatterwalk_start(&walk, sg);
395 scatterwalk_advance(&walk, start);
396 scatterwalk_copychunks(buf, &walk, nbytes, out);
397 scatterwalk_done(&walk, out, 0);
398}
399
Mark A. Greerebedbf72013-01-08 11:57:42 -0700400static int omap_aes_crypt_dma(struct crypto_tfm *tfm,
Joel Fernandes4b645c92013-08-17 21:42:25 -0500401 struct scatterlist *in_sg, struct scatterlist *out_sg,
402 int in_sg_len, int out_sg_len)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800403{
404 struct omap_aes_ctx *ctx = crypto_tfm_ctx(tfm);
405 struct omap_aes_dev *dd = ctx->dd;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700406 struct dma_async_tx_descriptor *tx_in, *tx_out;
407 struct dma_slave_config cfg;
Joel Fernandes4b645c92013-08-17 21:42:25 -0500408 int ret;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800409
Joel Fernandes98837ab2013-08-17 21:42:30 -0500410 if (dd->pio_only) {
411 scatterwalk_start(&dd->in_walk, dd->in_sg);
412 scatterwalk_start(&dd->out_walk, dd->out_sg);
413
414 /* Enable DATAIN interrupt and let it take
415 care of the rest */
416 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x2);
417 return 0;
418 }
419
Joel Fernandes0a641712013-08-17 21:42:26 -0500420 dma_sync_sg_for_device(dd->dev, dd->in_sg, in_sg_len, DMA_TO_DEVICE);
421
Mark A. Greerebedbf72013-01-08 11:57:42 -0700422 memset(&cfg, 0, sizeof(cfg));
423
Mark A. Greer0d355832013-01-08 11:57:46 -0700424 cfg.src_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
425 cfg.dst_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
Mark A. Greerebedbf72013-01-08 11:57:42 -0700426 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
427 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
428 cfg.src_maxburst = DST_MAXBURST;
429 cfg.dst_maxburst = DST_MAXBURST;
430
431 /* IN */
432 ret = dmaengine_slave_config(dd->dma_lch_in, &cfg);
433 if (ret) {
434 dev_err(dd->dev, "can't configure IN dmaengine slave: %d\n",
435 ret);
436 return ret;
437 }
438
Joel Fernandes4b645c92013-08-17 21:42:25 -0500439 tx_in = dmaengine_prep_slave_sg(dd->dma_lch_in, in_sg, in_sg_len,
Mark A. Greerebedbf72013-01-08 11:57:42 -0700440 DMA_MEM_TO_DEV,
441 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
442 if (!tx_in) {
443 dev_err(dd->dev, "IN prep_slave_sg() failed\n");
444 return -EINVAL;
445 }
446
447 /* No callback necessary */
448 tx_in->callback_param = dd;
449
450 /* OUT */
451 ret = dmaengine_slave_config(dd->dma_lch_out, &cfg);
452 if (ret) {
453 dev_err(dd->dev, "can't configure OUT dmaengine slave: %d\n",
454 ret);
455 return ret;
456 }
457
Joel Fernandes4b645c92013-08-17 21:42:25 -0500458 tx_out = dmaengine_prep_slave_sg(dd->dma_lch_out, out_sg, out_sg_len,
Mark A. Greerebedbf72013-01-08 11:57:42 -0700459 DMA_DEV_TO_MEM,
460 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
461 if (!tx_out) {
462 dev_err(dd->dev, "OUT prep_slave_sg() failed\n");
463 return -EINVAL;
464 }
465
466 tx_out->callback = omap_aes_dma_out_callback;
467 tx_out->callback_param = dd;
468
469 dmaengine_submit(tx_in);
470 dmaengine_submit(tx_out);
471
472 dma_async_issue_pending(dd->dma_lch_in);
473 dma_async_issue_pending(dd->dma_lch_out);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800474
Mark A. Greer0d355832013-01-08 11:57:46 -0700475 /* start DMA */
Joel Fernandes4b645c92013-08-17 21:42:25 -0500476 dd->pdata->trigger(dd, dd->total);
Dmitry Kasatkin83ea7e02010-11-30 10:13:31 +0200477
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800478 return 0;
479}
480
481static int omap_aes_crypt_dma_start(struct omap_aes_dev *dd)
482{
483 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(
484 crypto_ablkcipher_reqtfm(dd->req));
Joel Fernandes4b645c92013-08-17 21:42:25 -0500485 int err;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800486
487 pr_debug("total: %d\n", dd->total);
488
Joel Fernandes98837ab2013-08-17 21:42:30 -0500489 if (!dd->pio_only) {
490 err = dma_map_sg(dd->dev, dd->in_sg, dd->in_sg_len,
491 DMA_TO_DEVICE);
492 if (!err) {
493 dev_err(dd->dev, "dma_map_sg() error\n");
494 return -EINVAL;
495 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800496
Joel Fernandes98837ab2013-08-17 21:42:30 -0500497 err = dma_map_sg(dd->dev, dd->out_sg, dd->out_sg_len,
498 DMA_FROM_DEVICE);
499 if (!err) {
500 dev_err(dd->dev, "dma_map_sg() error\n");
501 return -EINVAL;
502 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800503 }
504
Joel Fernandes4b645c92013-08-17 21:42:25 -0500505 err = omap_aes_crypt_dma(tfm, dd->in_sg, dd->out_sg, dd->in_sg_len,
506 dd->out_sg_len);
Joel Fernandes98837ab2013-08-17 21:42:30 -0500507 if (err && !dd->pio_only) {
Joel Fernandes4b645c92013-08-17 21:42:25 -0500508 dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
509 dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
510 DMA_FROM_DEVICE);
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200511 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800512
513 return err;
514}
515
516static void omap_aes_finish_req(struct omap_aes_dev *dd, int err)
517{
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200518 struct ablkcipher_request *req = dd->req;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800519
520 pr_debug("err: %d\n", err);
521
Baolin Wang05299002016-01-26 20:25:40 +0800522 crypto_finalize_request(dd->engine, req, err);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800523}
524
525static int omap_aes_crypt_dma_stop(struct omap_aes_dev *dd)
526{
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800527 pr_debug("total: %d\n", dd->total);
528
Mark A. Greer0d355832013-01-08 11:57:46 -0700529 omap_aes_dma_stop(dd);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800530
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800531
Rahul Pathak16f080a2015-12-14 08:45:23 +0000532 return 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800533}
534
Vutla, Lokesh6d7e7e02015-03-31 09:52:25 +0530535static int omap_aes_check_aligned(struct scatterlist *sg, int total)
Joel Fernandes62423322013-08-17 21:42:32 -0500536{
Vutla, Lokesh6d7e7e02015-03-31 09:52:25 +0530537 int len = 0;
538
Vutla, Lokesh310b0d52015-07-07 21:01:43 +0530539 if (!IS_ALIGNED(total, AES_BLOCK_SIZE))
540 return -EINVAL;
541
Joel Fernandes62423322013-08-17 21:42:32 -0500542 while (sg) {
543 if (!IS_ALIGNED(sg->offset, 4))
544 return -1;
545 if (!IS_ALIGNED(sg->length, AES_BLOCK_SIZE))
546 return -1;
Vutla, Lokesh6d7e7e02015-03-31 09:52:25 +0530547
548 len += sg->length;
Joel Fernandes62423322013-08-17 21:42:32 -0500549 sg = sg_next(sg);
550 }
Vutla, Lokesh6d7e7e02015-03-31 09:52:25 +0530551
552 if (len != total)
553 return -1;
554
Joel Fernandes62423322013-08-17 21:42:32 -0500555 return 0;
556}
557
Sachin Kamat034568e2013-09-13 17:06:46 +0530558static int omap_aes_copy_sgs(struct omap_aes_dev *dd)
Joel Fernandes62423322013-08-17 21:42:32 -0500559{
560 void *buf_in, *buf_out;
Vutla, Lokesh310b0d52015-07-07 21:01:43 +0530561 int pages, total;
Joel Fernandes62423322013-08-17 21:42:32 -0500562
Vutla, Lokesh310b0d52015-07-07 21:01:43 +0530563 total = ALIGN(dd->total, AES_BLOCK_SIZE);
564 pages = get_order(total);
Joel Fernandes62423322013-08-17 21:42:32 -0500565
566 buf_in = (void *)__get_free_pages(GFP_ATOMIC, pages);
567 buf_out = (void *)__get_free_pages(GFP_ATOMIC, pages);
568
569 if (!buf_in || !buf_out) {
570 pr_err("Couldn't allocated pages for unaligned cases.\n");
571 return -1;
572 }
573
574 dd->orig_out = dd->out_sg;
575
576 sg_copy_buf(buf_in, dd->in_sg, 0, dd->total, 0);
577
578 sg_init_table(&dd->in_sgl, 1);
Vutla, Lokesh310b0d52015-07-07 21:01:43 +0530579 sg_set_buf(&dd->in_sgl, buf_in, total);
Joel Fernandes62423322013-08-17 21:42:32 -0500580 dd->in_sg = &dd->in_sgl;
Herbert Xu7c001a82016-07-12 13:17:52 +0800581 dd->in_sg_len = 1;
Joel Fernandes62423322013-08-17 21:42:32 -0500582
583 sg_init_table(&dd->out_sgl, 1);
Vutla, Lokesh310b0d52015-07-07 21:01:43 +0530584 sg_set_buf(&dd->out_sgl, buf_out, total);
Joel Fernandes62423322013-08-17 21:42:32 -0500585 dd->out_sg = &dd->out_sgl;
Herbert Xu7c001a82016-07-12 13:17:52 +0800586 dd->out_sg_len = 1;
Joel Fernandes62423322013-08-17 21:42:32 -0500587
588 return 0;
589}
590
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200591static int omap_aes_handle_queue(struct omap_aes_dev *dd,
Baolin Wang05299002016-01-26 20:25:40 +0800592 struct ablkcipher_request *req)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800593{
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200594 if (req)
Baolin Wang05299002016-01-26 20:25:40 +0800595 return crypto_transfer_request_to_engine(dd->engine, req);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800596
Baolin Wang05299002016-01-26 20:25:40 +0800597 return 0;
598}
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800599
Baolin Wang05299002016-01-26 20:25:40 +0800600static int omap_aes_prepare_req(struct crypto_engine *engine,
601 struct ablkcipher_request *req)
602{
603 struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(
604 crypto_ablkcipher_reqtfm(req));
605 struct omap_aes_dev *dd = omap_aes_find_dev(ctx);
606 struct omap_aes_reqctx *rctx;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800607
Baolin Wang05299002016-01-26 20:25:40 +0800608 if (!dd)
609 return -ENODEV;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800610
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800611 /* assign new request to device */
612 dd->req = req;
613 dd->total = req->nbytes;
Joel Fernandes62423322013-08-17 21:42:32 -0500614 dd->total_save = req->nbytes;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800615 dd->in_sg = req->src;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800616 dd->out_sg = req->dst;
617
Herbert Xu7c001a82016-07-12 13:17:52 +0800618 dd->in_sg_len = sg_nents_for_len(dd->in_sg, dd->total);
619 if (dd->in_sg_len < 0)
620 return dd->in_sg_len;
621
622 dd->out_sg_len = sg_nents_for_len(dd->out_sg, dd->total);
623 if (dd->out_sg_len < 0)
624 return dd->out_sg_len;
625
Vutla, Lokesh6d7e7e02015-03-31 09:52:25 +0530626 if (omap_aes_check_aligned(dd->in_sg, dd->total) ||
627 omap_aes_check_aligned(dd->out_sg, dd->total)) {
Joel Fernandes62423322013-08-17 21:42:32 -0500628 if (omap_aes_copy_sgs(dd))
629 pr_err("Failed to copy SGs for unaligned cases\n");
630 dd->sgs_copied = 1;
631 } else {
632 dd->sgs_copied = 0;
633 }
634
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800635 rctx = ablkcipher_request_ctx(req);
636 ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
637 rctx->mode &= FLAGS_MODE_MASK;
638 dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode;
639
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200640 dd->ctx = ctx;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800641 ctx->dd = dd;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800642
Baolin Wang05299002016-01-26 20:25:40 +0800643 return omap_aes_write_ctrl(dd);
644}
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800645
Baolin Wang05299002016-01-26 20:25:40 +0800646static int omap_aes_crypt_req(struct crypto_engine *engine,
647 struct ablkcipher_request *req)
648{
649 struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(
650 crypto_ablkcipher_reqtfm(req));
651 struct omap_aes_dev *dd = omap_aes_find_dev(ctx);
652
653 if (!dd)
654 return -ENODEV;
655
656 return omap_aes_crypt_dma_start(dd);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800657}
658
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200659static void omap_aes_done_task(unsigned long data)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800660{
661 struct omap_aes_dev *dd = (struct omap_aes_dev *)data;
Joel Fernandes62423322013-08-17 21:42:32 -0500662 void *buf_in, *buf_out;
Vutla, Lokesh310b0d52015-07-07 21:01:43 +0530663 int pages, len;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800664
Joel Fernandes4b645c92013-08-17 21:42:25 -0500665 pr_debug("enter done_task\n");
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800666
Joel Fernandes98837ab2013-08-17 21:42:30 -0500667 if (!dd->pio_only) {
668 dma_sync_sg_for_device(dd->dev, dd->out_sg, dd->out_sg_len,
669 DMA_FROM_DEVICE);
Joel Fernandes62423322013-08-17 21:42:32 -0500670 dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
671 dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
672 DMA_FROM_DEVICE);
Joel Fernandes98837ab2013-08-17 21:42:30 -0500673 omap_aes_crypt_dma_stop(dd);
674 }
Joel Fernandes62423322013-08-17 21:42:32 -0500675
676 if (dd->sgs_copied) {
677 buf_in = sg_virt(&dd->in_sgl);
678 buf_out = sg_virt(&dd->out_sgl);
679
680 sg_copy_buf(buf_out, dd->orig_out, 0, dd->total_save, 1);
681
Vutla, Lokesh310b0d52015-07-07 21:01:43 +0530682 len = ALIGN(dd->total_save, AES_BLOCK_SIZE);
683 pages = get_order(len);
Joel Fernandes62423322013-08-17 21:42:32 -0500684 free_pages((unsigned long)buf_in, pages);
685 free_pages((unsigned long)buf_out, pages);
686 }
687
Joel Fernandes4b645c92013-08-17 21:42:25 -0500688 omap_aes_finish_req(dd, 0);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800689
690 pr_debug("exit\n");
691}
692
693static int omap_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
694{
695 struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(
696 crypto_ablkcipher_reqtfm(req));
697 struct omap_aes_reqctx *rctx = ablkcipher_request_ctx(req);
698 struct omap_aes_dev *dd;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800699
700 pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req->nbytes,
701 !!(mode & FLAGS_ENCRYPT),
702 !!(mode & FLAGS_CBC));
703
704 dd = omap_aes_find_dev(ctx);
705 if (!dd)
706 return -ENODEV;
707
708 rctx->mode = mode;
709
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200710 return omap_aes_handle_queue(dd, req);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800711}
712
713/* ********************** ALG API ************************************ */
714
715static int omap_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
716 unsigned int keylen)
717{
718 struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(tfm);
719
720 if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
721 keylen != AES_KEYSIZE_256)
722 return -EINVAL;
723
724 pr_debug("enter, keylen: %d\n", keylen);
725
726 memcpy(ctx->key, key, keylen);
727 ctx->keylen = keylen;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800728
729 return 0;
730}
731
732static int omap_aes_ecb_encrypt(struct ablkcipher_request *req)
733{
734 return omap_aes_crypt(req, FLAGS_ENCRYPT);
735}
736
737static int omap_aes_ecb_decrypt(struct ablkcipher_request *req)
738{
739 return omap_aes_crypt(req, 0);
740}
741
742static int omap_aes_cbc_encrypt(struct ablkcipher_request *req)
743{
744 return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC);
745}
746
747static int omap_aes_cbc_decrypt(struct ablkcipher_request *req)
748{
749 return omap_aes_crypt(req, FLAGS_CBC);
750}
751
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700752static int omap_aes_ctr_encrypt(struct ablkcipher_request *req)
753{
754 return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CTR);
755}
756
757static int omap_aes_ctr_decrypt(struct ablkcipher_request *req)
758{
759 return omap_aes_crypt(req, FLAGS_CTR);
760}
761
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800762static int omap_aes_cra_init(struct crypto_tfm *tfm)
763{
Joel A Fernandesa3485e62013-05-28 19:02:55 -0500764 struct omap_aes_dev *dd = NULL;
Nishanth Menonf7b2b5d2013-12-03 19:43:13 -0600765 int err;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800766
Joel A Fernandesa3485e62013-05-28 19:02:55 -0500767 /* Find AES device, currently picks the first device */
768 spin_lock_bh(&list_lock);
769 list_for_each_entry(dd, &dev_list, list) {
770 break;
771 }
772 spin_unlock_bh(&list_lock);
773
Nishanth Menonf7b2b5d2013-12-03 19:43:13 -0600774 err = pm_runtime_get_sync(dd->dev);
775 if (err < 0) {
776 dev_err(dd->dev, "%s: failed to get_sync(%d)\n",
777 __func__, err);
778 return err;
779 }
780
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800781 tfm->crt_ablkcipher.reqsize = sizeof(struct omap_aes_reqctx);
782
783 return 0;
784}
785
786static void omap_aes_cra_exit(struct crypto_tfm *tfm)
787{
Joel A Fernandesa3485e62013-05-28 19:02:55 -0500788 struct omap_aes_dev *dd = NULL;
789
790 /* Find AES device, currently picks the first device */
791 spin_lock_bh(&list_lock);
792 list_for_each_entry(dd, &dev_list, list) {
793 break;
794 }
795 spin_unlock_bh(&list_lock);
796
797 pm_runtime_put_sync(dd->dev);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800798}
799
800/* ********************** ALGS ************************************ */
801
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700802static struct crypto_alg algs_ecb_cbc[] = {
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800803{
804 .cra_name = "ecb(aes)",
805 .cra_driver_name = "ecb-aes-omap",
Vutla, Lokesh6e2e3d12015-07-07 21:01:44 +0530806 .cra_priority = 300,
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +0100807 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
808 CRYPTO_ALG_KERN_DRIVER_ONLY |
809 CRYPTO_ALG_ASYNC,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800810 .cra_blocksize = AES_BLOCK_SIZE,
811 .cra_ctxsize = sizeof(struct omap_aes_ctx),
Dmitry Kasatkinefce41b2010-11-30 10:13:32 +0200812 .cra_alignmask = 0,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800813 .cra_type = &crypto_ablkcipher_type,
814 .cra_module = THIS_MODULE,
815 .cra_init = omap_aes_cra_init,
816 .cra_exit = omap_aes_cra_exit,
817 .cra_u.ablkcipher = {
818 .min_keysize = AES_MIN_KEY_SIZE,
819 .max_keysize = AES_MAX_KEY_SIZE,
820 .setkey = omap_aes_setkey,
821 .encrypt = omap_aes_ecb_encrypt,
822 .decrypt = omap_aes_ecb_decrypt,
823 }
824},
825{
826 .cra_name = "cbc(aes)",
827 .cra_driver_name = "cbc-aes-omap",
Vutla, Lokesh6e2e3d12015-07-07 21:01:44 +0530828 .cra_priority = 300,
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +0100829 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
830 CRYPTO_ALG_KERN_DRIVER_ONLY |
831 CRYPTO_ALG_ASYNC,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800832 .cra_blocksize = AES_BLOCK_SIZE,
833 .cra_ctxsize = sizeof(struct omap_aes_ctx),
Dmitry Kasatkinefce41b2010-11-30 10:13:32 +0200834 .cra_alignmask = 0,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800835 .cra_type = &crypto_ablkcipher_type,
836 .cra_module = THIS_MODULE,
837 .cra_init = omap_aes_cra_init,
838 .cra_exit = omap_aes_cra_exit,
839 .cra_u.ablkcipher = {
840 .min_keysize = AES_MIN_KEY_SIZE,
841 .max_keysize = AES_MAX_KEY_SIZE,
842 .ivsize = AES_BLOCK_SIZE,
843 .setkey = omap_aes_setkey,
844 .encrypt = omap_aes_cbc_encrypt,
845 .decrypt = omap_aes_cbc_decrypt,
846 }
847}
848};
849
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700850static struct crypto_alg algs_ctr[] = {
851{
852 .cra_name = "ctr(aes)",
853 .cra_driver_name = "ctr-aes-omap",
Vutla, Lokesh6e2e3d12015-07-07 21:01:44 +0530854 .cra_priority = 300,
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700855 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
856 CRYPTO_ALG_KERN_DRIVER_ONLY |
857 CRYPTO_ALG_ASYNC,
858 .cra_blocksize = AES_BLOCK_SIZE,
859 .cra_ctxsize = sizeof(struct omap_aes_ctx),
860 .cra_alignmask = 0,
861 .cra_type = &crypto_ablkcipher_type,
862 .cra_module = THIS_MODULE,
863 .cra_init = omap_aes_cra_init,
864 .cra_exit = omap_aes_cra_exit,
865 .cra_u.ablkcipher = {
866 .min_keysize = AES_MIN_KEY_SIZE,
867 .max_keysize = AES_MAX_KEY_SIZE,
868 .geniv = "eseqiv",
869 .ivsize = AES_BLOCK_SIZE,
870 .setkey = omap_aes_setkey,
871 .encrypt = omap_aes_ctr_encrypt,
872 .decrypt = omap_aes_ctr_decrypt,
873 }
874} ,
875};
876
877static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc[] = {
878 {
879 .algs_list = algs_ecb_cbc,
880 .size = ARRAY_SIZE(algs_ecb_cbc),
881 },
882};
883
Mark A. Greer0d355832013-01-08 11:57:46 -0700884static const struct omap_aes_pdata omap_aes_pdata_omap2 = {
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700885 .algs_info = omap_aes_algs_info_ecb_cbc,
886 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc),
Mark A. Greer0d355832013-01-08 11:57:46 -0700887 .trigger = omap_aes_dma_trigger_omap2,
888 .key_ofs = 0x1c,
889 .iv_ofs = 0x20,
890 .ctrl_ofs = 0x30,
891 .data_ofs = 0x34,
892 .rev_ofs = 0x44,
893 .mask_ofs = 0x48,
894 .dma_enable_in = BIT(2),
895 .dma_enable_out = BIT(3),
896 .dma_start = BIT(5),
897 .major_mask = 0xf0,
898 .major_shift = 4,
899 .minor_mask = 0x0f,
900 .minor_shift = 0,
901};
902
Mark A. Greerbc69d122013-01-08 11:57:44 -0700903#ifdef CONFIG_OF
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700904static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc_ctr[] = {
905 {
906 .algs_list = algs_ecb_cbc,
907 .size = ARRAY_SIZE(algs_ecb_cbc),
908 },
909 {
910 .algs_list = algs_ctr,
911 .size = ARRAY_SIZE(algs_ctr),
912 },
913};
914
915static const struct omap_aes_pdata omap_aes_pdata_omap3 = {
916 .algs_info = omap_aes_algs_info_ecb_cbc_ctr,
917 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
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
Mark A. Greer0d355832013-01-08 11:57:46 -0700934static const struct omap_aes_pdata omap_aes_pdata_omap4 = {
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700935 .algs_info = omap_aes_algs_info_ecb_cbc_ctr,
936 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
Mark A. Greer0d355832013-01-08 11:57:46 -0700937 .trigger = omap_aes_dma_trigger_omap4,
938 .key_ofs = 0x3c,
939 .iv_ofs = 0x40,
940 .ctrl_ofs = 0x50,
941 .data_ofs = 0x60,
942 .rev_ofs = 0x80,
943 .mask_ofs = 0x84,
Joel Fernandes67216752013-08-17 21:42:28 -0500944 .irq_status_ofs = 0x8c,
945 .irq_enable_ofs = 0x90,
Mark A. Greer0d355832013-01-08 11:57:46 -0700946 .dma_enable_in = BIT(5),
947 .dma_enable_out = BIT(6),
948 .major_mask = 0x0700,
949 .major_shift = 8,
950 .minor_mask = 0x003f,
951 .minor_shift = 0,
952};
953
Joel Fernandes1bf95cc2013-08-17 21:42:29 -0500954static irqreturn_t omap_aes_irq(int irq, void *dev_id)
955{
956 struct omap_aes_dev *dd = dev_id;
957 u32 status, i;
958 u32 *src, *dst;
959
960 status = omap_aes_read(dd, AES_REG_IRQ_STATUS(dd));
961 if (status & AES_REG_IRQ_DATA_IN) {
962 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x0);
963
964 BUG_ON(!dd->in_sg);
965
966 BUG_ON(_calc_walked(in) > dd->in_sg->length);
967
968 src = sg_virt(dd->in_sg) + _calc_walked(in);
969
970 for (i = 0; i < AES_BLOCK_WORDS; i++) {
971 omap_aes_write(dd, AES_REG_DATA_N(dd, i), *src);
972
973 scatterwalk_advance(&dd->in_walk, 4);
974 if (dd->in_sg->length == _calc_walked(in)) {
Cristian Stoica5be4d4c2015-01-20 10:06:16 +0200975 dd->in_sg = sg_next(dd->in_sg);
Joel Fernandes1bf95cc2013-08-17 21:42:29 -0500976 if (dd->in_sg) {
977 scatterwalk_start(&dd->in_walk,
978 dd->in_sg);
979 src = sg_virt(dd->in_sg) +
980 _calc_walked(in);
981 }
982 } else {
983 src++;
984 }
985 }
986
987 /* Clear IRQ status */
988 status &= ~AES_REG_IRQ_DATA_IN;
989 omap_aes_write(dd, AES_REG_IRQ_STATUS(dd), status);
990
991 /* Enable DATA_OUT interrupt */
992 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x4);
993
994 } else if (status & AES_REG_IRQ_DATA_OUT) {
995 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x0);
996
997 BUG_ON(!dd->out_sg);
998
999 BUG_ON(_calc_walked(out) > dd->out_sg->length);
1000
1001 dst = sg_virt(dd->out_sg) + _calc_walked(out);
1002
1003 for (i = 0; i < AES_BLOCK_WORDS; i++) {
1004 *dst = omap_aes_read(dd, AES_REG_DATA_N(dd, i));
1005 scatterwalk_advance(&dd->out_walk, 4);
1006 if (dd->out_sg->length == _calc_walked(out)) {
Cristian Stoica5be4d4c2015-01-20 10:06:16 +02001007 dd->out_sg = sg_next(dd->out_sg);
Joel Fernandes1bf95cc2013-08-17 21:42:29 -05001008 if (dd->out_sg) {
1009 scatterwalk_start(&dd->out_walk,
1010 dd->out_sg);
1011 dst = sg_virt(dd->out_sg) +
1012 _calc_walked(out);
1013 }
1014 } else {
1015 dst++;
1016 }
1017 }
1018
Vutla, Lokesh310b0d52015-07-07 21:01:43 +05301019 dd->total -= min_t(size_t, AES_BLOCK_SIZE, dd->total);
Joel Fernandes1bf95cc2013-08-17 21:42:29 -05001020
1021 /* Clear IRQ status */
1022 status &= ~AES_REG_IRQ_DATA_OUT;
1023 omap_aes_write(dd, AES_REG_IRQ_STATUS(dd), status);
1024
1025 if (!dd->total)
1026 /* All bytes read! */
1027 tasklet_schedule(&dd->done_task);
1028 else
1029 /* Enable DATA_IN interrupt for next block */
1030 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x2);
1031 }
1032
1033 return IRQ_HANDLED;
1034}
1035
Mark A. Greerbc69d122013-01-08 11:57:44 -07001036static const struct of_device_id omap_aes_of_match[] = {
1037 {
1038 .compatible = "ti,omap2-aes",
Mark A. Greer0d355832013-01-08 11:57:46 -07001039 .data = &omap_aes_pdata_omap2,
1040 },
1041 {
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001042 .compatible = "ti,omap3-aes",
1043 .data = &omap_aes_pdata_omap3,
1044 },
1045 {
Mark A. Greer0d355832013-01-08 11:57:46 -07001046 .compatible = "ti,omap4-aes",
1047 .data = &omap_aes_pdata_omap4,
Mark A. Greerbc69d122013-01-08 11:57:44 -07001048 },
1049 {},
1050};
1051MODULE_DEVICE_TABLE(of, omap_aes_of_match);
1052
1053static int omap_aes_get_res_of(struct omap_aes_dev *dd,
1054 struct device *dev, struct resource *res)
1055{
1056 struct device_node *node = dev->of_node;
1057 const struct of_device_id *match;
1058 int err = 0;
1059
1060 match = of_match_device(of_match_ptr(omap_aes_of_match), dev);
1061 if (!match) {
1062 dev_err(dev, "no compatible OF match\n");
1063 err = -EINVAL;
1064 goto err;
1065 }
1066
1067 err = of_address_to_resource(node, 0, res);
1068 if (err < 0) {
1069 dev_err(dev, "can't translate OF node address\n");
1070 err = -EINVAL;
1071 goto err;
1072 }
1073
Mark A. Greer0d355832013-01-08 11:57:46 -07001074 dd->pdata = match->data;
1075
Mark A. Greerbc69d122013-01-08 11:57:44 -07001076err:
1077 return err;
1078}
1079#else
1080static const struct of_device_id omap_aes_of_match[] = {
1081 {},
1082};
1083
1084static int omap_aes_get_res_of(struct omap_aes_dev *dd,
1085 struct device *dev, struct resource *res)
1086{
1087 return -EINVAL;
1088}
1089#endif
1090
1091static int omap_aes_get_res_pdev(struct omap_aes_dev *dd,
1092 struct platform_device *pdev, struct resource *res)
1093{
1094 struct device *dev = &pdev->dev;
1095 struct resource *r;
1096 int err = 0;
1097
1098 /* Get the base address */
1099 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1100 if (!r) {
1101 dev_err(dev, "no MEM resource info\n");
1102 err = -ENODEV;
1103 goto err;
1104 }
1105 memcpy(res, r, sizeof(*res));
1106
Mark A. Greer0d355832013-01-08 11:57:46 -07001107 /* Only OMAP2/3 can be non-DT */
1108 dd->pdata = &omap_aes_pdata_omap2;
1109
Mark A. Greerbc69d122013-01-08 11:57:44 -07001110err:
1111 return err;
1112}
1113
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001114static int omap_aes_probe(struct platform_device *pdev)
1115{
1116 struct device *dev = &pdev->dev;
1117 struct omap_aes_dev *dd;
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001118 struct crypto_alg *algp;
Mark A. Greerbc69d122013-01-08 11:57:44 -07001119 struct resource res;
Joel Fernandes1801ad92013-08-17 21:42:31 -05001120 int err = -ENOMEM, i, j, irq = -1;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001121 u32 reg;
1122
Joel Fernandes05007c12013-08-17 21:42:33 -05001123 dd = devm_kzalloc(dev, sizeof(struct omap_aes_dev), GFP_KERNEL);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001124 if (dd == NULL) {
1125 dev_err(dev, "unable to alloc data struct.\n");
1126 goto err_data;
1127 }
1128 dd->dev = dev;
1129 platform_set_drvdata(pdev, dd);
1130
Mark A. Greerbc69d122013-01-08 11:57:44 -07001131 err = (dev->of_node) ? omap_aes_get_res_of(dd, dev, &res) :
1132 omap_aes_get_res_pdev(dd, pdev, &res);
1133 if (err)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001134 goto err_res;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001135
Laurent Navet30862282013-05-02 14:00:38 +02001136 dd->io_base = devm_ioremap_resource(dev, &res);
1137 if (IS_ERR(dd->io_base)) {
1138 err = PTR_ERR(dd->io_base);
Mark A. Greer5946c4a2013-01-08 11:57:40 -07001139 goto err_res;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001140 }
Mark A. Greerbc69d122013-01-08 11:57:44 -07001141 dd->phys_base = res.start;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001142
Mark A. Greer5946c4a2013-01-08 11:57:40 -07001143 pm_runtime_enable(dev);
Nishanth Menonf7b2b5d2013-12-03 19:43:13 -06001144 err = pm_runtime_get_sync(dev);
1145 if (err < 0) {
1146 dev_err(dev, "%s: failed to get_sync(%d)\n",
1147 __func__, err);
1148 goto err_res;
1149 }
Mark A. Greer5946c4a2013-01-08 11:57:40 -07001150
Mark A. Greer0d355832013-01-08 11:57:46 -07001151 omap_aes_dma_stop(dd);
1152
1153 reg = omap_aes_read(dd, AES_REG_REV(dd));
Mark A. Greer5946c4a2013-01-08 11:57:40 -07001154
1155 pm_runtime_put_sync(dev);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001156
Mark A. Greer0d355832013-01-08 11:57:46 -07001157 dev_info(dev, "OMAP AES hw accel rev: %u.%u\n",
1158 (reg & dd->pdata->major_mask) >> dd->pdata->major_shift,
1159 (reg & dd->pdata->minor_mask) >> dd->pdata->minor_shift);
1160
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +02001161 tasklet_init(&dd->done_task, omap_aes_done_task, (unsigned long)dd);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001162
1163 err = omap_aes_dma_init(dd);
Peter Ujfalusida8b29a2016-04-29 16:02:18 +03001164 if (err == -EPROBE_DEFER) {
1165 goto err_irq;
1166 } else if (err && AES_REG_IRQ_STATUS(dd) && AES_REG_IRQ_ENABLE(dd)) {
Joel Fernandes1801ad92013-08-17 21:42:31 -05001167 dd->pio_only = 1;
1168
1169 irq = platform_get_irq(pdev, 0);
1170 if (irq < 0) {
1171 dev_err(dev, "can't get IRQ resource\n");
1172 goto err_irq;
1173 }
1174
Joel Fernandesbce2a222013-08-17 21:42:34 -05001175 err = devm_request_irq(dev, irq, omap_aes_irq, 0,
Joel Fernandes1801ad92013-08-17 21:42:31 -05001176 dev_name(dev), dd);
1177 if (err) {
1178 dev_err(dev, "Unable to grab omap-aes IRQ\n");
1179 goto err_irq;
1180 }
1181 }
1182
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001183
1184 INIT_LIST_HEAD(&dd->list);
1185 spin_lock(&list_lock);
1186 list_add_tail(&dd->list, &dev_list);
1187 spin_unlock(&list_lock);
1188
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001189 for (i = 0; i < dd->pdata->algs_info_size; i++) {
Lokesh Vutla3741bbb2016-06-01 11:56:02 +03001190 if (!dd->pdata->algs_info[i].registered) {
1191 for (j = 0; j < dd->pdata->algs_info[i].size; j++) {
1192 algp = &dd->pdata->algs_info[i].algs_list[j];
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001193
Lokesh Vutla3741bbb2016-06-01 11:56:02 +03001194 pr_debug("reg alg: %s\n", algp->cra_name);
1195 INIT_LIST_HEAD(&algp->cra_list);
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001196
Lokesh Vutla3741bbb2016-06-01 11:56:02 +03001197 err = crypto_register_alg(algp);
1198 if (err)
1199 goto err_algs;
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001200
Lokesh Vutla3741bbb2016-06-01 11:56:02 +03001201 dd->pdata->algs_info[i].registered++;
1202 }
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001203 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001204 }
1205
Baolin Wang05299002016-01-26 20:25:40 +08001206 /* Initialize crypto engine */
1207 dd->engine = crypto_engine_alloc_init(dev, 1);
1208 if (!dd->engine)
1209 goto err_algs;
1210
1211 dd->engine->prepare_request = omap_aes_prepare_req;
1212 dd->engine->crypt_one_request = omap_aes_crypt_req;
1213 err = crypto_engine_start(dd->engine);
1214 if (err)
1215 goto err_engine;
1216
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001217 return 0;
Baolin Wang05299002016-01-26 20:25:40 +08001218err_engine:
1219 crypto_engine_exit(dd->engine);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001220err_algs:
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001221 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1222 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
1223 crypto_unregister_alg(
1224 &dd->pdata->algs_info[i].algs_list[j]);
Peter Ujfalusida8b29a2016-04-29 16:02:18 +03001225
1226 omap_aes_dma_cleanup(dd);
Joel Fernandes1801ad92013-08-17 21:42:31 -05001227err_irq:
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +02001228 tasklet_kill(&dd->done_task);
Mark A. Greer5946c4a2013-01-08 11:57:40 -07001229 pm_runtime_disable(dev);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001230err_res:
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001231 dd = NULL;
1232err_data:
1233 dev_err(dev, "initialization failed.\n");
1234 return err;
1235}
1236
1237static int omap_aes_remove(struct platform_device *pdev)
1238{
1239 struct omap_aes_dev *dd = platform_get_drvdata(pdev);
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001240 int i, j;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001241
1242 if (!dd)
1243 return -ENODEV;
1244
1245 spin_lock(&list_lock);
1246 list_del(&dd->list);
1247 spin_unlock(&list_lock);
1248
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001249 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1250 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
1251 crypto_unregister_alg(
1252 &dd->pdata->algs_info[i].algs_list[j]);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001253
Baolin Wang05299002016-01-26 20:25:40 +08001254 crypto_engine_exit(dd->engine);
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +02001255 tasklet_kill(&dd->done_task);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001256 omap_aes_dma_cleanup(dd);
Mark A. Greer5946c4a2013-01-08 11:57:40 -07001257 pm_runtime_disable(dd->dev);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001258 dd = NULL;
1259
1260 return 0;
1261}
1262
Mark A. Greer0635fb32013-01-08 11:57:41 -07001263#ifdef CONFIG_PM_SLEEP
1264static int omap_aes_suspend(struct device *dev)
1265{
1266 pm_runtime_put_sync(dev);
1267 return 0;
1268}
1269
1270static int omap_aes_resume(struct device *dev)
1271{
1272 pm_runtime_get_sync(dev);
1273 return 0;
1274}
1275#endif
1276
Jingoo Hanea7b2842014-02-27 20:31:38 +09001277static SIMPLE_DEV_PM_OPS(omap_aes_pm_ops, omap_aes_suspend, omap_aes_resume);
Mark A. Greer0635fb32013-01-08 11:57:41 -07001278
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001279static struct platform_driver omap_aes_driver = {
1280 .probe = omap_aes_probe,
1281 .remove = omap_aes_remove,
1282 .driver = {
1283 .name = "omap-aes",
Mark A. Greer0635fb32013-01-08 11:57:41 -07001284 .pm = &omap_aes_pm_ops,
Mark A. Greerbc69d122013-01-08 11:57:44 -07001285 .of_match_table = omap_aes_of_match,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001286 },
1287};
1288
Sachin Kamat94e51df2013-03-04 15:09:42 +05301289module_platform_driver(omap_aes_driver);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001290
1291MODULE_DESCRIPTION("OMAP AES hw acceleration support.");
1292MODULE_LICENSE("GPL v2");
1293MODULE_AUTHOR("Dmitry Kasatkin");
1294