blob: bc033178d0e7e9a44e64eb99ad9fe70138c4c0c2 [file] [log] [blame]
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001/*
2 * Cryptographic API.
3 *
4 * Support for ATMEL SHA1/SHA256 HW acceleration.
5 *
6 * Copyright (c) 2012 Eukréa Electromatique - ATMEL
7 * Author: Nicolas Royer <nicolas@eukrea.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as published
11 * by the Free Software Foundation.
12 *
13 * Some ideas are from omap-sham.c drivers.
14 */
15
16
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/err.h>
21#include <linux/clk.h>
22#include <linux/io.h>
23#include <linux/hw_random.h>
24#include <linux/platform_device.h>
25
26#include <linux/device.h>
Nicolas Royerebc82ef2012-07-01 19:19:46 +020027#include <linux/init.h>
28#include <linux/errno.h>
29#include <linux/interrupt.h>
Nicolas Royerebc82ef2012-07-01 19:19:46 +020030#include <linux/irq.h>
Nicolas Royerebc82ef2012-07-01 19:19:46 +020031#include <linux/scatterlist.h>
32#include <linux/dma-mapping.h>
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +020033#include <linux/of_device.h>
Nicolas Royerebc82ef2012-07-01 19:19:46 +020034#include <linux/delay.h>
35#include <linux/crypto.h>
36#include <linux/cryptohash.h>
37#include <crypto/scatterwalk.h>
38#include <crypto/algapi.h>
39#include <crypto/sha.h>
40#include <crypto/hash.h>
41#include <crypto/internal/hash.h>
Nicolas Royerd4905b32013-02-20 17:10:26 +010042#include <linux/platform_data/crypto-atmel.h>
Nicolas Royerebc82ef2012-07-01 19:19:46 +020043#include "atmel-sha-regs.h"
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +010044#include "atmel-authenc.h"
Nicolas Royerebc82ef2012-07-01 19:19:46 +020045
46/* SHA flags */
47#define SHA_FLAGS_BUSY BIT(0)
48#define SHA_FLAGS_FINAL BIT(1)
49#define SHA_FLAGS_DMA_ACTIVE BIT(2)
50#define SHA_FLAGS_OUTPUT_READY BIT(3)
51#define SHA_FLAGS_INIT BIT(4)
52#define SHA_FLAGS_CPU BIT(5)
53#define SHA_FLAGS_DMA_READY BIT(6)
Cyrille Pitchen0569fc42017-01-26 17:07:57 +010054#define SHA_FLAGS_DUMP_REG BIT(7)
Nicolas Royerebc82ef2012-07-01 19:19:46 +020055
Cyrille Pitchen81d87502017-01-26 17:07:54 +010056/* bits[11:8] are reserved. */
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +010057
Nicolas Royerebc82ef2012-07-01 19:19:46 +020058#define SHA_FLAGS_FINUP BIT(16)
59#define SHA_FLAGS_SG BIT(17)
Nicolas Royerd4905b32013-02-20 17:10:26 +010060#define SHA_FLAGS_ERROR BIT(23)
61#define SHA_FLAGS_PAD BIT(24)
Cyrille Pitchen7cee3502016-01-15 15:49:34 +010062#define SHA_FLAGS_RESTORE BIT(25)
Cyrille Pitcheneec12f62017-01-26 17:07:52 +010063#define SHA_FLAGS_IDATAR0 BIT(26)
64#define SHA_FLAGS_WAIT_DATARDY BIT(27)
Nicolas Royerebc82ef2012-07-01 19:19:46 +020065
Cyrille Pitchen81d87502017-01-26 17:07:54 +010066#define SHA_OP_INIT 0
Nicolas Royerebc82ef2012-07-01 19:19:46 +020067#define SHA_OP_UPDATE 1
68#define SHA_OP_FINAL 2
Cyrille Pitchen81d87502017-01-26 17:07:54 +010069#define SHA_OP_DIGEST 3
Nicolas Royerebc82ef2012-07-01 19:19:46 +020070
Cyrille Pitchencc831d32016-01-29 17:04:02 +010071#define SHA_BUFFER_LEN (PAGE_SIZE / 16)
Nicolas Royerebc82ef2012-07-01 19:19:46 +020072
73#define ATMEL_SHA_DMA_THRESHOLD 56
74
Nicolas Royerd4905b32013-02-20 17:10:26 +010075struct atmel_sha_caps {
76 bool has_dma;
77 bool has_dualbuff;
78 bool has_sha224;
79 bool has_sha_384_512;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +010080 bool has_uihv;
Cyrille Pitchen81d87502017-01-26 17:07:54 +010081 bool has_hmac;
Nicolas Royerd4905b32013-02-20 17:10:26 +010082};
Nicolas Royerebc82ef2012-07-01 19:19:46 +020083
84struct atmel_sha_dev;
85
Cyrille Pitchencc831d32016-01-29 17:04:02 +010086/*
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +010087 * .statesize = sizeof(struct atmel_sha_reqctx) must be <= PAGE_SIZE / 8 as
Cyrille Pitchencc831d32016-01-29 17:04:02 +010088 * tested by the ahash_prepare_alg() function.
89 */
Nicolas Royerebc82ef2012-07-01 19:19:46 +020090struct atmel_sha_reqctx {
91 struct atmel_sha_dev *dd;
92 unsigned long flags;
93 unsigned long op;
94
Nicolas Royerd4905b32013-02-20 17:10:26 +010095 u8 digest[SHA512_DIGEST_SIZE] __aligned(sizeof(u32));
96 u64 digcnt[2];
Nicolas Royerebc82ef2012-07-01 19:19:46 +020097 size_t bufcnt;
98 size_t buflen;
99 dma_addr_t dma_addr;
100
101 /* walk state */
102 struct scatterlist *sg;
103 unsigned int offset; /* offset in current sg */
104 unsigned int total; /* total request */
105
Nicolas Royerd4905b32013-02-20 17:10:26 +0100106 size_t block_size;
Cyrille Pitchen81d87502017-01-26 17:07:54 +0100107 size_t hash_size;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100108
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +0100109 u8 buffer[SHA_BUFFER_LEN + SHA512_BLOCK_SIZE] __aligned(sizeof(u32));
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200110};
111
Cyrille Pitchena29af932017-01-26 17:07:47 +0100112typedef int (*atmel_sha_fn_t)(struct atmel_sha_dev *);
113
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200114struct atmel_sha_ctx {
115 struct atmel_sha_dev *dd;
Cyrille Pitchena29af932017-01-26 17:07:47 +0100116 atmel_sha_fn_t start;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200117
118 unsigned long flags;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200119};
120
Nicolas Royerd4905b32013-02-20 17:10:26 +0100121#define ATMEL_SHA_QUEUE_LENGTH 50
122
123struct atmel_sha_dma {
124 struct dma_chan *chan;
125 struct dma_slave_config dma_conf;
Cyrille Pitchen69303cf2017-01-26 17:07:53 +0100126 struct scatterlist *sg;
127 int nents;
128 unsigned int last_sg_length;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100129};
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200130
131struct atmel_sha_dev {
132 struct list_head list;
133 unsigned long phys_base;
134 struct device *dev;
135 struct clk *iclk;
136 int irq;
137 void __iomem *io_base;
138
139 spinlock_t lock;
140 int err;
141 struct tasklet_struct done_task;
Cyrille Pitchenf56809c2016-01-15 15:49:32 +0100142 struct tasklet_struct queue_task;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200143
144 unsigned long flags;
145 struct crypto_queue queue;
146 struct ahash_request *req;
Cyrille Pitchena29af932017-01-26 17:07:47 +0100147 bool is_async;
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +0100148 bool force_complete;
Cyrille Pitchenb5ce82a2017-01-26 17:07:48 +0100149 atmel_sha_fn_t resume;
Cyrille Pitcheneec12f62017-01-26 17:07:52 +0100150 atmel_sha_fn_t cpu_transfer_complete;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100151
152 struct atmel_sha_dma dma_lch_in;
153
154 struct atmel_sha_caps caps;
155
Cyrille Pitchen81d87502017-01-26 17:07:54 +0100156 struct scatterlist tmp;
157
Nicolas Royerd4905b32013-02-20 17:10:26 +0100158 u32 hw_version;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200159};
160
161struct atmel_sha_drv {
162 struct list_head dev_list;
163 spinlock_t lock;
164};
165
166static struct atmel_sha_drv atmel_sha = {
167 .dev_list = LIST_HEAD_INIT(atmel_sha.dev_list),
168 .lock = __SPIN_LOCK_UNLOCKED(atmel_sha.lock),
169};
170
Cyrille Pitchen0569fc42017-01-26 17:07:57 +0100171#ifdef VERBOSE_DEBUG
172static const char *atmel_sha_reg_name(u32 offset, char *tmp, size_t sz, bool wr)
173{
174 switch (offset) {
175 case SHA_CR:
176 return "CR";
177
178 case SHA_MR:
179 return "MR";
180
181 case SHA_IER:
182 return "IER";
183
184 case SHA_IDR:
185 return "IDR";
186
187 case SHA_IMR:
188 return "IMR";
189
190 case SHA_ISR:
191 return "ISR";
192
193 case SHA_MSR:
194 return "MSR";
195
196 case SHA_BCR:
197 return "BCR";
198
199 case SHA_REG_DIN(0):
200 case SHA_REG_DIN(1):
201 case SHA_REG_DIN(2):
202 case SHA_REG_DIN(3):
203 case SHA_REG_DIN(4):
204 case SHA_REG_DIN(5):
205 case SHA_REG_DIN(6):
206 case SHA_REG_DIN(7):
207 case SHA_REG_DIN(8):
208 case SHA_REG_DIN(9):
209 case SHA_REG_DIN(10):
210 case SHA_REG_DIN(11):
211 case SHA_REG_DIN(12):
212 case SHA_REG_DIN(13):
213 case SHA_REG_DIN(14):
214 case SHA_REG_DIN(15):
215 snprintf(tmp, sz, "IDATAR[%u]", (offset - SHA_REG_DIN(0)) >> 2);
216 break;
217
218 case SHA_REG_DIGEST(0):
219 case SHA_REG_DIGEST(1):
220 case SHA_REG_DIGEST(2):
221 case SHA_REG_DIGEST(3):
222 case SHA_REG_DIGEST(4):
223 case SHA_REG_DIGEST(5):
224 case SHA_REG_DIGEST(6):
225 case SHA_REG_DIGEST(7):
226 case SHA_REG_DIGEST(8):
227 case SHA_REG_DIGEST(9):
228 case SHA_REG_DIGEST(10):
229 case SHA_REG_DIGEST(11):
230 case SHA_REG_DIGEST(12):
231 case SHA_REG_DIGEST(13):
232 case SHA_REG_DIGEST(14):
233 case SHA_REG_DIGEST(15):
234 if (wr)
235 snprintf(tmp, sz, "IDATAR[%u]",
236 16u + ((offset - SHA_REG_DIGEST(0)) >> 2));
237 else
238 snprintf(tmp, sz, "ODATAR[%u]",
239 (offset - SHA_REG_DIGEST(0)) >> 2);
240 break;
241
242 case SHA_HW_VERSION:
243 return "HWVER";
244
245 default:
246 snprintf(tmp, sz, "0x%02x", offset);
247 break;
248 }
249
250 return tmp;
251}
252
253#endif /* VERBOSE_DEBUG */
254
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200255static inline u32 atmel_sha_read(struct atmel_sha_dev *dd, u32 offset)
256{
Cyrille Pitchen0569fc42017-01-26 17:07:57 +0100257 u32 value = readl_relaxed(dd->io_base + offset);
258
259#ifdef VERBOSE_DEBUG
260 if (dd->flags & SHA_FLAGS_DUMP_REG) {
261 char tmp[16];
262
263 dev_vdbg(dd->dev, "read 0x%08x from %s\n", value,
264 atmel_sha_reg_name(offset, tmp, sizeof(tmp), false));
265 }
266#endif /* VERBOSE_DEBUG */
267
268 return value;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200269}
270
271static inline void atmel_sha_write(struct atmel_sha_dev *dd,
272 u32 offset, u32 value)
273{
Cyrille Pitchen0569fc42017-01-26 17:07:57 +0100274#ifdef VERBOSE_DEBUG
275 if (dd->flags & SHA_FLAGS_DUMP_REG) {
276 char tmp[16];
277
278 dev_vdbg(dd->dev, "write 0x%08x into %s\n", value,
279 atmel_sha_reg_name(offset, tmp, sizeof(tmp), true));
280 }
281#endif /* VERBOSE_DEBUG */
282
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200283 writel_relaxed(value, dd->io_base + offset);
284}
285
Cyrille Pitchena29af932017-01-26 17:07:47 +0100286static inline int atmel_sha_complete(struct atmel_sha_dev *dd, int err)
287{
288 struct ahash_request *req = dd->req;
289
290 dd->flags &= ~(SHA_FLAGS_BUSY | SHA_FLAGS_FINAL | SHA_FLAGS_CPU |
Cyrille Pitchen0569fc42017-01-26 17:07:57 +0100291 SHA_FLAGS_DMA_READY | SHA_FLAGS_OUTPUT_READY |
292 SHA_FLAGS_DUMP_REG);
Cyrille Pitchena29af932017-01-26 17:07:47 +0100293
294 clk_disable(dd->iclk);
295
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +0100296 if ((dd->is_async || dd->force_complete) && req->base.complete)
Cyrille Pitchena29af932017-01-26 17:07:47 +0100297 req->base.complete(&req->base, err);
298
299 /* handle new request */
300 tasklet_schedule(&dd->queue_task);
301
302 return err;
303}
304
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200305static size_t atmel_sha_append_sg(struct atmel_sha_reqctx *ctx)
306{
307 size_t count;
308
309 while ((ctx->bufcnt < ctx->buflen) && ctx->total) {
310 count = min(ctx->sg->length - ctx->offset, ctx->total);
311 count = min(count, ctx->buflen - ctx->bufcnt);
312
Leilei Zhao803eeae2015-04-07 17:45:05 +0800313 if (count <= 0) {
314 /*
315 * Check if count <= 0 because the buffer is full or
316 * because the sg length is 0. In the latest case,
317 * check if there is another sg in the list, a 0 length
318 * sg doesn't necessarily mean the end of the sg list.
319 */
320 if ((ctx->sg->length == 0) && !sg_is_last(ctx->sg)) {
321 ctx->sg = sg_next(ctx->sg);
322 continue;
323 } else {
324 break;
325 }
326 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200327
328 scatterwalk_map_and_copy(ctx->buffer + ctx->bufcnt, ctx->sg,
329 ctx->offset, count, 0);
330
331 ctx->bufcnt += count;
332 ctx->offset += count;
333 ctx->total -= count;
334
335 if (ctx->offset == ctx->sg->length) {
336 ctx->sg = sg_next(ctx->sg);
337 if (ctx->sg)
338 ctx->offset = 0;
339 else
340 ctx->total = 0;
341 }
342 }
343
344 return 0;
345}
346
347/*
Nicolas Royerd4905b32013-02-20 17:10:26 +0100348 * The purpose of this padding is to ensure that the padded message is a
349 * multiple of 512 bits (SHA1/SHA224/SHA256) or 1024 bits (SHA384/SHA512).
350 * The bit "1" is appended at the end of the message followed by
351 * "padlen-1" zero bits. Then a 64 bits block (SHA1/SHA224/SHA256) or
352 * 128 bits block (SHA384/SHA512) equals to the message length in bits
353 * is appended.
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200354 *
Nicolas Royerd4905b32013-02-20 17:10:26 +0100355 * For SHA1/SHA224/SHA256, padlen is calculated as followed:
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200356 * - if message length < 56 bytes then padlen = 56 - message length
357 * - else padlen = 64 + 56 - message length
Nicolas Royerd4905b32013-02-20 17:10:26 +0100358 *
359 * For SHA384/SHA512, padlen is calculated as followed:
360 * - if message length < 112 bytes then padlen = 112 - message length
361 * - else padlen = 128 + 112 - message length
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200362 */
363static void atmel_sha_fill_padding(struct atmel_sha_reqctx *ctx, int length)
364{
365 unsigned int index, padlen;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100366 u64 bits[2];
367 u64 size[2];
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200368
Nicolas Royerd4905b32013-02-20 17:10:26 +0100369 size[0] = ctx->digcnt[0];
370 size[1] = ctx->digcnt[1];
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200371
Nicolas Royerd4905b32013-02-20 17:10:26 +0100372 size[0] += ctx->bufcnt;
373 if (size[0] < ctx->bufcnt)
374 size[1]++;
375
376 size[0] += length;
377 if (size[0] < length)
378 size[1]++;
379
380 bits[1] = cpu_to_be64(size[0] << 3);
381 bits[0] = cpu_to_be64(size[1] << 3 | size[0] >> 61);
382
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100383 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
384 case SHA_FLAGS_SHA384:
385 case SHA_FLAGS_SHA512:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100386 index = ctx->bufcnt & 0x7f;
387 padlen = (index < 112) ? (112 - index) : ((128+112) - index);
388 *(ctx->buffer + ctx->bufcnt) = 0x80;
389 memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1);
390 memcpy(ctx->buffer + ctx->bufcnt + padlen, bits, 16);
391 ctx->bufcnt += padlen + 16;
392 ctx->flags |= SHA_FLAGS_PAD;
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100393 break;
394
395 default:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100396 index = ctx->bufcnt & 0x3f;
397 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
398 *(ctx->buffer + ctx->bufcnt) = 0x80;
399 memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1);
400 memcpy(ctx->buffer + ctx->bufcnt + padlen, &bits[1], 8);
401 ctx->bufcnt += padlen + 8;
402 ctx->flags |= SHA_FLAGS_PAD;
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100403 break;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100404 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200405}
406
Cyrille Pitchen8340c7f2017-01-26 17:07:46 +0100407static struct atmel_sha_dev *atmel_sha_find_dev(struct atmel_sha_ctx *tctx)
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200408{
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200409 struct atmel_sha_dev *dd = NULL;
410 struct atmel_sha_dev *tmp;
411
412 spin_lock_bh(&atmel_sha.lock);
413 if (!tctx->dd) {
414 list_for_each_entry(tmp, &atmel_sha.dev_list, list) {
415 dd = tmp;
416 break;
417 }
418 tctx->dd = dd;
419 } else {
420 dd = tctx->dd;
421 }
422
423 spin_unlock_bh(&atmel_sha.lock);
424
Cyrille Pitchen8340c7f2017-01-26 17:07:46 +0100425 return dd;
426}
427
428static int atmel_sha_init(struct ahash_request *req)
429{
430 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
431 struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm);
432 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
433 struct atmel_sha_dev *dd = atmel_sha_find_dev(tctx);
434
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200435 ctx->dd = dd;
436
437 ctx->flags = 0;
438
439 dev_dbg(dd->dev, "init: digest size: %d\n",
440 crypto_ahash_digestsize(tfm));
441
Nicolas Royerd4905b32013-02-20 17:10:26 +0100442 switch (crypto_ahash_digestsize(tfm)) {
443 case SHA1_DIGEST_SIZE:
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200444 ctx->flags |= SHA_FLAGS_SHA1;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100445 ctx->block_size = SHA1_BLOCK_SIZE;
446 break;
447 case SHA224_DIGEST_SIZE:
448 ctx->flags |= SHA_FLAGS_SHA224;
449 ctx->block_size = SHA224_BLOCK_SIZE;
450 break;
451 case SHA256_DIGEST_SIZE:
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200452 ctx->flags |= SHA_FLAGS_SHA256;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100453 ctx->block_size = SHA256_BLOCK_SIZE;
454 break;
455 case SHA384_DIGEST_SIZE:
456 ctx->flags |= SHA_FLAGS_SHA384;
457 ctx->block_size = SHA384_BLOCK_SIZE;
458 break;
459 case SHA512_DIGEST_SIZE:
460 ctx->flags |= SHA_FLAGS_SHA512;
461 ctx->block_size = SHA512_BLOCK_SIZE;
462 break;
463 default:
464 return -EINVAL;
465 break;
466 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200467
468 ctx->bufcnt = 0;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100469 ctx->digcnt[0] = 0;
470 ctx->digcnt[1] = 0;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200471 ctx->buflen = SHA_BUFFER_LEN;
472
473 return 0;
474}
475
476static void atmel_sha_write_ctrl(struct atmel_sha_dev *dd, int dma)
477{
478 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100479 u32 valmr = SHA_MR_MODE_AUTO;
480 unsigned int i, hashsize = 0;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200481
482 if (likely(dma)) {
Nicolas Royerd4905b32013-02-20 17:10:26 +0100483 if (!dd->caps.has_dma)
484 atmel_sha_write(dd, SHA_IER, SHA_INT_TXBUFE);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200485 valmr = SHA_MR_MODE_PDC;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100486 if (dd->caps.has_dualbuff)
487 valmr |= SHA_MR_DUALBUFF;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200488 } else {
489 atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
490 }
491
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100492 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
493 case SHA_FLAGS_SHA1:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100494 valmr |= SHA_MR_ALGO_SHA1;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100495 hashsize = SHA1_DIGEST_SIZE;
496 break;
497
498 case SHA_FLAGS_SHA224:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100499 valmr |= SHA_MR_ALGO_SHA224;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100500 hashsize = SHA256_DIGEST_SIZE;
501 break;
502
503 case SHA_FLAGS_SHA256:
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200504 valmr |= SHA_MR_ALGO_SHA256;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100505 hashsize = SHA256_DIGEST_SIZE;
506 break;
507
508 case SHA_FLAGS_SHA384:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100509 valmr |= SHA_MR_ALGO_SHA384;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100510 hashsize = SHA512_DIGEST_SIZE;
511 break;
512
513 case SHA_FLAGS_SHA512:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100514 valmr |= SHA_MR_ALGO_SHA512;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100515 hashsize = SHA512_DIGEST_SIZE;
516 break;
517
518 default:
519 break;
520 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200521
522 /* Setting CR_FIRST only for the first iteration */
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100523 if (!(ctx->digcnt[0] || ctx->digcnt[1])) {
524 atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
525 } else if (dd->caps.has_uihv && (ctx->flags & SHA_FLAGS_RESTORE)) {
526 const u32 *hash = (const u32 *)ctx->digest;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200527
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100528 /*
529 * Restore the hardware context: update the User Initialize
530 * Hash Value (UIHV) with the value saved when the latest
531 * 'update' operation completed on this very same crypto
532 * request.
533 */
534 ctx->flags &= ~SHA_FLAGS_RESTORE;
535 atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
536 for (i = 0; i < hashsize / sizeof(u32); ++i)
537 atmel_sha_write(dd, SHA_REG_DIN(i), hash[i]);
538 atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
539 valmr |= SHA_MR_UIHV;
540 }
541 /*
542 * WARNING: If the UIHV feature is not available, the hardware CANNOT
543 * process concurrent requests: the internal registers used to store
544 * the hash/digest are still set to the partial digest output values
545 * computed during the latest round.
546 */
547
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200548 atmel_sha_write(dd, SHA_MR, valmr);
549}
550
Cyrille Pitchen9064ed92017-01-26 17:07:50 +0100551static inline int atmel_sha_wait_for_data_ready(struct atmel_sha_dev *dd,
552 atmel_sha_fn_t resume)
553{
554 u32 isr = atmel_sha_read(dd, SHA_ISR);
555
556 if (unlikely(isr & SHA_INT_DATARDY))
557 return resume(dd);
558
559 dd->resume = resume;
560 atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
561 return -EINPROGRESS;
562}
563
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200564static int atmel_sha_xmit_cpu(struct atmel_sha_dev *dd, const u8 *buf,
565 size_t length, int final)
566{
567 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
568 int count, len32;
569 const u32 *buffer = (const u32 *)buf;
570
Arnd Bergmann4c147bc2017-02-06 13:32:16 +0100571 dev_dbg(dd->dev, "xmit_cpu: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
Nicolas Royerd4905b32013-02-20 17:10:26 +0100572 ctx->digcnt[1], ctx->digcnt[0], length, final);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200573
574 atmel_sha_write_ctrl(dd, 0);
575
576 /* should be non-zero before next lines to disable clocks later */
Nicolas Royerd4905b32013-02-20 17:10:26 +0100577 ctx->digcnt[0] += length;
578 if (ctx->digcnt[0] < length)
579 ctx->digcnt[1]++;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200580
581 if (final)
582 dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
583
584 len32 = DIV_ROUND_UP(length, sizeof(u32));
585
586 dd->flags |= SHA_FLAGS_CPU;
587
588 for (count = 0; count < len32; count++)
589 atmel_sha_write(dd, SHA_REG_DIN(count), buffer[count]);
590
591 return -EINPROGRESS;
592}
593
594static int atmel_sha_xmit_pdc(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
595 size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
596{
597 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
598 int len32;
599
Arnd Bergmann4c147bc2017-02-06 13:32:16 +0100600 dev_dbg(dd->dev, "xmit_pdc: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
Nicolas Royerd4905b32013-02-20 17:10:26 +0100601 ctx->digcnt[1], ctx->digcnt[0], length1, final);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200602
603 len32 = DIV_ROUND_UP(length1, sizeof(u32));
604 atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTDIS);
605 atmel_sha_write(dd, SHA_TPR, dma_addr1);
606 atmel_sha_write(dd, SHA_TCR, len32);
607
608 len32 = DIV_ROUND_UP(length2, sizeof(u32));
609 atmel_sha_write(dd, SHA_TNPR, dma_addr2);
610 atmel_sha_write(dd, SHA_TNCR, len32);
611
612 atmel_sha_write_ctrl(dd, 1);
613
614 /* should be non-zero before next lines to disable clocks later */
Nicolas Royerd4905b32013-02-20 17:10:26 +0100615 ctx->digcnt[0] += length1;
616 if (ctx->digcnt[0] < length1)
617 ctx->digcnt[1]++;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200618
619 if (final)
620 dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
621
622 dd->flags |= SHA_FLAGS_DMA_ACTIVE;
623
624 /* Start DMA transfer */
625 atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTEN);
626
627 return -EINPROGRESS;
628}
629
Nicolas Royerd4905b32013-02-20 17:10:26 +0100630static void atmel_sha_dma_callback(void *data)
631{
632 struct atmel_sha_dev *dd = data;
633
Cyrille Pitchena29af932017-01-26 17:07:47 +0100634 dd->is_async = true;
635
Nicolas Royerd4905b32013-02-20 17:10:26 +0100636 /* dma_lch_in - completed - wait DATRDY */
637 atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
638}
639
640static int atmel_sha_xmit_dma(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
641 size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
642{
643 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
644 struct dma_async_tx_descriptor *in_desc;
645 struct scatterlist sg[2];
646
Arnd Bergmann4c147bc2017-02-06 13:32:16 +0100647 dev_dbg(dd->dev, "xmit_dma: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
Nicolas Royerd4905b32013-02-20 17:10:26 +0100648 ctx->digcnt[1], ctx->digcnt[0], length1, final);
649
Leilei Zhao3f1992c2015-04-07 17:45:07 +0800650 dd->dma_lch_in.dma_conf.src_maxburst = 16;
651 dd->dma_lch_in.dma_conf.dst_maxburst = 16;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100652
653 dmaengine_slave_config(dd->dma_lch_in.chan, &dd->dma_lch_in.dma_conf);
654
655 if (length2) {
656 sg_init_table(sg, 2);
657 sg_dma_address(&sg[0]) = dma_addr1;
658 sg_dma_len(&sg[0]) = length1;
659 sg_dma_address(&sg[1]) = dma_addr2;
660 sg_dma_len(&sg[1]) = length2;
661 in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 2,
662 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
663 } else {
664 sg_init_table(sg, 1);
665 sg_dma_address(&sg[0]) = dma_addr1;
666 sg_dma_len(&sg[0]) = length1;
667 in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 1,
668 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
669 }
670 if (!in_desc)
Cyrille Pitchendd3f9f42017-02-09 17:51:20 +0100671 return atmel_sha_complete(dd, -EINVAL);
Nicolas Royerd4905b32013-02-20 17:10:26 +0100672
673 in_desc->callback = atmel_sha_dma_callback;
674 in_desc->callback_param = dd;
675
676 atmel_sha_write_ctrl(dd, 1);
677
678 /* should be non-zero before next lines to disable clocks later */
679 ctx->digcnt[0] += length1;
680 if (ctx->digcnt[0] < length1)
681 ctx->digcnt[1]++;
682
683 if (final)
684 dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
685
686 dd->flags |= SHA_FLAGS_DMA_ACTIVE;
687
688 /* Start DMA transfer */
689 dmaengine_submit(in_desc);
690 dma_async_issue_pending(dd->dma_lch_in.chan);
691
692 return -EINPROGRESS;
693}
694
695static int atmel_sha_xmit_start(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
696 size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
697{
698 if (dd->caps.has_dma)
699 return atmel_sha_xmit_dma(dd, dma_addr1, length1,
700 dma_addr2, length2, final);
701 else
702 return atmel_sha_xmit_pdc(dd, dma_addr1, length1,
703 dma_addr2, length2, final);
704}
705
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200706static int atmel_sha_update_cpu(struct atmel_sha_dev *dd)
707{
708 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
709 int bufcnt;
710
711 atmel_sha_append_sg(ctx);
712 atmel_sha_fill_padding(ctx, 0);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200713 bufcnt = ctx->bufcnt;
714 ctx->bufcnt = 0;
715
716 return atmel_sha_xmit_cpu(dd, ctx->buffer, bufcnt, 1);
717}
718
719static int atmel_sha_xmit_dma_map(struct atmel_sha_dev *dd,
720 struct atmel_sha_reqctx *ctx,
721 size_t length, int final)
722{
723 ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
Nicolas Royerd4905b32013-02-20 17:10:26 +0100724 ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200725 if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
Arnd Bergmann4c147bc2017-02-06 13:32:16 +0100726 dev_err(dd->dev, "dma %zu bytes error\n", ctx->buflen +
Nicolas Royerd4905b32013-02-20 17:10:26 +0100727 ctx->block_size);
Cyrille Pitchendd3f9f42017-02-09 17:51:20 +0100728 return atmel_sha_complete(dd, -EINVAL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200729 }
730
731 ctx->flags &= ~SHA_FLAGS_SG;
732
733 /* next call does not fail... so no unmap in the case of error */
Nicolas Royerd4905b32013-02-20 17:10:26 +0100734 return atmel_sha_xmit_start(dd, ctx->dma_addr, length, 0, 0, final);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200735}
736
737static int atmel_sha_update_dma_slow(struct atmel_sha_dev *dd)
738{
739 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
740 unsigned int final;
741 size_t count;
742
743 atmel_sha_append_sg(ctx);
744
745 final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
746
Arnd Bergmann4c147bc2017-02-06 13:32:16 +0100747 dev_dbg(dd->dev, "slow: bufcnt: %zu, digcnt: 0x%llx 0x%llx, final: %d\n",
Nicolas Royerd4905b32013-02-20 17:10:26 +0100748 ctx->bufcnt, ctx->digcnt[1], ctx->digcnt[0], final);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200749
750 if (final)
751 atmel_sha_fill_padding(ctx, 0);
752
Ludovic Desroches00992862015-04-07 17:45:04 +0800753 if (final || (ctx->bufcnt == ctx->buflen)) {
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200754 count = ctx->bufcnt;
755 ctx->bufcnt = 0;
756 return atmel_sha_xmit_dma_map(dd, ctx, count, final);
757 }
758
759 return 0;
760}
761
762static int atmel_sha_update_dma_start(struct atmel_sha_dev *dd)
763{
764 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
765 unsigned int length, final, tail;
766 struct scatterlist *sg;
767 unsigned int count;
768
769 if (!ctx->total)
770 return 0;
771
772 if (ctx->bufcnt || ctx->offset)
773 return atmel_sha_update_dma_slow(dd);
774
Arnd Bergmann4c147bc2017-02-06 13:32:16 +0100775 dev_dbg(dd->dev, "fast: digcnt: 0x%llx 0x%llx, bufcnt: %zd, total: %u\n",
Nicolas Royerd4905b32013-02-20 17:10:26 +0100776 ctx->digcnt[1], ctx->digcnt[0], ctx->bufcnt, ctx->total);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200777
778 sg = ctx->sg;
779
780 if (!IS_ALIGNED(sg->offset, sizeof(u32)))
781 return atmel_sha_update_dma_slow(dd);
782
Nicolas Royerd4905b32013-02-20 17:10:26 +0100783 if (!sg_is_last(sg) && !IS_ALIGNED(sg->length, ctx->block_size))
784 /* size is not ctx->block_size aligned */
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200785 return atmel_sha_update_dma_slow(dd);
786
787 length = min(ctx->total, sg->length);
788
789 if (sg_is_last(sg)) {
790 if (!(ctx->flags & SHA_FLAGS_FINUP)) {
Nicolas Royerd4905b32013-02-20 17:10:26 +0100791 /* not last sg must be ctx->block_size aligned */
792 tail = length & (ctx->block_size - 1);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200793 length -= tail;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200794 }
795 }
796
797 ctx->total -= length;
798 ctx->offset = length; /* offset where to start slow */
799
800 final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
801
802 /* Add padding */
803 if (final) {
Nicolas Royerd4905b32013-02-20 17:10:26 +0100804 tail = length & (ctx->block_size - 1);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200805 length -= tail;
806 ctx->total += tail;
807 ctx->offset = length; /* offset where to start slow */
808
809 sg = ctx->sg;
810 atmel_sha_append_sg(ctx);
811
812 atmel_sha_fill_padding(ctx, length);
813
814 ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
Nicolas Royerd4905b32013-02-20 17:10:26 +0100815 ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200816 if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
Arnd Bergmann4c147bc2017-02-06 13:32:16 +0100817 dev_err(dd->dev, "dma %zu bytes error\n",
Nicolas Royerd4905b32013-02-20 17:10:26 +0100818 ctx->buflen + ctx->block_size);
Cyrille Pitchendd3f9f42017-02-09 17:51:20 +0100819 return atmel_sha_complete(dd, -EINVAL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200820 }
821
822 if (length == 0) {
823 ctx->flags &= ~SHA_FLAGS_SG;
824 count = ctx->bufcnt;
825 ctx->bufcnt = 0;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100826 return atmel_sha_xmit_start(dd, ctx->dma_addr, count, 0,
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200827 0, final);
828 } else {
829 ctx->sg = sg;
830 if (!dma_map_sg(dd->dev, ctx->sg, 1,
831 DMA_TO_DEVICE)) {
832 dev_err(dd->dev, "dma_map_sg error\n");
Cyrille Pitchendd3f9f42017-02-09 17:51:20 +0100833 return atmel_sha_complete(dd, -EINVAL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200834 }
835
836 ctx->flags |= SHA_FLAGS_SG;
837
838 count = ctx->bufcnt;
839 ctx->bufcnt = 0;
Nicolas Royerd4905b32013-02-20 17:10:26 +0100840 return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg),
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200841 length, ctx->dma_addr, count, final);
842 }
843 }
844
845 if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
846 dev_err(dd->dev, "dma_map_sg error\n");
Cyrille Pitchendd3f9f42017-02-09 17:51:20 +0100847 return atmel_sha_complete(dd, -EINVAL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200848 }
849
850 ctx->flags |= SHA_FLAGS_SG;
851
852 /* next call does not fail... so no unmap in the case of error */
Nicolas Royerd4905b32013-02-20 17:10:26 +0100853 return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg), length, 0,
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200854 0, final);
855}
856
857static int atmel_sha_update_dma_stop(struct atmel_sha_dev *dd)
858{
859 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
860
861 if (ctx->flags & SHA_FLAGS_SG) {
862 dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE);
863 if (ctx->sg->length == ctx->offset) {
864 ctx->sg = sg_next(ctx->sg);
865 if (ctx->sg)
866 ctx->offset = 0;
867 }
Nicolas Royerd4905b32013-02-20 17:10:26 +0100868 if (ctx->flags & SHA_FLAGS_PAD) {
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200869 dma_unmap_single(dd->dev, ctx->dma_addr,
Nicolas Royerd4905b32013-02-20 17:10:26 +0100870 ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
871 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200872 } else {
873 dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen +
Nicolas Royerd4905b32013-02-20 17:10:26 +0100874 ctx->block_size, DMA_TO_DEVICE);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200875 }
876
877 return 0;
878}
879
880static int atmel_sha_update_req(struct atmel_sha_dev *dd)
881{
882 struct ahash_request *req = dd->req;
883 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
884 int err;
885
Nicolas Royerd4905b32013-02-20 17:10:26 +0100886 dev_dbg(dd->dev, "update_req: total: %u, digcnt: 0x%llx 0x%llx\n",
887 ctx->total, ctx->digcnt[1], ctx->digcnt[0]);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200888
889 if (ctx->flags & SHA_FLAGS_CPU)
890 err = atmel_sha_update_cpu(dd);
891 else
892 err = atmel_sha_update_dma_start(dd);
893
894 /* wait for dma completion before can take more data */
Nicolas Royerd4905b32013-02-20 17:10:26 +0100895 dev_dbg(dd->dev, "update: err: %d, digcnt: 0x%llx 0%llx\n",
896 err, ctx->digcnt[1], ctx->digcnt[0]);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200897
898 return err;
899}
900
901static int atmel_sha_final_req(struct atmel_sha_dev *dd)
902{
903 struct ahash_request *req = dd->req;
904 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
905 int err = 0;
906 int count;
907
908 if (ctx->bufcnt >= ATMEL_SHA_DMA_THRESHOLD) {
909 atmel_sha_fill_padding(ctx, 0);
910 count = ctx->bufcnt;
911 ctx->bufcnt = 0;
912 err = atmel_sha_xmit_dma_map(dd, ctx, count, 1);
913 }
914 /* faster to handle last block with cpu */
915 else {
916 atmel_sha_fill_padding(ctx, 0);
917 count = ctx->bufcnt;
918 ctx->bufcnt = 0;
919 err = atmel_sha_xmit_cpu(dd, ctx->buffer, count, 1);
920 }
921
922 dev_dbg(dd->dev, "final_req: err: %d\n", err);
923
924 return err;
925}
926
927static void atmel_sha_copy_hash(struct ahash_request *req)
928{
929 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
930 u32 *hash = (u32 *)ctx->digest;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100931 unsigned int i, hashsize;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200932
Cyrille Pitchen7cee3502016-01-15 15:49:34 +0100933 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
934 case SHA_FLAGS_SHA1:
935 hashsize = SHA1_DIGEST_SIZE;
936 break;
937
938 case SHA_FLAGS_SHA224:
939 case SHA_FLAGS_SHA256:
940 hashsize = SHA256_DIGEST_SIZE;
941 break;
942
943 case SHA_FLAGS_SHA384:
944 case SHA_FLAGS_SHA512:
945 hashsize = SHA512_DIGEST_SIZE;
946 break;
947
948 default:
949 /* Should not happen... */
950 return;
951 }
952
953 for (i = 0; i < hashsize / sizeof(u32); ++i)
954 hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i));
955 ctx->flags |= SHA_FLAGS_RESTORE;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200956}
957
958static void atmel_sha_copy_ready_hash(struct ahash_request *req)
959{
960 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
961
962 if (!req->result)
963 return;
964
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100965 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
966 default:
967 case SHA_FLAGS_SHA1:
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200968 memcpy(req->result, ctx->digest, SHA1_DIGEST_SIZE);
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100969 break;
970
971 case SHA_FLAGS_SHA224:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100972 memcpy(req->result, ctx->digest, SHA224_DIGEST_SIZE);
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100973 break;
974
975 case SHA_FLAGS_SHA256:
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200976 memcpy(req->result, ctx->digest, SHA256_DIGEST_SIZE);
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100977 break;
978
979 case SHA_FLAGS_SHA384:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100980 memcpy(req->result, ctx->digest, SHA384_DIGEST_SIZE);
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100981 break;
982
983 case SHA_FLAGS_SHA512:
Nicolas Royerd4905b32013-02-20 17:10:26 +0100984 memcpy(req->result, ctx->digest, SHA512_DIGEST_SIZE);
Cyrille Pitchenf07ceba2017-01-26 17:07:49 +0100985 break;
986 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200987}
988
989static int atmel_sha_finish(struct ahash_request *req)
990{
991 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
992 struct atmel_sha_dev *dd = ctx->dd;
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200993
Nicolas Royerd4905b32013-02-20 17:10:26 +0100994 if (ctx->digcnt[0] || ctx->digcnt[1])
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200995 atmel_sha_copy_ready_hash(req);
996
Arnd Bergmann4c147bc2017-02-06 13:32:16 +0100997 dev_dbg(dd->dev, "digcnt: 0x%llx 0x%llx, bufcnt: %zd\n", ctx->digcnt[1],
Nicolas Royerd4905b32013-02-20 17:10:26 +0100998 ctx->digcnt[0], ctx->bufcnt);
Nicolas Royerebc82ef2012-07-01 19:19:46 +0200999
Rahul Pathak871b88a2015-12-14 08:44:19 +00001000 return 0;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001001}
1002
1003static void atmel_sha_finish_req(struct ahash_request *req, int err)
1004{
1005 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1006 struct atmel_sha_dev *dd = ctx->dd;
1007
1008 if (!err) {
1009 atmel_sha_copy_hash(req);
1010 if (SHA_FLAGS_FINAL & dd->flags)
1011 err = atmel_sha_finish(req);
1012 } else {
1013 ctx->flags |= SHA_FLAGS_ERROR;
1014 }
1015
1016 /* atomic operation is not needed here */
Cyrille Pitchena29af932017-01-26 17:07:47 +01001017 (void)atmel_sha_complete(dd, err);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001018}
1019
1020static int atmel_sha_hw_init(struct atmel_sha_dev *dd)
1021{
LABBE Corentin9d83d292015-10-02 14:12:58 +02001022 int err;
1023
Cyrille Pitchenc0330422016-02-05 13:45:13 +01001024 err = clk_enable(dd->iclk);
LABBE Corentin9d83d292015-10-02 14:12:58 +02001025 if (err)
1026 return err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001027
Nicolas Royerd4905b32013-02-20 17:10:26 +01001028 if (!(SHA_FLAGS_INIT & dd->flags)) {
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001029 atmel_sha_write(dd, SHA_CR, SHA_CR_SWRST);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001030 dd->flags |= SHA_FLAGS_INIT;
1031 dd->err = 0;
1032 }
1033
1034 return 0;
1035}
1036
Nicolas Royerd4905b32013-02-20 17:10:26 +01001037static inline unsigned int atmel_sha_get_version(struct atmel_sha_dev *dd)
1038{
1039 return atmel_sha_read(dd, SHA_HW_VERSION) & 0x00000fff;
1040}
1041
1042static void atmel_sha_hw_version_init(struct atmel_sha_dev *dd)
1043{
1044 atmel_sha_hw_init(dd);
1045
1046 dd->hw_version = atmel_sha_get_version(dd);
1047
1048 dev_info(dd->dev,
1049 "version: 0x%x\n", dd->hw_version);
1050
Cyrille Pitchenc0330422016-02-05 13:45:13 +01001051 clk_disable(dd->iclk);
Nicolas Royerd4905b32013-02-20 17:10:26 +01001052}
1053
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001054static int atmel_sha_handle_queue(struct atmel_sha_dev *dd,
1055 struct ahash_request *req)
1056{
1057 struct crypto_async_request *async_req, *backlog;
Cyrille Pitchena29af932017-01-26 17:07:47 +01001058 struct atmel_sha_ctx *ctx;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001059 unsigned long flags;
Cyrille Pitchena29af932017-01-26 17:07:47 +01001060 bool start_async;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001061 int err = 0, ret = 0;
1062
1063 spin_lock_irqsave(&dd->lock, flags);
1064 if (req)
1065 ret = ahash_enqueue_request(&dd->queue, req);
1066
1067 if (SHA_FLAGS_BUSY & dd->flags) {
1068 spin_unlock_irqrestore(&dd->lock, flags);
1069 return ret;
1070 }
1071
1072 backlog = crypto_get_backlog(&dd->queue);
1073 async_req = crypto_dequeue_request(&dd->queue);
1074 if (async_req)
1075 dd->flags |= SHA_FLAGS_BUSY;
1076
1077 spin_unlock_irqrestore(&dd->lock, flags);
1078
1079 if (!async_req)
1080 return ret;
1081
1082 if (backlog)
1083 backlog->complete(backlog, -EINPROGRESS);
1084
Cyrille Pitchena29af932017-01-26 17:07:47 +01001085 ctx = crypto_tfm_ctx(async_req->tfm);
1086
1087 dd->req = ahash_request_cast(async_req);
1088 start_async = (dd->req != req);
1089 dd->is_async = start_async;
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01001090 dd->force_complete = false;
Cyrille Pitchena29af932017-01-26 17:07:47 +01001091
1092 /* WARNING: ctx->start() MAY change dd->is_async. */
1093 err = ctx->start(dd);
1094 return (start_async) ? ret : err;
1095}
1096
Cyrille Pitchenb5ce82a2017-01-26 17:07:48 +01001097static int atmel_sha_done(struct atmel_sha_dev *dd);
1098
Cyrille Pitchena29af932017-01-26 17:07:47 +01001099static int atmel_sha_start(struct atmel_sha_dev *dd)
1100{
1101 struct ahash_request *req = dd->req;
1102 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1103 int err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001104
1105 dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n",
1106 ctx->op, req->nbytes);
1107
1108 err = atmel_sha_hw_init(dd);
1109
1110 if (err)
1111 goto err1;
1112
Cyrille Pitchenb5ce82a2017-01-26 17:07:48 +01001113 dd->resume = atmel_sha_done;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001114 if (ctx->op == SHA_OP_UPDATE) {
1115 err = atmel_sha_update_req(dd);
Nicolas Royerd4905b32013-02-20 17:10:26 +01001116 if (err != -EINPROGRESS && (ctx->flags & SHA_FLAGS_FINUP))
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001117 /* no final() after finup() */
1118 err = atmel_sha_final_req(dd);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001119 } else if (ctx->op == SHA_OP_FINAL) {
1120 err = atmel_sha_final_req(dd);
1121 }
1122
1123err1:
1124 if (err != -EINPROGRESS)
1125 /* done_task will not finish it, so do it here */
1126 atmel_sha_finish_req(req, err);
1127
1128 dev_dbg(dd->dev, "exit, err: %d\n", err);
1129
Cyrille Pitchena29af932017-01-26 17:07:47 +01001130 return err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001131}
1132
1133static int atmel_sha_enqueue(struct ahash_request *req, unsigned int op)
1134{
1135 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1136 struct atmel_sha_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
1137 struct atmel_sha_dev *dd = tctx->dd;
1138
1139 ctx->op = op;
1140
1141 return atmel_sha_handle_queue(dd, req);
1142}
1143
1144static int atmel_sha_update(struct ahash_request *req)
1145{
1146 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1147
1148 if (!req->nbytes)
1149 return 0;
1150
1151 ctx->total = req->nbytes;
1152 ctx->sg = req->src;
1153 ctx->offset = 0;
1154
1155 if (ctx->flags & SHA_FLAGS_FINUP) {
1156 if (ctx->bufcnt + ctx->total < ATMEL_SHA_DMA_THRESHOLD)
1157 /* faster to use CPU for short transfers */
1158 ctx->flags |= SHA_FLAGS_CPU;
1159 } else if (ctx->bufcnt + ctx->total < ctx->buflen) {
1160 atmel_sha_append_sg(ctx);
1161 return 0;
1162 }
1163 return atmel_sha_enqueue(req, SHA_OP_UPDATE);
1164}
1165
1166static int atmel_sha_final(struct ahash_request *req)
1167{
1168 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001169
1170 ctx->flags |= SHA_FLAGS_FINUP;
1171
1172 if (ctx->flags & SHA_FLAGS_ERROR)
1173 return 0; /* uncompleted hash is not needed */
1174
Cyrille Pitchenad841122016-02-08 16:26:49 +01001175 if (ctx->flags & SHA_FLAGS_PAD)
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001176 /* copy ready hash (+ finalize hmac) */
1177 return atmel_sha_finish(req);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001178
Cyrille Pitchenad841122016-02-08 16:26:49 +01001179 return atmel_sha_enqueue(req, SHA_OP_FINAL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001180}
1181
1182static int atmel_sha_finup(struct ahash_request *req)
1183{
1184 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1185 int err1, err2;
1186
1187 ctx->flags |= SHA_FLAGS_FINUP;
1188
1189 err1 = atmel_sha_update(req);
1190 if (err1 == -EINPROGRESS || err1 == -EBUSY)
1191 return err1;
1192
1193 /*
1194 * final() has to be always called to cleanup resources
1195 * even if udpate() failed, except EINPROGRESS
1196 */
1197 err2 = atmel_sha_final(req);
1198
1199 return err1 ?: err2;
1200}
1201
1202static int atmel_sha_digest(struct ahash_request *req)
1203{
1204 return atmel_sha_init(req) ?: atmel_sha_finup(req);
1205}
1206
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001207
1208static int atmel_sha_export(struct ahash_request *req, void *out)
1209{
1210 const struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001211
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001212 memcpy(out, ctx, sizeof(*ctx));
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001213 return 0;
1214}
1215
1216static int atmel_sha_import(struct ahash_request *req, const void *in)
1217{
1218 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001219
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001220 memcpy(ctx, in, sizeof(*ctx));
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001221 return 0;
1222}
1223
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001224static int atmel_sha_cra_init(struct crypto_tfm *tfm)
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001225{
Cyrille Pitchena29af932017-01-26 17:07:47 +01001226 struct atmel_sha_ctx *ctx = crypto_tfm_ctx(tfm);
1227
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001228 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001229 sizeof(struct atmel_sha_reqctx));
Cyrille Pitchena29af932017-01-26 17:07:47 +01001230 ctx->start = atmel_sha_start;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001231
1232 return 0;
1233}
1234
Nicolas Royerd4905b32013-02-20 17:10:26 +01001235static struct ahash_alg sha_1_256_algs[] = {
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001236{
1237 .init = atmel_sha_init,
1238 .update = atmel_sha_update,
1239 .final = atmel_sha_final,
1240 .finup = atmel_sha_finup,
1241 .digest = atmel_sha_digest,
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001242 .export = atmel_sha_export,
1243 .import = atmel_sha_import,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001244 .halg = {
1245 .digestsize = SHA1_DIGEST_SIZE,
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001246 .statesize = sizeof(struct atmel_sha_reqctx),
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001247 .base = {
1248 .cra_name = "sha1",
1249 .cra_driver_name = "atmel-sha1",
1250 .cra_priority = 100,
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001251 .cra_flags = CRYPTO_ALG_ASYNC,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001252 .cra_blocksize = SHA1_BLOCK_SIZE,
1253 .cra_ctxsize = sizeof(struct atmel_sha_ctx),
1254 .cra_alignmask = 0,
1255 .cra_module = THIS_MODULE,
1256 .cra_init = atmel_sha_cra_init,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001257 }
1258 }
1259},
1260{
1261 .init = atmel_sha_init,
1262 .update = atmel_sha_update,
1263 .final = atmel_sha_final,
1264 .finup = atmel_sha_finup,
1265 .digest = atmel_sha_digest,
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001266 .export = atmel_sha_export,
1267 .import = atmel_sha_import,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001268 .halg = {
1269 .digestsize = SHA256_DIGEST_SIZE,
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001270 .statesize = sizeof(struct atmel_sha_reqctx),
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001271 .base = {
1272 .cra_name = "sha256",
1273 .cra_driver_name = "atmel-sha256",
1274 .cra_priority = 100,
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001275 .cra_flags = CRYPTO_ALG_ASYNC,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001276 .cra_blocksize = SHA256_BLOCK_SIZE,
1277 .cra_ctxsize = sizeof(struct atmel_sha_ctx),
1278 .cra_alignmask = 0,
1279 .cra_module = THIS_MODULE,
1280 .cra_init = atmel_sha_cra_init,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001281 }
1282 }
1283},
1284};
1285
Nicolas Royerd4905b32013-02-20 17:10:26 +01001286static struct ahash_alg sha_224_alg = {
1287 .init = atmel_sha_init,
1288 .update = atmel_sha_update,
1289 .final = atmel_sha_final,
1290 .finup = atmel_sha_finup,
1291 .digest = atmel_sha_digest,
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001292 .export = atmel_sha_export,
1293 .import = atmel_sha_import,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001294 .halg = {
1295 .digestsize = SHA224_DIGEST_SIZE,
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001296 .statesize = sizeof(struct atmel_sha_reqctx),
Nicolas Royerd4905b32013-02-20 17:10:26 +01001297 .base = {
1298 .cra_name = "sha224",
1299 .cra_driver_name = "atmel-sha224",
1300 .cra_priority = 100,
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001301 .cra_flags = CRYPTO_ALG_ASYNC,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001302 .cra_blocksize = SHA224_BLOCK_SIZE,
1303 .cra_ctxsize = sizeof(struct atmel_sha_ctx),
1304 .cra_alignmask = 0,
1305 .cra_module = THIS_MODULE,
1306 .cra_init = atmel_sha_cra_init,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001307 }
1308 }
1309};
1310
1311static struct ahash_alg sha_384_512_algs[] = {
1312{
1313 .init = atmel_sha_init,
1314 .update = atmel_sha_update,
1315 .final = atmel_sha_final,
1316 .finup = atmel_sha_finup,
1317 .digest = atmel_sha_digest,
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001318 .export = atmel_sha_export,
1319 .import = atmel_sha_import,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001320 .halg = {
1321 .digestsize = SHA384_DIGEST_SIZE,
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001322 .statesize = sizeof(struct atmel_sha_reqctx),
Nicolas Royerd4905b32013-02-20 17:10:26 +01001323 .base = {
1324 .cra_name = "sha384",
1325 .cra_driver_name = "atmel-sha384",
1326 .cra_priority = 100,
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001327 .cra_flags = CRYPTO_ALG_ASYNC,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001328 .cra_blocksize = SHA384_BLOCK_SIZE,
1329 .cra_ctxsize = sizeof(struct atmel_sha_ctx),
1330 .cra_alignmask = 0x3,
1331 .cra_module = THIS_MODULE,
1332 .cra_init = atmel_sha_cra_init,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001333 }
1334 }
1335},
1336{
1337 .init = atmel_sha_init,
1338 .update = atmel_sha_update,
1339 .final = atmel_sha_final,
1340 .finup = atmel_sha_finup,
1341 .digest = atmel_sha_digest,
Cyrille Pitchencc831d32016-01-29 17:04:02 +01001342 .export = atmel_sha_export,
1343 .import = atmel_sha_import,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001344 .halg = {
1345 .digestsize = SHA512_DIGEST_SIZE,
Cyrille Pitchen9c4274d2016-02-08 16:26:48 +01001346 .statesize = sizeof(struct atmel_sha_reqctx),
Nicolas Royerd4905b32013-02-20 17:10:26 +01001347 .base = {
1348 .cra_name = "sha512",
1349 .cra_driver_name = "atmel-sha512",
1350 .cra_priority = 100,
Svenning Sørensenbe95f0f2014-12-05 01:18:57 +01001351 .cra_flags = CRYPTO_ALG_ASYNC,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001352 .cra_blocksize = SHA512_BLOCK_SIZE,
1353 .cra_ctxsize = sizeof(struct atmel_sha_ctx),
1354 .cra_alignmask = 0x3,
1355 .cra_module = THIS_MODULE,
1356 .cra_init = atmel_sha_cra_init,
Nicolas Royerd4905b32013-02-20 17:10:26 +01001357 }
1358 }
1359},
1360};
1361
Cyrille Pitchenf56809c2016-01-15 15:49:32 +01001362static void atmel_sha_queue_task(unsigned long data)
1363{
1364 struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
1365
1366 atmel_sha_handle_queue(dd, NULL);
1367}
1368
Cyrille Pitchenb5ce82a2017-01-26 17:07:48 +01001369static int atmel_sha_done(struct atmel_sha_dev *dd)
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001370{
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001371 int err = 0;
1372
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001373 if (SHA_FLAGS_CPU & dd->flags) {
1374 if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
1375 dd->flags &= ~SHA_FLAGS_OUTPUT_READY;
1376 goto finish;
1377 }
1378 } else if (SHA_FLAGS_DMA_READY & dd->flags) {
1379 if (SHA_FLAGS_DMA_ACTIVE & dd->flags) {
1380 dd->flags &= ~SHA_FLAGS_DMA_ACTIVE;
1381 atmel_sha_update_dma_stop(dd);
1382 if (dd->err) {
1383 err = dd->err;
1384 goto finish;
1385 }
1386 }
1387 if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
1388 /* hash or semi-hash ready */
1389 dd->flags &= ~(SHA_FLAGS_DMA_READY |
1390 SHA_FLAGS_OUTPUT_READY);
1391 err = atmel_sha_update_dma_start(dd);
1392 if (err != -EINPROGRESS)
1393 goto finish;
1394 }
1395 }
Cyrille Pitchenb5ce82a2017-01-26 17:07:48 +01001396 return err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001397
1398finish:
1399 /* finish curent request */
1400 atmel_sha_finish_req(dd->req, err);
Cyrille Pitchenb5ce82a2017-01-26 17:07:48 +01001401
1402 return err;
1403}
1404
1405static void atmel_sha_done_task(unsigned long data)
1406{
1407 struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
1408
1409 dd->is_async = true;
1410 (void)dd->resume(dd);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02001411}
1412
1413static irqreturn_t atmel_sha_irq(int irq, void *dev_id)
1414{
1415 struct atmel_sha_dev *sha_dd = dev_id;
1416 u32 reg;
1417
1418 reg = atmel_sha_read(sha_dd, SHA_ISR);
1419 if (reg & atmel_sha_read(sha_dd, SHA_IMR)) {
1420 atmel_sha_write(sha_dd, SHA_IDR, reg);
1421 if (SHA_FLAGS_BUSY & sha_dd->flags) {
1422 sha_dd->flags |= SHA_FLAGS_OUTPUT_READY;
1423 if (!(SHA_FLAGS_CPU & sha_dd->flags))
1424 sha_dd->flags |= SHA_FLAGS_DMA_READY;
1425 tasklet_schedule(&sha_dd->done_task);
1426 } else {
1427 dev_warn(sha_dd->dev, "SHA interrupt when no active requests.\n");
1428 }
1429 return IRQ_HANDLED;
1430 }
1431
1432 return IRQ_NONE;
1433}
1434
Cyrille Pitcheneec12f62017-01-26 17:07:52 +01001435
Cyrille Pitchen69303cf2017-01-26 17:07:53 +01001436/* DMA transfer functions */
1437
1438static bool atmel_sha_dma_check_aligned(struct atmel_sha_dev *dd,
1439 struct scatterlist *sg,
1440 size_t len)
1441{
1442 struct atmel_sha_dma *dma = &dd->dma_lch_in;
1443 struct ahash_request *req = dd->req;
1444 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1445 size_t bs = ctx->block_size;
1446 int nents;
1447
1448 for (nents = 0; sg; sg = sg_next(sg), ++nents) {
1449 if (!IS_ALIGNED(sg->offset, sizeof(u32)))
1450 return false;
1451
1452 /*
1453 * This is the last sg, the only one that is allowed to
1454 * have an unaligned length.
1455 */
1456 if (len <= sg->length) {
1457 dma->nents = nents + 1;
1458 dma->last_sg_length = sg->length;
1459 sg->length = ALIGN(len, sizeof(u32));
1460 return true;
1461 }
1462
1463 /* All other sg lengths MUST be aligned to the block size. */
1464 if (!IS_ALIGNED(sg->length, bs))
1465 return false;
1466
1467 len -= sg->length;
1468 }
1469
1470 return false;
1471}
1472
1473static void atmel_sha_dma_callback2(void *data)
1474{
1475 struct atmel_sha_dev *dd = data;
1476 struct atmel_sha_dma *dma = &dd->dma_lch_in;
1477 struct scatterlist *sg;
1478 int nents;
1479
1480 dmaengine_terminate_all(dma->chan);
1481 dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
1482
1483 sg = dma->sg;
1484 for (nents = 0; nents < dma->nents - 1; ++nents)
1485 sg = sg_next(sg);
1486 sg->length = dma->last_sg_length;
1487
1488 dd->is_async = true;
1489 (void)atmel_sha_wait_for_data_ready(dd, dd->resume);
1490}
1491
1492static int atmel_sha_dma_start(struct atmel_sha_dev *dd,
1493 struct scatterlist *src,
1494 size_t len,
1495 atmel_sha_fn_t resume)
1496{
1497 struct atmel_sha_dma *dma = &dd->dma_lch_in;
1498 struct dma_slave_config *config = &dma->dma_conf;
1499 struct dma_chan *chan = dma->chan;
1500 struct dma_async_tx_descriptor *desc;
1501 dma_cookie_t cookie;
1502 unsigned int sg_len;
1503 int err;
1504
1505 dd->resume = resume;
1506
1507 /*
1508 * dma->nents has already been initialized by
1509 * atmel_sha_dma_check_aligned().
1510 */
1511 dma->sg = src;
1512 sg_len = dma_map_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
1513 if (!sg_len) {
1514 err = -ENOMEM;
1515 goto exit;
1516 }
1517
1518 config->src_maxburst = 16;
1519 config->dst_maxburst = 16;
1520 err = dmaengine_slave_config(chan, config);
1521 if (err)
1522 goto unmap_sg;
1523
1524 desc = dmaengine_prep_slave_sg(chan, dma->sg, sg_len, DMA_MEM_TO_DEV,
1525 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1526 if (!desc) {
1527 err = -ENOMEM;
1528 goto unmap_sg;
1529 }
1530
1531 desc->callback = atmel_sha_dma_callback2;
1532 desc->callback_param = dd;
1533 cookie = dmaengine_submit(desc);
1534 err = dma_submit_error(cookie);
1535 if (err)
1536 goto unmap_sg;
1537
1538 dma_async_issue_pending(chan);
1539
1540 return -EINPROGRESS;
1541
1542unmap_sg:
1543 dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
1544exit:
1545 return atmel_sha_complete(dd, err);
1546}
1547
1548
Cyrille Pitcheneec12f62017-01-26 17:07:52 +01001549/* CPU transfer functions */
1550
1551static int atmel_sha_cpu_transfer(struct atmel_sha_dev *dd)
1552{
1553 struct ahash_request *req = dd->req;
1554 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1555 const u32 *words = (const u32 *)ctx->buffer;
1556 size_t i, num_words;
1557 u32 isr, din, din_inc;
1558
1559 din_inc = (ctx->flags & SHA_FLAGS_IDATAR0) ? 0 : 1;
1560 for (;;) {
1561 /* Write data into the Input Data Registers. */
1562 num_words = DIV_ROUND_UP(ctx->bufcnt, sizeof(u32));
1563 for (i = 0, din = 0; i < num_words; ++i, din += din_inc)
1564 atmel_sha_write(dd, SHA_REG_DIN(din), words[i]);
1565
1566 ctx->offset += ctx->bufcnt;
1567 ctx->total -= ctx->bufcnt;
1568
1569 if (!ctx->total)
1570 break;
1571
1572 /*
1573 * Prepare next block:
1574 * Fill ctx->buffer now with the next data to be written into
1575 * IDATARx: it gives time for the SHA hardware to process
1576 * the current data so the SHA_INT_DATARDY flag might be set
1577 * in SHA_ISR when polling this register at the beginning of
1578 * the next loop.
1579 */
1580 ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total);
1581 scatterwalk_map_and_copy(ctx->buffer, ctx->sg,
1582 ctx->offset, ctx->bufcnt, 0);
1583
1584 /* Wait for hardware to be ready again. */
1585 isr = atmel_sha_read(dd, SHA_ISR);
1586 if (!(isr & SHA_INT_DATARDY)) {
1587 /* Not ready yet. */
1588 dd->resume = atmel_sha_cpu_transfer;
1589 atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
1590 return -EINPROGRESS;
1591 }
1592 }
1593
1594 if (unlikely(!(ctx->flags & SHA_FLAGS_WAIT_DATARDY)))
1595 return dd->cpu_transfer_complete(dd);
1596
1597 return atmel_sha_wait_for_data_ready(dd, dd->cpu_transfer_complete);
1598}
1599
1600static int atmel_sha_cpu_start(struct atmel_sha_dev *dd,
1601 struct scatterlist *sg,
1602 unsigned int len,
1603 bool idatar0_only,
1604 bool wait_data_ready,
1605 atmel_sha_fn_t resume)
1606{
1607 struct ahash_request *req = dd->req;
1608 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1609
1610 if (!len)
1611 return resume(dd);
1612
1613 ctx->flags &= ~(SHA_FLAGS_IDATAR0 | SHA_FLAGS_WAIT_DATARDY);
1614
1615 if (idatar0_only)
1616 ctx->flags |= SHA_FLAGS_IDATAR0;
1617
1618 if (wait_data_ready)
1619 ctx->flags |= SHA_FLAGS_WAIT_DATARDY;
1620
1621 ctx->sg = sg;
1622 ctx->total = len;
1623 ctx->offset = 0;
1624
1625 /* Prepare the first block to be written. */
1626 ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total);
1627 scatterwalk_map_and_copy(ctx->buffer, ctx->sg,
1628 ctx->offset, ctx->bufcnt, 0);
1629
1630 dd->cpu_transfer_complete = resume;
1631 return atmel_sha_cpu_transfer(dd);
1632}
1633
Cyrille Pitchen81d87502017-01-26 17:07:54 +01001634static int atmel_sha_cpu_hash(struct atmel_sha_dev *dd,
1635 const void *data, unsigned int datalen,
1636 bool auto_padding,
1637 atmel_sha_fn_t resume)
1638{
1639 struct ahash_request *req = dd->req;
1640 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1641 u32 msglen = (auto_padding) ? datalen : 0;
1642 u32 mr = SHA_MR_MODE_AUTO;
1643
1644 if (!(IS_ALIGNED(datalen, ctx->block_size) || auto_padding))
1645 return atmel_sha_complete(dd, -EINVAL);
1646
1647 mr |= (ctx->flags & SHA_FLAGS_ALGO_MASK);
1648 atmel_sha_write(dd, SHA_MR, mr);
1649 atmel_sha_write(dd, SHA_MSR, msglen);
1650 atmel_sha_write(dd, SHA_BCR, msglen);
1651 atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
1652
1653 sg_init_one(&dd->tmp, data, datalen);
1654 return atmel_sha_cpu_start(dd, &dd->tmp, datalen, false, true, resume);
1655}
1656
1657
1658/* hmac functions */
1659
1660struct atmel_sha_hmac_key {
1661 bool valid;
1662 unsigned int keylen;
1663 u8 buffer[SHA512_BLOCK_SIZE];
1664 u8 *keydup;
1665};
1666
1667static inline void atmel_sha_hmac_key_init(struct atmel_sha_hmac_key *hkey)
1668{
1669 memset(hkey, 0, sizeof(*hkey));
1670}
1671
1672static inline void atmel_sha_hmac_key_release(struct atmel_sha_hmac_key *hkey)
1673{
1674 kfree(hkey->keydup);
1675 memset(hkey, 0, sizeof(*hkey));
1676}
1677
1678static inline int atmel_sha_hmac_key_set(struct atmel_sha_hmac_key *hkey,
1679 const u8 *key,
1680 unsigned int keylen)
1681{
1682 atmel_sha_hmac_key_release(hkey);
1683
1684 if (keylen > sizeof(hkey->buffer)) {
1685 hkey->keydup = kmemdup(key, keylen, GFP_KERNEL);
1686 if (!hkey->keydup)
1687 return -ENOMEM;
1688
1689 } else {
1690 memcpy(hkey->buffer, key, keylen);
1691 }
1692
1693 hkey->valid = true;
1694 hkey->keylen = keylen;
1695 return 0;
1696}
1697
1698static inline bool atmel_sha_hmac_key_get(const struct atmel_sha_hmac_key *hkey,
1699 const u8 **key,
1700 unsigned int *keylen)
1701{
1702 if (!hkey->valid)
1703 return false;
1704
1705 *keylen = hkey->keylen;
1706 *key = (hkey->keydup) ? hkey->keydup : hkey->buffer;
1707 return true;
1708}
1709
1710
1711struct atmel_sha_hmac_ctx {
1712 struct atmel_sha_ctx base;
1713
1714 struct atmel_sha_hmac_key hkey;
1715 u32 ipad[SHA512_BLOCK_SIZE / sizeof(u32)];
1716 u32 opad[SHA512_BLOCK_SIZE / sizeof(u32)];
1717 atmel_sha_fn_t resume;
1718};
1719
1720static int atmel_sha_hmac_setup(struct atmel_sha_dev *dd,
1721 atmel_sha_fn_t resume);
1722static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev *dd,
1723 const u8 *key, unsigned int keylen);
1724static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev *dd);
1725static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev *dd);
1726static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev *dd);
1727static int atmel_sha_hmac_setup_done(struct atmel_sha_dev *dd);
1728
1729static int atmel_sha_hmac_init_done(struct atmel_sha_dev *dd);
1730static int atmel_sha_hmac_final(struct atmel_sha_dev *dd);
1731static int atmel_sha_hmac_final_done(struct atmel_sha_dev *dd);
1732static int atmel_sha_hmac_digest2(struct atmel_sha_dev *dd);
1733
1734static int atmel_sha_hmac_setup(struct atmel_sha_dev *dd,
1735 atmel_sha_fn_t resume)
1736{
1737 struct ahash_request *req = dd->req;
1738 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1739 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1740 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
1741 unsigned int keylen;
1742 const u8 *key;
1743 size_t bs;
1744
1745 hmac->resume = resume;
1746 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
1747 case SHA_FLAGS_SHA1:
1748 ctx->block_size = SHA1_BLOCK_SIZE;
1749 ctx->hash_size = SHA1_DIGEST_SIZE;
1750 break;
1751
1752 case SHA_FLAGS_SHA224:
1753 ctx->block_size = SHA224_BLOCK_SIZE;
1754 ctx->hash_size = SHA256_DIGEST_SIZE;
1755 break;
1756
1757 case SHA_FLAGS_SHA256:
1758 ctx->block_size = SHA256_BLOCK_SIZE;
1759 ctx->hash_size = SHA256_DIGEST_SIZE;
1760 break;
1761
1762 case SHA_FLAGS_SHA384:
1763 ctx->block_size = SHA384_BLOCK_SIZE;
1764 ctx->hash_size = SHA512_DIGEST_SIZE;
1765 break;
1766
1767 case SHA_FLAGS_SHA512:
1768 ctx->block_size = SHA512_BLOCK_SIZE;
1769 ctx->hash_size = SHA512_DIGEST_SIZE;
1770 break;
1771
1772 default:
1773 return atmel_sha_complete(dd, -EINVAL);
1774 }
1775 bs = ctx->block_size;
1776
1777 if (likely(!atmel_sha_hmac_key_get(&hmac->hkey, &key, &keylen)))
1778 return resume(dd);
1779
1780 /* Compute K' from K. */
1781 if (unlikely(keylen > bs))
1782 return atmel_sha_hmac_prehash_key(dd, key, keylen);
1783
1784 /* Prepare ipad. */
1785 memcpy((u8 *)hmac->ipad, key, keylen);
1786 memset((u8 *)hmac->ipad + keylen, 0, bs - keylen);
1787 return atmel_sha_hmac_compute_ipad_hash(dd);
1788}
1789
1790static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev *dd,
1791 const u8 *key, unsigned int keylen)
1792{
1793 return atmel_sha_cpu_hash(dd, key, keylen, true,
1794 atmel_sha_hmac_prehash_key_done);
1795}
1796
1797static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev *dd)
1798{
1799 struct ahash_request *req = dd->req;
1800 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1801 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
1802 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1803 size_t ds = crypto_ahash_digestsize(tfm);
1804 size_t bs = ctx->block_size;
1805 size_t i, num_words = ds / sizeof(u32);
1806
1807 /* Prepare ipad. */
1808 for (i = 0; i < num_words; ++i)
1809 hmac->ipad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
1810 memset((u8 *)hmac->ipad + ds, 0, bs - ds);
1811 return atmel_sha_hmac_compute_ipad_hash(dd);
1812}
1813
1814static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev *dd)
1815{
1816 struct ahash_request *req = dd->req;
1817 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1818 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
1819 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1820 size_t bs = ctx->block_size;
1821 size_t i, num_words = bs / sizeof(u32);
1822
1823 memcpy(hmac->opad, hmac->ipad, bs);
1824 for (i = 0; i < num_words; ++i) {
1825 hmac->ipad[i] ^= 0x36363636;
1826 hmac->opad[i] ^= 0x5c5c5c5c;
1827 }
1828
1829 return atmel_sha_cpu_hash(dd, hmac->ipad, bs, false,
1830 atmel_sha_hmac_compute_opad_hash);
1831}
1832
1833static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev *dd)
1834{
1835 struct ahash_request *req = dd->req;
1836 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1837 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
1838 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1839 size_t bs = ctx->block_size;
1840 size_t hs = ctx->hash_size;
1841 size_t i, num_words = hs / sizeof(u32);
1842
1843 for (i = 0; i < num_words; ++i)
1844 hmac->ipad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
1845 return atmel_sha_cpu_hash(dd, hmac->opad, bs, false,
1846 atmel_sha_hmac_setup_done);
1847}
1848
1849static int atmel_sha_hmac_setup_done(struct atmel_sha_dev *dd)
1850{
1851 struct ahash_request *req = dd->req;
1852 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1853 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
1854 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1855 size_t hs = ctx->hash_size;
1856 size_t i, num_words = hs / sizeof(u32);
1857
1858 for (i = 0; i < num_words; ++i)
1859 hmac->opad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
1860 atmel_sha_hmac_key_release(&hmac->hkey);
1861 return hmac->resume(dd);
1862}
1863
1864static int atmel_sha_hmac_start(struct atmel_sha_dev *dd)
1865{
1866 struct ahash_request *req = dd->req;
1867 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1868 int err;
1869
1870 err = atmel_sha_hw_init(dd);
1871 if (err)
1872 return atmel_sha_complete(dd, err);
1873
1874 switch (ctx->op) {
1875 case SHA_OP_INIT:
1876 err = atmel_sha_hmac_setup(dd, atmel_sha_hmac_init_done);
1877 break;
1878
1879 case SHA_OP_UPDATE:
1880 dd->resume = atmel_sha_done;
1881 err = atmel_sha_update_req(dd);
1882 break;
1883
1884 case SHA_OP_FINAL:
1885 dd->resume = atmel_sha_hmac_final;
1886 err = atmel_sha_final_req(dd);
1887 break;
1888
1889 case SHA_OP_DIGEST:
1890 err = atmel_sha_hmac_setup(dd, atmel_sha_hmac_digest2);
1891 break;
1892
1893 default:
1894 return atmel_sha_complete(dd, -EINVAL);
1895 }
1896
1897 return err;
1898}
1899
1900static int atmel_sha_hmac_setkey(struct crypto_ahash *tfm, const u8 *key,
1901 unsigned int keylen)
1902{
1903 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
1904
1905 if (atmel_sha_hmac_key_set(&hmac->hkey, key, keylen)) {
1906 crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
1907 return -EINVAL;
1908 }
1909
1910 return 0;
1911}
1912
1913static int atmel_sha_hmac_init(struct ahash_request *req)
1914{
1915 int err;
1916
1917 err = atmel_sha_init(req);
1918 if (err)
1919 return err;
1920
1921 return atmel_sha_enqueue(req, SHA_OP_INIT);
1922}
1923
1924static int atmel_sha_hmac_init_done(struct atmel_sha_dev *dd)
1925{
1926 struct ahash_request *req = dd->req;
1927 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1928 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1929 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
1930 size_t bs = ctx->block_size;
1931 size_t hs = ctx->hash_size;
1932
1933 ctx->bufcnt = 0;
1934 ctx->digcnt[0] = bs;
1935 ctx->digcnt[1] = 0;
1936 ctx->flags |= SHA_FLAGS_RESTORE;
1937 memcpy(ctx->digest, hmac->ipad, hs);
1938 return atmel_sha_complete(dd, 0);
1939}
1940
1941static int atmel_sha_hmac_final(struct atmel_sha_dev *dd)
1942{
1943 struct ahash_request *req = dd->req;
1944 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1945 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1946 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
1947 u32 *digest = (u32 *)ctx->digest;
1948 size_t ds = crypto_ahash_digestsize(tfm);
1949 size_t bs = ctx->block_size;
1950 size_t hs = ctx->hash_size;
1951 size_t i, num_words;
1952 u32 mr;
1953
1954 /* Save d = SHA((K' + ipad) | msg). */
1955 num_words = ds / sizeof(u32);
1956 for (i = 0; i < num_words; ++i)
1957 digest[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
1958
1959 /* Restore context to finish computing SHA((K' + opad) | d). */
1960 atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
1961 num_words = hs / sizeof(u32);
1962 for (i = 0; i < num_words; ++i)
1963 atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
1964
1965 mr = SHA_MR_MODE_AUTO | SHA_MR_UIHV;
1966 mr |= (ctx->flags & SHA_FLAGS_ALGO_MASK);
1967 atmel_sha_write(dd, SHA_MR, mr);
1968 atmel_sha_write(dd, SHA_MSR, bs + ds);
1969 atmel_sha_write(dd, SHA_BCR, ds);
1970 atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
1971
1972 sg_init_one(&dd->tmp, digest, ds);
1973 return atmel_sha_cpu_start(dd, &dd->tmp, ds, false, true,
1974 atmel_sha_hmac_final_done);
1975}
1976
1977static int atmel_sha_hmac_final_done(struct atmel_sha_dev *dd)
1978{
1979 /*
1980 * req->result might not be sizeof(u32) aligned, so copy the
1981 * digest into ctx->digest[] before memcpy() the data into
1982 * req->result.
1983 */
1984 atmel_sha_copy_hash(dd->req);
1985 atmel_sha_copy_ready_hash(dd->req);
1986 return atmel_sha_complete(dd, 0);
1987}
1988
1989static int atmel_sha_hmac_digest(struct ahash_request *req)
1990{
1991 int err;
1992
1993 err = atmel_sha_init(req);
1994 if (err)
1995 return err;
1996
1997 return atmel_sha_enqueue(req, SHA_OP_DIGEST);
1998}
1999
2000static int atmel_sha_hmac_digest2(struct atmel_sha_dev *dd)
2001{
2002 struct ahash_request *req = dd->req;
2003 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
2004 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2005 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
2006 size_t hs = ctx->hash_size;
2007 size_t i, num_words = hs / sizeof(u32);
2008 bool use_dma = false;
2009 u32 mr;
2010
2011 /* Special case for empty message. */
2012 if (!req->nbytes)
2013 return atmel_sha_complete(dd, -EINVAL); // TODO:
2014
2015 /* Check DMA threshold and alignment. */
2016 if (req->nbytes > ATMEL_SHA_DMA_THRESHOLD &&
2017 atmel_sha_dma_check_aligned(dd, req->src, req->nbytes))
2018 use_dma = true;
2019
2020 /* Write both initial hash values to compute a HMAC. */
2021 atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
2022 for (i = 0; i < num_words; ++i)
2023 atmel_sha_write(dd, SHA_REG_DIN(i), hmac->ipad[i]);
2024
2025 atmel_sha_write(dd, SHA_CR, SHA_CR_WUIEHV);
2026 for (i = 0; i < num_words; ++i)
2027 atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
2028
2029 /* Write the Mode, Message Size, Bytes Count then Control Registers. */
2030 mr = (SHA_MR_HMAC | SHA_MR_DUALBUFF);
2031 mr |= ctx->flags & SHA_FLAGS_ALGO_MASK;
2032 if (use_dma)
2033 mr |= SHA_MR_MODE_IDATAR0;
2034 else
2035 mr |= SHA_MR_MODE_AUTO;
2036 atmel_sha_write(dd, SHA_MR, mr);
2037
2038 atmel_sha_write(dd, SHA_MSR, req->nbytes);
2039 atmel_sha_write(dd, SHA_BCR, req->nbytes);
2040
2041 atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
2042
2043 /* Process data. */
2044 if (use_dma)
2045 return atmel_sha_dma_start(dd, req->src, req->nbytes,
2046 atmel_sha_hmac_final_done);
2047
2048 return atmel_sha_cpu_start(dd, req->src, req->nbytes, false, true,
2049 atmel_sha_hmac_final_done);
2050}
2051
2052static int atmel_sha_hmac_cra_init(struct crypto_tfm *tfm)
2053{
2054 struct atmel_sha_hmac_ctx *hmac = crypto_tfm_ctx(tfm);
2055
2056 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2057 sizeof(struct atmel_sha_reqctx));
2058 hmac->base.start = atmel_sha_hmac_start;
2059 atmel_sha_hmac_key_init(&hmac->hkey);
2060
2061 return 0;
2062}
2063
2064static void atmel_sha_hmac_cra_exit(struct crypto_tfm *tfm)
2065{
2066 struct atmel_sha_hmac_ctx *hmac = crypto_tfm_ctx(tfm);
2067
2068 atmel_sha_hmac_key_release(&hmac->hkey);
2069}
2070
2071static struct ahash_alg sha_hmac_algs[] = {
2072{
2073 .init = atmel_sha_hmac_init,
2074 .update = atmel_sha_update,
2075 .final = atmel_sha_final,
2076 .digest = atmel_sha_hmac_digest,
2077 .setkey = atmel_sha_hmac_setkey,
2078 .export = atmel_sha_export,
2079 .import = atmel_sha_import,
2080 .halg = {
2081 .digestsize = SHA1_DIGEST_SIZE,
2082 .statesize = sizeof(struct atmel_sha_reqctx),
2083 .base = {
2084 .cra_name = "hmac(sha1)",
2085 .cra_driver_name = "atmel-hmac-sha1",
2086 .cra_priority = 100,
2087 .cra_flags = CRYPTO_ALG_ASYNC,
2088 .cra_blocksize = SHA1_BLOCK_SIZE,
2089 .cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx),
2090 .cra_alignmask = 0,
2091 .cra_module = THIS_MODULE,
2092 .cra_init = atmel_sha_hmac_cra_init,
2093 .cra_exit = atmel_sha_hmac_cra_exit,
2094 }
2095 }
2096},
2097{
2098 .init = atmel_sha_hmac_init,
2099 .update = atmel_sha_update,
2100 .final = atmel_sha_final,
2101 .digest = atmel_sha_hmac_digest,
2102 .setkey = atmel_sha_hmac_setkey,
2103 .export = atmel_sha_export,
2104 .import = atmel_sha_import,
2105 .halg = {
2106 .digestsize = SHA224_DIGEST_SIZE,
2107 .statesize = sizeof(struct atmel_sha_reqctx),
2108 .base = {
2109 .cra_name = "hmac(sha224)",
2110 .cra_driver_name = "atmel-hmac-sha224",
2111 .cra_priority = 100,
2112 .cra_flags = CRYPTO_ALG_ASYNC,
2113 .cra_blocksize = SHA224_BLOCK_SIZE,
2114 .cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx),
2115 .cra_alignmask = 0,
2116 .cra_module = THIS_MODULE,
2117 .cra_init = atmel_sha_hmac_cra_init,
2118 .cra_exit = atmel_sha_hmac_cra_exit,
2119 }
2120 }
2121},
2122{
2123 .init = atmel_sha_hmac_init,
2124 .update = atmel_sha_update,
2125 .final = atmel_sha_final,
2126 .digest = atmel_sha_hmac_digest,
2127 .setkey = atmel_sha_hmac_setkey,
2128 .export = atmel_sha_export,
2129 .import = atmel_sha_import,
2130 .halg = {
2131 .digestsize = SHA256_DIGEST_SIZE,
2132 .statesize = sizeof(struct atmel_sha_reqctx),
2133 .base = {
2134 .cra_name = "hmac(sha256)",
2135 .cra_driver_name = "atmel-hmac-sha256",
2136 .cra_priority = 100,
2137 .cra_flags = CRYPTO_ALG_ASYNC,
2138 .cra_blocksize = SHA256_BLOCK_SIZE,
2139 .cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx),
2140 .cra_alignmask = 0,
2141 .cra_module = THIS_MODULE,
2142 .cra_init = atmel_sha_hmac_cra_init,
2143 .cra_exit = atmel_sha_hmac_cra_exit,
2144 }
2145 }
2146},
2147{
2148 .init = atmel_sha_hmac_init,
2149 .update = atmel_sha_update,
2150 .final = atmel_sha_final,
2151 .digest = atmel_sha_hmac_digest,
2152 .setkey = atmel_sha_hmac_setkey,
2153 .export = atmel_sha_export,
2154 .import = atmel_sha_import,
2155 .halg = {
2156 .digestsize = SHA384_DIGEST_SIZE,
2157 .statesize = sizeof(struct atmel_sha_reqctx),
2158 .base = {
2159 .cra_name = "hmac(sha384)",
2160 .cra_driver_name = "atmel-hmac-sha384",
2161 .cra_priority = 100,
2162 .cra_flags = CRYPTO_ALG_ASYNC,
2163 .cra_blocksize = SHA384_BLOCK_SIZE,
2164 .cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx),
2165 .cra_alignmask = 0,
2166 .cra_module = THIS_MODULE,
2167 .cra_init = atmel_sha_hmac_cra_init,
2168 .cra_exit = atmel_sha_hmac_cra_exit,
2169 }
2170 }
2171},
2172{
2173 .init = atmel_sha_hmac_init,
2174 .update = atmel_sha_update,
2175 .final = atmel_sha_final,
2176 .digest = atmel_sha_hmac_digest,
2177 .setkey = atmel_sha_hmac_setkey,
2178 .export = atmel_sha_export,
2179 .import = atmel_sha_import,
2180 .halg = {
2181 .digestsize = SHA512_DIGEST_SIZE,
2182 .statesize = sizeof(struct atmel_sha_reqctx),
2183 .base = {
2184 .cra_name = "hmac(sha512)",
2185 .cra_driver_name = "atmel-hmac-sha512",
2186 .cra_priority = 100,
2187 .cra_flags = CRYPTO_ALG_ASYNC,
2188 .cra_blocksize = SHA512_BLOCK_SIZE,
2189 .cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx),
2190 .cra_alignmask = 0,
2191 .cra_module = THIS_MODULE,
2192 .cra_init = atmel_sha_hmac_cra_init,
2193 .cra_exit = atmel_sha_hmac_cra_exit,
2194 }
2195 }
2196},
2197};
Cyrille Pitcheneec12f62017-01-26 17:07:52 +01002198
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002199#ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
2200/* authenc functions */
2201
2202static int atmel_sha_authenc_init2(struct atmel_sha_dev *dd);
2203static int atmel_sha_authenc_init_done(struct atmel_sha_dev *dd);
2204static int atmel_sha_authenc_final_done(struct atmel_sha_dev *dd);
2205
2206
2207struct atmel_sha_authenc_ctx {
2208 struct crypto_ahash *tfm;
2209};
2210
2211struct atmel_sha_authenc_reqctx {
2212 struct atmel_sha_reqctx base;
2213
2214 atmel_aes_authenc_fn_t cb;
2215 struct atmel_aes_dev *aes_dev;
2216
2217 /* _init() parameters. */
2218 struct scatterlist *assoc;
2219 u32 assoclen;
2220 u32 textlen;
2221
2222 /* _final() parameters. */
2223 u32 *digest;
2224 unsigned int digestlen;
2225};
2226
2227static void atmel_sha_authenc_complete(struct crypto_async_request *areq,
2228 int err)
2229{
2230 struct ahash_request *req = areq->data;
2231 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
2232
2233 authctx->cb(authctx->aes_dev, err, authctx->base.dd->is_async);
2234}
2235
2236static int atmel_sha_authenc_start(struct atmel_sha_dev *dd)
2237{
2238 struct ahash_request *req = dd->req;
2239 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
2240 int err;
2241
2242 /*
2243 * Force atmel_sha_complete() to call req->base.complete(), ie
2244 * atmel_sha_authenc_complete(), which in turn calls authctx->cb().
2245 */
2246 dd->force_complete = true;
2247
2248 err = atmel_sha_hw_init(dd);
2249 return authctx->cb(authctx->aes_dev, err, dd->is_async);
2250}
2251
2252bool atmel_sha_authenc_is_ready(void)
2253{
2254 struct atmel_sha_ctx dummy;
2255
2256 dummy.dd = NULL;
2257 return (atmel_sha_find_dev(&dummy) != NULL);
2258}
2259EXPORT_SYMBOL_GPL(atmel_sha_authenc_is_ready);
2260
2261unsigned int atmel_sha_authenc_get_reqsize(void)
2262{
2263 return sizeof(struct atmel_sha_authenc_reqctx);
2264}
2265EXPORT_SYMBOL_GPL(atmel_sha_authenc_get_reqsize);
2266
2267struct atmel_sha_authenc_ctx *atmel_sha_authenc_spawn(unsigned long mode)
2268{
2269 struct atmel_sha_authenc_ctx *auth;
2270 struct crypto_ahash *tfm;
2271 struct atmel_sha_ctx *tctx;
2272 const char *name;
2273 int err = -EINVAL;
2274
2275 switch (mode & SHA_FLAGS_MODE_MASK) {
2276 case SHA_FLAGS_HMAC_SHA1:
2277 name = "atmel-hmac-sha1";
2278 break;
2279
2280 case SHA_FLAGS_HMAC_SHA224:
2281 name = "atmel-hmac-sha224";
2282 break;
2283
2284 case SHA_FLAGS_HMAC_SHA256:
2285 name = "atmel-hmac-sha256";
2286 break;
2287
2288 case SHA_FLAGS_HMAC_SHA384:
2289 name = "atmel-hmac-sha384";
2290 break;
2291
2292 case SHA_FLAGS_HMAC_SHA512:
2293 name = "atmel-hmac-sha512";
2294 break;
2295
2296 default:
2297 goto error;
2298 }
2299
2300 tfm = crypto_alloc_ahash(name,
2301 CRYPTO_ALG_TYPE_AHASH,
2302 CRYPTO_ALG_TYPE_AHASH_MASK);
2303 if (IS_ERR(tfm)) {
2304 err = PTR_ERR(tfm);
2305 goto error;
2306 }
2307 tctx = crypto_ahash_ctx(tfm);
2308 tctx->start = atmel_sha_authenc_start;
2309 tctx->flags = mode;
2310
2311 auth = kzalloc(sizeof(*auth), GFP_KERNEL);
2312 if (!auth) {
2313 err = -ENOMEM;
2314 goto err_free_ahash;
2315 }
2316 auth->tfm = tfm;
2317
2318 return auth;
2319
2320err_free_ahash:
2321 crypto_free_ahash(tfm);
2322error:
2323 return ERR_PTR(err);
2324}
2325EXPORT_SYMBOL_GPL(atmel_sha_authenc_spawn);
2326
2327void atmel_sha_authenc_free(struct atmel_sha_authenc_ctx *auth)
2328{
2329 if (auth)
2330 crypto_free_ahash(auth->tfm);
2331 kfree(auth);
2332}
2333EXPORT_SYMBOL_GPL(atmel_sha_authenc_free);
2334
2335int atmel_sha_authenc_setkey(struct atmel_sha_authenc_ctx *auth,
2336 const u8 *key, unsigned int keylen,
2337 u32 *flags)
2338{
2339 struct crypto_ahash *tfm = auth->tfm;
2340 int err;
2341
2342 crypto_ahash_clear_flags(tfm, CRYPTO_TFM_REQ_MASK);
2343 crypto_ahash_set_flags(tfm, *flags & CRYPTO_TFM_REQ_MASK);
2344 err = crypto_ahash_setkey(tfm, key, keylen);
2345 *flags = crypto_ahash_get_flags(tfm);
2346
2347 return err;
2348}
2349EXPORT_SYMBOL_GPL(atmel_sha_authenc_setkey);
2350
2351int atmel_sha_authenc_schedule(struct ahash_request *req,
2352 struct atmel_sha_authenc_ctx *auth,
2353 atmel_aes_authenc_fn_t cb,
2354 struct atmel_aes_dev *aes_dev)
2355{
2356 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
2357 struct atmel_sha_reqctx *ctx = &authctx->base;
2358 struct crypto_ahash *tfm = auth->tfm;
2359 struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm);
2360 struct atmel_sha_dev *dd;
2361
2362 /* Reset request context (MUST be done first). */
2363 memset(authctx, 0, sizeof(*authctx));
2364
2365 /* Get SHA device. */
2366 dd = atmel_sha_find_dev(tctx);
2367 if (!dd)
2368 return cb(aes_dev, -ENODEV, false);
2369
2370 /* Init request context. */
2371 ctx->dd = dd;
2372 ctx->buflen = SHA_BUFFER_LEN;
2373 authctx->cb = cb;
2374 authctx->aes_dev = aes_dev;
2375 ahash_request_set_tfm(req, tfm);
2376 ahash_request_set_callback(req, 0, atmel_sha_authenc_complete, req);
2377
2378 return atmel_sha_handle_queue(dd, req);
2379}
2380EXPORT_SYMBOL_GPL(atmel_sha_authenc_schedule);
2381
2382int atmel_sha_authenc_init(struct ahash_request *req,
2383 struct scatterlist *assoc, unsigned int assoclen,
2384 unsigned int textlen,
2385 atmel_aes_authenc_fn_t cb,
2386 struct atmel_aes_dev *aes_dev)
2387{
2388 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
2389 struct atmel_sha_reqctx *ctx = &authctx->base;
2390 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2391 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
2392 struct atmel_sha_dev *dd = ctx->dd;
2393
2394 if (unlikely(!IS_ALIGNED(assoclen, sizeof(u32))))
2395 return atmel_sha_complete(dd, -EINVAL);
2396
2397 authctx->cb = cb;
2398 authctx->aes_dev = aes_dev;
2399 authctx->assoc = assoc;
2400 authctx->assoclen = assoclen;
2401 authctx->textlen = textlen;
2402
2403 ctx->flags = hmac->base.flags;
2404 return atmel_sha_hmac_setup(dd, atmel_sha_authenc_init2);
2405}
2406EXPORT_SYMBOL_GPL(atmel_sha_authenc_init);
2407
2408static int atmel_sha_authenc_init2(struct atmel_sha_dev *dd)
2409{
2410 struct ahash_request *req = dd->req;
2411 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
2412 struct atmel_sha_reqctx *ctx = &authctx->base;
2413 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2414 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
2415 size_t hs = ctx->hash_size;
2416 size_t i, num_words = hs / sizeof(u32);
2417 u32 mr, msg_size;
2418
2419 atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
2420 for (i = 0; i < num_words; ++i)
2421 atmel_sha_write(dd, SHA_REG_DIN(i), hmac->ipad[i]);
2422
2423 atmel_sha_write(dd, SHA_CR, SHA_CR_WUIEHV);
2424 for (i = 0; i < num_words; ++i)
2425 atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
2426
2427 mr = (SHA_MR_MODE_IDATAR0 |
2428 SHA_MR_HMAC |
2429 SHA_MR_DUALBUFF);
2430 mr |= ctx->flags & SHA_FLAGS_ALGO_MASK;
2431 atmel_sha_write(dd, SHA_MR, mr);
2432
2433 msg_size = authctx->assoclen + authctx->textlen;
2434 atmel_sha_write(dd, SHA_MSR, msg_size);
2435 atmel_sha_write(dd, SHA_BCR, msg_size);
2436
2437 atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
2438
2439 /* Process assoc data. */
2440 return atmel_sha_cpu_start(dd, authctx->assoc, authctx->assoclen,
2441 true, false,
2442 atmel_sha_authenc_init_done);
2443}
2444
2445static int atmel_sha_authenc_init_done(struct atmel_sha_dev *dd)
2446{
2447 struct ahash_request *req = dd->req;
2448 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
2449
2450 return authctx->cb(authctx->aes_dev, 0, dd->is_async);
2451}
2452
2453int atmel_sha_authenc_final(struct ahash_request *req,
2454 u32 *digest, unsigned int digestlen,
2455 atmel_aes_authenc_fn_t cb,
2456 struct atmel_aes_dev *aes_dev)
2457{
2458 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
2459 struct atmel_sha_reqctx *ctx = &authctx->base;
2460 struct atmel_sha_dev *dd = ctx->dd;
2461
2462 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
2463 case SHA_FLAGS_SHA1:
2464 authctx->digestlen = SHA1_DIGEST_SIZE;
2465 break;
2466
2467 case SHA_FLAGS_SHA224:
2468 authctx->digestlen = SHA224_DIGEST_SIZE;
2469 break;
2470
2471 case SHA_FLAGS_SHA256:
2472 authctx->digestlen = SHA256_DIGEST_SIZE;
2473 break;
2474
2475 case SHA_FLAGS_SHA384:
2476 authctx->digestlen = SHA384_DIGEST_SIZE;
2477 break;
2478
2479 case SHA_FLAGS_SHA512:
2480 authctx->digestlen = SHA512_DIGEST_SIZE;
2481 break;
2482
2483 default:
2484 return atmel_sha_complete(dd, -EINVAL);
2485 }
2486 if (authctx->digestlen > digestlen)
2487 authctx->digestlen = digestlen;
2488
2489 authctx->cb = cb;
2490 authctx->aes_dev = aes_dev;
2491 authctx->digest = digest;
2492 return atmel_sha_wait_for_data_ready(dd,
2493 atmel_sha_authenc_final_done);
2494}
2495EXPORT_SYMBOL_GPL(atmel_sha_authenc_final);
2496
2497static int atmel_sha_authenc_final_done(struct atmel_sha_dev *dd)
2498{
2499 struct ahash_request *req = dd->req;
2500 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
2501 size_t i, num_words = authctx->digestlen / sizeof(u32);
2502
2503 for (i = 0; i < num_words; ++i)
2504 authctx->digest[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
2505
2506 return atmel_sha_complete(dd, 0);
2507}
2508
2509void atmel_sha_authenc_abort(struct ahash_request *req)
2510{
2511 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
2512 struct atmel_sha_reqctx *ctx = &authctx->base;
2513 struct atmel_sha_dev *dd = ctx->dd;
2514
2515 /* Prevent atmel_sha_complete() from calling req->base.complete(). */
2516 dd->is_async = false;
2517 dd->force_complete = false;
2518 (void)atmel_sha_complete(dd, 0);
2519}
2520EXPORT_SYMBOL_GPL(atmel_sha_authenc_abort);
2521
2522#endif /* CONFIG_CRYPTO_DEV_ATMEL_AUTHENC */
2523
2524
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002525static void atmel_sha_unregister_algs(struct atmel_sha_dev *dd)
2526{
2527 int i;
2528
Cyrille Pitchen81d87502017-01-26 17:07:54 +01002529 if (dd->caps.has_hmac)
2530 for (i = 0; i < ARRAY_SIZE(sha_hmac_algs); i++)
2531 crypto_unregister_ahash(&sha_hmac_algs[i]);
2532
Nicolas Royerd4905b32013-02-20 17:10:26 +01002533 for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++)
2534 crypto_unregister_ahash(&sha_1_256_algs[i]);
2535
2536 if (dd->caps.has_sha224)
2537 crypto_unregister_ahash(&sha_224_alg);
2538
2539 if (dd->caps.has_sha_384_512) {
2540 for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++)
2541 crypto_unregister_ahash(&sha_384_512_algs[i]);
2542 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002543}
2544
2545static int atmel_sha_register_algs(struct atmel_sha_dev *dd)
2546{
2547 int err, i, j;
2548
Nicolas Royerd4905b32013-02-20 17:10:26 +01002549 for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++) {
2550 err = crypto_register_ahash(&sha_1_256_algs[i]);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002551 if (err)
Nicolas Royerd4905b32013-02-20 17:10:26 +01002552 goto err_sha_1_256_algs;
2553 }
2554
2555 if (dd->caps.has_sha224) {
2556 err = crypto_register_ahash(&sha_224_alg);
2557 if (err)
2558 goto err_sha_224_algs;
2559 }
2560
2561 if (dd->caps.has_sha_384_512) {
2562 for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++) {
2563 err = crypto_register_ahash(&sha_384_512_algs[i]);
2564 if (err)
2565 goto err_sha_384_512_algs;
2566 }
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002567 }
2568
Cyrille Pitchen81d87502017-01-26 17:07:54 +01002569 if (dd->caps.has_hmac) {
2570 for (i = 0; i < ARRAY_SIZE(sha_hmac_algs); i++) {
2571 err = crypto_register_ahash(&sha_hmac_algs[i]);
2572 if (err)
2573 goto err_sha_hmac_algs;
2574 }
2575 }
2576
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002577 return 0;
2578
Cyrille Pitchen81d87502017-01-26 17:07:54 +01002579 /*i = ARRAY_SIZE(sha_hmac_algs);*/
2580err_sha_hmac_algs:
2581 for (j = 0; j < i; j++)
2582 crypto_unregister_ahash(&sha_hmac_algs[j]);
2583 i = ARRAY_SIZE(sha_384_512_algs);
Nicolas Royerd4905b32013-02-20 17:10:26 +01002584err_sha_384_512_algs:
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002585 for (j = 0; j < i; j++)
Nicolas Royerd4905b32013-02-20 17:10:26 +01002586 crypto_unregister_ahash(&sha_384_512_algs[j]);
2587 crypto_unregister_ahash(&sha_224_alg);
2588err_sha_224_algs:
2589 i = ARRAY_SIZE(sha_1_256_algs);
2590err_sha_1_256_algs:
2591 for (j = 0; j < i; j++)
2592 crypto_unregister_ahash(&sha_1_256_algs[j]);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002593
2594 return err;
2595}
2596
Nicolas Royerd4905b32013-02-20 17:10:26 +01002597static bool atmel_sha_filter(struct dma_chan *chan, void *slave)
2598{
2599 struct at_dma_slave *sl = slave;
2600
2601 if (sl && sl->dma_dev == chan->device->dev) {
2602 chan->private = sl;
2603 return true;
2604 } else {
2605 return false;
2606 }
2607}
2608
2609static int atmel_sha_dma_init(struct atmel_sha_dev *dd,
2610 struct crypto_platform_data *pdata)
2611{
2612 int err = -ENOMEM;
2613 dma_cap_mask_t mask_in;
2614
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02002615 /* Try to grab DMA channel */
2616 dma_cap_zero(mask_in);
2617 dma_cap_set(DMA_SLAVE, mask_in);
Nicolas Royerd4905b32013-02-20 17:10:26 +01002618
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02002619 dd->dma_lch_in.chan = dma_request_slave_channel_compat(mask_in,
2620 atmel_sha_filter, &pdata->dma_slave->rxdata, dd->dev, "tx");
2621 if (!dd->dma_lch_in.chan) {
2622 dev_warn(dd->dev, "no DMA channel available\n");
2623 return err;
Nicolas Royerd4905b32013-02-20 17:10:26 +01002624 }
2625
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02002626 dd->dma_lch_in.dma_conf.direction = DMA_MEM_TO_DEV;
2627 dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base +
2628 SHA_REG_DIN(0);
2629 dd->dma_lch_in.dma_conf.src_maxburst = 1;
2630 dd->dma_lch_in.dma_conf.src_addr_width =
2631 DMA_SLAVE_BUSWIDTH_4_BYTES;
2632 dd->dma_lch_in.dma_conf.dst_maxburst = 1;
2633 dd->dma_lch_in.dma_conf.dst_addr_width =
2634 DMA_SLAVE_BUSWIDTH_4_BYTES;
2635 dd->dma_lch_in.dma_conf.device_fc = false;
2636
2637 return 0;
Nicolas Royerd4905b32013-02-20 17:10:26 +01002638}
2639
2640static void atmel_sha_dma_cleanup(struct atmel_sha_dev *dd)
2641{
2642 dma_release_channel(dd->dma_lch_in.chan);
2643}
2644
2645static void atmel_sha_get_cap(struct atmel_sha_dev *dd)
2646{
2647
2648 dd->caps.has_dma = 0;
2649 dd->caps.has_dualbuff = 0;
2650 dd->caps.has_sha224 = 0;
2651 dd->caps.has_sha_384_512 = 0;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +01002652 dd->caps.has_uihv = 0;
Cyrille Pitchen81d87502017-01-26 17:07:54 +01002653 dd->caps.has_hmac = 0;
Nicolas Royerd4905b32013-02-20 17:10:26 +01002654
2655 /* keep only major version number */
2656 switch (dd->hw_version & 0xff0) {
Cyrille Pitchen507c5cc2016-01-15 15:49:33 +01002657 case 0x510:
2658 dd->caps.has_dma = 1;
2659 dd->caps.has_dualbuff = 1;
2660 dd->caps.has_sha224 = 1;
2661 dd->caps.has_sha_384_512 = 1;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +01002662 dd->caps.has_uihv = 1;
Cyrille Pitchen81d87502017-01-26 17:07:54 +01002663 dd->caps.has_hmac = 1;
Cyrille Pitchen507c5cc2016-01-15 15:49:33 +01002664 break;
Leilei Zhao141824d2015-04-07 17:45:03 +08002665 case 0x420:
2666 dd->caps.has_dma = 1;
2667 dd->caps.has_dualbuff = 1;
2668 dd->caps.has_sha224 = 1;
2669 dd->caps.has_sha_384_512 = 1;
Cyrille Pitchen7cee3502016-01-15 15:49:34 +01002670 dd->caps.has_uihv = 1;
Leilei Zhao141824d2015-04-07 17:45:03 +08002671 break;
Nicolas Royerd4905b32013-02-20 17:10:26 +01002672 case 0x410:
2673 dd->caps.has_dma = 1;
2674 dd->caps.has_dualbuff = 1;
2675 dd->caps.has_sha224 = 1;
2676 dd->caps.has_sha_384_512 = 1;
2677 break;
2678 case 0x400:
2679 dd->caps.has_dma = 1;
2680 dd->caps.has_dualbuff = 1;
2681 dd->caps.has_sha224 = 1;
2682 break;
2683 case 0x320:
2684 break;
2685 default:
2686 dev_warn(dd->dev,
2687 "Unmanaged sha version, set minimum capabilities\n");
2688 break;
2689 }
2690}
2691
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02002692#if defined(CONFIG_OF)
2693static const struct of_device_id atmel_sha_dt_ids[] = {
2694 { .compatible = "atmel,at91sam9g46-sha" },
2695 { /* sentinel */ }
2696};
2697
2698MODULE_DEVICE_TABLE(of, atmel_sha_dt_ids);
2699
2700static struct crypto_platform_data *atmel_sha_of_init(struct platform_device *pdev)
2701{
2702 struct device_node *np = pdev->dev.of_node;
2703 struct crypto_platform_data *pdata;
2704
2705 if (!np) {
2706 dev_err(&pdev->dev, "device node not found\n");
2707 return ERR_PTR(-EINVAL);
2708 }
2709
2710 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2711 if (!pdata) {
2712 dev_err(&pdev->dev, "could not allocate memory for pdata\n");
2713 return ERR_PTR(-ENOMEM);
2714 }
2715
2716 pdata->dma_slave = devm_kzalloc(&pdev->dev,
2717 sizeof(*(pdata->dma_slave)),
2718 GFP_KERNEL);
2719 if (!pdata->dma_slave) {
2720 dev_err(&pdev->dev, "could not allocate memory for dma_slave\n");
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02002721 return ERR_PTR(-ENOMEM);
2722 }
2723
2724 return pdata;
2725}
2726#else /* CONFIG_OF */
2727static inline struct crypto_platform_data *atmel_sha_of_init(struct platform_device *dev)
2728{
2729 return ERR_PTR(-EINVAL);
2730}
2731#endif
2732
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002733static int atmel_sha_probe(struct platform_device *pdev)
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002734{
2735 struct atmel_sha_dev *sha_dd;
Nicolas Royerd4905b32013-02-20 17:10:26 +01002736 struct crypto_platform_data *pdata;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002737 struct device *dev = &pdev->dev;
2738 struct resource *sha_res;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002739 int err;
2740
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002741 sha_dd = devm_kzalloc(&pdev->dev, sizeof(*sha_dd), GFP_KERNEL);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002742 if (sha_dd == NULL) {
2743 dev_err(dev, "unable to alloc data struct.\n");
2744 err = -ENOMEM;
2745 goto sha_dd_err;
2746 }
2747
2748 sha_dd->dev = dev;
2749
2750 platform_set_drvdata(pdev, sha_dd);
2751
2752 INIT_LIST_HEAD(&sha_dd->list);
Leilei Zhao62728e82015-04-07 17:45:06 +08002753 spin_lock_init(&sha_dd->lock);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002754
2755 tasklet_init(&sha_dd->done_task, atmel_sha_done_task,
2756 (unsigned long)sha_dd);
Cyrille Pitchenf56809c2016-01-15 15:49:32 +01002757 tasklet_init(&sha_dd->queue_task, atmel_sha_queue_task,
2758 (unsigned long)sha_dd);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002759
2760 crypto_init_queue(&sha_dd->queue, ATMEL_SHA_QUEUE_LENGTH);
2761
2762 sha_dd->irq = -1;
2763
2764 /* Get the base address */
2765 sha_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2766 if (!sha_res) {
2767 dev_err(dev, "no MEM resource info\n");
2768 err = -ENODEV;
2769 goto res_err;
2770 }
2771 sha_dd->phys_base = sha_res->start;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002772
2773 /* Get the IRQ */
2774 sha_dd->irq = platform_get_irq(pdev, 0);
2775 if (sha_dd->irq < 0) {
2776 dev_err(dev, "no IRQ resource info\n");
2777 err = sha_dd->irq;
2778 goto res_err;
2779 }
2780
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002781 err = devm_request_irq(&pdev->dev, sha_dd->irq, atmel_sha_irq,
2782 IRQF_SHARED, "atmel-sha", sha_dd);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002783 if (err) {
2784 dev_err(dev, "unable to request sha irq.\n");
2785 goto res_err;
2786 }
2787
2788 /* Initializing the clock */
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002789 sha_dd->iclk = devm_clk_get(&pdev->dev, "sha_clk");
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002790 if (IS_ERR(sha_dd->iclk)) {
Colin Ian Kingbe208352015-02-28 20:40:10 +00002791 dev_err(dev, "clock initialization failed.\n");
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002792 err = PTR_ERR(sha_dd->iclk);
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002793 goto res_err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002794 }
2795
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002796 sha_dd->io_base = devm_ioremap_resource(&pdev->dev, sha_res);
Vladimir Zapolskiy9b52d552016-03-06 03:21:52 +02002797 if (IS_ERR(sha_dd->io_base)) {
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002798 dev_err(dev, "can't ioremap\n");
Vladimir Zapolskiy9b52d552016-03-06 03:21:52 +02002799 err = PTR_ERR(sha_dd->io_base);
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002800 goto res_err;
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002801 }
2802
Cyrille Pitchenc0330422016-02-05 13:45:13 +01002803 err = clk_prepare(sha_dd->iclk);
2804 if (err)
2805 goto res_err;
2806
Nicolas Royerd4905b32013-02-20 17:10:26 +01002807 atmel_sha_hw_version_init(sha_dd);
2808
2809 atmel_sha_get_cap(sha_dd);
2810
2811 if (sha_dd->caps.has_dma) {
2812 pdata = pdev->dev.platform_data;
2813 if (!pdata) {
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02002814 pdata = atmel_sha_of_init(pdev);
2815 if (IS_ERR(pdata)) {
2816 dev_err(&pdev->dev, "platform data not available\n");
2817 err = PTR_ERR(pdata);
Cyrille Pitchenc0330422016-02-05 13:45:13 +01002818 goto iclk_unprepare;
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02002819 }
2820 }
2821 if (!pdata->dma_slave) {
Nicolas Royerd4905b32013-02-20 17:10:26 +01002822 err = -ENXIO;
Cyrille Pitchenc0330422016-02-05 13:45:13 +01002823 goto iclk_unprepare;
Nicolas Royerd4905b32013-02-20 17:10:26 +01002824 }
2825 err = atmel_sha_dma_init(sha_dd, pdata);
2826 if (err)
2827 goto err_sha_dma;
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02002828
2829 dev_info(dev, "using %s for DMA transfers\n",
2830 dma_chan_name(sha_dd->dma_lch_in.chan));
Nicolas Royerd4905b32013-02-20 17:10:26 +01002831 }
2832
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002833 spin_lock(&atmel_sha.lock);
2834 list_add_tail(&sha_dd->list, &atmel_sha.dev_list);
2835 spin_unlock(&atmel_sha.lock);
2836
2837 err = atmel_sha_register_algs(sha_dd);
2838 if (err)
2839 goto err_algs;
2840
Nicolas Ferre1ca5b7d2013-10-15 16:37:44 +02002841 dev_info(dev, "Atmel SHA1/SHA256%s%s\n",
2842 sha_dd->caps.has_sha224 ? "/SHA224" : "",
2843 sha_dd->caps.has_sha_384_512 ? "/SHA384/SHA512" : "");
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002844
2845 return 0;
2846
2847err_algs:
2848 spin_lock(&atmel_sha.lock);
2849 list_del(&sha_dd->list);
2850 spin_unlock(&atmel_sha.lock);
Nicolas Royerd4905b32013-02-20 17:10:26 +01002851 if (sha_dd->caps.has_dma)
2852 atmel_sha_dma_cleanup(sha_dd);
2853err_sha_dma:
Cyrille Pitchenc0330422016-02-05 13:45:13 +01002854iclk_unprepare:
2855 clk_unprepare(sha_dd->iclk);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002856res_err:
Cyrille Pitchenf56809c2016-01-15 15:49:32 +01002857 tasklet_kill(&sha_dd->queue_task);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002858 tasklet_kill(&sha_dd->done_task);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002859sha_dd_err:
2860 dev_err(dev, "initialization failed.\n");
2861
2862 return err;
2863}
2864
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002865static int atmel_sha_remove(struct platform_device *pdev)
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002866{
2867 static struct atmel_sha_dev *sha_dd;
2868
2869 sha_dd = platform_get_drvdata(pdev);
2870 if (!sha_dd)
2871 return -ENODEV;
2872 spin_lock(&atmel_sha.lock);
2873 list_del(&sha_dd->list);
2874 spin_unlock(&atmel_sha.lock);
2875
2876 atmel_sha_unregister_algs(sha_dd);
2877
Cyrille Pitchenf56809c2016-01-15 15:49:32 +01002878 tasklet_kill(&sha_dd->queue_task);
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002879 tasklet_kill(&sha_dd->done_task);
2880
Nicolas Royerd4905b32013-02-20 17:10:26 +01002881 if (sha_dd->caps.has_dma)
2882 atmel_sha_dma_cleanup(sha_dd);
2883
Cyrille Pitchenc0330422016-02-05 13:45:13 +01002884 clk_unprepare(sha_dd->iclk);
2885
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002886 return 0;
2887}
2888
2889static struct platform_driver atmel_sha_driver = {
2890 .probe = atmel_sha_probe,
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002891 .remove = atmel_sha_remove,
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002892 .driver = {
2893 .name = "atmel_sha",
Nicolas Ferreabfe7ae2013-10-15 15:36:34 +02002894 .of_match_table = of_match_ptr(atmel_sha_dt_ids),
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002895 },
2896};
2897
2898module_platform_driver(atmel_sha_driver);
2899
Nicolas Royerd4905b32013-02-20 17:10:26 +01002900MODULE_DESCRIPTION("Atmel SHA (1/256/224/384/512) hw acceleration support.");
Nicolas Royerebc82ef2012-07-01 19:19:46 +02002901MODULE_LICENSE("GPL v2");
2902MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");