mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v1 0/2] Add FCS Random Number Generator (RNG) driver
@ 2026-11-20  9:51 hang.suan.wang
  2026-11-20  9:51 ` [PATCH v1 1/2] firmware: stratix10-svc: add FCS random-number command for Agilex 5 hang.suan.wang
  2026-11-20  9:51 ` [PATCH v1 2/2] hwrng: socfpga-fcs - Add FCS random number generator driver hang.suan.wang
  0 siblings, 2 replies; 3+ messages in thread
From: hang.suan.wang @ 2026-11-20  9:51 UTC (permalink / raw)
  To: Dinh Nguyen, linux-kernel, Olivia Mackall, Herbert Xu,
	Eric Biggers, Miquel Raynal, Wolfram Sang, Frank Li,
	Jean-Michel Hautbois, linux-crypto
  Cc: muhammad.nazim.amirul.nazle.asmade, tze.yee.ng, chee.nouk.phoon,
	genevieve.chan, adrian.ho.yin.ng

From: Hang Suan Wang <hang.suan.wang@altera.com>

This series adds a hw_random front-end for the random-number service of
the Altera SoCFPGA Crypto Service (FCS).

The driver owns no session or service-layer state: every request goes
through the FCS core, which serializes access to the SDM crypto session
and forwards it to the SDM through ATF.

The SDM is the hardware security controller in Altera SoCFPGA devices. Host
software reaches it through the existing stratix10-svc mailbox, which uses
Arm Trusted Firmware SIP SMC calls.

The Agilex FPGA series generator is an SP 800-90A compliant DRNG hardware
block in the SDM, in particular, Agilex 7 is SP 800-90A compliant DRBG
generation flow and is seeded with the TRNG hardware block.

Unlike existing hw_random drivers, this RNG produces data only while an
SDM crypto session is open (for example, via FCS_IOC_OPEN_SESSION on
/dev/socfpga-fcs). The session belongs to the file that opened it, not
to this driver. With no session open, socfpga_fcs_core_engine() returns
-ENOTCONN and the error is passed through unchanged: read() on
/dev/hwrng fails with that error, and the hw_random entropy thread treats
it as a transient failure and retries. The open and close session ioctl
was added in the previous series:
https://lore.kernel.org/all/179492434088.3111216.6016447945055810112.b4-ty@kernel.org/

The ATF accepts only a non-zero, 4-byte-aligned length, from 4 bytes up
to 4080 bytes per request.

hwrng.quality is left at 0. The hw_random core documents 0 as the maximum
(1024 bits of entropy per 1024 bits of input), and with the default
quality of 1024 that is the value applied at registration. That matches
a conditioned SP 800-90A DRNG, so the driver does not publish a separate
estimate.

The series is organized as follows:
Patch 1 (prerequisite) extends the stratix10-svc service layer with the
FCS command codes and matching SIP SMC function IDs.

Patch 2 adds the RNG driver: socfpga-fcs-rng hw_random driver. Add
FCS_DEV_RANDOM_NUMBER_GEN_EXT in the FCS core. The socfpga-fcs-rng child
device registered by the FCS driver. Also, add MAINTAINERS entry for
this new driver.

Testing:
Built for arm64 (defconfig + CONFIG_HW_RANDOM_SOCFPGA_FCS=m).
Tested on an Agilex 5 SoC FPGA board
root@testsocfpga# ./fcs_client -R dbg.bin -j 64 && hexdump -C dbg.bin
00000000 15 fc 77 a9 86 17 0b b5 91 b3 db ce a9 39 48 f1 |..w..........9H.|
00000010 65 00 80 9a 63 b8 1e 99 df 56 6f b4 ef 66 73 f9 |e...c....Vo..fs.|
00000020 91 66 c6 94 94 38 f1 9f cd 3a c7 6e b1 63 1d e2 |.f...8...:.n.c..|
00000030 03 ec 29 01 4f 27 4a de e7 7e 67 95 c2 44 e7 04 |..).O'J..~g..D..|
00000040

Set timeout to 1000s for operation: rngtest
cat /dev/hwrng | rngtest -c 2000 2>&1
rngtest 6.17
Copyright (c) 2004 by Henrique de Moraes Holschuh
This is free software; see the source for copying conditions. There is NO 
warranty;
not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

	rngtest: starting FIPS tests...
	rngtest: bits received from input: 40000032
	rngtest: FIPS 140-2 successes: 1998
	rngtest: FIPS 140-2 failures: 2
	rngtest: FIPS 140-2(2001-10-10) Monobit: 0
	rngtest: FIPS 140-2(2001-10-10) Poker: 0
	rngtest: FIPS 140-2(2001-10-10) Runs: 1
	rngtest: FIPS 140-2(2001-10-10) Long run: 1
	rngtest: FIPS 140-2(2001-10-10) Continuous run: 0
	rngtest: input channel speed: (min=495.429; avg=52178.721; 
								   max=10000000000.000)bits/s
	rngtest: FIPS tests speed: (min=47.329; avg=70.926; max=91.699)Mibits/s
	rngtest: Program run time: 807497698 microseconds

---
Hang Suan Wang (2):
  firmware: stratix10-svc: add FCS random-number command for Agilex 5
  hwrng: socfpga-fcs - Add FCS random number generator driver

 MAINTAINERS                                   |   1 +
 drivers/char/hw_random/Kconfig                |  12 ++
 drivers/char/hw_random/Makefile               |   1 +
 drivers/char/hw_random/socfpga-fcs-rng.c      | 133 ++++++++++++++++++
 drivers/firmware/socfpga-fcs-core.c           |  71 +++++++---
 drivers/firmware/socfpga-fcs.c                |  33 +++++
 drivers/firmware/stratix10-svc.c              |   8 ++
 include/linux/firmware/intel/socfpga-fcs.h    |  23 ++-
 include/linux/firmware/intel/stratix10-smc.h  |  46 ++++++
 .../firmware/intel/stratix10-svc-client.h     |   1 +
 10 files changed, 300 insertions(+), 29 deletions(-)
 create mode 100644 drivers/char/hw_random/socfpga-fcs-rng.c

-- 
2.43.7


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

* [PATCH v1 1/2] firmware: stratix10-svc: add FCS random-number command for Agilex 5
  2026-11-20  9:51 [PATCH v1 0/2] Add FCS Random Number Generator (RNG) driver hang.suan.wang
@ 2026-11-20  9:51 ` hang.suan.wang
  2026-11-20  9:51 ` [PATCH v1 2/2] hwrng: socfpga-fcs - Add FCS random number generator driver hang.suan.wang
  1 sibling, 0 replies; 3+ messages in thread
From: hang.suan.wang @ 2026-11-20  9:51 UTC (permalink / raw)
  To: Dinh Nguyen, linux-kernel, Olivia Mackall, Herbert Xu,
	Eric Biggers, Miquel Raynal, Wolfram Sang, Frank Li,
	Jean-Michel Hautbois, linux-crypto
  Cc: muhammad.nazim.amirul.nazle.asmade, tze.yee.ng, chee.nouk.phoon,
	genevieve.chan, adrian.ho.yin.ng

From: Hang Suan Wang <hang.suan.wang@altera.com>

The Agilex 5 Secure Device Manager (SDM 1.5) exposes an FPGA Crypto
Service (FCS) random-number primitive over the existing SIP SMC
mailbox: a session-based interface, alongside SDOS encrypt/decrypt.
The service layer has no command to drive it yet.

Configure stratix10-svc about this interface so an in-kernel FCS client
can use it:

  - add the asynchronous client command code
    COMMAND_FCS_RANDOM_NUMBER_GEN_EXT

  - add the matching SIP SMC function IDs
    INTEL_SIP_SMC_FCS_RANDOM_NUMBER_EXT and
    INTEL_SIP_SMC_ASYNC_FCS_RANDOM_NUMBER_EXT, with their register-usage
    documentation

The transport is unchanged: Agilex 5 reuses the SIP SMC calling
convention and async mailbox ABI the driver already implements, so no
new transport mechanism is required.

COMMAND_FCS_RANDOM_NUMBER_GEN_EXT is dispatched as
INTEL_SIP_SMC_ASYNC_FCS_RANDOM_NUMBER_EXT. The session ID and context
ID are taken from the client message. The destination buffer is the
physical address of the service-layer payload, and the payload length is
the number of random bytes requested (4080 bytes at most). On completion
the value returned in a2 is passed back to the client, as for the other
asynchronous FCS commands.

This is a prerequisite for the SoCFPGA FCS hw_random driver, the in-tree
consumer of this command.

Signed-off-by: Hang Suan Wang <hang.suan.wang@altera.com>
---
 drivers/firmware/stratix10-svc.c              |  8 ++++
 include/linux/firmware/intel/stratix10-smc.h  | 46 +++++++++++++++++++
 .../firmware/intel/stratix10-svc-client.h     |  1 +
 3 files changed, 55 insertions(+)

diff --git a/drivers/firmware/stratix10-svc.c b/drivers/firmware/stratix10-svc.c
index 386673bc093f..d6d1cf19d5a5 100644
--- a/drivers/firmware/stratix10-svc.c
+++ b/drivers/firmware/stratix10-svc.c
@@ -1643,6 +1643,13 @@ int stratix10_svc_async_send(struct stratix10_svc_chan *chan, void *msg,
 		args.a0 = INTEL_SIP_SMC_ASYNC_FCS_CLOSE_CS_SESSION;
 		args.a2 = p_msg->arg[0];
 		break;
+	case COMMAND_FCS_RANDOM_NUMBER_GEN_EXT:
+		args.a0 = INTEL_SIP_SMC_ASYNC_FCS_RANDOM_NUMBER_EXT;
+		args.a2 = p_msg->arg[0];
+		args.a3 = p_msg->arg[1];
+		args.a4 = svc_va_to_pa(ctrl, p_msg->payload);
+		args.a5 = p_msg->payload_length;
+		break;
 	case COMMAND_FCS_SDOS_DATA_EXT:
 		args.a0 = INTEL_SIP_SMC_ASYNC_FCS_CRYPTION_EXT;
 		args.a2 = p_msg->arg[0];
@@ -1757,6 +1764,7 @@ static int stratix10_svc_async_prepare_response(struct stratix10_svc_chan *chan,
 	case COMMAND_RSU_NOTIFY:
 		break;
 	case COMMAND_FCS_CRYPTO_OPEN_SESSION:
+	case COMMAND_FCS_RANDOM_NUMBER_GEN_EXT:
 	case COMMAND_FCS_SDOS_DATA_EXT:
 		data->kaddr1 = (void *)&handle->res.a2;
 		break;
diff --git a/include/linux/firmware/intel/stratix10-smc.h b/include/linux/firmware/intel/stratix10-smc.h
index fb01116c1cb9..601f14bc8e29 100644
--- a/include/linux/firmware/intel/stratix10-smc.h
+++ b/include/linux/firmware/intel/stratix10-smc.h
@@ -669,6 +669,52 @@ INTEL_SIP_SMC_FAST_CALL_VAL(INTEL_SIP_SMC_FUNCID_FPGA_CONFIG_COMPLETED_WRITE)
 #define INTEL_SIP_SMC_FCS_GET_PROVISION_DATA \
 	INTEL_SIP_SMC_STD_CALL_VAL(INTEL_SIP_SMC_FUNCID_FCS_GET_PROVISION_DATA)
 
+/**
+ * Request INTEL_SIP_SMC_FCS_RANDOM_NUMBER_EXT
+ *
+ * Call used to query the random number generated by the firmware.
+ * The maximum random number is 4080 bytes.
+ *
+ * Call register usage:
+ * a0 INTEL_SIP_SMC_FCS_RANDOM_NUMBER_EXT
+ * a1 session ID
+ * a2 context ID
+ * a3 size of the requested random data
+ * a4-a7 not used
+ *
+ * Return status:
+ * a0 INTEL_SIP_SMC_STATUS_OK, INTEL_SIP_SMC_STATUS_NOT_SUPPORTED or
+ *    INTEL_SIP_SMC_STATUS_ERROR
+ * a1-a3 not used
+ */
+#define INTEL_SIP_SMC_FUNCID_FCS_RANDOM_NUMBER_EXT 143
+#define INTEL_SIP_SMC_FCS_RANDOM_NUMBER_EXT \
+	INTEL_SIP_SMC_STD_CALL_VAL(INTEL_SIP_SMC_FUNCID_FCS_RANDOM_NUMBER_EXT)
+
+/**
+ * Request INTEL_SIP_SMC_ASYNC_FCS_RANDOM_NUMBER_EXT
+ *
+ * Async call used to query the random number generated by the firmware.
+ * The maximum random number is 4080 bytes.
+ *
+ * Call register usage:
+ * a0 INTEL_SIP_SMC_ASYNC_FCS_RANDOM_NUMBER_EXT
+ * a1 transaction job id
+ * a2 session ID
+ * a3 context ID
+ * a4 buffer pointer for random data
+ * a5 size of the requested random data
+ * a6-a17 not used
+ *
+ * Return status:
+ * a0 INTEL_SIP_SMC_STATUS_OK ,INTEL_SIP_SMC_STATUS_REJECTED
+ * or INTEL_SIP_SMC_STATUS_BUSY
+ * a1-a17 not used
+ */
+#define INTEL_SIP_SMC_ASYNC_FUNC_ID_FCS_RANDOM_NUMBER_EXT (0x12D)
+#define INTEL_SIP_SMC_ASYNC_FCS_RANDOM_NUMBER_EXT \
+	INTEL_SIP_SMC_ASYNC_VAL(INTEL_SIP_SMC_ASYNC_FUNC_ID_FCS_RANDOM_NUMBER_EXT)
+
 /**
  * Request INTEL_SIP_SMC_ASYNC_FCS_CRYPTION_EXT
  * Async call to perform encryption/decryption
diff --git a/include/linux/firmware/intel/stratix10-svc-client.h b/include/linux/firmware/intel/stratix10-svc-client.h
index edd7953d5d08..c924dc9a6ef2 100644
--- a/include/linux/firmware/intel/stratix10-svc-client.h
+++ b/include/linux/firmware/intel/stratix10-svc-client.h
@@ -202,6 +202,7 @@ enum stratix10_svc_command_code {
 	/* for crypto service */
 	COMMAND_FCS_CRYPTO_OPEN_SESSION = 50,
 	COMMAND_FCS_CRYPTO_CLOSE_SESSION,
+	COMMAND_FCS_RANDOM_NUMBER_GEN_EXT = 81,
 	/* for extended SDOS encrypt/decrypt */
 	COMMAND_FCS_SDOS_DATA_EXT = 82,
 	/* for generic mailbox send command */
-- 
2.43.7


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

* [PATCH v1 2/2] hwrng: socfpga-fcs - Add FCS random number generator driver
  2026-11-20  9:51 [PATCH v1 0/2] Add FCS Random Number Generator (RNG) driver hang.suan.wang
  2026-11-20  9:51 ` [PATCH v1 1/2] firmware: stratix10-svc: add FCS random-number command for Agilex 5 hang.suan.wang
@ 2026-11-20  9:51 ` hang.suan.wang
  1 sibling, 0 replies; 3+ messages in thread
From: hang.suan.wang @ 2026-11-20  9:51 UTC (permalink / raw)
  To: Dinh Nguyen, linux-kernel, Olivia Mackall, Herbert Xu,
	Eric Biggers, Miquel Raynal, Wolfram Sang, Frank Li,
	Jean-Michel Hautbois, linux-crypto
  Cc: muhammad.nazim.amirul.nazle.asmade, tze.yee.ng, chee.nouk.phoon,
	genevieve.chan, adrian.ho.yin.ng

From: Hang Suan Wang <hang.suan.wang@altera.com>

Add a hw_random front-end for the SoCFPGA FCS random-number service.
The driver owns no session or service-layer state: every request goes
through the FCS core, which serializes access to the SDM crypto session
and forwards it to the SDM through ATF.

The Agilex FPGA series generator is an SP 800-90A compliant DRNG hardware
block in the SDM, in particular, Agilex 7 is SP 800-90A compliant DRBG
generation flow and is seeded with the TRNG hardware block.

Unlike existing hw_random drivers, this RNG produces data only if SDM
crypto session is open (for example via FCS_IOC_OPEN_SESSION
on /dev/socfpga-fcs). The session is held by the FCS service, not by this
driver. With no session open, socfpga_fcs_core_engine() returns -ENOTCONN
and the error is passed through unchanged: read() on /dev/hwrng fails with
it, and the hw_random entropy thread treats it as a transient failure and
retries.

The ATF accepts only a non-zero, 4-byte-aligned length, from 4 bytes up to
4080 bytes per request.

The hwrng.quality is left unset. The hw_random core treats 0 as the maximum
(1024 bits of entropy per 1024 bits of input), which matches a
conditioned SP 800-90A DRNG, so the driver claims no separate estimate.

Signed-off-by: Hang Suan Wang <hang.suan.wang@altera.com>
---
 MAINTAINERS                                |   1 +
 drivers/char/hw_random/Kconfig             |  12 ++
 drivers/char/hw_random/Makefile            |   1 +
 drivers/char/hw_random/socfpga-fcs-rng.c   | 133 +++++++++++++++++++++
 drivers/firmware/socfpga-fcs-core.c        |  71 +++++++----
 drivers/firmware/socfpga-fcs.c             |  33 +++++
 include/linux/firmware/intel/socfpga-fcs.h |  23 ++--
 7 files changed, 245 insertions(+), 29 deletions(-)
 create mode 100644 drivers/char/hw_random/socfpga-fcs-rng.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 8fed8012195a..52e8650d22eb 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -981,6 +981,7 @@ L:	linux-kernel@vger.kernel.org
 S:	Maintained
 F:	Documentation/ABI/testing/sysfs-devices-platform-stratix10-fcs
 F:	Documentation/misc-devices/socfpga-fcs.rst
+F:	drivers/char/hw_random/socfpga-fcs-rng.c
 F:	drivers/firmware/socfpga-fcs*
 F:	include/linux/firmware/intel/socfpga-fcs*
 F:	include/uapi/misc/socfpga-fcs*
diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
index d3108991c251..9ebb52536854 100644
--- a/drivers/char/hw_random/Kconfig
+++ b/drivers/char/hw_random/Kconfig
@@ -648,6 +648,18 @@ config HW_RANDOM_QCOM
 	  To compile this driver as a module, choose M here. The
 	  module will be called qcom-rng. If unsure, say N.
 
+config HW_RANDOM_SOCFPGA_FCS
+	tristate "Altera SoCFPGA FCS Random Number Generator support"
+	depends on ALTERA_SOCFPGA_FCS
+	help
+	  This driver provides support for the random number generator
+	  of the Altera SoCFPGA FPGA Crypto Service (FCS) in the Secure
+	  Device Manager, through the FCS core engine. It can only be
+	  used while an FCS crypto session is open.
+
+	  To compile this driver as a module, choose M here. The
+	  module will be called socfpga-fcs-rng. If unsure, say N.
+
 endif # HW_RANDOM
 
 config UML_RANDOM
diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
index 8fce2aa6cf7d..8496471d568b 100644
--- a/drivers/char/hw_random/Makefile
+++ b/drivers/char/hw_random/Makefile
@@ -55,3 +55,4 @@ obj-$(CONFIG_HW_RANDOM_ROCKCHIP) += rockchip-rng.o
 obj-$(CONFIG_HW_RANDOM_JH7110) += jh7110-trng.o
 obj-$(CONFIG_HW_RANDOM_XILINX) += xilinx-trng.o
 obj-$(CONFIG_HW_RANDOM_QCOM) += qcom-rng.o
+obj-$(CONFIG_HW_RANDOM_SOCFPGA_FCS) += socfpga-fcs-rng.o
diff --git a/drivers/char/hw_random/socfpga-fcs-rng.c b/drivers/char/hw_random/socfpga-fcs-rng.c
new file mode 100644
index 000000000000..17b91b5d63fb
--- /dev/null
+++ b/drivers/char/hw_random/socfpga-fcs-rng.c
@@ -0,0 +1,133 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Altera SoCFPGA hardware random number generator driver
+ *
+ * Copyright (c) 2026 Altera Corporation
+ *
+ * Front-end for the FCS random number service. This driver contains no
+ * session, locking or service-layer logic: the FCS core
+ * (drivers/firmware/socfpga-fcs-core.c) is the sole owner of the SDM
+ * crypto session and serializes every request. The RNG can only be
+ * used while a session is open (e.g. via FCS_IOC_OPEN_SESSION on
+ * /dev/socfpga-fcs); socfpga_fcs_core_engine() reports -ENOTCONN otherwise.
+ * The error is passed through unchanged. The hwrng core's background
+ * entropy thread treats it as a transient failure and retries after a
+ * delay, while a read() on /dev/hwrng fails with it.
+ */
+
+#include <linux/err.h>
+#include <linux/firmware/intel/socfpga-fcs.h>
+#include <linux/hw_random.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/random.h>
+#include <linux/slab.h>
+#include <linux/unaligned.h>
+
+/* Maximum bytes returned by one FCS_DEV_RANDOM_NUMBER_GEN_EXT request. */
+#define FCS_RANDOM_EXT_MAX_BYTES	(1020 * 4)
+
+/*
+ * The Agilex family SDM prefixes the random data with a 12-byte response
+ * header that consists of session ID (4), context ID (4), crypto parameter
+ * size (2), DRNG output type (1), crypto process stage (1).
+ */
+#define FCS_AGILEX_RNG_RESP_HDR_SIZE	12
+
+/* Offset of the context ID in the response header. */
+#define FCS_AGILEX_RNG_CONTEXT_ID_OFFSET	4
+
+struct socfpga_fcs_rng {
+	struct socfpga_fcs_priv *fcs;
+	struct hwrng rng;
+};
+
+static int socfpga_fcs_rng_read(struct hwrng *rng, void *buf, size_t max,
+				bool wait)
+{
+	struct socfpga_fcs_rng *frng = container_of(rng, struct socfpga_fcs_rng,
+						    rng);
+	struct fcs_cmd_params params = { };
+	size_t len;
+	int ret;
+
+	/* ATF requires a non-zero, 4-byte aligned request size. */
+	len = min_t(size_t, max, FCS_RANDOM_EXT_MAX_BYTES) & ~3UL;
+	if (!len)
+		return -EINVAL;
+
+	params.dst = buf;
+	params.dst_len = len;
+	/*
+	 * SDM requires a non-zero context ID per request; this wrapper
+	 * generates it.
+	 */
+	params.context_id = get_random_u32_above(0);
+
+	/*
+	 * No file owns this read. NULL is accepted only for the RNG
+	 * command; the session must already be open.
+	 */
+	ret = socfpga_fcs_core_engine(frng->fcs, NULL,
+				      FCS_DEV_RANDOM_NUMBER_GEN_EXT,
+				      &params, wait);
+	if (ret)
+		return ret;
+
+	if (params.status || params.dst_len <= FCS_AGILEX_RNG_RESP_HDR_SIZE)
+		return -EIO;
+
+	if (get_unaligned_le32(buf + FCS_AGILEX_RNG_CONTEXT_ID_OFFSET) !=
+	    params.context_id)
+		return -EIO;
+
+	/* Pass random number data to the caller */
+	params.dst_len -= FCS_AGILEX_RNG_RESP_HDR_SIZE;
+	memmove(buf, buf + FCS_AGILEX_RNG_RESP_HDR_SIZE, params.dst_len);
+
+	return params.dst_len;
+}
+
+static void socfpga_fcs_rng_put(void *data)
+{
+	socfpga_fcs_put(data);
+}
+
+static int socfpga_fcs_rng_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct socfpga_fcs_rng *frng;
+	int ret;
+
+	frng = devm_kzalloc(dev, sizeof(*frng), GFP_KERNEL);
+	if (!frng)
+		return -ENOMEM;
+
+	/* The parent (stratix10-fcs) publishes its state as drvdata. */
+	frng->fcs = dev_get_drvdata(dev->parent);
+	if (!frng->fcs)
+		return -EPROBE_DEFER;
+
+	socfpga_fcs_get(frng->fcs);
+	ret = devm_add_action_or_reset(dev, socfpga_fcs_rng_put, frng->fcs);
+	if (ret)
+		return ret;
+
+	frng->rng.name = "socfpga-fcs-rng";
+	frng->rng.read = socfpga_fcs_rng_read;
+
+	return devm_hwrng_register(dev, &frng->rng);
+}
+
+static struct platform_driver socfpga_fcs_rng_driver = {
+	.driver = {
+		.name = "socfpga-fcs-rng",
+	},
+	.probe = socfpga_fcs_rng_probe,
+};
+module_platform_driver(socfpga_fcs_rng_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Hang Suan Wang <hang.suan.wang@altera.com>");
+MODULE_DESCRIPTION("Altera SoCFPGA FCS Random Number Generator (RNG) driver");
+MODULE_ALIAS("platform:socfpga-fcs-rng");
diff --git a/drivers/firmware/socfpga-fcs-core.c b/drivers/firmware/socfpga-fcs-core.c
index 365cafb46545..b40877d3782d 100644
--- a/drivers/firmware/socfpga-fcs-core.c
+++ b/drivers/firmware/socfpga-fcs-core.c
@@ -99,7 +99,8 @@ static int fcs_svc_send_sync(struct socfpga_fcs_priv *priv,
  * @msg: service-layer message to send
  * @timeout: time to wait for the response
  *
- * Return: 0 on success, negative errno on failure.
+ * Return: 0 once the transaction completed, negative errno on transport
+ *         failure or timeout.
  */
 static int fcs_svc_send_async(struct socfpga_fcs_priv *priv,
 			      struct stratix10_svc_client_msg *msg,
@@ -238,6 +239,22 @@ static int fcs_svc_send_request(struct socfpga_fcs_priv *priv,
 		msg->command = COMMAND_FCS_SDOS_DATA_EXT;
 		break;
 
+	case FCS_DEV_RANDOM_NUMBER_GEN_EXT:
+		if (!params) {
+			ret = -EINVAL;
+			break;
+		}
+		dev_dbg(priv->client.dev, "Sending command: COMMAND_FCS_RANDOM_NUMBER_GEN_EXT with session_id: 0x%x, context_id: 0x%x, len: 0x%x\n",
+			priv->session_id, params->context_id,
+			params->dst_len);
+		msg->arg[0] = priv->session_id;
+		msg->arg[1] = params->context_id;
+		/* The svc layer passes msg->payload as the destination. */
+		msg->payload = params->dst;
+		msg->payload_length = params->dst_len;
+		msg->command = COMMAND_FCS_RANDOM_NUMBER_GEN_EXT;
+		break;
+
 	default:
 		dev_err(priv->client.dev, "Unknown command: 0x%x\n", command);
 		ret = -EINVAL;
@@ -257,17 +274,30 @@ static int fcs_svc_send_request(struct socfpga_fcs_priv *priv,
 	return ret;
 }
 
+/*
+ * fcs_non_owner_command() - commands that may run without the session owner
+ *
+ * The caller does not hold the file that opened the session. Every other
+ * command must be issued by that file.
+ */
+static bool fcs_non_owner_command(enum fcs_command_code command)
+{
+	return command == FCS_DEV_RANDOM_NUMBER_GEN_EXT;
+}
+
 /*
  * fcs_seq_begin() - start a serialized FCS request
  *
  * Takes @priv->lock and checks the device is present, a session is
- * open and @owner owns it.
+ * open, and @owner may run @command. A caller that is not the session
+ * owner is accepted only for commands from fcs_non_owner_command().
+ * Any other caller must be the file that opened the session.
  *
  * Return: 0 on success, with @priv->lock held; negative errno on
  * failure, with the lock dropped.
  */
-static int fcs_seq_begin(struct socfpga_fcs_priv *priv,
-			 const void *owner, bool wait)
+static int fcs_seq_begin(struct socfpga_fcs_priv *priv, const void *owner,
+			 enum fcs_command_code command, bool wait)
 {
 	int ret;
 
@@ -288,7 +318,12 @@ static int fcs_seq_begin(struct socfpga_fcs_priv *priv,
 		goto unlock;
 	}
 
-	if (priv->session_owner != owner) {
+	if (!owner) {
+		if (!fcs_non_owner_command(command)) {
+			ret = -EPERM;
+			goto unlock;
+		}
+	} else if (priv->session_owner != owner) {
 		ret = -EPERM;
 		goto unlock;
 	}
@@ -327,18 +362,6 @@ static int fcs_engine_step_locked(struct socfpga_fcs_priv *priv,
 	if (!params->context_id)
 		return -EINVAL;
 
-	/*
-	 * Copy-out below treats the firmware response value as a byte
-	 * count, which is only defined for SDOS. Reject
-	 * an output buffer for any other command instead of silently
-	 * misinterpreting that value.
-	 */
-	if (params->dst && command != FCS_DEV_SDOS_DATA_EXT) {
-		dev_err(priv->client.dev,
-			"copy-out not supported for command %d\n", command);
-		return -EINVAL;
-	}
-
 	params->status = 0;
 	params->status_valid = false;
 
@@ -393,14 +416,15 @@ static int fcs_engine_step_locked(struct socfpga_fcs_priv *priv,
 /**
  * socfpga_fcs_core_engine() - run one FCS command
  * @priv: state returned by fcs_init()
- * @owner: token passed to fcs_ctx_begin() when the session was opened
+ * @owner: token passed to fcs_ctx_begin() when the session was opened.
+ *         NULL is accepted only for FCS_DEV_RANDOM_NUMBER_GEN_EXT.
  * @command: FCS command code to send
  * @params: command parameters, with a caller-generated context ID
  * @wait: true to wait for the lock, false to return -EAGAIN if busy
  *
- * Takes @priv->lock, checks the session, sends the command and copies
- * the output back. The SDM status is stored in @params->status, valid
- * only when @params->status_valid is set.
+ * Takes @priv->lock, checks the session and the owner, sends the command
+ * and copies the output back. The SDM status is stored in
+ * @params->status, valid only when @params->status_valid is set.
  *
  * Return: 0 if the transaction completed, negative errno otherwise.
  */
@@ -410,7 +434,7 @@ int socfpga_fcs_core_engine(struct socfpga_fcs_priv *priv, const void *owner,
 {
 	int ret;
 
-	ret = fcs_seq_begin(priv, owner, wait);
+	ret = fcs_seq_begin(priv, owner, command, wait);
 	if (ret)
 		return ret;
 
@@ -419,6 +443,7 @@ int socfpga_fcs_core_engine(struct socfpga_fcs_priv *priv, const void *owner,
 
 	return ret;
 }
+EXPORT_SYMBOL_GPL(socfpga_fcs_core_engine);
 
 /**
  * fcs_open_session_locked() - open a crypto session on the SDM
@@ -771,6 +796,7 @@ void socfpga_fcs_get(struct socfpga_fcs_priv *priv)
 {
 	kref_get(&priv->refcount);
 }
+EXPORT_SYMBOL_GPL(socfpga_fcs_get);
 
 /**
  * socfpga_fcs_put() - drop a reference on the FCS state
@@ -780,6 +806,7 @@ void socfpga_fcs_put(struct socfpga_fcs_priv *priv)
 {
 	kref_put(&priv->refcount, fcs_kref_release);
 }
+EXPORT_SYMBOL_GPL(socfpga_fcs_put);
 
 /**
  * fcs_mark_removed() - refuse further operations after the device is gone
diff --git a/drivers/firmware/socfpga-fcs.c b/drivers/firmware/socfpga-fcs.c
index c4a0c96ccca9..51812d409304 100644
--- a/drivers/firmware/socfpga-fcs.c
+++ b/drivers/firmware/socfpga-fcs.c
@@ -235,6 +235,30 @@ static int fcs_driver_probe(struct platform_device *pdev)
 		return dev_err_probe(dev, ret, "Failed to register misc device\n");
 	}
 
+	/*
+	 * Spawn the hwrng front-end as a child device. It discovers this
+	 * state through dev_get_drvdata(dev->parent) and only ever calls
+	 * socfpga_fcs_core_engine(); a failure here is non-fatal, SDOS keeps
+	 * working without the RNG.
+	 */
+	if (!IS_ENABLED(CONFIG_HW_RANDOM_SOCFPGA_FCS))
+		return 0;
+
+	priv->rng_pdev = platform_device_alloc("socfpga-fcs-rng",
+					       PLATFORM_DEVID_NONE);
+	if (!priv->rng_pdev) {
+		dev_warn(dev, "failed to allocate socfpga-fcs-rng device\n");
+	} else {
+		priv->rng_pdev->dev.parent = dev;
+		ret = platform_device_add(priv->rng_pdev);
+		if (ret) {
+			dev_warn(dev, "failed to add socfpga-fcs-rng device: %d\n",
+				 ret);
+			platform_device_put(priv->rng_pdev);
+			priv->rng_pdev = NULL;
+		}
+	}
+
 	return 0;
 }
 
@@ -246,6 +270,12 @@ static void fcs_driver_remove(struct platform_device *pdev)
 {
 	struct socfpga_fcs_priv *priv = platform_get_drvdata(pdev);
 
+	/* Unregister the hwrng child first so it stops calling the engine. */
+	if (priv->rng_pdev) {
+		platform_device_unregister(priv->rng_pdev);
+		priv->rng_pdev = NULL;
+	}
+
 	/*
 	 * misc_deregister() does not wait for open files. Tear the svc
 	 * channel down here (parent may free it as soon as remove returns);
@@ -295,3 +325,6 @@ MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("Altera SoCFPGA FCS SDOS encrypt/decrypt driver");
 MODULE_AUTHOR("Altera Corporation");
 MODULE_ALIAS("platform:stratix10-fcs");
+#if IS_ENABLED(CONFIG_HW_RANDOM_SOCFPGA_FCS)
+MODULE_SOFTDEP("post: socfpga-fcs-rng");
+#endif
diff --git a/include/linux/firmware/intel/socfpga-fcs.h b/include/linux/firmware/intel/socfpga-fcs.h
index 4d2d9f538b16..8ddc5d434e3c 100644
--- a/include/linux/firmware/intel/socfpga-fcs.h
+++ b/include/linux/firmware/intel/socfpga-fcs.h
@@ -57,8 +57,10 @@ struct fcs_sdos_req {
  *                         socfpga_fcs_core_engine()
  * @src: kernel input buffer, or NULL for output-only commands
  * @src_len: bytes in @src
- * @dst: kernel output buffer, or NULL for input-only commands; only
- *       FCS_DEV_SDOS_DATA_EXT supports one in this series
+ * @dst: kernel output buffer, or NULL for input-only commands.
+ *       FCS_DEV_SDOS_DATA_EXT and FCS_DEV_RANDOM_NUMBER_GEN_EXT use one;
+ *       the engine treats the firmware response value as the number of
+ *       bytes produced.
  * @dst_len: on entry the capacity of @dst, on return the bytes produced
  * @context_id: SDM context ID, generated by the caller
  * @arg: per-command scalars, passed as message arg[2]/arg[3]
@@ -85,6 +87,8 @@ struct socfpga_fcs_priv {
 	struct stratix10_svc_chan *chan;
 	struct stratix10_svc_client client;
 	struct miscdevice miscdev;
+	/* Child device for the hwrng front-end, owned by socfpga-fcs.c. */
+	struct platform_device *rng_pdev;
 	/*
 	 * Held by the driver and by every open file. An fd may outlive
 	 * driver detach, so this state is not devm-managed: the firmware
@@ -114,8 +118,9 @@ struct socfpga_fcs_priv {
 	bool session_opened;
 	/*
 	 * Opaque token of whoever opened the session (the struct file for
-	 * the ioctl front end); guarded by @lock. Only the owner may run
-	 * commands on the session or close it. Compared, never dereferenced.
+	 * the ioctl front end); guarded by @lock. Only that owner may run
+	 * ioctl commands on the session or close it. A NULL owner is
+	 * accepted only for the RNG command. Compared, never dereferenced.
 	 */
 	const void *session_owner;
 	u32 atf_version[3];
@@ -126,6 +131,7 @@ enum fcs_command_code {
 	FCS_DEV_CRYPTO_OPEN_SESSION,
 	FCS_DEV_CRYPTO_CLOSE_SESSION,
 	FCS_DEV_SDOS_DATA_EXT,
+	FCS_DEV_RANDOM_NUMBER_GEN_EXT,
 	FCS_DEV_ATF_VERSION,
 };
 
@@ -144,15 +150,18 @@ void fcs_mark_removed(struct socfpga_fcs_priv *priv);
 int fcs_get_atf_version(struct socfpga_fcs_priv *priv, u32 *version);
 
 /*
- * Open/close a session. Only one session may be open, and only its
- * owner may use or close it. Each takes @priv->lock itself.
+ * Open/close a session. Only one session may be open, and only the same
+ * owner that opened the session can close the session. Each takes @priv->lock
+ * itself.
  */
 int fcs_ctx_begin(struct socfpga_fcs_priv *priv, const void *owner);
 int fcs_ctx_end(struct socfpga_fcs_priv *priv, const void *owner);
 
 /*
  * Run one FCS command on the core engine. @owner must be the session
- * owner; see the kernel-doc in socfpga-fcs-core.c.
+ * owner, except for FCS_DEV_RANDOM_NUMBER_GEN_EXT, which accepts NULL.
+ * The session must already be open. See the kernel-doc in
+ * socfpga-fcs-core.c.
  */
 int socfpga_fcs_core_engine(struct socfpga_fcs_priv *priv, const void *owner,
 			    enum fcs_command_code command,
-- 
2.43.7


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

end of thread, other threads:[~2026-11-20  9:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20  9:51 [PATCH v1 0/2] Add FCS Random Number Generator (RNG) driver hang.suan.wang
2026-11-20  9:51 ` [PATCH v1 1/2] firmware: stratix10-svc: add FCS random-number command for Agilex 5 hang.suan.wang
2026-11-20  9:51 ` [PATCH v1 2/2] hwrng: socfpga-fcs - Add FCS random number generator driver hang.suan.wang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®