From: Rick Altherr <raltherr@google.com>
To: alex@digriz.org.uk
Cc: openbmc@lists.ozlabs.org,
Herbert Xu <herbert@gondor.apana.org.au>,
Matt Mackall <mpm@selenic.com>,
linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2 1/3] hw_random: Migrate timeriomem_rng to new API
Date: Wed, 5 Apr 2017 16:20:58 -0700 [thread overview]
Message-ID: <20170405232100.2023-2-raltherr@google.com> (raw)
In-Reply-To: <20170405232100.2023-1-raltherr@google.com>
Preserves the existing behavior of only returning 32-bits per call.
Signed-off-by: Rick Altherr <raltherr@google.com>
---
Changes in v2:
- Split API migration into separate patch
drivers/char/hw_random/timeriomem-rng.c | 60 ++++++++++++++++-----------------
1 file changed, 30 insertions(+), 30 deletions(-)
diff --git a/drivers/char/hw_random/timeriomem-rng.c b/drivers/char/hw_random/timeriomem-rng.c
index cf37db263ecd..17574452fd35 100644
--- a/drivers/char/hw_random/timeriomem-rng.c
+++ b/drivers/char/hw_random/timeriomem-rng.c
@@ -20,18 +20,16 @@
* TODO: add support for reading sizes other than 32bits and masking
*/
-#include <linux/module.h>
-#include <linux/kernel.h>
-#include <linux/platform_device.h>
-#include <linux/of.h>
+#include <linux/completion.h>
#include <linux/hw_random.h>
#include <linux/io.h>
+#include <linux/jiffies.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/timeriomem-rng.h>
-#include <linux/jiffies.h>
-#include <linux/sched.h>
#include <linux/timer.h>
-#include <linux/completion.h>
struct timeriomem_rng_private_data {
void __iomem *io_base;
@@ -45,32 +43,36 @@ struct timeriomem_rng_private_data {
struct hwrng timeriomem_rng_ops;
};
-#define to_rng_priv(rng) \
- ((struct timeriomem_rng_private_data *)rng->priv)
-
-/*
- * have data return 1, however return 0 if we have nothing
- */
-static int timeriomem_rng_data_present(struct hwrng *rng, int wait)
+static int timeriomem_rng_read(struct hwrng *hwrng, void *data,
+ size_t max, bool wait)
{
- struct timeriomem_rng_private_data *priv = to_rng_priv(rng);
+ struct timeriomem_rng_private_data *priv =
+ container_of(hwrng, struct timeriomem_rng_private_data,
+ timeriomem_rng_ops);
+ unsigned long cur;
+ s32 delay;
- if (!wait || priv->present)
- return priv->present;
+ /* The RNG provides 32-bit per read. Ensure there is enough space. */
+ if (max < sizeof(u32))
+ return 0;
- wait_for_completion(&priv->completion);
+ /*
+ * There may not have been enough time for new data to be generated
+ * since the last request. If the caller doesn't want to wait, let them
+ * bail out. Otherwise, wait for the completion. If the new data has
+ * already been generated, the completion should already be available.
+ */
+ if (!wait && !priv->present)
+ return 0;
- return 1;
-}
-
-static int timeriomem_rng_data_read(struct hwrng *rng, u32 *data)
-{
- struct timeriomem_rng_private_data *priv = to_rng_priv(rng);
- unsigned long cur;
- s32 delay;
+ wait_for_completion(&priv->completion);
- *data = readl(priv->io_base);
+ *(u32 *)data = readl(priv->io_base);
+ /*
+ * Block any new callers until the RNG has had time to generate new
+ * data.
+ */
cur = jiffies;
delay = cur - priv->expires;
@@ -154,9 +156,7 @@ static int timeriomem_rng_probe(struct platform_device *pdev)
setup_timer(&priv->timer, timeriomem_rng_trigger, (unsigned long)priv);
priv->timeriomem_rng_ops.name = dev_name(&pdev->dev);
- priv->timeriomem_rng_ops.data_present = timeriomem_rng_data_present;
- priv->timeriomem_rng_ops.data_read = timeriomem_rng_data_read;
- priv->timeriomem_rng_ops.priv = (unsigned long)priv;
+ priv->timeriomem_rng_ops.read = timeriomem_rng_read;
priv->io_base = devm_ioremap_resource(&pdev->dev, res);
if (IS_ERR(priv->io_base)) {
--
2.12.2.715.g7642488e1d-goog
next prev parent reply other threads:[~2017-04-05 23:20 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-04-05 23:20 [PATCH v2 0/3] hw_random: timeriomem_rng: Migrate to new API and improve performance Rick Altherr
2017-04-05 23:20 ` Rick Altherr [this message]
2017-04-05 23:20 ` [PATCH v2 2/3] hw_random: timeriomem_rng: Shorten verbose type and variable names Rick Altherr
2017-04-05 23:21 ` [PATCH v2 3/3] hw_random: timeriomem_rng: Improve performance for sub-jiffie update periods Rick Altherr
2017-04-10 11:22 ` [PATCH v2 0/3] hw_random: timeriomem_rng: Migrate to new API and improve performance Herbert Xu
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=20170405232100.2023-2-raltherr@google.com \
--to=raltherr@google.com \
--cc=alex@digriz.org.uk \
--cc=herbert@gondor.apana.org.au \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mpm@selenic.com \
--cc=openbmc@lists.ozlabs.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox