Skip to contentSkip to conversation

hwrng: cn10k - Optimize random number generator driver

PATCH In review Vladis Dronov 2 participants 5 messages Apr 13, 2022 – Apr 21, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover hwrng: cn10k - Optimize random number generator driver

Hi,

I'm suggesting couple of tweaks and fixes for the CN10K Random Number Generator driver. Main points are: fix a logic, optimize a loop and make an error message more informative.

Vladis Dronov (2):
hwrng: cn10k - Optimize cn10k_rng_read()
hwrng: cn10k - Make check_rng_health() return an error code

 drivers/char/hw_random/cn10k-rng.c | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)
#1 lore raw

PATCH 1/2 hwrng: cn10k - Optimize cn10k_rng_read()

This function assumes that sizeof(void) is 1 and arithmetic works for void pointers. This is a GNU C extention and may not work with other compilers. Change this by using an u8 pointer.

Also move cn10k_read_trng() out of a loop thus saving some cycles.

Fixes 38e9791a0209 ("hwrng: cn10k - Add random number generator support")
---
 drivers/char/hw_random/cn10k-rng.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
drivers/char/hw_random/cn10k-rng.c +10 -6
index 35001c63648b..dd226630b67d 100644
@@ -90,6 +90,7 @@ static int cn10k_rng_read(struct hwrng *hwrng, void *data,
 {
 	struct cn10k_rng *rng = (struct cn10k_rng *)hwrng->priv;
 	unsigned int size;
+	u8 *pos = data;
 	int err = 0;
 	u64 value;
 
@@ -102,17 +103,20 @@ static int cn10k_rng_read(struct hwrng *hwrng, void *data,
 	while (size >= 8) {
 		cn10k_read_trng(rng, &value);
 
-		*((u64 *)data) = (u64)value;
+		*((u64 *)pos) = value;
 		size -= 8;
-		data += 8;
+		pos += 8;
 	}
 
-	while (size > 0) {
+	if (size > 0) {
 		cn10k_read_trng(rng, &value);
 
-		*((u8 *)data) = (u8)value;
-		size--;
-		data++;
+		while (size > 0) {
+			*pos = (u8)value;
+			value >>= 8;
+			size--;
+			pos++;
+		}
 	}
 
 	return max - size;
Signature · 1 line
2.35.1
#2 lore raw

PATCH 2/2 hwrng: cn10k - Make check_rng_health() return an error code

In reply to Vladis Dronov

Currently check_rng_health() returns zero unconditionally. Make it to output an error code and return it.

Fixes 38e9791a0209 ("hwrng: cn10k - Add random number generator support")
---
 drivers/char/hw_random/cn10k-rng.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
drivers/char/hw_random/cn10k-rng.c +7 -8
index dd226630b67d..a01e9307737c 100644
@@ -31,26 +31,23 @@ struct cn10k_rng {
 
 #define PLAT_OCTEONTX_RESET_RNG_EBG_HEALTH_STATE     0xc2000b0f
 
-static int reset_rng_health_state(struct cn10k_rng *rng)
+static unsigned long reset_rng_health_state(struct cn10k_rng *rng)
 {
 	struct arm_smccc_res res;
 
 	/* Send SMC service call to reset EBG health state */
 	arm_smccc_smc(PLAT_OCTEONTX_RESET_RNG_EBG_HEALTH_STATE, 0, 0, 0, 0, 0, 0, 0, &res);
-	if (res.a0 != 0UL)
-		return -EIO;
-
-	return 0;
+	return res.a0;
 }
 
 static int check_rng_health(struct cn10k_rng *rng)
 {
 	u64 status;
-	int err;
+	unsigned long err;
 
 	/* Skip checking health */
 	if (!rng->reg_base)
-		return 0;
+		return -ENODEV;
 
 	status = readq(rng->reg_base + RNM_PF_EBG_HEALTH);
 	if (status & BIT_ULL(20)) {
@@ -58,7 +55,9 @@ static int check_rng_health(struct cn10k_rng *rng)
 		if (err) {
 			dev_err(&rng->pdev->dev, "HWRNG: Health test failed (status=%llx)\n",
 					status);
-			dev_err(&rng->pdev->dev, "HWRNG: error during reset\n");
+			dev_err(&rng->pdev->dev, "HWRNG: error during reset (error=%lx)\n",
+					err);
+			return -EIO;
 		}
 	}
 	return 0;
Signature · 1 line
2.35.1
maintainer
#3 lore raw
In reply to Vladis Dronov
Quoted 6 lines · Vladis Dronov

Hi,

I'm suggesting couple of tweaks and fixes for the CN10K Random Number Generator driver. Main points are: fix a logic, optimize a loop and make an error message more informative.

Quoted 6 lines

Vladis Dronov (2):
hwrng: cn10k - Optimize cn10k_rng_read()
hwrng: cn10k - Make check_rng_health() return an error code

 drivers/char/hw_random/cn10k-rng.c | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)

All applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#4 lore raw
Quoted 2 lines · Herbert Xu

All applied. Thanks.

Thanks a ton, Herber!

Best regards,
Vladis