xref: /openbmc/linux/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-prng.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1ac2614d7SCorentin Labbe // SPDX-License-Identifier: GPL-2.0
2ac2614d7SCorentin Labbe /*
3ac2614d7SCorentin Labbe  * sun8i-ss-prng.c - hardware cryptographic offloader for
4ac2614d7SCorentin Labbe  * Allwinner A80/A83T SoC
5ac2614d7SCorentin Labbe  *
6ac2614d7SCorentin Labbe  * Copyright (C) 2015-2020 Corentin Labbe <clabbe@baylibre.com>
7ac2614d7SCorentin Labbe  *
8ac2614d7SCorentin Labbe  * This file handle the PRNG found in the SS
9ac2614d7SCorentin Labbe  *
10*39db3f15SJonathan Corbet  * You could find a link for the datasheet in Documentation/arch/arm/sunxi.rst
11ac2614d7SCorentin Labbe  */
12ac2614d7SCorentin Labbe #include "sun8i-ss.h"
13ac2614d7SCorentin Labbe #include <linux/dma-mapping.h>
1439a76cf1SHerbert Xu #include <linux/kernel.h>
1539a76cf1SHerbert Xu #include <linux/mm.h>
16ac2614d7SCorentin Labbe #include <linux/pm_runtime.h>
17ac2614d7SCorentin Labbe #include <crypto/internal/rng.h>
18ac2614d7SCorentin Labbe 
sun8i_ss_prng_seed(struct crypto_rng * tfm,const u8 * seed,unsigned int slen)19ac2614d7SCorentin Labbe int sun8i_ss_prng_seed(struct crypto_rng *tfm, const u8 *seed,
20ac2614d7SCorentin Labbe 		       unsigned int slen)
21ac2614d7SCorentin Labbe {
22ac2614d7SCorentin Labbe 	struct sun8i_ss_rng_tfm_ctx *ctx = crypto_rng_ctx(tfm);
23ac2614d7SCorentin Labbe 
24ac2614d7SCorentin Labbe 	if (ctx->seed && ctx->slen != slen) {
25192b722fSJason Wang 		kfree_sensitive(ctx->seed);
26ac2614d7SCorentin Labbe 		ctx->slen = 0;
27ac2614d7SCorentin Labbe 		ctx->seed = NULL;
28ac2614d7SCorentin Labbe 	}
29ac2614d7SCorentin Labbe 	if (!ctx->seed)
3039a76cf1SHerbert Xu 		ctx->seed = kmalloc(slen, GFP_KERNEL);
31ac2614d7SCorentin Labbe 	if (!ctx->seed)
32ac2614d7SCorentin Labbe 		return -ENOMEM;
33ac2614d7SCorentin Labbe 
34ac2614d7SCorentin Labbe 	memcpy(ctx->seed, seed, slen);
35ac2614d7SCorentin Labbe 	ctx->slen = slen;
36ac2614d7SCorentin Labbe 
37ac2614d7SCorentin Labbe 	return 0;
38ac2614d7SCorentin Labbe }
39ac2614d7SCorentin Labbe 
sun8i_ss_prng_init(struct crypto_tfm * tfm)40ac2614d7SCorentin Labbe int sun8i_ss_prng_init(struct crypto_tfm *tfm)
41ac2614d7SCorentin Labbe {
42ac2614d7SCorentin Labbe 	struct sun8i_ss_rng_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
43ac2614d7SCorentin Labbe 
44ac2614d7SCorentin Labbe 	memset(ctx, 0, sizeof(struct sun8i_ss_rng_tfm_ctx));
45ac2614d7SCorentin Labbe 	return 0;
46ac2614d7SCorentin Labbe }
47ac2614d7SCorentin Labbe 
sun8i_ss_prng_exit(struct crypto_tfm * tfm)48ac2614d7SCorentin Labbe void sun8i_ss_prng_exit(struct crypto_tfm *tfm)
49ac2614d7SCorentin Labbe {
50ac2614d7SCorentin Labbe 	struct sun8i_ss_rng_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
51ac2614d7SCorentin Labbe 
52192b722fSJason Wang 	kfree_sensitive(ctx->seed);
53ac2614d7SCorentin Labbe 	ctx->seed = NULL;
54ac2614d7SCorentin Labbe 	ctx->slen = 0;
55ac2614d7SCorentin Labbe }
56ac2614d7SCorentin Labbe 
sun8i_ss_prng_generate(struct crypto_rng * tfm,const u8 * src,unsigned int slen,u8 * dst,unsigned int dlen)57ac2614d7SCorentin Labbe int sun8i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,
58ac2614d7SCorentin Labbe 			   unsigned int slen, u8 *dst, unsigned int dlen)
59ac2614d7SCorentin Labbe {
60ac2614d7SCorentin Labbe 	struct sun8i_ss_rng_tfm_ctx *ctx = crypto_rng_ctx(tfm);
61ac2614d7SCorentin Labbe 	struct rng_alg *alg = crypto_rng_alg(tfm);
62ac2614d7SCorentin Labbe 	struct sun8i_ss_alg_template *algt;
6339a76cf1SHerbert Xu 	unsigned int todo_with_padding;
64ac2614d7SCorentin Labbe 	struct sun8i_ss_dev *ss;
65ac2614d7SCorentin Labbe 	dma_addr_t dma_iv, dma_dst;
66ac2614d7SCorentin Labbe 	unsigned int todo;
67ac2614d7SCorentin Labbe 	int err = 0;
68ac2614d7SCorentin Labbe 	int flow;
69ac2614d7SCorentin Labbe 	void *d;
70ac2614d7SCorentin Labbe 	u32 v;
71ac2614d7SCorentin Labbe 
72ac2614d7SCorentin Labbe 	algt = container_of(alg, struct sun8i_ss_alg_template, alg.rng);
73ac2614d7SCorentin Labbe 	ss = algt->ss;
74ac2614d7SCorentin Labbe 
75ac2614d7SCorentin Labbe 	if (ctx->slen == 0) {
76ac2614d7SCorentin Labbe 		dev_err(ss->dev, "The PRNG is not seeded\n");
77ac2614d7SCorentin Labbe 		return -EINVAL;
78ac2614d7SCorentin Labbe 	}
79ac2614d7SCorentin Labbe 
80ac2614d7SCorentin Labbe 	/* The SS does not give an updated seed, so we need to get a new one.
81ac2614d7SCorentin Labbe 	 * So we will ask for an extra PRNG_SEED_SIZE data.
82ac2614d7SCorentin Labbe 	 * We want dlen + seedsize rounded up to a multiple of PRNG_DATA_SIZE
83ac2614d7SCorentin Labbe 	 */
84ac2614d7SCorentin Labbe 	todo = dlen + PRNG_SEED_SIZE + PRNG_DATA_SIZE;
85ac2614d7SCorentin Labbe 	todo -= todo % PRNG_DATA_SIZE;
86ac2614d7SCorentin Labbe 
8739a76cf1SHerbert Xu 	todo_with_padding = ALIGN(todo, dma_get_cache_alignment());
8839a76cf1SHerbert Xu 	if (todo_with_padding < todo || todo < dlen)
8939a76cf1SHerbert Xu 		return -EOVERFLOW;
9039a76cf1SHerbert Xu 
9139a76cf1SHerbert Xu 	d = kzalloc(todo_with_padding, GFP_KERNEL);
92ac2614d7SCorentin Labbe 	if (!d)
93ac2614d7SCorentin Labbe 		return -ENOMEM;
94ac2614d7SCorentin Labbe 
95ac2614d7SCorentin Labbe 	flow = sun8i_ss_get_engine_number(ss);
96ac2614d7SCorentin Labbe 
97ac2614d7SCorentin Labbe #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
98ac2614d7SCorentin Labbe 	algt->stat_req++;
99ac2614d7SCorentin Labbe 	algt->stat_bytes += todo;
100ac2614d7SCorentin Labbe #endif
101ac2614d7SCorentin Labbe 
102ac2614d7SCorentin Labbe 	v = SS_ALG_PRNG | SS_PRNG_CONTINUE | SS_START;
103ac2614d7SCorentin Labbe 	if (flow)
104ac2614d7SCorentin Labbe 		v |= SS_FLOW1;
105ac2614d7SCorentin Labbe 	else
106ac2614d7SCorentin Labbe 		v |= SS_FLOW0;
107ac2614d7SCorentin Labbe 
108ac2614d7SCorentin Labbe 	dma_iv = dma_map_single(ss->dev, ctx->seed, ctx->slen, DMA_TO_DEVICE);
109ac2614d7SCorentin Labbe 	if (dma_mapping_error(ss->dev, dma_iv)) {
110ac2614d7SCorentin Labbe 		dev_err(ss->dev, "Cannot DMA MAP IV\n");
11198b5ef3eSColin Ian King 		err = -EFAULT;
11298b5ef3eSColin Ian King 		goto err_free;
113ac2614d7SCorentin Labbe 	}
114ac2614d7SCorentin Labbe 
115ac2614d7SCorentin Labbe 	dma_dst = dma_map_single(ss->dev, d, todo, DMA_FROM_DEVICE);
116ac2614d7SCorentin Labbe 	if (dma_mapping_error(ss->dev, dma_dst)) {
117ac2614d7SCorentin Labbe 		dev_err(ss->dev, "Cannot DMA MAP DST\n");
118ac2614d7SCorentin Labbe 		err = -EFAULT;
119ac2614d7SCorentin Labbe 		goto err_iv;
120ac2614d7SCorentin Labbe 	}
121ac2614d7SCorentin Labbe 
122ee74fdf0SMinghao Chi 	err = pm_runtime_resume_and_get(ss->dev);
123ee74fdf0SMinghao Chi 	if (err < 0)
124ac2614d7SCorentin Labbe 		goto err_pm;
125ac2614d7SCorentin Labbe 	err = 0;
126ac2614d7SCorentin Labbe 
127ac2614d7SCorentin Labbe 	mutex_lock(&ss->mlock);
128ac2614d7SCorentin Labbe 	writel(dma_iv, ss->base + SS_IV_ADR_REG);
129ac2614d7SCorentin Labbe 	/* the PRNG act badly (failing rngtest) without SS_KEY_ADR_REG set */
130ac2614d7SCorentin Labbe 	writel(dma_iv, ss->base + SS_KEY_ADR_REG);
131ac2614d7SCorentin Labbe 	writel(dma_dst, ss->base + SS_DST_ADR_REG);
132ac2614d7SCorentin Labbe 	writel(todo / 4, ss->base + SS_LEN_ADR_REG);
133ac2614d7SCorentin Labbe 
134ac2614d7SCorentin Labbe 	reinit_completion(&ss->flows[flow].complete);
135ac2614d7SCorentin Labbe 	ss->flows[flow].status = 0;
136ac2614d7SCorentin Labbe 	/* Be sure all data is written before enabling the task */
137ac2614d7SCorentin Labbe 	wmb();
138ac2614d7SCorentin Labbe 
139ac2614d7SCorentin Labbe 	writel(v, ss->base + SS_CTL_REG);
140ac2614d7SCorentin Labbe 
141ac2614d7SCorentin Labbe 	wait_for_completion_interruptible_timeout(&ss->flows[flow].complete,
142ac2614d7SCorentin Labbe 						  msecs_to_jiffies(todo));
143ac2614d7SCorentin Labbe 	if (ss->flows[flow].status == 0) {
144ac2614d7SCorentin Labbe 		dev_err(ss->dev, "DMA timeout for PRNG (size=%u)\n", todo);
145ac2614d7SCorentin Labbe 		err = -EFAULT;
146ac2614d7SCorentin Labbe 	}
147ac2614d7SCorentin Labbe 	/* Since cipher and hash use the linux/cryptoengine and that we have
148ac2614d7SCorentin Labbe 	 * a cryptoengine per flow, we are sure that they will issue only one
149ac2614d7SCorentin Labbe 	 * request per flow.
150ac2614d7SCorentin Labbe 	 * Since the cryptoengine wait for completion before submitting a new
151ac2614d7SCorentin Labbe 	 * one, the mlock could be left just after the final writel.
152ac2614d7SCorentin Labbe 	 * But cryptoengine cannot handle crypto_rng, so we need to be sure
153ac2614d7SCorentin Labbe 	 * nothing will use our flow.
154ac2614d7SCorentin Labbe 	 * The easiest way is to grab mlock until the hardware end our requests.
155ac2614d7SCorentin Labbe 	 * We could have used a per flow lock, but this would increase
156ac2614d7SCorentin Labbe 	 * complexity.
157ac2614d7SCorentin Labbe 	 * The drawback is that no request could be handled for the other flow.
158ac2614d7SCorentin Labbe 	 */
159ac2614d7SCorentin Labbe 	mutex_unlock(&ss->mlock);
160ac2614d7SCorentin Labbe 
161ac2614d7SCorentin Labbe 	pm_runtime_put(ss->dev);
162ac2614d7SCorentin Labbe 
163ac2614d7SCorentin Labbe err_pm:
164ac2614d7SCorentin Labbe 	dma_unmap_single(ss->dev, dma_dst, todo, DMA_FROM_DEVICE);
165ac2614d7SCorentin Labbe err_iv:
166ac2614d7SCorentin Labbe 	dma_unmap_single(ss->dev, dma_iv, ctx->slen, DMA_TO_DEVICE);
167ac2614d7SCorentin Labbe 
168ac2614d7SCorentin Labbe 	if (!err) {
169ac2614d7SCorentin Labbe 		memcpy(dst, d, dlen);
170ac2614d7SCorentin Labbe 		/* Update seed */
171ac2614d7SCorentin Labbe 		memcpy(ctx->seed, d + dlen, ctx->slen);
172ac2614d7SCorentin Labbe 	}
17398b5ef3eSColin Ian King err_free:
174192b722fSJason Wang 	kfree_sensitive(d);
175ac2614d7SCorentin Labbe 
176ac2614d7SCorentin Labbe 	return err;
177ac2614d7SCorentin Labbe }
178