Blame view

drivers/crypto/atmel-sha.c 71.7 KB
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  /*
   * Cryptographic API.
   *
   * Support for ATMEL SHA1/SHA256 HW acceleration.
   *
   * Copyright (c) 2012 Eukréa Electromatique - ATMEL
   * Author: Nicolas Royer <nicolas@eukrea.com>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as published
   * by the Free Software Foundation.
   *
   * Some ideas are from omap-sham.c drivers.
   */
  
  
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/slab.h>
  #include <linux/err.h>
  #include <linux/clk.h>
  #include <linux/io.h>
  #include <linux/hw_random.h>
  #include <linux/platform_device.h>
  
  #include <linux/device.h>
ebc82efa1   Nicolas Royer   crypto: atmel - a...
27
28
29
  #include <linux/init.h>
  #include <linux/errno.h>
  #include <linux/interrupt.h>
ebc82efa1   Nicolas Royer   crypto: atmel - a...
30
  #include <linux/irq.h>
ebc82efa1   Nicolas Royer   crypto: atmel - a...
31
32
  #include <linux/scatterlist.h>
  #include <linux/dma-mapping.h>
abfe7ae40   Nicolas Ferre   crypto: atmel-sha...
33
  #include <linux/of_device.h>
ebc82efa1   Nicolas Royer   crypto: atmel - a...
34
35
36
37
38
39
40
41
  #include <linux/delay.h>
  #include <linux/crypto.h>
  #include <linux/cryptohash.h>
  #include <crypto/scatterwalk.h>
  #include <crypto/algapi.h>
  #include <crypto/sha.h>
  #include <crypto/hash.h>
  #include <crypto/internal/hash.h>
d4905b38d   Nicolas Royer   crypto: atmel-sha...
42
  #include <linux/platform_data/crypto-atmel.h>
ebc82efa1   Nicolas Royer   crypto: atmel - a...
43
  #include "atmel-sha-regs.h"
89a82ef87   Cyrille Pitchen   crypto: atmel-aut...
44
  #include "atmel-authenc.h"
ebc82efa1   Nicolas Royer   crypto: atmel - a...
45
46
47
48
49
50
51
52
53
  
  /* SHA flags */
  #define SHA_FLAGS_BUSY			BIT(0)
  #define	SHA_FLAGS_FINAL			BIT(1)
  #define SHA_FLAGS_DMA_ACTIVE	BIT(2)
  #define SHA_FLAGS_OUTPUT_READY	BIT(3)
  #define SHA_FLAGS_INIT			BIT(4)
  #define SHA_FLAGS_CPU			BIT(5)
  #define SHA_FLAGS_DMA_READY		BIT(6)
0569fc46f   Cyrille Pitchen   crypto: atmel-sha...
54
  #define SHA_FLAGS_DUMP_REG	BIT(7)
ebc82efa1   Nicolas Royer   crypto: atmel - a...
55

81d8750b2   Cyrille Pitchen   crypto: atmel-sha...
56
  /* bits[11:8] are reserved. */
f07cebad6   Cyrille Pitchen   crypto: atmel-sha...
57

ebc82efa1   Nicolas Royer   crypto: atmel - a...
58
59
  #define SHA_FLAGS_FINUP		BIT(16)
  #define SHA_FLAGS_SG		BIT(17)
d4905b38d   Nicolas Royer   crypto: atmel-sha...
60
61
  #define SHA_FLAGS_ERROR		BIT(23)
  #define SHA_FLAGS_PAD		BIT(24)
7cee35081   Cyrille Pitchen   crypto: atmel-sha...
62
  #define SHA_FLAGS_RESTORE	BIT(25)
eec12f66b   Cyrille Pitchen   crypto: atmel-sha...
63
64
  #define SHA_FLAGS_IDATAR0	BIT(26)
  #define SHA_FLAGS_WAIT_DATARDY	BIT(27)
ebc82efa1   Nicolas Royer   crypto: atmel - a...
65

81d8750b2   Cyrille Pitchen   crypto: atmel-sha...
66
  #define SHA_OP_INIT	0
ebc82efa1   Nicolas Royer   crypto: atmel - a...
67
68
  #define SHA_OP_UPDATE	1
  #define SHA_OP_FINAL	2
81d8750b2   Cyrille Pitchen   crypto: atmel-sha...
69
  #define SHA_OP_DIGEST	3
ebc82efa1   Nicolas Royer   crypto: atmel - a...
70

cc831d32d   Cyrille Pitchen   crypto: atmel-sha...
71
  #define SHA_BUFFER_LEN		(PAGE_SIZE / 16)
ebc82efa1   Nicolas Royer   crypto: atmel - a...
72
73
  
  #define ATMEL_SHA_DMA_THRESHOLD		56
d4905b38d   Nicolas Royer   crypto: atmel-sha...
74
75
76
77
78
  struct atmel_sha_caps {
  	bool	has_dma;
  	bool	has_dualbuff;
  	bool	has_sha224;
  	bool	has_sha_384_512;
7cee35081   Cyrille Pitchen   crypto: atmel-sha...
79
  	bool	has_uihv;
81d8750b2   Cyrille Pitchen   crypto: atmel-sha...
80
  	bool	has_hmac;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
81
  };
ebc82efa1   Nicolas Royer   crypto: atmel - a...
82
83
  
  struct atmel_sha_dev;
cc831d32d   Cyrille Pitchen   crypto: atmel-sha...
84
  /*
9c4274d90   Cyrille Pitchen   crypto: atmel-sha...
85
   * .statesize = sizeof(struct atmel_sha_reqctx) must be <= PAGE_SIZE / 8 as
cc831d32d   Cyrille Pitchen   crypto: atmel-sha...
86
87
   * tested by the ahash_prepare_alg() function.
   */
ebc82efa1   Nicolas Royer   crypto: atmel - a...
88
89
90
91
  struct atmel_sha_reqctx {
  	struct atmel_sha_dev	*dd;
  	unsigned long	flags;
  	unsigned long	op;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
92
93
  	u8	digest[SHA512_DIGEST_SIZE] __aligned(sizeof(u32));
  	u64	digcnt[2];
ebc82efa1   Nicolas Royer   crypto: atmel - a...
94
95
96
97
98
99
100
101
  	size_t	bufcnt;
  	size_t	buflen;
  	dma_addr_t	dma_addr;
  
  	/* walk state */
  	struct scatterlist	*sg;
  	unsigned int	offset;	/* offset in current sg */
  	unsigned int	total;	/* total request */
d4905b38d   Nicolas Royer   crypto: atmel-sha...
102
  	size_t block_size;
81d8750b2   Cyrille Pitchen   crypto: atmel-sha...
103
  	size_t hash_size;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
104

9c4274d90   Cyrille Pitchen   crypto: atmel-sha...
105
  	u8 buffer[SHA_BUFFER_LEN + SHA512_BLOCK_SIZE] __aligned(sizeof(u32));
ebc82efa1   Nicolas Royer   crypto: atmel - a...
106
  };
a29af939b   Cyrille Pitchen   crypto: atmel-sha...
107
  typedef int (*atmel_sha_fn_t)(struct atmel_sha_dev *);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
108
109
  struct atmel_sha_ctx {
  	struct atmel_sha_dev	*dd;
a29af939b   Cyrille Pitchen   crypto: atmel-sha...
110
  	atmel_sha_fn_t		start;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
111
112
  
  	unsigned long		flags;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
113
  };
d4905b38d   Nicolas Royer   crypto: atmel-sha...
114
115
116
117
118
  #define ATMEL_SHA_QUEUE_LENGTH	50
  
  struct atmel_sha_dma {
  	struct dma_chan			*chan;
  	struct dma_slave_config dma_conf;
69303cf0f   Cyrille Pitchen   crypto: atmel-sha...
119
120
121
  	struct scatterlist	*sg;
  	int			nents;
  	unsigned int		last_sg_length;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
122
  };
ebc82efa1   Nicolas Royer   crypto: atmel - a...
123
124
125
126
127
128
129
130
131
132
133
134
  
  struct atmel_sha_dev {
  	struct list_head	list;
  	unsigned long		phys_base;
  	struct device		*dev;
  	struct clk			*iclk;
  	int					irq;
  	void __iomem		*io_base;
  
  	spinlock_t		lock;
  	int			err;
  	struct tasklet_struct	done_task;
f56809c3c   Cyrille Pitchen   crypto: atmel-sha...
135
  	struct tasklet_struct	queue_task;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
136
137
138
139
  
  	unsigned long		flags;
  	struct crypto_queue	queue;
  	struct ahash_request	*req;
a29af939b   Cyrille Pitchen   crypto: atmel-sha...
140
  	bool			is_async;
89a82ef87   Cyrille Pitchen   crypto: atmel-aut...
141
  	bool			force_complete;
b5ce82a7b   Cyrille Pitchen   crypto: atmel-sha...
142
  	atmel_sha_fn_t		resume;
eec12f66b   Cyrille Pitchen   crypto: atmel-sha...
143
  	atmel_sha_fn_t		cpu_transfer_complete;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
144
145
146
147
  
  	struct atmel_sha_dma	dma_lch_in;
  
  	struct atmel_sha_caps	caps;
81d8750b2   Cyrille Pitchen   crypto: atmel-sha...
148
  	struct scatterlist	tmp;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
149
  	u32	hw_version;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
150
151
152
153
154
155
156
157
158
159
160
  };
  
  struct atmel_sha_drv {
  	struct list_head	dev_list;
  	spinlock_t		lock;
  };
  
  static struct atmel_sha_drv atmel_sha = {
  	.dev_list = LIST_HEAD_INIT(atmel_sha.dev_list),
  	.lock = __SPIN_LOCK_UNLOCKED(atmel_sha.lock),
  };
0569fc46f   Cyrille Pitchen   crypto: atmel-sha...
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
  #ifdef VERBOSE_DEBUG
  static const char *atmel_sha_reg_name(u32 offset, char *tmp, size_t sz, bool wr)
  {
  	switch (offset) {
  	case SHA_CR:
  		return "CR";
  
  	case SHA_MR:
  		return "MR";
  
  	case SHA_IER:
  		return "IER";
  
  	case SHA_IDR:
  		return "IDR";
  
  	case SHA_IMR:
  		return "IMR";
  
  	case SHA_ISR:
  		return "ISR";
  
  	case SHA_MSR:
  		return "MSR";
  
  	case SHA_BCR:
  		return "BCR";
  
  	case SHA_REG_DIN(0):
  	case SHA_REG_DIN(1):
  	case SHA_REG_DIN(2):
  	case SHA_REG_DIN(3):
  	case SHA_REG_DIN(4):
  	case SHA_REG_DIN(5):
  	case SHA_REG_DIN(6):
  	case SHA_REG_DIN(7):
  	case SHA_REG_DIN(8):
  	case SHA_REG_DIN(9):
  	case SHA_REG_DIN(10):
  	case SHA_REG_DIN(11):
  	case SHA_REG_DIN(12):
  	case SHA_REG_DIN(13):
  	case SHA_REG_DIN(14):
  	case SHA_REG_DIN(15):
  		snprintf(tmp, sz, "IDATAR[%u]", (offset - SHA_REG_DIN(0)) >> 2);
  		break;
  
  	case SHA_REG_DIGEST(0):
  	case SHA_REG_DIGEST(1):
  	case SHA_REG_DIGEST(2):
  	case SHA_REG_DIGEST(3):
  	case SHA_REG_DIGEST(4):
  	case SHA_REG_DIGEST(5):
  	case SHA_REG_DIGEST(6):
  	case SHA_REG_DIGEST(7):
  	case SHA_REG_DIGEST(8):
  	case SHA_REG_DIGEST(9):
  	case SHA_REG_DIGEST(10):
  	case SHA_REG_DIGEST(11):
  	case SHA_REG_DIGEST(12):
  	case SHA_REG_DIGEST(13):
  	case SHA_REG_DIGEST(14):
  	case SHA_REG_DIGEST(15):
  		if (wr)
  			snprintf(tmp, sz, "IDATAR[%u]",
  				 16u + ((offset - SHA_REG_DIGEST(0)) >> 2));
  		else
  			snprintf(tmp, sz, "ODATAR[%u]",
  				 (offset - SHA_REG_DIGEST(0)) >> 2);
  		break;
  
  	case SHA_HW_VERSION:
  		return "HWVER";
  
  	default:
  		snprintf(tmp, sz, "0x%02x", offset);
  		break;
  	}
  
  	return tmp;
  }
  
  #endif /* VERBOSE_DEBUG */
ebc82efa1   Nicolas Royer   crypto: atmel - a...
244
245
  static inline u32 atmel_sha_read(struct atmel_sha_dev *dd, u32 offset)
  {
0569fc46f   Cyrille Pitchen   crypto: atmel-sha...
246
247
248
249
250
251
252
253
254
255
256
257
258
  	u32 value = readl_relaxed(dd->io_base + offset);
  
  #ifdef VERBOSE_DEBUG
  	if (dd->flags & SHA_FLAGS_DUMP_REG) {
  		char tmp[16];
  
  		dev_vdbg(dd->dev, "read 0x%08x from %s
  ", value,
  			 atmel_sha_reg_name(offset, tmp, sizeof(tmp), false));
  	}
  #endif /* VERBOSE_DEBUG */
  
  	return value;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
259
260
261
262
263
  }
  
  static inline void atmel_sha_write(struct atmel_sha_dev *dd,
  					u32 offset, u32 value)
  {
0569fc46f   Cyrille Pitchen   crypto: atmel-sha...
264
265
266
267
268
269
270
271
272
  #ifdef VERBOSE_DEBUG
  	if (dd->flags & SHA_FLAGS_DUMP_REG) {
  		char tmp[16];
  
  		dev_vdbg(dd->dev, "write 0x%08x into %s
  ", value,
  			 atmel_sha_reg_name(offset, tmp, sizeof(tmp), true));
  	}
  #endif /* VERBOSE_DEBUG */
ebc82efa1   Nicolas Royer   crypto: atmel - a...
273
274
  	writel_relaxed(value, dd->io_base + offset);
  }
a29af939b   Cyrille Pitchen   crypto: atmel-sha...
275
276
277
278
279
  static inline int atmel_sha_complete(struct atmel_sha_dev *dd, int err)
  {
  	struct ahash_request *req = dd->req;
  
  	dd->flags &= ~(SHA_FLAGS_BUSY | SHA_FLAGS_FINAL | SHA_FLAGS_CPU |
0569fc46f   Cyrille Pitchen   crypto: atmel-sha...
280
281
  		       SHA_FLAGS_DMA_READY | SHA_FLAGS_OUTPUT_READY |
  		       SHA_FLAGS_DUMP_REG);
a29af939b   Cyrille Pitchen   crypto: atmel-sha...
282
283
  
  	clk_disable(dd->iclk);
89a82ef87   Cyrille Pitchen   crypto: atmel-aut...
284
  	if ((dd->is_async || dd->force_complete) && req->base.complete)
a29af939b   Cyrille Pitchen   crypto: atmel-sha...
285
286
287
288
289
290
291
  		req->base.complete(&req->base, err);
  
  	/* handle new request */
  	tasklet_schedule(&dd->queue_task);
  
  	return err;
  }
ebc82efa1   Nicolas Royer   crypto: atmel - a...
292
293
294
295
296
297
298
  static size_t atmel_sha_append_sg(struct atmel_sha_reqctx *ctx)
  {
  	size_t count;
  
  	while ((ctx->bufcnt < ctx->buflen) && ctx->total) {
  		count = min(ctx->sg->length - ctx->offset, ctx->total);
  		count = min(count, ctx->buflen - ctx->bufcnt);
803eeae8f   Leilei Zhao   crypto: atmel-sha...
299
300
301
302
303
304
305
306
307
308
309
310
311
312
  		if (count <= 0) {
  			/*
  			* Check if count <= 0 because the buffer is full or
  			* because the sg length is 0. In the latest case,
  			* check if there is another sg in the list, a 0 length
  			* sg doesn't necessarily mean the end of the sg list.
  			*/
  			if ((ctx->sg->length == 0) && !sg_is_last(ctx->sg)) {
  				ctx->sg = sg_next(ctx->sg);
  				continue;
  			} else {
  				break;
  			}
  		}
ebc82efa1   Nicolas Royer   crypto: atmel - a...
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
  
  		scatterwalk_map_and_copy(ctx->buffer + ctx->bufcnt, ctx->sg,
  			ctx->offset, count, 0);
  
  		ctx->bufcnt += count;
  		ctx->offset += count;
  		ctx->total -= count;
  
  		if (ctx->offset == ctx->sg->length) {
  			ctx->sg = sg_next(ctx->sg);
  			if (ctx->sg)
  				ctx->offset = 0;
  			else
  				ctx->total = 0;
  		}
  	}
  
  	return 0;
  }
  
  /*
d4905b38d   Nicolas Royer   crypto: atmel-sha...
334
335
336
337
338
339
   * The purpose of this padding is to ensure that the padded message is a
   * multiple of 512 bits (SHA1/SHA224/SHA256) or 1024 bits (SHA384/SHA512).
   * The bit "1" is appended at the end of the message followed by
   * "padlen-1" zero bits. Then a 64 bits block (SHA1/SHA224/SHA256) or
   * 128 bits block (SHA384/SHA512) equals to the message length in bits
   * is appended.
ebc82efa1   Nicolas Royer   crypto: atmel - a...
340
   *
d4905b38d   Nicolas Royer   crypto: atmel-sha...
341
   * For SHA1/SHA224/SHA256, padlen is calculated as followed:
ebc82efa1   Nicolas Royer   crypto: atmel - a...
342
343
   *  - if message length < 56 bytes then padlen = 56 - message length
   *  - else padlen = 64 + 56 - message length
d4905b38d   Nicolas Royer   crypto: atmel-sha...
344
345
346
347
   *
   * For SHA384/SHA512, padlen is calculated as followed:
   *  - if message length < 112 bytes then padlen = 112 - message length
   *  - else padlen = 128 + 112 - message length
ebc82efa1   Nicolas Royer   crypto: atmel - a...
348
349
350
351
   */
  static void atmel_sha_fill_padding(struct atmel_sha_reqctx *ctx, int length)
  {
  	unsigned int index, padlen;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
  	u64 bits[2];
  	u64 size[2];
  
  	size[0] = ctx->digcnt[0];
  	size[1] = ctx->digcnt[1];
  
  	size[0] += ctx->bufcnt;
  	if (size[0] < ctx->bufcnt)
  		size[1]++;
  
  	size[0] += length;
  	if (size[0]  < length)
  		size[1]++;
  
  	bits[1] = cpu_to_be64(size[0] << 3);
  	bits[0] = cpu_to_be64(size[1] << 3 | size[0] >> 61);
f07cebad6   Cyrille Pitchen   crypto: atmel-sha...
368
369
370
  	switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  	case SHA_FLAGS_SHA384:
  	case SHA_FLAGS_SHA512:
d4905b38d   Nicolas Royer   crypto: atmel-sha...
371
372
373
374
375
376
377
  		index = ctx->bufcnt & 0x7f;
  		padlen = (index < 112) ? (112 - index) : ((128+112) - index);
  		*(ctx->buffer + ctx->bufcnt) = 0x80;
  		memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1);
  		memcpy(ctx->buffer + ctx->bufcnt + padlen, bits, 16);
  		ctx->bufcnt += padlen + 16;
  		ctx->flags |= SHA_FLAGS_PAD;
f07cebad6   Cyrille Pitchen   crypto: atmel-sha...
378
379
380
  		break;
  
  	default:
d4905b38d   Nicolas Royer   crypto: atmel-sha...
381
382
383
384
385
386
387
  		index = ctx->bufcnt & 0x3f;
  		padlen = (index < 56) ? (56 - index) : ((64+56) - index);
  		*(ctx->buffer + ctx->bufcnt) = 0x80;
  		memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1);
  		memcpy(ctx->buffer + ctx->bufcnt + padlen, &bits[1], 8);
  		ctx->bufcnt += padlen + 8;
  		ctx->flags |= SHA_FLAGS_PAD;
f07cebad6   Cyrille Pitchen   crypto: atmel-sha...
388
  		break;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
389
  	}
ebc82efa1   Nicolas Royer   crypto: atmel - a...
390
  }
8340c7fd2   Cyrille Pitchen   crypto: atmel-sha...
391
  static struct atmel_sha_dev *atmel_sha_find_dev(struct atmel_sha_ctx *tctx)
ebc82efa1   Nicolas Royer   crypto: atmel - a...
392
  {
ebc82efa1   Nicolas Royer   crypto: atmel - a...
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
  	struct atmel_sha_dev *dd = NULL;
  	struct atmel_sha_dev *tmp;
  
  	spin_lock_bh(&atmel_sha.lock);
  	if (!tctx->dd) {
  		list_for_each_entry(tmp, &atmel_sha.dev_list, list) {
  			dd = tmp;
  			break;
  		}
  		tctx->dd = dd;
  	} else {
  		dd = tctx->dd;
  	}
  
  	spin_unlock_bh(&atmel_sha.lock);
8340c7fd2   Cyrille Pitchen   crypto: atmel-sha...
408
409
410
411
412
413
414
415
416
  	return dd;
  }
  
  static int atmel_sha_init(struct ahash_request *req)
  {
  	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  	struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm);
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  	struct atmel_sha_dev *dd = atmel_sha_find_dev(tctx);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
417
418
419
420
421
422
423
  	ctx->dd = dd;
  
  	ctx->flags = 0;
  
  	dev_dbg(dd->dev, "init: digest size: %d
  ",
  		crypto_ahash_digestsize(tfm));
d4905b38d   Nicolas Royer   crypto: atmel-sha...
424
425
  	switch (crypto_ahash_digestsize(tfm)) {
  	case SHA1_DIGEST_SIZE:
ebc82efa1   Nicolas Royer   crypto: atmel - a...
426
  		ctx->flags |= SHA_FLAGS_SHA1;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
427
428
429
430
431
432
433
  		ctx->block_size = SHA1_BLOCK_SIZE;
  		break;
  	case SHA224_DIGEST_SIZE:
  		ctx->flags |= SHA_FLAGS_SHA224;
  		ctx->block_size = SHA224_BLOCK_SIZE;
  		break;
  	case SHA256_DIGEST_SIZE:
ebc82efa1   Nicolas Royer   crypto: atmel - a...
434
  		ctx->flags |= SHA_FLAGS_SHA256;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
435
436
437
438
439
440
441
442
443
444
445
446
447
448
  		ctx->block_size = SHA256_BLOCK_SIZE;
  		break;
  	case SHA384_DIGEST_SIZE:
  		ctx->flags |= SHA_FLAGS_SHA384;
  		ctx->block_size = SHA384_BLOCK_SIZE;
  		break;
  	case SHA512_DIGEST_SIZE:
  		ctx->flags |= SHA_FLAGS_SHA512;
  		ctx->block_size = SHA512_BLOCK_SIZE;
  		break;
  	default:
  		return -EINVAL;
  		break;
  	}
ebc82efa1   Nicolas Royer   crypto: atmel - a...
449
450
  
  	ctx->bufcnt = 0;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
451
452
  	ctx->digcnt[0] = 0;
  	ctx->digcnt[1] = 0;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
453
454
455
456
457
458
459
460
  	ctx->buflen = SHA_BUFFER_LEN;
  
  	return 0;
  }
  
  static void atmel_sha_write_ctrl(struct atmel_sha_dev *dd, int dma)
  {
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
7cee35081   Cyrille Pitchen   crypto: atmel-sha...
461
462
  	u32 valmr = SHA_MR_MODE_AUTO;
  	unsigned int i, hashsize = 0;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
463
464
  
  	if (likely(dma)) {
d4905b38d   Nicolas Royer   crypto: atmel-sha...
465
466
  		if (!dd->caps.has_dma)
  			atmel_sha_write(dd, SHA_IER, SHA_INT_TXBUFE);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
467
  		valmr = SHA_MR_MODE_PDC;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
468
469
  		if (dd->caps.has_dualbuff)
  			valmr |= SHA_MR_DUALBUFF;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
470
471
472
  	} else {
  		atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
  	}
7cee35081   Cyrille Pitchen   crypto: atmel-sha...
473
474
  	switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  	case SHA_FLAGS_SHA1:
d4905b38d   Nicolas Royer   crypto: atmel-sha...
475
  		valmr |= SHA_MR_ALGO_SHA1;
7cee35081   Cyrille Pitchen   crypto: atmel-sha...
476
477
478
479
  		hashsize = SHA1_DIGEST_SIZE;
  		break;
  
  	case SHA_FLAGS_SHA224:
d4905b38d   Nicolas Royer   crypto: atmel-sha...
480
  		valmr |= SHA_MR_ALGO_SHA224;
7cee35081   Cyrille Pitchen   crypto: atmel-sha...
481
482
483
484
  		hashsize = SHA256_DIGEST_SIZE;
  		break;
  
  	case SHA_FLAGS_SHA256:
ebc82efa1   Nicolas Royer   crypto: atmel - a...
485
  		valmr |= SHA_MR_ALGO_SHA256;
7cee35081   Cyrille Pitchen   crypto: atmel-sha...
486
487
488
489
  		hashsize = SHA256_DIGEST_SIZE;
  		break;
  
  	case SHA_FLAGS_SHA384:
d4905b38d   Nicolas Royer   crypto: atmel-sha...
490
  		valmr |= SHA_MR_ALGO_SHA384;
7cee35081   Cyrille Pitchen   crypto: atmel-sha...
491
492
493
494
  		hashsize = SHA512_DIGEST_SIZE;
  		break;
  
  	case SHA_FLAGS_SHA512:
d4905b38d   Nicolas Royer   crypto: atmel-sha...
495
  		valmr |= SHA_MR_ALGO_SHA512;
7cee35081   Cyrille Pitchen   crypto: atmel-sha...
496
497
498
499
500
501
  		hashsize = SHA512_DIGEST_SIZE;
  		break;
  
  	default:
  		break;
  	}
ebc82efa1   Nicolas Royer   crypto: atmel - a...
502
503
  
  	/* Setting CR_FIRST only for the first iteration */
7cee35081   Cyrille Pitchen   crypto: atmel-sha...
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
  	if (!(ctx->digcnt[0] || ctx->digcnt[1])) {
  		atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  	} else if (dd->caps.has_uihv && (ctx->flags & SHA_FLAGS_RESTORE)) {
  		const u32 *hash = (const u32 *)ctx->digest;
  
  		/*
  		 * Restore the hardware context: update the User Initialize
  		 * Hash Value (UIHV) with the value saved when the latest
  		 * 'update' operation completed on this very same crypto
  		 * request.
  		 */
  		ctx->flags &= ~SHA_FLAGS_RESTORE;
  		atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
  		for (i = 0; i < hashsize / sizeof(u32); ++i)
  			atmel_sha_write(dd, SHA_REG_DIN(i), hash[i]);
  		atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  		valmr |= SHA_MR_UIHV;
  	}
  	/*
  	 * WARNING: If the UIHV feature is not available, the hardware CANNOT
  	 * process concurrent requests: the internal registers used to store
  	 * the hash/digest are still set to the partial digest output values
  	 * computed during the latest round.
  	 */
ebc82efa1   Nicolas Royer   crypto: atmel - a...
528

ebc82efa1   Nicolas Royer   crypto: atmel - a...
529
530
  	atmel_sha_write(dd, SHA_MR, valmr);
  }
9064ed926   Cyrille Pitchen   crypto: atmel-sha...
531
532
533
534
535
536
537
538
539
540
541
542
  static inline int atmel_sha_wait_for_data_ready(struct atmel_sha_dev *dd,
  						atmel_sha_fn_t resume)
  {
  	u32 isr = atmel_sha_read(dd, SHA_ISR);
  
  	if (unlikely(isr & SHA_INT_DATARDY))
  		return resume(dd);
  
  	dd->resume = resume;
  	atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
  	return -EINPROGRESS;
  }
ebc82efa1   Nicolas Royer   crypto: atmel - a...
543
544
545
546
547
548
  static int atmel_sha_xmit_cpu(struct atmel_sha_dev *dd, const u8 *buf,
  			      size_t length, int final)
  {
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  	int count, len32;
  	const u32 *buffer = (const u32 *)buf;
4c147bcff   Arnd Bergmann   crypto: atmel - f...
549
550
  	dev_dbg(dd->dev, "xmit_cpu: digcnt: 0x%llx 0x%llx, length: %zd, final: %d
  ",
d4905b38d   Nicolas Royer   crypto: atmel-sha...
551
  		ctx->digcnt[1], ctx->digcnt[0], length, final);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
552
553
554
555
  
  	atmel_sha_write_ctrl(dd, 0);
  
  	/* should be non-zero before next lines to disable clocks later */
d4905b38d   Nicolas Royer   crypto: atmel-sha...
556
557
558
  	ctx->digcnt[0] += length;
  	if (ctx->digcnt[0] < length)
  		ctx->digcnt[1]++;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
  
  	if (final)
  		dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
  
  	len32 = DIV_ROUND_UP(length, sizeof(u32));
  
  	dd->flags |= SHA_FLAGS_CPU;
  
  	for (count = 0; count < len32; count++)
  		atmel_sha_write(dd, SHA_REG_DIN(count), buffer[count]);
  
  	return -EINPROGRESS;
  }
  
  static int atmel_sha_xmit_pdc(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
  		size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
  {
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  	int len32;
4c147bcff   Arnd Bergmann   crypto: atmel - f...
578
579
  	dev_dbg(dd->dev, "xmit_pdc: digcnt: 0x%llx 0x%llx, length: %zd, final: %d
  ",
d4905b38d   Nicolas Royer   crypto: atmel-sha...
580
  		ctx->digcnt[1], ctx->digcnt[0], length1, final);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
581
582
583
584
585
586
587
588
589
590
591
592
593
  
  	len32 = DIV_ROUND_UP(length1, sizeof(u32));
  	atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTDIS);
  	atmel_sha_write(dd, SHA_TPR, dma_addr1);
  	atmel_sha_write(dd, SHA_TCR, len32);
  
  	len32 = DIV_ROUND_UP(length2, sizeof(u32));
  	atmel_sha_write(dd, SHA_TNPR, dma_addr2);
  	atmel_sha_write(dd, SHA_TNCR, len32);
  
  	atmel_sha_write_ctrl(dd, 1);
  
  	/* should be non-zero before next lines to disable clocks later */
d4905b38d   Nicolas Royer   crypto: atmel-sha...
594
595
596
  	ctx->digcnt[0] += length1;
  	if (ctx->digcnt[0] < length1)
  		ctx->digcnt[1]++;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
597
598
599
600
601
602
603
604
605
606
607
  
  	if (final)
  		dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
  
  	dd->flags |=  SHA_FLAGS_DMA_ACTIVE;
  
  	/* Start DMA transfer */
  	atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTEN);
  
  	return -EINPROGRESS;
  }
d4905b38d   Nicolas Royer   crypto: atmel-sha...
608
609
610
  static void atmel_sha_dma_callback(void *data)
  {
  	struct atmel_sha_dev *dd = data;
a29af939b   Cyrille Pitchen   crypto: atmel-sha...
611
  	dd->is_async = true;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
612
613
614
615
616
617
618
619
620
621
  	/* dma_lch_in - completed - wait DATRDY */
  	atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
  }
  
  static int atmel_sha_xmit_dma(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
  		size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
  {
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  	struct dma_async_tx_descriptor	*in_desc;
  	struct scatterlist sg[2];
4c147bcff   Arnd Bergmann   crypto: atmel - f...
622
623
  	dev_dbg(dd->dev, "xmit_dma: digcnt: 0x%llx 0x%llx, length: %zd, final: %d
  ",
d4905b38d   Nicolas Royer   crypto: atmel-sha...
624
  		ctx->digcnt[1], ctx->digcnt[0], length1, final);
3f1992c00   Leilei Zhao   crypto: atmel-sha...
625
626
  	dd->dma_lch_in.dma_conf.src_maxburst = 16;
  	dd->dma_lch_in.dma_conf.dst_maxburst = 16;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
  
  	dmaengine_slave_config(dd->dma_lch_in.chan, &dd->dma_lch_in.dma_conf);
  
  	if (length2) {
  		sg_init_table(sg, 2);
  		sg_dma_address(&sg[0]) = dma_addr1;
  		sg_dma_len(&sg[0]) = length1;
  		sg_dma_address(&sg[1]) = dma_addr2;
  		sg_dma_len(&sg[1]) = length2;
  		in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 2,
  			DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  	} else {
  		sg_init_table(sg, 1);
  		sg_dma_address(&sg[0]) = dma_addr1;
  		sg_dma_len(&sg[0]) = length1;
  		in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 1,
  			DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  	}
  	if (!in_desc)
dd3f9f40b   Cyrille Pitchen   crypto: atmel-sha...
646
  		return atmel_sha_complete(dd, -EINVAL);
d4905b38d   Nicolas Royer   crypto: atmel-sha...
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
  
  	in_desc->callback = atmel_sha_dma_callback;
  	in_desc->callback_param = dd;
  
  	atmel_sha_write_ctrl(dd, 1);
  
  	/* should be non-zero before next lines to disable clocks later */
  	ctx->digcnt[0] += length1;
  	if (ctx->digcnt[0] < length1)
  		ctx->digcnt[1]++;
  
  	if (final)
  		dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
  
  	dd->flags |=  SHA_FLAGS_DMA_ACTIVE;
  
  	/* Start DMA transfer */
  	dmaengine_submit(in_desc);
  	dma_async_issue_pending(dd->dma_lch_in.chan);
  
  	return -EINPROGRESS;
  }
  
  static int atmel_sha_xmit_start(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
  		size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
  {
  	if (dd->caps.has_dma)
  		return atmel_sha_xmit_dma(dd, dma_addr1, length1,
  				dma_addr2, length2, final);
  	else
  		return atmel_sha_xmit_pdc(dd, dma_addr1, length1,
  				dma_addr2, length2, final);
  }
ebc82efa1   Nicolas Royer   crypto: atmel - a...
680
681
682
683
684
685
686
  static int atmel_sha_update_cpu(struct atmel_sha_dev *dd)
  {
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  	int bufcnt;
  
  	atmel_sha_append_sg(ctx);
  	atmel_sha_fill_padding(ctx, 0);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
687
688
689
690
691
692
693
694
695
696
697
  	bufcnt = ctx->bufcnt;
  	ctx->bufcnt = 0;
  
  	return atmel_sha_xmit_cpu(dd, ctx->buffer, bufcnt, 1);
  }
  
  static int atmel_sha_xmit_dma_map(struct atmel_sha_dev *dd,
  					struct atmel_sha_reqctx *ctx,
  					size_t length, int final)
  {
  	ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
d4905b38d   Nicolas Royer   crypto: atmel-sha...
698
  				ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
699
  	if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
4c147bcff   Arnd Bergmann   crypto: atmel - f...
700
701
  		dev_err(dd->dev, "dma %zu bytes error
  ", ctx->buflen +
d4905b38d   Nicolas Royer   crypto: atmel-sha...
702
  				ctx->block_size);
dd3f9f40b   Cyrille Pitchen   crypto: atmel-sha...
703
  		return atmel_sha_complete(dd, -EINVAL);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
704
705
706
707
708
  	}
  
  	ctx->flags &= ~SHA_FLAGS_SG;
  
  	/* next call does not fail... so no unmap in the case of error */
d4905b38d   Nicolas Royer   crypto: atmel-sha...
709
  	return atmel_sha_xmit_start(dd, ctx->dma_addr, length, 0, 0, final);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
710
711
712
713
714
715
716
717
718
719
720
  }
  
  static int atmel_sha_update_dma_slow(struct atmel_sha_dev *dd)
  {
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  	unsigned int final;
  	size_t count;
  
  	atmel_sha_append_sg(ctx);
  
  	final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
4c147bcff   Arnd Bergmann   crypto: atmel - f...
721
722
  	dev_dbg(dd->dev, "slow: bufcnt: %zu, digcnt: 0x%llx 0x%llx, final: %d
  ",
d4905b38d   Nicolas Royer   crypto: atmel-sha...
723
  		 ctx->bufcnt, ctx->digcnt[1], ctx->digcnt[0], final);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
724
725
726
  
  	if (final)
  		atmel_sha_fill_padding(ctx, 0);
0099286b6   Ludovic Desroches   crypto: atmel-sha...
727
  	if (final || (ctx->bufcnt == ctx->buflen)) {
ebc82efa1   Nicolas Royer   crypto: atmel - a...
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
  		count = ctx->bufcnt;
  		ctx->bufcnt = 0;
  		return atmel_sha_xmit_dma_map(dd, ctx, count, final);
  	}
  
  	return 0;
  }
  
  static int atmel_sha_update_dma_start(struct atmel_sha_dev *dd)
  {
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  	unsigned int length, final, tail;
  	struct scatterlist *sg;
  	unsigned int count;
  
  	if (!ctx->total)
  		return 0;
  
  	if (ctx->bufcnt || ctx->offset)
  		return atmel_sha_update_dma_slow(dd);
4c147bcff   Arnd Bergmann   crypto: atmel - f...
748
749
  	dev_dbg(dd->dev, "fast: digcnt: 0x%llx 0x%llx, bufcnt: %zd, total: %u
  ",
d4905b38d   Nicolas Royer   crypto: atmel-sha...
750
  		ctx->digcnt[1], ctx->digcnt[0], ctx->bufcnt, ctx->total);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
751
752
753
754
755
  
  	sg = ctx->sg;
  
  	if (!IS_ALIGNED(sg->offset, sizeof(u32)))
  		return atmel_sha_update_dma_slow(dd);
d4905b38d   Nicolas Royer   crypto: atmel-sha...
756
757
  	if (!sg_is_last(sg) && !IS_ALIGNED(sg->length, ctx->block_size))
  		/* size is not ctx->block_size aligned */
ebc82efa1   Nicolas Royer   crypto: atmel - a...
758
759
760
761
762
763
  		return atmel_sha_update_dma_slow(dd);
  
  	length = min(ctx->total, sg->length);
  
  	if (sg_is_last(sg)) {
  		if (!(ctx->flags & SHA_FLAGS_FINUP)) {
d4905b38d   Nicolas Royer   crypto: atmel-sha...
764
765
  			/* not last sg must be ctx->block_size aligned */
  			tail = length & (ctx->block_size - 1);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
766
  			length -= tail;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
767
768
769
770
771
772
773
774
775
776
  		}
  	}
  
  	ctx->total -= length;
  	ctx->offset = length; /* offset where to start slow */
  
  	final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
  
  	/* Add padding */
  	if (final) {
d4905b38d   Nicolas Royer   crypto: atmel-sha...
777
  		tail = length & (ctx->block_size - 1);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
778
779
780
781
782
783
784
785
786
787
  		length -= tail;
  		ctx->total += tail;
  		ctx->offset = length; /* offset where to start slow */
  
  		sg = ctx->sg;
  		atmel_sha_append_sg(ctx);
  
  		atmel_sha_fill_padding(ctx, length);
  
  		ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
d4905b38d   Nicolas Royer   crypto: atmel-sha...
788
  			ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
789
  		if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
4c147bcff   Arnd Bergmann   crypto: atmel - f...
790
791
  			dev_err(dd->dev, "dma %zu bytes error
  ",
d4905b38d   Nicolas Royer   crypto: atmel-sha...
792
  				ctx->buflen + ctx->block_size);
dd3f9f40b   Cyrille Pitchen   crypto: atmel-sha...
793
  			return atmel_sha_complete(dd, -EINVAL);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
794
795
796
797
798
799
  		}
  
  		if (length == 0) {
  			ctx->flags &= ~SHA_FLAGS_SG;
  			count = ctx->bufcnt;
  			ctx->bufcnt = 0;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
800
  			return atmel_sha_xmit_start(dd, ctx->dma_addr, count, 0,
ebc82efa1   Nicolas Royer   crypto: atmel - a...
801
802
803
804
805
806
807
  					0, final);
  		} else {
  			ctx->sg = sg;
  			if (!dma_map_sg(dd->dev, ctx->sg, 1,
  				DMA_TO_DEVICE)) {
  					dev_err(dd->dev, "dma_map_sg  error
  ");
dd3f9f40b   Cyrille Pitchen   crypto: atmel-sha...
808
  					return atmel_sha_complete(dd, -EINVAL);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
809
810
811
812
813
814
  			}
  
  			ctx->flags |= SHA_FLAGS_SG;
  
  			count = ctx->bufcnt;
  			ctx->bufcnt = 0;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
815
  			return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg),
ebc82efa1   Nicolas Royer   crypto: atmel - a...
816
817
818
819
820
821
822
  					length, ctx->dma_addr, count, final);
  		}
  	}
  
  	if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
  		dev_err(dd->dev, "dma_map_sg  error
  ");
dd3f9f40b   Cyrille Pitchen   crypto: atmel-sha...
823
  		return atmel_sha_complete(dd, -EINVAL);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
824
825
826
827
828
  	}
  
  	ctx->flags |= SHA_FLAGS_SG;
  
  	/* next call does not fail... so no unmap in the case of error */
d4905b38d   Nicolas Royer   crypto: atmel-sha...
829
  	return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg), length, 0,
ebc82efa1   Nicolas Royer   crypto: atmel - a...
830
831
832
833
834
835
836
837
838
839
840
841
842
843
  								0, final);
  }
  
  static int atmel_sha_update_dma_stop(struct atmel_sha_dev *dd)
  {
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  
  	if (ctx->flags & SHA_FLAGS_SG) {
  		dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE);
  		if (ctx->sg->length == ctx->offset) {
  			ctx->sg = sg_next(ctx->sg);
  			if (ctx->sg)
  				ctx->offset = 0;
  		}
d4905b38d   Nicolas Royer   crypto: atmel-sha...
844
  		if (ctx->flags & SHA_FLAGS_PAD) {
ebc82efa1   Nicolas Royer   crypto: atmel - a...
845
  			dma_unmap_single(dd->dev, ctx->dma_addr,
d4905b38d   Nicolas Royer   crypto: atmel-sha...
846
847
  				ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
  		}
ebc82efa1   Nicolas Royer   crypto: atmel - a...
848
849
  	} else {
  		dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen +
d4905b38d   Nicolas Royer   crypto: atmel-sha...
850
  						ctx->block_size, DMA_TO_DEVICE);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
851
852
853
854
855
856
857
858
859
860
  	}
  
  	return 0;
  }
  
  static int atmel_sha_update_req(struct atmel_sha_dev *dd)
  {
  	struct ahash_request *req = dd->req;
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  	int err;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
861
862
863
  	dev_dbg(dd->dev, "update_req: total: %u, digcnt: 0x%llx 0x%llx
  ",
  		ctx->total, ctx->digcnt[1], ctx->digcnt[0]);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
864
865
866
867
868
869
870
  
  	if (ctx->flags & SHA_FLAGS_CPU)
  		err = atmel_sha_update_cpu(dd);
  	else
  		err = atmel_sha_update_dma_start(dd);
  
  	/* wait for dma completion before can take more data */
d4905b38d   Nicolas Royer   crypto: atmel-sha...
871
872
873
  	dev_dbg(dd->dev, "update: err: %d, digcnt: 0x%llx 0%llx
  ",
  			err, ctx->digcnt[1], ctx->digcnt[0]);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
  
  	return err;
  }
  
  static int atmel_sha_final_req(struct atmel_sha_dev *dd)
  {
  	struct ahash_request *req = dd->req;
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  	int err = 0;
  	int count;
  
  	if (ctx->bufcnt >= ATMEL_SHA_DMA_THRESHOLD) {
  		atmel_sha_fill_padding(ctx, 0);
  		count = ctx->bufcnt;
  		ctx->bufcnt = 0;
  		err = atmel_sha_xmit_dma_map(dd, ctx, count, 1);
  	}
  	/* faster to handle last block with cpu */
  	else {
  		atmel_sha_fill_padding(ctx, 0);
  		count = ctx->bufcnt;
  		ctx->bufcnt = 0;
  		err = atmel_sha_xmit_cpu(dd, ctx->buffer, count, 1);
  	}
  
  	dev_dbg(dd->dev, "final_req: err: %d
  ", err);
  
  	return err;
  }
  
  static void atmel_sha_copy_hash(struct ahash_request *req)
  {
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  	u32 *hash = (u32 *)ctx->digest;
7cee35081   Cyrille Pitchen   crypto: atmel-sha...
909
  	unsigned int i, hashsize;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
910

7cee35081   Cyrille Pitchen   crypto: atmel-sha...
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
  	switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  	case SHA_FLAGS_SHA1:
  		hashsize = SHA1_DIGEST_SIZE;
  		break;
  
  	case SHA_FLAGS_SHA224:
  	case SHA_FLAGS_SHA256:
  		hashsize = SHA256_DIGEST_SIZE;
  		break;
  
  	case SHA_FLAGS_SHA384:
  	case SHA_FLAGS_SHA512:
  		hashsize = SHA512_DIGEST_SIZE;
  		break;
  
  	default:
  		/* Should not happen... */
  		return;
  	}
  
  	for (i = 0; i < hashsize / sizeof(u32); ++i)
  		hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i));
  	ctx->flags |= SHA_FLAGS_RESTORE;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
934
935
936
937
938
939
940
941
  }
  
  static void atmel_sha_copy_ready_hash(struct ahash_request *req)
  {
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  
  	if (!req->result)
  		return;
f07cebad6   Cyrille Pitchen   crypto: atmel-sha...
942
943
944
  	switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  	default:
  	case SHA_FLAGS_SHA1:
ebc82efa1   Nicolas Royer   crypto: atmel - a...
945
  		memcpy(req->result, ctx->digest, SHA1_DIGEST_SIZE);
f07cebad6   Cyrille Pitchen   crypto: atmel-sha...
946
947
948
  		break;
  
  	case SHA_FLAGS_SHA224:
d4905b38d   Nicolas Royer   crypto: atmel-sha...
949
  		memcpy(req->result, ctx->digest, SHA224_DIGEST_SIZE);
f07cebad6   Cyrille Pitchen   crypto: atmel-sha...
950
951
952
  		break;
  
  	case SHA_FLAGS_SHA256:
ebc82efa1   Nicolas Royer   crypto: atmel - a...
953
  		memcpy(req->result, ctx->digest, SHA256_DIGEST_SIZE);
f07cebad6   Cyrille Pitchen   crypto: atmel-sha...
954
955
956
  		break;
  
  	case SHA_FLAGS_SHA384:
d4905b38d   Nicolas Royer   crypto: atmel-sha...
957
  		memcpy(req->result, ctx->digest, SHA384_DIGEST_SIZE);
f07cebad6   Cyrille Pitchen   crypto: atmel-sha...
958
959
960
  		break;
  
  	case SHA_FLAGS_SHA512:
d4905b38d   Nicolas Royer   crypto: atmel-sha...
961
  		memcpy(req->result, ctx->digest, SHA512_DIGEST_SIZE);
f07cebad6   Cyrille Pitchen   crypto: atmel-sha...
962
963
  		break;
  	}
ebc82efa1   Nicolas Royer   crypto: atmel - a...
964
965
966
967
968
969
  }
  
  static int atmel_sha_finish(struct ahash_request *req)
  {
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  	struct atmel_sha_dev *dd = ctx->dd;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
970

d4905b38d   Nicolas Royer   crypto: atmel-sha...
971
  	if (ctx->digcnt[0] || ctx->digcnt[1])
ebc82efa1   Nicolas Royer   crypto: atmel - a...
972
  		atmel_sha_copy_ready_hash(req);
4c147bcff   Arnd Bergmann   crypto: atmel - f...
973
974
  	dev_dbg(dd->dev, "digcnt: 0x%llx 0x%llx, bufcnt: %zd
  ", ctx->digcnt[1],
d4905b38d   Nicolas Royer   crypto: atmel-sha...
975
  		ctx->digcnt[0], ctx->bufcnt);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
976

871b88a84   Rahul Pathak   crypto: atmel-sha...
977
  	return 0;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
  }
  
  static void atmel_sha_finish_req(struct ahash_request *req, int err)
  {
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  	struct atmel_sha_dev *dd = ctx->dd;
  
  	if (!err) {
  		atmel_sha_copy_hash(req);
  		if (SHA_FLAGS_FINAL & dd->flags)
  			err = atmel_sha_finish(req);
  	} else {
  		ctx->flags |= SHA_FLAGS_ERROR;
  	}
  
  	/* atomic operation is not needed here */
a29af939b   Cyrille Pitchen   crypto: atmel-sha...
994
  	(void)atmel_sha_complete(dd, err);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
995
996
997
998
  }
  
  static int atmel_sha_hw_init(struct atmel_sha_dev *dd)
  {
9d83d2995   LABBE Corentin   crypto: atmel - C...
999
  	int err;
c033042aa   Cyrille Pitchen   crypto: atmel-sha...
1000
  	err = clk_enable(dd->iclk);
9d83d2995   LABBE Corentin   crypto: atmel - C...
1001
1002
  	if (err)
  		return err;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1003

d4905b38d   Nicolas Royer   crypto: atmel-sha...
1004
  	if (!(SHA_FLAGS_INIT & dd->flags)) {
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1005
  		atmel_sha_write(dd, SHA_CR, SHA_CR_SWRST);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1006
1007
1008
1009
1010
1011
  		dd->flags |= SHA_FLAGS_INIT;
  		dd->err = 0;
  	}
  
  	return 0;
  }
d4905b38d   Nicolas Royer   crypto: atmel-sha...
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
  static inline unsigned int atmel_sha_get_version(struct atmel_sha_dev *dd)
  {
  	return atmel_sha_read(dd, SHA_HW_VERSION) & 0x00000fff;
  }
  
  static void atmel_sha_hw_version_init(struct atmel_sha_dev *dd)
  {
  	atmel_sha_hw_init(dd);
  
  	dd->hw_version = atmel_sha_get_version(dd);
  
  	dev_info(dd->dev,
  			"version: 0x%x
  ", dd->hw_version);
c033042aa   Cyrille Pitchen   crypto: atmel-sha...
1026
  	clk_disable(dd->iclk);
d4905b38d   Nicolas Royer   crypto: atmel-sha...
1027
  }
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1028
1029
1030
1031
  static int atmel_sha_handle_queue(struct atmel_sha_dev *dd,
  				  struct ahash_request *req)
  {
  	struct crypto_async_request *async_req, *backlog;
a29af939b   Cyrille Pitchen   crypto: atmel-sha...
1032
  	struct atmel_sha_ctx *ctx;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1033
  	unsigned long flags;
a29af939b   Cyrille Pitchen   crypto: atmel-sha...
1034
  	bool start_async;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
  	int err = 0, ret = 0;
  
  	spin_lock_irqsave(&dd->lock, flags);
  	if (req)
  		ret = ahash_enqueue_request(&dd->queue, req);
  
  	if (SHA_FLAGS_BUSY & dd->flags) {
  		spin_unlock_irqrestore(&dd->lock, flags);
  		return ret;
  	}
  
  	backlog = crypto_get_backlog(&dd->queue);
  	async_req = crypto_dequeue_request(&dd->queue);
  	if (async_req)
  		dd->flags |= SHA_FLAGS_BUSY;
  
  	spin_unlock_irqrestore(&dd->lock, flags);
  
  	if (!async_req)
  		return ret;
  
  	if (backlog)
  		backlog->complete(backlog, -EINPROGRESS);
a29af939b   Cyrille Pitchen   crypto: atmel-sha...
1058
1059
1060
1061
1062
  	ctx = crypto_tfm_ctx(async_req->tfm);
  
  	dd->req = ahash_request_cast(async_req);
  	start_async = (dd->req != req);
  	dd->is_async = start_async;
89a82ef87   Cyrille Pitchen   crypto: atmel-aut...
1063
  	dd->force_complete = false;
a29af939b   Cyrille Pitchen   crypto: atmel-sha...
1064
1065
1066
1067
1068
  
  	/* WARNING: ctx->start() MAY change dd->is_async. */
  	err = ctx->start(dd);
  	return (start_async) ? ret : err;
  }
b5ce82a7b   Cyrille Pitchen   crypto: atmel-sha...
1069
  static int atmel_sha_done(struct atmel_sha_dev *dd);
a29af939b   Cyrille Pitchen   crypto: atmel-sha...
1070
1071
1072
1073
1074
  static int atmel_sha_start(struct atmel_sha_dev *dd)
  {
  	struct ahash_request *req = dd->req;
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  	int err;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1075
1076
1077
1078
1079
1080
  
  	dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d
  ",
  						ctx->op, req->nbytes);
  
  	err = atmel_sha_hw_init(dd);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1081
  	if (err)
19998acb0   Cyrille Pitchen   crypto: atmel-sha...
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
  		return atmel_sha_complete(dd, err);
  
  	/*
  	 * atmel_sha_update_req() and atmel_sha_final_req() can return either:
  	 *  -EINPROGRESS: the hardware is busy and the SHA driver will resume
  	 *                its job later in the done_task.
  	 *                This is the main path.
  	 *
  	 * 0: the SHA driver can continue its job then release the hardware
  	 *    later, if needed, with atmel_sha_finish_req().
  	 *    This is the alternate path.
  	 *
  	 * < 0: an error has occurred so atmel_sha_complete(dd, err) has already
  	 *      been called, hence the hardware has been released.
  	 *      The SHA driver must stop its job without calling
  	 *      atmel_sha_finish_req(), otherwise atmel_sha_complete() would be
  	 *      called a second time.
  	 *
  	 * Please note that currently, atmel_sha_final_req() never returns 0.
  	 */
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1102

b5ce82a7b   Cyrille Pitchen   crypto: atmel-sha...
1103
  	dd->resume = atmel_sha_done;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1104
1105
  	if (ctx->op == SHA_OP_UPDATE) {
  		err = atmel_sha_update_req(dd);
19998acb0   Cyrille Pitchen   crypto: atmel-sha...
1106
  		if (!err && (ctx->flags & SHA_FLAGS_FINUP))
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1107
1108
  			/* no final() after finup() */
  			err = atmel_sha_final_req(dd);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1109
1110
1111
  	} else if (ctx->op == SHA_OP_FINAL) {
  		err = atmel_sha_final_req(dd);
  	}
19998acb0   Cyrille Pitchen   crypto: atmel-sha...
1112
  	if (!err)
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1113
1114
1115
1116
1117
  		/* done_task will not finish it, so do it here */
  		atmel_sha_finish_req(req, err);
  
  	dev_dbg(dd->dev, "exit, err: %d
  ", err);
a29af939b   Cyrille Pitchen   crypto: atmel-sha...
1118
  	return err;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
  }
  
  static int atmel_sha_enqueue(struct ahash_request *req, unsigned int op)
  {
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  	struct atmel_sha_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
  	struct atmel_sha_dev *dd = tctx->dd;
  
  	ctx->op = op;
  
  	return atmel_sha_handle_queue(dd, req);
  }
  
  static int atmel_sha_update(struct ahash_request *req)
  {
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  
  	if (!req->nbytes)
  		return 0;
  
  	ctx->total = req->nbytes;
  	ctx->sg = req->src;
  	ctx->offset = 0;
  
  	if (ctx->flags & SHA_FLAGS_FINUP) {
  		if (ctx->bufcnt + ctx->total < ATMEL_SHA_DMA_THRESHOLD)
  			/* faster to use CPU for short transfers */
  			ctx->flags |= SHA_FLAGS_CPU;
  	} else if (ctx->bufcnt + ctx->total < ctx->buflen) {
  		atmel_sha_append_sg(ctx);
  		return 0;
  	}
  	return atmel_sha_enqueue(req, SHA_OP_UPDATE);
  }
  
  static int atmel_sha_final(struct ahash_request *req)
  {
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1157
1158
1159
1160
1161
  
  	ctx->flags |= SHA_FLAGS_FINUP;
  
  	if (ctx->flags & SHA_FLAGS_ERROR)
  		return 0; /* uncompleted hash is not needed */
ad84112a1   Cyrille Pitchen   crypto: atmel-sha...
1162
  	if (ctx->flags & SHA_FLAGS_PAD)
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1163
1164
  		/* copy ready hash (+ finalize hmac) */
  		return atmel_sha_finish(req);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1165

ad84112a1   Cyrille Pitchen   crypto: atmel-sha...
1166
  	return atmel_sha_enqueue(req, SHA_OP_FINAL);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
  }
  
  static int atmel_sha_finup(struct ahash_request *req)
  {
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  	int err1, err2;
  
  	ctx->flags |= SHA_FLAGS_FINUP;
  
  	err1 = atmel_sha_update(req);
1606043f2   Gilad Ben-Yossef   crypto: atmel - o...
1177
1178
1179
  	if (err1 == -EINPROGRESS ||
  	    (err1 == -EBUSY && (ahash_request_flags(req) &
  				CRYPTO_TFM_REQ_MAY_BACKLOG)))
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
  		return err1;
  
  	/*
  	 * final() has to be always called to cleanup resources
  	 * even if udpate() failed, except EINPROGRESS
  	 */
  	err2 = atmel_sha_final(req);
  
  	return err1 ?: err2;
  }
  
  static int atmel_sha_digest(struct ahash_request *req)
  {
  	return atmel_sha_init(req) ?: atmel_sha_finup(req);
  }
cc831d32d   Cyrille Pitchen   crypto: atmel-sha...
1195
1196
1197
1198
  
  static int atmel_sha_export(struct ahash_request *req, void *out)
  {
  	const struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
cc831d32d   Cyrille Pitchen   crypto: atmel-sha...
1199

9c4274d90   Cyrille Pitchen   crypto: atmel-sha...
1200
  	memcpy(out, ctx, sizeof(*ctx));
cc831d32d   Cyrille Pitchen   crypto: atmel-sha...
1201
1202
1203
1204
1205
1206
  	return 0;
  }
  
  static int atmel_sha_import(struct ahash_request *req, const void *in)
  {
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
cc831d32d   Cyrille Pitchen   crypto: atmel-sha...
1207

9c4274d90   Cyrille Pitchen   crypto: atmel-sha...
1208
  	memcpy(ctx, in, sizeof(*ctx));
cc831d32d   Cyrille Pitchen   crypto: atmel-sha...
1209
1210
  	return 0;
  }
be95f0fa0   Svenning Sørensen   crypto: atmel_sha...
1211
  static int atmel_sha_cra_init(struct crypto_tfm *tfm)
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1212
  {
a29af939b   Cyrille Pitchen   crypto: atmel-sha...
1213
  	struct atmel_sha_ctx *ctx = crypto_tfm_ctx(tfm);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1214
  	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
9c4274d90   Cyrille Pitchen   crypto: atmel-sha...
1215
  				 sizeof(struct atmel_sha_reqctx));
a29af939b   Cyrille Pitchen   crypto: atmel-sha...
1216
  	ctx->start = atmel_sha_start;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1217
1218
1219
  
  	return 0;
  }
d4905b38d   Nicolas Royer   crypto: atmel-sha...
1220
  static struct ahash_alg sha_1_256_algs[] = {
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1221
1222
1223
1224
1225
1226
  {
  	.init		= atmel_sha_init,
  	.update		= atmel_sha_update,
  	.final		= atmel_sha_final,
  	.finup		= atmel_sha_finup,
  	.digest		= atmel_sha_digest,
cc831d32d   Cyrille Pitchen   crypto: atmel-sha...
1227
1228
  	.export		= atmel_sha_export,
  	.import		= atmel_sha_import,
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1229
1230
  	.halg = {
  		.digestsize	= SHA1_DIGEST_SIZE,
9c4274d90   Cyrille Pitchen   crypto: atmel-sha...
1231
  		.statesize	= sizeof(struct atmel_sha_reqctx),
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1232
1233
1234
1235
  		.base	= {
  			.cra_name		= "sha1",
  			.cra_driver_name	= "atmel-sha1",
  			.cra_priority		= 100,
be95f0fa0   Svenning Sørensen   crypto: atmel_sha...
1236
  			.cra_flags		= CRYPTO_ALG_ASYNC,
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1237
1238
1239
1240
1241
  			.cra_blocksize		= SHA1_BLOCK_SIZE,
  			.cra_ctxsize		= sizeof(struct atmel_sha_ctx),
  			.cra_alignmask		= 0,
  			.cra_module		= THIS_MODULE,
  			.cra_init		= atmel_sha_cra_init,
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1242
1243
1244
1245
1246
1247
1248
1249
1250
  		}
  	}
  },
  {
  	.init		= atmel_sha_init,
  	.update		= atmel_sha_update,
  	.final		= atmel_sha_final,
  	.finup		= atmel_sha_finup,
  	.digest		= atmel_sha_digest,
cc831d32d   Cyrille Pitchen   crypto: atmel-sha...
1251
1252
  	.export		= atmel_sha_export,
  	.import		= atmel_sha_import,
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1253
1254
  	.halg = {
  		.digestsize	= SHA256_DIGEST_SIZE,
9c4274d90   Cyrille Pitchen   crypto: atmel-sha...
1255
  		.statesize	= sizeof(struct atmel_sha_reqctx),
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1256
1257
1258
1259
  		.base	= {
  			.cra_name		= "sha256",
  			.cra_driver_name	= "atmel-sha256",
  			.cra_priority		= 100,
be95f0fa0   Svenning Sørensen   crypto: atmel_sha...
1260
  			.cra_flags		= CRYPTO_ALG_ASYNC,
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1261
1262
1263
1264
1265
  			.cra_blocksize		= SHA256_BLOCK_SIZE,
  			.cra_ctxsize		= sizeof(struct atmel_sha_ctx),
  			.cra_alignmask		= 0,
  			.cra_module		= THIS_MODULE,
  			.cra_init		= atmel_sha_cra_init,
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1266
1267
1268
1269
  		}
  	}
  },
  };
d4905b38d   Nicolas Royer   crypto: atmel-sha...
1270
1271
1272
1273
1274
1275
  static struct ahash_alg sha_224_alg = {
  	.init		= atmel_sha_init,
  	.update		= atmel_sha_update,
  	.final		= atmel_sha_final,
  	.finup		= atmel_sha_finup,
  	.digest		= atmel_sha_digest,
cc831d32d   Cyrille Pitchen   crypto: atmel-sha...
1276
1277
  	.export		= atmel_sha_export,
  	.import		= atmel_sha_import,
d4905b38d   Nicolas Royer   crypto: atmel-sha...
1278
1279
  	.halg = {
  		.digestsize	= SHA224_DIGEST_SIZE,
9c4274d90   Cyrille Pitchen   crypto: atmel-sha...
1280
  		.statesize	= sizeof(struct atmel_sha_reqctx),
d4905b38d   Nicolas Royer   crypto: atmel-sha...
1281
1282
1283
1284
  		.base	= {
  			.cra_name		= "sha224",
  			.cra_driver_name	= "atmel-sha224",
  			.cra_priority		= 100,
be95f0fa0   Svenning Sørensen   crypto: atmel_sha...
1285
  			.cra_flags		= CRYPTO_ALG_ASYNC,
d4905b38d   Nicolas Royer   crypto: atmel-sha...
1286
1287
1288
1289
1290
  			.cra_blocksize		= SHA224_BLOCK_SIZE,
  			.cra_ctxsize		= sizeof(struct atmel_sha_ctx),
  			.cra_alignmask		= 0,
  			.cra_module		= THIS_MODULE,
  			.cra_init		= atmel_sha_cra_init,
d4905b38d   Nicolas Royer   crypto: atmel-sha...
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
  		}
  	}
  };
  
  static struct ahash_alg sha_384_512_algs[] = {
  {
  	.init		= atmel_sha_init,
  	.update		= atmel_sha_update,
  	.final		= atmel_sha_final,
  	.finup		= atmel_sha_finup,
  	.digest		= atmel_sha_digest,
cc831d32d   Cyrille Pitchen   crypto: atmel-sha...
1302
1303
  	.export		= atmel_sha_export,
  	.import		= atmel_sha_import,
d4905b38d   Nicolas Royer   crypto: atmel-sha...
1304
1305
  	.halg = {
  		.digestsize	= SHA384_DIGEST_SIZE,
9c4274d90   Cyrille Pitchen   crypto: atmel-sha...
1306
  		.statesize	= sizeof(struct atmel_sha_reqctx),
d4905b38d   Nicolas Royer   crypto: atmel-sha...
1307
1308
1309
1310
  		.base	= {
  			.cra_name		= "sha384",
  			.cra_driver_name	= "atmel-sha384",
  			.cra_priority		= 100,
be95f0fa0   Svenning Sørensen   crypto: atmel_sha...
1311
  			.cra_flags		= CRYPTO_ALG_ASYNC,
d4905b38d   Nicolas Royer   crypto: atmel-sha...
1312
1313
1314
1315
1316
  			.cra_blocksize		= SHA384_BLOCK_SIZE,
  			.cra_ctxsize		= sizeof(struct atmel_sha_ctx),
  			.cra_alignmask		= 0x3,
  			.cra_module		= THIS_MODULE,
  			.cra_init		= atmel_sha_cra_init,
d4905b38d   Nicolas Royer   crypto: atmel-sha...
1317
1318
1319
1320
1321
1322
1323
1324
1325
  		}
  	}
  },
  {
  	.init		= atmel_sha_init,
  	.update		= atmel_sha_update,
  	.final		= atmel_sha_final,
  	.finup		= atmel_sha_finup,
  	.digest		= atmel_sha_digest,
cc831d32d   Cyrille Pitchen   crypto: atmel-sha...
1326
1327
  	.export		= atmel_sha_export,
  	.import		= atmel_sha_import,
d4905b38d   Nicolas Royer   crypto: atmel-sha...
1328
1329
  	.halg = {
  		.digestsize	= SHA512_DIGEST_SIZE,
9c4274d90   Cyrille Pitchen   crypto: atmel-sha...
1330
  		.statesize	= sizeof(struct atmel_sha_reqctx),
d4905b38d   Nicolas Royer   crypto: atmel-sha...
1331
1332
1333
1334
  		.base	= {
  			.cra_name		= "sha512",
  			.cra_driver_name	= "atmel-sha512",
  			.cra_priority		= 100,
be95f0fa0   Svenning Sørensen   crypto: atmel_sha...
1335
  			.cra_flags		= CRYPTO_ALG_ASYNC,
d4905b38d   Nicolas Royer   crypto: atmel-sha...
1336
1337
1338
1339
1340
  			.cra_blocksize		= SHA512_BLOCK_SIZE,
  			.cra_ctxsize		= sizeof(struct atmel_sha_ctx),
  			.cra_alignmask		= 0x3,
  			.cra_module		= THIS_MODULE,
  			.cra_init		= atmel_sha_cra_init,
d4905b38d   Nicolas Royer   crypto: atmel-sha...
1341
1342
1343
1344
  		}
  	}
  },
  };
f56809c3c   Cyrille Pitchen   crypto: atmel-sha...
1345
1346
1347
1348
1349
1350
  static void atmel_sha_queue_task(unsigned long data)
  {
  	struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
  
  	atmel_sha_handle_queue(dd, NULL);
  }
b5ce82a7b   Cyrille Pitchen   crypto: atmel-sha...
1351
  static int atmel_sha_done(struct atmel_sha_dev *dd)
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1352
  {
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1353
  	int err = 0;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
  	if (SHA_FLAGS_CPU & dd->flags) {
  		if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
  			dd->flags &= ~SHA_FLAGS_OUTPUT_READY;
  			goto finish;
  		}
  	} else if (SHA_FLAGS_DMA_READY & dd->flags) {
  		if (SHA_FLAGS_DMA_ACTIVE & dd->flags) {
  			dd->flags &= ~SHA_FLAGS_DMA_ACTIVE;
  			atmel_sha_update_dma_stop(dd);
  			if (dd->err) {
  				err = dd->err;
  				goto finish;
  			}
  		}
  		if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
  			/* hash or semi-hash ready */
  			dd->flags &= ~(SHA_FLAGS_DMA_READY |
  						SHA_FLAGS_OUTPUT_READY);
  			err = atmel_sha_update_dma_start(dd);
  			if (err != -EINPROGRESS)
  				goto finish;
  		}
  	}
b5ce82a7b   Cyrille Pitchen   crypto: atmel-sha...
1377
  	return err;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1378
1379
1380
1381
  
  finish:
  	/* finish curent request */
  	atmel_sha_finish_req(dd->req, err);
b5ce82a7b   Cyrille Pitchen   crypto: atmel-sha...
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
  
  	return err;
  }
  
  static void atmel_sha_done_task(unsigned long data)
  {
  	struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
  
  	dd->is_async = true;
  	(void)dd->resume(dd);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
  }
  
  static irqreturn_t atmel_sha_irq(int irq, void *dev_id)
  {
  	struct atmel_sha_dev *sha_dd = dev_id;
  	u32 reg;
  
  	reg = atmel_sha_read(sha_dd, SHA_ISR);
  	if (reg & atmel_sha_read(sha_dd, SHA_IMR)) {
  		atmel_sha_write(sha_dd, SHA_IDR, reg);
  		if (SHA_FLAGS_BUSY & sha_dd->flags) {
  			sha_dd->flags |= SHA_FLAGS_OUTPUT_READY;
  			if (!(SHA_FLAGS_CPU & sha_dd->flags))
  				sha_dd->flags |= SHA_FLAGS_DMA_READY;
  			tasklet_schedule(&sha_dd->done_task);
  		} else {
  			dev_warn(sha_dd->dev, "SHA interrupt when no active requests.
  ");
  		}
  		return IRQ_HANDLED;
  	}
  
  	return IRQ_NONE;
  }
eec12f66b   Cyrille Pitchen   crypto: atmel-sha...
1416

69303cf0f   Cyrille Pitchen   crypto: atmel-sha...
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
  /* DMA transfer functions */
  
  static bool atmel_sha_dma_check_aligned(struct atmel_sha_dev *dd,
  					struct scatterlist *sg,
  					size_t len)
  {
  	struct atmel_sha_dma *dma = &dd->dma_lch_in;
  	struct ahash_request *req = dd->req;
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  	size_t bs = ctx->block_size;
  	int nents;
  
  	for (nents = 0; sg; sg = sg_next(sg), ++nents) {
  		if (!IS_ALIGNED(sg->offset, sizeof(u32)))
  			return false;
  
  		/*
  		 * This is the last sg, the only one that is allowed to
  		 * have an unaligned length.
  		 */
  		if (len <= sg->length) {
  			dma->nents = nents + 1;
  			dma->last_sg_length = sg->length;
  			sg->length = ALIGN(len, sizeof(u32));
  			return true;
  		}
  
  		/* All other sg lengths MUST be aligned to the block size. */
  		if (!IS_ALIGNED(sg->length, bs))
  			return false;
  
  		len -= sg->length;
  	}
  
  	return false;
  }
  
  static void atmel_sha_dma_callback2(void *data)
  {
  	struct atmel_sha_dev *dd = data;
  	struct atmel_sha_dma *dma = &dd->dma_lch_in;
  	struct scatterlist *sg;
  	int nents;
  
  	dmaengine_terminate_all(dma->chan);
  	dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
  
  	sg = dma->sg;
  	for (nents = 0; nents < dma->nents - 1; ++nents)
  		sg = sg_next(sg);
  	sg->length = dma->last_sg_length;
  
  	dd->is_async = true;
  	(void)atmel_sha_wait_for_data_ready(dd, dd->resume);
  }
  
  static int atmel_sha_dma_start(struct atmel_sha_dev *dd,
  			       struct scatterlist *src,
  			       size_t len,
  			       atmel_sha_fn_t resume)
  {
  	struct atmel_sha_dma *dma = &dd->dma_lch_in;
  	struct dma_slave_config *config = &dma->dma_conf;
  	struct dma_chan *chan = dma->chan;
  	struct dma_async_tx_descriptor *desc;
  	dma_cookie_t cookie;
  	unsigned int sg_len;
  	int err;
  
  	dd->resume = resume;
  
  	/*
  	 * dma->nents has already been initialized by
  	 * atmel_sha_dma_check_aligned().
  	 */
  	dma->sg = src;
  	sg_len = dma_map_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
  	if (!sg_len) {
  		err = -ENOMEM;
  		goto exit;
  	}
  
  	config->src_maxburst = 16;
  	config->dst_maxburst = 16;
  	err = dmaengine_slave_config(chan, config);
  	if (err)
  		goto unmap_sg;
  
  	desc = dmaengine_prep_slave_sg(chan, dma->sg, sg_len, DMA_MEM_TO_DEV,
  				       DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  	if (!desc) {
  		err = -ENOMEM;
  		goto unmap_sg;
  	}
  
  	desc->callback = atmel_sha_dma_callback2;
  	desc->callback_param = dd;
  	cookie = dmaengine_submit(desc);
  	err = dma_submit_error(cookie);
  	if (err)
  		goto unmap_sg;
  
  	dma_async_issue_pending(chan);
  
  	return -EINPROGRESS;
  
  unmap_sg:
  	dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
  exit:
  	return atmel_sha_complete(dd, err);
  }
eec12f66b   Cyrille Pitchen   crypto: atmel-sha...
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
  /* CPU transfer functions */
  
  static int atmel_sha_cpu_transfer(struct atmel_sha_dev *dd)
  {
  	struct ahash_request *req = dd->req;
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  	const u32 *words = (const u32 *)ctx->buffer;
  	size_t i, num_words;
  	u32 isr, din, din_inc;
  
  	din_inc = (ctx->flags & SHA_FLAGS_IDATAR0) ? 0 : 1;
  	for (;;) {
  		/* Write data into the Input Data Registers. */
  		num_words = DIV_ROUND_UP(ctx->bufcnt, sizeof(u32));
  		for (i = 0, din = 0; i < num_words; ++i, din += din_inc)
  			atmel_sha_write(dd, SHA_REG_DIN(din), words[i]);
  
  		ctx->offset += ctx->bufcnt;
  		ctx->total -= ctx->bufcnt;
  
  		if (!ctx->total)
  			break;
  
  		/*
  		 * Prepare next block:
  		 * Fill ctx->buffer now with the next data to be written into
  		 * IDATARx: it gives time for the SHA hardware to process
  		 * the current data so the SHA_INT_DATARDY flag might be set
  		 * in SHA_ISR when polling this register at the beginning of
  		 * the next loop.
  		 */
  		ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total);
  		scatterwalk_map_and_copy(ctx->buffer, ctx->sg,
  					 ctx->offset, ctx->bufcnt, 0);
  
  		/* Wait for hardware to be ready again. */
  		isr = atmel_sha_read(dd, SHA_ISR);
  		if (!(isr & SHA_INT_DATARDY)) {
  			/* Not ready yet. */
  			dd->resume = atmel_sha_cpu_transfer;
  			atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
  			return -EINPROGRESS;
  		}
  	}
  
  	if (unlikely(!(ctx->flags & SHA_FLAGS_WAIT_DATARDY)))
  		return dd->cpu_transfer_complete(dd);
  
  	return atmel_sha_wait_for_data_ready(dd, dd->cpu_transfer_complete);
  }
  
  static int atmel_sha_cpu_start(struct atmel_sha_dev *dd,
  			       struct scatterlist *sg,
  			       unsigned int len,
  			       bool idatar0_only,
  			       bool wait_data_ready,
  			       atmel_sha_fn_t resume)
  {
  	struct ahash_request *req = dd->req;
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  
  	if (!len)
  		return resume(dd);
  
  	ctx->flags &= ~(SHA_FLAGS_IDATAR0 | SHA_FLAGS_WAIT_DATARDY);
  
  	if (idatar0_only)
  		ctx->flags |= SHA_FLAGS_IDATAR0;
  
  	if (wait_data_ready)
  		ctx->flags |= SHA_FLAGS_WAIT_DATARDY;
  
  	ctx->sg = sg;
  	ctx->total = len;
  	ctx->offset = 0;
  
  	/* Prepare the first block to be written. */
  	ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total);
  	scatterwalk_map_and_copy(ctx->buffer, ctx->sg,
  				 ctx->offset, ctx->bufcnt, 0);
  
  	dd->cpu_transfer_complete = resume;
  	return atmel_sha_cpu_transfer(dd);
  }
81d8750b2   Cyrille Pitchen   crypto: atmel-sha...
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
  static int atmel_sha_cpu_hash(struct atmel_sha_dev *dd,
  			      const void *data, unsigned int datalen,
  			      bool auto_padding,
  			      atmel_sha_fn_t resume)
  {
  	struct ahash_request *req = dd->req;
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  	u32 msglen = (auto_padding) ? datalen : 0;
  	u32 mr = SHA_MR_MODE_AUTO;
  
  	if (!(IS_ALIGNED(datalen, ctx->block_size) || auto_padding))
  		return atmel_sha_complete(dd, -EINVAL);
  
  	mr |= (ctx->flags & SHA_FLAGS_ALGO_MASK);
  	atmel_sha_write(dd, SHA_MR, mr);
  	atmel_sha_write(dd, SHA_MSR, msglen);
  	atmel_sha_write(dd, SHA_BCR, msglen);
  	atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  
  	sg_init_one(&dd->tmp, data, datalen);
  	return atmel_sha_cpu_start(dd, &dd->tmp, datalen, false, true, resume);
  }
  
  
  /* hmac functions */
  
  struct atmel_sha_hmac_key {
  	bool			valid;
  	unsigned int		keylen;
  	u8			buffer[SHA512_BLOCK_SIZE];
  	u8			*keydup;
  };
  
  static inline void atmel_sha_hmac_key_init(struct atmel_sha_hmac_key *hkey)
  {
  	memset(hkey, 0, sizeof(*hkey));
  }
  
  static inline void atmel_sha_hmac_key_release(struct atmel_sha_hmac_key *hkey)
  {
  	kfree(hkey->keydup);
  	memset(hkey, 0, sizeof(*hkey));
  }
  
  static inline int atmel_sha_hmac_key_set(struct atmel_sha_hmac_key *hkey,
  					 const u8 *key,
  					 unsigned int keylen)
  {
  	atmel_sha_hmac_key_release(hkey);
  
  	if (keylen > sizeof(hkey->buffer)) {
  		hkey->keydup = kmemdup(key, keylen, GFP_KERNEL);
  		if (!hkey->keydup)
  			return -ENOMEM;
  
  	} else {
  		memcpy(hkey->buffer, key, keylen);
  	}
  
  	hkey->valid = true;
  	hkey->keylen = keylen;
  	return 0;
  }
  
  static inline bool atmel_sha_hmac_key_get(const struct atmel_sha_hmac_key *hkey,
  					  const u8 **key,
  					  unsigned int *keylen)
  {
  	if (!hkey->valid)
  		return false;
  
  	*keylen = hkey->keylen;
  	*key = (hkey->keydup) ? hkey->keydup : hkey->buffer;
  	return true;
  }
  
  
  struct atmel_sha_hmac_ctx {
  	struct atmel_sha_ctx	base;
  
  	struct atmel_sha_hmac_key	hkey;
  	u32			ipad[SHA512_BLOCK_SIZE / sizeof(u32)];
  	u32			opad[SHA512_BLOCK_SIZE / sizeof(u32)];
  	atmel_sha_fn_t		resume;
  };
  
  static int atmel_sha_hmac_setup(struct atmel_sha_dev *dd,
  				atmel_sha_fn_t resume);
  static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev *dd,
  				      const u8 *key, unsigned int keylen);
  static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev *dd);
  static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev *dd);
  static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev *dd);
  static int atmel_sha_hmac_setup_done(struct atmel_sha_dev *dd);
  
  static int atmel_sha_hmac_init_done(struct atmel_sha_dev *dd);
  static int atmel_sha_hmac_final(struct atmel_sha_dev *dd);
  static int atmel_sha_hmac_final_done(struct atmel_sha_dev *dd);
  static int atmel_sha_hmac_digest2(struct atmel_sha_dev *dd);
  
  static int atmel_sha_hmac_setup(struct atmel_sha_dev *dd,
  				atmel_sha_fn_t resume)
  {
  	struct ahash_request *req = dd->req;
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  	struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  	unsigned int keylen;
  	const u8 *key;
  	size_t bs;
  
  	hmac->resume = resume;
  	switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  	case SHA_FLAGS_SHA1:
  		ctx->block_size = SHA1_BLOCK_SIZE;
  		ctx->hash_size = SHA1_DIGEST_SIZE;
  		break;
  
  	case SHA_FLAGS_SHA224:
  		ctx->block_size = SHA224_BLOCK_SIZE;
  		ctx->hash_size = SHA256_DIGEST_SIZE;
  		break;
  
  	case SHA_FLAGS_SHA256:
  		ctx->block_size = SHA256_BLOCK_SIZE;
  		ctx->hash_size = SHA256_DIGEST_SIZE;
  		break;
  
  	case SHA_FLAGS_SHA384:
  		ctx->block_size = SHA384_BLOCK_SIZE;
  		ctx->hash_size = SHA512_DIGEST_SIZE;
  		break;
  
  	case SHA_FLAGS_SHA512:
  		ctx->block_size = SHA512_BLOCK_SIZE;
  		ctx->hash_size = SHA512_DIGEST_SIZE;
  		break;
  
  	default:
  		return atmel_sha_complete(dd, -EINVAL);
  	}
  	bs = ctx->block_size;
  
  	if (likely(!atmel_sha_hmac_key_get(&hmac->hkey, &key, &keylen)))
  		return resume(dd);
  
  	/* Compute K' from K. */
  	if (unlikely(keylen > bs))
  		return atmel_sha_hmac_prehash_key(dd, key, keylen);
  
  	/* Prepare ipad. */
  	memcpy((u8 *)hmac->ipad, key, keylen);
  	memset((u8 *)hmac->ipad + keylen, 0, bs - keylen);
  	return atmel_sha_hmac_compute_ipad_hash(dd);
  }
  
  static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev *dd,
  				      const u8 *key, unsigned int keylen)
  {
  	return atmel_sha_cpu_hash(dd, key, keylen, true,
  				  atmel_sha_hmac_prehash_key_done);
  }
  
  static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev *dd)
  {
  	struct ahash_request *req = dd->req;
  	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  	struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  	size_t ds = crypto_ahash_digestsize(tfm);
  	size_t bs = ctx->block_size;
  	size_t i, num_words = ds / sizeof(u32);
  
  	/* Prepare ipad. */
  	for (i = 0; i < num_words; ++i)
  		hmac->ipad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
  	memset((u8 *)hmac->ipad + ds, 0, bs - ds);
  	return atmel_sha_hmac_compute_ipad_hash(dd);
  }
  
  static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev *dd)
  {
  	struct ahash_request *req = dd->req;
  	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  	struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  	size_t bs = ctx->block_size;
  	size_t i, num_words = bs / sizeof(u32);
  
  	memcpy(hmac->opad, hmac->ipad, bs);
  	for (i = 0; i < num_words; ++i) {
  		hmac->ipad[i] ^= 0x36363636;
  		hmac->opad[i] ^= 0x5c5c5c5c;
  	}
  
  	return atmel_sha_cpu_hash(dd, hmac->ipad, bs, false,
  				  atmel_sha_hmac_compute_opad_hash);
  }
  
  static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev *dd)
  {
  	struct ahash_request *req = dd->req;
  	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  	struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  	size_t bs = ctx->block_size;
  	size_t hs = ctx->hash_size;
  	size_t i, num_words = hs / sizeof(u32);
  
  	for (i = 0; i < num_words; ++i)
  		hmac->ipad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
  	return atmel_sha_cpu_hash(dd, hmac->opad, bs, false,
  				  atmel_sha_hmac_setup_done);
  }
  
  static int atmel_sha_hmac_setup_done(struct atmel_sha_dev *dd)
  {
  	struct ahash_request *req = dd->req;
  	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  	struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  	size_t hs = ctx->hash_size;
  	size_t i, num_words = hs / sizeof(u32);
  
  	for (i = 0; i < num_words; ++i)
  		hmac->opad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
  	atmel_sha_hmac_key_release(&hmac->hkey);
  	return hmac->resume(dd);
  }
  
  static int atmel_sha_hmac_start(struct atmel_sha_dev *dd)
  {
  	struct ahash_request *req = dd->req;
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  	int err;
  
  	err = atmel_sha_hw_init(dd);
  	if (err)
  		return atmel_sha_complete(dd, err);
  
  	switch (ctx->op) {
  	case SHA_OP_INIT:
  		err = atmel_sha_hmac_setup(dd, atmel_sha_hmac_init_done);
  		break;
  
  	case SHA_OP_UPDATE:
  		dd->resume = atmel_sha_done;
  		err = atmel_sha_update_req(dd);
  		break;
  
  	case SHA_OP_FINAL:
  		dd->resume = atmel_sha_hmac_final;
  		err = atmel_sha_final_req(dd);
  		break;
  
  	case SHA_OP_DIGEST:
  		err = atmel_sha_hmac_setup(dd, atmel_sha_hmac_digest2);
  		break;
  
  	default:
  		return atmel_sha_complete(dd, -EINVAL);
  	}
  
  	return err;
  }
  
  static int atmel_sha_hmac_setkey(struct crypto_ahash *tfm, const u8 *key,
  				 unsigned int keylen)
  {
  	struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  
  	if (atmel_sha_hmac_key_set(&hmac->hkey, key, keylen)) {
  		crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  		return -EINVAL;
  	}
  
  	return 0;
  }
  
  static int atmel_sha_hmac_init(struct ahash_request *req)
  {
  	int err;
  
  	err = atmel_sha_init(req);
  	if (err)
  		return err;
  
  	return atmel_sha_enqueue(req, SHA_OP_INIT);
  }
  
  static int atmel_sha_hmac_init_done(struct atmel_sha_dev *dd)
  {
  	struct ahash_request *req = dd->req;
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  	struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  	size_t bs = ctx->block_size;
  	size_t hs = ctx->hash_size;
  
  	ctx->bufcnt = 0;
  	ctx->digcnt[0] = bs;
  	ctx->digcnt[1] = 0;
  	ctx->flags |= SHA_FLAGS_RESTORE;
  	memcpy(ctx->digest, hmac->ipad, hs);
  	return atmel_sha_complete(dd, 0);
  }
  
  static int atmel_sha_hmac_final(struct atmel_sha_dev *dd)
  {
  	struct ahash_request *req = dd->req;
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  	struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  	u32 *digest = (u32 *)ctx->digest;
  	size_t ds = crypto_ahash_digestsize(tfm);
  	size_t bs = ctx->block_size;
  	size_t hs = ctx->hash_size;
  	size_t i, num_words;
  	u32 mr;
  
  	/* Save d = SHA((K' + ipad) | msg). */
  	num_words = ds / sizeof(u32);
  	for (i = 0; i < num_words; ++i)
  		digest[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
  
  	/* Restore context to finish computing SHA((K' + opad) | d). */
  	atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
  	num_words = hs / sizeof(u32);
  	for (i = 0; i < num_words; ++i)
  		atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
  
  	mr = SHA_MR_MODE_AUTO | SHA_MR_UIHV;
  	mr |= (ctx->flags & SHA_FLAGS_ALGO_MASK);
  	atmel_sha_write(dd, SHA_MR, mr);
  	atmel_sha_write(dd, SHA_MSR, bs + ds);
  	atmel_sha_write(dd, SHA_BCR, ds);
  	atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  
  	sg_init_one(&dd->tmp, digest, ds);
  	return atmel_sha_cpu_start(dd, &dd->tmp, ds, false, true,
  				   atmel_sha_hmac_final_done);
  }
  
  static int atmel_sha_hmac_final_done(struct atmel_sha_dev *dd)
  {
  	/*
  	 * req->result might not be sizeof(u32) aligned, so copy the
  	 * digest into ctx->digest[] before memcpy() the data into
  	 * req->result.
  	 */
  	atmel_sha_copy_hash(dd->req);
  	atmel_sha_copy_ready_hash(dd->req);
  	return atmel_sha_complete(dd, 0);
  }
  
  static int atmel_sha_hmac_digest(struct ahash_request *req)
  {
  	int err;
  
  	err = atmel_sha_init(req);
  	if (err)
  		return err;
  
  	return atmel_sha_enqueue(req, SHA_OP_DIGEST);
  }
  
  static int atmel_sha_hmac_digest2(struct atmel_sha_dev *dd)
  {
  	struct ahash_request *req = dd->req;
  	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  	struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  	size_t hs = ctx->hash_size;
  	size_t i, num_words = hs / sizeof(u32);
  	bool use_dma = false;
  	u32 mr;
  
  	/* Special case for empty message. */
  	if (!req->nbytes)
  		return atmel_sha_complete(dd, -EINVAL); // TODO:
  
  	/* Check DMA threshold and alignment. */
  	if (req->nbytes > ATMEL_SHA_DMA_THRESHOLD &&
  	    atmel_sha_dma_check_aligned(dd, req->src, req->nbytes))
  		use_dma = true;
  
  	/* Write both initial hash values to compute a HMAC. */
  	atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
  	for (i = 0; i < num_words; ++i)
  		atmel_sha_write(dd, SHA_REG_DIN(i), hmac->ipad[i]);
  
  	atmel_sha_write(dd, SHA_CR, SHA_CR_WUIEHV);
  	for (i = 0; i < num_words; ++i)
  		atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
  
  	/* Write the Mode, Message Size, Bytes Count then Control Registers. */
  	mr = (SHA_MR_HMAC | SHA_MR_DUALBUFF);
  	mr |= ctx->flags & SHA_FLAGS_ALGO_MASK;
  	if (use_dma)
  		mr |= SHA_MR_MODE_IDATAR0;
  	else
  		mr |= SHA_MR_MODE_AUTO;
  	atmel_sha_write(dd, SHA_MR, mr);
  
  	atmel_sha_write(dd, SHA_MSR, req->nbytes);
  	atmel_sha_write(dd, SHA_BCR, req->nbytes);
  
  	atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  
  	/* Process data. */
  	if (use_dma)
  		return atmel_sha_dma_start(dd, req->src, req->nbytes,
  					   atmel_sha_hmac_final_done);
  
  	return atmel_sha_cpu_start(dd, req->src, req->nbytes, false, true,
  				   atmel_sha_hmac_final_done);
  }
  
  static int atmel_sha_hmac_cra_init(struct crypto_tfm *tfm)
  {
  	struct atmel_sha_hmac_ctx *hmac = crypto_tfm_ctx(tfm);
  
  	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  				 sizeof(struct atmel_sha_reqctx));
  	hmac->base.start = atmel_sha_hmac_start;
  	atmel_sha_hmac_key_init(&hmac->hkey);
  
  	return 0;
  }
  
  static void atmel_sha_hmac_cra_exit(struct crypto_tfm *tfm)
  {
  	struct atmel_sha_hmac_ctx *hmac = crypto_tfm_ctx(tfm);
  
  	atmel_sha_hmac_key_release(&hmac->hkey);
  }
  
  static struct ahash_alg sha_hmac_algs[] = {
  {
  	.init		= atmel_sha_hmac_init,
  	.update		= atmel_sha_update,
  	.final		= atmel_sha_final,
  	.digest		= atmel_sha_hmac_digest,
  	.setkey		= atmel_sha_hmac_setkey,
  	.export		= atmel_sha_export,
  	.import		= atmel_sha_import,
  	.halg = {
  		.digestsize	= SHA1_DIGEST_SIZE,
  		.statesize	= sizeof(struct atmel_sha_reqctx),
  		.base	= {
  			.cra_name		= "hmac(sha1)",
  			.cra_driver_name	= "atmel-hmac-sha1",
  			.cra_priority		= 100,
  			.cra_flags		= CRYPTO_ALG_ASYNC,
  			.cra_blocksize		= SHA1_BLOCK_SIZE,
  			.cra_ctxsize		= sizeof(struct atmel_sha_hmac_ctx),
  			.cra_alignmask		= 0,
  			.cra_module		= THIS_MODULE,
  			.cra_init		= atmel_sha_hmac_cra_init,
  			.cra_exit		= atmel_sha_hmac_cra_exit,
  		}
  	}
  },
  {
  	.init		= atmel_sha_hmac_init,
  	.update		= atmel_sha_update,
  	.final		= atmel_sha_final,
  	.digest		= atmel_sha_hmac_digest,
  	.setkey		= atmel_sha_hmac_setkey,
  	.export		= atmel_sha_export,
  	.import		= atmel_sha_import,
  	.halg = {
  		.digestsize	= SHA224_DIGEST_SIZE,
  		.statesize	= sizeof(struct atmel_sha_reqctx),
  		.base	= {
  			.cra_name		= "hmac(sha224)",
  			.cra_driver_name	= "atmel-hmac-sha224",
  			.cra_priority		= 100,
  			.cra_flags		= CRYPTO_ALG_ASYNC,
  			.cra_blocksize		= SHA224_BLOCK_SIZE,
  			.cra_ctxsize		= sizeof(struct atmel_sha_hmac_ctx),
  			.cra_alignmask		= 0,
  			.cra_module		= THIS_MODULE,
  			.cra_init		= atmel_sha_hmac_cra_init,
  			.cra_exit		= atmel_sha_hmac_cra_exit,
  		}
  	}
  },
  {
  	.init		= atmel_sha_hmac_init,
  	.update		= atmel_sha_update,
  	.final		= atmel_sha_final,
  	.digest		= atmel_sha_hmac_digest,
  	.setkey		= atmel_sha_hmac_setkey,
  	.export		= atmel_sha_export,
  	.import		= atmel_sha_import,
  	.halg = {
  		.digestsize	= SHA256_DIGEST_SIZE,
  		.statesize	= sizeof(struct atmel_sha_reqctx),
  		.base	= {
  			.cra_name		= "hmac(sha256)",
  			.cra_driver_name	= "atmel-hmac-sha256",
  			.cra_priority		= 100,
  			.cra_flags		= CRYPTO_ALG_ASYNC,
  			.cra_blocksize		= SHA256_BLOCK_SIZE,
  			.cra_ctxsize		= sizeof(struct atmel_sha_hmac_ctx),
  			.cra_alignmask		= 0,
  			.cra_module		= THIS_MODULE,
  			.cra_init		= atmel_sha_hmac_cra_init,
  			.cra_exit		= atmel_sha_hmac_cra_exit,
  		}
  	}
  },
  {
  	.init		= atmel_sha_hmac_init,
  	.update		= atmel_sha_update,
  	.final		= atmel_sha_final,
  	.digest		= atmel_sha_hmac_digest,
  	.setkey		= atmel_sha_hmac_setkey,
  	.export		= atmel_sha_export,
  	.import		= atmel_sha_import,
  	.halg = {
  		.digestsize	= SHA384_DIGEST_SIZE,
  		.statesize	= sizeof(struct atmel_sha_reqctx),
  		.base	= {
  			.cra_name		= "hmac(sha384)",
  			.cra_driver_name	= "atmel-hmac-sha384",
  			.cra_priority		= 100,
  			.cra_flags		= CRYPTO_ALG_ASYNC,
  			.cra_blocksize		= SHA384_BLOCK_SIZE,
  			.cra_ctxsize		= sizeof(struct atmel_sha_hmac_ctx),
  			.cra_alignmask		= 0,
  			.cra_module		= THIS_MODULE,
  			.cra_init		= atmel_sha_hmac_cra_init,
  			.cra_exit		= atmel_sha_hmac_cra_exit,
  		}
  	}
  },
  {
  	.init		= atmel_sha_hmac_init,
  	.update		= atmel_sha_update,
  	.final		= atmel_sha_final,
  	.digest		= atmel_sha_hmac_digest,
  	.setkey		= atmel_sha_hmac_setkey,
  	.export		= atmel_sha_export,
  	.import		= atmel_sha_import,
  	.halg = {
  		.digestsize	= SHA512_DIGEST_SIZE,
  		.statesize	= sizeof(struct atmel_sha_reqctx),
  		.base	= {
  			.cra_name		= "hmac(sha512)",
  			.cra_driver_name	= "atmel-hmac-sha512",
  			.cra_priority		= 100,
  			.cra_flags		= CRYPTO_ALG_ASYNC,
  			.cra_blocksize		= SHA512_BLOCK_SIZE,
  			.cra_ctxsize		= sizeof(struct atmel_sha_hmac_ctx),
  			.cra_alignmask		= 0,
  			.cra_module		= THIS_MODULE,
  			.cra_init		= atmel_sha_hmac_cra_init,
  			.cra_exit		= atmel_sha_hmac_cra_exit,
  		}
  	}
  },
  };
eec12f66b   Cyrille Pitchen   crypto: atmel-sha...
2176

89a82ef87   Cyrille Pitchen   crypto: atmel-aut...
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
  #ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
  /* authenc functions */
  
  static int atmel_sha_authenc_init2(struct atmel_sha_dev *dd);
  static int atmel_sha_authenc_init_done(struct atmel_sha_dev *dd);
  static int atmel_sha_authenc_final_done(struct atmel_sha_dev *dd);
  
  
  struct atmel_sha_authenc_ctx {
  	struct crypto_ahash	*tfm;
  };
  
  struct atmel_sha_authenc_reqctx {
  	struct atmel_sha_reqctx	base;
  
  	atmel_aes_authenc_fn_t	cb;
  	struct atmel_aes_dev	*aes_dev;
  
  	/* _init() parameters. */
  	struct scatterlist	*assoc;
  	u32			assoclen;
  	u32			textlen;
  
  	/* _final() parameters. */
  	u32			*digest;
  	unsigned int		digestlen;
  };
  
  static void atmel_sha_authenc_complete(struct crypto_async_request *areq,
  				       int err)
  {
  	struct ahash_request *req = areq->data;
  	struct atmel_sha_authenc_reqctx *authctx  = ahash_request_ctx(req);
  
  	authctx->cb(authctx->aes_dev, err, authctx->base.dd->is_async);
  }
  
  static int atmel_sha_authenc_start(struct atmel_sha_dev *dd)
  {
  	struct ahash_request *req = dd->req;
  	struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  	int err;
  
  	/*
  	 * Force atmel_sha_complete() to call req->base.complete(), ie
  	 * atmel_sha_authenc_complete(), which in turn calls authctx->cb().
  	 */
  	dd->force_complete = true;
  
  	err = atmel_sha_hw_init(dd);
  	return authctx->cb(authctx->aes_dev, err, dd->is_async);
  }
  
  bool atmel_sha_authenc_is_ready(void)
  {
  	struct atmel_sha_ctx dummy;
  
  	dummy.dd = NULL;
  	return (atmel_sha_find_dev(&dummy) != NULL);
  }
  EXPORT_SYMBOL_GPL(atmel_sha_authenc_is_ready);
  
  unsigned int atmel_sha_authenc_get_reqsize(void)
  {
  	return sizeof(struct atmel_sha_authenc_reqctx);
  }
  EXPORT_SYMBOL_GPL(atmel_sha_authenc_get_reqsize);
  
  struct atmel_sha_authenc_ctx *atmel_sha_authenc_spawn(unsigned long mode)
  {
  	struct atmel_sha_authenc_ctx *auth;
  	struct crypto_ahash *tfm;
  	struct atmel_sha_ctx *tctx;
  	const char *name;
  	int err = -EINVAL;
  
  	switch (mode & SHA_FLAGS_MODE_MASK) {
  	case SHA_FLAGS_HMAC_SHA1:
  		name = "atmel-hmac-sha1";
  		break;
  
  	case SHA_FLAGS_HMAC_SHA224:
  		name = "atmel-hmac-sha224";
  		break;
  
  	case SHA_FLAGS_HMAC_SHA256:
  		name = "atmel-hmac-sha256";
  		break;
  
  	case SHA_FLAGS_HMAC_SHA384:
  		name = "atmel-hmac-sha384";
  		break;
  
  	case SHA_FLAGS_HMAC_SHA512:
  		name = "atmel-hmac-sha512";
  		break;
  
  	default:
  		goto error;
  	}
  
  	tfm = crypto_alloc_ahash(name,
  				 CRYPTO_ALG_TYPE_AHASH,
  				 CRYPTO_ALG_TYPE_AHASH_MASK);
  	if (IS_ERR(tfm)) {
  		err = PTR_ERR(tfm);
  		goto error;
  	}
  	tctx = crypto_ahash_ctx(tfm);
  	tctx->start = atmel_sha_authenc_start;
  	tctx->flags = mode;
  
  	auth = kzalloc(sizeof(*auth), GFP_KERNEL);
  	if (!auth) {
  		err = -ENOMEM;
  		goto err_free_ahash;
  	}
  	auth->tfm = tfm;
  
  	return auth;
  
  err_free_ahash:
  	crypto_free_ahash(tfm);
  error:
  	return ERR_PTR(err);
  }
  EXPORT_SYMBOL_GPL(atmel_sha_authenc_spawn);
  
  void atmel_sha_authenc_free(struct atmel_sha_authenc_ctx *auth)
  {
  	if (auth)
  		crypto_free_ahash(auth->tfm);
  	kfree(auth);
  }
  EXPORT_SYMBOL_GPL(atmel_sha_authenc_free);
  
  int atmel_sha_authenc_setkey(struct atmel_sha_authenc_ctx *auth,
  			     const u8 *key, unsigned int keylen,
  			     u32 *flags)
  {
  	struct crypto_ahash *tfm = auth->tfm;
  	int err;
  
  	crypto_ahash_clear_flags(tfm, CRYPTO_TFM_REQ_MASK);
  	crypto_ahash_set_flags(tfm, *flags & CRYPTO_TFM_REQ_MASK);
  	err = crypto_ahash_setkey(tfm, key, keylen);
  	*flags = crypto_ahash_get_flags(tfm);
  
  	return err;
  }
  EXPORT_SYMBOL_GPL(atmel_sha_authenc_setkey);
  
  int atmel_sha_authenc_schedule(struct ahash_request *req,
  			       struct atmel_sha_authenc_ctx *auth,
  			       atmel_aes_authenc_fn_t cb,
  			       struct atmel_aes_dev *aes_dev)
  {
  	struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  	struct atmel_sha_reqctx *ctx = &authctx->base;
  	struct crypto_ahash *tfm = auth->tfm;
  	struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm);
  	struct atmel_sha_dev *dd;
  
  	/* Reset request context (MUST be done first). */
  	memset(authctx, 0, sizeof(*authctx));
  
  	/* Get SHA device. */
  	dd = atmel_sha_find_dev(tctx);
  	if (!dd)
  		return cb(aes_dev, -ENODEV, false);
  
  	/* Init request context. */
  	ctx->dd = dd;
  	ctx->buflen = SHA_BUFFER_LEN;
  	authctx->cb = cb;
  	authctx->aes_dev = aes_dev;
  	ahash_request_set_tfm(req, tfm);
  	ahash_request_set_callback(req, 0, atmel_sha_authenc_complete, req);
  
  	return atmel_sha_handle_queue(dd, req);
  }
  EXPORT_SYMBOL_GPL(atmel_sha_authenc_schedule);
  
  int atmel_sha_authenc_init(struct ahash_request *req,
  			   struct scatterlist *assoc, unsigned int assoclen,
  			   unsigned int textlen,
  			   atmel_aes_authenc_fn_t cb,
  			   struct atmel_aes_dev *aes_dev)
  {
  	struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  	struct atmel_sha_reqctx *ctx = &authctx->base;
  	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  	struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  	struct atmel_sha_dev *dd = ctx->dd;
  
  	if (unlikely(!IS_ALIGNED(assoclen, sizeof(u32))))
  		return atmel_sha_complete(dd, -EINVAL);
  
  	authctx->cb = cb;
  	authctx->aes_dev = aes_dev;
  	authctx->assoc = assoc;
  	authctx->assoclen = assoclen;
  	authctx->textlen = textlen;
  
  	ctx->flags = hmac->base.flags;
  	return atmel_sha_hmac_setup(dd, atmel_sha_authenc_init2);
  }
  EXPORT_SYMBOL_GPL(atmel_sha_authenc_init);
  
  static int atmel_sha_authenc_init2(struct atmel_sha_dev *dd)
  {
  	struct ahash_request *req = dd->req;
  	struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  	struct atmel_sha_reqctx *ctx = &authctx->base;
  	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  	struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  	size_t hs = ctx->hash_size;
  	size_t i, num_words = hs / sizeof(u32);
  	u32 mr, msg_size;
  
  	atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
  	for (i = 0; i < num_words; ++i)
  		atmel_sha_write(dd, SHA_REG_DIN(i), hmac->ipad[i]);
  
  	atmel_sha_write(dd, SHA_CR, SHA_CR_WUIEHV);
  	for (i = 0; i < num_words; ++i)
  		atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
  
  	mr = (SHA_MR_MODE_IDATAR0 |
  	      SHA_MR_HMAC |
  	      SHA_MR_DUALBUFF);
  	mr |= ctx->flags & SHA_FLAGS_ALGO_MASK;
  	atmel_sha_write(dd, SHA_MR, mr);
  
  	msg_size = authctx->assoclen + authctx->textlen;
  	atmel_sha_write(dd, SHA_MSR, msg_size);
  	atmel_sha_write(dd, SHA_BCR, msg_size);
  
  	atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  
  	/* Process assoc data. */
  	return atmel_sha_cpu_start(dd, authctx->assoc, authctx->assoclen,
  				   true, false,
  				   atmel_sha_authenc_init_done);
  }
  
  static int atmel_sha_authenc_init_done(struct atmel_sha_dev *dd)
  {
  	struct ahash_request *req = dd->req;
  	struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  
  	return authctx->cb(authctx->aes_dev, 0, dd->is_async);
  }
  
  int atmel_sha_authenc_final(struct ahash_request *req,
  			    u32 *digest, unsigned int digestlen,
  			    atmel_aes_authenc_fn_t cb,
  			    struct atmel_aes_dev *aes_dev)
  {
  	struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  	struct atmel_sha_reqctx *ctx = &authctx->base;
  	struct atmel_sha_dev *dd = ctx->dd;
  
  	switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  	case SHA_FLAGS_SHA1:
  		authctx->digestlen = SHA1_DIGEST_SIZE;
  		break;
  
  	case SHA_FLAGS_SHA224:
  		authctx->digestlen = SHA224_DIGEST_SIZE;
  		break;
  
  	case SHA_FLAGS_SHA256:
  		authctx->digestlen = SHA256_DIGEST_SIZE;
  		break;
  
  	case SHA_FLAGS_SHA384:
  		authctx->digestlen = SHA384_DIGEST_SIZE;
  		break;
  
  	case SHA_FLAGS_SHA512:
  		authctx->digestlen = SHA512_DIGEST_SIZE;
  		break;
  
  	default:
  		return atmel_sha_complete(dd, -EINVAL);
  	}
  	if (authctx->digestlen > digestlen)
  		authctx->digestlen = digestlen;
  
  	authctx->cb = cb;
  	authctx->aes_dev = aes_dev;
  	authctx->digest = digest;
  	return atmel_sha_wait_for_data_ready(dd,
  					     atmel_sha_authenc_final_done);
  }
  EXPORT_SYMBOL_GPL(atmel_sha_authenc_final);
  
  static int atmel_sha_authenc_final_done(struct atmel_sha_dev *dd)
  {
  	struct ahash_request *req = dd->req;
  	struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  	size_t i, num_words = authctx->digestlen / sizeof(u32);
  
  	for (i = 0; i < num_words; ++i)
  		authctx->digest[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
  
  	return atmel_sha_complete(dd, 0);
  }
  
  void atmel_sha_authenc_abort(struct ahash_request *req)
  {
  	struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  	struct atmel_sha_reqctx *ctx = &authctx->base;
  	struct atmel_sha_dev *dd = ctx->dd;
  
  	/* Prevent atmel_sha_complete() from calling req->base.complete(). */
  	dd->is_async = false;
  	dd->force_complete = false;
  	(void)atmel_sha_complete(dd, 0);
  }
  EXPORT_SYMBOL_GPL(atmel_sha_authenc_abort);
  
  #endif /* CONFIG_CRYPTO_DEV_ATMEL_AUTHENC */
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2501
2502
2503
  static void atmel_sha_unregister_algs(struct atmel_sha_dev *dd)
  {
  	int i;
81d8750b2   Cyrille Pitchen   crypto: atmel-sha...
2504
2505
2506
  	if (dd->caps.has_hmac)
  		for (i = 0; i < ARRAY_SIZE(sha_hmac_algs); i++)
  			crypto_unregister_ahash(&sha_hmac_algs[i]);
d4905b38d   Nicolas Royer   crypto: atmel-sha...
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
  	for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++)
  		crypto_unregister_ahash(&sha_1_256_algs[i]);
  
  	if (dd->caps.has_sha224)
  		crypto_unregister_ahash(&sha_224_alg);
  
  	if (dd->caps.has_sha_384_512) {
  		for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++)
  			crypto_unregister_ahash(&sha_384_512_algs[i]);
  	}
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2517
2518
2519
2520
2521
  }
  
  static int atmel_sha_register_algs(struct atmel_sha_dev *dd)
  {
  	int err, i, j;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
2522
2523
  	for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++) {
  		err = crypto_register_ahash(&sha_1_256_algs[i]);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2524
  		if (err)
d4905b38d   Nicolas Royer   crypto: atmel-sha...
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
  			goto err_sha_1_256_algs;
  	}
  
  	if (dd->caps.has_sha224) {
  		err = crypto_register_ahash(&sha_224_alg);
  		if (err)
  			goto err_sha_224_algs;
  	}
  
  	if (dd->caps.has_sha_384_512) {
  		for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++) {
  			err = crypto_register_ahash(&sha_384_512_algs[i]);
  			if (err)
  				goto err_sha_384_512_algs;
  		}
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2540
  	}
81d8750b2   Cyrille Pitchen   crypto: atmel-sha...
2541
2542
2543
2544
2545
2546
2547
  	if (dd->caps.has_hmac) {
  		for (i = 0; i < ARRAY_SIZE(sha_hmac_algs); i++) {
  			err = crypto_register_ahash(&sha_hmac_algs[i]);
  			if (err)
  				goto err_sha_hmac_algs;
  		}
  	}
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2548
  	return 0;
81d8750b2   Cyrille Pitchen   crypto: atmel-sha...
2549
2550
2551
2552
2553
  	/*i = ARRAY_SIZE(sha_hmac_algs);*/
  err_sha_hmac_algs:
  	for (j = 0; j < i; j++)
  		crypto_unregister_ahash(&sha_hmac_algs[j]);
  	i = ARRAY_SIZE(sha_384_512_algs);
d4905b38d   Nicolas Royer   crypto: atmel-sha...
2554
2555
2556
2557
2558
2559
2560
  err_sha_384_512_algs:
  	for (j = 0; j < i; j++)
  		crypto_unregister_ahash(&sha_384_512_algs[j]);
  	crypto_unregister_ahash(&sha_224_alg);
  err_sha_224_algs:
  	i = ARRAY_SIZE(sha_1_256_algs);
  err_sha_1_256_algs:
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2561
  	for (j = 0; j < i; j++)
d4905b38d   Nicolas Royer   crypto: atmel-sha...
2562
  		crypto_unregister_ahash(&sha_1_256_algs[j]);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2563
2564
2565
  
  	return err;
  }
d4905b38d   Nicolas Royer   crypto: atmel-sha...
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
  static bool atmel_sha_filter(struct dma_chan *chan, void *slave)
  {
  	struct at_dma_slave	*sl = slave;
  
  	if (sl && sl->dma_dev == chan->device->dev) {
  		chan->private = sl;
  		return true;
  	} else {
  		return false;
  	}
  }
  
  static int atmel_sha_dma_init(struct atmel_sha_dev *dd,
  				struct crypto_platform_data *pdata)
  {
  	int err = -ENOMEM;
  	dma_cap_mask_t mask_in;
abfe7ae40   Nicolas Ferre   crypto: atmel-sha...
2583
2584
2585
  	/* Try to grab DMA channel */
  	dma_cap_zero(mask_in);
  	dma_cap_set(DMA_SLAVE, mask_in);
d4905b38d   Nicolas Royer   crypto: atmel-sha...
2586

abfe7ae40   Nicolas Ferre   crypto: atmel-sha...
2587
2588
2589
2590
2591
2592
  	dd->dma_lch_in.chan = dma_request_slave_channel_compat(mask_in,
  			atmel_sha_filter, &pdata->dma_slave->rxdata, dd->dev, "tx");
  	if (!dd->dma_lch_in.chan) {
  		dev_warn(dd->dev, "no DMA channel available
  ");
  		return err;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
2593
  	}
abfe7ae40   Nicolas Ferre   crypto: atmel-sha...
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
  	dd->dma_lch_in.dma_conf.direction = DMA_MEM_TO_DEV;
  	dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base +
  		SHA_REG_DIN(0);
  	dd->dma_lch_in.dma_conf.src_maxburst = 1;
  	dd->dma_lch_in.dma_conf.src_addr_width =
  		DMA_SLAVE_BUSWIDTH_4_BYTES;
  	dd->dma_lch_in.dma_conf.dst_maxburst = 1;
  	dd->dma_lch_in.dma_conf.dst_addr_width =
  		DMA_SLAVE_BUSWIDTH_4_BYTES;
  	dd->dma_lch_in.dma_conf.device_fc = false;
  
  	return 0;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
  }
  
  static void atmel_sha_dma_cleanup(struct atmel_sha_dev *dd)
  {
  	dma_release_channel(dd->dma_lch_in.chan);
  }
  
  static void atmel_sha_get_cap(struct atmel_sha_dev *dd)
  {
  
  	dd->caps.has_dma = 0;
  	dd->caps.has_dualbuff = 0;
  	dd->caps.has_sha224 = 0;
  	dd->caps.has_sha_384_512 = 0;
7cee35081   Cyrille Pitchen   crypto: atmel-sha...
2620
  	dd->caps.has_uihv = 0;
81d8750b2   Cyrille Pitchen   crypto: atmel-sha...
2621
  	dd->caps.has_hmac = 0;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
2622
2623
2624
  
  	/* keep only major version number */
  	switch (dd->hw_version & 0xff0) {
507c5cc23   Cyrille Pitchen   crypto: atmel-sha...
2625
2626
2627
2628
2629
  	case 0x510:
  		dd->caps.has_dma = 1;
  		dd->caps.has_dualbuff = 1;
  		dd->caps.has_sha224 = 1;
  		dd->caps.has_sha_384_512 = 1;
7cee35081   Cyrille Pitchen   crypto: atmel-sha...
2630
  		dd->caps.has_uihv = 1;
81d8750b2   Cyrille Pitchen   crypto: atmel-sha...
2631
  		dd->caps.has_hmac = 1;
507c5cc23   Cyrille Pitchen   crypto: atmel-sha...
2632
  		break;
141824d0a   Leilei Zhao   crypto: atmel-sha...
2633
2634
2635
2636
2637
  	case 0x420:
  		dd->caps.has_dma = 1;
  		dd->caps.has_dualbuff = 1;
  		dd->caps.has_sha224 = 1;
  		dd->caps.has_sha_384_512 = 1;
7cee35081   Cyrille Pitchen   crypto: atmel-sha...
2638
  		dd->caps.has_uihv = 1;
141824d0a   Leilei Zhao   crypto: atmel-sha...
2639
  		break;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
  	case 0x410:
  		dd->caps.has_dma = 1;
  		dd->caps.has_dualbuff = 1;
  		dd->caps.has_sha224 = 1;
  		dd->caps.has_sha_384_512 = 1;
  		break;
  	case 0x400:
  		dd->caps.has_dma = 1;
  		dd->caps.has_dualbuff = 1;
  		dd->caps.has_sha224 = 1;
  		break;
  	case 0x320:
  		break;
  	default:
  		dev_warn(dd->dev,
  				"Unmanaged sha version, set minimum capabilities
  ");
  		break;
  	}
  }
abfe7ae40   Nicolas Ferre   crypto: atmel-sha...
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
  #if defined(CONFIG_OF)
  static const struct of_device_id atmel_sha_dt_ids[] = {
  	{ .compatible = "atmel,at91sam9g46-sha" },
  	{ /* sentinel */ }
  };
  
  MODULE_DEVICE_TABLE(of, atmel_sha_dt_ids);
  
  static struct crypto_platform_data *atmel_sha_of_init(struct platform_device *pdev)
  {
  	struct device_node *np = pdev->dev.of_node;
  	struct crypto_platform_data *pdata;
  
  	if (!np) {
  		dev_err(&pdev->dev, "device node not found
  ");
  		return ERR_PTR(-EINVAL);
  	}
  
  	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  	if (!pdata) {
  		dev_err(&pdev->dev, "could not allocate memory for pdata
  ");
  		return ERR_PTR(-ENOMEM);
  	}
  
  	pdata->dma_slave = devm_kzalloc(&pdev->dev,
  					sizeof(*(pdata->dma_slave)),
  					GFP_KERNEL);
  	if (!pdata->dma_slave) {
  		dev_err(&pdev->dev, "could not allocate memory for dma_slave
  ");
abfe7ae40   Nicolas Ferre   crypto: atmel-sha...
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
  		return ERR_PTR(-ENOMEM);
  	}
  
  	return pdata;
  }
  #else /* CONFIG_OF */
  static inline struct crypto_platform_data *atmel_sha_of_init(struct platform_device *dev)
  {
  	return ERR_PTR(-EINVAL);
  }
  #endif
49cfe4db2   Greg Kroah-Hartman   Drivers: crypto: ...
2703
  static int atmel_sha_probe(struct platform_device *pdev)
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2704
2705
  {
  	struct atmel_sha_dev *sha_dd;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
2706
  	struct crypto_platform_data	*pdata;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2707
2708
  	struct device *dev = &pdev->dev;
  	struct resource *sha_res;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2709
  	int err;
b0e8b3417   LABBE Corentin   crypto: atmel - u...
2710
  	sha_dd = devm_kzalloc(&pdev->dev, sizeof(*sha_dd), GFP_KERNEL);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
  	if (sha_dd == NULL) {
  		dev_err(dev, "unable to alloc data struct.
  ");
  		err = -ENOMEM;
  		goto sha_dd_err;
  	}
  
  	sha_dd->dev = dev;
  
  	platform_set_drvdata(pdev, sha_dd);
  
  	INIT_LIST_HEAD(&sha_dd->list);
62728e820   Leilei Zhao   crypto: atmel-sha...
2723
  	spin_lock_init(&sha_dd->lock);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2724
2725
2726
  
  	tasklet_init(&sha_dd->done_task, atmel_sha_done_task,
  					(unsigned long)sha_dd);
f56809c3c   Cyrille Pitchen   crypto: atmel-sha...
2727
2728
  	tasklet_init(&sha_dd->queue_task, atmel_sha_queue_task,
  					(unsigned long)sha_dd);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
  
  	crypto_init_queue(&sha_dd->queue, ATMEL_SHA_QUEUE_LENGTH);
  
  	sha_dd->irq = -1;
  
  	/* Get the base address */
  	sha_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  	if (!sha_res) {
  		dev_err(dev, "no MEM resource info
  ");
  		err = -ENODEV;
  		goto res_err;
  	}
  	sha_dd->phys_base = sha_res->start;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2743
2744
2745
2746
2747
2748
2749
2750
2751
  
  	/* Get the IRQ */
  	sha_dd->irq = platform_get_irq(pdev,  0);
  	if (sha_dd->irq < 0) {
  		dev_err(dev, "no IRQ resource info
  ");
  		err = sha_dd->irq;
  		goto res_err;
  	}
b0e8b3417   LABBE Corentin   crypto: atmel - u...
2752
2753
  	err = devm_request_irq(&pdev->dev, sha_dd->irq, atmel_sha_irq,
  			       IRQF_SHARED, "atmel-sha", sha_dd);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2754
2755
2756
2757
2758
2759
2760
  	if (err) {
  		dev_err(dev, "unable to request sha irq.
  ");
  		goto res_err;
  	}
  
  	/* Initializing the clock */
b0e8b3417   LABBE Corentin   crypto: atmel - u...
2761
  	sha_dd->iclk = devm_clk_get(&pdev->dev, "sha_clk");
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2762
  	if (IS_ERR(sha_dd->iclk)) {
be2083567   Colin Ian King   crypto: atmel - f...
2763
2764
  		dev_err(dev, "clock initialization failed.
  ");
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2765
  		err = PTR_ERR(sha_dd->iclk);
b0e8b3417   LABBE Corentin   crypto: atmel - u...
2766
  		goto res_err;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2767
  	}
b0e8b3417   LABBE Corentin   crypto: atmel - u...
2768
  	sha_dd->io_base = devm_ioremap_resource(&pdev->dev, sha_res);
9b52d55f4   Vladimir Zapolskiy   crypto: atmel - f...
2769
  	if (IS_ERR(sha_dd->io_base)) {
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2770
2771
  		dev_err(dev, "can't ioremap
  ");
9b52d55f4   Vladimir Zapolskiy   crypto: atmel - f...
2772
  		err = PTR_ERR(sha_dd->io_base);
b0e8b3417   LABBE Corentin   crypto: atmel - u...
2773
  		goto res_err;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2774
  	}
c033042aa   Cyrille Pitchen   crypto: atmel-sha...
2775
2776
2777
  	err = clk_prepare(sha_dd->iclk);
  	if (err)
  		goto res_err;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
2778
2779
2780
2781
2782
2783
2784
  	atmel_sha_hw_version_init(sha_dd);
  
  	atmel_sha_get_cap(sha_dd);
  
  	if (sha_dd->caps.has_dma) {
  		pdata = pdev->dev.platform_data;
  		if (!pdata) {
abfe7ae40   Nicolas Ferre   crypto: atmel-sha...
2785
2786
2787
2788
2789
  			pdata = atmel_sha_of_init(pdev);
  			if (IS_ERR(pdata)) {
  				dev_err(&pdev->dev, "platform data not available
  ");
  				err = PTR_ERR(pdata);
c033042aa   Cyrille Pitchen   crypto: atmel-sha...
2790
  				goto iclk_unprepare;
abfe7ae40   Nicolas Ferre   crypto: atmel-sha...
2791
2792
2793
  			}
  		}
  		if (!pdata->dma_slave) {
d4905b38d   Nicolas Royer   crypto: atmel-sha...
2794
  			err = -ENXIO;
c033042aa   Cyrille Pitchen   crypto: atmel-sha...
2795
  			goto iclk_unprepare;
d4905b38d   Nicolas Royer   crypto: atmel-sha...
2796
2797
2798
2799
  		}
  		err = atmel_sha_dma_init(sha_dd, pdata);
  		if (err)
  			goto err_sha_dma;
abfe7ae40   Nicolas Ferre   crypto: atmel-sha...
2800
2801
2802
2803
  
  		dev_info(dev, "using %s for DMA transfers
  ",
  				dma_chan_name(sha_dd->dma_lch_in.chan));
d4905b38d   Nicolas Royer   crypto: atmel-sha...
2804
  	}
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2805
2806
2807
2808
2809
2810
2811
  	spin_lock(&atmel_sha.lock);
  	list_add_tail(&sha_dd->list, &atmel_sha.dev_list);
  	spin_unlock(&atmel_sha.lock);
  
  	err = atmel_sha_register_algs(sha_dd);
  	if (err)
  		goto err_algs;
1ca5b7d95   Nicolas Ferre   crypto: atmel-sha...
2812
2813
2814
2815
  	dev_info(dev, "Atmel SHA1/SHA256%s%s
  ",
  			sha_dd->caps.has_sha224 ? "/SHA224" : "",
  			sha_dd->caps.has_sha_384_512 ? "/SHA384/SHA512" : "");
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2816
2817
2818
2819
2820
2821
2822
  
  	return 0;
  
  err_algs:
  	spin_lock(&atmel_sha.lock);
  	list_del(&sha_dd->list);
  	spin_unlock(&atmel_sha.lock);
d4905b38d   Nicolas Royer   crypto: atmel-sha...
2823
2824
2825
  	if (sha_dd->caps.has_dma)
  		atmel_sha_dma_cleanup(sha_dd);
  err_sha_dma:
c033042aa   Cyrille Pitchen   crypto: atmel-sha...
2826
2827
  iclk_unprepare:
  	clk_unprepare(sha_dd->iclk);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2828
  res_err:
f56809c3c   Cyrille Pitchen   crypto: atmel-sha...
2829
  	tasklet_kill(&sha_dd->queue_task);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2830
  	tasklet_kill(&sha_dd->done_task);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2831
2832
2833
2834
2835
2836
  sha_dd_err:
  	dev_err(dev, "initialization failed.
  ");
  
  	return err;
  }
49cfe4db2   Greg Kroah-Hartman   Drivers: crypto: ...
2837
  static int atmel_sha_remove(struct platform_device *pdev)
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2838
  {
22d96f04b   Gustavo A. R. Silva   crypto: atmel-sha...
2839
  	struct atmel_sha_dev *sha_dd;
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2840
2841
2842
2843
2844
2845
2846
2847
2848
  
  	sha_dd = platform_get_drvdata(pdev);
  	if (!sha_dd)
  		return -ENODEV;
  	spin_lock(&atmel_sha.lock);
  	list_del(&sha_dd->list);
  	spin_unlock(&atmel_sha.lock);
  
  	atmel_sha_unregister_algs(sha_dd);
f56809c3c   Cyrille Pitchen   crypto: atmel-sha...
2849
  	tasklet_kill(&sha_dd->queue_task);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2850
  	tasklet_kill(&sha_dd->done_task);
d4905b38d   Nicolas Royer   crypto: atmel-sha...
2851
2852
  	if (sha_dd->caps.has_dma)
  		atmel_sha_dma_cleanup(sha_dd);
c033042aa   Cyrille Pitchen   crypto: atmel-sha...
2853
  	clk_unprepare(sha_dd->iclk);
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2854
2855
2856
2857
2858
  	return 0;
  }
  
  static struct platform_driver atmel_sha_driver = {
  	.probe		= atmel_sha_probe,
49cfe4db2   Greg Kroah-Hartman   Drivers: crypto: ...
2859
  	.remove		= atmel_sha_remove,
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2860
2861
  	.driver		= {
  		.name	= "atmel_sha",
abfe7ae40   Nicolas Ferre   crypto: atmel-sha...
2862
  		.of_match_table	= of_match_ptr(atmel_sha_dt_ids),
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2863
2864
2865
2866
  	},
  };
  
  module_platform_driver(atmel_sha_driver);
d4905b38d   Nicolas Royer   crypto: atmel-sha...
2867
  MODULE_DESCRIPTION("Atmel SHA (1/256/224/384/512) hw acceleration support.");
ebc82efa1   Nicolas Royer   crypto: atmel - a...
2868
2869
  MODULE_LICENSE("GPL v2");
  MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");