Skip to contentSkip to conversation

hwrng: histb: fix the FIFO batch timeout

PATCH In review David Yang 2 participants 4 messages Nov 4 – Nov 13

Subsystems (2)
lore
Share & tools
lore

David Yang

author
#0 lore raw

v2 cover hwrng: histb: fix the FIFO batch timeout

The poll timeout in histb_rng_wait() was derived for a single word, while the device feeds the FIFO in batches. Fix the timeout, and print with dev_*() so that the messages name the device.

v2:
- reword
v1: https://lore.kernel.org/r/[email protected]

David Yang (2):
hwrng: histb - fix read timeout
hwrng: histb - use device messages

 drivers/char/hw_random/histb-rng.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
Signature · 1 line
2.53.0
author
#1 lore raw

PATCH v2 1/2 hwrng: histb - fix read timeout

The old figures were per-word averages, but the device produces 4 words in one go, with each unit of post-process depth taking ~200us.

The old 30ms timeout was chosen on that per-word basis. At a depth of 144, a 4-word batch may take as long as ~30ms, causing random read timeouts.

Also, a restart after rewriting the control word may take ~300ms, so use a 500ms timeout instead to cover all of the above.

Fixes a2216e187471 ("crypto: hisilicon/trng - add support for HiSTB TRNG")
---
 drivers/char/hw_random/histb-rng.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/char/hw_random/histb-rng.c +9 -4
index 445b80beed62..d435d4ab9aa7 100644
@@ -26,16 +26,21 @@ struct histb_rng_priv {
 };
 
 /*
- * Observed:
- * depth = 1 -> ~1ms
- * depth = 255 -> ~16ms
+ * Observed: the FIFO fills with 4 words at a time, roughly every 200us per
+ * unit of post-process depth, i.e. one batch per:
+ * depth = 144 -> ~30ms
+ * depth = 255 -> ~52ms
+ * Rewriting the control word delays the first batch by up to 300ms.
  */
+#define BATCH_TIMEOUT_US	(500 * USEC_PER_MSEC)
+
 static int histb_rng_wait(void __iomem *base)
 {
 	u32 val;
 
 	return readl_relaxed_poll_timeout(base + RNG_STAT, val,
-					  val & DATA_COUNT, 1000, 30 * 1000);
+					  val & DATA_COUNT, 1000,
+					  BATCH_TIMEOUT_US);
 }
 
 static void histb_rng_init(void __iomem *base, unsigned int depth)
Signature · 1 line
2.53.0
author
#2 lore raw

PATCH v2 2/2 hwrng: histb - use device messages

In reply to David Yang

pr_*() prints without saying which device the message came from. Print with dev_*() instead and adjust error messages.

---
 drivers/char/hw_random/histb-rng.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/char/hw_random/histb-rng.c +5 -2
index d435d4ab9aa7..d28cc84e8367 100644
@@ -22,6 +22,7 @@
 
 struct histb_rng_priv {
 	struct hwrng rng;
+	struct device *dev;
 	void __iomem *base;
 };
 
@@ -71,8 +72,9 @@ static int histb_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
 			if (!wait)
 				return i;
 			if (histb_rng_wait(base)) {
-				pr_err("failed to generate random number, generated %d\n",
-				       i);
+				dev_err(priv->dev,
+					"RNG timeout, generated %d of %zu bytes\n",
+					i, max);
 				return i ? i : -ETIMEDOUT;
 			}
 		}
@@ -142,6 +144,7 @@ static int histb_rng_probe(struct platform_device *pdev)
 	}
 
 	priv->base = base;
+	priv->dev = dev;
 	priv->rng.name = pdev->name;
 	priv->rng.read = histb_rng_read;
 	ret = devm_hwrng_register(dev, &priv->rng);
Signature · 1 line
2.53.0
maintainer
#3 lore raw
In reply to David Yang
Quoted 8 lines · David Yang

The poll timeout in histb_rng_wait() was derived for a single word, while the device feeds the FIFO in batches. Fix the timeout, and print with dev_*() so that the messages name the device.

v2:
- reword
v1: https://lore.kernel.org/r/[email protected]

Quoted 9 lines

David Yang (2):
hwrng: histb - fix read timeout
hwrng: histb - use device messages

 drivers/char/hw_random/histb-rng.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
Signature · 1 line
2.53.0

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