blob: b001fdcd9d9537d0807e52b0b36144f858834b21 [file] [log] [blame]
Tudor Ambarus820684c2018-08-21 16:36:09 +03001// SPDX-License-Identifier: GPL-2.0
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002/*
3 * Cryptographic API.
4 *
5 * Support for ATMEL AES HW acceleration.
6 *
7 * Copyright (c) 2012 Eukréa Electromatique - ATMEL
8 * Author: Nicolas Royer <nicolas@eukrea.com>
9 *
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020010 * Some ideas are from omap-aes.c driver.
11 */
12
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/err.h>
18#include <linux/clk.h>
19#include <linux/io.h>
20#include <linux/hw_random.h>
21#include <linux/platform_device.h>
22
23#include <linux/device.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020024#include <linux/init.h>
25#include <linux/errno.h>
26#include <linux/interrupt.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020027#include <linux/irq.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020028#include <linux/scatterlist.h>
29#include <linux/dma-mapping.h>
Nicolas Ferrebe943c72013-10-14 17:52:38 +020030#include <linux/of_device.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020031#include <linux/delay.h>
32#include <linux/crypto.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020033#include <crypto/scatterwalk.h>
34#include <crypto/algapi.h>
35#include <crypto/aes.h>
Corentin LABBE219d51c2017-08-22 10:08:12 +020036#include <crypto/gcm.h>
Cyrille Pitchend52db512016-10-03 14:33:16 +020037#include <crypto/xts.h>
Cyrille Pitchend4419542015-12-17 18:13:07 +010038#include <crypto/internal/aead.h>
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +010039#include <crypto/internal/skcipher.h>
Nicolas Royercadc4ab2013-02-20 17:10:24 +010040#include <linux/platform_data/crypto-atmel.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020041#include "atmel-aes-regs.h"
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +010042#include "atmel-authenc.h"
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020043
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
Cyrille Pitchend52db512016-10-03 14:33:16 +020071#define AES_FLAGS_XTS AES_MR_OPMOD_XTS
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020072
Cyrille Pitchen77dacf52015-12-17 17:48:41 +010073#define AES_FLAGS_MODE_MASK (AES_FLAGS_OPMODE_MASK | \
Cyrille Pitchend4419542015-12-17 18:13:07 +010074 AES_FLAGS_ENCRYPT | \
75 AES_FLAGS_GTAGEN)
Cyrille Pitchen77dacf52015-12-17 17:48:41 +010076
Cyrille Pitchen77dacf52015-12-17 17:48:41 +010077#define AES_FLAGS_BUSY BIT(3)
Cyrille Pitchen45379922015-12-17 18:13:08 +010078#define AES_FLAGS_DUMP_REG BIT(4)
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +010079#define AES_FLAGS_OWN_SHA BIT(5)
Cyrille Pitchen77dacf52015-12-17 17:48:41 +010080
Romain Izard7a373fd2017-10-31 16:25:24 +010081#define AES_FLAGS_PERSISTENT AES_FLAGS_BUSY
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020082
Nicolas Royercadc4ab2013-02-20 17:10:24 +010083#define ATMEL_AES_QUEUE_LENGTH 50
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020084
Cyrille Pitchen129f8bb2015-12-17 18:13:06 +010085#define ATMEL_AES_DMA_THRESHOLD 256
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020086
87
Nicolas Royercadc4ab2013-02-20 17:10:24 +010088struct atmel_aes_caps {
Cyrille Pitchenafbac172015-12-17 18:13:02 +010089 bool has_dualbuff;
90 bool has_cfb64;
Cyrille Pitchend4419542015-12-17 18:13:07 +010091 bool has_gcm;
Cyrille Pitchend52db512016-10-03 14:33:16 +020092 bool has_xts;
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +010093 bool has_authenc;
Cyrille Pitchenafbac172015-12-17 18:13:02 +010094 u32 max_burst_size;
Nicolas Royercadc4ab2013-02-20 17:10:24 +010095};
96
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020097struct atmel_aes_dev;
98
Cyrille Pitchenccbf7292015-12-17 17:48:39 +010099
100typedef int (*atmel_aes_fn_t)(struct atmel_aes_dev *);
101
102
103struct atmel_aes_base_ctx {
Cyrille Pitchenafbac172015-12-17 18:13:02 +0100104 struct atmel_aes_dev *dd;
105 atmel_aes_fn_t start;
106 int keylen;
107 u32 key[AES_KEYSIZE_256 / sizeof(u32)];
108 u16 block_size;
Romain Izard91308012017-10-31 16:25:23 +0100109 bool is_aead;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200110};
111
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100112struct atmel_aes_ctx {
113 struct atmel_aes_base_ctx base;
114};
115
Cyrille Pitchenfcac83652015-12-17 18:13:05 +0100116struct atmel_aes_ctr_ctx {
117 struct atmel_aes_base_ctx base;
118
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +0100119 __be32 iv[AES_BLOCK_SIZE / sizeof(u32)];
Cyrille Pitchenfcac83652015-12-17 18:13:05 +0100120 size_t offset;
121 struct scatterlist src[2];
122 struct scatterlist dst[2];
Tudor Ambarus3907ccf2019-12-13 14:45:44 +0000123 u32 blocks;
Cyrille Pitchenfcac83652015-12-17 18:13:05 +0100124};
125
Cyrille Pitchend4419542015-12-17 18:13:07 +0100126struct atmel_aes_gcm_ctx {
127 struct atmel_aes_base_ctx base;
128
129 struct scatterlist src[2];
130 struct scatterlist dst[2];
131
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +0100132 __be32 j0[AES_BLOCK_SIZE / sizeof(u32)];
Cyrille Pitchend4419542015-12-17 18:13:07 +0100133 u32 tag[AES_BLOCK_SIZE / sizeof(u32)];
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +0100134 __be32 ghash[AES_BLOCK_SIZE / sizeof(u32)];
Cyrille Pitchend4419542015-12-17 18:13:07 +0100135 size_t textlen;
136
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +0100137 const __be32 *ghash_in;
138 __be32 *ghash_out;
Cyrille Pitchend4419542015-12-17 18:13:07 +0100139 atmel_aes_fn_t ghash_resume;
140};
141
Cyrille Pitchend52db512016-10-03 14:33:16 +0200142struct atmel_aes_xts_ctx {
143 struct atmel_aes_base_ctx base;
144
145 u32 key2[AES_KEYSIZE_256 / sizeof(u32)];
146};
147
Herbert Xu1520c722019-10-28 15:39:07 +0800148#if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +0100149struct atmel_aes_authenc_ctx {
150 struct atmel_aes_base_ctx base;
151 struct atmel_sha_authenc_ctx *auth;
152};
153#endif
154
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200155struct atmel_aes_reqctx {
Cyrille Pitchenafbac172015-12-17 18:13:02 +0100156 unsigned long mode;
Tudor Ambarus57d81542019-11-15 13:49:09 +0000157 u8 lastc[AES_BLOCK_SIZE];
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200158};
159
Herbert Xu1520c722019-10-28 15:39:07 +0800160#if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +0100161struct atmel_aes_authenc_reqctx {
162 struct atmel_aes_reqctx base;
163
164 struct scatterlist src[2];
165 struct scatterlist dst[2];
166 size_t textlen;
167 u32 digest[SHA512_DIGEST_SIZE / sizeof(u32)];
168
169 /* auth_req MUST be place last. */
170 struct ahash_request auth_req;
171};
172#endif
173
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200174struct atmel_aes_dma {
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100175 struct dma_chan *chan;
176 struct scatterlist *sg;
177 int nents;
178 unsigned int remainder;
179 unsigned int sg_len;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200180};
181
182struct atmel_aes_dev {
183 struct list_head list;
184 unsigned long phys_base;
185 void __iomem *io_base;
186
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100187 struct crypto_async_request *areq;
188 struct atmel_aes_base_ctx *ctx;
189
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100190 bool is_async;
191 atmel_aes_fn_t resume;
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100192 atmel_aes_fn_t cpu_transfer_complete;
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100193
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200194 struct device *dev;
195 struct clk *iclk;
Cyrille Pitchenafbac172015-12-17 18:13:02 +0100196 int irq;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200197
198 unsigned long flags;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200199
200 spinlock_t lock;
201 struct crypto_queue queue;
202
203 struct tasklet_struct done_task;
204 struct tasklet_struct queue_task;
205
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100206 size_t total;
207 size_t datalen;
208 u32 *data;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200209
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100210 struct atmel_aes_dma src;
211 struct atmel_aes_dma dst;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200212
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100213 size_t buflen;
214 void *buf;
215 struct scatterlist aligned_sg;
216 struct scatterlist *real_dst;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200217
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100218 struct atmel_aes_caps caps;
219
Cyrille Pitchenafbac172015-12-17 18:13:02 +0100220 u32 hw_version;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200221};
222
223struct atmel_aes_drv {
224 struct list_head dev_list;
225 spinlock_t lock;
226};
227
228static struct atmel_aes_drv atmel_aes = {
229 .dev_list = LIST_HEAD_INIT(atmel_aes.dev_list),
230 .lock = __SPIN_LOCK_UNLOCKED(atmel_aes.lock),
231};
232
Cyrille Pitchen45379922015-12-17 18:13:08 +0100233#ifdef VERBOSE_DEBUG
234static const char *atmel_aes_reg_name(u32 offset, char *tmp, size_t sz)
235{
236 switch (offset) {
237 case AES_CR:
238 return "CR";
239
240 case AES_MR:
241 return "MR";
242
243 case AES_ISR:
244 return "ISR";
245
246 case AES_IMR:
247 return "IMR";
248
249 case AES_IER:
250 return "IER";
251
252 case AES_IDR:
253 return "IDR";
254
255 case AES_KEYWR(0):
256 case AES_KEYWR(1):
257 case AES_KEYWR(2):
258 case AES_KEYWR(3):
259 case AES_KEYWR(4):
260 case AES_KEYWR(5):
261 case AES_KEYWR(6):
262 case AES_KEYWR(7):
263 snprintf(tmp, sz, "KEYWR[%u]", (offset - AES_KEYWR(0)) >> 2);
264 break;
265
266 case AES_IDATAR(0):
267 case AES_IDATAR(1):
268 case AES_IDATAR(2):
269 case AES_IDATAR(3):
270 snprintf(tmp, sz, "IDATAR[%u]", (offset - AES_IDATAR(0)) >> 2);
271 break;
272
273 case AES_ODATAR(0):
274 case AES_ODATAR(1):
275 case AES_ODATAR(2):
276 case AES_ODATAR(3):
277 snprintf(tmp, sz, "ODATAR[%u]", (offset - AES_ODATAR(0)) >> 2);
278 break;
279
280 case AES_IVR(0):
281 case AES_IVR(1):
282 case AES_IVR(2):
283 case AES_IVR(3):
284 snprintf(tmp, sz, "IVR[%u]", (offset - AES_IVR(0)) >> 2);
285 break;
286
287 case AES_AADLENR:
288 return "AADLENR";
289
290 case AES_CLENR:
291 return "CLENR";
292
293 case AES_GHASHR(0):
294 case AES_GHASHR(1):
295 case AES_GHASHR(2):
296 case AES_GHASHR(3):
297 snprintf(tmp, sz, "GHASHR[%u]", (offset - AES_GHASHR(0)) >> 2);
298 break;
299
300 case AES_TAGR(0):
301 case AES_TAGR(1):
302 case AES_TAGR(2):
303 case AES_TAGR(3):
304 snprintf(tmp, sz, "TAGR[%u]", (offset - AES_TAGR(0)) >> 2);
305 break;
306
307 case AES_CTRR:
308 return "CTRR";
309
310 case AES_GCMHR(0):
311 case AES_GCMHR(1):
312 case AES_GCMHR(2):
313 case AES_GCMHR(3):
314 snprintf(tmp, sz, "GCMHR[%u]", (offset - AES_GCMHR(0)) >> 2);
Herbert Xue31835a2016-01-19 09:05:43 +0800315 break;
Cyrille Pitchen45379922015-12-17 18:13:08 +0100316
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +0100317 case AES_EMR:
318 return "EMR";
319
Cyrille Pitchend52db512016-10-03 14:33:16 +0200320 case AES_TWR(0):
321 case AES_TWR(1):
322 case AES_TWR(2):
323 case AES_TWR(3):
324 snprintf(tmp, sz, "TWR[%u]", (offset - AES_TWR(0)) >> 2);
325 break;
326
327 case AES_ALPHAR(0):
328 case AES_ALPHAR(1):
329 case AES_ALPHAR(2):
330 case AES_ALPHAR(3):
331 snprintf(tmp, sz, "ALPHAR[%u]", (offset - AES_ALPHAR(0)) >> 2);
332 break;
333
Cyrille Pitchen45379922015-12-17 18:13:08 +0100334 default:
335 snprintf(tmp, sz, "0x%02x", offset);
336 break;
337 }
338
339 return tmp;
340}
341#endif /* VERBOSE_DEBUG */
342
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100343/* Shared functions */
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100344
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200345static inline u32 atmel_aes_read(struct atmel_aes_dev *dd, u32 offset)
346{
Cyrille Pitchen45379922015-12-17 18:13:08 +0100347 u32 value = readl_relaxed(dd->io_base + offset);
348
349#ifdef VERBOSE_DEBUG
350 if (dd->flags & AES_FLAGS_DUMP_REG) {
351 char tmp[16];
352
353 dev_vdbg(dd->dev, "read 0x%08x from %s\n", value,
354 atmel_aes_reg_name(offset, tmp, sizeof(tmp)));
355 }
356#endif /* VERBOSE_DEBUG */
357
358 return value;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200359}
360
361static inline void atmel_aes_write(struct atmel_aes_dev *dd,
362 u32 offset, u32 value)
363{
Cyrille Pitchen45379922015-12-17 18:13:08 +0100364#ifdef VERBOSE_DEBUG
365 if (dd->flags & AES_FLAGS_DUMP_REG) {
366 char tmp[16];
367
368 dev_vdbg(dd->dev, "write 0x%08x into %s\n", value,
Cyrille Pitchenf709dc82016-09-29 18:46:57 +0200369 atmel_aes_reg_name(offset, tmp, sizeof(tmp)));
Cyrille Pitchen45379922015-12-17 18:13:08 +0100370 }
371#endif /* VERBOSE_DEBUG */
372
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200373 writel_relaxed(value, dd->io_base + offset);
374}
375
376static void atmel_aes_read_n(struct atmel_aes_dev *dd, u32 offset,
377 u32 *value, int count)
378{
379 for (; count--; value++, offset += 4)
380 *value = atmel_aes_read(dd, offset);
381}
382
383static void atmel_aes_write_n(struct atmel_aes_dev *dd, u32 offset,
Cyrille Pitchenc0b28d82015-12-17 17:48:33 +0100384 const u32 *value, int count)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200385{
386 for (; count--; value++, offset += 4)
387 atmel_aes_write(dd, offset, *value);
388}
389
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100390static inline void atmel_aes_read_block(struct atmel_aes_dev *dd, u32 offset,
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +0100391 void *value)
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100392{
393 atmel_aes_read_n(dd, offset, value, SIZE_IN_WORDS(AES_BLOCK_SIZE));
394}
395
396static inline void atmel_aes_write_block(struct atmel_aes_dev *dd, u32 offset,
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +0100397 const void *value)
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100398{
399 atmel_aes_write_n(dd, offset, value, SIZE_IN_WORDS(AES_BLOCK_SIZE));
400}
401
402static inline int atmel_aes_wait_for_data_ready(struct atmel_aes_dev *dd,
403 atmel_aes_fn_t resume)
404{
405 u32 isr = atmel_aes_read(dd, AES_ISR);
406
407 if (unlikely(isr & AES_INT_DATARDY))
408 return resume(dd);
409
410 dd->resume = resume;
411 atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
412 return -EINPROGRESS;
413}
414
415static inline size_t atmel_aes_padlen(size_t len, size_t block_size)
416{
417 len &= block_size - 1;
418 return len ? block_size - len : 0;
419}
420
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100421static struct atmel_aes_dev *atmel_aes_find_dev(struct atmel_aes_base_ctx *ctx)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200422{
423 struct atmel_aes_dev *aes_dd = NULL;
424 struct atmel_aes_dev *tmp;
425
426 spin_lock_bh(&atmel_aes.lock);
427 if (!ctx->dd) {
428 list_for_each_entry(tmp, &atmel_aes.dev_list, list) {
429 aes_dd = tmp;
430 break;
431 }
432 ctx->dd = aes_dd;
433 } else {
434 aes_dd = ctx->dd;
435 }
436
437 spin_unlock_bh(&atmel_aes.lock);
438
439 return aes_dd;
440}
441
442static int atmel_aes_hw_init(struct atmel_aes_dev *dd)
443{
LABBE Corentin9d83d292015-10-02 14:12:58 +0200444 int err;
445
Cyrille Pitchen49a20452016-01-29 17:53:33 +0100446 err = clk_enable(dd->iclk);
LABBE Corentin9d83d292015-10-02 14:12:58 +0200447 if (err)
448 return err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200449
Romain Izard7a373fd2017-10-31 16:25:24 +0100450 atmel_aes_write(dd, AES_CR, AES_CR_SWRST);
451 atmel_aes_write(dd, AES_MR, 0xE << AES_MR_CKEY_OFFSET);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200452
453 return 0;
454}
455
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100456static inline unsigned int atmel_aes_get_version(struct atmel_aes_dev *dd)
457{
458 return atmel_aes_read(dd, AES_HW_VERSION) & 0x00000fff;
459}
460
Cyrille Pitchenaab0a392015-12-17 17:48:37 +0100461static int atmel_aes_hw_version_init(struct atmel_aes_dev *dd)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200462{
Cyrille Pitchenaab0a392015-12-17 17:48:37 +0100463 int err;
464
465 err = atmel_aes_hw_init(dd);
466 if (err)
467 return err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200468
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100469 dd->hw_version = atmel_aes_get_version(dd);
470
Cyrille Pitchenaab0a392015-12-17 17:48:37 +0100471 dev_info(dd->dev, "version: 0x%x\n", dd->hw_version);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200472
Cyrille Pitchen49a20452016-01-29 17:53:33 +0100473 clk_disable(dd->iclk);
Cyrille Pitchenaab0a392015-12-17 17:48:37 +0100474 return 0;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200475}
476
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100477static inline void atmel_aes_set_mode(struct atmel_aes_dev *dd,
478 const struct atmel_aes_reqctx *rctx)
479{
480 /* Clear all but persistent flags and set request flags. */
481 dd->flags = (dd->flags & AES_FLAGS_PERSISTENT) | rctx->mode;
482}
483
Cyrille Pitchend4419542015-12-17 18:13:07 +0100484static inline bool atmel_aes_is_encrypt(const struct atmel_aes_dev *dd)
485{
486 return (dd->flags & AES_FLAGS_ENCRYPT);
487}
488
Herbert Xu1520c722019-10-28 15:39:07 +0800489#if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +0100490static void atmel_aes_authenc_complete(struct atmel_aes_dev *dd, int err);
491#endif
492
Tudor Ambarus86ef1df2019-10-04 08:55:37 +0000493static void atmel_aes_set_iv_as_last_ciphertext_block(struct atmel_aes_dev *dd)
494{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +0100495 struct skcipher_request *req = skcipher_request_cast(dd->areq);
496 struct atmel_aes_reqctx *rctx = skcipher_request_ctx(req);
497 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
498 unsigned int ivsize = crypto_skcipher_ivsize(skcipher);
Tudor Ambarus86ef1df2019-10-04 08:55:37 +0000499
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +0100500 if (req->cryptlen < ivsize)
Tudor Ambarus86ef1df2019-10-04 08:55:37 +0000501 return;
502
503 if (rctx->mode & AES_FLAGS_ENCRYPT) {
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +0100504 scatterwalk_map_and_copy(req->iv, req->dst,
505 req->cryptlen - ivsize, ivsize, 0);
Tudor Ambarus86ef1df2019-10-04 08:55:37 +0000506 } else {
507 if (req->src == req->dst)
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +0100508 memcpy(req->iv, rctx->lastc, ivsize);
Tudor Ambarus86ef1df2019-10-04 08:55:37 +0000509 else
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +0100510 scatterwalk_map_and_copy(req->iv, req->src,
511 req->cryptlen - ivsize,
Tudor Ambarus86ef1df2019-10-04 08:55:37 +0000512 ivsize, 0);
513 }
514}
515
Tudor Ambarus371731e2019-12-05 09:54:03 +0000516static inline struct atmel_aes_ctr_ctx *
517atmel_aes_ctr_ctx_cast(struct atmel_aes_base_ctx *ctx)
518{
519 return container_of(ctx, struct atmel_aes_ctr_ctx, base);
520}
521
522static void atmel_aes_ctr_update_req_iv(struct atmel_aes_dev *dd)
523{
524 struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx);
525 struct skcipher_request *req = skcipher_request_cast(dd->areq);
526 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
527 unsigned int ivsize = crypto_skcipher_ivsize(skcipher);
528 int i;
529
Tudor Ambarus3907ccf2019-12-13 14:45:44 +0000530 /*
531 * The CTR transfer works in fragments of data of maximum 1 MByte
532 * because of the 16 bit CTR counter embedded in the IP. When reaching
533 * here, ctx->blocks contains the number of blocks of the last fragment
534 * processed, there is no need to explicit cast it to u16.
535 */
Tudor Ambarus371731e2019-12-05 09:54:03 +0000536 for (i = 0; i < ctx->blocks; i++)
537 crypto_inc((u8 *)ctx->iv, AES_BLOCK_SIZE);
538
539 memcpy(req->iv, ctx->iv, ivsize);
540}
541
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100542static inline int atmel_aes_complete(struct atmel_aes_dev *dd, int err)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200543{
Tudor Ambarusc65d1232019-12-05 09:54:00 +0000544 struct skcipher_request *req = skcipher_request_cast(dd->areq);
545 struct atmel_aes_reqctx *rctx = skcipher_request_ctx(req);
546
Herbert Xu1520c722019-10-28 15:39:07 +0800547#if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
Romain Izard91308012017-10-31 16:25:23 +0100548 if (dd->ctx->is_aead)
549 atmel_aes_authenc_complete(dd, err);
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +0100550#endif
551
Cyrille Pitchen49a20452016-01-29 17:53:33 +0100552 clk_disable(dd->iclk);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200553 dd->flags &= ~AES_FLAGS_BUSY;
554
Tudor Ambarus27f4adf2019-12-13 09:54:56 +0000555 if (!err && !dd->ctx->is_aead &&
Tudor Ambarus371731e2019-12-05 09:54:03 +0000556 (rctx->mode & AES_FLAGS_OPMODE_MASK) != AES_FLAGS_ECB) {
557 if ((rctx->mode & AES_FLAGS_OPMODE_MASK) != AES_FLAGS_CTR)
558 atmel_aes_set_iv_as_last_ciphertext_block(dd);
559 else
560 atmel_aes_ctr_update_req_iv(dd);
561 }
Romain Izard91308012017-10-31 16:25:23 +0100562
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100563 if (dd->is_async)
564 dd->areq->complete(dd->areq, err);
565
566 tasklet_schedule(&dd->queue_task);
567
568 return err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200569}
570
Cyrille Pitchend52db512016-10-03 14:33:16 +0200571static void atmel_aes_write_ctrl_key(struct atmel_aes_dev *dd, bool use_dma,
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +0100572 const __be32 *iv, const u32 *key, int keylen)
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100573{
574 u32 valmr = 0;
575
576 /* MR register must be set before IV registers */
Cyrille Pitchend52db512016-10-03 14:33:16 +0200577 if (keylen == AES_KEYSIZE_128)
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100578 valmr |= AES_MR_KEYSIZE_128;
Cyrille Pitchend52db512016-10-03 14:33:16 +0200579 else if (keylen == AES_KEYSIZE_192)
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100580 valmr |= AES_MR_KEYSIZE_192;
581 else
582 valmr |= AES_MR_KEYSIZE_256;
583
584 valmr |= dd->flags & AES_FLAGS_MODE_MASK;
585
586 if (use_dma) {
587 valmr |= AES_MR_SMOD_IDATAR0;
588 if (dd->caps.has_dualbuff)
589 valmr |= AES_MR_DUALBUFF;
590 } else {
591 valmr |= AES_MR_SMOD_AUTO;
592 }
593
594 atmel_aes_write(dd, AES_MR, valmr);
595
Cyrille Pitchend52db512016-10-03 14:33:16 +0200596 atmel_aes_write_n(dd, AES_KEYWR(0), key, SIZE_IN_WORDS(keylen));
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100597
598 if (iv && (valmr & AES_MR_OPMOD_MASK) != AES_MR_OPMOD_ECB)
599 atmel_aes_write_block(dd, AES_IVR(0), iv);
600}
601
Cyrille Pitchend52db512016-10-03 14:33:16 +0200602static inline void atmel_aes_write_ctrl(struct atmel_aes_dev *dd, bool use_dma,
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +0100603 const __be32 *iv)
Cyrille Pitchend52db512016-10-03 14:33:16 +0200604
605{
606 atmel_aes_write_ctrl_key(dd, use_dma, iv,
607 dd->ctx->key, dd->ctx->keylen);
608}
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200609
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100610/* CPU transfer */
611
612static int atmel_aes_cpu_transfer(struct atmel_aes_dev *dd)
613{
614 int err = 0;
615 u32 isr;
616
617 for (;;) {
618 atmel_aes_read_block(dd, AES_ODATAR(0), dd->data);
619 dd->data += 4;
620 dd->datalen -= AES_BLOCK_SIZE;
621
622 if (dd->datalen < AES_BLOCK_SIZE)
623 break;
624
625 atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
626
627 isr = atmel_aes_read(dd, AES_ISR);
628 if (!(isr & AES_INT_DATARDY)) {
629 dd->resume = atmel_aes_cpu_transfer;
630 atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
631 return -EINPROGRESS;
632 }
633 }
634
635 if (!sg_copy_from_buffer(dd->real_dst, sg_nents(dd->real_dst),
636 dd->buf, dd->total))
637 err = -EINVAL;
638
639 if (err)
640 return atmel_aes_complete(dd, err);
641
642 return dd->cpu_transfer_complete(dd);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200643}
644
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100645static int atmel_aes_cpu_start(struct atmel_aes_dev *dd,
646 struct scatterlist *src,
647 struct scatterlist *dst,
648 size_t len,
649 atmel_aes_fn_t resume)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200650{
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100651 size_t padlen = atmel_aes_padlen(len, AES_BLOCK_SIZE);
652
653 if (unlikely(len == 0))
654 return -EINVAL;
655
656 sg_copy_to_buffer(src, sg_nents(src), dd->buf, len);
657
658 dd->total = len;
659 dd->real_dst = dst;
660 dd->cpu_transfer_complete = resume;
661 dd->datalen = len + padlen;
662 dd->data = (u32 *)dd->buf;
663 atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
664 return atmel_aes_wait_for_data_ready(dd, atmel_aes_cpu_transfer);
665}
666
667
668/* DMA transfer */
669
670static void atmel_aes_dma_callback(void *data);
671
672static bool atmel_aes_check_aligned(struct atmel_aes_dev *dd,
673 struct scatterlist *sg,
674 size_t len,
675 struct atmel_aes_dma *dma)
676{
677 int nents;
678
679 if (!IS_ALIGNED(len, dd->ctx->block_size))
680 return false;
681
682 for (nents = 0; sg; sg = sg_next(sg), ++nents) {
683 if (!IS_ALIGNED(sg->offset, sizeof(u32)))
684 return false;
685
686 if (len <= sg->length) {
687 if (!IS_ALIGNED(len, dd->ctx->block_size))
688 return false;
689
690 dma->nents = nents+1;
691 dma->remainder = sg->length - len;
692 sg->length = len;
693 return true;
694 }
695
696 if (!IS_ALIGNED(sg->length, dd->ctx->block_size))
697 return false;
698
699 len -= sg->length;
700 }
701
702 return false;
703}
704
705static inline void atmel_aes_restore_sg(const struct atmel_aes_dma *dma)
706{
707 struct scatterlist *sg = dma->sg;
708 int nents = dma->nents;
709
710 if (!dma->remainder)
711 return;
712
713 while (--nents > 0 && sg)
714 sg = sg_next(sg);
715
716 if (!sg)
717 return;
718
719 sg->length += dma->remainder;
720}
721
722static int atmel_aes_map(struct atmel_aes_dev *dd,
723 struct scatterlist *src,
724 struct scatterlist *dst,
725 size_t len)
726{
727 bool src_aligned, dst_aligned;
728 size_t padlen;
729
730 dd->total = len;
731 dd->src.sg = src;
732 dd->dst.sg = dst;
733 dd->real_dst = dst;
734
735 src_aligned = atmel_aes_check_aligned(dd, src, len, &dd->src);
736 if (src == dst)
737 dst_aligned = src_aligned;
738 else
739 dst_aligned = atmel_aes_check_aligned(dd, dst, len, &dd->dst);
740 if (!src_aligned || !dst_aligned) {
741 padlen = atmel_aes_padlen(len, dd->ctx->block_size);
742
743 if (dd->buflen < len + padlen)
744 return -ENOMEM;
745
746 if (!src_aligned) {
747 sg_copy_to_buffer(src, sg_nents(src), dd->buf, len);
748 dd->src.sg = &dd->aligned_sg;
749 dd->src.nents = 1;
750 dd->src.remainder = 0;
751 }
752
753 if (!dst_aligned) {
754 dd->dst.sg = &dd->aligned_sg;
755 dd->dst.nents = 1;
756 dd->dst.remainder = 0;
757 }
758
759 sg_init_table(&dd->aligned_sg, 1);
760 sg_set_buf(&dd->aligned_sg, dd->buf, len + padlen);
761 }
762
763 if (dd->src.sg == dd->dst.sg) {
764 dd->src.sg_len = dma_map_sg(dd->dev, dd->src.sg, dd->src.nents,
765 DMA_BIDIRECTIONAL);
766 dd->dst.sg_len = dd->src.sg_len;
767 if (!dd->src.sg_len)
768 return -EFAULT;
769 } else {
770 dd->src.sg_len = dma_map_sg(dd->dev, dd->src.sg, dd->src.nents,
771 DMA_TO_DEVICE);
772 if (!dd->src.sg_len)
773 return -EFAULT;
774
775 dd->dst.sg_len = dma_map_sg(dd->dev, dd->dst.sg, dd->dst.nents,
776 DMA_FROM_DEVICE);
777 if (!dd->dst.sg_len) {
778 dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
779 DMA_TO_DEVICE);
780 return -EFAULT;
781 }
782 }
783
784 return 0;
785}
786
787static void atmel_aes_unmap(struct atmel_aes_dev *dd)
788{
789 if (dd->src.sg == dd->dst.sg) {
790 dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
791 DMA_BIDIRECTIONAL);
792
793 if (dd->src.sg != &dd->aligned_sg)
794 atmel_aes_restore_sg(&dd->src);
795 } else {
796 dma_unmap_sg(dd->dev, dd->dst.sg, dd->dst.nents,
797 DMA_FROM_DEVICE);
798
799 if (dd->dst.sg != &dd->aligned_sg)
800 atmel_aes_restore_sg(&dd->dst);
801
802 dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
803 DMA_TO_DEVICE);
804
805 if (dd->src.sg != &dd->aligned_sg)
806 atmel_aes_restore_sg(&dd->src);
807 }
808
809 if (dd->dst.sg == &dd->aligned_sg)
810 sg_copy_from_buffer(dd->real_dst, sg_nents(dd->real_dst),
811 dd->buf, dd->total);
812}
813
814static int atmel_aes_dma_transfer_start(struct atmel_aes_dev *dd,
815 enum dma_slave_buswidth addr_width,
816 enum dma_transfer_direction dir,
817 u32 maxburst)
818{
819 struct dma_async_tx_descriptor *desc;
820 struct dma_slave_config config;
821 dma_async_tx_callback callback;
822 struct atmel_aes_dma *dma;
823 int err;
824
825 memset(&config, 0, sizeof(config));
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100826 config.src_addr_width = addr_width;
827 config.dst_addr_width = addr_width;
828 config.src_maxburst = maxburst;
829 config.dst_maxburst = maxburst;
830
831 switch (dir) {
832 case DMA_MEM_TO_DEV:
833 dma = &dd->src;
834 callback = NULL;
835 config.dst_addr = dd->phys_base + AES_IDATAR(0);
836 break;
837
838 case DMA_DEV_TO_MEM:
839 dma = &dd->dst;
840 callback = atmel_aes_dma_callback;
841 config.src_addr = dd->phys_base + AES_ODATAR(0);
842 break;
843
844 default:
845 return -EINVAL;
846 }
847
848 err = dmaengine_slave_config(dma->chan, &config);
849 if (err)
850 return err;
851
852 desc = dmaengine_prep_slave_sg(dma->chan, dma->sg, dma->sg_len, dir,
853 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
854 if (!desc)
855 return -ENOMEM;
856
857 desc->callback = callback;
858 desc->callback_param = dd;
859 dmaengine_submit(desc);
860 dma_async_issue_pending(dma->chan);
861
862 return 0;
863}
864
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100865static int atmel_aes_dma_start(struct atmel_aes_dev *dd,
866 struct scatterlist *src,
867 struct scatterlist *dst,
868 size_t len,
869 atmel_aes_fn_t resume)
870{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100871 enum dma_slave_buswidth addr_width;
872 u32 maxburst;
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100873 int err;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100874
875 switch (dd->ctx->block_size) {
876 case CFB8_BLOCK_SIZE:
877 addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
878 maxburst = 1;
879 break;
880
881 case CFB16_BLOCK_SIZE:
882 addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
883 maxburst = 1;
884 break;
885
886 case CFB32_BLOCK_SIZE:
887 case CFB64_BLOCK_SIZE:
888 addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
889 maxburst = 1;
890 break;
891
892 case AES_BLOCK_SIZE:
893 addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
894 maxburst = dd->caps.max_burst_size;
895 break;
896
897 default:
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100898 err = -EINVAL;
899 goto exit;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100900 }
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200901
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100902 err = atmel_aes_map(dd, src, dst, len);
903 if (err)
904 goto exit;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200905
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100906 dd->resume = resume;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200907
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100908 /* Set output DMA transfer first */
909 err = atmel_aes_dma_transfer_start(dd, addr_width, DMA_DEV_TO_MEM,
910 maxburst);
911 if (err)
912 goto unmap;
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100913
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100914 /* Then set input DMA transfer */
915 err = atmel_aes_dma_transfer_start(dd, addr_width, DMA_MEM_TO_DEV,
916 maxburst);
917 if (err)
918 goto output_transfer_stop;
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100919
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100920 return -EINPROGRESS;
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100921
922output_transfer_stop:
Tudor Ambarus0e693782019-12-13 09:54:42 +0000923 dmaengine_terminate_sync(dd->dst.chan);
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100924unmap:
925 atmel_aes_unmap(dd);
926exit:
927 return atmel_aes_complete(dd, err);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200928}
929
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100930static void atmel_aes_dma_callback(void *data)
931{
932 struct atmel_aes_dev *dd = data;
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100933
Tudor Ambarus0e693782019-12-13 09:54:42 +0000934 atmel_aes_unmap(dd);
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100935 dd->is_async = true;
936 (void)dd->resume(dd);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200937}
938
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200939static int atmel_aes_handle_queue(struct atmel_aes_dev *dd,
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100940 struct crypto_async_request *new_areq)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200941{
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100942 struct crypto_async_request *areq, *backlog;
943 struct atmel_aes_base_ctx *ctx;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200944 unsigned long flags;
Cyrille Pitchena1f613f2017-01-26 17:07:55 +0100945 bool start_async;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200946 int err, ret = 0;
947
948 spin_lock_irqsave(&dd->lock, flags);
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100949 if (new_areq)
950 ret = crypto_enqueue_request(&dd->queue, new_areq);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200951 if (dd->flags & AES_FLAGS_BUSY) {
952 spin_unlock_irqrestore(&dd->lock, flags);
953 return ret;
954 }
955 backlog = crypto_get_backlog(&dd->queue);
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100956 areq = crypto_dequeue_request(&dd->queue);
957 if (areq)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200958 dd->flags |= AES_FLAGS_BUSY;
959 spin_unlock_irqrestore(&dd->lock, flags);
960
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100961 if (!areq)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200962 return ret;
963
964 if (backlog)
965 backlog->complete(backlog, -EINPROGRESS);
966
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100967 ctx = crypto_tfm_ctx(areq->tfm);
968
969 dd->areq = areq;
970 dd->ctx = ctx;
Cyrille Pitchena1f613f2017-01-26 17:07:55 +0100971 start_async = (areq != new_areq);
972 dd->is_async = start_async;
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100973
Cyrille Pitchena1f613f2017-01-26 17:07:55 +0100974 /* WARNING: ctx->start() MAY change dd->is_async. */
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100975 err = ctx->start(dd);
Cyrille Pitchena1f613f2017-01-26 17:07:55 +0100976 return (start_async) ? ret : err;
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100977}
978
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100979
980/* AES async block ciphers */
981
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100982static int atmel_aes_transfer_complete(struct atmel_aes_dev *dd)
983{
984 return atmel_aes_complete(dd, 0);
985}
986
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100987static int atmel_aes_start(struct atmel_aes_dev *dd)
988{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +0100989 struct skcipher_request *req = skcipher_request_cast(dd->areq);
990 struct atmel_aes_reqctx *rctx = skcipher_request_ctx(req);
991 bool use_dma = (req->cryptlen >= ATMEL_AES_DMA_THRESHOLD ||
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100992 dd->ctx->block_size != AES_BLOCK_SIZE);
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100993 int err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200994
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100995 atmel_aes_set_mode(dd, rctx);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200996
Cyrille Pitchencdfab4a2015-12-17 17:48:38 +0100997 err = atmel_aes_hw_init(dd);
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100998 if (err)
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100999 return atmel_aes_complete(dd, err);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001000
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001001 atmel_aes_write_ctrl(dd, use_dma, (void *)req->iv);
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +01001002 if (use_dma)
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001003 return atmel_aes_dma_start(dd, req->src, req->dst,
1004 req->cryptlen,
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +01001005 atmel_aes_transfer_complete);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001006
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001007 return atmel_aes_cpu_start(dd, req->src, req->dst, req->cryptlen,
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +01001008 atmel_aes_transfer_complete);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001009}
1010
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001011static int atmel_aes_ctr_transfer(struct atmel_aes_dev *dd)
1012{
1013 struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx);
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001014 struct skcipher_request *req = skcipher_request_cast(dd->areq);
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001015 struct scatterlist *src, *dst;
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001016 size_t datalen;
Tudor Ambarus781a08d2019-12-05 09:54:01 +00001017 u32 ctr;
Tudor Ambarus371731e2019-12-05 09:54:03 +00001018 u16 start, end;
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001019 bool use_dma, fragmented = false;
1020
1021 /* Check for transfer completion. */
1022 ctx->offset += dd->total;
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001023 if (ctx->offset >= req->cryptlen)
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001024 return atmel_aes_transfer_complete(dd);
1025
1026 /* Compute data length. */
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001027 datalen = req->cryptlen - ctx->offset;
Tudor Ambarus371731e2019-12-05 09:54:03 +00001028 ctx->blocks = DIV_ROUND_UP(datalen, AES_BLOCK_SIZE);
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001029 ctr = be32_to_cpu(ctx->iv[3]);
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001030
Tudor Ambarus781a08d2019-12-05 09:54:01 +00001031 /* Check 16bit counter overflow. */
1032 start = ctr & 0xffff;
Tudor Ambarus371731e2019-12-05 09:54:03 +00001033 end = start + ctx->blocks - 1;
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001034
Tudor Ambarus371731e2019-12-05 09:54:03 +00001035 if (ctx->blocks >> 16 || end < start) {
Tudor Ambarus781a08d2019-12-05 09:54:01 +00001036 ctr |= 0xffff;
1037 datalen = AES_BLOCK_SIZE * (0x10000 - start);
1038 fragmented = true;
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001039 }
Tudor Ambarus781a08d2019-12-05 09:54:01 +00001040
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001041 use_dma = (datalen >= ATMEL_AES_DMA_THRESHOLD);
1042
1043 /* Jump to offset. */
1044 src = scatterwalk_ffwd(ctx->src, req->src, ctx->offset);
1045 dst = ((req->src == req->dst) ? src :
1046 scatterwalk_ffwd(ctx->dst, req->dst, ctx->offset));
1047
1048 /* Configure hardware. */
1049 atmel_aes_write_ctrl(dd, use_dma, ctx->iv);
1050 if (unlikely(fragmented)) {
1051 /*
1052 * Increment the counter manually to cope with the hardware
1053 * counter overflow.
1054 */
1055 ctx->iv[3] = cpu_to_be32(ctr);
1056 crypto_inc((u8 *)ctx->iv, AES_BLOCK_SIZE);
1057 }
1058
1059 if (use_dma)
1060 return atmel_aes_dma_start(dd, src, dst, datalen,
1061 atmel_aes_ctr_transfer);
1062
1063 return atmel_aes_cpu_start(dd, src, dst, datalen,
1064 atmel_aes_ctr_transfer);
1065}
1066
1067static int atmel_aes_ctr_start(struct atmel_aes_dev *dd)
1068{
1069 struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx);
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001070 struct skcipher_request *req = skcipher_request_cast(dd->areq);
1071 struct atmel_aes_reqctx *rctx = skcipher_request_ctx(req);
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001072 int err;
1073
1074 atmel_aes_set_mode(dd, rctx);
1075
1076 err = atmel_aes_hw_init(dd);
1077 if (err)
1078 return atmel_aes_complete(dd, err);
1079
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001080 memcpy(ctx->iv, req->iv, AES_BLOCK_SIZE);
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001081 ctx->offset = 0;
1082 dd->total = 0;
1083 return atmel_aes_ctr_transfer(dd);
1084}
1085
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001086static int atmel_aes_crypt(struct skcipher_request *req, unsigned long mode)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001087{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001088 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
1089 struct atmel_aes_base_ctx *ctx = crypto_skcipher_ctx(skcipher);
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001090 struct atmel_aes_reqctx *rctx;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001091 struct atmel_aes_dev *dd;
1092
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001093 switch (mode & AES_FLAGS_OPMODE_MASK) {
1094 case AES_FLAGS_CFB8:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001095 ctx->block_size = CFB8_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001096 break;
1097
1098 case AES_FLAGS_CFB16:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001099 ctx->block_size = CFB16_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001100 break;
1101
1102 case AES_FLAGS_CFB32:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001103 ctx->block_size = CFB32_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001104 break;
1105
1106 case AES_FLAGS_CFB64:
Leilei Zhao9f849512014-04-22 15:23:24 +08001107 ctx->block_size = CFB64_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001108 break;
1109
1110 default:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001111 ctx->block_size = AES_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001112 break;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001113 }
Romain Izard91308012017-10-31 16:25:23 +01001114 ctx->is_aead = false;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001115
1116 dd = atmel_aes_find_dev(ctx);
1117 if (!dd)
1118 return -ENODEV;
1119
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001120 rctx = skcipher_request_ctx(req);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001121 rctx->mode = mode;
1122
Tudor Ambarusc65d1232019-12-05 09:54:00 +00001123 if ((mode & AES_FLAGS_OPMODE_MASK) != AES_FLAGS_ECB &&
1124 !(mode & AES_FLAGS_ENCRYPT) && req->src == req->dst) {
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001125 unsigned int ivsize = crypto_skcipher_ivsize(skcipher);
Romain Izard91308012017-10-31 16:25:23 +01001126
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001127 if (req->cryptlen >= ivsize)
Tudor Ambarus86ef1df2019-10-04 08:55:37 +00001128 scatterwalk_map_and_copy(rctx->lastc, req->src,
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001129 req->cryptlen - ivsize,
Tudor Ambarus86ef1df2019-10-04 08:55:37 +00001130 ivsize, 0);
Romain Izard91308012017-10-31 16:25:23 +01001131 }
1132
Cyrille Pitchenccbf7292015-12-17 17:48:39 +01001133 return atmel_aes_handle_queue(dd, &req->base);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001134}
1135
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001136static int atmel_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001137 unsigned int keylen)
1138{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001139 struct atmel_aes_base_ctx *ctx = crypto_skcipher_ctx(tfm);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001140
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001141 if (keylen != AES_KEYSIZE_128 &&
1142 keylen != AES_KEYSIZE_192 &&
1143 keylen != AES_KEYSIZE_256) {
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001144 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001145 return -EINVAL;
1146 }
1147
1148 memcpy(ctx->key, key, keylen);
1149 ctx->keylen = keylen;
1150
1151 return 0;
1152}
1153
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001154static int atmel_aes_ecb_encrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001155{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001156 return atmel_aes_crypt(req, AES_FLAGS_ECB | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001157}
1158
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001159static int atmel_aes_ecb_decrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001160{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001161 return atmel_aes_crypt(req, AES_FLAGS_ECB);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001162}
1163
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001164static int atmel_aes_cbc_encrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001165{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001166 return atmel_aes_crypt(req, AES_FLAGS_CBC | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001167}
1168
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001169static int atmel_aes_cbc_decrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001170{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001171 return atmel_aes_crypt(req, AES_FLAGS_CBC);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001172}
1173
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001174static int atmel_aes_ofb_encrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001175{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001176 return atmel_aes_crypt(req, AES_FLAGS_OFB | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001177}
1178
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001179static int atmel_aes_ofb_decrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001180{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001181 return atmel_aes_crypt(req, AES_FLAGS_OFB);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001182}
1183
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001184static int atmel_aes_cfb_encrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001185{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001186 return atmel_aes_crypt(req, AES_FLAGS_CFB128 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001187}
1188
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001189static int atmel_aes_cfb_decrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001190{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001191 return atmel_aes_crypt(req, AES_FLAGS_CFB128);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001192}
1193
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001194static int atmel_aes_cfb64_encrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001195{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001196 return atmel_aes_crypt(req, AES_FLAGS_CFB64 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001197}
1198
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001199static int atmel_aes_cfb64_decrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001200{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001201 return atmel_aes_crypt(req, AES_FLAGS_CFB64);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001202}
1203
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001204static int atmel_aes_cfb32_encrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001205{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001206 return atmel_aes_crypt(req, AES_FLAGS_CFB32 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001207}
1208
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001209static int atmel_aes_cfb32_decrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001210{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001211 return atmel_aes_crypt(req, AES_FLAGS_CFB32);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001212}
1213
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001214static int atmel_aes_cfb16_encrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001215{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001216 return atmel_aes_crypt(req, AES_FLAGS_CFB16 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001217}
1218
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001219static int atmel_aes_cfb16_decrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001220{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001221 return atmel_aes_crypt(req, AES_FLAGS_CFB16);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001222}
1223
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001224static int atmel_aes_cfb8_encrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001225{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001226 return atmel_aes_crypt(req, AES_FLAGS_CFB8 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001227}
1228
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001229static int atmel_aes_cfb8_decrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001230{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001231 return atmel_aes_crypt(req, AES_FLAGS_CFB8);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001232}
1233
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001234static int atmel_aes_ctr_encrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001235{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001236 return atmel_aes_crypt(req, AES_FLAGS_CTR | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001237}
1238
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001239static int atmel_aes_ctr_decrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001240{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001241 return atmel_aes_crypt(req, AES_FLAGS_CTR);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001242}
1243
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001244static int atmel_aes_init_tfm(struct crypto_skcipher *tfm)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001245{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001246 struct atmel_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
Cyrille Pitchenccbf7292015-12-17 17:48:39 +01001247
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001248 crypto_skcipher_set_reqsize(tfm, sizeof(struct atmel_aes_reqctx));
Cyrille Pitchenccbf7292015-12-17 17:48:39 +01001249 ctx->base.start = atmel_aes_start;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001250
1251 return 0;
1252}
1253
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001254static int atmel_aes_ctr_init_tfm(struct crypto_skcipher *tfm)
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001255{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001256 struct atmel_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001257
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001258 crypto_skcipher_set_reqsize(tfm, sizeof(struct atmel_aes_reqctx));
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001259 ctx->base.start = atmel_aes_ctr_start;
1260
1261 return 0;
1262}
1263
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001264static struct skcipher_alg aes_algs[] = {
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001265{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001266 .base.cra_name = "ecb(aes)",
1267 .base.cra_driver_name = "atmel-ecb-aes",
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001268 .base.cra_blocksize = AES_BLOCK_SIZE,
1269 .base.cra_ctxsize = sizeof(struct atmel_aes_ctx),
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001270
1271 .init = atmel_aes_init_tfm,
1272 .min_keysize = AES_MIN_KEY_SIZE,
1273 .max_keysize = AES_MAX_KEY_SIZE,
1274 .setkey = atmel_aes_setkey,
1275 .encrypt = atmel_aes_ecb_encrypt,
1276 .decrypt = atmel_aes_ecb_decrypt,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001277},
1278{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001279 .base.cra_name = "cbc(aes)",
1280 .base.cra_driver_name = "atmel-cbc-aes",
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001281 .base.cra_blocksize = AES_BLOCK_SIZE,
1282 .base.cra_ctxsize = sizeof(struct atmel_aes_ctx),
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001283
1284 .init = atmel_aes_init_tfm,
1285 .min_keysize = AES_MIN_KEY_SIZE,
1286 .max_keysize = AES_MAX_KEY_SIZE,
1287 .setkey = atmel_aes_setkey,
1288 .encrypt = atmel_aes_cbc_encrypt,
1289 .decrypt = atmel_aes_cbc_decrypt,
1290 .ivsize = AES_BLOCK_SIZE,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001291},
1292{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001293 .base.cra_name = "ofb(aes)",
1294 .base.cra_driver_name = "atmel-ofb-aes",
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001295 .base.cra_blocksize = AES_BLOCK_SIZE,
1296 .base.cra_ctxsize = sizeof(struct atmel_aes_ctx),
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001297
1298 .init = atmel_aes_init_tfm,
1299 .min_keysize = AES_MIN_KEY_SIZE,
1300 .max_keysize = AES_MAX_KEY_SIZE,
1301 .setkey = atmel_aes_setkey,
1302 .encrypt = atmel_aes_ofb_encrypt,
1303 .decrypt = atmel_aes_ofb_decrypt,
1304 .ivsize = AES_BLOCK_SIZE,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001305},
1306{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001307 .base.cra_name = "cfb(aes)",
1308 .base.cra_driver_name = "atmel-cfb-aes",
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001309 .base.cra_blocksize = AES_BLOCK_SIZE,
1310 .base.cra_ctxsize = sizeof(struct atmel_aes_ctx),
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001311
1312 .init = atmel_aes_init_tfm,
1313 .min_keysize = AES_MIN_KEY_SIZE,
1314 .max_keysize = AES_MAX_KEY_SIZE,
1315 .setkey = atmel_aes_setkey,
1316 .encrypt = atmel_aes_cfb_encrypt,
1317 .decrypt = atmel_aes_cfb_decrypt,
1318 .ivsize = AES_BLOCK_SIZE,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001319},
1320{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001321 .base.cra_name = "cfb32(aes)",
1322 .base.cra_driver_name = "atmel-cfb32-aes",
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001323 .base.cra_blocksize = CFB32_BLOCK_SIZE,
1324 .base.cra_ctxsize = sizeof(struct atmel_aes_ctx),
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001325
1326 .init = atmel_aes_init_tfm,
1327 .min_keysize = AES_MIN_KEY_SIZE,
1328 .max_keysize = AES_MAX_KEY_SIZE,
1329 .setkey = atmel_aes_setkey,
1330 .encrypt = atmel_aes_cfb32_encrypt,
1331 .decrypt = atmel_aes_cfb32_decrypt,
1332 .ivsize = AES_BLOCK_SIZE,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001333},
1334{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001335 .base.cra_name = "cfb16(aes)",
1336 .base.cra_driver_name = "atmel-cfb16-aes",
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001337 .base.cra_blocksize = CFB16_BLOCK_SIZE,
1338 .base.cra_ctxsize = sizeof(struct atmel_aes_ctx),
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001339
1340 .init = atmel_aes_init_tfm,
1341 .min_keysize = AES_MIN_KEY_SIZE,
1342 .max_keysize = AES_MAX_KEY_SIZE,
1343 .setkey = atmel_aes_setkey,
1344 .encrypt = atmel_aes_cfb16_encrypt,
1345 .decrypt = atmel_aes_cfb16_decrypt,
1346 .ivsize = AES_BLOCK_SIZE,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001347},
1348{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001349 .base.cra_name = "cfb8(aes)",
1350 .base.cra_driver_name = "atmel-cfb8-aes",
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001351 .base.cra_blocksize = CFB8_BLOCK_SIZE,
1352 .base.cra_ctxsize = sizeof(struct atmel_aes_ctx),
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001353
1354 .init = atmel_aes_init_tfm,
1355 .min_keysize = AES_MIN_KEY_SIZE,
1356 .max_keysize = AES_MAX_KEY_SIZE,
1357 .setkey = atmel_aes_setkey,
1358 .encrypt = atmel_aes_cfb8_encrypt,
1359 .decrypt = atmel_aes_cfb8_decrypt,
1360 .ivsize = AES_BLOCK_SIZE,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001361},
1362{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001363 .base.cra_name = "ctr(aes)",
1364 .base.cra_driver_name = "atmel-ctr-aes",
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001365 .base.cra_blocksize = 1,
1366 .base.cra_ctxsize = sizeof(struct atmel_aes_ctr_ctx),
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001367
1368 .init = atmel_aes_ctr_init_tfm,
1369 .min_keysize = AES_MIN_KEY_SIZE,
1370 .max_keysize = AES_MAX_KEY_SIZE,
1371 .setkey = atmel_aes_setkey,
1372 .encrypt = atmel_aes_ctr_encrypt,
1373 .decrypt = atmel_aes_ctr_decrypt,
1374 .ivsize = AES_BLOCK_SIZE,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001375},
1376};
1377
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001378static struct skcipher_alg aes_cfb64_alg = {
1379 .base.cra_name = "cfb64(aes)",
1380 .base.cra_driver_name = "atmel-cfb64-aes",
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001381 .base.cra_blocksize = CFB64_BLOCK_SIZE,
1382 .base.cra_ctxsize = sizeof(struct atmel_aes_ctx),
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001383
1384 .init = atmel_aes_init_tfm,
1385 .min_keysize = AES_MIN_KEY_SIZE,
1386 .max_keysize = AES_MAX_KEY_SIZE,
1387 .setkey = atmel_aes_setkey,
1388 .encrypt = atmel_aes_cfb64_encrypt,
1389 .decrypt = atmel_aes_cfb64_decrypt,
1390 .ivsize = AES_BLOCK_SIZE,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001391};
1392
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01001393
Cyrille Pitchend4419542015-12-17 18:13:07 +01001394/* gcm aead functions */
1395
1396static int atmel_aes_gcm_ghash(struct atmel_aes_dev *dd,
1397 const u32 *data, size_t datalen,
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +01001398 const __be32 *ghash_in, __be32 *ghash_out,
Cyrille Pitchend4419542015-12-17 18:13:07 +01001399 atmel_aes_fn_t resume);
1400static int atmel_aes_gcm_ghash_init(struct atmel_aes_dev *dd);
1401static int atmel_aes_gcm_ghash_finalize(struct atmel_aes_dev *dd);
1402
1403static int atmel_aes_gcm_start(struct atmel_aes_dev *dd);
1404static int atmel_aes_gcm_process(struct atmel_aes_dev *dd);
1405static int atmel_aes_gcm_length(struct atmel_aes_dev *dd);
1406static int atmel_aes_gcm_data(struct atmel_aes_dev *dd);
1407static int atmel_aes_gcm_tag_init(struct atmel_aes_dev *dd);
1408static int atmel_aes_gcm_tag(struct atmel_aes_dev *dd);
1409static int atmel_aes_gcm_finalize(struct atmel_aes_dev *dd);
1410
1411static inline struct atmel_aes_gcm_ctx *
1412atmel_aes_gcm_ctx_cast(struct atmel_aes_base_ctx *ctx)
1413{
1414 return container_of(ctx, struct atmel_aes_gcm_ctx, base);
1415}
1416
1417static int atmel_aes_gcm_ghash(struct atmel_aes_dev *dd,
1418 const u32 *data, size_t datalen,
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +01001419 const __be32 *ghash_in, __be32 *ghash_out,
Cyrille Pitchend4419542015-12-17 18:13:07 +01001420 atmel_aes_fn_t resume)
1421{
1422 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1423
1424 dd->data = (u32 *)data;
1425 dd->datalen = datalen;
1426 ctx->ghash_in = ghash_in;
1427 ctx->ghash_out = ghash_out;
1428 ctx->ghash_resume = resume;
1429
1430 atmel_aes_write_ctrl(dd, false, NULL);
1431 return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_ghash_init);
1432}
1433
1434static int atmel_aes_gcm_ghash_init(struct atmel_aes_dev *dd)
1435{
1436 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1437
1438 /* Set the data length. */
1439 atmel_aes_write(dd, AES_AADLENR, dd->total);
1440 atmel_aes_write(dd, AES_CLENR, 0);
1441
1442 /* If needed, overwrite the GCM Intermediate Hash Word Registers */
1443 if (ctx->ghash_in)
1444 atmel_aes_write_block(dd, AES_GHASHR(0), ctx->ghash_in);
1445
1446 return atmel_aes_gcm_ghash_finalize(dd);
1447}
1448
1449static int atmel_aes_gcm_ghash_finalize(struct atmel_aes_dev *dd)
1450{
1451 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1452 u32 isr;
1453
1454 /* Write data into the Input Data Registers. */
1455 while (dd->datalen > 0) {
1456 atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
1457 dd->data += 4;
1458 dd->datalen -= AES_BLOCK_SIZE;
1459
1460 isr = atmel_aes_read(dd, AES_ISR);
1461 if (!(isr & AES_INT_DATARDY)) {
1462 dd->resume = atmel_aes_gcm_ghash_finalize;
1463 atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
1464 return -EINPROGRESS;
1465 }
1466 }
1467
1468 /* Read the computed hash from GHASHRx. */
1469 atmel_aes_read_block(dd, AES_GHASHR(0), ctx->ghash_out);
1470
1471 return ctx->ghash_resume(dd);
1472}
1473
1474
1475static int atmel_aes_gcm_start(struct atmel_aes_dev *dd)
1476{
1477 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1478 struct aead_request *req = aead_request_cast(dd->areq);
1479 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1480 struct atmel_aes_reqctx *rctx = aead_request_ctx(req);
1481 size_t ivsize = crypto_aead_ivsize(tfm);
1482 size_t datalen, padlen;
1483 const void *iv = req->iv;
1484 u8 *data = dd->buf;
1485 int err;
1486
1487 atmel_aes_set_mode(dd, rctx);
1488
1489 err = atmel_aes_hw_init(dd);
1490 if (err)
1491 return atmel_aes_complete(dd, err);
1492
Corentin LABBE219d51c2017-08-22 10:08:12 +02001493 if (likely(ivsize == GCM_AES_IV_SIZE)) {
Cyrille Pitchend4419542015-12-17 18:13:07 +01001494 memcpy(ctx->j0, iv, ivsize);
1495 ctx->j0[3] = cpu_to_be32(1);
1496 return atmel_aes_gcm_process(dd);
1497 }
1498
1499 padlen = atmel_aes_padlen(ivsize, AES_BLOCK_SIZE);
1500 datalen = ivsize + padlen + AES_BLOCK_SIZE;
1501 if (datalen > dd->buflen)
1502 return atmel_aes_complete(dd, -EINVAL);
1503
1504 memcpy(data, iv, ivsize);
1505 memset(data + ivsize, 0, padlen + sizeof(u64));
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +01001506 ((__be64 *)(data + datalen))[-1] = cpu_to_be64(ivsize * 8);
Cyrille Pitchend4419542015-12-17 18:13:07 +01001507
1508 return atmel_aes_gcm_ghash(dd, (const u32 *)data, datalen,
1509 NULL, ctx->j0, atmel_aes_gcm_process);
1510}
1511
1512static int atmel_aes_gcm_process(struct atmel_aes_dev *dd)
1513{
1514 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1515 struct aead_request *req = aead_request_cast(dd->areq);
1516 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1517 bool enc = atmel_aes_is_encrypt(dd);
1518 u32 authsize;
1519
1520 /* Compute text length. */
1521 authsize = crypto_aead_authsize(tfm);
1522 ctx->textlen = req->cryptlen - (enc ? 0 : authsize);
1523
1524 /*
1525 * According to tcrypt test suite, the GCM Automatic Tag Generation
1526 * fails when both the message and its associated data are empty.
1527 */
1528 if (likely(req->assoclen != 0 || ctx->textlen != 0))
1529 dd->flags |= AES_FLAGS_GTAGEN;
1530
1531 atmel_aes_write_ctrl(dd, false, NULL);
1532 return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_length);
1533}
1534
1535static int atmel_aes_gcm_length(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);
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +01001539 __be32 j0_lsw, *j0 = ctx->j0;
Cyrille Pitchend4419542015-12-17 18:13:07 +01001540 size_t padlen;
1541
1542 /* Write incr32(J0) into IV. */
1543 j0_lsw = j0[3];
1544 j0[3] = cpu_to_be32(be32_to_cpu(j0[3]) + 1);
1545 atmel_aes_write_block(dd, AES_IVR(0), j0);
1546 j0[3] = j0_lsw;
1547
1548 /* Set aad and text lengths. */
1549 atmel_aes_write(dd, AES_AADLENR, req->assoclen);
1550 atmel_aes_write(dd, AES_CLENR, ctx->textlen);
1551
1552 /* Check whether AAD are present. */
1553 if (unlikely(req->assoclen == 0)) {
1554 dd->datalen = 0;
1555 return atmel_aes_gcm_data(dd);
1556 }
1557
1558 /* Copy assoc data and add padding. */
1559 padlen = atmel_aes_padlen(req->assoclen, AES_BLOCK_SIZE);
1560 if (unlikely(req->assoclen + padlen > dd->buflen))
1561 return atmel_aes_complete(dd, -EINVAL);
1562 sg_copy_to_buffer(req->src, sg_nents(req->src), dd->buf, req->assoclen);
1563
1564 /* Write assoc data into the Input Data register. */
1565 dd->data = (u32 *)dd->buf;
1566 dd->datalen = req->assoclen + padlen;
1567 return atmel_aes_gcm_data(dd);
1568}
1569
1570static int atmel_aes_gcm_data(struct atmel_aes_dev *dd)
1571{
1572 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1573 struct aead_request *req = aead_request_cast(dd->areq);
1574 bool use_dma = (ctx->textlen >= ATMEL_AES_DMA_THRESHOLD);
1575 struct scatterlist *src, *dst;
1576 u32 isr, mr;
1577
1578 /* Write AAD first. */
1579 while (dd->datalen > 0) {
1580 atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
1581 dd->data += 4;
1582 dd->datalen -= AES_BLOCK_SIZE;
1583
1584 isr = atmel_aes_read(dd, AES_ISR);
1585 if (!(isr & AES_INT_DATARDY)) {
1586 dd->resume = atmel_aes_gcm_data;
1587 atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
1588 return -EINPROGRESS;
1589 }
1590 }
1591
1592 /* GMAC only. */
1593 if (unlikely(ctx->textlen == 0))
1594 return atmel_aes_gcm_tag_init(dd);
1595
1596 /* Prepare src and dst scatter lists to transfer cipher/plain texts */
1597 src = scatterwalk_ffwd(ctx->src, req->src, req->assoclen);
1598 dst = ((req->src == req->dst) ? src :
1599 scatterwalk_ffwd(ctx->dst, req->dst, req->assoclen));
1600
1601 if (use_dma) {
1602 /* Update the Mode Register for DMA transfers. */
1603 mr = atmel_aes_read(dd, AES_MR);
1604 mr &= ~(AES_MR_SMOD_MASK | AES_MR_DUALBUFF);
1605 mr |= AES_MR_SMOD_IDATAR0;
1606 if (dd->caps.has_dualbuff)
1607 mr |= AES_MR_DUALBUFF;
1608 atmel_aes_write(dd, AES_MR, mr);
1609
1610 return atmel_aes_dma_start(dd, src, dst, ctx->textlen,
1611 atmel_aes_gcm_tag_init);
1612 }
1613
1614 return atmel_aes_cpu_start(dd, src, dst, ctx->textlen,
1615 atmel_aes_gcm_tag_init);
1616}
1617
1618static int atmel_aes_gcm_tag_init(struct atmel_aes_dev *dd)
1619{
1620 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1621 struct aead_request *req = aead_request_cast(dd->areq);
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +01001622 __be64 *data = dd->buf;
Cyrille Pitchend4419542015-12-17 18:13:07 +01001623
1624 if (likely(dd->flags & AES_FLAGS_GTAGEN)) {
1625 if (!(atmel_aes_read(dd, AES_ISR) & AES_INT_TAGRDY)) {
1626 dd->resume = atmel_aes_gcm_tag_init;
1627 atmel_aes_write(dd, AES_IER, AES_INT_TAGRDY);
1628 return -EINPROGRESS;
1629 }
1630
1631 return atmel_aes_gcm_finalize(dd);
1632 }
1633
1634 /* Read the GCM Intermediate Hash Word Registers. */
1635 atmel_aes_read_block(dd, AES_GHASHR(0), ctx->ghash);
1636
1637 data[0] = cpu_to_be64(req->assoclen * 8);
1638 data[1] = cpu_to_be64(ctx->textlen * 8);
1639
1640 return atmel_aes_gcm_ghash(dd, (const u32 *)data, AES_BLOCK_SIZE,
1641 ctx->ghash, ctx->ghash, atmel_aes_gcm_tag);
1642}
1643
1644static int atmel_aes_gcm_tag(struct atmel_aes_dev *dd)
1645{
1646 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1647 unsigned long flags;
1648
1649 /*
1650 * Change mode to CTR to complete the tag generation.
1651 * Use J0 as Initialization Vector.
1652 */
1653 flags = dd->flags;
1654 dd->flags &= ~(AES_FLAGS_OPMODE_MASK | AES_FLAGS_GTAGEN);
1655 dd->flags |= AES_FLAGS_CTR;
1656 atmel_aes_write_ctrl(dd, false, ctx->j0);
1657 dd->flags = flags;
1658
1659 atmel_aes_write_block(dd, AES_IDATAR(0), ctx->ghash);
1660 return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_finalize);
1661}
1662
1663static int atmel_aes_gcm_finalize(struct atmel_aes_dev *dd)
1664{
1665 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1666 struct aead_request *req = aead_request_cast(dd->areq);
1667 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1668 bool enc = atmel_aes_is_encrypt(dd);
1669 u32 offset, authsize, itag[4], *otag = ctx->tag;
1670 int err;
1671
1672 /* Read the computed tag. */
1673 if (likely(dd->flags & AES_FLAGS_GTAGEN))
1674 atmel_aes_read_block(dd, AES_TAGR(0), ctx->tag);
1675 else
1676 atmel_aes_read_block(dd, AES_ODATAR(0), ctx->tag);
1677
1678 offset = req->assoclen + ctx->textlen;
1679 authsize = crypto_aead_authsize(tfm);
1680 if (enc) {
1681 scatterwalk_map_and_copy(otag, req->dst, offset, authsize, 1);
1682 err = 0;
1683 } else {
1684 scatterwalk_map_and_copy(itag, req->src, offset, authsize, 0);
1685 err = crypto_memneq(itag, otag, authsize) ? -EBADMSG : 0;
1686 }
1687
1688 return atmel_aes_complete(dd, err);
1689}
1690
1691static int atmel_aes_gcm_crypt(struct aead_request *req,
1692 unsigned long mode)
1693{
1694 struct atmel_aes_base_ctx *ctx;
1695 struct atmel_aes_reqctx *rctx;
1696 struct atmel_aes_dev *dd;
1697
1698 ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
1699 ctx->block_size = AES_BLOCK_SIZE;
Romain Izard91308012017-10-31 16:25:23 +01001700 ctx->is_aead = true;
Cyrille Pitchend4419542015-12-17 18:13:07 +01001701
1702 dd = atmel_aes_find_dev(ctx);
1703 if (!dd)
1704 return -ENODEV;
1705
1706 rctx = aead_request_ctx(req);
1707 rctx->mode = AES_FLAGS_GCM | mode;
1708
1709 return atmel_aes_handle_queue(dd, &req->base);
1710}
1711
1712static int atmel_aes_gcm_setkey(struct crypto_aead *tfm, const u8 *key,
1713 unsigned int keylen)
1714{
1715 struct atmel_aes_base_ctx *ctx = crypto_aead_ctx(tfm);
1716
1717 if (keylen != AES_KEYSIZE_256 &&
1718 keylen != AES_KEYSIZE_192 &&
1719 keylen != AES_KEYSIZE_128) {
1720 crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
1721 return -EINVAL;
1722 }
1723
1724 memcpy(ctx->key, key, keylen);
1725 ctx->keylen = keylen;
1726
1727 return 0;
1728}
1729
1730static int atmel_aes_gcm_setauthsize(struct crypto_aead *tfm,
1731 unsigned int authsize)
1732{
Tudor Ambarus7db15aa2019-12-05 09:54:08 +00001733 return crypto_gcm_check_authsize(authsize);
Cyrille Pitchend4419542015-12-17 18:13:07 +01001734}
1735
1736static int atmel_aes_gcm_encrypt(struct aead_request *req)
1737{
1738 return atmel_aes_gcm_crypt(req, AES_FLAGS_ENCRYPT);
1739}
1740
1741static int atmel_aes_gcm_decrypt(struct aead_request *req)
1742{
1743 return atmel_aes_gcm_crypt(req, 0);
1744}
1745
1746static int atmel_aes_gcm_init(struct crypto_aead *tfm)
1747{
1748 struct atmel_aes_gcm_ctx *ctx = crypto_aead_ctx(tfm);
1749
1750 crypto_aead_set_reqsize(tfm, sizeof(struct atmel_aes_reqctx));
1751 ctx->base.start = atmel_aes_gcm_start;
1752
1753 return 0;
1754}
1755
Cyrille Pitchend4419542015-12-17 18:13:07 +01001756static struct aead_alg aes_gcm_alg = {
1757 .setkey = atmel_aes_gcm_setkey,
1758 .setauthsize = atmel_aes_gcm_setauthsize,
1759 .encrypt = atmel_aes_gcm_encrypt,
1760 .decrypt = atmel_aes_gcm_decrypt,
1761 .init = atmel_aes_gcm_init,
Corentin LABBE219d51c2017-08-22 10:08:12 +02001762 .ivsize = GCM_AES_IV_SIZE,
Cyrille Pitchend4419542015-12-17 18:13:07 +01001763 .maxauthsize = AES_BLOCK_SIZE,
1764
1765 .base = {
1766 .cra_name = "gcm(aes)",
1767 .cra_driver_name = "atmel-gcm-aes",
Cyrille Pitchend4419542015-12-17 18:13:07 +01001768 .cra_blocksize = 1,
1769 .cra_ctxsize = sizeof(struct atmel_aes_gcm_ctx),
Cyrille Pitchend4419542015-12-17 18:13:07 +01001770 },
1771};
1772
1773
Cyrille Pitchend52db512016-10-03 14:33:16 +02001774/* xts functions */
1775
1776static inline struct atmel_aes_xts_ctx *
1777atmel_aes_xts_ctx_cast(struct atmel_aes_base_ctx *ctx)
1778{
1779 return container_of(ctx, struct atmel_aes_xts_ctx, base);
1780}
1781
1782static int atmel_aes_xts_process_data(struct atmel_aes_dev *dd);
1783
1784static int atmel_aes_xts_start(struct atmel_aes_dev *dd)
1785{
1786 struct atmel_aes_xts_ctx *ctx = atmel_aes_xts_ctx_cast(dd->ctx);
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001787 struct skcipher_request *req = skcipher_request_cast(dd->areq);
1788 struct atmel_aes_reqctx *rctx = skcipher_request_ctx(req);
Cyrille Pitchend52db512016-10-03 14:33:16 +02001789 unsigned long flags;
1790 int err;
1791
1792 atmel_aes_set_mode(dd, rctx);
1793
1794 err = atmel_aes_hw_init(dd);
1795 if (err)
1796 return atmel_aes_complete(dd, err);
1797
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001798 /* Compute the tweak value from req->iv with ecb(aes). */
Cyrille Pitchend52db512016-10-03 14:33:16 +02001799 flags = dd->flags;
1800 dd->flags &= ~AES_FLAGS_MODE_MASK;
1801 dd->flags |= (AES_FLAGS_ECB | AES_FLAGS_ENCRYPT);
1802 atmel_aes_write_ctrl_key(dd, false, NULL,
1803 ctx->key2, ctx->base.keylen);
1804 dd->flags = flags;
1805
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001806 atmel_aes_write_block(dd, AES_IDATAR(0), req->iv);
Cyrille Pitchend52db512016-10-03 14:33:16 +02001807 return atmel_aes_wait_for_data_ready(dd, atmel_aes_xts_process_data);
1808}
1809
1810static int atmel_aes_xts_process_data(struct atmel_aes_dev *dd)
1811{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001812 struct skcipher_request *req = skcipher_request_cast(dd->areq);
1813 bool use_dma = (req->cryptlen >= ATMEL_AES_DMA_THRESHOLD);
Cyrille Pitchend52db512016-10-03 14:33:16 +02001814 u32 tweak[AES_BLOCK_SIZE / sizeof(u32)];
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +01001815 static const __le32 one[AES_BLOCK_SIZE / sizeof(u32)] = {cpu_to_le32(1), };
Cyrille Pitchend52db512016-10-03 14:33:16 +02001816 u8 *tweak_bytes = (u8 *)tweak;
1817 int i;
1818
1819 /* Read the computed ciphered tweak value. */
1820 atmel_aes_read_block(dd, AES_ODATAR(0), tweak);
1821 /*
1822 * Hardware quirk:
1823 * the order of the ciphered tweak bytes need to be reversed before
1824 * writing them into the ODATARx registers.
1825 */
1826 for (i = 0; i < AES_BLOCK_SIZE/2; ++i) {
1827 u8 tmp = tweak_bytes[AES_BLOCK_SIZE - 1 - i];
1828
1829 tweak_bytes[AES_BLOCK_SIZE - 1 - i] = tweak_bytes[i];
1830 tweak_bytes[i] = tmp;
1831 }
1832
1833 /* Process the data. */
1834 atmel_aes_write_ctrl(dd, use_dma, NULL);
1835 atmel_aes_write_block(dd, AES_TWR(0), tweak);
1836 atmel_aes_write_block(dd, AES_ALPHAR(0), one);
1837 if (use_dma)
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001838 return atmel_aes_dma_start(dd, req->src, req->dst,
1839 req->cryptlen,
Cyrille Pitchend52db512016-10-03 14:33:16 +02001840 atmel_aes_transfer_complete);
1841
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001842 return atmel_aes_cpu_start(dd, req->src, req->dst, req->cryptlen,
Cyrille Pitchend52db512016-10-03 14:33:16 +02001843 atmel_aes_transfer_complete);
1844}
1845
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001846static int atmel_aes_xts_setkey(struct crypto_skcipher *tfm, const u8 *key,
Cyrille Pitchend52db512016-10-03 14:33:16 +02001847 unsigned int keylen)
1848{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001849 struct atmel_aes_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
Cyrille Pitchend52db512016-10-03 14:33:16 +02001850 int err;
1851
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001852 err = xts_check_key(crypto_skcipher_tfm(tfm), key, keylen);
Cyrille Pitchend52db512016-10-03 14:33:16 +02001853 if (err)
1854 return err;
1855
1856 memcpy(ctx->base.key, key, keylen/2);
1857 memcpy(ctx->key2, key + keylen/2, keylen/2);
1858 ctx->base.keylen = keylen/2;
1859
1860 return 0;
1861}
1862
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001863static int atmel_aes_xts_encrypt(struct skcipher_request *req)
Cyrille Pitchend52db512016-10-03 14:33:16 +02001864{
1865 return atmel_aes_crypt(req, AES_FLAGS_XTS | AES_FLAGS_ENCRYPT);
1866}
1867
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001868static int atmel_aes_xts_decrypt(struct skcipher_request *req)
Cyrille Pitchend52db512016-10-03 14:33:16 +02001869{
1870 return atmel_aes_crypt(req, AES_FLAGS_XTS);
1871}
1872
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001873static int atmel_aes_xts_init_tfm(struct crypto_skcipher *tfm)
Cyrille Pitchend52db512016-10-03 14:33:16 +02001874{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001875 struct atmel_aes_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
Cyrille Pitchend52db512016-10-03 14:33:16 +02001876
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001877 crypto_skcipher_set_reqsize(tfm, sizeof(struct atmel_aes_reqctx));
Cyrille Pitchend52db512016-10-03 14:33:16 +02001878 ctx->base.start = atmel_aes_xts_start;
1879
1880 return 0;
1881}
1882
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001883static struct skcipher_alg aes_xts_alg = {
1884 .base.cra_name = "xts(aes)",
1885 .base.cra_driver_name = "atmel-xts-aes",
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001886 .base.cra_blocksize = AES_BLOCK_SIZE,
1887 .base.cra_ctxsize = sizeof(struct atmel_aes_xts_ctx),
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001888
1889 .min_keysize = 2 * AES_MIN_KEY_SIZE,
1890 .max_keysize = 2 * AES_MAX_KEY_SIZE,
1891 .ivsize = AES_BLOCK_SIZE,
1892 .setkey = atmel_aes_xts_setkey,
1893 .encrypt = atmel_aes_xts_encrypt,
1894 .decrypt = atmel_aes_xts_decrypt,
1895 .init = atmel_aes_xts_init_tfm,
Cyrille Pitchend52db512016-10-03 14:33:16 +02001896};
1897
Herbert Xu1520c722019-10-28 15:39:07 +08001898#if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01001899/* authenc aead functions */
1900
1901static int atmel_aes_authenc_start(struct atmel_aes_dev *dd);
1902static int atmel_aes_authenc_init(struct atmel_aes_dev *dd, int err,
1903 bool is_async);
1904static int atmel_aes_authenc_transfer(struct atmel_aes_dev *dd, int err,
1905 bool is_async);
1906static int atmel_aes_authenc_digest(struct atmel_aes_dev *dd);
1907static int atmel_aes_authenc_final(struct atmel_aes_dev *dd, int err,
1908 bool is_async);
1909
1910static void atmel_aes_authenc_complete(struct atmel_aes_dev *dd, int err)
1911{
1912 struct aead_request *req = aead_request_cast(dd->areq);
1913 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
1914
1915 if (err && (dd->flags & AES_FLAGS_OWN_SHA))
1916 atmel_sha_authenc_abort(&rctx->auth_req);
1917 dd->flags &= ~AES_FLAGS_OWN_SHA;
1918}
1919
1920static int atmel_aes_authenc_start(struct atmel_aes_dev *dd)
1921{
1922 struct aead_request *req = aead_request_cast(dd->areq);
1923 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
1924 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1925 struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
1926 int err;
1927
1928 atmel_aes_set_mode(dd, &rctx->base);
1929
1930 err = atmel_aes_hw_init(dd);
1931 if (err)
1932 return atmel_aes_complete(dd, err);
1933
1934 return atmel_sha_authenc_schedule(&rctx->auth_req, ctx->auth,
1935 atmel_aes_authenc_init, dd);
1936}
1937
1938static int atmel_aes_authenc_init(struct atmel_aes_dev *dd, int err,
1939 bool is_async)
1940{
1941 struct aead_request *req = aead_request_cast(dd->areq);
1942 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
1943
1944 if (is_async)
1945 dd->is_async = true;
1946 if (err)
1947 return atmel_aes_complete(dd, err);
1948
1949 /* If here, we've got the ownership of the SHA device. */
1950 dd->flags |= AES_FLAGS_OWN_SHA;
1951
1952 /* Configure the SHA device. */
1953 return atmel_sha_authenc_init(&rctx->auth_req,
1954 req->src, req->assoclen,
1955 rctx->textlen,
1956 atmel_aes_authenc_transfer, dd);
1957}
1958
1959static int atmel_aes_authenc_transfer(struct atmel_aes_dev *dd, int err,
1960 bool is_async)
1961{
1962 struct aead_request *req = aead_request_cast(dd->areq);
1963 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
1964 bool enc = atmel_aes_is_encrypt(dd);
1965 struct scatterlist *src, *dst;
Herbert Xu427e6e32019-10-28 15:45:02 +08001966 __be32 iv[AES_BLOCK_SIZE / sizeof(u32)];
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01001967 u32 emr;
1968
1969 if (is_async)
1970 dd->is_async = true;
1971 if (err)
1972 return atmel_aes_complete(dd, err);
1973
1974 /* Prepare src and dst scatter-lists to transfer cipher/plain texts. */
1975 src = scatterwalk_ffwd(rctx->src, req->src, req->assoclen);
1976 dst = src;
1977
1978 if (req->src != req->dst)
1979 dst = scatterwalk_ffwd(rctx->dst, req->dst, req->assoclen);
1980
1981 /* Configure the AES device. */
1982 memcpy(iv, req->iv, sizeof(iv));
1983
1984 /*
1985 * Here we always set the 2nd parameter of atmel_aes_write_ctrl() to
1986 * 'true' even if the data transfer is actually performed by the CPU (so
1987 * not by the DMA) because we must force the AES_MR_SMOD bitfield to the
1988 * value AES_MR_SMOD_IDATAR0. Indeed, both AES_MR_SMOD and SHA_MR_SMOD
1989 * must be set to *_MR_SMOD_IDATAR0.
1990 */
1991 atmel_aes_write_ctrl(dd, true, iv);
1992 emr = AES_EMR_PLIPEN;
1993 if (!enc)
1994 emr |= AES_EMR_PLIPD;
1995 atmel_aes_write(dd, AES_EMR, emr);
1996
1997 /* Transfer data. */
1998 return atmel_aes_dma_start(dd, src, dst, rctx->textlen,
1999 atmel_aes_authenc_digest);
2000}
2001
2002static int atmel_aes_authenc_digest(struct atmel_aes_dev *dd)
2003{
2004 struct aead_request *req = aead_request_cast(dd->areq);
2005 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
2006
2007 /* atmel_sha_authenc_final() releases the SHA device. */
2008 dd->flags &= ~AES_FLAGS_OWN_SHA;
2009 return atmel_sha_authenc_final(&rctx->auth_req,
2010 rctx->digest, sizeof(rctx->digest),
2011 atmel_aes_authenc_final, dd);
2012}
2013
2014static int atmel_aes_authenc_final(struct atmel_aes_dev *dd, int err,
2015 bool is_async)
2016{
2017 struct aead_request *req = aead_request_cast(dd->areq);
2018 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
2019 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2020 bool enc = atmel_aes_is_encrypt(dd);
2021 u32 idigest[SHA512_DIGEST_SIZE / sizeof(u32)], *odigest = rctx->digest;
2022 u32 offs, authsize;
2023
2024 if (is_async)
2025 dd->is_async = true;
2026 if (err)
2027 goto complete;
2028
2029 offs = req->assoclen + rctx->textlen;
2030 authsize = crypto_aead_authsize(tfm);
2031 if (enc) {
2032 scatterwalk_map_and_copy(odigest, req->dst, offs, authsize, 1);
2033 } else {
2034 scatterwalk_map_and_copy(idigest, req->src, offs, authsize, 0);
2035 if (crypto_memneq(idigest, odigest, authsize))
2036 err = -EBADMSG;
2037 }
2038
2039complete:
2040 return atmel_aes_complete(dd, err);
2041}
2042
2043static int atmel_aes_authenc_setkey(struct crypto_aead *tfm, const u8 *key,
2044 unsigned int keylen)
2045{
2046 struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
2047 struct crypto_authenc_keys keys;
2048 u32 flags;
2049 int err;
2050
2051 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
2052 goto badkey;
2053
2054 if (keys.enckeylen > sizeof(ctx->base.key))
2055 goto badkey;
2056
2057 /* Save auth key. */
2058 flags = crypto_aead_get_flags(tfm);
2059 err = atmel_sha_authenc_setkey(ctx->auth,
2060 keys.authkey, keys.authkeylen,
2061 &flags);
2062 crypto_aead_set_flags(tfm, flags & CRYPTO_TFM_RES_MASK);
2063 if (err) {
2064 memzero_explicit(&keys, sizeof(keys));
2065 return err;
2066 }
2067
2068 /* Save enc key. */
2069 ctx->base.keylen = keys.enckeylen;
2070 memcpy(ctx->base.key, keys.enckey, keys.enckeylen);
2071
2072 memzero_explicit(&keys, sizeof(keys));
2073 return 0;
2074
2075badkey:
2076 crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
Antoine Tenart5d804a512018-02-23 10:01:40 +01002077 memzero_explicit(&keys, sizeof(keys));
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002078 return -EINVAL;
2079}
2080
2081static int atmel_aes_authenc_init_tfm(struct crypto_aead *tfm,
2082 unsigned long auth_mode)
2083{
2084 struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
2085 unsigned int auth_reqsize = atmel_sha_authenc_get_reqsize();
2086
2087 ctx->auth = atmel_sha_authenc_spawn(auth_mode);
2088 if (IS_ERR(ctx->auth))
2089 return PTR_ERR(ctx->auth);
2090
2091 crypto_aead_set_reqsize(tfm, (sizeof(struct atmel_aes_authenc_reqctx) +
2092 auth_reqsize));
2093 ctx->base.start = atmel_aes_authenc_start;
2094
2095 return 0;
2096}
2097
2098static int atmel_aes_authenc_hmac_sha1_init_tfm(struct crypto_aead *tfm)
2099{
2100 return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA1);
2101}
2102
2103static int atmel_aes_authenc_hmac_sha224_init_tfm(struct crypto_aead *tfm)
2104{
2105 return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA224);
2106}
2107
2108static int atmel_aes_authenc_hmac_sha256_init_tfm(struct crypto_aead *tfm)
2109{
2110 return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA256);
2111}
2112
2113static int atmel_aes_authenc_hmac_sha384_init_tfm(struct crypto_aead *tfm)
2114{
2115 return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA384);
2116}
2117
2118static int atmel_aes_authenc_hmac_sha512_init_tfm(struct crypto_aead *tfm)
2119{
2120 return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA512);
2121}
2122
2123static void atmel_aes_authenc_exit_tfm(struct crypto_aead *tfm)
2124{
2125 struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
2126
2127 atmel_sha_authenc_free(ctx->auth);
2128}
2129
2130static int atmel_aes_authenc_crypt(struct aead_request *req,
2131 unsigned long mode)
2132{
2133 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
2134 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2135 struct atmel_aes_base_ctx *ctx = crypto_aead_ctx(tfm);
2136 u32 authsize = crypto_aead_authsize(tfm);
2137 bool enc = (mode & AES_FLAGS_ENCRYPT);
2138 struct atmel_aes_dev *dd;
2139
2140 /* Compute text length. */
2141 if (!enc && req->cryptlen < authsize)
2142 return -EINVAL;
2143 rctx->textlen = req->cryptlen - (enc ? 0 : authsize);
2144
2145 /*
2146 * Currently, empty messages are not supported yet:
2147 * the SHA auto-padding can be used only on non-empty messages.
2148 * Hence a special case needs to be implemented for empty message.
2149 */
2150 if (!rctx->textlen && !req->assoclen)
2151 return -EINVAL;
2152
2153 rctx->base.mode = mode;
2154 ctx->block_size = AES_BLOCK_SIZE;
Romain Izard91308012017-10-31 16:25:23 +01002155 ctx->is_aead = true;
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002156
2157 dd = atmel_aes_find_dev(ctx);
2158 if (!dd)
2159 return -ENODEV;
2160
2161 return atmel_aes_handle_queue(dd, &req->base);
2162}
2163
2164static int atmel_aes_authenc_cbc_aes_encrypt(struct aead_request *req)
2165{
2166 return atmel_aes_authenc_crypt(req, AES_FLAGS_CBC | AES_FLAGS_ENCRYPT);
2167}
2168
2169static int atmel_aes_authenc_cbc_aes_decrypt(struct aead_request *req)
2170{
2171 return atmel_aes_authenc_crypt(req, AES_FLAGS_CBC);
2172}
2173
2174static struct aead_alg aes_authenc_algs[] = {
2175{
2176 .setkey = atmel_aes_authenc_setkey,
2177 .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
2178 .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
2179 .init = atmel_aes_authenc_hmac_sha1_init_tfm,
2180 .exit = atmel_aes_authenc_exit_tfm,
2181 .ivsize = AES_BLOCK_SIZE,
2182 .maxauthsize = SHA1_DIGEST_SIZE,
2183
2184 .base = {
2185 .cra_name = "authenc(hmac(sha1),cbc(aes))",
2186 .cra_driver_name = "atmel-authenc-hmac-sha1-cbc-aes",
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002187 .cra_blocksize = AES_BLOCK_SIZE,
2188 .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002189 },
2190},
2191{
2192 .setkey = atmel_aes_authenc_setkey,
2193 .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
2194 .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
2195 .init = atmel_aes_authenc_hmac_sha224_init_tfm,
2196 .exit = atmel_aes_authenc_exit_tfm,
2197 .ivsize = AES_BLOCK_SIZE,
2198 .maxauthsize = SHA224_DIGEST_SIZE,
2199
2200 .base = {
2201 .cra_name = "authenc(hmac(sha224),cbc(aes))",
2202 .cra_driver_name = "atmel-authenc-hmac-sha224-cbc-aes",
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002203 .cra_blocksize = AES_BLOCK_SIZE,
2204 .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002205 },
2206},
2207{
2208 .setkey = atmel_aes_authenc_setkey,
2209 .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
2210 .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
2211 .init = atmel_aes_authenc_hmac_sha256_init_tfm,
2212 .exit = atmel_aes_authenc_exit_tfm,
2213 .ivsize = AES_BLOCK_SIZE,
2214 .maxauthsize = SHA256_DIGEST_SIZE,
2215
2216 .base = {
2217 .cra_name = "authenc(hmac(sha256),cbc(aes))",
2218 .cra_driver_name = "atmel-authenc-hmac-sha256-cbc-aes",
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002219 .cra_blocksize = AES_BLOCK_SIZE,
2220 .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002221 },
2222},
2223{
2224 .setkey = atmel_aes_authenc_setkey,
2225 .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
2226 .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
2227 .init = atmel_aes_authenc_hmac_sha384_init_tfm,
2228 .exit = atmel_aes_authenc_exit_tfm,
2229 .ivsize = AES_BLOCK_SIZE,
2230 .maxauthsize = SHA384_DIGEST_SIZE,
2231
2232 .base = {
2233 .cra_name = "authenc(hmac(sha384),cbc(aes))",
2234 .cra_driver_name = "atmel-authenc-hmac-sha384-cbc-aes",
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002235 .cra_blocksize = AES_BLOCK_SIZE,
2236 .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002237 },
2238},
2239{
2240 .setkey = atmel_aes_authenc_setkey,
2241 .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
2242 .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
2243 .init = atmel_aes_authenc_hmac_sha512_init_tfm,
2244 .exit = atmel_aes_authenc_exit_tfm,
2245 .ivsize = AES_BLOCK_SIZE,
2246 .maxauthsize = SHA512_DIGEST_SIZE,
2247
2248 .base = {
2249 .cra_name = "authenc(hmac(sha512),cbc(aes))",
2250 .cra_driver_name = "atmel-authenc-hmac-sha512-cbc-aes",
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002251 .cra_blocksize = AES_BLOCK_SIZE,
2252 .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002253 },
2254},
2255};
2256#endif /* CONFIG_CRYPTO_DEV_ATMEL_AUTHENC */
Cyrille Pitchend52db512016-10-03 14:33:16 +02002257
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01002258/* Probe functions */
2259
2260static int atmel_aes_buff_init(struct atmel_aes_dev *dd)
2261{
2262 dd->buf = (void *)__get_free_pages(GFP_KERNEL, ATMEL_AES_BUFFER_ORDER);
2263 dd->buflen = ATMEL_AES_BUFFER_SIZE;
2264 dd->buflen &= ~(AES_BLOCK_SIZE - 1);
2265
2266 if (!dd->buf) {
2267 dev_err(dd->dev, "unable to alloc pages.\n");
2268 return -ENOMEM;
2269 }
2270
2271 return 0;
2272}
2273
2274static void atmel_aes_buff_cleanup(struct atmel_aes_dev *dd)
2275{
2276 free_page((unsigned long)dd->buf);
2277}
2278
Tudor Ambarus827a98d2019-12-13 09:54:49 +00002279static int atmel_aes_dma_init(struct atmel_aes_dev *dd)
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01002280{
Peter Ujfalusi62f72cb2019-11-21 12:16:00 +02002281 int ret;
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01002282
2283 /* Try to grab 2 DMA channels */
Peter Ujfalusi62f72cb2019-11-21 12:16:00 +02002284 dd->src.chan = dma_request_chan(dd->dev, "tx");
2285 if (IS_ERR(dd->src.chan)) {
2286 ret = PTR_ERR(dd->src.chan);
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01002287 goto err_dma_in;
Peter Ujfalusi62f72cb2019-11-21 12:16:00 +02002288 }
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01002289
Peter Ujfalusi62f72cb2019-11-21 12:16:00 +02002290 dd->dst.chan = dma_request_chan(dd->dev, "rx");
2291 if (IS_ERR(dd->dst.chan)) {
2292 ret = PTR_ERR(dd->dst.chan);
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01002293 goto err_dma_out;
Peter Ujfalusi62f72cb2019-11-21 12:16:00 +02002294 }
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01002295
2296 return 0;
2297
2298err_dma_out:
2299 dma_release_channel(dd->src.chan);
2300err_dma_in:
Tudor Ambaruse9ce6ae2019-12-13 09:54:54 +00002301 dev_err(dd->dev, "no DMA channel available\n");
Peter Ujfalusi62f72cb2019-11-21 12:16:00 +02002302 return ret;
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01002303}
2304
2305static void atmel_aes_dma_cleanup(struct atmel_aes_dev *dd)
2306{
2307 dma_release_channel(dd->dst.chan);
2308 dma_release_channel(dd->src.chan);
2309}
2310
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002311static void atmel_aes_queue_task(unsigned long data)
2312{
2313 struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data;
2314
2315 atmel_aes_handle_queue(dd, NULL);
2316}
2317
2318static void atmel_aes_done_task(unsigned long data)
2319{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01002320 struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data;
Cyrille Pitchen10f12c12015-12-17 17:48:42 +01002321
2322 dd->is_async = true;
2323 (void)dd->resume(dd);
2324}
2325
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002326static irqreturn_t atmel_aes_irq(int irq, void *dev_id)
2327{
2328 struct atmel_aes_dev *aes_dd = dev_id;
2329 u32 reg;
2330
2331 reg = atmel_aes_read(aes_dd, AES_ISR);
2332 if (reg & atmel_aes_read(aes_dd, AES_IMR)) {
2333 atmel_aes_write(aes_dd, AES_IDR, reg);
2334 if (AES_FLAGS_BUSY & aes_dd->flags)
2335 tasklet_schedule(&aes_dd->done_task);
2336 else
2337 dev_warn(aes_dd->dev, "AES interrupt when no active requests.\n");
2338 return IRQ_HANDLED;
2339 }
2340
2341 return IRQ_NONE;
2342}
2343
2344static void atmel_aes_unregister_algs(struct atmel_aes_dev *dd)
2345{
2346 int i;
2347
Herbert Xu1520c722019-10-28 15:39:07 +08002348#if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002349 if (dd->caps.has_authenc)
2350 for (i = 0; i < ARRAY_SIZE(aes_authenc_algs); i++)
2351 crypto_unregister_aead(&aes_authenc_algs[i]);
2352#endif
2353
Cyrille Pitchend52db512016-10-03 14:33:16 +02002354 if (dd->caps.has_xts)
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01002355 crypto_unregister_skcipher(&aes_xts_alg);
Cyrille Pitchend52db512016-10-03 14:33:16 +02002356
Cyrille Pitchend4419542015-12-17 18:13:07 +01002357 if (dd->caps.has_gcm)
2358 crypto_unregister_aead(&aes_gcm_alg);
2359
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002360 if (dd->caps.has_cfb64)
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01002361 crypto_unregister_skcipher(&aes_cfb64_alg);
Cyrille Pitchen924a8bc2015-12-17 17:48:35 +01002362
2363 for (i = 0; i < ARRAY_SIZE(aes_algs); i++)
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01002364 crypto_unregister_skcipher(&aes_algs[i]);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002365}
2366
Tudor Ambarusaebe5bd2019-12-05 13:48:39 +00002367static void atmel_aes_crypto_alg_init(struct crypto_alg *alg)
2368{
2369 alg->cra_flags = CRYPTO_ALG_ASYNC;
2370 alg->cra_alignmask = 0xf;
2371 alg->cra_priority = ATMEL_AES_PRIORITY;
2372 alg->cra_module = THIS_MODULE;
2373}
2374
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002375static int atmel_aes_register_algs(struct atmel_aes_dev *dd)
2376{
2377 int err, i, j;
2378
2379 for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
Tudor Ambarusaebe5bd2019-12-05 13:48:39 +00002380 atmel_aes_crypto_alg_init(&aes_algs[i].base);
2381
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01002382 err = crypto_register_skcipher(&aes_algs[i]);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002383 if (err)
2384 goto err_aes_algs;
2385 }
2386
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002387 if (dd->caps.has_cfb64) {
Tudor Ambarusaebe5bd2019-12-05 13:48:39 +00002388 atmel_aes_crypto_alg_init(&aes_cfb64_alg.base);
2389
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01002390 err = crypto_register_skcipher(&aes_cfb64_alg);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002391 if (err)
2392 goto err_aes_cfb64_alg;
2393 }
2394
Cyrille Pitchend4419542015-12-17 18:13:07 +01002395 if (dd->caps.has_gcm) {
Tudor Ambarusaebe5bd2019-12-05 13:48:39 +00002396 atmel_aes_crypto_alg_init(&aes_gcm_alg.base);
2397
Cyrille Pitchend4419542015-12-17 18:13:07 +01002398 err = crypto_register_aead(&aes_gcm_alg);
2399 if (err)
2400 goto err_aes_gcm_alg;
2401 }
2402
Cyrille Pitchend52db512016-10-03 14:33:16 +02002403 if (dd->caps.has_xts) {
Tudor Ambarusaebe5bd2019-12-05 13:48:39 +00002404 atmel_aes_crypto_alg_init(&aes_xts_alg.base);
2405
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01002406 err = crypto_register_skcipher(&aes_xts_alg);
Cyrille Pitchend52db512016-10-03 14:33:16 +02002407 if (err)
2408 goto err_aes_xts_alg;
2409 }
2410
Herbert Xu1520c722019-10-28 15:39:07 +08002411#if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002412 if (dd->caps.has_authenc) {
2413 for (i = 0; i < ARRAY_SIZE(aes_authenc_algs); i++) {
Tudor Ambarusaebe5bd2019-12-05 13:48:39 +00002414 atmel_aes_crypto_alg_init(&aes_authenc_algs[i].base);
2415
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002416 err = crypto_register_aead(&aes_authenc_algs[i]);
2417 if (err)
2418 goto err_aes_authenc_alg;
2419 }
2420 }
2421#endif
2422
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002423 return 0;
2424
Herbert Xu1520c722019-10-28 15:39:07 +08002425#if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002426 /* i = ARRAY_SIZE(aes_authenc_algs); */
2427err_aes_authenc_alg:
2428 for (j = 0; j < i; j++)
2429 crypto_unregister_aead(&aes_authenc_algs[j]);
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01002430 crypto_unregister_skcipher(&aes_xts_alg);
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002431#endif
Cyrille Pitchend52db512016-10-03 14:33:16 +02002432err_aes_xts_alg:
2433 crypto_unregister_aead(&aes_gcm_alg);
Cyrille Pitchend4419542015-12-17 18:13:07 +01002434err_aes_gcm_alg:
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01002435 crypto_unregister_skcipher(&aes_cfb64_alg);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002436err_aes_cfb64_alg:
2437 i = ARRAY_SIZE(aes_algs);
2438err_aes_algs:
2439 for (j = 0; j < i; j++)
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01002440 crypto_unregister_skcipher(&aes_algs[j]);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002441
2442 return err;
2443}
2444
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002445static void atmel_aes_get_cap(struct atmel_aes_dev *dd)
2446{
2447 dd->caps.has_dualbuff = 0;
2448 dd->caps.has_cfb64 = 0;
Cyrille Pitchend4419542015-12-17 18:13:07 +01002449 dd->caps.has_gcm = 0;
Cyrille Pitchend52db512016-10-03 14:33:16 +02002450 dd->caps.has_xts = 0;
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002451 dd->caps.has_authenc = 0;
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002452 dd->caps.max_burst_size = 1;
2453
2454 /* keep only major version number */
2455 switch (dd->hw_version & 0xff0) {
Leilei Zhao973e2092015-12-17 17:48:32 +01002456 case 0x500:
2457 dd->caps.has_dualbuff = 1;
2458 dd->caps.has_cfb64 = 1;
Cyrille Pitchend4419542015-12-17 18:13:07 +01002459 dd->caps.has_gcm = 1;
Cyrille Pitchend52db512016-10-03 14:33:16 +02002460 dd->caps.has_xts = 1;
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002461 dd->caps.has_authenc = 1;
Leilei Zhao973e2092015-12-17 17:48:32 +01002462 dd->caps.max_burst_size = 4;
2463 break;
Leilei Zhaocf1f0d12015-04-07 17:45:02 +08002464 case 0x200:
2465 dd->caps.has_dualbuff = 1;
2466 dd->caps.has_cfb64 = 1;
Cyrille Pitchend4419542015-12-17 18:13:07 +01002467 dd->caps.has_gcm = 1;
Leilei Zhaocf1f0d12015-04-07 17:45:02 +08002468 dd->caps.max_burst_size = 4;
2469 break;
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002470 case 0x130:
2471 dd->caps.has_dualbuff = 1;
2472 dd->caps.has_cfb64 = 1;
2473 dd->caps.max_burst_size = 4;
2474 break;
2475 case 0x120:
2476 break;
2477 default:
2478 dev_warn(dd->dev,
2479 "Unmanaged aes version, set minimum capabilities\n");
2480 break;
2481 }
2482}
2483
Nicolas Ferrebe943c72013-10-14 17:52:38 +02002484#if defined(CONFIG_OF)
2485static const struct of_device_id atmel_aes_dt_ids[] = {
2486 { .compatible = "atmel,at91sam9g46-aes" },
2487 { /* sentinel */ }
2488};
2489MODULE_DEVICE_TABLE(of, atmel_aes_dt_ids);
2490
2491static struct crypto_platform_data *atmel_aes_of_init(struct platform_device *pdev)
2492{
2493 struct device_node *np = pdev->dev.of_node;
2494 struct crypto_platform_data *pdata;
2495
2496 if (!np) {
2497 dev_err(&pdev->dev, "device node not found\n");
2498 return ERR_PTR(-EINVAL);
2499 }
2500
2501 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
Markus Elfring02684832018-02-15 11:38:30 +01002502 if (!pdata)
Nicolas Ferrebe943c72013-10-14 17:52:38 +02002503 return ERR_PTR(-ENOMEM);
Nicolas Ferrebe943c72013-10-14 17:52:38 +02002504
Nicolas Ferrebe943c72013-10-14 17:52:38 +02002505 return pdata;
2506}
2507#else
2508static inline struct crypto_platform_data *atmel_aes_of_init(struct platform_device *pdev)
2509{
2510 return ERR_PTR(-EINVAL);
2511}
2512#endif
2513
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002514static int atmel_aes_probe(struct platform_device *pdev)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002515{
2516 struct atmel_aes_dev *aes_dd;
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002517 struct crypto_platform_data *pdata;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002518 struct device *dev = &pdev->dev;
2519 struct resource *aes_res;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002520 int err;
2521
2522 pdata = pdev->dev.platform_data;
2523 if (!pdata) {
Nicolas Ferrebe943c72013-10-14 17:52:38 +02002524 pdata = atmel_aes_of_init(pdev);
Tudor Ambarusc9063a02019-12-05 09:53:51 +00002525 if (IS_ERR(pdata))
2526 return PTR_ERR(pdata);
Nicolas Ferrebe943c72013-10-14 17:52:38 +02002527 }
2528
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002529 aes_dd = devm_kzalloc(&pdev->dev, sizeof(*aes_dd), GFP_KERNEL);
Tudor Ambarusc9063a02019-12-05 09:53:51 +00002530 if (!aes_dd)
2531 return -ENOMEM;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002532
2533 aes_dd->dev = dev;
2534
2535 platform_set_drvdata(pdev, aes_dd);
2536
2537 INIT_LIST_HEAD(&aes_dd->list);
Leilei Zhao8a10eb82015-04-07 17:45:09 +08002538 spin_lock_init(&aes_dd->lock);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002539
2540 tasklet_init(&aes_dd->done_task, atmel_aes_done_task,
2541 (unsigned long)aes_dd);
2542 tasklet_init(&aes_dd->queue_task, atmel_aes_queue_task,
2543 (unsigned long)aes_dd);
2544
2545 crypto_init_queue(&aes_dd->queue, ATMEL_AES_QUEUE_LENGTH);
2546
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002547 /* Get the base address */
2548 aes_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2549 if (!aes_res) {
2550 dev_err(dev, "no MEM resource info\n");
2551 err = -ENODEV;
Tudor Ambaruse7836512019-12-05 09:53:53 +00002552 goto err_tasklet_kill;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002553 }
2554 aes_dd->phys_base = aes_res->start;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002555
2556 /* Get the IRQ */
2557 aes_dd->irq = platform_get_irq(pdev, 0);
2558 if (aes_dd->irq < 0) {
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002559 err = aes_dd->irq;
Tudor Ambaruse7836512019-12-05 09:53:53 +00002560 goto err_tasklet_kill;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002561 }
2562
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002563 err = devm_request_irq(&pdev->dev, aes_dd->irq, atmel_aes_irq,
2564 IRQF_SHARED, "atmel-aes", aes_dd);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002565 if (err) {
2566 dev_err(dev, "unable to request aes irq.\n");
Tudor Ambaruse7836512019-12-05 09:53:53 +00002567 goto err_tasklet_kill;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002568 }
2569
2570 /* Initializing the clock */
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002571 aes_dd->iclk = devm_clk_get(&pdev->dev, "aes_clk");
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002572 if (IS_ERR(aes_dd->iclk)) {
Colin Ian Kingbe208352015-02-28 20:40:10 +00002573 dev_err(dev, "clock initialization failed.\n");
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002574 err = PTR_ERR(aes_dd->iclk);
Tudor Ambaruse7836512019-12-05 09:53:53 +00002575 goto err_tasklet_kill;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002576 }
2577
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002578 aes_dd->io_base = devm_ioremap_resource(&pdev->dev, aes_res);
Vladimir Zapolskiy9b52d552016-03-06 03:21:52 +02002579 if (IS_ERR(aes_dd->io_base)) {
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002580 dev_err(dev, "can't ioremap\n");
Vladimir Zapolskiy9b52d552016-03-06 03:21:52 +02002581 err = PTR_ERR(aes_dd->io_base);
Tudor Ambaruse7836512019-12-05 09:53:53 +00002582 goto err_tasklet_kill;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002583 }
2584
Cyrille Pitchen49a20452016-01-29 17:53:33 +01002585 err = clk_prepare(aes_dd->iclk);
Cyrille Pitchenaab0a392015-12-17 17:48:37 +01002586 if (err)
Tudor Ambaruse7836512019-12-05 09:53:53 +00002587 goto err_tasklet_kill;
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002588
Cyrille Pitchen49a20452016-01-29 17:53:33 +01002589 err = atmel_aes_hw_version_init(aes_dd);
2590 if (err)
Tudor Ambaruse7836512019-12-05 09:53:53 +00002591 goto err_iclk_unprepare;
Cyrille Pitchen49a20452016-01-29 17:53:33 +01002592
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002593 atmel_aes_get_cap(aes_dd);
2594
Herbert Xu1520c722019-10-28 15:39:07 +08002595#if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002596 if (aes_dd->caps.has_authenc && !atmel_sha_authenc_is_ready()) {
2597 err = -EPROBE_DEFER;
Tudor Ambaruse7836512019-12-05 09:53:53 +00002598 goto err_iclk_unprepare;
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002599 }
2600#endif
2601
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002602 err = atmel_aes_buff_init(aes_dd);
2603 if (err)
Tudor Ambaruse7836512019-12-05 09:53:53 +00002604 goto err_iclk_unprepare;
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002605
Tudor Ambarus827a98d2019-12-13 09:54:49 +00002606 err = atmel_aes_dma_init(aes_dd);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002607 if (err)
Tudor Ambaruse7836512019-12-05 09:53:53 +00002608 goto err_buff_cleanup;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002609
2610 spin_lock(&atmel_aes.lock);
2611 list_add_tail(&aes_dd->list, &atmel_aes.dev_list);
2612 spin_unlock(&atmel_aes.lock);
2613
2614 err = atmel_aes_register_algs(aes_dd);
2615 if (err)
2616 goto err_algs;
2617
Nicolas Ferrebe943c72013-10-14 17:52:38 +02002618 dev_info(dev, "Atmel AES - Using %s, %s for DMA transfers\n",
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +01002619 dma_chan_name(aes_dd->src.chan),
2620 dma_chan_name(aes_dd->dst.chan));
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002621
2622 return 0;
2623
2624err_algs:
2625 spin_lock(&atmel_aes.lock);
2626 list_del(&aes_dd->list);
2627 spin_unlock(&atmel_aes.lock);
2628 atmel_aes_dma_cleanup(aes_dd);
Tudor Ambaruse7836512019-12-05 09:53:53 +00002629err_buff_cleanup:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002630 atmel_aes_buff_cleanup(aes_dd);
Tudor Ambaruse7836512019-12-05 09:53:53 +00002631err_iclk_unprepare:
Cyrille Pitchen49a20452016-01-29 17:53:33 +01002632 clk_unprepare(aes_dd->iclk);
Tudor Ambaruse7836512019-12-05 09:53:53 +00002633err_tasklet_kill:
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002634 tasklet_kill(&aes_dd->done_task);
2635 tasklet_kill(&aes_dd->queue_task);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002636
2637 return err;
2638}
2639
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002640static int atmel_aes_remove(struct platform_device *pdev)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002641{
Wei Yongjunfc783342016-10-24 14:51:22 +00002642 struct atmel_aes_dev *aes_dd;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002643
2644 aes_dd = platform_get_drvdata(pdev);
2645 if (!aes_dd)
2646 return -ENODEV;
2647 spin_lock(&atmel_aes.lock);
2648 list_del(&aes_dd->list);
2649 spin_unlock(&atmel_aes.lock);
2650
2651 atmel_aes_unregister_algs(aes_dd);
2652
2653 tasklet_kill(&aes_dd->done_task);
2654 tasklet_kill(&aes_dd->queue_task);
2655
2656 atmel_aes_dma_cleanup(aes_dd);
Cyrille Pitchen2a377822015-12-17 17:48:46 +01002657 atmel_aes_buff_cleanup(aes_dd);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002658
Cyrille Pitchen49a20452016-01-29 17:53:33 +01002659 clk_unprepare(aes_dd->iclk);
2660
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002661 return 0;
2662}
2663
2664static struct platform_driver atmel_aes_driver = {
2665 .probe = atmel_aes_probe,
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002666 .remove = atmel_aes_remove,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002667 .driver = {
2668 .name = "atmel_aes",
Nicolas Ferrebe943c72013-10-14 17:52:38 +02002669 .of_match_table = of_match_ptr(atmel_aes_dt_ids),
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002670 },
2671};
2672
2673module_platform_driver(atmel_aes_driver);
2674
2675MODULE_DESCRIPTION("Atmel AES hw acceleration support.");
2676MODULE_LICENSE("GPL v2");
2677MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");