blob: 915253b9c9128242d066c2cbbfa9e807453600c4 [file] [log] [blame]
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001/*
2 * Cryptographic API.
3 *
4 * Support for ATMEL AES HW acceleration.
5 *
6 * Copyright (c) 2012 Eukréa Electromatique - ATMEL
7 * Author: Nicolas Royer <nicolas@eukrea.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as published
11 * by the Free Software Foundation.
12 *
13 * Some ideas are from omap-aes.c driver.
14 */
15
16
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/err.h>
21#include <linux/clk.h>
22#include <linux/io.h>
23#include <linux/hw_random.h>
24#include <linux/platform_device.h>
25
26#include <linux/device.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020027#include <linux/init.h>
28#include <linux/errno.h>
29#include <linux/interrupt.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020030#include <linux/irq.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020031#include <linux/scatterlist.h>
32#include <linux/dma-mapping.h>
Nicolas Ferrebe943c72013-10-14 17:52:38 +020033#include <linux/of_device.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020034#include <linux/delay.h>
35#include <linux/crypto.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020036#include <crypto/scatterwalk.h>
37#include <crypto/algapi.h>
38#include <crypto/aes.h>
Cyrille Pitchend4419542015-12-17 18:13:07 +010039#include <crypto/internal/aead.h>
Nicolas Royercadc4ab2013-02-20 17:10:24 +010040#include <linux/platform_data/crypto-atmel.h>
Nicolas Ferrebe943c72013-10-14 17:52:38 +020041#include <dt-bindings/dma/at91.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020042#include "atmel-aes-regs.h"
43
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +010044#define ATMEL_AES_PRIORITY 300
45
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +010046#define ATMEL_AES_BUFFER_ORDER 2
47#define ATMEL_AES_BUFFER_SIZE (PAGE_SIZE << ATMEL_AES_BUFFER_ORDER)
48
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020049#define CFB8_BLOCK_SIZE 1
50#define CFB16_BLOCK_SIZE 2
51#define CFB32_BLOCK_SIZE 4
52#define CFB64_BLOCK_SIZE 8
53
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +010054#define SIZE_IN_WORDS(x) ((x) >> 2)
55
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020056/* AES flags */
Cyrille Pitchend4419542015-12-17 18:13:07 +010057/* Reserve bits [18:16] [14:12] [1:0] for mode (same as for AES_MR) */
Cyrille Pitchen77dacf52015-12-17 17:48:41 +010058#define AES_FLAGS_ENCRYPT AES_MR_CYPHER_ENC
Cyrille Pitchend4419542015-12-17 18:13:07 +010059#define AES_FLAGS_GTAGEN AES_MR_GTAGEN
Cyrille Pitchen77dacf52015-12-17 17:48:41 +010060#define AES_FLAGS_OPMODE_MASK (AES_MR_OPMOD_MASK | AES_MR_CFBS_MASK)
61#define AES_FLAGS_ECB AES_MR_OPMOD_ECB
62#define AES_FLAGS_CBC AES_MR_OPMOD_CBC
63#define AES_FLAGS_OFB AES_MR_OPMOD_OFB
64#define AES_FLAGS_CFB128 (AES_MR_OPMOD_CFB | AES_MR_CFBS_128b)
65#define AES_FLAGS_CFB64 (AES_MR_OPMOD_CFB | AES_MR_CFBS_64b)
66#define AES_FLAGS_CFB32 (AES_MR_OPMOD_CFB | AES_MR_CFBS_32b)
67#define AES_FLAGS_CFB16 (AES_MR_OPMOD_CFB | AES_MR_CFBS_16b)
68#define AES_FLAGS_CFB8 (AES_MR_OPMOD_CFB | AES_MR_CFBS_8b)
69#define AES_FLAGS_CTR AES_MR_OPMOD_CTR
Cyrille Pitchend4419542015-12-17 18:13:07 +010070#define AES_FLAGS_GCM AES_MR_OPMOD_GCM
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020071
Cyrille Pitchen77dacf52015-12-17 17:48:41 +010072#define AES_FLAGS_MODE_MASK (AES_FLAGS_OPMODE_MASK | \
Cyrille Pitchend4419542015-12-17 18:13:07 +010073 AES_FLAGS_ENCRYPT | \
74 AES_FLAGS_GTAGEN)
Cyrille Pitchen77dacf52015-12-17 17:48:41 +010075
76#define AES_FLAGS_INIT BIT(2)
77#define AES_FLAGS_BUSY BIT(3)
Cyrille Pitchen45379922015-12-17 18:13:08 +010078#define AES_FLAGS_DUMP_REG BIT(4)
Cyrille Pitchen77dacf52015-12-17 17:48:41 +010079
80#define AES_FLAGS_PERSISTENT (AES_FLAGS_INIT | AES_FLAGS_BUSY)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020081
Nicolas Royercadc4ab2013-02-20 17:10:24 +010082#define ATMEL_AES_QUEUE_LENGTH 50
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020083
Cyrille Pitchen129f8bb2015-12-17 18:13:06 +010084#define ATMEL_AES_DMA_THRESHOLD 256
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020085
86
Nicolas Royercadc4ab2013-02-20 17:10:24 +010087struct atmel_aes_caps {
Cyrille Pitchenafbac172015-12-17 18:13:02 +010088 bool has_dualbuff;
89 bool has_cfb64;
Cyrille Pitchend4419542015-12-17 18:13:07 +010090 bool has_gcm;
Cyrille Pitchenafbac172015-12-17 18:13:02 +010091 u32 max_burst_size;
Nicolas Royercadc4ab2013-02-20 17:10:24 +010092};
93
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020094struct atmel_aes_dev;
95
Cyrille Pitchenccbf7292015-12-17 17:48:39 +010096
97typedef int (*atmel_aes_fn_t)(struct atmel_aes_dev *);
98
99
100struct atmel_aes_base_ctx {
Cyrille Pitchenafbac172015-12-17 18:13:02 +0100101 struct atmel_aes_dev *dd;
102 atmel_aes_fn_t start;
103 int keylen;
104 u32 key[AES_KEYSIZE_256 / sizeof(u32)];
105 u16 block_size;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200106};
107
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100108struct atmel_aes_ctx {
109 struct atmel_aes_base_ctx base;
110};
111
Cyrille Pitchenfcac8362015-12-17 18:13:05 +0100112struct atmel_aes_ctr_ctx {
113 struct atmel_aes_base_ctx base;
114
115 u32 iv[AES_BLOCK_SIZE / sizeof(u32)];
116 size_t offset;
117 struct scatterlist src[2];
118 struct scatterlist dst[2];
119};
120
Cyrille Pitchend4419542015-12-17 18:13:07 +0100121struct atmel_aes_gcm_ctx {
122 struct atmel_aes_base_ctx base;
123
124 struct scatterlist src[2];
125 struct scatterlist dst[2];
126
127 u32 j0[AES_BLOCK_SIZE / sizeof(u32)];
128 u32 tag[AES_BLOCK_SIZE / sizeof(u32)];
129 u32 ghash[AES_BLOCK_SIZE / sizeof(u32)];
130 size_t textlen;
131
132 const u32 *ghash_in;
133 u32 *ghash_out;
134 atmel_aes_fn_t ghash_resume;
135};
136
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200137struct atmel_aes_reqctx {
Cyrille Pitchenafbac172015-12-17 18:13:02 +0100138 unsigned long mode;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200139};
140
141struct atmel_aes_dma {
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100142 struct dma_chan *chan;
143 struct scatterlist *sg;
144 int nents;
145 unsigned int remainder;
146 unsigned int sg_len;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200147};
148
149struct atmel_aes_dev {
150 struct list_head list;
151 unsigned long phys_base;
152 void __iomem *io_base;
153
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100154 struct crypto_async_request *areq;
155 struct atmel_aes_base_ctx *ctx;
156
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100157 bool is_async;
158 atmel_aes_fn_t resume;
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100159 atmel_aes_fn_t cpu_transfer_complete;
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100160
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200161 struct device *dev;
162 struct clk *iclk;
Cyrille Pitchenafbac172015-12-17 18:13:02 +0100163 int irq;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200164
165 unsigned long flags;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200166
167 spinlock_t lock;
168 struct crypto_queue queue;
169
170 struct tasklet_struct done_task;
171 struct tasklet_struct queue_task;
172
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100173 size_t total;
174 size_t datalen;
175 u32 *data;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200176
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100177 struct atmel_aes_dma src;
178 struct atmel_aes_dma dst;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200179
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100180 size_t buflen;
181 void *buf;
182 struct scatterlist aligned_sg;
183 struct scatterlist *real_dst;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200184
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100185 struct atmel_aes_caps caps;
186
Cyrille Pitchenafbac172015-12-17 18:13:02 +0100187 u32 hw_version;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200188};
189
190struct atmel_aes_drv {
191 struct list_head dev_list;
192 spinlock_t lock;
193};
194
195static struct atmel_aes_drv atmel_aes = {
196 .dev_list = LIST_HEAD_INIT(atmel_aes.dev_list),
197 .lock = __SPIN_LOCK_UNLOCKED(atmel_aes.lock),
198};
199
Cyrille Pitchen45379922015-12-17 18:13:08 +0100200#ifdef VERBOSE_DEBUG
201static const char *atmel_aes_reg_name(u32 offset, char *tmp, size_t sz)
202{
203 switch (offset) {
204 case AES_CR:
205 return "CR";
206
207 case AES_MR:
208 return "MR";
209
210 case AES_ISR:
211 return "ISR";
212
213 case AES_IMR:
214 return "IMR";
215
216 case AES_IER:
217 return "IER";
218
219 case AES_IDR:
220 return "IDR";
221
222 case AES_KEYWR(0):
223 case AES_KEYWR(1):
224 case AES_KEYWR(2):
225 case AES_KEYWR(3):
226 case AES_KEYWR(4):
227 case AES_KEYWR(5):
228 case AES_KEYWR(6):
229 case AES_KEYWR(7):
230 snprintf(tmp, sz, "KEYWR[%u]", (offset - AES_KEYWR(0)) >> 2);
231 break;
232
233 case AES_IDATAR(0):
234 case AES_IDATAR(1):
235 case AES_IDATAR(2):
236 case AES_IDATAR(3):
237 snprintf(tmp, sz, "IDATAR[%u]", (offset - AES_IDATAR(0)) >> 2);
238 break;
239
240 case AES_ODATAR(0):
241 case AES_ODATAR(1):
242 case AES_ODATAR(2):
243 case AES_ODATAR(3):
244 snprintf(tmp, sz, "ODATAR[%u]", (offset - AES_ODATAR(0)) >> 2);
245 break;
246
247 case AES_IVR(0):
248 case AES_IVR(1):
249 case AES_IVR(2):
250 case AES_IVR(3):
251 snprintf(tmp, sz, "IVR[%u]", (offset - AES_IVR(0)) >> 2);
252 break;
253
254 case AES_AADLENR:
255 return "AADLENR";
256
257 case AES_CLENR:
258 return "CLENR";
259
260 case AES_GHASHR(0):
261 case AES_GHASHR(1):
262 case AES_GHASHR(2):
263 case AES_GHASHR(3):
264 snprintf(tmp, sz, "GHASHR[%u]", (offset - AES_GHASHR(0)) >> 2);
265 break;
266
267 case AES_TAGR(0):
268 case AES_TAGR(1):
269 case AES_TAGR(2):
270 case AES_TAGR(3):
271 snprintf(tmp, sz, "TAGR[%u]", (offset - AES_TAGR(0)) >> 2);
272 break;
273
274 case AES_CTRR:
275 return "CTRR";
276
277 case AES_GCMHR(0):
278 case AES_GCMHR(1):
279 case AES_GCMHR(2):
280 case AES_GCMHR(3):
281 snprintf(tmp, sz, "GCMHR[%u]", (offset - AES_GCMHR(0)) >> 2);
Herbert Xue31835a2016-01-19 09:05:43 +0800282 break;
Cyrille Pitchen45379922015-12-17 18:13:08 +0100283
284 default:
285 snprintf(tmp, sz, "0x%02x", offset);
286 break;
287 }
288
289 return tmp;
290}
291#endif /* VERBOSE_DEBUG */
292
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100293/* Shared functions */
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100294
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200295static inline u32 atmel_aes_read(struct atmel_aes_dev *dd, u32 offset)
296{
Cyrille Pitchen45379922015-12-17 18:13:08 +0100297 u32 value = readl_relaxed(dd->io_base + offset);
298
299#ifdef VERBOSE_DEBUG
300 if (dd->flags & AES_FLAGS_DUMP_REG) {
301 char tmp[16];
302
303 dev_vdbg(dd->dev, "read 0x%08x from %s\n", value,
304 atmel_aes_reg_name(offset, tmp, sizeof(tmp)));
305 }
306#endif /* VERBOSE_DEBUG */
307
308 return value;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200309}
310
311static inline void atmel_aes_write(struct atmel_aes_dev *dd,
312 u32 offset, u32 value)
313{
Cyrille Pitchen45379922015-12-17 18:13:08 +0100314#ifdef VERBOSE_DEBUG
315 if (dd->flags & AES_FLAGS_DUMP_REG) {
316 char tmp[16];
317
318 dev_vdbg(dd->dev, "write 0x%08x into %s\n", value,
319 atmel_aes_reg_name(offset, tmp));
320 }
321#endif /* VERBOSE_DEBUG */
322
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200323 writel_relaxed(value, dd->io_base + offset);
324}
325
326static void atmel_aes_read_n(struct atmel_aes_dev *dd, u32 offset,
327 u32 *value, int count)
328{
329 for (; count--; value++, offset += 4)
330 *value = atmel_aes_read(dd, offset);
331}
332
333static void atmel_aes_write_n(struct atmel_aes_dev *dd, u32 offset,
Cyrille Pitchenc0b28d82015-12-17 17:48:33 +0100334 const u32 *value, int count)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200335{
336 for (; count--; value++, offset += 4)
337 atmel_aes_write(dd, offset, *value);
338}
339
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100340static inline void atmel_aes_read_block(struct atmel_aes_dev *dd, u32 offset,
341 u32 *value)
342{
343 atmel_aes_read_n(dd, offset, value, SIZE_IN_WORDS(AES_BLOCK_SIZE));
344}
345
346static inline void atmel_aes_write_block(struct atmel_aes_dev *dd, u32 offset,
347 const u32 *value)
348{
349 atmel_aes_write_n(dd, offset, value, SIZE_IN_WORDS(AES_BLOCK_SIZE));
350}
351
352static inline int atmel_aes_wait_for_data_ready(struct atmel_aes_dev *dd,
353 atmel_aes_fn_t resume)
354{
355 u32 isr = atmel_aes_read(dd, AES_ISR);
356
357 if (unlikely(isr & AES_INT_DATARDY))
358 return resume(dd);
359
360 dd->resume = resume;
361 atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
362 return -EINPROGRESS;
363}
364
365static inline size_t atmel_aes_padlen(size_t len, size_t block_size)
366{
367 len &= block_size - 1;
368 return len ? block_size - len : 0;
369}
370
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100371static struct atmel_aes_dev *atmel_aes_find_dev(struct atmel_aes_base_ctx *ctx)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200372{
373 struct atmel_aes_dev *aes_dd = NULL;
374 struct atmel_aes_dev *tmp;
375
376 spin_lock_bh(&atmel_aes.lock);
377 if (!ctx->dd) {
378 list_for_each_entry(tmp, &atmel_aes.dev_list, list) {
379 aes_dd = tmp;
380 break;
381 }
382 ctx->dd = aes_dd;
383 } else {
384 aes_dd = ctx->dd;
385 }
386
387 spin_unlock_bh(&atmel_aes.lock);
388
389 return aes_dd;
390}
391
392static int atmel_aes_hw_init(struct atmel_aes_dev *dd)
393{
LABBE Corentin9d83d292015-10-02 14:12:58 +0200394 int err;
395
Cyrille Pitchen49a20452016-01-29 17:53:33 +0100396 err = clk_enable(dd->iclk);
LABBE Corentin9d83d292015-10-02 14:12:58 +0200397 if (err)
398 return err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200399
400 if (!(dd->flags & AES_FLAGS_INIT)) {
401 atmel_aes_write(dd, AES_CR, AES_CR_SWRST);
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100402 atmel_aes_write(dd, AES_MR, 0xE << AES_MR_CKEY_OFFSET);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200403 dd->flags |= AES_FLAGS_INIT;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200404 }
405
406 return 0;
407}
408
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100409static inline unsigned int atmel_aes_get_version(struct atmel_aes_dev *dd)
410{
411 return atmel_aes_read(dd, AES_HW_VERSION) & 0x00000fff;
412}
413
Cyrille Pitchenaab0a392015-12-17 17:48:37 +0100414static int atmel_aes_hw_version_init(struct atmel_aes_dev *dd)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200415{
Cyrille Pitchenaab0a392015-12-17 17:48:37 +0100416 int err;
417
418 err = atmel_aes_hw_init(dd);
419 if (err)
420 return err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200421
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100422 dd->hw_version = atmel_aes_get_version(dd);
423
Cyrille Pitchenaab0a392015-12-17 17:48:37 +0100424 dev_info(dd->dev, "version: 0x%x\n", dd->hw_version);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200425
Cyrille Pitchen49a20452016-01-29 17:53:33 +0100426 clk_disable(dd->iclk);
Cyrille Pitchenaab0a392015-12-17 17:48:37 +0100427 return 0;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200428}
429
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100430static inline void atmel_aes_set_mode(struct atmel_aes_dev *dd,
431 const struct atmel_aes_reqctx *rctx)
432{
433 /* Clear all but persistent flags and set request flags. */
434 dd->flags = (dd->flags & AES_FLAGS_PERSISTENT) | rctx->mode;
435}
436
Cyrille Pitchend4419542015-12-17 18:13:07 +0100437static inline bool atmel_aes_is_encrypt(const struct atmel_aes_dev *dd)
438{
439 return (dd->flags & AES_FLAGS_ENCRYPT);
440}
441
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100442static inline int atmel_aes_complete(struct atmel_aes_dev *dd, int err)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200443{
Cyrille Pitchen49a20452016-01-29 17:53:33 +0100444 clk_disable(dd->iclk);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200445 dd->flags &= ~AES_FLAGS_BUSY;
446
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100447 if (dd->is_async)
448 dd->areq->complete(dd->areq, err);
449
450 tasklet_schedule(&dd->queue_task);
451
452 return err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200453}
454
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100455static void atmel_aes_write_ctrl(struct atmel_aes_dev *dd, bool use_dma,
456 const u32 *iv)
457{
458 u32 valmr = 0;
459
460 /* MR register must be set before IV registers */
461 if (dd->ctx->keylen == AES_KEYSIZE_128)
462 valmr |= AES_MR_KEYSIZE_128;
463 else if (dd->ctx->keylen == AES_KEYSIZE_192)
464 valmr |= AES_MR_KEYSIZE_192;
465 else
466 valmr |= AES_MR_KEYSIZE_256;
467
468 valmr |= dd->flags & AES_FLAGS_MODE_MASK;
469
470 if (use_dma) {
471 valmr |= AES_MR_SMOD_IDATAR0;
472 if (dd->caps.has_dualbuff)
473 valmr |= AES_MR_DUALBUFF;
474 } else {
475 valmr |= AES_MR_SMOD_AUTO;
476 }
477
478 atmel_aes_write(dd, AES_MR, valmr);
479
480 atmel_aes_write_n(dd, AES_KEYWR(0), dd->ctx->key,
481 SIZE_IN_WORDS(dd->ctx->keylen));
482
483 if (iv && (valmr & AES_MR_OPMOD_MASK) != AES_MR_OPMOD_ECB)
484 atmel_aes_write_block(dd, AES_IVR(0), iv);
485}
486
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200487
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100488/* CPU transfer */
489
490static int atmel_aes_cpu_transfer(struct atmel_aes_dev *dd)
491{
492 int err = 0;
493 u32 isr;
494
495 for (;;) {
496 atmel_aes_read_block(dd, AES_ODATAR(0), dd->data);
497 dd->data += 4;
498 dd->datalen -= AES_BLOCK_SIZE;
499
500 if (dd->datalen < AES_BLOCK_SIZE)
501 break;
502
503 atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
504
505 isr = atmel_aes_read(dd, AES_ISR);
506 if (!(isr & AES_INT_DATARDY)) {
507 dd->resume = atmel_aes_cpu_transfer;
508 atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
509 return -EINPROGRESS;
510 }
511 }
512
513 if (!sg_copy_from_buffer(dd->real_dst, sg_nents(dd->real_dst),
514 dd->buf, dd->total))
515 err = -EINVAL;
516
517 if (err)
518 return atmel_aes_complete(dd, err);
519
520 return dd->cpu_transfer_complete(dd);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200521}
522
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100523static int atmel_aes_cpu_start(struct atmel_aes_dev *dd,
524 struct scatterlist *src,
525 struct scatterlist *dst,
526 size_t len,
527 atmel_aes_fn_t resume)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200528{
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100529 size_t padlen = atmel_aes_padlen(len, AES_BLOCK_SIZE);
530
531 if (unlikely(len == 0))
532 return -EINVAL;
533
534 sg_copy_to_buffer(src, sg_nents(src), dd->buf, len);
535
536 dd->total = len;
537 dd->real_dst = dst;
538 dd->cpu_transfer_complete = resume;
539 dd->datalen = len + padlen;
540 dd->data = (u32 *)dd->buf;
541 atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
542 return atmel_aes_wait_for_data_ready(dd, atmel_aes_cpu_transfer);
543}
544
545
546/* DMA transfer */
547
548static void atmel_aes_dma_callback(void *data);
549
550static bool atmel_aes_check_aligned(struct atmel_aes_dev *dd,
551 struct scatterlist *sg,
552 size_t len,
553 struct atmel_aes_dma *dma)
554{
555 int nents;
556
557 if (!IS_ALIGNED(len, dd->ctx->block_size))
558 return false;
559
560 for (nents = 0; sg; sg = sg_next(sg), ++nents) {
561 if (!IS_ALIGNED(sg->offset, sizeof(u32)))
562 return false;
563
564 if (len <= sg->length) {
565 if (!IS_ALIGNED(len, dd->ctx->block_size))
566 return false;
567
568 dma->nents = nents+1;
569 dma->remainder = sg->length - len;
570 sg->length = len;
571 return true;
572 }
573
574 if (!IS_ALIGNED(sg->length, dd->ctx->block_size))
575 return false;
576
577 len -= sg->length;
578 }
579
580 return false;
581}
582
583static inline void atmel_aes_restore_sg(const struct atmel_aes_dma *dma)
584{
585 struct scatterlist *sg = dma->sg;
586 int nents = dma->nents;
587
588 if (!dma->remainder)
589 return;
590
591 while (--nents > 0 && sg)
592 sg = sg_next(sg);
593
594 if (!sg)
595 return;
596
597 sg->length += dma->remainder;
598}
599
600static int atmel_aes_map(struct atmel_aes_dev *dd,
601 struct scatterlist *src,
602 struct scatterlist *dst,
603 size_t len)
604{
605 bool src_aligned, dst_aligned;
606 size_t padlen;
607
608 dd->total = len;
609 dd->src.sg = src;
610 dd->dst.sg = dst;
611 dd->real_dst = dst;
612
613 src_aligned = atmel_aes_check_aligned(dd, src, len, &dd->src);
614 if (src == dst)
615 dst_aligned = src_aligned;
616 else
617 dst_aligned = atmel_aes_check_aligned(dd, dst, len, &dd->dst);
618 if (!src_aligned || !dst_aligned) {
619 padlen = atmel_aes_padlen(len, dd->ctx->block_size);
620
621 if (dd->buflen < len + padlen)
622 return -ENOMEM;
623
624 if (!src_aligned) {
625 sg_copy_to_buffer(src, sg_nents(src), dd->buf, len);
626 dd->src.sg = &dd->aligned_sg;
627 dd->src.nents = 1;
628 dd->src.remainder = 0;
629 }
630
631 if (!dst_aligned) {
632 dd->dst.sg = &dd->aligned_sg;
633 dd->dst.nents = 1;
634 dd->dst.remainder = 0;
635 }
636
637 sg_init_table(&dd->aligned_sg, 1);
638 sg_set_buf(&dd->aligned_sg, dd->buf, len + padlen);
639 }
640
641 if (dd->src.sg == dd->dst.sg) {
642 dd->src.sg_len = dma_map_sg(dd->dev, dd->src.sg, dd->src.nents,
643 DMA_BIDIRECTIONAL);
644 dd->dst.sg_len = dd->src.sg_len;
645 if (!dd->src.sg_len)
646 return -EFAULT;
647 } else {
648 dd->src.sg_len = dma_map_sg(dd->dev, dd->src.sg, dd->src.nents,
649 DMA_TO_DEVICE);
650 if (!dd->src.sg_len)
651 return -EFAULT;
652
653 dd->dst.sg_len = dma_map_sg(dd->dev, dd->dst.sg, dd->dst.nents,
654 DMA_FROM_DEVICE);
655 if (!dd->dst.sg_len) {
656 dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
657 DMA_TO_DEVICE);
658 return -EFAULT;
659 }
660 }
661
662 return 0;
663}
664
665static void atmel_aes_unmap(struct atmel_aes_dev *dd)
666{
667 if (dd->src.sg == dd->dst.sg) {
668 dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
669 DMA_BIDIRECTIONAL);
670
671 if (dd->src.sg != &dd->aligned_sg)
672 atmel_aes_restore_sg(&dd->src);
673 } else {
674 dma_unmap_sg(dd->dev, dd->dst.sg, dd->dst.nents,
675 DMA_FROM_DEVICE);
676
677 if (dd->dst.sg != &dd->aligned_sg)
678 atmel_aes_restore_sg(&dd->dst);
679
680 dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
681 DMA_TO_DEVICE);
682
683 if (dd->src.sg != &dd->aligned_sg)
684 atmel_aes_restore_sg(&dd->src);
685 }
686
687 if (dd->dst.sg == &dd->aligned_sg)
688 sg_copy_from_buffer(dd->real_dst, sg_nents(dd->real_dst),
689 dd->buf, dd->total);
690}
691
692static int atmel_aes_dma_transfer_start(struct atmel_aes_dev *dd,
693 enum dma_slave_buswidth addr_width,
694 enum dma_transfer_direction dir,
695 u32 maxburst)
696{
697 struct dma_async_tx_descriptor *desc;
698 struct dma_slave_config config;
699 dma_async_tx_callback callback;
700 struct atmel_aes_dma *dma;
701 int err;
702
703 memset(&config, 0, sizeof(config));
704 config.direction = dir;
705 config.src_addr_width = addr_width;
706 config.dst_addr_width = addr_width;
707 config.src_maxburst = maxburst;
708 config.dst_maxburst = maxburst;
709
710 switch (dir) {
711 case DMA_MEM_TO_DEV:
712 dma = &dd->src;
713 callback = NULL;
714 config.dst_addr = dd->phys_base + AES_IDATAR(0);
715 break;
716
717 case DMA_DEV_TO_MEM:
718 dma = &dd->dst;
719 callback = atmel_aes_dma_callback;
720 config.src_addr = dd->phys_base + AES_ODATAR(0);
721 break;
722
723 default:
724 return -EINVAL;
725 }
726
727 err = dmaengine_slave_config(dma->chan, &config);
728 if (err)
729 return err;
730
731 desc = dmaengine_prep_slave_sg(dma->chan, dma->sg, dma->sg_len, dir,
732 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
733 if (!desc)
734 return -ENOMEM;
735
736 desc->callback = callback;
737 desc->callback_param = dd;
738 dmaengine_submit(desc);
739 dma_async_issue_pending(dma->chan);
740
741 return 0;
742}
743
744static void atmel_aes_dma_transfer_stop(struct atmel_aes_dev *dd,
745 enum dma_transfer_direction dir)
746{
747 struct atmel_aes_dma *dma;
748
749 switch (dir) {
750 case DMA_MEM_TO_DEV:
751 dma = &dd->src;
752 break;
753
754 case DMA_DEV_TO_MEM:
755 dma = &dd->dst;
756 break;
757
758 default:
759 return;
760 }
761
762 dmaengine_terminate_all(dma->chan);
763}
764
765static int atmel_aes_dma_start(struct atmel_aes_dev *dd,
766 struct scatterlist *src,
767 struct scatterlist *dst,
768 size_t len,
769 atmel_aes_fn_t resume)
770{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100771 enum dma_slave_buswidth addr_width;
772 u32 maxburst;
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100773 int err;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100774
775 switch (dd->ctx->block_size) {
776 case CFB8_BLOCK_SIZE:
777 addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
778 maxburst = 1;
779 break;
780
781 case CFB16_BLOCK_SIZE:
782 addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
783 maxburst = 1;
784 break;
785
786 case CFB32_BLOCK_SIZE:
787 case CFB64_BLOCK_SIZE:
788 addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
789 maxburst = 1;
790 break;
791
792 case AES_BLOCK_SIZE:
793 addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
794 maxburst = dd->caps.max_burst_size;
795 break;
796
797 default:
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100798 err = -EINVAL;
799 goto exit;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100800 }
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200801
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100802 err = atmel_aes_map(dd, src, dst, len);
803 if (err)
804 goto exit;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200805
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100806 dd->resume = resume;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200807
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100808 /* Set output DMA transfer first */
809 err = atmel_aes_dma_transfer_start(dd, addr_width, DMA_DEV_TO_MEM,
810 maxburst);
811 if (err)
812 goto unmap;
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100813
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100814 /* Then set input DMA transfer */
815 err = atmel_aes_dma_transfer_start(dd, addr_width, DMA_MEM_TO_DEV,
816 maxburst);
817 if (err)
818 goto output_transfer_stop;
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100819
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100820 return -EINPROGRESS;
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100821
822output_transfer_stop:
823 atmel_aes_dma_transfer_stop(dd, DMA_DEV_TO_MEM);
824unmap:
825 atmel_aes_unmap(dd);
826exit:
827 return atmel_aes_complete(dd, err);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200828}
829
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100830static void atmel_aes_dma_stop(struct atmel_aes_dev *dd)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200831{
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100832 atmel_aes_dma_transfer_stop(dd, DMA_MEM_TO_DEV);
833 atmel_aes_dma_transfer_stop(dd, DMA_DEV_TO_MEM);
834 atmel_aes_unmap(dd);
835}
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200836
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100837static void atmel_aes_dma_callback(void *data)
838{
839 struct atmel_aes_dev *dd = data;
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100840
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100841 atmel_aes_dma_stop(dd);
842 dd->is_async = true;
843 (void)dd->resume(dd);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200844}
845
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200846static int atmel_aes_handle_queue(struct atmel_aes_dev *dd,
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100847 struct crypto_async_request *new_areq)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200848{
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100849 struct crypto_async_request *areq, *backlog;
850 struct atmel_aes_base_ctx *ctx;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200851 unsigned long flags;
852 int err, ret = 0;
853
854 spin_lock_irqsave(&dd->lock, flags);
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100855 if (new_areq)
856 ret = crypto_enqueue_request(&dd->queue, new_areq);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200857 if (dd->flags & AES_FLAGS_BUSY) {
858 spin_unlock_irqrestore(&dd->lock, flags);
859 return ret;
860 }
861 backlog = crypto_get_backlog(&dd->queue);
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100862 areq = crypto_dequeue_request(&dd->queue);
863 if (areq)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200864 dd->flags |= AES_FLAGS_BUSY;
865 spin_unlock_irqrestore(&dd->lock, flags);
866
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100867 if (!areq)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200868 return ret;
869
870 if (backlog)
871 backlog->complete(backlog, -EINPROGRESS);
872
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100873 ctx = crypto_tfm_ctx(areq->tfm);
874
875 dd->areq = areq;
876 dd->ctx = ctx;
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100877 dd->is_async = (areq != new_areq);
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100878
879 err = ctx->start(dd);
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100880 return (dd->is_async) ? ret : err;
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100881}
882
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100883
884/* AES async block ciphers */
885
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100886static int atmel_aes_transfer_complete(struct atmel_aes_dev *dd)
887{
888 return atmel_aes_complete(dd, 0);
889}
890
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100891static int atmel_aes_start(struct atmel_aes_dev *dd)
892{
893 struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100894 struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req);
895 bool use_dma = (req->nbytes >= ATMEL_AES_DMA_THRESHOLD ||
896 dd->ctx->block_size != AES_BLOCK_SIZE);
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100897 int err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200898
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100899 atmel_aes_set_mode(dd, rctx);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200900
Cyrille Pitchencdfab4a2015-12-17 17:48:38 +0100901 err = atmel_aes_hw_init(dd);
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100902 if (err)
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100903 return atmel_aes_complete(dd, err);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200904
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100905 atmel_aes_write_ctrl(dd, use_dma, req->info);
906 if (use_dma)
907 return atmel_aes_dma_start(dd, req->src, req->dst, req->nbytes,
908 atmel_aes_transfer_complete);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200909
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100910 return atmel_aes_cpu_start(dd, req->src, req->dst, req->nbytes,
911 atmel_aes_transfer_complete);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200912}
913
Cyrille Pitchenfcac8362015-12-17 18:13:05 +0100914static inline struct atmel_aes_ctr_ctx *
915atmel_aes_ctr_ctx_cast(struct atmel_aes_base_ctx *ctx)
916{
917 return container_of(ctx, struct atmel_aes_ctr_ctx, base);
918}
919
920static int atmel_aes_ctr_transfer(struct atmel_aes_dev *dd)
921{
922 struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx);
923 struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
924 struct scatterlist *src, *dst;
Cyrille Pitchenfcac8362015-12-17 18:13:05 +0100925 size_t datalen;
Tudor Ambarusdf3168e2019-12-05 09:54:01 +0000926 u32 ctr;
927 u16 blocks, start, end;
Cyrille Pitchenfcac8362015-12-17 18:13:05 +0100928 bool use_dma, fragmented = false;
929
930 /* Check for transfer completion. */
931 ctx->offset += dd->total;
932 if (ctx->offset >= req->nbytes)
933 return atmel_aes_transfer_complete(dd);
934
935 /* Compute data length. */
936 datalen = req->nbytes - ctx->offset;
937 blocks = DIV_ROUND_UP(datalen, AES_BLOCK_SIZE);
938 ctr = be32_to_cpu(ctx->iv[3]);
Cyrille Pitchenfcac8362015-12-17 18:13:05 +0100939
Tudor Ambarusdf3168e2019-12-05 09:54:01 +0000940 /* Check 16bit counter overflow. */
941 start = ctr & 0xffff;
942 end = start + blocks - 1;
Cyrille Pitchenfcac8362015-12-17 18:13:05 +0100943
Tudor Ambarusdf3168e2019-12-05 09:54:01 +0000944 if (blocks >> 16 || end < start) {
945 ctr |= 0xffff;
946 datalen = AES_BLOCK_SIZE * (0x10000 - start);
947 fragmented = true;
Cyrille Pitchenfcac8362015-12-17 18:13:05 +0100948 }
Tudor Ambarusdf3168e2019-12-05 09:54:01 +0000949
Cyrille Pitchenfcac8362015-12-17 18:13:05 +0100950 use_dma = (datalen >= ATMEL_AES_DMA_THRESHOLD);
951
952 /* Jump to offset. */
953 src = scatterwalk_ffwd(ctx->src, req->src, ctx->offset);
954 dst = ((req->src == req->dst) ? src :
955 scatterwalk_ffwd(ctx->dst, req->dst, ctx->offset));
956
957 /* Configure hardware. */
958 atmel_aes_write_ctrl(dd, use_dma, ctx->iv);
959 if (unlikely(fragmented)) {
960 /*
961 * Increment the counter manually to cope with the hardware
962 * counter overflow.
963 */
964 ctx->iv[3] = cpu_to_be32(ctr);
965 crypto_inc((u8 *)ctx->iv, AES_BLOCK_SIZE);
966 }
967
968 if (use_dma)
969 return atmel_aes_dma_start(dd, src, dst, datalen,
970 atmel_aes_ctr_transfer);
971
972 return atmel_aes_cpu_start(dd, src, dst, datalen,
973 atmel_aes_ctr_transfer);
974}
975
976static int atmel_aes_ctr_start(struct atmel_aes_dev *dd)
977{
978 struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx);
979 struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
980 struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req);
981 int err;
982
983 atmel_aes_set_mode(dd, rctx);
984
985 err = atmel_aes_hw_init(dd);
986 if (err)
987 return atmel_aes_complete(dd, err);
988
989 memcpy(ctx->iv, req->info, AES_BLOCK_SIZE);
990 ctx->offset = 0;
991 dd->total = 0;
992 return atmel_aes_ctr_transfer(dd);
993}
994
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200995static int atmel_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
996{
Cyrille Pitchenafbac172015-12-17 18:13:02 +0100997 struct atmel_aes_base_ctx *ctx;
998 struct atmel_aes_reqctx *rctx;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200999 struct atmel_aes_dev *dd;
1000
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001001 ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001002 switch (mode & AES_FLAGS_OPMODE_MASK) {
1003 case AES_FLAGS_CFB8:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001004 ctx->block_size = CFB8_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001005 break;
1006
1007 case AES_FLAGS_CFB16:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001008 ctx->block_size = CFB16_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001009 break;
1010
1011 case AES_FLAGS_CFB32:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001012 ctx->block_size = CFB32_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001013 break;
1014
1015 case AES_FLAGS_CFB64:
Leilei Zhao9f849512014-04-22 15:23:24 +08001016 ctx->block_size = CFB64_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001017 break;
1018
1019 default:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001020 ctx->block_size = AES_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001021 break;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001022 }
1023
1024 dd = atmel_aes_find_dev(ctx);
1025 if (!dd)
1026 return -ENODEV;
1027
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001028 rctx = ablkcipher_request_ctx(req);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001029 rctx->mode = mode;
1030
Cyrille Pitchenccbf7292015-12-17 17:48:39 +01001031 return atmel_aes_handle_queue(dd, &req->base);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001032}
1033
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001034static int atmel_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
1035 unsigned int keylen)
1036{
Cyrille Pitchenccbf7292015-12-17 17:48:39 +01001037 struct atmel_aes_base_ctx *ctx = crypto_ablkcipher_ctx(tfm);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001038
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001039 if (keylen != AES_KEYSIZE_128 &&
1040 keylen != AES_KEYSIZE_192 &&
1041 keylen != AES_KEYSIZE_256) {
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001042 crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
1043 return -EINVAL;
1044 }
1045
1046 memcpy(ctx->key, key, keylen);
1047 ctx->keylen = keylen;
1048
1049 return 0;
1050}
1051
1052static int atmel_aes_ecb_encrypt(struct ablkcipher_request *req)
1053{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001054 return atmel_aes_crypt(req, AES_FLAGS_ECB | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001055}
1056
1057static int atmel_aes_ecb_decrypt(struct ablkcipher_request *req)
1058{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001059 return atmel_aes_crypt(req, AES_FLAGS_ECB);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001060}
1061
1062static int atmel_aes_cbc_encrypt(struct ablkcipher_request *req)
1063{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001064 return atmel_aes_crypt(req, AES_FLAGS_CBC | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001065}
1066
1067static int atmel_aes_cbc_decrypt(struct ablkcipher_request *req)
1068{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001069 return atmel_aes_crypt(req, AES_FLAGS_CBC);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001070}
1071
1072static int atmel_aes_ofb_encrypt(struct ablkcipher_request *req)
1073{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001074 return atmel_aes_crypt(req, AES_FLAGS_OFB | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001075}
1076
1077static int atmel_aes_ofb_decrypt(struct ablkcipher_request *req)
1078{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001079 return atmel_aes_crypt(req, AES_FLAGS_OFB);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001080}
1081
1082static int atmel_aes_cfb_encrypt(struct ablkcipher_request *req)
1083{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001084 return atmel_aes_crypt(req, AES_FLAGS_CFB128 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001085}
1086
1087static int atmel_aes_cfb_decrypt(struct ablkcipher_request *req)
1088{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001089 return atmel_aes_crypt(req, AES_FLAGS_CFB128);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001090}
1091
1092static int atmel_aes_cfb64_encrypt(struct ablkcipher_request *req)
1093{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001094 return atmel_aes_crypt(req, AES_FLAGS_CFB64 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001095}
1096
1097static int atmel_aes_cfb64_decrypt(struct ablkcipher_request *req)
1098{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001099 return atmel_aes_crypt(req, AES_FLAGS_CFB64);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001100}
1101
1102static int atmel_aes_cfb32_encrypt(struct ablkcipher_request *req)
1103{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001104 return atmel_aes_crypt(req, AES_FLAGS_CFB32 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001105}
1106
1107static int atmel_aes_cfb32_decrypt(struct ablkcipher_request *req)
1108{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001109 return atmel_aes_crypt(req, AES_FLAGS_CFB32);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001110}
1111
1112static int atmel_aes_cfb16_encrypt(struct ablkcipher_request *req)
1113{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001114 return atmel_aes_crypt(req, AES_FLAGS_CFB16 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001115}
1116
1117static int atmel_aes_cfb16_decrypt(struct ablkcipher_request *req)
1118{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001119 return atmel_aes_crypt(req, AES_FLAGS_CFB16);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001120}
1121
1122static int atmel_aes_cfb8_encrypt(struct ablkcipher_request *req)
1123{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001124 return atmel_aes_crypt(req, AES_FLAGS_CFB8 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001125}
1126
1127static int atmel_aes_cfb8_decrypt(struct ablkcipher_request *req)
1128{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001129 return atmel_aes_crypt(req, AES_FLAGS_CFB8);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001130}
1131
1132static int atmel_aes_ctr_encrypt(struct ablkcipher_request *req)
1133{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001134 return atmel_aes_crypt(req, AES_FLAGS_CTR | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001135}
1136
1137static int atmel_aes_ctr_decrypt(struct ablkcipher_request *req)
1138{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001139 return atmel_aes_crypt(req, AES_FLAGS_CTR);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001140}
1141
1142static int atmel_aes_cra_init(struct crypto_tfm *tfm)
1143{
Cyrille Pitchenccbf7292015-12-17 17:48:39 +01001144 struct atmel_aes_ctx *ctx = crypto_tfm_ctx(tfm);
1145
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001146 tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_aes_reqctx);
Cyrille Pitchenccbf7292015-12-17 17:48:39 +01001147 ctx->base.start = atmel_aes_start;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001148
1149 return 0;
1150}
1151
Cyrille Pitchenfcac8362015-12-17 18:13:05 +01001152static int atmel_aes_ctr_cra_init(struct crypto_tfm *tfm)
1153{
1154 struct atmel_aes_ctx *ctx = crypto_tfm_ctx(tfm);
1155
1156 tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_aes_reqctx);
1157 ctx->base.start = atmel_aes_ctr_start;
1158
1159 return 0;
1160}
1161
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001162static void atmel_aes_cra_exit(struct crypto_tfm *tfm)
1163{
1164}
1165
1166static struct crypto_alg aes_algs[] = {
1167{
1168 .cra_name = "ecb(aes)",
1169 .cra_driver_name = "atmel-ecb-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001170 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001171 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1172 .cra_blocksize = AES_BLOCK_SIZE,
1173 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001174 .cra_alignmask = 0xf,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001175 .cra_type = &crypto_ablkcipher_type,
1176 .cra_module = THIS_MODULE,
1177 .cra_init = atmel_aes_cra_init,
1178 .cra_exit = atmel_aes_cra_exit,
1179 .cra_u.ablkcipher = {
1180 .min_keysize = AES_MIN_KEY_SIZE,
1181 .max_keysize = AES_MAX_KEY_SIZE,
1182 .setkey = atmel_aes_setkey,
1183 .encrypt = atmel_aes_ecb_encrypt,
1184 .decrypt = atmel_aes_ecb_decrypt,
1185 }
1186},
1187{
1188 .cra_name = "cbc(aes)",
1189 .cra_driver_name = "atmel-cbc-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001190 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001191 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1192 .cra_blocksize = AES_BLOCK_SIZE,
1193 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001194 .cra_alignmask = 0xf,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001195 .cra_type = &crypto_ablkcipher_type,
1196 .cra_module = THIS_MODULE,
1197 .cra_init = atmel_aes_cra_init,
1198 .cra_exit = atmel_aes_cra_exit,
1199 .cra_u.ablkcipher = {
1200 .min_keysize = AES_MIN_KEY_SIZE,
1201 .max_keysize = AES_MAX_KEY_SIZE,
1202 .ivsize = AES_BLOCK_SIZE,
1203 .setkey = atmel_aes_setkey,
1204 .encrypt = atmel_aes_cbc_encrypt,
1205 .decrypt = atmel_aes_cbc_decrypt,
1206 }
1207},
1208{
1209 .cra_name = "ofb(aes)",
1210 .cra_driver_name = "atmel-ofb-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001211 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001212 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1213 .cra_blocksize = AES_BLOCK_SIZE,
1214 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001215 .cra_alignmask = 0xf,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001216 .cra_type = &crypto_ablkcipher_type,
1217 .cra_module = THIS_MODULE,
1218 .cra_init = atmel_aes_cra_init,
1219 .cra_exit = atmel_aes_cra_exit,
1220 .cra_u.ablkcipher = {
1221 .min_keysize = AES_MIN_KEY_SIZE,
1222 .max_keysize = AES_MAX_KEY_SIZE,
1223 .ivsize = AES_BLOCK_SIZE,
1224 .setkey = atmel_aes_setkey,
1225 .encrypt = atmel_aes_ofb_encrypt,
1226 .decrypt = atmel_aes_ofb_decrypt,
1227 }
1228},
1229{
1230 .cra_name = "cfb(aes)",
1231 .cra_driver_name = "atmel-cfb-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001232 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001233 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1234 .cra_blocksize = AES_BLOCK_SIZE,
1235 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001236 .cra_alignmask = 0xf,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001237 .cra_type = &crypto_ablkcipher_type,
1238 .cra_module = THIS_MODULE,
1239 .cra_init = atmel_aes_cra_init,
1240 .cra_exit = atmel_aes_cra_exit,
1241 .cra_u.ablkcipher = {
1242 .min_keysize = AES_MIN_KEY_SIZE,
1243 .max_keysize = AES_MAX_KEY_SIZE,
1244 .ivsize = AES_BLOCK_SIZE,
1245 .setkey = atmel_aes_setkey,
1246 .encrypt = atmel_aes_cfb_encrypt,
1247 .decrypt = atmel_aes_cfb_decrypt,
1248 }
1249},
1250{
1251 .cra_name = "cfb32(aes)",
1252 .cra_driver_name = "atmel-cfb32-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001253 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001254 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1255 .cra_blocksize = CFB32_BLOCK_SIZE,
1256 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001257 .cra_alignmask = 0x3,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001258 .cra_type = &crypto_ablkcipher_type,
1259 .cra_module = THIS_MODULE,
1260 .cra_init = atmel_aes_cra_init,
1261 .cra_exit = atmel_aes_cra_exit,
1262 .cra_u.ablkcipher = {
1263 .min_keysize = AES_MIN_KEY_SIZE,
1264 .max_keysize = AES_MAX_KEY_SIZE,
1265 .ivsize = AES_BLOCK_SIZE,
1266 .setkey = atmel_aes_setkey,
1267 .encrypt = atmel_aes_cfb32_encrypt,
1268 .decrypt = atmel_aes_cfb32_decrypt,
1269 }
1270},
1271{
1272 .cra_name = "cfb16(aes)",
1273 .cra_driver_name = "atmel-cfb16-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001274 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001275 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1276 .cra_blocksize = CFB16_BLOCK_SIZE,
1277 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001278 .cra_alignmask = 0x1,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001279 .cra_type = &crypto_ablkcipher_type,
1280 .cra_module = THIS_MODULE,
1281 .cra_init = atmel_aes_cra_init,
1282 .cra_exit = atmel_aes_cra_exit,
1283 .cra_u.ablkcipher = {
1284 .min_keysize = AES_MIN_KEY_SIZE,
1285 .max_keysize = AES_MAX_KEY_SIZE,
1286 .ivsize = AES_BLOCK_SIZE,
1287 .setkey = atmel_aes_setkey,
1288 .encrypt = atmel_aes_cfb16_encrypt,
1289 .decrypt = atmel_aes_cfb16_decrypt,
1290 }
1291},
1292{
1293 .cra_name = "cfb8(aes)",
1294 .cra_driver_name = "atmel-cfb8-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001295 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001296 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
Leilei Zhaoe5d8c962014-04-22 15:23:23 +08001297 .cra_blocksize = CFB8_BLOCK_SIZE,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001298 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
1299 .cra_alignmask = 0x0,
1300 .cra_type = &crypto_ablkcipher_type,
1301 .cra_module = THIS_MODULE,
1302 .cra_init = atmel_aes_cra_init,
1303 .cra_exit = atmel_aes_cra_exit,
1304 .cra_u.ablkcipher = {
1305 .min_keysize = AES_MIN_KEY_SIZE,
1306 .max_keysize = AES_MAX_KEY_SIZE,
1307 .ivsize = AES_BLOCK_SIZE,
1308 .setkey = atmel_aes_setkey,
1309 .encrypt = atmel_aes_cfb8_encrypt,
1310 .decrypt = atmel_aes_cfb8_decrypt,
1311 }
1312},
1313{
1314 .cra_name = "ctr(aes)",
1315 .cra_driver_name = "atmel-ctr-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001316 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001317 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
Cyrille Pitchenda7b8502015-12-17 18:13:04 +01001318 .cra_blocksize = 1,
Cyrille Pitchenfcac8362015-12-17 18:13:05 +01001319 .cra_ctxsize = sizeof(struct atmel_aes_ctr_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001320 .cra_alignmask = 0xf,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001321 .cra_type = &crypto_ablkcipher_type,
1322 .cra_module = THIS_MODULE,
Cyrille Pitchenfcac8362015-12-17 18:13:05 +01001323 .cra_init = atmel_aes_ctr_cra_init,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001324 .cra_exit = atmel_aes_cra_exit,
1325 .cra_u.ablkcipher = {
1326 .min_keysize = AES_MIN_KEY_SIZE,
1327 .max_keysize = AES_MAX_KEY_SIZE,
1328 .ivsize = AES_BLOCK_SIZE,
1329 .setkey = atmel_aes_setkey,
1330 .encrypt = atmel_aes_ctr_encrypt,
1331 .decrypt = atmel_aes_ctr_decrypt,
1332 }
1333},
1334};
1335
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001336static struct crypto_alg aes_cfb64_alg = {
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001337 .cra_name = "cfb64(aes)",
1338 .cra_driver_name = "atmel-cfb64-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001339 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001340 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1341 .cra_blocksize = CFB64_BLOCK_SIZE,
1342 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001343 .cra_alignmask = 0x7,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001344 .cra_type = &crypto_ablkcipher_type,
1345 .cra_module = THIS_MODULE,
1346 .cra_init = atmel_aes_cra_init,
1347 .cra_exit = atmel_aes_cra_exit,
1348 .cra_u.ablkcipher = {
1349 .min_keysize = AES_MIN_KEY_SIZE,
1350 .max_keysize = AES_MAX_KEY_SIZE,
1351 .ivsize = AES_BLOCK_SIZE,
1352 .setkey = atmel_aes_setkey,
1353 .encrypt = atmel_aes_cfb64_encrypt,
1354 .decrypt = atmel_aes_cfb64_decrypt,
1355 }
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001356};
1357
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01001358
Cyrille Pitchend4419542015-12-17 18:13:07 +01001359/* gcm aead functions */
1360
1361static int atmel_aes_gcm_ghash(struct atmel_aes_dev *dd,
1362 const u32 *data, size_t datalen,
1363 const u32 *ghash_in, u32 *ghash_out,
1364 atmel_aes_fn_t resume);
1365static int atmel_aes_gcm_ghash_init(struct atmel_aes_dev *dd);
1366static int atmel_aes_gcm_ghash_finalize(struct atmel_aes_dev *dd);
1367
1368static int atmel_aes_gcm_start(struct atmel_aes_dev *dd);
1369static int atmel_aes_gcm_process(struct atmel_aes_dev *dd);
1370static int atmel_aes_gcm_length(struct atmel_aes_dev *dd);
1371static int atmel_aes_gcm_data(struct atmel_aes_dev *dd);
1372static int atmel_aes_gcm_tag_init(struct atmel_aes_dev *dd);
1373static int atmel_aes_gcm_tag(struct atmel_aes_dev *dd);
1374static int atmel_aes_gcm_finalize(struct atmel_aes_dev *dd);
1375
1376static inline struct atmel_aes_gcm_ctx *
1377atmel_aes_gcm_ctx_cast(struct atmel_aes_base_ctx *ctx)
1378{
1379 return container_of(ctx, struct atmel_aes_gcm_ctx, base);
1380}
1381
1382static int atmel_aes_gcm_ghash(struct atmel_aes_dev *dd,
1383 const u32 *data, size_t datalen,
1384 const u32 *ghash_in, u32 *ghash_out,
1385 atmel_aes_fn_t resume)
1386{
1387 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1388
1389 dd->data = (u32 *)data;
1390 dd->datalen = datalen;
1391 ctx->ghash_in = ghash_in;
1392 ctx->ghash_out = ghash_out;
1393 ctx->ghash_resume = resume;
1394
1395 atmel_aes_write_ctrl(dd, false, NULL);
1396 return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_ghash_init);
1397}
1398
1399static int atmel_aes_gcm_ghash_init(struct atmel_aes_dev *dd)
1400{
1401 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1402
1403 /* Set the data length. */
1404 atmel_aes_write(dd, AES_AADLENR, dd->total);
1405 atmel_aes_write(dd, AES_CLENR, 0);
1406
1407 /* If needed, overwrite the GCM Intermediate Hash Word Registers */
1408 if (ctx->ghash_in)
1409 atmel_aes_write_block(dd, AES_GHASHR(0), ctx->ghash_in);
1410
1411 return atmel_aes_gcm_ghash_finalize(dd);
1412}
1413
1414static int atmel_aes_gcm_ghash_finalize(struct atmel_aes_dev *dd)
1415{
1416 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1417 u32 isr;
1418
1419 /* Write data into the Input Data Registers. */
1420 while (dd->datalen > 0) {
1421 atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
1422 dd->data += 4;
1423 dd->datalen -= AES_BLOCK_SIZE;
1424
1425 isr = atmel_aes_read(dd, AES_ISR);
1426 if (!(isr & AES_INT_DATARDY)) {
1427 dd->resume = atmel_aes_gcm_ghash_finalize;
1428 atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
1429 return -EINPROGRESS;
1430 }
1431 }
1432
1433 /* Read the computed hash from GHASHRx. */
1434 atmel_aes_read_block(dd, AES_GHASHR(0), ctx->ghash_out);
1435
1436 return ctx->ghash_resume(dd);
1437}
1438
1439
1440static int atmel_aes_gcm_start(struct atmel_aes_dev *dd)
1441{
1442 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1443 struct aead_request *req = aead_request_cast(dd->areq);
1444 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1445 struct atmel_aes_reqctx *rctx = aead_request_ctx(req);
1446 size_t ivsize = crypto_aead_ivsize(tfm);
1447 size_t datalen, padlen;
1448 const void *iv = req->iv;
1449 u8 *data = dd->buf;
1450 int err;
1451
1452 atmel_aes_set_mode(dd, rctx);
1453
1454 err = atmel_aes_hw_init(dd);
1455 if (err)
1456 return atmel_aes_complete(dd, err);
1457
1458 if (likely(ivsize == 12)) {
1459 memcpy(ctx->j0, iv, ivsize);
1460 ctx->j0[3] = cpu_to_be32(1);
1461 return atmel_aes_gcm_process(dd);
1462 }
1463
1464 padlen = atmel_aes_padlen(ivsize, AES_BLOCK_SIZE);
1465 datalen = ivsize + padlen + AES_BLOCK_SIZE;
1466 if (datalen > dd->buflen)
1467 return atmel_aes_complete(dd, -EINVAL);
1468
1469 memcpy(data, iv, ivsize);
1470 memset(data + ivsize, 0, padlen + sizeof(u64));
1471 ((u64 *)(data + datalen))[-1] = cpu_to_be64(ivsize * 8);
1472
1473 return atmel_aes_gcm_ghash(dd, (const u32 *)data, datalen,
1474 NULL, ctx->j0, atmel_aes_gcm_process);
1475}
1476
1477static int atmel_aes_gcm_process(struct atmel_aes_dev *dd)
1478{
1479 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1480 struct aead_request *req = aead_request_cast(dd->areq);
1481 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1482 bool enc = atmel_aes_is_encrypt(dd);
1483 u32 authsize;
1484
1485 /* Compute text length. */
1486 authsize = crypto_aead_authsize(tfm);
1487 ctx->textlen = req->cryptlen - (enc ? 0 : authsize);
1488
1489 /*
1490 * According to tcrypt test suite, the GCM Automatic Tag Generation
1491 * fails when both the message and its associated data are empty.
1492 */
1493 if (likely(req->assoclen != 0 || ctx->textlen != 0))
1494 dd->flags |= AES_FLAGS_GTAGEN;
1495
1496 atmel_aes_write_ctrl(dd, false, NULL);
1497 return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_length);
1498}
1499
1500static int atmel_aes_gcm_length(struct atmel_aes_dev *dd)
1501{
1502 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1503 struct aead_request *req = aead_request_cast(dd->areq);
1504 u32 j0_lsw, *j0 = ctx->j0;
1505 size_t padlen;
1506
1507 /* Write incr32(J0) into IV. */
1508 j0_lsw = j0[3];
1509 j0[3] = cpu_to_be32(be32_to_cpu(j0[3]) + 1);
1510 atmel_aes_write_block(dd, AES_IVR(0), j0);
1511 j0[3] = j0_lsw;
1512
1513 /* Set aad and text lengths. */
1514 atmel_aes_write(dd, AES_AADLENR, req->assoclen);
1515 atmel_aes_write(dd, AES_CLENR, ctx->textlen);
1516
1517 /* Check whether AAD are present. */
1518 if (unlikely(req->assoclen == 0)) {
1519 dd->datalen = 0;
1520 return atmel_aes_gcm_data(dd);
1521 }
1522
1523 /* Copy assoc data and add padding. */
1524 padlen = atmel_aes_padlen(req->assoclen, AES_BLOCK_SIZE);
1525 if (unlikely(req->assoclen + padlen > dd->buflen))
1526 return atmel_aes_complete(dd, -EINVAL);
1527 sg_copy_to_buffer(req->src, sg_nents(req->src), dd->buf, req->assoclen);
1528
1529 /* Write assoc data into the Input Data register. */
1530 dd->data = (u32 *)dd->buf;
1531 dd->datalen = req->assoclen + padlen;
1532 return atmel_aes_gcm_data(dd);
1533}
1534
1535static int atmel_aes_gcm_data(struct atmel_aes_dev *dd)
1536{
1537 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1538 struct aead_request *req = aead_request_cast(dd->areq);
1539 bool use_dma = (ctx->textlen >= ATMEL_AES_DMA_THRESHOLD);
1540 struct scatterlist *src, *dst;
1541 u32 isr, mr;
1542
1543 /* Write AAD first. */
1544 while (dd->datalen > 0) {
1545 atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
1546 dd->data += 4;
1547 dd->datalen -= AES_BLOCK_SIZE;
1548
1549 isr = atmel_aes_read(dd, AES_ISR);
1550 if (!(isr & AES_INT_DATARDY)) {
1551 dd->resume = atmel_aes_gcm_data;
1552 atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
1553 return -EINPROGRESS;
1554 }
1555 }
1556
1557 /* GMAC only. */
1558 if (unlikely(ctx->textlen == 0))
1559 return atmel_aes_gcm_tag_init(dd);
1560
1561 /* Prepare src and dst scatter lists to transfer cipher/plain texts */
1562 src = scatterwalk_ffwd(ctx->src, req->src, req->assoclen);
1563 dst = ((req->src == req->dst) ? src :
1564 scatterwalk_ffwd(ctx->dst, req->dst, req->assoclen));
1565
1566 if (use_dma) {
1567 /* Update the Mode Register for DMA transfers. */
1568 mr = atmel_aes_read(dd, AES_MR);
1569 mr &= ~(AES_MR_SMOD_MASK | AES_MR_DUALBUFF);
1570 mr |= AES_MR_SMOD_IDATAR0;
1571 if (dd->caps.has_dualbuff)
1572 mr |= AES_MR_DUALBUFF;
1573 atmel_aes_write(dd, AES_MR, mr);
1574
1575 return atmel_aes_dma_start(dd, src, dst, ctx->textlen,
1576 atmel_aes_gcm_tag_init);
1577 }
1578
1579 return atmel_aes_cpu_start(dd, src, dst, ctx->textlen,
1580 atmel_aes_gcm_tag_init);
1581}
1582
1583static int atmel_aes_gcm_tag_init(struct atmel_aes_dev *dd)
1584{
1585 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1586 struct aead_request *req = aead_request_cast(dd->areq);
1587 u64 *data = dd->buf;
1588
1589 if (likely(dd->flags & AES_FLAGS_GTAGEN)) {
1590 if (!(atmel_aes_read(dd, AES_ISR) & AES_INT_TAGRDY)) {
1591 dd->resume = atmel_aes_gcm_tag_init;
1592 atmel_aes_write(dd, AES_IER, AES_INT_TAGRDY);
1593 return -EINPROGRESS;
1594 }
1595
1596 return atmel_aes_gcm_finalize(dd);
1597 }
1598
1599 /* Read the GCM Intermediate Hash Word Registers. */
1600 atmel_aes_read_block(dd, AES_GHASHR(0), ctx->ghash);
1601
1602 data[0] = cpu_to_be64(req->assoclen * 8);
1603 data[1] = cpu_to_be64(ctx->textlen * 8);
1604
1605 return atmel_aes_gcm_ghash(dd, (const u32 *)data, AES_BLOCK_SIZE,
1606 ctx->ghash, ctx->ghash, atmel_aes_gcm_tag);
1607}
1608
1609static int atmel_aes_gcm_tag(struct atmel_aes_dev *dd)
1610{
1611 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1612 unsigned long flags;
1613
1614 /*
1615 * Change mode to CTR to complete the tag generation.
1616 * Use J0 as Initialization Vector.
1617 */
1618 flags = dd->flags;
1619 dd->flags &= ~(AES_FLAGS_OPMODE_MASK | AES_FLAGS_GTAGEN);
1620 dd->flags |= AES_FLAGS_CTR;
1621 atmel_aes_write_ctrl(dd, false, ctx->j0);
1622 dd->flags = flags;
1623
1624 atmel_aes_write_block(dd, AES_IDATAR(0), ctx->ghash);
1625 return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_finalize);
1626}
1627
1628static int atmel_aes_gcm_finalize(struct atmel_aes_dev *dd)
1629{
1630 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1631 struct aead_request *req = aead_request_cast(dd->areq);
1632 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1633 bool enc = atmel_aes_is_encrypt(dd);
1634 u32 offset, authsize, itag[4], *otag = ctx->tag;
1635 int err;
1636
1637 /* Read the computed tag. */
1638 if (likely(dd->flags & AES_FLAGS_GTAGEN))
1639 atmel_aes_read_block(dd, AES_TAGR(0), ctx->tag);
1640 else
1641 atmel_aes_read_block(dd, AES_ODATAR(0), ctx->tag);
1642
1643 offset = req->assoclen + ctx->textlen;
1644 authsize = crypto_aead_authsize(tfm);
1645 if (enc) {
1646 scatterwalk_map_and_copy(otag, req->dst, offset, authsize, 1);
1647 err = 0;
1648 } else {
1649 scatterwalk_map_and_copy(itag, req->src, offset, authsize, 0);
1650 err = crypto_memneq(itag, otag, authsize) ? -EBADMSG : 0;
1651 }
1652
1653 return atmel_aes_complete(dd, err);
1654}
1655
1656static int atmel_aes_gcm_crypt(struct aead_request *req,
1657 unsigned long mode)
1658{
1659 struct atmel_aes_base_ctx *ctx;
1660 struct atmel_aes_reqctx *rctx;
1661 struct atmel_aes_dev *dd;
1662
1663 ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
1664 ctx->block_size = AES_BLOCK_SIZE;
1665
1666 dd = atmel_aes_find_dev(ctx);
1667 if (!dd)
1668 return -ENODEV;
1669
1670 rctx = aead_request_ctx(req);
1671 rctx->mode = AES_FLAGS_GCM | mode;
1672
1673 return atmel_aes_handle_queue(dd, &req->base);
1674}
1675
1676static int atmel_aes_gcm_setkey(struct crypto_aead *tfm, const u8 *key,
1677 unsigned int keylen)
1678{
1679 struct atmel_aes_base_ctx *ctx = crypto_aead_ctx(tfm);
1680
1681 if (keylen != AES_KEYSIZE_256 &&
1682 keylen != AES_KEYSIZE_192 &&
1683 keylen != AES_KEYSIZE_128) {
1684 crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
1685 return -EINVAL;
1686 }
1687
1688 memcpy(ctx->key, key, keylen);
1689 ctx->keylen = keylen;
1690
1691 return 0;
1692}
1693
1694static int atmel_aes_gcm_setauthsize(struct crypto_aead *tfm,
1695 unsigned int authsize)
1696{
1697 /* Same as crypto_gcm_authsize() from crypto/gcm.c */
1698 switch (authsize) {
1699 case 4:
1700 case 8:
1701 case 12:
1702 case 13:
1703 case 14:
1704 case 15:
1705 case 16:
1706 break;
1707 default:
1708 return -EINVAL;
1709 }
1710
1711 return 0;
1712}
1713
1714static int atmel_aes_gcm_encrypt(struct aead_request *req)
1715{
1716 return atmel_aes_gcm_crypt(req, AES_FLAGS_ENCRYPT);
1717}
1718
1719static int atmel_aes_gcm_decrypt(struct aead_request *req)
1720{
1721 return atmel_aes_gcm_crypt(req, 0);
1722}
1723
1724static int atmel_aes_gcm_init(struct crypto_aead *tfm)
1725{
1726 struct atmel_aes_gcm_ctx *ctx = crypto_aead_ctx(tfm);
1727
1728 crypto_aead_set_reqsize(tfm, sizeof(struct atmel_aes_reqctx));
1729 ctx->base.start = atmel_aes_gcm_start;
1730
1731 return 0;
1732}
1733
1734static void atmel_aes_gcm_exit(struct crypto_aead *tfm)
1735{
1736
1737}
1738
1739static struct aead_alg aes_gcm_alg = {
1740 .setkey = atmel_aes_gcm_setkey,
1741 .setauthsize = atmel_aes_gcm_setauthsize,
1742 .encrypt = atmel_aes_gcm_encrypt,
1743 .decrypt = atmel_aes_gcm_decrypt,
1744 .init = atmel_aes_gcm_init,
1745 .exit = atmel_aes_gcm_exit,
1746 .ivsize = 12,
1747 .maxauthsize = AES_BLOCK_SIZE,
1748
1749 .base = {
1750 .cra_name = "gcm(aes)",
1751 .cra_driver_name = "atmel-gcm-aes",
1752 .cra_priority = ATMEL_AES_PRIORITY,
1753 .cra_flags = CRYPTO_ALG_ASYNC,
1754 .cra_blocksize = 1,
1755 .cra_ctxsize = sizeof(struct atmel_aes_gcm_ctx),
1756 .cra_alignmask = 0xf,
1757 .cra_module = THIS_MODULE,
1758 },
1759};
1760
1761
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01001762/* Probe functions */
1763
1764static int atmel_aes_buff_init(struct atmel_aes_dev *dd)
1765{
1766 dd->buf = (void *)__get_free_pages(GFP_KERNEL, ATMEL_AES_BUFFER_ORDER);
1767 dd->buflen = ATMEL_AES_BUFFER_SIZE;
1768 dd->buflen &= ~(AES_BLOCK_SIZE - 1);
1769
1770 if (!dd->buf) {
1771 dev_err(dd->dev, "unable to alloc pages.\n");
1772 return -ENOMEM;
1773 }
1774
1775 return 0;
1776}
1777
1778static void atmel_aes_buff_cleanup(struct atmel_aes_dev *dd)
1779{
1780 free_page((unsigned long)dd->buf);
1781}
1782
1783static bool atmel_aes_filter(struct dma_chan *chan, void *slave)
1784{
1785 struct at_dma_slave *sl = slave;
1786
1787 if (sl && sl->dma_dev == chan->device->dev) {
1788 chan->private = sl;
1789 return true;
1790 } else {
1791 return false;
1792 }
1793}
1794
1795static int atmel_aes_dma_init(struct atmel_aes_dev *dd,
1796 struct crypto_platform_data *pdata)
1797{
1798 struct at_dma_slave *slave;
1799 int err = -ENOMEM;
1800 dma_cap_mask_t mask;
1801
1802 dma_cap_zero(mask);
1803 dma_cap_set(DMA_SLAVE, mask);
1804
1805 /* Try to grab 2 DMA channels */
1806 slave = &pdata->dma_slave->rxdata;
1807 dd->src.chan = dma_request_slave_channel_compat(mask, atmel_aes_filter,
1808 slave, dd->dev, "tx");
1809 if (!dd->src.chan)
1810 goto err_dma_in;
1811
1812 slave = &pdata->dma_slave->txdata;
1813 dd->dst.chan = dma_request_slave_channel_compat(mask, atmel_aes_filter,
1814 slave, dd->dev, "rx");
1815 if (!dd->dst.chan)
1816 goto err_dma_out;
1817
1818 return 0;
1819
1820err_dma_out:
1821 dma_release_channel(dd->src.chan);
1822err_dma_in:
1823 dev_warn(dd->dev, "no DMA channel available\n");
1824 return err;
1825}
1826
1827static void atmel_aes_dma_cleanup(struct atmel_aes_dev *dd)
1828{
1829 dma_release_channel(dd->dst.chan);
1830 dma_release_channel(dd->src.chan);
1831}
1832
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001833static void atmel_aes_queue_task(unsigned long data)
1834{
1835 struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data;
1836
1837 atmel_aes_handle_queue(dd, NULL);
1838}
1839
1840static void atmel_aes_done_task(unsigned long data)
1841{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001842 struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data;
Cyrille Pitchen10f12c12015-12-17 17:48:42 +01001843
1844 dd->is_async = true;
1845 (void)dd->resume(dd);
1846}
1847
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001848static irqreturn_t atmel_aes_irq(int irq, void *dev_id)
1849{
1850 struct atmel_aes_dev *aes_dd = dev_id;
1851 u32 reg;
1852
1853 reg = atmel_aes_read(aes_dd, AES_ISR);
1854 if (reg & atmel_aes_read(aes_dd, AES_IMR)) {
1855 atmel_aes_write(aes_dd, AES_IDR, reg);
1856 if (AES_FLAGS_BUSY & aes_dd->flags)
1857 tasklet_schedule(&aes_dd->done_task);
1858 else
1859 dev_warn(aes_dd->dev, "AES interrupt when no active requests.\n");
1860 return IRQ_HANDLED;
1861 }
1862
1863 return IRQ_NONE;
1864}
1865
1866static void atmel_aes_unregister_algs(struct atmel_aes_dev *dd)
1867{
1868 int i;
1869
Cyrille Pitchend4419542015-12-17 18:13:07 +01001870 if (dd->caps.has_gcm)
1871 crypto_unregister_aead(&aes_gcm_alg);
1872
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001873 if (dd->caps.has_cfb64)
1874 crypto_unregister_alg(&aes_cfb64_alg);
Cyrille Pitchen924a8bc2015-12-17 17:48:35 +01001875
1876 for (i = 0; i < ARRAY_SIZE(aes_algs); i++)
1877 crypto_unregister_alg(&aes_algs[i]);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001878}
1879
1880static int atmel_aes_register_algs(struct atmel_aes_dev *dd)
1881{
1882 int err, i, j;
1883
1884 for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001885 err = crypto_register_alg(&aes_algs[i]);
1886 if (err)
1887 goto err_aes_algs;
1888 }
1889
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001890 if (dd->caps.has_cfb64) {
1891 err = crypto_register_alg(&aes_cfb64_alg);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001892 if (err)
1893 goto err_aes_cfb64_alg;
1894 }
1895
Cyrille Pitchend4419542015-12-17 18:13:07 +01001896 if (dd->caps.has_gcm) {
1897 err = crypto_register_aead(&aes_gcm_alg);
1898 if (err)
1899 goto err_aes_gcm_alg;
1900 }
1901
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001902 return 0;
1903
Cyrille Pitchend4419542015-12-17 18:13:07 +01001904err_aes_gcm_alg:
1905 crypto_unregister_alg(&aes_cfb64_alg);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001906err_aes_cfb64_alg:
1907 i = ARRAY_SIZE(aes_algs);
1908err_aes_algs:
1909 for (j = 0; j < i; j++)
1910 crypto_unregister_alg(&aes_algs[j]);
1911
1912 return err;
1913}
1914
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001915static void atmel_aes_get_cap(struct atmel_aes_dev *dd)
1916{
1917 dd->caps.has_dualbuff = 0;
1918 dd->caps.has_cfb64 = 0;
Cyrille Pitchend4419542015-12-17 18:13:07 +01001919 dd->caps.has_gcm = 0;
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001920 dd->caps.max_burst_size = 1;
1921
1922 /* keep only major version number */
1923 switch (dd->hw_version & 0xff0) {
Leilei Zhao973e2092015-12-17 17:48:32 +01001924 case 0x500:
1925 dd->caps.has_dualbuff = 1;
1926 dd->caps.has_cfb64 = 1;
Cyrille Pitchend4419542015-12-17 18:13:07 +01001927 dd->caps.has_gcm = 1;
Leilei Zhao973e2092015-12-17 17:48:32 +01001928 dd->caps.max_burst_size = 4;
1929 break;
Leilei Zhaocf1f0d12015-04-07 17:45:02 +08001930 case 0x200:
1931 dd->caps.has_dualbuff = 1;
1932 dd->caps.has_cfb64 = 1;
Cyrille Pitchend4419542015-12-17 18:13:07 +01001933 dd->caps.has_gcm = 1;
Leilei Zhaocf1f0d12015-04-07 17:45:02 +08001934 dd->caps.max_burst_size = 4;
1935 break;
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001936 case 0x130:
1937 dd->caps.has_dualbuff = 1;
1938 dd->caps.has_cfb64 = 1;
1939 dd->caps.max_burst_size = 4;
1940 break;
1941 case 0x120:
1942 break;
1943 default:
1944 dev_warn(dd->dev,
1945 "Unmanaged aes version, set minimum capabilities\n");
1946 break;
1947 }
1948}
1949
Nicolas Ferrebe943c72013-10-14 17:52:38 +02001950#if defined(CONFIG_OF)
1951static const struct of_device_id atmel_aes_dt_ids[] = {
1952 { .compatible = "atmel,at91sam9g46-aes" },
1953 { /* sentinel */ }
1954};
1955MODULE_DEVICE_TABLE(of, atmel_aes_dt_ids);
1956
1957static struct crypto_platform_data *atmel_aes_of_init(struct platform_device *pdev)
1958{
1959 struct device_node *np = pdev->dev.of_node;
1960 struct crypto_platform_data *pdata;
1961
1962 if (!np) {
1963 dev_err(&pdev->dev, "device node not found\n");
1964 return ERR_PTR(-EINVAL);
1965 }
1966
1967 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1968 if (!pdata) {
1969 dev_err(&pdev->dev, "could not allocate memory for pdata\n");
1970 return ERR_PTR(-ENOMEM);
1971 }
1972
1973 pdata->dma_slave = devm_kzalloc(&pdev->dev,
1974 sizeof(*(pdata->dma_slave)),
1975 GFP_KERNEL);
1976 if (!pdata->dma_slave) {
1977 dev_err(&pdev->dev, "could not allocate memory for dma_slave\n");
1978 devm_kfree(&pdev->dev, pdata);
1979 return ERR_PTR(-ENOMEM);
1980 }
1981
1982 return pdata;
1983}
1984#else
1985static inline struct crypto_platform_data *atmel_aes_of_init(struct platform_device *pdev)
1986{
1987 return ERR_PTR(-EINVAL);
1988}
1989#endif
1990
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08001991static int atmel_aes_probe(struct platform_device *pdev)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001992{
1993 struct atmel_aes_dev *aes_dd;
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001994 struct crypto_platform_data *pdata;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001995 struct device *dev = &pdev->dev;
1996 struct resource *aes_res;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001997 int err;
1998
1999 pdata = pdev->dev.platform_data;
2000 if (!pdata) {
Nicolas Ferrebe943c72013-10-14 17:52:38 +02002001 pdata = atmel_aes_of_init(pdev);
2002 if (IS_ERR(pdata)) {
2003 err = PTR_ERR(pdata);
2004 goto aes_dd_err;
2005 }
2006 }
2007
2008 if (!pdata->dma_slave) {
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002009 err = -ENXIO;
2010 goto aes_dd_err;
2011 }
2012
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002013 aes_dd = devm_kzalloc(&pdev->dev, sizeof(*aes_dd), GFP_KERNEL);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002014 if (aes_dd == NULL) {
2015 dev_err(dev, "unable to alloc data struct.\n");
2016 err = -ENOMEM;
2017 goto aes_dd_err;
2018 }
2019
2020 aes_dd->dev = dev;
2021
2022 platform_set_drvdata(pdev, aes_dd);
2023
2024 INIT_LIST_HEAD(&aes_dd->list);
Leilei Zhao8a10eb82015-04-07 17:45:09 +08002025 spin_lock_init(&aes_dd->lock);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002026
2027 tasklet_init(&aes_dd->done_task, atmel_aes_done_task,
2028 (unsigned long)aes_dd);
2029 tasklet_init(&aes_dd->queue_task, atmel_aes_queue_task,
2030 (unsigned long)aes_dd);
2031
2032 crypto_init_queue(&aes_dd->queue, ATMEL_AES_QUEUE_LENGTH);
2033
2034 aes_dd->irq = -1;
2035
2036 /* Get the base address */
2037 aes_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2038 if (!aes_res) {
2039 dev_err(dev, "no MEM resource info\n");
2040 err = -ENODEV;
2041 goto res_err;
2042 }
2043 aes_dd->phys_base = aes_res->start;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002044
2045 /* Get the IRQ */
2046 aes_dd->irq = platform_get_irq(pdev, 0);
2047 if (aes_dd->irq < 0) {
2048 dev_err(dev, "no IRQ resource info\n");
2049 err = aes_dd->irq;
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002050 goto res_err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002051 }
2052
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002053 err = devm_request_irq(&pdev->dev, aes_dd->irq, atmel_aes_irq,
2054 IRQF_SHARED, "atmel-aes", aes_dd);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002055 if (err) {
2056 dev_err(dev, "unable to request aes irq.\n");
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002057 goto res_err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002058 }
2059
2060 /* Initializing the clock */
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002061 aes_dd->iclk = devm_clk_get(&pdev->dev, "aes_clk");
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002062 if (IS_ERR(aes_dd->iclk)) {
Colin Ian Kingbe208352015-02-28 20:40:10 +00002063 dev_err(dev, "clock initialization failed.\n");
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002064 err = PTR_ERR(aes_dd->iclk);
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002065 goto res_err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002066 }
2067
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002068 aes_dd->io_base = devm_ioremap_resource(&pdev->dev, aes_res);
Vladimir Zapolskiy9b52d552016-03-06 03:21:52 +02002069 if (IS_ERR(aes_dd->io_base)) {
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002070 dev_err(dev, "can't ioremap\n");
Vladimir Zapolskiy9b52d552016-03-06 03:21:52 +02002071 err = PTR_ERR(aes_dd->io_base);
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002072 goto res_err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002073 }
2074
Cyrille Pitchen49a20452016-01-29 17:53:33 +01002075 err = clk_prepare(aes_dd->iclk);
Cyrille Pitchenaab0a392015-12-17 17:48:37 +01002076 if (err)
2077 goto res_err;
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002078
Cyrille Pitchen49a20452016-01-29 17:53:33 +01002079 err = atmel_aes_hw_version_init(aes_dd);
2080 if (err)
2081 goto iclk_unprepare;
2082
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002083 atmel_aes_get_cap(aes_dd);
2084
2085 err = atmel_aes_buff_init(aes_dd);
2086 if (err)
2087 goto err_aes_buff;
2088
2089 err = atmel_aes_dma_init(aes_dd, pdata);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002090 if (err)
2091 goto err_aes_dma;
2092
2093 spin_lock(&atmel_aes.lock);
2094 list_add_tail(&aes_dd->list, &atmel_aes.dev_list);
2095 spin_unlock(&atmel_aes.lock);
2096
2097 err = atmel_aes_register_algs(aes_dd);
2098 if (err)
2099 goto err_algs;
2100
Nicolas Ferrebe943c72013-10-14 17:52:38 +02002101 dev_info(dev, "Atmel AES - Using %s, %s for DMA transfers\n",
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +01002102 dma_chan_name(aes_dd->src.chan),
2103 dma_chan_name(aes_dd->dst.chan));
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002104
2105 return 0;
2106
2107err_algs:
2108 spin_lock(&atmel_aes.lock);
2109 list_del(&aes_dd->list);
2110 spin_unlock(&atmel_aes.lock);
2111 atmel_aes_dma_cleanup(aes_dd);
2112err_aes_dma:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002113 atmel_aes_buff_cleanup(aes_dd);
2114err_aes_buff:
Cyrille Pitchen49a20452016-01-29 17:53:33 +01002115iclk_unprepare:
2116 clk_unprepare(aes_dd->iclk);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002117res_err:
2118 tasklet_kill(&aes_dd->done_task);
2119 tasklet_kill(&aes_dd->queue_task);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002120aes_dd_err:
2121 dev_err(dev, "initialization failed.\n");
2122
2123 return err;
2124}
2125
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002126static int atmel_aes_remove(struct platform_device *pdev)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002127{
2128 static struct atmel_aes_dev *aes_dd;
2129
2130 aes_dd = platform_get_drvdata(pdev);
2131 if (!aes_dd)
2132 return -ENODEV;
2133 spin_lock(&atmel_aes.lock);
2134 list_del(&aes_dd->list);
2135 spin_unlock(&atmel_aes.lock);
2136
2137 atmel_aes_unregister_algs(aes_dd);
2138
2139 tasklet_kill(&aes_dd->done_task);
2140 tasklet_kill(&aes_dd->queue_task);
2141
2142 atmel_aes_dma_cleanup(aes_dd);
Cyrille Pitchen2a377822015-12-17 17:48:46 +01002143 atmel_aes_buff_cleanup(aes_dd);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002144
Cyrille Pitchen49a20452016-01-29 17:53:33 +01002145 clk_unprepare(aes_dd->iclk);
2146
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002147 return 0;
2148}
2149
2150static struct platform_driver atmel_aes_driver = {
2151 .probe = atmel_aes_probe,
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002152 .remove = atmel_aes_remove,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002153 .driver = {
2154 .name = "atmel_aes",
Nicolas Ferrebe943c72013-10-14 17:52:38 +02002155 .of_match_table = of_match_ptr(atmel_aes_dt_ids),
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002156 },
2157};
2158
2159module_platform_driver(atmel_aes_driver);
2160
2161MODULE_DESCRIPTION("Atmel AES hw acceleration support.");
2162MODULE_LICENSE("GPL v2");
2163MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");