Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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