xref: /linux/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-core.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
106f751b6SCorentin Labbe // SPDX-License-Identifier: GPL-2.0
206f751b6SCorentin Labbe /*
306f751b6SCorentin Labbe  * sun8i-ce-core.c - hardware cryptographic offloader for
406f751b6SCorentin Labbe  * Allwinner H3/A64/H5/H2+/H6/R40 SoC
506f751b6SCorentin Labbe  *
606f751b6SCorentin Labbe  * Copyright (C) 2015-2019 Corentin Labbe <clabbe.montjoie@gmail.com>
706f751b6SCorentin Labbe  *
806f751b6SCorentin Labbe  * Core file which registers crypto algorithms supported by the CryptoEngine.
906f751b6SCorentin Labbe  *
1039db3f15SJonathan Corbet  * You could find a link for the datasheet in Documentation/arch/arm/sunxi.rst
1106f751b6SCorentin Labbe  */
1207e34cd3SHerbert Xu 
1307e34cd3SHerbert Xu #include <crypto/engine.h>
1407e34cd3SHerbert Xu #include <crypto/internal/hash.h>
1507e34cd3SHerbert Xu #include <crypto/internal/rng.h>
1607e34cd3SHerbert Xu #include <crypto/internal/skcipher.h>
1706f751b6SCorentin Labbe #include <linux/clk.h>
1806f751b6SCorentin Labbe #include <linux/delay.h>
1906f751b6SCorentin Labbe #include <linux/dma-mapping.h>
2007e34cd3SHerbert Xu #include <linux/err.h>
2106f751b6SCorentin Labbe #include <linux/interrupt.h>
2206f751b6SCorentin Labbe #include <linux/io.h>
2306f751b6SCorentin Labbe #include <linux/irq.h>
2407e34cd3SHerbert Xu #include <linux/kernel.h>
2506f751b6SCorentin Labbe #include <linux/module.h>
2606f751b6SCorentin Labbe #include <linux/of.h>
2706f751b6SCorentin Labbe #include <linux/platform_device.h>
2806f751b6SCorentin Labbe #include <linux/pm_runtime.h>
2906f751b6SCorentin Labbe #include <linux/reset.h>
3006f751b6SCorentin Labbe 
3106f751b6SCorentin Labbe #include "sun8i-ce.h"
3206f751b6SCorentin Labbe 
3306f751b6SCorentin Labbe /*
3406f751b6SCorentin Labbe  * mod clock is lower on H3 than other SoC due to some DMA timeout occurring
3506f751b6SCorentin Labbe  * with high value.
3606f751b6SCorentin Labbe  * If you want to tune mod clock, loading driver and passing selftest is
3706f751b6SCorentin Labbe  * insufficient, you need to test with some LUKS test (mount and write to it)
3806f751b6SCorentin Labbe  */
3906f751b6SCorentin Labbe static const struct ce_variant ce_h3_variant = {
4006f751b6SCorentin Labbe 	.alg_cipher = { CE_ALG_AES, CE_ALG_DES, CE_ALG_3DES,
4106f751b6SCorentin Labbe 	},
4256f6d5aeSCorentin Labbe 	.alg_hash = { CE_ALG_MD5, CE_ALG_SHA1, CE_ALG_SHA224, CE_ALG_SHA256,
4356f6d5aeSCorentin Labbe 		CE_ALG_SHA384, CE_ALG_SHA512
4456f6d5aeSCorentin Labbe 	},
4506f751b6SCorentin Labbe 	.op_mode = { CE_OP_ECB, CE_OP_CBC
4606f751b6SCorentin Labbe 	},
4706f751b6SCorentin Labbe 	.ce_clks = {
4806f751b6SCorentin Labbe 		{ "bus", 0, 200000000 },
4906f751b6SCorentin Labbe 		{ "mod", 50000000, 0 },
50e66862e6SCorentin Labbe 		},
51e66862e6SCorentin Labbe 	.esr = ESR_H3,
525eb7e946SCorentin Labbe 	.prng = CE_ALG_PRNG,
534a07eab3SCorentin Labbe 	.trng = CE_ID_NOTSUPP,
5406f751b6SCorentin Labbe };
5506f751b6SCorentin Labbe 
5606f751b6SCorentin Labbe static const struct ce_variant ce_h5_variant = {
5706f751b6SCorentin Labbe 	.alg_cipher = { CE_ALG_AES, CE_ALG_DES, CE_ALG_3DES,
5806f751b6SCorentin Labbe 	},
5956f6d5aeSCorentin Labbe 	.alg_hash = { CE_ALG_MD5, CE_ALG_SHA1, CE_ALG_SHA224, CE_ALG_SHA256,
6056f6d5aeSCorentin Labbe 		CE_ID_NOTSUPP, CE_ID_NOTSUPP
6156f6d5aeSCorentin Labbe 	},
6206f751b6SCorentin Labbe 	.op_mode = { CE_OP_ECB, CE_OP_CBC
6306f751b6SCorentin Labbe 	},
6406f751b6SCorentin Labbe 	.ce_clks = {
6506f751b6SCorentin Labbe 		{ "bus", 0, 200000000 },
6606f751b6SCorentin Labbe 		{ "mod", 300000000, 0 },
67e66862e6SCorentin Labbe 		},
68e66862e6SCorentin Labbe 	.esr = ESR_H5,
695eb7e946SCorentin Labbe 	.prng = CE_ALG_PRNG,
704a07eab3SCorentin Labbe 	.trng = CE_ID_NOTSUPP,
7106f751b6SCorentin Labbe };
7206f751b6SCorentin Labbe 
7306f751b6SCorentin Labbe static const struct ce_variant ce_h6_variant = {
7406f751b6SCorentin Labbe 	.alg_cipher = { CE_ALG_AES, CE_ALG_DES, CE_ALG_3DES,
7506f751b6SCorentin Labbe 	},
7656f6d5aeSCorentin Labbe 	.alg_hash = { CE_ALG_MD5, CE_ALG_SHA1, CE_ALG_SHA224, CE_ALG_SHA256,
7756f6d5aeSCorentin Labbe 		CE_ALG_SHA384, CE_ALG_SHA512
7856f6d5aeSCorentin Labbe 	},
7906f751b6SCorentin Labbe 	.op_mode = { CE_OP_ECB, CE_OP_CBC
8006f751b6SCorentin Labbe 	},
816b4f76c2SCorentin Labbe 	.cipher_t_dlen_in_bytes = true,
8256f6d5aeSCorentin Labbe 	.hash_t_dlen_in_bits = true,
835eb7e946SCorentin Labbe 	.prng_t_dlen_in_bytes = true,
844a07eab3SCorentin Labbe 	.trng_t_dlen_in_bytes = true,
8506f751b6SCorentin Labbe 	.ce_clks = {
8606f751b6SCorentin Labbe 		{ "bus", 0, 200000000 },
8706f751b6SCorentin Labbe 		{ "mod", 300000000, 0 },
8806f751b6SCorentin Labbe 		{ "ram", 0, 400000000 },
89e66862e6SCorentin Labbe 		},
90e66862e6SCorentin Labbe 	.esr = ESR_H6,
915eb7e946SCorentin Labbe 	.prng = CE_ALG_PRNG_V2,
924a07eab3SCorentin Labbe 	.trng = CE_ALG_TRNG_V2,
9306f751b6SCorentin Labbe };
9406f751b6SCorentin Labbe 
95*1611f749SAndre Przywara static const struct ce_variant ce_h616_variant = {
96*1611f749SAndre Przywara 	.alg_cipher = { CE_ALG_AES, CE_ALG_DES, CE_ALG_3DES,
97*1611f749SAndre Przywara 	},
98*1611f749SAndre Przywara 	.alg_hash = { CE_ALG_MD5, CE_ALG_SHA1, CE_ALG_SHA224, CE_ALG_SHA256,
99*1611f749SAndre Przywara 		CE_ALG_SHA384, CE_ALG_SHA512
100*1611f749SAndre Przywara 	},
101*1611f749SAndre Przywara 	.op_mode = { CE_OP_ECB, CE_OP_CBC
102*1611f749SAndre Przywara 	},
103*1611f749SAndre Przywara 	.cipher_t_dlen_in_bytes = true,
104*1611f749SAndre Przywara 	.hash_t_dlen_in_bits = true,
105*1611f749SAndre Przywara 	.prng_t_dlen_in_bytes = true,
106*1611f749SAndre Przywara 	.trng_t_dlen_in_bytes = true,
107*1611f749SAndre Przywara 	.needs_word_addresses = true,
108*1611f749SAndre Przywara 	.ce_clks = {
109*1611f749SAndre Przywara 		{ "bus", 0, 200000000 },
110*1611f749SAndre Przywara 		{ "mod", 300000000, 0 },
111*1611f749SAndre Przywara 		{ "ram", 0, 400000000 },
112*1611f749SAndre Przywara 		{ "trng", 0, 0 },
113*1611f749SAndre Przywara 		},
114*1611f749SAndre Przywara 	.esr = ESR_H6,
115*1611f749SAndre Przywara 	.prng = CE_ALG_PRNG_V2,
116*1611f749SAndre Przywara 	.trng = CE_ALG_TRNG_V2,
117*1611f749SAndre Przywara };
118*1611f749SAndre Przywara 
11906f751b6SCorentin Labbe static const struct ce_variant ce_a64_variant = {
12006f751b6SCorentin Labbe 	.alg_cipher = { CE_ALG_AES, CE_ALG_DES, CE_ALG_3DES,
12106f751b6SCorentin Labbe 	},
12256f6d5aeSCorentin Labbe 	.alg_hash = { CE_ALG_MD5, CE_ALG_SHA1, CE_ALG_SHA224, CE_ALG_SHA256,
12356f6d5aeSCorentin Labbe 		CE_ID_NOTSUPP, CE_ID_NOTSUPP
12456f6d5aeSCorentin Labbe 	},
12506f751b6SCorentin Labbe 	.op_mode = { CE_OP_ECB, CE_OP_CBC
12606f751b6SCorentin Labbe 	},
12706f751b6SCorentin Labbe 	.ce_clks = {
12806f751b6SCorentin Labbe 		{ "bus", 0, 200000000 },
12906f751b6SCorentin Labbe 		{ "mod", 300000000, 0 },
130e66862e6SCorentin Labbe 		},
131e66862e6SCorentin Labbe 	.esr = ESR_A64,
1325eb7e946SCorentin Labbe 	.prng = CE_ALG_PRNG,
1334a07eab3SCorentin Labbe 	.trng = CE_ID_NOTSUPP,
13406f751b6SCorentin Labbe };
13506f751b6SCorentin Labbe 
13683f50f29SCorentin Labbe static const struct ce_variant ce_d1_variant = {
13783f50f29SCorentin Labbe 	.alg_cipher = { CE_ALG_AES, CE_ALG_DES, CE_ALG_3DES,
13883f50f29SCorentin Labbe 	},
13983f50f29SCorentin Labbe 	.alg_hash = { CE_ALG_MD5, CE_ALG_SHA1, CE_ALG_SHA224, CE_ALG_SHA256,
14083f50f29SCorentin Labbe 		CE_ALG_SHA384, CE_ALG_SHA512
14183f50f29SCorentin Labbe 	},
14283f50f29SCorentin Labbe 	.op_mode = { CE_OP_ECB, CE_OP_CBC
14383f50f29SCorentin Labbe 	},
14483f50f29SCorentin Labbe 	.ce_clks = {
14583f50f29SCorentin Labbe 		{ "bus", 0, 200000000 },
14683f50f29SCorentin Labbe 		{ "mod", 300000000, 0 },
14783f50f29SCorentin Labbe 		{ "ram", 0, 400000000 },
148f81c1d4aSSamuel Holland 		{ "trng", 0, 0 },
14983f50f29SCorentin Labbe 		},
15083f50f29SCorentin Labbe 	.esr = ESR_D1,
15183f50f29SCorentin Labbe 	.prng = CE_ALG_PRNG,
15283f50f29SCorentin Labbe 	.trng = CE_ALG_TRNG,
15383f50f29SCorentin Labbe };
15483f50f29SCorentin Labbe 
15506f751b6SCorentin Labbe static const struct ce_variant ce_r40_variant = {
15606f751b6SCorentin Labbe 	.alg_cipher = { CE_ALG_AES, CE_ALG_DES, CE_ALG_3DES,
15706f751b6SCorentin Labbe 	},
15856f6d5aeSCorentin Labbe 	.alg_hash = { CE_ALG_MD5, CE_ALG_SHA1, CE_ALG_SHA224, CE_ALG_SHA256,
15956f6d5aeSCorentin Labbe 		CE_ID_NOTSUPP, CE_ID_NOTSUPP
16056f6d5aeSCorentin Labbe 	},
16106f751b6SCorentin Labbe 	.op_mode = { CE_OP_ECB, CE_OP_CBC
16206f751b6SCorentin Labbe 	},
16306f751b6SCorentin Labbe 	.ce_clks = {
16406f751b6SCorentin Labbe 		{ "bus", 0, 200000000 },
16506f751b6SCorentin Labbe 		{ "mod", 300000000, 0 },
166e66862e6SCorentin Labbe 		},
167e66862e6SCorentin Labbe 	.esr = ESR_R40,
1685eb7e946SCorentin Labbe 	.prng = CE_ALG_PRNG,
1694a07eab3SCorentin Labbe 	.trng = CE_ID_NOTSUPP,
17006f751b6SCorentin Labbe };
17106f751b6SCorentin Labbe 
17206f751b6SCorentin Labbe /*
17306f751b6SCorentin Labbe  * sun8i_ce_get_engine_number() get the next channel slot
17406f751b6SCorentin Labbe  * This is a simple round-robin way of getting the next channel
1755eb7e946SCorentin Labbe  * The flow 3 is reserve for xRNG operations
17606f751b6SCorentin Labbe  */
sun8i_ce_get_engine_number(struct sun8i_ce_dev * ce)17706f751b6SCorentin Labbe int sun8i_ce_get_engine_number(struct sun8i_ce_dev *ce)
17806f751b6SCorentin Labbe {
1795eb7e946SCorentin Labbe 	return atomic_inc_return(&ce->flow) % (MAXFLOW - 1);
18006f751b6SCorentin Labbe }
18106f751b6SCorentin Labbe 
sun8i_ce_run_task(struct sun8i_ce_dev * ce,int flow,const char * name)18206f751b6SCorentin Labbe int sun8i_ce_run_task(struct sun8i_ce_dev *ce, int flow, const char *name)
18306f751b6SCorentin Labbe {
18406f751b6SCorentin Labbe 	u32 v;
18506f751b6SCorentin Labbe 	int err = 0;
186e66862e6SCorentin Labbe 	struct ce_task *cet = ce->chanlist[flow].tl;
18706f751b6SCorentin Labbe 
18806f751b6SCorentin Labbe #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
18906f751b6SCorentin Labbe 	ce->chanlist[flow].stat_req++;
19006f751b6SCorentin Labbe #endif
19106f751b6SCorentin Labbe 
19206f751b6SCorentin Labbe 	mutex_lock(&ce->mlock);
19306f751b6SCorentin Labbe 
19406f751b6SCorentin Labbe 	v = readl(ce->base + CE_ICR);
19506f751b6SCorentin Labbe 	v |= 1 << flow;
19606f751b6SCorentin Labbe 	writel(v, ce->base + CE_ICR);
19706f751b6SCorentin Labbe 
19806f751b6SCorentin Labbe 	reinit_completion(&ce->chanlist[flow].complete);
199e0740beeSAndre Przywara 	writel(desc_addr_val(ce, ce->chanlist[flow].t_phy), ce->base + CE_TDQ);
20006f751b6SCorentin Labbe 
20106f751b6SCorentin Labbe 	ce->chanlist[flow].status = 0;
20206f751b6SCorentin Labbe 	/* Be sure all data is written before enabling the task */
20306f751b6SCorentin Labbe 	wmb();
20406f751b6SCorentin Labbe 
20587f34260SCorentin Labbe 	/* Only H6 needs to write a part of t_common_ctl along with "1", but since it is ignored
20687f34260SCorentin Labbe 	 * on older SoCs, we have no reason to complicate things.
20787f34260SCorentin Labbe 	 */
20887f34260SCorentin Labbe 	v = 1 | ((le32_to_cpu(ce->chanlist[flow].tl->t_common_ctl) & 0x7F) << 8);
20906f751b6SCorentin Labbe 	writel(v, ce->base + CE_TLR);
21006f751b6SCorentin Labbe 	mutex_unlock(&ce->mlock);
21106f751b6SCorentin Labbe 
21206f751b6SCorentin Labbe 	wait_for_completion_interruptible_timeout(&ce->chanlist[flow].complete,
21306f751b6SCorentin Labbe 			msecs_to_jiffies(ce->chanlist[flow].timeout));
21406f751b6SCorentin Labbe 
21506f751b6SCorentin Labbe 	if (ce->chanlist[flow].status == 0) {
216e66862e6SCorentin Labbe 		dev_err(ce->dev, "DMA timeout for %s (tm=%d) on flow %d\n", name,
217e66862e6SCorentin Labbe 			ce->chanlist[flow].timeout, flow);
21806f751b6SCorentin Labbe 		err = -EFAULT;
21906f751b6SCorentin Labbe 	}
22006f751b6SCorentin Labbe 	/* No need to lock for this read, the channel is locked so
22106f751b6SCorentin Labbe 	 * nothing could modify the error value for this channel
22206f751b6SCorentin Labbe 	 */
22306f751b6SCorentin Labbe 	v = readl(ce->base + CE_ESR);
224e66862e6SCorentin Labbe 	switch (ce->variant->esr) {
225e66862e6SCorentin Labbe 	case ESR_H3:
226e66862e6SCorentin Labbe 		/* Sadly, the error bit is not per flow */
22706f751b6SCorentin Labbe 		if (v) {
228e66862e6SCorentin Labbe 			dev_err(ce->dev, "CE ERROR: %x for flow %x\n", v, flow);
229e66862e6SCorentin Labbe 			err = -EFAULT;
230e66862e6SCorentin Labbe 			print_hex_dump(KERN_INFO, "TASK: ", DUMP_PREFIX_NONE, 16, 4,
231e66862e6SCorentin Labbe 				       cet, sizeof(struct ce_task), false);
232e66862e6SCorentin Labbe 		}
233e66862e6SCorentin Labbe 		if (v & CE_ERR_ALGO_NOTSUP)
234e66862e6SCorentin Labbe 			dev_err(ce->dev, "CE ERROR: algorithm not supported\n");
235e66862e6SCorentin Labbe 		if (v & CE_ERR_DATALEN)
236e66862e6SCorentin Labbe 			dev_err(ce->dev, "CE ERROR: data length error\n");
237e66862e6SCorentin Labbe 		if (v & CE_ERR_KEYSRAM)
238e66862e6SCorentin Labbe 			dev_err(ce->dev, "CE ERROR: keysram access error for AES\n");
239e66862e6SCorentin Labbe 		break;
240e66862e6SCorentin Labbe 	case ESR_A64:
24183f50f29SCorentin Labbe 	case ESR_D1:
242e66862e6SCorentin Labbe 	case ESR_H5:
243e66862e6SCorentin Labbe 	case ESR_R40:
24406f751b6SCorentin Labbe 		v >>= (flow * 4);
245e66862e6SCorentin Labbe 		v &= 0xF;
246e66862e6SCorentin Labbe 		if (v) {
247e66862e6SCorentin Labbe 			dev_err(ce->dev, "CE ERROR: %x for flow %x\n", v, flow);
248e66862e6SCorentin Labbe 			err = -EFAULT;
249e66862e6SCorentin Labbe 			print_hex_dump(KERN_INFO, "TASK: ", DUMP_PREFIX_NONE, 16, 4,
250e66862e6SCorentin Labbe 				       cet, sizeof(struct ce_task), false);
251e66862e6SCorentin Labbe 		}
252e66862e6SCorentin Labbe 		if (v & CE_ERR_ALGO_NOTSUP)
253e66862e6SCorentin Labbe 			dev_err(ce->dev, "CE ERROR: algorithm not supported\n");
254e66862e6SCorentin Labbe 		if (v & CE_ERR_DATALEN)
255e66862e6SCorentin Labbe 			dev_err(ce->dev, "CE ERROR: data length error\n");
256e66862e6SCorentin Labbe 		if (v & CE_ERR_KEYSRAM)
257e66862e6SCorentin Labbe 			dev_err(ce->dev, "CE ERROR: keysram access error for AES\n");
258e66862e6SCorentin Labbe 		break;
259e66862e6SCorentin Labbe 	case ESR_H6:
260e66862e6SCorentin Labbe 		v >>= (flow * 8);
26106f751b6SCorentin Labbe 		v &= 0xFF;
26206f751b6SCorentin Labbe 		if (v) {
26306f751b6SCorentin Labbe 			dev_err(ce->dev, "CE ERROR: %x for flow %x\n", v, flow);
26406f751b6SCorentin Labbe 			err = -EFAULT;
265e66862e6SCorentin Labbe 			print_hex_dump(KERN_INFO, "TASK: ", DUMP_PREFIX_NONE, 16, 4,
266e66862e6SCorentin Labbe 				       cet, sizeof(struct ce_task), false);
26706f751b6SCorentin Labbe 		}
26806f751b6SCorentin Labbe 		if (v & CE_ERR_ALGO_NOTSUP)
26906f751b6SCorentin Labbe 			dev_err(ce->dev, "CE ERROR: algorithm not supported\n");
27006f751b6SCorentin Labbe 		if (v & CE_ERR_DATALEN)
27106f751b6SCorentin Labbe 			dev_err(ce->dev, "CE ERROR: data length error\n");
27206f751b6SCorentin Labbe 		if (v & CE_ERR_KEYSRAM)
27306f751b6SCorentin Labbe 			dev_err(ce->dev, "CE ERROR: keysram access error for AES\n");
27406f751b6SCorentin Labbe 		if (v & CE_ERR_ADDR_INVALID)
27506f751b6SCorentin Labbe 			dev_err(ce->dev, "CE ERROR: address invalid\n");
276e66862e6SCorentin Labbe 		if (v & CE_ERR_KEYLADDER)
277e66862e6SCorentin Labbe 			dev_err(ce->dev, "CE ERROR: key ladder configuration error\n");
278e66862e6SCorentin Labbe 		break;
27906f751b6SCorentin Labbe 	}
28006f751b6SCorentin Labbe 
28106f751b6SCorentin Labbe 	return err;
28206f751b6SCorentin Labbe }
28306f751b6SCorentin Labbe 
ce_irq_handler(int irq,void * data)28406f751b6SCorentin Labbe static irqreturn_t ce_irq_handler(int irq, void *data)
28506f751b6SCorentin Labbe {
28606f751b6SCorentin Labbe 	struct sun8i_ce_dev *ce = (struct sun8i_ce_dev *)data;
28706f751b6SCorentin Labbe 	int flow = 0;
28806f751b6SCorentin Labbe 	u32 p;
28906f751b6SCorentin Labbe 
29006f751b6SCorentin Labbe 	p = readl(ce->base + CE_ISR);
29106f751b6SCorentin Labbe 	for (flow = 0; flow < MAXFLOW; flow++) {
29206f751b6SCorentin Labbe 		if (p & (BIT(flow))) {
29306f751b6SCorentin Labbe 			writel(BIT(flow), ce->base + CE_ISR);
29406f751b6SCorentin Labbe 			ce->chanlist[flow].status = 1;
29506f751b6SCorentin Labbe 			complete(&ce->chanlist[flow].complete);
29606f751b6SCorentin Labbe 		}
29706f751b6SCorentin Labbe 	}
29806f751b6SCorentin Labbe 
29906f751b6SCorentin Labbe 	return IRQ_HANDLED;
30006f751b6SCorentin Labbe }
30106f751b6SCorentin Labbe 
30206f751b6SCorentin Labbe static struct sun8i_ce_alg_template ce_algs[] = {
30306f751b6SCorentin Labbe {
30406f751b6SCorentin Labbe 	.type = CRYPTO_ALG_TYPE_SKCIPHER,
30506f751b6SCorentin Labbe 	.ce_algo_id = CE_ID_CIPHER_AES,
30606f751b6SCorentin Labbe 	.ce_blockmode = CE_ID_OP_CBC,
30707e34cd3SHerbert Xu 	.alg.skcipher.base = {
30806f751b6SCorentin Labbe 		.base = {
30906f751b6SCorentin Labbe 			.cra_name = "cbc(aes)",
31006f751b6SCorentin Labbe 			.cra_driver_name = "cbc-aes-sun8i-ce",
31106f751b6SCorentin Labbe 			.cra_priority = 400,
31206f751b6SCorentin Labbe 			.cra_blocksize = AES_BLOCK_SIZE,
31306f751b6SCorentin Labbe 			.cra_flags = CRYPTO_ALG_TYPE_SKCIPHER |
31422f7c2f8SCorentin Labbe 				CRYPTO_ALG_ASYNC |
315b8aa7dc5SMikulas Patocka 				CRYPTO_ALG_NEED_FALLBACK,
31606f751b6SCorentin Labbe 			.cra_ctxsize = sizeof(struct sun8i_cipher_tfm_ctx),
31706f751b6SCorentin Labbe 			.cra_module = THIS_MODULE,
31806f751b6SCorentin Labbe 			.cra_alignmask = 0xf,
31906f751b6SCorentin Labbe 			.cra_init = sun8i_ce_cipher_init,
32006f751b6SCorentin Labbe 			.cra_exit = sun8i_ce_cipher_exit,
32106f751b6SCorentin Labbe 		},
32206f751b6SCorentin Labbe 		.min_keysize	= AES_MIN_KEY_SIZE,
32306f751b6SCorentin Labbe 		.max_keysize	= AES_MAX_KEY_SIZE,
32406f751b6SCorentin Labbe 		.ivsize		= AES_BLOCK_SIZE,
32506f751b6SCorentin Labbe 		.setkey		= sun8i_ce_aes_setkey,
32606f751b6SCorentin Labbe 		.encrypt	= sun8i_ce_skencrypt,
32706f751b6SCorentin Labbe 		.decrypt	= sun8i_ce_skdecrypt,
32807e34cd3SHerbert Xu 	},
32907e34cd3SHerbert Xu 	.alg.skcipher.op = {
33007e34cd3SHerbert Xu 		.do_one_request = sun8i_ce_cipher_do_one,
33107e34cd3SHerbert Xu 	},
33206f751b6SCorentin Labbe },
33306f751b6SCorentin Labbe {
33406f751b6SCorentin Labbe 	.type = CRYPTO_ALG_TYPE_SKCIPHER,
33506f751b6SCorentin Labbe 	.ce_algo_id = CE_ID_CIPHER_AES,
33606f751b6SCorentin Labbe 	.ce_blockmode = CE_ID_OP_ECB,
33707e34cd3SHerbert Xu 	.alg.skcipher.base = {
33806f751b6SCorentin Labbe 		.base = {
33906f751b6SCorentin Labbe 			.cra_name = "ecb(aes)",
34006f751b6SCorentin Labbe 			.cra_driver_name = "ecb-aes-sun8i-ce",
34106f751b6SCorentin Labbe 			.cra_priority = 400,
34206f751b6SCorentin Labbe 			.cra_blocksize = AES_BLOCK_SIZE,
34306f751b6SCorentin Labbe 			.cra_flags = CRYPTO_ALG_TYPE_SKCIPHER |
34422f7c2f8SCorentin Labbe 				CRYPTO_ALG_ASYNC |
345b8aa7dc5SMikulas Patocka 				CRYPTO_ALG_NEED_FALLBACK,
34606f751b6SCorentin Labbe 			.cra_ctxsize = sizeof(struct sun8i_cipher_tfm_ctx),
34706f751b6SCorentin Labbe 			.cra_module = THIS_MODULE,
34806f751b6SCorentin Labbe 			.cra_alignmask = 0xf,
34906f751b6SCorentin Labbe 			.cra_init = sun8i_ce_cipher_init,
35006f751b6SCorentin Labbe 			.cra_exit = sun8i_ce_cipher_exit,
35106f751b6SCorentin Labbe 		},
35206f751b6SCorentin Labbe 		.min_keysize	= AES_MIN_KEY_SIZE,
35306f751b6SCorentin Labbe 		.max_keysize	= AES_MAX_KEY_SIZE,
35406f751b6SCorentin Labbe 		.setkey		= sun8i_ce_aes_setkey,
35506f751b6SCorentin Labbe 		.encrypt	= sun8i_ce_skencrypt,
35606f751b6SCorentin Labbe 		.decrypt	= sun8i_ce_skdecrypt,
35707e34cd3SHerbert Xu 	},
35807e34cd3SHerbert Xu 	.alg.skcipher.op = {
35907e34cd3SHerbert Xu 		.do_one_request = sun8i_ce_cipher_do_one,
36007e34cd3SHerbert Xu 	},
36106f751b6SCorentin Labbe },
36206f751b6SCorentin Labbe {
36306f751b6SCorentin Labbe 	.type = CRYPTO_ALG_TYPE_SKCIPHER,
36406f751b6SCorentin Labbe 	.ce_algo_id = CE_ID_CIPHER_DES3,
36506f751b6SCorentin Labbe 	.ce_blockmode = CE_ID_OP_CBC,
36607e34cd3SHerbert Xu 	.alg.skcipher.base = {
36706f751b6SCorentin Labbe 		.base = {
36806f751b6SCorentin Labbe 			.cra_name = "cbc(des3_ede)",
36906f751b6SCorentin Labbe 			.cra_driver_name = "cbc-des3-sun8i-ce",
37006f751b6SCorentin Labbe 			.cra_priority = 400,
37106f751b6SCorentin Labbe 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
37206f751b6SCorentin Labbe 			.cra_flags = CRYPTO_ALG_TYPE_SKCIPHER |
37322f7c2f8SCorentin Labbe 				CRYPTO_ALG_ASYNC |
374b8aa7dc5SMikulas Patocka 				CRYPTO_ALG_NEED_FALLBACK,
37506f751b6SCorentin Labbe 			.cra_ctxsize = sizeof(struct sun8i_cipher_tfm_ctx),
37606f751b6SCorentin Labbe 			.cra_module = THIS_MODULE,
37706f751b6SCorentin Labbe 			.cra_alignmask = 0xf,
37806f751b6SCorentin Labbe 			.cra_init = sun8i_ce_cipher_init,
37906f751b6SCorentin Labbe 			.cra_exit = sun8i_ce_cipher_exit,
38006f751b6SCorentin Labbe 		},
38106f751b6SCorentin Labbe 		.min_keysize	= DES3_EDE_KEY_SIZE,
38206f751b6SCorentin Labbe 		.max_keysize	= DES3_EDE_KEY_SIZE,
38306f751b6SCorentin Labbe 		.ivsize		= DES3_EDE_BLOCK_SIZE,
38406f751b6SCorentin Labbe 		.setkey		= sun8i_ce_des3_setkey,
38506f751b6SCorentin Labbe 		.encrypt	= sun8i_ce_skencrypt,
38606f751b6SCorentin Labbe 		.decrypt	= sun8i_ce_skdecrypt,
38707e34cd3SHerbert Xu 	},
38807e34cd3SHerbert Xu 	.alg.skcipher.op = {
38907e34cd3SHerbert Xu 		.do_one_request = sun8i_ce_cipher_do_one,
39007e34cd3SHerbert Xu 	},
39106f751b6SCorentin Labbe },
39206f751b6SCorentin Labbe {
39306f751b6SCorentin Labbe 	.type = CRYPTO_ALG_TYPE_SKCIPHER,
39406f751b6SCorentin Labbe 	.ce_algo_id = CE_ID_CIPHER_DES3,
39506f751b6SCorentin Labbe 	.ce_blockmode = CE_ID_OP_ECB,
39607e34cd3SHerbert Xu 	.alg.skcipher.base = {
39706f751b6SCorentin Labbe 		.base = {
39806f751b6SCorentin Labbe 			.cra_name = "ecb(des3_ede)",
39906f751b6SCorentin Labbe 			.cra_driver_name = "ecb-des3-sun8i-ce",
40006f751b6SCorentin Labbe 			.cra_priority = 400,
40106f751b6SCorentin Labbe 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
40206f751b6SCorentin Labbe 			.cra_flags = CRYPTO_ALG_TYPE_SKCIPHER |
40322f7c2f8SCorentin Labbe 				CRYPTO_ALG_ASYNC |
404b8aa7dc5SMikulas Patocka 				CRYPTO_ALG_NEED_FALLBACK,
40506f751b6SCorentin Labbe 			.cra_ctxsize = sizeof(struct sun8i_cipher_tfm_ctx),
40606f751b6SCorentin Labbe 			.cra_module = THIS_MODULE,
40706f751b6SCorentin Labbe 			.cra_alignmask = 0xf,
40806f751b6SCorentin Labbe 			.cra_init = sun8i_ce_cipher_init,
40906f751b6SCorentin Labbe 			.cra_exit = sun8i_ce_cipher_exit,
41006f751b6SCorentin Labbe 		},
41106f751b6SCorentin Labbe 		.min_keysize	= DES3_EDE_KEY_SIZE,
41206f751b6SCorentin Labbe 		.max_keysize	= DES3_EDE_KEY_SIZE,
41306f751b6SCorentin Labbe 		.setkey		= sun8i_ce_des3_setkey,
41406f751b6SCorentin Labbe 		.encrypt	= sun8i_ce_skencrypt,
41506f751b6SCorentin Labbe 		.decrypt	= sun8i_ce_skdecrypt,
41607e34cd3SHerbert Xu 	},
41707e34cd3SHerbert Xu 	.alg.skcipher.op = {
41807e34cd3SHerbert Xu 		.do_one_request = sun8i_ce_cipher_do_one,
41907e34cd3SHerbert Xu 	},
42006f751b6SCorentin Labbe },
42156f6d5aeSCorentin Labbe #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_HASH
42256f6d5aeSCorentin Labbe {	.type = CRYPTO_ALG_TYPE_AHASH,
42356f6d5aeSCorentin Labbe 	.ce_algo_id = CE_ID_HASH_MD5,
42407e34cd3SHerbert Xu 	.alg.hash.base = {
42556f6d5aeSCorentin Labbe 		.init = sun8i_ce_hash_init,
42656f6d5aeSCorentin Labbe 		.update = sun8i_ce_hash_update,
42756f6d5aeSCorentin Labbe 		.final = sun8i_ce_hash_final,
42856f6d5aeSCorentin Labbe 		.finup = sun8i_ce_hash_finup,
42956f6d5aeSCorentin Labbe 		.digest = sun8i_ce_hash_digest,
43056f6d5aeSCorentin Labbe 		.export = sun8i_ce_hash_export,
43156f6d5aeSCorentin Labbe 		.import = sun8i_ce_hash_import,
43207e34cd3SHerbert Xu 		.init_tfm = sun8i_ce_hash_init_tfm,
43307e34cd3SHerbert Xu 		.exit_tfm = sun8i_ce_hash_exit_tfm,
43456f6d5aeSCorentin Labbe 		.halg = {
43556f6d5aeSCorentin Labbe 			.digestsize = MD5_DIGEST_SIZE,
43656f6d5aeSCorentin Labbe 			.statesize = sizeof(struct md5_state),
43756f6d5aeSCorentin Labbe 			.base = {
43856f6d5aeSCorentin Labbe 				.cra_name = "md5",
43956f6d5aeSCorentin Labbe 				.cra_driver_name = "md5-sun8i-ce",
44056f6d5aeSCorentin Labbe 				.cra_priority = 300,
44156f6d5aeSCorentin Labbe 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
44256f6d5aeSCorentin Labbe 					CRYPTO_ALG_ASYNC |
44356f6d5aeSCorentin Labbe 					CRYPTO_ALG_NEED_FALLBACK,
44456f6d5aeSCorentin Labbe 				.cra_blocksize = MD5_HMAC_BLOCK_SIZE,
44556f6d5aeSCorentin Labbe 				.cra_ctxsize = sizeof(struct sun8i_ce_hash_tfm_ctx),
44656f6d5aeSCorentin Labbe 				.cra_module = THIS_MODULE,
44756f6d5aeSCorentin Labbe 			}
44856f6d5aeSCorentin Labbe 		}
44956f6d5aeSCorentin Labbe 	},
45007e34cd3SHerbert Xu 	.alg.hash.op = {
45107e34cd3SHerbert Xu 		.do_one_request = sun8i_ce_hash_run,
45207e34cd3SHerbert Xu 	},
45307e34cd3SHerbert Xu 
45407e34cd3SHerbert Xu },
45556f6d5aeSCorentin Labbe {	.type = CRYPTO_ALG_TYPE_AHASH,
45656f6d5aeSCorentin Labbe 	.ce_algo_id = CE_ID_HASH_SHA1,
45707e34cd3SHerbert Xu 	.alg.hash.base = {
45856f6d5aeSCorentin Labbe 		.init = sun8i_ce_hash_init,
45956f6d5aeSCorentin Labbe 		.update = sun8i_ce_hash_update,
46056f6d5aeSCorentin Labbe 		.final = sun8i_ce_hash_final,
46156f6d5aeSCorentin Labbe 		.finup = sun8i_ce_hash_finup,
46256f6d5aeSCorentin Labbe 		.digest = sun8i_ce_hash_digest,
46356f6d5aeSCorentin Labbe 		.export = sun8i_ce_hash_export,
46456f6d5aeSCorentin Labbe 		.import = sun8i_ce_hash_import,
46507e34cd3SHerbert Xu 		.init_tfm = sun8i_ce_hash_init_tfm,
46607e34cd3SHerbert Xu 		.exit_tfm = sun8i_ce_hash_exit_tfm,
46756f6d5aeSCorentin Labbe 		.halg = {
46856f6d5aeSCorentin Labbe 			.digestsize = SHA1_DIGEST_SIZE,
46956f6d5aeSCorentin Labbe 			.statesize = sizeof(struct sha1_state),
47056f6d5aeSCorentin Labbe 			.base = {
47156f6d5aeSCorentin Labbe 				.cra_name = "sha1",
47256f6d5aeSCorentin Labbe 				.cra_driver_name = "sha1-sun8i-ce",
47356f6d5aeSCorentin Labbe 				.cra_priority = 300,
47456f6d5aeSCorentin Labbe 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
47556f6d5aeSCorentin Labbe 					CRYPTO_ALG_ASYNC |
47656f6d5aeSCorentin Labbe 					CRYPTO_ALG_NEED_FALLBACK,
47756f6d5aeSCorentin Labbe 				.cra_blocksize = SHA1_BLOCK_SIZE,
47856f6d5aeSCorentin Labbe 				.cra_ctxsize = sizeof(struct sun8i_ce_hash_tfm_ctx),
47956f6d5aeSCorentin Labbe 				.cra_module = THIS_MODULE,
48056f6d5aeSCorentin Labbe 			}
48156f6d5aeSCorentin Labbe 		}
48256f6d5aeSCorentin Labbe 	},
48307e34cd3SHerbert Xu 	.alg.hash.op = {
48407e34cd3SHerbert Xu 		.do_one_request = sun8i_ce_hash_run,
48507e34cd3SHerbert Xu 	},
48607e34cd3SHerbert Xu },
48756f6d5aeSCorentin Labbe {	.type = CRYPTO_ALG_TYPE_AHASH,
48856f6d5aeSCorentin Labbe 	.ce_algo_id = CE_ID_HASH_SHA224,
48907e34cd3SHerbert Xu 	.alg.hash.base = {
49056f6d5aeSCorentin Labbe 		.init = sun8i_ce_hash_init,
49156f6d5aeSCorentin Labbe 		.update = sun8i_ce_hash_update,
49256f6d5aeSCorentin Labbe 		.final = sun8i_ce_hash_final,
49356f6d5aeSCorentin Labbe 		.finup = sun8i_ce_hash_finup,
49456f6d5aeSCorentin Labbe 		.digest = sun8i_ce_hash_digest,
49556f6d5aeSCorentin Labbe 		.export = sun8i_ce_hash_export,
49656f6d5aeSCorentin Labbe 		.import = sun8i_ce_hash_import,
49707e34cd3SHerbert Xu 		.init_tfm = sun8i_ce_hash_init_tfm,
49807e34cd3SHerbert Xu 		.exit_tfm = sun8i_ce_hash_exit_tfm,
49956f6d5aeSCorentin Labbe 		.halg = {
50056f6d5aeSCorentin Labbe 			.digestsize = SHA224_DIGEST_SIZE,
50156f6d5aeSCorentin Labbe 			.statesize = sizeof(struct sha256_state),
50256f6d5aeSCorentin Labbe 			.base = {
50356f6d5aeSCorentin Labbe 				.cra_name = "sha224",
50456f6d5aeSCorentin Labbe 				.cra_driver_name = "sha224-sun8i-ce",
50556f6d5aeSCorentin Labbe 				.cra_priority = 300,
50656f6d5aeSCorentin Labbe 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
50756f6d5aeSCorentin Labbe 					CRYPTO_ALG_ASYNC |
50856f6d5aeSCorentin Labbe 					CRYPTO_ALG_NEED_FALLBACK,
50956f6d5aeSCorentin Labbe 				.cra_blocksize = SHA224_BLOCK_SIZE,
51056f6d5aeSCorentin Labbe 				.cra_ctxsize = sizeof(struct sun8i_ce_hash_tfm_ctx),
51156f6d5aeSCorentin Labbe 				.cra_module = THIS_MODULE,
51256f6d5aeSCorentin Labbe 			}
51356f6d5aeSCorentin Labbe 		}
51456f6d5aeSCorentin Labbe 	},
51507e34cd3SHerbert Xu 	.alg.hash.op = {
51607e34cd3SHerbert Xu 		.do_one_request = sun8i_ce_hash_run,
51707e34cd3SHerbert Xu 	},
51807e34cd3SHerbert Xu },
51956f6d5aeSCorentin Labbe {	.type = CRYPTO_ALG_TYPE_AHASH,
52056f6d5aeSCorentin Labbe 	.ce_algo_id = CE_ID_HASH_SHA256,
52107e34cd3SHerbert Xu 	.alg.hash.base = {
52256f6d5aeSCorentin Labbe 		.init = sun8i_ce_hash_init,
52356f6d5aeSCorentin Labbe 		.update = sun8i_ce_hash_update,
52456f6d5aeSCorentin Labbe 		.final = sun8i_ce_hash_final,
52556f6d5aeSCorentin Labbe 		.finup = sun8i_ce_hash_finup,
52656f6d5aeSCorentin Labbe 		.digest = sun8i_ce_hash_digest,
52756f6d5aeSCorentin Labbe 		.export = sun8i_ce_hash_export,
52856f6d5aeSCorentin Labbe 		.import = sun8i_ce_hash_import,
52907e34cd3SHerbert Xu 		.init_tfm = sun8i_ce_hash_init_tfm,
53007e34cd3SHerbert Xu 		.exit_tfm = sun8i_ce_hash_exit_tfm,
53156f6d5aeSCorentin Labbe 		.halg = {
53256f6d5aeSCorentin Labbe 			.digestsize = SHA256_DIGEST_SIZE,
53356f6d5aeSCorentin Labbe 			.statesize = sizeof(struct sha256_state),
53456f6d5aeSCorentin Labbe 			.base = {
53556f6d5aeSCorentin Labbe 				.cra_name = "sha256",
53656f6d5aeSCorentin Labbe 				.cra_driver_name = "sha256-sun8i-ce",
53756f6d5aeSCorentin Labbe 				.cra_priority = 300,
53856f6d5aeSCorentin Labbe 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
53956f6d5aeSCorentin Labbe 					CRYPTO_ALG_ASYNC |
54056f6d5aeSCorentin Labbe 					CRYPTO_ALG_NEED_FALLBACK,
54156f6d5aeSCorentin Labbe 				.cra_blocksize = SHA256_BLOCK_SIZE,
54256f6d5aeSCorentin Labbe 				.cra_ctxsize = sizeof(struct sun8i_ce_hash_tfm_ctx),
54356f6d5aeSCorentin Labbe 				.cra_module = THIS_MODULE,
54456f6d5aeSCorentin Labbe 			}
54556f6d5aeSCorentin Labbe 		}
54656f6d5aeSCorentin Labbe 	},
54707e34cd3SHerbert Xu 	.alg.hash.op = {
54807e34cd3SHerbert Xu 		.do_one_request = sun8i_ce_hash_run,
54907e34cd3SHerbert Xu 	},
55007e34cd3SHerbert Xu },
55156f6d5aeSCorentin Labbe {	.type = CRYPTO_ALG_TYPE_AHASH,
55256f6d5aeSCorentin Labbe 	.ce_algo_id = CE_ID_HASH_SHA384,
55307e34cd3SHerbert Xu 	.alg.hash.base = {
55456f6d5aeSCorentin Labbe 		.init = sun8i_ce_hash_init,
55556f6d5aeSCorentin Labbe 		.update = sun8i_ce_hash_update,
55656f6d5aeSCorentin Labbe 		.final = sun8i_ce_hash_final,
55756f6d5aeSCorentin Labbe 		.finup = sun8i_ce_hash_finup,
55856f6d5aeSCorentin Labbe 		.digest = sun8i_ce_hash_digest,
55956f6d5aeSCorentin Labbe 		.export = sun8i_ce_hash_export,
56056f6d5aeSCorentin Labbe 		.import = sun8i_ce_hash_import,
56107e34cd3SHerbert Xu 		.init_tfm = sun8i_ce_hash_init_tfm,
56207e34cd3SHerbert Xu 		.exit_tfm = sun8i_ce_hash_exit_tfm,
56356f6d5aeSCorentin Labbe 		.halg = {
56456f6d5aeSCorentin Labbe 			.digestsize = SHA384_DIGEST_SIZE,
56556f6d5aeSCorentin Labbe 			.statesize = sizeof(struct sha512_state),
56656f6d5aeSCorentin Labbe 			.base = {
56756f6d5aeSCorentin Labbe 				.cra_name = "sha384",
56856f6d5aeSCorentin Labbe 				.cra_driver_name = "sha384-sun8i-ce",
56956f6d5aeSCorentin Labbe 				.cra_priority = 300,
57056f6d5aeSCorentin Labbe 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
57156f6d5aeSCorentin Labbe 					CRYPTO_ALG_ASYNC |
57256f6d5aeSCorentin Labbe 					CRYPTO_ALG_NEED_FALLBACK,
57356f6d5aeSCorentin Labbe 				.cra_blocksize = SHA384_BLOCK_SIZE,
57456f6d5aeSCorentin Labbe 				.cra_ctxsize = sizeof(struct sun8i_ce_hash_tfm_ctx),
57556f6d5aeSCorentin Labbe 				.cra_module = THIS_MODULE,
57656f6d5aeSCorentin Labbe 			}
57756f6d5aeSCorentin Labbe 		}
57856f6d5aeSCorentin Labbe 	},
57907e34cd3SHerbert Xu 	.alg.hash.op = {
58007e34cd3SHerbert Xu 		.do_one_request = sun8i_ce_hash_run,
58107e34cd3SHerbert Xu 	},
58207e34cd3SHerbert Xu },
58356f6d5aeSCorentin Labbe {	.type = CRYPTO_ALG_TYPE_AHASH,
58456f6d5aeSCorentin Labbe 	.ce_algo_id = CE_ID_HASH_SHA512,
58507e34cd3SHerbert Xu 	.alg.hash.base = {
58656f6d5aeSCorentin Labbe 		.init = sun8i_ce_hash_init,
58756f6d5aeSCorentin Labbe 		.update = sun8i_ce_hash_update,
58856f6d5aeSCorentin Labbe 		.final = sun8i_ce_hash_final,
58956f6d5aeSCorentin Labbe 		.finup = sun8i_ce_hash_finup,
59056f6d5aeSCorentin Labbe 		.digest = sun8i_ce_hash_digest,
59156f6d5aeSCorentin Labbe 		.export = sun8i_ce_hash_export,
59256f6d5aeSCorentin Labbe 		.import = sun8i_ce_hash_import,
59307e34cd3SHerbert Xu 		.init_tfm = sun8i_ce_hash_init_tfm,
59407e34cd3SHerbert Xu 		.exit_tfm = sun8i_ce_hash_exit_tfm,
59556f6d5aeSCorentin Labbe 		.halg = {
59656f6d5aeSCorentin Labbe 			.digestsize = SHA512_DIGEST_SIZE,
59756f6d5aeSCorentin Labbe 			.statesize = sizeof(struct sha512_state),
59856f6d5aeSCorentin Labbe 			.base = {
59956f6d5aeSCorentin Labbe 				.cra_name = "sha512",
60056f6d5aeSCorentin Labbe 				.cra_driver_name = "sha512-sun8i-ce",
60156f6d5aeSCorentin Labbe 				.cra_priority = 300,
60256f6d5aeSCorentin Labbe 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
60356f6d5aeSCorentin Labbe 					CRYPTO_ALG_ASYNC |
60456f6d5aeSCorentin Labbe 					CRYPTO_ALG_NEED_FALLBACK,
60556f6d5aeSCorentin Labbe 				.cra_blocksize = SHA512_BLOCK_SIZE,
60656f6d5aeSCorentin Labbe 				.cra_ctxsize = sizeof(struct sun8i_ce_hash_tfm_ctx),
60756f6d5aeSCorentin Labbe 				.cra_module = THIS_MODULE,
60856f6d5aeSCorentin Labbe 			}
60956f6d5aeSCorentin Labbe 		}
61007e34cd3SHerbert Xu 	},
61107e34cd3SHerbert Xu 	.alg.hash.op = {
61207e34cd3SHerbert Xu 		.do_one_request = sun8i_ce_hash_run,
61307e34cd3SHerbert Xu 	},
61456f6d5aeSCorentin Labbe },
61556f6d5aeSCorentin Labbe #endif
6165eb7e946SCorentin Labbe #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_PRNG
6175eb7e946SCorentin Labbe {
6185eb7e946SCorentin Labbe 	.type = CRYPTO_ALG_TYPE_RNG,
6195eb7e946SCorentin Labbe 	.alg.rng = {
6205eb7e946SCorentin Labbe 		.base = {
6215eb7e946SCorentin Labbe 			.cra_name		= "stdrng",
6225eb7e946SCorentin Labbe 			.cra_driver_name	= "sun8i-ce-prng",
6235eb7e946SCorentin Labbe 			.cra_priority		= 300,
6245eb7e946SCorentin Labbe 			.cra_ctxsize		= sizeof(struct sun8i_ce_rng_tfm_ctx),
6255eb7e946SCorentin Labbe 			.cra_module		= THIS_MODULE,
6265eb7e946SCorentin Labbe 			.cra_init		= sun8i_ce_prng_init,
6275eb7e946SCorentin Labbe 			.cra_exit		= sun8i_ce_prng_exit,
6285eb7e946SCorentin Labbe 		},
6295eb7e946SCorentin Labbe 		.generate               = sun8i_ce_prng_generate,
6305eb7e946SCorentin Labbe 		.seed                   = sun8i_ce_prng_seed,
6315eb7e946SCorentin Labbe 		.seedsize               = PRNG_SEED_SIZE,
6325eb7e946SCorentin Labbe 	}
6335eb7e946SCorentin Labbe },
6345eb7e946SCorentin Labbe #endif
63506f751b6SCorentin Labbe };
63606f751b6SCorentin Labbe 
sun8i_ce_debugfs_show(struct seq_file * seq,void * v)637560daf9eSQinglang Miao static int sun8i_ce_debugfs_show(struct seq_file *seq, void *v)
63806f751b6SCorentin Labbe {
63907e34cd3SHerbert Xu 	struct sun8i_ce_dev *ce __maybe_unused = seq->private;
64025d85ffcSCorentin Labbe 	unsigned int i;
64106f751b6SCorentin Labbe 
64206f751b6SCorentin Labbe 	for (i = 0; i < MAXFLOW; i++)
64307e34cd3SHerbert Xu 		seq_printf(seq, "Channel %d: nreq %lu\n", i,
64407e34cd3SHerbert Xu #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
64507e34cd3SHerbert Xu 			   ce->chanlist[i].stat_req);
64607e34cd3SHerbert Xu #else
64707e34cd3SHerbert Xu 			   0ul);
64807e34cd3SHerbert Xu #endif
64906f751b6SCorentin Labbe 
65006f751b6SCorentin Labbe 	for (i = 0; i < ARRAY_SIZE(ce_algs); i++) {
65106f751b6SCorentin Labbe 		if (!ce_algs[i].ce)
65206f751b6SCorentin Labbe 			continue;
65306f751b6SCorentin Labbe 		switch (ce_algs[i].type) {
65406f751b6SCorentin Labbe 		case CRYPTO_ALG_TYPE_SKCIPHER:
655aff388f7SCorentin Labbe 			seq_printf(seq, "%s %s reqs=%lu fallback=%lu\n",
65607e34cd3SHerbert Xu 				   ce_algs[i].alg.skcipher.base.base.cra_driver_name,
65707e34cd3SHerbert Xu 				   ce_algs[i].alg.skcipher.base.base.cra_name,
65806f751b6SCorentin Labbe 				   ce_algs[i].stat_req, ce_algs[i].stat_fb);
659aff388f7SCorentin Labbe 			seq_printf(seq, "\tLast fallback is: %s\n",
660aff388f7SCorentin Labbe 				   ce_algs[i].fbname);
661aff388f7SCorentin Labbe 			seq_printf(seq, "\tFallback due to 0 length: %lu\n",
662aff388f7SCorentin Labbe 				   ce_algs[i].stat_fb_len0);
663aff388f7SCorentin Labbe 			seq_printf(seq, "\tFallback due to length !mod16: %lu\n",
664aff388f7SCorentin Labbe 				   ce_algs[i].stat_fb_mod16);
665aff388f7SCorentin Labbe 			seq_printf(seq, "\tFallback due to length < IV: %lu\n",
666aff388f7SCorentin Labbe 				   ce_algs[i].stat_fb_leniv);
667aff388f7SCorentin Labbe 			seq_printf(seq, "\tFallback due to source alignment: %lu\n",
668aff388f7SCorentin Labbe 				   ce_algs[i].stat_fb_srcali);
669aff388f7SCorentin Labbe 			seq_printf(seq, "\tFallback due to dest alignment: %lu\n",
670aff388f7SCorentin Labbe 				   ce_algs[i].stat_fb_dstali);
671aff388f7SCorentin Labbe 			seq_printf(seq, "\tFallback due to source length: %lu\n",
672aff388f7SCorentin Labbe 				   ce_algs[i].stat_fb_srclen);
673aff388f7SCorentin Labbe 			seq_printf(seq, "\tFallback due to dest length: %lu\n",
674aff388f7SCorentin Labbe 				   ce_algs[i].stat_fb_dstlen);
675aff388f7SCorentin Labbe 			seq_printf(seq, "\tFallback due to SG numbers: %lu\n",
676aff388f7SCorentin Labbe 				   ce_algs[i].stat_fb_maxsg);
67706f751b6SCorentin Labbe 			break;
67856f6d5aeSCorentin Labbe 		case CRYPTO_ALG_TYPE_AHASH:
679aff388f7SCorentin Labbe 			seq_printf(seq, "%s %s reqs=%lu fallback=%lu\n",
68007e34cd3SHerbert Xu 				   ce_algs[i].alg.hash.base.halg.base.cra_driver_name,
68107e34cd3SHerbert Xu 				   ce_algs[i].alg.hash.base.halg.base.cra_name,
68256f6d5aeSCorentin Labbe 				   ce_algs[i].stat_req, ce_algs[i].stat_fb);
683aff388f7SCorentin Labbe 			seq_printf(seq, "\tLast fallback is: %s\n",
684aff388f7SCorentin Labbe 				   ce_algs[i].fbname);
685aff388f7SCorentin Labbe 			seq_printf(seq, "\tFallback due to 0 length: %lu\n",
686aff388f7SCorentin Labbe 				   ce_algs[i].stat_fb_len0);
687aff388f7SCorentin Labbe 			seq_printf(seq, "\tFallback due to length: %lu\n",
688aff388f7SCorentin Labbe 				   ce_algs[i].stat_fb_srclen);
689aff388f7SCorentin Labbe 			seq_printf(seq, "\tFallback due to alignment: %lu\n",
690aff388f7SCorentin Labbe 				   ce_algs[i].stat_fb_srcali);
691aff388f7SCorentin Labbe 			seq_printf(seq, "\tFallback due to SG numbers: %lu\n",
692aff388f7SCorentin Labbe 				   ce_algs[i].stat_fb_maxsg);
69356f6d5aeSCorentin Labbe 			break;
6945eb7e946SCorentin Labbe 		case CRYPTO_ALG_TYPE_RNG:
695aff388f7SCorentin Labbe 			seq_printf(seq, "%s %s reqs=%lu bytes=%lu\n",
6965eb7e946SCorentin Labbe 				   ce_algs[i].alg.rng.base.cra_driver_name,
6975eb7e946SCorentin Labbe 				   ce_algs[i].alg.rng.base.cra_name,
6985eb7e946SCorentin Labbe 				   ce_algs[i].stat_req, ce_algs[i].stat_bytes);
6995eb7e946SCorentin Labbe 			break;
70006f751b6SCorentin Labbe 		}
70106f751b6SCorentin Labbe 	}
70207e34cd3SHerbert Xu #if defined(CONFIG_CRYPTO_DEV_SUN8I_CE_TRNG) && \
70307e34cd3SHerbert Xu     defined(CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG)
7044a07eab3SCorentin Labbe 	seq_printf(seq, "HWRNG %lu %lu\n",
7054a07eab3SCorentin Labbe 		   ce->hwrng_stat_req, ce->hwrng_stat_bytes);
7064a07eab3SCorentin Labbe #endif
70706f751b6SCorentin Labbe 	return 0;
70806f751b6SCorentin Labbe }
70906f751b6SCorentin Labbe 
710560daf9eSQinglang Miao DEFINE_SHOW_ATTRIBUTE(sun8i_ce_debugfs);
71106f751b6SCorentin Labbe 
sun8i_ce_free_chanlist(struct sun8i_ce_dev * ce,int i)71206f751b6SCorentin Labbe static void sun8i_ce_free_chanlist(struct sun8i_ce_dev *ce, int i)
71306f751b6SCorentin Labbe {
71406f751b6SCorentin Labbe 	while (i >= 0) {
71506f751b6SCorentin Labbe 		crypto_engine_exit(ce->chanlist[i].engine);
71606f751b6SCorentin Labbe 		if (ce->chanlist[i].tl)
71706f751b6SCorentin Labbe 			dma_free_coherent(ce->dev, sizeof(struct ce_task),
71806f751b6SCorentin Labbe 					  ce->chanlist[i].tl,
71906f751b6SCorentin Labbe 					  ce->chanlist[i].t_phy);
72006f751b6SCorentin Labbe 		i--;
72106f751b6SCorentin Labbe 	}
72206f751b6SCorentin Labbe }
72306f751b6SCorentin Labbe 
72406f751b6SCorentin Labbe /*
72506f751b6SCorentin Labbe  * Allocate the channel list structure
72606f751b6SCorentin Labbe  */
sun8i_ce_allocate_chanlist(struct sun8i_ce_dev * ce)72706f751b6SCorentin Labbe static int sun8i_ce_allocate_chanlist(struct sun8i_ce_dev *ce)
72806f751b6SCorentin Labbe {
72906f751b6SCorentin Labbe 	int i, err;
73006f751b6SCorentin Labbe 
73106f751b6SCorentin Labbe 	ce->chanlist = devm_kcalloc(ce->dev, MAXFLOW,
73206f751b6SCorentin Labbe 				    sizeof(struct sun8i_ce_flow), GFP_KERNEL);
73306f751b6SCorentin Labbe 	if (!ce->chanlist)
73406f751b6SCorentin Labbe 		return -ENOMEM;
73506f751b6SCorentin Labbe 
73606f751b6SCorentin Labbe 	for (i = 0; i < MAXFLOW; i++) {
73706f751b6SCorentin Labbe 		init_completion(&ce->chanlist[i].complete);
73806f751b6SCorentin Labbe 
73906f751b6SCorentin Labbe 		ce->chanlist[i].engine = crypto_engine_alloc_init(ce->dev, true);
74006f751b6SCorentin Labbe 		if (!ce->chanlist[i].engine) {
74106f751b6SCorentin Labbe 			dev_err(ce->dev, "Cannot allocate engine\n");
74206f751b6SCorentin Labbe 			i--;
74306f751b6SCorentin Labbe 			err = -ENOMEM;
74406f751b6SCorentin Labbe 			goto error_engine;
74506f751b6SCorentin Labbe 		}
74606f751b6SCorentin Labbe 		err = crypto_engine_start(ce->chanlist[i].engine);
74706f751b6SCorentin Labbe 		if (err) {
74806f751b6SCorentin Labbe 			dev_err(ce->dev, "Cannot start engine\n");
74906f751b6SCorentin Labbe 			goto error_engine;
75006f751b6SCorentin Labbe 		}
75106f751b6SCorentin Labbe 		ce->chanlist[i].tl = dma_alloc_coherent(ce->dev,
75206f751b6SCorentin Labbe 							sizeof(struct ce_task),
75306f751b6SCorentin Labbe 							&ce->chanlist[i].t_phy,
75406f751b6SCorentin Labbe 							GFP_KERNEL);
75506f751b6SCorentin Labbe 		if (!ce->chanlist[i].tl) {
75606f751b6SCorentin Labbe 			dev_err(ce->dev, "Cannot get DMA memory for task %d\n",
75706f751b6SCorentin Labbe 				i);
75806f751b6SCorentin Labbe 			err = -ENOMEM;
75906f751b6SCorentin Labbe 			goto error_engine;
76006f751b6SCorentin Labbe 		}
76122f7c2f8SCorentin Labbe 		ce->chanlist[i].bounce_iv = devm_kmalloc(ce->dev, AES_BLOCK_SIZE,
76222f7c2f8SCorentin Labbe 							 GFP_KERNEL | GFP_DMA);
76322f7c2f8SCorentin Labbe 		if (!ce->chanlist[i].bounce_iv) {
76422f7c2f8SCorentin Labbe 			err = -ENOMEM;
76522f7c2f8SCorentin Labbe 			goto error_engine;
76622f7c2f8SCorentin Labbe 		}
76722f7c2f8SCorentin Labbe 		ce->chanlist[i].backup_iv = devm_kmalloc(ce->dev, AES_BLOCK_SIZE,
76822f7c2f8SCorentin Labbe 							 GFP_KERNEL);
76922f7c2f8SCorentin Labbe 		if (!ce->chanlist[i].backup_iv) {
77022f7c2f8SCorentin Labbe 			err = -ENOMEM;
77122f7c2f8SCorentin Labbe 			goto error_engine;
77222f7c2f8SCorentin Labbe 		}
77306f751b6SCorentin Labbe 	}
77406f751b6SCorentin Labbe 	return 0;
77506f751b6SCorentin Labbe error_engine:
77606f751b6SCorentin Labbe 	sun8i_ce_free_chanlist(ce, i);
77706f751b6SCorentin Labbe 	return err;
77806f751b6SCorentin Labbe }
77906f751b6SCorentin Labbe 
78006f751b6SCorentin Labbe /*
78106f751b6SCorentin Labbe  * Power management strategy: The device is suspended unless a TFM exists for
78206f751b6SCorentin Labbe  * one of the algorithms proposed by this driver.
78306f751b6SCorentin Labbe  */
sun8i_ce_pm_suspend(struct device * dev)78406f751b6SCorentin Labbe static int sun8i_ce_pm_suspend(struct device *dev)
78506f751b6SCorentin Labbe {
78606f751b6SCorentin Labbe 	struct sun8i_ce_dev *ce = dev_get_drvdata(dev);
78706f751b6SCorentin Labbe 	int i;
78806f751b6SCorentin Labbe 
78906f751b6SCorentin Labbe 	reset_control_assert(ce->reset);
79006f751b6SCorentin Labbe 	for (i = 0; i < CE_MAX_CLOCKS; i++)
79106f751b6SCorentin Labbe 		clk_disable_unprepare(ce->ceclks[i]);
79206f751b6SCorentin Labbe 	return 0;
79306f751b6SCorentin Labbe }
79406f751b6SCorentin Labbe 
sun8i_ce_pm_resume(struct device * dev)79506f751b6SCorentin Labbe static int sun8i_ce_pm_resume(struct device *dev)
79606f751b6SCorentin Labbe {
79706f751b6SCorentin Labbe 	struct sun8i_ce_dev *ce = dev_get_drvdata(dev);
79806f751b6SCorentin Labbe 	int err, i;
79906f751b6SCorentin Labbe 
80006f751b6SCorentin Labbe 	for (i = 0; i < CE_MAX_CLOCKS; i++) {
80106f751b6SCorentin Labbe 		if (!ce->variant->ce_clks[i].name)
80206f751b6SCorentin Labbe 			continue;
80306f751b6SCorentin Labbe 		err = clk_prepare_enable(ce->ceclks[i]);
80406f751b6SCorentin Labbe 		if (err) {
80506f751b6SCorentin Labbe 			dev_err(ce->dev, "Cannot prepare_enable %s\n",
80606f751b6SCorentin Labbe 				ce->variant->ce_clks[i].name);
80706f751b6SCorentin Labbe 			goto error;
80806f751b6SCorentin Labbe 		}
80906f751b6SCorentin Labbe 	}
81006f751b6SCorentin Labbe 	err = reset_control_deassert(ce->reset);
81106f751b6SCorentin Labbe 	if (err) {
81206f751b6SCorentin Labbe 		dev_err(ce->dev, "Cannot deassert reset control\n");
81306f751b6SCorentin Labbe 		goto error;
81406f751b6SCorentin Labbe 	}
81506f751b6SCorentin Labbe 	return 0;
81606f751b6SCorentin Labbe error:
81706f751b6SCorentin Labbe 	sun8i_ce_pm_suspend(dev);
81806f751b6SCorentin Labbe 	return err;
81906f751b6SCorentin Labbe }
82006f751b6SCorentin Labbe 
82106f751b6SCorentin Labbe static const struct dev_pm_ops sun8i_ce_pm_ops = {
82206f751b6SCorentin Labbe 	SET_RUNTIME_PM_OPS(sun8i_ce_pm_suspend, sun8i_ce_pm_resume, NULL)
82306f751b6SCorentin Labbe };
82406f751b6SCorentin Labbe 
sun8i_ce_pm_init(struct sun8i_ce_dev * ce)82506f751b6SCorentin Labbe static int sun8i_ce_pm_init(struct sun8i_ce_dev *ce)
82606f751b6SCorentin Labbe {
82706f751b6SCorentin Labbe 	int err;
82806f751b6SCorentin Labbe 
82906f751b6SCorentin Labbe 	pm_runtime_use_autosuspend(ce->dev);
83006f751b6SCorentin Labbe 	pm_runtime_set_autosuspend_delay(ce->dev, 2000);
83106f751b6SCorentin Labbe 
83206f751b6SCorentin Labbe 	err = pm_runtime_set_suspended(ce->dev);
83306f751b6SCorentin Labbe 	if (err)
83406f751b6SCorentin Labbe 		return err;
83506f751b6SCorentin Labbe 	pm_runtime_enable(ce->dev);
83606f751b6SCorentin Labbe 	return err;
83706f751b6SCorentin Labbe }
83806f751b6SCorentin Labbe 
sun8i_ce_pm_exit(struct sun8i_ce_dev * ce)83906f751b6SCorentin Labbe static void sun8i_ce_pm_exit(struct sun8i_ce_dev *ce)
84006f751b6SCorentin Labbe {
84106f751b6SCorentin Labbe 	pm_runtime_disable(ce->dev);
84206f751b6SCorentin Labbe }
84306f751b6SCorentin Labbe 
sun8i_ce_get_clks(struct sun8i_ce_dev * ce)84406f751b6SCorentin Labbe static int sun8i_ce_get_clks(struct sun8i_ce_dev *ce)
84506f751b6SCorentin Labbe {
84606f751b6SCorentin Labbe 	unsigned long cr;
84706f751b6SCorentin Labbe 	int err, i;
84806f751b6SCorentin Labbe 
84906f751b6SCorentin Labbe 	for (i = 0; i < CE_MAX_CLOCKS; i++) {
85006f751b6SCorentin Labbe 		if (!ce->variant->ce_clks[i].name)
85106f751b6SCorentin Labbe 			continue;
85206f751b6SCorentin Labbe 		ce->ceclks[i] = devm_clk_get(ce->dev, ce->variant->ce_clks[i].name);
85306f751b6SCorentin Labbe 		if (IS_ERR(ce->ceclks[i])) {
85406f751b6SCorentin Labbe 			err = PTR_ERR(ce->ceclks[i]);
85506f751b6SCorentin Labbe 			dev_err(ce->dev, "Cannot get %s CE clock err=%d\n",
85606f751b6SCorentin Labbe 				ce->variant->ce_clks[i].name, err);
85706f751b6SCorentin Labbe 			return err;
85806f751b6SCorentin Labbe 		}
85906f751b6SCorentin Labbe 		cr = clk_get_rate(ce->ceclks[i]);
86006f751b6SCorentin Labbe 		if (!cr)
86106f751b6SCorentin Labbe 			return -EINVAL;
86206f751b6SCorentin Labbe 		if (ce->variant->ce_clks[i].freq > 0 &&
86306f751b6SCorentin Labbe 		    cr != ce->variant->ce_clks[i].freq) {
86406f751b6SCorentin Labbe 			dev_info(ce->dev, "Set %s clock to %lu (%lu Mhz) from %lu (%lu Mhz)\n",
86506f751b6SCorentin Labbe 				 ce->variant->ce_clks[i].name,
86606f751b6SCorentin Labbe 				 ce->variant->ce_clks[i].freq,
86706f751b6SCorentin Labbe 				 ce->variant->ce_clks[i].freq / 1000000,
86806f751b6SCorentin Labbe 				 cr, cr / 1000000);
86906f751b6SCorentin Labbe 			err = clk_set_rate(ce->ceclks[i], ce->variant->ce_clks[i].freq);
87006f751b6SCorentin Labbe 			if (err)
87106f751b6SCorentin Labbe 				dev_err(ce->dev, "Fail to set %s clk speed to %lu hz\n",
87206f751b6SCorentin Labbe 					ce->variant->ce_clks[i].name,
87306f751b6SCorentin Labbe 					ce->variant->ce_clks[i].freq);
87406f751b6SCorentin Labbe 		}
87506f751b6SCorentin Labbe 		if (ce->variant->ce_clks[i].max_freq > 0 &&
87606f751b6SCorentin Labbe 		    cr > ce->variant->ce_clks[i].max_freq)
8774509f437SColin Ian King 			dev_warn(ce->dev, "Frequency for %s (%lu hz) is higher than datasheet's recommendation (%lu hz)",
87806f751b6SCorentin Labbe 				 ce->variant->ce_clks[i].name, cr,
87906f751b6SCorentin Labbe 				 ce->variant->ce_clks[i].max_freq);
88006f751b6SCorentin Labbe 	}
88106f751b6SCorentin Labbe 	return 0;
88206f751b6SCorentin Labbe }
88306f751b6SCorentin Labbe 
sun8i_ce_register_algs(struct sun8i_ce_dev * ce)88406f751b6SCorentin Labbe static int sun8i_ce_register_algs(struct sun8i_ce_dev *ce)
88506f751b6SCorentin Labbe {
88625d85ffcSCorentin Labbe 	int ce_method, err, id;
88725d85ffcSCorentin Labbe 	unsigned int i;
88806f751b6SCorentin Labbe 
88906f751b6SCorentin Labbe 	for (i = 0; i < ARRAY_SIZE(ce_algs); i++) {
89006f751b6SCorentin Labbe 		ce_algs[i].ce = ce;
89106f751b6SCorentin Labbe 		switch (ce_algs[i].type) {
89206f751b6SCorentin Labbe 		case CRYPTO_ALG_TYPE_SKCIPHER:
89306f751b6SCorentin Labbe 			id = ce_algs[i].ce_algo_id;
89406f751b6SCorentin Labbe 			ce_method = ce->variant->alg_cipher[id];
89506f751b6SCorentin Labbe 			if (ce_method == CE_ID_NOTSUPP) {
89606f751b6SCorentin Labbe 				dev_dbg(ce->dev,
89706f751b6SCorentin Labbe 					"DEBUG: Algo of %s not supported\n",
89807e34cd3SHerbert Xu 					ce_algs[i].alg.skcipher.base.base.cra_name);
89906f751b6SCorentin Labbe 				ce_algs[i].ce = NULL;
90006f751b6SCorentin Labbe 				break;
90106f751b6SCorentin Labbe 			}
90206f751b6SCorentin Labbe 			id = ce_algs[i].ce_blockmode;
90306f751b6SCorentin Labbe 			ce_method = ce->variant->op_mode[id];
90406f751b6SCorentin Labbe 			if (ce_method == CE_ID_NOTSUPP) {
90506f751b6SCorentin Labbe 				dev_dbg(ce->dev, "DEBUG: Blockmode of %s not supported\n",
90607e34cd3SHerbert Xu 					ce_algs[i].alg.skcipher.base.base.cra_name);
90706f751b6SCorentin Labbe 				ce_algs[i].ce = NULL;
90806f751b6SCorentin Labbe 				break;
90906f751b6SCorentin Labbe 			}
91006f751b6SCorentin Labbe 			dev_info(ce->dev, "Register %s\n",
91107e34cd3SHerbert Xu 				 ce_algs[i].alg.skcipher.base.base.cra_name);
91207e34cd3SHerbert Xu 			err = crypto_engine_register_skcipher(&ce_algs[i].alg.skcipher);
91306f751b6SCorentin Labbe 			if (err) {
91406f751b6SCorentin Labbe 				dev_err(ce->dev, "ERROR: Fail to register %s\n",
91507e34cd3SHerbert Xu 					ce_algs[i].alg.skcipher.base.base.cra_name);
91606f751b6SCorentin Labbe 				ce_algs[i].ce = NULL;
91706f751b6SCorentin Labbe 				return err;
91806f751b6SCorentin Labbe 			}
91906f751b6SCorentin Labbe 			break;
92056f6d5aeSCorentin Labbe 		case CRYPTO_ALG_TYPE_AHASH:
92156f6d5aeSCorentin Labbe 			id = ce_algs[i].ce_algo_id;
92256f6d5aeSCorentin Labbe 			ce_method = ce->variant->alg_hash[id];
92356f6d5aeSCorentin Labbe 			if (ce_method == CE_ID_NOTSUPP) {
92456f6d5aeSCorentin Labbe 				dev_info(ce->dev,
92556f6d5aeSCorentin Labbe 					 "DEBUG: Algo of %s not supported\n",
92607e34cd3SHerbert Xu 					 ce_algs[i].alg.hash.base.halg.base.cra_name);
92756f6d5aeSCorentin Labbe 				ce_algs[i].ce = NULL;
92856f6d5aeSCorentin Labbe 				break;
92956f6d5aeSCorentin Labbe 			}
93056f6d5aeSCorentin Labbe 			dev_info(ce->dev, "Register %s\n",
93107e34cd3SHerbert Xu 				 ce_algs[i].alg.hash.base.halg.base.cra_name);
93207e34cd3SHerbert Xu 			err = crypto_engine_register_ahash(&ce_algs[i].alg.hash);
93356f6d5aeSCorentin Labbe 			if (err) {
93456f6d5aeSCorentin Labbe 				dev_err(ce->dev, "ERROR: Fail to register %s\n",
93507e34cd3SHerbert Xu 					ce_algs[i].alg.hash.base.halg.base.cra_name);
93656f6d5aeSCorentin Labbe 				ce_algs[i].ce = NULL;
93756f6d5aeSCorentin Labbe 				return err;
93856f6d5aeSCorentin Labbe 			}
93956f6d5aeSCorentin Labbe 			break;
9405eb7e946SCorentin Labbe 		case CRYPTO_ALG_TYPE_RNG:
9415eb7e946SCorentin Labbe 			if (ce->variant->prng == CE_ID_NOTSUPP) {
9425eb7e946SCorentin Labbe 				dev_info(ce->dev,
9435eb7e946SCorentin Labbe 					 "DEBUG: Algo of %s not supported\n",
9445eb7e946SCorentin Labbe 					 ce_algs[i].alg.rng.base.cra_name);
9455eb7e946SCorentin Labbe 				ce_algs[i].ce = NULL;
9465eb7e946SCorentin Labbe 				break;
9475eb7e946SCorentin Labbe 			}
9485eb7e946SCorentin Labbe 			dev_info(ce->dev, "Register %s\n",
9495eb7e946SCorentin Labbe 				 ce_algs[i].alg.rng.base.cra_name);
9505eb7e946SCorentin Labbe 			err = crypto_register_rng(&ce_algs[i].alg.rng);
9515eb7e946SCorentin Labbe 			if (err) {
9525eb7e946SCorentin Labbe 				dev_err(ce->dev, "Fail to register %s\n",
9535eb7e946SCorentin Labbe 					ce_algs[i].alg.rng.base.cra_name);
9545eb7e946SCorentin Labbe 				ce_algs[i].ce = NULL;
9555eb7e946SCorentin Labbe 			}
9565eb7e946SCorentin Labbe 			break;
95706f751b6SCorentin Labbe 		default:
95806f751b6SCorentin Labbe 			ce_algs[i].ce = NULL;
9594509f437SColin Ian King 			dev_err(ce->dev, "ERROR: tried to register an unknown algo\n");
96006f751b6SCorentin Labbe 		}
96106f751b6SCorentin Labbe 	}
96206f751b6SCorentin Labbe 	return 0;
96306f751b6SCorentin Labbe }
96406f751b6SCorentin Labbe 
sun8i_ce_unregister_algs(struct sun8i_ce_dev * ce)96506f751b6SCorentin Labbe static void sun8i_ce_unregister_algs(struct sun8i_ce_dev *ce)
96606f751b6SCorentin Labbe {
96725d85ffcSCorentin Labbe 	unsigned int i;
96806f751b6SCorentin Labbe 
96906f751b6SCorentin Labbe 	for (i = 0; i < ARRAY_SIZE(ce_algs); i++) {
97006f751b6SCorentin Labbe 		if (!ce_algs[i].ce)
97106f751b6SCorentin Labbe 			continue;
97206f751b6SCorentin Labbe 		switch (ce_algs[i].type) {
97306f751b6SCorentin Labbe 		case CRYPTO_ALG_TYPE_SKCIPHER:
97406f751b6SCorentin Labbe 			dev_info(ce->dev, "Unregister %d %s\n", i,
97507e34cd3SHerbert Xu 				 ce_algs[i].alg.skcipher.base.base.cra_name);
97607e34cd3SHerbert Xu 			crypto_engine_unregister_skcipher(&ce_algs[i].alg.skcipher);
97706f751b6SCorentin Labbe 			break;
97856f6d5aeSCorentin Labbe 		case CRYPTO_ALG_TYPE_AHASH:
97956f6d5aeSCorentin Labbe 			dev_info(ce->dev, "Unregister %d %s\n", i,
98007e34cd3SHerbert Xu 				 ce_algs[i].alg.hash.base.halg.base.cra_name);
98107e34cd3SHerbert Xu 			crypto_engine_unregister_ahash(&ce_algs[i].alg.hash);
98256f6d5aeSCorentin Labbe 			break;
9835eb7e946SCorentin Labbe 		case CRYPTO_ALG_TYPE_RNG:
9845eb7e946SCorentin Labbe 			dev_info(ce->dev, "Unregister %d %s\n", i,
9855eb7e946SCorentin Labbe 				 ce_algs[i].alg.rng.base.cra_name);
9865eb7e946SCorentin Labbe 			crypto_unregister_rng(&ce_algs[i].alg.rng);
9875eb7e946SCorentin Labbe 			break;
98806f751b6SCorentin Labbe 		}
98906f751b6SCorentin Labbe 	}
99006f751b6SCorentin Labbe }
99106f751b6SCorentin Labbe 
sun8i_ce_probe(struct platform_device * pdev)99206f751b6SCorentin Labbe static int sun8i_ce_probe(struct platform_device *pdev)
99306f751b6SCorentin Labbe {
99406f751b6SCorentin Labbe 	struct sun8i_ce_dev *ce;
99506f751b6SCorentin Labbe 	int err, irq;
99606f751b6SCorentin Labbe 	u32 v;
99706f751b6SCorentin Labbe 
99806f751b6SCorentin Labbe 	ce = devm_kzalloc(&pdev->dev, sizeof(*ce), GFP_KERNEL);
99906f751b6SCorentin Labbe 	if (!ce)
100006f751b6SCorentin Labbe 		return -ENOMEM;
100106f751b6SCorentin Labbe 
100206f751b6SCorentin Labbe 	ce->dev = &pdev->dev;
100306f751b6SCorentin Labbe 	platform_set_drvdata(pdev, ce);
100406f751b6SCorentin Labbe 
100506f751b6SCorentin Labbe 	ce->variant = of_device_get_match_data(&pdev->dev);
100606f751b6SCorentin Labbe 	if (!ce->variant) {
100706f751b6SCorentin Labbe 		dev_err(&pdev->dev, "Missing Crypto Engine variant\n");
100806f751b6SCorentin Labbe 		return -EINVAL;
100906f751b6SCorentin Labbe 	}
101006f751b6SCorentin Labbe 
1011eb5b9154SChen Zhou 	ce->base = devm_platform_ioremap_resource(pdev, 0);
101206f751b6SCorentin Labbe 	if (IS_ERR(ce->base))
101306f751b6SCorentin Labbe 		return PTR_ERR(ce->base);
101406f751b6SCorentin Labbe 
101506f751b6SCorentin Labbe 	err = sun8i_ce_get_clks(ce);
101606f751b6SCorentin Labbe 	if (err)
101706f751b6SCorentin Labbe 		return err;
101806f751b6SCorentin Labbe 
101906f751b6SCorentin Labbe 	/* Get Non Secure IRQ */
102006f751b6SCorentin Labbe 	irq = platform_get_irq(pdev, 0);
10219ce9a5d5SChen Zhou 	if (irq < 0)
102206f751b6SCorentin Labbe 		return irq;
102306f751b6SCorentin Labbe 
102406f751b6SCorentin Labbe 	ce->reset = devm_reset_control_get(&pdev->dev, NULL);
1025e61a2356SKrzysztof Kozlowski 	if (IS_ERR(ce->reset))
1026e61a2356SKrzysztof Kozlowski 		return dev_err_probe(&pdev->dev, PTR_ERR(ce->reset),
1027e61a2356SKrzysztof Kozlowski 				     "No reset control found\n");
102806f751b6SCorentin Labbe 
102906f751b6SCorentin Labbe 	mutex_init(&ce->mlock);
10305eb7e946SCorentin Labbe 	mutex_init(&ce->rnglock);
103106f751b6SCorentin Labbe 
103206f751b6SCorentin Labbe 	err = sun8i_ce_allocate_chanlist(ce);
103306f751b6SCorentin Labbe 	if (err)
103406f751b6SCorentin Labbe 		return err;
103506f751b6SCorentin Labbe 
103606f751b6SCorentin Labbe 	err = sun8i_ce_pm_init(ce);
103706f751b6SCorentin Labbe 	if (err)
103806f751b6SCorentin Labbe 		goto error_pm;
103906f751b6SCorentin Labbe 
104006f751b6SCorentin Labbe 	err = devm_request_irq(&pdev->dev, irq, ce_irq_handler, 0,
104106f751b6SCorentin Labbe 			       "sun8i-ce-ns", ce);
104206f751b6SCorentin Labbe 	if (err) {
104306f751b6SCorentin Labbe 		dev_err(ce->dev, "Cannot request CryptoEngine Non-secure IRQ (err=%d)\n", err);
104406f751b6SCorentin Labbe 		goto error_irq;
104506f751b6SCorentin Labbe 	}
104606f751b6SCorentin Labbe 
104706f751b6SCorentin Labbe 	err = sun8i_ce_register_algs(ce);
104806f751b6SCorentin Labbe 	if (err)
104906f751b6SCorentin Labbe 		goto error_alg;
105006f751b6SCorentin Labbe 
1051cc987ae9SShixin Liu 	err = pm_runtime_resume_and_get(ce->dev);
105206f751b6SCorentin Labbe 	if (err < 0)
105306f751b6SCorentin Labbe 		goto error_alg;
105406f751b6SCorentin Labbe 
10554a07eab3SCorentin Labbe #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_TRNG
10564a07eab3SCorentin Labbe 	sun8i_ce_hwrng_register(ce);
10574a07eab3SCorentin Labbe #endif
10584a07eab3SCorentin Labbe 
105906f751b6SCorentin Labbe 	v = readl(ce->base + CE_CTR);
106006f751b6SCorentin Labbe 	v >>= CE_DIE_ID_SHIFT;
106106f751b6SCorentin Labbe 	v &= CE_DIE_ID_MASK;
106206f751b6SCorentin Labbe 	dev_info(&pdev->dev, "CryptoEngine Die ID %x\n", v);
106306f751b6SCorentin Labbe 
106406f751b6SCorentin Labbe 	pm_runtime_put_sync(ce->dev);
106506f751b6SCorentin Labbe 
106607e34cd3SHerbert Xu 	if (IS_ENABLED(CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG)) {
106707e34cd3SHerbert Xu 		struct dentry *dbgfs_dir __maybe_unused;
106807e34cd3SHerbert Xu 		struct dentry *dbgfs_stats __maybe_unused;
106907e34cd3SHerbert Xu 
107006f751b6SCorentin Labbe 		/* Ignore error of debugfs */
107107e34cd3SHerbert Xu 		dbgfs_dir = debugfs_create_dir("sun8i-ce", NULL);
107207e34cd3SHerbert Xu 		dbgfs_stats = debugfs_create_file("stats", 0444,
107307e34cd3SHerbert Xu 						  dbgfs_dir, ce,
107406f751b6SCorentin Labbe 						  &sun8i_ce_debugfs_fops);
107507e34cd3SHerbert Xu 
107607e34cd3SHerbert Xu #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
107707e34cd3SHerbert Xu 		ce->dbgfs_dir = dbgfs_dir;
107807e34cd3SHerbert Xu 		ce->dbgfs_stats = dbgfs_stats;
107906f751b6SCorentin Labbe #endif
108007e34cd3SHerbert Xu 	}
108106f751b6SCorentin Labbe 
108206f751b6SCorentin Labbe 	return 0;
108306f751b6SCorentin Labbe error_alg:
108406f751b6SCorentin Labbe 	sun8i_ce_unregister_algs(ce);
108506f751b6SCorentin Labbe error_irq:
108606f751b6SCorentin Labbe 	sun8i_ce_pm_exit(ce);
108706f751b6SCorentin Labbe error_pm:
108893d24ac4SCorentin Labbe 	sun8i_ce_free_chanlist(ce, MAXFLOW - 1);
108906f751b6SCorentin Labbe 	return err;
109006f751b6SCorentin Labbe }
109106f751b6SCorentin Labbe 
sun8i_ce_remove(struct platform_device * pdev)109257e5d4deSUwe Kleine-König static void sun8i_ce_remove(struct platform_device *pdev)
109306f751b6SCorentin Labbe {
109406f751b6SCorentin Labbe 	struct sun8i_ce_dev *ce = platform_get_drvdata(pdev);
109506f751b6SCorentin Labbe 
10964a07eab3SCorentin Labbe #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_TRNG
10974a07eab3SCorentin Labbe 	sun8i_ce_hwrng_unregister(ce);
10984a07eab3SCorentin Labbe #endif
10994a07eab3SCorentin Labbe 
110006f751b6SCorentin Labbe 	sun8i_ce_unregister_algs(ce);
110106f751b6SCorentin Labbe 
110206f751b6SCorentin Labbe #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
110306f751b6SCorentin Labbe 	debugfs_remove_recursive(ce->dbgfs_dir);
110406f751b6SCorentin Labbe #endif
110506f751b6SCorentin Labbe 
110693d24ac4SCorentin Labbe 	sun8i_ce_free_chanlist(ce, MAXFLOW - 1);
110706f751b6SCorentin Labbe 
110806f751b6SCorentin Labbe 	sun8i_ce_pm_exit(ce);
110906f751b6SCorentin Labbe }
111006f751b6SCorentin Labbe 
111106f751b6SCorentin Labbe static const struct of_device_id sun8i_ce_crypto_of_match_table[] = {
111206f751b6SCorentin Labbe 	{ .compatible = "allwinner,sun8i-h3-crypto",
111306f751b6SCorentin Labbe 	  .data = &ce_h3_variant },
111406f751b6SCorentin Labbe 	{ .compatible = "allwinner,sun8i-r40-crypto",
111506f751b6SCorentin Labbe 	  .data = &ce_r40_variant },
111683f50f29SCorentin Labbe 	{ .compatible = "allwinner,sun20i-d1-crypto",
111783f50f29SCorentin Labbe 	  .data = &ce_d1_variant },
111806f751b6SCorentin Labbe 	{ .compatible = "allwinner,sun50i-a64-crypto",
111906f751b6SCorentin Labbe 	  .data = &ce_a64_variant },
112006f751b6SCorentin Labbe 	{ .compatible = "allwinner,sun50i-h5-crypto",
112106f751b6SCorentin Labbe 	  .data = &ce_h5_variant },
112206f751b6SCorentin Labbe 	{ .compatible = "allwinner,sun50i-h6-crypto",
112306f751b6SCorentin Labbe 	  .data = &ce_h6_variant },
1124*1611f749SAndre Przywara 	{ .compatible = "allwinner,sun50i-h616-crypto",
1125*1611f749SAndre Przywara 	  .data = &ce_h616_variant },
112606f751b6SCorentin Labbe 	{}
112706f751b6SCorentin Labbe };
112806f751b6SCorentin Labbe MODULE_DEVICE_TABLE(of, sun8i_ce_crypto_of_match_table);
112906f751b6SCorentin Labbe 
113006f751b6SCorentin Labbe static struct platform_driver sun8i_ce_driver = {
113106f751b6SCorentin Labbe 	.probe		 = sun8i_ce_probe,
113257e5d4deSUwe Kleine-König 	.remove_new	 = sun8i_ce_remove,
113306f751b6SCorentin Labbe 	.driver		 = {
113406f751b6SCorentin Labbe 		.name		= "sun8i-ce",
113506f751b6SCorentin Labbe 		.pm		= &sun8i_ce_pm_ops,
113606f751b6SCorentin Labbe 		.of_match_table	= sun8i_ce_crypto_of_match_table,
113706f751b6SCorentin Labbe 	},
113806f751b6SCorentin Labbe };
113906f751b6SCorentin Labbe 
114006f751b6SCorentin Labbe module_platform_driver(sun8i_ce_driver);
114106f751b6SCorentin Labbe 
114206f751b6SCorentin Labbe MODULE_DESCRIPTION("Allwinner Crypto Engine cryptographic offloader");
114306f751b6SCorentin Labbe MODULE_LICENSE("GPL");
114406f751b6SCorentin Labbe MODULE_AUTHOR("Corentin Labbe <clabbe.montjoie@gmail.com>");
1145