xref: /openbmc/linux/drivers/crypto/atmel-sha.c (revision af5034e8)
1820684ccSTudor Ambarus // SPDX-License-Identifier: GPL-2.0
2ebc82efaSNicolas Royer /*
3ebc82efaSNicolas Royer  * Cryptographic API.
4ebc82efaSNicolas Royer  *
5ebc82efaSNicolas Royer  * Support for ATMEL SHA1/SHA256 HW acceleration.
6ebc82efaSNicolas Royer  *
7ebc82efaSNicolas Royer  * Copyright (c) 2012 Eukréa Electromatique - ATMEL
8ebc82efaSNicolas Royer  * Author: Nicolas Royer <nicolas@eukrea.com>
9ebc82efaSNicolas Royer  *
10ebc82efaSNicolas Royer  * Some ideas are from omap-sham.c drivers.
11ebc82efaSNicolas Royer  */
12ebc82efaSNicolas Royer 
13ebc82efaSNicolas Royer 
14ebc82efaSNicolas Royer #include <linux/kernel.h>
15ebc82efaSNicolas Royer #include <linux/module.h>
16ebc82efaSNicolas Royer #include <linux/slab.h>
17ebc82efaSNicolas Royer #include <linux/err.h>
18ebc82efaSNicolas Royer #include <linux/clk.h>
19ebc82efaSNicolas Royer #include <linux/io.h>
20ebc82efaSNicolas Royer #include <linux/hw_random.h>
21ebc82efaSNicolas Royer #include <linux/platform_device.h>
22ebc82efaSNicolas Royer 
23ebc82efaSNicolas Royer #include <linux/device.h>
24ebc82efaSNicolas Royer #include <linux/init.h>
25ebc82efaSNicolas Royer #include <linux/errno.h>
26ebc82efaSNicolas Royer #include <linux/interrupt.h>
27ebc82efaSNicolas Royer #include <linux/irq.h>
28ebc82efaSNicolas Royer #include <linux/scatterlist.h>
29ebc82efaSNicolas Royer #include <linux/dma-mapping.h>
30abfe7ae4SNicolas Ferre #include <linux/of_device.h>
31ebc82efaSNicolas Royer #include <linux/delay.h>
32ebc82efaSNicolas Royer #include <linux/crypto.h>
33ebc82efaSNicolas Royer #include <linux/cryptohash.h>
34ebc82efaSNicolas Royer #include <crypto/scatterwalk.h>
35ebc82efaSNicolas Royer #include <crypto/algapi.h>
36ebc82efaSNicolas Royer #include <crypto/sha.h>
37ebc82efaSNicolas Royer #include <crypto/hash.h>
38ebc82efaSNicolas Royer #include <crypto/internal/hash.h>
39d4905b38SNicolas Royer #include <linux/platform_data/crypto-atmel.h>
40ebc82efaSNicolas Royer #include "atmel-sha-regs.h"
4189a82ef8SCyrille Pitchen #include "atmel-authenc.h"
42ebc82efaSNicolas Royer 
437c783029STudor Ambarus #define ATMEL_SHA_PRIORITY	300
447c783029STudor Ambarus 
45ebc82efaSNicolas Royer /* SHA flags */
46ebc82efaSNicolas Royer #define SHA_FLAGS_BUSY			BIT(0)
47ebc82efaSNicolas Royer #define	SHA_FLAGS_FINAL			BIT(1)
48ebc82efaSNicolas Royer #define SHA_FLAGS_DMA_ACTIVE	BIT(2)
49ebc82efaSNicolas Royer #define SHA_FLAGS_OUTPUT_READY	BIT(3)
50ebc82efaSNicolas Royer #define SHA_FLAGS_INIT			BIT(4)
51ebc82efaSNicolas Royer #define SHA_FLAGS_CPU			BIT(5)
52ebc82efaSNicolas Royer #define SHA_FLAGS_DMA_READY		BIT(6)
530569fc46SCyrille Pitchen #define SHA_FLAGS_DUMP_REG	BIT(7)
54ebc82efaSNicolas Royer 
5581d8750bSCyrille Pitchen /* bits[11:8] are reserved. */
56f07cebadSCyrille Pitchen 
57ebc82efaSNicolas Royer #define SHA_FLAGS_FINUP		BIT(16)
58ebc82efaSNicolas Royer #define SHA_FLAGS_SG		BIT(17)
59d4905b38SNicolas Royer #define SHA_FLAGS_ERROR		BIT(23)
60d4905b38SNicolas Royer #define SHA_FLAGS_PAD		BIT(24)
617cee3508SCyrille Pitchen #define SHA_FLAGS_RESTORE	BIT(25)
62eec12f66SCyrille Pitchen #define SHA_FLAGS_IDATAR0	BIT(26)
63eec12f66SCyrille Pitchen #define SHA_FLAGS_WAIT_DATARDY	BIT(27)
64ebc82efaSNicolas Royer 
6581d8750bSCyrille Pitchen #define SHA_OP_INIT	0
66ebc82efaSNicolas Royer #define SHA_OP_UPDATE	1
67ebc82efaSNicolas Royer #define SHA_OP_FINAL	2
6881d8750bSCyrille Pitchen #define SHA_OP_DIGEST	3
69ebc82efaSNicolas Royer 
70cc831d32SCyrille Pitchen #define SHA_BUFFER_LEN		(PAGE_SIZE / 16)
71ebc82efaSNicolas Royer 
72ebc82efaSNicolas Royer #define ATMEL_SHA_DMA_THRESHOLD		56
73ebc82efaSNicolas Royer 
74d4905b38SNicolas Royer struct atmel_sha_caps {
75d4905b38SNicolas Royer 	bool	has_dma;
76d4905b38SNicolas Royer 	bool	has_dualbuff;
77d4905b38SNicolas Royer 	bool	has_sha224;
78d4905b38SNicolas Royer 	bool	has_sha_384_512;
797cee3508SCyrille Pitchen 	bool	has_uihv;
8081d8750bSCyrille Pitchen 	bool	has_hmac;
81d4905b38SNicolas Royer };
82ebc82efaSNicolas Royer 
83ebc82efaSNicolas Royer struct atmel_sha_dev;
84ebc82efaSNicolas Royer 
85cc831d32SCyrille Pitchen /*
869c4274d9SCyrille Pitchen  * .statesize = sizeof(struct atmel_sha_reqctx) must be <= PAGE_SIZE / 8 as
87cc831d32SCyrille Pitchen  * tested by the ahash_prepare_alg() function.
88cc831d32SCyrille Pitchen  */
89ebc82efaSNicolas Royer struct atmel_sha_reqctx {
90ebc82efaSNicolas Royer 	struct atmel_sha_dev	*dd;
91ebc82efaSNicolas Royer 	unsigned long	flags;
92ebc82efaSNicolas Royer 	unsigned long	op;
93ebc82efaSNicolas Royer 
94d4905b38SNicolas Royer 	u8	digest[SHA512_DIGEST_SIZE] __aligned(sizeof(u32));
95d4905b38SNicolas Royer 	u64	digcnt[2];
96ebc82efaSNicolas Royer 	size_t	bufcnt;
97ebc82efaSNicolas Royer 	size_t	buflen;
98ebc82efaSNicolas Royer 	dma_addr_t	dma_addr;
99ebc82efaSNicolas Royer 
100ebc82efaSNicolas Royer 	/* walk state */
101ebc82efaSNicolas Royer 	struct scatterlist	*sg;
102ebc82efaSNicolas Royer 	unsigned int	offset;	/* offset in current sg */
103ebc82efaSNicolas Royer 	unsigned int	total;	/* total request */
104ebc82efaSNicolas Royer 
105d4905b38SNicolas Royer 	size_t block_size;
10681d8750bSCyrille Pitchen 	size_t hash_size;
107d4905b38SNicolas Royer 
1089c4274d9SCyrille Pitchen 	u8 buffer[SHA_BUFFER_LEN + SHA512_BLOCK_SIZE] __aligned(sizeof(u32));
109ebc82efaSNicolas Royer };
110ebc82efaSNicolas Royer 
111a29af939SCyrille Pitchen typedef int (*atmel_sha_fn_t)(struct atmel_sha_dev *);
112a29af939SCyrille Pitchen 
113ebc82efaSNicolas Royer struct atmel_sha_ctx {
114ebc82efaSNicolas Royer 	struct atmel_sha_dev	*dd;
115a29af939SCyrille Pitchen 	atmel_sha_fn_t		start;
116ebc82efaSNicolas Royer 
117ebc82efaSNicolas Royer 	unsigned long		flags;
118ebc82efaSNicolas Royer };
119ebc82efaSNicolas Royer 
120d4905b38SNicolas Royer #define ATMEL_SHA_QUEUE_LENGTH	50
121d4905b38SNicolas Royer 
122d4905b38SNicolas Royer struct atmel_sha_dma {
123d4905b38SNicolas Royer 	struct dma_chan			*chan;
124d4905b38SNicolas Royer 	struct dma_slave_config dma_conf;
12569303cf0SCyrille Pitchen 	struct scatterlist	*sg;
12669303cf0SCyrille Pitchen 	int			nents;
12769303cf0SCyrille Pitchen 	unsigned int		last_sg_length;
128d4905b38SNicolas Royer };
129ebc82efaSNicolas Royer 
130ebc82efaSNicolas Royer struct atmel_sha_dev {
131ebc82efaSNicolas Royer 	struct list_head	list;
132ebc82efaSNicolas Royer 	unsigned long		phys_base;
133ebc82efaSNicolas Royer 	struct device		*dev;
134ebc82efaSNicolas Royer 	struct clk			*iclk;
135ebc82efaSNicolas Royer 	int					irq;
136ebc82efaSNicolas Royer 	void __iomem		*io_base;
137ebc82efaSNicolas Royer 
138ebc82efaSNicolas Royer 	spinlock_t		lock;
139ebc82efaSNicolas Royer 	struct tasklet_struct	done_task;
140f56809c3SCyrille Pitchen 	struct tasklet_struct	queue_task;
141ebc82efaSNicolas Royer 
142ebc82efaSNicolas Royer 	unsigned long		flags;
143ebc82efaSNicolas Royer 	struct crypto_queue	queue;
144ebc82efaSNicolas Royer 	struct ahash_request	*req;
145a29af939SCyrille Pitchen 	bool			is_async;
14689a82ef8SCyrille Pitchen 	bool			force_complete;
147b5ce82a7SCyrille Pitchen 	atmel_sha_fn_t		resume;
148eec12f66SCyrille Pitchen 	atmel_sha_fn_t		cpu_transfer_complete;
149d4905b38SNicolas Royer 
150d4905b38SNicolas Royer 	struct atmel_sha_dma	dma_lch_in;
151d4905b38SNicolas Royer 
152d4905b38SNicolas Royer 	struct atmel_sha_caps	caps;
153d4905b38SNicolas Royer 
15481d8750bSCyrille Pitchen 	struct scatterlist	tmp;
15581d8750bSCyrille Pitchen 
156d4905b38SNicolas Royer 	u32	hw_version;
157ebc82efaSNicolas Royer };
158ebc82efaSNicolas Royer 
159ebc82efaSNicolas Royer struct atmel_sha_drv {
160ebc82efaSNicolas Royer 	struct list_head	dev_list;
161ebc82efaSNicolas Royer 	spinlock_t		lock;
162ebc82efaSNicolas Royer };
163ebc82efaSNicolas Royer 
164ebc82efaSNicolas Royer static struct atmel_sha_drv atmel_sha = {
165ebc82efaSNicolas Royer 	.dev_list = LIST_HEAD_INIT(atmel_sha.dev_list),
166ebc82efaSNicolas Royer 	.lock = __SPIN_LOCK_UNLOCKED(atmel_sha.lock),
167ebc82efaSNicolas Royer };
168ebc82efaSNicolas Royer 
1690569fc46SCyrille Pitchen #ifdef VERBOSE_DEBUG
1700569fc46SCyrille Pitchen static const char *atmel_sha_reg_name(u32 offset, char *tmp, size_t sz, bool wr)
1710569fc46SCyrille Pitchen {
1720569fc46SCyrille Pitchen 	switch (offset) {
1730569fc46SCyrille Pitchen 	case SHA_CR:
1740569fc46SCyrille Pitchen 		return "CR";
1750569fc46SCyrille Pitchen 
1760569fc46SCyrille Pitchen 	case SHA_MR:
1770569fc46SCyrille Pitchen 		return "MR";
1780569fc46SCyrille Pitchen 
1790569fc46SCyrille Pitchen 	case SHA_IER:
1800569fc46SCyrille Pitchen 		return "IER";
1810569fc46SCyrille Pitchen 
1820569fc46SCyrille Pitchen 	case SHA_IDR:
1830569fc46SCyrille Pitchen 		return "IDR";
1840569fc46SCyrille Pitchen 
1850569fc46SCyrille Pitchen 	case SHA_IMR:
1860569fc46SCyrille Pitchen 		return "IMR";
1870569fc46SCyrille Pitchen 
1880569fc46SCyrille Pitchen 	case SHA_ISR:
1890569fc46SCyrille Pitchen 		return "ISR";
1900569fc46SCyrille Pitchen 
1910569fc46SCyrille Pitchen 	case SHA_MSR:
1920569fc46SCyrille Pitchen 		return "MSR";
1930569fc46SCyrille Pitchen 
1940569fc46SCyrille Pitchen 	case SHA_BCR:
1950569fc46SCyrille Pitchen 		return "BCR";
1960569fc46SCyrille Pitchen 
1970569fc46SCyrille Pitchen 	case SHA_REG_DIN(0):
1980569fc46SCyrille Pitchen 	case SHA_REG_DIN(1):
1990569fc46SCyrille Pitchen 	case SHA_REG_DIN(2):
2000569fc46SCyrille Pitchen 	case SHA_REG_DIN(3):
2010569fc46SCyrille Pitchen 	case SHA_REG_DIN(4):
2020569fc46SCyrille Pitchen 	case SHA_REG_DIN(5):
2030569fc46SCyrille Pitchen 	case SHA_REG_DIN(6):
2040569fc46SCyrille Pitchen 	case SHA_REG_DIN(7):
2050569fc46SCyrille Pitchen 	case SHA_REG_DIN(8):
2060569fc46SCyrille Pitchen 	case SHA_REG_DIN(9):
2070569fc46SCyrille Pitchen 	case SHA_REG_DIN(10):
2080569fc46SCyrille Pitchen 	case SHA_REG_DIN(11):
2090569fc46SCyrille Pitchen 	case SHA_REG_DIN(12):
2100569fc46SCyrille Pitchen 	case SHA_REG_DIN(13):
2110569fc46SCyrille Pitchen 	case SHA_REG_DIN(14):
2120569fc46SCyrille Pitchen 	case SHA_REG_DIN(15):
2130569fc46SCyrille Pitchen 		snprintf(tmp, sz, "IDATAR[%u]", (offset - SHA_REG_DIN(0)) >> 2);
2140569fc46SCyrille Pitchen 		break;
2150569fc46SCyrille Pitchen 
2160569fc46SCyrille Pitchen 	case SHA_REG_DIGEST(0):
2170569fc46SCyrille Pitchen 	case SHA_REG_DIGEST(1):
2180569fc46SCyrille Pitchen 	case SHA_REG_DIGEST(2):
2190569fc46SCyrille Pitchen 	case SHA_REG_DIGEST(3):
2200569fc46SCyrille Pitchen 	case SHA_REG_DIGEST(4):
2210569fc46SCyrille Pitchen 	case SHA_REG_DIGEST(5):
2220569fc46SCyrille Pitchen 	case SHA_REG_DIGEST(6):
2230569fc46SCyrille Pitchen 	case SHA_REG_DIGEST(7):
2240569fc46SCyrille Pitchen 	case SHA_REG_DIGEST(8):
2250569fc46SCyrille Pitchen 	case SHA_REG_DIGEST(9):
2260569fc46SCyrille Pitchen 	case SHA_REG_DIGEST(10):
2270569fc46SCyrille Pitchen 	case SHA_REG_DIGEST(11):
2280569fc46SCyrille Pitchen 	case SHA_REG_DIGEST(12):
2290569fc46SCyrille Pitchen 	case SHA_REG_DIGEST(13):
2300569fc46SCyrille Pitchen 	case SHA_REG_DIGEST(14):
2310569fc46SCyrille Pitchen 	case SHA_REG_DIGEST(15):
2320569fc46SCyrille Pitchen 		if (wr)
2330569fc46SCyrille Pitchen 			snprintf(tmp, sz, "IDATAR[%u]",
2340569fc46SCyrille Pitchen 				 16u + ((offset - SHA_REG_DIGEST(0)) >> 2));
2350569fc46SCyrille Pitchen 		else
2360569fc46SCyrille Pitchen 			snprintf(tmp, sz, "ODATAR[%u]",
2370569fc46SCyrille Pitchen 				 (offset - SHA_REG_DIGEST(0)) >> 2);
2380569fc46SCyrille Pitchen 		break;
2390569fc46SCyrille Pitchen 
2400569fc46SCyrille Pitchen 	case SHA_HW_VERSION:
2410569fc46SCyrille Pitchen 		return "HWVER";
2420569fc46SCyrille Pitchen 
2430569fc46SCyrille Pitchen 	default:
2440569fc46SCyrille Pitchen 		snprintf(tmp, sz, "0x%02x", offset);
2450569fc46SCyrille Pitchen 		break;
2460569fc46SCyrille Pitchen 	}
2470569fc46SCyrille Pitchen 
2480569fc46SCyrille Pitchen 	return tmp;
2490569fc46SCyrille Pitchen }
2500569fc46SCyrille Pitchen 
2510569fc46SCyrille Pitchen #endif /* VERBOSE_DEBUG */
2520569fc46SCyrille Pitchen 
253ebc82efaSNicolas Royer static inline u32 atmel_sha_read(struct atmel_sha_dev *dd, u32 offset)
254ebc82efaSNicolas Royer {
2550569fc46SCyrille Pitchen 	u32 value = readl_relaxed(dd->io_base + offset);
2560569fc46SCyrille Pitchen 
2570569fc46SCyrille Pitchen #ifdef VERBOSE_DEBUG
2580569fc46SCyrille Pitchen 	if (dd->flags & SHA_FLAGS_DUMP_REG) {
2590569fc46SCyrille Pitchen 		char tmp[16];
2600569fc46SCyrille Pitchen 
2610569fc46SCyrille Pitchen 		dev_vdbg(dd->dev, "read 0x%08x from %s\n", value,
2620569fc46SCyrille Pitchen 			 atmel_sha_reg_name(offset, tmp, sizeof(tmp), false));
2630569fc46SCyrille Pitchen 	}
2640569fc46SCyrille Pitchen #endif /* VERBOSE_DEBUG */
2650569fc46SCyrille Pitchen 
2660569fc46SCyrille Pitchen 	return value;
267ebc82efaSNicolas Royer }
268ebc82efaSNicolas Royer 
269ebc82efaSNicolas Royer static inline void atmel_sha_write(struct atmel_sha_dev *dd,
270ebc82efaSNicolas Royer 					u32 offset, u32 value)
271ebc82efaSNicolas Royer {
2720569fc46SCyrille Pitchen #ifdef VERBOSE_DEBUG
2730569fc46SCyrille Pitchen 	if (dd->flags & SHA_FLAGS_DUMP_REG) {
2740569fc46SCyrille Pitchen 		char tmp[16];
2750569fc46SCyrille Pitchen 
2760569fc46SCyrille Pitchen 		dev_vdbg(dd->dev, "write 0x%08x into %s\n", value,
2770569fc46SCyrille Pitchen 			 atmel_sha_reg_name(offset, tmp, sizeof(tmp), true));
2780569fc46SCyrille Pitchen 	}
2790569fc46SCyrille Pitchen #endif /* VERBOSE_DEBUG */
2800569fc46SCyrille Pitchen 
281ebc82efaSNicolas Royer 	writel_relaxed(value, dd->io_base + offset);
282ebc82efaSNicolas Royer }
283ebc82efaSNicolas Royer 
284a29af939SCyrille Pitchen static inline int atmel_sha_complete(struct atmel_sha_dev *dd, int err)
285a29af939SCyrille Pitchen {
286a29af939SCyrille Pitchen 	struct ahash_request *req = dd->req;
287a29af939SCyrille Pitchen 
288a29af939SCyrille Pitchen 	dd->flags &= ~(SHA_FLAGS_BUSY | SHA_FLAGS_FINAL | SHA_FLAGS_CPU |
2890569fc46SCyrille Pitchen 		       SHA_FLAGS_DMA_READY | SHA_FLAGS_OUTPUT_READY |
2900569fc46SCyrille Pitchen 		       SHA_FLAGS_DUMP_REG);
291a29af939SCyrille Pitchen 
292a29af939SCyrille Pitchen 	clk_disable(dd->iclk);
293a29af939SCyrille Pitchen 
29489a82ef8SCyrille Pitchen 	if ((dd->is_async || dd->force_complete) && req->base.complete)
295a29af939SCyrille Pitchen 		req->base.complete(&req->base, err);
296a29af939SCyrille Pitchen 
297a29af939SCyrille Pitchen 	/* handle new request */
298a29af939SCyrille Pitchen 	tasklet_schedule(&dd->queue_task);
299a29af939SCyrille Pitchen 
300a29af939SCyrille Pitchen 	return err;
301a29af939SCyrille Pitchen }
302a29af939SCyrille Pitchen 
303ebc82efaSNicolas Royer static size_t atmel_sha_append_sg(struct atmel_sha_reqctx *ctx)
304ebc82efaSNicolas Royer {
305ebc82efaSNicolas Royer 	size_t count;
306ebc82efaSNicolas Royer 
307ebc82efaSNicolas Royer 	while ((ctx->bufcnt < ctx->buflen) && ctx->total) {
308ebc82efaSNicolas Royer 		count = min(ctx->sg->length - ctx->offset, ctx->total);
309ebc82efaSNicolas Royer 		count = min(count, ctx->buflen - ctx->bufcnt);
310ebc82efaSNicolas Royer 
311803eeae8SLeilei Zhao 		if (count <= 0) {
312803eeae8SLeilei Zhao 			/*
313803eeae8SLeilei Zhao 			* Check if count <= 0 because the buffer is full or
314803eeae8SLeilei Zhao 			* because the sg length is 0. In the latest case,
315803eeae8SLeilei Zhao 			* check if there is another sg in the list, a 0 length
316803eeae8SLeilei Zhao 			* sg doesn't necessarily mean the end of the sg list.
317803eeae8SLeilei Zhao 			*/
318803eeae8SLeilei Zhao 			if ((ctx->sg->length == 0) && !sg_is_last(ctx->sg)) {
319803eeae8SLeilei Zhao 				ctx->sg = sg_next(ctx->sg);
320803eeae8SLeilei Zhao 				continue;
321803eeae8SLeilei Zhao 			} else {
322ebc82efaSNicolas Royer 				break;
323803eeae8SLeilei Zhao 			}
324803eeae8SLeilei Zhao 		}
325ebc82efaSNicolas Royer 
326ebc82efaSNicolas Royer 		scatterwalk_map_and_copy(ctx->buffer + ctx->bufcnt, ctx->sg,
327ebc82efaSNicolas Royer 			ctx->offset, count, 0);
328ebc82efaSNicolas Royer 
329ebc82efaSNicolas Royer 		ctx->bufcnt += count;
330ebc82efaSNicolas Royer 		ctx->offset += count;
331ebc82efaSNicolas Royer 		ctx->total -= count;
332ebc82efaSNicolas Royer 
333ebc82efaSNicolas Royer 		if (ctx->offset == ctx->sg->length) {
334ebc82efaSNicolas Royer 			ctx->sg = sg_next(ctx->sg);
335ebc82efaSNicolas Royer 			if (ctx->sg)
336ebc82efaSNicolas Royer 				ctx->offset = 0;
337ebc82efaSNicolas Royer 			else
338ebc82efaSNicolas Royer 				ctx->total = 0;
339ebc82efaSNicolas Royer 		}
340ebc82efaSNicolas Royer 	}
341ebc82efaSNicolas Royer 
342ebc82efaSNicolas Royer 	return 0;
343ebc82efaSNicolas Royer }
344ebc82efaSNicolas Royer 
345ebc82efaSNicolas Royer /*
346d4905b38SNicolas Royer  * The purpose of this padding is to ensure that the padded message is a
347d4905b38SNicolas Royer  * multiple of 512 bits (SHA1/SHA224/SHA256) or 1024 bits (SHA384/SHA512).
348d4905b38SNicolas Royer  * The bit "1" is appended at the end of the message followed by
349d4905b38SNicolas Royer  * "padlen-1" zero bits. Then a 64 bits block (SHA1/SHA224/SHA256) or
350d4905b38SNicolas Royer  * 128 bits block (SHA384/SHA512) equals to the message length in bits
351d4905b38SNicolas Royer  * is appended.
352ebc82efaSNicolas Royer  *
353d4905b38SNicolas Royer  * For SHA1/SHA224/SHA256, padlen is calculated as followed:
354ebc82efaSNicolas Royer  *  - if message length < 56 bytes then padlen = 56 - message length
355ebc82efaSNicolas Royer  *  - else padlen = 64 + 56 - message length
356d4905b38SNicolas Royer  *
357d4905b38SNicolas Royer  * For SHA384/SHA512, padlen is calculated as followed:
358d4905b38SNicolas Royer  *  - if message length < 112 bytes then padlen = 112 - message length
359d4905b38SNicolas Royer  *  - else padlen = 128 + 112 - message length
360ebc82efaSNicolas Royer  */
361ebc82efaSNicolas Royer static void atmel_sha_fill_padding(struct atmel_sha_reqctx *ctx, int length)
362ebc82efaSNicolas Royer {
363ebc82efaSNicolas Royer 	unsigned int index, padlen;
364427e6e3aSHerbert Xu 	__be64 bits[2];
365d4905b38SNicolas Royer 	u64 size[2];
366ebc82efaSNicolas Royer 
367d4905b38SNicolas Royer 	size[0] = ctx->digcnt[0];
368d4905b38SNicolas Royer 	size[1] = ctx->digcnt[1];
369ebc82efaSNicolas Royer 
370d4905b38SNicolas Royer 	size[0] += ctx->bufcnt;
371d4905b38SNicolas Royer 	if (size[0] < ctx->bufcnt)
372d4905b38SNicolas Royer 		size[1]++;
373d4905b38SNicolas Royer 
374d4905b38SNicolas Royer 	size[0] += length;
375d4905b38SNicolas Royer 	if (size[0]  < length)
376d4905b38SNicolas Royer 		size[1]++;
377d4905b38SNicolas Royer 
378d4905b38SNicolas Royer 	bits[1] = cpu_to_be64(size[0] << 3);
379d4905b38SNicolas Royer 	bits[0] = cpu_to_be64(size[1] << 3 | size[0] >> 61);
380d4905b38SNicolas Royer 
381f07cebadSCyrille Pitchen 	switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
382f07cebadSCyrille Pitchen 	case SHA_FLAGS_SHA384:
383f07cebadSCyrille Pitchen 	case SHA_FLAGS_SHA512:
384d4905b38SNicolas Royer 		index = ctx->bufcnt & 0x7f;
385d4905b38SNicolas Royer 		padlen = (index < 112) ? (112 - index) : ((128+112) - index);
386d4905b38SNicolas Royer 		*(ctx->buffer + ctx->bufcnt) = 0x80;
387d4905b38SNicolas Royer 		memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1);
388d4905b38SNicolas Royer 		memcpy(ctx->buffer + ctx->bufcnt + padlen, bits, 16);
389d4905b38SNicolas Royer 		ctx->bufcnt += padlen + 16;
390d4905b38SNicolas Royer 		ctx->flags |= SHA_FLAGS_PAD;
391f07cebadSCyrille Pitchen 		break;
392f07cebadSCyrille Pitchen 
393f07cebadSCyrille Pitchen 	default:
394ebc82efaSNicolas Royer 		index = ctx->bufcnt & 0x3f;
395ebc82efaSNicolas Royer 		padlen = (index < 56) ? (56 - index) : ((64+56) - index);
396ebc82efaSNicolas Royer 		*(ctx->buffer + ctx->bufcnt) = 0x80;
397ebc82efaSNicolas Royer 		memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1);
398d4905b38SNicolas Royer 		memcpy(ctx->buffer + ctx->bufcnt + padlen, &bits[1], 8);
399ebc82efaSNicolas Royer 		ctx->bufcnt += padlen + 8;
400ebc82efaSNicolas Royer 		ctx->flags |= SHA_FLAGS_PAD;
401f07cebadSCyrille Pitchen 		break;
402ebc82efaSNicolas Royer 	}
403d4905b38SNicolas Royer }
404ebc82efaSNicolas Royer 
4058340c7fdSCyrille Pitchen static struct atmel_sha_dev *atmel_sha_find_dev(struct atmel_sha_ctx *tctx)
406ebc82efaSNicolas Royer {
407ebc82efaSNicolas Royer 	struct atmel_sha_dev *dd = NULL;
408ebc82efaSNicolas Royer 	struct atmel_sha_dev *tmp;
409ebc82efaSNicolas Royer 
410ebc82efaSNicolas Royer 	spin_lock_bh(&atmel_sha.lock);
411ebc82efaSNicolas Royer 	if (!tctx->dd) {
412ebc82efaSNicolas Royer 		list_for_each_entry(tmp, &atmel_sha.dev_list, list) {
413ebc82efaSNicolas Royer 			dd = tmp;
414ebc82efaSNicolas Royer 			break;
415ebc82efaSNicolas Royer 		}
416ebc82efaSNicolas Royer 		tctx->dd = dd;
417ebc82efaSNicolas Royer 	} else {
418ebc82efaSNicolas Royer 		dd = tctx->dd;
419ebc82efaSNicolas Royer 	}
420ebc82efaSNicolas Royer 
421ebc82efaSNicolas Royer 	spin_unlock_bh(&atmel_sha.lock);
422ebc82efaSNicolas Royer 
4238340c7fdSCyrille Pitchen 	return dd;
4248340c7fdSCyrille Pitchen }
4258340c7fdSCyrille Pitchen 
4268340c7fdSCyrille Pitchen static int atmel_sha_init(struct ahash_request *req)
4278340c7fdSCyrille Pitchen {
4288340c7fdSCyrille Pitchen 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
4298340c7fdSCyrille Pitchen 	struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm);
4308340c7fdSCyrille Pitchen 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
4318340c7fdSCyrille Pitchen 	struct atmel_sha_dev *dd = atmel_sha_find_dev(tctx);
4328340c7fdSCyrille Pitchen 
433ebc82efaSNicolas Royer 	ctx->dd = dd;
434ebc82efaSNicolas Royer 
435ebc82efaSNicolas Royer 	ctx->flags = 0;
436ebc82efaSNicolas Royer 
437ebc82efaSNicolas Royer 	dev_dbg(dd->dev, "init: digest size: %d\n",
438ebc82efaSNicolas Royer 		crypto_ahash_digestsize(tfm));
439ebc82efaSNicolas Royer 
440d4905b38SNicolas Royer 	switch (crypto_ahash_digestsize(tfm)) {
441d4905b38SNicolas Royer 	case SHA1_DIGEST_SIZE:
442ebc82efaSNicolas Royer 		ctx->flags |= SHA_FLAGS_SHA1;
443d4905b38SNicolas Royer 		ctx->block_size = SHA1_BLOCK_SIZE;
444d4905b38SNicolas Royer 		break;
445d4905b38SNicolas Royer 	case SHA224_DIGEST_SIZE:
446d4905b38SNicolas Royer 		ctx->flags |= SHA_FLAGS_SHA224;
447d4905b38SNicolas Royer 		ctx->block_size = SHA224_BLOCK_SIZE;
448d4905b38SNicolas Royer 		break;
449d4905b38SNicolas Royer 	case SHA256_DIGEST_SIZE:
450ebc82efaSNicolas Royer 		ctx->flags |= SHA_FLAGS_SHA256;
451d4905b38SNicolas Royer 		ctx->block_size = SHA256_BLOCK_SIZE;
452d4905b38SNicolas Royer 		break;
453d4905b38SNicolas Royer 	case SHA384_DIGEST_SIZE:
454d4905b38SNicolas Royer 		ctx->flags |= SHA_FLAGS_SHA384;
455d4905b38SNicolas Royer 		ctx->block_size = SHA384_BLOCK_SIZE;
456d4905b38SNicolas Royer 		break;
457d4905b38SNicolas Royer 	case SHA512_DIGEST_SIZE:
458d4905b38SNicolas Royer 		ctx->flags |= SHA_FLAGS_SHA512;
459d4905b38SNicolas Royer 		ctx->block_size = SHA512_BLOCK_SIZE;
460d4905b38SNicolas Royer 		break;
461d4905b38SNicolas Royer 	default:
462d4905b38SNicolas Royer 		return -EINVAL;
463d4905b38SNicolas Royer 		break;
464d4905b38SNicolas Royer 	}
465ebc82efaSNicolas Royer 
466ebc82efaSNicolas Royer 	ctx->bufcnt = 0;
467d4905b38SNicolas Royer 	ctx->digcnt[0] = 0;
468d4905b38SNicolas Royer 	ctx->digcnt[1] = 0;
469ebc82efaSNicolas Royer 	ctx->buflen = SHA_BUFFER_LEN;
470ebc82efaSNicolas Royer 
471ebc82efaSNicolas Royer 	return 0;
472ebc82efaSNicolas Royer }
473ebc82efaSNicolas Royer 
474ebc82efaSNicolas Royer static void atmel_sha_write_ctrl(struct atmel_sha_dev *dd, int dma)
475ebc82efaSNicolas Royer {
476ebc82efaSNicolas Royer 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
4777cee3508SCyrille Pitchen 	u32 valmr = SHA_MR_MODE_AUTO;
4787cee3508SCyrille Pitchen 	unsigned int i, hashsize = 0;
479ebc82efaSNicolas Royer 
480ebc82efaSNicolas Royer 	if (likely(dma)) {
481d4905b38SNicolas Royer 		if (!dd->caps.has_dma)
482ebc82efaSNicolas Royer 			atmel_sha_write(dd, SHA_IER, SHA_INT_TXBUFE);
483ebc82efaSNicolas Royer 		valmr = SHA_MR_MODE_PDC;
484d4905b38SNicolas Royer 		if (dd->caps.has_dualbuff)
485d4905b38SNicolas Royer 			valmr |= SHA_MR_DUALBUFF;
486ebc82efaSNicolas Royer 	} else {
487ebc82efaSNicolas Royer 		atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
488ebc82efaSNicolas Royer 	}
489ebc82efaSNicolas Royer 
4907cee3508SCyrille Pitchen 	switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
4917cee3508SCyrille Pitchen 	case SHA_FLAGS_SHA1:
492d4905b38SNicolas Royer 		valmr |= SHA_MR_ALGO_SHA1;
4937cee3508SCyrille Pitchen 		hashsize = SHA1_DIGEST_SIZE;
4947cee3508SCyrille Pitchen 		break;
4957cee3508SCyrille Pitchen 
4967cee3508SCyrille Pitchen 	case SHA_FLAGS_SHA224:
497d4905b38SNicolas Royer 		valmr |= SHA_MR_ALGO_SHA224;
4987cee3508SCyrille Pitchen 		hashsize = SHA256_DIGEST_SIZE;
4997cee3508SCyrille Pitchen 		break;
5007cee3508SCyrille Pitchen 
5017cee3508SCyrille Pitchen 	case SHA_FLAGS_SHA256:
502ebc82efaSNicolas Royer 		valmr |= SHA_MR_ALGO_SHA256;
5037cee3508SCyrille Pitchen 		hashsize = SHA256_DIGEST_SIZE;
5047cee3508SCyrille Pitchen 		break;
5057cee3508SCyrille Pitchen 
5067cee3508SCyrille Pitchen 	case SHA_FLAGS_SHA384:
507d4905b38SNicolas Royer 		valmr |= SHA_MR_ALGO_SHA384;
5087cee3508SCyrille Pitchen 		hashsize = SHA512_DIGEST_SIZE;
5097cee3508SCyrille Pitchen 		break;
5107cee3508SCyrille Pitchen 
5117cee3508SCyrille Pitchen 	case SHA_FLAGS_SHA512:
512d4905b38SNicolas Royer 		valmr |= SHA_MR_ALGO_SHA512;
5137cee3508SCyrille Pitchen 		hashsize = SHA512_DIGEST_SIZE;
5147cee3508SCyrille Pitchen 		break;
5157cee3508SCyrille Pitchen 
5167cee3508SCyrille Pitchen 	default:
5177cee3508SCyrille Pitchen 		break;
5187cee3508SCyrille Pitchen 	}
519ebc82efaSNicolas Royer 
520ebc82efaSNicolas Royer 	/* Setting CR_FIRST only for the first iteration */
5217cee3508SCyrille Pitchen 	if (!(ctx->digcnt[0] || ctx->digcnt[1])) {
5227cee3508SCyrille Pitchen 		atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
5237cee3508SCyrille Pitchen 	} else if (dd->caps.has_uihv && (ctx->flags & SHA_FLAGS_RESTORE)) {
5247cee3508SCyrille Pitchen 		const u32 *hash = (const u32 *)ctx->digest;
525ebc82efaSNicolas Royer 
5267cee3508SCyrille Pitchen 		/*
5277cee3508SCyrille Pitchen 		 * Restore the hardware context: update the User Initialize
5287cee3508SCyrille Pitchen 		 * Hash Value (UIHV) with the value saved when the latest
5297cee3508SCyrille Pitchen 		 * 'update' operation completed on this very same crypto
5307cee3508SCyrille Pitchen 		 * request.
5317cee3508SCyrille Pitchen 		 */
5327cee3508SCyrille Pitchen 		ctx->flags &= ~SHA_FLAGS_RESTORE;
5337cee3508SCyrille Pitchen 		atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
5347cee3508SCyrille Pitchen 		for (i = 0; i < hashsize / sizeof(u32); ++i)
5357cee3508SCyrille Pitchen 			atmel_sha_write(dd, SHA_REG_DIN(i), hash[i]);
5367cee3508SCyrille Pitchen 		atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
5377cee3508SCyrille Pitchen 		valmr |= SHA_MR_UIHV;
5387cee3508SCyrille Pitchen 	}
5397cee3508SCyrille Pitchen 	/*
5407cee3508SCyrille Pitchen 	 * WARNING: If the UIHV feature is not available, the hardware CANNOT
5417cee3508SCyrille Pitchen 	 * process concurrent requests: the internal registers used to store
5427cee3508SCyrille Pitchen 	 * the hash/digest are still set to the partial digest output values
5437cee3508SCyrille Pitchen 	 * computed during the latest round.
5447cee3508SCyrille Pitchen 	 */
5457cee3508SCyrille Pitchen 
546ebc82efaSNicolas Royer 	atmel_sha_write(dd, SHA_MR, valmr);
547ebc82efaSNicolas Royer }
548ebc82efaSNicolas Royer 
5499064ed92SCyrille Pitchen static inline int atmel_sha_wait_for_data_ready(struct atmel_sha_dev *dd,
5509064ed92SCyrille Pitchen 						atmel_sha_fn_t resume)
5519064ed92SCyrille Pitchen {
5529064ed92SCyrille Pitchen 	u32 isr = atmel_sha_read(dd, SHA_ISR);
5539064ed92SCyrille Pitchen 
5549064ed92SCyrille Pitchen 	if (unlikely(isr & SHA_INT_DATARDY))
5559064ed92SCyrille Pitchen 		return resume(dd);
5569064ed92SCyrille Pitchen 
5579064ed92SCyrille Pitchen 	dd->resume = resume;
5589064ed92SCyrille Pitchen 	atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
5599064ed92SCyrille Pitchen 	return -EINPROGRESS;
5609064ed92SCyrille Pitchen }
5619064ed92SCyrille Pitchen 
562ebc82efaSNicolas Royer static int atmel_sha_xmit_cpu(struct atmel_sha_dev *dd, const u8 *buf,
563ebc82efaSNicolas Royer 			      size_t length, int final)
564ebc82efaSNicolas Royer {
565ebc82efaSNicolas Royer 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
566ebc82efaSNicolas Royer 	int count, len32;
567ebc82efaSNicolas Royer 	const u32 *buffer = (const u32 *)buf;
568ebc82efaSNicolas Royer 
5694c147bcfSArnd Bergmann 	dev_dbg(dd->dev, "xmit_cpu: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
570d4905b38SNicolas Royer 		ctx->digcnt[1], ctx->digcnt[0], length, final);
571ebc82efaSNicolas Royer 
572ebc82efaSNicolas Royer 	atmel_sha_write_ctrl(dd, 0);
573ebc82efaSNicolas Royer 
574ebc82efaSNicolas Royer 	/* should be non-zero before next lines to disable clocks later */
575d4905b38SNicolas Royer 	ctx->digcnt[0] += length;
576d4905b38SNicolas Royer 	if (ctx->digcnt[0] < length)
577d4905b38SNicolas Royer 		ctx->digcnt[1]++;
578ebc82efaSNicolas Royer 
579ebc82efaSNicolas Royer 	if (final)
580ebc82efaSNicolas Royer 		dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
581ebc82efaSNicolas Royer 
582ebc82efaSNicolas Royer 	len32 = DIV_ROUND_UP(length, sizeof(u32));
583ebc82efaSNicolas Royer 
584ebc82efaSNicolas Royer 	dd->flags |= SHA_FLAGS_CPU;
585ebc82efaSNicolas Royer 
586ebc82efaSNicolas Royer 	for (count = 0; count < len32; count++)
587ebc82efaSNicolas Royer 		atmel_sha_write(dd, SHA_REG_DIN(count), buffer[count]);
588ebc82efaSNicolas Royer 
589ebc82efaSNicolas Royer 	return -EINPROGRESS;
590ebc82efaSNicolas Royer }
591ebc82efaSNicolas Royer 
592ebc82efaSNicolas Royer static int atmel_sha_xmit_pdc(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
593ebc82efaSNicolas Royer 		size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
594ebc82efaSNicolas Royer {
595ebc82efaSNicolas Royer 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
596ebc82efaSNicolas Royer 	int len32;
597ebc82efaSNicolas Royer 
5984c147bcfSArnd Bergmann 	dev_dbg(dd->dev, "xmit_pdc: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
599d4905b38SNicolas Royer 		ctx->digcnt[1], ctx->digcnt[0], length1, final);
600ebc82efaSNicolas Royer 
601ebc82efaSNicolas Royer 	len32 = DIV_ROUND_UP(length1, sizeof(u32));
602ebc82efaSNicolas Royer 	atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTDIS);
603ebc82efaSNicolas Royer 	atmel_sha_write(dd, SHA_TPR, dma_addr1);
604ebc82efaSNicolas Royer 	atmel_sha_write(dd, SHA_TCR, len32);
605ebc82efaSNicolas Royer 
606ebc82efaSNicolas Royer 	len32 = DIV_ROUND_UP(length2, sizeof(u32));
607ebc82efaSNicolas Royer 	atmel_sha_write(dd, SHA_TNPR, dma_addr2);
608ebc82efaSNicolas Royer 	atmel_sha_write(dd, SHA_TNCR, len32);
609ebc82efaSNicolas Royer 
610ebc82efaSNicolas Royer 	atmel_sha_write_ctrl(dd, 1);
611ebc82efaSNicolas Royer 
612ebc82efaSNicolas Royer 	/* should be non-zero before next lines to disable clocks later */
613d4905b38SNicolas Royer 	ctx->digcnt[0] += length1;
614d4905b38SNicolas Royer 	if (ctx->digcnt[0] < length1)
615d4905b38SNicolas Royer 		ctx->digcnt[1]++;
616ebc82efaSNicolas Royer 
617ebc82efaSNicolas Royer 	if (final)
618ebc82efaSNicolas Royer 		dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
619ebc82efaSNicolas Royer 
620ebc82efaSNicolas Royer 	dd->flags |=  SHA_FLAGS_DMA_ACTIVE;
621ebc82efaSNicolas Royer 
622ebc82efaSNicolas Royer 	/* Start DMA transfer */
623ebc82efaSNicolas Royer 	atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTEN);
624ebc82efaSNicolas Royer 
625ebc82efaSNicolas Royer 	return -EINPROGRESS;
626ebc82efaSNicolas Royer }
627ebc82efaSNicolas Royer 
628d4905b38SNicolas Royer static void atmel_sha_dma_callback(void *data)
629d4905b38SNicolas Royer {
630d4905b38SNicolas Royer 	struct atmel_sha_dev *dd = data;
631d4905b38SNicolas Royer 
632a29af939SCyrille Pitchen 	dd->is_async = true;
633a29af939SCyrille Pitchen 
634d4905b38SNicolas Royer 	/* dma_lch_in - completed - wait DATRDY */
635d4905b38SNicolas Royer 	atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
636d4905b38SNicolas Royer }
637d4905b38SNicolas Royer 
638d4905b38SNicolas Royer static int atmel_sha_xmit_dma(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
639d4905b38SNicolas Royer 		size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
640d4905b38SNicolas Royer {
641d4905b38SNicolas Royer 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
642d4905b38SNicolas Royer 	struct dma_async_tx_descriptor	*in_desc;
643d4905b38SNicolas Royer 	struct scatterlist sg[2];
644d4905b38SNicolas Royer 
6454c147bcfSArnd Bergmann 	dev_dbg(dd->dev, "xmit_dma: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
646d4905b38SNicolas Royer 		ctx->digcnt[1], ctx->digcnt[0], length1, final);
647d4905b38SNicolas Royer 
648d4905b38SNicolas Royer 	dd->dma_lch_in.dma_conf.src_maxburst = 16;
649d4905b38SNicolas Royer 	dd->dma_lch_in.dma_conf.dst_maxburst = 16;
650d4905b38SNicolas Royer 
651d4905b38SNicolas Royer 	dmaengine_slave_config(dd->dma_lch_in.chan, &dd->dma_lch_in.dma_conf);
652d4905b38SNicolas Royer 
653d4905b38SNicolas Royer 	if (length2) {
654d4905b38SNicolas Royer 		sg_init_table(sg, 2);
655d4905b38SNicolas Royer 		sg_dma_address(&sg[0]) = dma_addr1;
656d4905b38SNicolas Royer 		sg_dma_len(&sg[0]) = length1;
657d4905b38SNicolas Royer 		sg_dma_address(&sg[1]) = dma_addr2;
658d4905b38SNicolas Royer 		sg_dma_len(&sg[1]) = length2;
659d4905b38SNicolas Royer 		in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 2,
660d4905b38SNicolas Royer 			DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
661d4905b38SNicolas Royer 	} else {
662d4905b38SNicolas Royer 		sg_init_table(sg, 1);
663d4905b38SNicolas Royer 		sg_dma_address(&sg[0]) = dma_addr1;
664d4905b38SNicolas Royer 		sg_dma_len(&sg[0]) = length1;
665d4905b38SNicolas Royer 		in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 1,
666d4905b38SNicolas Royer 			DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
667d4905b38SNicolas Royer 	}
668d4905b38SNicolas Royer 	if (!in_desc)
669dd3f9f40SCyrille Pitchen 		return atmel_sha_complete(dd, -EINVAL);
670d4905b38SNicolas Royer 
671d4905b38SNicolas Royer 	in_desc->callback = atmel_sha_dma_callback;
672d4905b38SNicolas Royer 	in_desc->callback_param = dd;
673d4905b38SNicolas Royer 
674d4905b38SNicolas Royer 	atmel_sha_write_ctrl(dd, 1);
675d4905b38SNicolas Royer 
676d4905b38SNicolas Royer 	/* should be non-zero before next lines to disable clocks later */
677d4905b38SNicolas Royer 	ctx->digcnt[0] += length1;
678d4905b38SNicolas Royer 	if (ctx->digcnt[0] < length1)
679d4905b38SNicolas Royer 		ctx->digcnt[1]++;
680d4905b38SNicolas Royer 
681d4905b38SNicolas Royer 	if (final)
682d4905b38SNicolas Royer 		dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
683d4905b38SNicolas Royer 
684d4905b38SNicolas Royer 	dd->flags |=  SHA_FLAGS_DMA_ACTIVE;
685d4905b38SNicolas Royer 
686d4905b38SNicolas Royer 	/* Start DMA transfer */
687d4905b38SNicolas Royer 	dmaengine_submit(in_desc);
688d4905b38SNicolas Royer 	dma_async_issue_pending(dd->dma_lch_in.chan);
689d4905b38SNicolas Royer 
690d4905b38SNicolas Royer 	return -EINPROGRESS;
691d4905b38SNicolas Royer }
692d4905b38SNicolas Royer 
693d4905b38SNicolas Royer static int atmel_sha_xmit_start(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
694d4905b38SNicolas Royer 		size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
695d4905b38SNicolas Royer {
696d4905b38SNicolas Royer 	if (dd->caps.has_dma)
697d4905b38SNicolas Royer 		return atmel_sha_xmit_dma(dd, dma_addr1, length1,
698d4905b38SNicolas Royer 				dma_addr2, length2, final);
699d4905b38SNicolas Royer 	else
700d4905b38SNicolas Royer 		return atmel_sha_xmit_pdc(dd, dma_addr1, length1,
701d4905b38SNicolas Royer 				dma_addr2, length2, final);
702d4905b38SNicolas Royer }
703d4905b38SNicolas Royer 
704ebc82efaSNicolas Royer static int atmel_sha_update_cpu(struct atmel_sha_dev *dd)
705ebc82efaSNicolas Royer {
706ebc82efaSNicolas Royer 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
707ebc82efaSNicolas Royer 	int bufcnt;
708ebc82efaSNicolas Royer 
709ebc82efaSNicolas Royer 	atmel_sha_append_sg(ctx);
710ebc82efaSNicolas Royer 	atmel_sha_fill_padding(ctx, 0);
711ebc82efaSNicolas Royer 	bufcnt = ctx->bufcnt;
712ebc82efaSNicolas Royer 	ctx->bufcnt = 0;
713ebc82efaSNicolas Royer 
714ebc82efaSNicolas Royer 	return atmel_sha_xmit_cpu(dd, ctx->buffer, bufcnt, 1);
715ebc82efaSNicolas Royer }
716ebc82efaSNicolas Royer 
717ebc82efaSNicolas Royer static int atmel_sha_xmit_dma_map(struct atmel_sha_dev *dd,
718ebc82efaSNicolas Royer 					struct atmel_sha_reqctx *ctx,
719ebc82efaSNicolas Royer 					size_t length, int final)
720ebc82efaSNicolas Royer {
721ebc82efaSNicolas Royer 	ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
722d4905b38SNicolas Royer 				ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
723ebc82efaSNicolas Royer 	if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
7244c147bcfSArnd Bergmann 		dev_err(dd->dev, "dma %zu bytes error\n", ctx->buflen +
725d4905b38SNicolas Royer 				ctx->block_size);
726dd3f9f40SCyrille Pitchen 		return atmel_sha_complete(dd, -EINVAL);
727ebc82efaSNicolas Royer 	}
728ebc82efaSNicolas Royer 
729ebc82efaSNicolas Royer 	ctx->flags &= ~SHA_FLAGS_SG;
730ebc82efaSNicolas Royer 
731ebc82efaSNicolas Royer 	/* next call does not fail... so no unmap in the case of error */
732d4905b38SNicolas Royer 	return atmel_sha_xmit_start(dd, ctx->dma_addr, length, 0, 0, final);
733ebc82efaSNicolas Royer }
734ebc82efaSNicolas Royer 
735ebc82efaSNicolas Royer static int atmel_sha_update_dma_slow(struct atmel_sha_dev *dd)
736ebc82efaSNicolas Royer {
737ebc82efaSNicolas Royer 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
738ebc82efaSNicolas Royer 	unsigned int final;
739ebc82efaSNicolas Royer 	size_t count;
740ebc82efaSNicolas Royer 
741ebc82efaSNicolas Royer 	atmel_sha_append_sg(ctx);
742ebc82efaSNicolas Royer 
743ebc82efaSNicolas Royer 	final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
744ebc82efaSNicolas Royer 
7454c147bcfSArnd Bergmann 	dev_dbg(dd->dev, "slow: bufcnt: %zu, digcnt: 0x%llx 0x%llx, final: %d\n",
746d4905b38SNicolas Royer 		 ctx->bufcnt, ctx->digcnt[1], ctx->digcnt[0], final);
747ebc82efaSNicolas Royer 
748ebc82efaSNicolas Royer 	if (final)
749ebc82efaSNicolas Royer 		atmel_sha_fill_padding(ctx, 0);
750ebc82efaSNicolas Royer 
7510099286bSLudovic Desroches 	if (final || (ctx->bufcnt == ctx->buflen)) {
752ebc82efaSNicolas Royer 		count = ctx->bufcnt;
753ebc82efaSNicolas Royer 		ctx->bufcnt = 0;
754ebc82efaSNicolas Royer 		return atmel_sha_xmit_dma_map(dd, ctx, count, final);
755ebc82efaSNicolas Royer 	}
756ebc82efaSNicolas Royer 
757ebc82efaSNicolas Royer 	return 0;
758ebc82efaSNicolas Royer }
759ebc82efaSNicolas Royer 
760ebc82efaSNicolas Royer static int atmel_sha_update_dma_start(struct atmel_sha_dev *dd)
761ebc82efaSNicolas Royer {
762ebc82efaSNicolas Royer 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
763ebc82efaSNicolas Royer 	unsigned int length, final, tail;
764ebc82efaSNicolas Royer 	struct scatterlist *sg;
765ebc82efaSNicolas Royer 	unsigned int count;
766ebc82efaSNicolas Royer 
767ebc82efaSNicolas Royer 	if (!ctx->total)
768ebc82efaSNicolas Royer 		return 0;
769ebc82efaSNicolas Royer 
770ebc82efaSNicolas Royer 	if (ctx->bufcnt || ctx->offset)
771ebc82efaSNicolas Royer 		return atmel_sha_update_dma_slow(dd);
772ebc82efaSNicolas Royer 
7734c147bcfSArnd Bergmann 	dev_dbg(dd->dev, "fast: digcnt: 0x%llx 0x%llx, bufcnt: %zd, total: %u\n",
774d4905b38SNicolas Royer 		ctx->digcnt[1], ctx->digcnt[0], ctx->bufcnt, ctx->total);
775ebc82efaSNicolas Royer 
776ebc82efaSNicolas Royer 	sg = ctx->sg;
777ebc82efaSNicolas Royer 
778ebc82efaSNicolas Royer 	if (!IS_ALIGNED(sg->offset, sizeof(u32)))
779ebc82efaSNicolas Royer 		return atmel_sha_update_dma_slow(dd);
780ebc82efaSNicolas Royer 
781d4905b38SNicolas Royer 	if (!sg_is_last(sg) && !IS_ALIGNED(sg->length, ctx->block_size))
782d4905b38SNicolas Royer 		/* size is not ctx->block_size aligned */
783ebc82efaSNicolas Royer 		return atmel_sha_update_dma_slow(dd);
784ebc82efaSNicolas Royer 
785ebc82efaSNicolas Royer 	length = min(ctx->total, sg->length);
786ebc82efaSNicolas Royer 
787ebc82efaSNicolas Royer 	if (sg_is_last(sg)) {
788ebc82efaSNicolas Royer 		if (!(ctx->flags & SHA_FLAGS_FINUP)) {
789d4905b38SNicolas Royer 			/* not last sg must be ctx->block_size aligned */
790d4905b38SNicolas Royer 			tail = length & (ctx->block_size - 1);
791ebc82efaSNicolas Royer 			length -= tail;
792ebc82efaSNicolas Royer 		}
793ebc82efaSNicolas Royer 	}
794ebc82efaSNicolas Royer 
795ebc82efaSNicolas Royer 	ctx->total -= length;
796ebc82efaSNicolas Royer 	ctx->offset = length; /* offset where to start slow */
797ebc82efaSNicolas Royer 
798ebc82efaSNicolas Royer 	final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
799ebc82efaSNicolas Royer 
800ebc82efaSNicolas Royer 	/* Add padding */
801ebc82efaSNicolas Royer 	if (final) {
802d4905b38SNicolas Royer 		tail = length & (ctx->block_size - 1);
803ebc82efaSNicolas Royer 		length -= tail;
804ebc82efaSNicolas Royer 		ctx->total += tail;
805ebc82efaSNicolas Royer 		ctx->offset = length; /* offset where to start slow */
806ebc82efaSNicolas Royer 
807ebc82efaSNicolas Royer 		sg = ctx->sg;
808ebc82efaSNicolas Royer 		atmel_sha_append_sg(ctx);
809ebc82efaSNicolas Royer 
810ebc82efaSNicolas Royer 		atmel_sha_fill_padding(ctx, length);
811ebc82efaSNicolas Royer 
812ebc82efaSNicolas Royer 		ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
813d4905b38SNicolas Royer 			ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
814ebc82efaSNicolas Royer 		if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
8154c147bcfSArnd Bergmann 			dev_err(dd->dev, "dma %zu bytes error\n",
816d4905b38SNicolas Royer 				ctx->buflen + ctx->block_size);
817dd3f9f40SCyrille Pitchen 			return atmel_sha_complete(dd, -EINVAL);
818ebc82efaSNicolas Royer 		}
819ebc82efaSNicolas Royer 
820ebc82efaSNicolas Royer 		if (length == 0) {
821ebc82efaSNicolas Royer 			ctx->flags &= ~SHA_FLAGS_SG;
822ebc82efaSNicolas Royer 			count = ctx->bufcnt;
823ebc82efaSNicolas Royer 			ctx->bufcnt = 0;
824d4905b38SNicolas Royer 			return atmel_sha_xmit_start(dd, ctx->dma_addr, count, 0,
825ebc82efaSNicolas Royer 					0, final);
826ebc82efaSNicolas Royer 		} else {
827ebc82efaSNicolas Royer 			ctx->sg = sg;
828ebc82efaSNicolas Royer 			if (!dma_map_sg(dd->dev, ctx->sg, 1,
829ebc82efaSNicolas Royer 				DMA_TO_DEVICE)) {
830ebc82efaSNicolas Royer 					dev_err(dd->dev, "dma_map_sg  error\n");
831dd3f9f40SCyrille Pitchen 					return atmel_sha_complete(dd, -EINVAL);
832ebc82efaSNicolas Royer 			}
833ebc82efaSNicolas Royer 
834ebc82efaSNicolas Royer 			ctx->flags |= SHA_FLAGS_SG;
835ebc82efaSNicolas Royer 
836ebc82efaSNicolas Royer 			count = ctx->bufcnt;
837ebc82efaSNicolas Royer 			ctx->bufcnt = 0;
838d4905b38SNicolas Royer 			return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg),
839ebc82efaSNicolas Royer 					length, ctx->dma_addr, count, final);
840ebc82efaSNicolas Royer 		}
841ebc82efaSNicolas Royer 	}
842ebc82efaSNicolas Royer 
843ebc82efaSNicolas Royer 	if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
844ebc82efaSNicolas Royer 		dev_err(dd->dev, "dma_map_sg  error\n");
845dd3f9f40SCyrille Pitchen 		return atmel_sha_complete(dd, -EINVAL);
846ebc82efaSNicolas Royer 	}
847ebc82efaSNicolas Royer 
848ebc82efaSNicolas Royer 	ctx->flags |= SHA_FLAGS_SG;
849ebc82efaSNicolas Royer 
850ebc82efaSNicolas Royer 	/* next call does not fail... so no unmap in the case of error */
851d4905b38SNicolas Royer 	return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg), length, 0,
852ebc82efaSNicolas Royer 								0, final);
853ebc82efaSNicolas Royer }
854ebc82efaSNicolas Royer 
8554c977e37STudor Ambarus static void atmel_sha_update_dma_stop(struct atmel_sha_dev *dd)
856ebc82efaSNicolas Royer {
857ebc82efaSNicolas Royer 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
858ebc82efaSNicolas Royer 
859ebc82efaSNicolas Royer 	if (ctx->flags & SHA_FLAGS_SG) {
860ebc82efaSNicolas Royer 		dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE);
861ebc82efaSNicolas Royer 		if (ctx->sg->length == ctx->offset) {
862ebc82efaSNicolas Royer 			ctx->sg = sg_next(ctx->sg);
863ebc82efaSNicolas Royer 			if (ctx->sg)
864ebc82efaSNicolas Royer 				ctx->offset = 0;
865ebc82efaSNicolas Royer 		}
866d4905b38SNicolas Royer 		if (ctx->flags & SHA_FLAGS_PAD) {
867ebc82efaSNicolas Royer 			dma_unmap_single(dd->dev, ctx->dma_addr,
868d4905b38SNicolas Royer 				ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
869d4905b38SNicolas Royer 		}
870ebc82efaSNicolas Royer 	} else {
871ebc82efaSNicolas Royer 		dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen +
872d4905b38SNicolas Royer 						ctx->block_size, DMA_TO_DEVICE);
873ebc82efaSNicolas Royer 	}
874ebc82efaSNicolas Royer }
875ebc82efaSNicolas Royer 
876ebc82efaSNicolas Royer static int atmel_sha_update_req(struct atmel_sha_dev *dd)
877ebc82efaSNicolas Royer {
878ebc82efaSNicolas Royer 	struct ahash_request *req = dd->req;
879ebc82efaSNicolas Royer 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
880ebc82efaSNicolas Royer 	int err;
881ebc82efaSNicolas Royer 
882d4905b38SNicolas Royer 	dev_dbg(dd->dev, "update_req: total: %u, digcnt: 0x%llx 0x%llx\n",
883d4905b38SNicolas Royer 		ctx->total, ctx->digcnt[1], ctx->digcnt[0]);
884ebc82efaSNicolas Royer 
885ebc82efaSNicolas Royer 	if (ctx->flags & SHA_FLAGS_CPU)
886ebc82efaSNicolas Royer 		err = atmel_sha_update_cpu(dd);
887ebc82efaSNicolas Royer 	else
888ebc82efaSNicolas Royer 		err = atmel_sha_update_dma_start(dd);
889ebc82efaSNicolas Royer 
890ebc82efaSNicolas Royer 	/* wait for dma completion before can take more data */
891d4905b38SNicolas Royer 	dev_dbg(dd->dev, "update: err: %d, digcnt: 0x%llx 0%llx\n",
892d4905b38SNicolas Royer 			err, ctx->digcnt[1], ctx->digcnt[0]);
893ebc82efaSNicolas Royer 
894ebc82efaSNicolas Royer 	return err;
895ebc82efaSNicolas Royer }
896ebc82efaSNicolas Royer 
897ebc82efaSNicolas Royer static int atmel_sha_final_req(struct atmel_sha_dev *dd)
898ebc82efaSNicolas Royer {
899ebc82efaSNicolas Royer 	struct ahash_request *req = dd->req;
900ebc82efaSNicolas Royer 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
901ebc82efaSNicolas Royer 	int err = 0;
902ebc82efaSNicolas Royer 	int count;
903ebc82efaSNicolas Royer 
904ebc82efaSNicolas Royer 	if (ctx->bufcnt >= ATMEL_SHA_DMA_THRESHOLD) {
905ebc82efaSNicolas Royer 		atmel_sha_fill_padding(ctx, 0);
906ebc82efaSNicolas Royer 		count = ctx->bufcnt;
907ebc82efaSNicolas Royer 		ctx->bufcnt = 0;
908ebc82efaSNicolas Royer 		err = atmel_sha_xmit_dma_map(dd, ctx, count, 1);
909ebc82efaSNicolas Royer 	}
910ebc82efaSNicolas Royer 	/* faster to handle last block with cpu */
911ebc82efaSNicolas Royer 	else {
912ebc82efaSNicolas Royer 		atmel_sha_fill_padding(ctx, 0);
913ebc82efaSNicolas Royer 		count = ctx->bufcnt;
914ebc82efaSNicolas Royer 		ctx->bufcnt = 0;
915ebc82efaSNicolas Royer 		err = atmel_sha_xmit_cpu(dd, ctx->buffer, count, 1);
916ebc82efaSNicolas Royer 	}
917ebc82efaSNicolas Royer 
918ebc82efaSNicolas Royer 	dev_dbg(dd->dev, "final_req: err: %d\n", err);
919ebc82efaSNicolas Royer 
920ebc82efaSNicolas Royer 	return err;
921ebc82efaSNicolas Royer }
922ebc82efaSNicolas Royer 
923ebc82efaSNicolas Royer static void atmel_sha_copy_hash(struct ahash_request *req)
924ebc82efaSNicolas Royer {
925ebc82efaSNicolas Royer 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
926ebc82efaSNicolas Royer 	u32 *hash = (u32 *)ctx->digest;
9277cee3508SCyrille Pitchen 	unsigned int i, hashsize;
928ebc82efaSNicolas Royer 
9297cee3508SCyrille Pitchen 	switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
9307cee3508SCyrille Pitchen 	case SHA_FLAGS_SHA1:
9317cee3508SCyrille Pitchen 		hashsize = SHA1_DIGEST_SIZE;
9327cee3508SCyrille Pitchen 		break;
9337cee3508SCyrille Pitchen 
9347cee3508SCyrille Pitchen 	case SHA_FLAGS_SHA224:
9357cee3508SCyrille Pitchen 	case SHA_FLAGS_SHA256:
9367cee3508SCyrille Pitchen 		hashsize = SHA256_DIGEST_SIZE;
9377cee3508SCyrille Pitchen 		break;
9387cee3508SCyrille Pitchen 
9397cee3508SCyrille Pitchen 	case SHA_FLAGS_SHA384:
9407cee3508SCyrille Pitchen 	case SHA_FLAGS_SHA512:
9417cee3508SCyrille Pitchen 		hashsize = SHA512_DIGEST_SIZE;
9427cee3508SCyrille Pitchen 		break;
9437cee3508SCyrille Pitchen 
9447cee3508SCyrille Pitchen 	default:
9457cee3508SCyrille Pitchen 		/* Should not happen... */
9467cee3508SCyrille Pitchen 		return;
9477cee3508SCyrille Pitchen 	}
9487cee3508SCyrille Pitchen 
9497cee3508SCyrille Pitchen 	for (i = 0; i < hashsize / sizeof(u32); ++i)
950ebc82efaSNicolas Royer 		hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i));
9517cee3508SCyrille Pitchen 	ctx->flags |= SHA_FLAGS_RESTORE;
952ebc82efaSNicolas Royer }
953ebc82efaSNicolas Royer 
954ebc82efaSNicolas Royer static void atmel_sha_copy_ready_hash(struct ahash_request *req)
955ebc82efaSNicolas Royer {
956ebc82efaSNicolas Royer 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
957ebc82efaSNicolas Royer 
958ebc82efaSNicolas Royer 	if (!req->result)
959ebc82efaSNicolas Royer 		return;
960ebc82efaSNicolas Royer 
961f07cebadSCyrille Pitchen 	switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
962f07cebadSCyrille Pitchen 	default:
963f07cebadSCyrille Pitchen 	case SHA_FLAGS_SHA1:
964ebc82efaSNicolas Royer 		memcpy(req->result, ctx->digest, SHA1_DIGEST_SIZE);
965f07cebadSCyrille Pitchen 		break;
966f07cebadSCyrille Pitchen 
967f07cebadSCyrille Pitchen 	case SHA_FLAGS_SHA224:
968d4905b38SNicolas Royer 		memcpy(req->result, ctx->digest, SHA224_DIGEST_SIZE);
969f07cebadSCyrille Pitchen 		break;
970f07cebadSCyrille Pitchen 
971f07cebadSCyrille Pitchen 	case SHA_FLAGS_SHA256:
972ebc82efaSNicolas Royer 		memcpy(req->result, ctx->digest, SHA256_DIGEST_SIZE);
973f07cebadSCyrille Pitchen 		break;
974f07cebadSCyrille Pitchen 
975f07cebadSCyrille Pitchen 	case SHA_FLAGS_SHA384:
976d4905b38SNicolas Royer 		memcpy(req->result, ctx->digest, SHA384_DIGEST_SIZE);
977f07cebadSCyrille Pitchen 		break;
978f07cebadSCyrille Pitchen 
979f07cebadSCyrille Pitchen 	case SHA_FLAGS_SHA512:
980d4905b38SNicolas Royer 		memcpy(req->result, ctx->digest, SHA512_DIGEST_SIZE);
981f07cebadSCyrille Pitchen 		break;
982f07cebadSCyrille Pitchen 	}
983ebc82efaSNicolas Royer }
984ebc82efaSNicolas Royer 
985ebc82efaSNicolas Royer static int atmel_sha_finish(struct ahash_request *req)
986ebc82efaSNicolas Royer {
987ebc82efaSNicolas Royer 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
988ebc82efaSNicolas Royer 	struct atmel_sha_dev *dd = ctx->dd;
989ebc82efaSNicolas Royer 
990d4905b38SNicolas Royer 	if (ctx->digcnt[0] || ctx->digcnt[1])
991ebc82efaSNicolas Royer 		atmel_sha_copy_ready_hash(req);
992ebc82efaSNicolas Royer 
9934c147bcfSArnd Bergmann 	dev_dbg(dd->dev, "digcnt: 0x%llx 0x%llx, bufcnt: %zd\n", ctx->digcnt[1],
994d4905b38SNicolas Royer 		ctx->digcnt[0], ctx->bufcnt);
995ebc82efaSNicolas Royer 
996871b88a8SRahul Pathak 	return 0;
997ebc82efaSNicolas Royer }
998ebc82efaSNicolas Royer 
999ebc82efaSNicolas Royer static void atmel_sha_finish_req(struct ahash_request *req, int err)
1000ebc82efaSNicolas Royer {
1001ebc82efaSNicolas Royer 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1002ebc82efaSNicolas Royer 	struct atmel_sha_dev *dd = ctx->dd;
1003ebc82efaSNicolas Royer 
1004ebc82efaSNicolas Royer 	if (!err) {
1005ebc82efaSNicolas Royer 		atmel_sha_copy_hash(req);
1006ebc82efaSNicolas Royer 		if (SHA_FLAGS_FINAL & dd->flags)
1007ebc82efaSNicolas Royer 			err = atmel_sha_finish(req);
1008ebc82efaSNicolas Royer 	} else {
1009ebc82efaSNicolas Royer 		ctx->flags |= SHA_FLAGS_ERROR;
1010ebc82efaSNicolas Royer 	}
1011ebc82efaSNicolas Royer 
1012ebc82efaSNicolas Royer 	/* atomic operation is not needed here */
1013a29af939SCyrille Pitchen 	(void)atmel_sha_complete(dd, err);
1014ebc82efaSNicolas Royer }
1015ebc82efaSNicolas Royer 
1016ebc82efaSNicolas Royer static int atmel_sha_hw_init(struct atmel_sha_dev *dd)
1017ebc82efaSNicolas Royer {
10189d83d299SLABBE Corentin 	int err;
10199d83d299SLABBE Corentin 
1020c033042aSCyrille Pitchen 	err = clk_enable(dd->iclk);
10219d83d299SLABBE Corentin 	if (err)
10229d83d299SLABBE Corentin 		return err;
1023ebc82efaSNicolas Royer 
1024d4905b38SNicolas Royer 	if (!(SHA_FLAGS_INIT & dd->flags)) {
1025ebc82efaSNicolas Royer 		atmel_sha_write(dd, SHA_CR, SHA_CR_SWRST);
1026ebc82efaSNicolas Royer 		dd->flags |= SHA_FLAGS_INIT;
1027ebc82efaSNicolas Royer 	}
1028ebc82efaSNicolas Royer 
1029ebc82efaSNicolas Royer 	return 0;
1030ebc82efaSNicolas Royer }
1031ebc82efaSNicolas Royer 
1032d4905b38SNicolas Royer static inline unsigned int atmel_sha_get_version(struct atmel_sha_dev *dd)
1033d4905b38SNicolas Royer {
1034d4905b38SNicolas Royer 	return atmel_sha_read(dd, SHA_HW_VERSION) & 0x00000fff;
1035d4905b38SNicolas Royer }
1036d4905b38SNicolas Royer 
10370efe58f3STudor Ambarus static int atmel_sha_hw_version_init(struct atmel_sha_dev *dd)
1038d4905b38SNicolas Royer {
10390efe58f3STudor Ambarus 	int err;
10400efe58f3STudor Ambarus 
10410efe58f3STudor Ambarus 	err = atmel_sha_hw_init(dd);
10420efe58f3STudor Ambarus 	if (err)
10430efe58f3STudor Ambarus 		return err;
1044d4905b38SNicolas Royer 
1045d4905b38SNicolas Royer 	dd->hw_version = atmel_sha_get_version(dd);
1046d4905b38SNicolas Royer 
1047d4905b38SNicolas Royer 	dev_info(dd->dev,
1048d4905b38SNicolas Royer 			"version: 0x%x\n", dd->hw_version);
1049d4905b38SNicolas Royer 
1050c033042aSCyrille Pitchen 	clk_disable(dd->iclk);
10510efe58f3STudor Ambarus 
10520efe58f3STudor Ambarus 	return 0;
1053d4905b38SNicolas Royer }
1054d4905b38SNicolas Royer 
1055ebc82efaSNicolas Royer static int atmel_sha_handle_queue(struct atmel_sha_dev *dd,
1056ebc82efaSNicolas Royer 				  struct ahash_request *req)
1057ebc82efaSNicolas Royer {
1058ebc82efaSNicolas Royer 	struct crypto_async_request *async_req, *backlog;
1059a29af939SCyrille Pitchen 	struct atmel_sha_ctx *ctx;
1060ebc82efaSNicolas Royer 	unsigned long flags;
1061a29af939SCyrille Pitchen 	bool start_async;
1062ebc82efaSNicolas Royer 	int err = 0, ret = 0;
1063ebc82efaSNicolas Royer 
1064ebc82efaSNicolas Royer 	spin_lock_irqsave(&dd->lock, flags);
1065ebc82efaSNicolas Royer 	if (req)
1066ebc82efaSNicolas Royer 		ret = ahash_enqueue_request(&dd->queue, req);
1067ebc82efaSNicolas Royer 
1068ebc82efaSNicolas Royer 	if (SHA_FLAGS_BUSY & dd->flags) {
1069ebc82efaSNicolas Royer 		spin_unlock_irqrestore(&dd->lock, flags);
1070ebc82efaSNicolas Royer 		return ret;
1071ebc82efaSNicolas Royer 	}
1072ebc82efaSNicolas Royer 
1073ebc82efaSNicolas Royer 	backlog = crypto_get_backlog(&dd->queue);
1074ebc82efaSNicolas Royer 	async_req = crypto_dequeue_request(&dd->queue);
1075ebc82efaSNicolas Royer 	if (async_req)
1076ebc82efaSNicolas Royer 		dd->flags |= SHA_FLAGS_BUSY;
1077ebc82efaSNicolas Royer 
1078ebc82efaSNicolas Royer 	spin_unlock_irqrestore(&dd->lock, flags);
1079ebc82efaSNicolas Royer 
1080ebc82efaSNicolas Royer 	if (!async_req)
1081ebc82efaSNicolas Royer 		return ret;
1082ebc82efaSNicolas Royer 
1083ebc82efaSNicolas Royer 	if (backlog)
1084ebc82efaSNicolas Royer 		backlog->complete(backlog, -EINPROGRESS);
1085ebc82efaSNicolas Royer 
1086a29af939SCyrille Pitchen 	ctx = crypto_tfm_ctx(async_req->tfm);
1087a29af939SCyrille Pitchen 
1088a29af939SCyrille Pitchen 	dd->req = ahash_request_cast(async_req);
1089a29af939SCyrille Pitchen 	start_async = (dd->req != req);
1090a29af939SCyrille Pitchen 	dd->is_async = start_async;
109189a82ef8SCyrille Pitchen 	dd->force_complete = false;
1092a29af939SCyrille Pitchen 
1093a29af939SCyrille Pitchen 	/* WARNING: ctx->start() MAY change dd->is_async. */
1094a29af939SCyrille Pitchen 	err = ctx->start(dd);
1095a29af939SCyrille Pitchen 	return (start_async) ? ret : err;
1096a29af939SCyrille Pitchen }
1097a29af939SCyrille Pitchen 
1098b5ce82a7SCyrille Pitchen static int atmel_sha_done(struct atmel_sha_dev *dd);
1099b5ce82a7SCyrille Pitchen 
1100a29af939SCyrille Pitchen static int atmel_sha_start(struct atmel_sha_dev *dd)
1101a29af939SCyrille Pitchen {
1102a29af939SCyrille Pitchen 	struct ahash_request *req = dd->req;
1103a29af939SCyrille Pitchen 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1104a29af939SCyrille Pitchen 	int err;
1105ebc82efaSNicolas Royer 
1106ebc82efaSNicolas Royer 	dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n",
1107ebc82efaSNicolas Royer 						ctx->op, req->nbytes);
1108ebc82efaSNicolas Royer 
1109ebc82efaSNicolas Royer 	err = atmel_sha_hw_init(dd);
1110ebc82efaSNicolas Royer 	if (err)
111119998acbSCyrille Pitchen 		return atmel_sha_complete(dd, err);
111219998acbSCyrille Pitchen 
111319998acbSCyrille Pitchen 	/*
111419998acbSCyrille Pitchen 	 * atmel_sha_update_req() and atmel_sha_final_req() can return either:
111519998acbSCyrille Pitchen 	 *  -EINPROGRESS: the hardware is busy and the SHA driver will resume
111619998acbSCyrille Pitchen 	 *                its job later in the done_task.
111719998acbSCyrille Pitchen 	 *                This is the main path.
111819998acbSCyrille Pitchen 	 *
111919998acbSCyrille Pitchen 	 * 0: the SHA driver can continue its job then release the hardware
112019998acbSCyrille Pitchen 	 *    later, if needed, with atmel_sha_finish_req().
112119998acbSCyrille Pitchen 	 *    This is the alternate path.
112219998acbSCyrille Pitchen 	 *
112319998acbSCyrille Pitchen 	 * < 0: an error has occurred so atmel_sha_complete(dd, err) has already
112419998acbSCyrille Pitchen 	 *      been called, hence the hardware has been released.
112519998acbSCyrille Pitchen 	 *      The SHA driver must stop its job without calling
112619998acbSCyrille Pitchen 	 *      atmel_sha_finish_req(), otherwise atmel_sha_complete() would be
112719998acbSCyrille Pitchen 	 *      called a second time.
112819998acbSCyrille Pitchen 	 *
112919998acbSCyrille Pitchen 	 * Please note that currently, atmel_sha_final_req() never returns 0.
113019998acbSCyrille Pitchen 	 */
1131ebc82efaSNicolas Royer 
1132b5ce82a7SCyrille Pitchen 	dd->resume = atmel_sha_done;
1133ebc82efaSNicolas Royer 	if (ctx->op == SHA_OP_UPDATE) {
1134ebc82efaSNicolas Royer 		err = atmel_sha_update_req(dd);
113519998acbSCyrille Pitchen 		if (!err && (ctx->flags & SHA_FLAGS_FINUP))
1136ebc82efaSNicolas Royer 			/* no final() after finup() */
1137ebc82efaSNicolas Royer 			err = atmel_sha_final_req(dd);
1138ebc82efaSNicolas Royer 	} else if (ctx->op == SHA_OP_FINAL) {
1139ebc82efaSNicolas Royer 		err = atmel_sha_final_req(dd);
1140ebc82efaSNicolas Royer 	}
1141ebc82efaSNicolas Royer 
114219998acbSCyrille Pitchen 	if (!err)
1143ebc82efaSNicolas Royer 		/* done_task will not finish it, so do it here */
1144ebc82efaSNicolas Royer 		atmel_sha_finish_req(req, err);
1145ebc82efaSNicolas Royer 
1146ebc82efaSNicolas Royer 	dev_dbg(dd->dev, "exit, err: %d\n", err);
1147ebc82efaSNicolas Royer 
1148a29af939SCyrille Pitchen 	return err;
1149ebc82efaSNicolas Royer }
1150ebc82efaSNicolas Royer 
1151ebc82efaSNicolas Royer static int atmel_sha_enqueue(struct ahash_request *req, unsigned int op)
1152ebc82efaSNicolas Royer {
1153ebc82efaSNicolas Royer 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1154ebc82efaSNicolas Royer 	struct atmel_sha_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
1155ebc82efaSNicolas Royer 	struct atmel_sha_dev *dd = tctx->dd;
1156ebc82efaSNicolas Royer 
1157ebc82efaSNicolas Royer 	ctx->op = op;
1158ebc82efaSNicolas Royer 
1159ebc82efaSNicolas Royer 	return atmel_sha_handle_queue(dd, req);
1160ebc82efaSNicolas Royer }
1161ebc82efaSNicolas Royer 
1162ebc82efaSNicolas Royer static int atmel_sha_update(struct ahash_request *req)
1163ebc82efaSNicolas Royer {
1164ebc82efaSNicolas Royer 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1165ebc82efaSNicolas Royer 
1166ebc82efaSNicolas Royer 	if (!req->nbytes)
1167ebc82efaSNicolas Royer 		return 0;
1168ebc82efaSNicolas Royer 
1169ebc82efaSNicolas Royer 	ctx->total = req->nbytes;
1170ebc82efaSNicolas Royer 	ctx->sg = req->src;
1171ebc82efaSNicolas Royer 	ctx->offset = 0;
1172ebc82efaSNicolas Royer 
1173ebc82efaSNicolas Royer 	if (ctx->flags & SHA_FLAGS_FINUP) {
1174ebc82efaSNicolas Royer 		if (ctx->bufcnt + ctx->total < ATMEL_SHA_DMA_THRESHOLD)
1175ebc82efaSNicolas Royer 			/* faster to use CPU for short transfers */
1176ebc82efaSNicolas Royer 			ctx->flags |= SHA_FLAGS_CPU;
1177ebc82efaSNicolas Royer 	} else if (ctx->bufcnt + ctx->total < ctx->buflen) {
1178ebc82efaSNicolas Royer 		atmel_sha_append_sg(ctx);
1179ebc82efaSNicolas Royer 		return 0;
1180ebc82efaSNicolas Royer 	}
1181ebc82efaSNicolas Royer 	return atmel_sha_enqueue(req, SHA_OP_UPDATE);
1182ebc82efaSNicolas Royer }
1183ebc82efaSNicolas Royer 
1184ebc82efaSNicolas Royer static int atmel_sha_final(struct ahash_request *req)
1185ebc82efaSNicolas Royer {
1186ebc82efaSNicolas Royer 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1187ebc82efaSNicolas Royer 
1188ebc82efaSNicolas Royer 	ctx->flags |= SHA_FLAGS_FINUP;
1189ebc82efaSNicolas Royer 
1190ebc82efaSNicolas Royer 	if (ctx->flags & SHA_FLAGS_ERROR)
1191ebc82efaSNicolas Royer 		return 0; /* uncompleted hash is not needed */
1192ebc82efaSNicolas Royer 
1193ad84112aSCyrille Pitchen 	if (ctx->flags & SHA_FLAGS_PAD)
1194ebc82efaSNicolas Royer 		/* copy ready hash (+ finalize hmac) */
1195ebc82efaSNicolas Royer 		return atmel_sha_finish(req);
1196ebc82efaSNicolas Royer 
1197ad84112aSCyrille Pitchen 	return atmel_sha_enqueue(req, SHA_OP_FINAL);
1198ebc82efaSNicolas Royer }
1199ebc82efaSNicolas Royer 
1200ebc82efaSNicolas Royer static int atmel_sha_finup(struct ahash_request *req)
1201ebc82efaSNicolas Royer {
1202ebc82efaSNicolas Royer 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1203ebc82efaSNicolas Royer 	int err1, err2;
1204ebc82efaSNicolas Royer 
1205ebc82efaSNicolas Royer 	ctx->flags |= SHA_FLAGS_FINUP;
1206ebc82efaSNicolas Royer 
1207ebc82efaSNicolas Royer 	err1 = atmel_sha_update(req);
12081606043fSGilad Ben-Yossef 	if (err1 == -EINPROGRESS ||
12091606043fSGilad Ben-Yossef 	    (err1 == -EBUSY && (ahash_request_flags(req) &
12101606043fSGilad Ben-Yossef 				CRYPTO_TFM_REQ_MAY_BACKLOG)))
1211ebc82efaSNicolas Royer 		return err1;
1212ebc82efaSNicolas Royer 
1213ebc82efaSNicolas Royer 	/*
1214ebc82efaSNicolas Royer 	 * final() has to be always called to cleanup resources
1215ebc82efaSNicolas Royer 	 * even if udpate() failed, except EINPROGRESS
1216ebc82efaSNicolas Royer 	 */
1217ebc82efaSNicolas Royer 	err2 = atmel_sha_final(req);
1218ebc82efaSNicolas Royer 
1219ebc82efaSNicolas Royer 	return err1 ?: err2;
1220ebc82efaSNicolas Royer }
1221ebc82efaSNicolas Royer 
1222ebc82efaSNicolas Royer static int atmel_sha_digest(struct ahash_request *req)
1223ebc82efaSNicolas Royer {
1224ebc82efaSNicolas Royer 	return atmel_sha_init(req) ?: atmel_sha_finup(req);
1225ebc82efaSNicolas Royer }
1226ebc82efaSNicolas Royer 
1227cc831d32SCyrille Pitchen 
1228cc831d32SCyrille Pitchen static int atmel_sha_export(struct ahash_request *req, void *out)
1229cc831d32SCyrille Pitchen {
1230cc831d32SCyrille Pitchen 	const struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1231cc831d32SCyrille Pitchen 
12329c4274d9SCyrille Pitchen 	memcpy(out, ctx, sizeof(*ctx));
1233cc831d32SCyrille Pitchen 	return 0;
1234cc831d32SCyrille Pitchen }
1235cc831d32SCyrille Pitchen 
1236cc831d32SCyrille Pitchen static int atmel_sha_import(struct ahash_request *req, const void *in)
1237cc831d32SCyrille Pitchen {
1238cc831d32SCyrille Pitchen 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1239cc831d32SCyrille Pitchen 
12409c4274d9SCyrille Pitchen 	memcpy(ctx, in, sizeof(*ctx));
1241cc831d32SCyrille Pitchen 	return 0;
1242cc831d32SCyrille Pitchen }
1243cc831d32SCyrille Pitchen 
1244be95f0faSSvenning Sørensen static int atmel_sha_cra_init(struct crypto_tfm *tfm)
1245ebc82efaSNicolas Royer {
1246a29af939SCyrille Pitchen 	struct atmel_sha_ctx *ctx = crypto_tfm_ctx(tfm);
1247a29af939SCyrille Pitchen 
1248ebc82efaSNicolas Royer 	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
12499c4274d9SCyrille Pitchen 				 sizeof(struct atmel_sha_reqctx));
1250a29af939SCyrille Pitchen 	ctx->start = atmel_sha_start;
1251ebc82efaSNicolas Royer 
1252ebc82efaSNicolas Royer 	return 0;
1253ebc82efaSNicolas Royer }
1254ebc82efaSNicolas Royer 
1255aebe5bd7STudor Ambarus static void atmel_sha_alg_init(struct ahash_alg *alg)
1256aebe5bd7STudor Ambarus {
1257aebe5bd7STudor Ambarus 	alg->halg.base.cra_priority = ATMEL_SHA_PRIORITY;
1258aebe5bd7STudor Ambarus 	alg->halg.base.cra_flags = CRYPTO_ALG_ASYNC;
1259aebe5bd7STudor Ambarus 	alg->halg.base.cra_ctxsize = sizeof(struct atmel_sha_ctx);
1260aebe5bd7STudor Ambarus 	alg->halg.base.cra_module = THIS_MODULE;
1261aebe5bd7STudor Ambarus 	alg->halg.base.cra_init = atmel_sha_cra_init;
1262aebe5bd7STudor Ambarus 
1263aebe5bd7STudor Ambarus 	alg->halg.statesize = sizeof(struct atmel_sha_reqctx);
1264aebe5bd7STudor Ambarus 
1265aebe5bd7STudor Ambarus 	alg->init = atmel_sha_init;
1266aebe5bd7STudor Ambarus 	alg->update = atmel_sha_update;
1267aebe5bd7STudor Ambarus 	alg->final = atmel_sha_final;
1268aebe5bd7STudor Ambarus 	alg->finup = atmel_sha_finup;
1269aebe5bd7STudor Ambarus 	alg->digest = atmel_sha_digest;
1270aebe5bd7STudor Ambarus 	alg->export = atmel_sha_export;
1271aebe5bd7STudor Ambarus 	alg->import = atmel_sha_import;
1272aebe5bd7STudor Ambarus }
1273aebe5bd7STudor Ambarus 
1274d4905b38SNicolas Royer static struct ahash_alg sha_1_256_algs[] = {
1275ebc82efaSNicolas Royer {
1276aebe5bd7STudor Ambarus 	.halg.base.cra_name		= "sha1",
1277aebe5bd7STudor Ambarus 	.halg.base.cra_driver_name	= "atmel-sha1",
1278aebe5bd7STudor Ambarus 	.halg.base.cra_blocksize	= SHA1_BLOCK_SIZE,
1279aebe5bd7STudor Ambarus 
1280aebe5bd7STudor Ambarus 	.halg.digestsize = SHA1_DIGEST_SIZE,
1281ebc82efaSNicolas Royer },
1282ebc82efaSNicolas Royer {
1283aebe5bd7STudor Ambarus 	.halg.base.cra_name		= "sha256",
1284aebe5bd7STudor Ambarus 	.halg.base.cra_driver_name	= "atmel-sha256",
1285aebe5bd7STudor Ambarus 	.halg.base.cra_blocksize	= SHA256_BLOCK_SIZE,
1286aebe5bd7STudor Ambarus 
1287aebe5bd7STudor Ambarus 	.halg.digestsize = SHA256_DIGEST_SIZE,
1288ebc82efaSNicolas Royer },
1289ebc82efaSNicolas Royer };
1290ebc82efaSNicolas Royer 
1291d4905b38SNicolas Royer static struct ahash_alg sha_224_alg = {
1292aebe5bd7STudor Ambarus 	.halg.base.cra_name		= "sha224",
1293aebe5bd7STudor Ambarus 	.halg.base.cra_driver_name	= "atmel-sha224",
1294aebe5bd7STudor Ambarus 	.halg.base.cra_blocksize	= SHA224_BLOCK_SIZE,
1295aebe5bd7STudor Ambarus 
1296aebe5bd7STudor Ambarus 	.halg.digestsize = SHA224_DIGEST_SIZE,
1297d4905b38SNicolas Royer };
1298d4905b38SNicolas Royer 
1299d4905b38SNicolas Royer static struct ahash_alg sha_384_512_algs[] = {
1300d4905b38SNicolas Royer {
1301aebe5bd7STudor Ambarus 	.halg.base.cra_name		= "sha384",
1302aebe5bd7STudor Ambarus 	.halg.base.cra_driver_name	= "atmel-sha384",
1303aebe5bd7STudor Ambarus 	.halg.base.cra_blocksize	= SHA384_BLOCK_SIZE,
1304aebe5bd7STudor Ambarus 	.halg.base.cra_alignmask	= 0x3,
1305aebe5bd7STudor Ambarus 
1306aebe5bd7STudor Ambarus 	.halg.digestsize = SHA384_DIGEST_SIZE,
1307d4905b38SNicolas Royer },
1308d4905b38SNicolas Royer {
1309aebe5bd7STudor Ambarus 	.halg.base.cra_name		= "sha512",
1310aebe5bd7STudor Ambarus 	.halg.base.cra_driver_name	= "atmel-sha512",
1311aebe5bd7STudor Ambarus 	.halg.base.cra_blocksize	= SHA512_BLOCK_SIZE,
1312aebe5bd7STudor Ambarus 	.halg.base.cra_alignmask	= 0x3,
1313aebe5bd7STudor Ambarus 
1314aebe5bd7STudor Ambarus 	.halg.digestsize = SHA512_DIGEST_SIZE,
1315d4905b38SNicolas Royer },
1316d4905b38SNicolas Royer };
1317d4905b38SNicolas Royer 
1318f56809c3SCyrille Pitchen static void atmel_sha_queue_task(unsigned long data)
1319f56809c3SCyrille Pitchen {
1320f56809c3SCyrille Pitchen 	struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
1321f56809c3SCyrille Pitchen 
1322f56809c3SCyrille Pitchen 	atmel_sha_handle_queue(dd, NULL);
1323f56809c3SCyrille Pitchen }
1324f56809c3SCyrille Pitchen 
1325b5ce82a7SCyrille Pitchen static int atmel_sha_done(struct atmel_sha_dev *dd)
1326ebc82efaSNicolas Royer {
1327ebc82efaSNicolas Royer 	int err = 0;
1328ebc82efaSNicolas Royer 
1329ebc82efaSNicolas Royer 	if (SHA_FLAGS_CPU & dd->flags) {
1330ebc82efaSNicolas Royer 		if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
1331ebc82efaSNicolas Royer 			dd->flags &= ~SHA_FLAGS_OUTPUT_READY;
1332ebc82efaSNicolas Royer 			goto finish;
1333ebc82efaSNicolas Royer 		}
1334ebc82efaSNicolas Royer 	} else if (SHA_FLAGS_DMA_READY & dd->flags) {
1335ebc82efaSNicolas Royer 		if (SHA_FLAGS_DMA_ACTIVE & dd->flags) {
1336ebc82efaSNicolas Royer 			dd->flags &= ~SHA_FLAGS_DMA_ACTIVE;
1337ebc82efaSNicolas Royer 			atmel_sha_update_dma_stop(dd);
1338ebc82efaSNicolas Royer 		}
1339ebc82efaSNicolas Royer 		if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
1340ebc82efaSNicolas Royer 			/* hash or semi-hash ready */
1341ebc82efaSNicolas Royer 			dd->flags &= ~(SHA_FLAGS_DMA_READY |
1342ebc82efaSNicolas Royer 						SHA_FLAGS_OUTPUT_READY);
1343ebc82efaSNicolas Royer 			err = atmel_sha_update_dma_start(dd);
1344ebc82efaSNicolas Royer 			if (err != -EINPROGRESS)
1345ebc82efaSNicolas Royer 				goto finish;
1346ebc82efaSNicolas Royer 		}
1347ebc82efaSNicolas Royer 	}
1348b5ce82a7SCyrille Pitchen 	return err;
1349ebc82efaSNicolas Royer 
1350ebc82efaSNicolas Royer finish:
1351ebc82efaSNicolas Royer 	/* finish curent request */
1352ebc82efaSNicolas Royer 	atmel_sha_finish_req(dd->req, err);
1353b5ce82a7SCyrille Pitchen 
1354b5ce82a7SCyrille Pitchen 	return err;
1355b5ce82a7SCyrille Pitchen }
1356b5ce82a7SCyrille Pitchen 
1357b5ce82a7SCyrille Pitchen static void atmel_sha_done_task(unsigned long data)
1358b5ce82a7SCyrille Pitchen {
1359b5ce82a7SCyrille Pitchen 	struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
1360b5ce82a7SCyrille Pitchen 
1361b5ce82a7SCyrille Pitchen 	dd->is_async = true;
1362b5ce82a7SCyrille Pitchen 	(void)dd->resume(dd);
1363ebc82efaSNicolas Royer }
1364ebc82efaSNicolas Royer 
1365ebc82efaSNicolas Royer static irqreturn_t atmel_sha_irq(int irq, void *dev_id)
1366ebc82efaSNicolas Royer {
1367ebc82efaSNicolas Royer 	struct atmel_sha_dev *sha_dd = dev_id;
1368ebc82efaSNicolas Royer 	u32 reg;
1369ebc82efaSNicolas Royer 
1370ebc82efaSNicolas Royer 	reg = atmel_sha_read(sha_dd, SHA_ISR);
1371ebc82efaSNicolas Royer 	if (reg & atmel_sha_read(sha_dd, SHA_IMR)) {
1372ebc82efaSNicolas Royer 		atmel_sha_write(sha_dd, SHA_IDR, reg);
1373ebc82efaSNicolas Royer 		if (SHA_FLAGS_BUSY & sha_dd->flags) {
1374ebc82efaSNicolas Royer 			sha_dd->flags |= SHA_FLAGS_OUTPUT_READY;
1375ebc82efaSNicolas Royer 			if (!(SHA_FLAGS_CPU & sha_dd->flags))
1376ebc82efaSNicolas Royer 				sha_dd->flags |= SHA_FLAGS_DMA_READY;
1377ebc82efaSNicolas Royer 			tasklet_schedule(&sha_dd->done_task);
1378ebc82efaSNicolas Royer 		} else {
1379ebc82efaSNicolas Royer 			dev_warn(sha_dd->dev, "SHA interrupt when no active requests.\n");
1380ebc82efaSNicolas Royer 		}
1381ebc82efaSNicolas Royer 		return IRQ_HANDLED;
1382ebc82efaSNicolas Royer 	}
1383ebc82efaSNicolas Royer 
1384ebc82efaSNicolas Royer 	return IRQ_NONE;
1385ebc82efaSNicolas Royer }
1386ebc82efaSNicolas Royer 
1387eec12f66SCyrille Pitchen 
138869303cf0SCyrille Pitchen /* DMA transfer functions */
138969303cf0SCyrille Pitchen 
139069303cf0SCyrille Pitchen static bool atmel_sha_dma_check_aligned(struct atmel_sha_dev *dd,
139169303cf0SCyrille Pitchen 					struct scatterlist *sg,
139269303cf0SCyrille Pitchen 					size_t len)
139369303cf0SCyrille Pitchen {
139469303cf0SCyrille Pitchen 	struct atmel_sha_dma *dma = &dd->dma_lch_in;
139569303cf0SCyrille Pitchen 	struct ahash_request *req = dd->req;
139669303cf0SCyrille Pitchen 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
139769303cf0SCyrille Pitchen 	size_t bs = ctx->block_size;
139869303cf0SCyrille Pitchen 	int nents;
139969303cf0SCyrille Pitchen 
140069303cf0SCyrille Pitchen 	for (nents = 0; sg; sg = sg_next(sg), ++nents) {
140169303cf0SCyrille Pitchen 		if (!IS_ALIGNED(sg->offset, sizeof(u32)))
140269303cf0SCyrille Pitchen 			return false;
140369303cf0SCyrille Pitchen 
140469303cf0SCyrille Pitchen 		/*
140569303cf0SCyrille Pitchen 		 * This is the last sg, the only one that is allowed to
140669303cf0SCyrille Pitchen 		 * have an unaligned length.
140769303cf0SCyrille Pitchen 		 */
140869303cf0SCyrille Pitchen 		if (len <= sg->length) {
140969303cf0SCyrille Pitchen 			dma->nents = nents + 1;
141069303cf0SCyrille Pitchen 			dma->last_sg_length = sg->length;
141169303cf0SCyrille Pitchen 			sg->length = ALIGN(len, sizeof(u32));
141269303cf0SCyrille Pitchen 			return true;
141369303cf0SCyrille Pitchen 		}
141469303cf0SCyrille Pitchen 
141569303cf0SCyrille Pitchen 		/* All other sg lengths MUST be aligned to the block size. */
141669303cf0SCyrille Pitchen 		if (!IS_ALIGNED(sg->length, bs))
141769303cf0SCyrille Pitchen 			return false;
141869303cf0SCyrille Pitchen 
141969303cf0SCyrille Pitchen 		len -= sg->length;
142069303cf0SCyrille Pitchen 	}
142169303cf0SCyrille Pitchen 
142269303cf0SCyrille Pitchen 	return false;
142369303cf0SCyrille Pitchen }
142469303cf0SCyrille Pitchen 
142569303cf0SCyrille Pitchen static void atmel_sha_dma_callback2(void *data)
142669303cf0SCyrille Pitchen {
142769303cf0SCyrille Pitchen 	struct atmel_sha_dev *dd = data;
142869303cf0SCyrille Pitchen 	struct atmel_sha_dma *dma = &dd->dma_lch_in;
142969303cf0SCyrille Pitchen 	struct scatterlist *sg;
143069303cf0SCyrille Pitchen 	int nents;
143169303cf0SCyrille Pitchen 
143269303cf0SCyrille Pitchen 	dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
143369303cf0SCyrille Pitchen 
143469303cf0SCyrille Pitchen 	sg = dma->sg;
143569303cf0SCyrille Pitchen 	for (nents = 0; nents < dma->nents - 1; ++nents)
143669303cf0SCyrille Pitchen 		sg = sg_next(sg);
143769303cf0SCyrille Pitchen 	sg->length = dma->last_sg_length;
143869303cf0SCyrille Pitchen 
143969303cf0SCyrille Pitchen 	dd->is_async = true;
144069303cf0SCyrille Pitchen 	(void)atmel_sha_wait_for_data_ready(dd, dd->resume);
144169303cf0SCyrille Pitchen }
144269303cf0SCyrille Pitchen 
144369303cf0SCyrille Pitchen static int atmel_sha_dma_start(struct atmel_sha_dev *dd,
144469303cf0SCyrille Pitchen 			       struct scatterlist *src,
144569303cf0SCyrille Pitchen 			       size_t len,
144669303cf0SCyrille Pitchen 			       atmel_sha_fn_t resume)
144769303cf0SCyrille Pitchen {
144869303cf0SCyrille Pitchen 	struct atmel_sha_dma *dma = &dd->dma_lch_in;
144969303cf0SCyrille Pitchen 	struct dma_slave_config *config = &dma->dma_conf;
145069303cf0SCyrille Pitchen 	struct dma_chan *chan = dma->chan;
145169303cf0SCyrille Pitchen 	struct dma_async_tx_descriptor *desc;
145269303cf0SCyrille Pitchen 	dma_cookie_t cookie;
145369303cf0SCyrille Pitchen 	unsigned int sg_len;
145469303cf0SCyrille Pitchen 	int err;
145569303cf0SCyrille Pitchen 
145669303cf0SCyrille Pitchen 	dd->resume = resume;
145769303cf0SCyrille Pitchen 
145869303cf0SCyrille Pitchen 	/*
145969303cf0SCyrille Pitchen 	 * dma->nents has already been initialized by
146069303cf0SCyrille Pitchen 	 * atmel_sha_dma_check_aligned().
146169303cf0SCyrille Pitchen 	 */
146269303cf0SCyrille Pitchen 	dma->sg = src;
146369303cf0SCyrille Pitchen 	sg_len = dma_map_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
146469303cf0SCyrille Pitchen 	if (!sg_len) {
146569303cf0SCyrille Pitchen 		err = -ENOMEM;
146669303cf0SCyrille Pitchen 		goto exit;
146769303cf0SCyrille Pitchen 	}
146869303cf0SCyrille Pitchen 
146969303cf0SCyrille Pitchen 	config->src_maxburst = 16;
147069303cf0SCyrille Pitchen 	config->dst_maxburst = 16;
147169303cf0SCyrille Pitchen 	err = dmaengine_slave_config(chan, config);
147269303cf0SCyrille Pitchen 	if (err)
147369303cf0SCyrille Pitchen 		goto unmap_sg;
147469303cf0SCyrille Pitchen 
147569303cf0SCyrille Pitchen 	desc = dmaengine_prep_slave_sg(chan, dma->sg, sg_len, DMA_MEM_TO_DEV,
147669303cf0SCyrille Pitchen 				       DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
147769303cf0SCyrille Pitchen 	if (!desc) {
147869303cf0SCyrille Pitchen 		err = -ENOMEM;
147969303cf0SCyrille Pitchen 		goto unmap_sg;
148069303cf0SCyrille Pitchen 	}
148169303cf0SCyrille Pitchen 
148269303cf0SCyrille Pitchen 	desc->callback = atmel_sha_dma_callback2;
148369303cf0SCyrille Pitchen 	desc->callback_param = dd;
148469303cf0SCyrille Pitchen 	cookie = dmaengine_submit(desc);
148569303cf0SCyrille Pitchen 	err = dma_submit_error(cookie);
148669303cf0SCyrille Pitchen 	if (err)
148769303cf0SCyrille Pitchen 		goto unmap_sg;
148869303cf0SCyrille Pitchen 
148969303cf0SCyrille Pitchen 	dma_async_issue_pending(chan);
149069303cf0SCyrille Pitchen 
149169303cf0SCyrille Pitchen 	return -EINPROGRESS;
149269303cf0SCyrille Pitchen 
149369303cf0SCyrille Pitchen unmap_sg:
149469303cf0SCyrille Pitchen 	dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
149569303cf0SCyrille Pitchen exit:
149669303cf0SCyrille Pitchen 	return atmel_sha_complete(dd, err);
149769303cf0SCyrille Pitchen }
149869303cf0SCyrille Pitchen 
149969303cf0SCyrille Pitchen 
1500eec12f66SCyrille Pitchen /* CPU transfer functions */
1501eec12f66SCyrille Pitchen 
1502eec12f66SCyrille Pitchen static int atmel_sha_cpu_transfer(struct atmel_sha_dev *dd)
1503eec12f66SCyrille Pitchen {
1504eec12f66SCyrille Pitchen 	struct ahash_request *req = dd->req;
1505eec12f66SCyrille Pitchen 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1506eec12f66SCyrille Pitchen 	const u32 *words = (const u32 *)ctx->buffer;
1507eec12f66SCyrille Pitchen 	size_t i, num_words;
1508eec12f66SCyrille Pitchen 	u32 isr, din, din_inc;
1509eec12f66SCyrille Pitchen 
1510eec12f66SCyrille Pitchen 	din_inc = (ctx->flags & SHA_FLAGS_IDATAR0) ? 0 : 1;
1511eec12f66SCyrille Pitchen 	for (;;) {
1512eec12f66SCyrille Pitchen 		/* Write data into the Input Data Registers. */
1513eec12f66SCyrille Pitchen 		num_words = DIV_ROUND_UP(ctx->bufcnt, sizeof(u32));
1514eec12f66SCyrille Pitchen 		for (i = 0, din = 0; i < num_words; ++i, din += din_inc)
1515eec12f66SCyrille Pitchen 			atmel_sha_write(dd, SHA_REG_DIN(din), words[i]);
1516eec12f66SCyrille Pitchen 
1517eec12f66SCyrille Pitchen 		ctx->offset += ctx->bufcnt;
1518eec12f66SCyrille Pitchen 		ctx->total -= ctx->bufcnt;
1519eec12f66SCyrille Pitchen 
1520eec12f66SCyrille Pitchen 		if (!ctx->total)
1521eec12f66SCyrille Pitchen 			break;
1522eec12f66SCyrille Pitchen 
1523eec12f66SCyrille Pitchen 		/*
1524eec12f66SCyrille Pitchen 		 * Prepare next block:
1525eec12f66SCyrille Pitchen 		 * Fill ctx->buffer now with the next data to be written into
1526eec12f66SCyrille Pitchen 		 * IDATARx: it gives time for the SHA hardware to process
1527eec12f66SCyrille Pitchen 		 * the current data so the SHA_INT_DATARDY flag might be set
1528eec12f66SCyrille Pitchen 		 * in SHA_ISR when polling this register at the beginning of
1529eec12f66SCyrille Pitchen 		 * the next loop.
1530eec12f66SCyrille Pitchen 		 */
1531eec12f66SCyrille Pitchen 		ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total);
1532eec12f66SCyrille Pitchen 		scatterwalk_map_and_copy(ctx->buffer, ctx->sg,
1533eec12f66SCyrille Pitchen 					 ctx->offset, ctx->bufcnt, 0);
1534eec12f66SCyrille Pitchen 
1535eec12f66SCyrille Pitchen 		/* Wait for hardware to be ready again. */
1536eec12f66SCyrille Pitchen 		isr = atmel_sha_read(dd, SHA_ISR);
1537eec12f66SCyrille Pitchen 		if (!(isr & SHA_INT_DATARDY)) {
1538eec12f66SCyrille Pitchen 			/* Not ready yet. */
1539eec12f66SCyrille Pitchen 			dd->resume = atmel_sha_cpu_transfer;
1540eec12f66SCyrille Pitchen 			atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
1541eec12f66SCyrille Pitchen 			return -EINPROGRESS;
1542eec12f66SCyrille Pitchen 		}
1543eec12f66SCyrille Pitchen 	}
1544eec12f66SCyrille Pitchen 
1545eec12f66SCyrille Pitchen 	if (unlikely(!(ctx->flags & SHA_FLAGS_WAIT_DATARDY)))
1546eec12f66SCyrille Pitchen 		return dd->cpu_transfer_complete(dd);
1547eec12f66SCyrille Pitchen 
1548eec12f66SCyrille Pitchen 	return atmel_sha_wait_for_data_ready(dd, dd->cpu_transfer_complete);
1549eec12f66SCyrille Pitchen }
1550eec12f66SCyrille Pitchen 
1551eec12f66SCyrille Pitchen static int atmel_sha_cpu_start(struct atmel_sha_dev *dd,
1552eec12f66SCyrille Pitchen 			       struct scatterlist *sg,
1553eec12f66SCyrille Pitchen 			       unsigned int len,
1554eec12f66SCyrille Pitchen 			       bool idatar0_only,
1555eec12f66SCyrille Pitchen 			       bool wait_data_ready,
1556eec12f66SCyrille Pitchen 			       atmel_sha_fn_t resume)
1557eec12f66SCyrille Pitchen {
1558eec12f66SCyrille Pitchen 	struct ahash_request *req = dd->req;
1559eec12f66SCyrille Pitchen 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1560eec12f66SCyrille Pitchen 
1561eec12f66SCyrille Pitchen 	if (!len)
1562eec12f66SCyrille Pitchen 		return resume(dd);
1563eec12f66SCyrille Pitchen 
1564eec12f66SCyrille Pitchen 	ctx->flags &= ~(SHA_FLAGS_IDATAR0 | SHA_FLAGS_WAIT_DATARDY);
1565eec12f66SCyrille Pitchen 
1566eec12f66SCyrille Pitchen 	if (idatar0_only)
1567eec12f66SCyrille Pitchen 		ctx->flags |= SHA_FLAGS_IDATAR0;
1568eec12f66SCyrille Pitchen 
1569eec12f66SCyrille Pitchen 	if (wait_data_ready)
1570eec12f66SCyrille Pitchen 		ctx->flags |= SHA_FLAGS_WAIT_DATARDY;
1571eec12f66SCyrille Pitchen 
1572eec12f66SCyrille Pitchen 	ctx->sg = sg;
1573eec12f66SCyrille Pitchen 	ctx->total = len;
1574eec12f66SCyrille Pitchen 	ctx->offset = 0;
1575eec12f66SCyrille Pitchen 
1576eec12f66SCyrille Pitchen 	/* Prepare the first block to be written. */
1577eec12f66SCyrille Pitchen 	ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total);
1578eec12f66SCyrille Pitchen 	scatterwalk_map_and_copy(ctx->buffer, ctx->sg,
1579eec12f66SCyrille Pitchen 				 ctx->offset, ctx->bufcnt, 0);
1580eec12f66SCyrille Pitchen 
1581eec12f66SCyrille Pitchen 	dd->cpu_transfer_complete = resume;
1582eec12f66SCyrille Pitchen 	return atmel_sha_cpu_transfer(dd);
1583eec12f66SCyrille Pitchen }
1584eec12f66SCyrille Pitchen 
158581d8750bSCyrille Pitchen static int atmel_sha_cpu_hash(struct atmel_sha_dev *dd,
158681d8750bSCyrille Pitchen 			      const void *data, unsigned int datalen,
158781d8750bSCyrille Pitchen 			      bool auto_padding,
158881d8750bSCyrille Pitchen 			      atmel_sha_fn_t resume)
158981d8750bSCyrille Pitchen {
159081d8750bSCyrille Pitchen 	struct ahash_request *req = dd->req;
159181d8750bSCyrille Pitchen 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
159281d8750bSCyrille Pitchen 	u32 msglen = (auto_padding) ? datalen : 0;
159381d8750bSCyrille Pitchen 	u32 mr = SHA_MR_MODE_AUTO;
159481d8750bSCyrille Pitchen 
159581d8750bSCyrille Pitchen 	if (!(IS_ALIGNED(datalen, ctx->block_size) || auto_padding))
159681d8750bSCyrille Pitchen 		return atmel_sha_complete(dd, -EINVAL);
159781d8750bSCyrille Pitchen 
159881d8750bSCyrille Pitchen 	mr |= (ctx->flags & SHA_FLAGS_ALGO_MASK);
159981d8750bSCyrille Pitchen 	atmel_sha_write(dd, SHA_MR, mr);
160081d8750bSCyrille Pitchen 	atmel_sha_write(dd, SHA_MSR, msglen);
160181d8750bSCyrille Pitchen 	atmel_sha_write(dd, SHA_BCR, msglen);
160281d8750bSCyrille Pitchen 	atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
160381d8750bSCyrille Pitchen 
160481d8750bSCyrille Pitchen 	sg_init_one(&dd->tmp, data, datalen);
160581d8750bSCyrille Pitchen 	return atmel_sha_cpu_start(dd, &dd->tmp, datalen, false, true, resume);
160681d8750bSCyrille Pitchen }
160781d8750bSCyrille Pitchen 
160881d8750bSCyrille Pitchen 
160981d8750bSCyrille Pitchen /* hmac functions */
161081d8750bSCyrille Pitchen 
161181d8750bSCyrille Pitchen struct atmel_sha_hmac_key {
161281d8750bSCyrille Pitchen 	bool			valid;
161381d8750bSCyrille Pitchen 	unsigned int		keylen;
161481d8750bSCyrille Pitchen 	u8			buffer[SHA512_BLOCK_SIZE];
161581d8750bSCyrille Pitchen 	u8			*keydup;
161681d8750bSCyrille Pitchen };
161781d8750bSCyrille Pitchen 
161881d8750bSCyrille Pitchen static inline void atmel_sha_hmac_key_init(struct atmel_sha_hmac_key *hkey)
161981d8750bSCyrille Pitchen {
162081d8750bSCyrille Pitchen 	memset(hkey, 0, sizeof(*hkey));
162181d8750bSCyrille Pitchen }
162281d8750bSCyrille Pitchen 
162381d8750bSCyrille Pitchen static inline void atmel_sha_hmac_key_release(struct atmel_sha_hmac_key *hkey)
162481d8750bSCyrille Pitchen {
162581d8750bSCyrille Pitchen 	kfree(hkey->keydup);
162681d8750bSCyrille Pitchen 	memset(hkey, 0, sizeof(*hkey));
162781d8750bSCyrille Pitchen }
162881d8750bSCyrille Pitchen 
162981d8750bSCyrille Pitchen static inline int atmel_sha_hmac_key_set(struct atmel_sha_hmac_key *hkey,
163081d8750bSCyrille Pitchen 					 const u8 *key,
163181d8750bSCyrille Pitchen 					 unsigned int keylen)
163281d8750bSCyrille Pitchen {
163381d8750bSCyrille Pitchen 	atmel_sha_hmac_key_release(hkey);
163481d8750bSCyrille Pitchen 
163581d8750bSCyrille Pitchen 	if (keylen > sizeof(hkey->buffer)) {
163681d8750bSCyrille Pitchen 		hkey->keydup = kmemdup(key, keylen, GFP_KERNEL);
163781d8750bSCyrille Pitchen 		if (!hkey->keydup)
163881d8750bSCyrille Pitchen 			return -ENOMEM;
163981d8750bSCyrille Pitchen 
164081d8750bSCyrille Pitchen 	} else {
164181d8750bSCyrille Pitchen 		memcpy(hkey->buffer, key, keylen);
164281d8750bSCyrille Pitchen 	}
164381d8750bSCyrille Pitchen 
164481d8750bSCyrille Pitchen 	hkey->valid = true;
164581d8750bSCyrille Pitchen 	hkey->keylen = keylen;
164681d8750bSCyrille Pitchen 	return 0;
164781d8750bSCyrille Pitchen }
164881d8750bSCyrille Pitchen 
164981d8750bSCyrille Pitchen static inline bool atmel_sha_hmac_key_get(const struct atmel_sha_hmac_key *hkey,
165081d8750bSCyrille Pitchen 					  const u8 **key,
165181d8750bSCyrille Pitchen 					  unsigned int *keylen)
165281d8750bSCyrille Pitchen {
165381d8750bSCyrille Pitchen 	if (!hkey->valid)
165481d8750bSCyrille Pitchen 		return false;
165581d8750bSCyrille Pitchen 
165681d8750bSCyrille Pitchen 	*keylen = hkey->keylen;
165781d8750bSCyrille Pitchen 	*key = (hkey->keydup) ? hkey->keydup : hkey->buffer;
165881d8750bSCyrille Pitchen 	return true;
165981d8750bSCyrille Pitchen }
166081d8750bSCyrille Pitchen 
166181d8750bSCyrille Pitchen 
166281d8750bSCyrille Pitchen struct atmel_sha_hmac_ctx {
166381d8750bSCyrille Pitchen 	struct atmel_sha_ctx	base;
166481d8750bSCyrille Pitchen 
166581d8750bSCyrille Pitchen 	struct atmel_sha_hmac_key	hkey;
166681d8750bSCyrille Pitchen 	u32			ipad[SHA512_BLOCK_SIZE / sizeof(u32)];
166781d8750bSCyrille Pitchen 	u32			opad[SHA512_BLOCK_SIZE / sizeof(u32)];
166881d8750bSCyrille Pitchen 	atmel_sha_fn_t		resume;
166981d8750bSCyrille Pitchen };
167081d8750bSCyrille Pitchen 
167181d8750bSCyrille Pitchen static int atmel_sha_hmac_setup(struct atmel_sha_dev *dd,
167281d8750bSCyrille Pitchen 				atmel_sha_fn_t resume);
167381d8750bSCyrille Pitchen static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev *dd,
167481d8750bSCyrille Pitchen 				      const u8 *key, unsigned int keylen);
167581d8750bSCyrille Pitchen static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev *dd);
167681d8750bSCyrille Pitchen static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev *dd);
167781d8750bSCyrille Pitchen static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev *dd);
167881d8750bSCyrille Pitchen static int atmel_sha_hmac_setup_done(struct atmel_sha_dev *dd);
167981d8750bSCyrille Pitchen 
168081d8750bSCyrille Pitchen static int atmel_sha_hmac_init_done(struct atmel_sha_dev *dd);
168181d8750bSCyrille Pitchen static int atmel_sha_hmac_final(struct atmel_sha_dev *dd);
168281d8750bSCyrille Pitchen static int atmel_sha_hmac_final_done(struct atmel_sha_dev *dd);
168381d8750bSCyrille Pitchen static int atmel_sha_hmac_digest2(struct atmel_sha_dev *dd);
168481d8750bSCyrille Pitchen 
168581d8750bSCyrille Pitchen static int atmel_sha_hmac_setup(struct atmel_sha_dev *dd,
168681d8750bSCyrille Pitchen 				atmel_sha_fn_t resume)
168781d8750bSCyrille Pitchen {
168881d8750bSCyrille Pitchen 	struct ahash_request *req = dd->req;
168981d8750bSCyrille Pitchen 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
169081d8750bSCyrille Pitchen 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
169181d8750bSCyrille Pitchen 	struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
169281d8750bSCyrille Pitchen 	unsigned int keylen;
169381d8750bSCyrille Pitchen 	const u8 *key;
169481d8750bSCyrille Pitchen 	size_t bs;
169581d8750bSCyrille Pitchen 
169681d8750bSCyrille Pitchen 	hmac->resume = resume;
169781d8750bSCyrille Pitchen 	switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
169881d8750bSCyrille Pitchen 	case SHA_FLAGS_SHA1:
169981d8750bSCyrille Pitchen 		ctx->block_size = SHA1_BLOCK_SIZE;
170081d8750bSCyrille Pitchen 		ctx->hash_size = SHA1_DIGEST_SIZE;
170181d8750bSCyrille Pitchen 		break;
170281d8750bSCyrille Pitchen 
170381d8750bSCyrille Pitchen 	case SHA_FLAGS_SHA224:
170481d8750bSCyrille Pitchen 		ctx->block_size = SHA224_BLOCK_SIZE;
170581d8750bSCyrille Pitchen 		ctx->hash_size = SHA256_DIGEST_SIZE;
170681d8750bSCyrille Pitchen 		break;
170781d8750bSCyrille Pitchen 
170881d8750bSCyrille Pitchen 	case SHA_FLAGS_SHA256:
170981d8750bSCyrille Pitchen 		ctx->block_size = SHA256_BLOCK_SIZE;
171081d8750bSCyrille Pitchen 		ctx->hash_size = SHA256_DIGEST_SIZE;
171181d8750bSCyrille Pitchen 		break;
171281d8750bSCyrille Pitchen 
171381d8750bSCyrille Pitchen 	case SHA_FLAGS_SHA384:
171481d8750bSCyrille Pitchen 		ctx->block_size = SHA384_BLOCK_SIZE;
171581d8750bSCyrille Pitchen 		ctx->hash_size = SHA512_DIGEST_SIZE;
171681d8750bSCyrille Pitchen 		break;
171781d8750bSCyrille Pitchen 
171881d8750bSCyrille Pitchen 	case SHA_FLAGS_SHA512:
171981d8750bSCyrille Pitchen 		ctx->block_size = SHA512_BLOCK_SIZE;
172081d8750bSCyrille Pitchen 		ctx->hash_size = SHA512_DIGEST_SIZE;
172181d8750bSCyrille Pitchen 		break;
172281d8750bSCyrille Pitchen 
172381d8750bSCyrille Pitchen 	default:
172481d8750bSCyrille Pitchen 		return atmel_sha_complete(dd, -EINVAL);
172581d8750bSCyrille Pitchen 	}
172681d8750bSCyrille Pitchen 	bs = ctx->block_size;
172781d8750bSCyrille Pitchen 
172881d8750bSCyrille Pitchen 	if (likely(!atmel_sha_hmac_key_get(&hmac->hkey, &key, &keylen)))
172981d8750bSCyrille Pitchen 		return resume(dd);
173081d8750bSCyrille Pitchen 
173181d8750bSCyrille Pitchen 	/* Compute K' from K. */
173281d8750bSCyrille Pitchen 	if (unlikely(keylen > bs))
173381d8750bSCyrille Pitchen 		return atmel_sha_hmac_prehash_key(dd, key, keylen);
173481d8750bSCyrille Pitchen 
173581d8750bSCyrille Pitchen 	/* Prepare ipad. */
173681d8750bSCyrille Pitchen 	memcpy((u8 *)hmac->ipad, key, keylen);
173781d8750bSCyrille Pitchen 	memset((u8 *)hmac->ipad + keylen, 0, bs - keylen);
173881d8750bSCyrille Pitchen 	return atmel_sha_hmac_compute_ipad_hash(dd);
173981d8750bSCyrille Pitchen }
174081d8750bSCyrille Pitchen 
174181d8750bSCyrille Pitchen static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev *dd,
174281d8750bSCyrille Pitchen 				      const u8 *key, unsigned int keylen)
174381d8750bSCyrille Pitchen {
174481d8750bSCyrille Pitchen 	return atmel_sha_cpu_hash(dd, key, keylen, true,
174581d8750bSCyrille Pitchen 				  atmel_sha_hmac_prehash_key_done);
174681d8750bSCyrille Pitchen }
174781d8750bSCyrille Pitchen 
174881d8750bSCyrille Pitchen static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev *dd)
174981d8750bSCyrille Pitchen {
175081d8750bSCyrille Pitchen 	struct ahash_request *req = dd->req;
175181d8750bSCyrille Pitchen 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
175281d8750bSCyrille Pitchen 	struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
175381d8750bSCyrille Pitchen 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
175481d8750bSCyrille Pitchen 	size_t ds = crypto_ahash_digestsize(tfm);
175581d8750bSCyrille Pitchen 	size_t bs = ctx->block_size;
175681d8750bSCyrille Pitchen 	size_t i, num_words = ds / sizeof(u32);
175781d8750bSCyrille Pitchen 
175881d8750bSCyrille Pitchen 	/* Prepare ipad. */
175981d8750bSCyrille Pitchen 	for (i = 0; i < num_words; ++i)
176081d8750bSCyrille Pitchen 		hmac->ipad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
176181d8750bSCyrille Pitchen 	memset((u8 *)hmac->ipad + ds, 0, bs - ds);
176281d8750bSCyrille Pitchen 	return atmel_sha_hmac_compute_ipad_hash(dd);
176381d8750bSCyrille Pitchen }
176481d8750bSCyrille Pitchen 
176581d8750bSCyrille Pitchen static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev *dd)
176681d8750bSCyrille Pitchen {
176781d8750bSCyrille Pitchen 	struct ahash_request *req = dd->req;
176881d8750bSCyrille Pitchen 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
176981d8750bSCyrille Pitchen 	struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
177081d8750bSCyrille Pitchen 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
177181d8750bSCyrille Pitchen 	size_t bs = ctx->block_size;
177281d8750bSCyrille Pitchen 	size_t i, num_words = bs / sizeof(u32);
177381d8750bSCyrille Pitchen 
177481d8750bSCyrille Pitchen 	memcpy(hmac->opad, hmac->ipad, bs);
177581d8750bSCyrille Pitchen 	for (i = 0; i < num_words; ++i) {
177681d8750bSCyrille Pitchen 		hmac->ipad[i] ^= 0x36363636;
177781d8750bSCyrille Pitchen 		hmac->opad[i] ^= 0x5c5c5c5c;
177881d8750bSCyrille Pitchen 	}
177981d8750bSCyrille Pitchen 
178081d8750bSCyrille Pitchen 	return atmel_sha_cpu_hash(dd, hmac->ipad, bs, false,
178181d8750bSCyrille Pitchen 				  atmel_sha_hmac_compute_opad_hash);
178281d8750bSCyrille Pitchen }
178381d8750bSCyrille Pitchen 
178481d8750bSCyrille Pitchen static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev *dd)
178581d8750bSCyrille Pitchen {
178681d8750bSCyrille Pitchen 	struct ahash_request *req = dd->req;
178781d8750bSCyrille Pitchen 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
178881d8750bSCyrille Pitchen 	struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
178981d8750bSCyrille Pitchen 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
179081d8750bSCyrille Pitchen 	size_t bs = ctx->block_size;
179181d8750bSCyrille Pitchen 	size_t hs = ctx->hash_size;
179281d8750bSCyrille Pitchen 	size_t i, num_words = hs / sizeof(u32);
179381d8750bSCyrille Pitchen 
179481d8750bSCyrille Pitchen 	for (i = 0; i < num_words; ++i)
179581d8750bSCyrille Pitchen 		hmac->ipad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
179681d8750bSCyrille Pitchen 	return atmel_sha_cpu_hash(dd, hmac->opad, bs, false,
179781d8750bSCyrille Pitchen 				  atmel_sha_hmac_setup_done);
179881d8750bSCyrille Pitchen }
179981d8750bSCyrille Pitchen 
180081d8750bSCyrille Pitchen static int atmel_sha_hmac_setup_done(struct atmel_sha_dev *dd)
180181d8750bSCyrille Pitchen {
180281d8750bSCyrille Pitchen 	struct ahash_request *req = dd->req;
180381d8750bSCyrille Pitchen 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
180481d8750bSCyrille Pitchen 	struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
180581d8750bSCyrille Pitchen 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
180681d8750bSCyrille Pitchen 	size_t hs = ctx->hash_size;
180781d8750bSCyrille Pitchen 	size_t i, num_words = hs / sizeof(u32);
180881d8750bSCyrille Pitchen 
180981d8750bSCyrille Pitchen 	for (i = 0; i < num_words; ++i)
181081d8750bSCyrille Pitchen 		hmac->opad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
181181d8750bSCyrille Pitchen 	atmel_sha_hmac_key_release(&hmac->hkey);
181281d8750bSCyrille Pitchen 	return hmac->resume(dd);
181381d8750bSCyrille Pitchen }
181481d8750bSCyrille Pitchen 
181581d8750bSCyrille Pitchen static int atmel_sha_hmac_start(struct atmel_sha_dev *dd)
181681d8750bSCyrille Pitchen {
181781d8750bSCyrille Pitchen 	struct ahash_request *req = dd->req;
181881d8750bSCyrille Pitchen 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
181981d8750bSCyrille Pitchen 	int err;
182081d8750bSCyrille Pitchen 
182181d8750bSCyrille Pitchen 	err = atmel_sha_hw_init(dd);
182281d8750bSCyrille Pitchen 	if (err)
182381d8750bSCyrille Pitchen 		return atmel_sha_complete(dd, err);
182481d8750bSCyrille Pitchen 
182581d8750bSCyrille Pitchen 	switch (ctx->op) {
182681d8750bSCyrille Pitchen 	case SHA_OP_INIT:
182781d8750bSCyrille Pitchen 		err = atmel_sha_hmac_setup(dd, atmel_sha_hmac_init_done);
182881d8750bSCyrille Pitchen 		break;
182981d8750bSCyrille Pitchen 
183081d8750bSCyrille Pitchen 	case SHA_OP_UPDATE:
183181d8750bSCyrille Pitchen 		dd->resume = atmel_sha_done;
183281d8750bSCyrille Pitchen 		err = atmel_sha_update_req(dd);
183381d8750bSCyrille Pitchen 		break;
183481d8750bSCyrille Pitchen 
183581d8750bSCyrille Pitchen 	case SHA_OP_FINAL:
183681d8750bSCyrille Pitchen 		dd->resume = atmel_sha_hmac_final;
183781d8750bSCyrille Pitchen 		err = atmel_sha_final_req(dd);
183881d8750bSCyrille Pitchen 		break;
183981d8750bSCyrille Pitchen 
184081d8750bSCyrille Pitchen 	case SHA_OP_DIGEST:
184181d8750bSCyrille Pitchen 		err = atmel_sha_hmac_setup(dd, atmel_sha_hmac_digest2);
184281d8750bSCyrille Pitchen 		break;
184381d8750bSCyrille Pitchen 
184481d8750bSCyrille Pitchen 	default:
184581d8750bSCyrille Pitchen 		return atmel_sha_complete(dd, -EINVAL);
184681d8750bSCyrille Pitchen 	}
184781d8750bSCyrille Pitchen 
184881d8750bSCyrille Pitchen 	return err;
184981d8750bSCyrille Pitchen }
185081d8750bSCyrille Pitchen 
185181d8750bSCyrille Pitchen static int atmel_sha_hmac_setkey(struct crypto_ahash *tfm, const u8 *key,
185281d8750bSCyrille Pitchen 				 unsigned int keylen)
185381d8750bSCyrille Pitchen {
185481d8750bSCyrille Pitchen 	struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
185581d8750bSCyrille Pitchen 
1856b529f198SEric Biggers 	return atmel_sha_hmac_key_set(&hmac->hkey, key, keylen);
185781d8750bSCyrille Pitchen }
185881d8750bSCyrille Pitchen 
185981d8750bSCyrille Pitchen static int atmel_sha_hmac_init(struct ahash_request *req)
186081d8750bSCyrille Pitchen {
186181d8750bSCyrille Pitchen 	int err;
186281d8750bSCyrille Pitchen 
186381d8750bSCyrille Pitchen 	err = atmel_sha_init(req);
186481d8750bSCyrille Pitchen 	if (err)
186581d8750bSCyrille Pitchen 		return err;
186681d8750bSCyrille Pitchen 
186781d8750bSCyrille Pitchen 	return atmel_sha_enqueue(req, SHA_OP_INIT);
186881d8750bSCyrille Pitchen }
186981d8750bSCyrille Pitchen 
187081d8750bSCyrille Pitchen static int atmel_sha_hmac_init_done(struct atmel_sha_dev *dd)
187181d8750bSCyrille Pitchen {
187281d8750bSCyrille Pitchen 	struct ahash_request *req = dd->req;
187381d8750bSCyrille Pitchen 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
187481d8750bSCyrille Pitchen 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
187581d8750bSCyrille Pitchen 	struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
187681d8750bSCyrille Pitchen 	size_t bs = ctx->block_size;
187781d8750bSCyrille Pitchen 	size_t hs = ctx->hash_size;
187881d8750bSCyrille Pitchen 
187981d8750bSCyrille Pitchen 	ctx->bufcnt = 0;
188081d8750bSCyrille Pitchen 	ctx->digcnt[0] = bs;
188181d8750bSCyrille Pitchen 	ctx->digcnt[1] = 0;
188281d8750bSCyrille Pitchen 	ctx->flags |= SHA_FLAGS_RESTORE;
188381d8750bSCyrille Pitchen 	memcpy(ctx->digest, hmac->ipad, hs);
188481d8750bSCyrille Pitchen 	return atmel_sha_complete(dd, 0);
188581d8750bSCyrille Pitchen }
188681d8750bSCyrille Pitchen 
188781d8750bSCyrille Pitchen static int atmel_sha_hmac_final(struct atmel_sha_dev *dd)
188881d8750bSCyrille Pitchen {
188981d8750bSCyrille Pitchen 	struct ahash_request *req = dd->req;
189081d8750bSCyrille Pitchen 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
189181d8750bSCyrille Pitchen 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
189281d8750bSCyrille Pitchen 	struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
189381d8750bSCyrille Pitchen 	u32 *digest = (u32 *)ctx->digest;
189481d8750bSCyrille Pitchen 	size_t ds = crypto_ahash_digestsize(tfm);
189581d8750bSCyrille Pitchen 	size_t bs = ctx->block_size;
189681d8750bSCyrille Pitchen 	size_t hs = ctx->hash_size;
189781d8750bSCyrille Pitchen 	size_t i, num_words;
189881d8750bSCyrille Pitchen 	u32 mr;
189981d8750bSCyrille Pitchen 
190081d8750bSCyrille Pitchen 	/* Save d = SHA((K' + ipad) | msg). */
190181d8750bSCyrille Pitchen 	num_words = ds / sizeof(u32);
190281d8750bSCyrille Pitchen 	for (i = 0; i < num_words; ++i)
190381d8750bSCyrille Pitchen 		digest[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
190481d8750bSCyrille Pitchen 
190581d8750bSCyrille Pitchen 	/* Restore context to finish computing SHA((K' + opad) | d). */
190681d8750bSCyrille Pitchen 	atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
190781d8750bSCyrille Pitchen 	num_words = hs / sizeof(u32);
190881d8750bSCyrille Pitchen 	for (i = 0; i < num_words; ++i)
190981d8750bSCyrille Pitchen 		atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
191081d8750bSCyrille Pitchen 
191181d8750bSCyrille Pitchen 	mr = SHA_MR_MODE_AUTO | SHA_MR_UIHV;
191281d8750bSCyrille Pitchen 	mr |= (ctx->flags & SHA_FLAGS_ALGO_MASK);
191381d8750bSCyrille Pitchen 	atmel_sha_write(dd, SHA_MR, mr);
191481d8750bSCyrille Pitchen 	atmel_sha_write(dd, SHA_MSR, bs + ds);
191581d8750bSCyrille Pitchen 	atmel_sha_write(dd, SHA_BCR, ds);
191681d8750bSCyrille Pitchen 	atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
191781d8750bSCyrille Pitchen 
191881d8750bSCyrille Pitchen 	sg_init_one(&dd->tmp, digest, ds);
191981d8750bSCyrille Pitchen 	return atmel_sha_cpu_start(dd, &dd->tmp, ds, false, true,
192081d8750bSCyrille Pitchen 				   atmel_sha_hmac_final_done);
192181d8750bSCyrille Pitchen }
192281d8750bSCyrille Pitchen 
192381d8750bSCyrille Pitchen static int atmel_sha_hmac_final_done(struct atmel_sha_dev *dd)
192481d8750bSCyrille Pitchen {
192581d8750bSCyrille Pitchen 	/*
192681d8750bSCyrille Pitchen 	 * req->result might not be sizeof(u32) aligned, so copy the
192781d8750bSCyrille Pitchen 	 * digest into ctx->digest[] before memcpy() the data into
192881d8750bSCyrille Pitchen 	 * req->result.
192981d8750bSCyrille Pitchen 	 */
193081d8750bSCyrille Pitchen 	atmel_sha_copy_hash(dd->req);
193181d8750bSCyrille Pitchen 	atmel_sha_copy_ready_hash(dd->req);
193281d8750bSCyrille Pitchen 	return atmel_sha_complete(dd, 0);
193381d8750bSCyrille Pitchen }
193481d8750bSCyrille Pitchen 
193581d8750bSCyrille Pitchen static int atmel_sha_hmac_digest(struct ahash_request *req)
193681d8750bSCyrille Pitchen {
193781d8750bSCyrille Pitchen 	int err;
193881d8750bSCyrille Pitchen 
193981d8750bSCyrille Pitchen 	err = atmel_sha_init(req);
194081d8750bSCyrille Pitchen 	if (err)
194181d8750bSCyrille Pitchen 		return err;
194281d8750bSCyrille Pitchen 
194381d8750bSCyrille Pitchen 	return atmel_sha_enqueue(req, SHA_OP_DIGEST);
194481d8750bSCyrille Pitchen }
194581d8750bSCyrille Pitchen 
194681d8750bSCyrille Pitchen static int atmel_sha_hmac_digest2(struct atmel_sha_dev *dd)
194781d8750bSCyrille Pitchen {
194881d8750bSCyrille Pitchen 	struct ahash_request *req = dd->req;
194981d8750bSCyrille Pitchen 	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
195081d8750bSCyrille Pitchen 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
195181d8750bSCyrille Pitchen 	struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
195281d8750bSCyrille Pitchen 	size_t hs = ctx->hash_size;
195381d8750bSCyrille Pitchen 	size_t i, num_words = hs / sizeof(u32);
195481d8750bSCyrille Pitchen 	bool use_dma = false;
195581d8750bSCyrille Pitchen 	u32 mr;
195681d8750bSCyrille Pitchen 
195781d8750bSCyrille Pitchen 	/* Special case for empty message. */
195881d8750bSCyrille Pitchen 	if (!req->nbytes)
195981d8750bSCyrille Pitchen 		return atmel_sha_complete(dd, -EINVAL); // TODO:
196081d8750bSCyrille Pitchen 
196181d8750bSCyrille Pitchen 	/* Check DMA threshold and alignment. */
196281d8750bSCyrille Pitchen 	if (req->nbytes > ATMEL_SHA_DMA_THRESHOLD &&
196381d8750bSCyrille Pitchen 	    atmel_sha_dma_check_aligned(dd, req->src, req->nbytes))
196481d8750bSCyrille Pitchen 		use_dma = true;
196581d8750bSCyrille Pitchen 
196681d8750bSCyrille Pitchen 	/* Write both initial hash values to compute a HMAC. */
196781d8750bSCyrille Pitchen 	atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
196881d8750bSCyrille Pitchen 	for (i = 0; i < num_words; ++i)
196981d8750bSCyrille Pitchen 		atmel_sha_write(dd, SHA_REG_DIN(i), hmac->ipad[i]);
197081d8750bSCyrille Pitchen 
197181d8750bSCyrille Pitchen 	atmel_sha_write(dd, SHA_CR, SHA_CR_WUIEHV);
197281d8750bSCyrille Pitchen 	for (i = 0; i < num_words; ++i)
197381d8750bSCyrille Pitchen 		atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
197481d8750bSCyrille Pitchen 
197581d8750bSCyrille Pitchen 	/* Write the Mode, Message Size, Bytes Count then Control Registers. */
197681d8750bSCyrille Pitchen 	mr = (SHA_MR_HMAC | SHA_MR_DUALBUFF);
197781d8750bSCyrille Pitchen 	mr |= ctx->flags & SHA_FLAGS_ALGO_MASK;
197881d8750bSCyrille Pitchen 	if (use_dma)
197981d8750bSCyrille Pitchen 		mr |= SHA_MR_MODE_IDATAR0;
198081d8750bSCyrille Pitchen 	else
198181d8750bSCyrille Pitchen 		mr |= SHA_MR_MODE_AUTO;
198281d8750bSCyrille Pitchen 	atmel_sha_write(dd, SHA_MR, mr);
198381d8750bSCyrille Pitchen 
198481d8750bSCyrille Pitchen 	atmel_sha_write(dd, SHA_MSR, req->nbytes);
198581d8750bSCyrille Pitchen 	atmel_sha_write(dd, SHA_BCR, req->nbytes);
198681d8750bSCyrille Pitchen 
198781d8750bSCyrille Pitchen 	atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
198881d8750bSCyrille Pitchen 
198981d8750bSCyrille Pitchen 	/* Process data. */
199081d8750bSCyrille Pitchen 	if (use_dma)
199181d8750bSCyrille Pitchen 		return atmel_sha_dma_start(dd, req->src, req->nbytes,
199281d8750bSCyrille Pitchen 					   atmel_sha_hmac_final_done);
199381d8750bSCyrille Pitchen 
199481d8750bSCyrille Pitchen 	return atmel_sha_cpu_start(dd, req->src, req->nbytes, false, true,
199581d8750bSCyrille Pitchen 				   atmel_sha_hmac_final_done);
199681d8750bSCyrille Pitchen }
199781d8750bSCyrille Pitchen 
199881d8750bSCyrille Pitchen static int atmel_sha_hmac_cra_init(struct crypto_tfm *tfm)
199981d8750bSCyrille Pitchen {
200081d8750bSCyrille Pitchen 	struct atmel_sha_hmac_ctx *hmac = crypto_tfm_ctx(tfm);
200181d8750bSCyrille Pitchen 
200281d8750bSCyrille Pitchen 	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
200381d8750bSCyrille Pitchen 				 sizeof(struct atmel_sha_reqctx));
200481d8750bSCyrille Pitchen 	hmac->base.start = atmel_sha_hmac_start;
200581d8750bSCyrille Pitchen 	atmel_sha_hmac_key_init(&hmac->hkey);
200681d8750bSCyrille Pitchen 
200781d8750bSCyrille Pitchen 	return 0;
200881d8750bSCyrille Pitchen }
200981d8750bSCyrille Pitchen 
201081d8750bSCyrille Pitchen static void atmel_sha_hmac_cra_exit(struct crypto_tfm *tfm)
201181d8750bSCyrille Pitchen {
201281d8750bSCyrille Pitchen 	struct atmel_sha_hmac_ctx *hmac = crypto_tfm_ctx(tfm);
201381d8750bSCyrille Pitchen 
201481d8750bSCyrille Pitchen 	atmel_sha_hmac_key_release(&hmac->hkey);
201581d8750bSCyrille Pitchen }
201681d8750bSCyrille Pitchen 
2017aebe5bd7STudor Ambarus static void atmel_sha_hmac_alg_init(struct ahash_alg *alg)
2018aebe5bd7STudor Ambarus {
2019aebe5bd7STudor Ambarus 	alg->halg.base.cra_priority = ATMEL_SHA_PRIORITY;
2020aebe5bd7STudor Ambarus 	alg->halg.base.cra_flags = CRYPTO_ALG_ASYNC;
2021aebe5bd7STudor Ambarus 	alg->halg.base.cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx);
2022aebe5bd7STudor Ambarus 	alg->halg.base.cra_module = THIS_MODULE;
2023aebe5bd7STudor Ambarus 	alg->halg.base.cra_init	= atmel_sha_hmac_cra_init;
2024aebe5bd7STudor Ambarus 	alg->halg.base.cra_exit	= atmel_sha_hmac_cra_exit;
2025aebe5bd7STudor Ambarus 
2026aebe5bd7STudor Ambarus 	alg->halg.statesize = sizeof(struct atmel_sha_reqctx);
2027aebe5bd7STudor Ambarus 
2028aebe5bd7STudor Ambarus 	alg->init = atmel_sha_hmac_init;
2029aebe5bd7STudor Ambarus 	alg->update = atmel_sha_update;
2030aebe5bd7STudor Ambarus 	alg->final = atmel_sha_final;
2031aebe5bd7STudor Ambarus 	alg->digest = atmel_sha_hmac_digest;
2032aebe5bd7STudor Ambarus 	alg->setkey = atmel_sha_hmac_setkey;
2033aebe5bd7STudor Ambarus 	alg->export = atmel_sha_export;
2034aebe5bd7STudor Ambarus 	alg->import = atmel_sha_import;
2035aebe5bd7STudor Ambarus }
2036aebe5bd7STudor Ambarus 
203781d8750bSCyrille Pitchen static struct ahash_alg sha_hmac_algs[] = {
203881d8750bSCyrille Pitchen {
2039aebe5bd7STudor Ambarus 	.halg.base.cra_name		= "hmac(sha1)",
2040aebe5bd7STudor Ambarus 	.halg.base.cra_driver_name	= "atmel-hmac-sha1",
2041aebe5bd7STudor Ambarus 	.halg.base.cra_blocksize	= SHA1_BLOCK_SIZE,
2042aebe5bd7STudor Ambarus 
2043aebe5bd7STudor Ambarus 	.halg.digestsize = SHA1_DIGEST_SIZE,
204481d8750bSCyrille Pitchen },
204581d8750bSCyrille Pitchen {
2046aebe5bd7STudor Ambarus 	.halg.base.cra_name		= "hmac(sha224)",
2047aebe5bd7STudor Ambarus 	.halg.base.cra_driver_name	= "atmel-hmac-sha224",
2048aebe5bd7STudor Ambarus 	.halg.base.cra_blocksize	= SHA224_BLOCK_SIZE,
2049aebe5bd7STudor Ambarus 
2050aebe5bd7STudor Ambarus 	.halg.digestsize = SHA224_DIGEST_SIZE,
205181d8750bSCyrille Pitchen },
205281d8750bSCyrille Pitchen {
2053aebe5bd7STudor Ambarus 	.halg.base.cra_name		= "hmac(sha256)",
2054aebe5bd7STudor Ambarus 	.halg.base.cra_driver_name	= "atmel-hmac-sha256",
2055aebe5bd7STudor Ambarus 	.halg.base.cra_blocksize	= SHA256_BLOCK_SIZE,
2056aebe5bd7STudor Ambarus 
2057aebe5bd7STudor Ambarus 	.halg.digestsize = SHA256_DIGEST_SIZE,
205881d8750bSCyrille Pitchen },
205981d8750bSCyrille Pitchen {
2060aebe5bd7STudor Ambarus 	.halg.base.cra_name		= "hmac(sha384)",
2061aebe5bd7STudor Ambarus 	.halg.base.cra_driver_name	= "atmel-hmac-sha384",
2062aebe5bd7STudor Ambarus 	.halg.base.cra_blocksize	= SHA384_BLOCK_SIZE,
2063aebe5bd7STudor Ambarus 
2064aebe5bd7STudor Ambarus 	.halg.digestsize = SHA384_DIGEST_SIZE,
206581d8750bSCyrille Pitchen },
206681d8750bSCyrille Pitchen {
2067aebe5bd7STudor Ambarus 	.halg.base.cra_name		= "hmac(sha512)",
2068aebe5bd7STudor Ambarus 	.halg.base.cra_driver_name	= "atmel-hmac-sha512",
2069aebe5bd7STudor Ambarus 	.halg.base.cra_blocksize	= SHA512_BLOCK_SIZE,
2070aebe5bd7STudor Ambarus 
2071aebe5bd7STudor Ambarus 	.halg.digestsize = SHA512_DIGEST_SIZE,
207281d8750bSCyrille Pitchen },
207381d8750bSCyrille Pitchen };
2074eec12f66SCyrille Pitchen 
20751520c725SHerbert Xu #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
207689a82ef8SCyrille Pitchen /* authenc functions */
207789a82ef8SCyrille Pitchen 
207889a82ef8SCyrille Pitchen static int atmel_sha_authenc_init2(struct atmel_sha_dev *dd);
207989a82ef8SCyrille Pitchen static int atmel_sha_authenc_init_done(struct atmel_sha_dev *dd);
208089a82ef8SCyrille Pitchen static int atmel_sha_authenc_final_done(struct atmel_sha_dev *dd);
208189a82ef8SCyrille Pitchen 
208289a82ef8SCyrille Pitchen 
208389a82ef8SCyrille Pitchen struct atmel_sha_authenc_ctx {
208489a82ef8SCyrille Pitchen 	struct crypto_ahash	*tfm;
208589a82ef8SCyrille Pitchen };
208689a82ef8SCyrille Pitchen 
208789a82ef8SCyrille Pitchen struct atmel_sha_authenc_reqctx {
208889a82ef8SCyrille Pitchen 	struct atmel_sha_reqctx	base;
208989a82ef8SCyrille Pitchen 
209089a82ef8SCyrille Pitchen 	atmel_aes_authenc_fn_t	cb;
209189a82ef8SCyrille Pitchen 	struct atmel_aes_dev	*aes_dev;
209289a82ef8SCyrille Pitchen 
209389a82ef8SCyrille Pitchen 	/* _init() parameters. */
209489a82ef8SCyrille Pitchen 	struct scatterlist	*assoc;
209589a82ef8SCyrille Pitchen 	u32			assoclen;
209689a82ef8SCyrille Pitchen 	u32			textlen;
209789a82ef8SCyrille Pitchen 
209889a82ef8SCyrille Pitchen 	/* _final() parameters. */
209989a82ef8SCyrille Pitchen 	u32			*digest;
210089a82ef8SCyrille Pitchen 	unsigned int		digestlen;
210189a82ef8SCyrille Pitchen };
210289a82ef8SCyrille Pitchen 
210389a82ef8SCyrille Pitchen static void atmel_sha_authenc_complete(struct crypto_async_request *areq,
210489a82ef8SCyrille Pitchen 				       int err)
210589a82ef8SCyrille Pitchen {
210689a82ef8SCyrille Pitchen 	struct ahash_request *req = areq->data;
210789a82ef8SCyrille Pitchen 	struct atmel_sha_authenc_reqctx *authctx  = ahash_request_ctx(req);
210889a82ef8SCyrille Pitchen 
210989a82ef8SCyrille Pitchen 	authctx->cb(authctx->aes_dev, err, authctx->base.dd->is_async);
211089a82ef8SCyrille Pitchen }
211189a82ef8SCyrille Pitchen 
211289a82ef8SCyrille Pitchen static int atmel_sha_authenc_start(struct atmel_sha_dev *dd)
211389a82ef8SCyrille Pitchen {
211489a82ef8SCyrille Pitchen 	struct ahash_request *req = dd->req;
211589a82ef8SCyrille Pitchen 	struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
211689a82ef8SCyrille Pitchen 	int err;
211789a82ef8SCyrille Pitchen 
211889a82ef8SCyrille Pitchen 	/*
211989a82ef8SCyrille Pitchen 	 * Force atmel_sha_complete() to call req->base.complete(), ie
212089a82ef8SCyrille Pitchen 	 * atmel_sha_authenc_complete(), which in turn calls authctx->cb().
212189a82ef8SCyrille Pitchen 	 */
212289a82ef8SCyrille Pitchen 	dd->force_complete = true;
212389a82ef8SCyrille Pitchen 
212489a82ef8SCyrille Pitchen 	err = atmel_sha_hw_init(dd);
212589a82ef8SCyrille Pitchen 	return authctx->cb(authctx->aes_dev, err, dd->is_async);
212689a82ef8SCyrille Pitchen }
212789a82ef8SCyrille Pitchen 
212889a82ef8SCyrille Pitchen bool atmel_sha_authenc_is_ready(void)
212989a82ef8SCyrille Pitchen {
213089a82ef8SCyrille Pitchen 	struct atmel_sha_ctx dummy;
213189a82ef8SCyrille Pitchen 
213289a82ef8SCyrille Pitchen 	dummy.dd = NULL;
213389a82ef8SCyrille Pitchen 	return (atmel_sha_find_dev(&dummy) != NULL);
213489a82ef8SCyrille Pitchen }
213589a82ef8SCyrille Pitchen EXPORT_SYMBOL_GPL(atmel_sha_authenc_is_ready);
213689a82ef8SCyrille Pitchen 
213789a82ef8SCyrille Pitchen unsigned int atmel_sha_authenc_get_reqsize(void)
213889a82ef8SCyrille Pitchen {
213989a82ef8SCyrille Pitchen 	return sizeof(struct atmel_sha_authenc_reqctx);
214089a82ef8SCyrille Pitchen }
214189a82ef8SCyrille Pitchen EXPORT_SYMBOL_GPL(atmel_sha_authenc_get_reqsize);
214289a82ef8SCyrille Pitchen 
214389a82ef8SCyrille Pitchen struct atmel_sha_authenc_ctx *atmel_sha_authenc_spawn(unsigned long mode)
214489a82ef8SCyrille Pitchen {
214589a82ef8SCyrille Pitchen 	struct atmel_sha_authenc_ctx *auth;
214689a82ef8SCyrille Pitchen 	struct crypto_ahash *tfm;
214789a82ef8SCyrille Pitchen 	struct atmel_sha_ctx *tctx;
214889a82ef8SCyrille Pitchen 	const char *name;
214989a82ef8SCyrille Pitchen 	int err = -EINVAL;
215089a82ef8SCyrille Pitchen 
215189a82ef8SCyrille Pitchen 	switch (mode & SHA_FLAGS_MODE_MASK) {
215289a82ef8SCyrille Pitchen 	case SHA_FLAGS_HMAC_SHA1:
215389a82ef8SCyrille Pitchen 		name = "atmel-hmac-sha1";
215489a82ef8SCyrille Pitchen 		break;
215589a82ef8SCyrille Pitchen 
215689a82ef8SCyrille Pitchen 	case SHA_FLAGS_HMAC_SHA224:
215789a82ef8SCyrille Pitchen 		name = "atmel-hmac-sha224";
215889a82ef8SCyrille Pitchen 		break;
215989a82ef8SCyrille Pitchen 
216089a82ef8SCyrille Pitchen 	case SHA_FLAGS_HMAC_SHA256:
216189a82ef8SCyrille Pitchen 		name = "atmel-hmac-sha256";
216289a82ef8SCyrille Pitchen 		break;
216389a82ef8SCyrille Pitchen 
216489a82ef8SCyrille Pitchen 	case SHA_FLAGS_HMAC_SHA384:
216589a82ef8SCyrille Pitchen 		name = "atmel-hmac-sha384";
216689a82ef8SCyrille Pitchen 		break;
216789a82ef8SCyrille Pitchen 
216889a82ef8SCyrille Pitchen 	case SHA_FLAGS_HMAC_SHA512:
216989a82ef8SCyrille Pitchen 		name = "atmel-hmac-sha512";
217089a82ef8SCyrille Pitchen 		break;
217189a82ef8SCyrille Pitchen 
217289a82ef8SCyrille Pitchen 	default:
217389a82ef8SCyrille Pitchen 		goto error;
217489a82ef8SCyrille Pitchen 	}
217589a82ef8SCyrille Pitchen 
217685d7311fSEric Biggers 	tfm = crypto_alloc_ahash(name, 0, 0);
217789a82ef8SCyrille Pitchen 	if (IS_ERR(tfm)) {
217889a82ef8SCyrille Pitchen 		err = PTR_ERR(tfm);
217989a82ef8SCyrille Pitchen 		goto error;
218089a82ef8SCyrille Pitchen 	}
218189a82ef8SCyrille Pitchen 	tctx = crypto_ahash_ctx(tfm);
218289a82ef8SCyrille Pitchen 	tctx->start = atmel_sha_authenc_start;
218389a82ef8SCyrille Pitchen 	tctx->flags = mode;
218489a82ef8SCyrille Pitchen 
218589a82ef8SCyrille Pitchen 	auth = kzalloc(sizeof(*auth), GFP_KERNEL);
218689a82ef8SCyrille Pitchen 	if (!auth) {
218789a82ef8SCyrille Pitchen 		err = -ENOMEM;
218889a82ef8SCyrille Pitchen 		goto err_free_ahash;
218989a82ef8SCyrille Pitchen 	}
219089a82ef8SCyrille Pitchen 	auth->tfm = tfm;
219189a82ef8SCyrille Pitchen 
219289a82ef8SCyrille Pitchen 	return auth;
219389a82ef8SCyrille Pitchen 
219489a82ef8SCyrille Pitchen err_free_ahash:
219589a82ef8SCyrille Pitchen 	crypto_free_ahash(tfm);
219689a82ef8SCyrille Pitchen error:
219789a82ef8SCyrille Pitchen 	return ERR_PTR(err);
219889a82ef8SCyrille Pitchen }
219989a82ef8SCyrille Pitchen EXPORT_SYMBOL_GPL(atmel_sha_authenc_spawn);
220089a82ef8SCyrille Pitchen 
220189a82ef8SCyrille Pitchen void atmel_sha_authenc_free(struct atmel_sha_authenc_ctx *auth)
220289a82ef8SCyrille Pitchen {
220389a82ef8SCyrille Pitchen 	if (auth)
220489a82ef8SCyrille Pitchen 		crypto_free_ahash(auth->tfm);
220589a82ef8SCyrille Pitchen 	kfree(auth);
220689a82ef8SCyrille Pitchen }
220789a82ef8SCyrille Pitchen EXPORT_SYMBOL_GPL(atmel_sha_authenc_free);
220889a82ef8SCyrille Pitchen 
220989a82ef8SCyrille Pitchen int atmel_sha_authenc_setkey(struct atmel_sha_authenc_ctx *auth,
2210af5034e8SEric Biggers 			     const u8 *key, unsigned int keylen, u32 flags)
221189a82ef8SCyrille Pitchen {
221289a82ef8SCyrille Pitchen 	struct crypto_ahash *tfm = auth->tfm;
221389a82ef8SCyrille Pitchen 
221489a82ef8SCyrille Pitchen 	crypto_ahash_clear_flags(tfm, CRYPTO_TFM_REQ_MASK);
2215af5034e8SEric Biggers 	crypto_ahash_set_flags(tfm, flags & CRYPTO_TFM_REQ_MASK);
2216af5034e8SEric Biggers 	return crypto_ahash_setkey(tfm, key, keylen);
221789a82ef8SCyrille Pitchen }
221889a82ef8SCyrille Pitchen EXPORT_SYMBOL_GPL(atmel_sha_authenc_setkey);
221989a82ef8SCyrille Pitchen 
222089a82ef8SCyrille Pitchen int atmel_sha_authenc_schedule(struct ahash_request *req,
222189a82ef8SCyrille Pitchen 			       struct atmel_sha_authenc_ctx *auth,
222289a82ef8SCyrille Pitchen 			       atmel_aes_authenc_fn_t cb,
222389a82ef8SCyrille Pitchen 			       struct atmel_aes_dev *aes_dev)
222489a82ef8SCyrille Pitchen {
222589a82ef8SCyrille Pitchen 	struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
222689a82ef8SCyrille Pitchen 	struct atmel_sha_reqctx *ctx = &authctx->base;
222789a82ef8SCyrille Pitchen 	struct crypto_ahash *tfm = auth->tfm;
222889a82ef8SCyrille Pitchen 	struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm);
222989a82ef8SCyrille Pitchen 	struct atmel_sha_dev *dd;
223089a82ef8SCyrille Pitchen 
223189a82ef8SCyrille Pitchen 	/* Reset request context (MUST be done first). */
223289a82ef8SCyrille Pitchen 	memset(authctx, 0, sizeof(*authctx));
223389a82ef8SCyrille Pitchen 
223489a82ef8SCyrille Pitchen 	/* Get SHA device. */
223589a82ef8SCyrille Pitchen 	dd = atmel_sha_find_dev(tctx);
223689a82ef8SCyrille Pitchen 	if (!dd)
223789a82ef8SCyrille Pitchen 		return cb(aes_dev, -ENODEV, false);
223889a82ef8SCyrille Pitchen 
223989a82ef8SCyrille Pitchen 	/* Init request context. */
224089a82ef8SCyrille Pitchen 	ctx->dd = dd;
224189a82ef8SCyrille Pitchen 	ctx->buflen = SHA_BUFFER_LEN;
224289a82ef8SCyrille Pitchen 	authctx->cb = cb;
224389a82ef8SCyrille Pitchen 	authctx->aes_dev = aes_dev;
224489a82ef8SCyrille Pitchen 	ahash_request_set_tfm(req, tfm);
224589a82ef8SCyrille Pitchen 	ahash_request_set_callback(req, 0, atmel_sha_authenc_complete, req);
224689a82ef8SCyrille Pitchen 
224789a82ef8SCyrille Pitchen 	return atmel_sha_handle_queue(dd, req);
224889a82ef8SCyrille Pitchen }
224989a82ef8SCyrille Pitchen EXPORT_SYMBOL_GPL(atmel_sha_authenc_schedule);
225089a82ef8SCyrille Pitchen 
225189a82ef8SCyrille Pitchen int atmel_sha_authenc_init(struct ahash_request *req,
225289a82ef8SCyrille Pitchen 			   struct scatterlist *assoc, unsigned int assoclen,
225389a82ef8SCyrille Pitchen 			   unsigned int textlen,
225489a82ef8SCyrille Pitchen 			   atmel_aes_authenc_fn_t cb,
225589a82ef8SCyrille Pitchen 			   struct atmel_aes_dev *aes_dev)
225689a82ef8SCyrille Pitchen {
225789a82ef8SCyrille Pitchen 	struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
225889a82ef8SCyrille Pitchen 	struct atmel_sha_reqctx *ctx = &authctx->base;
225989a82ef8SCyrille Pitchen 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
226089a82ef8SCyrille Pitchen 	struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
226189a82ef8SCyrille Pitchen 	struct atmel_sha_dev *dd = ctx->dd;
226289a82ef8SCyrille Pitchen 
226389a82ef8SCyrille Pitchen 	if (unlikely(!IS_ALIGNED(assoclen, sizeof(u32))))
226489a82ef8SCyrille Pitchen 		return atmel_sha_complete(dd, -EINVAL);
226589a82ef8SCyrille Pitchen 
226689a82ef8SCyrille Pitchen 	authctx->cb = cb;
226789a82ef8SCyrille Pitchen 	authctx->aes_dev = aes_dev;
226889a82ef8SCyrille Pitchen 	authctx->assoc = assoc;
226989a82ef8SCyrille Pitchen 	authctx->assoclen = assoclen;
227089a82ef8SCyrille Pitchen 	authctx->textlen = textlen;
227189a82ef8SCyrille Pitchen 
227289a82ef8SCyrille Pitchen 	ctx->flags = hmac->base.flags;
227389a82ef8SCyrille Pitchen 	return atmel_sha_hmac_setup(dd, atmel_sha_authenc_init2);
227489a82ef8SCyrille Pitchen }
227589a82ef8SCyrille Pitchen EXPORT_SYMBOL_GPL(atmel_sha_authenc_init);
227689a82ef8SCyrille Pitchen 
227789a82ef8SCyrille Pitchen static int atmel_sha_authenc_init2(struct atmel_sha_dev *dd)
227889a82ef8SCyrille Pitchen {
227989a82ef8SCyrille Pitchen 	struct ahash_request *req = dd->req;
228089a82ef8SCyrille Pitchen 	struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
228189a82ef8SCyrille Pitchen 	struct atmel_sha_reqctx *ctx = &authctx->base;
228289a82ef8SCyrille Pitchen 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
228389a82ef8SCyrille Pitchen 	struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
228489a82ef8SCyrille Pitchen 	size_t hs = ctx->hash_size;
228589a82ef8SCyrille Pitchen 	size_t i, num_words = hs / sizeof(u32);
228689a82ef8SCyrille Pitchen 	u32 mr, msg_size;
228789a82ef8SCyrille Pitchen 
228889a82ef8SCyrille Pitchen 	atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
228989a82ef8SCyrille Pitchen 	for (i = 0; i < num_words; ++i)
229089a82ef8SCyrille Pitchen 		atmel_sha_write(dd, SHA_REG_DIN(i), hmac->ipad[i]);
229189a82ef8SCyrille Pitchen 
229289a82ef8SCyrille Pitchen 	atmel_sha_write(dd, SHA_CR, SHA_CR_WUIEHV);
229389a82ef8SCyrille Pitchen 	for (i = 0; i < num_words; ++i)
229489a82ef8SCyrille Pitchen 		atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
229589a82ef8SCyrille Pitchen 
229689a82ef8SCyrille Pitchen 	mr = (SHA_MR_MODE_IDATAR0 |
229789a82ef8SCyrille Pitchen 	      SHA_MR_HMAC |
229889a82ef8SCyrille Pitchen 	      SHA_MR_DUALBUFF);
229989a82ef8SCyrille Pitchen 	mr |= ctx->flags & SHA_FLAGS_ALGO_MASK;
230089a82ef8SCyrille Pitchen 	atmel_sha_write(dd, SHA_MR, mr);
230189a82ef8SCyrille Pitchen 
230289a82ef8SCyrille Pitchen 	msg_size = authctx->assoclen + authctx->textlen;
230389a82ef8SCyrille Pitchen 	atmel_sha_write(dd, SHA_MSR, msg_size);
230489a82ef8SCyrille Pitchen 	atmel_sha_write(dd, SHA_BCR, msg_size);
230589a82ef8SCyrille Pitchen 
230689a82ef8SCyrille Pitchen 	atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
230789a82ef8SCyrille Pitchen 
230889a82ef8SCyrille Pitchen 	/* Process assoc data. */
230989a82ef8SCyrille Pitchen 	return atmel_sha_cpu_start(dd, authctx->assoc, authctx->assoclen,
231089a82ef8SCyrille Pitchen 				   true, false,
231189a82ef8SCyrille Pitchen 				   atmel_sha_authenc_init_done);
231289a82ef8SCyrille Pitchen }
231389a82ef8SCyrille Pitchen 
231489a82ef8SCyrille Pitchen static int atmel_sha_authenc_init_done(struct atmel_sha_dev *dd)
231589a82ef8SCyrille Pitchen {
231689a82ef8SCyrille Pitchen 	struct ahash_request *req = dd->req;
231789a82ef8SCyrille Pitchen 	struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
231889a82ef8SCyrille Pitchen 
231989a82ef8SCyrille Pitchen 	return authctx->cb(authctx->aes_dev, 0, dd->is_async);
232089a82ef8SCyrille Pitchen }
232189a82ef8SCyrille Pitchen 
232289a82ef8SCyrille Pitchen int atmel_sha_authenc_final(struct ahash_request *req,
232389a82ef8SCyrille Pitchen 			    u32 *digest, unsigned int digestlen,
232489a82ef8SCyrille Pitchen 			    atmel_aes_authenc_fn_t cb,
232589a82ef8SCyrille Pitchen 			    struct atmel_aes_dev *aes_dev)
232689a82ef8SCyrille Pitchen {
232789a82ef8SCyrille Pitchen 	struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
232889a82ef8SCyrille Pitchen 	struct atmel_sha_reqctx *ctx = &authctx->base;
232989a82ef8SCyrille Pitchen 	struct atmel_sha_dev *dd = ctx->dd;
233089a82ef8SCyrille Pitchen 
233189a82ef8SCyrille Pitchen 	switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
233289a82ef8SCyrille Pitchen 	case SHA_FLAGS_SHA1:
233389a82ef8SCyrille Pitchen 		authctx->digestlen = SHA1_DIGEST_SIZE;
233489a82ef8SCyrille Pitchen 		break;
233589a82ef8SCyrille Pitchen 
233689a82ef8SCyrille Pitchen 	case SHA_FLAGS_SHA224:
233789a82ef8SCyrille Pitchen 		authctx->digestlen = SHA224_DIGEST_SIZE;
233889a82ef8SCyrille Pitchen 		break;
233989a82ef8SCyrille Pitchen 
234089a82ef8SCyrille Pitchen 	case SHA_FLAGS_SHA256:
234189a82ef8SCyrille Pitchen 		authctx->digestlen = SHA256_DIGEST_SIZE;
234289a82ef8SCyrille Pitchen 		break;
234389a82ef8SCyrille Pitchen 
234489a82ef8SCyrille Pitchen 	case SHA_FLAGS_SHA384:
234589a82ef8SCyrille Pitchen 		authctx->digestlen = SHA384_DIGEST_SIZE;
234689a82ef8SCyrille Pitchen 		break;
234789a82ef8SCyrille Pitchen 
234889a82ef8SCyrille Pitchen 	case SHA_FLAGS_SHA512:
234989a82ef8SCyrille Pitchen 		authctx->digestlen = SHA512_DIGEST_SIZE;
235089a82ef8SCyrille Pitchen 		break;
235189a82ef8SCyrille Pitchen 
235289a82ef8SCyrille Pitchen 	default:
235389a82ef8SCyrille Pitchen 		return atmel_sha_complete(dd, -EINVAL);
235489a82ef8SCyrille Pitchen 	}
235589a82ef8SCyrille Pitchen 	if (authctx->digestlen > digestlen)
235689a82ef8SCyrille Pitchen 		authctx->digestlen = digestlen;
235789a82ef8SCyrille Pitchen 
235889a82ef8SCyrille Pitchen 	authctx->cb = cb;
235989a82ef8SCyrille Pitchen 	authctx->aes_dev = aes_dev;
236089a82ef8SCyrille Pitchen 	authctx->digest = digest;
236189a82ef8SCyrille Pitchen 	return atmel_sha_wait_for_data_ready(dd,
236289a82ef8SCyrille Pitchen 					     atmel_sha_authenc_final_done);
236389a82ef8SCyrille Pitchen }
236489a82ef8SCyrille Pitchen EXPORT_SYMBOL_GPL(atmel_sha_authenc_final);
236589a82ef8SCyrille Pitchen 
236689a82ef8SCyrille Pitchen static int atmel_sha_authenc_final_done(struct atmel_sha_dev *dd)
236789a82ef8SCyrille Pitchen {
236889a82ef8SCyrille Pitchen 	struct ahash_request *req = dd->req;
236989a82ef8SCyrille Pitchen 	struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
237089a82ef8SCyrille Pitchen 	size_t i, num_words = authctx->digestlen / sizeof(u32);
237189a82ef8SCyrille Pitchen 
237289a82ef8SCyrille Pitchen 	for (i = 0; i < num_words; ++i)
237389a82ef8SCyrille Pitchen 		authctx->digest[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
237489a82ef8SCyrille Pitchen 
237589a82ef8SCyrille Pitchen 	return atmel_sha_complete(dd, 0);
237689a82ef8SCyrille Pitchen }
237789a82ef8SCyrille Pitchen 
237889a82ef8SCyrille Pitchen void atmel_sha_authenc_abort(struct ahash_request *req)
237989a82ef8SCyrille Pitchen {
238089a82ef8SCyrille Pitchen 	struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
238189a82ef8SCyrille Pitchen 	struct atmel_sha_reqctx *ctx = &authctx->base;
238289a82ef8SCyrille Pitchen 	struct atmel_sha_dev *dd = ctx->dd;
238389a82ef8SCyrille Pitchen 
238489a82ef8SCyrille Pitchen 	/* Prevent atmel_sha_complete() from calling req->base.complete(). */
238589a82ef8SCyrille Pitchen 	dd->is_async = false;
238689a82ef8SCyrille Pitchen 	dd->force_complete = false;
238789a82ef8SCyrille Pitchen 	(void)atmel_sha_complete(dd, 0);
238889a82ef8SCyrille Pitchen }
238989a82ef8SCyrille Pitchen EXPORT_SYMBOL_GPL(atmel_sha_authenc_abort);
239089a82ef8SCyrille Pitchen 
239189a82ef8SCyrille Pitchen #endif /* CONFIG_CRYPTO_DEV_ATMEL_AUTHENC */
239289a82ef8SCyrille Pitchen 
239389a82ef8SCyrille Pitchen 
2394ebc82efaSNicolas Royer static void atmel_sha_unregister_algs(struct atmel_sha_dev *dd)
2395ebc82efaSNicolas Royer {
2396ebc82efaSNicolas Royer 	int i;
2397ebc82efaSNicolas Royer 
239881d8750bSCyrille Pitchen 	if (dd->caps.has_hmac)
239981d8750bSCyrille Pitchen 		for (i = 0; i < ARRAY_SIZE(sha_hmac_algs); i++)
240081d8750bSCyrille Pitchen 			crypto_unregister_ahash(&sha_hmac_algs[i]);
240181d8750bSCyrille Pitchen 
2402d4905b38SNicolas Royer 	for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++)
2403d4905b38SNicolas Royer 		crypto_unregister_ahash(&sha_1_256_algs[i]);
2404d4905b38SNicolas Royer 
2405d4905b38SNicolas Royer 	if (dd->caps.has_sha224)
2406d4905b38SNicolas Royer 		crypto_unregister_ahash(&sha_224_alg);
2407d4905b38SNicolas Royer 
2408d4905b38SNicolas Royer 	if (dd->caps.has_sha_384_512) {
2409d4905b38SNicolas Royer 		for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++)
2410d4905b38SNicolas Royer 			crypto_unregister_ahash(&sha_384_512_algs[i]);
2411d4905b38SNicolas Royer 	}
2412ebc82efaSNicolas Royer }
2413ebc82efaSNicolas Royer 
2414ebc82efaSNicolas Royer static int atmel_sha_register_algs(struct atmel_sha_dev *dd)
2415ebc82efaSNicolas Royer {
2416ebc82efaSNicolas Royer 	int err, i, j;
2417ebc82efaSNicolas Royer 
2418d4905b38SNicolas Royer 	for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++) {
2419aebe5bd7STudor Ambarus 		atmel_sha_alg_init(&sha_1_256_algs[i]);
2420aebe5bd7STudor Ambarus 
2421d4905b38SNicolas Royer 		err = crypto_register_ahash(&sha_1_256_algs[i]);
2422ebc82efaSNicolas Royer 		if (err)
2423d4905b38SNicolas Royer 			goto err_sha_1_256_algs;
2424d4905b38SNicolas Royer 	}
2425d4905b38SNicolas Royer 
2426d4905b38SNicolas Royer 	if (dd->caps.has_sha224) {
2427aebe5bd7STudor Ambarus 		atmel_sha_alg_init(&sha_224_alg);
2428aebe5bd7STudor Ambarus 
2429d4905b38SNicolas Royer 		err = crypto_register_ahash(&sha_224_alg);
2430d4905b38SNicolas Royer 		if (err)
2431d4905b38SNicolas Royer 			goto err_sha_224_algs;
2432d4905b38SNicolas Royer 	}
2433d4905b38SNicolas Royer 
2434d4905b38SNicolas Royer 	if (dd->caps.has_sha_384_512) {
2435d4905b38SNicolas Royer 		for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++) {
2436aebe5bd7STudor Ambarus 			atmel_sha_alg_init(&sha_384_512_algs[i]);
2437aebe5bd7STudor Ambarus 
2438d4905b38SNicolas Royer 			err = crypto_register_ahash(&sha_384_512_algs[i]);
2439d4905b38SNicolas Royer 			if (err)
2440d4905b38SNicolas Royer 				goto err_sha_384_512_algs;
2441d4905b38SNicolas Royer 		}
2442ebc82efaSNicolas Royer 	}
2443ebc82efaSNicolas Royer 
244481d8750bSCyrille Pitchen 	if (dd->caps.has_hmac) {
244581d8750bSCyrille Pitchen 		for (i = 0; i < ARRAY_SIZE(sha_hmac_algs); i++) {
2446aebe5bd7STudor Ambarus 			atmel_sha_hmac_alg_init(&sha_hmac_algs[i]);
2447aebe5bd7STudor Ambarus 
244881d8750bSCyrille Pitchen 			err = crypto_register_ahash(&sha_hmac_algs[i]);
244981d8750bSCyrille Pitchen 			if (err)
245081d8750bSCyrille Pitchen 				goto err_sha_hmac_algs;
245181d8750bSCyrille Pitchen 		}
245281d8750bSCyrille Pitchen 	}
245381d8750bSCyrille Pitchen 
2454ebc82efaSNicolas Royer 	return 0;
2455ebc82efaSNicolas Royer 
245681d8750bSCyrille Pitchen 	/*i = ARRAY_SIZE(sha_hmac_algs);*/
245781d8750bSCyrille Pitchen err_sha_hmac_algs:
245881d8750bSCyrille Pitchen 	for (j = 0; j < i; j++)
245981d8750bSCyrille Pitchen 		crypto_unregister_ahash(&sha_hmac_algs[j]);
246081d8750bSCyrille Pitchen 	i = ARRAY_SIZE(sha_384_512_algs);
2461d4905b38SNicolas Royer err_sha_384_512_algs:
2462ebc82efaSNicolas Royer 	for (j = 0; j < i; j++)
2463d4905b38SNicolas Royer 		crypto_unregister_ahash(&sha_384_512_algs[j]);
2464d4905b38SNicolas Royer 	crypto_unregister_ahash(&sha_224_alg);
2465d4905b38SNicolas Royer err_sha_224_algs:
2466d4905b38SNicolas Royer 	i = ARRAY_SIZE(sha_1_256_algs);
2467d4905b38SNicolas Royer err_sha_1_256_algs:
2468d4905b38SNicolas Royer 	for (j = 0; j < i; j++)
2469d4905b38SNicolas Royer 		crypto_unregister_ahash(&sha_1_256_algs[j]);
2470ebc82efaSNicolas Royer 
2471ebc82efaSNicolas Royer 	return err;
2472ebc82efaSNicolas Royer }
2473ebc82efaSNicolas Royer 
2474827a98dfSTudor Ambarus static int atmel_sha_dma_init(struct atmel_sha_dev *dd)
2475d4905b38SNicolas Royer {
2476db28512fSPeter Ujfalusi 	dd->dma_lch_in.chan = dma_request_chan(dd->dev, "tx");
2477db28512fSPeter Ujfalusi 	if (IS_ERR(dd->dma_lch_in.chan)) {
2478e9ce6aeeSTudor Ambarus 		dev_err(dd->dev, "DMA channel is not available\n");
2479e9ce6aeeSTudor Ambarus 		return PTR_ERR(dd->dma_lch_in.chan);
2480abfe7ae4SNicolas Ferre 	}
2481d4905b38SNicolas Royer 
2482d4905b38SNicolas Royer 	dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base +
2483d4905b38SNicolas Royer 		SHA_REG_DIN(0);
2484d4905b38SNicolas Royer 	dd->dma_lch_in.dma_conf.src_maxburst = 1;
2485d4905b38SNicolas Royer 	dd->dma_lch_in.dma_conf.src_addr_width =
2486d4905b38SNicolas Royer 		DMA_SLAVE_BUSWIDTH_4_BYTES;
2487d4905b38SNicolas Royer 	dd->dma_lch_in.dma_conf.dst_maxburst = 1;
2488d4905b38SNicolas Royer 	dd->dma_lch_in.dma_conf.dst_addr_width =
2489d4905b38SNicolas Royer 		DMA_SLAVE_BUSWIDTH_4_BYTES;
2490d4905b38SNicolas Royer 	dd->dma_lch_in.dma_conf.device_fc = false;
2491d4905b38SNicolas Royer 
2492d4905b38SNicolas Royer 	return 0;
2493d4905b38SNicolas Royer }
2494d4905b38SNicolas Royer 
2495d4905b38SNicolas Royer static void atmel_sha_dma_cleanup(struct atmel_sha_dev *dd)
2496d4905b38SNicolas Royer {
2497d4905b38SNicolas Royer 	dma_release_channel(dd->dma_lch_in.chan);
2498d4905b38SNicolas Royer }
2499d4905b38SNicolas Royer 
2500d4905b38SNicolas Royer static void atmel_sha_get_cap(struct atmel_sha_dev *dd)
2501d4905b38SNicolas Royer {
2502d4905b38SNicolas Royer 
2503d4905b38SNicolas Royer 	dd->caps.has_dma = 0;
2504d4905b38SNicolas Royer 	dd->caps.has_dualbuff = 0;
2505d4905b38SNicolas Royer 	dd->caps.has_sha224 = 0;
2506d4905b38SNicolas Royer 	dd->caps.has_sha_384_512 = 0;
25077cee3508SCyrille Pitchen 	dd->caps.has_uihv = 0;
250881d8750bSCyrille Pitchen 	dd->caps.has_hmac = 0;
2509d4905b38SNicolas Royer 
2510d4905b38SNicolas Royer 	/* keep only major version number */
2511d4905b38SNicolas Royer 	switch (dd->hw_version & 0xff0) {
2512507c5cc2SCyrille Pitchen 	case 0x510:
2513507c5cc2SCyrille Pitchen 		dd->caps.has_dma = 1;
2514507c5cc2SCyrille Pitchen 		dd->caps.has_dualbuff = 1;
2515507c5cc2SCyrille Pitchen 		dd->caps.has_sha224 = 1;
2516507c5cc2SCyrille Pitchen 		dd->caps.has_sha_384_512 = 1;
25177cee3508SCyrille Pitchen 		dd->caps.has_uihv = 1;
251881d8750bSCyrille Pitchen 		dd->caps.has_hmac = 1;
2519507c5cc2SCyrille Pitchen 		break;
2520141824d0SLeilei Zhao 	case 0x420:
2521141824d0SLeilei Zhao 		dd->caps.has_dma = 1;
2522141824d0SLeilei Zhao 		dd->caps.has_dualbuff = 1;
2523141824d0SLeilei Zhao 		dd->caps.has_sha224 = 1;
2524141824d0SLeilei Zhao 		dd->caps.has_sha_384_512 = 1;
25257cee3508SCyrille Pitchen 		dd->caps.has_uihv = 1;
2526141824d0SLeilei Zhao 		break;
2527d4905b38SNicolas Royer 	case 0x410:
2528d4905b38SNicolas Royer 		dd->caps.has_dma = 1;
2529d4905b38SNicolas Royer 		dd->caps.has_dualbuff = 1;
2530d4905b38SNicolas Royer 		dd->caps.has_sha224 = 1;
2531d4905b38SNicolas Royer 		dd->caps.has_sha_384_512 = 1;
2532d4905b38SNicolas Royer 		break;
2533d4905b38SNicolas Royer 	case 0x400:
2534d4905b38SNicolas Royer 		dd->caps.has_dma = 1;
2535d4905b38SNicolas Royer 		dd->caps.has_dualbuff = 1;
2536d4905b38SNicolas Royer 		dd->caps.has_sha224 = 1;
2537d4905b38SNicolas Royer 		break;
2538d4905b38SNicolas Royer 	case 0x320:
2539d4905b38SNicolas Royer 		break;
2540d4905b38SNicolas Royer 	default:
2541d4905b38SNicolas Royer 		dev_warn(dd->dev,
2542d4905b38SNicolas Royer 				"Unmanaged sha version, set minimum capabilities\n");
2543d4905b38SNicolas Royer 		break;
2544d4905b38SNicolas Royer 	}
2545d4905b38SNicolas Royer }
2546d4905b38SNicolas Royer 
2547abfe7ae4SNicolas Ferre #if defined(CONFIG_OF)
2548abfe7ae4SNicolas Ferre static const struct of_device_id atmel_sha_dt_ids[] = {
2549abfe7ae4SNicolas Ferre 	{ .compatible = "atmel,at91sam9g46-sha" },
2550abfe7ae4SNicolas Ferre 	{ /* sentinel */ }
2551abfe7ae4SNicolas Ferre };
2552abfe7ae4SNicolas Ferre 
2553abfe7ae4SNicolas Ferre MODULE_DEVICE_TABLE(of, atmel_sha_dt_ids);
2554abfe7ae4SNicolas Ferre 
2555abfe7ae4SNicolas Ferre static struct crypto_platform_data *atmel_sha_of_init(struct platform_device *pdev)
2556abfe7ae4SNicolas Ferre {
2557abfe7ae4SNicolas Ferre 	struct device_node *np = pdev->dev.of_node;
2558abfe7ae4SNicolas Ferre 	struct crypto_platform_data *pdata;
2559abfe7ae4SNicolas Ferre 
2560abfe7ae4SNicolas Ferre 	if (!np) {
2561abfe7ae4SNicolas Ferre 		dev_err(&pdev->dev, "device node not found\n");
2562abfe7ae4SNicolas Ferre 		return ERR_PTR(-EINVAL);
2563abfe7ae4SNicolas Ferre 	}
2564abfe7ae4SNicolas Ferre 
2565abfe7ae4SNicolas Ferre 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
256602684839SMarkus Elfring 	if (!pdata)
2567abfe7ae4SNicolas Ferre 		return ERR_PTR(-ENOMEM);
2568abfe7ae4SNicolas Ferre 
2569abfe7ae4SNicolas Ferre 	return pdata;
2570abfe7ae4SNicolas Ferre }
2571abfe7ae4SNicolas Ferre #else /* CONFIG_OF */
2572abfe7ae4SNicolas Ferre static inline struct crypto_platform_data *atmel_sha_of_init(struct platform_device *dev)
2573abfe7ae4SNicolas Ferre {
2574abfe7ae4SNicolas Ferre 	return ERR_PTR(-EINVAL);
2575abfe7ae4SNicolas Ferre }
2576abfe7ae4SNicolas Ferre #endif
2577abfe7ae4SNicolas Ferre 
257849cfe4dbSGreg Kroah-Hartman static int atmel_sha_probe(struct platform_device *pdev)
2579ebc82efaSNicolas Royer {
2580ebc82efaSNicolas Royer 	struct atmel_sha_dev *sha_dd;
2581d4905b38SNicolas Royer 	struct crypto_platform_data	*pdata;
2582ebc82efaSNicolas Royer 	struct device *dev = &pdev->dev;
2583ebc82efaSNicolas Royer 	struct resource *sha_res;
2584ebc82efaSNicolas Royer 	int err;
2585ebc82efaSNicolas Royer 
2586b0e8b341SLABBE Corentin 	sha_dd = devm_kzalloc(&pdev->dev, sizeof(*sha_dd), GFP_KERNEL);
2587c9063a02STudor Ambarus 	if (!sha_dd)
2588c9063a02STudor Ambarus 		return -ENOMEM;
2589ebc82efaSNicolas Royer 
2590ebc82efaSNicolas Royer 	sha_dd->dev = dev;
2591ebc82efaSNicolas Royer 
2592ebc82efaSNicolas Royer 	platform_set_drvdata(pdev, sha_dd);
2593ebc82efaSNicolas Royer 
2594ebc82efaSNicolas Royer 	INIT_LIST_HEAD(&sha_dd->list);
259562728e82SLeilei Zhao 	spin_lock_init(&sha_dd->lock);
2596ebc82efaSNicolas Royer 
2597ebc82efaSNicolas Royer 	tasklet_init(&sha_dd->done_task, atmel_sha_done_task,
2598ebc82efaSNicolas Royer 					(unsigned long)sha_dd);
2599f56809c3SCyrille Pitchen 	tasklet_init(&sha_dd->queue_task, atmel_sha_queue_task,
2600f56809c3SCyrille Pitchen 					(unsigned long)sha_dd);
2601ebc82efaSNicolas Royer 
2602ebc82efaSNicolas Royer 	crypto_init_queue(&sha_dd->queue, ATMEL_SHA_QUEUE_LENGTH);
2603ebc82efaSNicolas Royer 
2604ebc82efaSNicolas Royer 	/* Get the base address */
2605ebc82efaSNicolas Royer 	sha_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2606ebc82efaSNicolas Royer 	if (!sha_res) {
2607ebc82efaSNicolas Royer 		dev_err(dev, "no MEM resource info\n");
2608ebc82efaSNicolas Royer 		err = -ENODEV;
2609e7836518STudor Ambarus 		goto err_tasklet_kill;
2610ebc82efaSNicolas Royer 	}
2611ebc82efaSNicolas Royer 	sha_dd->phys_base = sha_res->start;
2612ebc82efaSNicolas Royer 
2613ebc82efaSNicolas Royer 	/* Get the IRQ */
2614ebc82efaSNicolas Royer 	sha_dd->irq = platform_get_irq(pdev,  0);
2615ebc82efaSNicolas Royer 	if (sha_dd->irq < 0) {
2616ebc82efaSNicolas Royer 		err = sha_dd->irq;
2617e7836518STudor Ambarus 		goto err_tasklet_kill;
2618ebc82efaSNicolas Royer 	}
2619ebc82efaSNicolas Royer 
2620b0e8b341SLABBE Corentin 	err = devm_request_irq(&pdev->dev, sha_dd->irq, atmel_sha_irq,
2621b0e8b341SLABBE Corentin 			       IRQF_SHARED, "atmel-sha", sha_dd);
2622ebc82efaSNicolas Royer 	if (err) {
2623ebc82efaSNicolas Royer 		dev_err(dev, "unable to request sha irq.\n");
2624e7836518STudor Ambarus 		goto err_tasklet_kill;
2625ebc82efaSNicolas Royer 	}
2626ebc82efaSNicolas Royer 
2627ebc82efaSNicolas Royer 	/* Initializing the clock */
2628b0e8b341SLABBE Corentin 	sha_dd->iclk = devm_clk_get(&pdev->dev, "sha_clk");
2629ebc82efaSNicolas Royer 	if (IS_ERR(sha_dd->iclk)) {
2630be208356SColin Ian King 		dev_err(dev, "clock initialization failed.\n");
2631ebc82efaSNicolas Royer 		err = PTR_ERR(sha_dd->iclk);
2632e7836518STudor Ambarus 		goto err_tasklet_kill;
2633ebc82efaSNicolas Royer 	}
2634ebc82efaSNicolas Royer 
2635b0e8b341SLABBE Corentin 	sha_dd->io_base = devm_ioremap_resource(&pdev->dev, sha_res);
26369b52d55fSVladimir Zapolskiy 	if (IS_ERR(sha_dd->io_base)) {
2637ebc82efaSNicolas Royer 		dev_err(dev, "can't ioremap\n");
26389b52d55fSVladimir Zapolskiy 		err = PTR_ERR(sha_dd->io_base);
2639e7836518STudor Ambarus 		goto err_tasklet_kill;
2640ebc82efaSNicolas Royer 	}
2641ebc82efaSNicolas Royer 
2642c033042aSCyrille Pitchen 	err = clk_prepare(sha_dd->iclk);
2643c033042aSCyrille Pitchen 	if (err)
2644e7836518STudor Ambarus 		goto err_tasklet_kill;
2645c033042aSCyrille Pitchen 
26460efe58f3STudor Ambarus 	err = atmel_sha_hw_version_init(sha_dd);
26470efe58f3STudor Ambarus 	if (err)
2648e7836518STudor Ambarus 		goto err_iclk_unprepare;
2649d4905b38SNicolas Royer 
2650d4905b38SNicolas Royer 	atmel_sha_get_cap(sha_dd);
2651d4905b38SNicolas Royer 
2652d4905b38SNicolas Royer 	if (sha_dd->caps.has_dma) {
2653d4905b38SNicolas Royer 		pdata = pdev->dev.platform_data;
2654d4905b38SNicolas Royer 		if (!pdata) {
2655abfe7ae4SNicolas Ferre 			pdata = atmel_sha_of_init(pdev);
2656abfe7ae4SNicolas Ferre 			if (IS_ERR(pdata)) {
2657d4905b38SNicolas Royer 				dev_err(&pdev->dev, "platform data not available\n");
2658abfe7ae4SNicolas Ferre 				err = PTR_ERR(pdata);
2659e7836518STudor Ambarus 				goto err_iclk_unprepare;
2660abfe7ae4SNicolas Ferre 			}
2661abfe7ae4SNicolas Ferre 		}
2662db28512fSPeter Ujfalusi 
2663827a98dfSTudor Ambarus 		err = atmel_sha_dma_init(sha_dd);
2664d4905b38SNicolas Royer 		if (err)
2665e7836518STudor Ambarus 			goto err_iclk_unprepare;
2666abfe7ae4SNicolas Ferre 
2667abfe7ae4SNicolas Ferre 		dev_info(dev, "using %s for DMA transfers\n",
2668abfe7ae4SNicolas Ferre 				dma_chan_name(sha_dd->dma_lch_in.chan));
2669d4905b38SNicolas Royer 	}
2670d4905b38SNicolas Royer 
2671ebc82efaSNicolas Royer 	spin_lock(&atmel_sha.lock);
2672ebc82efaSNicolas Royer 	list_add_tail(&sha_dd->list, &atmel_sha.dev_list);
2673ebc82efaSNicolas Royer 	spin_unlock(&atmel_sha.lock);
2674ebc82efaSNicolas Royer 
2675ebc82efaSNicolas Royer 	err = atmel_sha_register_algs(sha_dd);
2676ebc82efaSNicolas Royer 	if (err)
2677ebc82efaSNicolas Royer 		goto err_algs;
2678ebc82efaSNicolas Royer 
26791ca5b7d9SNicolas Ferre 	dev_info(dev, "Atmel SHA1/SHA256%s%s\n",
26801ca5b7d9SNicolas Ferre 			sha_dd->caps.has_sha224 ? "/SHA224" : "",
26811ca5b7d9SNicolas Ferre 			sha_dd->caps.has_sha_384_512 ? "/SHA384/SHA512" : "");
2682ebc82efaSNicolas Royer 
2683ebc82efaSNicolas Royer 	return 0;
2684ebc82efaSNicolas Royer 
2685ebc82efaSNicolas Royer err_algs:
2686ebc82efaSNicolas Royer 	spin_lock(&atmel_sha.lock);
2687ebc82efaSNicolas Royer 	list_del(&sha_dd->list);
2688ebc82efaSNicolas Royer 	spin_unlock(&atmel_sha.lock);
2689d4905b38SNicolas Royer 	if (sha_dd->caps.has_dma)
2690d4905b38SNicolas Royer 		atmel_sha_dma_cleanup(sha_dd);
2691e7836518STudor Ambarus err_iclk_unprepare:
2692c033042aSCyrille Pitchen 	clk_unprepare(sha_dd->iclk);
2693e7836518STudor Ambarus err_tasklet_kill:
2694f56809c3SCyrille Pitchen 	tasklet_kill(&sha_dd->queue_task);
2695ebc82efaSNicolas Royer 	tasklet_kill(&sha_dd->done_task);
2696ebc82efaSNicolas Royer 
2697ebc82efaSNicolas Royer 	return err;
2698ebc82efaSNicolas Royer }
2699ebc82efaSNicolas Royer 
270049cfe4dbSGreg Kroah-Hartman static int atmel_sha_remove(struct platform_device *pdev)
2701ebc82efaSNicolas Royer {
270222d96f04SGustavo A. R. Silva 	struct atmel_sha_dev *sha_dd;
2703ebc82efaSNicolas Royer 
2704ebc82efaSNicolas Royer 	sha_dd = platform_get_drvdata(pdev);
2705ebc82efaSNicolas Royer 	if (!sha_dd)
2706ebc82efaSNicolas Royer 		return -ENODEV;
2707ebc82efaSNicolas Royer 	spin_lock(&atmel_sha.lock);
2708ebc82efaSNicolas Royer 	list_del(&sha_dd->list);
2709ebc82efaSNicolas Royer 	spin_unlock(&atmel_sha.lock);
2710ebc82efaSNicolas Royer 
2711ebc82efaSNicolas Royer 	atmel_sha_unregister_algs(sha_dd);
2712ebc82efaSNicolas Royer 
2713f56809c3SCyrille Pitchen 	tasklet_kill(&sha_dd->queue_task);
2714ebc82efaSNicolas Royer 	tasklet_kill(&sha_dd->done_task);
2715ebc82efaSNicolas Royer 
2716d4905b38SNicolas Royer 	if (sha_dd->caps.has_dma)
2717d4905b38SNicolas Royer 		atmel_sha_dma_cleanup(sha_dd);
2718d4905b38SNicolas Royer 
2719c033042aSCyrille Pitchen 	clk_unprepare(sha_dd->iclk);
2720c033042aSCyrille Pitchen 
2721ebc82efaSNicolas Royer 	return 0;
2722ebc82efaSNicolas Royer }
2723ebc82efaSNicolas Royer 
2724ebc82efaSNicolas Royer static struct platform_driver atmel_sha_driver = {
2725ebc82efaSNicolas Royer 	.probe		= atmel_sha_probe,
272649cfe4dbSGreg Kroah-Hartman 	.remove		= atmel_sha_remove,
2727ebc82efaSNicolas Royer 	.driver		= {
2728ebc82efaSNicolas Royer 		.name	= "atmel_sha",
2729abfe7ae4SNicolas Ferre 		.of_match_table	= of_match_ptr(atmel_sha_dt_ids),
2730ebc82efaSNicolas Royer 	},
2731ebc82efaSNicolas Royer };
2732ebc82efaSNicolas Royer 
2733ebc82efaSNicolas Royer module_platform_driver(atmel_sha_driver);
2734ebc82efaSNicolas Royer 
2735d4905b38SNicolas Royer MODULE_DESCRIPTION("Atmel SHA (1/256/224/384/512) hw acceleration support.");
2736ebc82efaSNicolas Royer MODULE_LICENSE("GPL v2");
2737ebc82efaSNicolas Royer MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");
2738