Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp5583130ybe; Tue, 10 Sep 2019 06:05:14 -0700 (PDT) X-Google-Smtp-Source: APXvYqwsTU0PPnTENcSazxOy97SmA7lJ+VCvZPClVwXfth3x3cHfhbAjOsB4P/Zb/VHUQEMcFXEJ X-Received: by 2002:a17:906:6dd4:: with SMTP id j20mr24726088ejt.173.1568120714129; Tue, 10 Sep 2019 06:05:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568120714; cv=none; d=google.com; s=arc-20160816; b=c8dR/Ei/pcqp7BHkNS25Vmungc1UcIu9+xqufxZ94S68SgfbBAq5T9SzRLNJZCYFWr T5uZIkuFvvvX1HEq7iix5E8CiWQh32PxhjSOOVv5f7dpL5DWkSln8FWU9A4ECa93kzAG odspjZl5Zh7dEHI3YHqq2TzNd6ODcv14yT3C4sTehXiS+Zi8zHZ7f6tYlwIgZ5eOTDkB V1hjkw7I7ZnrVClYZwhUo1nWQKB4MvNSmp53oseD25GgMW5nO5yOvgvVKMlUGa8Xcca+ VWMOSQZDWbTn0Zz+EKm7p9oSVO9D0S8JUEwVV5CC2/+f3lNQ2MwFq1CEHL633BJFziCe xInw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=0GF5M/Z2kwzroH5gkbeDVOgMn2NsaXM05m+OrHSEuCs=; b=KzD73YPA8elBRdWdQ2bfUZg6AU07uZyl80rsOufMpJsM73+H6cx2X5O08bzt0xCW5S FPH97qIkTaZa1QS5mSqZO0CbOOuIAwec+v9rIVpZPc+mcx8sGiH9st5xQLjBPAze1NtG KKGPLvXLED3XAPjK8PHntn12cRLsuD+i27J/oopxxSIL0w+SwHVI8Fm4CfLWkqyhvCsw VMEK+4sr3yfA23QgacaFLmPmSW/Z9T+JKulxKKGRrwg7P8ymLfhhwq/Q/kFWGc2OSIda PWnzHYJbOLw0LcIdaRj/wsv0lBxWANtYFubVaFhYj9Yd9Bhu/tP0KnmBFmzgIm3N45cf ndiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="dXbNB/04"; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n2si8839621ejy.35.2019.09.10.06.04.42; Tue, 10 Sep 2019 06:05:14 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="dXbNB/04"; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728897AbfIJL32 (ORCPT + 99 others); Tue, 10 Sep 2019 07:29:28 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:38135 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726073AbfIJL32 (ORCPT ); Tue, 10 Sep 2019 07:29:28 -0400 Received: by mail-wr1-f67.google.com with SMTP id l11so19436269wrx.5 for ; Tue, 10 Sep 2019 04:29:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=0GF5M/Z2kwzroH5gkbeDVOgMn2NsaXM05m+OrHSEuCs=; b=dXbNB/047jTSQr0PWNuw4HHchJDoL9QVKE4SzJOfcd/2dkDcfO2I6cmT5ioFlh705T SkueVrhEAx2PY7702X8M7/BLSG2OjBDoLNNyyq2HlDOwzFB+SGXLnhIiFh77IOJWyJmz m9dZOk+EckrHOMo8E6SK7wlshR/Wgm71GJAf51akQPwmkEs70BRJiAS18PeSbCFcBSve 0qX2CaXAWt5MXgDyNFhdLcPO/LEsqyf3BaQC8Urp74uXYcmQwnlnXqtT8SRuQC5ipFqr WdkSJYYZWFM1zhKEhSt4FCk1XOFDwjWJf/TaI8sl7Zy7hDZwU/Q+H3miLneJCPtlc5NS eSRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=0GF5M/Z2kwzroH5gkbeDVOgMn2NsaXM05m+OrHSEuCs=; b=azzy/ZPr2MWWnbO2O66S9mx70dzi+hNThTlSrrF7Xg2lCEjUtH7SXDUay4Wk/OiW8I ZgL0CE/D2UDNDdvlJUwnNznCOn+OZGleKr2+XH6mKFCjH7FSWXAvSLKNsG/niuCsYDTr km03V6HzD5Z6eTPBjrqFHUIaWnPmSujGSP+giW2VKjo3TTxLh10prBOGDsTLPycg0gex TYZnkzdDHjqimkJyR4Kux4NDVYZvRrhKSfsjztt3QIy64ZvTeJLcWWas6n+vXgXwSh70 U5mw7hofZTbEGlUAPeohv09vHxDwltoQ2l2AM24ohnZA6s2xs0lpG3kFO8PLRJMQhYR0 B6pw== X-Gm-Message-State: APjAAAUBfkwZ28L6ILkA7Ti6U3kxQCa/+Q4Jp04G+3KOH/+xGl9T6RLf nHDtDhNI8DnuDFoEu4l0fHg7Fw== X-Received: by 2002:adf:f812:: with SMTP id s18mr27406526wrp.32.1568114964889; Tue, 10 Sep 2019 04:29:24 -0700 (PDT) Received: from holly.lan (cpc141214-aztw34-2-0-cust773.18-1.cable.virginm.net. [86.9.19.6]) by smtp.gmail.com with ESMTPSA id q14sm37141616wrc.77.2019.09.10.04.29.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Sep 2019 04:29:24 -0700 (PDT) Date: Tue, 10 Sep 2019 12:29:22 +0100 From: Daniel Thompson To: Tomer Maimon Cc: mpm@selenic.com, herbert@gondor.apana.org.au, Arnd Bergmann , Greg KH , Rob Herring , Mark Rutland , Avi Fishman , Tali Perry , Patrick Venture , Nancy Yuen , Benjamin Fair , sumit.garg@linaro.org, jens.wiklander@linaro.org, vkoul@kernel.org, Thomas Gleixner , Joel Stanley , devicetree , Linux Kernel Mailing List , linux-crypto@vger.kernel.org, OpenBMC Maillist Subject: Re: [PATCH v1 2/2] hwrng: npcm: add NPCM RNG driver Message-ID: <20190910112922.xqho33smx6zsmank@holly.lan> References: <20190828162617.237398-1-tmaimon77@gmail.com> <20190828162617.237398-3-tmaimon77@gmail.com> <20190829104721.tnjk3bqt3cq6iagr@holly.lan> <20190909151033.f3inbbas4duzsmh5@holly.lan> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20180716 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org On Tue, Sep 10, 2019 at 01:52:35PM +0300, Tomer Maimon wrote: > Hi Daniel, > > Thanks for your prompt reply, > > > > On Mon, 9 Sep 2019 at 18:10, Daniel Thompson > wrote: > > > On Mon, Sep 09, 2019 at 05:31:30PM +0300, Tomer Maimon wrote: > > > Hi Daniel, > > > > > > appreciate your comments and sorry for the late reply > > > > > > On Thu, 29 Aug 2019 at 13:47, Daniel Thompson < > > daniel.thompson@linaro.org> > > > wrote: > > > > > > > On Wed, Aug 28, 2019 at 07:26:17PM +0300, Tomer Maimon wrote: > > > > > Add Nuvoton NPCM BMC Random Number Generator(RNG) driver. > > > > > > > > > > Signed-off-by: Tomer Maimon > > > > > --- > > > > > drivers/char/hw_random/Kconfig | 13 ++ > > > > > drivers/char/hw_random/Makefile | 1 + > > > > > drivers/char/hw_random/npcm-rng.c | 207 > > ++++++++++++++++++++++++++++++ > > > > > 3 files changed, 221 insertions(+) > > > > > create mode 100644 drivers/char/hw_random/npcm-rng.c > > > > > > > > > > diff --git a/drivers/char/hw_random/npcm-rng.c > > > > b/drivers/char/hw_random/npcm-rng.c > > > > > new file mode 100644 > > > > > index 000000000000..5b4b1b6cb362 > > > > > --- /dev/null > > > > > +++ b/drivers/char/hw_random/npcm-rng.c > > > > > @@ -0,0 +1,207 @@ > > > > > +// SPDX-License-Identifier: GPL-2.0 > > > > > +// Copyright (c) 2019 Nuvoton Technology corporation. > > > > > + > > > > > +#include > > > > > +#include > > > > > +#include > > > > > +#include > > > > > +#include > > > > > +#include > > > > > +#include > > > > > +#include > > > > > +#include > > > > > +#include > > > > > +#include > > > > > +#include > > > > > + > > > > > +#define NPCM_RNGCS_REG 0x00 /* Control and status > > > > register */ > > > > > +#define NPCM_RNGD_REG 0x04 /* Data register */ > > > > > +#define NPCM_RNGMODE_REG 0x08 /* Mode register */ > > > > > + > > > > > +#define NPCM_RNG_CLK_SET_25MHZ GENMASK(4, 3) /* 20-25 MHz */ > > > > > +#define NPCM_RNG_DATA_VALID BIT(1) > > > > > +#define NPCM_RNG_ENABLE BIT(0) > > > > > +#define NPCM_RNG_M1ROSEL BIT(1) > > > > > + > > > > > +#define NPCM_RNG_TIMEOUT_POLL 20 > > > > > > > > Might be better to define this in real-world units (such as > > > > milliseconds) since the timeout is effectively the longest time the > > > > hardware can take to generate 4 bytes. > > > > > > > > > + > > > > > +#define to_npcm_rng(p) container_of(p, struct npcm_rng, rng) > > > > > + > > > > > +struct npcm_rng { > > > > > + void __iomem *base; > > > > > + struct hwrng rng; > > > > > +}; > > > > > + > > > > > +static int npcm_rng_init(struct hwrng *rng) > > > > > +{ > > > > > + struct npcm_rng *priv = to_npcm_rng(rng); > > > > > + u32 val; > > > > > + > > > > > + val = readl(priv->base + NPCM_RNGCS_REG); > > > > > + val |= NPCM_RNG_ENABLE; > > > > > + writel(val, priv->base + NPCM_RNGCS_REG); > > > > > + > > > > > + return 0; > > > > > +} > > > > > + > > > > > +static void npcm_rng_cleanup(struct hwrng *rng) > > > > > +{ > > > > > + struct npcm_rng *priv = to_npcm_rng(rng); > > > > > + u32 val; > > > > > + > > > > > + val = readl(priv->base + NPCM_RNGCS_REG); > > > > > + val &= ~NPCM_RNG_ENABLE; > > > > > + writel(val, priv->base + NPCM_RNGCS_REG); > > > > > +} > > > > > + > > > > > +static bool npcm_rng_wait_ready(struct hwrng *rng, bool wait) > > > > > +{ > > > > > + struct npcm_rng *priv = to_npcm_rng(rng); > > > > > + int timeout_cnt = 0; > > > > > + int ready; > > > > > + > > > > > + ready = readl(priv->base + NPCM_RNGCS_REG) & > > NPCM_RNG_DATA_VALID; > > > > > + while ((ready == 0) && (timeout_cnt < NPCM_RNG_TIMEOUT_POLL)) { > > > > > + usleep_range(500, 1000); > > > > > + ready = readl(priv->base + NPCM_RNGCS_REG) & > > > > > + NPCM_RNG_DATA_VALID; > > > > > + timeout_cnt++; > > > > > + } > > > > > + > > > > > + return !!ready; > > > > > +} > > > > > > > > This looks like an open-coded version of readl_poll_timeout()... better > > > > to use the library function. > > > > > > > > Also the sleep looks a bit long to me. What is the generation rate of > > > > the peripheral? Most RNG drivers have short intervals between data > > > > generation so they use delays rather than sleeps (a.k.a. > > > > readl_poll_timeout_atomic() ). > > > > > > the HWRNG generate byte of random data in a few milliseconds so it is > > > better to use the sleep command. > > > > That's fine, just use readl_poll_timeout() then. > > > > > > > > > + > > > > > +static int npcm_rng_read(struct hwrng *rng, void *buf, size_t max, > > bool > > > > wait) > > > > > +{ > > > > > + struct npcm_rng *priv = to_npcm_rng(rng); > > > > > + int retval = 0; > > > > > + > > > > > + pm_runtime_get_sync((struct device *)priv->rng.priv); > > > > > + > > > > > + while (max >= sizeof(u32)) { > > > > > + if (!npcm_rng_wait_ready(rng, wait)) > > > > > + break; > > > > > > > > The code as currently written does not honour the wait parameter (e.g. > > > > it sleeps even when wait is false). > > > > > > > > > > > > > + > > > > > + *(u32 *)buf = readl(priv->base + NPCM_RNGD_REG); > > > > > + retval += sizeof(u32); > > > > > + buf += sizeof(u32); > > > > > + max -= sizeof(u32); > > > > > + } > > > > > + > > > > > + pm_runtime_mark_last_busy((struct device *)priv->rng.priv); > > > > > + pm_runtime_put_sync_autosuspend((struct device > > *)priv->rng.priv); > > > > > + > > > > > + return retval || !wait ? retval : -EIO; > > > > > +} > > > > > + > > > > > +static int npcm_rng_probe(struct platform_device *pdev) > > > > > +{ > > > > > + struct npcm_rng *priv; > > > > > + struct resource *res; > > > > > + u32 quality; > > > > > + int ret; > > > > > + > > > > > + priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); > > > > > + if (!priv) > > > > > + return -ENOMEM; > > > > > + > > > > > + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); > > > > > + priv->base = devm_ioremap_resource(&pdev->dev, res); > > > > > + if (IS_ERR(priv->base)) > > > > > + return PTR_ERR(priv->base); > > > > > + > > > > > + priv->rng.name = pdev->name; > > > > > +#ifndef CONFIG_PM > > > > > + priv->rng.init = npcm_rng_init; > > > > > + priv->rng.cleanup = npcm_rng_cleanup; > > > > > +#endif > > > > > + priv->rng.read = npcm_rng_read; > > > > > + priv->rng.priv = (unsigned long)&pdev->dev; > > > > > + if (of_property_read_u32(pdev->dev.of_node, "quality", > > &quality)) > > > > > + priv->rng.quality = 1000; > > > > > + else > > > > > + priv->rng.quality = quality; > > > > > + > > > > > + writel(NPCM_RNG_M1ROSEL, priv->base + NPCM_RNGMODE_REG); > > > > > +#ifndef CONFIG_PM > > > > > + writel(NPCM_RNG_CLK_SET_25MHZ, priv->base + NPCM_RNGCS_REG); > > > > > +#else > > > > > + writel(NPCM_RNG_CLK_SET_25MHZ | NPCM_RNG_ENABLE, > > > > > + priv->base + NPCM_RNGCS_REG); > > > > > +#endif > > > > > > > > If this initialization was moved to npcm_rng_init() then there would be > > > > no need for the additional ifdefing. It would also get rid of the > > > > (potentially slow) readl calls on the PM wakeup path. > > > > > > > > > > But when the Kernel have PM configuration than the priv->rng.init is not > > > set and > > > *add_early_randomness* function is called. for the *add_early_randomness* > > > success > > > the hwrng need to enabled in the probe. > > > > Sorry but I don't understand this reply. > > > > When CONFIG_PM is enabled then the probe function does not currently set > > NPCM_RNG_ENABLE; instead is relies on npcm_rng_init() being called by > > > > Sorry maybe I miss understood, but when the CONFIG_PM enabled so the > NPCM_RNG_ENABLE sets (the code use ifndef and not ifdef) > *#ifndef CONFIG_PM* > writel(NPCM_RNG_CLK_SET_25MHZ, priv->base + NPCM_RNGCS_REG); > #else (*CONFIG_PM enabled*) > writel(NPCM_RNG_CLK_SET_25MHZ | NPCM_RNG_ENABLE, > priv->base + NPCM_RNGCS_REG); > #endif > > And the hwrng needed to be enabled to run *add_early_randomness *function > successfully. > > If the hwrng driver will relay on PM logic to enable the hwrng will be > disable when *add_early_randomness *function is called. > > the PM logic (as part of pm_runtime_get_sync() ). I ended up reading my mail out of order and replied to the v2 patch. The question is *why* the driver doesn't resume properly when adding early randomness! I think it is because the hwrng_register() is being called before PM runtime is enabled. Daniel.