From: Lee Jones <lee.jones@linaro.org>
To: Kieran Bingham <kieranbingham@gmail.com>
Cc: devicetree@vger.kernel.org, herbert@gondor.apana.org.au,
linux-kernel@vger.kernel.org, Pankaj Dev <pankaj.dev@st.com>,
linux-crypto@vger.kernel.org, mpm@selenic.com,
linux-arm-kernel@lists.infradead.org, kernel@stlinux.com
Subject: Re: [PATCH 4/6] hwrng: st: Add support for ST's HW Random Number Generator
Date: Mon, 14 Sep 2015 09:04:51 +0100 [thread overview]
Message-ID: <20150914080451.GD27591@x1> (raw)
In-Reply-To: <CAB3z_Rpz6-mjfsvi55HOrePSVDEn7u-MR_N3e9wL6hT=wfLLYg@mail.gmail.com>
On Sat, 12 Sep 2015, Kieran Bingham wrote:
> On 11 September 2015 at 21:08, Lee Jones <lee.jones@linaro.org> wrote:
> > Signed-off-by: Pankaj Dev <pankaj.dev@st.com>
> > Signed-off-by: Lee Jones <lee.jones@linaro.org>
> > ---
> > drivers/char/hw_random/Kconfig | 10 +++
> > drivers/char/hw_random/Makefile | 1 +
> > drivers/char/hw_random/st-rng.c | 142 ++++++++++++++++++++++++++++++++++++++++
> > 3 files changed, 153 insertions(+)
> > create mode 100644 drivers/char/hw_random/st-rng.c
> >
> > diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
> > index f48cf11..bb6cb77 100644
> > --- a/drivers/char/hw_random/Kconfig
> > +++ b/drivers/char/hw_random/Kconfig
> > @@ -346,6 +346,16 @@ config HW_RANDOM_MSM
> >
> > If unsure, say Y.
> >
> > +config HW_RANDOM_ST
> > + tristate "ST Microelectronics HW Random Number Generator support"
> > + depends on HW_RANDOM && ARCH_STI
> > + ---help---
> > + This driver provides kernel-side support for the Random Number
> > + Generator hardware found on STi series of SoCs.
> > +
> > + To compile this driver as a module, choose M here: the
> > + module will be called st-rng.
> > +
> > config HW_RANDOM_XGENE
> > tristate "APM X-Gene True Random Number Generator (TRNG) support"
> > depends on HW_RANDOM && ARCH_XGENE
> > diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
> > index 055bb01..8bcfb45 100644
> > --- a/drivers/char/hw_random/Makefile
> > +++ b/drivers/char/hw_random/Makefile
> > @@ -30,4 +30,5 @@ obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
> > obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
> > obj-$(CONFIG_HW_RANDOM_IPROC_RNG200) += iproc-rng200.o
> > obj-$(CONFIG_HW_RANDOM_MSM) += msm-rng.o
> > +obj-$(CONFIG_HW_RANDOM_ST) += st-rng.o
> > obj-$(CONFIG_HW_RANDOM_XGENE) += xgene-rng.o
> > diff --git a/drivers/char/hw_random/st-rng.c b/drivers/char/hw_random/st-rng.c
> > new file mode 100644
> > index 0000000..717a831
> > --- /dev/null
> > +++ b/drivers/char/hw_random/st-rng.c
> > @@ -0,0 +1,142 @@
> > +/*
> > + * ST Random Number Generator Driver ST's Platforms
> > + *
> > + * Author: Pankaj Dev: <pankaj.dev@st.com>
> > + * Lee Jones <lee.jones@linaro.org>
> > + *
> > + * Copyright (C) 2015 STMicroelectronics (R&D) Limited
> > + *
> > + * 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.
> > + */
> > +
> > +#include <linux/clk.h>
> > +#include <linux/delay.h>
> > +#include <linux/hw_random.h>
> > +#include <linux/io.h>
> > +#include <linux/module.h>
> > +#include <linux/of.h>
> > +#include <linux/platform_device.h>
> > +#include <linux/slab.h>
> > +
> > +/* Registers */
> > +#define ST_RNG_STATUS_REG 0x20
> > +#define ST_RNG_DATA_REG 0x24
> > +
> > +/* Registers fields */
> > +#define ST_RNG_STATUS_BAD_SEQUENCE BIT(0)
> > +#define ST_RNG_STATUS_BAD_ALTERNANCE BIT(1)
> > +#define ST_RNG_STATUS_FIFO_FULL BIT(5)
> > +
> > +#define ST_RNG_FIFO_SIZE 8
> > +#define ST_RNG_SAMPLE_SIZE 2 /* 2 Byte (16bit) samples */
> > +
> > +/* Samples are available every 0.667us, which we round to 1us */
> > +#define ST_RNG_FILL_FIFO_TIMEOUT (1 * (ST_RNG_FIFO_SIZE / ST_RNG_SAMPLE_SIZE))
> > +
> > +struct st_rng_data {
> > + void __iomem *base;
> > + struct clk *clk;
> > + struct hwrng ops;
> > +};
> > +
> > +static int st_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
> > +{
> > + struct st_rng_data *ddata = (struct st_rng_data *)rng->priv;
> > + u32 status;
> > + int i;
> > +
> > + if (max < sizeof(u16))
> > + return -EINVAL;
> > +
> > + /* Wait until FIFO is full - max 4uS*/
> > + for (i = 0; i < ST_RNG_FILL_FIFO_TIMEOUT; i++) {
> > + status = readl_relaxed(ddata->base + ST_RNG_STATUS_REG);
> > + if (status & ST_RNG_STATUS_FIFO_FULL)
> > + break;
> > + udelay(1);
> > + }
> > +
> > + if (i == ST_RNG_FILL_FIFO_TIMEOUT)
> > + return 0;
> > +
> > + for (i = 0; i < ST_RNG_FIFO_SIZE && i < max; i += 2)
> > + *(u16 *)(data + i) =
> > + readl_relaxed(ddata->base + ST_RNG_DATA_REG);
>
> Will this code always be called with max % 2 == 0? ...
> I.e. will
> st_rng_read(rng, data[5], 5, waitflg)
> overflow an array?
.read() can be called with either 16, 32 or the size of the cache,
which is unlikely to be anything !(N^2) (it's 64 in our case).
> > +
> > + return i - 2; /* No of bytes read */
>
> Really? Doesn't this return 0 on st_rng_read(rng, data[2], 2, waitflg);
Hmm... I assumed the OA's math was good here.
Good spot. I will run some tests to confirm.
> > +}
> > +
> > +static int st_rng_probe(struct platform_device *pdev)
> > +{
> > + struct st_rng_data *ddata;
> > + struct resource *res;
> > + struct clk *clk;
> > + void __iomem *base;
> > + int ret;
> > +
> > + ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
> > + if (!ddata)
> > + return -ENOMEM;
> > +
> > + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> > + base = devm_ioremap_resource(&pdev->dev, res);
> > + if (IS_ERR(base))
>
> Are we leaking ddata here ? I.e. before its attached with set_drvdata?
It won't leak. That's the point of devm_*.
Please see: Documentation/driver-model/devres.txt
> > + return PTR_ERR(base);
> > +
> > + clk = devm_clk_get(&pdev->dev, NULL);
> > + if (!clk)
>
> And here ...
Nope.
> > + return -EINVAL;
> > +
> > + clk_prepare_enable(clk);
> > +
> > + ddata->ops.priv = (unsigned long)ddata;
> > + ddata->ops.read = st_rng_read;
> > + ddata->ops.name = pdev->name;
> > + ddata->base = base;
> > + ddata->clk = clk;
> > +
> > + dev_set_drvdata(&pdev->dev, ddata);
> > +
> > + ret = hwrng_register(&ddata->ops);
> > + if (ret) {
> > + dev_err(&pdev->dev, "Failed to register HW RNG\n");
> > + return ret;
> > + }
> > +
> > + dev_info(&pdev->dev, "Successfully registered HW RNG\n");
> > +
> > + return 0;
> > +}
> > +
> > +static int st_rng_remove(struct platform_device *pdev)
> > +{
> > + struct st_rng_data *ddata = dev_get_drvdata(&pdev->dev);
> > +
> > + hwrng_unregister(&ddata->ops);
> > +
> > + clk_disable_unprepare(ddata->clk);
> > +
> > + return 0;
> > +}
> > +
> > +static const struct of_device_id st_rng_match[] = {
> > + { .compatible = "st,rng" },
> > + {},
> > +};
> > +MODULE_DEVICE_TABLE(of, st_rng_match);
> > +
> > +static struct platform_driver st_rng_driver = {
> > + .driver = {
> > + .name = "st-hwrandom",
> > + .of_match_table = of_match_ptr(st_rng_match),
> > + },
> > + .probe = st_rng_probe,
> > + .remove = st_rng_remove
> > +};
> > +
> > +module_platform_driver(st_rng_driver);
> > +
> > +MODULE_AUTHOR("Pankaj Dev <pankaj.dev@st.com>");
> > +MODULE_LICENSE("GPL v2");
> >
>
>
--
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
WARNING: multiple messages have this Message-ID (diff)
From: lee.jones@linaro.org (Lee Jones)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH 4/6] hwrng: st: Add support for ST's HW Random Number Generator
Date: Mon, 14 Sep 2015 09:04:51 +0100 [thread overview]
Message-ID: <20150914080451.GD27591@x1> (raw)
In-Reply-To: <CAB3z_Rpz6-mjfsvi55HOrePSVDEn7u-MR_N3e9wL6hT=wfLLYg@mail.gmail.com>
On Sat, 12 Sep 2015, Kieran Bingham wrote:
> On 11 September 2015 at 21:08, Lee Jones <lee.jones@linaro.org> wrote:
> > Signed-off-by: Pankaj Dev <pankaj.dev@st.com>
> > Signed-off-by: Lee Jones <lee.jones@linaro.org>
> > ---
> > drivers/char/hw_random/Kconfig | 10 +++
> > drivers/char/hw_random/Makefile | 1 +
> > drivers/char/hw_random/st-rng.c | 142 ++++++++++++++++++++++++++++++++++++++++
> > 3 files changed, 153 insertions(+)
> > create mode 100644 drivers/char/hw_random/st-rng.c
> >
> > diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
> > index f48cf11..bb6cb77 100644
> > --- a/drivers/char/hw_random/Kconfig
> > +++ b/drivers/char/hw_random/Kconfig
> > @@ -346,6 +346,16 @@ config HW_RANDOM_MSM
> >
> > If unsure, say Y.
> >
> > +config HW_RANDOM_ST
> > + tristate "ST Microelectronics HW Random Number Generator support"
> > + depends on HW_RANDOM && ARCH_STI
> > + ---help---
> > + This driver provides kernel-side support for the Random Number
> > + Generator hardware found on STi series of SoCs.
> > +
> > + To compile this driver as a module, choose M here: the
> > + module will be called st-rng.
> > +
> > config HW_RANDOM_XGENE
> > tristate "APM X-Gene True Random Number Generator (TRNG) support"
> > depends on HW_RANDOM && ARCH_XGENE
> > diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
> > index 055bb01..8bcfb45 100644
> > --- a/drivers/char/hw_random/Makefile
> > +++ b/drivers/char/hw_random/Makefile
> > @@ -30,4 +30,5 @@ obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
> > obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
> > obj-$(CONFIG_HW_RANDOM_IPROC_RNG200) += iproc-rng200.o
> > obj-$(CONFIG_HW_RANDOM_MSM) += msm-rng.o
> > +obj-$(CONFIG_HW_RANDOM_ST) += st-rng.o
> > obj-$(CONFIG_HW_RANDOM_XGENE) += xgene-rng.o
> > diff --git a/drivers/char/hw_random/st-rng.c b/drivers/char/hw_random/st-rng.c
> > new file mode 100644
> > index 0000000..717a831
> > --- /dev/null
> > +++ b/drivers/char/hw_random/st-rng.c
> > @@ -0,0 +1,142 @@
> > +/*
> > + * ST Random Number Generator Driver ST's Platforms
> > + *
> > + * Author: Pankaj Dev: <pankaj.dev@st.com>
> > + * Lee Jones <lee.jones@linaro.org>
> > + *
> > + * Copyright (C) 2015 STMicroelectronics (R&D) Limited
> > + *
> > + * 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.
> > + */
> > +
> > +#include <linux/clk.h>
> > +#include <linux/delay.h>
> > +#include <linux/hw_random.h>
> > +#include <linux/io.h>
> > +#include <linux/module.h>
> > +#include <linux/of.h>
> > +#include <linux/platform_device.h>
> > +#include <linux/slab.h>
> > +
> > +/* Registers */
> > +#define ST_RNG_STATUS_REG 0x20
> > +#define ST_RNG_DATA_REG 0x24
> > +
> > +/* Registers fields */
> > +#define ST_RNG_STATUS_BAD_SEQUENCE BIT(0)
> > +#define ST_RNG_STATUS_BAD_ALTERNANCE BIT(1)
> > +#define ST_RNG_STATUS_FIFO_FULL BIT(5)
> > +
> > +#define ST_RNG_FIFO_SIZE 8
> > +#define ST_RNG_SAMPLE_SIZE 2 /* 2 Byte (16bit) samples */
> > +
> > +/* Samples are available every 0.667us, which we round to 1us */
> > +#define ST_RNG_FILL_FIFO_TIMEOUT (1 * (ST_RNG_FIFO_SIZE / ST_RNG_SAMPLE_SIZE))
> > +
> > +struct st_rng_data {
> > + void __iomem *base;
> > + struct clk *clk;
> > + struct hwrng ops;
> > +};
> > +
> > +static int st_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
> > +{
> > + struct st_rng_data *ddata = (struct st_rng_data *)rng->priv;
> > + u32 status;
> > + int i;
> > +
> > + if (max < sizeof(u16))
> > + return -EINVAL;
> > +
> > + /* Wait until FIFO is full - max 4uS*/
> > + for (i = 0; i < ST_RNG_FILL_FIFO_TIMEOUT; i++) {
> > + status = readl_relaxed(ddata->base + ST_RNG_STATUS_REG);
> > + if (status & ST_RNG_STATUS_FIFO_FULL)
> > + break;
> > + udelay(1);
> > + }
> > +
> > + if (i == ST_RNG_FILL_FIFO_TIMEOUT)
> > + return 0;
> > +
> > + for (i = 0; i < ST_RNG_FIFO_SIZE && i < max; i += 2)
> > + *(u16 *)(data + i) =
> > + readl_relaxed(ddata->base + ST_RNG_DATA_REG);
>
> Will this code always be called with max % 2 == 0? ...
> I.e. will
> st_rng_read(rng, data[5], 5, waitflg)
> overflow an array?
.read() can be called with either 16, 32 or the size of the cache,
which is unlikely to be anything !(N^2) (it's 64 in our case).
> > +
> > + return i - 2; /* No of bytes read */
>
> Really? Doesn't this return 0 on st_rng_read(rng, data[2], 2, waitflg);
Hmm... I assumed the OA's math was good here.
Good spot. I will run some tests to confirm.
> > +}
> > +
> > +static int st_rng_probe(struct platform_device *pdev)
> > +{
> > + struct st_rng_data *ddata;
> > + struct resource *res;
> > + struct clk *clk;
> > + void __iomem *base;
> > + int ret;
> > +
> > + ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
> > + if (!ddata)
> > + return -ENOMEM;
> > +
> > + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> > + base = devm_ioremap_resource(&pdev->dev, res);
> > + if (IS_ERR(base))
>
> Are we leaking ddata here ? I.e. before its attached with set_drvdata?
It won't leak. That's the point of devm_*.
Please see: Documentation/driver-model/devres.txt
> > + return PTR_ERR(base);
> > +
> > + clk = devm_clk_get(&pdev->dev, NULL);
> > + if (!clk)
>
> And here ...
Nope.
> > + return -EINVAL;
> > +
> > + clk_prepare_enable(clk);
> > +
> > + ddata->ops.priv = (unsigned long)ddata;
> > + ddata->ops.read = st_rng_read;
> > + ddata->ops.name = pdev->name;
> > + ddata->base = base;
> > + ddata->clk = clk;
> > +
> > + dev_set_drvdata(&pdev->dev, ddata);
> > +
> > + ret = hwrng_register(&ddata->ops);
> > + if (ret) {
> > + dev_err(&pdev->dev, "Failed to register HW RNG\n");
> > + return ret;
> > + }
> > +
> > + dev_info(&pdev->dev, "Successfully registered HW RNG\n");
> > +
> > + return 0;
> > +}
> > +
> > +static int st_rng_remove(struct platform_device *pdev)
> > +{
> > + struct st_rng_data *ddata = dev_get_drvdata(&pdev->dev);
> > +
> > + hwrng_unregister(&ddata->ops);
> > +
> > + clk_disable_unprepare(ddata->clk);
> > +
> > + return 0;
> > +}
> > +
> > +static const struct of_device_id st_rng_match[] = {
> > + { .compatible = "st,rng" },
> > + {},
> > +};
> > +MODULE_DEVICE_TABLE(of, st_rng_match);
> > +
> > +static struct platform_driver st_rng_driver = {
> > + .driver = {
> > + .name = "st-hwrandom",
> > + .of_match_table = of_match_ptr(st_rng_match),
> > + },
> > + .probe = st_rng_probe,
> > + .remove = st_rng_remove
> > +};
> > +
> > +module_platform_driver(st_rng_driver);
> > +
> > +MODULE_AUTHOR("Pankaj Dev <pankaj.dev@st.com>");
> > +MODULE_LICENSE("GPL v2");
> >
>
>
--
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org ? Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
WARNING: multiple messages have this Message-ID (diff)
From: Lee Jones <lee.jones@linaro.org>
To: Kieran Bingham <kieranbingham@gmail.com>
Cc: linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, kernel@stlinux.com,
mpm@selenic.com, herbert@gondor.apana.org.au,
linux-crypto@vger.kernel.org, devicetree@vger.kernel.org,
Pankaj Dev <pankaj.dev@st.com>
Subject: Re: [PATCH 4/6] hwrng: st: Add support for ST's HW Random Number Generator
Date: Mon, 14 Sep 2015 09:04:51 +0100 [thread overview]
Message-ID: <20150914080451.GD27591@x1> (raw)
In-Reply-To: <CAB3z_Rpz6-mjfsvi55HOrePSVDEn7u-MR_N3e9wL6hT=wfLLYg@mail.gmail.com>
On Sat, 12 Sep 2015, Kieran Bingham wrote:
> On 11 September 2015 at 21:08, Lee Jones <lee.jones@linaro.org> wrote:
> > Signed-off-by: Pankaj Dev <pankaj.dev@st.com>
> > Signed-off-by: Lee Jones <lee.jones@linaro.org>
> > ---
> > drivers/char/hw_random/Kconfig | 10 +++
> > drivers/char/hw_random/Makefile | 1 +
> > drivers/char/hw_random/st-rng.c | 142 ++++++++++++++++++++++++++++++++++++++++
> > 3 files changed, 153 insertions(+)
> > create mode 100644 drivers/char/hw_random/st-rng.c
> >
> > diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
> > index f48cf11..bb6cb77 100644
> > --- a/drivers/char/hw_random/Kconfig
> > +++ b/drivers/char/hw_random/Kconfig
> > @@ -346,6 +346,16 @@ config HW_RANDOM_MSM
> >
> > If unsure, say Y.
> >
> > +config HW_RANDOM_ST
> > + tristate "ST Microelectronics HW Random Number Generator support"
> > + depends on HW_RANDOM && ARCH_STI
> > + ---help---
> > + This driver provides kernel-side support for the Random Number
> > + Generator hardware found on STi series of SoCs.
> > +
> > + To compile this driver as a module, choose M here: the
> > + module will be called st-rng.
> > +
> > config HW_RANDOM_XGENE
> > tristate "APM X-Gene True Random Number Generator (TRNG) support"
> > depends on HW_RANDOM && ARCH_XGENE
> > diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
> > index 055bb01..8bcfb45 100644
> > --- a/drivers/char/hw_random/Makefile
> > +++ b/drivers/char/hw_random/Makefile
> > @@ -30,4 +30,5 @@ obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
> > obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
> > obj-$(CONFIG_HW_RANDOM_IPROC_RNG200) += iproc-rng200.o
> > obj-$(CONFIG_HW_RANDOM_MSM) += msm-rng.o
> > +obj-$(CONFIG_HW_RANDOM_ST) += st-rng.o
> > obj-$(CONFIG_HW_RANDOM_XGENE) += xgene-rng.o
> > diff --git a/drivers/char/hw_random/st-rng.c b/drivers/char/hw_random/st-rng.c
> > new file mode 100644
> > index 0000000..717a831
> > --- /dev/null
> > +++ b/drivers/char/hw_random/st-rng.c
> > @@ -0,0 +1,142 @@
> > +/*
> > + * ST Random Number Generator Driver ST's Platforms
> > + *
> > + * Author: Pankaj Dev: <pankaj.dev@st.com>
> > + * Lee Jones <lee.jones@linaro.org>
> > + *
> > + * Copyright (C) 2015 STMicroelectronics (R&D) Limited
> > + *
> > + * 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.
> > + */
> > +
> > +#include <linux/clk.h>
> > +#include <linux/delay.h>
> > +#include <linux/hw_random.h>
> > +#include <linux/io.h>
> > +#include <linux/module.h>
> > +#include <linux/of.h>
> > +#include <linux/platform_device.h>
> > +#include <linux/slab.h>
> > +
> > +/* Registers */
> > +#define ST_RNG_STATUS_REG 0x20
> > +#define ST_RNG_DATA_REG 0x24
> > +
> > +/* Registers fields */
> > +#define ST_RNG_STATUS_BAD_SEQUENCE BIT(0)
> > +#define ST_RNG_STATUS_BAD_ALTERNANCE BIT(1)
> > +#define ST_RNG_STATUS_FIFO_FULL BIT(5)
> > +
> > +#define ST_RNG_FIFO_SIZE 8
> > +#define ST_RNG_SAMPLE_SIZE 2 /* 2 Byte (16bit) samples */
> > +
> > +/* Samples are available every 0.667us, which we round to 1us */
> > +#define ST_RNG_FILL_FIFO_TIMEOUT (1 * (ST_RNG_FIFO_SIZE / ST_RNG_SAMPLE_SIZE))
> > +
> > +struct st_rng_data {
> > + void __iomem *base;
> > + struct clk *clk;
> > + struct hwrng ops;
> > +};
> > +
> > +static int st_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
> > +{
> > + struct st_rng_data *ddata = (struct st_rng_data *)rng->priv;
> > + u32 status;
> > + int i;
> > +
> > + if (max < sizeof(u16))
> > + return -EINVAL;
> > +
> > + /* Wait until FIFO is full - max 4uS*/
> > + for (i = 0; i < ST_RNG_FILL_FIFO_TIMEOUT; i++) {
> > + status = readl_relaxed(ddata->base + ST_RNG_STATUS_REG);
> > + if (status & ST_RNG_STATUS_FIFO_FULL)
> > + break;
> > + udelay(1);
> > + }
> > +
> > + if (i == ST_RNG_FILL_FIFO_TIMEOUT)
> > + return 0;
> > +
> > + for (i = 0; i < ST_RNG_FIFO_SIZE && i < max; i += 2)
> > + *(u16 *)(data + i) =
> > + readl_relaxed(ddata->base + ST_RNG_DATA_REG);
>
> Will this code always be called with max % 2 == 0? ...
> I.e. will
> st_rng_read(rng, data[5], 5, waitflg)
> overflow an array?
.read() can be called with either 16, 32 or the size of the cache,
which is unlikely to be anything !(N^2) (it's 64 in our case).
> > +
> > + return i - 2; /* No of bytes read */
>
> Really? Doesn't this return 0 on st_rng_read(rng, data[2], 2, waitflg);
Hmm... I assumed the OA's math was good here.
Good spot. I will run some tests to confirm.
> > +}
> > +
> > +static int st_rng_probe(struct platform_device *pdev)
> > +{
> > + struct st_rng_data *ddata;
> > + struct resource *res;
> > + struct clk *clk;
> > + void __iomem *base;
> > + int ret;
> > +
> > + ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
> > + if (!ddata)
> > + return -ENOMEM;
> > +
> > + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> > + base = devm_ioremap_resource(&pdev->dev, res);
> > + if (IS_ERR(base))
>
> Are we leaking ddata here ? I.e. before its attached with set_drvdata?
It won't leak. That's the point of devm_*.
Please see: Documentation/driver-model/devres.txt
> > + return PTR_ERR(base);
> > +
> > + clk = devm_clk_get(&pdev->dev, NULL);
> > + if (!clk)
>
> And here ...
Nope.
> > + return -EINVAL;
> > +
> > + clk_prepare_enable(clk);
> > +
> > + ddata->ops.priv = (unsigned long)ddata;
> > + ddata->ops.read = st_rng_read;
> > + ddata->ops.name = pdev->name;
> > + ddata->base = base;
> > + ddata->clk = clk;
> > +
> > + dev_set_drvdata(&pdev->dev, ddata);
> > +
> > + ret = hwrng_register(&ddata->ops);
> > + if (ret) {
> > + dev_err(&pdev->dev, "Failed to register HW RNG\n");
> > + return ret;
> > + }
> > +
> > + dev_info(&pdev->dev, "Successfully registered HW RNG\n");
> > +
> > + return 0;
> > +}
> > +
> > +static int st_rng_remove(struct platform_device *pdev)
> > +{
> > + struct st_rng_data *ddata = dev_get_drvdata(&pdev->dev);
> > +
> > + hwrng_unregister(&ddata->ops);
> > +
> > + clk_disable_unprepare(ddata->clk);
> > +
> > + return 0;
> > +}
> > +
> > +static const struct of_device_id st_rng_match[] = {
> > + { .compatible = "st,rng" },
> > + {},
> > +};
> > +MODULE_DEVICE_TABLE(of, st_rng_match);
> > +
> > +static struct platform_driver st_rng_driver = {
> > + .driver = {
> > + .name = "st-hwrandom",
> > + .of_match_table = of_match_ptr(st_rng_match),
> > + },
> > + .probe = st_rng_probe,
> > + .remove = st_rng_remove
> > +};
> > +
> > +module_platform_driver(st_rng_driver);
> > +
> > +MODULE_AUTHOR("Pankaj Dev <pankaj.dev@st.com>");
> > +MODULE_LICENSE("GPL v2");
> >
>
>
--
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
next prev parent reply other threads:[~2015-09-14 8:04 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-09-11 20:08 [PATCH 0/6] hwrng: Add support for STMicroelectronics' RNG IP Lee Jones
2015-09-11 20:08 ` Lee Jones
2015-09-11 20:08 ` [PATCH 1/6] Documentation: hw_random: Fix device node name /dev/hw_random => /dev/hwrng Lee Jones
2015-09-11 20:08 ` Lee Jones
[not found] ` <1442002110-28733-2-git-send-email-lee.jones-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
2015-09-14 14:57 ` Kieran Bingham
2015-09-14 14:57 ` Kieran Bingham
2015-09-14 14:57 ` Kieran Bingham
2015-09-11 20:08 ` [PATCH 2/6] hwrng: core: Simplify RNG switching from sysfs Lee Jones
2015-09-11 20:08 ` Lee Jones
2015-09-11 20:08 ` Lee Jones
[not found] ` <1442002110-28733-3-git-send-email-lee.jones-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
2015-09-13 14:24 ` Peter Korsgaard
2015-09-13 14:24 ` Peter Korsgaard
2015-09-13 14:24 ` Peter Korsgaard
2015-09-14 7:27 ` Lee Jones
2015-09-14 7:27 ` Lee Jones
2015-09-11 20:08 ` [PATCH 3/6] hwrng: st: Provide DT bindings for ST's Random Number Generator Lee Jones
2015-09-11 20:08 ` Lee Jones
2015-09-11 20:08 ` Lee Jones
2015-09-11 20:08 ` [PATCH 4/6] hwrng: st: Add support for ST's HW " Lee Jones
2015-09-11 20:08 ` Lee Jones
2015-09-12 22:58 ` Kieran Bingham
2015-09-12 22:58 ` Kieran Bingham
2015-09-14 8:04 ` Lee Jones [this message]
2015-09-14 8:04 ` Lee Jones
2015-09-14 8:04 ` Lee Jones
2015-09-12 23:04 ` Fabio Estevam
2015-09-12 23:04 ` Fabio Estevam
2015-09-14 7:42 ` Lee Jones
2015-09-14 7:42 ` Lee Jones
2015-09-11 20:08 ` [PATCH 5/6] ARM: STi: STiH407: Enable the 2 HW Random Number Generators for STiH4{07,10} Lee Jones
2015-09-11 20:08 ` [PATCH 5/6] ARM: STi: STiH407: Enable the 2 HW Random Number Generators for STiH4{07, 10} Lee Jones
2015-09-11 20:08 ` [PATCH 6/6] MAINTAINERS: Add ST's Random Number Generator to the ST entry Lee Jones
2015-09-11 20:08 ` Lee Jones
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=20150914080451.GD27591@x1 \
--to=lee.jones@linaro.org \
--cc=devicetree@vger.kernel.org \
--cc=herbert@gondor.apana.org.au \
--cc=kernel@stlinux.com \
--cc=kieranbingham@gmail.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mpm@selenic.com \
--cc=pankaj.dev@st.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.