From: Ilias Apalodimas <ilias.apalodimas@linaro.org>
To: u-boot@lists.denx.de
Subject: [PATCH v3 8/8] virtio: rng: Add a random number generator(rng) driver
Date: Fri, 13 Dec 2019 14:21:04 +0200 [thread overview]
Message-ID: <20191213122104.GA28025@apalos.home> (raw)
In-Reply-To: <1576221267-5948-9-git-send-email-sughosh.ganu@linaro.org>
On Fri, Dec 13, 2019 at 12:44:27PM +0530, Sughosh Ganu wrote:
> 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_VIRTIO
> CONFIG_DM_RNG
> CONFIG_VIRTIO_PCI
> CONFIG_VIRTIO_RNG
>
> Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
> ---
>
> drivers/virtio/Kconfig | 6 ++++
> drivers/virtio/Makefile | 1 +
> drivers/virtio/virtio-uclass.c | 1 +
> drivers/virtio/virtio_rng.c | 72 ++++++++++++++++++++++++++++++++++++++++++
> include/virtio.h | 4 ++-
> 5 files changed, 83 insertions(+), 1 deletion(-)
> create mode 100644 drivers/virtio/virtio_rng.c
>
> diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
> index a9d5fd0..2e3dd3b 100644
> --- a/drivers/virtio/Kconfig
> +++ b/drivers/virtio/Kconfig
> @@ -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
> diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile
> index 4579044..dc88809 100644
> --- a/drivers/virtio/Makefile
> +++ b/drivers/virtio/Makefile
> @@ -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
> diff --git a/drivers/virtio/virtio-uclass.c b/drivers/virtio/virtio-uclass.c
> index 34397d7..436faa4 100644
> --- a/drivers/virtio/virtio-uclass.c
> +++ b/drivers/virtio/virtio-uclass.c
> @@ -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,
> diff --git a/drivers/virtio/virtio_rng.c b/drivers/virtio/virtio_rng.c
> new file mode 100644
> index 0000000..19a0cc1
> --- /dev/null
> +++ b/drivers/virtio/virtio_rng.c
> @@ -0,0 +1,72 @@
> +// 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>
> +
> +struct virtio_rng_priv {
> + struct virtqueue *rng_vq;
> +};
> +
> +static int virtio_rng_read(struct udevice *dev, void *data, size_t len)
> +{
> + struct virtio_sg sg = { data, len };
> + struct virtio_sg *sgs[] = { &sg };
> + struct virtio_rng_priv *priv = dev_get_priv(dev);
> + unsigned int rsize;
> + int ret;
> +
> + 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))
> + ;
> +
> + return rsize;
> +}
> +
> +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)
> + 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,
> +};
> diff --git a/include/virtio.h b/include/virtio.h
> index 654fdf1..561dcc3 100644
> --- a/include/virtio.h
> +++ b/include/virtio.h
> @@ -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 */
>
> --
> 2.7.4
>
Tested-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
prev parent reply other threads:[~2019-12-13 12:21 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-13 7:14 [PATCH v3 0/8] Add a random number generator uclass Sughosh Ganu
2019-12-13 7:14 ` [PATCH v3 1/8] dm: rng: Add random number generator(rng) uclass Sughosh Ganu
2019-12-13 12:21 ` Ilias Apalodimas
2019-12-16 11:59 ` Patrick DELAUNAY
2019-12-16 12:13 ` Patrick DELAUNAY
2019-12-16 18:55 ` Sughosh Ganu
2019-12-13 7:14 ` [PATCH v3 2/8] clk: stm32mp1: Add a clock entry for RNG1 device Sughosh Ganu
2019-12-16 8:55 ` Patrick DELAUNAY
2019-12-13 7:14 ` [PATCH v3 3/8] stm32mp1: rng: Add a driver for random number generator(rng) device Sughosh Ganu
2019-12-16 11:58 ` Patrick DELAUNAY
2019-12-13 7:14 ` [PATCH v3 4/8] configs: stm32mp15: Enable " Sughosh Ganu
2019-12-16 12:00 ` Patrick DELAUNAY
2019-12-13 7:14 ` [PATCH v3 5/8] sandbox: rng: Add a random number generator(rng) driver Sughosh Ganu
2019-12-16 12:30 ` Patrick DELAUNAY
2019-12-16 18:59 ` Sughosh Ganu
2019-12-13 7:14 ` [PATCH v3 6/8] configs: sandbox: Enable random number generator(rng) device Sughosh Ganu
2019-12-16 12:07 ` Patrick DELAUNAY
2019-12-13 7:14 ` [PATCH v3 7/8] test: rng: Add basic test for random number generator(rng) uclass Sughosh Ganu
2019-12-16 12:42 ` Patrick DELAUNAY
2019-12-16 19:01 ` Sughosh Ganu
2019-12-13 7:14 ` [PATCH v3 8/8] virtio: rng: Add a random number generator(rng) driver Sughosh Ganu
2019-12-13 12:21 ` Ilias Apalodimas [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20191213122104.GA28025@apalos.home \
--to=ilias.apalodimas@linaro.org \
--cc=u-boot@lists.denx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox