linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/6] imx-rngc - several small fixes
@ 2020-01-28 11:00 Martin Kaiser
  2020-01-28 11:00 ` [PATCH 1/6] hwrng: imx-rngc - fix an error path Martin Kaiser
                   ` (6 more replies)
  0 siblings, 7 replies; 20+ messages in thread
From: Martin Kaiser @ 2020-01-28 11:00 UTC (permalink / raw)
  To: Herbert Xu, PrasannaKumar Muralidharan, NXP Linux Team
  Cc: linux-crypto, linux-arm-kernel, linux-kernel, Martin Kaiser

This is a set of small fixes for the imx-rngc driver.

I tried to clarify the approach for masking/unmasking the interrupt from
the rngc.

The rngc should be set to auto-seed mode, where it creates a new seed
when required.

In the probe function, we should check that the rng type is supported by
this driver.

Thanks for reviewing the patches,

   Martin


Martin Kaiser (6):
  hwrng: imx-rngc - fix an error path
  hwrng: imx-rngc - use automatic seeding
  hwrng: imx-rngc - use devres for registration
  hwrng: imx-rngc - (trivial) simplify error prints
  hwrng: imx-rngc - check the rng type
  hwrng: imx-rngc - simplify interrupt mask/unmask

 drivers/char/hw_random/imx-rngc.c | 89 ++++++++++++++++++++++++-------
 1 file changed, 70 insertions(+), 19 deletions(-)

-- 
2.20.1


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH 1/6] hwrng: imx-rngc - fix an error path
  2020-01-28 11:00 [PATCH 0/6] imx-rngc - several small fixes Martin Kaiser
@ 2020-01-28 11:00 ` Martin Kaiser
  2020-02-12 16:22   ` PrasannaKumar Muralidharan
  2020-01-28 11:00 ` [PATCH 2/6] hwrng: imx-rngc - use automatic seeding Martin Kaiser
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 20+ messages in thread
From: Martin Kaiser @ 2020-01-28 11:00 UTC (permalink / raw)
  To: Herbert Xu, PrasannaKumar Muralidharan, NXP Linux Team
  Cc: linux-crypto, linux-arm-kernel, linux-kernel, Martin Kaiser, stable

Make sure that the rngc interrupt is masked if the rngc self test fails.
Self test failure means that probe fails as well. Interrupts should be
masked in this case, regardless of the error.

Cc: stable@vger.kernel.org
Fixes: 1d5449445bd0 ("hwrng: mx-rngc - add a driver for Freescale RNGC")
Signed-off-by: Martin Kaiser <martin@kaiser.cx>
---
 drivers/char/hw_random/imx-rngc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/char/hw_random/imx-rngc.c b/drivers/char/hw_random/imx-rngc.c
index 30cf00f8e9a0..0576801944fd 100644
--- a/drivers/char/hw_random/imx-rngc.c
+++ b/drivers/char/hw_random/imx-rngc.c
@@ -105,8 +105,10 @@ static int imx_rngc_self_test(struct imx_rngc *rngc)
 		return -ETIMEDOUT;
 	}
 
-	if (rngc->err_reg != 0)
+	if (rngc->err_reg != 0) {
+		imx_rngc_irq_mask_clear(rngc);
 		return -EIO;
+	}
 
 	return 0;
 }
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 2/6] hwrng: imx-rngc - use automatic seeding
  2020-01-28 11:00 [PATCH 0/6] imx-rngc - several small fixes Martin Kaiser
  2020-01-28 11:00 ` [PATCH 1/6] hwrng: imx-rngc - fix an error path Martin Kaiser
@ 2020-01-28 11:00 ` Martin Kaiser
  2020-02-12 16:24   ` PrasannaKumar Muralidharan
  2020-01-28 11:00 ` [PATCH 3/6] hwrng: imx-rngc - use devres for registration Martin Kaiser
                   ` (4 subsequent siblings)
  6 siblings, 1 reply; 20+ messages in thread
From: Martin Kaiser @ 2020-01-28 11:00 UTC (permalink / raw)
  To: Herbert Xu, PrasannaKumar Muralidharan, NXP Linux Team
  Cc: linux-crypto, linux-arm-kernel, linux-kernel, Martin Kaiser

The rngc requires a new seed for its prng after generating 2^20 160-bit
words of random data. At the moment, we seed the prng only once during
initalisation.

Set the rngc to auto seed mode so that it kicks off the internal
reseeding operation when a new seed is required.

Keep the manual calculation of the initial seed when the device is
probed and switch to automatic seeding afterwards.

Signed-off-by: Martin Kaiser <martin@kaiser.cx>
---
 drivers/char/hw_random/imx-rngc.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/drivers/char/hw_random/imx-rngc.c b/drivers/char/hw_random/imx-rngc.c
index 0576801944fd..903894518c8d 100644
--- a/drivers/char/hw_random/imx-rngc.c
+++ b/drivers/char/hw_random/imx-rngc.c
@@ -31,6 +31,7 @@
 
 #define RNGC_CTRL_MASK_ERROR		0x00000040
 #define RNGC_CTRL_MASK_DONE		0x00000020
+#define RNGC_CTRL_AUTO_SEED		0x00000010
 
 #define RNGC_STATUS_ERROR		0x00010000
 #define RNGC_STATUS_FIFO_LEVEL_MASK	0x00000f00
@@ -167,7 +168,7 @@ static irqreturn_t imx_rngc_irq(int irq, void *priv)
 static int imx_rngc_init(struct hwrng *rng)
 {
 	struct imx_rngc *rngc = container_of(rng, struct imx_rngc, rng);
-	u32 cmd;
+	u32 cmd, ctrl;
 	int ret;
 
 	/* clear error */
@@ -192,7 +193,18 @@ static int imx_rngc_init(struct hwrng *rng)
 
 	} while (rngc->err_reg == RNGC_ERROR_STATUS_STAT_ERR);
 
-	return rngc->err_reg ? -EIO : 0;
+	if (rngc->err_reg)
+		return -EIO;
+
+	/*
+	 * enable automatic seeding, the rngc creates a new seed automatically
+	 * after serving 2^20 random 160-bit words
+	 */
+	ctrl = readl(rngc->base + RNGC_CONTROL);
+	ctrl |= RNGC_CTRL_AUTO_SEED;
+	writel(ctrl, rngc->base + RNGC_CONTROL);
+
+	return 0;
 }
 
 static int imx_rngc_probe(struct platform_device *pdev)
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 3/6] hwrng: imx-rngc - use devres for registration
  2020-01-28 11:00 [PATCH 0/6] imx-rngc - several small fixes Martin Kaiser
  2020-01-28 11:00 ` [PATCH 1/6] hwrng: imx-rngc - fix an error path Martin Kaiser
  2020-01-28 11:00 ` [PATCH 2/6] hwrng: imx-rngc - use automatic seeding Martin Kaiser
@ 2020-01-28 11:00 ` Martin Kaiser
  2020-02-12 16:13   ` PrasannaKumar Muralidharan
  2020-01-28 11:01 ` [PATCH 4/6] hwrng: imx-rngc - (trivial) simplify error prints Martin Kaiser
                   ` (3 subsequent siblings)
  6 siblings, 1 reply; 20+ messages in thread
From: Martin Kaiser @ 2020-01-28 11:00 UTC (permalink / raw)
  To: Herbert Xu, PrasannaKumar Muralidharan, NXP Linux Team
  Cc: linux-crypto, linux-arm-kernel, linux-kernel, Martin Kaiser

Use devres to register the rngc with the hwrng core. Drop the explicit
deregistration.

Signed-off-by: Martin Kaiser <martin@kaiser.cx>
---
 drivers/char/hw_random/imx-rngc.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/char/hw_random/imx-rngc.c b/drivers/char/hw_random/imx-rngc.c
index 903894518c8d..1381ddd5b891 100644
--- a/drivers/char/hw_random/imx-rngc.c
+++ b/drivers/char/hw_random/imx-rngc.c
@@ -263,7 +263,7 @@ static int imx_rngc_probe(struct platform_device *pdev)
 		}
 	}
 
-	ret = hwrng_register(&rngc->rng);
+	ret = devm_hwrng_register(&pdev->dev, &rngc->rng);
 	if (ret) {
 		dev_err(&pdev->dev, "FSL RNGC registering failed (%d)\n", ret);
 		goto err;
@@ -282,8 +282,6 @@ static int __exit imx_rngc_remove(struct platform_device *pdev)
 {
 	struct imx_rngc *rngc = platform_get_drvdata(pdev);
 
-	hwrng_unregister(&rngc->rng);
-
 	clk_disable_unprepare(rngc->clk);
 
 	return 0;
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 4/6] hwrng: imx-rngc - (trivial) simplify error prints
  2020-01-28 11:00 [PATCH 0/6] imx-rngc - several small fixes Martin Kaiser
                   ` (2 preceding siblings ...)
  2020-01-28 11:00 ` [PATCH 3/6] hwrng: imx-rngc - use devres for registration Martin Kaiser
@ 2020-01-28 11:01 ` Martin Kaiser
  2020-02-12 16:24   ` PrasannaKumar Muralidharan
  2020-01-28 11:01 ` [PATCH 5/6] hwrng: imx-rngc - check the rng type Martin Kaiser
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 20+ messages in thread
From: Martin Kaiser @ 2020-01-28 11:01 UTC (permalink / raw)
  To: Herbert Xu, PrasannaKumar Muralidharan, NXP Linux Team
  Cc: linux-crypto, linux-arm-kernel, linux-kernel, Martin Kaiser

Remove the device name, it is added by the dev_...() routines.

Drop the error code as well. It will be shown by the driver core when
the probe operation failed.

Signed-off-by: Martin Kaiser <martin@kaiser.cx>
---
 drivers/char/hw_random/imx-rngc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/char/hw_random/imx-rngc.c b/drivers/char/hw_random/imx-rngc.c
index 1381ddd5b891..8222055b9e9b 100644
--- a/drivers/char/hw_random/imx-rngc.c
+++ b/drivers/char/hw_random/imx-rngc.c
@@ -258,14 +258,14 @@ static int imx_rngc_probe(struct platform_device *pdev)
 	if (self_test) {
 		ret = imx_rngc_self_test(rngc);
 		if (ret) {
-			dev_err(rngc->dev, "FSL RNGC self test failed.\n");
+			dev_err(rngc->dev, "self test failed\n");
 			goto err;
 		}
 	}
 
 	ret = devm_hwrng_register(&pdev->dev, &rngc->rng);
 	if (ret) {
-		dev_err(&pdev->dev, "FSL RNGC registering failed (%d)\n", ret);
+		dev_err(&pdev->dev, "hwrng registration failed\n");
 		goto err;
 	}
 
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 5/6] hwrng: imx-rngc - check the rng type
  2020-01-28 11:00 [PATCH 0/6] imx-rngc - several small fixes Martin Kaiser
                   ` (3 preceding siblings ...)
  2020-01-28 11:01 ` [PATCH 4/6] hwrng: imx-rngc - (trivial) simplify error prints Martin Kaiser
@ 2020-01-28 11:01 ` Martin Kaiser
  2020-02-12 16:23   ` PrasannaKumar Muralidharan
  2020-01-28 11:01 ` [PATCH 6/6] hwrng: imx-rngc - simplify interrupt mask/unmask Martin Kaiser
  2020-03-05 20:58 ` [PATCH v2 0/5] imx-rngc - several small fixes Martin Kaiser
  6 siblings, 1 reply; 20+ messages in thread
From: Martin Kaiser @ 2020-01-28 11:01 UTC (permalink / raw)
  To: Herbert Xu, PrasannaKumar Muralidharan, NXP Linux Team
  Cc: linux-crypto, linux-arm-kernel, linux-kernel, Martin Kaiser

Read the rng type and hardware revision during probe. Fail the probe
operation if the type is not one of rngc or rngb.
(There's also an rnga type, which needs a different driver.)

Display the type and revision in a debug print if probe was successful.

Signed-off-by: Martin Kaiser <martin@kaiser.cx>
---
 drivers/char/hw_random/imx-rngc.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/drivers/char/hw_random/imx-rngc.c b/drivers/char/hw_random/imx-rngc.c
index 8222055b9e9b..27d85fced30b 100644
--- a/drivers/char/hw_random/imx-rngc.c
+++ b/drivers/char/hw_random/imx-rngc.c
@@ -18,12 +18,22 @@
 #include <linux/completion.h>
 #include <linux/io.h>
 
+#define RNGC_VER_ID			0x0000
 #define RNGC_COMMAND			0x0004
 #define RNGC_CONTROL			0x0008
 #define RNGC_STATUS			0x000C
 #define RNGC_ERROR			0x0010
 #define RNGC_FIFO			0x0014
 
+/* the fields in the ver id register */
+#define RNGC_TYPE_SHIFT		28
+#define RNGC_VER_MAJ_SHIFT		8
+
+/* the rng_type field */
+#define RNGC_TYPE_RNGB			0x1
+#define RNGC_TYPE_RNGC			0x2
+
+
 #define RNGC_CMD_CLR_ERR		0x00000020
 #define RNGC_CMD_CLR_INT		0x00000010
 #define RNGC_CMD_SEED			0x00000002
@@ -212,6 +222,8 @@ static int imx_rngc_probe(struct platform_device *pdev)
 	struct imx_rngc *rngc;
 	int ret;
 	int irq;
+	u32 ver_id;
+	u8  rng_type;
 
 	rngc = devm_kzalloc(&pdev->dev, sizeof(*rngc), GFP_KERNEL);
 	if (!rngc)
@@ -237,6 +249,17 @@ static int imx_rngc_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
+	ver_id = readl(rngc->base + RNGC_VER_ID);
+	rng_type = ver_id >> RNGC_TYPE_SHIFT;
+	/*
+	 * This driver supports only RNGC and RNGB. (There's a different
+	 * driver for RNGA.)
+	 */
+	if (rng_type != RNGC_TYPE_RNGC && rng_type != RNGC_TYPE_RNGB) {
+		ret = -ENODEV;
+		goto err;
+	}
+
 	ret = devm_request_irq(&pdev->dev,
 			irq, imx_rngc_irq, 0, pdev->name, (void *)rngc);
 	if (ret) {
@@ -269,7 +292,10 @@ static int imx_rngc_probe(struct platform_device *pdev)
 		goto err;
 	}
 
-	dev_info(&pdev->dev, "Freescale RNGC registered.\n");
+	dev_info(&pdev->dev,
+		"Freescale RNG%c registered (HW revision %d.%02d)\n",
+		rng_type == RNGC_TYPE_RNGB ? 'B' : 'C',
+		(ver_id >> RNGC_VER_MAJ_SHIFT) & 0xff, ver_id & 0xff);
 	return 0;
 
 err:
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 6/6] hwrng: imx-rngc - simplify interrupt mask/unmask
  2020-01-28 11:00 [PATCH 0/6] imx-rngc - several small fixes Martin Kaiser
                   ` (4 preceding siblings ...)
  2020-01-28 11:01 ` [PATCH 5/6] hwrng: imx-rngc - check the rng type Martin Kaiser
@ 2020-01-28 11:01 ` Martin Kaiser
  2020-03-05 20:58 ` [PATCH v2 0/5] imx-rngc - several small fixes Martin Kaiser
  6 siblings, 0 replies; 20+ messages in thread
From: Martin Kaiser @ 2020-01-28 11:01 UTC (permalink / raw)
  To: Herbert Xu, PrasannaKumar Muralidharan, NXP Linux Team
  Cc: linux-crypto, linux-arm-kernel, linux-kernel, Martin Kaiser

Use a simpler approach for masking / unmasking the rngc interrupt:
The interrupt is unmasked while self-test is running and when the rngc
driver is used by the hwrng core.

Mask the interrupt again when self test is finished, regardless of
self test success or failure.

Unmask the interrupt in the init function. Add a cleanup function where
the rngc interrupt is masked again.

Signed-off-by: Martin Kaiser <martin@kaiser.cx>
---
 drivers/char/hw_random/imx-rngc.c | 43 ++++++++++++++++++++-----------
 1 file changed, 28 insertions(+), 15 deletions(-)

diff --git a/drivers/char/hw_random/imx-rngc.c b/drivers/char/hw_random/imx-rngc.c
index 27d85fced30b..3363cbe18a8d 100644
--- a/drivers/char/hw_random/imx-rngc.c
+++ b/drivers/char/hw_random/imx-rngc.c
@@ -111,17 +111,11 @@ static int imx_rngc_self_test(struct imx_rngc *rngc)
 	writel(cmd | RNGC_CMD_SELF_TEST, rngc->base + RNGC_COMMAND);
 
 	ret = wait_for_completion_timeout(&rngc->rng_op_done, RNGC_TIMEOUT);
-	if (!ret) {
-		imx_rngc_irq_mask_clear(rngc);
+	imx_rngc_irq_mask_clear(rngc);
+	if (!ret)
 		return -ETIMEDOUT;
-	}
-
-	if (rngc->err_reg != 0) {
-		imx_rngc_irq_mask_clear(rngc);
-		return -EIO;
-	}
 
-	return 0;
+	return rngc->err_reg ? -EIO : 0;
 }
 
 static int imx_rngc_read(struct hwrng *rng, void *data, size_t max, bool wait)
@@ -185,10 +179,10 @@ static int imx_rngc_init(struct hwrng *rng)
 	cmd = readl(rngc->base + RNGC_COMMAND);
 	writel(cmd | RNGC_CMD_CLR_ERR, rngc->base + RNGC_COMMAND);
 
+	imx_rngc_irq_unmask(rngc);
+
 	/* create seed, repeat while there is some statistical error */
 	do {
-		imx_rngc_irq_unmask(rngc);
-
 		/* seed creation */
 		cmd = readl(rngc->base + RNGC_COMMAND);
 		writel(cmd | RNGC_CMD_SEED, rngc->base + RNGC_COMMAND);
@@ -197,14 +191,16 @@ static int imx_rngc_init(struct hwrng *rng)
 				RNGC_TIMEOUT);
 
 		if (!ret) {
-			imx_rngc_irq_mask_clear(rngc);
-			return -ETIMEDOUT;
+			ret = -ETIMEDOUT;
+			goto err;
 		}
 
 	} while (rngc->err_reg == RNGC_ERROR_STATUS_STAT_ERR);
 
-	if (rngc->err_reg)
-		return -EIO;
+	if (rngc->err_reg) {
+		ret = -EIO;
+		goto err;
+	}
 
 	/*
 	 * enable automatic seeding, the rngc creates a new seed automatically
@@ -214,7 +210,23 @@ static int imx_rngc_init(struct hwrng *rng)
 	ctrl |= RNGC_CTRL_AUTO_SEED;
 	writel(ctrl, rngc->base + RNGC_CONTROL);
 
+	/*
+	 * if initialisation was successful, we keep the interrupt
+	 * unmasked until imx_rngc_cleanup is called
+	 * we mask the interrupt ourselves if we return an error
+	 */
 	return 0;
+
+err:
+	imx_rngc_irq_mask_clear(rngc);
+	return ret;
+}
+
+static void imx_rngc_cleanup(struct hwrng *rng)
+{
+	struct imx_rngc *rngc = container_of(rng, struct imx_rngc, rng);
+
+	imx_rngc_irq_mask_clear(rngc);
 }
 
 static int imx_rngc_probe(struct platform_device *pdev)
@@ -272,6 +284,7 @@ static int imx_rngc_probe(struct platform_device *pdev)
 	rngc->rng.name = pdev->name;
 	rngc->rng.init = imx_rngc_init;
 	rngc->rng.read = imx_rngc_read;
+	rngc->rng.cleanup = imx_rngc_cleanup;
 
 	rngc->dev = &pdev->dev;
 	platform_set_drvdata(pdev, rngc);
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH 3/6] hwrng: imx-rngc - use devres for registration
  2020-01-28 11:00 ` [PATCH 3/6] hwrng: imx-rngc - use devres for registration Martin Kaiser
@ 2020-02-12 16:13   ` PrasannaKumar Muralidharan
  2020-02-17  9:35     ` Martin Kaiser
  0 siblings, 1 reply; 20+ messages in thread
From: PrasannaKumar Muralidharan @ 2020-02-12 16:13 UTC (permalink / raw)
  To: Martin Kaiser
  Cc: Herbert Xu, NXP Linux Team,
	open list:HARDWARE RANDOM NUMBER GENERATOR CORE,
	linux-arm-kernel, open list

Hi Martin,

On Tue, 28 Jan 2020 at 16:31, Martin Kaiser <martin@kaiser.cx> wrote:
>
> Use devres to register the rngc with the hwrng core. Drop the explicit
> deregistration.
>
> Signed-off-by: Martin Kaiser <martin@kaiser.cx>
> ---
>  drivers/char/hw_random/imx-rngc.c | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
>
> diff --git a/drivers/char/hw_random/imx-rngc.c b/drivers/char/hw_random/imx-rngc.c
> index 903894518c8d..1381ddd5b891 100644
> --- a/drivers/char/hw_random/imx-rngc.c
> +++ b/drivers/char/hw_random/imx-rngc.c
> @@ -263,7 +263,7 @@ static int imx_rngc_probe(struct platform_device *pdev)
>                 }
>         }
>
> -       ret = hwrng_register(&rngc->rng);
> +       ret = devm_hwrng_register(&pdev->dev, &rngc->rng);
>         if (ret) {
>                 dev_err(&pdev->dev, "FSL RNGC registering failed (%d)\n", ret);
>                 goto err;
> @@ -282,8 +282,6 @@ static int __exit imx_rngc_remove(struct platform_device *pdev)
>  {
>         struct imx_rngc *rngc = platform_get_drvdata(pdev);
>
> -       hwrng_unregister(&rngc->rng);
> -
>         clk_disable_unprepare(rngc->clk);
>
>         return 0;
> --
> 2.20.1
>

After imx_rngc_remove function hwrng_unregister will get called. This
leaves a window where the clock to rng hardware block is disabled but
still user space can access it via /dev/hwrng. This does not look
right, please revisit the patch.

Regards,
PrasannaKumar

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/6] hwrng: imx-rngc - fix an error path
  2020-01-28 11:00 ` [PATCH 1/6] hwrng: imx-rngc - fix an error path Martin Kaiser
@ 2020-02-12 16:22   ` PrasannaKumar Muralidharan
  0 siblings, 0 replies; 20+ messages in thread
From: PrasannaKumar Muralidharan @ 2020-02-12 16:22 UTC (permalink / raw)
  To: Martin Kaiser
  Cc: Herbert Xu, NXP Linux Team,
	open list:HARDWARE RANDOM NUMBER GENERATOR CORE,
	linux-arm-kernel, open list, stable

Hi Martin,

On Tue, 28 Jan 2020 at 16:31, Martin Kaiser <martin@kaiser.cx> wrote:
>
> Make sure that the rngc interrupt is masked if the rngc self test fails.
> Self test failure means that probe fails as well. Interrupts should be
> masked in this case, regardless of the error.
>
> Cc: stable@vger.kernel.org
> Fixes: 1d5449445bd0 ("hwrng: mx-rngc - add a driver for Freescale RNGC")
> Signed-off-by: Martin Kaiser <martin@kaiser.cx>
> ---
>  drivers/char/hw_random/imx-rngc.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/char/hw_random/imx-rngc.c b/drivers/char/hw_random/imx-rngc.c
> index 30cf00f8e9a0..0576801944fd 100644
> --- a/drivers/char/hw_random/imx-rngc.c
> +++ b/drivers/char/hw_random/imx-rngc.c
> @@ -105,8 +105,10 @@ static int imx_rngc_self_test(struct imx_rngc *rngc)
>                 return -ETIMEDOUT;
>         }
>
> -       if (rngc->err_reg != 0)
> +       if (rngc->err_reg != 0) {
> +               imx_rngc_irq_mask_clear(rngc);
>                 return -EIO;
> +       }
>
>         return 0;
>  }
> --
> 2.20.1
>

Looks good to me. You can add
Reviewed-by: PrasannaKumar Muralidharan <prasannatsmkumar@gmail.com>

Regards,
PrasannaKumar

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 5/6] hwrng: imx-rngc - check the rng type
  2020-01-28 11:01 ` [PATCH 5/6] hwrng: imx-rngc - check the rng type Martin Kaiser
@ 2020-02-12 16:23   ` PrasannaKumar Muralidharan
  0 siblings, 0 replies; 20+ messages in thread
From: PrasannaKumar Muralidharan @ 2020-02-12 16:23 UTC (permalink / raw)
  To: Martin Kaiser
  Cc: Herbert Xu, NXP Linux Team,
	open list:HARDWARE RANDOM NUMBER GENERATOR CORE,
	linux-arm-kernel, open list

On Tue, 28 Jan 2020 at 16:31, Martin Kaiser <martin@kaiser.cx> wrote:
>
> Read the rng type and hardware revision during probe. Fail the probe
> operation if the type is not one of rngc or rngb.
> (There's also an rnga type, which needs a different driver.)
>
> Display the type and revision in a debug print if probe was successful.
>
> Signed-off-by: Martin Kaiser <martin@kaiser.cx>
> ---
>  drivers/char/hw_random/imx-rngc.c | 28 +++++++++++++++++++++++++++-
>  1 file changed, 27 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/char/hw_random/imx-rngc.c b/drivers/char/hw_random/imx-rngc.c
> index 8222055b9e9b..27d85fced30b 100644
> --- a/drivers/char/hw_random/imx-rngc.c
> +++ b/drivers/char/hw_random/imx-rngc.c
> @@ -18,12 +18,22 @@
>  #include <linux/completion.h>
>  #include <linux/io.h>
>
> +#define RNGC_VER_ID                    0x0000
>  #define RNGC_COMMAND                   0x0004
>  #define RNGC_CONTROL                   0x0008
>  #define RNGC_STATUS                    0x000C
>  #define RNGC_ERROR                     0x0010
>  #define RNGC_FIFO                      0x0014
>
> +/* the fields in the ver id register */
> +#define RNGC_TYPE_SHIFT                28
> +#define RNGC_VER_MAJ_SHIFT             8
> +
> +/* the rng_type field */
> +#define RNGC_TYPE_RNGB                 0x1
> +#define RNGC_TYPE_RNGC                 0x2
> +
> +
>  #define RNGC_CMD_CLR_ERR               0x00000020
>  #define RNGC_CMD_CLR_INT               0x00000010
>  #define RNGC_CMD_SEED                  0x00000002
> @@ -212,6 +222,8 @@ static int imx_rngc_probe(struct platform_device *pdev)
>         struct imx_rngc *rngc;
>         int ret;
>         int irq;
> +       u32 ver_id;
> +       u8  rng_type;
>
>         rngc = devm_kzalloc(&pdev->dev, sizeof(*rngc), GFP_KERNEL);
>         if (!rngc)
> @@ -237,6 +249,17 @@ static int imx_rngc_probe(struct platform_device *pdev)
>         if (ret)
>                 return ret;
>
> +       ver_id = readl(rngc->base + RNGC_VER_ID);
> +       rng_type = ver_id >> RNGC_TYPE_SHIFT;
> +       /*
> +        * This driver supports only RNGC and RNGB. (There's a different
> +        * driver for RNGA.)
> +        */
> +       if (rng_type != RNGC_TYPE_RNGC && rng_type != RNGC_TYPE_RNGB) {
> +               ret = -ENODEV;
> +               goto err;
> +       }
> +
>         ret = devm_request_irq(&pdev->dev,
>                         irq, imx_rngc_irq, 0, pdev->name, (void *)rngc);
>         if (ret) {
> @@ -269,7 +292,10 @@ static int imx_rngc_probe(struct platform_device *pdev)
>                 goto err;
>         }
>
> -       dev_info(&pdev->dev, "Freescale RNGC registered.\n");
> +       dev_info(&pdev->dev,
> +               "Freescale RNG%c registered (HW revision %d.%02d)\n",
> +               rng_type == RNGC_TYPE_RNGB ? 'B' : 'C',
> +               (ver_id >> RNGC_VER_MAJ_SHIFT) & 0xff, ver_id & 0xff);
>         return 0;
>
>  err:
> --
> 2.20.1
>

Reviewed-by: PrasannaKumar Muralidharan <prasannatsmkumar@gmail.com>

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 4/6] hwrng: imx-rngc - (trivial) simplify error prints
  2020-01-28 11:01 ` [PATCH 4/6] hwrng: imx-rngc - (trivial) simplify error prints Martin Kaiser
@ 2020-02-12 16:24   ` PrasannaKumar Muralidharan
  0 siblings, 0 replies; 20+ messages in thread
From: PrasannaKumar Muralidharan @ 2020-02-12 16:24 UTC (permalink / raw)
  To: Martin Kaiser
  Cc: Herbert Xu, NXP Linux Team,
	open list:HARDWARE RANDOM NUMBER GENERATOR CORE,
	linux-arm-kernel, open list

On Tue, 28 Jan 2020 at 16:31, Martin Kaiser <martin@kaiser.cx> wrote:
>
> Remove the device name, it is added by the dev_...() routines.
>
> Drop the error code as well. It will be shown by the driver core when
> the probe operation failed.
>
> Signed-off-by: Martin Kaiser <martin@kaiser.cx>
> ---
>  drivers/char/hw_random/imx-rngc.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/char/hw_random/imx-rngc.c b/drivers/char/hw_random/imx-rngc.c
> index 1381ddd5b891..8222055b9e9b 100644
> --- a/drivers/char/hw_random/imx-rngc.c
> +++ b/drivers/char/hw_random/imx-rngc.c
> @@ -258,14 +258,14 @@ static int imx_rngc_probe(struct platform_device *pdev)
>         if (self_test) {
>                 ret = imx_rngc_self_test(rngc);
>                 if (ret) {
> -                       dev_err(rngc->dev, "FSL RNGC self test failed.\n");
> +                       dev_err(rngc->dev, "self test failed\n");
>                         goto err;
>                 }
>         }
>
>         ret = devm_hwrng_register(&pdev->dev, &rngc->rng);
>         if (ret) {
> -               dev_err(&pdev->dev, "FSL RNGC registering failed (%d)\n", ret);
> +               dev_err(&pdev->dev, "hwrng registration failed\n");
>                 goto err;
>         }
>
> --
> 2.20.1
>

Reviewed-by: PrasannaKumar Muralidharan <prasannatsmkumar@gmail.com>

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 2/6] hwrng: imx-rngc - use automatic seeding
  2020-01-28 11:00 ` [PATCH 2/6] hwrng: imx-rngc - use automatic seeding Martin Kaiser
@ 2020-02-12 16:24   ` PrasannaKumar Muralidharan
  0 siblings, 0 replies; 20+ messages in thread
From: PrasannaKumar Muralidharan @ 2020-02-12 16:24 UTC (permalink / raw)
  To: Martin Kaiser
  Cc: Herbert Xu, NXP Linux Team,
	open list:HARDWARE RANDOM NUMBER GENERATOR CORE,
	linux-arm-kernel, open list

On Tue, 28 Jan 2020 at 16:31, Martin Kaiser <martin@kaiser.cx> wrote:
>
> The rngc requires a new seed for its prng after generating 2^20 160-bit
> words of random data. At the moment, we seed the prng only once during
> initalisation.
>
> Set the rngc to auto seed mode so that it kicks off the internal
> reseeding operation when a new seed is required.
>
> Keep the manual calculation of the initial seed when the device is
> probed and switch to automatic seeding afterwards.
>
> Signed-off-by: Martin Kaiser <martin@kaiser.cx>
> ---
>  drivers/char/hw_random/imx-rngc.c | 16 ++++++++++++++--
>  1 file changed, 14 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/char/hw_random/imx-rngc.c b/drivers/char/hw_random/imx-rngc.c
> index 0576801944fd..903894518c8d 100644
> --- a/drivers/char/hw_random/imx-rngc.c
> +++ b/drivers/char/hw_random/imx-rngc.c
> @@ -31,6 +31,7 @@
>
>  #define RNGC_CTRL_MASK_ERROR           0x00000040
>  #define RNGC_CTRL_MASK_DONE            0x00000020
> +#define RNGC_CTRL_AUTO_SEED            0x00000010
>
>  #define RNGC_STATUS_ERROR              0x00010000
>  #define RNGC_STATUS_FIFO_LEVEL_MASK    0x00000f00
> @@ -167,7 +168,7 @@ static irqreturn_t imx_rngc_irq(int irq, void *priv)
>  static int imx_rngc_init(struct hwrng *rng)
>  {
>         struct imx_rngc *rngc = container_of(rng, struct imx_rngc, rng);
> -       u32 cmd;
> +       u32 cmd, ctrl;
>         int ret;
>
>         /* clear error */
> @@ -192,7 +193,18 @@ static int imx_rngc_init(struct hwrng *rng)
>
>         } while (rngc->err_reg == RNGC_ERROR_STATUS_STAT_ERR);
>
> -       return rngc->err_reg ? -EIO : 0;
> +       if (rngc->err_reg)
> +               return -EIO;
> +
> +       /*
> +        * enable automatic seeding, the rngc creates a new seed automatically
> +        * after serving 2^20 random 160-bit words
> +        */
> +       ctrl = readl(rngc->base + RNGC_CONTROL);
> +       ctrl |= RNGC_CTRL_AUTO_SEED;
> +       writel(ctrl, rngc->base + RNGC_CONTROL);
> +
> +       return 0;
>  }
>
>  static int imx_rngc_probe(struct platform_device *pdev)
> --
> 2.20.1
>

Reviewed-by: PrasannaKumar Muralidharan <prasannatsmkumar@gmail.com>

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 3/6] hwrng: imx-rngc - use devres for registration
  2020-02-12 16:13   ` PrasannaKumar Muralidharan
@ 2020-02-17  9:35     ` Martin Kaiser
  0 siblings, 0 replies; 20+ messages in thread
From: Martin Kaiser @ 2020-02-17  9:35 UTC (permalink / raw)
  To: PrasannaKumar Muralidharan
  Cc: Herbert Xu, NXP Linux Team,
	open list:HARDWARE RANDOM NUMBER GENERATOR CORE,
	linux-arm-kernel, open list

Hi PrasannaKumar,

Thus wrote PrasannaKumar Muralidharan (prasannatsmkumar@gmail.com):

> After imx_rngc_remove function hwrng_unregister will get called. This
> leaves a window where the clock to rng hardware block is disabled but
> still user space can access it via /dev/hwrng.

thanks for spotting this issue. I see that in __device_release_driver,
the driver's remove function is called before the devres cleanup.

> This does not look right, please revisit the patch.

I checked again how other hwrng drivers use devres. Some don't have to
disable a clock and need no remove function at all. Others enable the
clock in the hwrng init routine and disable it in the cleanup routine.

Both of these approaches don't work here. I should disable the clock
eventually and I need it in the probe function to run the selftest
before hwrng init is called.

Therefore, I suggest to drop this patch, at least for the moment.
Herbert, should I resend the series without this patch or is it ok for
you to take the remaining patches as-is?

BTW, 3e75241be808 ("hwrng: drivers - Use device-managed registration
API") makes the same change that I proposed here for a couple of other
hwrng drivers and seems to introduce the same race condition in som
drivers e.g. drivers/char/hw_random/exynos-trng.c. Should we try to fix
this?

Thanks,
Martin

^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v2 0/5] imx-rngc - several small fixes
  2020-01-28 11:00 [PATCH 0/6] imx-rngc - several small fixes Martin Kaiser
                   ` (5 preceding siblings ...)
  2020-01-28 11:01 ` [PATCH 6/6] hwrng: imx-rngc - simplify interrupt mask/unmask Martin Kaiser
@ 2020-03-05 20:58 ` Martin Kaiser
  2020-03-05 20:58   ` [PATCH v2 1/5] hwrng: imx-rngc - fix an error path Martin Kaiser
                     ` (5 more replies)
  6 siblings, 6 replies; 20+ messages in thread
From: Martin Kaiser @ 2020-03-05 20:58 UTC (permalink / raw)
  To: Herbert Xu, PrasannaKumar Muralidharan, NXP Linux Team
  Cc: linux-crypto, linux-arm-kernel, linux-kernel, Martin Kaiser

This is a set of small fixes for the imx-rngc driver.

I tried to clarify the approach for masking/unmasking the interrupt from
the rngc.

The rngc should be set to auto-seed mode, where it creates a new seed
when required.

In the probe function, we should check that the rng type is supported by
this driver.

Thanks for reviewing the patches,

   Martin

changes in v2:
- remove the contentious devres patch
- add PrasannaKumar's tags

Martin Kaiser (5):
  hwrng: imx-rngc - fix an error path
  hwrng: imx-rngc - use automatic seeding
  hwrng: imx-rngc - (trivial) simplify error prints
  hwrng: imx-rngc - check the rng type
  hwrng: imx-rngc - simplify interrupt mask/unmask

 drivers/char/hw_random/imx-rngc.c | 85 +++++++++++++++++++++++++------
 1 file changed, 69 insertions(+), 16 deletions(-)

-- 
2.20.1


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v2 1/5] hwrng: imx-rngc - fix an error path
  2020-03-05 20:58 ` [PATCH v2 0/5] imx-rngc - several small fixes Martin Kaiser
@ 2020-03-05 20:58   ` Martin Kaiser
  2020-03-05 20:58   ` [PATCH v2 2/5] hwrng: imx-rngc - use automatic seeding Martin Kaiser
                     ` (4 subsequent siblings)
  5 siblings, 0 replies; 20+ messages in thread
From: Martin Kaiser @ 2020-03-05 20:58 UTC (permalink / raw)
  To: Herbert Xu, PrasannaKumar Muralidharan, NXP Linux Team
  Cc: linux-crypto, linux-arm-kernel, linux-kernel, Martin Kaiser, stable

Make sure that the rngc interrupt is masked if the rngc self test fails.
Self test failure means that probe fails as well. Interrupts should be
masked in this case, regardless of the error.

Cc: stable@vger.kernel.org
Fixes: 1d5449445bd0 ("hwrng: mx-rngc - add a driver for Freescale RNGC")
Reviewed-by: PrasannaKumar Muralidharan <prasannatsmkumar@gmail.com>
Signed-off-by: Martin Kaiser <martin@kaiser.cx>
---
 drivers/char/hw_random/imx-rngc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/char/hw_random/imx-rngc.c b/drivers/char/hw_random/imx-rngc.c
index 30cf00f8e9a0..0576801944fd 100644
--- a/drivers/char/hw_random/imx-rngc.c
+++ b/drivers/char/hw_random/imx-rngc.c
@@ -105,8 +105,10 @@ static int imx_rngc_self_test(struct imx_rngc *rngc)
 		return -ETIMEDOUT;
 	}
 
-	if (rngc->err_reg != 0)
+	if (rngc->err_reg != 0) {
+		imx_rngc_irq_mask_clear(rngc);
 		return -EIO;
+	}
 
 	return 0;
 }
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH v2 2/5] hwrng: imx-rngc - use automatic seeding
  2020-03-05 20:58 ` [PATCH v2 0/5] imx-rngc - several small fixes Martin Kaiser
  2020-03-05 20:58   ` [PATCH v2 1/5] hwrng: imx-rngc - fix an error path Martin Kaiser
@ 2020-03-05 20:58   ` Martin Kaiser
  2020-03-05 20:58   ` [PATCH v2 3/5] hwrng: imx-rngc - (trivial) simplify error prints Martin Kaiser
                     ` (3 subsequent siblings)
  5 siblings, 0 replies; 20+ messages in thread
From: Martin Kaiser @ 2020-03-05 20:58 UTC (permalink / raw)
  To: Herbert Xu, PrasannaKumar Muralidharan, NXP Linux Team
  Cc: linux-crypto, linux-arm-kernel, linux-kernel, Martin Kaiser

The rngc requires a new seed for its prng after generating 2^20 160-bit
words of random data. At the moment, we seed the prng only once during
initalisation.

Set the rngc to auto seed mode so that it kicks off the internal
reseeding operation when a new seed is required.

Keep the manual calculation of the initial seed when the device is
probed and switch to automatic seeding afterwards.

Reviewed-by: PrasannaKumar Muralidharan <prasannatsmkumar@gmail.com>
Signed-off-by: Martin Kaiser <martin@kaiser.cx>
---
 drivers/char/hw_random/imx-rngc.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/drivers/char/hw_random/imx-rngc.c b/drivers/char/hw_random/imx-rngc.c
index 0576801944fd..903894518c8d 100644
--- a/drivers/char/hw_random/imx-rngc.c
+++ b/drivers/char/hw_random/imx-rngc.c
@@ -31,6 +31,7 @@
 
 #define RNGC_CTRL_MASK_ERROR		0x00000040
 #define RNGC_CTRL_MASK_DONE		0x00000020
+#define RNGC_CTRL_AUTO_SEED		0x00000010
 
 #define RNGC_STATUS_ERROR		0x00010000
 #define RNGC_STATUS_FIFO_LEVEL_MASK	0x00000f00
@@ -167,7 +168,7 @@ static irqreturn_t imx_rngc_irq(int irq, void *priv)
 static int imx_rngc_init(struct hwrng *rng)
 {
 	struct imx_rngc *rngc = container_of(rng, struct imx_rngc, rng);
-	u32 cmd;
+	u32 cmd, ctrl;
 	int ret;
 
 	/* clear error */
@@ -192,7 +193,18 @@ static int imx_rngc_init(struct hwrng *rng)
 
 	} while (rngc->err_reg == RNGC_ERROR_STATUS_STAT_ERR);
 
-	return rngc->err_reg ? -EIO : 0;
+	if (rngc->err_reg)
+		return -EIO;
+
+	/*
+	 * enable automatic seeding, the rngc creates a new seed automatically
+	 * after serving 2^20 random 160-bit words
+	 */
+	ctrl = readl(rngc->base + RNGC_CONTROL);
+	ctrl |= RNGC_CTRL_AUTO_SEED;
+	writel(ctrl, rngc->base + RNGC_CONTROL);
+
+	return 0;
 }
 
 static int imx_rngc_probe(struct platform_device *pdev)
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH v2 3/5] hwrng: imx-rngc - (trivial) simplify error prints
  2020-03-05 20:58 ` [PATCH v2 0/5] imx-rngc - several small fixes Martin Kaiser
  2020-03-05 20:58   ` [PATCH v2 1/5] hwrng: imx-rngc - fix an error path Martin Kaiser
  2020-03-05 20:58   ` [PATCH v2 2/5] hwrng: imx-rngc - use automatic seeding Martin Kaiser
@ 2020-03-05 20:58   ` Martin Kaiser
  2020-03-05 20:58   ` [PATCH v2 4/5] hwrng: imx-rngc - check the rng type Martin Kaiser
                     ` (2 subsequent siblings)
  5 siblings, 0 replies; 20+ messages in thread
From: Martin Kaiser @ 2020-03-05 20:58 UTC (permalink / raw)
  To: Herbert Xu, PrasannaKumar Muralidharan, NXP Linux Team
  Cc: linux-crypto, linux-arm-kernel, linux-kernel, Martin Kaiser

Remove the device name, it is added by the dev_...() routines.

Drop the error code as well. It will be shown by the driver core when
the probe operation failed.

Reviewed-by: PrasannaKumar Muralidharan <prasannatsmkumar@gmail.com>
Signed-off-by: Martin Kaiser <martin@kaiser.cx>
---
 drivers/char/hw_random/imx-rngc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/char/hw_random/imx-rngc.c b/drivers/char/hw_random/imx-rngc.c
index 903894518c8d..92e93abcc9cc 100644
--- a/drivers/char/hw_random/imx-rngc.c
+++ b/drivers/char/hw_random/imx-rngc.c
@@ -258,14 +258,14 @@ static int imx_rngc_probe(struct platform_device *pdev)
 	if (self_test) {
 		ret = imx_rngc_self_test(rngc);
 		if (ret) {
-			dev_err(rngc->dev, "FSL RNGC self test failed.\n");
+			dev_err(rngc->dev, "self test failed\n");
 			goto err;
 		}
 	}
 
 	ret = hwrng_register(&rngc->rng);
 	if (ret) {
-		dev_err(&pdev->dev, "FSL RNGC registering failed (%d)\n", ret);
+		dev_err(&pdev->dev, "hwrng registration failed\n");
 		goto err;
 	}
 
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH v2 4/5] hwrng: imx-rngc - check the rng type
  2020-03-05 20:58 ` [PATCH v2 0/5] imx-rngc - several small fixes Martin Kaiser
                     ` (2 preceding siblings ...)
  2020-03-05 20:58   ` [PATCH v2 3/5] hwrng: imx-rngc - (trivial) simplify error prints Martin Kaiser
@ 2020-03-05 20:58   ` Martin Kaiser
  2020-03-05 20:58   ` [PATCH v2 5/5] hwrng: imx-rngc - simplify interrupt mask/unmask Martin Kaiser
  2020-03-12 12:39   ` [PATCH v2 0/5] imx-rngc - several small fixes Herbert Xu
  5 siblings, 0 replies; 20+ messages in thread
From: Martin Kaiser @ 2020-03-05 20:58 UTC (permalink / raw)
  To: Herbert Xu, PrasannaKumar Muralidharan, NXP Linux Team
  Cc: linux-crypto, linux-arm-kernel, linux-kernel, Martin Kaiser

Read the rng type and hardware revision during probe. Fail the probe
operation if the type is not one of rngc or rngb.
(There's also an rnga type, which needs a different driver.)

Display the type and revision in a debug print if probe was successful.

Reviewed-by: PrasannaKumar Muralidharan <prasannatsmkumar@gmail.com>
Signed-off-by: Martin Kaiser <martin@kaiser.cx>
---
 drivers/char/hw_random/imx-rngc.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/drivers/char/hw_random/imx-rngc.c b/drivers/char/hw_random/imx-rngc.c
index 92e93abcc9cc..50a8923d829a 100644
--- a/drivers/char/hw_random/imx-rngc.c
+++ b/drivers/char/hw_random/imx-rngc.c
@@ -18,12 +18,22 @@
 #include <linux/completion.h>
 #include <linux/io.h>
 
+#define RNGC_VER_ID			0x0000
 #define RNGC_COMMAND			0x0004
 #define RNGC_CONTROL			0x0008
 #define RNGC_STATUS			0x000C
 #define RNGC_ERROR			0x0010
 #define RNGC_FIFO			0x0014
 
+/* the fields in the ver id register */
+#define RNGC_TYPE_SHIFT		28
+#define RNGC_VER_MAJ_SHIFT		8
+
+/* the rng_type field */
+#define RNGC_TYPE_RNGB			0x1
+#define RNGC_TYPE_RNGC			0x2
+
+
 #define RNGC_CMD_CLR_ERR		0x00000020
 #define RNGC_CMD_CLR_INT		0x00000010
 #define RNGC_CMD_SEED			0x00000002
@@ -212,6 +222,8 @@ static int imx_rngc_probe(struct platform_device *pdev)
 	struct imx_rngc *rngc;
 	int ret;
 	int irq;
+	u32 ver_id;
+	u8  rng_type;
 
 	rngc = devm_kzalloc(&pdev->dev, sizeof(*rngc), GFP_KERNEL);
 	if (!rngc)
@@ -237,6 +249,17 @@ static int imx_rngc_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
+	ver_id = readl(rngc->base + RNGC_VER_ID);
+	rng_type = ver_id >> RNGC_TYPE_SHIFT;
+	/*
+	 * This driver supports only RNGC and RNGB. (There's a different
+	 * driver for RNGA.)
+	 */
+	if (rng_type != RNGC_TYPE_RNGC && rng_type != RNGC_TYPE_RNGB) {
+		ret = -ENODEV;
+		goto err;
+	}
+
 	ret = devm_request_irq(&pdev->dev,
 			irq, imx_rngc_irq, 0, pdev->name, (void *)rngc);
 	if (ret) {
@@ -269,7 +292,10 @@ static int imx_rngc_probe(struct platform_device *pdev)
 		goto err;
 	}
 
-	dev_info(&pdev->dev, "Freescale RNGC registered.\n");
+	dev_info(&pdev->dev,
+		"Freescale RNG%c registered (HW revision %d.%02d)\n",
+		rng_type == RNGC_TYPE_RNGB ? 'B' : 'C',
+		(ver_id >> RNGC_VER_MAJ_SHIFT) & 0xff, ver_id & 0xff);
 	return 0;
 
 err:
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH v2 5/5] hwrng: imx-rngc - simplify interrupt mask/unmask
  2020-03-05 20:58 ` [PATCH v2 0/5] imx-rngc - several small fixes Martin Kaiser
                     ` (3 preceding siblings ...)
  2020-03-05 20:58   ` [PATCH v2 4/5] hwrng: imx-rngc - check the rng type Martin Kaiser
@ 2020-03-05 20:58   ` Martin Kaiser
  2020-03-12 12:39   ` [PATCH v2 0/5] imx-rngc - several small fixes Herbert Xu
  5 siblings, 0 replies; 20+ messages in thread
From: Martin Kaiser @ 2020-03-05 20:58 UTC (permalink / raw)
  To: Herbert Xu, PrasannaKumar Muralidharan, NXP Linux Team
  Cc: linux-crypto, linux-arm-kernel, linux-kernel, Martin Kaiser

Use a simpler approach for masking / unmasking the rngc interrupt:
The interrupt is unmasked while self-test is running and when the rngc
driver is used by the hwrng core.

Mask the interrupt again when self test is finished, regardless of
self test success or failure.

Unmask the interrupt in the init function. Add a cleanup function where
the rngc interrupt is masked again.

Signed-off-by: Martin Kaiser <martin@kaiser.cx>
---
 drivers/char/hw_random/imx-rngc.c | 43 ++++++++++++++++++++-----------
 1 file changed, 28 insertions(+), 15 deletions(-)

diff --git a/drivers/char/hw_random/imx-rngc.c b/drivers/char/hw_random/imx-rngc.c
index 50a8923d829a..9c47e431ce90 100644
--- a/drivers/char/hw_random/imx-rngc.c
+++ b/drivers/char/hw_random/imx-rngc.c
@@ -111,17 +111,11 @@ static int imx_rngc_self_test(struct imx_rngc *rngc)
 	writel(cmd | RNGC_CMD_SELF_TEST, rngc->base + RNGC_COMMAND);
 
 	ret = wait_for_completion_timeout(&rngc->rng_op_done, RNGC_TIMEOUT);
-	if (!ret) {
-		imx_rngc_irq_mask_clear(rngc);
+	imx_rngc_irq_mask_clear(rngc);
+	if (!ret)
 		return -ETIMEDOUT;
-	}
-
-	if (rngc->err_reg != 0) {
-		imx_rngc_irq_mask_clear(rngc);
-		return -EIO;
-	}
 
-	return 0;
+	return rngc->err_reg ? -EIO : 0;
 }
 
 static int imx_rngc_read(struct hwrng *rng, void *data, size_t max, bool wait)
@@ -185,10 +179,10 @@ static int imx_rngc_init(struct hwrng *rng)
 	cmd = readl(rngc->base + RNGC_COMMAND);
 	writel(cmd | RNGC_CMD_CLR_ERR, rngc->base + RNGC_COMMAND);
 
+	imx_rngc_irq_unmask(rngc);
+
 	/* create seed, repeat while there is some statistical error */
 	do {
-		imx_rngc_irq_unmask(rngc);
-
 		/* seed creation */
 		cmd = readl(rngc->base + RNGC_COMMAND);
 		writel(cmd | RNGC_CMD_SEED, rngc->base + RNGC_COMMAND);
@@ -197,14 +191,16 @@ static int imx_rngc_init(struct hwrng *rng)
 				RNGC_TIMEOUT);
 
 		if (!ret) {
-			imx_rngc_irq_mask_clear(rngc);
-			return -ETIMEDOUT;
+			ret = -ETIMEDOUT;
+			goto err;
 		}
 
 	} while (rngc->err_reg == RNGC_ERROR_STATUS_STAT_ERR);
 
-	if (rngc->err_reg)
-		return -EIO;
+	if (rngc->err_reg) {
+		ret = -EIO;
+		goto err;
+	}
 
 	/*
 	 * enable automatic seeding, the rngc creates a new seed automatically
@@ -214,7 +210,23 @@ static int imx_rngc_init(struct hwrng *rng)
 	ctrl |= RNGC_CTRL_AUTO_SEED;
 	writel(ctrl, rngc->base + RNGC_CONTROL);
 
+	/*
+	 * if initialisation was successful, we keep the interrupt
+	 * unmasked until imx_rngc_cleanup is called
+	 * we mask the interrupt ourselves if we return an error
+	 */
 	return 0;
+
+err:
+	imx_rngc_irq_mask_clear(rngc);
+	return ret;
+}
+
+static void imx_rngc_cleanup(struct hwrng *rng)
+{
+	struct imx_rngc *rngc = container_of(rng, struct imx_rngc, rng);
+
+	imx_rngc_irq_mask_clear(rngc);
 }
 
 static int imx_rngc_probe(struct platform_device *pdev)
@@ -272,6 +284,7 @@ static int imx_rngc_probe(struct platform_device *pdev)
 	rngc->rng.name = pdev->name;
 	rngc->rng.init = imx_rngc_init;
 	rngc->rng.read = imx_rngc_read;
+	rngc->rng.cleanup = imx_rngc_cleanup;
 
 	rngc->dev = &pdev->dev;
 	platform_set_drvdata(pdev, rngc);
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH v2 0/5] imx-rngc - several small fixes
  2020-03-05 20:58 ` [PATCH v2 0/5] imx-rngc - several small fixes Martin Kaiser
                     ` (4 preceding siblings ...)
  2020-03-05 20:58   ` [PATCH v2 5/5] hwrng: imx-rngc - simplify interrupt mask/unmask Martin Kaiser
@ 2020-03-12 12:39   ` Herbert Xu
  5 siblings, 0 replies; 20+ messages in thread
From: Herbert Xu @ 2020-03-12 12:39 UTC (permalink / raw)
  To: Martin Kaiser
  Cc: PrasannaKumar Muralidharan, NXP Linux Team, linux-crypto,
	linux-arm-kernel, linux-kernel

On Thu, Mar 05, 2020 at 09:58:19PM +0100, Martin Kaiser wrote:
> This is a set of small fixes for the imx-rngc driver.
> 
> I tried to clarify the approach for masking/unmasking the interrupt from
> the rngc.
> 
> The rngc should be set to auto-seed mode, where it creates a new seed
> when required.
> 
> In the probe function, we should check that the rng type is supported by
> this driver.
> 
> Thanks for reviewing the patches,
> 
>    Martin
> 
> changes in v2:
> - remove the contentious devres patch
> - add PrasannaKumar's tags
> 
> Martin Kaiser (5):
>   hwrng: imx-rngc - fix an error path
>   hwrng: imx-rngc - use automatic seeding
>   hwrng: imx-rngc - (trivial) simplify error prints
>   hwrng: imx-rngc - check the rng type
>   hwrng: imx-rngc - simplify interrupt mask/unmask
> 
>  drivers/char/hw_random/imx-rngc.c | 85 +++++++++++++++++++++++++------
>  1 file changed, 69 insertions(+), 16 deletions(-)

All applied.  Thanks.
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2020-03-12 12:40 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-28 11:00 [PATCH 0/6] imx-rngc - several small fixes Martin Kaiser
2020-01-28 11:00 ` [PATCH 1/6] hwrng: imx-rngc - fix an error path Martin Kaiser
2020-02-12 16:22   ` PrasannaKumar Muralidharan
2020-01-28 11:00 ` [PATCH 2/6] hwrng: imx-rngc - use automatic seeding Martin Kaiser
2020-02-12 16:24   ` PrasannaKumar Muralidharan
2020-01-28 11:00 ` [PATCH 3/6] hwrng: imx-rngc - use devres for registration Martin Kaiser
2020-02-12 16:13   ` PrasannaKumar Muralidharan
2020-02-17  9:35     ` Martin Kaiser
2020-01-28 11:01 ` [PATCH 4/6] hwrng: imx-rngc - (trivial) simplify error prints Martin Kaiser
2020-02-12 16:24   ` PrasannaKumar Muralidharan
2020-01-28 11:01 ` [PATCH 5/6] hwrng: imx-rngc - check the rng type Martin Kaiser
2020-02-12 16:23   ` PrasannaKumar Muralidharan
2020-01-28 11:01 ` [PATCH 6/6] hwrng: imx-rngc - simplify interrupt mask/unmask Martin Kaiser
2020-03-05 20:58 ` [PATCH v2 0/5] imx-rngc - several small fixes Martin Kaiser
2020-03-05 20:58   ` [PATCH v2 1/5] hwrng: imx-rngc - fix an error path Martin Kaiser
2020-03-05 20:58   ` [PATCH v2 2/5] hwrng: imx-rngc - use automatic seeding Martin Kaiser
2020-03-05 20:58   ` [PATCH v2 3/5] hwrng: imx-rngc - (trivial) simplify error prints Martin Kaiser
2020-03-05 20:58   ` [PATCH v2 4/5] hwrng: imx-rngc - check the rng type Martin Kaiser
2020-03-05 20:58   ` [PATCH v2 5/5] hwrng: imx-rngc - simplify interrupt mask/unmask Martin Kaiser
2020-03-12 12:39   ` [PATCH v2 0/5] imx-rngc - several small fixes Herbert Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).