All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: miaoqing@codeaurora.org, "Jason Cooper" <jason@lakedaemon.net>,
	"Sepehrdad, Pouyan" <pouyans@qti.qualcomm.com>,
	ath9k-devel <ath9k-devel@qca.qualcomm.com>,
	"linux-wireless@vger.kernel.org" <linux-wireless@vger.kernel.org>,
	"Kalle Valo" <kvalo@kernel.org>,
	"Toke Høiland-Jørgensen" <toke@redhat.com>,
	"Dominik Brodowski" <linux@dominikbrodowski.net>,
	"Linux Crypto Mailing List" <linux-crypto@vger.kernel.org>,
	"Herbert Xu" <herbert@gondor.apana.org.au>,
	LKML <linux-kernel@vger.kernel.org>,
	Netdev <netdev@vger.kernel.org>
Cc: "Jason A. Donenfeld" <Jason@zx2c4.com>
Subject: [PATCH] ath9k: use hw_random API instead of directly dumping into random.c
Date: Tue, 15 Feb 2022 17:28:12 +0100	[thread overview]
Message-ID: <20220215162812.195716-1-Jason@zx2c4.com> (raw)
In-Reply-To: <CAHmME9r4+ENUhZ6u26rAbq0iCWoKqTPYA7=_LWbGG98KvaCE6g@mail.gmail.com>

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.

This compiles, but I have no hardware or other ability to determine
whether it works. I'll leave further development up to the ath9k
and hw_random maintainers.

Cc: Toke Høiland-Jørgensen <toke@redhat.com>
Cc: Kalle Valo <kvalo@kernel.org>
Cc: Dominik Brodowski <linux@dominikbrodowski.net>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
 drivers/net/wireless/ath/ath9k/ath9k.h |  2 +-
 drivers/net/wireless/ath/ath9k/rng.c   | 62 +++++++++-----------------
 2 files changed, 23 insertions(+), 41 deletions(-)

diff --git a/drivers/net/wireless/ath/ath9k/ath9k.h b/drivers/net/wireless/ath/ath9k/ath9k.h
index ef6f5ea06c1f..142f472903dc 100644
--- a/drivers/net/wireless/ath/ath9k/ath9k.h
+++ b/drivers/net/wireless/ath/ath9k/ath9k.h
@@ -1072,7 +1072,7 @@ struct ath_softc {
 
 #ifdef CONFIG_ATH9K_HWRNG
 	u32 rng_last;
-	struct task_struct *rng_task;
+	struct hwrng rng_ops;
 #endif
 };
 
diff --git a/drivers/net/wireless/ath/ath9k/rng.c b/drivers/net/wireless/ath/ath9k/rng.c
index aae2bd3cac69..369b222908ba 100644
--- a/drivers/net/wireless/ath/ath9k/rng.c
+++ b/drivers/net/wireless/ath/ath9k/rng.c
@@ -22,9 +22,6 @@
 #include "hw.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)
@@ -72,61 +69,46 @@ static u32 ath9k_rng_delay_get(u32 fail_stats)
 	return delay;
 }
 
-static int ath9k_rng_kthread(void *data)
+static int ath9k_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
 {
+	struct ath_softc *sc = container_of(rng, struct ath_softc, rng_ops);
 	int bytes_read;
-	struct ath_softc *sc = data;
-	u32 *rng_buf;
-	u32 delay, fail_stats = 0;
-
-	rng_buf = kmalloc_array(ATH9K_RNG_BUF_SIZE, sizeof(u32), GFP_KERNEL);
-	if (!rng_buf)
-		goto out;
-
-	while (!kthread_should_stop()) {
-		bytes_read = ath9k_rng_data_read(sc, rng_buf,
-						 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;
-		}
-
-		fail_stats = 0;
-
-		/* sleep until entropy bits under write_wakeup_threshold */
-		add_hwgenerator_randomness((void *)rng_buf, bytes_read,
-					   ATH9K_RNG_ENTROPY(bytes_read));
-	}
+	u32 fail_stats = 0;
 
-	kfree(rng_buf);
-out:
-	sc->rng_task = NULL;
+retry:
+	bytes_read = ath9k_rng_data_read(sc, buf, max);
+	if (unlikely(!bytes_read) && wait) {
+		msleep(ath9k_rng_delay_get(++fail_stats));
+		goto retry;
+	}
 
-	return 0;
+	return bytes_read;
 }
 
 void ath9k_rng_start(struct ath_softc *sc)
 {
 	struct ath_hw *ah = sc->sc_ah;
+	int ret;
 
-	if (sc->rng_task)
+	if (sc->rng_ops.read)
 		return;
 
 	if (!AR_SREV_9300_20_OR_LATER(ah))
 		return;
 
-	sc->rng_task = kthread_run(ath9k_rng_kthread, sc, "ath9k-hwrng");
-	if (IS_ERR(sc->rng_task))
-		sc->rng_task = NULL;
+	sc->rng_ops.name = "ath9k";
+	sc->rng_ops.read = ath9k_rng_read;
+	sc->rng_ops.quality = 320;
+
+	ret = devm_hwrng_register(sc->dev, &sc->rng_ops);
+	if (ret)
+		sc->rng_ops.read = NULL;
 }
 
 void ath9k_rng_stop(struct ath_softc *sc)
 {
-	if (sc->rng_task) {
-		kthread_stop(sc->rng_task);
-		sc->rng_task = NULL;
+	if (sc->rng_ops.read) {
+		devm_hwrng_unregister(sc->dev, &sc->rng_ops);
+		sc->rng_ops.read = NULL;
 	}
 }
-- 
2.35.0


  reply	other threads:[~2022-02-15 16:28 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-08 16:51 ath9k should perhaps use hw_random api? Jason A. Donenfeld
2022-02-15 15:38 ` Jason A. Donenfeld
2022-02-15 16:28   ` Jason A. Donenfeld [this message]
2022-02-15 22:55     ` [PATCH] ath9k: use hw_random API instead of directly dumping into random.c Rui Salvaterra
2022-02-15 23:22     ` Toke Høiland-Jørgensen
2022-02-15 23:52       ` Jason A. Donenfeld
2022-02-16  0:02         ` [PATCH v2] " Jason A. Donenfeld
2022-02-16  3:13           ` Florian Fainelli
2022-02-16 10:43             ` Jason A. Donenfeld
2022-02-16 11:33               ` [PATCH v3] " Jason A. Donenfeld
2022-02-16 13:27                 ` Rui Salvaterra
2022-02-17 13:51                 ` Toke Høiland-Jørgensen
2022-02-21 10:22                 ` Kalle Valo
2022-02-22 10:01                   ` Ard Biesheuvel
2022-02-22 10:13                     ` Jason A. Donenfeld
2022-02-16  5:38           ` [PATCH v2] " Kalle Valo
2022-02-16  7:15           ` Dominik Brodowski
2022-02-16  7:11         ` [PATCH] " Dominik Brodowski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220215162812.195716-1-Jason@zx2c4.com \
    --to=jason@zx2c4.com \
    --cc=ath9k-devel@qca.qualcomm.com \
    --cc=herbert@gondor.apana.org.au \
    --cc=jason@lakedaemon.net \
    --cc=kvalo@kernel.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-wireless@vger.kernel.org \
    --cc=linux@dominikbrodowski.net \
    --cc=miaoqing@codeaurora.org \
    --cc=netdev@vger.kernel.org \
    --cc=pouyans@qti.qualcomm.com \
    --cc=toke@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.