Add a random number generator uclass
Subsystems (3)
v7 cover Add a random number generator uclass
Add a random number generator(rng) uclass to facilitate adding drivers for rng devices. I plan to add an implementation of the EFI_RNG_PROTOCOL, which would get the random number from the rng uclass -- the protocol would be used by the efi stub for getting a random number for the kaslr feature.
The patch series also adds a driver for the rng device found on the stm32mp1 and qemu platforms. A dummy rng driver for sandbox has also been added, along with the unit test for the rng uclass.
Changes since V6:
* Handle review comments from Heinrich Schuchardt to handle the scenario of data buffer being passed to the virtio rng driver not being aligned on 4 bytes.
* Handle review comment by Simon Glass to only keep a forward declaration of struct udevice in rng.h.
Changes since V5:
* Handle review comments from Heinrich Schuchardt to read all the bytes requested in the individual drivers.
Changes since V4:
* Get the read functions of individual drivers to return number of bytes read on successful read, and a -ve value on error.
* Handle review comments from Heinrich Schuchardt.
Changes since V3:
* Handle review comments from Patrick Delaunay
Changes since V2:
* Add a driver for the virtio-rng device on qemu platform
Changes since V1:
* Add a SPDX header in rng.h
* Change the UCLASS_DRIVER name from hwrng to rng, consistent with the rest of the naming convention
* Handle review comment from Patrice Chotard
Sughosh Ganu (8):
dm: rng: Add random number generator(rng) uclass
clk: stm32mp1: Add a clock entry for RNG1 device
stm32mp1: rng: Add a driver for random number generator(rng) device
configs: stm32mp15: Enable random number generator(rng) device
sandbox: rng: Add a random number generator(rng) driver
configs: sandbox: Enable random number generator(rng) device
test: rng: Add basic test for random number generator(rng) uclass
virtio: rng: Add a random number generator(rng) driver
arch/sandbox/dts/test.dts | 4 + configs/sandbox64_defconfig | 2 + configs/sandbox_defconfig | 2 + configs/stm32mp15_basic_defconfig | 2 + configs/stm32mp15_optee_defconfig | 2 + configs/stm32mp15_trusted_defconfig | 2 + drivers/Kconfig | 2 + drivers/Makefile | 1 + drivers/clk/clk_stm32mp1.c | 1 + drivers/rng/Kconfig | 22 +++++ drivers/rng/Makefile | 8 ++ drivers/rng/rng-uclass.c | 23 ++++++ drivers/rng/sandbox_rng.c | 56 +++++++++++++ drivers/rng/stm32mp1_rng.c | 161 ++++++++++++++++++++++++++++++++++++ drivers/virtio/Kconfig | 6 ++ drivers/virtio/Makefile | 1 + drivers/virtio/virtio-uclass.c | 1 + drivers/virtio/virtio_rng.c | 92 +++++++++++++++++++++ include/dm/uclass-id.h | 1 + include/rng.h | 33 ++++++++ include/virtio.h | 4 +- test/dm/Makefile | 1 + test/dm/rng.c | 26 ++++++ 23 files changed, 452 insertions(+), 1 deletion(-) create mode 100644 drivers/rng/Kconfig create mode 100644 drivers/rng/Makefile create mode 100644 drivers/rng/rng-uclass.c create mode 100644 drivers/rng/sandbox_rng.c create mode 100644 drivers/rng/stm32mp1_rng.c create mode 100644 drivers/virtio/virtio_rng.c create mode 100644 include/rng.h create mode 100644 test/dm/rng.c
Signature · 1 line
2.7.4
PATCH v7 1/8 dm: rng: Add random number generator(rng) uclass
Add a uclass for reading a random number seed from a random number generator device.
---
Changes since V6:
* Handle review comment from Simon Glass to declare struct udevice in rng.h instead of including dm.h
drivers/Kconfig | 2 ++ drivers/Makefile | 1 + drivers/rng/Kconfig | 7 +++++++ drivers/rng/Makefile | 6 ++++++ drivers/rng/rng-uclass.c | 23 +++++++++++++++++++++++ include/dm/uclass-id.h | 1 + include/rng.h | 33 +++++++++++++++++++++++++++++++++ 7 files changed, 73 insertions(+) create mode 100644 drivers/rng/Kconfig create mode 100644 drivers/rng/Makefile create mode 100644 drivers/rng/rng-uclass.c create mode 100644 include/rng.h
drivers/Kconfig +2 -0
@@ -90,6 +90,8 @@ source "drivers/remoteproc/Kconfig" source "drivers/reset/Kconfig" +source "drivers/rng/Kconfig" + source "drivers/rtc/Kconfig" source "drivers/scsi/Kconfig"
drivers/Makefile +1 -0
@@ -115,4 +115,5 @@ obj-$(CONFIG_W1_EEPROM) += w1-eeprom/ obj-$(CONFIG_MACH_PIC32) += ddr/microchip/ obj-$(CONFIG_DM_HWSPINLOCK) += hwspinlock/ +obj-$(CONFIG_DM_RNG) += rng/ endif
drivers/rng/Kconfig new file +7 -0
@@ -0,0 +1,7 @@ +config DM_RNG + bool "Driver support for Random Number Generator devices" + depends on DM + help + Enable driver model for random number generator(rng) devices. + This interface is used to initialise the rng device and to + read the random seed from the device.
drivers/rng/Makefile new file +6 -0
@@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0+ +# +# Copyright (c) 2019, Linaro Limited +# + +obj-$(CONFIG_DM_RNG) += rng-uclass.o
drivers/rng/rng-uclass.c new file +23 -0
@@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Copyright (c) 2019, Linaro Limited +*/ + +#include <common.h> +#include <dm.h> +#include <rng.h> + +int dm_rng_read (struct udevice *dev,void *buffer,size_t size) +{ +const struct dm_rng_ops *ops =device_get_ops (dev); + +if (!ops->read) +return -ENOSYS ; + +return ops->read (dev, buffer, size); +} + +UCLASS_DRIVER (rng) = { + .name ="rng" , + .id =UCLASS_RNG , +};
include/dm/uclass-id.h +1 -0
@@ -86,6 +86,7 @@ enum uclass_id {UCLASS_REGULATOR ,/* Regulator device */ UCLASS_REMOTEPROC ,/* Remote Processor device */ UCLASS_RESET ,/* Reset controller device */ +UCLASS_RNG ,/* Random Number Generator */ UCLASS_RTC ,/* Real time clock device */ UCLASS_SCSI ,/* SCSI device */ UCLASS_SERIAL, /* Serial UART */
include/rng.h new file +33 -0
@@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Copyright (c) 2019, Linaro Limited +*/ + +#if !defined _RNG_H_ +#define _RNG_H_ + +struct udevice ; + +/** +* dm_rng_read() - read a random number seed from the rng device +* @buffer: input buffer to put the read random seed into +* @size: number of bytes of random seed read +* +* Return: 0 if OK, -ve on error +*/ +int dm_rng_read (struct udevice *dev,void *buffer,size_t size); + +/* struct dm_rng_ops - Operations for the hwrng uclass */ +struct dm_rng_ops { +/** +* @read() - read a random number seed +* +* @data: input buffer to read the random seed +* @max: total number of bytes to read +* +* Return: 0 if OK, -ve on error +*/ +int (*read)(struct udevice *dev,void *data,size_t max); +}; + +#endif /* _RNG_H_ */
Signature · 1 line
2.7.4
PATCH v7 2/8 clk: stm32mp1: Add a clock entry for RNG1 device
In reply to Sughosh GanuAdd an entry for allowing clock enablement for the random number generator peripheral, RNG1.
--- drivers/clk/clk_stm32mp1.c | 1 + 1 file changed, 1 insertion(+)
drivers/clk/clk_stm32mp1.c +1 -0
@@ -563,6 +563,7 @@ static const struct stm32mp1_clk_gate stm32mp1_clk_gate[] = {STM32MP1_CLK_SET_CLR (RCC_MP_AHB4ENSETR ,10 ,GPIOK , _UNKNOWN_SEL),STM32MP1_CLK_SET_CLR (RCC_MP_AHB5ENSETR ,0 ,GPIOZ , _UNKNOWN_SEL), +STM32MP1_CLK_SET_CLR (RCC_MP_AHB5ENSETR ,6 ,RNG1_K , _UNKNOWN_SEL),STM32MP1_CLK_SET_CLR (RCC_MP_AHB6ENSETR ,7 ,ETHCK_K , _ETH_SEL), STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 8, ETHTX, _UNKNOWN_SEL),
Signature · 1 line
2.7.4
PATCH v7 3/8 stm32mp1: rng: Add a driver for random number generator(rng) device
In reply to Sughosh GanuAdd a driver for the rng device found on stm32mp1 platforms. The driver provides a routine for reading the random number seed from the hardware device.
--- drivers/rng/Kconfig | 7 ++ drivers/rng/Makefile | 1 + drivers/rng/stm32mp1_rng.c | 161 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 drivers/rng/stm32mp1_rng.c
drivers/rng/Kconfig +7 -0
@@ -5,3 +5,10 @@ config DM_RNG Enable driver model for random number generator(rng) devices. This interface is used to initialise the rng device and to read the random seed from the device. + +config RNG_STM32MP1 + bool "Enable random number generator for STM32MP1" + depends on ARCH_STM32MP && DM_RNG + default n + help + Enable STM32MP1 rng driver.
drivers/rng/Makefile +1 -0
@@ -4,3 +4,4 @@ # obj-$(CONFIG_DM_RNG) += rng-uclass.o +obj-$(CONFIG_RNG_STM32MP1) += stm32mp1_rng.o
drivers/rng/stm32mp1_rng.c new file +161 -0
161 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v7 4/8 configs: stm32mp15: Enable random number generator(rng) device
In reply to Sughosh GanuEnable support for the rng device on the stm32mp15 configs.
--- configs/stm32mp15_basic_defconfig | 2 ++ configs/stm32mp15_optee_defconfig | 2 ++ configs/stm32mp15_trusted_defconfig | 2 ++ 3 files changed, 6 insertions(+)
configs/stm32mp15_basic_defconfig +2 -0
@@ -143,3 +143,5 @@ CONFIG_VIDEO_STM32_DSI=y CONFIG_VIDEO_STM32_MAX_XRES=1280 CONFIG_VIDEO_STM32_MAX_YRES=800 CONFIG_FDT_FIXUP_PARTITIONS=y +CONFIG_DM_RNG=y +CONFIG_RNG_STM32MP1=y
configs/stm32mp15_optee_defconfig +2 -0
@@ -127,3 +127,5 @@ CONFIG_VIDEO_STM32_DSI=y CONFIG_VIDEO_STM32_MAX_XRES=1280 CONFIG_VIDEO_STM32_MAX_YRES=800 CONFIG_FDT_FIXUP_PARTITIONS=y +CONFIG_DM_RNG=y +CONFIG_RNG_STM32MP1=y
configs/stm32mp15_trusted_defconfig +2 -0
@@ -126,3 +126,5 @@ CONFIG_VIDEO_STM32_DSI=y CONFIG_VIDEO_STM32_MAX_XRES=1280 CONFIG_VIDEO_STM32_MAX_YRES=800 CONFIG_FDT_FIXUP_PARTITIONS=y +CONFIG_DM_RNG=y +CONFIG_RNG_STM32MP1=y
Signature · 1 line
2.7.4
PATCH v7 5/8 sandbox: rng: Add a random number generator(rng) driver
In reply to Sughosh GanuAdd a sandbox driver for random number generation. Mostly aimed at providing a unit test for rng uclass.
--- arch/sandbox/dts/test.dts | 4 ++++ drivers/rng/Kconfig | 8 +++++++ drivers/rng/Makefile | 1 + drivers/rng/sandbox_rng.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 drivers/rng/sandbox_rng.c
arch/sandbox/dts/test.dts +4 -0
@@ -599,6 +599,10 @@ reset-names = "other", "test"; }; + rng { + compatible = "sandbox,sandbox-rng"; + }; + rproc_1: rproc at 1 { compatible = "sandbox,test-processor"; remoteproc-name = "remoteproc-test-dev1";
drivers/rng/Kconfig +8 -0
@@ -6,6 +6,14 @@ config DM_RNG This interface is used to initialise the rng device and to read the random seed from the device. +config RNG_SANDBOX + bool "Sandbox random number generator" + depends on SANDBOX && DM_RNG + select CONFIG_LIB_RAND + help + Enable random number generator for sandbox. This is an + emulation of a rng device. + config RNG_STM32MP1 bool "Enable random number generator for STM32MP1" depends on ARCH_STM32MP && DM_RNG
drivers/rng/Makefile +1 -0
@@ -4,4 +4,5 @@ # obj-$(CONFIG_DM_RNG) += rng-uclass.o +obj-$(CONFIG_RNG_SANDBOX) += sandbox_rng.o obj-$(CONFIG_RNG_STM32MP1) += stm32mp1_rng.o
drivers/rng/sandbox_rng.c new file +56 -0
56 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v7 6/8 configs: sandbox: Enable random number generator(rng) device
In reply to Sughosh GanuEnable support for random number generator on sandbox configs. This is aimed primarily at adding unit test support for rng uclass.
--- configs/sandbox64_defconfig | 2 ++ configs/sandbox_defconfig | 2 ++ 2 files changed, 4 insertions(+)
configs/sandbox64_defconfig +2 -0
@@ -158,6 +158,8 @@ CONFIG_REGULATOR_RK8XX=y CONFIG_REGULATOR_S5M8767=y CONFIG_DM_REGULATOR_SANDBOX=y CONFIG_REGULATOR_TPS65090=y +CONFIG_DM_RNG=y +CONFIG_RNG_SANDBOX=y CONFIG_DM_PWM=y CONFIG_PWM_SANDBOX=y CONFIG_RAM=y
configs/sandbox_defconfig +2 -0
@@ -179,6 +179,8 @@ CONFIG_REGULATOR_RK8XX=y CONFIG_REGULATOR_S5M8767=y CONFIG_DM_REGULATOR_SANDBOX=y CONFIG_REGULATOR_TPS65090=y +CONFIG_DM_RNG=y +CONFIG_RNG_SANDBOX=y CONFIG_DM_PWM=y CONFIG_PWM_SANDBOX=y CONFIG_RAM=y
Signature · 1 line
2.7.4
PATCH v7 7/8 test: rng: Add basic test for random number generator(rng) uclass
In reply to Sughosh GanuAdd a unit test for testing the rng uclass functionality using the sandbox rng driver.
--- test/dm/Makefile | 1 + test/dm/rng.c | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 test/dm/rng.c
test/dm/Makefile +1 -0
@@ -65,4 +65,5 @@ obj-$(CONFIG_VIRTIO_SANDBOX) += virtio.o obj-$(CONFIG_DMA) += dma.o obj-$(CONFIG_DM_MDIO) += mdio.o obj-$(CONFIG_DM_MDIO_MUX) += mdio_mux.o +obj-$(CONFIG_DM_RNG) += rng.o endif
test/dm/rng.c new file +26 -0
@@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2019, Linaro Limited +*/ + +#include <common.h> +#include <dm.h> +#include <rng.h> +#include <dm/test.h> +#include <test/ut.h> + +/* Basic test of the rng uclass */ +static int dm_test_rng_read (struct unit_test_state *uts) +{ +unsigned long rand1 =0 , rand2 =0 ; +struct udevice *dev; + +ut_assertok (uclass_get_device (UCLASS_RNG ,0 , &dev)); +ut_assertnonnull (dev); +ut_assertok (dm_rng_read (dev, &rand1,sizeof (rand1))); +ut_assertok (dm_rng_read (dev, &rand2,sizeof (rand2))); +ut_assert (rand1 != rand2); + +return 0 ; +} +DM_TEST (dm_test_rng_read,DM_TESTF_SCAN_PDATA |DM_TESTF_SCAN_FDT );
Signature · 1 line
2.7.4
PATCH v7 8/8 virtio: rng: Add a random number generator(rng) driver
In reply to Sughosh GanuAdd a driver for the virtio-rng device on the qemu platform. The device uses pci as a transport medium. The driver can be enabled with the following configs
CONFIG_VIRTIOCONFIG_DM_RNGCONFIG_VIRTIO_PCICONFIG_VIRTIO_RNG
---
Changes since V6:
* Handle review comments from Heinrich Schuchardt to handle the scenario of data buffer being passed to the virtio rng driver not being aligned on 4 bytes.
drivers/virtio/Kconfig | 6 +++ drivers/virtio/Makefile | 1 + drivers/virtio/virtio-uclass.c | 1 + drivers/virtio/virtio_rng.c | 92 ++++++++++++++++++++++++++++++++++++++++++ include/virtio.h | 4 +- 5 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 drivers/virtio/virtio_rng.c
drivers/virtio/Kconfig +6 -0
@@ -59,4 +59,10 @@ config VIRTIO_BLK This is the virtual block driver for virtio. It can be used with QEMU based targets. +config VIRTIO_RNG + bool "virtio rng driver" + depends on VIRTIO + help + This is the virtual random number generator driver. It can be used + with Qemu based targets. endmenu
drivers/virtio/Makefile +1 -0
@@ -9,3 +9,4 @@ obj-$(CONFIG_VIRTIO_PCI) += virtio_pci_legacy.o virtio_pci_modern.o obj-$(CONFIG_VIRTIO_SANDBOX) += virtio_sandbox.o obj-$(CONFIG_VIRTIO_NET) += virtio_net.o obj-$(CONFIG_VIRTIO_BLK) += virtio_blk.o +obj-$(CONFIG_VIRTIO_RNG) += virtio_rng.o
drivers/virtio/virtio-uclass.c +1 -0
@@ -24,6 +24,7 @@static const char *const virtio_drv_name[VIRTIO_ID_MAX_NUM ] = { [VIRTIO_ID_NET ] =VIRTIO_NET_DRV_NAME , [VIRTIO_ID_BLOCK ] =VIRTIO_BLK_DRV_NAME , + [VIRTIO_ID_RNG ] =VIRTIO_RNG_DRV_NAME , }; int virtio_get_config(struct udevice *vdev, unsigned int offset,
drivers/virtio/virtio_rng.c new file +92 -0
92 lines not on this page · raw message
include/virtio.h +3 -1
@@ -22,10 +22,12 @@#define VIRTIO_ID_NET 1/* virtio net */ #define VIRTIO_ID_BLOCK 2/* virtio block */ -#define VIRTIO_ID_MAX_NUM 3 +#define VIRTIO_ID_RNG 4/* virtio rng */ +#define VIRTIO_ID_MAX_NUM 5#define VIRTIO_NET_DRV_NAME "virtio-net"#define VIRTIO_BLK_DRV_NAME "virtio-blk" +#define VIRTIO_RNG_DRV_NAME "virtio-rng"/* Status byte for guest to report progress, and synchronize features */
Signature · 1 line
2.7.4
PATCH v7 8/8 virtio: rng: Add a random number generator(rng) driver
Quoted 89 lines · Sughosh Ganu
Add a driver for the virtio-rng device on the qemu platform. The device uses pci as a transport medium. The driver can be enabled with the following configs
CONFIG_VIRTIOCONFIG_DM_RNGCONFIG_VIRTIO_PCICONFIG_VIRTIO_RNGSigned-off-by Sughosh Ganu <[email protected]>---
Changes since V6:
* Handle review comments from Heinrich Schuchardt to handle the scenario of data buffer being passed to the virtio rng driver not being aligned on 4 bytes.drivers/virtio/Kconfig | 6 +++ drivers/virtio/Makefile | 1 + drivers/virtio/virtio-uclass.c | 1 + drivers/virtio/virtio_rng.c | 92 ++++++++++++++++++++++++++++++++++++++++++ include/virtio.h | 4 +- 5 files changed, 103 insertions(+), 1 deletion(-)create mode 100644
drivers/virtio/virtio_rng.cdrivers/virtio/Kconfig +6
-0@@ -59,4 +59,10 @@ config VIRTIO_BLK This is the virtual block driver for virtio. It can be used with QEMU based targets. +config VIRTIO_RNG + bool "virtio rng driver" + depends on VIRTIO + help + This is the virtual random number generator driver. It can be used + with Qemu based targets. endmenudrivers/virtio/Makefile +1
-0@@ -9,3 +9,4 @@ obj-$(CONFIG_VIRTIO_PCI) += virtio_pci_legacy.o virtio_pci_modern.o obj-$(CONFIG_VIRTIO_SANDBOX) += virtio_sandbox.o obj-$(CONFIG_VIRTIO_NET) += virtio_net.o obj-$(CONFIG_VIRTIO_BLK) += virtio_blk.o +obj-$(CONFIG_VIRTIO_RNG) += virtio_rng.odrivers/virtio/virtio-uclass.c +1
-0@@ -24,6 +24,7 @@static const char *const virtio_drv_name[VIRTIO_ID_MAX_NUM ] = { [VIRTIO_ID_NET ] =VIRTIO_NET_DRV_NAME , [VIRTIO_ID_BLOCK ] =VIRTIO_BLK_DRV_NAME , + [VIRTIO_ID_RNG ] =VIRTIO_RNG_DRV_NAME , }; int virtio_get_config(struct udevice *vdev, unsigned int offset,drivers/virtio/virtio_rng.c new file +23
-0@@ -0,0 +1,92 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Copyright (c) 2019, Linaro Limited +*/ + +#include <common.h> +#include <dm.h> +#include <rng.h> +#include <virtio_types.h> +#include <virtio.h> +#include <virtio_ring.h> + +#define BUFFER_SIZE 16UL + +struct virtio_rng_priv { +struct virtqueue *rng_vq; +}; + +static int virtio_rng_read (struct udevice *dev,void *data,size_t len) +{ +int ret; +unsigned int rsize =0 ; +size_t nbytes = len;
drivers/virtio/virtio_rng.c line 33
Quoted 10 lines
drivers/virtio/virtio_rng.c new file +10
-0+unsigned char buf[BUFFER_SIZE ]__aligned(4) ; +unsigned char *ptr = data; +struct virtio_sg sg; +struct virtio_sg *sgs[1 ]; +struct virtio_rng_priv *priv =dev_get_priv (dev); + +if (!len) +return 0 ; + + do {
If you use
while(nbytes) {here you can go without the if above. This will save 4 bytes of code on ARMv8. In U-Boot we have tight code size constraints.
drivers/virtio/virtio_rng.c line 35
Quoted 2 lines
drivers/virtio/virtio_rng.c new file +2
-0+ sg.addr = buf; + sg.length = nbytes;
We must avoid buffer overruns for len > 16:
sg.length = min(nbytes, BUFFER_SIZE);
The other patches of the series look fine to me.
Best regards
Heinrich
Quoted 76 lines
drivers/virtio/virtio_rng.c new file +57
-0+ sgs[0 ] = &sg; + + ret =virtqueue_add (priv->rng_vq, sgs,0 ,1 ); +if (ret) +return ret; + +virtqueue_kick (priv->rng_vq); + +while (!virtqueue_get_buf (priv->rng_vq, &rsize)) + ; + +memcpy (ptr, buf, rsize); + nbytes -= rsize; + ptr += rsize; + }while (nbytes !=0 ); + +return 0 ; +} + +static int virtio_rng_bind (struct udevice *dev) +{ +struct virtio_dev_priv *uc_priv =dev_get_uclass_priv (dev->parent); + +/* Indicate what driver features we support */ +virtio_driver_features_init (uc_priv,NULL ,0 ,NULL ,0 ); + +return 0 ; +} + +static int virtio_rng_probe (struct udevice *dev) +{ +struct virtio_rng_priv *priv =dev_get_priv (dev); +int ret; + + ret =virtio_find_vqs (dev,1 , &priv->rng_vq); +if (ret <0 ) { +debug ("%s: virtio_find_vqs failed\n" , __func__); +return ret; + } + +return 0 ; +} + +static const struct dm_rng_ops virtio_rng_ops = { + .read = virtio_rng_read, +}; + +U_BOOT_DRIVER (virtio_rng) = { + .name =VIRTIO_RNG_DRV_NAME , + .id =UCLASS_RNG , + .bind = virtio_rng_bind, + .probe = virtio_rng_probe, + .remove = virtio_reset, + .ops = &virtio_rng_ops, + .priv_auto_alloc_size =sizeof (struct virtio_rng_priv ), + .flags =DM_FLAG_ACTIVE_DMA , +};include/virtio.h +3
-1@@ -22,10 +22,12 @@#define VIRTIO_ID_NET 1/* virtio net */ #define VIRTIO_ID_BLOCK 2/* virtio block */ -#define VIRTIO_ID_MAX_NUM 3 +#define VIRTIO_ID_RNG 4/* virtio rng */ +#define VIRTIO_ID_MAX_NUM 5#define VIRTIO_NET_DRV_NAME "virtio-net"#define VIRTIO_BLK_DRV_NAME "virtio-blk" +#define VIRTIO_RNG_DRV_NAME "virtio-rng"/* Status byte for guest to report progress, and synchronize features */
PATCH v7 8/8 virtio: rng: Add a random number generator(rng) driver
Quoted 26 lines · Heinrich Schuchardt
Quoted 16 lines · Sughosh Ganu
Add a driver for the virtio-rng device on the qemu platform. The device uses pci as a transport medium. The driver can be enabled with the following configs
CONFIG_VIRTIOCONFIG_DM_RNGCONFIG_VIRTIO_PCICONFIG_VIRTIO_RNGSigned-off-by Sughosh Ganu <[email protected]>---
Changes since V6:
* Handle review comments from Heinrich Schuchardt to handle the scenario of data buffer being passed to the virtio rng driver not being aligned on 4 bytes.Quoted 4 lines
drivers/virtio/Kconfig | 6 +++ drivers/virtio/Makefile | 1 + drivers/virtio/virtio-uclass.c | 1 + drivers/virtio/virtio_rng.c | 92++++++++++++++++++++++++++++++++++++++++++
Quoted 3 lines
include/virtio.h | 4 +- 5 files changed, 103 insertions(+), 1 deletion(-)create mode 100644
drivers/virtio/virtio_rng.c
<snip>
Quoted 42 lines
Quoted 29 lines
drivers/virtio/virtio_rng.c new file +23
-0@@ -0,0 +1,92 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Copyright (c) 2019, Linaro Limited +*/ + +#include <common.h> +#include <dm.h> +#include <rng.h> +#include <virtio_types.h> +#include <virtio.h> +#include <virtio_ring.h> + +#define BUFFER_SIZE 16UL + +struct virtio_rng_priv { +struct virtqueue *rng_vq; +}; + +static int virtio_rng_read (struct udevice *dev,void *data,size_t len) +{ +int ret; +unsigned int rsize =0 ; +size_t nbytes = len;drivers/virtio/virtio_rng.c line 33
Quoted 10 lines
drivers/virtio/virtio_rng.c new file +10
-0+unsigned char buf[BUFFER_SIZE ]__aligned(4) ; +unsigned char *ptr = data; +struct virtio_sg sg; +struct virtio_sg *sgs[1 ]; +struct virtio_rng_priv *priv =dev_get_priv (dev); + +if (!len) +return 0 ; + + do {If you use
Quoted 2 lines
while(nbytes) {Ok.
Quoted 11 lines
here you can go without the if above. This will save 4 bytes of code on ARMv8. In U-Boot we have tight code size constraints.
drivers/virtio/virtio_rng.c line 35
Quoted 2 lines
drivers/virtio/virtio_rng.c new file +2
-0+ sg.addr = buf; + sg.length = nbytes;We must avoid buffer overruns for len > 16:
sg.length =
min(nbytes, BUFFER_SIZE);
Oops. Forgot to take care of this. Will incorporate this in the next version. Also, I had sent you a unicast a couple of days back asking for the code in virtqueue_get_buf which requires a 4 byte alignment for the data pointer. The virtio32_to_cpu calls in virtqueue_get_buf are being used to reference 'id' and 'len' members of structs. I don't see the 'addr' member being used in this function. But i do see the data pointer being used in virtqueue_add function, which actually casts this to a u64 value. Can you point me to the function which actually dereferences the data pointer as a u32 pointer. Thanks a lot for your in depth review.
-sughosh