blob: e60d2973321dc19cdf5bee2e0a951a10b18b69b2 [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>
Corentin LABBE2589ad82016-08-31 14:02:57 +020038#include <crypto/engine.h>
Lokesh Vutla9fcb1912016-08-04 13:28:44 +030039#include <crypto/internal/skcipher.h>
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080040
Tero Kristoafc2dc12017-05-24 10:35:28 +030041#include "omap-crypto.h"
42
Mark A. Greerebedbf72013-01-08 11:57:42 -070043#define DST_MAXBURST 4
44#define DMA_MIN (DST_MAXBURST * sizeof(u32))
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080045
Joel Fernandes1bf95cc2013-08-17 21:42:29 -050046#define _calc_walked(inout) (dd->inout##_walk.offset - dd->inout##_sg->offset)
47
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080048/* OMAP TRM gives bitfields as start:end, where start is the higher bit
49 number. For example 7:0 */
50#define FLD_MASK(start, end) (((1 << ((start) - (end) + 1)) - 1) << (end))
51#define FLD_VAL(val, start, end) (((val) << (end)) & FLD_MASK(start, end))
52
Mark A. Greer0d355832013-01-08 11:57:46 -070053#define AES_REG_KEY(dd, x) ((dd)->pdata->key_ofs - \
54 ((x ^ 0x01) * 0x04))
55#define AES_REG_IV(dd, x) ((dd)->pdata->iv_ofs + ((x) * 0x04))
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080056
Mark A. Greer0d355832013-01-08 11:57:46 -070057#define AES_REG_CTRL(dd) ((dd)->pdata->ctrl_ofs)
Vutla, Lokesh340d9d32015-07-07 21:01:46 +053058#define AES_REG_CTRL_CTR_WIDTH_MASK GENMASK(8, 7)
59#define AES_REG_CTRL_CTR_WIDTH_32 0
60#define AES_REG_CTRL_CTR_WIDTH_64 BIT(7)
61#define AES_REG_CTRL_CTR_WIDTH_96 BIT(8)
62#define AES_REG_CTRL_CTR_WIDTH_128 GENMASK(8, 7)
63#define AES_REG_CTRL_CTR BIT(6)
64#define AES_REG_CTRL_CBC BIT(5)
65#define AES_REG_CTRL_KEY_SIZE GENMASK(4, 3)
66#define AES_REG_CTRL_DIRECTION BIT(2)
67#define AES_REG_CTRL_INPUT_READY BIT(1)
68#define AES_REG_CTRL_OUTPUT_READY BIT(0)
Vutla, Lokesh5396c6c2015-07-07 21:01:45 +053069#define AES_REG_CTRL_MASK GENMASK(24, 2)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080070
Mark A. Greer0d355832013-01-08 11:57:46 -070071#define AES_REG_DATA_N(dd, x) ((dd)->pdata->data_ofs + ((x) * 0x04))
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080072
Mark A. Greer0d355832013-01-08 11:57:46 -070073#define AES_REG_REV(dd) ((dd)->pdata->rev_ofs)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080074
Mark A. Greer0d355832013-01-08 11:57:46 -070075#define AES_REG_MASK(dd) ((dd)->pdata->mask_ofs)
Vutla, Lokesh340d9d32015-07-07 21:01:46 +053076#define AES_REG_MASK_SIDLE BIT(6)
77#define AES_REG_MASK_START BIT(5)
78#define AES_REG_MASK_DMA_OUT_EN BIT(3)
79#define AES_REG_MASK_DMA_IN_EN BIT(2)
80#define AES_REG_MASK_SOFTRESET BIT(1)
81#define AES_REG_AUTOIDLE BIT(0)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080082
Mark A. Greer0d355832013-01-08 11:57:46 -070083#define AES_REG_LENGTH_N(x) (0x54 + ((x) * 0x04))
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080084
Joel Fernandes67216752013-08-17 21:42:28 -050085#define AES_REG_IRQ_STATUS(dd) ((dd)->pdata->irq_status_ofs)
86#define AES_REG_IRQ_ENABLE(dd) ((dd)->pdata->irq_enable_ofs)
87#define AES_REG_IRQ_DATA_IN BIT(1)
88#define AES_REG_IRQ_DATA_OUT BIT(2)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080089#define DEFAULT_TIMEOUT (5*HZ)
90
Tero Kristof303b452016-08-04 13:28:42 +030091#define DEFAULT_AUTOSUSPEND_DELAY 1000
92
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080093#define FLAGS_MODE_MASK 0x000f
94#define FLAGS_ENCRYPT BIT(0)
95#define FLAGS_CBC BIT(1)
96#define FLAGS_GIV BIT(2)
Mark A. Greerf9fb69e2013-01-08 11:57:47 -070097#define FLAGS_CTR BIT(3)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +080098
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +020099#define FLAGS_INIT BIT(4)
100#define FLAGS_FAST BIT(5)
101#define FLAGS_BUSY BIT(6)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800102
Tero Kristoafc2dc12017-05-24 10:35:28 +0300103#define FLAGS_IN_DATA_ST_SHIFT 8
104#define FLAGS_OUT_DATA_ST_SHIFT 10
105
Joel Fernandes1bf95cc2013-08-17 21:42:29 -0500106#define AES_BLOCK_WORDS (AES_BLOCK_SIZE >> 2)
107
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800108struct omap_aes_ctx {
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800109 int keylen;
110 u32 key[AES_KEYSIZE_256 / sizeof(u32)];
Lokesh Vutla9fcb1912016-08-04 13:28:44 +0300111 struct crypto_skcipher *fallback;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800112};
113
114struct omap_aes_reqctx {
Tero Kristo619ce702017-05-24 10:35:23 +0300115 struct omap_aes_dev *dd;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800116 unsigned long mode;
117};
118
119#define OMAP_AES_QUEUE_LENGTH 1
120#define OMAP_AES_CACHE_SIZE 0
121
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700122struct omap_aes_algs_info {
123 struct crypto_alg *algs_list;
124 unsigned int size;
125 unsigned int registered;
126};
127
Mark A. Greer0d355832013-01-08 11:57:46 -0700128struct omap_aes_pdata {
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700129 struct omap_aes_algs_info *algs_info;
130 unsigned int algs_info_size;
131
Mark A. Greer0d355832013-01-08 11:57:46 -0700132 void (*trigger)(struct omap_aes_dev *dd, int length);
133
134 u32 key_ofs;
135 u32 iv_ofs;
136 u32 ctrl_ofs;
137 u32 data_ofs;
138 u32 rev_ofs;
139 u32 mask_ofs;
Joel Fernandes67216752013-08-17 21:42:28 -0500140 u32 irq_enable_ofs;
141 u32 irq_status_ofs;
Mark A. Greer0d355832013-01-08 11:57:46 -0700142
143 u32 dma_enable_in;
144 u32 dma_enable_out;
145 u32 dma_start;
146
147 u32 major_mask;
148 u32 major_shift;
149 u32 minor_mask;
150 u32 minor_shift;
151};
152
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800153struct omap_aes_dev {
154 struct list_head list;
155 unsigned long phys_base;
Dmitry Kasatkinefce41b2010-11-30 10:13:32 +0200156 void __iomem *io_base;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800157 struct omap_aes_ctx *ctx;
158 struct device *dev;
159 unsigned long flags;
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200160 int err;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800161
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200162 struct tasklet_struct done_task;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800163
164 struct ablkcipher_request *req;
Baolin Wang05299002016-01-26 20:25:40 +0800165 struct crypto_engine *engine;
Joel Fernandes62423322013-08-17 21:42:32 -0500166
167 /*
168 * total is used by PIO mode for book keeping so introduce
169 * variable total_save as need it to calc page_order
170 */
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800171 size_t total;
Joel Fernandes62423322013-08-17 21:42:32 -0500172 size_t total_save;
173
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800174 struct scatterlist *in_sg;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800175 struct scatterlist *out_sg;
Joel Fernandes62423322013-08-17 21:42:32 -0500176
177 /* Buffers for copying for unaligned cases */
178 struct scatterlist in_sgl;
179 struct scatterlist out_sgl;
180 struct scatterlist *orig_out;
Joel Fernandes62423322013-08-17 21:42:32 -0500181
Joel Fernandes1bf95cc2013-08-17 21:42:29 -0500182 struct scatter_walk in_walk;
183 struct scatter_walk out_walk;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700184 struct dma_chan *dma_lch_in;
Mark A. Greerebedbf72013-01-08 11:57:42 -0700185 struct dma_chan *dma_lch_out;
Joel Fernandese77c7562013-08-17 21:42:24 -0500186 int in_sg_len;
187 int out_sg_len;
Joel Fernandes98837ab2013-08-17 21:42:30 -0500188 int pio_only;
Mark A. Greer0d355832013-01-08 11:57:46 -0700189 const struct omap_aes_pdata *pdata;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800190};
191
192/* keep registered devices data here */
193static LIST_HEAD(dev_list);
194static DEFINE_SPINLOCK(list_lock);
195
Joel Fernandes016af9b2013-08-18 00:56:11 -0500196#ifdef DEBUG
197#define omap_aes_read(dd, offset) \
198({ \
199 int _read_ret; \
200 _read_ret = __raw_readl(dd->io_base + offset); \
201 pr_debug("omap_aes_read(" #offset "=%#x)= %#x\n", \
202 offset, _read_ret); \
203 _read_ret; \
204})
205#else
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800206static inline u32 omap_aes_read(struct omap_aes_dev *dd, u32 offset)
207{
208 return __raw_readl(dd->io_base + offset);
209}
Joel Fernandes016af9b2013-08-18 00:56:11 -0500210#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800211
Joel Fernandes016af9b2013-08-18 00:56:11 -0500212#ifdef DEBUG
213#define omap_aes_write(dd, offset, value) \
214 do { \
215 pr_debug("omap_aes_write(" #offset "=%#x) value=%#x\n", \
216 offset, value); \
217 __raw_writel(value, dd->io_base + offset); \
218 } while (0)
219#else
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800220static inline void omap_aes_write(struct omap_aes_dev *dd, u32 offset,
221 u32 value)
222{
223 __raw_writel(value, dd->io_base + offset);
224}
Joel Fernandes016af9b2013-08-18 00:56:11 -0500225#endif
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800226
227static inline void omap_aes_write_mask(struct omap_aes_dev *dd, u32 offset,
228 u32 value, u32 mask)
229{
230 u32 val;
231
232 val = omap_aes_read(dd, offset);
233 val &= ~mask;
234 val |= value;
235 omap_aes_write(dd, offset, val);
236}
237
238static void omap_aes_write_n(struct omap_aes_dev *dd, u32 offset,
239 u32 *value, int count)
240{
241 for (; count--; value++, offset += 4)
242 omap_aes_write(dd, offset, *value);
243}
244
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800245static int omap_aes_hw_init(struct omap_aes_dev *dd)
246{
Tero Kristof303b452016-08-04 13:28:42 +0300247 int err;
248
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800249 if (!(dd->flags & FLAGS_INIT)) {
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200250 dd->flags |= FLAGS_INIT;
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200251 dd->err = 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800252 }
253
Tero Kristof303b452016-08-04 13:28:42 +0300254 err = pm_runtime_get_sync(dd->dev);
255 if (err < 0) {
256 dev_err(dd->dev, "failed to get sync: %d\n", err);
257 return err;
258 }
259
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200260 return 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800261}
262
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200263static int omap_aes_write_ctrl(struct omap_aes_dev *dd)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800264{
265 unsigned int key32;
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200266 int i, err;
Vutla, Lokesh5396c6c2015-07-07 21:01:45 +0530267 u32 val;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800268
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200269 err = omap_aes_hw_init(dd);
270 if (err)
271 return err;
272
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800273 key32 = dd->ctx->keylen / sizeof(u32);
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200274
275 /* it seems a key should always be set even if it has not changed */
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800276 for (i = 0; i < key32; i++) {
Mark A. Greer0d355832013-01-08 11:57:46 -0700277 omap_aes_write(dd, AES_REG_KEY(dd, i),
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800278 __le32_to_cpu(dd->ctx->key[i]));
279 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800280
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700281 if ((dd->flags & (FLAGS_CBC | FLAGS_CTR)) && dd->req->info)
Mark A. Greer0d355832013-01-08 11:57:46 -0700282 omap_aes_write_n(dd, AES_REG_IV(dd, 0), dd->req->info, 4);
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200283
284 val = FLD_VAL(((dd->ctx->keylen >> 3) - 1), 4, 3);
285 if (dd->flags & FLAGS_CBC)
286 val |= AES_REG_CTRL_CBC;
Vutla, Lokesh5396c6c2015-07-07 21:01:45 +0530287 if (dd->flags & FLAGS_CTR)
Joel Fernandes8ed49c72013-10-29 17:37:38 -0500288 val |= AES_REG_CTRL_CTR | AES_REG_CTRL_CTR_WIDTH_128;
Vutla, Lokesh5396c6c2015-07-07 21:01:45 +0530289
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200290 if (dd->flags & FLAGS_ENCRYPT)
291 val |= AES_REG_CTRL_DIRECTION;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800292
Vutla, Lokesh5396c6c2015-07-07 21:01:45 +0530293 omap_aes_write_mask(dd, AES_REG_CTRL(dd), val, AES_REG_CTRL_MASK);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800294
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200295 return 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800296}
297
Mark A. Greer0d355832013-01-08 11:57:46 -0700298static void omap_aes_dma_trigger_omap2(struct omap_aes_dev *dd, int length)
299{
300 u32 mask, val;
301
302 val = dd->pdata->dma_start;
303
304 if (dd->dma_lch_out != NULL)
305 val |= dd->pdata->dma_enable_out;
306 if (dd->dma_lch_in != NULL)
307 val |= dd->pdata->dma_enable_in;
308
309 mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
310 dd->pdata->dma_start;
311
312 omap_aes_write_mask(dd, AES_REG_MASK(dd), val, mask);
313
314}
315
316static void omap_aes_dma_trigger_omap4(struct omap_aes_dev *dd, int length)
317{
318 omap_aes_write(dd, AES_REG_LENGTH_N(0), length);
319 omap_aes_write(dd, AES_REG_LENGTH_N(1), 0);
320
321 omap_aes_dma_trigger_omap2(dd, length);
322}
323
324static void omap_aes_dma_stop(struct omap_aes_dev *dd)
325{
326 u32 mask;
327
328 mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
329 dd->pdata->dma_start;
330
331 omap_aes_write_mask(dd, AES_REG_MASK(dd), 0, mask);
332}
333
Tero Kristo619ce702017-05-24 10:35:23 +0300334static struct omap_aes_dev *omap_aes_find_dev(struct omap_aes_reqctx *rctx)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800335{
Lokesh Vutla164f3ef2016-08-04 13:28:43 +0300336 struct omap_aes_dev *dd;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800337
338 spin_lock_bh(&list_lock);
Lokesh Vutla164f3ef2016-08-04 13:28:43 +0300339 dd = list_first_entry(&dev_list, struct omap_aes_dev, list);
340 list_move_tail(&dd->list, &dev_list);
Tero Kristo619ce702017-05-24 10:35:23 +0300341 rctx->dd = dd;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800342 spin_unlock_bh(&list_lock);
343
344 return dd;
345}
346
Mark A. Greerebedbf72013-01-08 11:57:42 -0700347static void omap_aes_dma_out_callback(void *data)
348{
349 struct omap_aes_dev *dd = data;
350
351 /* dma_lch_out - completed */
352 tasklet_schedule(&dd->done_task);
353}
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800354
355static int omap_aes_dma_init(struct omap_aes_dev *dd)
356{
Peter Ujfalusida8b29a2016-04-29 16:02:18 +0300357 int err;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800358
Mark A. Greerebedbf72013-01-08 11:57:42 -0700359 dd->dma_lch_out = NULL;
360 dd->dma_lch_in = NULL;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800361
Peter Ujfalusida8b29a2016-04-29 16:02:18 +0300362 dd->dma_lch_in = dma_request_chan(dd->dev, "rx");
363 if (IS_ERR(dd->dma_lch_in)) {
Mark A. Greerebedbf72013-01-08 11:57:42 -0700364 dev_err(dd->dev, "Unable to request in DMA channel\n");
Peter Ujfalusida8b29a2016-04-29 16:02:18 +0300365 return PTR_ERR(dd->dma_lch_in);
Mark A. Greerebedbf72013-01-08 11:57:42 -0700366 }
367
Peter Ujfalusida8b29a2016-04-29 16:02:18 +0300368 dd->dma_lch_out = dma_request_chan(dd->dev, "tx");
369 if (IS_ERR(dd->dma_lch_out)) {
Mark A. Greerebedbf72013-01-08 11:57:42 -0700370 dev_err(dd->dev, "Unable to request out DMA channel\n");
Peter Ujfalusida8b29a2016-04-29 16:02:18 +0300371 err = PTR_ERR(dd->dma_lch_out);
Mark A. Greerebedbf72013-01-08 11:57:42 -0700372 goto err_dma_out;
373 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800374
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800375 return 0;
376
377err_dma_out:
Mark A. Greerebedbf72013-01-08 11:57:42 -0700378 dma_release_channel(dd->dma_lch_in);
Peter Ujfalusida8b29a2016-04-29 16:02:18 +0300379
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800380 return err;
381}
382
383static void omap_aes_dma_cleanup(struct omap_aes_dev *dd)
384{
Peter Ujfalusida8b29a2016-04-29 16:02:18 +0300385 if (dd->pio_only)
386 return;
387
Mark A. Greerebedbf72013-01-08 11:57:42 -0700388 dma_release_channel(dd->dma_lch_out);
389 dma_release_channel(dd->dma_lch_in);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800390}
391
Tero Kristo619ce702017-05-24 10:35:23 +0300392static int omap_aes_crypt_dma(struct omap_aes_dev *dd,
393 struct scatterlist *in_sg,
394 struct scatterlist *out_sg,
395 int in_sg_len, int out_sg_len)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800396{
Mark A. Greerebedbf72013-01-08 11:57:42 -0700397 struct dma_async_tx_descriptor *tx_in, *tx_out;
398 struct dma_slave_config cfg;
Joel Fernandes4b645c92013-08-17 21:42:25 -0500399 int ret;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800400
Joel Fernandes98837ab2013-08-17 21:42:30 -0500401 if (dd->pio_only) {
402 scatterwalk_start(&dd->in_walk, dd->in_sg);
403 scatterwalk_start(&dd->out_walk, dd->out_sg);
404
405 /* Enable DATAIN interrupt and let it take
406 care of the rest */
407 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x2);
408 return 0;
409 }
410
Joel Fernandes0a641712013-08-17 21:42:26 -0500411 dma_sync_sg_for_device(dd->dev, dd->in_sg, in_sg_len, DMA_TO_DEVICE);
412
Mark A. Greerebedbf72013-01-08 11:57:42 -0700413 memset(&cfg, 0, sizeof(cfg));
414
Mark A. Greer0d355832013-01-08 11:57:46 -0700415 cfg.src_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
416 cfg.dst_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
Mark A. Greerebedbf72013-01-08 11:57:42 -0700417 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
418 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
419 cfg.src_maxburst = DST_MAXBURST;
420 cfg.dst_maxburst = DST_MAXBURST;
421
422 /* IN */
423 ret = dmaengine_slave_config(dd->dma_lch_in, &cfg);
424 if (ret) {
425 dev_err(dd->dev, "can't configure IN dmaengine slave: %d\n",
426 ret);
427 return ret;
428 }
429
Joel Fernandes4b645c92013-08-17 21:42:25 -0500430 tx_in = dmaengine_prep_slave_sg(dd->dma_lch_in, in_sg, in_sg_len,
Mark A. Greerebedbf72013-01-08 11:57:42 -0700431 DMA_MEM_TO_DEV,
432 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
433 if (!tx_in) {
434 dev_err(dd->dev, "IN prep_slave_sg() failed\n");
435 return -EINVAL;
436 }
437
438 /* No callback necessary */
439 tx_in->callback_param = dd;
440
441 /* OUT */
442 ret = dmaengine_slave_config(dd->dma_lch_out, &cfg);
443 if (ret) {
444 dev_err(dd->dev, "can't configure OUT dmaengine slave: %d\n",
445 ret);
446 return ret;
447 }
448
Joel Fernandes4b645c92013-08-17 21:42:25 -0500449 tx_out = dmaengine_prep_slave_sg(dd->dma_lch_out, out_sg, out_sg_len,
Mark A. Greerebedbf72013-01-08 11:57:42 -0700450 DMA_DEV_TO_MEM,
451 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
452 if (!tx_out) {
453 dev_err(dd->dev, "OUT prep_slave_sg() failed\n");
454 return -EINVAL;
455 }
456
457 tx_out->callback = omap_aes_dma_out_callback;
458 tx_out->callback_param = dd;
459
460 dmaengine_submit(tx_in);
461 dmaengine_submit(tx_out);
462
463 dma_async_issue_pending(dd->dma_lch_in);
464 dma_async_issue_pending(dd->dma_lch_out);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800465
Mark A. Greer0d355832013-01-08 11:57:46 -0700466 /* start DMA */
Joel Fernandes4b645c92013-08-17 21:42:25 -0500467 dd->pdata->trigger(dd, dd->total);
Dmitry Kasatkin83ea7e02010-11-30 10:13:31 +0200468
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800469 return 0;
470}
471
472static int omap_aes_crypt_dma_start(struct omap_aes_dev *dd)
473{
Joel Fernandes4b645c92013-08-17 21:42:25 -0500474 int err;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800475
476 pr_debug("total: %d\n", dd->total);
477
Joel Fernandes98837ab2013-08-17 21:42:30 -0500478 if (!dd->pio_only) {
479 err = dma_map_sg(dd->dev, dd->in_sg, dd->in_sg_len,
480 DMA_TO_DEVICE);
481 if (!err) {
482 dev_err(dd->dev, "dma_map_sg() error\n");
483 return -EINVAL;
484 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800485
Joel Fernandes98837ab2013-08-17 21:42:30 -0500486 err = dma_map_sg(dd->dev, dd->out_sg, dd->out_sg_len,
487 DMA_FROM_DEVICE);
488 if (!err) {
489 dev_err(dd->dev, "dma_map_sg() error\n");
490 return -EINVAL;
491 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800492 }
493
Tero Kristo619ce702017-05-24 10:35:23 +0300494 err = omap_aes_crypt_dma(dd, dd->in_sg, dd->out_sg, dd->in_sg_len,
Joel Fernandes4b645c92013-08-17 21:42:25 -0500495 dd->out_sg_len);
Joel Fernandes98837ab2013-08-17 21:42:30 -0500496 if (err && !dd->pio_only) {
Joel Fernandes4b645c92013-08-17 21:42:25 -0500497 dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
498 dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
499 DMA_FROM_DEVICE);
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200500 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800501
502 return err;
503}
504
505static void omap_aes_finish_req(struct omap_aes_dev *dd, int err)
506{
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200507 struct ablkcipher_request *req = dd->req;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800508
509 pr_debug("err: %d\n", err);
510
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200511 crypto_finalize_cipher_request(dd->engine, req, err);
Tero Kristof303b452016-08-04 13:28:42 +0300512
513 pm_runtime_mark_last_busy(dd->dev);
514 pm_runtime_put_autosuspend(dd->dev);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800515}
516
517static int omap_aes_crypt_dma_stop(struct omap_aes_dev *dd)
518{
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800519 pr_debug("total: %d\n", dd->total);
520
Mark A. Greer0d355832013-01-08 11:57:46 -0700521 omap_aes_dma_stop(dd);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800522
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800523
Rahul Pathak16f080a2015-12-14 08:45:23 +0000524 return 0;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800525}
526
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200527static int omap_aes_handle_queue(struct omap_aes_dev *dd,
Baolin Wang05299002016-01-26 20:25:40 +0800528 struct ablkcipher_request *req)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800529{
Dmitry Kasatkineeb2b202010-11-30 10:13:28 +0200530 if (req)
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200531 return crypto_transfer_cipher_request_to_engine(dd->engine, req);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800532
Baolin Wang05299002016-01-26 20:25:40 +0800533 return 0;
534}
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800535
Baolin Wang05299002016-01-26 20:25:40 +0800536static int omap_aes_prepare_req(struct crypto_engine *engine,
537 struct ablkcipher_request *req)
538{
539 struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(
540 crypto_ablkcipher_reqtfm(req));
Tero Kristo619ce702017-05-24 10:35:23 +0300541 struct omap_aes_reqctx *rctx = ablkcipher_request_ctx(req);
542 struct omap_aes_dev *dd = rctx->dd;
Tero Kristoafc2dc12017-05-24 10:35:28 +0300543 int ret;
544 u16 flags;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800545
Baolin Wang05299002016-01-26 20:25:40 +0800546 if (!dd)
547 return -ENODEV;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800548
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800549 /* assign new request to device */
550 dd->req = req;
551 dd->total = req->nbytes;
Joel Fernandes62423322013-08-17 21:42:32 -0500552 dd->total_save = req->nbytes;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800553 dd->in_sg = req->src;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800554 dd->out_sg = req->dst;
Tero Kristoafc2dc12017-05-24 10:35:28 +0300555 dd->orig_out = req->dst;
556
557 flags = OMAP_CRYPTO_COPY_DATA;
558 if (req->src == req->dst)
559 flags |= OMAP_CRYPTO_FORCE_COPY;
560
561 ret = omap_crypto_align_sg(&dd->in_sg, dd->total, AES_BLOCK_SIZE,
562 &dd->in_sgl, flags,
563 FLAGS_IN_DATA_ST_SHIFT, &dd->flags);
564 if (ret)
565 return ret;
566
567 ret = omap_crypto_align_sg(&dd->out_sg, dd->total, AES_BLOCK_SIZE,
568 &dd->out_sgl, 0,
569 FLAGS_OUT_DATA_ST_SHIFT, &dd->flags);
570 if (ret)
571 return ret;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800572
Herbert Xu7c001a82016-07-12 13:17:52 +0800573 dd->in_sg_len = sg_nents_for_len(dd->in_sg, dd->total);
574 if (dd->in_sg_len < 0)
575 return dd->in_sg_len;
576
577 dd->out_sg_len = sg_nents_for_len(dd->out_sg, dd->total);
578 if (dd->out_sg_len < 0)
579 return dd->out_sg_len;
580
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800581 rctx->mode &= FLAGS_MODE_MASK;
582 dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode;
583
Dmitry Kasatkin67a730c2010-11-30 10:13:30 +0200584 dd->ctx = ctx;
Tero Kristo619ce702017-05-24 10:35:23 +0300585 rctx->dd = dd;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800586
Baolin Wang05299002016-01-26 20:25:40 +0800587 return omap_aes_write_ctrl(dd);
588}
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800589
Baolin Wang05299002016-01-26 20:25:40 +0800590static int omap_aes_crypt_req(struct crypto_engine *engine,
591 struct ablkcipher_request *req)
592{
Tero Kristo619ce702017-05-24 10:35:23 +0300593 struct omap_aes_reqctx *rctx = ablkcipher_request_ctx(req);
594 struct omap_aes_dev *dd = rctx->dd;
Baolin Wang05299002016-01-26 20:25:40 +0800595
596 if (!dd)
597 return -ENODEV;
598
599 return omap_aes_crypt_dma_start(dd);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800600}
601
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200602static void omap_aes_done_task(unsigned long data)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800603{
604 struct omap_aes_dev *dd = (struct omap_aes_dev *)data;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800605
Joel Fernandes4b645c92013-08-17 21:42:25 -0500606 pr_debug("enter done_task\n");
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800607
Joel Fernandes98837ab2013-08-17 21:42:30 -0500608 if (!dd->pio_only) {
609 dma_sync_sg_for_device(dd->dev, dd->out_sg, dd->out_sg_len,
610 DMA_FROM_DEVICE);
Joel Fernandes62423322013-08-17 21:42:32 -0500611 dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
612 dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
613 DMA_FROM_DEVICE);
Joel Fernandes98837ab2013-08-17 21:42:30 -0500614 omap_aes_crypt_dma_stop(dd);
615 }
Joel Fernandes62423322013-08-17 21:42:32 -0500616
Tero Kristoafc2dc12017-05-24 10:35:28 +0300617 omap_crypto_cleanup(&dd->in_sgl, NULL, 0, dd->total_save,
618 FLAGS_IN_DATA_ST_SHIFT, dd->flags);
Joel Fernandes62423322013-08-17 21:42:32 -0500619
Tero Kristoafc2dc12017-05-24 10:35:28 +0300620 omap_crypto_cleanup(&dd->out_sgl, dd->orig_out, 0, dd->total_save,
621 FLAGS_OUT_DATA_ST_SHIFT, dd->flags);
Joel Fernandes62423322013-08-17 21:42:32 -0500622
Joel Fernandes4b645c92013-08-17 21:42:25 -0500623 omap_aes_finish_req(dd, 0);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800624
625 pr_debug("exit\n");
626}
627
628static int omap_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
629{
630 struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(
631 crypto_ablkcipher_reqtfm(req));
632 struct omap_aes_reqctx *rctx = ablkcipher_request_ctx(req);
633 struct omap_aes_dev *dd;
Lokesh Vutla9fcb1912016-08-04 13:28:44 +0300634 int ret;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800635
636 pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req->nbytes,
637 !!(mode & FLAGS_ENCRYPT),
638 !!(mode & FLAGS_CBC));
639
Lokesh Vutla9fcb1912016-08-04 13:28:44 +0300640 if (req->nbytes < 200) {
641 SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback);
642
643 skcipher_request_set_tfm(subreq, ctx->fallback);
644 skcipher_request_set_callback(subreq, req->base.flags, NULL,
645 NULL);
646 skcipher_request_set_crypt(subreq, req->src, req->dst,
647 req->nbytes, req->info);
648
649 if (mode & FLAGS_ENCRYPT)
650 ret = crypto_skcipher_encrypt(subreq);
651 else
652 ret = crypto_skcipher_decrypt(subreq);
653
654 skcipher_request_zero(subreq);
655 return ret;
656 }
Tero Kristo619ce702017-05-24 10:35:23 +0300657 dd = omap_aes_find_dev(rctx);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800658 if (!dd)
659 return -ENODEV;
660
661 rctx->mode = mode;
662
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +0200663 return omap_aes_handle_queue(dd, req);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800664}
665
666/* ********************** ALG API ************************************ */
667
668static int omap_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
669 unsigned int keylen)
670{
671 struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(tfm);
Lokesh Vutla9fcb1912016-08-04 13:28:44 +0300672 int ret;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800673
674 if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
675 keylen != AES_KEYSIZE_256)
676 return -EINVAL;
677
678 pr_debug("enter, keylen: %d\n", keylen);
679
680 memcpy(ctx->key, key, keylen);
681 ctx->keylen = keylen;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800682
Lokesh Vutla9fcb1912016-08-04 13:28:44 +0300683 crypto_skcipher_clear_flags(ctx->fallback, CRYPTO_TFM_REQ_MASK);
684 crypto_skcipher_set_flags(ctx->fallback, tfm->base.crt_flags &
685 CRYPTO_TFM_REQ_MASK);
686
687 ret = crypto_skcipher_setkey(ctx->fallback, key, keylen);
688 if (!ret)
689 return 0;
690
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800691 return 0;
692}
693
694static int omap_aes_ecb_encrypt(struct ablkcipher_request *req)
695{
696 return omap_aes_crypt(req, FLAGS_ENCRYPT);
697}
698
699static int omap_aes_ecb_decrypt(struct ablkcipher_request *req)
700{
701 return omap_aes_crypt(req, 0);
702}
703
704static int omap_aes_cbc_encrypt(struct ablkcipher_request *req)
705{
706 return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC);
707}
708
709static int omap_aes_cbc_decrypt(struct ablkcipher_request *req)
710{
711 return omap_aes_crypt(req, FLAGS_CBC);
712}
713
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700714static int omap_aes_ctr_encrypt(struct ablkcipher_request *req)
715{
716 return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CTR);
717}
718
719static int omap_aes_ctr_decrypt(struct ablkcipher_request *req)
720{
721 return omap_aes_crypt(req, FLAGS_CTR);
722}
723
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800724static int omap_aes_cra_init(struct crypto_tfm *tfm)
725{
Lokesh Vutla9fcb1912016-08-04 13:28:44 +0300726 const char *name = crypto_tfm_alg_name(tfm);
727 const u32 flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK;
728 struct omap_aes_ctx *ctx = crypto_tfm_ctx(tfm);
729 struct crypto_skcipher *blk;
730
731 blk = crypto_alloc_skcipher(name, 0, flags);
732 if (IS_ERR(blk))
733 return PTR_ERR(blk);
734
735 ctx->fallback = blk;
736
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800737 tfm->crt_ablkcipher.reqsize = sizeof(struct omap_aes_reqctx);
738
739 return 0;
740}
741
742static void omap_aes_cra_exit(struct crypto_tfm *tfm)
743{
Lokesh Vutla9fcb1912016-08-04 13:28:44 +0300744 struct omap_aes_ctx *ctx = crypto_tfm_ctx(tfm);
745
746 if (ctx->fallback)
747 crypto_free_skcipher(ctx->fallback);
748
749 ctx->fallback = NULL;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800750}
751
752/* ********************** ALGS ************************************ */
753
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700754static struct crypto_alg algs_ecb_cbc[] = {
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800755{
756 .cra_name = "ecb(aes)",
757 .cra_driver_name = "ecb-aes-omap",
Vutla, Lokesh6e2e3d12015-07-07 21:01:44 +0530758 .cra_priority = 300,
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +0100759 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
760 CRYPTO_ALG_KERN_DRIVER_ONLY |
Lokesh Vutla9fcb1912016-08-04 13:28:44 +0300761 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800762 .cra_blocksize = AES_BLOCK_SIZE,
763 .cra_ctxsize = sizeof(struct omap_aes_ctx),
Dmitry Kasatkinefce41b2010-11-30 10:13:32 +0200764 .cra_alignmask = 0,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800765 .cra_type = &crypto_ablkcipher_type,
766 .cra_module = THIS_MODULE,
767 .cra_init = omap_aes_cra_init,
768 .cra_exit = omap_aes_cra_exit,
769 .cra_u.ablkcipher = {
770 .min_keysize = AES_MIN_KEY_SIZE,
771 .max_keysize = AES_MAX_KEY_SIZE,
772 .setkey = omap_aes_setkey,
773 .encrypt = omap_aes_ecb_encrypt,
774 .decrypt = omap_aes_ecb_decrypt,
775 }
776},
777{
778 .cra_name = "cbc(aes)",
779 .cra_driver_name = "cbc-aes-omap",
Vutla, Lokesh6e2e3d12015-07-07 21:01:44 +0530780 .cra_priority = 300,
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +0100781 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
782 CRYPTO_ALG_KERN_DRIVER_ONLY |
Lokesh Vutla9fcb1912016-08-04 13:28:44 +0300783 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800784 .cra_blocksize = AES_BLOCK_SIZE,
785 .cra_ctxsize = sizeof(struct omap_aes_ctx),
Dmitry Kasatkinefce41b2010-11-30 10:13:32 +0200786 .cra_alignmask = 0,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +0800787 .cra_type = &crypto_ablkcipher_type,
788 .cra_module = THIS_MODULE,
789 .cra_init = omap_aes_cra_init,
790 .cra_exit = omap_aes_cra_exit,
791 .cra_u.ablkcipher = {
792 .min_keysize = AES_MIN_KEY_SIZE,
793 .max_keysize = AES_MAX_KEY_SIZE,
794 .ivsize = AES_BLOCK_SIZE,
795 .setkey = omap_aes_setkey,
796 .encrypt = omap_aes_cbc_encrypt,
797 .decrypt = omap_aes_cbc_decrypt,
798 }
799}
800};
801
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700802static struct crypto_alg algs_ctr[] = {
803{
804 .cra_name = "ctr(aes)",
805 .cra_driver_name = "ctr-aes-omap",
Vutla, Lokesh6e2e3d12015-07-07 21:01:44 +0530806 .cra_priority = 300,
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700807 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
808 CRYPTO_ALG_KERN_DRIVER_ONLY |
Lokesh Vutla9fcb1912016-08-04 13:28:44 +0300809 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700810 .cra_blocksize = AES_BLOCK_SIZE,
811 .cra_ctxsize = sizeof(struct omap_aes_ctx),
812 .cra_alignmask = 0,
813 .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 .geniv = "eseqiv",
821 .ivsize = AES_BLOCK_SIZE,
822 .setkey = omap_aes_setkey,
823 .encrypt = omap_aes_ctr_encrypt,
824 .decrypt = omap_aes_ctr_decrypt,
825 }
826} ,
827};
828
829static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc[] = {
830 {
831 .algs_list = algs_ecb_cbc,
832 .size = ARRAY_SIZE(algs_ecb_cbc),
833 },
834};
835
Mark A. Greer0d355832013-01-08 11:57:46 -0700836static const struct omap_aes_pdata omap_aes_pdata_omap2 = {
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700837 .algs_info = omap_aes_algs_info_ecb_cbc,
838 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc),
Mark A. Greer0d355832013-01-08 11:57:46 -0700839 .trigger = omap_aes_dma_trigger_omap2,
840 .key_ofs = 0x1c,
841 .iv_ofs = 0x20,
842 .ctrl_ofs = 0x30,
843 .data_ofs = 0x34,
844 .rev_ofs = 0x44,
845 .mask_ofs = 0x48,
846 .dma_enable_in = BIT(2),
847 .dma_enable_out = BIT(3),
848 .dma_start = BIT(5),
849 .major_mask = 0xf0,
850 .major_shift = 4,
851 .minor_mask = 0x0f,
852 .minor_shift = 0,
853};
854
Mark A. Greerbc69d122013-01-08 11:57:44 -0700855#ifdef CONFIG_OF
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700856static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc_ctr[] = {
857 {
858 .algs_list = algs_ecb_cbc,
859 .size = ARRAY_SIZE(algs_ecb_cbc),
860 },
861 {
862 .algs_list = algs_ctr,
863 .size = ARRAY_SIZE(algs_ctr),
864 },
865};
866
867static const struct omap_aes_pdata omap_aes_pdata_omap3 = {
868 .algs_info = omap_aes_algs_info_ecb_cbc_ctr,
869 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
870 .trigger = omap_aes_dma_trigger_omap2,
871 .key_ofs = 0x1c,
872 .iv_ofs = 0x20,
873 .ctrl_ofs = 0x30,
874 .data_ofs = 0x34,
875 .rev_ofs = 0x44,
876 .mask_ofs = 0x48,
877 .dma_enable_in = BIT(2),
878 .dma_enable_out = BIT(3),
879 .dma_start = BIT(5),
880 .major_mask = 0xf0,
881 .major_shift = 4,
882 .minor_mask = 0x0f,
883 .minor_shift = 0,
884};
885
Mark A. Greer0d355832013-01-08 11:57:46 -0700886static const struct omap_aes_pdata omap_aes_pdata_omap4 = {
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700887 .algs_info = omap_aes_algs_info_ecb_cbc_ctr,
888 .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
Mark A. Greer0d355832013-01-08 11:57:46 -0700889 .trigger = omap_aes_dma_trigger_omap4,
890 .key_ofs = 0x3c,
891 .iv_ofs = 0x40,
892 .ctrl_ofs = 0x50,
893 .data_ofs = 0x60,
894 .rev_ofs = 0x80,
895 .mask_ofs = 0x84,
Joel Fernandes67216752013-08-17 21:42:28 -0500896 .irq_status_ofs = 0x8c,
897 .irq_enable_ofs = 0x90,
Mark A. Greer0d355832013-01-08 11:57:46 -0700898 .dma_enable_in = BIT(5),
899 .dma_enable_out = BIT(6),
900 .major_mask = 0x0700,
901 .major_shift = 8,
902 .minor_mask = 0x003f,
903 .minor_shift = 0,
904};
905
Joel Fernandes1bf95cc2013-08-17 21:42:29 -0500906static irqreturn_t omap_aes_irq(int irq, void *dev_id)
907{
908 struct omap_aes_dev *dd = dev_id;
909 u32 status, i;
910 u32 *src, *dst;
911
912 status = omap_aes_read(dd, AES_REG_IRQ_STATUS(dd));
913 if (status & AES_REG_IRQ_DATA_IN) {
914 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x0);
915
916 BUG_ON(!dd->in_sg);
917
918 BUG_ON(_calc_walked(in) > dd->in_sg->length);
919
920 src = sg_virt(dd->in_sg) + _calc_walked(in);
921
922 for (i = 0; i < AES_BLOCK_WORDS; i++) {
923 omap_aes_write(dd, AES_REG_DATA_N(dd, i), *src);
924
925 scatterwalk_advance(&dd->in_walk, 4);
926 if (dd->in_sg->length == _calc_walked(in)) {
Cristian Stoica5be4d4c2015-01-20 10:06:16 +0200927 dd->in_sg = sg_next(dd->in_sg);
Joel Fernandes1bf95cc2013-08-17 21:42:29 -0500928 if (dd->in_sg) {
929 scatterwalk_start(&dd->in_walk,
930 dd->in_sg);
931 src = sg_virt(dd->in_sg) +
932 _calc_walked(in);
933 }
934 } else {
935 src++;
936 }
937 }
938
939 /* Clear IRQ status */
940 status &= ~AES_REG_IRQ_DATA_IN;
941 omap_aes_write(dd, AES_REG_IRQ_STATUS(dd), status);
942
943 /* Enable DATA_OUT interrupt */
944 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x4);
945
946 } else if (status & AES_REG_IRQ_DATA_OUT) {
947 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x0);
948
949 BUG_ON(!dd->out_sg);
950
951 BUG_ON(_calc_walked(out) > dd->out_sg->length);
952
953 dst = sg_virt(dd->out_sg) + _calc_walked(out);
954
955 for (i = 0; i < AES_BLOCK_WORDS; i++) {
956 *dst = omap_aes_read(dd, AES_REG_DATA_N(dd, i));
957 scatterwalk_advance(&dd->out_walk, 4);
958 if (dd->out_sg->length == _calc_walked(out)) {
Cristian Stoica5be4d4c2015-01-20 10:06:16 +0200959 dd->out_sg = sg_next(dd->out_sg);
Joel Fernandes1bf95cc2013-08-17 21:42:29 -0500960 if (dd->out_sg) {
961 scatterwalk_start(&dd->out_walk,
962 dd->out_sg);
963 dst = sg_virt(dd->out_sg) +
964 _calc_walked(out);
965 }
966 } else {
967 dst++;
968 }
969 }
970
Vutla, Lokesh310b0d52015-07-07 21:01:43 +0530971 dd->total -= min_t(size_t, AES_BLOCK_SIZE, dd->total);
Joel Fernandes1bf95cc2013-08-17 21:42:29 -0500972
973 /* Clear IRQ status */
974 status &= ~AES_REG_IRQ_DATA_OUT;
975 omap_aes_write(dd, AES_REG_IRQ_STATUS(dd), status);
976
977 if (!dd->total)
978 /* All bytes read! */
979 tasklet_schedule(&dd->done_task);
980 else
981 /* Enable DATA_IN interrupt for next block */
982 omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x2);
983 }
984
985 return IRQ_HANDLED;
986}
987
Mark A. Greerbc69d122013-01-08 11:57:44 -0700988static const struct of_device_id omap_aes_of_match[] = {
989 {
990 .compatible = "ti,omap2-aes",
Mark A. Greer0d355832013-01-08 11:57:46 -0700991 .data = &omap_aes_pdata_omap2,
992 },
993 {
Mark A. Greerf9fb69e2013-01-08 11:57:47 -0700994 .compatible = "ti,omap3-aes",
995 .data = &omap_aes_pdata_omap3,
996 },
997 {
Mark A. Greer0d355832013-01-08 11:57:46 -0700998 .compatible = "ti,omap4-aes",
999 .data = &omap_aes_pdata_omap4,
Mark A. Greerbc69d122013-01-08 11:57:44 -07001000 },
1001 {},
1002};
1003MODULE_DEVICE_TABLE(of, omap_aes_of_match);
1004
1005static int omap_aes_get_res_of(struct omap_aes_dev *dd,
1006 struct device *dev, struct resource *res)
1007{
1008 struct device_node *node = dev->of_node;
1009 const struct of_device_id *match;
1010 int err = 0;
1011
1012 match = of_match_device(of_match_ptr(omap_aes_of_match), dev);
1013 if (!match) {
1014 dev_err(dev, "no compatible OF match\n");
1015 err = -EINVAL;
1016 goto err;
1017 }
1018
1019 err = of_address_to_resource(node, 0, res);
1020 if (err < 0) {
1021 dev_err(dev, "can't translate OF node address\n");
1022 err = -EINVAL;
1023 goto err;
1024 }
1025
Mark A. Greer0d355832013-01-08 11:57:46 -07001026 dd->pdata = match->data;
1027
Mark A. Greerbc69d122013-01-08 11:57:44 -07001028err:
1029 return err;
1030}
1031#else
1032static const struct of_device_id omap_aes_of_match[] = {
1033 {},
1034};
1035
1036static int omap_aes_get_res_of(struct omap_aes_dev *dd,
1037 struct device *dev, struct resource *res)
1038{
1039 return -EINVAL;
1040}
1041#endif
1042
1043static int omap_aes_get_res_pdev(struct omap_aes_dev *dd,
1044 struct platform_device *pdev, struct resource *res)
1045{
1046 struct device *dev = &pdev->dev;
1047 struct resource *r;
1048 int err = 0;
1049
1050 /* Get the base address */
1051 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1052 if (!r) {
1053 dev_err(dev, "no MEM resource info\n");
1054 err = -ENODEV;
1055 goto err;
1056 }
1057 memcpy(res, r, sizeof(*res));
1058
Mark A. Greer0d355832013-01-08 11:57:46 -07001059 /* Only OMAP2/3 can be non-DT */
1060 dd->pdata = &omap_aes_pdata_omap2;
1061
Mark A. Greerbc69d122013-01-08 11:57:44 -07001062err:
1063 return err;
1064}
1065
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001066static int omap_aes_probe(struct platform_device *pdev)
1067{
1068 struct device *dev = &pdev->dev;
1069 struct omap_aes_dev *dd;
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001070 struct crypto_alg *algp;
Mark A. Greerbc69d122013-01-08 11:57:44 -07001071 struct resource res;
Joel Fernandes1801ad92013-08-17 21:42:31 -05001072 int err = -ENOMEM, i, j, irq = -1;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001073 u32 reg;
1074
Joel Fernandes05007c12013-08-17 21:42:33 -05001075 dd = devm_kzalloc(dev, sizeof(struct omap_aes_dev), GFP_KERNEL);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001076 if (dd == NULL) {
1077 dev_err(dev, "unable to alloc data struct.\n");
1078 goto err_data;
1079 }
1080 dd->dev = dev;
1081 platform_set_drvdata(pdev, dd);
1082
Mark A. Greerbc69d122013-01-08 11:57:44 -07001083 err = (dev->of_node) ? omap_aes_get_res_of(dd, dev, &res) :
1084 omap_aes_get_res_pdev(dd, pdev, &res);
1085 if (err)
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001086 goto err_res;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001087
Laurent Navet30862282013-05-02 14:00:38 +02001088 dd->io_base = devm_ioremap_resource(dev, &res);
1089 if (IS_ERR(dd->io_base)) {
1090 err = PTR_ERR(dd->io_base);
Mark A. Greer5946c4a2013-01-08 11:57:40 -07001091 goto err_res;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001092 }
Mark A. Greerbc69d122013-01-08 11:57:44 -07001093 dd->phys_base = res.start;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001094
Tero Kristof303b452016-08-04 13:28:42 +03001095 pm_runtime_use_autosuspend(dev);
1096 pm_runtime_set_autosuspend_delay(dev, DEFAULT_AUTOSUSPEND_DELAY);
1097
Mark A. Greer5946c4a2013-01-08 11:57:40 -07001098 pm_runtime_enable(dev);
Nishanth Menonf7b2b5d2013-12-03 19:43:13 -06001099 err = pm_runtime_get_sync(dev);
1100 if (err < 0) {
1101 dev_err(dev, "%s: failed to get_sync(%d)\n",
1102 __func__, err);
1103 goto err_res;
1104 }
Mark A. Greer5946c4a2013-01-08 11:57:40 -07001105
Mark A. Greer0d355832013-01-08 11:57:46 -07001106 omap_aes_dma_stop(dd);
1107
1108 reg = omap_aes_read(dd, AES_REG_REV(dd));
Mark A. Greer5946c4a2013-01-08 11:57:40 -07001109
1110 pm_runtime_put_sync(dev);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001111
Mark A. Greer0d355832013-01-08 11:57:46 -07001112 dev_info(dev, "OMAP AES hw accel rev: %u.%u\n",
1113 (reg & dd->pdata->major_mask) >> dd->pdata->major_shift,
1114 (reg & dd->pdata->minor_mask) >> dd->pdata->minor_shift);
1115
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +02001116 tasklet_init(&dd->done_task, omap_aes_done_task, (unsigned long)dd);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001117
1118 err = omap_aes_dma_init(dd);
Peter Ujfalusida8b29a2016-04-29 16:02:18 +03001119 if (err == -EPROBE_DEFER) {
1120 goto err_irq;
1121 } else if (err && AES_REG_IRQ_STATUS(dd) && AES_REG_IRQ_ENABLE(dd)) {
Joel Fernandes1801ad92013-08-17 21:42:31 -05001122 dd->pio_only = 1;
1123
1124 irq = platform_get_irq(pdev, 0);
1125 if (irq < 0) {
1126 dev_err(dev, "can't get IRQ resource\n");
1127 goto err_irq;
1128 }
1129
Joel Fernandesbce2a222013-08-17 21:42:34 -05001130 err = devm_request_irq(dev, irq, omap_aes_irq, 0,
Joel Fernandes1801ad92013-08-17 21:42:31 -05001131 dev_name(dev), dd);
1132 if (err) {
1133 dev_err(dev, "Unable to grab omap-aes IRQ\n");
1134 goto err_irq;
1135 }
1136 }
1137
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001138
1139 INIT_LIST_HEAD(&dd->list);
1140 spin_lock(&list_lock);
1141 list_add_tail(&dd->list, &dev_list);
1142 spin_unlock(&list_lock);
1143
Tero Kristo0d0cda932016-08-04 13:28:45 +03001144 /* Initialize crypto engine */
1145 dd->engine = crypto_engine_alloc_init(dev, 1);
Wei Yongjunc98ef8d2016-09-15 03:27:32 +00001146 if (!dd->engine) {
1147 err = -ENOMEM;
Tero Kristo0d0cda932016-08-04 13:28:45 +03001148 goto err_engine;
Wei Yongjunc98ef8d2016-09-15 03:27:32 +00001149 }
Tero Kristo0d0cda932016-08-04 13:28:45 +03001150
1151 dd->engine->prepare_cipher_request = omap_aes_prepare_req;
1152 dd->engine->cipher_one_request = omap_aes_crypt_req;
1153 err = crypto_engine_start(dd->engine);
1154 if (err)
1155 goto err_engine;
1156
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001157 for (i = 0; i < dd->pdata->algs_info_size; i++) {
Lokesh Vutla3741bbb2016-06-01 11:56:02 +03001158 if (!dd->pdata->algs_info[i].registered) {
1159 for (j = 0; j < dd->pdata->algs_info[i].size; j++) {
1160 algp = &dd->pdata->algs_info[i].algs_list[j];
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001161
Lokesh Vutla3741bbb2016-06-01 11:56:02 +03001162 pr_debug("reg alg: %s\n", algp->cra_name);
1163 INIT_LIST_HEAD(&algp->cra_list);
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001164
Lokesh Vutla3741bbb2016-06-01 11:56:02 +03001165 err = crypto_register_alg(algp);
1166 if (err)
1167 goto err_algs;
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001168
Lokesh Vutla3741bbb2016-06-01 11:56:02 +03001169 dd->pdata->algs_info[i].registered++;
1170 }
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001171 }
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001172 }
1173
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001174 return 0;
1175err_algs:
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001176 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1177 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
1178 crypto_unregister_alg(
1179 &dd->pdata->algs_info[i].algs_list[j]);
Peter Ujfalusida8b29a2016-04-29 16:02:18 +03001180
Tero Kristo0d0cda932016-08-04 13:28:45 +03001181err_engine:
1182 if (dd->engine)
1183 crypto_engine_exit(dd->engine);
1184
Peter Ujfalusida8b29a2016-04-29 16:02:18 +03001185 omap_aes_dma_cleanup(dd);
Joel Fernandes1801ad92013-08-17 21:42:31 -05001186err_irq:
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +02001187 tasklet_kill(&dd->done_task);
Mark A. Greer5946c4a2013-01-08 11:57:40 -07001188 pm_runtime_disable(dev);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001189err_res:
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001190 dd = NULL;
1191err_data:
1192 dev_err(dev, "initialization failed.\n");
1193 return err;
1194}
1195
1196static int omap_aes_remove(struct platform_device *pdev)
1197{
1198 struct omap_aes_dev *dd = platform_get_drvdata(pdev);
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001199 int i, j;
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001200
1201 if (!dd)
1202 return -ENODEV;
1203
1204 spin_lock(&list_lock);
1205 list_del(&dd->list);
1206 spin_unlock(&list_lock);
1207
Mark A. Greerf9fb69e2013-01-08 11:57:47 -07001208 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1209 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
1210 crypto_unregister_alg(
1211 &dd->pdata->algs_info[i].algs_list[j]);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001212
Baolin Wang05299002016-01-26 20:25:40 +08001213 crypto_engine_exit(dd->engine);
Dmitry Kasatkin21fe9762010-11-30 10:13:29 +02001214 tasklet_kill(&dd->done_task);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001215 omap_aes_dma_cleanup(dd);
Mark A. Greer5946c4a2013-01-08 11:57:40 -07001216 pm_runtime_disable(dd->dev);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001217 dd = NULL;
1218
1219 return 0;
1220}
1221
Mark A. Greer0635fb32013-01-08 11:57:41 -07001222#ifdef CONFIG_PM_SLEEP
1223static int omap_aes_suspend(struct device *dev)
1224{
1225 pm_runtime_put_sync(dev);
1226 return 0;
1227}
1228
1229static int omap_aes_resume(struct device *dev)
1230{
1231 pm_runtime_get_sync(dev);
1232 return 0;
1233}
1234#endif
1235
Jingoo Hanea7b2842014-02-27 20:31:38 +09001236static SIMPLE_DEV_PM_OPS(omap_aes_pm_ops, omap_aes_suspend, omap_aes_resume);
Mark A. Greer0635fb32013-01-08 11:57:41 -07001237
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001238static struct platform_driver omap_aes_driver = {
1239 .probe = omap_aes_probe,
1240 .remove = omap_aes_remove,
1241 .driver = {
1242 .name = "omap-aes",
Mark A. Greer0635fb32013-01-08 11:57:41 -07001243 .pm = &omap_aes_pm_ops,
Mark A. Greerbc69d122013-01-08 11:57:44 -07001244 .of_match_table = omap_aes_of_match,
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001245 },
1246};
1247
Sachin Kamat94e51df2013-03-04 15:09:42 +05301248module_platform_driver(omap_aes_driver);
Dmitry Kasatkin537559a2010-09-03 19:16:02 +08001249
1250MODULE_DESCRIPTION("OMAP AES hw acceleration support.");
1251MODULE_LICENSE("GPL v2");
1252MODULE_AUTHOR("Dmitry Kasatkin");
1253