Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp749626pxb; Wed, 16 Feb 2022 03:40:50 -0800 (PST) X-Google-Smtp-Source: ABdhPJysgUQ2Puixikc3ITdErMwKn09Q5Bw7VUJphp0M6DtMmDBcGlDBExOuTI24ZUlESB512O9K X-Received: by 2002:a17:902:a5c1:b0:14e:e9ae:b3f2 with SMTP id t1-20020a170902a5c100b0014ee9aeb3f2mr2348492plq.99.1645011650280; Wed, 16 Feb 2022 03:40:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645011650; cv=none; d=google.com; s=arc-20160816; b=eeHeG6/knOecaDAEvdE9l8Nv0WlvScswfQSiG89sZZMKmrnQN7YnrDsaCN4alHBMCG H+9fOuhKoeJ0Aegy8smW3CBQ3LaOV7eJz0XiP6cpGPDQxI+FGE6v6a+VcGVySbvukxPp WEPW6xJtBmJVdbaVcB6SA2oeR/Ac5k7bxOrqbWP//5VXseG4HRs3IgIK5rfGNqjQycd2 m9pZpoXhYfIBe27YghdBPRRU9tFAf2wJlaA7eVbQmPZrge+Nd5OO8fmgyVGSqluKBSww /6d9GSup8nJCeJhVfYiSyv8FPNNoMV6agRz7/LgIVKs5iULYa6mg1TAfd0jxpCM3DOM9 GkPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=esDVBJG6N16MKK8NeeRA41EIO93bGk1xkxH0HcIomqg=; b=dhMUEYkH6iDtHQgVXKnCqA44pT8Qd/DnvUiLwOGRWJsJJIMUpJ/1IAu67xBo+E6jtQ JY51ZOh+uF8nMpp/CWYaglncsxj75TjpE80qpXfnwFM+S2lOpcH9N3O6ObcWWW+vH+jX 3P00tSIDuRQVVGHrzuYt7oXOine1D5huSV9pzfXoOsWcSXJ/7cU8Q9zZ5wJ/9ExoLUWX See8i15z1BShdvxqd3HdS7txCWqc8XsXBAQRD6Qvz188CrDEjO7QZcah62Nro9DKG5Uh l43q3GPpiqmuetCslsx3+CwQieoz+4YP1kIPFNzjruZEINd33o/xMq4EV0e8JjIV/ZvP UgWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=DIxpza7y; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zx2c4.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q4si7838198plg.411.2022.02.16.03.40.24; Wed, 16 Feb 2022 03:40:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=DIxpza7y; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zx2c4.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231754AbiBPLd4 (ORCPT + 99 others); Wed, 16 Feb 2022 06:33:56 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:48276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229479AbiBPLdz (ORCPT ); Wed, 16 Feb 2022 06:33:55 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2C92322B24; Wed, 16 Feb 2022 03:33:43 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id D9614B81E99; Wed, 16 Feb 2022 11:33:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2C708C004E1; Wed, 16 Feb 2022 11:33:39 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="DIxpza7y" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1645011217; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=esDVBJG6N16MKK8NeeRA41EIO93bGk1xkxH0HcIomqg=; b=DIxpza7yGjQIWPpdUlT5lC+osTM+1SniCEQYp/cJFXcrkiAEWSj0hSZrw4/T3AbPfk3IEx jXU8w7ZpbfuwKJuXYG24c+dqbZFZtn3Kqtj/aNQlTgKoJF4fCOvw8qYq7OWfvVwVcAcXuO KAAAfcN+y03Bi5G4x+xQXOfzXdxCDnQ= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 3cd753ef (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO); Wed, 16 Feb 2022 11:33:37 +0000 (UTC) From: "Jason A. Donenfeld" To: miaoqing@codeaurora.org, Rui Salvaterra , =?UTF-8?q?Toke=20H=C3=B8iland-J=C3=B8rgensen?= , "Sepehrdad, Pouyan" , ath9k-devel , "linux-wireless@vger.kernel.org" , Kalle Valo , Dominik Brodowski , Linux Crypto Mailing List , Herbert Xu , LKML , Netdev , =?UTF-8?q?Toke=20H=C3=B8iland-J=C3=B8rgensen?= , Florian Fainelli Cc: "Jason A. Donenfeld" Subject: [PATCH v3] ath9k: use hw_random API instead of directly dumping into random.c Date: Wed, 16 Feb 2022 12:33:23 +0100 Message-Id: <20220216113323.53332-1-Jason@zx2c4.com> In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org 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 Cc: Kalle Valo Cc: Rui Salvaterra Cc: Dominik Brodowski Cc: Herbert Xu Signed-off-by: Jason A. Donenfeld --- Changes v2->v3: - Use msleep_interruptable like other hwrng drivers. - Give up after 110 tries. - Return -EIO after giving up like other hwrng drivers. - Use for loop for style nits. - Append serial number for driver in case of multiple cards. Changes v1->v2: - Count in words rather than bytes. drivers/net/wireless/ath/ath9k/ath9k.h | 3 +- drivers/net/wireless/ath/ath9k/rng.c | 72 +++++++++++--------------- 2 files changed, 33 insertions(+), 42 deletions(-) diff --git a/drivers/net/wireless/ath/ath9k/ath9k.h b/drivers/net/wireless/ath/ath9k/ath9k.h index ef6f5ea06c1f..3ccf8cfc6b63 100644 --- a/drivers/net/wireless/ath/ath9k/ath9k.h +++ b/drivers/net/wireless/ath/ath9k/ath9k.h @@ -1071,8 +1071,9 @@ struct ath_softc { #endif #ifdef CONFIG_ATH9K_HWRNG + struct hwrng rng_ops; u32 rng_last; - struct task_struct *rng_task; + char rng_name[sizeof("ath9k_65535")]; #endif }; diff --git a/drivers/net/wireless/ath/ath9k/rng.c b/drivers/net/wireless/ath/ath9k/rng.c index f9d3d6eedd3c..cb5414265a9b 100644 --- a/drivers/net/wireless/ath/ath9k/rng.c +++ b/drivers/net/wireless/ath/ath9k/rng.c @@ -21,11 +21,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) { int i, j; @@ -71,61 +66,56 @@ 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) { - 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; + struct ath_softc *sc = container_of(rng, struct ath_softc, rng_ops); + u32 fail_stats = 0, word; + int bytes_read = 0; + + for (;;) { + if (max & ~3UL) + bytes_read = ath9k_rng_data_read(sc, buf, max >> 2); + if ((max & 3UL) && ath9k_rng_data_read(sc, &word, 1)) { + memcpy(buf + bytes_read, &word, max & 3UL); + bytes_read += max & 3UL; + memzero_explicit(&word, sizeof(word)); } + if (!wait || !max || likely(bytes_read) || fail_stats > 110) + break; - fail_stats = 0; - - /* sleep until entropy bits under write_wakeup_threshold */ - add_hwgenerator_randomness((void *)rng_buf, bytes_read, - ATH9K_RNG_ENTROPY(bytes_read)); + msleep_interruptible(ath9k_rng_delay_get(++fail_stats)); } - kfree(rng_buf); -out: - sc->rng_task = NULL; - - return 0; + if (wait && !bytes_read && max) + bytes_read = -EIO; + return bytes_read; } void ath9k_rng_start(struct ath_softc *sc) { + static atomic_t serial = ATOMIC_INIT(0); struct ath_hw *ah = sc->sc_ah; - 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; + snprintf(sc->rng_name, sizeof(sc->rng_name), "ath9k_%u", + (atomic_inc_return(&serial) - 1) & U16_MAX); + 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) { - 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