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>
24b46f36c0STudor Ambarus #include <linux/dmaengine.h>
25ebc82efaSNicolas Royer #include <linux/init.h>
26ebc82efaSNicolas Royer #include <linux/errno.h>
27ebc82efaSNicolas Royer #include <linux/interrupt.h>
28ebc82efaSNicolas Royer #include <linux/irq.h>
29ebc82efaSNicolas Royer #include <linux/scatterlist.h>
30ebc82efaSNicolas Royer #include <linux/dma-mapping.h>
31*b0cc7491SRob Herring #include <linux/mod_devicetable.h>
32ebc82efaSNicolas Royer #include <linux/delay.h>
33ebc82efaSNicolas Royer #include <linux/crypto.h>
34ebc82efaSNicolas Royer #include <crypto/scatterwalk.h>
35ebc82efaSNicolas Royer #include <crypto/algapi.h>
36a24d22b2SEric Biggers #include <crypto/sha1.h>
37a24d22b2SEric Biggers #include <crypto/sha2.h>
38ebc82efaSNicolas Royer #include <crypto/hash.h>
39ebc82efaSNicolas Royer #include <crypto/internal/hash.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
atmel_sha_reg_name(u32 offset,char * tmp,size_t sz,bool wr)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
atmel_sha_read(struct atmel_sha_dev * dd,u32 offset)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
atmel_sha_write(struct atmel_sha_dev * dd,u32 offset,u32 value)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
atmel_sha_complete(struct atmel_sha_dev * dd,int err)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)
2957d19abdcSHerbert Xu ahash_request_complete(req, 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
atmel_sha_append_sg(struct atmel_sha_reqctx * ctx)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 */
atmel_sha_fill_padding(struct atmel_sha_reqctx * ctx,int length)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
atmel_sha_find_dev(struct atmel_sha_ctx * tctx)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
atmel_sha_init(struct ahash_request * req)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
4373eb75fc7SKai Ye dev_dbg(dd->dev, "init: digest size: %u\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 }
464ebc82efaSNicolas Royer
465ebc82efaSNicolas Royer ctx->bufcnt = 0;
466d4905b38SNicolas Royer ctx->digcnt[0] = 0;
467d4905b38SNicolas Royer ctx->digcnt[1] = 0;
468ebc82efaSNicolas Royer ctx->buflen = SHA_BUFFER_LEN;
469ebc82efaSNicolas Royer
470ebc82efaSNicolas Royer return 0;
471ebc82efaSNicolas Royer }
472ebc82efaSNicolas Royer
atmel_sha_write_ctrl(struct atmel_sha_dev * dd,int dma)473ebc82efaSNicolas Royer static void atmel_sha_write_ctrl(struct atmel_sha_dev *dd, int dma)
474ebc82efaSNicolas Royer {
475ebc82efaSNicolas Royer struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
4767cee3508SCyrille Pitchen u32 valmr = SHA_MR_MODE_AUTO;
4777cee3508SCyrille Pitchen unsigned int i, hashsize = 0;
478ebc82efaSNicolas Royer
479ebc82efaSNicolas Royer if (likely(dma)) {
480d4905b38SNicolas Royer if (!dd->caps.has_dma)
481ebc82efaSNicolas Royer atmel_sha_write(dd, SHA_IER, SHA_INT_TXBUFE);
482ebc82efaSNicolas Royer valmr = SHA_MR_MODE_PDC;
483d4905b38SNicolas Royer if (dd->caps.has_dualbuff)
484d4905b38SNicolas Royer valmr |= SHA_MR_DUALBUFF;
485ebc82efaSNicolas Royer } else {
486ebc82efaSNicolas Royer atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
487ebc82efaSNicolas Royer }
488ebc82efaSNicolas Royer
4897cee3508SCyrille Pitchen switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
4907cee3508SCyrille Pitchen case SHA_FLAGS_SHA1:
491d4905b38SNicolas Royer valmr |= SHA_MR_ALGO_SHA1;
4927cee3508SCyrille Pitchen hashsize = SHA1_DIGEST_SIZE;
4937cee3508SCyrille Pitchen break;
4947cee3508SCyrille Pitchen
4957cee3508SCyrille Pitchen case SHA_FLAGS_SHA224:
496d4905b38SNicolas Royer valmr |= SHA_MR_ALGO_SHA224;
4977cee3508SCyrille Pitchen hashsize = SHA256_DIGEST_SIZE;
4987cee3508SCyrille Pitchen break;
4997cee3508SCyrille Pitchen
5007cee3508SCyrille Pitchen case SHA_FLAGS_SHA256:
501ebc82efaSNicolas Royer valmr |= SHA_MR_ALGO_SHA256;
5027cee3508SCyrille Pitchen hashsize = SHA256_DIGEST_SIZE;
5037cee3508SCyrille Pitchen break;
5047cee3508SCyrille Pitchen
5057cee3508SCyrille Pitchen case SHA_FLAGS_SHA384:
506d4905b38SNicolas Royer valmr |= SHA_MR_ALGO_SHA384;
5077cee3508SCyrille Pitchen hashsize = SHA512_DIGEST_SIZE;
5087cee3508SCyrille Pitchen break;
5097cee3508SCyrille Pitchen
5107cee3508SCyrille Pitchen case SHA_FLAGS_SHA512:
511d4905b38SNicolas Royer valmr |= SHA_MR_ALGO_SHA512;
5127cee3508SCyrille Pitchen hashsize = SHA512_DIGEST_SIZE;
5137cee3508SCyrille Pitchen break;
5147cee3508SCyrille Pitchen
5157cee3508SCyrille Pitchen default:
5167cee3508SCyrille Pitchen break;
5177cee3508SCyrille Pitchen }
518ebc82efaSNicolas Royer
519ebc82efaSNicolas Royer /* Setting CR_FIRST only for the first iteration */
5207cee3508SCyrille Pitchen if (!(ctx->digcnt[0] || ctx->digcnt[1])) {
5217cee3508SCyrille Pitchen atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
5227cee3508SCyrille Pitchen } else if (dd->caps.has_uihv && (ctx->flags & SHA_FLAGS_RESTORE)) {
5237cee3508SCyrille Pitchen const u32 *hash = (const u32 *)ctx->digest;
524ebc82efaSNicolas Royer
5257cee3508SCyrille Pitchen /*
5267cee3508SCyrille Pitchen * Restore the hardware context: update the User Initialize
5277cee3508SCyrille Pitchen * Hash Value (UIHV) with the value saved when the latest
5287cee3508SCyrille Pitchen * 'update' operation completed on this very same crypto
5297cee3508SCyrille Pitchen * request.
5307cee3508SCyrille Pitchen */
5317cee3508SCyrille Pitchen ctx->flags &= ~SHA_FLAGS_RESTORE;
5327cee3508SCyrille Pitchen atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
5337cee3508SCyrille Pitchen for (i = 0; i < hashsize / sizeof(u32); ++i)
5347cee3508SCyrille Pitchen atmel_sha_write(dd, SHA_REG_DIN(i), hash[i]);
5357cee3508SCyrille Pitchen atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
5367cee3508SCyrille Pitchen valmr |= SHA_MR_UIHV;
5377cee3508SCyrille Pitchen }
5387cee3508SCyrille Pitchen /*
5397cee3508SCyrille Pitchen * WARNING: If the UIHV feature is not available, the hardware CANNOT
5407cee3508SCyrille Pitchen * process concurrent requests: the internal registers used to store
5417cee3508SCyrille Pitchen * the hash/digest are still set to the partial digest output values
5427cee3508SCyrille Pitchen * computed during the latest round.
5437cee3508SCyrille Pitchen */
5447cee3508SCyrille Pitchen
545ebc82efaSNicolas Royer atmel_sha_write(dd, SHA_MR, valmr);
546ebc82efaSNicolas Royer }
547ebc82efaSNicolas Royer
atmel_sha_wait_for_data_ready(struct atmel_sha_dev * dd,atmel_sha_fn_t resume)5489064ed92SCyrille Pitchen static inline int atmel_sha_wait_for_data_ready(struct atmel_sha_dev *dd,
5499064ed92SCyrille Pitchen atmel_sha_fn_t resume)
5509064ed92SCyrille Pitchen {
5519064ed92SCyrille Pitchen u32 isr = atmel_sha_read(dd, SHA_ISR);
5529064ed92SCyrille Pitchen
5539064ed92SCyrille Pitchen if (unlikely(isr & SHA_INT_DATARDY))
5549064ed92SCyrille Pitchen return resume(dd);
5559064ed92SCyrille Pitchen
5569064ed92SCyrille Pitchen dd->resume = resume;
5579064ed92SCyrille Pitchen atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
5589064ed92SCyrille Pitchen return -EINPROGRESS;
5599064ed92SCyrille Pitchen }
5609064ed92SCyrille Pitchen
atmel_sha_xmit_cpu(struct atmel_sha_dev * dd,const u8 * buf,size_t length,int final)561ebc82efaSNicolas Royer static int atmel_sha_xmit_cpu(struct atmel_sha_dev *dd, const u8 *buf,
562ebc82efaSNicolas Royer size_t length, int final)
563ebc82efaSNicolas Royer {
564ebc82efaSNicolas Royer struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
565ebc82efaSNicolas Royer int count, len32;
566ebc82efaSNicolas Royer const u32 *buffer = (const u32 *)buf;
567ebc82efaSNicolas Royer
5684c147bcfSArnd Bergmann dev_dbg(dd->dev, "xmit_cpu: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
569d4905b38SNicolas Royer ctx->digcnt[1], ctx->digcnt[0], length, final);
570ebc82efaSNicolas Royer
571ebc82efaSNicolas Royer atmel_sha_write_ctrl(dd, 0);
572ebc82efaSNicolas Royer
573ebc82efaSNicolas Royer /* should be non-zero before next lines to disable clocks later */
574d4905b38SNicolas Royer ctx->digcnt[0] += length;
575d4905b38SNicolas Royer if (ctx->digcnt[0] < length)
576d4905b38SNicolas Royer ctx->digcnt[1]++;
577ebc82efaSNicolas Royer
578ebc82efaSNicolas Royer if (final)
579ebc82efaSNicolas Royer dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
580ebc82efaSNicolas Royer
581ebc82efaSNicolas Royer len32 = DIV_ROUND_UP(length, sizeof(u32));
582ebc82efaSNicolas Royer
583ebc82efaSNicolas Royer dd->flags |= SHA_FLAGS_CPU;
584ebc82efaSNicolas Royer
585ebc82efaSNicolas Royer for (count = 0; count < len32; count++)
586ebc82efaSNicolas Royer atmel_sha_write(dd, SHA_REG_DIN(count), buffer[count]);
587ebc82efaSNicolas Royer
588ebc82efaSNicolas Royer return -EINPROGRESS;
589ebc82efaSNicolas Royer }
590ebc82efaSNicolas Royer
atmel_sha_xmit_pdc(struct atmel_sha_dev * dd,dma_addr_t dma_addr1,size_t length1,dma_addr_t dma_addr2,size_t length2,int final)591ebc82efaSNicolas Royer static int atmel_sha_xmit_pdc(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
592ebc82efaSNicolas Royer size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
593ebc82efaSNicolas Royer {
594ebc82efaSNicolas Royer struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
595ebc82efaSNicolas Royer int len32;
596ebc82efaSNicolas Royer
5974c147bcfSArnd Bergmann dev_dbg(dd->dev, "xmit_pdc: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
598d4905b38SNicolas Royer ctx->digcnt[1], ctx->digcnt[0], length1, final);
599ebc82efaSNicolas Royer
600ebc82efaSNicolas Royer len32 = DIV_ROUND_UP(length1, sizeof(u32));
601ebc82efaSNicolas Royer atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTDIS);
602ebc82efaSNicolas Royer atmel_sha_write(dd, SHA_TPR, dma_addr1);
603ebc82efaSNicolas Royer atmel_sha_write(dd, SHA_TCR, len32);
604ebc82efaSNicolas Royer
605ebc82efaSNicolas Royer len32 = DIV_ROUND_UP(length2, sizeof(u32));
606ebc82efaSNicolas Royer atmel_sha_write(dd, SHA_TNPR, dma_addr2);
607ebc82efaSNicolas Royer atmel_sha_write(dd, SHA_TNCR, len32);
608ebc82efaSNicolas Royer
609ebc82efaSNicolas Royer atmel_sha_write_ctrl(dd, 1);
610ebc82efaSNicolas Royer
611ebc82efaSNicolas Royer /* should be non-zero before next lines to disable clocks later */
612d4905b38SNicolas Royer ctx->digcnt[0] += length1;
613d4905b38SNicolas Royer if (ctx->digcnt[0] < length1)
614d4905b38SNicolas Royer ctx->digcnt[1]++;
615ebc82efaSNicolas Royer
616ebc82efaSNicolas Royer if (final)
617ebc82efaSNicolas Royer dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
618ebc82efaSNicolas Royer
619ebc82efaSNicolas Royer dd->flags |= SHA_FLAGS_DMA_ACTIVE;
620ebc82efaSNicolas Royer
621ebc82efaSNicolas Royer /* Start DMA transfer */
622ebc82efaSNicolas Royer atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTEN);
623ebc82efaSNicolas Royer
624ebc82efaSNicolas Royer return -EINPROGRESS;
625ebc82efaSNicolas Royer }
626ebc82efaSNicolas Royer
atmel_sha_dma_callback(void * data)627d4905b38SNicolas Royer static void atmel_sha_dma_callback(void *data)
628d4905b38SNicolas Royer {
629d4905b38SNicolas Royer struct atmel_sha_dev *dd = data;
630d4905b38SNicolas Royer
631a29af939SCyrille Pitchen dd->is_async = true;
632a29af939SCyrille Pitchen
633d4905b38SNicolas Royer /* dma_lch_in - completed - wait DATRDY */
634d4905b38SNicolas Royer atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
635d4905b38SNicolas Royer }
636d4905b38SNicolas Royer
atmel_sha_xmit_dma(struct atmel_sha_dev * dd,dma_addr_t dma_addr1,size_t length1,dma_addr_t dma_addr2,size_t length2,int final)637d4905b38SNicolas Royer static int atmel_sha_xmit_dma(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
638d4905b38SNicolas Royer size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
639d4905b38SNicolas Royer {
640d4905b38SNicolas Royer struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
641d4905b38SNicolas Royer struct dma_async_tx_descriptor *in_desc;
642d4905b38SNicolas Royer struct scatterlist sg[2];
643d4905b38SNicolas Royer
6444c147bcfSArnd Bergmann dev_dbg(dd->dev, "xmit_dma: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
645d4905b38SNicolas Royer ctx->digcnt[1], ctx->digcnt[0], length1, final);
646d4905b38SNicolas Royer
647d4905b38SNicolas Royer dd->dma_lch_in.dma_conf.src_maxburst = 16;
648d4905b38SNicolas Royer dd->dma_lch_in.dma_conf.dst_maxburst = 16;
649d4905b38SNicolas Royer
650d4905b38SNicolas Royer dmaengine_slave_config(dd->dma_lch_in.chan, &dd->dma_lch_in.dma_conf);
651d4905b38SNicolas Royer
652d4905b38SNicolas Royer if (length2) {
653d4905b38SNicolas Royer sg_init_table(sg, 2);
654d4905b38SNicolas Royer sg_dma_address(&sg[0]) = dma_addr1;
655d4905b38SNicolas Royer sg_dma_len(&sg[0]) = length1;
656d4905b38SNicolas Royer sg_dma_address(&sg[1]) = dma_addr2;
657d4905b38SNicolas Royer sg_dma_len(&sg[1]) = length2;
658d4905b38SNicolas Royer in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 2,
659d4905b38SNicolas Royer DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
660d4905b38SNicolas Royer } else {
661d4905b38SNicolas Royer sg_init_table(sg, 1);
662d4905b38SNicolas Royer sg_dma_address(&sg[0]) = dma_addr1;
663d4905b38SNicolas Royer sg_dma_len(&sg[0]) = length1;
664d4905b38SNicolas Royer in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 1,
665d4905b38SNicolas Royer DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
666d4905b38SNicolas Royer }
667d4905b38SNicolas Royer if (!in_desc)
668dd3f9f40SCyrille Pitchen return atmel_sha_complete(dd, -EINVAL);
669d4905b38SNicolas Royer
670d4905b38SNicolas Royer in_desc->callback = atmel_sha_dma_callback;
671d4905b38SNicolas Royer in_desc->callback_param = dd;
672d4905b38SNicolas Royer
673d4905b38SNicolas Royer atmel_sha_write_ctrl(dd, 1);
674d4905b38SNicolas Royer
675d4905b38SNicolas Royer /* should be non-zero before next lines to disable clocks later */
676d4905b38SNicolas Royer ctx->digcnt[0] += length1;
677d4905b38SNicolas Royer if (ctx->digcnt[0] < length1)
678d4905b38SNicolas Royer ctx->digcnt[1]++;
679d4905b38SNicolas Royer
680d4905b38SNicolas Royer if (final)
681d4905b38SNicolas Royer dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
682d4905b38SNicolas Royer
683d4905b38SNicolas Royer dd->flags |= SHA_FLAGS_DMA_ACTIVE;
684d4905b38SNicolas Royer
685d4905b38SNicolas Royer /* Start DMA transfer */
686d4905b38SNicolas Royer dmaengine_submit(in_desc);
687d4905b38SNicolas Royer dma_async_issue_pending(dd->dma_lch_in.chan);
688d4905b38SNicolas Royer
689d4905b38SNicolas Royer return -EINPROGRESS;
690d4905b38SNicolas Royer }
691d4905b38SNicolas Royer
atmel_sha_xmit_start(struct atmel_sha_dev * dd,dma_addr_t dma_addr1,size_t length1,dma_addr_t dma_addr2,size_t length2,int final)692d4905b38SNicolas Royer static int atmel_sha_xmit_start(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
693d4905b38SNicolas Royer size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
694d4905b38SNicolas Royer {
695d4905b38SNicolas Royer if (dd->caps.has_dma)
696d4905b38SNicolas Royer return atmel_sha_xmit_dma(dd, dma_addr1, length1,
697d4905b38SNicolas Royer dma_addr2, length2, final);
698d4905b38SNicolas Royer else
699d4905b38SNicolas Royer return atmel_sha_xmit_pdc(dd, dma_addr1, length1,
700d4905b38SNicolas Royer dma_addr2, length2, final);
701d4905b38SNicolas Royer }
702d4905b38SNicolas Royer
atmel_sha_update_cpu(struct atmel_sha_dev * dd)703ebc82efaSNicolas Royer static int atmel_sha_update_cpu(struct atmel_sha_dev *dd)
704ebc82efaSNicolas Royer {
705ebc82efaSNicolas Royer struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
706ebc82efaSNicolas Royer int bufcnt;
707ebc82efaSNicolas Royer
708ebc82efaSNicolas Royer atmel_sha_append_sg(ctx);
709ebc82efaSNicolas Royer atmel_sha_fill_padding(ctx, 0);
710ebc82efaSNicolas Royer bufcnt = ctx->bufcnt;
711ebc82efaSNicolas Royer ctx->bufcnt = 0;
712ebc82efaSNicolas Royer
713ebc82efaSNicolas Royer return atmel_sha_xmit_cpu(dd, ctx->buffer, bufcnt, 1);
714ebc82efaSNicolas Royer }
715ebc82efaSNicolas Royer
atmel_sha_xmit_dma_map(struct atmel_sha_dev * dd,struct atmel_sha_reqctx * ctx,size_t length,int final)716ebc82efaSNicolas Royer static int atmel_sha_xmit_dma_map(struct atmel_sha_dev *dd,
717ebc82efaSNicolas Royer struct atmel_sha_reqctx *ctx,
718ebc82efaSNicolas Royer size_t length, int final)
719ebc82efaSNicolas Royer {
720ebc82efaSNicolas Royer ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
721d4905b38SNicolas Royer ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
722ebc82efaSNicolas Royer if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
7234c147bcfSArnd Bergmann dev_err(dd->dev, "dma %zu bytes error\n", ctx->buflen +
724d4905b38SNicolas Royer ctx->block_size);
725dd3f9f40SCyrille Pitchen return atmel_sha_complete(dd, -EINVAL);
726ebc82efaSNicolas Royer }
727ebc82efaSNicolas Royer
728ebc82efaSNicolas Royer ctx->flags &= ~SHA_FLAGS_SG;
729ebc82efaSNicolas Royer
730ebc82efaSNicolas Royer /* next call does not fail... so no unmap in the case of error */
731d4905b38SNicolas Royer return atmel_sha_xmit_start(dd, ctx->dma_addr, length, 0, 0, final);
732ebc82efaSNicolas Royer }
733ebc82efaSNicolas Royer
atmel_sha_update_dma_slow(struct atmel_sha_dev * dd)734ebc82efaSNicolas Royer static int atmel_sha_update_dma_slow(struct atmel_sha_dev *dd)
735ebc82efaSNicolas Royer {
736ebc82efaSNicolas Royer struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
737ebc82efaSNicolas Royer unsigned int final;
738ebc82efaSNicolas Royer size_t count;
739ebc82efaSNicolas Royer
740ebc82efaSNicolas Royer atmel_sha_append_sg(ctx);
741ebc82efaSNicolas Royer
742ebc82efaSNicolas Royer final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
743ebc82efaSNicolas Royer
7444c147bcfSArnd Bergmann dev_dbg(dd->dev, "slow: bufcnt: %zu, digcnt: 0x%llx 0x%llx, final: %d\n",
745d4905b38SNicolas Royer ctx->bufcnt, ctx->digcnt[1], ctx->digcnt[0], final);
746ebc82efaSNicolas Royer
747ebc82efaSNicolas Royer if (final)
748ebc82efaSNicolas Royer atmel_sha_fill_padding(ctx, 0);
749ebc82efaSNicolas Royer
7500099286bSLudovic Desroches if (final || (ctx->bufcnt == ctx->buflen)) {
751ebc82efaSNicolas Royer count = ctx->bufcnt;
752ebc82efaSNicolas Royer ctx->bufcnt = 0;
753ebc82efaSNicolas Royer return atmel_sha_xmit_dma_map(dd, ctx, count, final);
754ebc82efaSNicolas Royer }
755ebc82efaSNicolas Royer
756ebc82efaSNicolas Royer return 0;
757ebc82efaSNicolas Royer }
758ebc82efaSNicolas Royer
atmel_sha_update_dma_start(struct atmel_sha_dev * dd)759ebc82efaSNicolas Royer static int atmel_sha_update_dma_start(struct atmel_sha_dev *dd)
760ebc82efaSNicolas Royer {
761ebc82efaSNicolas Royer struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
762ebc82efaSNicolas Royer unsigned int length, final, tail;
763ebc82efaSNicolas Royer struct scatterlist *sg;
764ebc82efaSNicolas Royer unsigned int count;
765ebc82efaSNicolas Royer
766ebc82efaSNicolas Royer if (!ctx->total)
767ebc82efaSNicolas Royer return 0;
768ebc82efaSNicolas Royer
769ebc82efaSNicolas Royer if (ctx->bufcnt || ctx->offset)
770ebc82efaSNicolas Royer return atmel_sha_update_dma_slow(dd);
771ebc82efaSNicolas Royer
7724c147bcfSArnd Bergmann dev_dbg(dd->dev, "fast: digcnt: 0x%llx 0x%llx, bufcnt: %zd, total: %u\n",
773d4905b38SNicolas Royer ctx->digcnt[1], ctx->digcnt[0], ctx->bufcnt, ctx->total);
774ebc82efaSNicolas Royer
775ebc82efaSNicolas Royer sg = ctx->sg;
776ebc82efaSNicolas Royer
777ebc82efaSNicolas Royer if (!IS_ALIGNED(sg->offset, sizeof(u32)))
778ebc82efaSNicolas Royer return atmel_sha_update_dma_slow(dd);
779ebc82efaSNicolas Royer
780d4905b38SNicolas Royer if (!sg_is_last(sg) && !IS_ALIGNED(sg->length, ctx->block_size))
781d4905b38SNicolas Royer /* size is not ctx->block_size aligned */
782ebc82efaSNicolas Royer return atmel_sha_update_dma_slow(dd);
783ebc82efaSNicolas Royer
784ebc82efaSNicolas Royer length = min(ctx->total, sg->length);
785ebc82efaSNicolas Royer
786ebc82efaSNicolas Royer if (sg_is_last(sg)) {
787ebc82efaSNicolas Royer if (!(ctx->flags & SHA_FLAGS_FINUP)) {
788d4905b38SNicolas Royer /* not last sg must be ctx->block_size aligned */
789d4905b38SNicolas Royer tail = length & (ctx->block_size - 1);
790ebc82efaSNicolas Royer length -= tail;
791ebc82efaSNicolas Royer }
792ebc82efaSNicolas Royer }
793ebc82efaSNicolas Royer
794ebc82efaSNicolas Royer ctx->total -= length;
795ebc82efaSNicolas Royer ctx->offset = length; /* offset where to start slow */
796ebc82efaSNicolas Royer
797ebc82efaSNicolas Royer final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
798ebc82efaSNicolas Royer
799ebc82efaSNicolas Royer /* Add padding */
800ebc82efaSNicolas Royer if (final) {
801d4905b38SNicolas Royer tail = length & (ctx->block_size - 1);
802ebc82efaSNicolas Royer length -= tail;
803ebc82efaSNicolas Royer ctx->total += tail;
804ebc82efaSNicolas Royer ctx->offset = length; /* offset where to start slow */
805ebc82efaSNicolas Royer
806ebc82efaSNicolas Royer sg = ctx->sg;
807ebc82efaSNicolas Royer atmel_sha_append_sg(ctx);
808ebc82efaSNicolas Royer
809ebc82efaSNicolas Royer atmel_sha_fill_padding(ctx, length);
810ebc82efaSNicolas Royer
811ebc82efaSNicolas Royer ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
812d4905b38SNicolas Royer ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
813ebc82efaSNicolas Royer if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
8144c147bcfSArnd Bergmann dev_err(dd->dev, "dma %zu bytes error\n",
815d4905b38SNicolas Royer ctx->buflen + ctx->block_size);
816dd3f9f40SCyrille Pitchen return atmel_sha_complete(dd, -EINVAL);
817ebc82efaSNicolas Royer }
818ebc82efaSNicolas Royer
819ebc82efaSNicolas Royer if (length == 0) {
820ebc82efaSNicolas Royer ctx->flags &= ~SHA_FLAGS_SG;
821ebc82efaSNicolas Royer count = ctx->bufcnt;
822ebc82efaSNicolas Royer ctx->bufcnt = 0;
823d4905b38SNicolas Royer return atmel_sha_xmit_start(dd, ctx->dma_addr, count, 0,
824ebc82efaSNicolas Royer 0, final);
825ebc82efaSNicolas Royer } else {
826ebc82efaSNicolas Royer ctx->sg = sg;
827ebc82efaSNicolas Royer if (!dma_map_sg(dd->dev, ctx->sg, 1,
828ebc82efaSNicolas Royer DMA_TO_DEVICE)) {
829ebc82efaSNicolas Royer dev_err(dd->dev, "dma_map_sg error\n");
830dd3f9f40SCyrille Pitchen return atmel_sha_complete(dd, -EINVAL);
831ebc82efaSNicolas Royer }
832ebc82efaSNicolas Royer
833ebc82efaSNicolas Royer ctx->flags |= SHA_FLAGS_SG;
834ebc82efaSNicolas Royer
835ebc82efaSNicolas Royer count = ctx->bufcnt;
836ebc82efaSNicolas Royer ctx->bufcnt = 0;
837d4905b38SNicolas Royer return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg),
838ebc82efaSNicolas Royer length, ctx->dma_addr, count, final);
839ebc82efaSNicolas Royer }
840ebc82efaSNicolas Royer }
841ebc82efaSNicolas Royer
842ebc82efaSNicolas Royer if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
843ebc82efaSNicolas Royer dev_err(dd->dev, "dma_map_sg error\n");
844dd3f9f40SCyrille Pitchen return atmel_sha_complete(dd, -EINVAL);
845ebc82efaSNicolas Royer }
846ebc82efaSNicolas Royer
847ebc82efaSNicolas Royer ctx->flags |= SHA_FLAGS_SG;
848ebc82efaSNicolas Royer
849ebc82efaSNicolas Royer /* next call does not fail... so no unmap in the case of error */
850d4905b38SNicolas Royer return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg), length, 0,
851ebc82efaSNicolas Royer 0, final);
852ebc82efaSNicolas Royer }
853ebc82efaSNicolas Royer
atmel_sha_update_dma_stop(struct atmel_sha_dev * dd)8544c977e37STudor Ambarus static void atmel_sha_update_dma_stop(struct atmel_sha_dev *dd)
855ebc82efaSNicolas Royer {
856ebc82efaSNicolas Royer struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
857ebc82efaSNicolas Royer
858ebc82efaSNicolas Royer if (ctx->flags & SHA_FLAGS_SG) {
859ebc82efaSNicolas Royer dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE);
860ebc82efaSNicolas Royer if (ctx->sg->length == ctx->offset) {
861ebc82efaSNicolas Royer ctx->sg = sg_next(ctx->sg);
862ebc82efaSNicolas Royer if (ctx->sg)
863ebc82efaSNicolas Royer ctx->offset = 0;
864ebc82efaSNicolas Royer }
865d4905b38SNicolas Royer if (ctx->flags & SHA_FLAGS_PAD) {
866ebc82efaSNicolas Royer dma_unmap_single(dd->dev, ctx->dma_addr,
867d4905b38SNicolas Royer ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
868d4905b38SNicolas Royer }
869ebc82efaSNicolas Royer } else {
870ebc82efaSNicolas Royer dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen +
871d4905b38SNicolas Royer ctx->block_size, DMA_TO_DEVICE);
872ebc82efaSNicolas Royer }
873ebc82efaSNicolas Royer }
874ebc82efaSNicolas Royer
atmel_sha_update_req(struct atmel_sha_dev * dd)875ebc82efaSNicolas Royer static int atmel_sha_update_req(struct atmel_sha_dev *dd)
876ebc82efaSNicolas Royer {
877ebc82efaSNicolas Royer struct ahash_request *req = dd->req;
878ebc82efaSNicolas Royer struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
879ebc82efaSNicolas Royer int err;
880ebc82efaSNicolas Royer
881d4905b38SNicolas Royer dev_dbg(dd->dev, "update_req: total: %u, digcnt: 0x%llx 0x%llx\n",
882d4905b38SNicolas Royer ctx->total, ctx->digcnt[1], ctx->digcnt[0]);
883ebc82efaSNicolas Royer
884ebc82efaSNicolas Royer if (ctx->flags & SHA_FLAGS_CPU)
885ebc82efaSNicolas Royer err = atmel_sha_update_cpu(dd);
886ebc82efaSNicolas Royer else
887ebc82efaSNicolas Royer err = atmel_sha_update_dma_start(dd);
888ebc82efaSNicolas Royer
889ebc82efaSNicolas Royer /* wait for dma completion before can take more data */
890d4905b38SNicolas Royer dev_dbg(dd->dev, "update: err: %d, digcnt: 0x%llx 0%llx\n",
891d4905b38SNicolas Royer err, ctx->digcnt[1], ctx->digcnt[0]);
892ebc82efaSNicolas Royer
893ebc82efaSNicolas Royer return err;
894ebc82efaSNicolas Royer }
895ebc82efaSNicolas Royer
atmel_sha_final_req(struct atmel_sha_dev * dd)896ebc82efaSNicolas Royer static int atmel_sha_final_req(struct atmel_sha_dev *dd)
897ebc82efaSNicolas Royer {
898ebc82efaSNicolas Royer struct ahash_request *req = dd->req;
899ebc82efaSNicolas Royer struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
900ebc82efaSNicolas Royer int err = 0;
901ebc82efaSNicolas Royer int count;
902ebc82efaSNicolas Royer
903ebc82efaSNicolas Royer if (ctx->bufcnt >= ATMEL_SHA_DMA_THRESHOLD) {
904ebc82efaSNicolas Royer atmel_sha_fill_padding(ctx, 0);
905ebc82efaSNicolas Royer count = ctx->bufcnt;
906ebc82efaSNicolas Royer ctx->bufcnt = 0;
907ebc82efaSNicolas Royer err = atmel_sha_xmit_dma_map(dd, ctx, count, 1);
908ebc82efaSNicolas Royer }
909ebc82efaSNicolas Royer /* faster to handle last block with cpu */
910ebc82efaSNicolas Royer else {
911ebc82efaSNicolas Royer atmel_sha_fill_padding(ctx, 0);
912ebc82efaSNicolas Royer count = ctx->bufcnt;
913ebc82efaSNicolas Royer ctx->bufcnt = 0;
914ebc82efaSNicolas Royer err = atmel_sha_xmit_cpu(dd, ctx->buffer, count, 1);
915ebc82efaSNicolas Royer }
916ebc82efaSNicolas Royer
917ebc82efaSNicolas Royer dev_dbg(dd->dev, "final_req: err: %d\n", err);
918ebc82efaSNicolas Royer
919ebc82efaSNicolas Royer return err;
920ebc82efaSNicolas Royer }
921ebc82efaSNicolas Royer
atmel_sha_copy_hash(struct ahash_request * req)922ebc82efaSNicolas Royer static void atmel_sha_copy_hash(struct ahash_request *req)
923ebc82efaSNicolas Royer {
924ebc82efaSNicolas Royer struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
925ebc82efaSNicolas Royer u32 *hash = (u32 *)ctx->digest;
9267cee3508SCyrille Pitchen unsigned int i, hashsize;
927ebc82efaSNicolas Royer
9287cee3508SCyrille Pitchen switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
9297cee3508SCyrille Pitchen case SHA_FLAGS_SHA1:
9307cee3508SCyrille Pitchen hashsize = SHA1_DIGEST_SIZE;
9317cee3508SCyrille Pitchen break;
9327cee3508SCyrille Pitchen
9337cee3508SCyrille Pitchen case SHA_FLAGS_SHA224:
9347cee3508SCyrille Pitchen case SHA_FLAGS_SHA256:
9357cee3508SCyrille Pitchen hashsize = SHA256_DIGEST_SIZE;
9367cee3508SCyrille Pitchen break;
9377cee3508SCyrille Pitchen
9387cee3508SCyrille Pitchen case SHA_FLAGS_SHA384:
9397cee3508SCyrille Pitchen case SHA_FLAGS_SHA512:
9407cee3508SCyrille Pitchen hashsize = SHA512_DIGEST_SIZE;
9417cee3508SCyrille Pitchen break;
9427cee3508SCyrille Pitchen
9437cee3508SCyrille Pitchen default:
9447cee3508SCyrille Pitchen /* Should not happen... */
9457cee3508SCyrille Pitchen return;
9467cee3508SCyrille Pitchen }
9477cee3508SCyrille Pitchen
9487cee3508SCyrille Pitchen for (i = 0; i < hashsize / sizeof(u32); ++i)
949ebc82efaSNicolas Royer hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i));
9507cee3508SCyrille Pitchen ctx->flags |= SHA_FLAGS_RESTORE;
951ebc82efaSNicolas Royer }
952ebc82efaSNicolas Royer
atmel_sha_copy_ready_hash(struct ahash_request * req)953ebc82efaSNicolas Royer static void atmel_sha_copy_ready_hash(struct ahash_request *req)
954ebc82efaSNicolas Royer {
955ebc82efaSNicolas Royer struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
956ebc82efaSNicolas Royer
957ebc82efaSNicolas Royer if (!req->result)
958ebc82efaSNicolas Royer return;
959ebc82efaSNicolas Royer
960f07cebadSCyrille Pitchen switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
961f07cebadSCyrille Pitchen default:
962f07cebadSCyrille Pitchen case SHA_FLAGS_SHA1:
963ebc82efaSNicolas Royer memcpy(req->result, ctx->digest, SHA1_DIGEST_SIZE);
964f07cebadSCyrille Pitchen break;
965f07cebadSCyrille Pitchen
966f07cebadSCyrille Pitchen case SHA_FLAGS_SHA224:
967d4905b38SNicolas Royer memcpy(req->result, ctx->digest, SHA224_DIGEST_SIZE);
968f07cebadSCyrille Pitchen break;
969f07cebadSCyrille Pitchen
970f07cebadSCyrille Pitchen case SHA_FLAGS_SHA256:
971ebc82efaSNicolas Royer memcpy(req->result, ctx->digest, SHA256_DIGEST_SIZE);
972f07cebadSCyrille Pitchen break;
973f07cebadSCyrille Pitchen
974f07cebadSCyrille Pitchen case SHA_FLAGS_SHA384:
975d4905b38SNicolas Royer memcpy(req->result, ctx->digest, SHA384_DIGEST_SIZE);
976f07cebadSCyrille Pitchen break;
977f07cebadSCyrille Pitchen
978f07cebadSCyrille Pitchen case SHA_FLAGS_SHA512:
979d4905b38SNicolas Royer memcpy(req->result, ctx->digest, SHA512_DIGEST_SIZE);
980f07cebadSCyrille Pitchen break;
981f07cebadSCyrille Pitchen }
982ebc82efaSNicolas Royer }
983ebc82efaSNicolas Royer
atmel_sha_finish(struct ahash_request * req)984ebc82efaSNicolas Royer static int atmel_sha_finish(struct ahash_request *req)
985ebc82efaSNicolas Royer {
986ebc82efaSNicolas Royer struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
987ebc82efaSNicolas Royer struct atmel_sha_dev *dd = ctx->dd;
988ebc82efaSNicolas Royer
989d4905b38SNicolas Royer if (ctx->digcnt[0] || ctx->digcnt[1])
990ebc82efaSNicolas Royer atmel_sha_copy_ready_hash(req);
991ebc82efaSNicolas Royer
9924c147bcfSArnd Bergmann dev_dbg(dd->dev, "digcnt: 0x%llx 0x%llx, bufcnt: %zd\n", ctx->digcnt[1],
993d4905b38SNicolas Royer ctx->digcnt[0], ctx->bufcnt);
994ebc82efaSNicolas Royer
995871b88a8SRahul Pathak return 0;
996ebc82efaSNicolas Royer }
997ebc82efaSNicolas Royer
atmel_sha_finish_req(struct ahash_request * req,int err)998ebc82efaSNicolas Royer static void atmel_sha_finish_req(struct ahash_request *req, int err)
999ebc82efaSNicolas Royer {
1000ebc82efaSNicolas Royer struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1001ebc82efaSNicolas Royer struct atmel_sha_dev *dd = ctx->dd;
1002ebc82efaSNicolas Royer
1003ebc82efaSNicolas Royer if (!err) {
1004ebc82efaSNicolas Royer atmel_sha_copy_hash(req);
1005ebc82efaSNicolas Royer if (SHA_FLAGS_FINAL & dd->flags)
1006ebc82efaSNicolas Royer err = atmel_sha_finish(req);
1007ebc82efaSNicolas Royer } else {
1008ebc82efaSNicolas Royer ctx->flags |= SHA_FLAGS_ERROR;
1009ebc82efaSNicolas Royer }
1010ebc82efaSNicolas Royer
1011ebc82efaSNicolas Royer /* atomic operation is not needed here */
1012a29af939SCyrille Pitchen (void)atmel_sha_complete(dd, err);
1013ebc82efaSNicolas Royer }
1014ebc82efaSNicolas Royer
atmel_sha_hw_init(struct atmel_sha_dev * dd)1015ebc82efaSNicolas Royer static int atmel_sha_hw_init(struct atmel_sha_dev *dd)
1016ebc82efaSNicolas Royer {
10179d83d299SLABBE Corentin int err;
10189d83d299SLABBE Corentin
1019c033042aSCyrille Pitchen err = clk_enable(dd->iclk);
10209d83d299SLABBE Corentin if (err)
10219d83d299SLABBE Corentin return err;
1022ebc82efaSNicolas Royer
1023d4905b38SNicolas Royer if (!(SHA_FLAGS_INIT & dd->flags)) {
1024ebc82efaSNicolas Royer atmel_sha_write(dd, SHA_CR, SHA_CR_SWRST);
1025ebc82efaSNicolas Royer dd->flags |= SHA_FLAGS_INIT;
1026ebc82efaSNicolas Royer }
1027ebc82efaSNicolas Royer
1028ebc82efaSNicolas Royer return 0;
1029ebc82efaSNicolas Royer }
1030ebc82efaSNicolas Royer
atmel_sha_get_version(struct atmel_sha_dev * dd)1031d4905b38SNicolas Royer static inline unsigned int atmel_sha_get_version(struct atmel_sha_dev *dd)
1032d4905b38SNicolas Royer {
1033d4905b38SNicolas Royer return atmel_sha_read(dd, SHA_HW_VERSION) & 0x00000fff;
1034d4905b38SNicolas Royer }
1035d4905b38SNicolas Royer
atmel_sha_hw_version_init(struct atmel_sha_dev * dd)10360efe58f3STudor Ambarus static int atmel_sha_hw_version_init(struct atmel_sha_dev *dd)
1037d4905b38SNicolas Royer {
10380efe58f3STudor Ambarus int err;
10390efe58f3STudor Ambarus
10400efe58f3STudor Ambarus err = atmel_sha_hw_init(dd);
10410efe58f3STudor Ambarus if (err)
10420efe58f3STudor Ambarus return err;
1043d4905b38SNicolas Royer
1044d4905b38SNicolas Royer dd->hw_version = atmel_sha_get_version(dd);
1045d4905b38SNicolas Royer
1046d4905b38SNicolas Royer dev_info(dd->dev,
1047d4905b38SNicolas Royer "version: 0x%x\n", dd->hw_version);
1048d4905b38SNicolas Royer
1049c033042aSCyrille Pitchen clk_disable(dd->iclk);
10500efe58f3STudor Ambarus
10510efe58f3STudor Ambarus return 0;
1052d4905b38SNicolas Royer }
1053d4905b38SNicolas Royer
atmel_sha_handle_queue(struct atmel_sha_dev * dd,struct ahash_request * req)1054ebc82efaSNicolas Royer static int atmel_sha_handle_queue(struct atmel_sha_dev *dd,
1055ebc82efaSNicolas Royer struct ahash_request *req)
1056ebc82efaSNicolas Royer {
1057ebc82efaSNicolas Royer struct crypto_async_request *async_req, *backlog;
1058a29af939SCyrille Pitchen struct atmel_sha_ctx *ctx;
1059ebc82efaSNicolas Royer unsigned long flags;
1060a29af939SCyrille Pitchen bool start_async;
1061ebc82efaSNicolas Royer int err = 0, ret = 0;
1062ebc82efaSNicolas Royer
1063ebc82efaSNicolas Royer spin_lock_irqsave(&dd->lock, flags);
1064ebc82efaSNicolas Royer if (req)
1065ebc82efaSNicolas Royer ret = ahash_enqueue_request(&dd->queue, req);
1066ebc82efaSNicolas Royer
1067ebc82efaSNicolas Royer if (SHA_FLAGS_BUSY & dd->flags) {
1068ebc82efaSNicolas Royer spin_unlock_irqrestore(&dd->lock, flags);
1069ebc82efaSNicolas Royer return ret;
1070ebc82efaSNicolas Royer }
1071ebc82efaSNicolas Royer
1072ebc82efaSNicolas Royer backlog = crypto_get_backlog(&dd->queue);
1073ebc82efaSNicolas Royer async_req = crypto_dequeue_request(&dd->queue);
1074ebc82efaSNicolas Royer if (async_req)
1075ebc82efaSNicolas Royer dd->flags |= SHA_FLAGS_BUSY;
1076ebc82efaSNicolas Royer
1077ebc82efaSNicolas Royer spin_unlock_irqrestore(&dd->lock, flags);
1078ebc82efaSNicolas Royer
1079ebc82efaSNicolas Royer if (!async_req)
1080ebc82efaSNicolas Royer return ret;
1081ebc82efaSNicolas Royer
1082ebc82efaSNicolas Royer if (backlog)
10837d19abdcSHerbert Xu crypto_request_complete(backlog, -EINPROGRESS);
1084ebc82efaSNicolas Royer
1085a29af939SCyrille Pitchen ctx = crypto_tfm_ctx(async_req->tfm);
1086a29af939SCyrille Pitchen
1087a29af939SCyrille Pitchen dd->req = ahash_request_cast(async_req);
1088a29af939SCyrille Pitchen start_async = (dd->req != req);
1089a29af939SCyrille Pitchen dd->is_async = start_async;
109089a82ef8SCyrille Pitchen dd->force_complete = false;
1091a29af939SCyrille Pitchen
1092a29af939SCyrille Pitchen /* WARNING: ctx->start() MAY change dd->is_async. */
1093a29af939SCyrille Pitchen err = ctx->start(dd);
1094a29af939SCyrille Pitchen return (start_async) ? ret : err;
1095a29af939SCyrille Pitchen }
1096a29af939SCyrille Pitchen
1097b5ce82a7SCyrille Pitchen static int atmel_sha_done(struct atmel_sha_dev *dd);
1098b5ce82a7SCyrille Pitchen
atmel_sha_start(struct atmel_sha_dev * dd)1099a29af939SCyrille Pitchen static int atmel_sha_start(struct atmel_sha_dev *dd)
1100a29af939SCyrille Pitchen {
1101a29af939SCyrille Pitchen struct ahash_request *req = dd->req;
1102a29af939SCyrille Pitchen struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1103a29af939SCyrille Pitchen int err;
1104ebc82efaSNicolas Royer
11053eb75fc7SKai Ye dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %u\n",
1106ebc82efaSNicolas Royer ctx->op, req->nbytes);
1107ebc82efaSNicolas Royer
1108ebc82efaSNicolas Royer err = atmel_sha_hw_init(dd);
1109ebc82efaSNicolas Royer if (err)
111019998acbSCyrille Pitchen return atmel_sha_complete(dd, err);
111119998acbSCyrille Pitchen
111219998acbSCyrille Pitchen /*
111319998acbSCyrille Pitchen * atmel_sha_update_req() and atmel_sha_final_req() can return either:
111419998acbSCyrille Pitchen * -EINPROGRESS: the hardware is busy and the SHA driver will resume
111519998acbSCyrille Pitchen * its job later in the done_task.
111619998acbSCyrille Pitchen * This is the main path.
111719998acbSCyrille Pitchen *
111819998acbSCyrille Pitchen * 0: the SHA driver can continue its job then release the hardware
111919998acbSCyrille Pitchen * later, if needed, with atmel_sha_finish_req().
112019998acbSCyrille Pitchen * This is the alternate path.
112119998acbSCyrille Pitchen *
112219998acbSCyrille Pitchen * < 0: an error has occurred so atmel_sha_complete(dd, err) has already
112319998acbSCyrille Pitchen * been called, hence the hardware has been released.
112419998acbSCyrille Pitchen * The SHA driver must stop its job without calling
112519998acbSCyrille Pitchen * atmel_sha_finish_req(), otherwise atmel_sha_complete() would be
112619998acbSCyrille Pitchen * called a second time.
112719998acbSCyrille Pitchen *
112819998acbSCyrille Pitchen * Please note that currently, atmel_sha_final_req() never returns 0.
112919998acbSCyrille Pitchen */
1130ebc82efaSNicolas Royer
1131b5ce82a7SCyrille Pitchen dd->resume = atmel_sha_done;
1132ebc82efaSNicolas Royer if (ctx->op == SHA_OP_UPDATE) {
1133ebc82efaSNicolas Royer err = atmel_sha_update_req(dd);
113419998acbSCyrille Pitchen if (!err && (ctx->flags & SHA_FLAGS_FINUP))
1135ebc82efaSNicolas Royer /* no final() after finup() */
1136ebc82efaSNicolas Royer err = atmel_sha_final_req(dd);
1137ebc82efaSNicolas Royer } else if (ctx->op == SHA_OP_FINAL) {
1138ebc82efaSNicolas Royer err = atmel_sha_final_req(dd);
1139ebc82efaSNicolas Royer }
1140ebc82efaSNicolas Royer
114119998acbSCyrille Pitchen if (!err)
1142ebc82efaSNicolas Royer /* done_task will not finish it, so do it here */
1143ebc82efaSNicolas Royer atmel_sha_finish_req(req, err);
1144ebc82efaSNicolas Royer
1145ebc82efaSNicolas Royer dev_dbg(dd->dev, "exit, err: %d\n", err);
1146ebc82efaSNicolas Royer
1147a29af939SCyrille Pitchen return err;
1148ebc82efaSNicolas Royer }
1149ebc82efaSNicolas Royer
atmel_sha_enqueue(struct ahash_request * req,unsigned int op)1150ebc82efaSNicolas Royer static int atmel_sha_enqueue(struct ahash_request *req, unsigned int op)
1151ebc82efaSNicolas Royer {
1152ebc82efaSNicolas Royer struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1153ebc82efaSNicolas Royer struct atmel_sha_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
1154ebc82efaSNicolas Royer struct atmel_sha_dev *dd = tctx->dd;
1155ebc82efaSNicolas Royer
1156ebc82efaSNicolas Royer ctx->op = op;
1157ebc82efaSNicolas Royer
1158ebc82efaSNicolas Royer return atmel_sha_handle_queue(dd, req);
1159ebc82efaSNicolas Royer }
1160ebc82efaSNicolas Royer
atmel_sha_update(struct ahash_request * req)1161ebc82efaSNicolas Royer static int atmel_sha_update(struct ahash_request *req)
1162ebc82efaSNicolas Royer {
1163ebc82efaSNicolas Royer struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1164ebc82efaSNicolas Royer
1165ebc82efaSNicolas Royer if (!req->nbytes)
1166ebc82efaSNicolas Royer return 0;
1167ebc82efaSNicolas Royer
1168ebc82efaSNicolas Royer ctx->total = req->nbytes;
1169ebc82efaSNicolas Royer ctx->sg = req->src;
1170ebc82efaSNicolas Royer ctx->offset = 0;
1171ebc82efaSNicolas Royer
1172ebc82efaSNicolas Royer if (ctx->flags & SHA_FLAGS_FINUP) {
1173ebc82efaSNicolas Royer if (ctx->bufcnt + ctx->total < ATMEL_SHA_DMA_THRESHOLD)
1174ebc82efaSNicolas Royer /* faster to use CPU for short transfers */
1175ebc82efaSNicolas Royer ctx->flags |= SHA_FLAGS_CPU;
1176ebc82efaSNicolas Royer } else if (ctx->bufcnt + ctx->total < ctx->buflen) {
1177ebc82efaSNicolas Royer atmel_sha_append_sg(ctx);
1178ebc82efaSNicolas Royer return 0;
1179ebc82efaSNicolas Royer }
1180ebc82efaSNicolas Royer return atmel_sha_enqueue(req, SHA_OP_UPDATE);
1181ebc82efaSNicolas Royer }
1182ebc82efaSNicolas Royer
atmel_sha_final(struct ahash_request * req)1183ebc82efaSNicolas Royer static int atmel_sha_final(struct ahash_request *req)
1184ebc82efaSNicolas Royer {
1185ebc82efaSNicolas Royer struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1186ebc82efaSNicolas Royer
1187ebc82efaSNicolas Royer ctx->flags |= SHA_FLAGS_FINUP;
1188ebc82efaSNicolas Royer
1189ebc82efaSNicolas Royer if (ctx->flags & SHA_FLAGS_ERROR)
1190ebc82efaSNicolas Royer return 0; /* uncompleted hash is not needed */
1191ebc82efaSNicolas Royer
1192ad84112aSCyrille Pitchen if (ctx->flags & SHA_FLAGS_PAD)
1193ebc82efaSNicolas Royer /* copy ready hash (+ finalize hmac) */
1194ebc82efaSNicolas Royer return atmel_sha_finish(req);
1195ebc82efaSNicolas Royer
1196ad84112aSCyrille Pitchen return atmel_sha_enqueue(req, SHA_OP_FINAL);
1197ebc82efaSNicolas Royer }
1198ebc82efaSNicolas Royer
atmel_sha_finup(struct ahash_request * req)1199ebc82efaSNicolas Royer static int atmel_sha_finup(struct ahash_request *req)
1200ebc82efaSNicolas Royer {
1201ebc82efaSNicolas Royer struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1202ebc82efaSNicolas Royer int err1, err2;
1203ebc82efaSNicolas Royer
1204ebc82efaSNicolas Royer ctx->flags |= SHA_FLAGS_FINUP;
1205ebc82efaSNicolas Royer
1206ebc82efaSNicolas Royer err1 = atmel_sha_update(req);
12071606043fSGilad Ben-Yossef if (err1 == -EINPROGRESS ||
12081606043fSGilad Ben-Yossef (err1 == -EBUSY && (ahash_request_flags(req) &
12091606043fSGilad Ben-Yossef CRYPTO_TFM_REQ_MAY_BACKLOG)))
1210ebc82efaSNicolas Royer return err1;
1211ebc82efaSNicolas Royer
1212ebc82efaSNicolas Royer /*
1213ebc82efaSNicolas Royer * final() has to be always called to cleanup resources
1214ebc82efaSNicolas Royer * even if udpate() failed, except EINPROGRESS
1215ebc82efaSNicolas Royer */
1216ebc82efaSNicolas Royer err2 = atmel_sha_final(req);
1217ebc82efaSNicolas Royer
1218ebc82efaSNicolas Royer return err1 ?: err2;
1219ebc82efaSNicolas Royer }
1220ebc82efaSNicolas Royer
atmel_sha_digest(struct ahash_request * req)1221ebc82efaSNicolas Royer static int atmel_sha_digest(struct ahash_request *req)
1222ebc82efaSNicolas Royer {
1223ebc82efaSNicolas Royer return atmel_sha_init(req) ?: atmel_sha_finup(req);
1224ebc82efaSNicolas Royer }
1225ebc82efaSNicolas Royer
1226cc831d32SCyrille Pitchen
atmel_sha_export(struct ahash_request * req,void * out)1227cc831d32SCyrille Pitchen static int atmel_sha_export(struct ahash_request *req, void *out)
1228cc831d32SCyrille Pitchen {
1229cc831d32SCyrille Pitchen const struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1230cc831d32SCyrille Pitchen
12319c4274d9SCyrille Pitchen memcpy(out, ctx, sizeof(*ctx));
1232cc831d32SCyrille Pitchen return 0;
1233cc831d32SCyrille Pitchen }
1234cc831d32SCyrille Pitchen
atmel_sha_import(struct ahash_request * req,const void * in)1235cc831d32SCyrille Pitchen static int atmel_sha_import(struct ahash_request *req, const void *in)
1236cc831d32SCyrille Pitchen {
1237cc831d32SCyrille Pitchen struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1238cc831d32SCyrille Pitchen
12399c4274d9SCyrille Pitchen memcpy(ctx, in, sizeof(*ctx));
1240cc831d32SCyrille Pitchen return 0;
1241cc831d32SCyrille Pitchen }
1242cc831d32SCyrille Pitchen
atmel_sha_cra_init(struct crypto_tfm * tfm)1243be95f0faSSvenning Sørensen static int atmel_sha_cra_init(struct crypto_tfm *tfm)
1244ebc82efaSNicolas Royer {
1245a29af939SCyrille Pitchen struct atmel_sha_ctx *ctx = crypto_tfm_ctx(tfm);
1246a29af939SCyrille Pitchen
1247ebc82efaSNicolas Royer crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
12489c4274d9SCyrille Pitchen sizeof(struct atmel_sha_reqctx));
1249a29af939SCyrille Pitchen ctx->start = atmel_sha_start;
1250ebc82efaSNicolas Royer
1251ebc82efaSNicolas Royer return 0;
1252ebc82efaSNicolas Royer }
1253ebc82efaSNicolas Royer
atmel_sha_alg_init(struct ahash_alg * alg)1254aebe5bd7STudor Ambarus static void atmel_sha_alg_init(struct ahash_alg *alg)
1255aebe5bd7STudor Ambarus {
1256aebe5bd7STudor Ambarus alg->halg.base.cra_priority = ATMEL_SHA_PRIORITY;
1257aebe5bd7STudor Ambarus alg->halg.base.cra_flags = CRYPTO_ALG_ASYNC;
1258aebe5bd7STudor Ambarus alg->halg.base.cra_ctxsize = sizeof(struct atmel_sha_ctx);
1259aebe5bd7STudor Ambarus alg->halg.base.cra_module = THIS_MODULE;
1260aebe5bd7STudor Ambarus alg->halg.base.cra_init = atmel_sha_cra_init;
1261aebe5bd7STudor Ambarus
1262aebe5bd7STudor Ambarus alg->halg.statesize = sizeof(struct atmel_sha_reqctx);
1263aebe5bd7STudor Ambarus
1264aebe5bd7STudor Ambarus alg->init = atmel_sha_init;
1265aebe5bd7STudor Ambarus alg->update = atmel_sha_update;
1266aebe5bd7STudor Ambarus alg->final = atmel_sha_final;
1267aebe5bd7STudor Ambarus alg->finup = atmel_sha_finup;
1268aebe5bd7STudor Ambarus alg->digest = atmel_sha_digest;
1269aebe5bd7STudor Ambarus alg->export = atmel_sha_export;
1270aebe5bd7STudor Ambarus alg->import = atmel_sha_import;
1271aebe5bd7STudor Ambarus }
1272aebe5bd7STudor Ambarus
1273d4905b38SNicolas Royer static struct ahash_alg sha_1_256_algs[] = {
1274ebc82efaSNicolas Royer {
1275aebe5bd7STudor Ambarus .halg.base.cra_name = "sha1",
1276aebe5bd7STudor Ambarus .halg.base.cra_driver_name = "atmel-sha1",
1277aebe5bd7STudor Ambarus .halg.base.cra_blocksize = SHA1_BLOCK_SIZE,
1278aebe5bd7STudor Ambarus
1279aebe5bd7STudor Ambarus .halg.digestsize = SHA1_DIGEST_SIZE,
1280ebc82efaSNicolas Royer },
1281ebc82efaSNicolas Royer {
1282aebe5bd7STudor Ambarus .halg.base.cra_name = "sha256",
1283aebe5bd7STudor Ambarus .halg.base.cra_driver_name = "atmel-sha256",
1284aebe5bd7STudor Ambarus .halg.base.cra_blocksize = SHA256_BLOCK_SIZE,
1285aebe5bd7STudor Ambarus
1286aebe5bd7STudor Ambarus .halg.digestsize = SHA256_DIGEST_SIZE,
1287ebc82efaSNicolas Royer },
1288ebc82efaSNicolas Royer };
1289ebc82efaSNicolas Royer
1290d4905b38SNicolas Royer static struct ahash_alg sha_224_alg = {
1291aebe5bd7STudor Ambarus .halg.base.cra_name = "sha224",
1292aebe5bd7STudor Ambarus .halg.base.cra_driver_name = "atmel-sha224",
1293aebe5bd7STudor Ambarus .halg.base.cra_blocksize = SHA224_BLOCK_SIZE,
1294aebe5bd7STudor Ambarus
1295aebe5bd7STudor Ambarus .halg.digestsize = SHA224_DIGEST_SIZE,
1296d4905b38SNicolas Royer };
1297d4905b38SNicolas Royer
1298d4905b38SNicolas Royer static struct ahash_alg sha_384_512_algs[] = {
1299d4905b38SNicolas Royer {
1300aebe5bd7STudor Ambarus .halg.base.cra_name = "sha384",
1301aebe5bd7STudor Ambarus .halg.base.cra_driver_name = "atmel-sha384",
1302aebe5bd7STudor Ambarus .halg.base.cra_blocksize = SHA384_BLOCK_SIZE,
1303aebe5bd7STudor Ambarus .halg.base.cra_alignmask = 0x3,
1304aebe5bd7STudor Ambarus
1305aebe5bd7STudor Ambarus .halg.digestsize = SHA384_DIGEST_SIZE,
1306d4905b38SNicolas Royer },
1307d4905b38SNicolas Royer {
1308aebe5bd7STudor Ambarus .halg.base.cra_name = "sha512",
1309aebe5bd7STudor Ambarus .halg.base.cra_driver_name = "atmel-sha512",
1310aebe5bd7STudor Ambarus .halg.base.cra_blocksize = SHA512_BLOCK_SIZE,
1311aebe5bd7STudor Ambarus .halg.base.cra_alignmask = 0x3,
1312aebe5bd7STudor Ambarus
1313aebe5bd7STudor Ambarus .halg.digestsize = SHA512_DIGEST_SIZE,
1314d4905b38SNicolas Royer },
1315d4905b38SNicolas Royer };
1316d4905b38SNicolas Royer
atmel_sha_queue_task(unsigned long data)1317f56809c3SCyrille Pitchen static void atmel_sha_queue_task(unsigned long data)
1318f56809c3SCyrille Pitchen {
1319f56809c3SCyrille Pitchen struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
1320f56809c3SCyrille Pitchen
1321f56809c3SCyrille Pitchen atmel_sha_handle_queue(dd, NULL);
1322f56809c3SCyrille Pitchen }
1323f56809c3SCyrille Pitchen
atmel_sha_done(struct atmel_sha_dev * dd)1324b5ce82a7SCyrille Pitchen static int atmel_sha_done(struct atmel_sha_dev *dd)
1325ebc82efaSNicolas Royer {
1326ebc82efaSNicolas Royer int err = 0;
1327ebc82efaSNicolas Royer
1328ebc82efaSNicolas Royer if (SHA_FLAGS_CPU & dd->flags) {
1329ebc82efaSNicolas Royer if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
1330ebc82efaSNicolas Royer dd->flags &= ~SHA_FLAGS_OUTPUT_READY;
1331ebc82efaSNicolas Royer goto finish;
1332ebc82efaSNicolas Royer }
1333ebc82efaSNicolas Royer } else if (SHA_FLAGS_DMA_READY & dd->flags) {
1334ebc82efaSNicolas Royer if (SHA_FLAGS_DMA_ACTIVE & dd->flags) {
1335ebc82efaSNicolas Royer dd->flags &= ~SHA_FLAGS_DMA_ACTIVE;
1336ebc82efaSNicolas Royer atmel_sha_update_dma_stop(dd);
1337ebc82efaSNicolas Royer }
1338ebc82efaSNicolas Royer if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
1339ebc82efaSNicolas Royer /* hash or semi-hash ready */
1340ebc82efaSNicolas Royer dd->flags &= ~(SHA_FLAGS_DMA_READY |
1341ebc82efaSNicolas Royer SHA_FLAGS_OUTPUT_READY);
1342ebc82efaSNicolas Royer err = atmel_sha_update_dma_start(dd);
1343ebc82efaSNicolas Royer if (err != -EINPROGRESS)
1344ebc82efaSNicolas Royer goto finish;
1345ebc82efaSNicolas Royer }
1346ebc82efaSNicolas Royer }
1347b5ce82a7SCyrille Pitchen return err;
1348ebc82efaSNicolas Royer
1349ebc82efaSNicolas Royer finish:
1350ebc82efaSNicolas Royer /* finish curent request */
1351ebc82efaSNicolas Royer atmel_sha_finish_req(dd->req, err);
1352b5ce82a7SCyrille Pitchen
1353b5ce82a7SCyrille Pitchen return err;
1354b5ce82a7SCyrille Pitchen }
1355b5ce82a7SCyrille Pitchen
atmel_sha_done_task(unsigned long data)1356b5ce82a7SCyrille Pitchen static void atmel_sha_done_task(unsigned long data)
1357b5ce82a7SCyrille Pitchen {
1358b5ce82a7SCyrille Pitchen struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
1359b5ce82a7SCyrille Pitchen
1360b5ce82a7SCyrille Pitchen dd->is_async = true;
1361b5ce82a7SCyrille Pitchen (void)dd->resume(dd);
1362ebc82efaSNicolas Royer }
1363ebc82efaSNicolas Royer
atmel_sha_irq(int irq,void * dev_id)1364ebc82efaSNicolas Royer static irqreturn_t atmel_sha_irq(int irq, void *dev_id)
1365ebc82efaSNicolas Royer {
1366ebc82efaSNicolas Royer struct atmel_sha_dev *sha_dd = dev_id;
1367ebc82efaSNicolas Royer u32 reg;
1368ebc82efaSNicolas Royer
1369ebc82efaSNicolas Royer reg = atmel_sha_read(sha_dd, SHA_ISR);
1370ebc82efaSNicolas Royer if (reg & atmel_sha_read(sha_dd, SHA_IMR)) {
1371ebc82efaSNicolas Royer atmel_sha_write(sha_dd, SHA_IDR, reg);
1372ebc82efaSNicolas Royer if (SHA_FLAGS_BUSY & sha_dd->flags) {
1373ebc82efaSNicolas Royer sha_dd->flags |= SHA_FLAGS_OUTPUT_READY;
1374ebc82efaSNicolas Royer if (!(SHA_FLAGS_CPU & sha_dd->flags))
1375ebc82efaSNicolas Royer sha_dd->flags |= SHA_FLAGS_DMA_READY;
1376ebc82efaSNicolas Royer tasklet_schedule(&sha_dd->done_task);
1377ebc82efaSNicolas Royer } else {
1378ebc82efaSNicolas Royer dev_warn(sha_dd->dev, "SHA interrupt when no active requests.\n");
1379ebc82efaSNicolas Royer }
1380ebc82efaSNicolas Royer return IRQ_HANDLED;
1381ebc82efaSNicolas Royer }
1382ebc82efaSNicolas Royer
1383ebc82efaSNicolas Royer return IRQ_NONE;
1384ebc82efaSNicolas Royer }
1385ebc82efaSNicolas Royer
1386eec12f66SCyrille Pitchen
138769303cf0SCyrille Pitchen /* DMA transfer functions */
138869303cf0SCyrille Pitchen
atmel_sha_dma_check_aligned(struct atmel_sha_dev * dd,struct scatterlist * sg,size_t len)138969303cf0SCyrille Pitchen static bool atmel_sha_dma_check_aligned(struct atmel_sha_dev *dd,
139069303cf0SCyrille Pitchen struct scatterlist *sg,
139169303cf0SCyrille Pitchen size_t len)
139269303cf0SCyrille Pitchen {
139369303cf0SCyrille Pitchen struct atmel_sha_dma *dma = &dd->dma_lch_in;
139469303cf0SCyrille Pitchen struct ahash_request *req = dd->req;
139569303cf0SCyrille Pitchen struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
139669303cf0SCyrille Pitchen size_t bs = ctx->block_size;
139769303cf0SCyrille Pitchen int nents;
139869303cf0SCyrille Pitchen
139969303cf0SCyrille Pitchen for (nents = 0; sg; sg = sg_next(sg), ++nents) {
140069303cf0SCyrille Pitchen if (!IS_ALIGNED(sg->offset, sizeof(u32)))
140169303cf0SCyrille Pitchen return false;
140269303cf0SCyrille Pitchen
140369303cf0SCyrille Pitchen /*
140469303cf0SCyrille Pitchen * This is the last sg, the only one that is allowed to
140569303cf0SCyrille Pitchen * have an unaligned length.
140669303cf0SCyrille Pitchen */
140769303cf0SCyrille Pitchen if (len <= sg->length) {
140869303cf0SCyrille Pitchen dma->nents = nents + 1;
140969303cf0SCyrille Pitchen dma->last_sg_length = sg->length;
141069303cf0SCyrille Pitchen sg->length = ALIGN(len, sizeof(u32));
141169303cf0SCyrille Pitchen return true;
141269303cf0SCyrille Pitchen }
141369303cf0SCyrille Pitchen
141469303cf0SCyrille Pitchen /* All other sg lengths MUST be aligned to the block size. */
141569303cf0SCyrille Pitchen if (!IS_ALIGNED(sg->length, bs))
141669303cf0SCyrille Pitchen return false;
141769303cf0SCyrille Pitchen
141869303cf0SCyrille Pitchen len -= sg->length;
141969303cf0SCyrille Pitchen }
142069303cf0SCyrille Pitchen
142169303cf0SCyrille Pitchen return false;
142269303cf0SCyrille Pitchen }
142369303cf0SCyrille Pitchen
atmel_sha_dma_callback2(void * data)142469303cf0SCyrille Pitchen static void atmel_sha_dma_callback2(void *data)
142569303cf0SCyrille Pitchen {
142669303cf0SCyrille Pitchen struct atmel_sha_dev *dd = data;
142769303cf0SCyrille Pitchen struct atmel_sha_dma *dma = &dd->dma_lch_in;
142869303cf0SCyrille Pitchen struct scatterlist *sg;
142969303cf0SCyrille Pitchen int nents;
143069303cf0SCyrille Pitchen
143169303cf0SCyrille Pitchen dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
143269303cf0SCyrille Pitchen
143369303cf0SCyrille Pitchen sg = dma->sg;
143469303cf0SCyrille Pitchen for (nents = 0; nents < dma->nents - 1; ++nents)
143569303cf0SCyrille Pitchen sg = sg_next(sg);
143669303cf0SCyrille Pitchen sg->length = dma->last_sg_length;
143769303cf0SCyrille Pitchen
143869303cf0SCyrille Pitchen dd->is_async = true;
143969303cf0SCyrille Pitchen (void)atmel_sha_wait_for_data_ready(dd, dd->resume);
144069303cf0SCyrille Pitchen }
144169303cf0SCyrille Pitchen
atmel_sha_dma_start(struct atmel_sha_dev * dd,struct scatterlist * src,size_t len,atmel_sha_fn_t resume)144269303cf0SCyrille Pitchen static int atmel_sha_dma_start(struct atmel_sha_dev *dd,
144369303cf0SCyrille Pitchen struct scatterlist *src,
144469303cf0SCyrille Pitchen size_t len,
144569303cf0SCyrille Pitchen atmel_sha_fn_t resume)
144669303cf0SCyrille Pitchen {
144769303cf0SCyrille Pitchen struct atmel_sha_dma *dma = &dd->dma_lch_in;
144869303cf0SCyrille Pitchen struct dma_slave_config *config = &dma->dma_conf;
144969303cf0SCyrille Pitchen struct dma_chan *chan = dma->chan;
145069303cf0SCyrille Pitchen struct dma_async_tx_descriptor *desc;
145169303cf0SCyrille Pitchen dma_cookie_t cookie;
145269303cf0SCyrille Pitchen unsigned int sg_len;
145369303cf0SCyrille Pitchen int err;
145469303cf0SCyrille Pitchen
145569303cf0SCyrille Pitchen dd->resume = resume;
145669303cf0SCyrille Pitchen
145769303cf0SCyrille Pitchen /*
145869303cf0SCyrille Pitchen * dma->nents has already been initialized by
145969303cf0SCyrille Pitchen * atmel_sha_dma_check_aligned().
146069303cf0SCyrille Pitchen */
146169303cf0SCyrille Pitchen dma->sg = src;
146269303cf0SCyrille Pitchen sg_len = dma_map_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
146369303cf0SCyrille Pitchen if (!sg_len) {
146469303cf0SCyrille Pitchen err = -ENOMEM;
146569303cf0SCyrille Pitchen goto exit;
146669303cf0SCyrille Pitchen }
146769303cf0SCyrille Pitchen
146869303cf0SCyrille Pitchen config->src_maxburst = 16;
146969303cf0SCyrille Pitchen config->dst_maxburst = 16;
147069303cf0SCyrille Pitchen err = dmaengine_slave_config(chan, config);
147169303cf0SCyrille Pitchen if (err)
147269303cf0SCyrille Pitchen goto unmap_sg;
147369303cf0SCyrille Pitchen
147469303cf0SCyrille Pitchen desc = dmaengine_prep_slave_sg(chan, dma->sg, sg_len, DMA_MEM_TO_DEV,
147569303cf0SCyrille Pitchen DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
147669303cf0SCyrille Pitchen if (!desc) {
147769303cf0SCyrille Pitchen err = -ENOMEM;
147869303cf0SCyrille Pitchen goto unmap_sg;
147969303cf0SCyrille Pitchen }
148069303cf0SCyrille Pitchen
148169303cf0SCyrille Pitchen desc->callback = atmel_sha_dma_callback2;
148269303cf0SCyrille Pitchen desc->callback_param = dd;
148369303cf0SCyrille Pitchen cookie = dmaengine_submit(desc);
148469303cf0SCyrille Pitchen err = dma_submit_error(cookie);
148569303cf0SCyrille Pitchen if (err)
148669303cf0SCyrille Pitchen goto unmap_sg;
148769303cf0SCyrille Pitchen
148869303cf0SCyrille Pitchen dma_async_issue_pending(chan);
148969303cf0SCyrille Pitchen
149069303cf0SCyrille Pitchen return -EINPROGRESS;
149169303cf0SCyrille Pitchen
149269303cf0SCyrille Pitchen unmap_sg:
149369303cf0SCyrille Pitchen dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
149469303cf0SCyrille Pitchen exit:
149569303cf0SCyrille Pitchen return atmel_sha_complete(dd, err);
149669303cf0SCyrille Pitchen }
149769303cf0SCyrille Pitchen
149869303cf0SCyrille Pitchen
1499eec12f66SCyrille Pitchen /* CPU transfer functions */
1500eec12f66SCyrille Pitchen
atmel_sha_cpu_transfer(struct atmel_sha_dev * dd)1501eec12f66SCyrille Pitchen static int atmel_sha_cpu_transfer(struct atmel_sha_dev *dd)
1502eec12f66SCyrille Pitchen {
1503eec12f66SCyrille Pitchen struct ahash_request *req = dd->req;
1504eec12f66SCyrille Pitchen struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1505eec12f66SCyrille Pitchen const u32 *words = (const u32 *)ctx->buffer;
1506eec12f66SCyrille Pitchen size_t i, num_words;
1507eec12f66SCyrille Pitchen u32 isr, din, din_inc;
1508eec12f66SCyrille Pitchen
1509eec12f66SCyrille Pitchen din_inc = (ctx->flags & SHA_FLAGS_IDATAR0) ? 0 : 1;
1510eec12f66SCyrille Pitchen for (;;) {
1511eec12f66SCyrille Pitchen /* Write data into the Input Data Registers. */
1512eec12f66SCyrille Pitchen num_words = DIV_ROUND_UP(ctx->bufcnt, sizeof(u32));
1513eec12f66SCyrille Pitchen for (i = 0, din = 0; i < num_words; ++i, din += din_inc)
1514eec12f66SCyrille Pitchen atmel_sha_write(dd, SHA_REG_DIN(din), words[i]);
1515eec12f66SCyrille Pitchen
1516eec12f66SCyrille Pitchen ctx->offset += ctx->bufcnt;
1517eec12f66SCyrille Pitchen ctx->total -= ctx->bufcnt;
1518eec12f66SCyrille Pitchen
1519eec12f66SCyrille Pitchen if (!ctx->total)
1520eec12f66SCyrille Pitchen break;
1521eec12f66SCyrille Pitchen
1522eec12f66SCyrille Pitchen /*
1523eec12f66SCyrille Pitchen * Prepare next block:
1524eec12f66SCyrille Pitchen * Fill ctx->buffer now with the next data to be written into
1525eec12f66SCyrille Pitchen * IDATARx: it gives time for the SHA hardware to process
1526eec12f66SCyrille Pitchen * the current data so the SHA_INT_DATARDY flag might be set
1527eec12f66SCyrille Pitchen * in SHA_ISR when polling this register at the beginning of
1528eec12f66SCyrille Pitchen * the next loop.
1529eec12f66SCyrille Pitchen */
1530eec12f66SCyrille Pitchen ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total);
1531eec12f66SCyrille Pitchen scatterwalk_map_and_copy(ctx->buffer, ctx->sg,
1532eec12f66SCyrille Pitchen ctx->offset, ctx->bufcnt, 0);
1533eec12f66SCyrille Pitchen
1534eec12f66SCyrille Pitchen /* Wait for hardware to be ready again. */
1535eec12f66SCyrille Pitchen isr = atmel_sha_read(dd, SHA_ISR);
1536eec12f66SCyrille Pitchen if (!(isr & SHA_INT_DATARDY)) {
1537eec12f66SCyrille Pitchen /* Not ready yet. */
1538eec12f66SCyrille Pitchen dd->resume = atmel_sha_cpu_transfer;
1539eec12f66SCyrille Pitchen atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
1540eec12f66SCyrille Pitchen return -EINPROGRESS;
1541eec12f66SCyrille Pitchen }
1542eec12f66SCyrille Pitchen }
1543eec12f66SCyrille Pitchen
1544eec12f66SCyrille Pitchen if (unlikely(!(ctx->flags & SHA_FLAGS_WAIT_DATARDY)))
1545eec12f66SCyrille Pitchen return dd->cpu_transfer_complete(dd);
1546eec12f66SCyrille Pitchen
1547eec12f66SCyrille Pitchen return atmel_sha_wait_for_data_ready(dd, dd->cpu_transfer_complete);
1548eec12f66SCyrille Pitchen }
1549eec12f66SCyrille Pitchen
atmel_sha_cpu_start(struct atmel_sha_dev * dd,struct scatterlist * sg,unsigned int len,bool idatar0_only,bool wait_data_ready,atmel_sha_fn_t resume)1550eec12f66SCyrille Pitchen static int atmel_sha_cpu_start(struct atmel_sha_dev *dd,
1551eec12f66SCyrille Pitchen struct scatterlist *sg,
1552eec12f66SCyrille Pitchen unsigned int len,
1553eec12f66SCyrille Pitchen bool idatar0_only,
1554eec12f66SCyrille Pitchen bool wait_data_ready,
1555eec12f66SCyrille Pitchen atmel_sha_fn_t resume)
1556eec12f66SCyrille Pitchen {
1557eec12f66SCyrille Pitchen struct ahash_request *req = dd->req;
1558eec12f66SCyrille Pitchen struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1559eec12f66SCyrille Pitchen
1560eec12f66SCyrille Pitchen if (!len)
1561eec12f66SCyrille Pitchen return resume(dd);
1562eec12f66SCyrille Pitchen
1563eec12f66SCyrille Pitchen ctx->flags &= ~(SHA_FLAGS_IDATAR0 | SHA_FLAGS_WAIT_DATARDY);
1564eec12f66SCyrille Pitchen
1565eec12f66SCyrille Pitchen if (idatar0_only)
1566eec12f66SCyrille Pitchen ctx->flags |= SHA_FLAGS_IDATAR0;
1567eec12f66SCyrille Pitchen
1568eec12f66SCyrille Pitchen if (wait_data_ready)
1569eec12f66SCyrille Pitchen ctx->flags |= SHA_FLAGS_WAIT_DATARDY;
1570eec12f66SCyrille Pitchen
1571eec12f66SCyrille Pitchen ctx->sg = sg;
1572eec12f66SCyrille Pitchen ctx->total = len;
1573eec12f66SCyrille Pitchen ctx->offset = 0;
1574eec12f66SCyrille Pitchen
1575eec12f66SCyrille Pitchen /* Prepare the first block to be written. */
1576eec12f66SCyrille Pitchen ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total);
1577eec12f66SCyrille Pitchen scatterwalk_map_and_copy(ctx->buffer, ctx->sg,
1578eec12f66SCyrille Pitchen ctx->offset, ctx->bufcnt, 0);
1579eec12f66SCyrille Pitchen
1580eec12f66SCyrille Pitchen dd->cpu_transfer_complete = resume;
1581eec12f66SCyrille Pitchen return atmel_sha_cpu_transfer(dd);
1582eec12f66SCyrille Pitchen }
1583eec12f66SCyrille Pitchen
atmel_sha_cpu_hash(struct atmel_sha_dev * dd,const void * data,unsigned int datalen,bool auto_padding,atmel_sha_fn_t resume)158481d8750bSCyrille Pitchen static int atmel_sha_cpu_hash(struct atmel_sha_dev *dd,
158581d8750bSCyrille Pitchen const void *data, unsigned int datalen,
158681d8750bSCyrille Pitchen bool auto_padding,
158781d8750bSCyrille Pitchen atmel_sha_fn_t resume)
158881d8750bSCyrille Pitchen {
158981d8750bSCyrille Pitchen struct ahash_request *req = dd->req;
159081d8750bSCyrille Pitchen struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
159181d8750bSCyrille Pitchen u32 msglen = (auto_padding) ? datalen : 0;
159281d8750bSCyrille Pitchen u32 mr = SHA_MR_MODE_AUTO;
159381d8750bSCyrille Pitchen
159481d8750bSCyrille Pitchen if (!(IS_ALIGNED(datalen, ctx->block_size) || auto_padding))
159581d8750bSCyrille Pitchen return atmel_sha_complete(dd, -EINVAL);
159681d8750bSCyrille Pitchen
159781d8750bSCyrille Pitchen mr |= (ctx->flags & SHA_FLAGS_ALGO_MASK);
159881d8750bSCyrille Pitchen atmel_sha_write(dd, SHA_MR, mr);
159981d8750bSCyrille Pitchen atmel_sha_write(dd, SHA_MSR, msglen);
160081d8750bSCyrille Pitchen atmel_sha_write(dd, SHA_BCR, msglen);
160181d8750bSCyrille Pitchen atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
160281d8750bSCyrille Pitchen
160381d8750bSCyrille Pitchen sg_init_one(&dd->tmp, data, datalen);
160481d8750bSCyrille Pitchen return atmel_sha_cpu_start(dd, &dd->tmp, datalen, false, true, resume);
160581d8750bSCyrille Pitchen }
160681d8750bSCyrille Pitchen
160781d8750bSCyrille Pitchen
160881d8750bSCyrille Pitchen /* hmac functions */
160981d8750bSCyrille Pitchen
161081d8750bSCyrille Pitchen struct atmel_sha_hmac_key {
161181d8750bSCyrille Pitchen bool valid;
161281d8750bSCyrille Pitchen unsigned int keylen;
161381d8750bSCyrille Pitchen u8 buffer[SHA512_BLOCK_SIZE];
161481d8750bSCyrille Pitchen u8 *keydup;
161581d8750bSCyrille Pitchen };
161681d8750bSCyrille Pitchen
atmel_sha_hmac_key_init(struct atmel_sha_hmac_key * hkey)161781d8750bSCyrille Pitchen static inline void atmel_sha_hmac_key_init(struct atmel_sha_hmac_key *hkey)
161881d8750bSCyrille Pitchen {
161981d8750bSCyrille Pitchen memset(hkey, 0, sizeof(*hkey));
162081d8750bSCyrille Pitchen }
162181d8750bSCyrille Pitchen
atmel_sha_hmac_key_release(struct atmel_sha_hmac_key * hkey)162281d8750bSCyrille Pitchen static inline void atmel_sha_hmac_key_release(struct atmel_sha_hmac_key *hkey)
162381d8750bSCyrille Pitchen {
162481d8750bSCyrille Pitchen kfree(hkey->keydup);
162581d8750bSCyrille Pitchen memset(hkey, 0, sizeof(*hkey));
162681d8750bSCyrille Pitchen }
162781d8750bSCyrille Pitchen
atmel_sha_hmac_key_set(struct atmel_sha_hmac_key * hkey,const u8 * key,unsigned int keylen)162881d8750bSCyrille Pitchen static inline int atmel_sha_hmac_key_set(struct atmel_sha_hmac_key *hkey,
162981d8750bSCyrille Pitchen const u8 *key,
163081d8750bSCyrille Pitchen unsigned int keylen)
163181d8750bSCyrille Pitchen {
163281d8750bSCyrille Pitchen atmel_sha_hmac_key_release(hkey);
163381d8750bSCyrille Pitchen
163481d8750bSCyrille Pitchen if (keylen > sizeof(hkey->buffer)) {
163581d8750bSCyrille Pitchen hkey->keydup = kmemdup(key, keylen, GFP_KERNEL);
163681d8750bSCyrille Pitchen if (!hkey->keydup)
163781d8750bSCyrille Pitchen return -ENOMEM;
163881d8750bSCyrille Pitchen
163981d8750bSCyrille Pitchen } else {
164081d8750bSCyrille Pitchen memcpy(hkey->buffer, key, keylen);
164181d8750bSCyrille Pitchen }
164281d8750bSCyrille Pitchen
164381d8750bSCyrille Pitchen hkey->valid = true;
164481d8750bSCyrille Pitchen hkey->keylen = keylen;
164581d8750bSCyrille Pitchen return 0;
164681d8750bSCyrille Pitchen }
164781d8750bSCyrille Pitchen
atmel_sha_hmac_key_get(const struct atmel_sha_hmac_key * hkey,const u8 ** key,unsigned int * keylen)164881d8750bSCyrille Pitchen static inline bool atmel_sha_hmac_key_get(const struct atmel_sha_hmac_key *hkey,
164981d8750bSCyrille Pitchen const u8 **key,
165081d8750bSCyrille Pitchen unsigned int *keylen)
165181d8750bSCyrille Pitchen {
165281d8750bSCyrille Pitchen if (!hkey->valid)
165381d8750bSCyrille Pitchen return false;
165481d8750bSCyrille Pitchen
165581d8750bSCyrille Pitchen *keylen = hkey->keylen;
165681d8750bSCyrille Pitchen *key = (hkey->keydup) ? hkey->keydup : hkey->buffer;
165781d8750bSCyrille Pitchen return true;
165881d8750bSCyrille Pitchen }
165981d8750bSCyrille Pitchen
166081d8750bSCyrille Pitchen
166181d8750bSCyrille Pitchen struct atmel_sha_hmac_ctx {
166281d8750bSCyrille Pitchen struct atmel_sha_ctx base;
166381d8750bSCyrille Pitchen
166481d8750bSCyrille Pitchen struct atmel_sha_hmac_key hkey;
166581d8750bSCyrille Pitchen u32 ipad[SHA512_BLOCK_SIZE / sizeof(u32)];
166681d8750bSCyrille Pitchen u32 opad[SHA512_BLOCK_SIZE / sizeof(u32)];
166781d8750bSCyrille Pitchen atmel_sha_fn_t resume;
166881d8750bSCyrille Pitchen };
166981d8750bSCyrille Pitchen
167081d8750bSCyrille Pitchen static int atmel_sha_hmac_setup(struct atmel_sha_dev *dd,
167181d8750bSCyrille Pitchen atmel_sha_fn_t resume);
167281d8750bSCyrille Pitchen static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev *dd,
167381d8750bSCyrille Pitchen const u8 *key, unsigned int keylen);
167481d8750bSCyrille Pitchen static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev *dd);
167581d8750bSCyrille Pitchen static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev *dd);
167681d8750bSCyrille Pitchen static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev *dd);
167781d8750bSCyrille Pitchen static int atmel_sha_hmac_setup_done(struct atmel_sha_dev *dd);
167881d8750bSCyrille Pitchen
167981d8750bSCyrille Pitchen static int atmel_sha_hmac_init_done(struct atmel_sha_dev *dd);
168081d8750bSCyrille Pitchen static int atmel_sha_hmac_final(struct atmel_sha_dev *dd);
168181d8750bSCyrille Pitchen static int atmel_sha_hmac_final_done(struct atmel_sha_dev *dd);
168281d8750bSCyrille Pitchen static int atmel_sha_hmac_digest2(struct atmel_sha_dev *dd);
168381d8750bSCyrille Pitchen
atmel_sha_hmac_setup(struct atmel_sha_dev * dd,atmel_sha_fn_t resume)168481d8750bSCyrille Pitchen static int atmel_sha_hmac_setup(struct atmel_sha_dev *dd,
168581d8750bSCyrille Pitchen atmel_sha_fn_t resume)
168681d8750bSCyrille Pitchen {
168781d8750bSCyrille Pitchen struct ahash_request *req = dd->req;
168881d8750bSCyrille Pitchen struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
168981d8750bSCyrille Pitchen struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
169081d8750bSCyrille Pitchen struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
169181d8750bSCyrille Pitchen unsigned int keylen;
169281d8750bSCyrille Pitchen const u8 *key;
169381d8750bSCyrille Pitchen size_t bs;
169481d8750bSCyrille Pitchen
169581d8750bSCyrille Pitchen hmac->resume = resume;
169681d8750bSCyrille Pitchen switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
169781d8750bSCyrille Pitchen case SHA_FLAGS_SHA1:
169881d8750bSCyrille Pitchen ctx->block_size = SHA1_BLOCK_SIZE;
169981d8750bSCyrille Pitchen ctx->hash_size = SHA1_DIGEST_SIZE;
170081d8750bSCyrille Pitchen break;
170181d8750bSCyrille Pitchen
170281d8750bSCyrille Pitchen case SHA_FLAGS_SHA224:
170381d8750bSCyrille Pitchen ctx->block_size = SHA224_BLOCK_SIZE;
170481d8750bSCyrille Pitchen ctx->hash_size = SHA256_DIGEST_SIZE;
170581d8750bSCyrille Pitchen break;
170681d8750bSCyrille Pitchen
170781d8750bSCyrille Pitchen case SHA_FLAGS_SHA256:
170881d8750bSCyrille Pitchen ctx->block_size = SHA256_BLOCK_SIZE;
170981d8750bSCyrille Pitchen ctx->hash_size = SHA256_DIGEST_SIZE;
171081d8750bSCyrille Pitchen break;
171181d8750bSCyrille Pitchen
171281d8750bSCyrille Pitchen case SHA_FLAGS_SHA384:
171381d8750bSCyrille Pitchen ctx->block_size = SHA384_BLOCK_SIZE;
171481d8750bSCyrille Pitchen ctx->hash_size = SHA512_DIGEST_SIZE;
171581d8750bSCyrille Pitchen break;
171681d8750bSCyrille Pitchen
171781d8750bSCyrille Pitchen case SHA_FLAGS_SHA512:
171881d8750bSCyrille Pitchen ctx->block_size = SHA512_BLOCK_SIZE;
171981d8750bSCyrille Pitchen ctx->hash_size = SHA512_DIGEST_SIZE;
172081d8750bSCyrille Pitchen break;
172181d8750bSCyrille Pitchen
172281d8750bSCyrille Pitchen default:
172381d8750bSCyrille Pitchen return atmel_sha_complete(dd, -EINVAL);
172481d8750bSCyrille Pitchen }
172581d8750bSCyrille Pitchen bs = ctx->block_size;
172681d8750bSCyrille Pitchen
172781d8750bSCyrille Pitchen if (likely(!atmel_sha_hmac_key_get(&hmac->hkey, &key, &keylen)))
172881d8750bSCyrille Pitchen return resume(dd);
172981d8750bSCyrille Pitchen
173081d8750bSCyrille Pitchen /* Compute K' from K. */
173181d8750bSCyrille Pitchen if (unlikely(keylen > bs))
173281d8750bSCyrille Pitchen return atmel_sha_hmac_prehash_key(dd, key, keylen);
173381d8750bSCyrille Pitchen
173481d8750bSCyrille Pitchen /* Prepare ipad. */
173581d8750bSCyrille Pitchen memcpy((u8 *)hmac->ipad, key, keylen);
173681d8750bSCyrille Pitchen memset((u8 *)hmac->ipad + keylen, 0, bs - keylen);
173781d8750bSCyrille Pitchen return atmel_sha_hmac_compute_ipad_hash(dd);
173881d8750bSCyrille Pitchen }
173981d8750bSCyrille Pitchen
atmel_sha_hmac_prehash_key(struct atmel_sha_dev * dd,const u8 * key,unsigned int keylen)174081d8750bSCyrille Pitchen static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev *dd,
174181d8750bSCyrille Pitchen const u8 *key, unsigned int keylen)
174281d8750bSCyrille Pitchen {
174381d8750bSCyrille Pitchen return atmel_sha_cpu_hash(dd, key, keylen, true,
174481d8750bSCyrille Pitchen atmel_sha_hmac_prehash_key_done);
174581d8750bSCyrille Pitchen }
174681d8750bSCyrille Pitchen
atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev * dd)174781d8750bSCyrille Pitchen static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev *dd)
174881d8750bSCyrille Pitchen {
174981d8750bSCyrille Pitchen struct ahash_request *req = dd->req;
175081d8750bSCyrille Pitchen struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
175181d8750bSCyrille Pitchen struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
175281d8750bSCyrille Pitchen struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
175381d8750bSCyrille Pitchen size_t ds = crypto_ahash_digestsize(tfm);
175481d8750bSCyrille Pitchen size_t bs = ctx->block_size;
175581d8750bSCyrille Pitchen size_t i, num_words = ds / sizeof(u32);
175681d8750bSCyrille Pitchen
175781d8750bSCyrille Pitchen /* Prepare ipad. */
175881d8750bSCyrille Pitchen for (i = 0; i < num_words; ++i)
175981d8750bSCyrille Pitchen hmac->ipad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
176081d8750bSCyrille Pitchen memset((u8 *)hmac->ipad + ds, 0, bs - ds);
176181d8750bSCyrille Pitchen return atmel_sha_hmac_compute_ipad_hash(dd);
176281d8750bSCyrille Pitchen }
176381d8750bSCyrille Pitchen
atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev * dd)176481d8750bSCyrille Pitchen static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev *dd)
176581d8750bSCyrille Pitchen {
176681d8750bSCyrille Pitchen struct ahash_request *req = dd->req;
176781d8750bSCyrille Pitchen struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
176881d8750bSCyrille Pitchen struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
176981d8750bSCyrille Pitchen struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
177081d8750bSCyrille Pitchen size_t bs = ctx->block_size;
177181d8750bSCyrille Pitchen size_t i, num_words = bs / sizeof(u32);
177281d8750bSCyrille Pitchen
1773f9fc1ec2SArnd Bergmann unsafe_memcpy(hmac->opad, hmac->ipad, bs,
1774f9fc1ec2SArnd Bergmann "fortified memcpy causes -Wrestrict warning");
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
atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev * dd)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
atmel_sha_hmac_setup_done(struct atmel_sha_dev * dd)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
atmel_sha_hmac_start(struct atmel_sha_dev * dd)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
atmel_sha_hmac_setkey(struct crypto_ahash * tfm,const u8 * key,unsigned int keylen)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
atmel_sha_hmac_init(struct ahash_request * req)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
atmel_sha_hmac_init_done(struct atmel_sha_dev * dd)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
atmel_sha_hmac_final(struct atmel_sha_dev * dd)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
atmel_sha_hmac_final_done(struct atmel_sha_dev * dd)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
atmel_sha_hmac_digest(struct ahash_request * req)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
atmel_sha_hmac_digest2(struct atmel_sha_dev * dd)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);
1952232c1e8eSRyan Wanner struct scatterlist *sgbuf;
195381d8750bSCyrille Pitchen size_t hs = ctx->hash_size;
195481d8750bSCyrille Pitchen size_t i, num_words = hs / sizeof(u32);
195581d8750bSCyrille Pitchen bool use_dma = false;
195681d8750bSCyrille Pitchen u32 mr;
195781d8750bSCyrille Pitchen
195881d8750bSCyrille Pitchen /* Special case for empty message. */
1959232c1e8eSRyan Wanner if (!req->nbytes) {
1960232c1e8eSRyan Wanner req->nbytes = 0;
1961232c1e8eSRyan Wanner ctx->bufcnt = 0;
1962232c1e8eSRyan Wanner ctx->digcnt[0] = 0;
1963232c1e8eSRyan Wanner ctx->digcnt[1] = 0;
1964232c1e8eSRyan Wanner switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
1965232c1e8eSRyan Wanner case SHA_FLAGS_SHA1:
1966232c1e8eSRyan Wanner case SHA_FLAGS_SHA224:
1967232c1e8eSRyan Wanner case SHA_FLAGS_SHA256:
1968232c1e8eSRyan Wanner atmel_sha_fill_padding(ctx, 64);
1969232c1e8eSRyan Wanner break;
1970232c1e8eSRyan Wanner
1971232c1e8eSRyan Wanner case SHA_FLAGS_SHA384:
1972232c1e8eSRyan Wanner case SHA_FLAGS_SHA512:
1973232c1e8eSRyan Wanner atmel_sha_fill_padding(ctx, 128);
1974232c1e8eSRyan Wanner break;
1975232c1e8eSRyan Wanner }
1976232c1e8eSRyan Wanner sg_init_one(&dd->tmp, ctx->buffer, ctx->bufcnt);
1977232c1e8eSRyan Wanner }
197881d8750bSCyrille Pitchen
197981d8750bSCyrille Pitchen /* Check DMA threshold and alignment. */
198081d8750bSCyrille Pitchen if (req->nbytes > ATMEL_SHA_DMA_THRESHOLD &&
198181d8750bSCyrille Pitchen atmel_sha_dma_check_aligned(dd, req->src, req->nbytes))
198281d8750bSCyrille Pitchen use_dma = true;
198381d8750bSCyrille Pitchen
198481d8750bSCyrille Pitchen /* Write both initial hash values to compute a HMAC. */
198581d8750bSCyrille Pitchen atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
198681d8750bSCyrille Pitchen for (i = 0; i < num_words; ++i)
198781d8750bSCyrille Pitchen atmel_sha_write(dd, SHA_REG_DIN(i), hmac->ipad[i]);
198881d8750bSCyrille Pitchen
198981d8750bSCyrille Pitchen atmel_sha_write(dd, SHA_CR, SHA_CR_WUIEHV);
199081d8750bSCyrille Pitchen for (i = 0; i < num_words; ++i)
199181d8750bSCyrille Pitchen atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
199281d8750bSCyrille Pitchen
199381d8750bSCyrille Pitchen /* Write the Mode, Message Size, Bytes Count then Control Registers. */
199481d8750bSCyrille Pitchen mr = (SHA_MR_HMAC | SHA_MR_DUALBUFF);
199581d8750bSCyrille Pitchen mr |= ctx->flags & SHA_FLAGS_ALGO_MASK;
199681d8750bSCyrille Pitchen if (use_dma)
199781d8750bSCyrille Pitchen mr |= SHA_MR_MODE_IDATAR0;
199881d8750bSCyrille Pitchen else
199981d8750bSCyrille Pitchen mr |= SHA_MR_MODE_AUTO;
200081d8750bSCyrille Pitchen atmel_sha_write(dd, SHA_MR, mr);
200181d8750bSCyrille Pitchen
200281d8750bSCyrille Pitchen atmel_sha_write(dd, SHA_MSR, req->nbytes);
200381d8750bSCyrille Pitchen atmel_sha_write(dd, SHA_BCR, req->nbytes);
200481d8750bSCyrille Pitchen
200581d8750bSCyrille Pitchen atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
200681d8750bSCyrille Pitchen
2007232c1e8eSRyan Wanner /* Special case for empty message. */
2008232c1e8eSRyan Wanner if (!req->nbytes) {
2009232c1e8eSRyan Wanner sgbuf = &dd->tmp;
2010232c1e8eSRyan Wanner req->nbytes = ctx->bufcnt;
2011232c1e8eSRyan Wanner } else {
2012232c1e8eSRyan Wanner sgbuf = req->src;
2013232c1e8eSRyan Wanner }
2014232c1e8eSRyan Wanner
201581d8750bSCyrille Pitchen /* Process data. */
201681d8750bSCyrille Pitchen if (use_dma)
2017232c1e8eSRyan Wanner return atmel_sha_dma_start(dd, sgbuf, req->nbytes,
201881d8750bSCyrille Pitchen atmel_sha_hmac_final_done);
201981d8750bSCyrille Pitchen
2020232c1e8eSRyan Wanner return atmel_sha_cpu_start(dd, sgbuf, req->nbytes, false, true,
202181d8750bSCyrille Pitchen atmel_sha_hmac_final_done);
202281d8750bSCyrille Pitchen }
202381d8750bSCyrille Pitchen
atmel_sha_hmac_cra_init(struct crypto_tfm * tfm)202481d8750bSCyrille Pitchen static int atmel_sha_hmac_cra_init(struct crypto_tfm *tfm)
202581d8750bSCyrille Pitchen {
202681d8750bSCyrille Pitchen struct atmel_sha_hmac_ctx *hmac = crypto_tfm_ctx(tfm);
202781d8750bSCyrille Pitchen
202881d8750bSCyrille Pitchen crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
202981d8750bSCyrille Pitchen sizeof(struct atmel_sha_reqctx));
203081d8750bSCyrille Pitchen hmac->base.start = atmel_sha_hmac_start;
203181d8750bSCyrille Pitchen atmel_sha_hmac_key_init(&hmac->hkey);
203281d8750bSCyrille Pitchen
203381d8750bSCyrille Pitchen return 0;
203481d8750bSCyrille Pitchen }
203581d8750bSCyrille Pitchen
atmel_sha_hmac_cra_exit(struct crypto_tfm * tfm)203681d8750bSCyrille Pitchen static void atmel_sha_hmac_cra_exit(struct crypto_tfm *tfm)
203781d8750bSCyrille Pitchen {
203881d8750bSCyrille Pitchen struct atmel_sha_hmac_ctx *hmac = crypto_tfm_ctx(tfm);
203981d8750bSCyrille Pitchen
204081d8750bSCyrille Pitchen atmel_sha_hmac_key_release(&hmac->hkey);
204181d8750bSCyrille Pitchen }
204281d8750bSCyrille Pitchen
atmel_sha_hmac_alg_init(struct ahash_alg * alg)2043aebe5bd7STudor Ambarus static void atmel_sha_hmac_alg_init(struct ahash_alg *alg)
2044aebe5bd7STudor Ambarus {
2045aebe5bd7STudor Ambarus alg->halg.base.cra_priority = ATMEL_SHA_PRIORITY;
2046aebe5bd7STudor Ambarus alg->halg.base.cra_flags = CRYPTO_ALG_ASYNC;
2047aebe5bd7STudor Ambarus alg->halg.base.cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx);
2048aebe5bd7STudor Ambarus alg->halg.base.cra_module = THIS_MODULE;
2049aebe5bd7STudor Ambarus alg->halg.base.cra_init = atmel_sha_hmac_cra_init;
2050aebe5bd7STudor Ambarus alg->halg.base.cra_exit = atmel_sha_hmac_cra_exit;
2051aebe5bd7STudor Ambarus
2052aebe5bd7STudor Ambarus alg->halg.statesize = sizeof(struct atmel_sha_reqctx);
2053aebe5bd7STudor Ambarus
2054aebe5bd7STudor Ambarus alg->init = atmel_sha_hmac_init;
2055aebe5bd7STudor Ambarus alg->update = atmel_sha_update;
2056aebe5bd7STudor Ambarus alg->final = atmel_sha_final;
2057aebe5bd7STudor Ambarus alg->digest = atmel_sha_hmac_digest;
2058aebe5bd7STudor Ambarus alg->setkey = atmel_sha_hmac_setkey;
2059aebe5bd7STudor Ambarus alg->export = atmel_sha_export;
2060aebe5bd7STudor Ambarus alg->import = atmel_sha_import;
2061aebe5bd7STudor Ambarus }
2062aebe5bd7STudor Ambarus
206381d8750bSCyrille Pitchen static struct ahash_alg sha_hmac_algs[] = {
206481d8750bSCyrille Pitchen {
2065aebe5bd7STudor Ambarus .halg.base.cra_name = "hmac(sha1)",
2066aebe5bd7STudor Ambarus .halg.base.cra_driver_name = "atmel-hmac-sha1",
2067aebe5bd7STudor Ambarus .halg.base.cra_blocksize = SHA1_BLOCK_SIZE,
2068aebe5bd7STudor Ambarus
2069aebe5bd7STudor Ambarus .halg.digestsize = SHA1_DIGEST_SIZE,
207081d8750bSCyrille Pitchen },
207181d8750bSCyrille Pitchen {
2072aebe5bd7STudor Ambarus .halg.base.cra_name = "hmac(sha224)",
2073aebe5bd7STudor Ambarus .halg.base.cra_driver_name = "atmel-hmac-sha224",
2074aebe5bd7STudor Ambarus .halg.base.cra_blocksize = SHA224_BLOCK_SIZE,
2075aebe5bd7STudor Ambarus
2076aebe5bd7STudor Ambarus .halg.digestsize = SHA224_DIGEST_SIZE,
207781d8750bSCyrille Pitchen },
207881d8750bSCyrille Pitchen {
2079aebe5bd7STudor Ambarus .halg.base.cra_name = "hmac(sha256)",
2080aebe5bd7STudor Ambarus .halg.base.cra_driver_name = "atmel-hmac-sha256",
2081aebe5bd7STudor Ambarus .halg.base.cra_blocksize = SHA256_BLOCK_SIZE,
2082aebe5bd7STudor Ambarus
2083aebe5bd7STudor Ambarus .halg.digestsize = SHA256_DIGEST_SIZE,
208481d8750bSCyrille Pitchen },
208581d8750bSCyrille Pitchen {
2086aebe5bd7STudor Ambarus .halg.base.cra_name = "hmac(sha384)",
2087aebe5bd7STudor Ambarus .halg.base.cra_driver_name = "atmel-hmac-sha384",
2088aebe5bd7STudor Ambarus .halg.base.cra_blocksize = SHA384_BLOCK_SIZE,
2089aebe5bd7STudor Ambarus
2090aebe5bd7STudor Ambarus .halg.digestsize = SHA384_DIGEST_SIZE,
209181d8750bSCyrille Pitchen },
209281d8750bSCyrille Pitchen {
2093aebe5bd7STudor Ambarus .halg.base.cra_name = "hmac(sha512)",
2094aebe5bd7STudor Ambarus .halg.base.cra_driver_name = "atmel-hmac-sha512",
2095aebe5bd7STudor Ambarus .halg.base.cra_blocksize = SHA512_BLOCK_SIZE,
2096aebe5bd7STudor Ambarus
2097aebe5bd7STudor Ambarus .halg.digestsize = SHA512_DIGEST_SIZE,
209881d8750bSCyrille Pitchen },
209981d8750bSCyrille Pitchen };
2100eec12f66SCyrille Pitchen
21011520c725SHerbert Xu #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
210289a82ef8SCyrille Pitchen /* authenc functions */
210389a82ef8SCyrille Pitchen
210489a82ef8SCyrille Pitchen static int atmel_sha_authenc_init2(struct atmel_sha_dev *dd);
210589a82ef8SCyrille Pitchen static int atmel_sha_authenc_init_done(struct atmel_sha_dev *dd);
210689a82ef8SCyrille Pitchen static int atmel_sha_authenc_final_done(struct atmel_sha_dev *dd);
210789a82ef8SCyrille Pitchen
210889a82ef8SCyrille Pitchen
210989a82ef8SCyrille Pitchen struct atmel_sha_authenc_ctx {
211089a82ef8SCyrille Pitchen struct crypto_ahash *tfm;
211189a82ef8SCyrille Pitchen };
211289a82ef8SCyrille Pitchen
211389a82ef8SCyrille Pitchen struct atmel_sha_authenc_reqctx {
211489a82ef8SCyrille Pitchen struct atmel_sha_reqctx base;
211589a82ef8SCyrille Pitchen
211689a82ef8SCyrille Pitchen atmel_aes_authenc_fn_t cb;
211789a82ef8SCyrille Pitchen struct atmel_aes_dev *aes_dev;
211889a82ef8SCyrille Pitchen
211989a82ef8SCyrille Pitchen /* _init() parameters. */
212089a82ef8SCyrille Pitchen struct scatterlist *assoc;
212189a82ef8SCyrille Pitchen u32 assoclen;
212289a82ef8SCyrille Pitchen u32 textlen;
212389a82ef8SCyrille Pitchen
212489a82ef8SCyrille Pitchen /* _final() parameters. */
212589a82ef8SCyrille Pitchen u32 *digest;
212689a82ef8SCyrille Pitchen unsigned int digestlen;
212789a82ef8SCyrille Pitchen };
212889a82ef8SCyrille Pitchen
atmel_sha_authenc_complete(void * data,int err)2129255e48ebSHerbert Xu static void atmel_sha_authenc_complete(void *data, int err)
213089a82ef8SCyrille Pitchen {
2131255e48ebSHerbert Xu struct ahash_request *req = data;
213289a82ef8SCyrille Pitchen struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
213389a82ef8SCyrille Pitchen
213489a82ef8SCyrille Pitchen authctx->cb(authctx->aes_dev, err, authctx->base.dd->is_async);
213589a82ef8SCyrille Pitchen }
213689a82ef8SCyrille Pitchen
atmel_sha_authenc_start(struct atmel_sha_dev * dd)213789a82ef8SCyrille Pitchen static int atmel_sha_authenc_start(struct atmel_sha_dev *dd)
213889a82ef8SCyrille Pitchen {
213989a82ef8SCyrille Pitchen struct ahash_request *req = dd->req;
214089a82ef8SCyrille Pitchen struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
214189a82ef8SCyrille Pitchen int err;
214289a82ef8SCyrille Pitchen
214389a82ef8SCyrille Pitchen /*
214489a82ef8SCyrille Pitchen * Force atmel_sha_complete() to call req->base.complete(), ie
214589a82ef8SCyrille Pitchen * atmel_sha_authenc_complete(), which in turn calls authctx->cb().
214689a82ef8SCyrille Pitchen */
214789a82ef8SCyrille Pitchen dd->force_complete = true;
214889a82ef8SCyrille Pitchen
214989a82ef8SCyrille Pitchen err = atmel_sha_hw_init(dd);
215089a82ef8SCyrille Pitchen return authctx->cb(authctx->aes_dev, err, dd->is_async);
215189a82ef8SCyrille Pitchen }
215289a82ef8SCyrille Pitchen
atmel_sha_authenc_is_ready(void)215389a82ef8SCyrille Pitchen bool atmel_sha_authenc_is_ready(void)
215489a82ef8SCyrille Pitchen {
215589a82ef8SCyrille Pitchen struct atmel_sha_ctx dummy;
215689a82ef8SCyrille Pitchen
215789a82ef8SCyrille Pitchen dummy.dd = NULL;
215889a82ef8SCyrille Pitchen return (atmel_sha_find_dev(&dummy) != NULL);
215989a82ef8SCyrille Pitchen }
216089a82ef8SCyrille Pitchen EXPORT_SYMBOL_GPL(atmel_sha_authenc_is_ready);
216189a82ef8SCyrille Pitchen
atmel_sha_authenc_get_reqsize(void)216289a82ef8SCyrille Pitchen unsigned int atmel_sha_authenc_get_reqsize(void)
216389a82ef8SCyrille Pitchen {
216489a82ef8SCyrille Pitchen return sizeof(struct atmel_sha_authenc_reqctx);
216589a82ef8SCyrille Pitchen }
216689a82ef8SCyrille Pitchen EXPORT_SYMBOL_GPL(atmel_sha_authenc_get_reqsize);
216789a82ef8SCyrille Pitchen
atmel_sha_authenc_spawn(unsigned long mode)216889a82ef8SCyrille Pitchen struct atmel_sha_authenc_ctx *atmel_sha_authenc_spawn(unsigned long mode)
216989a82ef8SCyrille Pitchen {
217089a82ef8SCyrille Pitchen struct atmel_sha_authenc_ctx *auth;
217189a82ef8SCyrille Pitchen struct crypto_ahash *tfm;
217289a82ef8SCyrille Pitchen struct atmel_sha_ctx *tctx;
217389a82ef8SCyrille Pitchen const char *name;
217489a82ef8SCyrille Pitchen int err = -EINVAL;
217589a82ef8SCyrille Pitchen
217689a82ef8SCyrille Pitchen switch (mode & SHA_FLAGS_MODE_MASK) {
217789a82ef8SCyrille Pitchen case SHA_FLAGS_HMAC_SHA1:
217889a82ef8SCyrille Pitchen name = "atmel-hmac-sha1";
217989a82ef8SCyrille Pitchen break;
218089a82ef8SCyrille Pitchen
218189a82ef8SCyrille Pitchen case SHA_FLAGS_HMAC_SHA224:
218289a82ef8SCyrille Pitchen name = "atmel-hmac-sha224";
218389a82ef8SCyrille Pitchen break;
218489a82ef8SCyrille Pitchen
218589a82ef8SCyrille Pitchen case SHA_FLAGS_HMAC_SHA256:
218689a82ef8SCyrille Pitchen name = "atmel-hmac-sha256";
218789a82ef8SCyrille Pitchen break;
218889a82ef8SCyrille Pitchen
218989a82ef8SCyrille Pitchen case SHA_FLAGS_HMAC_SHA384:
219089a82ef8SCyrille Pitchen name = "atmel-hmac-sha384";
219189a82ef8SCyrille Pitchen break;
219289a82ef8SCyrille Pitchen
219389a82ef8SCyrille Pitchen case SHA_FLAGS_HMAC_SHA512:
219489a82ef8SCyrille Pitchen name = "atmel-hmac-sha512";
219589a82ef8SCyrille Pitchen break;
219689a82ef8SCyrille Pitchen
219789a82ef8SCyrille Pitchen default:
219889a82ef8SCyrille Pitchen goto error;
219989a82ef8SCyrille Pitchen }
220089a82ef8SCyrille Pitchen
220185d7311fSEric Biggers tfm = crypto_alloc_ahash(name, 0, 0);
220289a82ef8SCyrille Pitchen if (IS_ERR(tfm)) {
220389a82ef8SCyrille Pitchen err = PTR_ERR(tfm);
220489a82ef8SCyrille Pitchen goto error;
220589a82ef8SCyrille Pitchen }
220689a82ef8SCyrille Pitchen tctx = crypto_ahash_ctx(tfm);
220789a82ef8SCyrille Pitchen tctx->start = atmel_sha_authenc_start;
220889a82ef8SCyrille Pitchen tctx->flags = mode;
220989a82ef8SCyrille Pitchen
221089a82ef8SCyrille Pitchen auth = kzalloc(sizeof(*auth), GFP_KERNEL);
221189a82ef8SCyrille Pitchen if (!auth) {
221289a82ef8SCyrille Pitchen err = -ENOMEM;
221389a82ef8SCyrille Pitchen goto err_free_ahash;
221489a82ef8SCyrille Pitchen }
221589a82ef8SCyrille Pitchen auth->tfm = tfm;
221689a82ef8SCyrille Pitchen
221789a82ef8SCyrille Pitchen return auth;
221889a82ef8SCyrille Pitchen
221989a82ef8SCyrille Pitchen err_free_ahash:
222089a82ef8SCyrille Pitchen crypto_free_ahash(tfm);
222189a82ef8SCyrille Pitchen error:
222289a82ef8SCyrille Pitchen return ERR_PTR(err);
222389a82ef8SCyrille Pitchen }
222489a82ef8SCyrille Pitchen EXPORT_SYMBOL_GPL(atmel_sha_authenc_spawn);
222589a82ef8SCyrille Pitchen
atmel_sha_authenc_free(struct atmel_sha_authenc_ctx * auth)222689a82ef8SCyrille Pitchen void atmel_sha_authenc_free(struct atmel_sha_authenc_ctx *auth)
222789a82ef8SCyrille Pitchen {
222889a82ef8SCyrille Pitchen if (auth)
222989a82ef8SCyrille Pitchen crypto_free_ahash(auth->tfm);
223089a82ef8SCyrille Pitchen kfree(auth);
223189a82ef8SCyrille Pitchen }
223289a82ef8SCyrille Pitchen EXPORT_SYMBOL_GPL(atmel_sha_authenc_free);
223389a82ef8SCyrille Pitchen
atmel_sha_authenc_setkey(struct atmel_sha_authenc_ctx * auth,const u8 * key,unsigned int keylen,u32 flags)223489a82ef8SCyrille Pitchen int atmel_sha_authenc_setkey(struct atmel_sha_authenc_ctx *auth,
2235af5034e8SEric Biggers const u8 *key, unsigned int keylen, u32 flags)
223689a82ef8SCyrille Pitchen {
223789a82ef8SCyrille Pitchen struct crypto_ahash *tfm = auth->tfm;
223889a82ef8SCyrille Pitchen
223989a82ef8SCyrille Pitchen crypto_ahash_clear_flags(tfm, CRYPTO_TFM_REQ_MASK);
2240af5034e8SEric Biggers crypto_ahash_set_flags(tfm, flags & CRYPTO_TFM_REQ_MASK);
2241af5034e8SEric Biggers return crypto_ahash_setkey(tfm, key, keylen);
224289a82ef8SCyrille Pitchen }
224389a82ef8SCyrille Pitchen EXPORT_SYMBOL_GPL(atmel_sha_authenc_setkey);
224489a82ef8SCyrille Pitchen
atmel_sha_authenc_schedule(struct ahash_request * req,struct atmel_sha_authenc_ctx * auth,atmel_aes_authenc_fn_t cb,struct atmel_aes_dev * aes_dev)224589a82ef8SCyrille Pitchen int atmel_sha_authenc_schedule(struct ahash_request *req,
224689a82ef8SCyrille Pitchen struct atmel_sha_authenc_ctx *auth,
224789a82ef8SCyrille Pitchen atmel_aes_authenc_fn_t cb,
224889a82ef8SCyrille Pitchen struct atmel_aes_dev *aes_dev)
224989a82ef8SCyrille Pitchen {
225089a82ef8SCyrille Pitchen struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
225189a82ef8SCyrille Pitchen struct atmel_sha_reqctx *ctx = &authctx->base;
225289a82ef8SCyrille Pitchen struct crypto_ahash *tfm = auth->tfm;
225389a82ef8SCyrille Pitchen struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm);
225489a82ef8SCyrille Pitchen struct atmel_sha_dev *dd;
225589a82ef8SCyrille Pitchen
225689a82ef8SCyrille Pitchen /* Reset request context (MUST be done first). */
225789a82ef8SCyrille Pitchen memset(authctx, 0, sizeof(*authctx));
225889a82ef8SCyrille Pitchen
225989a82ef8SCyrille Pitchen /* Get SHA device. */
226089a82ef8SCyrille Pitchen dd = atmel_sha_find_dev(tctx);
226189a82ef8SCyrille Pitchen if (!dd)
226289a82ef8SCyrille Pitchen return cb(aes_dev, -ENODEV, false);
226389a82ef8SCyrille Pitchen
226489a82ef8SCyrille Pitchen /* Init request context. */
226589a82ef8SCyrille Pitchen ctx->dd = dd;
226689a82ef8SCyrille Pitchen ctx->buflen = SHA_BUFFER_LEN;
226789a82ef8SCyrille Pitchen authctx->cb = cb;
226889a82ef8SCyrille Pitchen authctx->aes_dev = aes_dev;
226989a82ef8SCyrille Pitchen ahash_request_set_tfm(req, tfm);
227089a82ef8SCyrille Pitchen ahash_request_set_callback(req, 0, atmel_sha_authenc_complete, req);
227189a82ef8SCyrille Pitchen
227289a82ef8SCyrille Pitchen return atmel_sha_handle_queue(dd, req);
227389a82ef8SCyrille Pitchen }
227489a82ef8SCyrille Pitchen EXPORT_SYMBOL_GPL(atmel_sha_authenc_schedule);
227589a82ef8SCyrille Pitchen
atmel_sha_authenc_init(struct ahash_request * req,struct scatterlist * assoc,unsigned int assoclen,unsigned int textlen,atmel_aes_authenc_fn_t cb,struct atmel_aes_dev * aes_dev)227689a82ef8SCyrille Pitchen int atmel_sha_authenc_init(struct ahash_request *req,
227789a82ef8SCyrille Pitchen struct scatterlist *assoc, unsigned int assoclen,
227889a82ef8SCyrille Pitchen unsigned int textlen,
227989a82ef8SCyrille Pitchen atmel_aes_authenc_fn_t cb,
228089a82ef8SCyrille Pitchen struct atmel_aes_dev *aes_dev)
228189a82ef8SCyrille Pitchen {
228289a82ef8SCyrille Pitchen struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
228389a82ef8SCyrille Pitchen struct atmel_sha_reqctx *ctx = &authctx->base;
228489a82ef8SCyrille Pitchen struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
228589a82ef8SCyrille Pitchen struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
228689a82ef8SCyrille Pitchen struct atmel_sha_dev *dd = ctx->dd;
228789a82ef8SCyrille Pitchen
228889a82ef8SCyrille Pitchen if (unlikely(!IS_ALIGNED(assoclen, sizeof(u32))))
228989a82ef8SCyrille Pitchen return atmel_sha_complete(dd, -EINVAL);
229089a82ef8SCyrille Pitchen
229189a82ef8SCyrille Pitchen authctx->cb = cb;
229289a82ef8SCyrille Pitchen authctx->aes_dev = aes_dev;
229389a82ef8SCyrille Pitchen authctx->assoc = assoc;
229489a82ef8SCyrille Pitchen authctx->assoclen = assoclen;
229589a82ef8SCyrille Pitchen authctx->textlen = textlen;
229689a82ef8SCyrille Pitchen
229789a82ef8SCyrille Pitchen ctx->flags = hmac->base.flags;
229889a82ef8SCyrille Pitchen return atmel_sha_hmac_setup(dd, atmel_sha_authenc_init2);
229989a82ef8SCyrille Pitchen }
230089a82ef8SCyrille Pitchen EXPORT_SYMBOL_GPL(atmel_sha_authenc_init);
230189a82ef8SCyrille Pitchen
atmel_sha_authenc_init2(struct atmel_sha_dev * dd)230289a82ef8SCyrille Pitchen static int atmel_sha_authenc_init2(struct atmel_sha_dev *dd)
230389a82ef8SCyrille Pitchen {
230489a82ef8SCyrille Pitchen struct ahash_request *req = dd->req;
230589a82ef8SCyrille Pitchen struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
230689a82ef8SCyrille Pitchen struct atmel_sha_reqctx *ctx = &authctx->base;
230789a82ef8SCyrille Pitchen struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
230889a82ef8SCyrille Pitchen struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
230989a82ef8SCyrille Pitchen size_t hs = ctx->hash_size;
231089a82ef8SCyrille Pitchen size_t i, num_words = hs / sizeof(u32);
231189a82ef8SCyrille Pitchen u32 mr, msg_size;
231289a82ef8SCyrille Pitchen
231389a82ef8SCyrille Pitchen atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
231489a82ef8SCyrille Pitchen for (i = 0; i < num_words; ++i)
231589a82ef8SCyrille Pitchen atmel_sha_write(dd, SHA_REG_DIN(i), hmac->ipad[i]);
231689a82ef8SCyrille Pitchen
231789a82ef8SCyrille Pitchen atmel_sha_write(dd, SHA_CR, SHA_CR_WUIEHV);
231889a82ef8SCyrille Pitchen for (i = 0; i < num_words; ++i)
231989a82ef8SCyrille Pitchen atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
232089a82ef8SCyrille Pitchen
232189a82ef8SCyrille Pitchen mr = (SHA_MR_MODE_IDATAR0 |
232289a82ef8SCyrille Pitchen SHA_MR_HMAC |
232389a82ef8SCyrille Pitchen SHA_MR_DUALBUFF);
232489a82ef8SCyrille Pitchen mr |= ctx->flags & SHA_FLAGS_ALGO_MASK;
232589a82ef8SCyrille Pitchen atmel_sha_write(dd, SHA_MR, mr);
232689a82ef8SCyrille Pitchen
232789a82ef8SCyrille Pitchen msg_size = authctx->assoclen + authctx->textlen;
232889a82ef8SCyrille Pitchen atmel_sha_write(dd, SHA_MSR, msg_size);
232989a82ef8SCyrille Pitchen atmel_sha_write(dd, SHA_BCR, msg_size);
233089a82ef8SCyrille Pitchen
233189a82ef8SCyrille Pitchen atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
233289a82ef8SCyrille Pitchen
233389a82ef8SCyrille Pitchen /* Process assoc data. */
233489a82ef8SCyrille Pitchen return atmel_sha_cpu_start(dd, authctx->assoc, authctx->assoclen,
233589a82ef8SCyrille Pitchen true, false,
233689a82ef8SCyrille Pitchen atmel_sha_authenc_init_done);
233789a82ef8SCyrille Pitchen }
233889a82ef8SCyrille Pitchen
atmel_sha_authenc_init_done(struct atmel_sha_dev * dd)233989a82ef8SCyrille Pitchen static int atmel_sha_authenc_init_done(struct atmel_sha_dev *dd)
234089a82ef8SCyrille Pitchen {
234189a82ef8SCyrille Pitchen struct ahash_request *req = dd->req;
234289a82ef8SCyrille Pitchen struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
234389a82ef8SCyrille Pitchen
234489a82ef8SCyrille Pitchen return authctx->cb(authctx->aes_dev, 0, dd->is_async);
234589a82ef8SCyrille Pitchen }
234689a82ef8SCyrille Pitchen
atmel_sha_authenc_final(struct ahash_request * req,u32 * digest,unsigned int digestlen,atmel_aes_authenc_fn_t cb,struct atmel_aes_dev * aes_dev)234789a82ef8SCyrille Pitchen int atmel_sha_authenc_final(struct ahash_request *req,
234889a82ef8SCyrille Pitchen u32 *digest, unsigned int digestlen,
234989a82ef8SCyrille Pitchen atmel_aes_authenc_fn_t cb,
235089a82ef8SCyrille Pitchen struct atmel_aes_dev *aes_dev)
235189a82ef8SCyrille Pitchen {
235289a82ef8SCyrille Pitchen struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
235389a82ef8SCyrille Pitchen struct atmel_sha_reqctx *ctx = &authctx->base;
235489a82ef8SCyrille Pitchen struct atmel_sha_dev *dd = ctx->dd;
235589a82ef8SCyrille Pitchen
235689a82ef8SCyrille Pitchen switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
235789a82ef8SCyrille Pitchen case SHA_FLAGS_SHA1:
235889a82ef8SCyrille Pitchen authctx->digestlen = SHA1_DIGEST_SIZE;
235989a82ef8SCyrille Pitchen break;
236089a82ef8SCyrille Pitchen
236189a82ef8SCyrille Pitchen case SHA_FLAGS_SHA224:
236289a82ef8SCyrille Pitchen authctx->digestlen = SHA224_DIGEST_SIZE;
236389a82ef8SCyrille Pitchen break;
236489a82ef8SCyrille Pitchen
236589a82ef8SCyrille Pitchen case SHA_FLAGS_SHA256:
236689a82ef8SCyrille Pitchen authctx->digestlen = SHA256_DIGEST_SIZE;
236789a82ef8SCyrille Pitchen break;
236889a82ef8SCyrille Pitchen
236989a82ef8SCyrille Pitchen case SHA_FLAGS_SHA384:
237089a82ef8SCyrille Pitchen authctx->digestlen = SHA384_DIGEST_SIZE;
237189a82ef8SCyrille Pitchen break;
237289a82ef8SCyrille Pitchen
237389a82ef8SCyrille Pitchen case SHA_FLAGS_SHA512:
237489a82ef8SCyrille Pitchen authctx->digestlen = SHA512_DIGEST_SIZE;
237589a82ef8SCyrille Pitchen break;
237689a82ef8SCyrille Pitchen
237789a82ef8SCyrille Pitchen default:
237889a82ef8SCyrille Pitchen return atmel_sha_complete(dd, -EINVAL);
237989a82ef8SCyrille Pitchen }
238089a82ef8SCyrille Pitchen if (authctx->digestlen > digestlen)
238189a82ef8SCyrille Pitchen authctx->digestlen = digestlen;
238289a82ef8SCyrille Pitchen
238389a82ef8SCyrille Pitchen authctx->cb = cb;
238489a82ef8SCyrille Pitchen authctx->aes_dev = aes_dev;
238589a82ef8SCyrille Pitchen authctx->digest = digest;
238689a82ef8SCyrille Pitchen return atmel_sha_wait_for_data_ready(dd,
238789a82ef8SCyrille Pitchen atmel_sha_authenc_final_done);
238889a82ef8SCyrille Pitchen }
238989a82ef8SCyrille Pitchen EXPORT_SYMBOL_GPL(atmel_sha_authenc_final);
239089a82ef8SCyrille Pitchen
atmel_sha_authenc_final_done(struct atmel_sha_dev * dd)239189a82ef8SCyrille Pitchen static int atmel_sha_authenc_final_done(struct atmel_sha_dev *dd)
239289a82ef8SCyrille Pitchen {
239389a82ef8SCyrille Pitchen struct ahash_request *req = dd->req;
239489a82ef8SCyrille Pitchen struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
239589a82ef8SCyrille Pitchen size_t i, num_words = authctx->digestlen / sizeof(u32);
239689a82ef8SCyrille Pitchen
239789a82ef8SCyrille Pitchen for (i = 0; i < num_words; ++i)
239889a82ef8SCyrille Pitchen authctx->digest[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
239989a82ef8SCyrille Pitchen
240089a82ef8SCyrille Pitchen return atmel_sha_complete(dd, 0);
240189a82ef8SCyrille Pitchen }
240289a82ef8SCyrille Pitchen
atmel_sha_authenc_abort(struct ahash_request * req)240389a82ef8SCyrille Pitchen void atmel_sha_authenc_abort(struct ahash_request *req)
240489a82ef8SCyrille Pitchen {
240589a82ef8SCyrille Pitchen struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
240689a82ef8SCyrille Pitchen struct atmel_sha_reqctx *ctx = &authctx->base;
240789a82ef8SCyrille Pitchen struct atmel_sha_dev *dd = ctx->dd;
240889a82ef8SCyrille Pitchen
240989a82ef8SCyrille Pitchen /* Prevent atmel_sha_complete() from calling req->base.complete(). */
241089a82ef8SCyrille Pitchen dd->is_async = false;
241189a82ef8SCyrille Pitchen dd->force_complete = false;
241289a82ef8SCyrille Pitchen (void)atmel_sha_complete(dd, 0);
241389a82ef8SCyrille Pitchen }
241489a82ef8SCyrille Pitchen EXPORT_SYMBOL_GPL(atmel_sha_authenc_abort);
241589a82ef8SCyrille Pitchen
241689a82ef8SCyrille Pitchen #endif /* CONFIG_CRYPTO_DEV_ATMEL_AUTHENC */
241789a82ef8SCyrille Pitchen
241889a82ef8SCyrille Pitchen
atmel_sha_unregister_algs(struct atmel_sha_dev * dd)2419ebc82efaSNicolas Royer static void atmel_sha_unregister_algs(struct atmel_sha_dev *dd)
2420ebc82efaSNicolas Royer {
2421ebc82efaSNicolas Royer int i;
2422ebc82efaSNicolas Royer
242381d8750bSCyrille Pitchen if (dd->caps.has_hmac)
242481d8750bSCyrille Pitchen for (i = 0; i < ARRAY_SIZE(sha_hmac_algs); i++)
242581d8750bSCyrille Pitchen crypto_unregister_ahash(&sha_hmac_algs[i]);
242681d8750bSCyrille Pitchen
2427d4905b38SNicolas Royer for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++)
2428d4905b38SNicolas Royer crypto_unregister_ahash(&sha_1_256_algs[i]);
2429d4905b38SNicolas Royer
2430d4905b38SNicolas Royer if (dd->caps.has_sha224)
2431d4905b38SNicolas Royer crypto_unregister_ahash(&sha_224_alg);
2432d4905b38SNicolas Royer
2433d4905b38SNicolas Royer if (dd->caps.has_sha_384_512) {
2434d4905b38SNicolas Royer for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++)
2435d4905b38SNicolas Royer crypto_unregister_ahash(&sha_384_512_algs[i]);
2436d4905b38SNicolas Royer }
2437ebc82efaSNicolas Royer }
2438ebc82efaSNicolas Royer
atmel_sha_register_algs(struct atmel_sha_dev * dd)2439ebc82efaSNicolas Royer static int atmel_sha_register_algs(struct atmel_sha_dev *dd)
2440ebc82efaSNicolas Royer {
2441ebc82efaSNicolas Royer int err, i, j;
2442ebc82efaSNicolas Royer
2443d4905b38SNicolas Royer for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++) {
2444aebe5bd7STudor Ambarus atmel_sha_alg_init(&sha_1_256_algs[i]);
2445aebe5bd7STudor Ambarus
2446d4905b38SNicolas Royer err = crypto_register_ahash(&sha_1_256_algs[i]);
2447ebc82efaSNicolas Royer if (err)
2448d4905b38SNicolas Royer goto err_sha_1_256_algs;
2449d4905b38SNicolas Royer }
2450d4905b38SNicolas Royer
2451d4905b38SNicolas Royer if (dd->caps.has_sha224) {
2452aebe5bd7STudor Ambarus atmel_sha_alg_init(&sha_224_alg);
2453aebe5bd7STudor Ambarus
2454d4905b38SNicolas Royer err = crypto_register_ahash(&sha_224_alg);
2455d4905b38SNicolas Royer if (err)
2456d4905b38SNicolas Royer goto err_sha_224_algs;
2457d4905b38SNicolas Royer }
2458d4905b38SNicolas Royer
2459d4905b38SNicolas Royer if (dd->caps.has_sha_384_512) {
2460d4905b38SNicolas Royer for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++) {
2461aebe5bd7STudor Ambarus atmel_sha_alg_init(&sha_384_512_algs[i]);
2462aebe5bd7STudor Ambarus
2463d4905b38SNicolas Royer err = crypto_register_ahash(&sha_384_512_algs[i]);
2464d4905b38SNicolas Royer if (err)
2465d4905b38SNicolas Royer goto err_sha_384_512_algs;
2466d4905b38SNicolas Royer }
2467ebc82efaSNicolas Royer }
2468ebc82efaSNicolas Royer
246981d8750bSCyrille Pitchen if (dd->caps.has_hmac) {
247081d8750bSCyrille Pitchen for (i = 0; i < ARRAY_SIZE(sha_hmac_algs); i++) {
2471aebe5bd7STudor Ambarus atmel_sha_hmac_alg_init(&sha_hmac_algs[i]);
2472aebe5bd7STudor Ambarus
247381d8750bSCyrille Pitchen err = crypto_register_ahash(&sha_hmac_algs[i]);
247481d8750bSCyrille Pitchen if (err)
247581d8750bSCyrille Pitchen goto err_sha_hmac_algs;
247681d8750bSCyrille Pitchen }
247781d8750bSCyrille Pitchen }
247881d8750bSCyrille Pitchen
2479ebc82efaSNicolas Royer return 0;
2480ebc82efaSNicolas Royer
248181d8750bSCyrille Pitchen /*i = ARRAY_SIZE(sha_hmac_algs);*/
248281d8750bSCyrille Pitchen err_sha_hmac_algs:
248381d8750bSCyrille Pitchen for (j = 0; j < i; j++)
248481d8750bSCyrille Pitchen crypto_unregister_ahash(&sha_hmac_algs[j]);
248581d8750bSCyrille Pitchen i = ARRAY_SIZE(sha_384_512_algs);
2486d4905b38SNicolas Royer err_sha_384_512_algs:
2487ebc82efaSNicolas Royer for (j = 0; j < i; j++)
2488d4905b38SNicolas Royer crypto_unregister_ahash(&sha_384_512_algs[j]);
2489d4905b38SNicolas Royer crypto_unregister_ahash(&sha_224_alg);
2490d4905b38SNicolas Royer err_sha_224_algs:
2491d4905b38SNicolas Royer i = ARRAY_SIZE(sha_1_256_algs);
2492d4905b38SNicolas Royer err_sha_1_256_algs:
2493d4905b38SNicolas Royer for (j = 0; j < i; j++)
2494d4905b38SNicolas Royer crypto_unregister_ahash(&sha_1_256_algs[j]);
2495ebc82efaSNicolas Royer
2496ebc82efaSNicolas Royer return err;
2497ebc82efaSNicolas Royer }
2498ebc82efaSNicolas Royer
atmel_sha_dma_init(struct atmel_sha_dev * dd)2499827a98dfSTudor Ambarus static int atmel_sha_dma_init(struct atmel_sha_dev *dd)
2500d4905b38SNicolas Royer {
2501db28512fSPeter Ujfalusi dd->dma_lch_in.chan = dma_request_chan(dd->dev, "tx");
2502db28512fSPeter Ujfalusi if (IS_ERR(dd->dma_lch_in.chan)) {
2503355bf650SWang Ming return dev_err_probe(dd->dev, PTR_ERR(dd->dma_lch_in.chan),
2504355bf650SWang Ming "DMA channel is not available\n");
2505abfe7ae4SNicolas Ferre }
2506d4905b38SNicolas Royer
2507d4905b38SNicolas Royer dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base +
2508d4905b38SNicolas Royer SHA_REG_DIN(0);
2509d4905b38SNicolas Royer dd->dma_lch_in.dma_conf.src_maxburst = 1;
2510d4905b38SNicolas Royer dd->dma_lch_in.dma_conf.src_addr_width =
2511d4905b38SNicolas Royer DMA_SLAVE_BUSWIDTH_4_BYTES;
2512d4905b38SNicolas Royer dd->dma_lch_in.dma_conf.dst_maxburst = 1;
2513d4905b38SNicolas Royer dd->dma_lch_in.dma_conf.dst_addr_width =
2514d4905b38SNicolas Royer DMA_SLAVE_BUSWIDTH_4_BYTES;
2515d4905b38SNicolas Royer dd->dma_lch_in.dma_conf.device_fc = false;
2516d4905b38SNicolas Royer
2517d4905b38SNicolas Royer return 0;
2518d4905b38SNicolas Royer }
2519d4905b38SNicolas Royer
atmel_sha_dma_cleanup(struct atmel_sha_dev * dd)2520d4905b38SNicolas Royer static void atmel_sha_dma_cleanup(struct atmel_sha_dev *dd)
2521d4905b38SNicolas Royer {
2522d4905b38SNicolas Royer dma_release_channel(dd->dma_lch_in.chan);
2523d4905b38SNicolas Royer }
2524d4905b38SNicolas Royer
atmel_sha_get_cap(struct atmel_sha_dev * dd)2525d4905b38SNicolas Royer static void atmel_sha_get_cap(struct atmel_sha_dev *dd)
2526d4905b38SNicolas Royer {
2527d4905b38SNicolas Royer
2528d4905b38SNicolas Royer dd->caps.has_dma = 0;
2529d4905b38SNicolas Royer dd->caps.has_dualbuff = 0;
2530d4905b38SNicolas Royer dd->caps.has_sha224 = 0;
2531d4905b38SNicolas Royer dd->caps.has_sha_384_512 = 0;
25327cee3508SCyrille Pitchen dd->caps.has_uihv = 0;
253381d8750bSCyrille Pitchen dd->caps.has_hmac = 0;
2534d4905b38SNicolas Royer
2535d4905b38SNicolas Royer /* keep only major version number */
2536d4905b38SNicolas Royer switch (dd->hw_version & 0xff0) {
253716d20a08SKavyasree Kotagiri case 0x700:
25384838c519SSergiu Moga case 0x600:
2539507c5cc2SCyrille Pitchen case 0x510:
2540507c5cc2SCyrille Pitchen dd->caps.has_dma = 1;
2541507c5cc2SCyrille Pitchen dd->caps.has_dualbuff = 1;
2542507c5cc2SCyrille Pitchen dd->caps.has_sha224 = 1;
2543507c5cc2SCyrille Pitchen dd->caps.has_sha_384_512 = 1;
25447cee3508SCyrille Pitchen dd->caps.has_uihv = 1;
254581d8750bSCyrille Pitchen dd->caps.has_hmac = 1;
2546507c5cc2SCyrille Pitchen break;
2547141824d0SLeilei Zhao case 0x420:
2548141824d0SLeilei Zhao dd->caps.has_dma = 1;
2549141824d0SLeilei Zhao dd->caps.has_dualbuff = 1;
2550141824d0SLeilei Zhao dd->caps.has_sha224 = 1;
2551141824d0SLeilei Zhao dd->caps.has_sha_384_512 = 1;
25527cee3508SCyrille Pitchen dd->caps.has_uihv = 1;
2553141824d0SLeilei Zhao break;
2554d4905b38SNicolas Royer case 0x410:
2555d4905b38SNicolas Royer dd->caps.has_dma = 1;
2556d4905b38SNicolas Royer dd->caps.has_dualbuff = 1;
2557d4905b38SNicolas Royer dd->caps.has_sha224 = 1;
2558d4905b38SNicolas Royer dd->caps.has_sha_384_512 = 1;
2559d4905b38SNicolas Royer break;
2560d4905b38SNicolas Royer case 0x400:
2561d4905b38SNicolas Royer dd->caps.has_dma = 1;
2562d4905b38SNicolas Royer dd->caps.has_dualbuff = 1;
2563d4905b38SNicolas Royer dd->caps.has_sha224 = 1;
2564d4905b38SNicolas Royer break;
2565d4905b38SNicolas Royer case 0x320:
2566d4905b38SNicolas Royer break;
2567d4905b38SNicolas Royer default:
2568d4905b38SNicolas Royer dev_warn(dd->dev,
2569d4905b38SNicolas Royer "Unmanaged sha version, set minimum capabilities\n");
2570d4905b38SNicolas Royer break;
2571d4905b38SNicolas Royer }
2572d4905b38SNicolas Royer }
2573d4905b38SNicolas Royer
2574abfe7ae4SNicolas Ferre static const struct of_device_id atmel_sha_dt_ids[] = {
2575abfe7ae4SNicolas Ferre { .compatible = "atmel,at91sam9g46-sha" },
2576abfe7ae4SNicolas Ferre { /* sentinel */ }
2577abfe7ae4SNicolas Ferre };
2578abfe7ae4SNicolas Ferre
2579abfe7ae4SNicolas Ferre MODULE_DEVICE_TABLE(of, atmel_sha_dt_ids);
2580abfe7ae4SNicolas Ferre
atmel_sha_probe(struct platform_device * pdev)258149cfe4dbSGreg Kroah-Hartman static int atmel_sha_probe(struct platform_device *pdev)
2582ebc82efaSNicolas Royer {
2583ebc82efaSNicolas Royer struct atmel_sha_dev *sha_dd;
2584ebc82efaSNicolas Royer struct device *dev = &pdev->dev;
2585ebc82efaSNicolas Royer struct resource *sha_res;
2586ebc82efaSNicolas Royer int err;
2587ebc82efaSNicolas Royer
2588b0e8b341SLABBE Corentin sha_dd = devm_kzalloc(&pdev->dev, sizeof(*sha_dd), GFP_KERNEL);
2589c9063a02STudor Ambarus if (!sha_dd)
2590c9063a02STudor Ambarus return -ENOMEM;
2591ebc82efaSNicolas Royer
2592ebc82efaSNicolas Royer sha_dd->dev = dev;
2593ebc82efaSNicolas Royer
2594ebc82efaSNicolas Royer platform_set_drvdata(pdev, sha_dd);
2595ebc82efaSNicolas Royer
2596ebc82efaSNicolas Royer INIT_LIST_HEAD(&sha_dd->list);
259762728e82SLeilei Zhao spin_lock_init(&sha_dd->lock);
2598ebc82efaSNicolas Royer
2599ebc82efaSNicolas Royer tasklet_init(&sha_dd->done_task, atmel_sha_done_task,
2600ebc82efaSNicolas Royer (unsigned long)sha_dd);
2601f56809c3SCyrille Pitchen tasklet_init(&sha_dd->queue_task, atmel_sha_queue_task,
2602f56809c3SCyrille Pitchen (unsigned long)sha_dd);
2603ebc82efaSNicolas Royer
2604ebc82efaSNicolas Royer crypto_init_queue(&sha_dd->queue, ATMEL_SHA_QUEUE_LENGTH);
2605ebc82efaSNicolas Royer
2606f069fa9dSYangtao Li sha_dd->io_base = devm_platform_get_and_ioremap_resource(pdev, 0, &sha_res);
2607f069fa9dSYangtao Li if (IS_ERR(sha_dd->io_base)) {
2608f069fa9dSYangtao Li err = PTR_ERR(sha_dd->io_base);
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
2635c033042aSCyrille Pitchen err = clk_prepare(sha_dd->iclk);
2636c033042aSCyrille Pitchen if (err)
2637e7836518STudor Ambarus goto err_tasklet_kill;
2638c033042aSCyrille Pitchen
26390efe58f3STudor Ambarus err = atmel_sha_hw_version_init(sha_dd);
26400efe58f3STudor Ambarus if (err)
2641e7836518STudor Ambarus goto err_iclk_unprepare;
2642d4905b38SNicolas Royer
2643d4905b38SNicolas Royer atmel_sha_get_cap(sha_dd);
2644d4905b38SNicolas Royer
2645d4905b38SNicolas Royer if (sha_dd->caps.has_dma) {
2646827a98dfSTudor Ambarus err = atmel_sha_dma_init(sha_dd);
2647d4905b38SNicolas Royer if (err)
2648e7836518STudor Ambarus goto err_iclk_unprepare;
2649abfe7ae4SNicolas Ferre
2650abfe7ae4SNicolas Ferre dev_info(dev, "using %s for DMA transfers\n",
2651abfe7ae4SNicolas Ferre dma_chan_name(sha_dd->dma_lch_in.chan));
2652d4905b38SNicolas Royer }
2653d4905b38SNicolas Royer
2654ebc82efaSNicolas Royer spin_lock(&atmel_sha.lock);
2655ebc82efaSNicolas Royer list_add_tail(&sha_dd->list, &atmel_sha.dev_list);
2656ebc82efaSNicolas Royer spin_unlock(&atmel_sha.lock);
2657ebc82efaSNicolas Royer
2658ebc82efaSNicolas Royer err = atmel_sha_register_algs(sha_dd);
2659ebc82efaSNicolas Royer if (err)
2660ebc82efaSNicolas Royer goto err_algs;
2661ebc82efaSNicolas Royer
26621ca5b7d9SNicolas Ferre dev_info(dev, "Atmel SHA1/SHA256%s%s\n",
26631ca5b7d9SNicolas Ferre sha_dd->caps.has_sha224 ? "/SHA224" : "",
26641ca5b7d9SNicolas Ferre sha_dd->caps.has_sha_384_512 ? "/SHA384/SHA512" : "");
2665ebc82efaSNicolas Royer
2666ebc82efaSNicolas Royer return 0;
2667ebc82efaSNicolas Royer
2668ebc82efaSNicolas Royer err_algs:
2669ebc82efaSNicolas Royer spin_lock(&atmel_sha.lock);
2670ebc82efaSNicolas Royer list_del(&sha_dd->list);
2671ebc82efaSNicolas Royer spin_unlock(&atmel_sha.lock);
2672d4905b38SNicolas Royer if (sha_dd->caps.has_dma)
2673d4905b38SNicolas Royer atmel_sha_dma_cleanup(sha_dd);
2674e7836518STudor Ambarus err_iclk_unprepare:
2675c033042aSCyrille Pitchen clk_unprepare(sha_dd->iclk);
2676e7836518STudor Ambarus err_tasklet_kill:
2677f56809c3SCyrille Pitchen tasklet_kill(&sha_dd->queue_task);
2678ebc82efaSNicolas Royer tasklet_kill(&sha_dd->done_task);
2679ebc82efaSNicolas Royer
2680ebc82efaSNicolas Royer return err;
2681ebc82efaSNicolas Royer }
2682ebc82efaSNicolas Royer
atmel_sha_remove(struct platform_device * pdev)268349cfe4dbSGreg Kroah-Hartman static int atmel_sha_remove(struct platform_device *pdev)
2684ebc82efaSNicolas Royer {
2685c6a16f4bSClaudiu Beznea struct atmel_sha_dev *sha_dd = platform_get_drvdata(pdev);
268625edb4cdSUwe Kleine-König
2687ebc82efaSNicolas Royer spin_lock(&atmel_sha.lock);
2688ebc82efaSNicolas Royer list_del(&sha_dd->list);
2689ebc82efaSNicolas Royer spin_unlock(&atmel_sha.lock);
2690ebc82efaSNicolas Royer
2691ebc82efaSNicolas Royer atmel_sha_unregister_algs(sha_dd);
2692ebc82efaSNicolas Royer
2693f56809c3SCyrille Pitchen tasklet_kill(&sha_dd->queue_task);
2694ebc82efaSNicolas Royer tasklet_kill(&sha_dd->done_task);
2695ebc82efaSNicolas Royer
2696d4905b38SNicolas Royer if (sha_dd->caps.has_dma)
2697d4905b38SNicolas Royer atmel_sha_dma_cleanup(sha_dd);
2698d4905b38SNicolas Royer
2699c033042aSCyrille Pitchen clk_unprepare(sha_dd->iclk);
2700c033042aSCyrille Pitchen
2701ebc82efaSNicolas Royer return 0;
2702ebc82efaSNicolas Royer }
2703ebc82efaSNicolas Royer
2704ebc82efaSNicolas Royer static struct platform_driver atmel_sha_driver = {
2705ebc82efaSNicolas Royer .probe = atmel_sha_probe,
270649cfe4dbSGreg Kroah-Hartman .remove = atmel_sha_remove,
2707ebc82efaSNicolas Royer .driver = {
2708ebc82efaSNicolas Royer .name = "atmel_sha",
2709*b0cc7491SRob Herring .of_match_table = atmel_sha_dt_ids,
2710ebc82efaSNicolas Royer },
2711ebc82efaSNicolas Royer };
2712ebc82efaSNicolas Royer
2713ebc82efaSNicolas Royer module_platform_driver(atmel_sha_driver);
2714ebc82efaSNicolas Royer
2715d4905b38SNicolas Royer MODULE_DESCRIPTION("Atmel SHA (1/256/224/384/512) hw acceleration support.");
2716ebc82efaSNicolas Royer MODULE_LICENSE("GPL v2");
2717ebc82efaSNicolas Royer MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");
2718