Skip to contentSkip to conversation

crypto: allwinner: add xRNG and hashes

PATCH Superseded Corentin Labbe 2 participants 24 messages Jun 21, 2020 – Jun 22, 2020

Subsystems (3)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v3 cover crypto: allwinner: add xRNG and hashes

Hello

The main goal of this serie is to add support for TRNG, PRNG and hashes to the sun8i-ss/sun8i-ce.
The whole serie is tested with CRYPTO_EXTRA_TESTS enabled and loading tcrypt.
The PRNG and TRNG are tested with rngtest.

Regards

Change since v1:
- removed _crypto_rng_cast patch

Change since v2:
- cleaned unused variables from sun8i-ce-prng
- added some missing memzero_explicit

Corentin Labbe (14):
crypto: sun8i-ss: Add SS_START define
crypto: sun8i-ss: Add support for the PRNG
crypto: sun8i-ss: support hash algorithms
crypto: sun8i-ss: fix a trivial typo
crypto: sun8i-ss: Add more comment on some structures
crypto: sun8i-ss: better debug printing
crypto: sun8i-ce: move iv data to request context
crypto: sun8i-ce: split into prepare/run/unprepare
crypto: sun8i-ce: handle different error registers
crypto: sun8i-ce: rename has_t_dlen_in_bytes to cipher_t_dlen_in_bytes
crypto: sun8i-ce: support hash algorithms
crypto: sun8i-ce: Add stat_bytes debugfs
crypto: sun8i-ce: Add support for the PRNG
crypto: sun8i-ce: Add support for the TRNG

 drivers/crypto/allwinner/Kconfig              |  43 ++
 drivers/crypto/allwinner/sun8i-ce/Makefile    |   3 +
 .../allwinner/sun8i-ce/sun8i-ce-cipher.c      |  99 +++-
 .../crypto/allwinner/sun8i-ce/sun8i-ce-core.c | 369 ++++++++++++++-
 .../crypto/allwinner/sun8i-ce/sun8i-ce-hash.c | 415 ++++++++++++++++
 .../crypto/allwinner/sun8i-ce/sun8i-ce-prng.c | 161 +++++++
 .../crypto/allwinner/sun8i-ce/sun8i-ce-trng.c | 124 +++++
 drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h  | 135 +++++-
 drivers/crypto/allwinner/sun8i-ss/Makefile    |   2 +
 .../crypto/allwinner/sun8i-ss/sun8i-ss-core.c | 198 +++++++-
 .../crypto/allwinner/sun8i-ss/sun8i-ss-hash.c | 446 ++++++++++++++++++
 .../crypto/allwinner/sun8i-ss/sun8i-ss-prng.c | 170 +++++++
 drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h  |  93 +++-
 13 files changed, 2216 insertions(+), 42 deletions(-)
 create mode 100644 drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c
 create mode 100644 drivers/crypto/allwinner/sun8i-ce/sun8i-ce-prng.c
 create mode 100644 drivers/crypto/allwinner/sun8i-ce/sun8i-ce-trng.c
 create mode 100644 drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
 create mode 100644 drivers/crypto/allwinner/sun8i-ss/sun8i-ss-prng.c
Signature · 1 line
2.26.2
#1 lore raw

PATCH v3 1/14 crypto: sun8i-ss: Add SS_START define

Instead of using an hardcoded value, let's use a defined value for SS_START.

---
 drivers/crypto/allwinner/sun8i-ss/sun8i-ss-core.c | 2 +-
 drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h      | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
drivers/crypto/allwinner/sun8i-ss/sun8i-ss-core.c +1 -1
index 5d9d0fedcb06..81eff935fb5c 100644
@@ -61,7 +61,7 @@ int sun8i_ss_run_task(struct sun8i_ss_dev *ss, struct sun8i_cipher_req_ctx *rctx
 		      const char *name)
 {
 	int flow = rctx->flow;
-	u32 v = 1;
+	u32 v = SS_START;
 	int i;
 
 #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h +2 -0
index 29c44f279112..f7a64033fc03 100644
@@ -13,6 +13,8 @@
 #include <linux/debugfs.h>
 #include <linux/crypto.h>
 
+#define SS_START	1
+
 #define SS_ENCRYPTION		0
 #define SS_DECRYPTION		BIT(6)
 
Signature · 1 line
2.26.2
#2 lore raw

PATCH v3 2/14 crypto: sun8i-ss: Add support for the PRNG

In reply to Corentin Labbe

This patch had support for the PRNG present in the SS. The output was tested with rngtest without any failure.

---
 drivers/crypto/allwinner/Kconfig              |   8 +
 drivers/crypto/allwinner/sun8i-ss/Makefile    |   1 +
 .../crypto/allwinner/sun8i-ss/sun8i-ss-core.c |  39 ++++
 .../crypto/allwinner/sun8i-ss/sun8i-ss-prng.c | 170 ++++++++++++++++++
 drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h  |  25 +++
 5 files changed, 243 insertions(+)
 create mode 100644 drivers/crypto/allwinner/sun8i-ss/sun8i-ss-prng.c
drivers/crypto/allwinner/Kconfig +8 -0
index d315427ea1ba..a0bdb26d0fe4 100644
@@ -95,3 +95,11 @@ config CRYPTO_DEV_SUN8I_SS_DEBUG
 	  Say y to enable sun8i-ss debug stats.
 	  This will create /sys/kernel/debug/sun8i-ss/stats for displaying
 	  the number of requests per flow and per algorithm.
+
+config CRYPTO_DEV_SUN8I_SS_PRNG
+	bool "Support for Allwinner Security System PRNG"
+	depends on CRYPTO_DEV_SUN8I_SS
+	select CRYPTO_RNG
+	help
+	  Select this option if you want to provide kernel-side support for
+	  the Pseudo-Random Number Generator found in the Security System.
drivers/crypto/allwinner/sun8i-ss/Makefile +1 -0
index add7b0543fd5..49f2f912c816 100644
@@ -1,2 +1,3 @@
 obj-$(CONFIG_CRYPTO_DEV_SUN8I_SS) += sun8i-ss.o
 sun8i-ss-y += sun8i-ss-core.o sun8i-ss-cipher.o
+sun8i-ss-$(CONFIG_CRYPTO_DEV_SUN8I_SS_PRNG) += sun8i-ss-prng.o
drivers/crypto/allwinner/sun8i-ss/sun8i-ss-core.c +39 -0
index 81eff935fb5c..592ee35616ba 100644
@@ -22,6 +22,7 @@
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
 #include <linux/reset.h>
+#include <crypto/internal/rng.h>
 #include <crypto/internal/skcipher.h>
 
 #include "sun8i-ss.h"
@@ -260,6 +261,25 @@ static struct sun8i_ss_alg_template ss_algs[] = {
 		.decrypt	= sun8i_ss_skdecrypt,
 	}
 },
+#ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_PRNG
+{
+	.type = CRYPTO_ALG_TYPE_RNG,
+	.alg.rng = {
+		.base = {
+			.cra_name		= "stdrng",
+			.cra_driver_name	= "sun8i-ss-prng",
+			.cra_priority		= 300,
+			.cra_ctxsize = sizeof(struct sun8i_ss_rng_tfm_ctx),
+			.cra_module		= THIS_MODULE,
+			.cra_init		= sun8i_ss_prng_init,
+			.cra_exit		= sun8i_ss_prng_exit,
+		},
+		.generate               = sun8i_ss_prng_generate,
+		.seed                   = sun8i_ss_prng_seed,
+		.seedsize               = PRNG_SEED_SIZE,
+	}
+},
+#endif
 };
 
 #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
@@ -281,6 +301,12 @@ static int sun8i_ss_dbgfs_read(struct seq_file *seq, void *v)
 				   ss_algs[i].alg.skcipher.base.cra_name,
 				   ss_algs[i].stat_req, ss_algs[i].stat_fb);
 			break;
+		case CRYPTO_ALG_TYPE_RNG:
+			seq_printf(seq, "%s %s %lu %lu\n",
+				   ss_algs[i].alg.rng.base.cra_driver_name,
+				   ss_algs[i].alg.rng.base.cra_name,
+				   ss_algs[i].stat_req, ss_algs[i].stat_bytes);
+			break;
 		}
 	}
 	return 0;
@@ -444,6 +470,14 @@ static int sun8i_ss_register_algs(struct sun8i_ss_dev *ss)
 				return err;
 			}
 			break;
+		case CRYPTO_ALG_TYPE_RNG:
+			err = crypto_register_rng(&ss_algs[i].alg.rng);
+			if (err) {
+				dev_err(ss->dev, "Fail to register %s\n",
+					ss_algs[i].alg.rng.base.cra_name);
+				ss_algs[i].ss = NULL;
+			}
+			break;
 		default:
 			ss_algs[i].ss = NULL;
 			dev_err(ss->dev, "ERROR: tried to register an unknown algo\n");
@@ -465,6 +499,11 @@ static void sun8i_ss_unregister_algs(struct sun8i_ss_dev *ss)
 				 ss_algs[i].alg.skcipher.base.cra_name);
 			crypto_unregister_skcipher(&ss_algs[i].alg.skcipher);
 			break;
+		case CRYPTO_ALG_TYPE_RNG:
+			dev_info(ss->dev, "Unregister %d %s\n", i,
+				 ss_algs[i].alg.rng.base.cra_name);
+			crypto_unregister_rng(&ss_algs[i].alg.rng);
+			break;
 		}
 	}
 }
drivers/crypto/allwinner/sun8i-ss/sun8i-ss-prng.c new file +170 -0

170 lines not on this page · raw message

drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h +25 -0
index f7a64033fc03..0223e10495ac 100644
@@ -8,6 +8,7 @@
 #include <crypto/aes.h>
 #include <crypto/des.h>
 #include <crypto/engine.h>
+#include <crypto/rng.h>
 #include <crypto/skcipher.h>
 #include <linux/atomic.h>
 #include <linux/debugfs.h>
@@ -21,6 +22,7 @@
 #define SS_ALG_AES		0
 #define SS_ALG_DES		(1 << 2)
 #define SS_ALG_3DES		(2 << 2)
+#define SS_ALG_PRNG		(4 << 2)
 
 #define SS_CTL_REG		0x00
 #define SS_INT_CTL_REG		0x04
@@ -52,6 +54,8 @@
 #define SS_FLOW0	BIT(30)
 #define SS_FLOW1	BIT(31)
 
+#define SS_PRNG_CONTINUE	BIT(18)
+
 #define MAX_SG 8
 
 #define MAXFLOW 2
@@ -61,6 +65,9 @@
 #define SS_DIE_ID_SHIFT	20
 #define SS_DIE_ID_MASK	0x07
 
+#define PRNG_DATA_SIZE (160 / 8)
+#define PRNG_SEED_SIZE DIV_ROUND_UP(175, 8)
+
 /*
  * struct ss_clock - Describe clocks used by sun8i-ss
  * @name:       Name of clock needed by this variant
@@ -179,6 +186,16 @@ struct sun8i_cipher_tfm_ctx {
 	struct crypto_sync_skcipher *fallback_tfm;
 };
 
+/*
+ * struct sun8i_ss_prng_ctx - context for PRNG TFM
+ * @seed:	The seed to use
+ * @slen:	The size of the seed
+ */
+struct sun8i_ss_rng_tfm_ctx {
+	void *seed;
+	unsigned int slen;
+};
+
 /*
  * struct sun8i_ss_alg_template - crypto_alg template
  * @type:		the CRYPTO_ALG_TYPE for this template
@@ -189,6 +206,7 @@ struct sun8i_cipher_tfm_ctx {
  * @alg:		one of sub struct must be used
  * @stat_req:		number of request done on this template
  * @stat_fb:		number of request which has fallbacked
+ * @stat_bytes:		total data size done by this template
  */
 struct sun8i_ss_alg_template {
 	u32 type;
@@ -197,10 +215,12 @@ struct sun8i_ss_alg_template {
 	struct sun8i_ss_dev *ss;
 	union {
 		struct skcipher_alg skcipher;
+		struct rng_alg rng;
 	} alg;
 #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
 	unsigned long stat_req;
 	unsigned long stat_fb;
+	unsigned long stat_bytes;
 #endif
 };
 
@@ -218,3 +238,8 @@ int sun8i_ss_skencrypt(struct skcipher_request *areq);
 int sun8i_ss_get_engine_number(struct sun8i_ss_dev *ss);
 
 int sun8i_ss_run_task(struct sun8i_ss_dev *ss, struct sun8i_cipher_req_ctx *rctx, const char *name);
+int sun8i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,
+			   unsigned int slen, u8 *dst, unsigned int dlen);
+int sun8i_ss_prng_seed(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);
+int sun8i_ss_prng_init(struct crypto_tfm *tfm);
+void sun8i_ss_prng_exit(struct crypto_tfm *tfm);
Signature · 1 line
2.26.2
#3 lore raw

PATCH v3 3/14 crypto: sun8i-ss: support hash algorithms

In reply to Corentin Labbe

The SS support multiples hash algorithms, this patch adds support for MD5, SHA1, SHA224 and SHA256.

---
 drivers/crypto/allwinner/Kconfig              |   9 +
 drivers/crypto/allwinner/sun8i-ss/Makefile    |   1 +
 .../crypto/allwinner/sun8i-ss/sun8i-ss-core.c | 155 ++++++
 .../crypto/allwinner/sun8i-ss/sun8i-ss-hash.c | 446 ++++++++++++++++++
 drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h  |  60 +++
 5 files changed, 671 insertions(+)
 create mode 100644 drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
drivers/crypto/allwinner/Kconfig +9 -0
index a0bdb26d0fe4..945228b3a8c4 100644
@@ -103,3 +103,12 @@ config CRYPTO_DEV_SUN8I_SS_PRNG
 	help
 	  Select this option if you want to provide kernel-side support for
 	  the Pseudo-Random Number Generator found in the Security System.
+
+config CRYPTO_DEV_SUN8I_SS_HASH
+	bool "Enable support for hash on sun8i-ss"
+	depends on CRYPTO_DEV_SUN8I_SS
+	select MD5
+	select SHA1
+	select SHA256
+	help
+	  Say y to enable support for hash algorithms.
drivers/crypto/allwinner/sun8i-ss/Makefile +1 -0
index 49f2f912c816..aabfd893c817 100644
@@ -1,3 +1,4 @@
 obj-$(CONFIG_CRYPTO_DEV_SUN8I_SS) += sun8i-ss.o
 sun8i-ss-y += sun8i-ss-core.o sun8i-ss-cipher.o
 sun8i-ss-$(CONFIG_CRYPTO_DEV_SUN8I_SS_PRNG) += sun8i-ss-prng.o
+sun8i-ss-$(CONFIG_CRYPTO_DEV_SUN8I_SS_HASH) += sun8i-ss-hash.o
drivers/crypto/allwinner/sun8i-ss/sun8i-ss-core.c +155 -0

185 lines not on this page · raw message

drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c new file +446 -0

446 lines not on this page · raw message

drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h +60 -0

105 lines not on this page · raw message

Signature · 1 line
2.26.2
#4 lore raw

Re: [PATCH v3 03/14] crypto: sun8i-ss: support hash algorithms

I love your patch! Perhaps something to improve:

Hi Corentin,

I love your patch! Perhaps something to improve:

[auto build test WARNING on sunxi/sunxi/for-next]
[also build test WARNING on cryptodev/master crypto/master v5.8-rc1 next-20200621] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Corentin-Labbe/crypto-allwinner-add-xRNG-and-hashes/20200622-033401
base:   https://git.kernel.org/pub/scm/linux/kernel/git/sunxi/linux.git sunxi/for-next
config: x86_64-allyesconfig (attached as .config)
compiler: clang version 11.0.0 (https://github.com/llvm/llvm-project ef455a55bcf2cfea04a99c361b182ad18b7f03f1)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install x86_64 cross compiling tool for clang build
# apt-get install binutils-x86-64-linux-gnu
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>, old ones prefixed by <<):

>> drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c:175:5: warning: no previous prototype for function 'sun8i_ss_hash_digest_fb' [-Wmissing-prototypes]

int sun8i_ss_hash_digest_fb(struct ahash_request *areq)
^
drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c:175:1: note: declare 'static' if the function is not intended to be used outside of this translation unit int sun8i_ss_hash_digest_fb(struct ahash_request *areq)
^
static
1 warning generated.

vim +/sun8i_ss_hash_digest_fb +175 drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c

174

 > 175	int sun8i_ss_hash_digest_fb(struct ahash_request *areq)

176 {
177 struct sun8i_ss_hash_reqctx *rctx = ahash_request_ctx(areq);
178 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
179 struct sun8i_ss_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
180 #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
181 struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
182 struct sun8i_ss_alg_template *algt;
183 #endif
184
185 ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
186 rctx->fallback_req.base.flags = areq->base.flags &
187 CRYPTO_TFM_REQ_MAY_SLEEP;
188
189 rctx->fallback_req.nbytes = areq->nbytes;
190 rctx->fallback_req.src = areq->src;
191 rctx->fallback_req.result = areq->result;
192 #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
193 algt = container_of(alg, struct sun8i_ss_alg_template, alg.hash);
194 algt->stat_fb++;
195 #endif
196
197 return crypto_ahash_digest(&rctx->fallback_req);
198 }
199

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#5 lore raw

PATCH v3 4/14 crypto: sun8i-ss: fix a trivial typo

In reply to Corentin Labbe

This fixes a trivial typo.

---
 drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h +1 -1
index 350a611b0b9e..056fcdd14201 100644
@@ -100,7 +100,7 @@ struct ss_clock {
  * @alg_hash:	list of supported hashes. for each SS_ID_ this will give the
  *              corresponding SS_ALG_XXX value
  * @op_mode:	list of supported block modes
- * @ss_clks!	list of clock needed by this variant
+ * @ss_clks:	list of clock needed by this variant
  */
 struct ss_variant {
 	char alg_cipher[SS_ID_CIPHER_MAX];
Signature · 1 line
2.26.2
#6 lore raw

PATCH v3 5/14 crypto: sun8i-ss: Add more comment on some structures

In reply to Corentin Labbe

This patch adds some comment on structures used by sun8i-ss.

---
 drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h | 4 ++++
 1 file changed, 4 insertions(+)
drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h +4 -0
index 056fcdd14201..b2668e5b612f 100644
@@ -171,6 +171,8 @@ struct sun8i_ss_dev {
  * @ivlen:	size of biv
  * @keylen:	keylen for this request
  * @biv:	buffer which contain the IV
+ *
+ * t_src, t_dst, p_key, p_iv op_mode, op_dir and method must be in LE32
  */
 struct sun8i_cipher_req_ctx {
 	struct sginfo t_src[MAX_SG];
@@ -193,6 +195,8 @@ struct sun8i_cipher_req_ctx {
  * @keylen:		len of the key
  * @ss:			pointer to the private data of driver handling this TFM
  * @fallback_tfm:	pointer to the fallback TFM
+ *
+ * enginectx must be the first element
  */
 struct sun8i_cipher_tfm_ctx {
 	struct crypto_engine_ctx enginectx;
Signature · 1 line
2.26.2
#7 lore raw

PATCH v3 6/14 crypto: sun8i-ss: better debug printing

In reply to Corentin Labbe

This patch reworks the way debug info are printed. Instead of printing raw numbers, let's add a bit of context.

---
 drivers/crypto/allwinner/sun8i-ss/sun8i-ss-core.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/crypto/allwinner/sun8i-ss/sun8i-ss-core.c +3 -3
index cd408969bd03..8ab154842c9e 100644
@@ -420,19 +420,19 @@ static int sun8i_ss_dbgfs_read(struct seq_file *seq, void *v)
 			continue;
 		switch (ss_algs[i].type) {
 		case CRYPTO_ALG_TYPE_SKCIPHER:
-			seq_printf(seq, "%s %s %lu %lu\n",
+			seq_printf(seq, "%s %s reqs=%lu fallback=%lu\n",
 				   ss_algs[i].alg.skcipher.base.cra_driver_name,
 				   ss_algs[i].alg.skcipher.base.cra_name,
 				   ss_algs[i].stat_req, ss_algs[i].stat_fb);
 			break;
 		case CRYPTO_ALG_TYPE_RNG:
-			seq_printf(seq, "%s %s %lu %lu\n",
+			seq_printf(seq, "%s %s reqs=%lu tsize=%lu\n",
 				   ss_algs[i].alg.rng.base.cra_driver_name,
 				   ss_algs[i].alg.rng.base.cra_name,
 				   ss_algs[i].stat_req, ss_algs[i].stat_bytes);
 			break;
 		case CRYPTO_ALG_TYPE_AHASH:
-			seq_printf(seq, "%s %s %lu %lu\n",
+			seq_printf(seq, "%s %s reqs=%lu fallback=%lu\n",
 				   ss_algs[i].alg.hash.halg.base.cra_driver_name,
 				   ss_algs[i].alg.hash.halg.base.cra_name,
 				   ss_algs[i].stat_req, ss_algs[i].stat_fb);
Signature · 1 line
2.26.2
#8 lore raw

PATCH v3 7/14 crypto: sun8i-ce: move iv data to request context

In reply to Corentin Labbe

Instead of storing IV data in the channel context, store them in the request context.
Storing them in the channel structure was conceptualy wrong since they are per request related.

---
 .../allwinner/sun8i-ce/sun8i-ce-cipher.c      | 27 +++++++++----------
 drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h  | 10 ++++---
 2 files changed, 19 insertions(+), 18 deletions(-)
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c +13 -14
index a6abb701bfc6..7716fa2d3250 100644
@@ -91,7 +91,6 @@ static int sun8i_ce_cipher(struct skcipher_request *areq)
 	struct scatterlist *sg;
 	unsigned int todo, len, offset, ivsize;
 	dma_addr_t addr_iv = 0, addr_key = 0;
-	void *backup_iv = NULL;
 	u32 common, sym;
 	int flow, i;
 	int nr_sgs = 0;
@@ -154,24 +153,24 @@ static int sun8i_ce_cipher(struct skcipher_request *areq)
 
 	ivsize = crypto_skcipher_ivsize(tfm);
 	if (areq->iv && crypto_skcipher_ivsize(tfm) > 0) {
-		chan->ivlen = ivsize;
-		chan->bounce_iv = kzalloc(ivsize, GFP_KERNEL | GFP_DMA);
-		if (!chan->bounce_iv) {
+		rctx->ivlen = ivsize;
+		rctx->bounce_iv = kzalloc(ivsize, GFP_KERNEL | GFP_DMA);
+		if (!rctx->bounce_iv) {
 			err = -ENOMEM;
 			goto theend_key;
 		}
 		if (rctx->op_dir & CE_DECRYPTION) {
-			backup_iv = kzalloc(ivsize, GFP_KERNEL);
-			if (!backup_iv) {
+			rctx->backup_iv = kzalloc(ivsize, GFP_KERNEL);
+			if (!rctx->backup_iv) {
 				err = -ENOMEM;
 				goto theend_key;
 			}
 			offset = areq->cryptlen - ivsize;
-			scatterwalk_map_and_copy(backup_iv, areq->src, offset,
-						 ivsize, 0);
+			scatterwalk_map_and_copy(rctx->backup_iv, areq->src,
+						 offset, ivsize, 0);
 		}
-		memcpy(chan->bounce_iv, areq->iv, ivsize);
-		addr_iv = dma_map_single(ce->dev, chan->bounce_iv, chan->ivlen,
+		memcpy(rctx->bounce_iv, areq->iv, ivsize);
+		addr_iv = dma_map_single(ce->dev, rctx->bounce_iv, rctx->ivlen,
 					 DMA_TO_DEVICE);
 		cet->t_iv = cpu_to_le32(addr_iv);
 		if (dma_mapping_error(ce->dev, addr_iv)) {
@@ -252,17 +251,17 @@ static int sun8i_ce_cipher(struct skcipher_request *areq)
 theend_iv:
 	if (areq->iv && ivsize > 0) {
 		if (addr_iv)
-			dma_unmap_single(ce->dev, addr_iv, chan->ivlen,
+			dma_unmap_single(ce->dev, addr_iv, rctx->ivlen,
 					 DMA_TO_DEVICE);
 		offset = areq->cryptlen - ivsize;
 		if (rctx->op_dir & CE_DECRYPTION) {
-			memcpy(areq->iv, backup_iv, ivsize);
-			kzfree(backup_iv);
+			memcpy(areq->iv, rctx->backup_iv, ivsize);
+			kzfree(rctx->backup_iv);
 		} else {
 			scatterwalk_map_and_copy(areq->iv, areq->dst, offset,
 						 ivsize, 0);
 		}
-		kfree(chan->bounce_iv);
+		kfree(rctx->bounce_iv);
 	}
 
 theend_key:
drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h +6 -4
index 0e9eac397e1b..c9c7ef8299e2 100644
@@ -129,8 +129,6 @@ struct ce_task {
 /*
  * struct sun8i_ce_flow - Information used by each flow
  * @engine:	ptr to the crypto_engine for this flow
- * @bounce_iv:	buffer which contain the IV
- * @ivlen:	size of bounce_iv
  * @complete:	completion for the current task on this flow
  * @status:	set to 1 by interrupt if task is done
  * @t_phy:	Physical address of task
@@ -139,8 +137,6 @@ struct ce_task {
  */
 struct sun8i_ce_flow {
 	struct crypto_engine *engine;
-	void *bounce_iv;
-	unsigned int ivlen;
 	struct completion complete;
 	int status;
 	dma_addr_t t_phy;
@@ -183,10 +179,16 @@ struct sun8i_ce_dev {
  * struct sun8i_cipher_req_ctx - context for a skcipher request
  * @op_dir:	direction (encrypt vs decrypt) for this request
  * @flow:	the flow to use for this request
+ * @backup_iv:	buffer which contain the next IV to store
+ * @bounce_iv:	buffer which contain a copy of IV
+ * @ivlen:	size of bounce_iv
  */
 struct sun8i_cipher_req_ctx {
 	u32 op_dir;
 	int flow;
+	void *backup_iv;
+	void *bounce_iv;
+	unsigned int ivlen;
 };
 
 /*
Signature · 1 line
2.26.2
#9 lore raw

PATCH v3 8/14 crypto: sun8i-ce: split into prepare/run/unprepare

In reply to Corentin Labbe

This patch split the do_one_request into three.
Prepare will handle all DMA mapping and initialisation of the task structure.
Unprepare will clean all DMA mapping.
And the do_one_request will be limited to just executing the task.

---
 .../allwinner/sun8i-ce/sun8i-ce-cipher.c      | 70 ++++++++++++++++---
 drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h  |  4 ++
 2 files changed, 66 insertions(+), 8 deletions(-)
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c +62 -8
index 7716fa2d3250..d662dac83361 100644
@@ -78,8 +78,9 @@ static int sun8i_ce_cipher_fallback(struct skcipher_request *areq)
 	return err;
 }
 
-static int sun8i_ce_cipher(struct skcipher_request *areq)
+static int sun8i_ce_cipher_prepare(struct crypto_engine *engine, void *async_req)
 {
+	struct skcipher_request *areq = container_of(async_req, struct skcipher_request, base);
 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
 	struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
 	struct sun8i_ce_dev *ce = op->ce;
@@ -237,7 +238,9 @@ static int sun8i_ce_cipher(struct skcipher_request *areq)
 	}
 
 	chan->timeout = areq->cryptlen;
-	err = sun8i_ce_run_task(ce, flow, crypto_tfm_alg_name(areq->base.tfm));
+	rctx->nr_sgs = nr_sgs;
+	rctx->nr_sgd = nr_sgd;
+	return 0;
 
 theend_sgs:
 	if (areq->src == areq->dst) {
@@ -271,13 +274,64 @@ static int sun8i_ce_cipher(struct skcipher_request *areq)
 	return err;
 }
 
-static int sun8i_ce_handle_cipher_request(struct crypto_engine *engine, void *areq)
+int sun8i_ce_cipher_run(struct crypto_engine *engine, void *areq)
 {
-	int err;
 	struct skcipher_request *breq = container_of(areq, struct skcipher_request, base);
+	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(breq);
+	struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
+	struct sun8i_ce_dev *ce = op->ce;
+	struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(breq);
+	int flow, err;
 
-	err = sun8i_ce_cipher(breq);
+	flow = rctx->flow;
+	err = sun8i_ce_run_task(ce, flow, crypto_tfm_alg_name(breq->base.tfm));
 	crypto_finalize_skcipher_request(engine, breq, err);
+	return 0;
+}
+
+static int sun8i_ce_cipher_unprepare(struct crypto_engine *engine, void *async_req)
+{
+	struct skcipher_request *areq = container_of(async_req, struct skcipher_request, base);
+	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
+	struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
+	struct sun8i_ce_dev *ce = op->ce;
+	struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
+	struct sun8i_ce_flow *chan;
+	struct ce_task *cet;
+	unsigned int ivsize, offset;
+	int nr_sgs = rctx->nr_sgs;
+	int nr_sgd = rctx->nr_sgd;
+	int flow;
+
+	flow = rctx->flow;
+	chan = &ce->chanlist[flow];
+	cet = chan->tl;
+	ivsize = crypto_skcipher_ivsize(tfm);
+
+	if (areq->src == areq->dst) {
+		dma_unmap_sg(ce->dev, areq->src, nr_sgs, DMA_BIDIRECTIONAL);
+	} else {
+		if (nr_sgs > 0)
+			dma_unmap_sg(ce->dev, areq->src, nr_sgs, DMA_TO_DEVICE);
+		dma_unmap_sg(ce->dev, areq->dst, nr_sgd, DMA_FROM_DEVICE);
+	}
+
+	if (areq->iv && ivsize > 0) {
+		if (cet->t_iv)
+			dma_unmap_single(ce->dev, cet->t_iv, rctx->ivlen,
+					 DMA_TO_DEVICE);
+		offset = areq->cryptlen - ivsize;
+		if (rctx->op_dir & CE_DECRYPTION) {
+			memcpy(areq->iv, rctx->backup_iv, ivsize);
+			kzfree(rctx->backup_iv);
+		} else {
+			scatterwalk_map_and_copy(areq->iv, areq->dst, offset,
+						 ivsize, 0);
+		}
+		kfree(rctx->bounce_iv);
+	}
+
+	dma_unmap_single(ce->dev, cet->t_key, op->keylen, DMA_TO_DEVICE);
 
 	return 0;
 }
@@ -347,9 +401,9 @@ int sun8i_ce_cipher_init(struct crypto_tfm *tfm)
 		 crypto_tfm_alg_driver_name(&sktfm->base),
 		 crypto_tfm_alg_driver_name(crypto_skcipher_tfm(&op->fallback_tfm->base)));
 
-	op->enginectx.op.do_one_request = sun8i_ce_handle_cipher_request;
-	op->enginectx.op.prepare_request = NULL;
-	op->enginectx.op.unprepare_request = NULL;
+	op->enginectx.op.do_one_request = sun8i_ce_cipher_run;
+	op->enginectx.op.prepare_request = sun8i_ce_cipher_prepare;
+	op->enginectx.op.unprepare_request = sun8i_ce_cipher_unprepare;
 
 	err = pm_runtime_get_sync(op->ce->dev);
 	if (err < 0)
drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h +4 -0
index c9c7ef8299e2..fe97fee74e47 100644
@@ -182,6 +182,8 @@ struct sun8i_ce_dev {
  * @backup_iv:	buffer which contain the next IV to store
  * @bounce_iv:	buffer which contain a copy of IV
  * @ivlen:	size of bounce_iv
+ * @nr_sgs:	The number of source SG (as given by dma_map_sg())
+ * @nr_sgd:	The number of destination SG (as given by dma_map_sg())
  */
 struct sun8i_cipher_req_ctx {
 	u32 op_dir;
@@ -189,6 +191,8 @@ struct sun8i_cipher_req_ctx {
 	void *backup_iv;
 	void *bounce_iv;
 	unsigned int ivlen;
+	int nr_sgs;
+	int nr_sgd;
 };
 
 /*
Signature · 1 line
2.26.2
#10 lore raw

Re: [PATCH v3 08/14] crypto: sun8i-ce: split into prepare/run/unprepare

I love your patch! Perhaps something to improve:

Hi Corentin,

I love your patch! Perhaps something to improve:

[auto build test WARNING on sunxi/sunxi/for-next]
[also build test WARNING on cryptodev/master crypto/master v5.8-rc1] [cannot apply to next-20200621]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Corentin-Labbe/crypto-allwinner-add-xRNG-and-hashes/20200622-033401
base:   https://git.kernel.org/pub/scm/linux/kernel/git/sunxi/linux.git sunxi/for-next
config: arm-sunxi_defconfig (attached as .config)
compiler: arm-linux-gnueabi-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=arm

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>, old ones prefixed by <<):

>> drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c:277:5: warning: no previous prototype for 'sun8i_ce_cipher_run' [-Wmissing-prototypes]

277 | int sun8i_ce_cipher_run(struct crypto_engine *engine, void *areq)
| ^~~~~~~~~~~~~~~~~~~

vim +/sun8i_ce_cipher_run +277 drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c

276

 > 277	int sun8i_ce_cipher_run(struct crypto_engine *engine, void *areq)

278 {
279 struct skcipher_request *breq = container_of(areq, struct skcipher_request, base);
280 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(breq);
281 struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
282 struct sun8i_ce_dev *ce = op->ce;
283 struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(breq);
284 int flow, err;
285
286 flow = rctx->flow;
287 err = sun8i_ce_run_task(ce, flow, crypto_tfm_alg_name(breq->base.tfm));
288 crypto_finalize_skcipher_request(engine, breq, err);
289 return 0;
290 }
291

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#11 lore raw

Re: [PATCH v3 08/14] crypto: sun8i-ce: split into prepare/run/unprepare

In reply to Corentin Labbe
I love your patch! Perhaps something to improve:

Hi Corentin,

I love your patch! Perhaps something to improve:

[auto build test WARNING on sunxi/sunxi/for-next]
[also build test WARNING on cryptodev/master crypto/master v5.8-rc1] [cannot apply to next-20200621]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Corentin-Labbe/crypto-allwinner-add-xRNG-and-hashes/20200622-033401
base:   https://git.kernel.org/pub/scm/linux/kernel/git/sunxi/linux.git sunxi/for-next
config: x86_64-allyesconfig (attached as .config)
compiler: clang version 11.0.0 (https://github.com/llvm/llvm-project ef455a55bcf2cfea04a99c361b182ad18b7f03f1)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install x86_64 cross compiling tool for clang build
# apt-get install binutils-x86-64-linux-gnu
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>, old ones prefixed by <<):

>> drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c:277:5: warning: no previous prototype for function 'sun8i_ce_cipher_run' [-Wmissing-prototypes]

int sun8i_ce_cipher_run(struct crypto_engine *engine, void *areq)
^
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c:277:1: note: declare 'static' if the function is not intended to be used outside of this translation unit int sun8i_ce_cipher_run(struct crypto_engine *engine, void *areq)
^
static
1 warning generated.

vim +/sun8i_ce_cipher_run +277 drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c

276

 > 277	int sun8i_ce_cipher_run(struct crypto_engine *engine, void *areq)

278 {
279 struct skcipher_request *breq = container_of(areq, struct skcipher_request, base);
280 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(breq);
281 struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
282 struct sun8i_ce_dev *ce = op->ce;
283 struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(breq);
284 int flow, err;
285
286 flow = rctx->flow;
287 err = sun8i_ce_run_task(ce, flow, crypto_tfm_alg_name(breq->base.tfm));
288 crypto_finalize_skcipher_request(engine, breq, err);
289 return 0;
290 }
291

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#12 lore raw

PATCH v3 9/14 crypto: sun8i-ce: handle different error registers

In reply to Corentin Labbe

Error registers are different across SoCs.
This patch handle those difference.

---
 .../crypto/allwinner/sun8i-ce/sun8i-ce-core.c | 62 ++++++++++++++++---
 drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h  |  8 +++
 2 files changed, 62 insertions(+), 8 deletions(-)
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-core.c +54 -8
index b957061424a1..94969f86b967 100644
@@ -40,7 +40,8 @@ static const struct ce_variant ce_h3_variant = {
 	.ce_clks = {
 		{ "bus", 0, 200000000 },
 		{ "mod", 50000000, 0 },
-		}
+		},
+	.esr = ESR_H3,
 };
 
 static const struct ce_variant ce_h5_variant = {
@@ -51,7 +52,8 @@ static const struct ce_variant ce_h5_variant = {
 	.ce_clks = {
 		{ "bus", 0, 200000000 },
 		{ "mod", 300000000, 0 },
-		}
+		},
+	.esr = ESR_H5,
 };
 
 static const struct ce_variant ce_h6_variant = {
@@ -64,7 +66,8 @@ static const struct ce_variant ce_h6_variant = {
 		{ "bus", 0, 200000000 },
 		{ "mod", 300000000, 0 },
 		{ "ram", 0, 400000000 },
-		}
+		},
+	.esr = ESR_H6,
 };
 
 static const struct ce_variant ce_a64_variant = {
@@ -75,7 +78,8 @@ static const struct ce_variant ce_a64_variant = {
 	.ce_clks = {
 		{ "bus", 0, 200000000 },
 		{ "mod", 300000000, 0 },
-		}
+		},
+	.esr = ESR_A64,
 };
 
 static const struct ce_variant ce_r40_variant = {
@@ -86,7 +90,8 @@ static const struct ce_variant ce_r40_variant = {
 	.ce_clks = {
 		{ "bus", 0, 200000000 },
 		{ "mod", 300000000, 0 },
-		}
+		},
+	.esr = ESR_R40,
 };
 
 /*
@@ -102,6 +107,7 @@ int sun8i_ce_run_task(struct sun8i_ce_dev *ce, int flow, const char *name)
 {
 	u32 v;
 	int err = 0;
+	struct ce_task *cet = ce->chanlist[flow].tl;
 
 #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
 	ce->chanlist[flow].stat_req++;
@@ -128,19 +134,56 @@ int sun8i_ce_run_task(struct sun8i_ce_dev *ce, int flow, const char *name)
 			msecs_to_jiffies(ce->chanlist[flow].timeout));
 
 	if (ce->chanlist[flow].status == 0) {
-		dev_err(ce->dev, "DMA timeout for %s\n", name);
+		dev_err(ce->dev, "DMA timeout for %s (tm=%d) on flow %d\n", name,
+			ce->chanlist[flow].timeout, flow);
 		err = -EFAULT;
 	}
 	/* No need to lock for this read, the channel is locked so
 	 * nothing could modify the error value for this channel
 	 */
 	v = readl(ce->base + CE_ESR);
-	if (v) {
+	switch (ce->variant->esr) {
+	case ESR_H3:
+		/* Sadly, the error bit is not per flow */
+		if (v) {
+			dev_err(ce->dev, "CE ERROR: %x for flow %x\n", v, flow);
+			err = -EFAULT;
+			print_hex_dump(KERN_INFO, "TASK: ", DUMP_PREFIX_NONE, 16, 4,
+				       cet, sizeof(struct ce_task), false);
+		}
+		if (v & CE_ERR_ALGO_NOTSUP)
+			dev_err(ce->dev, "CE ERROR: algorithm not supported\n");
+		if (v & CE_ERR_DATALEN)
+			dev_err(ce->dev, "CE ERROR: data length error\n");
+		if (v & CE_ERR_KEYSRAM)
+			dev_err(ce->dev, "CE ERROR: keysram access error for AES\n");
+		break;
+	case ESR_A64:
+	case ESR_H5:
+	case ESR_R40:
 		v >>= (flow * 4);
+		v &= 0xF;
+		if (v) {
+			dev_err(ce->dev, "CE ERROR: %x for flow %x\n", v, flow);
+			err = -EFAULT;
+			print_hex_dump(KERN_INFO, "TASK: ", DUMP_PREFIX_NONE, 16, 4,
+				       cet, sizeof(struct ce_task), false);
+		}
+		if (v & CE_ERR_ALGO_NOTSUP)
+			dev_err(ce->dev, "CE ERROR: algorithm not supported\n");
+		if (v & CE_ERR_DATALEN)
+			dev_err(ce->dev, "CE ERROR: data length error\n");
+		if (v & CE_ERR_KEYSRAM)
+			dev_err(ce->dev, "CE ERROR: keysram access error for AES\n");
+		break;
+	case ESR_H6:
+		v >>= (flow * 8);
 		v &= 0xFF;
 		if (v) {
 			dev_err(ce->dev, "CE ERROR: %x for flow %x\n", v, flow);
 			err = -EFAULT;
+			print_hex_dump(KERN_INFO, "TASK: ", DUMP_PREFIX_NONE, 16, 4,
+				       cet, sizeof(struct ce_task), false);
 		}
 		if (v & CE_ERR_ALGO_NOTSUP)
 			dev_err(ce->dev, "CE ERROR: algorithm not supported\n");
@@ -150,7 +193,10 @@ int sun8i_ce_run_task(struct sun8i_ce_dev *ce, int flow, const char *name)
 			dev_err(ce->dev, "CE ERROR: keysram access error for AES\n");
 		if (v & CE_ERR_ADDR_INVALID)
 			dev_err(ce->dev, "CE ERROR: address invalid\n");
-		}
+		if (v & CE_ERR_KEYLADDER)
+			dev_err(ce->dev, "CE ERROR: key ladder configuration error\n");
+		break;
+	}
 
 	return err;
 }
drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h +8 -0
index fe97fee74e47..ed1a91da967b 100644
@@ -65,6 +65,12 @@
 #define CE_ERR_ADDR_INVALID	BIT(5)
 #define CE_ERR_KEYLADDER	BIT(6)
 
+#define ESR_H3	0
+#define ESR_A64	1
+#define ESR_R40	2
+#define ESR_H5	3
+#define ESR_H6	4
+
 #define CE_DIE_ID_SHIFT	16
 #define CE_DIE_ID_MASK	0x07
 
@@ -94,12 +100,14 @@ struct ce_clock {
  * @has_t_dlen_in_bytes:	Does the request size for cipher is in
  *				bytes or words
  * @ce_clks:	list of clocks needed by this variant
+ * @esr:	The type of error register
  */
 struct ce_variant {
 	char alg_cipher[CE_ID_CIPHER_MAX];
 	u32 op_mode[CE_ID_OP_MAX];
 	bool has_t_dlen_in_bytes;
 	struct ce_clock ce_clks[CE_MAX_CLOCKS];
+	int esr;
 };
 
 struct sginfo {
Signature · 1 line
2.26.2
#13 lore raw

PATCH v3 10/14 crypto: sun8i-ce: rename has_t_dlen_in_bytes to cipher_t_dlen_in_bytes

In reply to Corentin Labbe

Hash algorithms will need also a spetial t_dlen handling, but since the meaning will be different, rename the current flag to specify it apply only on ciphers algorithms.

---
 drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c | 2 +-
 drivers/crypto/allwinner/sun8i-ce/sun8i-ce-core.c   | 2 +-
 drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h        | 4 ++--
 3 files changed, 4 insertions(+), 4 deletions(-)
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c +1 -1
index d662dac83361..ee7add582e90 100644
@@ -122,7 +122,7 @@ static int sun8i_ce_cipher_prepare(struct crypto_engine *engine, void *async_req
 	common |= rctx->op_dir | CE_COMM_INT;
 	cet->t_common_ctl = cpu_to_le32(common);
 	/* CTS and recent CE (H6) need length in bytes, in word otherwise */
-	if (ce->variant->has_t_dlen_in_bytes)
+	if (ce->variant->cipher_t_dlen_in_bytes)
 		cet->t_dlen = cpu_to_le32(areq->cryptlen);
 	else
 		cet->t_dlen = cpu_to_le32(areq->cryptlen / 4);
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-core.c +1 -1
index 94969f86b967..8091ea1d5afc 100644
@@ -61,7 +61,7 @@ static const struct ce_variant ce_h6_variant = {
 	},
 	.op_mode = { CE_OP_ECB, CE_OP_CBC
 	},
-	.has_t_dlen_in_bytes = true,
+	.cipher_t_dlen_in_bytes = true,
 	.ce_clks = {
 		{ "bus", 0, 200000000 },
 		{ "mod", 300000000, 0 },
drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h +2 -2
index ed1a91da967b..0a70fcc102f1 100644
@@ -97,7 +97,7 @@ struct ce_clock {
  * @alg_cipher:	list of supported ciphers. for each CE_ID_ this will give the
  *              coresponding CE_ALG_XXX value
  * @op_mode:	list of supported block modes
- * @has_t_dlen_in_bytes:	Does the request size for cipher is in
+ * @cipher_t_dlen_in_bytes:	Does the request size for cipher is in
  *				bytes or words
  * @ce_clks:	list of clocks needed by this variant
  * @esr:	The type of error register
@@ -105,7 +105,7 @@ struct ce_clock {
 struct ce_variant {
 	char alg_cipher[CE_ID_CIPHER_MAX];
 	u32 op_mode[CE_ID_OP_MAX];
-	bool has_t_dlen_in_bytes;
+	bool cipher_t_dlen_in_bytes;
 	struct ce_clock ce_clks[CE_MAX_CLOCKS];
 	int esr;
 };
Signature · 1 line
2.26.2
#14 lore raw

PATCH v3 11/14 crypto: sun8i-ce: support hash algorithms

In reply to Corentin Labbe

The CE support multiples hash algorithms, this patch adds support for MD5, SHA1, SHA224, SHA256, SHA384 and SHA512.

---
 drivers/crypto/allwinner/Kconfig              |  10 +
 drivers/crypto/allwinner/sun8i-ce/Makefile    |   1 +
 .../crypto/allwinner/sun8i-ce/sun8i-ce-core.c | 229 ++++++++++
 .../crypto/allwinner/sun8i-ce/sun8i-ce-hash.c | 415 ++++++++++++++++++
 drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h  |  58 +++
 5 files changed, 713 insertions(+)
 create mode 100644 drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c
drivers/crypto/allwinner/Kconfig +10 -0
index 945228b3a8c4..93cc67adb1ed 100644
@@ -69,6 +69,16 @@ config CRYPTO_DEV_SUN8I_CE_DEBUG
 	  This will create /sys/kernel/debug/sun8i-ce/stats for displaying
 	  the number of requests per flow and per algorithm.
 
+config CRYPTO_DEV_SUN8I_CE_HASH
+	bool "Enable support for hash on sun8i-ce"
+	depends on CRYPTO_DEV_SUN8I_CE
+	select MD5
+	select SHA1
+	select SHA256
+	select SHA512
+	help
+	  Say y to enable support for hash algorithms.
+
 config CRYPTO_DEV_SUN8I_SS
 	tristate "Support for Allwinner Security System cryptographic offloader"
 	select CRYPTO_SKCIPHER
drivers/crypto/allwinner/sun8i-ce/Makefile +1 -0
index 08b68c3c1ca9..d1b1f0e86c79 100644
@@ -1,2 +1,3 @@
 obj-$(CONFIG_CRYPTO_DEV_SUN8I_CE) += sun8i-ce.o
 sun8i-ce-y += sun8i-ce-core.o sun8i-ce-cipher.o
+sun8i-ce-$(CONFIG_CRYPTO_DEV_SUN8I_CE_HASH) += sun8i-ce-hash.o
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-core.c +229 -0

286 lines not on this page · raw message

drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c new file +415 -0

415 lines not on this page · raw message

drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h +58 -0

107 lines not on this page · raw message

Signature · 1 line
2.26.2
#15 lore raw

Re: [PATCH v3 11/14] crypto: sun8i-ce: support hash algorithms

I love your patch! Perhaps something to improve:

Hi Corentin,

I love your patch! Perhaps something to improve:

[auto build test WARNING on sunxi/sunxi/for-next]
[also build test WARNING on cryptodev/master crypto/master v5.8-rc1] [cannot apply to next-20200621]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Corentin-Labbe/crypto-allwinner-add-xRNG-and-hashes/20200622-033401
base:   https://git.kernel.org/pub/scm/linux/kernel/git/sunxi/linux.git sunxi/for-next
config: sh-allmodconfig (attached as .config)
compiler: sh4-linux-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=sh

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>, old ones prefixed by <<):

drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c: In function 'sun8i_ce_hash_init':

>> drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c:76:32: warning: variable 'algt' set but not used [-Wunused-but-set-variable]
76 |  struct sun8i_ce_alg_template *algt;
|                                ^~~~
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c: At top level:
>> drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c:175:5: warning: no previous prototype for 'sun8i_ce_hash_digest_fb' [-Wmissing-prototypes]

175 | int sun8i_ce_hash_digest_fb(struct ahash_request *areq)
| ^~~~~~~~~~~~~~~~~~~~~~~

vim +/algt +76 drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c

    69	
    70	int sun8i_ce_hash_init(struct ahash_request *areq)
    71	{
    72		struct sun8i_ce_hash_reqctx *rctx = ahash_request_ctx(areq);
    73		struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
    74		struct sun8i_ce_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
    75		struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
  > 76		struct sun8i_ce_alg_template *algt;
    77	
    78		memset(rctx, 0, sizeof(struct sun8i_ce_hash_reqctx));
    79	
    80		algt = container_of(alg, struct sun8i_ce_alg_template, alg.hash);
    81	
    82		ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
    83		rctx->fallback_req.base.flags = areq->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP;
    84	
    85		return crypto_ahash_init(&rctx->fallback_req);
    86	}
    87	
    88	int sun8i_ce_hash_export(struct ahash_request *areq, void *out)
    89	{
    90		struct sun8i_ce_hash_reqctx *rctx = ahash_request_ctx(areq);
    91		struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
    92		struct sun8i_ce_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
    93	
    94		ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
    95		rctx->fallback_req.base.flags = areq->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP;
    96	
    97		return crypto_ahash_export(&rctx->fallback_req, out);
    98	}
    99	
   100	int sun8i_ce_hash_import(struct ahash_request *areq, const void *in)
   101	{
   102		struct sun8i_ce_hash_reqctx *rctx = ahash_request_ctx(areq);
   103		struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
   104		struct sun8i_ce_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
   105	
   106		ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
   107		rctx->fallback_req.base.flags = areq->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP;
   108	
   109		return crypto_ahash_import(&rctx->fallback_req, in);
   110	}
   111	
   112	int sun8i_ce_hash_final(struct ahash_request *areq)
   113	{
   114		struct sun8i_ce_hash_reqctx *rctx = ahash_request_ctx(areq);
   115		struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
   116		struct sun8i_ce_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
   117	#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
   118		struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
   119		struct sun8i_ce_alg_template *algt;
   120	#endif
   121	
   122		ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
   123		rctx->fallback_req.base.flags = areq->base.flags &
   124						CRYPTO_TFM_REQ_MAY_SLEEP;
   125		rctx->fallback_req.result = areq->result;
   126	
   127	#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
   128		algt = container_of(alg, struct sun8i_ce_alg_template, alg.hash);
   129		algt->stat_fb++;
   130	#endif
   131	
   132		return crypto_ahash_final(&rctx->fallback_req);
   133	}
   134	
   135	int sun8i_ce_hash_update(struct ahash_request *areq)
   136	{
   137		struct sun8i_ce_hash_reqctx *rctx = ahash_request_ctx(areq);
   138		struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
   139		struct sun8i_ce_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
   140	
   141		ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
   142		rctx->fallback_req.base.flags = areq->base.flags &
   143						CRYPTO_TFM_REQ_MAY_SLEEP;
   144		rctx->fallback_req.nbytes = areq->nbytes;
   145		rctx->fallback_req.src = areq->src;
   146	
   147		return crypto_ahash_update(&rctx->fallback_req);
   148	}
   149	
   150	int sun8i_ce_hash_finup(struct ahash_request *areq)
   151	{
   152		struct sun8i_ce_hash_reqctx *rctx = ahash_request_ctx(areq);
   153		struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
   154		struct sun8i_ce_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
   155	#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
   156		struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
   157		struct sun8i_ce_alg_template *algt;
   158	#endif
   159	
   160		ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
   161		rctx->fallback_req.base.flags = areq->base.flags &
   162						CRYPTO_TFM_REQ_MAY_SLEEP;
   163	
   164		rctx->fallback_req.nbytes = areq->nbytes;
   165		rctx->fallback_req.src = areq->src;
   166		rctx->fallback_req.result = areq->result;
   167	#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
   168		algt = container_of(alg, struct sun8i_ce_alg_template, alg.hash);
   169		algt->stat_fb++;
   170	#endif
   171	
   172		return crypto_ahash_finup(&rctx->fallback_req);
   173	}
   174	
 > 175	int sun8i_ce_hash_digest_fb(struct ahash_request *areq)

176 {
177 struct sun8i_ce_hash_reqctx *rctx = ahash_request_ctx(areq);
178 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
179 struct sun8i_ce_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
180 #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
181 struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
182 struct sun8i_ce_alg_template *algt;
183 #endif
184
185 ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
186 rctx->fallback_req.base.flags = areq->base.flags &
187 CRYPTO_TFM_REQ_MAY_SLEEP;
188
189 rctx->fallback_req.nbytes = areq->nbytes;
190 rctx->fallback_req.src = areq->src;
191 rctx->fallback_req.result = areq->result;
192 #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
193 algt = container_of(alg, struct sun8i_ce_alg_template, alg.hash);
194 algt->stat_fb++;
195 #endif
196
197 return crypto_ahash_digest(&rctx->fallback_req);
198 }
199

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#16 lore raw

Re: [PATCH v3 11/14] crypto: sun8i-ce: support hash algorithms

In reply to Corentin Labbe
I love your patch! Perhaps something to improve:

Hi Corentin,

I love your patch! Perhaps something to improve:

[auto build test WARNING on sunxi/sunxi/for-next]
[also build test WARNING on cryptodev/master crypto/master v5.8-rc1] [cannot apply to next-20200621]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Corentin-Labbe/crypto-allwinner-add-xRNG-and-hashes/20200622-033401
base:   https://git.kernel.org/pub/scm/linux/kernel/git/sunxi/linux.git sunxi/for-next
config: x86_64-allyesconfig (attached as .config)
compiler: clang version 11.0.0 (https://github.com/llvm/llvm-project ef455a55bcf2cfea04a99c361b182ad18b7f03f1)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install x86_64 cross compiling tool for clang build
# apt-get install binutils-x86-64-linux-gnu
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>, old ones prefixed by <<):

>> drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c:175:5: warning: no previous prototype for function 'sun8i_ce_hash_digest_fb' [-Wmissing-prototypes]

int sun8i_ce_hash_digest_fb(struct ahash_request *areq)
^
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c:175:1: note: declare 'static' if the function is not intended to be used outside of this translation unit int sun8i_ce_hash_digest_fb(struct ahash_request *areq)
^
static
1 warning generated.

vim +/sun8i_ce_hash_digest_fb +175 drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c

174

 > 175	int sun8i_ce_hash_digest_fb(struct ahash_request *areq)

176 {
177 struct sun8i_ce_hash_reqctx *rctx = ahash_request_ctx(areq);
178 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
179 struct sun8i_ce_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
180 #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
181 struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
182 struct sun8i_ce_alg_template *algt;
183 #endif
184
185 ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
186 rctx->fallback_req.base.flags = areq->base.flags &
187 CRYPTO_TFM_REQ_MAY_SLEEP;
188
189 rctx->fallback_req.nbytes = areq->nbytes;
190 rctx->fallback_req.src = areq->src;
191 rctx->fallback_req.result = areq->result;
192 #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
193 algt = container_of(alg, struct sun8i_ce_alg_template, alg.hash);
194 algt->stat_fb++;
195 #endif
196
197 return crypto_ahash_digest(&rctx->fallback_req);
198 }
199

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#17 lore raw

PATCH v3 12/14 crypto: sun8i-ce: Add stat_bytes debugfs

In reply to Corentin Labbe

This patch adds a new stat_bytes counter in the sun8i-ce debugfs.

---
 drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h | 2 ++
 1 file changed, 2 insertions(+)
drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h +2 -0
index 19ced8b1cd89..ef2f1e5aa23a 100644
@@ -274,6 +274,7 @@ struct sun8i_ce_hash_reqctx {
  * @alg:		one of sub struct must be used
  * @stat_req:		number of request done on this template
  * @stat_fb:		number of request which has fallbacked
+ * @stat_bytes:		total data size done by this template
  */
 struct sun8i_ce_alg_template {
 	u32 type;
@@ -287,6 +288,7 @@ struct sun8i_ce_alg_template {
 #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
 	unsigned long stat_req;
 	unsigned long stat_fb;
+	unsigned long stat_bytes;
 #endif
 };
 
Signature · 1 line
2.26.2
#18 lore raw

PATCH v3 13/14 crypto: sun8i-ce: Add support for the PRNG

In reply to Corentin Labbe

This patch had support for the PRNG present in the CE. The output was tested with rngtest without any failure.

---
 drivers/crypto/allwinner/Kconfig              |   8 +
 drivers/crypto/allwinner/sun8i-ce/Makefile    |   1 +
 .../crypto/allwinner/sun8i-ce/sun8i-ce-core.c |  58 ++++++-
 .../crypto/allwinner/sun8i-ce/sun8i-ce-prng.c | 161 ++++++++++++++++++
 drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h  |  31 ++++
 5 files changed, 258 insertions(+), 1 deletion(-)
 create mode 100644 drivers/crypto/allwinner/sun8i-ce/sun8i-ce-prng.c
drivers/crypto/allwinner/Kconfig +8 -0
index 93cc67adb1ed..223a5823867c 100644
@@ -79,6 +79,14 @@ config CRYPTO_DEV_SUN8I_CE_HASH
 	help
 	  Say y to enable support for hash algorithms.
 
+config CRYPTO_DEV_SUN8I_CE_PRNG
+	bool "Support for Allwinner Crypto Engine PRNG"
+	depends on CRYPTO_DEV_SUN8I_CE
+	select CRYPTO_RNG
+	help
+	  Select this option if you want to provide kernel-side support for
+	  the Pseudo-Random Number Generator found in the Crypto Engine.
+
 config CRYPTO_DEV_SUN8I_SS
 	tristate "Support for Allwinner Security System cryptographic offloader"
 	select CRYPTO_SKCIPHER
drivers/crypto/allwinner/sun8i-ce/Makefile +1 -0
index d1b1f0e86c79..c0ea81da2c7d 100644
@@ -1,3 +1,4 @@
 obj-$(CONFIG_CRYPTO_DEV_SUN8I_CE) += sun8i-ce.o
 sun8i-ce-y += sun8i-ce-core.o sun8i-ce-cipher.o
 sun8i-ce-$(CONFIG_CRYPTO_DEV_SUN8I_CE_HASH) += sun8i-ce-hash.o
+sun8i-ce-$(CONFIG_CRYPTO_DEV_SUN8I_CE_PRNG) += sun8i-ce-prng.o
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-core.c +57 -1

138 lines not on this page · raw message

drivers/crypto/allwinner/sun8i-ce/sun8i-ce-prng.c new file +161 -0

161 lines not on this page · raw message

drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h +31 -0
index ef2f1e5aa23a..2ef0c3814367 100644
@@ -14,6 +14,7 @@
 #include <linux/crypto.h>
 #include <crypto/internal/hash.h>
 #include <crypto/md5.h>
+#include <crypto/rng.h>
 #include <crypto/sha.h>
 
 /* CE Registers */
@@ -54,6 +55,8 @@
 #define CE_ALG_SHA256           19
 #define CE_ALG_SHA384           20
 #define CE_ALG_SHA512           21
+#define CE_ALG_PRNG		49
+#define CE_ALG_PRNG_V2		0x1d
 
 /* Used in ce_variant */
 #define CE_ID_NOTSUPP		0xFF
@@ -88,6 +91,10 @@
 #define ESR_H5	3
 #define ESR_H6	4
 
+#define PRNG_DATA_SIZE (160 / 8)
+#define PRNG_SEED_SIZE DIV_ROUND_UP(175, 8)
+#define PRNG_LD BIT(17)
+
 #define CE_DIE_ID_SHIFT	16
 #define CE_DIE_ID_MASK	0x07
 
@@ -120,8 +127,11 @@ struct ce_clock {
  *				bytes or words
  * @hash_t_dlen_in_bytes:	Does the request size for hash is in
  *				bits or words
+ * @prng_t_dlen_in_bytes:	Does the request size for PRNG is in
+ *				bytes or words
  * @ce_clks:	list of clocks needed by this variant
  * @esr:	The type of error register
+ * @prng:	The CE_ALG_XXX value for the PRNG
  */
 struct ce_variant {
 	char alg_cipher[CE_ID_CIPHER_MAX];
@@ -129,8 +139,10 @@ struct ce_variant {
 	u32 op_mode[CE_ID_OP_MAX];
 	bool cipher_t_dlen_in_bytes;
 	bool hash_t_dlen_in_bits;
+	bool prng_t_dlen_in_bytes;
 	struct ce_clock ce_clks[CE_MAX_CLOCKS];
 	int esr;
+	char prng;
 };
 
 struct sginfo {
@@ -185,6 +197,7 @@ struct sun8i_ce_flow {
  * @reset:	pointer to reset controller
  * @dev:	the platform device
  * @mlock:	Control access to device registers
+ * @rnglock:	Control access to the RNG (dedicated channel 3)
  * @chanlist:	array of all flow
  * @flow:	flow to use in next request
  * @variant:	pointer to variant specific data
@@ -197,6 +210,7 @@ struct sun8i_ce_dev {
 	struct reset_control *reset;
 	struct device *dev;
 	struct mutex mlock;
+	struct mutex rnglock;
 	struct sun8i_ce_flow *chanlist;
 	atomic_t flow;
 	const struct ce_variant *variant;
@@ -264,6 +278,16 @@ struct sun8i_ce_hash_reqctx {
 	int flow;
 };
 
+/*
+ * struct sun8i_ce_prng_ctx - context for PRNG TFM
+ * @seed:	The seed to use
+ * @slen:	The size of the seed
+ */
+struct sun8i_ce_rng_tfm_ctx {
+	void *seed;
+	unsigned int slen;
+};
+
 /*
  * struct sun8i_ce_alg_template - crypto_alg template
  * @type:		the CRYPTO_ALG_TYPE for this template
@@ -284,6 +308,7 @@ struct sun8i_ce_alg_template {
 	union {
 		struct skcipher_alg skcipher;
 		struct ahash_alg hash;
+		struct rng_alg rng;
 	} alg;
 #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
 	unsigned long stat_req;
@@ -318,3 +343,9 @@ int sun8i_ce_hash_update(struct ahash_request *areq);
 int sun8i_ce_hash_finup(struct ahash_request *areq);
 int sun8i_ce_hash_digest(struct ahash_request *areq);
 int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq);
+
+int sun8i_ce_prng_generate(struct crypto_rng *tfm, const u8 *src,
+			   unsigned int slen, u8 *dst, unsigned int dlen);
+int sun8i_ce_prng_seed(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);
+void sun8i_ce_prng_exit(struct crypto_tfm *tfm);
+int sun8i_ce_prng_init(struct crypto_tfm *tfm);
Signature · 1 line
2.26.2
#19 lore raw

Re: [PATCH v3 13/14] crypto: sun8i-ce: Add support for the PRNG

I love your patch! Perhaps something to improve:

Hi Corentin,

I love your patch! Perhaps something to improve:

[auto build test WARNING on sunxi/sunxi/for-next]
[also build test WARNING on cryptodev/master crypto/master v5.8-rc1] [cannot apply to next-20200621]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Corentin-Labbe/crypto-allwinner-add-xRNG-and-hashes/20200622-033401
base:   https://git.kernel.org/pub/scm/linux/kernel/git/sunxi/linux.git sunxi/for-next
config: sh-allmodconfig (attached as .config)
compiler: sh4-linux-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=sh

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>, old ones prefixed by <<):

drivers/crypto/allwinner/sun8i-ce/sun8i-ce-core.c: In function 'sun8i_ce_register_algs':

>> drivers/crypto/allwinner/sun8i-ce/sun8i-ce-core.c:818:26: warning: comparison is always false due to limited range of data type [-Wtype-limits]

818 | if (ce->variant->prng == CE_ID_NOTSUPP) {
| ^~

vim +818 drivers/crypto/allwinner/sun8i-ce/sun8i-ce-core.c

761
762 static int sun8i_ce_register_algs(struct sun8i_ce_dev *ce)
763 {
764 int ce_method, err, id, i;
765
766 for (i = 0; i < ARRAY_SIZE(ce_algs); i++) {
767 ce_algs[i].ce = ce;
768 switch (ce_algs[i].type) {
769 case CRYPTO_ALG_TYPE_SKCIPHER:
770 id = ce_algs[i].ce_algo_id;
771 ce_method = ce->variant->alg_cipher[id];
772 if (ce_method == CE_ID_NOTSUPP) {
773 dev_dbg(ce->dev,
774 "DEBUG: Algo of %s not supported\n",
775 ce_algs[i].alg.skcipher.base.cra_name);
776 ce_algs[i].ce = NULL;
777 break;
778 }
779 id = ce_algs[i].ce_blockmode;
780 ce_method = ce->variant->op_mode[id];
781 if (ce_method == CE_ID_NOTSUPP) {
782 dev_dbg(ce->dev, "DEBUG: Blockmode of %s not supported\n",
783 ce_algs[i].alg.skcipher.base.cra_name);
784 ce_algs[i].ce = NULL;
785 break;
786 }
787 dev_info(ce->dev, "Register %s\n",
788 ce_algs[i].alg.skcipher.base.cra_name);
789 err = crypto_register_skcipher(&ce_algs[i].alg.skcipher);
790 if (err) {
791 dev_err(ce->dev, "ERROR: Fail to register %s\n",
792 ce_algs[i].alg.skcipher.base.cra_name);
793 ce_algs[i].ce = NULL;
794 return err;
795 }
796 break;
797 case CRYPTO_ALG_TYPE_AHASH:
798 id = ce_algs[i].ce_algo_id;
799 ce_method = ce->variant->alg_hash[id];
800 if (ce_method == CE_ID_NOTSUPP) {
801 dev_info(ce->dev,
802 "DEBUG: Algo of %s not supported\n",
803 ce_algs[i].alg.hash.halg.base.cra_name);
804 ce_algs[i].ce = NULL;
805 break;
806 }
807 dev_info(ce->dev, "Register %s\n",
808 ce_algs[i].alg.hash.halg.base.cra_name);
809 err = crypto_register_ahash(&ce_algs[i].alg.hash);
810 if (err) {
811 dev_err(ce->dev, "ERROR: Fail to register %s\n",
812 ce_algs[i].alg.hash.halg.base.cra_name);
813 ce_algs[i].ce = NULL;
814 return err;
815 }
816 break;
817 case CRYPTO_ALG_TYPE_RNG:

 > 818				if (ce->variant->prng == CE_ID_NOTSUPP) {

819 dev_info(ce->dev,
820 "DEBUG: Algo of %s not supported\n",
821 ce_algs[i].alg.rng.base.cra_name);
822 ce_algs[i].ce = NULL;
823 break;
824 }
825 dev_info(ce->dev, "Register %s\n",
826 ce_algs[i].alg.rng.base.cra_name);
827 err = crypto_register_rng(&ce_algs[i].alg.rng);
828 if (err) {
829 dev_err(ce->dev, "Fail to register %s\n",
830 ce_algs[i].alg.rng.base.cra_name);
831 ce_algs[i].ce = NULL;
832 }
833 break;
834 default:
835 ce_algs[i].ce = NULL;
836 dev_err(ce->dev, "ERROR: tried to register an unknown algo\n");
837 }
838 }
839 return 0;
840 }
841

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#20 lore raw

Re: [PATCH v3 13/14] crypto: sun8i-ce: Add support for the PRNG

In reply to Corentin Labbe
I love your patch! Perhaps something to improve:

Hi Corentin,

I love your patch! Perhaps something to improve:

[auto build test WARNING on sunxi/sunxi/for-next]
[also build test WARNING on cryptodev/master crypto/master v5.8-rc2] [cannot apply to next-20200621]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Corentin-Labbe/crypto-allwinner-add-xRNG-and-hashes/20200622-033401
base:   https://git.kernel.org/pub/scm/linux/kernel/git/sunxi/linux.git sunxi/for-next
config: x86_64-allyesconfig (attached as .config)
compiler: clang version 11.0.0 (https://github.com/llvm/llvm-project ef455a55bcf2cfea04a99c361b182ad18b7f03f1)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install x86_64 cross compiling tool for clang build
# apt-get install binutils-x86-64-linux-gnu
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>, old ones prefixed by <<):

>> drivers/crypto/allwinner/sun8i-ce/sun8i-ce-core.c:818:26: warning: result of comparison of constant 255 with expression of type 'const char' is always false [-Wtautological-constant-out-of-range-compare]

if (ce->variant->prng == CE_ID_NOTSUPP) {
~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~
1 warning generated.

vim +818 drivers/crypto/allwinner/sun8i-ce/sun8i-ce-core.c

761
762 static int sun8i_ce_register_algs(struct sun8i_ce_dev *ce)
763 {
764 int ce_method, err, id, i;
765
766 for (i = 0; i < ARRAY_SIZE(ce_algs); i++) {
767 ce_algs[i].ce = ce;
768 switch (ce_algs[i].type) {
769 case CRYPTO_ALG_TYPE_SKCIPHER:
770 id = ce_algs[i].ce_algo_id;
771 ce_method = ce->variant->alg_cipher[id];
772 if (ce_method == CE_ID_NOTSUPP) {
773 dev_dbg(ce->dev,
774 "DEBUG: Algo of %s not supported\n",
775 ce_algs[i].alg.skcipher.base.cra_name);
776 ce_algs[i].ce = NULL;
777 break;
778 }
779 id = ce_algs[i].ce_blockmode;
780 ce_method = ce->variant->op_mode[id];
781 if (ce_method == CE_ID_NOTSUPP) {
782 dev_dbg(ce->dev, "DEBUG: Blockmode of %s not supported\n",
783 ce_algs[i].alg.skcipher.base.cra_name);
784 ce_algs[i].ce = NULL;
785 break;
786 }
787 dev_info(ce->dev, "Register %s\n",
788 ce_algs[i].alg.skcipher.base.cra_name);
789 err = crypto_register_skcipher(&ce_algs[i].alg.skcipher);
790 if (err) {
791 dev_err(ce->dev, "ERROR: Fail to register %s\n",
792 ce_algs[i].alg.skcipher.base.cra_name);
793 ce_algs[i].ce = NULL;
794 return err;
795 }
796 break;
797 case CRYPTO_ALG_TYPE_AHASH:
798 id = ce_algs[i].ce_algo_id;
799 ce_method = ce->variant->alg_hash[id];
800 if (ce_method == CE_ID_NOTSUPP) {
801 dev_info(ce->dev,
802 "DEBUG: Algo of %s not supported\n",
803 ce_algs[i].alg.hash.halg.base.cra_name);
804 ce_algs[i].ce = NULL;
805 break;
806 }
807 dev_info(ce->dev, "Register %s\n",
808 ce_algs[i].alg.hash.halg.base.cra_name);
809 err = crypto_register_ahash(&ce_algs[i].alg.hash);
810 if (err) {
811 dev_err(ce->dev, "ERROR: Fail to register %s\n",
812 ce_algs[i].alg.hash.halg.base.cra_name);
813 ce_algs[i].ce = NULL;
814 return err;
815 }
816 break;
817 case CRYPTO_ALG_TYPE_RNG:

 > 818				if (ce->variant->prng == CE_ID_NOTSUPP) {

819 dev_info(ce->dev,
820 "DEBUG: Algo of %s not supported\n",
821 ce_algs[i].alg.rng.base.cra_name);
822 ce_algs[i].ce = NULL;
823 break;
824 }
825 dev_info(ce->dev, "Register %s\n",
826 ce_algs[i].alg.rng.base.cra_name);
827 err = crypto_register_rng(&ce_algs[i].alg.rng);
828 if (err) {
829 dev_err(ce->dev, "Fail to register %s\n",
830 ce_algs[i].alg.rng.base.cra_name);
831 ce_algs[i].ce = NULL;
832 }
833 break;
834 default:
835 ce_algs[i].ce = NULL;
836 dev_err(ce->dev, "ERROR: tried to register an unknown algo\n");
837 }
838 }
839 return 0;
840 }
841

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#21 lore raw

PATCH v3 14/14 crypto: sun8i-ce: Add support for the TRNG

In reply to Corentin Labbe

This patch had support for the TRNG present in the CE.
Note that according to the algorithm ID, 2 version of the TRNG exists, the first present in H3/H5/R40/A64 and the second present in H6. This patch adds support for both, but only the second is working reliabily accoridng to rngtest.

---
 drivers/crypto/allwinner/Kconfig              |   8 ++
 drivers/crypto/allwinner/sun8i-ce/Makefile    |   1 +
 .../crypto/allwinner/sun8i-ce/sun8i-ce-core.c |  18 +++
 .../crypto/allwinner/sun8i-ce/sun8i-ce-trng.c | 124 ++++++++++++++++++
 drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h  |  18 +++
 5 files changed, 169 insertions(+)
 create mode 100644 drivers/crypto/allwinner/sun8i-ce/sun8i-ce-trng.c
drivers/crypto/allwinner/Kconfig +8 -0
index 223a5823867c..6aec31f7d2be 100644
@@ -87,6 +87,14 @@ config CRYPTO_DEV_SUN8I_CE_PRNG
 	  Select this option if you want to provide kernel-side support for
 	  the Pseudo-Random Number Generator found in the Crypto Engine.
 
+config CRYPTO_DEV_SUN8I_CE_TRNG
+	bool "Support for Allwinner Crypto Engine TRNG"
+	depends on CRYPTO_DEV_SUN8I_CE
+	select HW_RANDOM
+	help
+	  Select this option if you want to provide kernel-side support for
+	  the True Random Number Generator found in the Crypto Engine.
+
 config CRYPTO_DEV_SUN8I_SS
 	tristate "Support for Allwinner Security System cryptographic offloader"
 	select CRYPTO_SKCIPHER
drivers/crypto/allwinner/sun8i-ce/Makefile +1 -0
index c0ea81da2c7d..0842eb2d9408 100644
@@ -2,3 +2,4 @@ obj-$(CONFIG_CRYPTO_DEV_SUN8I_CE) += sun8i-ce.o
 sun8i-ce-y += sun8i-ce-core.o sun8i-ce-cipher.o
 sun8i-ce-$(CONFIG_CRYPTO_DEV_SUN8I_CE_HASH) += sun8i-ce-hash.o
 sun8i-ce-$(CONFIG_CRYPTO_DEV_SUN8I_CE_PRNG) += sun8i-ce-prng.o
+sun8i-ce-$(CONFIG_CRYPTO_DEV_SUN8I_CE_TRNG) += sun8i-ce-trng.o
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-core.c +18 -0
index 33790fee2c81..f1d6f34ea20e 100644
@@ -47,6 +47,7 @@ static const struct ce_variant ce_h3_variant = {
 		},
 	.esr = ESR_H3,
 	.prng = CE_ALG_PRNG,
+	.trng = CE_ID_NOTSUPP,
 };
 
 static const struct ce_variant ce_h5_variant = {
@@ -63,6 +64,7 @@ static const struct ce_variant ce_h5_variant = {
 		},
 	.esr = ESR_H5,
 	.prng = CE_ALG_PRNG,
+	.trng = CE_ID_NOTSUPP,
 };
 
 static const struct ce_variant ce_h6_variant = {
@@ -76,6 +78,7 @@ static const struct ce_variant ce_h6_variant = {
 	.cipher_t_dlen_in_bytes = true,
 	.hash_t_dlen_in_bits = true,
 	.prng_t_dlen_in_bytes = true,
+	.trng_t_dlen_in_bytes = true,
 	.ce_clks = {
 		{ "bus", 0, 200000000 },
 		{ "mod", 300000000, 0 },
@@ -83,6 +86,7 @@ static const struct ce_variant ce_h6_variant = {
 		},
 	.esr = ESR_H6,
 	.prng = CE_ALG_PRNG_V2,
+	.trng = CE_ALG_TRNG_V2,
 };
 
 static const struct ce_variant ce_a64_variant = {
@@ -99,6 +103,7 @@ static const struct ce_variant ce_a64_variant = {
 		},
 	.esr = ESR_A64,
 	.prng = CE_ALG_PRNG,
+	.trng = CE_ID_NOTSUPP,
 };
 
 static const struct ce_variant ce_r40_variant = {
@@ -115,6 +120,7 @@ static const struct ce_variant ce_r40_variant = {
 		},
 	.esr = ESR_R40,
 	.prng = CE_ALG_PRNG,
+	.trng = CE_ID_NOTSUPP,
 };
 
 /*
@@ -582,6 +588,10 @@ static int sun8i_ce_dbgfs_read(struct seq_file *seq, void *v)
 			break;
 		}
 	}
+#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_TRNG
+	seq_printf(seq, "HWRNG %lu %lu\n",
+		   ce->hwrng_stat_req, ce->hwrng_stat_bytes);
+#endif
 	return 0;
 }
 
@@ -932,6 +942,10 @@ static int sun8i_ce_probe(struct platform_device *pdev)
 	if (err < 0)
 		goto error_alg;
 
+#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_TRNG
+	sun8i_ce_hwrng_register(ce);
+#endif
+
 	v = readl(ce->base + CE_CTR);
 	v >>= CE_DIE_ID_SHIFT;
 	v &= CE_DIE_ID_MASK;
@@ -961,6 +975,10 @@ static int sun8i_ce_remove(struct platform_device *pdev)
 {
 	struct sun8i_ce_dev *ce = platform_get_drvdata(pdev);
 
+#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_TRNG
+	sun8i_ce_hwrng_unregister(ce);
+#endif
+
 	sun8i_ce_unregister_algs(ce);
 
 #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-trng.c new file +124 -0

124 lines not on this page · raw message

drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h +18 -0
index 2ef0c3814367..746e56c254d4 100644
@@ -12,6 +12,7 @@
 #include <linux/atomic.h>
 #include <linux/debugfs.h>
 #include <linux/crypto.h>
+#include <linux/hw_random.h>
 #include <crypto/internal/hash.h>
 #include <crypto/md5.h>
 #include <crypto/rng.h>
@@ -55,7 +56,9 @@
 #define CE_ALG_SHA256           19
 #define CE_ALG_SHA384           20
 #define CE_ALG_SHA512           21
+#define CE_ALG_TRNG		48
 #define CE_ALG_PRNG		49
+#define CE_ALG_TRNG_V2		0x1c
 #define CE_ALG_PRNG_V2		0x1d
 
 /* Used in ce_variant */
@@ -129,9 +132,12 @@ struct ce_clock {
  *				bits or words
  * @prng_t_dlen_in_bytes:	Does the request size for PRNG is in
  *				bytes or words
+ * @trng_t_dlen_in_bytes:	Does the request size for TRNG is in
+ *				bytes or words
  * @ce_clks:	list of clocks needed by this variant
  * @esr:	The type of error register
  * @prng:	The CE_ALG_XXX value for the PRNG
+ * @trng:	The CE_ALG_XXX value for the TRNG
  */
 struct ce_variant {
 	char alg_cipher[CE_ID_CIPHER_MAX];
@@ -140,9 +146,11 @@ struct ce_variant {
 	bool cipher_t_dlen_in_bytes;
 	bool hash_t_dlen_in_bits;
 	bool prng_t_dlen_in_bytes;
+	bool trng_t_dlen_in_bytes;
 	struct ce_clock ce_clks[CE_MAX_CLOCKS];
 	int esr;
 	char prng;
+	char trng;
 };
 
 struct sginfo {
@@ -218,6 +226,13 @@ struct sun8i_ce_dev {
 	struct dentry *dbgfs_dir;
 	struct dentry *dbgfs_stats;
 #endif
+#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_TRNG
+	struct hwrng trng;
+#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
+	unsigned long hwrng_stat_req;
+	unsigned long hwrng_stat_bytes;
+#endif
+#endif
 };
 
 /*
@@ -349,3 +364,6 @@ int sun8i_ce_prng_generate(struct crypto_rng *tfm, const u8 *src,
 int sun8i_ce_prng_seed(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);
 void sun8i_ce_prng_exit(struct crypto_tfm *tfm);
 int sun8i_ce_prng_init(struct crypto_tfm *tfm);
+
+int sun8i_ce_hwrng_register(struct sun8i_ce_dev *ce);
+void sun8i_ce_hwrng_unregister(struct sun8i_ce_dev *ce);
Signature · 1 line
2.26.2
#22 lore raw

Re: [PATCH v3 14/14] crypto: sun8i-ce: Add support for the TRNG

I love your patch! Perhaps something to improve:

Hi Corentin,

I love your patch! Perhaps something to improve:

[auto build test WARNING on sunxi/sunxi/for-next]
[also build test WARNING on cryptodev/master crypto/master v5.8-rc1] [cannot apply to next-20200621]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Corentin-Labbe/crypto-allwinner-add-xRNG-and-hashes/20200622-033401
base:   https://git.kernel.org/pub/scm/linux/kernel/git/sunxi/linux.git sunxi/for-next
config: sh-allmodconfig (attached as .config)
compiler: sh4-linux-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=sh

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>, old ones prefixed by <<):

>> drivers/crypto/allwinner/sun8i-ce/sun8i-ce-trng.c:22:5: warning: no previous prototype for 'sun8i_ce_trng_read' [-Wmissing-prototypes]
22 | int sun8i_ce_trng_read(struct hwrng *rng, void *data, size_t max, bool wait)
|     ^~~~~~~~~~~~~~~~~~
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-trng.c: In function 'sun8i_ce_hwrng_register':
>> drivers/crypto/allwinner/sun8i-ce/sun8i-ce-trng.c:105:24: warning: comparison is always false due to limited range of data type [-Wtype-limits]

105 | if (ce->variant->trng == CE_ID_NOTSUPP) {
| ^~
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-trng.c: In function 'sun8i_ce_hwrng_unregister':
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-trng.c:121:24: warning: comparison is always false due to limited range of data type [-Wtype-limits] 121 | if (ce->variant->trng == CE_ID_NOTSUPP)
| ^~

vim +/sun8i_ce_trng_read +22 drivers/crypto/allwinner/sun8i-ce/sun8i-ce-trng.c

  > 22	int sun8i_ce_trng_read(struct hwrng *rng, void *data, size_t max, bool wait)
    23	{
    24		struct sun8i_ce_dev *ce;
    25		dma_addr_t dma_dst;
    26		int err = 0;
    27		int flow = 3;
    28		unsigned int todo;
    29		struct sun8i_ce_flow *chan;
    30		struct ce_task *cet;
    31		u32 common;
    32		void *d;
    33	
    34		ce = container_of(rng, struct sun8i_ce_dev, trng);
    35	
    36		/* round the data length to a multiple of 32*/
    37		todo = max + 32;
    38		todo -= todo % 32;
    39	
    40		d = kzalloc(todo, GFP_KERNEL | GFP_DMA);
    41		if (!d)
    42			return -ENOMEM;
    43	
    44	#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
    45		ce->hwrng_stat_req++;
    46		ce->hwrng_stat_bytes += todo;
    47	#endif
    48	
    49		dma_dst = dma_map_single(ce->dev, d, todo, DMA_FROM_DEVICE);
    50		if (dma_mapping_error(ce->dev, dma_dst)) {
    51			dev_err(ce->dev, "Cannot DMA MAP DST\n");
    52			err = -EFAULT;
    53			goto err_dst;
    54		}
    55	
    56		err = pm_runtime_get_sync(ce->dev);
    57		if (err < 0)
    58			goto err_pm;
    59	
    60		mutex_lock(&ce->rnglock);
    61		chan = &ce->chanlist[flow];
    62	
    63		cet = &chan->tl[0];
    64		memset(cet, 0, sizeof(struct ce_task));
    65	
    66		cet->t_id = cpu_to_le32(flow);
    67		common = ce->variant->trng | CE_COMM_INT;
    68		cet->t_common_ctl = cpu_to_le32(common);
    69	
    70		/* recent CE (H6) need length in bytes, in word otherwise */
    71		if (ce->variant->trng_t_dlen_in_bytes)
    72			cet->t_dlen = cpu_to_le32(todo);
    73		else
    74			cet->t_dlen = cpu_to_le32(todo / 4);
    75	
    76		cet->t_sym_ctl = 0;
    77		cet->t_asym_ctl = 0;
    78	
    79		cet->t_dst[0].addr = cpu_to_le32(dma_dst);
    80		cet->t_dst[0].len = cpu_to_le32(todo / 4);
    81		ce->chanlist[flow].timeout = todo;
    82	
    83		err = sun8i_ce_run_task(ce, 3, "TRNG");
    84		mutex_unlock(&ce->rnglock);
    85	
    86		pm_runtime_put(ce->dev);
    87	
    88	err_pm:
    89		dma_unmap_single(ce->dev, dma_dst, todo, DMA_FROM_DEVICE);
    90	
    91		if (!err) {
    92			memcpy(data, d, max);
    93			err = max;
    94		}
    95		memzero_explicit(d, todo);
    96	err_dst:
    97		kfree(d);
    98		return err;
    99	}
   100	
   101	int sun8i_ce_hwrng_register(struct sun8i_ce_dev *ce)
   102	{
   103		int ret;
   104	
 > 105		if (ce->variant->trng == CE_ID_NOTSUPP) {

106 dev_info(ce->dev, "TRNG not supported\n");
107 return 0;
108 }
109 ce->trng.name = "sun8i Crypto Engine TRNG";
110 ce->trng.read = sun8i_ce_trng_read;
111 ce->trng.quality = 1000;
112
113 ret = hwrng_register(&ce->trng);
114 if (ret)
115 dev_err(ce->dev, "Fail to register the TRNG\n");
116 return ret;
117 }
118

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#23 lore raw

Re: [PATCH v3 14/14] crypto: sun8i-ce: Add support for the TRNG

In reply to Corentin Labbe
I love your patch! Perhaps something to improve:

Hi Corentin,

I love your patch! Perhaps something to improve:

[auto build test WARNING on sunxi/sunxi/for-next]
[also build test WARNING on cryptodev/master crypto/master v5.8-rc2] [cannot apply to next-20200621]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Corentin-Labbe/crypto-allwinner-add-xRNG-and-hashes/20200622-033401
base:   https://git.kernel.org/pub/scm/linux/kernel/git/sunxi/linux.git sunxi/for-next
config: x86_64-allyesconfig (attached as .config)
compiler: clang version 11.0.0 (https://github.com/llvm/llvm-project ef455a55bcf2cfea04a99c361b182ad18b7f03f1)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install x86_64 cross compiling tool for clang build
# apt-get install binutils-x86-64-linux-gnu
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>, old ones prefixed by <<):

>> drivers/crypto/allwinner/sun8i-ce/sun8i-ce-trng.c:22:5: warning: no previous prototype for function 'sun8i_ce_trng_read' [-Wmissing-prototypes]

int sun8i_ce_trng_read(struct hwrng *rng, void *data, size_t max, bool wait)
^
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-trng.c:22:1: note: declare 'static' if the function is not intended to be used outside of this translation unit int sun8i_ce_trng_read(struct hwrng *rng, void *data, size_t max, bool wait)
^
static

>> drivers/crypto/allwinner/sun8i-ce/sun8i-ce-trng.c:105:24: warning: result of comparison of constant 255 with expression of type 'const char' is always false [-Wtautological-constant-out-of-range-compare]

if (ce->variant->trng == CE_ID_NOTSUPP) {
~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-trng.c:121:24: warning: result of comparison of constant 255 with expression of type 'const char' is always false [-Wtautological-constant-out-of-range-compare] if (ce->variant->trng == CE_ID_NOTSUPP)
~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~
3 warnings generated.

vim +/sun8i_ce_trng_read +22 drivers/crypto/allwinner/sun8i-ce/sun8i-ce-trng.c

  > 22	int sun8i_ce_trng_read(struct hwrng *rng, void *data, size_t max, bool wait)
    23	{
    24		struct sun8i_ce_dev *ce;
    25		dma_addr_t dma_dst;
    26		int err = 0;
    27		int flow = 3;
    28		unsigned int todo;
    29		struct sun8i_ce_flow *chan;
    30		struct ce_task *cet;
    31		u32 common;
    32		void *d;
    33	
    34		ce = container_of(rng, struct sun8i_ce_dev, trng);
    35	
    36		/* round the data length to a multiple of 32*/
    37		todo = max + 32;
    38		todo -= todo % 32;
    39	
    40		d = kzalloc(todo, GFP_KERNEL | GFP_DMA);
    41		if (!d)
    42			return -ENOMEM;
    43	
    44	#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
    45		ce->hwrng_stat_req++;
    46		ce->hwrng_stat_bytes += todo;
    47	#endif
    48	
    49		dma_dst = dma_map_single(ce->dev, d, todo, DMA_FROM_DEVICE);
    50		if (dma_mapping_error(ce->dev, dma_dst)) {
    51			dev_err(ce->dev, "Cannot DMA MAP DST\n");
    52			err = -EFAULT;
    53			goto err_dst;
    54		}
    55	
    56		err = pm_runtime_get_sync(ce->dev);
    57		if (err < 0)
    58			goto err_pm;
    59	
    60		mutex_lock(&ce->rnglock);
    61		chan = &ce->chanlist[flow];
    62	
    63		cet = &chan->tl[0];
    64		memset(cet, 0, sizeof(struct ce_task));
    65	
    66		cet->t_id = cpu_to_le32(flow);
    67		common = ce->variant->trng | CE_COMM_INT;
    68		cet->t_common_ctl = cpu_to_le32(common);
    69	
    70		/* recent CE (H6) need length in bytes, in word otherwise */
    71		if (ce->variant->trng_t_dlen_in_bytes)
    72			cet->t_dlen = cpu_to_le32(todo);
    73		else
    74			cet->t_dlen = cpu_to_le32(todo / 4);
    75	
    76		cet->t_sym_ctl = 0;
    77		cet->t_asym_ctl = 0;
    78	
    79		cet->t_dst[0].addr = cpu_to_le32(dma_dst);
    80		cet->t_dst[0].len = cpu_to_le32(todo / 4);
    81		ce->chanlist[flow].timeout = todo;
    82	
    83		err = sun8i_ce_run_task(ce, 3, "TRNG");
    84		mutex_unlock(&ce->rnglock);
    85	
    86		pm_runtime_put(ce->dev);
    87	
    88	err_pm:
    89		dma_unmap_single(ce->dev, dma_dst, todo, DMA_FROM_DEVICE);
    90	
    91		if (!err) {
    92			memcpy(data, d, max);
    93			err = max;
    94		}
    95		memzero_explicit(d, todo);
    96	err_dst:
    97		kfree(d);
    98		return err;
    99	}
   100	
   101	int sun8i_ce_hwrng_register(struct sun8i_ce_dev *ce)
   102	{
   103		int ret;
   104	
 > 105		if (ce->variant->trng == CE_ID_NOTSUPP) {

106 dev_info(ce->dev, "TRNG not supported\n");
107 return 0;
108 }
109 ce->trng.name = "sun8i Crypto Engine TRNG";
110 ce->trng.read = sun8i_ce_trng_read;
111 ce->trng.quality = 1000;
112
113 ret = hwrng_register(&ce->trng);
114 if (ret)
115 dev_err(ce->dev, "Fail to register the TRNG\n");
116 return ret;
117 }
118

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org