Commit fcd09c90 authored by Jason A. Donenfeld's avatar Jason A. Donenfeld Committed by Kalle Valo

ath9k: use hw_random API instead of directly dumping into random.c

Hardware random number generators are supposed to use the hw_random
framework. This commit turns ath9k's kthread-based design into a proper
hw_random driver.

Cc: Toke Høiland-Jørgensen <toke@redhat.com>
Cc: Kalle Valo <kvalo@kernel.org>
Cc: Rui Salvaterra <rsalvaterra@gmail.com>
Cc: Dominik Brodowski <linux@dominikbrodowski.net>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: default avatarJason A. Donenfeld <Jason@zx2c4.com>
Tested-by: default avatarRui Salvaterra <rsalvaterra@gmail.com>
Acked-by: default avatarToke Høiland-Jørgensen <toke@toke.dk>
Signed-off-by: default avatarKalle Valo <quic_kvalo@quicinc.com>
Link: https://lore.kernel.org/r/20220216113323.53332-1-Jason@zx2c4.com
parent 03e0add7
...@@ -1071,8 +1071,9 @@ struct ath_softc { ...@@ -1071,8 +1071,9 @@ struct ath_softc {
#endif #endif
#ifdef CONFIG_ATH9K_HWRNG #ifdef CONFIG_ATH9K_HWRNG
struct hwrng rng_ops;
u32 rng_last; u32 rng_last;
struct task_struct *rng_task; char rng_name[sizeof("ath9k_65535")];
#endif #endif
}; };
......
...@@ -21,11 +21,6 @@ ...@@ -21,11 +21,6 @@
#include "hw.h" #include "hw.h"
#include "ar9003_phy.h" #include "ar9003_phy.h"
#define ATH9K_RNG_BUF_SIZE 320
#define ATH9K_RNG_ENTROPY(x) (((x) * 8 * 10) >> 5) /* quality: 10/32 */
static DECLARE_WAIT_QUEUE_HEAD(rng_queue);
static int ath9k_rng_data_read(struct ath_softc *sc, u32 *buf, u32 buf_size) static int ath9k_rng_data_read(struct ath_softc *sc, u32 *buf, u32 buf_size)
{ {
int i, j; int i, j;
...@@ -71,61 +66,56 @@ static u32 ath9k_rng_delay_get(u32 fail_stats) ...@@ -71,61 +66,56 @@ static u32 ath9k_rng_delay_get(u32 fail_stats)
return delay; return delay;
} }
static int ath9k_rng_kthread(void *data) static int ath9k_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
{ {
int bytes_read; struct ath_softc *sc = container_of(rng, struct ath_softc, rng_ops);
struct ath_softc *sc = data; u32 fail_stats = 0, word;
u32 *rng_buf; int bytes_read = 0;
u32 delay, fail_stats = 0;
for (;;) {
rng_buf = kmalloc_array(ATH9K_RNG_BUF_SIZE, sizeof(u32), GFP_KERNEL); if (max & ~3UL)
if (!rng_buf) bytes_read = ath9k_rng_data_read(sc, buf, max >> 2);
goto out; if ((max & 3UL) && ath9k_rng_data_read(sc, &word, 1)) {
memcpy(buf + bytes_read, &word, max & 3UL);
while (!kthread_should_stop()) { bytes_read += max & 3UL;
bytes_read = ath9k_rng_data_read(sc, rng_buf, memzero_explicit(&word, sizeof(word));
ATH9K_RNG_BUF_SIZE);
if (unlikely(!bytes_read)) {
delay = ath9k_rng_delay_get(++fail_stats);
wait_event_interruptible_timeout(rng_queue,
kthread_should_stop(),
msecs_to_jiffies(delay));
continue;
} }
if (!wait || !max || likely(bytes_read) || fail_stats > 110)
break;
fail_stats = 0; msleep_interruptible(ath9k_rng_delay_get(++fail_stats));
/* sleep until entropy bits under write_wakeup_threshold */
add_hwgenerator_randomness((void *)rng_buf, bytes_read,
ATH9K_RNG_ENTROPY(bytes_read));
} }
kfree(rng_buf); if (wait && !bytes_read && max)
out: bytes_read = -EIO;
sc->rng_task = NULL; return bytes_read;
return 0;
} }
void ath9k_rng_start(struct ath_softc *sc) void ath9k_rng_start(struct ath_softc *sc)
{ {
static atomic_t serial = ATOMIC_INIT(0);
struct ath_hw *ah = sc->sc_ah; struct ath_hw *ah = sc->sc_ah;
if (sc->rng_task) if (sc->rng_ops.read)
return; return;
if (!AR_SREV_9300_20_OR_LATER(ah)) if (!AR_SREV_9300_20_OR_LATER(ah))
return; return;
sc->rng_task = kthread_run(ath9k_rng_kthread, sc, "ath9k-hwrng"); snprintf(sc->rng_name, sizeof(sc->rng_name), "ath9k_%u",
if (IS_ERR(sc->rng_task)) (atomic_inc_return(&serial) - 1) & U16_MAX);
sc->rng_task = NULL; sc->rng_ops.name = sc->rng_name;
sc->rng_ops.read = ath9k_rng_read;
sc->rng_ops.quality = 320;
if (devm_hwrng_register(sc->dev, &sc->rng_ops))
sc->rng_ops.read = NULL;
} }
void ath9k_rng_stop(struct ath_softc *sc) void ath9k_rng_stop(struct ath_softc *sc)
{ {
if (sc->rng_task) { if (sc->rng_ops.read) {
kthread_stop(sc->rng_task); devm_hwrng_unregister(sc->dev, &sc->rng_ops);
sc->rng_task = NULL; sc->rng_ops.read = NULL;
} }
} }
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