Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp5314285ybe; Tue, 10 Sep 2019 01:35:56 -0700 (PDT) X-Google-Smtp-Source: APXvYqyN9FzSleqvHU1axtuFxYTHYyu36KqxIjBGNSq/tcbAwTTiBb0K5J7mtfPqT16LsQaEVukO X-Received: by 2002:a17:906:b34a:: with SMTP id cd10mr23606107ejb.300.1568104556667; Tue, 10 Sep 2019 01:35:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568104556; cv=none; d=google.com; s=arc-20160816; b=hoNa2frNioKnKaZPwT/Co3OQwYggZ5PhKC1rUlq4T0iRT2dFDz6A9fIX+LHuGCFHOl 6VkX5xghaWj0PA52jS8PvaAXjLNxnim+PalzycM6vACJ20MaTIs/FjkorOnzhaI8QHtw sdGk6dHFS66+eIvK70PtaXhgV9VdxVGWI7ebmvspA8oLQcBCyiIp95nYcxrp7Os9T8Hc a1oFtkZdu72DbsvHXJv1N4oX7+/SZjqrY5gyoIAdv6pQxQub6sgxe01dxjcg/R6n8Vgq z31LtSXvW7nJNMh+x9mCtYx2dUxuFWKWyOrxVZYD4f+qL9XjUN0u+NeAZVoC593YdajZ 9CxQ== 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=sNBY+Xlzyd2k0VKUgwFYB3mkBB2wr1jCJnHQkHU/vRc=; b=nkIWsLi6GlPX+XMfH63S8ytjg1oV+Tf8o3QOHAd0eaApL9ghn/6nLbaykayGiTsgmU zPS80M6IBXlGG4z26qRPV5gj70fB/vGdWmATES92yS+acFwegsbPbc4aFsIiw/FZ6QoC VHDqEhBh54TwTR7ve969NuomQ/JN2ZJsT9tSqtBBl7PJ5hnFjHKfd/zLGhQ2tOGUzR63 Ox/lLg2AfR2hxn2pTJ4UlUF5PcKm0g3M8BkMIOaNAbvDNQDoUd/ygs2oVLOPLDzg9i9v qHmW4yCZY3UuRJXNO9rzcTbR+8rDOKSYURsXIp72d2xdKW7ejFdZ1kajio63lDCEM7vm y7Zw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=T9wK89NI; 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 m26si8937135eji.61.2019.09.10.01.35.29; Tue, 10 Sep 2019 01:35:56 -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=T9wK89NI; 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 S1728249AbfIIPKk (ORCPT + 99 others); Mon, 9 Sep 2019 11:10:40 -0400 Received: from mail-wm1-f68.google.com ([209.85.128.68]:38570 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726899AbfIIPKk (ORCPT ); Mon, 9 Sep 2019 11:10:40 -0400 Received: by mail-wm1-f68.google.com with SMTP id o184so15125160wme.3 for ; Mon, 09 Sep 2019 08:10:37 -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=sNBY+Xlzyd2k0VKUgwFYB3mkBB2wr1jCJnHQkHU/vRc=; b=T9wK89NInM5L0iMeCYgbJKr09ZPQNzaBRk389o67nDwq2gC5USDMzIDm0QLB13jDY4 QVihZJnVSX0u1jO2JW8Ml+FnBP2mjI8CzyHOULyx9TAPQbVu7/nJwqrzNEaYg0zAuFd6 k42SOXMZiTOM06vckYunQvAAX+F3eEA5AKyFB58/RCO0rgbRJBGackWrMzWUohdRUK69 XsqWmuUrNLsB0/yUegaHrrApxuaBTc+iUAjxU2m7gsC+bsTMXYf5W83amQDTOu8EEP5H JNFL4QG9E2jkdlIxyuRHru7Vdl9mDmuJubY1d5Mdi1W6hMwtZAj2LrpHKmMSh9qp1XCy 3HVQ== 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=sNBY+Xlzyd2k0VKUgwFYB3mkBB2wr1jCJnHQkHU/vRc=; b=czNazn8z9/4ydz1xEwpF2OrMM2CRU1kq7C/WZ0Oi/Rhmk2CVeIwRvlVn8rDuqt+N6Z YHpQMSeQqc8eyoOnlCcoomSYf5/s4rz6xJWOtiOAV6jQVzWCjhcbf4rK0lUUaDp2Uk20 90eual0QlHNt1Uf5KtIqS2s0Bf3QrYPBxyJPh3Kl35jTosyZuzL9RTdiJhct82edFJbO wR2U+o+xUVaGlCFjiz3B8SmHm47vxUNhJJzx/aptC28766AQgqMSZTh2Ij6mYrZeETt0 rc562zFtVSHKfMrugj72bpulMjqrkukbd0cmyBL+b6Fwnoiii+68INe3+n/cCWQr/usl c7yw== X-Gm-Message-State: APjAAAW+JP24dVN7LJ8YocXA7s2VkuNSuadmm90DXQup2ONcH560d5vc RCstD5CmfunhRPYI3esUnX+9RQ== X-Received: by 2002:a05:600c:34d:: with SMTP id u13mr18970221wmd.97.1568041837014; Mon, 09 Sep 2019 08:10:37 -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 g15sm13921575wmk.17.2019.09.09.08.10.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Sep 2019 08:10:35 -0700 (PDT) Date: Mon, 9 Sep 2019 16:10:33 +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: <20190909151033.f3inbbas4duzsmh5@holly.lan> References: <20190828162617.237398-1-tmaimon77@gmail.com> <20190828162617.237398-3-tmaimon77@gmail.com> <20190829104721.tnjk3bqt3cq6iagr@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 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 > 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 the PM logic (as part of pm_runtime_get_sync() ). Given the code *already* relies on npcm_rng_init() being called by the PM logic why does it matter if additional init is put there? Daniel.