Skip to contentSkip to conversation

hw_random: add timeriomem-rng driver

PATCH Superseded Alexander Clouter 2 participants 5 messages Feb 7, 2009 – Feb 11, 2009 Hardware random number generator core

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 hw_random: add timeriomem-rng driver

Some hardware platforms, the TS-7800[1] is one for example, can supply the kernel with an entropy source, albeit a slow one for TS-7800 users, by just reading a particular IO address. This source must not be read above a certain rate otherwise the quality suffers.

The driver is then hooked into by calling platform_device_(register|add|del) passing a structure similar to:
------

static struct timeriomem_rng_data ts78xx_ts_rng_data = {
        .address        = (u32 *__iomem) TS_RNG,
        .period         = 1000000, /* one second */
};

static struct platform_device ts78xx_ts_rng_device = {

        .name           = "timeriomem_rng",
        .id             = -1,
        .dev            = {
                .platform_data  = &ts78xx_ts_rng_data,
        },
        .num_resources  = 0,
};

------

[1] http://www.embeddedarm.com/products/board-detail.php?product=TS-7800

---
 drivers/char/hw_random/Kconfig          |   14 +++
 drivers/char/hw_random/Makefile         |    1 +
 drivers/char/hw_random/timeriomem-rng.c |  151 +++++++++++++++++++++++++++++++
 include/linux/timeriomem-rng.h          |   16 ++++
 4 files changed, 182 insertions(+), 0 deletions(-)
 create mode 100644 drivers/char/hw_random/timeriomem-rng.c
 create mode 100644 include/linux/timeriomem-rng.h
drivers/char/hw_random/Kconfig +14 -0
index 8822eca..e86dd42 100644
@@ -20,6 +20,20 @@ config HW_RANDOM
 
 	  If unsure, say Y.
 
+config HW_RANDOM_TIMERIOMEM
+	tristate "Timer IOMEM HW Random Number Generator support"
+	depends on HW_RANDOM
+	---help---
+	  This driver provides kernel-side support for a generic Random
+	  Number Generator used by reading a 'dumb' iomem address that
+	  is to be read no faster than, for example, once a second;
+	  the default FPGA bitstream on the TS-7800 has such functionality.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called timeriomem-rng.
+
+	  If unsure, say Y.
+
 config HW_RANDOM_INTEL
 	tristate "Intel HW Random Number Generator support"
 	depends on HW_RANDOM && (X86 || IA64) && PCI
drivers/char/hw_random/Makefile +1 -0
index b6effb7..e81d21a 100644
@@ -4,6 +4,7 @@
 
 obj-$(CONFIG_HW_RANDOM) += rng-core.o
 rng-core-y := core.o
+obj-$(CONFIG_HW_RANDOM_TIMERIOMEM) += timeriomem-rng.o
 obj-$(CONFIG_HW_RANDOM_INTEL) += intel-rng.o
 obj-$(CONFIG_HW_RANDOM_AMD) += amd-rng.o
 obj-$(CONFIG_HW_RANDOM_GEODE) += geode-rng.o
drivers/char/hw_random/timeriomem-rng.c new file +151 -0

151 lines not on this page · raw message

include/linux/timeriomem-rng.h new file +16 -0
new file mode 100644
index 0000000..16dd9e4
@@ -0,0 +1,16 @@
+/*
+ * linux/include/linux/timeriomem-rng.h
+ *
+ * Copyright (c) 2008 Alexander Clouter <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+struct timeriomem_rng_data {
+	u32 __iomem	*address;
+
+	/* measures in usecs */
+	unsigned int	period;
+};
Signature · 1 line
1.5.6.5
maintainer
#1 lore raw
Quoted 9 lines · Alexander Clouter
drivers/char/hw_random/timeriomem-rng.c new file +8 -0
+/*
+ * have data return 1, however return 0 if we have nothing
+ */
+static int timeriomem_rng_data_present(struct hwrng *rng, int wait)
+{
+	s32 delay;
+
+	if (rng->priv == 0)

drivers/char/hw_random/timeriomem-rng.c line 55

Quoted 10 lines
drivers/char/hw_random/timeriomem-rng.c new file +10 -0
+		return 1;
+
+	if (del_timer_sync(&timeriomem_rng_timer)) {
+		if (!wait)
+			return 0;
+
+		delay = timeriomem_rng_timer.expires - jiffies;
+
+		schedule_timeout_uninterruptible(delay);
+	}

Sorry, but this just doesn't work for O_NONBLOCK reads. What'll happen is that the first failed read will return -EAGAIN, and when we're called again immediately (because hwrng polling support is non-existant), it'll just read the data right away because now there is no timer.

Quoted 6 lines
drivers/char/hw_random/timeriomem-rng.c new file +6 -0
+static int timeriomem_rng_data_read(struct hwrng *rng, u32 *data)
+{
+	unsigned long cur;
+	s32 delay;
+
+	*data = readl(timeriomem_rng_data->address);

drivers/char/hw_random/timeriomem-rng.c line 75

Quoted 10 lines
drivers/char/hw_random/timeriomem-rng.c new file +10 -0
+
+	if (rng->priv != 0) {
+		cur = jiffies;
+
+		delay = cur - timeriomem_rng_timer.expires;
+		delay = rng->priv - (delay % rng->priv);
+
+		timeriomem_rng_timer.expires = cur + delay;
+		add_timer(&timeriomem_rng_timer);
+	}

Instead of deleting the timer above, why not create a rng->present variable which you set to zero here, and the timer sets it to non-zero. Then you just need to return rng->present in your data_present function.

drivers/char/hw_random/timeriomem-rng.c line 83

Quoted 4 lines
drivers/char/hw_random/timeriomem-rng.c new file +4 -0
+static void timeriomem_rng_trigger(unsigned long dummy)
+{
+	del_timer_sync(&timeriomem_rng_timer);
+}

This is going to create an infinite loop. There is no point in deleting yourself.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#2 lore raw

Hi,

Quoted 19 lines · Herbert Xu
Quoted 17 lines · Alexander Clouter
drivers/char/hw_random/timeriomem-rng.c new file +16 -0
+/*
+ * have data return 1, however return 0 if we have nothing
+ */
+static int timeriomem_rng_data_present(struct hwrng *rng, int wait)
+{
+	s32 delay;
+
+	if (rng->priv == 0)
+		return 1;
+
+	if (del_timer_sync(&timeriomem_rng_timer)) {
+		if (!wait)
+			return 0;
+
+		delay = timeriomem_rng_timer.expires - jiffies;
+
Quoted 9 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+		schedule_timeout_uninterruptible(delay);
+	}

Sorry, but this just doesn't work for O_NONBLOCK reads. What'll happen is that the first failed read will return -EAGAIN, and when we're called again immediately (because hwrng polling support is non-existant), it'll just read the data right away because now there is no timer.

My original code[1] approach, with timer_pending() in there, would have been okay then, right?
----
if (timer_pending(&timeriomem_rng_timer)) {
if (!wait)

    return 0;
  del_timer_sync(&timeriomem_rng_timer);
  delay = timeriomem_rng_timer.expires - jiffies;

schedule_timeout_uninterruptible(delay);
}
----

Quoted 13 lines
Quoted 13 lines
drivers/char/hw_random/timeriomem-rng.c new file +13 -0
+static int timeriomem_rng_data_read(struct hwrng *rng, u32 *data)
+{
+	unsigned long cur;
+	s32 delay;
+
+	*data = readl(timeriomem_rng_data->address);
+
+	if (rng->priv != 0) {
+		cur = jiffies;
+
+		delay = cur - timeriomem_rng_timer.expires;
+		delay = rng->priv - (delay % rng->priv);
+
Quoted 9 lines

drivers/char/hw_random/timeriomem-rng.c line 75

Quoted 3 lines
drivers/char/hw_random/timeriomem-rng.c new file +3 -0
+		timeriomem_rng_timer.expires = cur + delay;
+		add_timer(&timeriomem_rng_timer);
+	}

Instead of deleting the timer above, why not create a rng->present variable which you set to zero here, and the timer sets it to non-zero. Then you just need to return rng->present in your data_present function.

Sounds fair, better than the timer_pending approach and easier to understand.

Quoted 8 lines

drivers/char/hw_random/timeriomem-rng.c line 83

Quoted 4 lines
drivers/char/hw_random/timeriomem-rng.c new file +4 -0
+static void timeriomem_rng_trigger(unsigned long dummy)
+{
+	del_timer_sync(&timeriomem_rng_timer);
+}

This is going to create an infinite loop. There is no point in deleting yourself.

Noted.

Cheers

#3 lore raw

Hi,

Quoted 25 lines · Alexander Clouter
Quoted 7 lines · Herbert Xu

Sorry, but this just doesn't work for O_NONBLOCK reads. What'll happen is that the first failed read will return -EAGAIN, and when we're called again immediately (because hwrng polling support is non-existant), it'll just read the data right away because now there is no timer.

My original code[1] approach, with timer_pending() in there, would have been okay then, right?
----
if (timer_pending(&timeriomem_rng_timer)) {
if (!wait)

    return 0;
  del_timer_sync(&timeriomem_rng_timer);
  delay = timeriomem_rng_timer.expires - jiffies;

schedule_timeout_uninterruptible(delay);
}
----

[snipped]

Quoted 1 line
Quoted 8 lines
Quoted 5 lines

Instead of deleting the timer above, why not create a rng->present variable which you set to zero here, and the timer sets it to non-zero. Then you just need to return rng->present in your data_present function.

Sounds fair, better than the timer_pending approach and easier to understand.

Actually, as timer_pending() just reads a field in timer_list (or so says the 'manual'), would it not be a waste to have a second effectively duplicate variable? In a blocking read of data_presnt you would have to have del_timer_sync() anyway...the two approaches would be identical except for the rng->present variable approach would force us to waste a whole extra bit of ram?

Should I keep with my old original timer_pending() approach?

Of course I could be talking rubbish...

Cheers

Signature · 2 lines
Alexander Clouter
.sigmonster says: So many men; so little time.
maintainer
#4 lore raw
In reply to Alexander Clouter
Quoted 6 lines · Alexander Clouter

My original code[1] approach, with timer_pending() in there, would have been okay then, right?
----
if (timer_pending(&timeriomem_rng_timer)) {
if (!wait)

Quoted 7 lines
    return 0;
  del_timer_sync(&timeriomem_rng_timer);
  delay = timeriomem_rng_timer.expires - jiffies;

schedule_timeout_uninterruptible(delay);
}

Yes probably but it is racy. Also it's better if we can avoid the expires - jiffies calculation if only because then we won't have to tell Andrew why it can't be negative :)

Quoted 7 lines
Quoted 4 lines

Instead of deleting the timer above, why not create a rng->present variable which you set to zero here, and the timer sets it to non-zero. Then you just need to return rng->present in your data_present function.

Sounds fair, better than the timer_pending approach and easier to understand.

Oh I forgot about the blocking case. We can probably add a completion object to handle that. So something like

	if (!wait || rng->present)
		return rng->present;

	wait_for_completion(&rng->completion);
	return 1;

The timer would do

	rng->present = 1;
	complete(&rng->completion);

And the reader would do

	rng->present = 0;
	INIT_COMPLETION(rng->completion);
	add_timer(...)

Note that everything but the timer is completely serialised by the hwrng mutex so we only need to worry about racing against the timer.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt