Commit 7e11a4fc authored by Tomas Marek's avatar Tomas Marek Committed by Herbert Xu

hwrng: stm32 - fix number of returned bytes on read

The stm32_rng_read() function uses `retval` variable as a counter of
generated random bytes. However, the same variable is used to store
a result of the polling function in case the driver is waiting until
the TRNG is ready. The TRNG generates random numbers by 16B. One
loop read 4B. So, the function calls the polling every 16B, i.e.
every 4th loop. The `retval` counter is reset on poll call and only
number of bytes read after the last poll call is returned to the
caller. The remaining sampled random bytes (for example 48 out of
64 in case 64 bytes are read) are not used.

Use different variable to store the polling function result and
do not overwrite `retval` counter.

Cc: Oleg Karfich <oleg.karfich@wago.com>
Signed-off-by: default avatarTomas Marek <tomas.marek@elrest.cz>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 854e25a6
...@@ -49,11 +49,13 @@ static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait) ...@@ -49,11 +49,13 @@ static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
/* Manage timeout which is based on timer and take */ /* Manage timeout which is based on timer and take */
/* care of initial delay time when enabling rng */ /* care of initial delay time when enabling rng */
if (!sr && wait) { if (!sr && wait) {
retval = readl_relaxed_poll_timeout_atomic(priv->base int ret;
ret = readl_relaxed_poll_timeout_atomic(priv->base
+ RNG_SR, + RNG_SR,
sr, sr, sr, sr,
10, 50000); 10, 50000);
if (retval) if (ret)
dev_err((struct device *)priv->rng.priv, dev_err((struct device *)priv->rng.priv,
"%s: timeout %x!\n", __func__, sr); "%s: timeout %x!\n", __func__, sr);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment